blob: c8070c3c91ce412fdfbadff51fe7fea985f82132 [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
Courtney Goeltzenleuchter42509992014-08-21 17:33:46 -060033static void shader_destroy(struct intel_obj *obj)
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060034{
Chia-I Wu9194a6d2014-08-28 11:36:48 +080035 struct intel_shader *sh = intel_shader_from_obj(obj);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060036
Chia-I Wua4d1b392014-10-10 13:57:29 +080037 if (sh->ir)
38 shader_destroy_ir(sh->ir);
Chia-I Wu9194a6d2014-08-28 11:36:48 +080039 intel_base_destroy(&sh->obj.base);
40}
41
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060042static VK_RESULT shader_create(struct intel_dev *dev,
43 const VK_SHADER_CREATE_INFO *info,
Chia-I Wu9194a6d2014-08-28 11:36:48 +080044 struct intel_shader **sh_ret)
45{
Cody Northropacfb0492015-03-17 15:55:58 -060046 const struct icd_spv_header *spv =
47 (const struct icd_spv_header *) info->pCode;
Chia-I Wu9194a6d2014-08-28 11:36:48 +080048 struct intel_shader *sh;
Chia-I Wu9194a6d2014-08-28 11:36:48 +080049
Chia-I Wu545c2e12015-02-22 13:19:54 +080050 sh = (struct intel_shader *) intel_base_create(&dev->base.handle,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060051 sizeof(*sh), dev->base.dbg, VK_DBG_OBJECT_SHADER, info, 0);
Chia-I Wu9194a6d2014-08-28 11:36:48 +080052 if (!sh)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060053 return VK_ERROR_OUT_OF_MEMORY;
Chia-I Wu9194a6d2014-08-28 11:36:48 +080054
Cody Northropacfb0492015-03-17 15:55:58 -060055 if (info->codeSize < sizeof(*spv))
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060056 return VK_ERROR_INVALID_MEMORY_SIZE;
Cody Northropacfb0492015-03-17 15:55:58 -060057 if (spv->magic != ICD_SPV_MAGIC)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060058 return VK_ERROR_BAD_SHADER_CODE;
Courtney Goeltzenleuchterd7f0dce2014-10-06 09:45:33 -060059
Chia-I Wua4d1b392014-10-10 13:57:29 +080060 sh->ir = shader_create_ir(dev->gpu, info->pCode, info->codeSize);
61 if (!sh->ir) {
Courtney Goeltzenleuchteref5b1162014-10-10 16:29:46 -060062 shader_destroy(&sh->obj);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060063 return VK_ERROR_UNKNOWN;
Chia-I Wu9194a6d2014-08-28 11:36:48 +080064 }
65
66 sh->obj.destroy = shader_destroy;
67
68 *sh_ret = sh;
69
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060070 return VK_SUCCESS;
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060071}
72
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060073ICD_EXPORT VK_RESULT VKAPI vkCreateShader(
74 VK_DEVICE device,
75 const VK_SHADER_CREATE_INFO* pCreateInfo,
76 VK_SHADER* pShader)
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060077{
78 struct intel_dev *dev = intel_dev(device);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060079
Chia-I Wu9194a6d2014-08-28 11:36:48 +080080 return shader_create(dev, pCreateInfo, (struct intel_shader **) pShader);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060081}