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