Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
AMultipleExecutionRenderPass.hpp
Go to the documentation of this file.
1#pragma once
2
7
8namespace Graphic::Resource {
9
10template <typename TDerived> class AMultipleExecutionRenderPass : public ARenderPass {
11 public:
12 explicit AMultipleExecutionRenderPass(std::string_view name) : ARenderPass(name) {}
13
14 virtual uint16_t GetNumberOfPasses(Engine::Core &core) = 0;
15
16 virtual void preMultiplePass(Engine::Core &core) {
17 // Default implementation does nothing
18 };
19 virtual void postMultiplePass(Engine::Core &core) {
20 // Default implementation does nothing
21 };
22 virtual void perPass(uint16_t passIndex, Engine::Core &core) {
23 // Default implementation does nothing
24 };
25 virtual void postPass(uint16_t passIndex, Engine::Core &core) {
26 // Default implementation does nothing
27 };
28
29 void Execute(Engine::Core &core) override
30 {
31 preMultiplePass(core);
32 const uint16_t numberOfPasses = GetNumberOfPasses(core);
33 for (uint16_t passIndex = 0; passIndex < numberOfPasses; passIndex++)
34 {
35 perPass(passIndex, core);
37 postPass(passIndex, core);
38 }
39 postMultiplePass(core);
40 }
41
43 {
44 auto &deviceContext = core.GetResource<Resource::DeviceContext>();
45 auto &queue = core.GetResource<Resource::Queue>();
46
47 if (this->GetOutputs().colorBuffers.empty() && !this->GetOutputs().depthBuffer.has_value())
48 {
50 fmt::format("RenderPass {}: No outputs defined for render pass, cannot execute.", this->GetName()));
51 return;
52 }
53
54 wgpu::RenderPassEncoder renderPass = this->_CreateRenderPass(deviceContext, core);
55
56 auto &shader = core.GetResource<Graphic::Resource::ShaderContainer>().Get(this->GetBoundShader().value());
57 renderPass.setPipeline(shader.GetPipeline());
58
59 for (auto &[index, name] : this->GetInputs())
60 {
61 Resource::BindGroup &bindGroup =
62 core.GetResource<Resource::BindGroupManager>().Get(entt::hashed_string{name.c_str()});
63 renderPass.setBindGroup(index, bindGroup.GetBindGroup(), 0, nullptr);
64 }
65
66 UniqueRenderCallback(renderPass, core);
67
68 renderPass.end();
69 renderPass.release();
70
71 wgpu::CommandBufferDescriptor cmdBufferDescriptor(wgpu::Default);
72 std::string cmdBufferDescriptorLabel = fmt::format("CreateRenderPass::{}::CommandBuffer", this->GetName());
73 cmdBufferDescriptor.label = wgpu::StringView(cmdBufferDescriptorLabel);
74 auto commandBuffer = _commandEncoder.finish(cmdBufferDescriptor);
75 _commandEncoder.release();
76
77 queue->submit(1, &commandBuffer);
78 commandBuffer.release();
79 }
80
81 virtual void UniqueRenderCallback(wgpu::RenderPassEncoder &renderPass, Engine::Core &core) = 0;
82
83 private:
84 wgpu::RenderPassEncoder _CreateRenderPass(Resource::DeviceContext &context, Engine::Core &core)
85 {
86 auto &device = context.GetDevice().value();
87
88 wgpu::CommandEncoderDescriptor encoderDesc(wgpu::Default);
89 std::string encoderDescLabel = fmt::format("CreateRenderPass::{}::CommandEncoder", this->GetName());
90 encoderDesc.label = wgpu::StringView(encoderDescLabel);
91 _commandEncoder = device.createCommandEncoder(encoderDesc);
92 if (_commandEncoder == nullptr)
94 "CreateRenderPass::{}::Command encoder is not created, cannot draw sprite.", this->GetName()));
95
96 wgpu::RenderPassDescriptor renderPassDesc(wgpu::Default);
97 std::string renderPassDescLabel = fmt::format("CreateRenderPass::{}::RenderPass", this->GetName());
98 renderPassDesc.label = wgpu::StringView(renderPassDescLabel);
99
100 std::vector<wgpu::RenderPassColorAttachment> colorAttachments;
101 colorAttachments.reserve(this->GetOutputs().colorBuffers.size());
102
103 for (const auto &colorTextureName : this->GetOutputs().colorBuffers)
104 {
105 const auto &colorTexture = colorTextureName.second;
106 wgpu::RenderPassColorAttachment colorAttachment(wgpu::Default);
107
108 entt::hashed_string textureId = colorTexture.textureId;
109 auto textureView =
110 core.GetResource<Resource::TextureContainer>().Get(textureId).GetDefaultView().GetWebGPUView();
111
112 colorAttachment.view = textureView;
113 if (colorTexture.textureResolveTargetName.has_value())
114 {
115 std::string resolveTargetName = colorTexture.textureResolveTargetName.value();
116 auto resolveTextureView = core.GetResource<Resource::TextureContainer>()
117 .Get(entt::hashed_string{resolveTargetName.c_str()})
118 .GetDefaultView()
119 .GetWebGPUView();
120 colorAttachment.resolveTarget = resolveTextureView;
121 }
122 colorAttachment.storeOp = colorTexture.storeOp;
123 glm::vec4 clearColor(0.0f);
124 if (colorTexture.getClearColorCallback(core, clearColor))
125 {
126 colorAttachment.clearValue = wgpu::Color{clearColor.r, clearColor.g, clearColor.b, clearColor.a};
127 colorAttachment.loadOp = wgpu::LoadOp::Clear;
128 }
129 else
130 {
131 colorAttachment.loadOp = wgpu::LoadOp::Load;
132 }
133 colorAttachments.push_back(colorAttachment);
134 }
135
136 renderPassDesc.colorAttachmentCount = static_cast<uint32_t>(colorAttachments.size());
137 renderPassDesc.colorAttachments = colorAttachments.data();
138
139 wgpu::RenderPassDepthStencilAttachment depthAttachment(wgpu::Default);
140 if (this->GetOutputs().depthBuffer.has_value())
141 {
142 const auto &depthTexture = this->GetOutputs().depthBuffer.value();
143
144 wgpu::TextureView depthView;
145 if (depthTexture.depthTextureViewId.has_value())
146 {
148 .Get(depthTexture.depthTextureViewId.value())
149 .GetWebGPUView();
150 }
151 else
152 {
153 entt::hashed_string textureId = depthTexture.textureId;
154 depthView =
155 core.GetResource<Resource::TextureContainer>().Get(textureId).GetDefaultView().GetWebGPUView();
156 }
157
158 depthAttachment.view = depthView;
159 depthAttachment.depthStoreOp = depthTexture.storeOp;
160 float clearDepth = 1.0f;
161 if (depthTexture.getClearDepthCallback(core, clearDepth))
162 {
163 depthAttachment.depthClearValue = clearDepth;
164 depthAttachment.depthLoadOp = wgpu::LoadOp::Clear;
165 }
166 else
167 {
168 depthAttachment.depthLoadOp = wgpu::LoadOp::Load;
169 }
170 renderPassDesc.depthStencilAttachment = &depthAttachment;
171 }
172
173 return _commandEncoder.beginRenderPass(renderPassDesc);
174 }
175
176 wgpu::CommandEncoder _commandEncoder;
177};
178} // namespace Graphic::Resource
The core is the place where all the data of the engine is stored. It contains the registry (entities)...
Definition Core.hpp:33
TResource & GetResource()
Get a reference of a resource.
Definition Core.ipp:14
Definition FailToCreateCommandEncoderError.hpp:7
AMultipleExecutionRenderPass(std::string_view name)
Definition AMultipleExecutionRenderPass.hpp:12
wgpu::RenderPassEncoder _CreateRenderPass(Resource::DeviceContext &context, Engine::Core &core)
Definition AMultipleExecutionRenderPass.hpp:84
virtual void UniqueRenderCallback(wgpu::RenderPassEncoder &renderPass, Engine::Core &core)=0
void ExecuteSinglePass(Engine::Core &core)
Definition AMultipleExecutionRenderPass.hpp:42
virtual void postMultiplePass(Engine::Core &core)
Definition AMultipleExecutionRenderPass.hpp:19
virtual void perPass(uint16_t passIndex, Engine::Core &core)
Definition AMultipleExecutionRenderPass.hpp:22
virtual void postPass(uint16_t passIndex, Engine::Core &core)
Definition AMultipleExecutionRenderPass.hpp:25
void Execute(Engine::Core &core) override
Definition AMultipleExecutionRenderPass.hpp:29
wgpu::CommandEncoder _commandEncoder
Definition AMultipleExecutionRenderPass.hpp:176
virtual uint16_t GetNumberOfPasses(Engine::Core &core)=0
virtual void preMultiplePass(Engine::Core &core)
Definition AMultipleExecutionRenderPass.hpp:16
const auto & GetInputs(void) const
Definition ARenderPass.hpp:146
const auto & GetName(void) const
Definition ARenderPass.hpp:147
ARenderPass(std::string_view name)
Definition ARenderPass.hpp:56
const auto & GetBoundShader(void) const
Definition ARenderPass.hpp:145
const auto & GetOutputs(void) const
Definition ARenderPass.hpp:148
Definition BindGroup.hpp:17
const wgpu::BindGroup & GetBindGroup() const
Definition BindGroup.hpp:54
Definition Queue.hpp:6
Definition Adapter.hpp:5
Object::Resource::ResourceManager< Resource::TextureView > TextureViewContainer
Definition TextureViewContainer.hpp:9
Object::Resource::ResourceManager< Shader > ShaderContainer
ShaderContainer is a resource that stores Shader resources.
Definition ShaderContainer.hpp:17
Object::Resource::ResourceManager< BindGroup > BindGroupManager
BindGroupManager is a resource that stores BindGroup resources.
Definition BindGroupManager.hpp:17
Object::Resource::ResourceManager< Texture > TextureContainer
TextureContainer is a resource that stores Texture resources.
Definition TextureContainer.hpp:17
void Error(const T &msg) noexcept(false)
Definition Logger.hpp:34
constexpr DefaultFlag Default
Definition webgpu.hpp:78
Color(double r, double g, double b, double a)
Definition webgpu.hpp:636
StringView(const std::string_view &cpp)
Definition webgpu.hpp:618
Definition DeviceContext.hpp:7
auto & GetDevice()
Definition DeviceContext.hpp:19