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