Commits

Alex Szpakowski committed 1ab70dc

ShaderEffects now take a dynamically sized list of source codes instead of one vertex and one fragment shader string

Comments (0)

Files changed (5)

src/modules/graphics/opengl/Graphics.cpp

 	return NULL; // never reached
 }
 
-ShaderEffect *Graphics::newShaderEffect(const std::string &vertcode, const std::string &fragcode)
+ShaderEffect *Graphics::newShaderEffect(const std::vector<ShaderEffect::ShaderSource> shadersources)
 {
 	ShaderEffect *effect = NULL;
 	try
 	{
-		effect = new ShaderEffect(vertcode, fragcode);
+		effect = new ShaderEffect(shadersources);
 	}
 	catch(love::Exception &e)
 	{

src/modules/graphics/opengl/Graphics.h

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

src/modules/graphics/opengl/ShaderEffect.cpp

 		if (preveffect != NULL)
 			preveffect->attach();
 		else
-			cureffect->detach();
+			love::graphics::opengl::ShaderEffect::detach();
 	}
 	
 	love::graphics::opengl::ShaderEffect *cureffect;
 GLint ShaderEffect::_max_texture_units = 0;
 std::vector<int> ShaderEffect::_texture_id_counters;
 
-ShaderEffect::ShaderEffect(const std::string &vertcode, const std::string &fragcode)
+ShaderEffect::ShaderEffect(const std::vector<ShaderSource> &shadersources)
 	: _program(0)
-	, _vertcode(vertcode)
-	, _fragcode(fragcode)
 {
+	if (shadersources.size() == 0)
+		throw love::Exception("Cannot create shader effect: no source code!");
+	
+	// copy shader sources from list to this ShaderEffect
+	std::vector<ShaderSource>::const_iterator it;
+	for (it = shadersources.begin(); it != shadersources.end(); ++it)
+		_shaders.push_back(*it);
+	
 	GLint maxtextureunits;
 	glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxtextureunits);
 	_max_texture_units = std::max(maxtextureunits - 1, 0);
 	if (_texture_id_counters.size() < (size_t) _max_texture_units)
 		_texture_id_counters.resize(_max_texture_units, 0);
 	
+	// load shader source and create program object
 	loadVolatile();
 }
 
 	unloadVolatile();
 }
 
-GLuint ShaderEffect::createShader(GLenum type, const std::string &code)
+GLuint ShaderEffect::createShader(const ShaderSource &source)
 {
+	GLenum shadertype;
 	const char *shadertypename = NULL;
 	
-	switch (type)
+	switch (source.type)
 	{
-	case GL_VERTEX_SHADER:
+	case TYPE_VERTEX:
+		shadertype = GL_VERTEX_SHADER;
 		shadertypename = "vertex";
 		break;
-	case GL_GEOMETRY_SHADER_ARB:
+	case TYPE_GEOMETRY:
+		shadertype = GL_GEOMETRY_SHADER_ARB;
 		shadertypename = "geometry";
-	case GL_FRAGMENT_SHADER:
+		break;
+	case TYPE_FRAGMENT:
+		shadertype = GL_FRAGMENT_SHADER;
+		shadertypename = "fragment";
+		break;
 	default:
-		shadertypename = "fragment";
+		// tesselation control and evaluation shaders aren't recognized by current version of GLee
+		throw love::Exception("Cannot create shader object: unknown shader type.");
 		break;
 	}
 	
-	GLuint shader = glCreateShader(type);
-	if (shader == 0) // should only fail when called between glBegin() and glEnd()
-		throw love::Exception("Cannot create %s shader object.", shadertypename);
+	// clear existing errors
+	while (glGetError() != GL_NO_ERROR);
 	
-	const char *src = code.c_str();
-	size_t srclen = code.length();
-	glShaderSource(shader, 1, (const GLchar **)&src, (GLint *)&srclen);
+	GLuint shaderid = glCreateShader(shadertype);
 	
-	glCompileShader(shader);
+	if (shaderid == 0) // oh no!
+	{
+		GLenum err = glGetError();
+		
+		if (err == GL_INVALID_OPERATION) // should only happen between glBegin() and glEnd()
+			throw love::Exception("Cannot create %s shader object.", shadertypename);
+		else if (err == GL_INVALID_ENUM)
+			throw love::Exception("Cannot create %s shader object: %s shaders not supported.", shadertypename, shadertypename);
+	}
+	
+	const char *src = source.code.c_str();
+	size_t srclen = source.code.length();
+	glShaderSource(shaderid, 1, (const GLchar **)&src, (GLint *)&srclen);
+	
+	glCompileShader(shaderid);
 	
 	GLint compile_status;
-	glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);
+	glGetShaderiv(shaderid, GL_COMPILE_STATUS, &compile_status);
+	
 	if (compile_status == GL_FALSE)
 	{
 		GLint infologlen;
-		glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infologlen);
+		glGetShaderiv(shaderid, GL_INFO_LOG_LENGTH, &infologlen);
 		
 		GLchar *errorlog = new GLchar[infologlen + 1];
-		glGetShaderInfoLog(shader, infologlen, NULL, errorlog);
+		glGetShaderInfoLog(shaderid, infologlen, NULL, errorlog);
 		
 		std::string tmp(errorlog);
 		
 		delete[] errorlog;
