freedreno/ir3: also precompile compute shaders for shaderdb
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3_gallium.c
1 /*
2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "pipe/p_state.h"
28 #include "pipe/p_screen.h"
29 #include "util/u_string.h"
30 #include "util/u_memory.h"
31 #include "util/u_inlines.h"
32 #include "util/format/u_format.h"
33 #include "tgsi/tgsi_dump.h"
34 #include "tgsi/tgsi_parse.h"
35
36 #include "nir/tgsi_to_nir.h"
37
38 #include "freedreno_context.h"
39 #include "freedreno_util.h"
40
41 #include "ir3/ir3_shader.h"
42 #include "ir3/ir3_gallium.h"
43 #include "ir3/ir3_compiler.h"
44 #include "ir3/ir3_nir.h"
45
46 static void
47 dump_shader_info(struct ir3_shader_variant *v, bool binning_pass,
48 struct pipe_debug_callback *debug)
49 {
50 if (!unlikely(fd_mesa_debug & FD_DBG_SHADERDB))
51 return;
52
53 pipe_debug_message(debug, SHADER_INFO,
54 "%s shader: %u inst, %u nops, %u non-nops, %u dwords, "
55 "%u last-baryf, %u half, %u full, %u constlen, "
56 "%u sstall, %u (ss), %u (sy), %d max_sun, %d loops\n",
57 ir3_shader_stage(v),
58 v->info.instrs_count,
59 v->info.nops_count,
60 v->info.instrs_count - v->info.nops_count,
61 v->info.sizedwords,
62 v->info.last_baryf,
63 v->info.max_half_reg + 1,
64 v->info.max_reg + 1,
65 v->constlen,
66 v->info.sstall,
67 v->info.ss, v->info.sy,
68 v->max_sun, v->loops);
69 }
70
71 struct ir3_shader_variant *
72 ir3_shader_variant(struct ir3_shader *shader, struct ir3_shader_key key,
73 bool binning_pass, struct pipe_debug_callback *debug)
74 {
75 struct ir3_shader_variant *v;
76 bool created = false;
77
78 /* some shader key values only apply to vertex or frag shader,
79 * so normalize the key to avoid constructing multiple identical
80 * variants:
81 */
82 ir3_normalize_key(&key, shader->type);
83
84 v = ir3_shader_get_variant(shader, &key, binning_pass, &created);
85
86 if (created) {
87 dump_shader_info(v, binning_pass, debug);
88 }
89
90 return v;
91 }
92
93 static void
94 copy_stream_out(struct ir3_stream_output_info *i,
95 const struct pipe_stream_output_info *p)
96 {
97 STATIC_ASSERT(ARRAY_SIZE(i->stride) == ARRAY_SIZE(p->stride));
98 STATIC_ASSERT(ARRAY_SIZE(i->output) == ARRAY_SIZE(p->output));
99
100 i->num_outputs = p->num_outputs;
101 for (int n = 0; n < ARRAY_SIZE(i->stride); n++)
102 i->stride[n] = p->stride[n];
103
104 for (int n = 0; n < ARRAY_SIZE(i->output); n++) {
105 i->output[n].register_index = p->output[n].register_index;
106 i->output[n].start_component = p->output[n].start_component;
107 i->output[n].num_components = p->output[n].num_components;
108 i->output[n].output_buffer = p->output[n].output_buffer;
109 i->output[n].dst_offset = p->output[n].dst_offset;
110 i->output[n].stream = p->output[n].stream;
111 }
112 }
113
114 struct ir3_shader *
115 ir3_shader_create(struct ir3_compiler *compiler,
116 const struct pipe_shader_state *cso,
117 struct pipe_debug_callback *debug,
118 struct pipe_screen *screen)
119 {
120 nir_shader *nir;
121 if (cso->type == PIPE_SHADER_IR_NIR) {
122 /* we take ownership of the reference: */
123 nir = cso->ir.nir;
124 } else {
125 debug_assert(cso->type == PIPE_SHADER_IR_TGSI);
126 if (ir3_shader_debug & IR3_DBG_DISASM) {
127 tgsi_dump(cso->tokens, 0);
128 }
129 nir = tgsi_to_nir(cso->tokens, screen);
130 }
131
132 struct ir3_shader *shader = ir3_shader_from_nir(compiler, nir);
133
134 copy_stream_out(&shader->stream_output, &cso->stream_output);
135
136 if (fd_mesa_debug & FD_DBG_SHADERDB) {
137 /* if shader-db run, create a standard variant immediately
138 * (as otherwise nothing will trigger the shader to be
139 * actually compiled)
140 */
141 static struct ir3_shader_key key; /* static is implicitly zeroed */
142 ir3_shader_variant(shader, key, false, debug);
143
144 if (nir->info.stage != MESA_SHADER_FRAGMENT)
145 ir3_shader_variant(shader, key, true, debug);
146 }
147
148 return shader;
149 }
150
151 /* a bit annoying that compute-shader and normal shader state objects
152 * aren't a bit more aligned.
153 */
154 struct ir3_shader *
155 ir3_shader_create_compute(struct ir3_compiler *compiler,
156 const struct pipe_compute_state *cso,
157 struct pipe_debug_callback *debug,
158 struct pipe_screen *screen)
159 {
160 nir_shader *nir;
161 if (cso->ir_type == PIPE_SHADER_IR_NIR) {
162 /* we take ownership of the reference: */
163 nir = (nir_shader *)cso->prog;
164 } else {
165 debug_assert(cso->ir_type == PIPE_SHADER_IR_TGSI);
166 if (ir3_shader_debug & IR3_DBG_DISASM) {
167 tgsi_dump(cso->prog, 0);
168 }
169 nir = tgsi_to_nir(cso->prog, screen);
170 }
171
172 struct ir3_shader *shader = ir3_shader_from_nir(compiler, nir);
173
174 if (fd_mesa_debug & FD_DBG_SHADERDB) {
175 /* if shader-db run, create a standard variant immediately
176 * (as otherwise nothing will trigger the shader to be
177 * actually compiled)
178 */
179 static struct ir3_shader_key key; /* static is implicitly zeroed */
180 ir3_shader_variant(shader, key, false, debug);
181 }
182
183 return shader;
184 }
185
186 /* This has to reach into the fd_context a bit more than the rest of
187 * ir3, but it needs to be aligned with the compiler, so both agree
188 * on which const regs hold what. And the logic is identical between
189 * a3xx/a4xx, the only difference is small details in the actual
190 * CP_LOAD_STATE packets (which is handled inside the generation
191 * specific ctx->emit_const(_bo)() fxns)
192 */
193
194 #include "freedreno_resource.h"
195
196 static inline bool
197 is_stateobj(struct fd_ringbuffer *ring)
198 {
199 /* XXX this is an ugly way to differentiate.. */
200 return !!(ring->flags & FD_RINGBUFFER_STREAMING);
201 }
202
203 static inline void
204 ring_wfi(struct fd_batch *batch, struct fd_ringbuffer *ring)
205 {
206 /* when we emit const state via ring (IB2) we need a WFI, but when
207 * it is emit'd via stateobj, we don't
208 */
209 if (is_stateobj(ring))
210 return;
211
212 fd_wfi(batch, ring);
213 }
214
215 static void
216 emit_const(struct fd_screen *screen, struct fd_ringbuffer *ring,
217 const struct ir3_shader_variant *v, uint32_t dst_offset,
218 uint32_t offset, uint32_t size,
219 const void *user_buffer, struct pipe_resource *buffer)
220 {
221 assert(dst_offset + size <= v->constlen * 4);
222
223 screen->emit_const(ring, v->type, dst_offset,
224 offset, size, user_buffer, buffer);
225 }
226
227 /**
228 * Indirectly calculates size of cmdstream needed for ir3_emit_user_consts().
229 * Returns number of packets, and total size of all the payload.
230 *
231 * The value can be a worst-case, ie. some shader variants may not read all
232 * consts, etc.
233 *
234 * Returns size in dwords.
235 */
236 void
237 ir3_user_consts_size(struct ir3_ubo_analysis_state *state,
238 unsigned *packets, unsigned *size)
239 {
240 *packets = *size = 0;
241
242 for (uint32_t i = 0; i < ARRAY_SIZE(state->range); i++) {
243 if (state->range[i].start < state->range[i].end) {
244 *size += state->range[i].end - state->range[i].start;
245 (*packets)++;
246 }
247 }
248 }
249
250 /**
251 * Uploads sub-ranges of UBOs to the hardware's constant buffer (UBO access
252 * outside of these ranges will be done using full UBO accesses in the
253 * shader).
254 */
255 void
256 ir3_emit_user_consts(struct fd_screen *screen, const struct ir3_shader_variant *v,
257 struct fd_ringbuffer *ring, struct fd_constbuf_stateobj *constbuf)
258 {
259 struct ir3_ubo_analysis_state *state;
260 state = &v->shader->ubo_state;
261
262 uint32_t i;
263 foreach_bit(i, state->enabled & constbuf->enabled_mask) {
264 struct pipe_constant_buffer *cb = &constbuf->cb[i];
265
266 uint32_t size = state->range[i].end - state->range[i].start;
267 uint32_t offset = cb->buffer_offset + state->range[i].start;
268
269 /* and even if the start of the const buffer is before
270 * first_immediate, the end may not be:
271 */
272 size = MIN2(size, (16 * v->constlen) - state->range[i].offset);
273
274 if (size == 0)
275 continue;
276
277 /* things should be aligned to vec4: */
278 debug_assert((state->range[i].offset % 16) == 0);
279 debug_assert((size % 16) == 0);
280 debug_assert((offset % 16) == 0);
281
282 emit_const(screen, ring, v, state->range[i].offset / 4,
283 offset, size / 4, cb->user_buffer, cb->buffer);
284 }
285 }
286
287 void
288 ir3_emit_ubos(struct fd_screen *screen, const struct ir3_shader_variant *v,
289 struct fd_ringbuffer *ring, struct fd_constbuf_stateobj *constbuf)
290 {
291 const struct ir3_const_state *const_state = &v->shader->const_state;
292 uint32_t offset = const_state->offsets.ubo;
293 if (v->constlen > offset) {
294 uint32_t params = const_state->num_ubos;
295 uint32_t offsets[params];
296 struct pipe_resource *prscs[params];
297
298 for (uint32_t i = 0; i < params; i++) {
299 const uint32_t index = i + 1; /* UBOs start at index 1 */
300 struct pipe_constant_buffer *cb = &constbuf->cb[index];
301 assert(!cb->user_buffer);
302
303 if ((constbuf->enabled_mask & (1 << index)) && cb->buffer) {
304 offsets[i] = cb->buffer_offset;
305 prscs[i] = cb->buffer;
306 } else {
307 offsets[i] = 0;
308 prscs[i] = NULL;
309 }
310 }
311
312 assert(offset * 4 + params < v->constlen * 4);
313
314 screen->emit_const_bo(ring, v->type, false, offset * 4, params, prscs, offsets);
315 }
316 }
317
318 void
319 ir3_emit_ssbo_sizes(struct fd_screen *screen, const struct ir3_shader_variant *v,
320 struct fd_ringbuffer *ring, struct fd_shaderbuf_stateobj *sb)
321 {
322 const struct ir3_const_state *const_state = &v->shader->const_state;
323 uint32_t offset = const_state->offsets.ssbo_sizes;
324 if (v->constlen > offset) {
325 uint32_t sizes[align(const_state->ssbo_size.count, 4)];
326 unsigned mask = const_state->ssbo_size.mask;
327
328 while (mask) {
329 unsigned index = u_bit_scan(&mask);
330 unsigned off = const_state->ssbo_size.off[index];
331 sizes[off] = sb->sb[index].buffer_size;
332 }
333
334 emit_const(screen, ring, v, offset * 4,
335 0, ARRAY_SIZE(sizes), sizes, NULL);
336 }
337 }
338
339 void
340 ir3_emit_image_dims(struct fd_screen *screen, const struct ir3_shader_variant *v,
341 struct fd_ringbuffer *ring, struct fd_shaderimg_stateobj *si)
342 {
343 const struct ir3_const_state *const_state = &v->shader->const_state;
344 uint32_t offset = const_state->offsets.image_dims;
345 if (v->constlen > offset) {
346 uint32_t dims[align(const_state->image_dims.count, 4)];
347 unsigned mask = const_state->image_dims.mask;
348
349 while (mask) {
350 struct pipe_image_view *img;
351 struct fd_resource *rsc;
352 unsigned index = u_bit_scan(&mask);
353 unsigned off = const_state->image_dims.off[index];
354
355 img = &si->si[index];
356 rsc = fd_resource(img->resource);
357
358 dims[off + 0] = util_format_get_blocksize(img->format);
359 if (img->resource->target != PIPE_BUFFER) {
360 struct fdl_slice *slice =
361 fd_resource_slice(rsc, img->u.tex.level);
362 /* note for 2d/cube/etc images, even if re-interpreted
363 * as a different color format, the pixel size should
364 * be the same, so use original dimensions for y and z
365 * stride:
366 */
367 dims[off + 1] = slice->pitch * rsc->layout.cpp;
368 /* see corresponding logic in fd_resource_offset(): */
369 if (rsc->layout.layer_first) {
370 dims[off + 2] = rsc->layout.layer_size;
371 } else {
372 dims[off + 2] = slice->size0;
373 }
374 } else {
375 /* For buffer-backed images, the log2 of the format's
376 * bytes-per-pixel is placed on the 2nd slot. This is useful
377 * when emitting image_size instructions, for which we need
378 * to divide by bpp for image buffers. Since the bpp
379 * can only be power-of-two, the division is implemented
380 * as a SHR, and for that it is handy to have the log2 of
381 * bpp as a constant. (log2 = first-set-bit - 1)
382 */
383 dims[off + 1] = ffs(dims[off + 0]) - 1;
384 }
385 }
386 uint32_t size = MIN2(ARRAY_SIZE(dims), v->constlen * 4 - offset * 4);
387
388 emit_const(screen, ring, v, offset * 4, 0, size, dims, NULL);
389 }
390 }
391
392 void
393 ir3_emit_immediates(struct fd_screen *screen, const struct ir3_shader_variant *v,
394 struct fd_ringbuffer *ring)
395 {
396 const struct ir3_const_state *const_state = &v->shader->const_state;
397 uint32_t base = const_state->offsets.immediate;
398 int size = const_state->immediates_count;
399
400 /* truncate size to avoid writing constants that shader
401 * does not use:
402 */
403 size = MIN2(size + base, v->constlen) - base;
404
405 /* convert out of vec4: */
406 base *= 4;
407 size *= 4;
408
409 if (size > 0) {
410 emit_const(screen, ring, v, base,
411 0, size, const_state->immediates[0].val, NULL);
412 }
413 }
414
415 static uint32_t
416 link_geometry_stages(const struct ir3_shader_variant *producer,
417 const struct ir3_shader_variant *consumer,
418 uint32_t *locs)
419 {
420 uint32_t num_loc = 0, factor;
421
422 switch (consumer->type) {
423 case MESA_SHADER_TESS_CTRL:
424 case MESA_SHADER_GEOMETRY:
425 /* These stages load with ldlw, which expects byte offsets. */
426 factor = 4;
427 break;
428 case MESA_SHADER_TESS_EVAL:
429 /* The tess eval shader uses ldg, which takes dword offsets. */
430 factor = 1;
431 break;
432 default:
433 unreachable("bad shader stage");
434 }
435
436 nir_foreach_variable(in_var, &consumer->shader->nir->inputs) {
437 nir_foreach_variable(out_var, &producer->shader->nir->outputs) {
438 if (in_var->data.location == out_var->data.location) {
439 locs[in_var->data.driver_location] =
440 producer->shader->output_loc[out_var->data.driver_location] * factor;
441
442 debug_assert(num_loc <= in_var->data.driver_location + 1);
443 num_loc = in_var->data.driver_location + 1;
444 }
445 }
446 }
447
448 return num_loc;
449 }
450
451 void
452 ir3_emit_link_map(struct fd_screen *screen,
453 const struct ir3_shader_variant *producer,
454 const struct ir3_shader_variant *v, struct fd_ringbuffer *ring)
455 {
456 const struct ir3_const_state *const_state = &v->shader->const_state;
457 uint32_t base = const_state->offsets.primitive_map;
458 uint32_t patch_locs[MAX_VARYING] = { }, num_loc;
459
460 num_loc = link_geometry_stages(producer, v, patch_locs);
461
462 int size = DIV_ROUND_UP(num_loc, 4);
463
464 /* truncate size to avoid writing constants that shader
465 * does not use:
466 */
467 size = MIN2(size + base, v->constlen) - base;
468
469 /* convert out of vec4: */
470 base *= 4;
471 size *= 4;
472
473 if (size > 0)
474 emit_const(screen, ring, v, base, 0, size, patch_locs, NULL);
475 }
476
477 /* emit stream-out buffers: */
478 static void
479 emit_tfbos(struct fd_context *ctx, const struct ir3_shader_variant *v,
480 struct fd_ringbuffer *ring)
481 {
482 /* streamout addresses after driver-params: */
483 const struct ir3_const_state *const_state = &v->shader->const_state;
484 uint32_t offset = const_state->offsets.tfbo;
485 if (v->constlen > offset) {
486 struct fd_streamout_stateobj *so = &ctx->streamout;
487 struct ir3_stream_output_info *info = &v->shader->stream_output;
488 uint32_t params = 4;
489 uint32_t offsets[params];
490 struct pipe_resource *prscs[params];
491
492 for (uint32_t i = 0; i < params; i++) {
493 struct pipe_stream_output_target *target = so->targets[i];
494
495 if (target) {
496 offsets[i] = (so->offsets[i] * info->stride[i] * 4) +
497 target->buffer_offset;
498 prscs[i] = target->buffer;
499 } else {
500 offsets[i] = 0;
501 prscs[i] = NULL;
502 }
503 }
504
505 assert(offset * 4 + params < v->constlen * 4);
506
507 ctx->screen->emit_const_bo(ring, v->type, true, offset * 4, params, prscs, offsets);
508 }
509 }
510
511 static uint32_t
512 max_tf_vtx(struct fd_context *ctx, const struct ir3_shader_variant *v)
513 {
514 struct fd_streamout_stateobj *so = &ctx->streamout;
515 struct ir3_stream_output_info *info = &v->shader->stream_output;
516 uint32_t maxvtxcnt = 0x7fffffff;
517
518 if (ctx->screen->gpu_id >= 500)
519 return 0;
520 if (v->binning_pass)
521 return 0;
522 if (v->shader->stream_output.num_outputs == 0)
523 return 0;
524 if (so->num_targets == 0)
525 return 0;
526
527 /* offset to write to is:
528 *
529 * total_vtxcnt = vtxcnt + offsets[i]
530 * offset = total_vtxcnt * stride[i]
531 *
532 * offset = vtxcnt * stride[i] ; calculated in shader
533 * + offsets[i] * stride[i] ; calculated at emit_tfbos()
534 *
535 * assuming for each vtx, each target buffer will have data written
536 * up to 'offset + stride[i]', that leaves maxvtxcnt as:
537 *
538 * buffer_size = (maxvtxcnt * stride[i]) + stride[i]
539 * maxvtxcnt = (buffer_size - stride[i]) / stride[i]
540 *
541 * but shader is actually doing a less-than (rather than less-than-
542 * equal) check, so we can drop the -stride[i].
543 *
544 * TODO is assumption about `offset + stride[i]` legit?
545 */
546 for (unsigned i = 0; i < so->num_targets; i++) {
547 struct pipe_stream_output_target *target = so->targets[i];
548 unsigned stride = info->stride[i] * 4; /* convert dwords->bytes */
549 if (target) {
550 uint32_t max = target->buffer_size / stride;
551 maxvtxcnt = MIN2(maxvtxcnt, max);
552 }
553 }
554
555 return maxvtxcnt;
556 }
557
558 static void
559 emit_common_consts(const struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
560 struct fd_context *ctx, enum pipe_shader_type t)
561 {
562 enum fd_dirty_shader_state dirty = ctx->dirty_shader[t];
563
564 /* When we use CP_SET_DRAW_STATE objects to emit constant state,
565 * if we emit any of it we need to emit all. This is because
566 * we are using the same state-group-id each time for uniform
567 * state, and if previous update is never evaluated (due to no
568 * visible primitives in the current tile) then the new stateobj
569 * completely replaces the old one.
570 *
571 * Possibly if we split up different parts of the const state to
572 * different state-objects we could avoid this.
573 */
574 if (dirty && is_stateobj(ring))
575 dirty = ~0;
576
577 if (dirty & (FD_DIRTY_SHADER_PROG | FD_DIRTY_SHADER_CONST)) {
578 struct fd_constbuf_stateobj *constbuf;
579 bool shader_dirty;
580
581 constbuf = &ctx->constbuf[t];
582 shader_dirty = !!(dirty & FD_DIRTY_SHADER_PROG);
583
584 ring_wfi(ctx->batch, ring);
585
586 ir3_emit_user_consts(ctx->screen, v, ring, constbuf);
587 ir3_emit_ubos(ctx->screen, v, ring, constbuf);
588 if (shader_dirty)
589 ir3_emit_immediates(ctx->screen, v, ring);
590 }
591
592 if (dirty & (FD_DIRTY_SHADER_PROG | FD_DIRTY_SHADER_SSBO)) {
593 struct fd_shaderbuf_stateobj *sb = &ctx->shaderbuf[t];
594 ring_wfi(ctx->batch, ring);
595 ir3_emit_ssbo_sizes(ctx->screen, v, ring, sb);
596 }
597
598 if (dirty & (FD_DIRTY_SHADER_PROG | FD_DIRTY_SHADER_IMAGE)) {
599 struct fd_shaderimg_stateobj *si = &ctx->shaderimg[t];
600 ring_wfi(ctx->batch, ring);
601 ir3_emit_image_dims(ctx->screen, v, ring, si);
602 }
603 }
604
605 void
606 ir3_emit_vs_driver_params(const struct ir3_shader_variant *v,
607 struct fd_ringbuffer *ring, struct fd_context *ctx,
608 const struct pipe_draw_info *info)
609 {
610 debug_assert(ir3_needs_vs_driver_params(v));
611
612 const struct ir3_const_state *const_state = &v->shader->const_state;
613 uint32_t offset = const_state->offsets.driver_param;
614 uint32_t vertex_params[IR3_DP_VS_COUNT] = {
615 [IR3_DP_VTXID_BASE] = info->index_size ?
616 info->index_bias : info->start,
617 [IR3_DP_VTXCNT_MAX] = max_tf_vtx(ctx, v),
618 };
619 /* if no user-clip-planes, we don't need to emit the
620 * entire thing:
621 */
622 uint32_t vertex_params_size = 4;
623
624 if (v->key.ucp_enables) {
625 struct pipe_clip_state *ucp = &ctx->ucp;
626 unsigned pos = IR3_DP_UCP0_X;
627 for (unsigned i = 0; pos <= IR3_DP_UCP7_W; i++) {
628 for (unsigned j = 0; j < 4; j++) {
629 vertex_params[pos] = fui(ucp->ucp[i][j]);
630 pos++;
631 }
632 }
633 vertex_params_size = ARRAY_SIZE(vertex_params);
634 }
635
636 vertex_params_size = MAX2(vertex_params_size, const_state->num_driver_params);
637
638 bool needs_vtxid_base =
639 ir3_find_sysval_regid(v, SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) != regid(63, 0);
640
641 /* for indirect draw, we need to copy VTXID_BASE from
642 * indirect-draw parameters buffer.. which is annoying
643 * and means we can't easily emit these consts in cmd
644 * stream so need to copy them to bo.
645 */
646 if (info->indirect && needs_vtxid_base) {
647 struct pipe_draw_indirect_info *indirect = info->indirect;
648 struct pipe_resource *vertex_params_rsc =
649 pipe_buffer_create(&ctx->screen->base,
650 PIPE_BIND_CONSTANT_BUFFER, PIPE_USAGE_STREAM,
651 vertex_params_size * 4);
652 unsigned src_off = info->indirect->offset;;
653 void *ptr;
654
655 ptr = fd_bo_map(fd_resource(vertex_params_rsc)->bo);
656 memcpy(ptr, vertex_params, vertex_params_size * 4);
657
658 if (info->index_size) {
659 /* indexed draw, index_bias is 4th field: */
660 src_off += 3 * 4;
661 } else {
662 /* non-indexed draw, start is 3rd field: */
663 src_off += 2 * 4;
664 }
665
666 /* copy index_bias or start from draw params: */
667 ctx->screen->mem_to_mem(ring, vertex_params_rsc, 0,
668 indirect->buffer, src_off, 1);
669
670 emit_const(ctx->screen, ring, v, offset * 4, 0,
671 vertex_params_size, NULL, vertex_params_rsc);
672
673 pipe_resource_reference(&vertex_params_rsc, NULL);
674 } else {
675 emit_const(ctx->screen, ring, v, offset * 4, 0,
676 vertex_params_size, vertex_params, NULL);
677 }
678
679 /* if needed, emit stream-out buffer addresses: */
680 if (vertex_params[IR3_DP_VTXCNT_MAX] > 0) {
681 emit_tfbos(ctx, v, ring);
682 }
683 }
684
685 void
686 ir3_emit_vs_consts(const struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
687 struct fd_context *ctx, const struct pipe_draw_info *info)
688 {
689 debug_assert(v->type == MESA_SHADER_VERTEX);
690
691 emit_common_consts(v, ring, ctx, PIPE_SHADER_VERTEX);
692
693 /* emit driver params every time: */
694 if (info && ir3_needs_vs_driver_params(v)) {
695 ring_wfi(ctx->batch, ring);
696 ir3_emit_vs_driver_params(v, ring, ctx, info);
697 }
698 }
699
700 void
701 ir3_emit_fs_consts(const struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
702 struct fd_context *ctx)
703 {
704 debug_assert(v->type == MESA_SHADER_FRAGMENT);
705
706 emit_common_consts(v, ring, ctx, PIPE_SHADER_FRAGMENT);
707 }
708
709 /* emit compute-shader consts: */
710 void
711 ir3_emit_cs_consts(const struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
712 struct fd_context *ctx, const struct pipe_grid_info *info)
713 {
714 debug_assert(gl_shader_stage_is_compute(v->type));
715
716 emit_common_consts(v, ring, ctx, PIPE_SHADER_COMPUTE);
717
718 /* emit compute-shader driver-params: */
719 const struct ir3_const_state *const_state = &v->shader->const_state;
720 uint32_t offset = const_state->offsets.driver_param;
721 if (v->constlen > offset) {
722 ring_wfi(ctx->batch, ring);
723
724 if (info->indirect) {
725 struct pipe_resource *indirect = NULL;
726 unsigned indirect_offset;
727
728 /* This is a bit awkward, but CP_LOAD_STATE.EXT_SRC_ADDR needs
729 * to be aligned more strongly than 4 bytes. So in this case
730 * we need a temporary buffer to copy NumWorkGroups.xyz to.
731 *
732 * TODO if previous compute job is writing to info->indirect,
733 * we might need a WFI.. but since we currently flush for each
734 * compute job, we are probably ok for now.
735 */
736 if (info->indirect_offset & 0xf) {
737 indirect = pipe_buffer_create(&ctx->screen->base,
738 PIPE_BIND_COMMAND_ARGS_BUFFER, PIPE_USAGE_STREAM,
739 0x1000);
740 indirect_offset = 0;
741
742 ctx->screen->mem_to_mem(ring, indirect, 0, info->indirect,
743 info->indirect_offset, 3);
744 } else {
745 pipe_resource_reference(&indirect, info->indirect);
746 indirect_offset = info->indirect_offset;
747 }
748
749 emit_const(ctx->screen, ring, v, offset * 4,
750 indirect_offset, 4, NULL, indirect);
751
752 pipe_resource_reference(&indirect, NULL);
753 } else {
754 uint32_t compute_params[IR3_DP_CS_COUNT] = {
755 [IR3_DP_NUM_WORK_GROUPS_X] = info->grid[0],
756 [IR3_DP_NUM_WORK_GROUPS_Y] = info->grid[1],
757 [IR3_DP_NUM_WORK_GROUPS_Z] = info->grid[2],
758 [IR3_DP_LOCAL_GROUP_SIZE_X] = info->block[0],
759 [IR3_DP_LOCAL_GROUP_SIZE_Y] = info->block[1],
760 [IR3_DP_LOCAL_GROUP_SIZE_Z] = info->block[2],
761 };
762 uint32_t size = MIN2(const_state->num_driver_params,
763 v->constlen * 4 - offset * 4);
764
765 emit_const(ctx->screen, ring, v, offset * 4, 0, size,
766 compute_params, NULL);
767 }
768 }
769 }
770
771 static void *
772 ir3_shader_state_create(struct pipe_context *pctx, const struct pipe_shader_state *cso)
773 {
774 struct fd_context *ctx = fd_context(pctx);
775 struct ir3_compiler *compiler = ctx->screen->compiler;
776 return ir3_shader_create(compiler, cso, &ctx->debug, pctx->screen);
777 }
778
779 static void
780 ir3_shader_state_delete(struct pipe_context *pctx, void *hwcso)
781 {
782 struct ir3_shader *so = hwcso;
783 ir3_shader_destroy(so);
784 }
785
786 void
787 ir3_prog_init(struct pipe_context *pctx)
788 {
789 pctx->create_vs_state = ir3_shader_state_create;
790 pctx->delete_vs_state = ir3_shader_state_delete;
791
792 pctx->create_tcs_state = ir3_shader_state_create;
793 pctx->delete_tcs_state = ir3_shader_state_delete;
794
795 pctx->create_tes_state = ir3_shader_state_create;
796 pctx->delete_tes_state = ir3_shader_state_delete;
797
798 pctx->create_gs_state = ir3_shader_state_create;
799 pctx->delete_gs_state = ir3_shader_state_delete;
800
801 pctx->create_fs_state = ir3_shader_state_create;
802 pctx->delete_fs_state = ir3_shader_state_delete;
803 }