Why is scissor test behind fragment operation?
Asked Answered
J

1

10

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.

Jaconet answered 19/11, 2015 at 15:28 Comment(2)
Why did compute fragment wich wouldnot be rasterized. The one only reason is the performance.Firer
I'm pretty certain that scissor testing will be performed before fragment shading on virtually all hardware. I think it's only ever after fragment shaders on conceptual pipeline diagrams.Wesla
K
16

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.

Katakana answered 19/11, 2015 at 23:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.