Passing colors through a pixel shader in HLSL

Justin R. picture Justin R. · Feb 7, 2013 · Viewed 17.8k times · Source

I have have a pixel shader that should simply pass the input color through, but instead I am getting a constant result. I think my syntax might be the problem. Here is the shader:

struct PixelShaderInput
{
    float3 color : COLOR;
};

struct PixelShaderOutput
{
    float4 color : SV_TARGET0;
};

PixelShaderOutput main(PixelShaderInput input)
{
    PixelShaderOutput output; 
    output.color.rgba = float4(input.color, 1.0f); // input.color is 0.5, 0.5, 0.5; output is black
    // output.color.rgba = float4(0.5f, 0.5f, 0.5f, 1); // output is gray
    return output;
}

For testing, I have the vertex shader that precedes this in the pipleline passing a COLOR parameter of 0.5, 0.5, 0.5. Stepping through the pixel shader in VisualStudio, input.color has the correct values, and these are being assinged to output.color correctly. However when rendered, the vertices that use this shader are all black.

Here is the vertex shader element description:

const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = 
{
    { "POSITION",   0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "COLOR",      0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "TEXCOORD",   0, DXGI_FORMAT_R32G32_FLOAT,    0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};

I'm not sure if it's important that the vertex shader takes colors as RGB outputs the same, but the pixel shader outputs RGBA. The alpha layer is working correctly at least.

If I comment out that first assignment, the one using input.color, and uncomment the other assignment, with the explicit values, then the rendered pixels are gray (as expected).

Any ideas on what I'm doing wrong here?

I'm using shader model 4 level 9_1, with optimizations disabled and debug info enabled.

Answer

mr bum picture mr bum · Mar 17, 2013
output.color.rgba = float4(input.color, 1.0f);

your input.color is a float4 and you are passing it into another float4, i think this should work

output.color.rgba = float4(input.color.rgb, 1.0f);

this is all you need to pass it thru simply

 return input.color;

if you want to change the colour to red then do something like

input.color = float4(1.0f, 0.0f, 0.0f, 1.0f);
return input.color;