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