radeonsi: import r600_streamout from drivers/radeon
[mesa.git] / src / gallium / drivers / radeonsi / si_descriptors.c
1 /*
2 * Copyright 2013 Advanced Micro Devices, Inc.
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Marek Olšák <marek.olsak@amd.com>
25 */
26
27 /* Resource binding slots and sampler states (each described with 8 or
28 * 4 dwords) are stored in lists in memory which is accessed by shaders
29 * using scalar load instructions.
30 *
31 * This file is responsible for managing such lists. It keeps a copy of all
32 * descriptors in CPU memory and re-uploads a whole list if some slots have
33 * been changed.
34 *
35 * This code is also reponsible for updating shader pointers to those lists.
36 *
37 * Note that CP DMA can't be used for updating the lists, because a GPU hang
38 * could leave the list in a mid-IB state and the next IB would get wrong
39 * descriptors and the whole context would be unusable at that point.
40 * (Note: The register shadowing can't be used due to the same reason)
41 *
42 * Also, uploading descriptors to newly allocated memory doesn't require
43 * a KCACHE flush.
44 *
45 *
46 * Possible scenarios for one 16 dword image+sampler slot:
47 *
48 * | Image | w/ FMASK | Buffer | NULL
49 * [ 0: 3] Image[0:3] | Image[0:3] | Null[0:3] | Null[0:3]
50 * [ 4: 7] Image[4:7] | Image[4:7] | Buffer[0:3] | 0
51 * [ 8:11] Null[0:3] | Fmask[0:3] | Null[0:3] | Null[0:3]
52 * [12:15] Sampler[0:3] | Fmask[4:7] | Sampler[0:3] | Sampler[0:3]
53 *
54 * FMASK implies MSAA, therefore no sampler state.
55 * Sampler states are never unbound except when FMASK is bound.
56 */
57
58 #include "radeon/r600_cs.h"
59 #include "si_pipe.h"
60 #include "sid.h"
61 #include "gfx9d.h"
62
63 #include "util/hash_table.h"
64 #include "util/u_idalloc.h"
65 #include "util/u_format.h"
66 #include "util/u_memory.h"
67 #include "util/u_upload_mgr.h"
68
69
70 /* NULL image and buffer descriptor for textures (alpha = 1) and images
71 * (alpha = 0).
72 *
73 * For images, all fields must be zero except for the swizzle, which
74 * supports arbitrary combinations of 0s and 1s. The texture type must be
75 * any valid type (e.g. 1D). If the texture type isn't set, the hw hangs.
76 *
77 * For buffers, all fields must be zero. If they are not, the hw hangs.
78 *
79 * This is the only reason why the buffer descriptor must be in words [4:7].
80 */
81 static uint32_t null_texture_descriptor[8] = {
82 0,
83 0,
84 0,
85 S_008F1C_DST_SEL_W(V_008F1C_SQ_SEL_1) |
86 S_008F1C_TYPE(V_008F1C_SQ_RSRC_IMG_1D)
87 /* the rest must contain zeros, which is also used by the buffer
88 * descriptor */
89 };
90
91 static uint32_t null_image_descriptor[8] = {
92 0,
93 0,
94 0,
95 S_008F1C_TYPE(V_008F1C_SQ_RSRC_IMG_1D)
96 /* the rest must contain zeros, which is also used by the buffer
97 * descriptor */
98 };
99
100 static void si_init_descriptor_list(uint32_t *desc_list,
101 unsigned element_dw_size,
102 unsigned num_elements,
103 const uint32_t *null_descriptor)
104 {
105 int i;
106
107 /* Initialize the array to NULL descriptors if the element size is 8. */
108 if (null_descriptor) {
109 assert(element_dw_size % 8 == 0);
110 for (i = 0; i < num_elements * element_dw_size / 8; i++)
111 memcpy(desc_list + i * 8, null_descriptor, 8 * 4);
112 }
113 }
114
115 static void si_init_descriptors(struct si_descriptors *desc,
116 unsigned shader_userdata_index,
117 unsigned element_dw_size,
118 unsigned num_elements)
119 {
120 desc->list = CALLOC(num_elements, element_dw_size * 4);
121 desc->element_dw_size = element_dw_size;
122 desc->num_elements = num_elements;
123 desc->shader_userdata_offset = shader_userdata_index * 4;
124 }
125
126 static void si_release_descriptors(struct si_descriptors *desc)
127 {
128 r600_resource_reference(&desc->buffer, NULL);
129 FREE(desc->list);
130 }
131
132 static bool si_upload_descriptors(struct si_context *sctx,
133 struct si_descriptors *desc,
134 struct r600_atom * atom)
135 {
136 unsigned slot_size = desc->element_dw_size * 4;
137 unsigned first_slot_offset = desc->first_active_slot * slot_size;
138 unsigned upload_size = desc->num_active_slots * slot_size;
139
140 /* Skip the upload if no shader is using the descriptors. dirty_mask
141 * will stay dirty and the descriptors will be uploaded when there is
142 * a shader using them.
143 */
144 if (!upload_size)
145 return true;
146
147 uint32_t *ptr;
148 u_upload_alloc(sctx->b.b.const_uploader, 0, upload_size,
149 si_optimal_tcc_alignment(sctx, upload_size),
150 (unsigned*)&desc->buffer_offset,
151 (struct pipe_resource**)&desc->buffer,
152 (void**)&ptr);
153 if (!desc->buffer)
154 return false; /* skip the draw call */
155
156 util_memcpy_cpu_to_le32(ptr, (char*)desc->list + first_slot_offset,
157 upload_size);
158 desc->gpu_list = ptr - first_slot_offset / 4;
159
160 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx, desc->buffer,
161 RADEON_USAGE_READ, RADEON_PRIO_DESCRIPTORS);
162
163 /* The shader pointer should point to slot 0. */
164 desc->buffer_offset -= first_slot_offset;
165
166 if (atom)
167 si_mark_atom_dirty(sctx, atom);
168
169 return true;
170 }
171
172 static void
173 si_descriptors_begin_new_cs(struct si_context *sctx, struct si_descriptors *desc)
174 {
175 if (!desc->buffer)
176 return;
177
178 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx, desc->buffer,
179 RADEON_USAGE_READ, RADEON_PRIO_DESCRIPTORS);
180 }
181
182 /* SAMPLER VIEWS */
183
184 static unsigned
185 si_sampler_and_image_descriptors_idx(unsigned shader)
186 {
187 return SI_DESCS_FIRST_SHADER + shader * SI_NUM_SHADER_DESCS +
188 SI_SHADER_DESCS_SAMPLERS_AND_IMAGES;
189 }
190
191 static struct si_descriptors *
192 si_sampler_and_image_descriptors(struct si_context *sctx, unsigned shader)
193 {
194 return &sctx->descriptors[si_sampler_and_image_descriptors_idx(shader)];
195 }
196
197 static void si_release_sampler_views(struct si_samplers *samplers)
198 {
199 int i;
200
201 for (i = 0; i < ARRAY_SIZE(samplers->views); i++) {
202 pipe_sampler_view_reference(&samplers->views[i], NULL);
203 }
204 }
205
206 static void si_sampler_view_add_buffer(struct si_context *sctx,
207 struct pipe_resource *resource,
208 enum radeon_bo_usage usage,
209 bool is_stencil_sampler,
210 bool check_mem)
211 {
212 struct r600_resource *rres;
213 struct r600_texture *rtex;
214 enum radeon_bo_priority priority;
215
216 if (!resource)
217 return;
218
219 if (resource->target != PIPE_BUFFER) {
220 struct r600_texture *tex = (struct r600_texture*)resource;
221
222 if (tex->is_depth && !r600_can_sample_zs(tex, is_stencil_sampler))
223 resource = &tex->flushed_depth_texture->resource.b.b;
224 }
225
226 rres = (struct r600_resource*)resource;
227 priority = r600_get_sampler_view_priority(rres);
228
229 radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx,
230 rres, usage, priority,
231 check_mem);
232
233 if (resource->target == PIPE_BUFFER)
234 return;
235
236 /* Now add separate DCC or HTILE. */
237 rtex = (struct r600_texture*)resource;
238 if (rtex->dcc_separate_buffer) {
239 radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx,
240 rtex->dcc_separate_buffer, usage,
241 RADEON_PRIO_DCC, check_mem);
242 }
243 }
244
245 static void si_sampler_views_begin_new_cs(struct si_context *sctx,
246 struct si_samplers *samplers)
247 {
248 unsigned mask = samplers->enabled_mask;
249
250 /* Add buffers to the CS. */
251 while (mask) {
252 int i = u_bit_scan(&mask);
253 struct si_sampler_view *sview = (struct si_sampler_view *)samplers->views[i];
254
255 si_sampler_view_add_buffer(sctx, sview->base.texture,
256 RADEON_USAGE_READ,
257 sview->is_stencil_sampler, false);
258 }
259 }
260
261 /* Set buffer descriptor fields that can be changed by reallocations. */
262 static void si_set_buf_desc_address(struct r600_resource *buf,
263 uint64_t offset, uint32_t *state)
264 {
265 uint64_t va = buf->gpu_address + offset;
266
267 state[0] = va;
268 state[1] &= C_008F04_BASE_ADDRESS_HI;
269 state[1] |= S_008F04_BASE_ADDRESS_HI(va >> 32);
270 }
271
272 /* Set texture descriptor fields that can be changed by reallocations.
273 *
274 * \param tex texture
275 * \param base_level_info information of the level of BASE_ADDRESS
276 * \param base_level the level of BASE_ADDRESS
277 * \param first_level pipe_sampler_view.u.tex.first_level
278 * \param block_width util_format_get_blockwidth()
279 * \param is_stencil select between separate Z & Stencil
280 * \param state descriptor to update
281 */
282 void si_set_mutable_tex_desc_fields(struct si_screen *sscreen,
283 struct r600_texture *tex,
284 const struct legacy_surf_level *base_level_info,
285 unsigned base_level, unsigned first_level,
286 unsigned block_width, bool is_stencil,
287 uint32_t *state)
288 {
289 uint64_t va, meta_va = 0;
290
291 if (tex->is_depth && !r600_can_sample_zs(tex, is_stencil)) {
292 tex = tex->flushed_depth_texture;
293 is_stencil = false;
294 }
295
296 va = tex->resource.gpu_address;
297
298 if (sscreen->b.chip_class >= GFX9) {
299 /* Only stencil_offset needs to be added here. */
300 if (is_stencil)
301 va += tex->surface.u.gfx9.stencil_offset;
302 else
303 va += tex->surface.u.gfx9.surf_offset;
304 } else {
305 va += base_level_info->offset;
306 }
307
308 state[0] = va >> 8;
309 state[1] &= C_008F14_BASE_ADDRESS_HI;
310 state[1] |= S_008F14_BASE_ADDRESS_HI(va >> 40);
311
312 /* Only macrotiled modes can set tile swizzle.
313 * GFX9 doesn't use (legacy) base_level_info.
314 */
315 if (sscreen->b.chip_class >= GFX9 ||
316 base_level_info->mode == RADEON_SURF_MODE_2D)
317 state[0] |= tex->surface.tile_swizzle;
318
319 if (sscreen->b.chip_class >= VI) {
320 state[6] &= C_008F28_COMPRESSION_EN;
321 state[7] = 0;
322
323 if (vi_dcc_enabled(tex, first_level)) {
324 meta_va = (!tex->dcc_separate_buffer ? tex->resource.gpu_address : 0) +
325 tex->dcc_offset;
326
327 if (sscreen->b.chip_class == VI) {
328 meta_va += base_level_info->dcc_offset;
329 assert(base_level_info->mode == RADEON_SURF_MODE_2D);
330 }
331
332 meta_va |= (uint32_t)tex->surface.tile_swizzle << 8;
333 } else if (vi_tc_compat_htile_enabled(tex, first_level)) {
334 meta_va = tex->resource.gpu_address + tex->htile_offset;
335 }
336
337 if (meta_va) {
338 state[6] |= S_008F28_COMPRESSION_EN(1);
339 state[7] = meta_va >> 8;
340 }
341 }
342
343 if (sscreen->b.chip_class >= GFX9) {
344 state[3] &= C_008F1C_SW_MODE;
345 state[4] &= C_008F20_PITCH_GFX9;
346
347 if (is_stencil) {
348 state[3] |= S_008F1C_SW_MODE(tex->surface.u.gfx9.stencil.swizzle_mode);
349 state[4] |= S_008F20_PITCH_GFX9(tex->surface.u.gfx9.stencil.epitch);
350 } else {
351 state[3] |= S_008F1C_SW_MODE(tex->surface.u.gfx9.surf.swizzle_mode);
352 state[4] |= S_008F20_PITCH_GFX9(tex->surface.u.gfx9.surf.epitch);
353 }
354
355 state[5] &= C_008F24_META_DATA_ADDRESS &
356 C_008F24_META_PIPE_ALIGNED &
357 C_008F24_META_RB_ALIGNED;
358 if (meta_va) {
359 struct gfx9_surf_meta_flags meta;
360
361 if (tex->dcc_offset)
362 meta = tex->surface.u.gfx9.dcc;
363 else
364 meta = tex->surface.u.gfx9.htile;
365
366 state[5] |= S_008F24_META_DATA_ADDRESS(meta_va >> 40) |
367 S_008F24_META_PIPE_ALIGNED(meta.pipe_aligned) |
368 S_008F24_META_RB_ALIGNED(meta.rb_aligned);
369 }
370 } else {
371 /* SI-CI-VI */
372 unsigned pitch = base_level_info->nblk_x * block_width;
373 unsigned index = si_tile_mode_index(tex, base_level, is_stencil);
374
375 state[3] &= C_008F1C_TILING_INDEX;
376 state[3] |= S_008F1C_TILING_INDEX(index);
377 state[4] &= C_008F20_PITCH_GFX6;
378 state[4] |= S_008F20_PITCH_GFX6(pitch - 1);
379 }
380 }
381
382 static void si_set_sampler_state_desc(struct si_sampler_state *sstate,
383 struct si_sampler_view *sview,
384 struct r600_texture *tex,
385 uint32_t *desc)
386 {
387 if (sview && sview->is_integer)
388 memcpy(desc, sstate->integer_val, 4*4);
389 else if (tex && tex->upgraded_depth &&
390 (!sview || !sview->is_stencil_sampler))
391 memcpy(desc, sstate->upgraded_depth_val, 4*4);
392 else
393 memcpy(desc, sstate->val, 4*4);
394 }
395
396 static void si_set_sampler_view_desc(struct si_context *sctx,
397 struct si_sampler_view *sview,
398 struct si_sampler_state *sstate,
399 uint32_t *desc)
400 {
401 struct pipe_sampler_view *view = &sview->base;
402 struct r600_texture *rtex = (struct r600_texture *)view->texture;
403 bool is_buffer = rtex->resource.b.b.target == PIPE_BUFFER;
404
405 if (unlikely(!is_buffer && sview->dcc_incompatible)) {
406 if (vi_dcc_enabled(rtex, view->u.tex.first_level))
407 if (!si_texture_disable_dcc(&sctx->b, rtex))
408 sctx->b.decompress_dcc(&sctx->b.b, rtex);
409
410 sview->dcc_incompatible = false;
411 }
412
413 assert(rtex); /* views with texture == NULL aren't supported */
414 memcpy(desc, sview->state, 8*4);
415
416 if (is_buffer) {
417 si_set_buf_desc_address(&rtex->resource,
418 sview->base.u.buf.offset,
419 desc + 4);
420 } else {
421 bool is_separate_stencil = rtex->db_compatible &&
422 sview->is_stencil_sampler;
423
424 si_set_mutable_tex_desc_fields(sctx->screen, rtex,
425 sview->base_level_info,
426 sview->base_level,
427 sview->base.u.tex.first_level,
428 sview->block_width,
429 is_separate_stencil,
430 desc);
431 }
432
433 if (!is_buffer && rtex->fmask.size) {
434 memcpy(desc + 8, sview->fmask_state, 8*4);
435 } else {
436 /* Disable FMASK and bind sampler state in [12:15]. */
437 memcpy(desc + 8, null_texture_descriptor, 4*4);
438
439 if (sstate)
440 si_set_sampler_state_desc(sstate, sview,
441 is_buffer ? NULL : rtex,
442 desc + 12);
443 }
444 }
445
446 static bool color_needs_decompression(struct r600_texture *rtex)
447 {
448 return rtex->fmask.size ||
449 (rtex->dirty_level_mask &&
450 (rtex->cmask.size || rtex->dcc_offset));
451 }
452
453 static bool depth_needs_decompression(struct r600_texture *rtex)
454 {
455 /* If the depth/stencil texture is TC-compatible, no decompression
456 * will be done. The decompression function will only flush DB caches
457 * to make it coherent with shaders. That's necessary because the driver
458 * doesn't flush DB caches in any other case.
459 */
460 return rtex->db_compatible;
461 }
462
463 static void si_set_sampler_view(struct si_context *sctx,
464 unsigned shader,
465 unsigned slot, struct pipe_sampler_view *view,
466 bool disallow_early_out)
467 {
468 struct si_samplers *samplers = &sctx->samplers[shader];
469 struct si_sampler_view *rview = (struct si_sampler_view*)view;
470 struct si_descriptors *descs = si_sampler_and_image_descriptors(sctx, shader);
471 unsigned desc_slot = si_get_sampler_slot(slot);
472 uint32_t *desc = descs->list + desc_slot * 16;
473
474 if (samplers->views[slot] == view && !disallow_early_out)
475 return;
476
477 if (view) {
478 struct r600_texture *rtex = (struct r600_texture *)view->texture;
479
480 si_set_sampler_view_desc(sctx, rview,
481 samplers->sampler_states[slot], desc);
482
483 if (rtex->resource.b.b.target == PIPE_BUFFER) {
484 rtex->resource.bind_history |= PIPE_BIND_SAMPLER_VIEW;
485 samplers->needs_depth_decompress_mask &= ~(1u << slot);
486 samplers->needs_color_decompress_mask &= ~(1u << slot);
487 } else {
488 if (depth_needs_decompression(rtex)) {
489 samplers->needs_depth_decompress_mask |= 1u << slot;
490 } else {
491 samplers->needs_depth_decompress_mask &= ~(1u << slot);
492 }
493 if (color_needs_decompression(rtex)) {
494 samplers->needs_color_decompress_mask |= 1u << slot;
495 } else {
496 samplers->needs_color_decompress_mask &= ~(1u << slot);
497 }
498
499 if (rtex->dcc_offset &&
500 p_atomic_read(&rtex->framebuffers_bound))
501 sctx->need_check_render_feedback = true;
502 }
503
504 pipe_sampler_view_reference(&samplers->views[slot], view);
505 samplers->enabled_mask |= 1u << slot;
506
507 /* Since this can flush, it must be done after enabled_mask is
508 * updated. */
509 si_sampler_view_add_buffer(sctx, view->texture,
510 RADEON_USAGE_READ,
511 rview->is_stencil_sampler, true);
512 } else {
513 pipe_sampler_view_reference(&samplers->views[slot], NULL);
514 memcpy(desc, null_texture_descriptor, 8*4);
515 /* Only clear the lower dwords of FMASK. */
516 memcpy(desc + 8, null_texture_descriptor, 4*4);
517 /* Re-set the sampler state if we are transitioning from FMASK. */
518 if (samplers->sampler_states[slot])
519 si_set_sampler_state_desc(samplers->sampler_states[slot], NULL, NULL,
520 desc + 12);
521
522 samplers->enabled_mask &= ~(1u << slot);
523 samplers->needs_depth_decompress_mask &= ~(1u << slot);
524 samplers->needs_color_decompress_mask &= ~(1u << slot);
525 }
526
527 sctx->descriptors_dirty |= 1u << si_sampler_and_image_descriptors_idx(shader);
528 }
529
530 static void si_update_shader_needs_decompress_mask(struct si_context *sctx,
531 unsigned shader)
532 {
533 struct si_samplers *samplers = &sctx->samplers[shader];
534 unsigned shader_bit = 1 << shader;
535
536 if (samplers->needs_depth_decompress_mask ||
537 samplers->needs_color_decompress_mask ||
538 sctx->images[shader].needs_color_decompress_mask)
539 sctx->shader_needs_decompress_mask |= shader_bit;
540 else
541 sctx->shader_needs_decompress_mask &= ~shader_bit;
542 }
543
544 static void si_set_sampler_views(struct pipe_context *ctx,
545 enum pipe_shader_type shader, unsigned start,
546 unsigned count,
547 struct pipe_sampler_view **views)
548 {
549 struct si_context *sctx = (struct si_context *)ctx;
550 int i;
551
552 if (!count || shader >= SI_NUM_SHADERS)
553 return;
554
555 if (views) {
556 for (i = 0; i < count; i++)
557 si_set_sampler_view(sctx, shader, start + i, views[i], false);
558 } else {
559 for (i = 0; i < count; i++)
560 si_set_sampler_view(sctx, shader, start + i, NULL, false);
561 }
562
563 si_update_shader_needs_decompress_mask(sctx, shader);
564 }
565
566 static void
567 si_samplers_update_needs_color_decompress_mask(struct si_samplers *samplers)
568 {
569 unsigned mask = samplers->enabled_mask;
570
571 while (mask) {
572 int i = u_bit_scan(&mask);
573 struct pipe_resource *res = samplers->views[i]->texture;
574
575 if (res && res->target != PIPE_BUFFER) {
576 struct r600_texture *rtex = (struct r600_texture *)res;
577
578 if (color_needs_decompression(rtex)) {
579 samplers->needs_color_decompress_mask |= 1u << i;
580 } else {
581 samplers->needs_color_decompress_mask &= ~(1u << i);
582 }
583 }
584 }
585 }
586
587 /* IMAGE VIEWS */
588
589 static void
590 si_release_image_views(struct si_images *images)
591 {
592 unsigned i;
593
594 for (i = 0; i < SI_NUM_IMAGES; ++i) {
595 struct pipe_image_view *view = &images->views[i];
596
597 pipe_resource_reference(&view->resource, NULL);
598 }
599 }
600
601 static void
602 si_image_views_begin_new_cs(struct si_context *sctx, struct si_images *images)
603 {
604 uint mask = images->enabled_mask;
605
606 /* Add buffers to the CS. */
607 while (mask) {
608 int i = u_bit_scan(&mask);
609 struct pipe_image_view *view = &images->views[i];
610
611 assert(view->resource);
612
613 si_sampler_view_add_buffer(sctx, view->resource,
614 RADEON_USAGE_READWRITE, false, false);
615 }
616 }
617
618 static void
619 si_disable_shader_image(struct si_context *ctx, unsigned shader, unsigned slot)
620 {
621 struct si_images *images = &ctx->images[shader];
622
623 if (images->enabled_mask & (1u << slot)) {
624 struct si_descriptors *descs = si_sampler_and_image_descriptors(ctx, shader);
625 unsigned desc_slot = si_get_image_slot(slot);
626
627 pipe_resource_reference(&images->views[slot].resource, NULL);
628 images->needs_color_decompress_mask &= ~(1 << slot);
629
630 memcpy(descs->list + desc_slot*8, null_image_descriptor, 8*4);
631 images->enabled_mask &= ~(1u << slot);
632 ctx->descriptors_dirty |= 1u << si_sampler_and_image_descriptors_idx(shader);
633 }
634 }
635
636 static void
637 si_mark_image_range_valid(const struct pipe_image_view *view)
638 {
639 struct r600_resource *res = (struct r600_resource *)view->resource;
640
641 assert(res && res->b.b.target == PIPE_BUFFER);
642
643 util_range_add(&res->valid_buffer_range,
644 view->u.buf.offset,
645 view->u.buf.offset + view->u.buf.size);
646 }
647
648 static void si_set_shader_image_desc(struct si_context *ctx,
649 const struct pipe_image_view *view,
650 bool skip_decompress,
651 uint32_t *desc)
652 {
653 struct si_screen *screen = ctx->screen;
654 struct r600_resource *res;
655
656 res = (struct r600_resource *)view->resource;
657
658 if (res->b.b.target == PIPE_BUFFER) {
659 if (view->access & PIPE_IMAGE_ACCESS_WRITE)
660 si_mark_image_range_valid(view);
661
662 si_make_buffer_descriptor(screen, res,
663 view->format,
664 view->u.buf.offset,
665 view->u.buf.size, desc);
666 si_set_buf_desc_address(res, view->u.buf.offset, desc + 4);
667 } else {
668 static const unsigned char swizzle[4] = { 0, 1, 2, 3 };
669 struct r600_texture *tex = (struct r600_texture *)res;
670 unsigned level = view->u.tex.level;
671 unsigned width, height, depth, hw_level;
672 bool uses_dcc = vi_dcc_enabled(tex, level);
673
674 assert(!tex->is_depth);
675 assert(tex->fmask.size == 0);
676
677 if (uses_dcc && !skip_decompress &&
678 (view->access & PIPE_IMAGE_ACCESS_WRITE ||
679 !vi_dcc_formats_compatible(res->b.b.format, view->format))) {
680 /* If DCC can't be disabled, at least decompress it.
681 * The decompression is relatively cheap if the surface
682 * has been decompressed already.
683 */
684 if (!si_texture_disable_dcc(&ctx->b, tex))
685 ctx->b.decompress_dcc(&ctx->b.b, tex);
686 }
687
688 if (ctx->b.chip_class >= GFX9) {
689 /* Always set the base address. The swizzle modes don't
690 * allow setting mipmap level offsets as the base.
691 */
692 width = res->b.b.width0;
693 height = res->b.b.height0;
694 depth = res->b.b.depth0;
695 hw_level = level;
696 } else {
697 /* Always force the base level to the selected level.
698 *
699 * This is required for 3D textures, where otherwise
700 * selecting a single slice for non-layered bindings
701 * fails. It doesn't hurt the other targets.
702 */
703 width = u_minify(res->b.b.width0, level);
704 height = u_minify(res->b.b.height0, level);
705 depth = u_minify(res->b.b.depth0, level);
706 hw_level = 0;
707 }
708
709 si_make_texture_descriptor(screen, tex,
710 false, res->b.b.target,
711 view->format, swizzle,
712 hw_level, hw_level,
713 view->u.tex.first_layer,
714 view->u.tex.last_layer,
715 width, height, depth,
716 desc, NULL);
717 si_set_mutable_tex_desc_fields(screen, tex,
718 &tex->surface.u.legacy.level[level],
719 level, level,
720 util_format_get_blockwidth(view->format),
721 false, desc);
722 }
723 }
724
725 static void si_set_shader_image(struct si_context *ctx,
726 unsigned shader,
727 unsigned slot, const struct pipe_image_view *view,
728 bool skip_decompress)
729 {
730 struct si_images *images = &ctx->images[shader];
731 struct si_descriptors *descs = si_sampler_and_image_descriptors(ctx, shader);
732 struct r600_resource *res;
733 unsigned desc_slot = si_get_image_slot(slot);
734 uint32_t *desc = descs->list + desc_slot * 8;
735
736 if (!view || !view->resource) {
737 si_disable_shader_image(ctx, shader, slot);
738 return;
739 }
740
741 res = (struct r600_resource *)view->resource;
742
743 if (&images->views[slot] != view)
744 util_copy_image_view(&images->views[slot], view);
745
746 si_set_shader_image_desc(ctx, view, skip_decompress, desc);
747
748 if (res->b.b.target == PIPE_BUFFER) {
749 images->needs_color_decompress_mask &= ~(1 << slot);
750 res->bind_history |= PIPE_BIND_SHADER_IMAGE;
751 } else {
752 struct r600_texture *tex = (struct r600_texture *)res;
753 unsigned level = view->u.tex.level;
754
755 if (color_needs_decompression(tex)) {
756 images->needs_color_decompress_mask |= 1 << slot;
757 } else {
758 images->needs_color_decompress_mask &= ~(1 << slot);
759 }
760
761 if (vi_dcc_enabled(tex, level) &&
762 p_atomic_read(&tex->framebuffers_bound))
763 ctx->need_check_render_feedback = true;
764 }
765
766 images->enabled_mask |= 1u << slot;
767 ctx->descriptors_dirty |= 1u << si_sampler_and_image_descriptors_idx(shader);
768
769 /* Since this can flush, it must be done after enabled_mask is updated. */
770 si_sampler_view_add_buffer(ctx, &res->b.b,
771 (view->access & PIPE_IMAGE_ACCESS_WRITE) ?
772 RADEON_USAGE_READWRITE : RADEON_USAGE_READ,
773 false, true);
774 }
775
776 static void
777 si_set_shader_images(struct pipe_context *pipe,
778 enum pipe_shader_type shader,
779 unsigned start_slot, unsigned count,
780 const struct pipe_image_view *views)
781 {
782 struct si_context *ctx = (struct si_context *)pipe;
783 unsigned i, slot;
784
785 assert(shader < SI_NUM_SHADERS);
786
787 if (!count)
788 return;
789
790 assert(start_slot + count <= SI_NUM_IMAGES);
791
792 if (views) {
793 for (i = 0, slot = start_slot; i < count; ++i, ++slot)
794 si_set_shader_image(ctx, shader, slot, &views[i], false);
795 } else {
796 for (i = 0, slot = start_slot; i < count; ++i, ++slot)
797 si_set_shader_image(ctx, shader, slot, NULL, false);
798 }
799
800 si_update_shader_needs_decompress_mask(ctx, shader);
801 }
802
803 static void
804 si_images_update_needs_color_decompress_mask(struct si_images *images)
805 {
806 unsigned mask = images->enabled_mask;
807
808 while (mask) {
809 int i = u_bit_scan(&mask);
810 struct pipe_resource *res = images->views[i].resource;
811
812 if (res && res->target != PIPE_BUFFER) {
813 struct r600_texture *rtex = (struct r600_texture *)res;
814
815 if (color_needs_decompression(rtex)) {
816 images->needs_color_decompress_mask |= 1 << i;
817 } else {
818 images->needs_color_decompress_mask &= ~(1 << i);
819 }
820 }
821 }
822 }
823
824 /* SAMPLER STATES */
825
826 static void si_bind_sampler_states(struct pipe_context *ctx,
827 enum pipe_shader_type shader,
828 unsigned start, unsigned count, void **states)
829 {
830 struct si_context *sctx = (struct si_context *)ctx;
831 struct si_samplers *samplers = &sctx->samplers[shader];
832 struct si_descriptors *desc = si_sampler_and_image_descriptors(sctx, shader);
833 struct si_sampler_state **sstates = (struct si_sampler_state**)states;
834 int i;
835
836 if (!count || shader >= SI_NUM_SHADERS)
837 return;
838
839 for (i = 0; i < count; i++) {
840 unsigned slot = start + i;
841 unsigned desc_slot = si_get_sampler_slot(slot);
842
843 if (!sstates[i] ||
844 sstates[i] == samplers->sampler_states[slot])
845 continue;
846
847 #ifdef DEBUG
848 assert(sstates[i]->magic == SI_SAMPLER_STATE_MAGIC);
849 #endif
850 samplers->sampler_states[slot] = sstates[i];
851
852 /* If FMASK is bound, don't overwrite it.
853 * The sampler state will be set after FMASK is unbound.
854 */
855 struct si_sampler_view *sview =
856 (struct si_sampler_view *)samplers->views[slot];
857
858 struct r600_texture *tex = NULL;
859
860 if (sview && sview->base.texture &&
861 sview->base.texture->target != PIPE_BUFFER)
862 tex = (struct r600_texture *)sview->base.texture;
863
864 if (tex && tex->fmask.size)
865 continue;
866
867 si_set_sampler_state_desc(sstates[i], sview, tex,
868 desc->list + desc_slot * 16 + 12);
869
870 sctx->descriptors_dirty |= 1u << si_sampler_and_image_descriptors_idx(shader);
871 }
872 }
873
874 /* BUFFER RESOURCES */
875
876 static void si_init_buffer_resources(struct si_buffer_resources *buffers,
877 struct si_descriptors *descs,
878 unsigned num_buffers,
879 unsigned shader_userdata_index,
880 enum radeon_bo_usage shader_usage,
881 enum radeon_bo_usage shader_usage_constbuf,
882 enum radeon_bo_priority priority,
883 enum radeon_bo_priority priority_constbuf)
884 {
885 buffers->shader_usage = shader_usage;
886 buffers->shader_usage_constbuf = shader_usage_constbuf;
887 buffers->priority = priority;
888 buffers->priority_constbuf = priority_constbuf;
889 buffers->buffers = CALLOC(num_buffers, sizeof(struct pipe_resource*));
890
891 si_init_descriptors(descs, shader_userdata_index, 4, num_buffers);
892 }
893
894 static void si_release_buffer_resources(struct si_buffer_resources *buffers,
895 struct si_descriptors *descs)
896 {
897 int i;
898
899 for (i = 0; i < descs->num_elements; i++) {
900 pipe_resource_reference(&buffers->buffers[i], NULL);
901 }
902
903 FREE(buffers->buffers);
904 }
905
906 static void si_buffer_resources_begin_new_cs(struct si_context *sctx,
907 struct si_buffer_resources *buffers)
908 {
909 unsigned mask = buffers->enabled_mask;
910
911 /* Add buffers to the CS. */
912 while (mask) {
913 int i = u_bit_scan(&mask);
914
915 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
916 r600_resource(buffers->buffers[i]),
917 i < SI_NUM_SHADER_BUFFERS ? buffers->shader_usage :
918 buffers->shader_usage_constbuf,
919 i < SI_NUM_SHADER_BUFFERS ? buffers->priority :
920 buffers->priority_constbuf);
921 }
922 }
923
924 static void si_get_buffer_from_descriptors(struct si_buffer_resources *buffers,
925 struct si_descriptors *descs,
926 unsigned idx, struct pipe_resource **buf,
927 unsigned *offset, unsigned *size)
928 {
929 pipe_resource_reference(buf, buffers->buffers[idx]);
930 if (*buf) {
931 struct r600_resource *res = r600_resource(*buf);
932 const uint32_t *desc = descs->list + idx * 4;
933 uint64_t va;
934
935 *size = desc[2];
936
937 assert(G_008F04_STRIDE(desc[1]) == 0);
938 va = ((uint64_t)desc[1] << 32) | desc[0];
939
940 assert(va >= res->gpu_address && va + *size <= res->gpu_address + res->bo_size);
941 *offset = va - res->gpu_address;
942 }
943 }
944
945 /* VERTEX BUFFERS */
946
947 static void si_vertex_buffers_begin_new_cs(struct si_context *sctx)
948 {
949 struct si_descriptors *desc = &sctx->vertex_buffers;
950 int count = sctx->vertex_elements ? sctx->vertex_elements->count : 0;
951 int i;
952
953 for (i = 0; i < count; i++) {
954 int vb = sctx->vertex_elements->vertex_buffer_index[i];
955
956 if (vb >= ARRAY_SIZE(sctx->vertex_buffer))
957 continue;
958 if (!sctx->vertex_buffer[vb].buffer.resource)
959 continue;
960
961 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
962 (struct r600_resource*)sctx->vertex_buffer[vb].buffer.resource,
963 RADEON_USAGE_READ, RADEON_PRIO_VERTEX_BUFFER);
964 }
965
966 if (!desc->buffer)
967 return;
968 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
969 desc->buffer, RADEON_USAGE_READ,
970 RADEON_PRIO_DESCRIPTORS);
971 }
972
973 bool si_upload_vertex_buffer_descriptors(struct si_context *sctx)
974 {
975 struct si_vertex_elements *velems = sctx->vertex_elements;
976 struct si_descriptors *desc = &sctx->vertex_buffers;
977 unsigned i, count;
978 unsigned desc_list_byte_size;
979 unsigned first_vb_use_mask;
980 uint64_t va;
981 uint32_t *ptr;
982
983 if (!sctx->vertex_buffers_dirty || !velems)
984 return true;
985
986 count = velems->count;
987
988 if (!count)
989 return true;
990
991 desc_list_byte_size = velems->desc_list_byte_size;
992 first_vb_use_mask = velems->first_vb_use_mask;
993
994 /* Vertex buffer descriptors are the only ones which are uploaded
995 * directly through a staging buffer and don't go through
996 * the fine-grained upload path.
997 */
998 u_upload_alloc(sctx->b.b.const_uploader, 0,
999 desc_list_byte_size,
1000 si_optimal_tcc_alignment(sctx, desc_list_byte_size),
1001 (unsigned*)&desc->buffer_offset,
1002 (struct pipe_resource**)&desc->buffer, (void**)&ptr);
1003 if (!desc->buffer)
1004 return false;
1005
1006 desc->list = ptr;
1007 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
1008 desc->buffer, RADEON_USAGE_READ,
1009 RADEON_PRIO_DESCRIPTORS);
1010
1011 assert(count <= SI_MAX_ATTRIBS);
1012
1013 for (i = 0; i < count; i++) {
1014 struct pipe_vertex_buffer *vb;
1015 struct r600_resource *rbuffer;
1016 unsigned offset;
1017 unsigned vbo_index = velems->vertex_buffer_index[i];
1018 uint32_t *desc = &ptr[i*4];
1019
1020 vb = &sctx->vertex_buffer[vbo_index];
1021 rbuffer = (struct r600_resource*)vb->buffer.resource;
1022 if (!rbuffer) {
1023 memset(desc, 0, 16);
1024 continue;
1025 }
1026
1027 offset = vb->buffer_offset + velems->src_offset[i];
1028 va = rbuffer->gpu_address + offset;
1029
1030 /* Fill in T# buffer resource description */
1031 desc[0] = va;
1032 desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32) |
1033 S_008F04_STRIDE(vb->stride);
1034
1035 if (sctx->b.chip_class != VI && vb->stride) {
1036 /* Round up by rounding down and adding 1 */
1037 desc[2] = (vb->buffer.resource->width0 - offset -
1038 velems->format_size[i]) /
1039 vb->stride + 1;
1040 } else {
1041 desc[2] = vb->buffer.resource->width0 - offset;
1042 }
1043
1044 desc[3] = velems->rsrc_word3[i];
1045
1046 if (first_vb_use_mask & (1 << i)) {
1047 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
1048 (struct r600_resource*)vb->buffer.resource,
1049 RADEON_USAGE_READ, RADEON_PRIO_VERTEX_BUFFER);
1050 }
1051 }
1052
1053 /* Don't flush the const cache. It would have a very negative effect
1054 * on performance (confirmed by testing). New descriptors are always
1055 * uploaded to a fresh new buffer, so I don't think flushing the const
1056 * cache is needed. */
1057 si_mark_atom_dirty(sctx, &sctx->shader_pointers.atom);
1058 sctx->vertex_buffers_dirty = false;
1059 sctx->vertex_buffer_pointer_dirty = true;
1060 sctx->prefetch_L2_mask |= SI_PREFETCH_VBO_DESCRIPTORS;
1061 return true;
1062 }
1063
1064
1065 /* CONSTANT BUFFERS */
1066
1067 static unsigned
1068 si_const_and_shader_buffer_descriptors_idx(unsigned shader)
1069 {
1070 return SI_DESCS_FIRST_SHADER + shader * SI_NUM_SHADER_DESCS +
1071 SI_SHADER_DESCS_CONST_AND_SHADER_BUFFERS;
1072 }
1073
1074 static struct si_descriptors *
1075 si_const_and_shader_buffer_descriptors(struct si_context *sctx, unsigned shader)
1076 {
1077 return &sctx->descriptors[si_const_and_shader_buffer_descriptors_idx(shader)];
1078 }
1079
1080 void si_upload_const_buffer(struct si_context *sctx, struct r600_resource **rbuffer,
1081 const uint8_t *ptr, unsigned size, uint32_t *const_offset)
1082 {
1083 void *tmp;
1084
1085 u_upload_alloc(sctx->b.b.const_uploader, 0, size,
1086 si_optimal_tcc_alignment(sctx, size),
1087 const_offset,
1088 (struct pipe_resource**)rbuffer, &tmp);
1089 if (*rbuffer)
1090 util_memcpy_cpu_to_le32(tmp, ptr, size);
1091 }
1092
1093 static void si_set_constant_buffer(struct si_context *sctx,
1094 struct si_buffer_resources *buffers,
1095 unsigned descriptors_idx,
1096 uint slot, const struct pipe_constant_buffer *input)
1097 {
1098 struct si_descriptors *descs = &sctx->descriptors[descriptors_idx];
1099 assert(slot < descs->num_elements);
1100 pipe_resource_reference(&buffers->buffers[slot], NULL);
1101
1102 /* CIK cannot unbind a constant buffer (S_BUFFER_LOAD is buggy
1103 * with a NULL buffer). We need to use a dummy buffer instead. */
1104 if (sctx->b.chip_class == CIK &&
1105 (!input || (!input->buffer && !input->user_buffer)))
1106 input = &sctx->null_const_buf;
1107
1108 if (input && (input->buffer || input->user_buffer)) {
1109 struct pipe_resource *buffer = NULL;
1110 uint64_t va;
1111
1112 /* Upload the user buffer if needed. */
1113 if (input->user_buffer) {
1114 unsigned buffer_offset;
1115
1116 si_upload_const_buffer(sctx,
1117 (struct r600_resource**)&buffer, input->user_buffer,
1118 input->buffer_size, &buffer_offset);
1119 if (!buffer) {
1120 /* Just unbind on failure. */
1121 si_set_constant_buffer(sctx, buffers, descriptors_idx, slot, NULL);
1122 return;
1123 }
1124 va = r600_resource(buffer)->gpu_address + buffer_offset;
1125 } else {
1126 pipe_resource_reference(&buffer, input->buffer);
1127 va = r600_resource(buffer)->gpu_address + input->buffer_offset;
1128 /* Only track usage for non-user buffers. */
1129 r600_resource(buffer)->bind_history |= PIPE_BIND_CONSTANT_BUFFER;
1130 }
1131
1132 /* Set the descriptor. */
1133 uint32_t *desc = descs->list + slot*4;
1134 desc[0] = va;
1135 desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32) |
1136 S_008F04_STRIDE(0);
1137 desc[2] = input->buffer_size;
1138 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1139 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1140 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1141 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1142 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
1143 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
1144
1145 buffers->buffers[slot] = buffer;
1146 radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx,
1147 (struct r600_resource*)buffer,
1148 buffers->shader_usage_constbuf,
1149 buffers->priority_constbuf, true);
1150 buffers->enabled_mask |= 1u << slot;
1151 } else {
1152 /* Clear the descriptor. */
1153 memset(descs->list + slot*4, 0, sizeof(uint32_t) * 4);
1154 buffers->enabled_mask &= ~(1u << slot);
1155 }
1156
1157 sctx->descriptors_dirty |= 1u << descriptors_idx;
1158 }
1159
1160 void si_set_rw_buffer(struct si_context *sctx,
1161 uint slot, const struct pipe_constant_buffer *input)
1162 {
1163 si_set_constant_buffer(sctx, &sctx->rw_buffers,
1164 SI_DESCS_RW_BUFFERS, slot, input);
1165 }
1166
1167 static void si_pipe_set_constant_buffer(struct pipe_context *ctx,
1168 enum pipe_shader_type shader, uint slot,
1169 const struct pipe_constant_buffer *input)
1170 {
1171 struct si_context *sctx = (struct si_context *)ctx;
1172
1173 if (shader >= SI_NUM_SHADERS)
1174 return;
1175
1176 slot = si_get_constbuf_slot(slot);
1177 si_set_constant_buffer(sctx, &sctx->const_and_shader_buffers[shader],
1178 si_const_and_shader_buffer_descriptors_idx(shader),
1179 slot, input);
1180 }
1181
1182 void si_get_pipe_constant_buffer(struct si_context *sctx, uint shader,
1183 uint slot, struct pipe_constant_buffer *cbuf)
1184 {
1185 cbuf->user_buffer = NULL;
1186 si_get_buffer_from_descriptors(
1187 &sctx->const_and_shader_buffers[shader],
1188 si_const_and_shader_buffer_descriptors(sctx, shader),
1189 si_get_constbuf_slot(slot),
1190 &cbuf->buffer, &cbuf->buffer_offset, &cbuf->buffer_size);
1191 }
1192
1193 /* SHADER BUFFERS */
1194
1195 static void si_set_shader_buffers(struct pipe_context *ctx,
1196 enum pipe_shader_type shader,
1197 unsigned start_slot, unsigned count,
1198 const struct pipe_shader_buffer *sbuffers)
1199 {
1200 struct si_context *sctx = (struct si_context *)ctx;
1201 struct si_buffer_resources *buffers = &sctx->const_and_shader_buffers[shader];
1202 struct si_descriptors *descs = si_const_and_shader_buffer_descriptors(sctx, shader);
1203 unsigned i;
1204
1205 assert(start_slot + count <= SI_NUM_SHADER_BUFFERS);
1206
1207 for (i = 0; i < count; ++i) {
1208 const struct pipe_shader_buffer *sbuffer = sbuffers ? &sbuffers[i] : NULL;
1209 struct r600_resource *buf;
1210 unsigned slot = si_get_shaderbuf_slot(start_slot + i);
1211 uint32_t *desc = descs->list + slot * 4;
1212 uint64_t va;
1213
1214 if (!sbuffer || !sbuffer->buffer) {
1215 pipe_resource_reference(&buffers->buffers[slot], NULL);
1216 memset(desc, 0, sizeof(uint32_t) * 4);
1217 buffers->enabled_mask &= ~(1u << slot);
1218 sctx->descriptors_dirty |=
1219 1u << si_const_and_shader_buffer_descriptors_idx(shader);
1220 continue;
1221 }
1222
1223 buf = (struct r600_resource *)sbuffer->buffer;
1224 va = buf->gpu_address + sbuffer->buffer_offset;
1225
1226 desc[0] = va;
1227 desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32) |
1228 S_008F04_STRIDE(0);
1229 desc[2] = sbuffer->buffer_size;
1230 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1231 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1232 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1233 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1234 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
1235 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
1236
1237 pipe_resource_reference(&buffers->buffers[slot], &buf->b.b);
1238 radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx, buf,
1239 buffers->shader_usage,
1240 buffers->priority, true);
1241 buf->bind_history |= PIPE_BIND_SHADER_BUFFER;
1242
1243 buffers->enabled_mask |= 1u << slot;
1244 sctx->descriptors_dirty |=
1245 1u << si_const_and_shader_buffer_descriptors_idx(shader);
1246
1247 util_range_add(&buf->valid_buffer_range, sbuffer->buffer_offset,
1248 sbuffer->buffer_offset + sbuffer->buffer_size);
1249 }
1250 }
1251
1252 void si_get_shader_buffers(struct si_context *sctx,
1253 enum pipe_shader_type shader,
1254 uint start_slot, uint count,
1255 struct pipe_shader_buffer *sbuf)
1256 {
1257 struct si_buffer_resources *buffers = &sctx->const_and_shader_buffers[shader];
1258 struct si_descriptors *descs = si_const_and_shader_buffer_descriptors(sctx, shader);
1259
1260 for (unsigned i = 0; i < count; ++i) {
1261 si_get_buffer_from_descriptors(
1262 buffers, descs,
1263 si_get_shaderbuf_slot(start_slot + i),
1264 &sbuf[i].buffer, &sbuf[i].buffer_offset,
1265 &sbuf[i].buffer_size);
1266 }
1267 }
1268
1269 /* RING BUFFERS */
1270
1271 void si_set_ring_buffer(struct pipe_context *ctx, uint slot,
1272 struct pipe_resource *buffer,
1273 unsigned stride, unsigned num_records,
1274 bool add_tid, bool swizzle,
1275 unsigned element_size, unsigned index_stride, uint64_t offset)
1276 {
1277 struct si_context *sctx = (struct si_context *)ctx;
1278 struct si_buffer_resources *buffers = &sctx->rw_buffers;
1279 struct si_descriptors *descs = &sctx->descriptors[SI_DESCS_RW_BUFFERS];
1280
1281 /* The stride field in the resource descriptor has 14 bits */
1282 assert(stride < (1 << 14));
1283
1284 assert(slot < descs->num_elements);
1285 pipe_resource_reference(&buffers->buffers[slot], NULL);
1286
1287 if (buffer) {
1288 uint64_t va;
1289
1290 va = r600_resource(buffer)->gpu_address + offset;
1291
1292 switch (element_size) {
1293 default:
1294 assert(!"Unsupported ring buffer element size");
1295 case 0:
1296 case 2:
1297 element_size = 0;
1298 break;
1299 case 4:
1300 element_size = 1;
1301 break;
1302 case 8:
1303 element_size = 2;
1304 break;
1305 case 16:
1306 element_size = 3;
1307 break;
1308 }
1309
1310 switch (index_stride) {
1311 default:
1312 assert(!"Unsupported ring buffer index stride");
1313 case 0:
1314 case 8:
1315 index_stride = 0;
1316 break;
1317 case 16:
1318 index_stride = 1;
1319 break;
1320 case 32:
1321 index_stride = 2;
1322 break;
1323 case 64:
1324 index_stride = 3;
1325 break;
1326 }
1327
1328 if (sctx->b.chip_class >= VI && stride)
1329 num_records *= stride;
1330
1331 /* Set the descriptor. */
1332 uint32_t *desc = descs->list + slot*4;
1333 desc[0] = va;
1334 desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32) |
1335 S_008F04_STRIDE(stride) |
1336 S_008F04_SWIZZLE_ENABLE(swizzle);
1337 desc[2] = num_records;
1338 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1339 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1340 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1341 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1342 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
1343 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32) |
1344 S_008F0C_INDEX_STRIDE(index_stride) |
1345 S_008F0C_ADD_TID_ENABLE(add_tid);
1346
1347 if (sctx->b.chip_class >= GFX9)
1348 assert(!swizzle || element_size == 1); /* always 4 bytes on GFX9 */
1349 else
1350 desc[3] |= S_008F0C_ELEMENT_SIZE(element_size);
1351
1352 pipe_resource_reference(&buffers->buffers[slot], buffer);
1353 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
1354 (struct r600_resource*)buffer,
1355 buffers->shader_usage, buffers->priority);
1356 buffers->enabled_mask |= 1u << slot;
1357 } else {
1358 /* Clear the descriptor. */
1359 memset(descs->list + slot*4, 0, sizeof(uint32_t) * 4);
1360 buffers->enabled_mask &= ~(1u << slot);
1361 }
1362
1363 sctx->descriptors_dirty |= 1u << SI_DESCS_RW_BUFFERS;
1364 }
1365
1366 /* STREAMOUT BUFFERS */
1367
1368 static void si_set_streamout_targets(struct pipe_context *ctx,
1369 unsigned num_targets,
1370 struct pipe_stream_output_target **targets,
1371 const unsigned *offsets)
1372 {
1373 struct si_context *sctx = (struct si_context *)ctx;
1374 struct si_buffer_resources *buffers = &sctx->rw_buffers;
1375 struct si_descriptors *descs = &sctx->descriptors[SI_DESCS_RW_BUFFERS];
1376 unsigned old_num_targets = sctx->streamout.num_targets;
1377 unsigned i, bufidx;
1378
1379 /* We are going to unbind the buffers. Mark which caches need to be flushed. */
1380 if (sctx->streamout.num_targets && sctx->streamout.begin_emitted) {
1381 /* Since streamout uses vector writes which go through TC L2
1382 * and most other clients can use TC L2 as well, we don't need
1383 * to flush it.
1384 *
1385 * The only cases which requires flushing it is VGT DMA index
1386 * fetching (on <= CIK) and indirect draw data, which are rare
1387 * cases. Thus, flag the TC L2 dirtiness in the resource and
1388 * handle it at draw call time.
1389 */
1390 for (i = 0; i < sctx->streamout.num_targets; i++)
1391 if (sctx->streamout.targets[i])
1392 r600_resource(sctx->streamout.targets[i]->b.buffer)->TC_L2_dirty = true;
1393
1394 /* Invalidate the scalar cache in case a streamout buffer is
1395 * going to be used as a constant buffer.
1396 *
1397 * Invalidate TC L1, because streamout bypasses it (done by
1398 * setting GLC=1 in the store instruction), but it can contain
1399 * outdated data of streamout buffers.
1400 *
1401 * VS_PARTIAL_FLUSH is required if the buffers are going to be
1402 * used as an input immediately.
1403 */
1404 sctx->b.flags |= SI_CONTEXT_INV_SMEM_L1 |
1405 SI_CONTEXT_INV_VMEM_L1 |
1406 SI_CONTEXT_VS_PARTIAL_FLUSH;
1407 }
1408
1409 /* All readers of the streamout targets need to be finished before we can
1410 * start writing to the targets.
1411 */
1412 if (num_targets)
1413 sctx->b.flags |= SI_CONTEXT_PS_PARTIAL_FLUSH |
1414 SI_CONTEXT_CS_PARTIAL_FLUSH;
1415
1416 /* Streamout buffers must be bound in 2 places:
1417 * 1) in VGT by setting the VGT_STRMOUT registers
1418 * 2) as shader resources
1419 */
1420
1421 /* Set the VGT regs. */
1422 si_common_set_streamout_targets(ctx, num_targets, targets, offsets);
1423
1424 /* Set the shader resources.*/
1425 for (i = 0; i < num_targets; i++) {
1426 bufidx = SI_VS_STREAMOUT_BUF0 + i;
1427
1428 if (targets[i]) {
1429 struct pipe_resource *buffer = targets[i]->buffer;
1430 uint64_t va = r600_resource(buffer)->gpu_address;
1431
1432 /* Set the descriptor.
1433 *
1434 * On VI, the format must be non-INVALID, otherwise
1435 * the buffer will be considered not bound and store
1436 * instructions will be no-ops.
1437 */
1438 uint32_t *desc = descs->list + bufidx*4;
1439 desc[0] = va;
1440 desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32);
1441 desc[2] = 0xffffffff;
1442 desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1443 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1444 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1445 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1446 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
1447
1448 /* Set the resource. */
1449 pipe_resource_reference(&buffers->buffers[bufidx],
1450 buffer);
1451 radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx,
1452 (struct r600_resource*)buffer,
1453 buffers->shader_usage,
1454 RADEON_PRIO_SHADER_RW_BUFFER,
1455 true);
1456 r600_resource(buffer)->bind_history |= PIPE_BIND_STREAM_OUTPUT;
1457
1458 buffers->enabled_mask |= 1u << bufidx;
1459 } else {
1460 /* Clear the descriptor and unset the resource. */
1461 memset(descs->list + bufidx*4, 0,
1462 sizeof(uint32_t) * 4);
1463 pipe_resource_reference(&buffers->buffers[bufidx],
1464 NULL);
1465 buffers->enabled_mask &= ~(1u << bufidx);
1466 }
1467 }
1468 for (; i < old_num_targets; i++) {
1469 bufidx = SI_VS_STREAMOUT_BUF0 + i;
1470 /* Clear the descriptor and unset the resource. */
1471 memset(descs->list + bufidx*4, 0, sizeof(uint32_t) * 4);
1472 pipe_resource_reference(&buffers->buffers[bufidx], NULL);
1473 buffers->enabled_mask &= ~(1u << bufidx);
1474 }
1475
1476 sctx->descriptors_dirty |= 1u << SI_DESCS_RW_BUFFERS;
1477 }
1478
1479 static void si_desc_reset_buffer_offset(struct pipe_context *ctx,
1480 uint32_t *desc, uint64_t old_buf_va,
1481 struct pipe_resource *new_buf)
1482 {
1483 /* Retrieve the buffer offset from the descriptor. */
1484 uint64_t old_desc_va =
1485 desc[0] | ((uint64_t)G_008F04_BASE_ADDRESS_HI(desc[1]) << 32);
1486
1487 assert(old_buf_va <= old_desc_va);
1488 uint64_t offset_within_buffer = old_desc_va - old_buf_va;
1489
1490 /* Update the descriptor. */
1491 si_set_buf_desc_address(r600_resource(new_buf), offset_within_buffer,
1492 desc);
1493 }
1494
1495 /* INTERNAL CONST BUFFERS */
1496
1497 static void si_set_polygon_stipple(struct pipe_context *ctx,
1498 const struct pipe_poly_stipple *state)
1499 {
1500 struct si_context *sctx = (struct si_context *)ctx;
1501 struct pipe_constant_buffer cb = {};
1502 unsigned stipple[32];
1503 int i;
1504
1505 for (i = 0; i < 32; i++)
1506 stipple[i] = util_bitreverse(state->stipple[i]);
1507
1508 cb.user_buffer = stipple;
1509 cb.buffer_size = sizeof(stipple);
1510
1511 si_set_rw_buffer(sctx, SI_PS_CONST_POLY_STIPPLE, &cb);
1512 }
1513
1514 /* TEXTURE METADATA ENABLE/DISABLE */
1515
1516 static void
1517 si_resident_handles_update_needs_color_decompress(struct si_context *sctx)
1518 {
1519 util_dynarray_clear(&sctx->resident_tex_needs_color_decompress);
1520 util_dynarray_clear(&sctx->resident_img_needs_color_decompress);
1521
1522 util_dynarray_foreach(&sctx->resident_tex_handles,
1523 struct si_texture_handle *, tex_handle) {
1524 struct pipe_resource *res = (*tex_handle)->view->texture;
1525 struct r600_texture *rtex;
1526
1527 if (!res || res->target == PIPE_BUFFER)
1528 continue;
1529
1530 rtex = (struct r600_texture *)res;
1531 if (!color_needs_decompression(rtex))
1532 continue;
1533
1534 util_dynarray_append(&sctx->resident_tex_needs_color_decompress,
1535 struct si_texture_handle *, *tex_handle);
1536 }
1537
1538 util_dynarray_foreach(&sctx->resident_img_handles,
1539 struct si_image_handle *, img_handle) {
1540 struct pipe_image_view *view = &(*img_handle)->view;
1541 struct pipe_resource *res = view->resource;
1542 struct r600_texture *rtex;
1543
1544 if (!res || res->target == PIPE_BUFFER)
1545 continue;
1546
1547 rtex = (struct r600_texture *)res;
1548 if (!color_needs_decompression(rtex))
1549 continue;
1550
1551 util_dynarray_append(&sctx->resident_img_needs_color_decompress,
1552 struct si_image_handle *, *img_handle);
1553 }
1554 }
1555
1556 /* CMASK can be enabled (for fast clear) and disabled (for texture export)
1557 * while the texture is bound, possibly by a different context. In that case,
1558 * call this function to update needs_*_decompress_masks.
1559 */
1560 void si_update_needs_color_decompress_masks(struct si_context *sctx)
1561 {
1562 for (int i = 0; i < SI_NUM_SHADERS; ++i) {
1563 si_samplers_update_needs_color_decompress_mask(&sctx->samplers[i]);
1564 si_images_update_needs_color_decompress_mask(&sctx->images[i]);
1565 si_update_shader_needs_decompress_mask(sctx, i);
1566 }
1567
1568 si_resident_handles_update_needs_color_decompress(sctx);
1569 }
1570
1571 /* BUFFER DISCARD/INVALIDATION */
1572
1573 /** Reset descriptors of buffer resources after \p buf has been invalidated. */
1574 static void si_reset_buffer_resources(struct si_context *sctx,
1575 struct si_buffer_resources *buffers,
1576 unsigned descriptors_idx,
1577 unsigned slot_mask,
1578 struct pipe_resource *buf,
1579 uint64_t old_va,
1580 enum radeon_bo_usage usage,
1581 enum radeon_bo_priority priority)
1582 {
1583 struct si_descriptors *descs = &sctx->descriptors[descriptors_idx];
1584 unsigned mask = buffers->enabled_mask & slot_mask;
1585
1586 while (mask) {
1587 unsigned i = u_bit_scan(&mask);
1588 if (buffers->buffers[i] == buf) {
1589 si_desc_reset_buffer_offset(&sctx->b.b,
1590 descs->list + i*4,
1591 old_va, buf);
1592 sctx->descriptors_dirty |= 1u << descriptors_idx;
1593
1594 radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx,
1595 (struct r600_resource *)buf,
1596 usage, priority, true);
1597 }
1598 }
1599 }
1600
1601 static void si_rebind_buffer(struct pipe_context *ctx, struct pipe_resource *buf,
1602 uint64_t old_va)
1603 {
1604 struct si_context *sctx = (struct si_context*)ctx;
1605 struct r600_resource *rbuffer = r600_resource(buf);
1606 unsigned i, shader;
1607 unsigned num_elems = sctx->vertex_elements ?
1608 sctx->vertex_elements->count : 0;
1609
1610 /* We changed the buffer, now we need to bind it where the old one
1611 * was bound. This consists of 2 things:
1612 * 1) Updating the resource descriptor and dirtying it.
1613 * 2) Adding a relocation to the CS, so that it's usable.
1614 */
1615
1616 /* Vertex buffers. */
1617 if (rbuffer->bind_history & PIPE_BIND_VERTEX_BUFFER) {
1618 for (i = 0; i < num_elems; i++) {
1619 int vb = sctx->vertex_elements->vertex_buffer_index[i];
1620
1621 if (vb >= ARRAY_SIZE(sctx->vertex_buffer))
1622 continue;
1623 if (!sctx->vertex_buffer[vb].buffer.resource)
1624 continue;
1625
1626 if (sctx->vertex_buffer[vb].buffer.resource == buf) {
1627 sctx->vertex_buffers_dirty = true;
1628 break;
1629 }
1630 }
1631 }
1632
1633 /* Streamout buffers. (other internal buffers can't be invalidated) */
1634 if (rbuffer->bind_history & PIPE_BIND_STREAM_OUTPUT) {
1635 for (i = SI_VS_STREAMOUT_BUF0; i <= SI_VS_STREAMOUT_BUF3; i++) {
1636 struct si_buffer_resources *buffers = &sctx->rw_buffers;
1637 struct si_descriptors *descs =
1638 &sctx->descriptors[SI_DESCS_RW_BUFFERS];
1639
1640 if (buffers->buffers[i] != buf)
1641 continue;
1642
1643 si_desc_reset_buffer_offset(ctx, descs->list + i*4,
1644 old_va, buf);
1645 sctx->descriptors_dirty |= 1u << SI_DESCS_RW_BUFFERS;
1646
1647 radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx,
1648 rbuffer, buffers->shader_usage,
1649 RADEON_PRIO_SHADER_RW_BUFFER,
1650 true);
1651
1652 /* Update the streamout state. */
1653 if (sctx->streamout.begin_emitted)
1654 si_emit_streamout_end(sctx);
1655 sctx->streamout.append_bitmask =
1656 sctx->streamout.enabled_mask;
1657 si_streamout_buffers_dirty(sctx);
1658 }
1659 }
1660
1661 /* Constant and shader buffers. */
1662 if (rbuffer->bind_history & PIPE_BIND_CONSTANT_BUFFER) {
1663 for (shader = 0; shader < SI_NUM_SHADERS; shader++)
1664 si_reset_buffer_resources(sctx, &sctx->const_and_shader_buffers[shader],
1665 si_const_and_shader_buffer_descriptors_idx(shader),
1666 u_bit_consecutive(SI_NUM_SHADER_BUFFERS, SI_NUM_CONST_BUFFERS),
1667 buf, old_va,
1668 sctx->const_and_shader_buffers[shader].shader_usage_constbuf,
1669 sctx->const_and_shader_buffers[shader].priority_constbuf);
1670 }
1671
1672 if (rbuffer->bind_history & PIPE_BIND_SHADER_BUFFER) {
1673 for (shader = 0; shader < SI_NUM_SHADERS; shader++)
1674 si_reset_buffer_resources(sctx, &sctx->const_and_shader_buffers[shader],
1675 si_const_and_shader_buffer_descriptors_idx(shader),
1676 u_bit_consecutive(0, SI_NUM_SHADER_BUFFERS),
1677 buf, old_va,
1678 sctx->const_and_shader_buffers[shader].shader_usage,
1679 sctx->const_and_shader_buffers[shader].priority);
1680 }
1681
1682 if (rbuffer->bind_history & PIPE_BIND_SAMPLER_VIEW) {
1683 /* Texture buffers - update bindings. */
1684 for (shader = 0; shader < SI_NUM_SHADERS; shader++) {
1685 struct si_samplers *samplers = &sctx->samplers[shader];
1686 struct si_descriptors *descs =
1687 si_sampler_and_image_descriptors(sctx, shader);
1688 unsigned mask = samplers->enabled_mask;
1689
1690 while (mask) {
1691 unsigned i = u_bit_scan(&mask);
1692 if (samplers->views[i]->texture == buf) {
1693 unsigned desc_slot = si_get_sampler_slot(i);
1694
1695 si_desc_reset_buffer_offset(ctx,
1696 descs->list +
1697 desc_slot * 16 + 4,
1698 old_va, buf);
1699 sctx->descriptors_dirty |=
1700 1u << si_sampler_and_image_descriptors_idx(shader);
1701
1702 radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx,
1703 rbuffer, RADEON_USAGE_READ,
1704 RADEON_PRIO_SAMPLER_BUFFER,
1705 true);
1706 }
1707 }
1708 }
1709 }
1710
1711 /* Shader images */
1712 if (rbuffer->bind_history & PIPE_BIND_SHADER_IMAGE) {
1713 for (shader = 0; shader < SI_NUM_SHADERS; ++shader) {
1714 struct si_images *images = &sctx->images[shader];
1715 struct si_descriptors *descs =
1716 si_sampler_and_image_descriptors(sctx, shader);
1717 unsigned mask = images->enabled_mask;
1718
1719 while (mask) {
1720 unsigned i = u_bit_scan(&mask);
1721
1722 if (images->views[i].resource == buf) {
1723 unsigned desc_slot = si_get_image_slot(i);
1724
1725 if (images->views[i].access & PIPE_IMAGE_ACCESS_WRITE)
1726 si_mark_image_range_valid(&images->views[i]);
1727
1728 si_desc_reset_buffer_offset(
1729 ctx, descs->list + desc_slot * 8 + 4,
1730 old_va, buf);
1731 sctx->descriptors_dirty |=
1732 1u << si_sampler_and_image_descriptors_idx(shader);
1733
1734 radeon_add_to_buffer_list_check_mem(
1735 &sctx->b, &sctx->b.gfx, rbuffer,
1736 RADEON_USAGE_READWRITE,
1737 RADEON_PRIO_SAMPLER_BUFFER, true);
1738 }
1739 }
1740 }
1741 }
1742
1743 /* Bindless texture handles */
1744 if (rbuffer->texture_handle_allocated) {
1745 struct si_descriptors *descs = &sctx->bindless_descriptors;
1746
1747 util_dynarray_foreach(&sctx->resident_tex_handles,
1748 struct si_texture_handle *, tex_handle) {
1749 struct pipe_sampler_view *view = (*tex_handle)->view;
1750 unsigned desc_slot = (*tex_handle)->desc_slot;
1751
1752 if (view->texture == buf) {
1753 si_set_buf_desc_address(rbuffer,
1754 view->u.buf.offset,
1755 descs->list +
1756 desc_slot * 16 + 4);
1757
1758 (*tex_handle)->desc_dirty = true;
1759 sctx->bindless_descriptors_dirty = true;
1760
1761 radeon_add_to_buffer_list_check_mem(
1762 &sctx->b, &sctx->b.gfx, rbuffer,
1763 RADEON_USAGE_READ,
1764 RADEON_PRIO_SAMPLER_BUFFER, true);
1765 }
1766 }
1767 }
1768
1769 /* Bindless image handles */
1770 if (rbuffer->image_handle_allocated) {
1771 struct si_descriptors *descs = &sctx->bindless_descriptors;
1772
1773 util_dynarray_foreach(&sctx->resident_img_handles,
1774 struct si_image_handle *, img_handle) {
1775 struct pipe_image_view *view = &(*img_handle)->view;
1776 unsigned desc_slot = (*img_handle)->desc_slot;
1777
1778 if (view->resource == buf) {
1779 if (view->access & PIPE_IMAGE_ACCESS_WRITE)
1780 si_mark_image_range_valid(view);
1781
1782 si_set_buf_desc_address(rbuffer,
1783 view->u.buf.offset,
1784 descs->list +
1785 desc_slot * 16 + 4);
1786
1787 (*img_handle)->desc_dirty = true;
1788 sctx->bindless_descriptors_dirty = true;
1789
1790 radeon_add_to_buffer_list_check_mem(
1791 &sctx->b, &sctx->b.gfx, rbuffer,
1792 RADEON_USAGE_READWRITE,
1793 RADEON_PRIO_SAMPLER_BUFFER, true);
1794 }
1795 }
1796 }
1797 }
1798
1799 /* Reallocate a buffer a update all resource bindings where the buffer is
1800 * bound.
1801 *
1802 * This is used to avoid CPU-GPU synchronizations, because it makes the buffer
1803 * idle by discarding its contents. Apps usually tell us when to do this using
1804 * map_buffer flags, for example.
1805 */
1806 static void si_invalidate_buffer(struct pipe_context *ctx, struct pipe_resource *buf)
1807 {
1808 struct si_context *sctx = (struct si_context*)ctx;
1809 struct r600_resource *rbuffer = r600_resource(buf);
1810 uint64_t old_va = rbuffer->gpu_address;
1811
1812 /* Reallocate the buffer in the same pipe_resource. */
1813 si_alloc_resource(&sctx->screen->b, rbuffer);
1814
1815 si_rebind_buffer(ctx, buf, old_va);
1816 }
1817
1818 static void si_upload_bindless_descriptor(struct si_context *sctx,
1819 unsigned desc_slot,
1820 unsigned num_dwords)
1821 {
1822 struct si_descriptors *desc = &sctx->bindless_descriptors;
1823 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
1824 unsigned desc_slot_offset = desc_slot * 16;
1825 uint32_t *data;
1826 uint64_t va;
1827
1828 data = desc->list + desc_slot_offset;
1829
1830 va = desc->buffer->gpu_address + desc->buffer_offset +
1831 desc_slot_offset * 4;
1832
1833 radeon_emit(cs, PKT3(PKT3_WRITE_DATA, 2 + num_dwords, 0));
1834 radeon_emit(cs, S_370_DST_SEL(V_370_TC_L2) |
1835 S_370_WR_CONFIRM(1) |
1836 S_370_ENGINE_SEL(V_370_ME));
1837 radeon_emit(cs, va);
1838 radeon_emit(cs, va >> 32);
1839 radeon_emit_array(cs, data, num_dwords);
1840 }
1841
1842 static void si_upload_bindless_descriptors(struct si_context *sctx)
1843 {
1844 if (!sctx->bindless_descriptors_dirty)
1845 return;
1846
1847 /* Wait for graphics/compute to be idle before updating the resident
1848 * descriptors directly in memory, in case the GPU is using them.
1849 */
1850 sctx->b.flags |= SI_CONTEXT_PS_PARTIAL_FLUSH |
1851 SI_CONTEXT_CS_PARTIAL_FLUSH;
1852 si_emit_cache_flush(sctx);
1853
1854 util_dynarray_foreach(&sctx->resident_tex_handles,
1855 struct si_texture_handle *, tex_handle) {
1856 unsigned desc_slot = (*tex_handle)->desc_slot;
1857
1858 if (!(*tex_handle)->desc_dirty)
1859 continue;
1860
1861 si_upload_bindless_descriptor(sctx, desc_slot, 16);
1862 (*tex_handle)->desc_dirty = false;
1863 }
1864
1865 util_dynarray_foreach(&sctx->resident_img_handles,
1866 struct si_image_handle *, img_handle) {
1867 unsigned desc_slot = (*img_handle)->desc_slot;
1868
1869 if (!(*img_handle)->desc_dirty)
1870 continue;
1871
1872 si_upload_bindless_descriptor(sctx, desc_slot, 8);
1873 (*img_handle)->desc_dirty = false;
1874 }
1875
1876 /* Invalidate L1 because it doesn't know that L2 changed. */
1877 sctx->b.flags |= SI_CONTEXT_INV_SMEM_L1;
1878 si_emit_cache_flush(sctx);
1879
1880 sctx->bindless_descriptors_dirty = false;
1881 }
1882
1883 /* Update mutable image descriptor fields of all resident textures. */
1884 static void si_update_bindless_texture_descriptor(struct si_context *sctx,
1885 struct si_texture_handle *tex_handle)
1886 {
1887 struct si_sampler_view *sview = (struct si_sampler_view *)tex_handle->view;
1888 struct si_descriptors *desc = &sctx->bindless_descriptors;
1889 unsigned desc_slot_offset = tex_handle->desc_slot * 16;
1890 uint32_t desc_list[16];
1891
1892 if (sview->base.texture->target == PIPE_BUFFER)
1893 return;
1894
1895 memcpy(desc_list, desc->list + desc_slot_offset, sizeof(desc_list));
1896 si_set_sampler_view_desc(sctx, sview, &tex_handle->sstate,
1897 desc->list + desc_slot_offset);
1898
1899 if (memcmp(desc_list, desc->list + desc_slot_offset,
1900 sizeof(desc_list))) {
1901 tex_handle->desc_dirty = true;
1902 sctx->bindless_descriptors_dirty = true;
1903 }
1904 }
1905
1906 static void si_update_bindless_image_descriptor(struct si_context *sctx,
1907 struct si_image_handle *img_handle)
1908 {
1909 struct si_descriptors *desc = &sctx->bindless_descriptors;
1910 unsigned desc_slot_offset = img_handle->desc_slot * 16;
1911 struct pipe_image_view *view = &img_handle->view;
1912 uint32_t desc_list[8];
1913
1914 if (view->resource->target == PIPE_BUFFER)
1915 return;
1916
1917 memcpy(desc_list, desc->list + desc_slot_offset,
1918 sizeof(desc_list));
1919 si_set_shader_image_desc(sctx, view, true,
1920 desc->list + desc_slot_offset);
1921
1922 if (memcmp(desc_list, desc->list + desc_slot_offset,
1923 sizeof(desc_list))) {
1924 img_handle->desc_dirty = true;
1925 sctx->bindless_descriptors_dirty = true;
1926 }
1927 }
1928
1929 static void si_update_all_resident_texture_descriptors(struct si_context *sctx)
1930 {
1931 util_dynarray_foreach(&sctx->resident_tex_handles,
1932 struct si_texture_handle *, tex_handle) {
1933 si_update_bindless_texture_descriptor(sctx, *tex_handle);
1934 }
1935
1936 util_dynarray_foreach(&sctx->resident_img_handles,
1937 struct si_image_handle *, img_handle) {
1938 si_update_bindless_image_descriptor(sctx, *img_handle);
1939 }
1940
1941 si_upload_bindless_descriptors(sctx);
1942 }
1943
1944 /* Update mutable image descriptor fields of all bound textures. */
1945 void si_update_all_texture_descriptors(struct si_context *sctx)
1946 {
1947 unsigned shader;
1948
1949 for (shader = 0; shader < SI_NUM_SHADERS; shader++) {
1950 struct si_samplers *samplers = &sctx->samplers[shader];
1951 struct si_images *images = &sctx->images[shader];
1952 unsigned mask;
1953
1954 /* Images. */
1955 mask = images->enabled_mask;
1956 while (mask) {
1957 unsigned i = u_bit_scan(&mask);
1958 struct pipe_image_view *view = &images->views[i];
1959
1960 if (!view->resource ||
1961 view->resource->target == PIPE_BUFFER)
1962 continue;
1963
1964 si_set_shader_image(sctx, shader, i, view, true);
1965 }
1966
1967 /* Sampler views. */
1968 mask = samplers->enabled_mask;
1969 while (mask) {
1970 unsigned i = u_bit_scan(&mask);
1971 struct pipe_sampler_view *view = samplers->views[i];
1972
1973 if (!view ||
1974 !view->texture ||
1975 view->texture->target == PIPE_BUFFER)
1976 continue;
1977
1978 si_set_sampler_view(sctx, shader, i,
1979 samplers->views[i], true);
1980 }
1981
1982 si_update_shader_needs_decompress_mask(sctx, shader);
1983 }
1984
1985 si_update_all_resident_texture_descriptors(sctx);
1986 }
1987
1988 /* SHADER USER DATA */
1989
1990 static void si_mark_shader_pointers_dirty(struct si_context *sctx,
1991 unsigned shader)
1992 {
1993 sctx->shader_pointers_dirty |=
1994 u_bit_consecutive(SI_DESCS_FIRST_SHADER + shader * SI_NUM_SHADER_DESCS,
1995 SI_NUM_SHADER_DESCS);
1996
1997 if (shader == PIPE_SHADER_VERTEX)
1998 sctx->vertex_buffer_pointer_dirty = sctx->vertex_buffers.buffer != NULL;
1999
2000 si_mark_atom_dirty(sctx, &sctx->shader_pointers.atom);
2001 }
2002
2003 static void si_shader_pointers_begin_new_cs(struct si_context *sctx)
2004 {
2005 sctx->shader_pointers_dirty = u_bit_consecutive(0, SI_NUM_DESCS);
2006 sctx->vertex_buffer_pointer_dirty = sctx->vertex_buffers.buffer != NULL;
2007 si_mark_atom_dirty(sctx, &sctx->shader_pointers.atom);
2008 sctx->graphics_bindless_pointer_dirty = sctx->bindless_descriptors.buffer != NULL;
2009 sctx->compute_bindless_pointer_dirty = sctx->bindless_descriptors.buffer != NULL;
2010 }
2011
2012 /* Set a base register address for user data constants in the given shader.
2013 * This assigns a mapping from PIPE_SHADER_* to SPI_SHADER_USER_DATA_*.
2014 */
2015 static void si_set_user_data_base(struct si_context *sctx,
2016 unsigned shader, uint32_t new_base)
2017 {
2018 uint32_t *base = &sctx->shader_pointers.sh_base[shader];
2019
2020 if (*base != new_base) {
2021 *base = new_base;
2022
2023 if (new_base) {
2024 si_mark_shader_pointers_dirty(sctx, shader);
2025
2026 if (shader == PIPE_SHADER_VERTEX)
2027 sctx->last_vs_state = ~0;
2028 }
2029 }
2030 }
2031
2032 /* This must be called when these shaders are changed from non-NULL to NULL
2033 * and vice versa:
2034 * - geometry shader
2035 * - tessellation control shader
2036 * - tessellation evaluation shader
2037 */
2038 void si_shader_change_notify(struct si_context *sctx)
2039 {
2040 /* VS can be bound as VS, ES, or LS. */
2041 if (sctx->tes_shader.cso) {
2042 if (sctx->b.chip_class >= GFX9) {
2043 si_set_user_data_base(sctx, PIPE_SHADER_VERTEX,
2044 R_00B430_SPI_SHADER_USER_DATA_LS_0);
2045 } else {
2046 si_set_user_data_base(sctx, PIPE_SHADER_VERTEX,
2047 R_00B530_SPI_SHADER_USER_DATA_LS_0);
2048 }
2049 } else if (sctx->gs_shader.cso) {
2050 si_set_user_data_base(sctx, PIPE_SHADER_VERTEX,
2051 R_00B330_SPI_SHADER_USER_DATA_ES_0);
2052 } else {
2053 si_set_user_data_base(sctx, PIPE_SHADER_VERTEX,
2054 R_00B130_SPI_SHADER_USER_DATA_VS_0);
2055 }
2056
2057 /* TES can be bound as ES, VS, or not bound. */
2058 if (sctx->tes_shader.cso) {
2059 if (sctx->gs_shader.cso)
2060 si_set_user_data_base(sctx, PIPE_SHADER_TESS_EVAL,
2061 R_00B330_SPI_SHADER_USER_DATA_ES_0);
2062 else
2063 si_set_user_data_base(sctx, PIPE_SHADER_TESS_EVAL,
2064 R_00B130_SPI_SHADER_USER_DATA_VS_0);
2065 } else {
2066 si_set_user_data_base(sctx, PIPE_SHADER_TESS_EVAL, 0);
2067 }
2068 }
2069
2070 static void si_emit_shader_pointer(struct si_context *sctx,
2071 struct si_descriptors *desc,
2072 unsigned sh_base)
2073 {
2074 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
2075 uint64_t va;
2076
2077 if (!desc->buffer)
2078 return; /* the pointer is not used by current shaders */
2079
2080 va = desc->buffer->gpu_address +
2081 desc->buffer_offset;
2082
2083 radeon_emit(cs, PKT3(PKT3_SET_SH_REG, 2, 0));
2084 radeon_emit(cs, (sh_base + desc->shader_userdata_offset - SI_SH_REG_OFFSET) >> 2);
2085 radeon_emit(cs, va);
2086 radeon_emit(cs, va >> 32);
2087 }
2088
2089 static void si_emit_global_shader_pointers(struct si_context *sctx,
2090 struct si_descriptors *descs)
2091 {
2092 si_emit_shader_pointer(sctx, descs,
2093 R_00B030_SPI_SHADER_USER_DATA_PS_0);
2094 si_emit_shader_pointer(sctx, descs,
2095 R_00B130_SPI_SHADER_USER_DATA_VS_0);
2096
2097 if (sctx->b.chip_class >= GFX9) {
2098 /* GFX9 merged LS-HS and ES-GS. */
2099 if (descs == &sctx->descriptors[SI_DESCS_RW_BUFFERS]) {
2100 /* Set RW_BUFFERS in the special registers, so that
2101 * it's preloaded into s[0:1] instead of s[8:9].
2102 */
2103 si_emit_shader_pointer(sctx, descs,
2104 R_00B208_SPI_SHADER_USER_DATA_ADDR_LO_GS);
2105 si_emit_shader_pointer(sctx, descs,
2106 R_00B408_SPI_SHADER_USER_DATA_ADDR_LO_HS);
2107 } else {
2108 /* Set BINDLESS_SAMPLERS_AND_IMAGES into s[10:11],
2109 * s[8:9] remains unused for now.
2110 */
2111 assert(descs == &sctx->bindless_descriptors);
2112 si_emit_shader_pointer(sctx, descs,
2113 R_00B330_SPI_SHADER_USER_DATA_ES_0);
2114 si_emit_shader_pointer(sctx, descs,
2115 R_00B430_SPI_SHADER_USER_DATA_LS_0);
2116 }
2117 } else {
2118 si_emit_shader_pointer(sctx, descs,
2119 R_00B230_SPI_SHADER_USER_DATA_GS_0);
2120 si_emit_shader_pointer(sctx, descs,
2121 R_00B330_SPI_SHADER_USER_DATA_ES_0);
2122 si_emit_shader_pointer(sctx, descs,
2123 R_00B430_SPI_SHADER_USER_DATA_HS_0);
2124 si_emit_shader_pointer(sctx, descs,
2125 R_00B530_SPI_SHADER_USER_DATA_LS_0);
2126 }
2127 }
2128
2129 void si_emit_graphics_shader_pointers(struct si_context *sctx,
2130 struct r600_atom *atom)
2131 {
2132 unsigned mask;
2133 uint32_t *sh_base = sctx->shader_pointers.sh_base;
2134 struct si_descriptors *descs;
2135
2136 descs = &sctx->descriptors[SI_DESCS_RW_BUFFERS];
2137
2138 if (sctx->shader_pointers_dirty & (1 << SI_DESCS_RW_BUFFERS))
2139 si_emit_global_shader_pointers(sctx, descs);
2140
2141 mask = sctx->shader_pointers_dirty &
2142 u_bit_consecutive(SI_DESCS_FIRST_SHADER,
2143 SI_DESCS_FIRST_COMPUTE - SI_DESCS_FIRST_SHADER);
2144
2145 while (mask) {
2146 unsigned i = u_bit_scan(&mask);
2147 unsigned shader = (i - SI_DESCS_FIRST_SHADER) / SI_NUM_SHADER_DESCS;
2148 unsigned base = sh_base[shader];
2149
2150 if (base)
2151 si_emit_shader_pointer(sctx, descs + i, base);
2152 }
2153 sctx->shader_pointers_dirty &=
2154 ~u_bit_consecutive(SI_DESCS_RW_BUFFERS, SI_DESCS_FIRST_COMPUTE);
2155
2156 if (sctx->vertex_buffer_pointer_dirty) {
2157 si_emit_shader_pointer(sctx, &sctx->vertex_buffers,
2158 sh_base[PIPE_SHADER_VERTEX]);
2159 sctx->vertex_buffer_pointer_dirty = false;
2160 }
2161
2162 if (sctx->graphics_bindless_pointer_dirty) {
2163 si_emit_global_shader_pointers(sctx,
2164 &sctx->bindless_descriptors);
2165 sctx->graphics_bindless_pointer_dirty = false;
2166 }
2167 }
2168
2169 void si_emit_compute_shader_pointers(struct si_context *sctx)
2170 {
2171 unsigned base = R_00B900_COMPUTE_USER_DATA_0;
2172 struct si_descriptors *descs = sctx->descriptors;
2173 unsigned compute_mask =
2174 u_bit_consecutive(SI_DESCS_FIRST_COMPUTE, SI_NUM_SHADER_DESCS);
2175 unsigned mask = sctx->shader_pointers_dirty & compute_mask;
2176
2177 while (mask) {
2178 unsigned i = u_bit_scan(&mask);
2179
2180 si_emit_shader_pointer(sctx, descs + i, base);
2181 }
2182 sctx->shader_pointers_dirty &= ~compute_mask;
2183
2184 if (sctx->compute_bindless_pointer_dirty) {
2185 si_emit_shader_pointer(sctx, &sctx->bindless_descriptors, base);
2186 sctx->compute_bindless_pointer_dirty = false;
2187 }
2188 }
2189
2190 /* BINDLESS */
2191
2192 static void si_init_bindless_descriptors(struct si_context *sctx,
2193 struct si_descriptors *desc,
2194 unsigned shader_userdata_index,
2195 unsigned num_elements)
2196 {
2197 MAYBE_UNUSED unsigned desc_slot;
2198
2199 si_init_descriptors(desc, shader_userdata_index, 16, num_elements);
2200 sctx->bindless_descriptors.num_active_slots = num_elements;
2201
2202 /* The first bindless descriptor is stored at slot 1, because 0 is not
2203 * considered to be a valid handle.
2204 */
2205 sctx->num_bindless_descriptors = 1;
2206
2207 /* Track which bindless slots are used (or not). */
2208 util_idalloc_init(&sctx->bindless_used_slots);
2209 util_idalloc_resize(&sctx->bindless_used_slots, num_elements);
2210
2211 /* Reserve slot 0 because it's an invalid handle for bindless. */
2212 desc_slot = util_idalloc_alloc(&sctx->bindless_used_slots);
2213 assert(desc_slot == 0);
2214 }
2215
2216 static void si_release_bindless_descriptors(struct si_context *sctx)
2217 {
2218 si_release_descriptors(&sctx->bindless_descriptors);
2219 util_idalloc_fini(&sctx->bindless_used_slots);
2220 }
2221
2222 static unsigned si_get_first_free_bindless_slot(struct si_context *sctx)
2223 {
2224 struct si_descriptors *desc = &sctx->bindless_descriptors;
2225 unsigned desc_slot;
2226
2227 desc_slot = util_idalloc_alloc(&sctx->bindless_used_slots);
2228 if (desc_slot >= desc->num_elements) {
2229 /* The array of bindless descriptors is full, resize it. */
2230 unsigned slot_size = desc->element_dw_size * 4;
2231 unsigned new_num_elements = desc->num_elements * 2;
2232
2233 desc->list = REALLOC(desc->list, desc->num_elements * slot_size,
2234 new_num_elements * slot_size);
2235 desc->num_elements = new_num_elements;
2236 desc->num_active_slots = new_num_elements;
2237 }
2238
2239 assert(desc_slot);
2240 return desc_slot;
2241 }
2242
2243 static unsigned
2244 si_create_bindless_descriptor(struct si_context *sctx, uint32_t *desc_list,
2245 unsigned size)
2246 {
2247 struct si_descriptors *desc = &sctx->bindless_descriptors;
2248 unsigned desc_slot, desc_slot_offset;
2249
2250 /* Find a free slot. */
2251 desc_slot = si_get_first_free_bindless_slot(sctx);
2252
2253 /* For simplicity, sampler and image bindless descriptors use fixed
2254 * 16-dword slots for now. Image descriptors only need 8-dword but this
2255 * doesn't really matter because no real apps use image handles.
2256 */
2257 desc_slot_offset = desc_slot * 16;
2258
2259 /* Copy the descriptor into the array. */
2260 memcpy(desc->list + desc_slot_offset, desc_list, size);
2261
2262 /* Re-upload the whole array of bindless descriptors into a new buffer.
2263 */
2264 if (!si_upload_descriptors(sctx, desc, &sctx->shader_pointers.atom))
2265 return 0;
2266
2267 /* Make sure to re-emit the shader pointers for all stages. */
2268 sctx->graphics_bindless_pointer_dirty = true;
2269 sctx->compute_bindless_pointer_dirty = true;
2270
2271 return desc_slot;
2272 }
2273
2274 static void si_update_bindless_buffer_descriptor(struct si_context *sctx,
2275 unsigned desc_slot,
2276 struct pipe_resource *resource,
2277 uint64_t offset,
2278 bool *desc_dirty)
2279 {
2280 struct si_descriptors *desc = &sctx->bindless_descriptors;
2281 struct r600_resource *buf = r600_resource(resource);
2282 unsigned desc_slot_offset = desc_slot * 16;
2283 uint32_t *desc_list = desc->list + desc_slot_offset + 4;
2284 uint64_t old_desc_va;
2285
2286 assert(resource->target == PIPE_BUFFER);
2287
2288 /* Retrieve the old buffer addr from the descriptor. */
2289 old_desc_va = desc_list[0];
2290 old_desc_va |= ((uint64_t)G_008F04_BASE_ADDRESS_HI(desc_list[1]) << 32);
2291
2292 if (old_desc_va != buf->gpu_address + offset) {
2293 /* The buffer has been invalidated when the handle wasn't
2294 * resident, update the descriptor and the dirty flag.
2295 */
2296 si_set_buf_desc_address(buf, offset, &desc_list[0]);
2297
2298 *desc_dirty = true;
2299 }
2300 }
2301
2302 static uint64_t si_create_texture_handle(struct pipe_context *ctx,
2303 struct pipe_sampler_view *view,
2304 const struct pipe_sampler_state *state)
2305 {
2306 struct si_sampler_view *sview = (struct si_sampler_view *)view;
2307 struct si_context *sctx = (struct si_context *)ctx;
2308 struct si_texture_handle *tex_handle;
2309 struct si_sampler_state *sstate;
2310 uint32_t desc_list[16];
2311 uint64_t handle;
2312
2313 tex_handle = CALLOC_STRUCT(si_texture_handle);
2314 if (!tex_handle)
2315 return 0;
2316
2317 memset(desc_list, 0, sizeof(desc_list));
2318 si_init_descriptor_list(&desc_list[0], 16, 1, null_texture_descriptor);
2319
2320 sstate = ctx->create_sampler_state(ctx, state);
2321 if (!sstate) {
2322 FREE(tex_handle);
2323 return 0;
2324 }
2325
2326 si_set_sampler_view_desc(sctx, sview, sstate, &desc_list[0]);
2327 memcpy(&tex_handle->sstate, sstate, sizeof(*sstate));
2328 ctx->delete_sampler_state(ctx, sstate);
2329
2330 tex_handle->desc_slot = si_create_bindless_descriptor(sctx, desc_list,
2331 sizeof(desc_list));
2332 if (!tex_handle->desc_slot) {
2333 FREE(tex_handle);
2334 return 0;
2335 }
2336
2337 handle = tex_handle->desc_slot;
2338
2339 if (!_mesa_hash_table_insert(sctx->tex_handles, (void *)handle,
2340 tex_handle)) {
2341 FREE(tex_handle);
2342 return 0;
2343 }
2344
2345 pipe_sampler_view_reference(&tex_handle->view, view);
2346
2347 r600_resource(sview->base.texture)->texture_handle_allocated = true;
2348
2349 return handle;
2350 }
2351
2352 static void si_delete_texture_handle(struct pipe_context *ctx, uint64_t handle)
2353 {
2354 struct si_context *sctx = (struct si_context *)ctx;
2355 struct si_texture_handle *tex_handle;
2356 struct hash_entry *entry;
2357
2358 entry = _mesa_hash_table_search(sctx->tex_handles, (void *)handle);
2359 if (!entry)
2360 return;
2361
2362 tex_handle = (struct si_texture_handle *)entry->data;
2363
2364 /* Allow this descriptor slot to be re-used. */
2365 util_idalloc_free(&sctx->bindless_used_slots, tex_handle->desc_slot);
2366
2367 pipe_sampler_view_reference(&tex_handle->view, NULL);
2368 _mesa_hash_table_remove(sctx->tex_handles, entry);
2369 FREE(tex_handle);
2370 }
2371
2372 static void si_make_texture_handle_resident(struct pipe_context *ctx,
2373 uint64_t handle, bool resident)
2374 {
2375 struct si_context *sctx = (struct si_context *)ctx;
2376 struct si_texture_handle *tex_handle;
2377 struct si_sampler_view *sview;
2378 struct hash_entry *entry;
2379
2380 entry = _mesa_hash_table_search(sctx->tex_handles, (void *)handle);
2381 if (!entry)
2382 return;
2383
2384 tex_handle = (struct si_texture_handle *)entry->data;
2385 sview = (struct si_sampler_view *)tex_handle->view;
2386
2387 if (resident) {
2388 if (sview->base.texture->target != PIPE_BUFFER) {
2389 struct r600_texture *rtex =
2390 (struct r600_texture *)sview->base.texture;
2391
2392 if (depth_needs_decompression(rtex)) {
2393 util_dynarray_append(
2394 &sctx->resident_tex_needs_depth_decompress,
2395 struct si_texture_handle *,
2396 tex_handle);
2397 }
2398
2399 if (color_needs_decompression(rtex)) {
2400 util_dynarray_append(
2401 &sctx->resident_tex_needs_color_decompress,
2402 struct si_texture_handle *,
2403 tex_handle);
2404 }
2405
2406 if (rtex->dcc_offset &&
2407 p_atomic_read(&rtex->framebuffers_bound))
2408 sctx->need_check_render_feedback = true;
2409
2410 si_update_bindless_texture_descriptor(sctx, tex_handle);
2411 } else {
2412 si_update_bindless_buffer_descriptor(sctx,
2413 tex_handle->desc_slot,
2414 sview->base.texture,
2415 sview->base.u.buf.offset,
2416 &tex_handle->desc_dirty);
2417 }
2418
2419 /* Re-upload the descriptor if it has been updated while it
2420 * wasn't resident.
2421 */
2422 if (tex_handle->desc_dirty)
2423 sctx->bindless_descriptors_dirty = true;
2424
2425 /* Add the texture handle to the per-context list. */
2426 util_dynarray_append(&sctx->resident_tex_handles,
2427 struct si_texture_handle *, tex_handle);
2428
2429 /* Add the buffers to the current CS in case si_begin_new_cs()
2430 * is not going to be called.
2431 */
2432 si_sampler_view_add_buffer(sctx, sview->base.texture,
2433 RADEON_USAGE_READ,
2434 sview->is_stencil_sampler, false);
2435 } else {
2436 /* Remove the texture handle from the per-context list. */
2437 util_dynarray_delete_unordered(&sctx->resident_tex_handles,
2438 struct si_texture_handle *,
2439 tex_handle);
2440
2441 if (sview->base.texture->target != PIPE_BUFFER) {
2442 util_dynarray_delete_unordered(
2443 &sctx->resident_tex_needs_depth_decompress,
2444 struct si_texture_handle *, tex_handle);
2445
2446 util_dynarray_delete_unordered(
2447 &sctx->resident_tex_needs_color_decompress,
2448 struct si_texture_handle *, tex_handle);
2449 }
2450 }
2451 }
2452
2453 static uint64_t si_create_image_handle(struct pipe_context *ctx,
2454 const struct pipe_image_view *view)
2455 {
2456 struct si_context *sctx = (struct si_context *)ctx;
2457 struct si_image_handle *img_handle;
2458 uint32_t desc_list[8];
2459 uint64_t handle;
2460
2461 if (!view || !view->resource)
2462 return 0;
2463
2464 img_handle = CALLOC_STRUCT(si_image_handle);
2465 if (!img_handle)
2466 return 0;
2467
2468 memset(desc_list, 0, sizeof(desc_list));
2469 si_init_descriptor_list(&desc_list[0], 8, 1, null_image_descriptor);
2470
2471 si_set_shader_image_desc(sctx, view, false, &desc_list[0]);
2472
2473 img_handle->desc_slot = si_create_bindless_descriptor(sctx, desc_list,
2474 sizeof(desc_list));
2475 if (!img_handle->desc_slot) {
2476 FREE(img_handle);
2477 return 0;
2478 }
2479
2480 handle = img_handle->desc_slot;
2481
2482 if (!_mesa_hash_table_insert(sctx->img_handles, (void *)handle,
2483 img_handle)) {
2484 FREE(img_handle);
2485 return 0;
2486 }
2487
2488 util_copy_image_view(&img_handle->view, view);
2489
2490 r600_resource(view->resource)->image_handle_allocated = true;
2491
2492 return handle;
2493 }
2494
2495 static void si_delete_image_handle(struct pipe_context *ctx, uint64_t handle)
2496 {
2497 struct si_context *sctx = (struct si_context *)ctx;
2498 struct si_image_handle *img_handle;
2499 struct hash_entry *entry;
2500
2501 entry = _mesa_hash_table_search(sctx->img_handles, (void *)handle);
2502 if (!entry)
2503 return;
2504
2505 img_handle = (struct si_image_handle *)entry->data;
2506
2507 util_copy_image_view(&img_handle->view, NULL);
2508 _mesa_hash_table_remove(sctx->img_handles, entry);
2509 FREE(img_handle);
2510 }
2511
2512 static void si_make_image_handle_resident(struct pipe_context *ctx,
2513 uint64_t handle, unsigned access,
2514 bool resident)
2515 {
2516 struct si_context *sctx = (struct si_context *)ctx;
2517 struct si_image_handle *img_handle;
2518 struct pipe_image_view *view;
2519 struct r600_resource *res;
2520 struct hash_entry *entry;
2521
2522 entry = _mesa_hash_table_search(sctx->img_handles, (void *)handle);
2523 if (!entry)
2524 return;
2525
2526 img_handle = (struct si_image_handle *)entry->data;
2527 view = &img_handle->view;
2528 res = (struct r600_resource *)view->resource;
2529
2530 if (resident) {
2531 if (res->b.b.target != PIPE_BUFFER) {
2532 struct r600_texture *rtex = (struct r600_texture *)res;
2533 unsigned level = view->u.tex.level;
2534
2535 if (color_needs_decompression(rtex)) {
2536 util_dynarray_append(
2537 &sctx->resident_img_needs_color_decompress,
2538 struct si_image_handle *,
2539 img_handle);
2540 }
2541
2542 if (vi_dcc_enabled(rtex, level) &&
2543 p_atomic_read(&rtex->framebuffers_bound))
2544 sctx->need_check_render_feedback = true;
2545
2546 si_update_bindless_image_descriptor(sctx, img_handle);
2547 } else {
2548 si_update_bindless_buffer_descriptor(sctx,
2549 img_handle->desc_slot,
2550 view->resource,
2551 view->u.buf.offset,
2552 &img_handle->desc_dirty);
2553 }
2554
2555 /* Re-upload the descriptor if it has been updated while it
2556 * wasn't resident.
2557 */
2558 if (img_handle->desc_dirty)
2559 sctx->bindless_descriptors_dirty = true;
2560
2561 /* Add the image handle to the per-context list. */
2562 util_dynarray_append(&sctx->resident_img_handles,
2563 struct si_image_handle *, img_handle);
2564
2565 /* Add the buffers to the current CS in case si_begin_new_cs()
2566 * is not going to be called.
2567 */
2568 si_sampler_view_add_buffer(sctx, view->resource,
2569 (access & PIPE_IMAGE_ACCESS_WRITE) ?
2570 RADEON_USAGE_READWRITE :
2571 RADEON_USAGE_READ, false, false);
2572 } else {
2573 /* Remove the image handle from the per-context list. */
2574 util_dynarray_delete_unordered(&sctx->resident_img_handles,
2575 struct si_image_handle *,
2576 img_handle);
2577
2578 if (res->b.b.target != PIPE_BUFFER) {
2579 util_dynarray_delete_unordered(
2580 &sctx->resident_img_needs_color_decompress,
2581 struct si_image_handle *,
2582 img_handle);
2583 }
2584 }
2585 }
2586
2587
2588 void si_all_resident_buffers_begin_new_cs(struct si_context *sctx)
2589 {
2590 unsigned num_resident_tex_handles, num_resident_img_handles;
2591
2592 num_resident_tex_handles = sctx->resident_tex_handles.size /
2593 sizeof(struct si_texture_handle *);
2594 num_resident_img_handles = sctx->resident_img_handles.size /
2595 sizeof(struct si_image_handle *);
2596
2597 /* Add all resident texture handles. */
2598 util_dynarray_foreach(&sctx->resident_tex_handles,
2599 struct si_texture_handle *, tex_handle) {
2600 struct si_sampler_view *sview =
2601 (struct si_sampler_view *)(*tex_handle)->view;
2602
2603 si_sampler_view_add_buffer(sctx, sview->base.texture,
2604 RADEON_USAGE_READ,
2605 sview->is_stencil_sampler, false);
2606 }
2607
2608 /* Add all resident image handles. */
2609 util_dynarray_foreach(&sctx->resident_img_handles,
2610 struct si_image_handle *, img_handle) {
2611 struct pipe_image_view *view = &(*img_handle)->view;
2612
2613 si_sampler_view_add_buffer(sctx, view->resource,
2614 RADEON_USAGE_READWRITE,
2615 false, false);
2616 }
2617
2618 sctx->b.num_resident_handles += num_resident_tex_handles +
2619 num_resident_img_handles;
2620 }
2621
2622 /* INIT/DEINIT/UPLOAD */
2623
2624 void si_init_all_descriptors(struct si_context *sctx)
2625 {
2626 int i;
2627
2628 STATIC_ASSERT(GFX9_SGPR_TCS_CONST_AND_SHADER_BUFFERS % 2 == 0);
2629 STATIC_ASSERT(GFX9_SGPR_GS_CONST_AND_SHADER_BUFFERS % 2 == 0);
2630
2631 for (i = 0; i < SI_NUM_SHADERS; i++) {
2632 bool gfx9_tcs = false;
2633 bool gfx9_gs = false;
2634 unsigned num_sampler_slots = SI_NUM_IMAGES / 2 + SI_NUM_SAMPLERS;
2635 unsigned num_buffer_slots = SI_NUM_SHADER_BUFFERS + SI_NUM_CONST_BUFFERS;
2636
2637 if (sctx->b.chip_class >= GFX9) {
2638 gfx9_tcs = i == PIPE_SHADER_TESS_CTRL;
2639 gfx9_gs = i == PIPE_SHADER_GEOMETRY;
2640 }
2641
2642 si_init_buffer_resources(&sctx->const_and_shader_buffers[i],
2643 si_const_and_shader_buffer_descriptors(sctx, i),
2644 num_buffer_slots,
2645 gfx9_tcs ? GFX9_SGPR_TCS_CONST_AND_SHADER_BUFFERS :
2646 gfx9_gs ? GFX9_SGPR_GS_CONST_AND_SHADER_BUFFERS :
2647 SI_SGPR_CONST_AND_SHADER_BUFFERS,
2648 RADEON_USAGE_READWRITE,
2649 RADEON_USAGE_READ,
2650 RADEON_PRIO_SHADER_RW_BUFFER,
2651 RADEON_PRIO_CONST_BUFFER);
2652
2653 struct si_descriptors *desc = si_sampler_and_image_descriptors(sctx, i);
2654 si_init_descriptors(desc,
2655 gfx9_tcs ? GFX9_SGPR_TCS_SAMPLERS_AND_IMAGES :
2656 gfx9_gs ? GFX9_SGPR_GS_SAMPLERS_AND_IMAGES :
2657 SI_SGPR_SAMPLERS_AND_IMAGES,
2658 16, num_sampler_slots);
2659
2660 int j;
2661 for (j = 0; j < SI_NUM_IMAGES; j++)
2662 memcpy(desc->list + j * 8, null_image_descriptor, 8 * 4);
2663 for (; j < SI_NUM_IMAGES + SI_NUM_SAMPLERS * 2; j++)
2664 memcpy(desc->list + j * 8, null_texture_descriptor, 8 * 4);
2665 }
2666
2667 si_init_buffer_resources(&sctx->rw_buffers,
2668 &sctx->descriptors[SI_DESCS_RW_BUFFERS],
2669 SI_NUM_RW_BUFFERS, SI_SGPR_RW_BUFFERS,
2670 /* The second set of usage/priority is used by
2671 * const buffers in RW buffer slots. */
2672 RADEON_USAGE_READWRITE, RADEON_USAGE_READ,
2673 RADEON_PRIO_SHADER_RINGS, RADEON_PRIO_CONST_BUFFER);
2674 sctx->descriptors[SI_DESCS_RW_BUFFERS].num_active_slots = SI_NUM_RW_BUFFERS;
2675
2676 si_init_descriptors(&sctx->vertex_buffers, SI_SGPR_VERTEX_BUFFERS,
2677 4, SI_NUM_VERTEX_BUFFERS);
2678 FREE(sctx->vertex_buffers.list); /* not used */
2679 sctx->vertex_buffers.list = NULL;
2680
2681 /* Initialize an array of 1024 bindless descriptors, when the limit is
2682 * reached, just make it larger and re-upload the whole array.
2683 */
2684 si_init_bindless_descriptors(sctx, &sctx->bindless_descriptors,
2685 SI_SGPR_BINDLESS_SAMPLERS_AND_IMAGES,
2686 1024);
2687
2688 sctx->descriptors_dirty = u_bit_consecutive(0, SI_NUM_DESCS);
2689
2690 /* Set pipe_context functions. */
2691 sctx->b.b.bind_sampler_states = si_bind_sampler_states;
2692 sctx->b.b.set_shader_images = si_set_shader_images;
2693 sctx->b.b.set_constant_buffer = si_pipe_set_constant_buffer;
2694 sctx->b.b.set_polygon_stipple = si_set_polygon_stipple;
2695 sctx->b.b.set_shader_buffers = si_set_shader_buffers;
2696 sctx->b.b.set_sampler_views = si_set_sampler_views;
2697 sctx->b.b.set_stream_output_targets = si_set_streamout_targets;
2698 sctx->b.b.create_texture_handle = si_create_texture_handle;
2699 sctx->b.b.delete_texture_handle = si_delete_texture_handle;
2700 sctx->b.b.make_texture_handle_resident = si_make_texture_handle_resident;
2701 sctx->b.b.create_image_handle = si_create_image_handle;
2702 sctx->b.b.delete_image_handle = si_delete_image_handle;
2703 sctx->b.b.make_image_handle_resident = si_make_image_handle_resident;
2704 sctx->b.invalidate_buffer = si_invalidate_buffer;
2705 sctx->b.rebind_buffer = si_rebind_buffer;
2706
2707 /* Shader user data. */
2708 si_init_atom(sctx, &sctx->shader_pointers.atom, &sctx->atoms.s.shader_pointers,
2709 si_emit_graphics_shader_pointers);
2710
2711 /* Set default and immutable mappings. */
2712 si_set_user_data_base(sctx, PIPE_SHADER_VERTEX, R_00B130_SPI_SHADER_USER_DATA_VS_0);
2713
2714 if (sctx->b.chip_class >= GFX9) {
2715 si_set_user_data_base(sctx, PIPE_SHADER_TESS_CTRL,
2716 R_00B430_SPI_SHADER_USER_DATA_LS_0);
2717 si_set_user_data_base(sctx, PIPE_SHADER_GEOMETRY,
2718 R_00B330_SPI_SHADER_USER_DATA_ES_0);
2719 } else {
2720 si_set_user_data_base(sctx, PIPE_SHADER_TESS_CTRL,
2721 R_00B430_SPI_SHADER_USER_DATA_HS_0);
2722 si_set_user_data_base(sctx, PIPE_SHADER_GEOMETRY,
2723 R_00B230_SPI_SHADER_USER_DATA_GS_0);
2724 }
2725 si_set_user_data_base(sctx, PIPE_SHADER_FRAGMENT, R_00B030_SPI_SHADER_USER_DATA_PS_0);
2726 }
2727
2728 static bool si_upload_shader_descriptors(struct si_context *sctx, unsigned mask)
2729 {
2730 unsigned dirty = sctx->descriptors_dirty & mask;
2731
2732 /* Assume nothing will go wrong: */
2733 sctx->shader_pointers_dirty |= dirty;
2734
2735 while (dirty) {
2736 unsigned i = u_bit_scan(&dirty);
2737
2738 if (!si_upload_descriptors(sctx, &sctx->descriptors[i],
2739 &sctx->shader_pointers.atom))
2740 return false;
2741 }
2742
2743 sctx->descriptors_dirty &= ~mask;
2744
2745 si_upload_bindless_descriptors(sctx);
2746
2747 return true;
2748 }
2749
2750 bool si_upload_graphics_shader_descriptors(struct si_context *sctx)
2751 {
2752 const unsigned mask = u_bit_consecutive(0, SI_DESCS_FIRST_COMPUTE);
2753 return si_upload_shader_descriptors(sctx, mask);
2754 }
2755
2756 bool si_upload_compute_shader_descriptors(struct si_context *sctx)
2757 {
2758 /* Does not update rw_buffers as that is not needed for compute shaders
2759 * and the input buffer is using the same SGPR's anyway.
2760 */
2761 const unsigned mask = u_bit_consecutive(SI_DESCS_FIRST_COMPUTE,
2762 SI_NUM_DESCS - SI_DESCS_FIRST_COMPUTE);
2763 return si_upload_shader_descriptors(sctx, mask);
2764 }
2765
2766 void si_release_all_descriptors(struct si_context *sctx)
2767 {
2768 int i;
2769
2770 for (i = 0; i < SI_NUM_SHADERS; i++) {
2771 si_release_buffer_resources(&sctx->const_and_shader_buffers[i],
2772 si_const_and_shader_buffer_descriptors(sctx, i));
2773 si_release_sampler_views(&sctx->samplers[i]);
2774 si_release_image_views(&sctx->images[i]);
2775 }
2776 si_release_buffer_resources(&sctx->rw_buffers,
2777 &sctx->descriptors[SI_DESCS_RW_BUFFERS]);
2778 for (i = 0; i < SI_NUM_VERTEX_BUFFERS; i++)
2779 pipe_vertex_buffer_unreference(&sctx->vertex_buffer[i]);
2780
2781 for (i = 0; i < SI_NUM_DESCS; ++i)
2782 si_release_descriptors(&sctx->descriptors[i]);
2783
2784 sctx->vertex_buffers.list = NULL; /* points into a mapped buffer */
2785 si_release_descriptors(&sctx->vertex_buffers);
2786 si_release_bindless_descriptors(sctx);
2787 }
2788
2789 void si_all_descriptors_begin_new_cs(struct si_context *sctx)
2790 {
2791 int i;
2792
2793 for (i = 0; i < SI_NUM_SHADERS; i++) {
2794 si_buffer_resources_begin_new_cs(sctx, &sctx->const_and_shader_buffers[i]);
2795 si_sampler_views_begin_new_cs(sctx, &sctx->samplers[i]);
2796 si_image_views_begin_new_cs(sctx, &sctx->images[i]);
2797 }
2798 si_buffer_resources_begin_new_cs(sctx, &sctx->rw_buffers);
2799 si_vertex_buffers_begin_new_cs(sctx);
2800
2801 for (i = 0; i < SI_NUM_DESCS; ++i)
2802 si_descriptors_begin_new_cs(sctx, &sctx->descriptors[i]);
2803 si_descriptors_begin_new_cs(sctx, &sctx->bindless_descriptors);
2804
2805 si_shader_pointers_begin_new_cs(sctx);
2806 }
2807
2808 void si_set_active_descriptors(struct si_context *sctx, unsigned desc_idx,
2809 uint64_t new_active_mask)
2810 {
2811 struct si_descriptors *desc = &sctx->descriptors[desc_idx];
2812
2813 /* Ignore no-op updates and updates that disable all slots. */
2814 if (!new_active_mask ||
2815 new_active_mask == u_bit_consecutive64(desc->first_active_slot,
2816 desc->num_active_slots))
2817 return;
2818
2819 int first, count;
2820 u_bit_scan_consecutive_range64(&new_active_mask, &first, &count);
2821 assert(new_active_mask == 0);
2822
2823 /* Upload/dump descriptors if slots are being enabled. */
2824 if (first < desc->first_active_slot ||
2825 first + count > desc->first_active_slot + desc->num_active_slots)
2826 sctx->descriptors_dirty |= 1u << desc_idx;
2827
2828 desc->first_active_slot = first;
2829 desc->num_active_slots = count;
2830 }
2831
2832 void si_set_active_descriptors_for_shader(struct si_context *sctx,
2833 struct si_shader_selector *sel)
2834 {
2835 if (!sel)
2836 return;
2837
2838 si_set_active_descriptors(sctx,
2839 si_const_and_shader_buffer_descriptors_idx(sel->type),
2840 sel->active_const_and_shader_buffers);
2841 si_set_active_descriptors(sctx,
2842 si_sampler_and_image_descriptors_idx(sel->type),
2843 sel->active_samplers_and_images);
2844 }