6
\$\begingroup\$

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?

\$\endgroup\$

2 Answers 2

5
\$\begingroup\$

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?

\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$ May 30, 2013 at 21:47
  • \$\begingroup\$ Thank you. I've edited my answer and added some info about floating-point textures. \$\endgroup\$ May 30, 2013 at 23:41
2
\$\begingroup\$

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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)? \$\endgroup\$ May 30, 2013 at 18:44
  • \$\begingroup\$ 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. \$\endgroup\$ May 30, 2013 at 21:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .