Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3985,6 +3985,13 @@ if(TARGET Vulkan::Headers)
target_compile_definitions(replay_copy_offset_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
endif()
add_test(NAME replay_copy_offset COMMAND replay_copy_offset_test)
# An indexed draw's index buffer view size bounds the index fetch: past the view reads 0 (no GPU).
add_executable(replay_index_view_test test/replay_index_view_test.cpp)
target_link_libraries(replay_index_view_test PRIVATE Vulkan::Headers)
if(WIN32)
target_compile_definitions(replay_index_view_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
endif()
add_test(NAME replay_index_view COMMAND replay_index_view_test)
# KHR CopyRaytracingAccelerationStructure (CLONE, COMPACT) and COMPACTED_SIZE postbuild (no GPU).
add_executable(replay_khr_as_copy_postbuild_test test/replay_khr_as_copy_postbuild_test.cpp)
target_link_libraries(replay_khr_as_copy_postbuild_test PRIVATE Vulkan::Headers)
Expand Down
5 changes: 4 additions & 1 deletion runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9841,7 +9841,10 @@ class Device final
[this, index_scratch](const VkMemoryRequirements& requirements) {
return state_.allocate(requirements, 0, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, nullptr, index_scratch);
},
index_scratch ? VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT : 0, state_.families);
// INDEX_BUFFER on every GPU: a draw that reads past its index view's end binds a staged copy
// (vulkan_replay.h, prepare_draw).
VK_BUFFER_USAGE_INDEX_BUFFER_BIT | (index_scratch ? VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT : 0),
state_.families);
if (info.buffer_device_address && info.physical && info.get_instance_proc) {
if (const auto get_props = reinterpret_cast<PFN_vkGetPhysicalDeviceProperties>(
info.get_instance_proc(info.instance, "vkGetPhysicalDeviceProperties"))) {
Expand Down
71 changes: 65 additions & 6 deletions runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,15 @@ struct PreparedDraw {
VkDeviceSize index_size = 0; // the bytes of the index buffer view inside its buffer; 0 with no index buffer
VkIndexType index_type = VK_INDEX_TYPE_UINT16;
QuadIndexExpansion quad_indices; // an indexed quad list: the triangle indices its draw binds instead
std::uint32_t index_first = 0; // firstIndex to draw with: the draw's, or 0 on the zero page or a staged copy
// A draw that reads past the index view's end binds a scratch copy of the indices it fetches instead: the
// in-view part copied from the buffer, the rest zero. fill_bytes 0: no copy.
struct IndexStage {
VkBuffer scratch = VK_NULL_HANDLE;
VkDeviceSize scratch_offset = 0, fill_bytes = 0;
VkBuffer source = VK_NULL_HANDLE;
VkDeviceSize source_offset = 0, copy_bytes = 0;
} index_stage;
VkExtent2D area{};
std::uint32_t layers = 1; // the fewest layers any bound attachment view has
// The draw is dropped at emit: its pipeline, a vertex buffer or its index buffer could not be resolved (see
Expand All @@ -1255,6 +1264,8 @@ struct PreparedDraw {
index_size = 0;
index_type = VK_INDEX_TYPE_UINT16;
quad_indices = {};
index_first = 0;
index_stage = {};
area = {};
layers = 1;
}
Expand Down Expand Up @@ -2511,11 +2522,15 @@ ReplayResult prepare_draw(const Draw& draw, Resolve&& resolve, const ReplayFunct
if (draw.index.format == 57) p.index_type = VK_INDEX_TYPE_UINT16;
else if (draw.index.format == 42) p.index_type = VK_INDEX_TYPE_UINT32;
else if (draw.index.va) return unsupported_at(__LINE__);
if (!draw.index.va) { // no index buffer: every index reads 0
if (!vk.zero_buffer) return unresolved_at(__LINE__);
p.index_buffer = vk.zero_buffer;
p.index_offset = vk.zero_offset;
} else {
p.index_first = draw.first;
// Index fetches outside the view read 0 in D3D12, but vkCmdBindIndexBuffer binds to the end of the buffer (the
// layer's device does not enable VK_KHR_maintenance5 for a bind size). A draw wholly inside the view binds
// the buffer. One that reads past the view's end binds the zero page when every index is outside, else a
// scratch copy of the indices it fetches (in-view part copied, the rest zero), so no byte past the view is read.
// An indexed quad list is left to its expansion pass, which reads only the view (prepare_quad_indices).
const std::uint64_t index_bytes = p.index_type == VK_INDEX_TYPE_UINT32 ? 4 : 2;
std::uint64_t view_indices = 0;
if (draw.index.va) {
std::uint64_t size = draw.index.size;
if (!vk.buffer_at(draw.index.va, size, p.index_buffer, p.index_offset)) {
size = draw.index.size;
Expand All @@ -2529,6 +2544,35 @@ ReplayResult prepare_draw(const Draw& draw, Resolve&& resolve, const ReplayFunct
}
}
p.index_size = size;
view_indices = size / index_bytes; // size is the view's, cut at the buffer's end
}
const std::uint64_t last = std::uint64_t(draw.first) + draw.count; // one past the last index fetched
if (draw.topology == topology_quad_list) {
if (!draw.index.va) { // no index buffer: every index reads 0
if (!vk.zero_buffer) return unresolved_at(__LINE__);
p.index_buffer = vk.zero_buffer;
p.index_offset = vk.zero_offset;
}
} else if (!draw.index.va || (draw.count > 0 && last > view_indices)) {
const std::uint64_t inside = draw.first < view_indices ? view_indices - draw.first : 0;
const std::uint64_t bytes = std::uint64_t(draw.count) * index_bytes;
if (inside == 0 && vk.zero_buffer && bytes <= vk.zero_size) {
p.index_buffer = vk.zero_buffer;
p.index_offset = vk.zero_offset;
p.index_first = 0;
} else {
if (!vk.copy_scratch || !vk.fill_buffer || !vk.copy_buffer) return unsupported_at(__LINE__);
const std::uint64_t fill = (bytes + 3) & ~std::uint64_t(3); // vkCmdFillBuffer: multiples of 4
VkBuffer scratch = VK_NULL_HANDLE;
VkDeviceSize offset = 0;
if (!vk.copy_scratch(fill + 4, scratch, offset)) return unsupported_at(__LINE__);
offset = (offset + 3) & ~VkDeviceSize(3); // also a multiple of the index size
p.index_stage = {scratch, offset, fill, p.index_buffer,
p.index_offset + VkDeviceSize(draw.first) * index_bytes, inside * index_bytes};
p.index_buffer = scratch;
p.index_offset = offset;
p.index_first = 0;
}
}
}
return ReplayResult::success;
Expand Down Expand Up @@ -4818,6 +4862,21 @@ inline void emit_commands(const PreparedList& prepared, VkCommandBuffer command_
const GraphicsPipeline& graphics = *d.graphics;
if (d.depth.id && (graphics.depth_aspects & VK_IMAGE_ASPECT_DEPTH_BIT)) rendering.pDepthAttachment = &depth;
if (d.depth.id && (graphics.depth_aspects & VK_IMAGE_ASPECT_STENCIL_BIT)) rendering.pStencilAttachment = &depth;
if (d.index_stage.fill_bytes) {
const auto& stage = d.index_stage;
constexpr VkAccessFlags transfer_rw = VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT;
detail::memory_barrier(vk, command_buffer, VK_ACCESS_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
transfer_rw, VK_PIPELINE_STAGE_TRANSFER_BIT);
vk.fill_buffer(command_buffer, stage.scratch, stage.scratch_offset, stage.fill_bytes, 0);
if (stage.copy_bytes) {
detail::memory_barrier(vk, command_buffer, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
transfer_rw, VK_PIPELINE_STAGE_TRANSFER_BIT);
const VkBufferCopy region{stage.source_offset, stage.scratch_offset, stage.copy_bytes};
vk.copy_buffer(command_buffer, stage.source, stage.scratch, 1, &region);
}
detail::memory_barrier(vk, command_buffer, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_ACCESS_INDEX_READ_BIT, VK_PIPELINE_STAGE_VERTEX_INPUT_BIT);
}
vk.begin_rendering(command_buffer, &rendering);

ShaderUserData data{};
Expand Down Expand Up @@ -4924,7 +4983,7 @@ inline void emit_commands(const PreparedList& prepared, VkCommandBuffer command_
}
} else if (draw->indexed) {
vk.bind_index_buffer(command_buffer, d.index_buffer, d.index_offset, d.index_type);
vk.draw_indexed(command_buffer, draw->count, draw->instances, draw->first, draw->base_vertex,
vk.draw_indexed(command_buffer, draw->count, draw->instances, d.index_first, draw->base_vertex,
draw->first_instance);
} else {
vk.draw(command_buffer, draw->count, draw->instances, draw->first, draw->first_instance);
Expand Down
Loading
Loading