Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
IndexGPUBuffer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "component/Mesh.hpp"
4#include "entity/Entity.hpp"
9#include "resource/Queue.hpp"
10
13 public:
14 explicit IndexGPUBuffer(Engine::Entity entity) : _entity(entity) {}
15 ~IndexGPUBuffer() override { Destroy(); }
16 void Create(Engine::Core &core) override
17 {
18
19 auto meshComponent = _entity.TryGetComponent<Object::Component::Mesh>();
20
21 if (!meshComponent)
22 {
24 "Cannot create a GPU buffer for an entity without a Mesh component.");
25 }
26
27 const auto &indices = meshComponent->GetIndices();
28
29 wgpu::BufferDescriptor bufferDesc(wgpu::Default);
30 bufferDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Index;
31 bufferDesc.size = sizeof(uint32_t) * indices.size();
32 std::string label = fmt::format("IndexGPUBuffer_{}", _entity);
33 bufferDesc.label = wgpu::StringView(label);
34
35 const auto &deviceContext = core.GetResource<Graphic::Resource::DeviceContext>();
36 const auto &queue = core.GetResource<Graphic::Resource::Queue>();
37
38 _buffer = deviceContext.GetDevice()->createBuffer(bufferDesc);
39
40 queue->writeBuffer(_buffer, 0, indices.data(), bufferDesc.size);
41
42 _isCreated = true;
43 };
44 void Destroy(Engine::Core &core) override { Destroy(); };
45
46 void Destroy()
47 {
48 if (_isCreated)
49 {
50 _isCreated = false;
51 _buffer.release();
52 }
53 }
54
55 bool IsCreated(Engine::Core &core) const override { return _isCreated; };
56 void Update(Engine::Core &core) override
57 {
58 if (!_isCreated)
59 {
60 throw Graphic::Exception::UpdateBufferError("Cannot update a GPU buffer that is not created.");
61 }
62
63 auto &meshComponent = _entity.GetComponents<Object::Component::Mesh>();
64
65 // For now, we will not implement dynamic resizing of the buffer. As we should have a way to know if the size
66 // changed. And it would be so heavy to check every frame every index.
67 };
68
69 const wgpu::Buffer &GetBuffer() const override { return _buffer; };
70
71 private:
72 wgpu::Buffer _buffer;
73 bool _isCreated = false;
75};
76} // namespace DefaultPipeline::Resource
Engine::Entity _entity
Definition IndexGPUBuffer.hpp:74
void Destroy(Engine::Core &core) override
Definition IndexGPUBuffer.hpp:44
bool _isCreated
Definition IndexGPUBuffer.hpp:73
void Update(Engine::Core &core) override
Definition IndexGPUBuffer.hpp:56
wgpu::Buffer _buffer
Definition IndexGPUBuffer.hpp:72
IndexGPUBuffer(Engine::Entity entity)
Definition IndexGPUBuffer.hpp:14
~IndexGPUBuffer() override
Definition IndexGPUBuffer.hpp:15
bool IsCreated(Engine::Core &core) const override
Definition IndexGPUBuffer.hpp:55
void Create(Engine::Core &core) override
Definition IndexGPUBuffer.hpp:16
void Destroy()
Definition IndexGPUBuffer.hpp:46
const wgpu::Buffer & GetBuffer() const override
Definition IndexGPUBuffer.hpp:69
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
Wrapper class providing a convenient interface for entity manipulation with the Core....
Definition Entity.hpp:20
Definition NonexistentComponentError.hpp:7
Definition UpdateBufferError.hpp:7
Definition AGPUBuffer.hpp:7
Definition Queue.hpp:6
Definition AmbientLight.cpp:6
constexpr DefaultFlag Default
Definition webgpu.hpp:78
StringView(const std::string_view &cpp)
Definition webgpu.hpp:618
Definition DeviceContext.hpp:7
Mesh structure.
Definition Mesh.hpp:40
const std::vector< uint32_t > & GetIndices() const
Definition Mesh.hpp:99