Fix new editor window resize presentation

This commit is contained in:
2026-04-13 12:20:25 +08:00
parent adb6fe4659
commit 0cc3d6da46
5 changed files with 1019 additions and 78 deletions

View File

@@ -25,12 +25,17 @@ public:
uint32_t GetCurrentBackBufferIndex() const override;
RHITexture* GetCurrentBackBuffer() override;
D3D12Texture* TryGetBackBuffer(uint32_t index);
const D3D12Texture* TryGetBackBuffer(uint32_t index) const;
D3D12Texture& GetBackBuffer(uint32_t index);
const D3D12Texture& GetBackBuffer(uint32_t index) const;
ID3D12CommandQueue* GetNativeCommandQueue() const { return m_commandQueue.Get(); }
void Present(uint32_t syncInterval = 1, uint32_t flags = 0) override;
void Resize(uint32_t width, uint32_t height) override;
void* GetNativeHandle() override;
HRESULT GetLastResizeResult() const { return m_lastResizeResult; }
uint32_t GetWidth() const { return m_width; }
uint32_t GetHeight() const { return m_height; }
private:
bool RefreshBackBuffers();
@@ -42,6 +47,7 @@ private:
uint32_t m_height;
uint32_t m_bufferCount;
std::vector<D3D12Texture> m_backBuffers;
HRESULT m_lastResizeResult = S_OK;
};
} // namespace RHI

View File

@@ -42,6 +42,7 @@ bool D3D12SwapChain::Initialize(IDXGIFactory4* factory, ID3D12CommandQueue* comm
m_width = width;
m_height = height;
m_bufferCount = bufferCount;
m_lastResizeResult = S_OK;
return RefreshBackBuffers();
}
@@ -61,6 +62,7 @@ bool D3D12SwapChain::Initialize(IDXGISwapChain* swapChain, uint32_t width, uint3
m_width = width;
m_height = height;
m_bufferCount = desc.BufferCount;
m_lastResizeResult = S_OK;
return RefreshBackBuffers();
}
@@ -71,20 +73,31 @@ void D3D12SwapChain::Shutdown() {
m_swapChain.Reset();
m_width = 0;
m_height = 0;
m_lastResizeResult = S_OK;
}
uint32_t D3D12SwapChain::GetCurrentBackBufferIndex() const {
return m_swapChain->GetCurrentBackBufferIndex();
}
D3D12Texture* D3D12SwapChain::TryGetBackBuffer(uint32_t index) {
return index < m_backBuffers.size() ? &m_backBuffers[index] : nullptr;
}
const D3D12Texture* D3D12SwapChain::TryGetBackBuffer(uint32_t index) const {
return index < m_backBuffers.size() ? &m_backBuffers[index] : nullptr;
}
D3D12Texture& D3D12SwapChain::GetBackBuffer(uint32_t index) {
assert(index < m_backBuffers.size() && "BackBuffer index out of range");
return m_backBuffers[index];
D3D12Texture* backBuffer = TryGetBackBuffer(index);
assert(backBuffer != nullptr && "BackBuffer index out of range");
return *backBuffer;
}
const D3D12Texture& D3D12SwapChain::GetBackBuffer(uint32_t index) const {
assert(index < m_backBuffers.size() && "BackBuffer index out of range");
return m_backBuffers[index];
const D3D12Texture* backBuffer = TryGetBackBuffer(index);
assert(backBuffer != nullptr && "BackBuffer index out of range");
return *backBuffer;
}
void D3D12SwapChain::Present(uint32_t syncInterval, uint32_t flags) {
@@ -92,10 +105,26 @@ void D3D12SwapChain::Present(uint32_t syncInterval, uint32_t flags) {
}
void D3D12SwapChain::Resize(uint32_t width, uint32_t height) {
if (!m_swapChain) {
m_lastResizeResult = E_POINTER;
return;
}
const uint32_t previousWidth = m_width;
const uint32_t previousHeight = m_height;
ReleaseBackBuffers();
const HRESULT hResult = m_swapChain->ResizeBuffers(m_bufferCount, width, height, DXGI_FORMAT_R8G8B8A8_UNORM, 0);
const HRESULT hResult = m_swapChain->ResizeBuffers(
0,
width,
height,
DXGI_FORMAT_UNKNOWN,
0);
m_lastResizeResult = hResult;
if (FAILED(hResult)) {
m_width = previousWidth;
m_height = previousHeight;
RefreshBackBuffers();
return;
}