From eb151160ef15845a135f9cc6e09cc88abaf7ae33 Mon Sep 17 00:00:00 2001
From: Tim Felgentreff <timfelgentreff@gmail.com>
Date: Wed, 25 May 2016 14:01:52 +0200
Subject: [PATCH] make shader debugging easier

---
 src/include/shaders.h |   1 +
 src/video/shaders.cpp | 117 +++++++++++++++++++++++++-----------------
 2 files changed, 71 insertions(+), 47 deletions(-)

diff --git a/src/include/shaders.h b/src/include/shaders.h
index 6cc07a479..94dcfe6d0 100644
--- a/src/include/shaders.h
+++ b/src/include/shaders.h
@@ -2,6 +2,7 @@
 #define __SHADERS_H__
 #ifdef USE_OPENGL
 #define MAX_SHADERS 5
+// #define SHADERDEBUG // Uncomment for loading shaders from file
 #ifndef __APPLE__
 extern PFNGLBINDFRAMEBUFFEREXTPROC glBindFramebuffer;
 #else
diff --git a/src/video/shaders.cpp b/src/video/shaders.cpp
index 08f502e79..d568a5c53 100644
--- a/src/video/shaders.cpp
+++ b/src/video/shaders.cpp
@@ -26,52 +26,52 @@ const char* fragment_shaders[MAX_SHADERS] = {
 		gl_FragColor = myColor;\n\
 	}",
 	// Scale2x
-	"#version 110\n\
-	\n\
+	"#version 110\n\
+	\n\
 	uniform sampler2D u_texture;\n\
 	uniform float u_width;\n\
 	uniform float u_height;\n\
 	uniform float u_widthrel;\n\
