blob: 84d6e27c385b3e852b202b2d409fb6ac51f7d66c [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
Chia-I Wu9194a6d2014-08-28 11:36:48 +080033static XGL_RESULT shader_parse_bil(struct intel_shader *sh,
34 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
46 ir->kernel = icd_alloc(ir->size, 0, XGL_SYSTEM_ALLOC_INTERNAL_SHADER);
47 if (!ir->kernel) {
48 icd_free(ir);
49 return XGL_ERROR_OUT_OF_MEMORY;
50 }
51
52 memcpy(ir->kernel, bil + 1, ir->size);
53
Cody Northrop0eb5eea2014-09-19 15:11:52 -060054 //
55 // TEMPORARY CODE TO INVOKE COMPILER
56 //
57
58 // invoke our program creation as well
59 ir->shader_program = shader_create_program(sh, bil);
60 if (!ir->shader_program)
61 return XGL_ERROR_BAD_SHADER_CODE;
62
63 //
64 // END TEMPORARY CODE
65 //
66
Chia-I Wu9194a6d2014-08-28 11:36:48 +080067 sh->ir = ir;
Chia-I Wuc123b7f2014-08-28 12:18:43 +080068 switch (bil->gen_magic) {
69 case 'v':
70 sh->uses |= INTEL_SHADER_USE_VID;
71 sh->in_count = 1;
Chia-I Wu277a8e92014-08-29 20:02:06 +080072 sh->out_count = 2;
Chia-I Wu29641c12014-08-31 00:35:27 +080073 sh->urb_grf_start = 1;
Chia-I Wuc123b7f2014-08-28 12:18:43 +080074 break;
75 case 'w':
76 sh->out_count = 1;
Chia-I Wu8108b4f2014-08-29 15:07:19 +080077 sh->surface_count = 1;
Chia-I Wu29641c12014-08-31 00:35:27 +080078 sh->urb_grf_start = 2;
Chia-I Wuc123b7f2014-08-28 12:18:43 +080079 break;
80 default:
81 break;
82 }
Chia-I Wu9194a6d2014-08-28 11:36:48 +080083
84 return XGL_SUCCESS;
85}
86
Courtney Goeltzenleuchter42509992014-08-21 17:33:46 -060087static void shader_destroy(struct intel_obj *obj)
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060088{
Chia-I Wu9194a6d2014-08-28 11:36:48 +080089 struct intel_shader *sh = intel_shader_from_obj(obj);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060090
Cody Northrop0eb5eea2014-09-19 15:11:52 -060091 shader_destroy_program(sh->ir->shader_program);
Chia-I Wu9194a6d2014-08-28 11:36:48 +080092 icd_free(sh->ir);
93 intel_base_destroy(&sh->obj.base);
94}
95
96static XGL_RESULT shader_create(struct intel_dev *dev,
97 const XGL_SHADER_CREATE_INFO *info,
98 struct intel_shader **sh_ret)
99{
100 const struct icd_bil_header *bil =
101 (const struct icd_bil_header *) info->pCode;
102 struct intel_shader *sh;
103 XGL_RESULT ret;
104
105 if (info->codeSize < sizeof(*bil))
106 return XGL_ERROR_INVALID_MEMORY_SIZE;
107 if (bil->magic != ICD_BIL_MAGIC)
108 return XGL_ERROR_BAD_SHADER_CODE;
109
110 sh = (struct intel_shader *) intel_base_create(dev, sizeof(*sh),
111 dev->base.dbg, XGL_DBG_OBJECT_SHADER, info, 0);
112 if (!sh)
113 return XGL_ERROR_OUT_OF_MEMORY;
114
115 ret = shader_parse_bil(sh, dev->gpu, bil, info->codeSize);
116 if (ret != XGL_SUCCESS) {
117 shader_destroy(&sh->obj);
118 return ret;
119 }
120
121 sh->obj.destroy = shader_destroy;
122
123 *sh_ret = sh;
124
125 return XGL_SUCCESS;
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600126}
127
128XGL_RESULT XGLAPI intelCreateShader(
129 XGL_DEVICE device,
130 const XGL_SHADER_CREATE_INFO* pCreateInfo,
131 XGL_SHADER* pShader)
132{
133 struct intel_dev *dev = intel_dev(device);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600134
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800135 return shader_create(dev, pCreateInfo, (struct intel_shader **) pShader);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600136}