mirror of
https://github.com/love2d/love.git
synced 2026-08-16 08:11:02 +02:00
Removed unnecessary tabs from empty lines
This commit is contained in:
@@ -124,7 +124,7 @@ bool Graphics::setMode(int width, int height, bool fullscreen, bool vsync, int f
|
||||
height = currentWindow->getHeight();
|
||||
|
||||
// Okay, setup OpenGL.
|
||||
|
||||
|
||||
initializeContext();
|
||||
|
||||
// Enable blending
|
||||
|
||||
@@ -38,44 +38,44 @@ void initializeContext()
|
||||
{
|
||||
if (contextInitialized)
|
||||
return;
|
||||
|
||||
|
||||
contextInitialized = true;
|
||||
|
||||
|
||||
textureUnits.clear();
|
||||
|
||||
|
||||
// initialize multiple texture unit support, if available
|
||||
if (GLEE_ARB_multitexture || GLEE_VERSION_1_3)
|
||||
{
|
||||
GLint maxtextureunits;
|
||||
glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxtextureunits);
|
||||
|
||||
|
||||
// shaders/GL2.0 added "Texture Image Units." Total max texture units is the greater of the two
|
||||
if (GLEE_VERSION_2_0 || GLEE_ARB_vertex_shader)
|
||||
{
|
||||
GLint maxtextureimageunits;
|
||||
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxtextureimageunits);
|
||||
|
||||
|
||||
if (maxtextureimageunits > maxtextureunits)
|
||||
maxtextureunits = maxtextureimageunits;
|
||||
}
|
||||
|
||||
|
||||
textureUnits.resize(maxtextureunits, 0);
|
||||
|
||||
|
||||
GLenum activetextureunit;
|
||||
glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint *)&activetextureunit);
|
||||
|
||||
|
||||
curTextureUnitIndex = activetextureunit - GL_TEXTURE0;
|
||||
|
||||
|
||||
for (size_t i = 0; i < textureUnits.size(); ++i)
|
||||
{
|
||||
if (GLEE_VERSION_1_3)
|
||||
glActiveTexture(GL_TEXTURE0 + i);
|
||||
else
|
||||
glActiveTextureARB(GL_TEXTURE0 + i);
|
||||
|
||||
|
||||
glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint *) &textureUnits[i]);
|
||||
}
|
||||
|
||||
|
||||
if (GLEE_VERSION_1_3)
|
||||
glActiveTexture(activetextureunit);
|
||||
else
|
||||
@@ -86,7 +86,7 @@ void initializeContext()
|
||||
// multitexturing not supported, so we only have 1 texture unit
|
||||
textureUnits.resize(1, 0);
|
||||
curTextureUnitIndex = 0;
|
||||
|
||||
|
||||
glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint *) &textureUnits[0]);
|
||||
}
|
||||
}
|
||||
@@ -99,12 +99,12 @@ void uninitializeContext()
|
||||
void setActiveTextureUnit(GLenum textureunit)
|
||||
{
|
||||
initializeContext();
|
||||
|
||||
|
||||
int textureunitindex = textureunit - GL_TEXTURE0;
|
||||
|
||||
|
||||
if (textureunitindex < 0 || (size_t) textureunitindex >= textureUnits.size())
|
||||
throw love::Exception("Invalid texture unit index.");
|
||||
|
||||
|
||||
if (textureunitindex != curTextureUnitIndex)
|
||||
{
|
||||
if (GLEE_VERSION_1_3)
|
||||
@@ -114,14 +114,14 @@ void setActiveTextureUnit(GLenum textureunit)
|
||||
else
|
||||
throw love::Exception("Multitexturing not supported.");
|
||||
}
|
||||
|
||||
|
||||
curTextureUnitIndex = textureunitindex;
|
||||
}
|
||||
|
||||
void bindTexture(GLuint texture)
|
||||
{
|
||||
initializeContext();
|
||||
|
||||
|
||||
if (texture != textureUnits[curTextureUnitIndex])
|
||||
{
|
||||
textureUnits[curTextureUnitIndex] = texture;
|
||||
@@ -132,20 +132,20 @@ void bindTexture(GLuint texture)
|
||||
void bindTextureToUnit(GLuint texture, GLenum textureunit, bool restoreprev)
|
||||
{
|
||||
initializeContext();
|
||||
|
||||
|
||||
int textureunitindex = textureunit - GL_TEXTURE0;
|
||||
|
||||
|
||||
if (textureunitindex < 0 || (size_t) textureunitindex >= textureUnits.size())
|
||||
throw love::Exception("Invalid texture unit index.");
|
||||
|
||||
|
||||
if (texture != textureUnits[textureunitindex])
|
||||
{
|
||||
int oldtexunitindex = curTextureUnitIndex;
|
||||
setActiveTextureUnit(textureunit);
|
||||
|
||||
|
||||
textureUnits[textureunitindex] = texture;
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
|
||||
|
||||
if (restoreprev)
|
||||
setActiveTextureUnit(GL_TEXTURE0 + oldtexunitindex);
|
||||
}
|
||||
@@ -154,7 +154,7 @@ void bindTextureToUnit(GLuint texture, GLenum textureunit, bool restoreprev)
|
||||
void deleteTexture(GLuint texture)
|
||||
{
|
||||
initializeContext();
|
||||
|
||||
|
||||
std::vector<GLuint>::iterator it;
|
||||
for (it = textureUnits.begin(); it != textureUnits.end(); ++it)
|
||||
{
|
||||
|
||||
@@ -40,7 +40,7 @@ namespace
|
||||
{
|
||||
cureffect->attach(true);
|
||||
}
|
||||
|
||||
|
||||
~TemporaryAttacher()
|
||||
{
|
||||
if (preveffect != NULL)
|
||||
@@ -48,7 +48,7 @@ namespace
|
||||
else
|
||||
ShaderEffect::detach();
|
||||
}
|
||||
|
||||
|
||||
ShaderEffect *cureffect;
|
||||
ShaderEffect *preveffect;
|
||||
};
|
||||
@@ -66,15 +66,15 @@ ShaderEffect::ShaderEffect(const std::vector<ShaderSource> &shadersources)
|
||||
{
|
||||
if (shadersources.size() == 0)
|
||||
throw love::Exception("Cannot create shader effect: no source code!");
|
||||
|
||||
|
||||
GLint maxtextureunits;
|
||||
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxtextureunits);
|
||||
_max_texture_units = std::max(maxtextureunits - 1, 0);
|
||||
|
||||
|
||||
// initialize global texture id counters if needed
|
||||
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();
|
||||
}
|
||||
@@ -83,7 +83,7 @@ ShaderEffect::~ShaderEffect()
|
||||
{
|
||||
if (current == this)
|
||||
detach();
|
||||
|
||||
|
||||
unloadVolatile();
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ GLuint ShaderEffect::createShader(const ShaderSource &source)
|
||||
{
|
||||
GLenum shadertype;
|
||||
const char *shadertypename = NULL;
|
||||
|
||||
|
||||
switch (source.type)
|
||||
{
|
||||
case TYPE_VERTEX:
|
||||
@@ -118,47 +118,47 @@ GLuint ShaderEffect::createShader(const ShaderSource &source)
|
||||
throw love::Exception("Cannot create shader object: unknown shader type.");
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
// clear existing errors
|
||||
while (glGetError() != GL_NO_ERROR);
|
||||
|
||||
|
||||
GLuint shaderid = glCreateShader(shadertype);
|
||||
|
||||
|
||||
if (shaderid == 0) // oh no!
|
||||
{
|
||||
GLenum err = glGetError();
|
||||
|
||||
|
||||
if (err == GL_INVALID_ENUM) // invalid or unsupported shader type
|
||||
throw love::Exception("Cannot create %s shader object: %s shaders not supported.", shadertypename, shadertypename);
|
||||
else // other errors should only happen between glBegin() and glEnd()
|
||||
throw love::Exception("Cannot create %s shader object.", 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(shaderid, GL_COMPILE_STATUS, &compile_status);
|
||||
|
||||
|
||||
if (compile_status == GL_FALSE)
|
||||
{
|
||||
GLint infologlen;
|
||||
glGetShaderiv(shaderid, GL_INFO_LOG_LENGTH, &infologlen);
|
||||
|
||||
|
||||
GLchar *errorlog = new GLchar[infologlen + 1];
|
||||
glGetShaderInfoLog(shaderid, infologlen, NULL, errorlog);
|
||||
|
||||
|
||||
std::string tmp(errorlog);
|
||||
|
||||
|
||||
delete[] errorlog;
|
||||
glDeleteShader(shaderid);
|
||||
|
||||
|
||||
throw love::Exception("Cannot compile %s shader:\n%s", shadertypename, tmp.c_str());
|
||||
}
|
||||
|
||||
|
||||
return shaderid;
|
||||
}
|
||||
|
||||
@@ -167,24 +167,24 @@ 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 = shaderids.begin(); it != shaderids.end(); ++it)
|
||||
glAttachShader(_program, *it);
|
||||
|
||||
|
||||
glLinkProgram(_program);
|
||||
|
||||
|
||||
for (it = shaderids.begin(); it != shaderids.end(); ++it)
|
||||
glDeleteShader(*it); // flag shaders for auto-deletion when program object is deleted
|
||||
|
||||
|
||||
GLint link_ok;
|
||||
glGetProgramiv(_program, GL_LINK_STATUS, &link_ok);
|
||||
|
||||
|
||||
if (link_ok == GL_FALSE)
|
||||
{
|
||||
const std::string warnings = getWarnings();
|
||||
glDeleteProgram(_program);
|
||||
|
||||
|
||||
throw love::Exception("Cannot link shader program object:\n%s", warnings.c_str());
|
||||
}
|
||||
}
|
||||
@@ -194,18 +194,18 @@ bool ShaderEffect::loadVolatile()
|
||||
// zero out texture id list
|
||||
_texture_id_list.clear();
|
||||
_texture_id_list.insert(_texture_id_list.begin(), _max_texture_units, 0);
|
||||
|
||||
|
||||
std::vector<GLuint> shaderids;
|
||||
|
||||
|
||||
std::vector<ShaderSource>::const_iterator source;
|
||||
for (source = _shadersources.begin(); source != _shadersources.end(); ++source)
|
||||
shaderids.push_back(createShader(*source));
|
||||
|
||||
|
||||
if (shaderids.size() == 0)
|
||||
throw love::Exception("Cannot create shader effect: no valid source code!");
|
||||
|
||||
|
||||
createProgram(shaderids);
|
||||
|
||||
|
||||
if (current == this)
|
||||
{
|
||||
current = NULL; // make sure glUseProgram gets called
|
||||
@@ -219,25 +219,25 @@ void ShaderEffect::unloadVolatile()
|
||||
{
|
||||
if (current == this)
|
||||
glUseProgram(0);
|
||||
|
||||
|
||||
if (_program != 0)
|
||||
glDeleteProgram(_program);
|
||||
|
||||
|
||||
_program = 0;
|
||||
|
||||
|
||||
// decrement global texture id counters for texture units which had textures bound from this shader
|
||||
for (size_t i = 0; i < _texture_id_list.size(); ++i)
|
||||
{
|
||||
if (_texture_id_list[i] == 0)
|
||||
continue;
|
||||
|
||||
|
||||
_texture_id_counters[i] = std::max(_texture_id_counters[i] - 1, 0);
|
||||
}
|
||||
|
||||
|
||||
// texture list is probably invalid, clear it
|
||||
_texture_id_list.clear();
|
||||
_texture_id_list.insert(_texture_id_list.begin(), _max_texture_units, 0);
|
||||
|
||||
|
||||
// same with uniform location list
|
||||
_uniforms.clear();
|
||||
}
|
||||
@@ -283,9 +283,9 @@ void ShaderEffect::attach(bool temporary)
|
||||
{
|
||||
if (current != this)
|
||||
glUseProgram(_program);
|
||||
|
||||
|
||||
current = this;
|
||||
|
||||
|
||||
if (!temporary)
|
||||
{
|
||||
// make sure all sent textures are properly bound to their respective texture units
|
||||
@@ -294,7 +294,7 @@ void ShaderEffect::attach(bool temporary)
|
||||
{
|
||||
if (_texture_id_list[i] == 0)
|
||||
continue;
|
||||
|
||||
|
||||
bindTextureToUnit(_texture_id_list[i], GL_TEXTURE0 + i + 1, false);
|
||||
}
|
||||
setActiveTextureUnit(GL_TEXTURE0);
|
||||
@@ -305,7 +305,7 @@ void ShaderEffect::detach()
|
||||
{
|
||||
if (current != NULL)
|
||||
glUseProgram(0);
|
||||
|
||||
|
||||
current = NULL;
|
||||
}
|
||||
|
||||
@@ -372,21 +372,21 @@ void ShaderEffect::sendTexture(const std::string &name, GLuint texture)
|
||||
TemporaryAttacher attacher(this);
|
||||
GLint location = getUniformLocation(name);
|
||||
GLint texture_unit = getTextureUnit(name);
|
||||
|
||||
|
||||
// bind texture to assigned texture unit and send uniform to bound shader program
|
||||
bindTextureToUnit(texture, GL_TEXTURE0 + texture_unit, false);
|
||||
glUniform1i(location, texture_unit);
|
||||
|
||||
|
||||
// reset texture unit
|
||||
setActiveTextureUnit(GL_TEXTURE0);
|
||||
|
||||
|
||||
// increment global shader texture id counter for this texture unit, if we haven't already
|
||||
if (_texture_id_list[texture_unit-1] == 0)
|
||||
++_texture_id_counters[texture_unit-1];
|
||||
|
||||
|
||||
// store texture id so it can be re-bound to the proper texture unit when necessary
|
||||
_texture_id_list[texture_unit-1] = texture;
|
||||
|
||||
|
||||
// throw error if needed
|
||||
checkSetUniformError();
|
||||
}
|
||||
@@ -422,28 +422,28 @@ GLint ShaderEffect::getUniformLocation(const std::string &name)
|
||||
GLint ShaderEffect::getTextureUnit(const std::string &name)
|
||||
{
|
||||
std::map<std::string, GLint>::const_iterator it = _texture_unit_pool.find(name);
|
||||
|
||||
|
||||
if (it != _texture_unit_pool.end())
|
||||
return it->second;
|
||||
|
||||
|
||||
int nextunitindex = 1;
|
||||
|
||||
|
||||
// prefer texture units which are unused by all other shaders
|
||||
std::vector<int>::iterator nextfreeunit = std::find(_texture_id_counters.begin(), _texture_id_counters.end(), 0);
|
||||
|
||||
|
||||
if (nextfreeunit != _texture_id_counters.end())
|
||||
nextunitindex = std::distance(_texture_id_counters.begin(), nextfreeunit) + 1; // we don't want to use unit 0
|
||||
else
|
||||
{
|
||||
// no completely unused texture units exist, try to use next free slot in our own list
|
||||
std::vector<GLuint>::iterator nexttexunit = std::find(_texture_id_list.begin(), _texture_id_list.end(), 0);
|
||||
|
||||
|
||||
if (nexttexunit == _texture_id_list.end())
|
||||
throw love::Exception("No more texture units available for shader.");
|
||||
|
||||
|
||||
nextunitindex = std::distance(_texture_id_list.begin(), nexttexunit) + 1; // we don't want to use unit 0
|
||||
}
|
||||
|
||||
|
||||
_texture_unit_pool[name] = nextunitindex;
|
||||
return nextunitindex;
|
||||
}
|
||||
|
||||
@@ -51,14 +51,14 @@ public:
|
||||
TYPE_FRAGMENT,
|
||||
TYPE_MAX_ENUM
|
||||
};
|
||||
|
||||
|
||||
// thin wrapper for GLSL source code.
|
||||
struct ShaderSource
|
||||
{
|
||||
std::string code;
|
||||
ShaderType type;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new ShaderEffect using a list of source codes.
|
||||
* Must contain at least one vertex or fragment shader source.
|
||||
@@ -66,7 +66,7 @@ public:
|
||||
* @param shadersources Vector of shader source codes.
|
||||
**/
|
||||
ShaderEffect(const std::vector<ShaderSource> &shadersources);
|
||||
|
||||
|
||||
virtual ~ShaderEffect();
|
||||
|
||||
// Implements Volatile
|
||||
@@ -79,23 +79,23 @@ public:
|
||||
* @param temporary True if we just want to send values to the shader with no intention of rendering.
|
||||
**/
|
||||
void attach(bool temporary = false);
|
||||
|
||||
|
||||
/**
|
||||
* Detach the currently bound ShaderEffect.
|
||||
* Causes OpenGL to use fixed functionality in place of shader programs.
|
||||
**/
|
||||
static void detach();
|
||||
|
||||
|
||||
/**
|
||||
* Returns any warnings this ShaderEffect may have generated.
|
||||
**/
|
||||
std::string getWarnings() const;
|
||||
|
||||
|
||||
/**
|
||||
* Returns the maximum GLSL version supported on this system.
|
||||
**/
|
||||
static std::string getGLSLVersion();
|
||||
|
||||
|
||||
/**
|
||||
* Returns whether ShaderEffects are supported on this system.
|
||||
**/
|
||||
@@ -111,7 +111,7 @@ public:
|
||||
* @param count Number of float or vector values.
|
||||
**/
|
||||
void sendFloat(const std::string &name, int size, const GLfloat *vec, int count);
|
||||
|
||||
|
||||
/**
|
||||
* Send at least one matrix to this ShaderEffect as a uniform.
|
||||
*
|
||||
@@ -121,7 +121,7 @@ public:
|
||||
* @param count Number of matrices to send.
|
||||
**/
|
||||
void sendMatrix(const std::string &name, int size, const GLfloat *m, int count);
|
||||
|
||||
|
||||
/**
|
||||
* Send an image to this ShaderEffect as a uniform.
|
||||
*
|
||||
@@ -129,7 +129,7 @@ public:
|
||||
* @param image The image to send.
|
||||
**/
|
||||
void sendImage(const std::string &name, const Image &image);
|
||||
|
||||
|
||||
/**
|
||||
* Send a canvas to this ShaderEffect as a uniform.
|
||||
*
|
||||
@@ -137,38 +137,37 @@ public:
|
||||
* @param canvas The canvas to send.
|
||||
**/
|
||||
void sendCanvas(const std::string &name, const Canvas &canvas);
|
||||
|
||||
|
||||
// pointer to currently active ShaderEffect.
|
||||
static ShaderEffect *current;
|
||||
|
||||
private:
|
||||
|
||||
|
||||
GLint getUniformLocation(const std::string &name);
|
||||
void checkSetUniformError();
|
||||
GLuint createShader(const ShaderSource &source);
|
||||
void createProgram(const std::vector<GLuint> &shaderids);
|
||||
|
||||
|
||||
// list of all shader code attached to this ShaderEffect
|
||||
std::vector<ShaderSource> _shadersources;
|
||||
|
||||
|
||||
GLuint _program; // volatile
|
||||
|
||||
// uniform location buffer map
|
||||
std::map<std::string, GLint> _uniforms;
|
||||
|
||||
|
||||
// total max GPU texture units for shaders
|
||||
static GLint _max_texture_units;
|
||||
|
||||
|
||||
// counts total number of textures bound to each texture unit in all shaders
|
||||
static std::vector<int> _texture_id_counters;
|
||||
|
||||
|
||||
// texture unit pool for setting images
|
||||
std::map<std::string, GLint> _texture_unit_pool;
|
||||
std::vector<GLuint> _texture_id_list;
|
||||
GLint getTextureUnit(const std::string &name);
|
||||
|
||||
|
||||
void sendTexture(const std::string &name, GLuint texture);
|
||||
|
||||
};
|
||||
|
||||
} // opengl
|
||||
|
||||
@@ -444,16 +444,16 @@ int w_newShaderEffect(lua_State *L)
|
||||
lua_settop(L, 2);
|
||||
|
||||
luax_getfunction(L, "graphics", "_effectCodeToGLSL");
|
||||
|
||||
|
||||
// push vertcode and fragcode strings to the top of the stack so they become arguments for the function
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushvalue(L, 2);
|
||||
|
||||
|
||||
// call effectCodeToGLSL, returned values will be at the top of the stack
|
||||
lua_pcall(L, 2, 2, 0);
|
||||
|
||||
|
||||
std::vector<ShaderEffect::ShaderSource> shaderlist;
|
||||
|
||||
|
||||
// vertex shader code
|
||||
if (lua_isstring(L, -2))
|
||||
{
|
||||
@@ -462,7 +462,7 @@ int w_newShaderEffect(lua_State *L)
|
||||
vertshader.code = luaL_checkstring(L, -2);
|
||||
shaderlist.push_back(vertshader);
|
||||
}
|
||||
|
||||
|
||||
// fragment shader code
|
||||
if (lua_isstring(L, -1))
|
||||
{
|
||||
@@ -471,7 +471,7 @@ int w_newShaderEffect(lua_State *L)
|
||||
fragshader.code = luaL_checkstring(L, -1);
|
||||
shaderlist.push_back(fragshader);
|
||||
}
|
||||
|
||||
|
||||
ShaderEffect *effect = instance->newShaderEffect(shaderlist);
|
||||
luax_newtype(L, "ShaderEffect", GRAPHICS_SHADEREFFECT_T, (void *)effect);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user