Skip to content

Reimplement 10 UI::Rendering PencilRenderCore functions at 100% - #370

Merged
gynt merged 12 commits into
sourcehold:mainfrom
kotijekriv:reimpl/UI-PencilRenderCore-2-100
Nov 7, 2026
Merged

gynt merged 12 commits into
sourcehold:mainfrom
kotijekriv:reimpl/UI-PencilRenderCore-2-100

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Ten more UI::Rendering PencilRenderCore functions at 100%, each with its status line: the horizontal, vertical and dimmed lines, a single pixel, the 4x4 pixel pattern, the two rounded boxes, the two bordered boxes with a blended background, and drawLine.

All ten continue gynt's drafts, in an area he agreed on Discord (2026-11-06) we may take: his early commits on the address, his closed #255, and his fork's reimpl/UI-Rendering-development (63cc49f). None is in his open #294. Five of them were in our draft #360, which drops them in the same push. Every commit body cites his commits by hash and says what was already his and what is new, and credits him as co-author. No header change is left: gynt's Ghidra sync e32319b took the split of TextInputDefinedData's corner tables (below) into main, and main is merged in (d15a7af); all ten still read 100.0%.

Other PRs: #358 (the first five PencilRenderCore functions); the ready #348, #349, #352, #353, #354, #357, #359, #364, #369 and #368 (a fix for main); the drafts #313, #318, #321, #327, #328, #356, #360 and #363.

Built on top of main (eef6f66) with the bundled MSVC1400-SP1, natively on Windows, against the Steam Stronghold Crusader.exe (3bb0a8c1…5387ac5a). All ten, together with #358's five, read 100.0% raw and normalized in one alone build, with the struct and function resolvers enabled locally (not committed), using sourcehold's reccmp with --resolve-wrapped-calls --ignore-security-cookie. None of it has been run in game.

It went through two fresh-eyes reviews (each function compared instruction by instruction with the original, every attribution checked against the whole history; their points applied and measured), openshc-pr-check with no errors, and status_regression.py, which finds nothing below a known score on main or any fork.

Header change

Update: on main since e32319b.

TextInputDefinedData::field_0x0 is two tables, of 5 and 6 rows (d6f40ed): field_0x0[11] at 0x00B37CBC holds 5, 3, 2, 1, 1 and then 6, 4, 3, 2, 1, 1, two tables of row indents for rounded box corners, each starting with its row count, as field_0x2c[22] at 0x00B37CE8 (22, 19, 16, ..., 1, 1) is the RBERL_STRONG one. The exe uses only the first five, for RBERL_SLIGHT (5): the two rounded-box functions load 0x00B37CBC (0x00470EFE, 0x004710BE) and 0x00B37CE8 (0x00470EF7, 0x004710B7), and nothing else in .text or in any data section points into the range below 0x00B37D40, where UserTextHandler_CharacterFilter_B_1_8_9 starts. So it becomes int field_0x0[5] and int field_0x14[6], in Ghidra's naming. The struct's size is unchanged, and main's seven other users of the struct read other fields and keep their code.

The handwritten loops

The original's three line functions, drawCurrentPixel and drawPixelPattern4x4 are handwritten assembly after a few compiler-generated statements (AGENTS.md's signals: mov reg, 0, mul for the row offset, a fixed mov edx, 2 step). Each is a normal function: the field copies are int const locals, as the original copies them into its frame, and one __asm block reads them from there, as main's renderMinimapPreview does. The blocks are as small as they can be (gynt on #323). The surface and stride always come from the global pencil (0x0191D720 / 24 / 28), whatever this is, and a sizeof line instantiates DAT_PencilRenderCore before the asm names it, as main's ApplyBlending does. drawPixelPattern4x4's colours are named by their RGB565 channels and assemble to the original's immediates.

Behaviour fixed compared with the drafts

  • drawLine (0x00472B20): the original returns after each call (0x00472B5B, 0x00472B6D, 0x00472B7C). The draft had none, so every line also went through drawDiagonalWiderThanHighUnk, a vertical one through drawDiagonalHeigherThanWideUnk too, and a single point through drawVerticalLine, drawHorizontalLine and drawDiagonalWiderThanHighUnk.
  • The two rounded boxes (0x00470EE0, 0x004710A0): a rounding level other than 5 or 22 returns without drawing (jne 0x00470FEF), where the drafts went on with the strong table; and drawStartX / drawEndX are restored once after the first loop, where the drafts reset them on every pass.
  • The two bordered boxes (0x00471340, 0x00471440): the background goes through drawBlendedBlackBox on the global pencil (mov ecx, 0x0191D720), where the drafts passed this.
