| 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::Software |
| 11 |
{ |
| 12 |
class SwShaderProgram; |
| 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 software backend needs about one uniform imported from the offline |
| 72 |
reflection: its name, scalar/vector/matrix type, array size, a synthetic sequential location and, |
| 73 |
for uniforms living inside a block, the owning block index and std140 byte offset. It carries no |
| 74 |
value storage — a @ref SwUniformCache holds the value. |
| 75 |
*/ |
| 76 |
class SwUniform |
| 77 |
{ |
| 78 |
friend class SwShaderProgram; |
| 79 |
friend class SwUniformBlock; |
| 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 |
SwUniform() |
| 86 |
: _type(ShaderCompiler::UniformType::Float), _size(0), _location(-1), _blockIndex(-1), _offset(0), _owner(nullptr) |
| 87 |
{ |
| 88 |
_name[0] = '\0'; |
| 89 |
} |
| 90 |
SwUniform(SwShaderProgram* 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 SwShaderProgram* 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 |
SwShaderProgram* _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 software backend does not pad the |
| 152 |
size to any device alignment, so @ref GetAlignAmount() is always 0. |
| 153 |
*/ |
| 154 |
class SwUniformBlock |
| 155 |
{ |
| 156 |
friend class SwShaderProgram; |
| 157 |
friend class SwUniformBlockCache; |
| 158 |
|
| 159 |
public: |
| 160 |
static constexpr std::uint32_t MaxNameLength = 48; |
| 161 |
|
| 162 |
enum class DiscoverUniforms |
| 163 |
{ |
| 164 |
Enabled, |
| 165 |
Disabled |
| 166 |
}; |
| 167 |
|
| 168 |
SwUniformBlock() |
| 169 |
: _index(0), _size(0), _alignAmount(0), _bindingIndex(-1) |
| 170 |
{ |
| 171 |
_name[0] = '\0'; |
| 172 |
} |
| 173 |
SwUniformBlock(std::uint32_t index, const char* name, std::int32_t dataSize) |
| 174 |
: _index(index), _size(dataSize), _alignAmount(0), _bindingIndex(-1) |
| 175 |
{ |
| 176 |
SetName(name); |
| 177 |
} |
| 178 |
|
| 179 |
inline std::uint32_t GetIndex() const { |
| 180 |
return _index; |
| 181 |
} |
| 182 |
inline std::int32_t GetBindingIndex() const { |
| 183 |
return _bindingIndex; |
| 184 |
} |
| 185 |
inline std::int32_t GetSize() const { |
| 186 |
return _size; |
| 187 |
} |
| 188 |
inline std::uint8_t GetAlignAmount() const { |
| 189 |
return _alignAmount; |
| 190 |
} |
| 191 |
inline const char* GetName() const { |
| 192 |
return _name; |
| 193 |
} |
| 194 |
|
| 195 |
/** @brief Returns the member uniform with the specified name, or `nullptr` if not found */ |
| 196 |
SwUniform* GetUniform(const char* name) { |
| 197 |
for (SwUniform& u : _members) { |
| 198 |
if (std::strcmp(u.GetName(), name) == 0) { |
| 199 |
return &u; |
| 200 |
} |
| 201 |
} |
| 202 |
return nullptr; |
| 203 |
} |
| 204 |
void SetBlockBinding(std::int32_t blockBinding) { |
| 205 |
_bindingIndex = blockBinding; |
| 206 |
} |
| 207 |
|
| 208 |
private: |
| 209 |
char _name[MaxNameLength]; |
| 210 |
std::uint32_t _index; |
| 211 |
std::int32_t _size; |
| 212 |
std::uint8_t _alignAmount; |
| 213 |
std::int32_t _bindingIndex; |
| 214 |
std::vector<SwUniform> _members; |
| 215 |
|
| 216 |
void SetName(const char* name) { |
| 217 |
std::size_t length = std::strlen(name); |
| 218 |
if (length >= MaxNameLength) { |
| 219 |
length = MaxNameLength - 1; |
| 220 |
} |
| 221 |
std::memcpy(_name, name, length); |
| 222 |
_name[length] = '\0'; |
| 223 |
} |
| 224 |
}; |
| 225 |
|
| 226 |
/** |
| 227 |
@brief Reflected metadata of a single active vertex attribute (aliased as `RHI::Attribute`) |
| 228 |
*/ |
| 229 |
class SwAttribute |
| 230 |
{ |
| 231 |
public: |
| 232 |
static constexpr std::uint32_t MaxNameLength = 32; |
| 233 |
|
| 234 |
SwAttribute() |
| 235 |
: _type(ShaderCompiler::UniformType::Float), _location(-1) |
| 236 |
{ |
| 237 |
_name[0] = '\0'; |
| 238 |
} |
| 239 |
SwAttribute(const char* name, ShaderCompiler::UniformType type, std::int32_t location) |
| 240 |
: _type(type), _location(location) |
| 241 |
{ |
| 242 |
std::size_t length = std::strlen(name); |
| 243 |
if (length >= MaxNameLength) { |
| 244 |
length = MaxNameLength - 1; |
| 245 |
} |
| 246 |
std::memcpy(_name, name, length); |
| 247 |
_name[length] = '\0'; |
| 248 |
} |
| 249 |
|
| 250 |
inline std::int32_t GetLocation() const { |
| 251 |
return _location; |
| 252 |
} |
| 253 |
inline ShaderCompiler::UniformType GetType() const { |
| 254 |
return _type; |
| 255 |
} |
| 256 |
inline const char* GetName() const { |
| 257 |
return _name; |
| 258 |
} |
| 259 |
inline std::int32_t GetComponentCount() const { |
| 260 |
return std::int32_t(UniformTypeInfo::ComponentCount(_type)); |
| 261 |
} |
| 262 |
|
| 263 |
private: |
| 264 |
char _name[MaxNameLength]; |
| 265 |
ShaderCompiler::UniformType _type; |
| 266 |
std::int32_t _location; |
| 267 |
}; |
| 268 |
} |