本文介绍了D3D11:如何绘制GDI文本到GXDI表面? (无D2D)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助,使用GDI和D3D11绘制文本到纹理。我尝试使用D2D / DirectWrite,但它支持只有D3D10,而不是D3D11,因为我需要。我试过的所有失败到目前为止...
现在我想使用GDI方法写在纹理。
所以我创建了一个纹理与这个params:

  Usage = D3D11_USAGE_DEFAULT; 
Format = DXGI_FORMAT_B8G8R8A8_UNORM;
BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
CPUAccessFlags = 0;
MiscFlags = D3D11_RESOURCE_MISC_GDI_COMPATIBLE


$ b $ p

然后我从这个纹理创建一个正常的RenderTargetView,



下一步:获取DXGI界面:

  m_pTexFSText-> QueryInterface(__uuidof(IDXGISurface1),(void **)(& m_pDXGISurface)); 

在Render函数中,我只需这样做:

  m_pDeviceContext-> OMSetRenderTargets(1,& m_pTextRenderTarget,NULL); 

HDC hDc = NULL;
if(FAILED(m_pDXGISurface-> GetDC(TRUE,& hDc)))
return E_FAIL;

COLORREF bla = SetPixel(hDc,1,1,RGB(255,255,255));
bool hmm = TextOutA(hDc,10,10,LALALA!,7);

if(FAILED(m_pDXGISurface-> ReleaseDC(NULL)))
return E_FAIL;

问题是,纹理在GDI绘图后还是空的
一切正常,没有错误消息。



我希望任何人都能解释它是如何工作的。



感谢您,Stefan



编辑:尝试使用 GetDC(FALSE,& hDc)

我实际上在上周很多次遇到这个问题 - 但我' ve得到一切工作!下面是一些你应该知道/做的事情,使它一切正常:




  • 检查表面要求GetDC方法在这里工作:






  • 如果要在后台缓冲区中使用它,请记住重新绑定渲染目标之后您呼叫ReleaseDC。


  • 您不能使用任何 Direct3D在GetDC()和ReleaseDC()调用之间绘图,因为表面被DXGI for GDI自动锁定。但是你可以混合使用GDI和D3D渲染,只要你每次需要使用GDI时调用GetDC()/ ReleaseDC(),然后再移到D3D。


  • 这最后一个位可能听起来很容易,但你会惊讶有多少开发人员陷入这个问题 - 当你在后台缓冲区使用GDI绘制时,记住这是缓冲区,而不是帧缓冲区,所以为了实际看到你画的,你必须重新绑定到OM和调用swapChain-> Present()方法,所以backbuffer将成为一个帧缓冲区,其内容将显示在屏幕上。 p>



I need some help with drawing a text to a texture with GDI and D3D11. I tried using D2D/DirectWrite, but it supports just D3D10 and not D3D11 as I need. Everything I tried failed so far...Now I want to use GDI methodes to write in the texture.So I created a texture with this params:

Usage = D3D11_USAGE_DEFAULT;
Format = DXGI_FORMAT_B8G8R8A8_UNORM;
BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
CPUAccessFlags = 0;
MiscFlags = D3D11_RESOURCE_MISC_GDI_COMPATIBLE

Then I created a normal RenderTargetView from this texture as Microsoft sais here: http://msdn.microsoft.com/en-us/library/ff476203%28v=vs.85%29.aspx

Next Step: Get The DXGI Interface:

m_pTexFSText->QueryInterface(__uuidof(IDXGISurface1), (void **)(&m_pDXGISurface));

On the Render function I do just this:

m_pDeviceContext->OMSetRenderTargets(1,&m_pTextRenderTarget,NULL);

HDC hDc = NULL;
if(FAILED(m_pDXGISurface->GetDC(TRUE,&hDc)))
    return E_FAIL;

COLORREF bla = SetPixel(hDc,1,1,RGB(255,255,255));
bool hmm = TextOutA(hDc, 10, 10, "LALALA!", 7);

if(FAILED(m_pDXGISurface->ReleaseDC(NULL)))
    return E_FAIL;

The problem is, that the texture is still empty after that GDI drawing (Also tested with PIX).Everything works and there are no error messages.

I hope that anybody can explain how it works.

Thanks, Stefan

EDIT: Tried it also with GetDC(FALSE,&hDc) (according to the documentation): same results -> nothing.

解决方案

I actually fought this problem a lot during last week - but I've got it all working! Here is a list of things you should know/do to make it all work:

  • If you're going to use it in the back buffer, remember to re-bind render target after you've called ReleaseDC. It is not neccessary to manually unbind RT before calling GetDC as this method does that for you.

  • You can not use any Direct3D drawing between GetDC() and ReleaseDC() calls as the surface is excusively locked out by DXGI for GDI. However you can mix GDI and D3D rendering provided that you call GetDC()/ReleaseDC() every time you need to use GDI, before moving on to D3D.

  • This last bit may sounds easy, but you'd be surprised how many developers fall into this issue - when you draw with GDI on the back buffer, remember that this is the back buffer, not a framebuffer, so in order to actually see what you've drawn, you have to re-bind RT to OM and call swapChain->Present() method so the backbuffer will become a framebuffer and its contents will be displayed on the screen.

这篇关于D3D11:如何绘制GDI文本到GXDI表面? (无D2D)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 23:12