# Geometry shader with stream output

## Recommended Posts

Kirill Alpin    325

Hello folks,

I tried to generate a stream output with a geometry shader.
But when I "draw" it, the window turns black.
Could someone show me some sample code on geometry shader with stream out or could someone correct my existing code?

Draw call:

bool GeometrySh::Render(ID3D11DeviceContext* deviceContext, int count)
{
HRESULT result;
D3D11_MAPPED_SUBRESOURCE mappedResource;
unsigned int bufferNumber;
BufferType* dataPtr;

result = deviceContext->Map(m_cBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
if(FAILED(result))
{
return false;
}

dataPtr = (BufferType*)mappedResource.pData;
dataPtr->lightdir = D3DXVECTOR4(4.0f, 5.0f, 10.0f, 1.0f);
deviceContext->Unmap(m_cBuffer, 0);

bufferNumber = 0;

deviceContext->GSSetConstantBuffers(bufferNumber, 1, &m_cBuffer);

unsigned int stride;
unsigned int offset;

stride = sizeof(VertexType);
offset = 0;

deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

deviceContext->IASetInputLayout(m_gslayout);

deviceContext->SOSetTargets(1, &m_outBuffer, &offset);

deviceContext->DrawIndexed(count, 0, 0); // If I disable this line that bug dosen't occur

ID3D11Buffer* pBuffer[1];
pBuffer[0] = NULL;
deviceContext->SOSetTargets(1, pBuffer, &offset);

return true;
}


cbuffer BufferConType
{
float4 lightdir;
}

struct GIN
{
float4 pos : POSITION;
float2 tex : TEXCOORD0;
float3 normal : NORMAL;
};

struct GOUT
{
float4 pos : POSITION;
};

[maxvertexcount(3)]
void GS(triangleadj GIN input[6], inout TriangleStream<GOUT> OutputStream)
{
GOUT p;
p.pos = input[0].pos;
p.pos.w = 1.0f;
OutputStream.Append(p);
p.pos = input[2].pos;
p.pos.w = 1.0f;
OutputStream.Append(p);
p.pos = input[4].pos;
p.pos.w = 1.0f;
OutputStream.Append(p);
OutputStream.RestartStrip();
}


##### Share on other sites
MJP    19755

You can't just use a geometry shader, you always need a vertex shader bound in order to draw geometry. Do you create your device with the D3D11_CREATE_DEVICE_DEBUG flag? If you do, it should warn you about issues like this.

Edited by MJP

##### Share on other sites
Kirill Alpin    325

I tried to set it to D3D11_CREATE_DEVICE_DEBUG but i still get no output.

And I also set my debugtype to mixed.

Anyway thank you, this was the solution

##### Share on other sites
ankhd    2304

Hi.

Is the pos member in screen space , you need a view and a projection matrix if not you may need a colour member to as well, and you need a vertex shader

a pass through type if not needed

struc vsin

{

//stuff

};

vsin VS(vsin in)

{

return in;

}

##### Share on other sites
Jason Z    6434

What did you pass as the rasterized stream number to the function ID3D11Device::CreateGeometryShaderWithStreamOutput?  You are allowed to rasterize one of the output streams, and the rest have to be stream output only.  If you set it to not output a rasterized stream (see here for details) then you will not get any rendered output at all.

##### Share on other sites
Kirill Alpin    325

Edit: Thanks for all that help but I solved my first problem with the help MJP ^^. But......

I have a new problem. The output of my geometry shader, which should only output what it gets for input, is really wierd.

I pass 36 vertices to the shader but I get 12.

Here they are (in this format ;x;y;z;w;):

-1;-1;-1;1;

;0;0;0;0;

;0;-1;-1;1;

;1;0;0;0;

;0;0;1;-1;

;1;1;0;0;

;0;0;0;1;

;-1;-1;1;0;

;0;0;0;0;

;-1;-1;-1;1;

;0;0;0;0;

;0;1;-1;1;

As you can see, some w-values are 0.

But the values that I've send have no 0 as w-values. Here is my new geometry shader:

struct GIN
{
float4 pos : POSITION;
};

struct GOUT
{
float4 pos : POSITION;
};

[maxvertexcount(3)]
void GS(triangle GIN input[3], inout TriangleStream<GOUT> OutputStream)
{
GOUT p;
p.pos = input[0].pos;
OutputStream.Append(p);
p.pos = input[1].pos;
OutputStream.Append(p);
p.pos = input[2].pos;
OutputStream.Append(p);
OutputStream.RestartStrip();
}


It is very hard to make geometry shader with stream out work, as there is no sample code on it on the net.

##### Share on other sites
Kirill Alpin    325

Nevermind I found the solution myself. But I still don't know what the reason of this (new) problem was. I just tweaked at all settings and now it works ^^