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