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