-	uniform float u_heightrel;\n\
-	\n\
-	void main() {\n\
-		// o = offset, the width of a pixel\n\
-        vec2 texCoord = gl_TexCoord[0].xy * vec2(u_widthrel, -u_heightrel);\n\
-        vec2 textureDimensions = vec2(u_width, u_height);\n\
-		vec2 o = 1.0 / textureDimensions;\n\
-		// texel arrangement\n\
-		// A B C\n\
-		// D E F\n\
-		// G H I\n\
-		vec4 A = texture2D(u_texture, texCoord + vec2( -o.x,  o.y));\n\
-		vec4 B = texture2D(u_texture, texCoord + vec2(    0,  o.y));\n\
-		vec4 C = texture2D(u_texture, texCoord + vec2(  o.x,  o.y));\n\
-		vec4 D = texture2D(u_texture, texCoord + vec2( -o.x,    0));\n\
-		vec4 E = texture2D(u_texture, texCoord + vec2(    0,    0));\n\
-		vec4 F = texture2D(u_texture, texCoord + vec2(  o.x,    0));\n\
-		vec4 G = texture2D(u_texture, texCoord + vec2( -o.x, -o.y));\n\
-		vec4 H = texture2D(u_texture, texCoord + vec2(    0, -o.y));\n\
-		vec4 I = texture2D(u_texture, texCoord + vec2(  o.x, -o.y));\n\
-		vec2 p = texCoord * textureDimensions;\n\
-		// p = the position within a pixel [0...1]\n\
-		p = p - floor(p);\n\
-		if (p.x > .5) {\n\
-			if (p.y > .5) {\n\
-				// Top Right\n\
-				gl_FragColor = B == F && B != D && F != H ? F : E;\n\
-			} else {\n\
-				// Bottom Right\n\
-				gl_FragColor = H == F && D != H && B != F ? F : E;\n\
-			}\n\
-		} else {\n\
-			if (p.y > .5) {\n\
-				// Top Left\n\
-				gl_FragColor = D == B && B != F && D != H ? D : E;\n\
-			} else {\n\
-				// Bottom Left\n\
-				gl_FragColor = D == H && D != B && H != F ? D : E;\n\
-			}\n\
-		}\n\
+	uniform float u_heightrel;\n\
+	\n\
+	void main() {\n\
+		// o = offset, the width of a pixel\n\
+        vec2 texCoord = gl_TexCoord[0].xy * vec2(u_widthrel, -u_heightrel);\n\
+        vec2 textureDimensions = vec2(u_width, u_height);\n\
+		vec2 o = 1.0 / textureDimensions;\n\
+		// texel arrangement\n\
+		// A B C\n\
+		// D E F\n\
+		// G H I\n\
+		vec4 A = texture2D(u_texture, texCoord + vec2( -o.x,  o.y));\n\
+		vec4 B = texture2D(u_texture, texCoord + vec2(    0,  o.y));\n\
+		vec4 C = texture2D(u_texture, texCoord + vec2(  o.x,  o.y));\n\
+		vec4 D = texture2D(u_texture, texCoord + vec2( -o.x,    0));\n\
+		vec4 E = texture2D(u_texture, texCoord + vec2(    0,    0));\n\
+		vec4 F = texture2D(u_texture, texCoord + vec2(  o.x,    0));\n\
+		vec4 G = texture2D(u_texture, texCoord + vec2( -o.x, -o.y));\n\
+		vec4 H = texture2D(u_texture, texCoord + vec2(    0, -o.y));\n\
+		vec4 I = texture2D(u_texture, texCoord + vec2(  o.x, -o.y));\n\
+		vec2 p = texCoord * textureDimensions;\n\
+		// p = the position within a pixel [0...1]\n\
+		p = p - floor(p);\n\
+		if (p.x > .5) {\n\
+			if (p.y > .5) {\n\
+				// Top Right\n\
+				gl_FragColor = B == F && B != D && F != H ? F : E;\n\
+			} else {\n\
+				// Bottom Right\n\
+				gl_FragColor = H == F && D != H && B != F ? F : E;\n\
+			}\n\
+		} else {\n\
+			if (p.y > .5) {\n\
+				// Top Left\n\
+				gl_FragColor = D == B && B != F && D != H ? D : E;\n\
+			} else {\n\
+				// Bottom Left\n\
+				gl_FragColor = D == H && D != B && H != F ? D : E;\n\
+			}\n\
+		}\n\
 	}",
 	// HQX
 	"#version 130\n\
@@ -359,6 +359,11 @@ GLuint fullscreenShader;
 GLuint fullscreenFramebuffer = 0;
 GLuint fullscreenTexture;
 
+#ifdef SHADERDEBUG
+#include <iostream>
+#include <fstream>
+#endif
+
 void printShaderInfoLog(GLuint obj, const char* prefix)
 {
 	int infologLength = 0;
@@ -401,29 +406,45 @@ extern bool LoadShaders() {
 	if (fs == 0) {
 	    return false;
 	}
+#ifdef SHADERDEBUG
+	std::ifstream myfile("fragment.txt");
+	std::string contents((std::istreambuf_iterator<char>(myfile)),
+						  std::istreambuf_iterator<char>());
+	const char* f = contents.c_str();
+	glShaderSource(fs, 1, &f, NULL);
+	myfile.close();
+#else
 	glShaderSource(fs, 1, (const char**)&(fragment_shaders[ShaderIndex]), NULL);
+#endif
 	glCompileShader(fs);
 	glGetShaderiv(fs, GL_COMPILE_STATUS, &params);
 	if (params == GL_FALSE) {
+#ifdef SHADERDEBUG
+		printShaderInfoLog(fs, "Fragment Shader");
+#endif
 		glDeleteShader(fs);
 		return false;
 	}
-	//printShaderInfoLog(fs, "Fragment Shader");
 	ShaderIndex = (ShaderIndex + 1) % MAX_SHADERS;
 	vs = glCreateShader(GL_VERTEX_SHADER);
 	if (fs == 0) {
-	    glDeleteShader(fs);
-	    return false;
+#ifdef SHADERDEBUG
+		printShaderInfoLog(fs, "Fragment Shader");
+#endif
+		glDeleteShader(fs);
+		return false;
 	}
 	glShaderSource(vs, 1, (const char**)&vertex_shader, NULL);
 	glCompileShader(vs);
 	glGetShaderiv(fs, GL_COMPILE_STATUS, &params);
 	if (params == GL_FALSE) {
+#ifdef SHADERDEBUG
+		printShaderInfoLog(vs, "Vertex Shader");
+#endif
 		glDeleteShader(fs);
 		glDeleteShader(vs);
 		return false;
 	}
-	//printShaderInfoLog(vs, "Vertex Shader");
 	if (glIsProgram(fullscreenShader)) {
 		glDeleteProgram(fullscreenShader);
 	}
@@ -438,6 +459,9 @@ extern bool LoadShaders() {
 	glLinkProgram(fullscreenShader);
 	glGetProgramiv(fullscreenShader, GL_LINK_STATUS, &params);
 	if (params == GL_FALSE) {
+#ifdef SHADERDEBUG
+		printProgramInfoLog(fullscreenShader, "Shader Program");
+#endif
 		glDeleteShader(fs);
 		glDeleteShader(vs);
 		glDeleteProgram(fullscreenShader);
@@ -445,7 +469,6 @@ extern bool LoadShaders() {
 	}
 	glDeleteShader(fs);
 	glDeleteShader(vs);
-	//printProgramInfoLog(fullscreenShader, "Shader Program");
 	return true;
 }