17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2021 Collabora Ltd.
37ec681f3Smrg *
47ec681f3Smrg * Derived from tu_shader.c which is:
57ec681f3Smrg * Copyright © 2019 Google LLC
67ec681f3Smrg *
77ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
87ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
97ec681f3Smrg * to deal in the Software without restriction, including without limitation
107ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
117ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
127ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
137ec681f3Smrg *
147ec681f3Smrg * The above copyright notice and this permission notice (including the next
157ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
167ec681f3Smrg * Software.
177ec681f3Smrg *
187ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
197ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
207ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
217ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
227ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
237ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
247ec681f3Smrg * DEALINGS IN THE SOFTWARE.
257ec681f3Smrg */
267ec681f3Smrg
277ec681f3Smrg#include "panvk_private.h"
287ec681f3Smrg
297ec681f3Smrg#include "nir_builder.h"
307ec681f3Smrg#include "nir_lower_blend.h"
317ec681f3Smrg#include "spirv/nir_spirv.h"
327ec681f3Smrg#include "util/mesa-sha1.h"
337ec681f3Smrg
347ec681f3Smrg#include "panfrost-quirks.h"
357ec681f3Smrg#include "pan_shader.h"
367ec681f3Smrg
377ec681f3Smrg#include "vk_util.h"
387ec681f3Smrg
397ec681f3Smrgvoid
407ec681f3Smrgpanvk_shader_destroy(struct panvk_device *dev,
417ec681f3Smrg                     struct panvk_shader *shader,
427ec681f3Smrg                     const VkAllocationCallbacks *alloc)
437ec681f3Smrg{
447ec681f3Smrg   util_dynarray_fini(&shader->binary);
457ec681f3Smrg   vk_free2(&dev->vk.alloc, alloc, shader);
467ec681f3Smrg}
477ec681f3Smrg
487ec681f3SmrgVkResult
497ec681f3Smrgpanvk_CreateShaderModule(VkDevice _device,
507ec681f3Smrg                         const VkShaderModuleCreateInfo *pCreateInfo,
517ec681f3Smrg                         const VkAllocationCallbacks *pAllocator,
527ec681f3Smrg                         VkShaderModule *pShaderModule)
537ec681f3Smrg{
547ec681f3Smrg   VK_FROM_HANDLE(panvk_device, device, _device);
557ec681f3Smrg   struct panvk_shader_module *module;
567ec681f3Smrg
577ec681f3Smrg   assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO);
587ec681f3Smrg   assert(pCreateInfo->flags == 0);
597ec681f3Smrg   assert(pCreateInfo->codeSize % 4 == 0);
607ec681f3Smrg
617ec681f3Smrg   module = vk_object_zalloc(&device->vk, pAllocator,
627ec681f3Smrg                             sizeof(*module) + pCreateInfo->codeSize,
637ec681f3Smrg                             VK_OBJECT_TYPE_SHADER_MODULE);
647ec681f3Smrg   if (module == NULL)
657ec681f3Smrg      return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
667ec681f3Smrg
677ec681f3Smrg   module->code_size = pCreateInfo->codeSize;
687ec681f3Smrg   memcpy(module->code, pCreateInfo->pCode, pCreateInfo->codeSize);
697ec681f3Smrg
707ec681f3Smrg   _mesa_sha1_compute(module->code, module->code_size, module->sha1);
717ec681f3Smrg
727ec681f3Smrg   *pShaderModule = panvk_shader_module_to_handle(module);
737ec681f3Smrg
747ec681f3Smrg   return VK_SUCCESS;
757ec681f3Smrg}
767ec681f3Smrg
777ec681f3Smrgvoid
787ec681f3Smrgpanvk_DestroyShaderModule(VkDevice _device,
797ec681f3Smrg                          VkShaderModule _module,
807ec681f3Smrg                          const VkAllocationCallbacks *pAllocator)
817ec681f3Smrg{
827ec681f3Smrg   VK_FROM_HANDLE(panvk_device, device, _device);
837ec681f3Smrg   VK_FROM_HANDLE(panvk_shader_module, module, _module);
847ec681f3Smrg
857ec681f3Smrg   if (!module)
867ec681f3Smrg      return;
877ec681f3Smrg
887ec681f3Smrg   vk_object_free(&device->vk, pAllocator, module);
897ec681f3Smrg}
90