[Bf-blender-cvs] [7a60f88] blender2.8: OpenGL: plug new matrix system into shaders (WIP)

Mike Erwin noreply at git.blender.org
Mon Oct 10 05:04:51 CEST 2016


Commit: 7a60f889d3f328e38aa882891cda615a06333ab6
Author: Mike Erwin
Date:   Sun Oct 9 23:03:35 2016 -0400
Branches: blender2.8
https://developer.blender.org/rB7a60f889d3f328e38aa882891cda615a06333ab6

OpenGL: plug new matrix system into shaders (WIP)

Built-in shaders now use uniforms instead of legacy built-in matrices. So far I only hooked this up for new immediate mode.

We use the same matrix naming convention as OpenGL, but without the gl_ prefix, e.g. gl_ModelView becomes ModelView.

Right now it can skip the new matrix stack and use the legacy built-in matrices app-side. This will help us transition gradually from glMatrix functions to gpuMatrix functions.

Still some work to do in gpuBindMatrices. See TODO comments in gpu_matrix.c for specifics.

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

M	source/blender/gpu/GPU_matrix.h
M	source/blender/gpu/intern/gpu_immediate.c
M	source/blender/gpu/intern/gpu_matrix.c
M	source/blender/gpu/shaders/gpu_shader_2D_flat_color_vert.glsl
M	source/blender/gpu/shaders/gpu_shader_2D_no_color_vert.glsl
M	source/blender/gpu/shaders/gpu_shader_2D_point_uniform_size_uniform_color_outline_smooth_vert.glsl
M	source/blender/gpu/shaders/gpu_shader_2D_point_uniform_size_uniform_color_smooth_vert.glsl
M	source/blender/gpu/shaders/gpu_shader_2D_point_varying_size_varying_color_vert.glsl
M	source/blender/gpu/shaders/gpu_shader_2D_smooth_color_vert.glsl
M	source/blender/gpu/shaders/gpu_shader_2D_texture_vert.glsl
M	source/blender/gpu/shaders/gpu_shader_3D_flat_color_vert.glsl
M	source/blender/gpu/shaders/gpu_shader_3D_no_color_vert.glsl
M	source/blender/gpu/shaders/gpu_shader_3D_point_fixed_size_varying_color_vert.glsl
M	source/blender/gpu/shaders/gpu_shader_3D_point_varying_size_no_color_vert.glsl
M	source/blender/gpu/shaders/gpu_shader_3D_point_varying_size_varying_color_vert.glsl
M	source/blender/gpu/shaders/gpu_shader_3D_smooth_color_vert.glsl

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

