[Mesa-dev] [PATCH 1/2] vulkan: deduplicate the util code

Grazvydas Ignotas notasas at gmail.com
Sun Dec 11 17:05:35 UTC 2016


anv_util.c and radv_util.c are essentially duplicates, so create a new
vk_util.c to be shared by both vulkan drivers. Some other content from
anv_private.h and radv_private.h was moved to vk_util.h too.

Signed-off-by: Grazvydas Ignotas <notasas at gmail.com>
---
no commit access

 configure.ac                      |   1 +
 src/Makefile.am                   |   1 +
 src/amd/vulkan/Makefile.am        |   1 +
 src/amd/vulkan/Makefile.sources   |   1 -
 src/amd/vulkan/radv_device.c      |   2 +-
 src/amd/vulkan/radv_private.h     |  60 +-----------------
 src/amd/vulkan/radv_util.c        | 130 --------------------------------------
 src/intel/vulkan/Makefile.am      |   1 +
 src/intel/vulkan/Makefile.sources |   1 -
 src/intel/vulkan/anv_private.h    |  60 +-----------------
 src/intel/vulkan/anv_util.c       | 127 -------------------------------------
 src/vulkan/Makefile.am            |  17 +++++
 src/vulkan/Makefile.sources       |   4 ++
 src/vulkan/vk_util.c              | 105 ++++++++++++++++++++++++++++++
 src/vulkan/vk_util.h              |  94 +++++++++++++++++++++++++++
 15 files changed, 229 insertions(+), 376 deletions(-)
 delete mode 100644 src/amd/vulkan/radv_util.c
 delete mode 100644 src/intel/vulkan/anv_util.c
 create mode 100644 src/vulkan/Makefile.am
 create mode 100644 src/vulkan/Makefile.sources
 create mode 100644 src/vulkan/vk_util.c
 create mode 100644 src/vulkan/vk_util.h

diff --git a/configure.ac b/configure.ac
index 799f5eb..715a0ff 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2855,6 +2855,7 @@ AC_CONFIG_FILES([Makefile
 		src/mesa/main/tests/Makefile
 		src/util/Makefile
 		src/util/tests/hash_table/Makefile
+		src/vulkan/Makefile
 		src/vulkan/wsi/Makefile])
 
 AC_OUTPUT
diff --git a/src/Makefile.am b/src/Makefile.am
index ad54356..2c36dbd 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -116,6 +116,7 @@ SUBDIRS += intel/tools
 endif
 
 if HAVE_VULKAN_COMMON
+SUBDIRS += vulkan
 SUBDIRS += vulkan/wsi
 endif
 
diff --git a/src/amd/vulkan/Makefile.am b/src/amd/vulkan/Makefile.am
index 6e184c0..2f746dd 100644
--- a/src/amd/vulkan/Makefile.am
+++ b/src/amd/vulkan/Makefile.am
@@ -94,6 +94,7 @@ libvulkan_common_la_SOURCES = $(VULKAN_SOURCES)
 
 VULKAN_LIB_DEPS += \
 	libvulkan_common.la \
+	$(top_builddir)/src/vulkan/libvulkan_shared.la \
 	$(top_builddir)/src/vulkan/wsi/libvulkan_wsi.la \
 	$(top_builddir)/src/amd/common/libamd_common.la \
 	$(top_builddir)/src/amd/addrlib/libamdgpu_addrlib.la \
diff --git a/src/amd/vulkan/Makefile.sources b/src/amd/vulkan/Makefile.sources
index 425a00f..139209b 100644
--- a/src/amd/vulkan/Makefile.sources
+++ b/src/amd/vulkan/Makefile.sources
@@ -56,7 +56,6 @@ VULKAN_FILES := \
 	radv_private.h \
 	radv_radeon_winsys.h \
 	radv_query.c \
-	radv_util.c \
 	radv_util.h \
 	radv_wsi.c \
 	si_cmd_buffer.c \
diff --git a/src/amd/vulkan/radv_device.c b/src/amd/vulkan/radv_device.c
index 75b7af1..3ecc24f 100644
--- a/src/amd/vulkan/radv_device.c
+++ b/src/amd/vulkan/radv_device.c
@@ -829,7 +829,7 @@ VkResult radv_QueueSubmit(
 						   pSubmits[i].commandBufferCount,
 						   can_patch, base_fence);
 		if (ret)
-			radv_loge("failed to submit CS %d\n", i);
+			vk_loge("failed to submit CS %d\n", i);
 		free(cs_array);
 	}
 
