0
votes

I am trying to create a shader where i have as input the positon of the vertex, some transformation matrixes and a float4 for the color of the vertex. The manipulation of the position works fine but i dont get the correct color out of it.

enter image description here

Okay so here is the Inputlayout of the shader:

D3D11_INPUT_ELEMENT_DESC solidColorLayout[] =
{
    { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};

And the shader itself does look like this:

cbuffer cbChangesEveryFrame : register(b0)
{
    matrix worldMatrix;
};

cbuffer cbNeverChanges : register(b1)
{
    matrix viewMatrix;
};

cbuffer cbChangeOnResize : register(b2)
{
    matrix projMatrix;
};

struct VS_Input
{
    float4 pos  : POSITION;
    float4 color : COLOR;
};

struct PS_Input
{
    float4 pos: SV_POSITION;
    float4 color: COLOR;
};

PS_Input VS_Main(VS_Input vert)
{
    PS_Input vsout = (PS_Input)0;
    vsout.color = vert.color;

    float4 worldPos = mul(vert.pos, worldMatrix);
    vsout.pos = mul(worldPos, viewMatrix);
    vsout.pos = mul(vsout.pos, projMatrix);

    return vsout;
}


float4 PS_Main(PS_Input psinput) : SV_TARGET
{
    return psinput.color;
}

Dont get confuesed about the matrix those transformation are correct i do get the right vertexposition and so on but i dont get the color i define.

So for example i create vertexes like this:

struct VertexPos
{
    XMFLOAT3 pos;
    XMFLOAT4 color;
};
...

VertexPos vertices[] =
{
    { XMFLOAT3(-1.0f, 0.0f, -1.0f), XMFLOAT4(0.1f,0.1f, 0.1f, 0.1f)},
    { XMFLOAT3(1.0f, 0.0f, -1.0f), XMFLOAT4(0.1f, 0.1f, 0.1f, 0.1f) },
    { XMFLOAT3(1.0f, 0.0f, 1.0f), XMFLOAT4(0.1f, 0.1f, 0.1f, 0.1f) },
    { XMFLOAT3(-1.0f, 0.0f, 1.0f), XMFLOAT4(0.1f, 0.1f, 0.1f, 0.1f) },
};

With some indexbuffer and the drawcall itself is preaty simple:

unsigned int stride = sizeof(VertexPos);
unsigned int offset = 0;

//set inputlayout and topology for drawing the sprites
context->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);
context->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R16_UINT, 0);

//calculate cube stuff
XMMATRIX worldMat = getWorldMatrix();
worldMat = XMMatrixTranspose(worldMat);
context->UpdateSubresource(worldBuffer, 0, 0, &worldMat, 0, 0);//update world matrix
//draw
context->DrawIndexed(6, 0, 0);

So i wonder whats wrong with it? (see the lines and the small faces at the site they should have the color)

1

1 Answers

1
votes

got it: My D3D11_INPUT_ELEMENT_DESCwas wrong. I forgot the AlignedByteOffset. So the correct Ofset should be:

D3D11_INPUT_ELEMENT_DESC solidColorLayout[] =
{
    { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};

The litle 12 ruined it. But it works fine now. Or even change it to this:

D3D11_INPUT_ELEMENT_DESC solidColorLayout[] =
{
    { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};

Alternative you can use D3D11_APPEND_ALIGNED_ELEMENTand it will automatically caluclate the right values. (Not sure if it's "save")