How do I instance in a geometry shader in DirectX11?

2.5k Views Asked by At

I know that in the vertex shader you do it like this:

PixelInputType TextureVertexShader(VertexInputType input)
{
        PixelInputType output;

// Change the position vector to be 4 units for proper matrix calculations.
        input.position.w = 1.0f;
// Update the position of the vertices based on the data for this particular instance.
        input.position.x += input.instancePosition.x;
        input.position.y += input.instancePosition.y;
        input.position.z += input.instancePosition.z;
// Calculate the position of the vertex against the world, view, and projection matrices.
        output.position = mul(input.position, worldMatrix);
        output.position = mul(output.position, viewMatrix);
        output.position = mul(output.position, projectionMatrix);

// Store the texture coordinates for the pixel shader.
output.tex = input.tex;

        return output;
}

What would be the equivalent for using instancedPosition in a geometry shader?Like when I wanna instance a model made of 1 vertex and for each instance to make a quad in the geometry shader and set the quad's position to be that of the instancePosition of the corresponding instance in the instance buffer.

1

There are 1 best solutions below

0
On

In order to pass the data in your geometry shader, you can simply pass in through from the VS to the GS, so your VS output structure would be like:

struct GS_INPUT
{
    float4 vertexposition :POSITION;
    float4 instanceposition : TEXCOORD0; //Choose any semantic you like here
    float2 tex : TEXCOORD1;
    //Add in any other relevant data your geometry shader is gonna need
};

then in your vertex shader, pass trough the data directly in your geometry shader (untransformed)

After depending on your input primitive topology your geometry shader will receive the data either as point, line or full triangle, since you mention you want to convert point to quad, prototype would look like this

[maxvertexcount(4)]
void GS(point GS_INPUT input[1], inout TriangleStream<PixelInputType> SpriteStream)
{
    PixelInputType output;

    //Prepare your 4 vertices to make a quad, transform them and once they ready, use
    //SpriteStream.Append(output); to emit them, they are triangle strips,
    //So if you need a brand new triangle,
    // you can also use SpriteStream.RestartStrip();       
}