Nameless Engine
Loading...
Searching...
No Matches
HlslShaderTextureResourceBinding.h
1#pragma once
2
3// Standard.
4#include <string>
5#include <memory>
6#include <unordered_set>
7#include <variant>
8#include <mutex>
9
10// Custom.
11#include "shader/general/resource/binding/ShaderResourceBinding.h"
12#include "render/directx/descriptors/DirectXDescriptorHeap.h"
13#include "render/general/pipeline/PipelineShaderConstantsManager.hpp"
14#include "render/directx/resource/DirectXResource.h"
15
16namespace ne {
17 class TextureHandle;
18 class Pipeline;
19 class DirectXPso;
20
23 // Only the manager should be able to create such resources.
25
26 public:
27 virtual ~HlslShaderTextureResourceBinding() override = default;
28
36 PipelineShaderConstantsManager* pShaderConstantsManager, DirectXPso* pUsedPipeline) {
37 // Since pipelines won't change here (because we are inside of the `draw` function)
38 // we don't need to lock the mutex here.
39
40 // Find root constant index for this pipeline.
41 const auto it = mtxUsedPipelineDescriptorRanges.second.find(pUsedPipeline);
42 if (it == mtxUsedPipelineDescriptorRanges.second.end()) [[unlikely]] {
43 Error error(std::format(
44 "shader resource \"{}\" was requested to set its root constant "
45 "index but this shader resource does not reference the specified pipeline",
47 mtxUsedPipelineDescriptorRanges.second.size()));
48 error.showError();
49 throw std::runtime_error(error.getFullErrorMessage());
50 }
51
52 const auto& [pSrvDescriptorRange, iShaderConstantIndex] =
53 mtxUsedPipelineDescriptorRanges.second.begin()->second;
54
55 // Query texture's SRV descriptor offset in the descriptor range.
56 unsigned int iRootConstantValue = 0;
57 {
58 std::scoped_lock textureGuard(mtxUsedTexture.first);
59
60 const auto pDirectXTexture =
61 reinterpret_cast<DirectXResource*>(mtxUsedTexture.second->getResource());
62
63 // Calculate descriptor offset. This may not be as fast as we want but this is the price we
64 // pay for having a simple approach. We could have cached the offset but we would need to keep
65 // the cached offset updated after the range resizes which seems like a complicated thing.
66 auto result = pSrvDescriptorRange->getResourceDescriptorOffsetFromRangeStart(
67 pDirectXTexture, DirectXDescriptorType::SRV);
68 if (std::holds_alternative<Error>(result)) [[unlikely]] {
69 auto error = std::get<Error>(std::move(result));
70 error.addCurrentLocationToErrorStack();
71 error.showError();
72 throw std::runtime_error(error.getFullErrorMessage());
73 }
74
75 iRootConstantValue = std::get<unsigned int>(result);
76 }
77
78 // Copy value to root constants.
79 pShaderConstantsManager->copyValueToShaderConstant(iShaderConstantIndex, iRootConstantValue);
80 }
81
93 [[nodiscard]] virtual std::optional<Error>
94 useNewTexture(std::unique_ptr<TextureHandle> pTextureToUse) override;
95
112 [[nodiscard]] virtual std::optional<Error>
113 changeUsedPipelines(const std::unordered_set<Pipeline*>& pipelinesToUse) override;
114
115 protected:
128 const std::string& sResourceName,
129 std::unique_ptr<TextureHandle> pTextureToUse,
130 std::unordered_map<DirectXPso*, std::pair<ContinuousDirectXDescriptorRange*, size_t>>&&
131 usedDescriptorRanges);
132
142 [[nodiscard]] virtual std::optional<Error> onAfterAllPipelinesRefreshedResources() override;
143
144 private:
155 static std::variant<std::unique_ptr<ShaderTextureResourceBinding>, Error> create(
156 const std::string& sShaderResourceName,
157 const std::unordered_set<Pipeline*>& pipelinesToUse,
158 std::unique_ptr<TextureHandle> pTextureToUse);
159
170 static std::variant<std::pair<ContinuousDirectXDescriptorRange*, size_t>, Error>
172 DirectXPso* pPipeline, const std::string& sShaderResourceName);
173
175 std::pair<std::mutex, std::unique_ptr<TextureHandle>> mtxUsedTexture;
176
186 std::pair<
187 std::recursive_mutex,
188 std::unordered_map<DirectXPso*, std::pair<ContinuousDirectXDescriptorRange*, size_t>>>
190
195 static constexpr bool bBindSrvToCubemapFaces = false;
196 };
197} // namespace ne
Definition: DirectXPso.h:22
Definition: DirectXResource.h:32
Definition: Error.h:27
std::string getFullErrorMessage() const
Definition: Error.cpp:84
void showError() const
Definition: Error.cpp:102
Definition: HlslShaderTextureResourceBinding.h:22
void copyResourceIndexToRootConstants(PipelineShaderConstantsManager *pShaderConstantsManager, DirectXPso *pUsedPipeline)
Definition: HlslShaderTextureResourceBinding.h:35
std::pair< std::recursive_mutex, std::unordered_map< DirectXPso *, std::pair< ContinuousDirectXDescriptorRange *, size_t > > > mtxUsedPipelineDescriptorRanges
Definition: HlslShaderTextureResourceBinding.h:189
virtual std::optional< Error > changeUsedPipelines(const std::unordered_set< Pipeline * > &pipelinesToUse) override
Definition: HlslShaderTextureResourceBinding.cpp:233
static std::variant< std::unique_ptr< ShaderTextureResourceBinding >, Error > create(const std::string &sShaderResourceName, const std::unordered_set< Pipeline * > &pipelinesToUse, std::unique_ptr< TextureHandle > pTextureToUse)
Definition: HlslShaderTextureResourceBinding.cpp:101
std::pair< std::mutex, std::unique_ptr< TextureHandle > > mtxUsedTexture
Definition: HlslShaderTextureResourceBinding.h:175
virtual std::optional< Error > useNewTexture(std::unique_ptr< TextureHandle > pTextureToUse) override
Definition: HlslShaderTextureResourceBinding.cpp:205
static constexpr bool bBindSrvToCubemapFaces
Definition: HlslShaderTextureResourceBinding.h:195
virtual std::optional< Error > onAfterAllPipelinesRefreshedResources() override
Definition: HlslShaderTextureResourceBinding.cpp:185
static std::variant< std::pair< ContinuousDirectXDescriptorRange *, size_t >, Error > getSrvDescriptorRangeAndRootConstantIndex(DirectXPso *pPipeline, const std::string &sShaderResourceName)
Definition: HlslShaderTextureResourceBinding.cpp:17
Definition: PipelineShaderConstantsManager.hpp:14
void copyValueToShaderConstant(size_t iShaderConstantIndex, unsigned int iValueToCopy)
Definition: PipelineShaderConstantsManager.hpp:75
std::string getShaderResourceName() const
Definition: ShaderResourceBinding.cpp:11
const std::string sShaderResourceName
Definition: ShaderResourceBinding.h:82
Definition: ShaderTextureResourceBindingManager.h:27
Definition: ShaderResourceBinding.h:86