我已经将以前的借出问题减少到了我遇到的核心问题。

我有一个顶点缓冲区,它由4个顶点组成,并排列在一个平面中(标记为03):

1.  .2

0.  .3

以及相应的索引缓冲区{0,1,2,3,0}

现在,当我使用D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP渲染时,我获得了预期的图像:
  __
 |  |
 |__|

但是,当我使用D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP渲染时,结果是:
 | /|
 |/ |

请注意,不执行三角形填充。

更令人困惑的是,当使用D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST时,结果是:
 |
 |

如果将索引缓冲区更改为{0,1,2,0,2,3},它将呈现:
 | /
 |/

即,仅在前两个顶点之间绘制了一条像素线。

我将着色器简化为最原始的示例:

顶点着色器:
struct VertexInputType
{
    float4 position : POSITION;
};
struct PixelInputType
{
    float4 position : SV_POSITION;
};
PixelInputType VertexShader(VertexInputType input)
{
    PixelInputType output;
    input.position.w = 1.0f;
    output.position = input.position;
    return output;
}

像素着色器:
struct PixelInputType
{
    float4 position : SV_POSITION;
};
float4 PixelShader(PixelInputType input) : SV_TARGET
{
    float4 color;
    color.r = 0;
    color.g = 0;
    color.b = 0;
    color.a = 1;
    return color;
}

作为顶点,我正在使用DirectX::XMFLOAT3:
D3D11_INPUT_ELEMENT_DESC polygon_layout[1];
polygon_layout[0].SemanticName = "POSITION";
polygon_layout[0].SemanticIndex = 0;
polygon_layout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
polygon_layout[0].InputSlot = 0;
polygon_layout[0].AlignedByteOffset = 0;
polygon_layout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
polygon_layout[0].InstanceDataStepRate = 0;
d3d11_device->CreateInputLayout(polygon_layout, 1, compiled_vshader_buffer->GetBufferPointer(), compiled_vshader_buffer->GetBufferSize(), &input_layout);


D3D11_BUFFER_DESC vertex_buffer_desc;
vertex_buffer_desc.Usage = D3D11_USAGE_DEFAULT;
vertex_buffer_desc.ByteWidth = sizeof(DirectX::XMFLOAT3) * 4;
vertex_buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertex_buffer_desc.CPUAccessFlags = 0;
vertex_buffer_desc.MiscFlags = 0;
vertex_buffer_desc.StructureByteStride = 0;
DirectX::XMFLOAT3 vertices[4];
vertices[0].x = -0.5;    vertices[0].y = -0.5;    vertices[0].z = 0;
vertices[1].x = -0.5;    vertices[1].y =  0.5;    vertices[1].z = 0;
vertices[2].x =  0.5;    vertices[2].y =  0.5;    vertices[2].z = 0;
vertices[3].x =  0.5;    vertices[3].y = -0.5;    vertices[3].z = 0;
D3D11_SUBRESOURCE_DATA vertex_buffer_data;
vertex_buffer_data.pSysMem = vertices;
vertex_buffer_data.SysMemPitch = 0;
vertex_buffer_data.SysMemSlicePitch = 0;
hr = d3d11_device->CreateBuffer(&vertex_buffer_desc, &vertex_buffer_data, &vertex_buffer);

D3D11_BUFFER_DESC index_buffer_desc;
index_buffer_desc.Usage = D3D11_USAGE_DEFAULT;
index_buffer_desc.ByteWidth = sizeof(int32_t) *  6;
index_buffer_desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
index_buffer_desc.CPUAccessFlags = 0;
index_buffer_desc.MiscFlags = 0;
index_buffer_desc.StructureByteStride = 0;
int32_t indices[6];
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
indices[3] = 2;
indices[4] = 3;
indices[5] = 0;
D3D11_SUBRESOURCE_DATA index_buffer_data;
index_buffer_data.pSysMem = indices;
index_buffer_data.SysMemPitch = 0;
index_buffer_data.SysMemSlicePitch = 0;
hr = d3d11_device->CreateBuffer(&index_buffer_desc, &index_buffer_data, &index_buffer);

// during rendering I set:
unsigned int stride = sizeof(DirectX::XMFLOAT3);
unsigned int offset = 0;
d3d11_context->IASetVertexBuffers(0, 1, &vertex_buffer, &stride, &offset);
d3d11_context->IASetIndexBuffer(index_buffer, DXGI_FORMAT_R32_UINT, 0);
d3d11_context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
d3d11_context->RSSetState(rasterizer_state);
d3d11_context->IASetInputLayout(input_layout);
d3d11_context->VSSetShader(vertex_shader, NULL, 0);
d3d11_context->PSSetShader(pixel_shader,  NULL, 0);
// and render with:
d3d11_context->DrawIndexed(6, 0, 0);

当我使用ID3D11ShaderReflection::GetGSInputPrimitive()查看着色器时,我同时为顶点着色器和像素着色器接收到D3D_PRIMITIVE_UNDEFINED

我正在使用D3D11_FILL_SOLIDD3D11_CULL_NONE设置光栅化器阶段。

D3D11上下文中是否有任何设置或状态可以解释这种行为?
我对在哪里寻找任何想法感到高兴。
提前致谢!

最佳答案

首先,三角形带完全绘制您所期望的-一系列三角形。三角形索引数组中的每个索引都与前两个索引组合在一起以创建三角形。

我建议您将Triangle List不能被3 DirectX整除可能会错误地渲染(请记住,由于这是一个高容量的系统,因此它会跳过检查和平衡来提高速度)。

在检查了每种绘制模式(列表,剥离,扇形等)背后的逻辑之后,尝试在纸上绘制预期结果,以确保使用正确的顶点排序和绘制模式。

祝好运!

10-02 00:50
查看更多