Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
ARenderPass.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "core/Core.hpp"
5#include "resource/Shader.hpp"
8#include <glm/vec4.hpp>
9
10namespace Graphic::Resource {
11
13 entt::hashed_string textureId{};
14 explicit ColorOutput(std::string_view textureId_ = {})
15 {
16 if (!textureId_.empty())
17 {
18 textureId = entt::hashed_string(textureId_.data(), textureId_.size());
19 }
20 }
21
22 std::optional<std::string> textureResolveTargetName = std::nullopt;
23 uint32_t depthSlice = 0;
24 wgpu::StoreOp storeOp = wgpu::StoreOp::Store;
25 std::function<bool(Engine::Core &, glm::vec4 &)> getClearColorCallback = [](Engine::Core &, glm::vec4 &) {
26 return false;
27 };
28};
29
31 // TODO: use textureView container
32 entt::hashed_string textureId{};
33 std::optional<wgpu::TextureView> depthTextureView;
34 explicit DepthOutput(std::string_view textureId_ = {})
35 {
36 if (!textureId_.empty())
37 {
38 textureId = entt::hashed_string(textureId_.data(), textureId_.size());
39 }
40 }
41 wgpu::StoreOp storeOp = wgpu::StoreOp::Store;
42 std::function<bool(Engine::Core &, float &)> getClearDepthCallback = [](Engine::Core &, float &) { return false; };
43};
44
46 std::map<uint32_t, ColorOutput> colorBuffers;
47 std::optional<DepthOutput> depthBuffer;
48};
49
50struct InputContainer : public std::map<uint32_t /* index inside shader */, std::string /* bind group name */> {
51 using std::map<uint32_t, std::string>::map;
52};
53
55 public:
56 explicit ARenderPass(std::string_view name) : _name(name) {}
57
58 virtual void Execute(Engine::Core &core) = 0;
59
60 void BindShader(std::string_view shaderName)
61 {
62 _boundShader = entt::hashed_string(shaderName.data(), shaderName.size());
63 }
64
65 void AddInput(uint32_t groupIndex, std::string_view bindGroupName)
66 {
67 if (_inputs.contains(groupIndex))
68 {
69 Log::Warning(fmt::format("RenderPass {}: Overwriting existing bind group at index {}", _name, groupIndex));
70 }
71 _inputs[groupIndex] = bindGroupName;
72 }
73
74 void AddOutput(uint32_t id, ColorOutput &&output)
75 {
76 if (_outputs.colorBuffers.contains(id))
77 {
78 Log::Warning(fmt::format("RenderPass {}: Overwriting existing color buffer at index {}", _name, id));
79 }
80 _outputs.colorBuffers[id] = std::move(output);
81 }
82
83 void AddOutput(DepthOutput &&output)
84 {
85 if (_outputs.depthBuffer.has_value())
86 {
87 Log::Warning(fmt::format("RenderPass {}: Overwriting existing depth buffer", _name));
88 }
89 _outputs.depthBuffer = output;
90 }
91
92 std::vector<Utils::ValidationError> validate(Engine::Core &core) const
93 {
94 std::vector<Utils::ValidationError> errors;
95 const std::string location = fmt::format("RenderPass({})", _name);
96 const auto &shaderManager = core.GetResource<Resource::ShaderContainer>();
97
98 if (!_boundShader.has_value())
99 {
100 errors.push_back(Utils::ValidationError{.message = "No shader bound to render pass",
101 .location = location,
103 }
104 else
105 {
106 if (!shaderManager.Contains(_boundShader.value()))
107 {
108 errors.push_back(Utils::ValidationError{
109 .message = fmt::format("Bound shader '{}' does not exist in ShaderManager",
110 std::string_view(_boundShader->data(), _boundShader->size())),
111 .location = location,
113 }
114 const auto &shader = shaderManager.Get(_boundShader.value());
115 for (const auto &[index, bindGroupName] : _inputs)
116 {
117 const auto &bindGroupLayouts = shader.GetDescriptor().getBindGroupLayouts();
118 if (index >= bindGroupLayouts.size())
119 {
120 errors.push_back(Utils::ValidationError{
121 .message = fmt::format(
122 "Input bind group index {} exceeds number of bind groups ({}) in shader '{}'", index,
123 bindGroupLayouts.size(), std::string_view(_boundShader->data(), _boundShader->size())),
124 .location = location,
126 continue;
127 }
128 }
129 }
130 const auto &bindGroups = core.GetResource<Resource::BindGroupManager>();
131 for (const auto &[index, bindGroupName] : _inputs)
132 {
133 if (!bindGroups.Contains(entt::hashed_string(bindGroupName.data(), bindGroupName.size())))
134 {
135 errors.push_back(Utils::ValidationError{
136 .message = fmt::format("Input bind group '{}' at index {} does not exist in BindGroupContainer",
137 bindGroupName, index),
138 .location = location,
140 }
141 }
142 return errors;
143 };
144
145 const auto &GetBoundShader(void) const { return _boundShader; }
146 const auto &GetInputs(void) const { return _inputs; }
147 const auto &GetName(void) const { return _name; }
148 const auto &GetOutputs(void) const { return _outputs; }
149 auto &GetOutputs(void) { return _outputs; }
150
151 private:
152 std::optional<entt::hashed_string> _boundShader = std::nullopt;
154 std::string _name;
156};
157} // 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
std::string _name
Definition ARenderPass.hpp:154
const auto & GetInputs(void) const
Definition ARenderPass.hpp:146
virtual void Execute(Engine::Core &core)=0
std::optional< entt::hashed_string > _boundShader
Definition ARenderPass.hpp:152
const auto & GetName(void) const
Definition ARenderPass.hpp:147
void AddInput(uint32_t groupIndex, std::string_view bindGroupName)
Definition ARenderPass.hpp:65
void AddOutput(uint32_t id, ColorOutput &&output)
Definition ARenderPass.hpp:74
void BindShader(std::string_view shaderName)
Definition ARenderPass.hpp:60
ARenderPass(std::string_view name)
Definition ARenderPass.hpp:56
OutputContainer _outputs
Definition ARenderPass.hpp:155
std::vector< Utils::ValidationError > validate(Engine::Core &core) const
Definition ARenderPass.hpp:92
const auto & GetBoundShader(void) const
Definition ARenderPass.hpp:145
auto & GetOutputs(void)
Definition ARenderPass.hpp:149
InputContainer _inputs
Definition ARenderPass.hpp:153
const auto & GetOutputs(void) const
Definition ARenderPass.hpp:148
void AddOutput(DepthOutput &&output)
Definition ARenderPass.hpp:83
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
void Warning(const T &msg) noexcept
Definition Logger.hpp:49
Definition ARenderPass.hpp:12
std::function< bool(Engine::Core &, glm::vec4 &)> getClearColorCallback
Definition ARenderPass.hpp:25
std::optional< std::string > textureResolveTargetName
Definition ARenderPass.hpp:22
uint32_t depthSlice
Definition ARenderPass.hpp:23
ColorOutput(std::string_view textureId_={})
Definition ARenderPass.hpp:14
wgpu::StoreOp storeOp
Definition ARenderPass.hpp:24
entt::hashed_string textureId
Definition ARenderPass.hpp:13
Definition ARenderPass.hpp:30
wgpu::StoreOp storeOp
Definition ARenderPass.hpp:41
entt::hashed_string textureId
Definition ARenderPass.hpp:32
std::function< bool(Engine::Core &, float &)> getClearDepthCallback
Definition ARenderPass.hpp:42
std::optional< wgpu::TextureView > depthTextureView
Definition ARenderPass.hpp:33
DepthOutput(std::string_view textureId_={})
Definition ARenderPass.hpp:34
Definition ARenderPass.hpp:50
Definition ARenderPass.hpp:45
std::optional< DepthOutput > depthBuffer
Definition ARenderPass.hpp:47
std::map< uint32_t, ColorOutput > colorBuffers
Definition ARenderPass.hpp:46
Definition IValidable.hpp:9
@ Error
Definition IValidable.hpp:14