blob: 29dd2ee3423840176e7dbea36041c553efdbf9e2 [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
Tony Barbourde4124d2015-07-03 10:33:54 -060085ICD_EXPORT VkResult VKAPI vkDestroyShaderModule(
86 VkDevice device,
87 VkShaderModule shaderModule)
88
89 {
90 struct intel_obj *obj = intel_obj(shaderModule.handle);
91
92 obj->destroy(obj);
93 return VK_SUCCESS;
94 }
95
96
Courtney Goeltzenleuchter42509992014-08-21 17:33:46 -060097static void shader_destroy(struct intel_obj *obj)
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060098{
Chia-I Wu9194a6d2014-08-28 11:36:48 +080099 struct intel_shader *sh = intel_shader_from_obj(obj);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600100
Chia-I Wua4d1b392014-10-10 13:57:29 +0800101 if (sh->ir)
102 shader_destroy_ir(sh->ir);
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800103 intel_base_destroy(&sh->obj.base);
104}
105
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600106static VkResult shader_create(struct intel_dev *dev,
107 const VkShaderCreateInfo *info,
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800108 struct intel_shader **sh_ret)
109{
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800110 struct intel_shader *sh;
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800111
Chia-I Wu545c2e12015-02-22 13:19:54 +0800112 sh = (struct intel_shader *) intel_base_create(&dev->base.handle,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600113 sizeof(*sh), dev->base.dbg, VK_OBJECT_TYPE_SHADER, info, 0);
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800114 if (!sh)
Tony Barbour8205d902015-04-16 15:59:00 -0600115 return VK_ERROR_OUT_OF_HOST_MEMORY;
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800116
Chris Forbes86ecdac2015-06-25 15:12:48 +1200117 struct intel_shader_module *sm = intel_shader_module(info->module);
Courtney Goeltzenleuchterd7f0dce2014-10-06 09:45:33 -0600118
Cody Northrop87ae5e12015-08-24 15:11:10 -0600119 sh->ir = shader_create_ir(dev->gpu, sm->code, sm->code_size, info->stage);
Chia-I Wua4d1b392014-10-10 13:57:29 +0800120 if (!sh->ir) {
Courtney Goeltzenleuchteref5b1162014-10-10 16:29:46 -0600121 shader_destroy(&sh->obj);
Cody Northrop293d4502015-05-05 09:38:03 -0600122 return VK_ERROR_BAD_SHADER_CODE;
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800123 }
124
125 sh->obj.destroy = shader_destroy;
126
127 *sh_ret = sh;
128
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600129 return VK_SUCCESS;
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600130}
131
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600132ICD_EXPORT VkResult VKAPI vkCreateShader(
133 VkDevice device,
134 const VkShaderCreateInfo* pCreateInfo,
135 VkShader* pShader)
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600136{
137 struct intel_dev *dev = intel_dev(device);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600138
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800139 return shader_create(dev, pCreateInfo, (struct intel_shader **) pShader);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600140}
Tony Barbourde4124d2015-07-03 10:33:54 -0600141
142ICD_EXPORT VkResult VKAPI vkDestroyShader(
143 VkDevice device,
144 VkShader shader)
145
146 {
147 struct intel_obj *obj = intel_obj(shader.handle);
148
149 obj->destroy(obj);
150 return VK_SUCCESS;
151 }