Sign in to follow this  
MahanGM

Problem with texture coordination in shader

Recommended Posts

I'm trying to draw 2d planes with textures. Previously, I was doing this with fixed functions which was fine, but now I'm moving into shaders and I'm stuck.

 

I have written below shader:

/**
 * Parameters
 */
const float4x4     WorldViewProjMatrix;
const int         MaxAnisotropy;
const texture     MainTexture;
const float4     MainColor;
const bool        IsMainTextureSet = true;

/**
 * Samplers
 */
sampler2D MainSampler = sampler_state
{
    Texture = <MainTexture>;
    AddressU = Clamp;
    AddressV = Clamp;
    MipFilter = Point;
    MinFilter = Point;
    MagFilter = Point;
    MaxAnisotropy = <MaxAnisotropy>;
    MipMapLodBias = 0;
};

/**
 * Structs
 */
struct VertexShaderInput
{
    float4 Position : POSITION0;
    float2 TextureUV : TEXCOORD0;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
    float2 TextureUV : TEXCOORD0;
};

struct PixelShaderOutput
{
    float4 Color : COLOR0;
};

/**
 * Functions
 */
VertexShaderOutput VertexShaderMain(VertexShaderInput input)
{
    VertexShaderOutput output;

    output.Position = mul(input.Position, WorldViewProjMatrix);
    output.TextureUV = input.TextureUV;

    return output;
}

PixelShaderOutput PixelShaderMain(VertexShaderOutput input)
{
    PixelShaderOutput output;
    float4 textureColor = (IsMainTextureSet) ? tex2D(MainSampler, input.TextureUV) : float4(1.0f, 1.0f, 1.0f, 1.0f);

    output.Color = saturate(textureColor * MainColor);

    return output;
}
 
/**
 * Techniques
 */
technique Diffuse
{
    pass Pass0
    {
        VertexShader = compile vs_2_0 VertexShaderMain();
        PixelShader = compile ps_2_0 PixelShaderMain();
    }
}

The problem is, only first row and column of the texture is drawn. This seems to be a uv coordination problem, but if it was the problem it shouldn't have worked with fixed functions too.

 

I have attached two files. One is the shot from my application and second is my 128x128 test texture. If you look at the first picture you can see there is a red 2d plane with only the first row and column part of the second picture applied to it. In second picture first row and column is a red line.

 

I'm sure there is something I'm missing.

Edited by MahanGM

Share this post


Link to post
Share on other sites

Shader looks fine. Could be lots. Mismatching VertexDeclaration, wrong texture bound, wrong texture coordinates.

 

You get anything from debug layer (enable D3D9 debugging in the DirectX Control Panel) ?

 

Also, best get familiar with a graphics debugger (renderdoc, or vendor specific). For D3D9 PIX should be fine (if it works on your system). 

 

PS: Please show the calling code:  setting effect constants, applying the pass, binding mesh, drawing. Also show or tell, what renderstates you have set. Maybe we see something.

Edited by unbird

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