Sign in to follow this  

DirectX11 3D and SpriteBatch Problem

This topic is 1197 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

 

I have just started my journey into DirectX 11 and Im having some trouble getting 2D and 3D to work at the same time in my app.

The 3D element was working fine until I intergrated SpriteBatch/SpriteFont. The sprite font now works but the 3D stuff doesnt.

 

Please see the link below for a visual representation

https://imagizer.imageshack.us/v2/1360x533q90/538/yPvXAe.png

 

The image on the left is without SpriteBatch and the image on the right is with it

 

I've been hunting around for a solution for a day and a half now but Im not having much luck solving it.

 

If anyone is able to shed any light on this it would be greatly appreciated.

 

This is the code i am using:

 

Init

 

    ZeroMemory(&mBufferDesc, sizeof(DXGI_MODE_DESC));

    mBufferDesc.Width = window->GetWindowWidth();
    mBufferDesc.Height = window->GetWindowHeight();
    mBufferDesc.RefreshRate.Numerator = 60;
    mBufferDesc.RefreshRate.Denominator = 1;
    mBufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    mBufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
    mBufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
    
    ZeroMemory(&mSwapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));

    mSwapChainDesc.BufferDesc = mBufferDesc;
    mSwapChainDesc.SampleDesc.Count = 4;
    mSwapChainDesc.SampleDesc.Quality = 0;
    mSwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    mSwapChainDesc.BufferCount = 1;
    mSwapChainDesc.OutputWindow = window->GetHwnd();
    mSwapChainDesc.Windowed = TRUE;
    mSwapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
    
    D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL, NULL, D3D11_SDK_VERSION, &mSwapChainDesc, &mSwapchain, &mDeviceInterface, NULL, &mDeviceContext);

    mSwapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&mBackBuffer);
    mDeviceInterface->CreateRenderTargetView(mBackBuffer, NULL, &mRenderTargetView);
    mBackBuffer->Release();

 

    mDepthStencilDesc.Width = window->GetWindowWidth();
    mDepthStencilDesc.Height = window->GetWindowHeight();
    mDepthStencilDesc.MipLevels = 1;
    mDepthStencilDesc.ArraySize = 1;
    mDepthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
    mDepthStencilDesc.SampleDesc.Count = 4;
    mDepthStencilDesc.SampleDesc.Quality = 0;
    mDepthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
    mDepthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
    mDepthStencilDesc.CPUAccessFlags = 0;
    mDepthStencilDesc.MiscFlags = 0;
    
    mDeviceInterface->CreateTexture2D(&mDepthStencilDesc, NULL, &mDepthStencilBuffer);
    mDeviceInterface->CreateDepthStencilView(mDepthStencilBuffer, NULL, &mDepthStencilView);
    mDeviceContext->OMSetRenderTargets(1, &mRenderTargetView, mDepthStencilView);

 

    mSpriteBatch.reset(new DirectX::SpriteBatch(mDeviceContext));
    mSpriteFont.reset(new DirectX::SpriteFont(mDeviceInterface, L"Fonts/arial.spritefont"));

 

Draw

 

mDeviceContext->ClearRenderTargetView(mRenderTargetView, mBackgroundColor);

mDeviceContext->ClearDepthStencilView(mDepthStencilView, D3D11_CLEAR_DEPTH| D3D11_CLEAR_STENCIL, 1.0f, 0);

 

mDeviceContext->UpdateSubresource( mConstantBuffer, 0, NULL, &WVP1, 0, 0 );
mDeviceContext->VSSetConstantBuffers( 0, 1, &mConstantBuffer );
mDeviceContext->DrawIndexed( 36, 0, 0 );

 

mDeviceContext->UpdateSubresource( mConstantBuffer, 0, NULL, &WVP2, 0, 0 );
mDeviceContext->VSSetConstantBuffers( 0, 1, &mConstantBuffer );
mDeviceContext->DrawIndexed( 36, 0, 0 );

 

mSpriteBatch->Begin(DirectX::SpriteSortMode_Deferred);
mSpriteFont->DrawString(mSpriteBatch.get(), L"Hello World", DirectX::XMFLOAT2(0, 0));
mSpriteBatch->End();

 

mSwapchain->Present(0, 0);

 

 

 

 

 

Share this post


Link to post
Share on other sites

You will need to set your vertex/index buffers and input layout before drawing the cubes.  Since SpriteBatch uses its own input assembler state, you need to manually set your cube state again before drawing them, otherwise whatever state is set during sprite rendering is getting applied to your cubes. Likewise if you set any other rendering states for your cubes, they will need to be reset also.

Edited by megadan

Share this post


Link to post
Share on other sites

Thanks megadan! This has fixed my problem. Turns out I was looking in the wrong place all along

 

If anyone else ever has this problem I had to recall the following to fix it

 

    mDeviceContext->VSSetShader(mVertexShader, 0, 0);
    mDeviceContext->PSSetShader(mPixelShader, 0, 0);    
    mDeviceContext->IASetIndexBuffer(mIndexBuffer, DXGI_FORMAT_R32_UINT, 0);    
    mDeviceContext->IASetVertexBuffers(0, 1, &mVertexBuffer, &stride, &offset);
    mDeviceContext->IASetInputLayout(mVertexLayout);

Share this post


Link to post
Share on other sites

This topic is 1197 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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