[Mesa-dev] [RFC PATCH 01/17] auxiliary: Introduce utilities for SPIR-V binaries
Pierre Moreau
pierre.morrow at free.fr
Wed May 3 21:56:49 UTC 2017
Signed-off-by: Pierre Moreau <pierre.morrow at free.fr>
---
src/gallium/auxiliary/Makefile.am | 1 +
src/gallium/auxiliary/Makefile.sources | 4 ++
src/gallium/auxiliary/spirv/spirv_utils.c | 75 +++++++++++++++++++++++++++
src/gallium/auxiliary/spirv/spirv_utils.h | 86 +++++++++++++++++++++++++++++++
4 files changed, 166 insertions(+)
create mode 100644 src/gallium/auxiliary/spirv/spirv_utils.c
create mode 100644 src/gallium/auxiliary/spirv/spirv_utils.h
diff --git a/src/gallium/auxiliary/Makefile.am b/src/gallium/auxiliary/Makefile.am
index dc4bd4a40c..d2530a1f90 100644
--- a/src/gallium/auxiliary/Makefile.am
+++ b/src/gallium/auxiliary/Makefile.am
@@ -19,6 +19,7 @@ AM_CXXFLAGS = \
libgallium_la_SOURCES = \
$(C_SOURCES) \
$(NIR_SOURCES) \
+ $(SPIRV_SOURCES) \
$(GENERATED_SOURCES)
if HAVE_LIBDRM
diff --git a/src/gallium/auxiliary/Makefile.sources b/src/gallium/auxiliary/Makefile.sources
index dbdb3ca815..f4817742ff 100644
--- a/src/gallium/auxiliary/Makefile.sources
+++ b/src/gallium/auxiliary/Makefile.sources
@@ -312,6 +312,10 @@ NIR_SOURCES := \
nir/tgsi_to_nir.c \
nir/tgsi_to_nir.h
+SPIRV_SOURCES := \
+ spirv/spirv_utils.c \
+ spirv/spirv_utils.h
+
VL_SOURCES := \
vl/vl_bicubic_filter.c \
vl/vl_bicubic_filter.h \
diff --git a/src/gallium/auxiliary/spirv/spirv_utils.c b/src/gallium/auxiliary/spirv/spirv_utils.c
new file mode 100644
index 0000000000..a2334d6909
--- /dev/null
+++ b/src/gallium/auxiliary/spirv/spirv_utils.c
@@ -0,0 +1,75 @@
+/**************************************************************************
+ *
+ * Copyright 2017 Pierre Moreau
+ * All Rights Reserved.
+ *
+ * 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, sub license, 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 NON-INFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS 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 "spirv_utils.h"
+
+#include "compiler/spirv/spirv.h"
+#include "util/u_math.h"
+
+spirv_word
+spirv_get_word(const char *binary, unsigned word_offset)
+{
+ return ((spirv_word *) binary)[word_offset];
+}
+
+void
+spirv_set_word(char *binary, unsigned word_offset, spirv_word word)
+{
+ ((spirv_word *) binary)[word_offset] = word;
+}
+
+const char *
+spirv_get_string(const char *binary, unsigned word_offset)
+{
+ return binary + word_offset * sizeof(spirv_word);
+}
+
+bool
+spirv_is_binary_spirv(const char *binary)
+{
+ const spirv_word first_word = spirv_get_word(binary, 0u);
+ const bool ret = (first_word == SpvMagicNumber) ||
+ (util_bswap32(first_word) == SpvMagicNumber);
+ return ret;
+}
+
+char *
+spirv_spirv_to_cpu(const char *binary, size_t length)
+{
+ spirv_word word = spirv_get_word(binary, 0u);
+ size_t i = 0;
+ char *cpu_endianness_binary = malloc(length);
+ if (word == SpvMagicNumber)
+ return memcpy(cpu_endianness_binary, binary, length);
+
+ for (i = 0; i < length; i += 4) {
+ word = spirv_get_word(binary, i);
+ spirv_set_word(cpu_endianness_binary, i, util_bswap32(word));
+ }
+
+ return cpu_endianness_binary;
+}
diff --git a/src/gallium/auxiliary/spirv/spirv_utils.h b/src/gallium/auxiliary/spirv/spirv_utils.h
new file mode 100644
index 0000000000..2db7f3b9dd
--- /dev/null
+++ b/src/gallium/auxiliary/spirv/spirv_utils.h
@@ -0,0 +1,86 @@
+/**************************************************************************
+ *
+ * Copyright 2017 Pierre Moreau
+ * All Rights Reserved.
+ *
+ * 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, sub license, 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 NON-INFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS 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 SPIRV_UTILS_H
+#define SPIRV_UTILS_H
+
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+
+#if defined __cplusplus
+extern "C" {
+#endif
+
+typedef uint32_t spirv_word;
+
+/**
+ * Return the word found at a given offset in a SPIR-V binary.
+ *
+ * The binary is assumed to have the same endianness as the CPU. Be sure to
+ * call `spirv_to_cpu()` to ensure that is the case.
+ */
+spirv_word
+spirv_get_word(const char *binary, unsigned word_offset);
+
+/**
+ * Set the word found at a given offset in a SPIR-V binary.
+ *
+ * The binary is assumed to have the same endianness as the CPU. Be sure to
+ * call `spirv_to_cpu()` to ensure that is the case.
+ */
+void
+spirv_set_word(char *binary, unsigned word_offset, spirv_word word);
+
+/**
+ * Return the string found at a given offset in a SPIR-V binary.
+ *
+ * The binary is assumed to have the same endianness as the CPU. Be sure to
+ * call `spirv_to_cpu()` to ensure that is the case.
+ */
+const char *
+spirv_get_string(const char *binary, unsigned word_offset);
+
+/**
+ * Return true whether the given binary starts with the SPIR-V magic number.
+ */
+bool
+spirv_is_binary_spirv(const char *binary);
+
+/**
+ * Convert if needed the binary to the endianness of the CPU.
+ *
+ * It is the responsability of the caller to free the returned pointer.
+ */
+char *
+spirv_spirv_to_cpu(const char *binary, size_t length);
+
+#if defined __cplusplus
+}
+#endif
+
+#endif /* SPIRV_UTILS_H */
--
2.12.2
More information about the mesa-dev
mailing list