user245019
user245019

Reputation:

DirectX10 HLSL - Converting between UV and Texel and back

I'm trying to do convert between texels and uv's

I thought I could define a texel's size by doing 1/width or 1/height. the divide that to get the texel number.

I be wrong.

This is what I've been trying to do.

float4 PostPS(PostIn In) : SV_Target
{
    float4 color = 0;

    uint width, height;
    PostTex.GetDimensions(width, height);

    float2 uv = In.texCoord;
    float2 texelSize = float2(1/width, 1/height);

    // Convert coords to texels
    uint x = (uv.x / texelSize.x);
    uint y = (uv.y / texelSize.y);

    // Convert back again to uv.
    uv.x = x * texelSize.x;
    uv.y = y * texelSize.y;

    color = PostTex.Sample(pointFilter, uv);

    return color;
}

I realise there could be some degree of rounding errors, but I would have thought it would have worked to some degree.

Thanks.

Upvotes: 1

Views: 1965

Answers (1)

Nico Schertler
Nico Schertler

Reputation: 32587

You declared width and height as uint. 1 is int or uint, too. If you divide uint and uint, you'll get an uint. So your texelSize will always be (0, 0).

Instead of dividing by the texel size, multiply by the width and height:

uint x = uv.x * width;
uint y = uv.y * height;

Upvotes: 1

Related Questions