diff --git a/source/blender/gpu/GPU_matrix.h b/source/blender/gpu/GPU_matrix.h
index 13648fd..7356629 100644
--- a/source/blender/gpu/GPU_matrix.h
+++ b/source/blender/gpu/GPU_matrix.h
@@ -115,6 +115,14 @@ bool gpuUnProject(const float win[3], const float model[4][4], const float proj[
 void gpuOrtho2D(float left, float right, float bottom, float top);
 
 
+/* functions to get matrix values */
+const float *gpuGetModelViewMatrix3D(float m[4][4]);
+const float *gpuGetProjectionMatrix3D(float m[4][4]);
+const float *gpuGetModelViewProjectionMatrix3D(float m[4][4]);
+
+/* set uniform values for currently bound shader */
+void gpuBindMatrices(GLuint program);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/gpu/intern/gpu_immediate.c b/source/blender/gpu/intern/gpu_immediate.c
index 0fc4253..5faedba 100644
--- a/source/blender/gpu/intern/gpu_immediate.c
+++ b/source/blender/gpu/intern/gpu_immediate.c
@@ -26,10 +26,12 @@
  */
 
 #include "GPU_immediate.h"
+#include "GPU_matrix.h"
 #include "gpu_shader_private.h"
 
 void immBindBuiltinProgram(GPUBuiltinShader shader_id)
 {
 	GPUShader *shader = GPU_shader_get_builtin_shader(shader_id);
 	immBindProgram(shader->program);
+	gpuBindMatrices(shader->program);
 }
diff --git a/source/blender/gpu/intern/gpu_matrix.c b/source/blender/gpu/intern/gpu_matrix.c
index 3cc93b9..2c9bc84 100644
--- a/source/blender/gpu/intern/gpu_matrix.c
+++ b/source/blender/gpu/intern/gpu_matrix.c
@@ -35,6 +35,10 @@
 #include "BLI_math_rotation.h"
 #include "BLI_math_vector.h"
 
+/* For now we support the legacy matrix stack in gpuGetMatrix functions.
+ * Will remove this after switching to core profile, which can happen after
+ * we convert all code to use the API in this file. */
+#define SUPPORT_LEGACY_MATRIX 1
 
 #define MATRIX_STACK_DEPTH 32
 
@@ -149,19 +153,6 @@ void gpuLoadMatrix2D(const float m[3][3])
 	CHECKMAT(ModelView2D);
 }
 
-//const float *gpuGetMatrix(eGPUMatrixMode stack, float *m)
-//{
-//	GPU_matrix_stack *ms_select = mstacks + stack;
-//
-//	if (m) {
-//		copy_m4_m4((float (*)[4])m, ms_select->dynstack[ms_select->pos]);
-//		return m;
-//	}
-//	else {
-//		return (float*)(ms_select->dynstack[ms_select->pos]);
-//	}
-//}
-
 void gpuLoadIdentity()
 {
 	switch (state.mode) {
@@ -507,3 +498,113 @@ bool gpuUnProject(const float win[3], const float model[4][4], const float proj[
 		return true;
 	}
 }
+
+const float *gpuGetModelViewMatrix3D(float m[4][4])
+{
+#if SUPPORT_LEGACY_MATRIX
+	if (state.mode == MATRIX_MODE_INACTIVE) {
+		if (m == NULL) {
+			static Mat4 temp;
+			m = temp;
+		}
+
+		glGetFloatv(GL_MODELVIEW_MATRIX, (float*)m);
+		return (const float*)m;		
+	}
+#endif
+
+	BLI_assert(state.mode == MATRIX_MODE_3D);
+
+	if (m) {
+		copy_m4_m4(m, ModelView3D);
+		return (const float*)m;
+	}
+	else {
+		return (const float*)ModelView3D;
+	}
+}
+
+const float *gpuGetProjectionMatrix3D(float m[4][4])
+{
+#if SUPPORT_LEGACY_MATRIX
+	if (state.mode == MATRIX_MODE_INACTIVE) {
+		if (m == NULL) {
+			static Mat4 temp;
+			m = temp;
+		}
+
+		glGetFloatv(GL_PROJECTION_MATRIX, (float*)m);
+		return (const float*)m;
+	}
+#endif
+
+	BLI_assert(state.mode == MATRIX_MODE_3D);
+
+	if (m) {
+		copy_m4_m4(m, ModelView3D);
+		return (const float*)m;
+	}
+	else {
+		return (const float*)ModelView3D;
+	}
+}
+
+const float *gpuGetModelViewProjectionMatrix3D(float m[4][4])
+{
+#if SUPPORT_LEGACY_MATRIX
+	if (state.mode == MATRIX_MODE_INACTIVE) {
+		if (m == NULL) {
+			static Mat4 temp;
+			m = temp;
+		}
+
+		Mat4 proj;
+		glGetFloatv(GL_MODELVIEW_MATRIX, (float*)proj);
+		glGetFloatv(GL_PROJECTION_MATRIX, (float*)m);
+		mul_m4_m4_post(m, proj);
+		return (const float*)m;
+	}
+#endif
+
+	BLI_assert(state.mode == MATRIX_MODE_3D);
+
+	if (m == NULL) {
+		static Mat4 temp;
+		m = temp;
+	}
+
+	mul_m4_m4m4(m, ModelView3D, Projection3D);
+	return (const float*)m;
+}
+
+void gpuBindMatrices(GLuint program)
+{
+	/* TODO: split this into 2 functions
+	 * 1) get uniform locations & determine 2D or 3D
+	 */
+	GLint loc_MV = glGetUniformLocation(program, "ModelViewMatrix");
+	GLint loc_P = glGetUniformLocation(program, "ProjectionMatrix");
+	GLint loc_MVP = glGetUniformLocation(program, "ModelViewProjectionMatrix");
+
+	/* 2) set uniform values to matrix stack values
+	 * program needs to be bound
+	 */
+	glUseProgram(program);
+
+
+	/* call this portion before a draw call if desired matrices are dirty */
+	if (loc_MV != -1) {
+		puts("setting MV matrix");
+		glUniformMatrix4fv(loc_MV, 1, GL_FALSE, gpuGetModelViewMatrix3D(NULL));
+	}
+
+	if (loc_P != -1) {
+		puts("setting P matrix");
+		glUniformMatrix4fv(loc_P, 1, GL_FALSE, gpuGetProjectionMatrix3D(NULL));
+	}
+
+	if (loc_MVP != -1) {
+		puts("setting MVP matrix");
+		glUniformMatrix4fv(loc_MVP, 1, GL_FALSE, gpuGetModelViewProjectionMatrix3D(NULL));
+	}
+}
diff --git a/source/blender/gpu/shaders/gpu_shader_2D_flat_color_vert.glsl b/source/blender/gpu/shaders/gpu_shader_2D_flat_color_vert.glsl
index aa1eba4..96c833f 100644
--- a/source/blender/gpu/shaders/gpu_shader_2D_flat_color_vert.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_2D_flat_color_vert.glsl
@@ -1,4 +1,6 @@
 
+uniform mat4 ModelViewProjectionMatrix;
+
 #if __VERSION__ == 120
   attribute vec2 pos;
   attribute vec4 color;
@@ -13,6 +15,6 @@
 
 void main()
 {
-	gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0);
+	gl_Position = ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0);
 	finalColor = color;
 }
diff --git a/source/blender/gpu/shaders/gpu_shader_2D_no_color_vert.glsl b/source/blender/gpu/shaders/gpu_shader_2D_no_color_vert.glsl
index 7ad3005..4049171 100644
--- a/source/blender/gpu/shaders/gpu_shader_2D_no_color_vert.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_2D_no_color_vert.glsl
@@ -1,4 +1,6 @@
 
+uniform mat4 ModelViewProjectionMatrix;
+
 #if __VERSION__ == 120
   attribute vec2 pos;
 #else
@@ -7,5 +9,5 @@
 
 void main()
 {
-	gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0);
+	gl_Position = ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0);
 }
