2021-09-01 22:30:31 +01:00
|
|
|
/*
|
2025-05-28 15:59:16 +00:00
|
|
|
* Copyright (c) 2021-2025 Arm Limited.
|
2021-09-01 22:30:31 +01:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
*
|
|
|
|
|
* @brief Contains the implementation for WSI synchronization primitives.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "synchronization.hpp"
|
|
|
|
|
#include "layer/private_data.hpp"
|
2024-04-09 16:04:28 +01:00
|
|
|
#include "util/helpers.hpp"
|
2021-09-01 22:30:31 +01:00
|
|
|
|
2023-01-10 15:45:09 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
2021-09-01 22:30:31 +01:00
|
|
|
namespace wsi
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
fence_sync::fence_sync(layer::device_private_data &device, VkFence vk_fence)
|
|
|
|
|
: fence{ vk_fence }
|
|
|
|
|
, has_payload{ false }
|
|
|
|
|
, dev{ &device }
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-19 08:56:59 +00:00
|
|
|
std::optional<fence_sync> fence_sync::create(layer::device_private_data &device)
|
2021-09-01 22:30:31 +01:00
|
|
|
{
|
|
|
|
|
VkFence fence{ VK_NULL_HANDLE };
|
|
|
|
|
VkFenceCreateInfo fence_info{ VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, nullptr, 0 };
|
|
|
|
|
VkResult res =
|
|
|
|
|
device.disp.CreateFence(device.device, &fence_info, device.get_allocator().get_original_callbacks(), &fence);
|
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
|
{
|
2022-08-19 08:56:59 +00:00
|
|
|
return std::nullopt;
|
2021-09-01 22:30:31 +01:00
|
|
|
}
|
|
|
|
|
return fence_sync(device, fence);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fence_sync::fence_sync(fence_sync &&rhs)
|
|
|
|
|
{
|
|
|
|
|
*this = std::move(rhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fence_sync &fence_sync::operator=(fence_sync &&rhs)
|
|
|
|
|
{
|
|
|
|
|
std::swap(fence, rhs.fence);
|
|
|
|
|
std::swap(has_payload, rhs.has_payload);
|
|
|
|
|
std::swap(payload_finished, rhs.payload_finished);
|
|
|
|
|
std::swap(dev, rhs.dev);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fence_sync::~fence_sync()
|
|
|
|
|
{
|
|
|
|
|
if (fence != VK_NULL_HANDLE)
|
|
|
|
|
{
|
|
|
|
|
wait_payload(UINT64_MAX);
|
|
|
|
|
dev->disp.DestroyFence(dev->device, fence, dev->get_allocator().get_original_callbacks());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VkResult fence_sync::wait_payload(uint64_t timeout)
|
|
|
|
|
{
|
|
|
|
|
VkResult res = VK_SUCCESS;
|
|
|
|
|
if (has_payload && !payload_finished)
|
|
|
|
|
{
|
|
|
|
|
res = dev->disp.WaitForFences(dev->device, 1, &fence, VK_TRUE, timeout);
|
|
|
|
|
if (res == VK_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
payload_finished = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 15:18:22 +00:00
|
|
|
VkResult fence_sync::set_payload(VkQueue queue, const queue_submit_semaphores &semaphores, const void *submission_pnext)
|
2021-09-01 22:30:31 +01:00
|
|
|
{
|
|
|
|
|
VkResult result = dev->disp.ResetFences(dev->device, 1, &fence);
|
|
|
|
|
if (result != VK_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
has_payload = false;
|
|
|
|
|
|
2024-10-01 15:18:22 +00:00
|
|
|
result = sync_queue_submit(*dev, queue, fence, semaphores, submission_pnext);
|
2021-09-01 22:30:31 +01:00
|
|
|
if (result == VK_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
has_payload = true;
|
|
|
|
|
payload_finished = false;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool fence_sync::swap_payload(bool new_payload)
|
|
|
|
|
{
|
|
|
|
|
bool old_payload = has_payload;
|
|
|
|
|
has_payload = new_payload;
|
|
|
|
|
payload_finished = false;
|
|
|
|
|
return old_payload;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sync_fd_fence_sync::sync_fd_fence_sync(layer::device_private_data &device, VkFence vk_fence)
|
|
|
|
|
: fence_sync{ device, vk_fence }
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool sync_fd_fence_sync::is_supported(layer::instance_private_data &instance, VkPhysicalDevice phys_dev)
|
|
|
|
|
{
|
|
|
|
|
VkPhysicalDeviceExternalFenceInfoKHR external_fence_info = {};
|
|
|
|
|
external_fence_info.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_FENCE_INFO;
|
|
|
|
|
external_fence_info.handleType = VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
|
|
|
|
|
VkExternalFencePropertiesKHR fence_properties = {};
|
|
|
|
|
fence_properties.sType = VK_STRUCTURE_TYPE_EXTERNAL_FENCE_PROPERTIES;
|
|
|
|
|
instance.disp.GetPhysicalDeviceExternalFencePropertiesKHR(phys_dev, &external_fence_info, &fence_properties);
|
|
|
|
|
return fence_properties.externalFenceFeatures & VK_EXTERNAL_FENCE_FEATURE_EXPORTABLE_BIT_KHR;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-19 08:56:59 +00:00
|
|
|
std::optional<sync_fd_fence_sync> sync_fd_fence_sync::create(layer::device_private_data &device)
|
2021-09-01 22:30:31 +01:00
|
|
|
{
|
|
|
|
|
VkExportFenceCreateInfo export_fence_create_info = {};
|
|
|
|
|
export_fence_create_info.sType = VK_STRUCTURE_TYPE_EXPORT_FENCE_CREATE_INFO;
|
|
|
|
|
export_fence_create_info.handleTypes = VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
|
|
|
|
|
VkFenceCreateInfo fence_info = { VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, &export_fence_create_info, 0 };
|
|
|
|
|
VkFence fence = VK_NULL_HANDLE;
|
|
|
|
|
VkResult res =
|
|
|
|
|
device.disp.CreateFence(device.device, &fence_info, device.get_allocator().get_original_callbacks(), &fence);
|
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
|
{
|
2022-08-19 08:56:59 +00:00
|
|
|
return std::nullopt;
|
2021-09-01 22:30:31 +01:00
|
|
|
}
|
|
|
|
|
return sync_fd_fence_sync{ device, fence };
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-19 08:56:59 +00:00
|
|
|
std::optional<util::fd_owner> sync_fd_fence_sync::export_sync_fd()
|
2021-09-01 22:30:31 +01:00
|
|
|
{
|
|
|
|
|
int exported_fd = -1;
|
|
|
|
|
VkFenceGetFdInfoKHR fence_fd_info = {};
|
|
|
|
|
fence_fd_info.sType = VK_STRUCTURE_TYPE_FENCE_GET_FD_INFO_KHR;
|
|
|
|
|
fence_fd_info.fence = get_fence();
|
|
|
|
|
fence_fd_info.handleType = VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
|
|
|
|
|
|
|
|
|
|
VkResult result = get_device().disp.GetFenceFdKHR(get_device().device, &fence_fd_info, &exported_fd);
|
|
|
|
|
if (result == VK_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
swap_payload(false);
|
|
|
|
|
return util::fd_owner(exported_fd);
|
|
|
|
|
}
|
2022-08-19 08:56:59 +00:00
|
|
|
return std::nullopt;
|
2021-09-01 22:30:31 +01:00
|
|
|
}
|
|
|
|
|
|
2024-04-09 16:04:28 +01:00
|
|
|
VkResult sync_queue_submit(const layer::device_private_data &device, VkQueue queue, VkFence fence,
|
2024-10-01 15:18:22 +00:00
|
|
|
const queue_submit_semaphores &semaphores, const void *submission_pnext)
|
2024-04-09 16:04:28 +01:00
|
|
|
{
|
|
|
|
|
/* When the semaphore that comes in is signalled, we know that all work is done. So, we do not
|
|
|
|
|
* want to block any future Vulkan queue work on it. So, we pass in BOTTOM_OF_PIPE bit as the
|
|
|
|
|
* wait flag.
|
|
|
|
|
*/
|
|
|
|
|
VkPipelineStageFlags pipeline_stage_flag = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
|
|
|
|
|
VkPipelineStageFlags *pipeline_stage_flag_data = &pipeline_stage_flag;
|
|
|
|
|
|
|
|
|
|
util::vector<VkPipelineStageFlags> pipeline_stage_flags_vector{ util::allocator(
|
|
|
|
|
device.get_allocator(), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND) };
|
|
|
|
|
/* Try to avoid memory allocation for single semaphore */
|
|
|
|
|
if (semaphores.wait_semaphores_count > 1)
|
|
|
|
|
{
|
|
|
|
|
if (!pipeline_stage_flags_vector.try_resize(semaphores.wait_semaphores_count))
|
|
|
|
|
{
|
|
|
|
|
return VK_ERROR_OUT_OF_HOST_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
std::fill(pipeline_stage_flags_vector.begin(), pipeline_stage_flags_vector.end(),
|
|
|
|
|
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);
|
|
|
|
|
pipeline_stage_flag_data = pipeline_stage_flags_vector.data();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VkSubmitInfo submit_info = { VK_STRUCTURE_TYPE_SUBMIT_INFO,
|
2024-10-01 15:18:22 +00:00
|
|
|
submission_pnext,
|
2024-04-09 16:04:28 +01:00
|
|
|
semaphores.wait_semaphores_count,
|
|
|
|
|
semaphores.wait_semaphores,
|
|
|
|
|
pipeline_stage_flag_data,
|
|
|
|
|
0,
|
|
|
|
|
nullptr,
|
|
|
|
|
semaphores.signal_semaphores_count,
|
|
|
|
|
semaphores.signal_semaphores };
|
|
|
|
|
|
|
|
|
|
TRY(device.disp.QueueSubmit(queue, 1, &submit_info, fence));
|
|
|
|
|
return VK_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-28 15:59:16 +00:00
|
|
|
VkResult sync_queue_submit(const layer::device_private_data &device, VkQueue queue, VkFence fence,
|
|
|
|
|
const queue_submit_semaphores &semaphores, const command_buffer_data &command_buffer_data)
|
|
|
|
|
{
|
|
|
|
|
util::vector<VkPipelineStageFlags> pipeline_stage_flags_vector{ util::allocator(
|
|
|
|
|
device.get_allocator(), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND) };
|
|
|
|
|
if (!pipeline_stage_flags_vector.try_resize(semaphores.wait_semaphores_count))
|
|
|
|
|
{
|
|
|
|
|
return VK_ERROR_OUT_OF_HOST_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
std::fill(pipeline_stage_flags_vector.begin(), pipeline_stage_flags_vector.end(),
|
|
|
|
|
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT);
|
|
|
|
|
/* When the semaphore that comes in is signalled, we know that all work is done. So, we do not
|
|
|
|
|
* want to block any future Vulkan queue work on it. So, we pass in BOTTOM_OF_PIPE bit as the
|
|
|
|
|
* wait flag.
|
|
|
|
|
*/
|
|
|
|
|
VkSubmitInfo submit_info = { VK_STRUCTURE_TYPE_SUBMIT_INFO, nullptr,
|
|
|
|
|
semaphores.wait_semaphores_count, semaphores.wait_semaphores,
|
|
|
|
|
pipeline_stage_flags_vector.data(), command_buffer_data.m_command_buffer_count,
|
|
|
|
|
command_buffer_data.m_command_buffers, semaphores.signal_semaphores_count,
|
|
|
|
|
semaphores.signal_semaphores };
|
|
|
|
|
TRY(device.disp.QueueSubmit(queue, 1, &submit_info, fence));
|
|
|
|
|
return VK_SUCCESS;
|
|
|
|
|
}
|
2021-10-04 08:44:14 +01:00
|
|
|
} /* namespace wsi */
|