<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Aug 23, 2017 at 11:23 PM, Tapani Pälli <span dir="ltr"><<a href="mailto:tapani.palli@intel.com" target="_blank">tapani.palli@intel.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Patch adds required functionality for extension to manage a list of<br>
application provided callbacks and handle debug reporting from driver<br>
and application side.<br>
<br>
Signed-off-by: Tapani Pälli <<a href="mailto:tapani.palli@intel.com">tapani.palli@intel.com</a>><br>
---<br>
 src/intel/Makefile.sources          |   1 +<br>
 src/intel/vulkan/anv_debug_<wbr>report.c | 133 ++++++++++++++++++++++++++++++<wbr>++++++<br>
 src/intel/vulkan/anv_device.c       |  40 +++++++++++<br>
 src/intel/vulkan/anv_<wbr>extensions.py  |   1 +<br>
 src/intel/vulkan/anv_private.h      |  32 +++++++++<br>
 5 files changed, 207 insertions(+)<br>
 create mode 100644 src/intel/vulkan/anv_debug_<wbr>report.c<br>
<br>
diff --git a/src/intel/Makefile.sources b/src/intel/Makefile.sources<br>
index 4074ba9ee5..200713b06e 100644<br>
--- a/src/intel/Makefile.sources<br>
+++ b/src/intel/Makefile.sources<br>
@@ -205,6 +205,7 @@ VULKAN_FILES := \<br>
        vulkan/anv_batch_chain.c \<br>
        vulkan/anv_blorp.c \<br>
        vulkan/anv_cmd_buffer.c \<br>
+       vulkan/anv_debug_report.c \<br>
        vulkan/anv_descriptor_set.c \<br>
        vulkan/anv_device.c \<br>
        vulkan/anv_dump.c \<br>
