blob: c026a84b39aca44eda325fbf0151b3e277211f44 [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.
23 */
24
Chia-I Wu9194a6d2014-08-28 11:36:48 +080025#include "dev.h"
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060026#include "shader.h"
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060027
Chia-I Wu9194a6d2014-08-28 11:36:48 +080028static XGL_RESULT shader_parse_bil(struct intel_shader *sh,
29 const struct intel_gpu *gpu,
30 const struct icd_bil_header *bil,
31 XGL_SIZE size)
32{
33 struct intel_ir *ir;
34
35 ir = icd_alloc(sizeof(*ir), 0, XGL_SYSTEM_ALLOC_INTERNAL_SHADER);
36 if (!ir)
37 return XGL_ERROR_OUT_OF_MEMORY;
38
39 ir->size = size - sizeof(*bil);
40
41 ir->kernel = icd_alloc(ir->size, 0, XGL_SYSTEM_ALLOC_INTERNAL_SHADER);
42 if (!ir->kernel) {
43 icd_free(ir);
44 return XGL_ERROR_OUT_OF_MEMORY;
45 }
46
47 memcpy(ir->kernel, bil + 1, ir->size);
48
49 sh->ir = ir;
Chia-I Wuc123b7f2014-08-28 12:18:43 +080050 switch (bil->gen_magic) {
51 case 'v':
52 sh->uses |= INTEL_SHADER_USE_VID;
53 sh->in_count = 1;
54 sh->out_count = 1;
55 break;
56 case 'w':
57 sh->out_count = 1;
Chia-I Wu8108b4f2014-08-29 15:07:19 +080058 sh->surface_count = 1;
Chia-I Wuc123b7f2014-08-28 12:18:43 +080059 break;
60 default:
61 break;
62 }
Chia-I Wu9194a6d2014-08-28 11:36:48 +080063
64 return XGL_SUCCESS;
65}
66
Courtney Goeltzenleuchter42509992014-08-21 17:33:46 -060067static void shader_destroy(struct intel_obj *obj)
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060068{
Chia-I Wu9194a6d2014-08-28 11:36:48 +080069 struct intel_shader *sh = intel_shader_from_obj(obj);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -060070
Chia-I Wu9194a6d2014-08-28 11:36:48 +080071 icd_free(sh->ir);
72 intel_base_destroy(&sh->obj.base);
73}
74
75static XGL_RESULT shader_create(struct intel_dev *dev,
76 const XGL_SHADER_CREATE_INFO *info,
77 struct intel_shader **sh_ret)
78{
79 const struct icd_bil_header *bil =
80 (const struct icd_bil_header *) info->pCode;
81 struct intel_shader *sh;
82 XGL_RESULT ret;
83
84 if (info->codeSize < sizeof(*bil))
85 return XGL_ERROR_INVALID_MEMORY_SIZE;
86 if (bil->magic != ICD_BIL_MAGIC)
87 return XGL_ERROR_BAD_SHADER_CODE;
88
89 sh = (struct intel_shader *) intel_base_create(dev, sizeof(*sh),
90 dev->base.dbg, XGL_DBG_OBJECT_SHADER, info, 0);
91 if (!sh)
92 return XGL_ERROR_OUT_OF_MEMORY;
93
94 ret = shader_parse_bil(sh, dev->gpu, bil, info->codeSize);
95 if (ret != XGL_SUCCESS) {
96 shader_destroy(&sh->obj);
97 return ret;
98 }
99
100 sh->obj.destroy = shader_destroy;
101
102 *sh_ret = sh;
103
104 return XGL_SUCCESS;
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600105}
106
107XGL_RESULT XGLAPI intelCreateShader(
108 XGL_DEVICE device,
109 const XGL_SHADER_CREATE_INFO* pCreateInfo,
110 XGL_SHADER* pShader)
111{
112 struct intel_dev *dev = intel_dev(device);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600113
Chia-I Wu9194a6d2014-08-28 11:36:48 +0800114 return shader_create(dev, pCreateInfo, (struct intel_shader **) pShader);
Courtney Goeltzenleuchter52ec3362014-08-19 11:52:02 -0600115}