Nameless Engine
Loading...
Searching...
No Matches
ShaderLightArray.h
1#pragma once
2
3// Standard.
4#include <memory>
5#include <functional>
6#include <variant>
7#include <optional>
8#include <array>
9#include <unordered_set>
10#include <mutex>
11
12// Custom.
13#include "misc/Error.h"
14#include "render/general/resource/UploadBuffer.h"
15#include "render/general/resource/frame/FrameResourceManager.h"
16#include "shader/general/resource/ShaderLightsInFrustumSingleType.hpp"
17
18namespace ne {
19 class Renderer;
20 class Pipeline;
21 class ShaderLightArray;
22
28 // Only arrays can create slots.
29 friend class ShaderLightArray;
30
31 public:
32 ShaderLightArraySlot() = delete;
33
35 ShaderLightArraySlot& operator=(const ShaderLightArraySlot&) = delete;
36
38
46 void markAsNeedsUpdate();
47
53 inline size_t getCurrentIndexIntoArray() const { return iIndexIntoArray; }
54
55 private:
70 size_t iIndexIntoArray,
71 const std::function<void*()>& startUpdateCallback,
72 const std::function<void()>& finishUpdateCallback);
73
76
83 Node* const pSpawnedOwnerLightNode = nullptr;
84
86 const std::function<void*()> startUpdateCallback;
87
89 const std::function<void()> finishUpdateCallback;
90
92 size_t iIndexIntoArray = 0;
93 };
94
100 // Frees the slot in its destructor.
101 friend class ShaderLightArraySlot;
102
103 // Manager notifies when pipeline updates happen and when we can copy new data of slots that
104 // need update.
106
107 // Modifies lights in frustum.
108 friend class Renderer;
109
110 public:
112 struct Resources {
127 std::array<std::unique_ptr<UploadBuffer>, FrameResourceManager::getFrameResourceCount()>
129
132
134 std::array<
135 std::unordered_set<ShaderLightArraySlot*>,
138
140 std::unordered_set<ShaderLightArraySlot*> activeSlots;
141 };
142
143 ShaderLightArray() = delete;
144
147
164 static std::unique_ptr<ShaderLightArray> create(
166 const std::string& sShaderLightResourceName,
167 const std::function<void(size_t)>& onSizeChanged,
168 const std::optional<std::pair<std::function<void(size_t)>, std::string>>&
169 optionalOnLightsInCameraFrustumCulled);
170
192 std::variant<std::unique_ptr<ShaderLightArraySlot>, Error> reserveNewSlot(
193 Node* pSpawnedOwnerLightNode,
194 size_t iDataSizeInBytes,
195 const std::function<void*()>& startUpdateCallback,
196 const std::function<void()>& finishUpdateCallback);
197
205 std::pair<std::recursive_mutex, Resources>* getInternalResources();
206
212 std::string getShaderResourceName() const;
213
214 private:
234 const std::string& sShaderLightResourceName,
235 const std::function<void(size_t)>& onSizeChanged,
236 const std::optional<std::function<void(size_t)>>& optionalCallbackOnLightsInCameraFrustumCulled,
237 const std::string& sIndicesLightsInFrustumShaderResourceName = "");
238
246 void onLightsInCameraFrustumCulled(size_t iCurrentFrameResourceIndex);
247
258 [[nodiscard]] std::optional<Error> recreateArray(bool bIsInitialization = false);
259
267 void updateSlotsMarkedAsNeedsUpdate(size_t iCurrentFrameResourceIndex);
268
276 [[nodiscard]] std::optional<Error> updateBindingsInAllPipelines();
277
288 [[nodiscard]] std::optional<Error> updatePipelineBinding(Pipeline* pPipeline);
289
295 void freeSlot(ShaderLightArraySlot* pSlot);
296
303
305 std::pair<std::recursive_mutex, Resources> mtxResources;
306
308 Renderer* pRenderer = nullptr;
309
312
318 const std::optional<std::function<void(size_t)>> optionalCallbackOnLightsInCameraFrustumCulled;
319
324 const std::function<void(size_t)> onSizeChanged;
325
327 const std::string sShaderLightResourceName;
328 };
329}
Definition: Error.h:27
static constexpr unsigned int getFrameResourceCount()
Definition: FrameResourceManager.h:74
Definition: LightingShaderResourceManager.h:50
Definition: Node.h:39
Definition: Pipeline.h:20
Definition: Renderer.h:43
Definition: ShaderLightArray.h:27
const std::function< void *()> startUpdateCallback
Definition: ShaderLightArray.h:86
void markAsNeedsUpdate()
Definition: ShaderLightArray.cpp:31
ShaderLightArray * pArray
Definition: ShaderLightArray.h:75
size_t iIndexIntoArray
Definition: ShaderLightArray.h:92
Node *const pSpawnedOwnerLightNode
Definition: ShaderLightArray.h:83
size_t getCurrentIndexIntoArray() const
Definition: ShaderLightArray.h:53
const std::function< void()> finishUpdateCallback
Definition: ShaderLightArray.h:89
Definition: ShaderLightArray.h:99
std::optional< Error > updateBindingsInAllPipelines()
Definition: ShaderLightArray.cpp:497
std::pair< std::recursive_mutex, Resources > mtxResources
Definition: ShaderLightArray.h:305
std::string getShaderResourceName() const
Definition: ShaderLightArray.cpp:214
std::pair< std::recursive_mutex, Resources > * getInternalResources()
Definition: ShaderLightArray.cpp:210
void freeSlot(ShaderLightArraySlot *pSlot)
Definition: ShaderLightArray.cpp:216
void markSlotAsNeedsUpdate(ShaderLightArraySlot *pSlot)
Definition: ShaderLightArray.cpp:279
void onLightsInCameraFrustumCulled(size_t iCurrentFrameResourceIndex)
Definition: ShaderLightArray.cpp:56
std::optional< Error > recreateArray(bool bIsInitialization=false)
Definition: ShaderLightArray.cpp:300
void updateSlotsMarkedAsNeedsUpdate(size_t iCurrentFrameResourceIndex)
Definition: ShaderLightArray.cpp:469
const std::optional< std::function< void(size_t)> > optionalCallbackOnLightsInCameraFrustumCulled
Definition: ShaderLightArray.h:318
std::optional< Error > updatePipelineBinding(Pipeline *pPipeline)
Definition: ShaderLightArray.cpp:543
Renderer * pRenderer
Definition: ShaderLightArray.h:308
std::variant< std::unique_ptr< ShaderLightArraySlot >, Error > reserveNewSlot(Node *pSpawnedOwnerLightNode, size_t iDataSizeInBytes, const std::function< void *()> &startUpdateCallback, const std::function< void()> &finishUpdateCallback)
Definition: ShaderLightArray.cpp:160
~ShaderLightArray()
Definition: ShaderLightArray.cpp:98
static std::unique_ptr< ShaderLightArray > create(Renderer *pRenderer, const std::string &sShaderLightResourceName, const std::function< void(size_t)> &onSizeChanged, const std::optional< std::pair< std::function< void(size_t)>, std::string > > &optionalOnLightsInCameraFrustumCulled)
Definition: ShaderLightArray.cpp:138
const std::function< void(size_t)> onSizeChanged
Definition: ShaderLightArray.h:324
size_t iElementSizeInBytes
Definition: ShaderLightArray.h:311
const std::string sShaderLightResourceName
Definition: ShaderLightArray.h:327
Definition: ShaderLightArray.h:112
std::unordered_set< ShaderLightArraySlot * > activeSlots
Definition: ShaderLightArray.h:140
ShaderLightsInFrustumSingleType lightsInFrustum
Definition: ShaderLightArray.h:131
std::array< std::unique_ptr< UploadBuffer >, FrameResourceManager::getFrameResourceCount()> vGpuArrayLightDataResources
Definition: ShaderLightArray.h:128
std::array< std::unordered_set< ShaderLightArraySlot * >, FrameResourceManager::getFrameResourceCount()> vSlotsToBeUpdated
Definition: ShaderLightArray.h:137
Definition: ShaderLightsInFrustumSingleType.hpp:18