Commits

Alex Szpakowski committed e7b09e8

Changed shader source list from a vector of sources to a map of shader types to source codes

Comments (0)

Files changed (5)

src/modules/graphics/opengl/Graphics.cpp

 	return NULL; // never reached
 }
 
-ShaderEffect *Graphics::newShaderEffect(const std::vector<ShaderEffect::ShaderSource> shadersources)
+ShaderEffect *Graphics::newShaderEffect(const ShaderEffect::ShaderSources &shadersources)
 {
 	ShaderEffect *effect = NULL;
 	try

src/modules/graphics/opengl/Graphics.h

 
 	Canvas *newCanvas(int width, int height, Canvas::TextureType texture_type = Canvas::TYPE_NORMAL);
 
-	ShaderEffect *newShaderEffect(const std::vector<ShaderEffect::ShaderSource> shadersources);
+	ShaderEffect *newShaderEffect(const ShaderEffect::ShaderSources &shadersources);
 
 	/**
 	 * Sets the foreground color.

src/modules/graphics/opengl/ShaderEffect.cpp

 GLint ShaderEffect::_maxtextureunits = 0;
 std::vector<int> ShaderEffect::_texturecounters;
 
-ShaderEffect::ShaderEffect(const std::vector<ShaderSource> &shadersources)
+ShaderEffect::ShaderEffect(const ShaderSources &shadersources)
 	: _shadersources(shadersources)
 	, _program(0)
 {
-	if (shadersources.size() == 0)
+	if (shadersources.empty())
 		throw love::Exception("Cannot create shader effect: no source code!");
 
 	GLint maxtextureunits;
 	unloadVolatile();
 }
 
-GLuint ShaderEffect::createShader(const ShaderSource &source)
+GLuint ShaderEffect::createShader(const ShaderType &type, const std::string &code)
 {
 	GLenum shadertype;
 	const char *shadertypename = NULL;
 
-	switch (source.type)
+	switch (type)
 	{
 	case TYPE_VERTEX:
 		shadertype = GL_VERTEX_SHADER;
 			throw love::Exception("Cannot create %s shader object.", shadertypename);
 	}
 
-	const char *src = source.code.c_str();
-	size_t srclen = source.code.length();
+	const char *src = code.c_str();
+	size_t srclen = code.length();
 	glShaderSource(shaderid, 1, (const GLchar **)&src, (GLint *)&srclen);
 
 	glCompileShader(shaderid);
 
 	std::vector<GLuint> shaderids;
 
-	std::vector<ShaderSource>::const_iterator source;
+	ShaderSources::const_iterator source;
 	for (source = _shadersources.begin(); source != _shadersources.end(); ++source)
-		shaderids.push_back(createShader(*source));
+	{
+		GLuint shaderid = createShader(source->first, source->second);
+		shaderids.push_back(shaderid);
+	}
 
 	if (shaderids.size() == 0)
 		throw love::Exception("Cannot create shader effect: no valid source code!");

src/modules/graphics/opengl/ShaderEffect.h

 		TYPE_MAX_ENUM
 	};
 
-	// thin wrapper for GLSL source code.
-	struct ShaderSource
-	{
-		std::string code;
-		ShaderType type;
-	};
+	// type for a list of shader source codes in the form of sources[shadertype] = code
+	typedef std::map<ShaderType, std::string> ShaderSources;
 
 	/**
 	 * Creates a new ShaderEffect using a list of source codes.
 	 * Must contain at least one vertex or fragment shader source.
 	 * 
-	 * @param shadersources Vector of shader source codes.
+	 * @param shadersources map of shader source codes.
 	 **/
-	ShaderEffect(const std::vector<ShaderSource> &shadersources);
+	ShaderEffect(const ShaderSources &shadersources);
 
 	virtual ~ShaderEffect();
 
 	GLint getUniformLocation(const std::string &name);
 	void checkSetUniformError();
 	
-	GLuint createShader(const ShaderSource &source);
+	GLuint createShader(const ShaderType &type, const std::string &code);
 	void createProgram(const std::vector<GLuint> &shaderids);
 
 	GLint getTextureUnit(const std::string &name);
 	void sendTexture(const std::string &name, GLuint texture);
 
 	// list of all shader code attached to this ShaderEffect
-	std::vector<ShaderSource> _shadersources;
+	ShaderSources _shadersources;
 
 	GLuint _program; // volatile
 

src/modules/graphics/opengl/wrap_Graphics.cpp

 		// call effectCodeToGLSL, returned values will be at the top of the stack
 		lua_pcall(L, 2, 2, 0);
 
-		std::vector<ShaderEffect::ShaderSource> shaderlist;
+		ShaderEffect::ShaderSources shadersources;
 
 		// vertex shader code
 		if (lua_isstring(L, -2))
 		{
-			ShaderEffect::ShaderSource vertshader;
-			vertshader.type = ShaderEffect::TYPE_VERTEX;
-			vertshader.code = luaL_checkstring(L, -2);
-			shaderlist.push_back(vertshader);
+			std::string vertcode(luaL_checkstring(L, -2));
+			shadersources[ShaderEffect::TYPE_VERTEX] = vertcode;
 		}
 
 		// fragment shader code
 		if (lua_isstring(L, -1))
 		{
-			ShaderEffect::ShaderSource fragshader;
-			fragshader.type = ShaderEffect::TYPE_FRAGMENT;
-			fragshader.code = luaL_checkstring(L, -1);
-			shaderlist.push_back(fragshader);
+			std::string fragcode(luaL_checkstring(L, -1));
+			shadersources[ShaderEffect::TYPE_FRAGMENT] = fragcode;
 		}
 
-		ShaderEffect *effect = instance->newShaderEffect(shaderlist);
+		ShaderEffect *effect = instance->newShaderEffect(shadersources);
 		luax_newtype(L, "ShaderEffect", GRAPHICS_SHADEREFFECT_T, (void *)effect);
 	}
 	catch(const love::Exception &e)