Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
BindGroup.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <initializer_list>
4#include <vector>
5
6#include "core/Core.hpp"
13#include "utils/webgpu.hpp"
14
15namespace Graphic::Resource {
16
17class BindGroup {
18 public:
19 struct Asset {
20 enum class Type {
24 };
25
26 uint32_t binding;
28 entt::hashed_string name;
29 uint64_t size; // For buffer type
30 };
31
32 BindGroup(Engine::Core &core, std::string_view name, entt::hashed_string shaderId, uint32_t layoutIndex,
33 std::initializer_list<Asset> assets)
34 : _shaderId(shaderId), _layoutIndex(layoutIndex), _name(name), _assets(assets)
35 {
38 }
39
40 BindGroup(const BindGroup &other) = delete;
41 BindGroup(BindGroup &&other) noexcept
42 : _shaderId(other._shaderId), _layoutIndex(other._layoutIndex), _name(std::move(other._name)),
43 _assets(std::move(other._assets)), _entries(std::move(other._entries)), _bindGroup(other._bindGroup)
44 {
45 other._bindGroup = nullptr;
46 }
47
49 {
50 if (_bindGroup)
51 _bindGroup.release();
52 }
53
54 const wgpu::BindGroup &GetBindGroup() const { return _bindGroup; }
55
56 uint32_t GetLayoutIndex() const { return _layoutIndex; }
57
58 const std::vector<wgpu::BindGroupEntry> &GetEntries() const { return _entries; }
59
61 {
64 }
65
66 private:
68 {
69 _entries.clear();
71 }
72
73 std::vector<wgpu::BindGroupEntry> _CreateBindGroupEntries(Engine::Core &core)
74 {
75 std::vector<wgpu::BindGroupEntry> entries;
76 entries.reserve(_assets.size());
77 for (const auto &asset : _assets)
78 {
79 entries.push_back(_CreateBindGroupEntry(core, asset));
80 }
81 return entries;
82 }
83
85 {
86 auto newBindGroup = _CreateBindGroup(core);
87 _bindGroup.release();
88 _bindGroup = newBindGroup;
89 }
90
91 wgpu::BindGroup _CreateBindGroup(Engine::Core &core)
92 {
93 auto &shaders = core.GetResource<Graphic::Resource::ShaderContainer>();
94 auto &shader = shaders.Get(_shaderId);
95 auto layout = shader.GetBindGroupLayout(_layoutIndex);
96
97 wgpu::BindGroupDescriptor descriptor(wgpu::Default);
98 descriptor.layout = layout;
99 descriptor.label = wgpu::StringView(_name);
100 descriptor.entryCount = static_cast<uint32_t>(_entries.size());
101 descriptor.entries = _entries.empty() ? nullptr : _entries.data();
102
103 auto &deviceContext = core.GetResource<Graphic::Resource::Context>();
104 auto bindGroup = deviceContext.deviceContext.GetDevice()->createBindGroup(descriptor);
105
106 layout.release();
107
108 if (!bindGroup)
109 throw Exception::BindGroupCreationError("Failed to create BindGroup");
110 return bindGroup;
111 }
112
113 wgpu::BindGroupEntry _CreateBindGroupEntry(Engine::Core &core, const Asset &asset)
114 {
115 wgpu::BindGroupEntry entry(wgpu::Default);
116 entry.buffer = nullptr;
117 entry.sampler = nullptr;
118 entry.textureView = nullptr;
119 entry.size = 0;
120 entry.offset = 0;
121 entry.binding = asset.binding;
122
123 switch (asset.type)
124 {
125 case Asset::Type::Buffer: {
126 auto &gpuBufferContainer = core.GetResource<Graphic::Resource::GPUBufferContainer>();
127 auto &buffer = gpuBufferContainer.Get(asset.name);
128 entry.buffer = buffer->GetBuffer();
129 entry.size = buffer->GetBuffer().getSize();
130 break;
131 }
133 auto &samplerContainer = core.GetResource<Graphic::Resource::SamplerContainer>();
134 auto &sampler = samplerContainer.Get(asset.name);
135 entry.sampler = sampler.GetSampler();
136 break;
137 }
139 auto &textureContainer = core.GetResource<Graphic::Resource::TextureContainer>();
140 auto &texture = textureContainer.GetOrDefault(asset.name);
141 entry.textureView = texture.GetDefaultView();
142 break;
143 }
144 default: throw Exception::BindGroupCreationError("Unexpected Asset::Type value in _CreateBindGroupEntry");
145 }
146
147 return entry;
148 }
149
150 entt::hashed_string _shaderId;
151 uint32_t _layoutIndex = 0;
152 std::string _name;
153 std::vector<Asset> _assets;
154 std::vector<wgpu::BindGroupEntry> _entries;
155 wgpu::BindGroup _bindGroup;
156};
157
158} // 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 BindGroupCreationError.hpp:7
void _RefreshBindGroupEntries(Engine::Core &core)
Definition BindGroup.hpp:67
std::vector< wgpu::BindGroupEntry > _CreateBindGroupEntries(Engine::Core &core)
Definition BindGroup.hpp:73
std::vector< Asset > _assets
Definition BindGroup.hpp:153
std::string _name
Definition BindGroup.hpp:152
wgpu::BindGroup _CreateBindGroup(Engine::Core &core)
Definition BindGroup.hpp:91
void Refresh(Engine::Core &core)
Definition BindGroup.hpp:60
uint32_t GetLayoutIndex() const
Definition BindGroup.hpp:56
wgpu::BindGroupEntry _CreateBindGroupEntry(Engine::Core &core, const Asset &asset)
Definition BindGroup.hpp:113
BindGroup(const BindGroup &other)=delete
std::vector< wgpu::BindGroupEntry > _entries
Definition BindGroup.hpp:154
const std::vector< wgpu::BindGroupEntry > & GetEntries() const
Definition BindGroup.hpp:58
BindGroup(BindGroup &&other) noexcept
Definition BindGroup.hpp:41
void _RefreshBindGroup(Engine::Core &core)
Definition BindGroup.hpp:84
wgpu::BindGroup _bindGroup
Definition BindGroup.hpp:155
entt::hashed_string _shaderId
Definition BindGroup.hpp:150
const wgpu::BindGroup & GetBindGroup() const
Definition BindGroup.hpp:54
~BindGroup()
Definition BindGroup.hpp:48
uint32_t _layoutIndex
Definition BindGroup.hpp:151
BindGroup(Engine::Core &core, std::string_view name, entt::hashed_string shaderId, uint32_t layoutIndex, std::initializer_list< Asset > assets)
Definition BindGroup.hpp:32
Definition Context.hpp:8
DeviceContext deviceContext
Definition Context.hpp:43
Definition Sampler.hpp:8
Definition Texture.hpp:110
Definition AGPUBuffer.hpp:6
Object::Resource::ResourceManager< Sampler > SamplerContainer
SamplerContainer is a resource that stores Sampler resources.
Definition SamplerContainer.hpp:17
Object::Resource::ResourceManager< std::unique_ptr< AGPUBuffer > > GPUBufferContainer
GPUBufferContainer is a resource that stores GPUBuffer resources.
Definition GPUBufferContainer.hpp:17
Object::Resource::ResourceManager< Shader > ShaderContainer
ShaderContainer is a resource that stores Shader resources.
Definition ShaderContainer.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
StringView(const std::string_view &cpp)
Definition webgpu.hpp:618
Definition BindGroup.hpp:19
entt::hashed_string name
Definition BindGroup.hpp:28
uint32_t binding
Definition BindGroup.hpp:26
Type
Definition BindGroup.hpp:20
@ Sampler
Definition BindGroup.hpp:22
@ Buffer
Definition BindGroup.hpp:21
@ Texture
Definition BindGroup.hpp:23
Type type
Definition BindGroup.hpp:27
uint64_t size
Definition BindGroup.hpp:29
auto & GetDevice()
Definition DeviceContext.hpp:13