mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-15 23:39:57 -04:00
Sample a flat texture that a shader reads as a 3D one through a one-slice 3D copy instead of a stand-in.
Maxwell samples a texture by its own descriptor, so a 2D texture under a 3D sample gives its texel at (u, v). Vulkan has no 3D view of a 2D image, and get_view found no view for such a binding: the draw sampled a stand-in. Such a texture (plain colour, one sample, up to 1024 x 1024 texels) now gets a 3D image of one slice with all of its levels, made in the upload batch and copied again when the texture was uploaded or drawn into since, or another layer is asked for (vk_texture.c vk_texture_flat3d_locked). NX2NAVI_TEX_FLAT3D=0 gives the stand-in as before. Seen in Kakariko: two of the game's 4 x 4 RGBA8 textures are sampled as 3D textures, and each got its copy; the runs through eight reloads at 60 and 144 fps ended without errors. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -58,7 +58,7 @@ static const char *const known[] = {
|
||||
"MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_CLICK", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MICROPHONE", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MONITOR", "MOTION", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOUSE_TRACE", "MOVIE_HOST", "MOVIE_PACE", "MOVIE_CLOCK", "TEST_HITCH",
|
||||
"MVOBJ_ALL", "MVOBJ_BANKS", "MVOBJ_EQUAL", "MVOBJ_EXACT", "MVOBJ_EXEC", "MVOBJ_NOC4", "MVOBJ_ONLY", "MVOBJ_TIMES", "MVPREV_CHECK", "MVPREV_GROW", "MVPREV_SKIP", "MVPREV_TRUST", "MV_CHECK",
|
||||
"MV_SURVEY",
|
||||
"ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PACE_HZ", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_RES_FIXED", "PATCH_SCENE", "PATCH_WIDE_ASPECT", "PATCH_WIDE_HUD", "PATCH_WIDE_MOVIE", "PATCH_WIDE_OUT", "PATCH_FREECAM", "PATCH_WIDE_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPELINE_CACHE_MAX_MB", "PIPELINE_CACHE_PLAY", "PIPELINE_CACHE_PLAY_MB", "PIPE_BINARIES", "PIPE_BINARIES_MAX_MB", "PIPE_FEEDBACK", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "POWER_FULL", "TEX_SAME", "PRESENT_HELD", "HELD_SYNC",
|
||||
"ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PACE_HZ", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_RES_FIXED", "PATCH_SCENE", "PATCH_WIDE_ASPECT", "PATCH_WIDE_HUD", "PATCH_WIDE_MOVIE", "PATCH_WIDE_OUT", "PATCH_FREECAM", "PATCH_WIDE_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPELINE_CACHE_MAX_MB", "PIPELINE_CACHE_PLAY", "PIPELINE_CACHE_PLAY_MB", "PIPE_BINARIES", "PIPE_BINARIES_MAX_MB", "PIPE_FEEDBACK", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "POWER_FULL", "TEX_SAME", "TEX_FLAT3D", "PRESENT_HELD", "HELD_SYNC",
|
||||
"PREPARE_PIPELINES", "PREWARM_FULL", "PREWARM_WAIT_MS", "PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES", "PROFILE_REPLAY", "PROFILE_SLOW",
|
||||
"POOL_FAST", "POOL_GROUP", "POOL_GROUP_CHECK", "POOL_PRESNAP",
|
||||
"PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_END", "REEXEC",
|
||||
|
||||
@@ -519,6 +519,9 @@ typedef struct {
|
||||
// the GPU is past the submission being recorded (vk_defer_free), as the replay and DLSS
|
||||
// keep pointers to render targets across a command buffer; no view of it is cached
|
||||
} VkTexImage;
|
||||
// A 2D texture a shader samples as a 3D one: its one-slice 3D copy, refreshed when its content changed (vk_texture.c);
|
||||
// NULL when it does not qualify. Texture lock held.
|
||||
const VkTexImage *vk_texture_flat3d_locked(const VkTexImage *img, uint32_t layer);
|
||||
// The window texture the backend presents (vk_backend.c): *image and *format become the float picture the game copied
|
||||
// into it, when the last draw into it was that copy and the swapchain is HDR10.
|
||||
void vk_hdr_present_source(const VkTexImage *win, VkImage *image, VkFormat *format);
|
||||
|
||||
@@ -754,7 +754,13 @@ static VkImageView get_view(VkExec *ex, const VkTexImage *img, const view_t *v,
|
||||
switch (vt) {
|
||||
case VK_IMAGE_VIEW_TYPE_1D:
|
||||
case VK_IMAGE_VIEW_TYPE_1D_ARRAY: ok = img->type == VK_IMAGE_TYPE_1D; break;
|
||||
case VK_IMAGE_VIEW_TYPE_3D: ok = img->type == VK_IMAGE_TYPE_3D; break;
|
||||
case VK_IMAGE_VIEW_TYPE_3D:
|
||||
ok = img->type == VK_IMAGE_TYPE_3D;
|
||||
if (!ok && !storage && !ms && img->type == VK_IMAGE_TYPE_2D) { // a flat texture under a 3D sample: its 3D copy
|
||||
const VkTexImage *f = vk_texture_flat3d_locked(img, hv && v->set_layers ? v->min_layer : 0);
|
||||
if (f) img = f, ok = 1;
|
||||
}
|
||||
break;
|
||||
case VK_IMAGE_VIEW_TYPE_CUBE:
|
||||
case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY:
|
||||
ok = img->type == VK_IMAGE_TYPE_2D && (img->view_type == VK_IMAGE_VIEW_TYPE_CUBE || img->view_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY);
|
||||
|
||||
@@ -107,6 +107,10 @@ typedef struct Tex {
|
||||
uint64_t og_frame; // gfx_counters.frames when it was made
|
||||
uint64_t og_drop_frame; // gfx_counters.frames of the drop og_drop names
|
||||
uint64_t og_back_frame; // gfx_counters.frames when it was last taken back
|
||||
VkTexImage *flat3d; // sampled as a 3D texture: its one-slice 3D copy (vk_texture_flat3d_locked), else NULL
|
||||
VmaAllocation flat3d_alloc;
|
||||
uint32_t flat3d_up, flat3d_layer; // n_up and the layer of the copy's last refresh
|
||||
uint64_t flat3d_wr; // wr_fence then
|
||||
} Tex;
|
||||
|
||||
// A slot keeps what vk_texture_by_id resolved for it, so a draw binding skips the key lookup and the address chain: the
|
||||
@@ -1580,6 +1584,12 @@ static void destroy_tex(Tex *e) {
|
||||
if (e->released) nreleased--, rel_unlink(e);
|
||||
if (e->pub.image) vk_exec_image_gone(e->pub.image); // none when another texture took it (create_tex, reuse)
|
||||
vk_defer_image(e->pub.image, e->alloc);
|
||||
if (e->flat3d) { // its 3D copy goes the same way
|
||||
vk_exec_image_gone(e->flat3d->image);
|
||||
vk_defer_image(e->flat3d->image, e->flat3d_alloc);
|
||||
e->flat3d->gone = 1;
|
||||
vk_defer_free(e->flat3d);
|
||||
}
|
||||
free(e->snap);
|
||||
// The record goes once the GPU is past the submission being recorded, as its image: the replay keeps &e->pub as a
|
||||
// render target for the rest of a command buffer (vk_exec.c resolve_target) and DLSS for the frame, and this runs on
|
||||
@@ -1589,6 +1599,80 @@ static void destroy_tex(Tex *e) {
|
||||
vk_defer_free(e);
|
||||
}
|
||||
|
||||
// ---- a flat texture sampled as a 3D one
|
||||
// Maxwell samples a texture by its own descriptor, whatever the shader's instruction says: a 2D texture under a 3D
|
||||
// sample gives its texel at (u, v), and the third coordinate does nothing. Vulkan has no 3D view of a 2D image, so
|
||||
// get_view (vk_exec_draw.c) found no view for such a binding and the draw sampled a stand-in. Such a texture now gets a
|
||||
// 3D image of one slice with its texels, all of its levels, made in the upload batch and copied again when the texture
|
||||
// was uploaded or drawn into since (n_up, wr_fence) or another layer is asked for; a 3D view of that gives the 2D
|
||||
// texel for any w. The copy is recorded in the upload batch, which runs before the part that samples it, so a texture
|
||||
// the GPU draws every frame is sampled as it was a part or a frame before. Small plain colour textures only:
|
||||
// FLAT3D_MAX texels, one sample, not block-compressed (BC rejects 3D on some drivers). NX2NAVI_TEX_FLAT3D=0: the
|
||||
// stand-in, as before. Texture lock held; NULL when the texture does not qualify or the image cannot be made.
|
||||
#define FLAT3D_MAX (1024u * 1024u)
|
||||
static volatile int32_t n_flat3d;
|
||||
|
||||
const VkTexImage *vk_texture_flat3d_locked(const VkTexImage *img, uint32_t layer) {
|
||||
static int on = -1;
|
||||
if (on < 0) on = rt_knob("TEX_FLAT3D", "1") == 1;
|
||||
Tex *e = (Tex *)img; // pub is the first member
|
||||
if (!on || img->gone || !img->image || img->type != VK_IMAGE_TYPE_2D || img->samples > 1 ||
|
||||
img->aspect != VK_IMAGE_ASPECT_COLOR_BIT || img->block_views || format_compressed(img->format) ||
|
||||
(uint64_t)img->width * img->height > FLAT3D_MAX || layer >= img->layers || img->levels > 16)
|
||||
return NULL;
|
||||
VkCtx *c = vk_ctx();
|
||||
VkCommandBuffer cb = VK_NULL_HANDLE;
|
||||
if (!e->flat3d) {
|
||||
VkTexImage *f = malloc(sizeof *f);
|
||||
if (!f) return NULL;
|
||||
*f = *img;
|
||||
VkImageCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, .imageType = VK_IMAGE_TYPE_3D,
|
||||
.format = img->format, .extent = {img->width, img->height, 1}, .mipLevels = img->levels,
|
||||
.arrayLayers = 1, .samples = VK_SAMPLE_COUNT_1_BIT, .tiling = VK_IMAGE_TILING_OPTIMAL,
|
||||
.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT,
|
||||
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED};
|
||||
VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE};
|
||||
if (vmaCreateImage(c->vma, &ci, &ai, &f->image, &e->flat3d_alloc, NULL) != VK_SUCCESS) {
|
||||
free(f);
|
||||
return NULL;
|
||||
}
|
||||
f->type = VK_IMAGE_TYPE_3D, f->view_type = VK_IMAGE_VIEW_TYPE_3D, f->depth = 1, f->layers = 1;
|
||||
f->usage = ci.usage, f->bound = 0, f->block_views = 0, f->fb_usage = 0, f->fb_learned = 0, f->gone = 0;
|
||||
e->flat3d = f;
|
||||
e->flat3d_up = ~0u;
|
||||
cb = vk_upload_begin();
|
||||
VkImageMemoryBarrier2 ib = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
|
||||
.srcStageMask = VK_PIPELINE_STAGE_2_NONE, .dstStageMask = VK_PIPELINE_STAGE_2_COPY_BIT,
|
||||
.dstAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT,
|
||||
.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED, .newLayout = VK_IMAGE_LAYOUT_GENERAL,
|
||||
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .image = f->image,
|
||||
.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, VK_REMAINING_MIP_LEVELS, 0, 1}};
|
||||
VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .imageMemoryBarrierCount = 1,
|
||||
.pImageMemoryBarriers = &ib};
|
||||
vkCmdPipelineBarrier2(cb, &di);
|
||||
log_limited(&n_flat3d, "texture %#llx (%ux%u, %u levels, format %d) is sampled as a 3D texture: a one-slice 3D "
|
||||
"copy of it is made", (unsigned long long)img->addr, img->width, img->height, img->levels,
|
||||
(int)img->format);
|
||||
}
|
||||
if (e->flat3d_up != e->n_up || e->flat3d_wr != e->wr_fence || e->flat3d_layer != layer) {
|
||||
if (!cb) cb = vk_upload_begin();
|
||||
VkImageCopy rg[16];
|
||||
for (uint32_t l = 0; l < img->levels; l++) {
|
||||
uint32_t w = img->width >> l, h = img->height >> l;
|
||||
rg[l] = (VkImageCopy){.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, l, layer, 1},
|
||||
.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, l, 0, 1},
|
||||
.extent = {w ? w : 1, h ? h : 1, 1}};
|
||||
}
|
||||
barrier(cb, ALL_RW, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_READ_BIT | VK_ACCESS_2_TRANSFER_WRITE_BIT);
|
||||
vkCmdCopyImage(cb, img->image, VK_IMAGE_LAYOUT_GENERAL, e->flat3d->image, VK_IMAGE_LAYOUT_GENERAL, img->levels, rg);
|
||||
barrier(cb, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, ALL_RW);
|
||||
e->flat3d_up = e->n_up, e->flat3d_wr = e->wr_fence, e->flat3d_layer = layer;
|
||||
}
|
||||
if (cb) vk_upload_end();
|
||||
return e->flat3d;
|
||||
}
|
||||
|
||||
// ---- origins (NX2NAVI_TRACE=perf; a run without it tests the flag and counts the drops, nothing more)
|
||||
// A texture drawn without its content leaves nothing in the log: no call fails and the image exists. What can still be
|
||||
// told afterwards is where the entry came from, so each entry keeps how it was made and how its content started
|
||||
|
||||
Reference in New Issue
Block a user