2 * Copyright 2013 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * Marek Olšák <marek.olsak@amd.com>
27 /* Resource binding slots and sampler states (each described with 8 or
28 * 4 dwords) are stored in lists in memory which is accessed by shaders
29 * using scalar load instructions.
31 * This file is responsible for managing such lists. It keeps a copy of all
32 * descriptors in CPU memory and re-uploads a whole list if some slots have
35 * This code is also reponsible for updating shader pointers to those lists.
37 * Note that CP DMA can't be used for updating the lists, because a GPU hang
38 * could leave the list in a mid-IB state and the next IB would get wrong
39 * descriptors and the whole context would be unusable at that point.
40 * (Note: The register shadowing can't be used due to the same reason)
42 * Also, uploading descriptors to newly allocated memory doesn't require
46 * Possible scenarios for one 16 dword image+sampler slot:
48 * | Image | w/ FMASK | Buffer | NULL
49 * [ 0: 3] Image[0:3] | Image[0:3] | Null[0:3] | Null[0:3]
50 * [ 4: 7] Image[4:7] | Image[4:7] | Buffer[0:3] | 0
51 * [ 8:11] Null[0:3] | Fmask[0:3] | Null[0:3] | Null[0:3]
52 * [12:15] Sampler[0:3] | Fmask[4:7] | Sampler[0:3] | Sampler[0:3]
54 * FMASK implies MSAA, therefore no sampler state.
55 * Sampler states are never unbound except when FMASK is bound.
58 #include "radeon/r600_cs.h"
63 #include "util/hash_table.h"
64 #include "util/u_format.h"
65 #include "util/u_memory.h"
66 #include "util/u_upload_mgr.h"
69 /* NULL image and buffer descriptor for textures (alpha = 1) and images
72 * For images, all fields must be zero except for the swizzle, which
73 * supports arbitrary combinations of 0s and 1s. The texture type must be
74 * any valid type (e.g. 1D). If the texture type isn't set, the hw hangs.
76 * For buffers, all fields must be zero. If they are not, the hw hangs.
78 * This is the only reason why the buffer descriptor must be in words [4:7].
80 static uint32_t null_texture_descriptor
[8] = {
84 S_008F1C_DST_SEL_W(V_008F1C_SQ_SEL_1
) |
85 S_008F1C_TYPE(V_008F1C_SQ_RSRC_IMG_1D
)
86 /* the rest must contain zeros, which is also used by the buffer
90 static uint32_t null_image_descriptor
[8] = {
94 S_008F1C_TYPE(V_008F1C_SQ_RSRC_IMG_1D
)
95 /* the rest must contain zeros, which is also used by the buffer
99 static void si_init_descriptor_list(uint32_t *desc_list
,
100 unsigned element_dw_size
,
101 unsigned num_elements
,
102 const uint32_t *null_descriptor
)
106 /* Initialize the array to NULL descriptors if the element size is 8. */
107 if (null_descriptor
) {
108 assert(element_dw_size
% 8 == 0);
109 for (i
= 0; i
< num_elements
* element_dw_size
/ 8; i
++)
110 memcpy(desc_list
+ i
* 8, null_descriptor
, 8 * 4);
114 static void si_init_descriptors(struct si_context
*sctx
,
115 struct si_descriptors
*desc
,
116 unsigned shader_userdata_index
,
117 unsigned element_dw_size
,
118 unsigned num_elements
,
119 unsigned first_ce_slot
,
120 unsigned num_ce_slots
,
123 assert(num_elements
<= sizeof(desc
->dirty_mask
)*8);
125 desc
->list
= CALLOC(num_elements
, element_dw_size
* 4);
126 desc
->element_dw_size
= element_dw_size
;
127 desc
->num_elements
= num_elements
;
128 desc
->first_ce_slot
= sctx
->ce_ib
? first_ce_slot
: 0;
129 desc
->num_ce_slots
= sctx
->ce_ib
? num_ce_slots
: 0;
130 desc
->dirty_mask
= u_bit_consecutive64(0, num_elements
);
131 desc
->shader_userdata_offset
= shader_userdata_index
* 4;
133 if (desc
->num_ce_slots
) {
134 desc
->uses_ce
= true;
135 desc
->ce_offset
= *ce_offset
;
137 *ce_offset
+= element_dw_size
* desc
->num_ce_slots
* 4;
141 static void si_release_descriptors(struct si_descriptors
*desc
)
143 r600_resource_reference(&desc
->buffer
, NULL
);
147 static bool si_ce_upload(struct si_context
*sctx
, unsigned ce_offset
, unsigned size
,
148 unsigned *out_offset
, struct r600_resource
**out_buf
)
152 u_suballocator_alloc(sctx
->ce_suballocator
, size
,
153 si_optimal_tcc_alignment(sctx
, size
),
155 (struct pipe_resource
**)out_buf
);
159 va
= (*out_buf
)->gpu_address
+ *out_offset
;
161 radeon_emit(sctx
->ce_ib
, PKT3(PKT3_DUMP_CONST_RAM
, 3, 0));
162 radeon_emit(sctx
->ce_ib
, ce_offset
);
163 radeon_emit(sctx
->ce_ib
, size
/ 4);
164 radeon_emit(sctx
->ce_ib
, va
);
165 radeon_emit(sctx
->ce_ib
, va
>> 32);
167 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, *out_buf
,
168 RADEON_USAGE_READWRITE
, RADEON_PRIO_DESCRIPTORS
);
170 sctx
->ce_need_synchronization
= true;
174 void si_ce_save_all_descriptors_at_ib_end(struct si_context
* sctx
)
176 bool success
= si_ce_upload(sctx
, 0, sctx
->total_ce_ram_allocated
,
177 &sctx
->ce_ram_saved_offset
,
178 &sctx
->ce_ram_saved_buffer
);
183 void si_ce_restore_all_descriptors_at_ib_start(struct si_context
*sctx
)
185 if (!sctx
->ce_ram_saved_buffer
)
188 struct radeon_winsys_cs
*ib
= sctx
->ce_preamble_ib
;
192 uint64_t va
= sctx
->ce_ram_saved_buffer
->gpu_address
+
193 sctx
->ce_ram_saved_offset
;
195 radeon_emit(ib
, PKT3(PKT3_LOAD_CONST_RAM
, 3, 0));
197 radeon_emit(ib
, va
>> 32);
198 radeon_emit(ib
, sctx
->total_ce_ram_allocated
/ 4);
201 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
202 sctx
->ce_ram_saved_buffer
,
203 RADEON_USAGE_READ
, RADEON_PRIO_DESCRIPTORS
);
206 void si_ce_enable_loads(struct radeon_winsys_cs
*ib
)
208 radeon_emit(ib
, PKT3(PKT3_CONTEXT_CONTROL
, 1, 0));
209 radeon_emit(ib
, CONTEXT_CONTROL_LOAD_ENABLE(1) |
210 CONTEXT_CONTROL_LOAD_CE_RAM(1));
211 radeon_emit(ib
, CONTEXT_CONTROL_SHADOW_ENABLE(1));
214 static bool si_upload_descriptors(struct si_context
*sctx
,
215 struct si_descriptors
*desc
,
216 struct r600_atom
* atom
)
218 unsigned slot_size
= desc
->element_dw_size
* 4;
219 unsigned first_slot_offset
= desc
->first_active_slot
* slot_size
;
220 unsigned upload_size
= desc
->num_active_slots
* slot_size
;
222 /* Skip the upload if no shader is using the descriptors. dirty_mask
223 * will stay dirty and the descriptors will be uploaded when there is
224 * a shader using them.
230 const uint32_t *list
= desc
->list
+
231 desc
->first_ce_slot
* desc
->element_dw_size
;
232 uint64_t mask
= (desc
->dirty_mask
>> desc
->first_ce_slot
) &
233 u_bit_consecutive64(0, desc
->num_ce_slots
);
238 u_bit_scan_consecutive_range64(&mask
, &begin
, &count
);
240 begin
*= desc
->element_dw_size
;
241 count
*= desc
->element_dw_size
;
243 radeon_emit(sctx
->ce_ib
,
244 PKT3(PKT3_WRITE_CONST_RAM
, count
, 0));
245 radeon_emit(sctx
->ce_ib
, desc
->ce_offset
+ begin
* 4);
246 radeon_emit_array(sctx
->ce_ib
, list
+ begin
, count
);
249 if (!si_ce_upload(sctx
,
251 (first_slot_offset
- desc
->first_ce_slot
* slot_size
),
252 upload_size
, (unsigned*)&desc
->buffer_offset
,
258 u_upload_alloc(sctx
->b
.b
.const_uploader
, 0, upload_size
,
259 si_optimal_tcc_alignment(sctx
, upload_size
),
260 (unsigned*)&desc
->buffer_offset
,
261 (struct pipe_resource
**)&desc
->buffer
,
264 return false; /* skip the draw call */
266 util_memcpy_cpu_to_le32(ptr
, (char*)desc
->list
+ first_slot_offset
,
268 desc
->gpu_list
= ptr
- first_slot_offset
/ 4;
270 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, desc
->buffer
,
271 RADEON_USAGE_READ
, RADEON_PRIO_DESCRIPTORS
);
274 /* The shader pointer should point to slot 0. */
275 desc
->buffer_offset
-= first_slot_offset
;
277 desc
->dirty_mask
= 0;
280 si_mark_atom_dirty(sctx
, atom
);
286 si_descriptors_begin_new_cs(struct si_context
*sctx
, struct si_descriptors
*desc
)
291 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, desc
->buffer
,
292 RADEON_USAGE_READ
, RADEON_PRIO_DESCRIPTORS
);
298 si_sampler_and_image_descriptors_idx(unsigned shader
)
300 return SI_DESCS_FIRST_SHADER
+ shader
* SI_NUM_SHADER_DESCS
+
301 SI_SHADER_DESCS_SAMPLERS_AND_IMAGES
;
304 static struct si_descriptors
*
305 si_sampler_and_image_descriptors(struct si_context
*sctx
, unsigned shader
)
307 return &sctx
->descriptors
[si_sampler_and_image_descriptors_idx(shader
)];
310 static void si_release_sampler_views(struct si_sampler_views
*views
)
314 for (i
= 0; i
< ARRAY_SIZE(views
->views
); i
++) {
315 pipe_sampler_view_reference(&views
->views
[i
], NULL
);
319 static void si_sampler_view_add_buffer(struct si_context
*sctx
,
320 struct pipe_resource
*resource
,
321 enum radeon_bo_usage usage
,
322 bool is_stencil_sampler
,
325 struct r600_resource
*rres
;
326 struct r600_texture
*rtex
;
327 enum radeon_bo_priority priority
;
332 if (resource
->target
!= PIPE_BUFFER
) {
333 struct r600_texture
*tex
= (struct r600_texture
*)resource
;
335 if (tex
->is_depth
&& !r600_can_sample_zs(tex
, is_stencil_sampler
))
336 resource
= &tex
->flushed_depth_texture
->resource
.b
.b
;
339 rres
= (struct r600_resource
*)resource
;
340 priority
= r600_get_sampler_view_priority(rres
);
342 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
343 rres
, usage
, priority
,
346 if (resource
->target
== PIPE_BUFFER
)
349 /* Now add separate DCC or HTILE. */
350 rtex
= (struct r600_texture
*)resource
;
351 if (rtex
->dcc_separate_buffer
) {
352 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
353 rtex
->dcc_separate_buffer
, usage
,
354 RADEON_PRIO_DCC
, check_mem
);
358 static void si_sampler_views_begin_new_cs(struct si_context
*sctx
,
359 struct si_sampler_views
*views
)
361 unsigned mask
= views
->enabled_mask
;
363 /* Add buffers to the CS. */
365 int i
= u_bit_scan(&mask
);
366 struct si_sampler_view
*sview
= (struct si_sampler_view
*)views
->views
[i
];
368 si_sampler_view_add_buffer(sctx
, sview
->base
.texture
,
370 sview
->is_stencil_sampler
, false);
374 /* Set buffer descriptor fields that can be changed by reallocations. */
375 static void si_set_buf_desc_address(struct r600_resource
*buf
,
376 uint64_t offset
, uint32_t *state
)
378 uint64_t va
= buf
->gpu_address
+ offset
;
381 state
[1] &= C_008F04_BASE_ADDRESS_HI
;
382 state
[1] |= S_008F04_BASE_ADDRESS_HI(va
>> 32);
385 /* Set texture descriptor fields that can be changed by reallocations.
388 * \param base_level_info information of the level of BASE_ADDRESS
389 * \param base_level the level of BASE_ADDRESS
390 * \param first_level pipe_sampler_view.u.tex.first_level
391 * \param block_width util_format_get_blockwidth()
392 * \param is_stencil select between separate Z & Stencil
393 * \param state descriptor to update
395 void si_set_mutable_tex_desc_fields(struct si_screen
*sscreen
,
396 struct r600_texture
*tex
,
397 const struct legacy_surf_level
*base_level_info
,
398 unsigned base_level
, unsigned first_level
,
399 unsigned block_width
, bool is_stencil
,
402 uint64_t va
, meta_va
= 0;
404 if (tex
->is_depth
&& !r600_can_sample_zs(tex
, is_stencil
)) {
405 tex
= tex
->flushed_depth_texture
;
409 va
= tex
->resource
.gpu_address
;
411 if (sscreen
->b
.chip_class
>= GFX9
) {
412 /* Only stencil_offset needs to be added here. */
414 va
+= tex
->surface
.u
.gfx9
.stencil_offset
;
416 va
+= tex
->surface
.u
.gfx9
.surf_offset
;
418 va
+= base_level_info
->offset
;
422 state
[1] &= C_008F14_BASE_ADDRESS_HI
;
423 state
[1] |= S_008F14_BASE_ADDRESS_HI(va
>> 40);
425 if (sscreen
->b
.chip_class
>= VI
) {
426 state
[6] &= C_008F28_COMPRESSION_EN
;
429 if (vi_dcc_enabled(tex
, first_level
)) {
430 meta_va
= (!tex
->dcc_separate_buffer
? tex
->resource
.gpu_address
: 0) +
433 if (sscreen
->b
.chip_class
<= VI
)
434 meta_va
+= base_level_info
->dcc_offset
;
435 } else if (tex
->tc_compatible_htile
) {
436 meta_va
= tex
->resource
.gpu_address
+ tex
->htile_offset
;
440 state
[6] |= S_008F28_COMPRESSION_EN(1);
441 state
[7] = meta_va
>> 8;
445 if (sscreen
->b
.chip_class
>= GFX9
) {
446 state
[3] &= C_008F1C_SW_MODE
;
447 state
[4] &= C_008F20_PITCH_GFX9
;
450 state
[3] |= S_008F1C_SW_MODE(tex
->surface
.u
.gfx9
.stencil
.swizzle_mode
);
451 state
[4] |= S_008F20_PITCH_GFX9(tex
->surface
.u
.gfx9
.stencil
.epitch
);
453 state
[3] |= S_008F1C_SW_MODE(tex
->surface
.u
.gfx9
.surf
.swizzle_mode
);
454 state
[4] |= S_008F20_PITCH_GFX9(tex
->surface
.u
.gfx9
.surf
.epitch
);
457 state
[5] &= C_008F24_META_DATA_ADDRESS
&
458 C_008F24_META_PIPE_ALIGNED
&
459 C_008F24_META_RB_ALIGNED
;
461 struct gfx9_surf_meta_flags meta
;
464 meta
= tex
->surface
.u
.gfx9
.dcc
;
466 meta
= tex
->surface
.u
.gfx9
.htile
;
468 state
[5] |= S_008F24_META_DATA_ADDRESS(meta_va
>> 40) |
469 S_008F24_META_PIPE_ALIGNED(meta
.pipe_aligned
) |
470 S_008F24_META_RB_ALIGNED(meta
.rb_aligned
);
474 unsigned pitch
= base_level_info
->nblk_x
* block_width
;
475 unsigned index
= si_tile_mode_index(tex
, base_level
, is_stencil
);
477 state
[3] &= C_008F1C_TILING_INDEX
;
478 state
[3] |= S_008F1C_TILING_INDEX(index
);
479 state
[4] &= C_008F20_PITCH_GFX6
;
480 state
[4] |= S_008F20_PITCH_GFX6(pitch
- 1);
484 static void si_set_sampler_view_desc(struct si_context
*sctx
,
485 struct si_sampler_view
*sview
,
486 struct si_sampler_state
*sstate
,
489 struct pipe_sampler_view
*view
= &sview
->base
;
490 struct r600_texture
*rtex
= (struct r600_texture
*)view
->texture
;
491 bool is_buffer
= rtex
->resource
.b
.b
.target
== PIPE_BUFFER
;
493 if (unlikely(!is_buffer
&& sview
->dcc_incompatible
)) {
494 if (vi_dcc_enabled(rtex
, view
->u
.tex
.first_level
))
495 if (!r600_texture_disable_dcc(&sctx
->b
, rtex
))
496 sctx
->b
.decompress_dcc(&sctx
->b
.b
, rtex
);
498 sview
->dcc_incompatible
= false;
501 assert(rtex
); /* views with texture == NULL aren't supported */
502 memcpy(desc
, sview
->state
, 8*4);
505 si_set_buf_desc_address(&rtex
->resource
,
506 sview
->base
.u
.buf
.offset
,
509 bool is_separate_stencil
= rtex
->db_compatible
&&
510 sview
->is_stencil_sampler
;
512 si_set_mutable_tex_desc_fields(sctx
->screen
, rtex
,
513 sview
->base_level_info
,
515 sview
->base
.u
.tex
.first_level
,
521 if (!is_buffer
&& rtex
->fmask
.size
) {
522 memcpy(desc
+ 8, sview
->fmask_state
, 8*4);
524 /* Disable FMASK and bind sampler state in [12:15]. */
525 memcpy(desc
+ 8, null_texture_descriptor
, 4*4);
528 memcpy(desc
+ 12, sstate
->val
, 4*4);
532 static void si_set_sampler_view(struct si_context
*sctx
,
534 unsigned slot
, struct pipe_sampler_view
*view
,
535 bool disallow_early_out
)
537 struct si_sampler_views
*views
= &sctx
->samplers
[shader
].views
;
538 struct si_sampler_view
*rview
= (struct si_sampler_view
*)view
;
539 struct si_descriptors
*descs
= si_sampler_and_image_descriptors(sctx
, shader
);
540 unsigned desc_slot
= si_get_sampler_slot(slot
);
541 uint32_t *desc
= descs
->list
+ desc_slot
* 16;
543 if (views
->views
[slot
] == view
&& !disallow_early_out
)
547 struct r600_texture
*rtex
= (struct r600_texture
*)view
->texture
;
549 si_set_sampler_view_desc(sctx
, rview
,
550 views
->sampler_states
[slot
], desc
);
552 if (rtex
->resource
.b
.b
.target
== PIPE_BUFFER
)
553 rtex
->resource
.bind_history
|= PIPE_BIND_SAMPLER_VIEW
;
555 pipe_sampler_view_reference(&views
->views
[slot
], view
);
556 views
->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 rview
->is_stencil_sampler
, true);
564 pipe_sampler_view_reference(&views
->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 (views
->sampler_states
[slot
])
571 views
->sampler_states
[slot
]->val
, 4*4);
573 views
->enabled_mask
&= ~(1u << slot
);
576 descs
->dirty_mask
|= 1ull << desc_slot
;
577 sctx
->descriptors_dirty
|= 1u << si_sampler_and_image_descriptors_idx(shader
);
580 static bool color_needs_decompression(struct r600_texture
*rtex
)
582 return rtex
->fmask
.size
||
583 (rtex
->dirty_level_mask
&&
584 (rtex
->cmask
.size
|| rtex
->dcc_offset
));
587 static bool depth_needs_decompression(struct r600_texture
*rtex
,
588 struct si_sampler_view
*sview
)
590 return rtex
->db_compatible
&&
591 (!rtex
->tc_compatible_htile
||
592 !r600_can_sample_zs(rtex
, sview
->is_stencil_sampler
));
595 static void si_update_shader_needs_decompress_mask(struct si_context
*sctx
,
598 struct si_textures_info
*samplers
= &sctx
->samplers
[shader
];
599 unsigned shader_bit
= 1 << shader
;
601 if (samplers
->needs_depth_decompress_mask
||
602 samplers
->needs_color_decompress_mask
||
603 sctx
->images
[shader
].needs_color_decompress_mask
)
604 sctx
->shader_needs_decompress_mask
|= shader_bit
;
606 sctx
->shader_needs_decompress_mask
&= ~shader_bit
;
609 static void si_set_sampler_views(struct pipe_context
*ctx
,
610 enum pipe_shader_type shader
, unsigned start
,
612 struct pipe_sampler_view
**views
)
614 struct si_context
*sctx
= (struct si_context
*)ctx
;
615 struct si_textures_info
*samplers
= &sctx
->samplers
[shader
];
618 if (!count
|| shader
>= SI_NUM_SHADERS
)
621 for (i
= 0; i
< count
; i
++) {
622 unsigned slot
= start
+ i
;
624 if (!views
|| !views
[i
]) {
625 samplers
->needs_depth_decompress_mask
&= ~(1u << slot
);
626 samplers
->needs_color_decompress_mask
&= ~(1u << slot
);
627 si_set_sampler_view(sctx
, shader
, slot
, NULL
, false);
631 si_set_sampler_view(sctx
, shader
, slot
, views
[i
], false);
633 if (views
[i
]->texture
&& views
[i
]->texture
->target
!= PIPE_BUFFER
) {
634 struct r600_texture
*rtex
=
635 (struct r600_texture
*)views
[i
]->texture
;
636 struct si_sampler_view
*rview
= (struct si_sampler_view
*)views
[i
];
638 if (depth_needs_decompression(rtex
, rview
)) {
639 samplers
->needs_depth_decompress_mask
|= 1u << slot
;
641 samplers
->needs_depth_decompress_mask
&= ~(1u << slot
);
643 if (color_needs_decompression(rtex
)) {
644 samplers
->needs_color_decompress_mask
|= 1u << slot
;
646 samplers
->needs_color_decompress_mask
&= ~(1u << slot
);
649 if (rtex
->dcc_offset
&&
650 p_atomic_read(&rtex
->framebuffers_bound
))
651 sctx
->need_check_render_feedback
= true;
653 samplers
->needs_depth_decompress_mask
&= ~(1u << slot
);
654 samplers
->needs_color_decompress_mask
&= ~(1u << slot
);
658 si_update_shader_needs_decompress_mask(sctx
, shader
);
662 si_samplers_update_needs_color_decompress_mask(struct si_textures_info
*samplers
)
664 unsigned mask
= samplers
->views
.enabled_mask
;
667 int i
= u_bit_scan(&mask
);
668 struct pipe_resource
*res
= samplers
->views
.views
[i
]->texture
;
670 if (res
&& res
->target
!= PIPE_BUFFER
) {
671 struct r600_texture
*rtex
= (struct r600_texture
*)res
;
673 if (color_needs_decompression(rtex
)) {
674 samplers
->needs_color_decompress_mask
|= 1u << i
;
676 samplers
->needs_color_decompress_mask
&= ~(1u << i
);
685 si_release_image_views(struct si_images_info
*images
)
689 for (i
= 0; i
< SI_NUM_IMAGES
; ++i
) {
690 struct pipe_image_view
*view
= &images
->views
[i
];
692 pipe_resource_reference(&view
->resource
, NULL
);
697 si_image_views_begin_new_cs(struct si_context
*sctx
, struct si_images_info
*images
)
699 uint mask
= images
->enabled_mask
;
701 /* Add buffers to the CS. */
703 int i
= u_bit_scan(&mask
);
704 struct pipe_image_view
*view
= &images
->views
[i
];
706 assert(view
->resource
);
708 si_sampler_view_add_buffer(sctx
, view
->resource
,
709 RADEON_USAGE_READWRITE
, false, false);
714 si_disable_shader_image(struct si_context
*ctx
, unsigned shader
, unsigned slot
)
716 struct si_images_info
*images
= &ctx
->images
[shader
];
718 if (images
->enabled_mask
& (1u << slot
)) {
719 struct si_descriptors
*descs
= si_sampler_and_image_descriptors(ctx
, shader
);
720 unsigned desc_slot
= si_get_image_slot(slot
);
722 pipe_resource_reference(&images
->views
[slot
].resource
, NULL
);
723 images
->needs_color_decompress_mask
&= ~(1 << slot
);
725 memcpy(descs
->list
+ desc_slot
*8, null_image_descriptor
, 8*4);
726 images
->enabled_mask
&= ~(1u << slot
);
727 /* two 8-byte images share one 16-byte slot */
728 descs
->dirty_mask
|= 1u << (desc_slot
/ 2);
729 ctx
->descriptors_dirty
|= 1u << si_sampler_and_image_descriptors_idx(shader
);
734 si_mark_image_range_valid(const struct pipe_image_view
*view
)
736 struct r600_resource
*res
= (struct r600_resource
*)view
->resource
;
738 assert(res
&& res
->b
.b
.target
== PIPE_BUFFER
);
740 util_range_add(&res
->valid_buffer_range
,
742 view
->u
.buf
.offset
+ view
->u
.buf
.size
);
745 static void si_set_shader_image_desc(struct si_context
*ctx
,
746 const struct pipe_image_view
*view
,
747 bool skip_decompress
,
750 struct si_screen
*screen
= ctx
->screen
;
751 struct r600_resource
*res
;
753 res
= (struct r600_resource
*)view
->resource
;
755 if (res
->b
.b
.target
== PIPE_BUFFER
) {
756 if (view
->access
& PIPE_IMAGE_ACCESS_WRITE
)
757 si_mark_image_range_valid(view
);
759 si_make_buffer_descriptor(screen
, res
,
762 view
->u
.buf
.size
, desc
);
763 si_set_buf_desc_address(res
, view
->u
.buf
.offset
, desc
+ 4);
765 static const unsigned char swizzle
[4] = { 0, 1, 2, 3 };
766 struct r600_texture
*tex
= (struct r600_texture
*)res
;
767 unsigned level
= view
->u
.tex
.level
;
768 unsigned width
, height
, depth
, hw_level
;
769 bool uses_dcc
= vi_dcc_enabled(tex
, level
);
771 assert(!tex
->is_depth
);
772 assert(tex
->fmask
.size
== 0);
774 if (uses_dcc
&& !skip_decompress
&&
775 (view
->access
& PIPE_IMAGE_ACCESS_WRITE
||
776 !vi_dcc_formats_compatible(res
->b
.b
.format
, view
->format
))) {
777 /* If DCC can't be disabled, at least decompress it.
778 * The decompression is relatively cheap if the surface
779 * has been decompressed already.
781 if (!r600_texture_disable_dcc(&ctx
->b
, tex
))
782 ctx
->b
.decompress_dcc(&ctx
->b
.b
, tex
);
785 if (ctx
->b
.chip_class
>= GFX9
) {
786 /* Always set the base address. The swizzle modes don't
787 * allow setting mipmap level offsets as the base.
789 width
= res
->b
.b
.width0
;
790 height
= res
->b
.b
.height0
;
791 depth
= res
->b
.b
.depth0
;
794 /* Always force the base level to the selected level.
796 * This is required for 3D textures, where otherwise
797 * selecting a single slice for non-layered bindings
798 * fails. It doesn't hurt the other targets.
800 width
= u_minify(res
->b
.b
.width0
, level
);
801 height
= u_minify(res
->b
.b
.height0
, level
);
802 depth
= u_minify(res
->b
.b
.depth0
, level
);
806 si_make_texture_descriptor(screen
, tex
,
807 false, res
->b
.b
.target
,
808 view
->format
, swizzle
,
810 view
->u
.tex
.first_layer
,
811 view
->u
.tex
.last_layer
,
812 width
, height
, depth
,
814 si_set_mutable_tex_desc_fields(screen
, tex
,
815 &tex
->surface
.u
.legacy
.level
[level
],
817 util_format_get_blockwidth(view
->format
),
822 static void si_set_shader_image(struct si_context
*ctx
,
824 unsigned slot
, const struct pipe_image_view
*view
,
825 bool skip_decompress
)
827 struct si_images_info
*images
= &ctx
->images
[shader
];
828 struct si_descriptors
*descs
= si_sampler_and_image_descriptors(ctx
, shader
);
829 struct r600_resource
*res
;
830 unsigned desc_slot
= si_get_image_slot(slot
);
831 uint32_t *desc
= descs
->list
+ desc_slot
* 8;
833 if (!view
|| !view
->resource
) {
834 si_disable_shader_image(ctx
, shader
, slot
);
838 res
= (struct r600_resource
*)view
->resource
;
840 if (&images
->views
[slot
] != view
)
841 util_copy_image_view(&images
->views
[slot
], view
);
843 si_set_shader_image_desc(ctx
, view
, skip_decompress
, desc
);
845 if (res
->b
.b
.target
== PIPE_BUFFER
) {
846 images
->needs_color_decompress_mask
&= ~(1 << slot
);
847 res
->bind_history
|= PIPE_BIND_SHADER_IMAGE
;
849 struct r600_texture
*tex
= (struct r600_texture
*)res
;
850 unsigned level
= view
->u
.tex
.level
;
852 if (color_needs_decompression(tex
)) {
853 images
->needs_color_decompress_mask
|= 1 << slot
;
855 images
->needs_color_decompress_mask
&= ~(1 << slot
);
858 if (vi_dcc_enabled(tex
, level
) &&
859 p_atomic_read(&tex
->framebuffers_bound
))
860 ctx
->need_check_render_feedback
= true;
863 images
->enabled_mask
|= 1u << slot
;
864 /* two 8-byte images share one 16-byte slot */
865 descs
->dirty_mask
|= 1u << (desc_slot
/ 2);
866 ctx
->descriptors_dirty
|= 1u << si_sampler_and_image_descriptors_idx(shader
);
868 /* Since this can flush, it must be done after enabled_mask is updated. */
869 si_sampler_view_add_buffer(ctx
, &res
->b
.b
,
870 RADEON_USAGE_READWRITE
, false, true);
874 si_set_shader_images(struct pipe_context
*pipe
,
875 enum pipe_shader_type shader
,
876 unsigned start_slot
, unsigned count
,
877 const struct pipe_image_view
*views
)
879 struct si_context
*ctx
= (struct si_context
*)pipe
;
882 assert(shader
< SI_NUM_SHADERS
);
887 assert(start_slot
+ count
<= SI_NUM_IMAGES
);
890 for (i
= 0, slot
= start_slot
; i
< count
; ++i
, ++slot
)
891 si_set_shader_image(ctx
, shader
, slot
, &views
[i
], false);
893 for (i
= 0, slot
= start_slot
; i
< count
; ++i
, ++slot
)
894 si_set_shader_image(ctx
, shader
, slot
, NULL
, false);
897 si_update_shader_needs_decompress_mask(ctx
, shader
);
901 si_images_update_needs_color_decompress_mask(struct si_images_info
*images
)
903 unsigned mask
= images
->enabled_mask
;
906 int i
= u_bit_scan(&mask
);
907 struct pipe_resource
*res
= images
->views
[i
].resource
;
909 if (res
&& res
->target
!= PIPE_BUFFER
) {
910 struct r600_texture
*rtex
= (struct r600_texture
*)res
;
912 if (color_needs_decompression(rtex
)) {
913 images
->needs_color_decompress_mask
|= 1 << i
;
915 images
->needs_color_decompress_mask
&= ~(1 << i
);
923 static void si_bind_sampler_states(struct pipe_context
*ctx
,
924 enum pipe_shader_type shader
,
925 unsigned start
, unsigned count
, void **states
)
927 struct si_context
*sctx
= (struct si_context
*)ctx
;
928 struct si_textures_info
*samplers
= &sctx
->samplers
[shader
];
929 struct si_descriptors
*desc
= si_sampler_and_image_descriptors(sctx
, shader
);
930 struct si_sampler_state
**sstates
= (struct si_sampler_state
**)states
;
933 if (!count
|| shader
>= SI_NUM_SHADERS
)
936 for (i
= 0; i
< count
; i
++) {
937 unsigned slot
= start
+ i
;
938 unsigned desc_slot
= si_get_sampler_slot(slot
);
941 sstates
[i
] == samplers
->views
.sampler_states
[slot
])
945 assert(sstates
[i
]->magic
== SI_SAMPLER_STATE_MAGIC
);
947 samplers
->views
.sampler_states
[slot
] = sstates
[i
];
949 /* If FMASK is bound, don't overwrite it.
950 * The sampler state will be set after FMASK is unbound.
952 if (samplers
->views
.views
[slot
] &&
953 samplers
->views
.views
[slot
]->texture
&&
954 samplers
->views
.views
[slot
]->texture
->target
!= PIPE_BUFFER
&&
955 ((struct r600_texture
*)samplers
->views
.views
[slot
]->texture
)->fmask
.size
)
958 memcpy(desc
->list
+ desc_slot
* 16 + 12, sstates
[i
]->val
, 4*4);
959 desc
->dirty_mask
|= 1ull << desc_slot
;
960 sctx
->descriptors_dirty
|= 1u << si_sampler_and_image_descriptors_idx(shader
);
964 /* BUFFER RESOURCES */
966 static void si_init_buffer_resources(struct si_context
*sctx
,
967 struct si_buffer_resources
*buffers
,
968 struct si_descriptors
*descs
,
969 unsigned num_buffers
,
970 unsigned first_ce_slot
,
971 unsigned num_ce_slots
,
972 unsigned shader_userdata_index
,
973 enum radeon_bo_usage shader_usage
,
974 enum radeon_bo_usage shader_usage_constbuf
,
975 enum radeon_bo_priority priority
,
976 enum radeon_bo_priority priority_constbuf
,
979 buffers
->shader_usage
= shader_usage
;
980 buffers
->shader_usage_constbuf
= shader_usage_constbuf
;
981 buffers
->priority
= priority
;
982 buffers
->priority_constbuf
= priority_constbuf
;
983 buffers
->buffers
= CALLOC(num_buffers
, sizeof(struct pipe_resource
*));
985 si_init_descriptors(sctx
, descs
, shader_userdata_index
, 4, num_buffers
,
986 first_ce_slot
, num_ce_slots
, ce_offset
);
989 static void si_release_buffer_resources(struct si_buffer_resources
*buffers
,
990 struct si_descriptors
*descs
)
994 for (i
= 0; i
< descs
->num_elements
; i
++) {
995 pipe_resource_reference(&buffers
->buffers
[i
], NULL
);
998 FREE(buffers
->buffers
);
1001 static void si_buffer_resources_begin_new_cs(struct si_context
*sctx
,
1002 struct si_buffer_resources
*buffers
)
1004 unsigned mask
= buffers
->enabled_mask
;
1006 /* Add buffers to the CS. */
1008 int i
= u_bit_scan(&mask
);
1010 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
1011 r600_resource(buffers
->buffers
[i
]),
1012 i
< SI_NUM_SHADER_BUFFERS
? buffers
->shader_usage
:
1013 buffers
->shader_usage_constbuf
,
1014 i
< SI_NUM_SHADER_BUFFERS
? buffers
->priority
:
1015 buffers
->priority_constbuf
);
1019 static void si_get_buffer_from_descriptors(struct si_buffer_resources
*buffers
,
1020 struct si_descriptors
*descs
,
1021 unsigned idx
, struct pipe_resource
**buf
,
1022 unsigned *offset
, unsigned *size
)
1024 pipe_resource_reference(buf
, buffers
->buffers
[idx
]);
1026 struct r600_resource
*res
= r600_resource(*buf
);
1027 const uint32_t *desc
= descs
->list
+ idx
* 4;
1032 assert(G_008F04_STRIDE(desc
[1]) == 0);
1033 va
= ((uint64_t)desc
[1] << 32) | desc
[0];
1035 assert(va
>= res
->gpu_address
&& va
+ *size
<= res
->gpu_address
+ res
->bo_size
);
1036 *offset
= va
- res
->gpu_address
;
1040 /* VERTEX BUFFERS */
1042 static void si_vertex_buffers_begin_new_cs(struct si_context
*sctx
)
1044 struct si_descriptors
*desc
= &sctx
->vertex_buffers
;
1045 int count
= sctx
->vertex_elements
? sctx
->vertex_elements
->count
: 0;
1048 for (i
= 0; i
< count
; i
++) {
1049 int vb
= sctx
->vertex_elements
->vertex_buffer_index
[i
];
1051 if (vb
>= ARRAY_SIZE(sctx
->vertex_buffer
))
1053 if (!sctx
->vertex_buffer
[vb
].buffer
.resource
)
1056 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
1057 (struct r600_resource
*)sctx
->vertex_buffer
[vb
].buffer
.resource
,
1058 RADEON_USAGE_READ
, RADEON_PRIO_VERTEX_BUFFER
);
1063 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
1064 desc
->buffer
, RADEON_USAGE_READ
,
1065 RADEON_PRIO_DESCRIPTORS
);
1068 bool si_upload_vertex_buffer_descriptors(struct si_context
*sctx
)
1070 struct si_vertex_elements
*velems
= sctx
->vertex_elements
;
1071 struct si_descriptors
*desc
= &sctx
->vertex_buffers
;
1073 unsigned desc_list_byte_size
;
1074 unsigned first_vb_use_mask
;
1078 if (!sctx
->vertex_buffers_dirty
|| !velems
)
1081 count
= velems
->count
;
1086 desc_list_byte_size
= velems
->desc_list_byte_size
;
1087 first_vb_use_mask
= velems
->first_vb_use_mask
;
1089 /* Vertex buffer descriptors are the only ones which are uploaded
1090 * directly through a staging buffer and don't go through
1091 * the fine-grained upload path.
1093 u_upload_alloc(sctx
->b
.b
.const_uploader
, 0,
1094 desc_list_byte_size
,
1095 si_optimal_tcc_alignment(sctx
, desc_list_byte_size
),
1096 (unsigned*)&desc
->buffer_offset
,
1097 (struct pipe_resource
**)&desc
->buffer
, (void**)&ptr
);
1101 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
1102 desc
->buffer
, RADEON_USAGE_READ
,
1103 RADEON_PRIO_DESCRIPTORS
);
1105 assert(count
<= SI_MAX_ATTRIBS
);
1107 for (i
= 0; i
< count
; i
++) {
1108 struct pipe_vertex_buffer
*vb
;
1109 struct r600_resource
*rbuffer
;
1111 unsigned vbo_index
= velems
->vertex_buffer_index
[i
];
1112 uint32_t *desc
= &ptr
[i
*4];
1114 vb
= &sctx
->vertex_buffer
[vbo_index
];
1115 rbuffer
= (struct r600_resource
*)vb
->buffer
.resource
;
1117 memset(desc
, 0, 16);
1121 offset
= vb
->buffer_offset
+ velems
->src_offset
[i
];
1122 va
= rbuffer
->gpu_address
+ offset
;
1124 /* Fill in T# buffer resource description */
1126 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1127 S_008F04_STRIDE(vb
->stride
);
1129 if (sctx
->b
.chip_class
!= VI
&& vb
->stride
) {
1130 /* Round up by rounding down and adding 1 */
1131 desc
[2] = (vb
->buffer
.resource
->width0
- offset
-
1132 velems
->format_size
[i
]) /
1135 desc
[2] = vb
->buffer
.resource
->width0
- offset
;
1138 desc
[3] = velems
->rsrc_word3
[i
];
1140 if (first_vb_use_mask
& (1 << i
)) {
1141 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
1142 (struct r600_resource
*)vb
->buffer
.resource
,
1143 RADEON_USAGE_READ
, RADEON_PRIO_VERTEX_BUFFER
);
1147 /* Don't flush the const cache. It would have a very negative effect
1148 * on performance (confirmed by testing). New descriptors are always
1149 * uploaded to a fresh new buffer, so I don't think flushing the const
1150 * cache is needed. */
1151 si_mark_atom_dirty(sctx
, &sctx
->shader_userdata
.atom
);
1152 if (sctx
->b
.chip_class
>= CIK
)
1153 si_mark_atom_dirty(sctx
, &sctx
->prefetch_L2
);
1154 sctx
->vertex_buffers_dirty
= false;
1155 sctx
->vertex_buffer_pointer_dirty
= true;
1160 /* CONSTANT BUFFERS */
1163 si_const_and_shader_buffer_descriptors_idx(unsigned shader
)
1165 return SI_DESCS_FIRST_SHADER
+ shader
* SI_NUM_SHADER_DESCS
+
1166 SI_SHADER_DESCS_CONST_AND_SHADER_BUFFERS
;
1169 static struct si_descriptors
*
1170 si_const_and_shader_buffer_descriptors(struct si_context
*sctx
, unsigned shader
)
1172 return &sctx
->descriptors
[si_const_and_shader_buffer_descriptors_idx(shader
)];
1175 void si_upload_const_buffer(struct si_context
*sctx
, struct r600_resource
**rbuffer
,
1176 const uint8_t *ptr
, unsigned size
, uint32_t *const_offset
)
1180 u_upload_alloc(sctx
->b
.b
.const_uploader
, 0, size
,
1181 si_optimal_tcc_alignment(sctx
, size
),
1183 (struct pipe_resource
**)rbuffer
, &tmp
);
1185 util_memcpy_cpu_to_le32(tmp
, ptr
, size
);
1188 static void si_set_constant_buffer(struct si_context
*sctx
,
1189 struct si_buffer_resources
*buffers
,
1190 unsigned descriptors_idx
,
1191 uint slot
, const struct pipe_constant_buffer
*input
)
1193 struct si_descriptors
*descs
= &sctx
->descriptors
[descriptors_idx
];
1194 assert(slot
< descs
->num_elements
);
1195 pipe_resource_reference(&buffers
->buffers
[slot
], NULL
);
1197 /* CIK cannot unbind a constant buffer (S_BUFFER_LOAD is buggy
1198 * with a NULL buffer). We need to use a dummy buffer instead. */
1199 if (sctx
->b
.chip_class
== CIK
&&
1200 (!input
|| (!input
->buffer
&& !input
->user_buffer
)))
1201 input
= &sctx
->null_const_buf
;
1203 if (input
&& (input
->buffer
|| input
->user_buffer
)) {
1204 struct pipe_resource
*buffer
= NULL
;
1207 /* Upload the user buffer if needed. */
1208 if (input
->user_buffer
) {
1209 unsigned buffer_offset
;
1211 si_upload_const_buffer(sctx
,
1212 (struct r600_resource
**)&buffer
, input
->user_buffer
,
1213 input
->buffer_size
, &buffer_offset
);
1215 /* Just unbind on failure. */
1216 si_set_constant_buffer(sctx
, buffers
, descriptors_idx
, slot
, NULL
);
1219 va
= r600_resource(buffer
)->gpu_address
+ buffer_offset
;
1221 pipe_resource_reference(&buffer
, input
->buffer
);
1222 va
= r600_resource(buffer
)->gpu_address
+ input
->buffer_offset
;
1223 /* Only track usage for non-user buffers. */
1224 r600_resource(buffer
)->bind_history
|= PIPE_BIND_CONSTANT_BUFFER
;
1227 /* Set the descriptor. */
1228 uint32_t *desc
= descs
->list
+ slot
*4;
1230 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1232 desc
[2] = input
->buffer_size
;
1233 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1234 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1235 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1236 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1237 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1238 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
1240 buffers
->buffers
[slot
] = buffer
;
1241 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
1242 (struct r600_resource
*)buffer
,
1243 buffers
->shader_usage_constbuf
,
1244 buffers
->priority_constbuf
, true);
1245 buffers
->enabled_mask
|= 1u << slot
;
1247 /* Clear the descriptor. */
1248 memset(descs
->list
+ slot
*4, 0, sizeof(uint32_t) * 4);
1249 buffers
->enabled_mask
&= ~(1u << slot
);
1252 descs
->dirty_mask
|= 1u << slot
;
1253 sctx
->descriptors_dirty
|= 1u << descriptors_idx
;
1256 void si_set_rw_buffer(struct si_context
*sctx
,
1257 uint slot
, const struct pipe_constant_buffer
*input
)
1259 si_set_constant_buffer(sctx
, &sctx
->rw_buffers
,
1260 SI_DESCS_RW_BUFFERS
, slot
, input
);
1263 static void si_pipe_set_constant_buffer(struct pipe_context
*ctx
,
1264 enum pipe_shader_type shader
, uint slot
,
1265 const struct pipe_constant_buffer
*input
)
1267 struct si_context
*sctx
= (struct si_context
*)ctx
;
1269 if (shader
>= SI_NUM_SHADERS
)
1272 slot
= si_get_constbuf_slot(slot
);
1273 si_set_constant_buffer(sctx
, &sctx
->const_and_shader_buffers
[shader
],
1274 si_const_and_shader_buffer_descriptors_idx(shader
),
1278 void si_get_pipe_constant_buffer(struct si_context
*sctx
, uint shader
,
1279 uint slot
, struct pipe_constant_buffer
*cbuf
)
1281 cbuf
->user_buffer
= NULL
;
1282 si_get_buffer_from_descriptors(
1283 &sctx
->const_and_shader_buffers
[shader
],
1284 si_const_and_shader_buffer_descriptors(sctx
, shader
),
1285 si_get_constbuf_slot(slot
),
1286 &cbuf
->buffer
, &cbuf
->buffer_offset
, &cbuf
->buffer_size
);
1289 /* SHADER BUFFERS */
1291 static void si_set_shader_buffers(struct pipe_context
*ctx
,
1292 enum pipe_shader_type shader
,
1293 unsigned start_slot
, unsigned count
,
1294 const struct pipe_shader_buffer
*sbuffers
)
1296 struct si_context
*sctx
= (struct si_context
*)ctx
;
1297 struct si_buffer_resources
*buffers
= &sctx
->const_and_shader_buffers
[shader
];
1298 struct si_descriptors
*descs
= si_const_and_shader_buffer_descriptors(sctx
, shader
);
1301 assert(start_slot
+ count
<= SI_NUM_SHADER_BUFFERS
);
1303 for (i
= 0; i
< count
; ++i
) {
1304 const struct pipe_shader_buffer
*sbuffer
= sbuffers
? &sbuffers
[i
] : NULL
;
1305 struct r600_resource
*buf
;
1306 unsigned slot
= si_get_shaderbuf_slot(start_slot
+ i
);
1307 uint32_t *desc
= descs
->list
+ slot
* 4;
1310 if (!sbuffer
|| !sbuffer
->buffer
) {
1311 pipe_resource_reference(&buffers
->buffers
[slot
], NULL
);
1312 memset(desc
, 0, sizeof(uint32_t) * 4);
1313 buffers
->enabled_mask
&= ~(1u << slot
);
1314 descs
->dirty_mask
|= 1u << slot
;
1315 sctx
->descriptors_dirty
|=
1316 1u << si_const_and_shader_buffer_descriptors_idx(shader
);
1320 buf
= (struct r600_resource
*)sbuffer
->buffer
;
1321 va
= buf
->gpu_address
+ sbuffer
->buffer_offset
;
1324 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1326 desc
[2] = sbuffer
->buffer_size
;
1327 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1328 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1329 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1330 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1331 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1332 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
1334 pipe_resource_reference(&buffers
->buffers
[slot
], &buf
->b
.b
);
1335 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
, buf
,
1336 buffers
->shader_usage
,
1337 buffers
->priority
, true);
1338 buf
->bind_history
|= PIPE_BIND_SHADER_BUFFER
;
1340 buffers
->enabled_mask
|= 1u << slot
;
1341 descs
->dirty_mask
|= 1u << slot
;
1342 sctx
->descriptors_dirty
|=
1343 1u << si_const_and_shader_buffer_descriptors_idx(shader
);
1345 util_range_add(&buf
->valid_buffer_range
, sbuffer
->buffer_offset
,
1346 sbuffer
->buffer_offset
+ sbuffer
->buffer_size
);
1350 void si_get_shader_buffers(struct si_context
*sctx
,
1351 enum pipe_shader_type shader
,
1352 uint start_slot
, uint count
,
1353 struct pipe_shader_buffer
*sbuf
)
1355 struct si_buffer_resources
*buffers
= &sctx
->const_and_shader_buffers
[shader
];
1356 struct si_descriptors
*descs
= si_const_and_shader_buffer_descriptors(sctx
, shader
);
1358 for (unsigned i
= 0; i
< count
; ++i
) {
1359 si_get_buffer_from_descriptors(
1361 si_get_shaderbuf_slot(start_slot
+ i
),
1362 &sbuf
[i
].buffer
, &sbuf
[i
].buffer_offset
,
1363 &sbuf
[i
].buffer_size
);
1369 void si_set_ring_buffer(struct pipe_context
*ctx
, uint slot
,
1370 struct pipe_resource
*buffer
,
1371 unsigned stride
, unsigned num_records
,
1372 bool add_tid
, bool swizzle
,
1373 unsigned element_size
, unsigned index_stride
, uint64_t offset
)
1375 struct si_context
*sctx
= (struct si_context
*)ctx
;
1376 struct si_buffer_resources
*buffers
= &sctx
->rw_buffers
;
1377 struct si_descriptors
*descs
= &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
1379 /* The stride field in the resource descriptor has 14 bits */
1380 assert(stride
< (1 << 14));
1382 assert(slot
< descs
->num_elements
);
1383 pipe_resource_reference(&buffers
->buffers
[slot
], NULL
);
1388 va
= r600_resource(buffer
)->gpu_address
+ offset
;
1390 switch (element_size
) {
1392 assert(!"Unsupported ring buffer element size");
1408 switch (index_stride
) {
1410 assert(!"Unsupported ring buffer index stride");
1426 if (sctx
->b
.chip_class
>= VI
&& stride
)
1427 num_records
*= stride
;
1429 /* Set the descriptor. */
1430 uint32_t *desc
= descs
->list
+ slot
*4;
1432 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1433 S_008F04_STRIDE(stride
) |
1434 S_008F04_SWIZZLE_ENABLE(swizzle
);
1435 desc
[2] = num_records
;
1436 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1437 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1438 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1439 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1440 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1441 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
) |
1442 S_008F0C_INDEX_STRIDE(index_stride
) |
1443 S_008F0C_ADD_TID_ENABLE(add_tid
);
1445 if (sctx
->b
.chip_class
>= GFX9
)
1446 assert(!swizzle
|| element_size
== 1); /* always 4 bytes on GFX9 */
1448 desc
[3] |= S_008F0C_ELEMENT_SIZE(element_size
);
1450 pipe_resource_reference(&buffers
->buffers
[slot
], buffer
);
1451 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
1452 (struct r600_resource
*)buffer
,
1453 buffers
->shader_usage
, buffers
->priority
);
1454 buffers
->enabled_mask
|= 1u << slot
;
1456 /* Clear the descriptor. */
1457 memset(descs
->list
+ slot
*4, 0, sizeof(uint32_t) * 4);
1458 buffers
->enabled_mask
&= ~(1u << slot
);
1461 descs
->dirty_mask
|= 1u << slot
;
1462 sctx
->descriptors_dirty
|= 1u << SI_DESCS_RW_BUFFERS
;
1465 /* STREAMOUT BUFFERS */
1467 static void si_set_streamout_targets(struct pipe_context
*ctx
,
1468 unsigned num_targets
,
1469 struct pipe_stream_output_target
**targets
,
1470 const unsigned *offsets
)
1472 struct si_context
*sctx
= (struct si_context
*)ctx
;
1473 struct si_buffer_resources
*buffers
= &sctx
->rw_buffers
;
1474 struct si_descriptors
*descs
= &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
1475 unsigned old_num_targets
= sctx
->b
.streamout
.num_targets
;
1478 /* We are going to unbind the buffers. Mark which caches need to be flushed. */
1479 if (sctx
->b
.streamout
.num_targets
&& sctx
->b
.streamout
.begin_emitted
) {
1480 /* Since streamout uses vector writes which go through TC L2
1481 * and most other clients can use TC L2 as well, we don't need
1484 * The only cases which requires flushing it is VGT DMA index
1485 * fetching (on <= CIK) and indirect draw data, which are rare
1486 * cases. Thus, flag the TC L2 dirtiness in the resource and
1487 * handle it at draw call time.
1489 for (i
= 0; i
< sctx
->b
.streamout
.num_targets
; i
++)
1490 if (sctx
->b
.streamout
.targets
[i
])
1491 r600_resource(sctx
->b
.streamout
.targets
[i
]->b
.buffer
)->TC_L2_dirty
= true;
1493 /* Invalidate the scalar cache in case a streamout buffer is
1494 * going to be used as a constant buffer.
1496 * Invalidate TC L1, because streamout bypasses it (done by
1497 * setting GLC=1 in the store instruction), but it can contain
1498 * outdated data of streamout buffers.
1500 * VS_PARTIAL_FLUSH is required if the buffers are going to be
1501 * used as an input immediately.
1503 sctx
->b
.flags
|= SI_CONTEXT_INV_SMEM_L1
|
1504 SI_CONTEXT_INV_VMEM_L1
|
1505 SI_CONTEXT_VS_PARTIAL_FLUSH
;
1508 /* All readers of the streamout targets need to be finished before we can
1509 * start writing to the targets.
1512 sctx
->b
.flags
|= SI_CONTEXT_PS_PARTIAL_FLUSH
|
1513 SI_CONTEXT_CS_PARTIAL_FLUSH
;
1515 /* Streamout buffers must be bound in 2 places:
1516 * 1) in VGT by setting the VGT_STRMOUT registers
1517 * 2) as shader resources
1520 /* Set the VGT regs. */
1521 r600_set_streamout_targets(ctx
, num_targets
, targets
, offsets
);
1523 /* Set the shader resources.*/
1524 for (i
= 0; i
< num_targets
; i
++) {
1525 bufidx
= SI_VS_STREAMOUT_BUF0
+ i
;
1528 struct pipe_resource
*buffer
= targets
[i
]->buffer
;
1529 uint64_t va
= r600_resource(buffer
)->gpu_address
;
1531 /* Set the descriptor.
1533 * On VI, the format must be non-INVALID, otherwise
1534 * the buffer will be considered not bound and store
1535 * instructions will be no-ops.
1537 uint32_t *desc
= descs
->list
+ bufidx
*4;
1539 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32);
1540 desc
[2] = 0xffffffff;
1541 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1542 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1543 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1544 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1545 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
1547 /* Set the resource. */
1548 pipe_resource_reference(&buffers
->buffers
[bufidx
],
1550 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
1551 (struct r600_resource
*)buffer
,
1552 buffers
->shader_usage
,
1553 RADEON_PRIO_SHADER_RW_BUFFER
,
1555 r600_resource(buffer
)->bind_history
|= PIPE_BIND_STREAM_OUTPUT
;
1557 buffers
->enabled_mask
|= 1u << bufidx
;
1559 /* Clear the descriptor and unset the resource. */
1560 memset(descs
->list
+ bufidx
*4, 0,
1561 sizeof(uint32_t) * 4);
1562 pipe_resource_reference(&buffers
->buffers
[bufidx
],
1564 buffers
->enabled_mask
&= ~(1u << bufidx
);
1566 descs
->dirty_mask
|= 1u << bufidx
;
1568 for (; i
< old_num_targets
; i
++) {
1569 bufidx
= SI_VS_STREAMOUT_BUF0
+ i
;
1570 /* Clear the descriptor and unset the resource. */
1571 memset(descs
->list
+ bufidx
*4, 0, sizeof(uint32_t) * 4);
1572 pipe_resource_reference(&buffers
->buffers
[bufidx
], NULL
);
1573 buffers
->enabled_mask
&= ~(1u << bufidx
);
1574 descs
->dirty_mask
|= 1u << bufidx
;
1577 sctx
->descriptors_dirty
|= 1u << SI_DESCS_RW_BUFFERS
;
1580 static void si_desc_reset_buffer_offset(struct pipe_context
*ctx
,
1581 uint32_t *desc
, uint64_t old_buf_va
,
1582 struct pipe_resource
*new_buf
)
1584 /* Retrieve the buffer offset from the descriptor. */
1585 uint64_t old_desc_va
=
1586 desc
[0] | ((uint64_t)G_008F04_BASE_ADDRESS_HI(desc
[1]) << 32);
1588 assert(old_buf_va
<= old_desc_va
);
1589 uint64_t offset_within_buffer
= old_desc_va
- old_buf_va
;
1591 /* Update the descriptor. */
1592 si_set_buf_desc_address(r600_resource(new_buf
), offset_within_buffer
,
1596 /* INTERNAL CONST BUFFERS */
1598 static void si_set_polygon_stipple(struct pipe_context
*ctx
,
1599 const struct pipe_poly_stipple
*state
)
1601 struct si_context
*sctx
= (struct si_context
*)ctx
;
1602 struct pipe_constant_buffer cb
= {};
1603 unsigned stipple
[32];
1606 for (i
= 0; i
< 32; i
++)
1607 stipple
[i
] = util_bitreverse(state
->stipple
[i
]);
1609 cb
.user_buffer
= stipple
;
1610 cb
.buffer_size
= sizeof(stipple
);
1612 si_set_rw_buffer(sctx
, SI_PS_CONST_POLY_STIPPLE
, &cb
);
1615 /* TEXTURE METADATA ENABLE/DISABLE */
1618 si_resident_handles_update_needs_color_decompress(struct si_context
*sctx
)
1620 unsigned num_resident_tex_handles
, num_resident_img_handles
;
1623 num_resident_tex_handles
= sctx
->resident_tex_handles
.size
/
1624 sizeof(struct si_texture_handle
*);
1626 for (i
= 0; i
< num_resident_tex_handles
; i
++) {
1627 struct si_texture_handle
*tex_handle
=
1628 *util_dynarray_element(&sctx
->resident_tex_handles
,
1629 struct si_texture_handle
*, i
);
1630 struct pipe_resource
*res
= tex_handle
->view
->texture
;
1632 if (res
&& res
->target
!= PIPE_BUFFER
) {
1633 struct r600_texture
*rtex
= (struct r600_texture
*)res
;
1635 tex_handle
->needs_color_decompress
=
1636 color_needs_decompression(rtex
);
1640 num_resident_img_handles
= sctx
->resident_img_handles
.size
/
1641 sizeof(struct si_image_handle
*);
1643 for (i
= 0; i
< num_resident_img_handles
; i
++) {
1644 struct si_image_handle
*img_handle
=
1645 *util_dynarray_element(&sctx
->resident_img_handles
,
1646 struct si_image_handle
*, i
);
1647 struct pipe_image_view
*view
= &img_handle
->view
;
1648 struct pipe_resource
*res
= view
->resource
;
1650 if (res
&& res
->target
!= PIPE_BUFFER
) {
1651 struct r600_texture
*rtex
= (struct r600_texture
*)res
;
1653 img_handle
->needs_color_decompress
=
1654 color_needs_decompression(rtex
);
1659 /* CMASK can be enabled (for fast clear) and disabled (for texture export)
1660 * while the texture is bound, possibly by a different context. In that case,
1661 * call this function to update needs_*_decompress_masks.
1663 void si_update_needs_color_decompress_masks(struct si_context
*sctx
)
1665 for (int i
= 0; i
< SI_NUM_SHADERS
; ++i
) {
1666 si_samplers_update_needs_color_decompress_mask(&sctx
->samplers
[i
]);
1667 si_images_update_needs_color_decompress_mask(&sctx
->images
[i
]);
1668 si_update_shader_needs_decompress_mask(sctx
, i
);
1671 si_resident_handles_update_needs_color_decompress(sctx
);
1674 /* BUFFER DISCARD/INVALIDATION */
1676 /** Reset descriptors of buffer resources after \p buf has been invalidated. */
1677 static void si_reset_buffer_resources(struct si_context
*sctx
,
1678 struct si_buffer_resources
*buffers
,
1679 unsigned descriptors_idx
,
1681 struct pipe_resource
*buf
,
1683 enum radeon_bo_usage usage
,
1684 enum radeon_bo_priority priority
)
1686 struct si_descriptors
*descs
= &sctx
->descriptors
[descriptors_idx
];
1687 unsigned mask
= buffers
->enabled_mask
& slot_mask
;
1690 unsigned i
= u_bit_scan(&mask
);
1691 if (buffers
->buffers
[i
] == buf
) {
1692 si_desc_reset_buffer_offset(&sctx
->b
.b
,
1695 descs
->dirty_mask
|= 1u << i
;
1696 sctx
->descriptors_dirty
|= 1u << descriptors_idx
;
1698 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
1699 (struct r600_resource
*)buf
,
1700 usage
, priority
, true);
1705 static void si_rebind_buffer(struct pipe_context
*ctx
, struct pipe_resource
*buf
,
1708 struct si_context
*sctx
= (struct si_context
*)ctx
;
1709 struct r600_resource
*rbuffer
= r600_resource(buf
);
1711 unsigned num_elems
= sctx
->vertex_elements
?
1712 sctx
->vertex_elements
->count
: 0;
1714 /* We changed the buffer, now we need to bind it where the old one
1715 * was bound. This consists of 2 things:
1716 * 1) Updating the resource descriptor and dirtying it.
1717 * 2) Adding a relocation to the CS, so that it's usable.
1720 /* Vertex buffers. */
1721 if (rbuffer
->bind_history
& PIPE_BIND_VERTEX_BUFFER
) {
1722 for (i
= 0; i
< num_elems
; i
++) {
1723 int vb
= sctx
->vertex_elements
->vertex_buffer_index
[i
];
1725 if (vb
>= ARRAY_SIZE(sctx
->vertex_buffer
))
1727 if (!sctx
->vertex_buffer
[vb
].buffer
.resource
)
1730 if (sctx
->vertex_buffer
[vb
].buffer
.resource
== buf
) {
1731 sctx
->vertex_buffers_dirty
= true;
1737 /* Streamout buffers. (other internal buffers can't be invalidated) */
1738 if (rbuffer
->bind_history
& PIPE_BIND_STREAM_OUTPUT
) {
1739 for (i
= SI_VS_STREAMOUT_BUF0
; i
<= SI_VS_STREAMOUT_BUF3
; i
++) {
1740 struct si_buffer_resources
*buffers
= &sctx
->rw_buffers
;
1741 struct si_descriptors
*descs
=
1742 &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
1744 if (buffers
->buffers
[i
] != buf
)
1747 si_desc_reset_buffer_offset(ctx
, descs
->list
+ i
*4,
1749 descs
->dirty_mask
|= 1u << i
;
1750 sctx
->descriptors_dirty
|= 1u << SI_DESCS_RW_BUFFERS
;
1752 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
1753 rbuffer
, buffers
->shader_usage
,
1754 RADEON_PRIO_SHADER_RW_BUFFER
,
1757 /* Update the streamout state. */
1758 if (sctx
->b
.streamout
.begin_emitted
)
1759 r600_emit_streamout_end(&sctx
->b
);
1760 sctx
->b
.streamout
.append_bitmask
=
1761 sctx
->b
.streamout
.enabled_mask
;
1762 r600_streamout_buffers_dirty(&sctx
->b
);
1766 /* Constant and shader buffers. */
1767 if (rbuffer
->bind_history
& PIPE_BIND_CONSTANT_BUFFER
) {
1768 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++)
1769 si_reset_buffer_resources(sctx
, &sctx
->const_and_shader_buffers
[shader
],
1770 si_const_and_shader_buffer_descriptors_idx(shader
),
1771 u_bit_consecutive(SI_NUM_SHADER_BUFFERS
, SI_NUM_CONST_BUFFERS
),
1773 sctx
->const_and_shader_buffers
[shader
].shader_usage_constbuf
,
1774 sctx
->const_and_shader_buffers
[shader
].priority_constbuf
);
1777 if (rbuffer
->bind_history
& PIPE_BIND_SHADER_BUFFER
) {
1778 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++)
1779 si_reset_buffer_resources(sctx
, &sctx
->const_and_shader_buffers
[shader
],
1780 si_const_and_shader_buffer_descriptors_idx(shader
),
1781 u_bit_consecutive(0, SI_NUM_SHADER_BUFFERS
),
1783 sctx
->const_and_shader_buffers
[shader
].shader_usage
,
1784 sctx
->const_and_shader_buffers
[shader
].priority
);
1787 if (rbuffer
->bind_history
& PIPE_BIND_SAMPLER_VIEW
) {
1788 /* Texture buffers - update bindings. */
1789 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++) {
1790 struct si_sampler_views
*views
= &sctx
->samplers
[shader
].views
;
1791 struct si_descriptors
*descs
=
1792 si_sampler_and_image_descriptors(sctx
, shader
);
1793 unsigned mask
= views
->enabled_mask
;
1796 unsigned i
= u_bit_scan(&mask
);
1797 if (views
->views
[i
]->texture
== buf
) {
1798 unsigned desc_slot
= si_get_sampler_slot(i
);
1800 si_desc_reset_buffer_offset(ctx
,
1804 descs
->dirty_mask
|= 1ull << desc_slot
;
1805 sctx
->descriptors_dirty
|=
1806 1u << si_sampler_and_image_descriptors_idx(shader
);
1808 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
1809 rbuffer
, RADEON_USAGE_READ
,
1810 RADEON_PRIO_SAMPLER_BUFFER
,
1818 if (rbuffer
->bind_history
& PIPE_BIND_SHADER_IMAGE
) {
1819 for (shader
= 0; shader
< SI_NUM_SHADERS
; ++shader
) {
1820 struct si_images_info
*images
= &sctx
->images
[shader
];
1821 struct si_descriptors
*descs
=
1822 si_sampler_and_image_descriptors(sctx
, shader
);
1823 unsigned mask
= images
->enabled_mask
;
1826 unsigned i
= u_bit_scan(&mask
);
1828 if (images
->views
[i
].resource
== buf
) {
1829 unsigned desc_slot
= si_get_image_slot(i
);
1831 if (images
->views
[i
].access
& PIPE_IMAGE_ACCESS_WRITE
)
1832 si_mark_image_range_valid(&images
->views
[i
]);
1834 si_desc_reset_buffer_offset(
1835 ctx
, descs
->list
+ desc_slot
* 8 + 4,
1837 /* two 8-byte images share one 16-byte slot */
1838 descs
->dirty_mask
|= 1u << (desc_slot
/ 2);
1839 sctx
->descriptors_dirty
|=
1840 1u << si_sampler_and_image_descriptors_idx(shader
);
1842 radeon_add_to_buffer_list_check_mem(
1843 &sctx
->b
, &sctx
->b
.gfx
, rbuffer
,
1844 RADEON_USAGE_READWRITE
,
1845 RADEON_PRIO_SAMPLER_BUFFER
, true);
1851 /* Bindless texture handles */
1852 if (rbuffer
->texture_handle_allocated
) {
1853 unsigned num_resident_tex_handles
;
1855 num_resident_tex_handles
= sctx
->resident_tex_handles
.size
/
1856 sizeof(struct si_texture_handle
*);
1858 for (i
= 0; i
< num_resident_tex_handles
; i
++) {
1859 struct si_texture_handle
*tex_handle
=
1860 *util_dynarray_element(&sctx
->resident_tex_handles
,
1861 struct si_texture_handle
*, i
);
1862 struct pipe_sampler_view
*view
= tex_handle
->view
;
1863 struct si_bindless_descriptor
*desc
= tex_handle
->desc
;
1865 if (view
->texture
== buf
) {
1866 si_set_buf_desc_address(rbuffer
,
1868 &desc
->desc_list
[4]);
1870 sctx
->bindless_descriptors_dirty
= true;
1872 radeon_add_to_buffer_list_check_mem(
1873 &sctx
->b
, &sctx
->b
.gfx
, rbuffer
,
1875 RADEON_PRIO_SAMPLER_BUFFER
, true);
1880 /* Bindless image handles */
1881 if (rbuffer
->image_handle_allocated
) {
1882 unsigned num_resident_img_handles
;
1884 num_resident_img_handles
= sctx
->resident_img_handles
.size
/
1885 sizeof(struct si_image_handle
*);
1887 for (i
= 0; i
< num_resident_img_handles
; i
++) {
1888 struct si_image_handle
*img_handle
=
1889 *util_dynarray_element(&sctx
->resident_img_handles
,
1890 struct si_image_handle
*, i
);
1891 struct pipe_image_view
*view
= &img_handle
->view
;
1892 struct si_bindless_descriptor
*desc
= img_handle
->desc
;
1894 if (view
->resource
== buf
) {
1895 if (view
->access
& PIPE_IMAGE_ACCESS_WRITE
)
1896 si_mark_image_range_valid(view
);
1898 si_set_buf_desc_address(rbuffer
,
1900 &desc
->desc_list
[4]);
1902 sctx
->bindless_descriptors_dirty
= true;
1904 radeon_add_to_buffer_list_check_mem(
1905 &sctx
->b
, &sctx
->b
.gfx
, rbuffer
,
1906 RADEON_USAGE_READWRITE
,
1907 RADEON_PRIO_SAMPLER_BUFFER
, true);
1913 /* Reallocate a buffer a update all resource bindings where the buffer is
1916 * This is used to avoid CPU-GPU synchronizations, because it makes the buffer
1917 * idle by discarding its contents. Apps usually tell us when to do this using
1918 * map_buffer flags, for example.
1920 static void si_invalidate_buffer(struct pipe_context
*ctx
, struct pipe_resource
*buf
)
1922 struct si_context
*sctx
= (struct si_context
*)ctx
;
1923 struct r600_resource
*rbuffer
= r600_resource(buf
);
1924 uint64_t old_va
= rbuffer
->gpu_address
;
1926 /* Reallocate the buffer in the same pipe_resource. */
1927 r600_alloc_resource(&sctx
->screen
->b
, rbuffer
);
1929 si_rebind_buffer(ctx
, buf
, old_va
);
1932 /* Update mutable image descriptor fields of all bound textures. */
1933 void si_update_all_texture_descriptors(struct si_context
*sctx
)
1937 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++) {
1938 struct si_sampler_views
*samplers
= &sctx
->samplers
[shader
].views
;
1939 struct si_images_info
*images
= &sctx
->images
[shader
];
1943 mask
= images
->enabled_mask
;
1945 unsigned i
= u_bit_scan(&mask
);
1946 struct pipe_image_view
*view
= &images
->views
[i
];
1948 if (!view
->resource
||
1949 view
->resource
->target
== PIPE_BUFFER
)
1952 si_set_shader_image(sctx
, shader
, i
, view
, true);
1955 /* Sampler views. */
1956 mask
= samplers
->enabled_mask
;
1958 unsigned i
= u_bit_scan(&mask
);
1959 struct pipe_sampler_view
*view
= samplers
->views
[i
];
1963 view
->texture
->target
== PIPE_BUFFER
)
1966 si_set_sampler_view(sctx
, shader
, i
,
1967 samplers
->views
[i
], true);
1970 si_update_shader_needs_decompress_mask(sctx
, shader
);
1974 /* SHADER USER DATA */
1976 static void si_mark_shader_pointers_dirty(struct si_context
*sctx
,
1979 sctx
->shader_pointers_dirty
|=
1980 u_bit_consecutive(SI_DESCS_FIRST_SHADER
+ shader
* SI_NUM_SHADER_DESCS
,
1981 SI_NUM_SHADER_DESCS
);
1983 if (shader
== PIPE_SHADER_VERTEX
)
1984 sctx
->vertex_buffer_pointer_dirty
= sctx
->vertex_buffers
.buffer
!= NULL
;
1986 si_mark_atom_dirty(sctx
, &sctx
->shader_userdata
.atom
);
1989 static void si_shader_userdata_begin_new_cs(struct si_context
*sctx
)
1991 sctx
->shader_pointers_dirty
= u_bit_consecutive(0, SI_NUM_DESCS
);
1992 sctx
->vertex_buffer_pointer_dirty
= sctx
->vertex_buffers
.buffer
!= NULL
;
1993 si_mark_atom_dirty(sctx
, &sctx
->shader_userdata
.atom
);
1996 /* Set a base register address for user data constants in the given shader.
1997 * This assigns a mapping from PIPE_SHADER_* to SPI_SHADER_USER_DATA_*.
1999 static void si_set_user_data_base(struct si_context
*sctx
,
2000 unsigned shader
, uint32_t new_base
)
2002 uint32_t *base
= &sctx
->shader_userdata
.sh_base
[shader
];
2004 if (*base
!= new_base
) {
2008 si_mark_shader_pointers_dirty(sctx
, shader
);
2010 if (shader
== PIPE_SHADER_VERTEX
)
2011 sctx
->last_vs_state
= ~0;
2016 /* This must be called when these shaders are changed from non-NULL to NULL
2019 * - tessellation control shader
2020 * - tessellation evaluation shader
2022 void si_shader_change_notify(struct si_context
*sctx
)
2024 /* VS can be bound as VS, ES, or LS. */
2025 if (sctx
->tes_shader
.cso
) {
2026 if (sctx
->b
.chip_class
>= GFX9
) {
2027 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
2028 R_00B430_SPI_SHADER_USER_DATA_LS_0
);
2030 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
2031 R_00B530_SPI_SHADER_USER_DATA_LS_0
);
2033 } else if (sctx
->gs_shader
.cso
) {
2034 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
2035 R_00B330_SPI_SHADER_USER_DATA_ES_0
);
2037 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
2038 R_00B130_SPI_SHADER_USER_DATA_VS_0
);
2041 /* TES can be bound as ES, VS, or not bound. */
2042 if (sctx
->tes_shader
.cso
) {
2043 if (sctx
->gs_shader
.cso
)
2044 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_EVAL
,
2045 R_00B330_SPI_SHADER_USER_DATA_ES_0
);
2047 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_EVAL
,
2048 R_00B130_SPI_SHADER_USER_DATA_VS_0
);
2050 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_EVAL
, 0);
2054 static void si_emit_shader_pointer(struct si_context
*sctx
,
2055 struct si_descriptors
*desc
,
2058 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
2062 return; /* the pointer is not used by current shaders */
2064 va
= desc
->buffer
->gpu_address
+
2065 desc
->buffer_offset
;
2067 radeon_emit(cs
, PKT3(PKT3_SET_SH_REG
, 2, 0));
2068 radeon_emit(cs
, (sh_base
+ desc
->shader_userdata_offset
- SI_SH_REG_OFFSET
) >> 2);
2069 radeon_emit(cs
, va
);
2070 radeon_emit(cs
, va
>> 32);
2073 void si_emit_graphics_shader_userdata(struct si_context
*sctx
,
2074 struct r600_atom
*atom
)
2077 uint32_t *sh_base
= sctx
->shader_userdata
.sh_base
;
2078 struct si_descriptors
*descs
;
2080 descs
= &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
2082 if (sctx
->shader_pointers_dirty
& (1 << SI_DESCS_RW_BUFFERS
)) {
2083 si_emit_shader_pointer(sctx
, descs
,
2084 R_00B030_SPI_SHADER_USER_DATA_PS_0
);
2085 si_emit_shader_pointer(sctx
, descs
,
2086 R_00B130_SPI_SHADER_USER_DATA_VS_0
);
2088 if (sctx
->b
.chip_class
>= GFX9
) {
2089 /* GFX9 merged LS-HS and ES-GS.
2090 * Set RW_BUFFERS in the special registers, so that
2091 * it's preloaded into s[0:1] instead of s[8:9].
2093 si_emit_shader_pointer(sctx
, descs
,
2094 R_00B208_SPI_SHADER_USER_DATA_ADDR_LO_GS
);
2095 si_emit_shader_pointer(sctx
, descs
,
2096 R_00B408_SPI_SHADER_USER_DATA_ADDR_LO_HS
);
2098 si_emit_shader_pointer(sctx
, descs
,
2099 R_00B230_SPI_SHADER_USER_DATA_GS_0
);
2100 si_emit_shader_pointer(sctx
, descs
,
2101 R_00B330_SPI_SHADER_USER_DATA_ES_0
);
2102 si_emit_shader_pointer(sctx
, descs
,
2103 R_00B430_SPI_SHADER_USER_DATA_HS_0
);
2107 mask
= sctx
->shader_pointers_dirty
&
2108 u_bit_consecutive(SI_DESCS_FIRST_SHADER
,
2109 SI_DESCS_FIRST_COMPUTE
- SI_DESCS_FIRST_SHADER
);
2112 unsigned i
= u_bit_scan(&mask
);
2113 unsigned shader
= (i
- SI_DESCS_FIRST_SHADER
) / SI_NUM_SHADER_DESCS
;
2114 unsigned base
= sh_base
[shader
];
2117 si_emit_shader_pointer(sctx
, descs
+ i
, base
);
2119 sctx
->shader_pointers_dirty
&=
2120 ~u_bit_consecutive(SI_DESCS_RW_BUFFERS
, SI_DESCS_FIRST_COMPUTE
);
2122 if (sctx
->vertex_buffer_pointer_dirty
) {
2123 si_emit_shader_pointer(sctx
, &sctx
->vertex_buffers
,
2124 sh_base
[PIPE_SHADER_VERTEX
]);
2125 sctx
->vertex_buffer_pointer_dirty
= false;
2129 void si_emit_compute_shader_userdata(struct si_context
*sctx
)
2131 unsigned base
= R_00B900_COMPUTE_USER_DATA_0
;
2132 struct si_descriptors
*descs
= sctx
->descriptors
;
2133 unsigned compute_mask
=
2134 u_bit_consecutive(SI_DESCS_FIRST_COMPUTE
, SI_NUM_SHADER_DESCS
);
2135 unsigned mask
= sctx
->shader_pointers_dirty
& compute_mask
;
2138 unsigned i
= u_bit_scan(&mask
);
2140 si_emit_shader_pointer(sctx
, descs
+ i
, base
);
2142 sctx
->shader_pointers_dirty
&= ~compute_mask
;
2147 struct si_bindless_descriptor_slab
2149 struct pb_slab base
;
2150 struct r600_resource
*buffer
;
2151 struct si_bindless_descriptor
*entries
;
2154 bool si_bindless_descriptor_can_reclaim_slab(void *priv
,
2155 struct pb_slab_entry
*entry
)
2157 /* Do not allow to reclaim any bindless descriptors for now because the
2158 * GPU might be using them. This should be improved later on.
2163 struct pb_slab
*si_bindless_descriptor_slab_alloc(void *priv
, unsigned heap
,
2164 unsigned entry_size
,
2165 unsigned group_index
)
2167 struct si_context
*sctx
= priv
;
2168 struct si_screen
*sscreen
= sctx
->screen
;
2169 struct si_bindless_descriptor_slab
*slab
;
2171 slab
= CALLOC_STRUCT(si_bindless_descriptor_slab
);
2175 /* Create a buffer in VRAM for 1024 bindless descriptors. */
2176 slab
->buffer
= (struct r600_resource
*)
2177 pipe_buffer_create(&sscreen
->b
.b
, 0,
2178 PIPE_USAGE_DEFAULT
, 64 * 1024);
2182 slab
->base
.num_entries
= slab
->buffer
->bo_size
/ entry_size
;
2183 slab
->base
.num_free
= slab
->base
.num_entries
;
2184 slab
->entries
= CALLOC(slab
->base
.num_entries
, sizeof(*slab
->entries
));
2188 LIST_INITHEAD(&slab
->base
.free
);
2190 for (unsigned i
= 0; i
< slab
->base
.num_entries
; ++i
) {
2191 struct si_bindless_descriptor
*desc
= &slab
->entries
[i
];
2193 desc
->entry
.slab
= &slab
->base
;
2194 desc
->entry
.group_index
= group_index
;
2195 desc
->buffer
= slab
->buffer
;
2196 desc
->offset
= i
* entry_size
;
2198 LIST_ADDTAIL(&desc
->entry
.head
, &slab
->base
.free
);
2201 /* Add the descriptor to the per-context list. */
2202 util_dynarray_append(&sctx
->bindless_descriptors
,
2203 struct r600_resource
*, slab
->buffer
);
2208 r600_resource_reference(&slab
->buffer
, NULL
);
2214 void si_bindless_descriptor_slab_free(void *priv
, struct pb_slab
*pslab
)
2216 struct si_context
*sctx
= priv
;
2217 struct si_bindless_descriptor_slab
*slab
=
2218 (struct si_bindless_descriptor_slab
*)pslab
;
2220 /* Remove the descriptor from the per-context list. */
2221 util_dynarray_delete_unordered(&sctx
->bindless_descriptors
,
2222 struct r600_resource
*, slab
->buffer
);
2224 r600_resource_reference(&slab
->buffer
, NULL
);
2225 FREE(slab
->entries
);
2229 static struct si_bindless_descriptor
*
2230 si_create_bindless_descriptor(struct si_context
*sctx
, uint32_t *desc_list
,
2233 struct si_screen
*sscreen
= sctx
->screen
;
2234 struct si_bindless_descriptor
*desc
;
2235 struct pb_slab_entry
*entry
;
2238 /* Sub-allocate the bindless descriptor from a slab to avoid dealing
2239 * with a ton of buffers and for reducing the winsys overhead.
2241 entry
= pb_slab_alloc(&sctx
->bindless_descriptor_slabs
, 64, 0);
2246 desc
= container_of(entry
, desc
, entry
);
2248 /* Upload the descriptor directly in VRAM. Because the slabs are
2249 * currently never reclaimed, we don't need to synchronize the
2252 ptr
= sscreen
->b
.ws
->buffer_map(desc
->buffer
->buf
, NULL
,
2253 PIPE_TRANSFER_WRITE
|
2254 PIPE_TRANSFER_UNSYNCHRONIZED
);
2255 util_memcpy_cpu_to_le32(ptr
+ desc
->offset
, desc_list
, size
);
2257 /* Keep track of the initial descriptor especially for buffers
2258 * invalidation because we might need to know the previous address.
2260 memcpy(desc
->desc_list
, desc_list
, sizeof(desc
->desc_list
));
2265 static void si_invalidate_bindless_buf_desc(struct si_context
*sctx
,
2266 struct si_bindless_descriptor
*desc
,
2267 struct pipe_resource
*resource
,
2270 struct r600_resource
*buf
= r600_resource(resource
);
2271 uint32_t *desc_list
= desc
->desc_list
;
2272 uint64_t old_desc_va
;
2274 assert(resource
->target
== PIPE_BUFFER
);
2276 /* Retrieve the old buffer addr from the descriptor. */
2277 old_desc_va
= desc_list
[0];
2278 old_desc_va
|= ((uint64_t)G_008F04_BASE_ADDRESS_HI(desc_list
[1]) << 32);
2280 if (old_desc_va
!= buf
->gpu_address
+ offset
) {
2281 /* The buffer has been invalidated when the handle wasn't
2282 * resident, update the descriptor and the dirty flag.
2284 si_set_buf_desc_address(buf
, offset
, &desc_list
[4]);
2287 sctx
->bindless_descriptors_dirty
= true;
2291 static uint64_t si_create_texture_handle(struct pipe_context
*ctx
,
2292 struct pipe_sampler_view
*view
,
2293 const struct pipe_sampler_state
*state
)
2295 struct si_sampler_view
*sview
= (struct si_sampler_view
*)view
;
2296 struct si_context
*sctx
= (struct si_context
*)ctx
;
2297 struct si_texture_handle
*tex_handle
;
2298 struct si_sampler_state
*sstate
;
2299 uint32_t desc_list
[16];
2302 tex_handle
= CALLOC_STRUCT(si_texture_handle
);
2306 memset(desc_list
, 0, sizeof(desc_list
));
2307 si_init_descriptor_list(&desc_list
[0], 16, 1, null_texture_descriptor
);
2309 sstate
= ctx
->create_sampler_state(ctx
, state
);
2315 si_set_sampler_view_desc(sctx
, sview
, sstate
, &desc_list
[0]);
2316 ctx
->delete_sampler_state(ctx
, sstate
);
2318 tex_handle
->desc
= si_create_bindless_descriptor(sctx
, desc_list
,
2320 if (!tex_handle
->desc
) {
2325 handle
= tex_handle
->desc
->buffer
->gpu_address
+
2326 tex_handle
->desc
->offset
;
2328 if (!_mesa_hash_table_insert(sctx
->tex_handles
, (void *)handle
,
2330 pb_slab_free(&sctx
->bindless_descriptor_slabs
,
2331 &tex_handle
->desc
->entry
);
2336 pipe_sampler_view_reference(&tex_handle
->view
, view
);
2338 r600_resource(sview
->base
.texture
)->texture_handle_allocated
= true;
2343 static void si_delete_texture_handle(struct pipe_context
*ctx
, uint64_t handle
)
2345 struct si_context
*sctx
= (struct si_context
*)ctx
;
2346 struct si_texture_handle
*tex_handle
;
2347 struct hash_entry
*entry
;
2349 entry
= _mesa_hash_table_search(sctx
->tex_handles
, (void *)handle
);
2353 tex_handle
= (struct si_texture_handle
*)entry
->data
;
2355 pipe_sampler_view_reference(&tex_handle
->view
, NULL
);
2356 _mesa_hash_table_remove(sctx
->tex_handles
, entry
);
2357 pb_slab_free(&sctx
->bindless_descriptor_slabs
,
2358 &tex_handle
->desc
->entry
);
2362 static void si_make_texture_handle_resident(struct pipe_context
*ctx
,
2363 uint64_t handle
, bool resident
)
2365 struct si_context
*sctx
= (struct si_context
*)ctx
;
2366 struct si_texture_handle
*tex_handle
;
2367 struct si_sampler_view
*sview
;
2368 struct hash_entry
*entry
;
2370 entry
= _mesa_hash_table_search(sctx
->tex_handles
, (void *)handle
);
2374 tex_handle
= (struct si_texture_handle
*)entry
->data
;
2375 sview
= (struct si_sampler_view
*)tex_handle
->view
;
2378 if (sview
->base
.texture
->target
!= PIPE_BUFFER
) {
2379 struct r600_texture
*rtex
=
2380 (struct r600_texture
*)sview
->base
.texture
;
2382 tex_handle
->needs_depth_decompress
=
2383 depth_needs_decompression(rtex
, sview
);
2384 tex_handle
->needs_color_decompress
=
2385 color_needs_decompression(rtex
);
2387 if (rtex
->dcc_offset
&&
2388 p_atomic_read(&rtex
->framebuffers_bound
))
2389 sctx
->need_check_render_feedback
= true;
2391 si_invalidate_bindless_buf_desc(sctx
, tex_handle
->desc
,
2392 sview
->base
.texture
,
2393 sview
->base
.u
.buf
.offset
);
2396 /* Add the texture handle to the per-context list. */
2397 util_dynarray_append(&sctx
->resident_tex_handles
,
2398 struct si_texture_handle
*, tex_handle
);
2400 /* Add the buffers to the current CS in case si_begin_new_cs()
2401 * is not going to be called.
2403 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
2404 tex_handle
->desc
->buffer
,
2405 RADEON_USAGE_READWRITE
,
2406 RADEON_PRIO_DESCRIPTORS
);
2408 si_sampler_view_add_buffer(sctx
, sview
->base
.texture
,
2410 sview
->is_stencil_sampler
, false);
2412 /* Remove the texture handle from the per-context list. */
2413 util_dynarray_delete_unordered(&sctx
->resident_tex_handles
,
2414 struct si_texture_handle
*,
2419 static uint64_t si_create_image_handle(struct pipe_context
*ctx
,
2420 const struct pipe_image_view
*view
)
2422 struct si_context
*sctx
= (struct si_context
*)ctx
;
2423 struct si_image_handle
*img_handle
;
2424 uint32_t desc_list
[16];
2427 if (!view
|| !view
->resource
)
2430 img_handle
= CALLOC_STRUCT(si_image_handle
);
2434 memset(desc_list
, 0, sizeof(desc_list
));
2435 si_init_descriptor_list(&desc_list
[0], 8, 1, null_image_descriptor
);
2437 si_set_shader_image_desc(sctx
, view
, false, &desc_list
[0]);
2439 img_handle
->desc
= si_create_bindless_descriptor(sctx
, desc_list
,
2441 if (!img_handle
->desc
) {
2446 handle
= img_handle
->desc
->buffer
->gpu_address
+
2447 img_handle
->desc
->offset
;
2449 if (!_mesa_hash_table_insert(sctx
->img_handles
, (void *)handle
,
2451 pb_slab_free(&sctx
->bindless_descriptor_slabs
,
2452 &img_handle
->desc
->entry
);
2457 util_copy_image_view(&img_handle
->view
, view
);
2459 r600_resource(view
->resource
)->image_handle_allocated
= true;
2464 static void si_delete_image_handle(struct pipe_context
*ctx
, uint64_t handle
)
2466 struct si_context
*sctx
= (struct si_context
*)ctx
;
2467 struct si_image_handle
*img_handle
;
2468 struct hash_entry
*entry
;
2470 entry
= _mesa_hash_table_search(sctx
->img_handles
, (void *)handle
);
2474 img_handle
= (struct si_image_handle
*)entry
->data
;
2476 util_copy_image_view(&img_handle
->view
, NULL
);
2477 _mesa_hash_table_remove(sctx
->img_handles
, entry
);
2478 pb_slab_free(&sctx
->bindless_descriptor_slabs
,
2479 &img_handle
->desc
->entry
);
2483 static void si_make_image_handle_resident(struct pipe_context
*ctx
,
2484 uint64_t handle
, unsigned access
,
2487 struct si_context
*sctx
= (struct si_context
*)ctx
;
2488 struct si_image_handle
*img_handle
;
2489 struct pipe_image_view
*view
;
2490 struct hash_entry
*entry
;
2492 entry
= _mesa_hash_table_search(sctx
->img_handles
, (void *)handle
);
2496 img_handle
= (struct si_image_handle
*)entry
->data
;
2497 view
= &img_handle
->view
;
2500 struct r600_resource
*res
=
2501 (struct r600_resource
*)view
->resource
;
2503 if (res
->b
.b
.target
!= PIPE_BUFFER
) {
2504 struct r600_texture
*rtex
= (struct r600_texture
*)res
;
2505 unsigned level
= view
->u
.tex
.level
;
2507 img_handle
->needs_color_decompress
=
2508 color_needs_decompression(rtex
);
2510 if (vi_dcc_enabled(rtex
, level
) &&
2511 p_atomic_read(&rtex
->framebuffers_bound
))
2512 sctx
->need_check_render_feedback
= true;
2514 si_invalidate_bindless_buf_desc(sctx
, img_handle
->desc
,
2516 view
->u
.buf
.offset
);
2519 /* Add the image handle to the per-context list. */
2520 util_dynarray_append(&sctx
->resident_img_handles
,
2521 struct si_image_handle
*, img_handle
);
2523 /* Add the buffers to the current CS in case si_begin_new_cs()
2524 * is not going to be called.
2526 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
2527 img_handle
->desc
->buffer
,
2528 RADEON_USAGE_READWRITE
,
2529 RADEON_PRIO_DESCRIPTORS
);
2531 si_sampler_view_add_buffer(sctx
, view
->resource
,
2532 (access
& PIPE_IMAGE_ACCESS_WRITE
) ?
2533 RADEON_USAGE_READWRITE
:
2534 RADEON_USAGE_READ
, false, false);
2536 /* Remove the image handle from the per-context list. */
2537 util_dynarray_delete_unordered(&sctx
->resident_img_handles
,
2538 struct si_image_handle
*,
2544 void si_all_resident_buffers_begin_new_cs(struct si_context
*sctx
)
2546 unsigned num_resident_tex_handles
, num_resident_img_handles
;
2547 unsigned num_bindless_descriptors
;
2550 num_resident_tex_handles
= sctx
->resident_tex_handles
.size
/
2551 sizeof(struct si_texture_handle
*);
2552 num_resident_img_handles
= sctx
->resident_img_handles
.size
/
2553 sizeof(struct si_image_handle
*);
2555 /* Skip adding the bindless descriptors when no handles are resident.
2557 if (!num_resident_tex_handles
&& !num_resident_img_handles
)
2560 num_bindless_descriptors
= sctx
->bindless_descriptors
.size
/
2561 sizeof(struct r600_resource
*);
2563 /* Add all bindless descriptors. */
2564 for (i
= 0; i
< num_bindless_descriptors
; i
++) {
2565 struct r600_resource
*desc
=
2566 *util_dynarray_element(&sctx
->bindless_descriptors
,
2567 struct r600_resource
*, i
);
2569 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, desc
,
2570 RADEON_USAGE_READWRITE
,
2571 RADEON_PRIO_DESCRIPTORS
);
2574 /* Add all resident texture handles. */
2575 for (i
= 0; i
< num_resident_tex_handles
; i
++) {
2576 struct si_texture_handle
*tex_handle
=
2577 *util_dynarray_element(&sctx
->resident_tex_handles
,
2578 struct si_texture_handle
*, i
);
2579 struct si_sampler_view
*sview
=
2580 (struct si_sampler_view
*)tex_handle
->view
;
2582 si_sampler_view_add_buffer(sctx
, sview
->base
.texture
,
2584 sview
->is_stencil_sampler
, false);
2587 /* Add all resident image handles. */
2588 for (i
= 0; i
< num_resident_img_handles
; i
++) {
2589 struct si_image_handle
*img_handle
=
2590 *util_dynarray_element(&sctx
->resident_img_handles
,
2591 struct si_image_handle
*, i
);
2592 struct pipe_image_view
*view
= &img_handle
->view
;
2594 si_sampler_view_add_buffer(sctx
, view
->resource
,
2595 RADEON_USAGE_READWRITE
,
2599 sctx
->b
.num_resident_handles
+= num_resident_tex_handles
+
2600 num_resident_img_handles
;
2603 /* INIT/DEINIT/UPLOAD */
2605 /* GFX9 has only 4KB of CE, while previous chips had 32KB. In order
2606 * to make CE RAM as useful as possible, this defines limits
2607 * for the number slots that can be in CE RAM on GFX9. If a shader
2608 * is using more, descriptors will be uploaded to memory directly and
2611 * These numbers are based on shader-db.
2613 static unsigned gfx9_max_ce_samplers
[SI_NUM_SHADERS
] = {
2614 [PIPE_SHADER_VERTEX
] = 0,
2615 [PIPE_SHADER_TESS_CTRL
] = 0,
2616 [PIPE_SHADER_TESS_EVAL
] = 1,
2617 [PIPE_SHADER_GEOMETRY
] = 0,
2618 [PIPE_SHADER_FRAGMENT
] = 24,
2619 [PIPE_SHADER_COMPUTE
] = 16,
2621 static unsigned gfx9_max_ce_images
[SI_NUM_SHADERS
] = {
2622 /* these must be even due to slot alignment */
2623 [PIPE_SHADER_VERTEX
] = 0,
2624 [PIPE_SHADER_TESS_CTRL
] = 0,
2625 [PIPE_SHADER_TESS_EVAL
] = 0,
2626 [PIPE_SHADER_GEOMETRY
] = 0,
2627 [PIPE_SHADER_FRAGMENT
] = 2,
2628 [PIPE_SHADER_COMPUTE
] = 8,
2630 static unsigned gfx9_max_ce_const_buffers
[SI_NUM_SHADERS
] = {
2631 [PIPE_SHADER_VERTEX
] = 9,
2632 [PIPE_SHADER_TESS_CTRL
] = 3,
2633 [PIPE_SHADER_TESS_EVAL
] = 5,
2634 [PIPE_SHADER_GEOMETRY
] = 0,
2635 [PIPE_SHADER_FRAGMENT
] = 8,
2636 [PIPE_SHADER_COMPUTE
] = 6,
2638 static unsigned gfx9_max_ce_shader_buffers
[SI_NUM_SHADERS
] = {
2639 [PIPE_SHADER_VERTEX
] = 0,
2640 [PIPE_SHADER_TESS_CTRL
] = 0,
2641 [PIPE_SHADER_TESS_EVAL
] = 0,
2642 [PIPE_SHADER_GEOMETRY
] = 0,
2643 [PIPE_SHADER_FRAGMENT
] = 12,
2644 [PIPE_SHADER_COMPUTE
] = 13,
2647 void si_init_all_descriptors(struct si_context
*sctx
)
2650 unsigned ce_offset
= 0;
2652 STATIC_ASSERT(GFX9_SGPR_TCS_CONST_AND_SHADER_BUFFERS
% 2 == 0);
2653 STATIC_ASSERT(GFX9_SGPR_GS_CONST_AND_SHADER_BUFFERS
% 2 == 0);
2655 for (i
= 0; i
< SI_NUM_SHADERS
; i
++) {
2656 bool gfx9_tcs
= false;
2657 bool gfx9_gs
= false;
2658 unsigned num_sampler_slots
= SI_NUM_IMAGES
/ 2 + SI_NUM_SAMPLERS
;
2659 unsigned num_buffer_slots
= SI_NUM_SHADER_BUFFERS
+ SI_NUM_CONST_BUFFERS
;
2661 unsigned first_sampler_ce_slot
= 0;
2662 unsigned num_sampler_ce_slots
= num_sampler_slots
;
2664 unsigned first_buffer_ce_slot
= 0;
2665 unsigned num_buffer_ce_slots
= num_buffer_slots
;
2667 /* Adjust CE slot ranges based on GFX9 CE RAM limits. */
2668 if (sctx
->b
.chip_class
>= GFX9
) {
2669 gfx9_tcs
= i
== PIPE_SHADER_TESS_CTRL
;
2670 gfx9_gs
= i
== PIPE_SHADER_GEOMETRY
;
2672 first_sampler_ce_slot
=
2673 si_get_image_slot(gfx9_max_ce_images
[i
] - 1) / 2;
2674 num_sampler_ce_slots
= gfx9_max_ce_images
[i
] / 2 +
2675 gfx9_max_ce_samplers
[i
];
2677 first_buffer_ce_slot
=
2678 si_get_shaderbuf_slot(gfx9_max_ce_shader_buffers
[i
] - 1);
2679 num_buffer_ce_slots
= gfx9_max_ce_shader_buffers
[i
] +
2680 gfx9_max_ce_const_buffers
[i
];
2683 si_init_buffer_resources(sctx
, &sctx
->const_and_shader_buffers
[i
],
2684 si_const_and_shader_buffer_descriptors(sctx
, i
),
2686 first_buffer_ce_slot
, num_buffer_ce_slots
,
2687 gfx9_tcs
? GFX9_SGPR_TCS_CONST_AND_SHADER_BUFFERS
:
2688 gfx9_gs
? GFX9_SGPR_GS_CONST_AND_SHADER_BUFFERS
:
2689 SI_SGPR_CONST_AND_SHADER_BUFFERS
,
2690 RADEON_USAGE_READWRITE
,
2692 RADEON_PRIO_SHADER_RW_BUFFER
,
2693 RADEON_PRIO_CONST_BUFFER
,
2696 struct si_descriptors
*desc
= si_sampler_and_image_descriptors(sctx
, i
);
2697 si_init_descriptors(sctx
, desc
,
2698 gfx9_tcs
? GFX9_SGPR_TCS_SAMPLERS_AND_IMAGES
:
2699 gfx9_gs
? GFX9_SGPR_GS_SAMPLERS_AND_IMAGES
:
2700 SI_SGPR_SAMPLERS_AND_IMAGES
,
2701 16, num_sampler_slots
,
2702 first_sampler_ce_slot
, num_sampler_ce_slots
,
2706 for (j
= 0; j
< SI_NUM_IMAGES
; j
++)
2707 memcpy(desc
->list
+ j
* 8, null_image_descriptor
, 8 * 4);
2708 for (; j
< SI_NUM_IMAGES
+ SI_NUM_SAMPLERS
* 2; j
++)
2709 memcpy(desc
->list
+ j
* 8, null_texture_descriptor
, 8 * 4);
2712 si_init_buffer_resources(sctx
, &sctx
->rw_buffers
,
2713 &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
],
2714 SI_NUM_RW_BUFFERS
, 0, SI_NUM_RW_BUFFERS
,
2716 /* The second set of usage/priority is used by
2717 * const buffers in RW buffer slots. */
2718 RADEON_USAGE_READWRITE
, RADEON_USAGE_READ
,
2719 RADEON_PRIO_SHADER_RINGS
, RADEON_PRIO_CONST_BUFFER
,
2721 sctx
->descriptors
[SI_DESCS_RW_BUFFERS
].num_active_slots
= SI_NUM_RW_BUFFERS
;
2723 si_init_descriptors(sctx
, &sctx
->vertex_buffers
, SI_SGPR_VERTEX_BUFFERS
,
2724 4, SI_NUM_VERTEX_BUFFERS
, 0, 0, NULL
);
2726 sctx
->descriptors_dirty
= u_bit_consecutive(0, SI_NUM_DESCS
);
2727 sctx
->total_ce_ram_allocated
= ce_offset
;
2729 if (sctx
->b
.chip_class
>= GFX9
)
2730 assert(ce_offset
<= 4096);
2732 assert(ce_offset
<= 32768);
2734 /* Set pipe_context functions. */
2735 sctx
->b
.b
.bind_sampler_states
= si_bind_sampler_states
;
2736 sctx
->b
.b
.set_shader_images
= si_set_shader_images
;
2737 sctx
->b
.b
.set_constant_buffer
= si_pipe_set_constant_buffer
;
2738 sctx
->b
.b
.set_polygon_stipple
= si_set_polygon_stipple
;
2739 sctx
->b
.b
.set_shader_buffers
= si_set_shader_buffers
;
2740 sctx
->b
.b
.set_sampler_views
= si_set_sampler_views
;
2741 sctx
->b
.b
.set_stream_output_targets
= si_set_streamout_targets
;
2742 sctx
->b
.b
.create_texture_handle
= si_create_texture_handle
;
2743 sctx
->b
.b
.delete_texture_handle
= si_delete_texture_handle
;
2744 sctx
->b
.b
.make_texture_handle_resident
= si_make_texture_handle_resident
;
2745 sctx
->b
.b
.create_image_handle
= si_create_image_handle
;
2746 sctx
->b
.b
.delete_image_handle
= si_delete_image_handle
;
2747 sctx
->b
.b
.make_image_handle_resident
= si_make_image_handle_resident
;
2748 sctx
->b
.invalidate_buffer
= si_invalidate_buffer
;
2749 sctx
->b
.rebind_buffer
= si_rebind_buffer
;
2751 /* Shader user data. */
2752 si_init_atom(sctx
, &sctx
->shader_userdata
.atom
, &sctx
->atoms
.s
.shader_userdata
,
2753 si_emit_graphics_shader_userdata
);
2755 /* Set default and immutable mappings. */
2756 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
, R_00B130_SPI_SHADER_USER_DATA_VS_0
);
2758 if (sctx
->b
.chip_class
>= GFX9
) {
2759 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_CTRL
,
2760 R_00B430_SPI_SHADER_USER_DATA_LS_0
);
2761 si_set_user_data_base(sctx
, PIPE_SHADER_GEOMETRY
,
2762 R_00B330_SPI_SHADER_USER_DATA_ES_0
);
2764 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_CTRL
,
2765 R_00B430_SPI_SHADER_USER_DATA_HS_0
);
2766 si_set_user_data_base(sctx
, PIPE_SHADER_GEOMETRY
,
2767 R_00B230_SPI_SHADER_USER_DATA_GS_0
);
2769 si_set_user_data_base(sctx
, PIPE_SHADER_FRAGMENT
, R_00B030_SPI_SHADER_USER_DATA_PS_0
);
2772 static void si_upload_bindless_descriptor(struct si_context
*sctx
,
2773 struct si_bindless_descriptor
*desc
)
2775 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
2776 uint64_t va
= desc
->buffer
->gpu_address
+ desc
->offset
;
2777 unsigned num_dwords
= sizeof(desc
->desc_list
) / 4;
2779 radeon_emit(cs
, PKT3(PKT3_WRITE_DATA
, 2 + num_dwords
, 0));
2780 radeon_emit(cs
, S_370_DST_SEL(V_370_TC_L2
) |
2781 S_370_WR_CONFIRM(1) |
2782 S_370_ENGINE_SEL(V_370_ME
));
2783 radeon_emit(cs
, va
);
2784 radeon_emit(cs
, va
>> 32);
2785 radeon_emit_array(cs
, desc
->desc_list
, num_dwords
);
2788 static void si_upload_bindless_descriptors(struct si_context
*sctx
)
2790 unsigned num_resident_tex_handles
, num_resident_img_handles
;
2793 if (!sctx
->bindless_descriptors_dirty
)
2796 /* Wait for graphics/compute to be idle before updating the resident
2797 * descriptors directly in memory, in case the GPU is using them.
2799 sctx
->b
.flags
|= SI_CONTEXT_PS_PARTIAL_FLUSH
|
2800 SI_CONTEXT_CS_PARTIAL_FLUSH
;
2801 si_emit_cache_flush(sctx
);
2803 num_resident_tex_handles
= sctx
->resident_tex_handles
.size
/
2804 sizeof(struct si_texture_handle
*);
2806 for (i
= 0; i
< num_resident_tex_handles
; i
++) {
2807 struct si_texture_handle
*tex_handle
=
2808 *util_dynarray_element(&sctx
->resident_tex_handles
,
2809 struct si_texture_handle
*, i
);
2810 struct si_bindless_descriptor
*desc
= tex_handle
->desc
;
2815 si_upload_bindless_descriptor(sctx
, desc
);
2816 desc
->dirty
= false;
2819 num_resident_img_handles
= sctx
->resident_img_handles
.size
/
2820 sizeof(struct si_image_handle
*);
2822 for (i
= 0; i
< num_resident_img_handles
; i
++) {
2823 struct si_image_handle
*img_handle
=
2824 *util_dynarray_element(&sctx
->resident_img_handles
,
2825 struct si_image_handle
*, i
);
2826 struct si_bindless_descriptor
*desc
= img_handle
->desc
;
2831 si_upload_bindless_descriptor(sctx
, desc
);
2832 desc
->dirty
= false;
2835 /* Invalidate L1 because it doesn't know that L2 changed. */
2836 sctx
->b
.flags
|= SI_CONTEXT_INV_SMEM_L1
;
2837 si_emit_cache_flush(sctx
);
2839 sctx
->bindless_descriptors_dirty
= false;
2842 bool si_upload_graphics_shader_descriptors(struct si_context
*sctx
)
2844 const unsigned mask
= u_bit_consecutive(0, SI_DESCS_FIRST_COMPUTE
);
2845 unsigned dirty
= sctx
->descriptors_dirty
& mask
;
2847 /* Assume nothing will go wrong: */
2848 sctx
->shader_pointers_dirty
|= dirty
;
2851 unsigned i
= u_bit_scan(&dirty
);
2853 if (!si_upload_descriptors(sctx
, &sctx
->descriptors
[i
],
2854 &sctx
->shader_userdata
.atom
))
2858 sctx
->descriptors_dirty
&= ~mask
;
2860 si_upload_bindless_descriptors(sctx
);
2865 bool si_upload_compute_shader_descriptors(struct si_context
*sctx
)
2867 /* Does not update rw_buffers as that is not needed for compute shaders
2868 * and the input buffer is using the same SGPR's anyway.
2870 const unsigned mask
= u_bit_consecutive(SI_DESCS_FIRST_COMPUTE
,
2871 SI_NUM_DESCS
- SI_DESCS_FIRST_COMPUTE
);
2872 unsigned dirty
= sctx
->descriptors_dirty
& mask
;
2874 /* Assume nothing will go wrong: */
2875 sctx
->shader_pointers_dirty
|= dirty
;
2878 unsigned i
= u_bit_scan(&dirty
);
2880 if (!si_upload_descriptors(sctx
, &sctx
->descriptors
[i
], NULL
))
2884 sctx
->descriptors_dirty
&= ~mask
;
2886 si_upload_bindless_descriptors(sctx
);
2891 void si_release_all_descriptors(struct si_context
*sctx
)
2895 for (i
= 0; i
< SI_NUM_SHADERS
; i
++) {
2896 si_release_buffer_resources(&sctx
->const_and_shader_buffers
[i
],
2897 si_const_and_shader_buffer_descriptors(sctx
, i
));
2898 si_release_sampler_views(&sctx
->samplers
[i
].views
);
2899 si_release_image_views(&sctx
->images
[i
]);
2901 si_release_buffer_resources(&sctx
->rw_buffers
,
2902 &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
]);
2904 for (i
= 0; i
< SI_NUM_DESCS
; ++i
)
2905 si_release_descriptors(&sctx
->descriptors
[i
]);
2906 si_release_descriptors(&sctx
->vertex_buffers
);
2909 void si_all_descriptors_begin_new_cs(struct si_context
*sctx
)
2913 for (i
= 0; i
< SI_NUM_SHADERS
; i
++) {
2914 si_buffer_resources_begin_new_cs(sctx
, &sctx
->const_and_shader_buffers
[i
]);
2915 si_sampler_views_begin_new_cs(sctx
, &sctx
->samplers
[i
].views
);
2916 si_image_views_begin_new_cs(sctx
, &sctx
->images
[i
]);
2918 si_buffer_resources_begin_new_cs(sctx
, &sctx
->rw_buffers
);
2919 si_vertex_buffers_begin_new_cs(sctx
);
2921 for (i
= 0; i
< SI_NUM_DESCS
; ++i
)
2922 si_descriptors_begin_new_cs(sctx
, &sctx
->descriptors
[i
]);
2924 si_shader_userdata_begin_new_cs(sctx
);
2927 void si_set_active_descriptors(struct si_context
*sctx
, unsigned desc_idx
,
2928 uint64_t new_active_mask
)
2930 struct si_descriptors
*desc
= &sctx
->descriptors
[desc_idx
];
2932 /* Ignore no-op updates and updates that disable all slots. */
2933 if (!new_active_mask
||
2934 new_active_mask
== u_bit_consecutive64(desc
->first_active_slot
,
2935 desc
->num_active_slots
))
2939 u_bit_scan_consecutive_range64(&new_active_mask
, &first
, &count
);
2940 assert(new_active_mask
== 0);
2942 /* Upload/dump descriptors if slots are being enabled. */
2943 if (first
< desc
->first_active_slot
||
2944 first
+ count
> desc
->first_active_slot
+ desc
->num_active_slots
)
2945 sctx
->descriptors_dirty
|= 1u << desc_idx
;
2947 /* Enable or disable CE for this descriptor array. */
2948 bool used_ce
= desc
->uses_ce
;
2949 desc
->uses_ce
= desc
->first_ce_slot
<= first
&&
2950 desc
->first_ce_slot
+ desc
->num_ce_slots
>= first
+ count
;
2952 if (desc
->uses_ce
!= used_ce
) {
2953 /* Upload or dump descriptors if we're disabling or enabling CE,
2955 sctx
->descriptors_dirty
|= 1u << desc_idx
;
2957 /* If we're enabling CE, re-upload all descriptors to CE RAM.
2958 * When CE was disabled, uploads to CE RAM stopped.
2960 if (desc
->uses_ce
) {
2962 u_bit_consecutive64(desc
->first_ce_slot
,
2963 desc
->num_ce_slots
);
2967 desc
->first_active_slot
= first
;
2968 desc
->num_active_slots
= count
;
2971 void si_set_active_descriptors_for_shader(struct si_context
*sctx
,
2972 struct si_shader_selector
*sel
)
2977 si_set_active_descriptors(sctx
,
2978 si_const_and_shader_buffer_descriptors_idx(sel
->type
),
2979 sel
->active_const_and_shader_buffers
);
2980 si_set_active_descriptors(sctx
,
2981 si_sampler_and_image_descriptors_idx(sel
->type
),
2982 sel
->active_samplers_and_images
);