Register
handmade.network»Forums»OpenGL issue with glScissor accross frame boundary
Simon Anciaux
855 posts
OpenGL issue with glScissor accross frame boundary
8 months, 1 week ago Edited by Simon Anciaux on Dec. 3, 2019, 6:06 p.m. Reason: emphasis
Hi,

I was writing a small test for modifications I made in some code to support glScissor calls in render batches and ran into an issue.

The test simply draws a rectangle twice with different textures (green or red) and glScissor calls.

If I write
1
2
3
4
5
6
7
// frame start
rect2 r = { 0, 0, 100, 100 };
glScissor( a );
glDraw( r );
glScissor( b );
glDraw( r );
swap_buffers( );

The result is what I expect: 2 rectangles corresponding to the scissor setups.

If I write
1
2
3
4
5
6
7
// frame start
rect2 r = { 0, 0, 100, 100 };
glDraw( r )
glScissor( b )
glDraw( r )
glScissor( a )
swap_buffers( );


I would expect a 100 x 100 rectangle and a second rectangle corresponding to the first scissor call on the first frame, than on subsequent frames the two rectangles similar to the first example.

Instead I get the first frame and second frame alternating. It's like OpenGL cached the frame buffers and doesn't see that the last glScissor call will affect the next frame rendering. If I resize the window then the rendering works as expected. I did some captures in RenderDoc and everything seems correct (events, mesh input/output, rasterizer scissor state).

Is there something I'm missing or not aware of ? I search a little bit in the OpenGL 3.3 spec but didn't find anything useful (I don't know what to search for).

At some point I changed the code that creates the rectangle to be:
1
2
3
static u32 frame = 0;
rect2 r = r2_min_size( ( r32 ) frame, 0, ( r32 ) width, ( r32 ) height );
frame++;

And it became even weirder: one of the rectangle seem to be subtracted, and the other doesn't resize unless I resize the window. Note that the application contains a Sleep( 1000 ); so you would need to wait 10 seconds before seeing a difference.

The only way I got the right result (with the rectangle moving, the code just above) was to have the scissor NOT cross frame boundaries AND disable the scissor test before swapping the buffers.

Here is the compiled exe, source and build script. The program entry point is in quick/main.c and the test code is in the #if TEST_IMAGE.

Does anyone knows what could be happening ?
Mārtiņš Možeiko
2108 posts / 1 project
OpenGL issue with glScissor accross frame boundary
8 months, 1 week ago
Are you sure the result should be same? Order matters if you have depth test disabled and triangles overlap.

Also - what about glClear? Is that not affecting your result? glClear is affected by glScissor.
Simon Anciaux
855 posts
OpenGL issue with glScissor accross frame boundary
8 months, 1 week ago
Depth testing is disabled but the 2 rectangles are drawn in the same order on each frame.

But you're correct that the problem was with glClear. I didn't pay attention that it would only clear in the current scissor box. With that in mind and the fact that we are alternating between two frame buffers, the observed result is what should be happening.

Thanks.