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