Nameless Engine
Loading...
Searching...
No Matches
GlslShaderTextureResourceBinding.h
1#pragma once
2
3// Standard.
4#include <string>
5#include <memory>
6#include <variant>
7#include <unordered_set>
8
9// Custom.
10#include "shader/general/resource/binding/ShaderResourceBinding.h"
11#include "render/general/pipeline/PipelineShaderConstantsManager.hpp"
12#include "shader/general/resource/ShaderArrayIndexManager.h"
13
14// External.
15#include "vulkan/vulkan.h"
16
17namespace ne {
18 class TextureHandle;
19 class Pipeline;
20 class VulkanPipeline;
21
24 // Only the manager should be able to create such resources.
26
31
39 size_t iPushConstantIndex, std::unique_ptr<ShaderArrayIndex> pShaderArrayIndex) {
40 this->iPushConstantIndex = iPushConstantIndex;
41 this->pShaderArrayIndex = std::move(pShaderArrayIndex);
42 }
43
46
53
56
58 std::unique_ptr<ShaderArrayIndex> pShaderArrayIndex;
59 };
60
61 public:
62 virtual ~GlslShaderTextureResourceBinding() override = default;
63
69 std::string getPathToTextureResource();
70
78 PipelineShaderConstantsManager* pPushConstantsManager, VulkanPipeline* pUsedPipeline) {
79 // Since pipelines won't change here (because we are inside of the `draw` function)
80 // we don't need to lock the mutex here.
81
82 // Find push constant index of this pipeline.
83 const auto it = mtxPushConstantIndices.second.find(pUsedPipeline);
84 if (it == mtxPushConstantIndices.second.end()) [[unlikely]] {
85 Error error(std::format(
86 "shader resource \"{}\" was requested to set its push constant "
87 "index but this shader resource does not reference the specified pipeline",
89 mtxPushConstantIndices.second.size()));
90 error.showError();
91 throw std::runtime_error(error.getFullErrorMessage());
92 }
93
94 // Copy value to push constants.
95 pPushConstantsManager->copyValueToShaderConstant(
96 it->second.iPushConstantIndex, it->second.pShaderArrayIndex->getActualIndex());
97 }
98
110 [[nodiscard]] virtual std::optional<Error>
111 useNewTexture(std::unique_ptr<TextureHandle> pTextureToUse) override;
112
129 [[nodiscard]] virtual std::optional<Error>
130 changeUsedPipelines(const std::unordered_set<Pipeline*>& pipelinesToUse) override;
131
132 protected:
145 const std::string& sResourceName,
146 std::unique_ptr<TextureHandle> pTextureToUse,
147 std::unordered_map<VulkanPipeline*, PushConstantIndices> pushConstantIndices);
148
158 [[nodiscard]] virtual std::optional<Error> onAfterAllPipelinesRefreshedResources() override;
159
160 private:
171 static std::variant<std::unique_ptr<ShaderTextureResourceBinding>, Error> create(
172 const std::string& sShaderResourceName,
173 const std::unordered_set<Pipeline*>& pipelinesToUse,
174 std::unique_ptr<TextureHandle> pTextureToUse);
175
185 static std::variant<std::unique_ptr<ShaderArrayIndex>, Error> getTextureIndexInShaderArray(
186 const std::string& sShaderResourceName, VulkanPipeline* pPipelineToLookIn);
187
199 static std::optional<Error> bindTextureToShaderDescriptorArray(
200 const std::string& sShaderResourceName,
201 VulkanPipeline* pPipelineWithDescriptors,
202 VkImageView pTextureView,
203 unsigned int iIndexIntoShaderArray);
204
206 std::pair<std::mutex, std::unique_ptr<TextureHandle>> mtxUsedTexture;
207
209 std::pair<std::recursive_mutex, std::unordered_map<VulkanPipeline*, PushConstantIndices>>
211 };
212} // namespace ne
Definition: Error.h:27
std::string getFullErrorMessage() const
Definition: Error.cpp:84
void showError() const
Definition: Error.cpp:102
Definition: GlslShaderTextureResourceBinding.h:23
std::string getPathToTextureResource()
Definition: GlslShaderTextureResourceBinding.cpp:235
void copyResourceIndexToPushConstants(PipelineShaderConstantsManager *pPushConstantsManager, VulkanPipeline *pUsedPipeline)
Definition: GlslShaderTextureResourceBinding.h:77
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: GlslShaderTextureResourceBinding.cpp:16
virtual std::optional< Error > changeUsedPipelines(const std::unordered_set< Pipeline * > &pipelinesToUse) override
Definition: GlslShaderTextureResourceBinding.cpp:274
std::pair< std::recursive_mutex, std::unordered_map< VulkanPipeline *, PushConstantIndices > > mtxPushConstantIndices
Definition: GlslShaderTextureResourceBinding.h:210
virtual std::optional< Error > onAfterAllPipelinesRefreshedResources() override
Definition: GlslShaderTextureResourceBinding.cpp:189
std::pair< std::mutex, std::unique_ptr< TextureHandle > > mtxUsedTexture
Definition: GlslShaderTextureResourceBinding.h:206
static std::optional< Error > bindTextureToShaderDescriptorArray(const std::string &sShaderResourceName, VulkanPipeline *pPipelineWithDescriptors, VkImageView pTextureView, unsigned int iIndexIntoShaderArray)
Definition: GlslShaderTextureResourceBinding.cpp:125
virtual std::optional< Error > useNewTexture(std::unique_ptr< TextureHandle > pTextureToUse) override
Definition: GlslShaderTextureResourceBinding.cpp:242
static std::variant< std::unique_ptr< ShaderArrayIndex >, Error > getTextureIndexInShaderArray(const std::string &sShaderResourceName, VulkanPipeline *pPipelineToLookIn)
Definition: GlslShaderTextureResourceBinding.cpp:96
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
Definition: VulkanPipeline.h:21
Definition: GlslShaderTextureResourceBinding.h:28
size_t iPushConstantIndex
Definition: GlslShaderTextureResourceBinding.h:55
PushConstantIndices & operator=(PushConstantIndices &&)=default
std::unique_ptr< ShaderArrayIndex > pShaderArrayIndex
Definition: GlslShaderTextureResourceBinding.h:58
PushConstantIndices(size_t iPushConstantIndex, std::unique_ptr< ShaderArrayIndex > pShaderArrayIndex)
Definition: GlslShaderTextureResourceBinding.h:38