Passing colors through a pixel shader in HLSL

20,127

Solution 1

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;

Solution 2

*Are you sure that your vertices are in the place they are supposed to be? You are starting to make me doubt my D3D knowledge. :P I believe your problem is that you are only passing a color, BOTH parts of the shader NEED a position in order to work. Your PixelShaderInput layout should be: struct PixelShaderInput { float4 position :SV_POSITION; float3 color : COLOR; };*

Could you maybe try this as your pixel shader?:

float4 main(float3 color : COLOR) : SV_TARGET
{
    return float4(color, 1.0f);
}
Share:
20,127
Justin R.
Author by

Justin R.

My new indention style is unstoppable.

Updated on March 17, 2020

Comments

  • Justin R.
    Justin R. over 4 years

    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.

  • Justin R.
    Justin R. over 11 years
    Thanks Yeti. The float4 initialization that I have there I took directly from the Direct3D sample that ships with VS2012. I tried changing to the one that you mention but am getting the same result.
  • Justin R.
    Justin R. over 11 years
    I modified my code such that the vertex declaration now has color as DXGI_FORMAT_R32G32B32A32_FLOAT, and I have modified the shaders to take and return float4 rather than float3, but no change in the output.
  • Justin R.
    Justin R. over 11 years
    I've verified that a pixel shader does not require a SV_POSITION input.
  • Stardidi
    Stardidi over 11 years
    float4(float3, 1.0f); is correct HSLS syntax, but I can't find the msdn doc on it.
  • Stardidi
    Stardidi over 11 years
    Not sure if this is the problem, but could you try replacing you pixel shader with: float4 main(float3 color : COLOR) : SV_TARGET { return float4(color, 1.0f); }