Passing colors through a pixel shader in HLSL
Asked Answered
S

3

7

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.

Syndic answered 7/2, 2013 at 21:2 Comment(0)
F
4
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;
Fertile answered 17/3, 2013 at 3:3 Comment(0)
A
1

*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);
}
Annelieseannelise answered 27/2, 2013 at 16:18 Comment(2)
I've verified that a pixel shader does not require a SV_POSITION input.Syndic
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); }Annelieseannelise
W
0

I have never seen this kind of constructor

float4(input.color, 1.0f);

this might be the problem, but I could be wrong. Try passing the float values one by one like this:

float4(input.color[0], input.color[1], input.color[2], 1.0f);

Edit:

Actually you might have to use float4 as type for COLOR (http://msdn.microsoft.com/en-us/library/windows/desktop/bb509647(v=vs.85).aspx)

Wafd answered 7/2, 2013 at 23:5 Comment(3)
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.Syndic
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.Syndic
float4(float3, 1.0f); is correct HSLS syntax, but I can't find the msdn doc on it.Annelieseannelise

© 2022 - 2024 — McMap. All rights reserved.