I use directx9, i can't understand the difference of tex2Dgrad and tex2D. Anyone can tell me??

**0**

# Anyone tell me the difference of tex2Dgrad and tex2D?

Started by tomwindcloud, Nov 02 2007 01:22 AM

6 replies to this topic

###
#4
Members - Reputation: **122**

Posted 02 November 2007 - 12:15 PM

Thank you,Matt Aufderheide.

I think tex2D also support mipmap.

In the doc, we can see the follow:

"tex2Dgrad(s, t, ddx, ddy),2D gradient texture lookup.This function samples a texture using the sampler specified by s, the texture coordinates in t, the x gradient in ddx, and the y gradient in ddy. The gradient values select the appropriate mipmap level of the texture for sampling."

Now my questions are:

1.ddx: On doc, "Returns the partial derivative of x with respect to the screen-space x coordinate." What's the screen-space? how to computer the the partial derivative of x.

2. "The gradient values select the appropriate mipmap level of the texture for sampling." How to do it?

I think tex2D also support mipmap.

In the doc, we can see the follow:

"tex2Dgrad(s, t, ddx, ddy),2D gradient texture lookup.This function samples a texture using the sampler specified by s, the texture coordinates in t, the x gradient in ddx, and the y gradient in ddy. The gradient values select the appropriate mipmap level of the texture for sampling."

Now my questions are:

1.ddx: On doc, "Returns the partial derivative of x with respect to the screen-space x coordinate." What's the screen-space? how to computer the the partial derivative of x.

2. "The gradient values select the appropriate mipmap level of the texture for sampling." How to do it?

###
#5
GDNet+ - Reputation: **1034**

Posted 02 November 2007 - 02:14 PM

You need to understand a bit about how graphics hardware works to understand ddx, ddy and tex2Dgrad. Graphics hardware works with 'quads' of pixels - four pixels together in a 2x2 grid. All four pixels are rendered at the same time by parallel hardware. The graphics hardware approximates the partial screen space derivative of a texture co-ordinate used for sampling a texture by taking the finite difference between adjacent pixels in the quad (ddx is the difference in the texture coordinate between a pixel and the pixel to the left or right of it in the quad and ddy between the pixel and the pixel above or below it in the quad).

This partial derivative says how fast the texture coordinate is changing in screen space and can be used to work out what mip level of the texture to sample (it will also feed into anisotropic filtering calcuations if aniso is enabled). A simple example: if you are mapping 1 texel to 1 pixel and rendering a screen aligned quad then the difference between the texture coordinate of a pixel and the adjacent pixel in the quad will be 1 / texwidth - from this the graphics card can work out it needs to use the top mip level. If you scale the quad down by half in each direction then the derivative will be twice as large and the card will know to use the next smaller mip level.

With ddx and ddy you can get those derivatives directly and they can be useful for things like anti-aliasing procedural textures. The tex2Dgrad instruction takes the derivatives as input and uses those derivatives to work out what mip to use rather than the ones from the texture coordinate being used to sample the texture. That can be useful in a variety of situations.

One common use for tex2Dgrad is when you want to sample a texture inside a dynamic branch. Because ddx and ddy are worked out from the adjacent pixels in a quad you can't do a dependent texture read inside a dynamic branch because the texture coordinate might be different or not available in the adjacent pixel if it goes down a different branch. You can work around this by explicitly calculating ddx and ddy yourself outside the branch and then using tex2Dgrad inside the branch to conditionally sample a texture.

Another use is if you want to implement texture wrapping yourself in a pixel shader - you don't want to sample using the difference between the wrapped coords but the difference using the unwrapped coords or you'll get artifacts at the wrapping point.

This partial derivative says how fast the texture coordinate is changing in screen space and can be used to work out what mip level of the texture to sample (it will also feed into anisotropic filtering calcuations if aniso is enabled). A simple example: if you are mapping 1 texel to 1 pixel and rendering a screen aligned quad then the difference between the texture coordinate of a pixel and the adjacent pixel in the quad will be 1 / texwidth - from this the graphics card can work out it needs to use the top mip level. If you scale the quad down by half in each direction then the derivative will be twice as large and the card will know to use the next smaller mip level.

With ddx and ddy you can get those derivatives directly and they can be useful for things like anti-aliasing procedural textures. The tex2Dgrad instruction takes the derivatives as input and uses those derivatives to work out what mip to use rather than the ones from the texture coordinate being used to sample the texture. That can be useful in a variety of situations.

One common use for tex2Dgrad is when you want to sample a texture inside a dynamic branch. Because ddx and ddy are worked out from the adjacent pixels in a quad you can't do a dependent texture read inside a dynamic branch because the texture coordinate might be different or not available in the adjacent pixel if it goes down a different branch. You can work around this by explicitly calculating ddx and ddy yourself outside the branch and then using tex2Dgrad inside the branch to conditionally sample a texture.

Another use is if you want to implement texture wrapping yourself in a pixel shader - you don't want to sample using the difference between the wrapped coords but the difference using the unwrapped coords or you'll get artifacts at the wrapping point.

###
#6
Members - Reputation: **121**

Posted 03 November 2007 - 12:50 AM

thanx a lot, I understand it much better now.

however, when I try this in a shader

it always choses the highest mip. what am I doing wrong? this is just an exercise, I could use tex2D here.

however, when I try this in a shader

float dx = ddx(textureUV.x);

float dy = ddy(textureUV.y);

finalColor = tex2Dgrad(defaultTexsampler, textureUV, dx, dy);

it always choses the highest mip. what am I doing wrong? this is just an exercise, I could use tex2D here.