在简单的 directX12 应用程序中获取 0xC000041D 崩溃

Getting 0xC000041D crash in simple directX12 application

我有一个简单的 Window class 并且我为 WM_PAINT 使用了一个函数来清除渲染目标视图并呈现最终的 image.so 没有太多内容但我在 FLOAT clearColor[] = { 0.2f, 0.4f, 0.6f, 1.0f }; 行发生崩溃 says:0xC000041D:在用户回调期间遇到未处理的异常。 这是 window class:

class Win32Window : public WindowsWindow
{
public:
    Win32Window(const WindowProps& props);
    virtual ~Win32Window();
    void OnUpdate() override;
    unsigned int GetWidth() const override { return m_Data.Width; }
    unsigned int GetHeight() const override { return m_Data.Height; }
    void SetEventCallback(const EventCallbackFn& callback) override { m_Data.EventCallback = callback; }
    void SetVSync(bool enabled) override;
    bool IsVSync() const override;
    virtual void* GetNativeWindow() const override { return m_Window; }
private:
    HWND m_Window;
    RECT m_WindowRect;
    HINSTANCE m_hInst;
    LRESULT CALLBACK MainWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
    static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
    virtual void Init(const WindowProps& props);
    virtual void Shutdown();
};

定义:

Win32Window::Win32Window(const WindowProps& props)
{
    HZ_PROFILE_FUNCTION();
    Init(props);
}
Win32Window::~Win32Window()
{
    HZ_PROFILE_FUNCTION();
    Shutdown();
}
void Win32Window::Init(const WindowProps& props)
{
    m_hInst = GetModuleHandle(0);
    m_Data.WideCharacterTitle = props.WideCharacterTitle;
    m_Data.Width = props.Width;
    m_Data.Height = props.Height;
    HZ_CORE_INFO("Creating window {0} ({1}, {2})",props.StringTypeTitle, props.Width, props.Height);

    WNDCLASSEXW windowClass = {};
    windowClass.cbSize = sizeof(WNDCLASSEX);
    windowClass.style = CS_HREDRAW | CS_VREDRAW;
    windowClass.lpfnWndProc = &WndProc;
    windowClass.cbClsExtra = 0;
    windowClass.cbWndExtra = 0;
    windowClass.hInstance = m_hInst;
    windowClass.hIcon = ::LoadIcon(m_hInst, NULL);
    windowClass.hCursor = ::LoadCursor(NULL, IDC_ARROW);
    windowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    windowClass.lpszMenuName = NULL;
    windowClass.lpszClassName = m_Data.WideCharacterTitle;
    windowClass.hIconSm = ::LoadIcon(m_hInst, NULL);
    static ATOM atom = ::RegisterClassExW(&windowClass);
    assert(atom > 0);

    int screenWidth = ::GetSystemMetrics(SM_CXSCREEN);
    int screenHeight = ::GetSystemMetrics(SM_CYSCREEN);
    RECT windowRect = { 0, 0, static_cast<LONG>(m_Data.Width), static_cast<LONG>(m_Data.Height) };
    ::AdjustWindowRect(&windowRect, WS_OVERLAPPEDWINDOW, FALSE);
    int windowWidth = windowRect.right - windowRect.left;
    int windowHeight = windowRect.bottom - windowRect.top;
    int windowX = std::max<int>(0, (screenWidth - windowWidth) / 2);
    int windowY = std::max<int>(0, (screenHeight - windowHeight) / 2);
    m_Window = ::CreateWindowExW(NULL, m_Data.WideCharacterTitle, m_Data.WideCharacterTitle, WS_OVERLAPPEDWINDOW, windowX, windowY, windowWidth, windowHeight, NULL, NULL, m_hInst, this);
    assert(m_Window && "Failed to create window");
    m_Context = GraphicsContext::Create(m_Window);
    m_Context->Init();
    ::ShowWindow(m_Window, SW_SHOW);
    ::UpdateWindow(m_Window);
}
LRESULT Win32Window::MainWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_PAINT:
       m_Context->RenderWindow();
        break;
    case WM_DESTROY:
        ::PostQuitMessage(0);
        break;
    default:
        return ::DefWindowProcW(hwnd, message, wParam, lParam);
    }
}
LRESULT Win32Window::WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    Win32Window* instance;
    if (message == WM_CREATE)
    {
        instance = (Win32Window*)(((LPCREATESTRUCT)lParam)->lpCreateParams);
        instance->m_Window = hwnd;
        SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)instance);
    }
    else
    {
        instance = (Win32Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
    }
    if (instance) 
    {
        return instance->MainWndProc(hwnd, message, wParam, lParam);
    }
    return ::DefWindowProcW(hwnd, message, wParam, lParam);
}
void Win32Window::Shutdown()
{
    DestroyWindow(m_Window);
    m_Window = nullptr;
}
void Win32Window::OnUpdate()
{
    m_Context->Update();
}
void Win32Window::SetVSync(bool enabled)
{
    m_Data.VSync = enabled;
}
bool Win32Window::IsVSync() const
{
    return m_Data.VSync;
}

