Sign in to follow this  
duhone2

HLSL shader texture sampling problem

Recommended Posts

duhone2    122
Having a problem with a shader that Ive been trying to solve for some time. Its basicly implementing a simple 2d old school tile engine using a shader. It pretty much works but I get these thin dashed lines at the edge between the tiles. The tile map is rendered a single large quad. It uses 2 textures, one to hold the tiles, and one to hold the tile map(tile map is a single channel A8 texture where A stores what tile to use for that block). During my debugging I've output the texture coords directly and saved out the results for analysis and pretty much proved that the texture coords are exactly what they should be. They never go outside the tile, or go past 0-1 range, or turn into a nan. No matter how much you zoom the even if the texels become 25 pixels wide on your screen, the offending line stays a dashed 1 pixel width line. I'm currently thinking the problem is not with the shader, and its just some video card setting I don't know about or something. The problem is always along the edge where the abrubt change in texture coords is. The color of the line is odd, its not the exact color of any particular pixel in my map, which shouldnt be possible since im using point filtering at the moment. Heres the shader. Any ideas would be much appreciated, been stuck on this for 2 weeks. Ignore commented code, just debuging hacks. Posted shader is hard coded hacked to ignore the tile map itself for now, to exclude that as a cause.
string description = "2d classic tile map shader";

//------------------------------------
float4x4 worldViewProj : WorldViewProjection;
//x is width of a tile. z is width of a map entry.
//float4 tileSize = float4(1.0f,0.0f,0.015625f,0.0f); 
float4 tileSize : TileSize
< string Type = "Material"; >; 

texture tilesTexture : Texture0;
texture mapTexture : Texture1;

//------------------------------------
struct vertexInput {
    float3 position				: POSITION;
    float2 texCoordDiffuse	: TEXCOORD0;
};

struct vertexOutput {
    float4 hPosition		: POSITION;
    float2 texCoordDiffuse	: TEXCOORD0;
};


//------------------------------------
vertexOutput VS_TransformAndFrame(vertexInput IN) 
{
    vertexOutput OUT;
    OUT.hPosition = mul( float4(IN.position.xyz , 1.0) , worldViewProj);
    //OUT.texCoordDiffuse.xy = (IN.position.xy + 0.5);
    OUT.texCoordDiffuse.xy = IN.texCoordDiffuse.xy;
    //OUT.texCoordDiffuse.y = 1.0f - OUT.texCoordDiffuse.y;
    return OUT;
}


//------------------------------------
sampler TilesSampler = sampler_state 
{
    texture = <tilesTexture>;
    AddressU  = CLAMP;        
    AddressV  = CLAMP;
    MIPFILTER = POINT;
    MINFILTER = POINT;
    MAGFILTER = POINT;
};

sampler MapSampler = sampler_state 
{
    texture = <mapTexture>;
    AddressU  = CLAMP;        
    AddressV  = CLAMP;
    AddressW  = CLAMP;
    MIPFILTER = POINT;
    MINFILTER = POINT;
    MAGFILTER = POINT;
};

//-----------------------------------
float4 PS_Sample( vertexOutput IN): COLOR
{
  //float4 mapEntry = tex2D( MapSampler, IN.texCoordDiffuse );
  float4 mapEntry;
  //mapEntry.x = 3.0f/255.0;
  //mapEntry.x *= 255.0f;
  float2 diffuseTex;
  //diffuseTex.y = 0;
  //diffuseTex.x = 0;//mapEntry.x*tileSize.x;
  float2 partial = IN.texCoordDiffuse.xy/tileSize.zz;
  float2 whole;
  float2 fraction = modf(partial,whole);
  float tempentry = 2;
  diffuseTex.x = tempentry*tileSize.x;
  diffuseTex.x += fraction.x*(tileSize.x);
  diffuseTex.y = fraction.y*(tileSize.y);
  float4 result = tex2D( TilesSampler, diffuseTex.xy );
  //float4 result;
  //if(isnan(diffuseTex.x))
	//result.x = fraction.x;
  //else
	//result.x = 0;//diffuseTex.x;
  //result.x = diffuseTex.x*(253/255.0)+(1.0/255.0);//percent.x*tileSize.x*0.2f+0.4f;
  //result.y = diffuseTex.y*(253/255.0)+(1.0/255.0);//diffuseTex.y*0.2f+0.4f;
  //result.z = 0;
  //result.w = 1;
  //if(result.w <= 0.99f)
  //	clip(-1);
  
  return result;
}

//-----------------------------------
technique q0_p0_main
{
    pass p0 
    {		
		ZEnable = true;
		ZWriteEnable = true;
		ZFunc = LessEqual;
		AlphaBlendEnable = true;
		CullMode = None;
                DitherEnable = false;
		VertexShader = compile vs_3_0 VS_TransformAndFrame();
		PixelShader  = compile ps_3_0 PS_Sample();
    }
}

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