Anyone tell me the difference of tex2Dgrad and tex2D?,
Members - Reputation: 122
Posted 02 November 2007 - 12:15 PM
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?
GDNet+ - Reputation: 1034
Posted 02 November 2007 - 02:14 PM
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.
Members - Reputation: 121
Posted 03 November 2007 - 12:50 AM
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.
GDNet+ - Reputation: 1034
Posted 03 November 2007 - 07:58 AM
float2 texDdx = ddx(texUV);
float2 texDdy = ddy(texUV);
float4 colour = tex2Dgrad(texSampler, texUV, texDdx, texDdy);