1/*
2 * Copyright © 2021 Collabora Ltd.
3 *
4 * Derived from tu_shader.c which is:
5 * Copyright © 2019 Google LLC
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 */
26
27#include "panvk_private.h"
28
29#include "nir_builder.h"
30#include "nir_lower_blend.h"
31#include "spirv/nir_spirv.h"
32#include "util/mesa-sha1.h"
33
34#include "panfrost-quirks.h"
35#include "pan_shader.h"
36
37#include "vk_util.h"
38
39void
40panvk_shader_destroy(struct panvk_device *dev,
41                     struct panvk_shader *shader,
42                     const VkAllocationCallbacks *alloc)
43{
44   util_dynarray_fini(&shader->binary);
45   vk_free2(&dev->vk.alloc, alloc, shader);
46}
47
48VkResult
49panvk_CreateShaderModule(VkDevice _device,
50                         const VkShaderModuleCreateInfo *pCreateInfo,
51                         const VkAllocationCallbacks *pAllocator,
52                         VkShaderModule *pShaderModule)
53{
54   VK_FROM_HANDLE(panvk_device, device, _device);
55   struct panvk_shader_module *module;
56
57   assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO);
58   assert(pCreateInfo->flags == 0);
59   assert(pCreateInfo->codeSize % 4 == 0);
60
61   module = vk_object_zalloc(&device->vk, pAllocator,
62                             sizeof(*module) + pCreateInfo->codeSize,
63                             VK_OBJECT_TYPE_SHADER_MODULE);
64   if (module == NULL)
65      return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
66
67   module->code_size = pCreateInfo->codeSize;
68   memcpy(module->code, pCreateInfo->pCode, pCreateInfo->codeSize);
69
70   _mesa_sha1_compute(module->code, module->code_size, module->sha1);
71
72   *pShaderModule = panvk_shader_module_to_handle(module);
73
74   return VK_SUCCESS;
75}
76
77void
78panvk_DestroyShaderModule(VkDevice _device,
79                          VkShaderModule _module,
80                          const VkAllocationCallbacks *pAllocator)
81{
82   VK_FROM_HANDLE(panvk_device, device, _device);
83   VK_FROM_HANDLE(panvk_shader_module, module, _module);
84
85   if (!module)
86      return;
87
88   vk_object_free(&device->vk, pAllocator, module);
89}
90