Skip to content

Commit

Permalink
Fixed|libgui: Bind texture when uploading, added GL error assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Apr 28, 2013
1 parent 1d08a50 commit 7d96079
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 13 deletions.
1 change: 1 addition & 0 deletions doomsday/libgui/include/de/gui/glbuffer.h
Expand Up @@ -142,6 +142,7 @@ template <typename VertexType>
class GLBufferT : public GLBuffer
{
public:
typedef VertexType Type;
typedef QVector<VertexType> Vertices;

public:
Expand Down
3 changes: 2 additions & 1 deletion doomsday/libgui/include/de/gui/gluniform.h
Expand Up @@ -98,6 +98,7 @@ class LIBGUI_PUBLIC GLUniform
GLUniform &operator = (Vector4f const &vec);
GLUniform &operator = (Matrix3f const &vec);
GLUniform &operator = (Matrix4f const &vec);
GLUniform &operator = (GLTexture const &texture);
GLUniform &operator = (GLTexture const *texture);

operator dint() const { return toInt(); }
Expand All @@ -120,7 +121,7 @@ class LIBGUI_PUBLIC GLUniform
Matrix3f const &toMatrix3f() const;
Matrix4f const &toMatrix4f() const;

GLTexture *texture() const;
GLTexture const *texture() const;

/**
* Updates the value of the uniform in a particular GL program.
Expand Down
2 changes: 2 additions & 0 deletions doomsday/libgui/include/de/gui/libgui.h
Expand Up @@ -36,6 +36,8 @@
# define LIBGUI_PUBLIC
#endif

#define LIBGUI_ASSERT_GL_OK() DENG2_ASSERT(glGetError() == GL_NO_ERROR)

namespace de {

} // namespace de
Expand Down
12 changes: 9 additions & 3 deletions doomsday/libgui/src/glprogram.cpp
Expand Up @@ -154,6 +154,7 @@ DENG2_OBSERVES(GLUniform, Deletion)
for(uint i = 0; i < sizeof(names)/sizeof(names[0]); ++i)
{
glBindAttribLocation(name, GLuint(names[i].semantic), names[i].varName);
LIBGUI_ASSERT_GL_OK();
}

if(!shaders.isEmpty())
Expand Down Expand Up @@ -191,7 +192,7 @@ DENG2_OBSERVES(GLUniform, Deletion)
// Apply the uniform values in this program.
foreach(GLUniform const *u, changed)
{
if(u->type() != GLUniform::Texture2D)
if(u->type() != GLUniform::Sampler2D)
{
u->applyInProgram(self);
}
Expand All @@ -206,6 +207,7 @@ DENG2_OBSERVES(GLUniform, Deletion)
if(loc >= 0)
{
glUniform1i(loc, unit);
LIBGUI_ASSERT_GL_OK();
}
}
texturesChanged = false;
Expand Down Expand Up @@ -287,7 +289,7 @@ GLProgram &GLProgram::bind(GLUniform const &uniform)
uniform.audienceForValueChange += d.get();
uniform.audienceForDeletion += d.get();

if(uniform.type() == GLUniform::Texture2D)
if(uniform.type() == GLUniform::Sampler2D)
{
d->textures << &uniform;
d->texturesChanged = true;
Expand All @@ -306,7 +308,7 @@ GLProgram &GLProgram::unbind(GLUniform const &uniform)
uniform.audienceForValueChange -= d.get();
uniform.audienceForDeletion -= d.get();

if(uniform.type() == GLUniform::Texture2D)
if(uniform.type() == GLUniform::Sampler2D)
{
d->textures.removeOne(&uniform);
d->texturesChanged = true;
Expand All @@ -325,8 +327,12 @@ void GLProgram::beginUse() const
// The program is now ready for use.
glUseProgram(d->name);

LIBGUI_ASSERT_GL_OK();

d->updateUniforms();
d->bindTextures();

LIBGUI_ASSERT_GL_OK();
}

void GLProgram::endUse() const
Expand Down
16 changes: 15 additions & 1 deletion doomsday/libgui/src/gltexture.cpp
Expand Up @@ -138,7 +138,10 @@ DENG2_PIMPL(GLTexture)

void glBind() const
{
glBindTexture(texTarget, self.isReady()? name : 0);
DENG2_ASSERT(name != 0);

glBindTexture(texTarget, name);
LIBGUI_ASSERT_GL_OK();
}

void glUnbind() const
Expand All @@ -157,6 +160,8 @@ DENG2_PIMPL(GLTexture)
glTexParameteri(texTarget, GL_TEXTURE_MAG_FILTER, minFilter == Nearest? GL_NEAREST : GL_LINEAR);
glTexParameteri(texTarget, GL_TEXTURE_MIN_FILTER, glMinFilter(minFilter, mipFilter));

LIBGUI_ASSERT_GL_OK();

flags &= ~ParamsChanged;
}

Expand All @@ -167,6 +172,8 @@ DENG2_PIMPL(GLTexture)
glTexImage2D(isCube()? glFace(face) : texTarget,
level, glFormat.format, size.x, size.y, 0,
glFormat.format, glFormat.type, data);

LIBGUI_ASSERT_GL_OK();
}

void glSubImage(int level, Vector2i const &pos, Size const &size,
Expand All @@ -176,6 +183,8 @@ DENG2_PIMPL(GLTexture)
glTexSubImage2D(isCube()? glFace(face) : texTarget,
level, pos.x, pos.y, size.x, size.y,
glFormat.format, glFormat.type, data);

LIBGUI_ASSERT_GL_OK();
}
};

Expand Down Expand Up @@ -263,6 +272,7 @@ bool GLTexture::autoGenMips() const
void GLTexture::setUndefinedImage(GLTexture::Size const &size, Image::Format format, int level)
{
d->texTarget = GL_TEXTURE_2D;
d->size = size;

d->alloc();
d->glBind();
Expand All @@ -276,6 +286,7 @@ void GLTexture::setUndefinedImage(CubeFace face, GLTexture::Size const &size,
Image::Format format, int level)
{
d->texTarget = GL_TEXTURE_CUBE_MAP;
d->size = size;

d->alloc();
d->glBind();
Expand All @@ -288,6 +299,7 @@ void GLTexture::setUndefinedImage(CubeFace face, GLTexture::Size const &size,
void GLTexture::setImage(Image const &image, int level)
{
d->texTarget = GL_TEXTURE_2D;
d->size = image.size();

d->alloc();
d->glBind();
Expand All @@ -305,6 +317,7 @@ void GLTexture::setImage(Image const &image, int level)
void GLTexture::setImage(CubeFace face, Image const &image, int level)
{
d->texTarget = GL_TEXTURE_CUBE_MAP;
d->size = image.size();

d->alloc();
d->glBind();
Expand Down Expand Up @@ -355,6 +368,7 @@ void GLTexture::generateMipmap()
{
d->glBind();
glGenerateMipmap(d->texTarget);
LIBGUI_ASSERT_GL_OK();
d->glUnbind();

d->flags |= MipmapAvailable;
Expand Down
33 changes: 25 additions & 8 deletions doomsday/libgui/src/gluniform.cpp
Expand Up @@ -31,13 +31,13 @@ DENG2_PIMPL(GLUniform)
Block name;
Type type;
union Value {
dint int32;
duint uint32;
dfloat float32;
Vector4f *vector;
Matrix3f *mat3;
Matrix4f *mat4;
GLTexture *tex;
dint int32;
duint uint32;
dfloat float32;
Vector4f *vector;
Matrix3f *mat3;
Matrix4f *mat4;
GLTexture const *tex;
} value;

Instance(Public *i, QLatin1String const &n, Type t)
Expand Down Expand Up @@ -251,6 +251,23 @@ GLUniform &GLUniform::operator = (Matrix4f const &mat)
return *this;
}

GLUniform &GLUniform::operator = (GLTexture const &texture)
{
return *this = &texture;
}

GLUniform &GLUniform::operator = (GLTexture const *texture)
{
DENG2_ASSERT(d->type == Sampler2D);

if(d->value.tex != texture)
{
d->value.tex = texture;
d->markAsChanged();
}
return *this;
}

dint GLUniform::toInt() const
{
DENG2_ASSERT(d->type == Int || d->type == UInt || d->type == Float);
Expand Down Expand Up @@ -341,7 +358,7 @@ Matrix4f const &GLUniform::toMatrix4f() const
return *d->value.mat4;
}

GLTexture *GLUniform::texture() const
GLTexture const *GLUniform::texture() const
{
return d->value.tex;
}
Expand Down

0 comments on commit 7d96079

Please sign in to comment.