CyberEngineTweaks: CET with AMD and my camera tools doing viewport resizing causes crashes

Hi,

I’m the developer of the cyberpunk camera tools used by many people. One of the features is to resize the game’s viewport by setting the new resolution in memory, after which the engine will resize the window and viewport to the new resolution. This is called ‘Hotsampling’ and is often used by virtual photographers to take a screenshot at a higher resolution.

Resizing the viewport of the game that way when CET is used however leads to a specific crash in ffx_backend_dx12_x64.dll: image

When CET isn’t active, no crash occurs.

Looking at your code, it seems you’re hooking the engine’s resize function instead of the swapchain’s ResizeBuffers (d3d12 swapchain VTable[13]). https://github.com/maximegmd/CyberEngineTweaks/blob/4aea40082ef6a59dc37a92e2cd9038c4dbf1936f/src/d3d12/D3D12_Hooks.cpp#L269

My question is: why not hook the swapchain resize buffers to get the callback, then do the cleanup? Could this be changed perhaps in the future so these crashes on AMD cards are avoided?

The resolution struct is interceptible using AOB: 8B 41 7C 39 41 18 75 19 8B 81 80 00 00 00 39 41 1C 75 0E, where rcx contains the structure pointer. offset 0x7C in that struct contains the screen width, offset 0x80 contains the height (in pixels). Changing any of the values there will resize the viewport by the engine and will then trigger the crash on AMD hardware.

Thanks!

About this issue

Most upvoted comments

This uses completely different functions now, ResizeBackbuffers was a bit of a bait overall as it was exposed in the address json game comes with, but it gets called 2 sometimes 4 times for no good reason in the end, and the DoPresent is kind of suboptimal overall as that way, we have to worry about HDR and other stuff.

New version uses different set of function overall, allowing us to draw into linear render target that game uses right before it is about to copy it into backbuffer and apply HDR mapping. Is much more stable and universal in the end in what it offers.

There is also proper fencing now implemented as great time was spent on reversing more render-specific structures. We can and do wait on proper fences now, so resources are ever getting removed and reallocated only when none is in active use. No need for mutex to sync this now in latest build as we can just wait for GPU to fully present and process everything before doing any dramatic changes.

There was #1003 merged yesterday into master which may help with your issue somewhat. But it does not fully solve all the problems like the fencing I mentioned here and I also found some resource leaks while rewriting some stuff…

We are also completely reworking practically everything atm, new version has completely different hooking mechanism which should be much more stable (was unable to crash it with usual culprits at least + it does more than it did before when it comes to rendering and resource managment overall so 🤞 )

See https://github.com/maximegmd/CyberEngineTweaks/issues/982#issuecomment-2669419470

We do it like this primarilly for the threading issues sort of as mentioned, render thread runs in parallel to main and when we had onDraw and whole ImGui::Render happen on the render thread, we were nonstop getting complains about different types of crashes and race conditions happening.

We ended up doing everything on the main thread after game updates everything. Only thread doing real work at that point are streaming-related and rendering related, so it is a safe space of sorts to call into game with custom scripts (nothing should cause race conditions + a lot of things can only be executed on main thread, otherwise it crashes with assertion error about incorrect thread being used).

Everything would be much simpler if users would not be trying to call into the game itself or otherwise access game resources there as was originally intended, but one can only dream I guess. onDraw was meant only for ImGui and their own local stuff, but nobody used it right probably ever and here we ended.

It is definitely slower overall and I suppose makes these sort of issues more apparent, but it was only safe way to handle this reliably without breaking ton of existing mods.