From 247adfe9e42cc7aa7ff04d7d5c647ced37af2a18 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Mon, 23 Nov 2026 02:04:33 -0300 Subject: [PATCH 1/4] Fix ANV pipeline statistics for transfers --- runtime/shims/d3d12_vulkan_device.cpp | 20 +++++++--- runtime/src/vulkan_backend.h | 4 +- runtime/src/vulkan_replay.h | 33 +++++++++++++---- runtime/test/replay_query_edges_test.cpp | 47 ++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 14 deletions(-) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index c7e3d99942..e9c725d6ae 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -4838,11 +4838,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 e659a762dc..5f1d10ff47 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h @@ -918,9 +918,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 1e0e598c7f..0e64ed1d79 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -804,6 +804,9 @@ struct ReplayFunctions { // Set 0 for the pipelines that declare one (their own `bindless_set` is only the marker): the device's current // generation of it, which a flush may have replaced since the pipeline was made. Null: the pipeline's. VkDescriptorSet bindless_set = VK_NULL_HANDLE; + // 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; @@ -2323,16 +2326,27 @@ inline ReplayResult plan_occlusion_segments(PreparedList& out) { return active.empty() ? ReplayResult::success : ReplayResult::invalid_query; } +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); +} + // 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. +// depend on heap state at planning time, so every non-timestamp resolve splits conservatively. ANV may implement +// transfer commands with compute shaders after a dispatch, so its opt-in path splits those out as well (D3D-494). // A mesh draw cannot run while a query with the input-assembly, vertex, geometry or tessellation counters is active // (VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076), though D3D12 allows it and reads those fields as 0. A query // that holds a mesh draw is split as well (D3D-291): a segment's first draw decides whether it runs in the full pool // or in the mesh one (statistics_mesh), and a later draw of the other kind ends it and begins one of its own kind. // Dispatches and segments without draws use the full pool; ExecuteIndirect draws count as vertex draws. Each draw // has its own rendering scope, so a split before one stays outside rendering, as a helper's does. -inline void plan_statistics_segments(PreparedList& out) { +inline void plan_statistics_segments(PreparedList& out, bool exclude_transfers) { detail::PreparedQuery* begin = nullptr; // A split point (a helper or a resolve), or a draw: kind 0 vertex, 1 mesh. struct Event { std::uint32_t* end; std::uint32_t* begin; int kind; }; @@ -2342,8 +2356,10 @@ inline void plan_statistics_segments(PreparedList& out) { const auto commands = out.list->commands(); for (std::size_t i = 0; i < out.commands.size(); ++i) { auto& p = out.commands[i]; + const Command& command = 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, -1}); splits = true; } @@ -2357,7 +2373,6 @@ inline void plan_statistics_segments(PreparedList& out) { } if (next_query == out.queries.size() || out.queries[next_query].command != i) continue; auto& query = out.queries[next_query++]; - const Command& command = 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. @@ -2409,7 +2424,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; @@ -2429,8 +2444,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 = open(); p.statistics_begin = current; @@ -3089,10 +3106,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 820dc0dc19..5715f33926 100644 --- a/runtime/test/replay_query_edges_test.cpp +++ b/runtime/test/replay_query_edges_test.cpp @@ -668,6 +668,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 a3693cd11e8dd6baad2e79c6dcc3b21b7896efa4 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Mon, 23 Nov 2026 02:38:54 -0300 Subject: [PATCH 2/4] Bracket ANV transfers with statistics segments --- runtime/src/vulkan_replay.h | 33 +++++++++++++++++++----- runtime/test/replay_query_edges_test.cpp | 10 +++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 0e64ed1d79..b298005200 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -3398,6 +3398,27 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c end_statistics(); begin_statistics(); } + struct TransferStatisticsSplit { + const PreparedList& prepared; + const ReplayFunctions& vk; + VkCommandBuffer command_buffer; + std::uint32_t end; + std::uint32_t begin; + bool enabled; + + TransferStatisticsSplit(const PreparedList& list, const ReplayFunctions& functions, VkCommandBuffer cb, + std::uint32_t query_end, std::uint32_t query_begin, bool split) + : prepared(list), vk(functions), command_buffer(cb), end(query_end), begin(query_begin), enabled(split) { + if (enabled) detail::end_statistics_segment(prepared, end, command_buffer, vk); + } + ~TransferStatisticsSplit() { + if (enabled) detail::begin_statistics_segment(prepared, begin, command_buffer, vk); + } + } transfer_statistics_split( + prepared, vk, command_buffer, 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); @@ -3654,9 +3675,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()); @@ -3724,9 +3745,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; @@ -3766,9 +3787,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 5715f33926..bde1377fb9 100644 --- a/runtime/test/replay_query_edges_test.cpp +++ b/runtime/test/replay_query_edges_test.cpp @@ -673,6 +673,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); @@ -695,6 +696,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}) && From 51a690a378c1717495c8d9d9b0f9da948f956fd2 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Mon, 23 Nov 2026 11:34:11 -0300 Subject: [PATCH 3/4] Test ANV statistics workaround on hardware --- runtime/test/vk_predicated_copy_test.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/runtime/test/vk_predicated_copy_test.cpp b/runtime/test/vk_predicated_copy_test.cpp index e5e9ff5871..3003a36898 100644 --- a/runtime/test/vk_predicated_copy_test.cpp +++ b/runtime/test/vk_predicated_copy_test.cpp @@ -37,7 +37,13 @@ int main() { // Smaller advertised limits force both dispatch and descriptor-window splits on ordinary GPUs. limits.maxStorageBufferRange=static_cast(std::max(256,limits.minStorageBufferOffsetAlignment)); limits.maxComputeWorkGroupCount[0]=1; - VulkanContext context(d.device_proc,d.device,true,true); + VkPhysicalDeviceDriverProperties driver{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES}; + VkPhysicalDeviceProperties2 driver_properties{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &driver}; + d.instance_fn("vkGetPhysicalDeviceProperties2")(d.physical, &driver_properties); + const bool exclude_transfers_from_pipeline_statistics = + driver.driverID == VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA; + VulkanContext context(d.device_proc, d.device, true, true, false, false, + exclude_transfers_from_pipeline_statistics); context.set_occlusion_resolve_memory(memory); context.configure_predication(d.device_proc,memory,properties.limits.maxStorageBufferRange,d.family); VulkanQueue queue(context,d.queue,d.family); From 40ff4bc3fcd7edfad5c58234f59e46ec3ac2e625 Mon Sep 17 00:00:00 2001 From: gut0to Date: Mon, 23 Nov 2026 13:40:55 -0300 Subject: [PATCH 4/4] Fix query predication planner calls --- runtime/test/replay_query_predication_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/test/replay_query_predication_test.cpp b/runtime/test/replay_query_predication_test.cpp index 3ef0b484ab..77966e1438 100644 --- a/runtime/test/replay_query_predication_test.cpp +++ b/runtime/test/replay_query_predication_test.cpp @@ -661,7 +661,7 @@ int main() { const bool begin = std::holds_alternative(list.commands()[i]); prepared.queries.push_back({i, &statistics_heap, begin}); // as prepare() records statistics_active } - plan_statistics_segments(prepared); + plan_statistics_segments(prepared, false); expect(prepared.statistics_segment_count == 4, "mixed list: three segments and one for the mesh-only query"); expect(prepared.statistics_mesh == std::vector({0, 1, 0, 1}), "mixed list: vertex, mesh, vertex, then the mesh-only query's mesh segment"); @@ -739,7 +739,7 @@ int main() { const bool begin = std::holds_alternative(list.commands()[i]); prepared.queries.push_back({i, &statistics_heap, begin}); } - expect(plan_overlapping_statistics_segments(prepared) == ReplayResult::success, "plan overlap-mesh list"); + expect(plan_overlapping_statistics_segments(prepared, false) == ReplayResult::success, "plan overlap-mesh list"); expect(prepared.statistics_segment_count == 4, "overlap-mesh: four segments"); expect(prepared.statistics_mesh == std::vector({0, 1, 1, 0}), "overlap-mesh: vertex, mesh, mesh, vertex");