blob: 9e4a312012540147c50f9acb16d0df2bae8e549e [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 Goeltzenleuchterabf612b2014-10-06 12:44:43 -060033static XGL_RESULT shader_parse_glsl(struct intel_shader *sh,
34 const struct intel_gpu *gpu,
35 const XGL_INTEL_COMPILE_GLSL *glsl_header,
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
Courtney Goeltzenleuchter04b119f2014-10-09 09:30:32 -060044 ir->size = size;
45
Courtney Goeltzenleuchterabf612b2014-10-06 12:44:43 -060046 // invoke our program creation as well
Steve K10b32512014-10-10 08:54:29 -060047 ir->shader_program = shader_create_program_glsl(sh, glsl_header);
Courtney Goeltzenleuchterabf612b2014-10-06 12:44:43 -060048 if (!ir->shader_program)
49 return XGL_ERROR_BAD_SHADER_CODE;
50
51 // TODO: set necessary shader information. This should really
52 // happen as result of create_program call.
53 sh->ir = ir;
Courtney Goeltzenleuchterabf612b2014-10-06 12:44:43 -060054
55 return XGL_SUCCESS;
56}
57
Chia-I Wu9194a6d2014-08-28 11:36:48 +080058static XGL_RESULT shader_parse_bil(struct intel_shader *sh,
59 const struct intel_gpu *gpu,
60 const struct icd_bil_header *bil,
61 XGL_SIZE size)
62{
63 struct intel_ir *ir;
64
65 ir = icd_alloc(sizeof(*ir), 0, XGL_SYSTEM_ALLOC_INTERNAL_SHADER);
66 if (!ir)
67 return XGL_ERROR_OUT_OF_MEMORY;
68
69 ir->size = size - sizeof(*bil);
70
Steve K10b32512014-10-10 08:54:29 -060071 // invoke our program creation as well
72 ir->shader_program = shader_create_program_bil(sh, bil, size);
73 if (!ir->shader_program)
74 return XGL_ERROR_BAD_SHADER_CODE;
Chia-I Wu9194a6d2014-08-28 11:36:48 +080075
Steve K10b32512014-10-10 08:54:29 -060076 // TODO: set necessary shader information. This should really
77 // happen as result of create_program call.
Chia-I Wu9194a6d2014-08-28 11:36:48 +080078 sh->ir = ir;
79
80 return XGL_SUCCESS;
81}
82
Courtney Goeltzenleuchter42509992014-08-21 17:33:46 -060083static void shader_destroy(struct intel_obj *obj)
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060084{
Chia-I Wu9194a6d2014-08-28 11:36:48 +080085 struct intel_shader *sh = intel_shader_from_obj(obj);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060086
Courtney Goeltzenleuchter69bc01b2014-10-09 15:24:39 -060087 if (sh->ir) {
88 shader_destroy_program(sh->ir->shader_program);
89 }
Chia-I Wu9194a6d2014-08-28 11:36:48 +080090 icd_free(sh->ir);
91 intel_base_destroy(&sh->obj.base);
92}
93
94static XGL_RESULT shader_create(struct intel_dev *dev,
95 const XGL_SHADER_CREATE_INFO *info,
96 struct intel_shader **sh_ret)
97{
98 const struct icd_bil_header *bil =
99 (const struct icd_bil_header *) info->pCode;
100 struct intel_shader *sh;
101 XGL_RESULT ret;
102
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800103 sh = (struct intel_shader *) intel_base_create(dev, sizeof(*sh),
104 dev->base.dbg, XGL_DBG_OBJECT_SHADER, info, 0);
105 if (!sh)
106 return XGL_ERROR_OUT_OF_MEMORY;
107
Courtney Goeltzenleuchterd7f0dce2014-10-06 09:45:33 -0600108 if (dev->exts[INTEL_EXT_COMPILE_GLSL] &&
109 info->sType == (XGL_STRUCTURE_TYPE) XGL_INTEL_STRUCTURE_TYPE_SHADER_CREATE_INFO) {
110 // use GLSL compiler extension
Courtney Goeltzenleuchterabf612b2014-10-06 12:44:43 -0600111 ret = shader_parse_glsl(sh, dev->gpu, info->pCode, info->codeSize);
112 if (ret != XGL_SUCCESS) {
113 shader_destroy(&sh->obj);
114 return ret;
115 }
Courtney Goeltzenleuchterd7f0dce2014-10-06 09:45:33 -0600116 } else {
117 if (info->codeSize < sizeof(*bil))
118 return XGL_ERROR_INVALID_MEMORY_SIZE;
119 if (bil->magic != ICD_BIL_MAGIC)
120 return XGL_ERROR_BAD_SHADER_CODE;
121
122
123 ret = shader_parse_bil(sh, dev->gpu, bil, info->codeSize);
124 if (ret != XGL_SUCCESS) {
125 shader_destroy(&sh->obj);
126 return ret;
127 }
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800128 }
129
130 sh->obj.destroy = shader_destroy;
131
132 *sh_ret = sh;
133
134 return XGL_SUCCESS;
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600135}
136
137XGL_RESULT XGLAPI intelCreateShader(
138 XGL_DEVICE device,
139 const XGL_SHADER_CREATE_INFO* pCreateInfo,
140 XGL_SHADER* pShader)
141{
142 struct intel_dev *dev = intel_dev(device);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600143
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800144 return shader_create(dev, pCreateInfo, (struct intel_shader **) pShader);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600145}
Courtney Goeltzenleuchterff87c822014-10-03 18:05:10 -0600146
147XGL_RESULT XGLAPI intelCreateShaderfromGLSL(
148 XGL_DEVICE device,
149 const XGL_SHADER_CREATE_INFO* pCreateInfo,
150 XGL_SHADER* pShader)
151{
152 struct intel_dev *dev = intel_dev(device);
153
154 return shader_create(dev, pCreateInfo, (struct intel_shader **) pShader);
155}