Search code examples
shaderdirectx-11pixel-shader

about pixel shader input buffer


I'm using VS2015, directx11

Here's my Vertex Shader code.

cbuffer cbperobject {
    float4x4 gWorldViewProj;
};

struct VertexIn {
    float3 Pos : POSITION;
    float2 tex : TEXCOORD0;
};

struct VertexOut {
    float4 PosH : SV_POSITION;
    float2 tex : TEXCOORD0;

};

VertexOut main( VertexIn vin ) 
{
    VertexOut vOut;

    vOut.PosH = mul(float4(vin.Pos, 1.0f), gWorldViewProj);
    vOut.tex = vin.tex;
    return vOut;
}

and this is my pixel shader code

Texture2D shaderTexture;
SamplerState SampleType;
struct VertexIn {
    float4 PosH : SV_POSITION;
    float2 tex : TEXCOORD0;
};


float4 main(VertexIn Pin) : SV_TARGET
{
    float4 textureColor;
    textureColor = shaderTexture.Sample(SampleType, Pin.tex);
    return  textureColor;
}

For pixel shaders input buffer, I don't need position buffer in this code. so I deleted float4 PosH in this code. then pixel shader doesn't work I see black beer crate. when I retore the struct form which has float4 for position info and float2 for textcoord info, it works fine agian. I think I don't understand how this rendering pipeline work. Could you explain me why is this and how pipeline work? thank you.


Solution

  • DirectX shaders need to have matching input and output between the shader stages, or the registers will miss align. If you run with the debug layer, you would have get a warning.

    If your pixel shader does not need sv_position, you can put it at the end of VertexOut