freedreno/ir3: refactor NIR IR handling
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3_shader.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
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 (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include "pipe/p_state.h"
30 #include "util/u_string.h"
31 #include "util/u_memory.h"
32 #include "util/u_inlines.h"
33 #include "util/u_format.h"
34 #include "tgsi/tgsi_dump.h"
35 #include "tgsi/tgsi_parse.h"
36
37 #include "freedreno_context.h"
38 #include "freedreno_util.h"
39
40 #include "ir3_shader.h"
41 #include "ir3_compiler.h"
42 #include "ir3_nir.h"
43
44 static void
45 delete_variant(struct ir3_shader_variant *v)
46 {
47 if (v->ir)
48 ir3_destroy(v->ir);
49 if (v->bo)
50 fd_bo_del(v->bo);
51 free(v);
52 }
53
54 /* for vertex shader, the inputs are loaded into registers before the shader
55 * is executed, so max_regs from the shader instructions might not properly
56 * reflect the # of registers actually used, especially in case passthrough
57 * varyings.
58 *
59 * Likewise, for fragment shader, we can have some regs which are passed
60 * input values but never touched by the resulting shader (ie. as result
61 * of dead code elimination or simply because we don't know how to turn
62 * the reg off.
63 */
64 static void
65 fixup_regfootprint(struct ir3_shader_variant *v)
66 {
67 if (v->type == SHADER_VERTEX) {
68 unsigned i;
69 for (i = 0; i < v->inputs_count; i++) {
70 /* skip frag inputs fetch via bary.f since their reg's are
71 * not written by gpu before shader starts (and in fact the
72 * regid's might not even be valid)
73 */
74 if (v->inputs[i].bary)
75 continue;
76
77 if (v->inputs[i].compmask) {
78 int32_t regid = (v->inputs[i].regid + 3) >> 2;
79 v->info.max_reg = MAX2(v->info.max_reg, regid);
80 }
81 }
82 for (i = 0; i < v->outputs_count; i++) {
83 int32_t regid = (v->outputs[i].regid + 3) >> 2;
84 v->info.max_reg = MAX2(v->info.max_reg, regid);
85 }
86 } else if (v->type == SHADER_FRAGMENT) {
87 /* NOTE: not sure how to turn pos_regid off.. but this could
88 * be, for example, r1.x while max reg used by the shader is
89 * r0.*, in which case we need to fixup the reg footprint:
90 */
91 v->info.max_reg = MAX2(v->info.max_reg, v->pos_regid >> 2);
92 if (v->frag_coord)
93 debug_assert(v->info.max_reg >= 0); /* hard coded r0.x */
94 if (v->frag_face)
95 debug_assert(v->info.max_half_reg >= 0); /* hr0.x */
96 }
97 }
98
99 /* wrapper for ir3_assemble() which does some info fixup based on
100 * shader state. Non-static since used by ir3_cmdline too.
101 */
102 void * ir3_shader_assemble(struct ir3_shader_variant *v, uint32_t gpu_id)
103 {
104 void *bin;
105
106 bin = ir3_assemble(v->ir, &v->info, gpu_id);
107 if (!bin)
108 return NULL;
109
110 if (gpu_id >= 400) {
111 v->instrlen = v->info.sizedwords / (2 * 16);
112 } else {
113 v->instrlen = v->info.sizedwords / (2 * 4);
114 }
115
116 /* NOTE: if relative addressing is used, we set constlen in
117 * the compiler (to worst-case value) since we don't know in
118 * the assembler what the max addr reg value can be:
119 */
120 v->constlen = MIN2(255, MAX2(v->constlen, v->info.max_const + 1));
121
122 fixup_regfootprint(v);
123
124 return bin;
125 }
126
127 static void
128 assemble_variant(struct ir3_shader_variant *v)
129 {
130 struct fd_context *ctx = fd_context(v->shader->pctx);
131 uint32_t gpu_id = v->shader->compiler->gpu_id;
132 uint32_t sz, *bin;
133
134 bin = ir3_shader_assemble(v, gpu_id);
135 sz = v->info.sizedwords * 4;
136
137 v->bo = fd_bo_new(ctx->dev, sz,
138 DRM_FREEDRENO_GEM_CACHE_WCOMBINE |
139 DRM_FREEDRENO_GEM_TYPE_KMEM);
140
141 memcpy(fd_bo_map(v->bo), bin, sz);
142
143 if (fd_mesa_debug & FD_DBG_DISASM) {
144 struct ir3_shader_key key = v->key;
145 DBG("disassemble: type=%d, k={bp=%u,cts=%u,hp=%u}", v->type,
146 key.binning_pass, key.color_two_side, key.half_precision);
147 ir3_shader_disasm(v, bin);
148 }
149
150 if (fd_mesa_debug & FD_DBG_SHADERDB) {
151 /* print generic shader info: */
152 fprintf(stderr, "SHADER-DB: %s prog %d/%d: %u instructions, %u dwords\n",
153 ir3_shader_stage(v->shader),
154 v->shader->id, v->id,
155 v->info.instrs_count,
156 v->info.sizedwords);
157 fprintf(stderr, "SHADER-DB: %s prog %d/%d: %u half, %u full\n",
158 ir3_shader_stage(v->shader),
159 v->shader->id, v->id,
160 v->info.max_half_reg + 1,
161 v->info.max_reg + 1);
162 fprintf(stderr, "SHADER-DB: %s prog %d/%d: %u const, %u constlen\n",
163 ir3_shader_stage(v->shader),
164 v->shader->id, v->id,
165 v->info.max_const + 1,
166 v->constlen);
167 }
168
169 free(bin);
170
171 /* no need to keep the ir around beyond this point: */
172 ir3_destroy(v->ir);
173 v->ir = NULL;
174 }
175
176 static struct ir3_shader_variant *
177 create_variant(struct ir3_shader *shader, struct ir3_shader_key key)
178 {
179 struct ir3_shader_variant *v = CALLOC_STRUCT(ir3_shader_variant);
180 int ret;
181
182 if (!v)
183 return NULL;
184
185 v->id = ++shader->variant_count;
186 v->shader = shader;
187 v->key = key;
188 v->type = shader->type;
189
190 ret = ir3_compile_shader_nir(shader->compiler, v);
191 if (ret) {
192 debug_error("compile failed!");
193 goto fail;
194 }
195
196 assemble_variant(v);
197 if (!v->bo) {
198 debug_error("assemble failed!");
199 goto fail;
200 }
201
202 return v;
203
204 fail:
205 delete_variant(v);
206 return NULL;
207 }
208
209 struct ir3_shader_variant *
210 ir3_shader_variant(struct ir3_shader *shader, struct ir3_shader_key key)
211 {
212 struct ir3_shader_variant *v;
213
214 /* some shader key values only apply to vertex or frag shader,
215 * so normalize the key to avoid constructing multiple identical
216 * variants:
217 */
218 switch (shader->type) {
219 case SHADER_FRAGMENT:
220 case SHADER_COMPUTE:
221 key.binning_pass = false;
222 if (key.has_per_samp) {
223 key.vsaturate_s = 0;
224 key.vsaturate_t = 0;
225 key.vsaturate_r = 0;
226 }
227 break;
228 case SHADER_VERTEX:
229 key.color_two_side = false;
230 key.half_precision = false;
231 key.rasterflat = false;
232 if (key.has_per_samp) {
233 key.fsaturate_s = 0;
234 key.fsaturate_t = 0;
235 key.fsaturate_r = 0;
236 }
237 break;
238 }
239
240 for (v = shader->variants; v; v = v->next)
241 if (ir3_shader_key_equal(&key, &v->key))
242 return v;
243
244 /* compile new variant if it doesn't exist already: */
245 v = create_variant(shader, key);
246 if (v) {
247 v->next = shader->variants;
248 shader->variants = v;
249 }
250
251 return v;
252 }
253
254
255 void
256 ir3_shader_destroy(struct ir3_shader *shader)
257 {
258 struct ir3_shader_variant *v, *t;
259 for (v = shader->variants; v; ) {
260 t = v;
261 v = v->next;
262 delete_variant(t);
263 }
264 ralloc_free(shader->nir);
265 free(shader);
266 }
267
268 struct ir3_shader *
269 ir3_shader_create(struct pipe_context *pctx,
270 const struct pipe_shader_state *cso,
271 enum shader_t type)
272 {
273 struct ir3_shader *shader = CALLOC_STRUCT(ir3_shader);
274 shader->compiler = fd_context(pctx)->screen->compiler;
275 shader->id = ++shader->compiler->shader_count;
276 shader->pctx = pctx;
277 shader->type = type;
278 if (fd_mesa_debug & FD_DBG_DISASM) {
279 DBG("dump tgsi: type=%d", shader->type);
280 tgsi_dump(cso->tokens, 0);
281 }
282 nir_shader *nir = ir3_tgsi_to_nir(cso->tokens);
283 /* do first pass optimization, ignoring the key: */
284 shader->nir = ir3_optimize_nir(shader, nir, NULL);
285 if (fd_mesa_debug & FD_DBG_DISASM) {
286 DBG("dump nir%d: type=%d", shader->id, shader->type);
287 nir_print_shader(shader->nir, stdout);
288 }
289 shader->stream_output = cso->stream_output;
290 if (fd_mesa_debug & FD_DBG_SHADERDB) {
291 /* if shader-db run, create a standard variant immediately
292 * (as otherwise nothing will trigger the shader to be
293 * actually compiled)
294 */
295 static struct ir3_shader_key key = {0};
296 ir3_shader_variant(shader, key);
297 }
298 return shader;
299 }
300
301 static void dump_reg(const char *name, uint32_t r)
302 {
303 if (r != regid(63,0))
304 debug_printf("; %s: r%d.%c\n", name, r >> 2, "xyzw"[r & 0x3]);
305 }
306
307 static void dump_output(struct ir3_shader_variant *so,
308 unsigned slot, const char *name)
309 {
310 uint32_t regid;
311 regid = ir3_find_output_regid(so, slot);
312 dump_reg(name, regid);
313 }
314
315 void
316 ir3_shader_disasm(struct ir3_shader_variant *so, uint32_t *bin)
317 {
318 struct ir3 *ir = so->ir;
319 struct ir3_register *reg;
320 const char *type = ir3_shader_stage(so->shader);
321 uint8_t regid;
322 unsigned i;
323
324 for (i = 0; i < ir->ninputs; i++) {
325 if (!ir->inputs[i]) {
326 debug_printf("; in%d unused\n", i);
327 continue;
328 }
329 reg = ir->inputs[i]->regs[0];
330 regid = reg->num;
331 debug_printf("@in(%sr%d.%c)\tin%d\n",
332 (reg->flags & IR3_REG_HALF) ? "h" : "",
333 (regid >> 2), "xyzw"[regid & 0x3], i);
334 }
335
336 for (i = 0; i < ir->noutputs; i++) {
337 if (!ir->outputs[i]) {
338 debug_printf("; out%d unused\n", i);
339 continue;
340 }
341 /* kill shows up as a virtual output.. skip it! */
342 if (is_kill(ir->outputs[i]))
343 continue;
344 reg = ir->outputs[i]->regs[0];
345 regid = reg->num;
346 debug_printf("@out(%sr%d.%c)\tout%d\n",
347 (reg->flags & IR3_REG_HALF) ? "h" : "",
348 (regid >> 2), "xyzw"[regid & 0x3], i);
349 }
350
351 for (i = 0; i < so->immediates_count; i++) {
352 debug_printf("@const(c%d.x)\t", so->first_immediate + i);
353 debug_printf("0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
354 so->immediates[i].val[0],
355 so->immediates[i].val[1],
356 so->immediates[i].val[2],
357 so->immediates[i].val[3]);
358 }
359
360 disasm_a3xx(bin, so->info.sizedwords, 0, so->type);
361
362 switch (so->type) {
363 case SHADER_VERTEX:
364 debug_printf("; %s: outputs:", type);
365 for (i = 0; i < so->outputs_count; i++) {
366 uint8_t regid = so->outputs[i].regid;
367 debug_printf(" r%d.%c (%s)",
368 (regid >> 2), "xyzw"[regid & 0x3],
369 gl_varying_slot_name(so->outputs[i].slot));
370 }
371 debug_printf("\n");
372 debug_printf("; %s: inputs:", type);
373 for (i = 0; i < so->inputs_count; i++) {
374 uint8_t regid = so->inputs[i].regid;
375 debug_printf(" r%d.%c (cm=%x,il=%u,b=%u)",
376 (regid >> 2), "xyzw"[regid & 0x3],
377 so->inputs[i].compmask,
378 so->inputs[i].inloc,
379 so->inputs[i].bary);
380 }
381 debug_printf("\n");
382 break;
383 case SHADER_FRAGMENT:
384 debug_printf("; %s: outputs:", type);
385 for (i = 0; i < so->outputs_count; i++) {
386 uint8_t regid = so->outputs[i].regid;
387 debug_printf(" r%d.%c (%s)",
388 (regid >> 2), "xyzw"[regid & 0x3],
389 gl_frag_result_name(so->outputs[i].slot));
390 }
391 debug_printf("\n");
392 debug_printf("; %s: inputs:", type);
393 for (i = 0; i < so->inputs_count; i++) {
394 uint8_t regid = so->inputs[i].regid;
395 debug_printf(" r%d.%c (%s,cm=%x,il=%u,b=%u)",
396 (regid >> 2), "xyzw"[regid & 0x3],
397 gl_varying_slot_name(so->inputs[i].slot),
398 so->inputs[i].compmask,
399 so->inputs[i].inloc,
400 so->inputs[i].bary);
401 }
402 debug_printf("\n");
403 break;
404 case SHADER_COMPUTE:
405 break;
406 }
407
408 /* print generic shader info: */
409 debug_printf("; %s prog %d/%d: %u instructions, %d half, %d full\n",
410 type, so->shader->id, so->id,
411 so->info.instrs_count,
412 so->info.max_half_reg + 1,
413 so->info.max_reg + 1);
414
415 debug_printf("; %d const, %u constlen\n",
416 so->info.max_const + 1,
417 so->constlen);
418
419 /* print shader type specific info: */
420 switch (so->type) {
421 case SHADER_VERTEX:
422 dump_output(so, VARYING_SLOT_POS, "pos");
423 dump_output(so, VARYING_SLOT_PSIZ, "psize");
424 break;
425 case SHADER_FRAGMENT:
426 dump_reg("pos (bary)", so->pos_regid);
427 dump_output(so, FRAG_RESULT_DEPTH, "posz");
428 if (so->color0_mrt) {
429 dump_output(so, FRAG_RESULT_COLOR, "color");
430 } else {
431 dump_output(so, FRAG_RESULT_DATA0, "data0");
432 dump_output(so, FRAG_RESULT_DATA1, "data1");
433 dump_output(so, FRAG_RESULT_DATA2, "data2");
434 dump_output(so, FRAG_RESULT_DATA3, "data3");
435 dump_output(so, FRAG_RESULT_DATA4, "data4");
436 dump_output(so, FRAG_RESULT_DATA5, "data5");
437 dump_output(so, FRAG_RESULT_DATA6, "data6");
438 dump_output(so, FRAG_RESULT_DATA7, "data7");
439 }
440 /* these two are hard-coded since we don't know how to
441 * program them to anything but all 0's...
442 */
443 if (so->frag_coord)
444 debug_printf("; fragcoord: r0.x\n");
445 if (so->frag_face)
446 debug_printf("; fragface: hr0.x\n");
447 break;
448 case SHADER_COMPUTE:
449 break;
450 }
451
452 debug_printf("\n");
453 }
454
455 /* This has to reach into the fd_context a bit more than the rest of
456 * ir3, but it needs to be aligned with the compiler, so both agree
457 * on which const regs hold what. And the logic is identical between
458 * a3xx/a4xx, the only difference is small details in the actual
459 * CP_LOAD_STATE packets (which is handled inside the generation
460 * specific ctx->emit_const(_bo)() fxns)
461 */
462
463 #include "freedreno_resource.h"
464
465 static void
466 emit_user_consts(struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
467 struct fd_constbuf_stateobj *constbuf)
468 {
469 struct fd_context *ctx = fd_context(v->shader->pctx);
470 const unsigned index = 0; /* user consts are index 0 */
471 /* TODO save/restore dirty_mask for binning pass instead: */
472 uint32_t dirty_mask = constbuf->enabled_mask;
473
474 if (dirty_mask & (1 << index)) {
475 struct pipe_constant_buffer *cb = &constbuf->cb[index];
476 unsigned size = align(cb->buffer_size, 4) / 4; /* size in dwords */
477
478 /* in particular, with binning shader we may end up with
479 * unused consts, ie. we could end up w/ constlen that is
480 * smaller than first_driver_param. In that case truncate
481 * the user consts early to avoid HLSQ lockup caused by
482 * writing too many consts
483 */
484 uint32_t max_const = MIN2(v->first_driver_param, v->constlen);
485
486 // I expect that size should be a multiple of vec4's:
487 assert(size == align(size, 4));
488
489 /* and even if the start of the const buffer is before
490 * first_immediate, the end may not be:
491 */
492 size = MIN2(size, 4 * max_const);
493
494 if (size > 0) {
495 fd_wfi(ctx, ring);
496 ctx->emit_const(ring, v->type, 0,
497 cb->buffer_offset, size,
498 cb->user_buffer, cb->buffer);
499 constbuf->dirty_mask &= ~(1 << index);
500 }
501 }
502 }
503
504 static void
505 emit_ubos(struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
506 struct fd_constbuf_stateobj *constbuf)
507 {
508 uint32_t offset = v->first_driver_param + IR3_UBOS_OFF;
509 if (v->constlen > offset) {
510 struct fd_context *ctx = fd_context(v->shader->pctx);
511 uint32_t params = MIN2(4, v->constlen - offset) * 4;
512 uint32_t offsets[params];
513 struct fd_bo *bos[params];
514
515 for (uint32_t i = 0; i < params; i++) {
516 const uint32_t index = i + 1; /* UBOs start at index 1 */
517 struct pipe_constant_buffer *cb = &constbuf->cb[index];
518 assert(!cb->user_buffer);
519
520 if ((constbuf->enabled_mask & (1 << index)) && cb->buffer) {
521 offsets[i] = cb->buffer_offset;
522 bos[i] = fd_resource(cb->buffer)->bo;
523 } else {
524 offsets[i] = 0;
525 bos[i] = NULL;
526 }
527 }
528
529 fd_wfi(ctx, ring);
530 ctx->emit_const_bo(ring, v->type, false, offset * 4, params, bos, offsets);
531 }
532 }
533
534 static void
535 emit_immediates(struct ir3_shader_variant *v, struct fd_ringbuffer *ring)
536 {
537 struct fd_context *ctx = fd_context(v->shader->pctx);
538 int size = v->immediates_count;
539 uint32_t base = v->first_immediate;
540
541 /* truncate size to avoid writing constants that shader
542 * does not use:
543 */
544 size = MIN2(size + base, v->constlen) - base;
545
546 /* convert out of vec4: */
547 base *= 4;
548 size *= 4;
549
550 if (size > 0) {
551 fd_wfi(ctx, ring);
552 ctx->emit_const(ring, v->type, base,
553 0, size, v->immediates[0].val, NULL);
554 }
555 }
556
557 /* emit stream-out buffers: */
558 static void
559 emit_tfbos(struct ir3_shader_variant *v, struct fd_ringbuffer *ring)
560 {
561 /* streamout addresses after driver-params: */
562 uint32_t offset = v->first_driver_param + IR3_TFBOS_OFF;
563 if (v->constlen > offset) {
564 struct fd_context *ctx = fd_context(v->shader->pctx);
565 struct fd_streamout_stateobj *so = &ctx->streamout;
566 struct pipe_stream_output_info *info = &v->shader->stream_output;
567 uint32_t params = 4;
568 uint32_t offsets[params];
569 struct fd_bo *bos[params];
570
571 for (uint32_t i = 0; i < params; i++) {
572 struct pipe_stream_output_target *target = so->targets[i];
573
574 if (target) {
575 offsets[i] = (so->offsets[i] * info->stride[i] * 4) +
576 target->buffer_offset;
577 bos[i] = fd_resource(target->buffer)->bo;
578 } else {
579 offsets[i] = 0;
580 bos[i] = NULL;
581 }
582 }
583
584 fd_wfi(ctx, ring);
585 ctx->emit_const_bo(ring, v->type, true, offset * 4, params, bos, offsets);
586 }
587 }
588
589 static uint32_t
590 max_tf_vtx(struct ir3_shader_variant *v)
591 {
592 struct fd_context *ctx = fd_context(v->shader->pctx);
593 struct fd_streamout_stateobj *so = &ctx->streamout;
594 struct pipe_stream_output_info *info = &v->shader->stream_output;
595 uint32_t maxvtxcnt = 0x7fffffff;
596
597 if (v->key.binning_pass)
598 return 0;
599 if (v->shader->stream_output.num_outputs == 0)
600 return 0;
601 if (so->num_targets == 0)
602 return 0;
603
604 /* offset to write to is:
605 *
606 * total_vtxcnt = vtxcnt + offsets[i]
607 * offset = total_vtxcnt * stride[i]
608 *
609 * offset = vtxcnt * stride[i] ; calculated in shader
610 * + offsets[i] * stride[i] ; calculated at emit_tfbos()
611 *
612 * assuming for each vtx, each target buffer will have data written
613 * up to 'offset + stride[i]', that leaves maxvtxcnt as:
614 *
615 * buffer_size = (maxvtxcnt * stride[i]) + stride[i]
616 * maxvtxcnt = (buffer_size - stride[i]) / stride[i]
617 *
618 * but shader is actually doing a less-than (rather than less-than-
619 * equal) check, so we can drop the -stride[i].
620 *
621 * TODO is assumption about `offset + stride[i]` legit?
622 */
623 for (unsigned i = 0; i < so->num_targets; i++) {
624 struct pipe_stream_output_target *target = so->targets[i];
625 unsigned stride = info->stride[i] * 4; /* convert dwords->bytes */
626 if (target) {
627 uint32_t max = target->buffer_size / stride;
628 maxvtxcnt = MIN2(maxvtxcnt, max);
629 }
630 }
631
632 return maxvtxcnt;
633 }
634
635 void
636 ir3_emit_consts(struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
637 const struct pipe_draw_info *info, uint32_t dirty)
638 {
639 struct fd_context *ctx = fd_context(v->shader->pctx);
640
641 if (dirty & (FD_DIRTY_PROG | FD_DIRTY_CONSTBUF)) {
642 struct fd_constbuf_stateobj *constbuf;
643 bool shader_dirty;
644
645 if (v->type == SHADER_VERTEX) {
646 constbuf = &ctx->constbuf[PIPE_SHADER_VERTEX];
647 shader_dirty = !!(ctx->prog.dirty & FD_SHADER_DIRTY_VP);
648 } else if (v->type == SHADER_FRAGMENT) {
649 constbuf = &ctx->constbuf[PIPE_SHADER_FRAGMENT];
650 shader_dirty = !!(ctx->prog.dirty & FD_SHADER_DIRTY_FP);
651 } else {
652 unreachable("bad shader type");
653 return;
654 }
655
656 emit_user_consts(v, ring, constbuf);
657 emit_ubos(v, ring, constbuf);
658 if (shader_dirty)
659 emit_immediates(v, ring);
660 }
661
662 /* emit driver params every time: */
663 /* TODO skip emit if shader doesn't use driver params to avoid WFI.. */
664 if (info && (v->type == SHADER_VERTEX)) {
665 uint32_t offset = v->first_driver_param + IR3_DRIVER_PARAM_OFF;
666 if (v->constlen >= offset) {
667 uint32_t vertex_params[IR3_DP_COUNT] = {
668 [IR3_DP_VTXID_BASE] = info->indexed ?
669 info->index_bias : info->start,
670 [IR3_DP_VTXCNT_MAX] = max_tf_vtx(v),
671 };
672 /* if no user-clip-planes, we don't need to emit the
673 * entire thing:
674 */
675 uint32_t vertex_params_size = 4;
676
677 if (v->key.ucp_enables) {
678 struct pipe_clip_state *ucp = &ctx->ucp;
679 unsigned pos = IR3_DP_UCP0_X;
680 for (unsigned i = 0; pos <= IR3_DP_UCP7_W; i++) {
681 for (unsigned j = 0; j < 4; j++) {
682 vertex_params[pos] = fui(ucp->ucp[i][j]);
683 pos++;
684 }
685 }
686 vertex_params_size = ARRAY_SIZE(vertex_params);
687 }
688
689 fd_wfi(ctx, ring);
690 ctx->emit_const(ring, SHADER_VERTEX, offset * 4, 0,
691 vertex_params_size, vertex_params, NULL);
692
693 /* if needed, emit stream-out buffer addresses: */
694 if (vertex_params[IR3_DP_VTXCNT_MAX] > 0) {
695 emit_tfbos(v, ring);
696 }
697 }
698 }
699 }