Advertisement

texture.Sample() returning same value

Started by July 28, 2018 06:53 PM
4 comments, last by MJP 6 years, 6 months ago

Hello,

I'm implementing SSAO for my engine but I noticed the kernel wasn't rotating properly.
What happens is when I sample my random-vector texture I always get the same result, regardless of input texture coordinates.
Here's my shader:
 


Texture2D randomTexture : register(t2);

SamplerState smplr
{
    Filter = D3D11_FILTER_ANISOTROPIC;
    AddressU = Wrap;
    AddressV = Wrap;
};

float4 PS_main( in float4 screenPos : SV_Position) : SV_TARGET0
{
    //screensize 1280x720, randomTexture size 64x64
    float2 rvecCoord = float2(screenPos.x * 1280.f / 64.f, screenPos.y * 720.f / 64.f);
    float3 rvec = randomTexture.Sample(smplr, rvecCoord).xyz;
    return float4(rvec, 1.0f);
}

Off-topic code omitted.

I cant for the love of my life figure out why this sample would always give me the same result.
Changing the line :


float2 rvecCoord = float2(screenPos.x * 1280.f / 64.f, screenPos.y * 720.f / 64.f);

to 


float2 rvecCoord = float2(screenPos.xy);

seems to make no difference.

Here's a print of the shader state.

Any help appreciated! ❤️
 

Sometimes it feels like I know what I'm doing.

SV_Position is the pixel position in screenspace (e.g. 0 to windowWidth) not a normalized value (0...1).

Advertisement

You certainly don't want anisotropic filtering for a per-pixel noise texture. In fact, you don't want any filtering at all. Instead, you probably want to sample the texture with integer coordinates and bypass filtering entirely. If you do it this way, you can use an integer mod operation to "tile" the 64x64 noise texture across the entire screen:
 


float4 PS_main( in float4 screenPos : SV_Position) : SV_TARGET0
{
    uint2 rvecCoord = uint2(screenPos.xy) % 64;
    float3 rvec = randomTexture[rvecCoord].xyz;
    return float4(rvec, 1.0f);
}

 

11 hours ago, sk8beast said:

SV_Position is the pixel position in screenspace (e.g. 0 to windowWidth) not a normalized value (0...1).

I was so sure it was a normalized value.. Not sure how I've been getting by with that erroneous information.
Anyways, thank you for correcting me on this!
 

10 hours ago, MJP said:

You certainly don't want anisotropic filtering for a per-pixel noise texture. In fact, you don't want any filtering at all. Instead, you probably want to sample the texture with integer coordinates and bypass filtering entirely. If you do it this way, you can use an integer mod operation to "tile" the 64x64 noise texture across the entire screen:
 



float4 PS_main( in float4 screenPos : SV_Position) : SV_TARGET0
{
    uint2 rvecCoord = uint2(screenPos.xy) % 64;
    float3 rvec = randomTexture[rvecCoord].xyz;
    return float4(rvec, 1.0f);
}

 

The anisotropic was the samplerState i had bound originally but i had tried changing to point-filtering.
I wasn't aware you could simply index into a texture like that; great to know! Is it equivalent to using randomTexture.Load()?

Sometimes it feels like I know what I'm doing.

On 7/29/2018 at 2:54 AM, Finoli said:

I wasn't aware you could simply index into a texture like that; great to know! Is it equivalent to using randomTexture.Load()?

Yup, it's exactly the same as Load().

This topic is closed to new replies.

Advertisement