Merge 'Support timestamp calibration for present stage local time domains' into 'main'

See merge request mesa/vulkan-wsi-layer!147
This commit is contained in:
Dennis Tsiang 2025-04-10 09:36:08 +00:00
commit 5097740a45
7 changed files with 169 additions and 2 deletions

View file

@ -266,6 +266,7 @@ endif()
# Layer
add_library(${PROJECT_NAME} SHARED
layer/calibrated_timestamps_api.cpp
layer/layer.cpp
layer/private_data.cpp
layer/surface_api.cpp

View file

@ -0,0 +1,112 @@
/*
* Copyright (c) 2025 Arm Limited.
*
* 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 calibrated_timestamps_api.cpp
*
* @brief Contains the Vulkan entrypoints for the calibrated timestamps extension.
*/
#include <wsi/extensions/present_timing.hpp>
#include <wsi/swapchain_base.hpp>
VWL_VKAPI_CALL(VkResult)
wsi_layer_vkGetCalibratedTimestampsEXT(VkDevice device, uint32_t timestampCount,
const VkCalibratedTimestampInfoKHR *pTimestampInfos, uint64_t *pTimestamps,
uint64_t *pMaxDeviation) VWL_API_POST
{
auto &device_data = layer::device_private_data::get(device);
auto get_calibrated_timestamps =
device_data.disp.get_fn<PFN_vkGetCalibratedTimestampsEXT>("PFN_vkGetCalibratedTimestampsEXT").has_value() ?
device_data.disp.get_fn<PFN_vkGetCalibratedTimestampsEXT>("vkGetCalibratedTimestampsEXT") :
device_data.disp.get_fn<PFN_vkGetCalibratedTimestampsEXT>("vkGetCalibratedTimestampsKHR");
assert(get_calibrated_timestamps.has_value());
#if VULKAN_WSI_LAYER_EXPERIMENTAL
struct stage_local_index_and_offset
{
uint32_t index;
uint64_t calibration_offset;
};
util::vector<VkCalibratedTimestampInfoKHR> time_stamp_info{ util::allocator(device_data.get_allocator(),
VK_SYSTEM_ALLOCATION_SCOPE_OBJECT) };
util::vector<stage_local_index_and_offset> calibration_index_and_offset{ util::allocator(
device_data.get_allocator(), VK_SYSTEM_ALLOCATION_SCOPE_OBJECT) };
for (uint32_t i = 0; i < timestampCount; ++i)
{
/* Intercept VkSwapchainCalibratedTimestampInfoEXT struct. */
auto *ext = util::find_extension<VkSwapchainCalibratedTimestampInfoEXT>(
VK_STRUCTURE_TYPE_SWAPCHAIN_CALIBRATED_TIMESTAMP_INFO_EXT, pTimestampInfos[i].pNext);
/* Make a copy of the pTimestampsInfo. */
if (!time_stamp_info.try_push_back(pTimestampInfos[i]))
{
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
/* The layer is only handling for present stage local time domains,
every other time domains including swapchain local (VK_TIME_DOMAIN_SWAPCHAIN_LOCAL_EXT)
is not handled by the layer. */
if ((ext != nullptr) && (pTimestampInfos[i].timeDomain == VK_TIME_DOMAIN_PRESENT_STAGE_LOCAL_EXT))
{
assert(ext->swapchain != VK_NULL_HANDLE);
if (!device_data.layer_owns_swapchain(ext->swapchain))
{
continue;
}
/* Check only one present stage is stated. */
assert(((ext->presentStage & (~(ext->presentStage) + 1)) == ext->presentStage));
auto *swapchain = reinterpret_cast<wsi::swapchain_base *>(ext->swapchain);
auto *present_timing_extension = swapchain->get_swapchain_extension<wsi::wsi_ext_present_timing>(true);
wsi::swapchain_calibrated_time calibrated_time;
TRY_LOG_CALL(present_timing_extension->get_swapchain_time_domains().calibrate(
static_cast<VkPresentStageFlagBitsEXT>(ext->presentStage), &calibrated_time));
stage_local_index_and_offset index_and_offset = { i, calibrated_time.offset };
if (!calibration_index_and_offset.try_push_back(index_and_offset))
{
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
time_stamp_info[i].timeDomain = calibrated_time.time_domain;
}
}
TRY_LOG_CALL((*get_calibrated_timestamps)(device, timestampCount, &time_stamp_info[0], pTimestamps, pMaxDeviation));
/* Loop through the calibration_index_and_offset vector and update the timestamps that are stage local
with its respective offset. */
for (const auto &iter : calibration_index_and_offset)
{
pTimestamps[iter.index] += iter.calibration_offset;
}
return VK_SUCCESS;
#endif /* VULKAN_WSI_LAYER_EXPERIMENTAL */
return (*get_calibrated_timestamps)(device, timestampCount, pTimestampInfos, pTimestamps, pMaxDeviation);
}
VWL_VKAPI_CALL(VkResult)
wsi_layer_vkGetCalibratedTimestampsKHR(VkDevice device, uint32_t timestampCount,
const VkCalibratedTimestampInfoKHR *pTimestampInfos, uint64_t *pTimestamps,
uint64_t *pMaxDeviation) VWL_API_POST
{
return wsi_layer_vkGetCalibratedTimestampsEXT(device, timestampCount, pTimestampInfos, pTimestamps, pMaxDeviation);
}

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2025 Arm Limited.
*
* 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 calibrated_timestamps_api.hpp
*
* @brief Contains the Vulkan entrypoints for Calibrated Timestamps extension.
*
*/
#pragma once
#include "util/macros.hpp"
VWL_VKAPI_CALL(VkResult)
wsi_layer_vkGetCalibratedTimestampsEXT(VkDevice device, uint32_t timestampCount,
const VkCalibratedTimestampInfoKHR *pTimestampInfos, uint64_t *pTimestamps,
uint64_t *pMaxDeviation) VWL_API_POST;
VWL_VKAPI_CALL(VkResult)
wsi_layer_vkGetCalibratedTimestampsKHR(VkDevice device, uint32_t timestampCount,
const VkCalibratedTimestampInfoKHR *pTimestampInfos, uint64_t *pTimestamps,
uint64_t *pMaxDeviation) VWL_API_POST;

View file

@ -30,6 +30,7 @@
#include <vulkan/vk_layer.h>
#include <vulkan/vulkan.h>
#include "layer/calibrated_timestamps_api.hpp"
#include "wsi_layer_experimental.hpp"
#include "private_data.hpp"
#include "surface_api.hpp"
@ -571,6 +572,14 @@ wsi_layer_vkGetDeviceProcAddr(VkDevice device, const char *funcName) VWL_API_POS
{
GET_PROC_ADDR(vkReleaseSwapchainImagesEXT);
}
if (layer::device_private_data::get(device).is_device_extension_enabled(VK_EXT_CALIBRATED_TIMESTAMPS_EXTENSION_NAME))
{
GET_PROC_ADDR(vkGetCalibratedTimestampsEXT);
}
if (layer::device_private_data::get(device).is_device_extension_enabled(VK_KHR_CALIBRATED_TIMESTAMPS_EXTENSION_NAME))
{
GET_PROC_ADDR(vkGetCalibratedTimestampsKHR);
}
return layer::device_private_data::get(device).disp.get_user_enabled_entrypoint(
device, layer::device_private_data::get(device).instance_data.api_version, funcName);

View file

@ -435,6 +435,8 @@ private:
false, GetImageSparseMemoryRequirements2KHR) \
/* VK_EXT_swapchain_maintenance1 */ \
EP(ReleaseSwapchainImagesEXT, VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME, VK_API_VERSION_1_1, false, ) \
EP(GetCalibratedTimestampsEXT, VK_EXT_CALIBRATED_TIMESTAMPS_EXTENSION_NAME, API_VERSION_MAX, false, ) \
EP(GetCalibratedTimestampsKHR, VK_KHR_CALIBRATED_TIMESTAMPS_EXTENSION_NAME, API_VERSION_MAX, false, ) \
/* Custom entrypoints */ \
DEVICE_ENTRYPOINTS_LIST_EXPANSION(EP)

View file

@ -60,4 +60,4 @@ void wsi_ext_present_id_wayland::remove_from_pending_present_feedback_list(uint6
} // namespace wayland
} // namespace wsi
#endif // VULKAN_WSI_LAYER_EXPERIMENTAL
#endif /* VULKAN_WSI_LAYER_EXPERIMENTAL */

View file

@ -76,4 +76,4 @@ private:
} // namespace wayland
} // namespace wsi
#endif // VULKAN_WSI_LAYER_EXPERIMENTAL
#endif /* VULKAN_WSI_LAYER_EXPERIMENTAL */