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