locked
DX11, Direct2D, and OMSetRenderTargets issue

    Question

  • I can succesfully render Direct2D text in my DX11 engine, by creating a ID2D1Factory1,  IDWriteFactory, IDXGIDevice1, ID2D1Device1, and ID2D1DeviceContext, as well as ID2D1Bitmap1 device. When I call "m_pD2D1Context->DrawText", I indeed see the text on the screen.
    However, when I in my scene code calls OMSetRenderTargets for a different render target than the main render target, when I now call "m_pD2D1Context->DrawText", I do not see any text rendered.
    When I again call OMSetRenderTargets with the main render target, and then call "m_pD2D1Context->DrawText", I again see the text rendered.

    What do I need to do in order to still render and see Direct2D text, after I call OMSetRenderTargets to render to a different target?

    If I insert "m_pSwapChain->Present(0, 0);" after "m_pD2D1Context->DrawText", I see the text rendered. Now, ofcourse, there are two Present calls in the scene, and that causes low FPS and a lot of flimmer. But I thought that discovery is important. It seems to me, that when I call OMSetRenderTargets for different RT, then ID2D and ID3D11 gets out of sync, or something like that.

    Saturday, March 09, 2013 2:48 PM

Answers

  • After some thoughts about the issue, I concluded that when I change RT, I need to change the ID2D1Bitmap1 used in the call m_pD2D1Context->SetTarget(pBitmap) to the ID3D11Texture2D (pTexture) created when I create the new RT. One way to do this is:

      IDXGISurface* pIDXGISurface=NULL;
      pTexture->QueryInterface(__uuidof(IDXGISurface), (void**) &pIDXGISurface);

      D2D1_BITMAP_PROPERTIES1 bmp;
      bmp.bitmapOptions = D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW;
      bmp.pixelFormat.format = DXGI_FORMAT_R8G8B8A8_UNORM;
      bmp.pixelFormat.alphaMode = D2D1_ALPHA_MODE_IGNORE;
      bmp.colorContext = NULL;
      bmp.dpiX = 96.0f;
      bmp.dpiY = 96.0f;

      ID2D1Bitmap1* pBitmap=NULL; 
      m_pD2D1Context->CreateBitmapFromDxgiSurface(pIDXGISurface, &bmp, &pBitmap);

    Saturday, March 09, 2013 8:29 PM

All replies

  • After some thoughts about the issue, I concluded that when I change RT, I need to change the ID2D1Bitmap1 used in the call m_pD2D1Context->SetTarget(pBitmap) to the ID3D11Texture2D (pTexture) created when I create the new RT. One way to do this is:

      IDXGISurface* pIDXGISurface=NULL;
      pTexture->QueryInterface(__uuidof(IDXGISurface), (void**) &pIDXGISurface);

      D2D1_BITMAP_PROPERTIES1 bmp;
      bmp.bitmapOptions = D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW;
      bmp.pixelFormat.format = DXGI_FORMAT_R8G8B8A8_UNORM;
      bmp.pixelFormat.alphaMode = D2D1_ALPHA_MODE_IGNORE;
      bmp.colorContext = NULL;
      bmp.dpiX = 96.0f;
      bmp.dpiY = 96.0f;

      ID2D1Bitmap1* pBitmap=NULL; 
      m_pD2D1Context->CreateBitmapFromDxgiSurface(pIDXGISurface, &bmp, &pBitmap);

    Saturday, March 09, 2013 8:29 PM
  • Thanks for sharing the solution.

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, March 11, 2013 3:01 AM
    Moderator