[Bf-blender-cvs] [8c0864b5ede] blender2.8: GPU matrix: add back type checks

Campbell Barton noreply at git.blender.org
Sat Apr 15 09:45:46 CEST 2017


Commit: 8c0864b5edea83b35c2ebd78191e44ac25928e3b
Author: Campbell Barton
Date:   Sat Apr 15 17:43:48 2017 +1000
Branches: blender2.8
https://developer.blender.org/rB8c0864b5edea83b35c2ebd78191e44ac25928e3b

GPU matrix: add back type checks

Without this gpuGet functions would cast everything
(no type or size checks and override const variables).

===================================================================

M	source/blender/editors/space_view3d/drawobject.c
M	source/blender/gpu/GPU_matrix.h
M	source/blender/gpu/intern/gpu_matrix.c

===================================================================

diff --git a/source/blender/editors/space_view3d/drawobject.c b/source/blender/editors/space_view3d/drawobject.c
index 91ee3ccd4ca..e163f88d356 100644
--- a/source/blender/editors/space_view3d/drawobject.c
+++ b/source/blender/editors/space_view3d/drawobject.c
@@ -4233,7 +4233,7 @@ static void draw_mesh_object_outline_new(View3D *v3d, RegionView3D *rv3d, Object
 			Batch_set_builtin_program(fancy_edges, GPU_SHADER_EDGES_FRONT_BACK_ORTHO);
 			/* set eye vector, transformed to object coords */
 			float eye[3] = { 0.0f, 0.0f, 1.0f }; /* looking into the screen */
-			mul_m3_v3((float (*)[3])gpuGetNormalMatrixInverse(NULL), eye);
+			mul_m3_v3(gpuGetNormalMatrixInverse(NULL), eye);
 			Batch_Uniform3fv(fancy_edges, "eye", eye);
 		}
 		else {
@@ -4757,7 +4757,7 @@ static void draw_mesh_fancy_new(Scene *scene, SceneLayer *sl, ARegion *ar, View3
 			Batch_set_builtin_program(fancy_edges, GPU_SHADER_EDGES_FRONT_BACK_ORTHO);
 			/* set eye vector, transformed to object coords */
 			float eye[3] = { 0.0f, 0.0f, 1.0f }; /* looking into the screen */
-			mul_m3_v3((float (*)[3])gpuGetNormalMatrixInverse(NULL), eye);
+			mul_m3_v3(gpuGetNormalMatrixInverse(NULL), eye);
 			Batch_Uniform3fv(fancy_edges, "eye", eye);
 		}
 		else {
diff --git a/source/blender/gpu/GPU_matrix.h b/source/blender/gpu/GPU_matrix.h
index 035b6234397..faa7d0bda64 100644
--- a/source/blender/gpu/GPU_matrix.h
+++ b/source/blender/gpu/GPU_matrix.h
@@ -107,12 +107,12 @@ void gpuOrtho2D(float left, float right, float bottom, float top);
 
 
 /* functions to get matrix values */
-const float *gpuGetModelViewMatrix(float m[4][4]);
-const float *gpuGetProjectionMatrix(float m[4][4]);
-const float *gpuGetModelViewProjectionMatrix(float m[4][4]);
+const float (*gpuGetModelViewMatrix(float m[4][4]))[4];
+const float (*gpuGetProjectionMatrix(float m[4][4]))[4];
+const float (*gpuGetModelViewProjectionMatrix(float m[4][4]))[4];
 
-const float *gpuGetNormalMatrix(float m[3][3]);
-const float *gpuGetNormalMatrixInverse(float m[3][3]);
+const float (*gpuGetNormalMatrix(float m[3][3]))[3];
+const float (*gpuGetNormalMatrixInverse(float m[3][3]))[3];
 
 
 /* set uniform values for currently bound shader */
@@ -123,16 +123,62 @@ bool gpuMatricesDirty(void); /* since last bind */
 }
 #endif
 
-
 #ifndef SUPPRESS_GENERIC_MATRIX_API
+
+#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
+#define _GPU_MAT3_CONST_CAST(x) (_Generic((x), \
+	void *:       (const float (*)[3])(x), \
+	float *:      (const float (*)[3])(x), \
+	float [9]:    (const float (*)[3])(x), \
+	float (*)[4]: (const float (*)[3])(x), \
+	float [4][4]: (const float (*)[3])(x), \
+	const void *:       (const float (*)[3])(x), \
+	const float *:      (const float (*)[3])(x), \
+	const float [9]:    (const float (*)[3])(x), \
+	const float (*)[3]: (const float (*)[3])(x), \
+	const float [3][3]: (const float (*)[3])(x)) \
+)
+#define _GPU_MAT3_CAST(x) (_Generic((x), \
+	void *:       (float (*)[3])(x), \
+	float *:      (float (*)[3])(x), \
+	float [9]:    (float (*)[3])(x), \
+	float (*)[3]: (float (*)[3])(x), \
+	float [3][3]: (float (*)[3])(x)) \
+)
+#define _GPU_MAT4_CONST_CAST(x) (_Generic((x), \
+	void *:       (const float (*)[4])(x), \
+	float *:      (const float (*)[4])(x), \
+	float [16]:   (const float (*)[4])(x), \
+	float (*)[4]: (const float (*)[4])(x), \
+	float [4][4]: (const float (*)[4])(x), \
+	const void *:       (const float (*)[4])(x), \
+	const float *:      (const float (*)[4])(x), \
+	const float [16]:   (const float (*)[4])(x), \
+	const float (*)[4]: (const float (*)[4])(x), \
+	const float [4][4]: (const float (*)[4])(x)) \
+)
+#define _GPU_MAT4_CAST(x) (_Generic((x), \
+	void *:       (float (*)[4])(x), \
+	float *:      (float (*)[4])(x), \
+	float [16]:   (float (*)[4])(x), \
+	float (*)[4]: (float (*)[4])(x), \
+	float [4][4]: (float (*)[4])(x)) \
+)
+#else
+#  define _GPU_MAT3_CONST_CAST(x) (const float (*)[3])(x)
+#  define _GPU_MAT3_CAST(x)             (float (*)[3])(x)
+#  define _GPU_MAT4_CONST_CAST(x) (const float (*)[4])(x)
+#  define _GPU_MAT4_CAST(x)             (float (*)[4])(x)
+#endif  /* C11 */
+
 /* make matrix inputs generic, to avoid warnings */
