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