Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
77 changes: 58 additions & 19 deletions dm/gfx/nvn/nvn_mtl.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
#import <QuartzCore/CAMetalLayer.h>
#import <QuartzCore/CAMetalDisplayLink.h>
#import <MetalFX/MetalFX.h>
// Xcode 26's headers do not declare these public macOS/iOS 27 setters. Calls remain under the OS 27 availability check below; this declaration only lets an older SDK build the host.
@protocol DmTemporalScalerOffsets <NSObject>
@property (nonatomic) NSUInteger colorContentOffsetX, colorContentOffsetY;
@property (nonatomic) NSUInteger depthContentOffsetX, depthContentOffsetY;
@property (nonatomic) NSUInteger motionContentOffsetX, motionContentOffsetY;
@property (nonatomic) NSUInteger outputOffsetX, outputOffsetY;
@end
#undef I // MetalFX brings in <complex.h>, whose I (the imaginary unit) would shadow the local tables named I below
#import <ImageIO/ImageIO.h>
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
Expand Down Expand Up @@ -2090,7 +2097,7 @@ static void pkg_absorb_take(NvPkg *pk, const tex_t *t) {
// not say which images a shader will use atomically
static inline int pf_r32i(MTLPixelFormat pf) { return pf == MTLPixelFormatR32Uint || pf == MTLPixelFormatR32Sint; }
static MTLTextureUsage img_usage(const tex_t *t, MTLPixelFormat pf) {
if (!(t->d.flags & NVN_TEXTURE_FLAGS_IMAGE)) return 0;
if (!(t->d.flags & NVN_TEXTURE_FLAGS_IMAGE) || fmt_compressed(t->d.format)) return 0; // compressed images are read-only; block encoders write uncompressed aliases
return MTLTextureUsageShaderWrite | (pf_r32i(pf) ? MTLTextureUsageShaderAtomic : 0);
}
// Metal leaves MTLTextureUsageShaderAtomic out of a texture's usage property (0x3 for a texture made with 0x23), so it cannot be read back: every shader-writable R32Uint / R32Sint
Expand Down Expand Up @@ -2515,12 +2522,39 @@ static void rb_swap_rows(uint8_t *p, size_t wb, size_t rows, size_t nz, size_t r
res_add(buf); [g_up_keep addObject:buf]; *off = (uint64_t)addr - a;
return buf;
}
// Optional compressed-texture quality reduction (#1319): omit 1..3 finest
// levels of CPU-written 2D / array / cube mip chains meeting the pixel threshold.
// Keep at least one level. Uploads retain the original coarser mip contents;
// views, sparse clamps and copies translate guest levels to the remaining host
// levels. Render targets and shader-written textures keep their storage. This
// trades close-up texture detail for less texture storage; it is off by default.
static unsigned mip_skip_px(void) { static int v = -1; if (v < 0) { const char *e = getenv("DM_NVN_TEX_MIP_SKIP"); v = e ? atoi(e) : 0; if (v == 1) v = 512; if (v < 0) v = 0; if (v) MLOG("DM_NVN_TEX_MIP_SKIP: compressed CPU-written textures of %d pixels and more are made without their configured finest mip levels", v); } return (unsigned)v; }
static unsigned mip_skip_levels(void) { static unsigned n; if (!n) { const char *e = getenv("DM_NVN_TEX_MIP_LEVELS"); int v = e ? atoi(e) : 1; n = (unsigned)MAX(1, MIN(v, 3)); } return n; }
static unsigned mip_skip(const tex_t *t, MTLTextureDescriptor *d) {
unsigned px = mip_skip_px();
if (!px || !fmt_compressed(t->d.format) || (d.usage & MTLTextureUsageShaderWrite) || d.mipmapLevelCount < 2) return 0;
if (t->d.target != 1 && t->d.target != 4 && t->d.target != 8 && t->d.target != 9) return 0;
return MIN(d.width, d.height) >= px ? MIN(mip_skip_levels(), (unsigned)d.mipmapLevelCount - 1) : 0;
}
// How many finest levels a host texture of t lacks (DM_NVN_TEX_MIP_SKIP): the levels by which its width falls short of the guest's (3D textures are never skipped)
static unsigned tex_mip_skipped(const tex_t *t, id<MTLTexture> m) {
unsigned px = mip_skip_px();
if (!px || !t || !m || !fmt_compressed(t->d.format) || t->d.levels < 2 || MIN(t->d.width, t->d.height) < px) return 0;
if (t->d.target != 1 && t->d.target != 4 && t->d.target != 8 && t->d.target != 9) return 0;
for (unsigned k = 1; k <= MIN(mip_skip_levels(), t->d.levels - 1); k++)
if (m.width == MAX(t->d.width >> k, 1u) && m.height == MAX(t->d.height >> k, 1u) && m.mipmapLevelCount == t->d.levels - k) return k;
return 0;
}
// A copy's guest level as the host texture's (tex_mip_skipped): 0 when the host lacks that level
static int lv_host(const tex_t *t, id<MTLTexture> m, unsigned *level) { unsigned k = tex_mip_skipped(t, m); if (*level < k) return 0; *level -= k; return 1; }
static id<MTLTexture> pkg_upload_(const tex_t *t, uint64_t *stamp, id<MTLTexture> reuse, MTLTextureUsage keep) {
MTLTextureDescriptor *d = tex_desc(t, 1);
if (!d) return nil;
unsigned sk = stamp ? mip_skip(t, d) : 0; // levels the host texture leaves out (mip_skip): the guest's level l + sk is its level l
if (sk) { d.width = MAX(d.width >> sk, 1u); d.height = MAX(d.height >> sk, 1u); d.mipmapLevelCount -= sk; static uint64_t n; n++; if (n <= 3 || !(n % 2000)) MLOG("DM_NVN_TEX_MIP_SKIP: %llu textures made without %u finest levels (ser=%llu %ux%u format %#x)", (unsigned long long)n, sk, (unsigned long long)t->serial, t->d.width, t->d.height, t->d.format); }
d.usage |= keep & (MTLTextureUsageRenderTarget | MTLTextureUsagePixelFormatView);
int rb_shift = rb_packed_shift(t->d.format); // the RGB565 family is repacked into Metal's order on the CPU (rb_packed_shift)
int direct = !rb_shift && stamp && direct_upload_on() && g_nvn_deswz_gpu && !(t->pool && nvn_sparse_pool(t->pool)) && nvn_deswz_gpu_direct_supported(&t->d);
int direct = !sk && !rb_shift && stamp && direct_upload_on() && g_nvn_deswz_gpu && !(t->pool && nvn_sparse_pool(t->pool)) && nvn_deswz_gpu_direct_supported(&t->d);
if (direct) d.usage |= MTLTextureUsageShaderWrite | MTLTextureUsagePixelFormatView;
id<MTLTexture> mt = reuse && reuse.pixelFormat == d.pixelFormat && reuse.width == d.width && reuse.height == d.height && reuse.mipmapLevelCount == d.mipmapLevelCount && reuse.textureType == d.textureType
&& (reuse.usage & d.usage) == d.usage
Expand All @@ -2538,7 +2572,7 @@ static void rb_swap_rows(uint8_t *p, size_t wb, size_t rows, size_t nz, size_t r
int bw, bh, bpb = nvn_fmt_bpb(t->d.format, &bw, &bh);
unsigned layers = (t->d.target == 4 || t->d.target == 9) ? t->d.depth : t->d.target == 8 ? 6 : t->d.target == 3 ? t->d.height : 1; // a cube array's depth counts layer-faces (tex_desc)
size_t total = 0;
for (unsigned l = 0; l < d.mipmapLevelCount; l++) {
for (unsigned l = sk; l < d.mipmapLevelCount + sk; l++) {
uint32_t w = MAX(t->d.width >> l, 1u), h = MAX(t->d.height >> l, 1u), z = t->d.target == 2 ? MAX(t->d.depth >> l, 1u) : 1;
total += (size_t)((w + bw - 1) / bw) * bpb * ((h + bh - 1) / bh) * z * layers;
}
Expand Down Expand Up @@ -2572,13 +2606,13 @@ static void rb_swap_rows(uint8_t *p, size_t wb, size_t rows, size_t nz, size_t r
id<MTLBuffer> stg = up_alloc(total ? total : 16, &stg_off);
if (upsrc) { uint64_t mo2; id<MTLBuffer> u2 = up_big_src((uint64_t)t->mem, t->L.size, &mo2); if (u2) { upsrc = u2; mb = (__bridge void *)u2; mo = mo2; } } // up_alloc may have flushed the batch that held the first one
size_t off = 0; int enc_ok = g_up_ce != nil, nb0 = g_nup_blits;
for (unsigned l = 0; enc_ok && l < d.mipmapLevelCount; l++) {
for (unsigned l = sk; enc_ok && l < d.mipmapLevelCount + sk; l++) {
uint32_t w = MAX(t->d.width >> l, 1u), h = MAX(t->d.height >> l, 1u), z = t->d.target == 2 ? MAX(t->d.depth >> l, 1u) : 1;
size_t pitch = (size_t)((w + bw - 1) / bw) * bpb, rows = (h + bh - 1) / bh;
for (unsigned y = 0; y < layers; y++) {
size_t poff; id<MTLBuffer> params = up_param_alloc(&poff); // Metal 4 has no setBytes: the parameter block of each deswizzle is cut from a shared block (no new GPU buffer each time)
if (nvn_deswz_gpu_encode_m4(g_up_ce, g_up_ct, params, poff, (__bridge id<MTLBuffer>)mb, mo, &t->d, l, y, stg, stg_off + off, pitch)) { enc_ok = 0; break; }
up_blit_add(stg, stg_off + off, pitch, pitch * rows, MTLSizeMake(w, h, z), mt, y, l);
up_blit_add(stg, stg_off + off, pitch, pitch * rows, MTLSizeMake(w, h, z), mt, y, l - sk);
off += pitch * rows * z;
}
}
Expand All @@ -2603,16 +2637,16 @@ static void rb_swap_rows(uint8_t *p, size_t wb, size_t rows, size_t nz, size_t r
res_add(stg); [g_up_keep addObject:stg];
size_t off = 0;
int zb = zblock_on() && t->d.format >= 0x79 && t->d.format <= 0x94; if (zb) { g_zb_ser = t->serial; g_zb_n = (int)MIN(d.mipmapLevelCount, 16u); memset(g_zb, 0, sizeof g_zb); memset(g_zb_tot, 0, sizeof g_zb_tot); }
for (unsigned l = 0; l < d.mipmapLevelCount; l++) {
for (unsigned l = sk; l < d.mipmapLevelCount + sk; l++) {
uint32_t w = MAX(t->d.width >> l, 1u), h = MAX(t->d.height >> l, 1u), z = t->d.target == 2 ? MAX(t->d.depth >> l, 1u) : 1;
size_t pitch = (size_t)((w + bw - 1) / bw) * bpb, rows = (h + bh - 1) / bh;
for (unsigned y = 0; y < layers; y++) {
if (nvn_deswizzle(&t->d, l, y, src, (uint8_t *)stg.contents + off, pitch)) { RED_ONCE(9, "deswizzle failed format %#x target %d", t->d.format, t->d.target); break; }
if (rb_shift) rb_swap_rows((uint8_t *)stg.contents + off, pitch, rows, z, pitch, pitch * rows, rb_shift);
if (zb && l < 16) { const uint64_t *q = (const uint64_t *)((const uint8_t *)stg.contents + off); size_t nb = pitch * rows * z / 16;
for (size_t i = 0; i < nb; i++) g_zb[l] += !(q[2 * i] | q[2 * i + 1]);
g_zb_tot[l] += (uint32_t)nb; }
up_blit_add(stg, off, pitch, pitch * rows, MTLSizeMake(w, h, z), mt, y, l);
if (zb && l - sk < 16) { const uint64_t *q = (const uint64_t *)((const uint8_t *)stg.contents + off); size_t nb = pitch * rows * z / 16;
for (size_t i = 0; i < nb; i++) g_zb[l - sk] += !(q[2 * i] | q[2 * i + 1]);
g_zb_tot[l - sk] += (uint32_t)nb; }
up_blit_add(stg, off, pitch, pitch * rows, MTLSizeMake(w, h, z), mt, y, l - sk);
off += pitch * rows * z;
}
}
Expand Down Expand Up @@ -3185,13 +3219,15 @@ static void cpu_unc_drop(void) {
// The translator drops TEX's LOD clamp and residency predicate, so Monster Hunter Rise's streamed textures were sampled at levels it had not mapped yet; those read 0, which ASTC
// decodes as its error colour (magenta clothes on the title screen). Single-layer 2D textures only; the view follows each upload, as the mappings change only with one
static void sparse_view(NvPkg *pk, const tex_t *t) {
unsigned n = (unsigned)pk->tex.mipmapLevelCount, r = n;
// r walks the guest's levels; the host texture lacks the k finest (DM_NVN_TEX_MIP_SKIP), so its view starts at r - k
unsigned k = tex_mip_skipped(t, pk->tex), n = (unsigned)pk->tex.mipmapLevelCount + k, r = n;
if (t->d.target == NVN_TEXTURE_TARGET_2D && n > 1)
while (r > 0) { uint64_t lo = nvn_tex_level_offset(&t->d, r - 1), hi = r < n ? nvn_tex_level_offset(&t->d, r) : t->L.size;
if (!nvn_sparse_mapped(t->pool, (uint64_t)(uintptr_t)t->mem + lo, hi - lo)) break;
r--; }
else r = 0;
if (r == n) r = n - 1; // nothing mapped: the smallest level (reads 0)
r = r > k ? r - k : 0; n -= k;
if (pk->view) defer_obj_unref(pk->view); // a view shares its texture's allocation: drop only the reference, the residency set keeps the texture
pk->view = r ? [pk->tex newTextureViewWithPixelFormat:pk->tex.pixelFormat textureType:pk->tex.textureType levels:NSMakeRange(r, n - r) slices:NSMakeRange(0, 1)] : nil;
if (r != (unsigned)pk->vbase) { static int nl; if (nl++ < 6) MLOG("sparse texture ser=%llu %ux%u: sampled from level %u of %u (finer levels not mapped)", (unsigned long long)t->serial, t->d.width, t->d.height, r, n); }
Expand Down Expand Up @@ -5278,6 +5314,8 @@ static void treg_pkg_stamp(treg_t *rp, id<MTLTexture> base) {
sg = treg_ep();
}
NSUInteger l0 = r.v.set_levels ? r.v.base_level : 0, nl = r.v.set_levels && r.v.nlevels ? r.v.nlevels : base.mipmapLevelCount - l0;
{ unsigned k = r.v.set_levels ? tex_mip_skipped(&rp->t, base) : 0; // the guest's levels as the host texture's (DM_NVN_TEX_MIP_SKIP); a view of the dropped level reads the finest one kept
if (k) { NSUInteger drop = l0 < k ? k - l0 : 0; l0 = l0 > k ? l0 - k : 0; if (r.v.nlevels) nl = nl > drop ? nl - drop : 1; else nl = base.mipmapLevelCount - l0; } }
if (l0 >= base.mipmapLevelCount) { RED_ONCE(109, "texture view id %d starts at mip %lu beyond %lu levels", tid, (unsigned long)l0, (unsigned long)base.mipmapLevelCount); return nil; }
// A level count past the texture's levels is cut to those it has, as deko3d's Tegra descriptor writer clips the maximum level (Echoes of Wisdom: levels 1 + 7 of a seven-level texture,
// which Metal's validation refuses)
Expand Down Expand Up @@ -11415,7 +11453,7 @@ static void copy_t2b(const p_copy_b2t *c) {
if (!t) return;
int bw, bh, bpb = nvn_fmt_bpb(c->tex->d.format, &bw, &bh);
unsigned level = c->has_view && c->view.set_levels ? c->view.base_level : 0, layer0 = c->has_view && c->view.set_layers ? c->view.min_layer : 0;
if (!tex_level_ok(t, level, "texture-to-buffer copy")) return;
if (!lv_host(c->tex, t, &level) || !tex_level_ok(t, level, "texture-to-buffer copy")) return;
size_t row = S.copy_stride ? (size_t)S.copy_stride : (size_t)((c->r.width + bw - 1) / bw) * bpb;
size_t rows = (c->r.height + bh - 1) / bh, img = S.copy_img_stride ? (size_t)S.copy_img_stride : row * rows; // per layer: SetCopyImageStride, else tightly packed
int is3d = c->tex->d.target == 2, nz = c->r.depth ? c->r.depth : 1;
Expand Down Expand Up @@ -11470,7 +11508,7 @@ void mtl_read_texels(const tex_t *t, unsigned level, unsigned layer0, const NVNc
// Finding: docs/reference/memory.md, GPU readback without an autorelease pool.
@autoreleasepool {
id<MTLTexture> mt = tex_mtl(t);
if (!mt || !tex_level_ok(mt, level, "CPU readback")) { memset(dst, 0, img * nz); return; }
if (!mt || !lv_host(t, mt, &level) || !tex_level_ok(mt, level, "CPU readback")) { memset(dst, 0, img * nz); return; }
mtl_finish_gpu_at(WS_READTEX);
id<MTLBuffer> buf = [g_dev newBufferWithLength:wb * hb * nz options:MTLResourceStorageModeShared];
id<MTLCommandBuffer> cb = q_cb(); id<MTLBlitCommandEncoder> b = enc_b_begin(cb);
Expand Down Expand Up @@ -11582,7 +11620,7 @@ static void copy_b2t(const p_copy_b2t *c) {
if (!t) { PD("C ser=%llu (no host texture)", c->tex->serial); return; }
int bw, bh, bpb = nvn_fmt_bpb(c->tex->d.format, &bw, &bh);
unsigned level = c->has_view && c->view.set_levels ? c->view.base_level : 0, layer0 = c->has_view && c->view.set_layers ? c->view.min_layer : 0;
if (!tex_level_ok(t, level, "buffer-to-texture copy")) return;
if (!lv_host(c->tex, t, &level) || !tex_level_ok(t, level, "buffer-to-texture copy")) return;
size_t row = S.copy_stride ? (size_t)S.copy_stride : (size_t)((c->r.width + bw - 1) / bw) * bpb;
size_t rows = (c->r.height + bh - 1) / bh, img = S.copy_img_stride ? (size_t)S.copy_img_stride : row * rows; // per layer: SetCopyImageStride, else tightly packed
int is3d = c->tex->d.target == 2, nz = c->r.depth ? c->r.depth : 1;
Expand Down Expand Up @@ -11774,7 +11812,7 @@ static void copy_t2t(const p_copy_t2t *c) {
if (!s || !d) return;
unsigned sl = c->has_sv && c->sv.set_levels ? c->sv.base_level : 0, dl = c->has_dv && c->dv.set_levels ? c->dv.base_level : 0;
unsigned sz0 = c->has_sv && c->sv.set_layers ? c->sv.min_layer : 0, dz0 = c->has_dv && c->dv.set_layers ? c->dv.min_layer : 0;
if (!tex_level_ok(s, sl, "texture copy source") || !tex_level_ok(d, dl, "texture copy destination")) return;
if (!lv_host(c->src, s, &sl) || !lv_host(c->dst, d, &dl) || !tex_level_ok(s, sl, "texture copy source") || !tex_level_ok(d, dl, "texture copy destination")) return;
if (c->flags & DM_COPY_RESOLVE) { // Downsample: multisample resolve into the single-sample destination (depth takes sample 0); with equal sample counts it is a plain whole copy
if (s.sampleCount == d.sampleCount && s.pixelFormat == d.pixelFormat && s.width == d.width && s.height == d.height) {
enc_b_copy_texture(blit(), s, sz0, sl, MTLOriginMake(0, 0, 0), MTLSizeMake(s.width, s.height, 1), d, dz0, dl, MTLOriginMake(0, 0, 0));
Expand Down Expand Up @@ -11940,10 +11978,11 @@ static void dlss_eval(const p_dlss *c) {
sc.colorTexture = col; sc.depthTexture = dep; sc.motionTexture = mot; sc.outputTexture = out; sc.exposureTexture = expo;
sc.inputContentWidth = iw; sc.inputContentHeight = ih;
if (@available(macOS 27.0, iOS 27.0, *)) { // the rectangles' corners: all at the origin on the game seen, so an older system loses nothing there
sc.colorContentOffsetX = (NSUInteger)a.base[DLSS_COLOR][0]; sc.colorContentOffsetY = (NSUInteger)a.base[DLSS_COLOR][1];
sc.depthContentOffsetX = (NSUInteger)a.base[DLSS_DEPTH][0]; sc.depthContentOffsetY = (NSUInteger)a.base[DLSS_DEPTH][1];
sc.motionContentOffsetX = (NSUInteger)a.base[DLSS_MOTION][0]; sc.motionContentOffsetY = (NSUInteger)a.base[DLSS_MOTION][1];
sc.outputOffsetX = (NSUInteger)a.base[DLSS_OUTPUT][0]; sc.outputOffsetY = (NSUInteger)a.base[DLSS_OUTPUT][1];
id<DmTemporalScalerOffsets> offsets = (id)sc;
offsets.colorContentOffsetX = (NSUInteger)a.base[DLSS_COLOR][0]; offsets.colorContentOffsetY = (NSUInteger)a.base[DLSS_COLOR][1];
offsets.depthContentOffsetX = (NSUInteger)a.base[DLSS_DEPTH][0]; offsets.depthContentOffsetY = (NSUInteger)a.base[DLSS_DEPTH][1];
offsets.motionContentOffsetX = (NSUInteger)a.base[DLSS_MOTION][0]; offsets.motionContentOffsetY = (NSUInteger)a.base[DLSS_MOTION][1];
offsets.outputOffsetX = (NSUInteger)a.base[DLSS_OUTPUT][0]; offsets.outputOffsetY = (NSUInteger)a.base[DLSS_OUTPUT][1];
}
sc.motionVectorScaleX = a.mv[0]; sc.motionVectorScaleY = a.mv[1];
sc.jitterOffsetX = a.jitter[0]; sc.jitterOffsetY = a.jitter[1];
Expand Down
6 changes: 6 additions & 0 deletions dm/gfx/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,9 @@ t_logic: t_logic.m $(MTL_T_DEP)
clang $(CFLAGS) -fobjc-arc -o $@ t_logic.m $(MTL_T_SRC) $(FW)
check_t_logic: t_logic
@set -e; work=$$(mktemp -d); trap 'rm -rf "$$work"' EXIT; python3 test_dmsh_logic.py --fixture "$$work"; MTL_DEBUG_LAYER=1 ./t_logic "$$work"

t_texture_quality: t_texture_quality.m $(MTL_T_DEP)
clang $(CFLAGS) -fobjc-arc -o $@ t_texture_quality.m $(MTL_T_SRC) $(FW)
check_t_texture_quality: t_texture_quality
DM_NVN_TEX_MIP_SKIP=0 MTL_DEBUG_LAYER=1 ./t_texture_quality
@for levels in 1 2 3; do DM_NVN_TEX_MIP_SKIP=512 DM_NVN_TEX_MIP_LEVELS=$$levels MTL_DEBUG_LAYER=1 ./t_texture_quality || exit $$?; done
Loading
Loading