Thanks a lot, Tiago and Jason.
Thiago's solution would indeed work. I admit i did not think about that since i was doing everything in one pass in OpenGL. But with some small performance penalty it would make things work..
But Jason says it's possible to do everything in the same pass? this is interesting so i don't have to change the algorithm..
I made a simple example of what i'm trying to do... A trivial effect that takes in input a bunch of vertices, expand them to triangles, and attempts to output a new, x-shifted position to a buffer.
With such GS, would it be possible to output to a buffer only a single vertex per GS invocation, even if i send a long triangle strip to the rasterizer?
And also, if my GS states "maxvertexcount(n)", does it mean that for each invocation of the GS, i should at least account for(and so allocate) n possible entries in the output buffer?
Below the code:
/* UNIFORMS */
extern const float4x4 matMetersToTexture;
extern const float4x4 matProjectionTransformation;
// SRV for reading
StructuredBuffer<float4> positionin : register( t0 );
StructuredBuffer<float4> positionorig : register( t4);
// UAV for writing
//RWStructuredBuffer<float4> positionout : register( u0 ); // Doesnt work with nVidia Kepler.
float4 pullData(uint idx)
{
return positionin[idx];
}
float3 pullCoords(uint idx)
{
float4 p;
p = positionin[idx];
float3 vecCoords = float3(0,0,0);
vecCoords.xy = p.xy;
return vecCoords;
}
float2 metersToTexture(float3 vecVertexCoords)
{
float2 vecTexCoords;
vecTexCoords = ( mul( matMetersToTexture , float4(vecVertexCoords,1.0) ) ).xy;
return vecTexCoords;
}
float2 metersToCamera(float2 vecCoords)
{
float2 vecScreenCoords = ( mul( matProjectionTransformation , float4(vecCoords,0.0,1.0) ) ).xy;
return vecScreenCoords;
}
/* VERTEX SHADERS */
/* Vertex-shader output structure. */
struct MYVS_OUTPUT
{
float4 pos : SV_Position; // screen coordinate
float3 tex : TEXCOORD0; // texture coordinate
//uint vertexID : SV_VertexID; // Invalid.
};
MYVS_OUTPUT IDToVertexTransformVS (uint idx: SV_VertexID)
{
MYVS_OUTPUT output;
output.tex = float3(0,0,0);
output.pos = float4(0,0,0, 1);
float3 vecInputCoords = pullCoords(idx);
float2 vecTexCoords = metersToTexture( vecInputCoords );
output.tex.xy = vecTexCoords;
output.pos.xy = metersToCamera(vecInputCoords.xy);
return output;
};
/* GEOMETRY SHADERS */
/* Geometry-shader output structures. */
struct MYGS_OUTPUT
{
float4 pos : SV_Position; // screen coordinate
float3 tex : TEXCOORD0; // texture coordinate
float4 color: COLOR0;
};
struct MYGS_STREAMOUTPUT {
float4 newpos : Position; // new screen coordinate
};
[maxvertexcount(3)]
void MySimpleGeometryShaderTriangleWSO(
point MYVS_OUTPUT input[1],
uint iPrimID : SV_PrimitiveID,
inout TriangleStream<MYGS_OUTPUT> triangleStream
inout PointStream<MYGS_STREAMOUTPUT> pointStream)
{
const float fWidth = 0.055;
float4 vecData = pullData(iPrimID);
float2 vecCoords = vecData.xy;
float2 vecPos = metersToCamera(vecCoords);
float4 vecVertex0 = float4(vecPos,0.0,1.0) + float4(-(fWidth * 0.5),-(fWidth * 0.5),0.0,0.0);
float4 vecVertex1 = float4(vecPos,0.0,1.0) + float4((fWidth * 0.5),-(fWidth * 0.5),0.0,0.0);
float4 vecVertex2 = float4(vecPos,0.0,1.0) + float4(0.0,(fWidth * 0.5),0.0,0.0);
float4 vecColor;
vecColor = float4(0.8,0.0,0.0,1.0);
MYGS_OUTPUT outputStructure;
outputStructure.color = vecColor;
outputStructure.tex = input[0].tex;
// Base triangle 0
outputStructure.pos = vecVertex0;
triangleStream.Append( outputStructure );
outputStructure.pos = vecVertex1;
triangleStream.Append( outputStructure );
outputStructure.pos = vecVertex2;
triangleStream.Append( outputStructure );
//triangleStream.RestartStrip();
MYGS_STREAMOUTPUT streamOutputStructure;
streamOutputStructure.newpos = float4(vecData.xy + float2(0.01,0.0), vecData.zw);
pointStream.Append(streamOutputStructure);
}
/* PIXEL SHADERS */
/* Pixel-shader output structure. */
struct PS_OUTPUT {
float4 color : SV_Target0; // pixel color
};
PS_OUTPUT pstest (MYGS_OUTPUT input)
{
PS_OUTPUT output;
//output.color = float4(1,0,1,1);
output.color = input.color;
return output;
}
/* TECHNIQUES */
technique11 showpoints
{
pass P0
{
SetBlendState(OverBlendingWithStraightInAndPremultipliedOut, float4(0.0f, 0.0f, 0.0f, 0.0f), 0xFFFFFFFF);
SetVertexShader(CompileShader(vs_5_0, IDToVertexTransformVS()) );
GeometryShader pGSComp = CompileShader(gs_5_0, MySimpleGeometryShaderTriangleWSO());
GeometryShader pGSwSO = ConstructGSWithSO(pGSComp,
"Here goes something that specifies the data in the SO to the rasterizer. Ideally all the content in the MYGS_OUTPUT",
"Here goes something that specifies the data in the SO to the buffer. Ideally all the content in MYGS_STREAMOUTPUT",
NULL,
NULL,
0);
SetGeometryShader(pGSwSO);
SetPixelShader( CompileShader(ps_5_0, pstest()) );
}
}