From 7bd6d4649e575e0a2a5f046fdf0d687cea87ca84 Mon Sep 17 00:00:00 2001 From: aap Date: Thu, 16 Apr 2020 23:09:47 +0200 Subject: [PATCH] fixed handling of lost d3d device --- skeleton/skeleton.cpp | 6 +++++- src/d3d/d3d.cpp | 3 +-- src/d3d/d3ddevice.cpp | 36 ++++++++++++++++++++++++++++++++---- src/d3d/rwd3dimpl.h | 1 + tools/clumpview/main.cpp | 2 ++ 5 files changed, 41 insertions(+), 7 deletions(-) diff --git a/skeleton/skeleton.cpp b/skeleton/skeleton.cpp index 7d2a064..ad84d1a 100644 --- a/skeleton/skeleton.cpp +++ b/skeleton/skeleton.cpp @@ -1,11 +1,13 @@ #include #include "skeleton.h" + namespace sk { Globals globals; Args args; + bool InitRW(void) { @@ -22,13 +24,15 @@ InitRW(void) for(i = 0; i < n; i++) if(Engine::getSubSystemInfo(&info, i)) printf("subsystem: %s\n", info.name); + Engine::setSubSystem(n-1); int want = -1; VideoMode mode; n = Engine::getNumVideoModes(); for(i = 0; i < n; i++) if(Engine::getVideoModeInfo(&mode, i)){ - if(mode.width == 640 && mode.height == 480 && mode.depth == 32) +// if(mode.width == 640 && mode.height == 480 && mode.depth == 32) + if(mode.width == 1920 && mode.height == 1080 && mode.depth == 32) want = i; printf("mode: %dx%dx%d %d\n", mode.width, mode.height, mode.depth, mode.flags); } diff --git a/src/d3d/d3d.cpp b/src/d3d/d3d.cpp index a956ae1..76f74d7 100644 --- a/src/d3d/d3d.cpp +++ b/src/d3d/d3d.cpp @@ -898,8 +898,7 @@ destroyNativeRaster(void *object, int32 offset, int32) Raster *raster = (Raster*)object; D3dRaster *natras = PLUGINOFFSET(D3dRaster, raster, offset); #ifdef RW_D3D9 - if(raster->type == Raster::CAMERATEXTURE) - removeVidmemRaster(raster); + destroyD3D9Raster(raster); #endif if(natras->texture) deleteObject(natras->texture); diff --git a/src/d3d/d3ddevice.cpp b/src/d3d/d3ddevice.cpp index 58cef51..85be0c2 100644 --- a/src/d3d/d3ddevice.cpp +++ b/src/d3d/d3ddevice.cpp @@ -244,6 +244,18 @@ resetD3d9Device(void) d3ddevice->SetMaterial(&d3dmaterial); } +void +destroyD3D9Raster(Raster *raster) +{ + int i; + if(raster->type == Raster::CAMERATEXTURE) + removeVidmemRaster(raster); + // Make sure we're not still referencing this raster + for(i = 0; i < MAXNUMSTAGES; i++) + if(rwStateCache.texstage[i].raster == raster) + rwStateCache.texstage[i].raster = nil; +} + // RW render state static void @@ -744,12 +756,14 @@ clearCamera(Camera *cam, RGBA *col, uint32 mode) Raster *ras = cam->frameBuffer; if(!icon && (r.right != d3d9Globals.present.BackBufferWidth || r.bottom != d3d9Globals.present.BackBufferHeight)){ - releaseVidmemRasters(); - d3d9Globals.present.BackBufferWidth = r.right; + d3d9Globals.present.BackBufferWidth = r.right; d3d9Globals.present.BackBufferHeight = r.bottom; - // TODO: check result + + releaseVidmemRasters(); d3d::d3ddevice->Reset(&d3d9Globals.present); + // important that we get all raster back before restoring state + recreateVidmemRasters(); resetD3d9Device(); } @@ -763,7 +777,19 @@ showRaster(Raster *raster) // not used but we want cameras to have rasters assert(raster); - d3ddevice->Present(nil, nil, 0, nil); + HRESULT res = d3ddevice->Present(nil, nil, 0, nil); + + if(res == D3DERR_DEVICELOST){ + res = d3ddevice->TestCooperativeLevel(); + // lost while being minimized, not reset once we're back + if(res == D3DERR_DEVICENOTRESET){ + releaseVidmemRasters(); + d3d::d3ddevice->Reset(&d3d9Globals.present); + // important that we get all raster back before restoring state + recreateVidmemRasters(); + resetD3d9Device(); + } + } } static bool32 @@ -960,6 +986,8 @@ startD3D(void) // Use window size in windowed mode, otherwise get size from video mode if(d3d9Globals.modes[d3d9Globals.currentMode].flags & VIDEOMODEEXCLUSIVE){ + // this will be much better for restoring after iconification + SetWindowLong(d3d9Globals.window, GWL_STYLE, WS_POPUP); width = d3d9Globals.modes[d3d9Globals.currentMode].mode.Width; height = d3d9Globals.modes[d3d9Globals.currentMode].mode.Height; }else{ diff --git a/src/d3d/rwd3dimpl.h b/src/d3d/rwd3dimpl.h index d4ffe17..f42b2a2 100644 --- a/src/d3d/rwd3dimpl.h +++ b/src/d3d/rwd3dimpl.h @@ -44,6 +44,7 @@ struct D3d9Globals extern D3d9Globals d3d9Globals; int findFormatDepth(uint32 format); +void destroyD3D9Raster(Raster *raster); #endif void rasterCreate(Raster *raster); diff --git a/tools/clumpview/main.cpp b/tools/clumpview/main.cpp index 8afce48..948523e 100644 --- a/tools/clumpview/main.cpp +++ b/tools/clumpview/main.cpp @@ -27,6 +27,8 @@ void genIm3DEnd(void); void initFont(void); void printScreen(const char *s, float x, float y); +//#include + void Init(void) {