• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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->b.caps.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 view_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          view_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       si_make_texture_descriptor(screen, tex, false, res->b.b.target, view->format, swizzle,
756                                  view_level, view_level, view->u.tex.first_layer,
757                                  view->u.tex.last_layer, width, height, depth, false, desc,
758                                  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), false, access, desc);
761    }
762 }
763 
si_set_shader_image(struct si_context * ctx,unsigned shader,unsigned slot,const struct pipe_image_view * view,bool skip_decompress)764 static void si_set_shader_image(struct si_context *ctx, unsigned shader, unsigned slot,
765                                 const struct pipe_image_view *view, bool skip_decompress)
766 {
767    struct si_images *images = &ctx->images[shader];
768    struct si_descriptors *descs = si_sampler_and_image_descriptors(ctx, shader);
769    struct si_resource *res;
770 
771    if (!view || !view->resource) {
772       si_disable_shader_image(ctx, shader, slot);
773       return;
774    }
775 
776    res = si_resource(view->resource);
777 
778    si_set_shader_image_desc(ctx, view, skip_decompress, descs->list + si_get_image_slot(slot) * 8,
779                             descs->list + si_get_image_slot(slot + SI_NUM_IMAGES) * 8);
780 
781    if (&images->views[slot] != view)
782       util_copy_image_view(&images->views[slot], view);
783 
784    if (ctx->gfx_level >= GFX12) {
785       /* Gfx12 doesn't do any decompression. */
786       if (res->b.b.target == PIPE_BUFFER)
787          res->bind_history |= SI_BIND_IMAGE_BUFFER(shader);
788    } else {
789       if (res->b.b.target == PIPE_BUFFER) {
790          images->needs_color_decompress_mask &= ~(1 << slot);
791          images->display_dcc_store_mask &= ~(1u << slot);
792          res->bind_history |= SI_BIND_IMAGE_BUFFER(shader);
793       } else {
794          struct si_texture *tex = (struct si_texture *)res;
795          unsigned level = view->u.tex.level;
796 
797          if (color_needs_decompression(tex)) {
798             images->needs_color_decompress_mask |= 1 << slot;
799          } else {
800             images->needs_color_decompress_mask &= ~(1 << slot);
801          }
802 
803          if (tex->surface.display_dcc_offset && view->access & PIPE_IMAGE_ACCESS_WRITE) {
804             images->display_dcc_store_mask |= 1u << slot;
805 
806             /* Set displayable_dcc_dirty for non-compute stages conservatively (before draw calls). */
807             if (shader != PIPE_SHADER_COMPUTE)
808                tex->displayable_dcc_dirty = true;
809          } else {
810             images->display_dcc_store_mask &= ~(1u << slot);
811          }
812 
813          if (shader == PIPE_SHADER_FRAGMENT && vi_dcc_enabled(tex, level) &&
814              p_atomic_read(&tex->framebuffers_bound))
815             ctx->need_check_render_feedback = true;
816       }
817    }
818 
819    images->enabled_mask |= 1u << slot;
820    ctx->descriptors_dirty |= 1u << si_sampler_and_image_descriptors_idx(shader);
821    if (shader != PIPE_SHADER_COMPUTE)
822       si_mark_atom_dirty(ctx, &ctx->atoms.s.gfx_shader_pointers);
823 
824    /* Since this can flush, it must be done after enabled_mask is updated. */
825    si_sampler_view_add_buffer(ctx, &res->b.b,
826                               (view->access & PIPE_IMAGE_ACCESS_WRITE) ?
827                                  RADEON_USAGE_READWRITE : RADEON_USAGE_READ, false);
828 }
829 
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)830 static void si_set_shader_images(struct pipe_context *pipe, enum pipe_shader_type shader,
831                                  unsigned start_slot, unsigned count,
832                                  unsigned unbind_num_trailing_slots,
833                                  const struct pipe_image_view *views)
834 {
835    struct si_context *ctx = (struct si_context *)pipe;
836    unsigned i, slot;
837 
838    assert(shader < SI_NUM_SHADERS);
839 
840    if (!count && !unbind_num_trailing_slots)
841       return;
842 
843    assert(start_slot + count + unbind_num_trailing_slots <= SI_NUM_IMAGES);
844 
845    if (views) {
846       for (i = 0, slot = start_slot; i < count; ++i, ++slot)
847          si_set_shader_image(ctx, shader, slot, &views[i], false);
848    } else {
849       for (i = 0, slot = start_slot; i < count; ++i, ++slot)
850          si_set_shader_image(ctx, shader, slot, NULL, false);
851    }
852 
853    for (i = 0; i < unbind_num_trailing_slots; ++i, ++slot)
854       si_set_shader_image(ctx, shader, slot, NULL, false);
855 
856    if (shader == PIPE_SHADER_COMPUTE &&
857        ctx->cs_shader_state.program &&
858        start_slot < ctx->cs_shader_state.program->sel.cs_num_images_in_user_sgprs)
859       ctx->compute_image_sgprs_dirty = true;
860 
861    si_update_shader_needs_decompress_mask(ctx, shader);
862 }
863 
si_images_update_needs_color_decompress_mask(struct si_context * sctx,struct si_images * images)864 static void si_images_update_needs_color_decompress_mask(struct si_context *sctx,
865                                                          struct si_images *images)
866 {
867    assert(sctx->gfx_level < GFX12);
868 
869    unsigned mask = images->enabled_mask;
870 
871    while (mask) {
872       int i = u_bit_scan(&mask);
873       struct pipe_resource *res = images->views[i].resource;
874 
875       if (res && res->target != PIPE_BUFFER) {
876          struct si_texture *tex = (struct si_texture *)res;
877 
878          if (color_needs_decompression(tex)) {
879             images->needs_color_decompress_mask |= 1 << i;
880          } else {
881             images->needs_color_decompress_mask &= ~(1 << i);
882          }
883       }
884    }
885 }
886 
si_force_disable_ps_colorbuf0_slot(struct si_context * sctx)887 void si_force_disable_ps_colorbuf0_slot(struct si_context *sctx)
888 {
889    if (sctx->ps_uses_fbfetch) {
890       sctx->ps_uses_fbfetch = false;
891       si_update_ps_iter_samples(sctx);
892    }
893 }
894 
si_update_ps_colorbuf0_slot(struct si_context * sctx)895 void si_update_ps_colorbuf0_slot(struct si_context *sctx)
896 {
897    struct si_buffer_resources *buffers = &sctx->internal_bindings;
898    struct si_descriptors *descs = &sctx->descriptors[SI_DESCS_INTERNAL];
899    unsigned slot = SI_PS_IMAGE_COLORBUF0;
900    struct pipe_surface *surf = NULL;
901    struct si_texture *tex = NULL;
902 
903    /* FBFETCH is always disabled for u_blitter, and will be re-enabled after u_blitter is done. */
904    if (sctx->blitter_running || sctx->suppress_update_ps_colorbuf0_slot) {
905       assert(!sctx->ps_uses_fbfetch);
906       return;
907    }
908 
909    /* Get the color buffer if FBFETCH should be enabled. */
910    if (sctx->shader.ps.cso && sctx->shader.ps.cso->info.base.fs.uses_fbfetch_output &&
911        sctx->framebuffer.state.nr_cbufs && sctx->framebuffer.state.cbufs[0]) {
912       surf = sctx->framebuffer.state.cbufs[0];
913       if (surf) {
914          tex = (struct si_texture *)surf->texture;
915          assert(tex && !tex->is_depth);
916       }
917    }
918 
919    /* Return if FBFETCH transitions from disabled to disabled. */
920    if (!sctx->ps_uses_fbfetch && !surf)
921       return;
922 
923    if (surf) {
924       bool disable_dcc = tex->surface.meta_offset != 0;
925       bool disable_cmask = tex->buffer.b.b.nr_samples <= 1 && tex->cmask_buffer;
926 
927       /* Disable DCC and eliminate fast clear because the texture is used as both a sampler
928        * and color buffer.
929        */
930       if (disable_dcc || disable_cmask) {
931          /* Disable fbfetch only for decompression. */
932          si_force_disable_ps_colorbuf0_slot(sctx);
933          sctx->suppress_update_ps_colorbuf0_slot = true;
934 
935          si_texture_disable_dcc(sctx, tex);
936 
937          if (disable_cmask) {
938             assert(tex->cmask_buffer != &tex->buffer);
939             si_eliminate_fast_color_clear(sctx, tex, NULL);
940             si_texture_discard_cmask(sctx->screen, tex);
941          }
942 
943          sctx->suppress_update_ps_colorbuf0_slot = false;
944       }
945 
946       /* Bind color buffer 0 as a shader image. */
947       struct pipe_image_view view = {0};
948       view.resource = surf->texture;
949       view.format = surf->format;
950       view.access = PIPE_IMAGE_ACCESS_READ;
951       view.u.tex.first_layer = surf->u.tex.first_layer;
952       view.u.tex.last_layer = surf->u.tex.last_layer;
953       view.u.tex.level = surf->u.tex.level;
954 
955       /* Set the descriptor. */
956       uint32_t *desc = descs->list + slot * 4;
957       memset(desc, 0, 16 * 4);
958       si_set_shader_image_desc(sctx, &view, true, desc, desc + 8);
959 
960       pipe_resource_reference(&buffers->buffers[slot], &tex->buffer.b.b);
961       radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, &tex->buffer,
962                                 RADEON_USAGE_READ | RADEON_PRIO_SHADER_RW_IMAGE);
963       buffers->enabled_mask |= 1llu << slot;
964    } else {
965       /* Clear the descriptor. */
966       memset(descs->list + slot * 4, 0, 8 * 4);
967       pipe_resource_reference(&buffers->buffers[slot], NULL);
968       buffers->enabled_mask &= ~(1llu << slot);
969    }
970 
971    sctx->descriptors_dirty |= 1u << SI_DESCS_INTERNAL;
972    si_mark_atom_dirty(sctx, &sctx->atoms.s.gfx_shader_pointers);
973    sctx->ps_uses_fbfetch = surf != NULL;
974    si_update_ps_iter_samples(sctx);
975    si_ps_key_update_framebuffer(sctx);
976 }
977 
978 /* SAMPLER STATES */
979 
si_bind_sampler_states(struct pipe_context * ctx,enum pipe_shader_type shader,unsigned start,unsigned count,void ** states)980 static void si_bind_sampler_states(struct pipe_context *ctx, enum pipe_shader_type shader,
981                                    unsigned start, unsigned count, void **states)
982 {
983    struct si_context *sctx = (struct si_context *)ctx;
984    struct si_samplers *samplers = &sctx->samplers[shader];
985    struct si_descriptors *desc = si_sampler_and_image_descriptors(sctx, shader);
986    struct si_sampler_state **sstates = (struct si_sampler_state **)states;
987    int i;
988 
989    if (!count || shader >= SI_NUM_SHADERS || !sstates)
990       return;
991 
992    for (i = 0; i < count; i++) {
993       unsigned slot = start + i;
994       unsigned desc_slot = si_get_sampler_slot(slot);
995 
996       if (!sstates[i] || sstates[i] == samplers->sampler_states[slot])
997          continue;
998 
999 #ifndef NDEBUG
1000       assert(sstates[i]->magic == SI_SAMPLER_STATE_MAGIC);
1001 #endif
1002       samplers->sampler_states[slot] = sstates[i];
1003 
1004       /* If FMASK is bound, don't overwrite it.
1005        * The sampler state will be set after FMASK is unbound.
1006        */
1007       struct si_sampler_view *sview = (struct si_sampler_view *)samplers->views[slot];
1008 
1009       struct si_texture *tex = NULL;
1010 
1011       if (sview && sview->base.texture && sview->base.texture->target != PIPE_BUFFER)
1012          tex = (struct si_texture *)sview->base.texture;
1013 
1014       if (tex && tex->surface.fmask_size)
1015          continue;
1016 
1017       si_set_sampler_state_desc(sstates[i], sview, tex, desc->list + desc_slot * 16 + 12);
1018 
1019       sctx->descriptors_dirty |= 1u << si_sampler_and_image_descriptors_idx(shader);
1020       if (shader != PIPE_SHADER_COMPUTE)
1021          si_mark_atom_dirty(sctx, &sctx->atoms.s.gfx_shader_pointers);
1022    }
1023 }
1024 
1025 /* BUFFER RESOURCES */
1026 
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)1027 static void si_init_buffer_resources(struct si_context *sctx,
1028                                      struct si_buffer_resources *buffers,
1029                                      struct si_descriptors *descs, unsigned num_buffers,
1030                                      short shader_userdata_rel_index,
1031                                      unsigned priority,
1032                                      unsigned priority_constbuf)
1033 {
1034    buffers->priority = priority;
1035    buffers->priority_constbuf = priority_constbuf;
1036    buffers->buffers = CALLOC(num_buffers, sizeof(struct pipe_resource *));
1037    buffers->offsets = CALLOC(num_buffers, sizeof(buffers->offsets[0]));
1038 
1039    si_init_descriptors(descs, shader_userdata_rel_index, 4, num_buffers);
1040 
1041    const struct ac_buffer_state buffer_state = {
1042       .format = PIPE_FORMAT_R32_FLOAT,
1043       .swizzle =
1044          {
1045             PIPE_SWIZZLE_X,
1046             PIPE_SWIZZLE_Y,
1047             PIPE_SWIZZLE_Z,
1048             PIPE_SWIZZLE_W,
1049          },
1050       .gfx10_oob_select = V_008F0C_OOB_SELECT_RAW,
1051    };
1052 
1053    /* Initialize buffer descriptors, so that we don't have to do it at bind time. */
1054    for (unsigned i = 0; i < num_buffers; i++) {
1055       uint32_t *desc = descs->list + i * 4;
1056 
1057       ac_set_buf_desc_word3(sctx->gfx_level, &buffer_state, &desc[3]);
1058    }
1059 }
1060 
si_release_buffer_resources(struct si_buffer_resources * buffers,struct si_descriptors * descs)1061 static void si_release_buffer_resources(struct si_buffer_resources *buffers,
1062                                         struct si_descriptors *descs)
1063 {
1064    int i;
1065 
1066    for (i = 0; i < descs->num_elements; i++) {
1067       pipe_resource_reference(&buffers->buffers[i], NULL);
1068    }
1069 
1070    FREE(buffers->buffers);
1071    FREE(buffers->offsets);
1072 }
1073 
si_buffer_resources_begin_new_cs(struct si_context * sctx,struct si_buffer_resources * buffers)1074 static void si_buffer_resources_begin_new_cs(struct si_context *sctx,
1075                                              struct si_buffer_resources *buffers)
1076 {
1077    uint64_t mask = buffers->enabled_mask;
1078 
1079    /* Add buffers to the CS. */
1080    while (mask) {
1081       int i = u_bit_scan64(&mask);
1082 
1083       radeon_add_to_buffer_list(
1084          sctx, &sctx->gfx_cs, si_resource(buffers->buffers[i]),
1085          (buffers->writable_mask & (1llu << i) ? RADEON_USAGE_READWRITE : RADEON_USAGE_READ) |
1086          (i < SI_NUM_SHADER_BUFFERS ? buffers->priority : buffers->priority_constbuf));
1087    }
1088 }
1089 
si_buffer_resources_check_encrypted(struct si_context * sctx,struct si_buffer_resources * buffers)1090 static bool si_buffer_resources_check_encrypted(struct si_context *sctx,
1091                                                 struct si_buffer_resources *buffers)
1092 {
1093    uint64_t mask = buffers->enabled_mask;
1094 
1095    while (mask) {
1096       int i = u_bit_scan64(&mask);
1097 
1098       if (si_resource(buffers->buffers[i])->flags & RADEON_FLAG_ENCRYPTED)
1099          return true;
1100    }
1101 
1102    return false;
1103 }
1104 
si_get_buffer_from_descriptors(struct si_buffer_resources * buffers,struct si_descriptors * descs,unsigned idx,struct pipe_resource ** buf,unsigned * offset,unsigned * size)1105 static void si_get_buffer_from_descriptors(struct si_buffer_resources *buffers,
1106                                            struct si_descriptors *descs, unsigned idx,
1107                                            struct pipe_resource **buf, unsigned *offset,
1108                                            unsigned *size)
1109 {
1110    pipe_resource_reference(buf, buffers->buffers[idx]);
1111    if (*buf) {
1112       struct si_resource *res = si_resource(*buf);
1113       const uint32_t *desc = descs->list + idx * 4;
1114       uint64_t va;
1115 
1116       *size = desc[2];
1117 
1118       assert(G_008F04_STRIDE(desc[1]) == 0);
1119       va = si_desc_extract_buffer_address(desc);
1120 
1121       assert(va >= res->gpu_address && va + *size <= res->gpu_address + res->bo_size);
1122       *offset = va - res->gpu_address;
1123    }
1124 }
1125 
1126 /* CONSTANT BUFFERS */
1127 
si_const_and_shader_buffer_descriptors(struct si_context * sctx,unsigned shader)1128 static struct si_descriptors *si_const_and_shader_buffer_descriptors(struct si_context *sctx,
1129                                                                      unsigned shader)
1130 {
1131    return &sctx->descriptors[si_const_and_shader_buffer_descriptors_idx(shader)];
1132 }
1133 
si_upload_const_buffer(struct si_context * sctx,struct si_resource ** buf,const uint8_t * ptr,unsigned size,uint32_t * const_offset)1134 static void si_upload_const_buffer(struct si_context *sctx, struct si_resource **buf,
1135                                    const uint8_t *ptr, unsigned size, uint32_t *const_offset)
1136 {
1137    void *tmp;
1138 
1139    u_upload_alloc(sctx->b.const_uploader, 0, size, si_optimal_tcc_alignment(sctx, size),
1140                   const_offset, (struct pipe_resource **)buf, &tmp);
1141    if (*buf)
1142       util_memcpy_cpu_to_le32(tmp, ptr, size);
1143 }
1144 
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)1145 static void si_set_constant_buffer(struct si_context *sctx, struct si_buffer_resources *buffers,
1146                                    unsigned descriptors_idx, uint slot, bool take_ownership,
1147                                    const struct pipe_constant_buffer *input)
1148 {
1149    struct si_descriptors *descs = &sctx->descriptors[descriptors_idx];
1150    assert(slot < descs->num_elements);
1151    pipe_resource_reference(&buffers->buffers[slot], NULL);
1152 
1153    /* GFX7 cannot unbind a constant buffer (S_BUFFER_LOAD is buggy
1154     * with a NULL buffer). We need to use a dummy buffer instead. */
1155    if (sctx->gfx_level == GFX7 && (!input || (!input->buffer && !input->user_buffer)))
1156       input = &sctx->null_const_buf;
1157 
1158    if (input && (input->buffer || input->user_buffer)) {
1159       struct pipe_resource *buffer = NULL;
1160       uint64_t va;
1161       unsigned buffer_offset;
1162 
1163       /* Upload the user buffer if needed. */
1164       if (input->user_buffer) {
1165          si_upload_const_buffer(sctx, (struct si_resource **)&buffer, input->user_buffer,
1166                                 input->buffer_size, &buffer_offset);
1167          if (!buffer) {
1168             /* Just unbind on failure. */
1169             si_set_constant_buffer(sctx, buffers, descriptors_idx, slot, false, NULL);
1170             return;
1171          }
1172       } else {
1173          if (take_ownership) {
1174             buffer = input->buffer;
1175          } else {
1176             pipe_resource_reference(&buffer, input->buffer);
1177          }
1178          buffer_offset = input->buffer_offset;
1179       }
1180 
1181       va = si_resource(buffer)->gpu_address + buffer_offset;
1182 
1183       /* Set the descriptor. */
1184       uint32_t *desc = descs->list + slot * 4;
1185       desc[0] = va;
1186       desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32) | S_008F04_STRIDE(0);
1187       desc[2] = input->buffer_size;
1188 
1189       buffers->buffers[slot] = buffer;
1190       buffers->offsets[slot] = buffer_offset;
1191       radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, si_resource(buffer),
1192                                 RADEON_USAGE_READ | buffers->priority_constbuf);
1193       buffers->enabled_mask |= 1llu << slot;
1194    } else {
1195       /* Clear the descriptor. Only 3 dwords are cleared. The 4th dword is immutable. */
1196       memset(descs->list + slot * 4, 0, sizeof(uint32_t) * 3);
1197       buffers->enabled_mask &= ~(1llu << slot);
1198    }
1199 
1200    sctx->descriptors_dirty |= 1u << descriptors_idx;
1201    if (descriptors_idx < SI_DESCS_FIRST_COMPUTE)
1202       si_mark_atom_dirty(sctx, &sctx->atoms.s.gfx_shader_pointers);
1203 }
1204 
si_get_inline_uniform_state(union si_shader_key * key,enum pipe_shader_type shader,bool * inline_uniforms,uint32_t ** inlined_values)1205 void si_get_inline_uniform_state(union si_shader_key *key, enum pipe_shader_type shader,
1206                                  bool *inline_uniforms, uint32_t **inlined_values)
1207 {
1208    if (shader == PIPE_SHADER_FRAGMENT) {
1209       *inline_uniforms = key->ps.opt.inline_uniforms;
1210       *inlined_values = key->ps.opt.inlined_uniform_values;
1211    } else {
1212       *inline_uniforms = key->ge.opt.inline_uniforms;
1213       *inlined_values = key->ge.opt.inlined_uniform_values;
1214    }
1215 }
1216 
si_invalidate_inlinable_uniforms(struct si_context * sctx,enum pipe_shader_type shader)1217 void si_invalidate_inlinable_uniforms(struct si_context *sctx, enum pipe_shader_type shader)
1218 {
1219    if (shader == PIPE_SHADER_COMPUTE)
1220       return;
1221 
1222    bool inline_uniforms;
1223    uint32_t *inlined_values;
1224    si_get_inline_uniform_state(&sctx->shaders[shader].key, shader, &inline_uniforms, &inlined_values);
1225 
1226    if (inline_uniforms) {
1227       if (shader == PIPE_SHADER_FRAGMENT)
1228          sctx->shaders[shader].key.ps.opt.inline_uniforms = false;
1229       else
1230          sctx->shaders[shader].key.ge.opt.inline_uniforms = false;
1231 
1232       memset(inlined_values, 0, MAX_INLINABLE_UNIFORMS * 4);
1233       sctx->do_update_shaders = true;
1234    }
1235 }
1236 
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)1237 static void si_pipe_set_constant_buffer(struct pipe_context *ctx, enum pipe_shader_type shader,
1238                                         uint slot, bool take_ownership,
1239                                         const struct pipe_constant_buffer *input)
1240 {
1241    struct si_context *sctx = (struct si_context *)ctx;
1242 
1243    if (shader >= SI_NUM_SHADERS)
1244       return;
1245 
1246    if (input) {
1247       if (input->buffer) {
1248          if (slot == 0 &&
1249              !(si_resource(input->buffer)->flags & RADEON_FLAG_32BIT)) {
1250             assert(!"constant buffer 0 must have a 32-bit VM address, use const_uploader");
1251             return;
1252          }
1253          si_resource(input->buffer)->bind_history |= SI_BIND_CONSTANT_BUFFER(shader);
1254       }
1255 
1256       if (slot == 0)
1257          si_invalidate_inlinable_uniforms(sctx, shader);
1258    }
1259 
1260    slot = si_get_constbuf_slot(slot);
1261    si_set_constant_buffer(sctx, &sctx->const_and_shader_buffers[shader],
1262                           si_const_and_shader_buffer_descriptors_idx(shader), slot,
1263                           take_ownership, input);
1264 }
1265 
si_set_inlinable_constants(struct pipe_context * ctx,enum pipe_shader_type shader,uint num_values,uint32_t * values)1266 static void si_set_inlinable_constants(struct pipe_context *ctx,
1267                                        enum pipe_shader_type shader,
1268                                        uint num_values, uint32_t *values)
1269 {
1270    struct si_context *sctx = (struct si_context *)ctx;
1271 
1272    if (shader == PIPE_SHADER_COMPUTE)
1273       return;
1274 
1275    bool inline_uniforms;
1276    uint32_t *inlined_values;
1277    si_get_inline_uniform_state(&sctx->shaders[shader].key, shader, &inline_uniforms, &inlined_values);
1278 
1279    if (!inline_uniforms) {
1280       /* It's the first time we set the constants. Always update shaders. */
1281       if (shader == PIPE_SHADER_FRAGMENT)
1282          sctx->shaders[shader].key.ps.opt.inline_uniforms = true;
1283       else
1284          sctx->shaders[shader].key.ge.opt.inline_uniforms = true;
1285 
1286       memcpy(inlined_values, values, num_values * 4);
1287       sctx->do_update_shaders = true;
1288       return;
1289    }
1290 
1291    /* We have already set inlinable constants for this shader. Update the shader only if
1292     * the constants are being changed so as not to update shaders needlessly.
1293     */
1294    if (memcmp(inlined_values, values, num_values * 4)) {
1295       memcpy(inlined_values, values, num_values * 4);
1296       sctx->do_update_shaders = true;
1297    }
1298 }
1299 
si_get_pipe_constant_buffer(struct si_context * sctx,uint shader,uint slot,struct pipe_constant_buffer * cbuf)1300 void si_get_pipe_constant_buffer(struct si_context *sctx, uint shader, uint slot,
1301                                  struct pipe_constant_buffer *cbuf)
1302 {
1303    cbuf->user_buffer = NULL;
1304    si_get_buffer_from_descriptors(
1305       &sctx->const_and_shader_buffers[shader], si_const_and_shader_buffer_descriptors(sctx, shader),
1306       si_get_constbuf_slot(slot), &cbuf->buffer, &cbuf->buffer_offset, &cbuf->buffer_size);
1307 }
1308 
1309 /* SHADER BUFFERS */
1310 
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)1311 static void si_set_shader_buffer(struct si_context *sctx, struct si_buffer_resources *buffers,
1312                                  unsigned descriptors_idx, uint slot,
1313                                  const struct pipe_shader_buffer *sbuffer, bool writable,
1314                                  unsigned priority)
1315 {
1316    struct si_descriptors *descs = &sctx->descriptors[descriptors_idx];
1317    uint32_t *desc = descs->list + slot * 4;
1318 
1319    if (!sbuffer || !sbuffer->buffer) {
1320       pipe_resource_reference(&buffers->buffers[slot], NULL);
1321       /* Clear the descriptor. Only 3 dwords are cleared. The 4th dword is immutable. */
1322       memset(desc, 0, sizeof(uint32_t) * 3);
1323       buffers->enabled_mask &= ~(1llu << slot);
1324       buffers->writable_mask &= ~(1llu << slot);
1325       sctx->descriptors_dirty |= 1u << descriptors_idx;
1326       if (descriptors_idx < SI_DESCS_FIRST_COMPUTE)
1327          si_mark_atom_dirty(sctx, &sctx->atoms.s.gfx_shader_pointers);
1328       return;
1329    }
1330 
1331    struct si_resource *buf = si_resource(sbuffer->buffer);
1332    /* Allow the size to be aligned to a dword even if the buffer size is less because we need
1333     * it for the clear/copy_buffer shader. It's safe because memory is allocated at a higher
1334     * granularity than 4 bytes.
1335     */
1336    assert(sbuffer->buffer_offset + sbuffer->buffer_size <= align(buf->bo_size, 4));
1337    uint64_t va = buf->gpu_address + sbuffer->buffer_offset;
1338 
1339    desc[0] = va;
1340    desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32) | S_008F04_STRIDE(0);
1341    desc[2] = sbuffer->buffer_size;
1342 
1343    pipe_resource_reference(&buffers->buffers[slot], &buf->b.b);
1344    buffers->offsets[slot] = sbuffer->buffer_offset;
1345    radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, buf,
1346                              (writable ? RADEON_USAGE_READWRITE : RADEON_USAGE_READ) | priority);
1347    if (writable)
1348       buffers->writable_mask |= 1llu << slot;
1349    else
1350       buffers->writable_mask &= ~(1llu << slot);
1351 
1352    buffers->enabled_mask |= 1llu << slot;
1353    sctx->descriptors_dirty |= 1lu << descriptors_idx;
1354    if (descriptors_idx < SI_DESCS_FIRST_COMPUTE)
1355       si_mark_atom_dirty(sctx, &sctx->atoms.s.gfx_shader_pointers);
1356 
1357    util_range_add(&buf->b.b, &buf->valid_buffer_range, sbuffer->buffer_offset,
1358                   sbuffer->buffer_offset + sbuffer->buffer_size);
1359 }
1360 
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)1361 void si_set_shader_buffers(struct pipe_context *ctx, enum pipe_shader_type shader,
1362                            unsigned start_slot, unsigned count,
1363                            const struct pipe_shader_buffer *sbuffers,
1364                            unsigned writable_bitmask, bool internal_blit)
1365 {
1366    struct si_context *sctx = (struct si_context *)ctx;
1367    struct si_buffer_resources *buffers = &sctx->const_and_shader_buffers[shader];
1368    unsigned descriptors_idx = si_const_and_shader_buffer_descriptors_idx(shader);
1369    unsigned i;
1370 
1371    assert(start_slot + count <= SI_NUM_SHADER_BUFFERS);
1372 
1373    if (shader == PIPE_SHADER_COMPUTE &&
1374        sctx->cs_shader_state.program &&
1375        start_slot < sctx->cs_shader_state.program->sel.cs_num_shaderbufs_in_user_sgprs)
1376       sctx->compute_shaderbuf_sgprs_dirty = true;
1377 
1378    for (i = 0; i < count; ++i) {
1379       const struct pipe_shader_buffer *sbuffer = sbuffers ? &sbuffers[i] : NULL;
1380       unsigned slot = si_get_shaderbuf_slot(start_slot + i);
1381 
1382       /* Don't track bind history for internal blits, such as clear_buffer and copy_buffer
1383        * to prevent unnecessary synchronization before compute blits later.
1384        */
1385       if (!internal_blit && sbuffer && sbuffer->buffer)
1386          si_resource(sbuffer->buffer)->bind_history |= SI_BIND_SHADER_BUFFER(shader);
1387 
1388       si_set_shader_buffer(sctx, buffers, descriptors_idx, slot, sbuffer,
1389                            !!(writable_bitmask & (1u << i)), buffers->priority);
1390    }
1391 }
1392 
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)1393 static void si_pipe_set_shader_buffers(struct pipe_context *ctx, enum pipe_shader_type shader,
1394                                        unsigned start_slot, unsigned count,
1395                                        const struct pipe_shader_buffer *sbuffers,
1396                                        unsigned writable_bitmask)
1397 {
1398    si_set_shader_buffers(ctx, shader, start_slot, count, sbuffers, writable_bitmask, false);
1399 }
1400 
si_get_shader_buffers(struct si_context * sctx,enum pipe_shader_type shader,uint start_slot,uint count,struct pipe_shader_buffer * sbuf)1401 void si_get_shader_buffers(struct si_context *sctx, enum pipe_shader_type shader, uint start_slot,
1402                            uint count, struct pipe_shader_buffer *sbuf)
1403 {
1404    struct si_buffer_resources *buffers = &sctx->const_and_shader_buffers[shader];
1405    struct si_descriptors *descs = si_const_and_shader_buffer_descriptors(sctx, shader);
1406 
1407    for (unsigned i = 0; i < count; ++i) {
1408       si_get_buffer_from_descriptors(buffers, descs, si_get_shaderbuf_slot(start_slot + i),
1409                                      &sbuf[i].buffer, &sbuf[i].buffer_offset, &sbuf[i].buffer_size);
1410    }
1411 }
1412 
1413 /* RING BUFFERS */
1414 
si_set_internal_const_buffer(struct si_context * sctx,uint slot,const struct pipe_constant_buffer * input)1415 void si_set_internal_const_buffer(struct si_context *sctx, uint slot,
1416                                   const struct pipe_constant_buffer *input)
1417 {
1418    si_set_constant_buffer(sctx, &sctx->internal_bindings, SI_DESCS_INTERNAL, slot, false, input);
1419 }
1420 
si_set_internal_shader_buffer(struct si_context * sctx,uint slot,const struct pipe_shader_buffer * sbuffer)1421 void si_set_internal_shader_buffer(struct si_context *sctx, uint slot,
1422                                    const struct pipe_shader_buffer *sbuffer)
1423 {
1424    si_set_shader_buffer(sctx, &sctx->internal_bindings, SI_DESCS_INTERNAL, slot, sbuffer, true,
1425                         RADEON_PRIO_SHADER_RW_BUFFER);
1426 }
1427 
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)1428 void si_set_ring_buffer(struct si_context *sctx, uint slot, struct pipe_resource *buffer,
1429                         unsigned stride, unsigned num_records, bool add_tid, bool swizzle,
1430                         unsigned element_size, unsigned index_stride, uint64_t offset)
1431 {
1432    struct si_buffer_resources *buffers = &sctx->internal_bindings;
1433    struct si_descriptors *descs = &sctx->descriptors[SI_DESCS_INTERNAL];
1434 
1435    /* Never used on GFX11+. (only used by ESGS and GSVS rings in memory) */
1436    assert(sctx->gfx_level < GFX11);
1437    /* The stride field in the resource descriptor has 14 bits */
1438    assert(stride < (1 << 14));
1439 
1440    assert(slot < descs->num_elements);
1441    pipe_resource_reference(&buffers->buffers[slot], NULL);
1442 
1443    if (buffer) {
1444       uint64_t va;
1445 
1446       va = si_resource(buffer)->gpu_address + offset;
1447 
1448       switch (element_size) {
1449       default:
1450          unreachable("Unsupported ring buffer element size");
1451       case 0:
1452       case 2:
1453          element_size = 0;
1454          break;
1455       case 4:
1456          element_size = 1;
1457          break;
1458       case 8:
1459          element_size = 2;
1460          break;
1461       case 16:
1462          element_size = 3;
1463          break;
1464       }
1465 
1466       switch (index_stride) {
1467       default:
1468          unreachable("Unsupported ring buffer index stride");
1469       case 0:
1470       case 8:
1471          index_stride = 0;
1472          break;
1473       case 16:
1474          index_stride = 1;
1475          break;
1476       case 32:
1477          index_stride = 2;
1478          break;
1479       case 64:
1480          index_stride = 3;
1481          break;
1482       }
1483 
1484       if (sctx->gfx_level >= GFX8 && stride)
1485          num_records *= stride;
1486 
1487       /* Set the descriptor. */
1488       uint32_t *desc = descs->list + slot * 4;
1489 
1490       uint32_t swizzle_enable;
1491 
1492       if (sctx->gfx_level >= GFX11) {
1493          swizzle_enable = swizzle ? element_size : 0;
1494       } else {
1495          swizzle_enable = swizzle;
1496       }
1497 
1498       const struct ac_buffer_state buffer_state = {
1499          .va = va,
1500          .size = num_records,
1501          .format = PIPE_FORMAT_R32_FLOAT,
1502          .swizzle = {
1503             PIPE_SWIZZLE_X, PIPE_SWIZZLE_Y, PIPE_SWIZZLE_Z, PIPE_SWIZZLE_W,
1504          },
1505          .stride = stride,
1506          .swizzle_enable = swizzle_enable,
1507          .gfx10_oob_select = V_008F0C_OOB_SELECT_DISABLED,
1508          .index_stride = index_stride,
1509          .element_size = element_size,
1510          .add_tid = add_tid,
1511       };
1512 
1513       ac_build_buffer_descriptor(sctx->gfx_level, &buffer_state, desc);
1514 
1515       pipe_resource_reference(&buffers->buffers[slot], buffer);
1516       radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, si_resource(buffer),
1517                                 RADEON_USAGE_READWRITE | buffers->priority);
1518       buffers->enabled_mask |= 1llu << slot;
1519    } else {
1520       /* Clear the descriptor. */
1521       memset(descs->list + slot * 4, 0, sizeof(uint32_t) * 4);
1522       buffers->enabled_mask &= ~(1llu << slot);
1523    }
1524 
1525    sctx->descriptors_dirty |= 1u << SI_DESCS_INTERNAL;
1526    si_mark_atom_dirty(sctx, &sctx->atoms.s.gfx_shader_pointers);
1527 }
1528 
1529 /* INTERNAL CONST BUFFERS */
1530 
si_set_polygon_stipple(struct pipe_context * ctx,const struct pipe_poly_stipple * state)1531 static void si_set_polygon_stipple(struct pipe_context *ctx, const struct pipe_poly_stipple *state)
1532 {
1533    struct si_context *sctx = (struct si_context *)ctx;
1534    struct pipe_constant_buffer cb = {};
1535    unsigned stipple[32];
1536    int i;
1537 
1538    for (i = 0; i < 32; i++)
1539       stipple[i] = util_bitreverse(state->stipple[i]);
1540 
1541    cb.user_buffer = stipple;
1542    cb.buffer_size = sizeof(stipple);
1543 
1544    si_set_internal_const_buffer(sctx, SI_PS_CONST_POLY_STIPPLE, &cb);
1545 }
1546 
1547 /* TEXTURE METADATA ENABLE/DISABLE */
1548 
si_resident_handles_update_needs_color_decompress(struct si_context * sctx)1549 static void si_resident_handles_update_needs_color_decompress(struct si_context *sctx)
1550 {
1551    assert(sctx->gfx_level < GFX12);
1552 
1553    util_dynarray_clear(&sctx->resident_tex_needs_color_decompress);
1554    util_dynarray_clear(&sctx->resident_img_needs_color_decompress);
1555 
1556    util_dynarray_foreach (&sctx->resident_tex_handles, struct si_texture_handle *, tex_handle) {
1557       struct pipe_resource *res = (*tex_handle)->view->texture;
1558       struct si_texture *tex;
1559 
1560       if (!res || res->target == PIPE_BUFFER)
1561          continue;
1562 
1563       tex = (struct si_texture *)res;
1564       if (!color_needs_decompression(tex))
1565          continue;
1566 
1567       util_dynarray_append(&sctx->resident_tex_needs_color_decompress, struct si_texture_handle *,
1568                            *tex_handle);
1569    }
1570 
1571    util_dynarray_foreach (&sctx->resident_img_handles, struct si_image_handle *, img_handle) {
1572       struct pipe_image_view *view = &(*img_handle)->view;
1573       struct pipe_resource *res = view->resource;
1574       struct si_texture *tex;
1575 
1576       if (!res || res->target == PIPE_BUFFER)
1577          continue;
1578 
1579       tex = (struct si_texture *)res;
1580       if (!color_needs_decompression(tex))
1581          continue;
1582 
1583       util_dynarray_append(&sctx->resident_img_needs_color_decompress, struct si_image_handle *,
1584                            *img_handle);
1585    }
1586 }
1587 
1588 /* CMASK can be enabled (for fast clear) and disabled (for texture export)
1589  * while the texture is bound, possibly by a different context. In that case,
1590  * call this function to update needs_*_decompress_masks.
1591  */
si_update_needs_color_decompress_masks(struct si_context * sctx)1592 void si_update_needs_color_decompress_masks(struct si_context *sctx)
1593 {
1594    assert(sctx->gfx_level < GFX11);
1595 
1596    for (int i = 0; i < SI_NUM_SHADERS; ++i) {
1597       si_samplers_update_needs_color_decompress_mask(sctx, &sctx->samplers[i]);
1598       si_images_update_needs_color_decompress_mask(sctx, &sctx->images[i]);
1599       si_update_shader_needs_decompress_mask(sctx, i);
1600    }
1601 
1602    si_resident_handles_update_needs_color_decompress(sctx);
1603 }
1604 
1605 /* BUFFER DISCARD/INVALIDATION */
1606 
1607 /* Reset descriptors of buffer resources after \p buf has been invalidated.
1608  * If buf == NULL, reset all descriptors.
1609  */
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)1610 static bool si_reset_buffer_resources(struct si_context *sctx, struct si_buffer_resources *buffers,
1611                                       unsigned descriptors_idx, uint64_t slot_mask,
1612                                       struct pipe_resource *buf, unsigned priority)
1613 {
1614    struct si_descriptors *descs = &sctx->descriptors[descriptors_idx];
1615    bool noop = true;
1616    uint64_t mask = buffers->enabled_mask & slot_mask;
1617 
1618    while (mask) {
1619       unsigned i = u_bit_scan64(&mask);
1620       struct pipe_resource *buffer = buffers->buffers[i];
1621 
1622       if (buffer && (!buf || buffer == buf)) {
1623          si_set_buf_desc_address(si_resource(buffer), buffers->offsets[i], descs->list + i * 4);
1624          sctx->descriptors_dirty |= 1u << descriptors_idx;
1625          if (descriptors_idx < SI_DESCS_FIRST_COMPUTE)
1626             si_mark_atom_dirty(sctx, &sctx->atoms.s.gfx_shader_pointers);
1627 
1628          radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, si_resource(buffer),
1629                                    (buffers->writable_mask & (1llu << i) ?
1630                                        RADEON_USAGE_READWRITE : RADEON_USAGE_READ) | priority);
1631          noop = false;
1632       }
1633    }
1634    return !noop;
1635 }
1636 
si_mark_bindless_descriptors_dirty(struct si_context * sctx)1637 static void si_mark_bindless_descriptors_dirty(struct si_context *sctx)
1638 {
1639    sctx->bindless_descriptors_dirty = true;
1640    /* gfx_shader_pointers uploads bindless descriptors. */
1641    si_mark_atom_dirty(sctx, &sctx->atoms.s.gfx_shader_pointers);
1642    /* gfx_shader_pointers can flag cache flags, so we need to dirty this too. */
1643    si_mark_atom_dirty(sctx, &sctx->atoms.s.barrier);
1644 }
1645 
1646 /* Update all buffer bindings where the buffer is bound, including
1647  * all resource descriptors. This is invalidate_buffer without
1648  * the invalidation.
1649  *
1650  * If buf == NULL, update all buffer bindings.
1651  */
si_rebind_buffer(struct si_context * sctx,struct pipe_resource * buf)1652 void si_rebind_buffer(struct si_context *sctx, struct pipe_resource *buf)
1653 {
1654    struct si_resource *buffer = si_resource(buf);
1655    unsigned i;
1656    unsigned num_elems = sctx->num_vertex_elements;
1657 
1658    /* We changed the buffer, now we need to bind it where the old one
1659     * was bound. This consists of 2 things:
1660     *   1) Updating the resource descriptor and dirtying it.
1661     *   2) Adding a relocation to the CS, so that it's usable.
1662     */
1663 
1664    /* Vertex buffers. */
1665    if (!buffer) {
1666       sctx->vertex_buffers_dirty = num_elems > 0;
1667 
1668       /* We don't know which buffer was invalidated, so we have to add all of them. */
1669       unsigned num_vb = sctx->num_vertex_buffers;
1670       for (unsigned i = 0; i < num_vb; i++) {
1671          struct si_resource *buf = si_resource(sctx->vertex_buffer[i].buffer.resource);
1672          if (buf) {
1673             radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, buf,
1674                                       RADEON_USAGE_READ |
1675                                       RADEON_PRIO_VERTEX_BUFFER);
1676          }
1677       }
1678    } else if (buffer->bind_history & SI_BIND_VERTEX_BUFFER) {
1679       unsigned num_vb = sctx->num_vertex_buffers;
1680 
1681       for (i = 0; i < num_elems; i++) {
1682          int vb = sctx->vertex_elements->vertex_buffer_index[i];
1683 
1684          if (vb >= num_vb)
1685             continue;
1686          if (!sctx->vertex_buffer[vb].buffer.resource)
1687             continue;
1688 
1689          if (sctx->vertex_buffer[vb].buffer.resource == buf) {
1690             sctx->vertex_buffers_dirty = num_elems > 0;
1691             radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, buffer,
1692                                       RADEON_USAGE_READ |
1693                                       RADEON_PRIO_VERTEX_BUFFER);
1694             break;
1695          }
1696       }
1697    }
1698 
1699    /* Streamout buffers. (other internal buffers can't be invalidated) */
1700    if (!buffer || buffer->bind_history & SI_BIND_STREAMOUT_BUFFER) {
1701       for (i = SI_VS_STREAMOUT_BUF0; i <= SI_VS_STREAMOUT_BUF3; i++) {
1702          struct si_buffer_resources *buffers = &sctx->internal_bindings;
1703          struct si_descriptors *descs = &sctx->descriptors[SI_DESCS_INTERNAL];
1704          struct pipe_resource *buffer = buffers->buffers[i];
1705 
1706          if (!buffer || (buf && buffer != buf))
1707             continue;
1708 
1709          si_set_buf_desc_address(si_resource(buffer), buffers->offsets[i], descs->list + i * 4);
1710          sctx->descriptors_dirty |= 1u << SI_DESCS_INTERNAL;
1711          si_mark_atom_dirty(sctx, &sctx->atoms.s.gfx_shader_pointers);
1712 
1713          radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, si_resource(buffer), RADEON_USAGE_WRITE |
1714                                    RADEON_PRIO_SHADER_RW_BUFFER);
1715 
1716          /* Update the streamout state. */
1717          if (sctx->streamout.begin_emitted)
1718             si_emit_streamout_end(sctx);
1719          sctx->streamout.append_bitmask = sctx->streamout.enabled_mask;
1720          si_streamout_buffers_dirty(sctx);
1721       }
1722    }
1723 
1724    /* Constant and shader buffers. */
1725    if (!buffer || buffer->bind_history & SI_BIND_CONSTANT_BUFFER_ALL) {
1726       unsigned mask = buffer ? (buffer->bind_history & SI_BIND_CONSTANT_BUFFER_ALL) >>
1727                                SI_BIND_CONSTANT_BUFFER_SHIFT : BITFIELD_MASK(SI_NUM_SHADERS);
1728       u_foreach_bit(shader, mask) {
1729          si_reset_buffer_resources(sctx, &sctx->const_and_shader_buffers[shader],
1730                                    si_const_and_shader_buffer_descriptors_idx(shader),
1731                                    u_bit_consecutive64(SI_NUM_SHADER_BUFFERS, SI_NUM_CONST_BUFFERS),
1732                                    buf, sctx->const_and_shader_buffers[shader].priority_constbuf);
1733       }
1734    }
1735 
1736    if (!buffer || buffer->bind_history & SI_BIND_SHADER_BUFFER_ALL) {
1737       unsigned mask = buffer ? (buffer->bind_history & SI_BIND_SHADER_BUFFER_ALL) >>
1738                                SI_BIND_SHADER_BUFFER_SHIFT : BITFIELD_MASK(SI_NUM_SHADERS);
1739       u_foreach_bit(shader, mask) {
1740          if (si_reset_buffer_resources(sctx, &sctx->const_and_shader_buffers[shader],
1741                                        si_const_and_shader_buffer_descriptors_idx(shader),
1742                                        u_bit_consecutive64(0, SI_NUM_SHADER_BUFFERS), buf,
1743                                        sctx->const_and_shader_buffers[shader].priority) &&
1744              shader == PIPE_SHADER_COMPUTE) {
1745             sctx->compute_shaderbuf_sgprs_dirty = true;
1746          }
1747       }
1748    }
1749 
1750    if (!buffer || buffer->bind_history & SI_BIND_SAMPLER_BUFFER_ALL) {
1751       unsigned mask = buffer ? (buffer->bind_history & SI_BIND_SAMPLER_BUFFER_ALL) >>
1752                                SI_BIND_SAMPLER_BUFFER_SHIFT : BITFIELD_MASK(SI_NUM_SHADERS);
1753       /* Texture buffers - update bindings. */
1754       u_foreach_bit(shader, mask) {
1755          struct si_samplers *samplers = &sctx->samplers[shader];
1756          struct si_descriptors *descs = si_sampler_and_image_descriptors(sctx, shader);
1757          unsigned mask = samplers->enabled_mask;
1758 
1759          while (mask) {
1760             unsigned i = u_bit_scan(&mask);
1761             struct pipe_resource *buffer = samplers->views[i]->texture;
1762 
1763             if (buffer && buffer->target == PIPE_BUFFER && (!buf || buffer == buf)) {
1764                unsigned desc_slot = si_get_sampler_slot(i);
1765 
1766                si_set_buf_desc_address(si_resource(buffer), samplers->views[i]->u.buf.offset,
1767                                        descs->list + desc_slot * 16 + 4);
1768                sctx->descriptors_dirty |= 1u << si_sampler_and_image_descriptors_idx(shader);
1769                if (shader != PIPE_SHADER_COMPUTE)
1770                   si_mark_atom_dirty(sctx, &sctx->atoms.s.gfx_shader_pointers);
1771 
1772                radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, si_resource(buffer), RADEON_USAGE_READ |
1773                                          RADEON_PRIO_SAMPLER_BUFFER);
1774             }
1775          }
1776       }
1777    }
1778 
1779    /* Shader images */
1780    if (!buffer || buffer->bind_history & SI_BIND_IMAGE_BUFFER_ALL) {
1781       unsigned mask = buffer ? (buffer->bind_history & SI_BIND_IMAGE_BUFFER_SHIFT) >>
1782                                SI_BIND_IMAGE_BUFFER_SHIFT : BITFIELD_MASK(SI_NUM_SHADERS);
1783       u_foreach_bit(shader, mask) {
1784          struct si_images *images = &sctx->images[shader];
1785          struct si_descriptors *descs = si_sampler_and_image_descriptors(sctx, shader);
1786          unsigned mask = images->enabled_mask;
1787 
1788          while (mask) {
1789             unsigned i = u_bit_scan(&mask);
1790             struct pipe_resource *buffer = images->views[i].resource;
1791 
1792             if (buffer && buffer->target == PIPE_BUFFER && (!buf || buffer == buf)) {
1793                unsigned desc_slot = si_get_image_slot(i);
1794 
1795                if (images->views[i].access & PIPE_IMAGE_ACCESS_WRITE)
1796                   si_mark_image_range_valid(&images->views[i]);
1797 
1798                si_set_buf_desc_address(si_resource(buffer), images->views[i].u.buf.offset,
1799                                        descs->list + desc_slot * 8 + 4);
1800                sctx->descriptors_dirty |= 1u << si_sampler_and_image_descriptors_idx(shader);
1801                if (shader != PIPE_SHADER_COMPUTE)
1802                   si_mark_atom_dirty(sctx, &sctx->atoms.s.gfx_shader_pointers);
1803 
1804                radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, si_resource(buffer),
1805                                          RADEON_USAGE_READWRITE |
1806                                          RADEON_PRIO_SAMPLER_BUFFER);
1807 
1808                if (shader == PIPE_SHADER_COMPUTE)
1809                   sctx->compute_image_sgprs_dirty = true;
1810             }
1811          }
1812       }
1813    }
1814 
1815    /* Bindless texture handles */
1816    if (!buffer || buffer->texture_handle_allocated) {
1817       struct si_descriptors *descs = &sctx->bindless_descriptors;
1818 
1819       util_dynarray_foreach (&sctx->resident_tex_handles, struct si_texture_handle *, tex_handle) {
1820          struct pipe_sampler_view *view = (*tex_handle)->view;
1821          unsigned desc_slot = (*tex_handle)->desc_slot;
1822          struct pipe_resource *buffer = view->texture;
1823 
1824          if (buffer && buffer->target == PIPE_BUFFER && (!buf || buffer == buf)) {
1825             si_set_buf_desc_address(si_resource(buffer), view->u.buf.offset,
1826                                     descs->list + desc_slot * 16 + 4);
1827 
1828             (*tex_handle)->desc_dirty = true;
1829             si_mark_bindless_descriptors_dirty(sctx);
1830 
1831             radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, si_resource(buffer), RADEON_USAGE_READ |
1832                                       RADEON_PRIO_SAMPLER_BUFFER);
1833          }
1834       }
1835    }
1836 
1837    /* Bindless image handles */
1838    if (!buffer || buffer->image_handle_allocated) {
1839       struct si_descriptors *descs = &sctx->bindless_descriptors;
1840 
1841       util_dynarray_foreach (&sctx->resident_img_handles, struct si_image_handle *, img_handle) {
1842          struct pipe_image_view *view = &(*img_handle)->view;
1843          unsigned desc_slot = (*img_handle)->desc_slot;
1844          struct pipe_resource *buffer = view->resource;
1845 
1846          if (buffer && buffer->target == PIPE_BUFFER && (!buf || buffer == buf)) {
1847             if (view->access & PIPE_IMAGE_ACCESS_WRITE)
1848                si_mark_image_range_valid(view);
1849 
1850             si_set_buf_desc_address(si_resource(buffer), view->u.buf.offset,
1851                                     descs->list + desc_slot * 16 + 4);
1852 
1853             (*img_handle)->desc_dirty = true;
1854             si_mark_bindless_descriptors_dirty(sctx);
1855 
1856             radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, si_resource(buffer),
1857                                       RADEON_USAGE_READWRITE | RADEON_PRIO_SAMPLER_BUFFER);
1858          }
1859       }
1860    }
1861 
1862    if (buffer) {
1863       /* Do the same for other contexts. They will invoke this function
1864        * with buffer == NULL.
1865        */
1866       unsigned new_counter = p_atomic_inc_return(&sctx->screen->dirty_buf_counter);
1867 
1868       /* Skip the update for the current context, because we have already updated
1869        * the buffer bindings.
1870        */
1871       if (new_counter == sctx->last_dirty_buf_counter + 1)
1872          sctx->last_dirty_buf_counter = new_counter;
1873    }
1874 }
1875 
si_upload_bindless_descriptor(struct si_context * sctx,unsigned desc_slot,unsigned num_dwords)1876 static void si_upload_bindless_descriptor(struct si_context *sctx, unsigned desc_slot,
1877                                           unsigned num_dwords)
1878 {
1879    struct si_descriptors *desc = &sctx->bindless_descriptors;
1880    unsigned desc_slot_offset = desc_slot * 16;
1881    uint32_t *data;
1882    uint64_t va;
1883 
1884    data = desc->list + desc_slot_offset;
1885    va = desc->gpu_address + desc_slot_offset * 4;
1886 
1887    si_cp_write_data(sctx, desc->buffer, va - desc->buffer->gpu_address, num_dwords * 4, V_370_TC_L2,
1888                     V_370_ME, data);
1889 }
1890 
si_upload_bindless_descriptors(struct si_context * sctx)1891 static void si_upload_bindless_descriptors(struct si_context *sctx)
1892 {
1893    if (!sctx->bindless_descriptors_dirty)
1894       return;
1895 
1896    /* Wait for graphics/compute to be idle before updating the resident
1897     * descriptors directly in memory, in case the GPU is using them.
1898     */
1899    sctx->barrier_flags |= SI_BARRIER_SYNC_PS | SI_BARRIER_SYNC_CS;
1900    si_emit_barrier_direct(sctx);
1901 
1902    util_dynarray_foreach (&sctx->resident_tex_handles, struct si_texture_handle *, tex_handle) {
1903       unsigned desc_slot = (*tex_handle)->desc_slot;
1904 
1905       if (!(*tex_handle)->desc_dirty)
1906          continue;
1907 
1908       si_upload_bindless_descriptor(sctx, desc_slot, 16);
1909       (*tex_handle)->desc_dirty = false;
1910    }
1911 
1912    util_dynarray_foreach (&sctx->resident_img_handles, struct si_image_handle *, img_handle) {
1913       unsigned desc_slot = (*img_handle)->desc_slot;
1914 
1915       if (!(*img_handle)->desc_dirty)
1916          continue;
1917 
1918       si_upload_bindless_descriptor(sctx, desc_slot, 8);
1919       (*img_handle)->desc_dirty = false;
1920    }
1921 
1922    /* Invalidate scalar L0 because the cache doesn't know that L2 changed. */
1923    sctx->barrier_flags |= SI_BARRIER_INV_SMEM;
1924 
1925    /* TODO: Range-invalidate GL2 */
1926    if (sctx->screen->info.cp_sdma_ge_use_system_memory_scope)
1927       sctx->barrier_flags |= SI_BARRIER_INV_L2;
1928 
1929    sctx->bindless_descriptors_dirty = false;
1930 }
1931 
1932 /* Update mutable image descriptor fields of all resident textures. */
si_update_bindless_texture_descriptor(struct si_context * sctx,struct si_texture_handle * tex_handle)1933 static void si_update_bindless_texture_descriptor(struct si_context *sctx,
1934                                                   struct si_texture_handle *tex_handle)
1935 {
1936    struct si_sampler_view *sview = (struct si_sampler_view *)tex_handle->view;
1937    struct si_descriptors *desc = &sctx->bindless_descriptors;
1938    unsigned desc_slot_offset = tex_handle->desc_slot * 16;
1939    uint32_t desc_list[16];
1940 
1941    if (sview->base.texture->target == PIPE_BUFFER)
1942       return;
1943 
1944    memcpy(desc_list, desc->list + desc_slot_offset, sizeof(desc_list));
1945    si_set_sampler_view_desc(sctx, sview, &tex_handle->sstate, desc->list + desc_slot_offset);
1946 
1947    if (memcmp(desc_list, desc->list + desc_slot_offset, sizeof(desc_list))) {
1948       tex_handle->desc_dirty = true;
1949       si_mark_bindless_descriptors_dirty(sctx);
1950    }
1951 }
1952 
si_update_bindless_image_descriptor(struct si_context * sctx,struct si_image_handle * img_handle)1953 static void si_update_bindless_image_descriptor(struct si_context *sctx,
1954                                                 struct si_image_handle *img_handle)
1955 {
1956    struct si_descriptors *desc = &sctx->bindless_descriptors;
1957    unsigned desc_slot_offset = img_handle->desc_slot * 16;
1958    struct pipe_image_view *view = &img_handle->view;
1959    struct pipe_resource *res = view->resource;
1960    uint32_t image_desc[16];
1961    unsigned desc_size = (res->nr_samples >= 2 ? 16 : 8) * 4;
1962 
1963    if (res->target == PIPE_BUFFER)
1964       return;
1965 
1966    memcpy(image_desc, desc->list + desc_slot_offset, desc_size);
1967    si_set_shader_image_desc(sctx, view, true, desc->list + desc_slot_offset,
1968                             desc->list + desc_slot_offset + 8);
1969 
1970    if (memcmp(image_desc, desc->list + desc_slot_offset, desc_size)) {
1971       img_handle->desc_dirty = true;
1972       si_mark_bindless_descriptors_dirty(sctx);
1973    }
1974 }
1975 
si_update_all_resident_texture_descriptors(struct si_context * sctx)1976 static void si_update_all_resident_texture_descriptors(struct si_context *sctx)
1977 {
1978    util_dynarray_foreach (&sctx->resident_tex_handles, struct si_texture_handle *, tex_handle) {
1979       si_update_bindless_texture_descriptor(sctx, *tex_handle);
1980    }
1981 
1982    util_dynarray_foreach (&sctx->resident_img_handles, struct si_image_handle *, img_handle) {
1983       si_update_bindless_image_descriptor(sctx, *img_handle);
1984    }
1985 }
1986 
1987 /* Update mutable image descriptor fields of all bound textures. */
si_update_all_texture_descriptors(struct si_context * sctx)1988 void si_update_all_texture_descriptors(struct si_context *sctx)
1989 {
1990    unsigned shader;
1991 
1992    for (shader = 0; shader < SI_NUM_SHADERS; shader++) {
1993       struct si_samplers *samplers = &sctx->samplers[shader];
1994       struct si_images *images = &sctx->images[shader];
1995       unsigned mask;
1996 
1997       /* Images. */
1998       mask = images->enabled_mask;
1999       while (mask) {
2000          unsigned i = u_bit_scan(&mask);
2001          struct pipe_image_view *view = &images->views[i];
2002 
2003          if (!view->resource || view->resource->target == PIPE_BUFFER)
2004             continue;
2005 
2006          si_set_shader_image(sctx, shader, i, view, true);
2007       }
2008 
2009       /* Sampler views. */
2010       mask = samplers->enabled_mask;
2011       while (mask) {
2012          unsigned i = u_bit_scan(&mask);
2013          struct pipe_sampler_view *view = samplers->views[i];
2014 
2015          if (!view || !view->texture || view->texture->target == PIPE_BUFFER)
2016             continue;
2017 
2018          si_set_sampler_views(sctx, shader, i, 1, 0, false, &samplers->views[i], true);
2019       }
2020 
2021       si_update_shader_needs_decompress_mask(sctx, shader);
2022    }
2023 
2024    si_update_all_resident_texture_descriptors(sctx);
2025    si_update_ps_colorbuf0_slot(sctx);
2026 }
2027 
2028 /* SHADER USER DATA */
2029 
si_mark_shader_pointers_dirty(struct si_context * sctx,unsigned shader)2030 static void si_mark_shader_pointers_dirty(struct si_context *sctx, unsigned shader)
2031 {
2032    sctx->shader_pointers_dirty |=
2033       u_bit_consecutive(SI_DESCS_FIRST_SHADER + shader * SI_NUM_SHADER_DESCS, SI_NUM_SHADER_DESCS);
2034 
2035    if (shader == PIPE_SHADER_VERTEX)
2036       sctx->vertex_buffers_dirty = sctx->num_vertex_elements > 0;
2037 
2038    si_mark_atom_dirty(sctx, &sctx->atoms.s.gfx_shader_pointers);
2039 }
2040 
si_shader_pointers_mark_dirty(struct si_context * sctx)2041 void si_shader_pointers_mark_dirty(struct si_context *sctx)
2042 {
2043    sctx->shader_pointers_dirty =
2044       u_bit_consecutive(SI_DESCS_FIRST_SHADER, SI_NUM_DESCS - SI_DESCS_FIRST_SHADER);
2045    sctx->vertex_buffers_dirty = sctx->num_vertex_elements > 0;
2046    si_mark_atom_dirty(sctx, &sctx->atoms.s.gfx_shader_pointers);
2047    sctx->graphics_internal_bindings_pointer_dirty = sctx->descriptors[SI_DESCS_INTERNAL].buffer != NULL;
2048    sctx->compute_internal_bindings_pointer_dirty = sctx->descriptors[SI_DESCS_INTERNAL].buffer != NULL;
2049    sctx->graphics_bindless_pointer_dirty = sctx->bindless_descriptors.buffer != NULL;
2050    sctx->compute_bindless_pointer_dirty = sctx->bindless_descriptors.buffer != NULL;
2051    sctx->compute_shaderbuf_sgprs_dirty = true;
2052    sctx->compute_image_sgprs_dirty = true;
2053    if (sctx->gfx_level >= GFX11)
2054       sctx->gs_attribute_ring_pointer_dirty = true;
2055 }
2056 
2057 /* Set a base register address for user data constants in the given shader.
2058  * This assigns a mapping from PIPE_SHADER_* to SPI_SHADER_USER_DATA_*.
2059  */
si_set_user_data_base(struct si_context * sctx,unsigned shader,uint32_t new_base)2060 static void si_set_user_data_base(struct si_context *sctx, unsigned shader, uint32_t new_base)
2061 {
2062    uint32_t *base = &sctx->shader_pointers.sh_base[shader];
2063 
2064    if (*base != new_base) {
2065       *base = new_base;
2066 
2067       if (new_base)
2068          si_mark_shader_pointers_dirty(sctx, shader);
2069 
2070       /* Any change in enabled shader stages requires re-emitting
2071        * the VS state SGPR, because it contains the clamp_vertex_color
2072        * state, which can be done in VS, TES, and GS.
2073        */
2074       sctx->last_vs_state = ~0;
2075       sctx->last_gs_state = ~0;
2076    }
2077 }
2078 
2079 /* This must be called when these are changed between enabled and disabled
2080  * - geometry shader
2081  * - tessellation evaluation shader
2082  * - NGG
2083  */
si_shader_change_notify(struct si_context * sctx)2084 void si_shader_change_notify(struct si_context *sctx)
2085 {
2086    si_set_user_data_base(sctx, PIPE_SHADER_VERTEX,
2087                          si_get_user_data_base(sctx->gfx_level,
2088                                                sctx->shader.tes.cso ? TESS_ON : TESS_OFF,
2089                                                sctx->shader.gs.cso ? GS_ON : GS_OFF,
2090                                                sctx->ngg ? NGG_ON : NGG_OFF,
2091                                                PIPE_SHADER_VERTEX));
2092 
2093    si_set_user_data_base(sctx, PIPE_SHADER_TESS_EVAL,
2094                          si_get_user_data_base(sctx->gfx_level,
2095                                                sctx->shader.tes.cso ? TESS_ON : TESS_OFF,
2096                                                sctx->shader.gs.cso ? GS_ON : GS_OFF,
2097                                                sctx->ngg ? NGG_ON : NGG_OFF,
2098                                                PIPE_SHADER_TESS_EVAL));
2099 
2100    /* Update as_* flags in shader keys. Ignore disabled shader stages.
2101     *   as_ls = VS before TCS
2102     *   as_es = VS before GS or TES before GS
2103     *   as_ngg = NGG enabled for the last geometry stage.
2104     *            If GS sets as_ngg, the previous stage must set as_ngg too.
2105     */
2106    if (sctx->shader.tes.cso) {
2107       sctx->shader.vs.key.ge.as_ls = 1;
2108       sctx->shader.vs.key.ge.as_es = 0;
2109       sctx->shader.vs.key.ge.as_ngg = 0;
2110 
2111       if (sctx->shader.gs.cso) {
2112          sctx->shader.tes.key.ge.as_es = 1;
2113          sctx->shader.tes.key.ge.as_ngg = sctx->ngg;
2114          sctx->shader.gs.key.ge.as_ngg = sctx->ngg;
2115       } else {
2116          sctx->shader.tes.key.ge.as_es = 0;
2117          sctx->shader.tes.key.ge.as_ngg = sctx->ngg;
2118       }
2119    } else if (sctx->shader.gs.cso) {
2120       sctx->shader.vs.key.ge.as_ls = 0;
2121       sctx->shader.vs.key.ge.as_es = 1;
2122       sctx->shader.vs.key.ge.as_ngg = sctx->ngg;
2123       sctx->shader.gs.key.ge.as_ngg = sctx->ngg;
2124    } else {
2125       sctx->shader.vs.key.ge.as_ls = 0;
2126       sctx->shader.vs.key.ge.as_es = 0;
2127       sctx->shader.vs.key.ge.as_ngg = sctx->ngg;
2128    }
2129 }
2130 
2131 #define si_emit_consecutive_shader_pointers(sctx, pointer_mask, sh_base, type) do { \
2132    unsigned sh_reg_base = (sh_base); \
2133    if (sh_reg_base) { \
2134       unsigned mask = shader_pointers_dirty & (pointer_mask); \
2135       \
2136       while (mask) { \
2137          int start, count; \
2138          u_bit_scan_consecutive_range(&mask, &start, &count); \
2139          \
2140          struct si_descriptors *descs = &sctx->descriptors[start]; \
2141          unsigned sh_offset = sh_reg_base + descs->shader_userdata_offset; \
2142          \
2143          radeon_set_sh_reg_seq(sh_offset, count); \
2144          for (int i = 0; i < count; i++) \
2145             radeon_emit_32bit_pointer(descs[i].gpu_address); \
2146       } \
2147    } \
2148 } while (0)
2149 
2150 #define gfx11_push_consecutive_shader_pointers(sctx, pointer_mask, sh_base, type) do { \
2151    unsigned sh_reg_base = (sh_base); \
2152    if (sh_reg_base) { \
2153       unsigned mask = shader_pointers_dirty & (pointer_mask); \
2154       \
2155       u_foreach_bit(i, mask) { \
2156          struct si_descriptors *descs = &sctx->descriptors[i]; \
2157          unsigned sh_reg = sh_reg_base + descs->shader_userdata_offset; \
2158          \
2159          gfx11_push_##type##_sh_reg(sh_reg, descs->gpu_address); \
2160       } \
2161    } \
2162 } while (0)
2163 
2164 #define gfx12_push_consecutive_shader_pointers(sctx, pointer_mask, sh_base, type) do { \
2165    unsigned sh_reg_base = (sh_base); \
2166    if (sh_reg_base) { \
2167       unsigned mask = shader_pointers_dirty & (pointer_mask); \
2168       \
2169       u_foreach_bit(i, mask) { \
2170          struct si_descriptors *descs = &sctx->descriptors[i]; \
2171          unsigned sh_reg = sh_reg_base + descs->shader_userdata_offset; \
2172          \
2173          gfx12_push_##type##_sh_reg(sh_reg, descs->gpu_address); \
2174       } \
2175    } \
2176 } while (0)
2177 
si_emit_global_shader_pointers(struct si_context * sctx,struct si_descriptors * descs)2178 static void si_emit_global_shader_pointers(struct si_context *sctx, struct si_descriptors *descs)
2179 {
2180    assert(sctx->gfx_level < GFX12 && !sctx->screen->info.has_set_sh_pairs_packed);
2181 
2182    radeon_begin(&sctx->gfx_cs);
2183 
2184    if (sctx->gfx_level >= GFX11) {
2185       radeon_emit_one_32bit_pointer(descs, R_00B030_SPI_SHADER_USER_DATA_PS_0);
2186       radeon_emit_one_32bit_pointer(descs, R_00B230_SPI_SHADER_USER_DATA_GS_0);
2187       radeon_emit_one_32bit_pointer(descs, R_00B430_SPI_SHADER_USER_DATA_HS_0);
2188    } else if (sctx->gfx_level >= GFX10) {
2189       radeon_emit_one_32bit_pointer(descs, R_00B030_SPI_SHADER_USER_DATA_PS_0);
2190       /* HW VS stage only used in non-NGG mode. */
2191       radeon_emit_one_32bit_pointer(descs, R_00B130_SPI_SHADER_USER_DATA_VS_0);
2192       radeon_emit_one_32bit_pointer(descs, R_00B230_SPI_SHADER_USER_DATA_GS_0);
2193       radeon_emit_one_32bit_pointer(descs, R_00B430_SPI_SHADER_USER_DATA_HS_0);
2194    } else if (sctx->gfx_level == GFX9 && sctx->shadowing.registers) {
2195       /* We can't use the COMMON registers with register shadowing. */
2196       radeon_emit_one_32bit_pointer(descs, R_00B030_SPI_SHADER_USER_DATA_PS_0);
2197       radeon_emit_one_32bit_pointer(descs, R_00B130_SPI_SHADER_USER_DATA_VS_0);
2198       radeon_emit_one_32bit_pointer(descs, R_00B330_SPI_SHADER_USER_DATA_ES_0);
2199       radeon_emit_one_32bit_pointer(descs, R_00B430_SPI_SHADER_USER_DATA_LS_0);
2200    } else if (sctx->gfx_level == GFX9) {
2201       /* Broadcast it to all shader stages. */
2202       radeon_emit_one_32bit_pointer(descs, R_00B530_SPI_SHADER_USER_DATA_COMMON_0);
2203    } else {
2204       radeon_emit_one_32bit_pointer(descs, R_00B030_SPI_SHADER_USER_DATA_PS_0);
2205       radeon_emit_one_32bit_pointer(descs, R_00B130_SPI_SHADER_USER_DATA_VS_0);
2206       radeon_emit_one_32bit_pointer(descs, R_00B330_SPI_SHADER_USER_DATA_ES_0);
2207       radeon_emit_one_32bit_pointer(descs, R_00B230_SPI_SHADER_USER_DATA_GS_0);
2208       radeon_emit_one_32bit_pointer(descs, R_00B430_SPI_SHADER_USER_DATA_HS_0);
2209       radeon_emit_one_32bit_pointer(descs, R_00B530_SPI_SHADER_USER_DATA_LS_0);
2210    }
2211    radeon_end();
2212 }
2213 
gfx11_push_global_shader_pointers(struct si_context * sctx,struct si_descriptors * descs)2214 static void gfx11_push_global_shader_pointers(struct si_context *sctx, struct si_descriptors *descs)
2215 {
2216    gfx11_push_gfx_sh_reg(R_00B030_SPI_SHADER_USER_DATA_PS_0 + descs->shader_userdata_offset,
2217                          descs->gpu_address);
2218    gfx11_push_gfx_sh_reg(R_00B230_SPI_SHADER_USER_DATA_GS_0 + descs->shader_userdata_offset,
2219                          descs->gpu_address);
2220    gfx11_push_gfx_sh_reg(R_00B430_SPI_SHADER_USER_DATA_HS_0 + descs->shader_userdata_offset,
2221                          descs->gpu_address);
2222 }
2223 
gfx12_push_global_shader_pointers(struct si_context * sctx,struct si_descriptors * descs)2224 static void gfx12_push_global_shader_pointers(struct si_context *sctx, struct si_descriptors *descs)
2225 {
2226    gfx12_push_gfx_sh_reg(R_00B030_SPI_SHADER_USER_DATA_PS_0 + descs->shader_userdata_offset,
2227                          descs->gpu_address);
2228    gfx12_push_gfx_sh_reg(R_00B230_SPI_SHADER_USER_DATA_GS_0 + descs->shader_userdata_offset,
2229                          descs->gpu_address);
2230    gfx12_push_gfx_sh_reg(R_00B430_SPI_SHADER_USER_DATA_HS_0 + descs->shader_userdata_offset,
2231                          descs->gpu_address);
2232 }
2233 
si_emit_graphics_shader_pointers(struct si_context * sctx,unsigned index)2234 void si_emit_graphics_shader_pointers(struct si_context *sctx, unsigned index)
2235 {
2236    uint32_t *sh_base = sctx->shader_pointers.sh_base;
2237    unsigned all_gfx_desc_mask = BITFIELD_RANGE(0, SI_DESCS_FIRST_COMPUTE);
2238    unsigned descriptors_dirty = sctx->descriptors_dirty & all_gfx_desc_mask;
2239    unsigned shader_pointers_dirty = sctx->shader_pointers_dirty | descriptors_dirty;
2240 
2241    if (descriptors_dirty & BITFIELD_BIT(SI_DESCS_INTERNAL)) {
2242       sctx->graphics_internal_bindings_pointer_dirty = true;
2243       sctx->compute_internal_bindings_pointer_dirty = true;
2244    }
2245 
2246    /* Blits shouldn't set VS shader pointers. */
2247    if (sctx->num_vs_blit_sgprs)
2248       shader_pointers_dirty &= ~SI_DESCS_SHADER_MASK(VERTEX);
2249 
2250    /* Upload descriptors. */
2251    if (descriptors_dirty) {
2252       sctx->descriptors_dirty &= ~descriptors_dirty;
2253 
2254       do {
2255          si_upload_descriptors(sctx, &sctx->descriptors[u_bit_scan(&descriptors_dirty)]);
2256       } while (descriptors_dirty);
2257    }
2258 
2259    si_upload_bindless_descriptors(sctx);
2260 
2261    /* Set shader pointers. */
2262    if (sctx->gfx_level >= GFX12) {
2263       gfx12_push_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(VERTEX),
2264                                              sh_base[PIPE_SHADER_VERTEX], gfx);
2265       gfx12_push_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(TESS_EVAL),
2266                                              sh_base[PIPE_SHADER_TESS_EVAL], gfx);
2267       gfx12_push_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(FRAGMENT),
2268                                              sh_base[PIPE_SHADER_FRAGMENT], gfx);
2269       gfx12_push_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(TESS_CTRL),
2270                                              sh_base[PIPE_SHADER_TESS_CTRL], gfx);
2271       gfx12_push_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(GEOMETRY),
2272                                              sh_base[PIPE_SHADER_GEOMETRY], gfx);
2273 
2274       if (sctx->gs_attribute_ring_pointer_dirty) {
2275          gfx12_push_gfx_sh_reg(R_00B230_SPI_SHADER_USER_DATA_GS_0 +
2276                                GFX9_SGPR_ATTRIBUTE_RING_ADDR * 4,
2277                                sctx->screen->attribute_pos_prim_ring->gpu_address);
2278          sctx->gs_attribute_ring_pointer_dirty = false;
2279       }
2280 
2281       if (sctx->graphics_internal_bindings_pointer_dirty) {
2282          gfx12_push_global_shader_pointers(sctx, &sctx->descriptors[SI_DESCS_INTERNAL]);
2283          sctx->graphics_internal_bindings_pointer_dirty = false;
2284       }
2285 
2286       if (sctx->graphics_bindless_pointer_dirty) {
2287          gfx12_push_global_shader_pointers(sctx, &sctx->bindless_descriptors);
2288          sctx->graphics_bindless_pointer_dirty = false;
2289       }
2290    } else if (sctx->screen->info.has_set_sh_pairs_packed) {
2291       gfx11_push_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(VERTEX),
2292                                              sh_base[PIPE_SHADER_VERTEX], gfx);
2293       gfx11_push_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(TESS_EVAL),
2294                                              sh_base[PIPE_SHADER_TESS_EVAL], gfx);
2295       gfx11_push_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(FRAGMENT),
2296                                              sh_base[PIPE_SHADER_FRAGMENT], gfx);
2297       gfx11_push_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(TESS_CTRL),
2298                                              sh_base[PIPE_SHADER_TESS_CTRL], gfx);
2299       gfx11_push_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(GEOMETRY),
2300                                              sh_base[PIPE_SHADER_GEOMETRY], gfx);
2301 
2302       if (sctx->gs_attribute_ring_pointer_dirty) {
2303          gfx11_push_gfx_sh_reg(R_00B230_SPI_SHADER_USER_DATA_GS_0 +
2304                                GFX9_SGPR_ATTRIBUTE_RING_ADDR * 4,
2305                                sctx->screen->attribute_pos_prim_ring->gpu_address);
2306          sctx->gs_attribute_ring_pointer_dirty = false;
2307       }
2308 
2309       if (sctx->graphics_internal_bindings_pointer_dirty) {
2310          gfx11_push_global_shader_pointers(sctx, &sctx->descriptors[SI_DESCS_INTERNAL]);
2311          sctx->graphics_internal_bindings_pointer_dirty = false;
2312       }
2313 
2314       if (sctx->graphics_bindless_pointer_dirty) {
2315          gfx11_push_global_shader_pointers(sctx, &sctx->bindless_descriptors);
2316          sctx->graphics_bindless_pointer_dirty = false;
2317       }
2318    } else {
2319       radeon_begin(&sctx->gfx_cs);
2320       si_emit_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(VERTEX),
2321                                           sh_base[PIPE_SHADER_VERTEX], gfx);
2322       si_emit_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(TESS_EVAL),
2323                                           sh_base[PIPE_SHADER_TESS_EVAL], gfx);
2324       si_emit_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(FRAGMENT),
2325                                           sh_base[PIPE_SHADER_FRAGMENT], gfx);
2326       si_emit_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(TESS_CTRL),
2327                                           sh_base[PIPE_SHADER_TESS_CTRL], gfx);
2328       si_emit_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(GEOMETRY),
2329                                           sh_base[PIPE_SHADER_GEOMETRY], gfx);
2330 
2331       if (sctx->gs_attribute_ring_pointer_dirty) {
2332          assert(sctx->gfx_level >= GFX11);
2333          radeon_set_sh_reg(R_00B230_SPI_SHADER_USER_DATA_GS_0 +
2334                            GFX9_SGPR_ATTRIBUTE_RING_ADDR * 4,
2335                            sctx->screen->attribute_pos_prim_ring->gpu_address);
2336          sctx->gs_attribute_ring_pointer_dirty = false;
2337       }
2338       radeon_end();
2339 
2340       if (sctx->graphics_internal_bindings_pointer_dirty) {
2341          si_emit_global_shader_pointers(sctx, &sctx->descriptors[SI_DESCS_INTERNAL]);
2342          sctx->graphics_internal_bindings_pointer_dirty = false;
2343       }
2344 
2345       if (sctx->graphics_bindless_pointer_dirty) {
2346          si_emit_global_shader_pointers(sctx, &sctx->bindless_descriptors);
2347          sctx->graphics_bindless_pointer_dirty = false;
2348       }
2349    }
2350 
2351    sctx->shader_pointers_dirty &= ~all_gfx_desc_mask;
2352 }
2353 
si_emit_compute_shader_pointers(struct si_context * sctx)2354 void si_emit_compute_shader_pointers(struct si_context *sctx)
2355 {
2356    /* This does not update internal bindings as that is not needed for compute shaders. */
2357    unsigned descriptors_dirty = sctx->descriptors_dirty &
2358                                 (BITFIELD_BIT(SI_DESCS_INTERNAL) | SI_DESCS_SHADER_MASK(COMPUTE));
2359    unsigned shader_pointers_dirty = sctx->shader_pointers_dirty | descriptors_dirty;
2360 
2361    if (descriptors_dirty & BITFIELD_BIT(SI_DESCS_INTERNAL)) {
2362       sctx->graphics_internal_bindings_pointer_dirty = true;
2363       sctx->compute_internal_bindings_pointer_dirty = true;
2364    }
2365 
2366    /* Upload descriptors. */
2367    if (descriptors_dirty) {
2368       sctx->descriptors_dirty &= ~descriptors_dirty;
2369 
2370       do {
2371          si_upload_descriptors(sctx, &sctx->descriptors[u_bit_scan(&descriptors_dirty)]);
2372       } while (descriptors_dirty);
2373    }
2374 
2375    si_upload_bindless_descriptors(sctx);
2376 
2377    radeon_begin(&sctx->gfx_cs);
2378 
2379    /* Set shader pointers. */
2380    if (sctx->gfx_level >= GFX12) {
2381       gfx12_push_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(COMPUTE),
2382                                              R_00B900_COMPUTE_USER_DATA_0, compute);
2383 
2384       if (sctx->compute_internal_bindings_pointer_dirty) {
2385          gfx12_push_compute_sh_reg(R_00B900_COMPUTE_USER_DATA_0 +
2386                                    sctx->descriptors[SI_DESCS_INTERNAL].shader_userdata_offset,
2387                                    sctx->descriptors[SI_DESCS_INTERNAL].gpu_address);
2388          sctx->compute_internal_bindings_pointer_dirty = false;
2389       }
2390 
2391       if (sctx->compute_bindless_pointer_dirty) {
2392          gfx12_push_compute_sh_reg(R_00B900_COMPUTE_USER_DATA_0 +
2393                                    sctx->bindless_descriptors.shader_userdata_offset,
2394                                    sctx->bindless_descriptors.gpu_address);
2395          sctx->compute_bindless_pointer_dirty = false;
2396       }
2397    } else if (sctx->screen->info.has_set_sh_pairs_packed) {
2398       gfx11_push_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(COMPUTE),
2399                                              R_00B900_COMPUTE_USER_DATA_0, compute);
2400 
2401       if (sctx->compute_internal_bindings_pointer_dirty) {
2402          gfx11_push_compute_sh_reg(R_00B900_COMPUTE_USER_DATA_0 +
2403                                    sctx->descriptors[SI_DESCS_INTERNAL].shader_userdata_offset,
2404                                    sctx->descriptors[SI_DESCS_INTERNAL].gpu_address);
2405          sctx->compute_internal_bindings_pointer_dirty = false;
2406       }
2407 
2408       if (sctx->compute_bindless_pointer_dirty) {
2409          gfx11_push_compute_sh_reg(R_00B900_COMPUTE_USER_DATA_0 +
2410                                    sctx->bindless_descriptors.shader_userdata_offset,
2411                                    sctx->bindless_descriptors.gpu_address);
2412          sctx->compute_bindless_pointer_dirty = false;
2413       }
2414    } else {
2415       si_emit_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(COMPUTE),
2416                                           R_00B900_COMPUTE_USER_DATA_0, compute);
2417 
2418       if (sctx->compute_internal_bindings_pointer_dirty) {
2419          radeon_emit_one_32bit_pointer(&sctx->descriptors[SI_DESCS_INTERNAL],
2420                                        R_00B900_COMPUTE_USER_DATA_0);
2421          sctx->compute_internal_bindings_pointer_dirty = false;
2422       }
2423 
2424       if (sctx->compute_bindless_pointer_dirty) {
2425          radeon_emit_one_32bit_pointer(&sctx->bindless_descriptors,
2426                                        R_00B900_COMPUTE_USER_DATA_0);
2427          sctx->compute_bindless_pointer_dirty = false;
2428       }
2429    }
2430 
2431    sctx->shader_pointers_dirty &= ~SI_DESCS_SHADER_MASK(COMPUTE);
2432 
2433    /* Set shader buffer descriptors in user SGPRs. */
2434    struct si_shader_selector *shader = &sctx->cs_shader_state.program->sel;
2435    unsigned num_shaderbufs = shader->cs_num_shaderbufs_in_user_sgprs;
2436 
2437    if (num_shaderbufs && sctx->compute_shaderbuf_sgprs_dirty) {
2438       struct si_descriptors *desc = si_const_and_shader_buffer_descriptors(sctx, PIPE_SHADER_COMPUTE);
2439 
2440       radeon_set_sh_reg_seq(R_00B900_COMPUTE_USER_DATA_0 +
2441                             shader->cs_shaderbufs_sgpr_index * 4,
2442                             num_shaderbufs * 4);
2443 
2444       for (unsigned i = 0; i < num_shaderbufs; i++)
2445          radeon_emit_array(&desc->list[si_get_shaderbuf_slot(i) * 4], 4);
2446 
2447       sctx->compute_shaderbuf_sgprs_dirty = false;
2448    }
2449 
2450    /* Set image descriptors in user SGPRs. */
2451    unsigned num_images = shader->cs_num_images_in_user_sgprs;
2452    if (num_images && sctx->compute_image_sgprs_dirty) {
2453       struct si_descriptors *desc = si_sampler_and_image_descriptors(sctx, PIPE_SHADER_COMPUTE);
2454 
2455       radeon_set_sh_reg_seq(R_00B900_COMPUTE_USER_DATA_0 +
2456                             shader->cs_images_sgpr_index * 4,
2457                             shader->cs_images_num_sgprs);
2458 
2459       for (unsigned i = 0; i < num_images; i++) {
2460          unsigned desc_offset = si_get_image_slot(i) * 8;
2461          unsigned num_sgprs = 8;
2462 
2463          /* Image buffers are in desc[4..7]. */
2464          if (BITSET_TEST(shader->info.base.image_buffers, i)) {
2465             desc_offset += 4;
2466             num_sgprs = 4;
2467          }
2468 
2469          radeon_emit_array(&desc->list[desc_offset], num_sgprs);
2470       }
2471 
2472       sctx->compute_image_sgprs_dirty = false;
2473    }
2474    radeon_end();
2475 }
2476 
2477 /* BINDLESS */
2478 
si_init_bindless_descriptors(struct si_context * sctx,struct si_descriptors * desc,short shader_userdata_rel_index,unsigned num_elements)2479 static void si_init_bindless_descriptors(struct si_context *sctx, struct si_descriptors *desc,
2480                                          short shader_userdata_rel_index, unsigned num_elements)
2481 {
2482    ASSERTED unsigned desc_slot;
2483 
2484    si_init_descriptors(desc, shader_userdata_rel_index, 16, num_elements);
2485    sctx->bindless_descriptors.num_active_slots = num_elements;
2486 
2487    /* The first bindless descriptor is stored at slot 1, because 0 is not
2488     * considered to be a valid handle.
2489     */
2490    sctx->num_bindless_descriptors = 1;
2491 
2492    /* Track which bindless slots are used (or not). */
2493    util_idalloc_init(&sctx->bindless_used_slots, num_elements);
2494 
2495    /* Reserve slot 0 because it's an invalid handle for bindless. */
2496    desc_slot = util_idalloc_alloc(&sctx->bindless_used_slots);
2497    assert(desc_slot == 0);
2498 }
2499 
si_release_bindless_descriptors(struct si_context * sctx)2500 static void si_release_bindless_descriptors(struct si_context *sctx)
2501 {
2502    si_release_descriptors(&sctx->bindless_descriptors);
2503    util_idalloc_fini(&sctx->bindless_used_slots);
2504 }
2505 
si_get_first_free_bindless_slot(struct si_context * sctx)2506 static unsigned si_get_first_free_bindless_slot(struct si_context *sctx)
2507 {
2508    struct si_descriptors *desc = &sctx->bindless_descriptors;
2509    unsigned desc_slot;
2510 
2511    desc_slot = util_idalloc_alloc(&sctx->bindless_used_slots);
2512    if (desc_slot >= desc->num_elements) {
2513       /* The array of bindless descriptors is full, resize it. */
2514       unsigned slot_size = desc->element_dw_size * 4;
2515       unsigned new_num_elements = desc->num_elements * 2;
2516 
2517       desc->list =
2518          REALLOC(desc->list, desc->num_elements * slot_size, new_num_elements * slot_size);
2519       desc->num_elements = new_num_elements;
2520       desc->num_active_slots = new_num_elements;
2521    }
2522 
2523    assert(desc_slot);
2524    return desc_slot;
2525 }
2526 
si_create_bindless_descriptor(struct si_context * sctx,uint32_t * desc_list,unsigned size)2527 static unsigned si_create_bindless_descriptor(struct si_context *sctx, uint32_t *desc_list,
2528                                               unsigned size)
2529 {
2530    struct si_descriptors *desc = &sctx->bindless_descriptors;
2531    unsigned desc_slot, desc_slot_offset;
2532 
2533    /* Find a free slot. */
2534    desc_slot = si_get_first_free_bindless_slot(sctx);
2535 
2536    /* For simplicity, sampler and image bindless descriptors use fixed
2537     * 16-dword slots for now. Image descriptors only need 8-dword but this
2538     * doesn't really matter because no real apps use image handles.
2539     */
2540    desc_slot_offset = desc_slot * 16;
2541 
2542    /* Copy the descriptor into the array. */
2543    memcpy(desc->list + desc_slot_offset, desc_list, size);
2544 
2545    /* Re-upload the whole array of bindless descriptors into a new buffer.
2546     */
2547    si_upload_descriptors(sctx, desc);
2548 
2549    /* Make sure to re-emit the shader pointers for all stages. */
2550    sctx->graphics_bindless_pointer_dirty = true;
2551    sctx->compute_bindless_pointer_dirty = true;
2552    si_mark_atom_dirty(sctx, &sctx->atoms.s.gfx_shader_pointers);
2553 
2554    return desc_slot;
2555 }
2556 
si_update_bindless_buffer_descriptor(struct si_context * sctx,unsigned desc_slot,struct pipe_resource * resource,uint64_t offset,bool * desc_dirty)2557 static void si_update_bindless_buffer_descriptor(struct si_context *sctx, unsigned desc_slot,
2558                                                  struct pipe_resource *resource, uint64_t offset,
2559                                                  bool *desc_dirty)
2560 {
2561    struct si_descriptors *desc = &sctx->bindless_descriptors;
2562    struct si_resource *buf = si_resource(resource);
2563    unsigned desc_slot_offset = desc_slot * 16;
2564    uint32_t *desc_list = desc->list + desc_slot_offset + 4;
2565    uint64_t old_desc_va;
2566 
2567    assert(resource->target == PIPE_BUFFER);
2568 
2569    /* Retrieve the old buffer addr from the descriptor. */
2570    old_desc_va = si_desc_extract_buffer_address(desc_list);
2571 
2572    if (old_desc_va != buf->gpu_address + offset) {
2573       /* The buffer has been invalidated when the handle wasn't
2574        * resident, update the descriptor and the dirty flag.
2575        */
2576       si_set_buf_desc_address(buf, offset, &desc_list[0]);
2577 
2578       *desc_dirty = true;
2579    }
2580 }
2581 
si_create_texture_handle(struct pipe_context * ctx,struct pipe_sampler_view * view,const struct pipe_sampler_state * state)2582 static uint64_t si_create_texture_handle(struct pipe_context *ctx, struct pipe_sampler_view *view,
2583                                          const struct pipe_sampler_state *state)
2584 {
2585    struct si_sampler_view *sview = (struct si_sampler_view *)view;
2586    struct si_context *sctx = (struct si_context *)ctx;
2587    struct si_texture_handle *tex_handle;
2588    struct si_sampler_state *sstate;
2589    uint32_t desc_list[16];
2590    uint64_t handle;
2591 
2592    tex_handle = CALLOC_STRUCT(si_texture_handle);
2593    if (!tex_handle)
2594       return 0;
2595 
2596    memset(desc_list, 0, sizeof(desc_list));
2597    si_init_descriptor_list(&desc_list[0], 16, 1, null_texture_descriptor);
2598 
2599    sstate = ctx->create_sampler_state(ctx, state);
2600    if (!sstate) {
2601       FREE(tex_handle);
2602       return 0;
2603    }
2604 
2605    si_set_sampler_view_desc(sctx, sview, sstate, &desc_list[0]);
2606    memcpy(&tex_handle->sstate, sstate, sizeof(*sstate));
2607    ctx->delete_sampler_state(ctx, sstate);
2608 
2609    tex_handle->desc_slot = si_create_bindless_descriptor(sctx, desc_list, sizeof(desc_list));
2610    if (!tex_handle->desc_slot) {
2611       FREE(tex_handle);
2612       return 0;
2613    }
2614 
2615    handle = tex_handle->desc_slot;
2616 
2617    if (!_mesa_hash_table_insert(sctx->tex_handles, (void *)(uintptr_t)handle, tex_handle)) {
2618       FREE(tex_handle);
2619       return 0;
2620    }
2621 
2622    pipe_sampler_view_reference(&tex_handle->view, view);
2623 
2624    si_resource(sview->base.texture)->texture_handle_allocated = true;
2625 
2626    return handle;
2627 }
2628 
si_delete_texture_handle(struct pipe_context * ctx,uint64_t handle)2629 static void si_delete_texture_handle(struct pipe_context *ctx, uint64_t handle)
2630 {
2631    struct si_context *sctx = (struct si_context *)ctx;
2632    struct si_texture_handle *tex_handle;
2633    struct hash_entry *entry;
2634 
2635    entry = _mesa_hash_table_search(sctx->tex_handles, (void *)(uintptr_t)handle);
2636    if (!entry)
2637       return;
2638 
2639    tex_handle = (struct si_texture_handle *)entry->data;
2640 
2641    /* Allow this descriptor slot to be re-used. */
2642    util_idalloc_free(&sctx->bindless_used_slots, tex_handle->desc_slot);
2643 
2644    pipe_sampler_view_reference(&tex_handle->view, NULL);
2645    _mesa_hash_table_remove(sctx->tex_handles, entry);
2646    FREE(tex_handle);
2647 }
2648 
si_make_texture_handle_resident(struct pipe_context * ctx,uint64_t handle,bool resident)2649 static void si_make_texture_handle_resident(struct pipe_context *ctx, uint64_t handle,
2650                                             bool resident)
2651 {
2652    struct si_context *sctx = (struct si_context *)ctx;
2653    struct si_texture_handle *tex_handle;
2654    struct si_sampler_view *sview;
2655    struct hash_entry *entry;
2656 
2657    entry = _mesa_hash_table_search(sctx->tex_handles, (void *)(uintptr_t)handle);
2658    if (!entry)
2659       return;
2660 
2661    tex_handle = (struct si_texture_handle *)entry->data;
2662    sview = (struct si_sampler_view *)tex_handle->view;
2663 
2664    if (resident) {
2665       if (sview->base.texture->target != PIPE_BUFFER) {
2666          struct si_texture *tex = (struct si_texture *)sview->base.texture;
2667 
2668          if (sctx->gfx_level < GFX12) {
2669             if (depth_needs_decompression(tex, sview->is_stencil_sampler)) {
2670                util_dynarray_append(&sctx->resident_tex_needs_depth_decompress,
2671                                     struct si_texture_handle *, tex_handle);
2672             }
2673 
2674             if (color_needs_decompression(tex)) {
2675                util_dynarray_append(&sctx->resident_tex_needs_color_decompress,
2676                                     struct si_texture_handle *, tex_handle);
2677             }
2678 
2679             if (vi_dcc_enabled(tex, sview->base.u.tex.first_level) &&
2680                 p_atomic_read(&tex->framebuffers_bound))
2681                sctx->need_check_render_feedback = true;
2682          }
2683 
2684          si_update_bindless_texture_descriptor(sctx, tex_handle);
2685       } else {
2686          si_update_bindless_buffer_descriptor(sctx, tex_handle->desc_slot, sview->base.texture,
2687                                               sview->base.u.buf.offset, &tex_handle->desc_dirty);
2688       }
2689 
2690       /* Re-upload the descriptor if it has been updated while it
2691        * wasn't resident.
2692        */
2693       if (tex_handle->desc_dirty)
2694          si_mark_bindless_descriptors_dirty(sctx);
2695 
2696       /* Add the texture handle to the per-context list. */
2697       util_dynarray_append(&sctx->resident_tex_handles, struct si_texture_handle *, tex_handle);
2698 
2699       /* Add the buffers to the current CS in case si_begin_new_cs()
2700        * is not going to be called.
2701        */
2702       si_sampler_view_add_buffer(sctx, sview->base.texture, RADEON_USAGE_READ,
2703                                  sview->is_stencil_sampler);
2704    } else {
2705       /* Remove the texture handle from the per-context list. */
2706       util_dynarray_delete_unordered(&sctx->resident_tex_handles, struct si_texture_handle *,
2707                                      tex_handle);
2708 
2709       if (sctx->gfx_level < GFX12 && sview->base.texture->target != PIPE_BUFFER) {
2710          util_dynarray_delete_unordered(&sctx->resident_tex_needs_depth_decompress,
2711                                         struct si_texture_handle *, tex_handle);
2712 
2713          util_dynarray_delete_unordered(&sctx->resident_tex_needs_color_decompress,
2714                                         struct si_texture_handle *, tex_handle);
2715       }
2716    }
2717 }
2718 
si_create_image_handle(struct pipe_context * ctx,const struct pipe_image_view * view)2719 static uint64_t si_create_image_handle(struct pipe_context *ctx, const struct pipe_image_view *view)
2720 {
2721    struct si_context *sctx = (struct si_context *)ctx;
2722    struct si_image_handle *img_handle;
2723    uint32_t desc_list[16];
2724    uint64_t handle;
2725 
2726    if (!view || !view->resource)
2727       return 0;
2728 
2729    img_handle = CALLOC_STRUCT(si_image_handle);
2730    if (!img_handle)
2731       return 0;
2732 
2733    memset(desc_list, 0, sizeof(desc_list));
2734    si_init_descriptor_list(&desc_list[0], 8, 2, null_image_descriptor);
2735 
2736    si_set_shader_image_desc(sctx, view, false, &desc_list[0], &desc_list[8]);
2737 
2738    img_handle->desc_slot = si_create_bindless_descriptor(sctx, desc_list, sizeof(desc_list));
2739    if (!img_handle->desc_slot) {
2740       FREE(img_handle);
2741       return 0;
2742    }
2743 
2744    handle = img_handle->desc_slot;
2745 
2746    if (!_mesa_hash_table_insert(sctx->img_handles, (void *)(uintptr_t)handle, img_handle)) {
2747       FREE(img_handle);
2748       return 0;
2749    }
2750 
2751    util_copy_image_view(&img_handle->view, view);
2752 
2753    si_resource(view->resource)->image_handle_allocated = true;
2754 
2755    if (view->access & PIPE_IMAGE_ACCESS_WRITE && view->resource)
2756       si_mark_image_range_valid(view);
2757 
2758    return handle;
2759 }
2760 
si_delete_image_handle(struct pipe_context * ctx,uint64_t handle)2761 static void si_delete_image_handle(struct pipe_context *ctx, uint64_t handle)
2762 {
2763    struct si_context *sctx = (struct si_context *)ctx;
2764    struct si_image_handle *img_handle;
2765    struct hash_entry *entry;
2766 
2767    entry = _mesa_hash_table_search(sctx->img_handles, (void *)(uintptr_t)handle);
2768    if (!entry)
2769       return;
2770 
2771    img_handle = (struct si_image_handle *)entry->data;
2772 
2773    util_copy_image_view(&img_handle->view, NULL);
2774    _mesa_hash_table_remove(sctx->img_handles, entry);
2775    FREE(img_handle);
2776 }
2777 
si_make_image_handle_resident(struct pipe_context * ctx,uint64_t handle,unsigned access,bool resident)2778 static void si_make_image_handle_resident(struct pipe_context *ctx, uint64_t handle,
2779                                           unsigned access, bool resident)
2780 {
2781    struct si_context *sctx = (struct si_context *)ctx;
2782    struct si_image_handle *img_handle;
2783    struct pipe_image_view *view;
2784    struct si_resource *res;
2785    struct hash_entry *entry;
2786 
2787    entry = _mesa_hash_table_search(sctx->img_handles, (void *)(uintptr_t)handle);
2788    if (!entry)
2789       return;
2790 
2791    img_handle = (struct si_image_handle *)entry->data;
2792    view = &img_handle->view;
2793    res = si_resource(view->resource);
2794 
2795    if (resident) {
2796       if (res->b.b.target != PIPE_BUFFER) {
2797          struct si_texture *tex = (struct si_texture *)res;
2798          unsigned level = view->u.tex.level;
2799 
2800          if (sctx->gfx_level < GFX12) {
2801             if (color_needs_decompression(tex)) {
2802                util_dynarray_append(&sctx->resident_img_needs_color_decompress,
2803                                     struct si_image_handle *, img_handle);
2804             }
2805 
2806             if (vi_dcc_enabled(tex, level) && p_atomic_read(&tex->framebuffers_bound))
2807                sctx->need_check_render_feedback = true;
2808          }
2809 
2810          si_update_bindless_image_descriptor(sctx, img_handle);
2811       } else {
2812          si_update_bindless_buffer_descriptor(sctx, img_handle->desc_slot, view->resource,
2813                                               view->u.buf.offset, &img_handle->desc_dirty);
2814       }
2815 
2816       /* Re-upload the descriptor if it has been updated while it
2817        * wasn't resident.
2818        */
2819       if (img_handle->desc_dirty)
2820          si_mark_bindless_descriptors_dirty(sctx);
2821 
2822       /* Add the image handle to the per-context list. */
2823       util_dynarray_append(&sctx->resident_img_handles, struct si_image_handle *, img_handle);
2824 
2825       /* Add the buffers to the current CS in case si_begin_new_cs()
2826        * is not going to be called.
2827        */
2828       si_sampler_view_add_buffer(sctx, view->resource,
2829                                  (access & PIPE_IMAGE_ACCESS_WRITE) ?
2830                                     RADEON_USAGE_READWRITE : RADEON_USAGE_READ, false);
2831    } else {
2832       /* Remove the image handle from the per-context list. */
2833       util_dynarray_delete_unordered(&sctx->resident_img_handles, struct si_image_handle *,
2834                                      img_handle);
2835 
2836       if (sctx->gfx_level < GFX12 && res->b.b.target != PIPE_BUFFER) {
2837          util_dynarray_delete_unordered(&sctx->resident_img_needs_color_decompress,
2838                                         struct si_image_handle *, img_handle);
2839       }
2840    }
2841 }
2842 
si_resident_buffers_add_all_to_bo_list(struct si_context * sctx)2843 static void si_resident_buffers_add_all_to_bo_list(struct si_context *sctx)
2844 {
2845    unsigned num_resident_tex_handles, num_resident_img_handles;
2846 
2847    num_resident_tex_handles = sctx->resident_tex_handles.size / sizeof(struct si_texture_handle *);
2848    num_resident_img_handles = sctx->resident_img_handles.size / sizeof(struct si_image_handle *);
2849 
2850    /* Add all resident texture handles. */
2851    util_dynarray_foreach (&sctx->resident_tex_handles, struct si_texture_handle *, tex_handle) {
2852       struct si_sampler_view *sview = (struct si_sampler_view *)(*tex_handle)->view;
2853 
2854       si_sampler_view_add_buffer(sctx, sview->base.texture, RADEON_USAGE_READ,
2855                                  sview->is_stencil_sampler);
2856    }
2857 
2858    /* Add all resident image handles. */
2859    util_dynarray_foreach (&sctx->resident_img_handles, struct si_image_handle *, img_handle) {
2860       struct pipe_image_view *view = &(*img_handle)->view;
2861 
2862       si_sampler_view_add_buffer(sctx, view->resource, RADEON_USAGE_READWRITE, false);
2863    }
2864 
2865    sctx->num_resident_handles += num_resident_tex_handles + num_resident_img_handles;
2866    assert(sctx->bo_list_add_all_resident_resources);
2867    sctx->bo_list_add_all_resident_resources = false;
2868 }
2869 
2870 static void si_emit_gfx_resources_add_all_to_bo_list(struct si_context *sctx, unsigned index);
2871 
2872 /* INIT/DEINIT/UPLOAD */
2873 
si_init_all_descriptors(struct si_context * sctx)2874 void si_init_all_descriptors(struct si_context *sctx)
2875 {
2876    int i;
2877    unsigned first_shader = sctx->has_graphics ? 0 : PIPE_SHADER_COMPUTE;
2878    unsigned hs_sgpr0, gs_sgpr0;
2879 
2880    if (sctx->gfx_level >= GFX12) {
2881       hs_sgpr0 = R_00B410_SPI_SHADER_PGM_LO_HS;
2882       gs_sgpr0 = R_00B210_SPI_SHADER_PGM_LO_GS;
2883    } else if (sctx->gfx_level >= GFX11) {
2884       hs_sgpr0 = R_00B420_SPI_SHADER_PGM_LO_HS;
2885       gs_sgpr0 = R_00B220_SPI_SHADER_PGM_LO_GS;
2886    } else {
2887       hs_sgpr0 = R_00B408_SPI_SHADER_USER_DATA_ADDR_LO_HS;
2888       gs_sgpr0 = R_00B208_SPI_SHADER_USER_DATA_ADDR_LO_GS;
2889    }
2890 
2891    for (i = first_shader; i < SI_NUM_SHADERS; i++) {
2892       bool is_2nd =
2893          sctx->gfx_level >= GFX9 && (i == PIPE_SHADER_TESS_CTRL || i == PIPE_SHADER_GEOMETRY);
2894       unsigned num_sampler_slots = SI_NUM_IMAGE_SLOTS / 2 + SI_NUM_SAMPLERS;
2895       unsigned num_buffer_slots = SI_NUM_SHADER_BUFFERS + SI_NUM_CONST_BUFFERS;
2896       int rel_dw_offset;
2897       struct si_descriptors *desc;
2898 
2899       if (is_2nd) {
2900          if (i == PIPE_SHADER_TESS_CTRL) {
2901             rel_dw_offset =
2902                (hs_sgpr0 - R_00B430_SPI_SHADER_USER_DATA_HS_0) / 4;
2903          } else if (sctx->gfx_level >= GFX10) { /* PIPE_SHADER_GEOMETRY */
2904             rel_dw_offset =
2905                (gs_sgpr0 - R_00B230_SPI_SHADER_USER_DATA_GS_0) / 4;
2906          } else {
2907             rel_dw_offset =
2908                (gs_sgpr0 - R_00B330_SPI_SHADER_USER_DATA_ES_0) / 4;
2909          }
2910       } else {
2911          rel_dw_offset = SI_SGPR_CONST_AND_SHADER_BUFFERS;
2912       }
2913       desc = si_const_and_shader_buffer_descriptors(sctx, i);
2914       si_init_buffer_resources(sctx, &sctx->const_and_shader_buffers[i], desc, num_buffer_slots,
2915                                rel_dw_offset, RADEON_PRIO_SHADER_RW_BUFFER,
2916                                RADEON_PRIO_CONST_BUFFER);
2917       desc->slot_index_to_bind_directly = si_get_constbuf_slot(0);
2918 
2919       if (is_2nd) {
2920          if (i == PIPE_SHADER_TESS_CTRL) {
2921             rel_dw_offset =
2922                (hs_sgpr0 + 4 - R_00B430_SPI_SHADER_USER_DATA_HS_0) / 4;
2923          } else if (sctx->gfx_level >= GFX10) { /* PIPE_SHADER_GEOMETRY */
2924             rel_dw_offset =
2925                (gs_sgpr0 + 4 - R_00B230_SPI_SHADER_USER_DATA_GS_0) / 4;
2926          } else {
2927             rel_dw_offset =
2928                (gs_sgpr0 + 4 - R_00B330_SPI_SHADER_USER_DATA_ES_0) / 4;
2929          }
2930       } else {
2931          rel_dw_offset = SI_SGPR_SAMPLERS_AND_IMAGES;
2932       }
2933 
2934       desc = si_sampler_and_image_descriptors(sctx, i);
2935       si_init_descriptors(desc, rel_dw_offset, 16, num_sampler_slots);
2936 
2937       int j;
2938       for (j = 0; j < SI_NUM_IMAGE_SLOTS; j++)
2939          memcpy(desc->list + j * 8, null_image_descriptor, 8 * 4);
2940       for (; j < SI_NUM_IMAGE_SLOTS + SI_NUM_SAMPLERS * 2; j++)
2941          memcpy(desc->list + j * 8, null_texture_descriptor, 8 * 4);
2942    }
2943 
2944    si_init_buffer_resources(sctx, &sctx->internal_bindings, &sctx->descriptors[SI_DESCS_INTERNAL],
2945                             SI_NUM_INTERNAL_BINDINGS, SI_SGPR_INTERNAL_BINDINGS,
2946                             /* The second priority is used by
2947                              * const buffers in RW buffer slots. */
2948                             RADEON_PRIO_SHADER_RINGS, RADEON_PRIO_CONST_BUFFER);
2949    sctx->descriptors[SI_DESCS_INTERNAL].num_active_slots = SI_NUM_INTERNAL_BINDINGS;
2950 
2951    /* Initialize an array of 1024 bindless descriptors, when the limit is
2952     * reached, just make it larger and re-upload the whole array.
2953     */
2954    si_init_bindless_descriptors(sctx, &sctx->bindless_descriptors,
2955                                 SI_SGPR_BINDLESS_SAMPLERS_AND_IMAGES, 1024);
2956 
2957    sctx->descriptors_dirty = u_bit_consecutive(0, SI_NUM_DESCS);
2958 
2959    /* Set pipe_context functions. */
2960    sctx->b.bind_sampler_states = si_bind_sampler_states;
2961    sctx->b.set_shader_images = si_set_shader_images;
2962    sctx->b.set_constant_buffer = si_pipe_set_constant_buffer;
2963    sctx->b.set_inlinable_constants = si_set_inlinable_constants;
2964    sctx->b.set_shader_buffers = si_pipe_set_shader_buffers;
2965    sctx->b.set_sampler_views = si_pipe_set_sampler_views;
2966    sctx->b.create_texture_handle = si_create_texture_handle;
2967    sctx->b.delete_texture_handle = si_delete_texture_handle;
2968    sctx->b.make_texture_handle_resident = si_make_texture_handle_resident;
2969    sctx->b.create_image_handle = si_create_image_handle;
2970    sctx->b.delete_image_handle = si_delete_image_handle;
2971    sctx->b.make_image_handle_resident = si_make_image_handle_resident;
2972 
2973    if (!sctx->has_graphics)
2974       return;
2975 
2976    sctx->b.set_polygon_stipple = si_set_polygon_stipple;
2977 
2978    sctx->atoms.s.gfx_add_all_to_bo_list.emit = si_emit_gfx_resources_add_all_to_bo_list;
2979    sctx->atoms.s.gfx_shader_pointers.emit = si_emit_graphics_shader_pointers;
2980 
2981    /* Set default and immutable mappings. */
2982    si_set_user_data_base(sctx, PIPE_SHADER_VERTEX,
2983                          si_get_user_data_base(sctx->gfx_level, TESS_OFF, GS_OFF,
2984                                                sctx->ngg, PIPE_SHADER_VERTEX));
2985    si_set_user_data_base(sctx, PIPE_SHADER_TESS_CTRL,
2986                          si_get_user_data_base(sctx->gfx_level, TESS_OFF, GS_OFF,
2987                                                NGG_OFF, PIPE_SHADER_TESS_CTRL));
2988    si_set_user_data_base(sctx, PIPE_SHADER_GEOMETRY,
2989                          si_get_user_data_base(sctx->gfx_level, TESS_OFF, GS_OFF,
2990                                                NGG_OFF, PIPE_SHADER_GEOMETRY));
2991    si_set_user_data_base(sctx, PIPE_SHADER_FRAGMENT, R_00B030_SPI_SHADER_USER_DATA_PS_0);
2992 }
2993 
si_release_all_descriptors(struct si_context * sctx)2994 void si_release_all_descriptors(struct si_context *sctx)
2995 {
2996    int i;
2997 
2998    for (i = 0; i < SI_NUM_SHADERS; i++) {
2999       si_release_buffer_resources(&sctx->const_and_shader_buffers[i],
3000                                   si_const_and_shader_buffer_descriptors(sctx, i));
3001       si_release_sampler_views(&sctx->samplers[i]);
3002       si_release_image_views(&sctx->images[i]);
3003    }
3004    si_release_buffer_resources(&sctx->internal_bindings, &sctx->descriptors[SI_DESCS_INTERNAL]);
3005    for (i = 0; i < SI_NUM_VERTEX_BUFFERS; i++)
3006       pipe_vertex_buffer_unreference(&sctx->vertex_buffer[i]);
3007 
3008    for (i = 0; i < SI_NUM_DESCS; ++i)
3009       si_release_descriptors(&sctx->descriptors[i]);
3010 
3011    si_release_bindless_descriptors(sctx);
3012 }
3013 
si_gfx_resources_check_encrypted(struct si_context * sctx)3014 bool si_gfx_resources_check_encrypted(struct si_context *sctx)
3015 {
3016    bool use_encrypted_bo = false;
3017 
3018    for (unsigned i = 0; i < SI_NUM_GRAPHICS_SHADERS && !use_encrypted_bo; i++) {
3019       struct si_shader_ctx_state *current_shader = &sctx->shaders[i];
3020       if (!current_shader->cso)
3021          continue;
3022 
3023       use_encrypted_bo |=
3024          si_buffer_resources_check_encrypted(sctx, &sctx->const_and_shader_buffers[i]);
3025       use_encrypted_bo |=
3026          si_sampler_views_check_encrypted(sctx, &sctx->samplers[i],
3027                                           current_shader->cso->info.base.textures_used[0]);
3028       use_encrypted_bo |= si_image_views_check_encrypted(sctx, &sctx->images[i],
3029                                           u_bit_consecutive(0, current_shader->cso->info.base.num_images));
3030    }
3031    use_encrypted_bo |= si_buffer_resources_check_encrypted(sctx, &sctx->internal_bindings);
3032 
3033    struct si_state_blend *blend = sctx->queued.named.blend;
3034    for (int i = 0; i < sctx->framebuffer.state.nr_cbufs && !use_encrypted_bo; i++) {
3035       struct pipe_surface *surf = sctx->framebuffer.state.cbufs[i];
3036       if (surf && surf->texture) {
3037          struct si_texture *tex = (struct si_texture *)surf->texture;
3038          if (!(tex->buffer.flags & RADEON_FLAG_ENCRYPTED))
3039             continue;
3040 
3041          /* Are we reading from this framebuffer */
3042          if (((blend->blend_enable_4bit >> (4 * i)) & 0xf) ||
3043              vi_dcc_enabled(tex, 0)) {
3044             use_encrypted_bo = true;
3045          }
3046       }
3047    }
3048 
3049    if (sctx->framebuffer.state.zsbuf) {
3050       struct si_texture* zs = (struct si_texture *)sctx->framebuffer.state.zsbuf->texture;
3051       if (zs &&
3052           (zs->buffer.flags & RADEON_FLAG_ENCRYPTED)) {
3053          /* TODO: This isn't needed if depth.func is PIPE_FUNC_NEVER or PIPE_FUNC_ALWAYS */
3054          use_encrypted_bo = true;
3055       }
3056    }
3057 
3058 #ifndef NDEBUG
3059    if (use_encrypted_bo) {
3060       /* Verify that color buffers are encrypted */
3061       for (int i = 0; i < sctx->framebuffer.state.nr_cbufs; i++) {
3062          struct pipe_surface *surf = sctx->framebuffer.state.cbufs[i];
3063          if (!surf)
3064             continue;
3065          struct si_texture *tex = (struct si_texture *)surf->texture;
3066          assert(!surf->texture || (tex->buffer.flags & RADEON_FLAG_ENCRYPTED));
3067       }
3068       /* Verify that depth/stencil buffer is encrypted */
3069       if (sctx->framebuffer.state.zsbuf) {
3070          struct pipe_surface *surf = sctx->framebuffer.state.zsbuf;
3071          struct si_texture *tex = (struct si_texture *)surf->texture;
3072          assert(!surf->texture || (tex->buffer.flags & RADEON_FLAG_ENCRYPTED));
3073       }
3074    }
3075 #endif
3076 
3077    return use_encrypted_bo;
3078 }
3079 
si_emit_gfx_resources_add_all_to_bo_list(struct si_context * sctx,unsigned index)3080 static void si_emit_gfx_resources_add_all_to_bo_list(struct si_context *sctx, unsigned index)
3081 {
3082    for (unsigned i = 0; i < SI_NUM_GRAPHICS_SHADERS; i++) {
3083       si_buffer_resources_begin_new_cs(sctx, &sctx->const_and_shader_buffers[i]);
3084       si_sampler_views_begin_new_cs(sctx, &sctx->samplers[i]);
3085       si_image_views_begin_new_cs(sctx, &sctx->images[i]);
3086    }
3087    si_buffer_resources_begin_new_cs(sctx, &sctx->internal_bindings);
3088 
3089    unsigned num_vb = sctx->num_vertex_buffers;
3090    for (unsigned i = 0; i < num_vb; i++) {
3091       struct si_resource *buf = si_resource(sctx->vertex_buffer[i].buffer.resource);
3092       if (buf) {
3093          radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, buf,
3094                                    RADEON_USAGE_READ | RADEON_PRIO_VERTEX_BUFFER);
3095       }
3096    }
3097 
3098    if (sctx->bo_list_add_all_resident_resources)
3099       si_resident_buffers_add_all_to_bo_list(sctx);
3100 }
3101 
si_compute_resources_check_encrypted(struct si_context * sctx)3102 bool si_compute_resources_check_encrypted(struct si_context *sctx)
3103 {
3104    unsigned sh = PIPE_SHADER_COMPUTE;
3105 
3106    struct si_shader_info* info = &sctx->cs_shader_state.program->sel.info;
3107 
3108    /* TODO: we should assert that either use_encrypted_bo is false,
3109     * or all writable buffers are encrypted.
3110     */
3111    return si_buffer_resources_check_encrypted(sctx, &sctx->const_and_shader_buffers[sh]) ||
3112           si_sampler_views_check_encrypted(sctx, &sctx->samplers[sh], info->base.textures_used[0]) ||
3113           si_image_views_check_encrypted(sctx, &sctx->images[sh], u_bit_consecutive(0, info->base.num_images)) ||
3114           si_buffer_resources_check_encrypted(sctx, &sctx->internal_bindings);
3115 }
3116 
si_compute_resources_add_all_to_bo_list(struct si_context * sctx)3117 void si_compute_resources_add_all_to_bo_list(struct si_context *sctx)
3118 {
3119    unsigned sh = PIPE_SHADER_COMPUTE;
3120 
3121    si_buffer_resources_begin_new_cs(sctx, &sctx->const_and_shader_buffers[sh]);
3122    si_sampler_views_begin_new_cs(sctx, &sctx->samplers[sh]);
3123    si_image_views_begin_new_cs(sctx, &sctx->images[sh]);
3124    si_buffer_resources_begin_new_cs(sctx, &sctx->internal_bindings);
3125 
3126    if (sctx->bo_list_add_all_resident_resources)
3127       si_resident_buffers_add_all_to_bo_list(sctx);
3128 
3129    assert(sctx->bo_list_add_all_compute_resources);
3130    sctx->bo_list_add_all_compute_resources = false;
3131 }
3132 
si_add_all_descriptors_to_bo_list(struct si_context * sctx)3133 void si_add_all_descriptors_to_bo_list(struct si_context *sctx)
3134 {
3135    for (unsigned i = 0; i < SI_NUM_DESCS; ++i)
3136       si_add_descriptors_to_bo_list(sctx, &sctx->descriptors[i]);
3137    si_add_descriptors_to_bo_list(sctx, &sctx->bindless_descriptors);
3138 
3139    sctx->bo_list_add_all_resident_resources = true;
3140    si_mark_atom_dirty(sctx, &sctx->atoms.s.gfx_add_all_to_bo_list);
3141    sctx->bo_list_add_all_compute_resources = true;
3142 }
3143 
si_set_active_descriptors(struct si_context * sctx,unsigned desc_idx,uint64_t new_active_mask)3144 void si_set_active_descriptors(struct si_context *sctx, unsigned desc_idx, uint64_t new_active_mask)
3145 {
3146    struct si_descriptors *desc = &sctx->descriptors[desc_idx];
3147 
3148    /* Ignore no-op updates and updates that disable all slots. */
3149    if (!new_active_mask ||
3150        new_active_mask == u_bit_consecutive64(desc->first_active_slot, desc->num_active_slots))
3151       return;
3152 
3153    int first, count;
3154    u_bit_scan_consecutive_range64(&new_active_mask, &first, &count);
3155    assert(new_active_mask == 0);
3156 
3157    /* Upload/dump descriptors if slots are being enabled. */
3158    if (first < desc->first_active_slot ||
3159        first + count > desc->first_active_slot + desc->num_active_slots) {
3160       sctx->descriptors_dirty |= 1u << desc_idx;
3161       if (desc_idx < SI_DESCS_FIRST_COMPUTE)
3162          si_mark_atom_dirty(sctx, &sctx->atoms.s.gfx_shader_pointers);
3163    }
3164 
3165    desc->first_active_slot = first;
3166    desc->num_active_slots = count;
3167 }
3168 
si_set_active_descriptors_for_shader(struct si_context * sctx,struct si_shader_selector * sel)3169 void si_set_active_descriptors_for_shader(struct si_context *sctx, struct si_shader_selector *sel)
3170 {
3171    if (!sel)
3172       return;
3173 
3174    si_set_active_descriptors(sctx, sel->const_and_shader_buf_descriptors_index,
3175                              sel->active_const_and_shader_buffers);
3176    si_set_active_descriptors(sctx, sel->sampler_and_images_descriptors_index,
3177                              sel->active_samplers_and_images);
3178 }
3179