diff --git a/src/amd/vulkan/radv_private.h b/src/amd/vulkan/radv_private.h
index 3d4b111..0abbe0b 100644
--- a/src/amd/vulkan/radv_private.h
+++ b/src/amd/vulkan/radv_private.h
@@ -49,6 +49,7 @@
 #include "util/list.h"
 #include "util/vk_alloc.h"
 #include "main/macros.h"
+#include "vulkan/vk_util.h"
 
 #include "radv_radeon_winsys.h"
 #include "ac_binary.h"
@@ -100,36 +101,12 @@ enum radv_mem_type {
 	RADV_MEM_TYPE_COUNT
 };
 
-#define radv_noreturn __attribute__((__noreturn__))
-#define radv_printflike(a, b) __attribute__((__format__(__printf__, a, b)))
-
-static inline uint32_t
-align_u32(uint32_t v, uint32_t a)
-{
-	assert(a != 0 && a == (a & -a));
-	return (v + a - 1) & ~(a - 1);
-}
-
 static inline uint32_t
 align_u32_npot(uint32_t v, uint32_t a)
 {
 	return (v + a - 1) / a * a;
 }
 
-static inline uint64_t
-align_u64(uint64_t v, uint64_t a)
-{
-	assert(a != 0 && a == (a & -a));
-	return (v + a - 1) & ~(a - 1);
-}
-
-static inline int32_t
-align_i32(int32_t v, int32_t a)
-{
-	assert(a != 0 && a == (a & -a));
-	return (v + a - 1) & ~(a - 1);
-}
-
 /** Alignment must be a power of 2. */
 static inline bool
 radv_is_aligned(uintmax_t n, uintmax_t a)
@@ -182,38 +159,8 @@ radv_clear_mask(uint32_t *inout_mask, uint32_t clear_mask)
 	}
 }
 
-#define for_each_bit(b, dword)                          \
-	for (uint32_t __dword = (dword);		\
-	     (b) = __builtin_ffs(__dword) - 1, __dword;	\
-	     __dword &= ~(1 << (b)))
-
-#define typed_memcpy(dest, src, count) ({				\
-			STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
-			memcpy((dest), (src), (count) * sizeof(*(src))); \
-		})
-
 #define zero(x) (memset(&(x), 0, sizeof(x)))
 
-/* Whenever we generate an error, pass it through this function. Useful for
- * debugging, where we can break on it. Only call at error site, not when
- * propagating errors. Might be useful to plug in a stack trace here.
- */
-
-VkResult __vk_errorf(VkResult error, const char *file, int line, const char *format, ...);
-
-#ifdef DEBUG
-#define vk_error(error) __vk_errorf(error, __FILE__, __LINE__, NULL);
-#define vk_errorf(error, format, ...) __vk_errorf(error, __FILE__, __LINE__, format, ## __VA_ARGS__);
-#else
-#define vk_error(error) error
-#define vk_errorf(error, format, ...) error
-#endif
-
-void __radv_finishme(const char *file, int line, const char *format, ...)
-	radv_printflike(3, 4);
-void radv_loge(const char *format, ...) radv_printflike(1, 2);
-void radv_loge_v(const char *format, va_list va);
-
 /**
  * Print a FINISHME message, including its source location.
  */
@@ -221,7 +168,7 @@ void radv_loge_v(const char *format, va_list va);
 	do { \
 		static bool reported = false; \
 		if (!reported) { \
-			__radv_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__); \
+			__vk_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__); \
 			reported = true; \
 		} \
 	} while (0)
@@ -236,9 +183,6 @@ void radv_loge_v(const char *format, va_list va);
 #define radv_assert(x)
 #endif
 