diff --git a/source/blender/gpu/shaders/gpu_shader_2D_point_uniform_size_uniform_color_outline_smooth_vert.glsl b/source/blender/gpu/shaders/gpu_shader_2D_point_uniform_size_uniform_color_outline_smooth_vert.glsl
index cff493b..a37ae16 100644
--- a/source/blender/gpu/shaders/gpu_shader_2D_point_uniform_size_uniform_color_outline_smooth_vert.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_2D_point_uniform_size_uniform_color_outline_smooth_vert.glsl
@@ -1,4 +1,5 @@
 
+uniform mat4 ModelViewProjectionMatrix;
 uniform float size;
 uniform float outlineWidth;
 
@@ -11,7 +12,7 @@ uniform float outlineWidth;
 #endif
 
 void main() {
-	gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0);
+	gl_Position = ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0);
 	gl_PointSize = size;
 
 	// calculate concentric radii in pixels
diff --git a/source/blender/gpu/shaders/gpu_shader_2D_point_uniform_size_uniform_color_smooth_vert.glsl b/source/blender/gpu/shaders/gpu_shader_2D_point_uniform_size_uniform_color_smooth_vert.glsl
index f72c4c5..201e5e9 100644
--- a/source/blender/gpu/shaders/gpu_shader_2D_point_uniform_size_uniform_color_smooth_vert.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_2D_point_uniform_size_uniform_color_smooth_vert.glsl
@@ -1,4 +1,5 @@
 
