Skip to content

60 fps for C&C 3 Tiberium Wars and Kane's Wrath: cnc3_fps_patch baked into the exe at conversion - #24

Open
marcocebrian wants to merge 33 commits into
nullmoth:mainfrom
marcocebrian:pr/13-sage-fps
Open

marcocebrian wants to merge 33 commits into
nullmoth:mainfrom
marcocebrian:pr/13-sage-fps

Conversation

@marcocebrian

Copy link
Copy Markdown

Stacked on #23. The new commits here (sage_fps:) add a game-specific patch, applied only to the games it supports.

60 fps for Command & Conquer 3: Tiberium Wars and Kane's Wrath. Their logic runs at 15 ticks a second and the drawing at 30 fps. CNCStuff/cnc3_fps_patch (WTFPL) raises the drawing to 45, 60, 75 or 90 fps and keeps game speed, build times and multiplayer timing unchanged. On Windows it is a dinput8.dll that rewrites the game's code in memory. DAISY converts the code ahead of time, so here the same edits are made to the exe instead, when DAISY converts the game. Nothing in DAISY's engine changes.

games/sage_fps/:

  • upstream/: the patch's sources at 4b8d499, with a few marked edits listed in upstream/UPSTREAM.
  • host/resolve_host.c: runs upstream's resolver on the Mac, so every patch site is found by signature as upstream does.
  • i686/ + coff_link.py + pe_sections.py: the hook code is built for 32-bit Windows (clang, COFF) and linked into two new sections of the exe.
  • bake.py: writes the 26 code edits upstream's installer would write, each checked against upstream's expected bytes. At run time upstream's installer runs as usual: it finds every site already patched and makes its data changes. Until then, and if it fails, the hooks behave like the stock 30 fps game.
  • tools/build.py calls it for every converted program. Anything that is not a supported exe is left alone: Kane's Wrath 1.02 (EA 2009, Steam 2012), 1.03; Tiberium Wars 1.09, 1.10. DAISY's loader builds the image from the exe in the game folder, so the baked exe goes there:
    • into the app's copy with --data;
    • otherwise in place, with the original kept as <exe>.orig.
  • DAISY_SAGE_FPS=off converts without it and restores the original exe; 45|60|75|90 picks the rate.

Multiplayer. Upstream reports desyncs in some long online games, most likely through map scripts waiting for sounds. Every player in a LAN game should use the same DAISY build.

Testing:

  • bash games/sage_fps/tests/run.sh: 21 tests cover the resolver, the COFF linker, the PE sections, the bake (every old byte matches, no overlaps, every redirect reaches the hooks) and the command line. The tests use a Kane's Wrath exe when one is present.
  • Kane's Wrath 1.02 (EA 2009) on an M5 Pro: "FPS patch installed successfully", 60 fps in the menus and skirmishes, and normal game speed.
  • Tiberium Wars 1.9 bakes (26 edits) and converts; it has not been played with the patch yet.

Fixes found bringing up C&C 3: Tiberium Wars (EA SAGE, Direct3D 9) from "closes
at once" to playable skirmishes, each written against Windows behaviour so other
games of the era benefit. docs/32bit-game-fixes.md describes every change.

Conversion
- Launchers: convert the program a launcher runs (SkuDef set-exe, a name the
  launcher holds, or its own name with another extension), from a sub-folder,
  with the launcher's arguments (%GAMEDIR% expanded at start).
- System libraries a game expects Windows to provide (msvcr80/90, d3dx9_24-43),
  fetched from Microsoft's redistributables (tools/fetch_sys32.sh) and rewritten
  with the game; msvcrt.dll served by whichever C runtime is built in.

Rewriter
- Programs linked without relocations: find code through pointer-looking values
  (never written back), switch tables bounded by their compare, overlapping
  starts; progress on stderr.
- ud0/ud1, enter's unsigned size, notrack, jcxz, far jecxz/loop targets.

Runtime and window layer
- 55 missing Windows functions (runtime/win_extra32.c); tools/missing32.py lists
  what a game still lacks; crash reports show the game's call chain.
- Exclusive fullscreen at the mode's size with mouse mapping; native display
  resolutions; DirectInput fed in the full window layer; a clipped visible
  cursor keeps moving; text into 24/16-bit bitmaps; .cur/.ani cursors.

Direct3D 9
- Lost devices on deactivation, freeing released textures and buffers,
  UpdateSurface rectangles, indexed draws without an index buffer, indexed
  pixel shader inputs, a GeForce 8800 GTX adapter identity.
