blob: f4b9a0c12155e9f98f0167ca49a7d3432adb43a8 [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
31static struct intel_rmap_slot *rmap_get_slot(struct intel_rmap *rmap,
32 XGL_DESCRIPTOR_SET_SLOT_TYPE type,
33 XGL_UINT index)
34{
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;
38 struct intel_rmap_slot *slot;
39
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
62static bool rmap_init_slots_with_path(struct intel_rmap *rmap,
63 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];
71 struct intel_rmap_slot *slot;
72
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
109static bool rmap_init_slots(struct intel_rmap *rmap,
110 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
133static void rmap_update_count(struct intel_rmap *rmap,
134 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
158static XGL_UINT rmap_init_counts(struct intel_rmap *rmap,
159 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 Wua6d50aa2014-09-02 10:21:34 +0800183static void rmap_destroy(struct intel_rmap *rmap)
184{
185 XGL_UINT i;
186
187 for (i = 0; i < rmap->slot_count; i++) {
188 struct intel_rmap_slot *slot = &rmap->slots[i];
189
190 switch (slot->path_len) {
191 case 0:
192 case 1:
193 case INTEL_RMAP_SLOT_RT:
194 case INTEL_RMAP_SLOT_DYN:
195 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
206static struct intel_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{
211 struct intel_rmap *rmap;
212 struct intel_rmap_slot *slot;
213 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) {
247 slot->path_len = INTEL_RMAP_SLOT_DYN;
248 slot->u.index = 0;
249 }
250 for (rt = 0; rt < rmap->rt_count; rt++) {
251 slot = &rmap->slots[rt];
252 slot->path_len = INTEL_RMAP_SLOT_RT;
253 slot->u.index = rt;
254 }
255
256 return rmap;
257}
258
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800259static XGL_RESULT pipeline_shader_copy_pcb(struct intel_pipe_shader *sh,
260 const XGL_LINK_CONST_BUFFER *buffers,
261 XGL_UINT buffer_count)
262{
263 XGL_SIZE pcb_size = 0;
264 XGL_UINT i;
265
266 /*
267 * XXX This is not enough. We need a mapping from buffer id to pcb
268 * offset, and we need to check against hardware limits.
269 */
270 for (i = 0; i < buffer_count; i++) {
271 const XGL_LINK_CONST_BUFFER *buf = &buffers[i];
272
273 pcb_size += buf->bufferSize;
274 }
275
276 sh->pcb = icd_alloc(pcb_size, 0, XGL_SYSTEM_ALLOC_INTERNAL);
277 if (!sh->pcb)
278 return XGL_ERROR_OUT_OF_MEMORY;
279
280 pcb_size = 0;
281 for (i = 0; i < buffer_count; i++) {
282 const XGL_LINK_CONST_BUFFER *buf = &buffers[i];
283
284 memcpy((char *) sh->pcb + pcb_size, buf->pBufferData, buf->bufferSize);
285 pcb_size += buf->bufferSize;
286 }
287
288 sh->pcb_size = pcb_size;
289
290 return XGL_SUCCESS;
291}
292
Chia-I Wu39026c92014-09-02 10:03:19 +0800293static XGL_RESULT pipeline_shader_copy_ir(struct intel_pipe_shader *sh,
294 const struct intel_shader *ir)
Chia-I Wu98824592014-09-02 09:42:46 +0800295{
Chia-I Wu39026c92014-09-02 10:03:19 +0800296 sh->pCode = icd_alloc(ir->ir->size, 0, XGL_SYSTEM_ALLOC_INTERNAL_SHADER);
297 if (!sh->pCode)
Chia-I Wu98824592014-09-02 09:42:46 +0800298 return XGL_ERROR_OUT_OF_MEMORY;
299
Chia-I Wu39026c92014-09-02 10:03:19 +0800300 memcpy(sh->pCode, ir->ir->kernel, ir->ir->size);
301 sh->codeSize = ir->ir->size;
Chia-I Wu98824592014-09-02 09:42:46 +0800302
Chia-I Wu39026c92014-09-02 10:03:19 +0800303 sh->uses = ir->uses;
Chia-I Wu98824592014-09-02 09:42:46 +0800304
Chia-I Wu39026c92014-09-02 10:03:19 +0800305 sh->in_count = ir->in_count;
306 sh->out_count = ir->out_count;
307 sh->sampler_count = ir->sampler_count;
308 sh->surface_count = ir->surface_count;
309 sh->urb_grf_start = ir->urb_grf_start;
310 sh->urb_read_length = ir->urb_read_length;
311 sh->barycentric_interps = ir->barycentric_interps;
312
313 return XGL_SUCCESS;
314}
315
316static XGL_RESULT pipeline_build_vs(struct intel_pipeline *pipeline,
317 const struct intel_pipeline_create_info *info)
318{
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800319 struct intel_pipe_shader *vs = &pipeline->vs;
Chia-I Wu39026c92014-09-02 10:03:19 +0800320 XGL_RESULT ret;
321
322 ret = pipeline_shader_copy_ir(vs, intel_shader(info->vs.shader));
323 if (ret != XGL_SUCCESS)
324 return ret;
325
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800326 ret = pipeline_shader_copy_pcb(vs, info->vs.pLinkConstBufferInfo,
327 info->vs.linkConstBufferCount);
328 if (ret != XGL_SUCCESS) {
329 icd_free(vs->pCode);
330 return ret;
331 }
332
Chia-I Wu39026c92014-09-02 10:03:19 +0800333 vs->rmap = rmap_create(pipeline->dev,
334 &info->vs.descriptorSetMapping[0],
335 &info->vs.dynamicMemoryViewMapping, 0);
336 if (!vs->rmap) {
337 icd_free(vs->pCode);
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800338 icd_free(vs->pcb);
Chia-I Wu39026c92014-09-02 10:03:19 +0800339 return XGL_ERROR_OUT_OF_MEMORY;
Chia-I Wu98824592014-09-02 09:42:46 +0800340 }
341
Chia-I Wu39026c92014-09-02 10:03:19 +0800342 pipeline->active_shaders |= SHADER_VERTEX_FLAG;
343
344 return XGL_SUCCESS;
345}
346
347static XGL_RESULT pipeline_build_tcs(struct intel_pipeline *pipeline,
348 const struct intel_pipeline_create_info *info)
349{
350 struct intel_pipe_shader *tcs = &pipeline->tess_control;
351 XGL_RESULT ret;
352
353 ret = pipeline_shader_copy_ir(tcs, intel_shader(info->tcs.shader));
354 if (ret != XGL_SUCCESS)
355 return ret;
356
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800357 ret = pipeline_shader_copy_pcb(tcs, info->tcs.pLinkConstBufferInfo,
358 info->tcs.linkConstBufferCount);
359 if (ret != XGL_SUCCESS) {
360 icd_free(tcs->pCode);
361 return ret;
362 }
363
Chia-I Wu39026c92014-09-02 10:03:19 +0800364 pipeline->active_shaders |= SHADER_TESS_CONTROL_FLAG;
365
366 return XGL_SUCCESS;
367}
368
369static XGL_RESULT pipeline_build_tes(struct intel_pipeline *pipeline,
370 const struct intel_pipeline_create_info *info)
371{
372 struct intel_pipe_shader *tes = &pipeline->tess_eval;
373 XGL_RESULT ret;
374
375 ret = pipeline_shader_copy_ir(tes, intel_shader(info->tes.shader));
376 if (ret != XGL_SUCCESS)
377 return ret;
378
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800379 ret = pipeline_shader_copy_pcb(tes, info->tes.pLinkConstBufferInfo,
380 info->tes.linkConstBufferCount);
381 if (ret != XGL_SUCCESS) {
382 icd_free(tes->pCode);
383 return ret;
384 }
385
Chia-I Wu39026c92014-09-02 10:03:19 +0800386 pipeline->active_shaders |= SHADER_TESS_EVAL_FLAG;
387
388 return XGL_SUCCESS;
389}
390
391static XGL_RESULT pipeline_build_gs(struct intel_pipeline *pipeline,
392 const struct intel_pipeline_create_info *info)
393{
394 struct intel_pipe_shader *gs = &pipeline->gs;
395 XGL_RESULT ret;
396
397 ret = pipeline_shader_copy_ir(gs, intel_shader(info->gs.shader));
398 if (ret != XGL_SUCCESS)
399 return ret;
400
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800401 ret = pipeline_shader_copy_pcb(gs, info->gs.pLinkConstBufferInfo,
402 info->gs.linkConstBufferCount);
403 if (ret != XGL_SUCCESS) {
404 icd_free(gs->pCode);
405 return ret;
406 }
407
Chia-I Wu39026c92014-09-02 10:03:19 +0800408 pipeline->active_shaders |= SHADER_GEOMETRY_FLAG;
409
410 return XGL_SUCCESS;
411}
412
413static XGL_RESULT pipeline_build_fs(struct intel_pipeline *pipeline,
414 const struct intel_pipeline_create_info *info)
415{
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800416 struct intel_pipe_shader *fs = &pipeline->fs;
Chia-I Wu39026c92014-09-02 10:03:19 +0800417 XGL_RESULT ret;
418
419 ret = pipeline_shader_copy_ir(fs, intel_shader(info->fs.shader));
420 if (ret != XGL_SUCCESS)
421 return ret;
422
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800423 ret = pipeline_shader_copy_pcb(fs, info->fs.pLinkConstBufferInfo,
424 info->fs.linkConstBufferCount);
425 if (ret != XGL_SUCCESS) {
426 icd_free(fs->pCode);
427 return ret;
428 }
429
Chia-I Wu39026c92014-09-02 10:03:19 +0800430 /* assuming one RT; need to parse the shader */
431 fs->rmap = rmap_create(pipeline->dev,
432 &info->fs.descriptorSetMapping[0],
433 &info->fs.dynamicMemoryViewMapping, 1);
434 if (!fs->rmap) {
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800435 icd_free(fs->pcb);
Chia-I Wu39026c92014-09-02 10:03:19 +0800436 icd_free(fs->pCode);
437 return XGL_ERROR_OUT_OF_MEMORY;
438 }
439
Chia-I Wu39026c92014-09-02 10:03:19 +0800440 pipeline->active_shaders |= SHADER_FRAGMENT_FLAG;
441
442 return XGL_SUCCESS;
443}
444
445static XGL_RESULT pipeline_build_cs(struct intel_pipeline *pipeline,
446 const struct intel_pipeline_create_info *info)
447{
448 struct intel_pipe_shader *cs = &pipeline->compute;
449 XGL_RESULT ret;
450
451 ret = pipeline_shader_copy_ir(cs, intel_shader(info->compute.cs.shader));
452 if (ret != XGL_SUCCESS)
453 return ret;
454
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800455 ret = pipeline_shader_copy_pcb(cs, info->compute.cs.pLinkConstBufferInfo,
456 info->compute.cs.linkConstBufferCount);
457 if (ret != XGL_SUCCESS) {
458 icd_free(cs->pCode);
459 return ret;
460 }
461
Chia-I Wu39026c92014-09-02 10:03:19 +0800462 pipeline->active_shaders |= SHADER_COMPUTE_FLAG;
463
Chia-I Wu98824592014-09-02 09:42:46 +0800464 return XGL_SUCCESS;
465}
466
467XGL_RESULT pipeline_build_shaders(struct intel_pipeline *pipeline,
468 const struct intel_pipeline_create_info *info)
469{
470 XGL_RESULT ret = XGL_SUCCESS;
471
472 if (ret == XGL_SUCCESS && info->vs.shader)
Chia-I Wu39026c92014-09-02 10:03:19 +0800473 ret = pipeline_build_vs(pipeline, info);
Chia-I Wu98824592014-09-02 09:42:46 +0800474 if (ret == XGL_SUCCESS && info->tcs.shader)
Chia-I Wu39026c92014-09-02 10:03:19 +0800475 ret = pipeline_build_tcs(pipeline, info);
Chia-I Wu98824592014-09-02 09:42:46 +0800476 if (ret == XGL_SUCCESS && info->tes.shader)
Chia-I Wu39026c92014-09-02 10:03:19 +0800477 ret = pipeline_build_tes(pipeline, info);
Chia-I Wu98824592014-09-02 09:42:46 +0800478 if (ret == XGL_SUCCESS && info->gs.shader)
Chia-I Wu39026c92014-09-02 10:03:19 +0800479 ret = pipeline_build_gs(pipeline, info);
Chia-I Wu98824592014-09-02 09:42:46 +0800480 if (ret == XGL_SUCCESS && info->fs.shader)
Chia-I Wu39026c92014-09-02 10:03:19 +0800481 ret = pipeline_build_fs(pipeline, info);
482
483 if (ret == XGL_SUCCESS && info->compute.cs.shader)
484 ret = pipeline_build_cs(pipeline, info);
Chia-I Wu98824592014-09-02 09:42:46 +0800485
486 return ret;
487}
488
Chia-I Wu39026c92014-09-02 10:03:19 +0800489static void pipeline_tear_shader(struct intel_pipe_shader *sh)
490{
491 icd_free(sh->pCode);
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800492 if (sh->pcb)
493 icd_free(sh->pcb);
Chia-I Wu39026c92014-09-02 10:03:19 +0800494 if (sh->rmap)
495 rmap_destroy(sh->rmap);
496}
497
Chia-I Wu98824592014-09-02 09:42:46 +0800498void pipeline_tear_shaders(struct intel_pipeline *pipeline)
499{
500 if (pipeline->active_shaders & SHADER_VERTEX_FLAG) {
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800501 pipeline_tear_shader(&pipeline->vs);
Chia-I Wu98824592014-09-02 09:42:46 +0800502 }
503
Chia-I Wu39026c92014-09-02 10:03:19 +0800504 if (pipeline->active_shaders & SHADER_TESS_CONTROL_FLAG) {
505 pipeline_tear_shader(&pipeline->tess_control);
506 }
507
508 if (pipeline->active_shaders & SHADER_TESS_EVAL_FLAG) {
509 pipeline_tear_shader(&pipeline->tess_eval);
510 }
511
512 if (pipeline->active_shaders & SHADER_GEOMETRY_FLAG) {
513 pipeline_tear_shader(&pipeline->gs);
514 }
515
516 if (pipeline->active_shaders & SHADER_FRAGMENT_FLAG) {
Chia-I Wuc3ddee62014-09-02 10:53:20 +0800517 pipeline_tear_shader(&pipeline->fs);
Chia-I Wu39026c92014-09-02 10:03:19 +0800518 }
519
520 if (pipeline->active_shaders & SHADER_COMPUTE_FLAG) {
521 pipeline_tear_shader(&pipeline->compute);
522 }
Chia-I Wu98824592014-09-02 09:42:46 +0800523}