问题描述
我有一个简单的 Window 类,我使用 WM_PAINT 函数清除渲染目标视图并显示最终图像。所以没有太多内容,但我在行 FLOAT clearColor[] = { 0.2f,0.4f,0.6f,1.0f };
上崩溃了:0xC000041D :在用户回调期间遇到未处理的异常。
这是窗口类:
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,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,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,windowX,windowY,windowWidth,windowHeight,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,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,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,lParam);
}
return ::DefWindowProcW(hwnd,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 来自父类,还有 WindowProps 有一些用于初始化窗口的数据。 m_Context->RenderWindow() 的定义:
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,1.0f };
CD3DX12_cpu_DESCRIPTOR_HANDLE rtv(m_RTVDescriptorHeap->GetcpuDescriptorHandleForHeapStart(),m_CurrentBackBufferIndex,m_RTVDescriptorSize);
m_CommandList->ClearrendertargetView(rtv,clearColor,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]);
}
如果还有什么我应该提出的问题,请告诉我。如果您想帮助我,请为我的愚蠢大喊大叫,谢谢。 编辑: 调试层:
#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。