执行错误 #362:DEVICE_DRAW_POSITION_NOT_PRESENT

EXECUTION ERROR #362: DEVICE_DRAW_POSITION_NOT_PRESENT

我正在尝试在 DirectX 中绘制立方体。但是,我收到以下错误消息;

D3D11 ERROR: ID3D11DeviceContext::DrawIndexed: Rasterization Unit is enabled (PixelShader is not NULL or Depth/Stencil test is enabled and RasterizedStream is not D3D11_SO_NO_RASTERIZED_STREAM) but position is not provided by the last shader before the Rasterization Unit. [ EXECUTION ERROR #362: DEVICE_DRAW_POSITION_NOT_PRESENT]

这是我的着色器的代码;

cbuffer WorldBuffer : register (b0)
{
    matrix World;
    matrix View;
    matrix Projection;
}

struct VS_OUTPUT
{
    float4 pos   : SV_POSITION;
    float4 color : COLOR0;
};

VS_OUTPUT VS(float4 pos : POSITION, float4 color : COLOR) : VS_OUTPUT_SEMANTICS
{
    VS_OUTPUT output = (VS_OUTPUT)0;
    output.pos = mul(pos, World);
    output.pos = mul(output.pos, View);
    output.pos = mul(output.pos, Projection);
    output.color = color;
    return output;
}

float4 PS(VS_OUTPUT input : VS_OUTPUT_SEMANTICS) : SV_TARGET
{
    return input.color;
}

因为我给VS_OUTPUT.pos赋予了SV_POSITION语义,我认为着色器应该传递位置,但它似乎不起作用。我该如何修复我的着色器?


这就是我编译着色器的方式:

UINT flags = D3DCOMPILE_ENABLE_STRICTNESS | D3DCOMPILE_SKIP_OPTIMIZATION | D3DCOMPILE_DEBUG;

r = D3DCompileFromFile(L"shader.fx", NULL, NULL, "VS", "vs_5_0", flags, 0, &vertex_blob, &errMsg);

使用时:

VS_OUTPUT VS(float4 pos : POSITION, float4 color : COLOR) : VS_OUTPUT_SEMANTICS

VS_OUTPUT_SEMANTICS 实际上会覆盖每个元素的语义,因此不会输出像这样的结构:

struct VS_OUTPUT
{
    float4 pos   : SV_POSITION;
    float4 color : COLOR0;
};

你居然输出了

struct VS_OUTPUT
{
    float4 pos   : VS_OUTPUT_SEMANTICS;
    float4 color : VS_OUTPUT_SEMANTICS;
};

当你打电话时:

UINT flags = D3DCOMPILE_ENABLE_STRICTNESS | D3DCOMPILE_SKIP_OPTIMIZATION | D3DCOMPILE_DEBUG;

r = D3DCompileFromFile(L"shader.fx", NULL, NULL, "VS", "vs_5_0", flags, 0, &vertex_blob, &errMsg);

即使着色器可以编译,您仍应查看 errMsg,因为编译器可能会发出一些警告(在本例中会发出警告):

"semantics in type overriden by variable/function or enclosing type"

If 对生成的 blob 调用 D3DDisassemble 也是一个好主意, 这可以以文本形式为您提供生成的反射数据。

在您的顶点着色器案例中,您将拥有(仅相关部分):

// Input signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// POSITION                 0   xyzw        0     NONE   float   xyzw
// COLOR                    0   xyzw        1     NONE   float   xyzw
//
//
// Output signature:
//
// Name                 Index   Mask Register SysValue  Format   Used
// -------------------- ----- ------ -------- -------- ------- ------
// VS_OUTPUT_SEMANTICS      0   xyzw        0     NONE   float   xyzw
// VS_OUTPUT_SEMANTICS      1   xyzw        1     NONE   float   xyzw
//