Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rebase compatibility port onto current upstream renderer
  • Loading branch information
widmonstertony committed Oct 28, 2026
commit e0d43bbf4e4cd8aee72d8c4920c8f5dbd71a9436
9,810 changes: 9,810 additions & 0 deletions dm/gfx/nvn/nvn_mtl_legacy.inc

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion dm/gfx/nvn/nvn_refl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
#include <stdint.h>

enum { NVN_RB_CBUF = 1, NVN_RB_SAMP, NVN_RB_TEX, NVN_RB_IMG, NVN_RB_TBUF,
NVN_RB_SBUFP, NVN_RB_SUPPORT, NVN_RB_C1, NVN_RB_DRV0, NVN_RB_DRV2, NVN_RB_SAMPT, NVN_RB_UBOP, NVN_RB_TEXU, NVN_RB_SAMPU, NVN_RB_SAMPI, NVN_RB_SBUFU, NVN_RB_TEXD, NVN_RB_SAMPD, NVN_RB_TEXA, NVN_RB_SAMPA, NVN_RB_GEOMETRY };
NVN_RB_SBUFP, NVN_RB_SUPPORT, NVN_RB_C1, NVN_RB_DRV0, NVN_RB_DRV2, NVN_RB_SAMPT, NVN_RB_UBOP, NVN_RB_TEXU, NVN_RB_SAMPU, NVN_RB_SAMPI, NVN_RB_SBUFU, NVN_RB_TEXD, NVN_RB_SAMPD, NVN_RB_IMGA, NVN_RB_TEXA, NVN_RB_SAMPA, NVN_RB_GEOMETRY };
// TEXA / SAMPA: an array of n handles in the driver table c2 that the shader indexes at run time (dmsh_build.py c2_array): element k = the texture / sampler of the slot whose c2 word lies
// at byte off + 8k (combined, else separate); words outside both tables give the dummy. Luigi's Mansion 3's shadow cascades
// IMGA: SPIRV-Cross's MSL 2.3 image atomics: a device atomic_uint / atomic_int pointer (<image name>_atomic) beside the image of NVN image slot r; the replay binds the image's storage address
// TEXD / SAMPD: Ryujinx's fallback for a texture handle it cannot trace to one slot (handle type 4, <stage>_<t|tb|s>_cb0_40000000): an array of n textures / samplers indexed by
// the handle's texture / sampler ID (capped at n - 1); the replay binds slot k at element k and puts slot indices in c2 (profile key tex_dyn_slots, Octopath Traveler 0)
// SAMPI: the sampler ID is a constant in the shader (reg = the ID; Ryujinx's SeparateConstantSamplerHandle: the handle ORs a constant sampler into a bound texture, Monster Hunter Rise)
Expand Down
9 changes: 8 additions & 1 deletion dm/gfx/tex/nvn_deswizzle_gpu.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <TargetConditionals.h>
#import <Metal/Metal.h>
#include "nvn_deswizzle.h"

