Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
PointLightsBuffer.hpp
Go to the documentation of this file.
1#pragma once
2
5#include "entity/Entity.hpp"
10#include <glm/gtc/type_ptr.hpp>
11#include <vector>
12
15 private:
16 static inline std::string _debugName = "PointLightsBuffer";
17
19 std::array<float, 3> position;
20 float intensity;
21 std::array<float, 3> color;
22 float radius;
23 float falloff;
24 std::array<float, 3> _padding;
25 };
26
27 static_assert(sizeof(GPUPointLight) == 48, "GPUPointLight must be 48 bytes for proper GPU alignment.");
28
30 std::array<GPUPointLight, Utils::MAX_POINT_LIGHTS> lights; // 64 * 48 = 3072 bytes
31 uint32_t count; // 4 bytes (3076 bytes)
32 std::array<float, 3> _padding; // 12 bytes (3088 bytes)
33 };
34
35 static_assert(sizeof(PointLightsData) == (48 * Utils::MAX_POINT_LIGHTS + 16),
36 "PointLightsData size does not match expected GPU requirements.");
37
38 public:
39 PointLightsBuffer() = default;
40 ~PointLightsBuffer() override { Destroy(); }
41
42 void Create(Engine::Core &core) override
43 {
44 const auto &deviceContext = core.GetResource<Graphic::Resource::DeviceContext>();
45 const auto &queue = core.GetResource<Graphic::Resource::Queue>();
46
47 _buffer = _CreateBuffer(deviceContext);
48 _isCreated = true;
49
50 PointLightsData data{};
51 data.count = 0;
52 queue->writeBuffer(_buffer, 0, &data, sizeof(PointLightsData));
53 }
54
55 void Destroy(Engine::Core &core) override { Destroy(); }
56
57 void Destroy()
58 {
59 if (_isCreated)
60 {
61 _isCreated = false;
62 _buffer.release();
63 }
64 }
65
66 bool IsCreated(Engine::Core &core) const override { return _isCreated; }
67
68 void Update(Engine::Core &core) override
69 {
70 if (!_isCreated)
71 {
72 throw Graphic::Exception::UpdateBufferError("Cannot update a GPU point lights buffer that is not created.");
73 }
74
75 const auto &queue = core.GetResource<Graphic::Resource::Queue>();
76 PointLightsData data{};
77
79
80 uint32_t index = 0;
81 uint32_t skippedCount = 0;
82 view.each([&data, &index, &skippedCount](auto, const Object::Component::PointLight &light,
83 const Object::Component::Transform &transform) {
84 if (index >= Utils::MAX_POINT_LIGHTS)
85 {
86 skippedCount++;
87 return;
88 }
89 const glm::vec3 &position = transform.GetPosition();
90 const glm::vec3 &color = light.color;
91
92 data.lights[index].position = {position.x, position.y, position.z};
93 data.lights[index].intensity = light.intensity;
94 data.lights[index].color = {color.x, color.y, color.z};
95 data.lights[index].radius = light.radius;
96 data.lights[index].falloff = light.falloff;
97 index++;
98 });
99 data.count = index;
100
101 if (skippedCount > 0)
102 {
103 Log::Warning(fmt::format("Maximum number of point lights ({}) reached. {} light(s) skipped.",
104 Utils::MAX_POINT_LIGHTS, skippedCount));
105 }
106
107 queue->writeBuffer(_buffer, 0, &data, sizeof(PointLightsData));
108 }
109
110 const wgpu::Buffer &GetBuffer() const override { return _buffer; }
111
112 std::string_view GetDebugName() const { return _debugName; }
113
114 static uint32_t GPUSize() { return sizeof(PointLightsData); }
115
116 private:
118 {
119 wgpu::BufferDescriptor bufferDesc(wgpu::Default);
120 bufferDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Uniform;
121 bufferDesc.size = sizeof(PointLightsData);
122 bufferDesc.label = wgpu::StringView(_debugName);
123
124 return context.GetDevice()->createBuffer(bufferDesc);
125 }
126
127 wgpu::Buffer _buffer;
128 bool _isCreated = false;
129};
130} // namespace DefaultPipeline::Resource
~PointLightsBuffer() override
Definition PointLightsBuffer.hpp:40
static std::string _debugName
Definition PointLightsBuffer.hpp:16
bool _isCreated
Definition PointLightsBuffer.hpp:128
const wgpu::Buffer & GetBuffer() const override
Definition PointLightsBuffer.hpp:110
bool IsCreated(Engine::Core &core) const override
Definition PointLightsBuffer.hpp:66
void Destroy(Engine::Core &core) override
Definition PointLightsBuffer.hpp:55
wgpu::Buffer _CreateBuffer(const Graphic::Resource::DeviceContext &context)
Definition PointLightsBuffer.hpp:117
void Update(Engine::Core &core) override
Definition PointLightsBuffer.hpp:68
wgpu::Buffer _buffer
Definition PointLightsBuffer.hpp:127
std::string_view GetDebugName() const
Definition PointLightsBuffer.hpp:112
void Create(Engine::Core &core) override
Definition PointLightsBuffer.hpp:42
static uint32_t GPUSize()
Definition PointLightsBuffer.hpp:114
void Destroy()
Definition PointLightsBuffer.hpp:57
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
Registry & GetRegistry()
Get the entt::registry that contains all components. It should be used to update component through sy...
Definition Core.cpp:44
Definition UpdateBufferError.hpp:7
Definition AGPUBuffer.hpp:7
Definition Queue.hpp:6
Definition AmbientLight.cpp:6
static constexpr size_t MAX_POINT_LIGHTS
Definition PointLights.hpp:8
void Warning(const T &msg) noexcept(false)
Definition Logger.hpp:32
constexpr DefaultFlag Default
Definition webgpu.hpp:78
StringView(const std::string_view &cpp)
Definition webgpu.hpp:618
std::array< float, 3 > position
Definition PointLightsBuffer.hpp:19
std::array< float, 3 > _padding
Definition PointLightsBuffer.hpp:24
std::array< float, 3 > color
Definition PointLightsBuffer.hpp:21
float falloff
Definition PointLightsBuffer.hpp:23
float radius
Definition PointLightsBuffer.hpp:22
float intensity
Definition PointLightsBuffer.hpp:20
uint32_t count
Definition PointLightsBuffer.hpp:31
std::array< float, 3 > _padding
Definition PointLightsBuffer.hpp:32
std::array< GPUPointLight, Utils::MAX_POINT_LIGHTS > lights
Definition PointLightsBuffer.hpp:30
Definition DeviceContext.hpp:7
auto & GetDevice()
Definition DeviceContext.hpp:19
Definition PointLight.hpp:7
glm::vec3 color
Definition PointLight.hpp:8
float intensity
Definition PointLight.hpp:9
float falloff
Definition PointLight.hpp:11
float radius
Definition PointLight.hpp:10
Definition Transform.hpp:18
const glm::vec3 & GetPosition() const
Definition Transform.hpp:37