summaryrefslogtreecommitdiffstats
path: root/src/vulkan
diff options
context:
space:
mode:
authorAlex Smith <[email protected]>2017-06-06 12:31:05 +0100
committerJason Ekstrand <[email protected]>2017-06-06 08:17:13 -0700
commit621b3410f5f88e2a3743bc025b100717cac26e63 (patch)
tree1b3714721de48490a4a25f361e93c400f0e03f7b /src/vulkan
parent2ef73473c8eedab679637334a0af597d26222e0a (diff)
util/vulkan: Move Vulkan utilities to src/vulkan/util
We have Vulkan utilities in both src/util and src/vulkan/util. The latter seems a more appropriate place for Vulkan-specific things, so move them there. v2: Android build system changes (from Tapani Pälli) Signed-off-by: Alex Smith <[email protected]> Reviewed-by: Emil Velikov <[email protected]> Reviewed-by: Eric Engestrom <[email protected]> Acked-by: Jason Ekstrand <[email protected]>
Diffstat (limited to 'src/vulkan')
-rw-r--r--src/vulkan/Android.mk2
-rw-r--r--src/vulkan/Makefile.am7
-rw-r--r--src/vulkan/Makefile.sources4
-rw-r--r--src/vulkan/util/vk_alloc.h94
-rw-r--r--src/vulkan/util/vk_util.h200
-rw-r--r--src/vulkan/wsi/wsi_common.h2
-rw-r--r--src/vulkan/wsi/wsi_common_wayland.c2
-rw-r--r--src/vulkan/wsi/wsi_common_x11.c2
8 files changed, 309 insertions, 4 deletions
diff --git a/src/vulkan/Android.mk b/src/vulkan/Android.mk
index 7653f34c68a..8eb42ece878 100644
--- a/src/vulkan/Android.mk
+++ b/src/vulkan/Android.mk
@@ -41,6 +41,8 @@ LOCAL_C_INCLUDES := \
LOCAL_GENERATED_SOURCES := $(addprefix $(intermediates)/, \
$(VULKAN_UTIL_GENERATED_FILES))
+LOCAL_SRC_FILES := $(VULKAN_UTIL_FILES))
+
vulkan_api_xml = $(MESA_TOP)/src/vulkan/registry/vk.xml
$(LOCAL_GENERATED_SOURCES): $(MESA_TOP)/src/vulkan/util/gen_enum_to_str.py $(vulkan_api_xml)
diff --git a/src/vulkan/Makefile.am b/src/vulkan/Makefile.am
index 71fb2d97bba..c897a07d6a8 100644
--- a/src/vulkan/Makefile.am
+++ b/src/vulkan/Makefile.am
@@ -11,6 +11,10 @@ PYTHON_GEN = $(AM_V_GEN)$(PYTHON2) $(PYTHON_FLAGS)
EXTRA_DIST = \
util/gen_enum_to_str.py
+VULKAN_UTIL_SOURCES = \
+ $(VULKAN_UTIL_FILES) \
+ $(VULKAN_UTIL_GENERATED_FILES)
+
BUILT_SOURCES = \
$(VULKAN_UTIL_GENERATED_FILES)
@@ -18,12 +22,13 @@ util/vk_enum_to_str.c util/vk_enum_to_str.h: util/gen_enum_to_str.py $(vulkan_ap
$(MKDIR_GEN)
$(PYTHON_GEN) $(srcdir)/util/gen_enum_to_str.py --xml $(vulkan_api_xml) --outdir $(top_builddir)/src/vulkan/util
-libvulkan_util_la_SOURCES = $(VULKAN_UTIL_GENERATED_FILES)
+libvulkan_util_la_SOURCES = $(VULKAN_UTIL_SOURCES)
AM_CPPFLAGS = \
$(DEFINES) \
-I$(top_srcdir)/include \
-I$(top_srcdir)/src \
+ -I$(top_srcdir)/src/vulkan/util \
-I$(top_srcdir)/src/gallium/auxiliary \
-I$(top_srcdir)/src/gallium/include
diff --git a/src/vulkan/Makefile.sources b/src/vulkan/Makefile.sources
index 63f4ac11620..9962c1b077d 100644
--- a/src/vulkan/Makefile.sources
+++ b/src/vulkan/Makefile.sources
@@ -15,6 +15,10 @@ VULKAN_WSI_X11_FILES := \
wsi/wsi_common_x11.c \
wsi/wsi_common_x11.h
+VULKAN_UTIL_FILES := \
+ util/vk_alloc.h \
+ util/vk_util.h
+
VULKAN_UTIL_GENERATED_FILES := \
util/vk_enum_to_str.c \
util/vk_enum_to_str.h
diff --git a/src/vulkan/util/vk_alloc.h b/src/vulkan/util/vk_alloc.h
new file mode 100644
index 00000000000..29150218269
--- /dev/null
+++ b/src/vulkan/util/vk_alloc.h
@@ -0,0 +1,94 @@
+/*
+ * Copyright © 2015 Intel Corporation
+ *
+ * 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 (including the next
+ * paragraph) 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.
+ */
+#ifndef VK_ALLOC_H
+#define VK_ALLOC_H
+
+/* common allocation inlines for vulkan drivers */
+
+#include <string.h>
+#include <vulkan/vulkan.h>
+
+static inline void *
+vk_alloc(const VkAllocationCallbacks *alloc,
+ size_t size, size_t align,
+ VkSystemAllocationScope scope)
+{
+ return alloc->pfnAllocation(alloc->pUserData, size, align, scope);
+}
+
+static inline void *
+vk_realloc(const VkAllocationCallbacks *alloc,
+ void *ptr, size_t size, size_t align,
+ VkSystemAllocationScope scope)
+{
+ return alloc->pfnReallocation(alloc->pUserData, ptr, size, align, scope);
+}
+
+static inline void
+vk_free(const VkAllocationCallbacks *alloc, void *data)
+{
+ if (data == NULL)
+ return;
+
+ alloc->pfnFree(alloc->pUserData, data);
+}
+
+static inline void *
+vk_alloc2(const VkAllocationCallbacks *parent_alloc,
+ const VkAllocationCallbacks *alloc,
+ size_t size, size_t align,
+ VkSystemAllocationScope scope)
+{
+ if (alloc)
+ return vk_alloc(alloc, size, align, scope);
+ else
+ return vk_alloc(parent_alloc, size, align, scope);
+}
+
+static inline void *
+vk_zalloc2(const VkAllocationCallbacks *parent_alloc,
+ const VkAllocationCallbacks *alloc,
+ size_t size, size_t align,
+ VkSystemAllocationScope scope)
+{
+ void *mem = vk_alloc2(parent_alloc, alloc, size, align, scope);
+ if (mem == NULL)
+ return NULL;
+
+ memset(mem, 0, size);
+
+ return mem;
+}
+
+static inline void
+vk_free2(const VkAllocationCallbacks *parent_alloc,
+ const VkAllocationCallbacks *alloc,
+ void *data)
+{
+ if (alloc)
+ vk_free(alloc, data);
+ else
+ vk_free(parent_alloc, data);
+}
+
+#endif
diff --git a/src/vulkan/util/vk_util.h b/src/vulkan/util/vk_util.h
new file mode 100644
index 00000000000..5ff1f00195c
--- /dev/null
+++ b/src/vulkan/util/vk_util.h
@@ -0,0 +1,200 @@
+/*
+ * Copyright © 2017 Intel Corporation
+ *
+ * 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 (including the next
+ * paragraph) 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.
+ */
+#ifndef VK_UTIL_H
+#define VK_UTIL_H
+
+/* common inlines and macros for vulkan drivers */
+
+#include <vulkan/vulkan.h>
+
+struct vk_struct_common {
+ VkStructureType sType;
+ struct vk_struct_common *pNext;
+};
+
+#define vk_foreach_struct(__iter, __start) \
+ for (struct vk_struct_common *__iter = (struct vk_struct_common *)(__start); \
+ __iter; __iter = __iter->pNext)
+
+#define vk_foreach_struct_const(__iter, __start) \
+ for (const struct vk_struct_common *__iter = (const struct vk_struct_common *)(__start); \
+ __iter; __iter = __iter->pNext)
+
+/**
+ * A wrapper for a Vulkan output array. A Vulkan output array is one that
+ * follows the convention of the parameters to
+ * vkGetPhysicalDeviceQueueFamilyProperties().
+ *
+ * Example Usage:
+ *
+ * VkResult
+ * vkGetPhysicalDeviceQueueFamilyProperties(
+ * VkPhysicalDevice physicalDevice,
+ * uint32_t* pQueueFamilyPropertyCount,
+ * VkQueueFamilyProperties* pQueueFamilyProperties)
+ * {
+ * VK_OUTARRAY_MAKE(props, pQueueFamilyProperties,
+ * pQueueFamilyPropertyCount);
+ *
+ * vk_outarray_append(&props, p) {
+ * p->queueFlags = ...;
+ * p->queueCount = ...;
+ * }
+ *
+ * vk_outarray_append(&props, p) {
+ * p->queueFlags = ...;
+ * p->queueCount = ...;
+ * }
+ *
+ * return vk_outarray_status(&props);
+ * }
+ */
+struct __vk_outarray {
+ /** May be null. */
+ void *data;
+
+ /**
+ * Capacity, in number of elements. Capacity is unlimited (UINT32_MAX) if
+ * data is null.
+ */
+ uint32_t cap;
+
+ /**
+ * Count of elements successfully written to the array. Every write is
+ * considered successful if data is null.
+ */
+ uint32_t *filled_len;
+
+ /**
+ * Count of elements that would have been written to the array if its
+ * capacity were sufficient. Vulkan functions often return VK_INCOMPLETE
+ * when `*filled_len < wanted_len`.
+ */
+ uint32_t wanted_len;
+};
+
+static inline void
+__vk_outarray_init(struct __vk_outarray *a,
+ void *data, uint32_t *restrict len)
+{
+ a->data = data;
+ a->cap = *len;
+ a->filled_len = len;
+ *a->filled_len = 0;
+ a->wanted_len = 0;
+
+ if (a->data == NULL)
+ a->cap = UINT32_MAX;
+}
+
+static inline VkResult
+__vk_outarray_status(const struct __vk_outarray *a)
+{
+ if (*a->filled_len < a->wanted_len)
+ return VK_INCOMPLETE;
+ else
+ return VK_SUCCESS;
+}
+
+static inline void *
+__vk_outarray_next(struct __vk_outarray *a, size_t elem_size)
+{
+ void *p = NULL;
+
+ a->wanted_len += 1;
+
+ if (*a->filled_len >= a->cap)
+ return NULL;
+
+ if (a->data != NULL)
+ p = a->data + (*a->filled_len) * elem_size;
+
+ *a->filled_len += 1;
+
+ return p;
+}
+
+#define vk_outarray(elem_t) \
+ struct { \
+ struct __vk_outarray base; \
+ elem_t meta[]; \
+ }
+
+#define vk_outarray_typeof_elem(a) __typeof__((a)->meta[0])
+#define vk_outarray_sizeof_elem(a) sizeof((a)->meta[0])
+
+#define vk_outarray_init(a, data, len) \
+ __vk_outarray_init(&(a)->base, (data), (len))
+
+#define VK_OUTARRAY_MAKE(name, data, len) \
+ vk_outarray(__typeof__((data)[0])) name; \
+ vk_outarray_init(&name, (data), (len))
+
+#define vk_outarray_status(a) \
+ __vk_outarray_status(&(a)->base)
+
+#define vk_outarray_next(a) \
+ ((vk_outarray_typeof_elem(a) *) \
+ __vk_outarray_next(&(a)->base, vk_outarray_sizeof_elem(a)))
+
+/**
+ * Append to a Vulkan output array.
+ *
+ * This is a block-based macro. For example:
+ *
+ * vk_outarray_append(&a, elem) {
+ * elem->foo = ...;
+ * elem->bar = ...;
+ * }
+ *
+ * The array `a` has type `vk_outarray(elem_t) *`. It is usually declared with
+ * VK_OUTARRAY_MAKE(). The variable `elem` is block-scoped and has type
+ * `elem_t *`.
+ *
+ * The macro unconditionally increments the array's `wanted_len`. If the array
+ * is not full, then the macro also increment its `filled_len` and then
+ * executes the block. When the block is executed, `elem` is non-null and
+ * points to the newly appended element.
+ */
+#define vk_outarray_append(a, elem) \
+ for (vk_outarray_typeof_elem(a) *elem = vk_outarray_next(a); \
+ elem != NULL; elem = NULL)
+
+static inline void *
+__vk_find_struct(void *start, VkStructureType sType)
+{
+ vk_foreach_struct(s, start) {
+ if (s->sType == sType)
+ return s;
+ }
+
+ return NULL;
+}
+
+#define vk_find_struct(__start, __sType) \
+ __vk_find_struct((__start), VK_STRUCTURE_TYPE_##__sType)
+
+#define vk_find_struct_const(__start, __sType) \
+ (const void *)__vk_find_struct((void *)(__start), VK_STRUCTURE_TYPE_##__sType)
+
+#endif /* VK_UTIL_H */
diff --git a/src/vulkan/wsi/wsi_common.h b/src/vulkan/wsi/wsi_common.h
index 8aee9c73513..8166b7dd344 100644
--- a/src/vulkan/wsi/wsi_common.h
+++ b/src/vulkan/wsi/wsi_common.h
@@ -26,7 +26,7 @@
#include <stdint.h>
#include <stdbool.h>
-#include "util/vk_alloc.h"
+#include "vk_alloc.h"
#include <vulkan/vulkan.h>
#include <vulkan/vk_icd.h>
diff --git a/src/vulkan/wsi/wsi_common_wayland.c b/src/vulkan/wsi/wsi_common_wayland.c
index 644ed62b414..dd283a12111 100644
--- a/src/vulkan/wsi/wsi_common_wayland.c
+++ b/src/vulkan/wsi/wsi_common_wayland.c
@@ -31,7 +31,7 @@
#include <string.h>
#include <pthread.h>
-#include "util/vk_util.h"
+#include "vk_util.h"
#include "wsi_common_wayland.h"
#include "wayland-drm-client-protocol.h"
diff --git a/src/vulkan/wsi/wsi_common_x11.c b/src/vulkan/wsi/wsi_common_x11.c
index 5be56f1232f..ecdaf914344 100644
--- a/src/vulkan/wsi/wsi_common_x11.c
+++ b/src/vulkan/wsi/wsi_common_x11.c
@@ -38,7 +38,7 @@
#include <xf86drm.h>
#include "util/hash_table.h"
-#include "util/vk_util.h"
+#include "vk_util.h"
#include "wsi_common.h"
#include "wsi_common_x11.h"
#include "wsi_common_queue.h"