Nameless Engine
Loading...
Searching...
No Matches
GlslShaderTextureResource.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/resources/ShaderResource.h"
11#include "material/TextureManager.h"
12#include "render/general/pipeline/PipelineShaderConstantsManager.hpp"
13#include "shader/general/resources/ShaderArrayIndexManager.h"
14
15// External.
16#include "vulkan/vulkan.h"
17
18namespace ne {
19 class Pipeline;
20 class VulkanPipeline;
21
24 // Only shader resource 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 ~GlslShaderTextureResource() override = default;
63
69 std::string getPathToTextureResource();
70
80 PipelineShaderConstantsManager* pPushConstantsManager) {
81 // Since pipelines won't change here (because we are inside of the `draw` function)
82 // we don't need to lock the mutex here.
83
84#if defined(DEBUG)
85 // Self check: make sure there is indeed just 1 pipeline.
86 if (mtxPushConstantIndices.second.size() != 1) [[unlikely]] {
87 Error error(std::format(
88 "shader resource \"{}\" was requested to set its push constant "
89 "index of the only used pipeline but this shader resource references "
90 "{} pipeline(s)",
92 mtxPushConstantIndices.second.size()));
93 error.showError();
94 throw std::runtime_error(error.getFullErrorMessage());
95 }
96#endif
97
98 // Save iterator to the first item.
99 const auto it = mtxPushConstantIndices.second.begin();
100
101 // Copy value to push constants.
102 pPushConstantsManager->copyValueToShaderConstant(
103 it->second.iPushConstantIndex, it->second.pShaderArrayIndex->getActualIndex());
104 }
105
113 PipelineShaderConstantsManager* pPushConstantsManager, VulkanPipeline* pUsedPipeline) {
114 // Since pipelines won't change here (because we are inside of the `draw` function)
115 // we don't need to lock the mutex here.
116
117 // Find push constant index of this pipeline.
118 const auto it = mtxPushConstantIndices.second.find(pUsedPipeline);
119 if (it == mtxPushConstantIndices.second.end()) [[unlikely]] {
120 Error error(std::format(
121 "shader resource \"{}\" was requested to set its push constant "
122 "index but this shader resource does not reference the specified pipeline",
124 mtxPushConstantIndices.second.size()));
125 error.showError();
126 throw std::runtime_error(error.getFullErrorMessage());
127 }
128
129 // Copy value to push constants.
130 pPushConstantsManager->copyValueToShaderConstant(
131 it->second.iPushConstantIndex, it->second.pShaderArrayIndex->getActualIndex());
132 }
133
145 [[nodiscard]] virtual std::optional<Error>
146 useNewTexture(std::unique_ptr<TextureHandle> pTextureToUse) override;
147
164 [[nodiscard]] virtual std::optional<Error>
165 changeUsedPipelines(const std::unordered_set<Pipeline*>& pipelinesToUse) override;
166
167 protected:
180 const std::string& sResourceName,
181 std::unique_ptr<TextureHandle> pTextureToUse,
182 std::unordered_map<VulkanPipeline*, PushConstantIndices> pushConstantIndices);
183
193 [[nodiscard]] virtual std::optional<Error> onAfterAllPipelinesRefreshedResources() override;
194
195 private:
206 static std::variant<std::unique_ptr<ShaderTextureResource>, Error> create(
207 const std::string& sShaderResourceName,
208 const std::unordered_set<Pipeline*>& pipelinesToUse,
209 std::unique_ptr<TextureHandle> pTextureToUse);
210
220 static std::variant<std::unique_ptr<ShaderArrayIndex>, Error> getTextureIndexInShaderArray(
221 const std::string& sShaderResourceName, VulkanPipeline* pPipelineToLookIn);
222
234 static std::optional<Error> bindTextureToShaderDescriptorArray(
235 const std::string& sShaderResourceName,
236 VulkanPipeline* pPipelineWithDescriptors,
237 VkImageView pTextureView,
238 unsigned int iIndexIntoShaderArray);
239
241 std::pair<std::mutex, std::unique_ptr<TextureHandle>> mtxUsedTexture;
242
244 std::pair<std::recursive_mutex, std::unordered_map<VulkanPipeline*, PushConstantIndices>>
246 };
247} // namespace ne
Definition: Error.h:27
std::string getFullErrorMessage() const
Definition: Error.cpp:84
void showError() const
Definition: Error.cpp:102
Definition: GlslShaderTextureResource.h:23
std::string getPathToTextureResource()
Definition: GlslShaderTextureResource.cpp:215
static std::variant< std::unique_ptr< ShaderArrayIndex >, Error > getTextureIndexInShaderArray(const std::string &sShaderResourceName, VulkanPipeline *pPipelineToLookIn)
Definition: GlslShaderTextureResource.cpp:79
virtual std::optional< Error > changeUsedPipelines(const std::unordered_set< Pipeline * > &pipelinesToUse) override
Definition: GlslShaderTextureResource.cpp:252
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: GlslShaderTextureResource.cpp:13
virtual std::optional< Error > useNewTexture(std::unique_ptr< TextureHandle > pTextureToUse) override
Definition: GlslShaderTextureResource.cpp:222
void copyResourceIndexOfOnlyPipelineToPushConstants(PipelineShaderConstantsManager *pPushConstantsManager)
Definition: GlslShaderTextureResource.h:79
void copyResourceIndexOfPipelineToPushConstants(PipelineShaderConstantsManager *pPushConstantsManager, VulkanPipeline *pUsedPipeline)
Definition: GlslShaderTextureResource.h:112
virtual std::optional< Error > onAfterAllPipelinesRefreshedResources() override
Definition: GlslShaderTextureResource.cpp:172
static std::optional< Error > bindTextureToShaderDescriptorArray(const std::string &sShaderResourceName, VulkanPipeline *pPipelineWithDescriptors, VkImageView pTextureView, unsigned int iIndexIntoShaderArray)
Definition: GlslShaderTextureResource.cpp:108
std::pair< std::mutex, std::unique_ptr< TextureHandle > > mtxUsedTexture
Definition: GlslShaderTextureResource.h:241
std::pair< std::recursive_mutex, std::unordered_map< VulkanPipeline *, PushConstantIndices > > mtxPushConstantIndices
Definition: GlslShaderTextureResource.h:245
Definition: PipelineShaderConstantsManager.hpp:14
void copyValueToShaderConstant(size_t iShaderConstantIndex, unsigned int iValueToCopy)
Definition: PipelineShaderConstantsManager.hpp:75
std::string sResourceName
Definition: ShaderResource.h:82
std::string getResourceName() const
Definition: ShaderResource.cpp:12
Definition: ShaderTextureResourceManager.h:27
Definition: ShaderResource.h:86
Definition: VulkanPipeline.h:21
Definition: GlslShaderTextureResource.h:28
PushConstantIndices(PushConstantIndices &&)=default
PushConstantIndices(size_t iPushConstantIndex, std::unique_ptr< ShaderArrayIndex > pShaderArrayIndex)
Definition: GlslShaderTextureResource.h:38
std::unique_ptr< ShaderArrayIndex > pShaderArrayIndex
Definition: GlslShaderTextureResource.h:58
size_t iPushConstantIndex
Definition: GlslShaderTextureResource.h:55
PushConstantIndices & operator=(PushConstantIndices &&)=default