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