blob: 0f940026d48530f9712b92ab6c148e4a73461408 [file] [log] [blame]
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -06001/*
2 * XGL
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 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 Goeltzenleuchteref5b1162014-10-10 16:29:46 -060033static XGL_RESULT shader_parse(struct intel_shader *sh,
Chia-I Wu9194a6d2014-08-28 11:36:48 +080034 const struct intel_gpu *gpu,
35 const struct icd_bil_header *bil,
36 XGL_SIZE size)
37{
38 struct intel_ir *ir;
39
40 ir = icd_alloc(sizeof(*ir), 0, XGL_SYSTEM_ALLOC_INTERNAL_SHADER);
41 if (!ir)
42 return XGL_ERROR_OUT_OF_MEMORY;
43
44 ir->size = size - sizeof(*bil);
45
Steve K10b32512014-10-10 08:54:29 -060046 // invoke our program creation as well
Courtney Goeltzenleuchteref5b1162014-10-10 16:29:46 -060047 ir->shader_program = shader_create_program(sh, bil, size);
Steve K10b32512014-10-10 08:54:29 -060048 if (!ir->shader_program)
49 return XGL_ERROR_BAD_SHADER_CODE;
Chia-I Wu9194a6d2014-08-28 11:36:48 +080050
Steve K10b32512014-10-10 08:54:29 -060051 // TODO: set necessary shader information. This should really
52 // happen as result of create_program call.
Chia-I Wu9194a6d2014-08-28 11:36:48 +080053 sh->ir = ir;
54
55 return XGL_SUCCESS;
56}
57
Courtney Goeltzenleuchter42509992014-08-21 17:33:46 -060058static void shader_destroy(struct intel_obj *obj)
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060059{
Chia-I Wu9194a6d2014-08-28 11:36:48 +080060 struct intel_shader *sh = intel_shader_from_obj(obj);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060061
Courtney Goeltzenleuchter69bc01b2014-10-09 15:24:39 -060062 if (sh->ir) {
63 shader_destroy_program(sh->ir->shader_program);
64 }
Chia-I Wu9194a6d2014-08-28 11:36:48 +080065 icd_free(sh->ir);
66 intel_base_destroy(&sh->obj.base);
67}
68
69static XGL_RESULT shader_create(struct intel_dev *dev,
70 const XGL_SHADER_CREATE_INFO *info,
71 struct intel_shader **sh_ret)
72{
73 const struct icd_bil_header *bil =
74 (const struct icd_bil_header *) info->pCode;
75 struct intel_shader *sh;
76 XGL_RESULT ret;
77
Chia-I Wu9194a6d2014-08-28 11:36:48 +080078 sh = (struct intel_shader *) intel_base_create(dev, sizeof(*sh),
79 dev->base.dbg, XGL_DBG_OBJECT_SHADER, info, 0);
80 if (!sh)
81 return XGL_ERROR_OUT_OF_MEMORY;
82
Courtney Goeltzenleuchteref5b1162014-10-10 16:29:46 -060083 if (info->codeSize < sizeof(*bil))
84 return XGL_ERROR_INVALID_MEMORY_SIZE;
85 if (bil->magic != ICD_BIL_MAGIC)
86 return XGL_ERROR_BAD_SHADER_CODE;
Courtney Goeltzenleuchterd7f0dce2014-10-06 09:45:33 -060087
88
Courtney Goeltzenleuchteref5b1162014-10-10 16:29:46 -060089 ret = shader_parse(sh, dev->gpu, bil, info->codeSize);
90 if (ret != XGL_SUCCESS) {
91 shader_destroy(&sh->obj);
92 return ret;
Chia-I Wu9194a6d2014-08-28 11:36:48 +080093 }
94
Courtney Goeltzenleuchteref5b1162014-10-10 16:29:46 -060095
Chia-I Wu9194a6d2014-08-28 11:36:48 +080096 sh->obj.destroy = shader_destroy;
97
98 *sh_ret = sh;
99
100 return XGL_SUCCESS;
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600101}
102
103XGL_RESULT XGLAPI intelCreateShader(
104 XGL_DEVICE device,
105 const XGL_SHADER_CREATE_INFO* pCreateInfo,
106 XGL_SHADER* pShader)
107{
108 struct intel_dev *dev = intel_dev(device);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600109
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800110 return shader_create(dev, pCreateInfo, (struct intel_shader **) pShader);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600111}
Courtney Goeltzenleuchterff87c822014-10-03 18:05:10 -0600112
113XGL_RESULT XGLAPI intelCreateShaderfromGLSL(
114 XGL_DEVICE device,
115 const XGL_SHADER_CREATE_INFO* pCreateInfo,
116 XGL_SHADER* pShader)
117{
118 struct intel_dev *dev = intel_dev(device);
119
120 return shader_create(dev, pCreateInfo, (struct intel_shader **) pShader);
121}