2012-03-20 56 views
1

使用几何着色器时我有一个很奇怪的问题。我制作了一个非常简单的几何着色器,它需要一个点并将其转换为三角形。几何着色器在原点上接收到额外的点

这里是着色器:

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)); 

而且我得到这样的输出:

output from the program, with the strange middle triangle

正如你所看到的,我只向输入汇编器发送了4个三角形,并且5个出现在输入汇编器中tput ..

关于为什么发生这种情况的任何想法?恐怕这可能与缓冲区分配的额外内存有关......但我似乎无法弄清楚问题出在哪里。

在此先感谢!

+1

您的绘图调用是否使用超过4个顶点数?如果是这样,过去的任何顶点(绘制计数 - 缓冲区大小)的值为零,并且您的多余三角形以0为中心。 – 2012-03-20 16:53:56

+0

没错!我已经放置了12个顶点,因为这是实际绘制的,但我想这不是很聪明,因为实际上只有4个顶点被发送到GPU。非常感谢你!你想把它作为答案,所以我可以接受它吗? – 2012-03-20 17:00:14

+1

不客气。常量缓冲区也会出现同样的问题(额外的值为0),因此请注意 – 2012-03-20 17:04:30

回答

3

如果您尝试绘制比顶点缓冲区中的顶点更多的顶点,则所有“额外”顶点的值都将为零。如果您通过Windbg运行程序(不会在Visual Studio中显示),您会看到以下警告。

D3D10:警告:ID3D10Device ::消耗:顶点缓冲区的输入顶点 插槽0不是什么抽奖*()调用预计 遍历足够大。这是可以的,因为读取缓冲区的末尾被定义为 返回0.但是开发者可能不打算使用 这种行为。 [执行警告#356: DEVICE_DRAW_VERTEX_BUFFER_TOO_SMALL]