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 &deviceContext = core.GetResource<Graphic::Resource::DeviceContext>();
39 const auto &queue = core.GetResource<Graphic::Resource::Queue>();
40
41 _buffer = _CreateBuffer(deviceContext);
42 _isCreated = true;
43
45 data.count = 0;
46 queue->writeBuffer(_buffer, 0, &data, sizeof(GPUDirectionalLights));
47 }
48
49 void Destroy(Engine::Core &core) override { Destroy(); }
50
51 void Destroy()
52 {
53 if (_isCreated)
54 {
55 _isCreated = false;
56 _buffer.release();
57 }
58 }
59
60 bool IsCreated(Engine::Core &core) const override { return _isCreated; }
61
62 void Update(Engine::Core &core) override
63 {
64 if (!_isCreated)
65 {
67 "Cannot update a GPU directional lights buffer that is not created.");
68 }
69
70 const auto &queue = core.GetResource<Graphic::Resource::Queue>();
72 auto view = core.GetRegistry()
75
76 uint32_t index = 0;
77 uint32_t skippedCount = 0;
78 view.each([&data, &index, &skippedCount](auto, const Object::Component::DirectionalLight &light,
79 const Component::GPUDirectionalLight &gpuLight,
80 const Object::Component::Transform &transform) {
82 {
83 skippedCount++;
84 return;
85 }
86 const auto &viewProjectionMatrix = gpuLight.viewProjectionMatrix;
87 const auto &color = light.color;
88 const auto &direction = -glm::normalize(transform.GetForwardVector() * transform.GetScale());
89 data.lights[index].viewProjectionMatrix = viewProjectionMatrix;
90 data.lights[index].color = color;
91 data.lights[index].direction = direction;
92 data.lights[index].shadowIndex = gpuLight.shadowTextureIndex;
93 index++;
94 });
95 data.count = index;
96
97 if (skippedCount > 0)
98 {
99 Log::Warning(fmt::format("Maximum number of directional lights ({}) reached. {} light(s) skipped.",
100 Utils::MAX_DIRECTIONAL_LIGHTS, skippedCount));
101 }
102
103 queue->writeBuffer(_buffer, 0, &data, sizeof(GPUDirectionalLights));
104 }
105
106 const wgpu::Buffer &GetBuffer() const override { return _buffer; }
107
108 std::string_view GetDebugName() const { return _debugName; }
109
110 static uint32_t GPUSize() { return sizeof(GPUDirectionalLights); }
111
112 private:
114 {
115 wgpu::BufferDescriptor bufferDesc(wgpu::Default);
116 bufferDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Uniform;
117 bufferDesc.size = sizeof(GPUDirectionalLights);
118 bufferDesc.label = wgpu::StringView(_debugName);
119
120 return context.GetDevice()->createBuffer(bufferDesc);
121 }
122
123 wgpu::Buffer _buffer;
124 bool _isCreated = false;
125};
126} // namespace DefaultPipeline::Resource
const wgpu::Buffer & GetBuffer() const override
Definition DirectionalLightsBuffer.hpp:106
bool IsCreated(Engine::Core &core) const override
Definition DirectionalLightsBuffer.hpp:60
wgpu::Buffer _CreateBuffer(const Graphic::Resource::DeviceContext &context)
Definition DirectionalLightsBuffer.hpp:113
wgpu::Buffer _buffer
Definition DirectionalLightsBuffer.hpp:123
static uint32_t GPUSize()
Definition DirectionalLightsBuffer.hpp:110
bool _isCreated
Definition DirectionalLightsBuffer.hpp:124
static std::string _debugName
Definition DirectionalLightsBuffer.hpp:17
~DirectionalLightsBuffer() override
Definition DirectionalLightsBuffer.hpp:34
void Destroy(Engine::Core &core) override
Definition DirectionalLightsBuffer.hpp:49
void Create(Engine::Core &core) override
Definition DirectionalLightsBuffer.hpp:36
void Destroy()
Definition DirectionalLightsBuffer.hpp:51
void Update(Engine::Core &core) override
Definition DirectionalLightsBuffer.hpp:62
std::string_view GetDebugName() const
Definition DirectionalLightsBuffer.hpp:108
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_DIRECTIONAL_LIGHTS
Definition DirectionalLights.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
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:19
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