Jump to content
  • Advertisement
Sign in to follow this  
RPTD

GLSL correct way to sample ramp-textures?

This topic is 2635 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to us a bunch of curves for mapping stuff in my shaders. I sample each curve 256 times which is enough. I want to place them in a texture like this:

row 1: curve1.sample0 , curve1.sample1 ... curve1.sample256
row 2: curve2.sample0 , curve2.sample1 ... curve2.sample256
row ...

The idea is to use the HW filtering to do linear interpolation for any kind of value along a curve, hence along the S direction. What I don't want though is filtering along the T direction as then I would end up with different curves mingled with each other.

Can I sample a texture with filtering only along the S direction and not the T direction?

I'm not sure how GLSL exactly filters textures. Is (0,0) at the "center" of a texel or at the left border? If I have (for example) 2 curves (hence a 256x2 texture) and I sample at (0,0.25) do I sample at the center (along T direction) of row1 or a quarter below? I do not want to do it using multi-tapping to do filtering in the shader. I also want to use one texture for many curves so I do not have to switch textures for each set of curves (just altering texture coordinates).

Share this post


Link to post
Share on other sites
Advertisement

Can I sample a texture with filtering only along the S direction and not the T direction?

Yes, sure.


I'm not sure how GLSL exactly filters textures. Is (0,0) at the "center" of a texel or at the left border? If I have (for example) 2 curves (hence a 256x2 texture) and I sample at (0,0.25) do I sample at the center (along T direction) of row1 or a quarter below? I do not want to do it using multi-tapping to do filtering in the shader. I also want to use one texture for many curves so I do not have to switch textures for each set of curves (just altering texture coordinates).
[/quote]
Default sampler behavior for the HW filter is like this: (0,0) is the bottom left edge of the bottom left texel. (1,1) is the top right edge of the top right texel. Filtering is done from the center of each texel. In your example, where you have two functions along the t direction of a (256,2) texture, 0.25 would be exactly accessing function 1, 0.75 would be exactly function 2, and 0.5 would be a 50/50 mix between both (which you don't want). Anything above 0.75 or below 0.25 depends on the border mode.

If filtering is disabled along the t direction, the texcoord values are less critical. However a texcoord that lies exactly on a texel edge (0.0, 0.5 or 1.0) can lead to random aliasing between both functions.

Share this post


Link to post
Share on other sites

[quote name='RPTD' timestamp='1316910304' post='4865609']
Can I sample a texture with filtering only along the S direction and not the T direction?

Yes, sure.


I'm not sure how GLSL exactly filters textures. Is (0,0) at the "center" of a texel or at the left border? If I have (for example) 2 curves (hence a 256x2 texture) and I sample at (0,0.25) do I sample at the center (along T direction) of row1 or a quarter below? I do not want to do it using multi-tapping to do filtering in the shader. I also want to use one texture for many curves so I do not have to switch textures for each set of curves (just altering texture coordinates).
[/quote]
Default sampler behavior for the HW filter is like this: (0,0) is the bottom left edge of the bottom left texel. (1,1) is the top right edge of the top right texel. Filtering is done from the center of each texel. In your example, where you have two functions along the t direction of a (256,2) texture, 0.25 would be exactly accessing function 1, 0.75 would be exactly function 2, and 0.5 would be a 50/50 mix between both (which you don't want). Anything above 0.75 or below 0.25 depends on the border mode.

If filtering is disabled along the t direction, the texcoord values are less critical. However a texcoord that lies exactly on a texel edge (0.0, 0.5 or 1.0) can lead to random aliasing between both functions.
[/quote]
How can you set filtering only for one direction? I know only how to set filtering for a texture which affects both directions.

Share this post


Link to post
Share on other sites

How can you set filtering only for one direction? I know only how to set filtering for a texture which affects both directions.

By using array textures, in your case a 1D array texture. This would be equivalent to a standard 2D texture, but one dimension won't be filtered. The other dimension will use your specified filter mode. Note that the unfiltered dimension (the layer) has a different valid texcoord range. Check the docs.

However, all this is not really needed if you carefully adjust the texture coordinates the way I outlined above. This will effectively disable filtering on the dimension of your choice, even on regular 2D textures with enabled HW filter.

Share this post


Link to post
Share on other sites
Ah okay, 1D textures or layered textures are not an option. It has to be a 2D texture as I plan to put multiple sampled curves on the same texture to avoid switching textures. Tried the version of yours and so far it seems to work... but I have also not yet tested with mean cases. Can I be sure nVidia works properly with this mapping scheme too or do I have to make a double code-path for (yet another) nVidia feature(invalid spec usage)?

Share this post


Link to post
Share on other sites
Can I be sure nVidia works properly with this mapping scheme too or do I have to make a double code-path for (yet another) nVidia feature(invalid spec usage)?
Being able to correctly address texels is a very important feature - if it doesn't work the same on all cards, then there's a bug (either in your code or in the driver).

Share this post


Link to post
Share on other sites

Ah okay, 1D textures or layered textures are not an option. It has to be a 2D texture as I plan to put multiple sampled curves on the same texture to avoid switching textures.

A 1D layered texture is a 2D texture. It can contain thousands of independent 1D functions without requiring a texture switch (unless you want to attach them to an FBO as a render target, ie. generate the functions on the GPU).

Share this post


Link to post
Share on other sites

[quote name='RPTD' timestamp='1316996148' post='4865905']
Ah okay, 1D textures or layered textures are not an option. It has to be a 2D texture as I plan to put multiple sampled curves on the same texture to avoid switching textures.

A 1D layered texture is a 2D texture. It can contain thousands of independent 1D functions without requiring a texture switch (unless you want to attach them to an FBO as a render target, ie. generate the functions on the GPU).
[/quote]
What's the depth limitation hence how many layers maximum? How about memory usage hence does a 128 layer texture consume more memory than a 2D texture with height 128 (128 as an example)?

Share this post


Link to post
Share on other sites

[quote name='RPTD' timestamp='1316996148' post='4865905']Can I be sure nVidia works properly with this mapping scheme too or do I have to make a double code-path for (yet another) nVidia feature(invalid spec usage)?
Being able to correctly address texels is a very important feature - if it doesn't work the same on all cards, then there's a bug (either in your code or in the driver).
[/quote]
I'm asking this since I had already troubles with ATI/nVidia for example rendering text where ATI rendered the text crystal clear but nVidia blurred it. Had to add an offset large enough for nVidia to work but small enough for ATI to continue working. Same goes for other features which ATI can and nVidia can't or vice versa. I even have a lovely little hack-test in the init code testing two code versions picking the working one (version 1 works on ATI but errors on nVidia, version two works on nVidia but errors on ATI). So I'm very skeptical about ATI/nVidia doing the same when it gets down to OpenGL.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!