int nvn_deswz_gpu_init(id<MTLDevice> dev);
int nvn_deswz_gpu_encode(id<MTLComputeCommandEncoder> enc, id<MTLBuffer> src, uint64_t src_off,
const nvn_texdesc *d, unsigned level, unsigned layer,
id<MTLBuffer> dst, uint64_t dst_off, size_t dst_pitch);
id<MTLBuffer> dst, uint64_t dst_off, size_t dst_pitch);
#if TARGET_OS_OSX
int nvn_deswz_gpu_encode_m4(id<MTL4ComputeCommandEncoder> enc, id<MTL4ArgumentTable> table,
id<MTLBuffer> params, size_t params_off, id<MTLBuffer> src, uint64_t src_off,
const nvn_texdesc *d, unsigned level, unsigned layer,
id<MTLBuffer> dst, uint64_t dst_off, size_t dst_pitch);
#endif
34 changes: 30 additions & 4 deletions dm/gfx/tex/nvn_deswizzle_gpu.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
static const char *const k_nvn_deswz_msl =
"#include <metal_stdlib>\n"
"using namespace metal;\n"
"struct P { ulong src_off, dst_off, base, pitch; uint row_bytes, rows, slices, th, td; };\n"
"struct P { ulong src_off, dst_off, base, pitch; uint row_bytes, rows, slices, th, td, gobs_x; };\n"
"kernel void deswz(device const uchar *src [[buffer(0)]], device uchar *dst [[buffer(1)]], constant P& p [[buffer(2)]], uint3 id [[thread_position_in_grid]]) {\n"
" uint bh = 1u << p.th, bd = 1u << p.td, gobs_x = (p.row_bytes + 63u) >> 6, blocks_y = (p.rows + (8u * bh) - 1u) >> (3u + p.th);\n"
" uint bh = 1u << p.th, bd = 1u << p.td, gobs_x = p.gobs_x, blocks_y = (p.rows + (8u * bh) - 1u) >> (3u + p.th);\n"
" uint tid = id.x & 31u, gob_x = id.x >> 5, gob_y = id.y, z = id.z; uint xseg = ((tid >> 4) << 1) | ((tid >> 1) & 1u), y = (gob_y << 3) | (((tid >> 2) & 3u) << 1) | (tid & 1u), x = (gob_x << 6) | (xseg << 4);\n"
" if (x >= p.row_bytes || y >= p.rows || z >= p.slices) return;\n"
" uint n = min(16u, p.row_bytes - x); ulong block = 512ul * bh * bd; ulong gob_in_block = ((ulong)(z & (bd - 1u)) << p.th) + (gob_y & (bh - 1u)); ulong so = p.src_off + p.base + ((ulong)(z >> p.td) * gobs_x * blocks_y + (gob_y >> p.th) * gobs_x + gob_x) * block + gob_in_block * 512ul + (ulong)tid * 16ul; ulong doff = p.dst_off + ((ulong)z * p.rows + y) * p.pitch + x;\n"
Expand Down Expand Up @@ -35,8 +35,8 @@ int nvn_deswz_gpu_encode(id<MTLComputeCommandEncoder> enc, id<MTLBuffer> src, ui
uint64_t base;
unsigned th, td;
if (nvn_deswizzle_geom(d, level, layer, &row_bytes, &rows, &slices, &base, &th, &td, &bpb) || dst_pitch < row_bytes) return -1;
struct { uint64_t src_off, dst_off, base, pitch; uint32_t row_bytes, rows, slices, th, td; } p = {
src_off, dst_off, base, dst_pitch, row_bytes, rows, slices, th, td
struct { uint64_t src_off, dst_off, base, pitch; uint32_t row_bytes, rows, slices, th, td, gobs_x; } p = {
src_off, dst_off, base, dst_pitch, row_bytes, rows, slices, th, td, MAX(nvn_tex_level_gobs(d, level), (row_bytes + 63u) >> 6) // GOB columns as laid out (nvn_tex_level_gobs)
};
[enc setComputePipelineState:g_nvn_deswz_pipeline];
[enc setBuffer:src offset:0 atIndex:0];
Expand All @@ -47,3 +47,29 @@ int nvn_deswz_gpu_encode(id<MTLComputeCommandEncoder> enc, id<MTLBuffer> src, ui
[enc dispatchThreadgroups:MTLSizeMake(gobs_x, gobs_y, slices) threadsPerThreadgroup:MTLSizeMake(32, 1, 1)];
return 0;
}

#if TARGET_OS_OSX
int nvn_deswz_gpu_encode_m4(id<MTL4ComputeCommandEncoder> enc, id<MTL4ArgumentTable> table,
id<MTLBuffer> params, size_t params_off, id<MTLBuffer> src, uint64_t src_off,
const nvn_texdesc *d, unsigned level, unsigned layer,
id<MTLBuffer> dst, uint64_t dst_off, size_t dst_pitch) {
if (!g_nvn_deswz_pipeline || !enc || !table || !params || !src || !dst || !d) return -1;
uint32_t row_bytes, rows, slices, bpb;
uint64_t base;
unsigned th, td;
if (nvn_deswizzle_geom(d, level, layer, &row_bytes, &rows, &slices, &base, &th, &td, &bpb) || dst_pitch < row_bytes) return -1;
struct { uint64_t src_off, dst_off, base, pitch; uint32_t row_bytes, rows, slices, th, td, gobs_x; } p = {
src_off, dst_off, base, dst_pitch, row_bytes, rows, slices, th, td, MAX(nvn_tex_level_gobs(d, level), (row_bytes + 63u) >> 6) // GOB columns as laid out (nvn_tex_level_gobs)
};
memcpy((uint8_t *)params.contents + params_off, &p, sizeof p);
[enc setArgumentTable:table];
[enc setComputePipelineState:g_nvn_deswz_pipeline];
[table setAddress:src.gpuAddress atIndex:0];
[table setAddress:dst.gpuAddress atIndex:1];
[table setAddress:params.gpuAddress + params_off atIndex:2];
NSUInteger gobs_x = (row_bytes + 63u) >> 6;
NSUInteger gobs_y = (rows + 7u) >> 3;
[enc dispatchThreadgroups:MTLSizeMake(gobs_x, gobs_y, slices) threadsPerThreadgroup:MTLSizeMake(32, 1, 1)];
return 0;
}
#endif
4 changes: 2 additions & 2 deletions dm/host/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ SRC=main.c loader.c hle.c crash.c probe.c thread.c memstat.m haptics_apple.m $(s
# NVN null device + nn::vi + the Vulkan layer (dm/gfx/)
# Sources under dm/gfx, dm/gfx/nvn, dm/gfx/tex, dm/gfx/vk and dm/gfx/gl are included by wildcard (tests/ and metal/ do not go into the host): adding a file does not touch this line
GFX=$(sort $(wildcard ../gfx/*.c ../gfx/nvn/*.c ../gfx/nvn/*.m ../gfx/nvn/*.S ../gfx/tex/*.c ../gfx/tex/*.m ../gfx/vk/*.m ../gfx/gl/*.m))
GFX_H=$(sort $(wildcard ../gfx/nvn/*.h ../gfx/nvn/include/*.h ../gfx/tex/*.h ../gfx/tex/*.inc))
# Third party: libvpx ships only the VP9 decoder's sources (movie decoding, hle_movie.c), with its configuration headers in config/ (arm64, shared by Mac and iOS). Built separately into a static library, file by file, so host changes do not recompile it
GFX_H=$(sort $(wildcard ../gfx/nvn/*.h ../gfx/nvn/*.inc ../gfx/nvn/include/*.h ../gfx/tex/*.h ../gfx/tex/*.inc)) # *.inc: nvn_mtl.m includes them (nvn_geometry.inc and the iOS compatibility renderer); an edit there alone must rebuild the host
# Third party: libvpx ships the VP8 and VP9 decoder sources (movie decoding, hle_movie.c), with its configuration headers in config/ (arm64, shared by Mac and iOS). Built separately into a static library, file by file, so host changes do not recompile it
VPX=../third_party/libvpx
VPX_SRC=$(sort $(shell find $(VPX) -name '*.c'))
VPX_OBJ=$(patsubst $(VPX)/%.c,gen/vpx/%.o,$(VPX_SRC))
Expand Down
9 changes: 8 additions & 1 deletion dm/host/hle_audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,13 @@ static void backend_pull(aout *a, int backend, uint8_t *dst, uint64_t bytes) {
!atomic_compare_exchange_strong_explicit(&a->pulling, &free, 1, memory_order_acquire, memory_order_relaxed)) {
memset(dst, 0, bytes); return;
}
if (atomic_load_explicit(&a->consumer, memory_order_acquire) == backend) pull(a, dst, bytes);
if (atomic_load_explicit(&a->consumer, memory_order_acquire) == backend) {
// The device callback requests device-channel bytes. Six-channel guest
// output therefore needs the existing 6 -> 2 downmix, while the null
// sink consumes the original guest-channel stream directly.
if (backend == BK_AU) pull_device(a, dst, bytes);
else pull(a, dst, bytes);
}
else memset(dst, 0, bytes);
atomic_store_explicit(&a->pulling, 0, memory_order_release);
}
Expand Down Expand Up @@ -251,6 +257,7 @@ static OSStatus au_create(aout *a) {
a->au = NULL; if (!c) return kAudio_ParamError;
OSStatus created = AudioComponentInstanceNew(c, &a->au);
if (created) { a->au = NULL; return created; }
unsigned ch = (unsigned)device_channels(a);
AudioStreamBasicDescription f = {.mSampleRate = RATE, .mFormatID = kAudioFormatLinearPCM,
.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked,
.mBytesPerPacket = 2u * ch, .mFramesPerPacket = 1, .mBytesPerFrame = 2u * ch,
Expand Down
1 change: 1 addition & 0 deletions dm/host/hle_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ static void path_unlock(pthread_mutex_t **m) { pthread_mutex_unlock(*m); }
#define PATH_LOCK() pthread_mutex_t *path_guard __attribute__((cleanup(path_unlock))) = &g_path_lock; pthread_mutex_lock(path_guard)
static uint8_t g_fmode[65536]; // fd → open mode
static uint8_t g_fsave[65536]; // fd → whether it is on a transactional save mount (the save write injection point only counts these)
static _Atomic uint16_t g_ftxn[65536]; // fd → 1-based canonical transaction index, or 0
// DM_ROMFS_REDIRECTS=<TSV>: virtual sequential archives backed by disjoint
// ranges of one materialized RomFS file. Some archive formats require their
// index offsets to stay both sorted and separated by each record's aligned
Expand Down
81 changes: 67 additions & 14 deletions dm/host/hle_oodle.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,28 @@ static uint64_t func_start(const module_t *m, uint64_t off) { // start of the
for (uint64_t i = 0; i < n; i++) { uint64_t s = eh_read(b, &o, tenc, ehs); eh_read(b, &o, tenc, ehs); if (s <= off && s > best) best = s; }
return best;
}
static uint64_t oodle_named_find(const module_t *m, const char *nm) {
size_t nm_n = strlen(nm) + 1; // include the terminating NUL, so a profiler-name substring cannot match
const uint8_t *b = m->base, *ro = b + m->ro_off, *re = b + m->data_off;
uint64_t strs[8]; int ns = 0;
for (const uint8_t *p = ro; ns < 8 && (p = memmem(p, (size_t)(re - p), nm, nm_n)); p += nm_n) if (p[-1] == 0) strs[ns++] = (uint64_t)(p - b);
if (!ns) die("DM_OODLE_JOB: main has no \"%s\" string", nm);
uint64_t fn = 0; int nfn = 0;
for (uint64_t pc = 0; pc + 4 <= m->text_end; pc += 4) {
static uint64_t func_next(const module_t *m, uint64_t off) {
const uint8_t *b = m->base; uint32_t m0; int32_t eh; memcpy(&m0, b + 4, 4); memcpy(&eh, b + m0 + 16, 4);
uint64_t ehs = m0 + (int64_t)eh, o = ehs + 4, best = m->text_end; int penc = b[ehs + 1], cenc = b[ehs + 2], tenc = b[ehs + 3];
eh_read(b, &o, penc, ehs); uint64_t n = eh_read(b, &o, cenc, ehs);
for (uint64_t i = 0; i < n; i++) { uint64_t s = eh_read(b, &o, tenc, ehs); eh_read(b, &o, tenc, ehs); if (s > off && s < best) best = s; }
return best;
}
static int str_fns(const module_t *m, const char *needle, int whole, uint64_t *fns, int max) {
const uint8_t *b = m->base, *ro = b + m->ro_off, *re = b + m->data_off; size_t len = strlen(needle);
uint64_t strs[16]; int ns = 0, nf = 0;
for (const uint8_t *p = ro; ns < 16 && (p = memmem(p, (size_t)(re - p), needle, len)); p += len) {
const uint8_t *s = p; while (s > ro && s[-1]) s--;
if (!whole || (s == p && p[len] == 0)) strs[ns++] = (uint64_t)(s - b);
}
for (uint64_t pc = 0; ns && pc + 4 <= m->text_end; pc += 4) {
uint32_t w; memcpy(&w, b + pc, 4);
if ((w & 0x9F000000) != 0x90000000) continue; // adrp xd, page
if ((w & 0x9F000000) != 0x90000000) continue;
int64_t imm = (int64_t)(((w >> 29) & 3) | (((w >> 5) & 0x7FFFF) << 2)); if (imm & (1 << 20)) imm -= 1 << 21;
uint64_t page = (pc & ~0xFFFull) + (uint64_t)(imm << 12);
for (int k = 1; k < 8 && pc + 4 * k + 4 <= m->text_end; k++) { // the add that completes it, within a few instructions
for (int k = 1; k < 8 && pc + 4 * k + 4 <= m->text_end; k++) {
uint32_t w2; memcpy(&w2, b + pc + 4 * k, 4);
if ((w2 & 0xFF800000) != 0x91000000 || ((w2 >> 5) & 31) != (w & 31)) continue; // add xd, xn = xd of the adrp, #imm
if ((w2 & 0xFF800000) != 0x91000000 || ((w2 >> 5) & 31) != (w & 31)) continue;
uint64_t a = page + ((w2 >> 10) & 0xFFF);
for (int s = 0; s < ns; s++) if (a == strs[s]) {
uint64_t f = func_start(m, pc + 4 * k); int seen = 0;
Expand All @@ -59,10 +66,56 @@ static uint64_t oodle_named_find(const module_t *m, const char *nm) {
break;
}
}
if (nfn != 1) die("DM_OODLE_JOB: %d functions reference \"%s\" in main (expected exactly 1)", nfn, nm);
return fn;
for (int i = 1; i < nf; i++) for (int j = i; j > 0 && fns[j - 1] > fns[j]; j--) { uint64_t t = fns[j]; fns[j] = fns[j - 1]; fns[j - 1] = t; }
return nf;
}
static uint64_t oodle_named_find(const module_t *m, const char *name) {
uint64_t fns[4]; int n = str_fns(m, name, 1, fns, 4);
if (n != 1) die("DM_OODLE_JOB: %d functions reference \"%s\" in main (expected exactly 1)", n, name);
return fns[0];
}
static uint64_t oodle_find(const module_t *m) {
uint64_t fns[4]; int nfn = str_fns(m, "OodleLZ_Decompress", 1, fns, 4);
if (nfn > 1) die("DM_OODLE_JOB: %d functions reference \"OodleLZ_Decompress\" in main (expected exactly 1)", nfn);
return nfn ? fns[0] : 0;
}

typedef int32_t (*lzd_size_fn)(int32_t compressor, int64_t raw_len);
typedef void *(*lzd_create_fn)(int32_t compressor, int64_t raw_len, void *mem, int64_t mem_size);
typedef void (*lzd_destroy_fn)(void *dec);
typedef int32_t (*lzd_some_fn)(void *dec, int32_t *out, void *buf, int64_t pos, int64_t size, int64_t avail, const void *comp, int64_t comp_avail, int64_t fuzz, int64_t crc, int64_t verb, int64_t phase);
static struct { lzd_size_fn size; lzd_create_fn create; lzd_destroy_fn destroy; lzd_some_fn some; } g_lzd;
static uint64_t lzd_find(const module_t *m) {
uint64_t src[4], leg[4]; int ns = str_fns(m, "oodlelzcompressors.cpp", 0, src, 4), nl = str_fns(m, "OODLE ERROR : OodleLZ_Decompress: asked to decode Legacy compressor", 0, leg, 4);
if (ns != 2 || nl != 1 || leg[0] != src[0])
die("DM_OODLE_JOB: main has no Oodle decoder entry point to find");
uint64_t create = func_next(m, src[0]), destroy = func_next(m, create);
if (func_next(m, destroy) != src[1]) die("DM_OODLE_JOB: Oodle streaming decoder layout is not recognized");
const uint8_t *b = m->base;
g_lzd.size = (lzd_size_fn)(void *)(b + src[0]); g_lzd.create = (lzd_create_fn)(void *)(b + create);
g_lzd.destroy = (lzd_destroy_fn)(void *)(b + destroy); g_lzd.some = (lzd_some_fn)(void *)(b + src[1]);
return src[1];
}
static int32_t lzd_compressor(const uint8_t *c, int64_t cl) {
if (cl < 2 || (c[0] & 0x0F) != 0x0C) return -1;
switch (c[1] & 0x7F) { case 6: return 8; case 10: return 9; case 12: return 13; default: return -1; }
}
static int64_t lzd_run(const void *c, int64_t cl, void *r, int64_t rl, void *scratch, int64_t scratch_n) {
int32_t comp = lzd_compressor(c, cl); if (comp < 0) return -1;
int64_t need = g_lzd.size(comp, rl); void *mem = scratch, *own = NULL;
if (need <= 0) return -1;
if (need + 64 > scratch_n) { if (!(own = malloc((size_t)need + 64))) return -1; mem = own; }
void *dec = g_lzd.create(comp, rl, mem, need + 64);
if (!dec) { free(own); return -1; }
int64_t pos = 0, cpos = 0;
while (pos < rl) {
int32_t out[4] = {0};
if (!g_lzd.some(dec, out, r, pos, rl, rl - pos, (const uint8_t *)c + cpos, cl - cpos, 1, 0, 0, 3) || (out[0] <= 0 && out[1] <= 0)) break;
pos += out[0]; cpos += out[1];
}
g_lzd.destroy(dec); free(own);
return pos;
}
static uint64_t oodle_find(const module_t *m) { return oodle_named_find(m, "OodleLZ_Decompress"); }

// OodleLZ_Decompress(compBuf, compBufSize, rawBuf, rawLen, fuzzSafe, checkCRC, verbosity, decBufBase, decBufSize, fpCallback, callbackUserData, decoderMemory, decoderMemorySize, threadPhase).
// Arguments 9-14 go on the stack; all are 64-bit here so the Apple and AAPCS64 stack layouts agree (Apple packs smaller stack arguments)
Expand Down
Loading