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.
This commit is contained in:
2016-10-09 23:03:35 -04:00
parent e636529e33
commit 7a60f889d3
16 changed files with 162 additions and 26 deletions

View File

@@ -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

View File

@@ -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);
}

View File

@@ -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));
}
}

View File

@@ -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;
}

View File

@@ -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);
}

View File

@@ -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

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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);
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}