【发布时间】:2017-01-17 01:25:52
【问题描述】:
我已将之前的撕裂问题减少到我卡住的核心。
我有一个顶点缓冲区,由 4 个顶点组成,排列在一个平面上(标记为 0 到 3):
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_SOLID 和D3D11_CULL_NONE 设置光栅化阶段。
在 D3D11 上下文中是否有任何设置或状态可以解释这种行为? 我很高兴看到任何想法。 提前致谢!
【问题讨论】:
-
也许是调用设置索引/顶点数据的问题?可能还需要展示这些。
-
感谢您的评论!我在问题中添加了该信息。
-
我看不出你的代码有什么特别的问题,尽管仅仅看代码通常很难找出这些错误。我建议运行一个框架调试工具,例如 Windows Graphics Diagnostics、Nvidia Nsight 等。
-
我要重点关注的是您对back-face culling 的正面缠绕订单。您的代码不包含您的
D3D11_RASTERIZER_DESC,尤其是CullMode、FillMode和FrontCounterClockwise的值。光栅化状态的设备默认值为:D3D11_CULL_BACK、D3D11_FILL_SOLID和FALSE。 -
感谢您的评论。正如我在问题中所写:我正在使用 D3D11_FILL_SOLID 和 D3D11_CULL_NONE 设置光栅化阶段。
标签: c++ direct3d direct3d11