<p>You can use a threshold set through a constant buffer to clip pixels (ie an alpha test):</p>

<pre><code>float threshold; // in constant buffer

float4 color = myTexture.Sample(...);
if (color.a &lt; threshold)
    discard;
</code></pre>

<p>Set <code>threshold</code> to 1.0 to discard all transparent fragments; set it to 0.0 to discard nothing. There will be some minor inefficency in the latter case compared to having a separate pixel shader with the alpha test removed entirely, but it shouldn't be a big deal on most GPUs nowadays.</p>

CC BY-SA 3.0
2017-10-18T20:04:57.427Z
2017-10-18T20:04:57.427Z
Nathan Reed
<p>Transparency can be achieved (or actually approximated) by using alpha-to-coverage (in case of MSAA) or basic alpha/transparency blending using the following approach:</p>

<ol>
<li>Bind a rasterizer state with a Less-Equal comparison function.</li>
<li>Bind no blend state.</li>
<li>Render opaque geometry.</li>
<li>Bind alpha-to-coverage or alpha/transparency blend state.</li>
<li>Render transparent geometry.</li>
</ol>

<p>Here, the sets of opaque and transparent geometry are mutually exclusive. The benefit of this approach is that geometry is only drawn once and the rasterization state does not need to change. Unfortunately, it could be possible to observe, through the semi-transparent holes of some double-sided object, that object itself. In this case there is no guarantee that fragments of this object would be blended with other fragments of this object, since the order in which the fragments are resolved is and should not be known.</p>

<p>Another approach:</p>

<ol>
<li>Bind a rasterizer state with a Less-Equal comparison function.</li>
<li>Bind no blend state.</li>
<li>Render all geometry and clip transparent fragments</li>
<li>Bind a rasterizer state with a Less comparison function.</li>
<li>Bind alpha-to-coverage or alpha/transparency blend state.</li>
<li>Render transparent geometry.</li>
</ol>

<p>Here, all transparent geometry is drawn twice. First, opaque fragments are processed by the pixel shader whereas transparent fragments are clipped inside the pixel shader. Second, opaque fragments will be discarded by the early-z-test of the rasterizer whereas transparent fragments are processed by the pixel shader (and blended afterwards). </p>

<p>Both approaches have some differences, but the difference that concerns me the most is the latter approach having two variants of the pixel shader: one that clips transparent fragments and one that does not clip transparent fragments. In case of multiple pixel shaders, this approach will always result in a doubling of the number of pixel shaders. Is it possible to have the benefits of the second approach while having only one variant of the pixel shader?</p>

<rendering><rasterizer><transparency><alpha>
Avoid having an opaque and transparent variant of each pixel shader
Matthias
# Avoid having an opaque and transparent variant of each pixel shader

(Asked by: username_0 on 2017-10-18)

Transparency can be achieved (or actually approximated) by using alpha-to-coverage (in case of MSAA) or basic alpha/transparency blending using the following approach:

1. Bind a rasterizer state with a Less-Equal comparison function.
2. Bind no blend state.
3. Render opaque geometry.
4. Bind alpha-to-coverage or alpha/transparency blend state.
5. Render transparent geometry.

Here, the sets of opaque and transparent geometry are mutually exclusive. The benefit of this approach is that geometry is only drawn once and the rasterization state does not need to change. Unfortunately, it could be possible to observe, through the semi-transparent holes of some double-sided object, that object itself. In this case there is no guarantee that fragments of this object would be blended with other fragments of this object, since the order in which the fragments are resolved is and should not be known.

Another approach:

1. Bind a rasterizer state with a Less-Equal comparison function.
2. Bind no blend state.
3. Render all geometry and clip transparent fragments
4. Bind a rasterizer state with a Less comparison function.
5. Bind alpha-to-coverage or alpha/transparency blend state.
6. Render transparent geometry.

Here, all transparent geometry is drawn twice. First, opaque fragments are processed by the pixel shader whereas transparent fragments are clipped inside the pixel shader. Second, opaque fragments will be discarded by the early-z-test of the rasterizer whereas transparent fragments are processed by the pixel shader (and blended afterwards).

Both approaches have some differences, but the difference that concerns me the most is the latter approach having two variants of the pixel shader: one that clips transparent fragments and one that does not clip transparent fragments. In case of multiple pixel shaders, this approach will always result in a doubling of the number of pixel shaders. Is it possible to have the benefits of the second approach while having only one variant of the pixel shader?

**Tags:** rendering, rasterizer, transparency, alpha


---

## 1 answers

### Answer by username_1 on 2017-10-18. Score: 3 | Accepted answer

You can use a threshold set through a constant buffer to clip pixels (ie an alpha test):

```
float threshold; // in constant buffer

float4 color = myTexture.Sample(...);
if (color.a < threshold)
    discard;

```

Set `threshold` to 1.0 to discard all transparent fragments; set it to 0.0 to discard nothing. There will be some minor inefficency in the latter case compared to having a separate pixel shader with the alpha test removed entirely, but it shouldn't be a big deal on most GPUs nowadays.


---

