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 "ir3/ir3_nir.h"
28
29 /* This has to reach into the fd_context a bit more than the rest of
30 * ir3, but it needs to be aligned with the compiler, so both agree
31 * on which const regs hold what. And the logic is identical between
32 * ir3 generations, the only difference is small details in the actual
33 * CP_LOAD_STATE packets (which is handled inside the generation
34 * specific ctx->emit_const(_bo)() fxns)
35 *
36 * This file should be included in only a single .c file per gen, which
37 * defines the following functions:
38 */
39
40 static bool is_stateobj(struct fd_ringbuffer *ring);
41
42 static void emit_const_user(struct fd_ringbuffer *ring,
43 const struct ir3_shader_variant *v, uint32_t regid,
44 uint32_t size, const uint32_t *user_buffer);
45
46 static void emit_const_bo(struct fd_ringbuffer *ring,
47 const struct ir3_shader_variant *v, uint32_t regid,
48 uint32_t offset, uint32_t size, struct fd_bo *bo);
49
50 static void
emit_const_prsc(struct fd_ringbuffer * ring,const struct ir3_shader_variant * v,uint32_t regid,uint32_t offset,uint32_t size,struct pipe_resource * buffer)51 emit_const_prsc(struct fd_ringbuffer *ring, const struct ir3_shader_variant *v,
52 uint32_t regid, uint32_t offset, uint32_t size,
53 struct pipe_resource *buffer)
54 {
55 struct fd_resource *rsc = fd_resource(buffer);
56 emit_const_bo(ring, v, regid, offset, size, rsc->bo);
57 }
58
59 static void emit_const_ptrs(struct fd_ringbuffer *ring,
60 const struct ir3_shader_variant *v,
61 uint32_t dst_offset, uint32_t num,
62 struct fd_bo **bos, uint32_t *offsets);
63
64 static void
emit_const_asserts(struct fd_ringbuffer * ring,const struct ir3_shader_variant * v,uint32_t regid,uint32_t sizedwords)65 emit_const_asserts(struct fd_ringbuffer *ring,
66 const struct ir3_shader_variant *v, uint32_t regid,
67 uint32_t sizedwords)
68 {
69 assert((regid % 4) == 0);
70 assert((sizedwords % 4) == 0);
71 assert(regid + sizedwords <= v->constlen * 4);
72 }
73
74 static void
ring_wfi(struct fd_batch * batch,struct fd_ringbuffer * ring)75 ring_wfi(struct fd_batch *batch, struct fd_ringbuffer *ring) assert_dt
76 {
77 /* when we emit const state via ring (IB2) we need a WFI, but when
78 * it is emit'd via stateobj, we don't
79 */
80 if (is_stateobj(ring))
81 return;
82
83 fd_wfi(batch, ring);
84 }
85
86 /**
87 * Indirectly calculates size of cmdstream needed for ir3_emit_user_consts().
88 * Returns number of packets, and total size of all the payload.
89 *
90 * The value can be a worst-case, ie. some shader variants may not read all
91 * consts, etc.
92 *
93 * Returns size in dwords.
94 */
95 static inline void
ir3_user_consts_size(struct ir3_ubo_analysis_state * state,unsigned * packets,unsigned * size)96 ir3_user_consts_size(struct ir3_ubo_analysis_state *state, unsigned *packets,
97 unsigned *size)
98 {
99 *packets = *size = 0;
100
101 for (uint32_t i = 0; i < ARRAY_SIZE(state->range); i++) {
102 if (state->range[i].start < state->range[i].end) {
103 *size += state->range[i].end - state->range[i].start;
104 (*packets)++;
105 }
106 }
107 }
108
109 /**
110 * Uploads the referenced subranges of the nir constant_data to the hardware's
111 * constant buffer.
112 */
113 static inline void
ir3_emit_constant_data(const struct ir3_shader_variant * v,struct fd_ringbuffer * ring)114 ir3_emit_constant_data(const struct ir3_shader_variant *v,
115 struct fd_ringbuffer *ring)
116 {
117 const struct ir3_const_state *const_state = ir3_const_state(v);
118 const struct ir3_ubo_analysis_state *state = &const_state->ubo_state;
119
120 for (unsigned i = 0; i < state->num_enabled; i++) {
121 unsigned ubo = state->range[i].ubo.block;
122 if (ubo != const_state->consts_ubo.idx)
123 continue;
124
125 uint32_t size = state->range[i].end - state->range[i].start;
126
127 /* Pre-a6xx, we might have ranges enabled in the shader that aren't
128 * used in the binning variant.
129 */
130 if (16 * v->constlen <= state->range[i].offset)
131 continue;
132
133 /* and even if the start of the const buffer is before
134 * first_immediate, the end may not be:
135 */
136 size = MIN2(size, (16 * v->constlen) - state->range[i].offset);
137
138 if (size == 0)
139 continue;
140
141 emit_const_bo(ring, v, state->range[i].offset / 4,
142 v->info.constant_data_offset + state->range[i].start,
143 size / 4, v->bo);
144 }
145 }
146
147 /**
148 * Uploads sub-ranges of UBOs to the hardware's constant buffer (UBO access
149 * outside of these ranges will be done using full UBO accesses in the
150 * shader).
151 */
152 static inline void
ir3_emit_user_consts(const struct ir3_shader_variant * v,struct fd_ringbuffer * ring,struct fd_constbuf_stateobj * constbuf)153 ir3_emit_user_consts(const struct ir3_shader_variant *v,
154 struct fd_ringbuffer *ring,
155 struct fd_constbuf_stateobj *constbuf)
156 {
157 const struct ir3_const_state *const_state = ir3_const_state(v);
158 const struct ir3_ubo_analysis_state *state = &const_state->ubo_state;
159
160 for (unsigned i = 0; i < state->num_enabled; i++) {
161 assert(!state->range[i].ubo.bindless);
162 unsigned ubo = state->range[i].ubo.block;
163 if (!(constbuf->enabled_mask & (1 << ubo)) ||
164 ubo == const_state->consts_ubo.idx) {
165 continue;
166 }
167 struct pipe_constant_buffer *cb = &constbuf->cb[ubo];
168
169 uint32_t size = state->range[i].end - state->range[i].start;
170 uint32_t offset = cb->buffer_offset + state->range[i].start;
171
172 /* Pre-a6xx, we might have ranges enabled in the shader that aren't
173 * used in the binning variant.
174 */
175 if (16 * v->constlen <= state->range[i].offset)
176 continue;
177
178 /* and even if the start of the const buffer is before
179 * first_immediate, the end may not be:
180 */
181 size = MIN2(size, (16 * v->constlen) - state->range[i].offset);
182
183 if (size == 0)
184 continue;
185
186 /* things should be aligned to vec4: */
187 assert((state->range[i].offset % 16) == 0);
188 assert((size % 16) == 0);
189 assert((offset % 16) == 0);
190
191 if (cb->user_buffer) {
192 uint8_t *p = (uint8_t *)cb->user_buffer;
193 p += state->range[i].start;
194 emit_const_user(ring, v, state->range[i].offset / 4, size / 4, (uint32_t *)p);
195 } else {
196 emit_const_prsc(ring, v, state->range[i].offset / 4, offset, size / 4,
197 cb->buffer);
198 }
199 }
200 }
201
202 static inline void
ir3_emit_ubos(struct fd_context * ctx,const struct ir3_shader_variant * v,struct fd_ringbuffer * ring,struct fd_constbuf_stateobj * constbuf)203 ir3_emit_ubos(struct fd_context *ctx, const struct ir3_shader_variant *v,
204 struct fd_ringbuffer *ring, struct fd_constbuf_stateobj *constbuf)
205 {
206 const struct ir3_const_state *const_state = ir3_const_state(v);
207 uint32_t offset = const_state->offsets.ubo;
208
209 /* a6xx+ uses UBO state and ldc instead of pointers emitted in
210 * const state and ldg:
211 */
212 if (ctx->screen->gen >= 6)
213 return;
214
215 if (v->constlen > offset) {
216 uint32_t params = const_state->num_ubos;
217 uint32_t offsets[params];
218 struct fd_bo *bos[params];
219
220 for (uint32_t i = 0; i < params; i++) {
221 if (i == const_state->consts_ubo.idx) {
222 bos[i] = v->bo;
223 offsets[i] = v->info.constant_data_offset;
224 continue;
225 }
226
227 struct pipe_constant_buffer *cb = &constbuf->cb[i];
228
229 /* If we have user pointers (constbuf 0, aka GL uniforms), upload
230 * them to a buffer now, and save it in the constbuf so that we
231 * don't have to reupload until they get changed.
232 */
233 if (cb->user_buffer) {
234 struct pipe_context *pctx = &ctx->base;
235 u_upload_data(pctx->stream_uploader, 0, cb->buffer_size, 64,
236 cb->user_buffer, &cb->buffer_offset, &cb->buffer);
237 cb->user_buffer = NULL;
238 }
239
240 if ((constbuf->enabled_mask & (1 << i)) && cb->buffer) {
241 offsets[i] = cb->buffer_offset;
242 bos[i] = fd_resource(cb->buffer)->bo;
243 } else {
244 offsets[i] = 0;
245 bos[i] = NULL;
246 }
247 }
248
249 assert(offset * 4 + params <= v->constlen * 4);
250
251 emit_const_ptrs(ring, v, offset * 4, params, bos, offsets);
252 }
253 }
254
255 static inline void
ir3_emit_image_dims(struct fd_screen * screen,const struct ir3_shader_variant * v,struct fd_ringbuffer * ring,struct fd_shaderimg_stateobj * si)256 ir3_emit_image_dims(struct fd_screen *screen,
257 const struct ir3_shader_variant *v,
258 struct fd_ringbuffer *ring,
259 struct fd_shaderimg_stateobj *si)
260 {
261 const struct ir3_const_state *const_state = ir3_const_state(v);
262 uint32_t offset = const_state->offsets.image_dims;
263 if (v->constlen > offset) {
264 uint32_t dims[align(const_state->image_dims.count, 4)];
265 unsigned mask = const_state->image_dims.mask;
266
267 while (mask) {
268 struct pipe_image_view *img;
269 struct fd_resource *rsc;
270 unsigned index = u_bit_scan(&mask);
271 unsigned off = const_state->image_dims.off[index];
272
273 img = &si->si[index];
274 rsc = fd_resource(img->resource);
275
276 dims[off + 0] = util_format_get_blocksize(img->format);
277 if (img->resource->target != PIPE_BUFFER) {
278 struct fdl_slice *slice = fd_resource_slice(rsc, img->u.tex.level);
279 /* note for 2d/cube/etc images, even if re-interpreted
280 * as a different color format, the pixel size should
281 * be the same, so use original dimensions for y and z
282 * stride:
283 */
284 dims[off + 1] = fd_resource_pitch(rsc, img->u.tex.level);
285 /* see corresponding logic in fd_resource_offset(): */
286 if (rsc->layout.layer_first) {
287 dims[off + 2] = rsc->layout.layer_size;
288 } else {
289 dims[off + 2] = slice->size0;
290 }
291 } else {
292 /* For buffer-backed images, the log2 of the format's
293 * bytes-per-pixel is placed on the 2nd slot. This is useful
294 * when emitting image_size instructions, for which we need
295 * to divide by bpp for image buffers. Since the bpp
296 * can only be power-of-two, the division is implemented
297 * as a SHR, and for that it is handy to have the log2 of
298 * bpp as a constant. (log2 = first-set-bit - 1)
299 */
300 dims[off + 1] = ffs(dims[off + 0]) - 1;
301 }
302 }
303 uint32_t size = MIN2(ARRAY_SIZE(dims), v->constlen * 4 - offset * 4);
304
305 emit_const_user(ring, v, offset * 4, size, dims);
306 }
307 }
308
309 static inline void
ir3_emit_immediates(const struct ir3_shader_variant * v,struct fd_ringbuffer * ring)310 ir3_emit_immediates(const struct ir3_shader_variant *v,
311 struct fd_ringbuffer *ring)
312 {
313 const struct ir3_const_state *const_state = ir3_const_state(v);
314 uint32_t base = const_state->offsets.immediate;
315 int size = DIV_ROUND_UP(const_state->immediates_count, 4);
316
317 /* truncate size to avoid writing constants that shader
318 * does not use:
319 */
320 size = MIN2(size + base, v->constlen) - base;
321
322 /* convert out of vec4: */
323 base *= 4;
324 size *= 4;
325
326 if (size > 0)
327 emit_const_user(ring, v, base, size, const_state->immediates);
328
329 /* NIR constant data has the same lifetime as immediates, so upload it
330 * now, too.
331 */
332 ir3_emit_constant_data(v, ring);
333 }
334
335 static inline void
ir3_emit_link_map(const struct ir3_shader_variant * producer,const struct ir3_shader_variant * consumer,struct fd_ringbuffer * ring)336 ir3_emit_link_map(const struct ir3_shader_variant *producer,
337 const struct ir3_shader_variant *consumer,
338 struct fd_ringbuffer *ring)
339 {
340 const struct ir3_const_state *const_state = ir3_const_state(consumer);
341 uint32_t base = const_state->offsets.primitive_map;
342 int size = DIV_ROUND_UP(consumer->input_size, 4);
343
344 /* truncate size to avoid writing constants that shader
345 * does not use:
346 */
347 size = MIN2(size + base, consumer->constlen) - base;
348
349 /* convert out of vec4: */
350 base *= 4;
351 size *= 4;
352
353 if (size > 0)
354 emit_const_user(ring, consumer, base, size, producer->output_loc);
355 }
356
357 /* emit stream-out buffers: */
358 static inline void
emit_tfbos(struct fd_context * ctx,const struct ir3_shader_variant * v,struct fd_ringbuffer * ring)359 emit_tfbos(struct fd_context *ctx, const struct ir3_shader_variant *v,
360 struct fd_ringbuffer *ring)
361 {
362 /* streamout addresses after driver-params: */
363 const struct ir3_const_state *const_state = ir3_const_state(v);
364 uint32_t offset = const_state->offsets.tfbo;
365 if (v->constlen > offset) {
366 struct fd_streamout_stateobj *so = &ctx->streamout;
367 const struct ir3_stream_output_info *info = &v->stream_output;
368 uint32_t params = 4;
369 uint32_t offsets[params];
370 struct fd_bo *bos[params];
371
372 for (uint32_t i = 0; i < params; i++) {
373 struct pipe_stream_output_target *target = so->targets[i];
374
375 if (target) {
376 offsets[i] =
377 (so->offsets[i] * info->stride[i] * 4) + target->buffer_offset;
378 bos[i] = fd_resource(target->buffer)->bo;
379 } else {
380 offsets[i] = 0;
381 bos[i] = NULL;
382 }
383 }
384
385 assert(offset * 4 + params <= v->constlen * 4);
386
387 emit_const_ptrs(ring, v, offset * 4, params, bos, offsets);
388 }
389 }
390
391 static inline void
emit_common_consts(const struct ir3_shader_variant * v,struct fd_ringbuffer * ring,struct fd_context * ctx,enum pipe_shader_type t)392 emit_common_consts(const struct ir3_shader_variant *v,
393 struct fd_ringbuffer *ring, struct fd_context *ctx,
394 enum pipe_shader_type t) assert_dt
395 {
396 enum fd_dirty_shader_state dirty = ctx->dirty_shader[t];
397
398 /* When we use CP_SET_DRAW_STATE objects to emit constant state,
399 * if we emit any of it we need to emit all. This is because
400 * we are using the same state-group-id each time for uniform
401 * state, and if previous update is never evaluated (due to no
402 * visible primitives in the current tile) then the new stateobj
403 * completely replaces the old one.
404 *
405 * Possibly if we split up different parts of the const state to
406 * different state-objects we could avoid this.
407 */
408 if (dirty && is_stateobj(ring))
409 dirty = (enum fd_dirty_shader_state)~0;
410
411 if (dirty & (FD_DIRTY_SHADER_PROG | FD_DIRTY_SHADER_CONST)) {
412 struct fd_constbuf_stateobj *constbuf;
413 bool shader_dirty;
414
415 constbuf = &ctx->constbuf[t];
416 shader_dirty = !!(dirty & FD_DIRTY_SHADER_PROG);
417
418 ring_wfi(ctx->batch, ring);
419
420 ir3_emit_user_consts(v, ring, constbuf);
421 ir3_emit_ubos(ctx, v, ring, constbuf);
422 if (shader_dirty)
423 ir3_emit_immediates(v, ring);
424 }
425
426 if (dirty & (FD_DIRTY_SHADER_PROG | FD_DIRTY_SHADER_IMAGE)) {
427 struct fd_shaderimg_stateobj *si = &ctx->shaderimg[t];
428 ring_wfi(ctx->batch, ring);
429 ir3_emit_image_dims(ctx->screen, v, ring, si);
430 }
431 }
432
433 /* emit kernel params */
434 static inline void
emit_kernel_params(struct fd_context * ctx,const struct ir3_shader_variant * v,struct fd_ringbuffer * ring,const struct pipe_grid_info * info)435 emit_kernel_params(struct fd_context *ctx, const struct ir3_shader_variant *v,
436 struct fd_ringbuffer *ring, const struct pipe_grid_info *info)
437 assert_dt
438 {
439 const struct ir3_const_state *const_state = ir3_const_state(v);
440 uint32_t offset = const_state->offsets.kernel_params;
441 if (v->constlen > offset) {
442 ring_wfi(ctx->batch, ring);
443 emit_const_user(ring, v, offset * 4,
444 align(v->cs.req_input_mem, 4),
445 (uint32_t *)info->input);
446 }
447 }
448
449 static inline void
ir3_emit_driver_params(const struct ir3_shader_variant * v,struct fd_ringbuffer * ring,struct fd_context * ctx,const struct pipe_draw_info * info,const struct pipe_draw_indirect_info * indirect,const struct pipe_draw_start_count_bias * draw,const uint32_t draw_id)450 ir3_emit_driver_params(const struct ir3_shader_variant *v,
451 struct fd_ringbuffer *ring, struct fd_context *ctx,
452 const struct pipe_draw_info *info,
453 const struct pipe_draw_indirect_info *indirect,
454 const struct pipe_draw_start_count_bias *draw,
455 const uint32_t draw_id) assert_dt
456 {
457 assert(v->need_driver_params);
458
459 const struct ir3_const_state *const_state = ir3_const_state(v);
460 uint32_t offset = const_state->offsets.driver_param;
461 uint32_t vertex_params[IR3_DP_VS_COUNT] = {
462 [IR3_DP_DRAWID] = draw_id, /* filled by hw (CP_DRAW_INDIRECT_MULTI) */
463 [IR3_DP_VTXID_BASE] = info->index_size ? draw->index_bias : draw->start,
464 [IR3_DP_INSTID_BASE] = info->start_instance,
465 [IR3_DP_VTXCNT_MAX] = ctx->streamout.max_tf_vtx,
466 [IR3_DP_IS_INDEXED_DRAW] = info->index_size != 0 ? ~0 : 0,
467 };
468 if (v->key.ucp_enables) {
469 struct pipe_clip_state *ucp = &ctx->ucp;
470 unsigned pos = IR3_DP_UCP0_X;
471 for (unsigned i = 0; pos <= IR3_DP_UCP7_W; i++) {
472 for (unsigned j = 0; j < 4; j++) {
473 vertex_params[pos] = fui(ucp->ucp[i][j]);
474 pos++;
475 }
476 }
477 }
478
479 /* Only emit as many params as needed, i.e. up to the highest enabled UCP
480 * plane. However a binning pass may drop even some of these, so limit to
481 * program max.
482 */
483 const uint32_t vertex_params_size =
484 MIN2(const_state->num_driver_params, (v->constlen - offset) * 4);
485 assert(vertex_params_size <= IR3_DP_VS_COUNT);
486
487 bool needs_vtxid_base =
488 ir3_find_sysval_regid(v, SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) !=
489 regid(63, 0);
490
491 /* for indirect draw, we need to copy VTXID_BASE from
492 * indirect-draw parameters buffer.. which is annoying
493 * and means we can't easily emit these consts in cmd
494 * stream so need to copy them to bo.
495 */
496 if (indirect && needs_vtxid_base) {
497 uint32_t vertex_params_area = align(vertex_params_size, 16);
498 struct pipe_resource *vertex_params_rsc =
499 pipe_buffer_create(&ctx->screen->base, PIPE_BIND_CONSTANT_BUFFER,
500 PIPE_USAGE_STREAM, vertex_params_area * 4);
501 unsigned src_off = indirect->offset;
502 void *ptr;
503
504 ptr = fd_bo_map(fd_resource(vertex_params_rsc)->bo);
505 memcpy(ptr, vertex_params, vertex_params_size * 4);
506
507 if (info->index_size) {
508 /* indexed draw, index_bias is 4th field: */
509 src_off += 3 * 4;
510 } else {
511 /* non-indexed draw, start is 3rd field: */
512 src_off += 2 * 4;
513 }
514
515 /* copy index_bias or start from draw params: */
516 ctx->screen->mem_to_mem(ring, vertex_params_rsc, 0, indirect->buffer,
517 src_off, 1);
518
519 emit_const_prsc(ring, v, offset * 4, 0, vertex_params_area,
520 vertex_params_rsc);
521
522 pipe_resource_reference(&vertex_params_rsc, NULL);
523 } else {
524 emit_const_user(ring, v, offset * 4, vertex_params_size, vertex_params);
525 }
526
527 /* if needed, emit stream-out buffer addresses: */
528 if (vertex_params[IR3_DP_VTXCNT_MAX] > 0) {
529 emit_tfbos(ctx, v, ring);
530 }
531 }
532
533
534 static inline void
ir3_emit_hs_driver_params(const struct ir3_shader_variant * v,struct fd_ringbuffer * ring,struct fd_context * ctx)535 ir3_emit_hs_driver_params(const struct ir3_shader_variant *v,
536 struct fd_ringbuffer *ring,
537 struct fd_context *ctx)
538 assert_dt
539 {
540 assert(v->need_driver_params);
541
542 const struct ir3_const_state *const_state = ir3_const_state(v);
543 uint32_t offset = const_state->offsets.driver_param;
544 uint32_t hs_params[IR3_DP_HS_COUNT] = {
545 [IR3_DP_HS_DEFAULT_OUTER_LEVEL_X] = fui(ctx->default_outer_level[0]),
546 [IR3_DP_HS_DEFAULT_OUTER_LEVEL_Y] = fui(ctx->default_outer_level[1]),
547 [IR3_DP_HS_DEFAULT_OUTER_LEVEL_Z] = fui(ctx->default_outer_level[2]),
548 [IR3_DP_HS_DEFAULT_OUTER_LEVEL_W] = fui(ctx->default_outer_level[3]),
549 [IR3_DP_HS_DEFAULT_INNER_LEVEL_X] = fui(ctx->default_inner_level[0]),
550 [IR3_DP_HS_DEFAULT_INNER_LEVEL_Y] = fui(ctx->default_inner_level[1]),
551 };
552
553 const uint32_t hs_params_size =
554 MIN2(const_state->num_driver_params, (v->constlen - offset) * 4);
555 assert(hs_params_size <= IR3_DP_HS_COUNT);
556
557 emit_const_user(ring, v, offset * 4, hs_params_size, hs_params);
558 }
559
560
561 static inline void
ir3_emit_vs_consts(const struct ir3_shader_variant * v,struct fd_ringbuffer * ring,struct fd_context * ctx,const struct pipe_draw_info * info,const struct pipe_draw_indirect_info * indirect,const struct pipe_draw_start_count_bias * draw)562 ir3_emit_vs_consts(const struct ir3_shader_variant *v,
563 struct fd_ringbuffer *ring, struct fd_context *ctx,
564 const struct pipe_draw_info *info,
565 const struct pipe_draw_indirect_info *indirect,
566 const struct pipe_draw_start_count_bias *draw) assert_dt
567 {
568 assert(v->type == MESA_SHADER_VERTEX);
569
570 emit_common_consts(v, ring, ctx, PIPE_SHADER_VERTEX);
571
572 /* emit driver params every time: */
573 if (info && v->need_driver_params) {
574 ring_wfi(ctx->batch, ring);
575 ir3_emit_driver_params(v, ring, ctx, info, indirect, draw, 0);
576 }
577 }
578
579 static inline void
ir3_emit_fs_consts(const struct ir3_shader_variant * v,struct fd_ringbuffer * ring,struct fd_context * ctx)580 ir3_emit_fs_consts(const struct ir3_shader_variant *v,
581 struct fd_ringbuffer *ring, struct fd_context *ctx) assert_dt
582 {
583 assert(v->type == MESA_SHADER_FRAGMENT);
584
585 emit_common_consts(v, ring, ctx, PIPE_SHADER_FRAGMENT);
586 }
587
588 static inline void
ir3_emit_cs_driver_params(const struct ir3_shader_variant * v,struct fd_ringbuffer * ring,struct fd_context * ctx,const struct pipe_grid_info * info)589 ir3_emit_cs_driver_params(const struct ir3_shader_variant *v,
590 struct fd_ringbuffer *ring, struct fd_context *ctx,
591 const struct pipe_grid_info *info)
592 assert_dt
593 {
594 emit_kernel_params(ctx, v, ring, info);
595
596 /* a3xx/a4xx can inject these directly */
597 if (ctx->screen->gen <= 4)
598 return;
599
600 /* emit compute-shader driver-params: */
601 const struct ir3_const_state *const_state = ir3_const_state(v);
602 uint32_t offset = const_state->offsets.driver_param;
603 if (v->constlen > offset) {
604 ring_wfi(ctx->batch, ring);
605
606 if (info->indirect) {
607 struct pipe_resource *indirect = NULL;
608 unsigned indirect_offset;
609
610 /* This is a bit awkward, but CP_LOAD_STATE.EXT_SRC_ADDR needs
611 * to be aligned more strongly than 4 bytes. So in this case
612 * we need a temporary buffer to copy NumWorkGroups.xyz to.
613 *
614 * TODO if previous compute job is writing to info->indirect,
615 * we might need a WFI.. but since we currently flush for each
616 * compute job, we are probably ok for now.
617 */
618 if (info->indirect_offset & 0xf) {
619 indirect = pipe_buffer_create(&ctx->screen->base,
620 PIPE_BIND_COMMAND_ARGS_BUFFER,
621 PIPE_USAGE_STREAM, 0x1000);
622 indirect_offset = 0;
623
624 ctx->screen->mem_to_mem(ring, indirect, 0, info->indirect,
625 info->indirect_offset, 3);
626 } else {
627 pipe_resource_reference(&indirect, info->indirect);
628 indirect_offset = info->indirect_offset;
629 }
630
631 emit_const_prsc(ring, v, offset * 4, indirect_offset, 16, indirect);
632
633 pipe_resource_reference(&indirect, NULL);
634 } else {
635 // TODO some of these are not part of the indirect state.. so we
636 // need to emit some of this directly in both cases.
637 uint32_t compute_params[IR3_DP_CS_COUNT] = {
638 [IR3_DP_NUM_WORK_GROUPS_X] = info->grid[0],
639 [IR3_DP_NUM_WORK_GROUPS_Y] = info->grid[1],
640 [IR3_DP_NUM_WORK_GROUPS_Z] = info->grid[2],
641 [IR3_DP_WORK_DIM] = info->work_dim,
642 [IR3_DP_BASE_GROUP_X] = info->grid_base[0],
643 [IR3_DP_BASE_GROUP_Y] = info->grid_base[1],
644 [IR3_DP_BASE_GROUP_Z] = info->grid_base[2],
645 [IR3_DP_CS_SUBGROUP_SIZE] = v->info.subgroup_size,
646 [IR3_DP_LOCAL_GROUP_SIZE_X] = info->block[0],
647 [IR3_DP_LOCAL_GROUP_SIZE_Y] = info->block[1],
648 [IR3_DP_LOCAL_GROUP_SIZE_Z] = info->block[2],
649 [IR3_DP_SUBGROUP_ID_SHIFT] = util_logbase2(v->info.subgroup_size),
650 [IR3_DP_WORKGROUP_ID_X] = 0, // TODO
651 [IR3_DP_WORKGROUP_ID_Y] = 0, // TODO
652 [IR3_DP_WORKGROUP_ID_Z] = 0, // TODO
653 };
654 uint32_t size =
655 MIN2(const_state->num_driver_params, v->constlen * 4 - offset * 4);
656
657 emit_const_user(ring, v, offset * 4, size, compute_params);
658 }
659 }
660 }
661
662 /* emit compute-shader consts: */
663 static inline void
ir3_emit_cs_consts(const struct ir3_shader_variant * v,struct fd_ringbuffer * ring,struct fd_context * ctx,const struct pipe_grid_info * info)664 ir3_emit_cs_consts(const struct ir3_shader_variant *v,
665 struct fd_ringbuffer *ring, struct fd_context *ctx,
666 const struct pipe_grid_info *info) assert_dt
667 {
668 assert(gl_shader_stage_is_compute(v->type));
669
670 emit_common_consts(v, ring, ctx, PIPE_SHADER_COMPUTE);
671
672 ir3_emit_cs_driver_params(v, ring, ctx, info);
673 }
674