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