blob: 471cf5198c50b38889b1ceee11dc261686d98f2e [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{
Chris Forbes86ecdac2015-06-25 15:12:48 +120046 struct intel_shader_module *sm;
47
48 sm = (struct intel_shader_module *) intel_base_create(&dev->base.handle,
49 sizeof(*sm), dev->base.dbg, VK_OBJECT_TYPE_SHADER_MODULE, info, 0);
50 if (!sm)
51 return VK_ERROR_OUT_OF_HOST_MEMORY;
52
Chris Forbes86ecdac2015-06-25 15:12:48 +120053 sm->code_size = info->codeSize;
54 sm->code = malloc(info->codeSize);
55 if (!sm->code) {
56 shader_module_destroy(&sm->obj);
57 return VK_ERROR_OUT_OF_HOST_MEMORY;
58 }
59
60 memcpy(sm->code, info->pCode, info->codeSize);
61 sm->obj.destroy = shader_module_destroy;
62
63 *sm_ret = sm;
64
65 return VK_SUCCESS;
66}
67
68ICD_EXPORT VkResult VKAPI vkCreateShaderModule(
69 VkDevice device,
70 const VkShaderModuleCreateInfo* pCreateInfo,
71 VkShaderModule* pShaderModule)
72{
73 struct intel_dev *dev = intel_dev(device);
74
75 return shader_module_create(dev, pCreateInfo, (struct intel_shader_module **) pShaderModule);
76}
77
Mark Lobodzinski67b42b72015-09-07 13:59:43 -060078ICD_EXPORT void VKAPI vkDestroyShaderModule(
Tony Barbourde4124d2015-07-03 10:33:54 -060079 VkDevice device,
80 VkShaderModule shaderModule)
81
82 {
83 struct intel_obj *obj = intel_obj(shaderModule.handle);
84
85 obj->destroy(obj);
Tony Barbourde4124d2015-07-03 10:33:54 -060086 }
87
88
Courtney Goeltzenleuchter42509992014-08-21 17:33:46 -060089static void shader_destroy(struct intel_obj *obj)
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060090{
Chia-I Wu9194a6d2014-08-28 11:36:48 +080091 struct intel_shader *sh = intel_shader_from_obj(obj);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060092
Chia-I Wua4d1b392014-10-10 13:57:29 +080093 if (sh->ir)
94 shader_destroy_ir(sh->ir);
Chia-I Wu9194a6d2014-08-28 11:36:48 +080095 intel_base_destroy(&sh->obj.base);
96}
97
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060098static VkResult shader_create(struct intel_dev *dev,
99 const VkShaderCreateInfo *info,
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800100 struct intel_shader **sh_ret)
101{
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800102 struct intel_shader *sh;
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800103
Chia-I Wu545c2e12015-02-22 13:19:54 +0800104 sh = (struct intel_shader *) intel_base_create(&dev->base.handle,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600105 sizeof(*sh), dev->base.dbg, VK_OBJECT_TYPE_SHADER, info, 0);
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800106 if (!sh)
Tony Barbour8205d902015-04-16 15:59:00 -0600107 return VK_ERROR_OUT_OF_HOST_MEMORY;
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800108
Chris Forbes86ecdac2015-06-25 15:12:48 +1200109 struct intel_shader_module *sm = intel_shader_module(info->module);
Courtney Goeltzenleuchterd7f0dce2014-10-06 09:45:33 -0600110
Cody Northrop87ae5e12015-08-24 15:11:10 -0600111 sh->ir = shader_create_ir(dev->gpu, sm->code, sm->code_size, info->stage);
Chia-I Wua4d1b392014-10-10 13:57:29 +0800112 if (!sh->ir) {
Courtney Goeltzenleuchteref5b1162014-10-10 16:29:46 -0600113 shader_destroy(&sh->obj);
Courtney Goeltzenleuchtera54b76a2015-09-04 13:39:59 -0600114 /* TODOVV: Can this move to validation layer? */
115// return VK_ERROR_BAD_SHADER_CODE;
Courtney Goeltzenleuchterac544f32015-09-14 18:01:17 -0600116 return VK_ERROR_VALIDATION_FAILED;
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800117 }
118
119 sh->obj.destroy = shader_destroy;
120
121 *sh_ret = sh;
122
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600123 return VK_SUCCESS;
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600124}
125
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600126ICD_EXPORT VkResult VKAPI vkCreateShader(
127 VkDevice device,
128 const VkShaderCreateInfo* pCreateInfo,
129 VkShader* pShader)
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600130{
131 struct intel_dev *dev = intel_dev(device);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600132
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800133 return shader_create(dev, pCreateInfo, (struct intel_shader **) pShader);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600134}
Tony Barbourde4124d2015-07-03 10:33:54 -0600135
Mark Lobodzinski67b42b72015-09-07 13:59:43 -0600136ICD_EXPORT void VKAPI vkDestroyShader(
Tony Barbourde4124d2015-07-03 10:33:54 -0600137 VkDevice device,
138 VkShader shader)
139
140 {
141 struct intel_obj *obj = intel_obj(shader.handle);
142
143 obj->destroy(obj);
Tony Barbourde4124d2015-07-03 10:33:54 -0600144 }