blob: 92d889ad2f08dce06784128ef49524971ae583d4 [file] [log] [blame]
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -06001/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002 * Vulkan
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -06003 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Chia-I Wu44e42362014-09-02 08:32:09 +080023 *
24 * Authors:
25 * Courtney Goeltzenleuchter <courtney@lunarg.com>
26 * Chia-I Wu <olv@lunarg.com>
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060027 */
28
Chia-I Wu9194a6d2014-08-28 11:36:48 +080029#include "dev.h"
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060030#include "shader.h"
Cody Northrop0eb5eea2014-09-19 15:11:52 -060031#include "compiler/shader/compiler_interface.h"
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060032
Chris Forbes86ecdac2015-06-25 15:12:48 +120033static void shader_module_destroy(struct intel_obj *obj)
34{
35 struct intel_shader_module *sm = intel_shader_module_from_obj(obj);
36
37 free(sm->code);
38 sm->code = 0;
39 intel_base_destroy(&sm->obj.base);
40}
41
42static VkResult shader_module_create(struct intel_dev *dev,
43 const VkShaderModuleCreateInfo *info,
44 struct intel_shader_module **sm_ret)
45{
46 const struct icd_spv_header *spv =
47 (const struct icd_spv_header *) info->pCode;
48 struct intel_shader_module *sm;
49
50 sm = (struct intel_shader_module *) intel_base_create(&dev->base.handle,
51 sizeof(*sm), dev->base.dbg, VK_OBJECT_TYPE_SHADER_MODULE, info, 0);
52 if (!sm)
53 return VK_ERROR_OUT_OF_HOST_MEMORY;
54
55 if (info->codeSize < sizeof(*spv))
56 return VK_ERROR_INVALID_MEMORY_SIZE;
57 if (spv->magic != ICD_SPV_MAGIC)
58 return VK_ERROR_BAD_SHADER_CODE;
59
60 sm->code_size = info->codeSize;
61 sm->code = malloc(info->codeSize);
62 if (!sm->code) {
63 shader_module_destroy(&sm->obj);
64 return VK_ERROR_OUT_OF_HOST_MEMORY;
65 }
66
67 memcpy(sm->code, info->pCode, info->codeSize);
68 sm->obj.destroy = shader_module_destroy;
69
70 *sm_ret = sm;
71
72 return VK_SUCCESS;
73}
74
75ICD_EXPORT VkResult VKAPI vkCreateShaderModule(
76 VkDevice device,
77 const VkShaderModuleCreateInfo* pCreateInfo,
78 VkShaderModule* pShaderModule)
79{
80 struct intel_dev *dev = intel_dev(device);
81
82 return shader_module_create(dev, pCreateInfo, (struct intel_shader_module **) pShaderModule);
83}
84
Courtney Goeltzenleuchter42509992014-08-21 17:33:46 -060085static void shader_destroy(struct intel_obj *obj)
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060086{
Chia-I Wu9194a6d2014-08-28 11:36:48 +080087 struct intel_shader *sh = intel_shader_from_obj(obj);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060088
Chia-I Wua4d1b392014-10-10 13:57:29 +080089 if (sh->ir)
90 shader_destroy_ir(sh->ir);
Chia-I Wu9194a6d2014-08-28 11:36:48 +080091 intel_base_destroy(&sh->obj.base);
92}
93
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060094static VkResult shader_create(struct intel_dev *dev,
95 const VkShaderCreateInfo *info,
Chia-I Wu9194a6d2014-08-28 11:36:48 +080096 struct intel_shader **sh_ret)
97{
Chia-I Wu9194a6d2014-08-28 11:36:48 +080098 struct intel_shader *sh;
Chia-I Wu9194a6d2014-08-28 11:36:48 +080099
Chia-I Wu545c2e12015-02-22 13:19:54 +0800100 sh = (struct intel_shader *) intel_base_create(&dev->base.handle,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600101 sizeof(*sh), dev->base.dbg, VK_OBJECT_TYPE_SHADER, info, 0);
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800102 if (!sh)
Tony Barbour8205d902015-04-16 15:59:00 -0600103 return VK_ERROR_OUT_OF_HOST_MEMORY;
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800104
Chris Forbes86ecdac2015-06-25 15:12:48 +1200105 struct intel_shader_module *sm = intel_shader_module(info->module);
Courtney Goeltzenleuchterd7f0dce2014-10-06 09:45:33 -0600106
Chris Forbes86ecdac2015-06-25 15:12:48 +1200107 sh->ir = shader_create_ir(dev->gpu, sm->code, sm->code_size);
Chia-I Wua4d1b392014-10-10 13:57:29 +0800108 if (!sh->ir) {
Courtney Goeltzenleuchteref5b1162014-10-10 16:29:46 -0600109 shader_destroy(&sh->obj);
Cody Northrop293d4502015-05-05 09:38:03 -0600110 return VK_ERROR_BAD_SHADER_CODE;
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800111 }
112
113 sh->obj.destroy = shader_destroy;
114
115 *sh_ret = sh;
116
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600117 return VK_SUCCESS;
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600118}
119
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600120ICD_EXPORT VkResult VKAPI vkCreateShader(
121 VkDevice device,
122 const VkShaderCreateInfo* pCreateInfo,
123 VkShader* pShader)
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600124{
125 struct intel_dev *dev = intel_dev(device);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600126
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800127 return shader_create(dev, pCreateInfo, (struct intel_shader **) pShader);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600128}