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