Browse Source

Unify separate GL/GLES code paths for love’s built in matrices in shaders.

--HG--
branch : minor
Alex Szpakowski 8 years ago
parent
commit
52b7b0f80d

+ 0 - 37
src/modules/graphics/opengl/OpenGL.cpp

@@ -167,14 +167,6 @@ void OpenGL::setupContext()
 
 	createDefaultTexture();
 
-	// Invalidate the cached matrices by setting some elements to NaN.
-	float nan = std::numeric_limits<float>::quiet_NaN();
-	state.lastProjectionMatrix.setTranslation(nan, nan);
-	state.lastTransformMatrix.setTranslation(nan, nan);
-
-	if (GLAD_VERSION_1_0)
-		glMatrixMode(GL_MODELVIEW);
-
 	contextInitialized = true;
 }
 
@@ -345,35 +337,6 @@ void OpenGL::prepareDraw()
 	// Make sure the active shader's love-provided uniforms are up to date.
 	if (Shader::current != nullptr)
 		Shader::current->checkSetBuiltinUniforms();
-
-	// We use glLoadMatrix rather than uniforms for our matrices when possible,
-	// because uniform uploads can be significantly slower than glLoadMatrix.
-	if (GLAD_VERSION_1_0)
-	{
-		auto gfx = Module::getInstance<graphics::Graphics>(Module::M_GRAPHICS);
-		const Matrix4 &curxform = gfx->getTransform();
-		const Matrix4 &curproj = gfx->getProjection();
-
-		const Matrix4 &lastproj = state.lastProjectionMatrix;
-		const Matrix4 &lastxform = state.lastTransformMatrix;
-
-		// We only need to re-upload the projection matrix if it's changed.
-		if (memcmp(curproj.getElements(), lastproj.getElements(), sizeof(float) * 16) != 0)
-		{
-			glMatrixMode(GL_PROJECTION);
-			glLoadMatrixf(curproj.getElements());
-			glMatrixMode(GL_MODELVIEW);
-
-			state.lastProjectionMatrix = curproj;
-		}
-
-		// Same with the transform matrix.
-		if (memcmp(curxform.getElements(), lastxform.getElements(), sizeof(float) * 16) != 0)
-		{
-			glLoadMatrixf(curxform.getElements());
-			state.lastTransformMatrix = curxform;
-		}
-	}
 }
 
 GLenum OpenGL::getGLBufferType(BufferType type)

+ 0 - 3
src/modules/graphics/opengl/OpenGL.h

@@ -439,9 +439,6 @@ private:
 
 		GLuint defaultTexture;
 
-		Matrix4 lastProjectionMatrix;
-		Matrix4 lastTransformMatrix;
-
 	} state;
 
 }; // OpenGL

+ 36 - 40
src/modules/graphics/opengl/Shader.cpp

@@ -892,58 +892,54 @@ void Shader::checkSetBuiltinUniforms()
 
 	checkSetScreenParams();
 
-	// We use a more efficient method for sending transformation matrices to
-	// the GPU on desktop GL.
 	if (GLAD_ES_VERSION_2_0)
