diff options
Diffstat (limited to 'runtime/src/vulkan_backend.h')
| -rw-r--r-- | runtime/src/vulkan_backend.h | 97 |
1 files changed, 76 insertions, 21 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index e3e4defe8..f4373787b 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -244,12 +244,16 @@ public: | |||
| 244 | 244 | ||
| 245 | // Adds `count` pipeline-statistics results into one D3D12_QUERY_DATA_PIPELINE_STATISTICS at dst + offset: the | 245 | // Adds `count` pipeline-statistics results into one D3D12_QUERY_DATA_PIPELINE_STATISTICS at dst + offset: the |
| 246 | // segments of a statistics query that was split around helper dispatches (D3D-162) or mesh draws (D3D-291). | 246 | // segments of a statistics query that was split around helper dispatches (D3D-162) or mesh draws (D3D-291). |
| 247 | // Segment i is in `mesh_queries` when mesh[i] is set (four counters: clipping, fragment and compute; see | 247 | // Segment i is in `mesh_queries` when mesh[i] is set (two counters: fragment and compute; see |
| 248 | // statistics_sum.comp), otherwise in `queries` (all eleven). | 248 | // statistics_sum.comp), otherwise in `queries` (all eleven). A mesh segment's clipping invocations come from |
| 249 | void sum_statistics(VkCommandBuffer cb, VkQueryPool queries, VkQueryPool mesh_queries, const std::uint8_t* mesh, | 249 | // `primitives_queries`, the mesh-primitives count of that segment (D3D-MESH-CLIPPING-COUNTER); it is null when |
| 250 | std::uint32_t first, std::uint32_t count, VkBuffer dst, VkDeviceSize offset) const { | 250 | // the device has no meshShaderQueries, and the field then reads 0. |
| 251 | void sum_statistics(VkCommandBuffer cb, VkQueryPool queries, VkQueryPool mesh_queries, | ||
| 252 | VkQueryPool primitives_queries, const std::uint8_t* mesh, std::uint32_t first, | ||
| 253 | std::uint32_t count, VkBuffer dst, VkDeviceSize offset) const { | ||
| 251 | constexpr VkDeviceSize result = statistics_counters * word_size; | 254 | constexpr VkDeviceSize result = statistics_counters * word_size; |
| 252 | constexpr VkDeviceSize mesh_slot = 5 * word_size; // CInvocations, where the mesh pool's first counter goes | 255 | constexpr VkDeviceSize mesh_slot = 7 * word_size; // PSInvocations, where the mesh pool's first counter goes |
| 256 | constexpr VkDeviceSize clipping_slot = 5 * word_size; // CInvocations, where the mesh-primitives count goes | ||
| 253 | begin_scratch_write(cb); | 257 | begin_scratch_write(cb); |
| 254 | vk_.vkCmdFillBuffer(cb, scratch_, 0, result, 0); | 258 | vk_.vkCmdFillBuffer(cb, scratch_, 0, result, 0); |
| 255 | bind(cb, statistics_pipeline_); | 259 | bind(cb, statistics_pipeline_); |
| @@ -258,9 +262,18 @@ public: | |||
| 258 | std::uint32_t chunk = 1; | 262 | std::uint32_t chunk = 1; |
| 259 | while (chunk < std::min(count, statistics_capacity) && bool(mesh && mesh[first + chunk]) == in_mesh) | 263 | while (chunk < std::min(count, statistics_capacity) && bool(mesh && mesh[first + chunk]) == in_mesh) |
| 260 | ++chunk; | 264 | ++chunk; |
| 265 | if (in_mesh) { | ||
| 266 | // The mesh pool holds only the fragment and compute counters, so the clipping field of a mesh | ||
| 267 | // segment reads 0 unless the mesh-primitives count fills it (D3D-MESH-CLIPPING-COUNTER). | ||
| 268 | vk_.vkCmdFillBuffer(cb, scratch_, result, chunk * result, 0); | ||
| 269 | barrier(cb, transfer, transfer, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT); | ||
| 270 | } | ||
| 261 | vk_.vkCmdCopyQueryPoolResults(cb, in_mesh ? mesh_queries : queries, first, chunk, scratch_, | 271 | vk_.vkCmdCopyQueryPoolResults(cb, in_mesh ? mesh_queries : queries, first, chunk, scratch_, |
| 262 | result + (in_mesh ? mesh_slot : 0), result, | 272 | result + (in_mesh ? mesh_slot : 0), result, |
| 263 | VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); | 273 | VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); |
| 274 | if (in_mesh && primitives_queries) | ||
| 275 | vk_.vkCmdCopyQueryPoolResults(cb, primitives_queries, first, chunk, scratch_, result + clipping_slot, | ||
| 276 | result, VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); | ||
| 264 | barrier(cb, transfer | compute, compute, VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_SHADER_WRITE_BIT, | 277 | barrier(cb, transfer | compute, compute, VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_SHADER_WRITE_BIT, |
| 265 | shader_access); | 278 | shader_access); |
| 266 | dispatch(cb, chunk, 1, in_mesh); | 279 | dispatch(cb, chunk, 1, in_mesh); |
| @@ -923,6 +936,10 @@ struct TileRange { | |||
| 923 | #endif | 936 | #endif |
| 924 | class VulkanContext { | 937 | class VulkanContext { |
| 925 | public: | 938 | public: |
| 939 | // mesh_queries: VK_EXT_mesh_shader's meshShaderQueries feature is enabled on `device`, so a mesh segment's | ||
| 940 | // clipping invocations can come from a mesh-primitives query (D3D-MESH-CLIPPING-COUNTER). Set by the device | ||
| 941 | // that created the context. | ||
| 942 | bool mesh_queries = false; | ||
| 926 | // conditional_rendering: VK_EXT_conditional_rendering is enabled on `device` (SetPredication). | 943 | // conditional_rendering: VK_EXT_conditional_rendering is enabled on `device` (SetPredication). |
| 927 | // ray_tracing: VK_KHR_acceleration_structure + VK_KHR_ray_query are enabled on `device`. | 944 | // ray_tracing: VK_KHR_acceleration_structure + VK_KHR_ray_query are enabled on `device`. |
| 928 | // xdxr: acceleration-structure builds write XDXR BVH4 records (xdxr_build.h) instead; needs bufferDeviceAddress. | 945 | // xdxr: acceleration-structure builds write XDXR BVH4 records (xdxr_build.h) instead; needs bufferDeviceAddress. |
| @@ -1000,7 +1017,7 @@ public: | |||
| 1000 | // binding's host pointer is null on DEFAULT and READBACK heaps, whose mapped bytes the GPU can write. | 1017 | // binding's host pointer is null on DEFAULT and READBACK heaps, whose mapped bytes the GPU can write. |
| 1001 | replay_.host_at = [this](std::uint64_t va, std::uint64_t bytes, const std::uint8_t*& host) { | 1018 | replay_.host_at = [this](std::uint64_t va, std::uint64_t bytes, const std::uint8_t*& host) { |
| 1002 | BufferVaIndex<ResourceId>::Hit hit{}; | 1019 | BufferVaIndex<ResourceId>::Hit hit{}; |
| 1003 | if (!buffer_vas_.find(va, hit)) return HostReadStatus::absent; | 1020 | if (!buffer_vas_.find(va, hit, bytes)) return HostReadStatus::absent; |
| 1004 | const ResourceBinding* b = resolve(hit.id); | 1021 | const ResourceBinding* b = resolve(hit.id); |
| 1005 | if (!b) return HostReadStatus::absent; | 1022 | if (!b) return HostReadStatus::absent; |
| 1006 | if (!b->host) return HostReadStatus::not_cpu_written; | 1023 | if (!b->host) return HostReadStatus::not_cpu_written; |
| @@ -1268,6 +1285,11 @@ public: | |||
| 1268 | replay_.zero_size = size; | 1285 | replay_.zero_size = size; |
| 1269 | } | 1286 | } |
| 1270 | 1287 | ||
| 1288 | void set_depth_range_unrestricted(bool enabled) { | ||
| 1289 | std::unique_lock lock(resource_mutex_); | ||
| 1290 | replay_.depth_range_unrestricted = enabled; | ||
| 1291 | } | ||
| 1292 | |||
| 1271 | // Physical-device COLOR_ATTACHMENT format check for reinterpreted UAV clears. | 1293 | // Physical-device COLOR_ATTACHMENT format check for reinterpreted UAV clears. |
| 1272 | void set_color_attachment_format(std::function<bool(VkFormat)> check) { | 1294 | void set_color_attachment_format(std::function<bool(VkFormat)> check) { |
| 1273 | std::unique_lock lock(resource_mutex_); | 1295 | std::unique_lock lock(resource_mutex_); |
| @@ -1483,12 +1505,23 @@ public: | |||
| 1483 | void note_bindless_upload() { bindless_upload_pending_ = true; } | 1505 | void note_bindless_upload() { bindless_upload_pending_ = true; } |
| 1484 | 1506 | ||
| 1485 | std::uint64_t completed_retire_ticket() { | 1507 | std::uint64_t completed_retire_ticket() { |
| 1486 | std::lock_guard lock(submit_mutex_); | 1508 | bool lost = false; |
| 1487 | if (is_device_lost()) return 0; | 1509 | std::uint64_t ticket = 0; |
| 1488 | return retire_tickets_.completed([this](VkSemaphore semaphore, std::uint64_t value) { | 1510 | { |
| 1489 | std::uint64_t done = 0; | 1511 | std::lock_guard lock(submit_mutex_); |
| 1490 | return vk.vkGetSemaphoreCounterValue(device_, semaphore, &done) == VK_SUCCESS && done >= value; | 1512 | if (is_device_lost()) return 0; |
| 1491 | }); | 1513 | ticket = retire_tickets_.completed([this, &lost](VkSemaphore semaphore, std::uint64_t value) { |
| 1514 | std::uint64_t done = 0; | ||
| 1515 | const VkResult result = vk.vkGetSemaphoreCounterValue(device_, semaphore, &done); | ||
| 1516 | if (result == VK_ERROR_DEVICE_LOST) lost = true; | ||
| 1517 | return result == VK_SUCCESS && done >= value; | ||
| 1518 | }); | ||
| 1519 | } | ||
| 1520 | // Outside submit_mutex_, as in submit(): notifying the fence dispatcher can run callbacks that submit. | ||
| 1521 | // A DEVICE_LOST seen here is the first Vulkan operation to discover it; it must not be mistaken for | ||
| 1522 | // "this ticket has not completed". | ||
| 1523 | if (lost) mark_device_lost(); | ||
| 1524 | return lost ? 0 : ticket; | ||
| 1492 | } | 1525 | } |
| 1493 | 1526 | ||
| 1494 | void drain_deleted_resources(std::uint64_t current_ticket) { | 1527 | void drain_deleted_resources(std::uint64_t current_ticket) { |
| @@ -2394,9 +2427,11 @@ private: | |||
| 2394 | occlusion_resolve_->normalize(cb, pool, first, count, dst, offset); | 2427 | occlusion_resolve_->normalize(cb, pool, first, count, dst, offset); |
| 2395 | }; | 2428 | }; |
| 2396 | replay.sum_statistics = [this](VkCommandBuffer cb, VkQueryPool pool, VkQueryPool mesh_pool, | 2429 | replay.sum_statistics = [this](VkCommandBuffer cb, VkQueryPool pool, VkQueryPool mesh_pool, |
| 2397 | const std::uint8_t* mesh, std::uint32_t first, std::uint32_t count, | 2430 | VkQueryPool primitives_pool, const std::uint8_t* mesh, |
| 2398 | VkBuffer dst, VkDeviceSize offset) { | 2431 | std::uint32_t first, std::uint32_t count, VkBuffer dst, |
| 2399 | occlusion_resolve_->sum_statistics(cb, pool, mesh_pool, mesh, first, count, dst, offset); | 2432 | VkDeviceSize offset) { |
| 2433 | occlusion_resolve_->sum_statistics(cb, pool, mesh_pool, primitives_pool, mesh, first, count, dst, | ||
| 2434 | offset); | ||
| 2400 | }; | 2435 | }; |
| 2401 | replay.sum_binary_occlusion = [this](VkCommandBuffer cb, VkQueryPool pool, std::uint32_t first, | 2436 | replay.sum_binary_occlusion = [this](VkCommandBuffer cb, VkQueryPool pool, std::uint32_t first, |
| 2402 | std::uint32_t count, VkBuffer dst, VkDeviceSize offset) { | 2437 | std::uint32_t count, VkBuffer dst, VkDeviceSize offset) { |
| @@ -2443,18 +2478,26 @@ private: | |||
| 2443 | if (std::find(statistics->mesh.begin(), statistics->mesh.end(), 1) == statistics->mesh.end()) { | 2478 | if (std::find(statistics->mesh.begin(), statistics->mesh.end(), 1) == statistics->mesh.end()) { |
| 2444 | statistics->mesh.clear(); | 2479 | statistics->mesh.clear(); |
| 2445 | } else { | 2480 | } else { |
| 2446 | // The counters a mesh draw allows (VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076): clipping | 2481 | // The counters a mesh draw allows (VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076): the |
| 2447 | // invocations and primitives, fragment and compute invocations (D3D-291). | 2482 | // fragment and compute invocations, and no other (D3D-291). |
| 2448 | VkQueryPoolCreateInfo info{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO}; | 2483 | VkQueryPoolCreateInfo info{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO}; |
| 2449 | info.queryType = VK_QUERY_TYPE_PIPELINE_STATISTICS; | 2484 | info.queryType = VK_QUERY_TYPE_PIPELINE_STATISTICS; |
| 2450 | info.queryCount = statistics_segments; | 2485 | info.queryCount = statistics_segments; |
| 2451 | info.pipelineStatistics = VK_QUERY_PIPELINE_STATISTIC_CLIPPING_INVOCATIONS_BIT | | 2486 | info.pipelineStatistics = kMeshStatisticsFlags; |
| 2452 | VK_QUERY_PIPELINE_STATISTIC_CLIPPING_PRIMITIVES_BIT | | ||
| 2453 | VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT | | ||
| 2454 | VK_QUERY_PIPELINE_STATISTIC_COMPUTE_SHADER_INVOCATIONS_BIT; | ||
| 2455 | if (context_.vk.vkCreateQueryPool(statistics->device, &info, nullptr, &statistics->mesh_pool) != | 2487 | if (context_.vk.vkCreateQueryPool(statistics->device, &info, nullptr, &statistics->mesh_pool) != |
| 2456 | VK_SUCCESS) | 2488 | VK_SUCCESS) |
| 2457 | return ReplayResult::unsupported; | 2489 | return ReplayResult::unsupported; |
| 2490 | // The mesh-primitives count of a mesh segment (D3D-MESH-CLIPPING-COUNTER): a query of its own type, | ||
| 2491 | // which the pipeline-statistics pool cannot hold. The device needs the meshShaderQueries feature, | ||
| 2492 | // or the clipping field of a mesh segment stays 0. | ||
| 2493 | if (context_.mesh_queries) { | ||
| 2494 | VkQueryPoolCreateInfo primitives{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO}; | ||
| 2495 | primitives.queryType = VK_QUERY_TYPE_MESH_PRIMITIVES_GENERATED_EXT; | ||
| 2496 | primitives.queryCount = statistics_segments; | ||
| 2497 | if (context_.vk.vkCreateQueryPool(statistics->device, &primitives, nullptr, | ||
| 2498 | &statistics->primitives_pool) != VK_SUCCESS) | ||
| 2499 | return ReplayResult::unsupported; | ||
| 2500 | } | ||
| 2458 | } | 2501 | } |
| 2459 | } | 2502 | } |
| 2460 | // QueryReplay::plan() reads QueryPool::ended / occlusion and QueryReplay::commit() writes them after | 2503 | // QueryReplay::plan() reads QueryPool::ended / occlusion and QueryReplay::commit() writes them after |
| @@ -2515,6 +2558,18 @@ private: | |||
| 2515 | discard(); | 2558 | discard(); |
| 2516 | return ReplayResult::unsupported; | 2559 | return ReplayResult::unsupported; |
| 2517 | } | 2560 | } |
| 2561 | // D3D12 ends every ExecuteCommandLists with all its work done and its writes visible to the next one on the | ||
| 2562 | // queue (implicit state decay relies on it), and a list may then touch the same memory without a barrier: | ||
| 2563 | // two submissions copying into one buffer, say. Vulkan orders submissions on a queue only by what their | ||
| 2564 | // barriers say, so this one starts with a full memory barrier against everything submitted before it | ||
| 2565 | // (synchronization validation: WRITE_AFTER_WRITE between vk_queue_test's back-to-back copies). | ||
| 2566 | if (context_.vk.vkCmdPipelineBarrier) { // a mocked context may leave it out | ||
| 2567 | VkMemoryBarrier full{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; | ||
| 2568 | full.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT; | ||
| 2569 | full.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT; | ||
| 2570 | context_.vk.vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, | ||
| 2571 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 1, &full, 0, nullptr, 0, nullptr); | ||
| 2572 | } | ||
| 2518 | if (!tracker_lock.owns_lock()) tracker_lock.lock(); | 2573 | if (!tracker_lock.owns_lock()) tracker_lock.lock(); |
| 2519 | if (!transfer_only && context_.bindless_prepare_ && !context_.bindless_prepare_(command_buffer)) { | 2574 | if (!transfer_only && context_.bindless_prepare_ && !context_.bindless_prepare_(command_buffer)) { |
| 2520 | const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS; | 2575 | const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS; |