mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-19 04:19:57 -04:00
Copy large textures out of the game's memory on several threads at once: a level of 1 MiB or more goes to a small worker pool as bands of rows (par_jobs.c, tex_deswizzle.c tex_bl_copy_mt), and so does a large block copy for the GPU's ASTC decoder, so the upload's storage copy no longer runs on the drawing thread alone. The bytes are those of the one-thread copy; par_jobs = 0 turns it off.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
// ASTC 4x4 title logo and BC4 masks decode without seams (an edge-continuity search over all 120 orders of the five
|
||||
// address bits picks it for both; OCARINA-BRINGUP.md round 18). Block and level arithmetic are unchanged.
|
||||
#include "host.h"
|
||||
#include "par_jobs.h"
|
||||
#include "tex.h"
|
||||
#include <string.h>
|
||||
|
||||
@@ -62,29 +63,71 @@ int nvn_deswizzle_geom(const nvn_texdesc *d, unsigned level, unsigned layer,
|
||||
|
||||
// nvn_bl_offset per row instead of per 16 bytes: within a row, GOB column k starts k blocks further, and the four
|
||||
// 16-byte segments of a GOB row (x % 64 = 0, 16, 32, 48) sit at 0, 32, 256 and 288 of it (Switch 2: 0, 64, 256, 320).
|
||||
void tex_bl_copy(uint8_t *bl, uint8_t *lin, size_t pitch, uint32_t row_bytes, uint32_t rows, uint32_t slices,
|
||||
unsigned th, unsigned td, int to_linear) {
|
||||
// Rows [r0, r1) of the slices one after the other (row z * rows + y): each row finds its own place in the block-linear
|
||||
// storage, so any range of rows copies on its own, and tex_bl_copy_mt hands ranges to several threads.
|
||||
static void bl_copy_rows(uint8_t *bl, uint8_t *lin, size_t pitch, uint32_t row_bytes, uint32_t rows, uint64_t r0,
|
||||
uint64_t r1, unsigned th, unsigned td, int to_linear) {
|
||||
static const uint32_t seg_x1[4] = {0, 32, 256, 288}, seg_nvn2[4] = {0, 64, 256, 320};
|
||||
const uint32_t *seg = gob_nvn2() ? seg_nvn2 : seg_x1;
|
||||
uint64_t block = 512ull << th << td;
|
||||
for (uint32_t z = 0; z < slices; z++)
|
||||
for (uint32_t y = 0; y < rows; y++) {
|
||||
uint8_t *row = lin + ((size_t)z * rows + y) * pitch, *g = bl + nvn_bl_offset(0, y, z, row_bytes, rows, th, td);
|
||||
uint32_t x = 0;
|
||||
if (to_linear)
|
||||
for (; x + 64 <= row_bytes; x += 64, g += block)
|
||||
for (int s = 0; s < 4; s++) memcpy(row + x + 16 * s, g + seg[s], 16);
|
||||
else
|
||||
for (; x + 64 <= row_bytes; x += 64, g += block)
|
||||
for (int s = 0; s < 4; s++) memcpy(g + seg[s], row + x + 16 * s, 16);
|
||||
for (int s = 0; x < row_bytes; s++, x += 16) { // a partial GOB at the row's end
|
||||
uint32_t n = row_bytes - x < 16 ? row_bytes - x : 16;
|
||||
if (to_linear) memcpy(row + x, g + seg[s], n); else memcpy(g + seg[s], row + x, n);
|
||||
}
|
||||
for (uint64_t r = r0; r < r1; r++) {
|
||||
uint32_t z = (uint32_t)(r / rows), y = (uint32_t)(r % rows);
|
||||
uint8_t *row = lin + (size_t)r * pitch, *g = bl + nvn_bl_offset(0, y, z, row_bytes, rows, th, td);
|
||||
uint32_t x = 0;
|
||||
if (to_linear)
|
||||
for (; x + 64 <= row_bytes; x += 64, g += block)
|
||||
for (int s = 0; s < 4; s++) memcpy(row + x + 16 * s, g + seg[s], 16);
|
||||
else
|
||||
for (; x + 64 <= row_bytes; x += 64, g += block)
|
||||
for (int s = 0; s < 4; s++) memcpy(g + seg[s], row + x + 16 * s, 16);
|
||||
for (int s = 0; x < row_bytes; s++, x += 16) { // a partial GOB at the row's end
|
||||
uint32_t n = row_bytes - x < 16 ? row_bytes - x : 16;
|
||||
if (to_linear) memcpy(row + x, g + seg[s], n); else memcpy(g + seg[s], row + x, n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int move(const nvn_texdesc *d, unsigned level, unsigned layer, uint8_t *bl, uint8_t *lin, size_t pitch, int to_linear) {
|
||||
void tex_bl_copy(uint8_t *bl, uint8_t *lin, size_t pitch, uint32_t row_bytes, uint32_t rows, uint32_t slices,
|
||||
unsigned th, unsigned td, int to_linear) {
|
||||
bl_copy_rows(bl, lin, pitch, row_bytes, rows, 0, (uint64_t)rows * slices, th, td, to_linear);
|
||||
}
|
||||
|
||||
// tex_bl_copy over the par_jobs pool: a level of BL_MT_MIN bytes or more (smaller ones gained nothing: 0.5 MiB took
|
||||
// 0.01 ms either way, 2 MiB went 3.3 times faster, 16 MiB 2.5 times) goes as bands of rows, about BL_MT_BAND
|
||||
// bytes each, so a large texture's copy out of the game's memory (the upload's storage copy, 75% of its CPU time in an
|
||||
// area's streaming) does not run on the drawing thread alone. The bytes written are those of tex_bl_copy.
|
||||
#define BL_MT_MIN (1u << 20)
|
||||
#define BL_MT_BAND (128u << 10)
|
||||
typedef struct {
|
||||
uint8_t *bl, *lin;
|
||||
size_t pitch;
|
||||
uint32_t row_bytes, rows;
|
||||
uint64_t all, band;
|
||||
unsigned th, td;
|
||||
int to_linear;
|
||||
} BlJob;
|
||||
|
||||
static void bl_band(void *arg, int i) {
|
||||
const BlJob *j = arg;
|
||||
uint64_t r0 = (uint64_t)i * j->band, r1 = r0 + j->band < j->all ? r0 + j->band : j->all;
|
||||
bl_copy_rows(j->bl, j->lin, j->pitch, j->row_bytes, j->rows, r0, r1, j->th, j->td, j->to_linear);
|
||||
}
|
||||
|
||||
void tex_bl_copy_mt(uint8_t *bl, uint8_t *lin, size_t pitch, uint32_t row_bytes, uint32_t rows, uint32_t slices,
|
||||
unsigned th, unsigned td, int to_linear) {
|
||||
uint64_t all = (uint64_t)rows * slices, bytes = all * row_bytes;
|
||||
if (bytes < BL_MT_MIN || !row_bytes || par_threads() < 2) {
|
||||
tex_bl_copy(bl, lin, pitch, row_bytes, rows, slices, th, td, to_linear);
|
||||
return;
|
||||
}
|
||||
uint64_t band = BL_MT_BAND / row_bytes;
|
||||
if (band < 1) band = 1;
|
||||
BlJob j = {bl, lin, pitch, row_bytes, rows, all, band, th, td, to_linear};
|
||||
par_run((int)((all + band - 1) / band), bl_band, &j);
|
||||
}
|
||||
|
||||
static int move(const nvn_texdesc *d, unsigned level, unsigned layer, uint8_t *bl, uint8_t *lin, size_t pitch, int to_linear,
|
||||
int mt) {
|
||||
if (nvn_tex_is_linear(d)) {
|
||||
if (level || layer || 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);
|
||||
@@ -101,7 +144,7 @@ static int move(const nvn_texdesc *d, unsigned level, unsigned layer, uint8_t *b
|
||||
uint32_t row_bytes, rows, slices, bpb; uint64_t base; unsigned th, td;
|
||||
if (nvn_deswizzle_geom(d, level, layer, &row_bytes, &rows, &slices, &base, &th, &td, &bpb)) return -1;
|
||||
if (pitch < row_bytes) return -1;
|
||||
tex_bl_copy(bl + base, lin, pitch, row_bytes, rows, slices, th, td, to_linear);
|
||||
(mt ? tex_bl_copy_mt : tex_bl_copy)(bl + base, lin, pitch, row_bytes, rows, slices, th, td, to_linear);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -138,8 +181,11 @@ int nvn_texels_box(const nvn_texdesc *d, unsigned level, unsigned layer, uint32_
|
||||
}
|
||||
|
||||
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);
|
||||
return move(d, level, layer, (uint8_t *)src, (uint8_t *)dst, dst_pitch, 1, 0);
|
||||
}
|
||||
int nvn_deswizzle_mt(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, 1);
|
||||
}
|
||||
int nvn_swizzle(const nvn_texdesc *d, unsigned level, unsigned layer, const void *src, size_t src_pitch, void *dst) {
|
||||
return move(d, level, layer, (uint8_t *)dst, (uint8_t *)src, src_pitch, 0);
|
||||
return move(d, level, layer, (uint8_t *)dst, (uint8_t *)src, src_pitch, 0, 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user