Ver Fonte

Fix an alignment bug

Panagiotis Christopoulos Charitos há 6 anos atrás
pai
commit
054ebe5aac

+ 1 - 1
src/anki/math/Mat.h

@@ -19,7 +19,7 @@ namespace anki
 /// @tparam J The number of rows.
 /// @tparam I The number of columns.
 template<typename T, U J, U I>
-class TMat
+class alignas(MathSimd<T, I>::ALIGNMENT) TMat
 {
 public:
 	using Scalar = T;

+ 4 - 1
src/anki/math/Simd.h

@@ -20,12 +20,13 @@
 namespace anki
 {
 
-/// Template class XXX
+/// Template class that holds SIMD info for the math classes.
 template<typename T, U N>
 class MathSimd
 {
 public:
 	using Type = T[N];
+	static constexpr U ALIGNMENT = alignof(T);
 };
 
 #if ANKI_SIMD == ANKI_SIMD_SSE
@@ -35,6 +36,7 @@ class MathSimd<F32, 4>
 {
 public:
 	using Type = __m128;
+	static constexpr U ALIGNMENT = 16;
 };
 #elif ANKI_SIMD == ANKI_SIMD_NEON
 // Specialize for F32
@@ -43,6 +45,7 @@ class MathSimd<F32, 4>
 {
 public:
 	using Type = float32x4_t;
+	static constexpr U ALIGNMENT = 16;
 };
 #endif
 

+ 3 - 1
src/anki/math/Vec.h

@@ -14,8 +14,10 @@ namespace anki
 /// @{
 
 /// Common code for all vectors
+/// @tparam T The scalar type. Eg float.
+/// @tparam N The number of the vector components (2, 3 or 4).
 template<typename T, U N>
-class TVec
+class alignas(MathSimd<T, N>::ALIGNMENT) TVec
 {
 public:
 	using Scalar = T;

+ 2 - 2
src/anki/scene/components/RenderComponent.cpp

@@ -51,7 +51,7 @@ void MaterialRenderComponent::allocateAndSetupUniforms(U set,
 	U8* uniforms;
 	void* uniformsBegin;
 	const void* uniformsEnd;
-	Array<U8, 256> pushConsts;
+	Array<Vec4, 256 / sizeof(Vec4)> pushConsts; // Use Vec4 so it will be aligned
 	if(!progVariant.usePushConstants())
 	{
 		StagingGpuMemoryToken token;
@@ -62,7 +62,7 @@ void MaterialRenderComponent::allocateAndSetupUniforms(U set,
 	}
 	else
 	{
-		uniforms = &pushConsts[0];
+		uniforms = reinterpret_cast<U8*>(&pushConsts[0]);
 	}
 
 	uniformsBegin = uniforms;