blob: 9215df7fa4f216926375474499d9c75651573d60 [file] [log] [blame]
Chia-I Wu1f7540b2014-08-22 13:56:18 +08001/*
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 * Chia-I Wu <olv@lunarg.com>
Chia-I Wu1f7540b2014-08-22 13:56:18 +080026 */
27
Chia-I Wu98824592014-09-02 09:42:46 +080028#include "shader.h"
Chia-I Wu1f7540b2014-08-22 13:56:18 +080029#include "pipeline_priv.h"
30
Chia-I Wu20983762014-09-02 12:07:28 +080031static struct intel_pipeline_rmap_slot *rmap_get_slot(struct intel_pipeline_rmap *rmap,
32 XGL_DESCRIPTOR_SET_SLOT_TYPE type,
33 XGL_UINT index)
Chia-I Wu1f7540b2014-08-22 13:56:18 +080034{
35 const XGL_UINT resource_offset = rmap->rt_count;
36 const XGL_UINT uav_offset = resource_offset + rmap->resource_count;
37 const XGL_UINT sampler_offset = uav_offset + rmap->uav_count;
Chia-I Wu20983762014-09-02 12:07:28 +080038 struct intel_pipeline_rmap_slot *slot;
Chia-I Wu1f7540b2014-08-22 13:56:18 +080039
40 switch (type) {
41 case XGL_SLOT_UNUSED:
42 slot = NULL;
43 break;
44 case XGL_SLOT_SHADER_RESOURCE:
45 slot = &rmap->slots[resource_offset + index];
46 break;
47 case XGL_SLOT_SHADER_UAV:
48 slot = &rmap->slots[uav_offset + index];
49 break;
50 case XGL_SLOT_SHADER_SAMPLER:
51 slot = &rmap->slots[sampler_offset + index];
52 break;
53 default:
54 assert(!"unknown rmap slot type");
55 slot = NULL;
56 break;
57 }
58
59 return slot;
60}
61
Chia-I Wu20983762014-09-02 12:07:28 +080062static bool rmap_init_slots_with_path(struct intel_pipeline_rmap *rmap,
Chia-I Wu1f7540b2014-08-22 13:56:18 +080063 const XGL_DESCRIPTOR_SET_MAPPING *mapping,
64 XGL_UINT *nest_path,
65 XGL_UINT nest_level)
66{
67 XGL_UINT i;
68
69 for (i = 0; i < mapping->descriptorCount; i++) {
70 const XGL_DESCRIPTOR_SLOT_INFO *info = &mapping->pDescriptorInfo[i];
Chia-I Wu20983762014-09-02 12:07:28 +080071 struct intel_pipeline_rmap_slot *slot;
Chia-I Wu1f7540b2014-08-22 13:56:18 +080072
73 if (info->slotObjectType == XGL_SLOT_NEXT_DESCRIPTOR_SET) {
74 nest_path[nest_level] = i;
75 if (!rmap_init_slots_with_path(rmap, info->pNextLevelSet,
76 nest_path, nest_level + 1))
77 return false;
78
79 continue;
80 }
81
82 slot = rmap_get_slot(rmap, info->slotObjectType,
83 info->shaderEntityIndex);
84 if (!slot)
85 continue;
86
87 assert(!slot->path_len);
88 slot->path_len = nest_level + 1;
89
90 if (nest_level) {
91 slot->u.path = icd_alloc(sizeof(slot->u.path[0]) *
92 slot->path_len, 0, XGL_SYSTEM_ALLOC_INTERNAL);
93 if (!slot->u.path) {
94 slot->path_len = 0;
95 return false;
96 }
97
98 memcpy(slot->u.path, nest_path,
99 sizeof(slot->u.path[0]) * nest_level);
100 slot->u.path[nest_level] = i;
101 } else {
102 slot->u.index = i;
103 }
104 }
105
106 return true;
107}
108
Chia-I Wu20983762014-09-02 12:07:28 +0800109static bool rmap_init_slots(struct intel_pipeline_rmap *rmap,
Chia-I Wu1f7540b2014-08-22 13:56:18 +0800110 const XGL_DESCRIPTOR_SET_MAPPING *mapping,
111 XGL_UINT depth)
112{
113 XGL_UINT *nest_path;
114 bool ok;
115
116 if (depth) {
117 nest_path = icd_alloc(sizeof(nest_path[0]) * depth,
118 0, XGL_SYSTEM_ALLOC_INTERNAL_TEMP);
119 if (!nest_path)
120 return false;
121 } else {
122 nest_path = NULL;
123 }
124
125 ok = rmap_init_slots_with_path(rmap, mapping, nest_path, 0);
126
127 if (nest_path)
128 icd_free(nest_path);
129
130 return ok;
131}
132
Chia-I Wu20983762014-09-02 12:07:28 +0800133static void rmap_update_count(struct intel_pipeline_rmap *rmap,
Chia-I Wu1f7540b2014-08-22 13:56:18 +0800134 XGL_DESCRIPTOR_SET_SLOT_TYPE type,
135 XGL_UINT index)
136{
137 switch (type) {
138 case XGL_SLOT_UNUSED:
139 break;
140 case XGL_SLOT_SHADER_RESOURCE:
141 if (rmap->resource_count < index + 1)
142 rmap->resource_count = index + 1;
143 break;
144 case XGL_SLOT_SHADER_UAV:
145 if (rmap->uav_count < index + 1)
146 rmap->uav_count = index + 1;
147 break;
148 case XGL_SLOT_SHADER_SAMPLER:
149 if (rmap->sampler_count < index + 1)
150 rmap->sampler_count = index + 1;
151 break;
152 default:
153 assert(!"unknown rmap slot type");
154 break;
155 }
156}
157
Chia-I Wu20983762014-09-02 12:07:28 +0800158static XGL_UINT rmap_init_counts(struct intel_pipeline_rmap *rmap,
Chia-I Wu1f7540b2014-08-22 13:56:18 +0800159 const XGL_DESCRIPTOR_SET_MAPPING *mapping)
160{
161 XGL_UINT depth = 0;
162 XGL_UINT i;
163
164 for (i = 0; i < mapping->descriptorCount; i++) {
165 const XGL_DESCRIPTOR_SLOT_INFO *info = &mapping->pDescriptorInfo[i];
166
167 if (info->slotObjectType == XGL_SLOT_NEXT_DESCRIPTOR_SET) {
168 const XGL_UINT d = rmap_init_counts(rmap,
169 info->pNextLevelSet);
170 if (depth < d + 1)
171 depth = d + 1;
172
173 continue;
174 }
175
176 rmap_update_count(rmap, info->slotObjectType,
177 info->shaderEntityIndex);
178 }
179
180 return depth;
181}
182
Chia-I Wu20983762014-09-02 12:07:28 +0800183static void rmap_destroy(struct intel_pipeline_rmap *rmap)
Chia-I Wua6d50aa2014-09-02 10:21:34 +0800184{
185 XGL_UINT i;
186
187 for (i = 0; i < rmap->slot_count; i++) {
Chia-I Wu20983762014-09-02 12:07:28 +0800188 struct intel_pipeline_rmap_slot *slot = &rmap->slots[i];
Chia-I Wua6d50aa2014-09-02 10:21:34 +0800189
190 switch (slot->path_len) {
191 case 0:
192 case 1:
Chia-I Wu20983762014-09-02 12:07:28 +0800193 case INTEL_PIPELINE_RMAP_SLOT_RT:
194 case INTEL_PIPELINE_RMAP_SLOT_DYN:
Chia-I Wua6d50aa2014-09-02 10:21:34 +0800195 break;
196 default:
197 icd_free(slot->u.path);
198 break;
199 }
200 }
201
202 icd_free(rmap->slots);
203 icd_free(rmap);
204}
205
Chia-I Wu20983762014-09-02 12:07:28 +0800206static struct intel_pipeline_rmap *rmap_create(struct intel_dev *dev,
207 const XGL_DESCRIPTOR_SET_MAPPING *mapping,
208 const XGL_DYNAMIC_MEMORY_VIEW_SLOT_INFO *dyn,
209 XGL_UINT rt_count)
Chia-I Wu1f7540b2014-08-22 13:56:18 +0800210{
Chia-I Wu20983762014-09-02 12:07:28 +0800211 struct intel_pipeline_rmap *rmap;
212 struct intel_pipeline_rmap_slot *slot;
Chia-I Wu1f7540b2014-08-22 13:56:18 +0800213 XGL_UINT depth, rt;
214
215 rmap = icd_alloc(sizeof(*rmap), 0, XGL_SYSTEM_ALLOC_INTERNAL);
216 if (!rmap)
217 return NULL;
218
219 memset(rmap, 0, sizeof(*rmap));
220
221 depth = rmap_init_counts(rmap, mapping);
222
223 /* add RTs and the dynamic memory view */
224 rmap_update_count(rmap, dyn->slotObjectType, dyn->shaderEntityIndex);
225 rmap->rt_count = rt_count;
226
227 rmap->slot_count = rmap->rt_count + rmap->resource_count +
228 rmap->uav_count + rmap->sampler_count;
229
230 rmap->slots = icd_alloc(sizeof(rmap->slots[0]) * rmap->slot_count,
231 0, XGL_SYSTEM_ALLOC_INTERNAL);
232 if (!rmap->slots) {
233 icd_free(rmap);
234 return NULL;
235 }
236
237 memset(rmap->slots, 0, sizeof(rmap->slots[0]) * rmap->slot_count);
238
239 if (!rmap_init_slots(rmap, mapping, depth)) {
Chia-I Wua6d50aa2014-09-02 10:21:34 +0800240 rmap_destroy(rmap);
Chia-I Wu1f7540b2014-08-22 13:56:18 +0800241 return NULL;
242 }
243
244 /* add RTs and the dynamic memory view */
245 slot = rmap_get_slot(rmap, dyn->slotObjectType, dyn->shaderEntityIndex);
246 if (slot) {
Chia-I Wu20983762014-09-02 12:07:28 +0800247 slot->path_len = INTEL_PIPELINE_RMAP_SLOT_DYN;
Chia-I Wu1f7540b2014-08-22 13:56:18 +0800248 slot->u.index = 0;
249 }
250 for (rt = 0; rt < rmap->rt_count; rt++) {
251 slot = &rmap->slots[rt];
Chia-I Wu20983762014-09-02 12:07:28 +0800252 slot->path_len = INTEL_PIPELINE_RMAP_SLOT_RT;
Chia-I Wu1f7540b2014-08-22 13:56:18 +0800253 slot->u.index = rt;
254 }
255
256 return rmap;
257}
258
Chia-I Wuf2b6d722014-09-02 08:52:27 +0800259static XGL_RESULT pipeline_shader_copy_ir(struct intel_pipeline_shader *sh,
Chia-I Wu39026c92014-09-02 10:03:19 +0800260 const struct intel_shader *ir)
Chia-I Wu98824592014-09-02 09:42:46 +0800261{
Chia-I Wu39026c92014-09-02 10:03:19 +0800262 sh->pCode = icd_alloc(ir->ir->size, 0, XGL_SYSTEM_ALLOC_INTERNAL_SHADER);
263 if (!sh->pCode)
Chia-I Wu98824592014-09-02 09:42:46 +0800264 return XGL_ERROR_OUT_OF_MEMORY;
265
Chia-I Wu39026c92014-09-02 10:03:19 +0800266 memcpy(sh->pCode, ir->ir->kernel, ir->ir->size);
267 sh->codeSize = ir->ir->size;
Chia-I Wu98824592014-09-02 09:42:46 +0800268
Chia-I Wu39026c92014-09-02 10:03:19 +0800269 sh->uses = ir->uses;
Chia-I Wu98824592014-09-02 09:42:46 +0800270
Chia-I Wu39026c92014-09-02 10:03:19 +0800271 sh->in_count = ir->in_count;
272 sh->out_count = ir->out_count;
273 sh->sampler_count = ir->sampler_count;
274 sh->surface_count = ir->surface_count;
275 sh->urb_grf_start = ir->urb_grf_start;
Chia-I Wu39026c92014-09-02 10:03:19 +0800276 sh->barycentric_interps = ir->barycentric_interps;
277
278 return XGL_SUCCESS;
279}
280
281static XGL_RESULT pipeline_build_vs(struct intel_pipeline *pipeline,
282 const struct intel_pipeline_create_info *info)
283{
Chia-I Wuf2b6d722014-09-02 08:52:27 +0800284 struct intel_pipeline_shader *vs = &pipeline->vs;
Chia-I Wu39026c92014-09-02 10:03:19 +0800285 XGL_RESULT ret;
286
287 ret = pipeline_shader_copy_ir(vs, intel_shader(info->vs.shader));
288 if (ret != XGL_SUCCESS)
289 return ret;
290
Chia-I Wu46809782014-10-07 15:40:38 +0800291 assert(!info->vs.linkConstBufferCount);
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800292
Chia-I Wu39026c92014-09-02 10:03:19 +0800293 vs->rmap = rmap_create(pipeline->dev,
294 &info->vs.descriptorSetMapping[0],
295 &info->vs.dynamicMemoryViewMapping, 0);
296 if (!vs->rmap) {
297 icd_free(vs->pCode);
298 return XGL_ERROR_OUT_OF_MEMORY;
Chia-I Wu98824592014-09-02 09:42:46 +0800299 }
300
Chia-I Wu39026c92014-09-02 10:03:19 +0800301 pipeline->active_shaders |= SHADER_VERTEX_FLAG;
302
303 return XGL_SUCCESS;
304}
305
306static XGL_RESULT pipeline_build_tcs(struct intel_pipeline *pipeline,
307 const struct intel_pipeline_create_info *info)
308{
Chia-I Wu95959fb2014-09-02 11:01:03 +0800309 struct intel_pipeline_shader *tcs = &pipeline->tcs;
Chia-I Wu39026c92014-09-02 10:03:19 +0800310 XGL_RESULT ret;
311
312 ret = pipeline_shader_copy_ir(tcs, intel_shader(info->tcs.shader));
313 if (ret != XGL_SUCCESS)
314 return ret;
315
Chia-I Wu46809782014-10-07 15:40:38 +0800316 assert(!info->tcs.linkConstBufferCount);
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800317
Chia-I Wu39026c92014-09-02 10:03:19 +0800318 pipeline->active_shaders |= SHADER_TESS_CONTROL_FLAG;
319
320 return XGL_SUCCESS;
321}
322
323static XGL_RESULT pipeline_build_tes(struct intel_pipeline *pipeline,
324 const struct intel_pipeline_create_info *info)
325{
Chia-I Wu95959fb2014-09-02 11:01:03 +0800326 struct intel_pipeline_shader *tes = &pipeline->tes;
Chia-I Wu39026c92014-09-02 10:03:19 +0800327 XGL_RESULT ret;
328
329 ret = pipeline_shader_copy_ir(tes, intel_shader(info->tes.shader));
330 if (ret != XGL_SUCCESS)
331 return ret;
332
Chia-I Wu46809782014-10-07 15:40:38 +0800333 assert(!info->tes.linkConstBufferCount);
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800334
Chia-I Wu39026c92014-09-02 10:03:19 +0800335 pipeline->active_shaders |= SHADER_TESS_EVAL_FLAG;
336
337 return XGL_SUCCESS;
338}
339
340static XGL_RESULT pipeline_build_gs(struct intel_pipeline *pipeline,
341 const struct intel_pipeline_create_info *info)
342{
Chia-I Wuf2b6d722014-09-02 08:52:27 +0800343 struct intel_pipeline_shader *gs = &pipeline->gs;
Chia-I Wu39026c92014-09-02 10:03:19 +0800344 XGL_RESULT ret;
345
346 ret = pipeline_shader_copy_ir(gs, intel_shader(info->gs.shader));
347 if (ret != XGL_SUCCESS)
348 return ret;
349
Chia-I Wu46809782014-10-07 15:40:38 +0800350 assert(!info->tes.linkConstBufferCount);
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800351
Chia-I Wu39026c92014-09-02 10:03:19 +0800352 pipeline->active_shaders |= SHADER_GEOMETRY_FLAG;
353
354 return XGL_SUCCESS;
355}
356
357static XGL_RESULT pipeline_build_fs(struct intel_pipeline *pipeline,
358 const struct intel_pipeline_create_info *info)
359{
Chia-I Wuf2b6d722014-09-02 08:52:27 +0800360 struct intel_pipeline_shader *fs = &pipeline->fs;
Chia-I Wu39026c92014-09-02 10:03:19 +0800361 XGL_RESULT ret;
362
363 ret = pipeline_shader_copy_ir(fs, intel_shader(info->fs.shader));
364 if (ret != XGL_SUCCESS)
365 return ret;
366
Chia-I Wu46809782014-10-07 15:40:38 +0800367 assert(!info->fs.linkConstBufferCount);
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800368
Chia-I Wu39026c92014-09-02 10:03:19 +0800369 /* assuming one RT; need to parse the shader */
370 fs->rmap = rmap_create(pipeline->dev,
371 &info->fs.descriptorSetMapping[0],
372 &info->fs.dynamicMemoryViewMapping, 1);
373 if (!fs->rmap) {
374 icd_free(fs->pCode);
375 return XGL_ERROR_OUT_OF_MEMORY;
376 }
377
Chia-I Wu39026c92014-09-02 10:03:19 +0800378 pipeline->active_shaders |= SHADER_FRAGMENT_FLAG;
379
380 return XGL_SUCCESS;
381}
382
383static XGL_RESULT pipeline_build_cs(struct intel_pipeline *pipeline,
384 const struct intel_pipeline_create_info *info)
385{
Chia-I Wu95959fb2014-09-02 11:01:03 +0800386 struct intel_pipeline_shader *cs = &pipeline->cs;
Chia-I Wu39026c92014-09-02 10:03:19 +0800387 XGL_RESULT ret;
388
389 ret = pipeline_shader_copy_ir(cs, intel_shader(info->compute.cs.shader));
390 if (ret != XGL_SUCCESS)
391 return ret;
392
Chia-I Wu46809782014-10-07 15:40:38 +0800393 assert(!info->compute.cs.linkConstBufferCount);
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800394
Chia-I Wu39026c92014-09-02 10:03:19 +0800395 pipeline->active_shaders |= SHADER_COMPUTE_FLAG;
396
Chia-I Wu98824592014-09-02 09:42:46 +0800397 return XGL_SUCCESS;
398}
399
400XGL_RESULT pipeline_build_shaders(struct intel_pipeline *pipeline,
401 const struct intel_pipeline_create_info *info)
402{
403 XGL_RESULT ret = XGL_SUCCESS;
404
405 if (ret == XGL_SUCCESS && info->vs.shader)
Chia-I Wu39026c92014-09-02 10:03:19 +0800406 ret = pipeline_build_vs(pipeline, info);
Chia-I Wu98824592014-09-02 09:42:46 +0800407 if (ret == XGL_SUCCESS && info->tcs.shader)
Chia-I Wu39026c92014-09-02 10:03:19 +0800408 ret = pipeline_build_tcs(pipeline, info);
Chia-I Wu98824592014-09-02 09:42:46 +0800409 if (ret == XGL_SUCCESS && info->tes.shader)
Chia-I Wu39026c92014-09-02 10:03:19 +0800410 ret = pipeline_build_tes(pipeline, info);
Chia-I Wu98824592014-09-02 09:42:46 +0800411 if (ret == XGL_SUCCESS && info->gs.shader)
Chia-I Wu39026c92014-09-02 10:03:19 +0800412 ret = pipeline_build_gs(pipeline, info);
Chia-I Wu98824592014-09-02 09:42:46 +0800413 if (ret == XGL_SUCCESS && info->fs.shader)
Chia-I Wu39026c92014-09-02 10:03:19 +0800414 ret = pipeline_build_fs(pipeline, info);
415
416 if (ret == XGL_SUCCESS && info->compute.cs.shader)
417 ret = pipeline_build_cs(pipeline, info);
Chia-I Wu98824592014-09-02 09:42:46 +0800418
419 return ret;
420}
421
Chia-I Wuf2b6d722014-09-02 08:52:27 +0800422static void pipeline_tear_shader(struct intel_pipeline_shader *sh)
Chia-I Wu39026c92014-09-02 10:03:19 +0800423{
424 icd_free(sh->pCode);
425 if (sh->rmap)
426 rmap_destroy(sh->rmap);
427}
428
Chia-I Wu98824592014-09-02 09:42:46 +0800429void pipeline_tear_shaders(struct intel_pipeline *pipeline)
430{
431 if (pipeline->active_shaders & SHADER_VERTEX_FLAG) {
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800432 pipeline_tear_shader(&pipeline->vs);
Chia-I Wu98824592014-09-02 09:42:46 +0800433 }
434
Chia-I Wu39026c92014-09-02 10:03:19 +0800435 if (pipeline->active_shaders & SHADER_TESS_CONTROL_FLAG) {
Chia-I Wu95959fb2014-09-02 11:01:03 +0800436 pipeline_tear_shader(&pipeline->tcs);
Chia-I Wu39026c92014-09-02 10:03:19 +0800437 }
438
439 if (pipeline->active_shaders & SHADER_TESS_EVAL_FLAG) {
Chia-I Wu95959fb2014-09-02 11:01:03 +0800440 pipeline_tear_shader(&pipeline->tes);
Chia-I Wu39026c92014-09-02 10:03:19 +0800441 }
442
443 if (pipeline->active_shaders & SHADER_GEOMETRY_FLAG) {
444 pipeline_tear_shader(&pipeline->gs);
445 }
446
447 if (pipeline->active_shaders & SHADER_FRAGMENT_FLAG) {
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800448 pipeline_tear_shader(&pipeline->fs);
Chia-I Wu39026c92014-09-02 10:03:19 +0800449 }
450
451 if (pipeline->active_shaders & SHADER_COMPUTE_FLAG) {
Chia-I Wu95959fb2014-09-02 11:01:03 +0800452 pipeline_tear_shader(&pipeline->cs);
Chia-I Wu39026c92014-09-02 10:03:19 +0800453 }
Chia-I Wu98824592014-09-02 09:42:46 +0800454}