…n game on its Space

StretchRect between rectangles of different sizes now draws the source into
the destination (point or linear); it copied the top-left part, so distortion
effects that shrink the frame smeared a magnified corner across the view.
GetAdapterDisplayMode reports a fullscreen device's mode, otherwise the
display's pixels (not points): native resolution survives applying options.
Arriving on the Space of a fullscreen window activates its program, so a lost
device is reset instead of staying lost. Notes in docs/32bit-game-fixes.md.
With D3DRS_POINTSPRITEENABLE, texture coordinate set 0 is the position across
the sprite and the size is the one the vertex shader writes (oPts or a PSIZE
output). Sprites sampled a single texel and drew flat squares: C&C 3's
Tiberium gas and smoke were hard-edged rectangles. Notes updated.
…D3DX

- Texture uploads: UnlockRect uploads only the locked rectangle, staged in the
  frame ring and batched in one blit pass (was a new Metal buffer, a command
  stream break and the whole level per unlock).
- Low heap: freed memory is zeroed keeping 16 KB hardware pages whole, so
  Metal can wrap Direct3D buffers placed there (newBufferWithBytesNoCopy
  returned nil under Rosetta's 4 KB pages and draws were dropped); buffer
  memory is 16 KB aligned.
- Accelerated exports: a native registered under a library family name
  (d3dx9.dll) serves that export of every d3dx9_NN.dll, falling back to the
  library's own code. First: D3DXLoadSurfaceFromMemory for 32-bit surfaces,
  whose per-pixel x87 conversion froze C&C 3 for ten seconds after each Reset.
- Window layer: WM_ACTIVATEAPP/WM_ACTIVATE follow the application's own
  activation for a fullscreen window.
- Diagnostics: PE2_TRACE_BURST logs calls and messages after each Reset;
  the frame-rate line reports uploads.
- docs/32bit-game-fixes.md: the above, and what is still open.
…ut focus

- Shaders: c[a0 + n] reads the shader's own def value for a register it
  defines, the program's constant otherwise, and 0 outside the register
  file (was clamped into the program's constants). C&C 3's particle quad
  corners live in def c21..c24 read through c[a0.w + 16]: a refinery's smoke
  became beams and a screen-wide haze (the white flash).
- Window layer: an exclusive-fullscreen window is minimised while its
  program is in the background (IsIconic, WM_SIZE SIZE_MINIMIZED, WM_ACTIVATE
  minimised flag) and restored on return.
- DirectInput: DISCL_FOREGROUND devices are unacquired on deactivation
  (DIERR_INPUTLOST, then DIERR_OTHERAPPHASPRIO until active again).
- Diagnostics: PE2_DRAWLOG_TRIGGER (log draws and save frames for a moment
  on demand), PE2_SKIP_SHADER (drop an effect's draws), W64_MSGLOG.
- docs/32bit-game-fixes.md: the above; the flash is solved, the pause menu
  on return is still open with what was found.
…twork access

- gethostbyname of the machine's own name lists loopback only when there is
  no other address, as Windows does. C&C 3 sorted the list, took 127.0.0.1
  and then announced its LAN games on loopback only.
- Converted apps declare NSLocalNetworkUsageDescription: macOS refuses LAN
  broadcasts ("no route to host") until the user allows it.
- PE2_TRACE_SOCK logs failed sendto calls.
- A standalone surface's hidden texture no longer holds its own reference,
  so releasing the surface frees both. None were ever freed: C&C 3's LAN
  lobby creates hundreds of offscreen surfaces a second and ran the low
  memory out in two minutes (crash in res_new on a null allocation).
- The five-second frame-rate line reports the low memory in use.
…nged state per draw; 16x anisotropic filtering; stall counters
…profiler; input log; missing-function messages once; notes on method
…er compiles again (32-bit-only D3DX hook and trace guarded, SH_FREE); build_app.sh DAISY_FROM reuses a built app's outside parts
… on Windows (bound to any address; getsockname reports the one asked for)
… count from the bytecode missed m4x4's rows: transparent units on M1 Macs); constant-poisoning and no-state-cache diagnostics
…x); converted apps draw at the screen's real pixels (high-resolution capable)
…A; the game's anti-aliasing option now works
…ALE=sharp keeps pixel doubling at exact multiples)
…ecked for it and chose none (PE2_AA overrides)
…W) and the keyboard-name functions in both runtimes; C&C 3 tooltips show their hotkeys
… a third (PE2_ALL_MODES=1 for the long list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant