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