Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
DirectionalLightsBuffer.hpp
Go to the documentation of this file.
1#pragma once
2
6#include "entity/Entity.hpp"
11#include <glm/gtc/type_ptr.hpp>
12#include <vector>
13
16 private:
17 static inline std::string _debugName = "DirectionalLightsBuffer";
18
21 glm::vec4 color;
22 glm::vec3 direction;
23 uint32_t shadowIndex;
24 };
25
27 std::array<GPUDirectionalLight, Utils::MAX_DIRECTIONAL_LIGHTS> lights;
28 uint32_t count;
29 std::array<float, 3> _padding;
30 };
31
32 public:
35
36 void Create(Engine::Core &core) override
37 {
38 const auto &context = core.GetResource<Graphic::Resource::Context>();
39
40 _buffer = _CreateBuffer(context.deviceContext);
41 _isCreated = true;
42
44 data.count = 0;
45 context.queue->writeBuffer(_buffer, 0, &data, sizeof(GPUDirectionalLights));
46 }
47
48 void Destroy(Engine::Core &core) override { Destroy(); }
49
50 void Destroy()
51 {
52 if (_isCreated)
53 {
54 _isCreated = false;
55 _buffer.release();
56 }
57 }
58
59 bool IsCreated(Engine::Core &core) const override { return _isCreated; }
60
61 void Update(Engine::Core &core) override
62 {
63 if (!_isCreated)
64 {
66 "Cannot update a GPU directional lights buffer that is not created.");
67 }
68
69 const auto &context = core.GetResource<Graphic::Resource::Context>();
71 auto view = core.GetRegistry()
74
75 uint32_t index = 0;
76 uint32_t skippedCount = 0;
77 view.each([&data, &index, &skippedCount](auto, const Object::Component::DirectionalLight &light,
78 const Component::GPUDirectionalLight &gpuLight,
79 const Object::Component::Transform &transform) {
81 {
82 skippedCount++;
83 return;
84 }
85 const auto &viewProjectionMatrix = gpuLight.viewProjectionMatrix;
86 const auto &color = light.color;
87 const auto &direction = -glm::normalize(transform.GetForwardVector() * transform.GetScale());
88 data.lights[index].viewProjectionMatrix = viewProjectionMatrix;
89 data.lights[index].color = color;
90 data.lights[index].direction = direction;
91 data.lights[index].shadowIndex = gpuLight.shadowTextureIndex;
92 index++;
93 });
94 data.count = index;
95
96 if (skippedCount > 0)
97 {
98 Log::Warning(fmt::format("Maximum number of directional lights ({}) reached. {} light(s) skipped.",
99 Utils::MAX_DIRECTIONAL_LIGHTS, skippedCount));
100 }
101
102 context.queue->writeBuffer(_buffer, 0, &data, sizeof(GPUDirectionalLights));
103 }
104
105 const wgpu::Buffer &GetBuffer() const override { return _buffer; }
106
107 std::string_view GetDebugName() const { return _debugName; }
108
109 static uint32_t GPUSize() { return sizeof(GPUDirectionalLights); }
110
111 private:
113 {
114 wgpu::BufferDescriptor bufferDesc(wgpu::Default);
115 bufferDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Uniform;
116 bufferDesc.size = sizeof(GPUDirectionalLights);
117 bufferDesc.label = wgpu::StringView(_debugName);
118
119 return context.GetDevice()->createBuffer(bufferDesc);
120 }
121
122 wgpu::Buffer _buffer;
123 bool _isCreated = false;
124};
125} // namespace DefaultPipeline::Resource
const wgpu::Buffer & GetBuffer() const override
Definition DirectionalLightsBuffer.hpp:105
bool IsCreated(Engine::Core &core) const override
Definition DirectionalLightsBuffer.hpp:59
wgpu::Buffer _CreateBuffer(const Graphic::Resource::DeviceContext &context)
Definition DirectionalLightsBuffer.hpp:112
wgpu::Buffer _buffer
Definition DirectionalLightsBuffer.hpp:122
static uint32_t GPUSize()
Definition DirectionalLightsBuffer.hpp:109
bool _isCreated
Definition DirectionalLightsBuffer.hpp:123
static std::string _debugName
Definition DirectionalLightsBuffer.hpp:17
~DirectionalLightsBuffer() override
Definition DirectionalLightsBuffer.hpp:34
void Destroy(Engine::Core &core) override
Definition DirectionalLightsBuffer.hpp:48
void Create(Engine::Core &core) override
Definition DirectionalLightsBuffer.hpp:36
void Destroy()
Definition DirectionalLightsBuffer.hpp:50
void Update(Engine::Core &core) override
Definition DirectionalLightsBuffer.hpp:61
std::string_view GetDebugName() const
Definition DirectionalLightsBuffer.hpp:107
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_DIRECTIONAL_LIGHTS
Definition DirectionalLights.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
Definition GPUDirectionalLight.hpp:11
uint32_t shadowTextureIndex
Definition GPUDirectionalLight.hpp:18
glm::mat4 viewProjectionMatrix
Definition GPUDirectionalLight.hpp:14
uint32_t shadowIndex
Definition DirectionalLightsBuffer.hpp:23
glm::vec3 direction
Definition DirectionalLightsBuffer.hpp:22
glm::mat4 viewProjectionMatrix
Definition DirectionalLightsBuffer.hpp:20
glm::vec4 color
Definition DirectionalLightsBuffer.hpp:21
std::array< GPUDirectionalLight, Utils::MAX_DIRECTIONAL_LIGHTS > lights
Definition DirectionalLightsBuffer.hpp:27
std::array< float, 3 > _padding
Definition DirectionalLightsBuffer.hpp:29
uint32_t count
Definition DirectionalLightsBuffer.hpp:28
Definition DeviceContext.hpp:7
auto & GetDevice()
Definition DeviceContext.hpp:13
Definition DirectionalLight.hpp:8
glm::vec4 color
Definition DirectionalLight.hpp:9
Definition Transform.hpp:18
glm::vec3 GetForwardVector() const
Definition Transform.hpp:73
const glm::vec3 & GetScale() const
Definition Transform.hpp:38