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