-	{
 		checkSetPointSize(gl.getPointSize());
 
-		auto gfx = Module::getInstance<graphics::Graphics>(Module::M_GRAPHICS);
+	auto gfx = Module::getInstance<graphics::Graphics>(Module::M_GRAPHICS);
 
-		const Matrix4 &curproj = gfx->getProjection();
-		const Matrix4 &curxform = gfx->getTransform();
+	const Matrix4 &curproj = gfx->getProjection();
+	const Matrix4 &curxform = gfx->getTransform();
 
-		bool tpmatrixneedsupdate = false;
+	bool tpmatrixneedsupdate = false;
 
-		// Only upload the matrices if they've changed.
-		if (memcmp(curxform.getElements(), lastTransformMatrix.getElements(), sizeof(float) * 16) != 0)
+	// Only upload the matrices if they've changed.
+	if (memcmp(curxform.getElements(), lastTransformMatrix.getElements(), sizeof(float) * 16) != 0)
+	{
+		GLint location = builtinUniforms[BUILTIN_TRANSFORM_MATRIX];
+		if (location >= 0)
+			glUniformMatrix4fv(location, 1, GL_FALSE, curxform.getElements());
+
+		// Also upload the re-calculated normal matrix, if possible. The normal
+		// matrix is the transpose of the inverse of the rotation portion
+		// (top-left 3x3) of the transform matrix.
+		location = builtinUniforms[BUILTIN_NORMAL_MATRIX];
+		if (location >= 0)
 		{
-			GLint location = builtinUniforms[BUILTIN_TRANSFORM_MATRIX];
-			if (location >= 0)
-				glUniformMatrix4fv(location, 1, GL_FALSE, curxform.getElements());
-
-			// Also upload the re-calculated normal matrix, if possible. The
-			// normal matrix is the transpose of the inverse of the rotation
-			// portion (top-left 3x3) of the transform matrix.
-			location = builtinUniforms[BUILTIN_NORMAL_MATRIX];
-			if (location >= 0)
-			{
-				Matrix3 normalmatrix = Matrix3(curxform).transposedInverse();
-				glUniformMatrix3fv(location, 1, GL_FALSE, normalmatrix.getElements());
-			}
-
-			tpmatrixneedsupdate = true;
-			lastTransformMatrix = curxform;
+			Matrix3 normalmatrix = Matrix3(curxform).transposedInverse();
+			glUniformMatrix3fv(location, 1, GL_FALSE, normalmatrix.getElements());
 		}
 
-		if (memcmp(curproj.getElements(), lastProjectionMatrix.getElements(), sizeof(float) * 16) != 0)
-		{
-			GLint location = builtinUniforms[BUILTIN_PROJECTION_MATRIX];
-			if (location >= 0)
-				glUniformMatrix4fv(location, 1, GL_FALSE, curproj.getElements());
+		tpmatrixneedsupdate = true;
+		lastTransformMatrix = curxform;
+	}
 
-			tpmatrixneedsupdate = true;
-			lastProjectionMatrix = curproj;
-		}
+	if (memcmp(curproj.getElements(), lastProjectionMatrix.getElements(), sizeof(float) * 16) != 0)
+	{
+		GLint location = builtinUniforms[BUILTIN_PROJECTION_MATRIX];
+		if (location >= 0)
+			glUniformMatrix4fv(location, 1, GL_FALSE, curproj.getElements());
+
+		tpmatrixneedsupdate = true;
+		lastProjectionMatrix = curproj;
+	}
 
-		if (tpmatrixneedsupdate)
+	if (tpmatrixneedsupdate)
+	{
+		GLint location = builtinUniforms[BUILTIN_TRANSFORM_PROJECTION_MATRIX];
+		if (location >= 0)
 		{
-			GLint location = builtinUniforms[BUILTIN_TRANSFORM_PROJECTION_MATRIX];
-			if (location >= 0)
-			{
-				Matrix4 tp_matrix(curproj * curxform);
-				glUniformMatrix4fv(location, 1, GL_FALSE, tp_matrix.getElements());
-			}
+			Matrix4 tp_matrix(curproj, curxform);
+			glUniformMatrix4fv(location, 1, GL_FALSE, tp_matrix.getElements());
 		}
 	}
 }

+ 0 - 7
src/modules/graphics/opengl/wrap_Graphics.lua

@@ -46,7 +46,6 @@ GLSL.SYNTAX = [[
 
 -- Uniforms shared by the vertex and pixel shader stages.
 GLSL.UNIFORMS = [[
-#ifdef GL_ES
 // According to the GLSL ES 1.0 spec, uniform precision must match between stages,
 // but we can't guarantee that highp is always supported in fragment shaders...
 // We *really* don't want to use mediump for these in vertex shaders though.
@@ -59,12 +58,6 @@ uniform LOVE_UNIFORM_PRECISION mat4 TransformMatrix;
 uniform LOVE_UNIFORM_PRECISION mat4 ProjectionMatrix;
 uniform LOVE_UNIFORM_PRECISION mat4 TransformProjectionMatrix;
 uniform LOVE_UNIFORM_PRECISION mat3 NormalMatrix;
-#else
-#define TransformMatrix gl_ModelViewMatrix
-#define ProjectionMatrix gl_ProjectionMatrix
-#define TransformProjectionMatrix gl_ModelViewProjectionMatrix
-#define NormalMatrix gl_NormalMatrix
-#endif
 uniform mediump vec4 love_ScreenSize;]]
 
 GLSL.FUNCTIONS = [[