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