1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2017 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "vk_shader_module.h"
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "util/mesa-sha1.h"
27bf215546Sopenharmony_ci#include "vk_common_entrypoints.h"
28bf215546Sopenharmony_ci#include "vk_device.h"
29bf215546Sopenharmony_ci#include "vk_log.h"
30bf215546Sopenharmony_ci#include "vk_nir.h"
31bf215546Sopenharmony_ci#include "vk_pipeline.h"
32bf215546Sopenharmony_ci#include "vk_util.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ciVKAPI_ATTR VkResult VKAPI_CALL
35bf215546Sopenharmony_civk_common_CreateShaderModule(VkDevice _device,
36bf215546Sopenharmony_ci                             const VkShaderModuleCreateInfo *pCreateInfo,
37bf215546Sopenharmony_ci                             const VkAllocationCallbacks *pAllocator,
38bf215546Sopenharmony_ci                             VkShaderModule *pShaderModule)
39bf215546Sopenharmony_ci{
40bf215546Sopenharmony_ci    VK_FROM_HANDLE(vk_device, device, _device);
41bf215546Sopenharmony_ci    struct vk_shader_module *module;
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO);
44bf215546Sopenharmony_ci    assert(pCreateInfo->flags == 0);
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci    module = vk_object_alloc(device, pAllocator,
47bf215546Sopenharmony_ci                             sizeof(*module) + pCreateInfo->codeSize,
48bf215546Sopenharmony_ci                             VK_OBJECT_TYPE_SHADER_MODULE);
49bf215546Sopenharmony_ci    if (module == NULL)
50bf215546Sopenharmony_ci       return VK_ERROR_OUT_OF_HOST_MEMORY;
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci    module->size = pCreateInfo->codeSize;
53bf215546Sopenharmony_ci    module->nir = NULL;
54bf215546Sopenharmony_ci    memcpy(module->data, pCreateInfo->pCode, module->size);
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci    _mesa_sha1_compute(module->data, module->size, module->sha1);
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci    *pShaderModule = vk_shader_module_to_handle(module);
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci    return VK_SUCCESS;
61bf215546Sopenharmony_ci}
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ciconst uint8_t vk_shaderModuleIdentifierAlgorithmUUID[VK_UUID_SIZE] = "MESA-SHA1";
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ciVKAPI_ATTR void VKAPI_CALL
66bf215546Sopenharmony_civk_common_GetShaderModuleIdentifierEXT(VkDevice _device,
67bf215546Sopenharmony_ci                                       VkShaderModule _module,
68bf215546Sopenharmony_ci                                       VkShaderModuleIdentifierEXT *pIdentifier)
69bf215546Sopenharmony_ci{
70bf215546Sopenharmony_ci   VK_FROM_HANDLE(vk_shader_module, module, _module);
71bf215546Sopenharmony_ci   memcpy(pIdentifier->identifier, module->sha1, sizeof(module->sha1));
72bf215546Sopenharmony_ci   pIdentifier->identifierSize = sizeof(module->sha1);
73bf215546Sopenharmony_ci}
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ciVKAPI_ATTR void VKAPI_CALL
76bf215546Sopenharmony_civk_common_GetShaderModuleCreateInfoIdentifierEXT(VkDevice _device,
77bf215546Sopenharmony_ci                                                 const VkShaderModuleCreateInfo *pCreateInfo,
78bf215546Sopenharmony_ci                                                 VkShaderModuleIdentifierEXT *pIdentifier)
79bf215546Sopenharmony_ci{
80bf215546Sopenharmony_ci   _mesa_sha1_compute(pCreateInfo->pCode, pCreateInfo->codeSize,
81bf215546Sopenharmony_ci                      pIdentifier->identifier);
82bf215546Sopenharmony_ci   pIdentifier->identifierSize = SHA1_DIGEST_LENGTH;
83bf215546Sopenharmony_ci}
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ciVKAPI_ATTR void VKAPI_CALL
86bf215546Sopenharmony_civk_common_DestroyShaderModule(VkDevice _device,
87bf215546Sopenharmony_ci                              VkShaderModule _module,
88bf215546Sopenharmony_ci                              const VkAllocationCallbacks *pAllocator)
89bf215546Sopenharmony_ci{
90bf215546Sopenharmony_ci   VK_FROM_HANDLE(vk_device, device, _device);
91bf215546Sopenharmony_ci   VK_FROM_HANDLE(vk_shader_module, module, _module);
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci   if (!module)
94bf215546Sopenharmony_ci      return;
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci   /* NIR modules (which are only created internally by the driver) are not
97bf215546Sopenharmony_ci    * dynamically allocated so we should never call this for them.
98bf215546Sopenharmony_ci    * Instead the driver is responsible for freeing the NIR code when it is
99bf215546Sopenharmony_ci    * no longer needed.
100bf215546Sopenharmony_ci    */
101bf215546Sopenharmony_ci   assert(module->nir == NULL);
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci   vk_object_free(device, pAllocator, module);
104bf215546Sopenharmony_ci}
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci#define SPIR_V_MAGIC_NUMBER 0x07230203
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ciuint32_t
109bf215546Sopenharmony_civk_shader_module_spirv_version(const struct vk_shader_module *mod)
110bf215546Sopenharmony_ci{
111bf215546Sopenharmony_ci   if (mod->nir != NULL)
112bf215546Sopenharmony_ci      return 0;
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci   return vk_spirv_version((uint32_t *)mod->data, mod->size);
115bf215546Sopenharmony_ci}
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ciVkResult
118bf215546Sopenharmony_civk_shader_module_to_nir(struct vk_device *device,
119bf215546Sopenharmony_ci                        const struct vk_shader_module *mod,
120bf215546Sopenharmony_ci                        gl_shader_stage stage,
121bf215546Sopenharmony_ci                        const char *entrypoint_name,
122bf215546Sopenharmony_ci                        const VkSpecializationInfo *spec_info,
123bf215546Sopenharmony_ci                        const struct spirv_to_nir_options *spirv_options,
124bf215546Sopenharmony_ci                        const nir_shader_compiler_options *nir_options,
125bf215546Sopenharmony_ci                        void *mem_ctx, nir_shader **nir_out)
126bf215546Sopenharmony_ci{
127bf215546Sopenharmony_ci   const VkPipelineShaderStageCreateInfo info = {
128bf215546Sopenharmony_ci      .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
129bf215546Sopenharmony_ci      .stage = mesa_to_vk_shader_stage(stage),
130bf215546Sopenharmony_ci      .module = vk_shader_module_to_handle((struct vk_shader_module *)mod),
131bf215546Sopenharmony_ci      .pName = entrypoint_name,
132bf215546Sopenharmony_ci      .pSpecializationInfo = spec_info,
133bf215546Sopenharmony_ci   };
134bf215546Sopenharmony_ci   return vk_pipeline_shader_stage_to_nir(device, &info,
135bf215546Sopenharmony_ci                                          spirv_options, nir_options,
136bf215546Sopenharmony_ci                                          mem_ctx, nir_out);
137bf215546Sopenharmony_ci}
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_cistruct vk_shader_module *
140bf215546Sopenharmony_civk_shader_module_clone(void *mem_ctx, const struct vk_shader_module *src)
141bf215546Sopenharmony_ci{
142bf215546Sopenharmony_ci   struct vk_shader_module *dst =
143bf215546Sopenharmony_ci      ralloc_size(mem_ctx, sizeof(struct vk_shader_module) + src->size);
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci   vk_object_base_init(src->base.device, &dst->base, VK_OBJECT_TYPE_SHADER_MODULE);
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci   dst->nir = NULL;
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci   memcpy(dst->sha1, src->sha1, sizeof(src->sha1));
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_ci   dst->size = src->size;
152bf215546Sopenharmony_ci   memcpy(dst->data, src->data, src->size);
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci   return dst;
155bf215546Sopenharmony_ci}
156