Sign in to follow this  
bottomy

Texture sampling issue

Recommended Posts

I've run into a sampling problem in my shader, it's not accessing the texel I expect in some cases. Now I'm not sure if this is because of floating point precision, or how texture2D works internally.

The problem is when I try access the texel at the y (t) coord using either 0.0, 0.5, 1.0 it doesn't return the texel I expect. For instance "texture2D(sometex, vec2(Xval, 0.5))" would not return the texel I'm expecting, however if the y (t) value is 0.48 (or lower) and then I add 0.01 (cannot be another value) till it reaches 0.5 "texture2D(sometex, vec2(Xval, 0.48 + 0.01 + 0.01))" it will return the texel I expect.


Now I've tried changing the texture format and it hasn't made any difference. The texture's minification and magnification filters are nearest, and s,t are clamped to edge (though shouldn't affect 0.5). Though the texture's height isn't a power of two, so while non power of two textures are supported I'm not sure if this would affect accessing those texels (the precision or whatever the actual problem is)?

Share this post


Link to post
Share on other sites
Something to try:

in your PS, output the color (0.48 + 0.01 + 0.01) < 0.5. If you see all white, it's likely a precision issue.

Also, "[color=#1C2837][size=2]minification and magnification filters are nearest,"... [/size][/color] so I assume you want 0.5 to access a different texel than 0.48?

Share this post


Link to post
Share on other sites
Consider a 2x2 texture which demonstrates exactly the problem you're having. The color of a texel is defined at its center, so consider where the coordinates 0, 0.5 and 1 ends up.

Coordinate 0.5 is the exact middle of the texture, which is exactly in the middle between the two texels in my example 2x2 texture. If you sample with nearest filtering, any of the two (or four) surrounding texels can be sampled, and it's up to roundings and such that determines which texel is sampled. Unless you have an odd number of texels along a dimension, the coordinate 0.5 will hit the exact middle between adjacent texels.

If you need exact texel addressing, you need to offset your coordinates by 0.5 in non-normalized texture coordinates. That is, you must offset by 0.5/width (or height), in order to hit the exact center of the texel. In a 2x2 texture, the colors of the texture are defined at coordinates 0.25, and 0.75.

Share this post


Link to post
Share on other sites
[quote name='jameszhao00' timestamp='1318764110' post='4873095']
Something to try:

in your PS, output the color (0.48 + 0.01 + 0.01) < 0.5. If you see all white, it's likely a precision issue.

Also, "[color="#1C2837"][size="2"]minification and magnification filters are nearest,"... [/size][/color] so I assume you want 0.5 to access a different texel than 0.48?
[/quote]


It returns true (so it is less than 0.5). And yes I need nearest, since I'm using this texture as a lookup for my lighting (converted my lighting to tiled-based deferred shading), so if it wasn't accurate I could end up getting some unlit patches in my light.


[quote name='Brother Bob' timestamp='1318764313' post='4873096']
Consider a 2x2 texture which demonstrates exactly the problem you're having. The color of a texel is defined at its center, so consider where the coordinates 0, 0.5 and 1 ends up.

Coordinate 0.5 is the exact middle of the texture, which is exactly in the middle between the two texels in my example 2x2 texture. If you sample with nearest filtering, any of the two (or four) surrounding texels can be sampled, and it's up to roundings and such that determines which texel is sampled. Unless you have an odd number of texels along a dimension, the coordinate 0.5 will hit the exact middle between adjacent texels.

If you need exact texel addressing, you need to offset your coordinates by 0.5 in non-normalized texture coordinates. That is, you must offset by 0.5/width (or height), in order to hit the exact center of the texel. In a 2x2 texture, the colors of the texture are defined at coordinates 0.25, and 0.75.
[/quote]


Yes that was my problem, it had an even numbered height (I should've specified the width and height in OP). I changed to use the offsetting and it works fine now. Thanks.

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