+uniform mat4 ModelViewProjectionMatrix;
 uniform float size;
 
 #if __VERSION__ == 120
@@ -10,7 +11,7 @@ uniform float size;
 #endif
 
 void main() {
-	gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0);
+	gl_Position = ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0);
 	gl_PointSize = size;
 
 	// calculate concentric radii in pixels
diff --git a/source/blender/gpu/shaders/gpu_shader_2D_point_varying_size_varying_color_vert.glsl b/source/blender/gpu/shaders/gpu_shader_2D_point_varying_size_varying_color_vert.glsl
index 4cdd43a..42ff51e 100644
--- a/source/blender/gpu/shaders/gpu_shader_2D_point_varying_size_varying_color_vert.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_2D_point_varying_size_varying_color_vert.glsl
@@ -1,4 +1,6 @@
 
+uniform mat4 ModelViewProjectionMatrix;
+
 #if __VERSION__ == 120
   attribute vec2 pos;
   attribute float size;
@@ -13,7 +15,7 @@
 
 void main()
 {
-	gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0);
+	gl_Position = ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0);
 	gl_PointSize = size;
 	finalColor = color;
 }
diff --git a/source/blender/gpu/shaders/gpu_shader_2D_smooth_color_vert.glsl b/source/blender/gpu/shaders/gpu_shader_2D_smooth_color_vert.glsl
index 0160662..9daf2d7 100644
--- a/source/blender/gpu/shaders/gpu_shader_2D_smooth_color_vert.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_2D_smooth_color_vert.glsl
@@ -1,4 +1,6 @@
 
+uniform mat4 ModelViewProjectionMatrix;
+
 #if __VERSION__ == 120
   attribute vec2 pos;
   attribute vec4 color;
@@ -13,6 +15,6 @@
 
 void main()
 {
-	gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0);
+	gl_Position = ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0);
 	finalColor = color;
 }
diff --git a/source/blender/gpu/shaders/gpu_shader_2D_texture_vert.glsl b/source/blender/gpu/shaders/gpu_shader_2D_texture_vert.glsl
index d4d36de..4c05cf1 100644
--- a/source/blender/gpu/shaders/gpu_shader_2D_texture_vert.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_2D_texture_vert.glsl
@@ -1,3 +1,6 @@
+
+uniform mat4 ModelViewProjectionMatrix;
+
 #if __VERSION__ == 120
   attribute vec2 texcoord;
   attribute vec3 position;
@@ -11,6 +14,6 @@
 
 void main()
 {
-	gl_Position = gl_ModelViewProjectionMatrix * vec4(position.xyz, 1.0f);
+	gl_Position = ModelViewProjectionMatrix * vec4(position.xyz, 1.0f);
 	texture_coord = texcoord;
 }
diff --git a/source/blender/gpu/shaders/gpu_shader_3D_flat_color_vert.glsl b/source/blender/gpu/shaders/gpu_shader_3D_flat_color_vert.glsl
index ec6037e..8c241cf 100644
--- a/source/blender/gpu/shaders/gpu_shader_3D_flat_color_vert.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_3D_flat_color_vert.glsl
@@ -1,4 +1,6 @@
 
+uniform mat4 ModelViewProjectionMatrix;
+
 #if __VERSION__ == 120
   attribute vec3 pos;
   attribute vec4 color;
@@ -13,6 +15,6 @@
 
 void main()
 {
-	gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1.0);
+	gl_Position = ModelVie

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list