D3D11屏幕桌面复制到ID3D11Texture2D [英] D3D11 screen desktop copy to ID3D11Texture2D

查看:1301
本文介绍了D3D11屏幕桌面复制到ID3D11Texture2D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个DLL插件,该插件将读取桌面框架缓冲区(整个屏幕)并将其直接呈现为传入的Texture2D指针。目标是将所有内容保留在视频内存中(并避免复制的开销)

I am writing a DLL plugin that will read the desktop frame buffer (whole screen) and render it directly into a Texture2D pointer that is passed in. The goal is to keep everything in video memory (and avoid the cost of copying back to system memory and back to video memory).

我能够通过Texture2D(显示为ID3D11Texture2D),但是在抓取桌面框架缓冲区时遇到问题与D3D11。 D3D9提供了GetFrontBufferData(),但似乎D3D11解决方案是使用GetBuffer()。

I am able to pass the Texture2D (showing up as a ID3D11Texture2D), but I am having issues grabbing the desktop frame buffer with D3D11. D3D9 offered GetFrontBufferData() but it seems D3D11 solution is to use GetBuffer().

我的问题是获取IDXGISwapChain。因为我想读取桌面框架缓冲区,并仅通过GetBuffer()读取内容并将其显示在ID3D11Texture2D上。我正在获取一个ID3D11Device,但我不明白如何获取其IDXGISwapChain。

My issue is about getting the IDXGISwapChain. Because I want to read the desktop frame buffer and simply read the content via GetBuffer() and display it on the ID3D11Texture2D. I am getting a ID3D11Device I do not understand how to get its IDXGISwapChain.

我有以下代码来获取帧缓冲区并将其放在Texture上:

I have code as follows to get the frame buffer and put it on the Texture:

ID3D11Texture2D* src = (ID3D11Texture2D*)g_TexturePointer;
ID3D11Texture2D* dst = NULL;
HRESULT hr = swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&dst);
g_devCon->CopyResource(dst, src);

在这里,我实际上使用D3D11CreateDeviceAndSwapChain()创建了自己的交换链,但我想知道是否有必要,因为我已经

Here I actually created my own swapchain using D3D11CreateDeviceAndSwapChain() but I wonder if it is necessary as I already have a ID3D11Device.

CopyResource()也似乎失败。

The CopyResource() also seems to fail.

推荐答案

不一定所有桌面内容都将使用D3D11呈现。 DXGI是Windows上所有图形的基础系统,因此您肯定需要以某种方式使用它来捕获桌面。但是,D3D11构建在DXGI上(例如,ID3D11Texture2D支持IDXGIResource接口)。下面的代码示例显示了如何将整个监视器的输出捕获到D3D11过渡纹理中:

Not necessarily all desktop content would be rendered with D3D11. DXGI is the underlying system for all graphics on Windows, so you will definitely need to use it in some way to get a capture of the desktop. However, D3D11 is built on DXGI (for example, ID3D11Texture2D supports the IDXGIResource interface). The code sample below shows how you can capture the output of an entire monitor into a D3D11 staging texture:

// IDXGIOutput* poutput = ...; // from DXGIAdapter::EnumOutputs.

// Get description of desktop.
DXGI_OUTPUT_DESC outdesc;
poutput->GetDesc(&outdesc);

// Create destination texture, sized same as desktop.
D3D11_TEXTURE2D_DESC texDesc;
memset(&texDesc, 0, sizeof(texDesc));
texDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
texDesc.BindFlags = 0;
texDesc.Width  = outdesc.DesktopCoordinates.right - outdesc.DesktopCoordinates.left;
texDesc.Height = outdesc.DesktopCoordinates.bottom - outdesc.DesktopCoordinates.top;
texDesc.MipLevels = 1;
texDesc.SampleDesc = { 1, 0 };
texDesc.Usage = D3D11_USAGE_STAGING;
texDesc.ArraySize = 1;
ID3D11Texture2D* destinationTexture = 0;
pDevice->CreateTexture2D(&texDesc, 0, &destinationTexture); // check HRESULT.

// Get IDXGIResource from texture.
IDXGIResource* destinationResource=0;
destinationTexture->QueryInterface(IID_PPV_ARGS(&destinationResource)); // check HRESULT.

// Get data.
IDXGIOutput1* poutput1;
poutput->QueryInterface(IID_PPV_ARGS(&poutput1)); // check HRESULT.
poutput1->TakeOwnership(pDevice, TRUE);
poutput1->GetDisplaySurfaceData1(destinationResource); // check HRESULT.
poutput1->ReleaseOwnership();

// Now use destinationTexture, it contains the contents of the desktop.

不幸的是,它具有令人讨厌的副作用,在期间将输出变黑IDXGIOutput :: TakeOwnership 调用。但是,如果没有此调用, GetDiplaySurfaceData1 将失败。根据您的情况,这可能是可以接受的。

Unfortunately, it has the nasty side effect of turning the output black during the IDXGIOutput::TakeOwnership call. However, without this call, the GetDiplaySurfaceData1 will fail. Depending on your situation, this may be acceptable.

这篇关于D3D11屏幕桌面复制到ID3D11Texture2D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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