<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Tue, Jul 10, 2018 at 9:47 AM Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Mon, Jul 9, 2018 at 5:35 PM Keith Packard <<a href="mailto:keithp@keithp.com" target="_blank">keithp@keithp.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This extension adds a single function to query the current GPU<br>
timestamp, just like glGetInteger64v(GL_TIMESTAMP, &timestamp). This<br>
function is needed to complete the implementation of<br>
GOOGLE_display_timing, which needs to be able to correlate GPU and CPU<br>
timestamps.<br>
<br>
v2:     Adopt Jason Ekstrand's coding conventions<br>
<br>
        Declare variables at first use, eliminate extra whitespace between<br>
        types and names. Wrap lines to 80 columns.<br>
<br>
        Add extension to list in alphabetical order<br>
<br>
        Suggested-by: Jason Ekstrand <<a href="mailto:jason.ekstrand@intel.com" target="_blank">jason.ekstrand@intel.com</a>><br>
<br>
v3:     Report both device and surface timestamps<br>
<br>
        This will allow us to get timestamps more closely aligned by<br>
        getting both in a single call from the kernel.<br>
<br>
        To make this independent of the timebase used by the WSI<br>
        layer, provide a new wsi hook which converts CLOCK_MONOTONIC<br>
        into the matching WSI timebase. Right now, all of our backends<br>
        use CLOCK_MONOTONIC, so there's nothing actually doing<br>
        conversions, but it seemed best to put the infrastructure in<br>
        place so that I could validate the extension interface would<br>
        work if that became necessary.<br>
<br>
Signed-off-by: Keith Packard <<a href="mailto:keithp@keithp.com" target="_blank">keithp@keithp.com</a>><br>
---<br>
 include/vulkan/vk_mesa_query_timestamp.h | 46 ++++++++++++++++++++++++<br>
 src/vulkan/registry/vk.xml               | 20 +++++++++++<br>
 src/vulkan/wsi/wsi_common.c              | 17 +++++++++<br>
 src/vulkan/wsi/wsi_common.h              |  7 ++++<br>
 src/vulkan/wsi/wsi_common_private.h      |  4 +++<br>
 5 files changed, 94 insertions(+)<br>
 create mode 100644 include/vulkan/vk_mesa_query_timestamp.h<br>
