From 2c8c897e89d7febf4433e20e6f73dfad4b61e59b Mon Sep 17 00:00:00 2001 From: Jonathan Greve Date: Wed, 5 Feb 2025 21:41:25 +0100 Subject: [PATCH] Fixzed a bug that would crash the program when extraction resolution was too high. --- SourceFiles/DeviceResources.cpp | 56 +++++++++++++++++++-------------- SourceFiles/DeviceResources.h | 2 +- SourceFiles/MapBrowser.cpp | 4 +-- 3 files changed, 36 insertions(+), 26 deletions(-) diff --git a/SourceFiles/DeviceResources.cpp b/SourceFiles/DeviceResources.cpp index a9d771c..a2980a2 100644 --- a/SourceFiles/DeviceResources.cpp +++ b/SourceFiles/DeviceResources.cpp @@ -250,9 +250,8 @@ void DeviceResources::CreateDeviceResources() } } -void DeviceResources::UpdateOffscreenResources(int width, int height) { +void DeviceResources::UpdateOffscreenResources(int width, int height, float aspecRatio) { m_d3dOffscreenRenderTargetView.Reset(); - //m_offscreenRenderTarget.Reset(); m_d3dOffscreenDepthStencilView.Reset(); m_offscreenDepthStencil.Reset(); m_d3dContext->Flush(); @@ -271,29 +270,41 @@ void DeviceResources::UpdateOffscreenResources(int width, int height) { m_d3dDevice->CheckMultisampleQualityLevels(backBufferFormat, count, &qualityLevels); if (qualityLevels > 0) { msaaLevel = count; - msaaQuality = qualityLevels - 1; // Highest quality level + msaaQuality = qualityLevels - 1; break; } } - const int maxWidth = D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // Dx11 maximum texture dimension - const int maxHeight = D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // Dx11 maximum texture dimension - width = std::min(width, maxWidth); - height = std::min(height, maxHeight); + // Determine max texture dimension based on MSAA + const int maxTextureDimension = (msaaLevel > 1) ? 8192 : D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; + if (aspecRatio > 1.0f) { + width = std::min(width, maxTextureDimension); + height = width / aspecRatio; + } + else { + height = std::min(height, maxTextureDimension); + width = height * aspecRatio; + } + + width = std::min(width, maxTextureDimension); + height = std::min(height, maxTextureDimension); + + + // --- Rest of the code remains unchanged --- // Create the offscreen render target texture CD3D11_TEXTURE2D_DESC offscreenTextureDesc( - backBufferFormat, // This format should match your requirements - width, // Texture width - height, // Texture height - 1, // Mip levels - 1, // Array size - D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE, // Bind flags + backBufferFormat, + width, + height, + 1, + 1, + D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE, D3D11_USAGE_DEFAULT, 0, - msaaLevel, // MSAA sample count - msaaQuality, // MSAA quality - 0 // Misc flags + msaaLevel, + msaaQuality, + 0 ); ThrowIfFailed(m_d3dDevice->CreateTexture2D(&offscreenTextureDesc, nullptr, &m_offscreenRenderTarget)); @@ -305,7 +316,7 @@ void DeviceResources::UpdateOffscreenResources(int width, int height) { 1, D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE ); - + ThrowIfFailed(m_d3dDevice->CreateTexture2D(&nonMsaaDesc, nullptr, &m_offscreenNonMsaaTexture)); // Create a render target view for the offscreen render target @@ -316,13 +327,13 @@ void DeviceResources::UpdateOffscreenResources(int width, int height) { m_depthBufferFormat, width, height, - 1, // This depth stencil view has only one texture. - 1, // Use a single mipmap level. + 1, + 1, D3D11_BIND_DEPTH_STENCIL, D3D11_USAGE_DEFAULT, 0, - msaaLevel, // MSAA sample count - msaaQuality, // MSAA quality + msaaLevel, + msaaQuality, 0 ); @@ -332,8 +343,7 @@ void DeviceResources::UpdateOffscreenResources(int width, int height) { CD3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc(D3D11_DSV_DIMENSION_TEXTURE2DMS); ThrowIfFailed(m_d3dDevice->CreateDepthStencilView(m_offscreenDepthStencil.Get(), &dsvDesc, &m_d3dOffscreenDepthStencilView)); - m_offscreenViewport = { 0.0f, 0.0f, static_cast(offscreenTextureDesc.Width), static_cast(offscreenTextureDesc.Height), - 0.f, 1.f }; + m_offscreenViewport = { 0.0f, 0.0f, static_cast(width), static_cast(height), 0.f, 1.f }; } diff --git a/SourceFiles/DeviceResources.h b/SourceFiles/DeviceResources.h index 4e91324..13bd5d0 100644 --- a/SourceFiles/DeviceResources.h +++ b/SourceFiles/DeviceResources.h @@ -38,7 +38,7 @@ namespace DX DeviceResources& operator= (DeviceResources const&) = delete; void CreateDeviceResources(); - void UpdateOffscreenResources(int width, int height); + void UpdateOffscreenResources(int width, int height, float aspecRatio); void CreateWindowSizeDependentResources(); void SetWindow(HWND window, int width, int height) noexcept; bool WindowSizeChanged(int width, int height); diff --git a/SourceFiles/MapBrowser.cpp b/SourceFiles/MapBrowser.cpp index 0714f2a..1da55fa 100644 --- a/SourceFiles/MapBrowser.cpp +++ b/SourceFiles/MapBrowser.cpp @@ -335,7 +335,7 @@ void MapBrowser::Render() case ExtractPanel::AllMapsTopDownOrthographic: case ExtractPanel::CurrentMapTopDownOrthographic: { - m_deviceResources->UpdateOffscreenResources(dim_x * m_extract_panel_info.pixels_per_tile_x, dim_z * m_extract_panel_info.pixels_per_tile_y); + m_deviceResources->UpdateOffscreenResources(dim_x * m_extract_panel_info.pixels_per_tile_x, dim_z * m_extract_panel_info.pixels_per_tile_y, (float)dim_x / dim_z); m_map_renderer->SetShouldRenderSky(false); m_map_renderer->SetShouldRenderFog(false); m_map_renderer->SetShouldRenderShadows(false); @@ -346,7 +346,7 @@ void MapBrowser::Render() { int res_x = dim_x * m_extract_panel_info.pixels_per_tile_x; int res_y = res_x / m_map_renderer->GetCamera()->GetAspectRatio(); - m_deviceResources->UpdateOffscreenResources(res_x, res_y); + m_deviceResources->UpdateOffscreenResources(res_x, res_y, (float)dim_x / dim_z); break; } default: