Nameless Engine
Loading...
Searching...
No Matches
PipelineShaderConstantsManager.hpp
1#pragma once
2
3// Standard.
4#include <stdexcept>
5#include <format>
6#include <vector>
7
8// Custom.
9#include "misc/Error.h"
10#include "io/Logger.h"
11
12namespace ne {
15 public:
17 using variable_type = unsigned int;
18
22
26 static constexpr auto pShadowPassLightInfoIndex = "iShadowPassLightInfoIndex";
27 };
28
34 static constexpr size_t getMaxConstantsSizeInBytes() { return iMaxShaderConstantsSizeInBytes; }
35
45 PipelineShaderConstantsManager(size_t iVariableCount) {
46 // Make sure we won't exceed maximum allowed size limit.
47 const size_t iSizeInBytes = iVariableCount * sizeof(variable_type);
48 if (iSizeInBytes > iMaxShaderConstantsSizeInBytes) [[unlikely]] {
49 Error error(std::format(
50 "failed to create shader constants manager with size {} bytes because the maximum "
51 "allowed size is {} bytes",
52 iSizeInBytes,
54 error.showError();
55 throw std::runtime_error(error.getFullErrorMessage());
56 }
57
58 // Make sure the specified size is not zero.
59 if (iVariableCount == 0) [[unlikely]] {
60 Error error("failed to create shader constants manager because the specified size is zero");
61 error.showError();
62 throw std::runtime_error(error.getFullErrorMessage());
63 }
64
65 // Prepare data.
66 vConstantsData.resize(iVariableCount, 0);
67 }
68
75 inline void copyValueToShaderConstant(size_t iShaderConstantIndex, unsigned int iValueToCopy) {
76 // Make sure we don't access out of bounds.
77 if (iShaderConstantIndex >= vConstantsData.size()) [[unlikely]] {
78 Logger::get().error(std::format(
79 "the specified push/root constant index {} is out of bounds (max: {})",
80 iShaderConstantIndex,
81 vConstantsData.size() - 1));
82 return;
83 }
84
85 // Set value.
86 vConstantsData[iShaderConstantIndex] = iValueToCopy;
87 }
88
94 inline unsigned int getTotalSizeInBytes() const {
95 // We can safely cast to `uint` here since push/root constants are very small in size.
96 return static_cast<unsigned int>(vConstantsData.size() * sizeof(variable_type));
97 }
98
104 inline unsigned int getVariableCount() const {
105 return static_cast<unsigned int>(vConstantsData.size());
106 }
107
113 inline void* getData() { return vConstantsData.data(); }
114
115 private:
117 std::vector<variable_type> vConstantsData;
118
120 static constexpr size_t iMaxShaderConstantsSizeInBytes =
121 128; // NOLINT: guaranteed minimum supported size from Vulkan specs, we should stick with it to
122 // support as much GPUs as possible
123 };
124} // namespace ne
Definition: Error.h:27
std::string getFullErrorMessage() const
Definition: Error.cpp:84
void showError() const
Definition: Error.cpp:102
void error(std::string_view sText, const std::source_location location=std::source_location::current()) const
Definition: Logger.cpp:75
static Logger & get()
Definition: Logger.cpp:41
Definition: PipelineShaderConstantsManager.hpp:14
unsigned int variable_type
Definition: PipelineShaderConstantsManager.hpp:17
static constexpr size_t iMaxShaderConstantsSizeInBytes
Definition: PipelineShaderConstantsManager.hpp:120
void * getData()
Definition: PipelineShaderConstantsManager.hpp:113
static constexpr size_t getMaxConstantsSizeInBytes()
Definition: PipelineShaderConstantsManager.hpp:34
PipelineShaderConstantsManager(size_t iVariableCount)
Definition: PipelineShaderConstantsManager.hpp:45
unsigned int getVariableCount() const
Definition: PipelineShaderConstantsManager.hpp:104
std::vector< variable_type > vConstantsData
Definition: PipelineShaderConstantsManager.hpp:117
unsigned int getTotalSizeInBytes() const
Definition: PipelineShaderConstantsManager.hpp:94
void copyValueToShaderConstant(size_t iShaderConstantIndex, unsigned int iValueToCopy)
Definition: PipelineShaderConstantsManager.hpp:75
Definition: PipelineShaderConstantsManager.hpp:24
static constexpr auto pShadowPassLightInfoIndex
Definition: PipelineShaderConstantsManager.hpp:26