Modules in libraries

A module can be a library built outside ScarletNX, so that a game's facts (its title and build IDs, its functions' offsets, its layout) stay in a repository of their own. The runtime loads it at start, checks its version, and adds it after the compiled-in modules (nn/module_lib.c). From then on it is a module like the others: hook_modules_select applies or refuses it. Its work is its CPU hooks, functions of the game's code by their offset in one build, as the address hooks of src/nx/patches/ are, whose handlers the runtime calls with the guest CPU through one adapter of its own (below). A handler can record an upscale into the game's command buffer. The library and its build files are found by nx/loader/hook_libs.c, which the runtime and the recompile share.

The header. A library includes one header of ScarletNX, src/nx/nn/snx_hook_module.h, with src/ of a ScarletNX checkout on its include path (#include "nx/nn/snx_hook_module.h"). It names no type of the runtime. It has its own code checks (SnxHookCode), CPU hooks (SnxHookCpuHook) and recorded upscale (SnxHookRecordedUpscale). The runtime gives the module a table of functions (SnxHookHost, module_lib.c): the log, a setting's value as text, and a handler's registers, guest memory, the function's own code and the upscale it records.

The entry point. The library exports one function, snx_hook_module(const SnxHookHost* host), which the runtime calls once, before the game's threads. It returns the module (SnxHookModule): the ABI version it was built for, its name, what it does, its title, module and build ID, its code checks and its CPU hooks, which stay where they are for the run (the runtime never unloads a library it took). It returns NULL to refuse the runtime, after it logs why.

The version. The ABI has its own, SNX_HOOK_ABI_MAJOR and SNX_HOOK_ABI_MINOR, 1.2 now. A new major changes what one side reads of the other. A new minor only adds: functions at the end of SnxHookHost, fields at the end of SnxHookModule and of SnxHookRecordedUpscale, which carries its size, and flags. The runtime takes a module of its own minor or an older one, and refuses one of another major or of a newer minor. In every major, the first fields of SnxHookHost and SnxHookModule and the signature of snx_hook_module stay as they are, so a module of any version can be read and refused. A module that needs a function of a newer minor reads host->abi_minor and refuses an older runtime.

Where. The hook_modules_dir setting names the folder, and when it is empty, hook_modules/ in the user's data folder (~/.local/share/scarletnx/hook_modules/ on Linux). Each NAME.so there (NAME.dll on Windows, NAME.dylib on macOS) is the module NAME, loaded in the names' order. The file's name is the module's (letters a to z, digits and _, at most 63), so hooks_off names a library's module as it names a compiled-in one, and the library of a module that is off is never loaded at start: none of its code runs in the game. hooks=0 loads none. The recompile is another process: it opens every library of the folder, an off one too, and calls its entry point to read its CPU hooks (below), but runs no handler.

The log. A library taken says hooks: library PATH: module NAME (hook ABI 1.2), 1 CPU hook. One refused says hooks: library PATH refused: WHY, and is closed. It is refused when the system can't load it (with the system's reason), when it has no snx_hook_module, when that refuses the runtime, for another major or a newer minor, for a module named otherwise than its file or as another module, for more than 64 code checks or one without bytes, and for its CPU hooks' faults (below). A library's module that doesn't apply to the game is said as a compiled-in one is.

CPU hooks in libraries

A library's module lists its CPU hooks (SnxHookCpuHook: an offset, a name for the log, a handler) in SnxHookModule.cpu_hooks, 1 to 32. They are in the game's module that SnxHookModule.module names (main for NULL), of the build its build_id names, which it must give. Each offset is a function's entry, 4-aligned.

