| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "GsShaderTypes.h" |
| 4 |
|
| 5 |
#include <cstdint> |
| 6 |
#include <vector> |
| 7 |
|
| 8 |
#include <Containers/String.h> |
| 9 |
#include <Containers/StringView.h> |
| 10 |
|
| 11 |
using namespace Death::Containers; |
| 12 |
|
| 13 |
namespace nCine::RHI::GS |
| 14 |
{ |
| 15 |
/** |
| 16 |
@brief Caches the host-side value of a single uniform (aliased as `RHI::UniformCache`) |
| 17 |
|
| 18 |
Mirrors the OpenGL backend's uniform cache: it holds a pointer into a shared host data buffer and the |
| 19 |
`Set*` methods that write values into it, plus a dirty flag. For a loose uniform, @ref CommitValue() |
| 20 |
publishes the value pointer to the owning shader program so the generated fixed-function effects can |
| 21 |
read it at draw time (the fixed-function equivalent of a `glUniform*` upload). For a uniform living |
| 22 |
inside a block, committing is a no-op - the whole block is published by @ref GsShaderUniformBlocks. |
| 23 |
*/ |
| 24 |
class GsUniformCache |
| 25 |
{ |
| 26 |
public: |
| 27 |
GsUniformCache() |
| 28 |
: _uniform(nullptr), _dataPointer(nullptr), _isDirty(false) {} |
| 29 |
explicit GsUniformCache(const GsUniform* uniform) |
| 30 |
: _uniform(uniform), _dataPointer(nullptr), _isDirty(false) {} |
| 31 |
|
| 32 |
inline const GsUniform* GetUniform() const { |
| 33 |
return _uniform; |
| 34 |
} |
| 35 |
inline std::uint8_t* GetDataPointer() const { |
| 36 |
return _dataPointer; |
| 37 |
} |
| 38 |
inline void SetDataPointer(std::uint8_t* dataPointer) { |
| 39 |
_dataPointer = dataPointer; |
| 40 |
} |
| 41 |
|
| 42 |
const float* GetFloatVector() const; |
| 43 |
float GetFloatValue(std::uint32_t index) const; |
| 44 |
const std::int32_t* GetIntVector() const; |
| 45 |
std::int32_t GetIntValue(std::uint32_t index) const; |
| 46 |
|
| 47 |
bool SetFloatVector(const float* vec); |
| 48 |
bool SetFloatValue(float v0); |
| 49 |
bool SetFloatValue(float v0, float v1); |
| 50 |
bool SetFloatValue(float v0, float v1, float v2); |
| 51 |
bool SetFloatValue(float v0, float v1, float v2, float v3); |
| 52 |
bool SetIntVector(const std::int32_t* vec); |
| 53 |
bool SetIntValue(std::int32_t v0); |
| 54 |
bool SetIntValue(std::int32_t v0, std::int32_t v1); |
| 55 |
bool SetIntValue(std::int32_t v0, std::int32_t v1, std::int32_t v2); |
| 56 |
bool SetIntValue(std::int32_t v0, std::int32_t v1, std::int32_t v2, std::int32_t v3); |
| 57 |
|
| 58 |
inline bool IsDirty() const { |
| 59 |
return _isDirty; |
| 60 |
} |
| 61 |
inline void SetDirty(bool isDirty) { |
| 62 |
_isDirty = isDirty; |
| 63 |
} |
| 64 |
/** @brief Publishes a dirty loose-uniform value to the owning program; a no-op for block members */ |
| 65 |
bool CommitValue(); |
| 66 |
|
| 67 |
private: |
| 68 |
const GsUniform* _uniform; |
| 69 |
std::uint8_t* _dataPointer; |
| 70 |
bool _isDirty; |
| 71 |
|
| 72 |
bool CheckFloat() const; |
| 73 |
bool CheckInt() const; |
| 74 |
bool CheckComponents(std::uint32_t requiredComponents) const; |
| 75 |
}; |
| 76 |
|
| 77 |
/** |
| 78 |
@brief Caches the contents of a uniform block (aliased as `RHI::UniformBlockCache`) |
| 79 |
|
| 80 |
Holds a pointer into a shared host data buffer mirroring the block's std140 layout and a |
| 81 |
@ref GsUniformCache for each member, each pointing at its member offset within that buffer. The block |
| 82 |
is published as one contiguous range by @ref GsShaderUniformBlocks, which the draw dispatch decodes. |
| 83 |
*/ |
| 84 |
class GsUniformBlockCache |
| 85 |
{ |
| 86 |
public: |
| 87 |
GsUniformBlockCache() |
| 88 |
: _uniformBlock(nullptr), _dataPointer(nullptr), _usedSize(0) {} |
| 89 |
explicit GsUniformBlockCache(GsUniformBlock* uniformBlock); |
| 90 |
|
| 91 |
inline const GsUniformBlock* uniformBlock() const { |
| 92 |
return _uniformBlock; |
| 93 |
} |
| 94 |
std::uint32_t GetIndex() const; |
| 95 |
std::int32_t GetBindingIndex() const; |
| 96 |
std::int32_t GetSize() const; |
| 97 |
std::uint8_t GetAlignAmount() const; |
| 98 |
|
| 99 |
inline std::uint8_t* GetDataPointer() { |
| 100 |
return _dataPointer; |
| 101 |
} |
| 102 |
inline const std::uint8_t* GetDataPointer() const { |
| 103 |
return _dataPointer; |
| 104 |
} |
| 105 |
void SetDataPointer(std::uint8_t* dataPointer); |
| 106 |
|
| 107 |
inline std::int32_t usedSize() const { |
| 108 |
return _usedSize; |
| 109 |
} |
| 110 |
void SetUsedSize(std::int32_t usedSize); |
| 111 |
|
| 112 |
bool CopyData(std::uint32_t destIndex, const std::uint8_t* src, std::uint32_t numBytes); |
| 113 |
inline bool CopyData(const std::uint8_t* src) { |
| 114 |
return CopyData(0, src, std::uint32_t(_usedSize)); |
| 115 |
} |
| 116 |
|
| 117 |
/** @brief Returns the member uniform cache with the specified name, or `nullptr` if not found */ |
| 118 |
GsUniformCache* GetUniform(StringView name); |
| 119 |
void SetBlockBinding(std::int32_t blockBinding); |
| 120 |
|
| 121 |
private: |
| 122 |
GsUniformBlock* _uniformBlock; |
| 123 |
std::uint8_t* _dataPointer; |
| 124 |
std::int32_t _usedSize; |
| 125 |
|
| 126 |
struct NamedCache |
| 127 |
{ |
| 128 |
String Name; |
| 129 |
GsUniformCache Cache; |
| 130 |
}; |
| 131 |
std::vector<NamedCache> _uniformCaches; |
| 132 |
}; |
| 133 |
} |