# 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->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.

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.

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.

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 on other sites

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 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.

## Create an account

Register a new account

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 29
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631739
• Total Posts
3001957
×