2 * Copyright 2013 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 /* Resource binding slots and sampler states (each described with 8 or
26 * 4 dwords) are stored in lists in memory which is accessed by shaders
27 * using scalar load instructions.
29 * This file is responsible for managing such lists. It keeps a copy of all
30 * descriptors in CPU memory and re-uploads a whole list if some slots have
33 * This code is also reponsible for updating shader pointers to those lists.
35 * Note that CP DMA can't be used for updating the lists, because a GPU hang
36 * could leave the list in a mid-IB state and the next IB would get wrong
37 * descriptors and the whole context would be unusable at that point.
38 * (Note: The register shadowing can't be used due to the same reason)
40 * Also, uploading descriptors to newly allocated memory doesn't require
44 * Possible scenarios for one 16 dword image+sampler slot:
46 * | Image | w/ FMASK | Buffer | NULL
47 * [ 0: 3] Image[0:3] | Image[0:3] | Null[0:3] | Null[0:3]
48 * [ 4: 7] Image[4:7] | Image[4:7] | Buffer[0:3] | 0
49 * [ 8:11] Null[0:3] | Fmask[0:3] | Null[0:3] | Null[0:3]
50 * [12:15] Sampler[0:3] | Fmask[4:7] | Sampler[0:3] | Sampler[0:3]
52 * FMASK implies MSAA, therefore no sampler state.
53 * Sampler states are never unbound except when FMASK is bound.
59 #include "util/hash_table.h"
60 #include "util/u_idalloc.h"
61 #include "util/format/u_format.h"
62 #include "util/u_memory.h"
63 #include "util/u_upload_mgr.h"
66 /* NULL image and buffer descriptor for textures (alpha = 1) and images
69 * For images, all fields must be zero except for the swizzle, which
70 * supports arbitrary combinations of 0s and 1s. The texture type must be
71 * any valid type (e.g. 1D). If the texture type isn't set, the hw hangs.
73 * For buffers, all fields must be zero. If they are not, the hw hangs.
75 * This is the only reason why the buffer descriptor must be in words [4:7].
77 static uint32_t null_texture_descriptor
[8] = {
81 S_008F1C_DST_SEL_W(V_008F1C_SQ_SEL_1
) |
82 S_008F1C_TYPE(V_008F1C_SQ_RSRC_IMG_1D
)
83 /* the rest must contain zeros, which is also used by the buffer
87 static uint32_t null_image_descriptor
[8] = {
91 S_008F1C_TYPE(V_008F1C_SQ_RSRC_IMG_1D
)
92 /* the rest must contain zeros, which is also used by the buffer
96 static uint64_t si_desc_extract_buffer_address(const uint32_t *desc
)
98 uint64_t va
= desc
[0] |
99 ((uint64_t)G_008F04_BASE_ADDRESS_HI(desc
[1]) << 32);
101 /* Sign-extend the 48-bit address. */
103 va
= (int64_t)va
>> 16;
107 static void si_init_descriptor_list(uint32_t *desc_list
,
108 unsigned element_dw_size
,
109 unsigned num_elements
,
110 const uint32_t *null_descriptor
)
114 /* Initialize the array to NULL descriptors if the element size is 8. */
115 if (null_descriptor
) {
116 assert(element_dw_size
% 8 == 0);
117 for (i
= 0; i
< num_elements
* element_dw_size
/ 8; i
++)
118 memcpy(desc_list
+ i
* 8, null_descriptor
, 8 * 4);
122 static void si_init_descriptors(struct si_descriptors
*desc
,
123 short shader_userdata_rel_index
,
124 unsigned element_dw_size
,
125 unsigned num_elements
)
127 desc
->list
= CALLOC(num_elements
, element_dw_size
* 4);
128 desc
->element_dw_size
= element_dw_size
;
129 desc
->num_elements
= num_elements
;
130 desc
->shader_userdata_offset
= shader_userdata_rel_index
* 4;
131 desc
->slot_index_to_bind_directly
= -1;
134 static void si_release_descriptors(struct si_descriptors
*desc
)
136 si_resource_reference(&desc
->buffer
, NULL
);
140 static bool si_upload_descriptors(struct si_context
*sctx
,
141 struct si_descriptors
*desc
)
143 unsigned slot_size
= desc
->element_dw_size
* 4;
144 unsigned first_slot_offset
= desc
->first_active_slot
* slot_size
;
145 unsigned upload_size
= desc
->num_active_slots
* slot_size
;
147 /* Skip the upload if no shader is using the descriptors. dirty_mask
148 * will stay dirty and the descriptors will be uploaded when there is
149 * a shader using them.
154 /* If there is just one active descriptor, bind it directly. */
155 if ((int)desc
->first_active_slot
== desc
->slot_index_to_bind_directly
&&
156 desc
->num_active_slots
== 1) {
157 uint32_t *descriptor
= &desc
->list
[desc
->slot_index_to_bind_directly
*
158 desc
->element_dw_size
];
160 /* The buffer is already in the buffer list. */
161 si_resource_reference(&desc
->buffer
, NULL
);
162 desc
->gpu_list
= NULL
;
163 desc
->gpu_address
= si_desc_extract_buffer_address(descriptor
);
164 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.shader_pointers
);
169 unsigned buffer_offset
;
170 u_upload_alloc(sctx
->b
.const_uploader
, first_slot_offset
, upload_size
,
171 si_optimal_tcc_alignment(sctx
, upload_size
),
172 &buffer_offset
, (struct pipe_resource
**)&desc
->buffer
,
175 desc
->gpu_address
= 0;
176 return false; /* skip the draw call */
179 util_memcpy_cpu_to_le32(ptr
, (char*)desc
->list
+ first_slot_offset
,
181 desc
->gpu_list
= ptr
- first_slot_offset
/ 4;
183 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
, desc
->buffer
,
184 RADEON_USAGE_READ
, RADEON_PRIO_DESCRIPTORS
);
186 /* The shader pointer should point to slot 0. */
187 buffer_offset
-= first_slot_offset
;
188 desc
->gpu_address
= desc
->buffer
->gpu_address
+ buffer_offset
;
190 assert(desc
->buffer
->flags
& RADEON_FLAG_32BIT
);
191 assert((desc
->buffer
->gpu_address
>> 32) == sctx
->screen
->info
.address32_hi
);
192 assert((desc
->gpu_address
>> 32) == sctx
->screen
->info
.address32_hi
);
194 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.shader_pointers
);
199 si_descriptors_begin_new_cs(struct si_context
*sctx
, struct si_descriptors
*desc
)
204 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
, desc
->buffer
,
205 RADEON_USAGE_READ
, RADEON_PRIO_DESCRIPTORS
);
210 static inline enum radeon_bo_priority
211 si_get_sampler_view_priority(struct si_resource
*res
)
213 if (res
->b
.b
.target
== PIPE_BUFFER
)
214 return RADEON_PRIO_SAMPLER_BUFFER
;
216 if (res
->b
.b
.nr_samples
> 1)
217 return RADEON_PRIO_SAMPLER_TEXTURE_MSAA
;
219 return RADEON_PRIO_SAMPLER_TEXTURE
;
222 static struct si_descriptors
*
223 si_sampler_and_image_descriptors(struct si_context
*sctx
, unsigned shader
)
225 return &sctx
->descriptors
[si_sampler_and_image_descriptors_idx(shader
)];
228 static void si_release_sampler_views(struct si_samplers
*samplers
)
232 for (i
= 0; i
< ARRAY_SIZE(samplers
->views
); i
++) {
233 pipe_sampler_view_reference(&samplers
->views
[i
], NULL
);
237 static void si_sampler_view_add_buffer(struct si_context
*sctx
,
238 struct pipe_resource
*resource
,
239 enum radeon_bo_usage usage
,
240 bool is_stencil_sampler
,
243 struct si_texture
*tex
= (struct si_texture
*)resource
;
244 enum radeon_bo_priority priority
;
249 /* Use the flushed depth texture if direct sampling is unsupported. */
250 if (resource
->target
!= PIPE_BUFFER
&&
251 tex
->is_depth
&& !si_can_sample_zs(tex
, is_stencil_sampler
))
252 tex
= tex
->flushed_depth_texture
;
254 priority
= si_get_sampler_view_priority(&tex
->buffer
);
255 radeon_add_to_gfx_buffer_list_check_mem(sctx
, &tex
->buffer
, usage
, priority
,
258 if (resource
->target
== PIPE_BUFFER
)
261 /* Add separate DCC. */
262 if (tex
->dcc_separate_buffer
) {
263 radeon_add_to_gfx_buffer_list_check_mem(sctx
, tex
->dcc_separate_buffer
,
264 usage
, RADEON_PRIO_SEPARATE_META
, check_mem
);
268 static void si_sampler_views_begin_new_cs(struct si_context
*sctx
,
269 struct si_samplers
*samplers
)
271 unsigned mask
= samplers
->enabled_mask
;
273 /* Add buffers to the CS. */
275 int i
= u_bit_scan(&mask
);
276 struct si_sampler_view
*sview
= (struct si_sampler_view
*)samplers
->views
[i
];
278 si_sampler_view_add_buffer(sctx
, sview
->base
.texture
,
280 sview
->is_stencil_sampler
, false);
284 /* Set buffer descriptor fields that can be changed by reallocations. */
285 static void si_set_buf_desc_address(struct si_resource
*buf
,
286 uint64_t offset
, uint32_t *state
)
288 uint64_t va
= buf
->gpu_address
+ offset
;
291 state
[1] &= C_008F04_BASE_ADDRESS_HI
;
292 state
[1] |= S_008F04_BASE_ADDRESS_HI(va
>> 32);
295 /* Set texture descriptor fields that can be changed by reallocations.
298 * \param base_level_info information of the level of BASE_ADDRESS
299 * \param base_level the level of BASE_ADDRESS
300 * \param first_level pipe_sampler_view.u.tex.first_level
301 * \param block_width util_format_get_blockwidth()
302 * \param is_stencil select between separate Z & Stencil
303 * \param state descriptor to update
305 void si_set_mutable_tex_desc_fields(struct si_screen
*sscreen
,
306 struct si_texture
*tex
,
307 const struct legacy_surf_level
*base_level_info
,
308 unsigned base_level
, unsigned first_level
,
309 unsigned block_width
, bool is_stencil
,
312 uint64_t va
, meta_va
= 0;
314 if (tex
->is_depth
&& !si_can_sample_zs(tex
, is_stencil
)) {
315 tex
= tex
->flushed_depth_texture
;
319 va
= tex
->buffer
.gpu_address
;
321 if (sscreen
->info
.chip_class
>= GFX9
) {
322 /* Only stencil_offset needs to be added here. */
324 va
+= tex
->surface
.u
.gfx9
.stencil_offset
;
326 va
+= tex
->surface
.u
.gfx9
.surf_offset
;
328 va
+= base_level_info
->offset
;
332 state
[1] &= C_008F14_BASE_ADDRESS_HI
;
333 state
[1] |= S_008F14_BASE_ADDRESS_HI(va
>> 40);
335 /* Only macrotiled modes can set tile swizzle.
336 * GFX9 doesn't use (legacy) base_level_info.
338 if (sscreen
->info
.chip_class
>= GFX9
||
339 base_level_info
->mode
== RADEON_SURF_MODE_2D
)
340 state
[0] |= tex
->surface
.tile_swizzle
;
342 if (sscreen
->info
.chip_class
>= GFX8
) {
343 state
[6] &= C_008F28_COMPRESSION_EN
;
345 if (vi_dcc_enabled(tex
, first_level
)) {
346 meta_va
= (!tex
->dcc_separate_buffer
? tex
->buffer
.gpu_address
: 0) +
347 tex
->surface
.dcc_offset
;
349 if (sscreen
->info
.chip_class
== GFX8
) {
350 meta_va
+= base_level_info
->dcc_offset
;
351 assert(base_level_info
->mode
== RADEON_SURF_MODE_2D
);
354 unsigned dcc_tile_swizzle
= tex
->surface
.tile_swizzle
<< 8;
355 dcc_tile_swizzle
&= tex
->surface
.dcc_alignment
- 1;
356 meta_va
|= dcc_tile_swizzle
;
357 } else if (vi_tc_compat_htile_enabled(tex
, first_level
,
358 is_stencil
? PIPE_MASK_S
: PIPE_MASK_Z
)) {
359 meta_va
= tex
->buffer
.gpu_address
+ tex
->surface
.htile_offset
;
363 state
[6] |= S_008F28_COMPRESSION_EN(1);
366 if (sscreen
->info
.chip_class
>= GFX8
&& sscreen
->info
.chip_class
<= GFX9
)
367 state
[7] = meta_va
>> 8;
369 if (sscreen
->info
.chip_class
>= GFX10
) {
370 state
[3] &= C_00A00C_SW_MODE
;
373 state
[3] |= S_00A00C_SW_MODE(tex
->surface
.u
.gfx9
.stencil
.swizzle_mode
);
375 state
[3] |= S_00A00C_SW_MODE(tex
->surface
.u
.gfx9
.surf
.swizzle_mode
);
378 state
[6] &= C_00A018_META_DATA_ADDRESS_LO
&
379 C_00A018_META_PIPE_ALIGNED
;
382 struct gfx9_surf_meta_flags meta
;
384 if (tex
->surface
.dcc_offset
)
385 meta
= tex
->surface
.u
.gfx9
.dcc
;
387 meta
= tex
->surface
.u
.gfx9
.htile
;
389 state
[6] |= S_00A018_META_PIPE_ALIGNED(meta
.pipe_aligned
) |
390 S_00A018_META_DATA_ADDRESS_LO(meta_va
>> 8);
393 state
[7] = meta_va
>> 16;
394 } else if (sscreen
->info
.chip_class
== GFX9
) {
395 state
[3] &= C_008F1C_SW_MODE
;
396 state
[4] &= C_008F20_PITCH
;
399 state
[3] |= S_008F1C_SW_MODE(tex
->surface
.u
.gfx9
.stencil
.swizzle_mode
);
400 state
[4] |= S_008F20_PITCH(tex
->surface
.u
.gfx9
.stencil
.epitch
);
402 state
[3] |= S_008F1C_SW_MODE(tex
->surface
.u
.gfx9
.surf
.swizzle_mode
);
403 state
[4] |= S_008F20_PITCH(tex
->surface
.u
.gfx9
.surf
.epitch
);
406 state
[5] &= C_008F24_META_DATA_ADDRESS
&
407 C_008F24_META_PIPE_ALIGNED
&
408 C_008F24_META_RB_ALIGNED
;
410 struct gfx9_surf_meta_flags meta
;
412 if (tex
->surface
.dcc_offset
)
413 meta
= tex
->surface
.u
.gfx9
.dcc
;
415 meta
= tex
->surface
.u
.gfx9
.htile
;
417 state
[5] |= S_008F24_META_DATA_ADDRESS(meta_va
>> 40) |
418 S_008F24_META_PIPE_ALIGNED(meta
.pipe_aligned
) |
419 S_008F24_META_RB_ALIGNED(meta
.rb_aligned
);
423 unsigned pitch
= base_level_info
->nblk_x
* block_width
;
424 unsigned index
= si_tile_mode_index(tex
, base_level
, is_stencil
);
426 state
[3] &= C_008F1C_TILING_INDEX
;
427 state
[3] |= S_008F1C_TILING_INDEX(index
);
428 state
[4] &= C_008F20_PITCH
;
429 state
[4] |= S_008F20_PITCH(pitch
- 1);
433 static void si_set_sampler_state_desc(struct si_sampler_state
*sstate
,
434 struct si_sampler_view
*sview
,
435 struct si_texture
*tex
,
438 if (sview
&& sview
->is_integer
)
439 memcpy(desc
, sstate
->integer_val
, 4*4);
440 else if (tex
&& tex
->upgraded_depth
&&
441 (!sview
|| !sview
->is_stencil_sampler
))
442 memcpy(desc
, sstate
->upgraded_depth_val
, 4*4);
444 memcpy(desc
, sstate
->val
, 4*4);
447 static void si_set_sampler_view_desc(struct si_context
*sctx
,
448 struct si_sampler_view
*sview
,
449 struct si_sampler_state
*sstate
,
452 struct pipe_sampler_view
*view
= &sview
->base
;
453 struct si_texture
*tex
= (struct si_texture
*)view
->texture
;
454 bool is_buffer
= tex
->buffer
.b
.b
.target
== PIPE_BUFFER
;
456 if (unlikely(!is_buffer
&& sview
->dcc_incompatible
)) {
457 if (vi_dcc_enabled(tex
, view
->u
.tex
.first_level
))
458 if (!si_texture_disable_dcc(sctx
, tex
))
459 si_decompress_dcc(sctx
, tex
);
461 sview
->dcc_incompatible
= false;
464 assert(tex
); /* views with texture == NULL aren't supported */
465 memcpy(desc
, sview
->state
, 8*4);
468 si_set_buf_desc_address(&tex
->buffer
,
469 sview
->base
.u
.buf
.offset
,
472 bool is_separate_stencil
= tex
->db_compatible
&&
473 sview
->is_stencil_sampler
;
475 si_set_mutable_tex_desc_fields(sctx
->screen
, tex
,
476 sview
->base_level_info
,
478 sview
->base
.u
.tex
.first_level
,
484 if (!is_buffer
&& tex
->surface
.fmask_size
) {
485 memcpy(desc
+ 8, sview
->fmask_state
, 8*4);
487 /* Disable FMASK and bind sampler state in [12:15]. */
488 memcpy(desc
+ 8, null_texture_descriptor
, 4*4);
491 si_set_sampler_state_desc(sstate
, sview
,
492 is_buffer
? NULL
: tex
,
497 static bool color_needs_decompression(struct si_texture
*tex
)
499 return tex
->surface
.fmask_size
||
500 (tex
->dirty_level_mask
&&
501 (tex
->cmask_buffer
|| tex
->surface
.dcc_offset
));
504 static bool depth_needs_decompression(struct si_texture
*tex
)
506 /* If the depth/stencil texture is TC-compatible, no decompression
507 * will be done. The decompression function will only flush DB caches
508 * to make it coherent with shaders. That's necessary because the driver
509 * doesn't flush DB caches in any other case.
511 return tex
->db_compatible
;
514 static void si_set_sampler_view(struct si_context
*sctx
,
516 unsigned slot
, struct pipe_sampler_view
*view
,
517 bool disallow_early_out
)
519 struct si_samplers
*samplers
= &sctx
->samplers
[shader
];
520 struct si_sampler_view
*sview
= (struct si_sampler_view
*)view
;
521 struct si_descriptors
*descs
= si_sampler_and_image_descriptors(sctx
, shader
);
522 unsigned desc_slot
= si_get_sampler_slot(slot
);
523 uint32_t *desc
= descs
->list
+ desc_slot
* 16;
525 if (samplers
->views
[slot
] == view
&& !disallow_early_out
)
529 struct si_texture
*tex
= (struct si_texture
*)view
->texture
;
531 si_set_sampler_view_desc(sctx
, sview
,
532 samplers
->sampler_states
[slot
], desc
);
534 if (tex
->buffer
.b
.b
.target
== PIPE_BUFFER
) {
535 tex
->buffer
.bind_history
|= PIPE_BIND_SAMPLER_VIEW
;
536 samplers
->needs_depth_decompress_mask
&= ~(1u << slot
);
537 samplers
->needs_color_decompress_mask
&= ~(1u << slot
);
539 if (depth_needs_decompression(tex
)) {
540 samplers
->needs_depth_decompress_mask
|= 1u << slot
;
542 samplers
->needs_depth_decompress_mask
&= ~(1u << slot
);
544 if (color_needs_decompression(tex
)) {
545 samplers
->needs_color_decompress_mask
|= 1u << slot
;
547 samplers
->needs_color_decompress_mask
&= ~(1u << slot
);
550 if (tex
->surface
.dcc_offset
&&
551 p_atomic_read(&tex
->framebuffers_bound
))
552 sctx
->need_check_render_feedback
= true;
555 pipe_sampler_view_reference(&samplers
->views
[slot
], view
);
556 samplers
->enabled_mask
|= 1u << slot
;
558 /* Since this can flush, it must be done after enabled_mask is
560 si_sampler_view_add_buffer(sctx
, view
->texture
,
562 sview
->is_stencil_sampler
, true);
564 pipe_sampler_view_reference(&samplers
->views
[slot
], NULL
);
565 memcpy(desc
, null_texture_descriptor
, 8*4);
566 /* Only clear the lower dwords of FMASK. */
567 memcpy(desc
+ 8, null_texture_descriptor
, 4*4);
568 /* Re-set the sampler state if we are transitioning from FMASK. */
569 if (samplers
->sampler_states
[slot
])
570 si_set_sampler_state_desc(samplers
->sampler_states
[slot
], NULL
, NULL
,
573 samplers
->enabled_mask
&= ~(1u << slot
);
574 samplers
->needs_depth_decompress_mask
&= ~(1u << slot
);
575 samplers
->needs_color_decompress_mask
&= ~(1u << slot
);
578 sctx
->descriptors_dirty
|= 1u << si_sampler_and_image_descriptors_idx(shader
);
581 static void si_update_shader_needs_decompress_mask(struct si_context
*sctx
,
584 struct si_samplers
*samplers
= &sctx
->samplers
[shader
];
585 unsigned shader_bit
= 1 << shader
;
587 if (samplers
->needs_depth_decompress_mask
||
588 samplers
->needs_color_decompress_mask
||
589 sctx
->images
[shader
].needs_color_decompress_mask
)
590 sctx
->shader_needs_decompress_mask
|= shader_bit
;
592 sctx
->shader_needs_decompress_mask
&= ~shader_bit
;
595 static void si_set_sampler_views(struct pipe_context
*ctx
,
596 enum pipe_shader_type shader
, unsigned start
,
598 struct pipe_sampler_view
**views
)
600 struct si_context
*sctx
= (struct si_context
*)ctx
;
603 if (!count
|| shader
>= SI_NUM_SHADERS
)
607 for (i
= 0; i
< count
; i
++)
608 si_set_sampler_view(sctx
, shader
, start
+ i
, views
[i
], false);
610 for (i
= 0; i
< count
; i
++)
611 si_set_sampler_view(sctx
, shader
, start
+ i
, NULL
, false);
614 si_update_shader_needs_decompress_mask(sctx
, shader
);
618 si_samplers_update_needs_color_decompress_mask(struct si_samplers
*samplers
)
620 unsigned mask
= samplers
->enabled_mask
;
623 int i
= u_bit_scan(&mask
);
624 struct pipe_resource
*res
= samplers
->views
[i
]->texture
;
626 if (res
&& res
->target
!= PIPE_BUFFER
) {
627 struct si_texture
*tex
= (struct si_texture
*)res
;
629 if (color_needs_decompression(tex
)) {
630 samplers
->needs_color_decompress_mask
|= 1u << i
;
632 samplers
->needs_color_decompress_mask
&= ~(1u << i
);
641 si_release_image_views(struct si_images
*images
)
645 for (i
= 0; i
< SI_NUM_IMAGES
; ++i
) {
646 struct pipe_image_view
*view
= &images
->views
[i
];
648 pipe_resource_reference(&view
->resource
, NULL
);
653 si_image_views_begin_new_cs(struct si_context
*sctx
, struct si_images
*images
)
655 uint mask
= images
->enabled_mask
;
657 /* Add buffers to the CS. */
659 int i
= u_bit_scan(&mask
);
660 struct pipe_image_view
*view
= &images
->views
[i
];
662 assert(view
->resource
);
664 si_sampler_view_add_buffer(sctx
, view
->resource
,
665 RADEON_USAGE_READWRITE
, false, false);
670 si_disable_shader_image(struct si_context
*ctx
, unsigned shader
, unsigned slot
)
672 struct si_images
*images
= &ctx
->images
[shader
];
674 if (images
->enabled_mask
& (1u << slot
)) {
675 struct si_descriptors
*descs
= si_sampler_and_image_descriptors(ctx
, shader
);
676 unsigned desc_slot
= si_get_image_slot(slot
);
678 pipe_resource_reference(&images
->views
[slot
].resource
, NULL
);
679 images
->needs_color_decompress_mask
&= ~(1 << slot
);
681 memcpy(descs
->list
+ desc_slot
*8, null_image_descriptor
, 8*4);
682 images
->enabled_mask
&= ~(1u << slot
);
683 ctx
->descriptors_dirty
|= 1u << si_sampler_and_image_descriptors_idx(shader
);
688 si_mark_image_range_valid(const struct pipe_image_view
*view
)
690 struct si_resource
*res
= si_resource(view
->resource
);
692 if (res
->b
.b
.target
!= PIPE_BUFFER
)
695 util_range_add(&res
->b
.b
, &res
->valid_buffer_range
,
697 view
->u
.buf
.offset
+ view
->u
.buf
.size
);
700 static void si_set_shader_image_desc(struct si_context
*ctx
,
701 const struct pipe_image_view
*view
,
702 bool skip_decompress
,
703 uint32_t *desc
, uint32_t *fmask_desc
)
705 struct si_screen
*screen
= ctx
->screen
;
706 struct si_resource
*res
;
708 res
= si_resource(view
->resource
);
710 if (res
->b
.b
.target
== PIPE_BUFFER
||
711 view
->shader_access
& SI_IMAGE_ACCESS_AS_BUFFER
) {
712 if (view
->access
& PIPE_IMAGE_ACCESS_WRITE
)
713 si_mark_image_range_valid(view
);
715 si_make_buffer_descriptor(screen
, res
,
718 view
->u
.buf
.size
, desc
);
719 si_set_buf_desc_address(res
, view
->u
.buf
.offset
, desc
+ 4);
721 static const unsigned char swizzle
[4] = { 0, 1, 2, 3 };
722 struct si_texture
*tex
= (struct si_texture
*)res
;
723 unsigned level
= view
->u
.tex
.level
;
724 unsigned width
, height
, depth
, hw_level
;
725 bool uses_dcc
= vi_dcc_enabled(tex
, level
);
726 unsigned access
= view
->access
;
728 assert(!tex
->is_depth
);
729 assert(fmask_desc
|| tex
->surface
.fmask_offset
== 0);
731 if (uses_dcc
&& !skip_decompress
&&
732 (access
& PIPE_IMAGE_ACCESS_WRITE
||
733 !vi_dcc_formats_compatible(screen
, res
->b
.b
.format
, view
->format
))) {
734 /* If DCC can't be disabled, at least decompress it.
735 * The decompression is relatively cheap if the surface
736 * has been decompressed already.
738 if (!si_texture_disable_dcc(ctx
, tex
))
739 si_decompress_dcc(ctx
, tex
);
742 if (ctx
->chip_class
>= GFX9
) {
743 /* Always set the base address. The swizzle modes don't
744 * allow setting mipmap level offsets as the base.
746 width
= res
->b
.b
.width0
;
747 height
= res
->b
.b
.height0
;
748 depth
= res
->b
.b
.depth0
;
751 /* Always force the base level to the selected level.
753 * This is required for 3D textures, where otherwise
754 * selecting a single slice for non-layered bindings
755 * fails. It doesn't hurt the other targets.
757 width
= u_minify(res
->b
.b
.width0
, level
);
758 height
= u_minify(res
->b
.b
.height0
, level
);
759 depth
= u_minify(res
->b
.b
.depth0
, level
);
763 screen
->make_texture_descriptor(screen
, tex
,
764 false, res
->b
.b
.target
,
765 view
->format
, swizzle
,
767 view
->u
.tex
.first_layer
,
768 view
->u
.tex
.last_layer
,
769 width
, height
, depth
,
771 si_set_mutable_tex_desc_fields(screen
, tex
,
772 &tex
->surface
.u
.legacy
.level
[level
],
774 util_format_get_blockwidth(view
->format
),
779 static void si_set_shader_image(struct si_context
*ctx
,
781 unsigned slot
, const struct pipe_image_view
*view
,
782 bool skip_decompress
)
784 struct si_images
*images
= &ctx
->images
[shader
];
785 struct si_descriptors
*descs
= si_sampler_and_image_descriptors(ctx
, shader
);
786 struct si_resource
*res
;
788 if (!view
|| !view
->resource
) {
789 si_disable_shader_image(ctx
, shader
, slot
);
793 res
= si_resource(view
->resource
);
795 if (&images
->views
[slot
] != view
)
796 util_copy_image_view(&images
->views
[slot
], view
);
798 si_set_shader_image_desc(ctx
, view
, skip_decompress
,
799 descs
->list
+ si_get_image_slot(slot
) * 8,
800 descs
->list
+ si_get_image_slot(slot
+ SI_NUM_IMAGES
) * 8);
802 if (res
->b
.b
.target
== PIPE_BUFFER
||
803 view
->shader_access
& SI_IMAGE_ACCESS_AS_BUFFER
) {
804 images
->needs_color_decompress_mask
&= ~(1 << slot
);
805 res
->bind_history
|= PIPE_BIND_SHADER_IMAGE
;
807 struct si_texture
*tex
= (struct si_texture
*)res
;
808 unsigned level
= view
->u
.tex
.level
;
810 if (color_needs_decompression(tex
)) {
811 images
->needs_color_decompress_mask
|= 1 << slot
;
813 images
->needs_color_decompress_mask
&= ~(1 << slot
);
816 if (vi_dcc_enabled(tex
, level
) &&
817 p_atomic_read(&tex
->framebuffers_bound
))
818 ctx
->need_check_render_feedback
= true;
821 images
->enabled_mask
|= 1u << slot
;
822 ctx
->descriptors_dirty
|= 1u << si_sampler_and_image_descriptors_idx(shader
);
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
,
832 si_set_shader_images(struct pipe_context
*pipe
,
833 enum pipe_shader_type shader
,
834 unsigned start_slot
, unsigned count
,
835 const struct pipe_image_view
*views
)
837 struct si_context
*ctx
= (struct si_context
*)pipe
;
840 assert(shader
< SI_NUM_SHADERS
);
845 assert(start_slot
+ count
<= SI_NUM_IMAGES
);
848 for (i
= 0, slot
= start_slot
; i
< count
; ++i
, ++slot
)
849 si_set_shader_image(ctx
, shader
, slot
, &views
[i
], false);
851 for (i
= 0, slot
= start_slot
; i
< count
; ++i
, ++slot
)
852 si_set_shader_image(ctx
, shader
, slot
, NULL
, false);
855 si_update_shader_needs_decompress_mask(ctx
, shader
);
859 si_images_update_needs_color_decompress_mask(struct si_images
*images
)
861 unsigned mask
= images
->enabled_mask
;
864 int i
= u_bit_scan(&mask
);
865 struct pipe_resource
*res
= images
->views
[i
].resource
;
867 if (res
&& res
->target
!= PIPE_BUFFER
) {
868 struct si_texture
*tex
= (struct si_texture
*)res
;
870 if (color_needs_decompression(tex
)) {
871 images
->needs_color_decompress_mask
|= 1 << i
;
873 images
->needs_color_decompress_mask
&= ~(1 << i
);
879 void si_update_ps_colorbuf0_slot(struct si_context
*sctx
)
881 struct si_buffer_resources
*buffers
= &sctx
->rw_buffers
;
882 struct si_descriptors
*descs
= &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
883 unsigned slot
= SI_PS_IMAGE_COLORBUF0
;
884 struct pipe_surface
*surf
= NULL
;
886 /* si_texture_disable_dcc can get us here again. */
887 if (sctx
->blitter
->running
)
890 /* See whether FBFETCH is used and color buffer 0 is set. */
891 if (sctx
->ps_shader
.cso
&&
892 sctx
->ps_shader
.cso
->info
.uses_fbfetch
&&
893 sctx
->framebuffer
.state
.nr_cbufs
&&
894 sctx
->framebuffer
.state
.cbufs
[0])
895 surf
= sctx
->framebuffer
.state
.cbufs
[0];
897 /* Return if FBFETCH transitions from disabled to disabled. */
898 if (!buffers
->buffers
[slot
] && !surf
)
901 sctx
->ps_uses_fbfetch
= surf
!= NULL
;
902 si_update_ps_iter_samples(sctx
);
905 struct si_texture
*tex
= (struct si_texture
*)surf
->texture
;
906 struct pipe_image_view view
;
909 assert(!tex
->is_depth
);
911 /* Disable DCC, because the texture is used as both a sampler
914 si_texture_disable_dcc(sctx
, tex
);
916 if (tex
->buffer
.b
.b
.nr_samples
<= 1 && tex
->cmask_buffer
) {
918 assert(tex
->cmask_buffer
!= &tex
->buffer
);
919 si_eliminate_fast_color_clear(sctx
, tex
);
920 si_texture_discard_cmask(sctx
->screen
, tex
);
923 view
.resource
= surf
->texture
;
924 view
.format
= surf
->format
;
925 view
.access
= PIPE_IMAGE_ACCESS_READ
;
926 view
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
;
927 view
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
;
928 view
.u
.tex
.level
= surf
->u
.tex
.level
;
930 /* Set the descriptor. */
931 uint32_t *desc
= descs
->list
+ slot
*4;
932 memset(desc
, 0, 16 * 4);
933 si_set_shader_image_desc(sctx
, &view
, true, desc
, desc
+ 8);
935 pipe_resource_reference(&buffers
->buffers
[slot
], &tex
->buffer
.b
.b
);
936 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
,
937 &tex
->buffer
, RADEON_USAGE_READ
,
938 RADEON_PRIO_SHADER_RW_IMAGE
);
939 buffers
->enabled_mask
|= 1u << slot
;
941 /* Clear the descriptor. */
942 memset(descs
->list
+ slot
*4, 0, 8*4);
943 pipe_resource_reference(&buffers
->buffers
[slot
], NULL
);
944 buffers
->enabled_mask
&= ~(1u << slot
);
947 sctx
->descriptors_dirty
|= 1u << SI_DESCS_RW_BUFFERS
;
952 static void si_bind_sampler_states(struct pipe_context
*ctx
,
953 enum pipe_shader_type shader
,
954 unsigned start
, unsigned count
, void **states
)
956 struct si_context
*sctx
= (struct si_context
*)ctx
;
957 struct si_samplers
*samplers
= &sctx
->samplers
[shader
];
958 struct si_descriptors
*desc
= si_sampler_and_image_descriptors(sctx
, shader
);
959 struct si_sampler_state
**sstates
= (struct si_sampler_state
**)states
;
962 if (!count
|| shader
>= SI_NUM_SHADERS
|| !sstates
)
965 for (i
= 0; i
< count
; i
++) {
966 unsigned slot
= start
+ i
;
967 unsigned desc_slot
= si_get_sampler_slot(slot
);
970 sstates
[i
] == samplers
->sampler_states
[slot
])
974 assert(sstates
[i
]->magic
== SI_SAMPLER_STATE_MAGIC
);
976 samplers
->sampler_states
[slot
] = sstates
[i
];
978 /* If FMASK is bound, don't overwrite it.
979 * The sampler state will be set after FMASK is unbound.
981 struct si_sampler_view
*sview
=
982 (struct si_sampler_view
*)samplers
->views
[slot
];
984 struct si_texture
*tex
= NULL
;
986 if (sview
&& sview
->base
.texture
&&
987 sview
->base
.texture
->target
!= PIPE_BUFFER
)
988 tex
= (struct si_texture
*)sview
->base
.texture
;
990 if (tex
&& tex
->surface
.fmask_size
)
993 si_set_sampler_state_desc(sstates
[i
], sview
, tex
,
994 desc
->list
+ desc_slot
* 16 + 12);
996 sctx
->descriptors_dirty
|= 1u << si_sampler_and_image_descriptors_idx(shader
);
1000 /* BUFFER RESOURCES */
1002 static void si_init_buffer_resources(struct si_buffer_resources
*buffers
,
1003 struct si_descriptors
*descs
,
1004 unsigned num_buffers
,
1005 short shader_userdata_rel_index
,
1006 enum radeon_bo_priority priority
,
1007 enum radeon_bo_priority priority_constbuf
)
1009 buffers
->priority
= priority
;
1010 buffers
->priority_constbuf
= priority_constbuf
;
1011 buffers
->buffers
= CALLOC(num_buffers
, sizeof(struct pipe_resource
*));
1012 buffers
->offsets
= CALLOC(num_buffers
, sizeof(buffers
->offsets
[0]));
1014 si_init_descriptors(descs
, shader_userdata_rel_index
, 4, num_buffers
);
1017 static void si_release_buffer_resources(struct si_buffer_resources
*buffers
,
1018 struct si_descriptors
*descs
)
1022 for (i
= 0; i
< descs
->num_elements
; i
++) {
1023 pipe_resource_reference(&buffers
->buffers
[i
], NULL
);
1026 FREE(buffers
->buffers
);
1027 FREE(buffers
->offsets
);
1030 static void si_buffer_resources_begin_new_cs(struct si_context
*sctx
,
1031 struct si_buffer_resources
*buffers
)
1033 unsigned mask
= buffers
->enabled_mask
;
1035 /* Add buffers to the CS. */
1037 int i
= u_bit_scan(&mask
);
1039 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
,
1040 si_resource(buffers
->buffers
[i
]),
1041 buffers
->writable_mask
& (1u << i
) ? RADEON_USAGE_READWRITE
:
1043 i
< SI_NUM_SHADER_BUFFERS
? buffers
->priority
:
1044 buffers
->priority_constbuf
);
1048 static void si_get_buffer_from_descriptors(struct si_buffer_resources
*buffers
,
1049 struct si_descriptors
*descs
,
1050 unsigned idx
, struct pipe_resource
**buf
,
1051 unsigned *offset
, unsigned *size
)
1053 pipe_resource_reference(buf
, buffers
->buffers
[idx
]);
1055 struct si_resource
*res
= si_resource(*buf
);
1056 const uint32_t *desc
= descs
->list
+ idx
* 4;
1061 assert(G_008F04_STRIDE(desc
[1]) == 0);
1062 va
= si_desc_extract_buffer_address(desc
);
1064 assert(va
>= res
->gpu_address
&& va
+ *size
<= res
->gpu_address
+ res
->bo_size
);
1065 *offset
= va
- res
->gpu_address
;
1069 /* VERTEX BUFFERS */
1071 static void si_vertex_buffers_begin_new_cs(struct si_context
*sctx
)
1073 int count
= sctx
->vertex_elements
? sctx
->vertex_elements
->count
: 0;
1076 for (i
= 0; i
< count
; i
++) {
1077 int vb
= sctx
->vertex_elements
->vertex_buffer_index
[i
];
1079 if (vb
>= ARRAY_SIZE(sctx
->vertex_buffer
))
1081 if (!sctx
->vertex_buffer
[vb
].buffer
.resource
)
1084 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
,
1085 si_resource(sctx
->vertex_buffer
[vb
].buffer
.resource
),
1086 RADEON_USAGE_READ
, RADEON_PRIO_VERTEX_BUFFER
);
1089 if (!sctx
->vb_descriptors_buffer
)
1091 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
,
1092 sctx
->vb_descriptors_buffer
, RADEON_USAGE_READ
,
1093 RADEON_PRIO_DESCRIPTORS
);
1096 bool si_upload_vertex_buffer_descriptors(struct si_context
*sctx
)
1098 struct si_vertex_elements
*velems
= sctx
->vertex_elements
;
1102 if (!sctx
->vertex_buffers_dirty
|| !velems
)
1105 count
= velems
->count
;
1110 unsigned alloc_size
= velems
->vb_desc_list_alloc_size
;
1111 unsigned first_vb_use_mask
= velems
->first_vb_use_mask
;
1113 /* Vertex buffer descriptors are the only ones which are uploaded
1114 * directly through a staging buffer and don't go through
1115 * the fine-grained upload path.
1117 u_upload_alloc(sctx
->b
.const_uploader
, 0,
1119 si_optimal_tcc_alignment(sctx
, alloc_size
),
1120 &sctx
->vb_descriptors_offset
,
1121 (struct pipe_resource
**)&sctx
->vb_descriptors_buffer
,
1123 if (!sctx
->vb_descriptors_buffer
) {
1124 sctx
->vb_descriptors_offset
= 0;
1125 sctx
->vb_descriptors_gpu_list
= NULL
;
1129 sctx
->vb_descriptors_gpu_list
= ptr
;
1130 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
,
1131 sctx
->vb_descriptors_buffer
, RADEON_USAGE_READ
,
1132 RADEON_PRIO_DESCRIPTORS
);
1134 assert(count
<= SI_MAX_ATTRIBS
);
1136 for (i
= 0; i
< count
; i
++) {
1137 struct pipe_vertex_buffer
*vb
;
1138 struct si_resource
*buf
;
1139 unsigned vbo_index
= velems
->vertex_buffer_index
[i
];
1140 uint32_t *desc
= &ptr
[i
*4];
1142 vb
= &sctx
->vertex_buffer
[vbo_index
];
1143 buf
= si_resource(vb
->buffer
.resource
);
1145 memset(desc
, 0, 16);
1149 int64_t offset
= (int64_t)((int)vb
->buffer_offset
) +
1150 velems
->src_offset
[i
];
1152 if (offset
>= buf
->b
.b
.width0
) {
1153 assert(offset
< buf
->b
.b
.width0
);
1154 memset(desc
, 0, 16);
1158 uint64_t va
= buf
->gpu_address
+ offset
;
1160 int64_t num_records
= (int64_t)buf
->b
.b
.width0
- offset
;
1161 if (sctx
->chip_class
!= GFX8
&& vb
->stride
) {
1162 /* Round up by rounding down and adding 1 */
1163 num_records
= (num_records
- velems
->format_size
[i
]) /
1166 assert(num_records
>= 0 && num_records
<= UINT_MAX
);
1168 uint32_t rsrc_word3
= velems
->rsrc_word3
[i
];
1170 /* OOB_SELECT chooses the out-of-bounds check:
1171 * - 1: index >= NUM_RECORDS (Structured)
1172 * - 3: offset >= NUM_RECORDS (Raw)
1174 if (sctx
->chip_class
>= GFX10
)
1175 rsrc_word3
|= S_008F0C_OOB_SELECT(vb
->stride
? V_008F0C_OOB_SELECT_STRUCTURED
: V_008F0C_OOB_SELECT_RAW
);
1178 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1179 S_008F04_STRIDE(vb
->stride
);
1180 desc
[2] = num_records
;
1181 desc
[3] = rsrc_word3
;
1183 if (first_vb_use_mask
& (1 << i
)) {
1184 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
,
1185 si_resource(vb
->buffer
.resource
),
1186 RADEON_USAGE_READ
, RADEON_PRIO_VERTEX_BUFFER
);
1190 /* Don't flush the const cache. It would have a very negative effect
1191 * on performance (confirmed by testing). New descriptors are always
1192 * uploaded to a fresh new buffer, so I don't think flushing the const
1193 * cache is needed. */
1194 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.shader_pointers
);
1195 sctx
->vertex_buffers_dirty
= false;
1196 sctx
->vertex_buffer_pointer_dirty
= true;
1197 sctx
->prefetch_L2_mask
|= SI_PREFETCH_VBO_DESCRIPTORS
;
1202 /* CONSTANT BUFFERS */
1204 static struct si_descriptors
*
1205 si_const_and_shader_buffer_descriptors(struct si_context
*sctx
, unsigned shader
)
1207 return &sctx
->descriptors
[si_const_and_shader_buffer_descriptors_idx(shader
)];
1210 void si_upload_const_buffer(struct si_context
*sctx
, struct si_resource
**buf
,
1211 const uint8_t *ptr
, unsigned size
, uint32_t *const_offset
)
1215 u_upload_alloc(sctx
->b
.const_uploader
, 0, size
,
1216 si_optimal_tcc_alignment(sctx
, size
),
1218 (struct pipe_resource
**)buf
, &tmp
);
1220 util_memcpy_cpu_to_le32(tmp
, ptr
, size
);
1223 static void si_set_constant_buffer(struct si_context
*sctx
,
1224 struct si_buffer_resources
*buffers
,
1225 unsigned descriptors_idx
,
1226 uint slot
, const struct pipe_constant_buffer
*input
)
1228 struct si_descriptors
*descs
= &sctx
->descriptors
[descriptors_idx
];
1229 assert(slot
< descs
->num_elements
);
1230 pipe_resource_reference(&buffers
->buffers
[slot
], NULL
);
1232 /* GFX7 cannot unbind a constant buffer (S_BUFFER_LOAD is buggy
1233 * with a NULL buffer). We need to use a dummy buffer instead. */
1234 if (sctx
->chip_class
== GFX7
&&
1235 (!input
|| (!input
->buffer
&& !input
->user_buffer
)))
1236 input
= &sctx
->null_const_buf
;
1238 if (input
&& (input
->buffer
|| input
->user_buffer
)) {
1239 struct pipe_resource
*buffer
= NULL
;
1241 unsigned buffer_offset
;
1243 /* Upload the user buffer if needed. */
1244 if (input
->user_buffer
) {
1245 si_upload_const_buffer(sctx
,
1246 (struct si_resource
**)&buffer
, input
->user_buffer
,
1247 input
->buffer_size
, &buffer_offset
);
1249 /* Just unbind on failure. */
1250 si_set_constant_buffer(sctx
, buffers
, descriptors_idx
, slot
, NULL
);
1254 pipe_resource_reference(&buffer
, input
->buffer
);
1255 buffer_offset
= input
->buffer_offset
;
1258 va
= si_resource(buffer
)->gpu_address
+ buffer_offset
;
1260 /* Set the descriptor. */
1261 uint32_t *desc
= descs
->list
+ slot
*4;
1263 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1265 desc
[2] = input
->buffer_size
;
1266 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1267 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1268 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1269 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
1271 if (sctx
->chip_class
>= GFX10
) {
1272 desc
[3] |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
1273 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW
) |
1274 S_008F0C_RESOURCE_LEVEL(1);
1276 desc
[3] |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1277 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
1280 buffers
->buffers
[slot
] = buffer
;
1281 buffers
->offsets
[slot
] = buffer_offset
;
1282 radeon_add_to_gfx_buffer_list_check_mem(sctx
,
1283 si_resource(buffer
),
1285 buffers
->priority_constbuf
, true);
1286 buffers
->enabled_mask
|= 1u << slot
;
1288 /* Clear the descriptor. */
1289 memset(descs
->list
+ slot
*4, 0, sizeof(uint32_t) * 4);
1290 buffers
->enabled_mask
&= ~(1u << slot
);
1293 sctx
->descriptors_dirty
|= 1u << descriptors_idx
;
1296 static void si_pipe_set_constant_buffer(struct pipe_context
*ctx
,
1297 enum pipe_shader_type shader
, uint slot
,
1298 const struct pipe_constant_buffer
*input
)
1300 struct si_context
*sctx
= (struct si_context
*)ctx
;
1302 if (shader
>= SI_NUM_SHADERS
)
1305 if (slot
== 0 && input
&& input
->buffer
&&
1306 !(si_resource(input
->buffer
)->flags
& RADEON_FLAG_32BIT
)) {
1307 assert(!"constant buffer 0 must have a 32-bit VM address, use const_uploader");
1311 if (input
&& input
->buffer
)
1312 si_resource(input
->buffer
)->bind_history
|= PIPE_BIND_CONSTANT_BUFFER
;
1314 slot
= si_get_constbuf_slot(slot
);
1315 si_set_constant_buffer(sctx
, &sctx
->const_and_shader_buffers
[shader
],
1316 si_const_and_shader_buffer_descriptors_idx(shader
),
1320 void si_get_pipe_constant_buffer(struct si_context
*sctx
, uint shader
,
1321 uint slot
, struct pipe_constant_buffer
*cbuf
)
1323 cbuf
->user_buffer
= NULL
;
1324 si_get_buffer_from_descriptors(
1325 &sctx
->const_and_shader_buffers
[shader
],
1326 si_const_and_shader_buffer_descriptors(sctx
, shader
),
1327 si_get_constbuf_slot(slot
),
1328 &cbuf
->buffer
, &cbuf
->buffer_offset
, &cbuf
->buffer_size
);
1331 /* SHADER BUFFERS */
1333 static void si_set_shader_buffer(struct si_context
*sctx
,
1334 struct si_buffer_resources
*buffers
,
1335 unsigned descriptors_idx
,
1336 uint slot
, const struct pipe_shader_buffer
*sbuffer
,
1337 bool writable
, enum radeon_bo_priority priority
)
1339 struct si_descriptors
*descs
= &sctx
->descriptors
[descriptors_idx
];
1340 uint32_t *desc
= descs
->list
+ slot
* 4;
1342 if (!sbuffer
|| !sbuffer
->buffer
) {
1343 pipe_resource_reference(&buffers
->buffers
[slot
], NULL
);
1344 memset(desc
, 0, sizeof(uint32_t) * 4);
1345 buffers
->enabled_mask
&= ~(1u << slot
);
1346 buffers
->writable_mask
&= ~(1u << slot
);
1347 sctx
->descriptors_dirty
|= 1u << descriptors_idx
;
1351 struct si_resource
*buf
= si_resource(sbuffer
->buffer
);
1352 uint64_t va
= buf
->gpu_address
+ sbuffer
->buffer_offset
;
1355 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1357 desc
[2] = sbuffer
->buffer_size
;
1358 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1359 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1360 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1361 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
1363 if (sctx
->chip_class
>= GFX10
) {
1364 desc
[3] |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
1365 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW
) |
1366 S_008F0C_RESOURCE_LEVEL(1);
1368 desc
[3] |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1369 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
1372 pipe_resource_reference(&buffers
->buffers
[slot
], &buf
->b
.b
);
1373 buffers
->offsets
[slot
] = sbuffer
->buffer_offset
;
1374 radeon_add_to_gfx_buffer_list_check_mem(sctx
, buf
,
1375 writable
? RADEON_USAGE_READWRITE
:
1379 buffers
->writable_mask
|= 1u << slot
;
1381 buffers
->writable_mask
&= ~(1u << slot
);
1383 buffers
->enabled_mask
|= 1u << slot
;
1384 sctx
->descriptors_dirty
|= 1u << descriptors_idx
;
1386 util_range_add(&buf
->b
.b
, &buf
->valid_buffer_range
, sbuffer
->buffer_offset
,
1387 sbuffer
->buffer_offset
+ sbuffer
->buffer_size
);
1390 static void si_set_shader_buffers(struct pipe_context
*ctx
,
1391 enum pipe_shader_type shader
,
1392 unsigned start_slot
, unsigned count
,
1393 const struct pipe_shader_buffer
*sbuffers
,
1394 unsigned writable_bitmask
)
1396 struct si_context
*sctx
= (struct si_context
*)ctx
;
1397 struct si_buffer_resources
*buffers
= &sctx
->const_and_shader_buffers
[shader
];
1398 unsigned descriptors_idx
= si_const_and_shader_buffer_descriptors_idx(shader
);
1401 assert(start_slot
+ count
<= SI_NUM_SHADER_BUFFERS
);
1403 for (i
= 0; i
< count
; ++i
) {
1404 const struct pipe_shader_buffer
*sbuffer
= sbuffers
? &sbuffers
[i
] : NULL
;
1405 unsigned slot
= si_get_shaderbuf_slot(start_slot
+ i
);
1407 if (sbuffer
&& sbuffer
->buffer
)
1408 si_resource(sbuffer
->buffer
)->bind_history
|= PIPE_BIND_SHADER_BUFFER
;
1410 si_set_shader_buffer(sctx
, buffers
, descriptors_idx
, slot
, sbuffer
,
1411 !!(writable_bitmask
& (1u << i
)),
1416 void si_get_shader_buffers(struct si_context
*sctx
,
1417 enum pipe_shader_type shader
,
1418 uint start_slot
, uint count
,
1419 struct pipe_shader_buffer
*sbuf
)
1421 struct si_buffer_resources
*buffers
= &sctx
->const_and_shader_buffers
[shader
];
1422 struct si_descriptors
*descs
= si_const_and_shader_buffer_descriptors(sctx
, shader
);
1424 for (unsigned i
= 0; i
< count
; ++i
) {
1425 si_get_buffer_from_descriptors(
1427 si_get_shaderbuf_slot(start_slot
+ i
),
1428 &sbuf
[i
].buffer
, &sbuf
[i
].buffer_offset
,
1429 &sbuf
[i
].buffer_size
);
1435 void si_set_rw_buffer(struct si_context
*sctx
,
1436 uint slot
, const struct pipe_constant_buffer
*input
)
1438 si_set_constant_buffer(sctx
, &sctx
->rw_buffers
, SI_DESCS_RW_BUFFERS
,
1442 void si_set_rw_shader_buffer(struct si_context
*sctx
, uint slot
,
1443 const struct pipe_shader_buffer
*sbuffer
)
1445 si_set_shader_buffer(sctx
, &sctx
->rw_buffers
, SI_DESCS_RW_BUFFERS
,
1446 slot
, sbuffer
, true, RADEON_PRIO_SHADER_RW_BUFFER
);
1449 void si_set_ring_buffer(struct si_context
*sctx
, uint slot
,
1450 struct pipe_resource
*buffer
,
1451 unsigned stride
, unsigned num_records
,
1452 bool add_tid
, bool swizzle
,
1453 unsigned element_size
, unsigned index_stride
, uint64_t offset
)
1455 struct si_buffer_resources
*buffers
= &sctx
->rw_buffers
;
1456 struct si_descriptors
*descs
= &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
1458 /* The stride field in the resource descriptor has 14 bits */
1459 assert(stride
< (1 << 14));
1461 assert(slot
< descs
->num_elements
);
1462 pipe_resource_reference(&buffers
->buffers
[slot
], NULL
);
1467 va
= si_resource(buffer
)->gpu_address
+ offset
;
1469 switch (element_size
) {
1471 assert(!"Unsupported ring buffer element size");
1487 switch (index_stride
) {
1489 assert(!"Unsupported ring buffer index stride");
1505 if (sctx
->chip_class
>= GFX8
&& stride
)
1506 num_records
*= stride
;
1508 /* Set the descriptor. */
1509 uint32_t *desc
= descs
->list
+ slot
*4;
1511 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1512 S_008F04_STRIDE(stride
) |
1513 S_008F04_SWIZZLE_ENABLE(swizzle
);
1514 desc
[2] = num_records
;
1515 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1516 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1517 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1518 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1519 S_008F0C_INDEX_STRIDE(index_stride
) |
1520 S_008F0C_ADD_TID_ENABLE(add_tid
);
1522 if (sctx
->chip_class
>= GFX9
)
1523 assert(!swizzle
|| element_size
== 1); /* always 4 bytes on GFX9 */
1525 desc
[3] |= S_008F0C_ELEMENT_SIZE(element_size
);
1527 if (sctx
->chip_class
>= GFX10
) {
1528 desc
[3] |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
1529 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_DISABLED
) |
1530 S_008F0C_RESOURCE_LEVEL(1);
1532 desc
[3] |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1533 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
1536 pipe_resource_reference(&buffers
->buffers
[slot
], buffer
);
1537 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
,
1538 si_resource(buffer
),
1539 RADEON_USAGE_READWRITE
, buffers
->priority
);
1540 buffers
->enabled_mask
|= 1u << slot
;
1542 /* Clear the descriptor. */
1543 memset(descs
->list
+ slot
*4, 0, sizeof(uint32_t) * 4);
1544 buffers
->enabled_mask
&= ~(1u << slot
);
1547 sctx
->descriptors_dirty
|= 1u << SI_DESCS_RW_BUFFERS
;
1550 /* INTERNAL CONST BUFFERS */
1552 static void si_set_polygon_stipple(struct pipe_context
*ctx
,
1553 const struct pipe_poly_stipple
*state
)
1555 struct si_context
*sctx
= (struct si_context
*)ctx
;
1556 struct pipe_constant_buffer cb
= {};
1557 unsigned stipple
[32];
1560 for (i
= 0; i
< 32; i
++)
1561 stipple
[i
] = util_bitreverse(state
->stipple
[i
]);
1563 cb
.user_buffer
= stipple
;
1564 cb
.buffer_size
= sizeof(stipple
);
1566 si_set_rw_buffer(sctx
, SI_PS_CONST_POLY_STIPPLE
, &cb
);
1569 /* TEXTURE METADATA ENABLE/DISABLE */
1572 si_resident_handles_update_needs_color_decompress(struct si_context
*sctx
)
1574 util_dynarray_clear(&sctx
->resident_tex_needs_color_decompress
);
1575 util_dynarray_clear(&sctx
->resident_img_needs_color_decompress
);
1577 util_dynarray_foreach(&sctx
->resident_tex_handles
,
1578 struct si_texture_handle
*, tex_handle
) {
1579 struct pipe_resource
*res
= (*tex_handle
)->view
->texture
;
1580 struct si_texture
*tex
;
1582 if (!res
|| res
->target
== PIPE_BUFFER
)
1585 tex
= (struct si_texture
*)res
;
1586 if (!color_needs_decompression(tex
))
1589 util_dynarray_append(&sctx
->resident_tex_needs_color_decompress
,
1590 struct si_texture_handle
*, *tex_handle
);
1593 util_dynarray_foreach(&sctx
->resident_img_handles
,
1594 struct si_image_handle
*, img_handle
) {
1595 struct pipe_image_view
*view
= &(*img_handle
)->view
;
1596 struct pipe_resource
*res
= view
->resource
;
1597 struct si_texture
*tex
;
1599 if (!res
|| res
->target
== PIPE_BUFFER
)
1602 tex
= (struct si_texture
*)res
;
1603 if (!color_needs_decompression(tex
))
1606 util_dynarray_append(&sctx
->resident_img_needs_color_decompress
,
1607 struct si_image_handle
*, *img_handle
);
1611 /* CMASK can be enabled (for fast clear) and disabled (for texture export)
1612 * while the texture is bound, possibly by a different context. In that case,
1613 * call this function to update needs_*_decompress_masks.
1615 void si_update_needs_color_decompress_masks(struct si_context
*sctx
)
1617 for (int i
= 0; i
< SI_NUM_SHADERS
; ++i
) {
1618 si_samplers_update_needs_color_decompress_mask(&sctx
->samplers
[i
]);
1619 si_images_update_needs_color_decompress_mask(&sctx
->images
[i
]);
1620 si_update_shader_needs_decompress_mask(sctx
, i
);
1623 si_resident_handles_update_needs_color_decompress(sctx
);
1626 /* BUFFER DISCARD/INVALIDATION */
1628 /* Reset descriptors of buffer resources after \p buf has been invalidated.
1629 * If buf == NULL, reset all descriptors.
1631 static void si_reset_buffer_resources(struct si_context
*sctx
,
1632 struct si_buffer_resources
*buffers
,
1633 unsigned descriptors_idx
,
1635 struct pipe_resource
*buf
,
1636 enum radeon_bo_priority priority
)
1638 struct si_descriptors
*descs
= &sctx
->descriptors
[descriptors_idx
];
1639 unsigned mask
= buffers
->enabled_mask
& slot_mask
;
1642 unsigned i
= u_bit_scan(&mask
);
1643 struct pipe_resource
*buffer
= buffers
->buffers
[i
];
1645 if (buffer
&& (!buf
|| buffer
== buf
)) {
1646 si_set_buf_desc_address(si_resource(buffer
), buffers
->offsets
[i
],
1648 sctx
->descriptors_dirty
|= 1u << descriptors_idx
;
1650 radeon_add_to_gfx_buffer_list_check_mem(sctx
,
1651 si_resource(buffer
),
1652 buffers
->writable_mask
& (1u << i
) ?
1653 RADEON_USAGE_READWRITE
:
1660 /* Update all buffer bindings where the buffer is bound, including
1661 * all resource descriptors. This is invalidate_buffer without
1664 * If buf == NULL, update all buffer bindings.
1666 void si_rebind_buffer(struct si_context
*sctx
, struct pipe_resource
*buf
)
1668 struct si_resource
*buffer
= si_resource(buf
);
1670 unsigned num_elems
= sctx
->vertex_elements
?
1671 sctx
->vertex_elements
->count
: 0;
1673 /* We changed the buffer, now we need to bind it where the old one
1674 * was bound. This consists of 2 things:
1675 * 1) Updating the resource descriptor and dirtying it.
1676 * 2) Adding a relocation to the CS, so that it's usable.
1679 /* Vertex buffers. */
1682 sctx
->vertex_buffers_dirty
= true;
1683 } else if (buffer
->bind_history
& PIPE_BIND_VERTEX_BUFFER
) {
1684 for (i
= 0; i
< num_elems
; i
++) {
1685 int vb
= sctx
->vertex_elements
->vertex_buffer_index
[i
];
1687 if (vb
>= ARRAY_SIZE(sctx
->vertex_buffer
))
1689 if (!sctx
->vertex_buffer
[vb
].buffer
.resource
)
1692 if (sctx
->vertex_buffer
[vb
].buffer
.resource
== buf
) {
1693 sctx
->vertex_buffers_dirty
= true;
1699 /* Streamout buffers. (other internal buffers can't be invalidated) */
1700 if (!buffer
|| buffer
->bind_history
& PIPE_BIND_STREAM_OUTPUT
) {
1701 for (i
= SI_VS_STREAMOUT_BUF0
; i
<= SI_VS_STREAMOUT_BUF3
; i
++) {
1702 struct si_buffer_resources
*buffers
= &sctx
->rw_buffers
;
1703 struct si_descriptors
*descs
=
1704 &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
1705 struct pipe_resource
*buffer
= buffers
->buffers
[i
];
1707 if (!buffer
|| (buf
&& buffer
!= buf
))
1710 si_set_buf_desc_address(si_resource(buffer
), buffers
->offsets
[i
],
1712 sctx
->descriptors_dirty
|= 1u << SI_DESCS_RW_BUFFERS
;
1714 radeon_add_to_gfx_buffer_list_check_mem(sctx
,
1715 si_resource(buffer
),
1717 RADEON_PRIO_SHADER_RW_BUFFER
,
1720 /* Update the streamout state. */
1721 if (sctx
->streamout
.begin_emitted
)
1722 si_emit_streamout_end(sctx
);
1723 sctx
->streamout
.append_bitmask
=
1724 sctx
->streamout
.enabled_mask
;
1725 si_streamout_buffers_dirty(sctx
);
1729 /* Constant and shader buffers. */
1730 if (!buffer
|| buffer
->bind_history
& PIPE_BIND_CONSTANT_BUFFER
) {
1731 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++)
1732 si_reset_buffer_resources(sctx
, &sctx
->const_and_shader_buffers
[shader
],
1733 si_const_and_shader_buffer_descriptors_idx(shader
),
1734 u_bit_consecutive(SI_NUM_SHADER_BUFFERS
, SI_NUM_CONST_BUFFERS
),
1736 sctx
->const_and_shader_buffers
[shader
].priority_constbuf
);
1739 if (!buffer
|| buffer
->bind_history
& PIPE_BIND_SHADER_BUFFER
) {
1740 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++)
1741 si_reset_buffer_resources(sctx
, &sctx
->const_and_shader_buffers
[shader
],
1742 si_const_and_shader_buffer_descriptors_idx(shader
),
1743 u_bit_consecutive(0, SI_NUM_SHADER_BUFFERS
),
1745 sctx
->const_and_shader_buffers
[shader
].priority
);
1748 if (!buffer
|| buffer
->bind_history
& PIPE_BIND_SAMPLER_VIEW
) {
1749 /* Texture buffers - update bindings. */
1750 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++) {
1751 struct si_samplers
*samplers
= &sctx
->samplers
[shader
];
1752 struct si_descriptors
*descs
=
1753 si_sampler_and_image_descriptors(sctx
, shader
);
1754 unsigned mask
= samplers
->enabled_mask
;
1757 unsigned i
= u_bit_scan(&mask
);
1758 struct pipe_resource
*buffer
= samplers
->views
[i
]->texture
;
1760 if (buffer
&& buffer
->target
== PIPE_BUFFER
&&
1761 (!buf
|| buffer
== buf
)) {
1762 unsigned desc_slot
= si_get_sampler_slot(i
);
1764 si_set_buf_desc_address(si_resource(buffer
),
1765 samplers
->views
[i
]->u
.buf
.offset
,
1766 descs
->list
+ desc_slot
* 16 + 4);
1767 sctx
->descriptors_dirty
|=
1768 1u << si_sampler_and_image_descriptors_idx(shader
);
1770 radeon_add_to_gfx_buffer_list_check_mem(
1771 sctx
, si_resource(buffer
),
1773 RADEON_PRIO_SAMPLER_BUFFER
, true);
1780 if (!buffer
|| buffer
->bind_history
& PIPE_BIND_SHADER_IMAGE
) {
1781 for (shader
= 0; shader
< SI_NUM_SHADERS
; ++shader
) {
1782 struct si_images
*images
= &sctx
->images
[shader
];
1783 struct si_descriptors
*descs
=
1784 si_sampler_and_image_descriptors(sctx
, shader
);
1785 unsigned mask
= images
->enabled_mask
;
1788 unsigned i
= u_bit_scan(&mask
);
1789 struct pipe_resource
*buffer
= images
->views
[i
].resource
;
1791 if (buffer
&& buffer
->target
== PIPE_BUFFER
&&
1792 (!buf
|| buffer
== buf
)) {
1793 unsigned desc_slot
= si_get_image_slot(i
);
1795 if (images
->views
[i
].access
& PIPE_IMAGE_ACCESS_WRITE
)
1796 si_mark_image_range_valid(&images
->views
[i
]);
1798 si_set_buf_desc_address(si_resource(buffer
),
1799 images
->views
[i
].u
.buf
.offset
,
1800 descs
->list
+ desc_slot
* 8 + 4);
1801 sctx
->descriptors_dirty
|=
1802 1u << si_sampler_and_image_descriptors_idx(shader
);
1804 radeon_add_to_gfx_buffer_list_check_mem(
1805 sctx
, si_resource(buffer
),
1806 RADEON_USAGE_READWRITE
,
1807 RADEON_PRIO_SAMPLER_BUFFER
, true);
1813 /* Bindless texture handles */
1814 if (!buffer
|| buffer
->texture_handle_allocated
) {
1815 struct si_descriptors
*descs
= &sctx
->bindless_descriptors
;
1817 util_dynarray_foreach(&sctx
->resident_tex_handles
,
1818 struct si_texture_handle
*, tex_handle
) {
1819 struct pipe_sampler_view
*view
= (*tex_handle
)->view
;
1820 unsigned desc_slot
= (*tex_handle
)->desc_slot
;
1821 struct pipe_resource
*buffer
= view
->texture
;
1823 if (buffer
&& buffer
->target
== PIPE_BUFFER
&&
1824 (!buf
|| buffer
== buf
)) {
1825 si_set_buf_desc_address(si_resource(buffer
),
1828 desc_slot
* 16 + 4);
1830 (*tex_handle
)->desc_dirty
= true;
1831 sctx
->bindless_descriptors_dirty
= true;
1833 radeon_add_to_gfx_buffer_list_check_mem(
1834 sctx
, si_resource(buffer
),
1836 RADEON_PRIO_SAMPLER_BUFFER
, true);
1841 /* Bindless image handles */
1842 if (!buffer
|| buffer
->image_handle_allocated
) {
1843 struct si_descriptors
*descs
= &sctx
->bindless_descriptors
;
1845 util_dynarray_foreach(&sctx
->resident_img_handles
,
1846 struct si_image_handle
*, img_handle
) {
1847 struct pipe_image_view
*view
= &(*img_handle
)->view
;
1848 unsigned desc_slot
= (*img_handle
)->desc_slot
;
1849 struct pipe_resource
*buffer
= view
->resource
;
1851 if (buffer
&& buffer
->target
== PIPE_BUFFER
&&
1852 (!buf
|| buffer
== buf
)) {
1853 if (view
->access
& PIPE_IMAGE_ACCESS_WRITE
)
1854 si_mark_image_range_valid(view
);
1856 si_set_buf_desc_address(si_resource(buffer
),
1859 desc_slot
* 16 + 4);
1861 (*img_handle
)->desc_dirty
= true;
1862 sctx
->bindless_descriptors_dirty
= true;
1864 radeon_add_to_gfx_buffer_list_check_mem(
1865 sctx
, si_resource(buffer
),
1866 RADEON_USAGE_READWRITE
,
1867 RADEON_PRIO_SAMPLER_BUFFER
, true);
1873 /* Do the same for other contexts. They will invoke this function
1874 * with buffer == NULL.
1876 unsigned new_counter
= p_atomic_inc_return(&sctx
->screen
->dirty_buf_counter
);
1878 /* Skip the update for the current context, because we have already updated
1879 * the buffer bindings.
1881 if (new_counter
== sctx
->last_dirty_buf_counter
+ 1)
1882 sctx
->last_dirty_buf_counter
= new_counter
;
1886 static void si_upload_bindless_descriptor(struct si_context
*sctx
,
1888 unsigned num_dwords
)
1890 struct si_descriptors
*desc
= &sctx
->bindless_descriptors
;
1891 unsigned desc_slot_offset
= desc_slot
* 16;
1895 data
= desc
->list
+ desc_slot_offset
;
1896 va
= desc
->gpu_address
+ desc_slot_offset
* 4;
1898 si_cp_write_data(sctx
, desc
->buffer
, va
- desc
->buffer
->gpu_address
,
1899 num_dwords
* 4, V_370_TC_L2
, V_370_ME
, data
);
1902 static void si_upload_bindless_descriptors(struct si_context
*sctx
)
1904 if (!sctx
->bindless_descriptors_dirty
)
1907 /* Wait for graphics/compute to be idle before updating the resident
1908 * descriptors directly in memory, in case the GPU is using them.
1910 sctx
->flags
|= SI_CONTEXT_PS_PARTIAL_FLUSH
|
1911 SI_CONTEXT_CS_PARTIAL_FLUSH
;
1912 sctx
->emit_cache_flush(sctx
);
1914 util_dynarray_foreach(&sctx
->resident_tex_handles
,
1915 struct si_texture_handle
*, tex_handle
) {
1916 unsigned desc_slot
= (*tex_handle
)->desc_slot
;
1918 if (!(*tex_handle
)->desc_dirty
)
1921 si_upload_bindless_descriptor(sctx
, desc_slot
, 16);
1922 (*tex_handle
)->desc_dirty
= false;
1925 util_dynarray_foreach(&sctx
->resident_img_handles
,
1926 struct si_image_handle
*, img_handle
) {
1927 unsigned desc_slot
= (*img_handle
)->desc_slot
;
1929 if (!(*img_handle
)->desc_dirty
)
1932 si_upload_bindless_descriptor(sctx
, desc_slot
, 8);
1933 (*img_handle
)->desc_dirty
= false;
1936 /* Invalidate L1 because it doesn't know that L2 changed. */
1937 sctx
->flags
|= SI_CONTEXT_INV_SCACHE
;
1938 sctx
->emit_cache_flush(sctx
);
1940 sctx
->bindless_descriptors_dirty
= false;
1943 /* Update mutable image descriptor fields of all resident textures. */
1944 static void si_update_bindless_texture_descriptor(struct si_context
*sctx
,
1945 struct si_texture_handle
*tex_handle
)
1947 struct si_sampler_view
*sview
= (struct si_sampler_view
*)tex_handle
->view
;
1948 struct si_descriptors
*desc
= &sctx
->bindless_descriptors
;
1949 unsigned desc_slot_offset
= tex_handle
->desc_slot
* 16;
1950 uint32_t desc_list
[16];
1952 if (sview
->base
.texture
->target
== PIPE_BUFFER
)
1955 memcpy(desc_list
, desc
->list
+ desc_slot_offset
, sizeof(desc_list
));
1956 si_set_sampler_view_desc(sctx
, sview
, &tex_handle
->sstate
,
1957 desc
->list
+ desc_slot_offset
);
1959 if (memcmp(desc_list
, desc
->list
+ desc_slot_offset
,
1960 sizeof(desc_list
))) {
1961 tex_handle
->desc_dirty
= true;
1962 sctx
->bindless_descriptors_dirty
= true;
1966 static void si_update_bindless_image_descriptor(struct si_context
*sctx
,
1967 struct si_image_handle
*img_handle
)
1969 struct si_descriptors
*desc
= &sctx
->bindless_descriptors
;
1970 unsigned desc_slot_offset
= img_handle
->desc_slot
* 16;
1971 struct pipe_image_view
*view
= &img_handle
->view
;
1972 struct pipe_resource
*res
= view
->resource
;
1973 uint32_t image_desc
[16];
1974 unsigned desc_size
= (res
->nr_samples
>= 2 ? 16 : 8) * 4;
1976 if (res
->target
== PIPE_BUFFER
)
1979 memcpy(image_desc
, desc
->list
+ desc_slot_offset
, desc_size
);
1980 si_set_shader_image_desc(sctx
, view
, true,
1981 desc
->list
+ desc_slot_offset
,
1982 desc
->list
+ desc_slot_offset
+ 8);
1984 if (memcmp(image_desc
, desc
->list
+ desc_slot_offset
, desc_size
)) {
1985 img_handle
->desc_dirty
= true;
1986 sctx
->bindless_descriptors_dirty
= true;
1990 static void si_update_all_resident_texture_descriptors(struct si_context
*sctx
)
1992 util_dynarray_foreach(&sctx
->resident_tex_handles
,
1993 struct si_texture_handle
*, tex_handle
) {
1994 si_update_bindless_texture_descriptor(sctx
, *tex_handle
);
1997 util_dynarray_foreach(&sctx
->resident_img_handles
,
1998 struct si_image_handle
*, img_handle
) {
1999 si_update_bindless_image_descriptor(sctx
, *img_handle
);
2002 si_upload_bindless_descriptors(sctx
);
2005 /* Update mutable image descriptor fields of all bound textures. */
2006 void si_update_all_texture_descriptors(struct si_context
*sctx
)
2010 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++) {
2011 struct si_samplers
*samplers
= &sctx
->samplers
[shader
];
2012 struct si_images
*images
= &sctx
->images
[shader
];
2016 mask
= images
->enabled_mask
;
2018 unsigned i
= u_bit_scan(&mask
);
2019 struct pipe_image_view
*view
= &images
->views
[i
];
2021 if (!view
->resource
||
2022 view
->resource
->target
== PIPE_BUFFER
)
2025 si_set_shader_image(sctx
, shader
, i
, view
, true);
2028 /* Sampler views. */
2029 mask
= samplers
->enabled_mask
;
2031 unsigned i
= u_bit_scan(&mask
);
2032 struct pipe_sampler_view
*view
= samplers
->views
[i
];
2036 view
->texture
->target
== PIPE_BUFFER
)
2039 si_set_sampler_view(sctx
, shader
, i
,
2040 samplers
->views
[i
], true);
2043 si_update_shader_needs_decompress_mask(sctx
, shader
);
2046 si_update_all_resident_texture_descriptors(sctx
);
2047 si_update_ps_colorbuf0_slot(sctx
);
2050 /* SHADER USER DATA */
2052 static void si_mark_shader_pointers_dirty(struct si_context
*sctx
,
2055 sctx
->shader_pointers_dirty
|=
2056 u_bit_consecutive(SI_DESCS_FIRST_SHADER
+ shader
* SI_NUM_SHADER_DESCS
,
2057 SI_NUM_SHADER_DESCS
);
2059 if (shader
== PIPE_SHADER_VERTEX
)
2060 sctx
->vertex_buffer_pointer_dirty
= sctx
->vb_descriptors_buffer
!= NULL
;
2062 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.shader_pointers
);
2065 static void si_shader_pointers_begin_new_cs(struct si_context
*sctx
)
2067 sctx
->shader_pointers_dirty
= u_bit_consecutive(0, SI_NUM_DESCS
);
2068 sctx
->vertex_buffer_pointer_dirty
= sctx
->vb_descriptors_buffer
!= NULL
;
2069 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.shader_pointers
);
2070 sctx
->graphics_bindless_pointer_dirty
= sctx
->bindless_descriptors
.buffer
!= NULL
;
2071 sctx
->compute_bindless_pointer_dirty
= sctx
->bindless_descriptors
.buffer
!= NULL
;
2074 /* Set a base register address for user data constants in the given shader.
2075 * This assigns a mapping from PIPE_SHADER_* to SPI_SHADER_USER_DATA_*.
2077 static void si_set_user_data_base(struct si_context
*sctx
,
2078 unsigned shader
, uint32_t new_base
)
2080 uint32_t *base
= &sctx
->shader_pointers
.sh_base
[shader
];
2082 if (*base
!= new_base
) {
2086 si_mark_shader_pointers_dirty(sctx
, shader
);
2088 /* Any change in enabled shader stages requires re-emitting
2089 * the VS state SGPR, because it contains the clamp_vertex_color
2090 * state, which can be done in VS, TES, and GS.
2092 sctx
->last_vs_state
= ~0;
2096 /* This must be called when these are changed between enabled and disabled
2098 * - tessellation evaluation shader
2101 void si_shader_change_notify(struct si_context
*sctx
)
2103 /* VS can be bound as VS, ES, or LS. */
2104 if (sctx
->tes_shader
.cso
) {
2105 if (sctx
->chip_class
>= GFX10
) {
2106 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
2107 R_00B430_SPI_SHADER_USER_DATA_HS_0
);
2108 } else if (sctx
->chip_class
== GFX9
) {
2109 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
2110 R_00B430_SPI_SHADER_USER_DATA_LS_0
);
2112 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
2113 R_00B530_SPI_SHADER_USER_DATA_LS_0
);
2115 } else if (sctx
->chip_class
>= GFX10
) {
2116 if (sctx
->ngg
|| sctx
->gs_shader
.cso
) {
2117 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
2118 R_00B230_SPI_SHADER_USER_DATA_GS_0
);
2120 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
2121 R_00B130_SPI_SHADER_USER_DATA_VS_0
);
2123 } else if (sctx
->gs_shader
.cso
) {
2124 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
2125 R_00B330_SPI_SHADER_USER_DATA_ES_0
);
2127 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
2128 R_00B130_SPI_SHADER_USER_DATA_VS_0
);
2131 /* TES can be bound as ES, VS, or not bound. */
2132 if (sctx
->tes_shader
.cso
) {
2133 if (sctx
->chip_class
>= GFX10
) {
2134 if (sctx
->ngg
|| sctx
->gs_shader
.cso
) {
2135 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_EVAL
,
2136 R_00B230_SPI_SHADER_USER_DATA_GS_0
);
2138 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_EVAL
,
2139 R_00B130_SPI_SHADER_USER_DATA_VS_0
);
2141 } else if (sctx
->gs_shader
.cso
) {
2142 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_EVAL
,
2143 R_00B330_SPI_SHADER_USER_DATA_ES_0
);
2145 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_EVAL
,
2146 R_00B130_SPI_SHADER_USER_DATA_VS_0
);
2149 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_EVAL
, 0);
2153 static void si_emit_shader_pointer_head(struct radeon_cmdbuf
*cs
,
2155 unsigned pointer_count
)
2157 radeon_emit(cs
, PKT3(PKT3_SET_SH_REG
, pointer_count
, 0));
2158 radeon_emit(cs
, (sh_offset
- SI_SH_REG_OFFSET
) >> 2);
2161 static void si_emit_shader_pointer_body(struct si_screen
*sscreen
,
2162 struct radeon_cmdbuf
*cs
,
2165 radeon_emit(cs
, va
);
2167 assert(va
== 0 || (va
>> 32) == sscreen
->info
.address32_hi
);
2170 static void si_emit_shader_pointer(struct si_context
*sctx
,
2171 struct si_descriptors
*desc
,
2174 struct radeon_cmdbuf
*cs
= sctx
->gfx_cs
;
2175 unsigned sh_offset
= sh_base
+ desc
->shader_userdata_offset
;
2177 si_emit_shader_pointer_head(cs
, sh_offset
, 1);
2178 si_emit_shader_pointer_body(sctx
->screen
, cs
, desc
->gpu_address
);
2181 static void si_emit_consecutive_shader_pointers(struct si_context
*sctx
,
2182 unsigned pointer_mask
,
2188 struct radeon_cmdbuf
*cs
= sctx
->gfx_cs
;
2189 unsigned mask
= sctx
->shader_pointers_dirty
& pointer_mask
;
2193 u_bit_scan_consecutive_range(&mask
, &start
, &count
);
2195 struct si_descriptors
*descs
= &sctx
->descriptors
[start
];
2196 unsigned sh_offset
= sh_base
+ descs
->shader_userdata_offset
;
2198 si_emit_shader_pointer_head(cs
, sh_offset
, count
);
2199 for (int i
= 0; i
< count
; i
++)
2200 si_emit_shader_pointer_body(sctx
->screen
, cs
,
2201 descs
[i
].gpu_address
);
2205 static void si_emit_global_shader_pointers(struct si_context
*sctx
,
2206 struct si_descriptors
*descs
)
2208 if (sctx
->chip_class
>= GFX10
) {
2209 si_emit_shader_pointer(sctx
, descs
,
2210 R_00B030_SPI_SHADER_USER_DATA_PS_0
);
2211 /* HW VS stage only used in non-NGG mode. */
2212 si_emit_shader_pointer(sctx
, descs
,
2213 R_00B130_SPI_SHADER_USER_DATA_VS_0
);
2214 si_emit_shader_pointer(sctx
, descs
,
2215 R_00B230_SPI_SHADER_USER_DATA_GS_0
);
2216 si_emit_shader_pointer(sctx
, descs
,
2217 R_00B430_SPI_SHADER_USER_DATA_HS_0
);
2219 } else if (sctx
->chip_class
== GFX9
) {
2220 /* Broadcast it to all shader stages. */
2221 si_emit_shader_pointer(sctx
, descs
,
2222 R_00B530_SPI_SHADER_USER_DATA_COMMON_0
);
2226 si_emit_shader_pointer(sctx
, descs
,
2227 R_00B030_SPI_SHADER_USER_DATA_PS_0
);
2228 si_emit_shader_pointer(sctx
, descs
,
2229 R_00B130_SPI_SHADER_USER_DATA_VS_0
);
2230 si_emit_shader_pointer(sctx
, descs
,
2231 R_00B330_SPI_SHADER_USER_DATA_ES_0
);
2232 si_emit_shader_pointer(sctx
, descs
,
2233 R_00B230_SPI_SHADER_USER_DATA_GS_0
);
2234 si_emit_shader_pointer(sctx
, descs
,
2235 R_00B430_SPI_SHADER_USER_DATA_HS_0
);
2236 si_emit_shader_pointer(sctx
, descs
,
2237 R_00B530_SPI_SHADER_USER_DATA_LS_0
);
2240 void si_emit_graphics_shader_pointers(struct si_context
*sctx
)
2242 uint32_t *sh_base
= sctx
->shader_pointers
.sh_base
;
2244 if (sctx
->shader_pointers_dirty
& (1 << SI_DESCS_RW_BUFFERS
)) {
2245 si_emit_global_shader_pointers(sctx
,
2246 &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
]);
2249 si_emit_consecutive_shader_pointers(sctx
, SI_DESCS_SHADER_MASK(VERTEX
),
2250 sh_base
[PIPE_SHADER_VERTEX
]);
2251 si_emit_consecutive_shader_pointers(sctx
, SI_DESCS_SHADER_MASK(TESS_EVAL
),
2252 sh_base
[PIPE_SHADER_TESS_EVAL
]);
2253 si_emit_consecutive_shader_pointers(sctx
, SI_DESCS_SHADER_MASK(FRAGMENT
),
2254 sh_base
[PIPE_SHADER_FRAGMENT
]);
2255 si_emit_consecutive_shader_pointers(sctx
, SI_DESCS_SHADER_MASK(TESS_CTRL
),
2256 sh_base
[PIPE_SHADER_TESS_CTRL
]);
2257 si_emit_consecutive_shader_pointers(sctx
, SI_DESCS_SHADER_MASK(GEOMETRY
),
2258 sh_base
[PIPE_SHADER_GEOMETRY
]);
2260 sctx
->shader_pointers_dirty
&=
2261 ~u_bit_consecutive(SI_DESCS_RW_BUFFERS
, SI_DESCS_FIRST_COMPUTE
);
2263 if (sctx
->vertex_buffer_pointer_dirty
) {
2264 struct radeon_cmdbuf
*cs
= sctx
->gfx_cs
;
2266 /* Find the location of the VB descriptor pointer. */
2267 /* TODO: In the future, the pointer will be packed in unused
2268 * bits of the first 2 VB descriptors. */
2269 unsigned sh_dw_offset
= SI_VS_NUM_USER_SGPR
;
2270 if (sctx
->chip_class
>= GFX9
) {
2271 if (sctx
->tes_shader
.cso
)
2272 sh_dw_offset
= GFX9_TCS_NUM_USER_SGPR
;
2273 else if (sctx
->gs_shader
.cso
)
2274 sh_dw_offset
= GFX9_VSGS_NUM_USER_SGPR
;
2277 unsigned sh_offset
= sh_base
[PIPE_SHADER_VERTEX
] + sh_dw_offset
* 4;
2278 si_emit_shader_pointer_head(cs
, sh_offset
, 1);
2279 si_emit_shader_pointer_body(sctx
->screen
, cs
,
2280 sctx
->vb_descriptors_buffer
->gpu_address
+
2281 sctx
->vb_descriptors_offset
);
2282 sctx
->vertex_buffer_pointer_dirty
= false;
2285 if (sctx
->graphics_bindless_pointer_dirty
) {
2286 si_emit_global_shader_pointers(sctx
,
2287 &sctx
->bindless_descriptors
);
2288 sctx
->graphics_bindless_pointer_dirty
= false;
2292 void si_emit_compute_shader_pointers(struct si_context
*sctx
)
2294 unsigned base
= R_00B900_COMPUTE_USER_DATA_0
;
2296 si_emit_consecutive_shader_pointers(sctx
, SI_DESCS_SHADER_MASK(COMPUTE
),
2297 R_00B900_COMPUTE_USER_DATA_0
);
2298 sctx
->shader_pointers_dirty
&= ~SI_DESCS_SHADER_MASK(COMPUTE
);
2300 if (sctx
->compute_bindless_pointer_dirty
) {
2301 si_emit_shader_pointer(sctx
, &sctx
->bindless_descriptors
, base
);
2302 sctx
->compute_bindless_pointer_dirty
= false;
2308 static void si_init_bindless_descriptors(struct si_context
*sctx
,
2309 struct si_descriptors
*desc
,
2310 short shader_userdata_rel_index
,
2311 unsigned num_elements
)
2313 ASSERTED
unsigned desc_slot
;
2315 si_init_descriptors(desc
, shader_userdata_rel_index
, 16, num_elements
);
2316 sctx
->bindless_descriptors
.num_active_slots
= num_elements
;
2318 /* The first bindless descriptor is stored at slot 1, because 0 is not
2319 * considered to be a valid handle.
2321 sctx
->num_bindless_descriptors
= 1;
2323 /* Track which bindless slots are used (or not). */
2324 util_idalloc_init(&sctx
->bindless_used_slots
);
2325 util_idalloc_resize(&sctx
->bindless_used_slots
, num_elements
);
2327 /* Reserve slot 0 because it's an invalid handle for bindless. */
2328 desc_slot
= util_idalloc_alloc(&sctx
->bindless_used_slots
);
2329 assert(desc_slot
== 0);
2332 static void si_release_bindless_descriptors(struct si_context
*sctx
)
2334 si_release_descriptors(&sctx
->bindless_descriptors
);
2335 util_idalloc_fini(&sctx
->bindless_used_slots
);
2338 static unsigned si_get_first_free_bindless_slot(struct si_context
*sctx
)
2340 struct si_descriptors
*desc
= &sctx
->bindless_descriptors
;
2343 desc_slot
= util_idalloc_alloc(&sctx
->bindless_used_slots
);
2344 if (desc_slot
>= desc
->num_elements
) {
2345 /* The array of bindless descriptors is full, resize it. */
2346 unsigned slot_size
= desc
->element_dw_size
* 4;
2347 unsigned new_num_elements
= desc
->num_elements
* 2;
2349 desc
->list
= REALLOC(desc
->list
, desc
->num_elements
* slot_size
,
2350 new_num_elements
* slot_size
);
2351 desc
->num_elements
= new_num_elements
;
2352 desc
->num_active_slots
= new_num_elements
;
2360 si_create_bindless_descriptor(struct si_context
*sctx
, uint32_t *desc_list
,
2363 struct si_descriptors
*desc
= &sctx
->bindless_descriptors
;
2364 unsigned desc_slot
, desc_slot_offset
;
2366 /* Find a free slot. */
2367 desc_slot
= si_get_first_free_bindless_slot(sctx
);
2369 /* For simplicity, sampler and image bindless descriptors use fixed
2370 * 16-dword slots for now. Image descriptors only need 8-dword but this
2371 * doesn't really matter because no real apps use image handles.
2373 desc_slot_offset
= desc_slot
* 16;
2375 /* Copy the descriptor into the array. */
2376 memcpy(desc
->list
+ desc_slot_offset
, desc_list
, size
);
2378 /* Re-upload the whole array of bindless descriptors into a new buffer.
2380 if (!si_upload_descriptors(sctx
, desc
))
2383 /* Make sure to re-emit the shader pointers for all stages. */
2384 sctx
->graphics_bindless_pointer_dirty
= true;
2385 sctx
->compute_bindless_pointer_dirty
= true;
2390 static void si_update_bindless_buffer_descriptor(struct si_context
*sctx
,
2392 struct pipe_resource
*resource
,
2396 struct si_descriptors
*desc
= &sctx
->bindless_descriptors
;
2397 struct si_resource
*buf
= si_resource(resource
);
2398 unsigned desc_slot_offset
= desc_slot
* 16;
2399 uint32_t *desc_list
= desc
->list
+ desc_slot_offset
+ 4;
2400 uint64_t old_desc_va
;
2402 assert(resource
->target
== PIPE_BUFFER
);
2404 /* Retrieve the old buffer addr from the descriptor. */
2405 old_desc_va
= si_desc_extract_buffer_address(desc_list
);
2407 if (old_desc_va
!= buf
->gpu_address
+ offset
) {
2408 /* The buffer has been invalidated when the handle wasn't
2409 * resident, update the descriptor and the dirty flag.
2411 si_set_buf_desc_address(buf
, offset
, &desc_list
[0]);
2417 static uint64_t si_create_texture_handle(struct pipe_context
*ctx
,
2418 struct pipe_sampler_view
*view
,
2419 const struct pipe_sampler_state
*state
)
2421 struct si_sampler_view
*sview
= (struct si_sampler_view
*)view
;
2422 struct si_context
*sctx
= (struct si_context
*)ctx
;
2423 struct si_texture_handle
*tex_handle
;
2424 struct si_sampler_state
*sstate
;
2425 uint32_t desc_list
[16];
2428 tex_handle
= CALLOC_STRUCT(si_texture_handle
);
2432 memset(desc_list
, 0, sizeof(desc_list
));
2433 si_init_descriptor_list(&desc_list
[0], 16, 1, null_texture_descriptor
);
2435 sstate
= ctx
->create_sampler_state(ctx
, state
);
2441 si_set_sampler_view_desc(sctx
, sview
, sstate
, &desc_list
[0]);
2442 memcpy(&tex_handle
->sstate
, sstate
, sizeof(*sstate
));
2443 ctx
->delete_sampler_state(ctx
, sstate
);
2445 tex_handle
->desc_slot
= si_create_bindless_descriptor(sctx
, desc_list
,
2447 if (!tex_handle
->desc_slot
) {
2452 handle
= tex_handle
->desc_slot
;
2454 if (!_mesa_hash_table_insert(sctx
->tex_handles
,
2455 (void *)(uintptr_t)handle
,
2461 pipe_sampler_view_reference(&tex_handle
->view
, view
);
2463 si_resource(sview
->base
.texture
)->texture_handle_allocated
= true;
2468 static void si_delete_texture_handle(struct pipe_context
*ctx
, uint64_t handle
)
2470 struct si_context
*sctx
= (struct si_context
*)ctx
;
2471 struct si_texture_handle
*tex_handle
;
2472 struct hash_entry
*entry
;
2474 entry
= _mesa_hash_table_search(sctx
->tex_handles
,
2475 (void *)(uintptr_t)handle
);
2479 tex_handle
= (struct si_texture_handle
*)entry
->data
;
2481 /* Allow this descriptor slot to be re-used. */
2482 util_idalloc_free(&sctx
->bindless_used_slots
, tex_handle
->desc_slot
);
2484 pipe_sampler_view_reference(&tex_handle
->view
, NULL
);
2485 _mesa_hash_table_remove(sctx
->tex_handles
, entry
);
2489 static void si_make_texture_handle_resident(struct pipe_context
*ctx
,
2490 uint64_t handle
, bool resident
)
2492 struct si_context
*sctx
= (struct si_context
*)ctx
;
2493 struct si_texture_handle
*tex_handle
;
2494 struct si_sampler_view
*sview
;
2495 struct hash_entry
*entry
;
2497 entry
= _mesa_hash_table_search(sctx
->tex_handles
,
2498 (void *)(uintptr_t)handle
);
2502 tex_handle
= (struct si_texture_handle
*)entry
->data
;
2503 sview
= (struct si_sampler_view
*)tex_handle
->view
;
2506 if (sview
->base
.texture
->target
!= PIPE_BUFFER
) {
2507 struct si_texture
*tex
=
2508 (struct si_texture
*)sview
->base
.texture
;
2510 if (depth_needs_decompression(tex
)) {
2511 util_dynarray_append(
2512 &sctx
->resident_tex_needs_depth_decompress
,
2513 struct si_texture_handle
*,
2517 if (color_needs_decompression(tex
)) {
2518 util_dynarray_append(
2519 &sctx
->resident_tex_needs_color_decompress
,
2520 struct si_texture_handle
*,
2524 if (tex
->surface
.dcc_offset
&&
2525 p_atomic_read(&tex
->framebuffers_bound
))
2526 sctx
->need_check_render_feedback
= true;
2528 si_update_bindless_texture_descriptor(sctx
, tex_handle
);
2530 si_update_bindless_buffer_descriptor(sctx
,
2531 tex_handle
->desc_slot
,
2532 sview
->base
.texture
,
2533 sview
->base
.u
.buf
.offset
,
2534 &tex_handle
->desc_dirty
);
2537 /* Re-upload the descriptor if it has been updated while it
2540 if (tex_handle
->desc_dirty
)
2541 sctx
->bindless_descriptors_dirty
= true;
2543 /* Add the texture handle to the per-context list. */
2544 util_dynarray_append(&sctx
->resident_tex_handles
,
2545 struct si_texture_handle
*, tex_handle
);
2547 /* Add the buffers to the current CS in case si_begin_new_cs()
2548 * is not going to be called.
2550 si_sampler_view_add_buffer(sctx
, sview
->base
.texture
,
2552 sview
->is_stencil_sampler
, false);
2554 /* Remove the texture handle from the per-context list. */
2555 util_dynarray_delete_unordered(&sctx
->resident_tex_handles
,
2556 struct si_texture_handle
*,
2559 if (sview
->base
.texture
->target
!= PIPE_BUFFER
) {
2560 util_dynarray_delete_unordered(
2561 &sctx
->resident_tex_needs_depth_decompress
,
2562 struct si_texture_handle
*, tex_handle
);
2564 util_dynarray_delete_unordered(
2565 &sctx
->resident_tex_needs_color_decompress
,
2566 struct si_texture_handle
*, tex_handle
);
2571 static uint64_t si_create_image_handle(struct pipe_context
*ctx
,
2572 const struct pipe_image_view
*view
)
2574 struct si_context
*sctx
= (struct si_context
*)ctx
;
2575 struct si_image_handle
*img_handle
;
2576 uint32_t desc_list
[16];
2579 if (!view
|| !view
->resource
)
2582 img_handle
= CALLOC_STRUCT(si_image_handle
);
2586 memset(desc_list
, 0, sizeof(desc_list
));
2587 si_init_descriptor_list(&desc_list
[0], 8, 2, null_image_descriptor
);
2589 si_set_shader_image_desc(sctx
, view
, false, &desc_list
[0], &desc_list
[8]);
2591 img_handle
->desc_slot
= si_create_bindless_descriptor(sctx
, desc_list
,
2593 if (!img_handle
->desc_slot
) {
2598 handle
= img_handle
->desc_slot
;
2600 if (!_mesa_hash_table_insert(sctx
->img_handles
,
2601 (void *)(uintptr_t)handle
,
2607 util_copy_image_view(&img_handle
->view
, view
);
2609 si_resource(view
->resource
)->image_handle_allocated
= true;
2614 static void si_delete_image_handle(struct pipe_context
*ctx
, uint64_t handle
)
2616 struct si_context
*sctx
= (struct si_context
*)ctx
;
2617 struct si_image_handle
*img_handle
;
2618 struct hash_entry
*entry
;
2620 entry
= _mesa_hash_table_search(sctx
->img_handles
,
2621 (void *)(uintptr_t)handle
);
2625 img_handle
= (struct si_image_handle
*)entry
->data
;
2627 util_copy_image_view(&img_handle
->view
, NULL
);
2628 _mesa_hash_table_remove(sctx
->img_handles
, entry
);
2632 static void si_make_image_handle_resident(struct pipe_context
*ctx
,
2633 uint64_t handle
, unsigned access
,
2636 struct si_context
*sctx
= (struct si_context
*)ctx
;
2637 struct si_image_handle
*img_handle
;
2638 struct pipe_image_view
*view
;
2639 struct si_resource
*res
;
2640 struct hash_entry
*entry
;
2642 entry
= _mesa_hash_table_search(sctx
->img_handles
,
2643 (void *)(uintptr_t)handle
);
2647 img_handle
= (struct si_image_handle
*)entry
->data
;
2648 view
= &img_handle
->view
;
2649 res
= si_resource(view
->resource
);
2652 if (res
->b
.b
.target
!= PIPE_BUFFER
) {
2653 struct si_texture
*tex
= (struct si_texture
*)res
;
2654 unsigned level
= view
->u
.tex
.level
;
2656 if (color_needs_decompression(tex
)) {
2657 util_dynarray_append(
2658 &sctx
->resident_img_needs_color_decompress
,
2659 struct si_image_handle
*,
2663 if (vi_dcc_enabled(tex
, level
) &&
2664 p_atomic_read(&tex
->framebuffers_bound
))
2665 sctx
->need_check_render_feedback
= true;
2667 si_update_bindless_image_descriptor(sctx
, img_handle
);
2669 si_update_bindless_buffer_descriptor(sctx
,
2670 img_handle
->desc_slot
,
2673 &img_handle
->desc_dirty
);
2676 /* Re-upload the descriptor if it has been updated while it
2679 if (img_handle
->desc_dirty
)
2680 sctx
->bindless_descriptors_dirty
= true;
2682 /* Add the image handle to the per-context list. */
2683 util_dynarray_append(&sctx
->resident_img_handles
,
2684 struct si_image_handle
*, img_handle
);
2686 /* Add the buffers to the current CS in case si_begin_new_cs()
2687 * is not going to be called.
2689 si_sampler_view_add_buffer(sctx
, view
->resource
,
2690 (access
& PIPE_IMAGE_ACCESS_WRITE
) ?
2691 RADEON_USAGE_READWRITE
:
2692 RADEON_USAGE_READ
, false, false);
2694 /* Remove the image handle from the per-context list. */
2695 util_dynarray_delete_unordered(&sctx
->resident_img_handles
,
2696 struct si_image_handle
*,
2699 if (res
->b
.b
.target
!= PIPE_BUFFER
) {
2700 util_dynarray_delete_unordered(
2701 &sctx
->resident_img_needs_color_decompress
,
2702 struct si_image_handle
*,
2708 static void si_resident_buffers_add_all_to_bo_list(struct si_context
*sctx
)
2710 unsigned num_resident_tex_handles
, num_resident_img_handles
;
2712 num_resident_tex_handles
= sctx
->resident_tex_handles
.size
/
2713 sizeof(struct si_texture_handle
*);
2714 num_resident_img_handles
= sctx
->resident_img_handles
.size
/
2715 sizeof(struct si_image_handle
*);
2717 /* Add all resident texture handles. */
2718 util_dynarray_foreach(&sctx
->resident_tex_handles
,
2719 struct si_texture_handle
*, tex_handle
) {
2720 struct si_sampler_view
*sview
=
2721 (struct si_sampler_view
*)(*tex_handle
)->view
;
2723 si_sampler_view_add_buffer(sctx
, sview
->base
.texture
,
2725 sview
->is_stencil_sampler
, false);
2728 /* Add all resident image handles. */
2729 util_dynarray_foreach(&sctx
->resident_img_handles
,
2730 struct si_image_handle
*, img_handle
) {
2731 struct pipe_image_view
*view
= &(*img_handle
)->view
;
2733 si_sampler_view_add_buffer(sctx
, view
->resource
,
2734 RADEON_USAGE_READWRITE
,
2738 sctx
->num_resident_handles
+= num_resident_tex_handles
+
2739 num_resident_img_handles
;
2740 assert(sctx
->bo_list_add_all_resident_resources
);
2741 sctx
->bo_list_add_all_resident_resources
= false;
2744 /* INIT/DEINIT/UPLOAD */
2746 void si_init_all_descriptors(struct si_context
*sctx
)
2749 unsigned first_shader
=
2750 sctx
->has_graphics
? 0 : PIPE_SHADER_COMPUTE
;
2752 for (i
= first_shader
; i
< SI_NUM_SHADERS
; i
++) {
2753 bool is_2nd
= sctx
->chip_class
>= GFX9
&&
2754 (i
== PIPE_SHADER_TESS_CTRL
||
2755 i
== PIPE_SHADER_GEOMETRY
);
2756 unsigned num_sampler_slots
= SI_NUM_IMAGE_SLOTS
/ 2 + SI_NUM_SAMPLERS
;
2757 unsigned num_buffer_slots
= SI_NUM_SHADER_BUFFERS
+ SI_NUM_CONST_BUFFERS
;
2759 struct si_descriptors
*desc
;
2762 if (i
== PIPE_SHADER_TESS_CTRL
) {
2763 rel_dw_offset
= (R_00B408_SPI_SHADER_USER_DATA_ADDR_LO_HS
-
2764 R_00B430_SPI_SHADER_USER_DATA_LS_0
) / 4;
2765 } else if (sctx
->chip_class
>= GFX10
) { /* PIPE_SHADER_GEOMETRY */
2766 rel_dw_offset
= (R_00B208_SPI_SHADER_USER_DATA_ADDR_LO_GS
-
2767 R_00B230_SPI_SHADER_USER_DATA_GS_0
) / 4;
2769 rel_dw_offset
= (R_00B208_SPI_SHADER_USER_DATA_ADDR_LO_GS
-
2770 R_00B330_SPI_SHADER_USER_DATA_ES_0
) / 4;
2773 rel_dw_offset
= SI_SGPR_CONST_AND_SHADER_BUFFERS
;
2775 desc
= si_const_and_shader_buffer_descriptors(sctx
, i
);
2776 si_init_buffer_resources(&sctx
->const_and_shader_buffers
[i
], desc
,
2777 num_buffer_slots
, rel_dw_offset
,
2778 RADEON_PRIO_SHADER_RW_BUFFER
,
2779 RADEON_PRIO_CONST_BUFFER
);
2780 desc
->slot_index_to_bind_directly
= si_get_constbuf_slot(0);
2783 if (i
== PIPE_SHADER_TESS_CTRL
) {
2784 rel_dw_offset
= (R_00B40C_SPI_SHADER_USER_DATA_ADDR_HI_HS
-
2785 R_00B430_SPI_SHADER_USER_DATA_LS_0
) / 4;
2786 } else if (sctx
->chip_class
>= GFX10
) { /* PIPE_SHADER_GEOMETRY */
2787 rel_dw_offset
= (R_00B20C_SPI_SHADER_USER_DATA_ADDR_HI_GS
-
2788 R_00B230_SPI_SHADER_USER_DATA_GS_0
) / 4;
2790 rel_dw_offset
= (R_00B20C_SPI_SHADER_USER_DATA_ADDR_HI_GS
-
2791 R_00B330_SPI_SHADER_USER_DATA_ES_0
) / 4;
2794 rel_dw_offset
= SI_SGPR_SAMPLERS_AND_IMAGES
;
2797 desc
= si_sampler_and_image_descriptors(sctx
, i
);
2798 si_init_descriptors(desc
, rel_dw_offset
, 16, num_sampler_slots
);
2801 for (j
= 0; j
< SI_NUM_IMAGE_SLOTS
; j
++)
2802 memcpy(desc
->list
+ j
* 8, null_image_descriptor
, 8 * 4);
2803 for (; j
< SI_NUM_IMAGE_SLOTS
+ SI_NUM_SAMPLERS
* 2; j
++)
2804 memcpy(desc
->list
+ j
* 8, null_texture_descriptor
, 8 * 4);
2807 si_init_buffer_resources(&sctx
->rw_buffers
,
2808 &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
],
2809 SI_NUM_RW_BUFFERS
, SI_SGPR_RW_BUFFERS
,
2810 /* The second priority is used by
2811 * const buffers in RW buffer slots. */
2812 RADEON_PRIO_SHADER_RINGS
, RADEON_PRIO_CONST_BUFFER
);
2813 sctx
->descriptors
[SI_DESCS_RW_BUFFERS
].num_active_slots
= SI_NUM_RW_BUFFERS
;
2815 /* Initialize an array of 1024 bindless descriptors, when the limit is
2816 * reached, just make it larger and re-upload the whole array.
2818 si_init_bindless_descriptors(sctx
, &sctx
->bindless_descriptors
,
2819 SI_SGPR_BINDLESS_SAMPLERS_AND_IMAGES
,
2822 sctx
->descriptors_dirty
= u_bit_consecutive(0, SI_NUM_DESCS
);
2824 /* Set pipe_context functions. */
2825 sctx
->b
.bind_sampler_states
= si_bind_sampler_states
;
2826 sctx
->b
.set_shader_images
= si_set_shader_images
;
2827 sctx
->b
.set_constant_buffer
= si_pipe_set_constant_buffer
;
2828 sctx
->b
.set_shader_buffers
= si_set_shader_buffers
;
2829 sctx
->b
.set_sampler_views
= si_set_sampler_views
;
2830 sctx
->b
.create_texture_handle
= si_create_texture_handle
;
2831 sctx
->b
.delete_texture_handle
= si_delete_texture_handle
;
2832 sctx
->b
.make_texture_handle_resident
= si_make_texture_handle_resident
;
2833 sctx
->b
.create_image_handle
= si_create_image_handle
;
2834 sctx
->b
.delete_image_handle
= si_delete_image_handle
;
2835 sctx
->b
.make_image_handle_resident
= si_make_image_handle_resident
;
2837 if (!sctx
->has_graphics
)
2840 sctx
->b
.set_polygon_stipple
= si_set_polygon_stipple
;
2842 /* Shader user data. */
2843 sctx
->atoms
.s
.shader_pointers
.emit
= si_emit_graphics_shader_pointers
;
2845 /* Set default and immutable mappings. */
2847 assert(sctx
->chip_class
>= GFX10
);
2848 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
, R_00B230_SPI_SHADER_USER_DATA_GS_0
);
2850 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
, R_00B130_SPI_SHADER_USER_DATA_VS_0
);
2853 if (sctx
->chip_class
== GFX9
) {
2854 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_CTRL
,
2855 R_00B430_SPI_SHADER_USER_DATA_LS_0
);
2856 si_set_user_data_base(sctx
, PIPE_SHADER_GEOMETRY
,
2857 R_00B330_SPI_SHADER_USER_DATA_ES_0
);
2859 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_CTRL
,
2860 R_00B430_SPI_SHADER_USER_DATA_HS_0
);
2861 si_set_user_data_base(sctx
, PIPE_SHADER_GEOMETRY
,
2862 R_00B230_SPI_SHADER_USER_DATA_GS_0
);
2864 si_set_user_data_base(sctx
, PIPE_SHADER_FRAGMENT
, R_00B030_SPI_SHADER_USER_DATA_PS_0
);
2867 static bool si_upload_shader_descriptors(struct si_context
*sctx
, unsigned mask
)
2869 unsigned dirty
= sctx
->descriptors_dirty
& mask
;
2871 /* Assume nothing will go wrong: */
2872 sctx
->shader_pointers_dirty
|= dirty
;
2875 unsigned i
= u_bit_scan(&dirty
);
2877 if (!si_upload_descriptors(sctx
, &sctx
->descriptors
[i
]))
2881 sctx
->descriptors_dirty
&= ~mask
;
2883 si_upload_bindless_descriptors(sctx
);
2888 bool si_upload_graphics_shader_descriptors(struct si_context
*sctx
)
2890 const unsigned mask
= u_bit_consecutive(0, SI_DESCS_FIRST_COMPUTE
);
2891 return si_upload_shader_descriptors(sctx
, mask
);
2894 bool si_upload_compute_shader_descriptors(struct si_context
*sctx
)
2896 /* Does not update rw_buffers as that is not needed for compute shaders
2897 * and the input buffer is using the same SGPR's anyway.
2899 const unsigned mask
= u_bit_consecutive(SI_DESCS_FIRST_COMPUTE
,
2900 SI_NUM_DESCS
- SI_DESCS_FIRST_COMPUTE
);
2901 return si_upload_shader_descriptors(sctx
, mask
);
2904 void si_release_all_descriptors(struct si_context
*sctx
)
2908 for (i
= 0; i
< SI_NUM_SHADERS
; i
++) {
2909 si_release_buffer_resources(&sctx
->const_and_shader_buffers
[i
],
2910 si_const_and_shader_buffer_descriptors(sctx
, i
));
2911 si_release_sampler_views(&sctx
->samplers
[i
]);
2912 si_release_image_views(&sctx
->images
[i
]);
2914 si_release_buffer_resources(&sctx
->rw_buffers
,
2915 &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
]);
2916 for (i
= 0; i
< SI_NUM_VERTEX_BUFFERS
; i
++)
2917 pipe_vertex_buffer_unreference(&sctx
->vertex_buffer
[i
]);
2919 for (i
= 0; i
< SI_NUM_DESCS
; ++i
)
2920 si_release_descriptors(&sctx
->descriptors
[i
]);
2922 si_resource_reference(&sctx
->vb_descriptors_buffer
, NULL
);
2923 sctx
->vb_descriptors_gpu_list
= NULL
; /* points into a mapped buffer */
2925 si_release_bindless_descriptors(sctx
);
2928 void si_gfx_resources_add_all_to_bo_list(struct si_context
*sctx
)
2930 for (unsigned i
= 0; i
< SI_NUM_GRAPHICS_SHADERS
; i
++) {
2931 si_buffer_resources_begin_new_cs(sctx
, &sctx
->const_and_shader_buffers
[i
]);
2932 si_sampler_views_begin_new_cs(sctx
, &sctx
->samplers
[i
]);
2933 si_image_views_begin_new_cs(sctx
, &sctx
->images
[i
]);
2935 si_buffer_resources_begin_new_cs(sctx
, &sctx
->rw_buffers
);
2936 si_vertex_buffers_begin_new_cs(sctx
);
2938 if (sctx
->bo_list_add_all_resident_resources
)
2939 si_resident_buffers_add_all_to_bo_list(sctx
);
2941 assert(sctx
->bo_list_add_all_gfx_resources
);
2942 sctx
->bo_list_add_all_gfx_resources
= false;
2945 void si_compute_resources_add_all_to_bo_list(struct si_context
*sctx
)
2947 unsigned sh
= PIPE_SHADER_COMPUTE
;
2949 si_buffer_resources_begin_new_cs(sctx
, &sctx
->const_and_shader_buffers
[sh
]);
2950 si_sampler_views_begin_new_cs(sctx
, &sctx
->samplers
[sh
]);
2951 si_image_views_begin_new_cs(sctx
, &sctx
->images
[sh
]);
2952 si_buffer_resources_begin_new_cs(sctx
, &sctx
->rw_buffers
);
2954 if (sctx
->bo_list_add_all_resident_resources
)
2955 si_resident_buffers_add_all_to_bo_list(sctx
);
2957 assert(sctx
->bo_list_add_all_compute_resources
);
2958 sctx
->bo_list_add_all_compute_resources
= false;
2961 void si_all_descriptors_begin_new_cs(struct si_context
*sctx
)
2963 for (unsigned i
= 0; i
< SI_NUM_DESCS
; ++i
)
2964 si_descriptors_begin_new_cs(sctx
, &sctx
->descriptors
[i
]);
2965 si_descriptors_begin_new_cs(sctx
, &sctx
->bindless_descriptors
);
2967 si_shader_pointers_begin_new_cs(sctx
);
2969 sctx
->bo_list_add_all_resident_resources
= true;
2970 sctx
->bo_list_add_all_gfx_resources
= true;
2971 sctx
->bo_list_add_all_compute_resources
= true;
2974 void si_set_active_descriptors(struct si_context
*sctx
, unsigned desc_idx
,
2975 uint64_t new_active_mask
)
2977 struct si_descriptors
*desc
= &sctx
->descriptors
[desc_idx
];
2979 /* Ignore no-op updates and updates that disable all slots. */
2980 if (!new_active_mask
||
2981 new_active_mask
== u_bit_consecutive64(desc
->first_active_slot
,
2982 desc
->num_active_slots
))
2986 u_bit_scan_consecutive_range64(&new_active_mask
, &first
, &count
);
2987 assert(new_active_mask
== 0);
2989 /* Upload/dump descriptors if slots are being enabled. */
2990 if (first
< desc
->first_active_slot
||
2991 first
+ count
> desc
->first_active_slot
+ desc
->num_active_slots
)
2992 sctx
->descriptors_dirty
|= 1u << desc_idx
;
2994 desc
->first_active_slot
= first
;
2995 desc
->num_active_slots
= count
;
2998 void si_set_active_descriptors_for_shader(struct si_context
*sctx
,
2999 struct si_shader_selector
*sel
)
3004 si_set_active_descriptors(sctx
,
3005 si_const_and_shader_buffer_descriptors_idx(sel
->type
),
3006 sel
->active_const_and_shader_buffers
);
3007 si_set_active_descriptors(sctx
,
3008 si_sampler_and_image_descriptors_idx(sel
->type
),
3009 sel
->active_samplers_and_images
);