Advertisement

DirectXTK SpriteBatch/SpriteFont and Draw calls

Started by December 29, 2017 06:43 PM
2 comments, last by pindrought 7 years, 1 month ago

I am trying to use DirectXTK for an easy way to render 2d font in a 3d engine.

I am getting very strange results, however.

 

Here is the code I am using for my render frame


void Graphics::RenderFrame()
{
	//Clear our backbuffer to the updated color
	const float bgColor[4] = { 0, 0, 1.0f, 1.0f };
	m_d3d11DevCon->ClearRenderTargetView(m_renderTargetView, bgColor);

	//Refresh the Depth/Stencil view
	m_d3d11DevCon->ClearDepthStencilView(m_depthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);

	m_d3d11DevCon->PSSetShaderResources(0, 1, &m_testTexture); //set texture to use for pixel shader
	m_d3d11DevCon->PSSetSamplers(0, 1, &m_samplerState); //set sampler state to use

	//Draw the square for texture

	m_d3d11DevCon->DrawIndexed(6, 0, 0);

	//draw text
	m_spriteBatch->Begin();

	const wchar_t* output = L"Hello World";


	XMVECTOR fontPos = XMVectorSet(0, 0, 0, 0);

	m_font->DrawString(m_spriteBatch.get(), output,
		fontPos, Colors::White, 0.f, g_XMZero, 2.0f);

	m_spriteBatch->End();

	//Present the backbuffer to the screen
	m_swapChain->Present(0, 0);
}

 

 

If I comment out all of the code for drawing the text, this is the result I get where my 3d square is being drawn correctly with its texture.

result1.PNG.ce45b5161ef5cd5640779ff4206a1db4.PNG

If I leave the code for Begin/End'ing the sprite batch and drawing the text, this is the result I get where my square in 3d space is no longer visible, and the H in "Hello" seems to have something gone wrong with it.

result2.PNG.d9409a9e4f85e1bdf71d1d12ce300bbb.PNG

If I only comment out the code to draw the string, but I leave the SpriteBatch->Begin / SpriteBatch->End calls, I get nothing on my screen, but I would expect to get the square with the texture on it.

result3.PNG.11d9a6204f1ab0222e30ae8601e3dab4.PNG

Is it not possible to combine Direct3D draw calls while using the DirectXTK SpriteBatch? If this will not work does anyone have any good recommendations of routes to go for efficient text rendering in dx11?

 

Is there anywhere in your code where you're setting device states before drawing your 3D square? There are several states that you can set on the context that will affect your rendering, such as blend state, depth/stencil state, rasterizer state, input layout, and vertex/index buffers, and I don't see you setting those anywhere in the code you've provided. SpriteBatch will set those states in order to do its thing (there's list of states that it will set listed here, under the section called "State management"). You'll want to make sure that you set all of the states that you need before issuing your draw call in order to ensure proper results. One thing that you can do to help with this is to call ID3D11DeviceContext::ClearState at the beginning of every frame, which will set the context back to a default state. I would also recommend enabling the debug validation layer when you create your device (but only in debug builts), and check out and warnings or errors that it reports. Another thing that can help with these kinds of issues is to use debugging tools like RenderDoc, which will let you inspect the device state at the time of a particular draw call.

Advertisement
3 hours ago, MJP said:

Is there anywhere in your code where you're setting device states before drawing your 3D square? There are several states that you can set on the context that will affect your rendering, such as blend state, depth/stencil state, rasterizer state, input layout, and vertex/index buffers, and I don't see you setting those anywhere in the code you've provided. SpriteBatch will set those states in order to do its thing (there's list of states that it will set listed here, under the section called "State management"). You'll want to make sure that you set all of the states that you need before issuing your draw call in order to ensure proper results. One thing that you can do to help with this is to call ID3D11DeviceContext::ClearState at the beginning of every frame, which will set the context back to a default state. I would also recommend enabling the debug validation layer when you create your device (but only in debug builts), and check out and warnings or errors that it reports. Another thing that can help with these kinds of issues is to use debugging tools like RenderDoc, which will let you inspect the device state at the time of a particular draw call.

Thank you so much! This is exactly what I needed. I didn't even think about all of the states being modified by the Spritebatch.

This topic is closed to new replies.

Advertisement