Skip to content

UI: TextureRenderCore's moveOverlappingMenuPartsToMapSurface at 100% - #476

Open
kotijekriv wants to merge 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round50-100
Open

kotijekriv wants to merge 1 commit into
sourcehold:mainfrom
kotijekriv:reimpl/UI-B-round50-100

Conversation

@kotijekriv

Copy link
Copy Markdown
Contributor

One function at 100%, from our draft in #363 (96.5% there), which started from gynt's asm draft. No header changes. One behaviour fix against most versions of his draft (below).

Function Address Before reccmp raw Normalised
TextureRenderCore::moveOverlappingMenuPartsToMapSurface 0x00454F00 96.5% (#363) 100.0% 100.0%

gynt's draft goes back to 4696942 (2026-10-01, "reimplement: SHC_3BB0A8C1_0x00454F00 24.60% Style pass, asm differs"); 196c54f is the same file, and it is what #255 had when it closed. His best is c2e5474 (2026-10-04, "reimplement: SHC_3BB0A8C1_0x00454F00 68.0% Asm copy loop recovered, fixed missing return at 800x600, ..."), on sourcehold's reimpl/TextureRenderCore. The newest version is cee658d (2026-11-02, "style: rules 1, 3, 15 and 22 over UI/Rendering, BuildingMenus, Helpers and the rest") on his fork's reimpl/UI-MenuItems-development, and 8ca7861 on reimpl/Wilps-2026-10-18 has his newer export.

Measured

  • Setup: the bundled MSVC1400-SP1 against the Steam Stronghold Crusader.exe (3bb0a8c1…5387ac5a), and sourcehold's reccmp with --resolve-wrapped-calls --ignore-security-cookie. The struct resolvers and the function resolvers are enabled locally (not committed).
  • Narrow build: this commit's file alone, on main 0e4a83b: 100.0 raw and normalised, with no diff line.
  • Full build: an all-on build of this branch's previous cut on main 0e4a83b (the same code: this cut only changes comments, four comment lines out and five in, and compiles to the same listing), with both resolver sets on, and with 0x004314E0 (the MapEditorLandscaping_GeneralButtons handler) left out and its resolver off, as in main's baseline: 0 failed of 2,426 compile steps, 99.58 over 2,442 functions. The new function reads 100.0 raw and normalised there too, with no diff entry. Against main's baseline build (made at f489b72, 2,439 functions) the other changes are main's own since then (One more UI::MenuModals function at 100%: ProgressBarBox #467's MenuModalRenderFunction_ProgressBarBox and UI: the single-player game launch at 100% #466's LaunchSinglePlayerGameUnk at 100.0, Constructor_MenuModal_ProgressBarBox at 100.0 instead of 93.8) and two functions that read lower only in how reccmp prints a constant: Menu::handleMenuItems (0x004F6280) reads 97.9 (91.4 normalised) and Menu::Constructor_Menu (0x004F4100) 98.4 (95.2), both 100.0 before. Both compare a menu item's type with the constant 0x11000000, five compares in all, and in this build's layout a symbol of our DLL lies at exactly that address, so reccmp prints the constant as the symbol's name on our side; the instructions are the same on both sides (UI: the single-player game launch at 100% #466 describes the same two readings). Every other function reads exactly what it read before (unrounded 99.5775 raw and 99.5475 normalised, 2,324 at 100.0 raw; with those two at 100.0 it would be 99.5790 and 99.5529, 2,326, as the same main with one other function at 100% reads).
  • Instructions: the original's 100, in its order, the pixel loop (handwritten in the original) included.
  • Checks: our local style checker gives 0 ERROR and 12 CHECK (below).
  • Not run in game.

What the function does

When a menu tab is open, the parts of it that reach over the map are copied from the menu surface onto the map surface, all but the magenta pixels. The tab's records in BlendingDefinedData hold x, y, width and height; -1 ends them, and the records after a -2 lie outside the 800 pixels of the menu, so at 800x600 the function stops there. The copy is the original's handwritten pixel loop, as inline assembly.

Behaviour against the draft

The rest does what the draft does.

What got it to 100%

Each of these was measured against the alternative it names:

  • The zero-based tab (tab - 1) as a value of its own. The original keeps the -5 of (tab - 1) * 5 in its lea (lea ecx, [eax + eax*4 - 5], then the shift). Written into the -1 test's index, the -5 is folded into that address. A redundant conditional (tab != 0 ? tab - 1 : 0) gives the same code as the local.
  • The record index computed a second time, in a local of its own, as the original does. With one shared index the second computation goes (98 instructions to the original's 100).
  • The four record pointers in the original's order (width, x, height, y).
  • The end test at the bottom of a for (;;), as in the original. With the test in the for's header (for (; *x != -1; y += 4, height += 4, width += 4, x += 4)), a narrow build reads 80.4% (70.6% normalised), with 104 instructions to the original's 100: the first record's x is tested once more in front of the loop.

Why the pixel loop is inline assembly

There is no mov reg, 0 here. The loop shows the inline-asm shape instead: each of its inputs is stored to a stack slot and loaded straight back into esi, edi, ecx or edx just before it (0x00454FA1 to 0x00455002), the width is reloaded from its slot on every row, the colour key sits in bx, and the function keeps a frame pointer. gynt's c2e5474 already had these 17 instructions as inline asm.

Checker CHECKs (not changed)

  • 5 x field_0x3014 is a placeholder: in BlendingDefinedData it holds the menu tabs' records of the parts that reach over the map (x, y, width, height per record; -1 ends a tab's list, and the records after a -2 lie outside the 800 pixels of the menu). Not renamed here (see below).
  • 4 x pointer to a field: the four record pointers, which the original keeps in stack slots and steps by one record (16 bytes) each; one index instead loses the match (93 instructions to 100). The code says so in one comment line.
  • SG 24 and "endless loop left by a break": the for (;;) with its end test at the bottom, see above.
  • Inline asm block: the pixel loop, 17 instructions, the original's handwritten loop and nothing else. Every operand is a C++ local except the colour key, which the original reads from the global COL_MAGENTA itself, so the block names COL_MAGENTA::instance.shortValue. Main's drawTgxOnFlaggedSurface names DAT_TextureRenderCoreObject::instance.currentRenderSurface the same way. The (void)sizeof(COL_MAGENTA::instance) line lets inline asm name it, as on main in ApplyBlending, BlitMapImageWithVerticalClip, RenderTerrainTilesCenterPiece and five PencilRenderCore files. The surface pointers, the line jumps and the record walk are C++.

Proposed, not changed here

  • The table's shape and name. field_0x3014 is int[55][8] in main's header, nothing else on main uses it, and in the exe only this function reads it. The exe's data at 0x00AB8730 is 11 tabs of 10 records of x, y, width and height (160 bytes a tab), so tabIndex * 5 is five rows of eight ints a tab; -1 ends each tab's list (the last tab has none), and every record after a -2 lies outside the 800 pixels of the menu: (800, 417, 22, 55), (-49, 464, 57, 8) and (869, 461, 36, 11). Proposed: a record struct { int x, y, width, height; } as [11][10], with a name such as menuTabMapOverlapRects. Not changed here: the match hangs on how the index is written, so a retyped table needs its own measurement (a [11][40] shape did not help at 84.8%, before the tabIndex change), and it is a header change of its own.
  • 4056. Main's ViewportConstants has VC_MAP_SURFACE_HEIGHT = 2076 but no width, and main's .cpp files write 4056 on 35 lines, with a comment as here (drawTgxOnFlaggedSurface, in the same class). Proposed: VC_MAP_SURFACE_WIDTH = 4056. Not added here: it is a header change, and its use belongs in all 35 places at once.
  • The -1 and -2 markers have no enum on main; the code says what they mean in a comment.

🤖 Generated with Claude Code

TextureRenderCore::moveOverlappingMenuPartsToMapSurface: copies the parts of the open menu tab that reach over the
map from the menu surface onto the map surface, all but the magenta pixels. The tab's records hold x, y, width
and height; -1 ends them, and the records after a -2 lie outside the 800 pixels of the menu, so at 800x600 the
function stops there. The pixel copy is the original's handwritten loop, as inline assembly. From gynt's draft:
his 68.0% c2e5474 ("Asm copy loop recovered, fixed missing return at 800x600") on sourcehold's
reimpl/TextureRenderCore, the newest cee658d (2026-11-02) on his fork's reimpl/UI-MenuItems-development.

Behaviour against the draft: at 800x600 the original returns at the first -2 marker (0x00454F64), so the records
behind it are not drawn. c2e5474 and 8ca7861 have that return; 4696942 / 196c54f (the 24.6% file,
which is what sourcehold#255 had when it closed), 81e1041, 5c82ddb, dd14331, 63cc49f and cee658d have
an empty `if` there and draw them anyway.

What the match needed: the zero-based tab (`tab - 1`) as a value of its own (written into the -1 test's index,
its -5 is folded into that address); the record index computed a second time in a local of its own, as the
original does; the four record pointers in the original's order; the end test at the bottom of a `for (;;)`.

Measured in a narrow build of this file on main 0e4a83b with both resolver sets on: 100.0% raw and normalised.
Not run in game.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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