<br>
diff --git a/include/vulkan/vk_mesa_query_timestamp.h b/include/vulkan/vk_mesa_query_timestamp.h<br>
new file mode 100644<br>
index 00000000000..262f094db27<br>
--- /dev/null<br>
+++ b/include/vulkan/vk_mesa_query_timestamp.h<br>
@@ -0,0 +1,46 @@<br>
+/*<br>
+ * Copyright © 2018 Keith Packard<br>
+ *<br>
+ * Permission to use, copy, modify, distribute, and sell this software and its<br>
+ * documentation for any purpose is hereby granted without fee, provided that<br>
+ * the above copyright notice appear in all copies and that both that copyright<br>
+ * notice and this permission notice appear in supporting documentation, and<br>
+ * that the name of the copyright holders not be used in advertising or<br>
+ * publicity pertaining to distribution of the software without specific,<br>
+ * written prior permission.  The copyright holders make no representations<br>
+ * about the suitability of this software for any purpose.  It is provided "as<br>
+ * is" without express or implied warranty.<br>
+ *<br>
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,<br>
+ * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO<br>
+ * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR<br>
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,<br>
+ * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER<br>
+ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE<br>
+ * OF THIS SOFTWARE.<br>
+ */<br>
+<br>
+#ifndef __VK_MESA_QUERY_TIMESTAMP_H__<br>
+#define __VK_MESA_QUERY_TIMESTAMP_H__<br>
+<br>
+#ifdef __cplusplus<br>
+extern "C" {<br>
+#endif<br>
+<br>
+typedef struct VkCurrentTimestampMESA {<br>
+    uint64_t    deviceTimestamp;<br>
+    uint64_t    surfaceTimestamp;<br>
+} VkCurrentTimestampMESA;<br>
+<br>
+typedef VkResult (VKAPI_PTR *PFN_vkQueryCurrentTimestampMESA)(<br>
+    VkDevice device, VkSurfaceKHR surface, VkCurrentTimestampMESA *timestamp);<br>
+<br>
+VKAPI_ATTR VkResult VKAPI_CALL vkQueryCurrentTimestampMESA(<br>
+    VkDevice device, VkSurfaceKHR surface, VkCurrentTimestampMESA *timestamp);<br></blockquote><div><br></div><div>I hate to salami extensions too much but it might be better if this were two extensions.  One would be</div><div><br></div><div>typedef struct VkCurrentTimestampMESA {</div><div>    VkStructType sType;</div><div>    void *pNext;</div><div>    uint64_t timestamp;<br></div><div>} VkCurrentTimestampMESA;</div><div><br></div><div>VkResult vkGetCurrentTimestampMESA(VkDevice device, VkBaseInStructureKHR *pInfo, VkCurrentTimestampMESA *pTimestamp);</div><div><br></div><div>And the second would just define two chain in structs</div><div><br></div><div>typedef struct VkCurrentPresentTimestampGetInfoMESA {</div><div>    VkStructureType sType;</div><div>    void *pNext;</div><div>    VkSurfaceKHR surface;<br></div><div>} VkCurrentPresentTimestampGetInfoMESA;</div><div><div><br></div><div>typedef struct VkCurrentPresentTimestampMESA {</div><div>    VkStructureType sType;</div><div>    void *pNext;</div>    uint64_t presentTimestamp;<br></div><div><div>} VkCurrentPresentTimestampMESA;</div></div><div><br></div><div>You could probably also just make the surface part of VkCurrentPresentTimestampMESA and not have the extendible input struct.  That might be a bit cleaner in some ways.  If the vkGetCurrentTimestampMESA call is extended to get the surface timestamp, the driver would have the option to call the kernel for the more accurate thing or it could just return the two separate things that your implementation already does.<br></div><div><br></div><div>The reason I suggest this is that Vulkan currently lacks any sort of CPU timestamp query and that's something which is likely useful in its own right.  It'd be annoying if we added such a useful thing to the API and then made it require a VkSurface to work. :-)</div></div></div></blockquote><div><br></div><div>Another option, instead of having two extensions,  would be to make the second timestamp that gets returned be some sort of OS timestamp.  On Linux, it would be defined to be CLOCK_MONOTONIC; on windows, I'm not sure what the right thing would be.  Then we could just magically know in the window system code which calls this extension that we get CLOCK_MONTONIC from that function and we can do whatever conversions we want there.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div>--Jason</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+#ifdef __cplusplus<br>
+}<br>
+#endif<br>
+<br>
+#endif /* __VK_MESA_QUERY_TIMESTAMP_H__ */<br>
+<br>
diff --git a/src/vulkan/registry/vk.xml b/src/vulkan/registry/vk.xml<br>
index 4419c6fbf96..9c5a2f79398 100644<br>
--- a/src/vulkan/registry/vk.xml<br>
+++ b/src/vulkan/registry/vk.xml<br>
@@ -3198,6 +3198,10 @@ server.<br>
             <member><type>VkBool32</type>                           <name>conditionalRendering</name></member><br>
             <member><type>VkBool32</type>                           <name>inheritedConditionalRendering</name></member><br>
         </type><br>
+       <type catagory="struct" name="VkCurrentTimestampMESA"><br>
+         <member><type>uint64_t</type>         <name>deviceTimestamp</name></member><br>
+         <member><type>uint64_t</type>        <name>surfaceTimestamp</name></member><br>
+       </type><br>
     </types><br>
<br>
     <comment>Vulkan enumerant (token) definitions</comment><br>
@@ -6239,6 +6243,12 @@ server.<br>
             <param><type>uint32_t</type> <name>maxDrawCount</name></param><br>
             <param><type>uint32_t</type> <name>stride</name></param><br>
         </command><br>
+        <command><br>
+            <proto><type>VkResult</type> <name>vkQueryCurrentTimestampMESA</name></proto><br>
+            <param><type>VkDevice</type> <name>device</name></param><br>
+           <param><type>VkSurfaceKHR</type> <name>surface</name></param><br>
+            <param><type>VkCurrentTimestampMESA</type>* <name>pTimestamp</name></param><br>
+        </command><br>
     </commands><br>
<br>
     <feature api="vulkan" name="VK_VERSION_1_0" number="1.0" comment="Vulkan core API interface definitions"><br>
