SFML community forums

Help => Graphics => Topic started by: MadMartin on April 05, 2018, 10:58:50 pm

Title: [SOLVED|OpenGL + SFML mixing] Using multiple texture units causes STACK{OVER|UNDER}FLOW
Post by: MadMartin on April 05, 2018, 10:58:50 pm
Hey everybody,

currently I'm trying to mix OpenGL and SFML. Used the great OpenGL 3.3 tutorials at learnopengl.com to render a cube with two textures on it. That works without problems, even when using sf::RenderWindow().
The shaders use the texture units GL_TEXTURE0 and GL_TEXTURE1.

The problems arise if I also use a sf::RectangleShape besides the pure gl-calls.

My loop is like:

window.setActive(true);

glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex1_id);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, tex2_id);

glUseProgram(shader_id);

model = glm::rotate(glm::mat4(1.0f), clock.getElapsedTime().asSeconds(), glm::vec3(0.5f, 1.0f, 0.0f));
glUniformMatrix4fv(glGetUniformLocation(shader_id, "model"), 1, GL_FALSE, &model[0][0]);

glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, 0);

glUseProgram(0);

window.setActive(false);


window.pushGLStates();
window.draw(cell_shape); // sf::RectangleShape cell_shape(sf::Vector2f(15, 15));
window.popGLStates();


window.display();
 

If I order the window to pushGLStates it issues:
An internal OpenGL call failed in rendertarget.cpp(381).
Expression:
   glPushMatrix()
Error description:
   GL_STACK_OVERFLOW
   This command would cause a stack overflow.
 

And at popGLStates:
An internal OpenGL call failed in rendertarget.cpp(398).
Expression:
   glPopMatrix()
Error description:
   GL_STACK_UNDERFLOW
   This command would cause a stack underflow.
 

This only happens if I use both texture units. The error disappears if I don't bind GL_TEXTURE1. Only one currently bound texture seems to be no problem, two simultaneously bound textures are indeed a problem. It doesn't matter which tex units I use (GL_TEXTURE2, GL_TEXTURE3, ...), they all issue the errors.

Most disturbing is that everything is rendered correctly...


Any ideas?
I'm using SFML from repo, cloned five days ago from master, together with VS2017 and Win7-64.
Perhaps it's a problem with my graphics card; I only have the crappy Intel HD 5500. I have no other device for testing.
Title: Re: [OpenGL + SFML mixing] Using multiple texture units causes STACK{OVER|UNDER}FLOW
Post by: MadMartin on April 06, 2018, 11:55:17 am
Tested on a GeForce GTX 970, same problems here.

Perhaps I investigate bindless textures to remove problems with texture units probably in use by SFML.
Title: Re: [OpenGL + SFML mixing] Using multiple texture units causes STACK{OVER|UNDER}FLOW
Post by: binary1248 on April 06, 2018, 04:16:37 pm
Try calling glActiveTexture in the opposite order when cleaning up in your loop so you are left with 0 active when you are done. This reversal of order is a good habit when setting up and tearing down anything that results in residual state. Each texture unit has its own matrix stack. SFML expects to always have 0 active when it does anything. This is currently not checked in pushGLStates/popGLStates.
Title: Re: [OpenGL + SFML mixing] Using multiple texture units causes STACK{OVER|UNDER}FLOW
Post by: MadMartin on April 06, 2018, 06:58:12 pm
Thank you for the input!
Swapping the texture unbinding was exactly what solved the issue.

These binding/unbinding operations seem to adhere to some kind of stack behaviour.
I will unwind any binding in the exact opposite order  ;D


Thanks!