D3D11:如何绘制GDI文本到GXDI表面? (无D2D) [英] D3D11: How to draw GDI Text to a GXDI Surface? (Without D2D)

查看:1198
本文介绍了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, http://msdn.microsoft.com/en-us/library/ ff476203%28v = vs.85%29.aspx



下一步:获取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得到一切工作!下面是一些你应该知道/做的事情,使它一切正常:






使用此方法时,请记住以下几点:



•您必须使用表面的D3D11_RESOURCE_MISC_GDI_COMPATIBLE标志创建曲面, DXGI_SWAP_CHAIN_FLAG_GDI_COMPATIBLE标志用于交换链,否则此方法将失败。



•在发出任何新的Direct3D命令之前,必须释放设备并调用IDXGISurface1 :: ReleaseDC方法。 p>

•如果此方法已创建未完成的DC,此方法将失败。



•表面的格式或交换链必须是DXGI_FORMAT_B8G8R8A8_UNORM_SRGB或DXGI_FORMAT_B8G8R8A8_UNORM。



•在GetDC上,Direct3D管道的输出合并中的渲染目标从表面解除绑定。您必须在GDI渲染之前在Direct3D渲染之前调用设备上的ID3D11DeviceContext :: OMSetRenderTargets方法。



•调整缓冲区大小之前,必须释放所有未完成的DC。 >




  • 如果要在后台缓冲区中使用它,请记住重新绑定渲染目标之后您呼叫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:

Keep the following in mind when using this method:

•You must create the surface by using the D3D11_RESOURCE_MISC_GDI_COMPATIBLE flag for a surface or by using the DXGI_SWAP_CHAIN_FLAG_GDI_COMPATIBLE flag for swap chains, otherwise this method fails.

•You must release the device and call the IDXGISurface1::ReleaseDC method before you issue any new Direct3D commands.

•This method fails if an outstanding DC has already been created by this method.

•The format for the surface or swap chain must be DXGI_FORMAT_B8G8R8A8_UNORM_SRGB or DXGI_FORMAT_B8G8R8A8_UNORM.

•On GetDC, the render target in the output merger of the Direct3D pipeline is unbound from the surface. You must call the ID3D11DeviceContext::OMSetRenderTargets method on the device prior to Direct3D rendering after GDI rendering.

•Prior to resizing buffers you must release all outstanding DCs.

  • 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