An upscale a CPU hook records. record_upscale(cpu, command_buffer, upscale), called from a CPU hook's handler alone, appends one record (OP_UPSCALE, nvn_cmd_record_upscale) to the game's command buffer at command_buffer, the guest address of its NVNcommandBuffer, which must be recording. Its textures are the game's NVNtexture objects (color and output required, depth, motion vectors, exposure and reactive optional), each with an NVNtextureView or 0, snapshotted as every NVN command's are. At replay, in order with the buffer's other commands, the color (read from its corner at the render size) and the output (written at out_base, the output size) go to the renderer's upscaler as a module's upscale: the render_upscaler setting chooses, a temporal upscaler only with depth and motion vectors, a spatial one or the copy when the flags allow. A render_max of 0 is the color's size. Where no upscaler can run, a filtered copy of the color's render rectangle onto the output's stands in, so the output is always written. The log names what ran and both surfaces, the first 4 times and at each change (nvn: a hook module's recorded upscale ran FSR 1 (EASU, RCAS): color ..., output ..., or ran no upscaler, the filtered copy stands in). It returns 0, and records nothing, for a command buffer that isn't recording, a color or output that isn't a live texture, a size of 0 or a call outside a handler: the handler then calls run_original, and the game's own pass runs.

Motion vectors from depth (1.2). A game with depth and a camera but no motion vectors of its own can still feed a temporal upscaler: with SNX_HOOK_UPSCALE_REPROJECTED_MOTION, depth given and motion not, the record's reprojection matrix (row-major) takes a render pixel of this frame, as (x, y, depth, 1) with x and y its place in the color's render rectangle as -1 to 1 (y down) and the depth as its texture stores it, to where it was in the frame before, in the same terms. It is the frame before's view-projection times the inverse of this frame's, with the picture's coordinates and the depth's range folded in by the module, which knows its game's conventions. At replay a compute pass (gpu/vk/upscaler/reproject.c) makes the motion vectors from the depth just before the upscaler runs, and the log says so once (vulkan: motion vectors from depth for a hook module, up to WxH). They are the camera's motion alone: whatever moves by itself gets the camera's motion at its pixels, which a temporal upscaler shows as a short trail. A record of 1.1's size has no matrix and is taken as before.

The runtime's jitter (1.2). A temporal upscaler needs the camera to move by a fraction of a pixel each frame, which a game without an upscaler of its own doesn't do. With SNX_HOOK_UPSCALE_RUNTIME_JITTER and depth given, while a temporal upscaler takes the module's upscale, the renderer moves the scene's draws itself (gpu/vk/render.c): those that test depth, with a viewport of the render size, into the depth texture given or another one of its size (a game may draw a depth pass into one image and test a later pass against another). The offset is a sub-pixel one of a Halton 2, 3 sequence of 8, set after each upscale for the next frame's draws, and the upscaler gets the one this frame's draws took in place of the record's jitter. A spatial upscaler or the copy would show it, so with those the draws don't move. The log says once that the runtime jitters the scene's draws.

The display and the resolution multiplier (1.1). display_size(out) gives the display's largest picture as the game is told it, the output a game sizes its frames for. resolution_scale(cpu, scale, target, render), from a CPU hook's handler or from the module's load (snx_hook_module, with a NULL cpu: the settings and display_size already answer there), asks for a resolution multiplier: every render target the renderer makes from then on is the game's size times scale, so a game rendering at render reaches the internal resolution target target the user picked (internal_resolution). The renderer takes it until it makes its first render target, so a module asks before the game draws, and only while the user's resolution setting is at its default, 1. It returns the multiplier in effect, and the log says what became of the request (hooks: module NAME sets the resolution multiplier to 2 (target 3840x2160, the game renders 1920x1080), hooks: a module at its load sets ... for one asking at its load, or that the user's setting stays, or that it came too late).

hook_lib_cpu (tests/hook_lib_cpu.c, libraries from tests/hook_lib_cpu_mod.c) checks the recompile's route entries and their keys (a 16-digit prefix and the whole ID alike), the refusals, a hook the route list leaves out, and handlers that leave the function's code out, run it around their own work, and record an upscale. nvn_commands checks the record and nvn_copy_formats its replay, an upscaler's and the stand-in's.

A module of one game, as a library (each 0 a value read off the game's code, its function found with the trace): its CPU hook on the function that records the game's upscaler pass, which records the runtime's upscale in its place, or runs the game's own code when it can't:


Made from docs/platforms/nx/hooks.md at 69237645142c. GPL-3.0-or-later.