-void radv_abortf(const char *format, ...) radv_noreturn radv_printflike(1, 2);
-void radv_abortfv(const char *format, va_list va) radv_noreturn;
-
 #define stub_return(v)					\
 	do {						\
 		radv_finishme("stub %s", __func__);	\
diff --git a/src/amd/vulkan/radv_util.c b/src/amd/vulkan/radv_util.c
deleted file mode 100644
index 8c7a948..0000000
--- a/src/amd/vulkan/radv_util.c
+++ /dev/null
@@ -1,130 +0,0 @@
-/*
- * 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.
- */
-
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <assert.h>
-
-#include "radv_private.h"
-
-#include "util/u_math.h"
-
-/** Log an error message.  */
-void radv_printflike(1, 2)
-	radv_loge(const char *format, ...)
-{
-	va_list va;
-
-	va_start(va, format);
-	radv_loge_v(format, va);
-	va_end(va);
-}
-
-/** \see radv_loge() */
-void
-radv_loge_v(const char *format, va_list va)
-{
-	fprintf(stderr, "vk: error: ");
-	vfprintf(stderr, format, va);
-	fprintf(stderr, "\n");
-}
-
-void radv_printflike(3, 4)
-	__radv_finishme(const char *file, int line, const char *format, ...)
-{
-	va_list ap;
-	char buffer[256];
-
-	va_start(ap, format);
-	vsnprintf(buffer, sizeof(buffer), format, ap);
-	va_end(ap);
-
-	fprintf(stderr, "%s:%d: FINISHME: %s\n", file, line, buffer);
-}
-
-void radv_noreturn radv_printflike(1, 2)
-	radv_abortf(const char *format, ...)
-{
-	va_list va;
-
-	va_start(va, format);
-	radv_abortfv(format, va);
-	va_end(va);
-}
-
-void radv_noreturn
-radv_abortfv(const char *format, va_list va)
-{
-	fprintf(stderr, "vk: error: ");
-	vfprintf(stderr, format, va);
-	fprintf(stderr, "\n");
-	abort();
-}
-
-VkResult
-__vk_errorf(VkResult error, const char *file, int line, const char *format, ...)
-{
-	va_list ap;
-	char buffer[256];
-
-#define ERROR_CASE(error) case error: error_str = #error; break;
-
-	const char *error_str;
-	switch ((int32_t)error) {
-
-		/* Core errors */
-		ERROR_CASE(VK_ERROR_OUT_OF_HOST_MEMORY)
-			ERROR_CASE(VK_ERROR_OUT_OF_DEVICE_MEMORY)
-			ERROR_CASE(VK_ERROR_INITIALIZATION_FAILED)
-			ERROR_CASE(VK_ERROR_DEVICE_LOST)
-			ERROR_CASE(VK_ERROR_MEMORY_MAP_FAILED)
-			ERROR_CASE(VK_ERROR_LAYER_NOT_PRESENT)
-			ERROR_CASE(VK_ERROR_EXTENSION_NOT_PRESENT)
-			ERROR_CASE(VK_ERROR_INCOMPATIBLE_DRIVER)
-
-			/* Extension errors */
-			ERROR_CASE(VK_ERROR_OUT_OF_DATE_KHR)
-
-	default:
-		assert(!"Unknown error");
-		error_str = "unknown error";
-	}
-
-#undef ERROR_CASE
-
-	if (format) {
-		va_start(ap, format);
-		vsnprintf(buffer, sizeof(buffer), format, ap);
-		va_end(ap);
-
-		fprintf(stderr, "%s:%d: %s (%s)\n", file, line, buffer, error_str);
-	} else {
-		fprintf(stderr, "%s:%d: %s\n", file, line, error_str);
-	}
-
-	return error;
-}
-
diff --git a/src/intel/vulkan/Makefile.am b/src/intel/vulkan/Makefile.am
index df7645f..4b42dbd 100644
--- a/src/intel/vulkan/Makefile.am
+++ b/src/intel/vulkan/Makefile.am
@@ -123,6 +123,7 @@ libvulkan_common_la_SOURCES = $(VULKAN_SOURCES)
 
 VULKAN_LIB_DEPS += \
 	libvulkan_common.la \
+	$(top_builddir)/src/vulkan/libvulkan_shared.la \
 	$(top_builddir)/src/vulkan/wsi/libvulkan_wsi.la \
 	$(top_builddir)/src/mesa/drivers/dri/i965/libi965_compiler.la \
 	$(top_builddir)/src/compiler/nir/libnir.la \
diff --git a/src/intel/vulkan/Makefile.sources b/src/intel/vulkan/Makefile.sources
index bd78805..15b0690 100644
--- a/src/intel/vulkan/Makefile.sources
+++ b/src/intel/vulkan/Makefile.sources
@@ -41,7 +41,6 @@ VULKAN_FILES := \
 	anv_pipeline_cache.c \
 	anv_private.h \
 	anv_query.c \
-	anv_util.c \
 	anv_wsi.c \
 	vk_format_info.h
 
diff --git a/src/intel/vulkan/anv_private.h b/src/intel/vulkan/anv_private.h
index 9e3b72e..1f412aa 100644
--- a/src/intel/vulkan/anv_private.h
+++ b/src/intel/vulkan/anv_private.h
@@ -48,6 +48,7 @@
 #include "util/list.h"
 #include "util/u_vector.h"
 #include "util/vk_alloc.h"
+#include "vulkan/vk_util.h"
 
 /* Pre-declarations needed for WSI entrypoints */
 struct wl_surface;
@@ -82,36 +83,12 @@ extern "C" {
 #define MAX_IMAGES 8
 #define MAX_SAMPLES_LOG2 4 /* SKL supports 16 samples */
 
-#define anv_noreturn __attribute__((__noreturn__))
-#define anv_printflike(a, b) __attribute__((__format__(__printf__, a, b)))
-
 static inline uint32_t
 align_down_npot_u32(uint32_t v, uint32_t a)
 {
    return v - (v % a);
 }
 
-static inline uint32_t
-align_u32(uint32_t v, uint32_t a)
-{
-   assert(a != 0 && a == (a & -a));
-   return (v + a - 1) & ~(a - 1);
-}
-
-static inline uint64_t
-align_u64(uint64_t v, uint64_t a)
-{
-   assert(a != 0 && a == (a & -a));
-   return (v + a - 1) & ~(a - 1);
-}
-
-static inline int32_t
-align_i32(int32_t v, int32_t a)
-{
-   assert(a != 0 && a == (a & -a));
-   return (v + a - 1) & ~(a - 1);
-}
-
 /** Alignment must be a power of 2. */
 static inline bool
 anv_is_aligned(uintmax_t n, uintmax_t a)
@@ -166,36 +143,6 @@ vk_to_isl_color(VkClearColorValue color)
    };
 }
 
