@@ -2369,6 +2369,13 @@ static uint32_t CreateDevice(uint32_t a1, uint32_t a2, uint32_t a3, uint32_t a4,
23692369 return 0 ;
23702370}
23712371
2372+ static void DestructResourceIm (GuestResource* resource)
2373+ {
2374+ resource->~GuestResource ();
2375+ resource = 0 ;
2376+ g_userHeap.Free (resource);
2377+ }
2378+
23722379static void DestructResource (GuestResource* resource)
23732380{
23742381 // Needed for hack in CreateSurface (remove if fix it)
@@ -3327,6 +3334,7 @@ void Video::Present()
33273334 s_next += 1000000000ns / Config::FPS;
33283335 }
33293336
3337+ // Please Do not use guest_stack_var, it mostly cause memory/stack corruption
33303338 if (App::s_pApp && g_needsResize)
33313339 {
33323340 g_needsResize = false ;
@@ -3381,48 +3389,67 @@ void Video::Present()
33813389 be<uint32_t > Usage;
33823390 };
33833391
3384- g_surfaceCache.clear ();
3392+ // Clear Cache, Experimental
3393+ auto it = g_surfaceCache.begin ();
3394+ while (it != g_surfaceCache.end ())
3395+ {
3396+
3397+ printf (" Cache Surface : %d\n " , it->first ->refCount .get (),it->first ->format );
3398+ if (it->first ->refCount .get () == 0 && it->first != g_depthStencil && it->first != g_backBuffer)
3399+ {
3400+ it->first ->wasCached = false ;
3401+ DestructResourceIm (it->first );
3402+ it = g_surfaceCache.erase (it);
3403+
3404+ }
3405+ else
3406+ {
3407+ ++it;
3408+ }
3409+ }
33853410
33863411 auto pFormatConfig = (GraphicsFormatConfig*)(g_memory.base + 0x82B7BD20 );
33873412
33883413 // Kill Auto Surfaces
3414+
33893415 if (g_backBuffer && g_backBuffer != pApp->m_pBackBufferSurface .get ())
3390- DestructResource (g_backBuffer);
3416+ DestructResourceIm (g_backBuffer);
33913417
33923418 if (g_depthStencil && g_depthStencil != pApp->m_pDepthStencilSurface .get ())
3393- DestructResource (g_depthStencil);
3419+ DestructResourceIm (g_depthStencil);
33943420
33953421 // Recreate main buffers
3396- DestructResource ((GuestTexture*)pApp->m_pFrontBufferTexture .get ());
3422+ DestructResourceIm ((GuestTexture*)pApp->m_pFrontBufferTexture .get ());
33973423 pApp->m_pFrontBufferTexture = CreateTexture (width, height, 1 , 1 , 1 , D3DFMT_LE_X8R8G8B8, 0 , 3 );
3398- ((GuestTexture*)pApp->m_pFrontBufferTexture .get ())->AddRef ();
3424+ // ((GuestTexture*)pApp->m_pFrontBufferTexture.get())->AddRef();
33993425
3400- guest_stack_var<GuestSurfaceCreateParams> surfaceParams = { 0 , 0 , 0 } ;
3426+ auto surfaceParams = g_userHeap. AllocPhysical <D3DXBSURFACE_PARAMETERS>( 0 , 0 , 0 ) ;
34013427
3402- DestructResource ((GuestSurface*)pApp->m_pBackBufferSurface .get ());
3403- pApp->m_pBackBufferSurface = CreateSurface (width, height, D3DFMT_A8R8G8B8, 0 , surfaceParams. get () );
3404- ((GuestSurface*)pApp->m_pBackBufferSurface .get ())->AddRef ();
3428+ DestructResourceIm ((GuestSurface*)pApp->m_pBackBufferSurface .get ());
3429+ pApp->m_pBackBufferSurface = CreateSurface (width, height, D3DFMT_A8R8G8B8, 0 , (GuestSurfaceCreateParams*)surfaceParams );
3430+ // ((GuestSurface*)pApp->m_pBackBufferSurface.get())->AddRef();
34053431
3406- pCreationDeviceData->SurfaceParamsA = *(D3DXBSURFACE_PARAMETERS*)surfaceParams. get () ;
3432+ pCreationDeviceData->SurfaceParamsA = *(D3DXBSURFACE_PARAMETERS*)surfaceParams;
34073433
34083434 // 0x340 default (for 720 mb?)
34093435 uint32_t cSurfaceBase = height * 1.155555555555556 ;
34103436
3411- surfaceParams->base = surfaceParams->base + cSurfaceBase;
3437+ surfaceParams->Base = surfaceParams->Base + cSurfaceBase;
34123438
3413- DestructResource ((GuestSurface*)pApp->m_pDepthStencilSurface .get ());
3414- pApp->m_pDepthStencilSurface = CreateSurface (width, height, D3DFMT_D24FS8, 0 , surfaceParams. get () );
3415- ((GuestSurface*)pApp->m_pDepthStencilSurface .get ())->AddRef ();
3439+ DestructResourceIm ((GuestSurface*)pApp->m_pDepthStencilSurface .get ());
3440+ pApp->m_pDepthStencilSurface = CreateSurface (width, height, D3DFMT_D24FS8, 0 , (GuestSurfaceCreateParams*)surfaceParams );
3441+ // ((GuestSurface*)pApp->m_pDepthStencilSurface.get())->AddRef();
34163442
3417- pCreationDeviceData->SurfaceParamsB = *(D3DXBSURFACE_PARAMETERS*)surfaceParams. get () ;
3443+ pCreationDeviceData->SurfaceParamsB = *(D3DXBSURFACE_PARAMETERS*)surfaceParams;
34183444 pCreationDeviceData->SurfaceParamsC = pCreationDeviceData->SurfaceParamsB ;
34193445 pCreationDeviceData->SurfaceParamsC .Base = pCreationDeviceData->SurfaceParamsB .Base + cSurfaceBase;
3446+ g_userHeap.Free (surfaceParams);
34203447
3421- // Setup device
34223448 // Viewport is reset here because we're using the game's backbuffer directly (Auto mode disabled)
3423- SetRenderTarget ((GuestDevice*)pApp->m_pDevice .get (), 0 , nullptr );
3449+ SetRenderTarget ((GuestDevice*)pApp->m_pDevice .get (), 0 , (GuestSurface*)pApp-> m_pBackBufferSurface . get () );
34243450 SetDepthStencilSurface ((GuestDevice*)pApp->m_pDevice .get (), (GuestSurface*)pApp->m_pDepthStencilSurface .get ());
34253451
3452+
34263453 g_backBuffer = (GuestSurface*)pApp->m_pBackBufferSurface .get ();
34273454 g_depthStencil = (GuestSurface*)pApp->m_pDepthStencilSurface .get ();
34283455
@@ -3459,7 +3486,7 @@ void Video::Present()
34593486 if (surface)
34603487 {
34613488 GuestToHostFunction<void >(sub_82593038, surface, guestSurface);
3462- LOGFN_UTILITY (" - \" {}\" ({}x{}) ({:08X})" , surface->m_MgrResourceName .c_str (), surface->m_Width .get (), surface->m_Height .get (), (uint64_t )surface->m_pGuestSurface .get ());
3489+ LOGFN_UTILITY (" - \" {}\" ({}x{}) ({:08X})" , surface->m_MgrResourceName .c_str (), surface->m_Width .get (), surface->m_Height .get (), (uint64_t )surface->m_pSurface .get ());
34633490 }
34643491 };
34653492
@@ -3529,6 +3556,7 @@ void Video::Present()
35293556 }
35303557
35313558 // Create and configure guest surface
3559+ auto PrevSuface = surface->m_pSurface ;
35323560 auto gSurface = CreateSurface (params.Width , params.Height , pFormatConfig[s2].SurfaceFormat , 0 , (GuestSurfaceCreateParams*)surfaceParams);
35333561
35343562 GuestToHostFunction<void >(sub_82592E98, surface, gSurface , params.Width , params.Height );
@@ -3588,103 +3616,6 @@ void Video::Present()
35883616 }
35893617 }
35903618
3591- // framebuffer_tile should properly reference framebuffer0's GuestTexture
3592- // PROPER FIX OPTIONS:
3593- // 1. Allocate new texture to the same pointer instead of this workaround
3594- // 2. Set framebuffer_tile->m_pTexture to framebuffer0's GuestTexture with proper reference counting (AddRef())
3595- // 3. Reset surface [0] to prevent framebuffer->m_pTexture from being set to null
3596- if (auto it = rmTextureResources.find (" framebuffer_tile" ); it != rmTextureResources.end ())
3597- {
3598- auto tex = (Sonicteam::SoX::Graphics::Xenon::TextureXenon*)it->second .get ();
3599- // tex->m_aspSurface[0].reset();
3600- // but better like
3601- // text->m_pTexture = ???
3602- }
3603-
3604-
3605- /*
3606- std::map<std::string, boost::shared_ptr<Sonicteam::SoX::Engine::RenderProcess>> savedProcesses;
3607-
3608- if (auto pRenderSceduler = pDocState->m_pRenderScheduler.get())
3609- {
3610- // Print all processes before
3611- for (auto& pair : pRenderSceduler->m_lpsspRenderProcess)
3612- {
3613- printf("m_lpsspRenderProcess[%s] = %p\n", pair.first.c_str(), pair.second.get());
3614- }
3615-
3616- // Save GE1Particle and Spanverse processes
3617- for (auto& pair : pRenderSceduler->m_lpsspRenderProcess)
3618- {
3619- if (pair.first == "GE1Particle" || pair.first == "Spanverse")
3620- {
3621- savedProcesses[pair.first.c_str()] = pair.second;
3622- printf("Saved %s: %p\n", pair.first.c_str(), pair.second.get());
3623- }
3624- }
3625- }
3626- */
3627-
3628-
3629-
3630-
3631- /*
3632- auto pManageParticle = Sonicteam::MyPE::CManageParticle::GetInstance();
3633-
3634- if (auto pRenderSceduler = pDocState->m_pRenderScheduler.get())
3635- {
3636- // Restore all saved processes
3637- for (auto& savedProcess : savedProcesses)
3638- {
3639- auto it = std::find_if(pRenderSceduler->m_lpsspRenderProcess.begin(),
3640- pRenderSceduler->m_lpsspRenderProcess.end(),
3641- [&savedProcess](const auto& pair) {
3642- return pair.first.c_str() == savedProcess.first;
3643- });
3644-
3645- if (it != pRenderSceduler->m_lpsspRenderProcess.end())
3646- {
3647- it->second = savedProcess.second;
3648- printf("Restored %s: %p\n", savedProcess.first.c_str(), savedProcess.second.get());
3649-
3650- // Print GTask components
3651- if (savedProcess.second && savedProcess.second->m_pGTask)
3652- {
3653- auto GTask = savedProcess.second->m_pGTask.get();
3654- for (auto& component : GTask->m_llComponent)
3655- {
3656- printf("%s[GTask] %s\n", savedProcess.first.c_str(), component.m_pThis->GetName());
3657- }
3658- }
3659- }
3660- }
3661-
3662- // Print all processes after restoration
3663- for (auto& pair : pRenderSceduler->m_lpsspRenderProcess)
3664- {
3665- printf("m_lpsspRenderProcess[%s] = %p\n", pair.first.c_str(), pair.second.get());
3666- }
3667- }
3668-
3669-
3670- printf("--------------------------{EEEEEEEEEEEEEEEEEEE]------------------\n");
3671- for (auto& it : pManageParticle->m_lContainer)
3672- {
3673- auto h = (int)g_userHeap.Size(&it);
3674- auto p1 = it._1.get();
3675- auto h1 = (uint32_t)g_userHeap.Size(p1);
3676- printf("%x - %d , %x - %d\t\n", &it,h,p1,h1);
3677- }
3678- if (pDocState->m_pRootGTask.get())
3679- {
3680- for (auto& component : pDocState->m_pRootGTask->m_llComponent)
3681- {
3682- printf("component : %s\n", component.m_pThis->GetName());
3683- }
3684-
3685- }
3686-
3687- */
36883619 auto pManageParticle = Sonicteam::MyPE::CManageParticle::GetInstance ();
36893620 auto pRenderSceduler = pDocState->m_pRenderScheduler .get ();
36903621
@@ -3712,11 +3643,9 @@ void Video::Present()
37123643 auto sfx2 = pDocState->m_pSFXAgent ->m_aSFXMatrices2 ;
37133644 pDocState->m_pSFXAgent ->m_aSFXMatrices1 = 0 ;
37143645 pDocState->m_pSFXAgent ->m_aSFXMatrices2 = 0 ;
3715- auto spFrameBufferObject = pMyGraphicsDevice->m_FrameBufferObject ;
3716- auto _array_ = pMyGraphicsDevice->m_apTexture ;
3717- pMyGraphicsDevice->m_FrameBufferObject = 0 ;
3646+
37183647 GuestToHostFunction<void >(sub_8260DF88, pDocState, 0x82B814F8 , 1 );
3719- pMyGraphicsDevice-> m_apTexture = _array_ ;
3648+ _cached_render_. clear () ;
37203649
37213650 // Clear New Ones
37223651 if (pDocState->m_pSFXAgent ->m_aSFXMatrices1 )
@@ -3727,55 +3656,8 @@ void Video::Present()
37273656
37283657 pDocState->m_pSFXAgent ->m_aSFXMatrices1 = sfx1;
37293658 pDocState->m_pSFXAgent ->m_aSFXMatrices2 = sfx2;
3730- pMyGraphicsDevice->m_FrameBufferObject = spFrameBufferObject;
3731-
3732-
3733- printf (" pDocState->m_PauseFlag %x\n " , pDocState->m_PauseFlags .get ());
3734- if (pDocState->m_PauseFlags .get () != 0 )
3735- {
3736-
3737- /*
3738- for (auto& thread : *pDocState->m_lnThread.get())
3739- {
3740-
3741- auto pThread = thread.m_pThis;
3742- printf("pDocState->m_lnThread %p - %x\n", &thread, pThread->m_pVftable.get());
3743- if (pThread->m_IsThreadReady)
3744- {
3745- pThread->m_DeltaTime = 1.0;
3746- pThread->m_IsThreadReady = 0;
3747- GuestToHostFunction<void>(sub_826FD180, pThread->m_Handle.get()); //ResumeThread
3748- }
3749- GuestToHostFunction<void>(sub_826FD140, pThread->m_StartEvent);
3750- }
3751-
3752- for (auto& thread : *pDocState->m_lnThread.get())
3753- {
3754- auto pThread = thread.m_pThis;
3755- pThread->Func4();
3756- }
3757- */
3758-
3759- // Always Update
3760- if (pDocState->m_PauseFlags .get () != 0 )
3761- {
3762-
3763-
3764-
3765-
3766- // GuestToHostFunction<void>(sub_825D49E0, &pDocState->m_CriticalSection1);
3767- // Traverse(pDocState->m_pRootTask.get());
3768- // GuestToHostFunction<void>(sub_82580920, &pDocState->m_CriticalSection1);
3769- }
3770-
3771- // GuestToHostFunction<void>(sub_82581D50, pDocState, 1.0/60.0);
3772- }
3773-
3774-
3775-
3776-
3777-
37783659
3660+ // Fix radermap
37793661 auto SetResource = [&](auto * spTextureTo, const char * name)
37803662 {
37813663 if (auto it = rmTextureResources.find (name); it != rmTextureResources.end ())
@@ -3796,7 +3678,6 @@ void Video::Present()
37963678 }
37973679 }
37983680
3799- // TODO: Fix particle not updating position or disappearing after Lua refresh
38003681 }
38013682PostResize:
38023683
@@ -4133,6 +4014,7 @@ static GuestSurface* CreateSurface(uint32_t width, uint32_t height, uint32_t for
41334014 cachedSurface->guestFormat == format &&
41344015 cachedBase == baseValue) {
41354016 surface = cachedSurface;
4017+ surface->AddRef ();
41364018 break ;
41374019 }
41384020 }
0 commit comments