@@ -9008,5 +9018,15 @@ server.<br>
                 <enum value="&quot;VK_KHR_extension_214&quot;"              name="VK_KHR_EXTENSION_214_EXTENSION_NAME"/><br>
             </require><br>
         </extension><br>
+        <extension name="VK_MESA_query_timestamp" number="215"<br>
+                  type="device" author="MESA"<br>
+                  contact="Keith Packard @keithp"<br>
+                  supported="vulkan"><br>
+            <require><br>
+                <enum value="1"                                         name="VK_MESA_QUERY_TIMESTAMP_SPEC_VERSION"/><br>
+                <enum value="&quot;VK_MESA_query_timestamp&quot;"       name="VK_MESA_QUERY_TIMESTAMP_NAME"/><br>
+                <command name="vkQueryCurrentTimestampMESA"/><br>
+            </require><br>
+        </extension><br>
     </extensions><br>
 </registry><br>
diff --git a/src/vulkan/wsi/wsi_common.c b/src/vulkan/wsi/wsi_common.c<br>
index f2d90a6bba2..9316470ad20 100644<br>
--- a/src/vulkan/wsi/wsi_common.c<br>
+++ b/src/vulkan/wsi/wsi_common.c<br>
@@ -975,3 +975,20 @@ wsi_common_queue_present(const struct wsi_device *wsi,<br>
<br>
    return final_result;<br>
 }<br>
+<br>
+VkResult<br>
+wsi_common_convert_timestamp(const struct wsi_device *wsi,<br>
+                             VkDevice device_h,<br>
+                             VkSurfaceKHR surface_h,<br>
+                             uint64_t monotonic_timestamp,<br>
+                             uint64_t *surface_timestamp)<br>
+{<br>
+   ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, surface_h);<br>
+   struct wsi_interface *iface = wsi->wsi[surface->platform];<br>
+<br>
+   if (iface->convert_timestamp)<br>
+      return iface->convert_timestamp(surface, wsi, monotonic_timestamp,<br>
+                                      surface_timestamp);<br>
+   *surface_timestamp = monotonic_timestamp;<br>
+   return VK_SUCCESS;<br>
+}<br>
diff --git a/src/vulkan/wsi/wsi_common.h b/src/vulkan/wsi/wsi_common.h<br>
index 33e4f849ac9..0a57dc4eb23 100644<br>
--- a/src/vulkan/wsi/wsi_common.h<br>
+++ b/src/vulkan/wsi/wsi_common.h<br>
@@ -235,4 +235,11 @@ wsi_common_queue_present(const struct wsi_device *wsi,<br>
                          int queue_family_index,<br>
                          const VkPresentInfoKHR *pPresentInfo);<br>
<br>
+VkResult<br>
+wsi_common_convert_timestamp(const struct wsi_device *wsi,<br>
+                             VkDevice device_h,<br>
+                             VkSurfaceKHR surface_h,<br>
+                             uint64_t monotonic_timestamp,<br>
+                             uint64_t *surface_timestamp);<br>
+<br>
 #endif<br>
diff --git a/src/vulkan/wsi/wsi_common_private.h b/src/vulkan/wsi/wsi_common_private.h<br>
index 9f2aacd6560..843b4c9b286 100644<br>
--- a/src/vulkan/wsi/wsi_common_private.h<br>
+++ b/src/vulkan/wsi/wsi_common_private.h<br>
@@ -126,6 +126,10 @@ struct wsi_interface {<br>
                                 const VkSwapchainCreateInfoKHR* pCreateInfo,<br>
                                 const VkAllocationCallbacks* pAllocator,<br>
                                 struct wsi_swapchain **swapchain);<br>
+   VkResult (*convert_timestamp)(VkIcdSurfaceBase *surface,<br>
+                                 const struct wsi_device *wsi_device,<br>
+                                 uint64_t monotonic_timestamp,<br>
+                                 uint64_t *surface_timestamp);<br>
 };<br>
<br>
 VkResult wsi_x11_init_wsi(struct wsi_device *wsi_device,<br>
-- <br>
2.18.0<br>
<br>
_______________________________________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org" target="_blank">mesa-dev@lists.freedesktop.org</a><br>
<a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</blockquote></div></div>
</blockquote></div></div>