freedreno/ir3: Stop shifting UBO 1 down to be UBO 0.
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3_const.h
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(struct fd_ringbuffer *ring,
43 const struct ir3_shader_variant *v, uint32_t dst_offset,
44 uint32_t offset, uint32_t size,
45 const void *user_buffer, struct pipe_resource *buffer);
46
47 static void emit_const_bo(struct fd_ringbuffer *ring,
48 const struct ir3_shader_variant *v, uint32_t dst_offset,
49 uint32_t num, struct pipe_resource **prscs, uint32_t *offsets);
50
51
52 static void
53 ring_wfi(struct fd_batch *batch, struct fd_ringbuffer *ring)
54 {
55 /* when we emit const state via ring (IB2) we need a WFI, but when
56 * it is emit'd via stateobj, we don't
57 */
58 if (is_stateobj(ring))
59 return;
60
61 fd_wfi(batch, ring);
62 }
63
64 /**
65 * Indirectly calculates size of cmdstream needed for ir3_emit_user_consts().
66 * Returns number of packets, and total size of all the payload.
67 *
68 * The value can be a worst-case, ie. some shader variants may not read all
69 * consts, etc.
70 *
71 * Returns size in dwords.
72 */
73 static inline void
74 ir3_user_consts_size(struct ir3_ubo_analysis_state *state,
75 unsigned *packets, unsigned *size)
76 {
77 *packets = *size = 0;
78
79 for (uint32_t i = 0; i < ARRAY_SIZE(state->range); i++) {
80 if (state->range[i].start < state->range[i].end) {
81 *size += state->range[i].end - state->range[i].start;
82 (*packets)++;
83 }
84 }
85 }
86
87 /**
88 * Uploads sub-ranges of UBOs to the hardware's constant buffer (UBO access
89 * outside of these ranges will be done using full UBO accesses in the
90 * shader).
91 */
92 static inline void
93 ir3_emit_user_consts(struct fd_screen *screen, const struct ir3_shader_variant *v,
94 struct fd_ringbuffer *ring, struct fd_constbuf_stateobj *constbuf)
95 {
96 struct ir3_ubo_analysis_state *state;
97 state = &v->shader->ubo_state;
98
99 for (unsigned i = 0; i < state->num_enabled; i++) {
100 assert(!state->range[i].bindless);
101 unsigned ubo = state->range[i].block;
102 if (!(constbuf->enabled_mask & (1 << ubo)))
103 continue;
104 struct pipe_constant_buffer *cb = &constbuf->cb[ubo];
105
106 uint32_t size = state->range[i].end - state->range[i].start;
107 uint32_t offset = cb->buffer_offset + state->range[i].start;
108
109 /* Pre-a6xx, we might have ranges enabled in the shader that aren't
110 * used in the binning variant.
111 */
112 if (16 * v->constlen <= state->range[i].offset)
113 continue;
114
115 /* and even if the start of the const buffer is before
116 * first_immediate, the end may not be:
117 */
118 size = MIN2(size, (16 * v->constlen) - state->range[i].offset);
119
120 if (size == 0)
121 continue;
122
123 /* things should be aligned to vec4: */
124 debug_assert((state->range[i].offset % 16) == 0);
125 debug_assert((size % 16) == 0);
126 debug_assert((offset % 16) == 0);
127
128 emit_const(ring, v, state->range[i].offset / 4,
129 offset, size / 4, cb->user_buffer, cb->buffer);
130 }
131 }
132
133 static inline void
134 ir3_emit_ubos(struct fd_context *ctx, const struct ir3_shader_variant *v,
135 struct fd_ringbuffer *ring, struct fd_constbuf_stateobj *constbuf)
136 {
137 const struct ir3_const_state *const_state = &v->shader->const_state;
138 uint32_t offset = const_state->offsets.ubo;
139 if (v->constlen > offset) {
140 uint32_t params = const_state->num_ubos;
141 uint32_t offsets[params];
142 struct pipe_resource *prscs[params];
143
144 for (uint32_t i = 0; i < params; i++) {
145 struct pipe_constant_buffer *cb = &constbuf->cb[i];
146
147 /* If we have user pointers (constbuf 0, aka GL uniforms), upload
148 * them to a buffer now, and save it in the constbuf so that we
149 * don't have to reupload until they get changed.
150 */
151 if (cb->user_buffer) {
152 struct pipe_context *pctx = &ctx->base;
153 u_upload_data(pctx->stream_uploader, 0,
154 cb->buffer_size,
155 64,
156 cb->user_buffer,
157 &cb->buffer_offset, &cb->buffer);
158 cb->user_buffer = NULL;
159 }
160
161 if ((constbuf->enabled_mask & (1 << i)) && cb->buffer) {
162 offsets[i] = cb->buffer_offset;
163 prscs[i] = cb->buffer;
164 } else {
165 offsets[i] = 0;
166 prscs[i] = NULL;
167 }
168 }
169
170 assert(offset * 4 + params <= v->constlen * 4);
171
172 emit_const_bo(ring, v, offset * 4, params, prscs, offsets);
173 }
174 }
175
176 static inline void
177 ir3_emit_ssbo_sizes(struct fd_screen *screen, const struct ir3_shader_variant *v,
178 struct fd_ringbuffer *ring, struct fd_shaderbuf_stateobj *sb)
179 {
180 const struct ir3_const_state *const_state = &v->shader->const_state;
181 uint32_t offset = const_state->offsets.ssbo_sizes;
182 if (v->constlen > offset) {
183 uint32_t sizes[align(const_state->ssbo_size.count, 4)];
184 unsigned mask = const_state->ssbo_size.mask;
185
186 while (mask) {
187 unsigned index = u_bit_scan(&mask);
188 unsigned off = const_state->ssbo_size.off[index];
189 sizes[off] = sb->sb[index].buffer_size;
190 }
191
192 emit_const(ring, v, offset * 4, 0, ARRAY_SIZE(sizes), sizes, NULL);
193 }
194 }
195
196 static inline void
197 ir3_emit_image_dims(struct fd_screen *screen, const struct ir3_shader_variant *v,
198 struct fd_ringbuffer *ring, struct fd_shaderimg_stateobj *si)
199 {
200 const struct ir3_const_state *const_state = &v->shader->const_state;
201 uint32_t offset = const_state->offsets.image_dims;
202 if (v->constlen > offset) {
203 uint32_t dims[align(const_state->image_dims.count, 4)];
204 unsigned mask = const_state->image_dims.mask;
205
206 while (mask) {
207 struct pipe_image_view *img;
208 struct fd_resource *rsc;
209 unsigned index = u_bit_scan(&mask);
210 unsigned off = const_state->image_dims.off[index];
211
212 img = &si->si[index];
213 rsc = fd_resource(img->resource);
214
215 dims[off + 0] = util_format_get_blocksize(img->format);
216 if (img->resource->target != PIPE_BUFFER) {
217 struct fdl_slice *slice =
218 fd_resource_slice(rsc, img->u.tex.level);
219 /* note for 2d/cube/etc images, even if re-interpreted
220 * as a different color format, the pixel size should
221 * be the same, so use original dimensions for y and z
222 * stride:
223 */
224 dims[off + 1] = slice->pitch;
225 /* see corresponding logic in fd_resource_offset(): */
226 if (rsc->layout.layer_first) {
227 dims[off + 2] = rsc->layout.layer_size;
228 } else {
229 dims[off + 2] = slice->size0;
230 }
231 } else {
232 /* For buffer-backed images, the log2 of the format's
233 * bytes-per-pixel is placed on the 2nd slot. This is useful
234 * when emitting image_size instructions, for which we need
235 * to divide by bpp for image buffers. Since the bpp
236 * can only be power-of-two, the division is implemented
237 * as a SHR, and for that it is handy to have the log2 of
238 * bpp as a constant. (log2 = first-set-bit - 1)
239 */
240 dims[off + 1] = ffs(dims[off + 0]) - 1;
241 }
242 }
243 uint32_t size = MIN2(ARRAY_SIZE(dims), v->constlen * 4 - offset * 4);
244
245 emit_const(ring, v, offset * 4, 0, size, dims, NULL);
246 }
247 }
248
249 static inline void
250 ir3_emit_immediates(struct fd_screen *screen, const struct ir3_shader_variant *v,
251 struct fd_ringbuffer *ring)
252 {
253 const struct ir3_const_state *const_state = &v->shader->const_state;
254 uint32_t base = const_state->offsets.immediate;
255 int size = const_state->immediates_count;
256
257 /* truncate size to avoid writing constants that shader
258 * does not use:
259 */
260 size = MIN2(size + base, v->constlen) - base;
261
262 /* convert out of vec4: */
263 base *= 4;
264 size *= 4;
265
266 if (size > 0)
267 emit_const(ring, v, base, 0, size, const_state->immediates[0].val, NULL);
268 }
269
270 static inline void
271 ir3_emit_link_map(struct fd_screen *screen,
272 const struct ir3_shader_variant *producer,
273 const struct ir3_shader_variant *v, struct fd_ringbuffer *ring)
274 {
275 const struct ir3_const_state *const_state = &v->shader->const_state;
276 uint32_t base = const_state->offsets.primitive_map;
277 uint32_t patch_locs[MAX_VARYING] = { }, num_loc;
278
279 num_loc = ir3_link_geometry_stages(producer, v, patch_locs);
280
281 int size = DIV_ROUND_UP(num_loc, 4);
282
283 /* truncate size to avoid writing constants that shader
284 * does not use:
285 */
286 size = MIN2(size + base, v->constlen) - base;
287
288 /* convert out of vec4: */
289 base *= 4;
290 size *= 4;
291
292 if (size > 0)
293 emit_const(ring, v, base, 0, size, patch_locs, NULL);
294 }
295
296 /* emit stream-out buffers: */
297 static inline void
298 emit_tfbos(struct fd_context *ctx, const struct ir3_shader_variant *v,
299 struct fd_ringbuffer *ring)
300 {
301 /* streamout addresses after driver-params: */
302 const struct ir3_const_state *const_state = &v->shader->const_state;
303 uint32_t offset = const_state->offsets.tfbo;
304 if (v->constlen > offset) {
305 struct fd_streamout_stateobj *so = &ctx->streamout;
306 struct ir3_stream_output_info *info = &v->shader->stream_output;
307 uint32_t params = 4;
308 uint32_t offsets[params];
309 struct pipe_resource *prscs[params];
310
311 for (uint32_t i = 0; i < params; i++) {
312 struct pipe_stream_output_target *target = so->targets[i];
313
314 if (target) {
315 offsets[i] = (so->offsets[i] * info->stride[i] * 4) +
316 target->buffer_offset;
317 prscs[i] = target->buffer;
318 } else {
319 offsets[i] = 0;
320 prscs[i] = NULL;
321 }
322 }
323
324 assert(offset * 4 + params <= v->constlen * 4);
325
326 emit_const_bo(ring, v, offset * 4, params, prscs, offsets);
327 }
328 }
329
330 static inline uint32_t
331 max_tf_vtx(struct fd_context *ctx, const struct ir3_shader_variant *v)
332 {
333 struct fd_streamout_stateobj *so = &ctx->streamout;
334 struct ir3_stream_output_info *info = &v->shader->stream_output;
335 uint32_t maxvtxcnt = 0x7fffffff;
336
337 if (ctx->screen->gpu_id >= 500)
338 return 0;
339 if (v->binning_pass)
340 return 0;
341 if (v->shader->stream_output.num_outputs == 0)
342 return 0;
343 if (so->num_targets == 0)
344 return 0;
345
346 /* offset to write to is:
347 *
348 * total_vtxcnt = vtxcnt + offsets[i]
349 * offset = total_vtxcnt * stride[i]
350 *
351 * offset = vtxcnt * stride[i] ; calculated in shader
352 * + offsets[i] * stride[i] ; calculated at emit_tfbos()
353 *
354 * assuming for each vtx, each target buffer will have data written
355 * up to 'offset + stride[i]', that leaves maxvtxcnt as:
356 *
357 * buffer_size = (maxvtxcnt * stride[i]) + stride[i]
358 * maxvtxcnt = (buffer_size - stride[i]) / stride[i]
359 *
360 * but shader is actually doing a less-than (rather than less-than-
361 * equal) check, so we can drop the -stride[i].
362 *
363 * TODO is assumption about `offset + stride[i]` legit?
364 */
365 for (unsigned i = 0; i < so->num_targets; i++) {
366 struct pipe_stream_output_target *target = so->targets[i];
367 unsigned stride = info->stride[i] * 4; /* convert dwords->bytes */
368 if (target) {
369 uint32_t max = target->buffer_size / stride;
370 maxvtxcnt = MIN2(maxvtxcnt, max);
371 }
372 }
373
374 return maxvtxcnt;
375 }
376
377 static inline void
378 emit_common_consts(const struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
379 struct fd_context *ctx, enum pipe_shader_type t)
380 {
381 enum fd_dirty_shader_state dirty = ctx->dirty_shader[t];
382
383 /* When we use CP_SET_DRAW_STATE objects to emit constant state,
384 * if we emit any of it we need to emit all. This is because
385 * we are using the same state-group-id each time for uniform
386 * state, and if previous update is never evaluated (due to no
387 * visible primitives in the current tile) then the new stateobj
388 * completely replaces the old one.
389 *
390 * Possibly if we split up different parts of the const state to
391 * different state-objects we could avoid this.
392 */
393 if (dirty && is_stateobj(ring))
394 dirty = ~0;
395
396 if (dirty & (FD_DIRTY_SHADER_PROG | FD_DIRTY_SHADER_CONST)) {
397 struct fd_constbuf_stateobj *constbuf;
398 bool shader_dirty;
399
400 constbuf = &ctx->constbuf[t];
401 shader_dirty = !!(dirty & FD_DIRTY_SHADER_PROG);
402
403 ring_wfi(ctx->batch, ring);
404
405 ir3_emit_user_consts(ctx->screen, v, ring, constbuf);
406 ir3_emit_ubos(ctx, v, ring, constbuf);
407 if (shader_dirty)
408 ir3_emit_immediates(ctx->screen, v, ring);
409 }
410
411 if (dirty & (FD_DIRTY_SHADER_PROG | FD_DIRTY_SHADER_SSBO)) {
412 struct fd_shaderbuf_stateobj *sb = &ctx->shaderbuf[t];
413 ring_wfi(ctx->batch, ring);
414 ir3_emit_ssbo_sizes(ctx->screen, v, ring, sb);
415 }
416
417 if (dirty & (FD_DIRTY_SHADER_PROG | FD_DIRTY_SHADER_IMAGE)) {
418 struct fd_shaderimg_stateobj *si = &ctx->shaderimg[t];
419 ring_wfi(ctx->batch, ring);
420 ir3_emit_image_dims(ctx->screen, v, ring, si);
421 }
422 }
423
424 static inline bool
425 ir3_needs_vs_driver_params(const struct ir3_shader_variant *v)
426 {
427 const struct ir3_const_state *const_state = &v->shader->const_state;
428 uint32_t offset = const_state->offsets.driver_param;
429
430 return v->constlen > offset;
431 }
432
433 static inline void
434 ir3_emit_vs_driver_params(const struct ir3_shader_variant *v,
435 struct fd_ringbuffer *ring, struct fd_context *ctx,
436 const struct pipe_draw_info *info)
437 {
438 debug_assert(ir3_needs_vs_driver_params(v));
439
440 const struct ir3_const_state *const_state = &v->shader->const_state;
441 uint32_t offset = const_state->offsets.driver_param;
442 uint32_t vertex_params[IR3_DP_VS_COUNT] = {
443 [IR3_DP_VTXID_BASE] = info->index_size ?
444 info->index_bias : info->start,
445 [IR3_DP_VTXCNT_MAX] = max_tf_vtx(ctx, v),
446 };
447 /* if no user-clip-planes, we don't need to emit the
448 * entire thing:
449 */
450 uint32_t vertex_params_size = 4;
451
452 if (v->key.ucp_enables) {
453 struct pipe_clip_state *ucp = &ctx->ucp;
454 unsigned pos = IR3_DP_UCP0_X;
455 for (unsigned i = 0; pos <= IR3_DP_UCP7_W; i++) {
456 for (unsigned j = 0; j < 4; j++) {
457 vertex_params[pos] = fui(ucp->ucp[i][j]);
458 pos++;
459 }
460 }
461 vertex_params_size = ARRAY_SIZE(vertex_params);
462 }
463
464 vertex_params_size = MAX2(vertex_params_size, const_state->num_driver_params);
465
466 bool needs_vtxid_base =
467 ir3_find_sysval_regid(v, SYSTEM_VALUE_VERTEX_ID_ZERO_BASE) != regid(63, 0);
468
469 /* for indirect draw, we need to copy VTXID_BASE from
470 * indirect-draw parameters buffer.. which is annoying
471 * and means we can't easily emit these consts in cmd
472 * stream so need to copy them to bo.
473 */
474 if (info->indirect && needs_vtxid_base) {
475 struct pipe_draw_indirect_info *indirect = info->indirect;
476 struct pipe_resource *vertex_params_rsc =
477 pipe_buffer_create(&ctx->screen->base,
478 PIPE_BIND_CONSTANT_BUFFER, PIPE_USAGE_STREAM,
479 vertex_params_size * 4);
480 unsigned src_off = info->indirect->offset;;
481 void *ptr;
482
483 ptr = fd_bo_map(fd_resource(vertex_params_rsc)->bo);
484 memcpy(ptr, vertex_params, vertex_params_size * 4);
485
486 if (info->index_size) {
487 /* indexed draw, index_bias is 4th field: */
488 src_off += 3 * 4;
489 } else {
490 /* non-indexed draw, start is 3rd field: */
491 src_off += 2 * 4;
492 }
493
494 /* copy index_bias or start from draw params: */
495 ctx->screen->mem_to_mem(ring, vertex_params_rsc, 0,
496 indirect->buffer, src_off, 1);
497
498 emit_const(ring, v, offset * 4, 0,
499 vertex_params_size, NULL, vertex_params_rsc);
500
501 pipe_resource_reference(&vertex_params_rsc, NULL);
502 } else {
503 emit_const(ring, v, offset * 4, 0,
504 vertex_params_size, vertex_params, NULL);
505 }
506
507 /* if needed, emit stream-out buffer addresses: */
508 if (vertex_params[IR3_DP_VTXCNT_MAX] > 0) {
509 emit_tfbos(ctx, v, ring);
510 }
511 }
512
513 static inline void
514 ir3_emit_vs_consts(const struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
515 struct fd_context *ctx, const struct pipe_draw_info *info)
516 {
517 debug_assert(v->type == MESA_SHADER_VERTEX);
518
519 emit_common_consts(v, ring, ctx, PIPE_SHADER_VERTEX);
520
521 /* emit driver params every time: */
522 if (info && ir3_needs_vs_driver_params(v)) {
523 ring_wfi(ctx->batch, ring);
524 ir3_emit_vs_driver_params(v, ring, ctx, info);
525 }
526 }
527
528 static inline void
529 ir3_emit_fs_consts(const struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
530 struct fd_context *ctx)
531 {
532 debug_assert(v->type == MESA_SHADER_FRAGMENT);
533
534 emit_common_consts(v, ring, ctx, PIPE_SHADER_FRAGMENT);
535 }
536
537 /* emit compute-shader consts: */
538 static inline void
539 ir3_emit_cs_consts(const struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
540 struct fd_context *ctx, const struct pipe_grid_info *info)
541 {
542 debug_assert(gl_shader_stage_is_compute(v->type));
543
544 emit_common_consts(v, ring, ctx, PIPE_SHADER_COMPUTE);
545
546 /* emit compute-shader driver-params: */
547 const struct ir3_const_state *const_state = &v->shader->const_state;
548 uint32_t offset = const_state->offsets.driver_param;
549 if (v->constlen > offset) {
550 ring_wfi(ctx->batch, ring);
551
552 if (info->indirect) {
553 struct pipe_resource *indirect = NULL;
554 unsigned indirect_offset;
555
556 /* This is a bit awkward, but CP_LOAD_STATE.EXT_SRC_ADDR needs
557 * to be aligned more strongly than 4 bytes. So in this case
558 * we need a temporary buffer to copy NumWorkGroups.xyz to.
559 *
560 * TODO if previous compute job is writing to info->indirect,
561 * we might need a WFI.. but since we currently flush for each
562 * compute job, we are probably ok for now.
563 */
564 if (info->indirect_offset & 0xf) {
565 indirect = pipe_buffer_create(&ctx->screen->base,
566 PIPE_BIND_COMMAND_ARGS_BUFFER, PIPE_USAGE_STREAM,
567 0x1000);
568 indirect_offset = 0;
569
570 ctx->screen->mem_to_mem(ring, indirect, 0, info->indirect,
571 info->indirect_offset, 3);
572 } else {
573 pipe_resource_reference(&indirect, info->indirect);
574 indirect_offset = info->indirect_offset;
575 }
576
577 emit_const(ring, v, offset * 4, indirect_offset, 4, NULL, indirect);
578
579 pipe_resource_reference(&indirect, NULL);
580 } else {
581 uint32_t compute_params[IR3_DP_CS_COUNT] = {
582 [IR3_DP_NUM_WORK_GROUPS_X] = info->grid[0],
583 [IR3_DP_NUM_WORK_GROUPS_Y] = info->grid[1],
584 [IR3_DP_NUM_WORK_GROUPS_Z] = info->grid[2],
585 [IR3_DP_LOCAL_GROUP_SIZE_X] = info->block[0],
586 [IR3_DP_LOCAL_GROUP_SIZE_Y] = info->block[1],
587 [IR3_DP_LOCAL_GROUP_SIZE_Z] = info->block[2],
588 };
589 uint32_t size = MIN2(const_state->num_driver_params,
590 v->constlen * 4 - offset * 4);
591
592 emit_const(ring, v, offset * 4, 0, size, compute_params, NULL);
593 }
594 }
595 }