Courtney Goeltzenleuchter | 52ec336 | 2014-08-19 11:52:02 -0600 | [diff] [blame] | 1 | /* |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2 | * Vulkan |
Courtney Goeltzenleuchter | 52ec336 | 2014-08-19 11:52:02 -0600 | [diff] [blame] | 3 | * |
| 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 Wu | 44e4236 | 2014-09-02 08:32:09 +0800 | [diff] [blame] | 23 | * |
| 24 | * Authors: |
| 25 | * Courtney Goeltzenleuchter <courtney@lunarg.com> |
| 26 | * Chia-I Wu <olv@lunarg.com> |
Courtney Goeltzenleuchter | 52ec336 | 2014-08-19 11:52:02 -0600 | [diff] [blame] | 27 | */ |
| 28 | |
Chia-I Wu | 9194a6d | 2014-08-28 11:36:48 +0800 | [diff] [blame] | 29 | #include "dev.h" |
Courtney Goeltzenleuchter | 52ec336 | 2014-08-19 11:52:02 -0600 | [diff] [blame] | 30 | #include "shader.h" |
Cody Northrop | 0eb5eea | 2014-09-19 15:11:52 -0600 | [diff] [blame] | 31 | #include "compiler/shader/compiler_interface.h" |
Courtney Goeltzenleuchter | 52ec336 | 2014-08-19 11:52:02 -0600 | [diff] [blame] | 32 | |
Chris Forbes | 86ecdac | 2015-06-25 15:12:48 +1200 | [diff] [blame] | 33 | static 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 | |
| 42 | static VkResult shader_module_create(struct intel_dev *dev, |
| 43 | const VkShaderModuleCreateInfo *info, |
| 44 | struct intel_shader_module **sm_ret) |
| 45 | { |
Chris Forbes | 86ecdac | 2015-06-25 15:12:48 +1200 | [diff] [blame] | 46 | 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 Forbes | 86ecdac | 2015-06-25 15:12:48 +1200 | [diff] [blame] | 53 | 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 | |
| 68 | ICD_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 Lobodzinski | 67b42b7 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 78 | ICD_EXPORT void VKAPI vkDestroyShaderModule( |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 79 | VkDevice device, |
| 80 | VkShaderModule shaderModule) |
| 81 | |
| 82 | { |
| 83 | struct intel_obj *obj = intel_obj(shaderModule.handle); |
| 84 | |
| 85 | obj->destroy(obj); |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | |
Courtney Goeltzenleuchter | 4250999 | 2014-08-21 17:33:46 -0600 | [diff] [blame] | 89 | static void shader_destroy(struct intel_obj *obj) |
Courtney Goeltzenleuchter | 52ec336 | 2014-08-19 11:52:02 -0600 | [diff] [blame] | 90 | { |
Chia-I Wu | 9194a6d | 2014-08-28 11:36:48 +0800 | [diff] [blame] | 91 | struct intel_shader *sh = intel_shader_from_obj(obj); |
Courtney Goeltzenleuchter | 52ec336 | 2014-08-19 11:52:02 -0600 | [diff] [blame] | 92 | |
Chia-I Wu | a4d1b39 | 2014-10-10 13:57:29 +0800 | [diff] [blame] | 93 | if (sh->ir) |
| 94 | shader_destroy_ir(sh->ir); |
Chia-I Wu | 9194a6d | 2014-08-28 11:36:48 +0800 | [diff] [blame] | 95 | intel_base_destroy(&sh->obj.base); |
| 96 | } |
| 97 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 98 | static VkResult shader_create(struct intel_dev *dev, |
| 99 | const VkShaderCreateInfo *info, |
Chia-I Wu | 9194a6d | 2014-08-28 11:36:48 +0800 | [diff] [blame] | 100 | struct intel_shader **sh_ret) |
| 101 | { |
Chia-I Wu | 9194a6d | 2014-08-28 11:36:48 +0800 | [diff] [blame] | 102 | struct intel_shader *sh; |
Chia-I Wu | 9194a6d | 2014-08-28 11:36:48 +0800 | [diff] [blame] | 103 | |
Chia-I Wu | 545c2e1 | 2015-02-22 13:19:54 +0800 | [diff] [blame] | 104 | sh = (struct intel_shader *) intel_base_create(&dev->base.handle, |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 105 | sizeof(*sh), dev->base.dbg, VK_OBJECT_TYPE_SHADER, info, 0); |
Chia-I Wu | 9194a6d | 2014-08-28 11:36:48 +0800 | [diff] [blame] | 106 | if (!sh) |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 107 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Chia-I Wu | 9194a6d | 2014-08-28 11:36:48 +0800 | [diff] [blame] | 108 | |
Chris Forbes | 86ecdac | 2015-06-25 15:12:48 +1200 | [diff] [blame] | 109 | struct intel_shader_module *sm = intel_shader_module(info->module); |
Courtney Goeltzenleuchter | d7f0dce | 2014-10-06 09:45:33 -0600 | [diff] [blame] | 110 | |
Cody Northrop | 87ae5e1 | 2015-08-24 15:11:10 -0600 | [diff] [blame] | 111 | sh->ir = shader_create_ir(dev->gpu, sm->code, sm->code_size, info->stage); |
Chia-I Wu | a4d1b39 | 2014-10-10 13:57:29 +0800 | [diff] [blame] | 112 | if (!sh->ir) { |
Courtney Goeltzenleuchter | ef5b116 | 2014-10-10 16:29:46 -0600 | [diff] [blame] | 113 | shader_destroy(&sh->obj); |
Courtney Goeltzenleuchter | a54b76a | 2015-09-04 13:39:59 -0600 | [diff] [blame] | 114 | /* TODOVV: Can this move to validation layer? */ |
| 115 | // return VK_ERROR_BAD_SHADER_CODE; |
Courtney Goeltzenleuchter | ac544f3 | 2015-09-14 18:01:17 -0600 | [diff] [blame] | 116 | return VK_ERROR_VALIDATION_FAILED; |
Chia-I Wu | 9194a6d | 2014-08-28 11:36:48 +0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | sh->obj.destroy = shader_destroy; |
| 120 | |
| 121 | *sh_ret = sh; |
| 122 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 123 | return VK_SUCCESS; |
Courtney Goeltzenleuchter | 52ec336 | 2014-08-19 11:52:02 -0600 | [diff] [blame] | 124 | } |
| 125 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 126 | ICD_EXPORT VkResult VKAPI vkCreateShader( |
| 127 | VkDevice device, |
| 128 | const VkShaderCreateInfo* pCreateInfo, |
| 129 | VkShader* pShader) |
Courtney Goeltzenleuchter | 52ec336 | 2014-08-19 11:52:02 -0600 | [diff] [blame] | 130 | { |
| 131 | struct intel_dev *dev = intel_dev(device); |
Courtney Goeltzenleuchter | 52ec336 | 2014-08-19 11:52:02 -0600 | [diff] [blame] | 132 | |
Chia-I Wu | 9194a6d | 2014-08-28 11:36:48 +0800 | [diff] [blame] | 133 | return shader_create(dev, pCreateInfo, (struct intel_shader **) pShader); |
Courtney Goeltzenleuchter | 52ec336 | 2014-08-19 11:52:02 -0600 | [diff] [blame] | 134 | } |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 135 | |
Mark Lobodzinski | 67b42b7 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 136 | ICD_EXPORT void VKAPI vkDestroyShader( |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 137 | VkDevice device, |
| 138 | VkShader shader) |
| 139 | |
| 140 | { |
| 141 | struct intel_obj *obj = intel_obj(shader.handle); |
| 142 | |
| 143 | obj->destroy(obj); |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 144 | } |