This project has moved and is read-only. For the latest updates, please go here.

Mixing spriteBatch and primitive batch

Dec 20, 2015 at 10:08 PM
Hi all can any one tell me if its possible to draw sprites with sprite batch or text with sprite font along with primitives drawn with primitive batch ? I'm working on pcg and for debugging purposes I'm drawing my rooms as primitives and also need to draw room number, however as soon as I call spritebatch begin and sprite batch end the primitives are not drawn and it doesn't matter if I render them first or last any ideas ?
Dec 21, 2015 at 8:17 PM
Edited Dec 21, 2015 at 8:21 PM
Until you call End on PrimitiveBatch, they might not be drawn. If you if is important that you draw them 'mixed' then you should call End on PrimitiveBatch before drawing the SpriteBatch and then restart a new primitive batch.

More typically, you'd draw all the PrimitiveBatch stuff first, and then draw all the SpriteBatch stuff. If you need to mix it, then you need to use smaller batches.

You should also read up on SpriteBatch sorting and PrimitiveBatch draw order.
Dec 21, 2015 at 8:39 PM
Edited Dec 22, 2015 at 1:19 PM
well my render loop looks like this:
// ClearRenderTargetView
    DirectXAPI::GetInstance()->GetD3D11DeviceContext()->ClearRenderTargetView( DirectXAPI::GetInstance()->GetD3D11RenderTagetView(), DirectX::Colors::BlueViolet );

    // Render Objects Here 
    // Apply Effects 
    m_pBasicEffect->Apply( DirectXAPI::GetInstance()->GetD3D11DeviceContext() );
    // Set input layout
    DirectXAPI::GetInstance()->GetD3D11DeviceContext()->IASetInputLayout( m_pInputLayout );

    // Begin to draw with primitive batch


    for(unsigned int i = 0; i < m_generatedRooms.size(); i++)
        m_generatedRooms[i].RenderDebug( m_pPrimitiveBatch, environmentPos );


       // end draw with primitive batch  

// this works fine and primitives are drawn but as soon as I call 

       // draw all sprites 

// the sprites are drawn and primitives are not drawn  and it doeasn matter if I draw sprites firs or primitives first

// Present back Buffer
    DirectXAPI::GetInstance()->GetD3D11SwapChain()->Present( 0, 0 );
Dec 23, 2015 at 12:46 AM
You are not setting all the state required for PrimitiveBatch.

The BasicEffect::Apply takes care of:
  • Shaders
  • Constant Buffers
  • Sampler State
  • ShaderResources
PrimitiveBatch::Begin takes care of:
  • Index Buffer
  • Vertex Buffer
  • Primitive Topology
You are missing sets for:
  • BlendState
  • DepthStencilState
  • RasterizerState
CommonStates states( device );


// Begin to draw with primitive batch
deviceContext->OMSetBlendState( states.Opaque(), nullptr, 0xFFFFFFFF );
deviceContext->OMSetDepthStencilState( states.DepthNone(), 0 );
deviceContext->RSSetState( states.CullNone() );
See Effects State management, PrimitiveBatch State management and SpriteBatch State management
Marked as answer by walbourn on 12/22/2015 at 4:46 PM
Dec 23, 2015 at 1:01 AM
Edited Dec 23, 2015 at 1:10 AM
Yep exactly now it works fine. Thanks a lot !
Btw I have this rendering code from "simple rendering - drawing triangle" tutorial so maybe it would be worth to add this there as well
Dec 23, 2015 at 8:40 PM
Edited Dec 23, 2015 at 9:53 PM
Hi again I don't want to create another thread to ask this question so I ask here, this time is the question regarding ComPtr lets say I have a texture

Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_texture;

and now I call Reset() is it the same as I would have raw pointer and call m_texture->Release() ?

Also can you tell me how do I make a full copy of such pointer if I have a simple sprite class wrapper and I want to provide my copy constructor and assignment operator to make a full copy ?

And also can you tell me If there are any rules regarding use of ComPtr and unique_ptr what I mean is do I have to use ComPtr for things such as textures etc. and unique pointers for things like SpriteBatch or SpriteFont or any other objects ? Or can I chose witch one to use and stick to it ? Sorry for silly questions but I'm used to raw pointers and all this stuff is kind of magic to me.
Dec 24, 2015 at 8:25 AM
Edited Dec 24, 2015 at 8:28 AM
The latest version of the Simple Rendering tutorial on GitHub includes these state objects. I've stopped updating the CodePlex copy as noted on the top of the page.

ComPtr is used for COM objects, i.e. things derived from IUnknown with the methods QueryInterface, AddRef, and Release. As with most smart-pointers, it works by relying on C++'s rules for scope: when the ComPtr variable is copied, it calls AddRef on the object, and when it is destroyed it calls Release on the object. See Managing the Lifetime of an Object and ComPtr.

std::unique_ptr and std::shared_ptr work on any C++ heap-allocated object, which is why we use them for SpriteBatch, SpriteFont, etc.

See Smart Pointers (Modern C++)
Dec 24, 2015 at 4:30 PM
Thanks ! switching to GitHub than !