<p>You can use a stencil instead, This allows the gpu to do an early out test before the fragment shader even starts.</p>

<p>You only have to build the stencil once (possibly with a discard in a simple shader) and you can reuse the stencil in future draws.</p>

CC BY-SA 4.0
2018-06-11T09:20:13.830Z
oh right. that is even better and a pretty simple soltuion, thank you.
Tare
CC BY-SA 4.0
2018-06-11T09:13:04.763Z
2018-06-11T09:13:04.763Z
ratchet freak
<p>When a simulating a camera, I may run into the situation that a black elliptic border is needed (rather than a rectangular image).
<a href="https://i.sstatic.net/E09UT.png" rel="noreferrer"><img src="https://i.sstatic.net/E09UT.png" alt="enter image description here"></a></p>

<p>In this case I have the option to discard the fragment in the fragment shader or draw it with a black color. Discarding obviously needs branching, so now I am not sure which option is faster. Generally I assume avoiding branching in shaders is the way to go, but does that hold true for discarding, especially if the branch contains only one call (the discard one)? Or is there no rule of thumb for this case at all and just benchmarking is the way to go?</p>

<glsl><optimization>
Is a branched discard to be avoided
Tare
# Is a branched discard to be avoided

(Asked by: username_0 on 2018-06-11)

When a simulating a camera, I may run into the situation that a black elliptic border is needed (rather than a rectangular image).
[![enter image description here](https://i.sstatic.net/E09UT.png)](https://i.sstatic.net/E09UT.png)

In this case I have the option to discard the fragment in the fragment shader or draw it with a black color. Discarding obviously needs branching, so now I am not sure which option is faster. Generally I assume avoiding branching in shaders is the way to go, but does that hold true for discarding, especially if the branch contains only one call (the discard one)? Or is there no rule of thumb for this case at all and just benchmarking is the way to go?

**Tags:** glsl, optimization


---

## 1 answers

### Answer by username_1 on 2018-06-11. Score: 7 | Accepted answer

You can use a stencil instead, This allows the gpu to do an early out test before the fragment shader even starts.

You only have to build the stencil once (possibly with a discard in a simple shader) and you can reuse the stencil in future draws.

> **Comments:**
> **username_0**: oh right. that is even better and a pretty simple soltuion, thank you.  

---

