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 @@ -4290,11 +4290,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 @@ -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;
Expand Down
71 changes: 58 additions & 13 deletions runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<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);
}

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<Split> 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<ResolveQueries>(command)) {
// A stream-output resolve only fills zeros (no dispatch), so it needs no split.
Expand Down Expand Up @@ -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::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 @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -3124,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<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 @@ -3373,9 +3418,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 @@ -3437,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;
Expand Down Expand Up @@ -3471,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);
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 @@ -666,6 +666,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