Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
AmbientLightBuffer.hpp
Go to the documentation of this file.
1#pragma once
2
4#include "entity/Entity.hpp"
8#include "resource/Queue.hpp"
9#include <glm/gtc/type_ptr.hpp>
10
13 private:
14 static inline std::string prefix = "AmbientLightBuffer_";
16 glm::vec3 color;
17
18 explicit AmbientLightTransfer(const Object::Component::AmbientLight &ambientLight) : color(ambientLight.color)
19 {
20 }
21
22 static uint32_t CPUSize() { return sizeof(AmbientLightTransfer); }
23 static uint32_t GPUSize() { return sizeof(AmbientLightTransfer) + sizeof(float) /*padding*/; }
24 };
25
26 static_assert(sizeof(AmbientLightTransfer) == (sizeof(float) * 3),
27 "AmbientLightTransfer struct size does not match GPU requirements.");
28
29 public:
31
33
34 ~AmbientLightBuffer() override { Destroy(); }
35 void Create(Engine::Core &core) override
36 {
37 const auto &deviceContext = core.GetResource<Graphic::Resource::DeviceContext>();
38
39 _buffer = _CreateBuffer(deviceContext);
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 (!_entity.has_value() || _entity->IsAlive() == false)
57 {
58 return;
59 }
60 const auto &ambientLight = _entity->GetComponents<Object::Component::AmbientLight>();
61 SetValue(core, ambientLight);
62 };
63
65 {
66 _entity = entity;
68 }
69
70 void SetValue(const Engine::Core &core, const Object::Component::AmbientLight &ambientLight)
71 {
72 if (!_isCreated)
73 {
75 "Cannot update a GPU ambient light buffer that is not created.");
76 }
77
78 const auto &queue = core.GetResource<Graphic::Resource::Queue>();
79 _UpdateBuffer(queue, ambientLight);
80 }
81
82 const wgpu::Buffer &GetBuffer() const override { return _buffer; };
83
84 std::string_view GetDebugName() const { return _debugName; }
85
86 private:
87 inline void _UpdateDebugName()
88 {
89 if (_entity.has_value())
90 {
91 _debugName = fmt::format("{}{}", prefix, *_entity);
92 }
93 else
94 {
95 _debugName = fmt::format("{}{}", prefix, "<no_entity>");
96 }
97 }
98
100 {
101 wgpu::BufferDescriptor bufferDesc(wgpu::Default);
102 bufferDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Uniform;
103 bufferDesc.size = AmbientLightTransfer::GPUSize();
104 bufferDesc.label = wgpu::StringView(_debugName);
105
106 return context.GetDevice()->createBuffer(bufferDesc);
107 }
108
110 const Object::Component::AmbientLight &ambientLightComponent)
111 {
112 AmbientLightTransfer ambientLightTransfer(ambientLightComponent);
113 queue->writeBuffer(_buffer, 0, std::addressof(ambientLightTransfer), AmbientLightTransfer::CPUSize());
114 }
115
116 wgpu::Buffer _buffer;
117 bool _isCreated = false;
118 std::optional<Engine::Entity> _entity;
119 std::string _debugName;
120};
121} // namespace DefaultPipeline::Resource
~AmbientLightBuffer() override
Definition AmbientLightBuffer.hpp:34
void _UpdateDebugName()
Definition AmbientLightBuffer.hpp:87
wgpu::Buffer _buffer
Definition AmbientLightBuffer.hpp:116
wgpu::Buffer _CreateBuffer(const Graphic::Resource::DeviceContext &context)
Definition AmbientLightBuffer.hpp:99
void Destroy()
Definition AmbientLightBuffer.hpp:44
void Update(Engine::Core &core) override
Definition AmbientLightBuffer.hpp:54
std::string _debugName
Definition AmbientLightBuffer.hpp:119
std::optional< Engine::Entity > _entity
Definition AmbientLightBuffer.hpp:118
bool IsCreated(Engine::Core &core) const override
Definition AmbientLightBuffer.hpp:53
void Create(Engine::Core &core) override
Definition AmbientLightBuffer.hpp:35
void SetEntity(Engine::Entity entity)
Definition AmbientLightBuffer.hpp:64
AmbientLightBuffer(Engine::Entity entity)
Definition AmbientLightBuffer.hpp:30
bool _isCreated
Definition AmbientLightBuffer.hpp:117
static std::string prefix
Definition AmbientLightBuffer.hpp:14
const wgpu::Buffer & GetBuffer() const override
Definition AmbientLightBuffer.hpp:82
std::string_view GetDebugName() const
Definition AmbientLightBuffer.hpp:84
AmbientLightBuffer(void)
Definition AmbientLightBuffer.hpp:32
void Destroy(Engine::Core &core) override
Definition AmbientLightBuffer.hpp:42
void _UpdateBuffer(const Graphic::Resource::Queue &queue, const Object::Component::AmbientLight &ambientLightComponent)
Definition AmbientLightBuffer.hpp:109
void SetValue(const Engine::Core &core, const Object::Component::AmbientLight &ambientLight)
Definition AmbientLightBuffer.hpp:70
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
static uint32_t GPUSize()
Definition AmbientLightBuffer.hpp:23
AmbientLightTransfer(const Object::Component::AmbientLight &ambientLight)
Definition AmbientLightBuffer.hpp:18
static uint32_t CPUSize()
Definition AmbientLightBuffer.hpp:22
glm::vec3 color
Definition AmbientLightBuffer.hpp:16
Definition DeviceContext.hpp:7
auto & GetDevice()
Definition DeviceContext.hpp:19
Definition AmbientLight.hpp:7