cbuffer MatrixBuffer
{
	matrix worldMatrix;
	matrix viewMatrix;
	matrix projectionMatrix;
};

struct VertexInputType
{
    float4 position : POSITION;
	float2 size: SIZE;
	uint flags: FLAGS;
};

struct GeoOut
{
	float4 Pos : SV_POSITION;
	float2 Tex : TEXCOORD0;
};

[maxvertexcount(4)]
void BillboardGeometryShader(point VertexInputType gin[1], inout TriangleStream<GeoOut> triStream)
{
	gin[0].position.w=1.0f;
	float4 up = float4(viewMatrix._12_22_32, 0.0f);
	float4 right = float4(viewMatrix._11_21_31, 0.0f);
	float halfWidth = gin[0].size.x;
	float halfHeight = gin[0].size.y;
	float4 v[4];
	v[0] = float4(gin[0].position - halfWidth*right - halfHeight*up);
	v[1] = float4(gin[0].position - halfWidth*right + halfHeight*up);
	v[2] = float4(gin[0].position + halfWidth*right - halfHeight*up);
	v[3] = float4(gin[0].position + halfWidth*right + halfHeight*up);

	float2 texCoord[4];
	texCoord[0] = float2(0, 1);
	texCoord[1] = float2(0, 0);
	texCoord[2] = float2(1, 1);
	texCoord[3] = float2(1, 0);

	GeoOut gout;
	[unroll]
	for(int i = 0; i < 4; ++i)
	{
		gout.Pos = mul(v[i], viewMatrix);
		gout.Pos = mul(gout.Pos, projectionMatrix);
		gout.Tex = texCoord[i];
		triStream.Append(gout);
	}
}