Frage

I have a very strange problem with using geometry shaders. I made a very simple geometry shader which takes a point and transforms it into a triangle.

here is the shader:

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

}

I send it 4 vertices like this:

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

And I get this output:

output from the program, with the strange middle triangle

As you can see, I only sent 4 triangles to the input assembler, and 5 are coming out in the output..

Any ideas on why this happens? I'm afraid it might be related to extra memory being allocated by the buffers... but I can't seem to figure out where exactly the problem is.

Thanks in advance!

War es hilfreich?

Lösung

If you try to draw more vertices than whats in your vertex buffer, all "extra" vertices will have the value zero. You would see the following warning if you ran your program through Windbg (won't show in Visual studio).

D3D10: WARNING: ID3D10Device::Draw: Vertex Buffer at the input vertex slot 0 is not big enough for what the Draw*() call expects to traverse. This is OK, as reading off the end of the Buffer is defined to return 0. However the developer probably did not intend to make use of this behavior. [ EXECUTION WARNING #356: DEVICE_DRAW_VERTEX_BUFFER_TOO_SMALL ]

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top