m_Context 来自父 class,还有 WindowProps 有一些数据用于初始化 window .definition of m_Context- >渲染Window():

m_CommnadQueue = D3D12Core::Get().GetCommandQueue(D3D12_COMMAND_LIST_TYPE_DIRECT);
    auto m_CommandList = m_CommnadQueue->GetCommandList();
    m_CurrentBackBufferIndex = m_SwapChain->GetCurrentBackBufferIndex();
    auto m_BackBuffer = D3D12Core::Get().m_BackBuffers[m_CurrentBackBufferIndex];
    auto m_RTVDescriptorSize = D3D12Core::Get().GetDevice()->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
    {
        CD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(m_BackBuffer.Get(), D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET);
        m_CommandList->ResourceBarrier(1, &barrier);
        FLOAT clearColor[] = { 0.2f, 0.4f, 0.6f, 1.0f };
        CD3DX12_CPU_DESCRIPTOR_HANDLE rtv(m_RTVDescriptorHeap->GetCPUDescriptorHandleForHeapStart(), m_CurrentBackBufferIndex, m_RTVDescriptorSize);
        m_CommandList->ClearRenderTargetView(rtv, clearColor, 0, nullptr);
    }
    {
        CD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(m_BackBuffer.Get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT);
        m_CommandList->ResourceBarrier(1, &barrier);
        m_CommandList->Close();
        ID3D12CommandList* const commandLists[] =
        {
            m_CommandList.Get()
        };
        D3D12Core::Get().m_FenceValues[m_CurrentBackBufferIndex] = m_CommnadQueue->ExecuteCommandList(m_CommandList);
        UINT syncInterval = m_VSync;
        UINT presentFlags = m_TearingSupport && !m_VSync ? DXGI_PRESENT_ALLOW_TEARING : 0;
        m_SwapChain->Present(syncInterval, presentFlags);
        m_CommnadQueue->WaitForFenceValue(D3D12Core::Get().m_FenceValues[m_CurrentBackBufferIndex]);
    }

如果还有什么我要问的,请告诉 me.You 如果你愿意,可以对我大喊大叫,但请帮助我,thx。 编辑: 调试层:

#ifdef _DEBUG
        ComPtr<ID3D12Debug> debugInterface;
        if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugInterface))))
        {
            debugInterface->EnableDebugLayer();
        }
#endif

WaitForFenceValue 在每个 Present 之后是在 DirectX 12 中渲染的一种极其缓慢的方式。您通常应该向 GPU 提示 2-3 帧,而不是强制 GPU 停止每一帧。让 WndProc 阻塞也很成问题,这就是这里发生的情况。您的 WndProc 应该总是很快完成,因为大多数应用程序每秒处理数百条消息。

通常 Direct3D 渲染不会在 WM_PAINT 内完成。有关常见渲染循环的一些示例,请参阅 GitHub