Jump to content
  • Advertisement
Sign in to follow this  
pindrought

DX11 DirectXTK SpriteBatch/SpriteFont and Draw calls

Recommended Posts

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?

 

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!