documentation: fixed a heap o' typos
[platform/upstream/gstreamer.git] / ext / vulkan / vkshader.c
1 /*
2  * GStreamer
3  * Copyright (C) 2019 Matthew Waters <matthew@centricular.com>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "vkshader.h"
26
27 #define SPIRV_MAGIC_NUMBER_NE 0x07230203
28 #define SPIRV_MAGIC_NUMBER_OE 0x03022307
29
30 VkShaderModule
31 _vk_create_shader (GstVulkanDevice * device, gchar * code, gsize size,
32     GError ** error)
33 {
34   VkShaderModule ret;
35   VkResult res;
36
37   /* *INDENT-OFF* */
38   VkShaderModuleCreateInfo info = {
39       .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
40       .pNext = NULL,
41       .flags = 0,
42       .codeSize = size,
43       .pCode = (guint32 *) code
44   };
45   /* *INDENT-ON* */
46   guint32 first_word;
47   guint32 *new_code = NULL;
48
49   g_return_val_if_fail (size >= 4, NULL);
50   g_return_val_if_fail (size % 4 == 0, NULL);
51
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     /* endianness swap... */
57     guint32 *old_code = (guint32 *) code;
58     gsize i;
59
60     GST_DEBUG ("performaing endianness conversion on spirv shader of size %"
61         G_GSIZE_FORMAT, size);
62     new_code = g_new0 (guint32, size / 4);
63
64     for (i = 0; i < size / 4; i++) {
65       guint32 old = old_code[i];
66       guint32 new = 0;
67
68       new |= (old & 0xff) << 24;
69       new |= (old & 0xff00) << 8;
70       new |= (old & 0xff0000) >> 8;
71       new |= (old & 0xff000000) >> 24;
72       new_code[i] = new;
73     }
74
75     first_word = ((guint32 *) new_code)[0];
76     g_assert (first_word == SPIRV_MAGIC_NUMBER_NE);
77
78     info.pCode = new_code;
79   }
80
81   res = vkCreateShaderModule (device->device, &info, NULL, &ret);
82   g_free (new_code);
83   if (gst_vulkan_error_to_g_error (res, error, "VkCreateShaderModule") < 0)
84     return NULL;
85
86   g_free (new_code);
87
88   return ret;
89 }