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