Sign in to follow this  
Narf the Mouse

Information on how to do texture LOD?

Recommended Posts

I'm trying to do texture level of detail, as it seems like it would provide a substantial speed increase. However, about the only information I could find out there leads to this function declaration:
[code]ret tex2D(s, t, ddx, ddy)[/code]
I have no clue how, why or what the "ddx" and "ddy" might do.

So - Any tutorials on texture LOD and what "ddx" and "ddy" do in that function?

Thanks.

Share this post


Link to post
Share on other sites
Actually, the correct function would be

[code]ret tex2Dbias(s, t)[/code]

where

[code]float4 t = float4(tex0.x, tex0.y, miplevel, miplevel)[/code]

Haven't figured out how to correctly calculate the miplevel though, should be something with dividing the z-position of the pixel/texel in view-space, but didn't work for me. I'll keep my eye at this thread ;)

Share this post


Link to post
Share on other sites
Texture LOD is automatic as long as the texture has mipmaps. You just use tex2D in your shader, and the correct miplevel is chosen by the hardware based on how large the texture is when mapped onto the screen. So as a triangle with the texture gets further from the camera, smaller mip levels will be used which reduces the amount of texture data that has to be read from memory. This also prevents texture aliasing, since the lower mip levels are prefiltered. You can bias the mip level if you want either using sampler states or by using tex2Dbias, but doing so can lead to lower quality or performance. Using a positive bias will cause a lower-than-normal level to be used which will make the texture look unusually blurry, while using a negative bias will cause texture cache thrashing as well as aliasing artifacts (but can make the textures appear sharper). You can also completely override the mip level using tex2Dlod or tex2Dgrad, but generally these are used for special cases where normal mip selection doesn't work.

Share this post


Link to post
Share on other sites
[quote name='The King2' timestamp='1318099590' post='4870556']
Haven't figured out how to correctly calculate the miplevel though, should be something with dividing the z-position of the pixel/texel in view-space, but didn't work for me. I'll keep my eye at this thread ;)
[/quote]

Basically it takes the X and Y gradients (derivatives) of the texture coordinate, and uses that to figure out how quickly the texture coordinate is changing in screen space. Then using the size of the texture, it can figure out the mip level that best matches the number of pixels that the texture will cover. So essentially larger gradients leads to a higher mip level being used (where mip level 0 is the full size of the texture). If the size is different in the X or Y direction, the larger gradient will be used which can result in blurriness at oblique angles. If anisotropic filtering is used, then for those cases additional samples are taken along the direction with the larger gradient.

In cases where pixel derivatives aren't available (for instance, in a vertex shader) it is common to use a metric based on Z depth to select a mip level. But it's not usually done in pixel shaders for "normal" textures.

Share this post


Link to post
Share on other sites
[quote name='MJP']Texture LOD is automatic as long as the texture has mipmaps. You just use tex2D in your shader, and the correct miplevel is chosen by the hardware based on how large the texture is when mapped onto the screen.[/quote]

I tried it out, but somehow it isn't working as expected. Well it does work but it looks kind of ugly:

[url="http://s14.directupload.net/file/d/2671/t9vojicv_jpg.htm"][img]http://s14.directupload.net/images/111008/temp/t9vojicv.jpg[/img][/url]

zoomed in:

[url="http://s14.directupload.net/file/d/2671/ovqidu9y_jpg.htm"][img]http://s14.directupload.net/images/111008/temp/ovqidu9y.jpg[/img][/url]

without mipmapping:

[url="http://s14.directupload.net/file/d/2671/kr7u9s6c_jpg.htm"][img]http://s14.directupload.net/images/111008/temp/kr7u9s6c.jpg[/img][/url]

I quess you shouldn see the effect of mipmapping on such small distances? Here is my samplerstate:

[code]sampler DiffuseSampler = sampler_state
{
Texture = <DiffuseMap>;

MagFilter = Linear;
MinFilter = Linear;
MipFilter = Linear;
};[/code]

Anything obviously wrong?

Share this post


Link to post
Share on other sites
Those results look perfectly normal for trilinear filtering. The issue with the bluriness on the sides of the cube is exactly the problem I mentioned before about oblique angles. To fix it, you can enable anisotropic filtering. You do that by setting the min filter to "Anisotropic", and then setting MaxAnisotropy to either 2, 4, 8, or 16. Higher values will have higher quality, with more performance cost.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this