3bb29daf9b8d10fdfa55c24e725814b9ee21dae1
[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/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, struct pipe_debug_callback *debug)
48 {
49 if (!unlikely(fd_mesa_debug & FD_DBG_SHADERDB))
50 return;
51
52 pipe_debug_message(debug, SHADER_INFO, "\n"
53 "SHADER-DB: %s prog %d/%d: %u instructions, %u dwords\n"
54 "SHADER-DB: %s prog %d/%d: %u half, %u full\n"
55 "SHADER-DB: %s prog %d/%d: %u const, %u constlen\n"
56 "SHADER-DB: %s prog %d/%d: %u (ss), %u (sy)\n"
57 "SHADER-DB: %s prog %d/%d: max_sun=%u\n",
58 ir3_shader_stage(v->shader),
59 v->shader->id, v->id,
60 v->info.instrs_count,
61 v->info.sizedwords,
62 ir3_shader_stage(v->shader),
63 v->shader->id, v->id,
64 v->info.max_half_reg + 1,
65 v->info.max_reg + 1,
66 ir3_shader_stage(v->shader),
67 v->shader->id, v->id,
68 v->info.max_const + 1,
69 v->constlen,
70 ir3_shader_stage(v->shader),
71 v->shader->id, v->id,
72 v->info.ss, v->info.sy,
73 ir3_shader_stage(v->shader),
74 v->shader->id, v->id,
75 v->max_sun);
76 }
77
78 struct ir3_shader_variant *
79 ir3_shader_variant(struct ir3_shader *shader, struct ir3_shader_key key,
80 bool binning_pass, struct pipe_debug_callback *debug)
81 {
82 struct ir3_shader_variant *v;
83 bool created = false;
84
85 /* some shader key values only apply to vertex or frag shader,
86 * so normalize the key to avoid constructing multiple identical
87 * variants:
88 */
89 ir3_normalize_key(&key, shader->type);
90
91 v = ir3_shader_get_variant(shader, &key, binning_pass, &created);
92
93 if (created) {
94 dump_shader_info(v, debug);
95 }
96
97 return v;
98 }
99
100 static void
101 copy_stream_out(struct ir3_stream_output_info *i,
102 const struct pipe_stream_output_info *p)
103 {
104 STATIC_ASSERT(ARRAY_SIZE(i->stride) == ARRAY_SIZE(p->stride));
105 STATIC_ASSERT(ARRAY_SIZE(i->output) == ARRAY_SIZE(p->output));
106
107 i->num_outputs = p->num_outputs;
108 for (int n = 0; n < ARRAY_SIZE(i->stride); n++)
109 i->stride[n] = p->stride[n];
110
111 for (int n = 0; n < ARRAY_SIZE(i->output); n++) {
112 i->output[n].register_index = p->output[n].register_index;
113 i->output[n].start_component = p->output[n].start_component;
114 i->output[n].num_components = p->output[n].num_components;
115 i->output[n].output_buffer = p->output[n].output_buffer;
116 i->output[n].dst_offset = p->output[n].dst_offset;
117 i->output[n].stream = p->output[n].stream;
118 }
119 }
120
121 struct ir3_shader *
122 ir3_shader_create(struct ir3_compiler *compiler,
123 const struct pipe_shader_state *cso, gl_shader_stage type,
124 struct pipe_debug_callback *debug,
125 struct pipe_screen *screen)
126 {
127 nir_shader *nir;
128 if (cso->type == PIPE_SHADER_IR_NIR) {
129 /* we take ownership of the reference: */
130 nir = cso->ir.nir;
131 } else {
132 debug_assert(cso->type == PIPE_SHADER_IR_TGSI);
133 if (ir3_shader_debug & IR3_DBG_DISASM) {
134 tgsi_dump(cso->tokens, 0);
135 }
136 nir = ir3_tgsi_to_nir(compiler, cso->tokens, screen);
137 }
138
139 struct ir3_shader *shader = ir3_shader_from_nir(compiler, nir);
140
141 copy_stream_out(&shader->stream_output, &cso->stream_output);
142
143 if (fd_mesa_debug & FD_DBG_SHADERDB) {
144 /* if shader-db run, create a standard variant immediately
145 * (as otherwise nothing will trigger the shader to be
146 * actually compiled)
147 */
148 static struct ir3_shader_key key;
149 memset(&key, 0, sizeof(key));
150 ir3_shader_variant(shader, key, false, debug);
151 }
152 return shader;
153 }
154
155 /* a bit annoying that compute-shader and normal shader state objects
156 * aren't a bit more aligned.
157 */
158 struct ir3_shader *
159 ir3_shader_create_compute(struct ir3_compiler *compiler,
160 const struct pipe_compute_state *cso,
161 struct pipe_debug_callback *debug,
162 struct pipe_screen *screen)
163 {
164 nir_shader *nir;
165 if (cso->ir_type == PIPE_SHADER_IR_NIR) {
166 /* we take ownership of the reference: */
167 nir = (nir_shader *)cso->prog;
168 } else {
169 debug_assert(cso->ir_type == PIPE_SHADER_IR_TGSI);
170 if (ir3_shader_debug & IR3_DBG_DISASM) {
171 tgsi_dump(cso->prog, 0);
172 }
173 nir = ir3_tgsi_to_nir(compiler, cso->prog, screen);
174 }
175
176 struct ir3_shader *shader = ir3_shader_from_nir(compiler, nir);
177
178 return shader;
179 }
180
181 struct nir_shader *
182 ir3_tgsi_to_nir(struct ir3_compiler *compiler,
183 const struct tgsi_token *tokens,
184 struct pipe_screen *screen)
185 {
186 if (!screen) {
187 const nir_shader_compiler_options *options =
188 ir3_get_compiler_options(compiler);
189 return tgsi_to_nir_noscreen(tokens, options);
190 }
191
192 return tgsi_to_nir(tokens, screen);
193 }
194
195 /* This has to reach into the fd_context a bit more than the rest of
196 * ir3, but it needs to be aligned with the compiler, so both agree
197 * on which const regs hold what. And the logic is identical between
198 * a3xx/a4xx, the only difference is small details in the actual
199 * CP_LOAD_STATE packets (which is handled inside the generation
200 * specific ctx->emit_const(_bo)() fxns)
201 */
202
203 #include "freedreno_resource.h"
204
205 static inline bool
206 is_stateobj(struct fd_ringbuffer *ring)
207 {
208 /* XXX this is an ugly way to differentiate.. */
209 return !!(ring->flags & FD_RINGBUFFER_STREAMING);
210 }
211
212 static inline void
213 ring_wfi(struct fd_batch *batch, struct fd_ringbuffer *ring)
214 {
215 /* when we emit const state via ring (IB2) we need a WFI, but when
216 * it is emit'd via stateobj, we don't
217 */
218 if (is_stateobj(ring))
219 return;
220
221 fd_wfi(batch, ring);
222 }
223
224 static void
225 emit_user_consts(struct fd_context *ctx, const struct ir3_shader_variant *v,
226 struct fd_ringbuffer *ring, struct fd_constbuf_stateobj *constbuf)
227 {
228 const unsigned index = 0; /* user consts are index 0 */
229
230 if (constbuf->enabled_mask & (1 << index)) {
231 struct pipe_constant_buffer *cb = &constbuf->cb[index];
232 /* size in dwords, aligned to vec4. (This works at least
233 * with mesa/st, which seems to align constant buffer to
234 * 16 bytes)
235 */
236 unsigned size = align(cb->buffer_size, 16) / 4;
237
238 /* in particular, with binning shader we may end up with
239 * unused consts, ie. we could end up w/ constlen that is
240 * smaller than first_driver_param. In that case truncate
241 * the user consts early to avoid HLSQ lockup caused by
242 * writing too many consts
243 */
244 const struct ir3_const_state *const_state = &v->const_state;
245 uint32_t max_const = MIN2(const_state->num_uniforms, v->constlen);
246
247 /* and even if the start of the const buffer is before
248 * first_immediate, the end may not be:
249 */
250 size = MIN2(size, 4 * max_const);
251
252 if (size > 0) {
253 ring_wfi(ctx->batch, ring);
254 ctx->emit_const(ring, v->type, 0,
255 cb->buffer_offset, size,
256 cb->user_buffer, cb->buffer);
257 }
258 }
259
260 struct ir3_ubo_analysis_state *state;
261 state = &v->shader->ubo_state;
262
263 for (uint32_t i = 1; i < ARRAY_SIZE(state->range); i++) {
264 struct pipe_constant_buffer *cb = &constbuf->cb[i];
265
266 if (state->range[i].start < state->range[i].end &&
267 constbuf->enabled_mask & (1 << i)) {
268
269 uint32_t size = state->range[i].end - state->range[i].start;
270 uint32_t offset = cb->buffer_offset + state->range[i].start;
271 debug_assert((state->range[i].offset % 16) == 0);
272 debug_assert((size % 16) == 0);
273 debug_assert((offset % 16) == 0);
274 ctx->emit_const(ring, v->type, state->range[i].offset / 4,
275 offset, size / 4, cb->user_buffer, cb->buffer);
276 }
277 }
278 }
279
280 static void
281 emit_ubos(struct fd_context *ctx, const struct ir3_shader_variant *v,
282 struct fd_ringbuffer *ring, struct fd_constbuf_stateobj *constbuf)
283 {
284 const struct ir3_const_state *const_state = &v->const_state;
285 uint32_t offset = const_state->offsets.ubo;
286 if (v->constlen > offset) {
287 uint32_t params = const_state->num_ubos;
288 uint32_t offsets[params];
289 struct pipe_resource *prscs[params];
290
291 for (uint32_t i = 0; i < params; i++) {
292 const uint32_t index = i + 1; /* UBOs start at index 1 */
293 struct pipe_constant_buffer *cb = &constbuf->cb[index];
294 assert(!cb->user_buffer);
295
296 if ((constbuf->enabled_mask & (1 << index)) && cb->buffer) {
297 offsets[i] = cb->buffer_offset;
298 prscs[i] = cb->buffer;
299 } else {
300 offsets[i] = 0;
301 prscs[i] = NULL;
302 }
303 }
304
305 ring_wfi(ctx->batch, ring);
306 ctx->emit_const_bo(ring, v->type, false, offset * 4, params, prscs, offsets);
307 }
308 }
309
310 static void
311 emit_ssbo_sizes(struct fd_context *ctx, const struct ir3_shader_variant *v,
312 struct fd_ringbuffer *ring, struct fd_shaderbuf_stateobj *sb)
313 {
314 const struct ir3_const_state *const_state = &v->const_state;
315 uint32_t offset = const_state->offsets.ssbo_sizes;
316 if (v->constlen > offset) {
317 uint32_t sizes[align(const_state->ssbo_size.count, 4)];
318 unsigned mask = const_state->ssbo_size.mask;
319
320 while (mask) {
321 unsigned index = u_bit_scan(&mask);
322 unsigned off = const_state->ssbo_size.off[index];
323 sizes[off] = sb->sb[index].buffer_size;
324 }
325
326 ring_wfi(ctx->batch, ring);
327 ctx->emit_const(ring, v->type, offset * 4,
328 0, ARRAY_SIZE(sizes), sizes, NULL);
329 }
330 }
331
332 static void
333 emit_image_dims(struct fd_context *ctx, const struct ir3_shader_variant *v,
334 struct fd_ringbuffer *ring, struct fd_shaderimg_stateobj *si)
335 {
336 const struct ir3_const_state *const_state = &v->const_state;
337 uint32_t offset = const_state->offsets.image_dims;
338 if (v->constlen > offset) {
339 uint32_t dims[align(const_state->image_dims.count, 4)];
340 unsigned mask = const_state->image_dims.mask;
341
342 while (mask) {
343 struct pipe_image_view *img;
344 struct fd_resource *rsc;
345 unsigned index = u_bit_scan(&mask);
346 unsigned off = const_state->image_dims.off[index];
347
348 img = &si->si[index];
349 rsc = fd_resource(img->resource);
350
351 dims[off + 0] = util_format_get_blocksize(img->format);
352 if (img->resource->target != PIPE_BUFFER) {
353 unsigned lvl = img->u.tex.level;
354 /* note for 2d/cube/etc images, even if re-interpreted
355 * as a different color format, the pixel size should
356 * be the same, so use original dimensions for y and z
357 * stride:
358 */
359 dims[off + 1] = rsc->slices[lvl].pitch * rsc->cpp;
360 /* see corresponding logic in fd_resource_offset(): */
361 if (rsc->layer_first) {
362 dims[off + 2] = rsc->layer_size;
363 } else {
364 dims[off + 2] = rsc->slices[lvl].size0;
365 }
366 } else {
367 /* For buffer-backed images, the log2 of the format's
368 * bytes-per-pixel is placed on the 2nd slot. This is useful
369 * when emitting image_size instructions, for which we need
370 * to divide by bpp for image buffers. Since the bpp
371 * can only be power-of-two, the division is implemented
372 * as a SHR, and for that it is handy to have the log2 of
373 * bpp as a constant. (log2 = first-set-bit - 1)
374 */
375 dims[off + 1] = ffs(dims[off + 0]) - 1;
376 }
377 }
378
379 ring_wfi(ctx->batch, ring);
380 ctx->emit_const(ring, v->type, offset * 4,
381 0, ARRAY_SIZE(dims), dims, NULL);
382 }
383 }
384
385 static void
386 emit_immediates(struct fd_context *ctx, const struct ir3_shader_variant *v,
387 struct fd_ringbuffer *ring)
388 {
389 const struct ir3_const_state *const_state = &v->const_state;
390 uint32_t base = const_state->offsets.immediate;
391 int size = v->immediates_count;
392
393 /* truncate size to avoid writing constants that shader
394 * does not use:
395 */
396 size = MIN2(size + base, v->constlen) - base;
397
398 /* convert out of vec4: */
399 base *= 4;
400 size *= 4;
401
402 if (size > 0) {
403 ring_wfi(ctx->batch, ring);
404 ctx->emit_const(ring, v->type, base,
405 0, size, v->immediates[0].val, NULL);
406 }
407 }
408
409 /* emit stream-out buffers: */
410 static void
411 emit_tfbos(struct fd_context *ctx, const struct ir3_shader_variant *v,
412 struct fd_ringbuffer *ring)
413 {
414 /* streamout addresses after driver-params: */
415 const struct ir3_const_state *const_state = &v->const_state;
416 uint32_t offset = const_state->offsets.tfbo;
417 if (v->constlen > offset) {
418 struct fd_streamout_stateobj *so = &ctx->streamout;
419 struct ir3_stream_output_info *info = &v->shader->stream_output;
420 uint32_t params = 4;
421 uint32_t offsets[params];
422 struct pipe_resource *prscs[params];
423
424 for (uint32_t i = 0; i < params; i++) {
425 struct pipe_stream_output_target *target = so->targets[i];
426
427 if (target) {
428 offsets[i] = (so->offsets[i] * info->stride[i] * 4) +
429 target->buffer_offset;
430 prscs[i] = target->buffer;
431 } else {
432 offsets[i] = 0;
433 prscs[i] = NULL;
434 }
435 }
436
437 ring_wfi(ctx->batch, ring);
438 ctx->emit_const_bo(ring, v->type, true, offset * 4, params, prscs, offsets);
439 }
440 }
441
442 static uint32_t
443 max_tf_vtx(struct fd_context *ctx, const struct ir3_shader_variant *v)
444 {
445 struct fd_streamout_stateobj *so = &ctx->streamout;
446 struct ir3_stream_output_info *info = &v->shader->stream_output;
447 uint32_t maxvtxcnt = 0x7fffffff;
448
449 if (ctx->screen->gpu_id >= 500)
450 return 0;
451 if (v->binning_pass)
452 return 0;
453 if (v->shader->stream_output.num_outputs == 0)
454 return 0;
455 if (so->num_targets == 0)
456 return 0;
457
458 /* offset to write to is:
459 *
460 * total_vtxcnt = vtxcnt + offsets[i]
461 * offset = total_vtxcnt * stride[i]
462 *
463 * offset = vtxcnt * stride[i] ; calculated in shader
464 * + offsets[i] * stride[i] ; calculated at emit_tfbos()
465 *
466 * assuming for each vtx, each target buffer will have data written
467 * up to 'offset + stride[i]', that leaves maxvtxcnt as:
468 *
469 * buffer_size = (maxvtxcnt * stride[i]) + stride[i]
470 * maxvtxcnt = (buffer_size - stride[i]) / stride[i]
471 *
472 * but shader is actually doing a less-than (rather than less-than-
473 * equal) check, so we can drop the -stride[i].
474 *
475 * TODO is assumption about `offset + stride[i]` legit?
476 */
477 for (unsigned i = 0; i < so->num_targets; i++) {
478 struct pipe_stream_output_target *target = so->targets[i];
479 unsigned stride = info->stride[i] * 4; /* convert dwords->bytes */
480 if (target) {
481 uint32_t max = target->buffer_size / stride;
482 maxvtxcnt = MIN2(maxvtxcnt, max);
483 }
484 }
485
486 return maxvtxcnt;
487 }
488
489 static void
490 emit_common_consts(const struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
491 struct fd_context *ctx, enum pipe_shader_type t)
492 {
493 enum fd_dirty_shader_state dirty = ctx->dirty_shader[t];
494
495 /* When we use CP_SET_DRAW_STATE objects to emit constant state,
496 * if we emit any of it we need to emit all. This is because
497 * we are using the same state-group-id each time for uniform
498 * state, and if previous update is never evaluated (due to no
499 * visible primitives in the current tile) then the new stateobj
500 * completely replaces the old one.
501 *
502 * Possibly if we split up different parts of the const state to
503 * different state-objects we could avoid this.
504 */
505 if (dirty && is_stateobj(ring))
506 dirty = ~0;
507
508 if (dirty & (FD_DIRTY_SHADER_PROG | FD_DIRTY_SHADER_CONST)) {
509 struct fd_constbuf_stateobj *constbuf;
510 bool shader_dirty;
511
512 constbuf = &ctx->constbuf[t];
513 shader_dirty = !!(dirty & FD_DIRTY_SHADER_PROG);
514
515 emit_user_consts(ctx, v, ring, constbuf);
516 emit_ubos(ctx, v, ring, constbuf);
517 if (shader_dirty)
518 emit_immediates(ctx, v, ring);
519 }
520
521 if (dirty & (FD_DIRTY_SHADER_PROG | FD_DIRTY_SHADER_SSBO)) {
522 struct fd_shaderbuf_stateobj *sb = &ctx->shaderbuf[t];
523 emit_ssbo_sizes(ctx, v, ring, sb);
524 }
525
526 if (dirty & (FD_DIRTY_SHADER_PROG | FD_DIRTY_SHADER_IMAGE)) {
527 struct fd_shaderimg_stateobj *si = &ctx->shaderimg[t];
528 emit_image_dims(ctx, v, ring, si);
529 }
530 }
531
532 void
533 ir3_emit_vs_consts(const struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
534 struct fd_context *ctx, const struct pipe_draw_info *info)
535 {
536 debug_assert(v->type == MESA_SHADER_VERTEX);
537
538 emit_common_consts(v, ring, ctx, PIPE_SHADER_VERTEX);
539
540 /* emit driver params every time: */
541 /* TODO skip emit if shader doesn't use driver params to avoid WFI.. */
542 if (info) {
543 const struct ir3_const_state *const_state = &v->const_state;
544 uint32_t offset = const_state->offsets.driver_param;
545 if (v->constlen > offset) {
546 uint32_t vertex_params[IR3_DP_VS_COUNT] = {
547 [IR3_DP_VTXID_BASE] = info->index_size ?
548 info->index_bias : info->start,
549 [IR3_DP_VTXCNT_MAX] = max_tf_vtx(ctx, v),
550 };
551 /* if no user-clip-planes, we don't need to emit the
552 * entire thing:
553 */
554 uint32_t vertex_params_size = 4;
555
556 if (v->key.ucp_enables) {
557 struct pipe_clip_state *ucp = &ctx->ucp;
558 unsigned pos = IR3_DP_UCP0_X;
559 for (unsigned i = 0; pos <= IR3_DP_UCP7_W; i++) {
560 for (unsigned j = 0; j < 4; j++) {
561 vertex_params[pos] = fui(ucp->ucp[i][j]);
562 pos++;
563 }
564 }
565 vertex_params_size = ARRAY_SIZE(vertex_params);
566 }
567
568 ring_wfi(ctx->batch, ring);
569
570 bool needs_vtxid_base =
571 ir3_find_sysval_regid(v, SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) != regid(63, 0);
572
573 /* for indirect draw, we need to copy VTXID_BASE from
574 * indirect-draw parameters buffer.. which is annoying
575 * and means we can't easily emit these consts in cmd
576 * stream so need to copy them to bo.
577 */
578 if (info->indirect && needs_vtxid_base) {
579 struct pipe_draw_indirect_info *indirect = info->indirect;
580 struct pipe_resource *vertex_params_rsc =
581 pipe_buffer_create(&ctx->screen->base,
582 PIPE_BIND_CONSTANT_BUFFER, PIPE_USAGE_STREAM,
583 vertex_params_size * 4);
584 unsigned src_off = info->indirect->offset;;
585 void *ptr;
586
587 ptr = fd_bo_map(fd_resource(vertex_params_rsc)->bo);
588 memcpy(ptr, vertex_params, vertex_params_size * 4);
589
590 if (info->index_size) {
591 /* indexed draw, index_bias is 4th field: */
592 src_off += 3 * 4;
593 } else {
594 /* non-indexed draw, start is 3rd field: */
595 src_off += 2 * 4;
596 }
597
598 /* copy index_bias or start from draw params: */
599 ctx->mem_to_mem(ring, vertex_params_rsc, 0,
600 indirect->buffer, src_off, 1);
601
602 ctx->emit_const(ring, MESA_SHADER_VERTEX, offset * 4, 0,
603 vertex_params_size, NULL, vertex_params_rsc);
604
605 pipe_resource_reference(&vertex_params_rsc, NULL);
606 } else {
607 ctx->emit_const(ring, MESA_SHADER_VERTEX, offset * 4, 0,
608 vertex_params_size, vertex_params, NULL);
609 }
610
611 /* if needed, emit stream-out buffer addresses: */
612 if (vertex_params[IR3_DP_VTXCNT_MAX] > 0) {
613 emit_tfbos(ctx, v, ring);
614 }
615 }
616 }
617 }
618
619 void
620 ir3_emit_fs_consts(const struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
621 struct fd_context *ctx)
622 {
623 debug_assert(v->type == MESA_SHADER_FRAGMENT);
624
625 emit_common_consts(v, ring, ctx, PIPE_SHADER_FRAGMENT);
626 }
627
628 /* emit compute-shader consts: */
629 void
630 ir3_emit_cs_consts(const struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
631 struct fd_context *ctx, const struct pipe_grid_info *info)
632 {
633 debug_assert(gl_shader_stage_is_compute(v->type));
634
635 emit_common_consts(v, ring, ctx, PIPE_SHADER_COMPUTE);
636
637 /* emit compute-shader driver-params: */
638 const struct ir3_const_state *const_state = &v->const_state;
639 uint32_t offset = const_state->offsets.driver_param;
640 if (v->constlen > offset) {
641 ring_wfi(ctx->batch, ring);
642
643 if (info->indirect) {
644 struct pipe_resource *indirect = NULL;
645 unsigned indirect_offset;
646
647 /* This is a bit awkward, but CP_LOAD_STATE.EXT_SRC_ADDR needs
648 * to be aligned more strongly than 4 bytes. So in this case
649 * we need a temporary buffer to copy NumWorkGroups.xyz to.
650 *
651 * TODO if previous compute job is writing to info->indirect,
652 * we might need a WFI.. but since we currently flush for each
653 * compute job, we are probably ok for now.
654 */
655 if (info->indirect_offset & 0xf) {
656 indirect = pipe_buffer_create(&ctx->screen->base,
657 PIPE_BIND_COMMAND_ARGS_BUFFER, PIPE_USAGE_STREAM,
658 0x1000);
659 indirect_offset = 0;
660
661 ctx->mem_to_mem(ring, indirect, 0, info->indirect,
662 info->indirect_offset, 3);
663 } else {
664 pipe_resource_reference(&indirect, info->indirect);
665 indirect_offset = info->indirect_offset;
666 }
667
668 ctx->emit_const(ring, MESA_SHADER_COMPUTE, offset * 4,
669 indirect_offset, 4, NULL, indirect);
670
671 pipe_resource_reference(&indirect, NULL);
672 } else {
673 uint32_t compute_params[IR3_DP_CS_COUNT] = {
674 [IR3_DP_NUM_WORK_GROUPS_X] = info->grid[0],
675 [IR3_DP_NUM_WORK_GROUPS_Y] = info->grid[1],
676 [IR3_DP_NUM_WORK_GROUPS_Z] = info->grid[2],
677 [IR3_DP_LOCAL_GROUP_SIZE_X] = info->block[0],
678 [IR3_DP_LOCAL_GROUP_SIZE_Y] = info->block[1],
679 [IR3_DP_LOCAL_GROUP_SIZE_Z] = info->block[2],
680 };
681
682 ctx->emit_const(ring, MESA_SHADER_COMPUTE, offset * 4, 0,
683 ARRAY_SIZE(compute_params), compute_params, NULL);
684 }
685 }
686 }