Skip to content
Open
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
20 changes: 15 additions & 5 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4838,11 +4838,21 @@ class Device final
}
}

state_.context = std::make_unique<leonida::VulkanContext>(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<PFN_vkGetPhysicalDeviceProperties2>(
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<leonida::VulkanContext>(
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) {
Expand Down
4 changes: 3 additions & 1 deletion runtime/src/vulkan_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
66 changes: 52 additions & 14 deletions runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ClearRenderTarget>(command) ||
std::holds_alternative<ClearDepthStencil>(command) ||
std::holds_alternative<CopyBuffer>(command) || std::holds_alternative<CopyTexture>(command) ||
std::holds_alternative<CopyResource>(command) || std::holds_alternative<ResolveSubresource>(command) ||
std::holds_alternative<ClearUnorderedAccess>(command) || std::holds_alternative<WriteImmediate>(command) ||
std::holds_alternative<AtomicCopyBuffer>(command) || std::holds_alternative<CopyAccelerationStructure>(command) ||
std::holds_alternative<AccelerationStructurePostbuild>(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; };
Expand All @@ -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;
}
Expand All @@ -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<ResolveQueries>(command)) {
// A stream-output resolve only fills zeros (no dispatch), so it needs no split.
Expand Down Expand Up @@ -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::pair<QueryHeapId, std::uint32_t>, std::uint32_t> active;
std::uint32_t current = detail::PreparedQuery::no_segment;
std::size_t next_query = 0;
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -3381,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<Predication>(&command)) {
// Conditional rendering cannot nest, so a new predicate ends the previous one first.
if (predicate.buffer) vk.end_conditional_rendering(command_buffer);
Expand Down Expand Up @@ -3637,9 +3675,9 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
std::holds_alternative<CopyResource>(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());
Expand Down Expand Up @@ -3707,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;
Expand Down Expand Up @@ -3749,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);
Expand Down
57 changes: 57 additions & 0 deletions runtime/test/replay_query_edges_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,63 @@ 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<VkQueryPool>(14), d3d12_query_heap::pipeline_statistics, heap_count, false,
std::vector<bool>(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);
};

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");
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<std::string>{"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}) &&
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<unsigned, 2> overlap_counts{};
for (const auto& query : overlapping_split.queries) {
const auto* end = std::get_if<EndQuery>(&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() {
Expand Down
4 changes: 2 additions & 2 deletions runtime/test/replay_query_predication_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ int main() {
const bool begin = std::holds_alternative<BeginQuery>(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<std::uint8_t>({0, 1, 0, 1}),
"mixed list: vertex, mesh, vertex, then the mesh-only query's mesh segment");
Expand Down Expand Up @@ -739,7 +739,7 @@ int main() {
const bool begin = std::holds_alternative<BeginQuery>(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<std::uint8_t>({0, 1, 1, 0}),
"overlap-mesh: vertex, mesh, mesh, vertex");
Expand Down
Loading