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