有关使用“ WGL_NV_DX_interop2”扩展将纹理从DX复制到GL的问题

时间:2019-07-07 16:39:58

标签: opengl directx textures

我的程序使用Windows桌面复制API进行屏幕捕获,捕获的图像存储在D3D11纹理中,所以我使用“ WGL_NV_DX_interop2”扩展名将D3D纹理复制到OpenGL纹理中,这是因为屏幕捕获任务和OpenGL渲染任务在不同的线程上运行,我创建一个名为“ tex_exchange”的D3D纹理,并将其注册为gl纹理对象,截屏任务将其渲染结果复制到tex_exchange,而OpenGL任务将tex_exchange的内容复制到gl纹理;但是我发现当OpenGL任务执行纹理复制时,屏幕捕获将报告错误,D3D api报告错误为ID3D11Device :: CreateShaderResourceView(),错误代码为0x887A0005,看来我的纹理复制代码有问题,所以我发布我程序的一些代码,希望对您有所帮助;

// the function to create intermedia texture "tex_exchange"
D3D11_TEXTURE2D_DESC desc;
RtlZeroMemory(&desc, sizeof(D3D11_TEXTURE2D_DESC));
desc.Width = desktopDup->m_dupDesc.ModeDesc.Width;
desc.Height = desktopDup->m_dupDesc.ModeDesc.Height;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = 0;
desc.CPUAccessFlags = 0;
desc.MiscFlags = i0;
HRESULT hr = desktopDup->m_device->CreateTexture2D(&desc, NULL, ppTex);
if (FAILED(hr))
{
    gLogger->WriteLog(LogLevel::error, "%s() fail for CreateTexture2D() fail, hr=%08X", __FUNCTION__, hr);
    assert(S_OK == hr);
    return false;
}


// the function to register tex_exchange in OpenGL
gl_DXDev = wglDXOpenDeviceNV(desktopDup->m_device.p);
if (!gl_DXDev)
{
    gLogger->WriteLog(LogLevel::error, "%s() fail for glDXDev is null, glError=%u", __FUNCTION__,glGetError());
    assert(gl_DXDev);
    return E_FAIL;
}

glGenTextures(1, &gl_tex_id);
if (gl_tex_id == 0)
{
    gLogger->WriteLog(LogLevel::error, "%s() gen gl texture fail, glError=%u", __FUNCTION__, glGetError());
    assert(gl_tex_id);
    return E_FAIL;
}
glBindTexture(GL_TEXTURE_2D, gl_tex_id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBindTexture(GL_TEXTURE_2D, 0);

gl_tex_obj = wglDXRegisterObjectNV(gl_DXDev, d3dtex_exchange.p, gl_tex_id, GL_TEXTURE_2D, WGL_ACCESS_READ_ONLY_NV);
if (!gl_tex_obj)
{
    gLogger->WriteLog(LogLevel::error, "%s() reg gl texture fail,errCode=%u", __FUNCTION__, GetLastError());
    assert(gl_tex_obj);
    return E_FAIL;
}

// the function to copy D3D Desktop Duplication rendering result to tex_exchange
desktopDup->m_context->CopyResource(d3dtex_exchange, desktopSurf);

// the function to copy tex_exchange to OpenGL
if (!wglDXLockObjectsNV(gl_DXDev, 1, &gl_tex_obj))  // when the function is called, DX task will report a error
{
    gLogger->WriteLog(LogLevel::error, "%s() wglDXLockObjectsNV() fail,errCode=%u", __FUNCTION__, GetLastError());
    assert(FALSE);
    return;
}

if (!wglDXUnlockObjectsNV(gl_DXDev, 1, &gl_tex_obj))
{
    gLogger->WriteLog(LogLevel::error, "%s() wglDXUnlockObjectsNV() fail,errCode=%u", __FUNCTION__, GetLastError());
    assert(FALSE);
    return;
}

0 个答案:

没有答案