0

I have an application that displays 3D object using D3D11 and DirectXMath. I also want to display a HUD in the Top Left Corner so i thought i would use DirectXTK sprintBatch/spriteFont to do this. My 3D is fine until i add the following code. Is DirectXTK using it's own shaders and changing some States?

Question is how do i fix this?

m_spriteBatch->Begin();
        const wchar_t* output = L"Hello World";
        m_font->DrawString(m_spriteBatch.get(), output, DirectX::XMFLOAT2{ 10, 10 }, Colors::Yellow);
        m_spriteBatch->End();

Without spritefont->DrawString

With spritefont->DrawString

Here is are my shaders.

Texture2D txDiffuse : register( t0 );
SamplerState samLinear : register(s0);
cbuffer WorldViewProjectionType : register(b0)
{
    matrix World;
    matrix View;
    matrix Projection;
};
cbuffer TransparentBuffer
{
    float4 blendAmount;
};
struct VS_INPUT
{
    float4 Pos : POSITION;
    float2 Tex : TEXCOORD0;
};
struct PS_INPUT
{
    float4 Pos : SV_POSITION;
    float2 Tex : TEXCOORD0;
};
PS_INPUT VS(VS_INPUT input)
{
    PS_INPUT output = (PS_INPUT)0;
    output.Pos.w = 1.0f;
    output.Pos = mul(input.Pos, World);
    output.Pos = mul(output.Pos, View);
    output.Pos = mul(output.Pos, Projection);
    output.Tex = input.Tex;
    return output;
}
float4 PS(PS_INPUT input) : SV_Target
{
    float4 color = txDiffuse.Sample(samLinear, input.Tex);
    color.a = blendAmount.a;
    return color;
}
float4 PSGray(PS_INPUT input) : SV_Target
{
    float4 color = txDiffuse.Sample(samLinear, input.Tex);
    float fLuminance = 0.299f * color.r + 0.587f * color.g + 0.114f *          color.b;
    return float4(fLuminance, fLuminance, fLuminance, blendAmount.a);
}
  • Yes, it does change states, including shaders. You can fix it by restoring all states required for rendering your objects just before draw call. – Ivan Aksamentov - Drop Oct 14 '15 at 13:19
  • This is a stab in the dark. The spriteBatch->Begin() takes a bunch of states. should i put my states in this and does spritebatch restore them after drawing the string? – Gary Rusher Oct 14 '15 at 13:34
  • Well, it is how rendering is usually done. DirectX API is a [state machine](https://en.wikipedia.org/wiki/Finite-state_machine). In rendering loop on each iteration you apply state set 1, draw object 1, apply state set 2, draw object2, ..., apply state set n, draw object n. DirectXTK is no magic, it just wraps one of those pairs for you. There is a [ID3D11DeviceContext::ClearState](https://msdn.microsoft.com/en-us/library/windows/desktop/ff476389%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396) method that could be useful sometimes. Check one of the Computer Graphics starter books for details. – Ivan Aksamentov - Drop Oct 14 '15 at 13:47

1 Answers1

0

Sweet, i got it working. yippy. after the swapchain->present i did reset all these.

            //*************************************************************************
        m_pImmediateContext->IASetInputLayout(m_pVertexLayout);
        m_pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
        TurnOffAlphaBlending(m_pImmediateContext);
        // Set the depth stencil state.
        m_pImmediateContext->OMSetDepthStencilState(m_pdepthStencilState, 1);
        m_pImmediateContext->OMSetRenderTargets(1, &m_pdesignerRenderTargetView, m_pdesignerDepthStencilView);
        m_pImmediateContext->RSSetState(m_prasterState);
        //*************************************************************************
  • Actually you should do this just before rendering your objects, not after presenting. – Ivan Aksamentov - Drop Oct 14 '15 at 13:49
  • Thanks for your help Drop. – Gary Rusher Oct 14 '15 at 13:51
  • 1
    For efficiency, DirectX Tool Kit does not 'restore' states. It changes any states it uses and then renders. It is up to the caller to change states they need for further draws. That said, you should not need to change the render target after using ``SpriteBatch``. – Chuck Walbourn Oct 14 '15 at 17:11
  • 1
    I added a section to the [SpriteBatch](https://github.com/Microsoft/DirectXTK/wiki/SpriteBatch) docs on state management. – Chuck Walbourn Oct 14 '15 at 17:58
  • Chuck, could you help me out with this issue.[Spritefont in 3d space](http://stackoverflow.com/questions/33155405/directxtk-spritefont-in-3d-space) – Gary Rusher Oct 16 '15 at 12:56