XNA Creators Club Online
Page 1 of 1 (2 items)
Sort Posts: Previous Next

Pixel shaders and texel offsets

Last post 05-07-2008 8:59 AM by Rim van Wersch. 1 replies.
  • 05-07-2008 7:05 AM

    Pixel shaders and texel offsets

    How do I get a pixel shader to access a relative texel without interpolation? i.e. one pixel in the texture

    I am trying to retreive the surrounding texels/pixels of a particular pixel/texel (think Conway's Game of Life).

    I am using SpriteBatch to render the texture and pixel shader to a texture render target.

    SpriteBatch is supposed to align texel/pixels as far as I can tell.

    But when I offset by a multiple of 1.0f / texturewidth or 1.0f / textureheight, I am getting weird results.

    e.g. this code should move everying thing down by one pixel (using black & white pixels)
    But over multiple iterations

    sampler tex : register(s0);

    float dx = 1.0f / 800.0f;
    float dy = 1.0f / 600.0;


    float4 PixelShaderFunction(float2 texCoord : TEXCOORD0) : COLOR0
    {   
        int count = 0;
       
        if (tex2D(tex, texCoord + float2(0, -dy)).x > 0)
            return float4(1, 1, 1, 1);
        else
            return float4(0, 0, 0, 1);
    }

    But instead it starts draing a weird looking solid triangle that slowly moves to the left and down.
  • 05-07-2008 8:59 AM In reply to

    Re: Pixel shaders and texel offsets

    Setting your min/mag/mip filters to Point should prevent the shader from interpolating the texels, which seems to be what your application calls for. Direct3D would default to that, but to make sure you can do this with:

    texture MyTexture;

    sampler MySampler =
    sampler_state
    {
      Texture = <MyTexture>;
      MinFilter = Point;
      MagFilter = Point;
      MipFilter = None;
    };

    More info on sampler states can be found over here.

    That said, I don't know if the XNA SpriteBatch exactly aligns texels with screen pixels, but this has been a common pitfall in Direct3D development for ages. You can find more information about mapping texels to pixels on this MSDN page, maybe this is of some help.

Page 1 of 1 (2 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG