Can someone clarify fwidth()

I am trying to get into antialiasing in my shaders, but I don’t know if I understand this function. I understand that dfdx() and dfdy() return the derivative (slope) of a varying parameter in screen space.

But for example, what is the use of the following which appears in a shader I’m studying:


Thanks for any help wrapping my brain around this.

It’s simply a function of what you already understand: fwidth = max(|dfdx|,|dfdy|).
Read the GLSL spec for further reference.

Yeah but I guess its the applications that I’m missing…

Well, it tells you how much a varying changes between this pixel and its neighbors (more precisely, left & top ones) in the absolute value.

For example fwidth(scene_depth) can be used for the edge detection filter (high value -> edge).

Yeah but I guess its the applications that I’m missing.

If you’re trying to emulate anisotropic filtering, it’s useful for knowing how many samples to take.

Ah ha. That’s a simple plain language explanation. But I guess only time will let me get an intuitive understanding of what can be done with it. Your example of an edge detect is helpful.

There are really powerful effects that rely on this concept. I was looking at a blog where someone was doing ambient occlusion, liquids textures, etc… For me its just about basic antialiasing at this point.

But, It’s exciting to be learning this stuff… :smiley: