Skip to content

Commit a9fc055

Browse files
committed
Add ShaderDataTypeTraits
1 parent 5d7d8b6 commit a9fc055

File tree

6 files changed

+67
-39
lines changed

6 files changed

+67
-39
lines changed

Mahakam/src/Mahakam/Renderer/GL.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ namespace Mahakam
267267

268268
// Interleave vertices
269269
MeshData meshData(vertexCount, indices, indexCount);
270-
meshData.SetVertices(VertexType::Position, ShaderDataType::Float3, positions);
271-
meshData.SetVertices(VertexType::TexCoords, ShaderDataType::Float2, uvs);
270+
meshData.SetVertices(VertexType::Position, positions);
271+
meshData.SetVertices(VertexType::TexCoords, uvs);
272272

273273
return SubMesh::Create(std::move(meshData));
274274
}
@@ -315,7 +315,7 @@ namespace Mahakam
315315

316316
// Interleave vertices
317317
MeshData meshData(vertexCount, indices, indexCount);
318-
meshData.SetVertices(VertexType::Position, ShaderDataType::Float3, positions);
318+
meshData.SetVertices(VertexType::Position, positions);
319319

320320
return SubMesh::Create(std::move(meshData));
321321
}

Mahakam/src/Mahakam/Renderer/Mesh.cpp

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -399,24 +399,22 @@ namespace Mahakam
399399
MeshData meshData(vertexCount, std::move(indices));
400400

401401
if (positionOffset)
402-
meshData.SetVertices(VertexType::Position, ShaderDataType::Float3, positions.data());
402+
meshData.SetVertices(VertexType::Position, positions.data());
403403
if (texcoordOffset)
404-
meshData.SetVertices(VertexType::TexCoords, ShaderDataType::Float2, texcoords.data());
404+
meshData.SetVertices(VertexType::TexCoords, texcoords.data());
405405
if (normalOffset)
406-
meshData.SetVertices(VertexType::Normals, ShaderDataType::Float3, normals.data());
406+
meshData.SetVertices(VertexType::Normals, normals.data());
407407
if (tangentOffset)
408-
meshData.SetVertices(VertexType::Tangents, ShaderDataType::Float4, tangents.data());
408+
meshData.SetVertices(VertexType::Tangents, tangents.data());
409409
if (colorOffset)
410-
meshData.SetVertices(VertexType::Colors, ShaderDataType::Float4, colors.data());
410+
meshData.SetVertices(VertexType::Colors, colors.data());
411411
if (boneIDOffset && boneWeightOffset)
412412
{
413-
meshData.SetVertices(VertexType::BoneIDs, ShaderDataType::Int4, boneIDs.data());
414-
meshData.SetVertices(VertexType::BoneWeights, ShaderDataType::Float4, boneWeights.data());
413+
meshData.SetVertices(VertexType::BoneIDs, boneIDs.data());
414+
meshData.SetVertices(VertexType::BoneWeights, boneWeights.data());
415415
}
416416

417-
Ref<SubMesh> mesh = SubMesh::Create(std::move(meshData));
418-
419-
skinnedMesh->Meshes.push_back(mesh);
417+
skinnedMesh->Meshes.push_back(SubMesh::Create(std::move(meshData)));
420418
}
421419

422420
// Extract nodes and bones
@@ -589,9 +587,9 @@ namespace Mahakam
589587

590588
// Interleave vertices
591589
MeshData meshData(vertexCount, std::move(indices));
592-
meshData.SetVertices(VertexType::Position, ShaderDataType::Float3, positions.data());
593-
meshData.SetVertices(VertexType::TexCoords, ShaderDataType::Float2, uvs.data());
594-
meshData.SetVertices(VertexType::Normals, ShaderDataType::Float3, normals.data());
590+
meshData.SetVertices(VertexType::Position, positions.data());
591+
meshData.SetVertices(VertexType::TexCoords, uvs.data());
592+
meshData.SetVertices(VertexType::Normals, normals.data());
595593
//meshData.SetVertices(VertexType::Tangents, ShaderDataType::Float4, tangents.data());
596594

597595
Ref<SubMesh> mesh = SubMesh::Create(std::move(meshData));
@@ -651,10 +649,10 @@ namespace Mahakam
651649

652650
// Interleave vertices
653651
MeshData meshData(vertexCount, std::move(indices));
654-
meshData.SetVertices(VertexType::Position, ShaderDataType::Float3, positions.data());
655-
meshData.SetVertices(VertexType::TexCoords, ShaderDataType::Float2, uvs.data());
656-
meshData.SetVertices(VertexType::Normals, ShaderDataType::Float3, normals.data());
657-
meshData.SetVertices(VertexType::Tangents, ShaderDataType::Float4, tangents.data());
652+
meshData.SetVertices(VertexType::Position, positions.data());
653+
meshData.SetVertices(VertexType::TexCoords, uvs.data());
654+
meshData.SetVertices(VertexType::Normals, normals.data());
655+
meshData.SetVertices(VertexType::Tangents, tangents.data());
658656

659657
Ref<SubMesh> mesh = SubMesh::Create(std::move(meshData));
660658

@@ -714,10 +712,10 @@ namespace Mahakam
714712

