PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
StorageHandler.hpp
1#pragma once
2
3#include "PixelBullet/Graphics/Buffers/StorageBuffer.hpp"
4
5#include <cstring>
6
7namespace PixelBullet
8{
10 {
11 public:
12 explicit StorageHandler(bool multipipeline = false);
13 explicit StorageHandler(const Shader::UniformBlock& uniformBlock, bool multipipeline = false);
14
15 void Push(void* data, std::size_t size)
16 {
17 if (this->m_Size != size)
18 {
19 this->m_Size = static_cast<uint32_t>(size);
20 m_HandlerStatus = Buffer::Status::Reset;
21 return;
22 }
23
24 if (!m_UniformBlock || !m_StorageBuffer)
25 {
26 return;
27 }
28
29 if (!m_Bound)
30 {
31 m_StorageBuffer->MapMemory(&this->m_Data);
32 m_Bound = true;
33 }
34
35 // If the buffer is already changed we can skip a memory comparison and just copy.
36 if (m_HandlerStatus == Buffer::Status::Changed ||
37 std::memcmp(static_cast<char*>(this->m_Data), data, size) != 0)
38 {
39 std::memcpy(static_cast<char*>(this->m_Data), data, size);
40 m_HandlerStatus = Buffer::Status::Changed;
41 }
42 }
43
44 template <typename T>
45 void Push(const T& object, std::size_t offset, std::size_t size)
46 {
47 if (!m_UniformBlock || !m_StorageBuffer)
48 {
49 return;
50 }
51
52 if (!m_Bound)
53 {
54 m_StorageBuffer->MapMemory(&this->m_Data);
55 m_Bound = true;
56 }
57
58 // If the buffer is already changed we can skip a memory comparison and just copy.
59 if (m_HandlerStatus == Buffer::Status::Changed ||
60 std::memcmp(static_cast<char*>(this->m_Data) + offset, &object, size) != 0)
61 {
62 std::memcpy(static_cast<char*>(this->m_Data) + offset, &object, size);
63 m_HandlerStatus = Buffer::Status::Changed;
64 }
65 }
66
67 template <typename T>
68 void Push(const std::string& uniformName, const T& object, std::size_t size = 0)
69 {
70 if (!m_UniformBlock)
71 {
72 return;
73 }
74
75 auto uniform = m_UniformBlock->GetUniform(uniformName);
76 if (!uniform)
77 {
78 return;
79 }
80
81 auto realSize = size;
82 if (realSize == 0)
83 {
84 realSize = std::min(sizeof(object), static_cast<std::size_t>(uniform->GetSize()));
85 }
86
87 Push(object, static_cast<std::size_t>(uniform->GetOffset()), realSize);
88 }
89
90 bool Update(const std::optional<Shader::UniformBlock>& uniformBlock);
91
92 const StorageBuffer* GetStorageBuffer() const
93 {
94 return m_StorageBuffer.get();
95 }
96
97 private:
98 bool m_Multipipeline;
99 std::optional<Shader::UniformBlock> m_UniformBlock;
100 uint32_t m_Size = 0;
101 void* m_Data = nullptr;
102 bool m_Bound = false;
103 std::unique_ptr<StorageBuffer> m_StorageBuffer;
104 Buffer::Status m_HandlerStatus;
105 };
106} // namespace PixelBullet
Definition Shader.hpp:162
Definition StorageBuffer.hpp:9
Definition StorageHandler.hpp:10