如何在DirectX11中的几何着色器中实例化?

时间:2012-09-10 12:45:44

标签: directx-11 geometry-shader geometry-instancing

我知道在顶点着色器中你可以这样做:

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

在几何着色器中使用instancedPosition的等价物是什么?就像当我想要实例化由1个顶点构成的模型并且为每个实例在几何着色器中制作四边形并将quad的位置设置为instancePosition的位置时实例缓冲区中相应实例的。

1 个答案:

答案 0 :(得分:0)

为了传递几何着色器中的数据,您可以简单地从VS传递到GS,因此您的VS输出结构如下:

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

然后在您的顶点着色器中,直接在几何着色器中传递数据(未转换)

根据您的输入原始拓扑结构,几何着色器将以点,线或全三角形的形式接收数据,因为您提到要将点转换为四边形,原型将如下所示

[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();       
}