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 &context = core.GetResource<Graphic::Resource::Context>();
45
46 _buffer = _CreateBuffer(context.deviceContext);
47 _isCreated = true;
48
49 PointLightsData data{};
50 data.count = 0;
51 context.queue->writeBuffer(_buffer, 0, &data, sizeof(PointLightsData));
52 }
53
54 void Destroy(Engine::Core &core) override { Destroy(); }
55
56 void Destroy()
57 {
58 if (_isCreated)
59 {
60 _isCreated = false;
61 _buffer.release();
62 }
63 }
64
65 bool IsCreated(Engine::Core &core) const override { return _isCreated; }
66
67 void Update(Engine::Core &core) override
68 {
69 if (!_isCreated)
70 {
71 throw Graphic::Exception::UpdateBufferError("Cannot update a GPU point lights buffer that is not created.");
72 }
73
74 const auto &context = core.GetResource<Graphic::Resource::Context>();
75 PointLightsData data{};
76
78
79 uint32_t index = 0;
80 uint32_t skippedCount = 0;
81 view.each([&data, &index, &skippedCount](auto, const Object::Component::PointLight &light,
82 const Object::Component::Transform &transform) {
83 if (index >= Utils::MAX_POINT_LIGHTS)
84 {
85 skippedCount++;
86 return;
87 }
88 const glm::vec3 &position = transform.GetPosition();
89 const glm::vec3 &color = light.color;
90
91 data.lights[index].position = {position.x, position.y, position.z};
92 data.lights[index].intensity = light.intensity;
93 data.lights[index].color = {color.x, color.y, color.z};
94 data.lights[index].radius = light.radius;
95 data.lights[index].falloff = light.falloff;
96 index++;
97 });
98 data.count = index;
99
100 if (skippedCount > 0)
101 {
102 Log::Warning(fmt::format("Maximum number of point lights ({}) reached. {} light(s) skipped.",
103 Utils::MAX_POINT_LIGHTS, skippedCount));
104 }
105
106 context.queue->writeBuffer(_buffer, 0, &data, sizeof(PointLightsData));
107 }
108
109 const wgpu::Buffer &GetBuffer() const override { return _buffer; }
110
111 std::string_view GetDebugName() const { return _debugName; }
112
113 static uint32_t GPUSize() { return sizeof(PointLightsData); }
114
115 private:
117 {
118 wgpu::BufferDescriptor bufferDesc(wgpu::Default);
119 bufferDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Uniform;
120 bufferDesc.size = sizeof(PointLightsData);
121 bufferDesc.label = wgpu::StringView(_debugName);
122
123 return context.GetDevice()->createBuffer(bufferDesc);
124 }
125
126 wgpu::Buffer _buffer;
127 bool _isCreated = false;
128};
129} // namespace DefaultPipeline::Resource
~PointLightsBuffer() override
Definition PointLightsBuffer.hpp:40
static std::string _debugName
Definition PointLightsBuffer.hpp:16
bool _isCreated
Definition PointLightsBuffer.hpp:127
const wgpu::Buffer & GetBuffer() const override
Definition PointLightsBuffer.hpp:109
bool IsCreated(Engine::Core &core) const override
Definition PointLightsBuffer.hpp:65
void Destroy(Engine::Core &core) override
Definition PointLightsBuffer.hpp:54
wgpu::Buffer _CreateBuffer(const Graphic::Resource::DeviceContext &context)
Definition PointLightsBuffer.hpp:116
void Update(Engine::Core &core) override
Definition PointLightsBuffer.hpp:67
wgpu::Buffer _buffer
Definition PointLightsBuffer.hpp:126
std::string_view GetDebugName() const
Definition PointLightsBuffer.hpp:111
void Create(Engine::Core &core) override
Definition PointLightsBuffer.hpp:42
static uint32_t GPUSize()
Definition PointLightsBuffer.hpp:113
void Destroy()
Definition PointLightsBuffer.hpp:56
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
Registry & GetRegistry()
Get the entt::registry that contains all components. It should be used to update component through sy...
Definition Core.hpp:50
Definition UpdateBufferError.hpp:7
Definition AGPUBuffer.hpp:7
Definition Context.hpp:8
Definition AmbientLight.cpp:6
static constexpr size_t MAX_POINT_LIGHTS
Definition PointLights.hpp:8
void Warning(const T &msg) noexcept
Definition Logger.hpp:49
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:13
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