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
11 public:
12 explicit PointGPUBuffer(Engine::Entity entity) : _entity(entity) {}
13 ~PointGPUBuffer() override { Destroy(); }
14 void Create(Engine::Core &core) override
15 {
16
17 auto meshComponent = _entity.TryGetComponent<Object::Component::Mesh>();
18
19 if (!meshComponent)
20 {
22 "Cannot create a GPU buffer for an entity without a Mesh component.");
23 }
24
25 if (meshComponent->GetNormals().size() != meshComponent->GetVertices().size() ||
26 meshComponent->GetTexCoords().size() != meshComponent->GetVertices().size())
27 {
29 "Cannot create GPU buffer: normals or texCoords size mismatch with vertices.");
30 }
31
32 wgpu::BufferDescriptor bufferDesc(wgpu::Default);
33 bufferDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Vertex;
34 bufferDesc.size = sizeof(float) * meshComponent->GetVertices().size() * 8;
35 std::string label = fmt::format("PointGPUBuffer_{}", _entity);
36 bufferDesc.label = wgpu::StringView(label);
37
38 const auto &context = core.GetResource<Graphic::Resource::Context>();
39 _buffer = context.deviceContext.GetDevice()->createBuffer(bufferDesc);
40
41 _isCreated = true;
42 Update(core);
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 const auto &meshComponent = _entity.GetComponents<Object::Component::Mesh>();
64 const auto &vertices = meshComponent.GetVertices();
65
66 if (vertices.empty())
67 {
69 "Cannot update a GPU buffer from a Mesh component with no vertices.");
70 }
71
72 const auto &normals = meshComponent.GetNormals();
73 const auto &texCoords = meshComponent.GetTexCoords();
74
75 if (normals.size() != vertices.size() || texCoords.size() != vertices.size())
76 {
78 "Cannot update GPU buffer: normals or texCoords size mismatch with vertices.");
79 }
80
81 std::vector<float> pointData;
82 pointData.reserve(vertices.size() * 8u);
83
84 for (uint64_t i = 0u; i < vertices.size(); ++i)
85 {
86 pointData.emplace_back(vertices[i].x);
87 pointData.emplace_back(vertices[i].y);
88 pointData.emplace_back(vertices[i].z);
89 pointData.emplace_back(normals[i].x);
90 pointData.emplace_back(normals[i].y);
91 pointData.emplace_back(normals[i].z);
92 pointData.emplace_back(texCoords[i].x);
93 pointData.emplace_back(texCoords[i].y);
94 }
95
96 core.GetResource<Graphic::Resource::Context>().queue->writeBuffer(_buffer, 0, pointData.data(),
97 sizeof(float) * pointData.size());
98 };
99
100 const wgpu::Buffer &GetBuffer() const override { return _buffer; };
101
102 private:
103 wgpu::Buffer _buffer;
104 bool _isCreated = false;
106};
107} // namespace DefaultPipeline::Resource
void Update(Engine::Core &core) override
Definition PointGPUBuffer.hpp:56
wgpu::Buffer _buffer
Definition PointGPUBuffer.hpp:103
bool _isCreated
Definition PointGPUBuffer.hpp:104
void Destroy(Engine::Core &core) override
Definition PointGPUBuffer.hpp:44
const wgpu::Buffer & GetBuffer() const override
Definition PointGPUBuffer.hpp:100
PointGPUBuffer(Engine::Entity entity)
Definition PointGPUBuffer.hpp:12
Engine::Entity _entity
Definition PointGPUBuffer.hpp:105
void Create(Engine::Core &core) override
Definition PointGPUBuffer.hpp:14
bool IsCreated(Engine::Core &core) const override
Definition PointGPUBuffer.hpp:55
~PointGPUBuffer() override
Definition PointGPUBuffer.hpp:13
void Destroy()
Definition PointGPUBuffer.hpp:46
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< glm::vec3 > & GetVertices() const
Definition Mesh.hpp:96