Why does setting a geometry shader cause my sprites to vanish?
Posted
by
ChaosDev
on Game Development
See other posts from Game Development
or by ChaosDev
Published on 2012-06-15T15:30:24Z
Indexed on
2012/06/15
21:25 UTC
Read the original article
Hit count: 549
My application has multiple screens with different tasks. Once I set a geometry shader to the device context for my custom terrain, it works and I get the desired results.
But then when I get back to the main menu, all sprites and text disappear. These sprites don't dissappear when I use pixel and vertex shaders.
The sprites are being drawn through D3D11, of course, with specified view and projection matrices as well an input layout, vertex, and pixel shader. I'm trying DeviceContext->ClearState()
but it does not help. Any ideas?
void gGeometry::DrawIndexedWithCustomEffect(gVertexShader*vs,gPixelShader* ps,gGeometryShader* gs=nullptr)
{
unsigned int offset = 0;
auto context = mp_D3D->mp_Context;
//set topology
context->IASetPrimitiveTopology(m_Topology);
//set input layout
context->IASetInputLayout(mp_inputLayout);
//set vertex and index buffers
context->IASetVertexBuffers(0,1,&mp_VertexBuffer->mp_Buffer,&m_VertexStride,&offset);
context->IASetIndexBuffer(mp_IndexBuffer->mp_Buffer,mp_IndexBuffer->m_DXGIFormat,0);
//send constant buffers to shaders
context->VSSetConstantBuffers(0,vs->m_CBufferCount,vs->m_CRawBuffers.data());
context->PSSetConstantBuffers(0,ps->m_CBufferCount,ps->m_CRawBuffers.data());
if(gs!=nullptr)
{
context->GSSetConstantBuffers(0,gs->m_CBufferCount,gs->m_CRawBuffers.data());
context->GSSetShader(gs->mp_D3DGeomShader,0,0);//after this call all sprites disappear
}
//set shaders
context->VSSetShader( vs->mp_D3DVertexShader, 0, 0 );
context->PSSetShader( ps->mp_D3DPixelShader, 0, 0 );
//draw
context->DrawIndexed(m_indexCount,0,0);
}
//sprites
void gSpriteDrawer::Draw(gTexture2D* texture,const RECT& dest,const RECT& source, const Matrix& spriteMatrix,const float& rotation,Vector2d& position,const Vector2d& origin,const Color& color)
{
VertexPositionColorTexture* verticesPtr;
D3D11_MAPPED_SUBRESOURCE mappedResource;
unsigned int TriangleVertexStride = sizeof(VertexPositionColorTexture);
unsigned int offset = 0;
float halfWidth = ( float )dest.right / 2.0f;
float halfHeight = ( float )dest.bottom / 2.0f;
float z = 0.1f;
int w = texture->Width();
int h = texture->Height();
float tu = (float)source.right/(w);
float tv = (float)source.bottom/(h);
float hu = (float)source.left/(w);
float hv = (float)source.top/(h);
Vector2d t0 = Vector2d( hu+tu, hv);
Vector2d t1 = Vector2d( hu+tu, hv+tv);
Vector2d t2 = Vector2d( hu, hv+tv);
Vector2d t3 = Vector2d( hu, hv+tv);
Vector2d t4 = Vector2d( hu, hv);
Vector2d t5 = Vector2d( hu+tu, hv);
float ex=(dest.right/2)+(origin.x);
float ey=(dest.bottom/2)+(origin.y);
Vector4d v4Color = Vector4d(color.r,color.g,color.b,color.a);
VertexPositionColorTexture vertices[] =
{
{ Vector3d( dest.right-ex, -ey, z),v4Color, t0},
{ Vector3d( dest.right-ex, dest.bottom-ey , z),v4Color, t1},
{ Vector3d( -ex, dest.bottom-ey , z),v4Color, t2},
{ Vector3d( -ex, dest.bottom-ey , z),v4Color, t3},
{ Vector3d( -ex, -ey , z),v4Color, t4},
{ Vector3d( dest.right-ex, -ey , z),v4Color, t5},
};
auto mp_context = mp_D3D->mp_Context;
// Lock the vertex buffer so it can be written to.
mp_context->Map(mp_vertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
// Get a pointer to the data in the vertex buffer.
verticesPtr = (VertexPositionColorTexture*)mappedResource.pData;
// Copy the data into the vertex buffer.
memcpy(verticesPtr, (void*)vertices, (sizeof(VertexPositionColorTexture) * 6));
// Unlock the vertex buffer.
mp_context->Unmap(mp_vertexBuffer, 0);
//set vertex shader
mp_context->IASetVertexBuffers( 0, 1, &mp_vertexBuffer, &TriangleVertexStride, &offset);
//set texture
mp_context->PSSetShaderResources( 0, 1, &texture->mp_SRV);
//set matrix to shader
mp_context->UpdateSubresource(mp_matrixBuffer, 0, 0, &spriteMatrix, 0, 0 );
mp_context->VSSetConstantBuffers( 0, 1, &mp_matrixBuffer);
//draw sprite
mp_context->Draw( 6, 0 );
}
© Game Development or respective owner