diff --git a/src/intel/vulkan/anv_debug_<wbr>report.c b/src/intel/vulkan/anv_debug_<wbr>report.c<br>
new file mode 100644<br>
index 0000000000..1a4868cd52<br>
--- /dev/null<br>
+++ b/src/intel/vulkan/anv_debug_<wbr>report.c<br>
@@ -0,0 +1,133 @@<br>
+/*<br>
+ * Copyright © 2017 Intel Corporation<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS<br>
+ * IN THE SOFTWARE.<br>
+ */<br>
+<br>
+#include "anv_private.h"<br>
+#include "vk_util.h"<br>
+<br>
+/* This file contains implementation for VK_EXT_debug_report. */<br>
+<br>
+VkResult<br>
+anv_<wbr>CreateDebugReportCallbackEXT(<wbr>VkInstance _instance,<br>
+                                 const VkDebugReportCallbackCreateInf<wbr>oEXT* pCreateInfo,<br>
+                                 const VkAllocationCallbacks* pAllocator,<br>
+                                 VkDebugReportCallbackEXT* pCallback)<br>
+{<br>
+   ANV_FROM_HANDLE(anv_instance, instance, _instance);<br>
+   const VkAllocationCallbacks *alloc =<br>
+      pAllocator ? pAllocator : &instance->alloc;<br></blockquote><div><br></div><div>This is what vk_alloc2 is for.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
+   vk_foreach_struct(info, pCreateInfo) {<br></blockquote><div><br></div><div>Usually, we handle the primary structure directly and then call vk_foreach_struct on pCreateInfo->pNext.  This is because the things in the pNext chain are going to be modifiers to the original thing so they probably need to happen between allocating the callback and list_addtail().<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+      switch (info->sType) {<br>
+      case VK_STRUCTURE_TYPE_DEBUG_<wbr>REPORT_CALLBACK_CREATE_INFO_<wbr>EXT: {<br>
+         struct anv_debug_callback *cb =<br>
+            vk_alloc(alloc, sizeof(struct anv_debug_callback), 8,<br>
+                     VK_SYSTEM_ALLOCATION_SCOPE_<wbr>INSTANCE);<br>
+         if (!cb)<br>
+            return vk_error(VK_ERROR_OUT_OF_HOST_<wbr>MEMORY);<br>
+<br>
+         cb->flags = pCreateInfo->flags;<br>
+         cb->callback = pCreateInfo->pfnCallback;<br>
+         cb->data = pCreateInfo->pUserData;<br>
+<br>
+         list_addtail(&cb->link, &instance->callbacks);<br></blockquote><div><br></div><div>What kind of threading guarantees does debug_report provide?  I'm guessing none in which case we need to lock around this list.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+         break;<br>
+      }<br>
+      default:<br>
+         anv_debug_ignored_stype(info-><wbr>sType);<br>
+         break;<br>
+      }<br>
+   }<br>
+<br>
+   return VK_SUCCESS;<br>
+}<br>
+<br>
+void<br>
+anv_<wbr>DestroyDebugReportCallbackEXT(<wbr>VkInstance _instance,<br>
+                                  VkDebugReportCallbackEXT callback,<br>
+                                  const VkAllocationCallbacks* pAllocator)<br>
+{<br>
+   ANV_FROM_HANDLE(anv_instance, instance, _instance);<br>
+   const VkAllocationCallbacks *alloc =<br>
+      pAllocator ? pAllocator : &instance->alloc;<br>
+<br>
+   list_for_each_entry_safe(<wbr>struct anv_debug_callback, debug_cb,<br>
+                            &instance->callbacks, link) {<br>
+      /* Found a match, remove from list and destroy given callback. */<br>
+      if ((VkDebugReportCallbackEXT)<wbr>debug_cb->callback == callback) {<br>
+         list_del(&debug_cb->link);<br></blockquote><div><br></div><div>lock<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+         vk_free(alloc, debug_cb);<br>
+      }<br>
+   }<br>
+}<br>
+<br>
+void<br>
+anv_DebugReportMessageEXT(<wbr>VkInstance _instance,<br>
+                          VkDebugReportFlagsEXT flags,<br>
+                          VkDebugReportObjectTypeEXT objectType,<br>
+                          uint64_t object,<br>
+                          size_t location,<br>
+                          int32_t messageCode,<br>
+                          const char* pLayerPrefix,<br>
+                          const char* pMessage)<br></blockquote><div><br></div><div>Woah... This is a bit unexpected.  I wonder why this entrypoint even exists.  One would think that the loader could just do the aggrigation without it.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+{<br>
+   ANV_FROM_HANDLE(anv_instance, instance, _instance);<br>
+   anv_debug_report(instance, flags, objectType, object,<br>
+                    location, messageCode, pLayerPrefix, pMessage);<br>
+<br>
+}<br>
+<br>
+void<br>
+anv_debug_report_call(struct anv_debug_callback *cb,<br>
+                      VkDebugReportFlagsEXT flags,<br>
+                      VkDebugReportObjectTypeEXT object_type,<br>
+                      uint64_t handle,<br>
+                      size_t location,<br>
+                      int32_t messageCode,<br>
+                      const char* pLayerPrefix,<br>
+                      const char *pMessage)<br></blockquote><div><br></div><div>Why is this broken out into a helper?  There's nothing strictly wrong with doing so but it seems kind-of pointless to me.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+{<br>
+   cb->callback(flags, object_type, handle, location, messageCode,<br>
+                pLayerPrefix, pMessage, cb->data);<br>
+}<br>
+<br>
+void<br>
+anv_debug_report(struct anv_instance *instance,<br>
+                 VkDebugReportFlagsEXT flags,<br>
+                 VkDebugReportObjectTypeEXT object_type,<br>
+                 uint64_t handle,<br>
+                 size_t location,<br>
+                 int32_t messageCode,<br>
+                 const char* pLayerPrefix,<br>
+                 const char *pMessage)<br>
+{<br>
+   /* Allow passing NULL for now. */<br>
+   if (!instance)<br>
+      return;<br>
+<br>
+   list_for_each_entry_safe(<wbr>struct anv_debug_callback, cb,<br>
+                            &instance->callbacks, link) {<br></blockquote><div><br></div><div>Why is are you using for_each_entry_safe?  You're not deleting anything.  I suppose it is possible that one of the callbacks could respond by deleting itself but that seems weird.  If that's legal then we have a bunch of other weird things to consider.</div><div><br></div><div>Also, I think we need to lock around walking this list.  Just to keep things efficient, we probably want to do an "if (list_empty(&instance->callbacks)) return" before taking the lock.  It's safe to check list_empty outside the lock because it's just one pointer compare.  The worst that happens is list_empty returns false and then we go, lock, and walk the list.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+      if (cb->flags & flags)<br>
+         anv_debug_report_call(cb, flags, object_type, handle,<br>
+                               location, messageCode, pLayerPrefix, pMessage);<br>
+   }<br>
+}<br>
diff --git a/src/intel/vulkan/anv_device.<wbr>c b/src/intel/vulkan/anv_device.<wbr>c<br>
index a6d5215ab8..01e1869720 100644<br>
--- a/src/intel/vulkan/anv_device.<wbr>c<br>
+++ b/src/intel/vulkan/anv_device.<wbr>c<br>
@@ -441,9 +441,32 @@ VkResult anv_CreateInstance(<br>
     VkInstance*                                 pInstance)<br>
 {<br>
    struct anv_instance *instance;<br>
+   struct anv_debug_callback ctor_cb = { 0 };<br>
<br>
    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_<wbr>CREATE_INFO);<br>
