vk_shader_module.c revision 7ec681f3
1/*
2 * Copyright © 2017 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include "vk_shader_module.h"
25#include "util/mesa-sha1.h"
26#include "vk_common_entrypoints.h"
27#include "vk_device.h"
28
29VKAPI_ATTR VkResult VKAPI_CALL
30vk_common_CreateShaderModule(VkDevice _device,
31                             const VkShaderModuleCreateInfo *pCreateInfo,
32                             const VkAllocationCallbacks *pAllocator,
33                             VkShaderModule *pShaderModule)
34{
35    VK_FROM_HANDLE(vk_device, device, _device);
36    struct vk_shader_module *module;
37
38    assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO);
39    assert(pCreateInfo->flags == 0);
40
41    module = vk_object_alloc(device, pAllocator,
42                             sizeof(*module) + pCreateInfo->codeSize,
43                             VK_OBJECT_TYPE_SHADER_MODULE);
44    if (module == NULL)
45       return VK_ERROR_OUT_OF_HOST_MEMORY;
46
47    module->size = pCreateInfo->codeSize;
48    module->nir = NULL;
49    memcpy(module->data, pCreateInfo->pCode, module->size);
50
51    _mesa_sha1_compute(module->data, module->size, module->sha1);
52
53    *pShaderModule = vk_shader_module_to_handle(module);
54
55    return VK_SUCCESS;
56}
57
58VKAPI_ATTR void VKAPI_CALL
59vk_common_DestroyShaderModule(VkDevice _device,
60                              VkShaderModule _module,
61                              const VkAllocationCallbacks *pAllocator)
62{
63   VK_FROM_HANDLE(vk_device, device, _device);
64   VK_FROM_HANDLE(vk_shader_module, module, _module);
65
66   if (!module)
67      return;
68
69   /* NIR modules (which are only created internally by the driver) are not
70    * dynamically allocated so we should never call this for them.
71    * Instead the driver is responsible for freeing the NIR code when it is
72    * no longer needed.
73    */
74   assert(module->nir == NULL);
75
76   vk_object_free(device, pAllocator, module);
77}
78