Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
DirectionalLightBuffer.hpp
Go to the documentation of this file.
1#pragma once
2
5#include "entity/Entity.hpp"
9#include <glm/gtc/type_ptr.hpp>
10
13 public:
14 static inline std::string prefix = "DirectionalLightBuffer_";
17
18 explicit DirectionalLightTransfer(const Component::GPUDirectionalLight &GPUDirectionalLight)
19 : viewProjectionMatrix(GPUDirectionalLight.viewProjectionMatrix)
20 {
21 }
22
23 static uint32_t CPUSize() { return sizeof(DirectionalLightTransfer); }
24 static uint32_t GPUSize() { return sizeof(DirectionalLightTransfer); }
25 };
26
28
30
31 void Create(Engine::Core &core) override
32 {
33 const auto &context = core.GetResource<Graphic::Resource::Context>();
34
35 _buffer = _CreateBuffer(context.deviceContext);
36 _isCreated = true;
37 };
38 void Destroy(Engine::Core &core) override { Destroy(); };
39
40 void Destroy()
41 {
42 if (_isCreated)
43 {
44 _isCreated = false;
45 _buffer.release();
46 }
47 }
48
49 bool IsCreated(Engine::Core &core) const override { return _isCreated; };
50 void Update(Engine::Core &core) override
51 {
52 if (_entity.IsAlive() == false)
53 {
54 return;
55 }
56 const auto &gpuDirectionalLight = _entity.GetComponents<Component::GPUDirectionalLight>();
57 const auto &context = core.GetResource<Graphic::Resource::Context>();
58 _UpdateBuffer(context, gpuDirectionalLight);
59 };
60
61 const wgpu::Buffer &GetBuffer() const override { return _buffer; };
62
63 std::string_view GetDebugName() const { return _debugName; }
64
65 private:
66 inline void _UpdateDebugName() { _debugName = fmt::format("{}{}", prefix, _entity); }
67
69 {
70 wgpu::BufferDescriptor bufferDesc(wgpu::Default);
71 bufferDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Uniform;
72 bufferDesc.size = DirectionalLightTransfer::GPUSize();
73 bufferDesc.label = wgpu::StringView(_debugName);
74
75 return context.GetDevice()->createBuffer(bufferDesc);
76 }
77
79 const Component::GPUDirectionalLight &GPUDirectionalLight)
80 {
81 DirectionalLightTransfer directionalLightTransfer(GPUDirectionalLight);
82 context.queue->writeBuffer(_buffer, 0, std::addressof(directionalLightTransfer),
84 }
85
86 wgpu::Buffer _buffer;
87 bool _isCreated = false;
89 std::string _debugName;
90};
91} // namespace DefaultPipeline::Resource
static std::string prefix
Definition DirectionalLightBuffer.hpp:14
void Destroy(Engine::Core &core) override
Definition DirectionalLightBuffer.hpp:38
const wgpu::Buffer & GetBuffer() const override
Definition DirectionalLightBuffer.hpp:61
DirectionalLightBuffer(Engine::Entity entity)
Definition DirectionalLightBuffer.hpp:27
wgpu::Buffer _buffer
Definition DirectionalLightBuffer.hpp:86
wgpu::Buffer _CreateBuffer(const Graphic::Resource::DeviceContext &context)
Definition DirectionalLightBuffer.hpp:68
~DirectionalLightBuffer() override
Definition DirectionalLightBuffer.hpp:29
bool _isCreated
Definition DirectionalLightBuffer.hpp:87
void _UpdateDebugName()
Definition DirectionalLightBuffer.hpp:66
std::string _debugName
Definition DirectionalLightBuffer.hpp:89
Engine::Entity _entity
Definition DirectionalLightBuffer.hpp:88
bool IsCreated(Engine::Core &core) const override
Definition DirectionalLightBuffer.hpp:49
void Destroy()
Definition DirectionalLightBuffer.hpp:40
std::string_view GetDebugName() const
Definition DirectionalLightBuffer.hpp:63
void _UpdateBuffer(const Graphic::Resource::Context &context, const Component::GPUDirectionalLight &GPUDirectionalLight)
Definition DirectionalLightBuffer.hpp:78
void Update(Engine::Core &core) override
Definition DirectionalLightBuffer.hpp:50
void Create(Engine::Core &core) override
Definition DirectionalLightBuffer.hpp:31
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
Wrapper class providing a convenient interface for entity manipulation with the Core....
Definition Entity.hpp:20
Definition AGPUBuffer.hpp:7
Definition Context.hpp:8
std::optional< wgpu::Queue > queue
Definition Context.hpp:44
Definition AmbientLight.cpp:6
constexpr DefaultFlag Default
Definition webgpu.hpp:78
StringView(const std::string_view &cpp)
Definition webgpu.hpp:618
Definition GPUDirectionalLight.hpp:11
static uint32_t CPUSize()
Definition DirectionalLightBuffer.hpp:23
glm::mat4 viewProjectionMatrix
Definition DirectionalLightBuffer.hpp:16
DirectionalLightTransfer(const Component::GPUDirectionalLight &GPUDirectionalLight)
Definition DirectionalLightBuffer.hpp:18
static uint32_t GPUSize()
Definition DirectionalLightBuffer.hpp:24
Definition DeviceContext.hpp:7
auto & GetDevice()
Definition DeviceContext.hpp:13