Function Address gynt's commits
drawHorizontalLine 0x00468DE0 f98d57a, b8ff55d, 63cc49f
drawVerticalLine 0x00468E40 e24b66b, 88f785c, 63cc49f
dimHorizontalLine 0x00468EA0 82f902f, 18ad79e, 63cc49f
drawCurrentPixel 0x00468F20 9f37b52, cc7f83f, 63cc49f
drawPixelPattern4x4 0x00468F70 f19bf37, ec6bf49, c5cc82e, 63cc49f
drawBoxWithRoundedEdgesAndColor 0x00470EE0 4ac54a9, b77851e, 63cc49f
drawBoxWithRoundedEdges 0x004710A0 8fcbd14, c890ad0, 63cc49f
drawBorderedBoxWithBlendedBackground 0x00471340 8e91764, c6809d4, 63cc49f
drawBorderedBoxWithCustomBlendedBackground 0x00471440 16efb3f, 08d3970, 7cca78d, 63cc49f
drawLine 0x00472B20 8a94992, dbccd67, 63cc49f

What was his and what is new is in each commit body: the dispatch, the table choice and loop shapes, the picture scheme and the blend formula are his; the asm transcriptions, the fixes above, the names and the types are new.

Kept as they are, and why

Each was measured; a one-line comment in the code says why.

  • The bordered boxes round width and height up in place, as the original stores the rounded width in width's own stack slot, which puts xPos + width in its lea order (a fresh local: quick_diff 0.9720 -> 0.9813 the other way).
  • Their tile picture keeps the original's nested shape (a flat else-if chain: quick_diff 0.8000 and 0.9637).
  • The rounded boxes choose the table with a switch, for the original's cmp 5 / je, cmp 0x16 / jne order.
  • The background blend stays (blendStrength - 32) * 16 / 32 + 32: the original's shl / sar, which / 2 doesn't give.

