Skip to content

Commit 9a89b9b

Browse files
anishmgoyalchaoticbobapazylbe
committed
Add scene renderer class
Co-authored-by: Hai Nguyen <[email protected]> Co-authored-by: Aliya Pazylbekova <[email protected]>
1 parent dfcac42 commit 9a89b9b

7 files changed

+773
-1
lines changed

assets/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ add_subdirectory(fluid_simulation/shaders)
2020
add_subdirectory(gbuffer/shaders)
2121
add_subdirectory(materials/shaders)
2222
add_subdirectory(oit_demo/shaders)
23+
add_subdirectory(scene_renderer/shaders)

cmake/ShaderCompile.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function(internal_add_compile_shader_target TARGET_NAME)
5656
add_custom_command(
5757
OUTPUT "${ARG_OUTPUT_FILE}"
5858
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
59-
COMMENT "------ Compiling ${ARG_SHADER_STAGE} Shader [${ARG_OUTPUT_FORMAT}] ------"
59+
COMMENT "------ Compiling ${ARG_SHADER_STAGE} Shader [${ARG_OUTPUT_FORMAT}] ------${INCLUDE_DIRS}"
6060
MAIN_DEPENDENCY "${ARG_SOURCE}"
6161
DEPENDS ${ARG_INCLUDES}
6262
COMMAND ${CMAKE_COMMAND} -E echo "[${ARG_OUTPUT_FORMAT}] Compiling ${ARG_SHADER_STAGE} ${ARG_SOURCE} to ${ARG_OUTPUT_FILE}"
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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_forward_renderer_h
16+
#define ppx_scene_forward_renderer_h
17+
18+
#include "ppx/scene/scene_config.h"
19+
#include "ppx/scene/scene_renderer.h"
20+
21+
namespace ppx {
22+
namespace scene {
23+
24+
class ForwardRenderer
25+
: public scene::Renderer
26+
{
27+
private:
28+
ForwardRenderer(grfx::Device* pDevice, uint32_t numInFlightFrames);
29+
30+
public:
31+
virtual ~ForwardRenderer();
32+
33+
static ppx::Result Create(grfx::Device* pDevice, uint32_t numFramesInFlight, scene::Renderer** ppRenderer);
34+
35+
private:
36+
ppx::Result CreateObjects();
37+
void DestroyObjects();
38+
39+
ppx::Result RenderInternal(scene::RenderOutput* pOutput, grfx::Semaphore* pRenderCompleteSemaphore) override;
40+
41+
private:
42+
struct Frame;
43+
44+
ppx::Result RenderToOutput(
45+
ForwardRenderer::Frame& frame,
46+
scene::RenderOutput* pOutput,
47+
grfx::Semaphore* pRenderCompleteSemaphore);
48+
49+
private:
50+
struct Frame
51+
{
52+
scene::RenderPass depthPrePass;
53+
scene::RenderPass shadowPass;
54+
scene::RenderPass lightingPass;
55+
grfx::CommandBufferPtr renderOutputCmd;
56+
grfx::SemaphorePtr timelineSemaphore;
57+
uint64_t timelineValue = 0;
58+
};
59+
60+
std::vector<Frame> mFrames;
61+
};
62+
63+
} // namespace scene
64+
} // namespace ppx
65+
66+
#endif // ppx_scene_forward_renderer_h

include/ppx/scene/scene_renderer.h

+229
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
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

src/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -283,19 +283,23 @@ list(
283283
list(
284284
APPEND PPX_SCENE_HEADER_FILES
285285
${INC_DIR}/ppx/scene/scene_config.h
286+
${INC_DIR}/ppx/scene/scene_forward_renderer.h
286287
${INC_DIR}/ppx/scene/scene_material.h
287288
${INC_DIR}/ppx/scene/scene_mesh.h
288289
${INC_DIR}/ppx/scene/scene_node.h
289290
${INC_DIR}/ppx/scene/scene_resource_manager.h
291+
${INC_DIR}/ppx/scene/scene_renderer.h
290292
${INC_DIR}/ppx/scene/scene_scene.h
291293
)
292294

293295
list(
294296
APPEND PPX_SCENE_SOURCE_FILES
297+
${SRC_DIR}/ppx/scene/scene_forward_renderer.cpp
295298
${SRC_DIR}/ppx/scene/scene_material.cpp
296299
${SRC_DIR}/ppx/scene/scene_mesh.cpp
297300
${SRC_DIR}/ppx/scene/scene_node.cpp
298301
${SRC_DIR}/ppx/scene/scene_resource_manager.cpp
302+
${SRC_DIR}/ppx/scene/scene_renderer.cpp
299303
${SRC_DIR}/ppx/scene/scene_scene.cpp
300304
)
301305

0 commit comments

Comments
 (0)