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