c++ - 使用相同的 HWND 时,D3D11CreateDeviceAndSwapChain 失败并出现 E_ACCESSDENIED

标签 c++ directx

如果我创建一个窗口并将 HWND 传递给 D3D11CreateDeviceAndSwapChain,它就可以工作。但是,在我释放设备、上下文、交换链等并尝试使用相同的 HWND 重复该过程后,D3D11CreateDeviceAndSwapChain 失败并显示 E_ACCESSDENIED。这告诉我一定有什么东西保留在 HWND 上,但是什么呢?我在类的析构函数中释放了所有全局变量。有人知道问题出在哪里吗?

~decoder()
    {
        m_VertexShader->Release();
        m_VertexShader = nullptr;

        m_PixelShader->Release();
        m_PixelShader = nullptr;

        m_InputLayout->Release();
        m_InputLayout = nullptr;

        device->Release();
        device = nullptr;

        context->Release();
        context = nullptr;

        swapchain->Release();
        swapchain = nullptr;

        rendertargetview->Release();
        rendertargetview = nullptr;

        m_SamplerLinear->Release();
        m_SamplerLinear = nullptr;

        HRESULT hr = S_OK;
        hr = decoder_transform->ProcessMessage(MFT_MESSAGE_NOTIFY_END_OF_STREAM, NULL);
        hr = decoder_transform->ProcessMessage(MFT_MESSAGE_NOTIFY_END_STREAMING, NULL);
        hr = decoder_transform->ProcessMessage(MFT_MESSAGE_COMMAND_FLUSH, NULL);

        decoder_transform.Release();
        color_transform.Release();

        hr = MFShutdown();
    }

最佳答案

同时D3D11CreateDeviceAndSwapChain文档中没有提及为什么会发生这种情况,它本质上只是创建 D3D11Device 和交换链的包装器。 IDXGIFactory2::CreateSwapChainForHwnd 的文档确实详细说明了为什么会发生这种情况。

Because you can associate only one flip presentation model swap chain at a time with an HWND, the Microsoft Direct3D 11 policy of deferring the destruction of objects can cause problems if you attempt to destroy a flip presentation model swap chain and replace it with another swap chain. For more info about this situation, see Deferred Destruction Issues with Flip Presentation Swap Chains.

有关 Deferred Destruction Issues with Flip Presentation Swap Chains 的文档建议调用ID3D11DeviceContext::ClearState接下来是 ID3D11DeviceContext::Flush .

However, if an application must actually destroy an old swap chain and create a new swap chain, the application must force the destruction of all objects that the application freed. To force the destruction, call ID3D11DeviceContext::ClearState (or otherwise ensure no views are bound to pipeline state), and then call Flush on the immediate context. You must force destruction before you call IDXGIFactory2::CreateSwapChainForHwnd, IDXGIFactory2::CreateSwapChainForCoreWindow, or IDXGIFactory2::CreateSwapChainForComposition again to create a new swap chain.

关于c++ - 使用相同的 HWND 时,D3D11CreateDeviceAndSwapChain 失败并出现 E_ACCESSDENIED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71515127/

相关文章:

directx - 使用 DirectX 的桌面捕获不起作用

opengl - XNA, 直接 X , OpenGL

C++重新分配指针然后将其删除

c++ - make_heap 和排序 x 和 y 坐标

c++ - 如何使用 GLFW 在 openGL 中旋转相机?

c++ - 是否可以从非 XYZ 顶点缓冲区在 GPU 上构建顶点?

c++ - 通过 HLSL C++ 时常量缓冲区为空

c++ - 如何通过 C++ FFI 从 Prolog 获取结果到 C++ 变量中

c++ - Sin 和 Cos 为众所周知的角度提供意想不到的结果

Mono 中的 Objective-C