Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
PointGPUBuffer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "component/Mesh.hpp"
4#include "entity/Entity.hpp"
8#include "resource/Queue.hpp"
9
12 public:
13 explicit PointGPUBuffer(Engine::Entity entity) : _entity(entity) {}
14 ~PointGPUBuffer() 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 if (meshComponent->GetNormals().size() != meshComponent->GetVertices().size() ||
27 meshComponent->GetTexCoords().size() != meshComponent->GetVertices().size())
28 {
30 "Cannot create GPU buffer: normals or texCoords size mismatch with vertices.");
31 }
32
33 wgpu::BufferDescriptor bufferDesc(wgpu::Default);
34 bufferDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Vertex;
35 bufferDesc.size = sizeof(float) * meshComponent->GetVertices().size() * 8;
36 std::string label = fmt::format("PointGPUBuffer_{}", _entity);
37 bufferDesc.label = wgpu::StringView(label);
38
39 const auto &deviceContext = core.GetResource<Graphic::Resource::DeviceContext>();
40 _buffer = deviceContext.GetDevice()->createBuffer(bufferDesc);
41
42 _isCreated = true;
43 Update(core);
44 };
45 void Destroy(Engine::Core &core) override { Destroy(); };
46
47 void Destroy()
48 {
49 if (_isCreated)
50 {
51 _isCreated = false;
52 _buffer.release();
53 }
54 }
55
56 bool IsCreated(Engine::Core &core) const override { return _isCreated; };
57 void Update(Engine::Core &core) override
58 {
59 if (!_isCreated)
60 {
61 throw Graphic::Exception::UpdateBufferError("Cannot update a GPU buffer that is not created.");
62 }
63
64 const auto &meshComponent = _entity.GetComponents<Object::Component::Mesh>();
65 const auto &vertices = meshComponent.GetVertices();
66
67 if (vertices.empty())
68 {
70 "Cannot update a GPU buffer from a Mesh component with no vertices.");
71 }
72
73 const auto &normals = meshComponent.GetNormals();
74 const auto &texCoords = meshComponent.GetTexCoords();
75
76 if (normals.size() != vertices.size() || texCoords.size() != vertices.size())
77 {
79 "Cannot update GPU buffer: normals or texCoords size mismatch with vertices.");
80 }
81
82 std::vector<float> pointData;
83 pointData.reserve(vertices.size() * 8u);
84
85 for (uint64_t i = 0u; i < vertices.size(); ++i)
86 {
87 pointData.emplace_back(vertices[i].x);
88 pointData.emplace_back(vertices[i].y);
89 pointData.emplace_back(vertices[i].z);
90 pointData.emplace_back(normals[i].x);
91 pointData.emplace_back(normals[i].y);
92 pointData.emplace_back(normals[i].z);
93 pointData.emplace_back(texCoords[i].x);
94 pointData.emplace_back(texCoords[i].y);
95 }
96
97 core.GetResource<Graphic::Resource::Queue>()->writeBuffer(_buffer, 0, pointData.data(),
98 sizeof(float) * pointData.size());
99 };
100
101 const wgpu::Buffer &GetBuffer() const override { return _buffer; };
102
103 private:
104 wgpu::Buffer _buffer;
105 bool _isCreated = false;
107};
108} // namespace DefaultPipeline::Resource
void Update(Engine::Core &core) override
Definition PointGPUBuffer.hpp:57
wgpu::Buffer _buffer
Definition PointGPUBuffer.hpp:104
bool _isCreated
Definition PointGPUBuffer.hpp:105
void Destroy(Engine::Core &core) override
Definition PointGPUBuffer.hpp:45
const wgpu::Buffer & GetBuffer() const override
Definition PointGPUBuffer.hpp:101
PointGPUBuffer(Engine::Entity entity)
Definition PointGPUBuffer.hpp:13
Engine::Entity _entity
Definition PointGPUBuffer.hpp:106
void Create(Engine::Core &core) override
Definition PointGPUBuffer.hpp:15
bool IsCreated(Engine::Core &core) const override
Definition PointGPUBuffer.hpp:56
~PointGPUBuffer() override
Definition PointGPUBuffer.hpp:14
void Destroy()
Definition PointGPUBuffer.hpp:47
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< glm::vec3 > & GetVertices() const
Definition Mesh.hpp:96