upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/vulkan_replay.h61
1 files changed, 61 insertions, 0 deletions
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h
index addd01795..8ece08d1e 100644
--- a/runtime/src/vulkan_replay.h
+++ b/runtime/src/vulkan_replay.h
@@ -1674,6 +1674,43 @@ inline ReplayResult prepare_resolve(const ResolveSubresource& resolve, Prepared&
1674 return ReplayResult::success; 1674 return ReplayResult::success;
1675} 1675}
1676 1676
1677// A depth or stencil resolve of a rectangle runs in a rendering scope whose render area is the rectangle, and Vulkan
1678// resolves only inside it. MoltenVK resolves the whole destination subresource instead: the resolve is Metal's
1679// store action, which has no render area (measured on MoltenVK 1.4.2 and 1.4.3, docs/d3d12_vulkan.md). No Vulkan
1680// property tells, so on every GPU the texels around the rectangle go into `staged` before the resolve and come back
1681// after it, in up to four bands (`staged_reads`, one region per band, used in both directions).
1682inline ReplayResult keep_around_resolve(const ReplayFunctions& vk, Prepared& p) {
1683 const VkExtent2D to = mip_extent(p.binding, p.range.baseMipLevel);
1684 const auto x0 = std::uint32_t(p.resolve.dstOffset.x), y0 = std::uint32_t(p.resolve.dstOffset.y);
1685 const std::uint32_t x1 = x0 + p.resolve.extent.width, y1 = y0 + p.resolve.extent.height;
1686 const VkRect2D bands[4] = {
1687 {{0, 0}, {to.width, y0}}, // above
1688 {{0, std::int32_t(y1)}, {to.width, to.height - y1}}, // below
1689 {{0, std::int32_t(y0)}, {x0, y1 - y0}}, // left
1690 {{std::int32_t(x1), std::int32_t(y0)}, {to.width - x1, y1 - y0}}, // right
1691 };
1692 // Depth aspects want 4-byte buffer offsets; 16 covers every texel size here.
1693 const std::uint32_t texel = block_format(p.binding.format, p.range.aspectMask).bytes;
1694 VkDeviceSize total = 0;
1695 for (const VkRect2D& band : bands) {
1696 if (!band.extent.width || !band.extent.height) continue;
1697 VkBufferImageCopy region{};
1698 region.bufferOffset = total;
1699 region.imageSubresource = p.resolve.dstSubresource;
1700 region.imageOffset = {band.offset.x, band.offset.y, 0};
1701 region.imageExtent = {band.extent.width, band.extent.height, 1};
1702 p.staged_reads.push_back(region);
1703 total += (VkDeviceSize(band.extent.width) * band.extent.height * texel + 15) / 16 * 16;
1704 }
1705 if (p.staged_reads.empty()) return ReplayResult::success; // the rectangle is the whole subresource
1706 VkDeviceSize base = 0;
1707 if (!texel || !vk.copy_image_to_buffer || !vk.copy_buffer_to_image || !vk.copy_scratch ||
1708 !vk.copy_scratch(total, p.staged, base))
1709 return ReplayResult::unsupported;
1710 for (VkBufferImageCopy& region : p.staged_reads) region.bufferOffset += base;
1711 return ReplayResult::success;
1712}
1713
1677// The layout a copy uses for a range: D3D12 requires copy_dest/copy_source, or common (GENERAL), where an implicit 1714// The layout a copy uses for a range: D3D12 requires copy_dest/copy_source, or common (GENERAL), where an implicit
1678// promotion lets the copy run in place. Anything else is moved to `wanted` and tracked. 1715// promotion lets the copy run in place. Anything else is moved to `wanted` and tracked.
1679inline VkImageLayout copy_layout(const ReplayFunctions& vk, VkCommandBuffer command_buffer, 1716inline VkImageLayout copy_layout(const ReplayFunctions& vk, VkCommandBuffer command_buffer,
@@ -2343,6 +2380,8 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun
2343 p.view = vk.image_view(p.source, p.source_range); 2380 p.view = vk.image_view(p.source, p.source_range);
2344 p.resolve_view = vk.image_view(p.binding, p.range); 2381 p.resolve_view = vk.image_view(p.binding, p.range);
2345 if (!p.view || !p.resolve_view) return ReplayResult::unsupported; 2382 if (!p.view || !p.resolve_view) return ReplayResult::unsupported;
2383 if (const auto result = detail::keep_around_resolve(vk, p); result != ReplayResult::success)
2384 return result;
2346 } 2385 }
2347 } else if (const auto* discard = std::get_if<DiscardResource>(&command)) { 2386 } else if (const auto* discard = std::get_if<DiscardResource>(&command)) {
2348 // Buffers and partial (rectangle) discards keep their contents, which "undefined" allows. 2387 // Buffers and partial (rectangle) discards keep their contents, which "undefined" allows.
@@ -3137,6 +3176,22 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
3137 auto dest_pieces = detail::current( 3176 auto dest_pieces = detail::current(
3138 tracker, dst_id, detail::planes_for_layout(p.binding, p.range, separate), dest_layout); 3177 tracker, dst_id, detail::planes_for_layout(p.binding, p.range, separate), dest_layout);
3139 detail::initialize(vk, command_buffer, p.binding, dst_id, dest_pieces, dest_layout, tracker); 3178 detail::initialize(vk, command_buffer, p.binding, dst_id, dest_pieces, dest_layout, tracker);
3179 // The texels around the rectangle, kept in `staged` across the resolve (keep_around_resolve).
3180 const auto keep = [&](VkImageLayout wanted, VkAccessFlags access, auto&& copy) {
3181 const VkImageLayout layout = image_layout(p.binding, wanted);
3182 detail::round_trip(vk, command_buffer, p.binding, dest_pieces, layout, dest_state, access,
3183 VK_PIPELINE_STAGE_TRANSFER_BIT, [&] { copy(layout); });
3184 };
3185 const auto bands = std::uint32_t(p.staged_reads.size());
3186 if (p.staged) {
3187 keep(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_ACCESS_TRANSFER_READ_BIT, [&](VkImageLayout layout) {
3188 vk.copy_image_to_buffer(command_buffer, p.binding.image, layout, p.staged, bands,
3189 p.staged_reads.data());
3190 });
3191 detail::memory_barrier(vk, command_buffer, VK_ACCESS_TRANSFER_WRITE_BIT,
3192 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT,
3193 VK_PIPELINE_STAGE_TRANSFER_BIT);
3194 }
3140 const VkImageLayout source_attachment = 3195 const VkImageLayout source_attachment =
3141 image_layout(p.source, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); 3196 image_layout(p.source, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
3142 const VkImageLayout dest_attachment = 3197 const VkImageLayout dest_attachment =
@@ -3164,6 +3219,12 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
3164 vk.end_rendering(command_buffer); 3219 vk.end_rendering(command_buffer);
3165 }); 3220 });
3166 }); 3221 });
3222 if (p.staged) {
3223 keep(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT, [&](VkImageLayout layout) {
3224 vk.copy_buffer_to_image(command_buffer, p.staged, p.binding.image, layout, bands,
3225 p.staged_reads.data());
3226 });
3227 }
3167 continue; 3228 continue;
3168 } 3229 }
3169 const VkImageLayout src_layout = detail::copy_layout( 3230 const VkImageLayout src_layout = detail::copy_layout(