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