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