Skip to content

Commit 02b5500

Browse files
committed
(Vertices) Added vertex layout system for custom vertex attributes.
1 parent b5059a6 commit 02b5500

8 files changed

Lines changed: 747 additions & 24 deletions

File tree

inc/Utils/Common/Vertices.h

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,145 @@
77

88
#include <Utils/Types/FastMemoryArray.h>
99

10+
#define SR_MAX_VERTEX_ATTRIBUTES 48
11+
12+
#ifdef SR_UTILS_ASSIMP
13+
struct aiMesh;
14+
#endif
15+
1016
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+
11149
struct Vec2 {
12150
float_t x, y;
13151
};

inc/Utils/ECS/Component.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ namespace SR_UTILS_NS {
152152
SR_WORLD_NS::Scene* m_scene = nullptr;
153153

154154
};
155-
}
156155

156+
constexpr static size_t SIZE_OF_COMPONENT_CLASS = sizeof(Component);
157+
}
157158

158159
#endif //SR_ENGINE_UTILS_COMPONENT_H

inc/Utils/Types/IRawMeshHolder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ namespace SR_HTYPES_NS {
3333
SR_NODISCARD SR_UTILS_NS::Path GetMeshPath() const noexcept;
3434
SR_NODISCARD std::string GetMeshStringPath() const noexcept;
3535
SR_NODISCARD bool IsValidMeshId() const noexcept;
36-
SR_NODISCARD std::vector<SR_UTILS_NS::Vertex> GetVertices() const noexcept;
3736
SR_NODISCARD std::string_view GetGeometryName() const noexcept;
37+
//SR_NODISCARD std::vector<SR_UTILS_NS::Vertex> GetVertices() const noexcept;
38+
SR_NODISCARD const SR_UTILS_NS::VertexDataBuffer& GetVertexBuffer(const SR_UTILS_NS::VertexLayoutDescription& layout) const;
3839

3940
virtual void OnRawMeshChanged() { }
4041

inc/Utils/Types/RawMesh.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ namespace SR_HTYPES_NS {
5959
SR_NODISCARD uint32_t GetMeshesCount() const;
6060
SR_NODISCARD std::string_view GetGeometryName(uint32_t id) const;
6161

62-
SR_NODISCARD std::vector<SR_UTILS_NS::Vertex> GetVertices(uint32_t id) const;
62+
SR_NODISCARD const SR_UTILS_NS::VertexDataBuffer& GetVertexBuffer(uint32_t id, const SR_UTILS_NS::VertexLayoutDescription& layout) const;
63+
//SR_NODISCARD std::vector<SR_UTILS_NS::Vertex> GetVertices(uint32_t id) const;
6364
SR_NODISCARD const SR_HTYPES_NS::FastMemoryArray<uint32_t>& GetIndices(uint32_t id) const;
6465
SR_NODISCARD const SR_HTYPES_NS::FlatHashMap<SR_UTILS_NS::StringAtom, uint32_t>& GetBones(uint32_t id) const;
6566
SR_NODISCARD const SR_HTYPES_NS::FlatHashMap<SR_UTILS_NS::StringAtom, uint16_t>& GetOptimizedBones() const;
@@ -102,6 +103,8 @@ namespace SR_HTYPES_NS {
102103
#endif
103104

104105
private:
106+
mutable std::vector<std::vector<SR_UTILS_NS::VertexDataBuffer>> m_vertexBuffersCache;
107+
105108
std::vector<SR_HTYPES_NS::FlatHashMap<SR_UTILS_NS::StringAtom, uint32_t>> m_bones;
106109
SR_HTYPES_NS::FlatHashMap<SR_UTILS_NS::StringAtom, uint16_t> m_optimizedBones;
107110

inc/Utils/Types/SortedVector.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ namespace SR_HTYPES_NS {
1818
SortedVector(SortedVector&& other) noexcept
1919
: m_data(SR_UTILS_NS::Exchange(other.m_data, { }))
2020
{ }
21+
SortedVector(const SortedVector& other)
22+
: m_data(other.m_data)
23+
{ }
2124

2225
SortedVector& operator=(SortedVector&& other) noexcept {
2326
if (this != &other) {
@@ -26,6 +29,13 @@ namespace SR_HTYPES_NS {
2629
return *this;
2730
}
2831

32+
SortedVector& operator=(const SortedVector& other) {
33+
if (this != &other) {
34+
m_data = other.m_data;
35+
}
36+
return *this;
37+
}
38+
2939
SR_NODISCARD SR_CONSTEXPR bool Empty() const { return m_data.empty(); }
3040
SR_NODISCARD SR_CONSTEXPR bool empty() const { return m_data.empty(); }
3141

@@ -101,6 +111,31 @@ namespace SR_HTYPES_NS {
101111
SR_NODISCARD SR_CONSTEXPR Iterator end() { return m_data.end(); }
102112
SR_NODISCARD SR_CONSTEXPR ConstIterator end() const { return m_data.end(); }
103113

114+
SR_NODISCARD bool Contains(const T& value) const {
115+
if (m_data.empty()) {
116+
return false;
117+
}
118+
119+
auto it = std::lower_bound(m_data.begin(), m_data.end(), value, m_predicate);
120+
return it != m_data.end() && *it == value;
121+
}
122+
123+
SR_NODISCARD const T* Find(const T& value) const {
124+
if (m_data.empty()) {
125+
return nullptr;
126+
}
127+
128+
auto it = std::lower_bound(m_data.begin(), m_data.end(), value, m_predicate);
129+
if (it != m_data.end() && *it == value) {
130+
return &(*it);
131+
}
132+
return nullptr;
133+
}
134+
135+
SR_NODISCARD T* Find(const T& value) {
136+
return const_cast<T*>(static_cast<const SortedVector&>(*this).Find(value));
137+
}
138+
104139
void Add(const T& value) {
105140
SR_TRACY_ZONE;
106141
if (m_data.empty()) {
@@ -148,6 +183,10 @@ namespace SR_HTYPES_NS {
148183
m_data.reserve(size);
149184
}
150185

186+
void reserve(uint64_t size) {
187+
m_data.reserve(size);
188+
}
189+
151190
void Clear() {
152191
SR_TRACY_ZONE;
153192
m_data.clear();

0 commit comments

Comments
 (0)