misc cleanup

This commit is contained in:
Alexander Szpakowski
2012-12-22 05:37:34 -04:00
parent 0c525ebb92
commit b55de47fb9
3 changed files with 24 additions and 17 deletions
+7 -3
View File
@@ -41,11 +41,13 @@ void initializeContext()
contextInitialized = true; contextInitialized = true;
// initialize multiple texture unit support, if available
if (GLEE_ARB_multitexture || GLEE_VERSION_1_3) if (GLEE_ARB_multitexture || GLEE_VERSION_1_3)
{ {
GLint maxtextureunits; GLint maxtextureunits;
glGetIntegerv(GL_MAX_TEXTURE_UNITS, &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) if (GLEE_VERSION_2_0 || GLEE_ARB_vertex_shader)
{ {
GLint maxtextureimageunits; GLint maxtextureimageunits;
@@ -55,6 +57,7 @@ void initializeContext()
maxtextureunits = maxtextureimageunits; maxtextureunits = maxtextureimageunits;
} }
textureUnits.clear();
textureUnits.resize(maxtextureunits, 0); textureUnits.resize(maxtextureunits, 0);
GLenum activetextureunit; GLenum activetextureunit;
@@ -62,7 +65,6 @@ void initializeContext()
curTextureUnitIndex = activetextureunit - GL_TEXTURE0; curTextureUnitIndex = activetextureunit - GL_TEXTURE0;
GLuint boundtexture;
for (size_t i = 0; i < textureUnits.size(); ++i) for (size_t i = 0; i < textureUnits.size(); ++i)
{ {
if (GLEE_VERSION_1_3) if (GLEE_VERSION_1_3)
@@ -70,8 +72,7 @@ void initializeContext()
else else
glActiveTextureARB(GL_TEXTURE0 + i); glActiveTextureARB(GL_TEXTURE0 + i);
glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint *) &boundtexture); glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint *) &textureUnits[i]);
textureUnits[i] = boundtexture;
} }
if (GLEE_VERSION_1_3) if (GLEE_VERSION_1_3)
@@ -82,8 +83,11 @@ void initializeContext()
else else
{ {
// multitexturing not supported so we only have 1 texture unit // multitexturing not supported so we only have 1 texture unit
textureUnits.clear();
textureUnits.resize(1, 0); textureUnits.resize(1, 0);
curTextureUnitIndex = 0; curTextureUnitIndex = 0;
glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint *) &textureUnits[0]);
} }
} }
+17 -13
View File
@@ -64,8 +64,9 @@ ShaderEffect::ShaderEffect(const std::string &vertcode, const std::string &fragc
, _vertcode(vertcode) , _vertcode(vertcode)
, _fragcode(fragcode) , _fragcode(fragcode)
{ {
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &_max_texture_units); GLint maxtextureunits;
_max_texture_units = std::max(_max_texture_units - 1, 0); glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxtextureunits);
_max_texture_units = std::max(maxtextureunits - 1, 0);
// initialize global texture id counters if needed // initialize global texture id counters if needed
if (_texture_id_counters.size() < (size_t) _max_texture_units) if (_texture_id_counters.size() < (size_t) _max_texture_units)
@@ -179,14 +180,14 @@ bool ShaderEffect::loadVolatile()
} }
catch (love::Exception &e) catch (love::Exception &e)
{ {
std::vector<GLuint>::iterator it; std::vector<GLuint>::const_iterator it;
for (it = shaders.begin(); it != shaders.end(); ++it) for (it = shaders.begin(); it != shaders.end(); ++it)
glDeleteShader(*it); glDeleteShader(*it);
throw; throw;
} }
std::vector<GLuint>::iterator it; std::vector<GLuint>::const_iterator it;
for (it = shaders.begin(); it != shaders.end(); ++it) for (it = shaders.begin(); it != shaders.end(); ++it)
glDeleteShader(*it); glDeleteShader(*it);
@@ -212,8 +213,10 @@ void ShaderEffect::unloadVolatile()
// decrement global texture id counters for texture units which had textures bound from this shader // 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) for (size_t i = 0; i < _texture_id_list.size(); ++i)
{ {
if (_texture_id_list[i] != 0) if (_texture_id_list[i] == 0)
_texture_id_counters[i] = std::max(_texture_id_counters[i] - 1, 0); continue;
_texture_id_counters[i] = std::max(_texture_id_counters[i] - 1, 0);
} }
// texture list is probably invalid, clear it // texture list is probably invalid, clear it
@@ -271,15 +274,14 @@ void ShaderEffect::attach(bool temporary)
if (!temporary) if (!temporary)
{ {
// make sure all sent textures are properly bound to their respective TIUs // make sure all sent textures are properly bound to their respective texture units
// note: list potentially contains textureids of deleted/invalid textures! // note: list potentially contains texture ids of deleted/invalid textures!
for (size_t i = 0; i < _texture_id_list.size(); ++i) for (size_t i = 0; i < _texture_id_list.size(); ++i)
{ {
if (_texture_id_list[i] != 0) if (_texture_id_list[i] == 0)
{ continue;
GLenum textureunit = GL_TEXTURE0 + i + 1;
bindTextureToUnit(_texture_id_list[i], textureunit, false); bindTextureToUnit(_texture_id_list[i], GL_TEXTURE0 + i + 1, false);
}
} }
setActiveTextureUnit(GL_TEXTURE0); setActiveTextureUnit(GL_TEXTURE0);
} }
@@ -416,12 +418,14 @@ GLint ShaderEffect::getTextureUnit(const std::string &name)
// prefer texture units which are unused by all other shaders // 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); std::vector<int>::iterator nextfreeunit = std::find(_texture_id_counters.begin(), _texture_id_counters.end(), 0);
if (nextfreeunit != _texture_id_counters.end()) if (nextfreeunit != _texture_id_counters.end())
nextunitindex = std::distance(_texture_id_counters.begin(), nextfreeunit) + 1; // we don't want to use unit 0 nextunitindex = std::distance(_texture_id_counters.begin(), nextfreeunit) + 1; // we don't want to use unit 0
else else
{ {
// no completely unused texture units exist, try to use next free slot in our own list // 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); std::vector<GLuint>::iterator nexttexunit = std::find(_texture_id_list.begin(), _texture_id_list.end(), 0);
if (nexttexunit == _texture_id_list.end()) if (nexttexunit == _texture_id_list.end())
throw love::Exception("No more texture units available for shader."); throw love::Exception("No more texture units available for shader.");
@@ -23,7 +23,6 @@
#include "wrap_Canvas.h" #include "wrap_Canvas.h"
#include <string> #include <string>
#include <iostream> #include <iostream>
using namespace std;
namespace love namespace love
{ {