Browse Source

Cleaned up some shader code internals

Alex Szpakowski 12 years ago
parent
commit
1c720afaf8
2 changed files with 31 additions and 11 deletions
  1. 21 8
      src/modules/graphics/opengl/Shader.cpp
  2. 10 3
      src/modules/graphics/opengl/Shader.h

+ 21 - 8
src/modules/graphics/opengl/Shader.cpp

@@ -97,17 +97,18 @@ Shader::~Shader()
 GLuint Shader::compileCode(ShaderType type, const std::string &code)
 GLuint Shader::compileCode(ShaderType type, const std::string &code)
 {
 {
 	GLenum glshadertype;
 	GLenum glshadertype;
-	const char *shadertypestr = NULL;
+	const char *typestr;
+
+	if (!typeNames.find(type, typestr))
+		typestr = "";
 
 
 	switch (type)
 	switch (type)
 	{
 	{
 	case TYPE_VERTEX:
 	case TYPE_VERTEX:
 		glshadertype = GL_VERTEX_SHADER;
 		glshadertype = GL_VERTEX_SHADER;
-		shadertypestr = "vertex";
 		break;
 		break;
 	case TYPE_PIXEL:
 	case TYPE_PIXEL:
 		glshadertype = GL_FRAGMENT_SHADER;
 		glshadertype = GL_FRAGMENT_SHADER;
-		shadertypestr = "pixel";
 		break;
 		break;
 	default:
 	default:
 		throw love::Exception("Cannot create shader object: unknown shader type.");
 		throw love::Exception("Cannot create shader object: unknown shader type.");
@@ -124,9 +125,9 @@ GLuint Shader::compileCode(ShaderType type, const std::string &code)
 		GLenum err = glGetError();
 		GLenum err = glGetError();
 
 
 		if (err == GL_INVALID_ENUM) // invalid or unsupported shader type
 		if (err == GL_INVALID_ENUM) // invalid or unsupported shader type
-			throw love::Exception("Cannot create %s shader object: %s shaders not supported.", shadertypestr, shadertypestr);
+			throw love::Exception("Cannot create %s shader object: %s shaders not supported.", typestr, typestr);
 		else // other errors should only happen between glBegin() and glEnd()
 		else // other errors should only happen between glBegin() and glEnd()
-			throw love::Exception("Cannot create %s shader object.", shadertypestr);
+			throw love::Exception("Cannot create %s shader object.", typestr);
 	}
 	}
 
 
 	const char *src = code.c_str();
 	const char *src = code.c_str();
@@ -144,7 +145,7 @@ GLuint Shader::compileCode(ShaderType type, const std::string &code)
 
 
 	// Save any warnings for later querying
 	// Save any warnings for later querying
 	if (infologlen > 0)
 	if (infologlen > 0)
-		shaderWarnings[type] = shadertypestr + std::string(" shader:\n") + infolog;
+		shaderWarnings[type] = infolog;
 
 
 	delete[] infolog;
 	delete[] infolog;
 
 
@@ -154,7 +155,7 @@ GLuint Shader::compileCode(ShaderType type, const std::string &code)
 	if (status == GL_FALSE)
 	if (status == GL_FALSE)
 	{
 	{
 		throw love::Exception("Cannot compile %s shader code:\n%s",
 		throw love::Exception("Cannot compile %s shader code:\n%s",
-		                      shadertypestr, shaderWarnings[type].c_str());
+		                      typestr, shaderWarnings[type].c_str());
 	}
 	}
 
 
 	return shaderid;
 	return shaderid;
@@ -263,11 +264,15 @@ std::string Shader::getProgramWarnings() const
 std::string Shader::getWarnings() const
 std::string Shader::getWarnings() const
 {
 {
 	std::string warnings;
 	std::string warnings;
+	const char *typestr;
 
 
 	// Get the individual shader stage warnings
 	// Get the individual shader stage warnings
 	std::map<ShaderType, std::string>::const_iterator it;
 	std::map<ShaderType, std::string>::const_iterator it;
 	for (it = shaderWarnings.begin(); it != shaderWarnings.end(); ++it)
 	for (it = shaderWarnings.begin(); it != shaderWarnings.end(); ++it)
-		warnings += it->second;
+	{
+		if (typeNames.find(it->first, typestr))
+			warnings += std::string(typestr) + std::string(" shader:\n") + it->second;
+	}
 
 
 	warnings += getProgramWarnings();
 	warnings += getProgramWarnings();
 
 
@@ -476,6 +481,14 @@ bool Shader::isSupported()
 	return GLEE_VERSION_2_0 && getGLSLVersion() >= "1.2";
 	return GLEE_VERSION_2_0 && getGLSLVersion() >= "1.2";
 }
 }
 
 
+StringMap<Shader::ShaderType, Shader::TYPE_MAX_ENUM>::Entry Shader::typeNameEntries[] =
+{
+	{"vertex", Shader::TYPE_VERTEX},
+	{"pixel", Shader::TYPE_PIXEL},
+};
+
+StringMap<Shader::ShaderType, Shader::TYPE_MAX_ENUM> Shader::typeNames(Shader::typeNameEntries, sizeof(Shader::typeNameEntries));
+
 } // opengl
 } // opengl
 } // graphics
 } // graphics
 } // love
 } // love

+ 10 - 3
src/modules/graphics/opengl/Shader.h

@@ -21,14 +21,18 @@
 #ifndef LOVE_GRAPHICS_SHADER_H
 #ifndef LOVE_GRAPHICS_SHADER_H
 #define LOVE_GRAPHICS_SHADER_H
 #define LOVE_GRAPHICS_SHADER_H
 
 
+// LOVE
 #include "common/Object.h"
 #include "common/Object.h"
-#include <string>
-#include <map>
-#include <vector>
+#include "common/StringMap.h"
 #include "OpenGL.h"
 #include "OpenGL.h"
 #include "Image.h"
 #include "Image.h"
 #include "Canvas.h"
 #include "Canvas.h"
 
 
+// STL
+#include <string>
+#include <map>
+#include <vector>
+
 namespace love
 namespace love
 {
 {
 namespace graphics
 namespace graphics
@@ -156,6 +160,9 @@ private:
 
 
 	// Counts total number of textures bound to each texture unit in all shaders
 	// Counts total number of textures bound to each texture unit in all shaders
 	static std::vector<int> textureCounters;
 	static std::vector<int> textureCounters;
+
+	static StringMap<ShaderType, TYPE_MAX_ENUM>::Entry typeNameEntries[];
+	static StringMap<ShaderType, TYPE_MAX_ENUM> typeNames;
 };
 };
 
 
 } // opengl
 } // opengl