Use of discard and return

Looking in the OpenGL shading language reference, I can’t find the definition of “discard”.

I think it will cancel output from the fragment shader. But will it also cancel execution? Or do you need to explicitely call “return” as well?

[QUOTE=Kopelrativ;1241913]Looking in the OpenGL shading language reference, I can’t find the definition of “discard”.

I think it will cancel output from the fragment shader. But will it also cancel execution? Or do you need to explicitely call “return” as well?[/QUOTE]

You do need to call return as well to cancel execution. IIRC, AMD drivers abandon shader execution upon discard, whilst nVidia drivers don’t. The OpenGL spec does not define a specific behavior.

Looking in the OpenGL shading language reference, I can’t find the definition of “discard”.

It’s on page 109 (115 in PDF numbering).

Whether discard “cancels execution” depends entirely on the hardware. In general, execution will only be canceled if an entire pixel group (the size of which depends on hardware) all execute a discard. So if you’re trying to discard to avoid expensive computations, it may or may not help, depending on the spatial locality of the discards.

“The discard keyword is only allowed within fragment shaders. It can be used within a fragment shader to abandon the operation on the current fragment. This keyword causes the fragment to be discarded and no updates to any buffers will occur. Control flow exits the shader, and subsequent implicit or explicit derivatives are undefined when this exit is non-uniform.”

Seems clear enough; you should not need to call return. Even if this does not really cancel execution for that fragment, it must logically act like it.

I had started a thread about this issue a year ago, but this didn’t go very far
http://www.opengl.org/discussion_boards/showthread.php/176164-Probable-nVidia-GLSL-compiler-bug

[QUOTE=Alfonse Reinheart;1241919]It’s on page 109 (115 in PDF numbering).
[/QUOTE]
Found it on page 107 (113 PDF).

I used the search function for the word “discard” when displaying the document in Google Chrome, which did not turn up this reference. When I downloaded the document, I could search as expected using Acrobat PDF reader. A little worrying, I have depended on this search a lot.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.