Sign in to follow this  
jaredf55

Issue with pixel shading - my white value is being turned to black

Recommended Posts

I am using a pixel shader to basically do a color lookup. So, the original texture's color is used as the index into another texture to translate the color. Currently it's just grayscale textures. It works fine except when the original color is white, instead of getting the color i set to translate white to, i get the color i set black to translate to. I'm curious if anyone knows of a reason for this or if they can see what's wrong with my pixel shader or code. Here is the pixel shader:
texture testTexture;

sampler ImageSampler : register(s0);

sampler TextureSampler = sampler_state
{
    Texture   = (testTexture);
    MipFilter = None;
    MinFilter = None;
    MagFilter = None;
};

float4 OurFirstPixelShader(float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0
{
// retrieves the current image color
	float4 image = tex2D(ImageSampler, texCoord);
	float2 coord;
	coord.x = image.r;
	coord.y = image.b;
// looks up the new color from the old color's r and b values.
	float4 tex = tex2D(TextureSampler, coord);
	return tex;
}

technique Simplest
{
    pass Pass0
    {        
    	Lighting = FALSE;

		Sampler[0] = (TextureSampler); // Needed by pixel shader
        PixelShader = compile ps_2_0 OurFirstPixelShader();
    }
}
[/Source]
And here is the code that creates the lookup texture, i'm using SlimDX to load directx in C# but that pretty much is the same as DirectX.
  // Load the original texture
  _drawTexture = Texture.FromFile(_device, "testtexture.png", D3DX.DefaultNonPowerOf2, D3DX.DefaultNonPowerOf2, D3DX.FromFile, Usage.Dynamic, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0, out _imageInformation);

  // Create the lookup texture
  _lookupTexture = new Texture(_device, 256, 256, 0, Usage.Dynamic, Format.X8R8G8B8, Pool.Default);

  DataRectangle textureData = _lookupTexture.LockRectangle(0, LockFlags.None);
  unsafe {
    Byte* texturePixelData = (Byte*)textureData.Data.DataPointer;
    Byte* texturePixelRow = texturePixelData;
    int row = 0;
    int value;
    // set the colors to grayscale from black to white.
    for (int y = 0; y < 256; ++y) {
      for (int x = 0; x < 256; ++x) {
        value = x;

        *(texturePixelRow + row) = (byte)(value);
        *(texturePixelRow + row + 1) = (byte)(value);
        *(texturePixelRow + row + 2) = (byte)(value);
        *(texturePixelRow + row + 3) = (byte)(255);
        row += 4;
      }
    }
  }
  _lookupTexture.UnlockRectangle(0);
[/Source]
any ideas why the white lookup is being turned to black would be appreciated. all other colors look up fine. the image looks exactly the same, except for the white values are black.

Share this post


Link to post
Share on other sites
I'd get PIX on the case [smile]

Run up a single frame capture and then inspect the individual textures and sanity check that they contain the data you think they do, then use pixel history and step-through debugging to check the logic and the intermediary values.

I suspect it'll be pretty obvious once you can, quite literally, see the problem...


Jack

Share this post


Link to post
Share on other sites
I tried using PIX, but wasn't sure what to look for. When i was looking through the events and viewing the render state for each direct3d event, i couldn't see my textures individually and the render step was blank until it rendered the resulting image.

Though, I played around with the pixel shader and discovered that if i use a texture coordinate of 1.0, then i would get black, but if i used a coordinate value of .999 i would get white, or a color value of 1.0.

Does anyone know why looking up a texture with a coordinate of 1.0 appears to be outside the texture data?

Share this post


Link to post
Share on other sites
It sounds like you have the addressing mode set to wrap when it should probably be set to clamp for your usage. I have no idea how to set this in an FX file because I never use them but it's probably something like


sampler TextureSampler = sampler_state
{
Texture = (testTexture);
MipFilter = None;
MinFilter = None;
MagFilter = None;
AddressU = Clamp;
AddressV = Clamp;
};


Something like that anyway.

HTH.

{edit} Yes, that is the syntax, I discovered after checking out one of the sample apps in the SDK. Interestingly the same .FX file uses <> to surround the texture name rather than parentheses. I don't know if this makes a difference too, but it might be worth checking out.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this