Nameless Engine
Loading...
Searching...
No Matches
ShaderCpuWriteResourceBinding.h
1#pragma once
2
3// Standard.
4#include <unordered_map>
5#include <optional>
6#include <variant>
7#include <string>
8#include <functional>
9
10// Custom.
11#include "render/general/resource/frame/FrameResourceManager.h"
12#include "shader/general/resource/cpuwrite/DynamicCpuWriteShaderResourceArray.h"
13#include "render/general/pipeline/PipelineShaderConstantsManager.hpp"
14#include "shader/general/resource/binding/ShaderResourceBinding.h"
15#include "misc/Error.h"
16
17namespace ne {
18 class Renderer;
19 class Pipeline;
20
26 // Only manager should be able to create this resource and update data of this resource.
28
29 public:
30 virtual ~ShaderCpuWriteResourceBinding() override = default;
31
40 PipelineShaderConstantsManager* pShaderConstantsManager,
41 Pipeline* pUsedPipeline,
42 size_t iCurrentFrameResourceIndex) {
43 // Since pipelines won't change here (because we are inside of the `draw` function)
44 // we don't need to lock the mutex here.
45
46 // Find push constant index of this pipeline.
47 const auto offsetIt = mtxUintShaderConstantOffsets.second.find(pUsedPipeline);
48
49#if defined(DEBUG)
50 if (offsetIt == mtxUintShaderConstantOffsets.second.end()) [[unlikely]] {
51 Error error(std::format(
52 "shader resource \"{}\" was requested to set its push constant "
53 "index but this shader resource does not reference the specified pipeline",
55 mtxUintShaderConstantOffsets.second.size()));
56 error.showError();
57 throw std::runtime_error(error.getFullErrorMessage());
58 }
59#endif
60
61 // Copy value to root/push constants.
62 pShaderConstantsManager->copyValueToShaderConstant(
63 offsetIt->second, vResourceData[iCurrentFrameResourceIndex]->getIndexIntoArray());
64 }
65
71 inline size_t getResourceDataSizeInBytes() const { return iResourceDataSizeInBytes; }
72
89 [[nodiscard]] virtual std::optional<Error>
90 changeUsedPipelines(const std::unordered_set<Pipeline*>& pipelinesToUse) override;
91
92 protected:
110 static std::variant<std::unique_ptr<ShaderCpuWriteResourceBinding>, Error> create(
111 const std::string& sShaderResourceName,
112 const std::string& sResourceAdditionalInfo,
113 size_t iResourceSizeInBytes,
114 const std::unordered_set<Pipeline*>& pipelinesToUse,
115 const std::function<void*()>& onStartedUpdatingResource,
116 const std::function<void()>& onFinishedUpdatingResource);
117
127 static std::variant<std::unordered_map<Pipeline*, size_t>, Error>
129 const std::unordered_set<Pipeline*>& pipelines, const std::string& sFieldName);
130
148 const std::string& sShaderResourceName,
150 const std::function<void*()>& onStartedUpdatingResource,
151 const std::function<void()>& onFinishedUpdatingResource,
152 const std::unordered_map<Pipeline*, size_t>& uintShaderConstantOffsets);
153
163 [[nodiscard]] virtual std::optional<Error> onAfterAllPipelinesRefreshedResources() override;
164
166 std::pair<std::recursive_mutex, std::unordered_map<Pipeline*, size_t>> mtxUintShaderConstantOffsets;
167
172 std::array<
173 std::unique_ptr<DynamicCpuWriteShaderResourceArraySlot>,
176
177 private:
188 inline void updateResource(size_t iCurrentFrameResourceIndex) {
189 void* pDataToCopy = onStartedUpdatingResource();
190
191 vResourceData[iCurrentFrameResourceIndex]->updateDataBecauseGpuUnused(pDataToCopy);
192
194 }
195
201 const std::function<void*()> onStartedUpdatingResource;
202
204 const std::function<void()> onFinishedUpdatingResource;
205
207 const size_t iResourceDataSizeInBytes = 0;
208 };
209}
Definition: Error.h:27
std::string getFullErrorMessage() const
Definition: Error.cpp:84
void showError() const
Definition: Error.cpp:102
static constexpr unsigned int getFrameResourceCount()
Definition: FrameResourceManager.h:74
Definition: PipelineShaderConstantsManager.hpp:14
void copyValueToShaderConstant(size_t iShaderConstantIndex, unsigned int iValueToCopy)
Definition: PipelineShaderConstantsManager.hpp:75
Definition: Pipeline.h:20
Definition: ShaderCpuWriteResourceBindingManager.h:21
Definition: ShaderCpuWriteResourceBinding.h:25
const std::function< void *()> onStartedUpdatingResource
Definition: ShaderCpuWriteResourceBinding.h:201
void updateResource(size_t iCurrentFrameResourceIndex)
Definition: ShaderCpuWriteResourceBinding.h:188
const std::function< void()> onFinishedUpdatingResource
Definition: ShaderCpuWriteResourceBinding.h:204
virtual std::optional< Error > onAfterAllPipelinesRefreshedResources() override
Definition: ShaderCpuWriteResourceBinding.cpp:138
virtual std::optional< Error > changeUsedPipelines(const std::unordered_set< Pipeline * > &pipelinesToUse) override
Definition: ShaderCpuWriteResourceBinding.cpp:83
static std::variant< std::unique_ptr< ShaderCpuWriteResourceBinding >, Error > create(const std::string &sShaderResourceName, const std::string &sResourceAdditionalInfo, size_t iResourceSizeInBytes, const std::unordered_set< Pipeline * > &pipelinesToUse, const std::function< void *()> &onStartedUpdatingResource, const std::function< void()> &onFinishedUpdatingResource)
Definition: ShaderCpuWriteResourceBinding.cpp:12
std::array< std::unique_ptr< DynamicCpuWriteShaderResourceArraySlot >, FrameResourceManager::getFrameResourceCount()> vResourceData
Definition: ShaderCpuWriteResourceBinding.h:175
const size_t iResourceDataSizeInBytes
Definition: ShaderCpuWriteResourceBinding.h:207
size_t getResourceDataSizeInBytes() const
Definition: ShaderCpuWriteResourceBinding.h:71
void copyResourceIndexToShaderConstants(PipelineShaderConstantsManager *pShaderConstantsManager, Pipeline *pUsedPipeline, size_t iCurrentFrameResourceIndex)
Definition: ShaderCpuWriteResourceBinding.h:39
std::pair< std::recursive_mutex, std::unordered_map< Pipeline *, size_t > > mtxUintShaderConstantOffsets
Definition: ShaderCpuWriteResourceBinding.h:166
static std::variant< std::unordered_map< Pipeline *, size_t >, Error > getUintShaderConstantOffsetsFromPipelines(const std::unordered_set< Pipeline * > &pipelines, const std::string &sFieldName)
Definition: ShaderCpuWriteResourceBinding.cpp:100
Definition: ShaderResourceBinding.h:27
std::string getShaderResourceName() const
Definition: ShaderResourceBinding.cpp:11
const std::string sShaderResourceName
Definition: ShaderResourceBinding.h:82