-#  define gpuMultMatrix(x)  gpuMultMatrix((const float (*)[4])(x))
-#  define gpuLoadMatrix(x)  gpuLoadMatrix((const float (*)[4])(x))
-#  define gpuLoadProjectionMatrix(x)  gpuLoadProjectionMatrix((const float (*)[4])(x))
-#  define gpuGetModelViewMatrix(x)  gpuGetModelViewMatrix((float (*)[4])(x))
-#  define gpuGetProjectionMatrix(x)  gpuGetProjectionMatrix((float (*)[4])(x))
-#  define gpuGetModelViewProjectionMatrix(x)  gpuGetModelViewProjectionMatrix((float (*)[4])(x))
-#  define gpuGetNormalMatrix(x)  gpuGetNormalMatrix((float (*)[3])(x))
-#  define gpuGetNormalMatrixInverse(x)  gpuGetNormalMatrixInverse((float (*)[3])(x))
+#  define gpuMultMatrix(x)  gpuMultMatrix(_GPU_MAT4_CONST_CAST(x))
+#  define gpuLoadMatrix(x)  gpuLoadMatrix(_GPU_MAT4_CONST_CAST(x))
+#  define gpuLoadProjectionMatrix(x)  gpuLoadProjectionMatrix(_GPU_MAT4_CONST_CAST(x))
+#  define gpuGetModelViewMatrix(x)  gpuGetModelViewMatrix(_GPU_MAT4_CAST(x))
+#  define gpuGetProjectionMatrix(x)  gpuGetProjectionMatrix(_GPU_MAT4_CAST(x))
+#  define gpuGetModelViewProjectionMatrix(x)  gpuGetModelViewProjectionMatrix(_GPU_MAT4_CAST(x))
+#  define gpuGetNormalMatrix(x)  gpuGetNormalMatrix(_GPU_MAT3_CAST(x))
+#  define gpuGetNormalMatrixInverse(x)  gpuGetNormalMatrixInverse(_GPU_MAT3_CAST(x))
 #endif /* SUPPRESS_GENERIC_MATRIX_API */
 #endif /* GPU_MATRIX_H */
diff --git a/source/blender/gpu/intern/gpu_matrix.c b/source/blender/gpu/intern/gpu_matrix.c
index 1ea6dcb0e83..a12cd48abed 100644
--- a/source/blender/gpu/intern/gpu_matrix.c
+++ b/source/blender/gpu/intern/gpu_matrix.c
@@ -648,7 +648,7 @@ bool gpuUnProject(const float win[3], const float model[4][4], const float proj[
 	return true;
 }
 
-const float *gpuGetModelViewMatrix(float m[4][4])
+const float (*gpuGetModelViewMatrix(float m[4][4]))[4]
 {
 #if SUPPORT_LEGACY_MATRIX
 	{
@@ -658,20 +658,20 @@ const float *gpuGetModelViewMatrix(float m[4][4])
 		}
 
 		glGetFloatv(GL_MODELVIEW_MATRIX, (float*)m);
-		return (const float*)m;		
+		return m;
 	}
 #endif
 
 	if (m) {
 		copy_m4_m4(m, ModelView);
-		return (const float*)m;
+		return m;
 	}
 	else {
-		return (const float*)ModelView;
+		return ModelView;
 	}
 }
 
