Fix some window messaging bugs for dx.

This commit is contained in:
jmsgrogan 2023-01-08 12:41:09 +00:00
parent 55ed0e9299
commit 39422838b7
13 changed files with 237 additions and 68 deletions

View file

@ -104,10 +104,60 @@ LRESULT CALLBACK Win32Window::WindowProc(UINT message, WPARAM wParam, LPARAM lPa
auto keyChar = (wchar_t)wParam;
key_event->setKeyString(StringUtils::convert(std::wstring(1, keyChar)));
mDesktopManager->onUiEvent(std::move(key_event));
return 0;
}
case WM_PAINT:
{
onPaintMessage();
return 0;
}
case WM_SIZE:
{
auto width = LOWORD(lParam);
auto height = HIWORD(lParam);
MLOG_INFO("WM_SIZE: " << width << " | " << height);
mWindow->setSize(width, height, false);
if (wParam == SIZE_MINIMIZED)
{
mWindow->setDisplayState(WindowState::Display::MINIMIZED);
}
else if (wParam == SIZE_MAXIMIZED)
{
mWindow->setDisplayState(WindowState::Display::MAXIMIZED);
mWindow->onResize();
}
else if (wParam == SIZE_RESTORED)
{
if (mWindow->getDisplayState() == WindowState::Display::MINIMIZED)
{
mWindow->setDisplayState(WindowState::Display::NORMAL);
mWindow->onResize();
}
else if (mWindow->getDisplayState() == WindowState::Display::MAXIMIZED)
{
mWindow->setDisplayState(WindowState::Display::NORMAL);
mWindow->onResize();
}
else if (!mWindow->getIsSizingOnGoing())
{
mWindow->onResize();
}
}
return 0;
}
case WM_ENTERSIZEMOVE:
{
MLOG_INFO("WM_ENTERSIZEMOVE");
mWindow->setIsSizingOnGoing(true);
return 0;
}
case WM_EXITSIZEMOVE:
{
MLOG_INFO("WM_EXITSIZEMOVE");
mWindow->setIsSizingOnGoing(false);
mWindow->onResize();
return 0;
}
}
return DefWindowProc(mHandle, message, wParam, lParam);
@ -171,4 +221,12 @@ void Win32Window::afterPaint(mt::Screen* screen)
EndPaint(mHandle, &ps);
}
}
void Win32Window::onResize(unsigned width, unsigned height)
{
if (mDxInterface)
{
mDxInterface->onResize();
}
}

View file

@ -43,7 +43,7 @@ public:
void clear() {};
void onResize(unsigned width, unsigned height) {};
void onResize(unsigned width, unsigned height);
void beforePaint(mt::Screen* screen);

View file

@ -63,6 +63,9 @@ void DirectX2dIntegration::clearBuffers()
buffer.Reset();
}
mD2dRenderTargets.clear();
mDxInterface->getD2dContext()->SetTarget(nullptr);
mDxInterface->getD3d11DeviceContext()->Flush();
}
void DirectX2dIntegration::wrapBuffer(ID3D12Resource* renderTarget)

View file

@ -2,6 +2,8 @@
#include "DirectXDescriptors.h"
#include "FileLogger.h"
#include <dxgi.h>
#include <dxgi1_6.h>
#include <d3d12.h>
@ -17,10 +19,17 @@ void DirectXBuffers::clear()
{
for (auto& buffer : mBackBuffers)
{
buffer.Reset();
auto count = buffer.Reset();
}
mBackBuffers.clear();
mDepthStencilBuffer.Reset();
mInitialized = false;
}
bool DirectXBuffers::isInitialized()
{
return mInitialized;
}
UINT DirectXBuffers::getCurrentBackBufferIndex() const
@ -57,8 +66,11 @@ void DirectXBuffers::setupBackBuffers(ID3D12Device* device, IDXGISwapChain4* swa
swapChain->GetBuffer(i, IID_PPV_ARGS(&mBackBuffers[mBackBuffers.size()-1]));
device->CreateRenderTargetView(mBackBuffers[mBackBuffers.size() - 1].Get(), nullptr, rtv_heap_handle);
rtv_heap_handle.Offset(1, descriptors->getRtvDescriptorSize());
}
mInitialized = true;
}
void DirectXBuffers::setupDepthStencilHeap(ID3D12Device* device, unsigned width, unsigned height)

View file

@ -23,6 +23,8 @@ public:
ID3D12Resource* getDepthStencilBuffer() const;
bool isInitialized();
void setupBackBuffers(ID3D12Device* device, IDXGISwapChain4* swapChain, DirectXDescriptors* descriptors);
void setupDepthStencilHeap(ID3D12Device* device, unsigned width, unsigned height);
@ -32,6 +34,7 @@ public:
void updateBackbufferIndex(UINT index);
private:
bool mInitialized{ false };
UINT mFrameCount{ 2 };
UINT mBackBufferIndex{ 0 };
std::vector<Microsoft::WRL::ComPtr<ID3D12Resource> > mBackBuffers;

