用于在屏幕上绘制线条的2D GUI着色器

时间:2015-07-09 03:15:17

标签: c++ shader direct3d hlsl

我喜欢2D着色器,例如,如果后备缓冲区是800x600,我可以在屏幕空间中绘制线条,方框等。

我已经通过将它们分成三角形来绘制线条的代码,这部分一切正常。我的问题是在顶点着色器中做什么。

这是我现在所拥有的,但没有任何东西出现。没有来自调试运行时的错误或消息。

VertexShaderOutput UILineVertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

    output.Position = float4(input.Position, 1.0f);

    // Convert pixel coords to (-1, +1)

    output.Position.x = output.Position.x / videoWidth  * 2.0f - 1.0f;
    output.Position.y = output.Position.y / videoHeight * 2.0f - 1.0f;
    output.Position.z = 1.0f;
    output.Position.w = 1.0f;

    output.Color    = input.Color;  

    return output;
}

videoWidth和videoHeight设置为常量缓冲区,在本例中设置为800和600.

我不知道我是否有一个微妙的错误或一个根本的误解。想法?

编辑:

以下是设置和绘制调用:

    V(g_UILineShader.SetShaderVariables());
    V(g_UILineShader.SetShaderConstants());

      auto pd3dImmediateContext = DXUTGetD3D11DeviceContext();

      pd3dImmediateContext->VSSetShader(_ptrVertexShader, nullptr, 0);
      pd3dImmediateContext->VSSetConstantBuffers(0, 1, &_ptrCBNeverChanges.GetInterfacePtr());
      pd3dImmediateContext->VSSetConstantBuffers(1, 1, &_ptrCBChangesEveryFrame.GetInterfacePtr());

      pd3dImmediateContext->PSSetShader(_ptrPixelShader, nullptr, 0);
      pd3dImmediateContext->PSSetConstantBuffers(1, 1, &_ptrCBChangesEveryFrame.GetInterfacePtr());

      ID3D11SamplerState * const * ppSamplers = { &_ptrSamplerLinear.GetInterfacePtr() };
      pd3dImmediateContext->PSSetSamplers(0, 1, ppSamplers);

      pd3dImmediateContext->IASetInputLayout(_ptrLayout);

    // BUGBUG no doubt seriously inefficient to create a buffer each line draw!
size_t byteSize = vertices.size() * sizeof(UIVertex);
ID3D11Buffer * pBuffer = CreateVertexBuffer(pd3dDevice, byteSize, true, false, nullptr);
if (pBuffer)
{
    UINT stride = sizeof(UIVertex);
    UINT offset = 0;
    pd3dImmediateContext->IASetVertexBuffers(0, 1, &pBuffer, &stride, &offset);
    pd3dImmediateContext->IASetIndexBuffer(NULL, DXGI_FORMAT_R16_UINT, 0);
    pd3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
    pd3dImmediateContext->Draw(vertices.size(), 0);
    pBuffer->Release();
}

0 个答案:

没有答案