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 {
45
46 if (this->GetOutputs().colorBuffers.empty() && !this->GetOutputs().depthBuffer.has_value())
47 {
49 fmt::format("RenderPass {}: No outputs defined for render pass, cannot execute.", this->GetName()));
50 return;
51 }
52
53 wgpu::RenderPassEncoder renderPass = this->_CreateRenderPass(context.deviceContext, core);
54
55 auto &shader = core.GetResource<Graphic::Resource::ShaderContainer>().Get(this->GetBoundShader().value());
56 renderPass.setPipeline(shader.GetPipeline());
57
58 for (auto &[index, name] : this->GetInputs())
59 {
60 Resource::BindGroup &bindGroup =
61 core.GetResource<Resource::BindGroupManager>().Get(entt::hashed_string{name.c_str()});
62 renderPass.setBindGroup(index, bindGroup.GetBindGroup(), 0, nullptr);
63 }
64
65 UniqueRenderCallback(renderPass, core);
66
67 renderPass.end();
68 renderPass.release();
69
70 wgpu::CommandBufferDescriptor cmdBufferDescriptor(wgpu::Default);
71 std::string cmdBufferDescriptorLabel = fmt::format("CreateRenderPass::{}::CommandBuffer", this->GetName());
72 cmdBufferDescriptor.label = wgpu::StringView(cmdBufferDescriptorLabel);
73 auto commandBuffer = _commandEncoder.finish(cmdBufferDescriptor);
74 _commandEncoder.release();
75
76 context.queue.value().submit(1, &commandBuffer);
77 commandBuffer.release();
78 }
79
80 virtual void UniqueRenderCallback(wgpu::RenderPassEncoder &renderPass, Engine::Core &core) = 0;
81
82 private:
83 wgpu::RenderPassEncoder _CreateRenderPass(Resource::DeviceContext &context, Engine::Core &core)
84 {
85 auto &device = context.GetDevice().value();
86
87 wgpu::CommandEncoderDescriptor encoderDesc(wgpu::Default);
88 std::string encoderDescLabel = fmt::format("CreateRenderPass::{}::CommandEncoder", this->GetName());
89 encoderDesc.label = wgpu::StringView(encoderDescLabel);
90 _commandEncoder = device.createCommandEncoder(encoderDesc);
91 if (_commandEncoder == nullptr)
93 "CreateRenderPass::{}::Command encoder is not created, cannot draw sprite.", this->GetName()));
94
95 wgpu::RenderPassDescriptor renderPassDesc(wgpu::Default);
96 std::string renderPassDescLabel = fmt::format("CreateRenderPass::{}::RenderPass", this->GetName());
97 renderPassDesc.label = wgpu::StringView(renderPassDescLabel);
98
99 std::vector<wgpu::RenderPassColorAttachment> colorAttachments;
100 colorAttachments.reserve(this->GetOutputs().colorBuffers.size());
101
102 for (const auto &colorTextureName : this->GetOutputs().colorBuffers)
103 {
104 const auto &colorTexture = colorTextureName.second;
105 wgpu::RenderPassColorAttachment colorAttachment(wgpu::Default);
106
107 entt::hashed_string textureId = colorTexture.textureId;
108 auto textureView =
109 core.GetResource<Resource::TextureContainer>().Get(textureId).GetDefaultView().GetWebGPUView();
110
111 colorAttachment.view = textureView;
112 if (colorTexture.textureResolveTargetName.has_value())
113 {
114 std::string resolveTargetName = colorTexture.textureResolveTargetName.value();
115 auto resolveTextureView = core.GetResource<Resource::TextureContainer>()
116 .Get(entt::hashed_string{resolveTargetName.c_str()})
117 .GetDefaultView()
118 .GetWebGPUView();
119 colorAttachment.resolveTarget = resolveTextureView;
120 }
121 colorAttachment.storeOp = colorTexture.storeOp;
122 glm::vec4 clearColor(0.0f);
123 if (colorTexture.getClearColorCallback(core, clearColor))
124 {
125 colorAttachment.clearValue = wgpu::Color{clearColor.r, clearColor.g, clearColor.b, clearColor.a};
126 colorAttachment.loadOp = wgpu::LoadOp::Clear;
127 }
128 else
129 {
130 colorAttachment.loadOp = wgpu::LoadOp::Load;
131 }
132 colorAttachments.push_back(colorAttachment);
133 }
134
135 renderPassDesc.colorAttachmentCount = static_cast<uint32_t>(colorAttachments.size());
136 renderPassDesc.colorAttachments = colorAttachments.data();
137
138 wgpu::RenderPassDepthStencilAttachment depthAttachment(wgpu::Default);
139 if (this->GetOutputs().depthBuffer.has_value())
140 {
141 const auto &depthTexture = this->GetOutputs().depthBuffer.value();
142
143 wgpu::TextureView depthView;
144 if (depthTexture.depthTextureViewId.has_value())
145 {
147 .Get(depthTexture.depthTextureViewId.value())
148 .GetWebGPUView();
149 }
150 else
151 {
152 entt::hashed_string textureId = depthTexture.textureId;
153 depthView =
154 core.GetResource<Resource::TextureContainer>().Get(textureId).GetDefaultView().GetWebGPUView();
155 }
156
157 depthAttachment.view = depthView;
158 depthAttachment.depthStoreOp = depthTexture.storeOp;
159 float clearDepth = 1.0f;
160 if (depthTexture.getClearDepthCallback(core, clearDepth))
161 {
162 depthAttachment.depthClearValue = clearDepth;
163 depthAttachment.depthLoadOp = wgpu::LoadOp::Clear;
164 }
165 else
166 {
167 depthAttachment.depthLoadOp = wgpu::LoadOp::Load;
168 }
169 renderPassDesc.depthStencilAttachment = &depthAttachment;
170 }
171
172 return _commandEncoder.beginRenderPass(renderPassDesc);
173 }
174
175 wgpu::CommandEncoder _commandEncoder;
176};
177} // 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:83
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:175
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 Context.hpp:8
DeviceContext deviceContext
Definition Context.hpp:43
std::optional< wgpu::Queue > queue
Definition Context.hpp:44
Definition AGPUBuffer.hpp:6
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:13