我在使用几何着色器时遇到了一个非常奇怪的问题。我制作了一个非常简单的几何着色器,它将一个点转换为一个三角形。
这是着色器:
struct GS_OUTPUT
{
float4 pos : SV_POSITION;
};
struct VS_INPUT
{
float3 pos : ANCHOR;
};
VS_INPUT VShader(VS_INPUT input)
{
return input;
}
float4 PShader(float4 position: SV_POSITION) : SV_Target
{
return float4(0.0, 1.0, 0.5, 0.0);
}
[maxvertexcount(3)]
void Triangulat0r( point VS_INPUT input[1], inout TriangleStream<GS_OUTPUT> OutputStream )
{
GS_OUTPUT output;
float3 _input;
_input = input[0].pos;
output.pos = float4(_input.x + 0.1, _input.y - 0.1, _input.z, 1.0);
OutputStream.Append( output );
output.pos = float4(_input.x - 0.1, _input.y - 0.1, _input.z, 1.0);
OutputStream.Append( output );
output.pos = float4(_input.x, _input.y + 0.1, _input.z, 1.0);
OutputStream.Append( output );
}
我向它发送 4 个这样的顶点:
// create test vertex data, making sure to rewind the stream afterward
var vertices = new DataStream(12*4, true, true);
vertices.Write(new Vector3(-0.5f, -0.5f, 0f));
vertices.Write(new Vector3(-0.5f, 0.5f, 0f));
vertices.Write(new Vector3(0.5f, 0.5f, 0f));
vertices.Write(new Vector3(0.5f, -0.5f, 0f));
vertices.Position = 0;
// create the vertex layout and buffer
var elements = new[] { new InputElement("ANCHOR", 0, Format.R32G32B32_Float, 0) };
var layout = new InputLayout(device, inputSignature, elements);
var vertexBuffer = new Buffer(device, vertices, 12 * 4, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
// configure the Input Assembler portion of the pipeline with the vertex data
context.InputAssembler.InputLayout = layout;
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.PointList;
context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, 12, 0));
我得到这个输出:
如您所见,我只向输入汇编器发送了 4 个三角形,输出中输出了 5 个。
关于为什么会发生这种情况的任何想法?恐怕这可能与缓冲区分配的额外内存有关……但我似乎无法弄清楚问题到底出在哪里。
提前致谢!