<p>In entirely technical terms, <code>fwidth(p)</code> is defined as</p>

<pre><code>fwidth(p) := abs(dFdx(p)) + abs(dFdy(p))
</code></pre>

<p>And <code>dFdx(p)</code>/<code>dFdy(p)</code> are the partial derivates of the value <code>p</code> with respect to the <code>x</code> and <code>y</code> screen dimensions. So they denote how the value of <code>p</code> behaves when going one pixel to the right (<code>x</code>) or one pixel up (<code>y</code>).</p>

<p>Now how can they be practically computed? Well, if you know the neighbour pixels' values for <code>p</code>, you can just compute those derivates as direct <strong>finite differences</strong> as an approximation for their actual mathematical derivatives (which might not have an exact analytical solution at all):</p>

<pre><code>dFdx(p) := p(x+1) - p(x)
</code></pre>

<p>But of course now you may ask, how do we even know the values of <code>p</code> (which could afterall be any arbitrarily computed value inside the shader program) for the neighbouring pixels? How do we compute those values without incurring major overhead by doing the whole shader computation two (or three) times?</p>

<p>Well, you know what, those neighbouring values are computed anyway, since for the neighbouring pixel you also run a fragment shader. So all that you need is access to this neighbouring fragment shader invocation when run for the neighbouring pixel. But it's even easier, because those neighbouring values are also computed at the exact same time.</p>

<p>Modern rasterizers call fragment shaders in larger tiles of more than one neighbouring pixels. At the smallest those would be a 2x2 grid of pixels. And for each such a pixel block the fragment shader is invoked for each pixel and those invocations run <strong>in perfectly parallel lock-step</strong> so that all computations are done in the exact same order and at the exact same time for each of those pixels in the block (which is also why branching in the fragment shader, while not deadly, should be avoided if possible, since each invocation of a block would have to explore every branch that is taken by at least one of the invocations, even if it just throws away the results afterwards, as also adressed in the answers to <a href="https://computergraphics.stackexchange.com/q/259/6">this related question</a>). So at any moment, a fragment shader theoretically has access to its neighbouring pixels' fragment shader values. And while you don't have direct access to those values, you have access to values computed from them, like the derivative functions <code>dFdx</code>, <code>dFdy</code>, <code>fwidth</code>, ...</p>

CC BY-SA 3.0
2015-08-05T15:13:45.657Z
2015-08-18T12:17:27.583Z
2017-04-13T13:00:43.660Z
Christian Rau
<p>Pixel screen-space derivatives <em>do</em> drastically impact performance, but they impact performance whether you use them or not, so from a certain point of view they're free!</p>

<p>Every GPU in recent history packs a quad of four pixels together and puts them in the same warp/wavefront, which essentially means that they're running right next to each other on the GPU, so accessing values from them is very cheap. Because warps/wavefronts are run in lockstep, the other pixels will also be at exactly the same place in the shader as you are, so the value of <code>p</code> for those pixels will just be sitting in a register waiting for you. These other three pixels will <em>always</em> be executed, even if their results will be thrown away. So a triangle that covers a single pixel will always shade four pixels and throw away the results of three of them, just so that these derivative features work!</p>

<p>This is considered an acceptable cost (for current hardware) because it isn't just functions like <code>fwidth</code> that use these derivatives: every single texture sample does as well, in order to pick what mipmap of your texture to read from. Consider: if you are very close to a surface, the UV coordinate you are using to sample the texture will have a very small derivative in screen space, meaning you need to use a larger mipmap, and if you are farther the UV coordinate will have a larger derivative in screen space, meaning you need to use a smaller mipmap.</p>

<p>As far as what it means in less mathematical terms: <code>fwidth</code> is equivalent to <code>abs(dFdx(p)) + abs(dFdy(p))</code>. <code>dFdx(p)</code> is simply the difference between the value of <code>p</code> at pixel x+1 and the value of <code>p</code> at pixel x, and similarly for <code>dFdy(p)</code>.</p>

CC BY-SA 3.0
2015-08-05T15:44:31.643Z
So, if `dFdx(p) = p(x1) - p(x)`, then `x1` can be either `(x+1)` or `(x-1)`, depending on the position of pixel `x` in the quad. Either way, `x1` has to be in the same warp/wavefront as `x`. Am I correct?
ApoorvaJ
CC BY-SA 3.0
2015-08-05T15:46:43.313Z
@ApoorvaJ Essentially the same value for `dFdx` is computed for each of the 2 neighbouring pixels in the 2x2 grid. And this value is just computed using the difference between the two neighbor values, if that is `p(x+1)-p(x)` or `p(x)-p(x-1)` just depends on your notion of what `x` is exactly here. The result is the same however. So yeah, you're correct.
Christian Rau
CC BY-SA 3.0
2015-08-05T15:10:50.933Z
2015-08-05T15:10:50.933Z
John Calsbeek
<p>The <a href="https://www.opengl.org/sdk/docs/man/html/fwidth.xhtml">OpenGL documentation</a> states that fwidth <code>returns the sum of the absolute value of derivatives in x and y</code>.</p>

