Skip to content

Commit 2c8c897

Browse files
Fixzed a bug that would crash the program when extraction resolution was too high.
1 parent ca60239 commit 2c8c897

File tree

3 files changed

+36
-26
lines changed

3 files changed

+36
-26
lines changed

SourceFiles/DeviceResources.cpp

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,8 @@ void DeviceResources::CreateDeviceResources()
250250
}
251251
}
252252

253-
void DeviceResources::UpdateOffscreenResources(int width, int height) {
253+
void DeviceResources::UpdateOffscreenResources(int width, int height, float aspecRatio) {
254254
m_d3dOffscreenRenderTargetView.Reset();
255-
//m_offscreenRenderTarget.Reset();
256255
m_d3dOffscreenDepthStencilView.Reset();
257256
m_offscreenDepthStencil.Reset();
258257
m_d3dContext->Flush();
@@ -271,29 +270,41 @@ void DeviceResources::UpdateOffscreenResources(int width, int height) {
271270
m_d3dDevice->CheckMultisampleQualityLevels(backBufferFormat, count, &qualityLevels);
272271
if (qualityLevels > 0) {
273272
msaaLevel = count;
274-
msaaQuality = qualityLevels - 1; // Highest quality level
273+
msaaQuality = qualityLevels - 1;
275274
break;
276275
}
277276
}
278277

279-
const int maxWidth = D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // Dx11 maximum texture dimension
280-
const int maxHeight = D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // Dx11 maximum texture dimension
281-
width = std::min(width, maxWidth);
282-
height = std::min(height, maxHeight);
278+
// Determine max texture dimension based on MSAA
279+
const int maxTextureDimension = (msaaLevel > 1) ? 8192 : D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION;
283280

281+
if (aspecRatio > 1.0f) {
282+
width = std::min(width, maxTextureDimension);
283+
height = width / aspecRatio;
284+
}
285+
else {
286+
height = std::min(height, maxTextureDimension);
287+
width = height * aspecRatio;
288+
}
289+
290+
width = std::min(width, maxTextureDimension);
291+
height = std::min(height, maxTextureDimension);
292+
293+
294+
// --- Rest of the code remains unchanged ---
284295
// Create the offscreen render target texture
285296
CD3D11_TEXTURE2D_DESC offscreenTextureDesc(
286-
backBufferFormat, // This format should match your requirements
287-
width, // Texture width
288-
height, // Texture height
289-
1, // Mip levels
290-
1, // Array size
291-
D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE, // Bind flags
297+
backBufferFormat,
298+
width,
299+
height,
300+
1,
301+
1,
302+
D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE,
292303
D3D11_USAGE_DEFAULT,
293304
0,
294-
msaaLevel, // MSAA sample count
295-
msaaQuality, // MSAA quality
296-
0 // Misc flags
305+
msaaLevel,
306+
msaaQuality,
307+
0
297308
);
298309
ThrowIfFailed(m_d3dDevice->CreateTexture2D(&offscreenTextureDesc, nullptr, &m_offscreenRenderTarget));
299310

@@ -305,7 +316,7 @@ void DeviceResources::UpdateOffscreenResources(int width, int height) {
305316
1,
306317
D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE
307318
);
308-
319+
309320
ThrowIfFailed(m_d3dDevice->CreateTexture2D(&nonMsaaDesc, nullptr, &m_offscreenNonMsaaTexture));
310321

311322
// Create a render target view for the offscreen render target
@@ -316,13 +327,13 @@ void DeviceResources::UpdateOffscreenResources(int width, int height) {
316327
m_depthBufferFormat,
317328
width,
318329
height,
319-
1, // This depth stencil view has only one texture.
320-
1, // Use a single mipmap level.
330+
1,
331+
1,
321332
D3D11_BIND_DEPTH_STENCIL,
322333
D3D11_USAGE_DEFAULT,
323334
0,
324-
msaaLevel, // MSAA sample count
325-
msaaQuality, // MSAA quality
335+
msaaLevel,
336+
msaaQuality,
326337
0
327338
);
328339

@@ -332,8 +343,7 @@ void DeviceResources::UpdateOffscreenResources(int width, int height) {
332343
CD3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc(D3D11_DSV_DIMENSION_TEXTURE2DMS);
333344
ThrowIfFailed(m_d3dDevice->CreateDepthStencilView(m_offscreenDepthStencil.Get(), &dsvDesc, &m_d3dOffscreenDepthStencilView));
334345

335-
m_offscreenViewport = { 0.0f, 0.0f, static_cast<float>(offscreenTextureDesc.Width), static_cast<float>(offscreenTextureDesc.Height),
336-
0.f, 1.f };
346+
m_offscreenViewport = { 0.0f, 0.0f, static_cast<float>(width), static_cast<float>(height), 0.f, 1.f };
337347
}
338348

339349

SourceFiles/DeviceResources.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace DX
3838
DeviceResources& operator= (DeviceResources const&) = delete;
3939

4040
void CreateDeviceResources();
41-
void UpdateOffscreenResources(int width, int height);
41+
void UpdateOffscreenResources(int width, int height, float aspecRatio);
4242
void CreateWindowSizeDependentResources();
4343
void SetWindow(HWND window, int width, int height) noexcept;
4444
bool WindowSizeChanged(int width, int height);

SourceFiles/MapBrowser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ void MapBrowser::Render()
335335
case ExtractPanel::AllMapsTopDownOrthographic:
336336
case ExtractPanel::CurrentMapTopDownOrthographic:
337337
{
338-
m_deviceResources->UpdateOffscreenResources(dim_x * m_extract_panel_info.pixels_per_tile_x, dim_z * m_extract_panel_info.pixels_per_tile_y);
338+
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);
339339
m_map_renderer->SetShouldRenderSky(false);
340340
m_map_renderer->SetShouldRenderFog(false);
341341
m_map_renderer->SetShouldRenderShadows(false);
@@ -346,7 +346,7 @@ void MapBrowser::Render()
346346
{
347347
int res_x = dim_x * m_extract_panel_info.pixels_per_tile_x;
348348
int res_y = res_x / m_map_renderer->GetCamera()->GetAspectRatio();
349-
m_deviceResources->UpdateOffscreenResources(res_x, res_y);
349+
m_deviceResources->UpdateOffscreenResources(res_x, res_y, (float)dim_x / dim_z);
350350
break;
351351
}
352352
default:

0 commit comments

Comments
 (0)