|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef ppx_scene_renderer_h |
| 16 | +#define ppx_scene_renderer_h |
| 17 | + |
| 18 | +#include "ppx/scene/scene_config.h" |
| 19 | + |
| 20 | +namespace ppx { |
| 21 | +namespace scene { |
| 22 | + |
| 23 | +struct GraphicsPipeline |
| 24 | +{ |
| 25 | + std::string idString = ""; |
| 26 | + grfx::DescriptorSetLayoutPtr descriptorSetLayout = nullptr; |
| 27 | + grfx::PipelineInterfacePtr pipelineInterface = nullptr; |
| 28 | + grfx::GraphicsPipelinePtr pipeline = nullptr; |
| 29 | +}; |
| 30 | + |
| 31 | +// ------------------------------------------------------------------------------------------------- |
| 32 | + |
| 33 | +struct ComputePipeline |
| 34 | +{ |
| 35 | + std::string idString = ""; |
| 36 | + grfx::DescriptorSetLayoutPtr descriptorSetLayout = nullptr; |
| 37 | + grfx::PipelineInterfacePtr pipelineInterface = nullptr; |
| 38 | + grfx::GraphicsPipelinePtr pipeline = nullptr; |
| 39 | +}; |
| 40 | + |
| 41 | +// ------------------------------------------------------------------------------------------------- |
| 42 | + |
| 43 | +struct RenderTargetAttachment |
| 44 | +{ |
| 45 | + grfx::ImagePtr image = nullptr; |
| 46 | + grfx::RenderTargetViewPtr renderTargetView = nullptr; |
| 47 | + grfx::SampledImageViewPtr sampledImageView = nullptr; |
| 48 | + grfx::RenderTargetClearValue clearValue = {}; |
| 49 | +}; |
| 50 | + |
| 51 | +// ------------------------------------------------------------------------------------------------- |
| 52 | + |
| 53 | +struct DepthStencilAttachment |
| 54 | +{ |
| 55 | + grfx::ImagePtr image = nullptr; |
| 56 | + grfx::DepthStencilViewPtr dephtStencilView = nullptr; |
| 57 | + grfx::SampledImageViewPtr sampledImageView = nullptr; |
| 58 | + grfx::DepthStencilClearValue clearValue = {}; |
| 59 | +}; |
| 60 | + |
| 61 | +// ------------------------------------------------------------------------------------------------- |
| 62 | + |
| 63 | +struct RenderPass |
| 64 | +{ |
| 65 | + std::string name = ""; |
| 66 | + std::vector<scene::RenderTargetAttachment> renderTargetAttachments = {}; |
| 67 | + scene::DepthStencilAttachment depthStencilAttachment = {}; |
| 68 | + grfx::RenderPassPtr renderPass = nullptr; |
| 69 | +}; |
| 70 | + |
| 71 | +// ------------------------------------------------------------------------------------------------- |
| 72 | + |
| 73 | +struct ComputePass |
| 74 | +{ |
| 75 | + const scene::GraphicsPipeline* pPipeline = nullptr; |
| 76 | + std::vector<grfx::BufferPtr> inputBuffers = {}; |
| 77 | + std::vector<grfx::SampledImageView*> inputTextures = {}; |
| 78 | + std::vector<grfx::BufferPtr> outputBuffers = {}; |
| 79 | + std::vector<grfx::StorageImageView*> outputTextures = {}; |
| 80 | +}; |
| 81 | + |
| 82 | +// ------------------------------------------------------------------------------------------------- |
| 83 | + |
| 84 | +class RenderOutput |
| 85 | +{ |
| 86 | +protected: |
| 87 | + RenderOutput( |
| 88 | + scene::Renderer* pRenderer); |
| 89 | + |
| 90 | +public: |
| 91 | + virtual ~RenderOutput(); |
| 92 | + |
| 93 | + scene::Renderer* GetRenderer() const { return mRenderer; } |
| 94 | + |
| 95 | + virtual ppx::Result GetRenderTargetImage( |
| 96 | + grfx::Image** ppImage, |
| 97 | + grfx::Semaphore* pImageReadySemaphore) = 0; |
| 98 | + |
| 99 | + virtual bool IsSwapchain() const { return false; } |
| 100 | + |
| 101 | +private: |
| 102 | + scene::Renderer* mRenderer = nullptr; |
| 103 | +}; |
| 104 | + |
| 105 | +// ------------------------------------------------------------------------------------------------- |
| 106 | + |
| 107 | +class RenderOutputToImage |
| 108 | + : public scene::RenderOutput |
| 109 | +{ |
| 110 | +protected: |
| 111 | + RenderOutputToImage( |
| 112 | + scene::Renderer* pRenderer, |
| 113 | + grfx::Image* pInitialImage = nullptr); |
| 114 | + |
| 115 | +public: |
| 116 | + virtual ~RenderOutputToImage(); |
| 117 | + |
| 118 | + static ppx::Result Create( |
| 119 | + scene::Renderer* pRenderer, |
| 120 | + grfx::Image* pInitialImage, // Can be NULL |
| 121 | + scene::RenderOutputToImage** ppRendererOutput); |
| 122 | + |
| 123 | + static void Destroy(scene::RenderOutputToImage* pRendererOutput); |
| 124 | + |
| 125 | + virtual ppx::Result GetRenderTargetImage( |
| 126 | + grfx::Image** ppImage, |
| 127 | + grfx::Semaphore* pImageReadySemaphore) override; |
| 128 | + |
| 129 | + void SetImage(grfx::Image* pImage); |
| 130 | + |
| 131 | +private: |
| 132 | + grfx::Image* mImage = nullptr; |
| 133 | +}; |
| 134 | + |
| 135 | +// ------------------------------------------------------------------------------------------------- |
| 136 | + |
| 137 | +class RenderOutputToSwapchain |
| 138 | + : public scene::RenderOutput |
| 139 | +{ |
| 140 | +protected: |
| 141 | + RenderOutputToSwapchain( |
| 142 | + scene::Renderer* pRenderer, |
| 143 | + grfx::Swapchain* pInitialSwapchain); |
| 144 | + |
| 145 | +public: |
| 146 | + virtual ~RenderOutputToSwapchain(); |
| 147 | + |
| 148 | + static ppx::Result Create( |
| 149 | + scene::Renderer* pRenderer, |
| 150 | + grfx::Swapchain* pInitialSwapchain, // Can be NULL |
| 151 | + scene::RenderOutputToSwapchain** ppRendererOutput); |
| 152 | + |
| 153 | + static void Destroy(scene::RenderOutputToSwapchain* pRendererOutput); |
| 154 | + |
| 155 | + virtual ppx::Result GetRenderTargetImage( |
| 156 | + grfx::Image** ppImage, |
| 157 | + grfx::Semaphore* pImageReadySemaphore) override; |
| 158 | + |
| 159 | + virtual bool IsSwapchain() const override { return true; } |
| 160 | + |
| 161 | + void SetSwapchain(grfx::Swapchain* pSwapchain); |
| 162 | + |
| 163 | +private: |
| 164 | + ppx::Result CreateObject(); |
| 165 | + void DestroyObject(); |
| 166 | + |
| 167 | +private: |
| 168 | + grfx::Swapchain* mSwapchain = nullptr; |
| 169 | + grfx::FencePtr mFence = nullptr; |
| 170 | + uint32_t mImageIndex = 0; |
| 171 | +}; |
| 172 | + |
| 173 | +// ------------------------------------------------------------------------------------------------- |
| 174 | + |
| 175 | +class Renderer |
| 176 | +{ |
| 177 | +protected: |
| 178 | + Renderer( |
| 179 | + grfx::Device* pDevice, |
| 180 | + uint32_t numInFlightFrames); |
| 181 | + |
| 182 | +public: |
| 183 | + virtual ~Renderer(); |
| 184 | + |
| 185 | + grfx::Device* GetDevice() const { return mDevice; } |
| 186 | + |
| 187 | + uint32_t GetNumInFlightFrames() const { return mNumInFlightFrames; } |
| 188 | + scene::Scene* GetScene() const { return mScene; } |
| 189 | + |
| 190 | + void SetScene(scene::Scene* pScene); |
| 191 | + |
| 192 | + ppx::Result Render( |
| 193 | + scene::RenderOutput* pOutput, |
| 194 | + grfx::Semaphore* pRenderCompleteSemaphore); |
| 195 | + |
| 196 | +protected: |
| 197 | + ppx::Result GetRenderOutputRenderPass( |
| 198 | + grfx::Image* pImage, |
| 199 | + grfx::RenderPass** ppRenderPass); |
| 200 | + |
| 201 | +private: |
| 202 | + virtual ppx::Result RenderInternal( |
| 203 | + scene::RenderOutput* pOutput, |
| 204 | + grfx::Semaphore* pRenderCompleteSemaphore) = 0; |
| 205 | + |
| 206 | + // Create render pass with 1 render target using pImage. |
| 207 | + // OVerride this method in derived renderers to customize output render passes. |
| 208 | + // |
| 209 | + virtual ppx::Result CreateOutputRenderPass( |
| 210 | + grfx::Image* pImage, |
| 211 | + grfx::RenderPass** ppRenderPass); |
| 212 | + |
| 213 | +protected: |
| 214 | + grfx::Device* mDevice = nullptr; |
| 215 | + uint32_t mNumInFlightFrames = 0; |
| 216 | + uint32_t mNumFramesRendered = 0; |
| 217 | + uint32_t mCurrentFrameIndex = 0; |
| 218 | + uint2 mRenderResolution = uint2(0, 0); |
| 219 | + std::vector<scene::GraphicsPipeline> mGraphicsPipelines = {}; |
| 220 | + std::vector<scene::GraphicsPipeline> mComputePipelines = {}; |
| 221 | + std::unordered_map<grfx::Image*, grfx::RenderPassPtr> mOutputRenderPasses = {}; |
| 222 | + bool mEnableDepthPrePass = false; |
| 223 | + scene::Scene* mScene = nullptr; |
| 224 | +}; |
| 225 | + |
| 226 | +} // namespace scene |
| 227 | +} // namespace ppx |
| 228 | + |
| 229 | +#endif // ppx_scene_renderer_h |
0 commit comments