Sign in to follow this  
Buckeye

DirectXTK - how to reset winding order/RH-to-LH after sprite batch?

Recommended Posts

Buckeye    10747

Problem: DirectXTK SpriteFont/SpriteBatch set a state I can't determine. I want to be able to reset whatever it's doing. Looking for information.

 

Setup: D3D11, DirectXTK, Windows 7, Windows SDK 8.1

 

I'm trying out the DirectXTK and using SpriteFont/SpriteBatch for text. Some state gets changed which, after googling, examining code, reading docs, etc., I haven't been able to determine.

 

The symptom is that meshes rendered with LH projection and rasterstate set to cull-back-face appear ... I'm guessing... maybe RH or cull-front-face.. I can't tell for sure.

 

Before:

[attachment=21621:captured11_before.png]

 

After drawing text with font/spritebatch:

[attachment=21622:captured11.png]

// initialization
    g_States.reset(new CommonStates(g_pd3dDevice));
    g_Sprites.reset(new SpriteBatch(g_pImmediateContext));
    g_Font.reset(new SpriteFont(g_pd3dDevice, L"c:\\visualstudio\\d11engine4\\DirectXTK\\italic.spritefont"));

    ....
// rendering
    if (bViewStats) // turns on and off text display
    {
        g_Sprites->Begin(SpriteSortMode_Deferred);
        g_Font->DrawString(g_Sprites.get(), L"DirectXTK Simple Sample", XMFLOAT2(100, 10), Colors::Yellow);
        g_Sprites->End();
    }

Yeah, I copied and pasted some code from the DirectXTK simple sample - but I have tried to find the answer. Really.

 

Any help?

Share this post


Link to post
Share on other sites
unbird    8336

I wonder if this (only) the winding. Rather looks like depth is failing.

 

Hmmm... have a look at [tt]SpriteBatch::Impl::PrepareForRendering[/tt], seems depth stencil state is changed, too. 

 

Edit: From a quick glance, it doesn't look like states are reset automatically (SpriteBatch::End()), so you probably have to do that yourself.

Edited by unbird

Share this post


Link to post
Share on other sites
Buckeye    10747

As always, unbird, you lead me in the right direction. I created a default depth stencil state and set that state after the sprite batch call. Works like a champ.

 

In case anyone's interested, these are the states that are set by SpriteBatch::Begin()

    auto blendState        = mBlendState        ? mBlendState.Get()        : mDeviceResources->stateObjects.AlphaBlend();
    auto depthStencilState = mDepthStencilState ? mDepthStencilState.Get() : mDeviceResources->stateObjects.DepthNone();
    auto rasterizerState   = mRasterizerState   ? mRasterizerState.Get()   : mDeviceResources->stateObjects.CullCounterClockwise();
    auto samplerState      = mSamplerState      ? mSamplerState.Get()      : mDeviceResources->stateObjects.LinearClamp();

In my render routine, I set the appropriate rasterizer state (both fillmode and cullmode) and the blend state as needed for various draw routines. Sampler states are set in each shader. That sets all but the depthstencilstate. So I revised my render routine:

    if (bViewStats)
    {
        g_Sprites->Begin(SpriteSortMode_Deferred);
        g_Font->DrawString(g_Sprites.get(), L"DirectXTK Simple Sample", XMFLOAT2(100, 10), Colors::Yellow);
        g_Sprites->End();
        g_pImmediateContext->OMSetDepthStencilState(pDefaultDepthStencilState, D3D11_COMPARISON_LESS); // restore the default
    }

I salute you! And, if anyone cares, the pix below is a PNG frame capture showing GDI, GDI+ and DirectXTK font rendering all playing nice in the backbuffer.

Edited by Buckeye

Share this post


Link to post
Share on other sites
L. Spiro    25622
I don’t know about DirectXTK, but if it is based around ID3DXSprite then it should have a flag similar to that of D3DXSPRITE_DONOTMODIFY_RENDERSTATE in Begin().
This is one of the most important flags to set as it will kill your performance otherwise (and cause bugs with more sophisticated engine wrappers which remove redundant render states, again for performance).


A quick glance at the documentation reveals they do have a way to override the states they change in Begin().
http://directxtk.codeplex.com/wikipage?title=SpriteBatch&referringTitle=DirectXTK

You should be overriding it to change only what is necessary, and instead of restoring them all only restore as needed. Render-state changes are to be avoided as much as possible.


L. Spiro Edited by L. Spiro

Share this post


Link to post
Share on other sites
Buckeye    10747


A quick glance at the documentation reveals they do have a way to override the states they change in Begin().

 

I don't want to override the spritebatch states. I want to restore the states it changes back to what I need for the rest of my routines.

 


You should be overriding it to change only what is necessary, and instead of restoring them all only restore as needed

 

See my post above. I stated that I change the states as needed.

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