vulkan: Add VK_GOOGLE_display_timing extension (x11+display, anv+radv) [v6]
PATCH 1/1 vulkan: Add VK_GOOGLE_display_timing extension (x11+display, anv+radv) [v6]
This adds support for the VK_GOOGLE_display timing extension, which provides two things:
1) Detailed information about when frames are displayed, including slack time between GPU execution and display frame.
2) Absolute time control over swapchain queue processing. This allows the application to request frames be displayed at specific absolute times, using the same timebase as that provided in vblank events.
Support for this extension has been implemented for the x11 and display backends; adding support to other backends should be reasonable straightforward for one familiar with those systems and should not require any additional device-specific code.
v2:
Adjust GOOGLE_display_timing earliest value. The earliestPresentTime for an image cannot be before the previous image was displayed, or even a frame later (in FIFO mode).
Make GOOGLE_display_timing use render completed time. Switch from VK_PIPELINE_TOP_OF_PIPE_BIT to VK_PIPELINE_STAGE_ALL_COMMANDS_BIT so that the time reported to applications as the end of rendering reflects the latest possible value to ensure that applications don't underestimate the amount of work done in the frame.
v3:
Adopt Jason Ekstrand's coding conventions. Declare variables at first use, eliminate extra whitespace between types and names. Wrap lines to 80 columns.
v4:
Adapt to changes in MESA_query_timestamp extension
v5:
Squash core bits and anv/radv wrappers into a single patch
v6:
Switch from MESA_query_timestamp to EXT_calibrated_timestamps
--- src/amd/vulkan/radv_extensions.py | 1 + src/amd/vulkan/radv_wsi.c | 33 +++ src/intel/vulkan/anv_extensions.py | 1 + src/intel/vulkan/anv_wsi.c | 33 +++ src/vulkan/wsi/wsi_common.c | 303 +++++++++++++++++++++++++++- src/vulkan/wsi/wsi_common.h | 32 +++ src/vulkan/wsi/wsi_common_display.c | 165 ++++++++++++++- src/vulkan/wsi/wsi_common_private.h | 35 ++++ src/vulkan/wsi/wsi_common_x11.c | 71 ++++++- 9 files changed, 662 insertions(+), 12 deletions(-)
src/amd/vulkan/radv_extensions.py +1 -0
@@ -119,6 +119,7 @@ EXTENSIONS = [ Extension('VK_AMD_shader_trinary_minmax', 1, True), Extension('VK_GOOGLE_decorate_string', 1, True), Extension('VK_GOOGLE_hlsl_functionality1', 1, True), + Extension('VK_GOOGLE_display_timing', 1, True), ] class VkVersion:
src/amd/vulkan/radv_wsi.c +33 -0
@@ -295,3 +295,36 @@ VkResult radv_GetPhysicalDevicePresentRectanglesKHR( surface, pRectCount, pRects); } + +/* VK_GOOGLE_display_timing */ +VkResult +radv_GetRefreshCycleDurationGOOGLE ( +VkDevice _device, +VkSwapchainKHR swapchain, +VkRefreshCycleDurationGOOGLE *pDisplayTimingProperties) +{ +RADV_FROM_HANDLE (radv_device, device, _device); +struct radv_physical_device *pdevice = device->physical_device; + +return wsi_common_get_refresh_cycle_duration (&pdevice->wsi_device, + _device, + swapchain, + pDisplayTimingProperties); +} + +VkResult +radv_GetPastPresentationTimingGOOGLE (VkDevice _device, +VkSwapchainKHR swapchain, +uint32_t *pPresentationTimingCount, +VkPastPresentationTimingGOOGLE + *pPresentationTimings) +{ +RADV_FROM_HANDLE (radv_device, device, _device); +struct radv_physical_device *pdevice = device->physical_device; + +return wsi_common_get_past_presentation_timing (&pdevice->wsi_device, + _device, + swapchain, + pPresentationTimingCount, + pPresentationTimings); +}
src/intel/vulkan/anv_extensions.py +1 -0
@@ -130,6 +130,7 @@ EXTENSIONS = [ Extension('VK_EXT_calibrated_timestamps', 1, True), Extension('VK_GOOGLE_decorate_string', 1, True), Extension('VK_GOOGLE_hlsl_functionality1', 1, True), + Extension('VK_GOOGLE_display_timing', 1, True), ] class VkVersion:
src/intel/vulkan/anv_wsi.c +33 -0
@@ -305,3 +305,36 @@ VkResult anv_GetPhysicalDevicePresentRectanglesKHR( surface, pRectCount, pRects); } + +/* VK_GOOGLE_display_timing */ +VkResult +anv_GetRefreshCycleDurationGOOGLE (VkDevice _device, +VkSwapchainKHR swapchain, +VkRefreshCycleDurationGOOGLE + *pDisplayTimingProperties) +{ +ANV_FROM_HANDLE (anv_device, device, _device); + +return wsi_common_get_refresh_cycle_duration (&device->instance-> + physicalDevice.wsi_device, + _device, + swapchain, + pDisplayTimingProperties); +} + +VkResult +anv_GetPastPresentationTimingGOOGLE (VkDevice _device, +VkSwapchainKHR swapchain, +uint32_t *pPresentationTimingCount, +VkPastPresentationTimingGOOGLE + *pPresentationTimings) +{ +ANV_FROM_HANDLE (anv_device, device, _device); + +return wsi_common_get_past_presentation_timing (&device->instance-> + physicalDevice.wsi_device, + _device, + swapchain, + pPresentationTimingCount, + pPresentationTimings); +}
src/vulkan/wsi/wsi_common.c +300 -3
397 lines not on this page · raw message
src/vulkan/wsi/wsi_common.h +32 -0
@@ -96,6 +96,7 @@ struct wsi_device {VkPhysicalDevice pdevice;VkPhysicalDeviceMemoryProperties memory_props;uint32_t queue_family_count; +float timestamp_period;VkPhysicalDevicePCIBusInfoPropertiesEXT pci_bus_info;
@@ -109,14 +110,18 @@ struct wsi_device {WSI_CB (BindImageMemory);WSI_CB (BeginCommandBuffer);WSI_CB (CmdCopyImageToBuffer); +WSI_CB (CmdResetQueryPool); +WSI_CB (CmdWriteTimestamp);WSI_CB (CreateBuffer);WSI_CB (CreateCommandPool);WSI_CB (CreateFence);WSI_CB (CreateImage); +WSI_CB (CreateQueryPool);WSI_CB (DestroyBuffer);WSI_CB (DestroyCommandPool);WSI_CB (DestroyFence);WSI_CB (DestroyImage); +WSI_CB (DestroyQueryPool);WSI_CB (EndCommandBuffer);WSI_CB (FreeMemory);WSI_CB (FreeCommandBuffers);
@@ -124,10 +129,14 @@ struct wsi_device {WSI_CB (GetImageMemoryRequirements);WSI_CB (GetImageSubresourceLayout);WSI_CB (GetMemoryFdKHR); +WSI_CB (GetPhysicalDeviceProperties);WSI_CB (GetPhysicalDeviceFormatProperties);WSI_CB (GetPhysicalDeviceFormatProperties2KHR); +WSI_CB (GetPhysicalDeviceQueueFamilyProperties); +WSI_CB (GetQueryPoolResults);WSI_CB (ResetFences);WSI_CB (QueueSubmit); +WSI_CB (GetCalibratedTimestampsEXT);WSI_CB (WaitForFences);#undef WSI_CB
@@ -241,4 +250,27 @@ wsi_common_queue_present(const struct wsi_device *wsi,int queue_family_index, const VkPresentInfoKHR *pPresentInfo); +VkResult +wsi_common_convert_timestamp (const struct wsi_device *wsi, +VkDevice device_h, +VkSurfaceKHR surface_h, +uint64_t monotonic_timestamp, +uint64_t *surface_timestamp); + +/* VK_GOOGLE_display_timing */ +VkResult +wsi_common_get_refresh_cycle_duration (const struct wsi_device *wsi, +VkDevice device_h, +VkSwapchainKHR swapchain, +VkRefreshCycleDurationGOOGLE + *pDisplayTimingProperties); + +VkResult +wsi_common_get_past_presentation_timing (const struct wsi_device *wsi, +VkDevice device_h, +VkSwapchainKHR swapchain, +uint32_t *pPresentationTimingCount, +VkPastPresentationTimingGOOGLE + *pPresentationTimings); +#endif
src/vulkan/wsi/wsi_common_display.c +159 -6
295 lines not on this page · raw message
src/vulkan/wsi/wsi_common_private.h +35 -0
@@ -25,6 +25,13 @@#include "wsi_common.h" +struct wsi_timing { +bool complete; +bool consumed; +uint64_t target_msc; +VkPastPresentationTimingGOOGLE timing; +}; +struct wsi_image {VkImage image;VkDeviceMemory memory;
@@ -41,8 +48,16 @@ struct wsi_image {uint32_t offsets[4 ];uint32_t row_pitches[4 ];int fds[4 ]; + +VkQueryPool query_pool; + +VkCommandBuffer timestamp_buffer; + +struct wsi_timing *timing; }; +#define WSI_TIMING_HISTORY 16 +struct wsi_swapchain {const struct wsi_device *wsi;
@@ -54,6 +69,16 @@ struct wsi_swapchain {bool use_prime_blit; +uint32_t timing_insert; +uint32_t timing_count; + +struct wsi_timing timing[WSI_TIMING_HISTORY ]; + +uint64_t frame_msc; +uint64_t frame_ust; + +float timestamp_period; +/* Command pools, one per queue family */ VkCommandPool *cmd_pools;
@@ -67,6 +92,10 @@ struct wsi_swapchain {VkResult (*queue_present)(struct wsi_swapchain *swap_chain,uint32_t image_index,const VkPresentRegionKHR *damage); +VkResult (*get_refresh_cycle_duration)(struct wsi_swapchain *swap_chain, +VkRefreshCycleDurationGOOGLE + *pDisplayTimingProperties); + };bool
@@ -100,6 +129,12 @@ wsi_destroy_image(const struct wsi_swapchain *chain, struct wsi_image *image); +void +wsi_mark_timing (struct wsi_swapchain *swapchain, +struct wsi_image *image, +uint64_t ust, +uint64_t msc); +struct wsi_interface { VkResult (*get_support)(VkIcdSurfaceBase *surface, struct wsi_device *wsi_device,
src/vulkan/wsi/wsi_common_x11.c +68 -3
136 lines not on this page · raw message
Signature · 7 lines
2.19.1 _______________________________________________ mesa-dev mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/mesa-dev
Quoted 84 lines · Keith Packard
This adds support for the
VK_GOOGLE_displaytiming extension, which provides two things:1) Detailed information about when frames are displayed, including slack time between GPU execution and display frame.
2) Absolute time control over swapchain queue processing. This allows the application to request frames be displayed at specific absolute times, using the same timebase as that provided in vblank events.
Support for this extension has been implemented for the x11 and display backends; adding support to other backends should be reasonable straightforward for one familiar with those systems and should not require any additional device-specific code.
v2:
AdjustGOOGLE_display_timingearliest value. The earliestPresentTime for an image cannot be before the previous image was displayed, or even a frame later (in FIFO mode).Make
GOOGLE_display_timinguse render completed time. Switch fromVK_PIPELINE_TOP_OF_PIPE_BITtoVK_PIPELINE_STAGE_ALL_COMMANDS_BITso that the time reported to applications as the end of rendering reflects the latest possible value to ensure that applications don't underestimate the amount of work done in the frame.v3:
Adopt Jason Ekstrand's coding conventions. Declare variables at first use, eliminate extra whitespace between types and names. Wrap lines to 80 columns.Suggested-by Jason Ekstrand <[email protected]>v4:
Adapt to changes inMESA_query_timestampextensionv5:
Squash core bits and anv/radv wrappers into a single patchSuggested-by Jason Ekstrand <[email protected]>v6:
Switch fromMESA_query_timestamptoEXT_calibrated_timestampsSigned-off-by Keith Packard <[email protected]>[...]
src/vulkan/wsi/wsi_common.c +27
-3@@ -979,9 +1187,49 @@ wsi_common_queue_present(const struct wsi_device *wsi,*/ struct wsi_image *image = swapchain->get_wsi_image (swapchain, pPresentInfo->pImageIndices[i]); - submit_info.commandBufferCount =1 ; - submit_info.pCommandBuffers = - &image->prime.blit_cmd_buffers[queue_family_index]; + submit_buffers[submit_info.commandBufferCount++] = + image->prime.blit_cmd_buffers[queue_family_index]; + } + +/* Set up GOOGLE_display_timing bits */ +if (present_times_info && + present_times_info->pTimes !=NULL && + i < present_times_info->swapchainCount) + { +const VkPresentTimeGOOGLE *present_time = + &present_times_info->pTimes[i]; + +struct wsi_image *image = + swapchain->get_wsi_image (swapchain, pPresentInfo->pImageIndices[i]); + + timing =wsi_next_timing (swapchain, pPresentInfo->pImageIndices[i]); + timing->timing.presentID = present_time->presentID; + timing->timing.desiredPresentTime = present_time->desiredPresentTime; + timing->target_msc =0 ; + image->timing = timing; + +if (present_time->desiredPresentTime !=0 ) + { +int64_t delta_nsec = (int64_t ) (present_time->desiredPresentTime - + swapchain->frame_ust); + +/* Set the target msc only if it's no more than two seconds from
src/vulkan/wsi/wsi_common.c wsi_common_queue_present(const struct wsi_device *wsi, · line 1226
Quoted 10 lines
src/vulkan/wsi/wsi_common.c +10
-0+* now, and not stale +*/ +if (0 <= delta_nsec && delta_nsec <=2000000000ul ) { +VkRefreshCycleDurationGOOGLE refresh_timing; + + swapchain->get_refresh_cycle_duration (swapchain, + &refresh_timing); + +int64_t refresh = (int64_t ) refresh_timing.refreshDuration; +int64_t frames = (delta_nsec + refresh/2 ) / refresh;
desiredPresentTime has "no sooner than" semantics, so I think this should be
int64_t frames = (delta_nsec + refresh-1) / refresh;
src/vulkan/wsi/wsi_common.c wsi_common_queue_present(const struct wsi_device *wsi, · line 1229
Quoted 3 lines
src/vulkan/wsi/wsi_common.c +3
-0+ timing->target_msc = swapchain->frame_msc + frames; + } + }
Note that MSC based timing won't work well with variable refresh rate. In the long term, support for PresentOptionUST should be implemented and used.
Quoted 53 lines
src/vulkan/wsi/wsi_common_display.c +49
-0@@ -1691,6 +1760,66 @@ wsi_display_queue_present(struct wsi_swapchain *drv_chain,pthread_mutex_lock (&wsi->wait_mutex); +if (image->base.timing && image->base.timing->target_msc !=0 ) { +VkIcdSurfaceDisplay *surface = chain->surface; +wsi_display_mode *display_mode = +wsi_display_mode_from_handle (surface->displayMode); +wsi_display_connector *connector = display_mode->connector; + +wsi_display_debug ("delta frame %ld\n" , + image->base.timing->target_msc - connector->last_frame); +if (image->base.timing->target_msc > connector->last_frame) { +uint64_t frame_queued; +VkDisplayKHR display =wsi_display_connector_to_handle (connector); + + wsi_display_debug_code(uint64_t current_frame, current_nsec; +drmCrtcGetSequence (wsi->fd, connector->crtc_id, + ¤t_frame, + ¤t_nsec); +wsi_display_debug ("from current: %ld\n" , + image->base.timing->target_msc + - current_frame)); + + image->fence =wsi_display_fence_alloc (chain->base.device, + chain->base.wsi, + display, &chain->base.alloc); + +if (!image->fence) { + result =VK_ERROR_OUT_OF_HOST_MEMORY ; + goto bail_unlock; + } + + result =wsi_register_vblank_event (image->fence, + chain->base.wsi, + display, +0 , + image->base.timing->target_msc -1 , + &frame_queued); + +if (result !=VK_SUCCESS ) + goto bail_unlock; + +/* Check and make sure we are queued for the right frame, otherwise +* just go queue an image +*/ +if (frame_queued <= image->base.timing->target_msc -1 ) { + image->state =WSI_IMAGE_WAITING ; + +/* +* Don't set the image member until we're going to wait for the +* event to arrive before flipping to the image. That way, if the +* register_vblank_event call happens to process the event, it
src/vulkan/wsi/wsi_common_display.c wsi_display_queue_present(struct wsi_swapchain *drv_chain, · line 1821
Quoted 10 lines
src/vulkan/wsi/wsi_common_display.c +10
-0+* won't actually do anything +*/ + image->fence->image = image; +wsi_display_start_wait_thread (wsi); + result =VK_SUCCESS ; + goto bail_unlock; + } + + } + }
What is this code for? At least with X11 Present, shouldn't it be sufficient to simply pass the target MSC value to x11_present_to_x11?
Signature · 6 lines
Earthling Michel Dänzer | http://www.amd.com Libre software enthusiast | Mesa and X developer _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
Michel Dänzer <[email protected]> writes:
Thanks for taking time to review this patch!
Quoted 6 lines
src/vulkan/wsi/wsi_common.c wsi_common_queue_present(const struct wsi_device *wsi, · line 1226
Quoted 2 lines
src/vulkan/wsi/wsi_common.c +2
-0+int64_t refresh = (int64_t ) refresh_timing.refreshDuration; +int64_t frames = (delta_nsec + refresh/2 ) / refresh;desiredPresentTime has "no sooner than" semantics, so I think this should be
int64_t frames = (delta_nsec + refresh-1) / refresh;
Hrm. You're certainly right that we want to make sure to not hit the wrong frame, and we need to be very careful with this computation. And that turns out to be 'hard'.
With a naïve computation of frame times:
future_frame_time = past_frame_time + n * refresh
If the reported refresh is longer than the actual interval, due to rounding of that value or clock skew, this computation might select frame n+1 if the driver uses a later frame for its basis than the application:
desiredPresentTime = application_past_frame_time + n * refresh
delta_nsec = (desiredPresentTime - driver_past_frame_time);
frames = (delta_nsec + refresh-1) / refresh;If 'driver_past_frame_time' was sampled 'm' frames after 'application_past_frame time', and 'refresh' is longer than the actual frame time:
desiredPresentTime > driver_past_frame_time + m * refresh
Because desiredPresentTime is *past* our estimate of the beginning of the frame the application wants, and because we're rounding the selected frame up, we end up targeting one frame too late.
Now, if we use my value for 'frames', then we hit the right frame using this value, as long as the error is less than 1/2 frame time:
desiredPresentTime > driver_past_frame_time + m * refresh desiredPresentTime < driver_past_frame_time + m * refresh + refresh/2
delta_nsec > m * refresh
delta_nsec < m * refresh + refresh /
frames > (m * refresh + refresh/2) / refresh frames < (m * refresh + refresh) / refresh
With this computation, frames = m, which is the desired result.
So at least you can see where my code came from. But, it's clearly wrong according to the spec, as you'll see in the next section.
An application can attempt to compensate for this by using an earlier time; a slightly less naïve computation might look like:
future_frame_time = past_frame_time + 1 + (n-1) * refresh
This makes 'future_frame_time' be the earliest possible time that should target the desired frame, given the 'no sooner than' semantics in the spec.
If the reported refresh is shorter than the actual interval, this computation might hit frame (n-1).
Ok, so now we make the application even 'smarter' by having it compute a time in the center of the target frame:
future_frame_time = past_frame_time + (refresh/2) + (n-1) * refresh
With your suggested code, this will hit the desired frame unless the error in the frame time is more than 1/2 of the refresh interval, which seems pretty good.
Ok, so what can we do? I think we start with what we know:
* driver_past_frame_time >= application_past_frame_time
Because all application frame time information comes from the driver, we just need to use the latest possible frame time in the driver to keep this true.
Now, what will cause errors in the 'refresh' value? 'refresh' error is a combination of rounding error and CPU vs GPU clock skew.
* Rounding error. This is always less than 1ns.
* Clock skew is related to the performance of a couple of crystals in the system.
Even cheap crystals provide significantly better than 100ppm (parts per million) performance. At 30Hz, refresh_interval is 33.3ms, or 33,333,333ns, so each crystals will have a maximum error of 3300ns; combine two and we've a maximum error of 6600ns.
As you can see, the rounding error is lost in the noise here, unless we find a system that uses CLOCK_MONOTONIC for display timing. It'll take 5000 frames before that error reaches a frame time.
As long as the application_past_frame_time is within 2500 frames of the driver_past_frame_time, the error in the future_frame_time estimate will be within one-half frame, and our application will work reliably using the 'smarter' computation of future frame time.
I would prefer to let applications use the initial naïve future_frame_time estimate, as I think that could also work with variable refresh timing, but that would require a fairly complicated change in the specification.
Quoted 7 lines
src/vulkan/wsi/wsi_common.c wsi_common_queue_present(const struct wsi_device *wsi, · line 1229
Quoted 3 lines
src/vulkan/wsi/wsi_common.c +3
-0+ timing->target_msc = swapchain->frame_msc + frames; + } + }Note that MSC based timing won't work well with variable refresh rate. In the long term, support for PresentOptionUST should be implemented and used.
Agreed. Given the above discussion, I think that will have to wait for a more sophisticated specification for what 'desiredPresentTime' means, as I think the current specification makes it "impossible" to provide an actual desiredPresentTime to the interface without that causing occasional incorrect frame selection.
Quoted 2 lines
What is this code for? At least with X11 Present, shouldn't it be sufficient to simply pass the target MSC value to
x11_present_to_x11?
This is the direct display back end which uses DRM interfaces in the kernel. Those interfaces do not support queuing a flip for anything other than the next frame.
I'll update this patch to correct the computation of the next frame from 'nearest' to 'no sooner than'. Following the spec is certainly better than making applications simpler as any application which does follow the spec is going to get the wrong answer...
Signature · 1 line
-keith
_______________________________________________
mesa-dev mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/mesa-dev