Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is scissor test behind fragment operation?

If I understand correctly, scissor test is per fragment operation, but I was wondering if it's possible to put the test before fragment shader so that fragments outside scissor box don't need to be shaded, or even before rasterizer? The only reason of not doing so I can think of is to scissor the clear color.

like image 622
painkiller Avatar asked Nov 19 '15 15:11

painkiller


People also ask

Why depth test after Fragment Shader?

The Depth Test is a per-sample processing operation performed after the Fragment Shader (and sometimes before). The Fragment's output depth value may be tested against the depth of the sample being written to. If the test fails, the fragment is discarded.

What is the scissor test?

The Scissor Test is a Per-Sample Processing operation that discards Fragments that fall outside of a certain rectangular portion of the screen. To activate the scissor test, first enable the GL_SCISSOR_TEST enumerator. Once enabled, pixels outside of the scissor box will be discarded.

What is per fragment operations?

The per-fragment operations are part of the OpenGL (ES) 2.0 pipeline and consist of a series of tests and operations that can modify the fragment color generated by the fragment shader before it is written to the corresponding pixel in the framebuffer. If any of the tests fails, the fragment will be discarded.

Where fragments output from Fragment Shader are processed?

Per-Sample Processing is a stage of the OpenGL rendering pipeline, where Fragments output from a Fragment Shader are processed, and their resulting data are written to various buffers.


1 Answers

The scissor test will in almost any real-world scenario affect the rasterization itself. The GPU's rasterizer units will not produce fragments outside of the scissor rect. When the OpenGL pipeline was first created, the fragment shader didn't exist. Stuff like texture mapping is considered part of the rasterization stage in earlier versions of the GL spec.

However, this conceptual pipeline is not what actual HW implements. And this poses no problem as long as the final result will not be changed by the deviating implementation.

You will typically see that even more importantly than scissor test, depth test will also be carried out before the fragment shader is invoked ("early Z"). This will work as long as the fragment shader does not modify the depth value of the fragments. Typically, the implementation will automatically enable early Z behind your back as long as there is no assignment to gl_FragDepth in the fragment shader.

Modern versions of the GL specification explicitly mention the "early" tests. Section 14.9 "Early Per-Fragment Tests" of the OpenGL 4.5 core profile specification states (emphasis mine):

Once fragments are produced by rasterization, a number of per-fragment operations may be performed prior to fragment shader execution (see section 15). If a fragment is discarded during any of these operations, it will not be processed by any subsequent stage, including fragment shader execution. Up to five operations are performed on each fragment, in the following order:

  • the pixel ownership test (see section 17.3.1);
  • the scissor test (see section 17.3.2);
  • the stencil test (see section 17.3.5);
  • the depth buffer test (see section 17.3.6); and
  • occlusion query sample counting (see section 17.3.7).

The pixel ownership and scissor tests are always performed. The other operations are performed if and only if early fragment tests are enabled in the active fragment shader (see section 15.2). [...]

So actually, the "late" scissor test doesn't really exist in the GL any more, even if the pipeline diagram in the very same document still lists it after the fragment shader.

like image 199
derhass Avatar answered Nov 13 '22 16:11

derhass