3 * Copyright (C) 2019 Matthew Waters <matthew@centricular.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
27 #define SPIRV_MAGIC_NUMBER_NE 0x07230203
28 #define SPIRV_MAGIC_NUMBER_OE 0x03022307
31 _vk_create_shader (GstVulkanDevice * device, gchar * code, gsize size,
38 VkShaderModuleCreateInfo info = {
39 .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
43 .pCode = (guint32 *) code
47 guint32 *new_code = NULL;
49 g_return_val_if_fail (size >= 4, NULL);
50 g_return_val_if_fail (size % 4 == 0, NULL);
52 first_word = ((guint32 *) code)[0];
53 g_return_val_if_fail (first_word == SPIRV_MAGIC_NUMBER_NE
54 || first_word == SPIRV_MAGIC_NUMBER_OE, NULL);
55 if (first_word == SPIRV_MAGIC_NUMBER_OE) {
56 /* endianess swap... */
57 guint32 *old_code = (guint32 *) code;
60 GST_DEBUG ("performaing endianess conversion on spirv shader of size %"
61 G_GSIZE_FORMAT, size);
62 new_code = g_new0 (guint32, size / 4);
64 for (i = 0; i < size / 4; i++) {
65 guint32 old = old_code[i];
68 new |= (old & 0xff) << 24;
69 new |= (old & 0xff00) << 8;
70 new |= (old & 0xff0000) >> 8;
71 new |= (old & 0xff000000) >> 24;
75 first_word = ((guint32 *) new_code)[0];
76 g_assert (first_word == SPIRV_MAGIC_NUMBER_NE);
78 info.pCode = new_code;
81 res = vkCreateShaderModule (device->device, &info, NULL, &ret);
83 if (gst_vulkan_error_to_g_error (res, error, "VkCreateShaderModule") < 0)