# Derivative instruction details (ddx ddy or dFdx dFdy etc.)?

This topic is 3987 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi All, From my understanding the derivative instructions ddx,ddy,fwidth etc. Use 2x2 blocks of pixels to compute the derivative. Can someone please explain: 1.) What happens on triangle boundaries where a triangle only intersects one or 2 pixels of the 2x2 region 2.) The exact formula for the derivatives for each pixel in the 2x2 block. For example, given the block: a b c d the ddx derivative for pixel a/b/c/d could be ((b-a)+(d-c))/2 or it could be b-a for a/b and d-c for c/d. I have scoured the net and haven't found a good reference for this. I have found that if I draw a box and take the derivative of a complicated calculated value that *sometimes* i get artifacts on the border of the box's polygons. I imagine this is due to how it is computed on polygon boundaries. Any help would be appreciated. Cheers! Eric [Edited by - WizardOfOzzz on January 9, 2008 8:16:57 PM]

##### Share on other sites
Quote:
 Original post by WizardOfOzzzFrom my understanding the derivative instructions ddx,ddy,fwidth etc. Use 2x2 blocks of pixels to compute the derivative.

That's correct.

Quote:
 Original post by WizardOfOzzz1.) What happens on triangle boundaries where a triangle only intersects one or 2 pixels of the 2x2 region

The shader is evaluated for all of the pixels in the 2x2 "quad", even if some of them fall outside of the triangle.

Quote:
 Original post by WizardOfOzzz2.) The exact formula for the derivatives for each pixel in the 2x2 block. For example, given the block: a b c d

IIRC the ddx(a) and ddx(b) will be (b-a), ddy(a) and ddy(c) will be (c-a) and so on.

Quote:
 Original post by WizardOfOzzzI have found that if I draw a box and take the derivative of a complicated calculated value that *sometimes* i get artifacts on the border of the box's polygons.

This problem can indeed occur if you're computing some function that is undefined or otherwise not well-behaved outside of the normal triangle interpolant range. Similar problems can occur with multi-sampling, but there you can often solve them using "centroid" interpolation. I don't recall exactly how derivatives interact with centroid interpolation but I wouldn't be surprised if it is implementation-dependent.

##### Share on other sites
Thanks a lot for your response! With a bit more testing I'm finding that they tend to have problems on triangle boundaries when I use the results from texture sampling.

I figured maybe they can compute derivatives for things like interpolants without evaluating the entire pixel shader, but if the entire shader has to be executed then they don't bother?

In any event thanks a lot for your response!

Eric

##### Share on other sites
I had some issues myself with the derivate instructions on triangle edges; I was computing the derivative of one of my interpolants. Basically, the way I got it fixed was changing the way that my interpolant was passed; instead of passing it in TEXCOORD, i passed it in COLOR. I finally found out that this works because COLOR has centroid sampling and TEXCOORD doesnt. Not sure if that bit will help you out in your case, buts thats how i fixed the artifacts on my triangle edges.

##### Share on other sites
I believe there is a suffix you can add to certain semantics to indicate you want centroid sampling, i.e. TEXCOORD_centroid. It's in the docs somewhere for sure, that's where I've seen it before.

##### Share on other sites
Thanks for all of your replies. I don't believe extrapolation should make any of my calculations undefined, but it's a good place to start looking. In one case I also need the derivatives to be accurate so centroid won't work. Thankfully in that case I'm not getting artifacts anyway.

Cheers!

Eric

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633702
• Total Posts
3013448
×