<br>
+   /* Check if user passed a debug report callback to be used during<br>
+    * Create/Destroy of instance.<br>
+    */<br>
+   vk_foreach_struct(ext, pCreateInfo->pNext) {<br>
+      switch (ext->sType) {<br>
+      case VK_STRUCTURE_TYPE_DEBUG_<wbr>REPORT_CALLBACK_CREATE_INFO_<wbr>EXT: {<br>
+         VkDebugReportCallbackCreateInf<wbr>oEXT *cb =<br>
+            (<wbr>VkDebugReportCallbackCreateInf<wbr>oEXT *) ext;<br>
+         ctor_cb.flags = cb->flags;<br>
+         ctor_cb.callback = cb->pfnCallback;<br>
+         ctor_cb.data = cb->pUserData;<br></blockquote><div><br></div><div>Would it be worth adding this to the list and them removing it later?  I don't know if that would actually simplify things or not.</div><div><br></div><div>Also, have you written any tests for debug_report?  Really, the only thing we can test it for is "did it crash?" but it's probably worth adding some debug_report stuff to crucible for that purpose.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+         break;<br>
+      }<br>
+      default:<br>
+         /* We ignore any other type by purpose here, we are only<br>
+          * interested if debug report callback was found for instance<br>
+          * ctor/dtor.<br>
+          */<br>
+         break;<br>
+      }<br>
+   };<br>
+<br>
    uint32_t client_version;<br>
    if (pCreateInfo->pApplicationInfo &&<br>
        pCreateInfo->pApplicationInfo-<wbr>>apiVersion != 0) {<br>
@@ -454,6 +477,17 @@ VkResult anv_CreateInstance(<br>
<br>
    if (VK_MAKE_VERSION(1, 0, 0) > client_version ||<br>
        client_version > VK_MAKE_VERSION(1, 0, 0xfff)) {<br>
+<br>
+      if (ctor_cb.callback && ctor_cb.flags & VK_DEBUG_REPORT_ERROR_BIT_EXT)<br>
+         anv_debug_report_call(&ctor_<wbr>cb,<br>
+                               VK_DEBUG_REPORT_ERROR_BIT_EXT,<br>
+                               VK_DEBUG_REPORT_OBJECT_TYPE_<wbr>INSTANCE_EXT,<br>
+                               0, /* No handle available yet. */<br>
+                               __LINE__,<br>
+                               0,<br>
+                               "anv",<br>
+                               "incompatible driver version");<br>
+<br>
       return vk_errorf(VK_ERROR_<wbr>INCOMPATIBLE_DRIVER,<br>
                        "Client requested version %d.%d.%d",<br>
                        VK_VERSION_MAJOR(client_<wbr>version),<br>
@@ -482,6 +516,12 @@ VkResult anv_CreateInstance(<br>
    instance->apiVersion = client_version;<br>
    instance->physicalDeviceCount = -1;<br>
<br>
+   list_inithead(&instance-><wbr>callbacks);<br>
+<br>
+   /* Store report debug callback to be used during DestroyInstance. */<br>
+   if (ctor_cb.callback)<br>
+      memcpy(&instance->dtor_cb, &ctor_cb, sizeof(struct anv_debug_callback));<br>
+<br>
    _mesa_locale_init();<br>
<br>
    VG(VALGRIND_CREATE_MEMPOOL(<wbr>instance, 0, false));<br>
diff --git a/src/intel/vulkan/anv_<wbr>extensions.py b/src/intel/vulkan/anv_<wbr>extensions.py<br>
index 3252e0fc56..db759bd467 100644<br>
--- a/src/intel/vulkan/anv_<wbr>extensions.py<br>
+++ b/src/intel/vulkan/anv_<wbr>extensions.py<br>
@@ -70,6 +70,7 @@ EXTENSIONS = [<br>
     Extension('VK_KHR_xcb_surface'<wbr>,                       6, 'VK_USE_PLATFORM_XCB_KHR'),<br>
     Extension('VK_KHR_xlib_<wbr>surface',                      6, 'VK_USE_PLATFORM_XLIB_KHR'),<br>
     Extension('VK_KHX_multiview',                         1, True),<br>
+    Extension('VK_EXT_debug_<wbr>report',                      8, True),<br>
 ]<br>
<br>
 class VkVersion:<br>
diff --git a/src/intel/vulkan/anv_<wbr>private.h b/src/intel/vulkan/anv_<wbr>private.h<br>
index 6b2414429f..e3733d5052 100644<br>
--- a/src/intel/vulkan/anv_<wbr>private.h<br>
+++ b/src/intel/vulkan/anv_<wbr>private.h<br>
@@ -61,6 +61,8 @@ typedef uint32_t xcb_window_t;<br>
 struct anv_buffer;<br>
 struct anv_buffer_view;<br>
 struct anv_image_view;<br>
+struct anv_instance;<br>
+struct anv_debug_callback;<br>
<br>
 struct gen_l3_config;<br>
<br>
@@ -237,6 +239,24 @@ void __anv_perf_warn(const char *file, int line, const char *format, ...)<br>
 void anv_loge(const char *format, ...) anv_printflike(1, 2);<br>
 void anv_loge_v(const char *format, va_list va);<br>
<br>
+void anv_debug_report(struct anv_instance *instance,<br>
+                      VkDebugReportFlagsEXT flags,<br>
+                      VkDebugReportObjectTypeEXT object_type,<br>
+                      uint64_t handle,<br>
+                      size_t location,<br>
+                      int32_t messageCode,<br>
+                      const char* pLayerPrefix,<br>
+                      const char *pMessage);<br>
+<br>
+void anv_debug_report_call(struct anv_debug_callback *cb,<br>
+                           VkDebugReportFlagsEXT flags,<br>
+                           VkDebugReportObjectTypeEXT object_type,<br>
+                           uint64_t handle,<br>
+                           size_t location,<br>
+                           int32_t messageCode,<br>
+                           const char* pLayerPrefix,<br>
+                           const char *pMessage);<br>
+<br>
 /**<br>
  * Print a FINISHME message, including its source location.<br>
  */<br>
@@ -665,6 +685,14 @@ struct anv_physical_device {<br>
     int                                         local_fd;<br>
 };<br>
<br>
+struct anv_debug_callback {<br>
+   /* Link in the 'callbacks' list in anv_instance struct. */<br>
+   struct list_head                             link;<br>
+   VkDebugReportFlagsEXT                        flags;<br>
+   PFN_vkDebugReportCallbackEXT                 callback;<br>
+   void *                                       data;<br>
+};<br>
+<br>
 struct anv_instance {<br>
     VK_LOADER_DATA                              _loader_data;<br>
<br>
@@ -673,6 +701,10 @@ struct anv_instance {<br>
     uint32_t                                    apiVersion;<br>
     int                                         physicalDeviceCount;<br>
     struct anv_physical_device                  physicalDevice;<br>
+<br>
+    /* VK_EXT_debug_report debug callbacks */<br>
+    struct list_head                            callbacks;<br>
+    struct anv_debug_callback                   dtor_cb;<br>
 };<br>
<br>
 VkResult anv_init_wsi(struct anv_physical_device *physical_device);<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.13.5<br>
<br>
</font></span></blockquote></div><br></div></div>