| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "VulkanShaderTypes.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::Vulkan |
| 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. For a uniform living inside a block, |
| 21 |
committing is a no-op — the whole block is published by @ref VulkanShaderUniformBlocks. |
| 22 |
*/ |
| 23 |
class VulkanUniformCache |
| 24 |
{ |
| 25 |
public: |
| 26 |
VulkanUniformCache() |
| 27 |
: _uniform(nullptr), _dataPointer(nullptr), _isDirty(false) {} |
| 28 |
explicit VulkanUniformCache(const VulkanUniform* uniform) |
| 29 |
: _uniform(uniform), _dataPointer(nullptr), _isDirty(false) {} |
| 30 |
|
| 31 |
inline const VulkanUniform* GetUniform() const { |
| 32 |
return _uniform; |
| 33 |
} |
| 34 |
inline std::uint8_t* GetDataPointer() const { |
| 35 |
return _dataPointer; |
| 36 |
} |
| 37 |
inline void SetDataPointer(std::uint8_t* dataPointer) { |
| 38 |
_dataPointer = dataPointer; |
| 39 |
} |
| 40 |
|
| 41 |
const float* GetFloatVector() const; |
| 42 |
float GetFloatValue(std::uint32_t index) const; |
| 43 |
const std::int32_t* GetIntVector() const; |
| 44 |
std::int32_t GetIntValue(std::uint32_t index) const; |
| 45 |
|
| 46 |
bool SetFloatVector(const float* vec); |
| 47 |
bool SetFloatValue(float v0); |
| 48 |
bool SetFloatValue(float v0, float v1); |
| 49 |
bool SetFloatValue(float v0, float v1, float v2); |
| 50 |
bool SetFloatValue(float v0, float v1, float v2, float v3); |
| 51 |
bool SetIntVector(const std::int32_t* vec); |
| 52 |
bool SetIntValue(std::int32_t v0); |
| 53 |
bool SetIntValue(std::int32_t v0, std::int32_t v1); |
| 54 |
bool SetIntValue(std::int32_t v0, std::int32_t v1, std::int32_t v2); |
| 55 |
bool SetIntValue(std::int32_t v0, std::int32_t v1, std::int32_t v2, std::int32_t v3); |
| 56 |
|
| 57 |
inline bool IsDirty() const { |
| 58 |
return _isDirty; |
| 59 |
} |
| 60 |
inline void SetDirty(bool isDirty) { |
| 61 |
_isDirty = isDirty; |
| 62 |
} |
| 63 |
/** @brief Publishes a dirty loose-uniform value to the owning program; a no-op for block members */ |
| 64 |
bool CommitValue(); |
| 65 |
|
| 66 |
private: |
| 67 |
const VulkanUniform* _uniform; |
| 68 |
std::uint8_t* _dataPointer; |
| 69 |
bool _isDirty; |
| 70 |
|
| 71 |
bool CheckFloat() const; |
| 72 |
bool CheckInt() const; |
| 73 |
bool CheckComponents(std::uint32_t requiredComponents) const; |
| 74 |
}; |
| 75 |
|
| 76 |
/** |
| 77 |
@brief Caches the contents of a uniform block (aliased as `RHI::UniformBlockCache`) |
| 78 |
|
| 79 |
Holds a pointer into a shared host data buffer mirroring the block's std140 layout and a @ref |
| 80 |
VulkanUniformCache for each member, each pointing at its member offset within that buffer. The block is |
| 81 |
uploaded as one contiguous range by @ref VulkanShaderUniformBlocks. |
| 82 |
*/ |
| 83 |
class VulkanUniformBlockCache |
| 84 |
{ |
| 85 |
public: |
| 86 |
VulkanUniformBlockCache() |
| 87 |
: _uniformBlock(nullptr), _dataPointer(nullptr), _usedSize(0) {} |
| 88 |
explicit VulkanUniformBlockCache(VulkanUniformBlock* uniformBlock); |
| 89 |
|
| 90 |
inline const VulkanUniformBlock* uniformBlock() const { |
| 91 |
return _uniformBlock; |
| 92 |
} |
| 93 |
std::uint32_t GetIndex() const; |
| 94 |
std::int32_t GetBindingIndex() const; |
| 95 |
std::int32_t GetSize() const; |
| 96 |
std::uint8_t GetAlignAmount() const; |
| 97 |
|
| 98 |
inline std::uint8_t* GetDataPointer() { |
| 99 |
return _dataPointer; |
| 100 |
} |
| 101 |
inline const std::uint8_t* GetDataPointer() const { |
| 102 |
return _dataPointer; |
| 103 |
} |
| 104 |
void SetDataPointer(std::uint8_t* dataPointer); |
| 105 |
|
| 106 |
inline std::int32_t usedSize() const { |
| 107 |
return _usedSize; |
| 108 |
} |
| 109 |
void SetUsedSize(std::int32_t usedSize); |
| 110 |
|
| 111 |
bool CopyData(std::uint32_t destIndex, const std::uint8_t* src, std::uint32_t numBytes); |
| 112 |
inline bool CopyData(const std::uint8_t* src) { |
| 113 |
return CopyData(0, src, std::uint32_t(_usedSize)); |
| 114 |
} |
| 115 |
|
| 116 |
/** @brief Returns the member uniform cache with the specified name, or `nullptr` if not found */ |
| 117 |
VulkanUniformCache* GetUniform(StringView name); |
| 118 |
void SetBlockBinding(std::int32_t blockBinding); |
| 119 |
|
| 120 |
private: |
| 121 |
VulkanUniformBlock* _uniformBlock; |
| 122 |
std::uint8_t* _dataPointer; |
| 123 |
std::int32_t _usedSize; |
| 124 |
|
| 125 |
struct NamedCache |
| 126 |
{ |
| 127 |
String Name; |
| 128 |
VulkanUniformCache Cache; |
| 129 |
}; |
| 130 |
std::vector<NamedCache> _uniformCaches; |
| 131 |
}; |
| 132 |
} |