|
7 | 7 |
|
8 | 8 | #include <Utils/Types/FastMemoryArray.h> |
9 | 9 |
|
| 10 | +#define SR_MAX_VERTEX_ATTRIBUTES 48 |
| 11 | + |
| 12 | +#ifdef SR_UTILS_ASSIMP |
| 13 | + struct aiMesh; |
| 14 | +#endif |
| 15 | + |
10 | 16 | namespace SR_UTILS_NS { |
| 17 | + SR_ENUM_NS_CLASS_T(VertexAttributeFormat, uint8_t, |
| 18 | + None, // no data |
| 19 | + |
| 20 | + // Float |
| 21 | + Float32, // 32-bit float |
| 22 | + Float16, // 16-bit half float |
| 23 | + |
| 24 | + // Integer |
| 25 | + UInt32, |
| 26 | + Int32, |
| 27 | + UInt16, |
| 28 | + Int16, |
| 29 | + UInt8, |
| 30 | + Int8, |
| 31 | + |
| 32 | + // Normalized integer |
| 33 | + UNorm8, // 0..1 |
| 34 | + UNorm16, // 0..1 |
| 35 | + SNorm8, // -1..1 |
| 36 | + SNorm16, // -1..1 |
| 37 | + |
| 38 | + // Packed |
| 39 | + R10G10B10A2_UNorm, // 10bit RGB + 2bit A |
| 40 | + R11G11B10_Float // HDR RGB 11+11+10 |
| 41 | + ) |
| 42 | + |
| 43 | + uint8_t GetVertexAttributeFormatSize(VertexAttributeFormat format); |
| 44 | + |
| 45 | + SR_ENUM_NS_CLASS_T(VertexAttribute, uint8_t, |
| 46 | + None = 0 , // none |
| 47 | + Position = 1 , // vec3 |
| 48 | + Normal = 2 , // vec3 |
| 49 | + Tangent = 3 , // vec4 (w = sign) |
| 50 | + UV0 = 4 , // vec2 |
| 51 | + UV1 = 5 , // vec2 |
| 52 | + UV2 = 6 , // vec2 |
| 53 | + UV3 = 7 , // vec2 |
| 54 | + UV4 = 8 , // vec2 |
| 55 | + UV5 = 9 , // vec2 |
| 56 | + UV6 = 10 , // vec2 |
| 57 | + UV7 = 11 , // vec2 |
| 58 | + BlendIndices = 12 , // vec4 (x,y,z,w = bone indices) |
| 59 | + BlendWeights = 13 , // vec4 (x,y,z,w = bone weights) |
| 60 | + BlendIndices2 = 14 , // vec4 (x,y,z,w = bone indices) extended |
| 61 | + BlendWeights2 = 15 , // vec4 (x,y,z,w = bone weights) extended |
| 62 | + Color0 = 16 , // vec4 |
| 63 | + Color1 = 17 , // vec4 |
| 64 | + Color2 = 18 , // vec4 |
| 65 | + Color3 = 19 , // vec4 |
| 66 | + Color4 = 20 , // vec4 |
| 67 | + Color5 = 21 , // vec4 |
| 68 | + Color6 = 22 , // vec4 |
| 69 | + Color7 = 23 , // vec4 |
| 70 | + MaterialID0 = 24 , // uint |
| 71 | + MaterialID1 = 25 , // uint |
| 72 | + MaterialID2 = 26 , // uint |
| 73 | + MaterialID3 = 27 , // uint |
| 74 | + MaterialID4 = 28 , // uint |
| 75 | + MaterialID5 = 29 , // uint |
| 76 | + MaterialID6 = 30 , // uint |
| 77 | + MaterialID7 = 31 , // uint |
| 78 | + BlendFactor = 32 , // float |
| 79 | + Custom0 = 33 , // vec4 |
| 80 | + Custom1 = 34 , // vec4 |
| 81 | + Custom2 = 35 , // vec4 |
| 82 | + Custom3 = 36 , // vec4 |
| 83 | + Custom4 = 37 , // vec4 |
| 84 | + Custom5 = 38 , // vec4 |
| 85 | + Custom6 = 39 , // vec4 |
| 86 | + Custom7 = 40 // vec4 |
| 87 | + ); |
| 88 | + |
| 89 | + VertexAttribute GetVertexAttributeByIndex(VertexAttribute attribute, uint8_t index); |
| 90 | + |
| 91 | + struct VertexAttributeDescription { |
| 92 | + VertexAttribute attribute = VertexAttribute::None; |
| 93 | + VertexAttributeFormat format = VertexAttributeFormat::None; |
| 94 | + uint8_t count = 0; |
| 95 | + uint16_t offset = 0; |
| 96 | + |
| 97 | + SR_NODISCARD uint8_t GetAttributeSizeInBytes() const; |
| 98 | + }; |
| 99 | + |
| 100 | + std::string_view VertexAttributeToName(VertexAttribute attribute); |
| 101 | + |
| 102 | + struct VertexLayoutDescription { |
| 103 | + VertexAttributeDescription attributes[SR_MAX_VERTEX_ATTRIBUTES] = {}; |
| 104 | + uint8_t attributesCount = 0; |
| 105 | + |
| 106 | + mutable uint64_t strideCache = 0; |
| 107 | + |
| 108 | + SR_NODISCARD bool Compare(const VertexLayoutDescription& other) const; |
| 109 | + |
| 110 | + SR_NODISCARD uint64_t GetHash() const; |
| 111 | + SR_NODISCARD uint64_t GetStride() const; |
| 112 | + SR_NODISCARD const VertexAttributeDescription* Find(VertexAttribute attribute) const; |
| 113 | + |
| 114 | + VertexLayoutDescription& AddAttribute(VertexAttribute attribute, VertexAttributeFormat format, uint8_t count); |
| 115 | + }; |
| 116 | + |
| 117 | + constexpr uint64_t VERTEX_LAYOUT_DESCRIPTION_SIZE = sizeof(VertexLayoutDescription); |
| 118 | + |
| 119 | + struct VertexDataBuffer : public NonCopyable { |
| 120 | + using Ptr = SR_HTYPES_NS::RawPointerHolder<VertexDataBuffer>; |
| 121 | + |
| 122 | + VertexDataBuffer() = default; |
| 123 | + VertexDataBuffer(VertexDataBuffer&& other) noexcept; |
| 124 | + VertexDataBuffer& operator=(VertexDataBuffer&& other) noexcept; |
| 125 | + |
| 126 | + SR_HTYPES_NS::FastMemoryArray<uint8_t> data; |
| 127 | + VertexLayoutDescription layout; |
| 128 | + uint64_t vertexCount = 0; |
| 129 | + |
| 130 | + void Allocate(uint64_t vertices); |
| 131 | + void SetVertex(uint64_t index, VertexAttribute attribute, const void* pSrc); |
| 132 | + void CopyFrom(const VertexDataBuffer& other); |
| 133 | + void SetLayout(const VertexLayoutDescription& newLayout) { layout = newLayout; } |
| 134 | + |
| 135 | + SR_NODISCARD const VertexLayoutDescription& GetLayout() const { return layout; } |
| 136 | + SR_NODISCARD void* GetVertex(uint64_t index, VertexAttribute attribute); |
| 137 | + SR_NODISCARD uint8_t* GetAttributeData(VertexAttribute attribute); |
| 138 | + SR_NODISCARD VertexDataBuffer TransitionToLayout(const VertexLayoutDescription& newLayout) const; |
| 139 | + SR_NODISCARD const void* GetRawData() const { return data.data(); } |
| 140 | + SR_NODISCARD uint64_t GetDataSize() const { return data.size(); } |
| 141 | + SR_NODISCARD uint64_t GetVertexCount() const { return vertexCount; } |
| 142 | + |
| 143 | +#ifdef SR_UTILS_ASSIMP |
| 144 | + SR_NODISCARD static VertexDataBuffer AllocateFromAssimp(aiMesh* pMesh, const SR_HTYPES_NS::FlatHashMap<SR_UTILS_NS::StringAtom, uint32_t>& bones); |
| 145 | +#endif |
| 146 | + |
| 147 | + }; |
| 148 | + |
11 | 149 | struct Vec2 { |
12 | 150 | float_t x, y; |
13 | 151 | }; |
|
0 commit comments