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