Copy only the region of a ReadTexels or WriteTexels call instead of swizzling the whole texture level

A guest WriteTexels of a small region deswizzled and reswizzled the whole level while its thread held a virtual core
and the texture lock: 80 ms preemption delays and 50-160 ms frames in the monsters2 replay (frames 765-790,
1225-1235), with the render thread waiting on the texture lock. nvn_texels_box copies the box alone; probe-vkmem
compares it with the whole-level path on random boxes of five block sizes, mip levels, array layers and 3D slices.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-10-25 14:12:43 +03:00
1 parent 8a41e3a7ef
commit f178be9eb2
4 files changed
+106 -21

No files matched your search

+5
View File
@@ -17,6 +17,11 @@ int nvn_deswizzle(const nvn_texdesc *d, unsigned level, unsigned layer, const vo
size_t dst_pitch);
int nvn_swizzle(const nvn_texdesc *d, unsigned level, unsigned layer, const void *src_linear, size_t src_pitch,
void *dst_storage);
// One box of a level and layer, [x, x + wb) bytes by [y, y + h) block rows of slice z (3D textures), between the storage
// and caller rows of pitch bytes, touching nothing else of the level: ReadTexels / WriteTexels of a region cost the
// region, not the level. Same textures as nvn_deswizzle; -1 for others and for a box outside the level.
int nvn_texels_box(const nvn_texdesc *d, unsigned level, unsigned layer, uint32_t x, uint32_t y, uint32_t z, uint32_t wb,
uint32_t h, void *storage, void *rows, size_t pitch, int to_linear);
// Byte offset of (x byte, y block row, z slice) inside one level of block height 2^th and block depth 2^td GOBs.
uint64_t nvn_bl_offset(uint32_t x, uint32_t y, uint32_t z, uint32_t row_bytes, uint32_t rows, unsigned th, unsigned td);
// Geometry of a level as the two functions above use it; -1 for textures they reject.
+32
View File
@@ -80,6 +80,38 @@ static int move(const nvn_texdesc *d, unsigned level, unsigned layer, uint8_t *b
return 0;
}
int nvn_texels_box(const nvn_texdesc *d, unsigned level, unsigned layer, uint32_t x, uint32_t y, uint32_t z, uint32_t wb,
uint32_t h, void *storage, void *rows, size_t pitch, int to_linear) {
uint8_t *st = storage, *ln = rows;
if (nvn_tex_is_linear(d)) {
if (level || layer || z || d->samples > 1 || (d->target != 0 && d->target != 1 && d->target != 7)) return -1;
int bw, bh, bpb = nvn_fmt_bpb(d->format, &bw, &bh);
if (!bpb) return -1;
size_t row = (size_t)((d->width + bw - 1) / bw) * bpb;
uint32_t nrows = d->target == 0 ? 1 : (d->height + bh - 1) / bh, stride = nvn_tex_linear_stride(d);
if (stride < row || x + (size_t)wb > row || y + (uint64_t)h > nrows) return -1;
for (uint32_t r = 0; r < h; r++) {
uint8_t *p = st + (size_t)(y + r) * stride + x, *q = ln + r * pitch;
if (to_linear) memcpy(q, p, wb); else memcpy(p, q, wb);
}
return 0;
}
uint32_t row_bytes, nrows, slices, bpb; uint64_t base; unsigned th, td;
if (nvn_deswizzle_geom(d, level, layer, &row_bytes, &nrows, &slices, &base, &th, &td, &bpb)) return -1;
if (x + (uint64_t)wb > row_bytes || y + (uint64_t)h > nrows || z >= slices) return -1;
for (uint32_t r = 0; r < h; r++) {
uint8_t *q = ln + r * pitch;
for (uint32_t i = 0; i < wb;) {
uint32_t xb = x + i, n = 16 - xb % 16; // contiguous up to the end of its 16-byte segment
if (n > wb - i) n = wb - i;
uint8_t *p = st + base + nvn_bl_offset(xb, y + r, z, row_bytes, nrows, th, td);
if (to_linear) memcpy(q + i, p, n); else memcpy(p, q + i, n);
i += n;
}
}
return 0;
}
int nvn_deswizzle(const nvn_texdesc *d, unsigned level, unsigned layer, const void *src, void *dst, size_t dst_pitch) {
return move(d, level, layer, (uint8_t *)src, (uint8_t *)dst, dst_pitch, 1);
}
+60 -1
View File
@@ -118,6 +118,65 @@ static int test_deswizzle(void) {
return 0;
}
// nvn_texels_box against the whole-level path on random boxes: a box read equals the box cut from the deswizzled level,
// and a box write leaves the storage as deswizzle, overwrite the box, swizzle does (nothing outside the box changes).
static int check_box(const nvn_texdesc *d, unsigned lv, unsigned layer) {
uint32_t rb, rows, sl, bpb; uint64_t base; unsigned th, td;
if (nvn_deswizzle_geom(d, lv, layer, &rb, &rows, &sl, &base, &th, &td, &bpb)) { printf("probe-vkmem: FAIL box geometry\n"); return 1; }
nvn_texlayout L;
nvn_tex_layout(d, &L);
uint8_t *store = malloc(L.size), *want = malloc(L.size), *lin = malloc((size_t)rb * rows * sl);
for (uint64_t i = 0; i < L.size; i++) store[i] = rnd8();
uint32_t cols = rb / bpb;
for (int k = 0; k < 8; k++) {
uint32_t bx = rnd8() % cols, bn = 1 + rnd8() % (cols - bx), y = rnd8() % rows, h = 1 + rnd8() % (rows - y);
uint32_t z = sl > 1 ? rnd8() % sl : 0, x = bx * bpb, wb = bn * bpb;
size_t pitch = wb + 8;
uint8_t *box = malloc(pitch * h), *ref = malloc(pitch * h);
// read
if (nvn_deswizzle(d, lv, layer, store, lin, rb)) { printf("probe-vkmem: FAIL box deswizzle\n"); return 1; }
memset(box, 0xAB, pitch * h), memset(ref, 0xAB, pitch * h);
for (uint32_t r = 0; r < h; r++) memcpy(ref + r * pitch, lin + ((size_t)z * rows + y + r) * rb + x, wb);
if (nvn_texels_box(d, lv, layer, x, y, z, wb, h, store, box, pitch, 1) || memcmp(box, ref, pitch * h)) {
printf("probe-vkmem: FAIL box read format %#x level %u layer %u box %u,%u,%u %ux%u\n", d->format, lv, layer, x, y, z, wb, h);
return 1;
}
// write
for (size_t i = 0; i < pitch * h; i++) box[i] = rnd8();
memcpy(want, store, L.size);
for (uint32_t r = 0; r < h; r++) memcpy(lin + ((size_t)z * rows + y + r) * rb + x, box + r * pitch, wb);
if (nvn_swizzle(d, lv, layer, lin, rb, want)) { printf("probe-vkmem: FAIL box swizzle\n"); return 1; }
if (nvn_texels_box(d, lv, layer, x, y, z, wb, h, store, box, pitch, 0) || memcmp(store, want, L.size)) {
printf("probe-vkmem: FAIL box write format %#x level %u layer %u box %u,%u,%u %ux%u\n", d->format, lv, layer, x, y, z, wb, h);
return 1;
}
g_cases++;
free(box), free(ref);
}
free(store), free(want), free(lin);
return 0;
}
static int test_box(void) {
// one format per block size: 1, 8 and 16 bytes per texel, 8 and 16 bytes per 4x4 block
int fmt[5] = {0}, n = 0;
static const int want_bpb[5] = {1, 8, 16, 8, 16}, want_bw[5] = {1, 1, 1, 4, 4};
for (int f = 1; f < 0x200 && n < 5; f++) {
int bw, bh, bpb = nvn_fmt_bpb(f, &bw, &bh);
for (int i = 0; i < 5; i++)
if (!fmt[i] && bpb == want_bpb[i] && bw == want_bw[i] && bh == bw) fmt[i] = f, n++;
}
for (int i = 0; i < 5; i++) {
if (!fmt[i]) continue;
nvn_texdesc d2 = {.target = 1, .format = fmt[i], .width = 300, .height = 170, .depth = 1, .levels = 3};
nvn_texdesc da = {.target = 4, .format = fmt[i], .width = 96, .height = 80, .depth = 3, .levels = 2};
nvn_texdesc d3 = {.target = 2, .format = fmt[i], .width = 72, .height = 40, .depth = 5, .levels = 2};
for (unsigned lv = 0; lv < 2; lv++)
if (check_box(&d2, lv, 0) || check_box(&da, lv, 2) || check_box(&d3, lv, 0)) return 1;
}
return 0;
}
// ---- ASTC ----
static void void_block(uint8_t b[16], uint16_t r, uint16_t g, uint16_t bl, uint16_t a, int hdr) {
uint64_t lo = 0xFFFFFFFFFFFFFDFCull; // 0x1FC mode, LDR, reserved bits 11, extent all ones
@@ -522,7 +581,7 @@ static int test_gpu(void) {
}
int probe_vkmem(void) {
if (test_deswizzle() || test_astc() || test_formats()) return 1;
if (test_deswizzle() || test_box() || test_astc() || test_formats()) return 1;
printf("probe-vkmem: cpu part passed (%u deswizzle cases, %u astc blocks, formats: %u mapped, %u supported by the GPU, "
"%u unsupported by the GPU, %u unmapped, %u with a fallback)\n",
g_cases, g_astc_blocks, n_mapped, n_supported, n_unsupported, n_unmapped, n_fallback);
+9 -20
View File
@@ -932,29 +932,18 @@ static int region_of(const tex_t *t, const view_t *v, const NVNcopyRegion *r, ui
return 1;
}
// Pool bytes <-> caller rows through a linear copy of the level (MNG nvn_mtl.m:10121-10132, 10158-10170).
// Pool bytes <-> caller rows of the region alone (nvn_texels_box). MNG nvn_mtl.m:10121-10132, 10158-10170 go through a
// linear copy of the whole level; for a small region of a large texture that cost tens of milliseconds while the guest
// thread held its virtual core and the caller the texture lock (monsters2 replay, frames 765-790).
static int storage_texels(const tex_t *t, const Region *g, const NVNcopyRegion *r, uint8_t *mem, int write) {
const nvn_texdesc *d = &t->d;
uint32_t lw = mip(d->width, g->level), lh = d->target == 0 || d->target == 3 ? 1 : mip(d->height, g->level);
uint32_t lz = g->is3d ? mip(d->depth, g->level) : 1;
size_t lp = (size_t)((lw + g->bw - 1) / g->bw) * (size_t)g->bpb, lr = (lh + g->bh - 1) / (uint32_t)g->bh;
if (g->x0 + g->wb > lp || g->y0 + g->hb > lr || (g->is3d && (uint32_t)(r->zoffset + g->nz) > lz)) return 0;
uint8_t *lin = malloc(lp * lr * lz);
if (!lin) rt_fatal("vk: out of memory for texels");
int ok = 1;
for (int z = 0; z < g->nz && ok; z++) {
for (int z = 0; z < g->nz; z++) {
unsigned layer = g->is3d ? 0 : g->layer0 + (unsigned)r->zoffset + (unsigned)z;
if ((!g->is3d || z == 0) && nvn_deswizzle(d, g->level, layer, t->mem, lin, lp)) { ok = 0; break; }
uint8_t *slice = lin + (g->is3d ? (size_t)(r->zoffset + z) * lp * lr : 0);
for (size_t y = 0; y < g->hb; y++) {
uint8_t *p = slice + (g->y0 + y) * lp + g->x0, *q = mem + (size_t)z * g->img + y * g->row;
if (write) memcpy(p, q, g->wb);
else memcpy(q, p, g->wb);
}
if (write && (!g->is3d || z == g->nz - 1) && nvn_swizzle(d, g->level, layer, lin, lp, t->mem)) ok = 0;
uint32_t slice = g->is3d ? (uint32_t)(r->zoffset + z) : 0;
if (nvn_texels_box(&t->d, g->level, layer, (uint32_t)g->x0, (uint32_t)g->y0, slice, (uint32_t)g->wb,
(uint32_t)g->hb, t->mem, mem + (size_t)z * g->img, g->row, !write))
return 0;
}
free(lin);
return ok;
return 1;
}
static int read_aspect(const Tex *e, VkImageAspectFlags *a) {