| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "../../../../Shaders/Generated/ShaderCompilerTypes.h" |
| 4 |
|
| 5 |
#include <cstddef> |
| 6 |
#include <cstdint> |
| 7 |
#include <cstring> |
| 8 |
#include <vector> |
| 9 |
|
| 10 |
namespace nCine::RHI::GS |
| 11 |
{ |
| 12 |
class GsShaderProgram; |
| 13 |
|
| 14 |
namespace UniformTypeInfo |
| 15 |
{ |
| 16 |
/** @brief Number of scalar components of a reflected uniform type (0 for aggregates) */ |
| 17 |
inline std::uint32_t ComponentCount(ShaderCompiler::UniformType type) |
| 18 |
{ |
| 19 |
switch (type) { |
| 20 |
case ShaderCompiler::UniformType::Float: |
| 21 |
case ShaderCompiler::UniformType::Int: |
| 22 |
case ShaderCompiler::UniformType::UInt: |
| 23 |
case ShaderCompiler::UniformType::Bool: |
| 24 |
case ShaderCompiler::UniformType::Sampler2D: |
| 25 |
case ShaderCompiler::UniformType::Sampler3D: |
| 26 |
case ShaderCompiler::UniformType::SamplerCube: |
| 27 |
return 1; |
| 28 |
case ShaderCompiler::UniformType::Vec2: |
| 29 |
case ShaderCompiler::UniformType::IVec2: |
| 30 |
case ShaderCompiler::UniformType::UVec2: |
| 31 |
case ShaderCompiler::UniformType::BVec2: |
| 32 |
return 2; |
| 33 |
case ShaderCompiler::UniformType::Vec3: |
| 34 |
case ShaderCompiler::UniformType::IVec3: |
| 35 |
case ShaderCompiler::UniformType::UVec3: |
| 36 |
case ShaderCompiler::UniformType::BVec3: |
| 37 |
return 3; |
| 38 |
case ShaderCompiler::UniformType::Vec4: |
| 39 |
case ShaderCompiler::UniformType::IVec4: |
| 40 |
case ShaderCompiler::UniformType::UVec4: |
| 41 |
case ShaderCompiler::UniformType::BVec4: |
| 42 |
return 4; |
| 43 |
case ShaderCompiler::UniformType::Mat2: return 4; |
| 44 |
case ShaderCompiler::UniformType::Mat3: return 9; |
| 45 |
case ShaderCompiler::UniformType::Mat4: return 16; |
| 46 |
default: return 0; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/** @brief Returns `true` if the basic component type of the uniform is floating point */ |
| 51 |
inline bool IsFloat(ShaderCompiler::UniformType type) |
| 52 |
{ |
| 53 |
switch (type) { |
| 54 |
case ShaderCompiler::UniformType::Float: |
| 55 |
case ShaderCompiler::UniformType::Vec2: |
| 56 |
case ShaderCompiler::UniformType::Vec3: |
| 57 |
case ShaderCompiler::UniformType::Vec4: |
| 58 |
case ShaderCompiler::UniformType::Mat2: |
| 59 |
case ShaderCompiler::UniformType::Mat3: |
| 60 |
case ShaderCompiler::UniformType::Mat4: |
| 61 |
return true; |
| 62 |
default: |
| 63 |
return false; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
@brief Reflected metadata of a single active uniform (aliased as `RHI::Uniform`) |
| 70 |
|
| 71 |
Holds everything the GS backend needs about one uniform imported from the offline reflection: its name, |
| 72 |
scalar/vector/matrix type, array size, a synthetic sequential location and, for uniforms living inside |
| 73 |
a block, the owning block index and std140 byte offset. It carries no value storage - a |
| 74 |
@ref GsUniformCache holds the value. |
| 75 |
*/ |
| 76 |
class GsUniform |
| 77 |
{ |
| 78 |
friend class GsShaderProgram; |
| 79 |
friend class GsUniformBlock; |
| 80 |
|
| 81 |
public: |
| 82 |
/** @brief Maximum length of a uniform name, including the terminating null character */ |
| 83 |
static constexpr std::uint32_t MaxNameLength = 48; |
| 84 |
|
| 85 |
GsUniform() |
| 86 |
: _type(ShaderCompiler::UniformType::Float), _size(0), _location(-1), _blockIndex(-1), _offset(0), _owner(nullptr) |
| 87 |
{ |
| 88 |
_name[0] = '\0'; |
| 89 |
} |
| 90 |
GsUniform(GsShaderProgram* owner, const char* name, ShaderCompiler::UniformType type, std::int32_t arraySize, std::int32_t location) |
| 91 |
: _type(type), _size(arraySize > 0 ? arraySize : 1), _location(location), _blockIndex(-1), _offset(0), _owner(owner) |
| 92 |
{ |
| 93 |
SetName(name); |
| 94 |
} |
| 95 |
|
| 96 |
inline std::int32_t GetLocation() const { |
| 97 |
return _location; |
| 98 |
} |
| 99 |
inline std::int32_t GetBlockIndex() const { |
| 100 |
return _blockIndex; |
| 101 |
} |
| 102 |
inline std::int32_t GetSize() const { |
| 103 |
return _size; |
| 104 |
} |
| 105 |
/** @brief Returns the reflected (backend-neutral) type of the uniform */ |
| 106 |
inline ShaderCompiler::UniformType GetType() const { |
| 107 |
return _type; |
| 108 |
} |
| 109 |
inline std::int32_t GetOffset() const { |
| 110 |
return _offset; |
| 111 |
} |
| 112 |
inline const char* GetName() const { |
| 113 |
return _name; |
| 114 |
} |
| 115 |
inline GsShaderProgram* GetOwner() const { |
| 116 |
return _owner; |
| 117 |
} |
| 118 |
inline std::uint32_t GetComponentCount() const { |
| 119 |
return UniformTypeInfo::ComponentCount(_type); |
| 120 |
} |
| 121 |
inline bool IsFloat() const { |
| 122 |
return UniformTypeInfo::IsFloat(_type); |
| 123 |
} |
| 124 |
inline std::uint32_t GetMemorySize() const { |
| 125 |
return std::uint32_t(GetSize()) * GetComponentCount() * 4u; |
| 126 |
} |
| 127 |
|
| 128 |
private: |
| 129 |
char _name[MaxNameLength]; |
| 130 |
ShaderCompiler::UniformType _type; |
| 131 |
std::int32_t _size; |
| 132 |
std::int32_t _location; |
| 133 |
std::int32_t _blockIndex; |
| 134 |
std::int32_t _offset; |
| 135 |
GsShaderProgram* _owner; |
| 136 |
|
| 137 |
void SetName(const char* name) { |
| 138 |
std::size_t length = std::strlen(name); |
| 139 |
if (length >= MaxNameLength) { |
| 140 |
length = MaxNameLength - 1; |
| 141 |
} |
| 142 |
std::memcpy(_name, name, length); |
| 143 |
_name[length] = '\0'; |
| 144 |
} |
| 145 |
}; |
| 146 |
|
| 147 |
/** |
| 148 |
@brief Reflected metadata of a single active uniform block (aliased as `RHI::UniformBlock`) |
| 149 |
|
| 150 |
Holds the block name, a synthetic sequential index and binding index, its std140 byte size and the |
| 151 |
metadata of its member uniforms (each with a std140 offset). The GS backend does not pad the size to |
| 152 |
any device alignment - the blocks are plain host memory the draw dispatch decodes - so |
| 153 |
@ref GetAlignAmount() is always 0. |
| 154 |
*/ |
| 155 |
class GsUniformBlock |
| 156 |
{ |
| 157 |
friend class GsShaderProgram; |
| 158 |
friend class GsUniformBlockCache; |
| 159 |
|
| 160 |
public: |
| 161 |
static constexpr std::uint32_t MaxNameLength = 48; |
| 162 |
|
| 163 |
enum class DiscoverUniforms |
| 164 |
{ |
| 165 |
Enabled, |
| 166 |
Disabled |
| 167 |
}; |
| 168 |
|
| 169 |
GsUniformBlock() |
| 170 |
: _index(0), _size(0), _alignAmount(0), _bindingIndex(-1) |
| 171 |
{ |
| 172 |
_name[0] = '\0'; |
| 173 |
} |
| 174 |
GsUniformBlock(std::uint32_t index, const char* name, std::int32_t dataSize) |
| 175 |
: _index(index), _size(dataSize), _alignAmount(0), _bindingIndex(-1) |
| 176 |
{ |
| 177 |
SetName(name); |
| 178 |
} |
| 179 |
|
| 180 |
inline std::uint32_t GetIndex() const { |
| 181 |
return _index; |
| 182 |
} |
| 183 |
inline std::int32_t GetBindingIndex() const { |
| 184 |
return _bindingIndex; |
| 185 |
} |
| 186 |
inline std::int32_t GetSize() const { |
| 187 |
return _size; |
| 188 |
} |
| 189 |
inline std::uint8_t GetAlignAmount() const { |
| 190 |
return _alignAmount; |
| 191 |
} |
| 192 |
inline const char* GetName() const { |
| 193 |
return _name; |
| 194 |
} |
| 195 |
|
| 196 |
/** @brief Returns the member uniform with the specified name, or `nullptr` if not found */ |
| 197 |
GsUniform* GetUniform(const char* name) { |
| 198 |
for (GsUniform& u : _members) { |
| 199 |
if (std::strcmp(u.GetName(), name) == 0) { |
| 200 |
return &u; |
| 201 |
} |
| 202 |
} |
| 203 |
return nullptr; |
| 204 |
} |
| 205 |
void SetBlockBinding(std::int32_t blockBinding) { |
| 206 |
_bindingIndex = blockBinding; |
| 207 |
} |
| 208 |
|
| 209 |
private: |
| 210 |
char _name[MaxNameLength]; |
| 211 |
std::uint32_t _index; |
| 212 |
std::int32_t _size; |
| 213 |
std::uint8_t _alignAmount; |
| 214 |
std::int32_t _bindingIndex; |
| 215 |
std::vector<GsUniform> _members; |
| 216 |
|
| 217 |
void SetName(const char* name) { |
| 218 |
std::size_t length = std::strlen(name); |
| 219 |
if (length >= MaxNameLength) { |
| 220 |
length = MaxNameLength - 1; |
| 221 |
} |
| 222 |
std::memcpy(_name, name, length); |
| 223 |
_name[length] = '\0'; |
| 224 |
} |
| 225 |
}; |
| 226 |
|
| 227 |
/** |
| 228 |
@brief Reflected metadata of a single active vertex attribute (aliased as `RHI::Attribute`) |
| 229 |
*/ |
| 230 |
class GsAttribute |
| 231 |
{ |
| 232 |
public: |
| 233 |
static constexpr std::uint32_t MaxNameLength = 32; |
| 234 |
|
| 235 |
GsAttribute() |
| 236 |
: _type(ShaderCompiler::UniformType::Float), _location(-1) |
| 237 |
{ |
| 238 |
_name[0] = '\0'; |
| 239 |
} |
| 240 |
GsAttribute(const char* name, ShaderCompiler::UniformType type, std::int32_t location) |
| 241 |
: _type(type), _location(location) |
| 242 |
{ |
| 243 |
std::size_t length = std::strlen(name); |
| 244 |
if (length >= MaxNameLength) { |
| 245 |
length = MaxNameLength - 1; |
| 246 |
} |
| 247 |
std::memcpy(_name, name, length); |
| 248 |
_name[length] = '\0'; |
| 249 |
} |
| 250 |
|
| 251 |
inline std::int32_t GetLocation() const { |
| 252 |
return _location; |
| 253 |
} |
| 254 |
inline ShaderCompiler::UniformType GetType() const { |
| 255 |
return _type; |
| 256 |
} |
| 257 |
inline const char* GetName() const { |
| 258 |
return _name; |
| 259 |
} |
| 260 |
inline std::int32_t GetComponentCount() const { |
| 261 |
return std::int32_t(UniformTypeInfo::ComponentCount(_type)); |
| 262 |
} |
| 263 |
|
| 264 |
private: |
| 265 |
char _name[MaxNameLength]; |
| 266 |
ShaderCompiler::UniformType _type; |
| 267 |
std::int32_t _location; |
| 268 |
}; |
| 269 |
} |