-#define for_each_bit(b, dword)                          \
-   for (uint32_t __dword = (dword);                     \
-        (b) = __builtin_ffs(__dword) - 1, __dword;      \
-        __dword &= ~(1 << (b)))
-
-#define typed_memcpy(dest, src, count) ({ \
-   STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
-   memcpy((dest), (src), (count) * sizeof(*(src))); \
-})
-
-/* Whenever we generate an error, pass it through this function. Useful for
- * debugging, where we can break on it. Only call at error site, not when
- * propagating errors. Might be useful to plug in a stack trace here.
- */
-
-VkResult __vk_errorf(VkResult error, const char *file, int line, const char *format, ...);
-
-#ifdef DEBUG
-#define vk_error(error) __vk_errorf(error, __FILE__, __LINE__, NULL);
-#define vk_errorf(error, format, ...) __vk_errorf(error, __FILE__, __LINE__, format, ## __VA_ARGS__);
-#else
-#define vk_error(error) error
-#define vk_errorf(error, format, ...) error
-#endif
-
-void __anv_finishme(const char *file, int line, const char *format, ...)
-   anv_printflike(3, 4);
-void anv_loge(const char *format, ...) anv_printflike(1, 2);
-void anv_loge_v(const char *format, va_list va);
-
 /**
  * Print a FINISHME message, including its source location.
  */
@@ -203,7 +150,7 @@ void anv_loge_v(const char *format, va_list va);
    do { \
       static bool reported = false; \
       if (!reported) { \
-         __anv_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__); \
+         __vk_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__); \
          reported = true; \
       } \
    } while (0)
@@ -228,9 +175,6 @@ void anv_loge_v(const char *format, va_list va);
 #define anv_validate if (0)
 #endif
 
