Nameless Engine
Loading...
Searching...
No Matches
HlslShaderTextureResource.h
1#pragma once
2
3// Standard.
4#include <string>
5#include <memory>
6#include <unordered_set>
7#include <variant>
8
9// Custom.
10#include "shader/general/resources/ShaderResource.h"
11#include "render/directx/descriptors/DirectXDescriptorHeap.h"
12
13namespace ne {
14 class Pipeline;
15 class DirectXPso;
16
19 // Only shader resource manager should be able to create such resources.
21
22 public:
23 virtual ~HlslShaderTextureResource() override = default;
24
34 const ComPtr<ID3D12GraphicsCommandList>& pCommandList) const {
35 // we don't need to lock texture mutex here because when this function is called it's called
36 // inside of the `draw` function and shader resource mutex (outside) is locked which means:
37 // - if our old texture resource is being destroyed before the GPU resource is deleted
38 // the rendering will be stopped and thus we won't be in the `draw` function
39 // - although after the old texture is destroyed we can start the `draw` function
40 // the renderer will still need to lock shader resources mutex (in material for example)
41 // but when the texture is being changed (in material for example) the material locks
42 // shader resources mutex which means that when we get in this function the texture will
43 // always be valid and so its GPU virtual address too
44
45 // only descriptor index (offset) may change due to heap re-creation (expand/shrink)
46 // but before a heap is destroyed (to be re-created) all rendering is stopped so
47 // that no new frames will be queued until all descriptor offsets are updated
48
49#if defined(DEBUG)
50 // Self check: make sure there is indeed just 1 pipeline.
51 if (mtxRootParameterIndices.second.size() != 1) [[unlikely]] {
52 Error error(std::format(
53 "shader resource \"{}\" was requested to set its graphics root descriptor "
54 "table of the only used pipeline but this shader resource references "
55 "{} pipeline(s)",
57 mtxRootParameterIndices.second.size()));
58 error.showError();
59 throw std::runtime_error(error.getFullErrorMessage());
60 }
61#endif
62
63 // Set table.
64 pCommandList->SetGraphicsRootDescriptorTable(
65 mtxRootParameterIndices.second.begin()->second,
66 D3D12_GPU_DESCRIPTOR_HANDLE{
67 pSrvHeap->getInternalHeap()->GetGPUDescriptorHandleForHeapStart().ptr +
68 pTextureSrv->getDescriptorOffsetInDescriptors() * iSrvDescriptorSize});
69 }
70
78 const ComPtr<ID3D12GraphicsCommandList>& pCommandList, DirectXPso* pUsedPipeline) const {
79 // same thing as above, we don't need to lock mutexes here
80
81 // Find root parameter index of this pipeline.
82 const auto it = mtxRootParameterIndices.second.find(pUsedPipeline);
83
84#if defined(DEBUG)
85 if (it == mtxRootParameterIndices.second.end()) [[unlikely]] {
86 Error error(std::format(
87 "shader resource \"{}\" was requested to set its graphics root descriptor table "
88 "but this shader resource does not reference the specified pipeline",
90 mtxRootParameterIndices.second.size()));
91 error.showError();
92 throw std::runtime_error(error.getFullErrorMessage());
93 }
94#endif
95
96 // Set table.
97 pCommandList->SetGraphicsRootDescriptorTable(
98 it->second,
99 D3D12_GPU_DESCRIPTOR_HANDLE{
100 pSrvHeap->getInternalHeap()->GetGPUDescriptorHandleForHeapStart().ptr +
101 pTextureSrv->getDescriptorOffsetInDescriptors() * iSrvDescriptorSize});
102 }
103
115 [[nodiscard]] virtual std::optional<Error>
116 useNewTexture(std::unique_ptr<TextureHandle> pTextureToUse) override;
117
134 [[nodiscard]] virtual std::optional<Error>
135 changeUsedPipelines(const std::unordered_set<Pipeline*>& pipelinesToUse) override;
136
137 protected:
149 const std::string& sResourceName,
150 std::unique_ptr<TextureHandle> pTextureToUse,
151 const std::unordered_map<DirectXPso*, UINT>& rootParameterIndices);
152
162 [[nodiscard]] virtual std::optional<Error> onAfterAllPipelinesRefreshedResources() override;
163
164 private:
175 static std::variant<std::unique_ptr<ShaderTextureResource>, Error> create(
176 const std::string& sShaderResourceName,
177 const std::unordered_set<Pipeline*>& pipelinesToUse,
178 std::unique_ptr<TextureHandle> pTextureToUse);
179
181 std::pair<std::mutex, std::unique_ptr<TextureHandle>> mtxUsedTexture;
182
185
188
191
193 std::pair<std::recursive_mutex, std::unordered_map<DirectXPso*, UINT>> mtxRootParameterIndices;
194 };
195} // namespace ne
Definition: DirectXDescriptorHeap.h:198
Definition: DirectXDescriptor.h:18
Definition: DirectXPso.h:22
Definition: Error.h:27
std::string getFullErrorMessage() const
Definition: Error.cpp:84
void showError() const
Definition: Error.cpp:102
Definition: HlslShaderTextureResource.h:18
virtual std::optional< Error > changeUsedPipelines(const std::unordered_set< Pipeline * > &pipelinesToUse) override
Definition: HlslShaderTextureResource.cpp:170
virtual std::optional< Error > useNewTexture(std::unique_ptr< TextureHandle > pTextureToUse) override
Definition: HlslShaderTextureResource.cpp:136
DirectXDescriptor * pTextureSrv
Definition: HlslShaderTextureResource.h:184
void setGraphicsRootDescriptorTableOfPipeline(const ComPtr< ID3D12GraphicsCommandList > &pCommandList, DirectXPso *pUsedPipeline) const
Definition: HlslShaderTextureResource.h:77
virtual std::optional< Error > onAfterAllPipelinesRefreshedResources() override
Definition: HlslShaderTextureResource.cpp:114
DirectXDescriptorHeap * pSrvHeap
Definition: HlslShaderTextureResource.h:187
std::pair< std::recursive_mutex, std::unordered_map< DirectXPso *, UINT > > mtxRootParameterIndices
Definition: HlslShaderTextureResource.h:193
std::pair< std::mutex, std::unique_ptr< TextureHandle > > mtxUsedTexture
Definition: HlslShaderTextureResource.h:181
UINT iSrvDescriptorSize
Definition: HlslShaderTextureResource.h:190
void setGraphicsRootDescriptorTableOfOnlyPipeline(const ComPtr< ID3D12GraphicsCommandList > &pCommandList) const
Definition: HlslShaderTextureResource.h:33
static std::variant< std::unique_ptr< ShaderTextureResource >, Error > create(const std::string &sShaderResourceName, const std::unordered_set< Pipeline * > &pipelinesToUse, std::unique_ptr< TextureHandle > pTextureToUse)
Definition: HlslShaderTextureResource.cpp:16
std::string sResourceName
Definition: ShaderResource.h:82
std::string getResourceName() const
Definition: ShaderResource.cpp:12
Definition: ShaderTextureResourceManager.h:27
Definition: ShaderResource.h:86