From e3f623942bae1bb3f33062e76c81a5e5f620894f Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 09:38:08 -0500 Subject: [PATCH] Check the texture detail in --probe-vkmem: ASTC textures are uploaded and rewritten, and every level and layer of their images must hold the game's level it stands for. The probe takes the level from NX2NAVI_TEXTURE_DETAIL. It covers a 1024x1024 texture, a 256x256 array of three layers, and a 128x128 and an unmipped texture that stay whole, with the images as RGBA8 or BC3, and the sampler LOD clamp of a reduced texture. It passes at full, reduced and low on an RTX 5090, with NX2NAVI_ASTC_BC 0 and 1. Co-Authored-By: Claude Fable 5.2 --- docs/building.md | 3 + runtime/tex_probe.c | 137 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 139 insertions(+), 1 deletion(-) diff --git a/docs/building.md b/docs/building.md index 516edbe0..9dc30332 100644 --- a/docs/building.md +++ b/docs/building.md @@ -340,6 +340,9 @@ Runtime probes, each a mode of `nx2navi.exe` (the list is at the top of `runtime part of `--probe-vkexec` translate shaders of the census below: they look for `build//shaders/jobs.json` of the exe's own version first, then of the other one. `--probe-vkmem` also checks CPU-backed texture uploads and rewrites, GPU-written texture aliases, and memory write watches. +A change to the texture detail (`runtime/vk_texture.c` "texture detail": `detail_skip`, `upload`, `astc_bl_regions`) +passes it three times, with `NX2NAVI_TEXTURE_DETAIL` set to `full`, `reduced` and `low`: each time ASTC textures are +uploaded and rewritten, and every level and layer of their images must hold the game's level it stands for. `--probe-prewarm` runs the boot's pipeline stages without the game, from the caches next to the exe (it skips when there are none): the pipeline cache load, the shader prewarm, the recorded pipelines and the compute pipeline prebuild, with a `mem: boot stage` log line after each that gives the device memory and the process commit so far. It then asks diff --git a/runtime/tex_probe.c b/runtime/tex_probe.c index f7c64eb6..49d85853 100644 --- a/runtime/tex_probe.c +++ b/runtime/tex_probe.c @@ -1,12 +1,14 @@ // nx2navi.exe --probe-vkmem: texture memory checks. The CPU part: block-linear copies against an // independent reference, ASTC void-extent blocks, and the NVN -> VkFormat table against GPU0. The GPU part: // guest heap imported as a VkBuffer, write watch against CPU, GPU and ReadFile writes, the virtual-pool page table, -// and textures uploaded from block-linear storage and read back. +// and textures uploaded from block-linear storage and read back, among them ASTC textures at the texture detail in +// effect (NX2NAVI_TEXTURE_DETAIL=reduced or low to check the levels left out). // The reference swizzler walks the GOBs in storage order and places each byte by bit operations, so it shares no // addressing code with nvn_bl_offset. #include #include #include +#include "cfg.h" #include "host.h" #include "nvn_api.h" #include "tex.h" @@ -659,6 +661,134 @@ static int image_is_color(VkImage image, uint32_t w, uint32_t h, const uint8_t w return same; } +// ---- texture detail (vk_texture.c "texture detail"): an ASTC texture made without its top mip levels +// Every level and layer of t's storage becomes ASTC void-extent blocks of one colour, each channel 0 or 255 so that +// the RGBA8 decode and its BC3 encoding both hold it exactly. The colour says which level and layer it is. +static void detail_color(unsigned lv, unsigned layer, unsigned round, uint8_t c[4]) { + unsigned k = 1 + (lv * 3 + layer + round * 2) % 7; // never black: 1 to 7 + c[0] = k & 1 ? 255 : 0, c[1] = k & 2 ? 255 : 0, c[2] = k & 4 ? 255 : 0, c[3] = 255; +} + +static int detail_fill(const tex_t *t, unsigned layers, unsigned round) { + for (unsigned lv = 0; lv < t->d.levels; lv++) + for (unsigned ly = 0; ly < layers; ly++) { + uint32_t rb, rows, sl, bpb; uint64_t base; unsigned th, td; + GCHECK(!nvn_deswizzle_geom(&t->d, lv, ly, &rb, &rows, &sl, &base, &th, &td, &bpb), "ASTC geometry of level %u", lv); + size_t n = (size_t)rb * rows; + uint8_t *tight = malloc(n), c[4]; + detail_color(lv, ly, round, c); + for (size_t i = 0; i < n; i += 16) + void_block(tight + i, (uint16_t)(c[0] * 257), (uint16_t)(c[1] * 257), (uint16_t)(c[2] * 257), 0xFFFF, 0); + ref_swizzle(tight, t->mem + base, rb, rows, 1, th, td); + free(tight); + } + return 0; +} + +// Whether level lv, layer ly of image (RGBA8 or BC3, w x h at that level) is `want` in every texel. +static int detail_level_is(const VkTexImage *img, uint32_t lv, uint32_t ly, uint32_t w, uint32_t h, const uint8_t want[4]) { + VkCtx *c = vk_ctx(); + int bc = img->format == VK_FORMAT_BC3_UNORM_BLOCK || img->format == VK_FORMAT_BC3_SRGB_BLOCK; + size_t n = bc ? (size_t)((w + 3) / 4) * ((h + 3) / 4) * 16 : (size_t)w * h * 4; + VkBuffer rb; + VmaAllocation ra; + uint8_t *r = vk_staging(n, &rb, &ra); + VkCommandBuffer cb = vk_upload_begin(); + VkMemoryBarrier2 mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, .srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, + .srcAccessMask = VK_ACCESS_2_MEMORY_WRITE_BIT, .dstStageMask = VK_PIPELINE_STAGE_2_COPY_BIT, + .dstAccessMask = VK_ACCESS_2_TRANSFER_READ_BIT}; + VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &mb}; + vkCmdPipelineBarrier2(cb, &di); + VkBufferImageCopy rg = {.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, lv, ly, 1}, .imageExtent = {w, h, 1}}; + vkCmdCopyImageToBuffer(cb, img->image, VK_IMAGE_LAYOUT_GENERAL, rb, 1, &rg); + mb = (VkMemoryBarrier2){.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, .srcStageMask = VK_PIPELINE_STAGE_2_COPY_BIT, + .srcAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT, .dstStageMask = VK_PIPELINE_STAGE_2_HOST_BIT, + .dstAccessMask = VK_ACCESS_2_HOST_READ_BIT}; + vkCmdPipelineBarrier2(cb, &di); + vk_upload_end(); + vk_upload_flush(1); + vmaInvalidateAllocation(c->vma, ra, 0, VK_WHOLE_SIZE); + int same = 1; + if (bc) { // BC3: two 5:6:5 endpoints at bytes 8 to 11; both are the colour, so every texel is + uint16_t e = (uint16_t)((want[0] >> 3) << 11 | (want[1] >> 2) << 5 | want[2] >> 3); + for (size_t i = 0; i < n; i += 16) { + uint16_t e0, e1; + memcpy(&e0, r + i + 8, 2), memcpy(&e1, r + i + 10, 2); + same &= e0 == e && e1 == e; + } + } else { + for (size_t i = 0; i < n; i += 4) same &= !memcmp(r + i, want, 4); + } + vmaDestroyBuffer(c->vma, rb, ra); + return same; +} + +// The image of an ASTC texture has the game's levels from the skip the setting gives it: its size is that level's, and +// each of its levels and layers holds that level's blocks, after the upload and after a rewrite of the storage. +static int detail_case(pool_t *pool, uint64_t off, uint64_t room, uint32_t size, uint32_t levels, unsigned layers, + uint64_t serial, uint32_t want_skip) { + tex_t t = make_tex(pool, off, 0x80, size, size, levels, serial); // ASTC 8x8 + if (layers > 1) { + t.d.target = NVN_TEXTURE_TARGET_2D_ARRAY, t.d.depth = layers; + nvn_tex_layout(&t.d, &t.L); + } + GCHECK(t.L.size <= room && off + room <= pool->size, "ASTC %ux%u storage of %llu bytes does not fit its %llu in the " + "probe's pool", size, size, (unsigned long long)t.L.size, (unsigned long long)room); + for (unsigned round = 0; round < 2; round++) { + if (detail_fill(&t, layers, round)) return 1; + const VkTexImage *img = vk_texture_get(&t); + GCHECK(img && img->mirrored, "ASTC %ux%u texture image", size, size); + if (img->format != VK_FORMAT_R8G8B8A8_UNORM && img->format != VK_FORMAT_BC3_UNORM_BLOCK) return 0; // native ASTC + GCHECK(img->skip == want_skip && img->levels == levels - want_skip && img->width == size >> want_skip && + img->height == size >> want_skip && img->layers == layers, + "ASTC %ux%u, %u levels, %u layers: image %ux%u with %u levels, %u layers, made without %u (want %u)", size, size, + levels, layers, img->width, img->height, img->levels, img->layers, img->skip, want_skip); + for (uint32_t lv = 0; lv < img->levels; lv++) + for (uint32_t ly = 0; ly < layers; ly++) { + uint8_t want[4]; + uint32_t w = size >> (lv + img->skip) ? size >> (lv + img->skip) : 1; + detail_color(lv + img->skip, ly, round, want); + GCHECK(detail_level_is(img, lv, ly, w, w, want), + "ASTC %ux%u made without %u levels: image level %u layer %u is not the game's level %u (%s)", size, size, + img->skip, lv, ly, lv + img->skip, round ? "after a rewrite of the storage" : "first upload"); + g_gpu_checks++; + } + } + vk_texture_release(&t); + return 0; +} + +static int test_detail(pool_t *pool) { + const char *v = cfg_value("NX2NAVI_TEXTURE_DETAIL"); + int most = !strcmp(v, "full") ? 0 : !strcmp(v, "reduced") ? 1 : !strcmp(v, "low") ? 2 : vk_texture_detail_auto(); + // 1024x1024 with 6 levels (two levels less at low), a 256x256 array of 3 layers (one less below full), and the + // ones the rule leaves whole: 128x128, and 512x512 without smaller levels + if (detail_case(pool, 0, 0x80000, 1024, 6, 1, 20, most > 1 ? 2 : (uint32_t)most) || + detail_case(pool, 0x80000, 0x40000, 256, 4, 3, 21, most ? 1 : 0) || + detail_case(pool, 0xc0000, 0x10000, 128, 4, 1, 22, 0) || detail_case(pool, 0xd0000, 0x20000, 512, 1, 1, 23, 0)) + return 1; + // the sampler of a texture made without levels: an open LOD clamp is the slot's sampler, a closed one moves down + samp_par sp; + memset(&sp, 0, sizeof sp); + sp.minf = NVN_MIN_FILTER_LINEAR_MIPMAP_LINEAR, sp.magf = NVN_MAG_FILTER_LINEAR, sp.ws = sp.wt = sp.wr = NVN_WRAP_MODE_REPEAT; + sp.lod_max = 1000; + vk_sampler_register(2, &sp); + sp.lod_min = 1, sp.lod_max = 4; + vk_sampler_register(3, &sp); + sp.lod_min = 0, sp.lod_max = 3; + vk_texture_lock(); + VkSampler open1 = vk_sampler_detail_by_id_locked(2, 1), shut1 = vk_sampler_detail_by_id_locked(3, 1); + vk_texture_unlock(); + GCHECK(open1 && open1 == vk_sampler_by_id(2), "the sampler of a reduced texture with an open LOD clamp is not the slot's"); + GCHECK(shut1 && shut1 != vk_sampler_by_id(3) && shut1 == vk_sampler_for(&sp), + "the sampler of a reduced texture with LOD 1..4 does not clamp to 0..3"); + g_gpu_checks += 2; + printf("probe-vkmem: texture detail %s: ASTC images hold the game's levels from the one they start at, after upload " + "and re-upload (1024x1024, a 256x256 array, and 128x128 and unmipped left whole); sampler LOD clamps follow\n", + most == 0 ? "full" : most == 1 ? "reduced" : "low"); + return 0; +} + static int test_textures(pool_t *tpool, pool_t *phys) { uint8_t *lin[4] = {0}; // block-linear RGBA8 with 3 levels in a CPU-visible pool: uploaded through the GPU, read back from the image @@ -753,6 +883,11 @@ static int test_gpu(void) { if (test_import(&pool) || test_readfile(pool.mem + 0x38000) || test_virtual(&phys, &virt)) return 1; GCHECK(vk_pool_init(&tpool), "vk_pool_init of the texture pool"); if (test_textures(&tpool, &phys)) return 1; + pool_t dpool = {.magic = NVN_POOL_MAGIC, .flags = 0x22, .mem = h + 0x500000, .size = 0x100000, .game_mem = h + 0x500000}; + GCHECK(vk_pool_init(&dpool), "vk_pool_init of the texture detail pool"); + int detail_bad = test_detail(&dpool); + vk_pool_fini(&dpool); + if (detail_bad) return 1; VkMemStats st; vk_mem_stats(&st); GCHECK(st.imported_pools == 2 && st.physical_pools == 1 && st.virtual_pools == 1 && !st.import_failures,