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