Skip to content

Commit

Permalink
Fixzed a bug that would crash the program when extraction resolution …
Browse files Browse the repository at this point in the history
…was too high.
  • Loading branch information
Jonathan-Greve committed Feb 5, 2025
1 parent ca60239 commit 2c8c897
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
56 changes: 33 additions & 23 deletions SourceFiles/DeviceResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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));

Expand All @@ -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
Expand All @@ -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
);

Expand All @@ -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<float>(offscreenTextureDesc.Width), static_cast<float>(offscreenTextureDesc.Height),
0.f, 1.f };
m_offscreenViewport = { 0.0f, 0.0f, static_cast<float>(width), static_cast<float>(height), 0.f, 1.f };
}


Expand Down
2 changes: 1 addition & 1 deletion SourceFiles/DeviceResources.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions SourceFiles/MapBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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:
Expand Down

0 comments on commit 2c8c897

Please sign in to comment.