Accessing pixels in HLSL

Started by
7 comments, last by CadeF 17 years, 5 months ago
I'm currently looking at a tutorial for HLSL, and it has this little bit of code:
float4 color = tex2D(tex0, IN.Texcoord0);
Where 'Texcoord0' is of type float2. Is it possible to do the same thing with a type int2? Or will I just have to convert each element into a tex coordinate?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Advertisement
All values you calculate with in HLSL are float. Textures U and V range is 0 to 1 (more than this wraps or clamps as you define).
In addition to ET3D's reply, if you were hoping to use int2 to address specific pixels in the source texture then you're out of luck. You still have to use the 0.0-1.0 range of coordinates in a pixel shader.

With D3D9 you can pass in the height/width of the texture (or, more usually, the reciprocal height and width) and convert the integer pixel coordinates to proper texture coordinates. With D3D10 you can avoid passing in the constants and retrieve the buffer dimensions directly.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Okay, a quick related question: converting to texture coords, it's just simply the position, but in a float between [0,1], right? So, just: xpos / width will get me the 'u' component of the texture coord, right?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Almost, but check out "Directly Mapping Texels to Pixels" in the DirectX docs.
Almost?

Quote:Directly Mapping Texels to Pixels
For the pixel at (3, 1), the interpolated coordinates are UV (0.75, 0.25) because that pixel is located at three-fourths of the texture's width and one-fourth of its height.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
As the next paragraph says: "The texels do not line up with the pixels in this example". Basically, dividing by the size is good enough, but if you want pixel accuracy, either the pixel coordinates or texture coordinates need to be shifted.
Okay, I am having a problem is putting some pieces together.

What I am intending to do is to render the scene to a texture, and then render a quad over the screen, having set a pixel shader that will do some processing, like sharpening, or blurring to the texture on the quad.

I understand that where you want to draw something might not be okay because it may technically be "between" pixels, so when it is shifted to be drawn at the best approximation of what you wanted, the texels and pixels no longer match up, right? So something has to be shifted in order to compensate to get the right colors for what you're drawing, otherwise you will be interpolated colors which will be a mix of what you want, as a result of the shifting at the start.

Is that right?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Yes, that seems correct :)

This should be helpful, if you have any doubts
http://developer.nvidia.com/object/Mapping_texels_Pixels.html

This topic is closed to new replies.

Advertisement