Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
fix(posixd3d9): preserve reset state and depth compatibility
Richard Louie Orilla7347f4890a73
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 34f8b533a..30b33e69c 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -179,6 +179,8 @@ found and fixed — EA's own, not port damage.**## Zero Hour on a Mac, on Linux and on the Steam Deck+- On Linux and Mac, a failed resolution change no longer discards the old picture before its replacement is ready. Pending drawing finishes before the old targets are retired, and a refused replacement leaves the previous display settings in place for another attempt.+- Linux and Mac keep the ordinary depth format when the graphics card cannot provide the depth copy needed by modern water and volumetric clouds, instead of choosing a potentially larger format for effects it cannot draw.- The game runs on Apple silicon Macs and on Linux, the Steam Deck included, as a program built for that machine. There is no Wine or Proton underneath. It draws through Metal on a Mac and through Vulkan on Linux, from the same Zero Hour files you already own.- The first start finds your game. It looks in your Steam libraries, `~/Games`, `/Applications`, and CrossOver and Whisky bottles, and asks for the folder only when it comes up empty.- One command builds it. `./build-macos.sh` leaves a Mac app ready to open and `./build-linux.sh` a folder ready to start, and each checks your machine first and names the package to install for anything missing. On Linux it can make an AppImage, a .deb, an .rpm, an Arch package and a Flatpak, none of them carrying EA's files, and the Flatpak has played a full check match on a Steam Deck.diff --git a/GeneralsMD/Code/BuildFingerprint.manifest b/GeneralsMD/Code/BuildFingerprint.manifestindex 544aace7b..15e2cc64a 100644--- a/GeneralsMD/Code/BuildFingerprint.manifest+++ b/GeneralsMD/Code/BuildFingerprint.manifest@@ -1825,6 +1825,7 @@ GameEngineDevice/Source/PosixDevice/Render/PosixResources9.cppGameEngineDevice/Source/PosixDevice/Render/PosixResources9.hGameEngineDevice/Source/PosixDevice/Render/SdlConstants.hGameEngineDevice/Source/PosixDevice/Render/SdlCreationLog.h+GameEngineDevice/Source/PosixDevice/Render/SdlGpuDepthFormat.hGameEngineDevice/Source/PosixDevice/Render/SdlGpuFrame.cppGameEngineDevice/Source/PosixDevice/Render/SdlGpuFrame.hGameEngineDevice/Source/PosixDevice/Render/SdlGpuFrameBatch.cpp@@ -3162,6 +3163,7 @@ Tests/test_observer_camera.incTests/test_pixelcentre.cppTests/test_posix_draw_resolve.cppTests/test_posix_gpu_draw.cpp+Tests/test_posix_reset.cppTests/test_posix_resources.cppTests/test_posixlocalfilesystem.cppTests/test_posixlocalfilesystem_stubs.cpp@@ -3172,6 +3174,7 @@ Tests/test_rank_ladder.cppTests/test_registryfile.cppTests/test_render_hooks.cppTests/test_road_buffer.cpp+Tests/test_sdl_gpu_depth_format.cppTests/test_sdl_gpu_frame.cppTests/test_sdl_input.cppTests/test_sdl_pipeline_state.cppdiff --git a/GeneralsMD/Code/CMakeLists.txt b/GeneralsMD/Code/CMakeLists.txtindex 53a707999..7f3010524 100644--- a/GeneralsMD/Code/CMakeLists.txt+++ b/GeneralsMD/Code/CMakeLists.txt@@ -1408,6 +1408,14 @@ if(ZH_PLATFORM_POSIX)# pass) the way the generated ones are compiled.target_link_libraries(posixd3d9 PRIVATE SDL3::SDL3-static sdl3shadercompile engineshader ffshader ffvertex)+ # Depth-format fallback follows the complete scene-depth capability, without requiring a GPU.+ add_executable(sdl_gpu_depth_format_selfcheck ${CMAKE_CURRENT_SOURCE_DIR}/Tests/test_sdl_gpu_depth_format.cpp)+ set_target_properties(sdl_gpu_depth_format_selfcheck PROPERTIES CXX_STANDARD 17)+ target_include_directories(sdl_gpu_depth_format_selfcheck PRIVATE+ ${CMAKE_CURRENT_SOURCE_DIR}/GameEngineDevice/Source/PosixDevice/Render)+ target_link_libraries(sdl_gpu_depth_format_selfcheck PRIVATE SDL3::Headers)+ add_test(NAME sdl_gpu_depth_format_selfcheck COMMAND sdl_gpu_depth_format_selfcheck)+# A3a's frame: a clear read back, and the gamma pass through a picture that shows any flip. Needs a GPU# but no window; exits 77 (skipped, and ctest says so) where SDL has no GPU device to give.add_executable(sdl_gpu_frame_selfcheck ${CMAKE_CURRENT_SOURCE_DIR}/Tests/test_sdl_gpu_frame.cpp)@@ -1455,6 +1463,18 @@ if(ZH_PLATFORM_POSIX)add_test(NAME posix_gpu_draw_selfcheck COMMAND posix_gpu_draw_selfcheck)set_tests_properties(posix_gpu_draw_selfcheck PROPERTIES SKIP_RETURN_CODE 77)+ if(ZH_PLATFORM_LINUX)+ add_executable(posix_reset_selfcheck ${CMAKE_CURRENT_SOURCE_DIR}/Tests/test_posix_reset.cpp)+ set_target_properties(posix_reset_selfcheck PROPERTIES CXX_STANDARD 17)+ target_include_directories(posix_reset_selfcheck PRIVATE+ ${CMAKE_CURRENT_SOURCE_DIR}/GameEngineDevice/Source/PosixDevice/Render)+ target_link_libraries(posix_reset_selfcheck PRIVATE posixd3d9 SDL3::SDL3-static)+ target_link_options(posix_reset_selfcheck PRIVATE+ "LINKER:--wrap=SDL_AcquireGPUCommandBuffer" "LINKER:--wrap=SDL_CreateGPUTexture")+ add_test(NAME posix_reset_selfcheck COMMAND posix_reset_selfcheck)+ set_tests_properties(posix_reset_selfcheck PROPERTIES SKIP_RETURN_CODE 77)+ endif()+if(ZH_PLATFORM_LINUX)# Dedicated water material editor: the same device, FFT and shader as the game.add_executable(water_preview EXCLUDE_FROM_ALLdiff --git a/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/PosixDevice9.cpp b/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/PosixDevice9.cppindex c27cfece2..16a16a0ff 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/PosixDevice9.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/PosixDevice9.cpp@@ -21,6 +21,7 @@// PosixD3D9Caps.cpp. See PosixDevice9.h for which file holds what and how a device without a window behaves.#include "PosixDevice9.h"+#include "PosixD3D9Caps.h"#include "SdlCreationLog.h"#include "Platform/EngineShaderName.h"#include "Platform/RendererName.h"@@ -507,12 +508,47 @@ RenderResult PosixDevice9::CreateAdditionalSwapChain(D3DPRESENT_PARAMETERS *, IDRenderResult PosixDevice9::Reset(D3DPRESENT_PARAMETERS *parameters){- Shore_End();if (parameters == NULL || parameters->BackBufferWidth == 0 || parameters->BackBufferHeight == 0) {return D3DERR_INVALIDCALL;}+ // Stage the CPU surfaces too: once the GPU attachments change, committing the reset cannot fail.+ const D3DFORMAT format = parameters->BackBufferFormat == D3DFMT_UNKNOWN+ ? D3DFMT_X8R8G8B8 : parameters->BackBufferFormat;+ if (!posixFormatSupported(D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, format)+ || (parameters->EnableAutoDepthStencil && !posixFormatSupported(D3DUSAGE_DEPTHSTENCIL,+ D3DRTYPE_SURFACE, parameters->AutoDepthStencilFormat))) {+ return D3DERR_INVALIDCALL;+ }+ PosixSurface9 *back = PosixSurface9::createStandalone(parameters->BackBufferWidth,+ parameters->BackBufferHeight, format, D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT,+ parameters->MultiSampleType, parameters->MultiSampleQuality);+ if (!back) return D3DERR_OUTOFVIDEOMEMORY;+ PosixSurface9 *depth = NULL;+ if (parameters->EnableAutoDepthStencil) {+ depth = PosixSurface9::createStandalone(parameters->BackBufferWidth, parameters->BackBufferHeight,+ parameters->AutoDepthStencilFormat, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT,+ parameters->MultiSampleType, parameters->MultiSampleQuality);+ if (!depth) {+ back->Release();+ return D3DERR_OUTOFVIDEOMEMORY;+ }+ }+ SdlGpuFrame::ResizeFailure failure = SdlGpuFrame::RESIZE_NONE;+ // Restore an active capture before Resize can retire the scene attachments it restores to.+ bool capture_submitted = true;+ if (SunActive) End_Sun_Shadow(&capture_submitted);+ if (!capture_submitted || (Gpu != NULL && !Gpu->Resize(parameters->BackBufferWidth,+ parameters->BackBufferHeight, &failure))) {+ back->Release();+ if (depth) depth->Release();+ return !capture_submitted || failure == SdlGpuFrame::RESIZE_FLUSH_FAILED+ ? D3DERR_DEVICELOST : D3DERR_OUTOFVIDEOMEMORY;+ }+ Shore_End();Clear_Sun_Shadow();Sun.reset();+ // A failed flush must never drop these bindings: a standalone target can have no other owner,+ // while the retained batch still references its mirror. Resize has now submitted that batch.Release_Surfaces();Parameters = *parameters;if (Parameters.hDeviceWindow != NULL) {@@ -524,10 +560,11 @@ RenderResult PosixDevice9::Reset(D3DPRESENT_PARAMETERS *parameters)Viewport.Height = Parameters.BackBufferHeight;Viewport.MinZ = 0.0f;Viewport.MaxZ = 1.0f;- if (Gpu != NULL && !Gpu->Resize(Parameters.BackBufferWidth, Parameters.BackBufferHeight)) {- return D3DERR_OUTOFVIDEOMEMORY;- }- return Create_Implicit_Surfaces();+ BackBuffer = back;+ Posix_Bind(RenderTargets[0], BackBuffer);+ DepthSurface = depth;+ Posix_Bind(DepthStencil, DepthSurface);+ return D3D_OK;}RenderResult PosixDevice9::Present(const RenderRect *, const RenderRect *, RenderWindow, const void *)diff --git a/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/PosixDevice9.h b/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/PosixDevice9.hindex e5915f8d0..47b62c586 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/PosixDevice9.h+++ b/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/PosixDevice9.h@@ -177,7 +177,8 @@ public:void Get_Sun_Caster_Effect(unsigned &effect, float parameters[4]) const;void Set_Smoke_Glow(bool glow) { SmokeGlow = glow; }bool Begin_Sun_Shadow(unsigned size);- bool End_Sun_Shadow();+ // Optional submission status is independent of whether the shadow image can be used.+ bool End_Sun_Shadow(bool *submission_ok = NULL);bool Set_Sun_Shadow_Parameters(float bias, float strength, float widest, float narrowest,float texelsPerGap, float unitsPerDepth, float skyFill);bool Sun_Receives_View();diff --git a/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/PosixDevice9SunShadow.cpp b/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/PosixDevice9SunShadow.cppindex 61da94f3f..41e2ebff0 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/PosixDevice9SunShadow.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/PosixDevice9SunShadow.cpp@@ -106,8 +106,9 @@ bool PosixDevice9::Sun_Target(SdlTarget &target) constreturn true;}-bool PosixDevice9::End_Sun_Shadow()+bool PosixDevice9::End_Sun_Shadow(bool *submission_ok){+ if (submission_ok) *submission_ok = false;if (!SunActive || !Sun) return false;const float *view = &Transforms[D3DTS_VIEW]._11;const float *projection = &Transforms[D3DTS_PROJECTION]._11;@@ -128,6 +129,7 @@ bool PosixDevice9::End_Sun_Shadow()std::string error;const bool restored = Resolve_Target(scene, error);if (restored) Gpu->Set_Target(scene);+ if (submission_ok) *submission_ok = submitted;SunReady = submitted && restored && !SunFailed;return SunReady;}diff --git a/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/SdlGpuDepthFormat.h b/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/SdlGpuDepthFormat.hnew file mode 100644index 000000000..961d57875--- /dev/null+++ b/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/SdlGpuDepthFormat.h@@ -0,0 +1,54 @@+/*+** Copyright 2026 İlyas Akın+** Additional terms under GNU GPL section 7 apply: see LICENSE.md.+**+** This program is free software: you can redistribute it and/or modify+** it under the terms of the GNU General Public License as published by+** the Free Software Foundation, either version 3 of the License, or+** (at your option) any later version.+**+** This program is distributed in the hope that it will be useful,+** but WITHOUT ANY WARRANTY; without even the implied warranty of+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+** GNU General Public License for more details.+**+** You should have received a copy of the GNU General Public License+** along with this program. If not, see <http://www.gnu.org/licenses/>.+*/++#ifndef SDL_GPU_DEPTH_FORMAT_H+#define SDL_GPU_DEPTH_FORMAT_H++#include <SDL3/SDL_gpu.h>++struct SdlGpuDepthFormat+{+ SDL_GPUTextureFormat Format;+ bool Sampleable;+};++// Keep the capability policy testable without creating a GPU device. The query has the same+// format/type/usage arguments as SDL_GPUTextureSupportsFormat after its device argument.+template <typename SupportsFormat>+SdlGpuDepthFormat Sdl_Select_Depth_Format(SupportsFormat supports)+{+ // Sampling scene depth also needs a sampled R32F colour target for the resolve. If it cannot+ // be created, retain the ordinary D24-first attachment preference for compatibility.+ const bool scene_depth = supports(SDL_GPU_TEXTUREFORMAT_R32_FLOAT, SDL_GPU_TEXTURETYPE_2D,+ SDL_GPU_TEXTUREUSAGE_COLOR_TARGET | SDL_GPU_TEXTUREUSAGE_SAMPLER);+ const SDL_GPUTextureFormat formats[] = {+ SDL_GPU_TEXTUREFORMAT_D24_UNORM_S8_UINT, SDL_GPU_TEXTUREFORMAT_D32_FLOAT_S8_UINT };+ for (unsigned int sampled = scene_depth ? 1 : 0; ; --sampled) {+ const SDL_GPUTextureUsageFlags usage = SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET+ | (sampled ? SDL_GPU_TEXTUREUSAGE_SAMPLER : 0);+ for (unsigned int i = 0; i < 2; ++i) {+ if (supports(formats[i], SDL_GPU_TEXTURETYPE_2D, usage)) {+ return { formats[i], sampled != 0 };+ }+ }+ if (!sampled) break;+ }+ return { SDL_GPU_TEXTUREFORMAT_INVALID, false };+}++#endifdiff --git a/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/SdlGpuFrame.cpp b/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/SdlGpuFrame.cppindex 48eaaac82..ff47a0bd8 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/SdlGpuFrame.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/SdlGpuFrame.cpp@@ -19,6 +19,7 @@// The device's frame on SDL3 GPU (decision 7, phase A3a). See SdlGpuFrame.h.#include "SdlGpuFrame.h"+#include "SdlGpuDepthFormat.h"#include "sdl3shadercompile.h"#include "Platform/WaterFftSpectrum.h"@@ -180,25 +181,12 @@ SdlGpuFrame * SdlGpuFrame::Create(RenderWindow window, unsigned int width, unsigdelete frame;return NULL;}- // Prefer a sampleable stencil format for the scene depth resolve (water, hazard clouds). Preserve rendering on a- // device without one by retaining the ordinary depth target, and disabling scene-depth sampling.- const SDL_GPUTextureFormat depth_formats[] = {- SDL_GPU_TEXTUREFORMAT_D24_UNORM_S8_UINT, SDL_GPU_TEXTUREFORMAT_D32_FLOAT_S8_UINT };- for (unsigned int sampled = 1; frame->DepthFormat == SDL_GPU_TEXTUREFORMAT_INVALID; --sampled) {- const SDL_GPUTextureUsageFlags usage = SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET- | (sampled ? SDL_GPU_TEXTUREUSAGE_SAMPLER : 0);- for (unsigned int i = 0; i < 2; ++i) {- if (SDL_GPUTextureSupportsFormat(frame->GpuDevice, depth_formats[i], SDL_GPU_TEXTURETYPE_2D, usage)) {- frame->DepthFormat = depth_formats[i];- frame->DepthSampleable = sampled != 0;- break;- }- }- if (!sampled) break;- }- frame->DepthSampleable = frame->DepthSampleable && SDL_GPUTextureSupportsFormat(frame->GpuDevice,- SDL_GPU_TEXTUREFORMAT_R32_FLOAT, SDL_GPU_TEXTURETYPE_2D,- SDL_GPU_TEXTUREUSAGE_COLOR_TARGET | SDL_GPU_TEXTUREUSAGE_SAMPLER);+ const SdlGpuDepthFormat depth = Sdl_Select_Depth_Format(+ [frame](SDL_GPUTextureFormat format, SDL_GPUTextureType type, SDL_GPUTextureUsageFlags usage) {+ return SDL_GPUTextureSupportsFormat(frame->GpuDevice, format, type, usage);+ });+ frame->DepthFormat = depth.Format;+ frame->DepthSampleable = depth.Sampleable;if (frame->DepthFormat == SDL_GPU_TEXTUREFORMAT_INVALID) {error = "no supported depth/stencil render-target format";delete frame;@@ -264,14 +252,25 @@ bool SdlGpuFrame::Create_Targets(unsigned int width, unsigned int height)info.layer_count_or_depth = 1;info.num_levels = 1;info.sample_count = SDL_GPU_SAMPLECOUNT_1;- BackBuffer = SDL_CreateGPUTexture(GpuDevice, &info);- FrontCopy = SDL_CreateGPUTexture(GpuDevice, &info);+ SDL_GPUTexture *back = SDL_CreateGPUTexture(GpuDevice, &info);+ SDL_GPUTexture *front = SDL_CreateGPUTexture(GpuDevice, &info);info.format = (SDL_GPUTextureFormat)DepthFormat;info.usage = Depth_Usage();- DepthStencil = SDL_CreateGPUTexture(GpuDevice, &info);+ SDL_GPUTexture *depth = SDL_CreateGPUTexture(GpuDevice, &info);+ if (!back || !front || !depth) {+ if (back) SDL_ReleaseGPUTexture(GpuDevice, back);+ if (front) SDL_ReleaseGPUTexture(GpuDevice, front);+ if (depth) SDL_ReleaseGPUTexture(GpuDevice, depth);+ return false;+ }+ // Keep the old attachments, scratch depths and snapshots until every replacement exists.+ Release_Targets();+ BackBuffer = back;+ FrontCopy = front;+ DepthStencil = depth;BackWidth = width;BackHeight = height;- return BackBuffer != NULL && DepthStencil != NULL && FrontCopy != NULL;+ return true;}void SdlGpuFrame::Release_Targets()@@ -670,14 +669,24 @@ bool SdlGpuFrame::Resolve_Scene_Depth(SDL_GPUCommandBuffer *commands, SDL_GPUTexreturn true;}-bool SdlGpuFrame::Resize(unsigned int width, unsigned int height)+bool SdlGpuFrame::Resize(unsigned int width, unsigned int height, ResizeFailure *failure){- // What is recorded runs first: the GPU copies count the uploads it carries as done. The current- // target named the old back buffer; the device sets it again before its next draw.- if (!width || !height || !Flush()) return false;+ if (failure) *failure = RESIZE_NONE;+ if (!width || !height) {+ if (failure) *failure = RESIZE_INVALID_SIZE;+ return false;+ }+ // Submit before replacing anything: queued targets and mirror uploads still name the old resources.+ if (!Flush()) {+ if (failure) *failure = RESIZE_FLUSH_FAILED;+ return false;+ }+ if (!Create_Targets(width, height)) {+ if (failure) *failure = RESIZE_ALLOCATION_FAILED;+ return false;+ }TargetSet = false;- Release_Targets();- return Create_Targets(width, height);+ return true;}void SdlGpuFrame::Clear_Back_Buffer(bool colour, bool depth, bool stencil, uint32_t argb, float z, uint32_t stencil_value)diff --git a/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/SdlGpuFrame.h b/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/SdlGpuFrame.hindex bc22fe095..162fc1b24 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/SdlGpuFrame.h+++ b/GeneralsMD/Code/GameEngineDevice/Source/PosixDevice/Render/SdlGpuFrame.h@@ -114,8 +114,10 @@ public:static SdlGpuFrame * Create(RenderWindow window, unsigned int width, unsigned int height, std::string & error);~SdlGpuFrame();- /// A new back buffer and depth-stencil of this size (Reset), after running what is recorded.- bool Resize(unsigned int width, unsigned int height);+ /// A failed flush or allocation retains the old attachments and dimensions.+ /// Recording/submission failures still have Flush's batch-consumption semantics.+ enum ResizeFailure { RESIZE_NONE, RESIZE_INVALID_SIZE, RESIZE_FLUSH_FAILED, RESIZE_ALLOCATION_FAILED };+ bool Resize(unsigned int width, unsigned int height, ResizeFailure *failure = NULL);/// Where the clears and draws recorded from now on go (A3d); the back buffer until told otherwise.void Set_Target(const SdlTarget & target);diff --git a/GeneralsMD/Code/Tests/test_posix_reset.cpp b/GeneralsMD/Code/Tests/test_posix_reset.cppnew file mode 100644index 000000000..62bffb714--- /dev/null+++ b/GeneralsMD/Code/Tests/test_posix_reset.cpp@@ -0,0 +1,257 @@+/*+** Copyright 2026 İlyas Akın+** Additional terms under GNU GPL section 7 apply: see LICENSE.md.+**+** This program is free software: you can redistribute it and/or modify+** it under the terms of the GNU General Public License as published by+** the Free Software Foundation, either version 3 of the License, or+** (at your option) any later version.+**+** This program is distributed in the hope that it will be useful,+** but WITHOUT ANY WARRANTY; without even the implied warranty of+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+** GNU General Public License for more details.+**+** You should have received a copy of the GNU General Public License+** along with this program. If not, see <http://www.gnu.org/licenses/>.+*/++// Faults at the SDL allocation/acquire boundary exercise the actual reset, batch and mirror code.+// No window or game is run. The linker wrappers are confined to this Linux selfcheck.+#include "PosixDevice9.h"+#include "SdlGpuFrame.h"+#include "SdlResourceMirror.h"+#include <SDL3/SDL.h>+#include <cstdio>+#include <cstring>+#include <vector>++static int failures = 0;+#define CHECK(condition) \+ do { if (!(condition)) { ++failures; printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #condition); } } while (0)++static bool fail_acquire = false;+static int fail_texture = 0;+static bool fail_all_textures = false;+static unsigned texture_calls = 0, acquire_calls = 0;+extern "C" SDL_GPUCommandBuffer *__real_SDL_AcquireGPUCommandBuffer(SDL_GPUDevice *);+extern "C" SDL_GPUTexture *__real_SDL_CreateGPUTexture(SDL_GPUDevice *, const SDL_GPUTextureCreateInfo *);+extern "C" SDL_GPUCommandBuffer *__wrap_SDL_AcquireGPUCommandBuffer(SDL_GPUDevice *device)+{+ ++acquire_calls;+ if (fail_acquire) {+ SDL_SetError("reset selfcheck: command acquisition refused");+ return NULL;+ }+ return __real_SDL_AcquireGPUCommandBuffer(device);+}+extern "C" SDL_GPUTexture *__wrap_SDL_CreateGPUTexture(SDL_GPUDevice *device, const SDL_GPUTextureCreateInfo *info)+{+ ++texture_calls;+ if (fail_all_textures || (fail_texture && --fail_texture == 0)) {+ SDL_SetError("reset selfcheck: target allocation refused");+ return NULL;+ }+ return __real_SDL_CreateGPUTexture(device, info);+}++static void check_state(PosixDevice9 *device, const D3DPRESENT_PARAMETERS ¶meters,+ const D3DVIEWPORT9 &viewport, IDirect3DSurface9 *back, IDirect3DSurface9 *depth,+ IDirect3DSurface9 *target, const SdlTarget &gpu_target)+{+ const D3DPRESENT_PARAMETERS ¤t = device->Get_Present_Parameters();+ CHECK(memcmp(¤t, ¶meters, sizeof(current)) == 0);+ D3DVIEWPORT9 got;+ CHECK(device->GetViewport(&got) == D3D_OK);+ CHECK(memcmp(&got, &viewport, sizeof(got)) == 0);+ IDirect3DSurface9 *surface = NULL;+ CHECK(device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &surface) == D3D_OK);+ CHECK(surface == back); if (surface) surface->Release();+ CHECK(device->GetDepthStencilSurface(&surface) == D3D_OK);+ CHECK(surface == depth); if (surface) surface->Release();+ CHECK(device->GetRenderTarget(0, &surface) == D3D_OK);+ CHECK(surface == target); if (surface) surface->Release();+ const SdlTarget gpu = device->Get_Gpu()->Back_Buffer_Target();+ CHECK(gpu.Colour == gpu_target.Colour && gpu.Depth == gpu_target.Depth);+ CHECK(gpu.Width == gpu_target.Width && gpu.Height == gpu_target.Height);+}++static void check_reset(PosixDevice9 *device)+{+ const D3DPRESENT_PARAMETERS original = device->Get_Present_Parameters();+ D3DPRESENT_PARAMETERS next = original;+ next.BackBufferWidth = 48; next.BackBufferHeight = 40;+ IDirect3DSurface9 *back = NULL, *depth = NULL, *target = NULL;+ CHECK(device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &back) == D3D_OK);+ CHECK(device->GetDepthStencilSurface(&depth) == D3D_OK);+ CHECK(device->CreateRenderTarget(32, 32, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, false,+ &target, NULL) == D3D_OK);+ CHECK(device->SetRenderTarget(0, target) == D3D_OK);+ const D3DVIEWPORT9 viewport = { 2, 3, 24, 20, 0.1f, 0.9f };+ CHECK(device->SetViewport(&viewport) == D3D_OK);+ SdlGpuFrame *frame = device->Get_Gpu();+ const SdlTarget old_gpu = frame->Back_Buffer_Target();+ // Queue a real mirror upload plus a clear referencing a standalone target held by the device.+ IDirect3DTexture9 *texture = NULL;+ CHECK(device->CreateTexture(1, 1, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &texture, NULL) == D3D_OK);+ D3DLOCKED_RECT locked;+ CHECK(texture->LockRect(0, &locked, NULL, 0) == D3D_OK);+ *(uint32_t *)locked.pBits = 0xFF123456;+ CHECK(texture->UnlockRect(0) == D3D_OK);+ std::string refusal;+ SDL_GPUTexture *mirror = device->Get_Mirrors()->Texture(texture, refusal);+ CHECK(mirror != NULL);+ CHECK(device->Clear(0, NULL, D3DCLEAR_TARGET, 0xFFABCDEF, 1, 0) == D3D_OK);+ SDL_GPUTexture *target_gpu = device->Get_Mirrors()->Surface(target, false, refusal);+ CHECK(target_gpu != NULL);+ target->Release(); // The device binding is now the standalone target's only owner.+ const uint64_t batch = frame->Batch();+ const unsigned uploaded = device->Get_Mirrors()->Textures_Uploaded();+ texture_calls = 0; acquire_calls = 0; fail_acquire = true;+ CHECK(device->Reset(&next) == D3DERR_DEVICELOST);+ fail_acquire = false;+ CHECK(acquire_calls == 1 && texture_calls == 0);+ CHECK(frame->Batch() == batch);+ check_state(device, original, viewport, back, depth, target, old_gpu);+ std::vector<uint8_t> pixels;+ CHECK(frame->Read_Back(target_gpu, 32, 32, pixels));+ CHECK(pixels.size() == 32 * 32 * 4);+ if (pixels.size() == 32 * 32 * 4) {+ const uint8_t *pixel = &pixels[(3 * 32 + 2) * 4];+ CHECK(pixel[0] == 0xEF && pixel[1] == 0xCD && pixel[2] == 0xAB);+ }+ CHECK(frame->Read_Back(mirror, 1, 1, pixels));+ if (pixels.size() == 4) CHECK(pixels[0] == 0x56 && pixels[1] == 0x34 && pixels[2] == 0x12);+ else CHECK(false);+ CHECK(device->Get_Mirrors()->Texture(texture, refusal) == mirror);+ CHECK(device->Get_Mirrors()->Textures_Uploaded() == uploaded);+ CHECK(frame->Flush());++ for (int allocation = 1; allocation <= 3; ++allocation) {+ CHECK(device->Clear(0, NULL, D3DCLEAR_TARGET, 0xFF102030, 1, 0) == D3D_OK);+ texture_calls = 0; fail_texture = allocation;+ CHECK(device->Reset(&next) == D3DERR_OUTOFVIDEOMEMORY);+ fail_texture = 0;+ CHECK(texture_calls == 3);+ check_state(device, original, viewport, back, depth, target, old_gpu);+ CHECK(frame->Read_Back(target_gpu, 32, 32, pixels));+ if (pixels.size() == 32 * 32 * 4) {+ const uint8_t *pixel = &pixels[(3 * 32 + 2) * 4];+ CHECK(pixel[0] == 0x30 && pixel[1] == 0x20 && pixel[2] == 0x10);+ }+ }+ // Invalid CPU surface formats fail before the queue is touched.+ D3DPRESENT_PARAMETERS invalid = next;+ invalid.AutoDepthStencilFormat = D3DFMT_A8R8G8B8;+ acquire_calls = 0;+ CHECK(device->Reset(&invalid) == D3DERR_INVALIDCALL);+ CHECK(acquire_calls == 0);+ check_state(device, original, viewport, back, depth, target, old_gpu);+ CHECK(device->Reset(&next) == D3D_OK);+ CHECK(frame->Width() == 48 && frame->Height() == 40);+ D3DVIEWPORT9 got;+ CHECK(device->GetViewport(&got) == D3D_OK);+ CHECK(got.X == 0 && got.Y == 0 && got.Width == 48 && got.Height == 40 && got.MinZ == 0 && got.MaxZ == 1);+ CHECK(device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xFF654321, 1, 0) == D3D_OK);+ CHECK(frame->Read_Back(frame->Back_Buffer(), 48, 40, pixels));+ if (!pixels.empty()) CHECK(pixels[0] == 0x21 && pixels[1] == 0x43 && pixels[2] == 0x65);+ back->Release(); depth->Release(); texture->Release();+}++static void check_capture_reset(PosixDevice9 *device)+{+ D3DPRESENT_PARAMETERS next = device->Get_Present_Parameters();+ next.BackBufferWidth += 4;+ CHECK(device->Begin_Sun_Shadow(32));+ // A refused caster makes the image unusable, but successful draining still permits Reset.+ CHECK(device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 1, NULL, sizeof(float) * 3) == D3DERR_INVALIDCALL);+ CHECK(device->Reset(&next) == D3D_OK);+ const D3DPRESENT_PARAMETERS original = device->Get_Present_Parameters();+ IDirect3DSurface9 *back = NULL, *depth = NULL;+ CHECK(device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &back) == D3D_OK);+ CHECK(device->GetDepthStencilSurface(&depth) == D3D_OK);+ D3DVIEWPORT9 viewport;+ CHECK(device->GetViewport(&viewport) == D3D_OK);+ SdlGpuFrame *frame = device->Get_Gpu();+ const SdlTarget old_gpu = frame->Back_Buffer_Target();+ CHECK(device->Begin_Sun_Shadow(32));+ const uint64_t batch = frame->Batch();+ next.BackBufferWidth += 4;+ fail_acquire = true; texture_calls = 0; acquire_calls = 0;+ CHECK(device->Reset(&next) == D3DERR_DEVICELOST);+ fail_acquire = false;+ CHECK(acquire_calls == 1 && texture_calls == 0 && frame->Batch() == batch);+ // The failed reset ends the active capture and restores its scene bindings/viewport.+ check_state(device, original, viewport, back, depth, back, old_gpu);+ CHECK(device->Reset(&next) == D3D_OK);+ CHECK(device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xFF765432, 1, 0) == D3D_OK);+ std::vector<uint8_t> pixels;+ CHECK(frame->Read_Back(frame->Back_Buffer(), next.BackBufferWidth, next.BackBufferHeight, pixels));+ if (!pixels.empty()) CHECK(pixels[0] == 0x32 && pixels[1] == 0x54 && pixels[2] == 0x76);+ back->Release(); depth->Release();+}++static void check_capture_restore_allocation(PosixDevice9 *device)+{+ for (int replacement_fails = 0; replacement_fails <= 1; ++replacement_fails) {+ const D3DPRESENT_PARAMETERS original = device->Get_Present_Parameters();+ D3DPRESENT_PARAMETERS next = original;+ next.BackBufferWidth += 4;+ IDirect3DSurface9 *back = NULL, *depth = NULL, *target = NULL;+ CHECK(device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &back) == D3D_OK);+ CHECK(device->GetDepthStencilSurface(&depth) == D3D_OK);+ CHECK(device->CreateRenderTarget(original.BackBufferWidth, original.BackBufferHeight,+ D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, false, &target, NULL) == D3D_OK);+ CHECK(device->SetRenderTarget(0, target) == D3D_OK);+ target->Release(); // Deliberately no mirror yet; only the scene binding owns the surface.+ D3DVIEWPORT9 viewport;+ CHECK(device->GetViewport(&viewport) == D3D_OK);+ SdlGpuFrame *frame = device->Get_Gpu();+ const SdlTarget old_gpu = frame->Back_Buffer_Target();+ CHECK(device->Begin_Sun_Shadow(32));+ texture_calls = 0; fail_texture = 1; fail_all_textures = replacement_fails;+ const RenderResult result = device->Reset(&next);+ fail_texture = 0; fail_all_textures = false;+ CHECK(texture_calls == 4); // Scene mirror restoration, then all three replacement attachments.+ CHECK(result == (replacement_fails ? D3DERR_OUTOFVIDEOMEMORY : D3D_OK));+ if (replacement_fails) {+ check_state(device, original, viewport, back, depth, target, old_gpu);+ // Restoration left the GPU's current target at the sun capture. A device clear must resolve+ // the retained CPU scene binding again before recording, and the sun target stays alive.+ CHECK(device->Clear(0, NULL, D3DCLEAR_TARGET, 0xFF2468AC, 1, 0) == D3D_OK);+ std::string refusal;+ SDL_GPUTexture *scene = device->Get_Mirrors()->Surface(target, false, refusal);+ CHECK(scene != NULL);+ std::vector<uint8_t> pixels;+ CHECK(frame->Read_Back(scene, original.BackBufferWidth, original.BackBufferHeight, pixels));+ if (!pixels.empty()) CHECK(pixels[0] == 0xAC && pixels[1] == 0x68 && pixels[2] == 0x24);+ CHECK(device->Reset(&next) == D3D_OK);+ }+ back->Release(); depth->Release();+ }+}++int main()+{+ IDirect3D9 *d3d = Direct3DCreate9(D3D_SDK_VERSION);+ D3DPRESENT_PARAMETERS parameters = {};+ parameters.BackBufferWidth = parameters.BackBufferHeight = 32;+ parameters.BackBufferFormat = D3DFMT_X8R8G8B8;+ parameters.Windowed = parameters.EnableAutoDepthStencil = 1;+ parameters.AutoDepthStencilFormat = D3DFMT_D24S8;+ IDirect3DDevice9 *created = NULL;+ CHECK(d3d->CreateDevice(0, D3DDEVTYPE_HAL, NULL, 0, ¶meters, &created) == D3D_OK);+ if (!created) return 1;+ PosixDevice9 *device = static_cast<PosixDevice9 *>(created);+ if (!SDL_Init(SDL_INIT_VIDEO) || device->Create_Gpu_Frame(true) != D3D_OK) {+ printf("posix_reset_selfcheck: SKIP - no GPU device: %s\n", SDL_GetError());+ device->Release(); d3d->Release(); SDL_Quit();+ return failures ? 1 : 77;+ }+ check_reset(device);+ check_capture_reset(device);+ check_capture_restore_allocation(device);+ device->Release(); d3d->Release(); SDL_Quit();+ printf("posix_reset_selfcheck: %s\n", failures ? "FAILED" : "PASS: acquire/allocation failures retain reset state and queued uploads; retry succeeds");+ return failures ? 1 : 0;+}diff --git a/GeneralsMD/Code/Tests/test_sdl_gpu_depth_format.cpp b/GeneralsMD/Code/Tests/test_sdl_gpu_depth_format.cppnew file mode 100644index 000000000..16c4f343d--- /dev/null+++ b/GeneralsMD/Code/Tests/test_sdl_gpu_depth_format.cpp@@ -0,0 +1,105 @@+/*+** Copyright 2026 İlyas Akın+** Additional terms under GNU GPL section 7 apply: see LICENSE.md.+**+** This program is free software: you can redistribute it and/or modify+** it under the terms of the GNU General Public License as published by+** the Free Software Foundation, either version 3 of the License, or+** (at your option) any later version.+**+** This program is distributed in the hope that it will be useful,+** but WITHOUT ANY WARRANTY; without even the implied warranty of+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+** GNU General Public License for more details.+**+** You should have received a copy of the GNU General Public License+** along with this program. If not, see <http://www.gnu.org/licenses/>.+*/++// Exercise the production attachment selection against every combination of the five queried+// capabilities. No SDL initialization or GPU is needed, so unsupported-device fallbacks run too.+#include "SdlGpuDepthFormat.h"++#include <stdio.h>+#include <vector>++static int failures = 0;+#define CHECK(condition) \+ do { if (!(condition)) { ++failures; printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #condition); } } while (0)++enum Capability+{+ Resolve = 1,+ SampledD24 = 2,+ SampledD32 = 4,+ PlainD24 = 8,+ PlainD32 = 16+};++static SdlGpuDepthFormat select(unsigned int capabilities)+{+ std::vector<unsigned int> queries;+ const SdlGpuDepthFormat result = Sdl_Select_Depth_Format(+ [&](SDL_GPUTextureFormat format, SDL_GPUTextureType type, SDL_GPUTextureUsageFlags usage) {+ CHECK(type == SDL_GPU_TEXTURETYPE_2D);+ unsigned int capability = 0;+ if (format == SDL_GPU_TEXTUREFORMAT_R32_FLOAT) {+ CHECK(usage == (SDL_GPU_TEXTUREUSAGE_COLOR_TARGET | SDL_GPU_TEXTUREUSAGE_SAMPLER));+ capability = Resolve;+ } else {+ CHECK(format == SDL_GPU_TEXTUREFORMAT_D24_UNORM_S8_UINT+ || format == SDL_GPU_TEXTUREFORMAT_D32_FLOAT_S8_UINT);+ CHECK(usage == SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET+ || usage == (SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET | SDL_GPU_TEXTUREUSAGE_SAMPLER));+ const bool d24 = format == SDL_GPU_TEXTUREFORMAT_D24_UNORM_S8_UINT;+ capability = usage & SDL_GPU_TEXTUREUSAGE_SAMPLER+ ? (d24 ? SampledD24 : SampledD32) : (d24 ? PlainD24 : PlainD32);+ }+ queries.push_back(capability);+ return (capabilities & capability) != 0;+ });+ CHECK(!queries.empty() && queries.front() == Resolve);+ if (!(capabilities & Resolve)) {+ for (unsigned int query : queries) CHECK(query != SampledD24 && query != SampledD32);+ }+ return result;+}++int main()+{+ // Ordered requirements describe the contract independently of the query implementation:+ // sampled D24, sampled D32, ordinary D24, ordinary D32, then unsupported.+ const struct Candidate {+ unsigned int Required;+ SDL_GPUTextureFormat Format;+ bool Sampleable;+ } candidates[] = {+ { Resolve | SampledD24, SDL_GPU_TEXTUREFORMAT_D24_UNORM_S8_UINT, true },+ { Resolve | SampledD32, SDL_GPU_TEXTUREFORMAT_D32_FLOAT_S8_UINT, true },+ { PlainD24, SDL_GPU_TEXTUREFORMAT_D24_UNORM_S8_UINT, false },+ { PlainD32, SDL_GPU_TEXTUREFORMAT_D32_FLOAT_S8_UINT, false },+ { 0, SDL_GPU_TEXTUREFORMAT_INVALID, false }+ };+ for (unsigned int capabilities = 0; capabilities < 32; ++capabilities) {+ const SdlGpuDepthFormat result = select(capabilities);+ for (const Candidate &expected : candidates) {+ if ((capabilities & expected.Required) != expected.Required) continue;+ if (result.Format != expected.Format || result.Sampleable != expected.Sampleable) {+ ++failures;+ printf("FAIL capabilities=%u: got format=%d sampled=%d; expected format=%d sampled=%d\n",+ capabilities, result.Format, result.Sampleable, expected.Format, expected.Sampleable);+ }+ break;+ }+ }++ // Regression: sampled D32 must not displace ordinary D24 when the R32F resolve is unavailable.+ const SdlGpuDepthFormat fallback = select(SampledD32 | PlainD24 | PlainD32);+ CHECK(fallback.Format == SDL_GPU_TEXTUREFORMAT_D24_UNORM_S8_UINT && !fallback.Sampleable);+ if (failures) {+ printf("sdl_gpu_depth_format_selfcheck: %d FAILED\n", failures);+ return 1;+ }+ printf("sdl_gpu_depth_format_selfcheck: 32 capability combinations and D24 compatibility fallback hold\n");+ return 0;+}