317 Commits
Author SHA1 Message Date
Valiantsin DzerakhandClaude Opus 5.5 6151ac4fbc Merge task/bindoff: TOTK_BIND_INC is off by default
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 19:11:50 +03:00
Valiantsin DzerakhandClaude Opus 5.5 ccf9837764 TOTK_BIND_INC is off by default
With it on, a GTX 1050 Ti lost the device at one place in the world, after the validation layer reported a descriptor
pool reset while a command buffer still used it and binds of that pool's sets. The cause is not found and the slower
diagnostic runs did not bring the loss back. bind_inc saves about 1% of the frame, so it waits until the cause is known;
bind_inc = 1 in totk.ini still turns it on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 19:11:50 +03:00
Valiantsin DzerakhandClaude Opus 5.5 c418dbef04 Merge task/pjournal: a device loss logs the last descriptor pool events, and a pool with two owners is logged at once
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 18:55:52 +03:00
Valiantsin DzerakhandClaude Opus 5.5 d6715029e5 A device loss logs the last descriptor pool events, and a pool with two owners is logged at once
The replay keeps a ring of the last 1024 descriptor pool events (a CbRes made, a pool handed to a CbRes from the spare
list or new, a pool reset or destroyed), a few per submission and nothing per draw. vk_device_fault_report logs the
newest 256 after a device loss. Each pool's owner is checked under spare_lock as the pool is handed out and as it is
reset: a pool handed out while another CbRes holds it, or reset by a CbRes that does not hold it, is logged at once
with the 64 events before it. On a GTX 1050 Ti with TOTK_BIND_INC on, the validation layer reported a descriptor pool
reset while a command buffer used it, then binds of its sets, before a device lost that slower diagnostic runs did
not bring back; the next one names the pool's owners.

--probe-vkexec gains a set batch part (60 submissions of batched sets with splits and empty submissions beside them),
a pool owner part (one pool handed to two CbRes, then reset by the one that no longer holds it: two conflicts) and
calls the device loss report on the working device to check that it comes out and returns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 18:55:39 +03:00
Valiantsin DzerakhandClaude Opus 5.5 434533aa11 Merge pull request #1207: the memory probe's test textures carry their pool flags, so --probe-vkmem passes again (shenjackyuanjie)
Only the fix commit is taken; the PR branch carries 88 unrelated fork merges. On top: the slot redirect check follows
the format rule of 1c5c6fdb, which the pool-flags failure had hidden.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 11:12:47 +03:00
Valiantsin DzerakhandClaude Opus 5.5 810cf2c2bb The memory probe's slot redirect check follows a texture of the slot's own format
Since 1c5c6fdb a finalized slot follows only a live entry of its texel format at its storage; the check still put an
RGBA8 texture over a BC1 slot and expected the slot to follow it. The live texture is now BC1 of another size, and a
second check puts an RGBA8 texture at the same storage and expects the slot to stay.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 11:12:40 +03:00
Valiantsin Dzerakh fad20c0fcd Up version 0.4.1 2026-11-15 11:08:49 +03:00
shenjack 74bb01d2af Preserve pool flags in the Vulkan memory probe texture fixture 2026-11-15 11:07:25 +03:00
Valiantsin DzerakhandClaude Opus 5.5 c26c3a04bc Merge pull request #1203: on AMD's Windows drivers objects against clouds and fog lose the light blocky fringe round their silhouettes; a discard demotes the fragment as on the Switch (codexx-dev, #164)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 10:38:24 +03:00
Valiantsin DzerakhandClaude Opus 5.5 f5e560d176 Merge pull request #1200: with contact shadows on, rock faces turned away from the sun no longer go dark and smoky, and fewer dark strokes cross the grass (itaskaev2)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 10:08:44 +03:00
Valiantsin DzerakhandClaude Opus 5.5 6ff98e2d61 Contact shadows take the in-image neighbour on the screen border when they find the surface normal
On the border the clamped neighbour is the pixel itself, so the nearer-in-depth pick took a zero difference and
normalize() returned NaN; `lit <= 0.0` is false for NaN, so whether those pixels were marched and what they wrote
depended on how the driver clamps NaN. The border now uses the side inside the image, and a NaN gate skips the march.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 10:05:54 +03:00
Valiantsin DzerakhandClaude Opus 5.5 f76a24a7cf Merge pull request #1200 into task/pr1200: with contact shadows on, rock faces turned away from the sun no longer go dark and smoky in haze and fog (itaskaev2)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:34:39 +03:00
Valiantsin DzerakhandClaude Opus 5.5 33bfd29ebb Merge pull request #1197: a Calibrate gyro button on the Controls tab removes the aim drift of a controller with a gyro bias; each controller keeps its own, and a calibration where the pad moved is rejected (palxex)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:32:40 +03:00
Valiantsin DzerakhandClaude Opus 5.5 0efa904d90 A new controller no longer inherits the previous one's gyro bias, and a calibration where the pad moved is rejected
The bias reset in sensors_ready only ran when sensor_pad was nonzero, but scan_pads zeroes it on every disconnect,
so a pad connected after another one kept the old pad's bias, and an attempt still collecting finished on the new
pad under the old pad's name. The bias was also loaded only once, at the first StartSixAxisSensor.

- input.c pad_bias_select runs for every pad the sensor code sees: it drops the old bias and attempt, then loads the
  saved bias when gyro_bias_pad names this pad. scan_pads drops them on disconnect. The bias loads with
  gyro_calibration off too, so turning the switch on mid-run applies it.
- The pad identity is the name plus an FNV-1a hash of SDL's serial when the pad reports one, so two pads of one
  model no longer share a bias; the hash keeps the Bluetooth address out of totk.ini and the log.
- An attempt skips 0.5 s of samples (the button press shakes the pad), then averages 1 s. It is rejected when the
  largest per-axis standard deviation exceeds 0.04 rad/s; a steady bias of any size passes. The row then says the
  controller moved (new UI_MENU_GYRO_CALIBRATE_MOVED in all four catalogs) and the bias in effect stays.
- The timeout counts 5 s without a sample, not 5 s from the start.
- Calibrate gyro starts the sensor thread when the game has not yet (title screen) instead of doing nothing.
- Row states are an enum (MOTION_CALIB_*); motion_calibrate_start takes the clock, and only the gamepad's fusion
  instance writes totk.ini, so --probe-services now checks a measured bias, a rejected attempt and the timeout.
- The duplicate copy_name in motion_fusion.c is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:32:05 +03:00
Valiantsin Dzerakh 0a792f22c1 Merge pull request #1197 into task/pr1197: a Calibrate gyro row on the Controls tab removes the aim drift of a controller with a large gyro bias (palxex) 2026-11-15 09:21:40 +03:00
Valiantsin DzerakhandClaude Opus 5.5 7b5265d1fd Discard as demote runs only where shaderDemoteToHelperInvocation is enabled, and the log says whether it is on (#164)
vk_device.c create_device records the feature from the Vulkan 1.3 chain it passes to vkCreateDevice. spirv_demote
leaves the module as the translator wrote it without the feature, also under TOTK_DEMOTE=1, since the
DemoteToHelperInvocation capability needs it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:19:24 +03:00
Valiantsin DzerakhandClaude Opus 5.5 db26ab1abb Merge pull request #1194: a charged attack that hits no longer crashes above 60 fps when the weapon's hit record map is full (project-snail)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:12:55 +03:00
Valiantsin DzerakhandClaude Opus 5.5 fda4e62405 Merge pull request #1203 into task/pr1203: objects against clouds and fog lose the light blocky fringe on AMD's Windows drivers (codexx-dev)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:08:13 +03:00
Valiantsin DzerakhandClaude Opus 5.5 c8a6b91c61 The empty hit list sites' comment names the functions, the stale slot and the registers the skipped path reads
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:04:34 +03:00
Valiantsin DzerakhandClaude Opus 5.5 8fde60d854 Merge pull request #1194 into task/pr1194: a charged spear attack that hits no longer crashes above 60 fps (project-snail)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:04:24 +03:00
Valiantsin DzerakhandClaude Opus 5.5 b642c849ef Merge task/fpslim: the FP fast paths of the game's code are smaller with the same results, 8% fewer instructions in the hottest functions (#381)
The directed-RMode test folds into the result compare through a per-thread threshold, vector slow paths take
their operands through the Cpu instead of stack copies (___chkstk_ms leaves the large lifted functions), the
static FP helpers use preserve_most, and the slow paths rerun only the failing lanes. Bit-identical to the old
headers: 14.85M edge cases under every FPCR mode on v3 and v2, and 13.4G FP ops compared in a check-mode build in
the town and fight2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 08:54:37 +03:00
Valiantsin DzerakhandClaude Opus 5.5 2ab5329909 The NVN probe's Cpu gets the FP result check threshold too
Its static Cpu ran guest code with a zeroed threshold, which lets +-smallest normal pass the check unexamined.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 08:54:22 +03:00
codexx-dev fc87618233 Demote instead of OpKill on AMD's drivers so a discard keeps the quad's derivatives defined (#164) 2026-11-14 21:25:22 -03:00
itaskaev2andClaude Opus 5.5 a3fee27cfe Leave surfaces turned away from the sun out of the contact shadows
The contact shadow march darkened the whole lit colour of every pixel whose ray toward the sun passed behind
geometry. On a face turned away from the sun the ray runs into that face at once, so every such face within 80 m
was darkened by the full strength, although it gets no direct sunlight to lose: what was darkened was its ambient
light and the fog the composite had already added. In hazy scenes (the Gerudo sandstorm) the shaded rock faces and
the undersides of overhangs showed as dark smoky silhouettes in the haze, and objects such as a wagon lost their
fogged look. The shader now takes the surface normal from the linear depth and scales the shadow by how far the
surface faces the sun: none for faces turned away, a growing share from a grazing angle on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 23:35:24 +01:00
Valiantsin DzerakhandClaude Opus 5.5 5c48f62fca The FP fast paths of the lifted code check FPCR.RMode inside the result compare and pass vector slow-path operands through the Cpu
The result check of every scalar and vector FP op compares |r| against a per-thread threshold in the Cpu (fp_th_s,
fp_th_d): the smallest normal in round-to-nearest, NaN under a directed RMode, so a directed mode sends every result
to the redo path and the separate load and test of c->fpcr per op goes. rt_fpcr_threshold sets it; rt_fpcr_sync
calls it on every host, and the Cpu inits and the fiber switch's fpcr copy call it too.

The vector slow path FN(vredo) takes its operands from c->fp_arg instead of three stack copies per call site, which
takes ___chkstk_ms out of the large lifted functions (275 call sites in main to 5). It reruns only the lanes that
failed the check. A NaN result in round-to-nearest goes from redo straight to fix with the FZ-flushed operands. The
static FP helpers use preserve_most, so their cold call sites do not spill the caller-saved registers.

Results are bit-identical to the old headers (equivalence harness of build/v143-genko/static2, 200M cases).
On the 42 hottest 1.4.3 functions: instructions -8.2%, bytes -8.8%, hot-path instructions -4.3%.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 00:31:00 +03:00
Valiantsin DzerakhandClaude Opus 5.5 94eb3d4628 Merge pull request #322: Steady detail under load, a switch that stops the game lowering model and texture detail when the frame rate drops (ProSep)
The PR's level-of-detail distance part is left out: it made no visible difference in play, and the emulator mod it came from was dropped there as a placebo.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 23:07:37 +03:00
Valiantsin DzerakhandClaude Opus 5.5 5f1d74023b Remove the lod_range setting and the lodrange patch set; lod_steady stays
Playtests showed no visible difference from lod_range, and the emulator mod scene dropped the same change as a placebo.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 23:07:00 +03:00
Valiantsin DzerakhandClaude Opus 5.5 9d05ccd920 Merge task/padaudio: the DualSense touchpad click is a bindable button, and the audio output device can be chosen in the menu and switches while the game runs (#482, #1014)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 22:54:36 +03:00
Valiantsin DzerakhandClaude Opus 5.5 6347b8262a The audio output device can be chosen on the Sound tab and in totk.ini (audio_device), and a change moves the sound to it while the game runs (#1014)
TOTK_AUDIO_DEVICE names the device as SDL lists it; unset, empty or default keeps the system default and the same open
as before. audio_mixer.c output_open opens the named device, or the default with one log line when it is not
connected. Once a device is chosen, an output worker thread (output_worker) owns the SDL stream: on a cfg change and
once a second it resolves the name again and output_switch opens the new device with the same spec, destroys the old
stream (which waits for its callback), restarts the prefill and resumes. The render thread only wakes or starts the
worker (output_watch), so the game's audio never waits for a device open. A chosen device that disappears falls back
to the system default and comes back within a second of being plugged in again. TOTK_AUDIO_CHANNELS=auto reads the
channels of the device chosen at start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 22:28:42 +03:00
Valiantsin DzerakhandClaude Opus 5.5 0ce53aa7c7 The DualSense and DualShock 4 touchpad click is a bindable gamepad input named touchpad, shown and captured on the Controls tab (#482)
PAD_TOUCHPAD follows the paddles in input_map.h and reads SDL_GAMEPAD_BUTTON_TOUCHPAD in input.c pad_physical, so
input_capture sees the click when a Controls cell waits for a press (#852 had no pad id to report it). It is unbound by
default; pads without a touchpad never report it held.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 22:19:51 +03:00
Valiantsin DzerakhandClaude Opus 5.5 cf3acd0a2f Model detail range replaces the distant detail switch: lod_range scales the depth at which models leave their detailed level (1x, 1.5x, 2x, 3x or max) (#949)
The pick patched before (main+0x1ee8424) is reached through its vtable only when the method is overridden; the culling
passes run about 20 inlined copies of it, so the switch changed almost nothing. The lodrange set patches the two places
per version that compute the level depth B at model setup (the setter and its inlined copy); the slope of the coarser
levels follows from B there, and max takes B = 1e30. The menu row sits under Grass range, with Steady detail under load
after it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 21:58:48 +03:00
palxex dc13b194ce Add gyro bias calibration for the six-axis sensor
The yaw axis has no gravity reference, so it drifts with the gyro's
manufacturing bias. Two controllers of the same model can drift in
opposite directions, and the sensor fusion had no way to estimate or
remove the offset: aiming with motion turned slowly on its own and
needed constant correction.

A Calibrate gyro row on the Controls tab collects 250 gyro samples
(about a second) while the controller is held still, averages them per
axis into a bias, subtracts that bias from every sample before the
fusion integrates it, and restarts the angle so the estimate starts
clean. Collection has no motion threshold: a controller whose bias
exceeds any absolute threshold would otherwise never finish, which is
how the first version of this stalled with no way out. Five seconds
without the expected samples ends the attempt, from the sample path and
from the menu alike, so a sensor thread that stops feeding it is
reported instead of leaving the row on "calibrating". The first raw
sample and the final bias are logged, so a report carries the numbers
that were measured.

The row's value says where the controller stands: calibrating,
"<controller>, calibrated" once it is done or when the bias was loaded
from totk.ini at start, and "calibration failed" when an attempt timed
out with no bias in effect. A controller change cancels a calibration
that is in progress and clears the bias and the label, so neither is
carried over to the new controller.

The behaviour sits behind gyro_calibration (the Controls tab's "Gyro
bias calibration", default on, live): off, the fusion takes the raw
gyro, an attempt in progress is dropped, and nothing is loaded at
start, while the saved keys stay in totk.ini so turning it back on
restores the last calibration.

The bias and the controller's name go to totk.ini when calibration
completes, outside the fusion's lock, and are loaded at the next start
only when the connected controller has the same name.

Tested with a Switch Pro Controller on the 1.4.3 build: the log shows a
z-axis bias of 0.139 rad/s (8 degrees per second), calibration finishes
in about a second, the yaw drift is gone, and the row reports each state.
2026-11-15 02:26:30 +08:00
Valiantsin DzerakhandClaude Opus 5.5 517a161457 Merge pull request #322: distant model detail settings, brought up to date with dev (ProSep)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 21:16:19 +03:00
Valiantsin DzerakhandClaude Opus 5.5 3858ad4b51 Merge task/pack: lifted functions skip the function-entry check except where the runtime watches them, and host calls are no longer counted outside probes and traces (#381)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 20:03:37 +03:00
Valiantsin DzerakhandClaude Opus 5.5 f1a771e493 Merge task/bindinc: a draw rebuilds only the bindings, shaders and pipeline state whose inputs changed since the draw before, about 3% less render-thread time on the CPU (#381)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 20:03:37 +03:00
Valiantsin DzerakhandClaude Opus 5.5 2f32f21f5c The incremental draw preparation is on by default, and bind_inc and memx are listed in totk.ini.example (#381)
TOTK_BIND_INC had no settings row, so unset meant off. It now has one with default 1, like TOTK_MEMX: 0 or off turns
it off, check keeps the comparison mode. The check mode found 0 differences on 13 scenes (town, fight2, Kakariko,
Great Sky Island, Depths lit and dark, a shrine, rain, a cave, evening, a field, the menus, entering a building).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 20:03:24 +03:00
Valiantsin DzerakhandClaude Opus 5.5 99878f3c55 Log the first guest write of a directed FPCR rounding mode, with its caller
Diagnostic only. The lifted MSR FPCR (recomp/lifter.py) now also tests the stored RMode bits (cpu.h RT_FPCR_NOTE); the
first nonzero one is logged once by main.c rt_fpcr_rmode with the caller of the writing function. The write itself
behaves as before. Whether the line ever shows decides if the directed-rounding slow paths of runtime/simd.h are dead
in the game. 10 MSR FPCR sites on 1.4.3; nothing on the other paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 19:05:19 +03:00
Valiantsin DzerakhandClaude Opus 5.5 2d5879f9e2 Host-function calls are counted only by the probes and under TOTK_TRACE=host
hostfn_entry bumped a per-thread call counter on every host-function call (a TLS load, two branches, a load and a
store) for readers that only the probes and the boot checkpoint line have. The count now sits beside the first-call
trace in a cold hostfn_watch, behind the same single test of rt_trace: RT_TRACE_HOSTCOUNT, switched by hostfn_count,
which --probe-kernel (test_lifted_ticks reads the count) and --probe-ipc turn on. The boot checkpoint line leaves the
host-call count out when nothing counted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 19:05:18 +03:00
Valiantsin DzerakhandClaude Opus 5.5 6a23628771 Lifted functions check the function-entry hook only where the runtime watches them, on 1.4.3 only the render size setup
FN_ENTER (runtime/cpu.h) opened all 282,702 lifted functions of 1.4.3 with a load of g_fnprof and a branch, for one
reader: runtime/resolution.c res_fn_hit, which waits for main+0x585bd0 (SET_RENDER_SIZE). A `hook` line of the patch
table (recomp/patches.py) now names the functions that keep it; patches-v143.txt hooks main+0x585bd0, and gen.py stops
on a hook that is no function start. Tables without hook lines (1.2.1, 1.1.0) keep FN_ENTER in every function, and
gen.py --fn-enter-all puts it everywhere for a whole-game function-entry profile. The hooked addresses of a chunk are
in its lift cache key (tests/lift_cache_test.py test_hooks_are_in_the_key).

On 1.4.3 the generated C is otherwise byte-identical to dev's (282,701 FN_ENTER lines fewer, nothing else differs).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 19:05:08 +03:00
Valiantsin DzerakhandClaude Opus 5.5 d52c16e651 Merge dev into task/bindinc: the incremental binding keeps its logic and takes dev's inline compares and copies (TOTK_MEMX)
The ten conflicts in runtime/vk_exec_draw.c were dev's memcmp/memcpy -> MX_EQ/MX_NE/MX_CPY swap on lines task/bindinc
had rewritten. Each keeps the task/bindinc code with the swap applied to its hot-path compares and copies (push and set
signature compares, the shader memo's pipe compare, the pipeline memo and recorded-state compares, the vertex
attribute and stream lists). The check-mode compares that call bi_mismatch keep memcmp as the reference.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 18:47:31 +03:00
project-snail f3efc5806c Send an empty charged-attack hit list to the game's own count == 0 check
The weapon's hit-record pool holds 10 records, and one charged swing above 60 fps fills it: the
insert fails, and the list of hits made this frame stays empty. main+0x2906de0 then reads the empty
list's slot, which holds a stale stack value, and passes it to the hit handler before the game's own
count == 0 check would return; the stale value here pointed at the temp hit map and the read at
main+0x870158 faulted reading 0x100000000a.

Both count == 0 branches now go to main+0x2906df0, the game's check, under the realtime set. A
non-empty list is unchanged, and below 60 fps the pool never fills.

Verified on 1.4.3: a charged spear attack on an enemy at 120 and 144 fps no longer faults, and the
damage at 60 and 120 fps matches the build without this patch.
2026-11-14 21:00:38 +08:00
Valiantsin DzerakhandClaude Opus 5.5 97d12240f4 Merge task/cheatsrebuild: on the Linux AppImage "Rebuild the game now" starts the setup AppImage itself, so code cheats get built in, and a rebuild that does not start is tried again (#560, #403)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 15:13:04 +03:00
Valiantsin DzerakhandClaude Opus 5.5 35c590bacd Start the Linux rebuild through the AppImage file, not its vanished mount
The setup wrote the path of its Python inside the AppImage mount into
build-cache/rebuild.txt. The mount is gone once the setup exits, so the
game could not start the rebuild for code cheats and ExeFS patches, kept
its old code, and rebuild.skip then stopped every later attempt. The
command now starts $APPIMAGE --rebuild <install folder>, and a rebuild
that does not start leaves no rebuild.skip (#560, #403)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 15:12:35 +03:00
Valiantsin DzerakhandClaude Opus 5.5 04ccb9cd36 Merge task/pr996: pull request #996, the lift redoes only the chunks whose inputs changed, so a cheat or mod change lifts in seconds instead of a minute or more (Wesley-CR)
On 1.4.3 the cached lift and a --no-cache lift give byte-identical C, tables and folded.txt; a run with unchanged
inputs reuses all 418 chunks in 2.8 s (92 s cold), and one patch site removed lifts one chunk in 3.7 s with the same
output as a full lift. tests/lift_cache_test.py passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 14:16:30 +03:00
Valiantsin DzerakhandClaude Opus 5.5 3fc643cb65 Merge pull request #996 into task/pr996: lift again only the chunks whose inputs changed
The conflict in recomp/gen.py was the Usage line and the options next to --v-locals and --v-trust; both are kept, and
v_locals and v_trust are in the Emitter options the cache key holds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 14:16:00 +03:00
Valiantsin DzerakhandClaude Opus 5.5 b2355be341 Merge task/memx: the replay compares and copies its per-draw state inline instead of calling the C library, about 5% less render-thread time on the CPU (#381)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 11:35:49 +03:00
Valiantsin DzerakhandClaude Opus 5.5 354887a2a3 The replay's per-draw compares and copies run inline instead of calling ucrtbase memcmp and memcpy (TOTK_MEMX)
runtime/memx.h: MX_EQ and MX_CPY compare and copy with 32-byte loads in the caller; a constant size unrolls, a variable
one loops without a call. Used for the pipeline memo state and the recorded dynamic state (prepare_draw), the shader
answers (shader_for, which also compares the stored hash first), descriptor signatures and push sets (bind_resources),
c0 and support buffers, the uniform update copies, the vertex attribute and stream binds, pipe_find, the dynamic state
delta, nvn_hm_get and the recorder's tail_copy. TOTK_MEMX=1 (default) takes the inline forms, 0 keeps memcmp/memcpy,
check runs memcmp and logs any compare the inline form answers differently.

Machine A, 4 cores, 1080p, bindinc on both sides: Lookout Landing replay 9.64 -> 9.16 ms over 4+4 runs (each ABBA block
-0.40 and -0.55 ms), Kakariko 9.04 -> 8.38 ms over 2+2. Check mode on dev: Lookout Landing pan, 0 mismatches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 11:35:29 +03:00
Valiantsin DzerakhandClaude Opus 5.5 1ada4a7586 Wake the recorder through the fenced check on every publish (#1174)
pub_now stored head and read Q.sleeping with no fence between them, while rec_main sets Q.sleeping and then reads head.
x86 may read Q.sleeping before the head store is visible, so both sides could miss each other and the recorder slept
out its 2 ms timeout with packets waiting. Since rec_submit returns without a drain (TOTK_REC_ASYNC=1), its publish is
the last of a submission, and the present, uploads and readbacks waited for that timeout in rec_queue_wait: a #1174 log
on an RX 9070 XT shows 1.6 to 3.4 ms a wait late in the session against 0.04 ms at its start. pub_now now wakes through
wake(), which has the fence. Cost: one locked instruction per publish, about 2300 a frame in that scene.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 11:15:01 +03:00
Valiantsin DzerakhandClaude Opus 5.5 42b36b0e0b Merge task/prfix: pull requests #1184 (with #1161), #1162, #1163, #1164 and #1165; with DLSS/DLAA the picture no longer flickers, the mist no longer flashes, the inventory icon is no longer stretched over the menu and the quick weapon menu no longer closes on a dark frame; the chunked replay no longer leaves a black strip by the quick menu; shadow is a known totk.ini key (ipavlin98, itaskaev2)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 08:42:55 +03:00
Valiantsin DzerakhandClaude Opus 5.5 e654aa288f Merge pull request #1165 into task/prfix: SHADOW is a known totk.ini key (itaskaev2)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 08:34:52 +03:00
Valiantsin DzerakhandClaude Opus 5.5 4e3464767e Merge pull request #1164 into task/prfix: A chunk of the chunked replay records a new pool-bound image's first transition and clear in its own command buffer, which removes the black strip by the quick menu (ipavlin98)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 08:34:52 +03:00
Valiantsin DzerakhandClaude Opus 5.5 06088863bc Merge pull request #1163 into task/prfix: DLSS arms only on a light target more than half the scene size, so the frame no longer goes dark when the quick weapon menu closes (ipavlin98)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 08:34:52 +03:00
Valiantsin DzerakhandClaude Opus 5.5 b8800bac86 Merge pull request #1162 into task/prfix: DLSS arms only on a light target of the scene's shape, so the inventory icon no longer stretches over the menu (ipavlin98)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 08:34:51 +03:00
Valiantsin DzerakhandClaude Opus 5.5 d77a637053 Merge pull request #1184 into task/prfix: DLAA ends the scene only at a bloom pass of the scene's shape without depth, so distant mist no longer flashes (ipavlin98)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 08:34:48 +03:00
Valiantsin DzerakhandClaude Opus 5.5 e7dbe74f44 Graphics descriptor sets are no longer shared within a submission, and the per-draw keep tests are made once per pipeline (TOTK_BIND_INC)
The set table probe cost about 54 ns a draw on Lookout Landing (a cache miss on the 4096-entry table plus two hashes of
every set) and shared 6.7% of the allocated sets. Batched allocation stays. bi_plan's walk over every binding is
replaced by per-binding tests (bi_tests, kept in the pipeline's RsList) that bind_resources evaluates in its own loop,
with the volatile flag kept per binding. A kept binding after a build rereads only the generation group it depends on.
Check mode: 0 mismatches in town, Kakariko, fight2, island and Depths, 60 s each.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 07:58:16 +03:00
ipavlin98 c18f3d6c5c End the DLAA scene only at a bloom pass of the scene's shape without depth, so the sky lookups and the quarter-size particles that follow transparents drawn early no longer end it before the mist, which DLSS then missed on runs of frames and the distant mist flashed between them 2026-11-14 05:37:10 +03:00
ipavlin98 a02413d2f6 Keep DLSS on frames where the game draws a half-size mip chain of the scene before the light target: the bloom chain's start now needs level 0, as the chain's level 1 ended the scene before anything was armed, DLAA skipped those frames and the picture flickered between anti-aliased and plain ones 2026-11-14 05:37:10 +03:00
Valiantsin DzerakhandClaude Opus 5.5 ecd0f004cc A kept descriptor is built again when a build earlier in the same draw moved the generations it depends on (TOTK_BIND_INC)
Check mode on the island save caught one kept texture descriptor whose view differed from the full build: a lookup
of an earlier binding of the same draw made a texture entry (vk_texture_slots_gen moved during the build), and the
full build resolved the later slot under the new generation. bi_plan now records per kept binding whether it depends
on G_tex or G_buf; after a binding is built, the loop reads both again and builds every later binding that depends
on one that moved. Island and Lookout Landing check, 60 s each: 0 mismatches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 00:18:52 +03:00
Valiantsin DzerakhandClaude Opus 5.5 dba054110b Graphics descriptor sets are shared within a submission and allocated in batches (TOTK_BIND_INC)
Adapted from Savage Wind Recomp's set_finish and vk_alloc_set (MIT, row in THIRD_PARTY_NOTICES.md). A graphics set
that would be allocated and written is first looked up in a per-context table keyed by two independent 64-bit hashes
of its layout and packed descriptors and stamped with the submission's fence: an equal set of the same submission is
bound instead. New sets come from the submission's last pool in batches of one layout that grow 1, 2, 4, 8, 16 while
the layout repeats, one vkAllocateDescriptorSets per batch instead of per set. check stores each set's contents,
compares every hit with them and allocates as before. Lookout Landing: about 2,000 allocated graphics sets a frame,
6.7% shared; check 0 mismatches; on ran 60 s without errors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 23:52:37 +03:00
Valiantsin DzerakhandClaude Opus 5.5 c26ed9ba7b A graphics draw with the previous draw's pipeline rebuilds only the descriptors whose inputs changed (TOTK_BIND_INC)
With TOTK_BIND_INC the graphics build writes into VkExecDraw.BI, which keeps the packed descriptors of the last build
and, per descriptor, what it counted for the traffic census and whether it copied guest bytes. bi_plan names the
bindings to build again: support always; c0 when a storage slot of the stage or the base instance changed; the
compiler constants when the program, its serial or G_buf changed; uniform and storage slots when marked or when G_buf
(pools and mirrors, snapshots, overlays, the submission) moved; textures and images when a handle was marked or G_tex
(slot registrations, samplers, the storage epoch, the feedback layout, DLSS's swap) moved; storage addresses held in
a uniform block, and any descriptor whose build copied guest bytes, every time. A pipeline with a written storage
binding is built in full. Kept descriptors count their census again; a set none of whose descriptors changed and that
is still pushed or bound records nothing, without the psig or sig compare. check builds everything as before and
compares each kept descriptor and its census with the build. Lookout Landing check, 60 s: about 17,000 kept bindings
compared a frame, 0 mismatches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 23:49:21 +03:00
Valiantsin DzerakhandClaude Opus 5.5 c86feefadc A draw skips the vertex stream loop while none of the streams it reads changed (TOTK_BIND_INC)
bind_vertex_buffers resolves and compares the 17 streams the vertex input reads on every draw. When the last
prepared draw bound the same set of streams in this command buffer, no op changed one of their ranges, vk_mem_gen
stayed, and with static vertex input the strides did (pipe_gen), every stream would resolve to what is bound: the
loop is skipped. check runs the loop and counts a stream it would bind again. Lookout Landing check, 60 s: 62% of
draws skip it, 0 mismatches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 23:47:24 +03:00
Valiantsin DzerakhandClaude Opus 5.5 772d9dea66 The feedback scan keeps its answer across texture binds that leave the fragment shader's slots alone (TOTK_BIND_INC)
feedback_scan's memo is keyed by tex_serial, which every texture or image bind of either stage moves. When the memo
holds the last prepared draw's scan (BiLast.fb_ok) of the same fragment shader in the same pass, no slot was
registered since, and no marked fragment texture slot is one the shader reads (BiMask.ftex, buffer textures
included), the answer stands. check scans and compares. Lookout Landing check, 60 s: kept on 68% of draws (the memo
alone 60%), 0 mismatches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 23:45:44 +03:00
Valiantsin DzerakhandClaude Opus 5.5 dcd5dc3cf4 A draw keeps the previous draw's shaders while no texture or image slot their translation reads changed (TOTK_BIND_INC)
The shader memo already keeps them while the program, the bound state and the first 32 handles of both stages are
equal. With the marks it also keeps them when other slots were rebound: no program or serial change, the same
pipe_gen and slot generation, and no marked texture or image slot inside the program's slot mask (slot_mask_of,
~0u while unknown), which are all answers_for reads. BiLast.sm_ok says the memo holds the last prepared draw's
shaders, so a mark cleared by a later draw never hides a change. check looks the shaders up and compares. Lookout
Landing check, 60 s: shaders kept on 65% of draws (the memo alone 55%), 0 mismatches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 23:44:01 +03:00
Valiantsin DzerakhandClaude Opus 5.5 ff3a95d0f1 A draw whose pipeline state no op changed takes the previous draw's pipeline and state without building and comparing them (TOTK_BIND_INC)
From task/fastrep 18eab597 (TOTK_PIPE_GEN), now under TOTK_BIND_INC: the pipeline memo keeps the pipe_gen its state
was built under; a draw with that generation and the same shaders, primitive, restart, feedback flags and depth mode
takes the memo's state and pipeline and skips the three 360-byte compares (memo, cache entry, recorded state). The
shader memo skips its compare of pipe the same way. check builds and compares as before and counts a state the
generation called unchanged that differs (bi_mismatch logs the first of each kind with its marks and generations).
The census runs only in check mode. Lookout Landing check, 60 s: 67% of draws keep the state, 0 mismatches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 23:42:11 +03:00
Valiantsin DzerakhandClaude Opus 5.5 2abfa23a67 Bind ops mark the slots whose bytes they changed and a per-draw census counts what the next draw could keep (TOTK_BIND_INC)
vk_exec_bind compares before it writes: VkExec.dirt gets a bit per program, uniform, storage, texture, image and
vertex buffer slot that changed, pipe_gen moves with every change of the pipeline state (also depth clamp, raster
discard and the target formats), fbl_serial with the feedback-layout images, OV.gen with the uniform overlays.
vk_exec_draw_new_cb, copy_state and state_reset mark everything. With TOTK_BIND_INC=on or check, bi_census sorts each
prepared draw by what changed since the draw before (nothing, uniform slots only, other slots, another pipeline) and
counts the descriptors to rebuild, the generation moves and the shader, pipeline, vertex buffer and feedback keeps;
a bind_inc line every 300 frames. No draw is prepared differently yet.

Lookout Landing, 4500 draws a frame: nothing changed 8.7%, uniforms only 55.5%, other slots 22.9%, another pipeline
12.8%; 3.4 of 11.2 descriptors a draw to rebuild; uniform overlays move on 2.3% of draws, snapshots 0.1%.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 23:40:01 +03:00
Valiantsin DzerakhandClaude Opus 5.5 673229727d Merge task/vloc: lifted functions keep their 8 most used V registers in host registers, 4-6% less guest time in a town
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 20:42:31 +03:00
Valiantsin Dzerakh 1a1f21ebe0 Merge branch 'dev' into task/vloc 2026-11-13 20:11:40 +03:00
Valiantsin DzerakhandClaude Opus 5.5 44bc5887f0 gen.py keeps 8 V registers per function in locals by default
K = 8 cut guest time 3.9-6.2% in the town on a 7900X against K = 0. func.py tests the same default; --v-locals 0
gives the previous output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 20:11:39 +03:00
Valiantsin DzerakhandClaude Opus 5.5 eb8c7ea888 gen.py --v-locals K keeps the K most used V registers of each function in C locals
recomp/vlocals.py rewrites each emitted function: c->v forms become the RT_VLOCALS accessors of runtime/simd.h, the
K registers the function names most get bits in VMASK, they are loaded at entry, stored where the dirty mask may hold
them before calls and exits, and reloaded after calls; --v-trust skips the reload of V8-V15. A function with a form the
rewrite does not know keeps every register in the Cpu. K = 0, the default, emits the same C as before. The manifest
counts the rewritten functions and syncs. tests/diff/func.py takes --v-locals, --v-trust and --work (one work
directory per concurrent run).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 19:40:05 +03:00
Valiantsin DzerakhandClaude Opus 5.5 520e80eb18 simd.h reaches V registers through macros that a chunk built with RT_VLOCALS maps to function locals
Work in progress: the generator side (gen.py --v-locals) is not written yet, so no chunk defines RT_VLOCALS and the
default build is unchanged. build_game.py gains --gen-o2 for A/B builds at the PGO optimisation level without a profile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 19:05:12 +03:00
Valiantsin DzerakhandClaude Opus 5.5 a5b3fcdc92 Merge task/patchside: ov_ability.c is compiled per install, so a release starts on 1.4.3 again, and the library build stops on any source with a per-version header (#1005)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 18:07:47 +03:00
Valiantsin DzerakhandClaude Opus 5.5 bdba6ba83f Compile ov_ability.c per install, so 1.4.3 starts from a release again
The ability target guard registers its host function on the game version's patch page (patches.h), but it was left
out of INSTALL_SIDE and went into libtotkrt.a, which make_release builds once from 1.2.1's generated headers. On a
1.4.3 install that address lies inside sdk and dispatch stops at start ("host function at 0x700472bf60 lies inside
module sdk"). The library build now also stops when a library source includes a per-version header. (#1005)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 18:06:37 +03:00
ipavlin98 1a3b2936d6 Arm DLSS only on a light target more than half the scene's size, so the half-size effects target that armed on frames around the quick weapon menu, where the half-size G-buffer came first, is no longer scaled to the output in place of the scene 2026-11-13 08:46:42 +03:00
ipavlin98 6ef1eafca7 Arm DLSS only on a light target of the scene's shape, so the square targets the inventory renders item icons into are no longer scaled to the output and drawn over the menu 2026-11-13 08:46:41 +03:00
Valiantsin DzerakhandClaude Opus 5.5 00baa18d14 Merge task/pr1146: pull request #1146, separate Music, SFX and Voice volume settings in the Sound menu; sounds are classified by their RomFS names, BARS entries and ADPCM coefficient tables, only while one of the three is below 100 (#722, RxEmam)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 08:38:24 +03:00
ipavlin98 cefc618f70 Record a new pool-bound image's first transition and depth clear inside the chunk that makes it, at its op, so the clear no longer runs after the chunks already on the GPU and blanks a strip of what they rendered into the shared storage, seen for a frame right of the quick weapon menu while its icons are drawn 2026-11-13 08:38:20 +03:00
Valiantsin DzerakhandClaude Opus 5.5 f83fdbbd0b The music, effects and voice volumes classify nothing at their defaults, look a voice up once per wave buffer, and count unmatched sounds as effects (#722)
At 100/100/100 the RomFS, zstd and Opus hooks and the mixer lookup are skipped (audio_cat_active); the
RomFS handle still keeps its category, so a stream opened earlier is classified by its later reads once a
setting moves. The mixer keeps each voice's category until it moves to another wave buffer or coefficient
table, instead of up to six scans of a 32768-entry ring per voice per frame; the ring is 4096 entries and
lookups take its lock. zstd decodes check the BARS magic before any lookup. The guesses from BWAV headers
(Opus means voice, stereo ADPCM means music) are gone: Opus streams are the DmT cutscene beds and stereo
ADPCM is common among effects. Unknown Opus output counts as effects. A once-a-minute log line counts the
categories while a setting is below 100.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 08:38:08 +03:00
Valiantsin DzerakhandClaude Opus 5.5 25f42aa84e Merge pull request #1146 into task/pr1146: separate Music, SFX and Voice volume sliders
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 08:26:13 +03:00
itaskaev2andClaude Opus 5.5 5c57d94098 List shadow among the known settings and document it as a totk.ini key
The runtime reads TOTK_SHADOW through cfg_get, so `shadow = 1` in totk.ini already turns shadow storage on, but the
key was missing from cfg.c known[]: every such totk.ini logged "unknown key 'SHADOW'", which reads as if the line did
nothing. The docs only named the environment variable.

known[] now has SHADOW, totk.ini.example describes `shadow` next to sync_ahead (which needs it), and the sync_ahead
text, its log line, docs/building.md and docs/architecture.md name the totk.ini key next to TOTK_SHADOW. Behaviour is
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 23:41:44 +01:00
Valiantsin DzerakhandClaude Opus 5.5 49e990fedf Merge task/pr552: pull request #552, the HDR tonemap rebuilds bright colours from the game's SDR picture with RenoDX's MacLeod-Boynton hue and purity transfer, Neutwo peak mapping and gamma 2.2 correction, replacing #442's highlight blend; hdr_hue now scales it (Zenrap0518)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 23:21:59 +03:00
Valiantsin DzerakhandClaude Opus 5.5 97dd94eb99 The HDR hue setting drives the RenoDX colour reconstruction, its matrix chains fold at compile time, and the notices name the adapted code
hdr_hue (TOTK_HDR_HUE) scales the MacLeod-Boynton hue and purity transfer that replaced #442's blend: the default
moves from 80 to 70, where the transfer is RenoDX's default 70% hue shift and 65% SDR blowout, and 0 turns it off
as the menu text says. The shader's two-step colour space conversions become one constant matrix each and the D65
white point a constant, which halves its matrix-vector products. THIRD_PARTY_NOTICES.md and vk_hdr.c now say the
reconstruction is adapted from RenoDX at 43c067b, MIT.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 23:21:04 +03:00
Valiantsin DzerakhandClaude Opus 5.5 9902e01aae Merge pull request #552 into task/pr552: RenoDX's colour reconstruction for the HDR tonemap
The shader conflict with #442's highlight hue correction is resolved in favour of #552's shader, which replaces that
blend; its SPIR-V header is regenerated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 23:20:36 +03:00
Valiantsin DzerakhandClaude Opus 5.5 c89b0f3ee3 Merge task/abilguard2: the Recall / Ultrahand target check also drops a target whose link record points into unreadable memory (#1005, #1137, #1145)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:31:46 +03:00
Valiantsin DzerakhandClaude Opus 5.5 47e6d908f7 The ability target check also drops a target whose link record the game would follow into unreadable memory (#1005, #1137, #1145)
main+0x155ec (1.2.1: main+0x809ecc) stores the actor's link record [actor+0x1a8] in the owner and updates its +0x50
atomically unless the actor's state word is 7..11. A freed actor can carry a stale pointer there that the old check
let through.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:31:40 +03:00
Valiantsin DzerakhandClaude Opus 5.5 e6839387fc Merge task/abilityguard: a stale Recall / Ultrahand target is dropped only where the game would fault on it, replacing the 4 GiB target check, and the model scene report loop gets a bound (#1005, #1137, #1145)
The ability guard (runtime/ov_ability.c) checks the holder, the actor, its component and the reference word the game
writes, so it catches every bad target seen in the reports: -1, 0x80, an empty bounding box (FLT_MAX) and a vtable in
main's data. The optref site at main+0x1bd01c8 (1.2.1: 0x1622bf4) is dropped: set ability patches the same address and
must win there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 19:57:07 +03:00
Valiantsin DzerakhandClaude Opus 5.5 ad54ab4910 Merge task/promptgen: the setup writes the shaped RomFS folders only for the player's screen shapes and prompt style, one folder per shape instead of per size, and the settings menu can prepare a missing one
Before, the setup wrote every prompt style for all 15 sizes: 64 folders, about 6.5 min and 370 MB of Common layout
archives. Now the four 16:9 folders stay, PcPrompts writes only the 16:9 keyboard prompts from the bind_ lines, and
the new Shapes step writes the layout and the selected style's prompts for the shapes of the connected displays and
the configured output_res. The game maps a resolution to its shape's folder, an exact-size folder from an older
install still wins, and the game-resolution row offers to start the generator for a missing shape.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 19:03:26 +03:00
Valiantsin DzerakhandClaude Opus 5.5 fa524128ce The settings menu starts each shape's generator once per game start and draws the keyboard prompts from the bindings
romfs.c romfs_shape_prepare remembers the folders it started a generator for, so a second click, or the same game
resolution chosen again while one runs, starts no second generator writing into the same folder. The shaped keyboard
and mouse prompts get totk.ini as --binds, as the setup's PcPrompts and Shapes steps draw them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 19:02:31 +03:00
Valiantsin DzerakhandClaude Opus 5.5 bc578148ab Write RomFS layout folders only for the screen shapes and prompts this player uses
The setup wrote 64 patch folders: every prompts style for all 15 shaped game resolutions. The new shapes step
(installer/screens.py) writes romfs-patches-<w>x<h> only for the shapes of the connected displays, the output_res the
game defaults to on each and the totk.ini output_res, and the prompts' shaped folder only for the style totk.ini
selects. One folder per shape: the runtime maps the game resolution to its shape's size (cfg.c cfg_shape_size, the
closest ratio within 1%), and an exact-size folder from an older install still wins. The settings menu marks a game
resolution whose shape has no folder and can start the generator for it in the background, for the next start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 19:02:31 +03:00
Valiantsin DzerakhandClaude Opus 5.5 df3e90dc4c Merge task/pr1136: opt-in gameplay tweaks for keyboard and mouse, the wheel turns and moves an Ultrahand object, switches weapons, steps the D-pad strips and zooms the map smoothly (RxEmam)
Pull request #1136 with three fixes: the Russian and Portuguese rows name Ultrahand as the catalogs do, smooth map
zoom takes the wheel only on the map menu_cursor.c finds, and the weapon bar opens only where the camera hooks saw
free roam, a lock-on or a mount. Off by default (gameplay_tweaks_enabled = 0).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 18:29:37 +03:00
Valiantsin DzerakhandClaude Opus 5.5 887ce32343 Open the wheel's weapon bar only where the camera hooks saw free roam, a lock-on or a mount
An unclassified camera is also a paused menu, a 1.2.1 menu or any screen menu_cursor.c does not read; opening the bar
there held D-pad right and moved the right stick inside that menu.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 18:29:24 +03:00
Valiantsin DzerakhandClaude Opus 5.5 a9de583d79 Merge pull request #1002: game 1.1.0 gets the core frame and camera patch sets of 1.2.1 and 1.4.3, so the frame rate, cursor, field of view and aspect ratio options work on it (shenjackyuanjie)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 18:29:21 +03:00
Valiantsin DzerakhandClaude Opus 5.5 2e494481d3 Smooth map zoom takes the wheel only on the map menu_cursor.c finds
With menu_cursor = 0 map_zoom_allowed answered yes everywhere outside aiming, scope, Ultrahand and lock-on, so the
wheel in free roam pushed the right stick's Y and tilted the camera, and the weapon wheel never got the wheel.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 18:25:40 +03:00
0xEmz 1f224dc0f9 Add separate Music, SFX, and Voice volume sliders (#722)
Add volume_music, volume_sfx, and volume_voice sliders under the Sound tab
in the F1 settings menu and in totk.ini to control background music, sound
effects, and character voices independently.

Classifies audio streams by file path in RomFS, parses BARS and BWAV headers
for ADPCM coefficient tables and stream formats, tags decompressed Opus streams
from nn::codec, and applies per-category gain factors in the audio mixer
during voice rendering.

Localized across English, Russian, Brazilian Portuguese, and Simplified Chinese.
Documented in totk.ini.example.
2026-11-12 17:25:33 +02:00
Valiantsin DzerakhandClaude Opus 5.5 8ef46a245a Name Ultrahand as the game does in the Russian and Portuguese gameplay tweak texts
The Russian rows called it Автоколесо, the catalog and the game say Ультрарука; the Portuguese rows kept the English
name where the catalog says Ultramão.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 18:25:14 +03:00
Valiantsin DzerakhandClaude Opus 5.5 d14e2a8f62 Merge pull request #1136 into task/pr1136
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 18:21:53 +03:00
Valiantsin DzerakhandClaude Opus 5.5 24be739310 Merge pull request #1142: the setup names an antivirus or a running game when it cannot write totk.exe, instead of the bare compile failure (itaskaev2) (#1122)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 18:19:47 +03:00
itaskaev2andClaude Opus 5.5 f854d67e6f Name the other program that holds totk.exe when the setup cannot link it
When ld.lld cannot write the totk.exe it links in the setup's temp build folder (Permission denied), the setup
showed only "Compiling totk.exe failed". In #1122 the holder was an antivirus that flagged the new exe, not a
running game. The compile step now recognises that failure in the build log and says what to do: close the game,
exclude the setup's and the install folder from the antivirus, restart Windows and run the setup again. Texts in
en, ru, pt-BR and zh-Hans; build_game.py's own line names the antivirus too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 11:17:51 +01:00
Valiantsin DzerakhandClaude Opus 5.5 b6c5d37fb7 Merge pull request #1115: on Linux the menu asks fontconfig for its CJK font before the five fixed paths (jcleng)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 07:57:00 +03:00
Valiantsin DzerakhandClaude Opus 5.5 608f915156 Merge pull request #1118: the game window's own pointer image returns as pointer_image, off by default (itaskaev2)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 07:57:00 +03:00
Valiantsin DzerakhandClaude Opus 5.5 357d148066 Merge pull request #1128: the chunked replay no longer freezes or crashes in chunk_record with replay_workers above 0, the target history's oldest-way search stays in its set (ipavlin98) (#1104)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 07:57:00 +03:00
Valiantsin DzerakhandClaude Opus 5.5 7692aabdc5 Merge task/vkey: a freed holder behind the optional reference at object +0x6b0 no longer crashes the game in main+0x1bd0138 / 0x1622bec, its target counts as absent (#1005)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 07:47:39 +03:00
Valiantsin DzerakhandClaude Opus 5.5 b80a568df5 Treat a target below 4 GiB behind the optional reference at object +0x6b0 as absent, so the freed-holder crash in main+0x1bd0138 stops on 1.4.3 and 1.2.1 (#1005)
The holder of the PlayerVirtualKey request can be freed while the request still points at it; its first word then reads
as a small number and the game dereferences it as the target actor. No guest object lives below 2^32, so such a target
now takes the game's own branch for a null target. Same TOTK_OPTREF_GUARD switch as the existing optref site.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 07:47:32 +03:00
0xEmz 312e59a9f7 Add optional mouse wheel and keyboard gameplay tweaks (gameplay_tweaks_enabled, off by default). 2026-11-12 03:29:24 +02:00
ipavlin98 3a8f93c64b Pick the oldest way of a target history set by index from the set's start, so a new target no longer overwrites the chunked replay's state (#1104)
tfr_note moved its pointer inside the search loop, so each later way was counted from the way it had picked and the
search could step up to 20 entries past the set. From the last set it wrote the new entry into RP, which follows the
table: the image handle into the take word, which left the submitting thread waiting in chunks_submit for a chunk no
recording thread took (a freeze with replay_workers above 0), and level and layer into the handle list, which crashed
chunk_record reading address 0x8. --probe-vkexec gets a part that stamps a set's ways so the old search left it.
2026-11-11 23:51:01 +03:00
Valiantsin DzerakhandClaude Opus 5.5 0ded6164a0 Describe optref_guard, ability_guard and report_guard in docs/settings.md (#1005)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 17:19:05 +03:00
Valiantsin DzerakhandClaude Opus 5.5 42c458712d Crash guards switchable from totk.ini: optref_guard, ability_guard, report_guard (#1005)
The three patch sets read their switch through cfg, so the keys already worked from totk.ini but logged as unknown and
were undocumented. They are now known keys with defaults (on), in [performance] of totk.ini.example; the settings menu
does not show them. The settings table reached 128 rows, the size of the menu's reset list, so that list grows to 256
and logs if the table ever outgrows it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 17:16:10 +03:00
Valiantsin DzerakhandClaude Opus 5.5 5d8c2ceb30 Drop an ability target only where the game would fault on it, and keep the live-actor test as a log line (#1005)
The game links an actor whose link record is null without complaint, so UltraTears' live-actor test could drop
targets the game handles. A target is now dropped only when the holder, the actor or its component is unreadable, or
the word the game's reference takes (+0x90, an atomic read-modify-write) is unreadable or in a module's code or
read-only data. The live-actor test logs the targets it would reject and changes nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 16:59:35 +03:00
itaskaev2andClaude Opus 5.5 fc985e690e Bring back the game window's pointer image as pointer_image, off by default
#1092's pointer image (runtime/cursor_art.c: a gold arrowhead with a dark edge, a turquoise gem and a soft shadow,
drawn at the window's DPI) was left out of dev because it changed the pointer for every player with no way to keep
the system arrow. It now comes with pointer_image (input section, live, default 0): 0 leaves the window class's arrow
as dev has it, 1 shows the image while the cursor is not captured. Win32 reads the setting at WM_SETCURSOR (the
next mouse move after a change); SDL sets the image or its default cursor when the settings change (input_sdl_idle).
The setting has its row under the mouse rows of the Controls tab, its text in the four menu languages, and
totk.ini.example and docs/controls.md describe it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 14:48:24 +01:00
Valiantsin DzerakhandClaude Opus 5.5 dde624e603 Skip a stale Recall / Ultrahand target and an unbounded model scene report row instead of crashing (#1005)
Set ability (1.4.3 main+0x1bd01c4, 1.2.1 main+0x1622bf0): the target actor load becomes a host call that reads the
holder safely and drops an actor failing the live-actor test (link record points back and carries its id), as
UltraTears found; the game's own cbz skips it for that frame. The answer comes back in x9, since the lifted code keeps
x21 in a C local across calls. Set report (1.4.3 only): the model scene report loop sends a negative handle or a row
index from 0x8000 to the game's handle reset, the class Weeping of the Empire met (#52, #73). Both on by default,
TOTK_ABILITY_GUARD=0 / TOTK_REPORT_GUARD=0 turn them off.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 16:41:29 +03:00
jcleng 00a0ffc197 Ask fontconfig for the menu's CJK font instead of hard-coding five paths
plat_font_path stats five absolute paths for NotoSansCJK-Regular.ttc and takes the first that exists. That covers
SteamOS and the usual distributions, but misses any system where the font lives elsewhere, and fails outright in a
sandbox: under Flatpak /usr is the runtime and ships no CJK font, the app may only write /app, and the host's fonts
appear at /run/host/fonts. All five paths miss, fonts_up merges 0 fonts, and every Chinese character in the settings
menu renders as '?' while English stays correct.

Asking fontconfig fixes all of these at once. It is the same database Tk/Xft and every other Linux toolkit reads; it
knows /app/share/fonts, the XDG data dirs, /run/host/fonts and the per-user directories, so installing the font where
the system looks is enough and no packaging can leave the menu stranded.

libfontconfig is dlopen'd, not linked: a host without it (headless, a bare container) still builds and runs and takes
the fallback, so this adds no build dependency, and the runtime already links -ldl for the NVIDIA libraries. The
result is checked against the character before it is used, because fontconfig returns the closest pattern even when
nothing covers a character -- without that a Latin-only system hands back DejaVu Sans and the menu believes it merged
glyphs when it did not. The pattern names zh-cn and zh-tw rather than zh-hans and zh-hant, which is how Noto CJK and
fontconfig tag the faces.

menu_font_fontconfig defaults to 1 and 0 goes back to the five fixed paths, for a player whose fontconfig resolves to
a font the menu's parser cannot read. TOTK_MENU_FONT names a file directly and wins over both. A system with the font
where the old paths expect it sees no change, and Windows, whose font names are fixed, is untouched.

Verified against the real fontconfig with the host having no CJK font, with fontconfig pointed at a directory holding
only NotoSansCJK-Regular.ttc (the Flatpak case, now resolved), with TOTK_MENU_FONT set and pointing at a missing file,
with menu_font_fontconfig=0, and with libfontconfig made unloadable (falls back quietly, no crash).
2026-11-11 13:15:39 +00:00
Valiantsin DzerakhandClaude Opus 5.5 d8a1d14104 Merge task/optref: an invalid optional reference no longer crashes the game in main+0x1bd0138 / 0x1622bec, it is treated as absent (#1005)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 15:43:08 +03:00
Valiantsin DzerakhandClaude Opus 5.5 1a033a4ec8 Apply the optref guard to 1.2.1 too, the same game code at main+0x1622bec (#1005)
1.2.1 has the identical cbz-then-load sequence, so set optref now patches both versions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 15:43:03 +03:00
Valiantsin DzerakhandClaude Opus 5.5 507e27da91 Treat an invalid optional reference at object +0x6b0 as absent on 1.4.3, so the random crash in main+0x1bd0138 stops (#1005)
The game checks the reference only for null, and players' crash dumps show it holding 0xffffffffffffffff after minutes to hours of play. MacNativeGames met the same field holding 0xffff0000ffffffff during a save load. Set optref (TOTK_OPTREF_GUARD, on by default) turns the cbz into a test for null or negative; valid holders are heap addresses below 2^63.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 15:26:26 +03:00
Valiantsin DzerakhandClaude Opus 5.5 0fef33ce30 Merge task/pr1112: DLSS upscaling on 1.2.1 shows the whole picture, the game's own scaling draw is skipped after DLSS (#1112 by itaskaev2, #1106)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 14:42:34 +03:00
Valiantsin Dzerakh 346f22bbd4 Merge pull request #1112 into task/pr1112 2026-11-11 14:41:21 +03:00
Valiantsin DzerakhandClaude Opus 5.5 a143cc1e6e Merge task/pr1087: grass_range, how far around the camera grass grows, off by default (#1087, #1085 by itaskaev2)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 13:53:25 +03:00
itaskaev2andClaude Opus 5.5 be5f4db3bf Skip 1.2.1's own scaling draw after DLSS instead of feeding it DLSS's picture (#1106)
On 1.2.1 the light target is scaled to the output by an upscaler of the game's own (twelve texelFetch taps,
fragment shader e6f7ae35b8479ef3), not by the FXAA draw vk_dlaa_skip_draw knows. It addresses its source in the
scene's pixels, so DLSS's output-sized picture swapped in for the light target (tex_info) showed only its top-left
part, two thirds at DLSS quality, stretched over the screen: the "floating camera" of #1106. #660 (169d057) made the
swap reach that draw: before it, DLSS ran on another target and its picture never reached the screen on 1.2.1.

DLSS already writes its picture into the scaling pass's target, so on 1.2.1 a draw in a pass larger than the light
target, without depth, that samples the light target is now skipped (vk_exec_draw_samples). The HUD drawn later into
the same target samples other textures and stays. 1.4.3 keeps the swap, where a tonemap can be the scaling draw.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 11:53:25 +01:00
Valiantsin Dzerakh 873fc64b41 Merge pull request #1087 into task/pr1087 2026-11-11 13:20:43 +03:00
Valiantsin DzerakhandClaude Opus 5.5 11c3ce1319 Merge task/kbmwave: T in the PC text prompts, opt-in mouse clicks on the map and in dialogue, the mouse wheel in menus, strips and the camera (#1098, #1096, #1097, #1092, #356, #62, #147)
Pull requests by lwj10case (#1098, #1096, #1097) and itaskaev2 (#1092); #1092's pointer image left out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 13:18:29 +03:00
Valiantsin DzerakhandClaude Opus 5.5 6576272a15 Leave the pointer image of pull request #1092 out: it changes the pointer for every player without a switch
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 13:13:22 +03:00
Valiantsin Dzerakh deba1a9b82 Merge pull request #1092 into task/kbmwave
# Conflicts:
#	runtime/input.h
#	runtime/menu_cursor.c
2026-11-11 13:12:21 +03:00
Valiantsin Dzerakh fc1cf24c42 Merge pull request #1097 into task/kbmwave
# Conflicts:
#	docs/controls.md
#	runtime/cfg.c
#	runtime/input.c
#	runtime/menu_cursor.c
#	runtime/ui/menu.cpp
#	runtime/ui_text.c
#	runtime/ui_text.h
#	totk.ini.example
2026-11-11 13:11:10 +03:00
Valiantsin Dzerakh 24bcc546e9 Merge pull request #1096 into task/kbmwave 2026-11-11 13:10:00 +03:00
Valiantsin Dzerakh 6ec533524c Merge pull request #1098 into task/kbmwave 2026-11-11 13:10:00 +03:00
Valiantsin DzerakhandClaude Opus 5.5 f23dc9ffb2 Merge pull request #1101: a mod's file named for another game version is served under its own name, so a sound mod that ships one plays (xellow78-lgtm)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 13:08:04 +03:00
Valiantsin Dzerakh 05003d517d Merge pull request #1101 into task/pr1101 2026-11-11 13:06:28 +03:00
Valiantsin DzerakhandClaude Opus 5.5 725c76b213 Merge pull request #1108: escape the pipes that split two docs tables and unlink the missing docs/milestones folder (viwe-monai)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 13:04:54 +03:00
itaskaev2andClaude Opus 5.5 9577d31f8b Zoom the Purah Pad's camera with the mouse wheel
In the camera (the ability wheel's camera) each notch of the wheel is a 100 ms press of the D-pad's up (in, for a
turn away from the player) or down, the zoom its button guide shows. The camera is found by its open screens: the
Purah Pad's frame and CameraPointer_00 without AppMap_00, which the Scope has (menu_cursor_camera). The Scope has no
zoom of its own, so the wheel stays unbound there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 10:23:19 +01:00
itaskaev2andClaude Opus 5.5 722a9c972a Describe the menus' dead zone and the pointer image in docs/controls.md
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 10:11:28 +01:00
viwe-monai 3360eca908 docs: escape table-splitting pipes and unlink the missing docs/milestones dir 2026-11-11 16:48:11 +08:00
itaskaev2andClaude Opus 5.5 c0ca89fdda Keep the wheel's focus until the pointer leaves a dead zone, and draw a pointer of our own
After the scroll wheel, the keys or the pad move the focus in a menu, the pointer takes it back only once it has
moved 6 layout units from where it was then (menu_cursor_poll), so a hand resting on the mouse no longer undoes a
scroll. While the cursor is not captured, the game window shows a gold arrowhead drawn at the window's DPI
(cursor_art.c) instead of the Windows arrow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 09:44:01 +01:00
Louis JusticeandClaude Opus 5.5 9f17d7899d Turn map_click off by default, so each key presses one controller button unless the player opts in.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 19:54:30 -05:00
Louis JusticeandClaude Opus 5.5 b477b489ff Turn dialogue_click off by default, so each key presses one controller button unless the player opts in.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 19:54:10 -05:00
Louis JusticeandClaude Opus 5.5 34ee6684a9 In dialogue the left mouse button presses B and the right one A instead of their bindings (dialogue_click, game 1.4.3).
By default the left button is Y, which opens the dialogue log in a dialogue box. While MessageDialog_00 is open the
left button now presses B, which advances the text and shows the rest of a scrolling line, and the right one A.
Dialogue choices keep the menu pointer, where a left click picks the choice under it, so clicking through a
conversation does not pick the choice the focus starts on. The setting is live and independent of menu_cursor.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 19:38:43 -05:00
itaskaev2andClaude Opus 5.5 a10c546a06 Scroll with the mouse wheel in the game's menus and along the D-pad strips
The wheel did nothing outside the map. Now each notch is one step of the stick a gamepad player would move: in a menu
with the game's focus box (the inventory, shops, Options, the save list) the focus moves up or down, and while a D-pad
strip (weapons, shields, bows, materials) is held open the choice moves left or right. The strip has no focus box
while it is held, so it is found by its open screen, ShortCut_00, as the map is. The wheel now also counts while the
cursor is released over the window, where the menus have the pointer. Input scripts take WHEEL lines and a recording
writes them, and a script's one-shot MOUSE or WHEEL line goes in at the first poll at or after its tick instead of
being lost when a poll skips that tick.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 00:23:48 +01:00
Louis JusticeandClaude Opus 5.5 c2716d2e7c The keyboard and mouse text prompts show Y as T, and the setup draws the PC prompts from totk.ini's bindings.
Y has two default keys: the left mouse button, which is a menu's A while the pointer is shown, and T, which presses Y
everywhere. The button font (menu guides, the dialogue log) now shows Y as its first bound key that is not a mouse
button; the button guide in play and the action prompt keep the left button. The PC prompts are a setup step of their
own, keyed by the bind_ lines of the install's totk.ini, so running the setup again after rebinding redraws them
without preparing the shaders again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 18:11:38 -05:00
Louis JusticeandClaude Opus 5.5 1becabd8f6 The left mouse button presses A on the Purah Pad's map instead of its binding (map_click, game 1.4.3).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 16:27:03 -05:00
Valiantsin DzerakhandClaude Opus 5.5 cc2b77bc4f Merge task/pr913: NVIDIA Reflex low latency, off by default, NVIDIA only (#913, #638 by STS78721, carried by itaskaev2)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 23:28:39 +03:00
Valiantsin DzerakhandClaude Opus 5.5 7fa727bbc5 Skip the present lock for the Reflex frame ID when nothing reads it, and declare the front end's Reflex calls in gfx.h
vk_reflex_present_frame took g_pl on every present although only flip_locked's VkPresentIdKHR (Reflex) and
latency_note (TOTK_TRACE=perf) read g_blit_pid. nvn_api_obj.c and ov_input.c declared vk_reflex_marker,
vk_reflex_frame_begin, vk_reflex_frame and vk_reflex_sleep locally; they now come from gfx.h, the graphics layer's
interface, which gfx_present.c also sees through vk.h, so a changed signature fails to compile. vk.h stays out of
those two files: it brings in Vulkan, VMA and vk_rec.h's vkCmd* macros.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 23:24:11 +03:00
Valiantsin DzerakhandClaude Opus 5.5 52b099bacb Merge pull request #913: NVIDIA Reflex low latency, off by default (#638 by STS78721, carried by itaskaev2)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 23:21:50 +03:00
Valiantsin DzerakhandClaude Opus 5.5 9f062e1d27 Merge task/pr864: tests/run_all.py runs every test with its interpreter, from pull request #864 (#675 by zzs181, carried by itaskaev2)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 23:08:43 +03:00
Valiantsin DzerakhandClaude Opus 5.5 bb339a614d Import at the top of tests/run_all.py and list cheats_vm_check
json, shutil and tomllib were imported inside the functions that use them. cheats_vm_check came into dev after the
table was written; it compiles runtime/cheats.c with clang like mouse_direct_check, so it takes the same cc need.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 23:08:43 +03:00
Valiantsin DzerakhandClaude Opus 5.5 8fc4d0a2d2 Merge pull request #864: tests/run_all.py, one entry point for the tests (#675 by zzs181, carried by itaskaev2)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 23:04:41 +03:00
toadjiangandClaude Opus 5.5 5e6ebd8bbc Free all DLSS size slots when the preset changes
evaluate's preset_dirty loop stopped at slot 3 of the five D.sizes entries, so a preset change left the features in
slots 3 and 4 created with the old model: they kept running until the slot was evicted. The loop now covers the whole
array through DLSS_SIZES instead of a literal.

Taken from pull request #1057 (t04dJ14n9) alone; the PR's dlss_prewarm commits are not in this change.

(cherry picked from commit a76bdf5e08)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 23:02:05 +03:00
Valiantsin DzerakhandClaude Opus 5.5 1cbe7bdb79 Merge pull request #990: the AppImage shadow test refuses a venv interpreter instead of passing without the user site (jay23606)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 22:59:10 +03:00
Valiantsin DzerakhandClaude Opus 5.5 dd91d60ba3 Merge pull request #1075: unpack a release archive again when its pin changes (ipavlin98)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 22:57:47 +03:00
Valiantsin DzerakhandClaude Opus 5.5 89de7b6c6e Merge pull request #1071: describe climb_fix in totk.ini.example (itaskaev2)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 22:57:46 +03:00
Claude 2cc0a4eaa6 Serve a mod's file named for another game version under its own name instead of leaving it out
A mod file whose name carries Product.<n> and which the RomFS lacks was counted as made for another game version and
never served. The game does ask for such names: on 1.4.3 with mods on, the file trace (trace = fs) shows it opening
Sound/Resource/Pack_MasterSword.Product.110.bars.zs, while its RomFS holds only the Product.140 and the unversioned
file. A sound mod that ships its pack under all those names was silent here and works on a console, where LayeredFS
shows the game every file of a mod. Such files are now added overlays like any other new file. Another version's
resource size table is still not served and still lends its entries; the files are still counted, and the Mods tab
and docs/mods.md now say they are read only when asked for by name.

tests/mods_romfs_check.py gets a case for it. Not run here: the test and --probe-fs need a build and the game's files.
runtime/romfs.c compiles with llvm-mingw.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TgQM2zwuVagwScByEJhT8V
2026-11-10 14:32:15 -05:00
itaskaev2andClaude Opus 5.5 9680b8e6ec Add grass_range: how far around the camera grass grows
The game grows grass blades only inside its near grass map, a 62.5 m square around the camera drawn into 256x256 and
128x128 textures, so on open meadows the grass appears a short way in front of the player. grass_range (percent, 50
to 400, default 100) scales the map's extent, step and both textures together through a new grass patch set (1.2.1
and 1.4.3) that loads them from patch page +128..+144, and grows the HeapDef grass heap and the application pool for
the larger textures. At 100 the set stays off and nothing changes. A row in the Display menu, text in all four menu
languages, and totk.ini.example describe it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 20:13:59 +01:00
Wes 5f6fc13712 Lift again only the chunks whose inputs changed
gen.py lifted every chunk on each run and only skipped rewriting the ones whose text came out the same, so a rebuild
after one changed code cheat still spent the whole lift (102 s on 12 cores, 178 s with 4 workers) to recompile two
chunks. A chunk's text depends only on the generator's sources and options, the module and the function starts
discovered from it, the chunk's number and functions, and the patch sites and spin waits inside its address range, so
the SHA-256 of these is now its key. <mod>/lift_cache.json records each chunk's key with the SHA-256, size and mtime of
its file and the statistics and folded ranges the manifest and folded.txt need, under a digest of the entry. A chunk
whose entry is intact, whose key matches and whose file is unchanged is reused without a worker; no pool starts when
nothing is left to lift. The cache is written after the manifest, and a failed write only warns. --no-cache, or
TOTK_LIFT_CACHE=0 for the installer's lift, lifts everything.

On 1.4.3 with the installed mods' cheats, a run with unchanged inputs takes 4 s and one with a changed cheat word lifts
one chunk in 7 s. Its output equals a --no-cache lift into an empty folder and the previous gen.py's output byte for
byte. tests/lift_cache_test.py checks reuse and invalidation on a module it makes itself.
2026-11-10 13:03:38 -06:00
ipavlin98 5956d6dd46 Unpack a release archive again when its pin changes instead of reusing the old archive's files 2026-11-10 21:08:37 +03:00
itaskaev2andClaude Opus 5.5 95052a280e List the tests added since, and the differential tests, in tests/run_all.py
The table predates exefs_patches_test and leaves out settings_doc_check, mouse_direct_check and tests/diff. A name may now name a script below tests/ (diff/simd), and a need of a compiler (cc, which mouse_direct_check passes as --cc) runs without --all, as only game files, a build, the embeddable Python and a desktop wait for it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 18:21:14 +01:00
zzs181 5635dd5e3a Let a test need more than one thing, and find the romfs where it actually lives
mods_romfs_check needs both a built totk.exe and the extracted romfs, and the romfs was hard-coded to build/extracted/<version>/romfs in two places inside the test. Needs now compose with '+' (exe+romfs) and each gets its own placeholder, the romfs is found in the extracted tree, the work dir or the GUI builder's record (build/build_gui.json), and any build/v143-*/totk.exe counts as the built exe - so the test runs on the layouts people actually build in instead of failing on a path it assumed. The test takes --romfs; its default is the old path, so running it by hand is unchanged.
2026-11-10 18:21:14 +01:00
zzs181 88cd32fddf Add tests/run_all.py: one entry point for the tests, each with its interpreter and needs
Each test already says in its header how it wants to be run; this reads that intent into one table (.venv for the lifter, .venv-py314 for the extraction tools) and runs them all:

    python tests/run_all.py                 # the ones that need no game files
    python tests/run_all.py --all           # also the ones that read game files or a built totk.exe

A need that cannot be met here is SKIP with the reason, never a failure: a missing game dump is not a broken build. Exit is 0 when nothing failed, 1 when something did. Windows picks clang up from C:/msys64/ucrt64/bin for the tests that compile C; on any other host the PATH is left alone. No third-party imports, so this runs before any venv is trusted.
2026-11-10 18:21:14 +01:00
itaskaev2andClaude Opus 5.5 6d6b66461b Reflex: one present ID per frame from its start to its present; mode again on a new swapchain; presentId feature
The latency markers and the present carried different present IDs: g_reflex_present_id went up only in flip_locked, so
frame k's simulation, input and submits had ID k and its present k+1 (and with frame_queue or replay_async frames could
share one). Each frame now gets its ID once, when the game acquires its window texture (SIMULATION_START,
vk_reflex_frame_begin), and carries it: INPUT_SAMPLE and SIMULATION_END take the game's current frame, a submission
the frame of the thread that submits it (the game thread's, or the one the replay thread got with its ReplayItem, or
the present thread's from its PresentJob), and the present the frame of its blit (PresentJob.pid -> g_flip_pid). A
present without a frame ID takes the next value, as present IDs must grow on a swapchain.

From the review: a new swapchain sets the sleep mode again (it starts with low latency off), Reflex is off without the
presentId feature, REFLEX sorted in known[], and the ru and pt-BR descriptions name the Boost option as the menu does.

TOTK_TRACE=perf logs every 600 presents the frame start to present time, and with Reflex on how many of the driver's
reports (vkGetLatencyTimingsNV) came back with every marker and their simulation start to GPU render end.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 18:20:16 +01:00
itaskaev2andClaude Opus 5.5 41623cec85 Call vkLatencySleepNV under the present lock
The swapchain must be externally synchronised, and the acquire and the present use it under g_pl on the present
thread while the game thread calls vk_reflex_sleep. With reflex = boost and TOTK_VK_VALIDATION=1 the validation layer
reported 20 threading errors in 30 s (vkLatencySleepNV against vkAcquireNextImageKHR and vkQueuePresentKHR). The
earlier head of #638 (318acd8) took the lock here; the rebase onto dev lost it. The semaphore wait stays outside.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 18:20:16 +01:00
itaskaev2andClaude Opus 5.5 c811d0e06d Carry Reflex's present ID through the recorder's submission
vk_sync_submit chains a VkLatencySubmissionPresentIdNV when the device has VK_NV_low_latency2, and on dev the
recorder thread (on by default except on AMD) makes that submission. rec_submit stopped the process on any pNext,
so every NVIDIA GPU with the extension would have failed at its first recorded submission. The recorder now copies
the present ID into its packet and chains it again in submit_run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 18:20:16 +01:00
Tears of the SteelTemple 3209ea4358 Implement Native NVIDIA Reflex Low Latency (VK_NV_low_latency2)
- Enable VK_NV_low_latency2 and VK_KHR_present_id with present ID features
- Add VkSwapchainLatencyCreateInfoNV to swapchain creation
- Link present and submission IDs with standard Vulkan latency types
- Insert latency markers: simulation start/end, input sample, render submit, and present
- Gate all extensions and latency chains on reflex != off (RESTART setting)
- Log graceful notice when Reflex is requested on unsupported hardware
- Add UI menu setting and translations for en, ru, pt-BR, zh-Hans
- Document reflex setting in totk.ini.example and docs/settings.md
2026-11-10 18:20:16 +01:00
itaskaev2andClaude Opus 5.5 2a564f648c Describe climb_fix in totk.ini.example
tests/settings_doc_check.py failed on v0.4.0: CLIMB_FIX is in known[] and described in neither file. It is a player
setting with a menu toggle under [frame rate], so it goes next to highfps_fix; frame_rate.c turns it off without the
real-time step.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 18:11:32 +01:00
Valiantsin DzerakhandClaude Opus 5.5 7a37fc3c34 Up version 0.4.0
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 19:25:38 +03:00
Valiantsin DzerakhandClaude Opus 5.5 bb8b4b1797 Turn the recorder thread on by default on AMD GPUs too; vk_recorder = 0 turns it off where the device is lost
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 19:25:37 +03:00
Valiantsin DzerakhandClaude Opus 5.5 396c4ae621 Describe the profile-guided build in docs/pgo.md: where and how to train, free play, merge, measure
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 19:07:38 +03:00
Valiantsin DzerakhandClaude Opus 5.5 27a63dfb0f Merge pull request #1065: Xbox prompts at every game resolution the setup lays the UI out for (cdonatom, #378)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 18:59:22 +03:00
Valiantsin DzerakhandClaude Opus 5.5 6f6704640a Merge pull request #1066: describe deswizzle, kspin_us, pcore, rec_async and rec_spin_us in docs/settings.md (itaskaev2)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 18:55:58 +03:00
Valiantsin DzerakhandClaude Opus 5.5 574c1ecc49 Merge task/climb: Link climbs walls and ladders at full speed above 60 fps (#35, #276, #1001)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 18:46:30 +03:00
Valiantsin DzerakhandClaude Opus 5.5 06811330e7 Extend the climb controller's frame-rate factor above 60 fps, so Link climbs walls and ladders at full speed (#35, #276, #1001)
The climb velocity step (1.4.3 main+0x1f49754, 1.2.1 main+0x1918220) multiplies the requested velocity by Nintendo's
factor for a movement loss that grows with the frame rate. 1.4.3 fits it at 30 and 60 fps and caps it at 2 from about
77 fps (63% climb speed at 120, 34% at 240); 1.2.1 has no 60 fps branch (78% at 60). Below a 1/60 s step the set climb
continues the factor with a curve fitted to a replayed ladder climb, taking the patch page's frame ratio without the
time scale, so 30 and 60 fps keep the shipping factor bit for bit, slow motion included. Only the player's climb
modules reach this code. Its own setting, climb_fix under [frame rate], with a menu toggle; off without the real-time
step. Ladder replay, 1.4.3: 1.665 / 1.663 / 1.669 / 1.681 m/s at 30 / 60 / 120 / 240 (shipping 120 1.046, 240 0.563).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 18:44:18 +03:00
itaskaev2andClaude Opus 5.5 e8fd6fd7a5 Describe deswizzle, kspin_us, pcore, rec_async and rec_spin_us in docs/settings.md
tests/settings_doc_check.py failed on dev with these five names in known[] and described nowhere. The rows follow the
code: vk_deswizzle.c (TOTK_DESWIZZLE), kernel.c kspin_ns, main.c (rt_knob PCORE), vk_rec.c rec_submit and rec_main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 16:28:21 +01:00
Valiantsin DzerakhandClaude Opus 5.5 be7c27047a Keep the 4096-a-side scene of an output near 8K under the new heap bound
The heap bound in scene_for counts the output's own growth against the 2 GiB limit, so a 7680x4320 output at
render_scale game went from the 4096x2304 scene dev renders to 2712x1526, with no setting changed. The bound now
shrinks no scene below 4096 a side, the size dev gave; scenes the 4096 cap kept smaller still grow up to the bound.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 18:20:31 +03:00
Valiantsin DzerakhandClaude Opus 5.5 1c1ff975f3 Default dynamic_res to 1 so the game keeps its own dynamic resolution
With dynamic_res at 0 by default the fixres set held the scene at 100 % for every player, the reason #573 was turned
down. The default is now 1, so the set stays off unless a player turns dynamic resolution off in the menu or
totk.ini, and the Steam Deck profile no longer needs its own entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 18:20:31 +03:00
Valiantsin DzerakhandClaude Opus 5.5 b6738cf41b Merge pull request #916: dynamic resolution as a setting, on by default, and a scene past 4096 wide (#573 by ProSep, carried by itaskaev2)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 18:20:31 +03:00
Valiantsin DzerakhandClaude Opus 5.5 dfc0b57457 Keep the per-frame pass table's comment to what the code does
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 17:59:54 +03:00
Valiantsin DzerakhandClaude Opus 5.5 173462f33d Merge pull request #1040: keep the targets a frame renders in the per-frame pass table, so draws with a pipeline still building skip a frame instead of waiting 50 to 400 ms (ipavlin98)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 17:59:53 +03:00
Valiantsin DzerakhandClaude Opus 5.5 3e037f0044 Keep the shadow log of players without sync_ahead as it was, sort the new setting names, and fix the sync_ahead frame-time sentence
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 17:55:57 +03:00
Valiantsin DzerakhandClaude Opus 5.5 b2db7809b4 Merge pull request #917: let the game run one frame further ahead of the GPU, sync_ahead, off by default (#676 by ProSep, carried by itaskaev2)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 17:55:23 +03:00
Carlos DonatoandClaude Opus 5.5 fb34177cfd Make the Xbox prompts for every game resolution the setup lays the UI out for (#378)
The setup wrote romfs-patches-xbox and romfs-patches-xbox-label for 16:9 only, so prompts = xbox on a 21:9, 32:9,
16:10 or 3:2 screen (the Steam Deck's 1280x800 among them) kept the game resolution at 1920x1080 (cfg.c) or, with
output_res set to the screen's shape, fell back to the Switch prompts. #979 fixed this for prompts = pc; this extends
the same mechanism to both Xbox folders, the shared-files option of #378 by way of #979's --reuse.

- The setup builds romfs-patches-xbox-<w>x<h> and romfs-patches-xbox-label-<w>x<h> for each of ASPECT_SIZES, from
  their 16:9 folder with --reuse, as it does for romfs-patches-pc-<w>x<h>.
- romfs.c overlay_init: at a game resolution of another shape any prompts' folder loads from <folder>-<w>x<h> in place
  of romfs-patches-<w>x<h>, not only the PC one; without it the Switch prompts stay, and the log names the missing
  folder and the command that makes it. romfs_shaped_pc_ready becomes romfs_shaped_prompts_ready(folder, w, h).
- cfg.c: the display-shaped output_res default applies with prompts = xbox when the folder of the pad layout
  (romfs-patches-xbox with the Nintendo layout, romfs-patches-xbox-label with any other) is there for that size.
- The menu's note for a missing shaped folder reads "off at this game resolution (no layout made for it)" in every
  menu language, since 16:9 is no longer the only shape with prompts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 15:53:49 +01:00
Valiantsin DzerakhandClaude Opus 5.5 8da13aa9b4 Merge pull request #1006: stop the installer space check at an unavailable drive root (Luxury213)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 17:43:41 +03:00
Valiantsin DzerakhandClaude Opus 5.5 719196f183 Merge pull request #1061: let a validation run with DLSS finish, a valid OpSelect in the motion fragment shader and set 1 bound first for the motion draws (itaskaev2)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 17:43:41 +03:00
Valiantsin DzerakhandClaude Opus 5.5 fce6364236 Merge pull request #1060: take the feedback-layout images back to GENERAL before the contact shadows' march (itaskaev2)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 17:43:40 +03:00
jay23606 bbd63f9720 Address review feedback 2026-11-10 09:24:56 -05:00
itaskaev2andClaude Opus 5.5 0adcc96bae Bind the motion draws' set 1 before pushing set 0 under the validation layer, as the replay's draws do
vk_mvobj_execute's draws (mo_emit) push set 0 and then bind set 1. Under the validation layer (SDK 1.4.363) a push for
a new INDEPENDENT_SETS layout keeps the push set the layer made for an earlier layout (the replay's last draw) and drops
the writes to bindings that layout lacks: every run with DLSS and validation reported the last-frame banks as missing
(VUID-vkCmdDrawIndexed-None-08114, variable vp_c20) and then faulted inside VkLayer_khronos_validation.dll. The
replay's own draws avoid this by binding set 1 first (set1_first in bind_descriptors); the motion draws now do the
same, only while the layer is active.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 15:11:08 +01:00
itaskaev2andClaude Opus 5.5 f804facd8b Take the feedback-layout images back to GENERAL before the contact shadows' march
vk_contact_exec ended the pass and recorded a memory barrier only. Once a G-buffer draw samples the linear depth or the
scene colour in its own pass, those images stay in ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT across passes
(vk_exec_draw.c, feedback loops), and the march then bound them in GENERAL as a sampled and a storage image. The Vulkan
validation layer reported it on every frame with contact shadows on (vkCmdDispatch imageLayout-00344, followed by
barrier, BeginRendering and QueueSubmit layout errors: 40 per run at Lookout Landing, 0 with contact_shadows = 0).
vk_exec_barrier ends the pass and moves those images back to GENERAL in the same full barrier, as every other
barrier does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 14:59:34 +01:00
itaskaev2andClaude Opus 5.5 708e0cfe8d Give the per-object motion fragment shader's OpSelect a condition vector, as SPIR-V 1.0 needs
tools/make_mvobj_fs.py assembled OpSelect of a vec4 with a scalar bool condition, which only SPIR-V 1.4 and later
allow. The module is 1.0, so spirv-val in the Vulkan validation layer rejected it (Expected vector sizes of Result Type
and the condition to be equal: Select). The condition is now splatted into a bvec4 first; runtime/vk_mvobj_fs.h is
regenerated and passes spirv-val.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 14:59:31 +01:00
Valiantsin DzerakhandClaude Opus 5.5 7f82a52da8 Merge task/gears: gear doors open above 30 fps on 1.2.1 (#330, #984)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 15:36:35 +03:00
Valiantsin DzerakhandClaude Opus 5.5 6433550e3d Scale the 1.2.1 gear rotation threshold by the AI update's frame delta, as 1.4.3 does (#330, #984)
ActorCheckRotateModule's update compared the angle a gear turned since the last update with one 30 Hz frame's worth,
so above 30 fps gears and water wheels read as stopped and the Riogok, Turakamik and Wind Temple doors stayed shut.
The new set gears keeps the update context's +8 (frames elapsed in 30 Hz units, the value 1.4.3 divides by) and
multiplies the threshold by it. Measured: the context is 0.25 at 120 fps with the real-time step and 0.5 at 60 fps
without it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 15:35:16 +03:00
Valiantsin DzerakhandClaude Opus 5.5 1503b17f7d Merge task/crash705: list the compound physics shape's children in the crash report (#705)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 10:20:07 +03:00
Valiantsin DzerakhandClaude Opus 5.5 4c4bd8c67a List the compound physics shape's children in the crash report for the 1.2.1 fault after the material lookup (#705)
On 1.2.1 the game asks a compound physics shape for its first child's material at main+0xe056d0 and reads the result
without a check. At the reported crash the shape had none of its 125 children left, although every child is filled
when the shape is built. The crash handler now prints that shape's live bits and each record's shape pointer and name,
which tells removed children from switched-off ones and names the object. It runs only after the crash.
--probe-kernel builds a fake shape in a crashing child and checks the listing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 10:19:50 +03:00
shenjack 41899438ea Merge remote-tracking branch 'origin/dev' into pr/v110-patch-parity 2026-11-10 14:25:07 +08:00
Valiantsin DzerakhandClaude Opus 5.5 3d386fe37e Merge task/inteluhd: dynamic alpha to coverage and vertex input off by default on Intel's Windows driver for integrated GPUs (#970, #992)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 09:13:52 +03:00
Valiantsin DzerakhandClaude Opus 5.5 905e2e720d Turn dynamic alpha to coverage and dynamic vertex input off by default on Intel's Windows driver for integrated GPUs (#970, #992)
UHD Graphics (i3-1215U, i3-1305U) and Iris Xe drew coloured noise on foliage with dynamic alpha to coverage and black
patches over the scenery with dynamic vertex input; players confirmed that vk_dyn_a2c = 0 and vk_dyn_vertex = 0 clear
both. vk_caps.intel_igpu_win marks Intel's proprietary Windows driver on an integrated GPU, and both defaults follow it.
An explicit ini value still wins. Arc discrete GPUs and Mesa's ANV keep both on: no report there, and the ANV report
(#989) had both off with its glitch still present.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 09:01:54 +03:00
Valiantsin DzerakhandClaude Opus 5.5 729b00a1c0 Merge task/sdl3420: ship SDL 3.4.20 in the release for pads without motion calibration (#912)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 08:58:32 +03:00
Valiantsin DzerakhandClaude Opus 5.5 2fe1ea1716 Bump the bundled Windows SDL to 3.4.20 (#912)
SDL 3.4.20 uses default Switch controller stick and IMU calibration when the
pad has none, which fixes the infinite gyro readings on the Beitong Asura 2 Pro+.
The pins in tools/make_release.py (version, zip and devel sha256, SDL3.dll sha256)
and the Windows rows of THIRD_PARTY_NOTICES.md follow. The sniper sysroot row stays.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 08:53:49 +03:00
shenjack ec62494bd9 Merge remote-tracking branch 'origin/dev' into pr/v110-patch-parity 2026-11-10 13:39:41 +08:00
Valiantsin DzerakhandClaude Opus 5.5 32f86a3296 Merge task/cheatc0: run cheat opcodes C0-C3 in the data cheat VM (#973)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 08:07:24 +03:00
Valiantsin DzerakhandClaude Opus 5.5 14b1b1aece Run cheat opcodes C0-C3 in the data cheat VM (#973)
The VM refused C0 (register conditional), so cheats like C046B400 FFFFFFFF were greyed out as unsupported. C0 now
runs with dmnt's operand types 0-5, widths and conditions; C1 and C2 save, restore and clear registers, C3 moves
registers to and from the static registers. tools/cheats.py gives the same opcode lengths and accepts the same opcodes.

skip() took the end of a nested block at depth 1 for the else of the failed outer block; it now counts depth the way
dmnt does, and 2x000000 is an else only for x = 1.

tests/cheats_vm_check.py runs tools/cheats.py and runtime/cheats.c on cheats of its own, the two from the issue
included.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 08:05:01 +03:00
ipavlin98 a4b46fa7a9 Keep the targets a frame renders in the table that tells per-frame passes from one-shot ones
The table of the frames each target was last rendered into was direct-mapped with 1024 entries. In a 1.4.3 field run about 16 entries rendered the frame before were taken by another subresource each frame, the scene's colour target among them, so every draw there whose pipeline was still building waited for it as one-shot, 50 to 400 ms, instead of skipping a frame. Sets of 8 ways that evict the way rendered longest ago lost none in the same run.
2026-11-09 21:22:38 +03:00
Valiantsin Dzerakh 8aba3647e0 Up version 0.3.14 2026-11-09 19:57:07 +03:00
Valiantsin DzerakhandClaude Opus 5.5 0238f3069a Merge task/pgostale: warn when a PGO profile was trained with other runtime headers
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 18:50:36 +03:00
Valiantsin DzerakhandClaude Opus 5.5 2d2c6782af Record the runtime headers a PGO profile was trained with and warn when they change
The floating-point helpers in simd.h and simd_fp.h inline into every lifted function before llvm-profdata matches
the profile, so the fpcheap change left pgo-v143 with a fifth of its weight while recomp/ stayed the same and
make_release saw nothing. pgo.py merge now reads the instrumented build's Ninja deps log and stores the digests of
the runtime/ headers the generated objects include (cpu.h, simd.h, simd_fp.h); make_release warns when one differs
or a record has none. docs/building.md says to retrain after a change to those headers, and build_game.py names the
libiconv-2.dll that MSYS2's SDL3.dll needs in deps-mingw/bin.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 18:50:36 +03:00
Valiantsin DzerakhandClaude Opus 5.5 bdcfb42c07 Add contributor and agent guides for pull requests, running and debugging
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 14:44:59 +03:00
Valiantsin DzerakhandClaude Opus 5.5 f9ed079614 Merge task/handheldscene: render the handheld scene at the asked size on 1.4.3
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 14:38:44 +03:00
Valiantsin DzerakhandClaude Opus 5.5 bed6962e09 Render the handheld scene at the asked size on 1.4.3
1.4.3 scales the scene size it reads from the patch page by canvas width over the console mode's base width, which is
1280 in handheld, not 1920. A handheld 1280x720 output at render_scale 100 put 1920x1080 on the page, so the game
rendered a 1920x1080 scene while the DynamicTextureAllocator heap was sized for 1280x720, and heavy scenes ran it out
(crash at main+0x1c2590). scene_scale now uses the mode's base for res_init and res_dlss_mode. In handheld the game
composes a 1280x720 canvas unless the output patch is on, so the output patch is switched on with the scene patch there
and the canvas is the output the factor and the heap assume. Docked is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 14:38:03 +03:00
itaskaev2 59eb026bd7 Merge branch 'dev' into carry/dynamic-res-100 2026-11-09 09:55:24 +01:00
itaskaev2 2e5d59df83 Merge remote-tracking branch 'origin/dev' into carry/sync-ahead
# Conflicts:
#	docs/architecture.md
#	runtime/cfg.c
#	runtime/vk_sync.c
2026-11-09 09:54:45 +01:00
Valiantsin DzerakhandClaude Opus 5.5 34b39ceca0 Merge task/memcommit: take 64 MiB blocks from the driver for buffers and other images
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 11:08:31 +03:00
Valiantsin DzerakhandClaude Opus 5.5 29cb79086f Take 64 MiB blocks from the driver for buffers and other images
Since the textures moved to a pool of their own, the default device-local
blocks hold mirrors and other buffers. With VMA's 256 MiB blocks one block
held 53 MiB of them after 10 minutes in the field, and the headroom thread
kept half a block of each device-local type ahead. TOTK_VMA_BLOCK_MIB
(default 64, 0 = VMA's 256) sets the size; the headroom spare follows it.

On the RX 9070, 10 minutes in the field: VMA blocks 3582 -> 3252 MiB,
process commit 8614 -> 8327 MB, slow frames 13 (2.8 s) -> 9 (1.1 s).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 10:51:49 +03:00
Valiantsin DzerakhandClaude Opus 5.5 a78268935a Merge task/memcommit: wait out a short out-of-memory spell instead of ending the game
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 10:15:50 +03:00
Valiantsin DzerakhandClaude Opus 5.5 cf44e9013c Merge task/memoom: wait out a short out-of-memory spell instead of ending the game
Conflicts: the image retry goes through vk_texture_image, sweep_pressure keeps the released-texture cap on the new
drop helpers, and vk_headroom_release also frees the texture pool's anchor.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 10:12:55 +03:00
Valiantsin DzerakhandClaude Opus 5.5 4ea472e864 Merge task/memcommit: textures in a pool of their own with 64 MiB blocks
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 10:11:51 +03:00
Valiantsin DzerakhandClaude Opus 5.5 8e05d2b409 Give textures a pool of their own with 64 MiB blocks
Textures shared the default 256 MiB blocks with the mirrors and other buffers, and a block went back to the driver
only once everything in it was gone. On the RX 9070 after a few minutes of roaming the field, 3 of 9 device-local
blocks were 13-27% full, held by a hundred small textures and buffers each, and blocks held 0.8-1.4 GB more than their
allocations; where device memory takes process commit, that commit backs nothing.

Own-memory textures now go to a VMA pool with 64 MiB blocks (TOTK_TEX_BLOCK_MIB, 0 = the default pools). Images of a
quarter block or more, images the driver wants a dedicated allocation for, and images the pool's memory type does not
take get memory of their own; the requirements are read before the image is made, since VMA checks neither for a
custom pool. The headroom thread keeps the pool's next block ahead like the default pools'.

Same 10-minute route: 807 MiB of blocks beyond their allocations instead of 1037, most pool blocks 75-100% full, slow
frames not up. Probes pass on v143.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 10:11:44 +03:00
Valiantsin DzerakhandClaude Opus 5.5 a79ff17314 Merge task/memcommit: count device memory against system memory where it takes process commit, BC3 for ASTC on integrated GPUs, cap the released-texture cache by bytes
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 09:58:10 +03:00
Valiantsin DzerakhandClaude Opus 5.5 0005c01cf5 Remove the stray brace in vk_mem_census that stopped vk_memory.c from compiling
79c6951 left the closing brace of a debug block in the per-heap loop, so the function ended early and the build failed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 09:57:32 +03:00
Valiantsin DzerakhandClaude Opus 5.5 34b340a208 Cap the released-texture cache by bytes
Released textures stayed until 2048 of them were kept or VRAM passed 90% of the budget. On the RX 9070 a 10-minute
field run kept 818 MiB of them, while almost every take-back came within a second. TOTK_TEX_RELEASED_MIB (default
256) runs the same drops as under VRAM pressure while the released entries hold more own memory than that. On the
same route the run ended with 1.1 GB less process commit and 1 GB less in VMA blocks; 15 dropped textures were
created again and the slow frames did not change.

Also documents TOTK_MEM_RESERVE_MIB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 09:57:32 +03:00
Valiantsin DzerakhandClaude Opus 5.5 213bc6af6a Wait out a short out-of-memory spell instead of ending the game
Crash logs ended in fatal errors when the system's commit ran out for a moment: a staging buffer (-2), a guest commit
with the system at 24442 of 24447 MB, image creation (-2). Windows grows a system-managed page file and some drivers
charge device memory to the process commit, so the same request often succeeds a moment later.

oom.c holds the wait nvn_api_cmd.c realloc_wait already used: it requests a release, retries every 10 ms for up to
30 s, logs the first 8 with the commit numbers. rt_oom_request sets a flag that vk_texture_new_submit takes (one atomic
load per submit): it drops up to 256 released textures of the two kinds sweep_pressure drops (overwritten, clean
mirrored), never GPU-written content, and frees the headroom blocks. vk_oom_release does at once what needs no submit:
headroom blocks, finished upload batches and deferred objects.

- memory.c commit: waits and retries without frz_lock between tries; every caller waits, boot ones too.
- vk_staging and the draw-data chunks (vk_exec_draw.c scratch): release, then wait; the open upload batch is not
  submitted, its caller still records into it.
- create_tex: release and one retry; no wait under the texture lock, the texture is asked for again at its next use.
- the texture write-back buffer: release, then wait.
- the headroom thread keeps no spare block for 30 s after a failure.

TOTK_TEST_OOM=<site>:<n> (commit, staging, image, chunk) fails the first n attempts; probe-kernel forces 3 refused
commits, probe-vkexec 2 staging buffers, 1 image and 1 chunk, and both pass through them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 09:53:30 +03:00
Valiantsin DzerakhandClaude Opus 5.5 5e479f593b Remove the stray brace in vk_mem_census that stopped vk_memory.c from compiling
79c6951 left the closing brace of a debug block in the per-heap loop, so the function ended early and the build failed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 09:51:04 +03:00
Valiantsin DzerakhandClaude Opus 5.5 79c6951688 Measure whether device-local memory takes process commit instead of assuming it per vendor
c838d96 counted system memory for every discrete GPU but NVIDIA's, an assumption. An RTX 5080 log (#398) showed 10.4 GB
of process commit at frame 1200, about what the RX 9070 holds with its VRAM counted, and the RTX 3060 Ti in #670 ran
out of system commit (17.8 GB) at 4K with its VRAM full. vk_mem_host_probe now allocates 128 MiB of device-local memory
once after device creation and compares the process commit before and after; half the size or more turns the system
memory cap on. The log line carries the measured amount, so user logs show what each driver does.

RX 9070: "a 128 MiB device-local allocation took 128 MiB of process commit".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 09:39:54 +03:00
Valiantsin DzerakhandClaude Opus 5.5 a689922180 Decode ASTC to BC3 by default on integrated GPUs
The size rule for TOTK_ASTC_BC looked at the largest device-local heap only. On an integrated GPU that heap is system
RAM reported as VRAM (10.6 GiB on the Ally X, 14.9 GiB on the 880M), so RGBA8 stayed on exactly where memory is
shortest. vk_mem_uma() now turns BC3 on there as well.

7900X iGPU, field save, same exe, frame 1800: RGBA8 commit 7047 MB, GPU 49-53 ms, 16.8-18.4 fps; BC3 commit 6155 MB,
GPU 42.6 ms, 21.6-21.8 fps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 09:20:09 +03:00
Valiantsin DzerakhandClaude Opus 5.5 d8090a85fe Keep headroom blocks for device-local memory types only
Headroom woke for every memory type that got new device memory, the staging pool's own 64 MiB blocks included, and
then kept a 256 MiB spare block in that type's default pool. On the RX 9070 the default pools of host memory types 1
and 3 held 4 MiB, beside 512 MiB of spare blocks. Textures and mirrors, whose block allocations headroom moves off the
render thread, live in device-local types.

RX 9070, field save, 60 s: heap 0 VMA blocks 192 MiB instead of 704, driver usage 1384 MiB instead of 1896; 60 fps,
1% low 51.9.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 09:05:09 +03:00
Valiantsin DzerakhandClaude Opus 5.5 c838d9692f Count system memory in the device-local budget where device memory takes host memory
The paths that give memory back under pressure (headroom's spare block, released textures, pool mirrors) watched the
Vulkan heap budget only. On a UMA GPU that budget is gigabytes of the same RAM the game runs in, and on Windows the
AMD driver charges device-local allocations to the process commit one to one (RX 9070: VRAM use and private bytes
grew by the same 1164, 256 and 256 MiB). Crash logs show the result: the process at 4.4-10.5 GB of commit, 16 GB
machines at 80-94% RAM, std::bad_alloc inside amdvlk64.dll (#285), allocation failures on Intel UHD.

vk_mem_host_room (vk_memory.c) is what the system can still give beyond a reserve (TOTK_MEM_RESERVE_MIB, 1536):
commit, and on a UMA GPU physical memory too; Linux overcommits, so there only a UMA GPU's available memory counts.
NVIDIA is left out, not measured. vk_mem_vram caps the budget at use plus that room, and headroom keeps its spare
block only while four blocks fit in it.

RX 9070, field save, 60 s with the room near zero from the start (TOTK_MEM_RESERVE_MIB=21000): commit 7124 MB instead
of 8965, VRAM 2631 MiB instead of 3781, released textures 153 instead of 1866; mirrors refused for 423 MiB of pools,
GPU 4.45 ms a frame instead of 3.34 ms, 60 fps either way. Without pressure nothing changes. The census logs the
room, the device class line the mode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 09:02:36 +03:00
Valiantsin DzerakhandClaude Opus 5.5 6e9ca1ebfd Log the process commit in crash reports and in the memory census
A driver that runs out of memory faults or throws std::bad_alloc instead of returning an error: #285's log shows
bad_alloc inside amdvlk64.dll under gpl_link, and nothing in the report told that apart from a driver bug. The crash
report now prints the process and system commit, as rt_fatal does for our own failed allocations; --probe-kernel
checks the line.

The census at frames 600 and 1800 logs the commit as a baseline and, per heap, VMA's blocks and allocations beside
the driver's usage and budget, so a report shows whose memory grew.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:53:57 +03:00
Valiantsin DzerakhandClaude Opus 5.5 717e68f601 Merge task/crashinfo: crash reports carry host registers, code bytes and memory at the register values
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:20:01 +03:00
Valiantsin DzerakhandClaude Opus 5.5 06d70d0dfa Report host registers, code bytes and memory at a crash
The crash report named the lifted function and printed the guest registers, but the lifted code keeps guest
registers in host locals and writes them back only at calls, so the dump showed stale values and the faulting
instruction stayed unknown. Logs from #1005, #967 and #998 could not be traced further than the function.

The report now adds the x86 registers of the faulting thread (Windows and Linux), 64 bytes of machine code around
rip with the offset into the lifted function, so the instruction can be found in a local build of the same version,
64 bytes of memory around every register value that points at readable memory, and the first 128 bytes of the
guest stack. --probe-kernel checks the new lines with a child that faults with known values in rbx and x19.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:19:31 +03:00
Luxury 2907f4efc3 Stop the installer space check at an unavailable drive root 2026-11-08 23:09:58 -05:00
shenjack 68b918933f Check every SIMD register for integer-only patch preservation 2026-11-09 09:13:09 +08:00
shenjack d2b1c084fa Keep the texture offset attribution in the component table 2026-11-09 08:59:35 +08:00
shenjack c8eb315c1e Describe 1.1.0 field of view support and strengthen patch preservation checks 2026-11-09 08:44:44 +08:00
shenjack 89364414ea Check the 1.1.0 patch contracts and document their coverage 2026-11-09 08:39:10 +08:00
shenjack 6966cee0d4 Bring the core frame and camera patches to game 1.1.0 2026-11-09 08:37:24 +08:00
Valiantsin DzerakhandClaude Opus 5.5 7e440be7ca Merge pull request #978: draw amiibo file names in any script in the F1 menu (KyokoMiki)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 21:13:04 +03:00
Valiantsin DzerakhandClaude Opus 5.5 e17a27acba Merge pull request #623: report heap corruption from the vectored handler (ipavlin98)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 21:13:04 +03:00
Valiantsin DzerakhandClaude Opus 5.5 35968c1efb Merge pull request #990: Linux AppImage: ignore the user site-packages so installer.gui imports (jay23606)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 21:13:00 +03:00
Valiantsin DzerakhandClaude Opus 5.5 169d057add Merge pull request #660: keep DLSS off the inventory screen (mictony6)
Ported onto vk_dlaa_plan (#794): the scene end in the inventory also records its submission and op (end_fence,
end_pos) so chunked replays see it at the same point. DLSS-only: everything sits behind dl_active().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 20:50:18 +03:00
Valiantsin DzerakhandClaude Opus 5.5 0704543249 Merge pull request #947: sync shadow pools from the per-submission write-watch snapshots (corne-ac)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 20:41:23 +03:00
Valiantsin DzerakhandClaude Opus 5.5 bdb41dff7e Merge pull request #979: PC prompts for every game resolution the setup lays the UI out for (Wintego)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 20:36:02 +03:00
Wintego 62b8e9dee5 Draw the PC prompts' Shift as an arrow over its name
The arrow alone read as a direction (review of #585). The key keeps the width of the game's own glyph, so the name goes
under a solid arrow rather than on a wider key.
2026-11-08 20:35:55 +03:00
Valiantsin DzerakhandClaude Opus 5.5 5e56bfc7f3 Merge task/chunked: record large submissions in chunks on several threads (#628, #794), off by default
replay_workers stays 0. On the 7900X 4 threads cut the replay by a quarter (town 6.7 -> 5.0 ms, fight2 5.3 -> 3.9 ms)
with no fps change: the game's own threads bound the frame there. Weak CPUs may gain.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 20:24:08 +03:00
Valiantsin DzerakhandClaude Opus 5.5 35b3d37ea3 Make a chunk's new texture entries follow replay order, and add a knob that records chunks out of order
entry_of decides at record time whether a new texture takes the content of a live GPU-written texture at its storage
(alias_copy) or the storage bytes. In a chunked replay the source's GPU ownership is set when a chunk records its
render, so the choice followed the order the threads got to the chunks: a later chunk could take the old bytes before
an earlier chunk recorded the render, and a mirrored texture keeps them. Now a chunk that makes an entry while a live
colour texture of its size sits at the storage waits until the chunks before it are recorded (vk_exec_chunk_settle,
the texture lock released for the wait), skips sources the scan revives at a later op, and ignores GPU ownership a
later chunk set in this submission. slot_resolve and vk_texture_by_id_locked no longer read tslots through a pointer
taken before the wait.

TOTK_REPLAY_SKEW_US (debug) makes the even chunks wait before they record, so the chunk after each one records
first in every submission. With it at 3000 us and 4 threads the field save runs clean (grass drawn, 0 validation errors); the alias path
did not fire there (0 copied, 0 filled from storage), so this does not reproduce the missing grass reported on
RADV, and the fix there stays unconfirmed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 19:53:12 +03:00
jay23606 9d07157b3e Linux AppImage fails with "No module named installer.gui" when the PyPI 2026-11-08 11:45:21 -05:00
ipavlin98 fd4427cb17 Run DLSS on the chunked replay
DLSS kept the replay on one thread: its decisions follow the passes in replay order, its jitter and camera are noted
draw by draw, and its evaluation reads the per-object motion list the scene's draws build. Now the scan takes DLSS's
decisions on its own state (vk_dlaa_plan, split out of vk_dlaa_pass) and notes the scene camera in replay order
(vk_dlaa_scan_draw), and the copy of the lit scene and the evaluation are records of the chunk that holds the op
(vk_dlaa_exec). A chunk's draws are jittered while they come before the op the scan ended the scene at. The
evaluation waits until the chunks before it are recorded, and the chunks after it wait for it (a hold), since they
sample its output. Chunks note their draws for the per-object motion one at a time (mo_lock in vk_exec_draw.c); the
uniform overrides of a motion draw and shader_for's last-frame variant flag are per thread, as the other chunks' draws
go on meanwhile; the FXAA shader table takes concurrent lookups. fxaa = 0 runs in chunks too.

With dlaa = 1 at 4K (DLSS Quality) on 1.2.1 and an RTX 5070, replay_workers = 4 takes the frame rate from 70 to 78
fps where the CPU is the limit, with no one-frame glitch in 241 consecutive frames.

(cherry picked from commit 04f8bb1b57)
2026-11-08 19:39:18 +03:00
ipavlin98 6b4d84ec50 Record large submissions in chunks on several threads
replay_workers = N (0, the default, keeps the sequential replay) records a submission of enough draws in chunks that start between passes, on N threads and in twice as many chunks, and submits them in order. In the town at 4K on an RTX 5070, 4 threads take the replay from 8.2 to 4.5 ms a frame and the frame rate from 71 to 79.

A sequential scan on the submitting thread applies only what is queue state (fixed-function and bind ops, render targets, render enable and conditions, samples-passed counting) and keeps a copy of it where each chunk starts, with how far its lists got: inline uniform updates, RENDER_COND results, contact shadow marches and the ops that drop uniform copies. A chunk takes those from the lists instead of from the chunks before it, starts with every image in GENERAL and ends with a full barrier, so its recording depends only on the copy and the lists.

Each thread records with a context of its own: the replay's memos, descriptor state, occlusion query segments and per-command-buffer state moved from file statics into VkExec and VkExecDraw, and the tables the contexts share (stand-ins, depth twins, views, mirrors, timestamps, completion callbacks, crumbs) take locks or atomics. The scan makes render targets live in replay order and notes the op (vk_texture_revive), since a chunk can sample a storage before the chunk that renders it got there or after the scan went past a later target there: a chunk's texture slots resolve as of its own op, and the chunks after a texture with no entry yet wait for the chunk that makes it. Uniform copies are dropped at the op that writes them (vk_usnap_drop_at).

Contact shadows plan their march in replay order (vk_contact_plan) and the chunk that holds its op records it; the HDR source keeps the copy that comes last in replay order. DLSS and fxaa = 0 act on the frame draw by draw and keep the replay sequential. TOTK_REPLAY_STATE_CHECK=1 compares the state after the last chunk with the scan's.

A draw or dispatch whose shaders write storage buffers drops the uniform copies they cover from its own op on, as the other GPU writes do (scan_writes). The scan still translates no shader: it reads the shaders the recording contexts translated from a table shared with them (vk_exec_draw_writes), and an op whose shader no context translated yet holds the chunks after it until it is recorded. Before, the chunk that recorded such a write dropped the copies for every position at that moment, so a later chunk recorded first bound the copy taken at the submit call: on an RX 7800 XT on RADV the grass near the camera came out dark or unshaded in some runs with 4 threads.

TOTK_TRACE=perf counts the chunks held per submission by reason and logs when each chunk of the last submission began. The settings menu has the thread count under Advanced, Scheduling, with the count this run uses under it.

The recorder thread begins, ends and submits the submission's open part (vk_rec.c rec_end_cb), so a chunk's mirror prologue stays off it: vk_sync_part waits until the recorder is idle, as the part pool allows one thread at a time, then records, ends and submits the prologue itself. Through the recorder the prologue could reach the queue before it was ended, and the GPU lost the device at the title screen.

(cherry picked from commit b94e2e9642)
2026-11-08 19:39:17 +03:00
Valiantsin DzerakhandClaude Opus 5.5 4fcedda5e6 Merge task/fpcheap: cheaper FPCR checks in the FP helpers
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 16:03:20 +03:00
Valiantsin DzerakhandClaude Opus 5.5 922edb177d Cheaper FPCR checks in the FP helpers
FCMP and the vector compares no longer flush denormal operands under FPCR.FZ: rt_fpcr_sync keeps MXCSR.DAZ equal to
FPCR.FZ, and x86 compares under DAZ read a denormal as a zero of its sign, as ARM does under FZ. The directed RMode
check of FN(op) and FN(vop) moved into the result check's branch, and min/max skip it, since they do not round.

Hot lifted functions shrink by about 10% of their x86 instructions; the town camera-pan bench (1080p, two runs each)
goes from 10.25 to 9.78 ms per frame. simd.py (60000 encodings), lift.py and func.py report 0 failures.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 16:03:14 +03:00
Valiantsin DzerakhandClaude Opus 5.5 31630dee65 Merge task/vbbind: rebind only the vertex streams a draw reads that changed (#974)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:01:38 +03:00
Valiantsin DzerakhandClaude Opus 5.5 7c5f88d33c Rebind only the vertex streams a draw reads that changed, and leave the streams it does not read bound
With vb_used the streams a draw does not read got the zero buffer, so draws in a row that read different streams
bound all 17 slots again almost every draw. On an RTX 5060 Laptop that cost 16 % of the frame rate (issue #974). Now
only the read streams are compared, one bind covers the changed ones, and the others keep the buffer bound earlier
in the command buffer, which the deferred frees keep alive until the submission is done.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 13:50:32 +03:00
美樹杏子 9382686c12 Fix Amiibo file names with non-ASCII characters displaying as garbled text in F1 menu 2026-11-08 18:39:21 +08:00
Wintego ceece5683a Make the PC prompts for every game resolution the setup lays the UI out for
The setup wrote romfs-patches-pc for 16:9 only, so prompts = pc on a 21:9, 32:9, 16:10 or 3:2 screen kept the game
resolution at 1920x1080 (cfg.c) or, with output_res set to the screen's shape, fell back to the Switch prompts.

- make_romfs_patches.py --reuse <folder>: a file of an earlier overlay whose contents match is hard-linked (copied
  where links fail) instead of compressed again; the output is byte for byte the same.
- The setup builds romfs-patches-pc-<w>x<h> for each of ASPECT_SIZES from romfs-patches-pc with --reuse: only the
  Common archive is compressed, so the fifteen folders take about two minutes and 6 MiB each instead of 22 minutes
  and 73 MiB each.
- cfg.c: the display-shaped output_res default applies with prompts = pc when romfs-patches-pc-<w>x<h> is there
  (romfs_shaped_pc_ready); Xbox prompts keep 1920x1080 as before.
2026-11-08 13:20:17 +03:00
Valiantsin DzerakhandClaude Opus 5.5 6cf4aab5f0 Merge task/pcore4: no P-core preference on hybrid CPUs with fewer than 4 P-cores
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 12:11:06 +03:00
Valiantsin DzerakhandClaude Opus 5.5 0d9525ba4a Leave the P-core preference off on hybrid CPUs with fewer than 4 P-cores
An i5-1235U or Core 3 100U has 2 P-cores. Binding the five frame-critical threads there, with a spinning recorder
among them, crowds two cores and their hyperthreads while the E-cores stay idle. plat_perf_core_setup now counts the
allowed physical P-cores and selects nothing below PC_MIN_CORES (4), so the scheduler places those threads as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 12:11:06 +03:00
Valiantsin Dzerakh b16c4c8938 Merge task/pcore: frame-critical threads prefer the P-cores of a hybrid CPU
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

# Conflicts:
#	runtime/vk_rec.c
2026-11-08 12:03:08 +03:00
Valiantsin DzerakhandClaude Opus 5.5 87fefa8ade Merge task/recasync: the recorder submits without the submitting thread waiting, larger publish batches without the poll
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 12:03:00 +03:00
Valiantsin DzerakhandClaude Opus 5.5 90fa6e1788 Let the recorder submit parts without the submitting thread waiting for it
rec_submit queued each part's submission and then drained the ring until the recorder had submitted it: 0.29 ms a
frame in the town on a Ryzen 7900X, 0.34 ms on 4 cores. Now it publishes the submission and returns
(TOTK_REC_ASYNC=1, default). The queue order the present, uploads, readbacks and device waits rely on is kept by
vk_queue_lock: a thread's first lock waits until the recorder submitted what was queued before (rec_queue_wait).
The recorder itself and a thread that already holds the lock skip the wait. rec_submit reports the first failed
submission the recorder made. Town, in-run A/B: replay -4.1% on all cores, -2.9% on 4 cores / 8 threads; queue
users waited about 0.02 ms a frame. fight2 with TOTK_REC_CHECK and the town under sync validation are clean.

Where the recorder does not poll (fewer than 6 usable logical processors) the producer now publishes the ring head
every 8192 words instead of 64, as Savage Wind Recomp does (runtime/nvn/vulkan/record.c rec_init): each publish
there costs an event set and a wake-up. Town on 4 logical processors: frame interval -1.3%. TOTK_REC_PUB=ab
compares both; the perf log counts the recorder's wake-ups and the queue waits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 12:02:53 +03:00
Valiantsin DzerakhandClaude Opus 5.5 3a26046cde Let the threads every frame goes through prefer the P-cores of a hybrid CPU
On an Intel CPU with P-cores and E-cores a thread on an E-core runs at about 60% of a P-core's speed, and the frame
waits for its slowest critical thread. The game's main thread, the thread that presents, and the vk replay, recorder
and present threads now select the CPU sets of the P-core logical processors the process affinity allows. Nothing is
selected on a CPU with one efficiency class or when the affinity leaves only one kind of core. TOTK_PCORE=0 turns it
off; "power:" log lines say what was selected and which threads took it. LAS Recomp does the same for its game,
render and vsync threads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 11:46:10 +03:00
Valiantsin DzerakhandClaude Opus 5.5 1446f3b06b Merge task/hybrid: hybrid laptops ask for the dedicated GPU, Windows power throttling off
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 11:13:45 +03:00
Valiantsin DzerakhandClaude Opus 5.5 320a41e0dc Ask hybrid laptops for the dedicated GPU and opt out of Windows power throttling
The executable exports NvOptimusEnablement and AmdPowerXpressRequestHighPerformance, which NVIDIA Optimus and AMD
switchable graphics read at start to run it on the dedicated GPU; Optimus can otherwise hide its GPU from a program
it put on the integrated one. At start the process opts out of EcoQoS (E-cores and low clocks on hybrid Intel CPUs
when Windows deems it in the background) and of Windows 11 ignoring its timer resolution while the window is hidden.
One "power:" log line says whether Windows accepted it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 11:13:40 +03:00
Valiantsin DzerakhandClaude Opus 5.5 8b81beb98b Merge task/kspin: kernel waits and the recorder poll briefly before they sleep
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 11:05:38 +03:00
Valiantsin DzerakhandClaude Opus 5.5 2d92565ada Poll briefly before kernel waits and the recorder sleep
Guest waits and waits for a virtual core usually end within microseconds. A thread now polls for 20 us with the
global lock released before it sleeps on its condition variable, so a quick wake costs no OS wake-up
(TOTK_KSPIN_US, 0 turns it off). The recorder polls by the clock for 60 us instead of 4096 pauses, which last a few
us on Zen 4 and about 140 us on Coffee Lake (TOTK_REC_SPIN_US). Both stay off with fewer than 6 usable logical
processors. Savage Wind Recomp polls the same way before it parks.

Machine A, town save, in-run A/B: frame interval -1.8% on average for the kernel poll (5 of 7 block pairs), -0.7%
with the process limited to 4 cores / 8 threads; the recorder poll is neutral there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 11:05:33 +03:00
itaskaev2 a48416dcf4 Merge branch 'dev' into carry/dynamic-res-100 2026-11-08 08:36:47 +01:00
itaskaev2 9b5ca3670f Merge branch 'dev' into carry/sync-ahead 2026-11-08 08:36:44 +01:00
corne-acandClaude Opus 5.5 a733b94645 Keep shadow_snap off by default under Wine until it has been measured there
Shadow storage is on by default under Wine as well, and the region snapshots were only measured on a native Linux
host. TOTK_SHADOW_SNAP=1 still turns them on under Wine.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 22:20:03 +02:00
corne-acandClaude Opus 5.5 02752376f7 Sync shadow pools from the per-submission write-watch snapshots
Each 2 MiB region is now queried at most once per guest submission, shared with the texture checks, instead of once
per pool at every part of a submission. On a Steam Deck at Lookout Landing the replay thread's write tracking fell
from about 4.4 to 1.3 ms a frame and the frame rate rose about 7.5%. TOTK_SHADOW_SNAP=0 restores the old queries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 22:01:05 +02:00
Valiantsin Dzerakh ae30a61b7b Some PR review ruleset 2026-11-07 20:22:50 +03:00
itaskaev2andClaude Opus 5.5 ce9ed75cee Merge dev into carry/sync-ahead
vk_memory.c: dev now keeps every finalized pool until retire_value (at
least the submission being recorded), which also covers a shadow with a
deferred copy, so shadow_had_deferred goes. The deferred shadow prologue
and dev's upload scratch pool both stay.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 18:22:50 +01:00
Valiantsin Dzerakh 214ba53a2a Up version 0.3.13 2026-11-07 20:22:35 +03:00
itaskaev2andClaude Opus 5.5 91f6fc9838 Merge dev into carry/dynamic-res-100
cfg.c: the key list takes dev's DESWIZZLE and keeps this branch's
DYNAMIC_RES and FIXED_RES_PATCH.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 18:21:41 +01:00
Valiantsin DzerakhandClaude Opus 5.5 8bbcf52709 Merge task/poolfree: pool memory and texture records kept until the submission being recorded is done (device faults under the synchronous replay)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 20:05:55 +03:00
Valiantsin DzerakhandClaude Opus 5.5 36a83bd78f Keep pool memory and texture records until the submission being recorded is done
vk_pool_fini freed import, physical and virtual pools right after vkQueueWaitIdle, which covers submitted work only;
with the synchronous replay another game thread may be recording a submission that names the pool. Every pool kind
now retires through the completion queue at the later of the recording's value and the next one.

destroy_tex freed the texture record while other threads still held &e->pub (render targets for a pass, storage
bindings writing wr_fence through it): they read a stale VkImage and cached views by that handle. The record is now
freed with its image's stamp (vk_defer_free), and views of a dropped entry are deferred at once instead of cached.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 20:05:49 +03:00
Valiantsin DzerakhandClaude Opus 5.5 9492315bac Merge task/deferstamp: deferred frees stamped with the submission being recorded (NVIDIA write-invalid faults, #788, #802) and the GPU address map for device faults
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 19:16:56 +03:00
Valiantsin DzerakhandClaude Opus 5.5 dc45de0e9a Stamp deferred frees with the submission being recorded, whichever thread destroys the object
With the synchronous replay (the default since 7be68ec) a game thread finalizing a texture destroys released texture
cache entries while another game thread records a submission that slot_resolve may already have handed one of them.
defer() saw the recording's value only on the recording thread and stamped the others with the last submitted value,
so the completion thread freed the image before the submission naming it ran: "write invalid" device faults on NVIDIA
(#788, #802). vk_sync_submit now notes the value at the start of the recording, and every deferral made from then on
waits for it. A fight run counted 10 such deferrals during the world load.

Device faults now log the deferred-free state, and with VK_EXT_device_address_binding_report the object that held the
faulting address and whether it was unbound before the fault (TOTK_ADDR_REPORT=0 turns the map off). The census line
counts deferrals made off the recording thread during a recording.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 19:16:50 +03:00
Valiantsin Dzerakh 6e5074e203 Merge task/pcsave: #911 (pipeline builds kept apart from the cache copy, the Intel crash at start, #681) and the cache save time in the log 2026-11-07 16:53:44 +03:00
Valiantsin DzerakhandClaude Opus 5.5 5b5273b541 Log how long each pipeline cache save copies and waits for builds
The copy time bounds how long a compute miss on the drawing thread can wait for the save now that builds and the copy exclude each other; a put-off autosave is logged too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 16:53:43 +03:00
Valiantsin Dzerakh d02a917af7 Merge #911: keep pipeline builds apart from the cache copy, so Intel's driver does not crash at start (#681) 2026-11-07 16:52:45 +03:00
Valiantsin DzerakhandClaude Opus 5.5 b7e72ae87f Merge task/gpuswz: block-linear textures deswizzle on the GPU (shorter stalls on entering new areas)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 16:38:33 +03:00
Valiantsin DzerakhandClaude Opus 5.5 57037ccc29 Deswizzle block-linear textures on the GPU when their storage is an imported pool
The replay thread deswizzled every block-linear texture on the CPU, and entering a new area stalled it for 50-236 ms. Now a compute pass reads the texture straight from the pool's imported buffer and writes a linear copy into a device-local scratch pool; the upload batch copies it into the image, or the ASTC decoder takes it. Large texture loads submit the batch early so the staging and scratch pools do not grow on the replay thread, and both pools are made at device creation.

On fight2 the render thread waits about 270 ms on textures per run instead of 900, and the load frame drops from 145 to about 80 ms. Mean fps is unchanged. TOTK_DESWIZZLE=cpu turns it off and check compares every upload with the CPU result (0 of 196k regions differ). Shadow mode (Linux/Wine) and textures outside the device's limits stay on the CPU.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 16:38:26 +03:00
itaskaev2andClaude Opus 5.5 b358b8f1f1 Keep pipeline builds apart from the cache copy, so Intel's driver does not fault in it
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 12:24:04 +01:00
ProSepandClaude Opus 5.5 a3d876e2d2 Let sync_ahead lag the 1.4.3 frame wait too
On 1.4.3 the per-frame wait is the second of the two nvnSyncWait calls in f_70002db8ac (return address 0x70002db99c). Like on 1.2.1, its sync comes from a two-entry ring indexed per frame; the first call waits on one object's sync behind a mode check. wdiasjunior found the site and measured it on an RX 7800 XT (#676): about 67-70 to 81-83 fps on Great Sky Island.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 10:55:56 +01:00
ProSepandClaude Opus 5.5 4624bf8076 Let the game run one frame further ahead of the GPU (TOTK_SYNC_AHEAD, off by default)
Each frame the game waits in one nvnSyncWait (1.2.1 lr 0x7000b23dfc) for the frame it presented last. On the Switch
that work is long done by then; here it is still in the replay and on the GPU, and on Great Sky Island the wait is
about 4.6 ms of a 13 ms frame. With TOTK_SYNC_AHEAD=1 that one wait waits for the frame before it instead, once the
replay thread submitted the asked value, so the game builds the next frame while the GPU runs the last one.

The game then writes guest memory that GPU work still in flight reads. Shadow storage makes that safe: a shadow update
of a pool with a reader in flight is queued and copied by the GPU in the next part's prologue, in queue order, instead
of memcpy'd under that reader (TOTK_SHADOW_DEFER=1, the default with the switch; 0 makes the replay thread wait for
the reader instead). The guard is read under the shadow lock and the wait takes that lock once after raising it, so no
sync that read the old guard is still copying when the game writes again. The switch needs shadow storage (Linux and
Wine by default, TOTK_SHADOW=1 on native Windows) and the 1.2.1 call site; otherwise it stays off with a log line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 10:55:55 +01:00
ProSepandClaude Opus 5.5 a6c33b072c Hold the game's dynamic resolution at 100 % and let the scene grow past 4096 wide
The game picks a scene tier of 100, 90 or 80 % mostly from a fixed cost model made for the Switch that grows with the
scene width squared, so on a wide output it drops to 80 % on any GPU: at 5120x1440 every frame drew 80 % of the scene.
Patch set fixres replaces the one instruction that moves the tier's scale into the controller's result (1.2.1
main+0xcc2c00, 1.4.3 main+0x5848a8 as MacNativeGames patches it) with 1.0. The new setting dynamic_res (menu toggle,
live) brings the game's stepping back and is on by default in the Steam Deck profile.

The scene cap goes from 4096 to 8192 a side, so render_scale above 80 works on a 32:9 output, and the scene also stays
within a DynamicTextureAllocatorMem under 2 GiB; the log says when it was made smaller. The memory selftest now maps
past the real pool size instead of a fixed 4 GiB, which a larger scene grows past.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 10:55:54 +01:00
Valiantsin Dzerakh 71d8b0cc61 Merge #900: lay the action prompt's diamond out as an Xbox pad with the xbox-label prompts 2026-11-07 12:38:12 +03:00
Valiantsin DzerakhandClaude Opus 5.5 561fe54c06 Merge task/crashinfo: crash reports name the driver frames, the C++ exception type and the host function called (#285)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 12:14:55 +03:00
Valiantsin DzerakhandClaude Opus 5.5 6e977280bb Name the driver code and the C++ exception type in crash reports
An exception no handler took (a fault in a GPU driver, a C++ exception a driver let escape) listed only the return addresses in totk.exe, and those match only the reporter's own build. #879 and #285 crash in the AMD driver with nothing in the log saying which driver code failed or what it threw.

The unhandled-exception filter now lists the frames of every module on the faulting thread with the module name and offset, up to 12 outside totk.exe so this executable's frames still fit. For a C++ exception thrown by MSVC-built code it reads the thrown type's decorated name from the ThrowInfo, and what() for a std::exception, with every read checked. The guest pc of a host function call is printed with that host function's name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 12:14:45 +03:00
Valiantsin DzerakhandClaude Opus 5.5 2f2bc6d9a3 Merge task/menumouse: #905 (T is Y's second default key, for the menus; a bind line takes its key from other buttons' defaults)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 12:08:37 +03:00
Valiantsin DzerakhandClaude Opus 5.5 469c528ffe Bind T to Y by default for the menus, and let a bind line take its key from the defaults of other buttons (#905)
While a game menu has the pointer the left mouse button is A, and it was Y's only default key, so keyboard and mouse players could not sort the inventory or press Y anywhere in the menus. T is now Y's second default key.

A bind line in totk.ini now also removes its keys from the defaults of buttons without a line, as the remapping rows already do: with bind_X = key:t, T presses X only, not X and Y.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 12:06:44 +03:00
Valiantsin DzerakhandClaude Opus 5.5 d2bb096cd6 Merge task/menumouse: #908 (the middle and side mouse buttons keep their bindings while a game menu has the pointer)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 12:03:22 +03:00
Valiantsin DzerakhandClaude Opus 5.5 61e8fd6bb9 Keep the middle and side mouse buttons bound while a game menu has the pointer (#908)
The bindings dropped every mouse button while the cursor was not captured, and the menu pointer releases the capture,
so side buttons bound to L and R turned the menu's tabs only in the frames before the pointer appeared. While the menu
pointer is shown and over the window, the middle and side buttons now count; the left and right buttons stay the
menu's A and B.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 11:58:03 +03:00
Valiantsin DzerakhandClaude Opus 5.5 fdf990957c Merge task/rtstamp: #795 (rebuild install-side objects and relink when the runtime contents change, whatever the file dates)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 11:46:01 +03:00
Valiantsin DzerakhandClaude Opus 5.5 1c59c98adf Merge #878: keep the native camera distance after aiming in direct mouse mode
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 11:21:19 +03:00
Valiantsin DzerakhandClaude Opus 5.5 2eb5adebd6 Merge #898: record only the dynamic states that changed since the last pipeline state
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 11:07:22 +03:00
Valiantsin DzerakhandClaude Opus 5.5 6c6f48c40d Merge #897: overlay uniform updates between passes too, and clear the overlays once their copies are recorded
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 11:07:22 +03:00
Valiantsin DzerakhandClaude Opus 5.5 f81dc57e9e Rebuild install-side objects and relink when the runtime contents change, whatever the file dates
The release zip dates every file 1980-01-01, so a new release extracted over an old Setup folder has sources and a
libtotkrt.a older than the objects and the totk.exe the last install left in temp\<name>\build. Ninja compares mtimes
only: it kept the old mouse_direct.o and romfs.o and linked them with the new library (undefined symbol
input_direct_mouse_shake on 0.3.11, romfs_shaped_ready on 0.3.12, #795), or skipped the link and kept the old exe
when the version string did not change.

With --runtime-lib, build_game.py now writes obj/runtime/inputs.sha256, one hash over the library, the INSTALL_SIDE
sources and every runtime header, rewritten only when the hash changes. The install-side objects and the link depend
on it. Builds without --runtime-lib are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 09:47:29 +03:00
Valiantsin DzerakhandClaude Opus 5.5 204f2843de Merge #861: load the NGX core through the platform layer, so vk_dlaa.c has no Win32 calls
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 09:39:34 +03:00
Valiantsin DzerakhandClaude Opus 5.5 316ba63619 Merge task/uborder: #896 (order overlapping uniform overlay copies), #859, #860, #862, #863 (checks that failed on dev)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 09:26:31 +03:00
Valiantsin DzerakhandClaude Opus 5.5 45b34ff4dd Merge #863: expect the base language in the probe's automatic menu language check
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 09:05:17 +03:00
Valiantsin DzerakhandClaude Opus 5.5 d9a789fb5f Merge #862: translate the speaker setting into Brazilian Portuguese and Simplified Chinese
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 09:05:17 +03:00
Valiantsin DzerakhandClaude Opus 5.5 bc7f2e1aff Merge #860: ignore every linked file in the worktree test's scratch repository
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 09:05:16 +03:00
Valiantsin DzerakhandClaude Opus 5.5 1b851e958f Merge #859: describe probe_movie_bench in docs/settings.md
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 09:05:16 +03:00
Valiantsin DzerakhandClaude Opus 5.5 70fa452002 Merge #896: order overlapping uniform overlay copies with a transfer barrier, so a partly overlapping newer update lands last
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 09:01:43 +03:00
Iker AcevedoandClaude Opus 5.5 90c14c9774 Lay the action prompt's diamond out as an Xbox pad with the xbox-label prompts
With pad_layout = xbox or auto, Switch A, B, X and Y are the buttons labelled so on an Xbox pad, but the diamond of
the action prompt (blyt/DoCommand_00.bflyt) kept the Switch positions: A on the right, where an Xbox pad has B.
make_romfs_patches.py --prompts xbox-label now moves each piece of the diamond to its partner's place (A with B, X
with Y): the button with its bar, the letters around it, the flashes of a repeated or held press, and the base image
that lights the button's side. X's bar reaches left, as Y's does on a Switch. The other prompt styles are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 23:11:33 -05:00
Claude 752e15bcc7 Record only the dynamic states that changed since the last pipeline state in the command buffer
Each change of pipeline state in prepare_draw re-recorded the whole dynamic state through vk_pipeline_set_dynamic:
about twenty commands, the vertex input packet rebuilt, and the blend enable, equation and write mask of every
attachment, however few fields differed. Every replay pipeline declares the same dynamic states, which a pipeline bind
leaves as they were, so vk_pipeline_set_dynamic_delta compares the values with what the command buffer already holds
and records only the commands that differ; the vertex input is rebuilt only when the vertex shader, attributes or
streams change. The record is per command buffer (B.dyn_valid, cleared with the rest of B on a new one or a split).
TOTK_DYN_DELTA=0 records the full set as before; ab alternates; TOTK_TRACE=perf counts calls, commands and calls that
changed nothing.

1.4.3, RTX 4070 Ti, 2560x1440 DLAA, standing still on Great Sky Island, TOTK_AB=10 (tools/ab_summary.py): 13.75 ms
a frame with the full sets, 12.59 ms with the deltas (-8.5%, 72.7 to 79.4 fps); replay 7.77 to 6.67 ms. An A/A run
with the same method moved the frame by 0.08 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BspNcezsiV8zT8wcnXrrME
2026-11-07 03:00:57 +00:00
Claude 324f85602f Clear the uniform overlays once their copies are recorded
After vk_exec_ub_flush the full barrier barrier_to records makes the copied bytes visible, so later bindings can read
the real buffer. The overlays stayed until the submission's end instead: every later binding inside their span
scanned them, and a block an overlay covered in part got a merged scratch copy that the descriptor build skip then
left alone. With the updates between passes overlaid too (Great Sky Island: 274 of 594 per submission) that was 290
merged copies and 1.1 ms more replay a frame, more than the barriers saved on the GPU (0.3 ms).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BspNcezsiV8zT8wcnXrrME
2026-11-07 00:18:52 +00:00
null15564 2a937319b4 Restore native camera distance after aiming with direct mouse input 2026-11-07 05:55:00 +07:00
Claude 49b2314dd8 Overlay uniform updates between passes too, so a run of them before a pass costs one barrier
An update between passes was copied at once behind a full barrier of its own, and the copy it recorded made the next
update or pass begin need another: a run of k updates before a pass cost k + 1 full barriers, about 250 updates a
frame on Great Sky Island. They now become overlays like the updates inside a pass; every GPU reader of the real
buffer after them comes behind barrier_to, which records the copies first. TOTK_UB_OVERLAY_OUT: 1, 0, ab; the perf
line counts the overlays made between passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BspNcezsiV8zT8wcnXrrME
2026-11-06 22:35:01 +00:00
Claude b317f1c37e Order overlapping uniform overlay copies with a transfer barrier
vk_exec_ub_flush recorded the copies of a pass's overlaid uniform updates back to back behind one barrier. Copies
with no barrier between them may run in any order, and ov_add only drops an older pending copy of exactly the same
range, so two updates that overlap in part could leave the older bytes in the buffer where they overlap. A copy that
overlaps one recorded since the last barrier now waits for it behind a transfer-to-transfer barrier; the perf line
counts those barriers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BspNcezsiV8zT8wcnXrrME
2026-11-06 22:33:24 +00:00
itaskaev2andClaude Opus 5.5 1dfe05679e Expect the base language in the probe's automatic menu language check
resolve_menu_lang falls back from a region tag the catalog lacks to its base language (ru-RU: ru), but probe_menu_text expected English for such a tag, so --probe-services failed on any Windows whose UI language is ru-RU, de-DE and the like; en-US maps directly and passed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 19:20:20 +01:00
itaskaev2andClaude Opus 5.5 1d89570ba8 Translate the speaker setting into Brazilian Portuguese and Simplified Chinese
963901c added the Speakers row (stereo or 5.1) in English and Russian only, so --probe-services failed: menu text ids 189 to 193 lacked pt-BR and zh-Hans, and the menu showed them in English in those languages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 19:19:47 +01:00
itaskaev2andClaude Opus 5.5 cc4df7d140 Load the NGX core through the platform layer, so vk_dlaa.c has no Win32 calls
tools/check_platform.py failed on vk_dlaa.c: it read the NGX core's folder from the registry, loaded it with LoadLibraryA and GetProcAddress and named the executable's folder with GetModuleFileNameW. platform.h gets plat_ngx_core_path (the registry folder plus _nvngx.dll on Windows, libnvidia-ngx.so.1 on Linux), plat_exe_dir_wide (the folder as NGX's wchar_t string; the UTF-8 to UTF-32 conversion moves from vk_dlaa.c to linux.c) and plat_lib_error, and the core loads through plat_lib_open and plat_lib_sym on both hosts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 19:18:21 +01:00
itaskaev2andClaude Opus 5.5 9233322aa8 Ignore every linked file in the worktree test's scratch repository
tools/worktree.py links config.local.toml and CLAUDE.local.md since e3e72df, but the test's .gitignore listed only the first, so git status in the new worktree showed CLAUDE.local.md and the test failed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 19:08:11 +01:00
itaskaev2andClaude Opus 5.5 5cd48ea696 Describe probe_movie_bench in docs/settings.md
tests/settings_doc_check.py failed on dev since 3cd2410 added TOTK_PROBE_MOVIE_BENCH to known[] without a row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 19:07:38 +01:00
ProSep e54c0f7f75 Merge remote-tracking branch 'origin/dev' into distant-detail-merge
# Conflicts:
#	recomp/patches-v121.txt
#	runtime/cfg.c
2026-11-05 23:28:19 +01:00
ProSepandClaude Opus 5.5 1584d1ae23 Add the 1.4.3 sites of the distant detail settings
The level picker is the same function in 1.4.3 (main+0x1ee8424) with the same first words and an early return straight
to its ret, so the lod site takes the 1.2.1 words as they are. The graphics settings setup (main+0x5868c4) inlines the
getters that 1.2.1 calls, and their slow paths come back before the compare, so the steady site is the compare itself:
w10 is set to 9 there and the list walk is skipped to the epilogue, which does not read w10. The menu texts, README and
totk.ini.example now name both versions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 01:13:01 +01:00
ProSep 84acab6fda Merge remote-tracking branch 'origin/dev' into distant-detail-merge
# Conflicts:
#	runtime/cfg.c
2026-11-05 00:57:35 +01:00
Michael Anthony Bitoon cd32de5ff9 Keep DLSS off the inventory screen and on at every camera angle, with the scope or the camera rune open
The inventory's preview light target is drawn without depth before the bloom chain; a pass like that with nothing armed ends the scene once the bloom follows, so DLSS no longer runs on the finished menu picture. Arming on that target stays allowed, because at some camera angles the field's effects composite comes before the first pass with the scene depth. The G-buffer width is the widest of the frame, a small pass ends the scene only into a scene colour target, and no pair narrower than the widest G-buffer arms: with the scope open the half-size effects pass armed and DLSS scaled the effects into the light target.
2026-11-04 20:01:27 +08:00
ProSepandClaude Opus 5.5 47eed81122 Add the Portuguese and Chinese texts of the distant detail settings
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 00:08:28 +01:00
ProSep 4403a3f8ea Merge remote-tracking branch 'origin/dev' into distant-detail-merge
# Conflicts:
#	recomp/patches-v121.txt
#	recomp/patches-v143.txt
#	runtime/cfg.c
#	runtime/ui/menu.cpp
#	totk.ini.example
2026-11-04 00:08:28 +01:00
ipavlin98 23d24bbc77 Report heap corruption from the vectored handler
The heap manager raises STATUS_HEAP_CORRUPTION and then ends the process itself, so the exception never reaches the unhandled-exception filter, and a run that corrupted its heap ended without a FATAL line. crash_veh hands it to crash_filter, which names the module and the host frames as for any other fault outside the executable.
2026-11-03 23:15:59 +03:00
Zenrap0518 22e42662e5 Match RenoDX default gamma correction 2026-11-02 15:41:11 -07:00
Zenrap0518 5d06d60bd7 Match HDR tonemapping more closely to RenoDX defaults 2026-11-02 14:15:36 -07:00
ProSepandClaude Opus 5.5 a211c4cb3e Keep the game's level for one-level models in the lod patch set
The level picker returns 0 for a model with one level, and the emulator mod returned the floor at its parameters' +0x28
for those too, without checking it against the level count; a floor above 0 would index past the one-level list. The
site now masks the floor to 0 when the count is 1, without flags (a site may use them once), and every other model still
takes the early return with the floor.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 04:14:56 +01:00
ProSepandClaude Opus 5.5 11cdd8f797 Add distant model detail settings for 1.2.1
lod_detail and lod_steady (default 0) are two patch sets after the emulator mods "LOD Improvement v2" (theboy181,
sweetmini) and "Disable LOD Quality Reduction": the model level picker returns its finest allowed level at any distance,
and the graphics settings setup skips the reduction of detail under load. The mods turn a conditional branch into an
unconditional one; the generator patches only plain instructions, so the sites feed those branches instead.

The sites are for 1.2.1 only. 1.4.3 and 1.1.1 declare the sets without sites until theirs are found in those builds.
On the Screen tab of the settings menu (English and Russian) and in totk.ini.example.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 03:17:10 +01:00
159 changed files with 16123 additions and 2155 deletions

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
<!--
Target branch: dev. Rules: CONTRIBUTING.md, "Pull requests". Write each section in your own words about this diff;
delete the rows that do not apply. Never attach game files, keys, extracted data or logs that contain them.
-->
## Problem
<!-- What the player sees or what is wrong in the code. Issue link if there is one. -->
## Change
<!-- What the diff does and why this way. Name the switch (totk.ini setting or TOTK_* variable) that turns it off. -->
## Where it reproduces
<!-- OS, CPU, GPU and driver, game update (1.2.1 / 1.4.3), settings. Hardware it cannot appear on, if known. -->
## Proof
<!-- Before and after on the same repro: screenshots, log lines, a test that failed and now passes. -->
## Checks run
<!-- The rows of CONTRIBUTING.md "Checks by what you changed" that match the diff, each with its result. -->
- [ ] `python tools/check_content.py --staged`
- [ ] `totk.exe --selftest`
## Measurements
<!-- Frame path, timing or scheduling only. Per run, switch off and on, at least two each: average fps, 1% low or
worst frame, `perf: slow frame` count. Scene, save, seconds, CPU, GPU, driver, resolution. -->
## Not run
<!-- Paths you could not run (Linux, HDR, a vendor you do not have) and how they are gated. -->
+9
View File
@@ -29,6 +29,15 @@ This repository is public. Game data lives outside it and every derived file sta
- Graphics bugs: [docs/maxwell.md](docs/maxwell.md) lists the Maxwell behaviour the Vulkan path must match and the TotK bugs other ports fixed.
- Comments say what the code does and why, in words that stand on their own. Cite our code by file plus function (a line number may follow as a hint) and upstream code by project, file and function: `refs/` moves, so its line numbers rot. Commit hashes belong to the attribution of copied code.
## Running and debugging
- Running the game, screenshots, frame dumps, timings: [docs/agents/running.md](docs/agents/running.md).
- Investigating a bug, glitch or crash: search [docs/agents/symptoms.md](docs/agents/symptoms.md) by the player's words first, then follow [docs/agents/debugging.md](docs/agents/debugging.md). Read the code before running the game.
## Pull requests
Preparing a pull request: target `dev`; the checks by changed area, the proof and the performance runs it carries are in [CONTRIBUTING.md](CONTRIBUTING.md), "Pull requests". Reviewing one: [docs/pr-review-kit/](docs/pr-review-kit/).
## Writing
Code, comments, docs and commit messages are in English. Commit messages are plain sentences without type prefixes.
+86 -7
View File
@@ -29,14 +29,93 @@ Use the issue form. Include the port version and game update from the title line
## Pull requests
- One topic per pull request.
- Describe the problem, then what changed.
- Say how you tested it: which version, which scene, how long.
- A change to the lifter (`recomp/`) or the CPU headers passes the differential tests in [docs/building.md](docs/building.md), "Checks".
- Commit messages are plain sentences, without `feat:` or other prefixes.
- AI-assisted code is welcome. You are responsible for having read and run it.
### Branch
Open pull requests against `dev`. Branch from the current `dev` and rebase on it before you open the pull request. `main` receives `dev` at a release; a pull request against `main` is retargeted to `dev` before review.
### Before you start
- Search the open pull requests and issues on the topic. When another pull request already covers it, improve that one or say in yours how they differ.
- Fixes are reviewed before features, in order of how many players the bug hits. A feature with a design in `docs/milestones/` (mods, cheats, the menu) follows that design. For a feature without one, open an issue first.
- Find out where the bug reproduces: OS, GPU vendor and generation, driver, game update, settings. Put that in the description.
### What the change must keep
- **The default for everyone.** A fix for one setup (a GPU vendor, a memory size, a display) is gated on that setup. Players without the problem see no change.
- **A switch.** New behaviour on the frame path or in rendering sits behind a `totk.ini` setting or a `TOTK_*` variable, so a player can turn it off and a reviewer can compare. A new setting is described in [totk.ini.example](totk.ini.example) and [docs/settings.md](docs/settings.md).
- **The fix, not a disabled feature.** A crash that goes away with a feature off points at the bug in that feature. Turning the feature off for a vendor is not a fix.
- **The frame time on weak hardware.** Game threads and the render thread do no file I/O, no GPU allocations, no per-draw state copies and no global atomic counters. Memory kept per frame or per object is freed. The full list with measured costs: [docs/pr-review-kit/02-performance.md](docs/pr-review-kit/02-performance.md), "What the hot path must not do".
- **Other GPUs.** New Vulkan code queries each feature, extension, format and limit it uses and keeps a working path without it. Nothing is hardcoded from what one GPU reports.
- **Text in the catalogs.** Text a player sees gets an id in `runtime/ui_text.h` and an entry in `runtime/ui_text.c` for every menu language (English, Russian, Brazilian Portuguese, Simplified Chinese); installer text goes into `installer/text/*.json`.
- **The generator owns generated C.** Change `recomp/`, never `generated/`.
### Checks by what you changed
Run every row that matches your diff and list the results in the description. Commands and details: [docs/building.md](docs/building.md), "Checks".
| You changed | Run |
|---|---|
| anything | `python tools/check_content.py --staged`; `build\<ver>\totk.exe --selftest` |
| `recomp/`, `runtime/cpu.h`, `runtime/simd.h` | the differential tests against Unicorn: `tests\diff\lift.py`, `simd.py`, `atomic.py`, `func.py`, `tests\flags_check.py`, `tests\excl_mt.py`; the same with a `--cpu v2` build |
| `recomp/jumptables.py` | `tests\jumptables_check.py`: 0 disagreements on 1.2.1 and 1.4.3 |
| `runtime/` | `--probe-kernel`, `--probe-vkexec`, `--probe-nvn-tex`, `--probe-shader`, plus the probe for the file you touched (list at the top of `runtime/main.c`) |
| settings | `totk.exe --probe-cfg`; `python tests\settings_doc_check.py` |
| button prompts | `.venv-py314\Scripts\python tests\romfs_prompts_test.py` |
| mods, ExeFS patches | `.venv\Scripts\python tests\exefs_patches_test.py`; `totk.exe --probe-mods` |
| `installer/` | `python tests\installer_test.py`; `python tests\installer_gui_test.py` |
| shader translator | `tools\xlate_smoke.py`; `tools\shader_census.py --version <ver> --translate` with no new failure groups |
| rendering | a run in a scene where the change acts, with `TOTK_VK_VALIDATION=1`: no new validation messages; screenshots before and after |
| frame path, timing, scheduling | the measurements below |
Checks that need game files run on your own copy of the game. Results go into the pull request as text: pass or fail, counts, log lines. Never attach logs or dumps that contain game data.
### Proof
- **A bug fix** shows the bug before the change and gone after it, on the same repro: a screenshot pair, a log line, a probe or test that fails before and passes after. A scripted repro (`TOTK_INPUT_RECORD`, [docs/controls.md](docs/controls.md), "Scripted input") is the strongest. If the bug cannot be reproduced on your hardware, say so and point at the code that causes it.
- **A crash fix** names the crash: the log's crash report, `vk: pass marks` lines for a lost device.
- **Parts you could not run** (Linux-only code, HDR, vendor-only paths) are listed as unrun and gated away from everyone else.
### Performance measurements
A change on the frame path, in timing or in scheduling carries these numbers, or it waits until someone measures it.
1. **Ceiling first.** Before the work, estimate the share of the frame the changed code holds on the critical path (`TOTK_CRIT` + `tools/crit_path.py`). A ceiling of a few percent is not worth a hot-path change.
2. **Build 1.4.3** with prepared shaders in that build folder (`tools/prepare_shaders.py`, [docs/building.md](docs/building.md) step 6). Caches copied from another build folder do not count.
3. **A heavy scene** where the change acts: a fight with a large enemy and a crowd, or a town, with `TOTK_FPS_LIMIT=360`.
4. **Whole runs, switch off and on, at least two each**, the same save and the same input script, timed by game seconds:
```powershell
.venv\Scripts\python tools\run_game.py build\v143 --script <input.txt> --seconds 60 --quiet --env TOTK_TRACE=perf,sched --env TOTK_FPS_LIMIT=360 --env <SWITCH>=0
```
`--quiet` waits until no other game or build runs. Close everything heavy on the machine. Before reading the numbers, check that each run reached the world and showed the same scene.
5. **Report per run**: average fps, 1% low or worst frame, the number of `perf: slow frame` lines, VRAM; `tools/perf_summary.py` condenses the log. Add CPU, GPU, driver, RAM, resolution and the settings you changed.
A switch toggled inside one run on a moving scene gives false results: compare whole runs. Zero new slow frames is a hard bar; an fps gain does not buy back new hitches. Background and the reasons behind each rule: [docs/pr-review-kit/02-performance.md](docs/pr-review-kit/02-performance.md).
### Description and commits
Fill in the pull request template: problem, change, where it reproduces, checks, measurements, what stayed unrun. One topic per pull request.
Commit messages are plain English sentences, without `feat:`, `fix:` or other prefixes. A commit that works on an issue ends its subject with the issue numbers: `Render the handheld scene at the asked size on 1.4.3 (#998, #536)`.
### Review and merge
The maintainers review every pull request against [docs/pr-review-kit/](docs/pr-review-kit/): read it to see what gets a pull request rejected, which bug classes are already fixed and which invariants a change must keep. A good pull request that misses the project's conventions is fixed up while merging rather than sent back. Part of a pull request can be taken; the reason for what was left out or rejected is posted on the pull request.
Accepted work merges into `dev` and reaches players through test builds. `main` moves only at a release.
## Coding agents
AI-assisted code is welcome. You are responsible for having read and run it, and the pull request says which checks ran on your machine.
Point your agent at [AGENTS.md](AGENTS.md) (Claude Code reaches it through `CLAUDE.md`), this file and [docs/pr-review-kit/](docs/pr-review-kit/) before it writes code. [docs/agents/](docs/agents/) holds what it needs to run the game unattended, measure, and debug: the method, the bug classes of a recompiled Switch game, and a symptom catalog. The rules in them bind the agent as they bind you:
- Key values stay out of prompts, logs, commands and files. The agent passes the keys file path to the tools.
- Results in the pull request come from runs that happened. A check the agent did not run is listed as not run.
- The agent writes the description in its own words about this diff. A review kit checklist pasted in with every box ticked tells the reviewer nothing.
## Code and docs
- Code, comments, commit messages and documentation are in English.
- Generated code is never edited by hand: change the generator.
- Comments say what the code does and why. Cite our code by file and function, upstream code by project, file and function.
+7 -5
View File
@@ -10,10 +10,10 @@ Code copied or adapted from another project is listed here with its source, the
| MacNativeGames TotK host runtime: dynamic linking, identity check, module permissions, guest memory and SVCs, threads, IPC, audio renderer, system services, RomFS and save storage, boot sequence, the NVN proc name list | https://github.com/ly05010419/MacNativeGames @ `b75be8cfa704df3701e86130c8ced268e79d6b11` (`games/totk/runtime/`, `games/totk/scripts/prepare_runtime.py`), Copyright (c) 2026 MacNativeGames contributors | MIT | `runtime/loader.c`, `runtime/memory.c`; `runtime/kernel.c`, `runtime/svc.c`, `runtime/ipc.c`, `runtime/ipc.h`, `runtime/audren.c`, `runtime/audren.h`, `runtime/audio_mixer.c`, `runtime/audio_effects.c`, `runtime/audio_effects.h`, `runtime/audio_reverb.c`, `runtime/audio_reverb.h` (SDL3 output replaces CoreAudio), `runtime/ov_system.c` (also `shared_font.c`: the BFTTF layout behind nn::pl), `runtime/ov_input.c` (also `hid_feedback.c`: vibration and six-axis handles and calls), `runtime/motion_fusion.c` (six-axis fusion of `motion_fusion.c`), `runtime/ov_media.c` (hardware Opus decoder over libopus), `runtime/swkbd_win.c` (`swkbd_host.c`: ShowKeyboardArg offsets, UTF-8/UTF-16 conversion, disabled-key filter, result codes; Win32 window replaces the macOS text bar), `runtime/ov_gfx.c`, `runtime/romfs.c`, `runtime/romfs_path.h`, `runtime/fs_host.c`, `runtime/fs.h`, `runtime/boot.c`; `runtime/gfx_window.c`; `tools/gen_nvn_procs.py`, `runtime/nvn_procs.inc` (`NVN/names.txt` and the `NVN_EXTRA` names of `totk_nvn_ext.c`) |
| MacNativeGames TotK frame-rate patches: 60 fps present-interval controller, real-time frame step and its present sampling, hair and cloth clock, trampoline slow-motion check (instruction words and 1.2.1 sites), player position chain; quick-menu capture background RomFS overlay | https://github.com/ly05010419/MacNativeGames @ `ebe621de99bfc48f3c42f2d2aa73e04fbcc6eae2` (`games/totk/runtime/game_patches.c`, `game_version.h`, `world_survey.c`, `games/totk/scripts/make_romfs_patches.py`), Copyright (c) 2026 MacNativeGames contributors | MIT | `runtime/frame_rate.c`, `recomp/patches-v121.txt`, `tools/make_romfs_patches.py` |
| MacNativeGames TotK 1.4.3 fade fix: the four distance / LOD and pre-actor fade sites, their instruction words and the floor of the fade step at 1 | https://github.com/ly05010419/MacNativeGames @ `3e9e7f67fdfdd3404134dcfa0f24969a9425a33f` (`games/totk/runtime/game_patches_v143.c` patch_fade_increment), Copyright (c) 2026 MacNativeGames contributors | MIT | `recomp/patches-v143.txt` (set fade; the floor without flags, clz >> 5, as in pull request #78) |
| MacNativeGames TotK 1.2.1 cursor and aim steps: the map cursor, vertical Ultrahand aim and vertical gyro aim sites, their instruction words and the step scaled by the frame ratio | https://github.com/ly05010419/MacNativeGames @ `363a6eb50434387f7d623349934bcf1e910be48f` (`games/totk/runtime/game_patches.c` patch_cursor_speed), Copyright (c) 2026 MacNativeGames contributors | MIT | `recomp/patches-v121.txt` (set cursor) |
| MacNativeGames TotK aspect ratio: the camera and viewport sites that hand the perspective projection its aspect ratio, and the UI layout edits for a canvas wider than 16:9 | https://github.com/ly05010419/MacNativeGames @ `933b6a0d1438de160208a0a13eb5e8d7a2cb8094` (`games/totk/runtime/aspect_patch.h`, `aspect_math.h`, `game_patches_v143.c`, `games/totk/scripts/ultrawide_layout.py`), Copyright (c) 2026 MacNativeGames contributors | MIT | `recomp/patches-v143.txt` (set aspect), `tools/ultrawide_layout.py`, `tools/make_romfs_patches.py` (--aspect) |
| MacNativeGames TotK 1.2.1 cursor and aim steps: the map cursor, vertical Ultrahand aim and vertical gyro aim sites, their instruction words and the step scaled by the frame ratio | https://github.com/ly05010419/MacNativeGames @ `363a6eb50434387f7d623349934bcf1e910be48f` (`games/totk/runtime/game_patches.c` patch_cursor_speed), Copyright (c) 2026 MacNativeGames contributors | MIT | `recomp/patches-v121.txt`, `recomp/patches-v110.txt` (set cursor) |
| MacNativeGames TotK aspect ratio: the camera and viewport sites that hand the perspective projection its aspect ratio, and the UI layout edits for a canvas wider than 16:9 | https://github.com/ly05010419/MacNativeGames @ `933b6a0d1438de160208a0a13eb5e8d7a2cb8094` (`games/totk/runtime/aspect_patch.h`, `aspect_math.h`, `game_patches_v143.c`, `games/totk/scripts/ultrawide_layout.py`), Copyright (c) 2026 MacNativeGames contributors | MIT | `recomp/patches-v110.txt`, `recomp/patches-v143.txt` (set aspect), `tools/ultrawide_layout.py`, `tools/make_romfs_patches.py` (--aspect) |
| MacNativeGames TotK button icons: the Xbox controller's shoulder buttons, triggers, Menu, View and d-pad drawn with letters from the game's own text fonts, the .bfotf key, the button guide's images and their block-linear layout, the resource size table raised for the archives that grew | https://github.com/ly05010419/MacNativeGames @ `933b6a0d1438de160208a0a13eb5e8d7a2cb8094` (`games/totk/scripts/make_button_icons.py`), Copyright (c) 2026 MacNativeGames contributors | MIT | `tools/button_icons.py`, `tools/make_romfs_patches.py` (--prompts) |
| RenoDX TotK mod (game mod by souperman9, tonemapper by Musa, RenoDX framework by ShortFuse, RenoVK fork by Ritsu): the approach of the HDR tonemap (the game's 1 - e^-x curve kept below a pivot and continued along its tangent above it, the saturation grade applied to the colour scaled into 0..1) and the observation that the passes after the tonemap keep values above 1. No code copied; the shader is our own | https://github.com/clshortfuse/renodx @ `b6f87272ad1bfdc0e6444910a93606815944a085` (`src/games/totk/tonemap.slang`, `addon.cpp`), Copyright (c) 2025 Carlos Lopez Jr. | MIT | `runtime/shaders/hdr_tonemap.frag`, `runtime/vk_hdr.c` |
| RenoDX TotK mod (game mod by souperman9, tonemapper by Musa, RenoDX framework by ShortFuse, RenoVK fork by Ritsu): the HDR tonemap's colour reconstruction (the game's 1 - e^-x curve continued along its tangent above 0.18, the SDR result as hue and purity donor through MacLeod-Boynton in BT.2020, the saturation grade on a Neutwo-compressed colour, per-channel Neutwo to the peak in BT.2020, the sRGB to gamma 2.2 correction) adapted into GLSL, and the observation that the passes after the tonemap keep values above 1 | https://github.com/clshortfuse/renodx @ `43c067b755a6a741f6500b2b936570f1462dcfbe` (`src/games/totk/tonemap.slang`, `src/games/totk/macleod_boynton.hlsli`, `src/shaders/colorcorrect.hlsl`, `src/shaders/tonemap/neutwo.hlsl`), Copyright (c) 2025 Carlos Lopez Jr. | MIT | `runtime/shaders/hdr_tonemap.frag`, `runtime/vk_hdr.c` |
| TOTK-Ultrawide, the community mod for emulators: the list of UI panes the ultrawide layout edits and the distances they move (data only, no code copied; the edit code is our own, and MacNativeGames' `ultrawide_layout.py` in the row above already carries the same values). The mod's repository URL and licence version are not recorded in this repository | TOTK-Ultrawide (community mod; URL not recorded here) | GPL (version not recorded here) | `tools/ultrawide_layout.py` (`KEEP_ROOT`, `edits`, `anim_edits`) |
| MacNativeGames TotK virtual-core preemption: asynchronous preemption of an owner in top-level game code, priority inheritance through guest mutexes, the SDK's preemption-disable protocol (TLR+0x100/+0x102, SVC 0x36), the core-wait diagnostics | https://github.com/ly05010419/MacNativeGames @ `33e8fa83052ea1ae09a3583137008e9797e12950` (`games/totk/runtime/threads.c`, commits 8bf0893 and 19730cd), Copyright (c) 2026 MacNativeGames contributors | MIT | `runtime/kernel.c` (SuspendThread and a register-saving stub replace SIGUSR2), `runtime/hostfn.c`, `runtime/svc.c` (host depth) |
| MacNativeGames RomFS index builder (`TOTKRFS1` layout) | https://github.com/ly05010419/MacNativeGames @ `b75be8cfa704df3701e86130c8ced268e79d6b11` (`games/totk/scripts/prepare_romfs_backend.py`), Copyright (c) 2026 MacNativeGames contributors | MIT | `tools/romfs_index.py` |
@@ -27,10 +27,11 @@ Code copied or adapted from another project is listed here with its source, the
| deko3d block-linear image layout, as ported in MacNativeGames' `nvn_tex.c` (Apple parts dropped, layout math unchanged; licence text in `games/totk/third_party/d2r_gfx/LICENSE.deko3d`, reproduced in the file header) | https://github.com/devkitPro/deko3d (`source/dk_image.cpp`), via MacNativeGames @ `b75be8cfa704df3701e86130c8ced268e79d6b11` (`games/totk/third_party/d2r_gfx/gfx/nvn/nvn_tex.c`, `nvn_tex.h`), Copyright (C) 2018-2020 fincs | zlib | `runtime/nvn_tex.c`, `runtime/nvn_tex.h` |
| MacNativeGames texture auto-layout goldens (40 original-SDK size and block-height cases) | https://github.com/ly05010419/MacNativeGames @ `b75be8cfa704df3701e86130c8ced268e79d6b11` (`games/totk/tests/texture_auto_layout/probe.c`), Copyright (c) 2026 jhaizhou | MIT | `runtime/nvn_tex_probe.c`; `runtime/nvn_probe.c` (four of the cases, through the procs) |
| MacNativeGames block-linear deswizzler (address function; copy loop split out as `tex_bl_copy`) | https://github.com/ly05010419/MacNativeGames @ `b75be8cfa704df3701e86130c8ced268e79d6b11` (`games/totk/third_party/d2r_gfx/gfx/tex/nvn_deswizzle.c`), Copyright (c) 2026 jhaizhou | MIT | `runtime/tex_deswizzle.c`, `runtime/tex.h` |
| SDL 3.4.18, dynamic link, `SDL3.dll` copied next to `totk.exe`. Developer builds use the MSYS2 build (`mingw-w64-ucrt-x86_64-sdl3`, which imports `libiconv-2.dll`, LGPL-2.1, unmodified, copied beside it); the installer release ships the upstream release `SDL3-3.4.18-win32-x64.zip`, which imports no libiconv | https://libsdl.org, Copyright (c) 1997-2026 Sam Lantinga | zlib | `runtime/input_win.c` (gamepad input, rumble, gyro and accelerometer) |
| SDL 3.4.20, dynamic link, `SDL3.dll` copied next to `totk.exe`. Developer builds use the MSYS2 build (`mingw-w64-ucrt-x86_64-sdl3`, which imports `libiconv-2.dll`, LGPL-2.1, unmodified, copied beside it); the installer release ships the upstream release `SDL3-3.4.20-win32-x64.zip`, which imports no libiconv | https://libsdl.org, Copyright (c) 1997-2026 Sam Lantinga | zlib | `runtime/input_win.c` (gamepad input, rumble, gyro and accelerometer) |
| libopus (MSYS2 `mingw-w64-ucrt-x86_64-opus`), dynamic link, `libopus-0.dll` copied next to `totk.exe` | https://opus-codec.org, Copyright (c) 2001-2023 Xiph.Org, Skype Limited, Octasic, Jean-Marc Valin, Timothy B. Terriberry, CSIRO, Gregory Maxwell, Mark Borgerding, Erik de Castro Lopo, Mozilla, Amazon | BSD-3-Clause | `runtime/ov_media.c` (nn::codec hardware Opus decoder) |
| Dear ImGui 1.91.9 (core and Vulkan back end, unmodified), as vendored by LAS Recomp | https://github.com/ocornut/imgui, via https://github.com/peiikai/LASRecomp @ `da35b83a9611d046a9a681aae668d7085954d37f` (`third_party/imgui/`), Copyright (c) 2014-2025 Omar Cornut | MIT | `runtime/third_party/imgui/` (compiled into `totk.exe` for the in-game menu, `runtime/ui/`) |
| SavageWindRecomp settings menu: two-pane option rows (row look, left / right stepping, hold speed-up, description pane), the menu style and tab bar | https://github.com/Revivedx/SavageWindRecomp @ `575ad88e02f9a89a9a885d90039d48af33c982f4` (`runtime/ui/option_rows.mm`, `runtime/ui/menu.mm`), Copyright (c) 2026 Revivedx and the Savage Wind Recomp contributors, Copyright (c) 2026 Peiikai | MIT | `runtime/ui/option_rows.cpp`, `runtime/ui/option_rows.h`, `runtime/ui/menu.cpp` |
| SavageWindRecomp descriptor sets allocated in growing batches per layout | https://github.com/Revivedx/SavageWindRecomp @ `740b18025fe11ba03c117dbb56668db304b5043e` (`runtime/nvn/vulkan/exec.c` vk_alloc_set), Copyright (c) 2026 Revivedx and the Savage Wind Recomp contributors, Copyright (c) 2026 Peiikai | MIT | `runtime/vk_exec_draw.c` (alloc_set_batched) |
| SavageWindRecomp performance overlay: the frame ring and its one-second summary, the NVML calls, the sampling loop, the full panel's layout | https://github.com/Revivedx/SavageWindRecomp @ `575ad88e02f9a89a9a885d90039d48af33c982f4` (`runtime/perfmon.c`, `runtime/perfmon.h`, `perf_overlay_draw` in `runtime/ui/menu.mm`), Copyright (c) 2026 Revivedx and the Savage Wind Recomp contributors, Copyright (c) 2026 Peiikai | MIT | `runtime/perfmon.c`, `runtime/perfmon.h`, `runtime/ui/menu.cpp` |
| Roboto Medium font (Dear ImGui's `misc/fonts/Roboto-Medium.ttf`, unmodified) | https://fonts.google.com/specimen/Roboto, via https://github.com/peiikai/LASRecomp @ `da35b83a9611d046a9a681aae668d7085954d37f` (`third_party/imgui/misc/fonts/`), Copyright 2011 Google Inc. | Apache-2.0 | `runtime/ui/fonts/Roboto-Medium.ttf` (licence text in `runtime/ui/fonts/LICENSE-Roboto.txt`), embedded by `runtime/ui/menu.cpp` |
| Vulkan Memory Allocator 3.4.0 (MSYS2 `mingw-w64-ucrt-x86_64-vulkan-memory-allocator`), header-only, compiled into `totk.exe` | https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator, Copyright (c) 2017-2026 Advanced Micro Devices, Inc. | MIT | `runtime/vk_vma.cpp` (the allocator behind `runtime/vk_memory.c`) |
@@ -41,6 +42,7 @@ Code copied or adapted from another project is listed here with its source, the
| AMD Display Library (ADL) declarations: `AdapterInfo`, `ADLSingleSensorData`, `ADLPMLogDataOutput`, the `ADL_PMLOG_*` sensor numbers and the `ADL2_*` function signatures | https://github.com/GPUOpen-LibrariesAndSDKs/display-library @ `84c881cb2136f04287eb3af12de00a9e2867aa9e` (`include/adl_structures.h`, `include/adl_defines.h`, `include/adl_sdk.h`), Copyright (c) 2016 - 2022 Advanced Micro Devices, Inc. | MIT | `runtime/perfmon.c` |
| Zstandard 1.5.7 (MSYS2 `mingw-w64-ucrt-x86_64-zstd`), static link | https://github.com/facebook/zstd, Copyright (c) Meta Platforms, Inc. and affiliates | BSD-3-Clause | `runtime/resolution.c` (the HeapDef table), `runtime/ov_zstd.c` (the game's zstd frames decoded on the host) |
| musl `fma.c`, `fmaf.c` and `scalbn.c`, adapted into one file for the x86-64-v2 build | https://github.com/kraj/musl @ `5a56ad13e1e0790c06122978aad2a14d6b9efc1a` (mirror of https://git.musl-libc.org/cgit/musl), Copyright (c) 2005-2020 Rich Felker and contributors | MIT | `runtime/fma.c` (the correctly rounded fma/fmaf the FP lowering needs on CPUs without FMA3) |
| MacNativeGames texture pool offset fix: widen the 32-bit texture storage and packaged-data offsets unsigned | https://github.com/ly05010419/MacNativeGames @ `ca311e2d` (`games/totk/runtime/game_patches.c` patch_pool_offset), Copyright (c) 2026 MacNativeGames contributors | MIT | `recomp/patches-v110.txt` (set pooloff; sites located again in 1.1.0) |
## Compiler runtime in a source build
@@ -73,7 +75,7 @@ These tools run on the developer's or player's machine during the build. They ar
| CPython 3.14.9 embeddable package, with `tkinter` and Tcl/Tk 9.0.4 taken from the python.org 3.14.9 installer's component MSI (unpacked, never run); includes `vcruntime140*.dll` (Microsoft, redistributable with Python), OpenSSL, libffi, SQLite, zlib, libtommath | https://www.python.org/downloads/release/python-3149/ | PSF-2.0 (Tcl/Tk: Tcl/Tk licence, BSD-style) | `python/` |
| numpy 2.5.4 (with its bundled OpenBLAS), lz4 4.4.5, cryptography 50.0.2 (with its bundled OpenSSL), fontTools 4.68.0, cffi 2.1.1, pycparser 3.11 | PyPI binary wheels, pins in `tools/requirements-installer.txt` | BSD-3-Clause; lz4 BSD-3-Clause; cryptography Apache-2.0 OR BSD-3-Clause; fontTools MIT; cffi MIT; pycparser BSD-3-Clause | `python/Lib/site-packages/` |
| llvm-mingw 20261020 (LLVM/Clang/LLD 23, mingw-w64, libc++, libunwind, compiler-rt), x86_64 subset | https://github.com/mstorsjo/llvm-mingw/releases/tag/20261020 | Apache-2.0 with LLVM exception; mingw-w64 ZPL-2.1/BSD/public domain; libc++ and libunwind Apache-2.0 with LLVM exception and MIT/UIUC; winpthreads MIT | `llvm/` |
| SDL 3.4.18 upstream Windows release (`SDL3.dll`) and mingw development package (headers, `libSDL3.dll.a`) | https://github.com/libsdl-org/SDL/releases/tag/release-3.4.18 | zlib | `bin/SDL3.dll`, `runtime/deps/` |
| SDL 3.4.20 upstream Windows release (`SDL3.dll`) and mingw development package (headers, `libSDL3.dll.a`) | https://github.com/libsdl-org/SDL/releases/tag/release-3.4.20 | zlib | `bin/SDL3.dll`, `runtime/deps/` |
| libopus 1.6.1 (MSYS2 `mingw-w64-ucrt-x86_64-opus` 1.6.1-2; `libopus-0.dll` imports only KERNEL32 and the UCRT api-ms-win-crt DLLs), headers and import library | https://repo.msys2.org/mingw/ucrt64/ | BSD-3-Clause | `bin/libopus-0.dll`, `runtime/deps/` |
| libvpx 1.17.0 (MSYS2 `mingw-w64-ucrt-x86_64-libvpx` 1.17.0-1), headers and static library compiled into `totk.exe`; `libvpx-LICENSE.txt` and `libvpx-PATENTS.txt` come from the v1.17.0 source tag | https://github.com/webmproject/libvpx | BSD-3-Clause with the WebM patent grant | `runtime/deps/`, `licenses/` |
| Zstandard 1.5.7 (MSYS2 `mingw-w64-ucrt-x86_64-zstd` 1.5.7-2), headers and static library compiled into `totk.exe`; `zstd-LICENSE.txt` from the package | https://github.com/facebook/zstd | BSD-3-Clause | `runtime/deps/`, `licenses/` |
+1 -1
View File
@@ -1 +1 @@
0.3.12
0.4.1
+117
View File
@@ -0,0 +1,117 @@
# Debugging the port: method for agents
A game run costs minutes and a full rebuild costs several more, while reading code costs seconds. Order the work so the
cheap steps decide as much as they can before the game starts. Each step ends on its stated condition.
## Steps
1. **Evidence.** Hold one artifact that shows the symptom: the player's screenshot, log or input recording, a
flight-recorder dump, a frame dump ([running.md](running.md), "Observing"). Done when you can point at the wrong
pixel, line or frame.
2. **Where it reproduces.** OS, GPU vendor and generation, driver, game update, settings, mods, the save. Done when you
know whether your hardware can show the bug at all. NVIDIA bugs do not reproduce on AMD; RDNA1/2 bugs do not
reproduce on RDNA3/4 (another driver branch); a Ryzen 7000 iGPU is RDNA2 and runs that branch. When your hardware
cannot show it, say so and work from the code and the reporter's logs; runs that cannot decide anything are skipped.
3. **Known already?** Search [symptoms.md](symptoms.md), [pr-review-kit/04-knowledge.md](../pr-review-kit/04-knowledge.md)
and [maxwell.md](../maxwell.md) sections 6-7 with the words the player used (black, magenta, flicker, slow motion,
missing, stale, streaks, stuck) and the object (sky, grass, shadow, fog, HUD, weapon). Then the upstream notes:
`refs/nxnative/docs`, `refs/MacNativeGames/docs` and the SavageWindRecomp issue tracker (`tools/fetch_refs.sh`
fetches `refs/`). Done when every hit is either ruled out or is your first hypothesis. A hit on a class we already
fixed: confirm that fix still holds before deriving anything new.
4. **Read the code.** The game's function in Ghidra ([building.md](../building.md), "Reverse engineering") or in
`generated/<ver>/`, and our runtime path in `runtime/`. Done when the code either rules the hypothesis out or names
the exact condition a run must show. A run confirms what the code could not rule out.
5. **The cheapest falsifying check.** An existing switch (a `totk.ini` setting or a `TOTK_*` variable), a dump, a trace
line, a probe. When it refutes the hypothesis, stop: write down what was checked and why it fell, and start again
at step 3 with the next hypothesis. No build, no benchmark for a refuted idea.
6. **Repro red, then green.** The same repro shows the bug before the fix and its absence after. A fix without a
reproduced failure is a guess.
7. **Fix the cause.** A crash that goes away with a feature off points at the bug in that feature; fix the feature.
A fix that one setup needs is gated on that setup, and everyone else keeps today's behaviour.
## Traps
- **One-off failures.** A failure that happens once and not again: rerun before you debug it as a code bug. A crash or
a forced shutdown leaves zero-filled cache files, and memory errors on the host flip bits in files and objects.
- **Every build fails the same way at once.** Suspect the inputs and caches (game files, the OS file cache, shader and
pipeline caches), not the code. Bisecting code is pointless when the old builds fail too.
- **Compile errors in generated C** on a line the generator could not have written are a flipped bit. Regenerate and
compare hashes; `recomp/` is fine.
- **A crash that looks like a race.** Try the save first: some saves crash the same way on a real Switch.
- **Theories about locks and threads.** Read the game's own protocol in Ghidra before running anything. Ten runs on
lock theories once went where one read of the game's double-buffer code would have narrowed it.
- **Numbers from a fast machine.** A change neutral on a fast desktop can matter on a weak laptop or the Steam Deck,
in either direction. Say which hardware your numbers come from.
## Bug classes of a recompiled Switch game
The port runs the game's own code on another OS and another GPU. Most bugs come from places where the host differs
from the console.
### Variable frame step and epsilon-gated timers
The console steps the game by whole 30 Hz units, so a countdown started at a whole value reaches exactly 0. With the
real-time step (`realtime = 1`, the default) the step is a measured ratio, and the last step can leave a remainder of
about 1e-8. Many of the game's updaters step a timer only while it is above `FLT_EPSILON`; a remainder in
(0, `FLT_EPSILON`] is never stepped again. A consumer that tests the same field with `> 0` then sees it active
forever, until a load reinitialises the object. Issue #18 (weapons locked after a Fuse) was this.
Symptom words: stuck until a reload, a teleport or a shrine; locked after an action; works sometimes; only above
30 fps. To check a report: find the field the stuck behaviour reads, then compare the updater's threshold with the
consumer's test. A change to the frame step, the pacer or the fps limit is checked for this class.
### Thread scheduling: Horizon is not a desktop OS
- Horizon runs a woken better-priority thread at once on its core, before the thread that woke it goes on. A host
runs both in parallel, so state can be read one step early or freed while another thread still uses it.
- Threads created with ideal core -2 start pinned to one core on Horizon. The game keys per-core data by
`GetCurrentProcessorNumber` without locks and expects that pinning.
- The game has hundreds of spin loops without a system call. On the console a spinning thread holds one core of three;
on the host it burns a host core and can keep an equal-priority waiter out.
- ARMv8 `stlr` followed by `ldar` is ordered (RCsc). C11 release/acquire lets x86 reorder that store and load. A
two-thread flag handshake that loses a wake points here; lifter changes to barriers and atomics pass
`tests\diff\atomic.py` and `tests\excl_mt.py`.
- A scheduling change is judged by how long a ready, better thread waits for a core it may use, plus the slow-frame
count. A check that made one bug rarer once added 50-75 ms hitches.
### Maxwell behaviour versus Vulkan defaults
The port must do what the Switch GPU does: last-vertex provoking, (0,0,0,1) for unwritten vertex outputs, colour
targets outside the shader's output map keep their texels, the G-buffer pass samples its own targets. The full list
with sources: [maxwell.md](../maxwell.md), section 7.
### Memory shared by the CPU and the GPU
- The game fills textures and buffers with the CPU and samples them on the GPU without the flush a desktop API needs.
Missing uploads show as magenta, brown or stale textures.
- The game reads GPU-rendered textures with the CPU (grass maps, cut and burn masks). Missing write-backs show as
effects that never happen: cut grass stays up, fire does not spread.
- Memory only the GPU writes looks idle to write-watch tracking: a store from a shader leaves no fault and no dirty bit.
- Anything kept per texture, per frame or per object without a free grows host memory until a commit failure after
hours. Watch private memory over a few idle minutes.
### Device lost and GPU hangs
- The `vk: pass marks` lines name the pass in flight. Validation names the misuse when there is one.
- Submits and presents share one queue under one lock. A present from its own thread on a second queue gave device
lost on NVIDIA before Turing in another port.
- Caches from another driver or GPU (pipeline caches, translated shaders) gave black windows after driver updates;
every cache is validated on load and dropped when vendor, device or `pipelineCacheUUID` change.
- A first-time shader stall over 2 s on a slow GPU trips the Windows GPU timeout (display event 4101). Large one-shot
submissions (a boot-time decode of every texture) do the same.
- Point lists from vertex shaders that write no point size are undefined without `VK_KHR_maintenance5`.
### Windows and Wine host costs
What the hot path must not do, with measured costs: [pr-review-kit/02-performance.md](../pr-review-kit/02-performance.md),
"What the hot path must not do". For debugging, also:
- `SuspendThread` returns at once, but `GetThreadContext` waits until the thread leaves the kernel. A sampler or a
debugger stalls for as long as the stopped thread's file open or driver call runs.
- A thread stopped between its `sub rsp` and its stack probe has its stack pointer inside the guard page. Check
`PAGE_GUARD` with `VirtualQuery` before reading another thread's stack.
- Paths go through the W APIs with UTF-8 to UTF-16 conversion. An A API call breaks on a path with non-Latin letters,
such as a Windows user name in Cyrillic or Chinese.
- Wine emulates `GetWriteWatch` with `mprotect` and one fault per first write. Anything relying on cheap OS memory
tricks (write-watch, protection changes, fault handlers) costs more under Proton.
- `win32u.dll` frames in a host stack are kernel graphics calls: memory allocation or submission.
+90
View File
@@ -0,0 +1,90 @@
# Running and observing the game as an agent
How an agent runs a build into gameplay without a person at the controls, captures what the game shows, and reads the
results without flooding its context. Setting names and file formats live in [settings.md](../settings.md) and
[controls.md](../controls.md); this page says which to use when. How to judge a performance change:
[pr-review-kit/02-performance.md](../pr-review-kit/02-performance.md).
## Before the first run
- **A build dir per change.** Build into its own folder (`tools/build_game.py --out build/<ver>-<name>`) so a
comparison has two exes side by side. After every build check the exe's modification time: a failed link leaves the
old `totk.exe` in place and you test old code.
- **Prepared shaders in that folder.** `tools/prepare_shaders.py build/<dir> --version <ver>` ([building.md](../building.md)
step 6). Without it the game compiles shaders during play, and every timing and every "a draw is missing" shot of the
first minutes says nothing about your change. Shader and pipeline caches are not copied between build folders.
- **The same `totk.ini` on both sides** of any comparison. A folder without one runs at the defaults; check the log's
`resolution:` line of each run before comparing them.
- **Saves.** `tools/run_game.py` copies the profile `build/profile`, swaps in a save from `build/save-backup/<name>/`
and deletes the copy afterwards, so the real profile is never written. To make a backup, copy the user folder from
`build/profile/save/` into `build/save-backup/<name>/`. Pick a save that starts where the change acts.
## Running
```powershell
.venv\Scripts\python tools\run_game.py build\<dir> --save <name> --seconds 30 [--script <input.txt>] [--shot out.png] [--env KEY=VALUE] [--quiet] [--tag <name>]
```
- `--seconds` is game time since the world loaded (the log line `frame step: world clock starts` marks the start), at
any frame rate. Measure and script by game time; a frame count at another fps is another scene.
- `--script` replays input ([controls.md](../controls.md), "Scripted input"). A new script starts with the presses of
`tools/input/load_save.txt`, which load the first save from the title. `TOTK_INPUT_RECORD` turns a person's play into
a script; ask the user to record the repro rather than guessing the button sequence.
- `--shot` saves the last frame and builds pipelines synchronously: use it for pictures, never for timing.
- `--quiet` waits until no other `totk.exe` or `ninja.exe` runs. Every timing you report comes from a `--quiet` run.
- The log goes to `<dir>\logs\<tag>.log`; the tool prints `<log>: rc=<code>`.
Run one game at a time: two instances share the GPU's memory and the CPU, and both runs' numbers are void.
Running `totk.exe` yourself: set `TOTK_ASSUME_FOCUS=1` (an unfocused window otherwise pauses the game),
`TOTK_INPUT=none` (the user's keyboard and pad stay out) and `TOTK_BACKGROUND_WINDOW=1`. Stop only the process you
started, by PID: `taskkill /IM totk.exe` also closes the user's game.
**Check that the run reached the world** before trusting it: the world clock line is in the log and the shot shows the
scene you expected. Title menus on a slow boot eat presses, and runs of one script drift apart after 10-15 game seconds
(enemy AI, physics, weather). Two runs are comparable only when shots at the same game second show the same scene.
## Observing
| You need | Use |
|---|---|
| pictures over a time span | `TOTK_SHOTS=w<lo>-<hi>:<step>` (world seconds) and `TOTK_SHOTS_DIR`; 480x270 by default |
| the frames around a hitch or a one-frame glitch | `TOTK_FLIGHT=1`: keeps the last 600 frames with timings and thumbnails, dumps on F8, on a frame over `TOTK_FLIGHT_SPIKE_MS` or at `TOTK_FLIGHT_AT=<frame>`; `python tools/flight_view.py <dump dir>` lists the worst frames and writes a contact sheet |
| every render pass of one frame | `TOTK_DUMP=<frame>` and `TOTK_DUMP_DIR`: a PNG per pass target plus `passes.txt` (draws, bound textures with swizzles, storage buffers, copies, clears) and `images.txt`; `TOTK_DUMP_BUF` adds a guest buffer |
| Vulkan misuse | `TOTK_VK_VALIDATION=1` (needs the Vulkan SDK), run into the world; compare the message kinds with a run without your change. The layer caps repeats, so counts are not totals |
| which pass the GPU was in at a device lost | the `vk: pass marks` lines; on by default on AMD, `TOTK_PASS_CRUMBS=1` elsewhere |
| a crash | the crash report at the end of the log: exception, guest and host addresses, registers |
| a run on a large card that acts like a small one | `TOTK_VRAM_LIMIT_MIB=<MiB>` |
Frame dumps: a draw's argument lines come before its pass header. Pass numbers shift between runs, so find a pass by
its draw count or its targets, not by its number. RenderDoc cannot capture this renderer ([controls.md](../controls.md),
"RenderDoc capture").
## Measuring
| What | How |
|---|---|
| fps, worst frame, render-thread waits, GPU busy time, VRAM, `bound: gpu\|cpu\|pace` | `--env TOTK_TRACE=perf,sched`, then `python tools\perf_summary.py <log> <lo> <hi>` |
| slow frames | count the `perf: slow frame` lines; each names what the render thread was blocked on (pipeline, translation, texture, fence, pacing) |
| the frame's critical path, by thread and state | `TOTK_CRIT=w<lo>-<hi>` and `TOTK_CRIT_OUT=<absolute path>` with `sched` tracing, then `python tools\crit_path.py <file>` |
| where CPU time goes | `TOTK_PROFILE=<path>` and `TOTK_PROFILE_FRAMES=w<lo>-<hi>`, then `python tools\prof_report.py <profile> <totk.map>`; `TOTK_PROFILE_RENDER=1` samples the render thread with call chains |
| a switch on a static scene | a setting set to `ab` with `TOTK_AB=<seconds>` alternates the two sides in blocks of game time; `python tools\ab_summary.py <log> --skip <seconds>`. On a moving scene compare whole runs instead |
A single run proves little: runs of one script on one build spread by a third in their 1% lows. Compare medians of at
least two whole runs per side, on a quiet machine.
## Probes
Each probe is a mode of `totk.exe`, run from the build folder; the list with what each covers is at the top of
`runtime/main.c`. `--selftest` needs no game files. `--probe-kernel`, `--probe-vkexec`, `--probe-nvn-tex` and
`--probe-shader` cover most runtime changes and run in seconds: run them before any game run.
## Reading output without filling the context
- Game logs run to megabytes. `grep` the lines you need, `tail` the end, pipe `perf_summary.py` through `grep SUMMARY`.
- Generated C runs to hundreds of thousands of lines per file. `grep -n` the function, then print its line range.
- Look at screenshots downscaled: paste several into one 1280x720 grid and read the grid once. Write down what it shows
instead of opening it again.
- Send long builds and runs to the background with their output in a file; read the last lines when they end.
- Keep a notes file in the build folder from the first finding: what is ruled out, the leading hypothesis, the next
step, temporary debug edits with file and line. A fresh session continues from it.
+73
View File
@@ -0,0 +1,73 @@
# Symptoms and their usual causes
What a player reports, the causes this port and other Switch ports (nxnative, MacNativeGames, SavageWindRecomp,
LAS Recomp) found behind it, and the first check. Search by the player's words and by the object. Which of these are
already fixed here, and where: [pr-review-kit/04-knowledge.md](../pr-review-kit/04-knowledge.md), "Fixed bug classes".
GPU semantics behind the graphics rows: [maxwell.md](../maxwell.md). The method around this table:
[debugging.md](debugging.md).
## Graphics
| Symptom | Usual causes | First check |
|---|---|---|
| magenta, brown or stale textures; caves or sky wrong | the CPU filled the texture and the GPU never got the upload (no flush on the console) | frame dump: the texture's upload count and mirror state in `passes.txt` |
| effects that never happen: cut grass stays up, fire does not spread | the CPU reads a GPU-rendered texture that was never written back | which texture the game reads after its fence; write-back of that target |
| black scene, black dialog background or loading map | deferred binds read at record time; a 3D target rejected; a texture compare ignoring the channel mapping | frame dump: the first pass whose target goes black |
| lime silhouettes, rectangles in the sky | a target outside the shader's output map overwritten instead of kept | dump: the target's previous contents versus after the pass |
| dark or magenta 16x16 blocks, missing characters | a pass samples its own render targets (G-buffer feedback) | feedback-loop layout and barriers on that pass |
| tile rectangles, wrong data in tiles | storage buffers bound in the wrong slot order | `passes.txt`: storage buffers with their c0 slots |
| streaks on cliffs, characters stretched into streaks | primitive restart ignored; integer vertex inputs against a float format | validation run; the draw's topology and vertex formats in the dump |
| garbage geometry on title or load screens | 8-bit indices widened on the CPU before a copy in the same submission wrote them | whether a copy to the index buffer precedes the draw in one submission |
| black tiles, masks not cleared | clears with a partial channel or stencil mask skipped or done in full | `passes.txt` clears and their masks |
| garbage in feedback effects at first use | new GPU-only images start undefined; the console starts them at zero | clear on creation and compare |
| empty clouds or fog for the whole session | a one-shot draw skipped while its pipeline built, never drawn again | `perf: slow frame` lines naming pipeline waits; the draw's first frame |
| fog that never parts, flicker in tile rows | driver constants (c0: dispatch size, point size, base instance) not filled | the c0 contents the translator expects for that stage |
| no sun shadows, wrong lighting | unwritten vertex outputs read as zero instead of (0,0,0,1) | the shader's output map |
| one flat ambient colour | a 1-layer 2D array sampled as 2D | the view type the shader expects |
| upside-down text | lower-left window origin | origin mode in the log |
| flicker or a texture swapped with a similar one | the newest texture picked at shared storage instead of the one with the matching view | texture lookup by id in the dump |
## Timing and frame pacing
| Symptom | Usual causes | First check |
|---|---|---|
| stuck until a reload or teleport, locked after an action | a timer remainder below `FLT_EPSILON` with the real-time step ([debugging.md](debugging.md)) | `realtime = 0` in `totk.ini`; if the bug is gone, find the timer field |
| slow motion in GPU-heavy scenes, dynamic resolution never drops | the engine's GPU time comes from report timestamps; wrong timestamps make the frame look free | what the engine reads as GPU time and what the report writes |
| frame counter wanders 50-60 | two 60 Hz clocks (vsync event and present acquire) drifting apart | whether both follow one display tick |
| a 30 Hz thread runs at 27 Hz, slow cutscenes | host sleep overshoot: Windows waitable timers wake 0.3-0.5 ms late | `TOTK_TRACE=sched` sleep asked versus took |
| 20 fps after a scheduler change | a yield back-off that starts too early | yield handling against the console's |
| climbing or movement wrong only above some fps | game code that assumes a fixed step count per frame | compare at 30, 60 and the failing rate |
## Threads and crashes
| Symptom | Usual causes | First check |
|---|---|---|
| state read one step early, one frame of inconsistent data | a woken better-priority thread does not run before the waker goes on | sched trace around the handoff |
| per-core data corrupted, rare crash in a container walk | threads the console pins to one core run on any host core | the thread's core mask from its creator; a run on one core |
| rare hang or lost wake | address-arbiter signal values or wake order differ from Horizon | the arbiter path against mesosphere |
| a flag handshake loses a wake | `stlr`/`ldar` ordering lost in x86 release/acquire | the guest code's barrier pattern |
| a host core at 100% while the game waits | game spin loops without system calls | profile: which guest function spins |
| crash on world load | sizes the game reads from the driver (command memory used) differ from the console's | the sizes the game asks for against the console's |
| crash with one save only | the save itself; some crash on a real Switch | the same save on another build and, if possible, on hardware |
| random crash after minutes to hours, access violation reading `0xffffffffffffffff` or a small address such as `0x90` in a game function (Windows reports any non-canonical address as `0xffffffffffffffff`) | an invalid value (-1, `0xffff0000ffffffff`) in an optional reference the game checks only for null; in main+0x1bd0138 (Recall / Ultrahand picking a target) also a reused holder whose actor points into other memory. Sets `optref` and `ability` in `recomp/patches-v143.txt` guard both there | the crash dump's faulting host instruction, the register it loads through and the field that register came from; `ability: target dropped` lines in the log |
| crash while using Ultrahand or aiming, in the model scene's report loop (1.4.3 main+0xa22bc..0xa234c) | a report row index read from the report buffer with no bound (Weeping of the Empire #52, #73); set `report` sends a negative handle or an index from 0x8000 to the game's own reset | the faulting address against the report rows base (x12) |
## Device lost, hangs, black window
| Symptom | Usual causes | First check |
|---|---|---|
| device lost a few frames in, GPU writes to address 0 (older NVIDIA) | present from another thread or queue | every submit and present on one queue under one lock |
| device lost or garbage on one AMD generation | views the driver branch mishandles (BC block views of 3D textures) | `vk: pass marks`, validation; the formats in the pass |
| device lost on one scene, every run, one vendor | a shader or pass that driver cannot run | pass marks name the pass; skip shaders by hash to bisect |
| GPU timeout after 1-2 min, single frames over 2 s | first-time shader stalls or a huge one-shot submission on a slow GPU | prepared shaders; submission sizes at boot |
| GPU hang at boot with pipeline libraries | point lists without a point size | the shader writes point size or the device clamps it |
| black window after a driver update | caches from another driver | the log line `vk_pipeline: ... written by another device or driver`; delete the caches next to `totk.exe` |
## Memory
| Symptom | Usual causes | First check |
|---|---|---|
| host memory grows in play until a commit failure after hours | state kept per texture init or per frame and never freed | private memory over a few idle minutes |
| multi-GB peaks while travelling | upload staging grows inside one submission | staging bytes per submission |
| out of memory after minutes | dead textures re-entering tables | live images and allocator bytes over time |
| stutter as VRAM fills | new memory blocks allocated on the render thread (8-23 ms each) | profile the render thread over the hitch: `vkAllocateMemory` under the allocator's lock |
+6
View File
@@ -40,6 +40,10 @@ MacNativeGames cannot run on x86-64 as is: its CPU path executes ARM64 code nati
(`recomp/flags.py`, `recomp/regs.py`): registers are stored back where they changed, before calls, SVCs and
exits, and reloaded after them. A call reloads only X0-X18 and X30, as AAPCS64 has the callee preserve X19-X29 and
SP; `gen.py --no-callee-saved-trust` reloads those too.
- `gen.py --v-locals K` keeps the K most used V registers of each function in a local `u128 vr[32]` that clang
holds in XMM registers (`recomp/vlocals.py`, the `RT_VLOCALS` accessors of `runtime/simd.h`): loaded at entry,
stored where they may have changed before calls and exits, reloaded after calls; `--v-trust` does not reload
V8-V15, whose low halves AAPCS64 has the callee preserve. K is 8 by default; K = 0 keeps every V register in the `Cpu`.
- Coverage oracle: MacNativeGames' NSO rewriter lists every site where game code touches the OS (SVCs, TLS reads, counter reads, `x18`, exclusive windows, jump tables): 68,968 sites in 1.4.3 `main`. The lifter must handle every one.
- Memory: guest address equals host address, all below 2^39 (IPC encodes 39-bit addresses). The heap, stacks and the alias region are reserved with `MEM_WRITE_WATCH`, so the graphics back end can ask which pages the CPU wrote since the last upload (`runtime/memory.c`).
- Indirect branches go through `rt_dispatch` (`runtime/dispatch.c`), which looks the target up in the address table and tail-calls it.
@@ -85,6 +89,8 @@ Every guest thread is a host thread. TotK assumes Horizon's scheduler: three app
- Vulkan 1.3 back end in place of MacNativeGames' Metal one. At `QueueSubmitCommands` the recorded NVN commands are replayed into Vulkan command buffers (`vk_exec*.c`).
- Memory pools. Apple's unified memory let MacNativeGames use the game's pools zero-copy. The back end imports the pools as host memory (`VK_EXT_external_memory_host`). On a discrete GPU, pools bound as buffers are mirrored in device-local memory and the pages the CPU wrote (write watch, above) are uploaded before a replay; idle pool regions are made read-only so the write-watch queries skip them (`vk_memory.c`). GPU-rendered textures are written back when the CPU reads them: on a pool invalidate, on a CPU copy, and for small targets once the CPU waits for a fence (`vk_texture.c`).
- Replay thread. `TOTK_REPLAY_ASYNC=1` moves the replay off the submitting thread onto a queue at most one frame behind the game; reads of GPU results wait for the newest writer instead of draining the queue (`vk_backend.c`). It is off by default. Where the game waits for the GPU it overlaps the replay with the game threads (a GTX 1650 in the Depths, issue #88), a gain measured only there; while it was the default, a GTX 1660 Ti at the 60 fps cap with the GPU at 26 % reported uneven motion (#659). It adds up to a frame of latency; `replay_async = 1` turns it on.
- Chunked replay. A large submission is recorded in chunks by several threads (`TOTK_REPLAY_WORKERS`, 0 by default: the sequential replay; 4 is a good value from 8 logical processors) and the chunks go to the queue in order (`vk_exec.c`). A sequential scan keeps a copy of the queue state where each chunk starts and lists what a chunk would otherwise learn from the chunks before it: inline uniform updates, conditions, contact shadow marches, uniform drops. It also makes render targets live in replay order and notes the op, since a chunk can sample a storage before the chunk that renders it got there or after the scan went past a later target there: a chunk's texture slots resolve as of its own op, and the chunks after a texture with no entry yet wait for the chunk that makes it. DLSS's decisions (the G-buffer, the light target, the end of the scene, the scaling pass) are the scan's too, and its copy of the lit scene and its evaluation are records of the chunk that holds the op; a chunk's draws are jittered while they come before the scan's end of the scene. The evaluation waits until the chunks before it are recorded, as it reads what their draws noted (the camera block, the jitter, the per-object motion list, which chunks add to under one lock), and the chunks after it wait for it, as they sample its output. `TOTK_REPLAY_STATE_CHECK=1` compares the state after the last chunk with the scan's.
- Frame ahead. `TOTK_SYNC_AHEAD=1` (`sync_ahead`, off by default) makes the one `nvnSyncWait` per frame that waits for the frame presented last wait for the frame before it, so the game builds the next frame while the GPU still runs the previous one (`nvn_api_core.c`, `nvnSyncWait`). The game then writes memory that in-flight work still reads. This is safe only with shadow storage (`vk_memory.c`, the CPU-visible pools are host-visible copies of guest memory, on by default on Linux and Wine, `shadow = 1` (`TOTK_SHADOW=1`) on native Windows; without it the switch is ignored): a CPU write reaches the GPU only through the shadow copy made before each submit (`vk_mem_shadow_sync`), and a copy that would overwrite bytes an unfinished submission reads is queued as a GPU copy in the prologue command buffer of the part submitted next (`vk_mem_shadow_prologue`, `TOTK_SHADOW_DEFER=1`, the default with the feature) or, with `=0`, waited for on the replay thread (`shadow_reader_wait`). Only that one wait lags (identified by its call site, known for 1.2.1 and 1.4.3); results the game reads back from the GPU after it can be a frame old.
- Recorder thread. The Vulkan calls the replay makes into its command buffer are made by a thread of their own (`vk_rec.c`, after Savage Wind Recomp and LAS Recomp): each `vkCmd*` copies its arguments into a packet of a ring and the recorder makes the calls in order; calls into other command buffers are made at once; the recorder also ends the command buffer, and the submit waits until it has. In the town on an RX 9070 the replay takes 17% less time on the submitting thread. It is off by default on AMD GPUs, where the driver loses the device or hangs the GPU at startup with it (`TOTK_VK_RECORDER=1` forces it on, `=0` makes every call on the submitting thread).
- Uniform snapshot. The game rewrites per-object uniform blocks on other threads soon after it submits the frame
that reads them, while the replay still records it. At the start of a submission's replay every uniform range it
+13 -27
View File
@@ -100,7 +100,7 @@ of them.
objects before it links: any AVX, BMI or other instruction above x86-64-v2 outside a function listed there as
dispatched by a runtime CPU check stops the build.
`gen.py` sizes its emit pool from the CPU count and the free memory; `--jobs N` sets it, and so does the environment variable `TOTK_LIFT_JOBS=N`, which the installer's lift step honours too (it has no `--jobs`). When a worker runs out of memory, the pool restarts with half the workers (`emit: out of memory with N workers, retrying with M`), down to one; only an out-of-memory error with one worker fails the step.
`gen.py` sizes its emit pool from the CPU count and the free memory; `--jobs N` sets it, and so does the environment variable `TOTK_LIFT_JOBS=N`, which the installer's lift step honours too (it has no `--jobs`). When a worker runs out of memory, the pool restarts with half the workers (`emit: out of memory with N workers, retrying with M`), down to one; only an out-of-memory error with one worker fails the step. A later lift into the same `--out` lifts again only the chunks whose inputs changed (`<mod>/lift_cache.json`, see the top of `recomp/gen.py`); `--no-cache`, or `TOTK_LIFT_CACHE=0` (which the installer's lift step honours too), lifts every chunk.
Lifting takes about 15 s, a clean compile about 200 s on 24 threads; later builds recompile only what changed.
A compiler cache in front of clang shortens it further: with `SCCACHE` set to an sccache executable, `build_game.py`
@@ -160,30 +160,9 @@ folder. The folder holds code and data derived from your game: for your own mach
### Profile-guided build
`build_game.py --pgo <file>.profdata` compiles the generated code at -O2 with an execution profile, and the runtime
with it too: hot paths laid out straight, hot calls inlined, code that never ran compiled for size. On 1.4.3 the fight
bench ran 12-15% faster per frame and a fight outside the training about 6%. The installer builds with
`data/pgo-<ver>.profdata` of the release when the update has one, else at -O1.
A profile only fits the generated C it was trained on: functions are matched by name and control-flow hash, so a
function whose C changes loses its counts without a warning. Retrain after a lifter or generator change, from a clean
`recomp/`. The instrumented build needs llvm-mingw, as MSYS2's clang has no profile runtime:
```powershell
$llvm = "toolchain\llvm-mingw-<date>\llvm-mingw-<date>-ucrt-x86_64"
.venv\Scripts\python tools\build_game.py --version v143 --out build\v143-pgogen --toolchain $llvm --pgo gen
```
Play `build\v143-pgogen\totk.exe` through varied scenes (fights, towns, riding, building, the Depths, menus) for 20-30
minutes; it is slower than a normal build. It writes `build\v143-pgogen\pgo\*.profraw` every minute and at exit.
Scripted runs from saves (`tools/run_game.py`) add to the same file. Then:
```powershell
.venv\Scripts\python tools\pgo.py merge build\v143-pgogen --version v143
```
writes `build\pgo\pgo-v143.profdata` and its record (`.json`: the `recomp/` tree it fits). `make_release.py` ships every
profile of `build\pgo` in `data\` and warns when a record names another `recomp/` tree than the one released.
`build_game.py --pgo <file>.profdata` compiles the generated code and the runtime at -O2 with an execution
profile, 6-12% shorter frames. Which build uses which profile, when to retrain, where and how to train and how to
measure a new profile: [pgo.md](pgo.md).
## Linux (in progress)
@@ -221,7 +200,7 @@ re-protecting imported memory invalidates the driver's userptr buffer on every s
amdgpu), and importing pins the pages, which drops their write protection. Each CPU-visible pool therefore gets a
host-visible copy (shadow storage, `runtime/vk_memory.c`): the pages the CPU wrote go in before each submit, and the GPU's
writes come back at its fence. In play that costs about 0.2 ms a frame and doubles the memory of those pools (about
1.1 GB). `TOTK_SHADOW=1` turns it on under Windows too. The Steam Deck's RADV has `VK_EXT_external_memory_host` as
1.1 GB). `shadow = 1` in `totk.ini` (or `TOTK_SHADOW=1`) turns it on under Windows too. The Steam Deck's RADV has `VK_EXT_external_memory_host` as
well: the shadow copies are chosen for the measured cost above, not for a missing extension.
The Deck's GPU (Van Gogh) has no native ASTC, so its textures go through the compute decoder (`runtime/vk_astc.c`,
@@ -241,7 +220,14 @@ files); a change to the settings passes `totk.exe --probe-cfg` and `python tests
when a setting is described neither in `totk.ini.example` nor in [settings.md](settings.md). A change to the mods' ExeFS patches
(`tools/exefs_patches.py`, `runtime/mods.c`, the `folded.txt` of `recomp/gen.py`) passes
`.venv\Scripts\python tests\exefs_patches_test.py`, which makes its own modules and runs `gen.py` on one with a jump
table (no game files), and `totk.exe --probe-mods`.
table (no game files), and `totk.exe --probe-mods`. A change to the cheat VM or the cheat opcode tables
(`runtime/cheats.c`, `tools/cheats.py`) passes `.venv\Scripts\python tests\cheats_vm_check.py`, which runs both on
cheats of its own with clang from PATH (`--cc` names another). A change to what a generated chunk depends on, or to
the lift cache of `recomp/gen.py` (`<mod>/lift_cache.json`), passes `.venv\Scripts\python tests\lift_cache_test.py`,
which lifts a module it makes itself with and without the cache and compares the output (no game files); a new input
of the chunk text goes into `chunk_key` with a case there. On game files, a lift with the cache into a used output
folder and a `--no-cache` lift into an empty one give the same files apart from `manifest.json` and
`lift_cache.json`.
The lifter (`recomp/`) and the CPU headers (`runtime/cpu.h`, `runtime/simd.h`) are checked against Unicorn. Every lifter
change passes these, all run with `.venv`:
+2 -2
View File
@@ -58,7 +58,7 @@ The game's code is compiled ahead of time, so a cheat cannot write new instructi
|---|---|---|
| code | Opcode 0 writes into main's code | Built into the game at setup as a patch set (`recomp/patches.py`); switches at once. |
| key | Code writes inside a button condition (`8` mask, writes, `21` else, the original words, `20`) | A patch set that is on while the cheat is on and the buttons are held. |
| data | Writes data only: heap, main's data, pointer chains, button conditions | Runs in a cheat VM after dmnt's, opcodes 0 to A, every presented frame while on. |
| data | Writes data only: heap, main's data, pointer chains, button conditions | Runs in a cheat VM after dmnt's, opcodes 0 to A and C0 to C3, every presented frame while on. |
| master | `{name}` with code writes | A patch set that is always on. |
| restore | Writes back only the game's original instructions | Not needed: switching a cheat off restores them. Greyed out. |
@@ -66,7 +66,7 @@ A cheat whose code writes call or jump into a code cave the cheat writes itself
Not supported, and shown greyed out with the reason:
- the 0xC and 0xF opcode extensions;
- opcode C4 (extended button condition) and the 0xF extensions (FF0 pause, FF1 resume, FFF debug log);
- opcode 5 with a register offset;
- code writes mixed with other opcodes, except the button pattern above;
- code writes that change the CPU flags differently from the game's instruction, or code caves with branches or pc-relative instructions.
+38 -12
View File
@@ -14,7 +14,7 @@ The log names the layout in effect (`input: gamepad layout ...`); the menu shows
|---|---|---|---|
| Left stick | left stick | W A S D | move |
| Right stick | right stick | mouse; arrow keys as a second source | camera |
| Y | west | left mouse button | attack |
| Y | west | left mouse button, T | attack |
| ZL | left trigger (over 50%) | right mouse button, Z | shield, lock on |
| ZR | right trigger (over 50%) | F, X | bow |
| X | north | Space | jump |
@@ -29,7 +29,7 @@ The log names the layout in effect (`input: gamepad layout ...`); the menu shows
| Minus | Back | M | map |
| Right stick shaken left and right | shake the right stick | G | detach a fused part from what Ultrahand holds |
Esc is unbound in the game; it opens the settings menu and releases the mouse cursor (see Mouse camera). Z and X duplicate ZL and ZR so menu pages turn with two neighbouring keys; the menu tabs (L, R) are Q and R. The mouse wheel zooms the Purah Pad's map and is unbound elsewhere. The bindings are the tables of `input_map` in `runtime/input_map.c`: the keys (platform-neutral key ids) and one gamepad table per layout (gamepad inputs by position); `runtime/input_win.c` translates Win32 virtual keys and SDL buttons.
Esc is unbound in the game; it opens the settings menu and releases the mouse cursor (see Mouse camera). Z and X duplicate ZL and ZR so menu pages turn with two neighbouring keys; the menu tabs (L, R) are Q and R. T duplicates Y for the game's menus, where the left mouse button is A while the pointer is shown (sort the inventory with T). The mouse wheel zooms the Purah Pad's map and its camera, steps the game's menus and the d-pad strips (see Mouse in the game's menus), and is unbound elsewhere. The bindings are the tables of `input_map` in `runtime/input_map.c`: the keys (platform-neutral key ids) and one gamepad table per layout (gamepad inputs by position); `runtime/input_win.c` translates Win32 virtual keys and SDL buttons.
## Xbox button prompts
@@ -46,7 +46,7 @@ Esc is unbound in the game; it opens the settings menu and releases the mouse cu
| Plus, Minus | Menu, View | Menu, View |
| D-pad | cross | cross |
The action prompt (`DoCommand_00`) shows its button in a diamond whose shape gives the face buttons' positions as on a Switch, with the other three letters around it. With `nintendo` its letters are swapped with the rest and every one names the button in that place. With `xbox` and `auto` the letters are right but the diamond keeps the Switch positions: A shows on its right, while the button labelled A is at the bottom. The sticks keep the Switch icons, and so do prompts drawn as fixed images (some tutorial animations).
The action prompt (`DoCommand_00`) shows its button in a diamond whose shape gives the face buttons' positions as on a Switch, with the other three letters around it. With `nintendo` its letters are swapped with the rest and every one names the button in that place. With `xbox` and `auto` the letters stay and the diamond is laid out as an Xbox pad: A at the bottom, B on the right, X on the left and Y at the top, the button the prompt names lit in its place, its bar reaching left from X and right from the others (`tools/make_romfs_patches.py`, `bflyt_xbox_diamond`: the panes of `blyt/DoCommand_00.bflyt` take their partner's place, A's of B and X's of Y). The sticks keep the Switch icons, and so do prompts drawn as fixed images (some tutorial animations).
The prompts are part of the game's fonts, textures and messages, so they follow the layout and not the bindings. A button remapped with a `bind_` line or on the Controls tab keeps the prompt the game draws for it, which then names the wrong button. Use the Xbox prompts with the default bindings.
@@ -55,7 +55,9 @@ The game reads its UI archive and fonts once at boot, so the prompts change at t
- every prompt but the button guide in play is text in the button font, `scft/External.bfotf` in each `Font/Font*.Nin_NX_NVN.bfarc.zs`: glyphs U+E040 to U+E043 (A B X Y), U+E044 to U+E047 (L R ZL ZR), U+E04B and U+E04C (Plus, Minus), U+E049 and U+E060 to U+E065 (the d-pad). fontTools writes the font again, so the archives grow and the folder also carries the resource size table (`System/Resource/ResourceSizeTable.Product.<n>...rsizetable.zs`) with their entries raised by as much; without that the game stops at start.
- the button guide in play draws L and ZL from the images `Nt_KeyTexL_00` and `Nt_KeyTexZL_00` of the Common archive's `timg/__Combined.bntx`, painted with the same icons. The action prompt draws its button from `Nt_KeyTexA/B/X/Y_00`, whose image data is swapped for `nintendo`, and the diamond's letters are text the game takes from `LayoutMsg/DoCommand_00.msbt` of each language's `Mals` archive (and the layout's own text panes behind it), swapped the same way.
The Switch prompts stay, with a note under *Button prompts* and a `RomFS overlay: ...` line in the log, when the folder is missing (an install made by an older setup, or one where the setup could not prepare it and showed a warning; running the setup again on the install folder writes it), when it fails its check, with a game resolution of another shape than 16:9 (its own UI layout wins; 16:10 and 21:9), and when the `romfs_mods` folder replaces one of its files: `romfs-patches` then loads in its place, where two overlays replacing one file would stop the game. In a source build, `.venv-py314\Scripts\python tools\make_romfs_patches.py --version <ver> --prompts xbox` (and `--prompts xbox-label`) writes `build\<ver>\romfs-patches-xbox` (`-label`); it needs fontTools from `tools/requirements-py314.txt`. `--xbox-prompts` is the older name of `--prompts xbox`.
The Switch prompts stay, with a note under *Button prompts* and a `RomFS overlay: ...` line in the log, when the folder is missing (an install made by an older setup, or one where the setup could not prepare it and showed a warning; running the setup again on the install folder writes it), when it fails its check, with a game resolution of another shape than 16:9 that has no folder of its own (below), and when the `romfs_mods` folder replaces one of its files: `romfs-patches` then loads in its place, where two overlays replacing one file would stop the game. In a source build, `.venv-py314\Scripts\python tools\make_romfs_patches.py --version <ver> --prompts xbox` (and `--prompts xbox-label`) writes `build\<ver>\romfs-patches-xbox` (`-label`); it needs fontTools from `tools/requirements-py314.txt`. `--xbox-prompts` is the older name of `--prompts xbox`.
At a game resolution of another shape than 16:9 the game loads `romfs-patches-xbox-<w>x<h>` (or `romfs-patches-xbox-label-<w>x<h>`) in place of `romfs-patches-<w>x<h>`: the same prompts with that shape's UI layout, `make_romfs_patches.py --prompts xbox --aspect <w>x<h> --reuse <install>\romfs-patches-xbox --out <install>\romfs-patches-xbox-<w>x<h>`. The setup writes both for each of the fifteen sizes it lays the UI out for, as for the PC prompts below, so on a display of one of those shapes the game resolution follows the display with these prompts too. At another size, without that folder, the Switch prompts stay (*off at this game resolution*).
## PC button prompts
@@ -63,17 +65,18 @@ The Switch prompts stay, with a note under *Button prompts* and a `RomFS overlay
| Switch | Prompt |
|---|---|
| A, B, X, Y | E, Shift, Space, left click |
| A, B, X, Y | E, Shift, Space, left click (T in text prompts) |
| L, R, ZL, ZR | Q, R, right click, F |
| Plus, Minus | Tab, M |
| D-pad | 1 (left), 2 (up), 3 (right), 4 (down), as a cross or the keys of the directions meant |
| Left stick, its press | W A S D (or the keys of the directions meant), Ctrl |
| Right stick, its press | the mouse, with an arrow for each direction meant, C |
- The glyphs keep the width of the game's own, so the right-aligned guides do not run into their text. A key is a rounded square with its label cut out in the game's own text fonts; a mouse button is a mouse with that button filled and the others hollow.
- The glyphs keep the width of the game's own, so the right-aligned guides do not run into their text. A key is a rounded square with its label cut out in the game's own text fonts (Shift: an arrow over its name); a mouse button is a mouse with that button filled and the others hollow.
- The button guide in play draws every button from images of the Common archive (`Nt_KeyTexA/B/X/Y/L/ZL_00`, `Nt_KeyTexLSt_00` and `_01`, the stick tilted and pressed): all of them are painted with the same icons. The letters of the action prompt's diamond are one character of the Normal font each: E, an arrow for Shift, a bar for Space, L and R for the mouse buttons.
- Like the Xbox prompts they follow the defaults and not the bindings. `--binds <totk.ini>` draws the first key of each `bind_` line of that file instead, for a folder made by hand.
- At a game resolution of another shape than 16:9 the game loads `romfs-patches-pc-<w>x<h>` in place of `romfs-patches-<w>x<h>` when it is there: the same prompts with that shape's UI layout, `make_romfs_patches.py --prompts pc --aspect <w>x<h> --out <install>\romfs-patches-pc-<w>x<h>`. The setup does not write those, so without one the Switch prompts stay (*off at a game resolution not 16:9*).
- Y has two keys, the left mouse button and T. The button guide in play and the action prompt show the left button, which attacks there; the prompts in text (the menus' guides such as *Sort*, the dialogue log's) show T, because while a menu has the pointer the left button is that menu's A, and T presses Y everywhere (`PC_TEXT_KEYS` in `tools/button_icons.py`). With a `bind_y` line, the text prompts take its first key that is not a mouse button (its only key when all are mouse buttons).
- They follow the bindings of the install's `totk.ini` as the setup found them: its *Drawing the keyboard and mouse prompts* step passes the file as `--binds` (the first key of each `bind_` line), and the keyboard defaults without one. After a change on the Controls tab run the setup again: that step alone runs, since it is keyed by the `bind_` lines (`PcPrompts` in `installer/steps.py`). The Xbox prompts follow the defaults.
- At a game resolution of another shape than 16:9 the game loads `romfs-patches-pc-<w>x<h>` in place of `romfs-patches-<w>x<h>`: the same prompts with that shape's UI layout, `make_romfs_patches.py --prompts pc --aspect <w>x<h> --reuse <install>\romfs-patches-pc --out <install>\romfs-patches-pc-<w>x<h>`. The setup writes one for each of the fifteen sizes it lays the UI out for, so on a display of one of those shapes the game resolution follows the display with these prompts too. At another size, without that folder, the Switch prompts stay (*off at this game resolution*).
## Window keys
@@ -91,12 +94,12 @@ The *Controls* tab of the settings menu lists, under the button layout, every Sw
The tab writes `totk.ini` lines, one per Switch button or stick direction: `bind_<button> = <entry>, <entry>, ...`. The button is `A B X Y L R ZL ZR PLUS MINUS LEFT UP RIGHT DOWN LCLICK RCLICK`, or a stick direction `LSTICK_LEFT LSTICK_UP LSTICK_RIGHT LSTICK_DOWN` (movement, W A S D by default) and `RSTICK_...` (camera, the arrow keys), or `SHAKE` (keys only, G by default). An entry is `pad:<name>` or `key:<name>`:
- pad names: `south east west north l r zl zr start back lstick rstick dpad_left dpad_up dpad_right dpad_down lpaddle1 rpaddle1 lpaddle2 rpaddle2` (`zl`, `zr` are the triggers; the paddles are the back buttons of an Xbox Elite, DualSense Edge or Steam Controller, upper pair 1, lower pair 2, and no Switch button has them by default)
- pad names: `south east west north l r zl zr start back lstick rstick dpad_left dpad_up dpad_right dpad_down lpaddle1 rpaddle1 lpaddle2 rpaddle2 touchpad` (`zl`, `zr` are the triggers; the paddles are the back buttons of an Xbox Elite, DualSense Edge or Steam Controller, upper pair 1, lower pair 2; `touchpad` is the touchpad click of a DualSense or DualShock 4; no Switch button has the paddles or the touchpad by default)
- key names: `a` to `z`, `0` to `9`, `f2` to `f7`, `f9`, `f12`, `num0` to `num9`, `space tab enter backspace lshift rshift lctrl rctrl lalt capslock insert delete home end pageup pagedown up down left right`, `num_add num_sub num_mul num_div num_decimal`, `minus equals lbracket rbracket semicolon apostrophe comma period slash backslash grave`, `mouse_l mouse_r mouse_m mouse_x1 mouse_x2`
- `key:default` and `pad:default` keep that part's defaults (`pad:default` is each layout's own button).
- `none` leaves the button unbound.
A line replaces every default of that button, pad and keyboard, so list the keys you still want or use `key:default`. Pad names are physical positions, so a line means the same in both layouts and replaces the layout's own button for that Switch button. Stick directions take keys only: the controller's sticks and the mouse camera always work. Buttons without a line keep the table above. Names are case-insensitive. An invalid entry logs one `input: bind ...` line and is skipped; a line without a valid entry changes nothing. The `TOTK_BIND_<button>` environment variables work like any other setting. A changed line applies at once (`runtime/input_win.c` builds the tables again at its next poll).
A line replaces every default of that button, pad and keyboard, so list the keys you still want or use `key:default`. Pad names are physical positions, so a line means the same in both layouts and replaces the layout's own button for that Switch button. Stick directions take keys only: the controller's sticks and the mouse camera always work. Buttons without a line keep the table above, less any key a line binds: with `bind_X = key:t`, T presses X only, not also Y. Names are case-insensitive. An invalid entry logs one `input: bind ...` line and is skipped; a line without a valid entry changes nothing. The `TOTK_BIND_<button>` environment variables work like any other setting. A changed line applies at once (`runtime/input_win.c` builds the tables again at its next poll).
A pad that reports its face buttons swapped (A/B and X/Y, as some GuliKit models do) is fixed with:
@@ -172,7 +175,8 @@ The horizontal quick lists (D-pad up/left/right: materials, shields and weapons)
with at least 32 ms of neutral between them. At sensitivity 1, 32 horizontal counts request a step; a large flick
cannot queue continued scrolling after the mouse stops. Vertical movement is ignored in these lists. Switching or
releasing a selector resets its cursor/step state without sending the last movement to the camera. Selector input is
not consumed by the direct camera or Ultrahand hooks.
not consumed by the direct camera or Ultrahand hooks. The scroll wheel steps these lists too: each notch is one step,
away from you to the left, towards you to the right.
To detach a fused part from what Ultrahand holds, shake the mouse left and right, or press G (`bind_SHAKE`). A press
swings the right stick left and right for 0.8 s, holding the key keeps it swinging. With `mouse_mode = direct` a shake
of the mouse while Ultrahand holds an object swings the right stick with it: once the swings (10 degrees of camera turn
@@ -197,10 +201,30 @@ The stick direction bits (`StickLLeft` to `StickRDown`) are derived from the sti
## Mouse in the game's menus
On game 1.4.3, while one of the game's menus has its focus box (the frame the d-pad moves: the title, the save list, the inventory and its System tab, Options, shops, dialogue choices), the cursor is released from the mouse camera and shown in the middle of the window. A focus gap of up to 300 ms (a tab switch, a popup opening: the game drops the focus box for a frame or two) keeps the pointer where it is instead of capturing and centring it again. The item under the pointer takes the focus as if the d-pad had moved there, with the game's highlight, sound and description; a left click is A on it, a right click is B. The keys and the pad keep working, and the focus moves only when the pointer moves. This happens only while the keyboard and mouse are the device used last: after a gamepad button press or a stick pushed past half way the menus are as on the console, with no pointer, until the mouse moves or a key is pressed (`input_kbm_last` in `runtime/input.c`). The ability wheel, the d-pad strips, Autobuild and the Purah Pad's camera stay gameplay: the mouse keeps the camera there. The map has no focus box; it is found in `eui::ScreenMgr`'s list of open screens (every 100 ms: the Purah Pad's frame `AppMenuOverlay_00` with `AppMap_00` and the menu background `AppMenuBG_00`), and there the mouse moves the map's cursor as the left stick does, with the cursor still captured, and the scroll wheel zooms: each notch is a full-tilt push of the right stick (the map's zoom) for 100 ms with 50 ms of neutral after it, up (in) for a turn away from you; a fast scroll's notches wait their turn (at most four), a turn the other way drops them. The Scope opens the same map screen without the menu background and keeps the mouse camera. When the menu closes the cursor is captured again.
On game 1.4.3, while one of the game's menus has its focus box (the frame the d-pad moves: the title, the save list, the inventory and its System tab, Options, shops, dialogue choices), the cursor is released from the mouse camera and shown in the middle of the window. A focus gap of up to 300 ms (a tab switch, a popup opening: the game drops the focus box for a frame or two) keeps the pointer where it is instead of capturing and centring it again. The item under the pointer takes the focus as if the d-pad had moved there, with the game's highlight, sound and description; a left click is A on it, a right click is B. The middle and side mouse buttons keep their bindings (bound to L and R they turn the tabs), and T presses Y (sort). The scroll wheel moves the focus up and down, one step a notch (a full-tilt push of the left stick for 70 ms with 50 ms of neutral after it, up for a turn away from you), so a long list of items scrolls without moving the pointer; `menu_cursor = 0` turns this off with the pointer. The keys and the pad keep working, and the focus moves with the pointer only when the pointer moves: after the wheel, a key or the pad moved the focus off the pointer's item, the pointer takes it back once it has moved 6 layout units (the menus' 1280 x 720: about 9 window pixels at 1080p, 18 at 4K) from where it was then (`DEAD_ZONE` in `runtime/menu_cursor.c`), so a hand resting on the mouse does not undo a scroll; a click acts at once. With `pointer_image = 1` (off by default) the game window shows a pointer of its own while the cursor is not captured, a gold arrowhead drawn at the window's DPI (`runtime/cursor_art.c`), in place of the system arrow. This happens only while the keyboard and mouse are the device used last: after a gamepad button press or a stick pushed past half way the menus are as on the console, with no pointer, until the mouse moves or a key is pressed (`input_kbm_last` in `runtime/input.c`). The ability wheel, the d-pad strips, Autobuild and the Purah Pad's camera stay gameplay: the mouse keeps the camera there. The map has no focus box; it is found in `eui::ScreenMgr`'s list of open screens (every 100 ms: the Purah Pad's frame `AppMenuOverlay_00` with `AppMap_00` and the menu background `AppMenuBG_00`), and there the mouse moves the map's cursor as the left stick does, with the cursor still captured, and the scroll wheel zooms: each notch is a full-tilt push of the right stick (the map's zoom) for 100 ms with 50 ms of neutral after it, up (in) for a turn away from you; a fast scroll's notches wait their turn (at most four), a turn the other way drops them. The Scope opens the same map screen without the menu background and keeps the mouse camera. The Purah Pad's camera (the ability wheel's camera) opens the frame and its pointer `CameraPointer_00` without `AppMap_00` (the Scope has it); there the scroll wheel zooms as the camera's guide shows: each notch is a press of the d-pad's up (in, for a turn away from you) or down for 100 ms with 50 ms of release after it (`menu_cursor_camera`). The Scope itself has no zoom; the wheel does nothing there. When the menu closes the cursor is captured again.
`runtime/menu_cursor.c` reads eui's screens at each pad read and moves the focus with the writes `eui::BoxCursorMgr::moveBoxCursor` makes, so no game code is called. `menu_cursor = 0` (`TOTK_MENU_CURSOR=0`, live; the *Mouse in the game's menus* row of the Controls tab) turns it off; `TOTK_MENU_CURSOR_LOG=1` logs every screen it reads (by default the first six), with the layout size, the node count and the hit panes it uses.
### Left click on the map
With `map_click = 1` (off by default, so each key presses one controller button as everywhere else), on game 1.4.3, while the Purah Pad's map is open (found as above; the Scope is not the map), the left mouse button presses A at the map's cursor instead of the button it is bound to (Y by default). A press is A or the binding from the moment the button goes down until it is released, so a button held as the map opens or closes keeps what it pressed (`poll_merged` in `runtime/input.c`). `map_click = 1` (`TOTK_MAP_CLICK=1`, live; the *Left click on the map* row of the Controls tab) turns it on and `0`, the default, leaves the left button on its binding there. The map is found only while `menu_cursor` is on.
### Clicks in dialogue
With `dialogue_click = 1` (off by default, so each key presses one controller button as everywhere else), on game 1.4.3, while a dialogue box is open (the layout `MessageDialog_00`: a character's lines and the game's own messages), the left mouse button presses B and the right one A, instead of the buttons they are bound to. B continues the text as A does and also shows the rest of a line that is still scrolling in; the left button's default, Y, would open the dialogue log there, and the log stays in the way until B closes it. Dialogue choices are a menu with the focus box, so the pointer above takes their clicks and a left click is A on the choice under it: clicking through a conversation fast does not pick the choice the focus starts on. A press is the dialogue's button or the binding from the moment the button goes down until it is released: the click that closes the last box does not become an attack while it is still held, and a button held when the box opens does not skip its first line. Between two lines of a conversation the game closes the box and opens it again, so both buttons stay B and A for 500 ms after the box was last open (`DIALOGUE_GAP_MS`); the first clicks after a conversation fall in that time too, as they do for a player who taps the button through the last line. `TOTK_MENU_CURSOR_LOG=1` logs how long each such gap was. The dialogue window has no focus box of its own: the scan of `eui::ScreenMgr`'s open screens (every 100 ms) finds its screen, and from then on that screen's state is read at every pad read (`dialogue_shown` in `runtime/menu_cursor.c`), so the first click after a box opens is already the dialogue's. `dialogue_click = 1` (`TOTK_DIALOGUE_CLICK=1`, live; the *Clicks in dialogue* row of the Controls tab) turns it on and `0`, the default, leaves both buttons on their bindings; the setting does not depend on `menu_cursor`. The log names the first box it sees (`menu_cursor: a dialogue box is open ...`).
## Gameplay tweaks
With `gameplay_tweaks_enabled = 1` (`TOTK_GAMEPLAY_TWEAKS_ENABLED=1`, live; off by default in `totk.ini`), the mouse wheel and keyboard can drive common gameplay interactions directly:
- **Ultrahand object rotation**: turning the mouse wheel while Ultrahand holds an object rotates it directly without holding R or pressing the D-pad (`gameplay_ultrahand_rotate = 1`, sensitivity `gameplay_rotation_sensitivity = 1`). Holding C while scrolling rotates around the alternate axis (`gameplay_ultrahand_c_axis = 1`).
- **Ultrahand distance**: holding the right mouse button while scrolling pushes or pulls the held object closer or farther (`gameplay_ultrahand_distance = 1`, sensitivity `gameplay_distance_sensitivity = 1`).
- **Free-roam weapon switching**: scrolling the mouse wheel in free roam steps through equipped weapons (`gameplay_weapon_wheel = 1`, sensitivity `gameplay_weapon_sensitivity = 1`). Press E to confirm the chosen weapon; right click or pressing B cancels.
- **Quick-selection menus**: scrolling the mouse wheel browses items while a quick-selection menu (weapons, bows, shields, materials) is open (`gameplay_item_wheel = 1`, sensitivity `gameplay_item_sensitivity = 1`). A and D keys step left and right through the strip (`gameplay_item_ad = 1`).
- **Smooth map zoom**: the mouse wheel performs continuous inertial zooming on the Purah Pad map (`gameplay_map_zoom = 1`, sensitivity `gameplay_map_zoom_sensitivity = 1`).
All sub-toggles default to 1 and sensitivities to 1 (range 0.25 to 4.0), so enabling `gameplay_tweaks_enabled` activates the full suite immediately while allowing individual features to be toggled or tuned.
## Nintendo Switch 2 controllers
Windows does not present the Switch 2 Pro Controller as a gamepad SDL can open (the log says `input: no gamepad`), so it needs a bridge program that wakes it and creates a virtual gamepad. The DS4Windows fork by hbashton ([github.com/hbashton/DS4Windows](https://github.com/hbashton/DS4Windows), tested with release VIIPERRC4.5.1, a pre-release) with the output set to an emulated DualSense gives buttons, rumble, gyro and accelerometer, and reads the sticks through the controller's calibration (they rest at 0 to 2%). The game reads its virtual DualSense directly; the log then shows `input: gamepad "DualSense Wireless Controller" (rumble, gyro and accelerometer, ...)` and `input: motion sensors on`. Its release notes also list Joy-Con 2 support, which is untested here.
@@ -218,6 +242,8 @@ Bridges that output a ViGEm DualShock 4 (ProCon2Windows, connectyourpro as teste
@<ms> <button> [hold_ms] # timed on the world clock
@<ms> LSTICK|RSTICK <x> <y> [hold_ms]
@<ms> MOUSE <dx> <dy>
<frame> WHEEL <notches> # scroll wheel notches, once; + away from the player
@<ms> WHEEL <notches>
```
`<frame>` counts 60 Hz ticks of game time since the first frame (presented frames at 60 fps), so the title menus get the presses at the same game time at any frame limit. `@<ms>` counts milliseconds of the world clock: game time stepped since the end of the first save load (the player leaves the title screen's origin), so a timed event lands at the same game time at any frame rate and load length. A later load, such as fast travel, still shifts what follows. In a script with timed lines the untimed ones stop when the world clock starts, as the load ends at another tick in each run. Each event logs once (`input: script frame N tick T: A`). `tools/input/load_save.txt` loads the first save from the title screen.
+33 -11
View File
@@ -72,29 +72,47 @@ loads the saves 1.4.3 wrote.
`totk.exe --prepare-shaders`) in the install folder, then fold the shader cache's loose files (four per shader, about
117,000) into `shader-cache/shader-cache.pack` (`tools/pack_shader_cache.py`). totk.exe reads the pack first and
writes the translations it makes in play as loose files; running the tool again with the game closed folds those in.
The patches are written four times: `romfs-patches`, `romfs-patches-xbox` and `romfs-patches-xbox-label`
The patches are written three times: `romfs-patches`, `romfs-patches-xbox` and `romfs-patches-xbox-label`
(`--prompts`: Xbox button prompts for `prompts = xbox`, the first for `pad_layout = nintendo`, the second for `xbox`
and `auto`), and `romfs-patches-pc` (keyboard and mouse prompts for `prompts = pc`). The three prompt folders are
optional: when one cannot be made, the setup warns and finishes.
and `auto`). The prompt folders are optional: when one cannot be made, the setup warns and finishes.
**PC prompts** (step `pc_prompts`): `romfs-patches-pc`, the keyboard and mouse prompts for `prompts = pc`, drawn
from the `bind_` lines of `totk.ini` (`--binds`). Its key holds those lines, so a setup run after a change of the
bindings draws only these again. Optional like the Xbox prompts. All four 16:9 folders are always written, so a
switch of the prompts style at 16:9 needs no setup run.
When the graphics driver crashes while building the shaders, `prepare_shaders.py` runs that part again without the
variants it was building ([building.md](building.md), step 6), and the setup finishes with a warning that names how
many were left out.
7. **DLSS** (the window's tick box, on by default, or `--dlss`; Windows with an NVIDIA RTX GPU): download
7. **Shapes**: the UI laid out for each screen shape this player needs, `romfs-patches-<w>x<h>`
(`--aspect`), and with Xbox or PC prompts in `totk.ini` that layout with those prompts too,
`romfs-patches-<style>-<w>x<h>` (`--prompts <style> --aspect --reuse romfs-patches-<style>`, which hard-links every
file but the UI archive; for `pc` with the same `--binds` as the PC prompts step, whose key the step follows). The shapes are those of every connected display (Windows: `EnumDisplayDevices`; Linux:
`xrandr`, else the DRM connectors), of the `output_res` the game defaults to on each, and of the `totk.ini`
`output_res` (`installer/screens.py`). One folder per shape, named after the shape's size in `ASPECT_SIZES` (the
closest within 1%; `5120x2160` uses `2560x1080`); `runtime/romfs.c` `overlay_init` maps the game resolution the
same way, and a folder of the exact size from an older setup still wins. A 16:9 player gets none. The step runs
again when the displays, the prompts or the patches change; at a game resolution whose shape has no folder the
game draws 16:9 with bars, and the settings menu marks such sizes and can write the shape's folders in the
background for the next start.
8. **DLSS** (the window's tick box, on by default, or `--dlss`; Windows with an NVIDIA RTX GPU): download
`nvngx_dlss.dll` from NVIDIA's own DLSS SDK repository (github.com/NVIDIA/DLSS, a pinned tag, NVIDIA RTX SDK
license) into the install folder and check its size and SHA-256 (`steps.py`, `Dlss`). The release does not ship
the library; NGX loads it from the folder of totk.exe for `dlaa` and the DLSS modes. A DLSS library already in the
folder (a newer one the player put there) is kept. Without an RTX GPU the step only logs and does nothing; a failed
download warns, the game runs without DLSS, and the next run of the setup tries again.
8. **Clean up** the temp folder, except the generated C and the objects (about 1.3 GB), and write
9. **Clean up** the temp folder, except the generated C and the objects (about 1.3 GB), and write
`build-cache/rebuild.txt` in the install folder.
A cancelled or failed setup resumes from the last finished step (`totk-install.json`).
## Rebuilding for the mods
The generated C and the objects stay in the temp folder, so the next lift rewrites only the chunks whose C changed and
Ninja recompiles only those: a new or edited code cheat costs the lift (under a minute on 24 cores, a few minutes on 4)
and a few dozen chunks, not a full compile, and the shaders stay prepared.
The generated C and the objects stay in the temp folder. The next lift lifts again only the chunks whose inputs
changed (the lift cache of `recomp/gen.py`: the patch sites inside a chunk, with their set indices) and rewrites only
those whose C changed, and Ninja recompiles only those. A new or edited code cheat costs a few seconds of lift
(discovery and a hash of the kept chunks; 7 s for one changed cheat word on 12 cores, against 102 s for the whole lift)
and the compile of the chunks that hold its sites, not a full compile, and the shaders stay prepared. A cheat set added
or removed before others in the patch file moves their set indices, so the chunks of their sites are lifted too. A
change to `recomp/*.py` lifts everything.
`build-cache/rebuild.txt` holds the command that opens the setup window in its rebuild mode on the install folder
(`installer/gui.py --rebuild <install folder>`, started through `python -c` so the release folder is on `sys.path`
@@ -102,8 +120,12 @@ whatever the working folder), one argument per line. When an enabled mod has cod
lacks, `totk.exe` runs it at start and exits (`runtime/cheats.c` `rebuild_if_needed`); the
window runs the setup on the folder at once and starts the game again when it succeeds. A failed or cancelled rebuild
offers to start the game with the code it has; `build-cache/rebuild.skip` then keeps the same files from starting it
again, until "Rebuild the game now" on the Cheats or Mods tab. `[mods] mods_rebuild = off` turns it off. The rebuild
needs the release folder the setup ran from: a moved or deleted release folder means running a setup again.
again, until "Rebuild the game now" on the Cheats or Mods tab. A command that does not start at all leaves no
`rebuild.skip`, so the next start tries it again. `[mods] mods_rebuild = off` turns it off. The rebuild needs the
release folder the setup ran from: a moved or deleted release folder means running a setup again. On Linux the command
starts the AppImage file itself (`$APPIMAGE --rebuild <install folder>`), because the AppImage's mounted folder is gone
once the setup exits; a moved or deleted AppImage means running a setup again. Installs set up by an AppImage before
this change hold a command into that mount: running the new setup once on the install folder writes the new one.
## Command line
@@ -157,7 +179,7 @@ runtime/ libtotkrt.a, libtotkrt-v2.a and the headers the generated
include
bin/ totk_xlate.exe, hactool.exe, SDL3.dll, libopus-0.dll, ninja.exe
data/ shader_list.tsv (MacNativeGames, MIT); pgo-<ver>.profdata + .json, the execution profile an
update's code and the runtime libraries compile with (building.md, "Profile-guided build")
update's code and the runtime libraries compile with (pgo.md)
THIRD_PARTY_NOTICES.md
totk.ini.example, VERSION the default ini; the release string (the repository's VERSION plus the commit)
```
+2 -2
View File
@@ -35,7 +35,7 @@ Source shorthands:
|---|---|---|---|---|
| 1.1 | **Colour target the fragment shader does not write keeps its old texel**, even with blending enabled. | Undefined value written/blended. | NXN: zero write mask and no blend for unwritten outputs (`reflection colors`). Already fixed here. | NXN; MNG `games/totk/tests/metal_semantics/probe.m:5` |
| 1.2 | **Unwritten vertex outputs read as (0, 0, 0, 1)** in the next stage, including the unwritten components of a partly written output (VS writes xyz, FS divides by w). | FS input with no VS output: interface mismatch, value undefined; an unwritten component of a written output is undefined (AMD: whatever the register holds, often 0). | Ryujinx initialises exactly the components the next stage reads, but only when stages are linked (`Translation/Translator.cs:212-227`, `NextInputAttributesComponents`); position starts (0,0,0,1) (`:259`). NXN Link's Awakening: sun shadow lookups divided by w = 0, scene dim, no shadows. Ours: missing outputs padded with (0, 0, 0, 1), unlinked outputs initialised by translator patch 0006 (section 7, check 2). | NXN |
| 1.3 | **Clears honour the scissor and the clear's own channel / stencil write mask**, and may come between draws inside a pass. `SET_CLEAR_SURFACE_CONTROL` (0x10f8) has `RESPECT_STENCIL_MASK`, `USE_CLEAR_RECT`, `USE_SCISSOR0`, `USE_VIEWPORT_CLIP0`. | `vkCmdClearAttachments` takes a rect but **ignores the colour write mask**; load-op clears are whole-attachment. | NXN/MNG draw a triangle with the clear's mask (`clear_color_tri`, `clear_ds_tri`); TotK uses mid-pass, partial-mask and partial-scissor clears (NXN). yuzu: "red dot" was clears hitting the wrong RT index (YZ-2305, PR 10243). Hardware recipe (DK3D): `CLEAR_BUFFERS` (`0x674` = 0x19d0) carries the colour channel mask (bits 2-5), depth (0), stencil (1), target id (6-9) and layer (10-20) in one word; `ClearBufferFlags` (`0x43E` = 0x10f8) is set once to `StencilMask | Scissor` (`maxwell/gpu_3d_base.cpp:69`), so every clear is cut by scissor 0 and its stencil bits by the stencil front write mask register, and DK3D's colour clear does not program `ColorWriteMask` (`0x680`). A stencil clear writes `StencilFrontMask` (`0x4E7`) and `ClearStencil` (`0x368`) just before the clear and restores the shadowed state after (`gpu_3d_base.cpp:497-510`). Multisample raster is switched off around a colour clear (`:459, :465`). Arrays: an MME macro repeats the clear for every layer (`maxwell/clear_buffers.mme:5-22`). No pass concept: clears sit between draws. Ours: masked colour and stencil clears are **skipped** (check 3). | OGD 0x10f8; NXN; DK3D `maxwell/gpu_3d_base.cpp:444-512`, `engine_3d.def:305, 644` |
| 1.3 | **Clears honour the scissor and the clear's own channel / stencil write mask**, and may come between draws inside a pass. `SET_CLEAR_SURFACE_CONTROL` (0x10f8) has `RESPECT_STENCIL_MASK`, `USE_CLEAR_RECT`, `USE_SCISSOR0`, `USE_VIEWPORT_CLIP0`. | `vkCmdClearAttachments` takes a rect but **ignores the colour write mask**; load-op clears are whole-attachment. | NXN/MNG draw a triangle with the clear's mask (`clear_color_tri`, `clear_ds_tri`); TotK uses mid-pass, partial-mask and partial-scissor clears (NXN). yuzu: "red dot" was clears hitting the wrong RT index (YZ-2305, PR 10243). Hardware recipe (DK3D): `CLEAR_BUFFERS` (`0x674` = 0x19d0) carries the colour channel mask (bits 2-5), depth (0), stencil (1), target id (6-9) and layer (10-20) in one word; `ClearBufferFlags` (`0x43E` = 0x10f8) is set once to `StencilMask \| Scissor` (`maxwell/gpu_3d_base.cpp:69`), so every clear is cut by scissor 0 and its stencil bits by the stencil front write mask register, and DK3D's colour clear does not program `ColorWriteMask` (`0x680`). A stencil clear writes `StencilFrontMask` (`0x4E7`) and `ClearStencil` (`0x368`) just before the clear and restores the shadowed state after (`gpu_3d_base.cpp:497-510`). Multisample raster is switched off around a colour clear (`:459, :465`). Arrays: an MME macro repeats the clear for every layer (`maxwell/clear_buffers.mme:5-22`). No pass concept: clears sit between draws. Ours: masked colour and stencil clears are **skipped** (check 3). | OGD 0x10f8; NXN; DK3D `maxwell/gpu_3d_base.cpp:444-512`, `engine_3d.def:305, 644` |
| 1.4 | **Conditional rendering** (`SET_RENDER_ENABLE_A..C` 0x1550-0x1558, NVN `SetRenderEnableConditional`) compares two 64-bit counters at `addr` and `addr+16` on the GPU, in command order. TotK wraps nearly every clear in it. A `TIMESTAMP` report writes 0 to the counter field. | `VK_EXT_conditional_rendering` tests one 32-bit word against zero. | NXN: honouring it fixed the black 3D scene (light-volume depth never cleared). deko3d has no conditional rendering at all (no `SET_RENDER_ENABLE` in `engine_3d.def`), so it cannot settle the compare rule. Ours: evaluated on the CPU at record time; a report from an unfinished submission counts as 0 (check 5). | OGD; NXN |
| 1.5 | **Depth mode** is runtime state: `nvnDeviceSetDepthMode` near = -w (GL, NVN default) or 0. Register `SET_VIEWPORT_Z_CLIP` (0x0d7c) `NEGATIVE_W_TO_POSITIVE_W` / `ZERO_TO_POSITIVE_W`. | Vulkan clips z < 0 unless `VK_EXT_depth_clip_control` `negativeOneToOne`. | Ryujinx infers the mode from the viewport transform, falling back to the register (`GetDepthMode`, RYU-SU). NXN: wrong mode either clips everything (black window) or puts all depth in 0.5..1 (DOF blurs the screen). Ours: `negativeOneToOne` from NVN state. DK3D programs both halves: `SetDepthMode` (`0x35F` = 0xd7c) selects the pre-transform clip range, and the viewport transform maps z to [near, far] itself: scale z = `0.5 * (far - near)` and offset z = `0.5 * (near + far)` for -1..1, scale `far - near` and offset `near` for 0..1 (`maxwell/gpu_3d_base.cpp:143-147, 336-343`). Same maths as Vulkan `negativeOneToOne`. | OGD 0x0d7c; NXN-64, NXN; DK3D `maxwell/gpu_3d_base.cpp:136-147, 321-362` |
| 1.6 | **Depth clamp / clip**: `SET_VIEWPORT_CLIP_CONTROL` (0x193c) has separate `PIXEL_MIN_Z`/`PIXEL_MAX_Z` CLIP or CLAMP and geometry-clip modes. | `depthClampEnable` also disables z clipping (unless `VK_EXT_depth_clip_enable`). RADV had a dynamic depth clamp driver bug causing TotK vertex explosions. | Ryujinx: clamp = `!(clip & DepthClampDisabled)` (RYU-SU `UpdateDepthClampState`). yuzu disabled dynamic depth clamp on affected RADV (YZ-2305, PR 10262). DK3D has one switch, `depthClampEnable`, and writes `VIEW_VOLUME_CLIP_CONTROL` (`0x64F` = 0x193c) with `0x101A` when on and `0x181D` when off (default at setup is `0x181D`); it labels the bits "todo: figure out what this actually does". Bits 3-4 (`DepthClampNear/Far`) are set in both values, bits 0-2 and 11 differ. There is no separate depth-clip toggle. The viewport near/far registers (`Viewport` array words 2-3) carry the clamp range and are swapped when far < near (`:349-350`). Vulkan clamp to [min, max] of the viewport depth matches. | OGD 0x193c; DK3D `maxwell/gpu_3d_state.cpp:42`, `gpu_3d_base.cpp:135-139, 349-350`, `engine_3d.def:621` |
@@ -81,7 +81,7 @@ Source shorthands:
| 3.2 | **FMZ multiply mode** (`FMUL/FFMA/...` field `Fmz`, `.FMZ`): 0 × anything = 0, including Inf/NaN (DX9 rule). | IEEE: 0 × Inf = NaN. | Ryujinx decodes `Fmz` (`Decoders/InstDecoders.cs:306-309, 1901`) but no emitter reads it. Ours: same. Census needed (check 13). | RYU-SH |
| 3.3 | **Denormals**: per-instruction `.FTZ`. | AMD flushes fp32 denormals by default; `VK_KHR_shader_float_controls` sets it per shader. | Ryujinx ignores `.FTZ`. Low risk. | RYU-SH (no `Ftz` use in `Instructions/`) |
| 3.4 | **Warp size 32**: `SHFL`, `VOTE`, `S2R LANEID`, ballots are 32-bit. | AMD RDNA runs wave32 or wave64 per shader (the driver chooses; RADV fragment default wave64); `VK_EXT_subgroup_size_control` `requiredSubgroupSize = 32` fixes it. | Ryujinx adapts lane masks to `QueryHostSubgroupSize()` (default 32, `IGpuAccessor.cs:218`; `Instructions/InstEmitWarp.cs:72, 106`, `InstEmitMove.cs:175-185`). Ours: not overridden, no required size (check 6). | <https://docs.mesa3d.org/envvars.html> (`RADV_PERFTEST=pswave32`) |
| 3.5 | **Discard (`KIL`)** keeps the quad's derivatives valid (demote semantics). | `OpKill` terminates; later derivatives in the quad undefined. | Ryujinx emits `OpKill` (`CodeGen/Spirv/Instructions.cs:508`). | — |
| 3.5 | **Discard (`KIL`)** keeps the quad's derivatives valid (demote semantics). | `OpKill` terminates; later derivatives in the quad undefined. | Ryujinx emits `OpKill` (`CodeGen/Spirv/Instructions.cs:508`). Ours: on AMD's own drivers (`vk_caps.amd` without `mesa`) `vk_pipeline.c` `spirv_demote` turns each `OpKill` that a selection header branches to into `OpDemoteToHelperInvocation` plus a branch to the selection's merge block; `TOTK_DEMOTE` overrides. TotK's cloud shader discards and then takes `OpImageQueryLod`: with `OpKill` AMD's Windows driver lost fog in the fragments sharing a quad with a discarded one (#164). | <https://github.com/KhronosGroup/SPIRV-Registry/blob/main/extensions/EXT/SPV_EXT_demote_to_helper_invocation.asciidoc> |
| 3.6 | **Implicit-LOD sampling in divergent control flow** works on Maxwell (TEX unit uses quad neighbours). | Undefined in non-uniform control flow; AMD whole-quad mode usually works. | — | VKSPEC (derivatives) |
| 3.7 | **Integer division** does not exist (compiled to `XMAD`/`IMAD` sequences); MUFU.RCP(0) = +Inf, LG2(0) = -Inf. | Same IEEE edge cases through `1/x`, `Log2`. | Low risk. | — |
| 3.8 | **FP64** in some TotK cutscene shaders. | No FP64 on some hosts. | yuzu FP64 → FP32 (YZ-2306, PR 10693); ours: `softfloat64` job flag (`tools/xlate/Program.cs` `QueryHostSupportsShaderFloat64`). | — |
+1 -1
View File
@@ -111,7 +111,7 @@ keeps its textures at a quarter of the size.
| On the Mods tab | Why | What to do |
|---|---|---|
| This folder has no romfs, exefs or cheats files. | The archive was unpacked one level too deep (`mods\Mod\Mod\romfs`), or it holds no game files. | Move the folders up so `romfs` sits directly in the mod's folder. |
| ... files are made for another game version and are left out. | The mod's files carry another update's version in their names (`.Product.<number>.`). | Download the mod's release for your game update. |
| ... files carry another game version in their names. | The files are named for another update (`.Product.<number>.`) and the game's own files have no such name. They are still served under those names, as on a console, because the game sometimes asks for one; another update's resource size table is the exception and only lends its entries. | Nothing if the mod works. If it does nothing, download its release for your game update. |
| Its code patches (exefs) are made for another game version. | The patch files name another update's build id. | Same: get the release for your update. |
| This mod brings its own game code (a subsdk plugin or a replaced main). | Mods such as UltraCam ship compiled Switch code. The port cannot load it. | No workaround; the whole mod stays off. |
| This mod patches the game code (exefs) in a way the port cannot apply. | The patch writes the game's data, not its code, or changes code in a form the port cannot build. | No workaround; the reason follows on the row. |
+41
View File
@@ -0,0 +1,41 @@
# Game 1.1.0 patch coverage
The built-in table is [recomp/patches-v110.txt](../recomp/patches-v110.txt). These sites were located in the local 1.1.0 main and checked against its original instruction words, rather than applying a version-wide address shift. The generator rejects a different word or an invalid spin back-edge.
## Added coverage
| Set | Sites added | Runtime control |
| --- | ---: | --- |
| realtime | 1 | Equipment-switch cooldown: step every positive remainder, including values below FLT_EPSILON. Follows the existing real-time clock switch. |
| cursor | 3 | Scale map panning, vertical ability aim and vertical gyro aim by the 30 Hz ratio; follows `TOTK_HIGHFPS_FIX`. |
| aspect | 4 | The two camera and two viewport projections read page +56. The resolution code enables it for non-16:9 output. |
| fov | 2 | The two camera projections multiply their vertical angle by page +60. The field-of-view setting is live; 100 leaves the original path. |
| pooloff | 2 | Widen texture-pool offsets unsigned, covering 2..4 GiB without changing offsets below 2 GiB. Enabled by default; `TOTK_POOL_OFFSET_PATCH=0` retains the original path. |
| shadow | 1 | The sun shadow map takes its fixed size from page +92 before rounding. The shadow-resolution setting enables it; `shadow_res=game` leaves the game's sizing. |
| cascade | 4 | Read the four sun shadow cascade defaults from page +64..76. Shadow range 100 leaves the original constructor. |
The table has 12 sets and 50 instruction sites in total. Its existing 889 spin back-edges are unchanged. The new sets use the existing runtime page and settings; no new setting, allocator or renderer path is introduced.
The camera sites were cross-checked by normalized instruction-window matching from the local 1.0.0 main. The viewport routine has two matching copies: both are patched. Other sites were checked through their data flow: the equipment timer and its gate, the map clock, the ability clock, the NVN texture storage/package calls, the shadow size publication and the cascade parameter constructor. FOV's s8 is overwritten by the next original instruction; x10 is saved. The cursor sites use the same scratch registers as their 1.2.1 counterparts.
## Deliberately separate
- Direct mouse: 1.1.0 continues to use stick emulation. Porting the complete camera hook set also needs the runtime layout and load-site table; matching an individual store or one camera mode is not sufficient. No partial set is enabled here.
- Fade: the four 1.4.3 sites are not presumed to exist in 1.1.0. An equivalent zero-increment path has not been established.
- Native Zstandard: the existing host bridge calls back to hard-coded 1.4.3 decoder entry points on fallback. Adding only the BL patches to 1.1.0 would be incorrect.
- Live DLSS scene sizing and game-menu mouse input retain their existing version restrictions. They are not patch-table parity work.
## Validation
Run with Python's numpy, lz4, capstone and unicorn dependencies and a runtime-supported Clang:
```sh
# CC selects the Clang used by the differential harness.
python tests/patches_v110_check.py
python tests/patches_v110_check.py --cflags=-march=x86-64-v2
python tests/patches_v110_check.py --exefs <local ExeFS directory>
```
Without ExeFS, the test uses synthetic memory and the patch table only. It compares the complete integer/SIMD register state, SP and NZCV of the lifted C and Unicorn for all 17 additions, on and off, at 30/60/120 Hz, including pool offsets immediately below and above 2 GiB and at the end of the 32-bit range. Windows test mappings respect its 64 KiB allocation granularity. With ExeFS it additionally validates every original word and spin target. Extracted images and generated C are local build artifacts, never repository content.
These checks establish instruction and code-generation contracts. World-scene visual acceptance, texture packs allocating a pool above 2 GiB, physical gyro and comparisons of shadow quality still require gameplay checks.
+120
View File
@@ -0,0 +1,120 @@
# Profile-guided build
A profile-guided build compiles the generated code and the runtime at -O2 with an execution profile: a record of
which functions and branches ran while someone played. Hot paths are laid out straight, hot calls are inlined, and code
that never ran is compiled for size. On 1.4.3 a trained fight ran 12% faster per frame and an untrained fight about
6%; on 1.2.1 the trained fight ran 11% faster.
Each game version has its own profile, `pgo-v121.profdata` and `pgo-v143.profdata`, trained on that version's code.
## How a build uses a profile
- `build_game.py --pgo <file>.profdata` compiles with that profile. Every compile depends on the file, so a new
profile rebuilds everything.
- `tools/pgo.py merge` writes the profile to `build\pgo\pgo-<ver>.profdata`, next to its record `pgo-<ver>.json`. The
record holds the git tree of `recomp/` it was trained on and the digests of the runtime headers the generated C
includes (`cpu.h`, `simd.h`, `simd_fp.h`).
- `make_release.py` ships every profile in `build\pgo` in the release's `data\` folder. It warns when a record names
another `recomp/` tree than the one released, or a header digest differs. The runtime libraries of the release
compile with the newest version's profile.
- The installer builds a player's game with `data\pgo-<ver>.profdata` when the release has one for that version, and
at -O1 without it.
A profile only fits the code it was trained on. clang matches each function by name and control-flow hash, and a
function whose code changed loses its counts without a warning (`build_game.py` passes `-Wno-backend-plugin`, which
hides the "hash mismatch" messages). The inline helpers of `cpu.h`, `simd.h` and `simd_fp.h` become part of every
lifted function, so a change to them invalidates the profile as surely as a generator change: one change to the
floating-point helpers left a profile with a fifth of its weight and 2% of its gain.
## When to retrain
- After a change to the lifter or the generator (`recomp/`) that changes the generated C. Patch table changes
(`recomp/patches-<ver>.txt`) touch only the patched functions and can wait for the next retrain.
- After a change to `runtime/cpu.h`, `runtime/simd.h` or `runtime/simd_fp.h`.
- Before a release whose `make_release.py` run warns about a profile.
Train both versions from the same commit, from a clean `recomp/`.
## Train
### 1. Instrumented build
MSYS2's clang has no profile runtime, so the instrumented build uses llvm-mingw (unpack it under `toolchain\`):
```powershell
$llvm = "toolchain\llvm-mingw-<date>\llvm-mingw-<date>-ucrt-x86_64"
.venv\Scripts\python tools\build_game.py --version v143 --out build\v143-pgogen --toolchain $llvm --pgo gen
```
Regenerate `generated\<ver>` first if it is older than the commit you train on. If `totk.exe` stops at start with
`0xc000007b`, copy `libiconv-2.dll` from `C:\msys64\ucrt64\bin` next to it.
`totk.exe` writes `build\v143-pgogen\pgo\default_<id>.profraw` every minute and at exit. Each run of the same exe adds
to that file.
### 2. Shaders
Copy your `totk.ini` into the build folder and prepare the shaders (`tools\prepare_shaders.py build\v143-pgogen
--version v143`). Shader preparation runs `totk.exe` too, so move `pgo\*.profraw` out of the build folder afterwards:
its counts describe shader building, not play.
Keep the default settings, with the GPU command recorder on (`vk_recorder = 1`, the default).
### 3. Training scenes
The instrumented build runs slower than a normal one. Count each scene's time by the game clock, not by the wall
clock. Spend 90 to 120 seconds on each of the first four scenes, where most frames are spent, and about 60 seconds on
each of the others, which cover code the first four do not reach.
| Place | What to do | Time |
| --- | --- | --- |
| Lookout Landing | Stand in the middle of the town and turn the camera slowly all the way round, then walk through the crowd. | 120 s |
| Kakariko Village | The same: a slow camera turn, then walk between the houses. | 100 s |
| A fight with a Battle Talus | Fight it with weapons and the bow. | 110 s |
| The Depths, near a Lightroot | Turn the camera over the lit area, throw a Brightbloom Seed. | 90 s |
| The Depths, away from any light | Walk and turn the camera in the dark. | 60 s |
| The edge of a sky island | Run off the edge, skydive, open the paraglider. | 60 s |
| Outdoors in the rain | Walk and look around while it rains. | 60 s |
| A cave with Brightbloom plants | Walk in and fight the Moblins and Bokoblins inside. | 60 s |
| Inside a shrine | Walk through the first rooms. | 60 s |
| A Moblin camp | Fight several Moblins at once. | 60 s |
| Next to Yunobo | Use his ability: roll him into something. | 60 s |
| Hyrule Field, by the standing stone pillars | Run across the field, also backwards past the pillars. | 60 s |
Keep two scenes out of the training, so that you can measure on scenes the profile has not seen: Hyrule Field in tall
grass, and a fight with Moblins and Zonai constructs at once.
With a save at a scene, `tools\run_game.py` plays it without you: `--save <backup> --script <input file> --seconds
<game time>` ([agents/running.md](agents/running.md)). Without a save, play the scenes by hand.
### 4. Free play
Then play for 15 to 25 minutes and touch every kind of thing the game does, so that no common path is left without
counts:
- Run, sprint, climb, swim and glide; ride a horse; jump from a tower and skydive.
- Fight with one-handed and two-handed weapons, a spear and the bow; shield parry, flurry rush, fused arrows.
- Use every ability: Ultrahand (build a vehicle with a fan and wheels and drive it), Fuse, Ascend, Recall, Autobuild.
- Call the sages and use their abilities: Tulin, Riju, Sidon, Yunobo, Mineru.
- Open the inventory, the map and the camera; cook at a pot; talk to people in a town.
- Travel quickly to a few places, so that areas load; enter and leave a shrine and the Depths.
- Play in daytime and at night, in clear weather and in rain or snow.
### 5. Merge
```powershell
.venv\Scripts\python tools\pgo.py merge build\v143-pgogen --version v143
```
writes `build\pgo\pgo-v143.profdata` and its record. Merge right after the training, from the checkout and commit the
instrumented build was made in: the record reads the `recomp/` tree from git and the header digests from the build's
Ninja deps log.
Repeat steps 1 to 5 with `v121` for 1.2.1.
## Measure
Build the same code three ways in three folders with llvm-mingw, as the installer does: with the new profile, with
the old one and with none. Prepare the shaders in each folder. Then run the held-out scenes and one trained scene
in the order new, old, none, none, old, new, and compare the mean frame time over the same stretch of game time. One
run of a fight can vary by up to 15% from the next, so a profile counts as better only when it wins both pairs.
+91
View File
@@ -0,0 +1,91 @@
# Rules for taking a PR
## 1. What to review first
1. Fixes before features. A PR that fixes a bug, a glitch or a crash goes first; feature PRs wait.
2. Order fixes by reach: how many players the bug hits and how badly. Windows and default settings first, rare
hardware and unusual setups later.
3. Scale the depth of the checks to the reach. A crash many players hit and anything on the frame path get the full
checks. A rare case (a CPU without AVX, an unusual setup) gets the minimal fix, one targeted check and a short
note. A crash fix on rare hardware is still worth taking: cut the checks around it, not the fix.
4. Before testing, find out where the bug reproduces: OS, GPU vendor and generation, game version, settings. If the
symptom cannot appear on the hardware at hand (an NVIDIA paging bug on an AMD machine), say so and do not run
tests that cannot decide it.
5. Check a feature PR against the designs in `docs/milestones/`. A feature with a design (mods, cheats) goes in as
part of that design.
6. Several PRs on one topic: compare them before taking either. Another PR may already do it right.
## 2. How to read the diff
- The author's claim is a hypothesis. Read the whole diff: races, wrong semantics, edge cases, leaks, lock order.
- Check the behaviour against evidence: how yuzu, Ryujinx, LAS Recomp, SavageWindRecomp, nxnative and MacNativeGames
do the same thing in `refs/` (`tools/fetch_refs.sh` recreates it); the Vulkan spec; hardware docs
([docs/maxwell.md](../../docs/maxwell.md)); upstream issues.
- Weigh two things for every change: its cost on the hot path, and how it fails (device lost, hang, stale data, weak
or non-AVX CPUs, other GPU vendors, Wine/Proton).
- Read the code a hypothesis depends on before running the game: the game's functions in Ghidra or
`generated/<ver>`, our path in `runtime/`. A run confirms what the code could not rule out. It is one run, not a
series of variants.
- Order the work so the cheapest check that could refute the PR's idea runs first. Refuted: stop, write down why,
no build, no benches.
## 3. What a PR needs
- **Proof.** The bug reproduced before the fix, the same repro after, both results side by side: a screenshot, a log
line, a probe, a differential test. A fix without a reproduced failure is not proof. Where a repro is impossible
on the hardware at hand, say so; the fix is then taken only on strong static evidence in the code. Where a repro is
risky (device lost or a TDR can hang the machine), write the recipe and let the owner decide.
- **Frame-path changes** get whole runs of the heavy scene with the switch off and on, at least two each
([02-performance.md](02-performance.md)). Zero new slow frames is a hard bar.
- **A switch.** A change on the hot path or in rendering sits behind an env or `totk.ini` switch, so a player can turn
it off and we can compare.
- **Nothing changes for players outside the feature.** The DLSS PRs touched shared code; they went in once every
path that runs without DLSS was gated on DLSS being on.
- **A fix for one setup keeps the default for everyone.** The PRs that raised the low memory block from 6 to 10 GiB for
all players were rejected; the right shape is 6 GiB plus the extra where it is needed.
- **Parts we cannot run** (Linux-only code, an HDR display, RTX-only paths) are gated so they cannot touch anyone
else, or wait for a report from someone with that hardware. Note them as unrun.
- **Project tests** from [03-correctness.md](03-correctness.md): lifter differential tests, probes, `--selftest`,
`tools/check_content.py --staged`.
- **Text the player sees** goes into the catalog: `runtime/ui_text.h` ids and `runtime/ui_text.c` entries in every
language the menu has (English, Russian, Brazilian Portuguese, Simplified Chinese), and `installer/text/*.json` for
the installer. No hardcoded sentences, no second string table.
- **A new setting** is described in `totk.ini.example` and [docs/settings.md](../../docs/settings.md).
- **Licences.** MIT, BSD, ISC, zlib and Apache-2.0 code may be copied, with its header and a row in
`THIRD_PARTY_NOTICES.md`. yuzu, suyu and other GPL code is read for behaviour only; we write our own.
## 4. Reasons to reject or rework
- No test or repro for a change that is not trivial.
- Slow: a state copy per draw, a global atomic counter on the hot path, synchronous file I/O or a GPU allocation on a
game or render thread, new slow frames.
- Changes behaviour for everyone to serve one setup, or is on by default without a run in a scene where it acts.
- "Fixes" a crash by turning a feature off for a vendor. A crash that goes away with a feature off points at the bug;
the feature gets fixed.
- Breaks another feature (a preview that loses anti-aliasing).
- Fake features: labelled FSR4 or frame generation, and not that.
- Wrong measurements: percentiles over window means, counters over the wrong set.
- Generated C edited by hand instead of the generator (`recomp/`).
- A disallowed licence or pasted GPL code.
## 5. Finishing a good PR
A good PR that misses the project's scheme (hardcoded text, conflicts, layout, comment style) is brought to it while
merging, not sent back to the author for another round. When only part of a PR is good, take that part and note what
was left out and why.
A bug found outside the PR under review is not fixed on the spot: search the open PRs on that topic, report the bug
with them, and let the owner decide.
## 6. Merging, branches, commits
- Each PR or wave of PRs gets its own branch `task/<tag>` from a fresh `dev` and its own build dir. It merges into
`dev`.
- Once the review is done, the symptom is gone on one paired shot and the probes pass, merge into `dev` and hand the
build to players: they test in game faster and wider than our routes. The wider checks (other routes, validation,
fps cost) follow as a separate diff. Speed never skips the review of the diff itself.
- `main` only with the owner's explicit yes. Push only when told to.
- A commit message is a plain English sentence, no `feat:`/`fix:` prefixes.
- The version lives in the repo-root `VERSION` file; the owner decides when it moves.
- After a merge, record which PRs went in, what was reworked, what was rejected and why. The reason for a rejection
goes into the PR as a plain English comment.
+97
View File
@@ -0,0 +1,97 @@
# Performance: no lost frames on weak hardware
## Who we tune for
- The goal is many more fps on modest hardware, and Steam Deck later (Zen 2 4c/8t APU, shared LPDDR5, the exe runs
through Proton/Wine).
- The developer's machine (Ryzen 9 7900X + RX 9070) is fast and its numbers do not carry over. A laptop with an
i7-9750H and a GTX 1050 Ti is a sample of a weak machine, not the target to fit constants to.
- What weak hardware measured:
- GTX 1050 Ti (4 GiB, 214 MiB BAR, no ReBAR): the GPU read imported host pools over PCIe, 110-120 ms a frame
against 4.3 ms on the RX 9070. The VRAM mirror brought it to 10-11 ms; that machine is now CPU-bound at 25-45 fps,
replay 25-30 ms a frame.
- The same write-watch scan costs about 3x the CPU time there.
- NVIDIA and desktop AMD GPUs have no ASTC: textures decode in a compute shader. CPU decodes before that made
0.4-0.7 s load frames on the 1050 Ti.
- Wine emulates `GetWriteWatch` with `mprotect` and a fault per first write. Anything relying on cheap OS memory
tricks (write-watch, `VirtualProtect` freezing, fault handlers) is checked against Wine's implementation.
- The port uses about half of even a weak CPU, so the frame's critical path is the limit, not throughput. TotK keys
per-core data by `GetCurrentProcessorNumber` without locks, so the game's threads share three virtual cores on any
host. In a fight the main thread and five job workers keep 2.6 of the 3 busy: guest code speed sets the fps.
- Perf work builds and measures 1.4.3. A 1.2.1 build is for a change about to merge with a large gain.
## Gain ceiling first
Before writing or taking a perf change, compute its ceiling: the share of the code it removes times the share of the
frame that code holds on the critical path, on weak hardware. A ceiling of a few percent or less is not worth it.
- Use the critical path (`TOTK_CRIT` + `tools/crit_path.py`, heavy frames only), not one thread's profile. On the weak
laptop the submit thread is 55% replay, yet the critical path of heavy frames is 67% guest code, 15%
`nvnSyncWait`, 13% core waits and wake latency.
- Changes that can move the frame by 10% or more are usually design changes. Polishing a hot function from a flat
profile almost always gives zero.
## A neutral safe change stays
An optimization that measures neutral on the fast machine stays on by default when:
- upstream (`refs/`, other ports) reports a gain for it, especially on weak CPUs or GPUs;
- the static read leaves no hazard, and the probe or differential test passes;
- whole heavy-scene runs off/on x2 show no fps loss and no new slow frames;
- a switch turns it off.
"No gain here" is not a reason to drop a safe change with upstream evidence. A regression or a hazard nobody ruled
out is.
## How to measure
- **By game time, never by frame counts.** Run length, analysis windows, A/B blocks and input scripts go by the world
clock (`--seconds`, `TOTK_CRIT=w<lo>-<hi>`, `TOTK_PROFILE_FRAMES=w<lo>-<hi>`, `TOTK_SHOTS=w<lo>-<hi>:<step>`). The
same frame count at another fps is another scene.
- **Check a scripted run reached the world.** Title menus on a slow boot eat button presses; confirm the run is in the
world before trusting its numbers.
- **Whole runs off and on, two each at least.** An in-run A/B on a moving scene lies: the A and B blocks land on the
same game seconds every run, so a content difference repeats. One in-run A/B showed "-11% replay"; whole runs gave
5.84/5.73 against 5.76/5.73 ms, nothing. In-run A/B only on a static scene (standing still).
- **Spread.** One script on near-identical builds gave 1% lows from 31.5 to 50 fps. Compare medians of three runs,
never one run against one run.
- **A quiet machine.** `tools/run_game.py --quiet` takes the machine-wide lock; no other `totk.exe`, build or test runs
meanwhile. One game at a time.
- **A warm shader cache, prepared properly.** Shader and pipeline caches are not copied between build dirs:
`totk.exe --prepare-shaders` or `--warm-shaders` in that dir. The AMD driver cache is keyed to the exe path and
limited in size: preparing another build dir can evict this one's libraries (40-220 ms per new effect in play).
- **A heavy scene.** A fight with a large enemy and a crowd, fps limit at the maximum (`TOTK_FPS_LIMIT=360`). Compare
one shot with the scene's reference before trusting numbers.
- **No base runs by habit.** Run the candidate alone and judge it against the targets: max fps, zero slow frames
(`perf: slow frame`), no black or flat frames, no new validation errors. Glitches are found by looking at shots and
frame dumps, not by diffing metrics. Run a base build only when a specific question needs a side-by-side.
| What | How |
|---|---|
| fps, worst frame, render-thread buckets, GPU busy, VRAM | `--env TOTK_TRACE=perf,sched`, then `python tools/perf_summary.py <log> <lo> <hi>` |
| the frame's critical path | `TOTK_CRIT=w<lo>-<hi>` + `TOTK_CRIT_OUT=<path>`, then `python tools/crit_path.py <file>` |
| sampling profiler | `TOTK_PROFILE=<path>` + `TOTK_PROFILE_FRAMES=w<lo>-<hi>`, then `tools/prof_report.py <profile> <totk.map>` |
| A/B on a static scene | `TOTK_AB=<seconds>`, then `tools/ab_summary.py --skip <seconds>` |
| VRAM pressure on a large card | `TOTK_VRAM_LIMIT_MIB=<MiB>` caps the budget the port sees |
## What the hot path must not do (Windows host)
- **Files.** The game threads (main, job workers) and the render thread never open, read, write or flush a file, and
never wait for a thread that does. Opening a 20-byte file in a 139k-file folder: 0.1 ms idle, 11-40 ms in play while
the translator, the pipeline cache and the save wrote to the same volume. A save's `FlushFileBuffers` and
`MOVEFILE_WRITE_THROUGH`: 93-135 ms. Saving the pipeline cache under its lock on the present thread froze the replay
every 20 s. File I/O goes to a background thread; results come back in memory.
- **Waits.** Short waits spin on the counter; waits over 1 ms use the high-resolution timer. A waitable timer wakes
0.3-0.5 ms late; the SDK's 20 us sleeps took 460-515 us. A 1 ms timer thread does not help: it gave more slow frames.
- **GPU allocations.** VMA's `vkAllocateMemory` of a 256 MiB block under its lock: 8-23 ms. A new block on the render
thread is a hitch.
- **128-bit division.** `(unsigned __int128)t * num / den` compiles to the library call `__udivti3`; counter
conversions that way took a tenth of the game threads' time in a fight. Use the precomputed scale (`platform.h`
`PlatScale`).
- **Per-draw copies and counters.** A pipeline-state copy per draw, a global atomic counter, a scan of every page each
frame: on a weak CPU each is frames.
- **Memory kept per frame.** Anything allocated per texture initialization or per frame must be freed. One copy kept
per texture init grew host memory by ~44 MiB a game minute until a commit failure after hours (#670). Check private
memory over a few idle minutes for a PR that stores per-object state.
- **Scheduling.** Any timing or scheduling change is checked for slow frames. A preempt check at every host call made
one bug rarer but added 50-75 ms CPU hitches and was reverted.
+92
View File
@@ -0,0 +1,92 @@
# Correctness: checks that keep behaviour intact
## Lifter and generated C
- Generated C is never edited by hand: change the generator in `recomp/`.
- `recomp/layout.py` alone decides module bases.
- Every change to the lifter or the CPU headers (`runtime/cpu.h`, `runtime/simd.h`) passes the differential tests
against Unicorn, all run with `.venv`:
| Test | Checks |
|---|---|
| `tests\diff\lift.py` | instructions sampled from the modules; `--coverage` lists the words the lifter rejects |
| `tests\diff\simd.py` | the whole FP/SIMD encoding space |
| `tests\diff\atomic.py` | exclusive load/store and ARMv8.1 atomics |
| `tests\diff\func.py` | whole functions as the generator emits them: loops, jump tables, calls |
| `tests\flags_check.py` | the function-local flag helpers against the packed ones |
| `tests\excl_mt.py` | the exclusive monitor under contention and ABA, on several threads |
| `tests\jumptables_check.py` | `recomp/jumptables.py` against Unicorn: 0 disagreements on 1.2.1 and 1.4.3 |
- A `--cpu v2` build (no AVX2) passes the same tests with `--cflags=-march=x86-64-v2`. Players without AVX exist.
- A compile error in generated C on a line the generator could not have written is a bit flip on disk or in memory.
Regenerate and compare hashes; leave `recomp/` alone.
## Runtime
- From the build dir: `totk.exe --selftest`, `--probe-kernel`, `--probe-vkexec`, `--probe-nvn-tex`, `--probe-shader`
pass.
- Settings: `totk.exe --probe-cfg` and `python tests\settings_doc_check.py`.
- Button prompts: `.venv-py314\Scripts\python tests\romfs_prompts_test.py`. Build dirs carry only `romfs-patches`; to
test PC or Xbox prompts in game, generate the overlay first:
`.venv-py314\Scripts\python tools\make_romfs_patches.py --version <ver> --prompts pc --out build\<dir>\romfs-patches-pc`,
then restart the game.
- Installer: `tests\installer_gui_test.py` (event model, form, catalogs, cancel, display scale).
- Shaders: `tools\shader_census.py --version <ver> --translate`, `tools\xlate_smoke.py`. A translator patch
(`tools/xlate/patches/`) needs `build/xlate` rebuilt and changes shader hashes: anything keyed to a shader hash
breaks with it.
## GPU portability
- New Vulkan code queries the feature, extension, format or limit it uses and keeps a working path or a logged
fallback without it (provoking vertex and subgroup size pinning in `vk_pipeline.c` are examples).
- Never hardcode what one GPU reports: alignments, subgroup size (RDNA4 may run wave64, old AMD Vega is wave64 only),
depth formats, `maxPushDescriptors`.
- One queue: every submit and present takes `vk_queue_lock`. Presenting from its own thread on a second queue gave
device lost on GTX 10-series in another port.
- RDNA1/2 bugs do not reproduce on RDNA4 (another driver branch); NVIDIA bugs do not reproduce on AMD. A Ryzen 7000
iGPU is RDNA2 and runs the RDNA1/2 driver branch: a cheap testbed for those reports.
- Validation: a `--validation` run in the world shows no new messages.
## Maxwell / NVN behaviour
The port must do what the Switch GPU does, not what Vulkan does by default. The full list with sources:
[docs/maxwell.md](../../docs/maxwell.md), section 7. The traps a PR breaks most easily:
- Provoking vertex: Vulkan takes the first vertex, NVN the last.
- Unwritten vertex outputs read (0, 0, 0, 1).
- A colour target outside the fragment shader's output map keeps its texel (write mask 0). AMD writes garbage there.
- Feedback loops: the G-buffer pass reads its own targets. It needs the feedback layout and a by-region barrier.
- Depth bias argument order, depth mode, depth clamp: checked against deko3d.
- Report timestamps: the engine's GPU time comes from them, and dynamic resolution and game speed follow it.
## Timers and the real-time step
- With `realtime` set, the game steps by a measured ratio, not whole 30 Hz units. A countdown remainder in
(0, FLT_EPSILON] is never stepped again, and a consumer testing `> 0` sees it active forever. That was "weapons
cannot be changed after a Fuse until a reload" (#18).
- Symptoms of this class: stuck until a reload or teleport, locked after an action, works sometimes.
- A PR touching the frame step, the pacer or the fps limit is checked for this class.
## Threads and scheduling
- ARMv8 `stlr` then `ldar` is ordered (RCsc); C11 release/acquire lets x86 reorder that store and load. Changes to
the lifter's barriers or atomics pass `tests\diff\atomic.py` and `tests\excl_mt.py`.
- A scheduler change is judged from one `TOTK_TRACE=sched` run by how long a ready, better thread waits for a core it
may use, plus the slow-frame count. Not by statistics over many runs.
- A crash that looks like a race: rule out the save first. One reported crash was the save itself; it crashed the
same way on a real Switch.
## Files and caches
- A crash leaves zero-filled files. Caches are validated on load, not trusted because they exist.
- The pipeline cache is dropped when vendor, device or `pipelineCacheUUID` change.
- Unicode paths: W APIs with UTF-8 to UTF-16 conversion.
- A failure that happens once and does not come back: rerun before debugging it as a code bug. Broken caches and bit
flips happen.
## Game content boundary
- The repository is public. No game files, and nothing extracted or generated outside `build/` and `generated/`.
- Keys never appear in logs, commands, files, issues or comments.
- Every file the generator emits carries the `GENERATED_MARKER` defined in `tools/check_content.py`.
- `python tools/check_content.py --staged` before every commit. CI runs the same check.
+97
View File
@@ -0,0 +1,97 @@
# Knowledge base
Before investigating a symptom, or trusting a PR that "fixes" it, look for it here. Search by what the player sees
(black, magenta, flicker, slow motion, missing, stale, streaks) and by the object (sky, grass, shadow, fog, HUD).
## Invariants of recent changes
A PR that touches these areas must keep them.
- **Queue order.** When anything is submitted, every earlier part is already on the queue. Present, uploads, readbacks
from guest threads and the `vkDeviceWaitIdle` sites rely on it. The recorder thread now begins, ends and submits
the parts it records; the submitting thread still waits for that submission, so the order holds.
`TOTK_REC_END=0` restores the old split.
- **GPU-written memory is never frozen.** Storage-buffer stores, copies and fills land in imported guest memory
without a fault or a write-watch bit, so a region only the GPU writes looks idle. Such regions are never frozen
read-only, and a frozen region is thawed before a GPU write. LAS Recomp never changes the protection of imported
memory because of NVIDIA. A suspected cause of NVIDIA "write invalid" device faults (#788, #802), not proven.
- **VRAM pressure.** Under pressure, released textures whose storage the game has given to another texture go first:
30 s after release and once no finalized slot has bound them for 600 submits. Clean mirrored entries wait 5 s.
Both stop once use is back under the line. A PR touching texture lifetime is tested with `TOTK_VRAM_LIMIT_MIB`.
- **Texture pins are freed.** Each pin counts the recording copies naming it and is freed 1200 submits after its last
use unless the replay holds it as a render target. Before that, pins leaked ~44 MiB a game minute (#670).
## Fixed bug classes
If a PR touches this code, confirm the fix still holds.
| Symptom | Cause | Where the fix is |
|---|---|---|
| lime silhouettes in grass; rectangles in the sky | a target outside the output map must keep its texel; zero-filling it wiped the half-res depth | `vk_pipeline.c` `target_wmask` |
| stale grass maps in the far terrain for a second after a hit | write-back of a texture the CPU had rewritten | `vk_texture.c` `write_back` |
| cut grass stays standing; fire does not spread (#132) | CPU reads of GPU-rendered textures without an invalidate | `vk_texture.c` `vk_texture_write_back_small`, `nvn_ext.c` `nvn_pool_invalidate` |
| cloud and fog volumes empty for the session | a one-shot draw skipped instead of waiting for its pipeline | `vk_exec_draw.c` `prepare_draw` |
| tile rectangles | storage buffers bound by Ryujinx first-use order, not the c0 slot | `vk_exec_draw.c` `ssbo_slot` |
| black dialog background and loading map | 3D targets rejected, the grading LUT never written | `vk_exec.c` `resolve_target` |
| streaks on cliffs | primitive restart ignored | `vk_exec_draw.c` `restart_for` |
| streaky map screen | the newest texture picked at shared storage | `vk_texture.c` `vk_texture_by_id` |
| dark 16x16 blocks on the ground, sage spirit not drawn | G-buffer feedback loop | `vk_exec_draw.c`, `TOTK_FEEDBACK` |
| magenta cave walls, brown sky | CPU-filled textures without a flush | write-watched mirrors, `vk_memory.c`, `vk_texture.c` |
| black 3D scene | deferred texture binds | `nvn_api_cmd.c` `Bind*Deferred` |
| HUD prompt boxes, black stamina wheel | `nvnTextureCompare` ignored the channel mapping | `nvn_api_obj.c` |
| crash on world load | `GetCommandMemoryUsed` did not follow the driver's sizes; the game sizes model heaps from it | `nvn_api_cmd.c` |
| 20 fps | a yield back-off starting before 2 ms | `kernel.c` SVC 0x0B: no back-off |
| weapons locked after a Fuse (#18) | timer remainder in (0, FLT_EPSILON] with the real-time step | patch under the `realtime` setting |
| magenta/blue garbage or device lost on RDNA1/2 (#5) | writes into 3D BC block views | in `dev` |
| black window after a driver update | a pipeline cache from another driver | `vk_pipeline.c` drops the cache by UUID |
## Known open risks
A PR touching these needs stronger proof.
- Buffer textures are not replayed and bind a stand-in: light and decal lists may read 0.
- New GPU-only colour images start undefined; feedback effects may show garbage.
- Clears with a partial channel or stencil mask are skipped (`W_CLEAR_MASK`, `W_STENCIL_MASK`).
- 8-bit indices are widened on the CPU at record time; a copy in the same submission gives stale indices.
- `UpdateUniformBuffer` is a copy between full barriers and ends the render pass (fps outdoors).
- Report timestamps carry host time at fence completion: the engine's GPU time is wrong.
- The game's SVC-free spin loops burn a host core (843 in `main`).
- Images aliased onto one GPU-only pool allocation do not see each other's data on AMD.
## Past PR decisions
| PR | Decision | Why |
|---|---|---|
| #690 shrine snapshots | rejected | a pass restart per copy, a `VkPipeState` copy per draw, broken `bind_sig` skip; fps loss |
| #674 | rejected | a global STXR counter on the hot path |
| #660 | rejected | the preview loses anti-aliasing |
| #713, #576 | rejected | low memory block 10 GiB for everyone; should be 6 GiB plus the extra where needed |
| #573 | rejected | dynamic resolution off by default for everyone, the 1.4.3 site never run |
| #568, #569 | rejected | fake FSR4 and frame generation |
| #487 | rejected | wrong metrics: percentiles over window means, counters over the wrong set |
| #451, #380 | rejected | not painless to take |
| #566, #594, #534 DLSS | taken with changes | #594's shared paths gated on DLSS being on; nothing changes without DLSS |
| #489 | partly taken | the runtime part checked by a frame dump; the installer's DLL download was the owner's call, its pinned hash re-checked |
| #519, #582 amiibo | rewritten by us | took the data-format fixes; left out the features and the Linux event rework |
| #483 vs #585 | #585's mapping kept | #483's stick glyph mapping was rewritten unasked while #585 already had it right |
| #528 rain | taken after a check | code review fine, merged only after a run on a rain save |
| #767 | taken with our fix | the default was skipped with `prompts = xbox\|pc` when those folders did not exist |
| #625 frame queue | taken, off by default | a new frame queue, one field run with it on |
| #743 UB overlay | waiting | +25% in a Depths fight on an RTX 4080; needs review, fight runs x2 and shots on AMD |
## Our mistakes, not to repeat
- **"-11%" was an A/B artifact** of a moving scene. Whole runs off/on showed zero.
- **Micro-optimizations from a flat profile** (1-3% of samples each) gave 26.2 against 26.4 ms on the weak laptop:
nothing. Nobody had computed a ceiling.
- **A scheduler patch merged on run statistics** added 50-75 ms hitches. Slow frames are a hard bar.
- **Ten game runs on lock theories** where one Ghidra read of the game's code narrowed it at once.
- **45 minutes of builds and benches** on an idea its own first thread census had refuted: the game pins 53 of 56
threads itself. The cheapest refuting check goes first; when it refutes, stop and write the idea down.
- **Tests of a bug that could not appear on the hardware at hand** (NVIDIA paging on an AMD machine): 245k tokens and
37 minutes for nothing. Ask first where the bug reproduces.
- **Turning the recorder off for a vendor** instead of finding its bug. Stopped: the feature gets fixed, not silenced.
- **Bisecting code when every build crashed the same way at once.** The cause was a bad page of a game file in the OS
file cache. When all builds fail identically, suspect inputs and caches first.
- **A duplicate issue closed with a bare "Same as #N"** lost the logs and links. Move everything useful to the issue
that stays open first: hardware, driver, version, when it happens, workarounds, links from comments.
+49
View File
@@ -0,0 +1,49 @@
# Per-PR checklist
## Before reading
- [ ] Fix or feature? Fixes first, ordered by how many players they reach.
- [ ] Where does the bug reproduce: OS, GPU vendor and generation, game version, settings? Can the hardware at hand
show it?
- [ ] Is the symptom in [04-knowledge.md](04-knowledge.md) or `docs/maxwell.md`?
- [ ] Another open PR on the same topic? Compare them.
- [ ] Feature: is there a design in `docs/milestones/`?
## Reading the diff
- [ ] The whole diff read, not the description: races, edge cases, lock order, leaks.
- [ ] Behaviour checked against `refs/` (Ryujinx, yuzu, LAS, SavageWind, nxnative, MacNativeGames), the Vulkan spec,
`docs/maxwell.md`.
- [ ] Hot-path cost: no file I/O, GPU allocations, waits, per-draw copies or global atomics on game or render threads.
- [ ] No memory kept per frame or per object without a free.
- [ ] Failure modes: device lost, hang, stale data, CPUs without AVX, NVIDIA/Intel/old AMD, Wine/Proton.
- [ ] New Vulkan code queries the feature and has a fallback; nothing hardcoded for one GPU.
- [ ] Invariants kept: queue order, GPU-written memory never frozen, VRAM pressure path, pins freed.
- [ ] Players without the feature see no change. The default for everyone stays for one setup's sake.
- [ ] New behaviour behind a switch. A new setting described in `totk.ini.example` and `docs/settings.md`.
- [ ] Player-visible text in the catalogs, in every menu language (`runtime/ui_text.c`, `installer/text/*.json`).
- [ ] Copied code: allowed licence, header kept, row in `THIRD_PARTY_NOTICES.md`. No GPL code.
- [ ] Generated C not edited by hand.
- [ ] Parts we cannot run (Linux, HDR, RTX-only) gated away from everyone else and noted as unrun.
## Proof
- [ ] Repro before the fix and the same repro after, side by side.
- [ ] Lifter or CPU headers: differential tests against Unicorn; `jumptables_check.py` 0 disagreements.
- [ ] Runtime: `--selftest` and the four probes pass.
- [ ] Settings: `--probe-cfg` and `tests\settings_doc_check.py`. Prompts: `romfs_prompts_test.py`, overlay generated
for a game check.
- [ ] Frame path: whole heavy-scene runs off/on, two each at least, by game time, quiet machine, warm cache prepared
in that build dir. No fps loss, zero new slow frames.
- [ ] Rendering: shots or a frame dump in a scene where the change acts; no black or flat frames; no new validation
errors.
- [ ] Perf PR: gain ceiling computed for weak hardware.
## Decision
- [ ] Taken, partly taken or rejected, with the reason written down and posted to the PR.
- [ ] What missed the project's scheme brought to it while merging.
- [ ] Bugs found outside the PR reported, not fixed on the spot.
- [ ] Merged into `dev` from `task/<tag>`. `main` only with the owner's yes.
- [ ] `python tools/check_content.py --staged` before the commit.
- [ ] Commit message: a plain English sentence, no `feat:`/`fix:`.
+32
View File
@@ -0,0 +1,32 @@
# PR review kit
Rules and background for a tester or reviewer who takes community PRs into `dev`. One goal: no PR breaks the
game's behaviour or costs frames on weak hardware.
## The short version
1. A PR description and "works for me" prove nothing. Read the diff yourself and check it against `refs/`, the
Vulkan spec and upstream issues.
2. Almost every change needs a test: a repro before and after, a probe, a differential test, or whole runs with the
switch off and on.
3. The developer's machine is fast; players run weak machines. "Neutral here" says nothing about them, in either
direction.
4. A player without the problem sees no change: the new path sits behind a switch and the old path stays as it was.
5. Nothing goes into `main` without the project owner's explicit yes. Accepted work lives in `dev` first.
## Files
| File | Covers |
|---|---|
| [01-rules.md](01-rules.md) | triage order, what a PR needs, reasons to reject, merging and commits |
| [02-performance.md](02-performance.md) | target hardware, gain ceiling, how to measure, what the hot path must not do |
| [03-correctness.md](03-correctness.md) | lifter, tests and probes, GPU portability, Maxwell behaviour, timers, threads, game content |
| [04-knowledge.md](04-knowledge.md) | invariants of recent changes, fixed bug classes, open risks, past PR decisions, our mistakes |
| [05-checklist.md](05-checklist.md) | the per-PR checklist |
The public docs this kit builds on: [docs/architecture.md](../../docs/architecture.md),
[docs/building.md](../../docs/building.md) ("Checks"), [docs/maxwell.md](../../docs/maxwell.md),
[docs/settings.md](../../docs/settings.md), [docs/tester-guide.md](../../docs/tester-guide.md), `AGENTS.md`.
Working with a coding agent: give it this folder and `AGENTS.md` before the first review.
State as of 2026-11-06. The code and `docs/` win over this text where they differ.
+25
View File
@@ -74,8 +74,10 @@ Many rows below are developer tools that only act together with another setting
| `menu_test_set` | `<frame>:<key>=<value>[,...][;...]` changes settings live at that frame, with or without the menu open; `SAVE=1` also writes `totk.ini`. | Settings change during play without input, and `SAVE=1` rewrites the file. | Caution |
| `probe_movie` | Text that picks the movie for `totk.exe --probe-movie`: the first file whose path contains it. | Only used by the movie decoder probe; ignored in normal play. | Safe |
| `probe_movie_shot` | Path of a `.ppm` file that the movie probe writes the middle frame to. | Only used by the movie decoder probe. | Safe |
| `probe_movie_bench` | `1` makes the movie probe also time the resampler that fits a movie to a game resolution of another shape, and print the time per frame. | Only used by the movie decoder probe. | Safe |
| `swkbd_answer` | `<text>`, `<cancel>` or `<initial>`: answers the on-screen keyboard (for example a horse name) without opening a window. | The game gets that answer every time it asks for text. | Caution |
| `swkbd_test` | `ok` or `cancel` opens the keyboard window and presses OK or Cancel on the first timer tick. | The keyboard window closes by itself. | Caution |
| `test_oom` | `<site>:<n>[,...]` fails the first n allocations at a site: `commit` (guest memory), `staging` (upload buffers), `image` (texture images), `chunk` (draw data buffers). | Each forced failure takes the out-of-memory path: a memory release, a wait of 10 ms per retry and log lines; a large n stalls up to 30 s and then ends the game. | Caution |
| `test_pad` | `<lx>,<ly>,<rx>,<ry>` attaches a virtual gamepad whose sticks rest at these raw SDL axis values (-32768 to 32767). | The game sees a controller with that drift or deflection; a malformed value is logged and ignored. | Caution |
| `test_prepare_shaders` | `1` translates every recorded shader on all usable CPUs during start. | Start takes longer and fills the shader cache folder; the CPU is fully used while it runs. | Caution |
@@ -91,6 +93,7 @@ Many rows below are developer tools that only act together with another setting
| --- | --- | --- | --- |
| `amiibo_dir` | Folder the Amiibo tab starts in; the tab saves the folder you last browsed here. | Only changes which folder the tab lists first. | Safe |
| `exefs` | Folder of the game's ExeFS (with `main.npdm`); a relative path is relative to `totk.exe`'s folder. | Without it, an `exefs` folder next to `totk.exe` is used, else the path set at build time. The game code is compiled into `totk.exe`, and the loader checks the files here by SHA-256, so files from another build or game version stop the start with a message. | Caution |
| `menu_font_fontconfig` (default `1`) | On Linux, `1` asks fontconfig which font file draws each menu language's name, and `0` keeps the fixed distribution paths (`/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc` and four more). The menu merges that file into its font atlas. | `0` is for a system whose fontconfig resolves to a font the menu's parser cannot read; the log then says `menu: system font ... left out`. Has no effect on Windows, where the file names are fixed, and none on a language the ASCII font already draws. The atlas is rebuilt when the window size or the menu language changes. `TOTK_MENU_FONT` names a file directly and wins over both. | Safe |
| `mods_dir` (default `mods`) | Folder that holds one subfolder per player mod, beside `totk.exe`. | Mods found there replace game files; see `docs/mods.md`. | Caution |
| `mods_disabled` | Names of mods that are switched off, separated by `\|`. | Listed mods are not applied; the Mods tab writes this line. | Safe |
| `mods_order` | Mod names, top first, separated by `\|`; the top mod wins a file that two mods replace. | Changes which mod's file the game gets; unlisted mods go below the listed ones, by name. | Safe |
@@ -119,9 +122,13 @@ Many rows below are developer tools that only act together with another setting
| Setting | What it does | Effect of changing it | Safety |
| --- | --- | --- | --- |
| `bind_inc` (default `0`) | `1` (or `on`) prepares a draw from what changed since the draw before (the bind ops mark the slots whose bytes they changed) instead of from the whole bound state; `check` prepares every draw as before, takes every decision of `on` beside it, logs each kind of difference once and counts them in a `bind_inc:` line every 300 frames; `0` or `off`: as before. `check` also counts what changed between draws in that line. | Less work per draw on the render thread; a change the marks missed would leave a stale descriptor, a wrong picture or a lost device, which `check` would show first. Off by default: with it on, a GTX 1050 Ti lost the device at one place, after the validation layer reported a descriptor pool reset while a command buffer still used its sets; the cause is not found yet. | Caution |
| `block_views_3d` (default `0`) | `1` gives 3D block-compressed images block views too. | On some AMD cards a GPU write past the end of the image's memory loses the Vulkan device, and the game stops. | Risky |
| `deswizzle` (default `auto`) | Where block-linear textures are deswizzled: `auto` or `gpu` uses a compute pass where the texture's storage is an imported pool and the CPU elsewhere, `cpu` always uses the CPU, `check` uses the GPU and also deswizzles every upload on the CPU and compares the two. | `cpu` brings back the long frames on entering new areas (the replay thread deswizzles every texture); `check` waits for the GPU at every upload and is for testing only. The log names the mode in use. | Caution |
| `dyn_delta` (default `1`) | Records only the pipeline dynamic states (cull mode, depth and stencil tests, vertex input, blend enables, equations and write masks, ...) whose values changed since the last draw in the command buffer, also across pipeline changes; `0` records the full set at every change of pipeline state, as before; `ab` alternates with the `ab` blocks. | Fewer commands per pipeline change on the render and recorder threads; a driver that loses dynamic state across pipeline binds would draw with stale state. `TOTK_TRACE=perf` prints the calls and commands per submission. | Caution |
| `feedback` | `0` samples a target that an earlier draw of the same pass wrote with no barrier; `restart` ends and restarts the pass behind a full barrier; otherwise the feedback-loop layout is used when the GPU supports it. | `0` brings back the old behaviour that the barrier fixed, so the picture can break in passes that read a target they draw into; `restart` costs extra passes. | Risky |
| `flush_span` (default `0`) | `1` copies one span from the lowest to the highest flushed address of a pool, as an older version did. | Copies far more data to the GPU in crowded towns: an estimated 30-50 ms of GPU time per large copy over PCIe 3.0 x8. | Caution |
| `shadow_snap` (default `1`) | Shadow storage (Linux, Wine): `1` syncs the pools from per-submission write-watch snapshots, each 2 MiB region queried at most once per submission and shared with the texture checks; `0` queries each pool at every part of a submission, as before. | `1` cut the replay thread's write tracking from about 4.4 to 1.3 ms a frame on a Steam Deck; `0` is for comparison runs. Under Wine/Proton the default is `0` until it has been measured there; `1` turns it on. No effect where shadow storage is off (Windows by default). | Caution |
| `freeze_idle` (default `60`) | Number of submissions a 2 MiB region stays unwritten before it is made read-only; `0` turns the freeze off. | A larger value keeps more regions writable and costs more write tracking; a write to a frozen region costs about 60 microseconds. | Caution |
| `gpl_draw_build` (default `1`) | `0` leaves the missing shader libraries of a waiting draw to its background job instead of building them in parallel at once. | Draws whose pipeline is not ready are skipped meanwhile, until the job finishes. | Caution |
| `lib_prebuild_keep` (default `0`) | Number of recorded graphics shader library variants that stay in memory after the boot prebuild. | Each kept library costs 140-460 KB of memory; the driver cache already makes a rebuild cheap. | Caution |
@@ -132,10 +139,13 @@ Many rows below are developer tools that only act together with another setting
| `pcss_k` (default `0.5`) | Depth-to-penumbra factor of soft shadows at 100 percent, for tuning. | Changes how wide soft shadows blur; has no effect while `pcss` is `0`. | Caution |
| `pipe_sync` | Any value other than `0` or empty stops graphics pipelines from being built in the background. | Pipelines are built on the render path, which causes stutter when new effects appear. | Caution |
| `push_stage` | `fs` pushes the descriptor set from the fragment stage instead of the vertex stage. | The log names the stage in use; the code does not say what changes in the picture. | Unknown |
| `rec_async` (default `1`) | `0` makes the submitting thread wait until the recorder thread has recorded and submitted each part; `ab` alternates with the `ab` blocks. | With `0` the submitting thread waits for every part (about 0.3 ms a frame in a town on a Ryzen 7900X). | Caution |
| `rec_end` (default `1`) | `0` lets the submitting thread begin, end and submit a recorded part again, as older versions did. | On some AMD cards the GPU hangs or the scene turns green and then white when a submission is split. | Risky |
| `rec_spin_us` (default `60`) | Microseconds the recorder thread polls for the next packets before it sleeps; `0` polls 4096 pauses instead; `ab` polls only in the B blocks of `ab`. With fewer than 6 logical processors it always polls 4096 pauses. | A longer poll saves an OS wake-up per packet and takes more CPU time; a shorter one sleeps sooner. | Caution |
| `tex_vram_pressure` (default `90`) | Percent of the device-local memory budget above which released textures are dropped, 0 to 100; `0` is off. First those the game has overwritten with a later texture and left alone for 30 s, then those that can be uploaded again. | A low value drops textures sooner and re-uploads them; `0` keeps them and lets the card spill into system memory. Also a slider in the menu. | Caution |
| `txd_transpose` (default `1`) | `0` turns off the swap of gradient components for gradient texture samples. | Terrain textures turned across the screen show crawling patches again. | Caution |
| `ub_overlay` (default `1`) | `0` copies each uniform update inside a pass into its buffer at once, ending the pass with a full barrier; `ab` alternates with the `ab` blocks. | With `0` scenes with many updated blocks (the terrain, the sky islands) take longer on the GPU; `TOTK_TRACE=perf` prints how many updates stayed in their pass. | Caution |
| `ub_overlay_out` (default `1`) | `0` copies each uniform update between passes into its buffer at once, behind a full barrier of its own; `ab` alternates with the `ab` blocks. Needs `ub_overlay`. | With `0` a run of updates before a pass costs a full GPU barrier each; `TOTK_TRACE=perf` prints how many updates between passes were overlaid. | Caution |
| `ubo_snapshot` (default `1`) | `0` binds live memory for uniform blocks; `ab` alternates with the `ab` blocks. | Without snapshots late draws read another object's matrix, so objects can be drawn in the wrong place. | Risky |
| `vk_no_float64` (default `0`) | `1` clears the 64-bit float feature, so the translator lowers FP64 to FP32. | Shaders change, so the shader cache entries for it are made again; precision of FP64 shaders drops. | Caution |
| `vk_ssbo_align` (default unset) | Power of two that storage buffer offsets are treated as needing, to test the unaligned path. | The translator and the draw code round storage offsets down to it; shaders are translated again. | Caution |
@@ -170,6 +180,7 @@ Many rows below are developer tools that only act together with another setting
| `mvprev_grow` (default `1`) | `0` keeps only three banks in the previous-frame tables. | Object motion that depends on the other banks is no longer tracked. | Caution |
| `mvprev_skip` | Hex bank mask of banks left out of the previous-frame copy, replacing the default. | Different banks are tracked, which changes object motion. | Caution |
| `mvprev_trust` | Hex bank mask of banks whose found previous-frame copy is taken without the check. | A copy that does not match is used anyway, which can give wrong motion. | Caution |
| `reflex` (default `off`) | `off`, `on`, or `boost`; enables NVIDIA Reflex Low Latency (`VK_NV_low_latency2`) to reduce system input latency. Read at start. | On NVIDIA GPUs supporting the extension, coordinates simulation and presentation cadence to lower latency; `boost` keeps GPU clocks high in CPU-bound scenes. Has no effect on non-NVIDIA GPUs. | Safe |
## Scheduling and host
@@ -177,11 +188,21 @@ Many rows below are developer tools that only act together with another setting
| --- | --- | --- | --- |
| `app_pool` | Size in bytes of the guest application memory pool; accepts decimal or `0x` hex. | The default depends on the resolution settings; `0` stops the start with a message, and too small a pool leaves the game without memory. | Risky |
| `audio_off` | Any value mixes audio without opening an output device. | The game runs in silence and reports two channels; same as `audio = none`. | Caution |
| `kspin_us` (default `20`) | Microseconds a guest wait or a wait for a virtual core polls before it sleeps; `0` turns the poll off; `ab` polls only in the B blocks of `ab`. Off with fewer than 6 logical processors. | A wake within the poll costs no OS wake-up; a longer poll takes more CPU time from other threads. | Caution |
| `mem_reserve_mib` (default `1536`) | MiB of system memory kept free for Windows, other programs and the game's own heap when the renderer sizes what it keeps in device memory, where device memory takes system memory (integrated GPUs, and discrete GPUs whose driver charges it to the process). | A larger value keeps fewer released textures and spare blocks when the system runs short; a smaller one lets the game use more before it holds back. | Caution |
| `one_per_core` (default `0`) | `1` keeps the process on the first logical processor of each physical core; `ab` switches it with the `ab` blocks. | Busy threads stop sharing a core with a sibling thread, which can help or hurt on the CPU; it only applies when the CPU has hyperthreads. | Caution |
| `pcore` (default `1`) | On a hybrid CPU (performance and efficiency cores), the game's main and render threads and the replay, recorder and present threads prefer the performance cores; `0` turns that off. | With `0` Windows may run those threads on efficiency cores; no effect on a CPU with one kind of core. The log says which cores were found. | Caution |
| `reexec` | Counter of address-space retries that the runtime sets itself when it starts the process again. | The counter is cleared when the window is recreated; a value of 20 or more stops the start with a fatal error. | Risky |
| `replay_chunks` (default `0`) | Most chunks a submission is cut into when `replay_workers` is set; `0` uses twice the number of recording threads. | Fewer chunks leave threads idle at the end of a submission, more add scan and submit work per chunk; no effect while `replay_workers` is `0`. | Caution |
| `replay_chunk_draws` (default `128`) | Least draws a chunk of the chunked replay holds before it may end at a pass. | Small values make many short chunks, large ones leave big submissions on few threads; no effect while `replay_workers` is `0`. | Caution |
| `replay_state_check` (default `0`) | `1` compares the replay state after the last chunk of a chunked submission with the state of the scan and logs differences. | Adds a comparison per submission and log lines on a mismatch; no effect while `replay_workers` is `0`. | Caution |
| `replay_skew_us` (default `0`) | Debug: the even chunks of a chunked submission wait this many microseconds before they record, so the chunk after each one records first. | Shows a recording that depends on which thread gets to a chunk first in every submission; slows the replay; no effect with fewer than 2 `replay_workers`. | Caution |
| `restart_wait` | Set by the runtime when it restarts itself; names the old process by id and creation time so the new process waits for it to exit. | Meant for the runtime's own use; the code does not describe a manual value. | Unknown |
| `spin` (default `1`) | `0` makes a guest thread that polls memory keep its virtual core. | Polling threads can starve the thread whose item they wait for, so frames can stall. | Caution |
| `spin_handoff_us` (default `50`) | Microseconds a polling wait lasts before it gives its virtual core to a waiting thread. | A shorter value hands cores over sooner; a longer one keeps them longer. | Caution |
| `tex_block_mib` (default `64`) | Block size in MiB of the pool that holds the game's textures; images of a quarter block or more get memory of their own. `0` puts textures in the default pools with the buffers. | Larger blocks hold more memory that no texture uses; smaller ones mean more allocations from the driver. | Caution |
| `tex_released_mib` (default `256`) | MiB of textures the game released that are kept for reuse; older ones beyond it are dropped. `0` keeps up to 2048 of them, whatever their size. | A larger value keeps more device memory and process commit; a smaller one makes the game create more textures again. | Caution |
| `vma_block_mib` (default `64`) | Block size in MiB that the Vulkan memory allocator takes from the driver for buffers and other images on heaps over 1 GiB; larger allocations than half a block get memory of their own. `0` uses the allocator's 256. | Larger blocks hold more memory that nothing uses; smaller ones mean more allocations from the driver. | Caution |
## Generated-code patches
@@ -189,8 +210,12 @@ These names are code patch sets built into the generated game code (`recomp/patc
| Setting | What it does | Effect of changing it | Safety |
| --- | --- | --- | --- |
| `ability_guard` (default `1`) | Patch set that checks the target a Recall or Ultrahand pick hands to the game (1.4.3 main+0x1bd01c4, 1.2.1 main+0x1622bf0) and drops it for that frame where the game's own code would fault on it: the holder, the actor or its component unreadable, or the word the game's reference writes not writable (`runtime/ov_ability.c`). Every other target goes on unchanged; a target that fails UltraTears' live-actor test is only logged (`ability: target kept although ...`). | `0` runs the game's code as shipped: the random crash reading `0xffffffffffffffff` during an ability pick can return. | Risky |
| `aspect_patch` (default `0`) | Patch set that feeds the camera the output's aspect ratio when it is not 16:9. | The resolution code switches this set on or off itself at start, so a value here is replaced; the code does not settle what a manual value does. | Unknown |
| `fixed_res_patch` (default `0`) | Patch set that holds the game's dynamic resolution at 100 percent (the scene tier). | The resolution code switches this set from `dynamic_res` (on at `0`); a manual value is replaced. | Unknown |
| `fov_patch` (default `0`) | Patch set that scales the camera's vertical view angle by the field-of-view setting. | The resolution code switches this set itself whenever the field of view is not 100 percent; a manual value is replaced. | Unknown |
| `mouse_direct_patch` (default `1`) | Patch set of the hooks in the game's camera code behind `mouse_mode = direct` on game 1.4.3. | `0` stops `mouse_mode = direct` from taking effect; other mouse modes are unaffected. | Safe |
| `optref_guard` (default `1`) | Patch set that treats a negative value in the optional reference at object +0x6b0 (1.4.3 main+0x1bd01c0, 1.2.1 main+0x1622bec) as absent instead of loading through it (#1005). | `0` runs the game's code as shipped: the random crash reading `0xffffffffffffffff` after minutes to hours of play can return. | Risky |
| `patch_output` (default `0`) | Patch set that makes the game use the configured output size for its swapchain and display configuration. | The resolution code switches this set whenever the output differs from the base size; a manual value is replaced. | Unknown |
| `patch_scene` (default `0`) | Patch set that makes the game use the configured scene render size. | The resolution code switches this set from the render size setting; a manual value is replaced. | Unknown |
| `report_guard` (default `1`) | Patch set on 1.4.3 only: in the model scene's report loop (main+0xa22d8, main+0xa2310) an object whose report handle is negative or whose row index is 0x8000 or more gets coverage 0 for that frame and its handle reset, the path the game takes after every read, instead of reading past the report rows. On valid rows the game's code runs unchanged. | `0` runs the game's code as shipped: a bad row index reads far past the rows and crashes, the class Weeping of the Empire met while using Ultrahand and aiming. | Risky |
+1 -1
View File
@@ -619,7 +619,7 @@ class App:
# ---- self test ---------------------------------------------------------------------------------------------------
SAMPLE_START = {'type': 'start', 'release': 'selftest', 'mode': 'install', 'dest': 'Z:/selftest/TotK', 'log': 'D:\\log.txt',
'steps': ['check', 'fonts', 'extract', 'lift', 'compile', 'prepare', 'dlss', 'cleanup']}
'steps': ['check', 'fonts', 'extract', 'lift', 'compile', 'prepare', 'pc_prompts', 'shapes', 'dlss', 'cleanup']}
SAMPLE_RUN = [
SAMPLE_START,
{'type': 'plan', 'run': ['check', 'fonts', 'extract'], 'skip': {}},
+1 -1
View File
@@ -27,7 +27,7 @@ TEXT = Path(__file__).resolve().parent / 'text'
UI_LANGUAGES = ('en', 'ru', 'pt-BR', 'zh-Hans')
# what the language switch in the window's corner shows for each
UI_LANGUAGE_LABELS = {'en': 'English', 'ru': 'Русский', 'pt-BR': 'Português (Brasil)', 'zh-Hans': '简体中文'}
STEP_ORDER = ('check', 'fonts', 'extract', 'lift', 'compile', 'prepare', 'dlss', 'cleanup')
STEP_ORDER = ('check', 'fonts', 'extract', 'lift', 'compile', 'prepare', 'pc_prompts', 'shapes', 'dlss', 'cleanup')
# message parameter -> catalog id prefix of the names that replace its raw values
PARAM_NAMES = {'lang': 'lang.', 'known': 'lang.', 'step': 'step.', 'input': 'param.input.'}
LANG_RU = 0x19 # primary language id of Russian in a Windows LANGID
+174
View File
@@ -0,0 +1,174 @@
"""The screen shapes and button prompts this player needs RomFS patch folders for (steps.Shapes).
A game resolution of another shape than 16:9 takes romfs-patches-<w>x<h>, the UI laid out for that shape
(tools/make_romfs_patches.py --aspect), and the prompts' folder of that shape (romfs-patches-<style>-<w>x<h>). The
layout depends only on the aspect ratio (tools/ultrawide_layout.py apply), so the setup writes one folder per shape,
named after the shape's size in ASPECT_SIZES (shape_size); runtime/romfs.c overlay_init maps the game resolution the
same way (cfg.c cfg_shape_size). It writes them only for the shapes of the connected displays and the totk.ini
output_res, and the prompts' ones only for the style totk.ini selects (prompts_style).
"""
import ctypes
import os
import re
import shutil
import subprocess
from pathlib import Path
# The game resolutions of another shape than 16:9 that runtime/ui/menu.cpp row_output_res lists, in the order of
# runtime/cfg.c SHAPED: 21:9 and 32:9 monitors, 16:10 screens (the Steam Deck's 1280x800, handhelds and laptops) and 3:2
# screens (Surface and Framework laptops and tablets)
ASPECT_SIZES = ('2560x1080', '3440x1440', '3840x1600', '5120x2160', '3840x1080', '5120x1440',
'1280x800', '1680x1050', '1920x1200', '2560x1600',
'1920x1280', '2160x1440', '2256x1504', '2880x1920', '3000x2000')
SHAPED = tuple(tuple(int(v) for v in s.split('x')) for s in ASPECT_SIZES)
TOLERANCE = 0.01 # cfg.c and resolution.c res_is_16x9: shapes within 1% are the same
def is_16x9(w, h):
return abs((w / h) / (16 / 9) - 1) <= TOLERANCE
def shape_size(w, h):
"""The folder size of w x h's shape: of ASPECT_SIZES within 1% of its aspect ratio, the closest, the first of equal
ones (5120x2160 is 2560x1080's shape). None for 16:9 and for a shape the setup has no layout for. The same rule as
runtime/cfg.c cfg_shape_size."""
if not w or not h or is_16x9(w, h):
return None
r, best, err = w / h, None, None
for sw, sh in SHAPED:
e = abs(sw / sh / r - 1)
if e <= TOLERANCE and (err is None or e < err):
best, err = f'{sw}x{sh}', e
return best
def default_size(w, h):
"""The output_res the game picks on a display of w x h while totk.ini sets none (runtime/cfg.c screen_default): of
ASPECT_SIZES within 1% of its shape, the smallest at least 1080 high that fits, else the largest that fits."""
if not w or not h or is_16x9(w, h):
return None
r, best, fit = w / h, None, None
for sw, sh in SHAPED:
if abs(sw / sh / r - 1) > TOLERANCE or sw > w or sh > h:
continue
if fit is None or sw > fit[0]:
fit = (sw, sh)
if sh >= 1080 and (best is None or sw < best[0]):
best = (sw, sh)
best = best or fit
return f'{best[0]}x{best[1]}' if best else None
# ---- the connected displays, as (width, height) of their current mode, the long side first (a portrait monitor or
# the Steam Deck's panel reports its height first; the game window is landscape)
class DISPLAY_DEVICEW(ctypes.Structure):
_fields_ = [('cb', ctypes.c_uint32), ('DeviceName', ctypes.c_wchar * 32), ('DeviceString', ctypes.c_wchar * 128),
('StateFlags', ctypes.c_uint32), ('DeviceID', ctypes.c_wchar * 128), ('DeviceKey', ctypes.c_wchar * 128)]
ATTACHED_TO_DESKTOP = 0x1
ENUM_CURRENT_SETTINGS = -1
DEVMODEW_SIZE = 220 # DEVMODEW: dmSize at 68, dmPelsWidth at 172, dmPelsHeight at 176
DEVMODE_PELS = 172
def windows_displays():
"""Every display attached to the desktop: EnumDisplayDevicesW over the adapters, EnumDisplaySettingsW for the
current mode, which is in pixels whatever the DPI awareness of this process."""
user32 = ctypes.WinDLL('user32')
out, i = [], 0
while True:
dev = DISPLAY_DEVICEW()
dev.cb = ctypes.sizeof(dev)
if not user32.EnumDisplayDevicesW(None, i, ctypes.byref(dev), 0):
break
i += 1
if not dev.StateFlags & ATTACHED_TO_DESKTOP:
continue
mode = ctypes.create_string_buffer(DEVMODEW_SIZE)
ctypes.c_uint16.from_buffer(mode, 68).value = DEVMODEW_SIZE
if user32.EnumDisplaySettingsW(dev.DeviceName, ENUM_CURRENT_SETTINGS, mode):
out.append((ctypes.c_uint32.from_buffer(mode, DEVMODE_PELS).value,
ctypes.c_uint32.from_buffer(mode, DEVMODE_PELS + 4).value))
return out
XRANDR = re.compile(r'\sconnected\s+(?:primary\s+)?(\d+)x(\d+)\+')
def linux_displays():
"""xrandr's connected outputs (X11 and XWayland); without xrandr the preferred mode of each connected DRM
connector."""
if shutil.which('xrandr'):
try:
text = subprocess.run(['xrandr', '--current'], capture_output=True, text=True, timeout=10).stdout
found = [(int(w), int(h)) for w, h in XRANDR.findall(text)]
if found:
return found
except (OSError, subprocess.SubprocessError):
pass
out = []
for conn in sorted(Path('/sys/class/drm').glob('card*-*')):
try:
if (conn / 'status').read_text().strip() != 'connected':
continue
m = re.match(r'(\d+)x(\d+)', (conn / 'modes').read_text())
except OSError:
continue
if m:
out.append((int(m.group(1)), int(m.group(2))))
return out
def displays():
try:
found = windows_displays() if os.name == 'nt' else linux_displays()
except (OSError, AttributeError, ValueError):
found = []
return sorted({(max(w, h), min(w, h)) for w, h in found if w and h})
# ---- totk.ini (runtime/cfg.c reads the same keys, in any section, with or without the totk_ prefix)
def ini_value(text, name):
value = None
for line in text.splitlines():
m = re.match(rf'^\s*(?:totk_)?{name}\s*=\s*([^\s#;]*)', line, re.IGNORECASE)
if m:
value = m.group(1)
return value
def prompts_style(text):
"""The prompts' folder suffix the settings select, as runtime/input_map.c input_prompts_pick and
input_prompts_folder pick it at start: 'pc', 'xbox' (Nintendo pad layout), 'xbox-label' (any other) or None for the
game's own prompts, the default without a totk.ini."""
p, layout = ini_value(text, 'prompts'), (ini_value(text, 'pad_layout') or '').lower()
if p and p.lower() == 'pc':
return 'pc'
if not (p.lower() == 'xbox' if p else layout == 'xbox_prompts'):
return None
return 'xbox' if layout in ('nintendo', 'xbox_prompts') else 'xbox-label'
def wanted(dest, screens=None):
"""(sizes, style): the shape sizes to write layouts for, sorted, and the prompts style. A shape is wanted for each
display's own size and for the output_res the game defaults to there, and for the totk.ini output_res."""
screens = displays() if screens is None else screens
try:
text = (Path(dest) / 'totk.ini').read_text(encoding='utf-8-sig', errors='replace')
except OSError:
text = ''
sizes = set()
for w, h in screens:
w, h = max(w, h), min(w, h)
sizes.add(shape_size(w, h))
d = default_size(w, h)
if d:
sizes.add(shape_size(*(int(v) for v in d.split('x'))))
m = re.fullmatch(r'(\d+)x(\d+)', (ini_value(text, 'output_res') or '').lower())
if m:
sizes.add(shape_size(int(m.group(1)), int(m.group(2))))
sizes.discard(None)
return sorted(sizes, key=ASPECT_SIZES.index), prompts_style(text)
+155 -30
View File
@@ -1,4 +1,5 @@
"""The eight setup steps of docs/installer.md: check, fonts, extract, lift, compile, prepare, dlss, cleanup.
"""The ten setup steps of docs/installer.md: check, fonts, extract, lift, compile, prepare, pc_prompts, shapes, dlss,
cleanup.
Extraction runs in this process through tools/extract.py (the package's sibling tools/, which ships with it); fonts
runs the vendored extract_shared_fonts.py on the firmware folder; lift, compile and prepare run the bundled Python on
@@ -8,13 +9,15 @@ with the bundled clang (hostcheck.c).
Install folder: exefs/, romfs/ (romfs.bin, index.json), profile/, shared-fonts/ (Chinese and Korean text), totk.exe
and its DLLs, nvngx_dlss.dll (with --dlss), romfs-index.bin, romfs-patches/, romfs-patches-xbox/,
romfs-patches-xbox-label/, romfs-patches-pc/, totk_xlate.exe, the
romfs-patches-xbox-label/, romfs-patches-pc/, romfs-patches-<w>x<h>/ and romfs-patches-<style>-<w>x<h>/ (for the
screen shapes and the prompts style this player needs, installer/screens.py), totk_xlate.exe, the
shader state of totk.exe --prepare-shaders, totk.ini, totk-install.json.
Temp folder (<temp>/<dest name>-<hash>): check/ (ExeFS of both inputs, the host check), unpacked/ (the NSP of an NSZ
or XCZ input until the extraction is done), gen/ (C), build/ (objects), logs/. The cleanup keeps gen/, build/ and
logs/: the next run lifts into the same gen/, where recomp/gen.py rewrites only the chunks whose C changed, and Ninja
in build/ recompiles only those. A change of the mods' code cheats then costs the lift (about a minute) and the chunks
it touches, not a full compile.
logs/: the next run lifts into the same gen/, where recomp/gen.py lifts again only the chunks whose inputs changed (its
lift cache, gen/<mod>/lift_cache.json) and rewrites only those whose C changed, and Ninja in build/ recompiles only
those. A change of the mods' code cheats then costs a few seconds of lift and the chunks holding its sites, not a full
lift or compile.
Install folder build-cache/: cheats.status (tools/cheats.py), exefs.status and exefs.data (tools/exefs_patches.py) and
rebuild.txt, the command that starts this setup in its rebuild mode on the install folder (installer/gui.py --rebuild),
@@ -35,7 +38,7 @@ import time
import urllib.request
from pathlib import Path
from . import ini
from . import ini, screens
from .layout import EXE, SUPPORTED, version_tag
from .runner import InstallError, Step
@@ -139,6 +142,17 @@ def linux_kernel():
return (int(m.group(1)), int(m.group(2))) if m else None
def link_output_locked(log):
"""Whether a build failed because another program holds the exe the linker writes: ld.lld cannot write it and clang
cannot remove it (Permission denied). A game started from the temp build folder does that, and so does an
antivirus that flagged or quarantined the new totk.exe there (#1122)."""
try:
text = Path(log).read_text(encoding='utf-8', errors='replace')
except OSError:
return False
return 'failed to write output' in text and 'Permission denied' in text
def run_tool(ctx, step, script, args, parse=None, cwd=None):
"""Run a tool script with the bundled Python. Its output goes to <temp>/logs/<step>-<script>.log and, line by line,
to parse(line). Returns (exit code, log path)."""
@@ -168,7 +182,8 @@ def run_tool(ctx, step, script, args, parse=None, cwd=None):
def free_bytes(path):
"""Free bytes on the disk of path (its nearest existing parent) and that parent's device."""
p = Path(path)
while not p.exists():
# An unavailable drive or share has no existing ancestor: let disk_usage report its OS error at the root.
while not p.exists() and p.parent != p:
p = p.parent
return shutil.disk_usage(p).free, os.stat(p).st_dev
@@ -886,8 +901,9 @@ class Lift(Step):
return (gen / 'layout.json').is_file() and stamp.is_file() and stamp.read_text().strip() == ctx.keys['lift']
def run(self, ctx):
# gen/ of the last run stays: gen.py rewrites only the chunks whose C changed (and removes the ones a smaller
# partition no longer has), so Ninja recompiles only those
# gen/ of the last run stays: gen.py lifts again only the chunks whose inputs changed
# (gen/<mod>/lift_cache.json; TOTK_LIFT_CACHE=0 lifts all), rewrites only those whose C changed (and removes
# the ones a smaller partition no longer has), so Ninja recompiles only those
layout, rep, gen = ctx.layout, ctx.reporter, ctx.temp / 'gen'
(gen / STAMP).unlink(missing_ok=True)
rep.progress('lift', 'discover', force=True)
@@ -1007,6 +1023,8 @@ class Compile(Step):
'--exefs', dest / 'exefs', '--romfs', dest / 'romfs',
'--profile', dest / 'profile'], parse)
exe = build / ('totk' + EXE)
if code and link_output_locked(log):
raise InstallError('compile.exe_locked', path=str(exe), log=str(log))
if code or not exe.is_file():
raise InstallError('compile.failed', code=code, log=str(log))
try:
@@ -1027,12 +1045,7 @@ TRANSLATED = re.compile(r'gfx_shader: prepare: (\d+) of (\d+) pairs')
SKIPPED = re.compile(r'prepare-shaders: (\d+) variants skipped: the graphics driver crashed')
# The game resolutions of another shape than 16:9 that runtime/ui/menu.cpp row_output_res lists: 21:9 and 32:9 monitors,
# 16:10 screens (the Steam Deck's 1280x800, handhelds and laptops) and 3:2 screens (Surface and Framework laptops and
# tablets)
ASPECT_SIZES = ('2560x1080', '3440x1440', '3840x1600', '5120x2160', '3840x1080', '5120x1440',
'1280x800', '1680x1050', '1920x1200', '2560x1600',
'1920x1280', '2160x1440', '2256x1504', '2880x1920', '3000x2000')
ASPECT_SIZES = screens.ASPECT_SIZES
class Prepare(Step):
@@ -1082,24 +1095,14 @@ class Prepare(Step):
dest / 'romfs-patches'])
if code:
raise InstallError('prepare.romfs_patches_failed', code=code, log=str(log))
# The same overlay with the UI laid out for each game resolution of another shape the settings menu offers
# (runtime/romfs.c overlay_init picks the one that matches; the camera's side is the patch set aspect,
# recomp/patches-v<ver>.txt).
for size in ASPECT_SIZES:
remove(dest / f'romfs-patches-{size}')
code, log = run_tool(ctx, 'prepare', layout.tools / 'make_romfs_patches.py',
['--version', version_tag(game_version(ctx)), '--romfs', dest / 'romfs', '--out',
dest / f'romfs-patches-{size}', '--aspect', size])
if code:
raise InstallError('prepare.romfs_patches_failed', code=code, log=str(log))
# The same with Xbox button icons in the prompts, for prompts = xbox (make_romfs_patches.py --prompts):
# romfs-patches-xbox for the Nintendo pad layout, romfs-patches-xbox-label for xbox and auto (runtime/input_map.c
# input_prompts_folder). Both are written on every install, so the menu choices work without another run of
# the setup. Optional: when one fails, the game keeps the Switch prompts with that layout and the menu says the
# files are missing, so the setup warns once and goes on. romfs-patches-pc likewise, with keyboard and mouse
# prompts for prompts = pc (16:9 only: another shape takes make_romfs_patches.py --prompts pc --aspect).
# files are missing, so the setup warns once and goes on. The keyboard and mouse prompts are the PcPrompts
# step's: they follow totk.ini's bindings.
failed = None
for style in ('xbox', 'xbox-label', 'pc'):
for style in ('xbox', 'xbox-label'):
out = dest / f'romfs-patches-{style}'
remove(out)
code, log = run_tool(ctx, f'prepare-{style}', layout.tools / 'make_romfs_patches.py',
@@ -1146,6 +1149,121 @@ class Prepare(Step):
return {'xlate_sha256': file_sha256(xlate)}
# ---- 6b. Keyboard and mouse prompts
def bind_lines(dest):
"""The bind_ lines of the install's totk.ini (the Controls tab writes them), '' without the file."""
try:
text = (dest / 'totk.ini').read_text(encoding='utf-8', errors='replace')
except OSError:
return ''
return '\n'.join(s for s in (line.split('#')[0].split(';')[0].strip() for line in text.splitlines())
if s.lower().startswith('bind_'))
def pc_binds(dest):
"""make_romfs_patches.py's --binds argument for the keyboard and mouse prompts: totk.ini when it has bind_ lines."""
return ['--binds', dest / 'totk.ini'] if bind_lines(dest) else []
class PcPrompts(Step):
"""romfs-patches-pc (16:9), for prompts = pc: the prompts drawn as the keys and mouse buttons of totk.ini's bind_
lines (make_romfs_patches.py --prompts pc --binds), the keyboard defaults without them. Its own step, keyed by those
lines, so running the setup again after a change of the bindings draws the prompts again and leaves the shaders
alone. The shaped romfs-patches-pc-<w>x<h> are the Shapes step's, which follows this key. Optional like the Xbox
prompts: a failure warns, and the game keeps the Switch prompts."""
id = 'pc_prompts'
def key(self, ctx):
layout = ctx.layout
tools = [layout.tools / n for n in ('make_romfs_patches.py', 'ultrawide_layout.py', 'button_icons.py')]
return fingerprint('pc-prompts-1', ctx.keys['extract'], *tools, bind_lines(ctx.dest))
def current(self, ctx, record):
return record.get('failed') or (ctx.dest / 'romfs-patches-pc' / 'manifest.tsv').is_file()
def run(self, ctx):
layout, rep, dest = ctx.layout, ctx.reporter, ctx.dest
rep.progress('pc_prompts', 'draw', force=True)
plain = dest / 'romfs-patches-pc'
remove(plain)
code, log = run_tool(ctx, 'prepare-pc', layout.tools / 'make_romfs_patches.py',
['--version', version_tag(game_version(ctx)), '--romfs', dest / 'romfs', '--out', plain,
'--prompts', 'pc', *pc_binds(dest)])
if code:
remove(plain)
rep.warning('warn.xbox_prompts_failed', code=code, log=str(log))
return {'failed': True}
return {}
# ---- 6b. screen shapes
SHAPED_FOLDER = re.compile(r'^romfs-patches(?:-xbox|-xbox-label|-pc)?-\d+x\d+$')
class Shapes(Step):
"""romfs-patches-<w>x<h> (the UI laid out for a shape, make_romfs_patches.py --aspect) and, with Xbox or PC prompts
in totk.ini, romfs-patches-<style>-<w>x<h> (that layout with those prompts), for the shapes this player needs:
those of the connected displays and of the totk.ini output_res, one folder per shape (installer/screens.py; the
game maps its resolution to the folder in runtime/romfs.c overlay_init). A setup before this step wrote all 15
sizes for every prompts style, 64 folders. The key holds the shapes and the style, so a run of the setup after a
monitor or settings change (or its rebuild mode) writes only these again; the game's settings menu can also start
the generator for the shape in use (runtime/ui/menu.cpp row_output_res)."""
id = 'shapes'
def wanted(self, ctx):
if not hasattr(ctx, 'shapes_wanted'):
ctx.shapes_wanted = screens.wanted(ctx.dest)
return ctx.shapes_wanted
def key(self, ctx):
sizes, style = self.wanted(ctx)
# The pc prompts' shaped folders are drawn from the same bindings as romfs-patches-pc, so they follow its key
prompts = ctx.keys.get('pc_prompts', '') if style == 'pc' else ''
return fingerprint('shapes-2', ctx.keys.get('prepare', ''), prompts, style or 'nintendo', *sizes)
def current(self, ctx, record):
return all((ctx.dest / name / 'manifest.tsv').is_file() for name in record.get('folders', []))
def run(self, ctx):
layout, rep, dest = ctx.layout, ctx.reporter, ctx.dest
sizes, style = self.wanted(ctx)
rep.log(f'shapes: displays {screens.displays()}, layouts for {sizes or "none"}, prompts {style or "nintendo"}')
for old in dest.iterdir() if dest.is_dir() else ():
if old.is_dir() and SHAPED_FOLDER.match(old.name):
remove(old)
version = version_tag(game_version(ctx))
plain = dest / f'romfs-patches-{style}' if style else None
folders, failed = [], None
for i, size in enumerate(sizes):
rep.progress('shapes', 'layouts', i, len(sizes), 'shapes', force=True)
out = dest / f'romfs-patches-{size}'
code, log = run_tool(ctx, 'shapes', layout.tools / 'make_romfs_patches.py',
['--version', version, '--romfs', dest / 'romfs', '--out', out, '--aspect', size])
if code:
raise InstallError('prepare.romfs_patches_failed', code=code, log=str(log))
folders.append(out.name)
# Only the Common archive differs from romfs-patches-<style>: --reuse links the fonts, messages and size
# table from there. Optional like that folder: the game keeps the Switch prompts with this layout.
if not plain or not (plain / 'manifest.tsv').is_file():
continue
out = dest / f'romfs-patches-{style}-{size}'
code, log = run_tool(ctx, f'shapes-{style}', layout.tools / 'make_romfs_patches.py',
['--version', version, '--romfs', dest / 'romfs', '--out', out, '--prompts', style,
'--aspect', size, '--reuse', plain, *(pc_binds(dest) if style == 'pc' else ())])
if code:
remove(out)
failed = failed or (code, log)
else:
folders.append(out.name)
rep.progress('shapes', 'layouts', len(sizes), len(sizes), 'shapes', force=True)
if failed:
rep.warning('warn.xbox_prompts_failed', code=failed[0], log=str(failed[1]))
return {'folders': folders, 'sizes': sizes, 'style': style}
# ---- 7. NVIDIA DLSS
# NVIDIA's DLSS library, which NGX loads from the folder of totk.exe (runtime/vk_dlaa.c). The setup does not ship it:
@@ -1256,8 +1374,15 @@ REBUILD_BOOT = ('import runpy, sys; sys.path.insert(0, sys.argv.pop(1)); '
def rebuild_command(ctx):
"""The command that starts the setup window in its rebuild mode on this install folder (installer/gui.py
--rebuild): pythonw beside the setup's Python on Windows, so no console opens. In a repository that is the Python
running this setup: the tools' embeddable Python there has no tkinter."""
running this setup: the tools' embeddable Python there has no tkinter.
In the Linux AppImage the release folder is the AppImage's mount (/tmp/.mount_*), which goes away when the setup
exits, so a path into it would fail to start later and the game would keep its old code. The command starts the
AppImage file itself instead ($APPIMAGE, set by the AppImage runtime); its AppRun passes the arguments to
installer.gui."""
layout = ctx.layout
appimage = os.environ.get('APPIMAGE', '')
if not layout.dev and appimage and Path(appimage).is_file():
return [str(Path(appimage).resolve()), '--rebuild', str(ctx.dest)]
python = Path(sys.executable) if layout.dev else layout.python
if os.name == 'nt' and python.with_name('pythonw.exe').is_file():
python = python.with_name('pythonw.exe')
@@ -1289,4 +1414,4 @@ def romfs_problem(romfs, expected):
def all_steps():
return [Check(), Fonts(), Extract(), Lift(), Compile(), Prepare(), Dlss(), Cleanup()]
return [Check(), Fonts(), Extract(), Lift(), Compile(), Prepare(), PcPrompts(), Shapes(), Dlss(), Cleanup()]
+5
View File
@@ -5,6 +5,8 @@
"step.lift": "Translating the game code to C",
"step.compile": "Compiling {game_exe}",
"step.prepare": "Preparing the shaders",
"step.pc_prompts": "Drawing the keyboard and mouse prompts",
"step.shapes": "Laying out the game screens for your displays",
"step.dlss": "Downloading NVIDIA DLSS",
"step.cleanup": "Cleaning up",
@@ -29,10 +31,12 @@
"phase.compile.compile": "Compiling",
"phase.compile.link": "Linking {game_exe}",
"phase.prepare.romfs_patches": "Writing the 60 fps game data patches",
"phase.pc_prompts.draw": "Drawing the keyboard and mouse button prompts from totk.ini's bindings",
"phase.prepare.seed": "Reading the shaders from the game data",
"phase.prepare.translate": "Translating the shaders",
"phase.prepare.libraries": "Building the shaders for the graphics driver",
"phase.prepare.pack": "Packing the shader cache into one file",
"phase.shapes.layouts": "Writing the screen layouts and button prompts for each display shape",
"phase.dlss.download": "Downloading nvngx_dlss.dll from NVIDIA's DLSS SDK",
"phase.cleanup.ini": "Writing totk.ini",
"phase.cleanup.temp": "Removing temporary files",
@@ -91,6 +95,7 @@
"lift.failed": "Translating the game code failed (exit code {code}). Details: {log}",
"compile.failed": "Compiling {game_exe} failed (exit code {code}). Details: {log}",
"compile.exe_in_use": "{path} is in use. Close the game and run the installer again.",
"compile.exe_locked": "{path} cannot be written: another program holds it, a game started from that folder or an antivirus that flagged the file. Close the game, add the setup's folder and the install folder to the antivirus exclusions, restart Windows, then run the installer again. Details: {log}",
"prepare.xlate_in_use": "{path} is in use. Close the game, end any totk_xlate process left from an earlier run, then run the installer again.",
"prepare.shader_list_missing": "The shader list is missing.",
"prepare.romfs_patches_failed": "Writing the game data patches failed (exit code {code}). Details: {log}",
+5
View File
@@ -5,6 +5,8 @@
"step.lift": "Traduzindo o código do jogo para C",
"step.compile": "Compilando o {game_exe}",
"step.prepare": "Preparando os shaders",
"step.pc_prompts": "Desenhando os ícones de teclado e mouse",
"step.shapes": "Ajustando as telas do jogo aos seus monitores",
"step.dlss": "Baixando o NVIDIA DLSS",
"step.cleanup": "Finalizando",
@@ -29,10 +31,12 @@
"phase.compile.compile": "Compilando",
"phase.compile.link": "Ligando o {game_exe}",
"phase.prepare.romfs_patches": "Gravando os patches de 60 fps nos dados do jogo",
"phase.pc_prompts.draw": "Desenhando os ícones de teclas e botões do mouse a partir das atribuições do totk.ini",
"phase.prepare.seed": "Lendo os shaders dos dados do jogo",
"phase.prepare.translate": "Traduzindo os shaders",
"phase.prepare.libraries": "Montando os shaders para o driver de vídeo",
"phase.prepare.pack": "Juntando o cache de shaders num único arquivo",
"phase.shapes.layouts": "Gravando os layouts de tela e os botões para cada formato de monitor",
"phase.dlss.download": "Baixando o nvngx_dlss.dll do DLSS SDK da NVIDIA",
"phase.cleanup.ini": "Gravando o totk.ini",
"phase.cleanup.temp": "Removendo os arquivos temporários",
@@ -91,6 +95,7 @@
"lift.failed": "Falha ao traduzir o código do jogo (código de saída {code}). Detalhes: {log}",
"compile.failed": "Falha ao compilar o {game_exe} (código de saída {code}). Detalhes: {log}",
"compile.exe_in_use": "{path} está em uso. Feche o jogo e execute o instalador de novo.",
"compile.exe_locked": "Não é possível gravar {path}: outro programa o está usando, um jogo aberto dessa pasta ou um antivírus que marcou o arquivo. Feche o jogo, adicione a pasta do instalador e a pasta de instalação às exceções do antivírus, reinicie o Windows e execute o instalador de novo. Detalhes: {log}",
"prepare.xlate_in_use": "{path} está em uso. Feche o jogo, encerre qualquer processo totk_xlate que tenha sobrado de uma execução anterior e execute o instalador de novo.",
"prepare.shader_list_missing": "A lista de shaders está faltando.",
"prepare.romfs_patches_failed": "Falha ao gravar os patches nos dados do jogo (código de saída {code}). Detalhes: {log}",
+5
View File
@@ -5,6 +5,8 @@
"step.lift": "Перевод кода игры в C",
"step.compile": "Сборка {game_exe}",
"step.prepare": "Подготовка шейдеров",
"step.pc_prompts": "Рисование подсказок клавиатуры и мыши",
"step.shapes": "Раскладка экранов игры под ваши мониторы",
"step.dlss": "Загрузка NVIDIA DLSS",
"step.cleanup": "Уборка",
@@ -29,10 +31,12 @@
"phase.compile.compile": "Компиляция",
"phase.compile.link": "Компоновка {game_exe}",
"phase.prepare.romfs_patches": "Запись патчей данных игры для 60 кадров/с",
"phase.pc_prompts.draw": "Рисование подсказок клавиш и кнопок мыши по назначениям из totk.ini",
"phase.prepare.seed": "Чтение шейдеров из данных игры",
"phase.prepare.translate": "Трансляция шейдеров",
"phase.prepare.libraries": "Сборка шейдеров для драйвера видеокарты",
"phase.prepare.pack": "Упаковка кэша шейдеров в один файл",
"phase.shapes.layouts": "Запись раскладок экрана и кнопок для каждой формы монитора",
"phase.dlss.download": "Загрузка nvngx_dlss.dll из DLSS SDK от NVIDIA",
"phase.cleanup.ini": "Запись totk.ini",
"phase.cleanup.temp": "Удаление временных файлов",
@@ -91,6 +95,7 @@
"lift.failed": "Не удалось перевести код игры (код выхода {code}). Подробности: {log}",
"compile.failed": "Не удалось собрать {game_exe} (код выхода {code}). Подробности: {log}",
"compile.exe_in_use": "Файл {path} занят. Закройте игру и запустите установку снова.",
"compile.exe_locked": "Файл {path} не удаётся записать: его держит другая программа — игра, запущенная из этой папки, или антивирус, который пометил файл. Закройте игру, добавьте папку установщика и папку установки в исключения антивируса, перезагрузите Windows и запустите установку снова. Подробности: {log}",
"prepare.xlate_in_use": "Файл {path} занят. Закройте игру, завершите процесс totk_xlate, оставшийся от прошлого запуска, и запустите установку снова.",
"prepare.shader_list_missing": "Нет списка шейдеров.",
"prepare.romfs_patches_failed": "Не удалось записать патчи данных игры (код выхода {code}). Подробности: {log}",
+5
View File
@@ -5,6 +5,8 @@
"step.lift": "将游戏代码转换为 C 代码",
"step.compile": "编译 {game_exe}",
"step.prepare": "准备着色器",
"step.pc_prompts": "绘制键盘和鼠标按键提示",
"step.shapes": "为你的显示器调整游戏画面布局",
"step.dlss": "下载 NVIDIA DLSS",
"step.cleanup": "清理",
@@ -29,10 +31,12 @@
"phase.compile.compile": "编译",
"phase.compile.link": "链接 {game_exe}",
"phase.prepare.romfs_patches": "写入 60 帧游戏数据补丁",
"phase.pc_prompts.draw": "根据 totk.ini 中的按键绑定绘制键盘和鼠标按键提示",
"phase.prepare.seed": "从游戏数据中读取着色器",
"phase.prepare.translate": "转译着色器",
"phase.prepare.libraries": "为显卡驱动构建着色器",
"phase.prepare.pack": "将着色器缓存打包成一个文件",
"phase.shapes.layouts": "为每种显示器比例写入画面布局和按键提示",
"phase.dlss.download": "从 NVIDIA 的 DLSS SDK 下载 nvngx_dlss.dll",
"phase.cleanup.ini": "写入 totk.ini",
"phase.cleanup.temp": "删除临时文件",
@@ -91,6 +95,7 @@
"lift.failed": "游戏代码转换失败(退出代码 {code})。详情:{log}",
"compile.failed": "编译 {game_exe} 失败(退出代码 {code})。详情:{log}",
"compile.exe_in_use": "{path} 正在使用中。请关闭游戏后重新运行安装程序。",
"compile.exe_locked": "无法写入 {path}:另一个程序正在占用它,可能是从该文件夹启动的游戏,或标记了该文件的杀毒软件。请关闭游戏,将安装程序文件夹和安装文件夹加入杀毒软件的排除项,重启 Windows,然后重新运行安装程序。详情:{log}",
"prepare.xlate_in_use": "{path} 正在被占用。请关闭游戏,结束上次残留的 totk_xlate 进程,再重新运行安装程序。",
"prepare.shader_list_missing": "缺少着色器列表。",
"prepare.romfs_patches_failed": "写入游戏数据补丁失败(退出代码 {code})。详情:{log}",
+225 -23
View File
@@ -2,13 +2,15 @@
"""Generate C for the ExeFS modules: function discovery, then parallel emission into chunked C files.
Usage: gen.py --exefs build/extracted/v121/exefs --out generated/v121 [--modules main,subsdk0,sdk] [--jobs N]
[--limit N] [--chunk-fns N] [--chunk-bytes B] [--big-fn B] [--patches FILE]
[--limit N] [--chunk-fns N] [--chunk-bytes B] [--big-fn B] [--patches FILE] [--v-locals K [--v-trust]]
[--no-cache] [--fn-enter-all]
Output, under --out:
layout.h, layout.json module bases (recomp/layout.py)
<mod>/<mod>_NNNN.c one `void f_<addr>(Cpu *c)` per guest function, about --chunk-bytes of C per file
<mod>/<mod>_table.c sorted <mod>_fn_addrs[] / <mod>_fn_ptrs[] / <mod>_fn_count for rt_dispatch
<mod>/manifest.json chunk list, function count, statistics and timings
<mod>/lift_cache.json per chunk: the key of its inputs, its file's SHA-256, size and mtime, and its results
<mod>/folded.txt the module's bytes the generated code took as constants: what the jump tables read at
lift time, as module offsets [start, end); tools/exefs_patches.py keeps a mod's data
writes off them
@@ -24,8 +26,20 @@ Emission runs in worker processes, one chunk per task. Each worker loads the mod
time and resolves its jump tables (recomp/jumptables.py), so memory per worker does not grow with the module.
Chunks are sized from a per-instruction estimate of the C they produce, so their names and contents only depend on
the input; a chunk whose text did not change is not rewritten and keeps its mtime.
A chunk is not lifted again when its inputs did not change. Its text depends only on the generator (recomp/*.py,
GENERATED_MARKER, the Emitter options), the module (its file, its base, and the function starts discovered from it),
the chunk's number and functions, and the patch sites, spin waits and function hooks inside the chunk's address
range (and whether the patch table has hooks at all): the per-module caches of recomp/jumptables.py are derived from
the module's bytes and base, and its per-function state is reset for each function. The SHA-256 of all of these is the chunk's key. <mod>/lift_cache.json records the key of each chunk
with the SHA-256, size and mtime of the file it wrote and the results the manifest and folded.txt need, under a
digest of the entry. A chunk whose entry is intact, whose key matches and whose file still has that size, mtime and
hash is reused as it is; any other chunk is lifted. So a rebuild after a change to one cheat lifts only the chunks
holding its sites. --no-cache, or TOTK_LIFT_CACHE=0 in the environment, lifts every chunk.
"""
import argparse
import bisect
import hashlib
import json
import multiprocessing as mp
import os
@@ -33,6 +47,7 @@ import re
import sys
import time
from collections import Counter
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
import numpy as np
@@ -46,6 +61,7 @@ import hostmem # noqa: E402
import jumptables # noqa: E402
import patches # noqa: E402
import regs # noqa: E402
import vlocals # noqa: E402
from check_content import GENERATED_MARKER # noqa: E402
from layout import layout, write as write_layout # noqa: E402
from lifter import hx # noqa: E402
@@ -61,7 +77,7 @@ MUSTTAIL = '__attribute__((musttail)) return'
EST_INT = 62
EST_SIMD = 75
EST_SIMD_LDST = 48
EST_FN = 38 # per function: signature, FN_ENTER, closing brace
EST_FN = 38 # per function: signature, FN_ENTER (recomp/patches.py `hook`), closing brace
EST_BR_LABELS = 10 # per instruction of a function with a `br`: switch cases, or labels if it falls back
@@ -209,15 +225,21 @@ def partition(est, max_fns, max_bytes, big_fn):
# ------------------------------------------------------------------------------------------------ emission
class Emitter:
"""C for the functions of one module. starts is the set of every function start of the module."""
"""C for the functions of one module. starts is the set of every function start of the module. A new input of
the C (an argument here, or anything else it reads) must go into the lift cache's key: chunk_key."""
def __init__(self, mod, starts, patched=None, reg_locals=regs.LOCALS, callee_saved=True, spins=None):
def __init__(self, mod, starts, patched=None, reg_locals=regs.LOCALS, callee_saved=True, spins=None,
v_locals=vlocals.LOCALS, v_trust=False, hooks=None, fn_enter_all=False):
self.mod = mod
self.starts = starts
self.patched = patched or {} # {address: [(set index, [replacement words])]} (recomp/patches.py)
self.spins = spins or {} # {spin wait back-edge: its head} (recomp/patches.py `spin`)
self.reg_locals = reg_locals # recomp/regs.py: registers per function in C locals
self.kept = regs.CALLEE_SAVED if callee_saved else frozenset() # not reloaded after a call
self.v_locals = v_locals # recomp/vlocals.py: V registers per function in C locals (0: all in c->v)
self.v_trust = v_trust # with v_locals: V8-V15 are not reloaded after a call
# Functions that start with FN_ENTER (recomp/patches.py `hook`): None is every function
self.hooks = None if fn_enter_all else hooks
self.uses_patches = False
self.stats = Counter()
self.unsupported = Counter()
@@ -387,6 +409,7 @@ class Emitter:
self.stats['external_branches'] += 1
return f'{publish(i)}{spill}c->pc = {hx(t)}; {MUSTTAIL} rt_dispatch(c);'
enter = [f' FN_ENTER({hx(entry)});'] if self.hooks is None or entry in self.hooks else []
if all_labels:
# An unresolved computed br re-enters the function through fb_<entry>, which switches on c->pc to any
# instruction. A goto to a shared dispatch block inside the body instead turns every instruction into
@@ -395,13 +418,13 @@ class Emitter:
cases = ' '.join(f'case {hx(t)}: goto L_{t:x};' for t in range(entry, end, 4))
redispatch = f'{MUSTTAIL} {fb}(c);'
out = [f'static void {fb}(Cpu *c);',
f'void {fname(entry)}(Cpu *c) {{', f' FN_ENTER({hx(entry)});', f' c->pc = {hx(entry)};',
f'void {fname(entry)}(Cpu *c) {{', *enter, f' c->pc = {hx(entry)};',
f' {redispatch}', '}',
f'static void {fb}(Cpu *c) {{', f' switch (c->pc) {{ {cases} default: break; }}',
f' {MUSTTAIL} rt_dispatch(c);']
else:
redispatch = f'{MUSTTAIL} rt_dispatch(c);'
out = [f'void {fname(entry)}(Cpu *c) {{', f' FN_ENTER({hx(entry)});']
out = [f'void {fname(entry)}(Cpu *c) {{', *enter]
if fp.used:
out.append(' ' + fp.decl())
@@ -504,6 +527,8 @@ class Emitter:
else:
out += [line for _, line in body]
out.append('}')
if self.v_locals:
out = vlocals.rewrite('\n'.join(out), self.v_locals, self.v_trust, self.stats).split('\n')
self.stats['functions'] += 1
self.stats['instructions'] += len(insns)
return out, refs
@@ -521,13 +546,15 @@ def chunk_text(name, number, emitter, fns):
protos = ''.join(f'void {fname(r)}(Cpu *c);\n' for r in sorted(refs))
if emitter.uses_patches:
protos += 'extern uint8_t rt_patch_on[];\n'
return (header(f'{name} chunk {number:04d}') + '#include "cpu.h"\n#include "simd.h"\n\n' + protos + '\n'
+ '\n\n'.join(body) + '\n')
vdef = '#define RT_VLOCALS 1\n' if emitter.v_locals else '' # runtime/simd.h: the V-locals accessors
return (header(f'{name} chunk {number:04d}') + vdef + '#include "cpu.h"\n#include "simd.h"\n\n' + protos
+ '\n' + '\n\n'.join(body) + '\n')
def write_if_changed(path, text):
def write_if_changed(path, text, wait=True):
"""Returns True if the file was written. An unchanged file keeps its mtime, so the build skips it. A changed one
is written under a temporary name and renamed into place, so a reader never sees it half written."""
is written under a temporary name and renamed into place, so a reader never sees it half written. wait: retry
the rename while another process holds the file (_retry); without it the first failure raises."""
data = text.encode()
try:
if path.stat().st_size == len(data) and path.read_bytes() == data:
@@ -537,7 +564,10 @@ def write_if_changed(path, text):
tmp = path.with_name(f'.{path.name}.{os.getpid()}.tmp')
tmp.write_bytes(data)
try:
_retry(os.replace, tmp, path)
if wait:
_retry(os.replace, tmp, path)
else:
os.replace(tmp, path)
except OSError:
tmp.unlink(missing_ok=True)
raise
@@ -587,7 +617,8 @@ def _emitter(name):
mod.read = recorded
e = _W['emitters'][name] = Emitter(mod, set(_W['starts'][name].tolist()),
_W['patches'].for_module(name, base),
spins=_W['patches'].spins_for(name, base), **_W['opts'])
spins=_W['patches'].spins_for(name, base),
hooks=_W['patches'].hooks_for(name, base), **_W['opts'])
return e
@@ -603,8 +634,8 @@ def merge_ranges(ranges):
def _emit_chunk(task):
"""Worker: write one chunk. Returns its statistics."""
name, number, path, fns = task
"""Worker: write one chunk. Returns its statistics, and the SHA-256, size and mtime of its file for the cache."""
name, number, path, fns, key = task
t0 = time.perf_counter()
# Test hook: a pool wider than this raises the MemoryError a busy machine does, so the retry can be exercised.
limit = os.environ.get('TOTK_LIFT_TEST_OOM_ABOVE')
@@ -614,17 +645,20 @@ def _emit_chunk(task):
e.stats.clear()
e.unsupported.clear()
text = chunk_text(name, number, e, fns)
error = None
error, st = None, None
try:
written = write_if_changed(Path(path), text)
st = os.stat(path)
except OSError as ex:
written, error = False, f'{path}: {ex}'
reads = _W['reads'].get(name, [])
folded = merge_ranges(reads)
reads.clear()
return {'module': name, 'number': number, 'file': Path(path).name, 'functions': len(fns), 'bytes': len(text),
'written': written, 'error': error, 'seconds': time.perf_counter() - t0, 'stats': dict(e.stats),
'unsupported': dict(e.unsupported), 'folded': folded}
'written': written, 'reused': False, 'error': error, 'seconds': time.perf_counter() - t0,
'stats': dict(e.stats), 'unsupported': dict(e.unsupported), 'folded': folded, 'key': key,
'sha256': _sha256(text.encode()), 'size': st.st_size if st else None,
'mtime_ns': st.st_mtime_ns if st else None}
# ------------------------------------------------------------------------------------------------ driver
@@ -663,6 +697,122 @@ def remove_stale(moddir, name, nchunks, errors):
return removed
# ------------------------------------------------------------------------------------------------ lift cache
CACHE_FILE = 'lift_cache.json'
CACHE_VERSION = 1 # raise when the cache's format or the meaning of its key changes
# Per-chunk results the cache keeps beside the key: what the manifest, folded.txt and the reuse check need.
CACHED = ('functions', 'bytes', 'stats', 'unsupported', 'folded', 'sha256', 'size', 'mtime_ns')
def _sha256(data):
return hashlib.sha256(data).hexdigest()
def _key(*parts):
"""SHA-256 of JSON-encodable parts."""
return _sha256(json.dumps(parts, separators=(',', ':')).encode())
def generator_key(opts):
"""Key of everything the chunk text depends on besides the module and the patches: the generator's sources
(recomp/*.py, not the patch tables beside them), the marker in each header, the Emitter options, the Python
version that runs them."""
srcs = [[p.name, _sha256(p.read_bytes())] for p in sorted((ROOT / 'recomp').glob('*.py'))]
return _key('gen', CACHE_VERSION, list(sys.version_info[:2]), GENERATED_MARKER, sorted(opts.items()), srcs)
def module_key(gen_key, name, path, base, abs_starts):
"""Key of one module's inputs: the generator, the module's file and base, and the function starts discovered
from them (they decide which branches become direct calls)."""
return _key(gen_key, name, _sha256(Path(path).read_bytes()), base, _sha256(abs_starts.tobytes()))
def in_span(addrs, lo, hi):
"""The addresses of the sorted list addrs in [lo, hi)."""
return addrs[bisect.bisect_left(addrs, lo):bisect.bisect_left(addrs, hi)]
def chunk_key(mod_key, number, fns, patched, site_addrs, spins, spin_addrs, hook_addrs):
"""Key of one chunk: its module, number (in its header) and functions, and the patch sites (set index and
replacement words, @page resolved, so the patch page is in them), spin waits and hooked functions inside its
address range; hook_addrs None (FN_ENTER in every function) keys apart from an empty list. Every input of the
chunk text that is not in generator_key or module_key must be added here, with a case in
tests/lift_cache_test.py."""
lo, hi = fns[0][0], fns[-1][1]
sites = [[a, [[k, words] for k, words in patched[a]]] for a in in_span(site_addrs, lo, hi)]
hooks = None if hook_addrs is None else in_span(hook_addrs, lo, hi)
return _key(mod_key, number, fns, sites, [[b, spins[b]] for b in in_span(spin_addrs, lo, hi)], hooks)
def _entry_check(file, entry):
"""Digest of a cache entry: its chunk file name, key and results. The results go into the manifest and folded.txt
as they are, so a changed value in the cache file must not be taken (tools/exefs_patches.py --folded keeps a
mod's data writes off the folded bytes)."""
return _key(file, entry['key'], [entry[k] for k in CACHED])
def load_cache(moddir):
"""{chunk file name: entry} from <mod>/lift_cache.json: the entries whose digest matches; empty when the file is
missing or unreadable."""
try:
data = json.loads((moddir / CACHE_FILE).read_text())
if data.get('version') != CACHE_VERSION or not isinstance(data.get('chunks'), dict):
return {}
out = {}
for file, e in data['chunks'].items():
try:
if e['check'] == _entry_check(file, e):
out[file] = e
except (KeyError, TypeError):
pass
return out
except (OSError, ValueError, AttributeError):
return {}
def _intact(path, entry):
"""True when the file at path still is the one the entry recorded: same size and mtime, then same SHA-256 (a bit
flipped in a kept file would otherwise stay forever, as tools/build_game.py check_objects says of objects)."""
try:
st = path.stat()
if st.st_size != entry['size'] or st.st_mtime_ns != entry['mtime_ns']:
return False
return _sha256(path.read_bytes()) == entry['sha256']
except (OSError, KeyError, TypeError):
return False
def reuse(tasks, caches):
"""The results of the tasks whose chunk can be reused: its recorded key equals the task's and its file is intact.
The files are hashed on threads (hashlib releases the GIL), about 1 GB in all for the three modules."""
cands = []
for name, number, path, fns, key in tasks:
e = caches.get(name, {}).get(Path(path).name)
if e is not None and e['key'] == key:
cands.append((name, number, Path(path), key, e))
with ThreadPoolExecutor(min(16, os.cpu_count() or 4)) as ex:
ok = list(ex.map(lambda c: _intact(c[2], c[4]), cands))
out = []
for (name, number, path, key, e), good in zip(cands, ok):
if good:
out.append({'module': name, 'number': number, 'file': path.name, 'written': False, 'reused': True,
'error': None, 'seconds': 0.0, 'key': key, **{k: e[k] for k in CACHED}})
return out
def cache_text(name, results):
"""<mod>/lift_cache.json for this run's chunks; a chunk that could not be written gets no entry. Keys keep their
order (no sort_keys), so a reused chunk's `unsupported` merges into the census as it did when it was lifted."""
chunks = {}
for r in results:
if not r['error'] and r['size'] is not None:
e = {'key': r['key'], **{k: r[k] for k in CACHED}}
chunks[r['file']] = {**e, 'check': _entry_check(r['file'], e)}
return json.dumps({'module': name, 'marker': GENERATED_MARKER, 'version': CACHE_VERSION, 'chunks': chunks},
separators=(',', ':')) + '\n'
# Peak memory of one emit worker on 1.4.3 is 1.3 GiB (1.5 GiB with margin); the parent holds about 0.7 GiB.
WORKER_MEM = 1536 << 20
MEM_SHARE = 0.8 # of the free memory the workers may take, the rest is for the system and other programs
@@ -705,8 +855,25 @@ def main():
ap.add_argument('--no-callee-saved-trust', action='store_true',
help='reload X19-X29 and SP after every call too, instead of trusting AAPCS64 (slower; for '
'telling a broken callee from a generator bug)')
ap.add_argument('--v-locals', type=int, default=vlocals.LOCALS,
help='V registers per function kept in C locals that clang holds in XMM registers, the most used '
f'ones (recomp/vlocals.py; default {vlocals.LOCALS}, 0: all in the Cpu)')
ap.add_argument('--v-trust', action='store_true',
help='with --v-locals: do not reload V8-V15 after a call (AAPCS64: the callee preserves their low '
'64 bits)')
ap.add_argument('--fn-enter-all', action='store_true',
help='FN_ENTER (runtime/cpu.h) in every function, also with hook lines in the patch table '
'(recomp/patches.py `hook`): a whole-game function-entry profile')
ap.add_argument('--no-cache', action='store_true',
help=f'lift every chunk, also those whose inputs match <mod>/{CACHE_FILE} (it is written anew); '
'so does the environment variable TOTK_LIFT_CACHE=0')
args = ap.parse_args()
opts = {'reg_locals': args.reg_locals, 'callee_saved': not args.no_callee_saved_trust}
# The installer's lift step passes no --no-cache; the environment reaches it, as TOTK_LIFT_JOBS does (pick_jobs)
no_cache = ('--no-cache' if args.no_cache else
'TOTK_LIFT_CACHE=0' if os.environ.get('TOTK_LIFT_CACHE', '').strip() == '0' else None)
opts = {'reg_locals': args.reg_locals, 'callee_saved': not args.no_callee_saved_trust, 'v_locals': args.v_locals,
'v_trust': args.v_trust, 'fn_enter_all': args.fn_enter_all}
gen_key = generator_key(opts)
t0 = time.perf_counter()
mods = layout(args.exefs)
@@ -734,9 +901,19 @@ def main():
chunks = partition(est[:n], args.chunk_fns, args.chunk_bytes, args.big_fn)
moddir = args.out / name
moddir.mkdir(parents=True, exist_ok=True)
mkey = module_key(gen_key, name, path, base, abs_starts)
patched, spins = table.for_module(name, base), table.spins_for(name, base)
site_addrs, spin_addrs = sorted(patched), sorted(spins)
hooks = None if args.fn_enter_all else table.hooks_for(name, base)
stray = sorted(set(hooks or ()) - set(abs_starts.tolist()))
if stray:
raise SystemExit(f'{args.patches}: hook {", ".join(f"{name}+{a - base:#x}" for a in stray)} is no function '
f'start of {name}')
hook_addrs = None if hooks is None else sorted(hooks)
for number, (i0, i1) in enumerate(chunks):
fns = list(zip(abs_starts[i0:i1].tolist(), ends[i0:i1].tolist()))
tasks.append((int(est[i0:i1].sum()), (name, number, str(moddir / f'{name}_{number:04d}.c'), fns)))
key = chunk_key(mkey, number, fns, patched, site_addrs, spins, spin_addrs, hook_addrs)
tasks.append((int(est[i0:i1].sum()), (name, number, str(moddir / f'{name}_{number:04d}.c'), fns, key)))
paths[name] = (str(path), base)
starts_abs[name] = abs_starts
info[name] = {'base': base, 'lo': mod.lo, 'hi': mod.hi, 'emitted': abs_starts[:n].tolist(),
@@ -755,9 +932,23 @@ def main():
ctx = mp.get_context('spawn')
jobs, why = pick_jobs(args.jobs)
pending = [t for _, t in tasks]
emitted = 0
while True:
print(f'emitting with {jobs} workers ({why})', file=sys.stderr, flush=True)
reused = [] if no_cache else reuse(pending, {name: load_cache(d['moddir']) for name, d in info.items()})
for r in reused:
results[r['module']].append(r)
folded[r['module']] += r['folded']
skip = {(r['module'], r['number']) for r in reused}
pending = [t for t in pending if (t[0], t[1]) not in skip]
emitted = len(reused)
print(f'reusing {len(reused)} of {len(tasks)} chunks (inputs unchanged), {time.perf_counter() - t_emit:.1f}s'
if not no_cache else f'lifting every chunk ({no_cache})', file=sys.stderr, flush=True)
if reused or not pending:
# installer/steps.py EMITTED reads these lines for its progress bar: the reused chunks count as done, and a
# run with nothing to lift still reaches M/M
print(f'emitted {emitted}/{len(tasks)} chunks, 0s', file=sys.stderr, flush=True)
if len(pending) < jobs:
jobs, why = max(1, len(pending)), f'{len(pending)} chunk{"s" if len(pending) != 1 else ""} to lift'
while pending:
print(f'emitting with {jobs} worker{"s" if jobs != 1 else ""} ({why})', file=sys.stderr, flush=True)
try:
# Chunk writes are atomic and idempotent, so a chunk lost with a failed pool is simply emitted again.
# Its folded bytes come with its result: a chunk emitted again reads them again in its new worker.
@@ -827,7 +1018,9 @@ def main():
'c_bytes': sum(r['bytes'] for r in rs),
'largest_chunk_bytes': max((r['bytes'] for r in rs), default=0),
'chunks_written': sum(r['written'] for r in rs) + table_written,
'chunks_reused': sum(r['reused'] for r in rs),
'stale_chunks_removed': removed,
**{k: v for k, v in sorted(stats.items()) if k.startswith('v_')}, # --v-locals only
},
'unsupported_census': dict(unsupported.most_common(50)),
'timings': {
@@ -838,8 +1031,17 @@ def main():
},
}
write_if_changed(moddir / 'manifest.json', json.dumps(manifest, indent=1) + '\n')
# The cache goes last, after every output it describes. It only saves time, so a failed write warns and the
# lift still succeeds, without waiting for a holder of the file: the previous cache stays, and its entries
# for the chunks this run rewrote no longer match their files' size, mtime and hash. A run stopped before
# here leaves it the same way.
try:
write_if_changed(moddir / CACHE_FILE, cache_text(name, rs), wait=False)
except OSError as ex:
print(f'{name}: {CACHE_FILE} not written, the next lift reuses less: {ex}', file=sys.stderr, flush=True)
s = manifest['stats']
print(f'{name}: {s["emitted_functions"]} functions, {len(rs)} chunks, {s["c_bytes"] / 1e6:.0f} MB of C, '
print(f'{name}: {s["emitted_functions"]} functions, {len(rs)} chunks ({s["chunks_reused"]} reused), '
f'{s["c_bytes"] / 1e6:.0f} MB of C, '
f'{s["jump_tables"]} jump tables, {s["all_label_fallbacks"]} all-label fallbacks, '
f'{s["br_dispatch"]} br via dispatch, {s["traps"]} traps ({s["unsupported"]} unsupported)',
file=sys.stderr)
+2 -1
View File
@@ -352,7 +352,8 @@ class Lifter:
dst = {
(3, 3, 13, 0, 2): 'c->tpidr = {v};',
(3, 3, 4, 2, 0): 'c->nzcv = {v} & 0xf0000000ull;',
(3, 3, 4, 4, 0): 'c->fpcr = {v} & 0x7c00000ull; rt_fpcr_sync(c);' if self.sc else 'rt_set_fpcr({v});',
(3, 3, 4, 4, 0): ('c->fpcr = {v} & 0x7c00000ull; rt_fpcr_sync(c); RT_FPCR_NOTE(c, c->x[30]);' if self.sc
else 'rt_set_fpcr({v});'),
(3, 3, 4, 4, 1): 'c->fpsr = {v} & 0xf800009full;' if self.sc else 'rt_set_fpsr({v});',
}.get(key)
if dst is None:
+48
View File
@@ -10,6 +10,10 @@ set cloth TOTK_CLOTH_REALTIME 1
set bounce TOTK_HIGHFPS_FIX 1
set output TOTK_PATCH_OUTPUT 0
set scene TOTK_PATCH_SCENE 0
set shadow TOTK_PATCH_SHADOW 0
set cascade TOTK_PATCH_CASCADE 0
# Load-dependent quality (patches-v121.txt explains it): no sites found in this version yet, so the setting does nothing.
set steady TOTK_LOD_STEADY 0
# 60 fps: the adaptive present-interval controller (main+0x8f26c8, 1.0.0 0x8f6bec) gets the minimum interval 1.
site fps60 main 90e978 6b08015f 7100055f # clamp: cmp w10, #1 (was cmp w10, w8 = minimum)
@@ -66,6 +70,50 @@ site scene main cea5e0 5e21d900 @page:x9 # scvtf s0, s8 -> adrp x9, page
site scene main cea5e8 bd0cc660 2d420520 # str s0, [x19, #0xcc4] -> ldp s0, s1, [x9, #16]
site scene main cea5ec 5e21d921 bd0cc660 # scvtf s1, s9 -> str s0, [x19, #0xcc4]
# Equipment-switch cooldown (issue #18): the player update tests state +0x109c against FLT_EPSILON,
# while the equipment gate (main+0x7f0134) blocks changes above zero. Step every positive remainder through
# main+0xa26bb0, including (0, FLT_EPSILON], so a measured frame ratio cannot leave equipment locked.
site realtime main a20518 1e270101 1e2703e1 # fmov s1, w8 (FLT_EPSILON) -> fmov s1, wzr
# Cursor and aim steps (MacNativeGames game_patches.c patch_cursor_speed, 363a6eb): use the same 30 Hz ratio
# already read by these updates for smoothing. x9 / s16 are scratch at the map site; x20 holds the aim clock.
set cursor TOTK_HIGHFPS_FIX 1
site cursor main f31e80 bd400361 f94002e9 bd400930 1e300800 bd400361 # map pan s0 *= [[x23] + 8]
site cursor main 1d55f24 1e2a198c 1e2a198c bd400a90 1e30098c # vertical ability aim s12 *= [x20 + 8]
site cursor main 1d556a4 1e200923 1e200923 bd400a90 1e300863 # vertical gyro aim s3 *= [x20 + 8]
# Camera aspect (Hor+): two camera projection updates and the two copies of the viewport projection routine.
# The camera sites were matched against 1.0.0 by instruction windows; both viewport copies match the same
# 193-word normalized window. Page +56 is set by runtime/resolution.c. Save x10 instead of assuming it dead.
set aspect TOTK_ASPECT_PATCH 0
site aspect main cdbf30 bd414260 f81f0fea @page:x10 bd403940 f84107ea
site aspect main 1261a64 bd414260 f81f0fea @page:x10 bd403940 f84107ea
site aspect main 19c2260 1e211800 f81f0fea @page:x10 bd403940 f84107ea
site aspect main 1ed8fa4 1e211800 f81f0fea @page:x10 bd403940 f84107ea
# Field of view: load the camera's vertical angle as before and multiply by page +60. The following projection
# setup overwrites s8 before reading it; s1 (the half-angle multiplier) stays intact. x10 is saved on the stack.
set fov TOTK_FOV_PATCH 0
site fov main cdbef8 bd413e60 f81f0fea @page:x10 bd413e60 bd403d48 1e280800 f84107ea
site fov main 1261a2c bd413e60 f81f0fea @page:x10 bd413e60 bd403d48 1e280800 f84107ea
# Texture pool offsets above 2 GiB (MacNativeGames game_patches.c patch_pool_offset, ca311e2d): the loader
# passes w25 to SetStorage and SetPackagedTextureData. Widen unsigned to cover the complete 32-bit pool offset.
set pooloff TOTK_POOL_OFFSET_PATCH 1
site pooloff main 9b93a8 93407f22 2a1903e2
site pooloff main 9b93c4 8b39c101 8b394101
# Sun shadow map: the size rounded here is used for width (+0x3d8), height (+0x3f8) and viewport together.
# runtime/resolution.c shadow_init supplies the fixed size at page +92; the default remains the game's size.
site shadow main d0760c 11001d6b @page:x11 b9405d6b 11001d6b
# Sun shadow cascade parameter defaults: depth_shadow_near_0_1..3 and depth_shadow_far_0. The constructor
# stores 20, 40, 80 and 160 metres; runtime/resolution.c res_shadow_range supplies page +64..+76.
site cascade main 1157fb8 52a83408 @page:x8 b9404108
site cascade main 1158040 52a84408 @page:x8 b9404508
site cascade main 11580c8 52a85408 @page:x8 b9404908
site cascade main 115814c 52a86408 @page:x8 b9404d08
# Spin waits, from tools/spin_waits.py --exefs build/extracted/v110/exefs --patches recomp/patches-v110.txt: loops that only
# another thread can end; their back-edges pass rt_spin_gate (runtime/kernel.c), TOTK_SPIN=0 keeps the core.
+75
View File
@@ -10,6 +10,7 @@ set bounce TOTK_HIGHFPS_FIX 1
set output TOTK_PATCH_OUTPUT 0
set scene TOTK_PATCH_SCENE 0
set shadow TOTK_PATCH_SHADOW 0
set grass TOTK_PATCH_GRASS 0
# 60 fps: the adaptive present-interval controller (main+0x8f26c8, 1.0.0 0x8f6bec) gets the minimum interval 1.
site fps60 main 8f273c 6b08015f 7100055f # clamp: cmp w10, #1 (was cmp w10, w8 = minimum)
@@ -55,6 +56,21 @@ site cloth main 8222f0 9401d7cf @page:x8 bd400104 1e240842 1e240863 9401d7cf #
# 100; compare +0x28 * +0x38 (30 / time scale) instead, the same value at 30 Hz and independent of the frame rate.
site bounce main bda798 bd402900 bd402900 bd403901 1e210800 # ldr s0, [x8, #0x28]; ldr s1, [x8, #0x38]; fmul s0, s0, s1
# Climbing (issues #35, #276, #1001): the climb controller's velocity step (main+0x1918220, the same code as 1.4.3
# main+0x1f49754) multiplies the requested velocity by a factor that makes up for a movement loss growing with the
# frame rate: 1.272 - 0.362 (r - 1) clamped to [0.5, 1.5] (r = VFR +0, the 30 Hz ratio times the time scale). Right at
# 30 fps, 78% of the speed at 60 and 30% at 240. Below r = 1 use 1.4.3's factor, 1.272 + 1.189 (1 - r) at most 2
# (Nintendo's 60 fps fit), and below r = 0.5 the 1.4.3 set climb's g(r) = 0.66265 / r + 0.4982 + 0.0848 r, at most 8.
# Without flags: h = max(min(1.4.3 factor, 2), g - 16 max(r - 0.5, 0)), s0 = max(s0, h), s1 = max(1.5, h), and the
# following fmin s1, s0, s1 gives max(min(s0, 1.5), h). For r >= 1 h is at most the shipping value, so 30 fps and
# slower keep the factor bit for bit. x8 is dead after the load at 0x19182c0; s5-s7 and s16 are written before any
# later read in this function.
# g takes u = max(page +0, r): the patch page's 30 Hz frame ratio without the time scale (runtime/frame_rate.c), so
# slow motion at 30 or 60 fps keeps the shipping factor and only a real frame rate above 60 raises it; the runtime
# switches the set off without the real-time step, where the page is not written.
set climb TOTK_CLIMB_FIX 1
site climb main 19182cc 1e2f1001 @page:x8 bd400111 1e296a31 1e2e1005 1e3138a5 52862508 72a7e308 1e270106 1e2608a5 1e2528a5 529a1ca8 72a7f448 1e270106 1e2628a5 1e201006 1e2678a5 52946dc8 72a7e528 1e270106 1e3118c6 52828248 72a7dfe8 1e270107 1e2728c6 529573e8 72a7b5a8 1e270107 1f1118e6 1e283a27 2f00e410 1e3068e7 1e261010 1f1098e6 1e241010 1e3078c6 1e2668a5 1e256800 1e2f1001 1e256821 # fmov s1, #1.5 -> s5 = h(r); s0 = max(s0, s5); s1 = max(1.5, s5)
# Cursor and aim steps (upstream game_patches.c patch_cursor_speed, 363a6eb). These updates read the game's 30 Hz frame
# ratio for their smoothing but leave it out of the step itself, so the map cursor and the vertical Ultrahand aim moved
# 2x as fast at 60 fps and 4x at 120 as at the shipping 30 fps (issue #44). Each site multiplies the step by that ratio,
@@ -65,6 +81,22 @@ site cursor main ec3d9c bd400361 f94002e9 bd400930 1e300800 bd400361 # map pan
site cursor main 1d48d64 1e2a198c 1e2a198c bd400a90 1e30098c # fdiv s12, s12, s10, then s12 *= [x20 + 8]: vertical Ultrahand aim
site cursor main 1d484e4 1e200923 1e200923 bd400a90 1e300863 # fmul s3, s9, s0, then s3 *= [x20 + 8]: vertical gyro aim (Recall)
# Gear rotation check (issues #330, #984). The update of ActorCheckRotateModule (main+0x21350c0; vtable +0x110, called
# by the AI node dispatchers main+0x11f5a40 / 0x2adbcf8 with x1 = the update context) reports a gear or water wheel as
# turning when the angle it turned since the previous update is above RotateStartDeg / 30, the angle of one 30 Hz
# frame. Above 30 fps a gear turns less per update than that, reads as stopped, and the shrine doors that wait on it
# (Riogok, Turakamik, the Wind Temple) never open. 1.4.3 (main+0x273d080) divides the angle by the context's +8, the
# frame delta in 30 Hz frames (the AI root sets it from its owner's virtual +0x30, main+0x106acc4, and the frame
# counter node SelectorGenericFrameCounter, main+0x10b66f8, adds it up per update). 1.2.1 gets the same context and
# ignores it: the update overwrites x1 with its first instruction. The first site keeps ctx+8 in s12 (the prologue has
# saved d12, the function first writes it at the second site, and callees preserve it); the second multiplies the
# threshold by it, which is 1.4.3's test with both sides multiplied by the delta. s4 is free after the fdiv: the next
# read follows a write in the callee main+0x11c2014. The context follows the measured step with the set realtime and is
# the whole-vsync ratio without it (0.25 at 120 fps, 0.5 at 60 fps measured), so the set works either way.
set gears TOTK_HIGHFPS_FIX 1
site gears main 21350e0 f9402001 bd40082c f9402001 # ldr x1, [x0, #0x40], after ldr s12, [x1, #8] (frame delta)
site gears main 21352d0 1e24180c 1e241804 1e2c088c # fdiv s12, s0, s4 -> fdiv s4, s0, s4; fmul s12, s4, s12
# Output size (upstream games/totk/runtime/game_patches.c patch_output_res; runtime/resolution.c switches the set on).
# The swapchain size nnMain writes before NvInitializeGraphics comes from page +48 ({w, h} as one 64-bit value), and
# the output size the renderer lays out its canvas, crop and UI in (main+0xcc3f00, read from the graphics parameters
@@ -81,6 +113,14 @@ site scene main cc3f68 5e21d900 @page:x9 # scvtf s0, s8 -> adrp x9, page
site scene main cc3f70 bd0cc660 2d420520 # str s0, [x19, #0xcc4] -> ldp s0, s1, [x9, #16]
site scene main cc3f74 5e21d921 bd0cc660 # scvtf s1, s9 -> str s0, [x19, #0xcc4]
# Fixed scene size (MacNativeGames game_patches_v143.c patch_fixed_res, 1.4.3 main+0x5848a8; runtime/resolution.c
# res_dynres_live switches the set on unless TOTK_DYNAMIC_RES=1). The game's dynamic resolution controller
# (main+0xcc26f4, called once per frame from the renderer at main+0xcc0fa0) steps the scene through tiers of 100, 90 and
# 80 % by a cost model that grows with the scene width squared (runtime/resolution.c res_dynres_live), and the renderer stores scene width * tier / output width at +0xccc (main+0xcc1150).
# Every path leaves through one epilogue, where the tier's scale moves into the result: 1.0 there makes every tier 100 %.
set fixres TOTK_FIXED_RES_PATCH 0
site fixres main cc2c00 2a1703e9 52a7f009 # mov w9, w23 -> mov w9, #0x3f800000 (1.0f)
# Camera aspect ratio for a game resolution that is not 16:9 (upstream game_patches.c, the 1.2.1 sites of aspect_install;
# runtime/resolution.c res_init writes the output's width / height to page +56 and switches the set on). The same four
# sites as patches-v143.txt, with the same original words: two camera sites (update and setup) load the camera's copy of
@@ -103,6 +143,17 @@ set pooloff TOTK_POOL_OFFSET_PATCH 1
site pooloff main ad43f0 93407f22 2a1903e2 # sxtw x2, w25 -> mov w2, w25 (SetStorage offset)
site pooloff main ad440c 8b39c101 8b394101 # add x1, x8, w25, sxtw -> add x1, x8, w25, uxtw (packaged data source)
# Load-dependent quality, after the emulator mod "Disable LOD Quality Reduction" (main-1.2.1.pchtxt), which turns a
# conditional branch into an unconditional one; the generator patches only plain instructions (recomp/gen.py, the
# emitter's patch), so the site feeds the branch instead. The graphics settings setup (main+0xc7e9xx, it also reads the
# FSR and FXAA switches) walks a
# list at its +0x120 and passes three floats of each entry to virtual setters, which the mod describes as the drop of
# model and texture detail when the frame rate falls; it skips all of it, to the epilogue, when the value it reads at
# main+0xc7eb24 is above 8. Reading 9 there skips it as the mod does; w21 is not read again on that path (the epilogue
# restores it).
set steady TOTK_LOD_STEADY 0
site steady main c7eb24 b9400815 52800135 # ldr w21, [x0, #8] -> mov w21, #9 (b.gt 0xc7ee4c taken)
# Direct mouse camera (runtime/mouse_direct.c; the same hooks as patches-v143.txt, mapped by enclosing function to the
# 1.2.1 main, each original word checked). The camera input update (main+0xd18088, 1.4.3 0x874c1c) after its dead zone,
# clamp and StickExponent keeps x in s11 (1.4.3 s9); the host callback performs the displaced store and adds raw mouse
@@ -1076,3 +1127,27 @@ spin sdk 3d3250 3d3270 # 9 instructions; loads
# goes in just before the rounding, as the community's shadow resolution mods do (1.1.0: mov w11, #1024 over the
# computation). Found by a write watch on the size field (the writer was this function, and no other).
site shadow main cec22c 11001d6b @page:x11 b9405d6b 11001d6b # add w11, w11, #7 -> adrp x11, page; ldr w11, [x11, #92]; add w11, w11, #7
# Grass range (runtime/resolution.c res_grass_range; patches-v143.txt describes the set and the page). The near grass
# map's constructor loads its extent, step and texture sizes at main+0xfe58b4..0xfe5950, and main+0x1a30a28 sets the
# extent to 62.5 or 31.25 m, as on 1.4.3.
site grass main fe58b4 52a84f48 @page:x8 b9408108 # mov w8, #62.5 -> ldr w8, [page+128]
site grass main fe58c0 52a80f48 @page:x8 b9408508 # mov w8, #3.90625 -> ldr w8, [page+132]
site grass main fe58ec 52802002 @page:x2 b9408842 # mov w2, #0x100 -> ldr w2, [page+136]
site grass main fe5950 52801002 @page:x2 b9408c42 # mov w2, #0x80 -> ldr w2, [page+140]
site grass main 1a30a8c 52a84f48 @page:x8 b9408108 # mov w8, #62.5 -> ldr w8, [page+128]
site grass main 1a30a98 52a83f48 @page:x8 b9409108 # mov w8, #31.25 -> ldr w8, [page+144]
# Optional reference with an invalid value (#1005). The same guard as set optref in patches-v143.txt (main+0x1bd01c0),
# here for 1.2.1's main+0x1622bec: ldr x8, [x19, #0x6b0]; cbz x8; ldr x21, [x8]. A negative value counts as absent.
# x8 and x9 are dead at the branch target 0x1622c1c, which loads x8 again.
set optref TOTK_OPTREF_GUARD 1
site optref main 1622bec b4000188 937ffd09 8a290108 b4000188 # cbz x8 -> asr x9, x8, #63; bic x8, x8, x9; cbz x8
# Recall and Ultrahand target that is stale memory: set ability in patches-v143.txt, here for 1.2.1's main+0x1622bf0
# (function main+0x1622b60; the callees main+0x809ecc, 0x842aa4 and 0x842d10 read the same actor offsets). The model
# scene report-row guard (set report there) has no 1.2.1 site: this version does not have that loop's code shape.
set ability TOTK_ABILITY_GUARD 1
site ability main 1622bf0 f9400115 94c424dc # ldr x21, [x8] -> bl page+0xf60
site ability main 1622bf4 b4000155 aa0903f5 b4000155 # cbz x21 -> mov x21, x9; cbz x21 (x9 dead after it)
+100
View File
@@ -12,6 +12,14 @@ set output TOTK_PATCH_OUTPUT 0
set scene TOTK_PATCH_SCENE 0
set shadow TOTK_PATCH_SHADOW 0
set cascade TOTK_PATCH_CASCADE 0
set grass TOTK_PATCH_GRASS 0
# Load-dependent quality (patches-v121.txt, set steady). The graphics settings setup (main+0x5868c4) inlines the
# three getters 1.2.1 calls: the value 1.2.1 reads at
# main+0xc7eb24 is the ldr w10, [x10, #0x38] at main+0x58692c, and the getters' slow paths (main+0x586bc8, 0x586c18,
# 0x586f44) come back before the compare. So the site is the compare, which every path reaches: w10 = 9 skips the
# list walk to the epilogue (main+0x58729c), which does not read w10.
set steady TOTK_LOD_STEADY 0
site steady main 586954 7100215f 5280012a 7100215f # cmp w10, #8 -> mov w10, #9; cmp w10, #8 (b.gt 0x58729c taken)
# 60 fps: the adaptive present-interval controller (main+0x2db174, 1.2.1 0x8f26c8) gets the minimum interval 1.
site fps60 main 2db204 6b08015f 7100055f # clamp: cmp w10, #1 (was cmp w10, w8 = minimum)
@@ -41,6 +49,16 @@ site realtime main 2daa78 1e210800 d503201f # fmul s0, s0, s1 -> nop
# keeps equipment locked until a load. Stepping while above 0 takes the remainder below 0 like any other last step.
site realtime main 2b036c 0f016681 0f000401 # movi v1.2s, #0x34, lsl #24 (FLT_EPSILON) -> movi v1.2s, #0
# Attack hit processing (main+0x2906a24) builds a list of this frame's hit records on its stack (count at [sp], slots
# at [sp+8] -> an inline array that is never cleared). The record insert (main+0x86e1d8) returns null when the
# attacker's record map (+0x190) is full, so a full map leaves the list empty. After its two loops the function
# passes slot 0 to the hit handler (main+0x870158, which dereferences it) unconditionally at main+0x2906de0 and only
# then checks count == 0 at main+0x2906df0; with an empty list slot 0 is stale stack data and the handler faults.
# Charged spear hits above 60 fps fill the map. Both count == 0 branches go to the check instead, which reloads the
# count and leaves through main+0x2906ce8; x19 and x20 are the only registers read there. A non-empty list is unchanged.
site realtime main 2906d98 34000248 340002c8 # cbz w8, 0x2906de0 -> 0x2906df0 (first loop's empty check)
site realtime main 2906dc0 34000108 34000188 # cbz w8, 0x2906de0 -> 0x2906df0 (second loop's empty check)
# Hair and cloth: the actor animation update builds a second clock {seconds = factor / 30, ratio = factor} per
# rendered frame. 1.4.3 inlines it in two actor paths, where the factor was just stored to the ratio slot: the
# replacement stores measured ratio * factor there and computes measured seconds * factor. Two other callers share the
@@ -82,6 +100,13 @@ site scene main 585e50 bd0cc660 2d420460 # str s0, [x19, #0xcc4] -> ldp s0, s
site scene main 585e54 1e220140 bd0cc660 # scvtf s0, w10 -> str s0, [x19, #0xcc4]
site scene main 585e60 bd0cca60 bd0cca61 # str s0, [x19, #0xcc8] -> str s1, [x19, #0xcc8]
# Fixed scene size (MacNativeGames game_patches_v143.c patch_fixed_res; runtime/resolution.c res_dynres_live switches
# the set on unless TOTK_DYNAMIC_RES=1). The dynamic resolution controller (main+0x584300, 1.2.1 0xcc26f4) steps the
# scene through tiers of 100, 90 and 80 % by a cost model that grows with the scene width squared; at its one epilogue the tier's scale moves into
# the result, and 1.0 there makes every tier 100 %. The original word is MacNativeGames' check against 1.4.3.
set fixres TOTK_FIXED_RES_PATCH 0
site fixres main 5848a8 2a1703e9 52a7f009 # mov w9, w23 -> mov w9, #0x3f800000 (1.0f)
# Camera aspect ratio for a game resolution that is not 16:9 (upstream game_patches_v143.c and aspect_patch.h
# aspect_install / aspect_stub; runtime/resolution.c res_init writes the output's width / height to page +56 and
# switches the set on). Each site leaves the ratio sead's perspective projection (+0xb0) takes in s0: two camera sites
@@ -111,6 +136,8 @@ site mouse_direct main 874cb4 bd042289 94c9acc7
site mouse_direct main 874d7c 6d40a3e9 94c9ac91
site mouse_direct main 18e444 f9400408 94e546db
site mouse_direct main 18f108 bd4047ec 94e543a6
# Keep native camera-distance easing when direct mouse orbit bypasses the stick integrators.
site mouse_direct main 18ea34 b9418a88 94e5454f # orbit state: ldr w8, [x20, #0x188]
site mouse_direct main 1d59460 b9004be8 94761acc
# The aiming, scope, ability, horse and Purah Pad camera cameras: per axis, the float load the stick turn is computed
# from (the gamepad's part only) and the load of the angle it is added to (plus the mouse's degrees), each a bl to page
@@ -1077,3 +1104,76 @@ site cascade main 6fabb8 52a83408 @page:x8 b9404108 # mov w8, #20.0 -> ldr w8
site cascade main 6fac40 52a84408 @page:x8 b9404508 # mov w8, #40.0 -> ldr w8, [page+68]
site cascade main 6facc8 52a85408 @page:x8 b9404908 # mov w8, #80.0 -> ldr w8, [page+72]
site cascade main 6fad4c 52a86408 @page:x8 b9404d08 # mov w8, #160.0 -> ldr w8, [page+76]
# Climbing (issues #35, #276, #1001): the climb controller's velocity step (main+0x1f49754, shared by wall and ladder
# climbing, climb jumps and climb-overs) multiplies the requested velocity by a factor that makes up for a movement loss
# growing with the frame rate. Below ratio 1 the factor is 1.272 + 1.189 (1 - r) clamped to [1, 2] (r = VFR +0, the
# 30 Hz ratio times the time scale): Nintendo's fit for 30 and 60 fps, flat at 2 from about 77 fps, so Link climbs at
# 63% of the speed at 120 fps and 34% at 240. Below r = 0.5 take g(r) = 0.66265 / r + 0.4982 + 0.0848 r instead,
# fitted to the replayed ladder climb at 120 and 240 fps and equal to the shipping 1.8665 at r = 0.5; at most 8. The
# site computes g with 16 * max(r - 0.5, 0) subtracted, so g stays under the shipping factor for r >= 0.5, and folds it
# into the existing clamp without flags: s0 = max(s0, g) (the value clamped), s1 = max(2, g) (the bound). r >= 0.5
# keeps the shipping factor bit for bit. x8 is dead after the constant load at 0x1f497e8, s2-s4 are loaded again at
# 0x1f49844 before any read.
# g takes u = max(page +0, r): the patch page's 30 Hz frame ratio without the time scale (runtime/frame_rate.c), so
# slow motion at 30 or 60 fps keeps the shipping factor and only a real frame rate above 60 raises it; the runtime
# switches the set off without the real-time step, where the page is not written.
set climb TOTK_CLIMB_FIX 1
site climb main 1f497f4 1e201001 @page:x8 bd400104 1e286884 52946dc8 72a7e528 1e270102 1e241842 52828248 72a7dfe8 1e270103 1e232842 529573e8 72a7b5a8 1e270103 1f040862 1e2a3883 2f00e404 1e246863 1e261004 1f048862 1e241004 1e247842 1e226800 1e201001 1e226821 # fmov s1, #2.0 -> s2 = g(u); s0 = max(s0, s2); s1 = max(2.0, s2)
# Grass range (runtime/resolution.c res_grass_range switches the set on). Grass blades grow from the near grass map, a
# square texture the game draws around the camera (GrassShader.sharcb). Its constructor (main+0xa38e58) gives it a
# 62.5 m extent, a 3.90625 m step (1/16 of it) and 256x256 and 128x128 textures; main+0x206340c later sets the extent
# to 62.5 or 31.25 m. With the set on all of them come from page +128..+144, scaled together so the map keeps its
# texels per metre; resolution.c grows the grass heap of the HeapDef for the larger textures.
site grass main a38e58 52a84f48 @page:x8 b9408108 # mov w8, #62.5 -> ldr w8, [page+128]
site grass main a38e64 52a80f48 @page:x8 b9408508 # mov w8, #3.90625 -> ldr w8, [page+132]
site grass main a38e94 52802002 @page:x2 b9408842 # mov w2, #0x100 -> ldr w2, [page+136]
site grass main a38ef4 52801002 @page:x2 b9408c42 # mov w2, #0x80 -> ldr w2, [page+140]
site grass main 2063454 52a84f48 @page:x8 b9408108 # mov w8, #62.5 -> ldr w8, [page+128]
site grass main 2063460 52a83f48 @page:x8 b9409108 # mov w8, #31.25 -> ldr w8, [page+144]
# Optional reference with an invalid value (#1005). main+0x1bd0138 reads an optional reference at object +0x6b0 (null or
# a holder whose first word is the target, bit 0 a tag) and checks it only for null: ldr x8, [x19, #0x6b0]; cbz x8;
# ldr x21, [x8]. Players' crash reports show the field holding 0xffffffffffffffff after minutes to hours of play, and
# the load of the holder faults. MacNativeGames (games/totk/runtime/loader.c, crash) met the same field holding
# 0xffff0000ffffffff during a save load and treats the reference as absent there. Every valid holder is a heap address
# below 2^63, so the cbz becomes a test for null or negative: x9 = x8 >> 63 (arithmetic, all ones when negative),
# x8 &= ~x9, cbz x8. Both x8 and x9 are dead at the branch target 0x1bd01f0, which loads x8 again and writes x9 before
# any read; on the fall-through path x8 is unchanged.
set optref TOTK_OPTREF_GUARD 1
site optref main 1bd01c0 b4000188 937ffd09 8a290108 b4000188 # cbz x8 -> asr x9, x8, #63; bic x8, x8, x9; cbz x8
# Recall and Ultrahand target that is stale memory. The same function reads the target actor from the holder
# (ldr x21, [x8]) and follows it into main+0x155ec, main+0x9a89d4 and main+0x2b8464 (an atomic write at +0x90). The
# holder can be reused memory by then, so the actor points into .rodata, outside the address space or at other heap
# data, and the game crashes reading 0xffffffffffffffff. UltraTears (kuraeow/ultratears, ability target guard) found
# the class. The load becomes a call to runtime/ov_ability.c host_ability_target (page+0xf60): x21 is the holder's
# first word, or 0 where the game's own code would fault on the target (holder, actor or component unreadable, the
# reference word not writable), which the game's cbz x21 skips for this frame. Every other target goes on unchanged.
# The answer comes back in x9 and the cbz site moves it to x21: the lifted code keeps x21 (callee-saved) in a C local
# across the call. x9 is dead after 0x1bd01c8 on every path (written or clobbered by a bl before any read); x30 is saved by the
# prologue and clobbered by the next bl anyway.
set ability TOTK_ABILITY_GUARD 1
site ability main 1bd01c4 f9400115 947c3f67 # ldr x21, [x8] -> bl page+0xf60
site ability main 1bd01c8 b4000155 aa0903f5 b4000155 # cbz x21 -> mov x21, x9; cbz x21
# Model scene report rows (WoE, daniel-madrid-07/weeping-of-the-empire, MIT, games/totk/patches/sites_v143.py
# reportguard; their #52 and #73: crashes while using Ultrahand and aiming). gsys::ModelScene::calcGPU_ is inlined in
# main+0xa1f84; its per-object loop (0xa22bc..0xa234c) takes the object's report handle h (int16 at obj+0x98 + 2 * side),
# exits for -1 only, reads row 2h+1 and then u32 n from row 2h, and follows n to row 2n with no bound (WoE's crash had
# n = the float 0.05, a read 32 GB past the rows). n indexes a row pair as h does, so a valid one is below 0x8000.
# A negative h (other than -1) or n >= 0x8000 goes to 0xa2340, where the game resets the handle to -1 after every use:
# the object has coverage 0 this frame (stored at 0xa22cc) and the game asks for a new report, as after a normal read.
# WoE's veneers exit at 0xa2344 instead, which keeps the bad handle for the next frames. No flags change; w4 is never
# used in main+0xa1f84; x1, x16, x15 and w13, which 0xa2340 reads, hold what they hold on the game's own path there.
set report TOTK_REPORT_GUARD 1
site report main a22d8 2a010571 2a010571 37f80341 # orr w17, w11, w1, lsl #1; tbnz w1, #31, 0xa2340
site report main a2310 531f7821 530f7c24 531f7821 35000184 # lsr w4, w1, #15; lsl w1, w1, #1; cbnz w4, 0xa2340
# Function-entry hooks (FN_ENTER, runtime/cpu.h): only these functions check g_fnprof on entry, the others carry no
# hook. main+0x585bd0 sets the render size (runtime/resolution.c SET_RENDER_SIZE): res_fn_hit takes the renderer from
# the game's own call at start, so a DLSS mode can change while the game runs.
hook main 585bd0
+25 -5
View File
@@ -1,13 +1,15 @@
"""Code patch table for the generator: guest instructions replaced in the lifted C, switchable at run time.
Lifted code cannot be byte-patched when the game runs, so a patch is applied when the C is generated. A patch file
(gen.py --patches) has two kinds of lines; `#` starts a comment, fields are separated by whitespace:
(gen.py --patches) has these kinds of lines; `#` starts a comment, fields are separated by whitespace:
set <name> <env> <default> a patch set: a run-time switch <env>=0/1, <default> when it is unset
site <set> <module> <address> <original> <replacement> [<replacement> ...]
spin <module> <head> <back-edge> [<back-edge> ...]
hook <module> <function> [<function> ...]
<address> is a module offset (hex), <original> the instruction word the module has there (checked; a mismatch stops
the generator), each <replacement> an instruction word (hex) or `@page:x<n>`, which is `adrp x<n>, <patch page>`
encoded for the site's address. The patch page is the first 4 KiB page after main's image (layout.py leaves the rest
@@ -29,6 +31,11 @@ A `spin` line names a spin wait found by tools/spin_waits.py: a loop with head <
and the direct branches back to it (module offsets). Each back-edge (checked: a b, b.cond, cbz / cbnz or tbz / tbnz to
<head>) is lifted as `rt_spin_gate(c, <head>); goto <head>`, which keeps every register and only adds time
(runtime/kernel.c kern_spin_gate).
A `hook` line names guest functions (module offsets of function starts; gen.py stops on any other address) whose
entry the runtime watches: their lifted C starts with FN_ENTER (runtime/cpu.h), a load of g_fnprof and a predicted
branch to fnprof_hit while the runtime sets it. A table with hook lines puts FN_ENTER into these functions only; a table
without any keeps it in every function, as does gen.py --fn-enter-all (a whole-game function-entry profile).
"""
from pathlib import Path
@@ -49,13 +56,22 @@ def adrp(pc, target, rd):
class Table:
"""sets: [(name, env, default)]; sites: {module: {offset: [(set index, [word | ('page', reg)], original)]}} (the
alternatives of a site in file order); page."""
alternatives of a site in file order); page. What the generated C takes from here (for_module, spins_for, hooks_for)
is in the lift cache's key (recomp/gen.py chunk_key); a new kind of line the Emitter reads must go there too."""
def __init__(self, sets=(), sites=None, page=0, spins=None):
def __init__(self, sets=(), sites=None, page=0, spins=None, hooks=None):
self.sets = list(sets)
self.sites = sites or {}
self.page = page
self.spins = spins or {} # {module: {back-edge offset: head offset}}
self.hooks = hooks or {} # {module: {function offset}}; empty: FN_ENTER in every function
def hooks_for(self, name, base):
"""The absolute addresses of the module's hooked functions, or None when the table has no hook line at all
(FN_ENTER in every function)."""
if not self.hooks:
return None
return {base + o for o in self.hooks.get(name, ())}
def spins_for(self, name, base):
"""{absolute back-edge address: absolute head address}."""
@@ -73,7 +89,7 @@ class Table:
def load(path, mods):
"""Table from a patch file; mods is layout() output [(name, base, size)]."""
sets, index, sites, spins = [], {}, {}, {}
sets, index, sites, spins, hooks = [], {}, {}, {}, {}
bases = {n: (b, s) for n, b, s in mods}
for no, raw in enumerate(Path(path).read_text().splitlines(), 1):
f = raw.split('#', 1)[0].split()
@@ -112,6 +128,10 @@ def load(path, mods):
if b in mod:
raise PatchError(f'{where}: {f[1]}+{b:#x} is a back-edge twice')
mod[b] = head
elif f[0] == 'hook' and len(f) >= 3:
if f[1] not in bases:
raise PatchError(f'{where}: module {f[1]} not in the layout')
hooks.setdefault(f[1], set()).update(int(o, 16) for o in f[2:])
else:
raise PatchError(f'{where}: cannot parse {raw!r}')
base, size = bases['main'][0], bases['main'][1]
@@ -119,7 +139,7 @@ def load(path, mods):
nxt = min((b for _, b, _ in mods if b > base), default=None)
if nxt is not None and page + PAGE > nxt:
raise PatchError(f'no room for the patch page after main ({page:#x})')
return Table(sets, sites, page, spins)
return Table(sets, sites, page, spins, hooks)
def check(table, mod, name):
+246
View File
@@ -0,0 +1,246 @@
"""V registers in C locals (gen.py --v-locals K): a rewrite of one emitted function's C.
The lifter writes V registers as `c->v[n]`, lane forms `((T *)&c->v[n])[i]`, the register macros of runtime/simd.h
(VREG, VQ, VW, VS32, ...) and helper calls that name registers by number (fp_op_s(c, op, d, n, m), ...). A chunk
generated with K > 0 defines RT_VLOCALS, under which those macros and helpers reach a register through a local array
`u128 vr[32]` when its bit in the function's VMASK is set, else through c->v; every access is a whole-vector load or
store, so clang's SROA keeps each local register in an XMM register. This module
- turns the direct forms into the accessors (c->v[n] = e -> VSET, c->v[n] -> VGET, lane forms -> VLANE/VLSET),
- picks the K registers the function names most often (VMASK),
- loads them at entry; before every call, rt_dispatch, rt_svc/rt_udf/rt_brk, rt_spin_gate and exit stores the ones
the function writes (VSYNC_OUT_D: only if the dirty mask may have the bit; the mask folds to a constant on most
paths); after a call reloads them, except V8-V15 with trust (AAPCS64: a callee preserves their low 64 bits, and a
conforming caller does not read their upper halves after the call).
Why the cap: 32 guest registers on 16 XMM registers spill (all 32 cost +20% instructions, the stack traffic tripled),
while K = 6..8 removed a quarter of the c->v traffic and kept dependent FP chains in registers; build/v143-vloc
NOTES.md has the measurements.
"""
import re
from collections import Counter
# Default K: 8 cut guest time 3.9-6.2% in the town on a 7900X against K = 0 (build/v143-vloc NOTES.md, series s1 and g1).
LOCALS = 8
# runtime/simd.h helpers that take VCTX, with their parameters after c. Register-number parameters are d (written,
# also read by some), n, m, a (read) and t (the first of up to 4 consecutive registers; written by the sl_ld* loads).
HELPERS = {
'fp_op_s': ['op', 'd', 'n', 'm'],
'fp_op_d': ['op', 'd', 'n', 'm'],
'fp_fma_s': ['na', 'nn', 'd', 'n', 'm', 'a'],
'fp_fma_d': ['na', 'nn', 'd', 'n', 'm', 'a'],
'sl_gen': ['op', 'sz', 'ne', 'd', 'szd', 'offd', 'keep', 'n', 'szn', 'offn', 'm', 'szm', 'offm', 'bidx', 'bimm'],
'sl_addlp': ['u', 'acc', 'Q', 'sz', 'd', 'n'],
'sl_across': ['op', 'u', 'Q', 'sz', 'd', 'n'],
'sl_pmull64': ['part', 'd', 'n', 'm'],
'sv_fred_s': ['op', 'd', 'n'],
'sv_fcvtl': ['sz', 'part', 'd', 'n'],
'sv_fcvtn': ['sz', 'part', 'odd', 'd', 'n'],
'sv_tbl': ['Q', 'len', 'tbx', 'd', 'n', 'm'],
'sv_aes': ['op', 'd', 'n'],
'sv_sha1hash': ['op', 'd', 'n', 'm'],
'sv_sha256hash': ['part1', 'd', 'n', 'm'],
'sv_sha_misc': ['op', 'd', 'n', 'm'],
'sl_ldm': ['Q', 'sz', 'selem', 'rpt', 't', 'ea'],
'sl_stm': ['Q', 'sz', 'selem', 'rpt', 't', 'ea'],
'sl_lds': ['sz', 'selem', 'idx', 't', 'ea'],
'sl_sts': ['sz', 'selem', 'idx', 't', 'ea'],
'sl_ldr': ['Q', 'sz', 'selem', 't', 'ea'],
}
REG_PARAMS = ('d', 'n', 'm', 'a', 't')
LANE_T = {'sa16': 'vu16', 'sa32': 'vu32', 'sa64': 'vu64', 'saf32': 'vf32', 'saf64': 'vf64', 'uint8_t': 'vu8',
'uint16_t': 'vu16', 'uint32_t': 'vu32', 'uint64_t': 'vu64', 'float': 'vf32', 'double': 'vf64'}
CALLEE_SAVED = frozenset(range(8, 16))
PLAIN = '\n const unsigned VMASK = 0; u128 *const vr = c->v; unsigned vd_ = 0, *const vdp = &vd_; (void)vr; (void)vdp;'
FN = re.compile(r'^(?:static )?void (?:f|fb)_[0-9a-f]+\(Cpu \*c\) \{$', re.M)
CALL = re.compile(r'\b(?:f|fb)_[0-9a-f]+\(c\);|\brt_dispatch\(c\);|\brt_(?:svc|udf|brk|spin_gate)\(c,[^;]*;')
RET = re.compile(r'(?:__attribute__\(\(musttail\)\)\s*)?\breturn\b[^;]*;')
RET_BLOCK = re.compile(r'(\{ [^{}]*?(?:__attribute__\(\(musttail\)\) )?return\b[^;]*; \})')
LANE = re.compile(r'\(\((\w+) \*\)&c->v\[')
WHOLE = re.compile(r'c->v\[')
USES = re.compile(r'\b(' + '|'.join(['VSET', 'VLSET', 'VGET', 'VLANE', 'VS8', 'VS16', 'VS32', 'VS64', 'VSF32', 'VSF64',
'VHI64', 'VREG', 'VQ', 'VW', 'FP_SETS', 'FP_SETD', 'SV_FOP_S', 'SV_FOP_D']
+ sorted(HELPERS)) + r')\(')
class Unsupported(ValueError):
pass
def _close(s, i):
"""s[i-1] opens a bracket; the index after its match"""
d = 1
while d:
d += {'[': 1, ']': -1}.get(s[i], 0)
i += 1
return i
def _stmt_end(s, i):
"""index of the ';' that ends the expression starting at s[i]"""
d = 0
while True:
ch = s[i]
if ch in '([{':
d += 1
elif ch in ')]}':
d -= 1
elif ch == ';' and d == 0:
return i
i += 1
def _args(s, i):
"""arguments of the call whose '(' is s[i]"""
d, cur, out = 0, '', []
while True:
ch = s[i]
if ch == '(':
d += 1
if d == 1:
i += 1
continue
elif ch == ')':
d -= 1
if d == 0:
out.append(cur.strip())
return out
elif ch == ',' and d == 1:
out.append(cur.strip())
cur = ''
i += 1
continue
cur += ch
i += 1
def accessors(t):
"""c->v forms of emitted C -> the RT_VLOCALS accessors of runtime/simd.h"""
out, i = [], 0
while True:
m1, m2 = LANE.search(t, i), WHOLE.search(t, i)
m = min((x for x in (m1, m2) if x), key=lambda x: x.start(), default=None)
if not m:
out.append(t[i:])
return ''.join(out)
out.append(t[i:m.start()])
if m is m1:
e = _close(t, m.end())
if m.group(1) not in LANE_T or t[e:e + 2] != ')[':
raise Unsupported(f'V register pointer form {t[m.start():e + 10]!r}')
je = _close(t, e + 2)
reg, lane, vt = accessors(t[m.end():e - 1]), t[e + 2:je - 1], LANE_T[m.group(1)]
a = re.match(r'\s*=(?!=)', t[je:])
if a:
se = _stmt_end(t, je + a.end())
out.append(f'VLSET({reg}, {vt}, {lane}, {accessors(t[je + a.end():se]).strip()})')
i = se
else:
out.append(f'VLANE({reg}, {vt}, {lane})')
i = je
continue
if t[m.start() - 1:m.start()] == '&':
raise Unsupported(f'V register address {t[m.start() - 1:m.start() + 20]!r}')
e = _close(t, m.end())
reg = accessors(t[m.end():e - 1])
a = re.match(r'\s*=(?!=)', t[e:])
if a:
se = _stmt_end(t, e + a.end())
out.append(f'VSET({reg}, {accessors(t[e + a.end():se]).strip()})')
i = se
else:
out.append(f'VGET({reg})')
i = e
def _lit(a):
a = a.strip().rstrip('uUlL')
if re.fullmatch(r'-\d+', a):
return -1
return int(a, 0) if re.fullmatch(r'0x[0-9a-fA-F]+|\d+', a) else None
def registers(body):
"""(Counter of register uses, set of written registers) of a body in accessor form"""
used, wr = Counter(), set()
for m in USES.finditer(body):
name = m.group(1)
args = _args(body, m.end() - 1)
if name in HELPERS:
for p, a in zip(['c'] + HELPERS[name], args):
if p not in REG_PARAMS:
continue
v = _lit(a)
if v == -1:
continue
if v is None:
raise Unsupported(f'register argument {a!r} of {name}')
span = 4 if p == 't' or (name == 'sv_tbl' and p == 'n') else 1
rs = [(v + k) & 31 for k in range(span)]
used.update(rs)
if p == 'd' or (p == 't' and name.startswith('sl_ld')):
wr.update(rs)
continue
pos, write = {'VREG': (1, False), 'VQ': (2, False), 'VW': (2, True), 'SV_FOP_S': (2, True),
'SV_FOP_D': (2, True), 'VSET': (0, True), 'VLSET': (0, True), 'FP_SETS': (0, True),
'FP_SETD': (0, True)}.get(name, (0, False))
v = _lit(args[pos])
if v is None or v < 0:
raise Unsupported(f'register {args[pos]!r} of {name}')
used[v] += 1
if write:
wr.add(v)
return used, wr
def rewrite_one(body, k, trust, stats):
"""Body of one C function (text between its braces)."""
try:
new = accessors(body)
cnt, written = registers(new)
except Unsupported:
# a form this rewrite does not know: every register stays in c->v (VMASK 0)
stats['v_unsupported_functions'] += 1
return PLAIN + body
body = new
if not cnt:
return body
local = {r for r, _ in sorted(cnt.items(), key=lambda x: (-x[1], x[0]))[:k]}
written &= local
load = ''.join(f'VSYNC_IN({r}); ' for r in sorted(local))
reload = ''.join(f'VSYNC_IN({r}); ' for r in sorted(local - (CALLEE_SAVED if trust else set())))
store = ''.join(f'VSYNC_OUT_D({r}); ' for r in sorted(written))
stats['v_local_functions'] += 1
stats['v_local_registers'] += len(local)
def at_ret(m):
stats['v_exit_syncs'] += 1
return '{ ' + store + m.group(0) + ' }'
def at_call(m):
stats['v_call_syncs'] += 1
return '{ ' + store + m.group(0) + ' ' + reload + 'vd_ = 0; }'
body = RET.sub(at_ret, body)
parts = RET_BLOCK.split(body)
body = ''.join(p if i % 2 else CALL.sub(at_call, p) for i, p in enumerate(parts))
mask = sum(1 << r for r in local)
return (f'\n const unsigned VMASK = {mask:#x}u; u128 vr[32]; unsigned vd_ = 0, *const vdp = &vd_; {load.rstrip()}'
+ body + ('\n ' + store.rstrip() if store else ''))
def rewrite(text, k, trust, stats):
"""C text of one emitted guest function (an f_ and, for a computed branch, its fb_) with K registers local."""
out, pos = [], 0
for m in FN.finditer(text):
d, i = 1, m.end()
while d:
d += {'{': 1, '}': -1}.get(text[i], 0)
i += 1
out.append(text[pos:m.end()])
out.append(rewrite_one(text[m.end():i - 1], k, trust, stats) + text[i - 1])
pos = i
out.append(text[pos:])
return ''.join(out)
+289
View File
@@ -0,0 +1,289 @@
// Sound categories (music, effects, voices) for the volume_music / volume_sfx / volume_voice settings.
//
// The audio renderer only sees sample memory, so the category is learned where the game loads its sounds:
// - RomFS opens and reads of Sound/Resource/ files (romfs.c romfs_open / romfs_read): the file name gives the
// category (BGM_* music, NV_* and *_Vo_* / *Voice* voices, the rest effects);
// - zstd decodes of BARS banks (ov_zstd.c decode): each bank entry's AMTA name refines the bank's category;
// - Opus decodes (ov_media.c ov_decode): the decoded PCM inherits the category of the packet it came from.
// Two tables answer the mixer (audio_mixer.c voice_category):
// - DSP-ADPCM coefficient tables, by a hash of their 32 bytes, read from each BWAV channel header. The game copies
// these coefficients next to the voice, so a voice's own table names its sound wherever its samples live;
// - guest address ranges of loaded or decoded audio, newest first, for PCM voices (Opus output).
// Everything here runs only while one of the three settings is below 100 (audio_cat_active): at the defaults no table
// is written or read. A setting moved during play classifies what loads from then on; sounds loaded before (an area's
// effect banks, a stream opened earlier) are found when their next stream read lands, otherwise they count as effects.
#include "audio_cat.h"
#include "cfg.h"
#include "host.h"
#include "platform/platform.h"
#include <stdatomic.h>
#include <string.h>
#include <strings.h>
#define RANGE_CAP 4096 // address ranges, a ring: the oldest go first. Lookups scan it, so it stays small
#define COEF_CAP 65536 // coefficient tables, open addressing; a full probe run drops the new table (it counts as SFX)
#define COEF_PROBES 64
typedef struct {
uint64_t start, end;
uint8_t cat;
} Range;
static Range ranges[RANGE_CAP];
static uint32_t range_head; // ranges written so far; under range_lock
static PlatMutex range_lock = PLAT_MUTEX_INIT;
typedef struct {
_Atomic uint64_t hash; // 0 = free; a slot is claimed once and never freed
_Atomic uint8_t cat;
} Coef;
static Coef coefs[COEF_CAP];
typedef struct {
uint64_t guest;
uint8_t cat;
} Decoder;
static Decoder decoders[64]; // the category each Opus decoder produced last; under decoder_lock
static unsigned decoder_next;
static PlatMutex decoder_lock = PLAT_MUTEX_INIT;
static _Atomic uint64_t coef_count[4], range_count, bars_count;
int audio_cat_active(void) {
static _Atomic unsigned seen = ~0u;
static _Atomic int on;
unsigned ver = cfg_version();
if (atomic_load_explicit(&seen, memory_order_acquire) != ver) {
int a = cfg_double("TOTK_VOLUME_MUSIC") < 100 || cfg_double("TOTK_VOLUME_SFX") < 100 ||
cfg_double("TOTK_VOLUME_VOICE") < 100;
atomic_store_explicit(&on, a, memory_order_relaxed);
atomic_store_explicit(&seen, ver, memory_order_release);
}
return atomic_load_explicit(&on, memory_order_relaxed);
}
static int ci_str_has(const char *haystack, const char *needle) {
size_t nlen = strlen(needle);
for (; *haystack; haystack++)
if (strncasecmp(haystack, needle, nlen) == 0) return 1;
return 0;
}
static int all_zero32(const uint8_t *p) {
for (int i = 0; i < 32; i++)
if (p[i]) return 0;
return 1;
}
static uint64_t fnv1a32(const uint8_t *p) {
uint64_t h = 14695981039346656037ULL;
for (int i = 0; i < 32; i++) {
h ^= p[i];
h *= 1099511628211ULL;
}
return h ? h : 1;
}
// A table seen under several categories keeps music or voice over effects: one sample shared by a voice line and an
// effect bank follows the voice setting.
static void register_coef32(const uint8_t *coef, int cat) {
if (cat <= AUDIO_CAT_UNKNOWN || cat > AUDIO_CAT_VOICE || all_zero32(coef)) return;
uint64_t h = fnv1a32(coef);
for (uint32_t step = 0; step < COEF_PROBES; step++) {
Coef *e = &coefs[((uint32_t)h + step) & (COEF_CAP - 1)];
uint64_t cur = atomic_load_explicit(&e->hash, memory_order_acquire);
if (cur == 0 && atomic_compare_exchange_strong_explicit(&e->hash, &cur, h, memory_order_acq_rel,
memory_order_acquire)) {
atomic_store_explicit(&e->cat, (uint8_t)cat, memory_order_release);
atomic_fetch_add_explicit(&coef_count[cat], 1, memory_order_relaxed);
return;
}
if (cur == h) {
if (cat != AUDIO_CAT_SFX) atomic_store_explicit(&e->cat, (uint8_t)cat, memory_order_release);
return;
}
}
}
int audio_cat_lookup_coef(uint64_t extra_addr, uint64_t extra_size) {
if (!extra_addr || extra_size < 32 || extra_addr >= RT_ADDRESS_LIMIT || !mem_guest_readable(extra_addr, 32))
return AUDIO_CAT_UNKNOWN;
const uint8_t *p = (const uint8_t *)(uintptr_t)extra_addr;
if (all_zero32(p)) return AUDIO_CAT_UNKNOWN;
uint64_t h = fnv1a32(p);
for (uint32_t step = 0; step < COEF_PROBES; step++) {
Coef *e = &coefs[((uint32_t)h + step) & (COEF_CAP - 1)];
uint64_t cur = atomic_load_explicit(&e->hash, memory_order_acquire);
if (cur == h) return atomic_load_explicit(&e->cat, memory_order_acquire); // 0 while the writer is midway
if (cur == 0) break;
}
return AUDIO_CAT_UNKNOWN;
}
// BWAV: "BWAV", channel count at 0x0E, then 0x4C-byte channel infos from 0x10: codec u16 (1 = DSP-ADPCM) and the 16
// ADPCM coefficients at +0x10.
void audio_cat_scan_bwav(const void *data, uint64_t size, int cat) {
if (!data || size < 0x40 || cat <= AUDIO_CAT_UNKNOWN) return;
const uint8_t *b = data;
uint32_t magic;
uint16_t nch;
memcpy(&magic, b, 4);
memcpy(&nch, b + 0x0E, 2);
if (magic != 0x56415742u || nch == 0 || nch > 16) return;
for (uint16_t ch = 0; ch < nch; ch++) {
uint64_t at = 0x10ull + ch * 0x4Cull;
if (at + 0x30 > size) break;
uint16_t codec;
memcpy(&codec, b + at, 2);
if (codec == 1) register_coef32(b + at + 0x10, cat);
}
}
// Every sound bank and stream of the game is under Sound/Resource/ (Sound/Resource/*.bars.zs, Sound/Resource/Stream/
// *.bwav); everything else is not audio. Creature and player vocal banks (Bokoblin_Voice0, Player_Voice, *_Vo_*)
// count as voices, as do the NV_* character voice lines.
int audio_cat_from_path(const char *path) {
if (!path || strncmp(path, "Sound/Resource/", 15)) return AUDIO_CAT_UNKNOWN;
const char *name = strrchr(path, '/') + 1;
if (!strncasecmp(name, "nv_", 3) || !strncasecmp(name, "vo_", 3)) return AUDIO_CAT_VOICE;
if (ci_str_has(name, "bgm_")) return AUDIO_CAT_MUSIC;
if (ci_str_has(name, "_voice") || ci_str_has(name, "voice_") || ci_str_has(name, "_vo_") ||
ci_str_has(name, "shoutvoice") || ci_str_has(name, "shoutmsg"))
return AUDIO_CAT_VOICE;
return AUDIO_CAT_SFX;
}
void audio_cat_tag_range(uint64_t addr, uint64_t size, int cat) {
if (!addr || !size || cat <= AUDIO_CAT_UNKNOWN || cat > AUDIO_CAT_VOICE) return;
plat_mutex_lock(&range_lock);
// A stream refills the same buffers: the same range read again only changes its category.
uint32_t depth = range_head < 64 ? range_head : 64;
for (uint32_t k = 0; k < depth; k++) {
Range *r = &ranges[(range_head - 1 - k) & (RANGE_CAP - 1)];
if (r->start == addr && r->end == addr + size) {
r->cat = (uint8_t)cat;
plat_mutex_unlock(&range_lock);
return;
}
}
Range *r = &ranges[range_head & (RANGE_CAP - 1)];
r->start = addr;
r->end = addr + size;
r->cat = (uint8_t)cat;
range_head++;
plat_mutex_unlock(&range_lock);
atomic_fetch_add_explicit(&range_count, 1, memory_order_relaxed);
}
// The newest range holding addr; newer loads into reused memory win over older ones.
int audio_cat_lookup(uint64_t addr) {
if (!addr) return AUDIO_CAT_UNKNOWN;
int cat = AUDIO_CAT_UNKNOWN;
plat_mutex_lock_shared(&range_lock);
uint32_t n = range_head < RANGE_CAP ? range_head : RANGE_CAP;
for (uint32_t k = 0; k < n; k++) {
const Range *r = &ranges[(range_head - 1 - k) & (RANGE_CAP - 1)];
if (addr >= r->start && addr < r->end) {
cat = r->cat;
break;
}
}
plat_mutex_unlock_shared(&range_lock);
return cat;
}
// BARS: "BARS", entry count at 0x0C, then count CRC32 name hashes and count (AMTA offset, BWAV offset) pairs. The AMTA
// block holds the entry's name; music and voice names inside an effects bank (a boss bank with its BGM) get their own
// category. ADPCM entries register their coefficients; other entries tag their bytes.
void audio_cat_scan_bars(uint64_t dst, uint64_t size, int fallback_cat) {
if (!dst || size < 16) return;
atomic_fetch_add_explicit(&bars_count, 1, memory_order_relaxed);
if (fallback_cat != AUDIO_CAT_UNKNOWN) audio_cat_tag_range(dst, size, fallback_cat);
const uint8_t *base = (const uint8_t *)(uintptr_t)dst;
uint32_t count;
memcpy(&count, base + 0x0C, 4);
if (count > 16384 || 0x10ull + (uint64_t)count * 12ull > size) return;
const uint8_t *pairs = base + 0x10 + (uint64_t)count * 4;
for (uint32_t k = 0; k < count; k++) {
uint32_t amta_off, bwav_off;
memcpy(&amta_off, pairs + k * 8ull, 4);
memcpy(&bwav_off, pairs + k * 8ull + 4, 4);
if (bwav_off >= size) continue;
int cat = fallback_cat != AUDIO_CAT_UNKNOWN ? fallback_cat : AUDIO_CAT_SFX;
if (amta_off < size) {
uint64_t limit = size - amta_off < 512 ? size - amta_off : 512;
const char *a = (const char *)(base + amta_off);
for (uint64_t s = 0; s + 4 <= limit; s++) {
if (!memcmp(a + s, "BGM_", 4)) { cat = AUDIO_CAT_MUSIC; break; }
if (!memcmp(a + s, "Vo_", 3) || !memcmp(a + s, "NV_", 3)) { cat = AUDIO_CAT_VOICE; break; }
if (s + 5 <= limit && !memcmp(a + s, "Voice", 5)) { cat = AUDIO_CAT_VOICE; break; }
}
}
const uint8_t *w = base + bwav_off;
uint32_t magic = 0;
uint16_t codec = 0;
if (bwav_off + 0x40 <= size) {
memcpy(&magic, w, 4);
memcpy(&codec, w + 0x10, 2);
}
if (magic == 0x56415742u) audio_cat_scan_bwav(w, size - bwav_off, cat);
if (cat == fallback_cat || (magic == 0x56415742u && codec == 1)) continue;
// The entry ends where the next BWAV starts: one pass over the offsets, only for the few entries tagged here.
uint64_t next = size;
for (uint32_t j = 0; j < count; j++) {
uint32_t o;
memcpy(&o, pairs + j * 8ull + 4, 4);
if (o > bwav_off && o < next) next = o;
}
audio_cat_tag_range(dst + bwav_off, next - bwav_off, cat);
}
}
// The PCM out of one Opus packet takes the packet's category, else the decoder's last one, else effects. Consecutive
// packets of one decoder fill one buffer, so the output grows the newest matching range instead of adding one each.
void audio_cat_on_opus_decode(uint64_t decoder_guest, uint64_t in_addr, uint64_t out_addr, uint64_t out_size) {
if (!out_addr || !out_size) return;
int cat = audio_cat_lookup(in_addr);
plat_mutex_lock(&decoder_lock);
int slot = -1;
for (int i = 0; i < 64 && decoder_guest; i++)
if (decoders[i].guest == decoder_guest) slot = i;
if (cat == AUDIO_CAT_UNKNOWN && slot >= 0) cat = decoders[slot].cat;
if (cat == AUDIO_CAT_UNKNOWN) cat = AUDIO_CAT_SFX;
if (decoder_guest) {
if (slot < 0) {
slot = (int)(decoder_next++ & 63);
decoders[slot].guest = decoder_guest;
}
decoders[slot].cat = (uint8_t)cat;
}
plat_mutex_unlock(&decoder_lock);
plat_mutex_lock(&range_lock);
uint32_t depth = range_head < 32 ? range_head : 32;
for (uint32_t k = 0; k < depth; k++) {
Range *r = &ranges[(range_head - 1 - k) & (RANGE_CAP - 1)];
if (r->cat != (uint8_t)cat) continue;
uint64_t lo = r->start > 65536 ? r->start - 65536 : 0, hi = r->end + 65536;
if (out_addr >= lo && out_addr + out_size <= hi) {
if (out_addr < r->start) r->start = out_addr;
if (out_addr + out_size > r->end) r->end = out_addr + out_size;
plat_mutex_unlock(&range_lock);
return;
}
}
plat_mutex_unlock(&range_lock);
audio_cat_tag_range(out_addr, out_size, cat);
}
void audio_cat_log_tables(void) {
rt_log("audio: categories known: coefficient tables music %llu, sfx %llu, voice %llu; address ranges %llu "
"(ring %d); BARS banks scanned %llu",
(unsigned long long)atomic_load(&coef_count[AUDIO_CAT_MUSIC]),
(unsigned long long)atomic_load(&coef_count[AUDIO_CAT_SFX]),
(unsigned long long)atomic_load(&coef_count[AUDIO_CAT_VOICE]), (unsigned long long)atomic_load(&range_count),
RANGE_CAP, (unsigned long long)atomic_load(&bars_count));
}
+30
View File
@@ -0,0 +1,30 @@
// Sound categories for the music / effects / voice volume settings (audio_cat.c).
#ifndef TOTK_AUDIO_CAT_H
#define TOTK_AUDIO_CAT_H
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
enum {
AUDIO_CAT_UNKNOWN = 0,
AUDIO_CAT_MUSIC = 1,
AUDIO_CAT_SFX = 2,
AUDIO_CAT_VOICE = 3,
};
int audio_cat_active(void); // one of volume_music / volume_sfx / volume_voice is below 100
int audio_cat_from_path(const char *path); // a RomFS-relative path; UNKNOWN outside Sound/Resource/
void audio_cat_tag_range(uint64_t addr, uint64_t size, int cat);
void audio_cat_scan_bars(uint64_t dst, uint64_t size, int fallback_cat);
void audio_cat_scan_bwav(const void *data, uint64_t size, int cat);
void audio_cat_on_opus_decode(uint64_t decoder_guest, uint64_t in_addr, uint64_t out_addr, uint64_t out_size);
int audio_cat_lookup(uint64_t addr);
int audio_cat_lookup_coef(uint64_t extra_addr, uint64_t extra_size);
void audio_cat_log_tables(void);
#ifdef __cplusplus
}
#endif
#endif
+212 -23
View File
@@ -28,6 +28,7 @@
#include "cfg.h"
#include "audio_effects.h"
#include "audio_reverb.h"
#include "audio_cat.h"
#include "audren.h"
#include "host.h"
#include "ipc.h"
@@ -52,7 +53,10 @@ typedef struct Mixer {
float *buf; int nbuf, frame;
int *offset; // first mix buffer of each mix id
SplitInfo *split; int nsplit; SplitDest *dest; int ndest;
SDL_AudioStream *stream; int device_ok, rate;
SDL_AudioStream *stream; int device_ok, rate; // stream: output_open's, then the output worker's alone
SDL_AudioDeviceID open_id, failed_id; // the device the stream plays on; the last one that failed to open
PlatThread *worker; PlatEvent wake; // output_worker, NULL until a device is chosen; wake: a cfg change
_Atomic int worker_quit; unsigned watch_ver; // watch_ver: the render thread's last cfg_version (output_watch)
int channels; // output channels, 2 or 6 (output_channels)
float ring[RING_FRAMES * OUT_MAX];
_Atomic uint64_t wr, rd, underruns, overruns, missing_frames, callback_frames, callback_nonzero, skipped_frames;
@@ -83,6 +87,64 @@ static float host_volume(void) {
v = (float)((pct < 0 ? 0 : pct > 100 ? 100 : pct) / 100);
return v;
}
// TOTK_VOLUME_MUSIC / _SFX / _VOICE: per-category volumes in percent on top of TOTK_VOLUME, live like it. Render thread
// only.
static float category_volume(int cat) {
static const char *const names[4] = {NULL, "TOTK_VOLUME_MUSIC", "TOTK_VOLUME_SFX", "TOTK_VOLUME_VOICE"};
static float v[4] = {1, 1, 1, 1};
static unsigned seen = ~0u;
unsigned ver = cfg_version();
if (ver != seen) {
seen = ver;
for (int k = 1; k < 4; k++) {
double pct = cfg_double(names[k]);
v[k] = (float)((pct < 0 ? 0 : pct > 100 ? 100 : pct) / 100);
}
}
return v[cat >= AUDIO_CAT_MUSIC && cat <= AUDIO_CAT_VOICE ? cat : AUDIO_CAT_SFX];
}
// What the voice plays (audio_cat.c): an ADPCM voice by its coefficient table, any voice by the newest loaded or decoded
// range holding its current wave buffer, else effects. Kept per voice until it moves to another buffer or table, so a
// table lookup runs once per wave buffer, not once per frame. The counts feed category_report.
static uint64_t cat_counts[4], cat_by_coef, cat_by_range, cat_ns;
static int voice_category(ServerVoice *v) {
uint64_t addr = v->buffers[v->index].address, extra = v->in.extra_address;
if (v->cat && v->cat_address == addr && v->cat_extra == extra) return v->cat;
uint64_t t0 = kern_now_ns();
int cat = AUDIO_CAT_UNKNOWN;
if (v->in.sample_format == FMT_ADPCM) cat = audio_cat_lookup_coef(extra, v->in.extra_size);
if (cat != AUDIO_CAT_UNKNOWN) cat_by_coef++;
else if ((cat = audio_cat_lookup(addr)) != AUDIO_CAT_UNKNOWN) cat_by_range++;
else cat = AUDIO_CAT_SFX;
cat_counts[cat]++;
v->cat = (uint8_t)cat;
v->cat_address = addr;
v->cat_extra = extra;
cat_ns += kern_now_ns() - t0;
return cat;
}
// While a category volume is set, once a minute: how the wave buffers played were classified and what it cost.
static void category_report(void) {
static uint64_t next;
uint64_t now = kern_now_ns();
if (!next) next = now + 60000000000ull;
if (now < next) return;
next = now + 60000000000ull;
uint64_t n = cat_counts[AUDIO_CAT_MUSIC] + cat_counts[AUDIO_CAT_SFX] + cat_counts[AUDIO_CAT_VOICE];
rt_log("audio: categories over the last minute: %llu wave buffers, music %llu, sfx %llu, voice %llu; by coefficients "
"%llu, by address %llu, unmatched (played as sfx) %llu; %.1f us classifying",
(unsigned long long)n, (unsigned long long)cat_counts[AUDIO_CAT_MUSIC],
(unsigned long long)cat_counts[AUDIO_CAT_SFX], (unsigned long long)cat_counts[AUDIO_CAT_VOICE],
(unsigned long long)cat_by_coef, (unsigned long long)cat_by_range,
(unsigned long long)(n - cat_by_coef - cat_by_range), cat_ns / 1000.0);
audio_cat_log_tables();
memset(cat_counts, 0, sizeof cat_counts);
cat_by_coef = cat_by_range = cat_ns = 0;
}
static unsigned audio_revision(const AudioRenderer *r) { unsigned byte = r->revision >> 24; return byte >= '0' ? byte - '0' : 0; }
static void report_once(Mixer *m, unsigned bit, const char *msg, int value) {
if (m->reported & (1u << bit)) return;
@@ -108,8 +170,41 @@ static int prefill_blocks_for(const char *e) {
return (int)ceil(ms / 5.0);
}
// TOTK_AUDIO_DEVICE: the playback device by the name SDL reports (SDL_GetAudioDeviceName), since SDL's device ids
// change between runs. Unset, empty or "default": the system default, which SDL follows when Windows changes it.
static int device_is_default(const char *want) { return !want || !*want || !strcmp(want, "default"); }
// The device for `want`: the first playback device of that name, *found 1; else the system default, *found 0 for a name
// SDL does not list (unplugged). The default asks SDL nothing.
static SDL_AudioDeviceID device_pick(const char *want, int *found) {
*found = 1;
if (device_is_default(want)) return SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK;
int n = 0;
SDL_AudioDeviceID *ids = SDL_GetAudioPlaybackDevices(&n), id = 0;
for (int i = 0; ids && i < n && !id; i++) {
const char *name = SDL_GetAudioDeviceName(ids[i]);
if (name && !strcmp(name, want)) id = ids[i];
}
SDL_free(ids);
*found = id != 0;
return id ? id : SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK;
}
int audio_output_devices(char (*names)[AUDIO_DEVICE_NAME_MAX], int max) {
if (!SDL_WasInit(SDL_INIT_AUDIO)) return 0; // TOTK_AUDIO=none or no output: nothing to pick
int n = 0, k = 0;
SDL_AudioDeviceID *ids = SDL_GetAudioPlaybackDevices(&n);
for (int i = 0; ids && i < n && k < max; i++) {
const char *name = SDL_GetAudioDeviceName(ids[i]);
if (name && *name && strlen(name) < AUDIO_DEVICE_NAME_MAX) snprintf(names[k++], AUDIO_DEVICE_NAME_MAX, "%s", name);
}
SDL_free(ids);
return k;
}
// TOTK_AUDIO_CHANNELS: the output channels of this run, decided once. stereo gives 2; 5.1 gives 6 (FL FR C LFE RL RR,
// the device sink's order and SDL's); auto gives 6 when the default playback device has at least six channels (a 5.1
// the device sink's order and SDL's); auto gives 6 when the chosen playback device (TOTK_AUDIO_DEVICE at start, the
// system default without one) has at least six channels (a 5.1
// or 7.1 speaker setup in Windows; SDL places 5.1 on 7.1) and 2 otherwise. IAudioDevice reports the same count to the
// game (GetActiveChannelCount), which picks its sink layout from it. TOTK_AUDIO=none: 2, without touching SDL.
static int output_channels(void) {
@@ -122,10 +217,12 @@ static int output_channels(void) {
else if (!strcmp(want, "stereo")) n = 2;
else {
SDL_AudioSpec dev = {0};
n = SDL_InitSubSystem(SDL_INIT_AUDIO) && SDL_GetAudioDeviceFormat(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &dev, NULL) &&
dev.channels >= 6 ? 6 : 2;
rt_log("audio: TOTK_AUDIO_CHANNELS=auto: the default device has %d channels, output %s", dev.channels,
n == 6 ? "5.1" : "stereo");
int found, ok = SDL_InitSubSystem(SDL_INIT_AUDIO);
SDL_AudioDeviceID id = ok ? device_pick(cfg_get("TOTK_AUDIO_DEVICE"), &found) : 0;
n = ok && SDL_GetAudioDeviceFormat(id, &dev, NULL) && dev.channels >= 6 ? 6 : 2;
const char *name = ok ? SDL_GetAudioDeviceName(id) : NULL;
rt_log("audio: TOTK_AUDIO_CHANNELS=auto: device '%s' has %d channels, output %s", name ? name : "?",
dev.channels, n == 6 ? "5.1" : "stereo");
}
int expected = 0;
return atomic_compare_exchange_strong(&decided, &expected, n) ? n : expected;
@@ -207,6 +304,28 @@ static void SDLCALL sdl_pull(void *user, SDL_AudioStream *stream, int additional
}
}
// The pacing figures of the stream's device: its period, the output target and the stall time. Set before the stream
// is resumed; the render thread reads target and stall_ns while a switch sets them (audio_mixer_pace_ns).
static void output_timing(Mixer *m, SDL_AudioSpec *dev) {
int period = 0;
if (!SDL_GetAudioDeviceFormat(SDL_GetAudioStreamDevice(m->stream), dev, &period) || period <= 0) period = m->frame;
m->period = period;
int target = m->frame * m->prefill > 2 * period ? m->frame * m->prefill : 2 * period;
__atomic_store_n(&m->target, target > RING_FRAMES / 2 ? RING_FRAMES / 2 : target, __ATOMIC_RELAXED);
uint64_t period_ns = (uint64_t)period * 1000000000ull / (uint64_t)m->rate;
__atomic_store_n(&m->stall_ns, 4 * period_ns > STALL_MS * 1000000ull ? 4 * period_ns : STALL_MS * 1000000ull,
__ATOMIC_RELAXED);
}
static void output_log(Mixer *m, const char *what, const SDL_AudioSpec *dev) {
const char *name = SDL_GetAudioDeviceName(SDL_GetAudioStreamDevice(m->stream));
rt_log("audio: SDL device '%s' %s, %d Hz %s float (device %d channels), prefill %d ms, device period %d frames; "
"the device paces rendering at %.1f ms queued", name ? name : "?", what, m->rate,
m->channels == 6 ? "5.1" : "stereo", dev->channels, m->prefill * 5, m->period, m->target * 1000.0 / m->rate);
}
static void output_worker_start(Mixer *m);
static void output_open(Mixer *m, int rate) {
const char *mode = cfg_get("TOTK_AUDIO");
if ((mode && !strcmp(mode, "none")) || cfg_get("TOTK_AUDIO_OFF")) {
@@ -217,12 +336,18 @@ static void output_open(Mixer *m, int rate) {
rt_log("audio: SDL audio init failed (%s): mixing without sound", SDL_GetError());
return;
}
const char *want = cfg_get("TOTK_AUDIO_DEVICE");
int found;
m->open_id = device_pick(want, &found);
if (!found) rt_log("audio: TOTK_AUDIO_DEVICE '%s' is not connected: the system default plays", want);
SDL_AudioSpec spec = {SDL_AUDIO_F32, m->channels, rate};
m->stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, sdl_pull, m);
m->stream = SDL_OpenAudioDeviceStream(m->open_id, &spec, sdl_pull, m);
if (!m->stream) {
rt_log("audio: no output device (%s): mixing without sound", SDL_GetError());
return;
}
SDL_AudioSpec dev = {0};
output_timing(m, &dev);
if (!SDL_ResumeAudioStreamDevice(m->stream)) {
rt_log("audio: starting the output failed (%s): mixing without sound", SDL_GetError());
SDL_DestroyAudioStream(m->stream);
@@ -230,28 +355,84 @@ static void output_open(Mixer *m, int rate) {
return;
}
m->device_ok = 1;
output_log(m, "opened", &dev);
m->watch_ver = cfg_version();
if (!device_is_default(want)) output_worker_start(m); // watches the chosen device from the start
}
// ---- switching the output device while the game plays (TOTK_AUDIO_DEVICE is live)
// One worker thread per mixer, started when a device is first chosen, owns the stream from then on: it opens the new
// device (tens of ms) off the render thread, so the game's audio never waits for it. Once a second, and at once when
// cfg changes, it resolves the setting again: a chosen device that disappears falls back to the system default, and the
// output returns to it within a second of it being plugged in again. SDL's device events are not used: on Windows
// nothing pumps SDL's event queue (the window is Win32, gfx_window_win.c), while SDL's device list stays current.
// Moves the output to device `id` with the same spec; SDL converts to the device's own format and channels.
static void output_switch(Mixer *m, SDL_AudioDeviceID id) {
SDL_AudioSpec spec = {SDL_AUDIO_F32, m->channels, m->rate};
SDL_AudioStream *s = SDL_OpenAudioDeviceStream(id, &spec, sdl_pull, m); // opens paused: no callback yet
if (!s) {
const char *name = SDL_GetAudioDeviceName(id);
rt_log("audio: opening device '%s' failed (%s): the output stays where it was", name ? name : "?",
SDL_GetError());
m->failed_id = id; // not tried again until the setting or the device list leads elsewhere
return;
}
SDL_DestroyAudioStream(m->stream); // closes the old device; returns once its callback has finished
m->stream = s;
m->open_id = id;
m->failed_id = 0;
// No callback runs until the resume: its prefill starts over, and the render thread paces by its timer (pulled_ns
// 0) until the new device asks for audio. The ring and the game go on; ring_pull trims what piled up meanwhile.
m->ready = 0;
atomic_store_explicit(&m->pulled_ns, 0, memory_order_release);
SDL_AudioSpec dev = {0};
int period = 0;
if (!SDL_GetAudioDeviceFormat(SDL_GetAudioStreamDevice(m->stream), &dev, &period) || period <= 0) period = m->frame;
m->period = period;
m->target = m->frame * m->prefill > 2 * period ? m->frame * m->prefill : 2 * period;
if (m->target > RING_FRAMES / 2) m->target = RING_FRAMES / 2;
uint64_t period_ns = (uint64_t)period * 1000000000ull / (uint64_t)rate;
m->stall_ns = 4 * period_ns > STALL_MS * 1000000ull ? 4 * period_ns : STALL_MS * 1000000ull;
const char *name = SDL_GetAudioDeviceName(SDL_GetAudioStreamDevice(m->stream));
rt_log("audio: SDL device '%s' opened, %d Hz %s float (device %d channels), prefill %d ms, device period %d frames; "
"the device paces rendering at %.1f ms queued", name ? name : "?", rate, m->channels == 6 ? "5.1" : "stereo",
dev.channels, m->prefill * 5, period, m->target * 1000.0 / rate);
output_timing(m, &dev);
if (!SDL_ResumeAudioStreamDevice(s)) rt_log("audio: starting the output failed (%s)", SDL_GetError());
output_log(m, "switched to", &dev);
}
static void output_worker(void *p) {
Mixer *m = p;
while (!atomic_load(&m->worker_quit)) {
const char *want = cfg_get("TOTK_AUDIO_DEVICE");
int found;
SDL_AudioDeviceID id = device_pick(want, &found);
if (id != m->open_id && id != m->failed_id) {
if (!found) rt_log("audio: TOTK_AUDIO_DEVICE '%s' is not connected: switching to the system default", want);
output_switch(m, id);
}
if (id == m->open_id) m->failed_id = 0;
plat_event_wait_ns(&m->wake, 1000000000ull);
}
}
static void output_worker_start(Mixer *m) {
plat_event_init(&m->wake, 0, 0);
m->worker = plat_thread_new(output_worker, m, "audio-out");
if (!m->worker) rt_log("audio: no output worker thread: the device stays as it is until the next start");
}
// Render thread: wakes the worker when cfg changed, or starts it when a device is chosen for the first time. Until the
// worker runs the stream plays on the system default (output_open), so an unset setting costs this check alone.
static void output_watch(Mixer *m) {
unsigned ver = cfg_version();
if (ver == m->watch_ver) return;
m->watch_ver = ver;
if (m->worker) plat_event_set(&m->wake);
else if (!device_is_default(cfg_get("TOTK_AUDIO_DEVICE"))) output_worker_start(m);
}
int64_t audio_mixer_pace_ns(AudioRenderer *r, uint64_t now) {
Mixer *m = r->mixer;
if (m && m->device_ok) output_watch(m);
if (!m || !m->device_ok || !atomic_load_explicit(&m->playing, memory_order_acquire)) return -1;
uint64_t pulled = atomic_load_explicit(&m->pulled_ns, memory_order_acquire);
if (!pulled || (now > pulled && now - pulled > m->stall_ns)) return -1;
if (!pulled || (now > pulled && now - pulled > __atomic_load_n(&m->stall_ns, __ATOMIC_RELAXED))) return -1;
uint64_t have = atomic_load(&m->wr) - atomic_load_explicit(&m->rd, memory_order_acquire);
if (have < (uint64_t)m->target) return 0;
int64_t ns = (int64_t)((have - (uint64_t)m->target + 1) * 1000000000ull / (uint64_t)m->rate);
uint64_t target = (uint64_t)__atomic_load_n(&m->target, __ATOMIC_RELAXED);
if (have < target) return 0;
int64_t ns = (int64_t)((have - target + 1) * 1000000000ull / (uint64_t)m->rate);
return ns < 500000 ? 500000 : ns; // until the device has taken it below the target
}
@@ -379,6 +560,11 @@ void audio_mixer_set_running(AudioRenderer *r, int running) {
void audio_mixer_close(AudioRenderer *r) {
Mixer *m = r->mixer;
if (!m) return;
if (m->worker) { // the render thread is joined: nothing wakes or starts the worker any more
atomic_store(&m->worker_quit, 1);
plat_event_set(&m->wake);
plat_thread_join(m->worker);
}
if (m->stream) { // destroying the stream closes the device and waits for the callback to finish
SDL_DestroyAudioStream(m->stream);
SDL_QuitSubSystem(SDL_INIT_AUDIO);
@@ -812,10 +998,13 @@ void audio_mixer_render(AudioRenderer *r, double seconds) {
if (at > m->nbuf) { rt_log("audio: mixes use %d buffers, renderer has %d", at, m->nbuf); rt_fatal("audio mix buffer layout"); }
float (*vs)[MAX_FRAME]=m->voice_scratch,*filtered=m->filter_scratch;
const int by_category = audio_cat_active(); // the defaults (all 100) skip the category lookups
if (by_category) category_report();
for (int i = 0; i < r->param.voice_count; i++) {
ServerVoice *v = &r->voices[i];
if (!v->in.in_use || v->state != SV_STARTED || !v->in.sample_rate || !v->valid[v->index]) continue;
int cc = (int)v->in.channel_count; if (cc < 1 || cc > 6) { report_once(m, 2, "voice channel count unsupported", cc); continue; }
const float vvol = by_category ? v->in.volume * category_volume(voice_category(v)) : v->in.volume;
voice_render(r, v, vs, frames);
for (int f = 0; f < 2; f++) if (v->in.biquads[f].enable) {
const AudrenBiquad *b = &v->in.biquads[f];
@@ -841,13 +1030,13 @@ void audio_mixer_render(AudioRenderer *r, double seconds) {
SplitDest *d = &m->dest[s->ids[k]];
if (!d->used || !mix_valid(r, d->dest)) continue;
const float *source=splitter_filter(d,vs[c],filtered,frames);
for (int b = 0; b < r->mixes[d->dest].buffer_count && b < 24; b++) accumulate(m, m->offset[d->dest] + b, source, d->vol[b] * v->in.volume, frames);
for (int b = 0; b < r->mixes[d->dest].buffer_count && b < 24; b++) accumulate(m, m->offset[d->dest] + b, source, d->vol[b] * vvol, frames);
}
} else if (mix_valid(r, v->in.mix_id)) {
for (int c = 0; c < cc; c++) {
int32_t id = v->in.channel_ids[c]; if (id < 0 || id >= r->param.voice_count * 6) continue;
const AudrenChannelIn *ch = &r->channels[id];
for (int b = 0; b < r->mixes[v->in.mix_id].buffer_count && b < 24; b++) accumulate(m, m->offset[v->in.mix_id] + b, vs[c], ch->mix[b] * v->in.volume, frames);
for (int b = 0; b < r->mixes[v->in.mix_id].buffer_count && b < 24; b++) accumulate(m, m->offset[v->in.mix_id] + b, vs[c], ch->mix[b] * vvol, frames);
}
}
}
+1
View File
@@ -86,6 +86,7 @@ static void voice_reset(ServerVoice *v) {
v->loops_done = 0;
memset(&v->adpcm, 0, sizeof v->adpcm);
memset(v->biquad_state, 0, sizeof v->biquad_state);
v->cat = 0;
}
static void update_voice(AudioRenderer *r, const AudrenVoiceIn *in) {
+6
View File
@@ -145,6 +145,8 @@ typedef struct {
uint32_t consumed, drops;
struct { int16_t history[2]; int32_t buffer; uint64_t at; } adpcm; // DSP-ADPCM decode cursor (buffer slot, next sample) and history
float biquad_state[2][6][2]; // filter, channel: transposed direct-form II delays
uint64_t cat_address, cat_extra; // the wave buffer and coefficients voice_category (audio_mixer.c) last classified
uint8_t cat; // their AUDIO_CAT_*, 0 until classified; voice_reset clears it
} ServerVoice;
enum { SV_STOPPED, SV_STARTED, SV_PAUSED, SV_STOPPING };
@@ -184,6 +186,10 @@ void audio_mixer_render(AudioRenderer *r, double seconds); // after audio_voic
// many ns (the ring is at its target), -1 leaves the pace to the render thread's timer (no device, renderer stopped,
// device stalled).
int64_t audio_mixer_pace_ns(AudioRenderer *r, uint64_t now);
// The playback devices SDL lists now, by name (TOTK_AUDIO_DEVICE takes one), at most max; 0 while SDL audio is not up
// (TOTK_AUDIO=none). Any thread; the Sound tab of the menu lists them.
enum { AUDIO_DEVICE_NAME_MAX = 256 };
int audio_output_devices(char (*names)[AUDIO_DEVICE_NAME_MAX], int max);
uint64_t audio_voice_consumed(const AudioRenderer *r, ServerVoice *v, int frames); // source samples over one frame; advances v->fraction
int audio_buffer_empty(const AudrenWaveBuffer *b); // no samples to decode: played as soon as it is reached
void audio_device_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs); // IAudioDevice
+111
View File
@@ -0,0 +1,111 @@
// Generated by tools/build_shaders.py from runtime/shaders/bl_deswizzle.comp; do not edit.
// source sha256 bd2065290e315e85d2fcdb609d812c15ba9a109d7506b3105850a5860677b0ef
#pragma once
#include <stdint.h>
static const uint32_t bl_deswizzle_spv[] = {
0x07230203, 0x00010300, 0x000d000b, 0x000000d0, 0x00000000, 0x00020011, 0x00000001, 0x0006000b,
0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, 0x00000000, 0x00000001,
0x0007000f, 0x00000005, 0x00000004, 0x6e69616d, 0x00000000, 0x0000000b, 0x0000001f, 0x00060010,
0x00000004, 0x00000011, 0x00000040, 0x00000001, 0x00000001, 0x00040047, 0x0000000b, 0x0000000b,
0x0000001a, 0x00030047, 0x00000010, 0x00000002, 0x00050048, 0x00000010, 0x00000000, 0x00000023,
0x00000000, 0x00050048, 0x00000010, 0x00000001, 0x00000023, 0x00000004, 0x00050048, 0x00000010,
0x00000002, 0x00000023, 0x00000008, 0x00050048, 0x00000010, 0x00000003, 0x00000023, 0x0000000c,
0x00050048, 0x00000010, 0x00000004, 0x00000023, 0x00000010, 0x00050048, 0x00000010, 0x00000005,
0x00000023, 0x00000014, 0x00050048, 0x00000010, 0x00000006, 0x00000023, 0x00000018, 0x00050048,
0x00000010, 0x00000007, 0x00000023, 0x0000001c, 0x00050048, 0x00000010, 0x00000008, 0x00000023,
0x00000020, 0x00050048, 0x00000010, 0x00000009, 0x00000023, 0x00000024, 0x00050048, 0x00000010,
0x0000000a, 0x00000023, 0x00000028, 0x00040047, 0x0000001f, 0x0000000b, 0x0000001b, 0x00040047,
0x000000ad, 0x00000006, 0x00000010, 0x00030047, 0x000000ae, 0x00000002, 0x00040048, 0x000000ae,
0x00000000, 0x00000019, 0x00050048, 0x000000ae, 0x00000000, 0x00000023, 0x00000000, 0x00030047,
0x000000b0, 0x00000019, 0x00040047, 0x000000b0, 0x00000021, 0x00000001, 0x00040047, 0x000000b0,
0x00000022, 0x00000000, 0x00040047, 0x000000c1, 0x00000006, 0x00000010, 0x00030047, 0x000000c2,
0x00000002, 0x00040048, 0x000000c2, 0x00000000, 0x00000018, 0x00050048, 0x000000c2, 0x00000000,
0x00000023, 0x00000000, 0x00030047, 0x000000c4, 0x00000018, 0x00040047, 0x000000c4, 0x00000021,
0x00000000, 0x00040047, 0x000000c4, 0x00000022, 0x00000000, 0x00040047, 0x000000cd, 0x0000000b,
0x00000019, 0x00020013, 0x00000002, 0x00030021, 0x00000003, 0x00000002, 0x00040015, 0x00000006,
0x00000020, 0x00000000, 0x00040017, 0x00000009, 0x00000006, 0x00000003, 0x00040020, 0x0000000a,
0x00000001, 0x00000009, 0x0004003b, 0x0000000a, 0x0000000b, 0x00000001, 0x0004002b, 0x00000006,
0x0000000c, 0x00000001, 0x00040020, 0x0000000d, 0x00000001, 0x00000006, 0x000d001e, 0x00000010,
0x00000006, 0x00000006, 0x00000006, 0x00000006, 0x00000006, 0x00000006, 0x00000006, 0x00000006,
0x00000006, 0x00000006, 0x00000006, 0x00040020, 0x00000011, 0x00000009, 0x00000010, 0x0004003b,
0x00000011, 0x00000012, 0x00000009, 0x00040015, 0x00000013, 0x00000020, 0x00000001, 0x0004002b,
0x00000013, 0x00000014, 0x0000000a, 0x00040020, 0x00000015, 0x00000009, 0x00000006, 0x0004002b,
0x00000006, 0x00000019, 0x00000000, 0x0004002b, 0x00000006, 0x0000001d, 0x00000040, 0x0004003b,
0x0000000a, 0x0000001f, 0x00000001, 0x0004002b, 0x00000013, 0x00000024, 0x00000002, 0x00020014,
0x00000027, 0x0004002b, 0x00000013, 0x0000002d, 0x00000006, 0x0004002b, 0x00000013, 0x00000032,
0x00000007, 0x0004002b, 0x00000006, 0x00000037, 0x00000020, 0x0004002b, 0x00000006, 0x00000048,
0x00000005, 0x0004002b, 0x00000006, 0x0000004c, 0x0000001f, 0x0004002b, 0x00000013, 0x0000004f,
0x00000008, 0x0004002b, 0x00000013, 0x00000052, 0x00000009, 0x0004002b, 0x00000006, 0x0000006a,
0x00000004, 0x0004002b, 0x00000006, 0x0000006f, 0x00000002, 0x0004002b, 0x00000006, 0x00000078,
0x00000008, 0x0004002b, 0x00000006, 0x00000083, 0x00000003, 0x0004002b, 0x00000013, 0x00000093,
0x00000003, 0x0004002b, 0x00000013, 0x0000009b, 0x00000004, 0x0004002b, 0x00000013, 0x000000a4,
0x00000005, 0x00040017, 0x000000ac, 0x00000006, 0x00000004, 0x0003001d, 0x000000ad, 0x000000ac,
0x0003001e, 0x000000ae, 0x000000ad, 0x00040020, 0x000000af, 0x0000000c, 0x000000ae, 0x0004003b,
0x000000af, 0x000000b0, 0x0000000c, 0x0004002b, 0x00000013, 0x000000b1, 0x00000000, 0x0004002b,
0x00000013, 0x000000b2, 0x00000001, 0x0003001d, 0x000000c1, 0x000000ac, 0x0003001e, 0x000000c2,
0x000000c1, 0x00040020, 0x000000c3, 0x0000000c, 0x000000c2, 0x0004003b, 0x000000c3, 0x000000c4,
0x0000000c, 0x00040020, 0x000000c9, 0x0000000c, 0x000000ac, 0x0006002c, 0x00000009, 0x000000cd,
0x0000001d, 0x0000000c, 0x0000000c, 0x00050036, 0x00000002, 0x00000004, 0x00000000, 0x00000003,
0x000200f8, 0x00000005, 0x000300f7, 0x000000ce, 0x00000000, 0x000300fb, 0x00000019, 0x000000cf,
0x000200f8, 0x000000cf, 0x00050041, 0x0000000d, 0x0000000e, 0x0000000b, 0x0000000c, 0x0004003d,
0x00000006, 0x0000000f, 0x0000000e, 0x00050041, 0x00000015, 0x00000016, 0x00000012, 0x00000014,
0x0004003d, 0x00000006, 0x00000017, 0x00000016, 0x00050084, 0x00000006, 0x00000018, 0x0000000f,
0x00000017, 0x00050041, 0x0000000d, 0x0000001a, 0x0000000b, 0x00000019, 0x0004003d, 0x00000006,
0x0000001b, 0x0000001a, 0x00050080, 0x00000006, 0x0000001c, 0x00000018, 0x0000001b, 0x00050084,
0x00000006, 0x0000001e, 0x0000001c, 0x0000001d, 0x00050041, 0x0000000d, 0x00000020, 0x0000001f,
0x00000019, 0x0004003d, 0x00000006, 0x00000021, 0x00000020, 0x00050080, 0x00000006, 0x00000022,
0x0000001e, 0x00000021, 0x00050041, 0x00000015, 0x00000025, 0x00000012, 0x00000024, 0x0004003d,
0x00000006, 0x00000026, 0x00000025, 0x000500ae, 0x00000027, 0x00000028, 0x00000022, 0x00000026,
0x000300f7, 0x0000002a, 0x00000000, 0x000400fa, 0x00000028, 0x00000029, 0x0000002a, 0x000200f8,
0x00000029, 0x000200f9, 0x000000ce, 0x000200f8, 0x0000002a, 0x00050041, 0x00000015, 0x0000002e,
0x00000012, 0x0000002d, 0x0004003d, 0x00000006, 0x0000002f, 0x0000002e, 0x000500c4, 0x00000006,
0x00000030, 0x0000000c, 0x0000002f, 0x00050041, 0x00000015, 0x00000033, 0x00000012, 0x00000032,
0x0004003d, 0x00000006, 0x00000034, 0x00000033, 0x000500c4, 0x00000006, 0x00000035, 0x0000000c,
0x00000034, 0x00050080, 0x00000006, 0x0000003c, 0x0000002f, 0x00000034, 0x000500c4, 0x00000006,
0x0000003d, 0x00000037, 0x0000003c, 0x00050086, 0x00000006, 0x00000041, 0x00000022, 0x0000003d,
0x00050089, 0x00000006, 0x00000045, 0x00000022, 0x0000003d, 0x000500c2, 0x00000006, 0x00000049,
0x00000045, 0x00000048, 0x000500c7, 0x00000006, 0x0000004d, 0x00000045, 0x0000004c, 0x00050041,
0x00000015, 0x00000050, 0x00000012, 0x0000004f, 0x0004003d, 0x00000006, 0x00000051, 0x00000050,
0x00050041, 0x00000015, 0x00000053, 0x00000012, 0x00000052, 0x0004003d, 0x00000006, 0x00000054,
0x00000053, 0x00050084, 0x00000006, 0x00000055, 0x00000051, 0x00000054, 0x00050086, 0x00000006,
0x00000059, 0x00000041, 0x00000055, 0x00050089, 0x00000006, 0x0000005d, 0x00000041, 0x00000055,
0x00050086, 0x00000006, 0x00000062, 0x0000005d, 0x00000051, 0x00050089, 0x00000006, 0x00000067,
0x0000005d, 0x00000051, 0x00050084, 0x00000006, 0x0000006b, 0x00000067, 0x0000006a, 0x000500c2,
0x00000006, 0x0000006d, 0x0000004d, 0x0000006a, 0x000500c7, 0x00000006, 0x0000006e, 0x0000006d,
0x0000000c, 0x00050084, 0x00000006, 0x00000070, 0x0000006e, 0x0000006f, 0x00050080, 0x00000006,
0x00000071, 0x0000006b, 0x00000070, 0x000500c2, 0x00000006, 0x00000073, 0x0000004d, 0x0000000c,
0x000500c7, 0x00000006, 0x00000074, 0x00000073, 0x0000000c, 0x00050080, 0x00000006, 0x00000075,
0x00000071, 0x00000074, 0x00050084, 0x00000006, 0x00000079, 0x00000062, 0x00000078, 0x00050084,
0x00000006, 0x0000007b, 0x00000079, 0x00000030, 0x00050089, 0x00000006, 0x0000007e, 0x00000049,
0x00000030, 0x00050084, 0x00000006, 0x0000007f, 0x0000007e, 0x00000078, 0x00050080, 0x00000006,
0x00000080, 0x0000007b, 0x0000007f, 0x000500c2, 0x00000006, 0x00000082, 0x0000004d, 0x0000006f,
0x000500c7, 0x00000006, 0x00000084, 0x00000082, 0x00000083, 0x00050084, 0x00000006, 0x00000085,
0x00000084, 0x0000006f, 0x00050080, 0x00000006, 0x00000086, 0x00000080, 0x00000085, 0x000500c7,
0x00000006, 0x00000088, 0x00000045, 0x0000000c, 0x00050080, 0x00000006, 0x00000089, 0x00000086,
0x00000088, 0x00050084, 0x00000006, 0x0000008d, 0x00000059, 0x00000035, 0x00050086, 0x00000006,
0x00000090, 0x00000049, 0x00000030, 0x00050080, 0x00000006, 0x00000091, 0x0000008d, 0x00000090,
0x00050041, 0x00000015, 0x00000094, 0x00000012, 0x00000093, 0x0004003d, 0x00000006, 0x00000095,
0x00000094, 0x000500ae, 0x00000027, 0x00000096, 0x00000075, 0x00000095, 0x000400a8, 0x00000027,
0x00000097, 0x00000096, 0x000300f7, 0x00000099, 0x00000000, 0x000400fa, 0x00000097, 0x00000098,
0x00000099, 0x000200f8, 0x00000098, 0x00050041, 0x00000015, 0x0000009c, 0x00000012, 0x0000009b,
0x0004003d, 0x00000006, 0x0000009d, 0x0000009c, 0x000500ae, 0x00000027, 0x0000009e, 0x00000089,
0x0000009d, 0x000200f9, 0x00000099, 0x000200f8, 0x00000099, 0x000700f5, 0x00000027, 0x0000009f,
0x00000096, 0x0000002a, 0x0000009e, 0x00000098, 0x000400a8, 0x00000027, 0x000000a0, 0x0000009f,
0x000300f7, 0x000000a2, 0x00000000, 0x000400fa, 0x000000a0, 0x000000a1, 0x000000a2, 0x000200f8,
0x000000a1, 0x00050041, 0x00000015, 0x000000a5, 0x00000012, 0x000000a4, 0x0004003d, 0x00000006,
0x000000a6, 0x000000a5, 0x000500ae, 0x00000027, 0x000000a7, 0x00000091, 0x000000a6, 0x000200f9,
0x000000a2, 0x000200f8, 0x000000a2, 0x000700f5, 0x00000027, 0x000000a8, 0x0000009f, 0x00000099,
0x000000a7, 0x000000a1, 0x000300f7, 0x000000aa, 0x00000000, 0x000400fa, 0x000000a8, 0x000000a9,
0x000000aa, 0x000200f8, 0x000000a9, 0x000200f9, 0x000000ce, 0x000200f8, 0x000000aa, 0x00050041,
0x00000015, 0x000000b3, 0x00000012, 0x000000b2, 0x0004003d, 0x00000006, 0x000000b4, 0x000000b3,
0x00050041, 0x00000015, 0x000000b6, 0x00000012, 0x0000009b, 0x0004003d, 0x00000006, 0x000000b7,
0x000000b6, 0x00050084, 0x00000006, 0x000000b8, 0x00000091, 0x000000b7, 0x00050080, 0x00000006,
0x000000ba, 0x000000b8, 0x00000089, 0x00050084, 0x00000006, 0x000000bd, 0x000000ba, 0x00000095,
0x00050080, 0x00000006, 0x000000be, 0x000000b4, 0x000000bd, 0x00050080, 0x00000006, 0x000000c0,
0x000000be, 0x00000075, 0x00050041, 0x00000015, 0x000000c5, 0x00000012, 0x000000b1, 0x0004003d,
0x00000006, 0x000000c6, 0x000000c5, 0x00050080, 0x00000006, 0x000000c8, 0x000000c6, 0x00000022,
0x00060041, 0x000000c9, 0x000000ca, 0x000000c4, 0x000000b1, 0x000000c8, 0x0004003d, 0x000000ac,
0x000000cb, 0x000000ca, 0x00060041, 0x000000c9, 0x000000cc, 0x000000b0, 0x000000b1, 0x000000c0,
0x0003003e, 0x000000cc, 0x000000cb, 0x000200f9, 0x000000ce, 0x000200f8, 0x000000ce, 0x000100fd,
0x00010038,
};
+7 -3
View File
@@ -22,7 +22,7 @@
#include "nvn_api.h"
#include "platform/platform.h"
#include "vk.h"
#include "platform/platform.h"
#include "gameplay_tweaks.h"
#define SYM_INIT_START "_ZN2nn4init5StartEmmPFvvES2_S2_"
// main: return addresses of nnMain's SetFocusHandlingMode call (1.1.0 and 1.2.1 from this port's analysis of main;
@@ -101,8 +101,11 @@ static void cb_module_finis(Cpu *c) {
// ---- checkpoints (host.h)
static void stats_line(void) {
RtStats s = rt_stats();
rt_logf("boot: passed (%llu SVCs, %llu threads, %llu host calls)\n", (unsigned long long)s.svcs,
(unsigned long long)s.threads, (unsigned long long)s.host_calls);
if (rt_trace & (RT_TRACE_HOSTCALL | RT_TRACE_HOSTCOUNT)) // counted only then (hostfn.c hostfn_entry)
rt_logf("boot: passed (%llu SVCs, %llu threads, %llu host calls)\n", (unsigned long long)s.svcs,
(unsigned long long)s.threads, (unsigned long long)s.host_calls);
else
rt_logf("boot: passed (%llu SVCs, %llu threads)\n", (unsigned long long)s.svcs, (unsigned long long)s.threads);
}
void rt_boot_event(const char *what, Cpu *c, const char *detail) {
@@ -347,6 +350,7 @@ void rt_boot(void) {
rt_patches_init();
mods_exefs_switch(); // after the patch sets' settings: an enabled mod's ExeFS patches on
frame_step_init();
gameplay_tweaks_init();
res_init();
cheats_init(); // after the patch sets: a cheat's set follows its setting
cb_addr[0] = hostfn_register("<rtld loader_ready>", cb_loader_ready);
+127 -44
View File
@@ -32,35 +32,41 @@ static int save_ok = 1; // the last cfg_save_to wrote (o
// logged as unknown. A new variable goes here too, or its ini key logs a harmless "unknown key" line, and gets a
// description in totk.ini.example or docs/settings.md (tests/settings_doc_check.py checks both).
static const char *const known[] = {
"AB", "AMIIBO_DIR", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CHANNELS", "AUDIO_OFF", "AUDIO_PREFILL_MS",
"AB", "ABILITY_GUARD", "AMIIBO_DIR", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CHANNELS", "AUDIO_DEVICE", "AUDIO_OFF", "AUDIO_PREFILL_MS",
"BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT", "BIND_LSTICK_DOWN", "BIND_LSTICK_LEFT",
"BIND_LSTICK_RIGHT", "BIND_LSTICK_UP", "BIND_MINUS",
"BIND_PLUS", "BIND_R", "BIND_RCLICK", "BIND_RIGHT", "BIND_RSTICK_DOWN", "BIND_RSTICK_LEFT", "BIND_RSTICK_RIGHT", "BIND_RSTICK_UP",
"BIND_SHAKE",
"BIND_SKIP", "BIND_STATS", "BIND_UP", "BIND_X", "BIND_Y",
"BIND_ZL", "BIND_ZR", "BLOCK_VIEWS_3D", "CLOTH_REALTIME", "COLOR_BRIGHTNESS", "COLOR_CONTRAST", "COLOR_GAMMA",
"BIND_INC", "BIND_SKIP", "BIND_STATS", "BIND_UP", "BIND_X", "BIND_Y",
"BIND_ZL", "BIND_ZR", "BLOCK_VIEWS_3D", "CLIMB_FIX", "CLOTH_REALTIME", "COLOR_BRIGHTNESS", "COLOR_CONTRAST", "COLOR_GAMMA",
"COLOR_PRESET", "COLOR_SATURATION", "CONTACT_DEBUG", "CONTACT_SHADOWS", "CORE_DIAG", "CRIT", "CRIT_OUT",
"DESC_TEMPLATE", "DEVICE_PROFILE", "DISCORD_APP_ID", "DISCORD_IMAGE", "DISCORD_RPC", "DLAA", "DLAA_JITTER", "DLAA_MV", "DLAA_MASKDEBUG", "DLAA_MVDEBUG", "DLAA_OBJMV", "DLAA_SURVEY", "DLAA_TIMES", "DLSS", "DLSS_CHAR_MASK", "DLSS_PRESET", "DLSS_REACTIVE", "DUMP", "DUMP_BUF", "DUMP_DIR",
"DUMP_LOG", "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES",
"DESC_TEMPLATE", "DESWIZZLE", "DEVICE_PROFILE", "DIALOGUE_CLICK", "DISCORD_APP_ID", "DISCORD_IMAGE", "DISCORD_RPC", "DLAA", "DLAA_JITTER", "DLAA_MV", "DLAA_MASKDEBUG", "DLAA_MVDEBUG", "DLAA_OBJMV", "DLAA_SURVEY", "DLAA_TIMES", "DLSS", "DLSS_CHAR_MASK", "DLSS_PRESET", "DLSS_REACTIVE", "DUMP", "DUMP_BUF", "DUMP_DIR",
"DUMP_LOG", "DYNAMIC_RES", "DYN_DELTA", "EXEFS", "FEEDBACK", "FIXED_RES_PATCH", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES",
"FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLUSH_SPAN", "FOV", "FOV_PATCH", "FPS60", "FPS_LIMIT", "FRAME_QUEUE",
"FREEZE_IDLE", "FREE_CORES",
"FULLSCREEN", "FXAA", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_NAME", "HDR", "HDR_GAME_NITS", "HDR_HUE",
"HDR_PEAK_NITS", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD",
"LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MENU_LANG", "MENU_TEST",
"MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MOTION", "MOUSE_AIM_SCALE", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_ASPECT", "MOVIE_HOST", "MOVIE_PACE",
"FULLSCREEN", "FXAA",
"GAMEPLAY_DISTANCE_SENSITIVITY", "GAMEPLAY_ITEM_AD", "GAMEPLAY_ITEM_SENSITIVITY", "GAMEPLAY_ITEM_WHEEL",
"GAMEPLAY_MAP_ZOOM", "GAMEPLAY_MAP_ZOOM_SENSITIVITY", "GAMEPLAY_ROTATION_SENSITIVITY", "GAMEPLAY_TWEAKS_ENABLED",
"GAMEPLAY_ULTRAHAND_C_AXIS", "GAMEPLAY_ULTRAHAND_DISTANCE", "GAMEPLAY_ULTRAHAND_ROTATE", "GAMEPLAY_WEAPON_SENSITIVITY",
"GAMEPLAY_WEAPON_WHEEL",
"GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_NAME", "GRASS_RANGE",
"GYRO_BIAS_PAD", "GYRO_BIAS_X", "GYRO_BIAS_Y", "GYRO_BIAS_Z", "GYRO_CALIBRATION", "HDR", "HDR_GAME_NITS", "HDR_HUE",
"HDR_PEAK_NITS", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "KSPIN_US", "LANG", "LIB_PREBUILD",
"LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "LOD_STEADY", "LSTICK_DEADZONE", "MAP_CLICK", "MEMORY", "MEMX", "MENU_LANG", "MENU_TEST", "MENU_FONT_FONTCONFIG",
"MEM_RESERVE_MIB", "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MOTION", "MOUSE_AIM_SCALE", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_ASPECT", "MOVIE_HOST", "MOVIE_PACE",
"MVOBJ_ALL", "MVOBJ_BANKS", "MVOBJ_EQUAL", "MVOBJ_EXACT", "MVOBJ_EXEC", "MVOBJ_NOC4", "MVOBJ_ONLY", "MVOBJ_TIMES", "MVPREV_CHECK", "MVPREV_GROW", "MVPREV_SKIP", "MVPREV_TRUST", "MV_CHECK",
"MV_SURVEY",
"ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT",
"ONE_PER_CORE", "OPERATION_MODE", "OPTREF_GUARD", "OUTPUT_RES", "PAD_LAYOUT", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_SCENE", "PCORE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "POINTER_IMAGE",
"PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_BENCH", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES",
"PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_END", "REEXEC",
"RENDER_SCALE", "REPLAY_ASYNC",
"REPLAY_CHECK", "RESOLVE_CHECK", "RESTART_WAIT", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES",
"PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_ASYNC", "REC_END", "REC_SPIN_US", "REEXEC", "REFLEX",
"RENDER_SCALE", "REPLAY_ASYNC", "REPLAY_CHUNKS", "REPLAY_CHUNK_DRAWS", "REPLAY_SKEW_US", "REPLAY_STATE_CHECK", "REPLAY_WORKERS",
"REPLAY_CHECK", "REPORT_GUARD", "RESOLVE_CHECK", "RESTART_WAIT", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES",
"ROMFS_PROOF_DIR", "ROMFS_VERIFY", "RSTICK_DEADZONE", "RUMBLE", "RUMBLE_CURVE", "RUMBLE_STRENGTH", "RUMBLE_TRIGGERS", "SHADER_CACHE",
"SHADER_MEMO", "SHADOW_RANGE", "SHADOW_RES", "SHADOW_SCALE", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR",
"SHADER_MEMO", "SHADOW", "SHADOW_DEFER", "SHADOW_RANGE", "SHADOW_RES", "SHADOW_SCALE", "SHADOW_SNAP", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR",
"SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_US", "STICK_DEADZONE", "STICK_SENS",
"SWKBD_ANSWER",
"SWKBD_TEST", "TEST_PAD", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_HEAP_EXTRA_MB", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "UB_OVERLAY", "UPSCALER", "VB_USED", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NO_FLOAT64",
"VK_PRESENT", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VOLUME", "VRAM_HEADROOM", "VRAM_LIMIT_MIB", "VS_OUTS",
"SWKBD_ANSWER", "SYNC_AHEAD",
"SWKBD_TEST", "TEST_OOM", "TEST_PAD", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_BLOCK_MIB", "TEX_HEAP_EXTRA_MB", "TEX_MEMO", "TEX_RELEASED_MIB", "TEX_VRAM_PRESSURE", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "UB_OVERLAY", "UB_OVERLAY_OUT", "UPSCALER", "VB_USED", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NO_FLOAT64",
"VK_PRESENT", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VMA_BLOCK_MIB", "VOLUME", "VOLUME_MUSIC", "VOLUME_SFX", "VOLUME_VOICE", "VRAM_HEADROOM", "VRAM_LIMIT_MIB", "VS_OUTS",
"WALK_KEY", "WALK_SCALE", "WB_EXACT", "WB_FENCE_LIMIT", "WB_MERGE", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_KEEP", "XLATE_PROCS",
"ZSTD_CHECK", "ZSTD_NATIVE",
};
@@ -69,43 +75,54 @@ static const char *const known[] = {
static const struct { const char *key, *section; } sections[] = {
{"FPS_LIMIT", "frame rate"}, {"FPS60", "frame rate"}, {"FRAME_QUEUE", "frame rate"},
{"REALTIME", "frame rate"}, {"CLOTH_REALTIME", "frame rate"}, {"HIGHFPS_FIX", "frame rate"},
{"CLIMB_FIX", "frame rate"},
{"FULLSCREEN", "display"}, {"WINDOW_SIZE", "display"}, {"PERF_OVERLAY", "display"}, {"VK_PRESENT", "display"},
{"GPU_INDEX", "display"}, {"OUTPUT_RES", "display"}, {"RENDER_SCALE", "display"}, {"UPSCALER", "display"},
{"SHARPNESS", "display"}, {"ASPECT", "display"}, {"MOVIE_ASPECT", "display"}, {"FOV", "display"}, {"ANISO", "display"},
{"COLOR_PRESET", "display"},
{"SHARPNESS", "display"}, {"DYNAMIC_RES", "display"}, {"ASPECT", "display"}, {"MOVIE_ASPECT", "display"}, {"FOV", "display"}, {"ANISO", "display"},
{"LOD_STEADY", "display"}, {"COLOR_PRESET", "display"},
{"COLOR_BRIGHTNESS", "display"}, {"COLOR_CONTRAST", "display"}, {"COLOR_SATURATION", "display"},
{"COLOR_GAMMA", "display"}, {"HDR", "display"}, {"HDR_PEAK_NITS", "display"}, {"HDR_GAME_NITS", "display"},
{"HDR_UI_NITS", "display"}, {"HDR_HUE", "display"}, {"SHADOW_RES", "display"},
{"DLAA", "display"}, {"DLSS", "display"}, {"DLSS_PRESET", "display"}, {"DLSS_CHAR_MASK", "display"}, {"FXAA", "display"},
{"DLAA", "display"}, {"DLSS", "display"}, {"DLSS_PRESET", "display"}, {"DLSS_CHAR_MASK", "display"}, {"REFLEX", "display"}, {"FXAA", "display"},
{"DEVICE_PROFILE", "display"}, {"ASSUME_FOCUS", "display"},
{"DISCORD_RPC", "discord"}, {"DISCORD_APP_ID", "discord"}, {"DISCORD_IMAGE", "discord"},
{"SHADOW_SCALE", "display"}, {"SHADOW_RANGE", "display"}, {"CONTACT_SHADOWS", "display"},
{"SHADOW_SCALE", "display"}, {"SHADOW_RANGE", "display"}, {"GRASS_RANGE", "display"}, {"CONTACT_SHADOWS", "display"},
{"PCSS", "display"},
{"MODS_DIR", "mods"}, {"MODS_ORDER", "mods"}, {"MODS_DISABLED", "mods"}, {"MODS_REBUILD", "mods"},
{"TEX_HEAP_EXTRA_MB", "mods"},
{"MENU_LANG", "language"}, {"LANG", "language"}, {"OPERATION_MODE", "language"},
{"VOLUME", "audio"}, {"AUDIO_CHANNELS", "audio"},
{"MOTION", "input"}, {"RUMBLE_STRENGTH", "input"}, {"RUMBLE_CURVE", "input"}, {"RUMBLE_TRIGGERS", "input"},
{"MENU_LANG", "language"}, {"MENU_FONT_FONTCONFIG", "language"}, {"LANG", "language"}, {"OPERATION_MODE", "language"},
{"VOLUME", "audio"}, {"VOLUME_MUSIC", "audio"}, {"VOLUME_SFX", "audio"}, {"VOLUME_VOICE", "audio"}, {"AUDIO_CHANNELS", "audio"},
{"AUDIO_DEVICE", "audio"},
{"MOTION", "input"}, {"GYRO_CALIBRATION", "input"}, {"RUMBLE_STRENGTH", "input"}, {"RUMBLE_CURVE", "input"}, {"RUMBLE_TRIGGERS", "input"},
{"GYRO_BIAS_X", "input"}, {"GYRO_BIAS_Y", "input"}, {"GYRO_BIAS_Z", "input"}, {"GYRO_BIAS_PAD", "input"},
{"PAD_LAYOUT", "input"}, {"PROMPTS", "input"}, {"STICK_DEADZONE", "input"}, {"LSTICK_DEADZONE", "input"}, {"RSTICK_DEADZONE", "input"},
{"STICK_SENS", "input"},
{"MENU_CURSOR", "input"}, {"MOUSE_MODE", "input"}, {"MOUSE_SENS", "input"}, {"MOUSE_AIM_SCALE", "input"}, {"WALK_KEY", "input"}, {"WALK_SCALE", "input"}, {"INPUT_RECORD", "input"},
{"MENU_CURSOR", "input"}, {"MAP_CLICK", "input"}, {"DIALOGUE_CLICK", "input"}, {"POINTER_IMAGE", "input"}, {"MOUSE_MODE", "input"}, {"MOUSE_SENS", "input"}, {"MOUSE_AIM_SCALE", "input"}, {"WALK_KEY", "input"}, {"WALK_SCALE", "input"}, {"INPUT_RECORD", "input"},
{"BIND_A", "input"}, {"BIND_B", "input"}, {"BIND_X", "input"}, {"BIND_Y", "input"}, {"BIND_LCLICK", "input"},
{"BIND_RCLICK", "input"}, {"BIND_L", "input"}, {"BIND_R", "input"}, {"BIND_ZL", "input"}, {"BIND_ZR", "input"},
{"BIND_PLUS", "input"}, {"BIND_MINUS", "input"}, {"BIND_LEFT", "input"}, {"BIND_UP", "input"},
{"BIND_RIGHT", "input"}, {"BIND_DOWN", "input"}, {"BIND_LSTICK_LEFT", "input"}, {"BIND_LSTICK_UP", "input"},
{"BIND_LSTICK_RIGHT", "input"}, {"BIND_LSTICK_DOWN", "input"}, {"BIND_RSTICK_LEFT", "input"},
{"BIND_RSTICK_UP", "input"}, {"BIND_RSTICK_RIGHT", "input"}, {"BIND_RSTICK_DOWN", "input"}, {"BIND_SHAKE", "input"},
{"ASYNC_PREEMPT", "performance"}, {"REPLAY_ASYNC", "performance"}, {"FREE_CORES", "performance"},
{"ASYNC_PREEMPT", "performance"}, {"REPLAY_ASYNC", "performance"}, {"REPLAY_WORKERS", "performance"}, {"FREE_CORES", "performance"},
{"TIMER_1MS", "performance"}, {"PIPE_WAIT", "performance"}, {"PIPE_SKIP", "performance"},
{"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"PIPE_PREBUILD", "performance"},
{"PREWARM_XLATE", "performance"}, {"GPL", "performance"}, {"ASTC", "performance"}, {"MEMORY", "performance"},
{"TEX_ALIAS", "performance"}, {"TEX_VRAM_PRESSURE", "performance"}, {"ASTC_BC", "performance"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"},
{"BIND_SKIP", "performance"}, {"SHADER_MEMO", "performance"}, {"PIPE_MEMO", "performance"}, {"TEX_MEMO", "performance"}, {"VB_USED", "performance"}, {"DESC_TEMPLATE", "performance"},
{"PUSH_SKIP", "performance"}, {"SPLIT_DRAWS", "performance"}, {"SPLIT_US", "performance"}, {"VK_VALIDATION", "performance"},
{"TEX_ALIAS", "performance"}, {"TEX_VRAM_PRESSURE", "performance"}, {"TEX_RELEASED_MIB", "performance"}, {"TEX_BLOCK_MIB", "performance"}, {"VMA_BLOCK_MIB", "performance"}, {"ASTC_BC", "performance"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"},
{"BIND_SKIP", "performance"}, {"SHADER_MEMO", "performance"}, {"PIPE_MEMO", "performance"}, {"MEMX", "performance"}, {"BIND_INC", "performance"}, {"TEX_MEMO", "performance"}, {"VB_USED", "performance"}, {"DESC_TEMPLATE", "performance"},
{"SYNC_AHEAD", "performance"}, {"SHADOW_DEFER", "performance"}, {"PUSH_SKIP", "performance"}, {"SPLIT_DRAWS", "performance"}, {"SPLIT_US", "performance"}, {"VK_VALIDATION", "performance"},
{"VK_DYN_VERTEX", "performance"}, {"VK_DYN_A2C", "performance"}, {"VK_SUBGROUP_PIN", "performance"},
{"OPTREF_GUARD", "performance"}, {"ABILITY_GUARD", "performance"}, {"REPORT_GUARD", "performance"},
{"TRACE", "diagnostics"}, {"FLIGHT", "diagnostics"}, {"PASS_CRUMBS", "diagnostics"}, {"FLIGHT_SPIKE_MS", "diagnostics"},
{"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"},
{"MV_SURVEY", "diagnostics"},
{"GAMEPLAY_TWEAKS_ENABLED", "gameplay tweaks"}, {"GAMEPLAY_ULTRAHAND_ROTATE", "gameplay tweaks"},
{"GAMEPLAY_ULTRAHAND_C_AXIS", "gameplay tweaks"}, {"GAMEPLAY_ULTRAHAND_DISTANCE", "gameplay tweaks"},
{"GAMEPLAY_ITEM_WHEEL", "gameplay tweaks"}, {"GAMEPLAY_ITEM_AD", "gameplay tweaks"},
{"GAMEPLAY_MAP_ZOOM", "gameplay tweaks"}, {"GAMEPLAY_MAP_ZOOM_SENSITIVITY", "gameplay tweaks"},
{"GAMEPLAY_ROTATION_SENSITIVITY", "gameplay tweaks"}, {"GAMEPLAY_DISTANCE_SENSITIVITY", "gameplay tweaks"},
{"GAMEPLAY_ITEM_SENSITIVITY", "gameplay tweaks"},
{"GAMEPLAY_WEAPON_WHEEL", "gameplay tweaks"}, {"GAMEPLAY_WEAPON_SENSITIVITY", "gameplay tweaks"},
};
// The settings the menu shows: the value in effect when the key is unset, and what the key accepts. The only place a
@@ -119,10 +136,12 @@ static const CfgMeta metas[] = {
{"VK_PRESENT", "mailbox", "mailbox|fifo|immediate", LIVE}, {"GPU_INDEX", "", NULL, RESTART},
{"PERF_OVERLAY", "off", "off|fps|full", LIVE},
{"OUTPUT_RES", "1920x1080", NULL, RESTART}, {"RENDER_SCALE", "game", "game|50|67|75|100|125|150|200", RESTART},
{"DYNAMIC_RES", "1", "0|1", LIVE}, // resolution.c res_dynres_live
{"UPSCALER", "fsr", "fsr|bilinear", LIVE}, {"SHARPNESS", "80", "0..100", LIVE},
{"DLAA", "0", "0|1", RESTART}, {"DLSS", "dlaa", "dlaa|quality|balanced|performance|ultra_performance", LIVE},
{"DLSS_PRESET", "default", "default|k|j", LIVE},
{"DLSS_CHAR_MASK", "65", "0|35|65|100", LIVE}, {"FXAA", "1", "0|1", LIVE},
{"DLSS_CHAR_MASK", "65", "0|35|65|100", LIVE}, {"REFLEX", "off", "off|on|boost", RESTART}, {"FXAA", "1", "0|1", LIVE},
{"LOD_STEADY", "0", "0|1", RESTART}, // sets steady
{"ASPECT", "fit", "fit|stretch", LIVE}, {"MOVIE_ASPECT", "keep", "keep|stretch", LIVE}, {"FOV", "100", "70..140", LIVE},
{"ANISO", "game", "game|2|4|8|16", RESTART}, // vk_texture.c sampler_features
// colour adjustment (vk_color.c); the preset is the menu's, the four values are what the present uses
@@ -131,31 +150,51 @@ static const CfgMeta metas[] = {
{"COLOR_GAMMA", "1", "0.8..1.2", LIVE},
// HDR output (vk_hdr.c): on at start (the tonemap shader is swapped as shaders load); the nits apply at once
{"HDR", "0", "0|1", RESTART}, {"HDR_PEAK_NITS", "1000", "400..10000", LIVE}, {"HDR_GAME_NITS", "203", "80..500", LIVE},
{"HDR_UI_NITS", "203", "80..500", LIVE}, {"HDR_HUE", "80", "0..100", LIVE},
{"HDR_UI_NITS", "203", "80..500", LIVE}, {"HDR_HUE", "70", "0..100", LIVE},
{"DEVICE_PROFILE", "auto", "auto|desktop|deck", RESTART},
{"SHADOW_RES", "game", "game|256|512|768|1024|1536|2048", RESTART},
{"SHADOW_SCALE", "1", "1..4", RESTART},
{"SHADOW_RANGE", "100", "50..400", RESTART}, {"CONTACT_SHADOWS", "0", "0..100", RESTART},
{"SHADOW_RANGE", "100", "50..400", RESTART}, {"GRASS_RANGE", "100", "50..400", RESTART}, {"CONTACT_SHADOWS", "0", "0..100", RESTART},
{"PCSS", "0", "0..200", RESTART},
// Game
{"FPS_LIMIT", "60", "30|40|45|60|90|120|144|display", LIVE}, {"FPS60", "1", "0|1", LIVE}, {"FRAME_QUEUE", "0", "0|1", LIVE},
{"REALTIME", "1", "0|1", RESTART}, {"CLOTH_REALTIME", "1", "0|1", RESTART},
{"MODS_REBUILD", "auto", "auto|off", RESTART},
{"HIGHFPS_FIX", "1", "0|1", RESTART},
{"HIGHFPS_FIX", "1", "0|1", RESTART}, {"CLIMB_FIX", "1", "0|1", RESTART},
{"LANG", "", "ja|en-US|fr|de|it|es|zh-CN|ko|nl|pt|ru|zh-TW|en-GB|fr-CA|es-419|zh-Hans|zh-Hant|pt-BR", RESTART},
{"OPERATION_MODE", "docked", "docked|handheld", RESTART},
{"ASSUME_FOCUS", "1", "0|1", LIVE}, // read on every focus change (ov_system.c)
// Sound, Controls, Menu
{"VOLUME", "100", "0..100", LIVE}, {"AUDIO_CHANNELS", "stereo", "auto|stereo|5.1", RESTART},
{"MOTION", "1", "0|1", LIVE}, {"RUMBLE_STRENGTH", "130", "0..300", LIVE}, {"RUMBLE_CURVE", "0.6", "0.2..1.5", LIVE},
{"VOLUME", "100", "0..100", LIVE}, {"VOLUME_MUSIC", "100", "0..100", LIVE}, {"VOLUME_SFX", "100", "0..100", LIVE}, {"VOLUME_VOICE", "100", "0..100", LIVE}, {"AUDIO_CHANNELS", "stereo", "auto|stereo|5.1", RESTART},
{"AUDIO_DEVICE", "", NULL, LIVE}, // audio_mixer.c output_worker: a device name, empty for the system default
{"MOTION", "1", "0|1", LIVE}, {"GYRO_CALIBRATION", "1", "0|1", LIVE},
{"RUMBLE_STRENGTH", "130", "0..300", LIVE}, {"RUMBLE_CURVE", "0.6", "0.2..1.5", LIVE},
// Written by the Calibrate gyro row (motion_fusion.c save_bias), not by hand; read by input.c pad_bias_select when a
// controller connects and gyro_bias_pad names it.
{"GYRO_BIAS_X", "", NULL, RESTART}, {"GYRO_BIAS_Y", "", NULL, RESTART}, {"GYRO_BIAS_Z", "", NULL, RESTART},
{"GYRO_BIAS_PAD", "", NULL, RESTART},
{"RUMBLE_TRIGGERS", "0", "0|1", LIVE}, {"PAD_LAYOUT", "auto", "auto|nintendo|xbox", LIVE},
{"PROMPTS", "nintendo", "nintendo|xbox|pc", RESTART},
{"STICK_DEADZONE", "8", "0..50", LIVE}, {"STICK_SENS", "1", "0.1..3", RESTART},
{"LSTICK_DEADZONE", "", "0..50", LIVE}, {"RSTICK_DEADZONE", "", "0..50", LIVE},
{"MOUSE_MODE", "direct", "stick|direct", LIVE},
{"MOUSE_SENS", "1", "0..10", LIVE}, {"MOUSE_AIM_SCALE", "1", "0.1..3", LIVE}, {"MENU_CURSOR", "1", "0|1", LIVE},
{"MAP_CLICK", "0", "0|1", LIVE}, // input.c: the left mouse button is A on the Purah Pad's map
{"DIALOGUE_CLICK", "0", "0|1", LIVE}, // menu_cursor.c: in a dialogue box left click is B, right click A
{"POINTER_IMAGE", "0", "0|1", LIVE}, // input_win.c / input_sdl.c: the game window's own pointer (cursor_art.c)
{"WALK_KEY", "none", NULL, LIVE}, {"WALK_SCALE", "0.51", "0.2..0.9", LIVE},
// Gameplay Tweaks
{"GAMEPLAY_TWEAKS_ENABLED", "0", "0|1", LIVE},
{"GAMEPLAY_ULTRAHAND_ROTATE", "1", "0|1", LIVE}, {"GAMEPLAY_ULTRAHAND_C_AXIS", "1", "0|1", LIVE},
{"GAMEPLAY_ULTRAHAND_DISTANCE", "1", "0|1", LIVE}, {"GAMEPLAY_ITEM_WHEEL", "1", "0|1", LIVE},
{"GAMEPLAY_ITEM_AD", "1", "0|1", LIVE}, {"GAMEPLAY_MAP_ZOOM", "1", "0|1", LIVE},
{"GAMEPLAY_MAP_ZOOM_SENSITIVITY", "1", "0.25..4", LIVE}, {"GAMEPLAY_ROTATION_SENSITIVITY", "1", "0.25..4", LIVE},
{"GAMEPLAY_DISTANCE_SENSITIVITY", "1", "0.25..4", LIVE}, {"GAMEPLAY_ITEM_SENSITIVITY", "1", "0.25..4", LIVE},
{"GAMEPLAY_WEAPON_WHEEL", "1", "0|1", LIVE}, {"GAMEPLAY_WEAPON_SENSITIVITY", "1", "0.25..4", LIVE},
{"MENU_LANG", "", "en|ru|pt-BR|zh-Hans", LIVE},
// plat_font_path on Linux: 1 asks fontconfig which file draws the language, 0 keeps the fixed distribution
// paths. A rebuild of the atlas reads it, so a change shows on the next window size or language switch.
{"MENU_FONT_FONTCONFIG", "1", "0|1", LIVE},
{"AMIIBO_DIR", "", NULL, LIVE},
// Discord Rich Presence (discord_rpc.c): off by default, the Menu tab of the settings menu switches it.
{"DISCORD_RPC", "0", "0|1", LIVE},
@@ -163,6 +202,10 @@ static const CfgMeta metas[] = {
{"DISCORD_IMAGE", "totk_512x512", NULL, LIVE},
// Advanced: scheduling
{"ASYNC_PREEMPT", "1", "0|1|2", RESTART}, {"REPLAY_ASYNC", "0", "0|1", RESTART},
{"REPLAY_WORKERS", "0", "0..16", RESTART}, {"REPLAY_CHUNKS", "0", "0..64", RESTART},
{"REPLAY_CHUNK_DRAWS", "128", "1..100000", RESTART}, {"REPLAY_STATE_CHECK", "0", "0|1", RESTART},
{"REPLAY_SKEW_US", "0", "0..100000", RESTART},
{"SYNC_AHEAD", "0", "0|1", RESTART}, {"SHADOW_DEFER", "1", "0|1", RESTART},
{"FREE_CORES", "0", "0|1", RESTART}, {"TIMER_1MS", "1", "0|1", RESTART},
// shaders and pipelines
{"PIPE_WAIT", "default", "default|draw", RESTART}, {"PIPE_SKIP", "fullscreen", "strict|any|fullscreen", RESTART},
@@ -170,10 +213,13 @@ static const CfgMeta metas[] = {
{"PIPE_PREBUILD", "1", "0|1", RESTART}, {"PREWARM_XLATE", "", "1..64", RESTART}, {"GPL", "1", "0|1", RESTART},
// textures and memory
{"ASTC", "auto", "auto|gpu|native|cpu", RESTART}, {"MEMORY", "auto", "auto|import|mirror|verify", RESTART},
{"TEX_ALIAS", "1", "0|1", RESTART}, {"TEX_VRAM_PRESSURE", "90", "0..100", RESTART}, {"ASTC_BC", "", "0|1", RESTART}, {"ZSTD_NATIVE", "1", "0|1", RESTART}, {"ZSTD_CHECK", "0", "0|1", RESTART},
{"TEX_ALIAS", "1", "0|1", RESTART}, {"TEX_VRAM_PRESSURE", "90", "0..100", RESTART},
{"TEX_RELEASED_MIB", "256", "0..65536", RESTART}, {"TEX_BLOCK_MIB", "64", "0..1024", RESTART}, {"VMA_BLOCK_MIB", "64", "0..1024", RESTART}, {"ASTC_BC", "", "0|1", RESTART}, {"ZSTD_NATIVE", "1", "0|1", RESTART}, {"ZSTD_CHECK", "0", "0|1", RESTART},
// crash guards: code patch sets (recomp/patches-v*.txt) over game reads that crash on stale data
{"OPTREF_GUARD", "1", "0|1", RESTART}, {"ABILITY_GUARD", "1", "0|1", RESTART}, {"REPORT_GUARD", "1", "0|1", RESTART},
// render fixes
{"LOD_GUARD", "1", "0|1", RESTART}, {"FIX_C0", "1", "0|1", RESTART}, {"FIX_ZERO", "1", "0|1", RESTART},
{"BIND_SKIP", "1", "0|1", RESTART}, {"SHADER_MEMO", "1", "0|1", RESTART}, {"PIPE_MEMO", "1", "0|1", RESTART}, {"TEX_MEMO", "1", "0|1", RESTART},
{"BIND_SKIP", "1", "0|1", RESTART}, {"SHADER_MEMO", "1", "0|1", RESTART}, {"PIPE_MEMO", "1", "0|1", RESTART}, {"MEMX", "1", "0|1|check", RESTART}, {"BIND_INC", "0", "0|1|check", RESTART}, {"TEX_MEMO", "1", "0|1", RESTART},
{"VB_USED", "1", "0|1", RESTART},
{"DESC_TEMPLATE", "1", "0|1", RESTART},
{"PUSH_SKIP", "1", "0|1", RESTART}, {"SPLIT_DRAWS", "64", "0..1000", RESTART},
@@ -337,19 +383,53 @@ static const char *raw_get(const char *name) {
// picture between black bars. The sizes are the ones the installer lays the HUD out for (installer/steps.py
// ASPECT_SIZES, ui/menu.cpp row_output_res): of the display's shape within 1%, the smallest at least 1080 high that fits
// the display, else the largest that fits. A 16:9 display, an unknown one or one of another shape keeps 1920x1080.
// Xbox or keyboard prompts keep 1920x1080 too: the setup builds their folders for 16:9 only (romfs.c overlay_init), so
// a default of another shape would trade the prompts the player chose for the Switch ones.
// Xbox and keyboard prompts need their folder for that size's shape too (romfs-patches-xbox-<w>x<h>, -xbox-label-,
// -pc-, written by installer/steps.py Shapes for the style totk.ini selects and the connected displays' shapes): they
// keep 1920x1080 when it is missing, as in a source build without make_romfs_patches.py --prompts <style> --aspect or
// after a switch of prompts in the menu, where a default of another shape would trade the prompts the player chose for
// the Switch ones.
static const unsigned SHAPED[][2] = {
{2560, 1080}, {3440, 1440}, {3840, 1600}, {5120, 2160}, // 21:9
{3840, 1080}, {5120, 1440}, // 32:9
{1280, 800}, {1680, 1050}, {1920, 1200}, {2560, 1600}, // 16:10
{1920, 1280}, {2160, 1440}, {2256, 1504}, {2880, 1920}, {3000, 2000}, // 3:2
};
static char screen_res[16]; // set once by cfg_init, "" for none
static int switch_prompts(void) {
// The folder size of w x h's shape: of SHAPED within 1% of its aspect ratio, the closest, the first of equal ones
// (5120x2160 takes 2560x1080). The UI layout depends on the aspect ratio alone (tools/ultrawide_layout.py apply), so
// the setup writes one romfs-patches-<w>x<h> per shape, named after this size (installer/screens.py shape_size, the
// same rule), and romfs.c overlay_init looks for it. 0 for 16:9 and for a shape without a layout.
int cfg_shape_size(unsigned w, unsigned h, unsigned out[2]) {
if (!w || !h) return 0;
double r = (double)w / h, err = 0;
if (fabs(r / (16.0 / 9.0) - 1) <= 0.01) return 0;
int best = -1;
for (int i = 0; i < (int)(sizeof SHAPED / sizeof *SHAPED); i++) {
double e = fabs((double)SHAPED[i][0] / SHAPED[i][1] / r - 1);
if (e <= 0.01 && (best < 0 || e < err)) best = i, err = e;
}
if (best < 0) return 0;
out[0] = SHAPED[best][0], out[1] = SHAPED[best][1];
return 1;
}
static char screen_res[16]; // set once by cfg_init, "" for none
// romfs-patches-xbox-, romfs-patches-xbox-label- and romfs-patches-pc-<screen_res> have a manifest (set once by
// cfg_init)
static const char *const screen_folders[3] = {"romfs-patches-xbox", "romfs-patches-xbox-label", "romfs-patches-pc"};
static int screen_prompts[3];
// Whether the prompts in effect have their folder at screen_res. The same picks as input_map.c input_prompts_pick and
// input_prompts_folder: an unknown or empty value is the Nintendo prompts, an unset one Xbox while the pad layout is
// "xbox_prompts"; the Xbox prompts take romfs-patches-xbox with the Nintendo pad layout (or "xbox_prompts") and
// romfs-patches-xbox-label with any other.
static int shape_keeps_prompts(void) {
const char *p = raw_get(PREFIX "PROMPTS"), *l = raw_get(PREFIX "PAD_LAYOUT");
return (!p || !strcasecmp(p, "nintendo")) && !(l && !strcasecmp(l, "xbox_prompts"));
if (p && *p && !strcasecmp(p, "pc")) return screen_prompts[2];
int xbox = p && *p ? !strcasecmp(p, "xbox") : l && !strcasecmp(l, "xbox_prompts");
if (!xbox) return 1;
int by_position = l && (!strcasecmp(l, "nintendo") || !strcasecmp(l, "xbox_prompts"));
return screen_prompts[by_position ? 0 : 1];
}
static void screen_default(void) {
@@ -367,7 +447,9 @@ static void screen_default(void) {
if (best < 0) best = fit;
if (best < 0) return;
snprintf(screen_res, sizeof screen_res, "%ux%u", SHAPED[best][0], SHAPED[best][1]);
if (!raw_get(PREFIX "OUTPUT_RES") && switch_prompts())
for (int i = 0; i < 3; i++)
screen_prompts[i] = romfs_shaped_prompts_ready(screen_folders[i], SHAPED[best][0], SHAPED[best][1]);
if (!raw_get(PREFIX "OUTPUT_RES") && shape_keeps_prompts())
rt_log("cfg: the display is %dx%d: output_res defaults to %s, its shape (totk.ini output_res sets another)", sw,
sh, screen_res);
}
@@ -376,7 +458,8 @@ const char *cfg_get(const char *name) {
const char *v = raw_get(name);
if (v || strncmp(name, PREFIX, PREFIX_LEN)) return v;
if (!profile_of())
return screen_res[0] && !strcasecmp(name + PREFIX_LEN, "OUTPUT_RES") && switch_prompts() ? screen_res : NULL;
return screen_res[0] && !strcasecmp(name + PREFIX_LEN, "OUTPUT_RES") && shape_keeps_prompts() ? screen_res
: NULL;
for (size_t i = 0; i < sizeof DECK / sizeof *DECK; i++)
if (!strcasecmp(DECK[i].key, name + PREFIX_LEN)) return DECK[i].value;
return NULL;
+3
View File
@@ -23,6 +23,9 @@ unsigned cfg_version(void);
int cfg_from_env(const char *name);
// 1 when the player set name (the environment or totk.ini), 0 when cfg_get gives a device profile's default or NULL.
int cfg_is_set(const char *name);
// The folder size of w x h's shape (cfg.c SHAPED: the closest aspect ratio within 1%, the first of equal ones), the
// one romfs-patches-<w>x<h> the setup writes for it. 0 for 16:9 and for a shape without a UI layout.
int cfg_shape_size(unsigned w, unsigned h, unsigned out[2]);
// The device profile in use, "deck" or "desktop" (TOTK_DEVICE_PROFILE, else what the machine is).
const char *cfg_device_profile(void);
// Writes the keys cfg_set changed since the last save into totk.ini next to totk.exe (cfg_save_to: the file at path).
+50
View File
@@ -230,6 +230,16 @@ int probe_cfg(void) {
dn[KEY_MOUSE_R] = dn[KEY_Z] = 1;
CHECK(input_map_buttons(dn) == (1ull << B_Y | 1ull << B_ZL), "keys: right mouse button and Z both press ZL");
memset(dn, 0, sizeof dn);
dn[KEY_T] = 1;
CHECK(input_map_buttons(dn) == 1ull << B_Y, "keys: T is Y too (the left button is A in the menus)");
plat_setenv("TOTK_BIND_X", "key:t");
input_map_configure();
CHECK(input_map_buttons(dn) == 1ull << B_X, "bind: bind_X = key:t takes T from Y's defaults");
dn[KEY_T] = 0, dn[KEY_MOUSE_L] = 1;
CHECK(input_map_buttons(dn) == 1ull << B_Y, "bind: Y keeps the left mouse button");
plat_setenv("TOTK_BIND_X", NULL);
input_map_reset();
memset(dn, 0, sizeof dn);
dn[KEY_X] = dn[KEY_TAB] = dn[KEY_M] = dn[KEY_LCTRL] = dn[KEY_3] = 1;
CHECK(input_map_buttons(dn) == (1ull << B_ZR | 1ull << B_PLUS | 1ull << B_MINUS | 1ull << B_STICK_L | 1ull << B_RIGHT),
"keys: X ZR, Tab Plus, M Minus, Left Ctrl stick click, 3 D-pad right");
@@ -329,6 +339,38 @@ int probe_cfg(void) {
CHECK(a_east == EQ(input_prompts_folder(PROMPTS_XBOX, layouts[i]), "romfs-patches-xbox"),
"prompts: the folder names A as B exactly when the layout puts A on the button labelled B");
}
// One folder per screen shape (cfg.c cfg_shape_size, installer/screens.py shape_size): the closest aspect ratio of
// SHAPED within 1%, the first of equal ones; 16:9 and unlisted shapes have none.
unsigned sz[2] = {0, 0};
CHECK(cfg_shape_size(5120, 2160, sz) && sz[0] == 2560 && sz[1] == 1080, "shape: 5120x2160 takes 2560x1080's folder");
CHECK(cfg_shape_size(3440, 1440, sz) && sz[0] == 3440 && sz[1] == 1440, "shape: 3440x1440 keeps its own (0.8% off 2560x1080)");
CHECK(cfg_shape_size(1920, 800, sz) && sz[0] == 3840 && sz[1] == 1600, "shape: 1920x800 takes 3840x1600's folder");
CHECK(cfg_shape_size(2560, 1600, sz) && sz[0] == 1280 && sz[1] == 800 && cfg_shape_size(3000, 2000, sz) &&
sz[0] == 1920 && sz[1] == 1280 && cfg_shape_size(5120, 1440, sz) && sz[0] == 3840 && sz[1] == 1080,
"shape: 16:10, 3:2 and 32:9 take their first size");
CHECK(!cfg_shape_size(1920, 1080, sz) && !cfg_shape_size(2560, 1440, sz) && !cfg_shape_size(1024, 768, sz),
"shape: none for 16:9 and 4:3");
// romfs.c shaped_name: the exact size's folder (an older setup wrote every size) wins, else the shape's
{
char dir[1100], f[1200];
snprintf(dir, sizeof dir, "%.*scfg-probe-shapes", (int)(cut + 1 - path), path);
static const char *const made[] = {"romfs-patches-2560x1080", "romfs-patches-xbox-5120x2160"};
for (int i = 0; i < 2; i++) {
snprintf(f, sizeof f, "%s/%s", dir, made[i]);
plat_file_mkdir_p(f);
snprintf(f, sizeof f, "%s/%s/manifest.tsv", dir, made[i]);
plat_file_write(f, "#\n", 2);
}
snprintf(f, sizeof f, "%s/romfs-index.bin", dir);
plat_setenv("TOTK_ROMFS_INDEX", f);
CHECK(romfs_shaped_ready(2560, 1080) && romfs_shaped_ready(5120, 2160) && !romfs_shaped_ready(3440, 1440),
"shape: 5120x2160 finds 2560x1080's layout folder, 3440x1440 does not");
CHECK(romfs_shaped_prompts_ready("romfs-patches-xbox", 5120, 2160) &&
!romfs_shaped_prompts_ready("romfs-patches-xbox", 2560, 1080) &&
!romfs_shaped_prompts_ready("romfs-patches-pc", 5120, 2160),
"shape: an older install's exact-size prompts folder serves that size only");
plat_setenv("TOTK_ROMFS_INDEX", NULL);
}
uint8_t pd[PAD_COUNT] = {0};
pd[PAD_SOUTH] = pd[PAD_NORTH] = 1;
CHECK(input_map_pad_buttons(pd, PAD_LAYOUT_NINTENDO) == (1ull << B_B | 1ull << B_X) &&
@@ -350,6 +392,14 @@ int probe_cfg(void) {
input_map_pad_buttons((uint8_t[PAD_COUNT]){[PAD_ZL] = 1}, PAD_LAYOUT_NINTENDO) == 1ull << B_ZL,
"bind: pad:rpaddle1 adds a back paddle, pad:default keeps the trigger");
input_map_reset();
CHECK(input_map_pad_buttons((uint8_t[PAD_COUNT]){[PAD_TOUCHPAD] = 1}, PAD_LAYOUT_NINTENDO) == 0 &&
input_map_pad_buttons((uint8_t[PAD_COUNT]){[PAD_TOUCHPAD] = 1}, PAD_LAYOUT_XBOX) == 0,
"pad layout: the touchpad click is unbound by default");
CHECK(input_map_bind("MINUS", "pad:default, pad:touchpad") &&
input_map_pad_buttons((uint8_t[PAD_COUNT]){[PAD_TOUCHPAD] = 1}, PAD_LAYOUT_XBOX) == 1ull << B_MINUS &&
input_map_pad_buttons((uint8_t[PAD_COUNT]){[PAD_BACK] = 1}, PAD_LAYOUT_XBOX) == 1ull << B_MINUS,
"bind: pad:touchpad adds the touchpad click, pad:default keeps the button");
input_map_reset();
// Bind lines for stick directions and default parts; the remapping rows' edits (input_map.c)
memset(dn, 0, sizeof dn);
dn[KEY_I] = 1;
+87 -10
View File
@@ -10,8 +10,9 @@
// (tools/cheats.py has the format). A cheat whose status line is missing or has another content hash was added or
// edited after the build: a data cheat runs anyway, a code cheat waits for the setup to run again.
//
// Data cheats run in a VM after Atmosphere's dmnt cheat VM (opcodes 0-A; the 0xC.. and 0xF.. extensions are not
// supported), every presented frame while their switch is on, with registers cleared before each cheat. Memory is the
// Data cheats run in a VM after Atmosphere's dmnt cheat VM (opcodes 0-A and C0-C3; C4 and the 0xF.. extensions are not
// supported), every presented frame while their switch is on, with registers and saved registers cleared before each
// cheat; the static registers (C3) stay, shared by all cheats, as dmnt keeps them. Memory is the
// guest's, mapped at its own addresses: main's image is written with its pages made writable for the write (read-only
// data included, as dmnt writes through the debugger), everything else after mem_query says the guest may read or write
// it. Buttons are input_poll's HidNpadButton bits, the same as the cheat format's key masks; they read 0 while the menu
@@ -53,6 +54,7 @@ static int g_n, g_cap, g_vm; // g_vm: some cheat needs cheats_frame
static char g_file[64]; // "<build id>.txt"
static char g_dir[PLAT_PATH_MAX]; // the folder of totk.exe, with a trailing separator
static int g_files; // mods whose file was read
static uint64_t g_static[256]; // C3's static registers: 0x00-0x7F loaded by cheats, 0x80-0xFF stored by them
static uint64_t g_main, g_main_size, g_code_end; // main's image; its code with the padding up to .rodata
static uint32_t fnv(const char *s, size_t n) {
@@ -96,6 +98,11 @@ static int op_len(const uint32_t *w, int left) {
case 5: case 7: n = 2; break;
case 9: n = 1 + (((f >> 8) & 0xF) ? (width == 8 ? 2 : 1) : 0); break;
case 0xA: n = ((f >> 8) & 0xF) == 2 || ((f >> 8) & 0xF) == 4 || ((f >> 8) & 0xF) == 5 ? 2 : 1; break;
case 0xC: // C0TcSX..: an address word for operand types 0 and 2, the value (two words at width 8) for 4
if (((f >> 24) & 0xF) != 0) n = 1;
else if (((f >> 8) & 0xF) == 0 || ((f >> 8) & 0xF) == 2) n = 2;
else n = ((f >> 8) & 0xF) == 4 ? (((f >> 20) & 0xF) == 8 ? 3 : 2) : 1;
break;
default: n = 1; break;
}
return n <= left ? n : 0;
@@ -105,12 +112,15 @@ static int op_len(const uint32_t *w, int left) {
static const char *vm_unsupported(const uint32_t *w, char *buf, int n) {
uint32_t f = w[0];
int t = f >> 28, width = (f >> 24) & 0xF;
if (t > 0xA || (t == 5 && ((f >> 12) & 0xF) > 1) || (t == 0xA && ((f >> 8) & 0xF) > 5)) {
int ext = f >> 24; // 0xC0-0xC3 run; C0's width is at bits 20-23 and its operand type (at most 5) at bits 8-11
if (ext == 0xC0) t = -1, width = (f >> 20) & 0xF;
if ((t > 0xA && (ext < 0xC1 || ext > 0xC3)) || (t == -1 && ((f >> 8) & 0xF) > 5) ||
(t == 5 && ((f >> 12) & 0xF) > 1) || (t == 0xA && ((f >> 8) & 0xF) > 5)) {
snprintf(buf, n, "opcode %08X is not supported", f);
return buf;
}
if ((t <= 1 || t == 5 || t == 6 || t == 7 || t == 9 || t == 0xA) && width != 1 && width != 2 && width != 4 &&
width != 8) {
width != 8) { // C0 too (t -1)
snprintf(buf, n, "opcode %08X has a bad width", f);
return buf;
}
@@ -227,7 +237,9 @@ static int compare(int cond, uint64_t a, uint64_t b) {
}
// From i (after a failed condition, or an else reached while running), the opcode to continue at: after the
// matching else (else_ok) or end.
// matching else (else_ok) or end. Conditions 1, 8 and C0 open a block, as in Atmosphere's dmnt
// (stratosphere/dmnt/source/cheat/impl/dmnt_cheat_vm.cpp, CheatVirtualMachine::SkipConditionalBlock), which steps
// over whole opcodes so that an operand word reading 20000000 ends nothing.
static int skip(const uint32_t *w, int n, int i, int else_ok) {
int depth = 0;
while (i < n) {
@@ -236,17 +248,41 @@ static int skip(const uint32_t *w, int n, int i, int else_ok) {
uint32_t f = w[i];
int t = f >> 28;
i += len;
if (t == 1 || t == 8) depth++;
else if (t == 2 && ((f >> 24) & 0xF) == 0 && depth-- == 0) return i;
else if (t == 2 && depth == 0 && else_ok) return i;
if (t == 1 || t == 8 || f >> 24 == 0xC0) depth++;
else if (t == 2 && ((f >> 24) & 0xF) == 1) { // else; any other 2x000000 ends, as dmnt decodes is_else
if (depth == 0 && else_ok) return i;
} else if (t == 2 && depth-- == 0) {
return i;
}
}
return n;
}
static uint64_t imm64(const uint32_t *w) { return (uint64_t)w[0] << 32 | w[1]; }
// C0TcSX..: if register S <cond c> operand X, all cut to width T; the operand as Atmosphere's dmnt reads it
// (stratosphere/dmnt/source/cheat/impl/dmnt_cheat_vm.cpp, CheatVirtualMachine::DecodeNextOpcode and Execute,
// BeginRegisterConditionalBlock). X: 0 region M + a, 1 region M + register r, 2 register R + a, 3 register R +
// register r, 4 the value V, 5 register R. M, R at bits 4-7, r and the top bits of a at bits 0-3. A read the guest
// cannot make compares as 0: dmnt ignores the read's result and compares the zero it started with.
static int reg_cond(const uint32_t *w, const uint64_t *r) {
uint32_t f = w[0];
int width = (f >> 20) & 0xF, cond = (f >> 16) & 0xF, a4 = (f >> 4) & 0xF, a0 = f & 0xF;
uint64_t src = masked(width, r[(f >> 12) & 0xF]), b = 0, addr;
switch ((f >> 8) & 0xF) { // w[1] exists for types 0, 2 and 4 only (op_len)
case 0: addr = region(a4) + (((uint64_t)a0 << 32) | w[1]); break;
case 1: addr = region(a4) + r[a0]; break;
case 2: addr = r[a4] + (((uint64_t)a0 << 32) | w[1]); break;
case 3: addr = r[a4] + r[a0]; break;
case 4: return compare(cond, src, width == 8 ? imm64(w + 1) : masked(width, w[1]));
default: return compare(cond, src, masked(width, r[a4]));
}
if (!rd(addr, width, &b)) b = 0;
return compare(cond, src, b);
}
static void run(const Cheat *c, uint64_t keys) {
uint64_t r[16] = {0};
uint64_t r[16] = {0}, saved[16] = {0};
int loop[16] = {0};
const uint32_t *w = c->words;
int n = c->n, budget = OPS_PER_RUN;
@@ -323,6 +359,44 @@ static void run(const Cheat *c, uint64_t keys) {
if (z) r[y] += (uint64_t)width;
break;
}
case 0xC: // C1-C3 as dmnt's Execute runs SaveRestoreRegister(Mask) and ReadWriteStaticRegister
switch (f >> 24) {
case 0xC0:
if (!reg_cond(w + i, r)) {
i = skip(w, n, i + len, 1);
continue;
}
break;
case 0xC1: { // C10D0Sx0: x 0 register D = saved S, 1 saved D = register S, 2 saved D = 0,
// 3 register D = 0
int d = (f >> 16) & 0xF, s = (f >> 8) & 0xF, op = (f >> 4) & 0xF;
if (op == 1) saved[d] = r[s];
else if (op == 2) saved[d] = 0;
else if (op == 3) r[d] = 0;
else r[d] = saved[s]; // dmnt restores for any other x too
break;
}
case 0xC2: { // C2x0XXXX: for each register in the mask X, x as C1 with D = S = that register
int op = (f >> 20) & 0xF;
for (int k = 0; k < 16; k++) {
if (!(f >> k & 1)) continue;
if (op == 1) saved[k] = r[k];
else if (op == 2) saved[k] = 0;
else if (op == 3) r[k] = 0;
else r[k] = saved[k];
}
break;
}
case 0xC3: { // C3000XXx: static register XX < 0x80 into register x, else register x into it
int s = (f >> 4) & 0xFF;
if (s < 0x80) r[f & 0xF] = g_static[s];
else g_static[s] = r[f & 0xF];
break;
}
default:
return;
}
break;
default:
return;
}
@@ -501,7 +575,10 @@ static void rebuild_if_needed(const char *dir) {
int ok = plat_spawn(argv, dir);
free(cmd);
if (!ok) {
rt_log("cheats: the rebuild did not start; the game starts with the code it has");
// The skip key is for a rebuild that ran and failed or was cancelled. One that never started (a command whose
// program is gone) leaves no key, so the next start and "Rebuild the game now" try it again.
plat_fs_unlink(skip_path);
rt_log("cheats: the rebuild did not start (%s); the game starts with the code it has", cmd_path);
return;
}
prof_write(); // plat_exit runs no atexit handlers
+247 -148
View File
@@ -1,156 +1,255 @@
// Generated by tools/build_shaders.py from runtime/shaders/contact_shadow.comp; do not edit.
// source sha256 b452a1920e59c4d83b2c994b0c6573cc1e479f7032a1310a7432d11c5c2e9908
// source sha256 88275d28f96ea19281dbcd08a440ff79821716210894596ffb05ba7bc572dc9a
#pragma once
#include <stdint.h>
static const uint32_t contact_shadow_spv[] = {
0x07230203, 0x00010300, 0x000d000b, 0x000001b1, 0x00000000, 0x00020011, 0x00000001, 0x00020011,
0x07230203, 0x00010300, 0x000d000b, 0x000003e4, 0x00000000, 0x00020011, 0x00000001, 0x00020011,
0x00000031, 0x0006000b, 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e,
0x00000000, 0x00000001, 0x0006000f, 0x00000005, 0x00000004, 0x6e69616d, 0x00000000, 0x00000069,
0x00060010, 0x00000004, 0x00000011, 0x00000008, 0x00000008, 0x00000001, 0x00030047, 0x00000027,
0x00000002, 0x00050048, 0x00000027, 0x00000000, 0x00000023, 0x00000000, 0x00050048, 0x00000027,
0x00000001, 0x00000023, 0x00000010, 0x00050048, 0x00000027, 0x00000002, 0x00000023, 0x00000020,
0x00050048, 0x00000027, 0x00000003, 0x00000023, 0x00000030, 0x00040047, 0x00000069, 0x0000000b,
0x0000001c, 0x00040047, 0x00000088, 0x00000021, 0x00000000, 0x00040047, 0x00000088, 0x00000022,
0x00000000, 0x00040047, 0x0000013f, 0x00000021, 0x00000001, 0x00040047, 0x0000013f, 0x00000022,
0x00000000, 0x00040047, 0x0000015c, 0x0000000b, 0x00000019, 0x00020013, 0x00000002, 0x00030021,
0x00000000, 0x00000001, 0x0006000f, 0x00000005, 0x00000004, 0x6e69616d, 0x00000000, 0x0000011f,
0x00060010, 0x00000004, 0x00000011, 0x00000008, 0x00000008, 0x00000001, 0x00030047, 0x00000033,
0x00000002, 0x00050048, 0x00000033, 0x00000000, 0x00000023, 0x00000000, 0x00050048, 0x00000033,
0x00000001, 0x00000023, 0x00000010, 0x00050048, 0x00000033, 0x00000002, 0x00000023, 0x00000020,
0x00050048, 0x00000033, 0x00000003, 0x00000023, 0x00000030, 0x00040047, 0x00000086, 0x00000021,
0x00000000, 0x00040047, 0x00000086, 0x00000022, 0x00000000, 0x00040047, 0x0000011f, 0x0000000b,
0x0000001c, 0x00040047, 0x000001ff, 0x00000021, 0x00000001, 0x00040047, 0x000001ff, 0x00000022,
0x00000000, 0x00040047, 0x0000021c, 0x0000000b, 0x00000019, 0x00020013, 0x00000002, 0x00030021,
0x00000003, 0x00000002, 0x00030016, 0x00000006, 0x00000020, 0x00040017, 0x00000007, 0x00000006,
0x00000002, 0x00040017, 0x0000000a, 0x00000006, 0x00000003, 0x0004002b, 0x00000006, 0x0000001b,
0x40000000, 0x0004002b, 0x00000006, 0x0000001d, 0x3f800000, 0x00040015, 0x00000020, 0x00000020,
0x00000000, 0x0004002b, 0x00000020, 0x00000021, 0x00000000, 0x00040017, 0x00000026, 0x00000006,
0x00000004, 0x0006001e, 0x00000027, 0x00000026, 0x00000026, 0x00000026, 0x00000026, 0x00040020,
0x00000028, 0x00000009, 0x00000027, 0x0004003b, 0x00000028, 0x00000029, 0x00000009, 0x00040015,
0x0000002a, 0x00000020, 0x00000001, 0x0004002b, 0x0000002a, 0x0000002b, 0x00000000, 0x00040020,
0x0000002c, 0x00000009, 0x00000006, 0x0004002b, 0x00000020, 0x00000030, 0x00000001, 0x0004002b,
0x00000020, 0x0000003f, 0x00000002, 0x0004002b, 0x00000006, 0x00000043, 0x3f000000, 0x0004002b,
0x00000006, 0x00000059, 0x4253ee82, 0x0004002b, 0x00000006, 0x0000005b, 0x3d897143, 0x0004002b,
0x00000006, 0x0000005c, 0x3bbf4590, 0x0005002c, 0x00000007, 0x0000005d, 0x0000005b, 0x0000005c,
0x00040017, 0x00000064, 0x0000002a, 0x00000002, 0x00040017, 0x00000067, 0x00000020, 0x00000003,
0x00040020, 0x00000068, 0x00000001, 0x00000067, 0x0004003b, 0x00000068, 0x00000069, 0x00000001,
0x00040017, 0x0000006a, 0x00000020, 0x00000002, 0x00020014, 0x0000006e, 0x0004002b, 0x0000002a,
0x00000072, 0x00000002, 0x00090019, 0x00000085, 0x00000006, 0x00000001, 0x00000000, 0x00000000,
0x00000000, 0x00000001, 0x00000000, 0x0003001b, 0x00000086, 0x00000085, 0x00040020, 0x00000087,
0x00000000, 0x00000086, 0x0004003b, 0x00000087, 0x00000088, 0x00000000, 0x0004002b, 0x00000006,
0x0000008f, 0x00000000, 0x0004002b, 0x00000006, 0x00000092, 0x3f7fbe77, 0x0004002b, 0x00000006,
0x0000009e, 0x42a00000, 0x00040020, 0x000000a8, 0x00000009, 0x00000026, 0x0004002b, 0x0000002a,
0x000000b4, 0x00000001, 0x0004002b, 0x00000020, 0x000000b5, 0x00000003, 0x0004002b, 0x00000006,
0x000000bb, 0x3ecccccd, 0x0004002b, 0x00000006, 0x000000bc, 0x40400000, 0x0004002b, 0x00000006,
0x000000c0, 0x3e19999a, 0x0004002b, 0x00000006, 0x000000c2, 0x3c23d70a, 0x0004002b, 0x00000006,
0x000000cb, 0x3e800000, 0x0004002b, 0x0000002a, 0x000000da, 0x00000010, 0x0004002b, 0x00000006,
0x000000e6, 0xbd4ccccd, 0x0005002c, 0x00000007, 0x000000f0, 0x0000008f, 0x0000008f, 0x00040017,
0x000000f1, 0x0000006e, 0x00000002, 0x0005002c, 0x00000007, 0x000000f8, 0x0000001d, 0x0000001d,
0x0004002b, 0x00000006, 0x00000114, 0x3ca3d70a, 0x0004002b, 0x00000006, 0x00000116, 0x3b03126f,
0x0004002b, 0x00000006, 0x0000012b, 0x42200000, 0x0004002b, 0x0000002a, 0x00000137, 0x00000003,
0x00090019, 0x0000013d, 0x00000006, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000002,
0x00000008, 0x00040020, 0x0000013e, 0x00000000, 0x0000013d, 0x0004003b, 0x0000013e, 0x0000013f,
0x00000000, 0x0004002b, 0x00000020, 0x0000015b, 0x00000008, 0x0006002c, 0x00000067, 0x0000015c,
0x0000015b, 0x0000015b, 0x00000030, 0x0005002c, 0x00000007, 0x000001ad, 0x00000043, 0x00000043,
0x0004002b, 0x00000006, 0x000001af, 0x3dcccccd, 0x0004002b, 0x00000006, 0x000001b0, 0x3d800000,
0x00000002, 0x00040017, 0x0000000a, 0x00000006, 0x00000003, 0x00040015, 0x00000019, 0x00000020,
0x00000001, 0x00040017, 0x0000001a, 0x00000019, 0x00000002, 0x0004002b, 0x00000006, 0x00000027,
0x40000000, 0x0004002b, 0x00000006, 0x00000029, 0x3f800000, 0x00040015, 0x0000002c, 0x00000020,
0x00000000, 0x0004002b, 0x0000002c, 0x0000002d, 0x00000000, 0x00040017, 0x00000032, 0x00000006,
0x00000004, 0x0006001e, 0x00000033, 0x00000032, 0x00000032, 0x00000032, 0x00000032, 0x00040020,
0x00000034, 0x00000009, 0x00000033, 0x0004003b, 0x00000034, 0x00000035, 0x00000009, 0x0004002b,
0x00000019, 0x00000036, 0x00000000, 0x00040020, 0x00000037, 0x00000009, 0x00000006, 0x0004002b,
0x0000002c, 0x0000003b, 0x00000001, 0x0004002b, 0x0000002c, 0x0000004a, 0x00000002, 0x0004002b,
0x00000006, 0x0000004e, 0x3f000000, 0x0004002b, 0x00000006, 0x00000064, 0x4253ee82, 0x0004002b,
0x00000006, 0x00000066, 0x3d897143, 0x0004002b, 0x00000006, 0x00000067, 0x3bbf4590, 0x0005002c,
0x00000007, 0x00000068, 0x00000066, 0x00000067, 0x0005002c, 0x0000001a, 0x00000070, 0x00000036,
0x00000036, 0x0004002b, 0x00000019, 0x00000071, 0x00000002, 0x00040020, 0x00000072, 0x00000009,
0x00000032, 0x0004002b, 0x00000019, 0x00000077, 0x00000001, 0x00090019, 0x00000083, 0x00000006,
0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000001, 0x00000000, 0x0003001b, 0x00000084,
0x00000083, 0x00040020, 0x00000085, 0x00000000, 0x00000084, 0x0004003b, 0x00000085, 0x00000086,
0x00000000, 0x0005002c, 0x0000001a, 0x0000009d, 0x00000077, 0x00000036, 0x0005002c, 0x0000001a,
0x000000a8, 0x00000036, 0x00000077, 0x00020014, 0x000000b1, 0x0004002b, 0x00000006, 0x00000110,
0x00000000, 0x00040017, 0x0000011d, 0x0000002c, 0x00000003, 0x00040020, 0x0000011e, 0x00000001,
0x0000011d, 0x0004003b, 0x0000011e, 0x0000011f, 0x00000001, 0x00040017, 0x00000120, 0x0000002c,
0x00000002, 0x0004002b, 0x00000006, 0x00000140, 0x3f7fbe77, 0x0004002b, 0x00000006, 0x0000014c,
0x42a00000, 0x0004002b, 0x00000006, 0x00000161, 0x3e99999a, 0x0004002b, 0x0000002c, 0x00000173,
0x00000003, 0x0004002b, 0x00000006, 0x00000179, 0x3ecccccd, 0x0004002b, 0x00000006, 0x0000017a,
0x40400000, 0x0004002b, 0x00000006, 0x0000017e, 0x3e19999a, 0x0004002b, 0x00000006, 0x00000180,
0x3c23d70a, 0x0004002b, 0x00000006, 0x00000189, 0x3e800000, 0x0004002b, 0x00000019, 0x00000198,
0x00000010, 0x0004002b, 0x00000006, 0x000001a4, 0xbd4ccccd, 0x0005002c, 0x00000007, 0x000001ae,
0x00000110, 0x00000110, 0x00040017, 0x000001af, 0x000000b1, 0x00000002, 0x0005002c, 0x00000007,
0x000001b6, 0x00000029, 0x00000029, 0x0004002b, 0x00000006, 0x000001d2, 0x3ca3d70a, 0x0004002b,
0x00000006, 0x000001d4, 0x3b03126f, 0x0004002b, 0x00000006, 0x000001e9, 0x42200000, 0x0004002b,
0x00000019, 0x000001f7, 0x00000003, 0x00090019, 0x000001fd, 0x00000006, 0x00000001, 0x00000000,
0x00000000, 0x00000000, 0x00000002, 0x00000008, 0x00040020, 0x000001fe, 0x00000000, 0x000001fd,
0x0004003b, 0x000001fe, 0x000001ff, 0x00000000, 0x0004002b, 0x0000002c, 0x0000021b, 0x00000008,
0x0006002c, 0x0000011d, 0x0000021c, 0x0000021b, 0x0000021b, 0x0000003b, 0x0005002c, 0x00000007,
0x000003df, 0x0000004e, 0x0000004e, 0x0005002c, 0x0000001a, 0x000003e0, 0x00000077, 0x00000077,
0x0004002b, 0x00000006, 0x000003e2, 0x3dcccccd, 0x0004002b, 0x00000006, 0x000003e3, 0x3d800000,
0x00050036, 0x00000002, 0x00000004, 0x00000000, 0x00000003, 0x000200f8, 0x00000005, 0x000300f7,
0x0000015d, 0x00000000, 0x000300fb, 0x00000021, 0x0000015e, 0x000200f8, 0x0000015e, 0x0004003d,
0x00000067, 0x0000006b, 0x00000069, 0x0007004f, 0x0000006a, 0x0000006c, 0x0000006b, 0x0000006b,
0x00000000, 0x00000001, 0x0004007c, 0x00000064, 0x0000006d, 0x0000006c, 0x00050051, 0x0000002a,
0x00000071, 0x0000006d, 0x00000000, 0x00060041, 0x0000002c, 0x00000073, 0x00000029, 0x00000072,
0x00000021, 0x0004003d, 0x00000006, 0x00000074, 0x00000073, 0x0004006e, 0x0000002a, 0x00000075,
0x00000074, 0x000500af, 0x0000006e, 0x00000076, 0x00000071, 0x00000075, 0x000400a8, 0x0000006e,
0x00000077, 0x00000076, 0x000300f7, 0x00000079, 0x00000000, 0x000400fa, 0x00000077, 0x00000078,
0x00000079, 0x000200f8, 0x00000078, 0x00050051, 0x0000002a, 0x0000007b, 0x0000006d, 0x00000001,
0x00060041, 0x0000002c, 0x0000007c, 0x00000029, 0x00000072, 0x00000030, 0x0004003d, 0x00000006,
0x0000007d, 0x0000007c, 0x0004006e, 0x0000002a, 0x0000007e, 0x0000007d, 0x000500af, 0x0000006e,
0x0000007f, 0x0000007b, 0x0000007e, 0x000200f9, 0x00000079, 0x000200f8, 0x00000079, 0x000700f5,
0x0000006e, 0x00000080, 0x00000076, 0x0000015e, 0x0000007f, 0x00000078, 0x000300f7, 0x00000082,
0x00000000, 0x000400fa, 0x00000080, 0x00000081, 0x00000082, 0x000200f8, 0x00000081, 0x000200f9,
0x0000015d, 0x000200f8, 0x00000082, 0x0004003d, 0x00000086, 0x00000089, 0x00000088, 0x00040064,
0x00000085, 0x0000008b, 0x00000089, 0x0007005f, 0x00000026, 0x0000008c, 0x0000008b, 0x0000006d,
0x00000002, 0x0000002b, 0x00050051, 0x00000006, 0x0000008d, 0x0000008c, 0x00000000, 0x000500bc,
0x0000006e, 0x00000090, 0x0000008d, 0x0000008f, 0x000500be, 0x0000006e, 0x00000093, 0x0000008d,
0x00000092, 0x000500a6, 0x0000006e, 0x00000094, 0x00000090, 0x00000093, 0x000300f7, 0x00000096,
0x00000000, 0x000400fa, 0x00000094, 0x00000095, 0x00000096, 0x000200f8, 0x00000095, 0x000200f9,
0x0000015d, 0x000200f8, 0x00000096, 0x00060041, 0x0000002c, 0x0000009a, 0x00000029, 0x0000002b,
0x0000003f, 0x0004003d, 0x00000006, 0x0000009b, 0x0000009a, 0x00050085, 0x00000006, 0x0000009c,
0x0000008d, 0x0000009b, 0x000500ba, 0x0000006e, 0x0000009f, 0x0000009c, 0x0000009e, 0x000300f7,
0x000000a1, 0x00000000, 0x000400fa, 0x0000009f, 0x000000a0, 0x000000a1, 0x000200f8, 0x000000a0,
0x000200f9, 0x0000015d, 0x000200f8, 0x000000a1, 0x0004006f, 0x00000007, 0x000000a5, 0x0000006d,
0x00050081, 0x00000007, 0x000000a7, 0x000000a5, 0x000001ad, 0x00050041, 0x000000a8, 0x000000a9,
0x00000029, 0x00000072, 0x0004003d, 0x00000026, 0x000000aa, 0x000000a9, 0x0007004f, 0x00000007,
0x000000ab, 0x000000aa, 0x000000aa, 0x00000002, 0x00000003, 0x00050085, 0x00000007, 0x000000ac,
0x000000a7, 0x000000ab, 0x0005008e, 0x00000007, 0x00000167, 0x000000ac, 0x0000001b, 0x00050083,
0x00000007, 0x00000169, 0x00000167, 0x000000f8, 0x00050051, 0x00000006, 0x0000016b, 0x00000169,
0x00000000, 0x00050085, 0x00000006, 0x0000016d, 0x0000016b, 0x0000009c, 0x00060041, 0x0000002c,
0x0000016e, 0x00000029, 0x0000002b, 0x00000021, 0x0004003d, 0x00000006, 0x0000016f, 0x0000016e,
0x00050088, 0x00000006, 0x00000170, 0x0000016d, 0x0000016f, 0x00050051, 0x00000006, 0x00000172,
0x00000169, 0x00000001, 0x0004007f, 0x00000006, 0x00000173, 0x00000172, 0x00050085, 0x00000006,
0x00000175, 0x00000173, 0x0000009c, 0x00060041, 0x0000002c, 0x00000176, 0x00000029, 0x0000002b,
0x00000030, 0x0004003d, 0x00000006, 0x00000177, 0x00000176, 0x00050088, 0x00000006, 0x00000178,
0x00000175, 0x00000177, 0x0004007f, 0x00000006, 0x0000017a, 0x0000009c, 0x00060050, 0x0000000a,
0x0000017b, 0x00000170, 0x00000178, 0x0000017a, 0x00060041, 0x0000002c, 0x000000b6, 0x00000029,
0x000000b4, 0x000000b5, 0x0004003d, 0x00000006, 0x000000b7, 0x000000b6, 0x00050085, 0x00000006,
0x000000ba, 0x0000009c, 0x000001af, 0x0008000c, 0x00000006, 0x000000bd, 0x00000001, 0x0000002b,
0x000000ba, 0x000000bb, 0x000000bc, 0x00050085, 0x00000006, 0x000000be, 0x000000b7, 0x000000bd,
0x00050085, 0x00000006, 0x000000c3, 0x0000009c, 0x000000c2, 0x00050081, 0x00000006, 0x000000c4,
0x000000c0, 0x000000c3, 0x00050085, 0x00000006, 0x000000c8, 0x000000be, 0x000001b0, 0x00050094,
0x00000006, 0x0000017f, 0x000000a5, 0x0000005d, 0x0006000c, 0x00000006, 0x00000180, 0x00000001,
0x0000000a, 0x0000017f, 0x00050085, 0x00000006, 0x00000181, 0x00000059, 0x00000180, 0x0006000c,
0x00000006, 0x00000182, 0x00000001, 0x0000000a, 0x00000181, 0x00050081, 0x00000006, 0x000000d0,
0x000000cb, 0x00000182, 0x00050085, 0x00000006, 0x000000d1, 0x000000c8, 0x000000d0, 0x000200f9,
0x000000d4, 0x000200f8, 0x000000d4, 0x000700f5, 0x00000006, 0x000001a7, 0x000000d1, 0x000000a1,
0x00000129, 0x000000d7, 0x000700f5, 0x0000002a, 0x000001a6, 0x0000002b, 0x000000a1, 0x00000126,
0x000000d7, 0x000500b1, 0x0000006e, 0x000000db, 0x000001a6, 0x000000da, 0x000400f6, 0x000000d6,
0x000000d7, 0x00000000, 0x000400fa, 0x000000db, 0x000000d5, 0x000000d6, 0x000200f8, 0x000000d5,
0x00050041, 0x000000a8, 0x000000de, 0x00000029, 0x000000b4, 0x0004003d, 0x00000026, 0x000000df,
0x000000de, 0x0008004f, 0x0000000a, 0x000000e0, 0x000000df, 0x000000df, 0x00000000, 0x00000001,
0x00000002, 0x0005008e, 0x0000000a, 0x000000e2, 0x000000e0, 0x000001a7, 0x00050081, 0x0000000a,
0x000000e3, 0x0000017b, 0x000000e2, 0x00050051, 0x00000006, 0x000000e5, 0x000000e3, 0x00000002,
0x000500be, 0x0000006e, 0x000000e7, 0x000000e5, 0x000000e6, 0x000300f7, 0x000000e9, 0x00000000,
0x000400fa, 0x000000e7, 0x000000e8, 0x000000e9, 0x000200f8, 0x000000e8, 0x000200f9, 0x000000d6,
0x000200f8, 0x000000e9, 0x0004007f, 0x00000006, 0x00000188, 0x000000e5, 0x00050051, 0x00000006,
0x0000018a, 0x000000e3, 0x00000000, 0x00050085, 0x00000006, 0x0000018b, 0x00000043, 0x0000018a,
0x00050085, 0x00000006, 0x0000018e, 0x0000018b, 0x0000016f, 0x00050088, 0x00000006, 0x00000190,
0x0000018e, 0x00000188, 0x00050081, 0x00000006, 0x00000191, 0x00000043, 0x00000190, 0x00050051,
0x00000006, 0x00000193, 0x000000e3, 0x00000001, 0x00050085, 0x00000006, 0x00000194, 0x00000043,
0x00000193, 0x00050085, 0x00000006, 0x00000197, 0x00000194, 0x00000177, 0x00050088, 0x00000006,
0x00000199, 0x00000197, 0x00000188, 0x00050083, 0x00000006, 0x0000019a, 0x00000043, 0x00000199,
0x00050050, 0x00000007, 0x0000019b, 0x00000191, 0x0000019a, 0x000500b8, 0x000000f1, 0x000000f2,
0x0000019b, 0x000000f0, 0x0004009a, 0x0000006e, 0x000000f3, 0x000000f2, 0x000400a8, 0x0000006e,
0x000000f4, 0x000000f3, 0x000300f7, 0x000000f6, 0x00000000, 0x000400fa, 0x000000f4, 0x000000f5,
0x000000f6, 0x000200f8, 0x000000f5, 0x000500ba, 0x000000f1, 0x000000f9, 0x0000019b, 0x000000f8,
0x0004009a, 0x0000006e, 0x000000fa, 0x000000f9, 0x000200f9, 0x000000f6, 0x000200f8, 0x000000f6,
0x000700f5, 0x0000006e, 0x000000fb, 0x000000f3, 0x000000e9, 0x000000fa, 0x000000f5, 0x000300f7,
0x000000fd, 0x00000000, 0x000400fa, 0x000000fb, 0x000000fc, 0x000000fd, 0x000200f8, 0x000000fc,
0x000200f9, 0x000000d6, 0x000200f8, 0x000000fd, 0x0004003d, 0x00000086, 0x00000100, 0x00000088,
0x0007004f, 0x00000007, 0x00000104, 0x000000aa, 0x000000aa, 0x00000000, 0x00000001, 0x00050085,
0x00000007, 0x00000105, 0x0000019b, 0x00000104, 0x0004006e, 0x00000064, 0x00000106, 0x00000105,
0x00040064, 0x00000085, 0x00000107, 0x00000100, 0x0007005f, 0x00000026, 0x00000108, 0x00000107,
0x00000106, 0x00000002, 0x0000002b, 0x00050051, 0x00000006, 0x00000109, 0x00000108, 0x00000000,
0x00050085, 0x00000006, 0x0000010c, 0x00000109, 0x0000009b, 0x00050083, 0x00000006, 0x00000112,
0x00000188, 0x0000010c, 0x00050085, 0x00000006, 0x00000117, 0x0000009c, 0x00000116, 0x00050081,
0x00000006, 0x00000118, 0x00000114, 0x00000117, 0x000500ba, 0x0000006e, 0x00000119, 0x00000112,
0x00000118, 0x000500b8, 0x0000006e, 0x0000011c, 0x00000112, 0x000000c4, 0x000500a7, 0x0000006e,
0x0000011d, 0x00000119, 0x0000011c, 0x000300f7, 0x0000011f, 0x00000000, 0x000400fa, 0x0000011d,
0x0000011e, 0x0000011f, 0x000200f8, 0x0000011e, 0x0004006f, 0x00000006, 0x00000121, 0x000001a6,
0x00050085, 0x00000006, 0x00000122, 0x00000121, 0x000001b0, 0x00050083, 0x00000006, 0x00000123,
0x0000001d, 0x00000122, 0x000200f9, 0x000000d6, 0x000200f8, 0x0000011f, 0x000200f9, 0x000000d7,
0x000200f8, 0x000000d7, 0x00050080, 0x0000002a, 0x00000126, 0x000001a6, 0x000000b4, 0x00050081,
0x00000006, 0x00000129, 0x000001a7, 0x000000c8, 0x000200f9, 0x000000d4, 0x000200f8, 0x000000d6,
0x000b00f5, 0x00000006, 0x000001aa, 0x0000008f, 0x000000d4, 0x0000008f, 0x000000e8, 0x0000008f,
0x000000fc, 0x00000123, 0x0000011e, 0x0008000c, 0x00000006, 0x0000012d, 0x00000001, 0x00000031,
0x0000012b, 0x0000009e, 0x0000009c, 0x00050083, 0x00000006, 0x0000012e, 0x0000001d, 0x0000012d,
0x00060041, 0x0000002c, 0x00000130, 0x00000029, 0x0000002b, 0x000000b5, 0x0004003d, 0x00000006,
0x00000131, 0x00000130, 0x00050085, 0x00000006, 0x00000133, 0x00000131, 0x000001aa, 0x00050085,
0x00000006, 0x00000135, 0x00000133, 0x0000012e, 0x00050083, 0x00000006, 0x00000136, 0x0000001d,
0x00000135, 0x00060041, 0x0000002c, 0x00000138, 0x00000029, 0x00000137, 0x00000021, 0x0004003d,
0x00000006, 0x00000139, 0x00000138, 0x000500ba, 0x0000006e, 0x0000013a, 0x00000139, 0x00000043,
0x000300f7, 0x0000013c, 0x00000000, 0x000400fa, 0x0000013a, 0x0000013b, 0x0000013c, 0x000200f8,
0x0000013b, 0x0004003d, 0x0000013d, 0x00000140, 0x0000013f, 0x00070050, 0x00000026, 0x00000143,
0x00000136, 0x00000136, 0x00000136, 0x0000001d, 0x00040063, 0x00000140, 0x0000006d, 0x00000143,
0x000200f9, 0x0000015d, 0x000200f8, 0x0000013c, 0x000500be, 0x0000006e, 0x00000146, 0x00000136,
0x00000092, 0x000300f7, 0x00000148, 0x00000000, 0x000400fa, 0x00000146, 0x00000147, 0x00000148,
0x000200f8, 0x00000147, 0x000200f9, 0x0000015d, 0x000200f8, 0x00000148, 0x0004003d, 0x0000013d,
0x0000014c, 0x0000013f, 0x00050062, 0x00000026, 0x0000014e, 0x0000014c, 0x0000006d, 0x0004003d,
0x0000013d, 0x0000014f, 0x0000013f, 0x0008004f, 0x0000000a, 0x00000152, 0x0000014e, 0x0000014e,
0x00000000, 0x00000001, 0x00000002, 0x0005008e, 0x0000000a, 0x00000154, 0x00000152, 0x00000136,
0x00050051, 0x00000006, 0x00000156, 0x0000014e, 0x00000003, 0x00050051, 0x00000006, 0x00000157,
0x00000154, 0x00000000, 0x00050051, 0x00000006, 0x00000158, 0x00000154, 0x00000001, 0x00050051,
0x00000006, 0x00000159, 0x00000154, 0x00000002, 0x00070050, 0x00000026, 0x0000015a, 0x00000157,
0x00000158, 0x00000159, 0x00000156, 0x00040063, 0x0000014f, 0x0000006d, 0x0000015a, 0x000200f9,
0x0000015d, 0x000200f8, 0x0000015d, 0x000100fd, 0x00010038,
0x0000021d, 0x00000000, 0x000300fb, 0x0000002d, 0x0000021e, 0x000200f8, 0x0000021e, 0x0004003d,
0x0000011d, 0x00000121, 0x0000011f, 0x0007004f, 0x00000120, 0x00000122, 0x00000121, 0x00000121,
0x00000000, 0x00000001, 0x0004007c, 0x0000001a, 0x00000123, 0x00000122, 0x00050051, 0x00000019,
0x00000125, 0x00000123, 0x00000000, 0x00060041, 0x00000037, 0x00000126, 0x00000035, 0x00000071,
0x0000002d, 0x0004003d, 0x00000006, 0x00000127, 0x00000126, 0x0004006e, 0x00000019, 0x00000128,
0x00000127, 0x000500af, 0x000000b1, 0x00000129, 0x00000125, 0x00000128, 0x000400a8, 0x000000b1,
0x0000012a, 0x00000129, 0x000300f7, 0x0000012c, 0x00000000, 0x000400fa, 0x0000012a, 0x0000012b,
0x0000012c, 0x000200f8, 0x0000012b, 0x00050051, 0x00000019, 0x0000012e, 0x00000123, 0x00000001,
0x00060041, 0x00000037, 0x0000012f, 0x00000035, 0x00000071, 0x0000003b, 0x0004003d, 0x00000006,
0x00000130, 0x0000012f, 0x0004006e, 0x00000019, 0x00000131, 0x00000130, 0x000500af, 0x000000b1,
0x00000132, 0x0000012e, 0x00000131, 0x000200f9, 0x0000012c, 0x000200f8, 0x0000012c, 0x000700f5,
0x000000b1, 0x00000133, 0x00000129, 0x0000021e, 0x00000132, 0x0000012b, 0x000300f7, 0x00000135,
0x00000000, 0x000400fa, 0x00000133, 0x00000134, 0x00000135, 0x000200f8, 0x00000134, 0x000200f9,
0x0000021d, 0x000200f8, 0x00000135, 0x0004003d, 0x00000084, 0x00000138, 0x00000086, 0x00040064,
0x00000083, 0x0000013a, 0x00000138, 0x0007005f, 0x00000032, 0x0000013b, 0x0000013a, 0x00000123,
0x00000002, 0x00000036, 0x00050051, 0x00000006, 0x0000013c, 0x0000013b, 0x00000000, 0x000500bc,
0x000000b1, 0x0000013e, 0x0000013c, 0x00000110, 0x000500be, 0x000000b1, 0x00000141, 0x0000013c,
0x00000140, 0x000500a6, 0x000000b1, 0x00000142, 0x0000013e, 0x00000141, 0x000300f7, 0x00000144,
0x00000000, 0x000400fa, 0x00000142, 0x00000143, 0x00000144, 0x000200f8, 0x00000143, 0x000200f9,
0x0000021d, 0x000200f8, 0x00000144, 0x00060041, 0x00000037, 0x00000148, 0x00000035, 0x00000036,
0x0000004a, 0x0004003d, 0x00000006, 0x00000149, 0x00000148, 0x00050085, 0x00000006, 0x0000014a,
0x0000013c, 0x00000149, 0x000500ba, 0x000000b1, 0x0000014d, 0x0000014a, 0x0000014c, 0x000300f7,
0x0000014f, 0x00000000, 0x000400fa, 0x0000014d, 0x0000014e, 0x0000014f, 0x000200f8, 0x0000014e,
0x000200f9, 0x0000021d, 0x000200f8, 0x0000014f, 0x0004006f, 0x00000007, 0x00000153, 0x00000123,
0x00050081, 0x00000007, 0x00000155, 0x00000153, 0x000003df, 0x00050041, 0x00000072, 0x00000156,
0x00000035, 0x00000071, 0x0004003d, 0x00000032, 0x00000157, 0x00000156, 0x0007004f, 0x00000007,
0x00000158, 0x00000157, 0x00000157, 0x00000002, 0x00000003, 0x00050085, 0x00000007, 0x00000159,
0x00000155, 0x00000158, 0x0005008e, 0x00000007, 0x00000226, 0x00000159, 0x00000027, 0x00050083,
0x00000007, 0x00000228, 0x00000226, 0x000001b6, 0x00050051, 0x00000006, 0x0000022a, 0x00000228,
0x00000000, 0x00050085, 0x00000006, 0x0000022c, 0x0000022a, 0x0000014a, 0x00060041, 0x00000037,
0x0000022d, 0x00000035, 0x00000036, 0x0000002d, 0x0004003d, 0x00000006, 0x0000022e, 0x0000022d,
0x00050088, 0x00000006, 0x0000022f, 0x0000022c, 0x0000022e, 0x00050051, 0x00000006, 0x00000231,
0x00000228, 0x00000001, 0x0004007f, 0x00000006, 0x00000232, 0x00000231, 0x00050085, 0x00000006,
0x00000234, 0x00000232, 0x0000014a, 0x00060041, 0x00000037, 0x00000235, 0x00000035, 0x00000036,
0x0000003b, 0x0004003d, 0x00000006, 0x00000236, 0x00000235, 0x00050088, 0x00000006, 0x00000237,
0x00000234, 0x00000236, 0x0004007f, 0x00000006, 0x00000239, 0x0000014a, 0x00060050, 0x0000000a,
0x0000023a, 0x0000022f, 0x00000237, 0x00000239, 0x0007004f, 0x00000007, 0x00000250, 0x00000157,
0x00000157, 0x00000000, 0x00000001, 0x0004006e, 0x0000001a, 0x00000251, 0x00000250, 0x00050082,
0x0000001a, 0x00000253, 0x00000251, 0x000003e0, 0x00050082, 0x0000001a, 0x00000255, 0x00000123,
0x0000009d, 0x0008000c, 0x0000001a, 0x000002c8, 0x00000001, 0x0000002d, 0x00000255, 0x00000070,
0x00000253, 0x0004006f, 0x00000007, 0x000002ca, 0x000002c8, 0x00050081, 0x00000007, 0x000002cc,
0x000002ca, 0x000003df, 0x00050085, 0x00000007, 0x000002d0, 0x000002cc, 0x00000158, 0x0004003d,
0x00000084, 0x000002d1, 0x00000086, 0x00040064, 0x00000083, 0x000002d3, 0x000002d1, 0x0007005f,
0x00000032, 0x000002d4, 0x000002d3, 0x000002c8, 0x00000002, 0x00000036, 0x00050051, 0x00000006,
0x000002d5, 0x000002d4, 0x00000000, 0x00050085, 0x00000006, 0x000002d8, 0x000002d5, 0x00000149,
0x0005008e, 0x00000007, 0x000002de, 0x000002d0, 0x00000027, 0x00050083, 0x00000007, 0x000002e0,
0x000002de, 0x000001b6, 0x00050051, 0x00000006, 0x000002e2, 0x000002e0, 0x00000000, 0x00050085,
0x00000006, 0x000002e4, 0x000002e2, 0x000002d8, 0x00050088, 0x00000006, 0x000002e7, 0x000002e4,
0x0000022e, 0x00050051, 0x00000006, 0x000002e9, 0x000002e0, 0x00000001, 0x0004007f, 0x00000006,
0x000002ea, 0x000002e9, 0x00050085, 0x00000006, 0x000002ec, 0x000002ea, 0x000002d8, 0x00050088,
0x00000006, 0x000002ef, 0x000002ec, 0x00000236, 0x0004007f, 0x00000006, 0x000002f1, 0x000002d8,
0x00060050, 0x0000000a, 0x000002f2, 0x000002e7, 0x000002ef, 0x000002f1, 0x00050080, 0x0000001a,
0x00000258, 0x00000123, 0x0000009d, 0x0008000c, 0x0000001a, 0x000002fe, 0x00000001, 0x0000002d,
0x00000258, 0x00000070, 0x00000253, 0x0004006f, 0x00000007, 0x00000300, 0x000002fe, 0x00050081,
0x00000007, 0x00000302, 0x00000300, 0x000003df, 0x00050085, 0x00000007, 0x00000306, 0x00000302,
0x00000158, 0x0004003d, 0x00000084, 0x00000307, 0x00000086, 0x00040064, 0x00000083, 0x00000309,
0x00000307, 0x0007005f, 0x00000032, 0x0000030a, 0x00000309, 0x000002fe, 0x00000002, 0x00000036,
0x00050051, 0x00000006, 0x0000030b, 0x0000030a, 0x00000000, 0x00050085, 0x00000006, 0x0000030e,
0x0000030b, 0x00000149, 0x0005008e, 0x00000007, 0x00000314, 0x00000306, 0x00000027, 0x00050083,
0x00000007, 0x00000316, 0x00000314, 0x000001b6, 0x00050051, 0x00000006, 0x00000318, 0x00000316,
0x00000000, 0x00050085, 0x00000006, 0x0000031a, 0x00000318, 0x0000030e, 0x00050088, 0x00000006,
0x0000031d, 0x0000031a, 0x0000022e, 0x00050051, 0x00000006, 0x0000031f, 0x00000316, 0x00000001,
0x0004007f, 0x00000006, 0x00000320, 0x0000031f, 0x00050085, 0x00000006, 0x00000322, 0x00000320,
0x0000030e, 0x00050088, 0x00000006, 0x00000325, 0x00000322, 0x00000236, 0x0004007f, 0x00000006,
0x00000327, 0x0000030e, 0x00060050, 0x0000000a, 0x00000328, 0x0000031d, 0x00000325, 0x00000327,
0x00050082, 0x0000001a, 0x0000025b, 0x00000123, 0x000000a8, 0x0008000c, 0x0000001a, 0x00000334,
0x00000001, 0x0000002d, 0x0000025b, 0x00000070, 0x00000253, 0x0004006f, 0x00000007, 0x00000336,
0x00000334, 0x00050081, 0x00000007, 0x00000338, 0x00000336, 0x000003df, 0x00050085, 0x00000007,
0x0000033c, 0x00000338, 0x00000158, 0x0004003d, 0x00000084, 0x0000033d, 0x00000086, 0x00040064,
0x00000083, 0x0000033f, 0x0000033d, 0x0007005f, 0x00000032, 0x00000340, 0x0000033f, 0x00000334,
0x00000002, 0x00000036, 0x00050051, 0x00000006, 0x00000341, 0x00000340, 0x00000000, 0x00050085,
0x00000006, 0x00000344, 0x00000341, 0x00000149, 0x0005008e, 0x00000007, 0x0000034a, 0x0000033c,
0x00000027, 0x00050083, 0x00000007, 0x0000034c, 0x0000034a, 0x000001b6, 0x00050051, 0x00000006,
0x0000034e, 0x0000034c, 0x00000000, 0x00050085, 0x00000006, 0x00000350, 0x0000034e, 0x00000344,
0x00050088, 0x00000006, 0x00000353, 0x00000350, 0x0000022e, 0x00050051, 0x00000006, 0x00000355,
0x0000034c, 0x00000001, 0x0004007f, 0x00000006, 0x00000356, 0x00000355, 0x00050085, 0x00000006,
0x00000358, 0x00000356, 0x00000344, 0x00050088, 0x00000006, 0x0000035b, 0x00000358, 0x00000236,
0x0004007f, 0x00000006, 0x0000035d, 0x00000344, 0x00060050, 0x0000000a, 0x0000035e, 0x00000353,
0x0000035b, 0x0000035d, 0x00050080, 0x0000001a, 0x0000025e, 0x00000123, 0x000000a8, 0x0008000c,
0x0000001a, 0x0000036a, 0x00000001, 0x0000002d, 0x0000025e, 0x00000070, 0x00000253, 0x0004006f,
0x00000007, 0x0000036c, 0x0000036a, 0x00050081, 0x00000007, 0x0000036e, 0x0000036c, 0x000003df,
0x00050085, 0x00000007, 0x00000372, 0x0000036e, 0x00000158, 0x0004003d, 0x00000084, 0x00000373,
0x00000086, 0x00040064, 0x00000083, 0x00000375, 0x00000373, 0x0007005f, 0x00000032, 0x00000376,
0x00000375, 0x0000036a, 0x00000002, 0x00000036, 0x00050051, 0x00000006, 0x00000377, 0x00000376,
0x00000000, 0x00050085, 0x00000006, 0x0000037a, 0x00000377, 0x00000149, 0x0005008e, 0x00000007,
0x00000380, 0x00000372, 0x00000027, 0x00050083, 0x00000007, 0x00000382, 0x00000380, 0x000001b6,
0x00050051, 0x00000006, 0x00000384, 0x00000382, 0x00000000, 0x00050085, 0x00000006, 0x00000386,
0x00000384, 0x0000037a, 0x00050088, 0x00000006, 0x00000389, 0x00000386, 0x0000022e, 0x00050051,
0x00000006, 0x0000038b, 0x00000382, 0x00000001, 0x0004007f, 0x00000006, 0x0000038c, 0x0000038b,
0x00050085, 0x00000006, 0x0000038e, 0x0000038c, 0x0000037a, 0x00050088, 0x00000006, 0x00000391,
0x0000038e, 0x00000236, 0x0004007f, 0x00000006, 0x00000393, 0x0000037a, 0x00060050, 0x0000000a,
0x00000394, 0x00000389, 0x00000391, 0x00000393, 0x000500aa, 0x000000b1, 0x00000262, 0x00000125,
0x00000036, 0x000400a8, 0x000000b1, 0x00000263, 0x00000262, 0x000300f7, 0x0000027a, 0x00000000,
0x000400fa, 0x00000263, 0x00000264, 0x0000027a, 0x000200f8, 0x00000264, 0x00050051, 0x00000019,
0x00000268, 0x00000253, 0x00000000, 0x000500b1, 0x000000b1, 0x00000269, 0x00000125, 0x00000268,
0x000300f7, 0x00000278, 0x00000000, 0x000400fa, 0x00000269, 0x0000026a, 0x00000278, 0x000200f8,
0x0000026a, 0x00050083, 0x00000006, 0x0000026f, 0x0000014a, 0x0000030e, 0x0006000c, 0x00000006,
0x00000270, 0x00000001, 0x00000004, 0x0000026f, 0x00050083, 0x00000006, 0x00000275, 0x000002d8,
0x0000014a, 0x0006000c, 0x00000006, 0x00000276, 0x00000001, 0x00000004, 0x00000275, 0x000500b8,
0x000000b1, 0x00000277, 0x00000270, 0x00000276, 0x000200f9, 0x00000278, 0x000200f8, 0x00000278,
0x000700f5, 0x000000b1, 0x00000279, 0x00000269, 0x00000264, 0x00000277, 0x0000026a, 0x000200f9,
0x0000027a, 0x000200f8, 0x0000027a, 0x000700f5, 0x000000b1, 0x0000027b, 0x00000262, 0x0000014f,
0x00000279, 0x00000278, 0x00050051, 0x00000019, 0x0000027d, 0x00000123, 0x00000001, 0x000500aa,
0x000000b1, 0x0000027e, 0x0000027d, 0x00000036, 0x000400a8, 0x000000b1, 0x0000027f, 0x0000027e,
0x000300f7, 0x00000296, 0x00000000, 0x000400fa, 0x0000027f, 0x00000280, 0x00000296, 0x000200f8,
0x00000280, 0x00050051, 0x00000019, 0x00000284, 0x00000253, 0x00000001, 0x000500b1, 0x000000b1,
0x00000285, 0x0000027d, 0x00000284, 0x000300f7, 0x00000294, 0x00000000, 0x000400fa, 0x00000285,
0x00000286, 0x00000294, 0x000200f8, 0x00000286, 0x00050083, 0x00000006, 0x0000028b, 0x0000014a,
0x0000037a, 0x0006000c, 0x00000006, 0x0000028c, 0x00000001, 0x00000004, 0x0000028b, 0x00050083,
0x00000006, 0x00000291, 0x00000344, 0x0000014a, 0x0006000c, 0x00000006, 0x00000292, 0x00000001,
0x00000004, 0x00000291, 0x000500b8, 0x000000b1, 0x00000293, 0x0000028c, 0x00000292, 0x000200f9,
0x00000294, 0x000200f8, 0x00000294, 0x000700f5, 0x000000b1, 0x00000295, 0x00000285, 0x00000280,
0x00000293, 0x00000286, 0x000200f9, 0x00000296, 0x000200f8, 0x00000296, 0x000700f5, 0x000000b1,
0x00000297, 0x0000027e, 0x0000027a, 0x00000295, 0x00000294, 0x000300f7, 0x000002a1, 0x00000000,
0x000400fa, 0x0000027b, 0x00000299, 0x0000029d, 0x000200f8, 0x00000299, 0x00050083, 0x0000000a,
0x0000029c, 0x00000328, 0x0000023a, 0x000200f9, 0x000002a1, 0x000200f8, 0x0000029d, 0x00050083,
0x0000000a, 0x000002a0, 0x0000023a, 0x000002f2, 0x000200f9, 0x000002a1, 0x000200f8, 0x000002a1,
0x000700f5, 0x0000000a, 0x000003d5, 0x0000029c, 0x00000299, 0x000002a0, 0x0000029d, 0x000300f7,
0x000002ac, 0x00000000, 0x000400fa, 0x00000297, 0x000002a4, 0x000002a8, 0x000200f8, 0x000002a4,
0x00050083, 0x0000000a, 0x000002a7, 0x00000394, 0x0000023a, 0x000200f9, 0x000002ac, 0x000200f8,
0x000002a8, 0x00050083, 0x0000000a, 0x000002ab, 0x0000023a, 0x0000035e, 0x000200f9, 0x000002ac,
0x000200f8, 0x000002ac, 0x000700f5, 0x0000000a, 0x000003d6, 0x000002a7, 0x000002a4, 0x000002ab,
0x000002a8, 0x0007000c, 0x0000000a, 0x000002b0, 0x00000001, 0x00000044, 0x000003d5, 0x000003d6,
0x0006000c, 0x0000000a, 0x000002b1, 0x00000001, 0x00000045, 0x000002b0, 0x00050094, 0x00000006,
0x000002b4, 0x000002b1, 0x0000023a, 0x000500ba, 0x000000b1, 0x000002b5, 0x000002b4, 0x00000110,
0x000300f7, 0x000002bb, 0x00000000, 0x000400fa, 0x000002b5, 0x000002b6, 0x000002b9, 0x000200f8,
0x000002b6, 0x0004007f, 0x0000000a, 0x000002b8, 0x000002b1, 0x000200f9, 0x000002bb, 0x000200f8,
0x000002b9, 0x000200f9, 0x000002bb, 0x000200f8, 0x000002bb, 0x000700f5, 0x0000000a, 0x000003d7,
0x000002b8, 0x000002b6, 0x000002b1, 0x000002b9, 0x00050041, 0x00000072, 0x00000167, 0x00000035,
0x00000077, 0x0004003d, 0x00000032, 0x00000168, 0x00000167, 0x0008004f, 0x0000000a, 0x00000169,
0x00000168, 0x00000168, 0x00000000, 0x00000001, 0x00000002, 0x00050094, 0x00000006, 0x0000016a,
0x000003d7, 0x00000169, 0x0008000c, 0x00000006, 0x0000016b, 0x00000001, 0x00000031, 0x00000110,
0x00000161, 0x0000016a, 0x000500bd, 0x000000b1, 0x0000016e, 0x0000016b, 0x00000110, 0x000300f7,
0x00000170, 0x00000000, 0x000400fa, 0x0000016e, 0x0000016f, 0x00000170, 0x000200f8, 0x0000016f,
0x000200f9, 0x0000021d, 0x000200f8, 0x00000170, 0x00060041, 0x00000037, 0x00000174, 0x00000035,
0x00000077, 0x00000173, 0x0004003d, 0x00000006, 0x00000175, 0x00000174, 0x00050085, 0x00000006,
0x00000178, 0x0000014a, 0x000003e2, 0x0008000c, 0x00000006, 0x0000017b, 0x00000001, 0x0000002b,
0x00000178, 0x00000179, 0x0000017a, 0x00050085, 0x00000006, 0x0000017c, 0x00000175, 0x0000017b,
0x00050085, 0x00000006, 0x00000181, 0x0000014a, 0x00000180, 0x00050081, 0x00000006, 0x00000182,
0x0000017e, 0x00000181, 0x00050085, 0x00000006, 0x00000186, 0x0000017c, 0x000003e3, 0x00050094,
0x00000006, 0x00000398, 0x00000153, 0x00000068, 0x0006000c, 0x00000006, 0x00000399, 0x00000001,
0x0000000a, 0x00000398, 0x00050085, 0x00000006, 0x0000039a, 0x00000064, 0x00000399, 0x0006000c,
0x00000006, 0x0000039b, 0x00000001, 0x0000000a, 0x0000039a, 0x00050081, 0x00000006, 0x0000018e,
0x00000189, 0x0000039b, 0x00050085, 0x00000006, 0x0000018f, 0x00000186, 0x0000018e, 0x000200f9,
0x00000192, 0x000200f8, 0x00000192, 0x000700f5, 0x00000006, 0x000003d9, 0x0000018f, 0x00000170,
0x000001e7, 0x00000195, 0x000700f5, 0x00000019, 0x000003d8, 0x00000036, 0x00000170, 0x000001e4,
0x00000195, 0x000500b1, 0x000000b1, 0x00000199, 0x000003d8, 0x00000198, 0x000400f6, 0x00000194,
0x00000195, 0x00000000, 0x000400fa, 0x00000199, 0x00000193, 0x00000194, 0x000200f8, 0x00000193,
0x0005008e, 0x0000000a, 0x000001a0, 0x00000169, 0x000003d9, 0x00050081, 0x0000000a, 0x000001a1,
0x0000023a, 0x000001a0, 0x00050051, 0x00000006, 0x000001a3, 0x000001a1, 0x00000002, 0x000500be,
0x000000b1, 0x000001a5, 0x000001a3, 0x000001a4, 0x000300f7, 0x000001a7, 0x00000000, 0x000400fa,
0x000001a5, 0x000001a6, 0x000001a7, 0x000200f8, 0x000001a6, 0x000200f9, 0x00000194, 0x000200f8,
0x000001a7, 0x0004007f, 0x00000006, 0x000003a1, 0x000001a3, 0x00050051, 0x00000006, 0x000003a3,
0x000001a1, 0x00000000, 0x00050085, 0x00000006, 0x000003a4, 0x0000004e, 0x000003a3, 0x00050085,
0x00000006, 0x000003a7, 0x000003a4, 0x0000022e, 0x00050088, 0x00000006, 0x000003a9, 0x000003a7,
0x000003a1, 0x00050081, 0x00000006, 0x000003aa, 0x0000004e, 0x000003a9, 0x00050051, 0x00000006,
0x000003ac, 0x000001a1, 0x00000001, 0x00050085, 0x00000006, 0x000003ad, 0x0000004e, 0x000003ac,
0x00050085, 0x00000006, 0x000003b0, 0x000003ad, 0x00000236, 0x00050088, 0x00000006, 0x000003b2,
0x000003b0, 0x000003a1, 0x00050083, 0x00000006, 0x000003b3, 0x0000004e, 0x000003b2, 0x00050050,
0x00000007, 0x000003b4, 0x000003aa, 0x000003b3, 0x000500b8, 0x000001af, 0x000001b0, 0x000003b4,
0x000001ae, 0x0004009a, 0x000000b1, 0x000001b1, 0x000001b0, 0x000400a8, 0x000000b1, 0x000001b2,
0x000001b1, 0x000300f7, 0x000001b4, 0x00000000, 0x000400fa, 0x000001b2, 0x000001b3, 0x000001b4,
0x000200f8, 0x000001b3, 0x000500ba, 0x000001af, 0x000001b7, 0x000003b4, 0x000001b6, 0x0004009a,
0x000000b1, 0x000001b8, 0x000001b7, 0x000200f9, 0x000001b4, 0x000200f8, 0x000001b4, 0x000700f5,
0x000000b1, 0x000001b9, 0x000001b1, 0x000001a7, 0x000001b8, 0x000001b3, 0x000300f7, 0x000001bb,
0x00000000, 0x000400fa, 0x000001b9, 0x000001ba, 0x000001bb, 0x000200f8, 0x000001ba, 0x000200f9,
0x00000194, 0x000200f8, 0x000001bb, 0x0004003d, 0x00000084, 0x000001be, 0x00000086, 0x00050085,
0x00000007, 0x000001c3, 0x000003b4, 0x00000250, 0x0004006e, 0x0000001a, 0x000001c4, 0x000001c3,
0x00040064, 0x00000083, 0x000001c5, 0x000001be, 0x0007005f, 0x00000032, 0x000001c6, 0x000001c5,
0x000001c4, 0x00000002, 0x00000036, 0x00050051, 0x00000006, 0x000001c7, 0x000001c6, 0x00000000,
0x00050085, 0x00000006, 0x000001ca, 0x000001c7, 0x00000149, 0x00050083, 0x00000006, 0x000001d0,
0x000003a1, 0x000001ca, 0x00050085, 0x00000006, 0x000001d5, 0x0000014a, 0x000001d4, 0x00050081,
0x00000006, 0x000001d6, 0x000001d2, 0x000001d5, 0x000500ba, 0x000000b1, 0x000001d7, 0x000001d0,
0x000001d6, 0x000500b8, 0x000000b1, 0x000001da, 0x000001d0, 0x00000182, 0x000500a7, 0x000000b1,
0x000001db, 0x000001d7, 0x000001da, 0x000300f7, 0x000001dd, 0x00000000, 0x000400fa, 0x000001db,
0x000001dc, 0x000001dd, 0x000200f8, 0x000001dc, 0x0004006f, 0x00000006, 0x000001df, 0x000003d8,
0x00050085, 0x00000006, 0x000001e0, 0x000001df, 0x000003e3, 0x00050083, 0x00000006, 0x000001e1,
0x00000029, 0x000001e0, 0x000200f9, 0x00000194, 0x000200f8, 0x000001dd, 0x000200f9, 0x00000195,
0x000200f8, 0x00000195, 0x00050080, 0x00000019, 0x000001e4, 0x000003d8, 0x00000077, 0x00050081,
0x00000006, 0x000001e7, 0x000003d9, 0x00000186, 0x000200f9, 0x00000192, 0x000200f8, 0x00000194,
0x000b00f5, 0x00000006, 0x000003dc, 0x00000110, 0x00000192, 0x00000110, 0x000001a6, 0x00000110,
0x000001ba, 0x000001e1, 0x000001dc, 0x0008000c, 0x00000006, 0x000001eb, 0x00000001, 0x00000031,
0x000001e9, 0x0000014c, 0x0000014a, 0x00050083, 0x00000006, 0x000001ec, 0x00000029, 0x000001eb,
0x00060041, 0x00000037, 0x000001ee, 0x00000035, 0x00000036, 0x00000173, 0x0004003d, 0x00000006,
0x000001ef, 0x000001ee, 0x00050085, 0x00000006, 0x000001f1, 0x000001ef, 0x000003dc, 0x00050085,
0x00000006, 0x000001f3, 0x000001f1, 0x000001ec, 0x00050085, 0x00000006, 0x000001f5, 0x000001f3,
0x0000016b, 0x00050083, 0x00000006, 0x000001f6, 0x00000029, 0x000001f5, 0x00060041, 0x00000037,
0x000001f8, 0x00000035, 0x000001f7, 0x0000002d, 0x0004003d, 0x00000006, 0x000001f9, 0x000001f8,
0x000500ba, 0x000000b1, 0x000001fa, 0x000001f9, 0x0000004e, 0x000300f7, 0x000001fc, 0x00000000,
0x000400fa, 0x000001fa, 0x000001fb, 0x000001fc, 0x000200f8, 0x000001fb, 0x0004003d, 0x000001fd,
0x00000200, 0x000001ff, 0x00070050, 0x00000032, 0x00000203, 0x000001f6, 0x000001f6, 0x000001f6,
0x00000029, 0x00040063, 0x00000200, 0x00000123, 0x00000203, 0x000200f9, 0x0000021d, 0x000200f8,
0x000001fc, 0x000500be, 0x000000b1, 0x00000206, 0x000001f6, 0x00000140, 0x000300f7, 0x00000208,
0x00000000, 0x000400fa, 0x00000206, 0x00000207, 0x00000208, 0x000200f8, 0x00000207, 0x000200f9,
0x0000021d, 0x000200f8, 0x00000208, 0x0004003d, 0x000001fd, 0x0000020c, 0x000001ff, 0x00050062,
0x00000032, 0x0000020e, 0x0000020c, 0x00000123, 0x0004003d, 0x000001fd, 0x0000020f, 0x000001ff,
0x0008004f, 0x0000000a, 0x00000212, 0x0000020e, 0x0000020e, 0x00000000, 0x00000001, 0x00000002,
0x0005008e, 0x0000000a, 0x00000214, 0x00000212, 0x000001f6, 0x00050051, 0x00000006, 0x00000216,
0x0000020e, 0x00000003, 0x00050051, 0x00000006, 0x00000217, 0x00000214, 0x00000000, 0x00050051,
0x00000006, 0x00000218, 0x00000214, 0x00000001, 0x00050051, 0x00000006, 0x00000219, 0x00000214,
0x00000002, 0x00070050, 0x00000032, 0x0000021a, 0x00000217, 0x00000218, 0x00000219, 0x00000216,
0x00040063, 0x0000020f, 0x00000123, 0x0000021a, 0x000200f9, 0x0000021d, 0x000200f8, 0x0000021d,
0x000100fd, 0x00010038,
};
+32 -4
View File
@@ -18,7 +18,11 @@ typedef struct __attribute__((aligned(16))) Cpu {
uint64_t fpcr; // 320: guest FPCR/FPSR (portable FP/SIMD lowering only; the asm path uses the host's)
uint64_t fpsr; // 328
uint64_t excl_val_hi; // 336: upper half of a 128-bit exclusive pair (LDXP/LDAXP of two X registers)
uint8_t _pad[512 - 344];
uint64_t _pad0; // 344
float fp_th_s[4]; // 352: FP result check threshold, every lane (rt_fpcr_threshold, runtime/simd_fp.h FOKT)
double fp_th_d[2]; // 368
__uint128_t fp_arg[3]; // 384: operands of a vector FP op handed to its slow path (runtime/simd_fp.h FN(vredo))
uint8_t _pad[512 - 432];
__uint128_t v[32]; // 512
} Cpu;
@@ -27,6 +31,8 @@ _Static_assert(offsetof(Cpu, pc) == 264, "pc");
_Static_assert(offsetof(Cpu, nzcv) == 272, "nzcv");
_Static_assert(offsetof(Cpu, fpcr) == 320, "fpcr");
_Static_assert(offsetof(Cpu, fpsr) == 328, "fpsr");
_Static_assert(offsetof(Cpu, fp_th_s) == 352 && offsetof(Cpu, fp_th_d) == 368, "fp_th");
_Static_assert(offsetof(Cpu, fp_arg) == 384, "fp_arg");
_Static_assert(offsetof(Cpu, v) == 512, "v");
typedef void (*GuestFn)(Cpu *);
@@ -39,12 +45,24 @@ void rt_brk(Cpu *c, uint32_t imm, uint64_t pc) __attribute__((noreturn));
void rt_udf(Cpu *c, uint64_t pc) __attribute__((noreturn));
uint64_t rt_ticks(void);
// The FP fast paths of runtime/simd_fp.h test FPCR.RMode and the result in one compare: a result is good when its
// magnitude differs from c->fp_th_*. In round-to-nearest the threshold is the smallest normal, so NaN and +-smallest
// normal fail as before; under a directed RMode it is NaN, both compares are unordered and every result goes through
// the redo path, which rounds in the FPCR mode. It must follow every change of c->fpcr: rt_fpcr_sync sets it, and so
// does code that creates a Cpu or copies fpcr into one. A zeroed threshold is wrong, not merely slow: it passes
// +-smallest normal.
static inline void rt_fpcr_threshold(Cpu *c) {
int directed = (c->fpcr & (3u << 22)) != 0;
for (int i = 0; i < 4; i++) c->fp_th_s[i] = directed ? __builtin_nanf("") : 0x1p-126f;
for (int i = 0; i < 2; i++) c->fp_th_d[i] = directed ? __builtin_nan("") : 0x1p-1022;
}
#if defined(__aarch64__)
static inline uint64_t rt_get_fpcr(void) { uint64_t v; __asm__ volatile("mrs %0, fpcr" : "=r"(v)); return v; }
static inline uint64_t rt_get_fpsr(void) { uint64_t v; __asm__ volatile("mrs %0, fpsr" : "=r"(v)); return v; }
static inline void rt_set_fpcr(uint64_t v) { __asm__ volatile("msr fpcr, %0" :: "r"(v)); }
static inline void rt_set_fpsr(uint64_t v) { __asm__ volatile("msr fpsr, %0" :: "r"(v)); }
static inline void rt_fpcr_sync(const Cpu *c) { (void)c; }
static inline void rt_fpcr_sync(Cpu *c) { rt_fpcr_threshold(c); }
#else
// Host FP state is not the guest's here: code lifted with --simd=c keeps FPCR/FPSR in Cpu (c->fpcr, zeroed
// with the Cpu), so the host-side reset in thread start-up has nothing to do.
@@ -56,12 +74,20 @@ static inline void rt_set_fpsr(uint64_t v) { (void)v; }
// the slow paths of runtime/simd.h, which switch RC around one operation). The FP helpers rely on it without checking,
// so it is set wherever FPCR or the host FP context changes: MSR FPCR (recomp/lifter.py), rt_call (thread and fiber
// entry, host code calling guest code), fiber resume (runtime/kernel.c fiber_resumed; fibers keep their own MXCSR).
static inline void rt_fpcr_sync(const Cpu *c) {
// It also sets the FP result check threshold (rt_fpcr_threshold).
static inline void rt_fpcr_sync(Cpu *c) {
rt_fpcr_threshold(c);
unsigned cur = __builtin_ia32_stmxcsr();
unsigned want = (cur & ~0xE040u) | (c->fpcr & (1u << 24) ? 0x8040u : 0); // FTZ 0x8000, DAZ 0x40, RC 0x6000
if (cur != want) __builtin_ia32_ldmxcsr(want);
}
#endif
// MSR FPCR (recomp/lifter.py): the first guest write of a directed rounding mode (FPCR.RMode, bits 22-23) is logged
// once with the caller (main.c rt_fpcr_rmode), to learn whether the game ever leaves round-to-nearest. A test of the
// value just stored, at the MSR sites only.
void rt_fpcr_rmode(uint64_t fpcr, uint64_t lr);
#define RT_FPCR_NOTE(c, lr) \
do { if (__builtin_expect((c)->fpcr & (3ull << 22), 0)) rt_fpcr_rmode((c)->fpcr, lr); } while (0)
// ---- memory: guest addresses are host addresses
typedef uint16_t __attribute__((aligned(1), may_alias)) u16u;
@@ -69,7 +95,9 @@ typedef uint32_t __attribute__((aligned(1), may_alias)) u32u;
typedef uint64_t __attribute__((aligned(1), may_alias)) u64u;
typedef uint8_t __attribute__((may_alias)) u8a;
// Function-entry profiler (runtime/fnprof.c, LARECOMP_FNPROF): one predictable branch per call when off.
// Function-entry hook: one predictable branch per call while g_fnprof is 0. The generator emits it only in the
// functions of the patch table's `hook` lines (recomp/patches.py), or in every function for a table without any and
// with gen.py --fn-enter-all.
extern volatile int g_fnprof;
void fnprof_hit(uint64_t addr, uint64_t lr, uint64_t x0);
#define FN_ENTER(a) do { if (__builtin_expect(g_fnprof, 0)) fnprof_hit(a, c->x[30], c->x[0]); } while (0)
+89
View File
@@ -0,0 +1,89 @@
// The mouse pointer's image in the game window while the cursor is not captured (the game's menus, the settings menu):
// a gold arrowhead with a notched back, a dark outline, a turquoise gem and a soft shadow, drawn here at the size the
// window's DPI asks for instead of the Windows arrow. input_win.c and input_sdl.c make the system cursor from it.
// Nothing comes from the game's files.
#include <math.h>
#include <stdint.h>
#include "input_wsys.h"
typedef struct { float x, y; } V;
// In units of a 32-pixel cursor: the tip at the top left (the hotspot), the wings to the right and below, the notch
// between them
static const V SHAPE[] = {{1.5f, 1.5f}, {25.0f, 10.5f}, {14.5f, 14.5f}, {10.5f, 25.0f}};
static const V GEM[] = {{10.4f, 7.6f}, {13.0f, 10.2f}, {10.4f, 12.8f}, {7.8f, 10.2f}};
#define OUTLINE 1.7f // the dark edge's width
#define SHADOW 1.6f // the shadow's offset to the bottom right
static float seg_dist(V p, V a, V b) {
float dx = b.x - a.x, dy = b.y - a.y, t = ((p.x - a.x) * dx + (p.y - a.y) * dy) / (dx * dx + dy * dy);
t = t < 0 ? 0 : t > 1 ? 1 : t;
float ex = a.x + t * dx - p.x, ey = a.y + t * dy - p.y;
return sqrtf(ex * ex + ey * ey);
}
// Distance to the polygon's edge, negative inside (even-odd: the shapes are simple)
static float poly_sdf(V p, const V *v, int n) {
float d = 1e9f;
int in = 0;
for (int i = 0, j = n - 1; i < n; j = i++) {
float e = seg_dist(p, v[j], v[i]);
if (e < d) d = e;
if ((v[i].y > p.y) != (v[j].y > p.y) && p.x < (v[j].x - v[i].x) * (p.y - v[i].y) / (v[j].y - v[i].y) + v[i].x)
in = !in;
}
return in ? -d : d;
}
// One sample's colour (straight r, g, b in 0..1) and coverage
static float sample(V p, float *r, float *g, float *b) {
float d = poly_sdf(p, SHAPE, 4);
if (d < 0) {
if (-d < OUTLINE) { *r = 0.17f, *g = 0.11f, *b = 0.04f; return 1; }
if (poly_sdf(p, GEM, 4) < 0) { // the gem, lighter towards its top left
float t = (p.x + p.y - 20.4f) / 5.2f + 0.5f;
t = t < 0 ? 0 : t > 1 ? 1 : t;
*r = 0.62f - 0.42f * t, *g = 0.98f - 0.22f * t, *b = 0.92f - 0.12f * t;
return 1;
}
// gold, pale at the tip and deeper at the wings; a thin light line inside the outline
float t = (p.x + p.y) / 34.0f;
t = t < 0 ? 0 : t > 1 ? 1 : t;
float rim = -d < OUTLINE + 1.0f ? 0.12f : 0;
*r = fminf(1, 1.00f - 0.12f * t + rim), *g = fminf(1, 0.93f - 0.32f * t + rim),
*b = fminf(1, 0.70f - 0.52f * t + rim);
return 1;
}
V s = {p.x - SHADOW, p.y - SHADOW};
float ds = poly_sdf(s, SHAPE, 4);
if (ds < 0.8f) { // the shadow, soft at its edge
*r = *g = *b = 0;
float a = ds < -0.8f ? 1 : (0.8f - ds) / 1.6f;
return 0.38f * a;
}
return 0;
}
void cursor_art(uint32_t *argb, int size, int *hot) {
float k = 32.0f / (float)size;
enum { SS = 4 }; // 4 x 4 samples a pixel
for (int y = 0; y < size; y++)
for (int x = 0; x < size; x++) {
float r = 0, g = 0, b = 0, a = 0;
for (int sy = 0; sy < SS; sy++)
for (int sx = 0; sx < SS; sx++) {
V p = {(x + (sx + 0.5f) / SS) * k, (y + (sy + 0.5f) / SS) * k};
float sr, sg, sb, c = sample(p, &sr, &sg, &sb);
r += sr * c, g += sg * c, b += sb * c, a += c;
}
uint32_t px = 0;
if (a > 0) { // straight alpha, as Windows and SDL take it
r /= a, g /= a, b /= a, a /= SS * SS;
px = (uint32_t)lrintf(a * 255) << 24 | (uint32_t)lrintf(r * 255) << 16 |
(uint32_t)lrintf(g * 255) << 8 | (uint32_t)lrintf(b * 255);
}
argb[y * size + x] = px;
}
*hot = (int)(1.5f / k);
}
+5
View File
@@ -91,6 +91,11 @@ void frame_step_init(void) {
rt_patch_disable(cloth);
rt_log("frame rate: cloth clock off, it needs the real-time step (TOTK_REALTIME=0)");
}
int climb = rt_patch_set("climb"); // reads the page's frame ratio, written only with the real-time step
if (climb >= 0 && !rt_patch_enabled(realtime) && rt_patch_enabled(climb)) {
rt_patch_disable(climb);
rt_log("frame rate: climb speed fix off, it needs the real-time step (TOTK_REALTIME=0)");
}
if (rt_patch_enabled(realtime)) {
step_word = (_Atomic uint64_t *)rt_patch_page();
step_interval = (_Atomic uint32_t *)((uint8_t *)rt_patch_page() + 8);
+8
View File
@@ -47,6 +47,14 @@ enum { ROMFS_PROMPTS_OK, ROMFS_PROMPTS_MISSING, ROMFS_PROMPTS_FAILED, ROMFS_PROM
ROMFS_PROMPTS_PATCHES };
const char *romfs_prompts(void);
int romfs_prompts_ready(const char *folder);
// 1 when the game resolution w x h is of another shape whose folders are missing: the UI layout, or that of
// prompts_folder (a prompts' folder that is there at 16:9; NULL for the game's own prompts). romfs.c shaped_name.
int romfs_shape_missing(unsigned w, unsigned h, const char *prompts_folder);
// 1 when build-cache/rebuild.txt names the setup's Python and tools/make_romfs_patches.py and both exist, with romfs/.
int romfs_shape_can_prepare(void);
// Starts the generator for w x h's shape (its layout, and prompts_folder's when that is there at 16:9 and lacks it) as
// child processes the game does not wait for; the next start loads the result. 1 when every one started.
int romfs_shape_prepare(unsigned w, unsigned h, const char *prompts_folder);
int romfs_prompts_off(const char *folder);
const char *romfs_overlay(size_t k, const char **file, uint64_t *size, int *is_added); // RomFS path of overlay k
void fs_trace(const char *what, const char *path); // TOTK_TRACE=fs: one line per open
+1
View File
@@ -1770,6 +1770,7 @@ int probe_fs(void) {
pcpu = plat_aligned_alloc(sizeof(Cpu), 64);
need(pcpu != NULL, "probe Cpu");
memset(pcpu, 0, sizeof *pcpu);
rt_fpcr_threshold(pcpu);
uint8_t *stack = mem_kernel_alloc(0x10000, 16);
pcpu->sp = (uint64_t)(uintptr_t)stack + 0x10000;
pcpu->excl_addr = ~0ull;
+474
View File
@@ -0,0 +1,474 @@
// Mouse wheel and keyboard gameplay tweaks with independent live settings.
// The game's native R + D-pad object rotation and D-pad distance controls remain authoritative.
#include "gameplay_tweaks.h"
#include "cfg.h"
#include "host.h"
#include "input.h"
#include "input_map.h"
#include "input_wsys.h"
#include "platform/platform.h"
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define BIT(x) (1ull << (x))
#define HOLD_FRAMES 3
#define GAP_FRAMES 1
#define MAX_QUEUED 16
enum { NONE, ROTATE, DISTANCE, ITEMS, MAP_ZOOM, WEAPONS };
enum { W_IDLE = 0, W_PRIME, W_HOLD, W_GAP, W_WAIT };
#define WEAPON_PRIME_MIN_NS 75000000ull
#define WEAPON_PRIME_MAX_NS 135000000ull
#define WEAPON_HOLD_NS 42000000ull
#define WEAPON_GAP_NS 36000000ull
static PlatMutex lock = PLAT_MUTEX_INIT;
static unsigned seen = UINT_MAX;
static struct {
int available, rotate, c_axis, distance, wheel, keys, map_zoom, weapon_wheel;
double rotation_speed, distance_speed, item_speed, map_zoom_speed, weapon_speed;
} options;
static struct {
int kind, axis, direction, queued, hold, gap, primed, valid;
double fraction;
uint64_t frame, buttons;
int32_t rx;
} pulse;
static struct {
int active, phase, direction, scroll_dir, queued, polls_in_phase;
int confirm_pending;
double fraction;
uint64_t phase_until_ns, prime_start_ns, last_frame;
int32_t rx;
} weapon;
static double map_zoom_velocity;
static int held_a, held_d;
static int consumed_c, consumed_right;
static int was_e_down, consumed_e;
static uint64_t physical_buttons;
static void clear_weapon_locked(void) { memset(&weapon, 0, sizeof weapon); }
static void clear_locked(void) { memset(&pulse, 0, sizeof pulse); clear_weapon_locked(); map_zoom_velocity = 0; }
static void refresh_locked(void) {
unsigned version = cfg_version();
if (seen == version) return;
options.available = cfg_bool("TOTK_GAMEPLAY_TWEAKS_ENABLED");
options.rotate = cfg_bool("TOTK_GAMEPLAY_ULTRAHAND_ROTATE");
options.c_axis = cfg_bool("TOTK_GAMEPLAY_ULTRAHAND_C_AXIS");
options.distance = cfg_bool("TOTK_GAMEPLAY_ULTRAHAND_DISTANCE");
options.wheel = cfg_bool("TOTK_GAMEPLAY_ITEM_WHEEL");
options.keys = cfg_bool("TOTK_GAMEPLAY_ITEM_AD");
options.map_zoom = cfg_bool("TOTK_GAMEPLAY_MAP_ZOOM");
options.weapon_wheel = cfg_bool("TOTK_GAMEPLAY_WEAPON_WHEEL");
options.rotation_speed = cfg_double("TOTK_GAMEPLAY_ROTATION_SENSITIVITY");
options.distance_speed = cfg_double("TOTK_GAMEPLAY_DISTANCE_SENSITIVITY");
options.item_speed = cfg_double("TOTK_GAMEPLAY_ITEM_SENSITIVITY");
options.map_zoom_speed = cfg_double("TOTK_GAMEPLAY_MAP_ZOOM_SENSITIVITY");
options.weapon_speed = cfg_double("TOTK_GAMEPLAY_WEAPON_SENSITIVITY");
if (!isfinite(options.rotation_speed) || options.rotation_speed < .25 || options.rotation_speed > 4) options.rotation_speed = 1;
if (!isfinite(options.distance_speed) || options.distance_speed < .25 || options.distance_speed > 4) options.distance_speed = 1;
if (!isfinite(options.item_speed) || options.item_speed < .25 || options.item_speed > 4) options.item_speed = 1;
if (!isfinite(options.map_zoom_speed) || options.map_zoom_speed < .25 || options.map_zoom_speed > 4) options.map_zoom_speed = 1;
if (!isfinite(options.weapon_speed) || options.weapon_speed < .25 || options.weapon_speed > 4) options.weapon_speed = 1;
seen = version;
clear_locked();
}
static int item_menu(uint64_t buttons) {
if (buttons & BIT(B_L)) return 0;
const uint64_t held = BIT(B_LEFT) | BIT(B_UP) | BIT(B_RIGHT) | BIT(B_DOWN);
return input_camera_context() == INPUT_CAM_MENU_WHEEL || (buttons & held) != 0;
}
static int object_context(uint64_t buttons) {
const uint64_t menus = BIT(B_L) | BIT(B_LEFT) | BIT(B_UP) | BIT(B_RIGHT) | BIT(B_DOWN) |
BIT(B_B) | BIT(B_PLUS) | BIT(B_MINUS);
return input_camera_context() == INPUT_CAM_ULTRAHAND_OBJECT && !(buttons & menus);
}
static int map_zoom_allowed(uint64_t buttons) {
if (!options.map_zoom) return 0;
if (object_context(buttons) || item_menu(buttons)) return 0;
if (buttons & (BIT(B_L) | BIT(B_ZL) | BIT(B_ZR) | BIT(B_R))) return 0;
int ctx = input_camera_context();
if (ctx == INPUT_CAM_AIM || ctx == INPUT_CAM_SCOPE ||
ctx == INPUT_CAM_ULTRAHAND_OBJECT || ctx == INPUT_CAM_LOCK_ON) {
return 0;
}
// Only the map menu_cursor.c finds: with menu_cursor = 0 (or on 1.2.1) the map is not read, and a fallback to
// "allowed" would push the right stick's Y in free roam, where it tilts the camera.
return menu_cursor_map();
}
static int weapon_wheel_allowed(uint64_t buttons, int right_down, int already_active) {
if (!options.available || !options.weapon_wheel) return 0;
if (right_down) return 0;
if (!ikb_captured() || menu_cursor_map() || menu_cursor_pointer()) return 0;
const uint64_t forbidden = BIT(B_L) | BIT(B_R) | BIT(B_ZL) | BIT(B_ZR) |
BIT(B_LEFT) | BIT(B_UP) | BIT(B_RIGHT) | BIT(B_DOWN) |
BIT(B_PLUS) | BIT(B_MINUS);
if (buttons & forbidden) return 0;
if (!already_active) {
if (menu_cursor_shortcut() == 1 || menu_cursor_strip() || menu_cursor_camera() || menu_cursor_menu())
return 0;
}
int ctx = input_camera_context();
if (!already_active) {
// Opening needs a camera the mouse_direct hooks saw this moment: an unclassified one is also a paused menu,
// 1.2.1's menus or any screen menu_cursor.c does not read, where D-pad right and the right stick would move
// the menu instead.
if (ctx != INPUT_CAM_FREE_ROAM && ctx != INPUT_CAM_LOCK_ON && ctx != INPUT_CAM_VEHICLE)
return 0;
} else {
if (ctx != INPUT_CAM_UNCLASSIFIED && ctx != INPUT_CAM_MENU_WHEEL &&
ctx != INPUT_CAM_FREE_ROAM && ctx != INPUT_CAM_LOCK_ON && ctx != INPUT_CAM_VEHICLE)
return 0;
}
return 1;
}
int gameplay_tweaks_available(void) {
plat_mutex_lock(&lock);
refresh_locked();
int available = options.available;
plat_mutex_unlock(&lock);
return available;
}
int gameplay_tweaks_capture_needed(void) {
plat_mutex_lock(&lock);
refresh_locked();
int active = options.available && (options.rotate || options.distance || options.wheel ||
options.keys || options.map_zoom || options.weapon_wheel);
plat_mutex_unlock(&lock);
return active;
}
int gameplay_tweaks_map_zoom(void) {
plat_mutex_lock(&lock);
refresh_locked();
int active = options.available && options.map_zoom;
plat_mutex_unlock(&lock);
return active;
}
int gameplay_tweaks_strip_wheel(uint64_t buttons) {
plat_mutex_lock(&lock);
refresh_locked();
int active = options.available && (weapon.active || (options.wheel && item_menu(buttons)));
plat_mutex_unlock(&lock);
return active;
}
void gameplay_tweaks_init(void) {
gameplay_tweaks_available();
}
void gameplay_tweaks_reset(void) {
plat_mutex_lock(&lock);
clear_locked();
held_a = held_d = 0;
physical_buttons = 0;
was_e_down = consumed_e = 0;
plat_mutex_unlock(&lock);
}
void gameplay_tweaks_wheel(double delta, int c_down, int right_down) {
if (!isfinite(delta) || delta == 0) return;
if (delta > MAX_QUEUED) delta = MAX_QUEUED;
if (delta < -MAX_QUEUED) delta = -MAX_QUEUED;
plat_mutex_lock(&lock);
refresh_locked();
if (!options.available) {
clear_locked();
plat_mutex_unlock(&lock);
return;
}
int kind = NONE, axis = 0;
double sensitivity = 1;
if (object_context(physical_buttons)) {
if (right_down && options.distance) {
kind = DISTANCE;
sensitivity = options.distance_speed;
} else if (options.rotate) {
kind = ROTATE;
axis = options.c_axis && c_down;
sensitivity = options.rotation_speed;
}
} else if ((physical_buttons & (BIT(B_LEFT) | BIT(B_UP) | BIT(B_RIGHT) | BIT(B_DOWN))) &&
options.wheel && item_menu(physical_buttons)) {
kind = ITEMS;
sensitivity = options.item_speed;
} else if (map_zoom_allowed(physical_buttons)) {
kind = MAP_ZOOM;
sensitivity = options.map_zoom_speed;
} else if (weapon_wheel_allowed(physical_buttons, right_down, weapon.active)) {
kind = WEAPONS;
}
if (!kind) {
clear_locked();
plat_mutex_unlock(&lock);
return;
}
if (kind == MAP_ZOOM) {
if (weapon.active) clear_weapon_locked();
map_zoom_velocity -= delta * sensitivity * 16000.0;
if (map_zoom_velocity > 65536.0) map_zoom_velocity = 65536.0;
if (map_zoom_velocity < -65536.0) map_zoom_velocity = -65536.0;
plat_mutex_unlock(&lock);
return;
}
if (kind == WEAPONS) {
uint64_t now = plat_now_ns();
int new_dir = delta > 0 ? 1 : -1;
if (weapon.scroll_dir != 0 && new_dir != weapon.scroll_dir) {
weapon.fraction = 0;
weapon.queued = 0;
if (weapon.active && weapon.phase == W_HOLD) {
weapon.phase = W_GAP;
weapon.rx = 0;
weapon.polls_in_phase = 0;
weapon.phase_until_ns = now + WEAPON_GAP_NS;
}
}
weapon.scroll_dir = new_dir;
weapon.fraction += delta * options.weapon_speed;
int steps = (int)trunc(weapon.fraction);
if (steps != 0) {
weapon.confirm_pending = 0;
weapon.fraction -= steps;
weapon.direction = steps > 0 ? 1 : -1;
weapon.queued += (steps > 0 ? steps : -steps);
if (weapon.queued > MAX_QUEUED) weapon.queued = MAX_QUEUED;
if (!weapon.active) {
weapon.active = 1;
weapon.phase = W_PRIME;
weapon.polls_in_phase = 0;
weapon.prime_start_ns = now;
weapon.phase_until_ns = now + WEAPON_PRIME_MAX_NS;
weapon.rx = 0;
} else if (weapon.phase == W_WAIT) {
weapon.phase = W_HOLD;
weapon.polls_in_phase = 0;
weapon.phase_until_ns = now + WEAPON_HOLD_NS;
weapon.rx = 32767 * weapon.direction;
if (weapon.queued > 0) weapon.queued--;
}
}
plat_mutex_unlock(&lock);
return;
}
if (weapon.active) clear_weapon_locked();
if (kind != pulse.kind || axis != pulse.axis ||
(delta > 0 && pulse.direction < 0) || (delta < 0 && pulse.direction > 0)) {
clear_locked();
}
pulse.kind = kind;
pulse.axis = axis;
pulse.fraction += delta * sensitivity;
int steps = (int)trunc(pulse.fraction);
if (steps) {
pulse.fraction -= steps;
pulse.direction = steps > 0 ? 1 : -1;
if (!pulse.queued && !pulse.hold && !pulse.gap) pulse.valid = 0;
pulse.queued += steps > 0 ? steps : -steps;
if (pulse.queued > MAX_QUEUED) pulse.queued = MAX_QUEUED;
}
plat_mutex_unlock(&lock);
}
void gameplay_tweaks_prepare_keys(uint8_t *down, uint64_t pad_buttons) {
plat_mutex_lock(&lock);
refresh_locked();
held_a = held_d = 0;
int c = down[KEY_C], right = down[KEY_MOUSE_R];
int e = down[KEY_E];
int e_pressed = e && !was_e_down;
was_e_down = e;
if (!e) consumed_e = 0;
if (!c) consumed_c = 0;
if (!right) consumed_right = 0;
if (options.available) {
if (input_camera_context() == INPUT_CAM_ULTRAHAND_OBJECT) {
if (options.rotate && options.c_axis && !(options.distance && right) && c) consumed_c = 1;
if (options.distance && right) consumed_right = 1;
if (pulse.kind == ROTATE && pulse.axis != (options.c_axis && c)) clear_locked();
}
if (weapon.active && e_pressed) {
consumed_e = 1;
weapon.confirm_pending = 1;
}
if (consumed_e) down[KEY_E] = 0;
if (consumed_c) down[KEY_C] = 0;
if (consumed_right) down[KEY_MOUSE_R] = 0;
uint64_t buttons = pad_buttons | input_map_buttons(down);
if (options.keys && item_menu(buttons) && !weapon.active) {
held_a = down[KEY_A];
held_d = down[KEY_D];
down[KEY_A] = down[KEY_D] = 0;
}
} else {
consumed_c = consumed_right = consumed_e = 0;
}
plat_mutex_unlock(&lock);
}
static void sample_locked(uint64_t frame) {
if (pulse.valid && pulse.frame == frame) return;
pulse.buttons = 0;
pulse.rx = 0;
if (pulse.queued || pulse.hold || pulse.gap) {
if (pulse.kind == ROTATE) pulse.buttons = BIT(B_R);
if (pulse.kind == ROTATE && !pulse.primed) pulse.primed = 1;
else if (pulse.gap) pulse.gap--;
else {
if (!pulse.hold && pulse.queued) {
pulse.queued--;
pulse.hold = HOLD_FRAMES;
}
if (pulse.hold) {
if (pulse.kind == ROTATE)
pulse.buttons |= pulse.axis ? (pulse.direction > 0 ? BIT(B_DOWN) : BIT(B_UP)) :
(pulse.direction > 0 ? BIT(B_RIGHT) : BIT(B_LEFT));
else if (pulse.kind == DISTANCE)
pulse.buttons = pulse.direction < 0 ? BIT(B_UP) : BIT(B_DOWN);
else pulse.rx = 32767 * pulse.direction;
if (!--pulse.hold) pulse.gap = GAP_FRAMES;
}
}
} else pulse.primed = 0;
pulse.valid = 1;
pulse.frame = frame;
}
void gameplay_tweaks_apply(uint64_t frame, int active, uint64_t *buttons,
int32_t *lx, int32_t *ly, int32_t *rx, int32_t *ry) {
(void)lx;
(void)ly;
plat_mutex_lock(&lock);
refresh_locked();
physical_buttons = *buttons;
if (!active || !options.available) {
clear_locked();
held_a = held_d = 0;
plat_mutex_unlock(&lock);
return;
}
int right = ikb_down(KEY_MOUSE_R);
if (weapon.active) {
if (!weapon_wheel_allowed(physical_buttons, right, 1) || (physical_buttons & BIT(B_B))) {
clear_weapon_locked();
}
}
if (weapon.active) {
uint64_t now = plat_now_ns();
if (weapon.last_frame != frame) {
weapon.last_frame = frame;
weapon.polls_in_phase++;
}
switch (weapon.phase) {
case W_PRIME: {
uint64_t elapsed = now - weapon.prime_start_ns;
int shortcut = (menu_cursor_shortcut() == 1);
int ready = (elapsed >= WEAPON_PRIME_MIN_NS && weapon.polls_in_phase >= 3 && shortcut) ||
(elapsed >= WEAPON_PRIME_MAX_NS && weapon.polls_in_phase >= 4);
if (ready) {
if (weapon.queued > 0) {
weapon.phase = W_HOLD;
weapon.polls_in_phase = 0;
weapon.phase_until_ns = now + WEAPON_HOLD_NS;
weapon.rx = 32767 * weapon.direction;
weapon.queued--;
} else if (weapon.confirm_pending) {
clear_weapon_locked();
} else {
weapon.phase = W_WAIT;
weapon.polls_in_phase = 0;
weapon.rx = 0;
}
}
break;
}
case W_HOLD: {
if (now >= weapon.phase_until_ns && weapon.polls_in_phase >= 2) {
weapon.phase = W_GAP;
weapon.polls_in_phase = 0;
weapon.phase_until_ns = now + WEAPON_GAP_NS;
weapon.rx = 0;
}
break;
}
case W_GAP: {
if (now >= weapon.phase_until_ns && weapon.polls_in_phase >= 2) {
if (weapon.queued > 0) {
weapon.phase = W_HOLD;
weapon.polls_in_phase = 0;
weapon.phase_until_ns = now + WEAPON_HOLD_NS;
weapon.rx = 32767 * weapon.direction;
weapon.queued--;
} else if (weapon.confirm_pending) {
clear_weapon_locked();
} else {
weapon.phase = W_WAIT;
weapon.polls_in_phase = 0;
weapon.rx = 0;
}
}
break;
}
case W_WAIT: {
if (weapon.confirm_pending) {
clear_weapon_locked();
}
break;
}
default:
clear_weapon_locked();
break;
}
}
if (weapon.active) {
*buttons |= BIT(B_RIGHT);
*rx = weapon.rx;
*ry = 0;
} else {
int object = object_context(*buttons), items = item_menu(*buttons);
if ((pulse.kind == ROTATE && (!object || !options.rotate || (right && options.distance))) ||
(pulse.kind == DISTANCE && (!object || !right || !options.distance)) ||
(pulse.kind == ITEMS && (!items || !options.wheel))) {
clear_locked();
}
sample_locked(frame);
if (!map_zoom_allowed(*buttons)) {
map_zoom_velocity = 0;
} else if (fabs(map_zoom_velocity) > 50.0) {
int32_t stick_y = (int32_t)map_zoom_velocity;
if (stick_y > 32767) stick_y = 32767;
if (stick_y < -32767) stick_y = -32767;
if (abs(stick_y) > abs(*ry)) *ry = stick_y;
map_zoom_velocity *= 0.78;
if (fabs(map_zoom_velocity) < 100.0) map_zoom_velocity = 0;
}
if (pulse.kind == ROTATE || pulse.kind == DISTANCE) {
*buttons |= pulse.buttons;
// Distance D-pad pulses do not own the look stick. R rotation does,
// because the game's rotation mode would otherwise rotate both axes.
if (pulse.kind == ROTATE && pulse.buttons) *rx = *ry = 0;
} else if (pulse.kind == ITEMS && (pulse.queued || pulse.hold || pulse.gap || pulse.rx)) {
*rx = pulse.rx;
*ry = 0;
} else if (items && options.keys && (held_a || held_d)) {
*rx = ((held_d != 0) - (held_a != 0)) * 32767;
*ry = 0;
}
}
plat_mutex_unlock(&lock);
}
+14
View File
@@ -0,0 +1,14 @@
// Mouse wheel and keyboard gameplay tweaks: Ultrahand rotation and distance, item and weapon wheel, map zoom.
#pragma once
#include <stdint.h>
int gameplay_tweaks_available(void);
int gameplay_tweaks_capture_needed(void);
int gameplay_tweaks_map_zoom(void);
int gameplay_tweaks_strip_wheel(uint64_t buttons);
void gameplay_tweaks_init(void);
void gameplay_tweaks_reset(void);
void gameplay_tweaks_wheel(double delta, int c_down, int right_down);
void gameplay_tweaks_prepare_keys(uint8_t *down, uint64_t pad_buttons);
void gameplay_tweaks_apply(uint64_t frame, int active, uint64_t *buttons,
int32_t *lx, int32_t *ly, int32_t *rx, int32_t *ry);
+6
View File
@@ -24,6 +24,12 @@ uint64_t gfx_vsync_time_ns(uint64_t tick); // kern_now_ns() time
double gfx_window_refresh_hz(void); // refresh rate of the window's monitor; 0 unknown
int gfx_window_focused(void);
int gfx_picture_rect(float out[4]); // where the last present put the picture, fractions of the swapchain (x, y, w, h); 0 before one
// NVIDIA Reflex (gfx_present.c, VK_NV_low_latency2): the NVN front end and the input service mark a frame's stages.
// The markers and the sleep return at once with Reflex off or unsupported.
void vk_reflex_marker(int marker, uint64_t present_id); // present_id: the frame's (vk_reflex_frame_begin)
uint64_t vk_reflex_frame_begin(void); // SIMULATION_START: the new frame's present ID
uint64_t vk_reflex_frame(void); // the ID of the frame the game simulates now
void vk_reflex_sleep(void); // before SIMULATION_START: the driver's latency sleep
// ---- shader service client (gfx_shader.c)
enum { GFX_STAGE_VS, GFX_STAGE_TCS, GFX_STAGE_TES, GFX_STAGE_GS, GFX_STAGE_FS, GFX_STAGE_CS };
+228 -5
View File
@@ -33,9 +33,20 @@
#define VSYNC_HZ 60
// ---- queue lock
// The recorder queues the parts it records and submits them later (vk_rec.c rec_submit): the first lock of a thread
// waits until the parts queued before it are submitted, so whatever it puts on the queue comes after them, as when
// the submitting thread submitted them itself. Not inside a held lock: the recorder takes this lock to submit.
static PlatRMutex g_ql;
void vk_queue_lock(void) { plat_rmutex_lock(&g_ql); }
void vk_queue_unlock(void) { plat_rmutex_unlock(&g_ql); }
static _Thread_local int ql_depth;
void vk_queue_lock(void) {
if (!ql_depth) rec_queue_wait();
plat_rmutex_lock(&g_ql);
ql_depth++;
}
void vk_queue_unlock(void) {
ql_depth--;
plat_rmutex_unlock(&g_ql);
}
// ---- window state shared with the back end
static VkSurfaceKHR g_surface;
@@ -228,6 +239,8 @@ unsigned gfx_window_focus_messages(void) { return (unsigned)g_focus_msgs; }
// Present path (g_pl held): a settings change (cfg_version) reaches the window thread, which applies a changed size or
// fullscreen; a changed TOTK_VK_PRESENT rebuilds the swapchain at this present.
static void reflex_apply_locked(void);
static void window_settings_live(void) {
static unsigned seen;
static int started;
@@ -236,15 +249,192 @@ static void window_settings_live(void) {
if (started && v == seen) return;
seen = v;
res_fov_live();
res_dynres_live();
const char *e = cfg_value("TOTK_VK_PRESENT");
if (!started || strncmp(e, present, sizeof present - 1)) {
snprintf(present, sizeof present, "%s", e);
if (started) plat_xchg32(&g_dirty, 1);
}
if (started) gfx_window_apply_settings();
reflex_apply_locked();
started = 1;
}
// ---- NVIDIA Reflex Low Latency (VK_NV_low_latency2)
static PFN_vkSetLatencySleepModeNV g_reflex_set_mode;
static PFN_vkLatencySleepNV g_reflex_sleep;
static PFN_vkSetLatencyMarkerNV g_reflex_marker;
static VkSemaphore g_reflex_sem;
static uint64_t g_reflex_val;
// Present IDs (VK_KHR_present_id, VK_NV_low_latency2): each frame gets its ID once, when the game acquires its window
// texture (SIMULATION_START, vk_reflex_frame_begin), and the frame carries it to its input sample, its submissions
// (vk_reflex_submit_id: the game thread's current frame, or the frame a replay or present thread set for the work it
// does) and its present (PresentJob.pid, vk_reflex_present_frame, flip_locked), so the driver can match every marker
// of a frame to the present of that frame.
static uint64_t g_reflex_frame_next; // the last ID handed out
static uint64_t g_reflex_frame; // the frame the game simulates now
static _Thread_local uint64_t t_reflex_submit; // the frame whose work this thread submits; 0: g_reflex_frame
static uint64_t g_blit_pid, g_flip_pid; // the frame of the blit being made and of the present waiting (g_pl)
static uint64_t g_last_pid; // the last present ID presented (g_pl): present IDs only go up
static int g_reflex_mode = -1; // 0=off, 1=on, 2=boost
static uint32_t g_reflex_min_us = 1;
static int reflex_mode_from_cfg(void) {
const char *v = cfg_value("TOTK_REFLEX");
if (!v || !strcmp(v, "off") || !strcmp(v, "0")) return 0;
if (!strcmp(v, "boost") || !strcmp(v, "2")) return 2;
return 1;
}
static void reflex_apply_locked(void) {
VkCtx *c = vk_ctx();
if (!c || !c->has_nv_reflex || !g_sc) return;
if (!g_reflex_set_mode) {
g_reflex_set_mode = (PFN_vkSetLatencySleepModeNV)vkGetDeviceProcAddr(c->device, "vkSetLatencySleepModeNV");
g_reflex_sleep = (PFN_vkLatencySleepNV)vkGetDeviceProcAddr(c->device, "vkLatencySleepNV");
g_reflex_marker = (PFN_vkSetLatencyMarkerNV)vkGetDeviceProcAddr(c->device, "vkSetLatencyMarkerNV");
}
if (!g_reflex_set_mode) return;
if (!g_reflex_sem) {
VkSemaphoreTypeCreateInfo ti = {
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO,
.semaphoreType = VK_SEMAPHORE_TYPE_TIMELINE,
.initialValue = 0
};
VkSemaphoreCreateInfo si = {.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, .pNext = &ti};
if (vkCreateSemaphore(c->device, &si, NULL, &g_reflex_sem) != VK_SUCCESS) return;
}
int mode = reflex_mode_from_cfg();
uint32_t min_us = 0;
int limit = cfg_int("TOTK_FPS_LIMIT");
if (limit > 0) min_us = (uint32_t)(1000000 / limit);
if (mode == g_reflex_mode && min_us == g_reflex_min_us) return;
g_reflex_mode = mode;
g_reflex_min_us = min_us;
VkLatencySleepModeInfoNV mi = {
.sType = VK_STRUCTURE_TYPE_LATENCY_SLEEP_MODE_INFO_NV,
.lowLatencyMode = mode > 0 ? VK_TRUE : VK_FALSE,
.lowLatencyBoost = mode == 2 ? VK_TRUE : VK_FALSE,
.minimumIntervalUs = min_us
};
VkResult r = g_reflex_set_mode(c->device, g_sc, &mi);
rt_log("REFLEX: NVIDIA Reflex Low Latency -> %s (minInterval=%u us, VK_NV_low_latency2=%s)",
mode == 2 ? "ON + BOOST" : mode == 1 ? "ON" : "OFF", min_us, r == VK_SUCCESS ? "OK" : "ERR");
}
int vk_has_reflex(void) {
VkCtx *c = vk_ctx();
return c && c->has_nv_reflex;
}
// TOTK_TRACE=perf, every LAT_EVERY presents: the time from a frame's start (its window texture acquired) to its
// present call on the host, with Reflex off or on, and with Reflex on the driver's own reports of the same frames
// (vkGetLatencyTimingsNV): how many came back with every marker matched to their present, and their simulation start
// to GPU render end. The frame's start is kept by its ID, so it also shows that a present carries its frame's ID.
#define LAT_RING 64
#define LAT_EVERY 600
static struct { uint64_t id, start_ns; } g_lat_start[LAT_RING];
static struct { uint64_t n, ns, presents; } g_lat;
static PFN_vkGetLatencyTimingsNV g_reflex_timings;
uint64_t vk_reflex_frame_begin(void) {
uint64_t id = __atomic_add_fetch(&g_reflex_frame_next, 1, __ATOMIC_RELAXED);
__atomic_store_n(&g_reflex_frame, id, __ATOMIC_RELAXED);
if (rt_trace & RT_TRACE_PERF) g_lat_start[id % LAT_RING].start_ns = kern_now_ns(), g_lat_start[id % LAT_RING].id = id;
return id;
}
// g_pl held, after the present of frame pid
static void latency_note(VkCtx *c, uint64_t pid) {
if (!(rt_trace & RT_TRACE_PERF) || !pid) return;
if (g_lat_start[pid % LAT_RING].id == pid) g_lat.n++, g_lat.ns += kern_now_ns() - g_lat_start[pid % LAT_RING].start_ns;
if (++g_lat.presents % LAT_EVERY) return;
char drv[200] = "";
if (g_reflex_mode > 0 && g_sc) {
if (!g_reflex_timings) g_reflex_timings = (PFN_vkGetLatencyTimingsNV)vkGetDeviceProcAddr(c->device, "vkGetLatencyTimingsNV");
static VkLatencyTimingsFrameReportNV rep[64];
VkGetLatencyMarkerInfoNV gi = {.sType = VK_STRUCTURE_TYPE_GET_LATENCY_MARKER_INFO_NV};
if (g_reflex_timings) {
g_reflex_timings(c->device, g_sc, &gi); // the count first
if (gi.timingCount > 64) gi.timingCount = 64;
for (uint32_t i = 0; i < gi.timingCount; i++)
rep[i] = (VkLatencyTimingsFrameReportNV){.sType = VK_STRUCTURE_TYPE_LATENCY_TIMINGS_FRAME_REPORT_NV};
gi.pTimings = rep;
if (gi.timingCount) g_reflex_timings(c->device, g_sc, &gi);
uint32_t full = 0;
double sum = 0;
for (uint32_t i = 0; i < gi.timingCount; i++) {
const VkLatencyTimingsFrameReportNV *r = &rep[i];
if (r->simStartTimeUs && r->simEndTimeUs && r->renderSubmitStartTimeUs && r->presentStartTimeUs &&
r->gpuRenderEndTimeUs > r->simStartTimeUs)
full++, sum += (double)(r->gpuRenderEndTimeUs - r->simStartTimeUs);
}
snprintf(drv, sizeof drv, "; driver reports: %u of %u frames with every marker, simulation start to GPU render "
"end %.2f ms", full, gi.timingCount, full ? sum / full / 1000.0 : 0.0);
}
}
rt_log("reflex: frame start to present %.2f ms over %llu frames (reflex %s)%s", g_lat.n ? g_lat.ns / 1e6 / g_lat.n : 0.0,
(unsigned long long)g_lat.n, g_reflex_mode == 2 ? "boost" : g_reflex_mode == 1 ? "on" : "off", drv);
g_lat.n = g_lat.ns = 0;
}
uint64_t vk_reflex_frame(void) { return __atomic_load_n(&g_reflex_frame, __ATOMIC_RELAXED); }
void vk_reflex_submit_frame(uint64_t id) { t_reflex_submit = id; }
uint64_t vk_reflex_submit_id(void) { return t_reflex_submit ? t_reflex_submit : vk_reflex_frame(); }
// The present thread, before the blit of frame id: the present of that blit carries id.
void vk_reflex_present_frame(uint64_t id) {
// g_blit_pid feeds flip_locked's VkPresentIdKHR (Reflex) and latency_note (TOTK_TRACE=perf); with neither, the
// present skips the lock
if (!vk_has_reflex() && !(rt_trace & RT_TRACE_PERF)) return;
plat_rmutex_lock(&g_pl);
g_blit_pid = id;
plat_rmutex_unlock(&g_pl);
}
void vk_reflex_marker(int marker, uint64_t pid) {
VkCtx *c = vk_ctx();
if (!c || !c->has_nv_reflex || !g_reflex_marker || g_reflex_mode <= 0 || !g_sc || !pid) return;
plat_rmutex_lock(&g_pl);
if (g_sc && g_reflex_marker) {
VkSetLatencyMarkerInfoNV mi = {
.sType = VK_STRUCTURE_TYPE_SET_LATENCY_MARKER_INFO_NV,
.presentID = pid,
.marker = (VkLatencyMarkerNV)marker
};
g_reflex_marker(c->device, g_sc, &mi);
}
plat_rmutex_unlock(&g_pl);
}
void vk_reflex_sleep(void) {
VkCtx *c = vk_ctx();
if (!c || !c->has_nv_reflex || !g_reflex_sleep || !g_reflex_sem || g_reflex_mode <= 0 || !g_sc) return;
uint64_t val = __atomic_add_fetch(&g_reflex_val, 1, __ATOMIC_RELAXED);
VkLatencySleepInfoNV si = {
.sType = VK_STRUCTURE_TYPE_LATENCY_SLEEP_INFO_NV,
.signalSemaphore = g_reflex_sem,
.value = val
};
// vkLatencySleepNV uses the swapchain, which Vulkan wants externally synchronised with the acquire and the present
// (they run under g_pl on the present thread). The wait for the semaphore stays outside the lock.
VkResult r = VK_ERROR_INITIALIZATION_FAILED;
plat_rmutex_lock(&g_pl);
if (g_sc) r = g_reflex_sleep(c->device, g_sc, &si);
plat_rmutex_unlock(&g_pl);
if (r == VK_SUCCESS) {
VkSemaphoreWaitInfo wi = {
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO,
.semaphoreCount = 1,
.pSemaphores = &g_reflex_sem,
.pValues = &val
};
vkWaitSemaphores(c->device, &wi, 100000000ULL);
}
}
// ---- swapchain
static const char *g_present_got = ""; // the TOTK_VK_PRESENT value of the mode the swapchain got; "" before one
@@ -277,12 +467,29 @@ static void flip_locked(void) {
if (!g_flip_on) return;
g_flip_on = 0;
if (!g_sc) return;
VkPresentInfoKHR pi = {.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, .waitSemaphoreCount = 1,
VkCtx *c = vk_ctx();
uint64_t pid = 0;
VkPresentIdKHR pid_info = {0};
if (c && c->has_nv_reflex) {
// The frame's own ID; a present without one (a probe, a skipped frame's neighbour) takes the next free value,
// as present IDs must grow on a swapchain
pid = g_flip_pid > g_last_pid ? g_flip_pid : g_last_pid + 1;
g_last_pid = pid;
pid_info.sType = VK_STRUCTURE_TYPE_PRESENT_ID_KHR;
pid_info.swapchainCount = 1;
pid_info.pPresentIds = &pid;
vk_reflex_marker(4, pid); // VK_LATENCY_MARKER_PRESENT_START_NV
}
VkPresentInfoKHR pi = {.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
.pNext = (c && c->has_nv_reflex) ? &pid_info : NULL,
.waitSemaphoreCount = 1,
.pWaitSemaphores = &g_render_done[g_flip_idx], .swapchainCount = 1, .pSwapchains = &g_sc,
.pImageIndices = &g_flip_idx};
vk_queue_lock();
VkResult r = vkQueuePresentKHR(vk_ctx()->gfx_queue, &pi);
VkResult r = vkQueuePresentKHR(c->gfx_queue, &pi);
vk_queue_unlock();
if (c && c->has_nv_reflex) vk_reflex_marker(5, pid); // VK_LATENCY_MARKER_PRESENT_END_NV
if (c) latency_note(c, pid ? pid : g_flip_pid);
vk_device_fault_report(r);
if (r == VK_ERROR_OUT_OF_DATE_KHR || r == VK_SUBOPTIMAL_KHR) plat_xchg32(&g_dirty, 1);
}
@@ -326,8 +533,14 @@ static int build_swapchain(void) {
vk_queue_lock();
vkDeviceWaitIdle(c->device);
VkSwapchainKHR old = g_sc;
VkSwapchainLatencyCreateInfoNV sci = {
.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_LATENCY_CREATE_INFO_NV,
.latencyModeEnable = VK_TRUE
};
VkSwapchainCreateInfoKHR ci = {
.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, .surface = g_surface, .minImageCount = count,
.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
.pNext = (c->has_nv_reflex) ? &sci : NULL,
.surface = g_surface, .minImageCount = count,
.imageFormat = sf.format, .imageColorSpace = sf.colorSpace, .imageExtent = ext, .imageArrayLayers = 1,
.imageUsage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, // every surface supports it (the menu overlay draws into it)
@@ -364,6 +577,10 @@ static int build_swapchain(void) {
}
free_capture();
vk_queue_unlock();
// The Reflex sleep mode belongs to the swapchain: a new one (resize, fullscreen, out of date) starts with low
// latency off, so the mode is set on it again even though the setting did not change.
g_reflex_mode = -1;
reflex_apply_locked();
return 1;
}
@@ -734,6 +951,7 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w
if (r == VK_SUCCESS) {
g_flip_on = 1;
g_flip_idx = idx;
g_flip_pid = g_blit_pid; // the frame this blit shows (vk_reflex_present_frame)
if (!defer) flip_locked();
} else {
vk_device_fault_report(r);
@@ -788,6 +1006,11 @@ void gfx_window_release_vk(void) {
vk_color_release();
vk_hdr_release();
vk_contact_release();
if (g_reflex_sem) {
vkDestroySemaphore(c->device, g_reflex_sem, NULL);
g_reflex_sem = VK_NULL_HANDLE;
}
g_reflex_mode = -1;
free_capture();
free_thumbs();
for (int i = 0; i < FRAMES; i++) {
+1004 -284
View File
File diff suppressed because it is too large. Load diff
+31 -2
View File
@@ -24,7 +24,8 @@ void rt_vlogf(const char *fmt, va_list ap);
void rt_log_raw(const char *p, size_t n);
const char *rt_log_path(void); // the log file, NULL when this run writes none
enum { RT_TRACE_SVC = 1, RT_TRACE_HOSTCALL = 2, RT_TRACE_IPC = 4, RT_TRACE_FS = 8, RT_TRACE_GFX = 16, RT_TRACE_PERF = 32,
RT_TRACE_SCHED = 64, RT_TRACE_AUDIO = 128, RT_TRACE_PRESENT = 256 };
RT_TRACE_SCHED = 64, RT_TRACE_AUDIO = 128, RT_TRACE_PRESENT = 256,
RT_TRACE_HOSTCOUNT = 512 }; // no TOTK_TRACE name of its own: hostfn_count switches it (host.h)
extern unsigned rt_trace; // from TOTK_TRACE=svc,host,ipc,fs,gfx,perf,sched,audio,present (or all)
// prof_win.c: TOTK_PROFILE=<path> sampling profiler; TOTK_TRACE=perf frame timing (waits of the render thread)
// PERF_TEX: texture uploads on the render thread (deswizzle, decode, staging), PERF_ASTC included
@@ -36,6 +37,10 @@ void perf_end(int kind, uint64_t t0); // counts render-thread time onl
void perf_replay(uint64_t t0); // replay (NVN -> Vulkan recording) time on any thread, waits inside it included
void perf_replay_thread(void); // the calling thread replays for the render thread: perf_end counts its waits
void perf_main_thread(void); // the calling thread is the guest main thread (TOTK_PROFILE_MAIN)
// The calling thread is one every frame goes through: on a hybrid CPU it prefers the P-cores (main.c, TOTK_PCORE);
// the first thread of each role logs it.
enum { PCORE_MAIN, PCORE_RENDER, PCORE_REPLAY, PCORE_RECORDER, PCORE_PRESENT, PCORE_N };
void perf_core_thread(int role);
// Events per 60-frame window: graphics pipelines finished by the background workers, draws skipped while pending,
// first texture uploads and re-uploads after CPU writes, draws skipped while a shader translates in the background,
// draws that waited for their pending pipeline and got it, draws into a target the previous frame did not render that
@@ -116,7 +121,9 @@ void hostfn_add_override(const char *name, GuestFn fn); // an override outsid
const char *hostfn_name(uint64_t addr); // name registered at a synthetic address, NULL if none
void hostfn_seal(void); // build the dispatch table (rt_dispatch_init over every
// linked module); idempotent; registration ends here
uint64_t hostfn_calls(void); // host-function calls so far, all threads
uint64_t hostfn_calls(void); // host-function calls counted so far, all threads: only
// while hostfn_count is on or under TOTK_TRACE=host
void hostfn_count(int on); // count host-function calls (probes); off by default
void hostfn_report(void); // call count per host function to stderr, busiest first
int hostfn_table_size(int observers); // linked HOST_OVERRIDE (0) / HOST_OBSERVER (1) entries
const char *hostfn_table_name(int observers, int i); // symbol of linked entry i, NULL past the end
@@ -171,6 +178,23 @@ int mem_guest_readable(uint64_t a, uint64_t n); // [a, a+n) can be re
// module or the committed heap answered from the layout,
// anything else by plat_mem_readable (an OS query)
// ---- short out-of-memory spells (oom.c)
enum { RT_OOM_COMMIT, RT_OOM_STAGING, RT_OOM_IMAGE, RT_OOM_CHUNK, RT_OOM_SITES };
extern volatile int32_t rt_oom_req; // set by rt_oom_request; vk_texture_new_submit takes it
void rt_oom_request(void); // asks the Vulkan back end to give memory back at its next
// submit (released textures, headroom blocks); any thread,
// any lock held
int rt_oom_wait(const char *what, uint64_t bytes, int (*retry)(void *arg), void *arg);
// after a failed allocation: requests a release, then calls
// retry every 10 ms for up to 30 s; 1 once retry returned 1,
// 0 after the wait (the caller ends the game). Logs the
// first 8 with the commit numbers
void *rt_realloc_wait(void *old, size_t n, const char *what); // realloc, NULL only after rt_oom_wait gave up
int rt_oom_forced(int site); // 1: fail this attempt at site (TOTK_TEST_OOM=<site>:<n>,
// sites commit, staging, image, chunk; or rt_oom_force)
void rt_oom_force(int site, int n); // probes: fail the next n attempts at site
int rt_oom_forced_left(int site); // forced failures still to come at site
// ---- modules (loader.c)
typedef struct {
const char *name; uint64_t base, size, text_end, data_off; // text_end, data_off: offsets from base
@@ -258,6 +282,8 @@ int cam_pack_build(const void *in, size_t n, const void *zsdic, size_t zsdic_n,
// multiplied, malloc'd; 0 = factor 1 or cannot (logged)
void cam_pack_served(void); // romfs.c serves the multiplied pack
// ---- game resolution (resolution.c): TOTK_OUTPUT_RES, TOTK_RENDER_SCALE
uint64_t res_grass_heap_extra_kib(void); // TOTK_GRASS_RANGE: added to the HeapDef grass heap; 0 = none
uint64_t res_grass_heap_extra_bytes(void); // the same, added to the application pool, 2 MiB steps
uint64_t res_heap_extra_bytes(void); // added to the application pool (mem_init), 2 MiB steps
uint64_t res_heap_extra_kib(void); // added to the HeapDef texture heap and its parent; 0 = none
uint64_t res_tex_heap_extra_bytes(void); // TOTK_TEX_HEAP_EXTRA_MB: added to the application pool and
@@ -280,8 +306,11 @@ int res_is_16x9(unsigned w, unsigned h); // within 1% of 16:9
int res_wants_wide_overlay(const unsigned out[2]); // not 16:9 on a game version with the camera aspect patch
int romfs_shaped_ready(unsigned w, unsigned h); // romfs-patches-<w>x<h> has a manifest (romfs.c; works
// before fs_init)
int romfs_shaped_prompts_ready(const char *folder, unsigned w, unsigned h); // the same for <folder>-<w>x<h>, a
// prompts' folder (romfs-patches-xbox, -xbox-label, -pc)
void res_wanted(unsigned out[2]); // the output size TOTK_OUTPUT_RES asks for
void res_fov_live(void); // TOTK_FOV to the camera (set fov); on a settings change
void res_dynres_live(void); // TOTK_DYNAMIC_RES to the game (set fixres); on a settings change
// ---- code patches (patches.c) and the frame step (frame_rate.c)
void rt_patches_init(void); // after ldr_load: switches from the environment, the patch
// page committed; logs the sets
+20 -9
View File
@@ -2,9 +2,9 @@
// Host functions at synthetic guest addresses: the HOST_OVERRIDE / HOST_OBSERVER
// linker tables, the registry behind them, and the calls between host and guest (rt_call, rt_original).
//
// Every registered function dispatches through hostfn_entry, which counts the call, traces the first one
// (TOTK_TRACE=host) and tail-calls the real function. The registry is fixed by hostfn_seal, which builds the dispatch
// table; guest threads start after that, so lookups need no lock.
// Every registered function dispatches through hostfn_entry, which counts the call (only while hostfn_count is on or
// under TOTK_TRACE=host), traces the first one (TOTK_TRACE=host) and tail-calls the real function. The registry is
// fixed by hostfn_seal, which builds the dispatch table; guest threads start after that, so lookups need no lock.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -46,7 +46,8 @@ static int sealed;
// The call count (hostfn_calls): one counter per calling thread, each on its own cache line, written only by that
// thread. A locked add on a per-function counter beside the read-mostly slot fields made the game threads share and
// bounce those lines on every host call (1.4.3 town: about 1% of the game threads' time in hostfn_entry).
// bounce those lines on every host call (1.4.3 town: about 1% of the game threads' time in hostfn_entry). Only the
// probes and TOTK_TRACE=host read it, so a game run does not count: hostfn_entry tests one rt_trace word for both.
#define MAX_COUNTERS 4096
typedef struct { _Alignas(64) uint64_t n; } Counter;
static Counter counters[MAX_COUNTERS];
@@ -157,18 +158,28 @@ __attribute__((noinline)) static void hostfn_blocking(Cpu *c, GuestFn fn) {
kern_core_back(&away);
}
static void hostfn_entry(Cpu *c) {
uint64_t i = (c->pc - RT_HOSTFN_BASE) >> 4;
if (__builtin_expect((c->pc & 15) || i >= (uint64_t)nslots, 0)) bad_entry(c);
Slot *s = &slots[i];
void hostfn_count(int on) {
if (on) __atomic_fetch_or(&rt_trace, RT_TRACE_HOSTCOUNT, __ATOMIC_RELAXED);
else __atomic_fetch_and(&rt_trace, ~(unsigned)RT_TRACE_HOSTCOUNT, __ATOMIC_RELAXED);
}
// The counted path of hostfn_entry: the call count, and under TOTK_TRACE=host the first call of each function.
__attribute__((noinline, cold)) static void hostfn_watch(Cpu *c, Slot *s) {
uint64_t *k = thread_calls;
if (__builtin_expect(!k, 0)) k = counter_new();
if (__builtin_expect(k == &shared_calls, 0)) __atomic_fetch_add(k, 1, __ATOMIC_RELAXED);
else __atomic_store_n(k, *k + 1, __ATOMIC_RELAXED);
if (__builtin_expect(rt_trace & RT_TRACE_HOSTCALL, 0) && !__atomic_fetch_add(&s->calls, 1, __ATOMIC_RELAXED)) {
if ((rt_trace & RT_TRACE_HOSTCALL) && !__atomic_fetch_add(&s->calls, 1, __ATOMIC_RELAXED)) {
char lr[96];
rt_log("host call %s from %s", s->name, ldr_describe(c->x[30] - 4, lr, sizeof lr));
}
}
static void hostfn_entry(Cpu *c) {
uint64_t i = (c->pc - RT_HOSTFN_BASE) >> 4;
if (__builtin_expect((c->pc & 15) || i >= (uint64_t)nslots, 0)) bad_entry(c);
Slot *s = &slots[i];
if (__builtin_expect(rt_trace & (RT_TRACE_HOSTCALL | RT_TRACE_HOSTCOUNT), 0)) hostfn_watch(c, s);
if (s->blocking) {
hostfn_blocking(c, s->fn);
return;
+227 -36
View File
@@ -21,6 +21,7 @@
#include "ui/menu.h"
#include "vk.h"
#include "platform/platform.h"
#include "gameplay_tweaks.h"
enum { STICK_MAX = 32767, STICK_DIR = 16384, TRIGGER_ON = 16384, RESCAN_MS = 1000 };
@@ -33,6 +34,7 @@ static volatile int32_t g_block, g_menu; // text entry / host menu: 0 off, 1 o
enum { PAD_USED = 16384, MOUSE_USED = 6 };
static volatile int32_t g_kbm_last = 1;
int input_kbm_last(void) { return __atomic_load_n(&g_kbm_last, __ATOMIC_RELAXED); }
int input_gameplay_active(void) { return !g_menu && !g_block && ikb_foreground() && ikb_captured(); }
static SDL_Gamepad *pad;
static uint64_t next_scan;
static int pad_rumble; // the open gamepad has rumble motors
@@ -44,6 +46,7 @@ static volatile int32_t pad_layout_shown = -1; // pad_layout for the menu (any
static SDL_JoystickID sensor_pad; // the gamepad whose sensors the motion thread set up, 0 none
static int sensor_ok; // ... and it has both sensors, enabled
static int motion_thread; // 0 not started, 1 running, -1 off (TOTK_MOTION=0, no SDL)
static char calib_pad_name[128]; // the controller the bias belongs to; empty when there is none
// SDL buttons of the gamepad inputs (input_map.h PAD_*), by position; PAD_ZL and PAD_ZR are the triggers (axes).
static const SDL_GamepadButton pad_sdl[PAD_COUNT] = {
@@ -55,9 +58,11 @@ static const SDL_GamepadButton pad_sdl[PAD_COUNT] = {
[PAD_DPAD_RIGHT] = SDL_GAMEPAD_BUTTON_DPAD_RIGHT, [PAD_DPAD_DOWN] = SDL_GAMEPAD_BUTTON_DPAD_DOWN,
[PAD_LPADDLE1] = SDL_GAMEPAD_BUTTON_LEFT_PADDLE1, [PAD_RPADDLE1] = SDL_GAMEPAD_BUTTON_RIGHT_PADDLE1,
[PAD_LPADDLE2] = SDL_GAMEPAD_BUTTON_LEFT_PADDLE2, [PAD_RPADDLE2] = SDL_GAMEPAD_BUTTON_RIGHT_PADDLE2,
[PAD_TOUCHPAD] = SDL_GAMEPAD_BUTTON_TOUCHPAD,
};
// TOTK_INPUT_SCRIPT=<file>: lines "<frame> <button> [hold]" or "<frame> LSTICK|RSTICK <x> <y> [hold]", ORed into the real input.
// TOTK_INPUT_SCRIPT=<file>: lines "<frame> <button> [hold]", "<frame> LSTICK|RSTICK <x> <y> [hold]", "<frame> MOUSE
// <dx> <dy>" or "<frame> WHEEL <notches>" (+ away from the player), ORed into the real input.
// <frame> and its hold are 60 Hz ticks of game time since the first present (frame_step_ticks: presents at 60 fps), so
// the title menus get the presses at the same game time at any frame limit. A line "@<ms> ..." is timed instead: <ms>
// and its hold are milliseconds of the world clock (frame_step_world_us, game time since the loaded save's first
@@ -74,6 +79,7 @@ static ScriptEv *script_ev;
static int script_n, script_state; // 0 not loaded, 1 loaded
static int script_mixed; // the script has timed lines: its untimed ones end where the world clock starts
static int32_t script_mouse[2]; // one-shot raw counts, consumed by the camera hook; under lock
static int32_t script_wheel; // one-shot wheel notches, taken with the real ones (wheel_stick); under lock
static void script_load(void) {
script_state = 1;
@@ -96,7 +102,11 @@ static void script_load(void) {
ScriptEv e = {.at = (uint64_t)(at * unit + 0.5), .hold = timed ? 66667 : 4, .bit = -1, .timed = timed};
long a, b;
double c;
if (!strcmp(name, "MOUSE") || !strcmp(name, "LSTICK") || !strcmp(name, "RSTICK")) {
if (!strcmp(name, "WHEEL")) {
if (sscanf(line + off, "%ld", &a) != 1) { rt_log("input: script line %d: WHEEL needs notches", ln); continue; }
e.stick = 4, e.x = (int32_t)a, e.hold = 1; // one turn, never held
snprintf(e.text, sizeof e.text, "WHEEL %d", e.x);
} else if (!strcmp(name, "MOUSE") || !strcmp(name, "LSTICK") || !strcmp(name, "RSTICK")) {
int n = sscanf(line + off, "%ld %ld %lf", &a, &b, &c);
if (n < 2) { rt_log("input: script line %d: %s needs x y [hold]", ln, name); continue; }
e.stick = name[0] == 'M' ? 3 : name[0] == 'L' ? 1 : 2; e.x = (int32_t)a; e.y = (int32_t)b;
@@ -131,14 +141,17 @@ static void script_apply(uint64_t *b, int32_t sl[2], int32_t sr[2]) {
ScriptEv *e = &script_ev[i];
if (e->timed ? w < 0 : script_mixed && w >= 0) continue;
uint64_t now = e->timed ? (uint64_t)w : tick;
if (now < e->at || (now >= e->at + e->hold && (e->logged || !e->timed))) continue;
if (e->stick == 3 && e->logged) continue;
// A one-shot (MOUSE, WHEEL) goes in at the first poll at or after its time: a poll that skips a tick (the
// game below 60 fps) would otherwise lose it
if (e->stick >= 3 ? e->logged || now < e->at
: now < e->at || (now >= e->at + e->hold && (e->logged || !e->timed))) continue;
if (!e->logged) {
e->logged = 1;
if (e->timed) rt_log("input: script frame %llu at %.1f ms: %s", (unsigned long long)fr, w / 1000.0, e->text);
else rt_log("input: script frame %llu tick %llu: %s", (unsigned long long)fr, (unsigned long long)tick, e->text);
}
if (e->stick == 3) {
if (e->stick == 4) script_wheel += e->x;
else if (e->stick == 3) {
int64_t x = (int64_t)script_mouse[0] + e->x, y = (int64_t)script_mouse[1] + e->y;
script_mouse[0] = (int32_t)(x < INT32_MIN ? INT32_MIN : x > INT32_MAX ? INT32_MAX : x);
script_mouse[1] = (int32_t)(y < INT32_MIN ? INT32_MIN : y > INT32_MAX ? INT32_MAX : y);
@@ -261,6 +274,17 @@ void input_direct_mouse_sample(int enabled, int32_t *dx, int32_t *dy) {
plat_mutex_unlock(&lock);
}
// A turn of the real wheel as a WHEEL line of the recording (one-shot, as a MOUSE line). Under lock.
static void rec_wheel(int32_t notches) {
if (rec_state != 1) return;
char what[32];
snprintf(what, sizeof what, "WHEEL %d", notches);
int64_t w = frame_step_world_us();
uint64_t tick = frame_step_ticks();
rec_line(tick, w, tick + 1, w, what);
fflush(rec);
}
static float mouse_sens = -1; // TOTK_MOUSE_SENS, 0 = mouse camera off; live (cfg_version)
static unsigned mouse_sens_seen;
@@ -401,6 +425,9 @@ static void scan_pads(void) {
if (pad && SDL_GamepadConnected(pad)) return;
if (pad) {
SDL_CloseGamepad(pad); pad = NULL; sensor_pad = 0; sensor_ok = 0;
// Its bias and an attempt collecting from it go with it; the next pad gets its own in pad_bias_select.
motion_bias_reset(motion_global());
calib_pad_name[0] = 0;
plat_xchg32(&pad_layout_shown, -1);
for (int i = 0; i < 4; i++) plat_xchg32(&pad_labels[i], PAD_LABEL_NONE);
rt_log("input: gamepad disconnected");
@@ -541,35 +568,86 @@ static int32_t shake_stick(int held) {
return (now - from) / SHAKE_SIDE_NS % 2 ? STICK_MAX : -STICK_MAX;
}
// The scroll wheel on the Purah Pad's map, which zooms with the right stick's y (up: in): each notch is a full-tilt
// pulse of MAP_ZOOM_PULSE_NS on that stick with MAP_ZOOM_GAP_NS of neutral after it, so a step of the zoom and a
// held zoom both see one notch as one push. A fast scroll's notches wait their turn (at most MAP_ZOOM_QUEUE), a turn the
// other way drops them. Returns the stick y for this poll, 0 between pulses and off the map. Under lock.
#define MAP_ZOOM_PULSE_NS 100000000ull
#define MAP_ZOOM_GAP_NS 50000000ull
#define MAP_ZOOM_QUEUE 4
static int32_t map_zoom_stick(int32_t notches, int on_map) {
// The scroll wheel (ikb_wheel_take, and an input script's WHEEL lines) as the stick a gamepad player would move:
// - on the Purah Pad's map it zooms: the right stick's y, away from the player zooming in;
// - while a D-pad strip (weapons, shields, bows, materials) is held open it steps the choice: the right stick's x, away
// from the player to the left, towards the player to the right;
// - in a menu with the game's focus box (the inventory, shops, Options, the save list; menu_cursor_menu) it steps the
// focus: the left stick's y, away from the player up;
// - in the Purah Pad's camera (menu_cursor_camera) it zooms: the D-pad's up (away from the player, in) or down, the
// buttons its guide shows for the zoom.
// Each notch is a full-tilt pulse with neutral after it (wheel_timing), so the game sees one notch as one step, and a
// fast scroll's notches wait their turn (at most WHEEL_QUEUE); a turn the other way drops them. A strip's pulse is the
// mouse's (input_direct_mouse_row: 32 ms each way); a menu takes the stick on its own update, a frame of the game's
// 30 Hz or less, so its pulse covers two. Elsewhere, and while the settings menu is open, the notches are dropped.
enum { WHEEL_NONE, WHEEL_MAP, WHEEL_STRIP, WHEEL_MENU, WHEEL_CAMERA };
static const struct { uint64_t pulse_ns, gap_ns; } wheel_timing[] = {
[WHEEL_MAP] = {100000000ull, 50000000ull}, [WHEEL_STRIP] = {32000000ull, 32000000ull},
[WHEEL_MENU] = {70000000ull, 50000000ull}, [WHEEL_CAMERA] = {100000000ull, 50000000ull}};
#define WHEEL_QUEUE 4
#define WHEEL_GAP_NS 300000000ull // a menu's focus gap (a tab switch): queued notches wait this long for its next screen
// The stick value for this poll at the place `where` (WHEEL_*): -STICK_MAX, 0 or STICK_MAX. Under lock.
static int32_t wheel_stick(int32_t notches, int where) {
static int32_t queued, dir;
static uint64_t until; // the end of the pulse (dir != 0) or of the neutral after it
static int place;
static uint64_t until, none_since; // until: the end of the pulse (dir != 0) or of the neutral after it
uint64_t now = plat_now_ns();
if (!on_map) { queued = dir = 0, until = 0; return 0; }
if (where == WHEEL_NONE) {
if (!none_since) none_since = now;
if (now - none_since > WHEEL_GAP_NS) queued = 0, place = WHEEL_NONE;
dir = 0;
return 0;
}
none_since = 0;
if (where != place) queued = dir = 0, until = 0, place = where;
if (notches) {
if (queued && (notches > 0) != (queued > 0)) queued = 0;
queued += notches;
queued = queued > MAP_ZOOM_QUEUE ? MAP_ZOOM_QUEUE : queued < -MAP_ZOOM_QUEUE ? -MAP_ZOOM_QUEUE : queued;
queued = queued > WHEEL_QUEUE ? WHEEL_QUEUE : queued < -WHEEL_QUEUE ? -WHEEL_QUEUE : queued;
}
if (dir) {
if (now < until) return dir * STICK_MAX;
dir = 0, until = now + MAP_ZOOM_GAP_NS;
dir = 0, until = now + wheel_timing[where].gap_ns;
return 0;
}
if (now < until || !queued) return 0;
dir = queued > 0 ? 1 : -1;
queued -= dir;
until = now + MAP_ZOOM_PULSE_NS;
until = now + wheel_timing[where].pulse_ns;
return dir * STICK_MAX;
}
// After the keys, the gamepad and the script: where the wheel goes this poll (b holds the script's D-pad too, so a
// scripted strip takes scripted notches). Under lock.
static void wheel_route(uint64_t *bp, int32_t sl[2], int32_t sr[2]) {
uint64_t b = *bp;
int32_t real = state > 0 ? ikb_wheel_take() : 0, notches = real + script_wheel; // the script's under TOTK_INPUT=none too
script_wheel = 0;
if (menu_is_open()) notches = 0;
if (real && notches) {
__atomic_store_n(&g_kbm_last, 1, __ATOMIC_RELAXED); // the pointer comes to a menu the wheel turns in
rec_wheel(real);
}
// A strip is its D-pad direction held with its screen open; with the menu cursor off (the screens not read,
// menu_cursor_shortcut -1) the buttons alone decide, as for the mouse's selectors
int sc = b & (1ull << B_UP | 1ull << B_LEFT | 1ull << B_RIGHT) && !(b & 1ull << B_L) ? menu_cursor_shortcut() : 0;
int strip = sc == -1 || sc == 1 || (sc == 0 && menu_cursor_strip());
int where = menu_cursor_map() ? WHEEL_MAP
: strip ? WHEEL_STRIP
: menu_cursor_camera() ? WHEEL_CAMERA
: menu_cursor_menu() ? WHEEL_MENU
: WHEEL_NONE;
if (where == WHEEL_MAP && gameplay_tweaks_map_zoom()) where = WHEEL_NONE;
else if (where == WHEEL_STRIP && gameplay_tweaks_strip_wheel(b)) where = WHEEL_NONE;
int32_t v = wheel_stick(notches, where);
if (!v) return;
if (where == WHEEL_CAMERA) *bp |= 1ull << (v > 0 ? B_UP : B_DOWN);
else if (where == WHEEL_MAP) pick(&sr[0], &sr[1], 0, v);
else if (where == WHEEL_STRIP) pick(&sr[0], &sr[1], -v, 0);
else pick(&sl[0], &sl[1], 0, v);
}
// Under lock, state 1: the open gamepad's inputs by position (pd[PAD_*] != 0 held); 0 without a connected gamepad.
static int pad_physical(uint8_t pd[PAD_COUNT]) {
if ((!pad || !SDL_GamepadConnected(pad)) && plat_now_ns() / 1000000u >= next_scan) { next_scan = plat_now_ns() / 1000000u + RESCAN_MS; scan_pads(); }
@@ -578,7 +656,7 @@ static int pad_physical(uint8_t pd[PAD_COUNT]) {
for (int p = PAD_SOUTH; p < PAD_ZL; p++) pd[p] = SDL_GetGamepadButton(pad, pad_sdl[p]);
pd[PAD_ZL] = SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER) > TRIGGER_ON;
pd[PAD_ZR] = SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) > TRIGGER_ON;
for (int p = PAD_ZR + 1; p < PAD_COUNT; p++) pd[p] = SDL_GetGamepadButton(pad, pad_sdl[p]); // false without paddles
for (int p = PAD_ZR + 1; p < PAD_COUNT; p++) pd[p] = SDL_GetGamepadButton(pad, pad_sdl[p]); // false without paddles or touchpad
return 1;
}
@@ -623,19 +701,48 @@ static void poll_merged(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx
if (ikb_foreground()) {
uint8_t down[KEY_COUNT] = {0};
// Mouse buttons count only while the cursor is captured, and a button already held when the capture began
// (the click that refocused the window) is ignored until it is released.
// (the click that refocused the window) is ignored until it is released. While a game menu has the pointer
// (menu_cursor.c) and it is over the window, the left and right buttons are that menu's A and B, and the
// middle and side buttons keep their bindings: bound to L and R they turn the menu's tabs.
static int was_captured;
static uint8_t mouse_ignore[KEY_COUNT];
int cap = ikb_captured();
float pointer_x, pointer_y;
int menu_pointer = !cap && menu_cursor_pointer() && ikb_pointer(&pointer_x, &pointer_y);
for (int k = 1; k < KEY_COUNT; k++) {
down[k] = ikb_down(k);
kbm_used |= down[k];
if (k < KEY_MOUSE_L || k > KEY_MOUSE_X2) continue;
if (cap && !was_captured) mouse_ignore[k] = down[k];
if (!down[k]) mouse_ignore[k] = 0;
if (!cap || mouse_ignore[k]) down[k] = 0;
int counts = cap || (menu_pointer && k != KEY_MOUSE_L && k != KEY_MOUSE_R);
if (!counts || mouse_ignore[k]) down[k] = 0;
}
was_captured = cap;
// On the Purah Pad's map (menu_cursor.c finds it, TOTK_MAP_CLICK) the left mouse button presses A at the
// map's cursor, which the mouse moves there, instead of its binding (Y by default). A press is one or the
// other from the moment it goes down until it is released, so a button held as the map opens or closes
// keeps what it pressed.
static int map_held, map_a;
if (!down[KEY_MOUSE_L]) map_held = map_a = 0;
else if (!map_held) map_held = 1, map_a = menu_cursor_map() && cfg_bool("TOTK_MAP_CLICK");
if (map_a) down[KEY_MOUSE_L] = 0, b |= 1ull << B_A;
// While a dialogue box is on screen (menu_cursor.c, TOTK_DIALOGUE_CLICK) the left mouse button presses B,
// which advances the text and also shows the rest of a line at once, and the right one presses A, instead
// of their bindings: Y by default, which opens the dialogue log there, and ZL. A press is one or the other
// from the moment it goes down until it is released, so a click that closes the last box does not turn
// into an attack while it is still held, and a button held as the box opens does not skip its first line.
// Dialogue choices have the pointer, and their clicks are menu_cursor.c's: a left click there is A.
static const uint8_t click_key[2] = {KEY_MOUSE_L, KEY_MOUSE_R}, click_btn[2] = {B_B, B_A};
static int click_held[2], click_talk[2];
for (int i = 0; i < 2; i++) {
int key = click_key[i];
if (!down[key]) click_held[i] = click_talk[i] = 0;
else if (!click_held[i]) click_held[i] = 1, click_talk[i] = menu_cursor_dialogue();
if (click_talk[i]) down[key] = 0, b |= 1ull << click_btn[i];
}
if (cap && !g_menu && !g_block) gameplay_tweaks_prepare_keys(down, b);
else gameplay_tweaks_reset();
b |= input_map_buttons(down);
int32_t k[2];
walk_get();
@@ -653,13 +760,10 @@ static void poll_merged(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx
// Before the screens were read (the menu cursor off) the buttons alone decide, as before.
if (selector && menu_cursor_shortcut() == 0) selector = 0;
mouse_camera(k, selector);
// On the map the left stick moves the cursor and the right one zooms: the mouse moves the cursor there,
// the scroll wheel zooms (map_zoom_stick). Elsewhere the wheel is unbound; its notches are dropped.
int map = menu_cursor_map();
if (map) pick(&sl[0], &sl[1], k[0], k[1]);
// On the map the left stick moves the cursor and the right one zooms: the mouse moves the cursor there
// (the scroll wheel zooms, wheel_route)
if (menu_cursor_map()) pick(&sl[0], &sl[1], k[0], k[1]);
else pick(&sr[0], &sr[1], k[0], k[1]);
int32_t zoom = map_zoom_stick(ikb_wheel_take(), map);
if (zoom) pick(&sr[0], &sr[1], 0, zoom);
int32_t shake = shake_stick(input_map_held(down, B_SHAKE));
if (shake) sr[0] = shake, sr[1] = 0;
// A mouse shake (mouse_direct.c) moves the stick with the mouse, side by side, and lets go when it rests
@@ -674,6 +778,8 @@ static void poll_merged(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx
else if (kbm_used) __atomic_store_n(&g_kbm_last, 1, __ATOMIC_RELAXED);
}
script_apply(&b, sl, sr);
wheel_route(&b, sl, sr);
gameplay_tweaks_apply(__atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED), input_gameplay_active(), &b, &sl[0], &sl[1], &sr[0], &sr[1]);
rec_apply(b, sl, sr);
// Minus and Plus held together (gamepad or keys) open or close the menu and never reach the game
static int combo_held;
@@ -705,10 +811,12 @@ static volatile uint64_t g_block_closed, g_menu_closed;
int input_cursor_allowed(void) {
const char *in = cfg_get("TOTK_INPUT");
return g_block != 1 && g_menu != 1 && mouse_sens_get() > 0 && !(in && !strcmp(in, "none")) && !menu_cursor_pointer();
return g_block != 1 && g_menu != 1 && (gameplay_tweaks_capture_needed() || mouse_sens_get() > 0) &&
!(in && !strcmp(in, "none")) && !menu_cursor_pointer();
}
void input_block(int on) {
if (on) gameplay_tweaks_reset();
if (!on) __atomic_store_n(&g_block_closed, plat_now_ns(), __ATOMIC_RELEASE);
plat_xchg32(&g_block, on ? 1 : 2);
ikb_cursor_update();
@@ -717,6 +825,7 @@ void input_block(int on) {
// The in-game menu (ui/menu.cpp) blocks the game's input the same way: g_menu 0 closed, 1 open, 2 closed, waiting for
// the release. The cursor is released while it is open, for the menu's mouse.
void input_menu(int open) {
if (open) gameplay_tweaks_reset();
if (!open) __atomic_store_n(&g_menu_closed, plat_now_ns(), __ATOMIC_RELEASE);
plat_xchg32(&g_menu, open ? 1 : 2);
if (!open) ikb_menu_closed(); // the capture before Esc comes back
@@ -851,11 +960,56 @@ void input_motion_axes(const float a[3], const float g[3], double acc[3], double
gyro[2] = g[1];
}
// Copy a controller name into cap bytes without splitting a UTF-8 sequence: a name cut mid-character would show as
// garbage in the menu and be written to totk.ini the same way.
static void copy_name(char *dst, size_t cap, const char *src) {
if (!src) src = "";
size_t n = strlen(src);
if (n >= cap) {
n = cap - 1;
while (n > 0 && ((unsigned char)src[n] & 0xC0) == 0x80) n--; // back off to the start of the character
}
memcpy(dst, src, n);
dst[n] = 0;
}
// Under lock: the open gamepad's identity for the saved bias. Its name, plus a hash of SDL's serial when the pad
// reports one (Switch and PlayStation pads report their Bluetooth address), so two controllers of one model keep their
// own bias; the hash keeps the address out of totk.ini and the log, which players post with bug reports.
static void pad_identity(char *dst, size_t cap) {
const char *name = SDL_GetGamepadName(pad), *serial = SDL_GetGamepadSerial(pad);
char id[160];
if (serial && *serial) {
uint32_t h = 2166136261u; // FNV-1a
for (const char *c = serial; *c; c++) h = (h ^ (uint8_t)*c) * 16777619u;
snprintf(id, sizeof id, "%s #%08x", name ? name : "", h);
} else snprintf(id, sizeof id, "%s", name ? name : "");
copy_name(dst, cap, id);
}
// Under lock, for each gamepad the sensor code sees (a first one, or one connected after another went away): the
// previous controller's bias and any attempt still collecting from it are dropped, then the bias saved in totk.ini is
// taken when gyro_bias_pad names this controller. It is taken with gyro_calibration off too: the switch only decides
// whether motion_feed subtracts it, so turning the switch on mid-run applies the saved bias.
static void pad_bias_select(void) {
char id[128];
pad_identity(id, sizeof id);
motion_bias_reset(motion_global());
calib_pad_name[0] = 0;
const char *saved = cfg_get("TOTK_GYRO_BIAS_PAD");
if (saved && !strcmp(saved, id)) {
motion_bias_load(motion_global());
copy_name(calib_pad_name, sizeof calib_pad_name, SDL_GetGamepadName(pad));
rt_log("input: gyro bias loaded from totk.ini for %s", calib_pad_name);
}
}
// Under lock: (re)enable the sensors when the open gamepad changed; 1 if it has both and they are on.
static int sensors_ready(void) {
SDL_JoystickID id = SDL_GetGamepadID(pad);
if (id == sensor_pad) return sensor_ok;
sensor_pad = id;
pad_bias_select();
// Nothing drains the SDL event queue: no event per sample (the data still updates, SDL_SendJoystickSensor)
SDL_SetEventEnabled(SDL_EVENT_GAMEPAD_SENSOR_UPDATE, false);
sensor_ok =SDL_GamepadHasSensor(pad, SDL_SENSOR_GYRO) && SDL_GamepadHasSensor(pad, SDL_SENSOR_ACCEL) &&
@@ -903,16 +1057,53 @@ static void motion_main(void *arg) {
}
}
// Under lock: the sensor thread, once; -1 when SDL is not running.
static void motion_thread_start(void) {
if (!state) start();
if (motion_thread) return;
if (!input_motion_enabled()) rt_log("input: motion off (TOTK_MOTION=0); it can be turned on while the game runs");
if (state != 1) motion_thread = -1;
else {
if (!plat_thread_spawn(motion_main, NULL, "motion", 0)) rt_fatal("input: cannot start the motion thread");
motion_thread = 1;
}
}
void input_motion_start(void) {
plat_mutex_lock(&lock);
if (!state) start();
if (!motion_thread) {
if (!input_motion_enabled()) rt_log("input: motion off (TOTK_MOTION=0); it can be turned on while the game runs");
if (state != 1) motion_thread = -1;
else {
if (!plat_thread_spawn(motion_main, NULL, "motion", 0)) rt_fatal("input: cannot start the motion thread");
motion_thread = 1;
}
motion_thread_start();
plat_mutex_unlock(&lock);
}
void input_motion_calibrate(void) {
plat_mutex_lock(&lock);
// The menu can run before the game starts the six-axis sensor (the title screen): the thread starts here then.
if (input_gyro_calibration_enabled() && input_motion_enabled()) motion_thread_start();
const char *why = NULL;
if (!input_gyro_calibration_enabled()) why = "gyro_calibration is off";
else if (!input_motion_enabled()) why = "motion is off (TOTK_MOTION=0)";
else if (motion_thread != 1) why = "no SDL gamepad support";
else if (!pad || !SDL_GamepadConnected(pad)) why = "no gamepad";
// sensors_ready here, not at the sensor thread's next look at a new pad: that look selects the pad's bias and
// would cancel the attempt that starts below.
else if (!sensors_ready()) why = "the gamepad has no gyro and accelerometer";
if (why) rt_log("input: calibrate gyro ignored: %s", why);
else {
char id[128];
pad_identity(id, sizeof id);
copy_name(calib_pad_name, sizeof calib_pad_name, SDL_GetGamepadName(pad));
motion_calibrate_start(motion_global(), id, kern_now_ns());
rt_log("input: gyro calibration started on %s", calib_pad_name);
}
plat_mutex_unlock(&lock);
}
int input_gyro_calibration_enabled(void) { return motion_calibration_enabled(); }
int input_motion_calib_state(char *pad, size_t cap) {
plat_mutex_lock(&lock);
int st = motion_thread == 1 ? motion_calib_poll(motion_global(), kern_now_ns()) : MOTION_CALIB_NONE;
if (pad && cap) snprintf(pad, cap, "%s", calib_pad_name);
plat_mutex_unlock(&lock);
return st;
}
+43
View File
@@ -28,6 +28,20 @@ uint64_t input_buttons_last(void);
// mouse_direct.c: the right stick's side a mouse shake gives while Ultrahand holds an object (-1 / +1), 0 while the
// mouse does not shake; input.c puts it on the stick
int input_direct_mouse_shake(void);
enum {
INPUT_CAM_UNCLASSIFIED = 0,
INPUT_CAM_FREE_ROAM,
INPUT_CAM_AIM,
INPUT_CAM_SCOPE,
INPUT_CAM_LOCK_ON,
INPUT_CAM_MENU_WHEEL,
INPUT_CAM_ABILITY,
INPUT_CAM_ULTRAHAND_OBJECT,
INPUT_CAM_VEHICLE,
INPUT_CAM_DISABLED,
};
int input_camera_context(void);
int input_gameplay_active(void);
// menu_cursor.c: the L ability wheel or a D-pad strip has the focus: 1 / 0, -1 with the menu cursor off (not read)
int menu_cursor_shortcut(void);
// menu_cursor.c: the pointer in the game's menus (1.4.3). poll: once per pad read, adds a click's A or B; pointer: a
@@ -35,6 +49,15 @@ int menu_cursor_shortcut(void);
void menu_cursor_poll(uint64_t *buttons);
int menu_cursor_pointer(void);
int menu_cursor_map(void); // the Purah Pad's map is open: the mouse's stick is the left one (its cursor)
// a dialogue box is on screen and TOTK_DIALOGUE_CLICK is on: left click is B, right click A (input.c poll_merged)
int menu_cursor_dialogue(void);
// A menu screen (not the ability wheel, a D-pad strip or the map) has the game's focus box, whichever device was used
// last: input.c turns the scroll wheel into steps of its focus. 0 with the menu cursor off.
int menu_cursor_menu(void);
// A D-pad strip's screen (ShortCut_00) is open; it has no focus box while held. 0 with the menu cursor off.
int menu_cursor_strip(void);
// The Purah Pad's camera is open: its frame and pointer without the map the Scope has. 0 with the menu cursor off.
int menu_cursor_camera(void);
// The same for host UI (the text entry): never blocked. input_block(1) while a text entry is open: input_poll then
// reports nothing; input_block(0) when it closes: nothing until all buttons and stick directions are released.
void input_poll_ui(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx, int32_t *ry);
@@ -91,6 +114,20 @@ int input_rumble_triggers_enabled(void);
void input_motion_start(void);
// TOTK_MOTION is not "0" (a live setting: read again when cfg_version changes). Off: the sensor reads at rest.
int input_motion_enabled(void);
// The Controls tab's Calibrate gyro row: start a bias measurement on the open gamepad (starts the sensor thread when
// the game has not yet). Callable from any thread.
void input_motion_calibrate(void);
// What the Calibrate gyro row shows (MOTION_CALIB_*): NONE no bias for this controller, COLLECTING, DONE a bias in
// effect (measured in this run or loaded from totk.ini), FAILED the last attempt got no samples and no bias is in
// effect, MOVED the last attempt was rejected because the controller moved. pad receives the name of the controller the
// bias belongs to (empty when none), copied so the caller does not read a buffer another thread may be rewriting.
enum { MOTION_CALIB_NONE, MOTION_CALIB_COLLECTING, MOTION_CALIB_DONE, MOTION_CALIB_FAILED, MOTION_CALIB_MOVED };
int input_motion_calib_state(char *pad, size_t cap);
// The bias's own switch (gyro_calibration, live). Off: no collection, the fusion takes the raw gyro, and the row's
// state reads as before any calibration while the saved keys stay in totk.ini.
int input_gyro_calibration_enabled(void);
// The fusion's own read of the same switch (motion_fusion.c); input.c reaches it for the row's state.
int motion_calibration_enabled(void);
// SDL sensor values (accelerometer m/s^2, gyro rad/s, SDL gamepad axes) to the Switch controller frame motion_feed
// takes (acceleration in G as gravity reads, rotation rad/s). The mapping is documented in input_win.c.
void input_motion_axes(const float sdl_acc[3], const float sdl_gyro[3], double acc_g[3], double gyro[3]);
@@ -108,3 +145,9 @@ MotionFusion *motion_global(void); // the gamepad's, fed by input_win.c
void motion_feed(MotionFusion *m, const double acc_g[3], const double gyro_rad_s[3], uint64_t now_ns);
int motion_read(MotionFusion *m, MotionState *out, int max, uint64_t now_ns); // newest first; 0 = none in 200 ms
void motion_restart(MotionFusion *m); // angle and direction start over at the next sample
// Start a bias measurement at now_ns: samples while the controller rests, then the bias or a rejection (MOTION_CALIB_*).
// pad is the controller's identity, written to totk.ini with the bias.
void motion_calibrate_start(MotionFusion *m, const char *pad, uint64_t now_ns);
int motion_calib_poll(MotionFusion *m, uint64_t now_ns); // MOTION_CALIB_*; ends an attempt whose samples stopped
void motion_bias_load(MotionFusion *m); // take the bias from totk.ini (TOTK_GYRO_BIAS_*)
void motion_bias_reset(MotionFusion *m); // no bias and no attempt (another controller)
+21 -4
View File
@@ -16,7 +16,7 @@
#define DEFAULT_KEYS \
{KEY_W, B_SL_UP}, {KEY_A, B_SL_LEFT}, {KEY_S, B_SL_DOWN}, {KEY_D, B_SL_RIGHT}, /* move */ \
{KEY_UP, B_SR_UP}, {KEY_LEFT, B_SR_LEFT}, {KEY_DOWN, B_SR_DOWN}, {KEY_RIGHT, B_SR_RIGHT}, /* camera, second source */ \
{KEY_MOUSE_L, B_Y}, {KEY_MOUSE_R, B_ZL}, {KEY_Z, B_ZL}, {KEY_F, B_ZR}, {KEY_X, B_ZR}, \
{KEY_MOUSE_L, B_Y}, {KEY_T, B_Y}, {KEY_MOUSE_R, B_ZL}, {KEY_Z, B_ZL}, {KEY_F, B_ZR}, {KEY_X, B_ZR}, \
{KEY_SPACE, B_X}, {KEY_LSHIFT, B_B}, {KEY_E, B_A}, {KEY_Q, B_L}, {KEY_R, B_R}, \
{KEY_LCTRL, B_STICK_L}, {KEY_C, B_STICK_R}, \
{KEY_1, B_LEFT}, {KEY_2, B_UP}, {KEY_3, B_RIGHT}, {KEY_4, B_DOWN}, \
@@ -31,7 +31,7 @@
{PAD_ZR, B_ZR}
#define NINTENDO_PADS DEFAULT_PADS(B_B, B_A, B_Y, B_X)
#define XBOX_PADS DEFAULT_PADS(B_A, B_B, B_X, B_Y)
enum { DEFAULT_KEY_COUNT = 27, DEFAULT_PAD_COUNT = 16 };
enum { DEFAULT_KEY_COUNT = 28, DEFAULT_PAD_COUNT = 16 };
_Static_assert(sizeof (Binding[]){DEFAULT_KEYS} / sizeof (Binding) == DEFAULT_KEY_COUNT, "DEFAULT_KEY_COUNT");
static const InputMap defaults = {{DEFAULT_KEYS}, DEFAULT_KEY_COUNT, {{NINTENDO_PADS}, {XBOX_PADS}},
@@ -44,7 +44,8 @@ static const char *const action_names[ACTION_COUNT] = {
"LSTICK_LEFT", "LSTICK_UP", "LSTICK_RIGHT", "LSTICK_DOWN", "RSTICK_LEFT", "RSTICK_UP", "RSTICK_RIGHT", "RSTICK_DOWN", "SHAKE"};
static const char *const pad_names[PAD_COUNT] = {
NULL, "south", "east", "west", "north", "lstick", "rstick", "l", "r", "start", "back",
"dpad_left", "dpad_up", "dpad_right", "dpad_down", "zl", "zr", "lpaddle1", "rpaddle1", "lpaddle2", "rpaddle2"};
"dpad_left", "dpad_up", "dpad_right", "dpad_down", "zl", "zr", "lpaddle1", "rpaddle1", "lpaddle2", "rpaddle2",
"touchpad"};
static const char *const key_names[KEY_COUNT] = { // each group starts at its id; the ids of a group are consecutive
[KEY_A] = "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z",
@@ -166,11 +167,27 @@ int input_map_bind(const char *button, const char *list) {
void input_map_configure(void) {
input_map_reset();
uint8_t lined[ACTION_COUNT] = {0};
for (int a = 0; a < ACTION_COUNT; a++) {
char var[32];
snprintf(var, sizeof var, "TOTK_BIND_%s", action_names[a]);
const char *v = cfg_get(var);
if (v && *v && input_map_bind(action_names[a], v)) rt_log("input: %s = %s", var + 5, v);
if (v && *v && input_map_bind(action_names[a], v)) lined[a] = 1, rt_log("input: %s = %s", var + 5, v);
}
// A key a line binds leaves the defaults of the actions without a line, as the remapping rows move a key: with
// bind_X = key:t, T presses X only, not also Y, whose defaults hold it.
uint8_t bound[KEY_COUNT] = {0}, list[INPUT_BINDING_MAX];
for (int a = 0; a < ACTION_COUNT; a++) {
if (!lined[a]) continue;
int n = input_map_keys_of(&input_map, a, list, INPUT_BINDING_MAX);
for (int i = 0; i < n; i++) bound[list[i]] = 1;
}
for (int a = 0; a < ACTION_COUNT; a++) {
if (lined[a]) continue;
int n = input_map_keys_of(&input_map, a, list, INPUT_BINDING_MAX), w = 0;
for (int i = 0; i < n; i++)
if (!bound[list[i]]) list[w++] = list[i];
if (w < n) keys_put(&input_map, a, list, w);
}
}
+5 -3
View File
@@ -37,13 +37,14 @@ enum {
// Gamepad inputs by position (Nintendo layout, as SDL names them), platform-neutral like the key ids. ZL and ZR are the
// analog triggers; input_win.c translates SDL buttons and axes to these (one place). The paddles are the back buttons
// of an Xbox Elite, DualSense Edge or Steam Controller (SDL's LEFT_PADDLE1 and so on); no Switch button has them by
// default, a bind line or the remapping rows add them.
// of an Xbox Elite, DualSense Edge or Steam Controller (SDL's LEFT_PADDLE1 and so on), the touchpad is the click of a
// DualSense or DualShock 4 touchpad (SDL's TOUCHPAD); no Switch button has them by default, a bind line or the
// remapping rows add them. A pad without one never reports it held.
enum {
PAD_NONE = 0,
PAD_SOUTH, PAD_EAST, PAD_WEST, PAD_NORTH, PAD_LSTICK, PAD_RSTICK, PAD_L, PAD_R, PAD_START, PAD_BACK,
PAD_DPAD_LEFT, PAD_DPAD_UP, PAD_DPAD_RIGHT, PAD_DPAD_DOWN, PAD_ZL, PAD_ZR,
PAD_LPADDLE1, PAD_RPADDLE1, PAD_LPADDLE2, PAD_RPADDLE2,
PAD_LPADDLE1, PAD_RPADDLE1, PAD_LPADDLE2, PAD_RPADDLE2, PAD_TOUCHPAD,
PAD_COUNT
};
@@ -80,6 +81,7 @@ const char *input_pad_name(int pad);
// skipped; when none is valid the action keeps its bindings. 1 when applied.
int input_map_bind(const char *button, const char *list);
// Applies the TOTK_BIND_<action> settings (cfg_get) over the defaults: at start, and again when one of them changes.
// A key a line binds leaves the defaults of the actions that have no line.
void input_map_configure(void);
// The layout for a TOTK_PAD_LAYOUT value: "nintendo" or "xbox" (any case); "xbox_prompts", 0.1.9's name for the Nintendo
+41 -2
View File
@@ -12,18 +12,21 @@
#include <SDL3/SDL.h>
#include "cfg.h"
#include "host.h"
#include "input.h"
#include "input_map.h"
#include "input_sdl.h"
#include "input_wsys.h"
#include "gameplay_tweaks.h"
#include "platform/platform.h"
static SDL_Window *win;
static volatile uint8_t key_held[KEY_COUNT];
static volatile int32_t esc_held, f8_held, focused;
static volatile int32_t mouse_dx, mouse_dy; // relative motion since the last ikb_mouse_take, whole counts
static volatile int32_t mouse_wheel; // wheel turned while captured, in 1/120 notches (ikb_wheel_take)
static volatile int32_t mouse_wheel; // wheel turned while captured or over the window, in 1/120 notches
// (ikb_wheel_take)
static float rem_x, rem_y; // window thread: the fractions SDL reported beyond them
static volatile int32_t mouse_travel; // raw counts moved in total, captured or not (ikb_mouse_travel)
static float travel_rem; // window thread: the fraction of a count beyond mouse_travel
@@ -143,6 +146,7 @@ static void cursor_apply(void) {
plat_xchg32(&mouse_dy, 0);
rem_x = rem_y = 0;
plat_xchg32(&captured, want);
if (!want) gameplay_tweaks_reset();
}
// The pointer's place in the window (the window thread): x and y are SDL window units, the same space the mouse
@@ -199,8 +203,37 @@ void ikb_menu_closed(void) {
if (o & CUR_ESC) plat_cas32(&cur_rel, 0, o);
}
// pointer_image = 1: the pointer over the window while the cursor is not captured is cursor_art.c's, sized by the
// window's display scale; 0 (the default) keeps SDL's arrow. Window thread, at the window's creation and whenever the
// settings change (input_sdl_idle), so the menu switches it live. SDL's arrow stays when SDL refuses the image.
static void art_cursor(SDL_Window *w) {
static SDL_Cursor *art;
static int shown = -1, failed;
static unsigned seen = ~0u;
unsigned v = cfg_version();
if (v == seen && shown >= 0) return;
seen = v;
int want = cfg_bool("TOTK_POINTER_IMAGE");
if (want == shown) return;
if (want && !art && !failed) {
float scale = SDL_GetWindowDisplayScale(w);
int size = (int)lrintf(32 * (scale > 0 ? scale : 1)), hot;
size = size < 32 ? 32 : size > 128 ? 128 : size;
SDL_Surface *s = SDL_CreateSurface(size, size, SDL_PIXELFORMAT_ARGB8888);
if (s && s->pitch == size * 4) {
cursor_art(s->pixels, size, &hot);
art = SDL_CreateColorCursor(s, hot, hot);
}
if (s) SDL_DestroySurface(s);
if (!art) failed = 1, rt_log("input: the pointer's image failed (%s); the arrow stays", SDL_GetError());
}
SDL_SetCursor(want && art ? art : SDL_GetDefaultCursor());
shown = want;
}
void input_sdl_window(SDL_Window *w) {
win = w;
art_cursor(w);
wake_event = SDL_RegisterEvents(1);
plat_xchg32(&focused, (SDL_GetWindowFlags(w) & SDL_WINDOW_INPUT_FOCUS) != 0);
__atomic_store_n(&ptr_have, 0, __ATOMIC_RELEASE); // the pointer's place is unknown until it enters or moves
@@ -209,6 +242,7 @@ void input_sdl_window(SDL_Window *w) {
void input_sdl_idle(void) {
if (cur_pending) cursor_apply();
if (win) art_cursor(win);
pointer_apply();
}
@@ -266,10 +300,15 @@ void input_sdl_event(const SDL_Event *e) {
__atomic_store_n(&ptr_have, 0, __ATOMIC_RELEASE);
break;
case SDL_EVENT_MOUSE_WHEEL: // notches, + away from the player (a flipped wheel reports the other sign)
if (captured) {
// Captured, or released over the focused window: a game menu with the pointer takes it (input.c)
if (captured || (focused && __atomic_load_n(&ptr_have, __ATOMIC_ACQUIRE))) {
float y = e->wheel.direction == SDL_MOUSEWHEEL_FLIPPED ? -e->wheel.y : e->wheel.y;
plat_add32(&mouse_wheel, (int32_t)(y * 120));
}
if (input_gameplay_active()) {
double delta = e->wheel.direction == SDL_MOUSEWHEEL_FLIPPED ? e->wheel.y : -e->wheel.y;
gameplay_tweaks_wheel(delta, ikb_down(KEY_C), ikb_down(KEY_MOUSE_R));
}
break;
case SDL_EVENT_WINDOW_FOCUS_GAINED:
plat_xchg32(&focused, 1);
+54 -4
View File
@@ -13,6 +13,7 @@
#include "input_win.h"
#include "input_map.h"
#include "input_wsys.h"
#include "gameplay_tweaks.h"
#include "nvn_api.h"
#include "ui/menu.h"
#include "vk.h"
@@ -84,7 +85,8 @@ uint32_t input_key_char(int key) {
// ---- mouse camera: raw motion accumulated by the window thread, one stick sample per presented frame
static volatile LONG mouse_dx, mouse_dy; // raw counts since the last frame sample
static volatile LONG mouse_travel; // raw counts moved in total, captured or not (ikb_mouse_travel)
static volatile LONG mouse_wheel; // wheel turned while captured, in WHEEL_DELTA units (ikb_wheel_take)
static volatile LONG mouse_wheel; // wheel turned while captured or over the window, in WHEEL_DELTA units
// (ikb_wheel_take)
static volatile LONG cur_captured; // cursor hidden and confined to the client area
static volatile LONG cur_active; // WM_ACTIVATEAPP: the window's application is active
// Bits: CUR_REL, Esc or activation by a click on the title bar or border: until a client click; CUR_ESC, set with CUR_REL
@@ -118,9 +120,43 @@ static void cursor_update(HWND h) {
if (cur_captured) ClipCursor(NULL);
InterlockedExchange(&cur_captured, 0);
}
if (!cur_captured) gameplay_tweaks_reset();
ReleaseSRWLockExclusive(&cur_lock);
}
// The pointer shown over the client area while the cursor is not captured (cursor_art.c), 32 pixels at 96 DPI and
// larger with the window's DPI; made again when the DPI changes. Window thread. NULL when Windows refuses it: the
// window class's arrow stays.
static HCURSOR art_cursor(HWND h) {
static HCURSOR cur;
static UINT made_dpi;
UINT dpi = GetDpiForWindow(h);
if (!dpi) dpi = USER_DEFAULT_SCREEN_DPI;
if (cur && dpi == made_dpi) return cur;
int size = MulDiv(32, (int)dpi, USER_DEFAULT_SCREEN_DPI), hot;
size = size < 32 ? 32 : size > 128 ? 128 : size;
BITMAPV5HEADER bh = {.bV5Size = sizeof bh, .bV5Width = size, .bV5Height = -size, .bV5Planes = 1, .bV5BitCount = 32,
.bV5Compression = BI_BITFIELDS, .bV5RedMask = 0xff0000, .bV5GreenMask = 0xff00,
.bV5BlueMask = 0xff, .bV5AlphaMask = 0xff000000u};
void *bits = NULL;
HDC dc = GetDC(NULL);
HBITMAP color = CreateDIBSection(dc, (BITMAPINFO *)&bh, DIB_RGB_COLORS, &bits, NULL, 0);
ReleaseDC(NULL, dc);
HBITMAP mask = CreateBitmap(size, size, 1, 1, NULL);
HCURSOR made = NULL;
if (color && mask && bits) {
cursor_art(bits, size, &hot);
ICONINFO ii = {.fIcon = FALSE, .xHotspot = (DWORD)hot, .yHotspot = (DWORD)hot, .hbmMask = mask,
.hbmColor = color};
made = (HCURSOR)CreateIconIndirect(&ii);
}
if (color) DeleteObject(color);
if (mask) DeleteObject(mask);
if (!made) return cur; // the old size, or NULL
cur = made, made_dpi = dpi; // the old one may still be on screen: kept (a DPI change is rare)
return cur;
}
int input_cursor_released(void) { return (cur_rel & CUR_REL) != 0; }
void input_cursor_suspend(int on) {
@@ -148,13 +184,27 @@ int input_window_msg(HWND h, UINT m, WPARAM w, LPARAM l, LRESULT *res) {
InterlockedAdd(&mouse_dy, y);
}
}
// The wheel comes in the same message (usButtonData, signed, + away from the player)
if (cur_captured && ri.header.dwType == RIM_TYPEMOUSE && (ri.data.mouse.usButtonFlags & RI_MOUSE_WHEEL))
// The wheel comes in the same message (usButtonData, signed, + away from the player). It counts while the
// cursor is captured, or released over the client area: a game menu with the pointer (menu_cursor.c) takes it
// as steps of its focus. input.c drops the notches where nothing takes them.
float px, py;
if (ri.header.dwType == RIM_TYPEMOUSE && (ri.data.mouse.usButtonFlags & RI_MOUSE_WHEEL) &&
(cur_captured || ikb_pointer(&px, &py)))
InterlockedAdd(&mouse_wheel, (SHORT)ri.data.mouse.usButtonData);
break; // DefWindowProc must see WM_INPUT for cleanup
}
case WM_MOUSEWHEEL:
if (input_gameplay_active())
gameplay_tweaks_wheel(-(double)(short)HIWORD(w) / WHEEL_DELTA,
ikb_down(KEY_C), ikb_down(KEY_MOUSE_R));
break;
case WM_SETCURSOR:
if (cur_captured && LOWORD(l) == HTCLIENT) { SetCursor(NULL); *res = TRUE; return 1; }
if (LOWORD(l) == HTCLIENT) {
// pointer_image = 1: the pointer of cursor_art.c in place of the window class's arrow (0, the default:
// the arrow, left to DefWindowProc)
HCURSOR c = cur_captured || !cfg_bool("TOTK_POINTER_IMAGE") ? NULL : art_cursor(h);
if (cur_captured || c) { SetCursor(c); *res = TRUE; return 1; }
}
break;
case WM_SYSCOMMAND: // Alt pressed alone would enter the window menu loop, which stalls the game: Alt is a game key
if ((w & 0xFFF0) == SC_KEYMENU && l == 0) { *res = 0; return 1; }
+8 -2
View File
@@ -13,10 +13,16 @@ void ikb_pointer_set(float nx, float ny); // moves the pointer there (the wind
int ikb_captured(void); // the cursor is captured for the mouse camera
void ikb_mouse_take(int32_t *dx, int32_t *dy); // raw mouse motion since the last call, in counts
uint32_t ikb_mouse_travel(void); // raw counts the mouse moved in total (|dx| + |dy|), captured or not
int32_t ikb_wheel_take(void); // whole wheel notches turned while captured since the last call (+ away
// from the player); a high-resolution wheel's part of a notch is kept
int32_t ikb_wheel_take(void); // whole wheel notches turned since the last call while the cursor was
// captured or over the focused window (+ away from the player); a
// high-resolution wheel's part of a notch is kept
void ikb_cursor_update(void); // what input_cursor_allowed says may have changed
void ikb_menu_closed(void); // the menu closed: a capture that Esc gave up comes back
// ---- shared by the back ends
// cursor_art.c: the pointer's image at size x size pixels (0xAARRGGBB, straight alpha, rows from the top) and its
// hotspot (*hot, the same in x and y). Pure.
void cursor_art(uint32_t *argb, int size, int *hot);
// ---- what input.c provides the back end
int input_cursor_allowed(void); // no text entry or menu open, the mouse camera on, input not disabled
+1
View File
@@ -783,6 +783,7 @@ int probe_ipc(void) {
return 0;
}
ldr_load(rt_config()->exefs);
hostfn_count(1); // the stats line reads the host-call count
kern_run_on_guest_main(guest_main);
probe_unknown_service();
RtStats s = rt_stats();
+47
View File
@@ -246,6 +246,44 @@ static void wake_sync_waiters(void) {
}
}
// ---- a short poll before a sleep
// Most guest waits and waits for a virtual core end within microseconds. The thread polls for TOTK_KSPIN_US (default
// 20) with G released before it sleeps on its condition variable: a wake in that time costs no OS wake-up (4-12 us
// on a desktop Ryzen, more from a deep C-state) and the waker's signal finds nobody asleep. Off with fewer than 6 logical
// processors, where the poll would take a CPU that a running thread needs; TOTK_KSPIN_US=0 turns it off. Savage Wind
// Recomp polls the same way before it parks (runtime/ksync.c cond_wait). TOTK_KSPIN_US=ab polls 20 us in the
// B blocks of TOTK_AB only.
static int64_t g_kspin_ns = -1;
static int g_kspin_ab;
static uint64_t kspin_ns(void) {
int64_t v = __atomic_load_n(&g_kspin_ns, __ATOMIC_RELAXED);
if (v < 0) {
const char *e = cfg_get("TOTK_KSPIN_US");
g_kspin_ab = e && !strcmp(e, "ab");
long long us = e && *e && !g_kspin_ab ? atoll(e) : 20;
uint32_t cpus = plat_cpu_usable();
v = us <= 0 || cpus < 6 ? 0 : us * 1000;
__atomic_store_n(&g_kspin_ns, v, __ATOMIC_RELAXED);
rt_log("kernel: waits poll %lld us before they sleep%s (TOTK_KSPIN_US, %u logical processors)",
(long long)(v / 1000), g_kspin_ab ? " in the B blocks" : "", cpus);
}
return v && (!g_kspin_ab || rt_ab()) ? (uint64_t)v : 0;
}
// Holding G: releases it and polls *p until it reaches at_least or ns pass, then takes G again. 1 if it was reached.
static int kspin(const int *p, int at_least, uint64_t ns) {
uint64_t end = kern_now_ns() + ns;
int ok = 0;
unlock();
for (unsigned i = 1; !(ok = __atomic_load_n(p, __ATOMIC_ACQUIRE) >= at_least); i++) {
plat_pause();
if (!(i & 31) && kern_now_ns() >= end) break;
}
lock();
return ok;
}
// SleepConditionVariableSRW counts milliseconds: round up (late is legal, early is not) and re-check the deadline
// against the performance counter, because the wait may also end early.
static uint32_t block_inner(RtThread *t, int kind, int64_t ns) {
@@ -262,6 +300,8 @@ static uint32_t block_inner(RtThread *t, int kind, int64_t ns) {
uint64_t now = kern_now_ns();
int forever = ns < 0 || (uint64_t)ns > UINT64_MAX - now;
uint64_t deadline = forever ? 0 : now + (uint64_t)ns;
uint64_t poll = kspin_ns();
if (poll) kspin(&t->woke, 1, forever || poll < (uint64_t)ns ? poll : (uint64_t)ns);
while (!t->woke) {
if (forever) {
plat_cond_wait(&t->cv, &G);
@@ -991,6 +1031,9 @@ static void core_take(RtThread *t) {
while (__atomic_load_n(&t->cur_core, __ATOMIC_RELAXED) < 0 && kern_now_ns() < end) plat_pause();
lock();
if (t->cur_core >= 0) g_ap_spin_ok++;
} else if (t->cur_core < 0) { // a holder may block or yield any moment (kspin_ns)
uint64_t poll = kspin_ns();
if (poll) kspin(&t->cur_core, 0, poll);
}
int sampled = 0;
while (t->cur_core < 0) {
@@ -1402,6 +1445,7 @@ static RtThread *thr_new(int prio, int core, uint64_t mask) {
Cpu *c = plat_aligned_alloc(sizeof(Cpu), 64);
if (!t || !c) rt_fatal("kernel: thread allocation");
memset(c, 0, sizeof *c);
rt_fpcr_threshold(c);
plat_cond_init(&t->cv);
t->prio = prio;
t->core = core;
@@ -1518,6 +1562,7 @@ uint32_t kern_main_thread(Cpu **cpu) {
t->cpu->sp = t->stack_top;
t->tid = plat_thread_id();
perf_main_thread();
perf_core_thread(PCORE_MAIN);
PlatIntr *self = ap_open_self();
lock();
t->hthr = self;
@@ -2215,6 +2260,7 @@ static void fiber_switch_to(RtThread *t, const Cpu *from, Fiber *to) {
dst->tpidr = from->tpidr;
dst->tpidrro = from->tpidrro;
dst->fpcr = from->fpcr;
rt_fpcr_threshold(dst); // fiber_resumed syncs MXCSR on the other side; the threshold follows fpcr at once
dst->fpsr = from->fpsr;
dst->excl_addr = ~0ull;
dst->thread = t;
@@ -2247,6 +2293,7 @@ void kern_fiber_initialize(Cpu *c) {
plat_fiber_free(f->host);
}
memset(f->cpu, 0, sizeof *f->cpu);
rt_fpcr_threshold(f->cpu);
f->cpu->excl_addr = ~0ull;
f->addr = addr;
f->func = c->x[1];
+113
View File
@@ -10,6 +10,7 @@
#include "cfg.h"
#include "host.h"
#include "kernel.h"
#include "layout.h"
#include "platform/platform.h"
#define STACK 0x10000
@@ -682,6 +683,25 @@ static int cmp_u64(const void *a, const void *b) {
return x < y ? -1 : x > y;
}
// A refused commit waits and retries (memory.c commit, oom.c rt_oom_wait): three forced refusals, then the page maps.
static void test_oom_commit(void) {
const RtGuestLayout *g = mem_layout();
uint64_t a = g->alias.base + g->alias.size - 0x10000;
rt_oom_force(RT_OOM_COMMIT, 3);
uint64_t t0 = plat_now_ms();
uint32_t r = mem_map_physical(a, 0x1000);
uint64_t ms = plat_now_ms() - t0;
CHECK(r == 0 && rt_oom_forced_left(RT_OOM_COMMIT) == 0 && ms >= 20,
"MapPhysicalMemory after 3 refused commits: result %#x, %d refusals left, %llu ms", r,
rt_oom_forced_left(RT_OOM_COMMIT), (unsigned long long)ms);
rt_oom_force(RT_OOM_COMMIT, 0);
if (r == 0) {
*(volatile uint64_t *)(uintptr_t)a = 1;
CHECK(mem_unmap_physical(a, 0x1000) == 0, "UnmapPhysicalMemory after the refused commits");
}
printf("oom: commit waited %llu ms through 3 refusals\n", (unsigned long long)ms);
}
static void test_platform(void) {
PlatThread *t = plat_thread_new(plat_worker, (void *)(intptr_t)1, "probe-kernel plat");
CHECK(t, "plat_thread_new");
@@ -719,6 +739,90 @@ static void test_platform(void) {
CHECK(over[N / 2] < 500000, "plat_sleep_ns(1 ms) median overshoot %.3f ms (want < 0.5)", over[N / 2] / 1e6);
}
// ---- crash report (main.c crash_handler): a child faults on a guest thread with known values in rbx and x19; the
// report must hold the host registers, the faulting instruction's bytes and the memory both values point at.
#define CRASH_CHILD_ENV "TOTK_PROBE_KERNEL_CRASH_CHILD"
static uint64_t crash_host_words[6] = {0, 0, 0x5ca1ab1e0c0de001ull, 0, 0, 0};
static uint64_t crash_guest_words[6] = {0, 0, 0x5ca1ab1e0c0de002ull, 0, 0, 0};
// The compound shape note (main.c crash_compound_shape, 1.2.1): a shape of 3 records whose child 0 has no shape, child
// 1 has one but its live bit is clear, child 2 is live and filled.
static uint64_t crash_shape[0x1a0 / 8], crash_records[3 * 0x78 / 8], crash_child_shape;
static void crash_shape_fill(Cpu *c) {
uint8_t *s = (uint8_t *)crash_shape, *r = (uint8_t *)crash_records;
static const char *const names[3] = {"PartA", "PartB", "PartC"};
for (int i = 0; i < 3; i++) {
uint64_t name = (uint64_t)(uintptr_t)(r + i * 0x78 + 0x14);
memcpy(r + i * 0x78 + 8, &name, 8);
memcpy(r + i * 0x78 + 0x14, names[i], strlen(names[i]) + 1);
}
uint64_t child = (uint64_t)(uintptr_t)&crash_child_shape, vt = MOD_MAIN_BASE + 0x45e1218;
uint64_t rec = (uint64_t)(uintptr_t)crash_records;
uint32_t n = 3, bits = 0x5;
memcpy(r + 0x78, &child, 8);
memcpy(r + 2 * 0x78, &child, 8);
memcpy(s, &vt, 8);
memcpy(s + 0x78, &n, 4);
memcpy(s + 0x80, &rec, 8);
memcpy(s + 0x88, &bits, 4);
memcpy(s + 0x190, &n, 4);
c->x[20] = (uint64_t)(uintptr_t)crash_shape;
c->x[21] = 0x1000038;
c->x[30] = MOD_MAIN_BASE + 0xe056d4;
}
static void crash_child_main(Cpu *c) {
const char *mode = getenv(CRASH_CHILD_ENV);
if (mode && !strcmp(mode, "shape")) crash_shape_fill(c);
c->x[19] = (uint64_t)(uintptr_t)&crash_guest_words[2];
c->sp -= 0x100; // a frame on the guest stack, as a crashing function has
__asm__ volatile("movq %0, %%rbx\n\tmovq 0x10, %%rax" : : "r"(&crash_host_words[2]) : "rax", "rbx", "memory");
}
// The report line that holds needle, NULL when none does
static const char *report_line(const char *out, const char *needle, char *line, size_t cap) {
const char *p = strstr(out, needle);
if (!p) return NULL;
while (p > out && p[-1] != '\n') p--;
size_t n = strcspn(p, "\r\n");
snprintf(line, cap, "%.*s", (int)(n < cap ? n : cap - 1), p);
return line;
}
static void test_crash_report(void) {
static char out[65536];
char line[512], want[64];
int code = plat_run_self(CRASH_CHILD_ENV, "1", out, sizeof out, 30000);
CHECK(code == 71 && strstr(out, "FATAL: access violation") && strstr(out, "reading address 0x10"),
"crash child: exit code %d, no access violation at 0x10 reported", code);
snprintf(want, sizeof want, "rbx %016llx", (unsigned long long)(uintptr_t)&crash_host_words[2]);
CHECK(strstr(out, want), "crash report: no host register line with %s", want);
CHECK(report_line(out, "host code at rip-", line, sizeof line) && strstr(line, "| 48 8b 04 25 10 00 00 00"),
"crash report: the faulting instruction's bytes are missing");
const char *h = report_line(out, "*5ca1ab1e0c0de001", line, sizeof line);
CHECK(h && strstr(h, "rbx"), "crash report: no memory line for rbx (%s)", h ? h : "none");
const char *g = report_line(out, "*5ca1ab1e0c0de002", line, sizeof line);
CHECK(g && strstr(g, "x19"), "crash report: no memory line for x19 (%s)", g ? g : "none");
CHECK(strstr(out, "guest stack sp+0x00:"), "crash report: no guest stack words");
CHECK(report_line(out, " memory commit: this process ", line, sizeof line) && !strstr(line, "process -1 MB"),
"crash report: no memory commit line");
printf("crash report: exit code %d, host registers, code bytes, memory at rbx and x19, guest stack, commit %s\n", code,
failures ? "checked with failures" : "present");
if (rt_game_version != 121) return;
code = plat_run_self(CRASH_CHILD_ENV, "shape", out, sizeof out, 30000);
CHECK(report_line(out, " compound shape ", line, sizeof line) && strstr(line, "3 children, 3 records") &&
strstr(line, "live bits: 00000005"),
"crash report: no compound shape line (%s)", strstr(out, "compound shape") ? line : "none");
CHECK(report_line(out, "child 0:", line, sizeof line) && strstr(line, "shape 0 \"PartA\""),
"crash report: child 0 should have no shape (%s)", line);
CHECK(report_line(out, "child 1:", line, sizeof line) && strstr(line, "(off) \"PartB\""),
"crash report: child 1 should be off (%s)", line);
CHECK(report_line(out, "child 2:", line, sizeof line) && !strstr(line, "(off)") && strstr(line, "\"PartC\""),
"crash report: child 2 should be live (%s)", line);
printf("crash report, compound shape: exit code %d, %s\n", code, failures ? "checked with failures" : "children listed");
}
int probe_kernel(void) {
a_lock_waiter = hostfn_register("probe-kernel lock waiter", lock_waiter);
a_cv_waiter = hostfn_register("probe-kernel condvar waiter", cv_waiter);
@@ -742,7 +846,16 @@ int probe_kernel(void) {
printf("probe-kernel: FAILED (no PLT stub for %s in main)\n", TICK_SYMBOL);
return 1;
}
const char *child = getenv(CRASH_CHILD_ENV);
if (child && *child) {
kern_run_on_guest_main(crash_child_main);
printf("crash child: returned without a fault\n");
return 1;
}
test_platform();
test_oom_commit();
test_crash_report();
hostfn_count(1); // test_lifted_ticks and the stats line read the host-call count
kern_run_on_guest_main(probe_main);
RtStats s = rt_stats();
printf("stats: %llu SVCs, %llu threads, %llu host calls\n", (unsigned long long)s.svcs,
+1
View File
@@ -643,6 +643,7 @@ static void check_got(void) {
hostfn_seal();
Cpu *c = plat_aligned_alloc(sizeof(Cpu), 64);
memset(c, 0, sizeof *c);
rt_fpcr_threshold(c);
uint8_t *stack = mem_kernel_alloc(0x10000, 16);
c->sp = (uint64_t)(uintptr_t)stack + 0x10000;
c->excl_addr = ~0ull;
+165 -9
View File
@@ -21,7 +21,8 @@
//
// Also here: the runtime services the generated code calls besides rt_dispatch and rt_svc (rt_brk, rt_udf,
// rt_ticks, the function profiler hook), logging, SHA-256, and the crash reporter (a vectored exception handler that
// names the lifted function, the guest registers and the guest frame-pointer chain).
// names the lifted function, the host and guest registers, the machine code around the fault, the guest frame-pointer
// chain and the memory the registers point at).
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
@@ -29,6 +30,7 @@
#include "cfg.h"
#include "host.h"
#include "layout.h"
#include "platform/platform.h"
#include "sha256.h"
@@ -159,6 +161,16 @@ const RtConfig *rt_config(void) { return &cfg; }
volatile int g_fnprof = 0; // resolution.c switches it on until the game sets its render size (res_fn_hit)
void fnprof_hit(uint64_t addr, uint64_t lr, uint64_t x0) { (void)lr, res_fn_hit(addr, x0); }
// cpu.h RT_FPCR_NOTE: diagnostic only, the write itself takes effect as before (runtime/simd.h slow paths).
void rt_fpcr_rmode(uint64_t fpcr, uint64_t lr) {
static int seen;
if (__atomic_exchange_n(&seen, 1, __ATOMIC_RELAXED)) return;
static const char *const mode[] = {"nearest", "+inf", "-inf", "zero"};
char at[128];
rt_log("fpcr: guest code set rounding mode %s (FPCR %#llx), in a function called from %s", mode[(fpcr >> 22) & 3],
(unsigned long long)fpcr, ldr_describe(lr, at, sizeof at));
}
void rt_brk(Cpu *c, uint32_t imm, uint64_t pc) {
char at[128], lr[128];
rt_fatal("brk #%#x at %s (lr %s)", imm, ldr_describe(pc, at, sizeof at), ldr_describe(c->x[30], lr, sizeof lr));
@@ -188,8 +200,8 @@ static int by_host(const void *a, const void *b) {
}
// The lifted function that contains a host code address, by the nearest function start below it; 0 outside the lifted
// code.
static uint64_t lifted_function(uintptr_t rip) {
// code. *host, when given, gets the function's host start.
static uint64_t lifted_function_at(uintptr_t rip, uintptr_t *host) {
if (!fn_rev) {
static RtModule mods[MAX_MODULES];
int n = rt_linked_modules(mods, MAX_MODULES);
@@ -211,11 +223,113 @@ static uint64_t lifted_function(uintptr_t rip) {
if (fn_rev[mid].host <= rip) lo = mid + 1;
else hi = mid;
}
if (lo && host) *host = fn_rev[lo - 1].host;
return lo ? fn_rev[lo - 1].guest : 0;
}
static uint64_t lifted_function(uintptr_t rip) { return lifted_function_at(rip, NULL); }
static int readable(uint64_t a, uint64_t n) { return plat_mem_readable(a, n); }
// The machine code around the faulting instruction: 32 bytes before rip, then 32 from rip. The user's totk.exe is built
// on their machine, so its offsets need not match ours; these bytes and the offset into the lifted function find the
// same instruction in a local build of that version, and with it the guest instruction and the operands' registers.
static void crash_code_bytes(uintptr_t rip) {
uint8_t b[64];
int before = 32;
if (!plat_mem_read(rip - 32, b, sizeof b)) {
before = 0; // rip near the start of a mapping: the bytes from rip only
if (!plat_mem_read(rip, b + 32, 32)) return;
}
rt_logf(" host code at rip-%d:", before);
for (int i = 32 - before; i < 64; i++) rt_logf("%s%02x", i == 32 ? " | " : " ", b[i]);
rt_log_raw("\n", 1);
}
// 64 bytes around each register value that points at readable memory, as 8-byte words from 16 bytes below the value,
// the word holding the value marked with *. Host and guest registers that land on the same window share a line. A
// guest pointer is a host address here (the guest address space is mapped one to one), so both kinds are read alike.
// The guest registers come from the Cpu, which the lifted code writes back only at calls and returns, so the host
// registers carry the faulting function's live values.
typedef struct { uint64_t start; char names[56]; uint64_t w[8]; } CrashWin;
static void crash_win_add(CrashWin *wins, int *n, int cap, const char *name, uint64_t v) {
if (v < 0x10000 || v > 0x00007fffffffffffull) return; // small integers and non-canonical values
uint64_t start = (v & ~7ull) - 16;
for (int i = 0; i < *n; i++)
if (wins[i].start == start) {
size_t len = strlen(wins[i].names);
if (len + strlen(name) + 2 < sizeof wins[i].names)
snprintf(wins[i].names + len, sizeof wins[i].names - len, " %s", name);
return;
}
if (*n == cap || !plat_mem_read(start, wins[*n].w, sizeof wins[*n].w)) return;
wins[*n].start = start;
snprintf(wins[*n].names, sizeof wins[*n].names, "%s", name);
(*n)++;
}
static void crash_memory(const PlatCrash *x, const Cpu *c) {
static CrashWin wins[64]; // the crash handler runs once (busy flag), so static storage needs no allocation
static const char *const gpr_names[16] = PLAT_GPR_NAMES;
int n = 0;
char name[8];
if (x->has_regs)
for (int i = 0; i < 16; i++) crash_win_add(wins, &n, 64, gpr_names[i], x->gpr[i]);
if (c) {
for (int i = 0; i < 31; i++) snprintf(name, sizeof name, "x%d", i), crash_win_add(wins, &n, 64, name, c->x[i]);
crash_win_add(wins, &n, 64, "sp", c->sp);
}
if (!n) return;
rt_logf(" memory at the register values (8-byte words from value-16, * the value's word):\n");
for (int i = 0; i < n; i++) {
rt_logf(" %016llx %-12s", (unsigned long long)wins[i].start, wins[i].names);
for (int k = 0; k < 8; k++) rt_logf(" %s%016llx", k == 2 ? "*" : "", (unsigned long long)wins[i].w[k]);
rt_log_raw("\n", 1);
}
// The guest stack above sp: the crashing function's spills and locals, and the callers' saved registers
uint64_t s[16];
if (c && plat_mem_read(c->sp, s, sizeof s))
for (int i = 0; i < 16; i += 4)
rt_logf(" guest stack sp+0x%02x: %016llx %016llx %016llx %016llx\n", i * 8, (unsigned long long)s[i],
(unsigned long long)s[i + 1], (unsigned long long)s[i + 2], (unsigned long long)s[i + 3]);
}
// 1.2.1: a fault right after a compound physics shape was asked for its first child's material (the call returning to
// main+0xe056d4 reads the result without a check). The shape (vtable main+0x45e1218, built by main+0x7359fc) is in x20
// there. It returns null when no child slot is both live (bitmap at +0x88) and filled (shape pointer at +0 of the
// 0x78-byte records at +0x80). Every child is filled when the shape is built, so the list tells children removed later
// (null shape) from children switched off (clear bit), and the names name the object. Runs only in the crash handler.
static void crash_compound_shape(const Cpu *c) {
if (!c || rt_game_version != 121 || c->x[30] != MOD_MAIN_BASE + 0xe056d4) return;
uint64_t shape = c->x[20];
if (!readable(shape, 0x1a0) || LD64(shape) != MOD_MAIN_BASE + 0x45e1218) {
rt_logf(" compound shape: x20 %#llx is not one\n", (unsigned long long)shape);
return;
}
uint32_t children = LD32(shape + 0x190), records = LD32(shape + 0x78);
uint64_t rec = LD64(shape + 0x80);
rt_logf(" compound shape %#llx (holder %#llx): %u children, %u records at %#llx, live bits:",
(unsigned long long)shape, (unsigned long long)(c->x[21] - 0x38), children, records,
(unsigned long long)rec);
uint32_t n = children > 0x400 ? 0x400 : children; // the bitmap holds 0x400 bits
for (uint32_t w = 0; w < (n + 31) / 32; w++) rt_logf(" %08x", LD32(shape + 0x88 + 4 * w));
rt_log_raw("\n", 1);
if (records > 0x400 || !readable(rec, (uint64_t)records * 0x78)) return;
for (uint32_t i = 0; i < records; i++) {
uint64_t r = rec + (uint64_t)i * 0x78, child = LD64(r), s = LD64(r + 8);
char name[33];
size_t k = 0;
for (; k < 32 && readable(s + k, 1); k++) {
name[k] = *(const char *)(uintptr_t)(s + k);
if (!name[k]) break;
}
name[k] = 0;
int live = LD32(shape + 0x88 + 4 * (i >> 5)) >> (i & 31) & 1;
rt_logf(" child %3u: shape %#llx%s \"%s\"\n", i, (unsigned long long)child, live ? "" : " (off)", name);
}
}
static void crash_handler(const PlatCrash *x) {
static volatile int32_t busy;
if (plat_xchg32(&busy, 1)) plat_exit(71);
@@ -228,23 +342,48 @@ static void crash_handler(const PlatCrash *x) {
rt_logf(", %s address %#llx", x->access == 1 ? "writing" : x->access == 8 ? "executing" : "reading",
(unsigned long long)x->addr);
rt_log_raw("\n", 1);
uint64_t f = x->module ? 0 : lifted_function(rip);
if (f) rt_logf(" in lifted function f_%llx = %s\n", (unsigned long long)f, ldr_describe(f, d, sizeof d));
// abort and invalid parameter: where the host code called it; a fault in another module: the host calls that led
// into it
if (x->cxx_type)
rt_logf(" C++ type %s%s%s\n", x->cxx_type, x->cxx_what ? ", what(): " : "", x->cxx_what ? x->cxx_what : "");
// A driver that runs out of memory faults or throws (std::bad_alloc) instead of returning an error: the commit
// tells that apart from a driver bug, as rt_fatal's line does for our own failed allocations.
double proc, used, limit;
plat_commit_info(&proc, &used, &limit);
rt_logf(" memory commit: this process %.0f MB, the system %.0f of %.0f MB\n", proc, used, limit);
uintptr_t fstart = 0;
uint64_t f = x->module ? 0 : lifted_function_at(rip, &fstart);
if (f)
rt_logf(" in lifted function f_%llx = %s, host +%#llx into it\n", (unsigned long long)f,
ldr_describe(f, d, sizeof d), (unsigned long long)(rip - fstart));
if (x->has_regs) {
static const char *const gpr_names[16] = PLAT_GPR_NAMES;
for (int i = 0; i < 16; i++)
rt_logf(" %-3s %016llx%s", gpr_names[i], (unsigned long long)x->gpr[i], i % 4 == 3 ? "\n" : "");
crash_code_bytes(rip);
}
// abort and invalid parameter: where the host code called it; a fault in another module: the driver code and the
// host calls that led into it
uintptr_t exe = plat_exe_base();
for (int i = 0; i < x->nframes; i++) {
const char *m = x->frame_module[i];
if (m) {
rt_logf(" host frame %2d: %s+%#llx\n", i, *m ? m : "outside every module",
(unsigned long long)x->frame_offset[i]);
continue;
}
uint64_t g = lifted_function(x->frames[i]);
rt_logf(" host frame %2d: totk+%#llx", i, (unsigned long long)(x->frames[i] - exe));
if (g) rt_logf(" in lifted function f_%llx", (unsigned long long)g);
rt_log_raw("\n", 1);
}
if (x->frames_left_out) rt_logf(" (%d frames in other modules left out)\n", x->frames_left_out);
Cpu *c = kern_cpu();
if (c) {
for (int i = 0; i < 31; i++)
rt_logf(" x%-2d %016llx%s", i, (unsigned long long)c->x[i], i % 4 == 3 ? "\n" : "");
rt_logf(" sp %016llx\n pc %016llx lr %s\n", (unsigned long long)c->sp, (unsigned long long)c->pc,
ldr_describe(c->x[30], d, sizeof d));
// pc: the guest code running, or the host function a guest call entered (its name tells the host call apart)
rt_logf(" sp %016llx\n pc %016llx", (unsigned long long)c->sp, (unsigned long long)c->pc);
if (hostfn_name(c->pc)) rt_logf(" (%s)", ldr_describe(c->pc, d, sizeof d));
rt_logf(" lr %s\n", ldr_describe(c->x[30], d, sizeof d));
uint64_t fp = c->x[29];
for (int n = 0; n < 32 && fp && !(fp & 7) && readable(fp, 16); n++) {
uint64_t next = LD64(fp), lr = LD64(fp + 8);
@@ -255,6 +394,8 @@ static void crash_handler(const PlatCrash *x) {
} else {
rt_logf(" not on a guest thread\n");
}
crash_memory(x, c);
crash_compound_shape(c);
kern_free_cores_note();
fflush(stderr);
plat_exit(71);
@@ -427,6 +568,7 @@ static int selftest(void) {
CHECK(s.functions > 0, "no functions linked");
Cpu *c = plat_aligned_alloc(sizeof(Cpu), 64);
memset(c, 0, sizeof *c);
rt_fpcr_threshold(c);
uint64_t r = rt_call(c, probe, 1, (uint64_t)7);
CHECK(rt_dispatch_lookup(probe) && r == 42 && host_probe_hits == 1 && c->x[30] == 0,
"a registered host function does not dispatch through rt_call");
@@ -521,6 +663,20 @@ int main(int argc, char **argv) {
// Timed guest waits (kernel.c block_inner) sleep in whole milliseconds; at the default 15.6 ms tick a 1 ms wait
// costs a full tick. TOTK_TIMER_1MS=0 keeps the default for comparison.
if (cfg_bool("TOTK_TIMER_1MS")) plat_timer_1ms();
int power = plat_power_full();
if (power >= 0)
rt_log("power: %s", power ? "OS power throttling off (EcoQoS, timer resolution in the background)"
: "OS power throttling: the opt-out was refused");
// Hybrid CPUs: the threads every frame goes through prefer the P-cores (perf_core_thread). TOTK_PCORE=0: off.
if (rt_knob("PCORE", "1")) {
char why[160];
int n = plat_perf_core_setup(why, sizeof why);
if (n) rt_log("power: %s; the game's main and render threads and the vk replay, recorder and present threads "
"prefer the %d P-core ones (TOTK_PCORE=0 turns it off)", why, n);
else rt_log("power: %s: no P-core preference", why);
} else {
rt_log("power: TOTK_PCORE=0: no P-core preference");
}
cfg = (RtConfig){.exefs = rt_default_exefs, .romfs = rt_default_romfs, .profile = rt_default_profile,
.gpu = cfg_get("TOTK_GPU")};
portable_paths(&cfg); // command-line paths below still win
+24 -6
View File
@@ -175,12 +175,29 @@ static int thaw_fault(uint64_t a) {
}
// Mapping changes thaw the regions they touch and run under frz_lock, so no freeze sees them half done.
static void commit(uint64_t a, uint64_t n, const char *what) {
typedef struct { uint64_t a, n; } CommitTry;
static int commit_try(void *arg) {
const CommitTry *t = arg;
if (rt_oom_forced(RT_OOM_COMMIT)) return 0;
plat_mutex_lock(&frz_lock);
thaw_range_locked(a, n, 3);
int ok = !n || plat_mem_commit(a, n);
thaw_range_locked(t->a, t->n, 3);
int ok = !t->n || plat_mem_commit(t->a, t->n);
plat_mutex_unlock(&frz_lock);
if (!ok) rt_fatal("memory: commit %s %#llx+%#llx failed", what, (unsigned long long)a, (unsigned long long)n);
return ok;
}
// A refused commit waits for memory (rt_oom_wait): the system's commit ran out for a moment (a page file growing, the
// driver's device memory charged to this process), and the Vulkan back end gives memory back at its next submit. The
// wait holds the caller's lock (lock or klock: only memory SVCs and kernel-region allocations wait behind it) but not
// frz_lock, which write faults on frozen regions need. Every caller waits, the boot's ones too: there a refusal has the
// same cause, and the wait only delays an end the game would reach anyway.
static void commit(uint64_t a, uint64_t n, const char *what) {
CommitTry t = {a, n};
if (commit_try(&t)) return;
char w[64];
snprintf(w, sizeof w, "memory: commit of %s at %#llx", what, (unsigned long long)a);
if (!rt_oom_wait(w, n, commit_try, &t))
rt_fatal("memory: commit %s %#llx+%#llx failed", what, (unsigned long long)a, (unsigned long long)n);
}
static void decommit(uint64_t a, uint64_t n, const char *what) {
@@ -246,11 +263,12 @@ void mem_init(void) {
L = (RtGuestLayout){
.aslr = {low, RT_ADDRESS_LIMIT - low},
.stack = {low + STACK_OFF, STACK_SIZE},
.heap = {low + HEAP_OFF, HEAP_CAP + res_tex_heap_extra_bytes()},
.heap = {low + HEAP_OFF, HEAP_CAP + res_tex_heap_extra_bytes() + res_grass_heap_extra_bytes()},
.alias = {alias, ALIAS_SIZE},
.kernel = {RT_KERNEL_BASE, RT_KERNEL_SIZE},
.hostfn = {RT_HOSTFN_BASE, RT_HOSTFN_SIZE},
.pool = pool ? strtoull(pool, NULL, 0) : DEFAULT_POOL + res_heap_extra_bytes() + res_tex_heap_extra_bytes(),
.pool = pool ? strtoull(pool, NULL, 0)
: DEFAULT_POOL + res_heap_extra_bytes() + res_tex_heap_extra_bytes() + res_grass_heap_extra_bytes(),
};
if (!L.pool) rt_fatal("memory: TOTK_APP_POOL is 0");
}
+120
View File
@@ -0,0 +1,120 @@
// Inline equality compares and copies for the replay's per-draw paths (TOTK_MEMX=0|1|check, default 1).
// ucrtbase's memcmp walks 8 bytes a step with a call around it, and the replay compares and copies a few hundred
// bytes at a time thousands of times a frame (pipeline states, shader answers, descriptor signatures): MX_EQ and
// MX_CPY do the same work with 32-byte loads in the caller, so a constant size unrolls and a variable one loops
// without a call. MX_EQ answers only equal or not, never an order. MX_CPY is memcpy: source and destination never
// overlap. 1: the inline forms. 0: memcmp/memcpy as before. check: memcmp/memcpy, and every MX_EQ also runs the
// inline compare and logs a different answer (vk_memx_bad). The mode is read once per process (vk_memx_init); calls
// before that take the off path.
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <string.h>
extern int vk_memx_mode; // -1 before vk_memx_init, 0 off, 1 on, 2 check
void vk_memx_init(void);
void vk_memx_bad(const char *file, int line, size_t n);
typedef uint64_t mx_v4 __attribute__((vector_size(32), aligned(1)));
typedef uint64_t mx_v2 __attribute__((vector_size(16), aligned(1)));
static inline __attribute__((always_inline)) mx_v4 mx_ld4(const uint8_t *p) {
mx_v4 v;
__builtin_memcpy(&v, p, 32);
return v;
}
static inline __attribute__((always_inline)) mx_v2 mx_ld2(const uint8_t *p) {
mx_v2 v;
__builtin_memcpy(&v, p, 16);
return v;
}
static inline __attribute__((always_inline)) uint64_t mx_ld1(const uint8_t *p) {
uint64_t v;
__builtin_memcpy(&v, p, 8);
return v;
}
static inline __attribute__((always_inline)) uint32_t mx_ldw(const uint8_t *p) {
uint32_t v;
__builtin_memcpy(&v, p, 4);
return v;
}
// 1 when the n bytes at a and b are equal. Sizes of 32 and up compare in 128-byte steps with one branch each, the
// last partial step as an overlapping 32-byte load from the end; smaller sizes take two overlapping loads.
static inline __attribute__((always_inline)) int mx_eq(const void *a, const void *b, size_t n) {
const uint8_t *p = a, *q = b;
if (n >= 32) {
size_t i = 0;
for (; i + 128 <= n; i += 128) {
mx_v4 d = (mx_ld4(p + i) ^ mx_ld4(q + i)) | (mx_ld4(p + i + 32) ^ mx_ld4(q + i + 32)) |
(mx_ld4(p + i + 64) ^ mx_ld4(q + i + 64)) | (mx_ld4(p + i + 96) ^ mx_ld4(q + i + 96));
if (d[0] | d[1] | d[2] | d[3]) return 0;
}
mx_v4 d = {0, 0, 0, 0};
for (; i + 32 <= n; i += 32) d |= mx_ld4(p + i) ^ mx_ld4(q + i);
if (i < n) d |= mx_ld4(p + n - 32) ^ mx_ld4(q + n - 32);
return !(d[0] | d[1] | d[2] | d[3]);
}
if (n >= 16) {
mx_v2 d = (mx_ld2(p) ^ mx_ld2(q)) | (mx_ld2(p + n - 16) ^ mx_ld2(q + n - 16));
return !(d[0] | d[1]);
}
if (n >= 8) return !((mx_ld1(p) ^ mx_ld1(q)) | (mx_ld1(p + n - 8) ^ mx_ld1(q + n - 8)));
if (n >= 4) return !((mx_ldw(p) ^ mx_ldw(q)) | (mx_ldw(p + n - 4) ^ mx_ldw(q + n - 4)));
for (size_t i = 0; i < n; i++)
if (p[i] != q[i]) return 0;
return 1;
}
// memcpy of n bytes between buffers that do not overlap: 32-byte moves, the last partial one overlapping the one
// before it (it rewrites bytes it already wrote with the same values).
static inline __attribute__((always_inline)) void mx_cpy(void *d, const void *s, size_t n) {
uint8_t *p = d;
const uint8_t *q = s;
if (n >= 32) {
mx_v4 last = mx_ld4(q + n - 32);
for (size_t i = 0; i + 32 <= n; i += 32) {
__asm__("" : "+r"(p)); // opaque to the loop-idiom pass, which would turn the loop back into a memcpy call
mx_v4 v = mx_ld4(q + i);
__builtin_memcpy(p + i, &v, 32);
}
__builtin_memcpy(p + n - 32, &last, 32);
return;
}
if (n >= 16) {
mx_v2 x = mx_ld2(q), y = mx_ld2(q + n - 16);
__builtin_memcpy(p, &x, 16), __builtin_memcpy(p + n - 16, &y, 16);
return;
}
if (n >= 8) {
uint64_t x = mx_ld1(q), y = mx_ld1(q + n - 8);
__builtin_memcpy(p, &x, 8), __builtin_memcpy(p + n - 8, &y, 8);
return;
}
if (n >= 4) {
uint32_t x = mx_ldw(q), y = mx_ldw(q + n - 4);
__builtin_memcpy(p, &x, 4), __builtin_memcpy(p + n - 4, &y, 4);
return;
}
for (size_t i = 0; i < n; i++) p[i] = q[i];
}
static inline __attribute__((always_inline)) int mx_eq_at(const void *a, const void *b, size_t n, const char *file,
int line) {
if (vk_memx_mode == 1) return mx_eq(a, b, n);
int r = !memcmp(a, b, n);
if (vk_memx_mode == 2 && mx_eq(a, b, n) != r) vk_memx_bad(file, line, n);
return r;
}
static inline __attribute__((always_inline)) void mx_cpy_sw(void *d, const void *s, size_t n) {
if (vk_memx_mode == 1) mx_cpy(d, s, n);
else memcpy(d, s, n);
}
#define MX_EQ(a, b, n) mx_eq_at((a), (b), (n), __FILE__, __LINE__)
#define MX_NE(a, b, n) (!MX_EQ(a, b, n))
#define MX_CPY(d, s, n) mx_cpy_sw((d), (s), (n))
+114 -11
View File
@@ -20,6 +20,14 @@
//
// Only while keyboard and mouse are the device used last (input_kbm_last): a gamepad player sees the menus unchanged.
// TOTK_MENU_CURSOR (live, default 1): 0 leaves the menus to the keys and the pad.
//
// With TOTK_MAP_CLICK=1 (live, default 0: one key per controller button) input.c makes the left mouse button A on the
// map (menu_cursor_map), so a click acts at the map's cursor. The map is found only while TOTK_MENU_CURSOR is on.
//
// The same read of the open screens tells input.c when a dialogue box is on screen (menu_cursor_dialogue), where the
// left mouse button presses B (it advances the text and skips its scroll) and the right one A, instead of their
// bindings (Y by default, which opens the dialogue log, and ZL). TOTK_DIALOGUE_CLICK (live, default 0: one key per
// controller button) switches that on its own: it does not need the pointer.
#include <math.h>
#include <stdint.h>
#include <stdio.h>
@@ -50,6 +58,10 @@ enum { DI_PROJ = 0x10, DI_VIEW = 0x50, LYT_ROOT = 0x18, LYT_SIZE = 0x28, LYT_NAM
// nn::ui2d::Pane
enum { PANE_PARENT = 0x18, PANE_SIZE = 0x50, PANE_FLAG = 0x58, PANE_ALPHA = 0x59, PANE_BASE = 0x5b, PANE_MTX = 0x70 };
// How far the pointer moves, in layout units (1280 x 720 across the picture: 1.5 window pixels at 1080p, 3 at 4K),
// before it takes back a focus that the wheel, the keys or the pad moved away from it (menu_cursor_poll)
#define DEAD_ZONE 6.0f
static uint64_t main_lo, main_hi;
static int ok_at(uint64_t p, uint64_t n) {
@@ -166,16 +178,24 @@ static void layout_name(uint64_t scr, char *out, size_t n) {
// 1.4.3 constructor keeps). The map has no focus box, so it is found here: the Purah Pad's frame (AppMenuOverlay_00)
// with AppMap_00 and the menu's background AppMenuBG_00 open. The Scope opens the frame over AppMap_00 without that
// background (Weeping of the Empire's engine/runtime/totk_menu_cursor.cpp census tells them apart the same way).
// The dialogue window (DIALOGUE_SCREEN) has no focus box either: the scan remembers its screen for dialogue_shown.
// A D-pad strip has no focus box either while it is held open: *strip is ShortCut_00 being open. *camera is the
// Purah Pad's camera (the ability wheel's camera): the frame and its pointer CameraPointer_00 open without AppMap_00,
// which the Scope has with them.
// TOTK_MENU_CURSOR_LOG=1 logs the open screens whenever the set changes.
enum { SM_COUNT = 0x8, SM_SCREENS = 0x10 };
static int map_open(void) {
#define DIALOGUE_SCREEN "MessageDialog_00" // the box a character's lines and the game's messages are shown in
#define DIALOGUE_GAP_MS 500 // how long after the box closed a click is still the dialogue's
static uint64_t dialogue_scr; // game thread only
static int map_open(int *strip, int *camera) {
uint64_t sm = screen_mgr();
if (!sm) return 0;
int32_t n = (int32_t)rd32(sm + SM_COUNT);
uint64_t arr = rd64(sm + SM_SCREENS);
*strip = *camera = 0;
if (n <= 0 || n > 1024 || !ok_at(arr, 8ull * (uint64_t)n)) return 0;
const char *e = cfg_get("TOTK_MENU_CURSOR_LOG");
int verbose = e && !strcmp(e, "1"), pad = 0, map = 0, bg = 0;
int verbose = e && !strcmp(e, "1"), pad = 0, map = 0, bg = 0, pointer = 0;
char list[1024];
size_t len = 0;
uint64_t h = 1469598103934665603ull;
@@ -187,6 +207,9 @@ static int map_open(void) {
pad |= !strcmp(nm, "AppMenuOverlay_00");
map |= !strcmp(nm, "AppMap_00");
bg |= !strcmp(nm, "AppMenuBG_00");
if (!strcmp(nm, DIALOGUE_SCREEN)) dialogue_scr = scr;
pointer |= !strcmp(nm, "CameraPointer_00");
*strip |= !strcmp(nm, "ShortCut_00");
if (!verbose) continue;
for (const char *c = nm; *c; c++) h = (h ^ (uint8_t)*c) * 1099511628211ull;
if (len < sizeof list - 64) len += (size_t)snprintf(list + len, sizeof list - len, "%s%s", len ? " " : "", nm);
@@ -194,8 +217,22 @@ static int map_open(void) {
static uint64_t last;
static int lines;
if (verbose && h != last && lines < 200) last = h, lines++, rt_log("menu_cursor: open screens: %s", list);
*camera = pad && pointer && !map && !bg;
return pad && map && bg;
}
// The dialogue window is open. Read at every pad read from the screen the last scan found open, so a click follows the
// box without the scan's 100 ms: the game keeps its screens, so the one found once is the one that opens the next time.
// A screen that is no longer that layout (a reload built the screens again) is forgotten until a scan finds it.
static int dialogue_shown(void) {
uint64_t scr = dialogue_scr;
if (!scr) return 0;
if (!game_object(scr, SCR_REQUEST + 1)) { dialogue_scr = 0; return 0; }
if (rd8(scr + SCR_STATE) != SCR_OPENED) return 0;
char nm[48];
layout_name(scr, nm, sizeof nm);
if (strcmp(nm, DIALOGUE_SCREEN)) { dialogue_scr = 0; return 0; }
return 1;
}
static void layout_size(uint64_t scr, float *lw, float *lh) {
*lw = 1920, *lh = 1080;
uint64_t lyt = rd64(scr + SCR_LAYOUT);
@@ -280,15 +317,29 @@ static int gameplay_screen(const char *nm) {
static volatile int32_t g_pointer; // a menu has the pointer: input_cursor_allowed releases the capture
static volatile int32_t g_map; // the Purah Pad's map is open (not the Scope): the mouse moves its cursor
static volatile int32_t g_dialogue; // a dialogue box is on screen and TOTK_DIALOGUE_CLICK is on: left B, right A
static volatile int32_t g_menu; // a menu screen has the focus box, from either device (the scroll wheel's steps)
static volatile int32_t g_strip; // a D-pad strip's screen is open (the scroll wheel's steps along it)
static volatile int32_t g_camera; // the Purah Pad's camera is open (the scroll wheel zooms it)
int menu_cursor_pointer(void) { return __atomic_load_n(&g_pointer, __ATOMIC_RELAXED); }
int menu_cursor_map(void) { return __atomic_load_n(&g_map, __ATOMIC_RELAXED); }
int menu_cursor_dialogue(void) { return __atomic_load_n(&g_dialogue, __ATOMIC_RELAXED); }
int menu_cursor_menu(void) { return __atomic_load_n(&g_menu, __ATOMIC_RELAXED); }
int menu_cursor_strip(void) { return __atomic_load_n(&g_strip, __ATOMIC_RELAXED); }
int menu_cursor_camera(void) { return __atomic_load_n(&g_camera, __ATOMIC_RELAXED); }
static int enabled(void) {
if (!RT_V143) return 0;
const char *v = cfg_get("TOTK_MENU_CURSOR"), *in = cfg_get("TOTK_INPUT");
return !(v && !strcmp(v, "0")) && !(in && !strcmp(in, "none"));
}
// The left click in dialogue: its own setting, on with the menu cursor off too
static int dialogue_enabled(void) {
if (!RT_V143) return 0;
const char *in = cfg_get("TOTK_INPUT");
return cfg_bool("TOTK_DIALOGUE_CLICK") && !(in && !strcmp(in, "none"));
}
static void set_pointer(int on) {
if (plat_xchg32(&g_pointer, on) == on) return;
@@ -317,27 +368,68 @@ static void log_screen(const Focus *f, const char *nm, int n, float lw, float lh
// Once per pad read on the game thread: the pointer's hover and clicks, as pad buttons (A, B) for this read
void menu_cursor_poll(uint64_t *buttons) {
static int inited, pending_a, held_l, held_r;
static int inited, pending_a, held_l, held_r, anchored;
static uint64_t pending_node, pending_until, hold_until, hold_bit, menu_seen;
static float last_px = -1, last_py = -1;
static uint64_t ptr_node, last_focus; // the item the pointer last pointed at, the focus at the last read
static float last_px = -1, last_py = -1, anchor_px, anchor_py;
if (!inited) {
inited = 1;
main_lo = MOD_MAIN_BASE, main_hi = MOD_MAIN_BASE + MAIN_SIZE;
if (RT_V143) rt_log("menu_cursor: on for 1.4.3 (TOTK_MENU_CURSOR=0 turns it off)");
}
if (!enabled()) { set_pointer(0); __atomic_store_n(&g_map, 0, __ATOMIC_RELAXED); return; }
Focus f = focus_now();
char nm[48] = "";
if (f.screen) layout_name(f.screen, nm, sizeof nm);
int cursor = enabled(), talk = dialogue_enabled();
if (!cursor && !talk) {
set_pointer(0);
__atomic_store_n(&g_map, 0, __ATOMIC_RELAXED);
__atomic_store_n(&g_dialogue, 0, __ATOMIC_RELAXED);
__atomic_store_n(&g_menu, 0, __ATOMIC_RELAXED);
__atomic_store_n(&g_strip, 0, __ATOMIC_RELAXED);
__atomic_store_n(&g_camera, 0, __ATOMIC_RELAXED);
return;
}
// The open screens are scanned every 100 ms, not at every pad read: each screen costs memory queries
uint64_t now = plat_now_ms();
static uint64_t map_at;
static int map, map_logged;
if (!map_at || now - map_at >= 100) map_at = now, map = map_open();
static int map, map_logged, dialogue_logged, strip, camera;
if (!map_at || now - map_at >= 100) map_at = now, map = map_open(&strip, &camera);
// Between two lines of a conversation the game closes the box and opens it again, so a click made in that gap still
// counts as dialogue for DIALOGUE_GAP_MS after the box was last open. The first clicks after a conversation fall
// in it too and are B or A, as they are for a player who taps the button through the last line.
// TOTK_MENU_CURSOR_LOG=1 logs how long each gap was.
static uint64_t dialogue_seen;
int box = talk && dialogue_shown();
if (!talk) dialogue_seen = 0;
if (box) {
static int gap_lines;
const char *e = cfg_get("TOTK_MENU_CURSOR_LOG");
uint64_t gap = dialogue_seen ? now - dialogue_seen : 0;
if (gap > 30 && gap < 5000 && gap_lines < 100 && e && !strcmp(e, "1"))
gap_lines++, rt_log("menu_cursor: the dialogue box was closed for %llu ms", (unsigned long long)gap);
dialogue_seen = now;
}
int dialogue = box || (dialogue_seen && now - dialogue_seen < DIALOGUE_GAP_MS);
if (dialogue && !dialogue_logged)
dialogue_logged = 1, rt_log("menu_cursor: a dialogue box is open (" DIALOGUE_SCREEN "): left click is B, "
"right click A (TOTK_DIALOGUE_CLICK=1)");
__atomic_store_n(&g_dialogue, dialogue, __ATOMIC_RELAXED);
if (!cursor) {
set_pointer(0);
__atomic_store_n(&g_map, 0, __ATOMIC_RELAXED);
__atomic_store_n(&g_menu, 0, __ATOMIC_RELAXED);
__atomic_store_n(&g_strip, 0, __ATOMIC_RELAXED);
__atomic_store_n(&g_camera, 0, __ATOMIC_RELAXED);
return;
}
__atomic_store_n(&g_strip, strip, __ATOMIC_RELAXED);
__atomic_store_n(&g_camera, camera, __ATOMIC_RELAXED);
Focus f = focus_now();
char nm[48] = "";
if (f.screen) layout_name(f.screen, nm, sizeof nm);
if (map && !map_logged) map_logged = 1, rt_log("menu_cursor: the map is open: the mouse moves its cursor");
__atomic_store_n(&g_map, map, __ATOMIC_RELAXED);
// With the gamepad as the last device the menus stay as they are on the console: no pointer, the capture kept.
// Moving the mouse (or a key) makes keyboard and mouse the last device and the pointer comes.
__atomic_store_n(&g_menu, f.screen && !gameplay_screen(nm) && !map, __ATOMIC_RELAXED);
int menu = f.screen && !gameplay_screen(nm) && ikb_foreground() && input_kbm_last();
if (menu) menu_seen = now;
// A menu's focus gap (a tab switch, a popup opening: no focus box for a frame or two) keeps the pointer where it
@@ -352,7 +444,7 @@ void menu_cursor_poll(uint64_t *buttons) {
return;
}
if (gap) return; // a click's focus request waits for the next screen
if (!menu) { pending_a = 0; last_px = last_py = -1; return; }
if (!menu) { pending_a = 0, anchored = 0, ptr_node = last_focus = 0; last_px = last_py = -1; return; }
if (pending_a) { // the focus asked for by a click: A once it is there
if (rd64(f.control + CTL_ACTIVE) == pending_node) {
pending_a = 0, hold_until = now + 60, hold_bit = 1ull << B_A;
@@ -366,11 +458,21 @@ void menu_cursor_poll(uint64_t *buttons) {
if (click_r) { hold_until = now + 60, hold_bit = 1ull << B_B; *buttons |= hold_bit; return; }
float px, py;
if (!picture_pos(&px, &py)) return;
// The focus moved to another item than the pointer's (the scroll wheel, the keys, the pad): the pointer takes it
// back only after it has left a dead zone of DEAD_ZONE layout units around where it was then, so a hand resting on
// the mouse does not undo a scroll. A click still acts at once.
if (f.node != last_focus) {
if (f.node != ptr_node) anchored = 1, anchor_px = px, anchor_py = py;
last_focus = f.node;
}
int moved = fabsf(px - last_px) > 0.001f || fabsf(py - last_py) > 0.001f;
if (!moved && !click_l) return;
last_px = px, last_py = py;
float lw, lh;
layout_size(f.screen, &lw, &lh);
if (anchored && !click_l && fabsf(px - anchor_px) * lw < DEAD_ZONE && fabsf(py - anchor_py) * lh < DEAD_ZONE)
return;
anchored = 0;
Xf xf = screen_xf(f.screen);
Pt p = {(px - 0.5f) * lw, (0.5f - py) * lh, px * 2 - 1, 1 - py * 2, NULL};
if (xf.ok && !xf_plain(&xf, lw, lh)) p.t = &xf;
@@ -379,6 +481,7 @@ void menu_cursor_poll(uint64_t *buttons) {
log_screen(&f, nm, n, lw, lh, p.t, n && stale_hit_panes(n, btn, lw, lh, p.t));
uint64_t at = n ? node_at(n, node, btn, &p, lw, lh) : 0;
if (!at) return;
ptr_node = at;
if (at != f.node) move_focus(&f, at);
if (click_l) {
if (at == f.node) { hold_until = now + 60, hold_bit = 1ull << B_A; *buttons |= hold_bit; }
+3 -2
View File
@@ -30,8 +30,9 @@ typedef struct {
int kind, enabled;
ModProblem problem;
uint32_t replaced, added, overridden; // romfs.c: files served (replacing / added), files a higher mod serves
uint32_t other_version; // files for another game version (RomFS *.Product.<n>.*, ExeFS patches
// for another build), not used
uint32_t other_version; // files named for another game version: RomFS *.Product.<n>.* the RomFS
// lacks (served under their own names; another version's size table is
// not) and ExeFS patches for another build (not used)
char overridden_by[256]; // the first higher mod that took one of its files
char note[256]; // MOD_EXEFS_UNSUPPORTED: why
uint32_t pending, pending_key; // ExeFS patch files the build lacks; FNV-1a of their names and contents
+206 -6
View File
@@ -7,19 +7,30 @@
// axis, direction = the controller's three axes in a reference frame whose z is up. The orientation is integrated from
// the gyro and, while the controller is not being accelerated (|a| within 15 % of 1 G), pulled toward the measured
// gravity (Mahony, gain 0.5/s: about 2 s), as the Switch's sensor fusion does; yaw has no reference and drifts with the
// gyro's bias. The newest MOTION_RING samples are kept; reads are newest first. A restart (StartSixAxisSensor) drops
// the samples before it: reads return nothing until the next sample, which starts a fresh frame and angle; sampling
// numbers go on. The feeding thread writes, guest threads read, both under the instance's SRW lock.
// gyro's bias, so a bias measured with the controller at rest (the Calibrate gyro row, motion_calibrate_start) is
// subtracted from every sample while gyro_calibration is on. The newest MOTION_RING samples are kept; reads are newest
// first. A restart (StartSixAxisSensor) drops the samples before it: reads return nothing until the next sample, which
// starts a fresh frame and angle; sampling numbers go on. The feeding thread writes, guest threads read, both under the
// instance's SRW lock.
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "cfg.h"
#include "host.h"
#include "input.h"
#include "platform/platform.h"
enum { MOTION_RING = 64 };
// A calibration skips CALIB_SETTLE samples (0.5 s at the sensor thread's 4 ms), then averages CALIB_SAMPLES (1 s).
enum { MOTION_RING = 64, CALIB_SETTLE = 125, CALIB_SAMPLES = 250 };
#define STALE_NS 200000000ull // no sample for 200 ms: no motion
#define CALIB_TIMEOUT_NS 5000000000ull // 5 s without a sample: the attempt gives up
// Largest per-axis standard deviation of the averaged samples (rad/s, about 2.3 deg/s) that still counts as holding
// still. The spread, not the mean, tells a moved controller from a biased one: a factory bias shifts every sample
// alike (0.14 rad/s on one Pro Controller's z axis) and passes, while the noise of a resting pad or a steady hand stays
// near 0.01-0.02 rad/s. A jolt that stays under the limit moves the mean by at most the limit times the square root of
// the share of the second it lasts.
#define CALIB_MAX_SPREAD 0.04
struct MotionFusion {
PlatMutex lock;
@@ -29,6 +40,15 @@ struct MotionFusion {
double R[3][3]; // controller -> reference; row i = reference axis i in controller coordinates
double angle[3];
int restart;
double gyro_bias[3]; // the controller's resting gyro (rad/s), subtracted from every sample while the switch is on
int bias_valid; // gyro_bias holds a measured or loaded estimate, so a zero is a value
int calibrating; // an attempt is collecting samples
int calib_count; // samples of the attempt so far, the skipped settle samples included
double calib_sum[3], calib_sq[3]; // sum and sum of squares of the averaged samples, per axis
uint64_t calib_start_ns; // when the attempt started (the timeout)
char calib_pad[128]; // the controller the attempt measures (input.c pad_identity), written with the bias
int calib_result; // MOTION_CALIB_NONE, _DONE, _FAILED or _MOVED: the outcome the row shows between attempts
int persist; // the gamepad's instance: a finished calibration is written to totk.ini
};
MotionFusion *motion_new(void) {
@@ -44,6 +64,7 @@ static MotionFusion *global;
static void global_init(void *arg) {
(void)arg;
global = motion_new();
global->persist = 1;
}
MotionFusion *motion_global(void) {
@@ -107,11 +128,115 @@ static void integrate(double R[3][3], const double gyro[3], const double acc[3],
orthonormalize(R);
}
void motion_feed(MotionFusion *m, const double acc[3], const double gyro[3], uint64_t now) {
// Write the calibrated bias to totk.ini. Called with m->lock released: cfg_set takes cfg.c's own lock and cfg_save
// rewrites the file, and neither belongs inside the sensor thread's critical section.
static void save_bias(const double bias[3], const char *pad, double spread) {
char buf[32];
snprintf(buf, sizeof buf, "%.6f", bias[0]);
cfg_set("TOTK_GYRO_BIAS_X", buf);
snprintf(buf, sizeof buf, "%.6f", bias[1]);
cfg_set("TOTK_GYRO_BIAS_Y", buf);
snprintf(buf, sizeof buf, "%.6f", bias[2]);
cfg_set("TOTK_GYRO_BIAS_Z", buf);
cfg_set("TOTK_GYRO_BIAS_PAD", pad);
// Saved now rather than at the menu's save tick, which may not run if the menu is closed first.
if (cfg_save())
rt_log("motion: gyro bias calibrated from %d samples: %.5f %.5f %.5f rad/s (spread %.4f rad/s)", CALIB_SAMPLES,
bias[0], bias[1], bias[2], spread);
else
rt_log("motion: gyro bias %.5f %.5f %.5f rad/s could not be written to totk.ini; it applies to this run only",
bias[0], bias[1], bias[2]);
}
// The one timeout decision, for the sample path and the menu's poll alike: CALIB_TIMEOUT_NS without a sample since the
// attempt started or since its last sample, so a slow sensor tick only lengthens the attempt. now may trail a sample
// the sensor thread stored meanwhile; that is no timeout. Called with m->lock held; the caller logs, with the lock
// released. A bias that is already in effect stays in effect and the row keeps reporting it; a controller with no
// bias shows the failure.
static int calib_check_timeout(MotionFusion *m, uint64_t now) {
uint64_t since = m->last_ns > m->calib_start_ns ? m->last_ns : m->calib_start_ns;
if (!m->calibrating || now <= since || now - since <= CALIB_TIMEOUT_NS) return 0;
m->calibrating = 0;
m->calib_result = m->bias_valid ? MOTION_CALIB_DONE : MOTION_CALIB_FAILED;
return 1;
}
// The bias's own switch (gyro_calibration, live, default 1): off, the fusion takes the raw gyro, an attempt in
// progress is dropped, and the menu's calibrate entry does nothing. The saved keys stay untouched, so turning it
// back on restores the last calibration.
int motion_calibration_enabled(void) {
static int on = -1;
static unsigned seen;
unsigned v = cfg_version();
if (on < 0 || v != seen) {
seen = v;
on = cfg_bool("TOTK_GYRO_CALIBRATION");
}
return on;
}
// One calibration sample, m->lock held. The first CALIB_SETTLE samples are skipped: the button press that started the
// attempt shakes the controller. The next CALIB_SAMPLES are averaged per axis into the bias, unless their spread shows
// the controller moved (CALIB_MAX_SPREAD). Returns 1 when the attempt ended with a new bias, -1 when it was rejected,
// 0 while it goes on; *spread receives the largest per-axis standard deviation of a finished attempt.
static int calib_sample(MotionFusion *m, const double g[3], double *spread) {
if (++m->calib_count <= CALIB_SETTLE) return 0;
for (int i = 0; i < 3; i++) {
m->calib_sum[i] += g[i];
m->calib_sq[i] += g[i] * g[i];
}
if (m->calib_count < CALIB_SETTLE + CALIB_SAMPLES) return 0;
m->calibrating = 0;
double mean[3], worst = 0;
for (int i = 0; i < 3; i++) {
mean[i] = m->calib_sum[i] / CALIB_SAMPLES;
double var = m->calib_sq[i] / CALIB_SAMPLES - mean[i] * mean[i];
double sd = var > 0 ? sqrt(var) : 0;
if (sd > worst) worst = sd;
}
*spread = worst;
if (worst > CALIB_MAX_SPREAD) {
m->calib_result = MOTION_CALIB_MOVED; // the bias in effect, if any, stays
return -1;
}
memcpy(m->gyro_bias, mean, sizeof mean);
m->bias_valid = 1;
m->calib_result = MOTION_CALIB_DONE;
m->restart = 1; // the angle and direction start over from the new bias
return 1;
}
void motion_feed(MotionFusion *m, const double acc[3], const double gyro_raw[3], uint64_t now) {
const int calib_on = motion_calibration_enabled(); // read before the lock: a settings change takes cfg.c's lock
double saved[3] = {0, 0, 0}, spread = 0;
char saved_pad[sizeof m->calib_pad] = "";
int ended = 0, timed_out = 0, samples = 0;
uint64_t started = 0;
plat_mutex_lock(&m->lock);
double dt = m->last_ns && now > m->last_ns ? (double)(now - m->last_ns) / 1e9 : 0.004;
if (dt > 0.05) dt = 0.004; // a gap (no controller, sensors were off): no integration across it
m->last_ns = now;
// Calibration: the player holds the controller still, so the raw gyro reads the bias. With the switch off an
// attempt in progress is dropped.
if (m->calibrating && !calib_on) {
m->calibrating = 0;
m->calib_result = m->bias_valid ? MOTION_CALIB_DONE : MOTION_CALIB_NONE;
}
if (m->calibrating) {
if (calib_check_timeout(m, now)) {
timed_out = 1;
samples = m->calib_count;
started = m->calib_start_ns;
} else if ((ended = calib_sample(m, gyro_raw, &spread)) != 0) {
memcpy(saved, ended > 0 ? m->gyro_bias : m->calib_sum, sizeof saved);
if (ended < 0)
for (int i = 0; i < 3; i++) saved[i] /= CALIB_SAMPLES;
snprintf(saved_pad, sizeof saved_pad, "%s", m->calib_pad);
}
}
m->last_ns = now; // after the timeout check, which measures the gap before this sample
// The bias applies only while the switch is on; with no bias measured it is zero and the gyro passes unchanged.
double gyro[3];
for (int i = 0; i < 3; i++) gyro[i] = gyro_raw[i] - (calib_on ? m->gyro_bias[i] : 0);
if (m->restart) {
align_to(m->R, acc);
memset(m->angle, 0, sizeof m->angle);
@@ -128,7 +253,20 @@ void motion_feed(MotionFusion *m, const double acc[3], const double gyro[3], uin
for (int j = 0; j < 3; j++) s->dir[i * 3 + j] = (float)m->R[j][i]; // direction i = controller axis i in the reference frame
}
m->produced++;
// The logs run with the lock released: the log back end can be slow, and the menu thread holds this lock in
// motion_calib_poll.
plat_mutex_unlock(&m->lock);
if (timed_out)
rt_log("motion: gyro calibration timed out after %.1f s (%d samples): sensor data not arriving",
(now - started) / 1e9, samples);
if (ended < 0)
rt_log("motion: gyro calibration rejected, the controller moved: spread %.4f rad/s over the limit %.2f "
"(mean %.5f %.5f %.5f rad/s)", spread, CALIB_MAX_SPREAD, saved[0], saved[1], saved[2]);
if (ended > 0) {
if (m->persist) save_bias(saved, saved_pad, spread);
else rt_log("motion: gyro bias calibrated: %.5f %.5f %.5f rad/s (spread %.4f rad/s)", saved[0], saved[1],
saved[2], spread);
}
}
int motion_read(MotionFusion *m, MotionState *out, int max, uint64_t now) {
@@ -152,3 +290,65 @@ void motion_restart(MotionFusion *m) {
memset(m->angle, 0, sizeof m->angle);
plat_mutex_unlock(&m->lock);
}
void motion_calibrate_start(MotionFusion *m, const char *pad, uint64_t now) {
plat_mutex_lock(&m->lock);
m->calibrating = 1;
m->calib_count = 0;
memset(m->calib_sum, 0, sizeof m->calib_sum);
memset(m->calib_sq, 0, sizeof m->calib_sq);
m->calib_start_ns = now;
snprintf(m->calib_pad, sizeof m->calib_pad, "%s", pad ? pad : "");
plat_mutex_unlock(&m->lock);
}
// One axis of totk.ini; a value that is not a number leaves the axis at zero and is named in the log, so a
// hand-edited or damaged line does not silently read as a calibrated zero.
static double bias_value(const char *key) {
const char *v = cfg_get(key);
if (!v || !*v) return 0;
char *end;
double d = strtod(v, &end);
if (end == v || *end || !isfinite(d)) {
rt_log("motion: %s = \"%s\" is not a number; that axis starts uncalibrated", key, v);
return 0;
}
return d;
}
void motion_bias_load(MotionFusion *m) {
double bias[3] = {bias_value("TOTK_GYRO_BIAS_X"), bias_value("TOTK_GYRO_BIAS_Y"), bias_value("TOTK_GYRO_BIAS_Z")};
plat_mutex_lock(&m->lock);
memcpy(m->gyro_bias, bias, sizeof bias);
m->bias_valid = 1;
m->calib_result = MOTION_CALIB_DONE;
plat_mutex_unlock(&m->lock);
}
void motion_bias_reset(MotionFusion *m) {
plat_mutex_lock(&m->lock);
memset(m->gyro_bias, 0, sizeof m->gyro_bias);
// An attempt that was collecting belongs to the previous controller: its samples must not complete against this
// one, and its name must not label a bias measured from another controller.
m->calibrating = 0;
m->calib_count = 0;
m->calib_pad[0] = 0;
m->calib_result = MOTION_CALIB_NONE;
m->bias_valid = 0;
plat_mutex_unlock(&m->lock);
}
// Also ends an attempt whose samples stopped (the sensor thread no longer calls motion_feed): without this the row
// would read "Calibrating..." forever. Logs with the lock released: the menu thread calls this on every redraw.
int motion_calib_poll(MotionFusion *m, uint64_t now) {
plat_mutex_lock(&m->lock);
int timed_out = calib_check_timeout(m, now);
int st = m->calibrating ? MOTION_CALIB_COLLECTING : m->calib_result;
uint64_t start = m->calib_start_ns;
int count = m->calib_count;
plat_mutex_unlock(&m->lock);
if (timed_out)
rt_log("motion: gyro calibration timed out after %.1f s (%d samples): the sensor thread stopped feeding it",
(now - start) / 1e9, count);
return st;
}
+112 -16
View File
@@ -119,7 +119,10 @@ void input_direct_mouse_row(int32_t dx, float sensitivity, uint64_t now, InputMo
#define HOST_MOUSE_ORBIT_BEGIN (RT_PATCH_PAGE + 0xfb0)
#define HOST_MOUSE_ORBIT_ANGLES (RT_PATCH_PAGE + 0xfa0)
#define HOST_MOUSE_HAND (RT_PATCH_PAGE + 0xf90)
#define HOST_MOUSE_ORBIT_STATE (RT_PATCH_PAGE + 0xf70)
static _Thread_local uint64_t mouse_input_object;
static volatile int32_t cam_blocked, cam_kind;
static uint64_t cam_kind_at;
// Where a game version keeps what the hooks read and write (recomp/patches-v143.txt, patches-v121.txt). The camera
// input update keeps its flags in w21, its input object in x20 and y in s10 in both; the free orbit's object is x0 at
@@ -162,8 +165,8 @@ static void sample_mark_taken(uint64_t generation) {
static _Thread_local struct { uint64_t object; MouseSample mouse; } orbit;
typedef struct {
uint64_t object, generation, input, seen;
float pitch, original_target;
int hold_pitch, target_saved, resume;
float pitch;
int hold_pitch, resume;
} OrbitTrack;
static OrbitTrack orbit_seen[16];
@@ -268,19 +271,17 @@ static uint32_t player_state_logged(void) {
return f;
}
// main+0x191d78 builds the pitch target from +0x15c and +0x11c; the orbit update filters +0x108 toward it and adds
// (1 - +0x88) * +0x10c at output. Updating only +0x108 makes the camera spring back to the unchanged target.
// The orbit output adds (1 - +0x88) * +0x10c to its filtered pitch at +0x108.
// Hold the final angle in orbit_angles. Do not overwrite the configured target
// at +0x15c: main+0x191534 also uses it as the reference for camera distance.
// Replacing that reference with the held pitch changes the native return from
// aiming and collision close-ups, even though no distance field is written.
static float orbit_pitch_offset(uint64_t object) {
return (1 - guest_float(object + 0x88)) * guest_float(object + 0x10c);
}
static void orbit_pitch_target(uint64_t object, float pitch) {
guest_float_store(object + 0x15c, pitch - orbit_pitch_offset(object) - guest_float(object + 0x11c));
}
static void orbit_pitch_release(OrbitTrack *track) {
if (track->target_saved) guest_float_store(track->object + 0x15c, track->original_target);
track->hold_pitch = track->target_saved = track->resume = 0;
track->hold_pitch = track->resume = 0;
}
// The pitch the camera has now (the filtered pitch plus its vertical offset), as orbit_angles leaves it
@@ -288,8 +289,8 @@ static float orbit_pitch_now(uint64_t object) {
return wrap_degrees(guest_float(object + 0x108) + orbit_pitch_offset(object));
}
// The camera stays where it is now: the mouse holds the pitch the camera has (orbit_begin writes its target), so
// neither the game's default target nor the mouse's target from before an aim or a lock-on pulls it back.
// Resume from the pitch the camera reached during the handoff. orbit_angles
// holds this final angle without replacing the native configuration reference.
static void orbit_pitch_settle(OrbitTrack *track) {
track->pitch = orbit_pitch_now(track->object);
track->hold_pitch = 1, track->resume = 0;
@@ -363,6 +364,7 @@ static void camera_input(Cpu *c) {
const unsigned rx = mouse_layout()->input_x;
mouse_input_object = 0;
uint32_t flags = (uint32_t)c->x[21];
__atomic_store_n(&cam_blocked, (flags & 1) != 0, __ATOMIC_RELAXED);
int accept = !(flags & 1) && !input_direct_mouse_selector_active();
__atomic_store_n(&accepts_mouse, accept, __ATOMIC_RELAXED);
__atomic_store_n(&last_update, plat_now_ns(), __ATOMIC_RELEASE);
@@ -457,6 +459,8 @@ static void camera_input(Cpu *c) {
// directly and move its persistent pitch/yaw by the same amounts so the next update starts at the new orientation.
// The controller still handles its gamepad input, following, collision and focus normally.
static void orbit_begin(Cpu *c) {
__atomic_store_n(&cam_kind, INPUT_CAM_FREE_ROAM, __ATOMIC_RELAXED);
__atomic_store_n(&cam_kind_at, plat_now_ns(), __ATOMIC_RELEASE);
orbit.object = 0;
uint64_t object = c->x[0];
if (direct_mode()) {
@@ -471,7 +475,7 @@ static void orbit_begin(Cpu *c) {
memcpy(&override, (void *)(uintptr_t)(input + 0x15c), 1);
plat_mutex_lock(&sample_lock);
OrbitTrack *track = orbit_track(object);
if (track->input != input) track->hold_pitch = track->target_saved = track->resume = 0;
if (track->input != input) track->hold_pitch = track->resume = 0;
uint64_t now = plat_now_ns();
// A gamepad's vertical input gives the pitch back to the game's own target. The game's lock-on or override
// (aiming) drives the camera itself: the mouse lets go without restoring the target it had before, and
@@ -484,7 +488,6 @@ static void orbit_begin(Cpu *c) {
track->seen = now;
take = moved || track->hold_pitch;
if (take) track->input = input;
if (track->hold_pitch) orbit_pitch_target(object, track->pitch);
plat_mutex_unlock(&sample_lock);
if (moved) { // the mouse's part of this step's stick goes to the final angles instead
guest_float_store(input + 0x420, m.pad[0]);
@@ -500,6 +503,42 @@ static void orbit_begin(Cpu *c) {
c->x[8] = guest_pointer(object + 8); // displaced ldr x8, [x0, #8]
}
// The native manual-orbit state (4) integrates both pitch and distance from
// the stick. Direct mouse orbit bypasses those integrators, so entering that
// state during an aim return can latch the close-up's radius indefinitely.
// Select the native automatic-distance state (3) at the existing state decision
// point, before its transition initializer runs (main+0x18ea34, 1.4.3).
// This retains the game's own radius curve, transition easing and collision.
static void orbit_state(Cpu *c) {
uint64_t object = c->x[20];
uint32_t override;
memcpy(&override, (void *)(uintptr_t)(object + 0x188), sizeof override);
if (rt_game_version == 143 && direct_mode() && (uint32_t)c->x[10] == 4 && !override) {
MouseSample m = sample_read();
uint64_t input = guest_pointer(object + 0x20);
if (m.object == input && m.camera_enabled && plat_now_ns() - m.when < 150000000ull &&
m.pad[0] == 0 && m.pad[1] == 0 &&
!(input_buttons_last() & ((1ull << B_ZR) | (1ull << B_R)))) {
uint64_t held = guest_pointer(input + 0x148);
uint8_t flags;
memcpy(&flags, (void *)(uintptr_t)(input + 0x15c), 1);
plat_mutex_lock(&sample_lock);
OrbitTrack *track = orbit_track(object);
int owned = track->input == input && track->hold_pitch;
plat_mutex_unlock(&sample_lock);
if (owned && !(held & 0x20) && !(flags & 0x40)) {
c->x[10] = 3;
static int logged;
if (!logged) {
logged = 1;
rt_log("mouse: native distance transition retained for direct orbit");
}
}
}
}
c->x[8] = override; // displaced ldr w8, [x20, #0x188]
}
// The final pitch and yaw are on the stack (1.4.3 sp + 0x44 / 0x48, 1.2.1 sp + 0x3c / 0x40)
static void orbit_angles(Cpu *c) {
const MouseLayout *ml = mouse_layout();
@@ -526,9 +565,7 @@ static void orbit_angles(Cpu *c) {
guest_float_store(yaw_at, wrap_degrees(yaw + yaw_delta));
guest_float_store(object + 0x108, wrap_degrees(new_pitch - orbit_pitch_offset(object)));
guest_float_store(object + 0x120, wrap_degrees(guest_float(object + 0x120) + yaw_delta));
if (!track->target_saved) track->original_target = guest_float(object + 0x15c), track->target_saved = 1;
track->pitch = new_pitch; track->input = orbit.mouse.object; track->hold_pitch = 1;
orbit_pitch_target(object, new_pitch);
static int logged;
if (!logged) {
logged = 1;
@@ -1002,6 +1039,45 @@ static void camera_load(Cpu *c) {
rt_log("mouse: load hook called from main+%#llx, no such site", (unsigned long long)offset);
abort();
}
int mapped = INPUT_CAM_UNCLASSIFIED;
switch (s->mode) {
case MODE_AIM:
case MODE_LOCKON_AIM:
mapped = INPUT_CAM_AIM;
break;
case MODE_SCOPE:
case MODE_FINDER:
mapped = INPUT_CAM_SCOPE;
break;
case MODE_ABILITY:
case MODE_ABILITY_OFFSET:
mapped = INPUT_CAM_ABILITY;
break;
case MODE_LOCKON:
mapped = INPUT_CAM_LOCK_ON;
break;
case MODE_HORSE:
case MODE_HORSE_LOCKON:
mapped = INPUT_CAM_VEHICLE;
break;
case MODE_TALK:
case MODE_TALK_MANUAL:
mapped = INPUT_CAM_DISABLED;
break;
case MODE_CLIMB:
case MODE_WAKEBOARD:
case MODE_SHIELD:
case MODE_WATERFALL:
case MODE_TAIL:
mapped = INPUT_CAM_FREE_ROAM;
break;
default:
break;
}
if (mapped != INPUT_CAM_UNCLASSIFIED) {
__atomic_store_n(&cam_kind, mapped, __ATOMIC_RELAXED);
__atomic_store_n(&cam_kind_at, plat_now_ns(), __ATOMIC_RELEASE);
}
uint64_t at = load_address(c, s->insn);
if (s->role == ROLE_BASE_STORE) { // performs the store, of the value plus the degrees when there are any
uint32_t bits = (uint32_t)c->v[s->insn & 31];
@@ -1062,8 +1138,28 @@ __attribute__((constructor)) static void mouse_direct_register(void) {
rt_register_host(HOST_MOUSE_DIRECT, camera_input);
rt_register_host(HOST_MOUSE_ACTIVITY, camera_activity);
rt_register_host(HOST_MOUSE_ORBIT_BEGIN, orbit_begin);
if (rt_game_version == 143) rt_register_host(HOST_MOUSE_ORBIT_STATE, orbit_state);
rt_register_host(HOST_MOUSE_ORBIT_ANGLES, orbit_angles);
rt_register_host(HOST_MOUSE_HAND, hand_input);
rt_register_host(HOST_MOUSE_LOAD, camera_load);
}
int input_camera_context(void) {
static int patch_id = -2;
if (patch_id == -2) patch_id = rt_patch_set("mouse_direct");
if (patch_id < 0 || !rt_patch_enabled(patch_id)) return INPUT_CAM_UNCLASSIFIED;
uint64_t now = plat_now_ns();
if (hand_active(now)) return INPUT_CAM_ULTRAHAND_OBJECT;
uint64_t kat = __atomic_load_n(&cam_kind_at, __ATOMIC_ACQUIRE);
int kind = (kat && now - kat < 150000000ull) ? __atomic_load_n(&cam_kind, __ATOMIC_RELAXED) : INPUT_CAM_UNCLASSIFIED;
if (kind == INPUT_CAM_SCOPE || kind == INPUT_CAM_AIM) return kind;
uint64_t last = __atomic_load_n(&last_update, __ATOMIC_ACQUIRE);
int cam_alive = last && now - last < 150000000ull;
if (cam_alive && __atomic_load_n(&cam_blocked, __ATOMIC_RELAXED)) return INPUT_CAM_MENU_WHEEL;
if (kind != INPUT_CAM_UNCLASSIFIED) return kind;
if (cam_alive) return INPUT_CAM_FREE_ROAM;
return INPUT_CAM_UNCLASSIFIED;
}
#else
int input_camera_context(void) { return INPUT_CAM_UNCLASSIFIED; }
#endif
+1
View File
@@ -435,6 +435,7 @@ int probe_movie(void) {
C = plat_aligned_alloc(sizeof(Cpu), 64);
CHECK(C, "Cpu allocation");
memset(C, 0, sizeof *C);
rt_fpcr_threshold(C);
uint64_t obj_mem[2] = {0}, obj = U(obj_mem);
volatile int32_t ev[4] = {0};
+9
View File
@@ -43,6 +43,15 @@ _Static_assert(sizeof(nvn_mapping_t) == 0x28, "NVNmappingRequest stride");
const pool_t *nvn_pool_find(uint64_t addr, uint64_t len); // GPU-visible pool holding [addr, addr+len], NULL if none
uint64_t nvn_new_serial(void); // next tex_t / prog_t serial, from 1
// TOTK_SYNC_AHEAD (nvn_api_core.c): nvnQueuePresentTexture notes the newest fence at each present; the newest
// previous-frame value the game was told is done although it may not be (0: none). The back end's shadow copies
// wait for it or are queued for the GPU (vk_sync.c prologue), so the GPU never reads CPU writes the game made under
// that assumption too early.
void nvn_sync_ahead_present(void);
uint64_t nvn_sync_ahead_guard(void);
int vk_mem_shadow_on(void); // vk_memory.c: 1 with shadow storage on (valid once the Vulkan back end is up)
void vk_mem_shadow_fence(void); // vk_memory.c: returns once every shadow sync that read the old guard is done
void gfx_replay_wait_value(uint64_t v); // vk_backend.c: the replay thread submitted value v
uint64_t nvn_fence_last(void); // fence value of the newest submit (0 before the first)
int nvn_depth_minus_w(void); // 1 after DeviceSetDepthMode(NEAR_IS_MINUS_W), else 0
void nvn_guest_written(uint64_t addr, uint64_t size); // the host or the game wrote pool storage: back end re-reads
+1
View File
@@ -148,6 +148,7 @@ static void *realloc_wait(void *old, size_t n, const char *what) {
if (p) return p;
pool_drain();
big_drain();
rt_oom_request(); // the Vulkan back end gives memory back at its next submit (oom.c)
static uint32_t reports;
int report = __atomic_fetch_add(&reports, 1, __ATOMIC_RELAXED) < 8;
if (report) {
+67 -3
View File
@@ -471,6 +471,49 @@ NVN_API(nvnQueueFenceSync) {
s->fenced = 1;
s->fence = nvn_fence_last();
}
// TOTK_SYNC_AHEAD=1: one more frame in flight. Each frame the game waits (one nvnSyncWait, frame_wait_lr) for
// the frame it presented last; on the Switch that work is long done by then, here it is still in the replay and on the
// GPU (about 11 ms from the submit), and the wait sets the frame time. With the switch on, that wait, and only that
// one, waits for the frame before it instead. What the game reads back of GPU results after the wait can then be a
// frame old. The game's CPU writes to memory the GPU still reads reach the GPU only through shadow copies, which are
// queued for the GPU or wait (nvn_sync_ahead_guard). The wait still lasts until the replay thread submitted the asked
// value, so no copy of that value itself picks up those writes. pa_fence: the newest fence at the latest present and
// the one before, present thread only for writes. pa_guard: the newest value the game was told is done early.
static uint64_t pa_fence[2], pa_guard;
// The return address of that wait. 1.2.1: 0x7000b23dfc. 1.4.3: 0x70002db99c, the second of the two nvnSyncWait calls
// in f_70002db8ac; like on 1.2.1 its sync comes from a two-entry ring indexed per frame, and the first call waits on
// one object's sync behind a mode check. 0: no site known, the switch stays off.
static uint64_t frame_wait_lr(void) {
return rt_game_version == 121 ? 0x7000b23dfcull : rt_game_version == 143 ? 0x70002db99cull : 0;
}
static int sync_ahead(void) {
static int on = -1;
if (on < 0) {
on = cfg_bool("TOTK_SYNC_AHEAD");
// Without shadow storage the GPU reads the game's own memory, which the game writes ahead: nothing guards
// that. The Vulkan back end sets the mode while it starts, before the game's first present or wait; the null
// back end never does.
if (on && !vk_mem_shadow_on()) {
rt_log("nvn: TOTK_SYNC_AHEAD needs shadow storage (shadow = 1 in totk.ini or TOTK_SHADOW=1 on native Windows); "
"the game waits for the GPU as before");
on = 0;
}
if (on && !frame_wait_lr()) {
rt_log("nvn: TOTK_SYNC_AHEAD: no frame wait site is known for this game version; the game waits for the "
"GPU as before");
on = 0;
}
if (on) rt_log("nvn: TOTK_SYNC_AHEAD: a wait for the frame presented last waits for the frame before it");
}
return on;
}
void nvn_sync_ahead_present(void) {
if (!sync_ahead()) return;
__atomic_store_n(&pa_fence[1], __atomic_load_n(&pa_fence[0], __ATOMIC_RELAXED), __ATOMIC_RELEASE);
__atomic_store_n(&pa_fence[0], nvn_fence_last(), __ATOMIC_RELEASE);
}
uint64_t nvn_sync_ahead_guard(void) { return __atomic_load_n(&pa_guard, __ATOMIC_ACQUIRE); }
// NVNsyncWaitResult nvnSyncWait(const NVNsync *s, uint64_t timeout_ns): a sync never fenced reports a timeout at once;
// a fenced one is released only after the back end completed that fence value
NVN_API(nvnSyncWait) {
@@ -478,13 +521,34 @@ NVN_API(nvnSyncWait) {
uint64_t timeout = arg_x(c, 1);
if (!s->fenced) { ret_x(c, NVN_SYNC_WAIT_RESULT_TIMEOUT_EXPIRED); return; }
const GfxBackend *b = gfx_backend();
if (b->fence_done() >= s->fence) { b->cpu_synced(s->fence); ret_x(c, NVN_SYNC_WAIT_RESULT_ALREADY_SIGNALED); return; }
uint64_t want = s->fence;
// Only the game's per-frame wait lags, and not a poll (timeout 0).
if (timeout && c->x[30] == frame_wait_lr() && sync_ahead() && b->fence_done() < want) {
uint64_t p0 = __atomic_load_n(&pa_fence[0], __ATOMIC_ACQUIRE);
uint64_t p1 = __atomic_load_n(&pa_fence[1], __ATOMIC_ACQUIRE);
if (want <= p0 && want > p1) want = p1;
}
if (want != s->fence) { // the replay of the asked value is done first: the guard then covers every copy of it
{
NVN_BLOCKING();
gfx_replay_wait_value(s->fence);
}
uint64_t g = __atomic_load_n(&pa_guard, __ATOMIC_RELAXED);
while (s->fence > g &&
!__atomic_compare_exchange_n(&pa_guard, &g, s->fence, 1, __ATOMIC_RELEASE, __ATOMIC_RELAXED)) {}
vk_mem_shadow_fence(); // a shadow sync that read the old guard finishes before the game writes again
}
if (b->fence_done() >= want) {
b->cpu_synced(want == s->fence ? s->fence : b->fence_done());
ret_x(c, NVN_SYNC_WAIT_RESULT_ALREADY_SIGNALED);
return;
}
int ok;
{
NVN_BLOCKING();
ok = b->fence_wait(s->fence, timeout);
ok = b->fence_wait(want, timeout);
}
if (ok) b->cpu_synced(s->fence);
if (ok) b->cpu_synced(want == s->fence ? s->fence : b->fence_done());
ret_x(c, ok ? NVN_SYNC_WAIT_RESULT_CONDITION_SATISFIED : NVN_SYNC_WAIT_RESULT_TIMEOUT_EXPIRED);
}
// void nvnEventBuilderSetDefaults(NVNeventBuilder *b)
+4
View File
@@ -687,6 +687,8 @@ NVN_API(nvnWindowAcquireTexture) {
sleep_until(at);
perf_end(PERF_PACE, p0);
}
vk_reflex_sleep();
vk_reflex_marker(0, vk_reflex_frame_begin()); // VK_LATENCY_MARKER_SIMULATION_START_NV: a new frame and its ID
// The sync means "the display no longer reads this image". Presents are copies queued in submit order, so it is
// already satisfied: fenced = 3 with fence 0.
if (s) {
@@ -710,9 +712,11 @@ NVN_API(nvnQueuePresentTexture) {
if (o->magic != NVN_WIN_MAGIC || idx < 0 || idx >= o->ntex || idx >= 4 || !o->tex[idx])
rt_fatal("nvn: QueuePresentTexture of image %d on window %p", idx, (void *)o);
o->presents++;
vk_reflex_marker(1, vk_reflex_frame()); // VK_LATENCY_MARKER_SIMULATION_END_NV
const tex_t *t = (const tex_t *)o->tex[idx];
uint64_t period = present_period(o), now = kern_now_ns();
uint64_t slot = now + pace(&o->pace, now, period);
nvn_sync_ahead_present();
gfx_backend()->present(t, o->crop, slot);
o->ticket[idx] = o->presents;
o->avail[idx] = cfg_bool("TOTK_FRAME_QUEUE") ? slot : slot + period;
+5 -2
View File
@@ -1,5 +1,6 @@
// Ported from MacNativeGames games/totk/third_party/d2r_gfx/gfx/nvn/nvn_hmap.h @ b75be8cf (MIT, Copyright (c) 2026
// jhaizhou; origin d2r-mobile); Chinese duplicate comments dropped, code unchanged.
// jhaizhou; origin d2r-mobile); Chinese duplicate comments dropped, code unchanged but for
// nvn_hm_get's key compare (MX_EQ, memx.h).
//
// Open-addressing hash table from fixed-length byte keys -> pointers (linear probing + backward shift on deletion, no
// tombstones). The replay hot path uses it instead of a boxed dictionary: keys are not boxed, no allocation, a lookup
@@ -11,6 +12,8 @@
#include <stdlib.h>
#include <string.h>
#include "memx.h"
typedef struct { uint8_t *keys; void **vals; uint32_t klen, cap, n; } nvn_hmap; // vals[i] == NULL means an empty slot
static inline uint64_t nvn_hm_hash(const void *k, uint32_t len) {
@@ -25,7 +28,7 @@ static inline void *nvn_hm_get(const nvn_hmap *m, const void *k) {
if (!m->n) return NULL;
for (uint32_t i = (uint32_t)nvn_hm_hash(k, m->klen) & (m->cap - 1);; i = (i + 1) & (m->cap - 1)) {
if (!m->vals[i]) return NULL;
if (!memcmp(m->keys + (size_t)i * m->klen, k, m->klen)) return m->vals[i];
if (MX_EQ(m->keys + (size_t)i * m->klen, k, m->klen)) return m->vals[i];
}
}
static inline void *nvn_hm_put(nvn_hmap *m, const void *k, void *v);
+1
View File
@@ -919,6 +919,7 @@ int probe_nvn(void) {
if (child && *child) return run_as_child(child);
// The probe checks the null back end whatever TOTK_GPU says; the choice is made once, at the first object call.
((RtConfig *)rt_config())->gpu = "null";
rt_fpcr_threshold(&cpu); // the FP result check threshold of a fresh Cpu (cpu.h): a zeroed one is wrong, not slow
nvn_procs_register();
hostfn_seal();
kern_run_on_guest_main(probe_main);
+106
View File
@@ -0,0 +1,106 @@
// Short out-of-memory spells: a failed allocation waits for memory instead of ending the game.
//
// Windows refuses commit beyond the current limit while it grows a system-managed page file, and on some drivers device
// memory counts as process commit (vk_memory.c vk_mem_host_probe), so for a moment every allocation of the process can
// fail and the same request succeeds a little later. The failure sites (memory.c commit, vk_memory.c vk_staging,
// vk_texture.c create_tex, vk_exec_draw.c transient chunks, nvn_api_cmd.c realloc_wait) ask the Vulkan back end to give
// memory back (rt_oom_request: released textures and headroom blocks, at its next submit, where its locks are free) and
// retry for up to OOM_WAIT_MS before they end the game.
//
// TOTK_TEST_OOM=<site>:<n>[,<site>:<n>...] (sites commit, staging, image, chunk) fails the first n attempts at a site, so
// the wait can be tested without running out of memory; the probes arm it with rt_oom_force.
#include <stdlib.h>
#include <string.h>
#include "cfg.h"
#include "host.h"
#include "platform/platform.h"
#define OOM_WAIT_MS 30000u
#define OOM_STEP_MS 10u
#define OOM_REPORTS 8
#define OOM_REQUEST_EVERY 100 // retries between two release requests: a second, since a release only frees what is old enough
volatile int32_t rt_oom_req;
static const char *const site_name[RT_OOM_SITES] = {"commit", "staging", "image", "chunk"};
static volatile int32_t force_left[RT_OOM_SITES];
static volatile int32_t forced_parsed;
static PlatOnce force_once = PLAT_ONCE_INIT;
static void force_parse(void *arg) {
(void)arg;
const char *s = cfg_get("TOTK_TEST_OOM");
while (s && *s) {
const char *colon = strchr(s, ':'), *end = strchr(s, ',');
if (!end) end = s + strlen(s);
if (colon && colon < end) {
for (int i = 0; i < RT_OOM_SITES; i++)
if ((size_t)(colon - s) == strlen(site_name[i]) && !strncmp(s, site_name[i], (size_t)(colon - s))) {
int n = atoi(colon + 1);
__atomic_store_n(&force_left[i], n > 0 ? n : 0, __ATOMIC_RELAXED);
rt_log("oom: TOTK_TEST_OOM fails the first %d %s attempts", n, site_name[i]);
}
}
s = *end ? end + 1 : end;
}
__atomic_store_n(&forced_parsed, 1, __ATOMIC_RELEASE);
}
int rt_oom_forced(int site) {
if (!__atomic_load_n(&forced_parsed, __ATOMIC_ACQUIRE)) plat_once(&force_once, force_parse, NULL);
if (__atomic_load_n(&force_left[site], __ATOMIC_RELAXED) <= 0) return 0;
return plat_dec32(&force_left[site]) >= 0;
}
void rt_oom_force(int site, int n) {
if (!__atomic_load_n(&forced_parsed, __ATOMIC_ACQUIRE)) plat_once(&force_once, force_parse, NULL);
__atomic_store_n(&force_left[site], n, __ATOMIC_RELAXED);
}
int rt_oom_forced_left(int site) {
int n = __atomic_load_n(&force_left[site], __ATOMIC_RELAXED);
return n > 0 ? n : 0;
}
void rt_oom_request(void) { __atomic_store_n(&rt_oom_req, 1, __ATOMIC_RELEASE); }
int rt_oom_wait(const char *what, uint64_t bytes, int (*retry)(void *arg), void *arg) {
rt_oom_request();
static volatile int32_t reports;
int report = plat_inc32(&reports) <= OOM_REPORTS;
double proc, used, limit;
if (report) {
plat_commit_info(&proc, &used, &limit);
rt_log("%s of %llu bytes failed (memory commit: this process %.0f MB, the system %.0f of %.0f MB); waiting up "
"to %u s for memory", what, (unsigned long long)bytes, proc, used, limit, OOM_WAIT_MS / 1000);
}
uint64_t t0 = plat_now_ms();
for (int n = 1; plat_now_ms() - t0 < OOM_WAIT_MS; n++) {
plat_sleep_ms(OOM_STEP_MS);
if (retry(arg)) {
if (report)
rt_log("%s of %llu bytes succeeded after %llu ms", what, (unsigned long long)bytes,
(unsigned long long)(plat_now_ms() - t0));
return 1;
}
if (n % OOM_REQUEST_EVERY == 0) rt_oom_request();
}
plat_commit_info(&proc, &used, &limit);
rt_log("%s of %llu bytes still failed after %u s (memory commit: this process %.0f MB, the system %.0f of %.0f MB)",
what, (unsigned long long)bytes, OOM_WAIT_MS / 1000, proc, used, limit);
return 0;
}
typedef struct { void *old, *p; size_t n; } Realloc;
static int realloc_try(void *arg) {
Realloc *r = arg;
return (r->p = realloc(r->old, r->n)) != NULL;
}
void *rt_realloc_wait(void *old, size_t n, const char *what) {
void *p = realloc(old, n);
if (p || !n) return p;
Realloc r = {old, NULL, n};
return rt_oom_wait(what, n, realloc_try, &r) ? r.p : NULL;
}
+111
View File
@@ -0,0 +1,111 @@
// Recall and Ultrahand target check (patch set "ability", recomp/patches-v143.txt and patches-v121.txt).
//
// main+0x1bd0138 on 1.4.3 (main+0x1622b60 on 1.2.1, the same code and actor offsets) runs while an ability picks its
// target. Once the owner's request says ready, it loads the result holder from the owner (x8 = [x19+0x6b0]), the
// target actor from the holder (x21 = [x8]), links the actor (main+0x155ec: reads the actor's +0x10, +0x24 and
// +0x1a8), reads [[x21+0x228]+0xb0] (main+0x9a89d4) and takes a reference on that (main+0x2b8464: an atomic
// read-modify-write of the word at +0x90). The holder can be reused memory by then, and the game crashes reading
// 0xffffffffffffffff (Windows' report of a non-canonical address) minutes to hours into play. UltraTears
// (kuraeow/ultratears, "ability target guard") found the class; this code is ours.
//
// A target is dropped only where the game's own code would fault on it: the holder or the actor unreadable, a link
// record the game follows unreadable or read-only, the component pointer unreadable, or the reference word
// unreadable or in a module's code or read-only data. Every
// other target goes on exactly as the game reads it, so no target the game can handle is ever lost. UltraTears' test
// of a live actor (its link record [actor+0x1a8] points back to it at +0x40 and carries its id at +0x48) is not in the
// game's code: the game links an actor whose link record is null without complaint. It runs here as a log line only,
// to show in players' logs whether it would ever reject a live target.
//
// The first site replaces `ldr x21, [x8]` with a call here; the second, at the game's `cbz x21` right after it, becomes
// `mov x21, x9; cbz x21`. The answer travels in x9 because the lifted caller keeps x21 (callee-saved) in a C local
// across the call and reloads only caller-saved registers; x9 is dead there on every path. x9 gets the holder's first
// word, or 0 for a dropped target, which the cbz skips for this frame as it skips an empty one. A null or tagged
// (bit 0) word goes back unchanged: the game skips those itself. No other register changes.
// Only an ability's target pick reaches this (hundreds of calls an hour), so the memory queries cost nothing.
#include <stdint.h>
#include "host.h"
#if __has_include("patches.h")
#include "patches.h"
#endif
#ifdef RT_PATCH_PAGE
#define HOST_ABILITY_TARGET (RT_PATCH_PAGE + 0xf60)
#define ACTOR_SPAN 0x230 // the actor fields the function and its callees read: +0x10 .. +0x22f
static int readable(uint64_t a, uint64_t n) { return a && !(a & 3) && mem_guest_readable(a, n); }
static uint64_t u64_at(uint64_t a) { return *(volatile uint64_t *)(uintptr_t)a; }
static uint32_t u32_at(uint64_t a) { return *(volatile uint32_t *)(uintptr_t)a; }
// [a, a+4) inside a module's code or read-only data: a write there faults.
static int read_only_module(uint64_t a) {
for (int i = 0; i < ldr_count(); i++) {
const RtLoaded *m = ldr_module(i);
if (a - m->base < m->size) return a - m->base < m->data_off;
}
return 0;
}
// NULL when the game's code can follow the actor without a fault, else what would fault.
static const char *faults(uint64_t actor) {
if (!readable(actor, ACTOR_SPAN)) return "actor not readable";
// main+0x155ec (1.2.1: main+0x809ecc) follows a non-null link record unless the actor's state word +0x24 is 7..11:
// it stores the record in the owner, updates +0x50 atomically (main+0x15838) and later compares +0x40 and +0x48.
uint64_t link = u64_at(actor + 0x1a8);
if (link && u32_at(actor + 0x24) - 7 >= 5 &&
(!readable(link + 0x40, 0x18) || read_only_module(link + 0x50)))
return "link record not writable";
uint64_t comp = u64_at(actor + 0x228);
if (!comp) return NULL; // main+0x9a89d4 returns 0 for it and the game stops there
if (!readable(comp + 0xb0, 8)) return "component not readable";
uint64_t ref = u64_at(comp + 0xb0);
if (!ref) return NULL; // the game's cbz x0 skips the reference
if (!readable(ref + 0x90, 4) || read_only_module(ref + 0x90)) return "reference word not writable";
return NULL;
}
// UltraTears' live-actor test, for the log: NULL when it passes.
static const char *not_live(uint64_t actor) {
uint64_t link = u64_at(actor + 0x1a8);
if (!readable(link + 0x40, 0x10)) return "link record not readable";
if (u64_at(link + 0x40) != actor) return "link record points elsewhere";
if (u32_at(link + 0x48) != u32_at(actor + 0x10)) return "link record has another id";
return NULL;
}
static void host_ability_target(Cpu *c) {
static uint64_t checked, dropped, mismatched;
uint64_t holder = c->x[8], actor = 0;
const char *why = NULL;
if (!readable(holder, 8)) why = "holder not readable";
else if ((actor = u64_at(holder)) && !(actor & 1)) why = faults(actor);
if (why) {
c->x[9] = 0;
uint64_t n = __atomic_add_fetch(&dropped, 1, __ATOMIC_RELAXED);
if (n <= 20 || !(n & (n - 1)))
rt_log("ability: target dropped, the game would fault on it (%s): holder %#llx actor %#llx; %llu dropped",
why, (unsigned long long)holder, (unsigned long long)actor, (unsigned long long)n);
return;
}
c->x[9] = actor;
if (!actor || (actor & 1)) return;
uint64_t k = __atomic_add_fetch(&checked, 1, __ATOMIC_RELAXED);
const char *odd = not_live(actor);
if (!odd) {
if (k == 1) rt_log("ability: first target checked (TOTK_ABILITY_GUARD=0: no check)");
return;
}
uint64_t n = __atomic_add_fetch(&mismatched, 1, __ATOMIC_RELAXED);
if (n <= 20 || !(n & (n - 1)))
rt_log("ability: target kept although the live-actor test fails (%s): actor %#llx; %llu of %llu targets", odd,
(unsigned long long)actor, (unsigned long long)n, (unsigned long long)k);
}
__attribute__((constructor)) static void ability_register(void) {
rt_register_host(HOST_ABILITY_TARGET, host_ability_target);
}
#endif
+2
View File
@@ -12,6 +12,7 @@
#include <string.h>
#include "cfg.h"
#include "gfx.h"
#include "host.h"
#include "input.h"
#include "ov.h"
@@ -93,6 +94,7 @@ static void npad_states(Cpu *c, uint32_t want) {
if (live) {
s.attributes = ATTR_CONNECTED;
input_poll(&s.buttons, &s.lx, &s.ly, &s.rx, &s.ry);
vk_reflex_marker(6, vk_reflex_frame()); // VK_LATENCY_MARKER_INPUT_SAMPLE_NV
}
*out = s;
ret_x(c, 1);
+4
View File
@@ -9,6 +9,7 @@
#include <opus/opus.h>
#include "host.h"
#include "audio_cat.h"
#include "platform/platform.h"
#define CODEC_R_INVALID 0x1fcd1u // MacNativeGames' "invalid argument" result for nn::codec
@@ -153,6 +154,7 @@ static void ov_decode(Cpu *c) {
int got = opus_decode(d->dec, in + 8, (opus_int32)n, out, (int)capacity, 0);
opus_uint32 final = 0;
if (got >= 0) opus_decoder_ctl(d->dec, OPUS_GET_FINAL_RANGE(&final));
int channels = d->channels;
release(d);
if (got < 0) {
rt_log("nn::codec: opus_decode: %s", opus_strerror(got));
@@ -163,6 +165,8 @@ static void ov_decode(Cpu *c) {
rt_log("nn::codec: Opus final range %#x != stream %#x (logged once)", (unsigned)final, (unsigned)range);
*consumed = 8 + n;
*samples = got;
if (got > 0 && audio_cat_active()) // the PCM's sound category for the music / effects / voice volumes
audio_cat_on_opus_decode(guest, (uint64_t)(uintptr_t)in, (uint64_t)(uintptr_t)out, (uint64_t)got * (uint64_t)channels * sizeof(int16_t));
ret_x(c, 0);
}
+44 -1
View File
@@ -618,6 +618,43 @@ static void probe_feedback(uint32_t pid, uint32_t style) {
near3(ms[0].dir, 1, 0, 0, 0.01),
"fusion restart: %d states in the frame of the Start, %d after one sample (sampling %lld, angle %.4f rev)", same,
next, (long long)ms[0].sampling, (double)ms[0].angle[2]);
// Gyro bias calibration on the private instance (not written to totk.ini): a large steady bias with resting noise
// is measured and then subtracted, so a resting controller reads no rotation and its yaw holds; samples that swing
// (the controller moved) are rejected and leave the measured bias in effect; an attempt without samples times out.
const double bias[3] = {-0.031, -0.011, -0.141};
double g[3];
motion_calibrate_start(m, "probe pad", t);
for (int i = 0; i < 375; i++) {
for (int j = 0; j < 3; j++) g[j] = bias[j] + (i & 1 ? 0.01 : -0.01);
motion_feed(m, down, g, t += 4000000);
}
int done = motion_calib_poll(m, t);
for (int i = 0; i < 250; i++) motion_feed(m, down, bias, t += 4000000);
motion_read(m, ms, 1, t + 1000000);
CHECK(done == MOTION_CALIB_DONE && near3(ms[0].gyro, 0, 0, 0, 1e-4) && fabs(ms[0].angle[2]) < 1e-3 &&
near3(ms[0].dir, 1, 0, 0, 0.001),
"calibration: state %d, resting gyro %.5f %.5f %.5f rev/s, yaw %.5f rev after 1 s", done,
(double)ms[0].gyro[0], (double)ms[0].gyro[1], (double)ms[0].gyro[2], (double)ms[0].angle[2]);
motion_calibrate_start(m, "probe pad", t);
for (int i = 0; i < 375; i++) {
for (int j = 0; j < 3; j++) g[j] = bias[j] + (j == 0 && (i & 1) ? 0.2 : 0);
motion_feed(m, down, g, t += 4000000);
}
int moved = motion_calib_poll(m, t);
motion_feed(m, down, bias, t += 4000000);
motion_read(m, ms, 1, t + 1000000);
CHECK(moved == MOTION_CALIB_MOVED && near3(ms[0].gyro, 0, 0, 0, 1e-4),
"calibration of a moving controller: state %d, resting gyro %.5f %.5f %.5f rev/s", moved,
(double)ms[0].gyro[0], (double)ms[0].gyro[1], (double)ms[0].gyro[2]);
motion_bias_reset(m);
motion_calibrate_start(m, "probe pad", t);
int waiting = motion_calib_poll(m, t + 1000000000ull), failed = motion_calib_poll(m, t + 6000000000ull);
motion_feed(m, down, bias, t += 4000000);
motion_read(m, ms, 1, t + 1000000);
CHECK(waiting == MOTION_CALIB_COLLECTING && failed == MOTION_CALIB_FAILED &&
fabs(ms[0].gyro[2] - bias[2] / (2 * M_PI)) < 1e-6,
"calibration without samples: state %d after 1 s, %d after 6 s; after a reset the raw gyro passes", waiting,
failed);
printf("hid feedback: vibration handles %#x %#x, six-axis %#x, at rest when stopped; fusion quarter turn %.4f rev\n",
vh[0], vh[1], sh[0], quarter);
}
@@ -929,8 +966,14 @@ static void probe_menu_text(void) {
cfg_set("TOTK_MENU_LANG", "zh-Hans");
CHECK(menu_text(UI_MENU_TITLE) == ui_text_in("zh-Hans", UI_MENU_TITLE), "menu_text ignores MENU_LANG=zh-Hans");
cfg_set("TOTK_MENU_LANG", "");
// the system language as ui_text.c resolve_menu_lang takes it: the tag, else its base language (ru-RU: ru), else en
const char *sys_lang = ui_text_lang(plat_ui_language());
const char *auto_lang = ui_text_defined(sys_lang, UI_MENU_TITLE) ? sys_lang : "en";
char sys_base[16] = "";
size_t base_n = strcspn(sys_lang, "-");
if (sys_lang[base_n] && base_n < sizeof sys_base) memcpy(sys_base, sys_lang, base_n);
const char *auto_lang = ui_text_defined(sys_lang, UI_MENU_TITLE) ? sys_lang
: *sys_base && ui_text_defined(sys_base, UI_MENU_TITLE) ? sys_base
: "en";
CHECK(menu_text(UI_MENU_CONTINUE) == ui_text_in(auto_lang, UI_MENU_CONTINUE), "menu_text auto ignores the system language");
}
+5
View File
@@ -20,6 +20,7 @@
#include "cfg.h"
#include "host.h"
#include "audio_cat.h"
#if __has_include("patches.h")
#include "patches.h"
@@ -235,6 +236,10 @@ static void decode(Cpu *c, uint64_t dict, uint64_t dict_size, uint64_t ddict, ui
}
c->x[0] = r;
count(size, r, kern_now_ns() - t0);
// A sound bank (BARS) for the music / effects / voice volumes (audio_cat.c); its compressed bytes were tagged
// by the RomFS read, which gives the bank's category.
if (r >= 16 && !memcmp((const void *)(uintptr_t)dst, "BARS", 4) && audio_cat_active())
audio_cat_scan_bars(dst, (uint64_t)r, audio_cat_lookup(src));
}
static void host_multiframe(Cpu *c) { decode(c, c->x[5], c->x[6], c->x[7], GUEST_MULTIFRAME, 8); }
+9
View File
@@ -62,6 +62,15 @@ uint64_t perf_begin(void) { return kern_now_ns(); }
void perf_replay_thread(void) { prof_replay_tid = plat_thread_id(); }
void perf_main_thread(void) { prof_main_tid = plat_thread_id(); }
void perf_core_thread(int role) {
static const char *const names[] = {"game main", "game render", "vk replay", "vk recorder", "vk present"};
static int32_t said;
int r = plat_thread_perf_core();
if (r < 0 || role < 0 || role >= PCORE_N) return;
if (__atomic_fetch_or(&said, 1 << role, __ATOMIC_RELAXED) & (1 << role)) return;
rt_log("power: the %s thread %s the P-cores", names[role], r ? "prefers" : "could not select");
}
void perf_end(int kind, uint64_t t0) {
if (!t0) return;
uint32_t tid = plat_thread_id();
+196 -2
View File
@@ -34,6 +34,7 @@
#include <SDL3/SDL_clipboard.h>
#include "cfg.h"
#include "platform.h"
// The write-watch ABI of Linux 6.7 (uapi linux/fs.h and linux/userfaultfd.h), for headers older than it: the release
@@ -481,6 +482,14 @@ uint64_t plat_counter_hz(void) { return 1000000000u; }
// inherit it from the thread that creates them.
void plat_timer_1ms(void) { prctl(PR_SET_TIMERSLACK, 1000ul, 0, 0, 0); }
int plat_power_full(void) { return -1; }
int plat_perf_core_setup(char *why, size_t n) {
snprintf(why, n, "not done on Linux");
return 0;
}
int plat_thread_perf_core(void) { return -1; }
// clock_nanosleep wakes within the timer slack (50 us by default) of its deadline; the last PLAT_SPIN_NS are spent
// yielding so the deadline is met closely, as on Windows.
#define PLAT_SPIN_NS 100000u
@@ -949,6 +958,10 @@ static void fault_handler(int sig, siginfo_t *si, void *uc) {
}
if (sig == SIGSEGV || sig == SIGBUS)
c.access = (err & 16) ? 8 : (err & 2) ? 1 : 0, c.addr = (uint64_t)(uintptr_t)si->si_addr;
static const int gr[16] = {REG_RAX, REG_RCX, REG_RDX, REG_RBX, REG_RSP, REG_RBP, REG_RSI, REG_RDI,
REG_R8, REG_R9, REG_R10, REG_R11, REG_R12, REG_R13, REG_R14, REG_R15};
for (int i = 0; i < 16; i++) c.gpr[i] = (uint64_t)u->uc_mcontext.gregs[gr[i]];
c.has_regs = 1;
crash_fn(&c);
}
signal(sig, SIG_DFL); // returns into the faulting instruction, which faults again with the default action
@@ -1238,7 +1251,135 @@ const char *plat_user_locale(void) {
return tag;
}
// Noto CJK as the common distributions and SteamOS install it; one collection covers every CJK language.
// A CJK font for the menu: the file that draws this language on this system.
//
// The languages the catalog has (ja, ko, zh-Hans, zh-Hant) are not covered by the font built into the menu, so their
// glyphs are merged in from a system file. Distributions disagree on where Noto CJK lives, and a sandboxed build
// (Flatpak, Snap, an AppImage on a host whose fonts are mounted at /run/host/fonts) may have no Noto CJK where the
// fixed paths expect it while the host still has CJK fonts that would do. So ask fontconfig first: it is the database
// Tk/Xft and every other Linux toolkit reads, it knows /app/share/fonts, the XDG data dirs, /run/host/fonts and the
// per-user dirs, and adding a font there is enough -- the menu does not need to learn about it. The fixed
// distribution paths stay as the fallback for a system without fontconfig, and TOTK_MENU_FONT overrides both.
//
// fontconfig is loaded with dlopen rather than linked, so a host without libfontconfig (a bare container) still builds
// and runs and just takes the fallback. The runtime already links -ldl for the NVIDIA libraries.
static int cjk_lang(const char *lang) {
return !strcmp(lang, "ja") || !strcmp(lang, "ko") || !strcmp(lang, "zh-Hans") || !strcmp(lang, "zh-Hant");
}
// The fontconfig language tag of a catalog language. GL says zh-Hans/zh-Hant, but fontconfig and OpenType name the
// faces zh-cn/zh-tw (Noto Sans CJK SC is lang=zh-cn), and a pattern naming only zh-hans does not reach it.
static const char *cjk_lang_tag(const char *lang) {
if (!strcmp(lang, "ja")) return "ja";
if (!strcmp(lang, "ko")) return "ko";
if (!strcmp(lang, "zh-Hans")) return "zh-cn";
return "zh-tw";
}
// The handful of fontconfig calls used here (2.0 ABI, unchanged since). Loaded once, kept for the process: the menu
// rebuilds its atlas when the window height changes and must not dlopen on every rebuild.
typedef struct _FcPattern FcPattern;
typedef struct _FcCharSet FcCharSet;
typedef struct _FcConfig FcConfig;
typedef struct {
int state; // 0 untried, 1 usable, -1 no fontconfig on this system
FcPattern *(*NameParse)(const unsigned char *);
void (*PatternDestroy)(FcPattern *);
int (*PatternAddCharSet)(FcPattern *, const char *, const FcCharSet *);
int (*PatternGetString)(const FcPattern *, const char *, int, unsigned char **);
FcCharSet *(*CharSetCreate)(void);
int (*CharSetAddChar)(FcCharSet *, unsigned int);
void (*CharSetDestroy)(FcCharSet *);
FcPattern *(*FontMatch)(FcConfig *, FcPattern *, int *); // 3 args: (config, pattern, FcResult *)
int (*PatternGetCharSet)(const FcPattern *, const char *, int, FcCharSet **);
int (*CharSetHasChar)(const FcCharSet *, unsigned int);
FcConfig *(*ConfigGetCurrent)(void);
int (*Init)(void);
} Fc;
static Fc fc;
static int fc_load(void) {
if (fc.state) return fc.state > 0;
void *lib = plat_lib_open("libfontconfig.so.1");
if (!lib) lib = plat_lib_open("libfontconfig.so");
if (!lib) { fc.state = -1; return 0; }
fc.NameParse = (FcPattern *(*)(const unsigned char *))plat_lib_sym(lib, "FcNameParse");
fc.PatternDestroy = (void (*)(FcPattern *))plat_lib_sym(lib, "FcPatternDestroy");
fc.PatternAddCharSet = (int (*)(FcPattern *, const char *, const FcCharSet *))plat_lib_sym(lib, "FcPatternAddCharSet");
fc.PatternGetString = (int (*)(const FcPattern *, const char *, int, unsigned char **))plat_lib_sym(lib, "FcPatternGetString");
fc.CharSetCreate = (FcCharSet *(*)(void))plat_lib_sym(lib, "FcCharSetCreate");
fc.CharSetAddChar = (int (*)(FcCharSet *, unsigned int))plat_lib_sym(lib, "FcCharSetAddChar");
fc.CharSetDestroy = (void (*)(FcCharSet *))plat_lib_sym(lib, "FcCharSetDestroy");
fc.FontMatch = (FcPattern *(*)(FcConfig *, FcPattern *, int *))plat_lib_sym(lib, "FcFontMatch");
fc.PatternGetCharSet = (int (*)(const FcPattern *, const char *, int, FcCharSet **))plat_lib_sym(lib, "FcPatternGetCharSet");
fc.CharSetHasChar = (int (*)(const FcCharSet *, unsigned int))plat_lib_sym(lib, "FcCharSetHasChar");
fc.ConfigGetCurrent = (FcConfig *(*)(void))plat_lib_sym(lib, "FcConfigGetCurrent");
fc.Init = (int (*)(void))plat_lib_sym(lib, "FcInit");
if (!fc.NameParse || !fc.PatternDestroy || !fc.PatternAddCharSet || !fc.PatternGetString || !fc.CharSetCreate ||
!fc.CharSetAddChar || !fc.CharSetDestroy || !fc.FontMatch || !fc.ConfigGetCurrent ||
!fc.PatternGetCharSet || !fc.CharSetHasChar) {
fc.state = -1;
return 0;
}
// FcInit loads the configuration and the font set; FcConfigGetCurrent is NULL before it, and FcFontMatch asserts
// on a NULL config. Idempotent, so the second call of a rebuild costs a flag check.
if (fc.Init) fc.Init();
if (!fc.ConfigGetCurrent()) {
fc.state = -1;
return 0;
}
fc.state = 1;
return 1;
}
// The file fontconfig picks for this language, or 0. Asks for the generic CJK-capable family with the language set and
// one representative character of that language in the charset, so a Latin-only or Japanese face cannot win by default.
static int fc_font_for(const char *lang, char *out, size_t cap) {
static const struct { const char *lang; unsigned int cp; } probe[] = {
{"ja", 0x65E5}, // 日
{"ko", 0xD55C}, // 한
{"zh-Hans", 0x8BBE}, // 设
{"zh-Hant", 0x8A2D}, // 設
};
unsigned int cp = 0;
for (size_t i = 0; i < sizeof probe / sizeof *probe; i++)
if (!strcmp(probe[i].lang, lang)) cp = probe[i].cp;
if (!cp || !fc_load()) return 0;
char query[64];
snprintf(query, sizeof query, "sans-serif:lang=%s", cjk_lang_tag(lang));
FcPattern *pat = fc.NameParse((const unsigned char *)query);
if (!pat) return 0;
FcCharSet *cs = fc.CharSetCreate();
int ok = 0;
if (cs) {
fc.CharSetAddChar(cs, cp);
if (fc.PatternAddCharSet(pat, "charset", cs)) {
int result = 0; // FcResult; FcFontMatch asserts when this pointer is NULL
FcPattern *m = fc.FontMatch(fc.ConfigGetCurrent(), pat, &result);
unsigned char *file = NULL;
if (m) {
// fontconfig returns the closest pattern even when nothing covers the character, so check the charset
// before trusting it: without this a Latin-only system hands back DejaVu Sans and the menu merges no
// glyphs while believing it did.
FcCharSet *got = NULL;
if (result == 0 /* FcResultMatch */ &&
fc.PatternGetCharSet(m, "charset", 0, &got) == 0 && got && fc.CharSetHasChar(got, cp) &&
fc.PatternGetString(m, "file", 0, &file) == 0 && file)
ok = snprintf(out, cap, "%s", (const char *)file) < (int)cap;
fc.PatternDestroy(m);
}
}
fc.CharSetDestroy(cs);
}
fc.PatternDestroy(pat);
return ok;
}
// See platform.h. TOTK_MENU_FONT names a file to use for every CJK language; then fontconfig; then the fixed paths,
// which are the normal layout on SteamOS and the distributions that predate fontconfig's own lookup.
int plat_font_path(const char *lang, char *out, size_t cap) {
static const char *files[] = {
"/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc",
@@ -1247,7 +1388,14 @@ int plat_font_path(const char *lang, char *out, size_t cap) {
"/usr/share/fonts/noto/NotoSansCJK-Regular.ttc",
"/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc",
};
if (strcmp(lang, "ja") && strcmp(lang, "ko") && strcmp(lang, "zh-Hans") && strcmp(lang, "zh-Hant")) return 0;
if (!cjk_lang(lang)) return 0;
char override[PATH_MAX];
if (plat_getenv("TOTK_MENU_FONT", override, sizeof override) && plat_file_exists(override))
return snprintf(out, cap, "%s", override) < (int)cap;
// menu_font_fontconfig=0 skips the fontconfig lookup and keeps the five fixed paths, for a player whose fontconfig
// resolves to a face the font parser cannot read. Read through cfg_bool like every other switch, so totk.ini and
// the TOTK_ variable both work and the menu can grey it out.
if (cfg_bool("TOTK_MENU_FONT_FONTCONFIG") && fc_font_for(lang, out, cap) && plat_file_exists(out)) return 1;
for (size_t i = 0; i < sizeof files / sizeof *files; i++)
if (plat_file_exists(files[i])) return snprintf(out, cap, "%s", files[i]) < (int)cap;
return 0;
@@ -1456,6 +1604,41 @@ int plat_open_folder(const char *path) {
void *plat_lib_open(const char *name) { return dlopen(name, RTLD_NOW | RTLD_LOCAL); }
void *plat_lib_sym(void *lib, const char *name) { return lib ? dlsym(lib, name) : NULL; }
void plat_lib_error(char *out, size_t cap) {
const char *e = dlerror();
snprintf(out, cap, "%s", e ? e : "unknown error");
}
int plat_ngx_core_path(char *out, size_t cap) { return snprintf(out, cap, "libnvidia-ngx.so.1") < (int)cap; }
// UTF-8 to UTF-32 wchar_t, without the C library's locale (the runtime never sets one, so mbstowcs would stop at the
// first non-ASCII byte). Returns 0 on malformed input or when out has no room.
static int utf8_to_wide(const char *in, wchar_t *out, size_t cap) {
const unsigned char *u = (const unsigned char *)in;
size_t n = 0;
while (*u) {
unsigned c = *u++, more = c < 0x80 ? 0 : c >= 0xF0 ? 3 : c >= 0xE0 ? 2 : c >= 0xC0 ? 1 : 4;
if (more == 4) return 0;
if (more) c &= 0x3F >> more;
for (unsigned k = 0; k < more; k++, u++) {
if ((*u & 0xC0) != 0x80) return 0;
c = c << 6 | (*u & 0x3F);
}
if (n + 1 >= cap) return 0;
out[n++] = (wchar_t)c;
}
out[n] = 0;
return 1;
}
int plat_exe_dir_wide(wchar_t *out, size_t cap) {
char dir[PLAT_PATH_MAX];
char *cut = plat_exe_path(dir, sizeof dir) ? plat_last_sep(dir) : NULL;
if (!cut) return 0;
cut[1] = 0;
return utf8_to_wide(dir, out, cap);
}
// ---- machine load: /proc/stat and /proc/self/stat for the CPU, hwmon for the temperature, the amdgpu sysfs files of
// the render device for the GPU.
@@ -1626,6 +1809,17 @@ void plat_commit_info(double *proc_mb, double *used_mb, double *limit_mb) {
if (private_bytes) *proc_mb = (double)(private_bytes >> 20);
}
void plat_mem_room(uint64_t *commit_room, uint64_t *phys_room) {
*commit_room = *phys_room = UINT64_MAX;
char line[128];
unsigned long long k;
FILE *f = fopen("/proc/meminfo", "r");
if (!f) return;
while (fgets(line, sizeof line, f))
if (sscanf(line, "MemAvailable: %llu kB", &k) == 1) *phys_room = (uint64_t)k << 10;
fclose(f);
}
// ---- child processes
struct PlatChild {
+37 -6
View File
@@ -99,6 +99,12 @@ uint64_t plat_now_ns(void); // monotonic, high resolution, arbitra
uint64_t plat_counter(void); // the OS's raw monotonic counter (QPC on Windows), plat_counter_hz per second
uint64_t plat_counter_hz(void);
void plat_timer_1ms(void); // asks the OS for timed waits accurate to 1 ms or better (call before threads)
int plat_power_full(void); // opts the process out of OS power throttling: 1 done, 0 refused, -1 no such thing
// Hybrid CPUs: finds the performance-core logical processors the process may use, once, before any thread asks; returns
// their count (0: none to prefer, why says which case). plat_thread_perf_core: the calling thread prefers them; 1 done,
// 0 refused, -1 nothing selected.
int plat_perf_core_setup(char *why, size_t n);
int plat_thread_perf_core(void);
// t * num / den for a fixed num / den without a 128-bit division per call (__udivti3 took 5% of the game's running
// samples through plat_now_ns and rt_ticks): t * q + (t * f >> 64) with q = num / den and f = (num % den) * 2^64 / den.
// Monotonic in t and at most one below the exact quotient while t * (num % den) / den fits 64 bits.
@@ -222,9 +228,10 @@ _Noreturn void plat_reexec(void);
// - A fault in this executable's own code (an access violation, an illegal instruction, a division by zero, a stack
// overflow) is reported as it happens.
// - A fault in another module (a GPU driver, a system library) is reported only when nothing handles it: on Windows
// from the unhandled-exception filter, with the return addresses in this executable on the faulting thread's stack,
// so the report shows which host call ran into the driver; on Linux from the signal handler, without frames. Drivers
// that raise and catch their own exceptions never reach fn.
// from the unhandled-exception filter, with the return addresses on the faulting thread's stack in every module, so
// the report shows which driver code failed and which host call ran into it; on Linux from the signal handler,
// without frames. Drivers that raise and catch their own exceptions never reach fn. For a C++ exception thrown by
// MSVC-built code (a driver) the report names its type and, for a std::exception, its what() text.
// - On Windows abort() (a failed assert, std::terminate) and an invalid parameter to a C runtime function go there
// too, with the host frames in this executable: the C runtime ends the process for both with 0xc0000409 and no
// exception.
@@ -234,10 +241,19 @@ typedef struct {
uintptr_t rip, offset; // the faulting instruction, and its offset in its module
int access; // the data access that faulted: 0 read, 1 write, 8 execute; -1 when not an access
uint64_t addr; // its address when access >= 0
int nframes; // return addresses in this executable, innermost first: for abort and invalid parameter
uintptr_t frames[16]; // (rip is frames[0]) and for a fault in another module; 0 for a fault in this one
int nframes; // return addresses, innermost first: for abort and invalid parameter (rip is frames[0])
uintptr_t frames[32]; // and for a fault in another module; 0 for a fault in this one
const char *frame_module[32]; // the module of each frame: NULL for this executable, "" for none
uintptr_t frame_offset[32]; // each frame's offset in its module
int frames_left_out; // frames in other modules past the limit the walk keeps, so this executable's still fit
const char *module; // the module holding rip: NULL for this executable, "" for none (a jump to address 0)
const char *cxx_type; // a C++ exception from MSVC-built code: the thrown type's decorated name (.?AV...@@)
const char *cxx_what; // and what() when the type derives from std::exception; NULL when unknown
int has_regs; // gpr holds the faulting thread's registers (a fault; not abort or invalid parameter)
uint64_t gpr[16]; // rax rcx rdx rbx rsp rbp rsi rdi r8..r15, in the x86 encoding order
} PlatCrash;
#define PLAT_GPR_NAMES {"rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi", \
"r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"}
void plat_crash_hook(void (*fn)(const PlatCrash *c));
int plat_mem_read(uint64_t addr, void *out, size_t n); // copies n bytes, 0 instead of faulting on unmapped memory
int plat_screen_size(int *w, int *h); // the primary display's current mode in pixels; 0 unknown
@@ -371,7 +387,9 @@ const char *plat_ui_language(void);
const char *plat_user_locale(void);
uint32_t plat_thread_id(void); // id of the calling thread, for unique file names
// A system font file with the glyphs of a catalog language ("ja", "ko", "zh-Hans", "zh-Hant"): the menu shows each
// language's name in its own script. Copies the UTF-8 path into out; 0 when the system has none.
// language's name in its own script. Copies the UTF-8 path into out; 0 when the system has none. On Linux the file is
// the one fontconfig picks for that language (so a sandbox that installs a font where its fontconfig finds it needs no
// code change), with TOTK_MENU_FONT overriding it and the usual distribution paths as the fallback.
int plat_font_path(const char *lang, char *out, size_t cap);
// The operating system and its build ("Windows 10.0.28000") into out; 0 when unknown.
int plat_os_version(char *out, size_t cap);
@@ -387,6 +405,16 @@ int plat_open_folder(const char *path); // shows the folder in
// in the system directories only. NULL when missing.
void *plat_lib_open(const char *name);
void *plat_lib_sym(void *lib, const char *name);
// Why the calling thread's last plat_lib_open failed, for a log line (the OS error code on Windows, dlerror on Linux).
void plat_lib_error(char *out, size_t cap);
// ---- NVIDIA NGX (vk_dlaa.c). The path of the driver's NGX core library into out: on Windows _nvngx.dll in the folder
// the driver records in the registry (NGXCore FullPath), on Linux libnvidia-ngx.so.1, which the dynamic loader finds
// where the driver put it. 0 when the driver records none.
int plat_ngx_core_path(char *out, size_t cap);
// The running executable's folder with a trailing separator, as the wchar_t string NGX takes for its application data
// path (UTF-16 on Windows, UTF-32 code points on Linux). 0 on failure or when out has no room.
int plat_exe_dir_wide(wchar_t *out, size_t cap);
// ---- machine load for the performance overlay (perfmon.c's sampling thread; each call can take milliseconds). A number
// the system does not give is -1.
@@ -407,6 +435,9 @@ void plat_mem_info(float *used_mb, float *total_mb, float *proc_mb);
// Commit charge in MB: this process's private bytes, the system's commit in use and its limit (RAM plus page files).
// An allocation fails when the system's reaches the limit, however much physical memory is free. -1 when unknown.
void plat_commit_info(double *proc_mb, double *used_mb, double *limit_mb);
// What the system can still give, in bytes: commit (Windows: limit minus charge; Linux, which overcommits: UINT64_MAX)
// and physical memory available without paging. UINT64_MAX when unknown.
void plat_mem_room(uint64_t *commit_room, uint64_t *phys_room);
// This process's memory in bytes: resident (the working set) and private (Windows: committed private bytes; Linux: the
// resident anonymous pages). 0 when unknown.
void plat_process_memory(uint64_t *resident, uint64_t *private_bytes);
+220 -11
View File
@@ -18,6 +18,13 @@
#include "platform.h"
// Hybrid laptops: the drivers read these exports of the executable when it starts and run it on the dedicated GPU.
// NVIDIA Optimus can otherwise hide its GPU from a program it put on the integrated one, whatever the Windows and NVIDIA
// settings say; AMD switchable graphics read the second one. TOTK_GPU_INDEX still picks another GPU. Savage Wind Recomp
// exports the same two (runtime/platform/windows.c).
__declspec(dllexport) DWORD NvOptimusEnablement = 1;
__declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
_Static_assert(sizeof(PlatMutex) == sizeof(SRWLOCK), "PlatMutex holds an SRWLOCK");
_Static_assert(sizeof(PlatCond) == sizeof(CONDITION_VARIABLE), "PlatCond holds a CONDITION_VARIABLE");
@@ -289,6 +296,98 @@ uint32_t plat_cpu_usable(void) {
return plat_cpu_count();
}
// ---- performance cores of a hybrid CPU
// An Intel CPU with P-cores and E-cores (a Core 5 210H: 4 P-cores with two threads each, 4 E-cores) runs a thread on an
// E-core at about 60% of a P-core's speed, and the frame waits for its slowest critical thread. The few threads every
// frame goes through ask for the P-cores (plat_thread_perf_core): their selected CPU sets are the logical processors
// above the lowest efficiency class that the process affinity allows. A thread's CPU sets bind it like an affinity
// mask, except that processors the system reserves for itself are left out instead of failing. Nothing is selected
// on a CPU with one efficiency class, or when the affinity already leaves only P-cores or only E-cores. LAS Recomp
// does the same for its game, render and vsync threads (runtime/platform/windows.c plat_thread_perf_core).
// Nothing is selected either below PC_MIN_CORES physical P-cores (an i5-1235U or Core 3 100U has 2): the five bound
// threads (perf_core_thread) and a spinning recorder would crowd two cores and their hyperthreads while the E-cores
// stay idle, which can be slower than letting the scheduler place them.
#define PC_MIN_CORES 4
static ULONG *pc_ids; // the CPU set ids of the allowed P-core logical processors; NULL: none selected
static ULONG pc_n;
int plat_perf_core_setup(char *why, size_t n) {
ULONG len = 0;
GetSystemCpuSetInformation(NULL, 0, &len, GetCurrentProcess(), 0);
uint8_t *buf = len ? malloc(len) : NULL;
if (!buf || !GetSystemCpuSetInformation((PSYSTEM_CPU_SET_INFORMATION)buf, len, &len, GetCurrentProcess(), 0)) {
free(buf);
snprintf(why, n, "no CPU set information (error %lu)", GetLastError());
return 0;
}
// The process affinity mask covers its first processor group only: it filters on a single-group system.
DWORD_PTR proc = 0, sys = 0;
int filter = GetActiveProcessorGroupCount() == 1 && GetProcessAffinityMask(GetCurrentProcess(), &proc, &sys) && proc;
int lo = 255, hi = -1, all = 0;
for (ULONG off = 0; off < len;) {
SYSTEM_CPU_SET_INFORMATION *i = (SYSTEM_CPU_SET_INFORMATION *)(buf + off);
if (!i->Size) break;
if (i->Type == CpuSetInformation) {
int ec = i->CpuSet.EfficiencyClass;
lo = ec < lo ? ec : lo, hi = ec > hi ? ec : hi, all++;
}
off += i->Size;
}
if (!all || hi <= lo) {
free(buf);
snprintf(why, n, "not a hybrid CPU (%d logical processors, one efficiency class)", all);
return 0;
}
ULONG *ids = calloc((size_t)all, sizeof *ids);
uint16_t *cores = calloc((size_t)all, sizeof *cores); // group << 8 | core index of each allowed physical P-core
int np = 0, ne = 0, ok_p = 0, ok_e = 0, nc = 0;
for (ULONG off = 0; ids && cores && off < len;) {
SYSTEM_CPU_SET_INFORMATION *i = (SYSTEM_CPU_SET_INFORMATION *)(buf + off);
if (!i->Size) break;
if (i->Type == CpuSetInformation) {
int perf = i->CpuSet.EfficiencyClass > lo;
int allowed = !filter || (i->CpuSet.Group == 0 && i->CpuSet.LogicalProcessorIndex < 64 &&
(proc >> i->CpuSet.LogicalProcessorIndex) & 1);
perf ? np++ : ne++;
if (allowed) perf ? ok_p++ : ok_e++;
if (allowed && perf) {
ids[pc_n++] = i->CpuSet.Id;
uint16_t key = (uint16_t)(i->CpuSet.Group << 8 | i->CpuSet.CoreIndex);
int k = 0;
while (k < nc && cores[k] != key) k++;
if (k == nc) cores[nc++] = key;
}
}
off += i->Size;
}
int counted = ids && cores;
free(buf);
free(cores);
if (!counted || !ok_p || !ok_e) {
free(ids);
pc_n = 0;
snprintf(why, n, "hybrid CPU, %d P-core + %d E-core logical processors; the affinity allows %d + %d, so it decides",
np, ne, ok_p, ok_e);
return 0;
}
if (nc < PC_MIN_CORES) {
free(ids);
pc_n = 0;
snprintf(why, n, "hybrid CPU, %d P-core + %d E-core logical processors on %d P-cores, fewer than %d", np, ne, nc,
PC_MIN_CORES);
return 0;
}
pc_ids = ids;
snprintf(why, n, "hybrid CPU, %d P-core + %d E-core logical processors%s", np, ne,
ok_p != np || ok_e != ne ? " (affinity-limited)" : "");
return (int)pc_n;
}
int plat_thread_perf_core(void) {
if (!pc_ids) return -1;
return SetThreadSelectedCpuSets(GetCurrentThread(), pc_ids, pc_n) ? 1 : 0;
}
// PLAT_PRIO_* are the THREAD_PRIORITY_* values.
void plat_thread_priority(int prio) { SetThreadPriority(GetCurrentThread(), prio); }
void plat_thread_set_priority(PlatThread *t, int prio) { SetThreadPriority(t->h, prio); }
@@ -319,6 +418,17 @@ uint64_t plat_counter_hz(void) { return qpc_freq(); }
// The default timer tick is 15.6 ms; a 1 ms wait would cost a full tick.
void plat_timer_1ms(void) { timeBeginPeriod(1); }
// Windows 11 runs the threads of a process it deems in the background at EcoQoS (on the E-cores of a hybrid Intel CPU,
// at a low clock) and ignores its timer resolution while its window is hidden. The game is always latency-bound, so it
// opts out of both. Windows before 10 1709 does not know the class: 0.
int plat_power_full(void) {
PROCESS_POWER_THROTTLING_STATE s = {
.Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION,
.ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED | PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION,
.StateMask = 0};
return SetProcessInformation(GetCurrentProcess(), ProcessPowerThrottling, &s, sizeof s) ? 1 : 0;
}
static PlatScale qpc_ns; // set before main, so no thread reads it half written
__attribute__((constructor)) static void qpc_ns_init(void) { qpc_ns = plat_scale(1000000000u, qpc_freq()); }
@@ -626,8 +736,21 @@ void plat_mem_fault_hook(int (*fn)(uint64_t addr)) {
static void (*crash_fn)(const PlatCrash *c);
// The faulting thread's general registers, for the report to tie the faulting instruction to its operands.
static void regs_of(const CONTEXT *x, PlatCrash *c) {
const DWORD64 r[16] = {x->Rax, x->Rcx, x->Rdx, x->Rbx, x->Rsp, x->Rbp, x->Rsi, x->Rdi,
x->R8, x->R9, x->R10, x->R11, x->R12, x->R13, x->R14, x->R15};
for (int i = 0; i < 16; i++) c->gpr[i] = r[i];
c->has_regs = 1;
}
static LONG WINAPI crash_filter(EXCEPTION_POINTERS *ep);
static LONG CALLBACK crash_veh(EXCEPTION_POINTERS *ep) {
DWORD code = ep->ExceptionRecord->ExceptionCode;
// Heap corruption is raised by the heap manager, which then ends the process itself: it never reaches the
// unhandled-exception filter, so it is reported from here, wherever the heap was called from.
if (code == 0xc0000374u) return crash_filter(ep); // STATUS_HEAP_CORRUPTION
const char *what = code == EXCEPTION_ACCESS_VIOLATION ? "access violation"
: code == EXCEPTION_ILLEGAL_INSTRUCTION ? "illegal instruction"
: code == EXCEPTION_INT_DIVIDE_BY_ZERO ? "integer division by zero"
@@ -643,6 +766,7 @@ static LONG CALLBACK crash_veh(EXCEPTION_POINTERS *ep) {
PlatCrash c = {what, (uint32_t)code, rip, rip - (uintptr_t)mi.lpBaseOfDll, -1, 0};
if (code == EXCEPTION_ACCESS_VIOLATION && ep->ExceptionRecord->NumberParameters >= 2)
c.access = (int)ep->ExceptionRecord->ExceptionInformation[0], c.addr = ep->ExceptionRecord->ExceptionInformation[1];
regs_of(ep->ContextRecord, &c);
crash_fn(&c);
return EXCEPTION_CONTINUE_SEARCH;
}
@@ -674,16 +798,20 @@ static const char *image_name(const char *base) {
return base + ((const IMAGE_EXPORT_DIRECTORY *)(base + d->VirtualAddress))->Name;
}
// The return addresses in [lo, lo + size) on the crashed thread's stack, innermost first: unwinds the faulting context
// The return addresses on the crashed thread's stack, innermost first, with their modules: unwinds the faulting context
// with the x64 unwind tables; a function without a table entry (a leaf, or address 0 after a call through a null
// pointer) has its return address on top of the stack. The walk stops at return address 0 (the thread's first frame)
// and when the stack pointer leaves the thread's stack, so a damaged stack ends it instead of faulting.
static int frames_in(const CONTEXT *at, uintptr_t lo, uintptr_t size, uintptr_t *out, int max) {
// and when the stack pointer leaves the thread's stack, so a damaged stack ends it instead of faulting. A driver can
// nest deeper than the report holds, so frames in other modules stop being kept after the first 12 (counted in
// frames_left_out) and the frames in this executable, which name the host call that ran into the driver, still fit.
static void frames_of(const CONTEXT *at, PlatCrash *x) {
enum { MAX = sizeof x->frames / sizeof *x->frames, OTHER_MAX = 12 };
CONTEXT c = *at;
ULONG_PTR low, high;
GetCurrentThreadStackLimits(&low, &high);
int n = 0;
for (int i = 0; i < 256 && n < max && (c.Rip || !i) && c.Rsp >= low && c.Rsp + 8 <= high; i++) {
void *exe = GetModuleHandleW(NULL);
int other = 0;
for (int i = 0; i < 256 && x->nframes < MAX && (c.Rip || !i) && c.Rsp >= low && c.Rsp + 8 <= high; i++) {
DWORD64 img;
RUNTIME_FUNCTION *fe = RtlLookupFunctionEntry(c.Rip, &img, NULL);
if (fe) {
@@ -694,9 +822,64 @@ static int frames_in(const CONTEXT *at, uintptr_t lo, uintptr_t size, uintptr_t
c.Rip = *(const DWORD64 *)c.Rsp;
c.Rsp += 8;
}
if (c.Rip - lo < size) out[n++] = (uintptr_t)c.Rip;
if (!c.Rip) break;
void *base = NULL;
RtlPcToFileHeader((void *)(uintptr_t)c.Rip, &base);
if (base != exe && other++ >= OTHER_MAX) {
x->frames_left_out++;
continue;
}
int n = x->nframes++;
x->frames[n] = (uintptr_t)c.Rip;
x->frame_module[n] = base == exe ? NULL : base ? image_name(base) : "";
x->frame_offset[n] = (uintptr_t)c.Rip - (uintptr_t)base;
}
return n;
}
// MSVC's C++ throw (0xe06d7363) passes the thrown object in ExceptionInformation[1], its ThrowInfo in [2] and the image
// base the ThrowInfo's 32-bit offsets are relative to in [3] (magic 0x19930520..0x19930522 in [0]). The ThrowInfo lists
// the types a catch may match, the thrown type first; each type descriptor holds a vtable pointer, a spare pointer and
// the decorated name. A std::exception keeps its what() text in the pointer right after its vtable pointer (MSVC's
// __std_exception_data). Every read is checked: the exception may come from a damaged heap.
typedef struct { uint32_t attributes; int32_t unwind, forward_compat, catchable_types; } CxxThrowInfo;
typedef struct { uint32_t properties; int32_t type, this_mdisp, this_pdisp, this_vdisp, size, copy; } CxxCatchableType;
// Copies the string at p into buf (cut to n - 1 characters), checking each page before reading it; NULL when the first
// byte cannot be read.
static const char *copy_cstr(uintptr_t p, char *buf, size_t n) {
size_t i = 0;
for (; p && i + 1 < n; i++) {
if ((!i || !((p + i) & 4095)) && !plat_mem_readable(p + i, 1)) break;
if (!(buf[i] = ((const char *)p)[i])) return buf;
}
buf[i] = 0;
return i ? buf : NULL;
}
static void cxx_info(const EXCEPTION_RECORD *er, PlatCrash *x) {
if (er->NumberParameters < 4 || er->ExceptionInformation[0] - 0x19930520u > 2) return;
uintptr_t obj = er->ExceptionInformation[1], info = er->ExceptionInformation[2],
base = er->ExceptionInformation[3];
if (!info || !plat_mem_readable(info, sizeof(CxxThrowInfo))) return;
const CxxThrowInfo *ti = (const void *)info;
uintptr_t arr = base + (uintptr_t)(intptr_t)ti->catchable_types;
if (!ti->catchable_types || !plat_mem_readable(arr, 4)) return;
int32_t n = *(const int32_t *)arr;
if (n <= 0 || n > 64 || !plat_mem_readable(arr + 4, 4u * (uint64_t)n)) return;
static char type[128], what[256]; // the crash report runs once: crash_fn ends the process
int exception = 0;
for (int32_t i = 0; i < n; i++) {
uintptr_t ct = base + (uintptr_t)(intptr_t)((const int32_t *)(arr + 4))[i];
if (!plat_mem_readable(ct, sizeof(CxxCatchableType))) continue;
uintptr_t td = base + (uintptr_t)(intptr_t)((const CxxCatchableType *)ct)->type;
char name[64];
if (!copy_cstr(td + 16, i ? name : type, i ? sizeof name : sizeof type)) // past vtable and spare pointers
continue;
if (!i) x->cxx_type = type;
if (!strcmp(i ? name : type, ".?AVexception@std@@")) exception = 1;
}
if (exception && obj && plat_mem_readable(obj + 8, 8))
x->cxx_what = copy_cstr(*(const uintptr_t *)(obj + 8), what, sizeof what);
}
// The last-chance filter: an exception no handler took, in any module. crash_veh reports faults in this executable as
@@ -721,16 +904,15 @@ static LONG WINAPI crash_filter(EXCEPTION_POINTERS *ep) {
(uint32_t)code, rip, 0, -1, 0};
if (code == EXCEPTION_ACCESS_VIOLATION && er->NumberParameters >= 2)
c.access = (int)er->ExceptionInformation[0], c.addr = er->ExceptionInformation[1];
regs_of(ep->ContextRecord, &c);
void *base = NULL;
RtlPcToFileHeader((void *)rip, &base);
MODULEINFO mi;
uintptr_t exe = (uintptr_t)GetModuleHandleW(NULL);
c.offset = rip - (uintptr_t)base;
if (!base) c.module = "";
else if ((uintptr_t)base != exe) c.module = image_name(base);
if (c.module && GetModuleInformation(GetCurrentProcess(), (HMODULE)exe, &mi, sizeof mi))
c.nframes = frames_in(ep->ContextRecord, exe, mi.SizeOfImage, c.frames,
(int)(sizeof c.frames / sizeof *c.frames));
if (c.module) frames_of(ep->ContextRecord, &c);
if (code == 0xe06d7363u) cxx_info(er, &c);
crash_fn(&c);
return EXCEPTION_CONTINUE_SEARCH;
}
@@ -1389,6 +1571,27 @@ void *plat_lib_open(const char *name) {
void *plat_lib_sym(void *lib, const char *name) { return lib ? (void *)GetProcAddress((HMODULE)lib, name) : NULL; }
void plat_lib_error(char *out, size_t cap) { snprintf(out, cap, "error %lu", GetLastError()); }
int plat_ngx_core_path(char *out, size_t cap) {
wchar_t dir[WPATH];
DWORD n = sizeof dir;
char d[PLAT_PATH_MAX];
if (RegGetValueW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\NVIDIA Corporation\\Global\\NGXCore", L"FullPath", RRF_RT_REG_SZ,
NULL, dir, &n) != ERROR_SUCCESS ||
!WideCharToMultiByte(CP_UTF8, 0, dir, -1, d, (int)sizeof d, NULL, NULL))
return 0;
return snprintf(out, cap, "%s\\_nvngx.dll", d) < (int)cap;
}
int plat_exe_dir_wide(wchar_t *out, size_t cap) {
DWORD len = GetModuleFileNameW(NULL, out, (DWORD)cap);
if (!len || len >= cap) return 0;
while (len && out[len - 1] != L'\\' && out[len - 1] != L'/') len--;
out[len] = 0;
return len != 0;
}
// ---- machine load: GetSystemTimes for the CPU, PDH (pdh.dll, loaded on first use) for the thermal zones and the GPU
// engine and adapter memory counters that Task Manager reads.
@@ -1581,6 +1784,12 @@ void plat_commit_info(double *proc_mb, double *used_mb, double *limit_mb) {
*proc_mb = (double)(pmc.PrivateUsage >> 20);
}
void plat_mem_room(uint64_t *commit_room, uint64_t *phys_room) {
*commit_room = *phys_room = UINT64_MAX;
MEMORYSTATUSEX ms = {.dwLength = sizeof ms};
if (GlobalMemoryStatusEx(&ms)) *commit_room = ms.ullAvailPageFile, *phys_room = ms.ullAvailPhys;
}
void plat_process_memory(uint64_t *resident, uint64_t *private_bytes) {
PROCESS_MEMORY_COUNTERS_EX pm = {.cb = sizeof pm};
*resident = *private_bytes = 0;
+132 -22
View File
@@ -14,10 +14,11 @@
// 64-bit word, +40 the renderer (1.2.1) or display configuration (1.4.3) the patched code stores there.
// +56 the camera's aspect ratio as a float when the output is not 16:9 (set aspect), +60 the field-of-view factor
// (set fov, res_fov_live).
// 1.4.3 scales the scene it reads by output width / 1920 (a 2560x1440 output renders its 1600x900 scene at 2128x1200;
// MacNativeGames measured the same), so its page holds the wanted size divided by that factor and the game's 5/6 needs
// no scene patch. 1.2.1 renders the size it reads (1600x900 on a 2560x1440 output): its page holds the wanted size, and
// the scene set is on whenever the output differs from 1920x1080.
// 1.4.3 scales the scene it reads by canvas width / the console mode's base width, 1920 docked and 1280 handheld (a
// 2560x1440 output renders its 1600x900 scene at 2128x1200 docked; MacNativeGames measured the same), so its page holds
// the wanted size divided by that factor (scene_scale) and the game's 5/6 needs no scene patch. 1.2.1 renders the size
// it reads (1600x900 on a 2560x1440 output): its page holds the wanted size, and the scene set is on whenever the
// output differs from 1920x1080.
//
// Heap: agl's DynamicTextureAllocator is one fixed heap, "DynamicTextureAllocatorMem" in
// System/HeapSize/Product.Nin_NX_NVN.HeapDef.byml.zs (113664 KiB, carved from "Graphics", 201728 KiB). At 2560x1440
@@ -40,6 +41,7 @@
#include "cfg.h"
#include "host.h"
#include "mods.h"
#include "ov.h"
#include "platform/platform.h"
#define BASE_W 1920
@@ -48,7 +50,9 @@
#define SCENE_H 900
#define DTA_KIB 113664u // DynamicTextureAllocatorMem, shipped
#define TEX_HEAP_KIB 506880u // TextureHeap, shipped (1.2.1 and 1.4.3): what a mod's own HeapDef is measured against
#define SCENE_MAX 4096 // per side, as MacNativeGames' TOTK_RENDER_RES
#define SCENE_MAX 8192 // per side
#define SCENE_KEEP 4096 // per side: the heap bound shrinks no scene below this (scene_for)
#define DTA_MAX_KIB 2031616u // DynamicTextureAllocatorMem grown at most to 1984 MiB, under 2 GiB (decide)
#define SHADOW_BASE 960 // the sun shadow map's base size (its cascades: a D16 array of 4 layers)
static struct {
@@ -91,12 +95,25 @@ static int dlss_dll(void) {
#endif
}
// The scene size for a share k of the output, within SCENE_MAX per side.
static void scene_for(double k, unsigned *w, unsigned *h) {
static uint64_t shadow_kib(void);
// The scene size for a share k of the output, within SCENE_MAX a side and within what a texture heap of DTA_MAX_KIB
// holds next to the output and the shadow map (res_heap_extra_kib's growth). 1.2.1 already misplaced textures in a
// pool past 2 GiB, where a signed 32-bit offset turns negative (recomp/patches-v121.txt, set pooloff); larger heaps
// have not been run. The heap bound never takes the scene below 4096 a side, the size earlier builds gave: an output
// near 8K grows the heap past 2 GiB for its own targets, and its scene stays what it was. Needs R.shadow. 1 when the
// scene was made smaller than k asks.
static int scene_for(double k, unsigned *w, unsigned *h) {
double sw = R.ow * k, sh = R.oh * k;
double fit = fmin(1.0, fmin(SCENE_MAX / sw, SCENE_MAX / sh));
double keep = fmin(1.0, fmin(SCENE_KEEP / sw, SCENE_KEEP / sh));
double room = (double)(DTA_MAX_KIB - shadow_kib()) / DTA_KIB - 1.0 -
fmax(0.0, (double)R.ow * R.oh / (BASE_W * BASE_H) - 1.0);
double most = (1.0 + fmax(0.0, room)) * SCENE_W * SCENE_H; // scene pixels that fit
if (sw * sh * fit * fit > most) fit = fmax(keep, sqrt(most / (sw * sh)));
*w = (unsigned)(sw * fit + 0.5);
*h = (unsigned)(sh * fit + 0.5);
return fit < 1.0;
}
// The scene's share of the output for a TOTK_DLSS mode; 0 for dlaa (the scene keeps base_k), while DLSS/DLAA is off
@@ -168,6 +185,12 @@ static void decide(void) {
rt_log("resolution: TOTK_RENDER_SCALE=%s is not game or a percentage 25..400; game", s);
}
}
const char *sr = cfg_value("TOTK_SHADOW_RES");
if (strcmp(sr, "game")) {
unsigned n = (unsigned)atoi(sr);
if (n >= 256 && n <= 4096 && !(n & 7)) R.shadow = n;
else rt_log("resolution: TOTK_SHADOW_RES=%s is not game or a size 256..4096 (a multiple of 8); game", sr);
}
// A DLSS mode other than dlaa sets the scene size instead. On 1.4.3 with DLSS able to run (DLAA on, the DLL there)
// the mode can change while the game runs (res_dlss_mode): the scene patch is then always on and the heap is sized
// for the largest scene the modes ask for. Everyone else keeps the game's own scene size and its dynamic resolution
@@ -178,16 +201,14 @@ static void decide(void) {
R.dlss_k = dlss_share(m);
if (R.dlss_k) rt_log("resolution: DLSS %s: the scene at %.0f %% of the output (render_scale ignored)", m, R.dlss_k * 100);
double sk = R.dlss_k ? R.dlss_k : k;
if (R.live || R.dlss_k || R.ow * sk > SCENE_MAX || R.oh * sk > SCENE_MAX) R.scene_set = 1;
scene_for(sk, &R.sw, &R.sh);
if (R.live || R.dlss_k) R.scene_set = 1;
if (scene_for(sk, &R.sw, &R.sh)) {
R.scene_set = 1;
rt_log("resolution: a %.0fx%.0f scene is larger than the port allows (8192 a side, a texture heap under 2 GiB "
"past 4096 a side); %ux%u", R.ow * sk, R.oh * sk, R.sw, R.sh);
}
if (R.live) scene_for(fmax(k, 2.0 / 3.0), &R.hw, &R.hh);
else R.hw = R.sw, R.hh = R.sh;
const char *sr = cfg_value("TOTK_SHADOW_RES");
if (strcmp(sr, "game")) {
unsigned n = (unsigned)atoi(sr);
if (n >= 256 && n <= 4096 && !(n & 7)) R.shadow = n;
else rt_log("resolution: TOTK_SHADOW_RES=%s is not game or a size 256..4096 (a multiple of 8); game", sr);
}
}
// Texture heap for a sun shadow map above the base size: the four layers of 2 bytes, three maps in flight (the engine
@@ -209,8 +230,36 @@ uint64_t res_heap_extra_kib(void) {
return kib + ((shadow_kib() + 1023) & ~1023ull); // whole MiB
}
// 1.4.3's factor between the scene size it reads from the page and the one it renders. The game's scene setter
// (main+0x585e48 and the code before it) multiplies the page's size by the canvas width (display configuration +0x10)
// over the current console mode's base width (+0xf4 of the mode's entry, mode 1 handheld: main+0x2dac1c stores
// GetOperationMode != 1 at +0x546). The base is 1280 handheld and 1920 docked. The canvas is the output whenever the
// scene patch is on: docked the game's own canvas is 1920, handheld res_init switches the output patch on (a 1280 base
// with the game's 1280x720 handheld canvas would render R.ow / 1280 times the scene). 1.2.1 renders the size it reads.
static double scene_scale(void) {
if (!RT_V143) return 1.0;
return (double)R.ow / (ov_handheld() ? 1280 : BASE_W);
}
uint64_t res_heap_extra_bytes(void) { return ((res_heap_extra_kib() << 10) + 0x1fffff) & ~0x1fffffull; }
// TOTK_GRASS_RANGE (percent, 50 to 400; 100 = the game's): how far around the camera grass blades grow. The near grass
// map (the grass patch set, recomp/patches-v121.txt and patches-v143.txt) covers 62.5 m and draws into 256x256 and
// 128x128 textures; other values scale the extent, its step and both textures together, so the grass keeps its detail
// per metre. The larger textures come from the grass heap "草" of the HeapDef, which has no room for them (at 200 %
// without growth the game stops at start on a failed allocation), so it grows by 9318 KiB, its size in 1.4.3, times
// the textures' growth in area (k^2 - 1).
#define GRASS_HEAP_KIB 9318
static int grass_pct(void) {
int pct = cfg_int("TOTK_GRASS_RANGE");
return pct < 50 ? 50 : pct > 400 ? 400 : pct;
}
uint64_t res_grass_heap_extra_kib(void) {
int pct = grass_pct();
return pct > 100 ? (uint64_t)GRASS_HEAP_KIB * (uint64_t)(pct * pct - 10000) / 10000 : 0;
}
uint64_t res_grass_heap_extra_bytes(void) { return ((res_grass_heap_extra_kib() << 10) + 0x1fffff) & ~0x1fffffull; }
// TOTK_TEX_HEAP_EXTRA_MB (0 to 4096): more room in "TextureHeap" and its parent "ゲームグラフィックス", the heap the
// world's textures stream into. HD texture packs made for emulators with 8 GB of DRAM grow it by about 3.3 GiB in their
// own HeapDef; without the room the game runs out and starts its panic Blood Moon over and over. The pool and the heap
@@ -365,9 +414,9 @@ int res_heapdef_plain(const void *in, size_t n) {
}
int res_heapdef_build(const void *in, size_t n, void **out, size_t *out_n) {
uint64_t extra = res_heap_extra_kib(), tex = tex_heap_extra_kib();
uint64_t extra = res_heap_extra_kib(), tex = tex_heap_extra_kib(), grass = res_grass_heap_extra_kib();
*out = NULL;
if (!extra && !tex) return 0;
if (!extra && !tex && !grass) return 0;
Byml b;
uint32_t heaps = 0;
size_t dta_at = 0, gfx_at = 0;
@@ -407,6 +456,17 @@ int res_heapdef_build(const void *in, size_t n, void **out, size_t *out_n) {
}
}
}
if (grass) {
size_t at = heap_size_field(&b, heaps, "\xe8\x8d\x89"); // 草, the grass heap
if (at) {
int32_t g, g2;
memcpy(&g, b.p + at, 4);
g2 = g + (int32_t)grass;
memcpy(b.p + at, &g2, 4);
rt_log("resolution: HeapDef grass heap %d -> %d KiB (TOTK_GRASS_RANGE)", g, g2);
} else
rt_log("resolution: the HeapDef table has no grass heap; grass_range may run out of grass memory");
}
size_t cap = ZSTD_compressBound(b.n);
void *z = malloc(cap);
size_t zn = z ? ZSTD_compress(z, cap, b.p, b.n, 9) : 0;
@@ -467,8 +527,29 @@ static void res_shadow_range(void) {
rt_log("resolution: shadow cascades end at %g, %g, %g and %g m", d[0], d[1], d[2], d[3]);
}
// The grass map's values on page +128..+144 (the grass patch set): extent, step, the two texture sizes, and the
// half extent main+0x206340c picks instead of the extent. Texture sizes stay multiples of 32.
static void res_grass_range(void) {
int pct = grass_pct(), set = rt_patch_set("grass");
if (pct == 100) return;
if (set < 0) {
rt_log("resolution: this game version has no grass patch set; grass_range stays 100");
return;
}
float k = pct / 100.0f, f[2] = {62.5f * k, 3.90625f * k}, half = 31.25f * k;
uint32_t t[2] = {(uint32_t)(256 * pct / 100 + 16) / 32 * 32, (uint32_t)(128 * pct / 100 + 16) / 32 * 32};
uint8_t *page = rt_patch_page();
memcpy(page + 128, f, sizeof f);
memcpy(page + 136, t, sizeof t);
memcpy(page + 144, &half, sizeof half);
rt_patch_switch(set, 1);
rt_log("resolution: grass map %g m, textures %ux%u and %ux%u (TOTK_GRASS_RANGE=%d)", f[0], t[0], t[0], t[1], t[1],
pct);
}
void res_init(void) {
res_shadow_range();
res_grass_range();
if (!bigger()) {
rt_log("resolution: the game's own (output 1920x1080, scene 1600x900)");
shadow_init(rt_patch_page());
@@ -485,15 +566,20 @@ void res_init(void) {
return;
}
uint8_t *page = rt_patch_page();
double k = RT_V143 ? (double)R.ow / BASE_W : 1.0; // 1.4.3's own scaling of the scene with the output width
double k = scene_scale();
int scene_on = R.scene_set || (!RT_V143 && (R.ow != BASE_W || R.oh != BASE_H));
// Handheld, 1.4.3 draws a 1280x720 canvas unless the output patch is on, whatever the output setting (measured: a
// 1920x1080 output with the patch off composed the frame at 1280x720 and only the swapchain was 1920x1080). With
// the scene patch on the output patch is switched on too, so the canvas is the output size that scene_scale and
// the heap size (res_heap_extra_kib) assume.
int output_on = R.ow != BASE_W || R.oh != BASE_H || (RT_V143 && scene_on && ov_handheld());
float sc[2] = {(float)(R.sw / k), (float)(R.sh / k)};
uint64_t out = pack(R.ow, R.oh), none = 0;
memcpy(page + 16, sc, sizeof sc);
memcpy(page + 32, &out, 8);
memcpy(page + 40, &none, 8);
memcpy(page + 48, &out, 8);
rt_patch_switch(output, R.ow != BASE_W || R.oh != BASE_H);
rt_patch_switch(output, output_on);
rt_patch_switch(scene, scene_on);
R.on = 1;
if (R.live) g_fnprof = 1; // until the game sets its render size: res_fn_hit, or res_present gives up
@@ -562,6 +648,29 @@ void res_fov_live(void) {
rt_log("resolution: field of view %d%% of the game's", pct);
}
// TOTK_DYNAMIC_RES = 0 holds the game's dynamic resolution at 100 % (recomp/patches-v121.txt and patches-v143.txt,
// set fixres), so the scene renders at the size render_scale chose. The game picks its tier (100, 90 or 80 %) mostly
// from a fixed cost model made for the Switch: one term grows with the object counts, one with (scene width / 1600)
// squared. The GPU time it also measures (two NVN report timestamps, a frame apart) only lowers that prediction when the
// GPU is slower than the model says, which a PC GPU never is, so the model alone decides. Measured on 1.2.1: a
// 2867-wide scene stays at 100 %, one wider than about 2970 does not, and a 4267-wide one (5120x1440 at render_scale
// 83) fits no tier and falls back to 80 % every frame, whatever the GPU's load. 1 (the default) leaves the game's own
// stepping and the set off. Runs on a settings change (gfx_present.c window_settings_live), like res_fov_live.
void res_dynres_live(void) {
static int set = -2, shown = -1;
if (set == -2) set = rt_patch_set("fixres");
int dyn = cfg_bool("TOTK_DYNAMIC_RES");
if (dyn == shown) return;
shown = dyn;
if (set < 0) {
if (!dyn) rt_log("resolution: this game version has no fixed-resolution patch; the game keeps its dynamic resolution");
return;
}
rt_patch_switch(set, !dyn);
rt_log("resolution: dynamic resolution %s",
dyn ? "on: the game's own 100, 90 or 80 %" : "off: the scene stays at 100 %");
}
void res_wanted(unsigned out[2]) {
decide();
out[0] = R.ow, out[1] = R.oh;
@@ -585,8 +694,9 @@ void res_fn_hit(uint64_t addr, uint64_t x0) {
}
void res_present(Cpu *c) {
// The hook runs on every guest function entry while it waits: should the game set its render size before
// res_init or never, it stops after 1800 presents (half a minute at 60 fps) and modes change at the next start.
// Should the game set its render size before res_init or never, the hook (FN_ENTER of the hooked functions,
// recomp/patches-v143.txt `hook`) stops after 1800 presents (half a minute at 60 fps) and modes change at the
// next start.
static unsigned waited;
if (g_fnprof && !renderer && ++waited == 1800) {
g_fnprof = 0;
@@ -614,7 +724,7 @@ int res_dlss_mode(const char *m) {
unsigned w, h;
scene_for(k ? k : R.base_k, &w, &h);
if (w == R.sw && h == R.sh) return 0;
double s = RT_V143 ? (double)R.ow / BASE_W : 1.0;
double s = scene_scale();
float sc[2] = {(float)(w / s), (float)(h / s)};
memcpy((uint8_t *)rt_patch_page() + 16, sc, sizeof sc);
R.sw = w, R.sh = h;
+221 -50
View File
@@ -38,6 +38,7 @@
#include "mods.h"
#include "romfs_config.h"
#include "romfs_path.h"
#include "audio_cat.h"
#include "platform/platform.h"
#include "restbl.h"
#include "sha256.h"
@@ -69,6 +70,7 @@ static PlatMutex lock = PLAT_MUTEX_INIT;
static uint64_t handles[NH]; // file index + 1; 0 is free
static uint32_t handle_ovl[NH]; // overlay slot + 1 of an open handle, 0 = original
static PlatFile *handle_file[NH]; // the handle's open overlay file, when handle_ovl is set
static uint8_t handle_audio_cat[NH]; // the open file's sound category (audio_cat.c), AUDIO_CAT_UNKNOWN when not audio
static Overlay *overlays; // sorted by RomFS index after overlay_init
static size_t noverlay, overlay_cap;
static char **added; // RomFS paths of the added files, sorted; added[k] is index file_count + k
@@ -340,7 +342,8 @@ static void overlay_push(Overlay o) {
}
// 1 when path names a file of one game version only (Product.<n> in a name: the resource size table, the actor
// database and other version-bound files); the RomFS lacking it means it is another version's.
// database and other version-bound files); the RomFS lacking it means it is another version's, or a name this version
// still asks for (mod_walk_cb).
static int other_version(const char *path) {
for (const char *p = strstr(path, ".Product."); p; p = strstr(p + 1, ".Product.")) {
const char *d = p + strlen(".Product.");
@@ -420,9 +423,18 @@ static int mod_walk_cb(const char *leaf, int kind, uint64_t size, void *ctx) {
}
uint64_t i = index_any_case(path);
if (i == UINT64_MAX && other_version(path)) {
if (is_restbl(path)) shipped_add(file, w->k + 1, 1); // its entries for this mod's files still apply
m->other_version++;
return 1;
// Another version's resource size table is no file this game opens; its entries for this mod's files still
// apply.
if (is_restbl(path)) {
shipped_add(file, w->k + 1, 1);
return 1;
}
// Any other file is served under its own name below, as an added file, the way LayeredFS shows a console every
// file of a mod: the game does ask for names its RomFS lacks. Measured on 1.4.3 with mods on (trace = fs): it
// opened Sound/Resource/Pack_MasterSword.Product.110.bars.zs, where its RomFS holds Product.140 and the
// unversioned file only. A sound mod that ships its pack under all those names stayed silent while this file
// was left out.
}
PlatFile *f = open_read(file);
if (!f) {
@@ -878,7 +890,7 @@ static void *plain_mod_heapdef(size_t *n, const char **who) {
return NULL;
}
static void heapdef_overlay(void) {
if (!res_heap_extra_kib() && !res_tex_heap_extra_bytes()) return;
if (!res_heap_extra_kib() && !res_tex_heap_extra_bytes() && !res_grass_heap_extra_kib()) return;
static const char keeps[] = "the game keeps its own resolution";
Overlay *base;
size_t n;
@@ -919,12 +931,11 @@ static void camera_overlay(void) {
// The button prompts' folders (input_prompts_folder) and whether each had a manifest next to romfs-index.bin at start;
// the one the settings asked for at start, the one overlay_init loaded (NULL: the game's own prompts) and why the first
// did not load (ROMFS_PROMPTS_*). The menu compares them with the settings in effect (ui/menu.cpp prompts_note).
// romfs-patches-pc keeps that name, but at a game resolution of another shape than 16:9 its files come from pc_dir,
// romfs-patches-pc-<w>x<h> (overlay_init).
static struct { const char *name; int ready; } prompts_folders[3];
// Each folder keeps its name, but at a game resolution of another shape than 16:9 its files come from dir,
// <name>-<w>x<h> (overlay_init), and ready is that one's.
static struct { const char *name; int ready; char dir[64]; } prompts_folders[3];
static const char *prompts_wanted, *prompts_loaded;
static int prompts_off;
static char pc_dir[64];
const char *romfs_prompts(void) { return prompts_loaded; }
@@ -958,78 +969,104 @@ static void overlay_drop(size_t a, size_t b) {
noverlay -= b - a;
}
// The folder <prefix>-<w>x<h> the game resolution w x h takes in base: that exact size when it has a manifest (an
// install by a setup that wrote every size, or make_romfs_patches.py --aspect <w>x<h> in a source build), else the
// size of its shape (cfg.c cfg_shape_size), the one folder per shape installer/steps.py Shapes writes. name gets the
// folder found, or the shape's one when neither has a manifest (for the log). 1 when found.
static int shaped_name(const char *base, const char *prefix, unsigned w, unsigned h, char *name, size_t cap) {
char path[2400];
unsigned s[2] = {w, h};
for (int pass = 0; pass < 2; pass++) {
if (pass && !cfg_shape_size(w, h, s)) break;
if (pass && s[0] == w && s[1] == h) break;
snprintf(name, cap, "%s-%ux%u", prefix, s[0], s[1]);
if (snprintf(path, sizeof path, "%s/%s/manifest.tsv", base, name) < (int)sizeof path && plat_file_exists(path))
return 1;
}
return 0;
}
static void overlay_init(void) {
// TOTK_ROMFS_PATCHES unset: romfs-patches next to romfs-index.bin if it has a manifest (tools/make_romfs_patches.py).
// A game resolution that is not 16:9 takes romfs-patches-<w>x<h> instead when it is there: the same overlay with
// the UI laid out for that shape (make_romfs_patches.py --aspect), when the game version has the camera aspect patch
// the layout goes with (res_wants_wide_overlay).
// A game resolution that is not 16:9 takes romfs-patches-<w>x<h> instead when it is there (shaped_name: the exact
// size, else the folder of its shape): the same overlay with the UI laid out for that shape
// (make_romfs_patches.py --aspect), when the game version has the camera aspect patch the layout goes with
// (res_wants_wide_overlay).
// With prompts = xbox, the Xbox prompts' folder for the pad layout in its place (input_prompts_folder): the same
// patches with Xbox button icons (make_romfs_patches.py --prompts). The game reads the archives once at boot, so
// the prompts stay until restart even though the layout itself changes live. Each folder replaces romfs-patches as
// a whole and the setup builds no folder of another shape with Xbox prompts, so a game resolution that is not 16:9
// keeps the Switch prompts. So does a RomFS mod (the TOTK_ROMFS_MODS manifest or a player mod) that replaces one of
// the prompts' fonts: romfs-patches loads in their place, so the mod's file is not served beside prompts files made
// for the game's own. A mod's resource size table is no clash: restbl_overlay merges it with the prompts' one.
// prompts = pc takes romfs-patches-pc whatever the pad layout, and at a game resolution of another shape
// romfs-patches-pc-<w>x<h> (make_romfs_patches.py --prompts pc --aspect): it carries that shape's UI layout too, so
// it replaces romfs-patches-<w>x<h> as romfs-patches-pc replaces romfs-patches.
// patches with Xbox button icons (make_romfs_patches.py --prompts); prompts = pc takes romfs-patches-pc whatever
// the pad layout. The game reads the archives once at boot, so the prompts stay until restart even though the
// layout itself changes live. Each folder replaces romfs-patches as a whole. At a game resolution of another shape
// the prompts come from <folder>-<w>x<h> (make_romfs_patches.py --prompts <style> --aspect): it carries that
// shape's UI layout too, so it replaces romfs-patches-<w>x<h> as the folder replaces romfs-patches. Without it the
// Switch prompts stay with the shape's layout. So they do when a RomFS mod (the TOTK_ROMFS_MODS manifest or a
// player mod) replaces one of the prompts' fonts: romfs-patches loads in their place, so the mod's file is not
// served beside prompts files made for the game's own. A mod's resource size table is no clash: restbl_overlay
// merges it with the prompts' one.
char def[2100], path[2100];
const char *patches = cfg_get("TOTK_ROMFS_PATCHES"), *mods = cfg_get("TOTK_ROMFS_MODS");
int wide = 0;
unsigned out[2];
res_wanted(out);
unsigned shape[2] = {out[0], out[1]}; // the --aspect of the folder made for this shape
cfg_shape_size(out[0], out[1], shape);
if (!patches && res_wants_wide_overlay(out)) {
if (snprintf(def, sizeof def, "%s/romfs-patches-%ux%u/manifest.tsv", index_dir, out[0], out[1]) < (int)sizeof def &&
plat_file_exists(def)) {
def[strlen(def) - strlen("/manifest.tsv")] = 0;
char name[128];
if (shaped_name(index_dir, "romfs-patches", out[0], out[1], name, sizeof name) &&
snprintf(def, sizeof def, "%s/%s", index_dir, name) < (int)sizeof def) {
patches = def, wide = 1;
rt_log("RomFS overlay: the UI layout for %ux%u from %s", out[0], out[1], name);
} else {
rt_log("RomFS overlay: no romfs-patches-%ux%u for this game resolution, the UI keeps its 16:9 layout "
"(tools/make_romfs_patches.py --aspect %ux%u)", out[0], out[1], out[0], out[1]);
rt_log("RomFS overlay: no %s for this game resolution, the UI keeps its 16:9 layout (run the setup again, "
"or tools/make_romfs_patches.py --aspect %ux%u)", name, shape[0], shape[1]);
}
}
static const char *const layouts[2] = {"nintendo", "xbox"}; // one of each Xbox folder, then the PC one
for (int i = 0; i < 3; i++) {
const char *name = i < 2 ? input_prompts_folder(PROMPTS_XBOX, layouts[i]) : input_prompts_folder(PROMPTS_PC, 0);
const char *dir = name;
if (i == 2) {
if (wide) snprintf(pc_dir, sizeof pc_dir, "%s-%ux%u", name, out[0], out[1]);
else snprintf(pc_dir, sizeof pc_dir, "%s", name);
dir = pc_dir;
}
char *dir = prompts_folders[i].dir;
prompts_folders[i].name = name;
if (wide) {
prompts_folders[i].ready = shaped_name(index_dir, name, out[0], out[1], dir, sizeof prompts_folders[i].dir);
continue;
}
snprintf(dir, sizeof prompts_folders[i].dir, "%s", name);
prompts_folders[i].ready =
snprintf(path, sizeof path, "%s/%s/manifest.tsv", index_dir, dir) < (int)sizeof path && plat_file_exists(path);
}
const char *layout = cfg_value("TOTK_PAD_LAYOUT");
int pick = input_prompts_pick(cfg_get("TOTK_PROMPTS"), layout), shaped = wide && pick == PROMPTS_PC;
int pick = input_prompts_pick(cfg_get("TOTK_PROMPTS"), layout);
const char *style = pick == PROMPTS_PC ? "keyboard and mouse" : "Xbox";
prompts_wanted = input_prompts_folder(pick, layout);
const char *wanted_dir = prompts_wanted; // the folder its files come from
for (int i = 0; i < 3; i++)
if (prompts_wanted && !strcmp(prompts_folders[i].name, prompts_wanted)) wanted_dir = prompts_folders[i].dir;
prompts_off = !prompts_wanted ? ROMFS_PROMPTS_OK
: wide && !shaped ? ROMFS_PROMPTS_SHAPE
: patches && !wide ? ROMFS_PROMPTS_PATCHES
: !romfs_prompts_ready(prompts_wanted) ? (shaped ? ROMFS_PROMPTS_SHAPE : ROMFS_PROMPTS_MISSING)
: !romfs_prompts_ready(prompts_wanted) ? (wide ? ROMFS_PROMPTS_SHAPE : ROMFS_PROMPTS_MISSING)
: ROMFS_PROMPTS_OK;
restbl_find();
size_t first = noverlay;
if (prompts_wanted && prompts_off == ROMFS_PROMPTS_OK) {
// fits: its manifest path did
snprintf(path, sizeof path, "%s/%s", index_dir, shaped ? pc_dir : prompts_wanted);
snprintf(path, sizeof path, "%s/%s", index_dir, wanted_dir);
if (overlay_load(path, 1, 0)) {
prompts_loaded = prompts_wanted;
rt_log("RomFS overlay: %s button prompts (%s)", style, path);
if (shaped) patches = NULL; // in place of romfs-patches-<w>x<h>, whose UI layout it carries
if (wide) patches = NULL; // in place of romfs-patches-<w>x<h>, whose UI layout it carries
} else {
prompts_off = ROMFS_PROMPTS_FAILED;
rt_log("RomFS overlay: %s failed its check (run the setup again); romfs-patches loads instead", path);
}
} else if (prompts_wanted) {
rt_log("RomFS overlay: prompts = %s, but %s; Switch prompts stay", pick == PROMPTS_PC ? "pc" : "xbox",
prompts_off == ROMFS_PROMPTS_SHAPE ? (shaped ? "the game resolution has no romfs-patches-pc-<w>x<h> "
"(tools/make_romfs_patches.py --prompts pc --aspect)"
: "the game resolution takes its own UI layout")
: prompts_off == ROMFS_PROMPTS_PATCHES ? "TOTK_ROMFS_PATCHES is set"
: "their folder has no manifest.tsv (run the setup again)");
char why[256];
if (prompts_off == ROMFS_PROMPTS_SHAPE)
snprintf(why, sizeof why, "the game resolution has no %s (tools/make_romfs_patches.py --prompts %s "
"--aspect %ux%u)", wanted_dir, prompts_wanted + strlen("romfs-patches-"), shape[0], shape[1]);
else
snprintf(why, sizeof why, "%s", prompts_off == ROMFS_PROMPTS_PATCHES
? "TOTK_ROMFS_PATCHES is set"
: "their folder has no manifest.tsv (run the setup again)");
rt_log("RomFS overlay: prompts = %s, but %s; Switch prompts stay", pick == PROMPTS_PC ? "pc" : "xbox", why);
}
size_t prompts_end = noverlay;
char plain[2100]; // romfs-patches, when it has a manifest
@@ -1048,7 +1085,7 @@ static void overlay_init(void) {
overlay_drop(first, prompts_end);
prompts_loaded = NULL;
prompts_off = ROMFS_PROMPTS_MODS;
if (shaped) overlay_load(def, 0, 0);
if (wide) overlay_load(def, 0, 0);
else if (plain_ok) overlay_load(plain, 0, 0);
}
restbl_collect();
@@ -1080,17 +1117,136 @@ static void index_path(char *out, size_t cap) {
strcpy(out + k, "romfs-index.bin");
}
// 1 when the game resolution w x h has the UI layout of its shape: romfs-patches-<w>x<h> next to romfs-index.bin with a
// manifest, the folder overlay_init looks for. Works before fs_init: index_dir is not set that early (res_init and
// The folder of romfs-index.bin. Works before fs_init: index_dir is not set that early (cfg_init, res_init and
// resolution.c decide run first), so the index path is worked out again here.
int romfs_shaped_ready(unsigned w, unsigned h) {
char dir[2100], path[2200];
index_path(dir, sizeof dir);
static void index_folder(char *dir, size_t cap) {
index_path(dir, cap);
char *sep = plat_last_sep(dir);
if (sep) *sep = 0;
else strcpy(dir, ".");
return snprintf(path, sizeof path, "%s/romfs-patches-%ux%u/manifest.tsv", dir, w, h) < (int)sizeof path &&
plat_file_exists(path);
}
// 1 when the folder overlay_init takes for <prefix> at w x h (shaped_name) has a manifest next to romfs-index.bin.
static int shaped_folder_ready(const char *prefix, unsigned w, unsigned h) {
char dir[2100], name[128];
index_folder(dir, sizeof dir);
return shaped_name(dir, prefix, w, h, name, sizeof name);
}
// 1 when the game resolution w x h has the UI layout of its shape: romfs-patches-<w>x<h> of that size or of its shape,
// the folder overlay_init looks for.
int romfs_shaped_ready(unsigned w, unsigned h) { return shaped_folder_ready("romfs-patches", w, h); }
// 1 when it has a prompts' folder (input_prompts_folder) with that layout too: <folder>-<w>x<h>, which overlay_init
// loads in its place with those prompts.
int romfs_shaped_prompts_ready(const char *folder, unsigned w, unsigned h) { return shaped_folder_ready(folder, w, h); }
// ---- the layout for a new screen, from the settings menu (ui/menu.cpp row_output_res)
// The setup writes the shaped folders for the displays connected when it ran (installer/steps.py Shapes). After a
// monitor change the menu can start the same generator for the shape of the game resolution: the setup's Python and
// tools/make_romfs_patches.py, which build-cache/rebuild.txt names (installer/steps.py write_rebuild_command: line 1 the
// Python, pythonw on Windows, so no console opens; line 4 the setup's folder), on the install folder's romfs/. Child
// processes the game does not wait for: the generator writes manifest.tsv last, so a folder it has not finished is not
// taken, and the next start loads the finished one.
static int prepare_tools(char *python, size_t pcap, char *script, size_t scap, char *dir, size_t dcap) {
char path[2400];
index_folder(dir, dcap);
uint64_t n = 0;
snprintf(path, sizeof path, "%s/build-cache/rebuild.txt", dir);
char *text = (char *)plat_file_read(path, &n);
if (!text) return 0;
char *lines[5] = {0};
int k = 0;
for (char *p = text; k < 5 && p < text + n;) {
char *e = memchr(p, '\n', (size_t)(text + n - p));
if (!e) e = text + n;
if (e > p && e[-1] == '\r') e[-1] = 0;
*e = 0;
lines[k++] = p;
p = e + 1;
}
int ok = k >= 4 && !strcmp(lines[1], "-c") && snprintf(python, pcap, "%s", lines[0]) < (int)pcap &&
snprintf(script, scap, "%s/tools/make_romfs_patches.py", lines[3]) < (int)scap;
free(text);
snprintf(path, sizeof path, "%s/romfs/romfs.bin", dir);
return ok && plat_file_exists(python) && plat_file_exists(script) && plat_file_exists(path);
}
int romfs_shape_missing(unsigned w, unsigned h, const char *prompts_folder) {
unsigned out[2] = {w, h};
if (!res_wants_wide_overlay(out)) return 0;
return !shaped_folder_ready("romfs-patches", w, h) ||
(prompts_folder && romfs_prompts_ready(prompts_folder) && !shaped_folder_ready(prompts_folder, w, h));
}
int romfs_shape_can_prepare(void) {
static int known = -1;
if (known < 0) {
char python[1024], script[1100], dir[2100];
known = prepare_tools(python, sizeof python, script, sizeof script, dir, sizeof dir);
}
return known;
}
// The folders a generator was started for in this process. The menu thread alone calls romfs_shape_prepare, so no
// lock. The game picks its overlay at start, so a folder is not started twice before the next start, even when its
// generator has ended: a second click, or the same size chosen again, starts no second generator writing into it.
static char shape_started[8][160];
static int shape_started_n;
static int shape_was_started(const char *folder) {
for (int i = 0; i < shape_started_n; i++)
if (!strcmp(shape_started[i], folder)) return 1;
return 0;
}
// Starts the generator for folder (a name in dir) unless one was started for it before. 1 when one runs for it, 0 when
// it could not start. plat_spawn starts it detached and closes its handles without waiting, so a click costs the menu
// thread one CreateProcess.
static int shape_spawn(const char *const *argv, const char *dir, const char *folder) {
if (shape_was_started(folder)) return 1;
if (!plat_spawn(argv, dir)) return 0;
if (shape_started_n < (int)(sizeof shape_started / sizeof *shape_started))
snprintf(shape_started[shape_started_n++], sizeof *shape_started, "%s", folder);
return 1;
}
int romfs_shape_prepare(unsigned w, unsigned h, const char *prompts_folder) {
char python[1024], script[1100], dir[2100], romfs[2200], out[2300], reuse[2300], binds[2300], size[32], version[8],
name[128], folder[160];
unsigned s[2];
if (!cfg_shape_size(w, h, s) || !prepare_tools(python, sizeof python, script, sizeof script, dir, sizeof dir))
return 0;
snprintf(size, sizeof size, "%ux%u", s[0], s[1]);
snprintf(version, sizeof version, "v%d", rt_game_version);
snprintf(romfs, sizeof romfs, "%s/romfs", dir);
int started = 0, failed = 0;
if (!shaped_name(dir, "romfs-patches", w, h, name, sizeof name)) {
snprintf(folder, sizeof folder, "romfs-patches-%s", size);
snprintf(out, sizeof out, "%s/%s", dir, folder);
const char *argv[] = {python, script, "--version", version, "--romfs", romfs, "--out", out, "--aspect", size,
NULL};
if (shape_spawn(argv, dir, folder)) started++;
else failed++;
}
snprintf(reuse, sizeof reuse, "%s/%s/manifest.tsv", dir, prompts_folder ? prompts_folder : "");
if (prompts_folder && plat_file_exists(reuse) && !shaped_name(dir, prompts_folder, w, h, name, sizeof name)) {
const char *style = prompts_folder + strlen("romfs-patches-");
snprintf(reuse, sizeof reuse, "%s/%s", dir, prompts_folder);
snprintf(folder, sizeof folder, "%s-%s", prompts_folder, size);
snprintf(out, sizeof out, "%s/%s", dir, folder);
// The keyboard and mouse prompts are drawn from totk.ini's bind_ lines, as the setup's PcPrompts and Shapes
// steps draw them (installer/steps.py pc_binds); a file without such lines gives the keyboard defaults.
snprintf(binds, sizeof binds, "%s/totk.ini", dir);
int pc = !strcmp(style, "pc") && plat_file_exists(binds);
const char *argv[] = {python, script, "--version", version, "--romfs", romfs, "--out", out, "--prompts", style,
"--aspect", size, "--reuse", reuse, pc ? "--binds" : NULL, binds, NULL};
if (shape_spawn(argv, dir, folder)) started++;
else failed++;
}
rt_log("RomFS overlay: making the layout for %s in the background (%d generators running, %d failed; %s)", size,
started, failed, script);
return started && !failed;
}
static void load_index(void) {
@@ -1241,12 +1397,21 @@ uint32_t romfs_open(uint64_t *out, const char *path, int mode) {
if (!f || memcmp(&id, &o->id, sizeof id))
rt_fatal("RomFS overlay %s is missing or changed since the start check (restart the game)", o->path);
}
// The handle keeps its sound category (audio_cat.c) even while the volume settings are at 100, so a stream opened
// before a setting moves is classified by its later reads. A stream may start reading past its header (the
// header came with the BARS bank), so the coefficients are taken from the image here.
int cat = audio_cat_from_path(p);
if (cat != AUDIO_CAT_UNKNOWN && !ov && image_map && audio_cat_active()) {
Entry e = entry(i);
if (e.size >= 0x40) audio_cat_scan_bwav(image_map + e.offset, e.size < 4096 ? e.size : 4096, cat);
}
plat_mutex_lock(&lock);
for (unsigned j = 1; j < NH; j++)
if (!handles[j]) {
handles[j] = i + 1;
handle_ovl[j] = ov;
handle_file[j] = f;
handle_audio_cat[j] = (uint8_t)cat;
*out = ROMFS_HANDLE_BASE + j;
plat_mutex_unlock(&lock);
return 0;
@@ -1296,6 +1461,11 @@ uint32_t romfs_read(uint64_t *out, uint64_t h, int64_t offset, void *buffer, uin
done += (uint64_t)r;
}
*out = done;
uint64_t j = h - ROMFS_HANDLE_BASE;
if (handle_audio_cat[j] != AUDIO_CAT_UNKNOWN && done && audio_cat_active()) {
if (offset == 0) audio_cat_scan_bwav(buffer, done, handle_audio_cat[j]);
audio_cat_tag_range((uint64_t)(uintptr_t)buffer, done, handle_audio_cat[j]);
}
return 0;
}
@@ -1318,6 +1488,7 @@ void romfs_close(uint64_t h) {
handles[j] = 0;
handle_ovl[j] = 0;
handle_file[j] = NULL;
handle_audio_cat[j] = 0;
plat_mutex_unlock(&lock);
if (of) plat_file_close(of);
}
Loaded 100 of 159 files, more files were not shown because too many files have changed in this diff. Show more