mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-19 04:15:57 +00:00
Merge task/cmpfmt: depth twins for compare-sampled colour textures in compute passes
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
332dfce18e
1 file changed
+182
-2
+182
-2
@@ -366,10 +366,14 @@ static void dummies_init(void) {
|
||||
Z.ready = 1;
|
||||
}
|
||||
|
||||
// Releases the stand-ins. The device must be idle; the game path leaves them to process exit like the pipelines.
|
||||
static void twins_release(VkCtx *c);
|
||||
|
||||
// Releases the stand-ins and the depth twins. The device must be idle; the game path leaves them to process exit like
|
||||
// the pipelines.
|
||||
void vk_exec_draw_shutdown(void) {
|
||||
if (!Z.ready) return;
|
||||
VkCtx *c = vk_ctx();
|
||||
twins_release(c);
|
||||
for (int i = 0; i < DV_COUNT; i++) {
|
||||
if (Z.img[i].sview != Z.img[i].view) vkDestroyImageView(c->device, Z.img[i].sview, NULL);
|
||||
vkDestroyImageView(c->device, Z.img[i].view, NULL);
|
||||
@@ -1146,7 +1150,8 @@ static struct {
|
||||
VkDescriptorImageInfo out;
|
||||
} TD[TD_N];
|
||||
|
||||
static void tex_info(VkExec *ex, const GfxShaderRes *r, uint64_t h, int storage, int separate, VkDescriptorImageInfo *out) {
|
||||
static void tex_info_lookup(VkExec *ex, const GfxShaderRes *r, uint64_t h, int storage, int separate,
|
||||
VkDescriptorImageInfo *out) {
|
||||
int td_on = td_mode();
|
||||
VkImage image;
|
||||
if (!h || storage || !td_on) return tex_info_full(ex, r, h, storage, separate, out, &image);
|
||||
@@ -1207,6 +1212,177 @@ static void tex_info_full(VkExec *ex, const GfxShaderRes *r, uint64_t h, int sto
|
||||
*out = (VkDescriptorImageInfo){separate || storage ? VK_NULL_HANDLE : samp, view, layout};
|
||||
}
|
||||
|
||||
// ---- depth twins: colour images under a compare sampler in a compute shader
|
||||
// TotK's compute passes sample an R32_SFLOAT texture through a shadow sampler. Maxwell compares any single-channel
|
||||
// float or unorm texel; Vulkan only formats with SAMPLED_IMAGE_DEPTH_COMPARISON (VUID-vkCmdDispatch-None-06479), which
|
||||
// AMD's drivers do not give R32_SFLOAT, so the result there is undefined. Such an image gets a depth twin of the same
|
||||
// size and bits (R32_SFLOAT -> D32_SFLOAT, R16_UNORM -> D16_UNORM), copied through a buffer right before each dispatch
|
||||
// that samples it (a dispatch is outside any render pass), and the dispatch samples the twin. Draws keep the colour
|
||||
// image: a copy cannot go inside their pass. Design: Savage Wind Recomp runtime/nvn/vulkan/exec.c depth_twin_update
|
||||
// (https://github.com/Revivedx/SavageWindRecomp, MIT); the code is ours. TOTK_DEPTH_TWIN=0: off.
|
||||
#define TWINS 8
|
||||
typedef struct { VkImage src, img; VmaAllocation mem; VkImageView view[2]; uint32_t w, h, layers; VkFormat fmt; } Twin;
|
||||
static Twin twins[TWINS];
|
||||
static unsigned twin_next;
|
||||
static int twin_dispatch; // op_dispatch*: the resources being bound are a dispatch's
|
||||
static VkBuffer twin_buf;
|
||||
static VmaAllocation twin_buf_mem;
|
||||
static VkDeviceSize twin_buf_cap;
|
||||
|
||||
static void twin_free(void *arg) { // completion thread, after the last submission that may sample it
|
||||
Twin *t = arg;
|
||||
VkCtx *c = vk_ctx();
|
||||
for (int k = 0; k < 2; k++)
|
||||
if (t->view[k]) vkDestroyImageView(c->device, t->view[k], NULL);
|
||||
vmaDestroyImage(c->vma, t->img, t->mem);
|
||||
free(t);
|
||||
}
|
||||
|
||||
typedef struct { VkBuffer buf; VmaAllocation mem; } TwinBuf;
|
||||
static void twin_buf_free(void *arg) { // completion thread, after the last submission that may copy through it
|
||||
TwinBuf *b = arg;
|
||||
vmaDestroyBuffer(vk_ctx()->vma, b->buf, b->mem);
|
||||
free(b);
|
||||
}
|
||||
|
||||
static VkFormat twin_format(const VkTexImage *img) {
|
||||
static int on = -1;
|
||||
if (on < 0) on = rt_knob("DEPTH_TWIN", "1");
|
||||
if (!on || (img->aspect & VK_IMAGE_ASPECT_DEPTH_BIT) || img->samples > 1 || img->depth > 1) return VK_FORMAT_UNDEFINED;
|
||||
VkFormat df = img->format == VK_FORMAT_R32_SFLOAT ? VK_FORMAT_D32_SFLOAT
|
||||
: img->format == VK_FORMAT_R16_UNORM ? VK_FORMAT_D16_UNORM : VK_FORMAT_UNDEFINED;
|
||||
if (!df) return df;
|
||||
static int compares[2] = {-1, -1}; // per source format: the driver compares it as it is
|
||||
int i = df == VK_FORMAT_D16_UNORM;
|
||||
if (compares[i] < 0) {
|
||||
VkFormatProperties3 p3 = {.sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_3};
|
||||
VkFormatProperties2 p2 = {.sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2, .pNext = &p3};
|
||||
vkGetPhysicalDeviceFormatProperties2(vk_ctx()->phys, img->format, &p2);
|
||||
compares[i] = (p3.optimalTilingFeatures & VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_DEPTH_COMPARISON_BIT) != 0;
|
||||
rt_log("vk_exec: %s under a compare sampler: %s", i ? "R16_UNORM" : "R32_SFLOAT",
|
||||
compares[i] ? "compared by the driver" : "sampled through a depth twin in compute passes");
|
||||
}
|
||||
return compares[i] ? VK_FORMAT_UNDEFINED : df;
|
||||
}
|
||||
|
||||
static void twins_release(VkCtx *c) {
|
||||
for (int i = 0; i < TWINS; i++) {
|
||||
for (int k = 0; k < 2; k++)
|
||||
if (twins[i].view[k]) vkDestroyImageView(c->device, twins[i].view[k], NULL);
|
||||
if (twins[i].img) vmaDestroyImage(c->vma, twins[i].img, twins[i].mem);
|
||||
}
|
||||
memset(twins, 0, sizeof twins);
|
||||
if (twin_buf) vmaDestroyBuffer(c->vma, twin_buf, twin_buf_mem);
|
||||
twin_buf = VK_NULL_HANDLE, twin_buf_mem = NULL, twin_buf_cap = 0;
|
||||
}
|
||||
|
||||
// The twin of img holding img's current texels, the copies recorded into ex->cb; NULL when img needs none or it cannot
|
||||
// be made.
|
||||
static Twin *twin_update(VkExec *ex, const VkTexImage *img) {
|
||||
VkFormat df = twin_format(img);
|
||||
if (!df) return NULL;
|
||||
VkCtx *c = vk_ctx();
|
||||
uint32_t bpp = df == VK_FORMAT_D32_SFLOAT ? 4 : 2, layers = img->layers ? img->layers : 1;
|
||||
VkDeviceSize bytes = (VkDeviceSize)img->width * img->height * layers * bpp;
|
||||
if (bytes > (64u << 20)) return NULL;
|
||||
if (bytes > twin_buf_cap) { // one copy buffer, grown; the old one goes after the submission being recorded
|
||||
VkDeviceSize want = twin_buf_cap ? twin_buf_cap * 2 : 1u << 16;
|
||||
while (want < bytes) want *= 2;
|
||||
VkBufferCreateInfo bi = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = want,
|
||||
.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT};
|
||||
VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE};
|
||||
VkBuffer nb;
|
||||
VmaAllocation nm;
|
||||
if (vmaCreateBuffer(c->vma, &bi, &ai, &nb, &nm, NULL) != VK_SUCCESS) return NULL;
|
||||
if (twin_buf) {
|
||||
TwinBuf *old = malloc(sizeof *old);
|
||||
if (!old) rt_fatal("vk: out of memory (depth twin)");
|
||||
*old = (TwinBuf){twin_buf, twin_buf_mem};
|
||||
vk_completion_add(vk_sync_issued() + 1, twin_buf_free, old);
|
||||
}
|
||||
twin_buf = nb, twin_buf_mem = nm, twin_buf_cap = want;
|
||||
}
|
||||
Twin *t = NULL;
|
||||
for (int i = 0; i < TWINS && !t; i++)
|
||||
if (twins[i].img && twins[i].src == img->image && twins[i].w == img->width && twins[i].h == img->height &&
|
||||
twins[i].layers == layers && twins[i].fmt == df)
|
||||
t = &twins[i];
|
||||
if (!t) {
|
||||
t = &twins[twin_next++ % TWINS];
|
||||
if (t->img) { // the submission being recorded may still sample the replaced twin
|
||||
Twin *old = malloc(sizeof *old);
|
||||
if (!old) rt_fatal("vk: out of memory (depth twin)");
|
||||
*old = *t;
|
||||
vk_completion_add(vk_sync_issued() + 1, twin_free, old);
|
||||
}
|
||||
*t = (Twin){.src = img->image, .w = img->width, .h = img->height, .layers = layers, .fmt = df};
|
||||
VkImageCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, .imageType = VK_IMAGE_TYPE_2D, .format = df,
|
||||
.extent = {img->width, img->height, 1}, .mipLevels = 1, .arrayLayers = layers,
|
||||
.samples = VK_SAMPLE_COUNT_1_BIT, .tiling = VK_IMAGE_TILING_OPTIMAL,
|
||||
.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT};
|
||||
VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE};
|
||||
if (vmaCreateImage(c->vma, &ci, &ai, &t->img, &t->mem, NULL) != VK_SUCCESS) {
|
||||
*t = (Twin){0};
|
||||
return NULL;
|
||||
}
|
||||
for (int k = 0; k < 2; k++) {
|
||||
VkImageViewCreateInfo vi = {.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .image = t->img,
|
||||
.viewType = k ? VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_2D, .format = df,
|
||||
.subresourceRange = {VK_IMAGE_ASPECT_DEPTH_BIT, 0, 1, 0, k ? layers : 1}};
|
||||
if (vkCreateImageView(c->device, &vi, NULL, &t->view[k]) != VK_SUCCESS) t->view[k] = VK_NULL_HANDLE;
|
||||
}
|
||||
static int nlog;
|
||||
if (nlog++ < 8)
|
||||
rt_log("vk_exec: depth twin %ux%ux%u %s for a compute pass's compare sampler", img->width, img->height,
|
||||
layers, df == VK_FORMAT_D32_SFLOAT ? "D32_SFLOAT" : "D16_UNORM");
|
||||
}
|
||||
// The source's earlier writes and the twin's earlier readers before the copies; the twin's old texels are dropped.
|
||||
VkMemoryBarrier2 m0 = {.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 | VK_ACCESS_2_TRANSFER_WRITE_BIT};
|
||||
VkImageMemoryBarrier2 ib = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
|
||||
.srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
|
||||
.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 = t->img, .subresourceRange = {VK_IMAGE_ASPECT_DEPTH_BIT, 0, 1, 0, layers}};
|
||||
VkDependencyInfo d0 = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &m0,
|
||||
.imageMemoryBarrierCount = 1, .pImageMemoryBarriers = &ib};
|
||||
vkCmdPipelineBarrier2(ex->cb, &d0);
|
||||
VkBufferImageCopy src = {.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, layers},
|
||||
.imageExtent = {img->width, img->height, 1}};
|
||||
vkCmdCopyImageToBuffer(ex->cb, img->image, VK_IMAGE_LAYOUT_GENERAL, twin_buf, 1, &src);
|
||||
VkMemoryBarrier2 m1 = {.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_COPY_BIT,
|
||||
.dstAccessMask = VK_ACCESS_2_TRANSFER_READ_BIT};
|
||||
VkDependencyInfo d1 = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &m1};
|
||||
vkCmdPipelineBarrier2(ex->cb, &d1);
|
||||
VkBufferImageCopy dst = {.imageSubresource = {VK_IMAGE_ASPECT_DEPTH_BIT, 0, 0, layers},
|
||||
.imageExtent = {img->width, img->height, 1}};
|
||||
vkCmdCopyBufferToImage(ex->cb, twin_buf, t->img, VK_IMAGE_LAYOUT_GENERAL, 1, &dst);
|
||||
// The twin's texels before the dispatch; the source's next writers and the buffer's next copy after these copies.
|
||||
VkMemoryBarrier2 m2 = {.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_ALL_COMMANDS_BIT,
|
||||
.dstAccessMask = VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT};
|
||||
VkDependencyInfo d2 = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &m2};
|
||||
vkCmdPipelineBarrier2(ex->cb, &d2);
|
||||
ex->recorded += 5;
|
||||
return t;
|
||||
}
|
||||
|
||||
// The image descriptor of slot h; in a dispatch, a compare-sampled colour image the driver cannot compare is swapped
|
||||
// for its depth twin.
|
||||
static void tex_info(VkExec *ex, const GfxShaderRes *r, uint64_t h, int storage, int separate, VkDescriptorImageInfo *out) {
|
||||
tex_info_lookup(ex, r, h, storage, separate, out);
|
||||
if (!twin_dispatch || storage || separate || !h || !(r->type & 0x400)) return;
|
||||
VkImageViewType vt = res_view_type(r->type);
|
||||
if (vt != VK_IMAGE_VIEW_TYPE_2D && vt != VK_IMAGE_VIEW_TYPE_2D_ARRAY) return;
|
||||
const VkTexImage *img = vk_texture_by_id_locked((int)(h & 0xfffff), NULL, NULL);
|
||||
Twin *t = img ? twin_update(ex, img) : NULL;
|
||||
VkImageView v = t ? t->view[vt == VK_IMAGE_VIEW_TYPE_2D_ARRAY] : VK_NULL_HANDLE;
|
||||
if (v) out->imageView = v, out->imageLayout = VK_IMAGE_LAYOUT_GENERAL;
|
||||
}
|
||||
|
||||
static VkDescriptorSet alloc_set(VkExec *ex, VkDescriptorSetLayout layout) {
|
||||
CbRes *r = cb_res(ex);
|
||||
VkCtx *c = vk_ctx();
|
||||
@@ -2076,7 +2252,9 @@ static void op_dispatch(VkExec *ex, const p_dispatch *d) {
|
||||
vk_exec_barrier(ex); // ends the pass; earlier writes are visible to the shader
|
||||
bind_pipeline(ex, VK_PIPELINE_BIND_POINT_COMPUTE, p);
|
||||
c0_dim[0] = (uint32_t)d->x, c0_dim[1] = (uint32_t)d->y, c0_dim[2] = (uint32_t)d->z;
|
||||
twin_dispatch = 1;
|
||||
bind_resources(ex, info);
|
||||
twin_dispatch = 0;
|
||||
vkCmdDispatch(ex->cb, (uint32_t)d->x, (uint32_t)d->y, (uint32_t)d->z);
|
||||
ex->recorded++;
|
||||
if (vk_dump_armed) vk_dump_dispatch(ex, cs, p, info, d);
|
||||
@@ -2102,7 +2280,9 @@ static void op_dispatch_ind(VkExec *ex, const p_dispatch_ind *d) {
|
||||
bind_pipeline(ex, VK_PIPELINE_BIND_POINT_COMPUTE, p);
|
||||
c0_dim[0] = c0_dim[1] = c0_dim[2] = 0;
|
||||
B.c0_valid[2] = 0; // a fresh c0 range: the copy below must not change one an earlier dispatch reads
|
||||
twin_dispatch = 1;
|
||||
bind_resources(ex, info);
|
||||
twin_dispatch = 0;
|
||||
if (B.c0_valid[2]) { // the shader binds c0
|
||||
VkBufferCopy cp = {ip.offset, B.c0_off[2] + 0x200, sizeof(VkDispatchIndirectCommand)};
|
||||
vkCmdCopyBuffer(ex->cb, ip.buffer, B.c0_buf[2], 1, &cp);
|
||||
|
||||
Reference in new issue
Block a user