Proposed, not changed here

  • Names for the corner tables (Ghidra's side): field_0x0 -> roundedBoxSlightRowIndents, field_0x2c -> roundedBoxStrongRowIndents; field_0x14 is never read.
  • These call functions that are still Pending: setupPencil (0x00468C80), drawBlendedBlackBox (0x00471000) and renderGMWithAlphaMask (0x004557B0, in Reimplement 23 UI::Rendering TextureRenderCore functions at 100% #364).

🤖 Generated with Claude Code

kotijekriv and others added 11 commits November 7, 2026 22:06
field_0x0[11] at 0x00B37CBC holds 5, 3, 2, 1, 1 and then 6, 4, 3, 2, 1, 1: two tables of row indents for rounded
box corners, each starting with its row count (a corner's outermost row is indented by its radius), as
field_0x2c[22] at 0x00B37CE8 (22, 19, 16, ..., 1, 1) is the RBERL_STRONG table. The exe uses only the first five:
drawBoxWithRoundedEdgesAndColor and drawBoxWithRoundedEdges load 0x00B37CBC for RBERL_SLIGHT (5) (mov ebx, 0xb37cbc
at 0x00470EFE and 0x004710BE) and read rows 0 to roundingLevel - 1 of it. Nothing refers to the 6-row table at
0x00B37CD0: no instruction in .text has an immediate or a displacement from 0x00B37CBC up to below 0x00B37D40,
where UserTextHandler_CharacterFilter_B_1_8_9 starts (0x00469984 and 0x00474146 read that address itself), other
than those two loads and mov ebx, 0xb37ce8 (0x00470EF7, 0x004710B7), and no dword in any section points into that
range. So field_0x0 becomes int[5] and the 6-row table field_0x14 int[6], named in Ghidra's style. The struct's
size is unchanged and no user's code changes: the two rounded-box functions in this branch take field_0x0 as an
int*, and main's other users of the struct (the three file filters and four FontSizeClass functions) read other
fields.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PencilRenderCore::drawHorizontalLine, from gynt's draft (f98d57a, 21.05%, "orig keeps this in ecx at /Od";
b8ff55d, 21.1%, in his closed sourcehold#255; the same body at 63cc49f on his fork's reimpl/UI-Rendering-development).
His plate comment already found what the function does: it copies drawStartX, drawStartY, drawEndX and drawColor
into locals and writes the row through the global pencil's surface and stride (0x0191D724 / 0x0191D728), not
through this.

New here: the loop is handwritten assembly in the original (mov edx, 2 as the step, the row offset through mul), so
it is transcribed into an __asm block, as main's renderMinimapPreview does; the four copies are int const locals
the asm reads from their frame slots; the asm block is what gives the frame pointer and the saved ebx / edi, so his
#pragma optimize("gy", off) goes; and a sizeof line instantiates DAT_PencilRenderCore before the asm names it, as
main's ApplyBlending does. reccmp 100.0%.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PencilRenderCore::drawVerticalLine, from gynt's draft (e24b66b, 21.05%, "orig keeps this in ecx at /Od";
88f785c, 21.1%, in his closed sourcehold#255; the same body at 63cc49f on his fork's reimpl/UI-Rendering-development).
His draft already had the shape: four local copies, the global pencil's surface, and a step of the global's
horizontalByteSize per row.

New here, as in drawHorizontalLine: the loop transcribed from the binary into an __asm block (it reads the stride
into edx again for the step), the copies as int const locals, the pragma gone and the sizeof line. reccmp 100.0%.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PencilRenderCore::dimHorizontalLine, from gynt's draft (82f902f, 15.56%, "orig keeps this in ecx at /Od";
18ad79e, 15.6%, in his closed sourcehold#255; the same body at 63cc49f on his fork's reimpl/UI-Rendering-development).
The mask choice by the colour mode and its explanation are his: the mask clears the lowest bit of every colour
channel before the shift, 0xf7de for RGB565 and 0x7bde for RGB555, so that halving a pixel moves no bit into the
channel below.

New here: the loop transcribed from the binary into an __asm block like drawHorizontalLine; the masks named and
written as every bit but the lowest of each channel (no hex left for the style-lint bot); the test written
colorBitMode == RGB_555 first, as main's ApplyBlending does, which compiles to the original's neg / sbb / and
0x7c00 / add 0x7bde as the other order does; int const copies; the pragma gone and the sizeof line. reccmp 100.0%.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PencilRenderCore::drawBoxWithRoundedEdgesAndColor, from gynt's draft (4ac54a9, 29.47%; b77851e, 29.5%, in his
closed sourcehold#255; at 63cc49f on his fork's reimpl/UI-Rendering-development it no longer compiles on today's headers,
TextInputDefinedData::field0_0x0). The table choice by rounding level (0x00B37CBC for RBERL_SLIGHT, 0x00B37CE8 for
RBERL_STRONG) and the three-loop shape (the first rows indented by the table from the top edge, the middle rows
full width, the last rows by the table backwards) are his.

New here, fixed against the binary: any other rounding level returns without drawing (jne 0x00470FEF), where the
draft went on with the strong table; the tables are the fields field_0x0 and field_0x2c of DAT_TextInputDefinedData
(split by the header commit before) rather than a cast address; a switch gives the original's cmp 5 / je, cmp 0x16
/ jne order; drawStartX / drawEndX are restored once after the first loop, where the draft reset them on every
pass; and the middle loop decrements the height before it moves drawStartY, both in the for's step. quick_diff
leaves only an alignment jmp; reccmp 100.0%.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PencilRenderCore::drawBorderedBoxWithCustomBlendedBackground, from gynt's draft (16efb3f, 36.87%; 08d3970,
36.9%, in his closed sourcehold#255; 7cca78d, 40.93%, and the same body with his style passes at 63cc49f, both on his
fork's reimpl/UI-Rendering-development). The drawing is his: the box rounded up to whole 24-pixel tiles, the
pictures of GID_INTERFACE_ICONS_3 by row (1, 7 and 13 start the top, middle and bottom rows, + 1 and + 2 for the
middle and right tiles, + 3 for each mask), the inside tiles skipped, the renderGMWithAlphaMask call per tile and
the background's (blendStrength - 32) * 16 / 32 + 32.

New here: drawBlendedBlackBox on the global pencil (mov ecx, 0x0191D720), where the draft passed this; width and
height rounded up in place, as the original stores the rounded width in width's own stack slot, which puts xPos +
width in the original's lea order (quick_diff 0.9720 -> 0.9813, where only the imul constant's display differs);
the pictures named; and the row's first picture by an if / else if chain instead of the masked ternary. The tile's
picture keeps the original's nested shape (a flat else-if chain: quick_diff 0.8000). reccmp 100.0%.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PencilRenderCore::drawCurrentPixel, from gynt's drafts. 9f37b52 (0.00%, "orig is /Od, frame 0xc") and cc7f83f
(0.0%, in his closed sourcehold#255) write the pixel through this, with no locals, no global and no pragma. At 63cc49f on
his fork's reimpl/UI-Rendering-development he copied currentX, currentY and drawColor into locals, wrote the pixel
through the global pencil's surface and stride, and added #pragma optimize("g", off); our 3e7ec7e (4.5%, in the
draft sourcehold#360) is that body.

New here: the store is handwritten assembly in the original, transcribed from the binary like drawHorizontalLine,
with the copies as int const locals and the sizeof line. The original is not /Od: it has no frame pointer (sub esp,
0xc; locals at esp), which is what MSVC builds for this function, so the pragma goes. quick_diff leaves only ret /
ret 0; reccmp 100.0%.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PencilRenderCore::drawPixelPattern4x4, from gynt's drafts (f19bf37, 0.00%; ec6bf49, 0.0%; c5cc82e, 4.8%,
"Running row offset and global instance"; at 63cc49f on his fork's reimpl/UI-Rendering-development the same rows,
with the row pixels and the right pair in two locals, the right pair kept across three rows as the original keeps
it in ebx; our 2c99182, 9.3%, in the draft sourcehold#360, is that body). The pattern is his: the four rows of two dword
stores through the global pencil's surface, with the original's pixel pairs and a running row offset.

New here: the whole body is handwritten assembly in the original (mov ecx, 0 and mov eax, 0 materialise zero), so
it is one __asm block transcribed from the binary; the colours are named by their RGB565 channels (PATTERN_GREY
0x7bcf, PATTERN_WHITE 0xffff, PATTERN_DARK_GREY 0x39c7) and each pixel pair is built from them in the asm operand,
which assembles to the original's immediates (0xffff7bcf, 0x39c70000, 0x7bcf7bcf, 0x39c739c7). No frame pointer, as
in the original. quick_diff differs only in how it displays the constants; reccmp 100.0%.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PencilRenderCore::drawBoxWithRoundedEdges, from gynt's draft (8fcbd14, 32.43%; c890ad0, 32.4%, in his closed
sourcehold#255; the same body with his style passes at 63cc49f on his fork's reimpl/UI-Rendering-development; our
ea42833, 38.9%, in the draft sourcehold#360). It is drawBoxWithRoundedEdgesAndColor with colour 0 for setupPencil and
dimHorizontalLine for drawHorizontalLine, so it darkens a rounded box instead of filling it; that, the table choice
and the three-loop shape are his, as in that function.

New here: the same fixes as in drawBoxWithRoundedEdgesAndColor (other rounding levels return, the tables as fields,
the switch, drawStartX / drawEndX restored once after the first loop, the middle loop's decrement order). The exe's
two functions differ in push 0 at 0x004710D4, where the other loads the colour (and the stack offsets and registers
that follow from one argument less), and in the three calls to 0x00468EA0. quick_diff 1.0000; reccmp 100.0%.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PencilRenderCore::drawBorderedBoxWithBlendedBackground, from gynt's draft (8e91764, 31.79%; c6809d4, 31.8%, in
his closed sourcehold#255; the same body with his style passes at 63cc49f on his fork's reimpl/UI-Rendering-development;
our 8543e6b, 32.8%, in the draft sourcehold#360). It is drawBorderedBoxWithCustomBlendedBackground with fixed blends, 0 for
the border tiles and 16 for the background; the drawing and both blend values are his, as in that function.

New here: the same changes as in drawBorderedBoxWithCustomBlendedBackground (drawBlendedBlackBox on the global
pencil, mov ecx, 0x0191D720, where the draft passed this; the in-place rounding; the names; the if / else if chain
for the row; the tile's nested shape, which a flat chain loses, quick_diff 0.9637). quick_diff 0.9792, where only
the imul constant's display differs; reccmp 100.0%.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PencilRenderCore::drawLine, from gynt's draft (8a94992, 47.89%; dbccd67, 47.9%, in his closed sourcehold#255; the same
body with his style passes at 63cc49f on his fork's reimpl/UI-Rendering-development; our cfbab8e, 56.3%, in the
draft sourcehold#360). The four calls and their conditions are his.

New here: a return after each call (ret at 0x00472B5B, 0x00472B6D, 0x00472B7C). The draft had none, so every line
also went through drawDiagonalWiderThanHighUnk, a vertical one through drawDiagonalHeigherThanWideUnk too, and a
single point through drawVerticalLine, drawHorizontalLine and drawDiagonalWiderThanHighUnk. quick_diff 1.0000;
reccmp 100.0%.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the declaration/type change This requires changes to the generated/exported types or definitions. label Nov 7, 2026
…ore-2-100

e32319b carries this branch's header change, TextInputDefinedData::field_0x0[5] and field_0x14[6] (d6f40ed),
so the branch no longer changes any header. No conflicts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot removed the declaration/type change This requires changes to the generated/exported types or definitions. label Nov 7, 2026
@gynt
gynt added this pull request to the merge queue Nov 7, 2026
Merged via the queue into sourcehold:main with commit 1246762 Nov 7, 2026
4 checks passed
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.

2 participants