-void anv_abortf(const char *format, ...) anv_noreturn anv_printflike(1, 2);
-void anv_abortfv(const char *format, va_list va) anv_noreturn;
-
 #define stub_return(v) \
    do { \
       anv_finishme("stub %s", __func__); \
diff --git a/src/intel/vulkan/anv_util.c b/src/intel/vulkan/anv_util.c
deleted file mode 100644
index 2972cd2..0000000
--- a/src/intel/vulkan/anv_util.c
+++ /dev/null
@@ -1,127 +0,0 @@
-/*
- * 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.
- */
-
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <assert.h>
-
-#include "anv_private.h"
-
-/** Log an error message.  */
-void anv_printflike(1, 2)
-anv_loge(const char *format, ...)
-{
-   va_list va;
-
-   va_start(va, format);
-   anv_loge_v(format, va);
-   va_end(va);
-}
-
-/** \see anv_loge() */
-void
-anv_loge_v(const char *format, va_list va)
-{
-   fprintf(stderr, "vk: error: ");
-   vfprintf(stderr, format, va);
-   fprintf(stderr, "\n");
-}
-
-void anv_printflike(3, 4)
-__anv_finishme(const char *file, int line, const char *format, ...)
-{
-   va_list ap;
-   char buffer[256];
-
-   va_start(ap, format);
-   vsnprintf(buffer, sizeof(buffer), format, ap);
-   va_end(ap);
-
-   fprintf(stderr, "%s:%d: FINISHME: %s\n", file, line, buffer);
-}
-
-void anv_noreturn anv_printflike(1, 2)
-anv_abortf(const char *format, ...)
-{
-   va_list va;
-
-   va_start(va, format);
-   anv_abortfv(format, va);
-   va_end(va);
-}
-
-void anv_noreturn
-anv_abortfv(const char *format, va_list va)
-{
-   fprintf(stderr, "vk: error: ");
-   vfprintf(stderr, format, va);
-   fprintf(stderr, "\n");
-   abort();
-}
-
-VkResult
-__vk_errorf(VkResult error, const char *file, int line, const char *format, ...)
-{
-   va_list ap;
-   char buffer[256];
-
-#define ERROR_CASE(error) case error: error_str = #error; break;
-
-   const char *error_str;
-   switch ((int32_t)error) {
-
-   /* Core errors */
-   ERROR_CASE(VK_ERROR_OUT_OF_HOST_MEMORY)
-   ERROR_CASE(VK_ERROR_OUT_OF_DEVICE_MEMORY)
-   ERROR_CASE(VK_ERROR_INITIALIZATION_FAILED)
-   ERROR_CASE(VK_ERROR_DEVICE_LOST)
-   ERROR_CASE(VK_ERROR_MEMORY_MAP_FAILED)
-   ERROR_CASE(VK_ERROR_LAYER_NOT_PRESENT)
-   ERROR_CASE(VK_ERROR_EXTENSION_NOT_PRESENT)
-   ERROR_CASE(VK_ERROR_INCOMPATIBLE_DRIVER)
-
-   /* Extension errors */
-   ERROR_CASE(VK_ERROR_OUT_OF_DATE_KHR)
-
-   default:
-      assert(!"Unknown error");
-      error_str = "unknown error";
-   }
-
-#undef ERROR_CASE
-
-   if (format) {
-      va_start(ap, format);
-      vsnprintf(buffer, sizeof(buffer), format, ap);
-      va_end(ap);
-
-      fprintf(stderr, "%s:%d: %s (%s)\n", file, line, buffer, error_str);
-   } else {
-      fprintf(stderr, "%s:%d: %s\n", file, line, error_str);
-   }
-
-   return error;
-}
diff --git a/src/vulkan/Makefile.am b/src/vulkan/Makefile.am
new file mode 100644
index 0000000..cf21b2c
--- /dev/null
+++ b/src/vulkan/Makefile.am
@@ -0,0 +1,17 @@
+
+include Makefile.sources
+
+noinst_LTLIBRARIES = libvulkan_shared.la
+
+AM_CPPFLAGS = \
+	$(DEFINES) \
+	-I$(top_srcdir)/include \
+	-I$(top_srcdir)/src
+
+AM_CFLAGS = \
+	$(VISIBILITY_CFLAGS)
+
+VULKAN_SHARED_SOURCES = \
+	$(VULKAN_SHARED_FILES)
+
+libvulkan_shared_la_SOURCES = $(VULKAN_SHARED_SOURCES)
diff --git a/src/vulkan/Makefile.sources b/src/vulkan/Makefile.sources
new file mode 100644
index 0000000..61059c7
--- /dev/null
+++ b/src/vulkan/Makefile.sources
@@ -0,0 +1,4 @@
+
+VULKAN_SHARED_FILES := \
+	vk_util.c \
+	vk_util.h
diff --git a/src/vulkan/vk_util.c b/src/vulkan/vk_util.c
new file mode 100644
index 0000000..5d38117
--- /dev/null
+++ b/src/vulkan/vk_util.c
@@ -0,0 +1,105 @@
+/*
+ * 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.
+ */
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <assert.h>
+
+#include "vk_util.h"
+
+/** Log an error message.  */
+void vk_printflike(1, 2)
+vk_loge(const char *format, ...)
+{
+   va_list va;
+
+   va_start(va, format);
+   vk_loge_v(format, va);
+   va_end(va);
+}
+
+/** \see vk_loge() */
+void
+vk_loge_v(const char *format, va_list va)
+{
+   fprintf(stderr, "vk: error: ");
+   vfprintf(stderr, format, va);
+   fprintf(stderr, "\n");
+}
+
+void vk_printflike(3, 4)
+__vk_finishme(const char *file, int line, const char *format, ...)
+{
+   va_list ap;
+   char buffer[256];
+
+   va_start(ap, format);
+   vsnprintf(buffer, sizeof(buffer), format, ap);
+   va_end(ap);
+
+   fprintf(stderr, "%s:%d: FINISHME: %s\n", file, line, buffer);
+}
+
+VkResult
+__vk_errorf(VkResult error, const char *file, int line, const char *format, ...)
+{
+   va_list ap;
+   char buffer[256];
+
+#define ERROR_CASE(error) case error: error_str = #error; break;
+
+   const char *error_str;
+   switch ((int32_t)error) {
+
+   /* Core errors */
+   ERROR_CASE(VK_ERROR_OUT_OF_HOST_MEMORY)
+   ERROR_CASE(VK_ERROR_OUT_OF_DEVICE_MEMORY)
+   ERROR_CASE(VK_ERROR_INITIALIZATION_FAILED)
+   ERROR_CASE(VK_ERROR_DEVICE_LOST)
+   ERROR_CASE(VK_ERROR_MEMORY_MAP_FAILED)
+   ERROR_CASE(VK_ERROR_LAYER_NOT_PRESENT)
+   ERROR_CASE(VK_ERROR_EXTENSION_NOT_PRESENT)
+   ERROR_CASE(VK_ERROR_INCOMPATIBLE_DRIVER)
+
+   /* Extension errors */
+   ERROR_CASE(VK_ERROR_OUT_OF_DATE_KHR)
+
+   default:
+      assert(!"Unknown error");
+      error_str = "unknown error";
+   }
+
+#undef ERROR_CASE
+
+   if (format) {
+      va_start(ap, format);
+      vsnprintf(buffer, sizeof(buffer), format, ap);
+      va_end(ap);
+
+      fprintf(stderr, "%s:%d: %s (%s)\n", file, line, buffer, error_str);
+   } else {
+      fprintf(stderr, "%s:%d: %s\n", file, line, error_str);
+   }
+
+   return error;
+}
diff --git a/src/vulkan/vk_util.h b/src/vulkan/vk_util.h
new file mode 100644
index 0000000..c384838
--- /dev/null
+++ b/src/vulkan/vk_util.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_UTIL_H
+#define VK_UTIL_H
+
+#include <string.h>
+#include <assert.h>
+#include <vulkan/vulkan.h>
+
+#include "util/macros.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+static inline uint32_t
+align_u32(uint32_t v, uint32_t a)
+{
+   assert(a != 0 && a == (a & -a));
+   return (v + a - 1) & ~(a - 1);
+}
+
+static inline uint64_t
+align_u64(uint64_t v, uint64_t a)
+{
+   assert(a != 0 && a == (a & -a));
+   return (v + a - 1) & ~(a - 1);
+}
+
+static inline int32_t
+align_i32(int32_t v, int32_t a)
+{
+   assert(a != 0 && a == (a & -a));
+   return (v + a - 1) & ~(a - 1);
+}
+
+#define for_each_bit(b, dword)                          \
+   for (uint32_t __dword = (dword);                     \
+        (b) = __builtin_ffs(__dword) - 1, __dword;      \
+        __dword &= ~(1 << (b)))
+
+#define typed_memcpy(dest, src, count) ({               \
+   STATIC_ASSERT(sizeof(*src) == sizeof(*dest));        \
+   memcpy((dest), (src), (count) * sizeof(*(src)));     \
+})
+
+#define vk_printflike(a, b) __attribute__((__format__(__printf__, a, b)))
+
+/* Whenever we generate an error, pass it through this function. Useful for
+ * debugging, where we can break on it. Only call at error site, not when
+ * propagating errors. Might be useful to plug in a stack trace here.
+ */
+
+VkResult __vk_errorf(VkResult error, const char *file, int line, const char *format, ...);
+
+#ifdef DEBUG
+#define vk_error(error) __vk_errorf(error, __FILE__, __LINE__, NULL);
+#define vk_errorf(error, format, ...) __vk_errorf(error, __FILE__, __LINE__, format, ## __VA_ARGS__);
+#else
+#define vk_error(error) error
+#define vk_errorf(error, format, ...) error
+#endif
+
+void __vk_finishme(const char *file, int line, const char *format, ...)
+   vk_printflike(3, 4);
+void vk_loge(const char *format, ...) vk_printflike(1, 2);
+void vk_loge_v(const char *format, va_list va);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* VK_UTIL_H */
-- 
2.7.4



More information about the mesa-dev mailing list