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.
60 #include "util/hash_table.h"
61 #include "util/u_idalloc.h"
62 #include "util/u_format.h"
63 #include "util/u_memory.h"
64 #include "util/u_upload_mgr.h"
67 /* NULL image and buffer descriptor for textures (alpha = 1) and images
70 * For images, all fields must be zero except for the swizzle, which
71 * supports arbitrary combinations of 0s and 1s. The texture type must be
72 * any valid type (e.g. 1D). If the texture type isn't set, the hw hangs.
74 * For buffers, all fields must be zero. If they are not, the hw hangs.
76 * This is the only reason why the buffer descriptor must be in words [4:7].
78 static uint32_t null_texture_descriptor
[8] = {
82 S_008F1C_DST_SEL_W(V_008F1C_SQ_SEL_1
) |
83 S_008F1C_TYPE(V_008F1C_SQ_RSRC_IMG_1D
)
84 /* the rest must contain zeros, which is also used by the buffer
88 static uint32_t null_image_descriptor
[8] = {
92 S_008F1C_TYPE(V_008F1C_SQ_RSRC_IMG_1D
)
93 /* the rest must contain zeros, which is also used by the buffer
97 static uint64_t si_desc_extract_buffer_address(const uint32_t *desc
)
99 uint64_t va
= desc
[0] |
100 ((uint64_t)G_008F04_BASE_ADDRESS_HI(desc
[1]) << 32);
102 /* Sign-extend the 48-bit address. */
104 va
= (int64_t)va
>> 16;
108 static void si_init_descriptor_list(uint32_t *desc_list
,
109 unsigned element_dw_size
,
110 unsigned num_elements
,
111 const uint32_t *null_descriptor
)
115 /* Initialize the array to NULL descriptors if the element size is 8. */
116 if (null_descriptor
) {
117 assert(element_dw_size
% 8 == 0);
118 for (i
= 0; i
< num_elements
* element_dw_size
/ 8; i
++)
119 memcpy(desc_list
+ i
* 8, null_descriptor
, 8 * 4);
123 static void si_init_descriptors(struct si_descriptors
*desc
,
124 short shader_userdata_rel_index
,
125 unsigned element_dw_size
,
126 unsigned num_elements
)
128 desc
->list
= CALLOC(num_elements
, element_dw_size
* 4);
129 desc
->element_dw_size
= element_dw_size
;
130 desc
->num_elements
= num_elements
;
131 desc
->shader_userdata_offset
= shader_userdata_rel_index
* 4;
132 desc
->slot_index_to_bind_directly
= -1;
135 static void si_release_descriptors(struct si_descriptors
*desc
)
137 si_resource_reference(&desc
->buffer
, NULL
);
141 static bool si_upload_descriptors(struct si_context
*sctx
,
142 struct si_descriptors
*desc
)
144 unsigned slot_size
= desc
->element_dw_size
* 4;
145 unsigned first_slot_offset
= desc
->first_active_slot
* slot_size
;
146 unsigned upload_size
= desc
->num_active_slots
* slot_size
;
148 /* Skip the upload if no shader is using the descriptors. dirty_mask
149 * will stay dirty and the descriptors will be uploaded when there is
150 * a shader using them.
155 /* If there is just one active descriptor, bind it directly. */
156 if ((int)desc
->first_active_slot
== desc
->slot_index_to_bind_directly
&&
157 desc
->num_active_slots
== 1) {
158 uint32_t *descriptor
= &desc
->list
[desc
->slot_index_to_bind_directly
*
159 desc
->element_dw_size
];
161 /* The buffer is already in the buffer list. */
162 si_resource_reference(&desc
->buffer
, NULL
);
163 desc
->gpu_list
= NULL
;
164 desc
->gpu_address
= si_desc_extract_buffer_address(descriptor
);
165 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.shader_pointers
);
170 unsigned buffer_offset
;
171 u_upload_alloc(sctx
->b
.const_uploader
, first_slot_offset
, upload_size
,
172 si_optimal_tcc_alignment(sctx
, upload_size
),
173 &buffer_offset
, (struct pipe_resource
**)&desc
->buffer
,
176 desc
->gpu_address
= 0;
177 return false; /* skip the draw call */
180 util_memcpy_cpu_to_le32(ptr
, (char*)desc
->list
+ first_slot_offset
,
182 desc
->gpu_list
= ptr
- first_slot_offset
/ 4;
184 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
, desc
->buffer
,
185 RADEON_USAGE_READ
, RADEON_PRIO_DESCRIPTORS
);
187 /* The shader pointer should point to slot 0. */
188 buffer_offset
-= first_slot_offset
;
189 desc
->gpu_address
= desc
->buffer
->gpu_address
+ buffer_offset
;
191 assert(desc
->buffer
->flags
& RADEON_FLAG_32BIT
);
192 assert((desc
->buffer
->gpu_address
>> 32) == sctx
->screen
->info
.address32_hi
);
193 assert((desc
->gpu_address
>> 32) == sctx
->screen
->info
.address32_hi
);
195 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.shader_pointers
);
200 si_descriptors_begin_new_cs(struct si_context
*sctx
, struct si_descriptors
*desc
)
205 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
, desc
->buffer
,
206 RADEON_USAGE_READ
, RADEON_PRIO_DESCRIPTORS
);
211 static inline enum radeon_bo_priority
212 si_get_sampler_view_priority(struct si_resource
*res
)
214 if (res
->b
.b
.target
== PIPE_BUFFER
)
215 return RADEON_PRIO_SAMPLER_BUFFER
;
217 if (res
->b
.b
.nr_samples
> 1)
218 return RADEON_PRIO_SAMPLER_TEXTURE_MSAA
;
220 return RADEON_PRIO_SAMPLER_TEXTURE
;
224 si_sampler_and_image_descriptors_idx(unsigned shader
)
226 return SI_DESCS_FIRST_SHADER
+ shader
* SI_NUM_SHADER_DESCS
+
227 SI_SHADER_DESCS_SAMPLERS_AND_IMAGES
;
230 static struct si_descriptors
*
231 si_sampler_and_image_descriptors(struct si_context
*sctx
, unsigned shader
)
233 return &sctx
->descriptors
[si_sampler_and_image_descriptors_idx(shader
)];
236 static void si_release_sampler_views(struct si_samplers
*samplers
)
240 for (i
= 0; i
< ARRAY_SIZE(samplers
->views
); i
++) {
241 pipe_sampler_view_reference(&samplers
->views
[i
], NULL
);
245 static void si_sampler_view_add_buffer(struct si_context
*sctx
,
246 struct pipe_resource
*resource
,
247 enum radeon_bo_usage usage
,
248 bool is_stencil_sampler
,
251 struct si_texture
*tex
= (struct si_texture
*)resource
;
252 enum radeon_bo_priority priority
;
257 /* Use the flushed depth texture if direct sampling is unsupported. */
258 if (resource
->target
!= PIPE_BUFFER
&&
259 tex
->is_depth
&& !si_can_sample_zs(tex
, is_stencil_sampler
))
260 tex
= tex
->flushed_depth_texture
;
262 priority
= si_get_sampler_view_priority(&tex
->buffer
);
263 radeon_add_to_gfx_buffer_list_check_mem(sctx
, &tex
->buffer
, usage
, priority
,
266 if (resource
->target
== PIPE_BUFFER
)
269 /* Add separate DCC. */
270 if (tex
->dcc_separate_buffer
) {
271 radeon_add_to_gfx_buffer_list_check_mem(sctx
, tex
->dcc_separate_buffer
,
272 usage
, RADEON_PRIO_SEPARATE_META
, check_mem
);
276 static void si_sampler_views_begin_new_cs(struct si_context
*sctx
,
277 struct si_samplers
*samplers
)
279 unsigned mask
= samplers
->enabled_mask
;
281 /* Add buffers to the CS. */
283 int i
= u_bit_scan(&mask
);
284 struct si_sampler_view
*sview
= (struct si_sampler_view
*)samplers
->views
[i
];
286 si_sampler_view_add_buffer(sctx
, sview
->base
.texture
,
288 sview
->is_stencil_sampler
, false);
292 /* Set buffer descriptor fields that can be changed by reallocations. */
293 static void si_set_buf_desc_address(struct si_resource
*buf
,
294 uint64_t offset
, uint32_t *state
)
296 uint64_t va
= buf
->gpu_address
+ offset
;
299 state
[1] &= C_008F04_BASE_ADDRESS_HI
;
300 state
[1] |= S_008F04_BASE_ADDRESS_HI(va
>> 32);
303 /* Set texture descriptor fields that can be changed by reallocations.
306 * \param base_level_info information of the level of BASE_ADDRESS
307 * \param base_level the level of BASE_ADDRESS
308 * \param first_level pipe_sampler_view.u.tex.first_level
309 * \param block_width util_format_get_blockwidth()
310 * \param is_stencil select between separate Z & Stencil
311 * \param state descriptor to update
313 void si_set_mutable_tex_desc_fields(struct si_screen
*sscreen
,
314 struct si_texture
*tex
,
315 const struct legacy_surf_level
*base_level_info
,
316 unsigned base_level
, unsigned first_level
,
317 unsigned block_width
, bool is_stencil
,
320 uint64_t va
, meta_va
= 0;
322 if (tex
->is_depth
&& !si_can_sample_zs(tex
, is_stencil
)) {
323 tex
= tex
->flushed_depth_texture
;
327 va
= tex
->buffer
.gpu_address
;
329 if (sscreen
->info
.chip_class
>= GFX9
) {
330 /* Only stencil_offset needs to be added here. */
332 va
+= tex
->surface
.u
.gfx9
.stencil_offset
;
334 va
+= tex
->surface
.u
.gfx9
.surf_offset
;
336 va
+= base_level_info
->offset
;
340 state
[1] &= C_008F14_BASE_ADDRESS_HI
;
341 state
[1] |= S_008F14_BASE_ADDRESS_HI(va
>> 40);
343 /* Only macrotiled modes can set tile swizzle.
344 * GFX9 doesn't use (legacy) base_level_info.
346 if (sscreen
->info
.chip_class
>= GFX9
||
347 base_level_info
->mode
== RADEON_SURF_MODE_2D
)
348 state
[0] |= tex
->surface
.tile_swizzle
;
350 if (sscreen
->info
.chip_class
>= VI
) {
351 state
[6] &= C_008F28_COMPRESSION_EN
;
354 if (vi_dcc_enabled(tex
, first_level
)) {
355 meta_va
= (!tex
->dcc_separate_buffer
? tex
->buffer
.gpu_address
: 0) +
358 if (sscreen
->info
.chip_class
== VI
) {
359 meta_va
+= base_level_info
->dcc_offset
;
360 assert(base_level_info
->mode
== RADEON_SURF_MODE_2D
);
363 meta_va
|= (uint32_t)tex
->surface
.tile_swizzle
<< 8;
364 } else if (vi_tc_compat_htile_enabled(tex
, first_level
)) {
365 meta_va
= tex
->buffer
.gpu_address
+ tex
->htile_offset
;
369 state
[6] |= S_008F28_COMPRESSION_EN(1);
370 state
[7] = meta_va
>> 8;
374 if (sscreen
->info
.chip_class
>= GFX9
) {
375 state
[3] &= C_008F1C_SW_MODE
;
376 state
[4] &= C_008F20_PITCH_GFX9
;
379 state
[3] |= S_008F1C_SW_MODE(tex
->surface
.u
.gfx9
.stencil
.swizzle_mode
);
380 state
[4] |= S_008F20_PITCH_GFX9(tex
->surface
.u
.gfx9
.stencil
.epitch
);
382 state
[3] |= S_008F1C_SW_MODE(tex
->surface
.u
.gfx9
.surf
.swizzle_mode
);
383 state
[4] |= S_008F20_PITCH_GFX9(tex
->surface
.u
.gfx9
.surf
.epitch
);
386 state
[5] &= C_008F24_META_DATA_ADDRESS
&
387 C_008F24_META_PIPE_ALIGNED
&
388 C_008F24_META_RB_ALIGNED
;
390 struct gfx9_surf_meta_flags meta
;
393 meta
= tex
->surface
.u
.gfx9
.dcc
;
395 meta
= tex
->surface
.u
.gfx9
.htile
;
397 state
[5] |= S_008F24_META_DATA_ADDRESS(meta_va
>> 40) |
398 S_008F24_META_PIPE_ALIGNED(meta
.pipe_aligned
) |
399 S_008F24_META_RB_ALIGNED(meta
.rb_aligned
);
403 unsigned pitch
= base_level_info
->nblk_x
* block_width
;
404 unsigned index
= si_tile_mode_index(tex
, base_level
, is_stencil
);
406 state
[3] &= C_008F1C_TILING_INDEX
;
407 state
[3] |= S_008F1C_TILING_INDEX(index
);
408 state
[4] &= C_008F20_PITCH_GFX6
;
409 state
[4] |= S_008F20_PITCH_GFX6(pitch
- 1);
413 static void si_set_sampler_state_desc(struct si_sampler_state
*sstate
,
414 struct si_sampler_view
*sview
,
415 struct si_texture
*tex
,
418 if (sview
&& sview
->is_integer
)
419 memcpy(desc
, sstate
->integer_val
, 4*4);
420 else if (tex
&& tex
->upgraded_depth
&&
421 (!sview
|| !sview
->is_stencil_sampler
))
422 memcpy(desc
, sstate
->upgraded_depth_val
, 4*4);
424 memcpy(desc
, sstate
->val
, 4*4);
427 static void si_set_sampler_view_desc(struct si_context
*sctx
,
428 struct si_sampler_view
*sview
,
429 struct si_sampler_state
*sstate
,
432 struct pipe_sampler_view
*view
= &sview
->base
;
433 struct si_texture
*tex
= (struct si_texture
*)view
->texture
;
434 bool is_buffer
= tex
->buffer
.b
.b
.target
== PIPE_BUFFER
;
436 if (unlikely(!is_buffer
&& sview
->dcc_incompatible
)) {
437 if (vi_dcc_enabled(tex
, view
->u
.tex
.first_level
))
438 if (!si_texture_disable_dcc(sctx
, tex
))
439 si_decompress_dcc(sctx
, tex
);
441 sview
->dcc_incompatible
= false;
444 assert(tex
); /* views with texture == NULL aren't supported */
445 memcpy(desc
, sview
->state
, 8*4);
448 si_set_buf_desc_address(&tex
->buffer
,
449 sview
->base
.u
.buf
.offset
,
452 bool is_separate_stencil
= tex
->db_compatible
&&
453 sview
->is_stencil_sampler
;
455 si_set_mutable_tex_desc_fields(sctx
->screen
, tex
,
456 sview
->base_level_info
,
458 sview
->base
.u
.tex
.first_level
,
464 if (!is_buffer
&& tex
->surface
.fmask_size
) {
465 memcpy(desc
+ 8, sview
->fmask_state
, 8*4);
467 /* Disable FMASK and bind sampler state in [12:15]. */
468 memcpy(desc
+ 8, null_texture_descriptor
, 4*4);
471 si_set_sampler_state_desc(sstate
, sview
,
472 is_buffer
? NULL
: tex
,
477 static bool color_needs_decompression(struct si_texture
*tex
)
479 return tex
->surface
.fmask_size
||
480 (tex
->dirty_level_mask
&&
481 (tex
->cmask_buffer
|| tex
->dcc_offset
));
484 static bool depth_needs_decompression(struct si_texture
*tex
)
486 /* If the depth/stencil texture is TC-compatible, no decompression
487 * will be done. The decompression function will only flush DB caches
488 * to make it coherent with shaders. That's necessary because the driver
489 * doesn't flush DB caches in any other case.
491 return tex
->db_compatible
;
494 static void si_set_sampler_view(struct si_context
*sctx
,
496 unsigned slot
, struct pipe_sampler_view
*view
,
497 bool disallow_early_out
)
499 struct si_samplers
*samplers
= &sctx
->samplers
[shader
];
500 struct si_sampler_view
*sview
= (struct si_sampler_view
*)view
;
501 struct si_descriptors
*descs
= si_sampler_and_image_descriptors(sctx
, shader
);
502 unsigned desc_slot
= si_get_sampler_slot(slot
);
503 uint32_t *desc
= descs
->list
+ desc_slot
* 16;
505 if (samplers
->views
[slot
] == view
&& !disallow_early_out
)
509 struct si_texture
*tex
= (struct si_texture
*)view
->texture
;
511 si_set_sampler_view_desc(sctx
, sview
,
512 samplers
->sampler_states
[slot
], desc
);
514 if (tex
->buffer
.b
.b
.target
== PIPE_BUFFER
) {
515 tex
->buffer
.bind_history
|= PIPE_BIND_SAMPLER_VIEW
;
516 samplers
->needs_depth_decompress_mask
&= ~(1u << slot
);
517 samplers
->needs_color_decompress_mask
&= ~(1u << slot
);
519 if (depth_needs_decompression(tex
)) {
520 samplers
->needs_depth_decompress_mask
|= 1u << slot
;
522 samplers
->needs_depth_decompress_mask
&= ~(1u << slot
);
524 if (color_needs_decompression(tex
)) {
525 samplers
->needs_color_decompress_mask
|= 1u << slot
;
527 samplers
->needs_color_decompress_mask
&= ~(1u << slot
);
530 if (tex
->dcc_offset
&&
531 p_atomic_read(&tex
->framebuffers_bound
))
532 sctx
->need_check_render_feedback
= true;
535 pipe_sampler_view_reference(&samplers
->views
[slot
], view
);
536 samplers
->enabled_mask
|= 1u << slot
;
538 /* Since this can flush, it must be done after enabled_mask is
540 si_sampler_view_add_buffer(sctx
, view
->texture
,
542 sview
->is_stencil_sampler
, true);
544 pipe_sampler_view_reference(&samplers
->views
[slot
], NULL
);
545 memcpy(desc
, null_texture_descriptor
, 8*4);
546 /* Only clear the lower dwords of FMASK. */
547 memcpy(desc
+ 8, null_texture_descriptor
, 4*4);
548 /* Re-set the sampler state if we are transitioning from FMASK. */
549 if (samplers
->sampler_states
[slot
])
550 si_set_sampler_state_desc(samplers
->sampler_states
[slot
], NULL
, NULL
,
553 samplers
->enabled_mask
&= ~(1u << slot
);
554 samplers
->needs_depth_decompress_mask
&= ~(1u << slot
);
555 samplers
->needs_color_decompress_mask
&= ~(1u << slot
);
558 sctx
->descriptors_dirty
|= 1u << si_sampler_and_image_descriptors_idx(shader
);
561 static void si_update_shader_needs_decompress_mask(struct si_context
*sctx
,
564 struct si_samplers
*samplers
= &sctx
->samplers
[shader
];
565 unsigned shader_bit
= 1 << shader
;
567 if (samplers
->needs_depth_decompress_mask
||
568 samplers
->needs_color_decompress_mask
||
569 sctx
->images
[shader
].needs_color_decompress_mask
)
570 sctx
->shader_needs_decompress_mask
|= shader_bit
;
572 sctx
->shader_needs_decompress_mask
&= ~shader_bit
;
575 static void si_set_sampler_views(struct pipe_context
*ctx
,
576 enum pipe_shader_type shader
, unsigned start
,
578 struct pipe_sampler_view
**views
)
580 struct si_context
*sctx
= (struct si_context
*)ctx
;
583 if (!count
|| shader
>= SI_NUM_SHADERS
)
587 for (i
= 0; i
< count
; i
++)
588 si_set_sampler_view(sctx
, shader
, start
+ i
, views
[i
], false);
590 for (i
= 0; i
< count
; i
++)
591 si_set_sampler_view(sctx
, shader
, start
+ i
, NULL
, false);
594 si_update_shader_needs_decompress_mask(sctx
, shader
);
598 si_samplers_update_needs_color_decompress_mask(struct si_samplers
*samplers
)
600 unsigned mask
= samplers
->enabled_mask
;
603 int i
= u_bit_scan(&mask
);
604 struct pipe_resource
*res
= samplers
->views
[i
]->texture
;
606 if (res
&& res
->target
!= PIPE_BUFFER
) {
607 struct si_texture
*tex
= (struct si_texture
*)res
;
609 if (color_needs_decompression(tex
)) {
610 samplers
->needs_color_decompress_mask
|= 1u << i
;
612 samplers
->needs_color_decompress_mask
&= ~(1u << i
);
621 si_release_image_views(struct si_images
*images
)
625 for (i
= 0; i
< SI_NUM_IMAGES
; ++i
) {
626 struct pipe_image_view
*view
= &images
->views
[i
];
628 pipe_resource_reference(&view
->resource
, NULL
);
633 si_image_views_begin_new_cs(struct si_context
*sctx
, struct si_images
*images
)
635 uint mask
= images
->enabled_mask
;
637 /* Add buffers to the CS. */
639 int i
= u_bit_scan(&mask
);
640 struct pipe_image_view
*view
= &images
->views
[i
];
642 assert(view
->resource
);
644 si_sampler_view_add_buffer(sctx
, view
->resource
,
645 RADEON_USAGE_READWRITE
, false, false);
650 si_disable_shader_image(struct si_context
*ctx
, unsigned shader
, unsigned slot
)
652 struct si_images
*images
= &ctx
->images
[shader
];
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
);
658 pipe_resource_reference(&images
->views
[slot
].resource
, NULL
);
659 images
->needs_color_decompress_mask
&= ~(1 << slot
);
661 memcpy(descs
->list
+ desc_slot
*8, null_image_descriptor
, 8*4);
662 images
->enabled_mask
&= ~(1u << slot
);
663 ctx
->descriptors_dirty
|= 1u << si_sampler_and_image_descriptors_idx(shader
);
668 si_mark_image_range_valid(const struct pipe_image_view
*view
)
670 struct si_resource
*res
= si_resource(view
->resource
);
672 if (res
->b
.b
.target
!= PIPE_BUFFER
)
675 util_range_add(&res
->valid_buffer_range
,
677 view
->u
.buf
.offset
+ view
->u
.buf
.size
);
680 static void si_set_shader_image_desc(struct si_context
*ctx
,
681 const struct pipe_image_view
*view
,
682 bool skip_decompress
,
683 uint32_t *desc
, uint32_t *fmask_desc
)
685 struct si_screen
*screen
= ctx
->screen
;
686 struct si_resource
*res
;
688 res
= si_resource(view
->resource
);
690 if (res
->b
.b
.target
== PIPE_BUFFER
||
691 view
->shader_access
& SI_IMAGE_ACCESS_AS_BUFFER
) {
692 if (view
->access
& PIPE_IMAGE_ACCESS_WRITE
)
693 si_mark_image_range_valid(view
);
695 si_make_buffer_descriptor(screen
, res
,
698 view
->u
.buf
.size
, desc
);
699 si_set_buf_desc_address(res
, view
->u
.buf
.offset
, desc
+ 4);
701 static const unsigned char swizzle
[4] = { 0, 1, 2, 3 };
702 struct si_texture
*tex
= (struct si_texture
*)res
;
703 unsigned level
= view
->u
.tex
.level
;
704 unsigned width
, height
, depth
, hw_level
;
705 bool uses_dcc
= vi_dcc_enabled(tex
, level
);
706 unsigned access
= view
->access
;
708 /* Clear the write flag when writes can't occur.
709 * Note that DCC_DECOMPRESS for MSAA doesn't work in some cases,
710 * so we don't wanna trigger it.
713 (!fmask_desc
&& tex
->surface
.fmask_size
!= 0)) {
714 assert(!"Z/S and MSAA image stores are not supported");
715 access
&= ~PIPE_IMAGE_ACCESS_WRITE
;
718 assert(!tex
->is_depth
);
719 assert(fmask_desc
|| tex
->surface
.fmask_size
== 0);
721 if (uses_dcc
&& !skip_decompress
&&
722 (view
->access
& PIPE_IMAGE_ACCESS_WRITE
||
723 !vi_dcc_formats_compatible(res
->b
.b
.format
, view
->format
))) {
724 /* If DCC can't be disabled, at least decompress it.
725 * The decompression is relatively cheap if the surface
726 * has been decompressed already.
728 if (!si_texture_disable_dcc(ctx
, tex
))
729 si_decompress_dcc(ctx
, tex
);
732 if (ctx
->chip_class
>= GFX9
) {
733 /* Always set the base address. The swizzle modes don't
734 * allow setting mipmap level offsets as the base.
736 width
= res
->b
.b
.width0
;
737 height
= res
->b
.b
.height0
;
738 depth
= res
->b
.b
.depth0
;
741 /* Always force the base level to the selected level.
743 * This is required for 3D textures, where otherwise
744 * selecting a single slice for non-layered bindings
745 * fails. It doesn't hurt the other targets.
747 width
= u_minify(res
->b
.b
.width0
, level
);
748 height
= u_minify(res
->b
.b
.height0
, level
);
749 depth
= u_minify(res
->b
.b
.depth0
, level
);
753 si_make_texture_descriptor(screen
, tex
,
754 false, res
->b
.b
.target
,
755 view
->format
, swizzle
,
757 view
->u
.tex
.first_layer
,
758 view
->u
.tex
.last_layer
,
759 width
, height
, depth
,
761 si_set_mutable_tex_desc_fields(screen
, tex
,
762 &tex
->surface
.u
.legacy
.level
[level
],
764 util_format_get_blockwidth(view
->format
),
769 static void si_set_shader_image(struct si_context
*ctx
,
771 unsigned slot
, const struct pipe_image_view
*view
,
772 bool skip_decompress
)
774 struct si_images
*images
= &ctx
->images
[shader
];
775 struct si_descriptors
*descs
= si_sampler_and_image_descriptors(ctx
, shader
);
776 struct si_resource
*res
;
777 unsigned desc_slot
= si_get_image_slot(slot
);
778 uint32_t *desc
= descs
->list
+ desc_slot
* 8;
780 if (!view
|| !view
->resource
) {
781 si_disable_shader_image(ctx
, shader
, slot
);
785 res
= si_resource(view
->resource
);
787 if (&images
->views
[slot
] != view
)
788 util_copy_image_view(&images
->views
[slot
], view
);
790 si_set_shader_image_desc(ctx
, view
, skip_decompress
, desc
, NULL
);
792 if (res
->b
.b
.target
== PIPE_BUFFER
||
793 view
->shader_access
& SI_IMAGE_ACCESS_AS_BUFFER
) {
794 images
->needs_color_decompress_mask
&= ~(1 << slot
);
795 res
->bind_history
|= PIPE_BIND_SHADER_IMAGE
;
797 struct si_texture
*tex
= (struct si_texture
*)res
;
798 unsigned level
= view
->u
.tex
.level
;
800 if (color_needs_decompression(tex
)) {
801 images
->needs_color_decompress_mask
|= 1 << slot
;
803 images
->needs_color_decompress_mask
&= ~(1 << slot
);
806 if (vi_dcc_enabled(tex
, level
) &&
807 p_atomic_read(&tex
->framebuffers_bound
))
808 ctx
->need_check_render_feedback
= true;
811 images
->enabled_mask
|= 1u << slot
;
812 ctx
->descriptors_dirty
|= 1u << si_sampler_and_image_descriptors_idx(shader
);
814 /* Since this can flush, it must be done after enabled_mask is updated. */
815 si_sampler_view_add_buffer(ctx
, &res
->b
.b
,
816 (view
->access
& PIPE_IMAGE_ACCESS_WRITE
) ?
817 RADEON_USAGE_READWRITE
: RADEON_USAGE_READ
,
822 si_set_shader_images(struct pipe_context
*pipe
,
823 enum pipe_shader_type shader
,
824 unsigned start_slot
, unsigned count
,
825 const struct pipe_image_view
*views
)
827 struct si_context
*ctx
= (struct si_context
*)pipe
;
830 assert(shader
< SI_NUM_SHADERS
);
835 assert(start_slot
+ count
<= SI_NUM_IMAGES
);
838 for (i
= 0, slot
= start_slot
; i
< count
; ++i
, ++slot
)
839 si_set_shader_image(ctx
, shader
, slot
, &views
[i
], false);
841 for (i
= 0, slot
= start_slot
; i
< count
; ++i
, ++slot
)
842 si_set_shader_image(ctx
, shader
, slot
, NULL
, false);
845 si_update_shader_needs_decompress_mask(ctx
, shader
);
849 si_images_update_needs_color_decompress_mask(struct si_images
*images
)
851 unsigned mask
= images
->enabled_mask
;
854 int i
= u_bit_scan(&mask
);
855 struct pipe_resource
*res
= images
->views
[i
].resource
;
857 if (res
&& res
->target
!= PIPE_BUFFER
) {
858 struct si_texture
*tex
= (struct si_texture
*)res
;
860 if (color_needs_decompression(tex
)) {
861 images
->needs_color_decompress_mask
|= 1 << i
;
863 images
->needs_color_decompress_mask
&= ~(1 << i
);
869 void si_update_ps_colorbuf0_slot(struct si_context
*sctx
)
871 struct si_buffer_resources
*buffers
= &sctx
->rw_buffers
;
872 struct si_descriptors
*descs
= &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
873 unsigned slot
= SI_PS_IMAGE_COLORBUF0
;
874 struct pipe_surface
*surf
= NULL
;
876 /* si_texture_disable_dcc can get us here again. */
877 if (sctx
->blitter
->running
)
880 /* See whether FBFETCH is used and color buffer 0 is set. */
881 if (sctx
->ps_shader
.cso
&&
882 sctx
->ps_shader
.cso
->info
.opcode_count
[TGSI_OPCODE_FBFETCH
] &&
883 sctx
->framebuffer
.state
.nr_cbufs
&&
884 sctx
->framebuffer
.state
.cbufs
[0])
885 surf
= sctx
->framebuffer
.state
.cbufs
[0];
887 /* Return if FBFETCH transitions from disabled to disabled. */
888 if (!buffers
->buffers
[slot
] && !surf
)
891 sctx
->ps_uses_fbfetch
= surf
!= NULL
;
892 si_update_ps_iter_samples(sctx
);
895 struct si_texture
*tex
= (struct si_texture
*)surf
->texture
;
896 struct pipe_image_view view
;
899 assert(!tex
->is_depth
);
901 /* Disable DCC, because the texture is used as both a sampler
904 si_texture_disable_dcc(sctx
, tex
);
906 if (tex
->buffer
.b
.b
.nr_samples
<= 1 && tex
->cmask_buffer
) {
908 assert(tex
->cmask_buffer
!= &tex
->buffer
);
909 si_eliminate_fast_color_clear(sctx
, tex
);
910 si_texture_discard_cmask(sctx
->screen
, tex
);
913 view
.resource
= surf
->texture
;
914 view
.format
= surf
->format
;
915 view
.access
= PIPE_IMAGE_ACCESS_READ
;
916 view
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
;
917 view
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
;
918 view
.u
.tex
.level
= surf
->u
.tex
.level
;
920 /* Set the descriptor. */
921 uint32_t *desc
= descs
->list
+ slot
*4;
922 memset(desc
, 0, 16 * 4);
923 si_set_shader_image_desc(sctx
, &view
, true, desc
, desc
+ 8);
925 pipe_resource_reference(&buffers
->buffers
[slot
], &tex
->buffer
.b
.b
);
926 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
,
927 &tex
->buffer
, RADEON_USAGE_READ
,
928 RADEON_PRIO_SHADER_RW_IMAGE
);
929 buffers
->enabled_mask
|= 1u << slot
;
931 /* Clear the descriptor. */
932 memset(descs
->list
+ slot
*4, 0, 8*4);
933 pipe_resource_reference(&buffers
->buffers
[slot
], NULL
);
934 buffers
->enabled_mask
&= ~(1u << slot
);
937 sctx
->descriptors_dirty
|= 1u << SI_DESCS_RW_BUFFERS
;
942 static void si_bind_sampler_states(struct pipe_context
*ctx
,
943 enum pipe_shader_type shader
,
944 unsigned start
, unsigned count
, void **states
)
946 struct si_context
*sctx
= (struct si_context
*)ctx
;
947 struct si_samplers
*samplers
= &sctx
->samplers
[shader
];
948 struct si_descriptors
*desc
= si_sampler_and_image_descriptors(sctx
, shader
);
949 struct si_sampler_state
**sstates
= (struct si_sampler_state
**)states
;
952 if (!count
|| shader
>= SI_NUM_SHADERS
)
955 for (i
= 0; i
< count
; i
++) {
956 unsigned slot
= start
+ i
;
957 unsigned desc_slot
= si_get_sampler_slot(slot
);
960 sstates
[i
] == samplers
->sampler_states
[slot
])
964 assert(sstates
[i
]->magic
== SI_SAMPLER_STATE_MAGIC
);
966 samplers
->sampler_states
[slot
] = sstates
[i
];
968 /* If FMASK is bound, don't overwrite it.
969 * The sampler state will be set after FMASK is unbound.
971 struct si_sampler_view
*sview
=
972 (struct si_sampler_view
*)samplers
->views
[slot
];
974 struct si_texture
*tex
= NULL
;
976 if (sview
&& sview
->base
.texture
&&
977 sview
->base
.texture
->target
!= PIPE_BUFFER
)
978 tex
= (struct si_texture
*)sview
->base
.texture
;
980 if (tex
&& tex
->surface
.fmask_size
)
983 si_set_sampler_state_desc(sstates
[i
], sview
, tex
,
984 desc
->list
+ desc_slot
* 16 + 12);
986 sctx
->descriptors_dirty
|= 1u << si_sampler_and_image_descriptors_idx(shader
);
990 /* BUFFER RESOURCES */
992 static void si_init_buffer_resources(struct si_buffer_resources
*buffers
,
993 struct si_descriptors
*descs
,
994 unsigned num_buffers
,
995 short shader_userdata_rel_index
,
996 enum radeon_bo_usage shader_usage
,
997 enum radeon_bo_usage shader_usage_constbuf
,
998 enum radeon_bo_priority priority
,
999 enum radeon_bo_priority priority_constbuf
)
1001 buffers
->shader_usage
= shader_usage
;
1002 buffers
->shader_usage_constbuf
= shader_usage_constbuf
;
1003 buffers
->priority
= priority
;
1004 buffers
->priority_constbuf
= priority_constbuf
;
1005 buffers
->buffers
= CALLOC(num_buffers
, sizeof(struct pipe_resource
*));
1007 si_init_descriptors(descs
, shader_userdata_rel_index
, 4, num_buffers
);
1010 static void si_release_buffer_resources(struct si_buffer_resources
*buffers
,
1011 struct si_descriptors
*descs
)
1015 for (i
= 0; i
< descs
->num_elements
; i
++) {
1016 pipe_resource_reference(&buffers
->buffers
[i
], NULL
);
1019 FREE(buffers
->buffers
);
1022 static void si_buffer_resources_begin_new_cs(struct si_context
*sctx
,
1023 struct si_buffer_resources
*buffers
)
1025 unsigned mask
= buffers
->enabled_mask
;
1027 /* Add buffers to the CS. */
1029 int i
= u_bit_scan(&mask
);
1031 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
,
1032 si_resource(buffers
->buffers
[i
]),
1033 i
< SI_NUM_SHADER_BUFFERS
? buffers
->shader_usage
:
1034 buffers
->shader_usage_constbuf
,
1035 i
< SI_NUM_SHADER_BUFFERS
? buffers
->priority
:
1036 buffers
->priority_constbuf
);
1040 static void si_get_buffer_from_descriptors(struct si_buffer_resources
*buffers
,
1041 struct si_descriptors
*descs
,
1042 unsigned idx
, struct pipe_resource
**buf
,
1043 unsigned *offset
, unsigned *size
)
1045 pipe_resource_reference(buf
, buffers
->buffers
[idx
]);
1047 struct si_resource
*res
= si_resource(*buf
);
1048 const uint32_t *desc
= descs
->list
+ idx
* 4;
1053 assert(G_008F04_STRIDE(desc
[1]) == 0);
1054 va
= si_desc_extract_buffer_address(desc
);
1056 assert(va
>= res
->gpu_address
&& va
+ *size
<= res
->gpu_address
+ res
->bo_size
);
1057 *offset
= va
- res
->gpu_address
;
1061 /* VERTEX BUFFERS */
1063 static void si_vertex_buffers_begin_new_cs(struct si_context
*sctx
)
1065 int count
= sctx
->vertex_elements
? sctx
->vertex_elements
->count
: 0;
1068 for (i
= 0; i
< count
; i
++) {
1069 int vb
= sctx
->vertex_elements
->vertex_buffer_index
[i
];
1071 if (vb
>= ARRAY_SIZE(sctx
->vertex_buffer
))
1073 if (!sctx
->vertex_buffer
[vb
].buffer
.resource
)
1076 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
,
1077 si_resource(sctx
->vertex_buffer
[vb
].buffer
.resource
),
1078 RADEON_USAGE_READ
, RADEON_PRIO_VERTEX_BUFFER
);
1081 if (!sctx
->vb_descriptors_buffer
)
1083 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
,
1084 sctx
->vb_descriptors_buffer
, RADEON_USAGE_READ
,
1085 RADEON_PRIO_DESCRIPTORS
);
1088 bool si_upload_vertex_buffer_descriptors(struct si_context
*sctx
)
1090 struct si_vertex_elements
*velems
= sctx
->vertex_elements
;
1092 unsigned desc_list_byte_size
;
1093 unsigned first_vb_use_mask
;
1096 if (!sctx
->vertex_buffers_dirty
|| !velems
)
1099 count
= velems
->count
;
1104 desc_list_byte_size
= velems
->desc_list_byte_size
;
1105 first_vb_use_mask
= velems
->first_vb_use_mask
;
1107 /* Vertex buffer descriptors are the only ones which are uploaded
1108 * directly through a staging buffer and don't go through
1109 * the fine-grained upload path.
1111 u_upload_alloc(sctx
->b
.const_uploader
, 0,
1112 desc_list_byte_size
,
1113 si_optimal_tcc_alignment(sctx
, desc_list_byte_size
),
1114 &sctx
->vb_descriptors_offset
,
1115 (struct pipe_resource
**)&sctx
->vb_descriptors_buffer
,
1117 if (!sctx
->vb_descriptors_buffer
) {
1118 sctx
->vb_descriptors_offset
= 0;
1119 sctx
->vb_descriptors_gpu_list
= NULL
;
1123 sctx
->vb_descriptors_gpu_list
= ptr
;
1124 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
,
1125 sctx
->vb_descriptors_buffer
, RADEON_USAGE_READ
,
1126 RADEON_PRIO_DESCRIPTORS
);
1128 assert(count
<= SI_MAX_ATTRIBS
);
1130 for (i
= 0; i
< count
; i
++) {
1131 struct pipe_vertex_buffer
*vb
;
1132 struct si_resource
*buf
;
1133 unsigned vbo_index
= velems
->vertex_buffer_index
[i
];
1134 uint32_t *desc
= &ptr
[i
*4];
1136 vb
= &sctx
->vertex_buffer
[vbo_index
];
1137 buf
= si_resource(vb
->buffer
.resource
);
1139 memset(desc
, 0, 16);
1143 int64_t offset
= (int64_t)((int)vb
->buffer_offset
) +
1144 velems
->src_offset
[i
];
1145 uint64_t va
= buf
->gpu_address
+ offset
;
1147 int64_t num_records
= (int64_t)buf
->b
.b
.width0
- offset
;
1148 if (sctx
->chip_class
!= VI
&& vb
->stride
) {
1149 /* Round up by rounding down and adding 1 */
1150 num_records
= (num_records
- velems
->format_size
[i
]) /
1153 assert(num_records
>= 0 && num_records
<= UINT_MAX
);
1156 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1157 S_008F04_STRIDE(vb
->stride
);
1158 desc
[2] = num_records
;
1159 desc
[3] = velems
->rsrc_word3
[i
];
1161 if (first_vb_use_mask
& (1 << i
)) {
1162 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
,
1163 si_resource(vb
->buffer
.resource
),
1164 RADEON_USAGE_READ
, RADEON_PRIO_VERTEX_BUFFER
);
1168 /* Don't flush the const cache. It would have a very negative effect
1169 * on performance (confirmed by testing). New descriptors are always
1170 * uploaded to a fresh new buffer, so I don't think flushing the const
1171 * cache is needed. */
1172 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.shader_pointers
);
1173 sctx
->vertex_buffers_dirty
= false;
1174 sctx
->vertex_buffer_pointer_dirty
= true;
1175 sctx
->prefetch_L2_mask
|= SI_PREFETCH_VBO_DESCRIPTORS
;
1180 /* CONSTANT BUFFERS */
1183 si_const_and_shader_buffer_descriptors_idx(unsigned shader
)
1185 return SI_DESCS_FIRST_SHADER
+ shader
* SI_NUM_SHADER_DESCS
+
1186 SI_SHADER_DESCS_CONST_AND_SHADER_BUFFERS
;
1189 static struct si_descriptors
*
1190 si_const_and_shader_buffer_descriptors(struct si_context
*sctx
, unsigned shader
)
1192 return &sctx
->descriptors
[si_const_and_shader_buffer_descriptors_idx(shader
)];
1195 void si_upload_const_buffer(struct si_context
*sctx
, struct si_resource
**buf
,
1196 const uint8_t *ptr
, unsigned size
, uint32_t *const_offset
)
1200 u_upload_alloc(sctx
->b
.const_uploader
, 0, size
,
1201 si_optimal_tcc_alignment(sctx
, size
),
1203 (struct pipe_resource
**)buf
, &tmp
);
1205 util_memcpy_cpu_to_le32(tmp
, ptr
, size
);
1208 static void si_set_constant_buffer(struct si_context
*sctx
,
1209 struct si_buffer_resources
*buffers
,
1210 unsigned descriptors_idx
,
1211 uint slot
, const struct pipe_constant_buffer
*input
)
1213 struct si_descriptors
*descs
= &sctx
->descriptors
[descriptors_idx
];
1214 assert(slot
< descs
->num_elements
);
1215 pipe_resource_reference(&buffers
->buffers
[slot
], NULL
);
1217 /* CIK cannot unbind a constant buffer (S_BUFFER_LOAD is buggy
1218 * with a NULL buffer). We need to use a dummy buffer instead. */
1219 if (sctx
->chip_class
== CIK
&&
1220 (!input
|| (!input
->buffer
&& !input
->user_buffer
)))
1221 input
= &sctx
->null_const_buf
;
1223 if (input
&& (input
->buffer
|| input
->user_buffer
)) {
1224 struct pipe_resource
*buffer
= NULL
;
1227 /* Upload the user buffer if needed. */
1228 if (input
->user_buffer
) {
1229 unsigned buffer_offset
;
1231 si_upload_const_buffer(sctx
,
1232 (struct si_resource
**)&buffer
, input
->user_buffer
,
1233 input
->buffer_size
, &buffer_offset
);
1235 /* Just unbind on failure. */
1236 si_set_constant_buffer(sctx
, buffers
, descriptors_idx
, slot
, NULL
);
1239 va
= si_resource(buffer
)->gpu_address
+ buffer_offset
;
1241 pipe_resource_reference(&buffer
, input
->buffer
);
1242 va
= si_resource(buffer
)->gpu_address
+ input
->buffer_offset
;
1245 /* Set the descriptor. */
1246 uint32_t *desc
= descs
->list
+ slot
*4;
1248 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1250 desc
[2] = input
->buffer_size
;
1251 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1252 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1253 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1254 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1255 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1256 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
1258 buffers
->buffers
[slot
] = buffer
;
1259 radeon_add_to_gfx_buffer_list_check_mem(sctx
,
1260 si_resource(buffer
),
1261 buffers
->shader_usage_constbuf
,
1262 buffers
->priority_constbuf
, true);
1263 buffers
->enabled_mask
|= 1u << slot
;
1265 /* Clear the descriptor. */
1266 memset(descs
->list
+ slot
*4, 0, sizeof(uint32_t) * 4);
1267 buffers
->enabled_mask
&= ~(1u << slot
);
1270 sctx
->descriptors_dirty
|= 1u << descriptors_idx
;
1273 static void si_pipe_set_constant_buffer(struct pipe_context
*ctx
,
1274 enum pipe_shader_type shader
, uint slot
,
1275 const struct pipe_constant_buffer
*input
)
1277 struct si_context
*sctx
= (struct si_context
*)ctx
;
1279 if (shader
>= SI_NUM_SHADERS
)
1282 if (slot
== 0 && input
&& input
->buffer
&&
1283 !(si_resource(input
->buffer
)->flags
& RADEON_FLAG_32BIT
)) {
1284 assert(!"constant buffer 0 must have a 32-bit VM address, use const_uploader");
1288 if (input
&& input
->buffer
)
1289 si_resource(input
->buffer
)->bind_history
|= PIPE_BIND_CONSTANT_BUFFER
;
1291 slot
= si_get_constbuf_slot(slot
);
1292 si_set_constant_buffer(sctx
, &sctx
->const_and_shader_buffers
[shader
],
1293 si_const_and_shader_buffer_descriptors_idx(shader
),
1297 void si_get_pipe_constant_buffer(struct si_context
*sctx
, uint shader
,
1298 uint slot
, struct pipe_constant_buffer
*cbuf
)
1300 cbuf
->user_buffer
= NULL
;
1301 si_get_buffer_from_descriptors(
1302 &sctx
->const_and_shader_buffers
[shader
],
1303 si_const_and_shader_buffer_descriptors(sctx
, shader
),
1304 si_get_constbuf_slot(slot
),
1305 &cbuf
->buffer
, &cbuf
->buffer_offset
, &cbuf
->buffer_size
);
1308 /* SHADER BUFFERS */
1310 static void si_set_shader_buffer(struct si_context
*sctx
,
1311 struct si_buffer_resources
*buffers
,
1312 unsigned descriptors_idx
,
1313 uint slot
, const struct pipe_shader_buffer
*sbuffer
,
1314 enum radeon_bo_priority priority
)
1316 struct si_descriptors
*descs
= &sctx
->descriptors
[descriptors_idx
];
1317 uint32_t *desc
= descs
->list
+ slot
* 4;
1319 if (!sbuffer
|| !sbuffer
->buffer
) {
1320 pipe_resource_reference(&buffers
->buffers
[slot
], NULL
);
1321 memset(desc
, 0, sizeof(uint32_t) * 4);
1322 buffers
->enabled_mask
&= ~(1u << slot
);
1323 sctx
->descriptors_dirty
|= 1u << descriptors_idx
;
1327 struct si_resource
*buf
= si_resource(sbuffer
->buffer
);
1328 uint64_t va
= buf
->gpu_address
+ sbuffer
->buffer_offset
;
1331 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1333 desc
[2] = sbuffer
->buffer_size
;
1334 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1335 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1336 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1337 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1338 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1339 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
1341 pipe_resource_reference(&buffers
->buffers
[slot
], &buf
->b
.b
);
1342 radeon_add_to_gfx_buffer_list_check_mem(sctx
, buf
,
1343 buffers
->shader_usage
,
1346 buffers
->enabled_mask
|= 1u << slot
;
1347 sctx
->descriptors_dirty
|= 1u << descriptors_idx
;
1349 util_range_add(&buf
->valid_buffer_range
, sbuffer
->buffer_offset
,
1350 sbuffer
->buffer_offset
+ sbuffer
->buffer_size
);
1353 static void si_set_shader_buffers(struct pipe_context
*ctx
,
1354 enum pipe_shader_type shader
,
1355 unsigned start_slot
, unsigned count
,
1356 const struct pipe_shader_buffer
*sbuffers
)
1358 struct si_context
*sctx
= (struct si_context
*)ctx
;
1359 struct si_buffer_resources
*buffers
= &sctx
->const_and_shader_buffers
[shader
];
1360 unsigned descriptors_idx
= si_const_and_shader_buffer_descriptors_idx(shader
);
1363 assert(start_slot
+ count
<= SI_NUM_SHADER_BUFFERS
);
1365 for (i
= 0; i
< count
; ++i
) {
1366 const struct pipe_shader_buffer
*sbuffer
= sbuffers
? &sbuffers
[i
] : NULL
;
1367 unsigned slot
= si_get_shaderbuf_slot(start_slot
+ i
);
1369 if (sbuffer
&& sbuffer
->buffer
)
1370 si_resource(sbuffer
->buffer
)->bind_history
|= PIPE_BIND_SHADER_BUFFER
;
1372 si_set_shader_buffer(sctx
, buffers
, descriptors_idx
, slot
, sbuffer
,
1377 void si_get_shader_buffers(struct si_context
*sctx
,
1378 enum pipe_shader_type shader
,
1379 uint start_slot
, uint count
,
1380 struct pipe_shader_buffer
*sbuf
)
1382 struct si_buffer_resources
*buffers
= &sctx
->const_and_shader_buffers
[shader
];
1383 struct si_descriptors
*descs
= si_const_and_shader_buffer_descriptors(sctx
, shader
);
1385 for (unsigned i
= 0; i
< count
; ++i
) {
1386 si_get_buffer_from_descriptors(
1388 si_get_shaderbuf_slot(start_slot
+ i
),
1389 &sbuf
[i
].buffer
, &sbuf
[i
].buffer_offset
,
1390 &sbuf
[i
].buffer_size
);
1396 void si_set_rw_buffer(struct si_context
*sctx
,
1397 uint slot
, const struct pipe_constant_buffer
*input
)
1399 si_set_constant_buffer(sctx
, &sctx
->rw_buffers
, SI_DESCS_RW_BUFFERS
,
1403 void si_set_rw_shader_buffer(struct si_context
*sctx
, uint slot
,
1404 const struct pipe_shader_buffer
*sbuffer
)
1406 si_set_shader_buffer(sctx
, &sctx
->rw_buffers
, SI_DESCS_RW_BUFFERS
,
1407 slot
, sbuffer
, RADEON_PRIO_SHADER_RW_BUFFER
);
1410 void si_set_ring_buffer(struct si_context
*sctx
, uint slot
,
1411 struct pipe_resource
*buffer
,
1412 unsigned stride
, unsigned num_records
,
1413 bool add_tid
, bool swizzle
,
1414 unsigned element_size
, unsigned index_stride
, uint64_t offset
)
1416 struct si_buffer_resources
*buffers
= &sctx
->rw_buffers
;
1417 struct si_descriptors
*descs
= &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
1419 /* The stride field in the resource descriptor has 14 bits */
1420 assert(stride
< (1 << 14));
1422 assert(slot
< descs
->num_elements
);
1423 pipe_resource_reference(&buffers
->buffers
[slot
], NULL
);
1428 va
= si_resource(buffer
)->gpu_address
+ offset
;
1430 switch (element_size
) {
1432 assert(!"Unsupported ring buffer element size");
1448 switch (index_stride
) {
1450 assert(!"Unsupported ring buffer index stride");
1466 if (sctx
->chip_class
>= VI
&& stride
)
1467 num_records
*= stride
;
1469 /* Set the descriptor. */
1470 uint32_t *desc
= descs
->list
+ slot
*4;
1472 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1473 S_008F04_STRIDE(stride
) |
1474 S_008F04_SWIZZLE_ENABLE(swizzle
);
1475 desc
[2] = num_records
;
1476 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1477 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1478 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1479 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1480 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1481 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
) |
1482 S_008F0C_INDEX_STRIDE(index_stride
) |
1483 S_008F0C_ADD_TID_ENABLE(add_tid
);
1485 if (sctx
->chip_class
>= GFX9
)
1486 assert(!swizzle
|| element_size
== 1); /* always 4 bytes on GFX9 */
1488 desc
[3] |= S_008F0C_ELEMENT_SIZE(element_size
);
1490 pipe_resource_reference(&buffers
->buffers
[slot
], buffer
);
1491 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
,
1492 si_resource(buffer
),
1493 buffers
->shader_usage
, buffers
->priority
);
1494 buffers
->enabled_mask
|= 1u << slot
;
1496 /* Clear the descriptor. */
1497 memset(descs
->list
+ slot
*4, 0, sizeof(uint32_t) * 4);
1498 buffers
->enabled_mask
&= ~(1u << slot
);
1501 sctx
->descriptors_dirty
|= 1u << SI_DESCS_RW_BUFFERS
;
1504 static void si_desc_reset_buffer_offset(uint32_t *desc
, uint64_t old_buf_va
,
1505 struct pipe_resource
*new_buf
)
1507 /* Retrieve the buffer offset from the descriptor. */
1508 uint64_t old_desc_va
= si_desc_extract_buffer_address(desc
);
1510 assert(old_buf_va
<= old_desc_va
);
1511 uint64_t offset_within_buffer
= old_desc_va
- old_buf_va
;
1513 /* Update the descriptor. */
1514 si_set_buf_desc_address(si_resource(new_buf
), offset_within_buffer
,
1518 /* INTERNAL CONST BUFFERS */
1520 static void si_set_polygon_stipple(struct pipe_context
*ctx
,
1521 const struct pipe_poly_stipple
*state
)
1523 struct si_context
*sctx
= (struct si_context
*)ctx
;
1524 struct pipe_constant_buffer cb
= {};
1525 unsigned stipple
[32];
1528 for (i
= 0; i
< 32; i
++)
1529 stipple
[i
] = util_bitreverse(state
->stipple
[i
]);
1531 cb
.user_buffer
= stipple
;
1532 cb
.buffer_size
= sizeof(stipple
);
1534 si_set_rw_buffer(sctx
, SI_PS_CONST_POLY_STIPPLE
, &cb
);
1537 /* TEXTURE METADATA ENABLE/DISABLE */
1540 si_resident_handles_update_needs_color_decompress(struct si_context
*sctx
)
1542 util_dynarray_clear(&sctx
->resident_tex_needs_color_decompress
);
1543 util_dynarray_clear(&sctx
->resident_img_needs_color_decompress
);
1545 util_dynarray_foreach(&sctx
->resident_tex_handles
,
1546 struct si_texture_handle
*, tex_handle
) {
1547 struct pipe_resource
*res
= (*tex_handle
)->view
->texture
;
1548 struct si_texture
*tex
;
1550 if (!res
|| res
->target
== PIPE_BUFFER
)
1553 tex
= (struct si_texture
*)res
;
1554 if (!color_needs_decompression(tex
))
1557 util_dynarray_append(&sctx
->resident_tex_needs_color_decompress
,
1558 struct si_texture_handle
*, *tex_handle
);
1561 util_dynarray_foreach(&sctx
->resident_img_handles
,
1562 struct si_image_handle
*, img_handle
) {
1563 struct pipe_image_view
*view
= &(*img_handle
)->view
;
1564 struct pipe_resource
*res
= view
->resource
;
1565 struct si_texture
*tex
;
1567 if (!res
|| res
->target
== PIPE_BUFFER
)
1570 tex
= (struct si_texture
*)res
;
1571 if (!color_needs_decompression(tex
))
1574 util_dynarray_append(&sctx
->resident_img_needs_color_decompress
,
1575 struct si_image_handle
*, *img_handle
);
1579 /* CMASK can be enabled (for fast clear) and disabled (for texture export)
1580 * while the texture is bound, possibly by a different context. In that case,
1581 * call this function to update needs_*_decompress_masks.
1583 void si_update_needs_color_decompress_masks(struct si_context
*sctx
)
1585 for (int i
= 0; i
< SI_NUM_SHADERS
; ++i
) {
1586 si_samplers_update_needs_color_decompress_mask(&sctx
->samplers
[i
]);
1587 si_images_update_needs_color_decompress_mask(&sctx
->images
[i
]);
1588 si_update_shader_needs_decompress_mask(sctx
, i
);
1591 si_resident_handles_update_needs_color_decompress(sctx
);
1594 /* BUFFER DISCARD/INVALIDATION */
1596 /** Reset descriptors of buffer resources after \p buf has been invalidated. */
1597 static void si_reset_buffer_resources(struct si_context
*sctx
,
1598 struct si_buffer_resources
*buffers
,
1599 unsigned descriptors_idx
,
1601 struct pipe_resource
*buf
,
1603 enum radeon_bo_usage usage
,
1604 enum radeon_bo_priority priority
)
1606 struct si_descriptors
*descs
= &sctx
->descriptors
[descriptors_idx
];
1607 unsigned mask
= buffers
->enabled_mask
& slot_mask
;
1610 unsigned i
= u_bit_scan(&mask
);
1611 if (buffers
->buffers
[i
] == buf
) {
1612 si_desc_reset_buffer_offset(descs
->list
+ i
*4,
1614 sctx
->descriptors_dirty
|= 1u << descriptors_idx
;
1616 radeon_add_to_gfx_buffer_list_check_mem(sctx
,
1618 usage
, priority
, true);
1623 /* Update all resource bindings where the buffer is bound, including
1624 * all resource descriptors. This is invalidate_buffer without
1625 * the invalidation. */
1626 void si_rebind_buffer(struct si_context
*sctx
, struct pipe_resource
*buf
,
1629 struct si_resource
*buffer
= si_resource(buf
);
1631 unsigned num_elems
= sctx
->vertex_elements
?
1632 sctx
->vertex_elements
->count
: 0;
1634 /* We changed the buffer, now we need to bind it where the old one
1635 * was bound. This consists of 2 things:
1636 * 1) Updating the resource descriptor and dirtying it.
1637 * 2) Adding a relocation to the CS, so that it's usable.
1640 /* Vertex buffers. */
1641 if (buffer
->bind_history
& PIPE_BIND_VERTEX_BUFFER
) {
1642 for (i
= 0; i
< num_elems
; i
++) {
1643 int vb
= sctx
->vertex_elements
->vertex_buffer_index
[i
];
1645 if (vb
>= ARRAY_SIZE(sctx
->vertex_buffer
))
1647 if (!sctx
->vertex_buffer
[vb
].buffer
.resource
)
1650 if (sctx
->vertex_buffer
[vb
].buffer
.resource
== buf
) {
1651 sctx
->vertex_buffers_dirty
= true;
1657 /* Streamout buffers. (other internal buffers can't be invalidated) */
1658 if (buffer
->bind_history
& PIPE_BIND_STREAM_OUTPUT
) {
1659 for (i
= SI_VS_STREAMOUT_BUF0
; i
<= SI_VS_STREAMOUT_BUF3
; i
++) {
1660 struct si_buffer_resources
*buffers
= &sctx
->rw_buffers
;
1661 struct si_descriptors
*descs
=
1662 &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
1664 if (buffers
->buffers
[i
] != buf
)
1667 si_desc_reset_buffer_offset(descs
->list
+ i
*4,
1669 sctx
->descriptors_dirty
|= 1u << SI_DESCS_RW_BUFFERS
;
1671 radeon_add_to_gfx_buffer_list_check_mem(sctx
,
1672 buffer
, buffers
->shader_usage
,
1673 RADEON_PRIO_SHADER_RW_BUFFER
,
1676 /* Update the streamout state. */
1677 if (sctx
->streamout
.begin_emitted
)
1678 si_emit_streamout_end(sctx
);
1679 sctx
->streamout
.append_bitmask
=
1680 sctx
->streamout
.enabled_mask
;
1681 si_streamout_buffers_dirty(sctx
);
1685 /* Constant and shader buffers. */
1686 if (buffer
->bind_history
& PIPE_BIND_CONSTANT_BUFFER
) {
1687 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++)
1688 si_reset_buffer_resources(sctx
, &sctx
->const_and_shader_buffers
[shader
],
1689 si_const_and_shader_buffer_descriptors_idx(shader
),
1690 u_bit_consecutive(SI_NUM_SHADER_BUFFERS
, SI_NUM_CONST_BUFFERS
),
1692 sctx
->const_and_shader_buffers
[shader
].shader_usage_constbuf
,
1693 sctx
->const_and_shader_buffers
[shader
].priority_constbuf
);
1696 if (buffer
->bind_history
& PIPE_BIND_SHADER_BUFFER
) {
1697 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++)
1698 si_reset_buffer_resources(sctx
, &sctx
->const_and_shader_buffers
[shader
],
1699 si_const_and_shader_buffer_descriptors_idx(shader
),
1700 u_bit_consecutive(0, SI_NUM_SHADER_BUFFERS
),
1702 sctx
->const_and_shader_buffers
[shader
].shader_usage
,
1703 sctx
->const_and_shader_buffers
[shader
].priority
);
1706 if (buffer
->bind_history
& PIPE_BIND_SAMPLER_VIEW
) {
1707 /* Texture buffers - update bindings. */
1708 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++) {
1709 struct si_samplers
*samplers
= &sctx
->samplers
[shader
];
1710 struct si_descriptors
*descs
=
1711 si_sampler_and_image_descriptors(sctx
, shader
);
1712 unsigned mask
= samplers
->enabled_mask
;
1715 unsigned i
= u_bit_scan(&mask
);
1716 if (samplers
->views
[i
]->texture
== buf
) {
1717 unsigned desc_slot
= si_get_sampler_slot(i
);
1719 si_desc_reset_buffer_offset(descs
->list
+
1722 sctx
->descriptors_dirty
|=
1723 1u << si_sampler_and_image_descriptors_idx(shader
);
1725 radeon_add_to_gfx_buffer_list_check_mem(sctx
,
1726 buffer
, RADEON_USAGE_READ
,
1727 RADEON_PRIO_SAMPLER_BUFFER
,
1735 if (buffer
->bind_history
& PIPE_BIND_SHADER_IMAGE
) {
1736 for (shader
= 0; shader
< SI_NUM_SHADERS
; ++shader
) {
1737 struct si_images
*images
= &sctx
->images
[shader
];
1738 struct si_descriptors
*descs
=
1739 si_sampler_and_image_descriptors(sctx
, shader
);
1740 unsigned mask
= images
->enabled_mask
;
1743 unsigned i
= u_bit_scan(&mask
);
1745 if (images
->views
[i
].resource
== buf
) {
1746 unsigned desc_slot
= si_get_image_slot(i
);
1748 if (images
->views
[i
].access
& PIPE_IMAGE_ACCESS_WRITE
)
1749 si_mark_image_range_valid(&images
->views
[i
]);
1751 si_desc_reset_buffer_offset(
1752 descs
->list
+ desc_slot
* 8 + 4,
1754 sctx
->descriptors_dirty
|=
1755 1u << si_sampler_and_image_descriptors_idx(shader
);
1757 radeon_add_to_gfx_buffer_list_check_mem(
1759 RADEON_USAGE_READWRITE
,
1760 RADEON_PRIO_SAMPLER_BUFFER
, true);
1766 /* Bindless texture handles */
1767 if (buffer
->texture_handle_allocated
) {
1768 struct si_descriptors
*descs
= &sctx
->bindless_descriptors
;
1770 util_dynarray_foreach(&sctx
->resident_tex_handles
,
1771 struct si_texture_handle
*, tex_handle
) {
1772 struct pipe_sampler_view
*view
= (*tex_handle
)->view
;
1773 unsigned desc_slot
= (*tex_handle
)->desc_slot
;
1775 if (view
->texture
== buf
) {
1776 si_set_buf_desc_address(buffer
,
1779 desc_slot
* 16 + 4);
1781 (*tex_handle
)->desc_dirty
= true;
1782 sctx
->bindless_descriptors_dirty
= true;
1784 radeon_add_to_gfx_buffer_list_check_mem(
1787 RADEON_PRIO_SAMPLER_BUFFER
, true);
1792 /* Bindless image handles */
1793 if (buffer
->image_handle_allocated
) {
1794 struct si_descriptors
*descs
= &sctx
->bindless_descriptors
;
1796 util_dynarray_foreach(&sctx
->resident_img_handles
,
1797 struct si_image_handle
*, img_handle
) {
1798 struct pipe_image_view
*view
= &(*img_handle
)->view
;
1799 unsigned desc_slot
= (*img_handle
)->desc_slot
;
1801 if (view
->resource
== buf
) {
1802 if (view
->access
& PIPE_IMAGE_ACCESS_WRITE
)
1803 si_mark_image_range_valid(view
);
1805 si_set_buf_desc_address(buffer
,
1808 desc_slot
* 16 + 4);
1810 (*img_handle
)->desc_dirty
= true;
1811 sctx
->bindless_descriptors_dirty
= true;
1813 radeon_add_to_gfx_buffer_list_check_mem(
1815 RADEON_USAGE_READWRITE
,
1816 RADEON_PRIO_SAMPLER_BUFFER
, true);
1822 static void si_upload_bindless_descriptor(struct si_context
*sctx
,
1824 unsigned num_dwords
)
1826 struct si_descriptors
*desc
= &sctx
->bindless_descriptors
;
1827 unsigned desc_slot_offset
= desc_slot
* 16;
1831 data
= desc
->list
+ desc_slot_offset
;
1832 va
= desc
->gpu_address
+ desc_slot_offset
* 4;
1834 si_cp_write_data(sctx
, desc
->buffer
, va
- desc
->buffer
->gpu_address
,
1835 num_dwords
* 4, V_370_TC_L2
, V_370_ME
, data
);
1838 static void si_upload_bindless_descriptors(struct si_context
*sctx
)
1840 if (!sctx
->bindless_descriptors_dirty
)
1843 /* Wait for graphics/compute to be idle before updating the resident
1844 * descriptors directly in memory, in case the GPU is using them.
1846 sctx
->flags
|= SI_CONTEXT_PS_PARTIAL_FLUSH
|
1847 SI_CONTEXT_CS_PARTIAL_FLUSH
;
1848 si_emit_cache_flush(sctx
);
1850 util_dynarray_foreach(&sctx
->resident_tex_handles
,
1851 struct si_texture_handle
*, tex_handle
) {
1852 unsigned desc_slot
= (*tex_handle
)->desc_slot
;
1854 if (!(*tex_handle
)->desc_dirty
)
1857 si_upload_bindless_descriptor(sctx
, desc_slot
, 16);
1858 (*tex_handle
)->desc_dirty
= false;
1861 util_dynarray_foreach(&sctx
->resident_img_handles
,
1862 struct si_image_handle
*, img_handle
) {
1863 unsigned desc_slot
= (*img_handle
)->desc_slot
;
1865 if (!(*img_handle
)->desc_dirty
)
1868 si_upload_bindless_descriptor(sctx
, desc_slot
, 8);
1869 (*img_handle
)->desc_dirty
= false;
1872 /* Invalidate L1 because it doesn't know that L2 changed. */
1873 sctx
->flags
|= SI_CONTEXT_INV_SMEM_L1
;
1874 si_emit_cache_flush(sctx
);
1876 sctx
->bindless_descriptors_dirty
= false;
1879 /* Update mutable image descriptor fields of all resident textures. */
1880 static void si_update_bindless_texture_descriptor(struct si_context
*sctx
,
1881 struct si_texture_handle
*tex_handle
)
1883 struct si_sampler_view
*sview
= (struct si_sampler_view
*)tex_handle
->view
;
1884 struct si_descriptors
*desc
= &sctx
->bindless_descriptors
;
1885 unsigned desc_slot_offset
= tex_handle
->desc_slot
* 16;
1886 uint32_t desc_list
[16];
1888 if (sview
->base
.texture
->target
== PIPE_BUFFER
)
1891 memcpy(desc_list
, desc
->list
+ desc_slot_offset
, sizeof(desc_list
));
1892 si_set_sampler_view_desc(sctx
, sview
, &tex_handle
->sstate
,
1893 desc
->list
+ desc_slot_offset
);
1895 if (memcmp(desc_list
, desc
->list
+ desc_slot_offset
,
1896 sizeof(desc_list
))) {
1897 tex_handle
->desc_dirty
= true;
1898 sctx
->bindless_descriptors_dirty
= true;
1902 static void si_update_bindless_image_descriptor(struct si_context
*sctx
,
1903 struct si_image_handle
*img_handle
)
1905 struct si_descriptors
*desc
= &sctx
->bindless_descriptors
;
1906 unsigned desc_slot_offset
= img_handle
->desc_slot
* 16;
1907 struct pipe_image_view
*view
= &img_handle
->view
;
1908 uint32_t desc_list
[8];
1910 if (view
->resource
->target
== PIPE_BUFFER
)
1913 memcpy(desc_list
, desc
->list
+ desc_slot_offset
,
1915 si_set_shader_image_desc(sctx
, view
, true,
1916 desc
->list
+ desc_slot_offset
, NULL
);
1918 if (memcmp(desc_list
, desc
->list
+ desc_slot_offset
,
1919 sizeof(desc_list
))) {
1920 img_handle
->desc_dirty
= true;
1921 sctx
->bindless_descriptors_dirty
= true;
1925 static void si_update_all_resident_texture_descriptors(struct si_context
*sctx
)
1927 util_dynarray_foreach(&sctx
->resident_tex_handles
,
1928 struct si_texture_handle
*, tex_handle
) {
1929 si_update_bindless_texture_descriptor(sctx
, *tex_handle
);
1932 util_dynarray_foreach(&sctx
->resident_img_handles
,
1933 struct si_image_handle
*, img_handle
) {
1934 si_update_bindless_image_descriptor(sctx
, *img_handle
);
1937 si_upload_bindless_descriptors(sctx
);
1940 /* Update mutable image descriptor fields of all bound textures. */
1941 void si_update_all_texture_descriptors(struct si_context
*sctx
)
1945 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++) {
1946 struct si_samplers
*samplers
= &sctx
->samplers
[shader
];
1947 struct si_images
*images
= &sctx
->images
[shader
];
1951 mask
= images
->enabled_mask
;
1953 unsigned i
= u_bit_scan(&mask
);
1954 struct pipe_image_view
*view
= &images
->views
[i
];
1956 if (!view
->resource
||
1957 view
->resource
->target
== PIPE_BUFFER
)
1960 si_set_shader_image(sctx
, shader
, i
, view
, true);
1963 /* Sampler views. */
1964 mask
= samplers
->enabled_mask
;
1966 unsigned i
= u_bit_scan(&mask
);
1967 struct pipe_sampler_view
*view
= samplers
->views
[i
];
1971 view
->texture
->target
== PIPE_BUFFER
)
1974 si_set_sampler_view(sctx
, shader
, i
,
1975 samplers
->views
[i
], true);
1978 si_update_shader_needs_decompress_mask(sctx
, shader
);
1981 si_update_all_resident_texture_descriptors(sctx
);
1982 si_update_ps_colorbuf0_slot(sctx
);
1985 /* SHADER USER DATA */
1987 static void si_mark_shader_pointers_dirty(struct si_context
*sctx
,
1990 sctx
->shader_pointers_dirty
|=
1991 u_bit_consecutive(SI_DESCS_FIRST_SHADER
+ shader
* SI_NUM_SHADER_DESCS
,
1992 SI_NUM_SHADER_DESCS
);
1994 if (shader
== PIPE_SHADER_VERTEX
)
1995 sctx
->vertex_buffer_pointer_dirty
= sctx
->vb_descriptors_buffer
!= NULL
;
1997 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.shader_pointers
);
2000 static void si_shader_pointers_begin_new_cs(struct si_context
*sctx
)
2002 sctx
->shader_pointers_dirty
= u_bit_consecutive(0, SI_NUM_DESCS
);
2003 sctx
->vertex_buffer_pointer_dirty
= sctx
->vb_descriptors_buffer
!= NULL
;
2004 si_mark_atom_dirty(sctx
, &sctx
->atoms
.s
.shader_pointers
);
2005 sctx
->graphics_bindless_pointer_dirty
= sctx
->bindless_descriptors
.buffer
!= NULL
;
2006 sctx
->compute_bindless_pointer_dirty
= sctx
->bindless_descriptors
.buffer
!= NULL
;
2009 /* Set a base register address for user data constants in the given shader.
2010 * This assigns a mapping from PIPE_SHADER_* to SPI_SHADER_USER_DATA_*.
2012 static void si_set_user_data_base(struct si_context
*sctx
,
2013 unsigned shader
, uint32_t new_base
)
2015 uint32_t *base
= &sctx
->shader_pointers
.sh_base
[shader
];
2017 if (*base
!= new_base
) {
2021 si_mark_shader_pointers_dirty(sctx
, shader
);
2023 /* Any change in enabled shader stages requires re-emitting
2024 * the VS state SGPR, because it contains the clamp_vertex_color
2025 * state, which can be done in VS, TES, and GS.
2027 sctx
->last_vs_state
= ~0;
2031 /* This must be called when these shaders are changed from non-NULL to NULL
2034 * - tessellation control shader
2035 * - tessellation evaluation shader
2037 void si_shader_change_notify(struct si_context
*sctx
)
2039 /* VS can be bound as VS, ES, or LS. */
2040 if (sctx
->tes_shader
.cso
) {
2041 if (sctx
->chip_class
>= GFX9
) {
2042 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
2043 R_00B430_SPI_SHADER_USER_DATA_LS_0
);
2045 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
2046 R_00B530_SPI_SHADER_USER_DATA_LS_0
);
2048 } else if (sctx
->gs_shader
.cso
) {
2049 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
2050 R_00B330_SPI_SHADER_USER_DATA_ES_0
);
2052 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
2053 R_00B130_SPI_SHADER_USER_DATA_VS_0
);
2056 /* TES can be bound as ES, VS, or not bound. */
2057 if (sctx
->tes_shader
.cso
) {
2058 if (sctx
->gs_shader
.cso
)
2059 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_EVAL
,
2060 R_00B330_SPI_SHADER_USER_DATA_ES_0
);
2062 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_EVAL
,
2063 R_00B130_SPI_SHADER_USER_DATA_VS_0
);
2065 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_EVAL
, 0);
2069 static void si_emit_shader_pointer_head(struct radeon_cmdbuf
*cs
,
2071 unsigned pointer_count
)
2073 radeon_emit(cs
, PKT3(PKT3_SET_SH_REG
, pointer_count
, 0));
2074 radeon_emit(cs
, (sh_offset
- SI_SH_REG_OFFSET
) >> 2);
2077 static void si_emit_shader_pointer_body(struct si_screen
*sscreen
,
2078 struct radeon_cmdbuf
*cs
,
2081 radeon_emit(cs
, va
);
2083 assert(va
== 0 || (va
>> 32) == sscreen
->info
.address32_hi
);
2086 static void si_emit_shader_pointer(struct si_context
*sctx
,
2087 struct si_descriptors
*desc
,
2090 struct radeon_cmdbuf
*cs
= sctx
->gfx_cs
;
2091 unsigned sh_offset
= sh_base
+ desc
->shader_userdata_offset
;
2093 si_emit_shader_pointer_head(cs
, sh_offset
, 1);
2094 si_emit_shader_pointer_body(sctx
->screen
, cs
, desc
->gpu_address
);
2097 static void si_emit_consecutive_shader_pointers(struct si_context
*sctx
,
2098 unsigned pointer_mask
,
2104 struct radeon_cmdbuf
*cs
= sctx
->gfx_cs
;
2105 unsigned mask
= sctx
->shader_pointers_dirty
& pointer_mask
;
2109 u_bit_scan_consecutive_range(&mask
, &start
, &count
);
2111 struct si_descriptors
*descs
= &sctx
->descriptors
[start
];
2112 unsigned sh_offset
= sh_base
+ descs
->shader_userdata_offset
;
2114 si_emit_shader_pointer_head(cs
, sh_offset
, count
);
2115 for (int i
= 0; i
< count
; i
++)
2116 si_emit_shader_pointer_body(sctx
->screen
, cs
,
2117 descs
[i
].gpu_address
);
2121 static void si_emit_global_shader_pointers(struct si_context
*sctx
,
2122 struct si_descriptors
*descs
)
2124 if (sctx
->chip_class
== GFX9
) {
2125 /* Broadcast it to all shader stages. */
2126 si_emit_shader_pointer(sctx
, descs
,
2127 R_00B530_SPI_SHADER_USER_DATA_COMMON_0
);
2131 si_emit_shader_pointer(sctx
, descs
,
2132 R_00B030_SPI_SHADER_USER_DATA_PS_0
);
2133 si_emit_shader_pointer(sctx
, descs
,
2134 R_00B130_SPI_SHADER_USER_DATA_VS_0
);
2135 si_emit_shader_pointer(sctx
, descs
,
2136 R_00B330_SPI_SHADER_USER_DATA_ES_0
);
2137 si_emit_shader_pointer(sctx
, descs
,
2138 R_00B230_SPI_SHADER_USER_DATA_GS_0
);
2139 si_emit_shader_pointer(sctx
, descs
,
2140 R_00B430_SPI_SHADER_USER_DATA_HS_0
);
2141 si_emit_shader_pointer(sctx
, descs
,
2142 R_00B530_SPI_SHADER_USER_DATA_LS_0
);
2145 void si_emit_graphics_shader_pointers(struct si_context
*sctx
)
2147 uint32_t *sh_base
= sctx
->shader_pointers
.sh_base
;
2149 if (sctx
->shader_pointers_dirty
& (1 << SI_DESCS_RW_BUFFERS
)) {
2150 si_emit_global_shader_pointers(sctx
,
2151 &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
]);
2154 si_emit_consecutive_shader_pointers(sctx
, SI_DESCS_SHADER_MASK(VERTEX
),
2155 sh_base
[PIPE_SHADER_VERTEX
]);
2156 si_emit_consecutive_shader_pointers(sctx
, SI_DESCS_SHADER_MASK(TESS_EVAL
),
2157 sh_base
[PIPE_SHADER_TESS_EVAL
]);
2158 si_emit_consecutive_shader_pointers(sctx
, SI_DESCS_SHADER_MASK(FRAGMENT
),
2159 sh_base
[PIPE_SHADER_FRAGMENT
]);
2160 si_emit_consecutive_shader_pointers(sctx
, SI_DESCS_SHADER_MASK(TESS_CTRL
),
2161 sh_base
[PIPE_SHADER_TESS_CTRL
]);
2162 si_emit_consecutive_shader_pointers(sctx
, SI_DESCS_SHADER_MASK(GEOMETRY
),
2163 sh_base
[PIPE_SHADER_GEOMETRY
]);
2165 sctx
->shader_pointers_dirty
&=
2166 ~u_bit_consecutive(SI_DESCS_RW_BUFFERS
, SI_DESCS_FIRST_COMPUTE
);
2168 if (sctx
->vertex_buffer_pointer_dirty
) {
2169 struct radeon_cmdbuf
*cs
= sctx
->gfx_cs
;
2171 /* Find the location of the VB descriptor pointer. */
2172 /* TODO: In the future, the pointer will be packed in unused
2173 * bits of the first 2 VB descriptors. */
2174 unsigned sh_dw_offset
= SI_VS_NUM_USER_SGPR
;
2175 if (sctx
->chip_class
>= GFX9
) {
2176 if (sctx
->tes_shader
.cso
)
2177 sh_dw_offset
= GFX9_TCS_NUM_USER_SGPR
;
2178 else if (sctx
->gs_shader
.cso
)
2179 sh_dw_offset
= GFX9_VSGS_NUM_USER_SGPR
;
2182 unsigned sh_offset
= sh_base
[PIPE_SHADER_VERTEX
] + sh_dw_offset
* 4;
2183 si_emit_shader_pointer_head(cs
, sh_offset
, 1);
2184 si_emit_shader_pointer_body(sctx
->screen
, cs
,
2185 sctx
->vb_descriptors_buffer
->gpu_address
+
2186 sctx
->vb_descriptors_offset
);
2187 sctx
->vertex_buffer_pointer_dirty
= false;
2190 if (sctx
->graphics_bindless_pointer_dirty
) {
2191 si_emit_global_shader_pointers(sctx
,
2192 &sctx
->bindless_descriptors
);
2193 sctx
->graphics_bindless_pointer_dirty
= false;
2197 void si_emit_compute_shader_pointers(struct si_context
*sctx
)
2199 unsigned base
= R_00B900_COMPUTE_USER_DATA_0
;
2201 si_emit_consecutive_shader_pointers(sctx
, SI_DESCS_SHADER_MASK(COMPUTE
),
2202 R_00B900_COMPUTE_USER_DATA_0
);
2203 sctx
->shader_pointers_dirty
&= ~SI_DESCS_SHADER_MASK(COMPUTE
);
2205 if (sctx
->compute_bindless_pointer_dirty
) {
2206 si_emit_shader_pointer(sctx
, &sctx
->bindless_descriptors
, base
);
2207 sctx
->compute_bindless_pointer_dirty
= false;
2213 static void si_init_bindless_descriptors(struct si_context
*sctx
,
2214 struct si_descriptors
*desc
,
2215 short shader_userdata_rel_index
,
2216 unsigned num_elements
)
2218 MAYBE_UNUSED
unsigned desc_slot
;
2220 si_init_descriptors(desc
, shader_userdata_rel_index
, 16, num_elements
);
2221 sctx
->bindless_descriptors
.num_active_slots
= num_elements
;
2223 /* The first bindless descriptor is stored at slot 1, because 0 is not
2224 * considered to be a valid handle.
2226 sctx
->num_bindless_descriptors
= 1;
2228 /* Track which bindless slots are used (or not). */
2229 util_idalloc_init(&sctx
->bindless_used_slots
);
2230 util_idalloc_resize(&sctx
->bindless_used_slots
, num_elements
);
2232 /* Reserve slot 0 because it's an invalid handle for bindless. */
2233 desc_slot
= util_idalloc_alloc(&sctx
->bindless_used_slots
);
2234 assert(desc_slot
== 0);
2237 static void si_release_bindless_descriptors(struct si_context
*sctx
)
2239 si_release_descriptors(&sctx
->bindless_descriptors
);
2240 util_idalloc_fini(&sctx
->bindless_used_slots
);
2243 static unsigned si_get_first_free_bindless_slot(struct si_context
*sctx
)
2245 struct si_descriptors
*desc
= &sctx
->bindless_descriptors
;
2248 desc_slot
= util_idalloc_alloc(&sctx
->bindless_used_slots
);
2249 if (desc_slot
>= desc
->num_elements
) {
2250 /* The array of bindless descriptors is full, resize it. */
2251 unsigned slot_size
= desc
->element_dw_size
* 4;
2252 unsigned new_num_elements
= desc
->num_elements
* 2;
2254 desc
->list
= REALLOC(desc
->list
, desc
->num_elements
* slot_size
,
2255 new_num_elements
* slot_size
);
2256 desc
->num_elements
= new_num_elements
;
2257 desc
->num_active_slots
= new_num_elements
;
2265 si_create_bindless_descriptor(struct si_context
*sctx
, uint32_t *desc_list
,
2268 struct si_descriptors
*desc
= &sctx
->bindless_descriptors
;
2269 unsigned desc_slot
, desc_slot_offset
;
2271 /* Find a free slot. */
2272 desc_slot
= si_get_first_free_bindless_slot(sctx
);
2274 /* For simplicity, sampler and image bindless descriptors use fixed
2275 * 16-dword slots for now. Image descriptors only need 8-dword but this
2276 * doesn't really matter because no real apps use image handles.
2278 desc_slot_offset
= desc_slot
* 16;
2280 /* Copy the descriptor into the array. */
2281 memcpy(desc
->list
+ desc_slot_offset
, desc_list
, size
);
2283 /* Re-upload the whole array of bindless descriptors into a new buffer.
2285 if (!si_upload_descriptors(sctx
, desc
))
2288 /* Make sure to re-emit the shader pointers for all stages. */
2289 sctx
->graphics_bindless_pointer_dirty
= true;
2290 sctx
->compute_bindless_pointer_dirty
= true;
2295 static void si_update_bindless_buffer_descriptor(struct si_context
*sctx
,
2297 struct pipe_resource
*resource
,
2301 struct si_descriptors
*desc
= &sctx
->bindless_descriptors
;
2302 struct si_resource
*buf
= si_resource(resource
);
2303 unsigned desc_slot_offset
= desc_slot
* 16;
2304 uint32_t *desc_list
= desc
->list
+ desc_slot_offset
+ 4;
2305 uint64_t old_desc_va
;
2307 assert(resource
->target
== PIPE_BUFFER
);
2309 /* Retrieve the old buffer addr from the descriptor. */
2310 old_desc_va
= si_desc_extract_buffer_address(desc_list
);
2312 if (old_desc_va
!= buf
->gpu_address
+ offset
) {
2313 /* The buffer has been invalidated when the handle wasn't
2314 * resident, update the descriptor and the dirty flag.
2316 si_set_buf_desc_address(buf
, offset
, &desc_list
[0]);
2322 static uint64_t si_create_texture_handle(struct pipe_context
*ctx
,
2323 struct pipe_sampler_view
*view
,
2324 const struct pipe_sampler_state
*state
)
2326 struct si_sampler_view
*sview
= (struct si_sampler_view
*)view
;
2327 struct si_context
*sctx
= (struct si_context
*)ctx
;
2328 struct si_texture_handle
*tex_handle
;
2329 struct si_sampler_state
*sstate
;
2330 uint32_t desc_list
[16];
2333 tex_handle
= CALLOC_STRUCT(si_texture_handle
);
2337 memset(desc_list
, 0, sizeof(desc_list
));
2338 si_init_descriptor_list(&desc_list
[0], 16, 1, null_texture_descriptor
);
2340 sstate
= ctx
->create_sampler_state(ctx
, state
);
2346 si_set_sampler_view_desc(sctx
, sview
, sstate
, &desc_list
[0]);
2347 memcpy(&tex_handle
->sstate
, sstate
, sizeof(*sstate
));
2348 ctx
->delete_sampler_state(ctx
, sstate
);
2350 tex_handle
->desc_slot
= si_create_bindless_descriptor(sctx
, desc_list
,
2352 if (!tex_handle
->desc_slot
) {
2357 handle
= tex_handle
->desc_slot
;
2359 if (!_mesa_hash_table_insert(sctx
->tex_handles
,
2360 (void *)(uintptr_t)handle
,
2366 pipe_sampler_view_reference(&tex_handle
->view
, view
);
2368 si_resource(sview
->base
.texture
)->texture_handle_allocated
= true;
2373 static void si_delete_texture_handle(struct pipe_context
*ctx
, uint64_t handle
)
2375 struct si_context
*sctx
= (struct si_context
*)ctx
;
2376 struct si_texture_handle
*tex_handle
;
2377 struct hash_entry
*entry
;
2379 entry
= _mesa_hash_table_search(sctx
->tex_handles
,
2380 (void *)(uintptr_t)handle
);
2384 tex_handle
= (struct si_texture_handle
*)entry
->data
;
2386 /* Allow this descriptor slot to be re-used. */
2387 util_idalloc_free(&sctx
->bindless_used_slots
, tex_handle
->desc_slot
);
2389 pipe_sampler_view_reference(&tex_handle
->view
, NULL
);
2390 _mesa_hash_table_remove(sctx
->tex_handles
, entry
);
2394 static void si_make_texture_handle_resident(struct pipe_context
*ctx
,
2395 uint64_t handle
, bool resident
)
2397 struct si_context
*sctx
= (struct si_context
*)ctx
;
2398 struct si_texture_handle
*tex_handle
;
2399 struct si_sampler_view
*sview
;
2400 struct hash_entry
*entry
;
2402 entry
= _mesa_hash_table_search(sctx
->tex_handles
,
2403 (void *)(uintptr_t)handle
);
2407 tex_handle
= (struct si_texture_handle
*)entry
->data
;
2408 sview
= (struct si_sampler_view
*)tex_handle
->view
;
2411 if (sview
->base
.texture
->target
!= PIPE_BUFFER
) {
2412 struct si_texture
*tex
=
2413 (struct si_texture
*)sview
->base
.texture
;
2415 if (depth_needs_decompression(tex
)) {
2416 util_dynarray_append(
2417 &sctx
->resident_tex_needs_depth_decompress
,
2418 struct si_texture_handle
*,
2422 if (color_needs_decompression(tex
)) {
2423 util_dynarray_append(
2424 &sctx
->resident_tex_needs_color_decompress
,
2425 struct si_texture_handle
*,
2429 if (tex
->dcc_offset
&&
2430 p_atomic_read(&tex
->framebuffers_bound
))
2431 sctx
->need_check_render_feedback
= true;
2433 si_update_bindless_texture_descriptor(sctx
, tex_handle
);
2435 si_update_bindless_buffer_descriptor(sctx
,
2436 tex_handle
->desc_slot
,
2437 sview
->base
.texture
,
2438 sview
->base
.u
.buf
.offset
,
2439 &tex_handle
->desc_dirty
);
2442 /* Re-upload the descriptor if it has been updated while it
2445 if (tex_handle
->desc_dirty
)
2446 sctx
->bindless_descriptors_dirty
= true;
2448 /* Add the texture handle to the per-context list. */
2449 util_dynarray_append(&sctx
->resident_tex_handles
,
2450 struct si_texture_handle
*, tex_handle
);
2452 /* Add the buffers to the current CS in case si_begin_new_cs()
2453 * is not going to be called.
2455 si_sampler_view_add_buffer(sctx
, sview
->base
.texture
,
2457 sview
->is_stencil_sampler
, false);
2459 /* Remove the texture handle from the per-context list. */
2460 util_dynarray_delete_unordered(&sctx
->resident_tex_handles
,
2461 struct si_texture_handle
*,
2464 if (sview
->base
.texture
->target
!= PIPE_BUFFER
) {
2465 util_dynarray_delete_unordered(
2466 &sctx
->resident_tex_needs_depth_decompress
,
2467 struct si_texture_handle
*, tex_handle
);
2469 util_dynarray_delete_unordered(
2470 &sctx
->resident_tex_needs_color_decompress
,
2471 struct si_texture_handle
*, tex_handle
);
2476 static uint64_t si_create_image_handle(struct pipe_context
*ctx
,
2477 const struct pipe_image_view
*view
)
2479 struct si_context
*sctx
= (struct si_context
*)ctx
;
2480 struct si_image_handle
*img_handle
;
2481 uint32_t desc_list
[8];
2484 if (!view
|| !view
->resource
)
2487 img_handle
= CALLOC_STRUCT(si_image_handle
);
2491 memset(desc_list
, 0, sizeof(desc_list
));
2492 si_init_descriptor_list(&desc_list
[0], 8, 1, null_image_descriptor
);
2494 si_set_shader_image_desc(sctx
, view
, false, &desc_list
[0], NULL
);
2496 img_handle
->desc_slot
= si_create_bindless_descriptor(sctx
, desc_list
,
2498 if (!img_handle
->desc_slot
) {
2503 handle
= img_handle
->desc_slot
;
2505 if (!_mesa_hash_table_insert(sctx
->img_handles
,
2506 (void *)(uintptr_t)handle
,
2512 util_copy_image_view(&img_handle
->view
, view
);
2514 si_resource(view
->resource
)->image_handle_allocated
= true;
2519 static void si_delete_image_handle(struct pipe_context
*ctx
, uint64_t handle
)
2521 struct si_context
*sctx
= (struct si_context
*)ctx
;
2522 struct si_image_handle
*img_handle
;
2523 struct hash_entry
*entry
;
2525 entry
= _mesa_hash_table_search(sctx
->img_handles
,
2526 (void *)(uintptr_t)handle
);
2530 img_handle
= (struct si_image_handle
*)entry
->data
;
2532 util_copy_image_view(&img_handle
->view
, NULL
);
2533 _mesa_hash_table_remove(sctx
->img_handles
, entry
);
2537 static void si_make_image_handle_resident(struct pipe_context
*ctx
,
2538 uint64_t handle
, unsigned access
,
2541 struct si_context
*sctx
= (struct si_context
*)ctx
;
2542 struct si_image_handle
*img_handle
;
2543 struct pipe_image_view
*view
;
2544 struct si_resource
*res
;
2545 struct hash_entry
*entry
;
2547 entry
= _mesa_hash_table_search(sctx
->img_handles
,
2548 (void *)(uintptr_t)handle
);
2552 img_handle
= (struct si_image_handle
*)entry
->data
;
2553 view
= &img_handle
->view
;
2554 res
= si_resource(view
->resource
);
2557 if (res
->b
.b
.target
!= PIPE_BUFFER
) {
2558 struct si_texture
*tex
= (struct si_texture
*)res
;
2559 unsigned level
= view
->u
.tex
.level
;
2561 if (color_needs_decompression(tex
)) {
2562 util_dynarray_append(
2563 &sctx
->resident_img_needs_color_decompress
,
2564 struct si_image_handle
*,
2568 if (vi_dcc_enabled(tex
, level
) &&
2569 p_atomic_read(&tex
->framebuffers_bound
))
2570 sctx
->need_check_render_feedback
= true;
2572 si_update_bindless_image_descriptor(sctx
, img_handle
);
2574 si_update_bindless_buffer_descriptor(sctx
,
2575 img_handle
->desc_slot
,
2578 &img_handle
->desc_dirty
);
2581 /* Re-upload the descriptor if it has been updated while it
2584 if (img_handle
->desc_dirty
)
2585 sctx
->bindless_descriptors_dirty
= true;
2587 /* Add the image handle to the per-context list. */
2588 util_dynarray_append(&sctx
->resident_img_handles
,
2589 struct si_image_handle
*, img_handle
);
2591 /* Add the buffers to the current CS in case si_begin_new_cs()
2592 * is not going to be called.
2594 si_sampler_view_add_buffer(sctx
, view
->resource
,
2595 (access
& PIPE_IMAGE_ACCESS_WRITE
) ?
2596 RADEON_USAGE_READWRITE
:
2597 RADEON_USAGE_READ
, false, false);
2599 /* Remove the image handle from the per-context list. */
2600 util_dynarray_delete_unordered(&sctx
->resident_img_handles
,
2601 struct si_image_handle
*,
2604 if (res
->b
.b
.target
!= PIPE_BUFFER
) {
2605 util_dynarray_delete_unordered(
2606 &sctx
->resident_img_needs_color_decompress
,
2607 struct si_image_handle
*,
2614 void si_all_resident_buffers_begin_new_cs(struct si_context
*sctx
)
2616 unsigned num_resident_tex_handles
, num_resident_img_handles
;
2618 num_resident_tex_handles
= sctx
->resident_tex_handles
.size
/
2619 sizeof(struct si_texture_handle
*);
2620 num_resident_img_handles
= sctx
->resident_img_handles
.size
/
2621 sizeof(struct si_image_handle
*);
2623 /* Add all resident texture handles. */
2624 util_dynarray_foreach(&sctx
->resident_tex_handles
,
2625 struct si_texture_handle
*, tex_handle
) {
2626 struct si_sampler_view
*sview
=
2627 (struct si_sampler_view
*)(*tex_handle
)->view
;
2629 si_sampler_view_add_buffer(sctx
, sview
->base
.texture
,
2631 sview
->is_stencil_sampler
, false);
2634 /* Add all resident image handles. */
2635 util_dynarray_foreach(&sctx
->resident_img_handles
,
2636 struct si_image_handle
*, img_handle
) {
2637 struct pipe_image_view
*view
= &(*img_handle
)->view
;
2639 si_sampler_view_add_buffer(sctx
, view
->resource
,
2640 RADEON_USAGE_READWRITE
,
2644 sctx
->num_resident_handles
+= num_resident_tex_handles
+
2645 num_resident_img_handles
;
2648 /* INIT/DEINIT/UPLOAD */
2650 void si_init_all_descriptors(struct si_context
*sctx
)
2653 unsigned first_shader
=
2654 sctx
->has_graphics
? 0 : PIPE_SHADER_COMPUTE
;
2656 for (i
= first_shader
; i
< SI_NUM_SHADERS
; i
++) {
2657 bool is_2nd
= sctx
->chip_class
>= GFX9
&&
2658 (i
== PIPE_SHADER_TESS_CTRL
||
2659 i
== PIPE_SHADER_GEOMETRY
);
2660 unsigned num_sampler_slots
= SI_NUM_IMAGES
/ 2 + SI_NUM_SAMPLERS
;
2661 unsigned num_buffer_slots
= SI_NUM_SHADER_BUFFERS
+ SI_NUM_CONST_BUFFERS
;
2663 struct si_descriptors
*desc
;
2666 if (i
== PIPE_SHADER_TESS_CTRL
) {
2667 rel_dw_offset
= (R_00B408_SPI_SHADER_USER_DATA_ADDR_LO_HS
-
2668 R_00B430_SPI_SHADER_USER_DATA_LS_0
) / 4;
2669 } else { /* PIPE_SHADER_GEOMETRY */
2670 rel_dw_offset
= (R_00B208_SPI_SHADER_USER_DATA_ADDR_LO_GS
-
2671 R_00B330_SPI_SHADER_USER_DATA_ES_0
) / 4;
2674 rel_dw_offset
= SI_SGPR_CONST_AND_SHADER_BUFFERS
;
2676 desc
= si_const_and_shader_buffer_descriptors(sctx
, i
);
2677 si_init_buffer_resources(&sctx
->const_and_shader_buffers
[i
], desc
,
2678 num_buffer_slots
, rel_dw_offset
,
2679 RADEON_USAGE_READWRITE
,
2681 RADEON_PRIO_SHADER_RW_BUFFER
,
2682 RADEON_PRIO_CONST_BUFFER
);
2683 desc
->slot_index_to_bind_directly
= si_get_constbuf_slot(0);
2686 if (i
== PIPE_SHADER_TESS_CTRL
) {
2687 rel_dw_offset
= (R_00B40C_SPI_SHADER_USER_DATA_ADDR_HI_HS
-
2688 R_00B430_SPI_SHADER_USER_DATA_LS_0
) / 4;
2689 } else { /* PIPE_SHADER_GEOMETRY */
2690 rel_dw_offset
= (R_00B20C_SPI_SHADER_USER_DATA_ADDR_HI_GS
-
2691 R_00B330_SPI_SHADER_USER_DATA_ES_0
) / 4;
2694 rel_dw_offset
= SI_SGPR_SAMPLERS_AND_IMAGES
;
2697 desc
= si_sampler_and_image_descriptors(sctx
, i
);
2698 si_init_descriptors(desc
, rel_dw_offset
, 16, num_sampler_slots
);
2701 for (j
= 0; j
< SI_NUM_IMAGES
; j
++)
2702 memcpy(desc
->list
+ j
* 8, null_image_descriptor
, 8 * 4);
2703 for (; j
< SI_NUM_IMAGES
+ SI_NUM_SAMPLERS
* 2; j
++)
2704 memcpy(desc
->list
+ j
* 8, null_texture_descriptor
, 8 * 4);
2707 si_init_buffer_resources(&sctx
->rw_buffers
,
2708 &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
],
2709 SI_NUM_RW_BUFFERS
, SI_SGPR_RW_BUFFERS
,
2710 /* The second set of usage/priority is used by
2711 * const buffers in RW buffer slots. */
2712 RADEON_USAGE_READWRITE
, RADEON_USAGE_READ
,
2713 RADEON_PRIO_SHADER_RINGS
, RADEON_PRIO_CONST_BUFFER
);
2714 sctx
->descriptors
[SI_DESCS_RW_BUFFERS
].num_active_slots
= SI_NUM_RW_BUFFERS
;
2716 /* Initialize an array of 1024 bindless descriptors, when the limit is
2717 * reached, just make it larger and re-upload the whole array.
2719 si_init_bindless_descriptors(sctx
, &sctx
->bindless_descriptors
,
2720 SI_SGPR_BINDLESS_SAMPLERS_AND_IMAGES
,
2723 sctx
->descriptors_dirty
= u_bit_consecutive(0, SI_NUM_DESCS
);
2725 /* Set pipe_context functions. */
2726 sctx
->b
.bind_sampler_states
= si_bind_sampler_states
;
2727 sctx
->b
.set_shader_images
= si_set_shader_images
;
2728 sctx
->b
.set_constant_buffer
= si_pipe_set_constant_buffer
;
2729 sctx
->b
.set_shader_buffers
= si_set_shader_buffers
;
2730 sctx
->b
.set_sampler_views
= si_set_sampler_views
;
2731 sctx
->b
.create_texture_handle
= si_create_texture_handle
;
2732 sctx
->b
.delete_texture_handle
= si_delete_texture_handle
;
2733 sctx
->b
.make_texture_handle_resident
= si_make_texture_handle_resident
;
2734 sctx
->b
.create_image_handle
= si_create_image_handle
;
2735 sctx
->b
.delete_image_handle
= si_delete_image_handle
;
2736 sctx
->b
.make_image_handle_resident
= si_make_image_handle_resident
;
2738 if (!sctx
->has_graphics
)
2741 sctx
->b
.set_polygon_stipple
= si_set_polygon_stipple
;
2743 /* Shader user data. */
2744 sctx
->atoms
.s
.shader_pointers
.emit
= si_emit_graphics_shader_pointers
;
2746 /* Set default and immutable mappings. */
2747 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
, R_00B130_SPI_SHADER_USER_DATA_VS_0
);
2749 if (sctx
->chip_class
>= GFX9
) {
2750 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_CTRL
,
2751 R_00B430_SPI_SHADER_USER_DATA_LS_0
);
2752 si_set_user_data_base(sctx
, PIPE_SHADER_GEOMETRY
,
2753 R_00B330_SPI_SHADER_USER_DATA_ES_0
);
2755 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_CTRL
,
2756 R_00B430_SPI_SHADER_USER_DATA_HS_0
);
2757 si_set_user_data_base(sctx
, PIPE_SHADER_GEOMETRY
,
2758 R_00B230_SPI_SHADER_USER_DATA_GS_0
);
2760 si_set_user_data_base(sctx
, PIPE_SHADER_FRAGMENT
, R_00B030_SPI_SHADER_USER_DATA_PS_0
);
2763 static bool si_upload_shader_descriptors(struct si_context
*sctx
, unsigned mask
)
2765 unsigned dirty
= sctx
->descriptors_dirty
& mask
;
2767 /* Assume nothing will go wrong: */
2768 sctx
->shader_pointers_dirty
|= dirty
;
2771 unsigned i
= u_bit_scan(&dirty
);
2773 if (!si_upload_descriptors(sctx
, &sctx
->descriptors
[i
]))
2777 sctx
->descriptors_dirty
&= ~mask
;
2779 si_upload_bindless_descriptors(sctx
);
2784 bool si_upload_graphics_shader_descriptors(struct si_context
*sctx
)
2786 const unsigned mask
= u_bit_consecutive(0, SI_DESCS_FIRST_COMPUTE
);
2787 return si_upload_shader_descriptors(sctx
, mask
);
2790 bool si_upload_compute_shader_descriptors(struct si_context
*sctx
)
2792 /* Does not update rw_buffers as that is not needed for compute shaders
2793 * and the input buffer is using the same SGPR's anyway.
2795 const unsigned mask
= u_bit_consecutive(SI_DESCS_FIRST_COMPUTE
,
2796 SI_NUM_DESCS
- SI_DESCS_FIRST_COMPUTE
);
2797 return si_upload_shader_descriptors(sctx
, mask
);
2800 void si_release_all_descriptors(struct si_context
*sctx
)
2804 for (i
= 0; i
< SI_NUM_SHADERS
; i
++) {
2805 si_release_buffer_resources(&sctx
->const_and_shader_buffers
[i
],
2806 si_const_and_shader_buffer_descriptors(sctx
, i
));
2807 si_release_sampler_views(&sctx
->samplers
[i
]);
2808 si_release_image_views(&sctx
->images
[i
]);
2810 si_release_buffer_resources(&sctx
->rw_buffers
,
2811 &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
]);
2812 for (i
= 0; i
< SI_NUM_VERTEX_BUFFERS
; i
++)
2813 pipe_vertex_buffer_unreference(&sctx
->vertex_buffer
[i
]);
2815 for (i
= 0; i
< SI_NUM_DESCS
; ++i
)
2816 si_release_descriptors(&sctx
->descriptors
[i
]);
2818 si_resource_reference(&sctx
->vb_descriptors_buffer
, NULL
);
2819 sctx
->vb_descriptors_gpu_list
= NULL
; /* points into a mapped buffer */
2821 si_release_bindless_descriptors(sctx
);
2824 void si_all_descriptors_begin_new_cs(struct si_context
*sctx
)
2828 for (i
= 0; i
< SI_NUM_SHADERS
; i
++) {
2829 si_buffer_resources_begin_new_cs(sctx
, &sctx
->const_and_shader_buffers
[i
]);
2830 si_sampler_views_begin_new_cs(sctx
, &sctx
->samplers
[i
]);
2831 si_image_views_begin_new_cs(sctx
, &sctx
->images
[i
]);
2833 si_buffer_resources_begin_new_cs(sctx
, &sctx
->rw_buffers
);
2834 si_vertex_buffers_begin_new_cs(sctx
);
2836 for (i
= 0; i
< SI_NUM_DESCS
; ++i
)
2837 si_descriptors_begin_new_cs(sctx
, &sctx
->descriptors
[i
]);
2838 si_descriptors_begin_new_cs(sctx
, &sctx
->bindless_descriptors
);
2840 si_shader_pointers_begin_new_cs(sctx
);
2843 void si_set_active_descriptors(struct si_context
*sctx
, unsigned desc_idx
,
2844 uint64_t new_active_mask
)
2846 struct si_descriptors
*desc
= &sctx
->descriptors
[desc_idx
];
2848 /* Ignore no-op updates and updates that disable all slots. */
2849 if (!new_active_mask
||
2850 new_active_mask
== u_bit_consecutive64(desc
->first_active_slot
,
2851 desc
->num_active_slots
))
2855 u_bit_scan_consecutive_range64(&new_active_mask
, &first
, &count
);
2856 assert(new_active_mask
== 0);
2858 /* Upload/dump descriptors if slots are being enabled. */
2859 if (first
< desc
->first_active_slot
||
2860 first
+ count
> desc
->first_active_slot
+ desc
->num_active_slots
)
2861 sctx
->descriptors_dirty
|= 1u << desc_idx
;
2863 desc
->first_active_slot
= first
;
2864 desc
->num_active_slots
= count
;
2867 void si_set_active_descriptors_for_shader(struct si_context
*sctx
,
2868 struct si_shader_selector
*sel
)
2873 si_set_active_descriptors(sctx
,
2874 si_const_and_shader_buffer_descriptors_idx(sel
->type
),
2875 sel
->active_const_and_shader_buffers
);
2876 si_set_active_descriptors(sctx
,
2877 si_sampler_and_image_descriptors_idx(sel
->type
),
2878 sel
->active_samplers_and_images
);