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