Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

When using the tex2d function in a HLSL shader, as so:

float height = tex2D(heightMap, IN.texCoord).r;

What range of values can be returned to the height variable? Is it always a number between 0.0f - 1.0f? Or does it depend on the texture or sampler?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

The tex2D function usually returns a float in the range of [0.0, 1.0].

Think of tex2D as a function that returns a color.

Color components (like red, for example) range from 0.0 to 1.0 where the former is the total absence of that color and the latter is full intensity.

However, as Nathan Reed and other members have pointed out, texture formats that are not normalized may return values outside this range. Here is a StackOverflow post that explains floating-point textures: What exactly is a floating point texture?

share|improve this answer
    
As noted in the comments on the other answer, this is only true for the "usual" (normalized) texture formats. If you have floating-point textures it can return any value at all. –  Nathan Reed May 30 '13 at 21:47
    
Thank you. I've edited my answer and added some info about floating-point textures. –  Nick Caplinger May 30 '13 at 23:41
add comment

it always returns 0.0f -> 1.0f. even if you save your texture as an uint, you will still sample it as a float. the gpu is specially built for fast float operations and lots of them.

And if the case comes that you want it to range from -1.0 to 1.0 ( where black is -1 and gray ( 128 ) is 0 and white is 1 ) you can simply convert it like this

tex2D() * 2.0 - 1.0; this is really normal when you are using normalmaps.

share|improve this answer
1  
What about texture with unnormalized integer formats? Or HDR float buffers? Isn't it up to the texture view to determine how to interpret and return the pixel data? Or are those features only available with the newer sampling syntax (I've never really touched D3D9)? –  Sean Middleditch May 30 '13 at 18:44
    
Like Sean said, it only returns a value in [0, 1] for the "usual" (normalized) texture formats. If you have floating-point textures it can return any value at all. –  Nathan Reed May 30 '13 at 21:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.