-const float *gpuGetProjectionMatrix(float m[4][4])
+const float (*gpuGetProjectionMatrix(float m[4][4]))[4]
 {
 #if SUPPORT_LEGACY_MATRIX
 	{
@@ -681,20 +681,20 @@ const float *gpuGetProjectionMatrix(float m[4][4])
 		}
 
 		glGetFloatv(GL_PROJECTION_MATRIX, (float*)m);
-		return (const float*)m;
+		return m;
 	}
 #endif
 
 	if (m) {
 		copy_m4_m4(m, Projection);
-		return (const float*)m;
+		return m;
 	}
 	else {
-		return (const float*)Projection;
+		return Projection;
 	}
 }
 
-const float *gpuGetModelViewProjectionMatrix(float m[4][4])
+const float (*gpuGetModelViewProjectionMatrix(float m[4][4]))[4]
 {
 	if (m == NULL) {
 		static Mat4 temp;
@@ -707,15 +707,15 @@ const float *gpuGetModelViewProjectionMatrix(float m[4][4])
 		glGetFloatv(GL_MODELVIEW_MATRIX, (float*)m);
 		glGetFloatv(GL_PROJECTION_MATRIX, (float*)proj);
 		mul_m4_m4_pre(m, proj);
-		return (const float*)m;
+		return m;
 	}
 #endif
 
 	mul_m4_m4m4(m, Projection, ModelView);
-	return (const float*)m;
+	return m;
 }
 
-const float *gpuGetNormalMatrix(float m[3][3])
+const float (*gpuGetNormalMatrix(float m[3][3]))[3]
 {
 	if (m == NULL) {
 		static Mat3 temp3;
@@ -727,10 +727,10 @@ const float *gpuGetNormalMatrix(float m[3][3])
 	invert_m3(m);
 	transpose_m3(m);
 
-	return (const float*)m;
+	return m;
 }
 
-const float *gpuGetNormalMatrixInverse(float m[3][3])
+const float (*gpuGetNormalMatrixInverse(float m[3][3]))[3]
 {
 	if (m == NULL) {
 		static Mat3 temp3;
@@ -740,7 +740,7 @@ const float *gpuGetNormalMatrixInverse(float m[3][3])
 	gpuGetNormalMatrix(m);
 	invert_m3(m);
 
-	return (const float*)m;
+	return m;
 }
 
 void gpuBindMatrices(const ShaderInterface* shaderface)
@@ -763,7 +763,7 @@ void gpuBindMatrices(const ShaderInterface* shaderface)
 		puts("setting MV matrix");
 		#endif
 
-		glUniformMatrix4fv(MV->location, 1, GL_FALSE, gpuGetModelViewMatrix(NULL));
+		glUniformMatrix4fv(MV->location, 1, GL_FALSE, (const float *)gpuGetModelViewMatrix(NULL));
 	}
 
 	if (P) {
@@ -771,7 +771,7 @@ void gpuBindMatrices(const ShaderInterface* shaderface)
 		puts("setting P matrix");
 		#endif
 
-		glUniformMatrix4fv(P->location, 1, GL_FALSE, gpuGetProjectionMatrix(NULL));
+		glUniformMatrix4fv(P->location, 1, GL_FALSE, (const float *)gpuGetProjectionMatrix(NULL));
 	}
 
 	if (MVP) {
@@ -779,7 +779,7 @@ void gpuBindMatrices(const ShaderInterface* shaderface)
 		puts("setting MVP matrix");
 		#endif
 
-		glUniformMatrix4fv(MVP->location, 1, GL_FALSE, gpuGetModelViewProjectionMatrix(NULL));
+		glUniformMatrix4fv(MVP->location, 1, GL_FALSE, (const float *)gpuGetModelViewProjectionMatrix(NULL));
 	}
 
 	if (N) {
@@ -787,7 +787,7 @@ void gpuBindMatrices(const ShaderInterface* shaderface)
 		puts("setting normal matrix");
 		#endif
 
-		glUniformMatrix3fv(N->location, 1, GL_FALSE, gpuGetNormalMatrix(NULL));
+		glUniformMatrix3fv(N->location, 1, GL_FALSE, (const float *)gpuGetNormalMatrix(NULL));
 	}
 
 	if (MV_inv) {




More information about the Bf-blender-cvs mailing list