我已经将之前的渲染问题减少到我被卡住的核心。
我有一个顶点缓冲区,由4个顶点组成,排列在一个平面上(标记为0 to 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上下文中是否有任何设置或状态可以解释这种行为?我很高兴有任何想法可以去哪里寻找。提前感谢!
发布于 2017-01-19 12:58:29
事实证明,代码不是问题所在。在之前的某个地方,Direct3D状态发生了一些变化。
给context->ClearState();打电话解决了这个问题。
发布于 2017-01-17 10:07:52
首先,三角形条带绘制的正是您所期望的--一系列三角形。三角形索引数组中的每个索引都与前面的两个索引组合,以创建一个三角形。
我建议,由于您的三角形列表不能被3整除,DirectX可能渲染不正确(请记住,由于这是一个高容量系统,它会跳过检查和平衡,以提高速度)。
在查看了每种绘制模式背后的逻辑后,尝试在纸上绘制您的预期结果-列表,条带,扇形等,以确保您使用的是正确的顶点顺序和绘图模式。
祝好运!
https://stackoverflow.com/questions/41687662
复制相似问题