Nameless Engine
Loading...
Searching...
No Matches
DynamicCpuWriteShaderResourceArray.h
1#pragma once
2
3// Standard.
4#include <string>
5#include <stdexcept>
6#include <format>
7#include <queue>
8#include <unordered_set>
9#include <mutex>
10#include <variant>
11#include <memory>
12#include <optional>
13
14// Custom.
15#include "render/general/resource/UploadBuffer.h"
16#include "misc/Error.h"
17
18namespace ne {
19 class GpuResourceManager;
20 class ShaderCpuWriteResourceBinding;
21 class VulkanRenderer;
22 class VulkanPipeline;
23
30 // Only arrays are allowed to create and modify the internal index of this slot.
32
33 public:
35
38 operator=(const DynamicCpuWriteShaderResourceArraySlot&) = delete;
39
41
52 void updateDataBecauseGpuUnused(void* pData);
53
59 inline unsigned int getIndexIntoArray() const { return iIndexInArray; }
60
61 private:
71 size_t iIndexInArray,
73
79 inline void updateIndex(size_t iNewIndex) {
80 // Self check:
81 static_assert(
82 std::is_same_v<decltype(this->iIndexInArray), unsigned int>,
83 "update cast and type limit check below");
84
85 // Check before converting to unsigned int, see slot index docs for more info.
86 if (iNewIndex > UINT_MAX) [[unlikely]] {
87 Error error(std::format("received slot index {} exceeds type limit", iNewIndex));
88 error.showError();
89 throw std::runtime_error(error.getFullErrorMessage());
90 }
91
92 iIndexInArray = static_cast<unsigned int>(iNewIndex);
93 }
94
97
100
108 unsigned int iIndexInArray = 0;
109 };
110
119 // Slots notify the array in their destructor.
121
122 // Only manager should create the arrays and insert new slots.
124
125 public:
128 InternalResources() = default;
129
131 std::unique_ptr<UploadBuffer> pUploadBuffer = nullptr;
132
137 size_t iCapacity = 0;
138
146
148 std::queue<size_t> noLongerUsedArrayIndices;
149
158 std::unordered_set<DynamicCpuWriteShaderResourceArraySlot*> activeSlots;
159 };
160
164
171 size_t getSize();
172
179 size_t getCapacity();
180
186 size_t getSizeInBytes();
187
193 size_t getElementSize() const;
194
202 size_t getCapacityStepSize() const;
203
209 std::string_view getHandledShaderResourceName() const;
210
220 std::pair<std::recursive_mutex, InternalResources>* getInternalResources();
221
222 private:
233 static std::variant<std::unique_ptr<DynamicCpuWriteShaderResourceArray>, Error> create(
235 const std::string& sHandledShaderResourceName,
236 size_t iElementSizeInBytes);
237
246 static std::string formatBytesToKilobytes(size_t iSizeInBytes);
247
256 static std::variant<size_t, Error> calculateCapacityStepSize(size_t iElementSizeInBytes);
257
271 const std::string& sHandledShaderResourceName,
272 size_t iElementSizeInBytes,
273 size_t iCapacityStepSize);
274
288 std::variant<std::unique_ptr<DynamicCpuWriteShaderResourceArraySlot>, Error>
289 insert(ShaderCpuWriteResourceBinding* pShaderResource);
290
297
305
319 [[nodiscard]] std::optional<Error> createArray(size_t iCapacity);
320
328 [[nodiscard]] std::optional<Error> expandArray();
329
337 [[nodiscard]] std::optional<Error> shrinkArray();
338
340 std::pair<std::recursive_mutex, InternalResources> mtxInternalResources;
341
344
351 const size_t iCapacityStepSize = 0;
352
354 const std::string sHandledShaderResourceName;
355
357 const size_t iElementSizeInBytes = 0;
358 };
359}
Definition: DynamicCpuWriteShaderResourceArrayManager.h:27
Definition: DynamicCpuWriteShaderResourceArray.h:29
unsigned int getIndexIntoArray() const
Definition: DynamicCpuWriteShaderResourceArray.h:59
DynamicCpuWriteShaderResourceArray *const pArray
Definition: DynamicCpuWriteShaderResourceArray.h:96
unsigned int iIndexInArray
Definition: DynamicCpuWriteShaderResourceArray.h:108
void updateDataBecauseGpuUnused(void *pData)
Definition: DynamicCpuWriteShaderResourceArray.cpp:43
void updateIndex(size_t iNewIndex)
Definition: DynamicCpuWriteShaderResourceArray.h:79
ShaderCpuWriteResourceBinding *const pShaderResource
Definition: DynamicCpuWriteShaderResourceArray.h:99
Definition: DynamicCpuWriteShaderResourceArray.h:118
void updateSlotData(DynamicCpuWriteShaderResourceArraySlot *pSlot, void *pData)
Definition: DynamicCpuWriteShaderResourceArray.cpp:169
size_t getCapacityStepSize() const
Definition: DynamicCpuWriteShaderResourceArray.cpp:460
size_t getSizeInBytes()
Definition: DynamicCpuWriteShaderResourceArray.cpp:453
std::variant< std::unique_ptr< DynamicCpuWriteShaderResourceArraySlot >, Error > insert(ShaderCpuWriteResourceBinding *pShaderResource)
Definition: DynamicCpuWriteShaderResourceArray.cpp:380
const std::string sHandledShaderResourceName
Definition: DynamicCpuWriteShaderResourceArray.h:354
size_t getElementSize() const
Definition: DynamicCpuWriteShaderResourceArray.cpp:458
std::string_view getHandledShaderResourceName() const
Definition: DynamicCpuWriteShaderResourceArray.cpp:47
std::pair< std::recursive_mutex, InternalResources > * getInternalResources()
Definition: DynamicCpuWriteShaderResourceArray.cpp:52
std::pair< std::recursive_mutex, InternalResources > mtxInternalResources
Definition: DynamicCpuWriteShaderResourceArray.h:340
const size_t iCapacityStepSize
Definition: DynamicCpuWriteShaderResourceArray.h:351
static std::variant< size_t, Error > calculateCapacityStepSize(size_t iElementSizeInBytes)
Definition: DynamicCpuWriteShaderResourceArray.cpp:95
size_t getSize()
Definition: DynamicCpuWriteShaderResourceArray.cpp:443
static std::string formatBytesToKilobytes(size_t iSizeInBytes)
Definition: DynamicCpuWriteShaderResourceArray.cpp:88
static std::variant< std::unique_ptr< DynamicCpuWriteShaderResourceArray >, Error > create(GpuResourceManager *pResourceManager, const std::string &sHandledShaderResourceName, size_t iElementSizeInBytes)
Definition: DynamicCpuWriteShaderResourceArray.cpp:57
std::optional< Error > createArray(size_t iCapacity)
Definition: DynamicCpuWriteShaderResourceArray.cpp:182
std::optional< Error > shrinkArray()
Definition: DynamicCpuWriteShaderResourceArray.cpp:328
GpuResourceManager *const pResourceManager
Definition: DynamicCpuWriteShaderResourceArray.h:343
void markSlotAsNoLongerBeingUsed(DynamicCpuWriteShaderResourceArraySlot *pSlot)
Definition: DynamicCpuWriteShaderResourceArray.cpp:130
std::optional< Error > expandArray()
Definition: DynamicCpuWriteShaderResourceArray.cpp:275
const size_t iElementSizeInBytes
Definition: DynamicCpuWriteShaderResourceArray.h:357
size_t getCapacity()
Definition: DynamicCpuWriteShaderResourceArray.cpp:448
Definition: Error.h:27
std::string getFullErrorMessage() const
Definition: Error.cpp:84
void showError() const
Definition: Error.cpp:102
Definition: GpuResourceManager.h:43
Definition: ShaderCpuWriteResourceBinding.h:25
Definition: DynamicCpuWriteShaderResourceArray.h:127
size_t iNextFreeArrayIndex
Definition: DynamicCpuWriteShaderResourceArray.h:145
std::queue< size_t > noLongerUsedArrayIndices
Definition: DynamicCpuWriteShaderResourceArray.h:148
std::unique_ptr< UploadBuffer > pUploadBuffer
Definition: DynamicCpuWriteShaderResourceArray.h:131
size_t iCapacity
Definition: DynamicCpuWriteShaderResourceArray.h:137
std::unordered_set< DynamicCpuWriteShaderResourceArraySlot * > activeSlots
Definition: DynamicCpuWriteShaderResourceArray.h:158