Problem with texture coordination in shader

Started by
1 comment, last by MahanGM 9 years, 1 month ago

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.

Advertisement

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.

I figured I had a problem with vertex declaration. It's fixed now. Thanks for your time smile.png

This topic is closed to new replies.

Advertisement