Anyone tell me the difference of tex2Dgrad and tex2D?

Started by
5 comments, last by mattnewport 16 years, 5 months ago
I use directx9, i can't understand the difference of tex2Dgrad and tex2D. Anyone can tell me??
Advertisement
tex2Dgrad was introduced in ps_2.0 where you can determine the LOD for mipmapping and filtering you cant use this function in ps_1.0.

tex2D was a ps_1.0 function which i think got lately also support for LOD ;)
So what's the difference between tex2Dgrad and tex2Dlod?

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?
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.

Game Programming Blog: www.mattnewport.com/blog

thanx a lot, I understand it much better now.

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.
The gradients need to be 2D - the texture u and v direction don't necessarily line up with the screen x and y. Try this:
float2 texDdx = ddx(texUV);float2 texDdy = ddy(texUV);float4 colour = tex2Dgrad(texSampler, texUV, texDdx, texDdy);

Game Programming Blog: www.mattnewport.com/blog

This topic is closed to new replies.

Advertisement