diff --git a/dm/gfx/nvn/nvn_mtl.m b/dm/gfx/nvn/nvn_mtl.m index f73da6dac7..5e360a60a3 100644 --- a/dm/gfx/nvn/nvn_mtl.m +++ b/dm/gfx/nvn/nvn_mtl.m @@ -3,6 +3,13 @@ #import #import #import +// 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 +@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 , whose I (the imaginary unit) would shadow the local tables named I below #import #import @@ -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 @@ -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 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 m, unsigned *level) { unsigned k = tex_mip_skipped(t, m); if (*level < k) return 0; *level -= k; return 1; } static id pkg_upload_(const tex_t *t, uint64_t *stamp, id 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 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 @@ -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; } @@ -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 stg = up_alloc(total ? total : 16, &stg_off); if (upsrc) { uint64_t mo2; id 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 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)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; } } @@ -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; } } @@ -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); } @@ -5278,6 +5314,8 @@ static void treg_pkg_stamp(treg_t *rp, id 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) @@ -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; @@ -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 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 buf = [g_dev newBufferWithLength:wb * hb * nz options:MTLResourceStorageModeShared]; id cb = q_cb(); id b = enc_b_begin(cb); @@ -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; @@ -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)); @@ -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 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]; diff --git a/dm/gfx/tests/Makefile b/dm/gfx/tests/Makefile index 5288b37c2b..8a1a8798ec 100644 --- a/dm/gfx/tests/Makefile +++ b/dm/gfx/tests/Makefile @@ -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 diff --git a/dm/gfx/tests/t_texture_quality.m b/dm/gfx/tests/t_texture_quality.m new file mode 100644 index 0000000000..2470772ff7 --- /dev/null +++ b/dm/gfx/tests/t_texture_quality.m @@ -0,0 +1,59 @@ +// Reduced texture quality keeps the original mip contents and remaps guest levels. +#define nvn_game_fps nvn_game_fps_mtl_default +#include "../nvn/nvn_mtl.m" +#undef nvn_game_fps +uint64_t g_t0_ns; +void *front_metal_layer(void) { return NULL; } +module_t *module_of(const void *p) { (void)p; return NULL; } +module_t *g_mod; int g_nmod; +void svc_thread_dump(void) {} +uint64_t now_ns(void) { return bp_now(); } +static int checks, failed; +#define CHECK(c, s) do { checks++; if (!(c)) { failed++; fprintf(stderr, "FAIL: %s\n", s); } } while (0) +static uint16_t first_colour(id texture, unsigned level) { + NSUInteger w = texture.width >> level, h = texture.height >> level, row = ((w + 3) / 4) * 8; + id buffer = [g_dev newBufferWithLength:row * ((h + 3) / 4) options:MTLResourceStorageModeShared]; + id cb = [g_q commandBuffer]; id blit = [cb blitCommandEncoder]; + [blit copyFromTexture:texture sourceSlice:0 sourceLevel:level sourceOrigin:MTLOriginMake(0, 0, 0) sourceSize:MTLSizeMake(w, h, 1) + toBuffer:buffer destinationOffset:0 destinationBytesPerRow:row destinationBytesPerImage:buffer.length]; + [blit endEncoding]; [cb commit]; [cb waitUntilCompleted]; + CHECK(cb.status == MTLCommandBufferStatusCompleted, "compressed readback completed"); + return *(uint16_t *)buffer.contents; +} +int main(void) { @autoreleasepool { + mtl_init(); + tex_t t = {.serial=81001, .d={.target=NVN_TEXTURE_TARGET_2D, .flags=NVN_TEXTURE_FLAGS_IMAGE, .format=NVN_FORMAT_RGBA_DXT1, .width=512, .height=512, .depth=1, .levels=5}}; + nvn_tex_layout(&t.d, &t.L); + CHECK(t.L.size > 0, "texture storage layout is nonempty"); if (!t.L.size) return 1; + void *mem = mmap(NULL, t.L.size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); + CHECK(mem != MAP_FAILED, "guest texture storage allocated"); if (mem == MAP_FAILED) return 1; + t.mem = mem; const uint16_t colours[5] = {0xf800, 0x07e0, 0x001f, 0xffff, 0xffe0}; + for (unsigned l = 0; l < 5; l++) { + uint64_t lo = nvn_tex_level_offset(&t.d, l), hi = l + 1 < 5 ? nvn_tex_level_offset(&t.d, l + 1) : t.L.size; + for (uint64_t off = lo; off + 8 <= hi; off += 8) { uint16_t *block = (uint16_t *)((uint8_t *)mem + off); block[0] = block[1] = colours[l]; } + } + mtl_pool_add(mem, t.L.size); uint64_t stamp = 0; + mtl_sub_begin(); id image = pkg_upload_(&t, &stamp, nil, 0); + CHECK(image != nil, "compressed CPU texture uploaded"); + CHECK(mtl_wait(mtl_sub_commit(), 5000000000ull) && !mtl_last_commit_error(), "upload submission completed"); + unsigned skipped = mip_skip_px() ? mip_skip_levels() : 0; + CHECK(image.width == (512u >> skipped) && image.height == (512u >> skipped), "requested dimensions"); + CHECK(image.mipmapLevelCount == 5 - skipped, "only the requested finest levels were removed"); + CHECK(first_colour(image, 0) == colours[skipped], "host mip zero contains the correct original mip"); + CHECK(first_colour(image, 1) == colours[1 + skipped], "the following mip retains its original content"); + unsigned level = skipped + 1; CHECK(lv_host(&t, image, &level) && level == 1, "guest mip maps to its host level"); + level = 0; CHECK(lv_host(&t, image, &level) == !skipped, "copies of a removed mip are refused"); + MTLTextureDescriptor *d = tex_desc(&t, 1); d.usage |= MTLTextureUsageShaderWrite; + CHECK(!mip_skip(&t, d), "shader-written textures keep their storage"); + tex_t rgba = t; rgba.d.format = NVN_FORMAT_RGBA8; + CHECK(!mip_skip(&rgba, tex_desc(&rgba, 1)), "uncompressed render formats are unchanged"); + CHECK(!tex_mip_skipped(&rgba, image), "an unrelated size difference does not remap levels"); + tex_t few = t; few.d.levels = 2; + CHECK(mip_skip(&few, tex_desc(&few, 1)) == MIN(skipped, 1u), "at least one mip is always retained"); + tex_t small = t; small.d.width = small.d.height = 256; + CHECK(!mip_skip(&small, tex_desc(&small, 1)), "textures below the pixel threshold are unchanged"); + tex_t volume = t; volume.d.target = NVN_TEXTURE_TARGET_3D; + CHECK(!mip_skip(&volume, tex_desc(&volume, 1)), "volume textures are unchanged"); + mtl_finish(); mtl_pool_del(mem); mtl_finish(); + printf("texture quality: %d checks, %d failed\n", checks, failed); return failed != 0; +} } diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index 22c799e454..82c805eac1 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -700,6 +700,7 @@ One line per symptom in the [subsystem references](../reference/): what you see - read-only depth/stencil attachments are written back after every pass, and the periodic unwritten-store savings counter remains zero with the default configuration. → [Unwritten attachment stores without lookahead](../reference/rendering.md#unwritten-attachment-stores-without-lookahead) - portrait gameplay overlaps the touch controls, touch coordinates land outside the game picture, or a TV's picture renders at 4K again after a layout pass. → [iOS layouts: portrait, a half-folded Duo, iPad windows](../reference/rendering.md#ios-layouts-portrait-a-half-folded-duo-ipad-windows) - with *Picture shape* at 1.43:1 (`DM_SCREEN_ASPECT`) the game renders 1920x1344, but on an iPhone Duo in Fit (the default) the picture comes out smaller than the 16:9 one, with black bars on all four sides; it fills the screen only in Fill. On the Mac a click lands off the point under the pointer (it was mapped on a 16:9 rectangle of the window). → [The picture laid out in the shape the game presents](../reference/rendering.md#the-picture-laid-out-in-the-shape-the-game-presents) +- CPU-written compressed texture chains consume more GPU storage than a constrained device can sustain. → [Optional compressed texture mip reduction](../reference/rendering.md#optional-compressed-texture-mip-reduction) ## Shaders diff --git a/docs/env_switches.tsv b/docs/env_switches.tsv index 598f89cc20..adf91d193a 100644 --- a/docs/env_switches.tsv +++ b/docs/env_switches.tsv @@ -193,6 +193,8 @@ DM_NVN_TEXDUMP diagnostic `=:`, `size=x:` or `layers=x` logs storage and description of every texture of that size on first use, registration and initialization (pool, flags, packaged, non-zero bytes, swizzles) DM_NVN_TEXRAW diagnostic `=,…` or `info` (the textures `DM_NVN_TEXINFO` picked out): saves those textures' raw bytes at the `DM_NVN_TEXDUMP` frame, with a text header line DM_NVN_TEXSNAP diagnostic `=,…`: with `DM_NVN_TEXDUMP`, copies those passes' colour attachments as they end and saves them with the end-of-frame dump +DM_NVN_TEX_MIP_LEVELS player With `DM_NVN_TEX_MIP_SKIP`, number of finest levels to omit, from `1` (default) to `3`; at least one mip is retained. Lower texture detail reduces GPU texture storage +DM_NVN_TEX_MIP_SKIP player ``: omit finest mip levels from CPU-written compressed textures whose width and height meet the threshold; `1` means `512`, unset or `0` keeps all levels. Shader-written textures and render targets are unchanged DM_NVN_TREG_ID diagnostic `=` logs every registration of texture ID n: what it holds from then on, and the frame being recorded DM_NVN_TSV diagnostic `=` writes the per-frame NVN call table and timing rows (frame, metric, value) to a TSV, read by `scripts/bench.py` DM_NVN_TSV_CALLS diagnostic `0` makes `DM_NVN_TSV` write only the timing rows, without the call counts (about two thirds of the file) diff --git a/docs/reference/rendering.md b/docs/reference/rendering.md index 93a9bbf5c3..dfb5bb7d96 100644 --- a/docs/reference/rendering.md +++ b/docs/reference/rendering.md @@ -1457,3 +1457,11 @@ Digimon Story Cyber Sleuth: Complete Edition (Media.Vision's MVGL engine, SDK 6. - **Fix:** the present reports the shape of the frame it draws, the valid rectangle of the presented texture, whenever its width / height changes (`nvn_mtl.m` `present_now` → `front.h` `front_picture_shape`; every graphics API presents there). The iOS front end lays the game's view out in that shape (`DmLayout.h` takes it as a parameter: the overlay, the split, Fill and the TV alike), so a 1.43:1 picture fills a 1.43:1 screen in Fit and touches are normalized on the picture as drawn; the Mac maps the mouse on the picture's own rectangle. A 16:9 game reports 16:9 and keeps its layout, and a size change of the same shape (1280x720 handheld, 1920x1080 docked) is not reported. - **Check:** the [Duo harness](../../dm/ios/tests/duo/README.md) tests the taller picture in Fit, in Fill and on a wider screen, and the production root view following a reported shape and returning to 16:9. Not yet run on a device. - **Found on:** [Breath of the Wild](../../dm/games/01007EF00011E000/README.md#screen-shape-dm_screen_aspect-current) and [Animal Crossing: New Horizons](../../dm/games/01006F8002326000/README.md#screen-shape-dm_screen_aspect-current) with `DM_SCREEN_ASPECT=1.43`. + +### Optional compressed texture mip reduction + +- **Symptom:** CPU-written compressed texture chains consume more GPU storage than a constrained device can sustain. +- **Cause:** retaining every finest mip keeps the entire original texture chain even when reduced texture detail is acceptable. +- **Fix:** the texture-quality path from #1319 omits finest levels only for eligible CPU-written compressed 2D/array/cube textures. `DM_NVN_TEX_MIP_SKIP=` enables it at a width-and-height threshold (`1` means `512`); `DM_NVN_TEX_MIP_LEVELS=1..3` selects the reduction, with at least one mip retained. Validate both dimensions and mip count when detecting a reduced texture; remap guest views, sparse clamps, copies and readbacks to host levels, and refuse copies of removed levels. Compressed `IMAGE` resources retain read-only usage, while shader-written textures and uncompressed render formats stay unchanged. The option is off by default, reduces close-up texture detail and changes texture-size queries; it is not internal render-resolution scaling. +- **Found on:** Ocarina of Time memory investigation (#1299), extending #1319's one-level option. This branch has synthetic content checks, not an isolated gameplay-memory, FPS or thermal measurement. +- **Check:** `check_t_texture_quality` uploads a five-level BC1 chain with distinct contents per mip, then reads the GPU texture with reduction off and at levels 1, 2 and 3. It verifies dimensions, retained content, guest-to-host level mapping, removed-level copy refusal and ineligible formats/targets.