Direct X Input Assembler - 顶点着色器链接错误

Direct X Input Assembler - Vertex Shader linkage error

我正在学习 Direct X 并尝试在屏幕上绘制一个基本三角形。我有一个简单的顶点着色器和像素着色器。

顶点着色器:

float4 main( float2 pos : POSITION) : SV_Position
{
    return float4(pos.x,pos.y,0.0f,1.0f);
}

像素着色器:

float4 main() : SV_Target
{
    return float4(1.0f,1.0f,1.0f,1.0f);
}

当运行程序出现输入汇编器和顶点着色器之间的链接错误时:

Error: ID3D11DeviceContext::Draw: Input Assembler - Vertex Shader linkage error: Signatures between stages are incompatible. The input stage requires Semantic/Index (POSITION,0) as input, but it is not provided by the output stage.

我的渲染代码:

void renderer_draw_triangle()
{
    struct vertex
    {
        float x;
        float y;
    };

    const struct vertex vertices[] =
        {
            {0.0f, 0.5f},
            {0.5f, -0.5f},
            {-0.5f, -0.5f},
        };

    const UINT stride = sizeof(struct vertex);
    const UINT offset = 0u;

    ID3D11Buffer *vertex_buffer;
    D3D11_BUFFER_DESC bd = {
        .BindFlags = D3D11_BIND_VERTEX_BUFFER,
        .Usage = D3D11_USAGE_IMMUTABLE,
        .CPUAccessFlags = 0u,
        .MiscFlags = 0u,
        .ByteWidth = sizeof(vertices),
        .StructureByteStride = stride};
    D3D11_SUBRESOURCE_DATA sd = {.pSysMem = vertices};

    CHECK_ERROR(device->lpVtbl->CreateBuffer(device, &bd, &sd, &vertex_buffer));
    context->lpVtbl->IASetVertexBuffers(context, 0u, 1u, &vertex_buffer, &stride, &offset);

    ID3D11VertexShader *vertexShader;
    ID3DBlob *blob;
    CHECK_DX_ERROR(D3DReadFileToBlob(L"../Engine/build/shaders/VertexShader.cso", &blob));
    CHECK_DX_ERROR(device->lpVtbl->CreateVertexShader(device, blob->lpVtbl->GetBufferPointer(blob), blob->lpVtbl->GetBufferSize(blob), NULL, &vertexShader));

    context->lpVtbl->VSSetShader(context, vertexShader, NULL, 0u);

    ID3D11PixelShader *pixelShader;
    CHECK_DX_ERROR(D3DReadFileToBlob(L"../Engine/build/shaders/PixelShader.cso", &blob));
    CHECK_DX_ERROR(device->lpVtbl->CreatePixelShader(device, blob->lpVtbl->GetBufferPointer(blob), blob->lpVtbl->GetBufferSize(blob), NULL, &pixelShader));

    context->lpVtbl->PSSetShader(context, pixelShader, NULL, 0u);

    ID3D11InputLayout *input_layout;
    const D3D11_INPUT_ELEMENT_DESC inputs[] =
        {
            {"POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
        };

    CHECK_DX_ERROR(device->lpVtbl->CreateInputLayout(device, inputs, ASIZE(inputs), blob->lpVtbl->GetBufferPointer(blob), blob->lpVtbl->GetBufferSize(blob), &input_layout));

    CHECK_DX_ERROR(context->lpVtbl->IASetInputLayout(context, input_layout));

    context->lpVtbl->OMSetRenderTargets(context, 1u, &target, NULL);

    context->lpVtbl->IASetPrimitiveTopology(context, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

    D3D11_VIEWPORT vp = {.Width = 800, .Height = 600, .MinDepth = 0, .MaxDepth = 1, .TopLeftX = 0, .TopLeftY = 0};
    context->lpVtbl->RSSetViewports(context, 1u, &vp);

    CHECK_DX_ERROR(context->lpVtbl->Draw(context, ASIZE(vertices), 0u));
}

它说它无法在输入层中找到语义,但我已经重读了该代码一百万次并且无法弄清楚出了什么问题。语义显然在输入元素描述数组中。我很确定输入层绑定正确。有人可以帮忙吗?

POSITION 是旧的 Direct3D 9 名称,SV_Position 是 Direct3D 10+ 语义。欢迎您使用其中之一,但您必须保持一致。

IOW:如果您在着色器中使用 SV_Position(这是首选),那么您需要在 D3D11_INPUT_ELEMENT_DESC 输入布局结构中使用 SV_Position

原来我必须在顶点着色器之前创建像素着色器。我不太确定为什么,但它有效。

void renderer_draw_triangle()
{
    struct vertex
    {
        float x;
        float y;
    };

    const struct vertex vertices[] =
        {
            {0.0f, 0.5f},
            {0.5f, -0.5f},
            {-0.5f, -0.5f},
        };

    const UINT stride = sizeof(struct vertex);
    const UINT offset = 0u;

    ID3D11Buffer *vertex_buffer;
    D3D11_BUFFER_DESC bd = {
        .BindFlags = D3D11_BIND_VERTEX_BUFFER,
        .Usage = D3D11_USAGE_IMMUTABLE,
        .CPUAccessFlags = 0u,
        .MiscFlags = 0u,
        .ByteWidth = sizeof(vertices),
        .StructureByteStride = stride};
    D3D11_SUBRESOURCE_DATA sd = {.pSysMem = vertices};

    CHECK_ERROR(device->lpVtbl->CreateBuffer(device, &bd, &sd, &vertex_buffer));
    context->lpVtbl->IASetVertexBuffers(context, 0u, 1u, &vertex_buffer, &stride, &offset);

    ID3DBlob *blob;

    ID3D11PixelShader *pixelShader;
    CHECK_DX_ERROR(D3DReadFileToBlob(L"../Engine/build/shaders/PixelShader.cso", &blob));
    CHECK_DX_ERROR(device->lpVtbl->CreatePixelShader(device, blob->lpVtbl->GetBufferPointer(blob), blob->lpVtbl->GetBufferSize(blob), NULL, &pixelShader));
    
    context->lpVtbl->PSSetShader(context, pixelShader, NULL, 0u);

    ID3D11VertexShader *vertexShader;
    CHECK_DX_ERROR(D3DReadFileToBlob(L"../Engine/build/shaders/VertexShader.cso", &blob));
    CHECK_DX_ERROR(device->lpVtbl->CreateVertexShader(device, blob->lpVtbl->GetBufferPointer(blob), blob->lpVtbl->GetBufferSize(blob), NULL, &vertexShader));

    context->lpVtbl->VSSetShader(context, vertexShader, NULL, 0u);

    ID3D11InputLayout *input_layout;
    const D3D11_INPUT_ELEMENT_DESC inputs[] =
        {
            {"POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
        };

    CHECK_DX_ERROR(device->lpVtbl->CreateInputLayout(device, inputs, ASIZE(inputs), blob->lpVtbl->GetBufferPointer(blob), blob->lpVtbl->GetBufferSize(blob), &input_layout));

    CHECK_DX_ERROR(context->lpVtbl->IASetInputLayout(context, input_layout));

    context->lpVtbl->OMSetRenderTargets(context, 1u, &target, NULL);

    context->lpVtbl->IASetPrimitiveTopology(context, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

    D3D11_VIEWPORT vp = {.Width = 800, .Height = 600, .MinDepth = 0, .MaxDepth = 1, .TopLeftX = 0, .TopLeftY = 0};
    context->lpVtbl->RSSetViewports(context, 1u, &vp);

    CHECK_DX_ERROR(context->lpVtbl->Draw(context, ASIZE(vertices), 0u));
}