OpenGL: clean up glActiveTexture usage

Removed some of my earlier glActiveTexture calls. After reviewing the
code I now trust that GL_TEXTURE0 is active by default. Fewer GL calls,
same results.

Fixed some misuse of glActiveTexture & glUniformi, mostly my fault.
Caught by --debug-gpu on Windows. Don't know why this appeared to be
working previously!

Plus some easy cleanup nearby.
This commit is contained in:
2016-10-25 01:02:41 -04:00
parent 4d11b2fb91
commit c5072941c3
5 changed files with 38 additions and 48 deletions

View File

@@ -652,12 +652,10 @@ static void draw_empty_image(Object *ob, const short dflag, const unsigned char
const float ob_alpha = ob->col[3];
float width, height;
const int texUnit = GL_TEXTURE0;
int bindcode = 0;
if (ima) {
if (ob_alpha > 0.0f) {
glActiveTexture(texUnit);
bindcode = GPU_verify_image(ima, ob->iuser, GL_TEXTURE_2D, 0, false, false, false);
/* don't bother drawing the image if alpha = 0 */
}
@@ -694,7 +692,7 @@ static void draw_empty_image(Object *ob, const short dflag, const unsigned char
unsigned texCoord = add_attrib(format, "texCoord", GL_FLOAT, 2, KEEP_FLOAT);
immBindBuiltinProgram(GPU_SHADER_3D_IMAGE_MODULATE_ALPHA);
immUniform1f("alpha", ob_alpha);
immUniform1i("image", texUnit);
immUniform1i("image", 0); /* default GL_TEXTURE0 unit */
immBegin(GL_TRIANGLE_FAN, 4);
immAttrib2f(texCoord, 0.0f, 0.0f);

View File

@@ -578,7 +578,6 @@ void GPU_shader_geometry_stage_primitive_io(GPUShader *shader, int input, int ou
void GPU_shader_uniform_texture(GPUShader *UNUSED(shader), int location, GPUTexture *tex)
{
GLenum arbnumber;
int number = GPU_texture_bound_number(tex);
int bindcode = GPU_texture_opengl_bindcode(tex);
int target = GPU_texture_target(tex);
@@ -594,16 +593,18 @@ void GPU_shader_uniform_texture(GPUShader *UNUSED(shader), int location, GPUText
if (location == -1)
return;
arbnumber = (GLenum)((GLuint)GL_TEXTURE0 + number);
if (number != 0)
glActiveTexture(GL_TEXTURE0 + number);
if (number != 0) glActiveTexture(arbnumber);
if (bindcode != 0)
glBindTexture(target, bindcode);
else
GPU_invalid_tex_bind(target);
glUniform1i(location, number);
glEnable(target);
if (number != 0) glActiveTexture(GL_TEXTURE0);
if (number != 0)
glActiveTexture(GL_TEXTURE0);
}
int GPU_shader_get_attribute(GPUShader *shader, const char *name)

View File

@@ -616,15 +616,18 @@ void GPU_texture_bind(GPUTexture *tex, int number)
if (number < 0)
return;
GLenum arbnumber = (GLenum)((GLuint)GL_TEXTURE0 + number);
if (number != 0) glActiveTexture(arbnumber);
if (tex->bindcode != 0) {
if (number != 0)
glActiveTexture(GL_TEXTURE0 + number);
if (tex->bindcode != 0)
glBindTexture(tex->target_base, tex->bindcode);
}
else
GPU_invalid_tex_bind(tex->target_base);
glEnable(tex->target_base);
if (number != 0) glActiveTexture(GL_TEXTURE0);
glEnable(tex->target_base); /* TODO: remove this line once we're using GLSL everywhere */
if (number != 0)
glActiveTexture(GL_TEXTURE0);
tex->number = number;
}
@@ -639,11 +642,14 @@ void GPU_texture_unbind(GPUTexture *tex)
if (tex->number == -1)
return;
GLenum arbnumber = (GLenum)((GLuint)GL_TEXTURE0 + tex->number);
if (tex->number != 0) glActiveTexture(arbnumber);
if (tex->number != 0)
glActiveTexture(GL_TEXTURE0 + tex->number);
glBindTexture(tex->target_base, 0);
glDisable(tex->target_base);
if (tex->number != 0) glActiveTexture(GL_TEXTURE0);
glDisable(tex->target_base); /* TODO: remove this line */
if (tex->number != 0)
glActiveTexture(GL_TEXTURE0);
tex->number = -1;
}
@@ -663,25 +669,19 @@ void GPU_texture_filter_mode(GPUTexture *tex, bool compare, bool use_filter)
if (tex->number == -1)
return;
GLenum arbnumber = (GLenum)((GLuint)GL_TEXTURE0 + tex->number);
if (tex->number != 0) glActiveTexture(arbnumber);
if (tex->number != 0)
glActiveTexture(GL_TEXTURE0 + tex->number);
if (tex->depth) {
if (compare)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
else
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE);
}
if (use_filter) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
if (tex->number != 0) glActiveTexture(GL_TEXTURE0);
GLenum filter = use_filter ? GL_LINEAR : GL_NEAREST;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
if (tex->number != 0)
glActiveTexture(GL_TEXTURE0);
}
void GPU_texture_free(GPUTexture *tex)

View File

@@ -89,9 +89,6 @@ void GPU_viewport_debug_depth_draw(GPUViewport *viewport, const float znear, con
const float w = (float)GPU_texture_width(viewport->debug_depth);
const float h = (float)GPU_texture_height(viewport->debug_depth);
const int activeTex = GL_TEXTURE0;
glActiveTexture(activeTex);
VertexFormat *format = immVertexFormat();
unsigned texcoord = add_attrib(format, "texCoord", GL_FLOAT, 2, KEEP_FLOAT);
unsigned pos = add_attrib(format, "pos", GL_FLOAT, 2, KEEP_FLOAT);
@@ -102,7 +99,7 @@ void GPU_viewport_debug_depth_draw(GPUViewport *viewport, const float znear, con
immUniform1f("znear", znear);
immUniform1f("zfar", zfar);
immUniform1i("image", activeTex);
immUniform1i("image", 0); /* default GL_TEXTURE0 unit */
immBegin(GL_QUADS, 4);

View File

@@ -173,9 +173,6 @@ static void wm_method_draw_stereo3d_sidebyside(wmWindow *win)
int soffx;
bool cross_eyed = (win->stereo3d_format->flag & S3D_SIDEBYSIDE_CROSSEYED) != 0;
const int activeTex = GL_TEXTURE0;
glActiveTexture(activeTex);
VertexFormat *format = immVertexFormat();
unsigned texcoord = add_attrib(format, "texCoord", GL_FLOAT, 2, KEEP_FLOAT);
unsigned pos = add_attrib(format, "pos", GL_FLOAT, 2, KEEP_FLOAT);
@@ -217,7 +214,7 @@ static void wm_method_draw_stereo3d_sidebyside(wmWindow *win)
glBindTexture(triple->target, triple->bind);
immUniform1f("alpha", 1.0f);
immUniform1i("image", activeTex);
immUniform1i("image", 0); /* default GL_TEXTURE0 unit */
immBegin(GL_QUADS, 4);
@@ -249,9 +246,6 @@ static void wm_method_draw_stereo3d_topbottom(wmWindow *win)
int view;
int soffy;
const int activeTex = GL_TEXTURE0;
glActiveTexture(activeTex);
VertexFormat *format = immVertexFormat();
unsigned texcoord = add_attrib(format, "texCoord", GL_FLOAT, 2, KEEP_FLOAT);
unsigned pos = add_attrib(format, "pos", GL_FLOAT, 2, KEEP_FLOAT);
@@ -290,7 +284,7 @@ static void wm_method_draw_stereo3d_topbottom(wmWindow *win)
glBindTexture(triple->target, triple->bind);
immUniform1f("alpha", 1.0f);
immUniform1i("image", activeTex);
immUniform1i("image", 0); /* default GL_TEXTURE0 unit */
immBegin(GL_QUADS, 4);
@@ -339,9 +333,9 @@ void wm_method_draw_stereo3d(const bContext *UNUSED(C), wmWindow *win)
static bool wm_stereo3d_quadbuffer_supported(void)
{
int gl_stereo = 0;
glGetBooleanv(GL_STEREO, (GLboolean *)&gl_stereo);
return gl_stereo != 0;
GLboolean stereo = GL_FALSE;
glGetBooleanv(GL_STEREO, &stereo);
return stereo == GL_TRUE;
}
static bool wm_stereo3d_is_fullscreen_required(eStereoDisplayMode stereo_display)