<p>What does this mean in less mathematical terms, and is there a way to visualize it?</p>

<p>Based on my understanding of the function, <code>fwidth(p)</code> has access to the value of <code>p</code> in neighboring pixels. How does this work on the GPU without drastically impacting performance, and does it work reliably and uniformly across all pixels?</p>

<opengl>
What is fwidth and how does it work?
ApoorvaJ
# What is fwidth and how does it work?

(Asked by: username_0 on 2015-08-05)

The [OpenGL documentation](https://www.opengl.org/sdk/docs/man/html/fwidth.xhtml) states that fwidth `returns the sum of the absolute value of derivatives in x and y`.

What does this mean in less mathematical terms, and is there a way to visualize it?

Based on my understanding of the function, `fwidth(p)` has access to the value of `p` in neighboring pixels. How does this work on the GPU without drastically impacting performance, and does it work reliably and uniformly across all pixels?

**Tags:** opengl


---

## 2 answers

### Answer by username_2 on 2015-08-05. Score: 26 | Accepted answer

Pixel screen-space derivatives *do* drastically impact performance, but they impact performance whether you use them or not, so from a certain point of view they're free!

Every GPU in recent history packs a quad of four pixels together and puts them in the same warp/wavefront, which essentially means that they're running right next to each other on the GPU, so accessing values from them is very cheap. Because warps/wavefronts are run in lockstep, the other pixels will also be at exactly the same place in the shader as you are, so the value of `p` for those pixels will just be sitting in a register waiting for you. These other three pixels will *always* be executed, even if their results will be thrown away. So a triangle that covers a single pixel will always shade four pixels and throw away the results of three of them, just so that these derivative features work!

This is considered an acceptable cost (for current hardware) because it isn't just functions like `fwidth` that use these derivatives: every single texture sample does as well, in order to pick what mipmap of your texture to read from. Consider: if you are very close to a surface, the UV coordinate you are using to sample the texture will have a very small derivative in screen space, meaning you need to use a larger mipmap, and if you are farther the UV coordinate will have a larger derivative in screen space, meaning you need to use a smaller mipmap.

As far as what it means in less mathematical terms: `fwidth` is equivalent to `abs(dFdx(p)) + abs(dFdy(p))`. `dFdx(p)` is simply the difference between the value of `p` at pixel x+1 and the value of `p` at pixel x, and similarly for `dFdy(p)`.

> **Comments:**
> **username_0**: So, if `dFdx(p) = p(x1) - p(x)`, then `x1` can be either `(x+1)` or `(x-1)`, depending on the position of pixel `x` in the quad. Either way, `x1` has to be in the same warp/wavefront as `x`. Am I correct?  
> **username_1**: @username_0 Essentially the same value for `dFdx` is computed for each of the 2 neighbouring pixels in the 2x2 grid. And this value is just computed using the difference between the two neighbor values, if that is `p(x+1)-p(x)` or `p(x)-p(x-1)` just depends on your notion of what `x` is exactly here. The result is the same however. So yeah, you're correct.  

---

### Answer by username_1 on 2015-08-05. Score: 12

In entirely technical terms, `fwidth(p)` is defined as

```
fwidth(p) := abs(dFdx(p)) + abs(dFdy(p))

```

And `dFdx(p)`/`dFdy(p)` are the partial derivates of the value `p` with respect to the `x` and `y` screen dimensions. So they denote how the value of `p` behaves when going one pixel to the right (`x`) or one pixel up (`y`).

Now how can they be practically computed? Well, if you know the neighbour pixels' values for `p`, you can just compute those derivates as direct **finite differences** as an approximation for their actual mathematical derivatives (which might not have an exact analytical solution at all):

```
dFdx(p) := p(x+1) - p(x)

```

But of course now you may ask, how do we even know the values of `p` (which could afterall be any arbitrarily computed value inside the shader program) for the neighbouring pixels? How do we compute those values without incurring major overhead by doing the whole shader computation two (or three) times?

Well, you know what, those neighbouring values are computed anyway, since for the neighbouring pixel you also run a fragment shader. So all that you need is access to this neighbouring fragment shader invocation when run for the neighbouring pixel. But it's even easier, because those neighbouring values are also computed at the exact same time.

Modern rasterizers call fragment shaders in larger tiles of more than one neighbouring pixels. At the smallest those would be a 2x2 grid of pixels. And for each such a pixel block the fragment shader is invoked for each pixel and those invocations run **in perfectly parallel lock-step** so that all computations are done in the exact same order and at the exact same time for each of those pixels in the block (which is also why branching in the fragment shader, while not deadly, should be avoided if possible, since each invocation of a block would have to explore every branch that is taken by at least one of the invocations, even if it just throws away the results afterwards, as also adressed in the answers to [this related question](https://computergraphics.stackexchange.com/q/259/6)). So at any moment, a fragment shader theoretically has access to its neighbouring pixels' fragment shader values. And while you don't have direct access to those values, you have access to values computed from them, like the derivative functions `dFdx`, `dFdy`, `fwidth`, ...


---

