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 &context = core.GetResource<Graphic::Resource::Context>();
38
39 _buffer = _CreateBuffer(context.deviceContext);
40 _UpdateBuffer(transformComponent, context);
41 _isCreated = true;
42 };
43 void Destroy(Engine::Core &core) override { Destroy(); };
44
45 void Destroy()
46 {
47 if (_isCreated)
48 {
49 _isCreated = false;
50 _buffer.release();
51 }
52 }
53
54 bool IsCreated(Engine::Core &core) const override { return _isCreated; };
55 void Update(Engine::Core &core) override
56 {
57 if (!_isCreated)
58 {
59 throw Graphic::Exception::UpdateBufferError("Cannot update a GPU transform buffer that is not created.");
60 }
61
62 const auto &transformComponent = _entity.GetComponents<Object::Component::Transform>();
63 const auto &context = core.GetResource<Graphic::Resource::Context>();
64 _UpdateBuffer(transformComponent, context);
65 };
66
67 const wgpu::Buffer &GetBuffer() const override { return _buffer; };
68
69 private:
71 {
72 wgpu::BufferDescriptor bufferDesc(wgpu::Default);
73 bufferDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Uniform;
74 bufferDesc.size = sizeof(TransformGPUData);
75 const std::string label = fmt::format("TransformGPUBuffer_{}", _entity);
76 bufferDesc.label = wgpu::StringView(label);
77
78 return context.GetDevice()->createBuffer(bufferDesc);
79 }
80
92 void _UpdateBuffer(const Object::Component::Transform &transformComponent,
93 const Graphic::Resource::Context &context)
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 context.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:70
void _UpdateBuffer(const Object::Component::Transform &transformComponent, const Graphic::Resource::Context &context)
Update the GPU buffer with the entity's current model and normal matrices.
Definition TransformGPUBuffer.hpp:92
bool IsCreated(Engine::Core &core) const override
Definition TransformGPUBuffer.hpp:54
void Update(Engine::Core &core) override
Definition TransformGPUBuffer.hpp:55
void Destroy(Engine::Core &core) override
Definition TransformGPUBuffer.hpp:43
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:67
TransformGPUBuffer(Engine::Entity entity)
Definition TransformGPUBuffer.hpp:32
void Destroy()
Definition TransformGPUBuffer.hpp:45
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 UpdateBufferError.hpp:7
Definition AGPUBuffer.hpp:7
Definition Context.hpp:8
std::optional< wgpu::Queue > queue
Definition Context.hpp:44
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:13
Definition Transform.hpp:18
glm::mat4 ComputeTransformationMatrix() const
Definition Transform.hpp:84