Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing colors through a pixel shader in HLSL

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.

like image 690
Justin R. Avatar asked Feb 07 '13 21:02

Justin R.


2 Answers

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;
like image 181
mr bum Avatar answered Sep 22 '22 02:09

mr bum


*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);
}
like image 21
Stardidi Avatar answered Sep 22 '22 02:09

Stardidi