Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
TransformGPUBuffer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "component/Mesh.hpp"
5#include "entity/Entity.hpp"
9#include <glm/gtc/type_ptr.hpp>
10
12
26 glm::mat4 modelMatrix;
27 glm::mat4 normalMatrix;
28};
29
31 public:
32 explicit TransformGPUBuffer(Engine::Entity entity) : _entity(entity) {}
33 ~TransformGPUBuffer() override { Destroy(); };
34 void Create(Engine::Core &core) override
35 {
36 const auto &transformComponent = _entity.GetComponents<Object::Component::Transform>();
37 const auto &queue = core.GetResource<Graphic::Resource::Queue>();
38 const auto &deviceContext = core.GetResource<Graphic::Resource::DeviceContext>();
39
40 _buffer = _CreateBuffer(deviceContext);
41 _UpdateBuffer(transformComponent, queue);
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 transform buffer that is not created.");
61 }
62
63 const auto &transformComponent = _entity.GetComponents<Object::Component::Transform>();
64 const auto &queue = core.GetResource<Graphic::Resource::Queue>();
65 _UpdateBuffer(transformComponent, queue);
66 };
67
68 const wgpu::Buffer &GetBuffer() const override { return _buffer; };
69
70 private:
72 {
73 wgpu::BufferDescriptor bufferDesc(wgpu::Default);
74 bufferDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Uniform;
75 bufferDesc.size = sizeof(TransformGPUData);
76 const std::string label = fmt::format("TransformGPUBuffer_{}", _entity);
77 bufferDesc.label = wgpu::StringView(label);
78
79 return context.GetDevice()->createBuffer(bufferDesc);
80 }
81
93 void _UpdateBuffer(const Object::Component::Transform &transformComponent, const Graphic::Resource::Queue &queue)
94 {
95 const glm::mat4 &modelMatrix = transformComponent.ComputeTransformationMatrix();
96
97 const glm::mat4 normalMatrix = glm::transpose(glm::inverse(modelMatrix));
98
99 TransformGPUData gpuData;
100 gpuData.modelMatrix = modelMatrix;
101 gpuData.normalMatrix = normalMatrix;
102
103 queue->writeBuffer(_buffer, 0, &gpuData, sizeof(TransformGPUData));
104 }
105
106 wgpu::Buffer _buffer;
107 bool _isCreated = false;
109};
110} // namespace DefaultPipeline::Resource
Engine::Entity _entity
Definition TransformGPUBuffer.hpp:108
wgpu::Buffer _CreateBuffer(const Graphic::Resource::DeviceContext &context)
Definition TransformGPUBuffer.hpp:71
bool IsCreated(Engine::Core &core) const override
Definition TransformGPUBuffer.hpp:55
void Update(Engine::Core &core) override
Definition TransformGPUBuffer.hpp:56
void Destroy(Engine::Core &core) override
Definition TransformGPUBuffer.hpp:44
wgpu::Buffer _buffer
Definition TransformGPUBuffer.hpp:106
void Create(Engine::Core &core) override
Definition TransformGPUBuffer.hpp:34
~TransformGPUBuffer() override
Definition TransformGPUBuffer.hpp:33
bool _isCreated
Definition TransformGPUBuffer.hpp:107
const wgpu::Buffer & GetBuffer() const override
Definition TransformGPUBuffer.hpp:68
TransformGPUBuffer(Engine::Entity entity)
Definition TransformGPUBuffer.hpp:32
void _UpdateBuffer(const Object::Component::Transform &transformComponent, const Graphic::Resource::Queue &queue)
Update the GPU buffer with the entity's current model and normal matrices.
Definition TransformGPUBuffer.hpp:93
void Destroy()
Definition TransformGPUBuffer.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.ipp:14
Wrapper class providing a convenient interface for entity manipulation with the Core....
Definition Entity.hpp:20
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
GPU buffer structure for model transform data.
Definition TransformGPUBuffer.hpp:25
glm::mat4 modelMatrix
Definition TransformGPUBuffer.hpp:26
glm::mat4 normalMatrix
Definition TransformGPUBuffer.hpp:27
Definition DeviceContext.hpp:7
auto & GetDevice()
Definition DeviceContext.hpp:19
Definition Transform.hpp:18
glm::mat4 ComputeTransformationMatrix() const
Definition Transform.hpp:84