blob: 18b2412c7e1f466a8230e5513bb73a10e4b6df86 [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
Courtney Goeltzenleuchtera54b76a2015-09-04 13:39:59 -060055 /* TODOVV: Move test to validation layer */
56 if (info->codeSize < sizeof(*spv)) {
57// return VK_ERROR_INVALID_MEMORY_SIZE;
58 return VK_ERROR_UNKNOWN;
59 }
60 /* TODOVV: Move test to validation layer */
61 if (spv->magic != ICD_SPV_MAGIC) {
62// return VK_ERROR_BAD_SHADER_CODE;
63 return VK_ERROR_UNKNOWN;
64 }
Chris Forbes86ecdac2015-06-25 15:12:48 +120065
66 sm->code_size = info->codeSize;
67 sm->code = malloc(info->codeSize);
68 if (!sm->code) {
69 shader_module_destroy(&sm->obj);
70 return VK_ERROR_OUT_OF_HOST_MEMORY;
71 }
72
73 memcpy(sm->code, info->pCode, info->codeSize);
74 sm->obj.destroy = shader_module_destroy;
75
76 *sm_ret = sm;
77
78 return VK_SUCCESS;
79}
80
81ICD_EXPORT VkResult VKAPI vkCreateShaderModule(
82 VkDevice device,
83 const VkShaderModuleCreateInfo* pCreateInfo,
84 VkShaderModule* pShaderModule)
85{
86 struct intel_dev *dev = intel_dev(device);
87
88 return shader_module_create(dev, pCreateInfo, (struct intel_shader_module **) pShaderModule);
89}
90
Mark Lobodzinski67b42b72015-09-07 13:59:43 -060091ICD_EXPORT void VKAPI vkDestroyShaderModule(
Tony Barbourde4124d2015-07-03 10:33:54 -060092 VkDevice device,
93 VkShaderModule shaderModule)
94
95 {
96 struct intel_obj *obj = intel_obj(shaderModule.handle);
97
98 obj->destroy(obj);
Tony Barbourde4124d2015-07-03 10:33:54 -060099 }
100
101
Courtney Goeltzenleuchter42509992014-08-21 17:33:46 -0600102static void shader_destroy(struct intel_obj *obj)
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600103{
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800104 struct intel_shader *sh = intel_shader_from_obj(obj);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600105
Chia-I Wua4d1b392014-10-10 13:57:29 +0800106 if (sh->ir)
107 shader_destroy_ir(sh->ir);
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800108 intel_base_destroy(&sh->obj.base);
109}
110
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600111static VkResult shader_create(struct intel_dev *dev,
112 const VkShaderCreateInfo *info,
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800113 struct intel_shader **sh_ret)
114{
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800115 struct intel_shader *sh;
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800116
Chia-I Wu545c2e12015-02-22 13:19:54 +0800117 sh = (struct intel_shader *) intel_base_create(&dev->base.handle,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600118 sizeof(*sh), dev->base.dbg, VK_OBJECT_TYPE_SHADER, info, 0);
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800119 if (!sh)
Tony Barbour8205d902015-04-16 15:59:00 -0600120 return VK_ERROR_OUT_OF_HOST_MEMORY;
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800121
Chris Forbes86ecdac2015-06-25 15:12:48 +1200122 struct intel_shader_module *sm = intel_shader_module(info->module);
Courtney Goeltzenleuchterd7f0dce2014-10-06 09:45:33 -0600123
Cody Northrop87ae5e12015-08-24 15:11:10 -0600124 sh->ir = shader_create_ir(dev->gpu, sm->code, sm->code_size, info->stage);
Chia-I Wua4d1b392014-10-10 13:57:29 +0800125 if (!sh->ir) {
Courtney Goeltzenleuchteref5b1162014-10-10 16:29:46 -0600126 shader_destroy(&sh->obj);
Courtney Goeltzenleuchtera54b76a2015-09-04 13:39:59 -0600127 /* TODOVV: Can this move to validation layer? */
128// return VK_ERROR_BAD_SHADER_CODE;
129 return VK_ERROR_UNKNOWN;
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800130 }
131
132 sh->obj.destroy = shader_destroy;
133
134 *sh_ret = sh;
135
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600136 return VK_SUCCESS;
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600137}
138
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600139ICD_EXPORT VkResult VKAPI vkCreateShader(
140 VkDevice device,
141 const VkShaderCreateInfo* pCreateInfo,
142 VkShader* pShader)
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600143{
144 struct intel_dev *dev = intel_dev(device);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600145
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800146 return shader_create(dev, pCreateInfo, (struct intel_shader **) pShader);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600147}
Tony Barbourde4124d2015-07-03 10:33:54 -0600148
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600149ICD_EXPORT void VKAPI vkDestroyShader(
Tony Barbourde4124d2015-07-03 10:33:54 -0600150 VkDevice device,
151 VkShader shader)
152
153 {
154 struct intel_obj *obj = intel_obj(shader.handle);
155
156 obj->destroy(obj);
Tony Barbourde4124d2015-07-03 10:33:54 -0600157 }