SpiecsEngine
 
Loading...
Searching...
No Matches
BasePassRenderer.cpp
Go to the documentation of this file.
1/**
2* @file BasePassRenderer.cpp.
3* @brief The BasePassRenderer Class Implementation.
4* @author Spices.
5*/
6
7#include "Pchheader.h"
9
10namespace Spices {
11
12 /**
13 * @brief Use DGC or not.
14 */
15 constexpr bool m_IsUseDGC = false;
16
18 {
20
21 RendererPassBuilder{ "BassPass", this }
22 .AddSubPass("Mesh")
23 .AddColorAttachment("Albedo", TextureType::Texture2D, [](bool& isEnableBlend, VkAttachmentDescription& description) {
24 description.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
25 description.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
26 description.format = VK_FORMAT_R16G16B16A16_SFLOAT;
27 })
28 .AddColorAttachment("Normal", TextureType::Texture2D, [](bool& isEnableBlend, VkAttachmentDescription& description) {
29 description.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
30 description.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
31 description.format = VK_FORMAT_R16G16B16A16_SFLOAT;
32 })
33 .AddColorAttachment("Roughness", TextureType::Texture2D, [](bool& isEnableBlend, VkAttachmentDescription& description) {
34 description.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
35 description.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
36 description.format = VK_FORMAT_R16G16B16A16_SFLOAT;
37 })
38 .AddColorAttachment("Metallic", TextureType::Texture2D, [](bool& isEnableBlend, VkAttachmentDescription& description) {
39 description.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
40 description.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
41 description.format = VK_FORMAT_R16G16B16A16_SFLOAT;
42 })
43 .AddColorAttachment("Position", TextureType::Texture2D, [](bool& isEnableBlend, VkAttachmentDescription& description) {
44 description.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
45 description.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
46 description.format = VK_FORMAT_R32G32B32A32_SFLOAT;
47 })
48 .AddColorAttachment("EntityID", TextureType::Texture2D, [](bool& isEnableBlend, VkAttachmentDescription& description) {
49 description.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
50 description.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
51 description.format = VK_FORMAT_R32_SFLOAT;
52 })
53 .AddColorAttachment("TriangleID", TextureType::Texture2D, [](bool& isEnableBlend, VkAttachmentDescription& description) {
54 description.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
55 description.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
56 description.finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
57 })
58 .AddColorAttachment("MeshletID", TextureType::Texture2D, [](bool& isEnableBlend, VkAttachmentDescription& description) {
59 description.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
60 description.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
61 description.finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
62 })
63 .AddDepthAttachment("Depth", TextureType::Texture2D, [](VkAttachmentDescription& description) {
64 description.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; /* @attention It seams that layout transform is not work? */
65 description.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
66 })
67 .AddSelfDependency(VK_ACCESS_COMMAND_PREPROCESS_WRITE_BIT_NV,VK_ACCESS_INDIRECT_COMMAND_READ_BIT,VK_PIPELINE_STAGE_COMMAND_PREPROCESS_BIT_NV,VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT)
68 .EndSubPass()
69 .AddSubPass("SkyBox", Querier::None)
70 .AddColorAttachment("Albedo", TextureType::Texture2D, [](bool& isEnableBlend, VkAttachmentDescription& description) {
71 description.format = VK_FORMAT_R16G16B16A16_SFLOAT;
72 })
73 .AddColorAttachment("Position", TextureType::Texture2D, [](bool& isEnableBlend, VkAttachmentDescription& description) {
74 description.format = VK_FORMAT_R32G32B32A32_SFLOAT;
75 })
76 .AddColorAttachment("EntityID", TextureType::Texture2D, [](bool& isEnableBlend, VkAttachmentDescription& description) {
77 description.format = VK_FORMAT_R32_SFLOAT;
78 })
79 .AddColorAttachment("TriangleID", TextureType::Texture2D, [](bool& isEnableBlend, VkAttachmentDescription& description) {
80 })
81 .AddColorAttachment("MeshletID", TextureType::Texture2D, [](bool& isEnableBlend, VkAttachmentDescription& description) {
82 })
83 .AddDepthAttachment("Depth", TextureType::Texture2D, [](VkAttachmentDescription& description) {})
84 .EndSubPass()
85 .Build();
86 }
87
89 {
91
92 DescriptorSetBuilder{ "Mesh", this }
93 .AddPushConstant(sizeof(uint64_t))
94 .Build();
95
96 DescriptorSetBuilder{ "SkyBox", this }
97 .AddPushConstant(sizeof(uint64_t))
98 .Build();
99 }
100
102 {
104
105 DGCLayoutBuilder{ "Mesh", this }
109 .Build();
110 }
111
113 {
115
116 const auto view = GetEntityWithComponent<MeshComponent>(FrameInfo::Get().m_World.get());
117
118 //AsyncTask(ThreadPoolEnum::Custom, [=]() {
119
120 SPICES_PROFILE_ZONEN("RayTracingRenderer::OnMeshAddedWorld");
121
122 const auto dgcInstance = FillIndirectRenderData<MeshComponent>("Mesh", view);
123
124 //AsyncMainTask(ThreadPoolEnum::Main, [=]() {
125
126 vkQueueWaitIdle(m_VulkanState.m_GraphicQueue);
127 m_DGCData["Mesh"] = dgcInstance;
128 m_View = view;
129
130 //});
131 //});
132 }
133
135 std::shared_ptr<Material> material ,
136 VkPipelineLayout& layout ,
137 std::shared_ptr<RendererSubPass> subPass
138 )
139 {
141
142 PipelineBuilder{ subPass, material, this }
143 .SetDefault()
144 .SetRenderPass()
145 .SetSubPassIndex()
146 .SetPipelineLayout(layout)
147 .SetCullMode(VK_CULL_MODE_NONE)
148 .SetColorAttachments()
149 .BuildMesh();
150 }
151
153 const std::string& pipelineName ,
154 const std::string& materialName ,
155 VkPipelineLayout& layout ,
156 std::shared_ptr<RendererSubPass> subPass ,
158 )
159 {
161
162 PipelineBuilder{ subPass, nullptr, this }
163 .SetDefault()
164 .SetRenderPass()
165 .SetSubPassIndex()
166 .SetPipelineLayout(layout)
167 .SetCullMode(VK_CULL_MODE_NONE)
168 .SetColorAttachments()
169 .BuildDeviceGeneratedCommand(pipelineName, materialName, indirectPtr);
170 }
171
173 {
175
176 if(frameInfo.m_RendererType != RendererType::Rasterization) return;
177
178 RenderBehaveBuilder builder{ this ,frameInfo.m_FrameIndex, frameInfo.m_ImageIndex };
179
181
182 if constexpr (m_IsUseDGC)
183 {
184 builder.Await([&](const VkCommandBuffer& cmdBuffer) {
185
186 builder.SetViewPort(cmdBuffer);
187
188 builder.BindDescriptorSet(DescriptorSetManager::GetByName("PreRenderer"), cmdBuffer);
189
190 builder.BindDescriptorSet(DescriptorSetManager::GetByName({ m_Pass->GetName(), "Mesh" }), cmdBuffer);
191
192 builder.RunDGC(cmdBuffer);
193
194 });
195 }
196 else
197 {
198 const size_t nThreads = ThreadModel::Get()->GetRHIThreadPool()->GetThreadsCount();
199 const size_t nTask = m_View ? (m_View->size() > 40 ? nThreads : 1) : 0;
200 const size_t nCount = m_View ? m_View->size() / nTask : 0;
201
202 std::vector<std::future<VkCommandBuffer>> futureCmdBuffers;
203 for(size_t i = 0; i < nTask; i++)
204 {
205 futureCmdBuffers.push_back(
206
207 builder.Async([&, i](const VkCommandBuffer& cmdBuffer) {
208
209 builder.SetViewPort(cmdBuffer);
210
211 builder.BindDescriptorSet(DescriptorSetManager::GetByName("PreRenderer"), cmdBuffer);
212
213 builder.BindDescriptorSet(DescriptorSetManager::GetByName({ m_Pass->GetName(), "Mesh" }), cmdBuffer);
214
215 IterWorldCompWithRange<MeshComponent>(frameInfo, *m_View, i * nCount, std::min((i + 1) * nCount, m_View->size() - 1), [&](int entityId, TransformComponent& transComp, MeshComponent& meshComp) {
216
217 meshComp.GetMesh()->DrawMeshTasks(cmdBuffer, [&](const uint32_t& meshpackId, const auto& meshPack) {
218
219 builder.BindPipeline(meshPack->GetMaterial()->GetName(), cmdBuffer);
220
221 builder.UpdatePushConstant<uint64_t>([&](auto& push) {
222 push = meshPack->GetMeshDesc().GetBufferAddress();
223 }, cmdBuffer);
224 });
225 });
226 })
227 );
228 }
229
230 if (nTask > 0)
231 {
232 builder.Wait(futureCmdBuffers);
233 }
234 }
235
236 builder.BeginNextSubPass("SkyBox");
237
238 builder.SetViewPort();
239
240 builder.BindDescriptorSet(DescriptorSetManager::GetByName("PreRenderer"));
241
242 builder.BindDescriptorSet(DescriptorSetManager::GetByName({ m_Pass->GetName(), "SkyBox" }));
243
244 IterWorldCompWithBreak<SkyBoxComponent>(frameInfo, [&](int entityId, TransformComponent& transComp, SkyBoxComponent& skyboxComp) {
245
246 skyboxComp.GetMesh()->DrawMeshTasks(m_VulkanState.m_GraphicCommandBuffer[frameInfo.m_FrameIndex], [&](const uint32_t& meshPackId, const auto& meshPack) {
247
248 builder.BindPipeline(meshPack->GetMaterial()->GetName());
249
250 builder.UpdatePushConstant<uint64_t>([&](auto& push) {
251 push = meshPack->GetMeshDesc().GetBufferAddress();
252 });
253 });
254
255 return true;
256 });
257
258 builder.EndRenderPass();
259 }
260}
#define SPICES_PROFILE_ZONEN(...)
#define SPICES_PROFILE_ZONE
virtual void CreatePipeline(std::shared_ptr< Material > material, VkPipelineLayout &layout, std::shared_ptr< RendererSubPass > subPass) override
The interface is inherited from Renderer. Create Material Specific Pipeline.
virtual void CreateRendererPass() override
The interface is inherited from Renderer. Create Specific Renderer pass.
virtual void CreateDescriptorSet() override
The interface is inherited from Renderer. Create specific descriptor set for sub pass.
virtual void CreateDeviceGeneratedCommandsPipeline(const std::string &pipelineName, const std::string &materialName, VkPipelineLayout &layout, std::shared_ptr< RendererSubPass > subPass, VulkanDeviceGeneratedCommandsNV *indirectPtr) override
Create device generated command Pipeline.
virtual void CreateDeviceGeneratedCommandsLayout() override
This interface is called during OnSystemInitialize(). Create Device Generated Commands Layout.
virtual void OnMeshAddedWorld() override
virtual void Render(TimeStep &ts, FrameInfo &frameInfo) override
The interface is inherited from Renderer.
BasePassRenderer Class. This class defines the base pass render behaves.
uint32_t m_FrameIndex
FrameIndex, varying during 0 - (MaxFrameInFlight - 1). Used almost anywhere.
Definition FrameInfo.h:69
static FrameInfo & Get()
Get FrameInfo.
Definition FrameInfo.cpp:14
RendererType m_RendererType
The renderer type of current world.
Definition FrameInfo.h:99
uint32_t m_ImageIndex
ImageIndex, varying during 0 - (MaxFrameInFlight - 1). Used in swapchain index and framebuffer index.
Definition FrameInfo.h:75
FrameInfo Class. This class defines the FrameInfo data.
Definition FrameInfo.h:32
Material Class. This class contains a branch of parameter and shader, also descriptor.
Definition Material.h:62
MeshComponent Class. This class defines the specific behaves of MeshComponent.
This Class Combines some data relative to sub pass. Usually as a member variable of RendererPass.
DGCLayoutBuilder & AddDrawMeshTaskInput()
Add Draw Mesh Task Command to Input.
DGCLayoutBuilder & AddShaderGroupInput()
Add Binding Shader Group Command to Input.
void Build() const
Create GDC Layout.
DGCLayoutBuilder & AddPushConstantInput()
Add Binding PushConstant Command to Input.
DescriptorSetBuilder & AddPushConstant(uint32_t size)
Set VkPushConstantRange by a specific push constant struct.
virtual void BeginNextSubPass(const std::string &subPassName)
End a preview sub pass and stat next sub pass.
Definition Renderer.cpp:765
void Await(std::function< void(const VkCommandBuffer &cmdBuffer)> func) const
Await Async Commands.
Definition Renderer.cpp:583
void BeginRenderPassAsync()
Begin this Renderer's RenderPass Async.
Definition Renderer.cpp:925
virtual void EndRenderPass()
End this Renderer's RenderPass.
Definition Renderer.cpp:981
This class helps to bind pipeline and bind buffer. Only instanced during Render().
Definition Renderer.h:985
virtual void OnMeshAddedWorld()
Definition Renderer.cpp:86
VulkanState & m_VulkanState
This variable is passed while renderer instanced.
Definition Renderer.h:1992
Renderer Class. This class defines the basic behaves of renderer. When we add an new Renderer,...
Definition Renderer.h:57
SkyBoxComponent Class. This class defines the specific behaves of SkyBoxComponent.
This Class handles our engine time step during frames. Global Unique.
Definition TimeStep.h:22
TransformComponent Class. This class defines the specific behaves of TransformComponent.
VulkanDeviceGeneratedCommandsNV Class. This class defines the VulkanDeviceGeneratedCommandsNV behaves...
constexpr bool m_IsUseDGC
Use DGC or not.
RendererType
Definition FrameInfo.h:22