View file

@ -32,6 +32,6 @@ void DirectXCommandList::reset(ID3D12PipelineState* pso)
void DirectXCommandList::resetAllocator()
{
mCommandAllocator.Reset();
mCommandAllocator->Reset();
}

View file

@ -46,6 +46,13 @@ void Win32DxWindowInterface::onRender()
{
if (!mIsValid)
{
MLOG_INFO("Invalid state - returning");
return;
}
if (!mBuffers->isInitialized())
{
MLOG_INFO("Buffer not initialized - returning");
return;
}
@ -89,8 +96,8 @@ void Win32DxWindowInterface::updateCommandList()
painter->paintMesh(mCommandList->get());
// Indicate that the back buffer will now be used to present. (Exclude as handled by D2D integration later)
//barrier = CD3DX12_RESOURCE_BARRIER::Transition(mRenderTargets[mFrameIndex].Get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT);
//mCommandList->ResourceBarrier(1, &barrier);
//barrier = CD3DX12_RESOURCE_BARRIER::Transition(mBuffers->getCurrentBackBuffer(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT);
//mCommandList->get()->ResourceBarrier(1, &barrier);
mCommandList->close();
}
@ -102,6 +109,10 @@ bool Win32DxWindowInterface::initialize()
return true;
}
mInitialized = true;
MLOG_INFO("D3D Win init");
mD2dIntegration = std::make_unique<DirectX2dIntegration>(mWindow, mDxInterface, FrameCount);
mBuffers = std::make_unique<DirectXBuffers>(FrameCount);
if (!createSwapChain())
{
@ -115,8 +126,6 @@ bool Win32DxWindowInterface::initialize()
mDescriptors = std::make_unique<DirectXDescriptors>();
mDescriptors->setupDefaultDescriptorHeaps(mDxInterface->getD3dDevice());
mD2dIntegration = std::make_unique<DirectX2dIntegration>(mWindow, mDxInterface, FrameCount);
mBuffers = std::make_unique<DirectXBuffers>(FrameCount);
onResize();
@ -133,6 +142,9 @@ bool Win32DxWindowInterface::initialize()
void Win32DxWindowInterface::onResize()
{
MLOG_INFO("On resize");
mIsValid = false;
flushCommandQueue();
mCommandList->reset(getPipelineState());
@ -150,12 +162,15 @@ void Win32DxWindowInterface::onResize()
flushCommandQueue();
updateViewport();
mIsValid = true;
}
void Win32DxWindowInterface::resizeSwapChain()
{
const auto width = mWindow->getWidth();
const auto height = mWindow->getHeight();
MLOG_INFO("Resizing swap chain to: " << width << " | " << height);
mSwapChain->ResizeBuffers(FrameCount, width, height, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH);
updateBackbufferIndex(0);
}
@ -170,8 +185,8 @@ void Win32DxWindowInterface::updateViewport()
void Win32DxWindowInterface::clearBuffers()
{
mBuffers->clear();
mD2dIntegration->clearBuffers();
mBuffers->clear();
}
ID3D12PipelineState* Win32DxWindowInterface::getPipelineState() const
@ -182,6 +197,7 @@ ID3D12PipelineState* Win32DxWindowInterface::getPipelineState() const
void Win32DxWindowInterface::populateBuffers()
{
MLOG_INFO("Populating buffers");
auto d3d_device = mDxInterface->getD3dDevice();
mBuffers->setupBackBuffers(d3d_device, mSwapChain.Get(), mDescriptors.get());
for (UINT idx = 0; idx < FrameCount; idx++)
@ -191,11 +207,14 @@ void Win32DxWindowInterface::populateBuffers()
const auto width = mWindow->getWidth();
const auto height = mWindow->getHeight();
mBuffers->setupDepthStencilHeap(mDxInterface->getD3dDevice(), width, height);
mBuffers->setupDepthStencilView(mDxInterface->getD3dDevice(), mDescriptors.get());
if (width > 0 and height > 0)
{
mBuffers->setupDepthStencilHeap(mDxInterface->getD3dDevice(), width, height);
mBuffers->setupDepthStencilView(mDxInterface->getD3dDevice(), mDescriptors.get());
auto barrier = CD3DX12_RESOURCE_BARRIER::Transition(mBuffers->getDepthStencilBuffer(), D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_DEPTH_WRITE);
mCommandList->get()->ResourceBarrier(1, &barrier);
auto barrier = CD3DX12_RESOURCE_BARRIER::Transition(mBuffers->getDepthStencilBuffer(), D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_DEPTH_WRITE);
mCommandList->get()->ResourceBarrier(1, &barrier);
}
}
bool Win32DxWindowInterface::createSwapChain()