Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

BC on the CPU, part 2: the upload path expands the blocks

LNPANDY840b58baca9a
diff --git a/dm/gfx/nvn/nvn_mtl.m b/dm/gfx/nvn/nvn_mtl.m
index 270c8ace..879ae689 100644
--- a/dm/gfx/nvn/nvn_mtl.m
+++ b/dm/gfx/nvn/nvn_mtl.m
@@ -2084,15 +2084,43 @@ static id<MTLTexture> pkg_upload_(const tex_t *t, uint64_t *stamp, id<MTLTexture
if (!(part = malloc(t->L.size ? t->L.size : 1))) MDIE("sparse texture copy of %llu bytes failed", (unsigned long long)t->L.size);
nvn_sparse_copy(t->pool, part, (uint64_t)(uintptr_t)t->mem, t->L.size); src = part;
}
+ // A BC texture on a GPU without BC support: tex_desc made the Metal texture RGBA8 (or R8/RG8,
+ // RGBA16F for BC6H), so the staging slot is per-pixel, not per-block, and the deswizzled block
+ // data is expanded through nvn_bc_decode before the blit. bc_cpu stays 0 on Macs and A19+,
+ // where the compressed path below is used unchanged.
+ uint32_t bc_pf = 0; int bc_bpp = 0;
+ int bc_cpu = g_bc_cpu && nvn_bc_unpacked(t->d.format, &bc_pf, &bc_bpp);
+ size_t total_bc = 0;
+ if (bc_cpu) {
+ total_bc = 0;
+ for (unsigned l = 0; l < d.mipmapLevelCount; 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_bc += (size_t)w * h * z * layers * (size_t)bc_bpp;
+ }
+ }
up_begin();
- id<MTLBuffer> stg = [g_dev newBufferWithLength:total ? total : 16 options:MTLResourceStorageModeShared];
+ id<MTLBuffer> stg = [g_dev newBufferWithLength:(bc_cpu ? total_bc : total) ? (bc_cpu ? total_bc : total) : 16 options:MTLResourceStorageModeShared];
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); }
+ // CPU-side scratch for the compressed block data when bc_cpu (one level-slice at a time)
+ uint8_t *bc_tmp = NULL; size_t bc_tmp_sz = 0;
for (unsigned l = 0; l < d.mipmapLevelCount; 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 (bc_cpu) {
+ size_t need = pitch * rows * z;
+ if (need > bc_tmp_sz) { free(bc_tmp); bc_tmp = malloc(need); bc_tmp_sz = need; if (!bc_tmp) MDIE("BC decode scratch of %zu bytes failed", need); }
+ if (nvn_deswizzle(&t->d, l, y, src, bc_tmp, pitch)) { RED_ONCE(9, "deswizzle failed format %#x target %d", t->d.format, t->d.target); break; }
+ size_t dst_pitch = (size_t)w * bc_bpp;
+ for (uint32_t zz = 0; zz < z; zz++)
+ nvn_bc_decode(t->d.format, bc_tmp + (size_t)zz * pitch * rows, pitch, w, h,
+ (uint8_t *)stg.contents + off + (size_t)zz * dst_pitch * h, dst_pitch);
+ up_blit_add(stg, off, dst_pitch, dst_pitch * h, MTLSizeMake(w, h, z), mt, y, l);
+ off += dst_pitch * h * z;
+ continue;
+ }
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 (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]);
@@ -2101,6 +2129,7 @@ static id<MTLTexture> pkg_upload_(const tex_t *t, uint64_t *stamp, id<MTLTexture
off += pitch * rows * z;
}
}
+ free(bc_tmp);
free(part);
up_flush();
return mt;
diff --git a/scripts/sync_upstream.py b/scripts/sync_upstream.py
index d50b8031..aaa6b796 100644
--- a/scripts/sync_upstream.py
+++ b/scripts/sync_upstream.py
@@ -521,13 +521,17 @@ static int g_bc_cpu; // decode BC on the CPU: no GPU support and the profi
g_bc_gpu = [g_dev supportsBCTextureCompression];
g_bc_cpu = !g_bc_gpu && DM_GAME_TEX_BC_CPU;
if (g_bc_cpu) MLOG("BC textures: this GPU has no BC support, decoding them on the CPU (profile tex_bc_cpu)");''')]),
- ('bc cpu unpack', ['nvn_bc_unpacked'], [(
- ''' uint32_t pf = 0; uint8_t swz[4];
- if (!nvn_fmt_to_mtl(t->d.format, &pf, swz) || !pf) {''',
- ''' uint32_t pf = 0; uint8_t swz[4];
- uint32_t bc_unpacked = 0; int bc_bpp = 0;
- if (g_bc_cpu && nvn_bc_unpacked(t->d.format, &bc_unpacked, &bc_bpp)) pf = bc_unpacked;
- if (!pf && (!nvn_fmt_to_mtl(t->d.format, &pf, swz) || !pf)) {''')]),
+ # BC decode on the CPU when the GPU has no BC support (profile tex_bc_cpu): the texture is
+ # created uncompressed, and the upload expands the blocks before the blit.
+ ('bc cpu tex_desc', ['nvn_bc_unpacked(t->d.format, &bc_unpacked'], [(
+ ' uint32_t pf = 0; uint8_t swz[4];\n if (!nvn_fmt_to_mtl(t->d.format, &pf, swz) || !pf) {',
+ ' uint32_t pf = 0; uint8_t swz[4];\n // A BC format on a GPU without BC support: create the texture in the uncompressed format the\n // decoder produces, and let the upload path fill it through nvn_bc_decode (below). Without\n // this the Metal format is a BC one the device cannot allocate, and every such texture is nil.\n uint32_t bc_unpacked = 0; int bc_bpp = 0;\n if (g_bc_cpu && nvn_bc_unpacked(t->d.format, &bc_unpacked, &bc_bpp)) pf = bc_unpacked;\n if (!pf && (!nvn_fmt_to_mtl(t->d.format, &pf, swz) || !pf)) {')]),
+ ('bc cpu upload', ['nvn_bc_decode(t->d.format'], [(
+ ' up_begin();\n id<MTLBuffer> stg = [g_dev newBufferWithLength:total ? total : 16 options:MTLResourceStorageModeShared];\n res_add(stg); [g_up_keep addObject:stg];\n size_t off = 0;\n 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); }\n for (unsigned l = 0; l < d.mipmapLevelCount; l++) {\n 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;\n size_t pitch = (size_t)((w + bw - 1) / bw) * bpb, rows = (h + bh - 1) / bh;\n for (unsigned y = 0; y < layers; y++) {\n 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; }\n if (zb && l < 16) { const uint64_t *q = (const uint64_t *)((const uint8_t *)stg.contents + off); size_t nb = pitch * rows * z / 16;\n for (size_t i = 0; i < nb; i++) g_zb[l] += !(q[2 * i] | q[2 * i + 1]);\n g_zb_tot[l] += (uint32_t)nb; }\n up_blit_add(stg, off, pitch, pitch * rows, MTLSizeMake(w, h, z), mt, y, l);\n',
+ ' // A BC texture on a GPU without BC support: tex_desc made the Metal texture RGBA8 (or R8/RG8,\n // RGBA16F for BC6H), so the staging slot is per-pixel, not per-block, and the deswizzled block\n // data is expanded through nvn_bc_decode before the blit. bc_cpu stays 0 on Macs and A19+,\n // where the compressed path below is used unchanged.\n uint32_t bc_pf = 0; int bc_bpp = 0;\n int bc_cpu = g_bc_cpu && nvn_bc_unpacked(t->d.format, &bc_pf, &bc_bpp);\n size_t total_bc = 0;\n if (bc_cpu) {\n total_bc = 0;\n for (unsigned l = 0; l < d.mipmapLevelCount; l++) {\n 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;\n total_bc += (size_t)w * h * z * layers * (size_t)bc_bpp;\n }\n }\n up_begin();\n id<MTLBuffer> stg = [g_dev newBufferWithLength:(bc_cpu ? total_bc : total) ? (bc_cpu ? total_bc : total) : 16 options:MTLResourceStorageModeShared];\n res_add(stg); [g_up_keep addObject:stg];\n size_t off = 0;\n 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); }\n // CPU-side scratch for the compressed block data when bc_cpu (one level-slice at a time)\n uint8_t *bc_tmp = NULL; size_t bc_tmp_sz = 0;\n for (unsigned l = 0; l < d.mipmapLevelCount; l++) {\n 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;\n size_t pitch = (size_t)((w + bw - 1) / bw) * bpb, rows = (h + bh - 1) / bh;\n for (unsigned y = 0; y < layers; y++) {\n if (bc_cpu) {\n size_t need = pitch * rows * z;\n if (need > bc_tmp_sz) { free(bc_tmp); bc_tmp = malloc(need); bc_tmp_sz = need; if (!bc_tmp) MDIE("BC decode scratch of %zu bytes failed", need); }\n if (nvn_deswizzle(&t->d, l, y, src, bc_tmp, pitch)) { RED_ONCE(9, "deswizzle failed format %#x target %d", t->d.format, t->d.target); break; }\n size_t dst_pitch = (size_t)w * bc_bpp;\n for (uint32_t zz = 0; zz < z; zz++)\n nvn_bc_decode(t->d.format, bc_tmp + (size_t)zz * pitch * rows, pitch, w, h,\n (uint8_t *)stg.contents + off + (size_t)zz * dst_pitch * h, dst_pitch);\n up_blit_add(stg, off, dst_pitch, dst_pitch * h, MTLSizeMake(w, h, z), mt, y, l);\n off += dst_pitch * h * z;\n continue;\n }\n 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; }\n if (zb && l < 16) { const uint64_t *q = (const uint64_t *)((const uint8_t *)stg.contents + off); size_t nb = pitch * rows * z / 16;\n for (size_t i = 0; i < nb; i++) g_zb[l] += !(q[2 * i] | q[2 * i + 1]);\n g_zb_tot[l] += (uint32_t)nb; }\n up_blit_add(stg, off, pitch, pitch * rows, MTLSizeMake(w, h, z), mt, y, l);\n')]),
+ ('bc cpu free', ['free(bc_tmp);\n free(part);'], [(
+ " free(part);\n up_flush();\n return mt;",
+ " free(bc_tmp);\n free(part);\n up_flush();\n return mt;")]),
('up_begin publish', ['the iOS save-load freeze of NX2Apple issue #4', "Little Nightmares II's first render passes"], [(
''' unsigned slot = g_up_seq++ % 3;
up_wait_zero(&g_up_alloc_busy[slot]);''',