Help! GS StreamOutput..

Started by
4 comments, last by Jason Z 9 years, 6 months ago

Hello, I'm new of D3D, and i have a problem with the stream output feature..

What i need is to output a single structure (a float4, to be precise) per GeometryShader invocation.
The problem is that the geometry shader generates a triangle strip or a line strip, so the vertexcount is never 1, and, even if i can set the maxvertexcount to some number, the number of actual elements of the TriangleStream or LineStream is variable.

So what i wonder is whether it is possible to output only one float4 even if the Geometry Shader sends a list of various elements to the rasterizer..

Should this be not possible (I'm afraid it's not as the api seemingly do not really let me specify it), is there another way to do the same?
I thought of writing to a UAV from the GS, but i'm on nVidia hardware, and this seems to be not supported.
I also thought of what in OpenGL would be a texture buffer object + the extension to write textures (ext_image_load_store).
Is there something equivalent in D3D11?

thanks!

Advertisement

You can stream output a PointStream (more info here), so it can be one float4 per GS invocation.

EDIT:

I think I misunderstood your question...

You want to send a triangle/line stream to the rasterizer and stream output to a PointStream in the same geometry shader right?

From the docs:

When using multiple output streams, all streams must contain points, and up to one output stream can be sent to the rasterizer. More commonly, an application will not rasterize any stream.

So, unless there's a trick that I don't know, I think you'll have to use two passes, one to stream output points, and a second to rasterize triangles or lines.

This is how particle systems are implemented using the geometry shader. 1st update each particle in the GS and stream output, and then use a different GS that expands the particles to quads and sends them to the pixel shader.

You can actually stream output and rasterize at the same time, you just use multiple stream outputs in your geometry shader. You would append the single float4 to your stream output stream, and then just send your other geometry out another stream to be rasterized. Have you tried that out yet? Perhaps if you could show your steam output declaration entries, it might help to understand what you are currently set up for.

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

Concerning Tiago's suggestion, i was also wondering what i should put as the last parameter of ConstructGSWithSO(), in the case i don't want to rasterize any stream.
And also how to specify the same thing on the client side, since i use D3DX11CreateEffectFromFile() , which from what i understood, calls automatically CreateGeometryShaderWithStreamObject()

thanks!

I haven't ever done this with the effects framework, but here is what you would do if you were manually setting the parameters:

1. Fill out the D3D11_SO_DECLARATION_ENTRY elements to enumerate your streamed out data items. I presume you are already doing this somewhere in your C++ code. This is also what declares which data is going into which stream.

2. When you create the geometry shader with ID3D11Device::CreateGeometryShaderWithStreamOutput method, you specify which stream gets rasterized in the RasterizedStream argument.

Thus it should be possible for you to define two streams, one for rasterization of the triangle stream and one for point outputs that goes to the stream output buffer.

The max vertex output is a limitation supplied mostly for the driver to efficiently allocate enough memory for each invocation of the GS. Your stream output buffer should be at least big enough to hold whatever sized data you are planning to store in it!

This topic is closed to new replies.

Advertisement