diff --git a/CMakeLists.txt b/CMakeLists.txt index 62d388f..c8063ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/layer/calibrated_timestamps_api.cpp b/layer/calibrated_timestamps_api.cpp new file mode 100644 index 0000000..553dedd --- /dev/null +++ b/layer/calibrated_timestamps_api.cpp @@ -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 +#include + +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").has_value() ? + device_data.disp.get_fn("vkGetCalibratedTimestampsEXT") : + device_data.disp.get_fn("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 time_stamp_info{ util::allocator(device_data.get_allocator(), + VK_SYSTEM_ALLOCATION_SCOPE_OBJECT) }; + util::vector 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( + 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(ext->swapchain); + auto *present_timing_extension = swapchain->get_swapchain_extension(true); + wsi::swapchain_calibrated_time calibrated_time; + TRY_LOG_CALL(present_timing_extension->get_swapchain_time_domains().calibrate( + static_cast(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); +} \ No newline at end of file diff --git a/layer/calibrated_timestamps_api.hpp b/layer/calibrated_timestamps_api.hpp new file mode 100644 index 0000000..4179f0f --- /dev/null +++ b/layer/calibrated_timestamps_api.hpp @@ -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; \ No newline at end of file diff --git a/layer/layer.cpp b/layer/layer.cpp index 276fd8b..fe847e9 100644 --- a/layer/layer.cpp +++ b/layer/layer.cpp @@ -30,6 +30,7 @@ #include #include +#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); diff --git a/layer/private_data.hpp b/layer/private_data.hpp index 4eaa92b..36edb8e 100644 --- a/layer/private_data.hpp +++ b/layer/private_data.hpp @@ -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) diff --git a/wsi/wayland/present_id_wayland.cpp b/wsi/wayland/present_id_wayland.cpp index 050f8eb..4da04e0 100644 --- a/wsi/wayland/present_id_wayland.cpp +++ b/wsi/wayland/present_id_wayland.cpp @@ -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 \ No newline at end of file +#endif /* VULKAN_WSI_LAYER_EXPERIMENTAL */ diff --git a/wsi/wayland/present_id_wayland.hpp b/wsi/wayland/present_id_wayland.hpp index 6fa774f..8bbeaa8 100644 --- a/wsi/wayland/present_id_wayland.hpp +++ b/wsi/wayland/present_id_wayland.hpp @@ -76,4 +76,4 @@ private: } // namespace wayland } // namespace wsi -#endif // VULKAN_WSI_LAYER_EXPERIMENTAL \ No newline at end of file +#endif /* VULKAN_WSI_LAYER_EXPERIMENTAL */