From 7a60f889d3f328e38aa882891cda615a06333ab6 Mon Sep 17 00:00:00 2001 From: Mike Erwin Date: Sun, 9 Oct 2016 23:03:35 -0400 Subject: 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. --- source/blender/gpu/GPU_matrix.h | 8 ++ source/blender/gpu/intern/gpu_immediate.c | 2 + source/blender/gpu/intern/gpu_matrix.c | 127 ++++++++++++++++++--- .../gpu/shaders/gpu_shader_2D_flat_color_vert.glsl | 4 +- .../gpu/shaders/gpu_shader_2D_no_color_vert.glsl | 4 +- ...orm_size_uniform_color_outline_smooth_vert.glsl | 3 +- ...int_uniform_size_uniform_color_smooth_vert.glsl | 3 +- ...r_2D_point_varying_size_varying_color_vert.glsl | 4 +- .../shaders/gpu_shader_2D_smooth_color_vert.glsl | 4 +- .../gpu/shaders/gpu_shader_2D_texture_vert.glsl | 5 +- .../gpu/shaders/gpu_shader_3D_flat_color_vert.glsl | 4 +- .../gpu/shaders/gpu_shader_3D_no_color_vert.glsl | 4 +- ...der_3D_point_fixed_size_varying_color_vert.glsl | 4 +- ...shader_3D_point_varying_size_no_color_vert.glsl | 4 +- ...r_3D_point_varying_size_varying_color_vert.glsl | 4 +- .../shaders/gpu_shader_3D_smooth_color_vert.glsl | 4 +- 16 files changed, 162 insertions(+), 26 deletions(-) (limited to 'source') diff --git a/source/blender/gpu/GPU_matrix.h b/source/blender/gpu/GPU_matrix.h index 13648fdc3bd..73566292799 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 0fc42533475..5faedbae04d 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 3cc93b9c1f1..2c9bc842083 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 aa1eba470ab..96c833f3b93 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 7ad30052cf5..4049171f73d 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 cff493bcfc7..a37ae16f837 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 f72c4c5a9e7..201e5e90ecc 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 4cdd43a0a97..42ff51e3d03 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 01606622c78..9daf2d75016 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 d4d36def8cd..4c05cf1d25b 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 ec6037e5077..8c241cff5d4 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 = ModelViewProjectionMatrix * vec4(pos, 1.0); finalColor = color; } diff --git a/source/blender/gpu/shaders/gpu_shader_3D_no_color_vert.glsl b/source/blender/gpu/shaders/gpu_shader_3D_no_color_vert.glsl index 5d10eaec6d8..32da3a99c63 100644 --- a/source/blender/gpu/shaders/gpu_shader_3D_no_color_vert.glsl +++ b/source/blender/gpu/shaders/gpu_shader_3D_no_color_vert.glsl @@ -1,4 +1,6 @@ +uniform mat4 ModelViewProjectionMatrix; + #if __VERSION__ == 120 attribute vec3 pos; #else @@ -7,5 +9,5 @@ void main() { - gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1.0); + gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0); } diff --git a/source/blender/gpu/shaders/gpu_shader_3D_point_fixed_size_varying_color_vert.glsl b/source/blender/gpu/shaders/gpu_shader_3D_point_fixed_size_varying_color_vert.glsl index 451d8620596..84e77e59e90 100644 --- a/source/blender/gpu/shaders/gpu_shader_3D_point_fixed_size_varying_color_vert.glsl +++ b/source/blender/gpu/shaders/gpu_shader_3D_point_fixed_size_varying_color_vert.glsl @@ -1,4 +1,6 @@ +uniform mat4 ModelViewProjectionMatrix; + #if __VERSION__ == 120 attribute vec3 pos; attribute vec4 color; @@ -11,6 +13,6 @@ void main() { - gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1.0); + gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0); finalColor = color; } diff --git a/source/blender/gpu/shaders/gpu_shader_3D_point_varying_size_no_color_vert.glsl b/source/blender/gpu/shaders/gpu_shader_3D_point_varying_size_no_color_vert.glsl index 0bdcbd5fef3..1fcda765b99 100644 --- a/source/blender/gpu/shaders/gpu_shader_3D_point_varying_size_no_color_vert.glsl +++ b/source/blender/gpu/shaders/gpu_shader_3D_point_varying_size_no_color_vert.glsl @@ -1,4 +1,6 @@ +uniform mat4 ModelViewProjectionMatrix; + #if __VERSION__ == 120 attribute vec3 pos; attribute float size; @@ -9,6 +11,6 @@ void main() { - gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1.0); + gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0); gl_PointSize = size; } diff --git a/source/blender/gpu/shaders/gpu_shader_3D_point_varying_size_varying_color_vert.glsl b/source/blender/gpu/shaders/gpu_shader_3D_point_varying_size_varying_color_vert.glsl index 529b46d3e2b..7999435f0e4 100644 --- a/source/blender/gpu/shaders/gpu_shader_3D_point_varying_size_varying_color_vert.glsl +++ b/source/blender/gpu/shaders/gpu_shader_3D_point_varying_size_varying_color_vert.glsl @@ -1,4 +1,6 @@ +uniform mat4 ModelViewProjectionMatrix; + #if __VERSION__ == 120 attribute vec3 pos; attribute float size; @@ -13,7 +15,7 @@ void main() { - gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1.0); + gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0); gl_PointSize = size; finalColor = color; } diff --git a/source/blender/gpu/shaders/gpu_shader_3D_smooth_color_vert.glsl b/source/blender/gpu/shaders/gpu_shader_3D_smooth_color_vert.glsl index 20b5e126736..22c2cfa8b93 100644 --- a/source/blender/gpu/shaders/gpu_shader_3D_smooth_color_vert.glsl +++ b/source/blender/gpu/shaders/gpu_shader_3D_smooth_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 = ModelViewProjectionMatrix * vec4(pos, 1.0); finalColor = color; } -- cgit v1.2.3