From 0555b563ba909e11f1cf02372318d5dc5ccff2dd Mon Sep 17 00:00:00 2001 From: Gustavo Date: Mon, 23 Nov 2026 02:04:33 -0300 Subject: [PATCH 1/2] Fix ANV pipeline statistics for transfers --- runtime/shims/d3d12_vulkan_device.cpp | 20 +++++++--- runtime/src/vulkan_backend.h | 4 +- runtime/src/vulkan_replay.h | 30 +++++++++++---- runtime/test/replay_query_edges_test.cpp | 47 ++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 13 deletions(-) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index ded17e79d2..d9748f3e1f 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -4290,11 +4290,21 @@ class Device final } } - state_.context = std::make_unique(get_device, info.device, - info.separate_depth_stencil_layouts, - info.conditional_rendering, info.ray_tracing, - info.xdxr_acceleration_structures && - info.buffer_device_address); + bool anv_counts_transfer_in_pipeline_statistics = false; + if (info.physical && info.get_instance_proc) { + const auto get_props2 = reinterpret_cast( + info.get_instance_proc(info.instance, "vkGetPhysicalDeviceProperties2")); + if (get_props2) { + VkPhysicalDeviceDriverProperties driver{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES}; + VkPhysicalDeviceProperties2 properties{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &driver}; + get_props2(info.physical, &properties); + anv_counts_transfer_in_pipeline_statistics = driver.driverID == VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA; + } + } + state_.context = std::make_unique( + get_device, info.device, info.separate_depth_stencil_layouts, info.conditional_rendering, + info.ray_tracing, info.xdxr_acceleration_structures && info.buffer_device_address, + anv_counts_transfer_in_pipeline_statistics); if (info.physical && state_.format_properties) { state_.context->set_color_attachment_format( [physical = info.physical, get_props = state_.format_properties](VkFormat format) { diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index d247b8cb01..d84841c25e 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h @@ -809,9 +809,11 @@ class VulkanContext { // ray_tracing: VK_KHR_acceleration_structure + VK_KHR_ray_query are enabled on `device`. // xdxr: acceleration-structure builds write XDXR BVH4 records (xdxr_build.h) instead; needs bufferDeviceAddress. VulkanContext(PFN_vkGetDeviceProcAddr get, VkDevice device, bool separate_depth_stencil_layouts, - bool conditional_rendering = false, bool ray_tracing = false, bool xdxr = false) + bool conditional_rendering = false, bool ray_tracing = false, bool xdxr = false, + bool exclude_transfers_from_pipeline_statistics = false) : device_(device), tracker_(separate_depth_stencil_layouts), ray_tracing_(ray_tracing) { loaded_ = vk.load(get, device); + replay_.exclude_transfers_from_pipeline_statistics = exclude_transfers_from_pipeline_statistics; replay_.pipeline_barrier = vk.vkCmdPipelineBarrier; replay_.clear_color_image = vk.vkCmdClearColorImage; replay_.clear_depth_stencil_image = vk.vkCmdClearDepthStencilImage; diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index d5effb8308..76128493c8 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -772,6 +772,9 @@ struct ShaderUserData { static_assert(sizeof(ShaderUserData) == 144); struct ReplayFunctions { + // ANV may implement transfer commands with compute shaders after a dispatch; keep those internal invocations + // outside D3D12 pipeline-statistics queries (D3D-494). + bool exclude_transfers_from_pipeline_statistics = false; PFN_vkCmdPipelineBarrier pipeline_barrier = nullptr; PFN_vkCmdClearColorImage clear_color_image = nullptr; PFN_vkCmdClearDepthStencilImage clear_depth_stencil_image = nullptr; @@ -2190,19 +2193,30 @@ inline ReplayResult plan_occlusion_segments(PreparedList& out) { // Helper dispatches must not contribute to the application's CSInvocations (D3D-162). Query resolves and // ordinary helper commands share one sequence of physical statistics segments. Resolve dispatch requirements // depend on heap state at planning time, so every non-timestamp resolve splits conservatively. -inline void plan_statistics_segments(PreparedList& out) { +inline bool statistics_transfer_command(const Command& command) { + return std::holds_alternative(command) || + std::holds_alternative(command) || + std::holds_alternative(command) || std::holds_alternative(command) || + std::holds_alternative(command) || std::holds_alternative(command) || + std::holds_alternative(command) || std::holds_alternative(command) || + std::holds_alternative(command) || std::holds_alternative(command) || + std::holds_alternative(command); +} + +inline void plan_statistics_segments(PreparedList& out, bool exclude_transfers) { detail::PreparedQuery* begin = nullptr; struct Split { std::uint32_t* end; std::uint32_t* begin; }; std::vector inner; std::size_t next_query = 0; for (std::size_t i = 0; i < out.commands.size(); ++i) { auto& p = out.commands[i]; + const Command& command = out.list->commands()[i]; if (begin && (p.predicate.scratch || !p.predicated_copy.empty() || p.has_depth24 || - p.counted_dispatch.indirect)) + p.counted_dispatch.indirect || + (exclude_transfers && statistics_transfer_command(command)))) inner.push_back({&p.statistics_end, &p.statistics_begin}); if (next_query == out.queries.size() || out.queries[next_query].command != i) continue; auto& query = out.queries[next_query++]; - const Command& command = out.list->commands()[i]; const auto range = query_range(command); if (std::holds_alternative(command)) { // A stream-output resolve only fills zeros (no dispatch), so it needs no split. @@ -2236,7 +2250,7 @@ inline void plan_statistics_segments(PreparedList& out) { // while one runs, because its helper dispatch would be counted — closes the one open physical query and opens // a new one, so each logical query keeps a contiguous slot range and its result is the sum of that range // (statistics_sum.comp). A Begin or End of another query kind leaves the segments alone. -inline ReplayResult plan_overlapping_statistics_segments(PreparedList& out) { +inline ReplayResult plan_overlapping_statistics_segments(PreparedList& out, bool exclude_transfers) { std::map, std::uint32_t> active; std::uint32_t current = detail::PreparedQuery::no_segment; std::size_t next_query = 0; @@ -2248,8 +2262,10 @@ inline ReplayResult plan_overlapping_statistics_segments(PreparedList& out) { if (!query) { // A helper dispatch while a query runs would be counted into it (D3D-162): close the shared physical // segment before it and open a new one after, the way resolves do. + const Command& command = out.list->commands()[i]; if (!active.empty() && (p.predicate.scratch || !p.predicated_copy.empty() || p.has_depth24 || - p.counted_dispatch.indirect)) { + p.counted_dispatch.indirect || + (exclude_transfers && statistics_transfer_command(command)))) { p.statistics_end = current; current = out.statistics_segment_count++; p.statistics_begin = current; @@ -2862,10 +2878,10 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun if (ordinary_active || binary_active) return ReplayResult::invalid_query; out.statistics_overlaps = statistics_overlaps; if (statistics_overlaps) { - const auto result = plan_overlapping_statistics_segments(out); + const auto result = plan_overlapping_statistics_segments(out, vk.exclude_transfers_from_pipeline_statistics); if (result != ReplayResult::success) return result; } else { - plan_statistics_segments(out); + plan_statistics_segments(out, vk.exclude_transfers_from_pipeline_statistics); } if (overlaps) { if (!vk.sum_occlusion || out.queries.size() >= detail::PreparedQuery::no_segment) diff --git a/runtime/test/replay_query_edges_test.cpp b/runtime/test/replay_query_edges_test.cpp index 1bd55d237f..83853e2767 100644 --- a/runtime/test/replay_query_edges_test.cpp +++ b/runtime/test/replay_query_edges_test.cpp @@ -666,6 +666,53 @@ void test_query_state() { expect(c.word(dst.buffer, arena_offset + inner_statistics_offset + word_size * j) == c.word(dst.buffer, arena_offset + statistics_offset + word_size * j), "previous statistics sum inside a new scope"); } + + // ANV can count internal compute work for transfers inside pipeline-statistics queries. Keep the workaround + // opt-in, and split both ordinary and overlapping queries around a buffer copy (D3D-494). + QueryPool transfer_stat{handle(14), d3d12_query_heap::pipeline_statistics, heap_count, false, + std::vector(heap_count), {}}; + auto previous_query_pool = vk.query_pool; + vk.query_pool = [&](QueryHeapId id) -> QueryPool* { + return id == 2 ? &transfer_stat : previous_query_pool(id); + }; + + CommandList ordinary_transfer, ordinary_control; + expect(ordinary_transfer.begin_query({2, d3d12_query::pipeline_statistics, 0}) && + ordinary_transfer.copy_buffer({1, 16, 1, 0, 8}) && + ordinary_transfer.end_query({2, d3d12_query::pipeline_statistics, 0}), "record transfer in statistics"); + expect(ordinary_control.begin_query({2, d3d12_query::pipeline_statistics, 1}) && + ordinary_control.copy_buffer({1, 16, 1, 0, 8}) && + ordinary_control.end_query({2, d3d12_query::pipeline_statistics, 1}), "record control transfer"); + PreparedList transfer_control, transfer_split; + vk.exclude_transfers_from_pipeline_statistics = false; + prep(ordinary_control, transfer_control); + expect(transfer_control.statistics_segment_count == 0, + "non-ANV driver keeps ordinary transfer inside the original query"); + vk.exclude_transfers_from_pipeline_statistics = true; + prep(ordinary_transfer, transfer_split); + expect(transfer_split.statistics_segment_count == 2 && transfer_split.queries.size() == 2 && + transfer_split.queries[1].statistics_count == 2, + "ANV transfer splits ordinary statistics into two summed segments"); + + CommandList overlapping_transfer; + expect(overlapping_transfer.begin_query({2, d3d12_query::pipeline_statistics, 2}) && + overlapping_transfer.begin_query({2, d3d12_query::pipeline_statistics, 3}) && + overlapping_transfer.copy_buffer({1, 16, 1, 0, 8}) && + overlapping_transfer.end_query({2, d3d12_query::pipeline_statistics, 2}) && + overlapping_transfer.end_query({2, d3d12_query::pipeline_statistics, 3}), + "record transfer in overlapping statistics"); + PreparedList overlapping_split; + prep(overlapping_transfer, overlapping_split); + expect(overlapping_split.statistics_overlaps && overlapping_split.statistics_segment_count == 4, + "ANV transfer splits overlapping statistics segments"); + std::array overlap_counts{}; + for (const auto& query : overlapping_split.queries) { + const auto* end = std::get_if(&overlapping_transfer.commands()[query.command]); + if (end && end->type == d3d12_query::pipeline_statistics && end->index >= 2) + overlap_counts[end->index - 2] = query.statistics_count; + } + expect(overlap_counts[0] == 3 && overlap_counts[1] == 3, + "both overlapping statistics queries include both sides of the transfer"); } void test_predicated_copies() { From e84c51fbe92e0061d6fe51eb7ad437e3b856a322 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Mon, 23 Nov 2026 02:38:54 -0300 Subject: [PATCH 2/2] Bracket ANV transfers with statistics segments --- runtime/src/vulkan_replay.h | 41 ++++++++++++++++++++---- runtime/test/replay_query_edges_test.cpp | 10 ++++++ 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 76128493c8..5bb1f4d95d 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -3140,6 +3140,35 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c vk.reset_query_pool(command_buffer, prepared.statistics_pool, index, 1); vk.begin_query(command_buffer, prepared.statistics_pool, index, 0); }; + struct TransferStatisticsSplit { + const ReplayFunctions& vk; + VkCommandBuffer command_buffer; + VkQueryPool pool; + std::uint32_t base; + std::uint32_t end; + std::uint32_t begin; + bool enabled; + + TransferStatisticsSplit(const ReplayFunctions& functions, VkCommandBuffer cb, VkQueryPool query_pool, + std::uint32_t query_base, std::uint32_t query_end, std::uint32_t query_begin, + bool split) + : vk(functions), command_buffer(cb), pool(query_pool), base(query_base), end(query_end), + begin(query_begin), enabled(split) { + if (enabled && end != detail::PreparedQuery::no_segment) + vk.end_query(command_buffer, pool, base + end); + } + ~TransferStatisticsSplit() { + if (enabled && begin != detail::PreparedQuery::no_segment) { + const auto index = base + begin; + vk.reset_query_pool(command_buffer, pool, index, 1); + vk.begin_query(command_buffer, pool, index, 0); + } + } + } transfer_statistics_split( + vk, command_buffer, prepared.statistics_pool, prepared.statistics_base, p.statistics_end, + p.statistics_begin, vk.exclude_transfers_from_pipeline_statistics && statistics_transfer_command(command) && + p.statistics_end != detail::PreparedQuery::no_segment && + p.statistics_begin != detail::PreparedQuery::no_segment); if (const auto* predication = std::get_if(&command)) { // Conditional rendering cannot nest, so a new predicate ends the previous one first. if (predicate.buffer) vk.end_conditional_rendering(command_buffer); @@ -3389,9 +3418,9 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c std::holds_alternative(command)) { const auto [dst_id, src_id] = copy_ids(command); if (!p.predicated_copy.empty()) { - end_statistics(); + if (!transfer_statistics_split.enabled) end_statistics(); vk.emit_predicated_copy(command_buffer, p.predicated_copy); - begin_statistics(); + if (!transfer_statistics_split.enabled) begin_statistics(); } else if (!p.buffer_copies.empty()) { vk.copy_buffer(command_buffer, p.source.buffer, p.binding.buffer, std::uint32_t(p.buffer_copies.size()), p.buffer_copies.data()); @@ -3453,9 +3482,9 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c detail::memory_barrier(vk, command_buffer, VK_ACCESS_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT); - end_statistics(); + if (!transfer_statistics_split.enabled) end_statistics(); for (const auto& part : p.depth24_parts) run_depth24(part); - begin_statistics(); + if (!transfer_statistics_split.enabled) begin_statistics(); detail::memory_barrier(vk, command_buffer, VK_ACCESS_SHADER_WRITE_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); converted = p.buffer_image_copies; @@ -3487,9 +3516,9 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c detail::memory_barrier(vk, command_buffer, VK_ACCESS_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT); - end_statistics(); + if (!transfer_statistics_split.enabled) end_statistics(); for (const auto& part : p.depth24_parts) run_depth24(part); - begin_statistics(); + if (!transfer_statistics_split.enabled) begin_statistics(); detail::memory_barrier(vk, command_buffer, VK_ACCESS_SHADER_WRITE_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); diff --git a/runtime/test/replay_query_edges_test.cpp b/runtime/test/replay_query_edges_test.cpp index 83853e2767..f802996a6c 100644 --- a/runtime/test/replay_query_edges_test.cpp +++ b/runtime/test/replay_query_edges_test.cpp @@ -671,6 +671,7 @@ void test_query_state() { // opt-in, and split both ordinary and overlapping queries around a buffer copy (D3D-494). QueryPool transfer_stat{handle(14), d3d12_query_heap::pipeline_statistics, heap_count, false, std::vector(heap_count), {}}; + c.values[transfer_stat.pool].resize(heap_count); auto previous_query_pool = vk.query_pool; vk.query_pool = [&](QueryHeapId id) -> QueryPool* { return id == 2 ? &transfer_stat : previous_query_pool(id); @@ -693,6 +694,15 @@ void test_query_state() { expect(transfer_split.statistics_segment_count == 2 && transfer_split.queries.size() == 2 && transfer_split.queries[1].statistics_count == 2, "ANV transfer splits ordinary statistics into two summed segments"); + auto transfer_pool = make_segment_pool(15); + c.values[transfer_pool->pool].resize(2); + QueryReplay transfer_batch; + expect(transfer_batch.plan(transfer_split, {}, 0, true, transfer_pool) == ReplayResult::success, + "plan ANV transfer statistics emission"); + c.events.clear(); + emit(transfer_split, {}, vk); + expect(c.events == std::vector{"begin_query", "end_query", "native_copy", "begin_query", "end_query"}, + "emitted statistics segments bracket the transfer command"); CommandList overlapping_transfer; expect(overlapping_transfer.begin_query({2, d3d12_query::pipeline_statistics, 2}) &&