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
50 void updateData(void* pData);
51
57 inline unsigned int getIndexIntoArray() const { return iIndexInArray; }
58
59 private:
69 size_t iIndexInArray,
71
77 inline void updateIndex(size_t iNewIndex) {
78 // Self check:
79 static_assert(
80 std::is_same_v<decltype(this->iIndexInArray), unsigned int>,
81 "update cast and type limit check below");
82
83 // Check before converting to unsigned int, see slot index docs for more info.
84 if (iNewIndex > UINT_MAX) [[unlikely]] {
85 Error error(std::format("received slot index {} exceeds type limit", iNewIndex));
86 error.showError();
87 throw std::runtime_error(error.getFullErrorMessage());
88 }
89
90 iIndexInArray = static_cast<unsigned int>(iNewIndex);
91 }
92
95
98
106 unsigned int iIndexInArray = 0;
107 };
108
117 // Slots notify the array in their destructor.
119
120 // Only manager should create the arrays and insert new slots.
122
123 public:
126 InternalResources() = default;
127
129 std::unique_ptr<UploadBuffer> pUploadBuffer = nullptr;
130
135 size_t iCapacity = 0;
136
144
146 std::queue<size_t> noLongerUsedArrayIndices;
147
156 std::unordered_set<DynamicCpuWriteShaderResourceArraySlot*> activeSlots;
157 };
158
162
169 size_t getSize();
170
177 size_t getCapacity();
178
184 size_t getSizeInBytes();
185
191 size_t getElementSize() const;
192
200 size_t getCapacityStepSize() const;
201
207 std::string_view getHandledShaderResourceName() const;
208
218 std::pair<std::recursive_mutex, InternalResources>* getInternalResources();
219
220 private:
231 static std::variant<std::unique_ptr<DynamicCpuWriteShaderResourceArray>, Error> create(
233 const std::string& sHandledShaderResourceName,
234 size_t iElementSizeInBytes);
235
244 static std::string formatBytesToKilobytes(size_t iSizeInBytes);
245
254 static std::variant<size_t, Error> calculateCapacityStepSize(size_t iElementSizeInBytes);
255
269 const std::string& sHandledShaderResourceName,
270 size_t iElementSizeInBytes,
271 size_t iCapacityStepSize);
272
286 std::variant<std::unique_ptr<DynamicCpuWriteShaderResourceArraySlot>, Error>
287 insert(ShaderCpuWriteResourceBinding* pShaderResource);
288
295
303
317 [[nodiscard]] std::optional<Error> createArray(size_t iCapacity);
318
326 [[nodiscard]] std::optional<Error> expandArray();
327
335 [[nodiscard]] std::optional<Error> shrinkArray();
336
338 std::pair<std::recursive_mutex, InternalResources> mtxInternalResources;
339
342
349 const size_t iCapacityStepSize = 0;
350
352 const std::string sHandledShaderResourceName;
353
355 const size_t iElementSizeInBytes = 0;
356 };
357}
Definition: DynamicCpuWriteShaderResourceArrayManager.h:27
Definition: DynamicCpuWriteShaderResourceArray.h:29
unsigned int getIndexIntoArray() const
Definition: DynamicCpuWriteShaderResourceArray.h:57
DynamicCpuWriteShaderResourceArray *const pArray
Definition: DynamicCpuWriteShaderResourceArray.h:94
unsigned int iIndexInArray
Definition: DynamicCpuWriteShaderResourceArray.h:106
void updateData(void *pData)
Definition: DynamicCpuWriteShaderResourceArray.cpp:43
void updateIndex(size_t iNewIndex)
Definition: DynamicCpuWriteShaderResourceArray.h:77
ShaderCpuWriteResourceBinding *const pShaderResource
Definition: DynamicCpuWriteShaderResourceArray.h:97
Definition: DynamicCpuWriteShaderResourceArray.h:116
void updateSlotData(DynamicCpuWriteShaderResourceArraySlot *pSlot, void *pData)
Definition: DynamicCpuWriteShaderResourceArray.cpp:169
size_t getCapacityStepSize() const
Definition: DynamicCpuWriteShaderResourceArray.cpp:458
size_t getSizeInBytes()
Definition: DynamicCpuWriteShaderResourceArray.cpp:451
std::variant< std::unique_ptr< DynamicCpuWriteShaderResourceArraySlot >, Error > insert(ShaderCpuWriteResourceBinding *pShaderResource)
Definition: DynamicCpuWriteShaderResourceArray.cpp:378
const std::string sHandledShaderResourceName
Definition: DynamicCpuWriteShaderResourceArray.h:352
size_t getElementSize() const
Definition: DynamicCpuWriteShaderResourceArray.cpp:456
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:338
const size_t iCapacityStepSize
Definition: DynamicCpuWriteShaderResourceArray.h:349
static std::variant< size_t, Error > calculateCapacityStepSize(size_t iElementSizeInBytes)
Definition: DynamicCpuWriteShaderResourceArray.cpp:95
size_t getSize()
Definition: DynamicCpuWriteShaderResourceArray.cpp:441
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:326
GpuResourceManager *const pResourceManager
Definition: DynamicCpuWriteShaderResourceArray.h:341
void markSlotAsNoLongerBeingUsed(DynamicCpuWriteShaderResourceArraySlot *pSlot)
Definition: DynamicCpuWriteShaderResourceArray.cpp:130
std::optional< Error > expandArray()
Definition: DynamicCpuWriteShaderResourceArray.cpp:273
const size_t iElementSizeInBytes
Definition: DynamicCpuWriteShaderResourceArray.h:355
size_t getCapacity()
Definition: DynamicCpuWriteShaderResourceArray.cpp:446
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:125
size_t iNextFreeArrayIndex
Definition: DynamicCpuWriteShaderResourceArray.h:143
std::queue< size_t > noLongerUsedArrayIndices
Definition: DynamicCpuWriteShaderResourceArray.h:146
std::unique_ptr< UploadBuffer > pUploadBuffer
Definition: DynamicCpuWriteShaderResourceArray.h:129
size_t iCapacity
Definition: DynamicCpuWriteShaderResourceArray.h:135
std::unordered_set< DynamicCpuWriteShaderResourceArraySlot * > activeSlots
Definition: DynamicCpuWriteShaderResourceArray.h:156