Merge the MacNativeGames rendering ports and the Depths terrain fix: storage buffers addressed from user uniform blocks are bound, so the Depths ground draws again; NVN texture sizing, attachment-less pass size, early fragment tests, depth sampling as one texel and D16 compare rounding follow MacNativeGames

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-10-26 16:26:51 +03:00
commit e3e0eb06c8
12 files changed
+289 -32

No files matched your search

+4
View File
@@ -874,6 +874,10 @@ static size_t build_request(char *o0, const char *hex, const char *stage, const
else memcpy(l, control + 0x748, 12);
o += sprintf(o, ",\"local\":[%u,%u,%u]", l[0], l[1], l[2]);
}
/* Control byte +0x718 of a fragment shader: early fragment tests (the NVN driver's early-Z-force, MacNativeGames
ae8f4f4). Without it a shader that stores to memory is depth-tested late, and the G-buffer draws' EQUAL test
after the depth prepass fails along contour lines. Already part of the code hash (the whole control section). */
if (!strcmp(stage, "ps") && (control[0x718] & 1)) o += sprintf(o, ",\"early_fragment_tests\":true");
int si = !strcmp(stage, "vs") ? 0 : !strcmp(stage, "cs") ? 2 : 1; // gfx_shader_set_host answers
if (!host_f64) o += sprintf(o, ",\"softfloat64\":true");
if (host_sbuf) o += sprintf(o, ",\"sbuf_align\":%u", host_sbuf);
+1 -1
View File
@@ -17,7 +17,7 @@
typedef struct {
int target, format, flags, samples; // NVNtextureTarget / NVNformat / NVNtextureFlags / sample count (0 or 1 = single-sampled)
uint32_t width, height, depth, levels, stride;
signed int pkg_block_h:8; unsigned int swizzle_r:3,swizzle_g:3,swizzle_b:3,swizzle_a:3,swizzle_pad:12; // Byte 0 of the packaged texture layout parameters (log2 of block height) + 1; 0 = use the automatic algorithm
signed int pkg_block_h:8; unsigned int swizzle_r:3,swizzle_g:3,swizzle_b:3,swizzle_a:3,swizzle_pad:12; // Byte 0 of the packaged texture layout parameters + 1 (low nibble log2 block height, high nibble log2 block depth); 0 = use the automatic algorithm
} nvn_texdesc;
typedef struct {
+7 -3
View File
@@ -75,7 +75,7 @@ static void shape(const nvn_texdesc *d, unsigned *dpl, int *layered, uint32_t *w
case 2: *dpl = 3; break; // 3D
case 4: case 6: *dpl = 2; *layered = 1; break; // 2D_ARRAY / MS_ARRAY
case 8: *dpl = 2; *z = 6; *layered = 1; break; // CUBEMAP
case 9: *dpl = 2; *z = d->depth * 6; *layered = 1; break; // CUBEMAP_ARRAY
case 9: *dpl = 2; *layered = 1; break; // CUBEMAP_ARRAY: depth already counts layer-faces
default: *dpl = 2; *z = 1; break; // 2D / MS / RECTANGLE
}
if (*z == 0) *z = 1;
@@ -140,14 +140,18 @@ void nvn_tex_layout(const nvn_texdesc *d, nvn_texlayout *o) {
if (nvn_tex_is_linear(d)) { // Pitch storage: explicit stride, otherwise 32-byte (LINEAR) / 128-byte (LINEAR_RENDER_TARGET) alignment
o->align = (d->flags & 0x40) ? 128 : 32;
uint32_t stride = nvn_tex_linear_stride(d);
o->layer_size = o->size = (uint64_t)stride * blk(h, t.bh);
o->layer_size = o->size = (uint64_t)stride * h; // the driver counts pixel rows, also for block-compressed formats
return;
}
unsigned tileH = 0, tileD = 0;
/* NVN truncates height*1.5 to whole GOB rows before choosing the power of
two. Rounding up here changes e.g. 40 BC rows from H2 to H3 even when
the total allocation size is unchanged, so padding becomes black texels. */
if (dpl == 2) { uint32_t ah = blk(h * sy, t.bh); tileH = d->pkg_block_h ? (unsigned)d->pkg_block_h - 1 : pick_tile((ah + ah / 2) / 8); }
if (d->pkg_block_h) { // packaged byte 0: low nibble block height, high nibble block depth
unsigned pl = (unsigned)(d->pkg_block_h - 1) & 0xFF;
if (dpl >= 2) tileH = pl & 0xF;
if (dpl == 3) tileD = pl >> 4;
} else if (dpl == 2) { uint32_t ah = blk(h * sy, t.bh); tileH = pick_tile((ah + ah / 2) / 8); }
else if (dpl == 3) tileD = pick_tile(z + z / 2);
uint64_t ls = layer_bytes(d, dpl, layered, w, h, z, tileH, tileD, t);
if ((d->flags & 0x200) && !d->pkg_block_h && minimal_layout_on()) {
+3
View File
@@ -258,6 +258,9 @@ void vk_texture_lock(void);
void vk_texture_unlock(void);
const VkTexImage *vk_texture_by_id_locked(int id, view_t *view, int *has_view);
VkSampler vk_sampler_by_id_locked(int id);
// The slot's sampler with nearest filtering and no anisotropy: a depth texture read without a compare lands on one texel
// on the Switch although the game binds a linear sampler (MacNativeGames 6283599). VK_NULL_HANDLE for an empty slot.
VkSampler vk_sampler_point_by_id_locked(int id);
uint32_t vk_texture_slots_gen(void);
uint32_t vk_sampler_gen(void); // changes whenever a sampler slot is registered
void vk_texture_gpu_written(const tex_t *t); // rendered or copied into by the GPU: no re-upload from storage
+72 -7
View File
@@ -16,6 +16,13 @@
// snapshots one GPU buffer range after every logged pass (buf_<pass>.bin and bufs.txt, see buf_snap).
// PNGs are opaque (alpha dropped). Integer formats are scaled by the image maximum, depth by its min..max range, SNORM
// as x/2+1/2, UNORM and sRGB bytes written as stored. Caps: MAX_PASSES passes copied, PNG_CAP bytes of PNGs.
// TOTK_DUMP_TEX=<id>|<w>x<h>[,...]: those texture slots, or 2D textures of that size, are copied where first sampled,
// whatever their format.
// TOTK_DUMP_UBO=1: each listed pipeline also prints the first 16 vec4 of its user uniform blocks (c3 and up), read from
// guest memory when the entry is listed. A block the submission rewrites with UpdateUniformBuffer shows its value from
// before the submission (vk_exec_draw.c upd_note).
// TOTK_DUMP_SKIP_RUNS=1: a render pass with the same targets as the render pass before it is logged but not copied
// (the terrain decals draw one pass each into the G-buffer and would use up both caps).
#include <windows.h>
#include <math.h>
#include <stdarg.h>
@@ -40,7 +47,12 @@ int vk_dump_armed;
static uint64_t dump_frame, log_from; // log_from: TOTK_DUMP_LOG, first frame logged to passes.txt without images
static char dir[400];
static FILE *passes_f, *images_f;
static int seq, finished, dump_base; // dump_base: first pass number of the dump frame
static int seq, finished, copied; // copied: passes and dispatches of the dump frame whose images were copied
static int skip_runs, dump_ubo; // TOTK_DUMP_SKIP_RUNS, TOTK_DUMP_UBO
static uint32_t want_tex[16][2]; // TOTK_DUMP_TEX: slot id and 0, or width and height
static int nwant_tex;
static uint64_t last_targets; // targets_sig of the last render pass; 0 after a dispatch
static const char *pass_skip; // why the current pass copies nothing, or NULL
static volatile LONG64 png_bytes, inflight;
static SRWLOCK rec_lock = SRWLOCK_INIT, img_lock = SRWLOCK_INIT;
@@ -556,6 +568,23 @@ static void describe_bindings(Str *b, const VkExec *ex, const VkPipeInfo *info)
sp(b, "\n");
continue;
}
if (r && bd->kind == GFX_RES_UBO && dump_ubo && r->slot >= 3 && r->slot - 3 < 18) {
int st = nvn_stage(bd->stage);
const p_bind_buf *u = &ex->ubo[st][r->slot - 3];
VkMemSpan ms;
sp(b, " %s ubo c%d addr %#llx size %#llx", stn[st], r->slot, (unsigned long long)u->addr, (unsigned long long)u->size);
if (u->addr && vk_mem_resolve(u->addr, 1, &ms)) {
uint64_t n = u->size && u->size < ms.avail ? u->size : ms.avail;
const float *f = (const float *)(uintptr_t)u->addr;
for (uint64_t k = 0; k < 16 && (k + 1) * 16 <= n; k++)
sp(b, "%s[%llu] %g %g %g %g", k % 4 ? " " : "\n ", (unsigned long long)k, f[4 * k], f[4 * k + 1],
f[4 * k + 2], f[4 * k + 3]);
} else {
sp(b, " (not mapped)");
}
sp(b, "\n");
continue;
}
if (!r || (bd->kind != GFX_RES_TEX && bd->kind != GFX_RES_IMG)) continue;
int st = nvn_stage(bd->stage), img = bd->kind == GFX_RES_IMG;
char dt[24], fn[16];
@@ -607,7 +636,12 @@ static void describe_bindings(Str *b, const VkExec *ex, const VkPipeInfo *info)
ssw[3]);
if (hv && v.set_swizzle) sp(b, " view %u%u%u%u", v.swz[0], v.swz[1], v.swz[2], v.swz[3]);
sp(b, "\n");
int wanted = 0;
for (int k = 0; k < nwant_tex; k++)
wanted |= want_tex[k][1] ? want_tex[k][0] == t->width && want_tex[k][1] == t->height
: want_tex[k][0] == (uint32_t)NVN_HANDLE_TEX(h);
if (t->type == VK_IMAGE_TYPE_3D) queue_lut(t, NVN_HANDLE_TEX(h));
else if (wanted && t->type == VK_IMAGE_TYPE_2D && t->samples == 1) queue_lut(t, NVN_HANDLE_TEX(h));
else if (!img && t->type == VK_IMAGE_TYPE_2D && t->samples == 1 && (uint64_t)t->width * t->height <= TEX2D_CAP &&
(t->format == VK_FORMAT_R8_UNORM || t->format == VK_FORMAT_R8G8_UNORM ||
t->format == VK_FORMAT_R8G8B8A8_UNORM || t->format == VK_FORMAT_R8G8B8A8_SRGB ||
@@ -668,7 +702,8 @@ static void target_line(VkExec *ex, const VkExecTarget *t, const char *tag, int
// a block view: the plane's blocks, read as the view format (one texel per block)
uint32_t tw = t->tex->d.width >> t->level ? t->tex->d.width >> t->level : 1;
uint32_t th = t->tex->d.height >> t->level ? t->tex->d.height >> t->level : 1;
const char *why = n - dump_base >= MAX_PASSES ? "pass cap" : t->samples > 1 ? "multisampled" :
(void)n;
const char *why = pass_skip ? pass_skip : t->samples > 1 ? "multisampled" :
t->blocks ? copy_region(ex, t->image, t->format, t->aspect, t->level, t->is3d ? 0 : t->base_layer,
t->is3d ? t->base_layer : 0, (VkExtent3D){tw, th, 1}, t->width,
t->height, 1, name) :
@@ -680,11 +715,30 @@ static void target_line(VkExec *ex, const VkExecTarget *t, const char *tag, int
why ? why : "");
}
static uint64_t targets_sig(const VkExec *ex) {
uint64_t h = 1469598103934665603ull;
for (int i = 0; i < 9; i++) {
const VkExecTarget *t = i < 8 ? &ex->color[i] : &ex->depth;
uint64_t v[3] = {(uint64_t)(uintptr_t)t->image, t->level, t->base_layer};
for (int k = 0; k < 3; k++) h = (h ^ v[k]) * 1099511628211ull;
}
return h;
}
// Sets pass_skip for the pass or dispatch being logged and counts it when it copies.
static void pass_decide(int same_targets) {
if (frame_now() != dump_frame) copied = 0;
pass_skip = copied >= MAX_PASSES ? "pass cap" : same_targets ? "same targets as the previous pass" : NULL;
if (!pass_skip) copied++;
}
void vk_dump_pass_end(VkExec *ex) {
AcquireSRWLockExclusive(&rec_lock);
if (!active()) goto out;
int n = seq++;
if (frame_now() != dump_frame) dump_base = seq;
uint64_t sig = targets_sig(ex);
pass_decide(skip_runs && sig == last_targets);
last_targets = sig;
fprintf(passes_f, "pass %04d: render pass, draws %d, area %ux%u layers %u, fence %llu, frame %llu\n", n, pass_draws,
ex->pass_w, ex->pass_h, ex->pass_layers, (unsigned long long)ex->fence, (unsigned long long)frame_now());
for (int i = 0; i < 8; i++) {
@@ -699,7 +753,7 @@ void vk_dump_pass_end(VkExec *ex) {
free(ents[i].text);
}
if (ents_over) fprintf(passes_f, " ... %d more draws with other pipeline/texture sets\n", ents_over);
if (n - dump_base < MAX_PASSES) flush_luts(ex, n);
if (!pass_skip) flush_luts(ex, n);
else nluts = 0;
buf_snap(ex, n);
fflush(passes_f);
@@ -713,14 +767,15 @@ void vk_dump_dispatch(VkExec *ex, const GfxShader *cs, VkPipeline p, const VkPip
AcquireSRWLockExclusive(&rec_lock);
if (!active()) goto out;
int n = seq++;
if (frame_now() != dump_frame) dump_base = seq;
pass_decide(0);
last_targets = 0;
Str b = {0};
describe_bindings(&b, ex, info);
fprintf(passes_f, "pass %04d: dispatch %dx%dx%d cs %016llx prog %llu caller %#llx, fence %llu\n%s", n, d->x, d->y,
d->z, (unsigned long long)shader_hash(cs), (unsigned long long)(ex->prog ? ex->prog->serial : 0),
(unsigned long long)d->caller, (unsigned long long)ex->fence, b.s ? b.s : "");
free(b.s);
for (uint32_t i = 0; i < info->n_bindings && n - dump_base < MAX_PASSES; i++) { // storage images the dispatch writes
for (uint32_t i = 0; i < info->n_bindings && !pass_skip; i++) { // storage images the dispatch writes
const VkPipeBinding *bd = &info->bindings[i];
if (!bd->res || bd->kind != GFX_RES_IMG || !(bd->res->flags & 4) || bd->type != VK_DESCRIPTOR_TYPE_STORAGE_IMAGE)
continue;
@@ -745,7 +800,7 @@ void vk_dump_dispatch(VkExec *ex, const GfxShader *cs, VkPipeline p, const VkPip
fprintf(passes_f, " writes img slot %d level %u layer %u depth %u -> %s.png%s%s\n", bd->res->slot, lv, layer,
dd, name, why ? " skipped: " : "", why ? why : "");
}
if (n - dump_base < MAX_PASSES) flush_luts(ex, n);
if (!pass_skip) flush_luts(ex, n);
else nluts = 0;
buf_snap(ex, n);
fflush(passes_f);
@@ -821,6 +876,16 @@ void vk_dump_init(void) {
if (!e || !*e || !(dump_frame = strtoull(e, NULL, 10))) return;
const char *lf = cfg_get("TOTK_DUMP_LOG");
if (lf && *lf) log_from = strtoull(lf, NULL, 10);
const char *sr = cfg_get("TOTK_DUMP_SKIP_RUNS");
skip_runs = sr && *sr && *sr != '0';
const char *du = cfg_get("TOTK_DUMP_UBO");
dump_ubo = du && *du && *du != '0';
const char *wt = cfg_get("TOTK_DUMP_TEX");
for (char *q = (char *)wt; q && *q && nwant_tex < 16; q += *q == ',') {
uint32_t *w = want_tex[nwant_tex++];
w[0] = (uint32_t)strtoul(q, &q, 10);
w[1] = *q == 'x' ? (uint32_t)strtoul(q + 1, &q, 10) : 0;
}
const char *d = cfg_get("TOTK_DUMP_DIR");
snprintf(dir, sizeof dir, "%s", d && *d ? d : "dump");
make_dirs(dir);
+23 -3
View File
@@ -12,6 +12,7 @@
// full barrier before each pass), the scissor clip and attachment clears follow LAS Recomp
// runtime/nvn/vulkan/exec.c:855-1149, 3553-3778 (https://github.com/peiikai/LASRecomp @
// da35b83a9611d046a9a681aae668d7085954d37f, MIT, Copyright (c) 2026 Peiikai).
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@@ -204,6 +205,21 @@ static void resolve_target(const tex_t *t, const view_t *v, int has_v, int depth
if (!o->aspect) o->image = VK_NULL_HANDLE;
}
/* The size of a pass without attachments: the far corner of viewport 0, else of the scissor, 1..16384 (MacNativeGames
62da927, nxnative 7ed061f). TotK draws a fragment shader that only writes a storage buffer into a 1x1 viewport; a
pass sized by the viewport's width alone puts an offset viewport outside the scissor, and the cave rock loses its
ambient light. */
void vk_exec_noatt_dims(const VkExec *ex, uint32_t *w, uint32_t *h) {
double x1 = 0, y1 = 0;
if (ex->has_vp) {
x1 = ceil(fmax(ex->vp[0][0], ex->vp[0][0] + ex->vp[0][2]));
y1 = ceil(fmax(ex->vp[0][1], ex->vp[0][1] + ex->vp[0][3]));
}
if ((x1 < 1 || y1 < 1) && ex->has_scissor) x1 = (double)ex->scissor.x + ex->scissor.w, y1 = (double)ex->scissor.y + ex->scissor.h;
*w = x1 < 1 ? 1 : x1 > 16384 ? 16384 : (uint32_t)x1;
*h = y1 < 1 ? 1 : y1 > 16384 ? 16384 : (uint32_t)y1;
}
// Resolves ex->rt to this command buffer's images and derives the pass parameters and the pipeline's target formats.
static void resolve_rt(VkExec *ex) {
if (ex->rt_resolved) return;
@@ -233,10 +249,14 @@ static void resolve_rt(VkExec *ex) {
if (d->layers < layers) layers = d->layers;
if (!samples) samples = d->samples;
}
if (w == UINT32_MAX) { // no attachment: the area of viewport 0
w = ex->has_vp && ex->vp[0][2] >= 1 ? (uint32_t)ex->vp[0][2] : 16;
h = ex->has_vp && ex->vp[0][3] >= 1 ? (uint32_t)ex->vp[0][3] : 16;
ex->pass_noatt = w == UINT32_MAX;
if (ex->pass_noatt) {
vk_exec_noatt_dims(ex, &w, &h);
layers = 1;
static int logged;
if (logged < 8 && ex->has_vp && (ex->vp[0][0] != 0 || ex->vp[0][1] != 0 || ex->vp[0][2] != w || ex->vp[0][3] != h))
logged++, rt_log("vk_exec: pass without attachments %ux%u for viewport %g,%g %gx%g", w, h, ex->vp[0][0],
ex->vp[0][1], ex->vp[0][2], ex->vp[0][3]);
}
ex->pass_w = w, ex->pass_h = h, ex->pass_layers = layers;
ex->pass_ncolor = (uint32_t)n;
+2
View File
@@ -51,6 +51,7 @@ typedef struct VkExec {
int rt_resolved; // color[] and depth hold this command buffer's images for rt
VkExecTarget color[8], depth; // resolved targets
uint32_t pass_w, pass_h, pass_layers; // render area and layer count of the open (or next) pass
int pass_noatt; // the pass has no attachment: its size follows the draws (vk_exec_noatt_dims)
VkFormat pass_color[8], pass_depth, pass_stencil; // attachment formats of the pass (VkPipelineRenderingCreateInfo);
// VK_FORMAT_UNDEFINED: none
uint32_t pass_ncolor, pass_samples; // colorAttachmentCount and rasterization samples of the pass
@@ -123,6 +124,7 @@ void vk_exec_draw_new_cb(void);
VkBuffer vk_exec_inline_buffer(VkDeviceSize size);
// Scissor 0 clipped to the render area of the open pass; extent 0: nothing to draw.
VkRect2D vk_exec_scissor(const VkExec *ex);
void vk_exec_noatt_dims(const VkExec *ex, uint32_t *w, uint32_t *h); // pass size when no attachment is set
// 1: every target of the current pass (any 1: at least one) was rendered in the previous presented frame (per-frame
// targets); 0 otherwise, or when the pass has none.
int vk_exec_targets_recent(VkExec *ex, int any);
+90 -9
View File
@@ -157,9 +157,13 @@ static struct {
// The bound-slot bytes of VkExec a pipeline's descriptors read, as ranges merged where they touch, built once per
// pipeline info: c0's storage table, the uniform and storage ranges of the bound slots and the texture and image
// handles. sup and c0: the support inputs and the base instance count too. ok 0: never skipped (too many descriptors).
// handles. sup and c0: the support inputs and the base instance count too. p: storage buffers whose address the shader
// loads from a user uniform block; the address read from the block counts too. ok 0: never skipped (too many
// descriptors or such buffers).
#define RS_PTRS 4
typedef struct { uint32_t off, len; } RsRange;
typedef struct { uint32_t n, bytes; uint8_t sup, c0, ok; RsRange r[]; } RsList;
typedef struct { int st; const GfxShaderRes *res; } RsPtr;
typedef struct { uint32_t n, bytes; uint8_t sup, c0, ok, np; RsPtr p[RS_PTRS]; RsRange r[]; } RsList;
static nvn_hmap rs_lists; // VkPipeInfo pointer -> RsList, until shutdown
static const VkPipeInfo *rs_last; // the last lookup
static const RsList *rs_last_l;
@@ -776,7 +780,7 @@ static VkDescriptorBufferInfo ubo_info(VkExec *ex, int st, int slot) {
// (GfxShaderRes.slot); the NVN slot is the entry of the driver constant buffer c0 that holds the buffer's address
// (cbuf_slot 0, handle = word offset): 16-byte entries at byte 0x110 (vertex), 0x510 (fragment) and 0x310 (compute).
// A compute address at c0 0x210..0x30f is uniform block (offset - 0x210) / 16 read as storage: *ubo is set. -1: the
// address is not in c0 (the shader loads it from a user uniform block, not replayed) or the stage has no known table.
// address is not in c0 (the shader loads it from a user uniform block: ubo_ptr) or the stage has no known table.
// Mapping: MacNativeGames games/totk/tools/totk_xlate/Service.cs:206-222 @ b75be8cf (MIT, Copyright (c) 2026 jhaizhou).
static int ssbo_slot(const GfxShaderRes *r, int st, int *ubo) {
*ubo = 0;
@@ -789,12 +793,63 @@ static int ssbo_slot(const GfxShaderRes *r, int st, int *ubo) {
return base >= 0 && off >= base && off < base + 0x100 && (off - base) % 16 == 0 ? (off - base) / 16 : -1;
}
// 1 when the shader loads the storage buffer's address from a user uniform block (cbuf_slot 3.., NVN uniform slot
// cbuf_slot - 3, byte offset handle * 4). The Depths terrain reads its vertices through one, about 500 draws a frame;
// with a zero buffer they collapse and the ground is missing. MacNativeGames resolves these the same way (NVN_RB_SBUFU,
// games/totk/third_party/d2r_gfx/gfx/nvn/nvn_mtl.m @ b75be8cf).
static int ubo_ptr_res(const GfxShaderRes *r) {
return r->cbuf_slot >= 3 && r->cbuf_slot - 3 < 18;
}
// The inline uniform updates of the submission being replayed, newest last (a ring of UPD_N). op_update_ub writes them
// into the buffer's GPU copy; guest memory gets them only from the write-back when the submission ends. The terrain
// rewrites its pointer block this way before every draw, all at one address, so ubo_ptr reads the latest update that
// covers the pointer, else memory. Payloads stay in the recorded command buffer for the whole replay.
#define UPD_N 4096
static struct {
uint64_t fence; // the submission the entries belong to
uint32_t n; // updates noted in it
struct { uint64_t lo, hi; const uint8_t *data; } u[UPD_N];
} UPD;
static void upd_note(const VkExec *ex, const p_update_ub *u) {
if (UPD.fence != ex->fence) UPD.fence = ex->fence, UPD.n = 0;
UPD.u[UPD.n++ % UPD_N] = (typeof(UPD.u[0])){u->dst, u->dst + u->size, u->data};
}
// 1 when an update of this submission supplied [a, a+n).
static int upd_read(const VkExec *ex, uint64_t a, void *out, size_t n) {
if (UPD.fence != ex->fence) return 0;
for (uint32_t i = UPD.n, end = UPD.n > UPD_N ? UPD.n - UPD_N : 0; i-- > end;) {
const typeof(UPD.u[0]) *e = &UPD.u[i % UPD_N];
if (a >= e->hi || a + n <= e->lo) continue;
if (a < e->lo || a + n > e->hi) return 0; // partly covered: memory
memcpy(out, e->data + (a - e->lo), n);
return 1;
}
return 0;
}
// That address as the bound block holds it at this point of the replay; 0 when the block is unbound or too short. The
// block has no size field.
static uint64_t ubo_ptr(const VkExec *ex, int st, const GfxShaderRes *r) {
const p_bind_buf *u = &ex->ubo[st][r->cbuf_slot - 3];
uint64_t off = (uint64_t)r->handle * 4, a = 0;
VkMemSpan sp;
if (!u->addr || u->size < off + 8 || upd_read(ex, u->addr + off, &a, 8)) return a;
if (vk_mem_resolve(u->addr + off, 8, &sp)) memcpy(&a, (const void *)(uintptr_t)(u->addr + off), 8);
return a;
}
static VkDescriptorBufferInfo ssbo_info(VkExec *ex, int st, const GfxShaderRes *r, int written) {
uint64_t max = vk_ctx()->props.limits.maxStorageBufferRange, align = vk_ctx()->props.limits.minStorageBufferOffsetAlignment;
VkDescriptorBufferInfo bi;
int ubo, slot = ssbo_slot(r, st, &ubo);
if (slot >= 0 && slot < 16) {
const p_bind_buf *b = ubo ? &ex->ubo[st][slot] : &ex->ssbo[st][slot];
p_bind_buf up = {0};
const p_bind_buf *b = NULL;
if (slot >= 0 && slot < 16) b = ubo ? &ex->ubo[st][slot] : &ex->ssbo[st][slot];
else if (ubo_ptr_res(r)) up.addr = ubo_ptr(ex, st, r), b = &up; // size 0: up to the end of the backing
if (b) {
// vk_caps.c: shaders translated for sbuf_align address the buffer from its address rounded down to it.
uint64_t lead = vk_caps.sbuf_align ? b->addr & (vk_caps.sbuf_align - 1) : 0;
if (bind_range(ex, b->addr - lead, b->size ? b->size + lead : 0, align, max, ubo ? VK_MUSE_UBO : VK_MUSE_SSBO,
@@ -923,7 +978,14 @@ static void tex_info(VkExec *ex, const GfxShaderRes *r, uint64_t h, int storage,
if (img) view = get_view(ex, img, &v, hv, r->type, storage);
if (img && storage) vk_texture_storage_bound_locked(img);
if (img && !storage && ex->nfbl && in_fbl(ex, img->image)) layout = VK_IMAGE_LAYOUT_ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT;
if (!storage && !separate) samp = vk_sampler_by_id_locked((int)(h >> 20 & 0xfff));
if (!storage && !separate) {
// A depth texture sampled without a compare reads one texel on the Switch, whatever the sampler's filter:
// blending neighbouring depths puts reconstructed positions in mid-air at object edges (shadow masks).
int sid = (int)(h >> 20 & 0xfff);
int point = img && (img->aspect & VK_IMAGE_ASPECT_DEPTH_BIT) && !(r->type & 0x400) &&
vk_exec_view_aspect(img, &v, hv) != VK_IMAGE_ASPECT_STENCIL_BIT;
samp = point ? vk_sampler_point_by_id_locked(sid) : vk_sampler_by_id_locked(sid);
}
}
if (!view) {
int di = dummy_index(res_view_type(r->type), ms);
@@ -1056,7 +1118,9 @@ static int push_skip_on(void) { // TOTK_PUSH_SKIP=0: every graphics draw pushe
// - vk_mem_gen (buffer and offset of a span, mirror creation and per-submission sync);
// - the images in the feedback layout (the sampled layout);
// - per binding: the support inputs, c0's storage table and base instance, the bound uniform and storage ranges, the
// texture and image handles (RsList; compared only when a bind op ran since the last draw, bind_gen).
// texture and image handles (RsList; compared only when a bind op ran since the last draw, bind_gen);
// - storage buffer addresses read from user uniform blocks (RsList.p, ubo_ptr): compared on every draw, since the
// block's contents change without a bind op.
// The side effects of a build repeat as no-ops in the same command buffer and submission: c0 and support buffers kept
// in B, note_written's ranges, vk_mem_gpu_wrote's write-backs, the per-submission mirror sync and dirty census note,
// the views cached per submission. The census counts are added again. A build that copied guest bytes into transient
@@ -1085,7 +1149,8 @@ static const RsList *rs_list(const VkExec *ex, const VkPipeInfo *info) {
RsList *l = nvn_hm_get(&rs_lists, &info);
if (!l) {
static RsRange tmp[MAX_DESC];
uint32_t n = 0, nd = 0;
RsPtr ptr[RS_PTRS];
uint32_t n = 0, nd = 0, np = 0;
int sup = 0, c0 = 0, ok = 1;
for (uint32_t i = 0; i < info->n_bindings && ok; i++) {
const VkPipeBinding *b = &info->bindings[i];
@@ -1103,7 +1168,13 @@ static const RsList *rs_list(const VkExec *ex, const VkPipeInfo *info) {
} // slot 1: the program (bind_sig); others: the zero buffer
} else if (b->kind == GFX_RES_SSBO) { // every descriptor of the binding is the same range
int ubo, slot = ssbo_slot(r, st, &ubo);
if (slot >= 0 && slot < 16) rs_add(tmp, &n, ex, ubo ? &ex->ubo[st][slot] : &ex->ssbo[st][slot], sizeof(p_bind_buf));
if (slot >= 0 && slot < 16) {
rs_add(tmp, &n, ex, ubo ? &ex->ubo[st][slot] : &ex->ssbo[st][slot], sizeof(p_bind_buf));
} else if (ubo_ptr_res(r)) {
if (np == RS_PTRS) { ok = 0; break; }
rs_add(tmp, &n, ex, &ex->ubo[st][r->cbuf_slot - 3], sizeof(p_bind_buf));
ptr[np++] = (RsPtr){st, r};
}
} else if ((b->kind == GFX_RES_TEX || b->kind == GFX_RES_IMG) && b->type != VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER &&
b->type != VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER) {
int img = b->kind == GFX_RES_IMG, cap = img ? 32 : 128;
@@ -1119,6 +1190,8 @@ static const RsList *rs_list(const VkExec *ex, const VkPipeInfo *info) {
l = malloc(sizeof *l + sizeof *tmp * n);
if (!l) rt_fatal("vk: out of memory");
l->n = n, l->bytes = (uint32_t)bytes, l->sup = (uint8_t)sup, l->c0 = (uint8_t)c0, l->ok = (uint8_t)ok;
l->np = (uint8_t)np;
memcpy(l->p, ptr, sizeof *ptr * np);
memcpy(l->r, tmp, sizeof *tmp * n);
nvn_hm_put(&rs_lists, &info, l);
}
@@ -1152,6 +1225,7 @@ static int bind_sig(const VkExec *ex, const VkPipeInfo *info) {
PUT(ps);
}
if (l->c0) PUT(c0_binst);
for (int i = 0; i < l->np; i++) PUT(ubo_ptr(ex, l->p[i].st, l->p[i].res));
#undef PUT
diff |= n != RS.hn;
RS.hn = n;
@@ -1508,6 +1582,12 @@ static int prepare_draw(VkExec *ex, int prim, int32_t count, int restart) {
if (ex->render_disabled) return 0;
cb_res(ex);
dummies_init();
if (ex->pass_noatt) { // no attachment: the pass covers this draw's viewport or scissor, reopened when it needs more
uint32_t w, h;
vk_exec_noatt_dims(ex, &w, &h);
if (ex->in_pass && (w > ex->pass_w || h > ex->pass_h)) vk_exec_pass_end(ex);
if (!ex->in_pass) ex->rt_resolved = 0;
}
if (!ex->in_pass) ex->dirty |= VK_EXEC_DIRTY_VIEWPORT | VK_EXEC_DIRTY_SCISSOR;
vk_exec_pass_begin(ex);
VkRect2D sc = vk_exec_scissor(ex);
@@ -1800,6 +1880,7 @@ static void op_update_ub(VkExec *ex, const p_update_ub *u) {
ex->recorded++;
vk_mem_count(sp.cls, VK_MUSE_GPU_WRITE, u->size);
note_written(ex, u->dst, u->size);
upd_note(ex, u);
}
// ---- bind ops
+18 -2
View File
@@ -70,7 +70,8 @@ typedef struct {
view_t v;
tex_t t;
} TexSlot;
typedef struct { samp_par p; VkSampler s; uint8_t used; } SampSlot; // s: vk_sampler_for(&p) once looked up
typedef struct { samp_par p; VkSampler s, sp; uint8_t used; } SampSlot; // s: vk_sampler_for(&p) once looked up; sp:
// its point-sampled form (vk_sampler_point_by_id_locked)
static CRITICAL_SECTION tex_lock;
static INIT_ONCE tex_once = INIT_ONCE_STATIC_INIT;
@@ -1462,7 +1463,7 @@ void vk_sampler_register(int id, const samp_par *s) {
sslots = p, nsslots = n;
}
sslots[id].p = *s;
sslots[id].s = VK_NULL_HANDLE;
sslots[id].s = sslots[id].sp = VK_NULL_HANDLE;
sslots[id].used = 1;
samp_gen++;
unlock();
@@ -1503,6 +1504,21 @@ VkSampler vk_sampler_by_id_locked(int id) {
return v;
}
VkSampler vk_sampler_point_by_id_locked(int id) {
VkCtx *c = vk_ctx();
if (!c || id < 0 || id >= nsslots || !sslots[id].used) return VK_NULL_HANDLE;
SampSlot *s = &sslots[id];
if (!s->sp) {
samp_par k = s->p;
k.magf = NVN_MAG_FILTER_NEAREST;
k.minf = k.minf == NVN_MIN_FILTER_LINEAR ? NVN_MIN_FILTER_NEAREST :
k.minf > NVN_MIN_FILTER_LINEAR ? NVN_MIN_FILTER_NEAREST_MIPMAP_NEAREST : k.minf;
k.aniso = 1;
s->sp = sampler_for(c, &k);
}
return s->sp;
}
VkSampler vk_sampler_by_id(int id) {
lock();
VkSampler v = vk_sampler_by_id_locked(id);
+44 -6
View File
@@ -1,7 +1,7 @@
"""Build the shader translator: build/xlate/totk_xlate.exe (Ryujinx 1.3.2 + patches, NativeAOT, SPIR-V for Vulkan).
Copies tools/vendor/ryujinx to build/xlate/ryujinx, applies tools/xlate/patches/*.patch there (tools/vendor stays
byte-identical; 0001-0004 are MNG's, 0005 and 0006 are ours), then runs `dotnet publish`. Needs the .NET 10 SDK, the MSVC build
byte-identical; 0001-0004 are MNG's, 0005-0009 are ours), then runs `dotnet publish`. Needs the .NET 10 SDK, the MSVC build
tools and vswhere.exe (docs/toolchain-windows.md). Run with .venv.
Next to the exe it writes totk_xlate.id: the translator identity, a sha256 over everything the build reads (this script,
@@ -9,7 +9,9 @@ Program.cs, the csproj, the patches, the vendor tree). runtime/gfx_shader.c puts
translator change re-keys the cache without a hand-made KEY_VERSION bump. --id-only writes it for the exe already built
(the caller vouches that the exe matches the sources).
python tools/build_xlate.py [--id-only]
python tools/build_xlate.py [--id-only] [--out DIR]
--out builds into DIR instead of build/xlate (a task build dir: totk.exe takes the translator next to it).
"""
import argparse
@@ -42,12 +44,44 @@ def tree_hashes(base: Path) -> dict[str, str]:
}
def mng_pin() -> str:
"""The MacNativeGames commit tools/fetch_refs.sh pins."""
for line in (ROOT / "tools" / "fetch_refs.sh").read_text(encoding="utf-8").splitlines():
f = line.split()
if len(f) == 4 and f[:2] == ["fetch", "MacNativeGames"]:
return f[3]
sys.exit("tools/fetch_refs.sh pins no MacNativeGames commit")
def pinned_blobs() -> dict[str, str]:
"""Git blob ids of MNG's vendor/ryujinx at the pinned commit. The checkout itself may be newer than the pin or
carry CRLF line ends (core.autocrlf), so its files are not compared."""
out = subprocess.run(["git", "-C", str(MNG_VENDOR.parent.parent), "ls-tree", "-r", mng_pin(), "vendor/ryujinx"],
capture_output=True, text=True)
if out.returncode != 0:
sys.exit(f"git ls-tree of the MacNativeGames pin failed: {out.stderr.strip()}")
blobs = {}
for line in out.stdout.splitlines():
meta, path = line.split("\t", 1)
blobs[path.removeprefix("vendor/ryujinx/")] = meta.split()[2]
return blobs
def blob_ids(base: Path) -> dict[str, str]:
"""Git blob ids of the source files tree_hashes() hashes."""
ids = {}
for rel in tree_hashes(base):
data = (base / rel).read_bytes()
ids[rel] = hashlib.sha1(b"blob %d\0" % len(data) + data).hexdigest()
return ids
def verify_vendor() -> None:
"""tools/vendor/ryujinx must be byte-identical to MNG's copy (when refs/ is present)."""
"""tools/vendor/ryujinx must be byte-identical to MNG's copy at the pinned commit (when refs/ is present)."""
if not MNG_VENDOR.is_dir():
print("refs/MacNativeGames missing: vendor comparison skipped")
return
ours, theirs = tree_hashes(VENDOR), tree_hashes(MNG_VENDOR)
ours, theirs = blob_ids(VENDOR), pinned_blobs()
if ours != theirs:
diff = sorted(k for k in ours.keys() | theirs.keys() if ours.get(k) != theirs.get(k))
sys.exit(f"tools/vendor/ryujinx differs from MNG in {len(diff)} files, first: {diff[:5]}")
@@ -62,8 +96,8 @@ def prepare_sources() -> Path:
for junk in list(src.glob("src/*/obj")) + list(src.glob("src/*/bin")):
shutil.rmtree(junk)
patches = sorted((XLATE / "patches").glob("*.patch"))
if len(patches) != 8:
sys.exit(f"expected 8 patches, found {len(patches)}")
if len(patches) != 9:
sys.exit(f"expected 9 patches, found {len(patches)}")
for patch in patches:
# git apply works outside a repository when given a directory; --check first so a failure names the patch.
for extra in (["--check"], []):
@@ -98,7 +132,11 @@ def write_identity(exe: Path) -> None:
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument("--id-only", action="store_true", help="write totk_xlate.id for the exe already built")
parser.add_argument("--out", type=Path, help="build directory instead of build/xlate")
a = parser.parse_args()
if a.out:
global BUILD
BUILD = a.out.resolve()
if a.id_only:
write_identity(BUILD / "totk_xlate.exe")
return
+4 -1
View File
@@ -89,7 +89,10 @@ static class SwshXlate
if (_job["fout"] is JsonObject f) foreach (var kv in f) fout[int.Parse(kv.Key)] = Attr((string)kv.Value);
// Vulkan: VK_EXT_depth_clip_control handles NEAR_IS_MINUS_W, so the shader is never rewritten ("depth_minus_w" in a job is ignored).
const bool depthMinusW = false;
return new GpuGraphicsState(false, InputTopology.Triangles, false, TessPatchType.Triangles, TessSpacing.EqualSpacing,
// "early_fragment_tests": the fragment shader's NVN control byte +0x718 (the driver's early-Z-force, Ryujinx's
// EarlyZForce); the SPIR-V then declares EarlyFragmentTests (MacNativeGames ae8f4f4).
bool earlyZ = _job["early_fragment_tests"] is JsonValue ez && ez.TryGetValue<bool>(out bool eb) && eb;
return new GpuGraphicsState(earlyZ, InputTopology.Triangles, false, TessPatchType.Triangles, TessSpacing.EqualSpacing,
false, false, false, depthMinusW, false, 1f, AlphaTestOp.Always, 0f, attr, false, fout, false,
false /* 不做 Y 取反 | no Y flip */, true /* 原点左上 | origin upper-left */, false);
}
@@ -0,0 +1,21 @@
--- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs
+++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs
@@ -1283,6 +1283,18 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
if (isShadow)
{
dRef = Src(AggregateType.FP32);
+
+ // TOTK: the shadow cascades are D16 array textures. Maxwell rounds the compare reference to the texture's
+ // 16-bit precision before the compare; Vulkan drivers may compare the full float (AMD does), so a lit
+ // surface fails its own compare by a fraction of a step and the whole face reads as shadowed. Array
+ // compares only: in TotK those are the D16 cascades (MacNativeGames 6283599 rounds the same ones).
+ if (isArray)
+ {
+ SpvInstruction d16Fp32 = context.TypeFP32();
+ SpvInstruction d16Clamped = context.GlslFClamp(d16Fp32, dRef, context.Constant(d16Fp32, 0f), context.Constant(d16Fp32, 1f));
+ SpvInstruction d16Scaled = context.FAdd(d16Fp32, context.FMul(d16Fp32, d16Clamped, context.Constant(d16Fp32, 65535f)), context.Constant(d16Fp32, 0.5f));
+ dRef = context.FMul(d16Fp32, context.GlslFloor(d16Fp32, d16Scaled), context.Constant(d16Fp32, 1f / 65535f));
+ }
}
SpvInstruction[] derivatives = null;