715713
// Interleave vertices
716714
MeshData meshData(vertexCount, std::move(indices));
717-
meshData.SetVertices(VertexType::Position, ShaderDataType::Float3, positions.data());
718-
meshData.SetVertices(VertexType::TexCoords, ShaderDataType::Float2, uvs.data());
719-
meshData.SetVertices(VertexType::Normals, ShaderDataType::Float3, normals.data());
720-
meshData.SetVertices(VertexType::Tangents, ShaderDataType::Float4, tangents.data());
715+
meshData.SetVertices(VertexType::Position, positions.data());
716+
meshData.SetVertices(VertexType::TexCoords, uvs.data());
717+
meshData.SetVertices(VertexType::Normals, normals.data());
718+
meshData.SetVertices(VertexType::Tangents, tangents.data());
721719

722720
Ref<SubMesh> mesh = SubMesh::Create(std::move(meshData));
723721

@@ -813,10 +811,10 @@ namespace Mahakam
813811

814812
// Interleave vertices
815813
MeshData meshData(vertexCount, std::move(indices));
816-
meshData.SetVertices(VertexType::Position, ShaderDataType::Float3, positions.data());
817-
meshData.SetVertices(VertexType::TexCoords, ShaderDataType::Float2, uvs.data());
818-
meshData.SetVertices(VertexType::Normals, ShaderDataType::Float3, normals.data());
819-
meshData.SetVertices(VertexType::Tangents, ShaderDataType::Float4, tangents.data());
814+
meshData.SetVertices(VertexType::Position, positions.data());
815+
meshData.SetVertices(VertexType::TexCoords, uvs.data());
816+
meshData.SetVertices(VertexType::Normals, normals.data());
817+
meshData.SetVertices(VertexType::Tangents, tangents.data());
820818

821819
Ref<SubMesh> mesh = SubMesh::Create(std::move(meshData));
822820

Mahakam/src/Mahakam/Renderer/Mesh.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ namespace Mahakam
137137
}
138138

139139
template<typename U, typename T>
140-
void SetVertices(U index, ShaderDataType dataType, const T* data)
140+
void SetVertices(U index, const T* data)
141141
{
142-
SetVertices(Input(index), dataType, data, sizeof(T));
142+
SetVertices(Input(index), ShaderDataTypeTraits<T>::value, data, sizeof(T));
143143
}
144144

145145
template<typename T>
@@ -210,13 +210,7 @@ namespace Mahakam
210210

211211
bool HasVertices(int index) { return GetVertices(index) == nullptr; }
212212

213-
const glm::vec3* GetPositions() const { return reinterpret_cast<const glm::vec3*>(GetVertices(0)); }
214-
const glm::vec2* GetTexcoords() const { return reinterpret_cast<const glm::vec2*>(GetVertices(1)); }
215-
const glm::vec3* GetNormals() const { return reinterpret_cast<const glm::vec3*>(GetVertices(2)); }
216-
const glm::vec4* GetTangents() const { return reinterpret_cast<const glm::vec4*>(GetVertices(3)); }
217-
const glm::vec4* GetColors() const { return reinterpret_cast<const glm::vec4*>(GetVertices(4)); }
218-
const glm::ivec4* GetBoneIDs() const { return reinterpret_cast<const glm::ivec4*>(GetVertices(5)); }
219-
const glm::vec4* GetBoneWeights() const { return reinterpret_cast<const glm::vec4*>(GetVertices(6)); }
213+
const glm::vec3* GetPositions() const { return reinterpret_cast<const glm::vec3*>(GetVertices(0)); }
220214

221215
inline static Ref<SubMesh> Create(MeshData&& mesh) { return CreateImpl(std::move(mesh)); }
222216

Mahakam/src/Mahakam/Renderer/ShaderDataTypes.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,42 @@ namespace Mahakam
3434
SamplerCube
3535
};
3636

37+
template<typename T, typename = void>
38+
struct ShaderDataTypeTraits {};
39+
40+
template<>
41+
struct ShaderDataTypeTraits<float> : std::integral_constant<ShaderDataType, ShaderDataType::Float> {};
42+
43+
template<>
44+
struct ShaderDataTypeTraits<glm::vec2> : std::integral_constant<ShaderDataType, ShaderDataType::Float2> {};
45+
46+
template<>
47+
struct ShaderDataTypeTraits<glm::vec3> : std::integral_constant<ShaderDataType, ShaderDataType::Float3> {};
48+
49+
template<>
50+
struct ShaderDataTypeTraits<glm::vec4> : std::integral_constant<ShaderDataType, ShaderDataType::Float4> {};
51+
52+
template<>
53+
struct ShaderDataTypeTraits<glm::mat3> : std::integral_constant<ShaderDataType, ShaderDataType::Mat3> {};
54+
55+
template<>
56+
struct ShaderDataTypeTraits<glm::mat4> : std::integral_constant<ShaderDataType, ShaderDataType::Mat4> {};
57+
58+
template<>
59+
struct ShaderDataTypeTraits<int> : std::integral_constant<ShaderDataType, ShaderDataType::Int> {};
60+
61+
template<>
62+
struct ShaderDataTypeTraits<glm::ivec2> : std::integral_constant<ShaderDataType, ShaderDataType::Int2> {};
63+
64+
template<>
65+
struct ShaderDataTypeTraits<glm::ivec3> : std::integral_constant<ShaderDataType, ShaderDataType::Int3> {};
66+
67+
template<>
68+
struct ShaderDataTypeTraits<glm::ivec4> : std::integral_constant<ShaderDataType, ShaderDataType::Int4> {};
69+
70+
template<>
71+
struct ShaderDataTypeTraits<bool> : std::integral_constant<ShaderDataType, ShaderDataType::Bool> {};
72+
3773
struct ShaderSource
3874
{
3975
UnorderedMap<ShaderStage, std::string> Sources;

Sandbox/profiling/Shutdown.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

Sandbox/profiling/Startup.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)