开发者

D3DX11SaveTextureToFile with shared resources

I'm using shared resources to send textures from one D3D11 device to another, so that I can copy the back buffer over the second device and use the second device context to save that texture to a file. This seems to be working, but when I save the texture, it saves an empty PNG. I tried saving the texture with the primary device context and it works, except if I use the D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX MiscFlag, whic开发者_Go百科h I need in order to share the resources. Is there any reason D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX prevents a texture from being saved ? Or am I maybe missing something out in order for it to work ?

Here is the code I'm using:

D3D11_TEXTURE2D_DESC td;
backBuffer->GetDesc(&td);
td.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
this->_device->CreateTexture2D(&td, 0, &g_tex);
this->_context->CopyResource(g_tex, backBuffer);

// saves a blank image if using D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX
D3DX11SaveTextureToFile(this->_context, g_tex, D3DX11_IFF_JPG, "test.jpg");

g_tex->QueryInterface(__uuidof(IDXGIResource), reinterpret_cast<void **>(&g_dxgiResource));
g_dxgiResource->GetSharedHandle(&g_shaderHandle);
g_dxgiResource->Release();
g_tex->QueryInterface(__uuidof(IDXGIKeyedMutex), reinterpret_cast<void **>(&g_dxgiMutex));

And this is the code used to save the shared textue in the second device

ID3D11Texture2D *texture = 0;
IDXGIKeyedMutex *keyedMutex = 0;
device2->OpenSharedResource(g_shaderHandle, __uuidof(ID3D11Texture2D), reinterpret_cast<void **>(&texture));
texture->QueryInterface(__uuidof(IDXGIKeyedMutex), reinterpret_cast<void **>(&keyedMutex));
UINT acqKey = 0;
UINT relKey = 1;
DWORD timeout = 16;
DWORD res = keyedMutex->AcquireSync(acqKey, timeout);
if (res == WAIT_OBJECT_0 && texture)
{
    // saves a blank image too
    D3DX10SaveTextureToFile(texture, D3DX10_IFF_JPG, "test2.jpg");
}
keyedMutex->ReleaseSync(relKey);

Also, the code that is supposed to save the shared texture to the filesystem is running in it's own thread.


Here is how I solved the problem. Turns out, using the D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX misc flag doesn't allow to save the texture to a file. So I used the D3D11_RESOURCE_MISC_SHARED flag instead with a regular mutex, without using the DXGIKeyedMutex stuff.

Copy the back buffer to shared texture and create a shared handle:

D3D11_TEXTURE2D_DESC td;
backBuffer->GetDesc(&td);
td.MiscFlags = D3D11_RESOURCE_MISC_SHARED;

this->_device->CreateTexture2D(&td, 0, &g_tex);
this->_context->CopyResource(g_tex, backBuffer);
IDXGIResource *dxgiResource = 0;
g_tex->QueryInterface(__uuidof(IDXGIResource), reinterpret_cast<void **>(&dxgiResource));
dxgiResource->GetSharedHandle(&g_shaderHandle);
dxgiResource->Release();
backBuffer->Release();

Now that we have a shared copy of the back buffer and handle to it, we can save it to a file from another thread without hogging the device context:

// device_2 and context_2 are the "secondary" device and context
bool imgSaved = true;
ID3D11Texture2D *texture = 0;
HRESULT h = WaitForSingleObject(g_mutex, INFINITE);
if (h == WAIT_OBJECT_0)
{
    // check to see if there is an image to save
    if (wdata->hasFrame)
    {
        wdata->hasFrame = false;
        imgSaved = false
    }
}
ReleaseMutex(g_mutex);
if (!imgSaved)
{
    device_2->OpenSharedResource(g_shaderHandle, __uuidof(ID3D11Texture2D), (LPVOID*)&texture);
    if (texture)
    {
        h = D3DX11SaveTextureToFile(context_2, texture, D3DX11_IFF_PNG, "image.png");
        texture->Release();
    }
}

Basically this allows me to save HD screen captures without killing the frame rate because my device context is not stuck in the D3DX11SaveTextureToFile function, it's handled by the secondary context. On a side note, I've not tested it a whole lot, I just hack this together this morning, it might not work in some cases, but it solves the problem I had with the other flag, which caused empty images to be saved to file.


Wouldn't it be more logical to CreateTexture2D and CopyResource from the _device that owns backBuffer?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