-		glDeleteShader(shader);
+		glDeleteShader(shaderid);
 		
 		throw love::Exception("Cannot compile %s shader:\n%s", shadertypename, tmp.c_str());
 	}
 	
-	return shader;
+	return shaderid;
 }
 
-void ShaderEffect::createProgram(const std::vector<GLuint> &shaders)
+void ShaderEffect::createProgram(const std::vector<GLuint> &shaderids)
 {
 	_program = glCreateProgram();
 	if (_program == 0) // should only fail when called between glBegin() and glEnd()
 		throw love::Exception("Cannot create shader program object.");
 	
 	std::vector<GLuint>::const_iterator it;
-	for (it = shaders.begin(); it != shaders.end(); ++it)
+	for (it = shaderids.begin(); it != shaderids.end(); ++it)
 		glAttachShader(_program, *it);
 	
 	glLinkProgram(_program);
 	
-	for (it = shaders.begin(); it != shaders.end(); ++it)
+	for (it = shaderids.begin(); it != shaderids.end(); ++it)
 		glDetachShader(_program, *it); // we can freely detach shaders after linking
 	
 	GLint link_ok;
 	glGetProgramiv(_program, GL_LINK_STATUS, &link_ok);
+	
 	if (link_ok == GL_FALSE)
 	{
 		const std::string warnings = getWarnings();
 	_texture_id_list.clear();
 	_texture_id_list.insert(_texture_id_list.begin(), _max_texture_units, 0);
 	
-	std::vector<GLuint> shaders;
+	std::vector<GLuint> shaderids;
 	
-	if (_vertcode.length() > 0)
-		shaders.push_back(createShader(GL_VERTEX_SHADER, _vertcode));
+	std::vector<ShaderSource>::const_iterator curshader;
+	for (curshader = _shaders.begin(); curshader != _shaders.end(); ++curshader)
+		shaderids.push_back(createShader(*curshader));
 	
-	if (_fragcode.length() > 0)
-		shaders.push_back(createShader(GL_FRAGMENT_SHADER, _fragcode));
-	
-	if (shaders.size() == 0)
-		throw love::Exception("Cannot create shader effect: no source code!");
+	if (shaderids.size() == 0)
+		throw love::Exception("Cannot create shader effect: no valid source code!");
 	
 	try
 	{
-		createProgram(shaders);
+		createProgram(shaderids);
 	}
 	catch (love::Exception &e)
 	{
 		std::vector<GLuint>::const_iterator it;
-		for (it = shaders.begin(); it != shaders.end(); ++it)
+		for (it = shaderids.begin(); it != shaderids.end(); ++it)
 			glDeleteShader(*it);
 		
 		throw;
 	}
 	
 	std::vector<GLuint>::const_iterator it;
-	for (it = shaders.begin(); it != shaders.end(); ++it)
+	for (it = shaderids.begin(); it != shaderids.end(); ++it)
 		glDeleteShader(*it);
 	
 	if (current == this)

src/modules/graphics/opengl/ShaderEffect.h

 class ShaderEffect : public Object, public Volatile
 {
 public:
-	ShaderEffect(const std::string &vertcode, const std::string &fragcode);
+	enum ShaderType
+	{
+		TYPE_VERTEX,
+		TYPE_TESSCONTROL,
+		TYPE_TESSEVAL,
+		TYPE_GEOMETRY,
+		TYPE_FRAGMENT,
+		TYPE_MAX_ENUM
+	};
+	
+	// thin wrapper for GLSL source code
+	struct ShaderSource
+	{
+		std::string code;
+		ShaderType type;
+	};
+	
+	ShaderEffect(const std::vector<ShaderSource> &shadersources);
 	virtual ~ShaderEffect();
 	std::string getWarnings() const;
 
 private:
 	GLint getUniformLocation(const std::string &name);
 	void checkSetUniformError();
-	GLuint createShader(GLenum type, const std::string &code);
+	GLuint createShader(const ShaderSource &source);
 	void createProgram(const std::vector<GLuint> &shaders);
 	
-	GLuint _program;
-	std::string _vertcode;
-	std::string _fragcode; // volatile and stuff
+	std::vector<ShaderSource> _shaders; // all shader code attached to this ShaderEffect
+	
+	GLuint _program; // volatile
 
 	// uniform location buffer
 	std::map<std::string, GLint> _uniforms;

src/modules/graphics/opengl/wrap_Graphics.cpp

 		lua_pushvalue(L, 1);
 		lua_pushvalue(L, 2);
 		
-		// call effectCodeToGLSL
+		// call effectCodeToGLSL, returned values will be at the top of the stack
 		lua_pcall(L, 2, 2, 0);
 		
-		// get returned values from the top of the stack
-		const char *vertcode = luaL_optstring(L, -2, "");
-		const char *fragcode = luaL_optstring(L, -1, "");
+		std::vector<ShaderEffect::ShaderSource> shaderlist;
 		
-		ShaderEffect *effect = instance->newShaderEffect(vertcode, fragcode);
+		// 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);
+		}
+		
+		// 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);
+		}
+		
+		ShaderEffect *effect = instance->newShaderEffect(shaderlist);
 		luax_newtype(L, "ShaderEffect", GRAPHICS_SHADEREFFECT_T, (void *)effect);
 	}
 	catch(const love::Exception &e)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.