| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <cstddef> |
| 4 |
#include <cstdint> |
| 5 |
|
| 6 |
#include <Containers/SmallVector.h> |
| 7 |
#include <Containers/StringView.h> |
| 8 |
|
| 9 |
using namespace Death::Containers; |
| 10 |
|
| 11 |
namespace nCine::RHI::GXM |
| 12 |
{ |
| 13 |
class GxmBufferObject; |
| 14 |
|
| 15 |
/** |
| 16 |
@brief Vertex layout description of the sceGxm backend (aliased as `RHI::VertexFormat`) |
| 17 |
|
| 18 |
Records the set of vertex attributes (component count, type, stride, offset and source buffer) the |
| 19 |
way the OpenGL backend does, so the pipeline's attribute setup code compiles unchanged. The backend |
| 20 |
translates the recorded layout into the `SceGxmVertexAttribute` / `SceGxmVertexStream` arrays that a |
| 21 |
patched `SceGxmVertexProgram` is created from (see @ref GxmShaderProgram::GetVertexProgram()). |
| 22 |
*/ |
| 23 |
class GxmVertexFormat |
| 24 |
{ |
| 25 |
public: |
| 26 |
/** @brief The maximum number of vertex attributes */ |
| 27 |
static constexpr std::uint32_t MaxAttributes = 16; |
| 28 |
|
| 29 |
/** @brief A single vertex attribute within a vertex format */ |
| 30 |
class Attribute |
| 31 |
{ |
| 32 |
friend class GxmVertexFormat; |
| 33 |
|
| 34 |
public: |
| 35 |
Attribute() |
| 36 |
: _vbo(nullptr), _pointer(nullptr), _index(0), _size(0), _type(0), _stride(0), _baseOffset(0), _enabled(false), _normalized(false) {} |
| 37 |
|
| 38 |
void Init(std::uint32_t index, std::int32_t size, std::uint32_t type) { |
| 39 |
_index = index; |
| 40 |
_size = size; |
| 41 |
_type = type; |
| 42 |
_enabled = true; |
| 43 |
} |
| 44 |
|
| 45 |
// Mirrors the OpenGL backend's `Attribute::operator==` (only enabled attributes are compared in |
| 46 |
// depth); the source buffer is compared by pointer identity, which is equivalent for this backend |
| 47 |
// and avoids needing the complete `GxmBufferObject` type in this header |
| 48 |
bool operator==(const Attribute& other) const { |
| 49 |
return ((other._enabled == false && _enabled == false) || |
| 50 |
((other._enabled == true && _enabled == true) && |
| 51 |
other._vbo == _vbo && |
| 52 |
other._index == _index && |
| 53 |
other._size == _size && |
| 54 |
other._type == _type && |
| 55 |
other._normalized == _normalized && |
| 56 |
other._stride == _stride && |
| 57 |
other._pointer == _pointer && |
| 58 |
other._baseOffset == _baseOffset)); |
| 59 |
} |
| 60 |
bool operator!=(const Attribute& other) const { |
| 61 |
return !operator==(other); |
| 62 |
} |
| 63 |
|
| 64 |
inline bool IsEnabled() const { |
| 65 |
return _enabled; |
| 66 |
} |
| 67 |
inline const GxmBufferObject* GetVbo() const { |
| 68 |
return _vbo; |
| 69 |
} |
| 70 |
inline std::uint32_t GetIndex() const { |
| 71 |
return _index; |
| 72 |
} |
| 73 |
inline std::int32_t GetSize() const { |
| 74 |
return _size; |
| 75 |
} |
| 76 |
inline std::uint32_t GetType() const { |
| 77 |
return _type; |
| 78 |
} |
| 79 |
inline bool IsNormalized() const { |
| 80 |
return _normalized; |
| 81 |
} |
| 82 |
inline std::int32_t GetStride() const { |
| 83 |
return _stride; |
| 84 |
} |
| 85 |
inline const void* GetPointer() const { |
| 86 |
return _pointer; |
| 87 |
} |
| 88 |
inline std::uint32_t GetBaseOffset() const { |
| 89 |
return _baseOffset; |
| 90 |
} |
| 91 |
|
| 92 |
void SetVboParameters(std::int32_t stride, const void* pointer) { |
| 93 |
_stride = stride; |
| 94 |
_pointer = pointer; |
| 95 |
} |
| 96 |
inline void setVbo(const GxmBufferObject* vbo) { |
| 97 |
_vbo = vbo; |
| 98 |
} |
| 99 |
inline void SetBaseOffset(std::uint32_t baseOffset) { |
| 100 |
_baseOffset = baseOffset; |
| 101 |
} |
| 102 |
inline void SetSize(std::int32_t size) { |
| 103 |
_size = size; |
| 104 |
} |
| 105 |
inline void SetType(std::uint32_t type) { |
| 106 |
_type = type; |
| 107 |
} |
| 108 |
inline void SetNormalized(bool normalized) { |
| 109 |
_normalized = normalized; |
| 110 |
} |
| 111 |
|
| 112 |
private: |
| 113 |
const GxmBufferObject* _vbo; |
| 114 |
const void* _pointer; |
| 115 |
std::uint32_t _index; |
| 116 |
std::int32_t _size; |
| 117 |
std::uint32_t _type; |
| 118 |
std::int32_t _stride; |
| 119 |
std::uint32_t _baseOffset; |
| 120 |
bool _enabled; |
| 121 |
bool _normalized; |
| 122 |
}; |
| 123 |
|
| 124 |
GxmVertexFormat() |
| 125 |
: _ibo(nullptr) {} |
| 126 |
|
| 127 |
inline std::uint32_t GetAttributeCount() const { |
| 128 |
return std::uint32_t(_attributes.size()); |
| 129 |
} |
| 130 |
|
| 131 |
inline const GxmBufferObject* GetIbo() const { |
| 132 |
return _ibo; |
| 133 |
} |
| 134 |
inline void SetIbo(const GxmBufferObject* ibo) { |
| 135 |
_ibo = ibo; |
| 136 |
} |
| 137 |
/** @brief Applies the vertex format (no-op; the recorded layout is consumed at input-layout creation) */ |
| 138 |
void Define() {} |
| 139 |
/** @brief Disables all attributes and clears the index buffer */ |
| 140 |
void Reset() { |
| 141 |
_attributes.clear(); |
| 142 |
_ibo = nullptr; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
@brief Returns a value identifying this layout, used as the key of the patched-vertex-program cache |
| 147 |
|
| 148 |
A patched `SceGxmVertexProgram` bakes the layout in, so it may only be reused for an identical one. |
| 149 |
The digest covers everything that ends up in the `SceGxmVertexAttribute` / `SceGxmVertexStream` |
| 150 |
arrays - which attribute slots are enabled, and each one's component count, component type, |
| 151 |
normalization, within-vertex offset and stream stride. The source buffer and its base offset are |
| 152 |
deliberately left out: they are passed to the draw call as an address, not compiled into the |
| 153 |
program, so two draws out of different ring-buffer regions share one patched program. |
| 154 |
*/ |
| 155 |
std::uint64_t CalculateFingerprint() const { |
| 156 |
std::uint64_t hash = 0xcbf29ce484222325ull; // FNV-1a, 64-bit |
| 157 |
const auto mix = [&hash](std::uint64_t value) { |
| 158 |
hash = (hash ^ value) * 0x100000001b3ull; |
| 159 |
}; |
| 160 |
mix(_attributes.size()); |
| 161 |
for (std::uint32_t i = 0; i < _attributes.size(); i++) { |
| 162 |
const Attribute& attribute = _attributes[i]; |
| 163 |
if (!attribute.IsEnabled()) { |
| 164 |
mix(0); |
| 165 |
continue; |
| 166 |
} |
| 167 |
mix(std::uint64_t(attribute.GetIndex()) | (std::uint64_t(std::uint32_t(attribute.GetSize())) << 8) | |
| 168 |
(std::uint64_t(attribute.GetType()) << 16) | (std::uint64_t(attribute.IsNormalized() ? 1 : 0) << 48)); |
| 169 |
mix(std::uint64_t(std::uint32_t(attribute.GetStride())) | |
| 170 |
(std::uint64_t(reinterpret_cast<std::uintptr_t>(attribute.GetPointer())) << 32)); |
| 171 |
} |
| 172 |
return hash; |
| 173 |
} |
| 174 |
|
| 175 |
inline Attribute& operator[](std::uint32_t index) { |
| 176 |
if (index >= _attributes.size()) { |
| 177 |
_attributes.resize(index + 1); |
| 178 |
} |
| 179 |
return _attributes[index]; |
| 180 |
} |
| 181 |
inline const Attribute& operator[](std::uint32_t index) const { |
| 182 |
return _attributes[index]; |
| 183 |
} |
| 184 |
|
| 185 |
// Mirrors the OpenGL backend's `GLVertexFormat::operator==`: equal index buffers and attribute sets |
| 186 |
bool operator==(const GxmVertexFormat& other) const { |
| 187 |
if (other._ibo != _ibo || other._attributes.size() != _attributes.size()) { |
| 188 |
return false; |
| 189 |
} |
| 190 |
for (std::uint32_t i = 0; i < _attributes.size(); i++) { |
| 191 |
if (other._attributes[i] != _attributes[i]) { |
| 192 |
return false; |
| 193 |
} |
| 194 |
} |
| 195 |
return true; |
| 196 |
} |
| 197 |
bool operator!=(const GxmVertexFormat& other) const { |
| 198 |
return !operator==(other); |
| 199 |
} |
| 200 |
|
| 201 |
private: |
| 202 |
SmallVector<Attribute, MaxAttributes> _attributes; |
| 203 |
const GxmBufferObject* _ibo; |
| 204 |
}; |
| 205 |
|
| 206 |
/** |
| 207 |
@brief Vertex array object stub of the sceGxm backend (aliased as `RHI::VertexArray`) |
| 208 |
|
| 209 |
sceGxm has no vertex-array object; the vertex layout is baked into the patched vertex program and the |
| 210 |
stream base addresses are set per draw. This only satisfies the contract alias with inert bind/label |
| 211 |
operations. |
| 212 |
*/ |
| 213 |
class GxmVertexArray |
| 214 |
{ |
| 215 |
public: |
| 216 |
GxmVertexArray() = default; |
| 217 |
~GxmVertexArray() = default; |
| 218 |
|
| 219 |
GxmVertexArray(const GxmVertexArray&) = delete; |
| 220 |
GxmVertexArray& operator=(const GxmVertexArray&) = delete; |
| 221 |
|
| 222 |
inline std::uint32_t GetGLHandle() const { |
| 223 |
return 0; |
| 224 |
} |
| 225 |
bool Bind() const { |
| 226 |
return true; |
| 227 |
} |
| 228 |
static bool Unbind() { |
| 229 |
return true; |
| 230 |
} |
| 231 |
void SetObjectLabel(StringView label) { |
| 232 |
static_cast<void>(label); |
| 233 |
} |
| 234 |
}; |
| 235 |
} |