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/u_format.h"
64 #include "util/u_memory.h"
65 #include "util/u_upload_mgr.h"
68 /* NULL image and buffer descriptor for textures (alpha = 1) and images
71 * For images, all fields must be zero except for the swizzle, which
72 * supports arbitrary combinations of 0s and 1s. The texture type must be
73 * any valid type (e.g. 1D). If the texture type isn't set, the hw hangs.
75 * For buffers, all fields must be zero. If they are not, the hw hangs.
77 * This is the only reason why the buffer descriptor must be in words [4:7].
79 static uint32_t null_texture_descriptor
[8] = {
83 S_008F1C_DST_SEL_W(V_008F1C_SQ_SEL_1
) |
84 S_008F1C_TYPE(V_008F1C_SQ_RSRC_IMG_1D
)
85 /* the rest must contain zeros, which is also used by the buffer
89 static uint32_t null_image_descriptor
[8] = {
93 S_008F1C_TYPE(V_008F1C_SQ_RSRC_IMG_1D
)
94 /* the rest must contain zeros, which is also used by the buffer
98 static void si_init_descriptors(struct si_context
*sctx
,
99 struct si_descriptors
*desc
,
100 unsigned shader_userdata_index
,
101 unsigned element_dw_size
,
102 unsigned num_elements
,
103 unsigned first_ce_slot
,
104 unsigned num_ce_slots
,
107 assert(num_elements
<= sizeof(desc
->dirty_mask
)*8);
109 desc
->list
= CALLOC(num_elements
, element_dw_size
* 4);
110 desc
->element_dw_size
= element_dw_size
;
111 desc
->num_elements
= num_elements
;
112 desc
->first_ce_slot
= sctx
->ce_ib
? first_ce_slot
: 0;
113 desc
->num_ce_slots
= sctx
->ce_ib
? num_ce_slots
: 0;
114 desc
->dirty_mask
= u_bit_consecutive64(0, num_elements
);
115 desc
->shader_userdata_offset
= shader_userdata_index
* 4;
117 if (desc
->num_ce_slots
) {
118 desc
->uses_ce
= true;
119 desc
->ce_offset
= *ce_offset
;
121 *ce_offset
+= element_dw_size
* desc
->num_ce_slots
* 4;
125 static void si_release_descriptors(struct si_descriptors
*desc
)
127 r600_resource_reference(&desc
->buffer
, NULL
);
131 static bool si_ce_upload(struct si_context
*sctx
, unsigned ce_offset
, unsigned size
,
132 unsigned *out_offset
, struct r600_resource
**out_buf
)
136 u_suballocator_alloc(sctx
->ce_suballocator
, size
,
137 si_optimal_tcc_alignment(sctx
, size
),
139 (struct pipe_resource
**)out_buf
);
143 va
= (*out_buf
)->gpu_address
+ *out_offset
;
145 radeon_emit(sctx
->ce_ib
, PKT3(PKT3_DUMP_CONST_RAM
, 3, 0));
146 radeon_emit(sctx
->ce_ib
, ce_offset
);
147 radeon_emit(sctx
->ce_ib
, size
/ 4);
148 radeon_emit(sctx
->ce_ib
, va
);
149 radeon_emit(sctx
->ce_ib
, va
>> 32);
151 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, *out_buf
,
152 RADEON_USAGE_READWRITE
, RADEON_PRIO_DESCRIPTORS
);
154 sctx
->ce_need_synchronization
= true;
158 void si_ce_save_all_descriptors_at_ib_end(struct si_context
* sctx
)
160 bool success
= si_ce_upload(sctx
, 0, sctx
->total_ce_ram_allocated
,
161 &sctx
->ce_ram_saved_offset
,
162 &sctx
->ce_ram_saved_buffer
);
167 void si_ce_restore_all_descriptors_at_ib_start(struct si_context
*sctx
)
169 if (!sctx
->ce_ram_saved_buffer
)
172 struct radeon_winsys_cs
*ib
= sctx
->ce_preamble_ib
;
176 uint64_t va
= sctx
->ce_ram_saved_buffer
->gpu_address
+
177 sctx
->ce_ram_saved_offset
;
179 radeon_emit(ib
, PKT3(PKT3_LOAD_CONST_RAM
, 3, 0));
181 radeon_emit(ib
, va
>> 32);
182 radeon_emit(ib
, sctx
->total_ce_ram_allocated
/ 4);
185 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
186 sctx
->ce_ram_saved_buffer
,
187 RADEON_USAGE_READ
, RADEON_PRIO_DESCRIPTORS
);
190 void si_ce_enable_loads(struct radeon_winsys_cs
*ib
)
192 radeon_emit(ib
, PKT3(PKT3_CONTEXT_CONTROL
, 1, 0));
193 radeon_emit(ib
, CONTEXT_CONTROL_LOAD_ENABLE(1) |
194 CONTEXT_CONTROL_LOAD_CE_RAM(1));
195 radeon_emit(ib
, CONTEXT_CONTROL_SHADOW_ENABLE(1));
198 static bool si_upload_descriptors(struct si_context
*sctx
,
199 struct si_descriptors
*desc
,
200 struct r600_atom
* atom
)
202 unsigned slot_size
= desc
->element_dw_size
* 4;
203 unsigned first_slot_offset
= desc
->first_active_slot
* slot_size
;
204 unsigned upload_size
= desc
->num_active_slots
* slot_size
;
206 /* Skip the upload if no shader is using the descriptors. dirty_mask
207 * will stay dirty and the descriptors will be uploaded when there is
208 * a shader using them.
214 const uint32_t *list
= desc
->list
+
215 desc
->first_ce_slot
* desc
->element_dw_size
;
216 uint64_t mask
= (desc
->dirty_mask
>> desc
->first_ce_slot
) &
217 u_bit_consecutive64(0, desc
->num_ce_slots
);
222 u_bit_scan_consecutive_range64(&mask
, &begin
, &count
);
224 begin
*= desc
->element_dw_size
;
225 count
*= desc
->element_dw_size
;
227 radeon_emit(sctx
->ce_ib
,
228 PKT3(PKT3_WRITE_CONST_RAM
, count
, 0));
229 radeon_emit(sctx
->ce_ib
, desc
->ce_offset
+ begin
* 4);
230 radeon_emit_array(sctx
->ce_ib
, list
+ begin
, count
);
233 if (!si_ce_upload(sctx
,
235 (first_slot_offset
- desc
->first_ce_slot
* slot_size
),
236 upload_size
, (unsigned*)&desc
->buffer_offset
,
242 u_upload_alloc(sctx
->b
.b
.const_uploader
, 0, upload_size
,
243 si_optimal_tcc_alignment(sctx
, upload_size
),
244 (unsigned*)&desc
->buffer_offset
,
245 (struct pipe_resource
**)&desc
->buffer
,
248 return false; /* skip the draw call */
250 util_memcpy_cpu_to_le32(ptr
, (char*)desc
->list
+ first_slot_offset
,
252 desc
->gpu_list
= ptr
- first_slot_offset
/ 4;
254 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, desc
->buffer
,
255 RADEON_USAGE_READ
, RADEON_PRIO_DESCRIPTORS
);
258 /* The shader pointer should point to slot 0. */
259 desc
->buffer_offset
-= first_slot_offset
;
261 desc
->dirty_mask
= 0;
264 si_mark_atom_dirty(sctx
, atom
);
270 si_descriptors_begin_new_cs(struct si_context
*sctx
, struct si_descriptors
*desc
)
275 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, desc
->buffer
,
276 RADEON_USAGE_READ
, RADEON_PRIO_DESCRIPTORS
);
282 si_sampler_and_image_descriptors_idx(unsigned shader
)
284 return SI_DESCS_FIRST_SHADER
+ shader
* SI_NUM_SHADER_DESCS
+
285 SI_SHADER_DESCS_SAMPLERS_AND_IMAGES
;
288 static struct si_descriptors
*
289 si_sampler_and_image_descriptors(struct si_context
*sctx
, unsigned shader
)
291 return &sctx
->descriptors
[si_sampler_and_image_descriptors_idx(shader
)];
294 static void si_release_sampler_views(struct si_sampler_views
*views
)
298 for (i
= 0; i
< ARRAY_SIZE(views
->views
); i
++) {
299 pipe_sampler_view_reference(&views
->views
[i
], NULL
);
303 static void si_sampler_view_add_buffer(struct si_context
*sctx
,
304 struct pipe_resource
*resource
,
305 enum radeon_bo_usage usage
,
306 bool is_stencil_sampler
,
309 struct r600_resource
*rres
;
310 struct r600_texture
*rtex
;
311 enum radeon_bo_priority priority
;
316 if (resource
->target
!= PIPE_BUFFER
) {
317 struct r600_texture
*tex
= (struct r600_texture
*)resource
;
319 if (tex
->is_depth
&& !r600_can_sample_zs(tex
, is_stencil_sampler
))
320 resource
= &tex
->flushed_depth_texture
->resource
.b
.b
;
323 rres
= (struct r600_resource
*)resource
;
324 priority
= r600_get_sampler_view_priority(rres
);
326 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
327 rres
, usage
, priority
,
330 if (resource
->target
== PIPE_BUFFER
)
333 /* Now add separate DCC or HTILE. */
334 rtex
= (struct r600_texture
*)resource
;
335 if (rtex
->dcc_separate_buffer
) {
336 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
337 rtex
->dcc_separate_buffer
, usage
,
338 RADEON_PRIO_DCC
, check_mem
);
341 if (rtex
->htile_buffer
&&
342 rtex
->tc_compatible_htile
) {
343 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
344 rtex
->htile_buffer
, usage
,
345 RADEON_PRIO_HTILE
, check_mem
);
349 static void si_sampler_views_begin_new_cs(struct si_context
*sctx
,
350 struct si_sampler_views
*views
)
352 unsigned mask
= views
->enabled_mask
;
354 /* Add buffers to the CS. */
356 int i
= u_bit_scan(&mask
);
357 struct si_sampler_view
*sview
= (struct si_sampler_view
*)views
->views
[i
];
359 si_sampler_view_add_buffer(sctx
, sview
->base
.texture
,
361 sview
->is_stencil_sampler
, false);
365 /* Set buffer descriptor fields that can be changed by reallocations. */
366 static void si_set_buf_desc_address(struct r600_resource
*buf
,
367 uint64_t offset
, uint32_t *state
)
369 uint64_t va
= buf
->gpu_address
+ offset
;
372 state
[1] &= C_008F04_BASE_ADDRESS_HI
;
373 state
[1] |= S_008F04_BASE_ADDRESS_HI(va
>> 32);
376 /* Set texture descriptor fields that can be changed by reallocations.
379 * \param base_level_info information of the level of BASE_ADDRESS
380 * \param base_level the level of BASE_ADDRESS
381 * \param first_level pipe_sampler_view.u.tex.first_level
382 * \param block_width util_format_get_blockwidth()
383 * \param is_stencil select between separate Z & Stencil
384 * \param state descriptor to update
386 void si_set_mutable_tex_desc_fields(struct si_screen
*sscreen
,
387 struct r600_texture
*tex
,
388 const struct legacy_surf_level
*base_level_info
,
389 unsigned base_level
, unsigned first_level
,
390 unsigned block_width
, bool is_stencil
,
393 uint64_t va
, meta_va
= 0;
395 if (tex
->is_depth
&& !r600_can_sample_zs(tex
, is_stencil
)) {
396 tex
= tex
->flushed_depth_texture
;
400 va
= tex
->resource
.gpu_address
;
402 if (sscreen
->b
.chip_class
>= GFX9
) {
403 /* Only stencil_offset needs to be added here. */
405 va
+= tex
->surface
.u
.gfx9
.stencil_offset
;
407 va
+= tex
->surface
.u
.gfx9
.surf_offset
;
409 va
+= base_level_info
->offset
;
413 state
[1] &= C_008F14_BASE_ADDRESS_HI
;
414 state
[1] |= S_008F14_BASE_ADDRESS_HI(va
>> 40);
416 if (sscreen
->b
.chip_class
>= VI
) {
417 state
[6] &= C_008F28_COMPRESSION_EN
;
420 if (vi_dcc_enabled(tex
, first_level
)) {
421 meta_va
= (!tex
->dcc_separate_buffer
? tex
->resource
.gpu_address
: 0) +
424 if (sscreen
->b
.chip_class
<= VI
)
425 meta_va
+= base_level_info
->dcc_offset
;
426 } else if (tex
->tc_compatible_htile
) {
427 meta_va
= tex
->htile_buffer
->gpu_address
;
431 state
[6] |= S_008F28_COMPRESSION_EN(1);
432 state
[7] = meta_va
>> 8;
436 if (sscreen
->b
.chip_class
>= GFX9
) {
437 state
[3] &= C_008F1C_SW_MODE
;
438 state
[4] &= C_008F20_PITCH_GFX9
;
441 state
[3] |= S_008F1C_SW_MODE(tex
->surface
.u
.gfx9
.stencil
.swizzle_mode
);
442 state
[4] |= S_008F20_PITCH_GFX9(tex
->surface
.u
.gfx9
.stencil
.epitch
);
444 state
[3] |= S_008F1C_SW_MODE(tex
->surface
.u
.gfx9
.surf
.swizzle_mode
);
445 state
[4] |= S_008F20_PITCH_GFX9(tex
->surface
.u
.gfx9
.surf
.epitch
);
448 state
[5] &= C_008F24_META_DATA_ADDRESS
&
449 C_008F24_META_PIPE_ALIGNED
&
450 C_008F24_META_RB_ALIGNED
;
452 struct gfx9_surf_meta_flags meta
;
455 meta
= tex
->surface
.u
.gfx9
.dcc
;
457 meta
= tex
->surface
.u
.gfx9
.htile
;
459 state
[5] |= S_008F24_META_DATA_ADDRESS(meta_va
>> 40) |
460 S_008F24_META_PIPE_ALIGNED(meta
.pipe_aligned
) |
461 S_008F24_META_RB_ALIGNED(meta
.rb_aligned
);
465 unsigned pitch
= base_level_info
->nblk_x
* block_width
;
466 unsigned index
= si_tile_mode_index(tex
, base_level
, is_stencil
);
468 state
[3] &= C_008F1C_TILING_INDEX
;
469 state
[3] |= S_008F1C_TILING_INDEX(index
);
470 state
[4] &= C_008F20_PITCH_GFX6
;
471 state
[4] |= S_008F20_PITCH_GFX6(pitch
- 1);
475 static void si_set_sampler_view(struct si_context
*sctx
,
477 unsigned slot
, struct pipe_sampler_view
*view
,
478 bool disallow_early_out
)
480 struct si_sampler_views
*views
= &sctx
->samplers
[shader
].views
;
481 struct si_sampler_view
*rview
= (struct si_sampler_view
*)view
;
482 struct si_descriptors
*descs
= si_sampler_and_image_descriptors(sctx
, shader
);
483 unsigned desc_slot
= si_get_sampler_slot(slot
);
484 uint32_t *desc
= descs
->list
+ desc_slot
* 16;
486 if (views
->views
[slot
] == view
&& !disallow_early_out
)
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
&& rview
->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 rview
->dcc_incompatible
= false;
501 assert(rtex
); /* views with texture == NULL aren't supported */
502 pipe_sampler_view_reference(&views
->views
[slot
], view
);
503 memcpy(desc
, rview
->state
, 8*4);
506 rtex
->resource
.bind_history
|= PIPE_BIND_SAMPLER_VIEW
;
508 si_set_buf_desc_address(&rtex
->resource
,
512 bool is_separate_stencil
=
513 rtex
->db_compatible
&&
514 rview
->is_stencil_sampler
;
516 si_set_mutable_tex_desc_fields(sctx
->screen
, rtex
,
517 rview
->base_level_info
,
519 rview
->base
.u
.tex
.first_level
,
525 if (!is_buffer
&& rtex
->fmask
.size
) {
527 rview
->fmask_state
, 8*4);
529 /* Disable FMASK and bind sampler state in [12:15]. */
531 null_texture_descriptor
, 4*4);
533 if (views
->sampler_states
[slot
])
535 views
->sampler_states
[slot
]->val
, 4*4);
538 views
->enabled_mask
|= 1u << slot
;
540 /* Since this can flush, it must be done after enabled_mask is
542 si_sampler_view_add_buffer(sctx
, view
->texture
,
544 rview
->is_stencil_sampler
, true);
546 pipe_sampler_view_reference(&views
->views
[slot
], NULL
);
547 memcpy(desc
, null_texture_descriptor
, 8*4);
548 /* Only clear the lower dwords of FMASK. */
549 memcpy(desc
+ 8, null_texture_descriptor
, 4*4);
550 /* Re-set the sampler state if we are transitioning from FMASK. */
551 if (views
->sampler_states
[slot
])
553 views
->sampler_states
[slot
]->val
, 4*4);
555 views
->enabled_mask
&= ~(1u << slot
);
558 descs
->dirty_mask
|= 1ull << desc_slot
;
559 sctx
->descriptors_dirty
|= 1u << si_sampler_and_image_descriptors_idx(shader
);
562 static bool is_compressed_colortex(struct r600_texture
*rtex
)
564 return rtex
->fmask
.size
||
565 (rtex
->dirty_level_mask
&&
566 (rtex
->cmask
.size
|| rtex
->dcc_offset
));
569 static bool depth_needs_decompression(struct r600_texture
*rtex
,
570 struct si_sampler_view
*sview
)
572 return rtex
->db_compatible
&&
573 (!rtex
->tc_compatible_htile
||
574 !r600_can_sample_zs(rtex
, sview
->is_stencil_sampler
));
577 static void si_update_compressed_tex_shader_mask(struct si_context
*sctx
,
580 struct si_textures_info
*samplers
= &sctx
->samplers
[shader
];
581 unsigned shader_bit
= 1 << shader
;
583 if (samplers
->depth_texture_mask
||
584 samplers
->compressed_colortex_mask
||
585 sctx
->images
[shader
].compressed_colortex_mask
)
586 sctx
->compressed_tex_shader_mask
|= shader_bit
;
588 sctx
->compressed_tex_shader_mask
&= ~shader_bit
;
591 static void si_set_sampler_views(struct pipe_context
*ctx
,
592 enum pipe_shader_type shader
, unsigned start
,
594 struct pipe_sampler_view
**views
)
596 struct si_context
*sctx
= (struct si_context
*)ctx
;
597 struct si_textures_info
*samplers
= &sctx
->samplers
[shader
];
600 if (!count
|| shader
>= SI_NUM_SHADERS
)
603 for (i
= 0; i
< count
; i
++) {
604 unsigned slot
= start
+ i
;
606 if (!views
|| !views
[i
]) {
607 samplers
->depth_texture_mask
&= ~(1u << slot
);
608 samplers
->compressed_colortex_mask
&= ~(1u << slot
);
609 si_set_sampler_view(sctx
, shader
, slot
, NULL
, false);
613 si_set_sampler_view(sctx
, shader
, slot
, views
[i
], false);
615 if (views
[i
]->texture
&& views
[i
]->texture
->target
!= PIPE_BUFFER
) {
616 struct r600_texture
*rtex
=
617 (struct r600_texture
*)views
[i
]->texture
;
618 struct si_sampler_view
*rview
= (struct si_sampler_view
*)views
[i
];
620 if (depth_needs_decompression(rtex
, rview
)) {
621 samplers
->depth_texture_mask
|= 1u << slot
;
623 samplers
->depth_texture_mask
&= ~(1u << slot
);
625 if (is_compressed_colortex(rtex
)) {
626 samplers
->compressed_colortex_mask
|= 1u << slot
;
628 samplers
->compressed_colortex_mask
&= ~(1u << slot
);
631 if (rtex
->dcc_offset
&&
632 p_atomic_read(&rtex
->framebuffers_bound
))
633 sctx
->need_check_render_feedback
= true;
635 samplers
->depth_texture_mask
&= ~(1u << slot
);
636 samplers
->compressed_colortex_mask
&= ~(1u << slot
);
640 si_update_compressed_tex_shader_mask(sctx
, shader
);
644 si_samplers_update_compressed_colortex_mask(struct si_textures_info
*samplers
)
646 unsigned mask
= samplers
->views
.enabled_mask
;
649 int i
= u_bit_scan(&mask
);
650 struct pipe_resource
*res
= samplers
->views
.views
[i
]->texture
;
652 if (res
&& res
->target
!= PIPE_BUFFER
) {
653 struct r600_texture
*rtex
= (struct r600_texture
*)res
;
655 if (is_compressed_colortex(rtex
)) {
656 samplers
->compressed_colortex_mask
|= 1u << i
;
658 samplers
->compressed_colortex_mask
&= ~(1u << i
);
667 si_release_image_views(struct si_images_info
*images
)
671 for (i
= 0; i
< SI_NUM_IMAGES
; ++i
) {
672 struct pipe_image_view
*view
= &images
->views
[i
];
674 pipe_resource_reference(&view
->resource
, NULL
);
679 si_image_views_begin_new_cs(struct si_context
*sctx
, struct si_images_info
*images
)
681 uint mask
= images
->enabled_mask
;
683 /* Add buffers to the CS. */
685 int i
= u_bit_scan(&mask
);
686 struct pipe_image_view
*view
= &images
->views
[i
];
688 assert(view
->resource
);
690 si_sampler_view_add_buffer(sctx
, view
->resource
,
691 RADEON_USAGE_READWRITE
, false, false);
696 si_disable_shader_image(struct si_context
*ctx
, unsigned shader
, unsigned slot
)
698 struct si_images_info
*images
= &ctx
->images
[shader
];
700 if (images
->enabled_mask
& (1u << slot
)) {
701 struct si_descriptors
*descs
= si_sampler_and_image_descriptors(ctx
, shader
);
702 unsigned desc_slot
= si_get_image_slot(slot
);
704 pipe_resource_reference(&images
->views
[slot
].resource
, NULL
);
705 images
->compressed_colortex_mask
&= ~(1 << slot
);
707 memcpy(descs
->list
+ desc_slot
*8, null_image_descriptor
, 8*4);
708 images
->enabled_mask
&= ~(1u << slot
);
709 /* two 8-byte images share one 16-byte slot */
710 descs
->dirty_mask
|= 1u << (desc_slot
/ 2);
711 ctx
->descriptors_dirty
|= 1u << si_sampler_and_image_descriptors_idx(shader
);
716 si_mark_image_range_valid(const struct pipe_image_view
*view
)
718 struct r600_resource
*res
= (struct r600_resource
*)view
->resource
;
720 assert(res
&& res
->b
.b
.target
== PIPE_BUFFER
);
722 util_range_add(&res
->valid_buffer_range
,
724 view
->u
.buf
.offset
+ view
->u
.buf
.size
);
727 static void si_set_shader_image(struct si_context
*ctx
,
729 unsigned slot
, const struct pipe_image_view
*view
,
730 bool skip_decompress
)
732 struct si_screen
*screen
= ctx
->screen
;
733 struct si_images_info
*images
= &ctx
->images
[shader
];
734 struct si_descriptors
*descs
= si_sampler_and_image_descriptors(ctx
, shader
);
735 struct r600_resource
*res
;
736 unsigned desc_slot
= si_get_image_slot(slot
);
737 uint32_t *desc
= descs
->list
+ desc_slot
* 8;
739 if (!view
|| !view
->resource
) {
740 si_disable_shader_image(ctx
, shader
, slot
);
744 res
= (struct r600_resource
*)view
->resource
;
746 if (&images
->views
[slot
] != view
)
747 util_copy_image_view(&images
->views
[slot
], view
);
749 if (res
->b
.b
.target
== PIPE_BUFFER
) {
750 if (view
->access
& PIPE_IMAGE_ACCESS_WRITE
)
751 si_mark_image_range_valid(view
);
753 si_make_buffer_descriptor(screen
, res
,
756 view
->u
.buf
.size
, desc
);
757 si_set_buf_desc_address(res
, view
->u
.buf
.offset
, desc
+ 4);
759 images
->compressed_colortex_mask
&= ~(1 << slot
);
760 res
->bind_history
|= PIPE_BIND_SHADER_IMAGE
;
762 static const unsigned char swizzle
[4] = { 0, 1, 2, 3 };
763 struct r600_texture
*tex
= (struct r600_texture
*)res
;
764 unsigned level
= view
->u
.tex
.level
;
765 unsigned width
, height
, depth
, hw_level
;
766 bool uses_dcc
= vi_dcc_enabled(tex
, level
);
768 assert(!tex
->is_depth
);
769 assert(tex
->fmask
.size
== 0);
771 if (uses_dcc
&& !skip_decompress
&&
772 (view
->access
& PIPE_IMAGE_ACCESS_WRITE
||
773 !vi_dcc_formats_compatible(res
->b
.b
.format
, view
->format
))) {
774 /* If DCC can't be disabled, at least decompress it.
775 * The decompression is relatively cheap if the surface
776 * has been decompressed already.
778 if (r600_texture_disable_dcc(&ctx
->b
, tex
))
781 ctx
->b
.decompress_dcc(&ctx
->b
.b
, tex
);
784 if (is_compressed_colortex(tex
)) {
785 images
->compressed_colortex_mask
|= 1 << slot
;
787 images
->compressed_colortex_mask
&= ~(1 << slot
);
791 p_atomic_read(&tex
->framebuffers_bound
))
792 ctx
->need_check_render_feedback
= true;
794 if (ctx
->b
.chip_class
>= GFX9
) {
795 /* Always set the base address. The swizzle modes don't
796 * allow setting mipmap level offsets as the base.
798 width
= res
->b
.b
.width0
;
799 height
= res
->b
.b
.height0
;
800 depth
= res
->b
.b
.depth0
;
803 /* Always force the base level to the selected level.
805 * This is required for 3D textures, where otherwise
806 * selecting a single slice for non-layered bindings
807 * fails. It doesn't hurt the other targets.
809 width
= u_minify(res
->b
.b
.width0
, level
);
810 height
= u_minify(res
->b
.b
.height0
, level
);
811 depth
= u_minify(res
->b
.b
.depth0
, level
);
815 si_make_texture_descriptor(screen
, tex
,
816 false, res
->b
.b
.target
,
817 view
->format
, swizzle
,
819 view
->u
.tex
.first_layer
,
820 view
->u
.tex
.last_layer
,
821 width
, height
, depth
,
823 si_set_mutable_tex_desc_fields(screen
, tex
,
824 &tex
->surface
.u
.legacy
.level
[level
],
826 util_format_get_blockwidth(view
->format
),
830 images
->enabled_mask
|= 1u << slot
;
831 /* two 8-byte images share one 16-byte slot */
832 descs
->dirty_mask
|= 1u << (desc_slot
/ 2);
833 ctx
->descriptors_dirty
|= 1u << si_sampler_and_image_descriptors_idx(shader
);
835 /* Since this can flush, it must be done after enabled_mask is updated. */
836 si_sampler_view_add_buffer(ctx
, &res
->b
.b
,
837 RADEON_USAGE_READWRITE
, false, true);
841 si_set_shader_images(struct pipe_context
*pipe
,
842 enum pipe_shader_type shader
,
843 unsigned start_slot
, unsigned count
,
844 const struct pipe_image_view
*views
)
846 struct si_context
*ctx
= (struct si_context
*)pipe
;
849 assert(shader
< SI_NUM_SHADERS
);
854 assert(start_slot
+ count
<= SI_NUM_IMAGES
);
857 for (i
= 0, slot
= start_slot
; i
< count
; ++i
, ++slot
)
858 si_set_shader_image(ctx
, shader
, slot
, &views
[i
], false);
860 for (i
= 0, slot
= start_slot
; i
< count
; ++i
, ++slot
)
861 si_set_shader_image(ctx
, shader
, slot
, NULL
, false);
864 si_update_compressed_tex_shader_mask(ctx
, shader
);
868 si_images_update_compressed_colortex_mask(struct si_images_info
*images
)
870 unsigned mask
= images
->enabled_mask
;
873 int i
= u_bit_scan(&mask
);
874 struct pipe_resource
*res
= images
->views
[i
].resource
;
876 if (res
&& res
->target
!= PIPE_BUFFER
) {
877 struct r600_texture
*rtex
= (struct r600_texture
*)res
;
879 if (is_compressed_colortex(rtex
)) {
880 images
->compressed_colortex_mask
|= 1 << i
;
882 images
->compressed_colortex_mask
&= ~(1 << i
);
890 static void si_bind_sampler_states(struct pipe_context
*ctx
,
891 enum pipe_shader_type shader
,
892 unsigned start
, unsigned count
, void **states
)
894 struct si_context
*sctx
= (struct si_context
*)ctx
;
895 struct si_textures_info
*samplers
= &sctx
->samplers
[shader
];
896 struct si_descriptors
*desc
= si_sampler_and_image_descriptors(sctx
, shader
);
897 struct si_sampler_state
**sstates
= (struct si_sampler_state
**)states
;
900 if (!count
|| shader
>= SI_NUM_SHADERS
)
903 for (i
= 0; i
< count
; i
++) {
904 unsigned slot
= start
+ i
;
905 unsigned desc_slot
= si_get_sampler_slot(slot
);
908 sstates
[i
] == samplers
->views
.sampler_states
[slot
])
912 assert(sstates
[i
]->magic
== SI_SAMPLER_STATE_MAGIC
);
914 samplers
->views
.sampler_states
[slot
] = sstates
[i
];
916 /* If FMASK is bound, don't overwrite it.
917 * The sampler state will be set after FMASK is unbound.
919 if (samplers
->views
.views
[slot
] &&
920 samplers
->views
.views
[slot
]->texture
&&
921 samplers
->views
.views
[slot
]->texture
->target
!= PIPE_BUFFER
&&
922 ((struct r600_texture
*)samplers
->views
.views
[slot
]->texture
)->fmask
.size
)
925 memcpy(desc
->list
+ desc_slot
* 16 + 12, sstates
[i
]->val
, 4*4);
926 desc
->dirty_mask
|= 1ull << desc_slot
;
927 sctx
->descriptors_dirty
|= 1u << si_sampler_and_image_descriptors_idx(shader
);
931 /* BUFFER RESOURCES */
933 static void si_init_buffer_resources(struct si_context
*sctx
,
934 struct si_buffer_resources
*buffers
,
935 struct si_descriptors
*descs
,
936 unsigned num_buffers
,
937 unsigned first_ce_slot
,
938 unsigned num_ce_slots
,
939 unsigned shader_userdata_index
,
940 enum radeon_bo_usage shader_usage
,
941 enum radeon_bo_usage shader_usage_constbuf
,
942 enum radeon_bo_priority priority
,
943 enum radeon_bo_priority priority_constbuf
,
946 buffers
->shader_usage
= shader_usage
;
947 buffers
->shader_usage_constbuf
= shader_usage_constbuf
;
948 buffers
->priority
= priority
;
949 buffers
->priority_constbuf
= priority_constbuf
;
950 buffers
->buffers
= CALLOC(num_buffers
, sizeof(struct pipe_resource
*));
952 si_init_descriptors(sctx
, descs
, shader_userdata_index
, 4, num_buffers
,
953 first_ce_slot
, num_ce_slots
, ce_offset
);
956 static void si_release_buffer_resources(struct si_buffer_resources
*buffers
,
957 struct si_descriptors
*descs
)
961 for (i
= 0; i
< descs
->num_elements
; i
++) {
962 pipe_resource_reference(&buffers
->buffers
[i
], NULL
);
965 FREE(buffers
->buffers
);
968 static void si_buffer_resources_begin_new_cs(struct si_context
*sctx
,
969 struct si_buffer_resources
*buffers
)
971 unsigned mask
= buffers
->enabled_mask
;
973 /* Add buffers to the CS. */
975 int i
= u_bit_scan(&mask
);
977 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
978 r600_resource(buffers
->buffers
[i
]),
979 i
< SI_NUM_SHADER_BUFFERS
? buffers
->shader_usage
:
980 buffers
->shader_usage_constbuf
,
981 i
< SI_NUM_SHADER_BUFFERS
? buffers
->priority
:
982 buffers
->priority_constbuf
);
986 static void si_get_buffer_from_descriptors(struct si_buffer_resources
*buffers
,
987 struct si_descriptors
*descs
,
988 unsigned idx
, struct pipe_resource
**buf
,
989 unsigned *offset
, unsigned *size
)
991 pipe_resource_reference(buf
, buffers
->buffers
[idx
]);
993 struct r600_resource
*res
= r600_resource(*buf
);
994 const uint32_t *desc
= descs
->list
+ idx
* 4;
999 assert(G_008F04_STRIDE(desc
[1]) == 0);
1000 va
= ((uint64_t)desc
[1] << 32) | desc
[0];
1002 assert(va
>= res
->gpu_address
&& va
+ *size
<= res
->gpu_address
+ res
->bo_size
);
1003 *offset
= va
- res
->gpu_address
;
1007 /* VERTEX BUFFERS */
1009 static void si_vertex_buffers_begin_new_cs(struct si_context
*sctx
)
1011 struct si_descriptors
*desc
= &sctx
->vertex_buffers
;
1012 int count
= sctx
->vertex_elements
? sctx
->vertex_elements
->count
: 0;
1015 for (i
= 0; i
< count
; i
++) {
1016 int vb
= sctx
->vertex_elements
->elements
[i
].vertex_buffer_index
;
1018 if (vb
>= ARRAY_SIZE(sctx
->vertex_buffer
))
1020 if (!sctx
->vertex_buffer
[vb
].buffer
.resource
)
1023 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
1024 (struct r600_resource
*)sctx
->vertex_buffer
[vb
].buffer
.resource
,
1025 RADEON_USAGE_READ
, RADEON_PRIO_VERTEX_BUFFER
);
1030 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
1031 desc
->buffer
, RADEON_USAGE_READ
,
1032 RADEON_PRIO_DESCRIPTORS
);
1035 bool si_upload_vertex_buffer_descriptors(struct si_context
*sctx
)
1037 struct si_vertex_element
*velems
= sctx
->vertex_elements
;
1038 struct si_descriptors
*desc
= &sctx
->vertex_buffers
;
1040 unsigned desc_list_byte_size
;
1041 unsigned first_vb_use_mask
;
1045 if (!sctx
->vertex_buffers_dirty
|| !velems
)
1048 count
= velems
->count
;
1053 desc_list_byte_size
= velems
->desc_list_byte_size
;
1054 first_vb_use_mask
= velems
->first_vb_use_mask
;
1056 /* Vertex buffer descriptors are the only ones which are uploaded
1057 * directly through a staging buffer and don't go through
1058 * the fine-grained upload path.
1060 u_upload_alloc(sctx
->b
.b
.const_uploader
, 0,
1061 desc_list_byte_size
,
1062 si_optimal_tcc_alignment(sctx
, desc_list_byte_size
),
1063 (unsigned*)&desc
->buffer_offset
,
1064 (struct pipe_resource
**)&desc
->buffer
, (void**)&ptr
);
1068 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
1069 desc
->buffer
, RADEON_USAGE_READ
,
1070 RADEON_PRIO_DESCRIPTORS
);
1072 assert(count
<= SI_MAX_ATTRIBS
);
1074 for (i
= 0; i
< count
; i
++) {
1075 struct pipe_vertex_element
*ve
= &velems
->elements
[i
];
1076 struct pipe_vertex_buffer
*vb
;
1077 struct r600_resource
*rbuffer
;
1079 unsigned vbo_index
= ve
->vertex_buffer_index
;
1080 uint32_t *desc
= &ptr
[i
*4];
1082 vb
= &sctx
->vertex_buffer
[vbo_index
];
1083 rbuffer
= (struct r600_resource
*)vb
->buffer
.resource
;
1085 memset(desc
, 0, 16);
1089 offset
= vb
->buffer_offset
+ ve
->src_offset
;
1090 va
= rbuffer
->gpu_address
+ offset
;
1092 /* Fill in T# buffer resource description */
1094 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1095 S_008F04_STRIDE(vb
->stride
);
1097 if (sctx
->b
.chip_class
!= VI
&& vb
->stride
) {
1098 /* Round up by rounding down and adding 1 */
1099 desc
[2] = (vb
->buffer
.resource
->width0
- offset
-
1100 velems
->format_size
[i
]) /
1103 desc
[2] = vb
->buffer
.resource
->width0
- offset
;
1106 desc
[3] = velems
->rsrc_word3
[i
];
1108 if (first_vb_use_mask
& (1 << i
)) {
1109 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
1110 (struct r600_resource
*)vb
->buffer
.resource
,
1111 RADEON_USAGE_READ
, RADEON_PRIO_VERTEX_BUFFER
);
1115 /* Don't flush the const cache. It would have a very negative effect
1116 * on performance (confirmed by testing). New descriptors are always
1117 * uploaded to a fresh new buffer, so I don't think flushing the const
1118 * cache is needed. */
1119 si_mark_atom_dirty(sctx
, &sctx
->shader_userdata
.atom
);
1120 if (sctx
->b
.chip_class
>= CIK
)
1121 si_mark_atom_dirty(sctx
, &sctx
->prefetch_L2
);
1122 sctx
->vertex_buffers_dirty
= false;
1123 sctx
->vertex_buffer_pointer_dirty
= true;
1128 /* CONSTANT BUFFERS */
1131 si_const_and_shader_buffer_descriptors_idx(unsigned shader
)
1133 return SI_DESCS_FIRST_SHADER
+ shader
* SI_NUM_SHADER_DESCS
+
1134 SI_SHADER_DESCS_CONST_AND_SHADER_BUFFERS
;
1137 static struct si_descriptors
*
1138 si_const_and_shader_buffer_descriptors(struct si_context
*sctx
, unsigned shader
)
1140 return &sctx
->descriptors
[si_const_and_shader_buffer_descriptors_idx(shader
)];
1143 void si_upload_const_buffer(struct si_context
*sctx
, struct r600_resource
**rbuffer
,
1144 const uint8_t *ptr
, unsigned size
, uint32_t *const_offset
)
1148 u_upload_alloc(sctx
->b
.b
.const_uploader
, 0, size
,
1149 si_optimal_tcc_alignment(sctx
, size
),
1151 (struct pipe_resource
**)rbuffer
, &tmp
);
1153 util_memcpy_cpu_to_le32(tmp
, ptr
, size
);
1156 static void si_set_constant_buffer(struct si_context
*sctx
,
1157 struct si_buffer_resources
*buffers
,
1158 unsigned descriptors_idx
,
1159 uint slot
, const struct pipe_constant_buffer
*input
)
1161 struct si_descriptors
*descs
= &sctx
->descriptors
[descriptors_idx
];
1162 assert(slot
< descs
->num_elements
);
1163 pipe_resource_reference(&buffers
->buffers
[slot
], NULL
);
1165 /* CIK cannot unbind a constant buffer (S_BUFFER_LOAD is buggy
1166 * with a NULL buffer). We need to use a dummy buffer instead. */
1167 if (sctx
->b
.chip_class
== CIK
&&
1168 (!input
|| (!input
->buffer
&& !input
->user_buffer
)))
1169 input
= &sctx
->null_const_buf
;
1171 if (input
&& (input
->buffer
|| input
->user_buffer
)) {
1172 struct pipe_resource
*buffer
= NULL
;
1175 /* Upload the user buffer if needed. */
1176 if (input
->user_buffer
) {
1177 unsigned buffer_offset
;
1179 si_upload_const_buffer(sctx
,
1180 (struct r600_resource
**)&buffer
, input
->user_buffer
,
1181 input
->buffer_size
, &buffer_offset
);
1183 /* Just unbind on failure. */
1184 si_set_constant_buffer(sctx
, buffers
, descriptors_idx
, slot
, NULL
);
1187 va
= r600_resource(buffer
)->gpu_address
+ buffer_offset
;
1189 pipe_resource_reference(&buffer
, input
->buffer
);
1190 va
= r600_resource(buffer
)->gpu_address
+ input
->buffer_offset
;
1191 /* Only track usage for non-user buffers. */
1192 r600_resource(buffer
)->bind_history
|= PIPE_BIND_CONSTANT_BUFFER
;
1195 /* Set the descriptor. */
1196 uint32_t *desc
= descs
->list
+ slot
*4;
1198 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1200 desc
[2] = input
->buffer_size
;
1201 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1202 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1203 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1204 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1205 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1206 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
1208 buffers
->buffers
[slot
] = buffer
;
1209 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
1210 (struct r600_resource
*)buffer
,
1211 buffers
->shader_usage_constbuf
,
1212 buffers
->priority_constbuf
, true);
1213 buffers
->enabled_mask
|= 1u << slot
;
1215 /* Clear the descriptor. */
1216 memset(descs
->list
+ slot
*4, 0, sizeof(uint32_t) * 4);
1217 buffers
->enabled_mask
&= ~(1u << slot
);
1220 descs
->dirty_mask
|= 1u << slot
;
1221 sctx
->descriptors_dirty
|= 1u << descriptors_idx
;
1224 void si_set_rw_buffer(struct si_context
*sctx
,
1225 uint slot
, const struct pipe_constant_buffer
*input
)
1227 si_set_constant_buffer(sctx
, &sctx
->rw_buffers
,
1228 SI_DESCS_RW_BUFFERS
, slot
, input
);
1231 static void si_pipe_set_constant_buffer(struct pipe_context
*ctx
,
1232 enum pipe_shader_type shader
, uint slot
,
1233 const struct pipe_constant_buffer
*input
)
1235 struct si_context
*sctx
= (struct si_context
*)ctx
;
1237 if (shader
>= SI_NUM_SHADERS
)
1240 slot
= si_get_constbuf_slot(slot
);
1241 si_set_constant_buffer(sctx
, &sctx
->const_and_shader_buffers
[shader
],
1242 si_const_and_shader_buffer_descriptors_idx(shader
),
1246 void si_get_pipe_constant_buffer(struct si_context
*sctx
, uint shader
,
1247 uint slot
, struct pipe_constant_buffer
*cbuf
)
1249 cbuf
->user_buffer
= NULL
;
1250 si_get_buffer_from_descriptors(
1251 &sctx
->const_and_shader_buffers
[shader
],
1252 si_const_and_shader_buffer_descriptors(sctx
, shader
),
1253 si_get_constbuf_slot(slot
),
1254 &cbuf
->buffer
, &cbuf
->buffer_offset
, &cbuf
->buffer_size
);
1257 /* SHADER BUFFERS */
1259 static void si_set_shader_buffers(struct pipe_context
*ctx
,
1260 enum pipe_shader_type shader
,
1261 unsigned start_slot
, unsigned count
,
1262 const struct pipe_shader_buffer
*sbuffers
)
1264 struct si_context
*sctx
= (struct si_context
*)ctx
;
1265 struct si_buffer_resources
*buffers
= &sctx
->const_and_shader_buffers
[shader
];
1266 struct si_descriptors
*descs
= si_const_and_shader_buffer_descriptors(sctx
, shader
);
1269 assert(start_slot
+ count
<= SI_NUM_SHADER_BUFFERS
);
1271 for (i
= 0; i
< count
; ++i
) {
1272 const struct pipe_shader_buffer
*sbuffer
= sbuffers
? &sbuffers
[i
] : NULL
;
1273 struct r600_resource
*buf
;
1274 unsigned slot
= si_get_shaderbuf_slot(start_slot
+ i
);
1275 uint32_t *desc
= descs
->list
+ slot
* 4;
1278 if (!sbuffer
|| !sbuffer
->buffer
) {
1279 pipe_resource_reference(&buffers
->buffers
[slot
], NULL
);
1280 memset(desc
, 0, sizeof(uint32_t) * 4);
1281 buffers
->enabled_mask
&= ~(1u << slot
);
1282 descs
->dirty_mask
|= 1u << slot
;
1283 sctx
->descriptors_dirty
|=
1284 1u << si_const_and_shader_buffer_descriptors_idx(shader
);
1288 buf
= (struct r600_resource
*)sbuffer
->buffer
;
1289 va
= buf
->gpu_address
+ sbuffer
->buffer_offset
;
1292 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1294 desc
[2] = sbuffer
->buffer_size
;
1295 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1296 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1297 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1298 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1299 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1300 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
1302 pipe_resource_reference(&buffers
->buffers
[slot
], &buf
->b
.b
);
1303 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
, buf
,
1304 buffers
->shader_usage
,
1305 buffers
->priority
, true);
1306 buf
->bind_history
|= PIPE_BIND_SHADER_BUFFER
;
1308 buffers
->enabled_mask
|= 1u << slot
;
1309 descs
->dirty_mask
|= 1u << slot
;
1310 sctx
->descriptors_dirty
|=
1311 1u << si_const_and_shader_buffer_descriptors_idx(shader
);
1313 util_range_add(&buf
->valid_buffer_range
, sbuffer
->buffer_offset
,
1314 sbuffer
->buffer_offset
+ sbuffer
->buffer_size
);
1318 void si_get_shader_buffers(struct si_context
*sctx
,
1319 enum pipe_shader_type shader
,
1320 uint start_slot
, uint count
,
1321 struct pipe_shader_buffer
*sbuf
)
1323 struct si_buffer_resources
*buffers
= &sctx
->const_and_shader_buffers
[shader
];
1324 struct si_descriptors
*descs
= si_const_and_shader_buffer_descriptors(sctx
, shader
);
1326 for (unsigned i
= 0; i
< count
; ++i
) {
1327 si_get_buffer_from_descriptors(
1329 si_get_shaderbuf_slot(start_slot
+ i
),
1330 &sbuf
[i
].buffer
, &sbuf
[i
].buffer_offset
,
1331 &sbuf
[i
].buffer_size
);
1337 void si_set_ring_buffer(struct pipe_context
*ctx
, uint slot
,
1338 struct pipe_resource
*buffer
,
1339 unsigned stride
, unsigned num_records
,
1340 bool add_tid
, bool swizzle
,
1341 unsigned element_size
, unsigned index_stride
, uint64_t offset
)
1343 struct si_context
*sctx
= (struct si_context
*)ctx
;
1344 struct si_buffer_resources
*buffers
= &sctx
->rw_buffers
;
1345 struct si_descriptors
*descs
= &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
1347 /* The stride field in the resource descriptor has 14 bits */
1348 assert(stride
< (1 << 14));
1350 assert(slot
< descs
->num_elements
);
1351 pipe_resource_reference(&buffers
->buffers
[slot
], NULL
);
1356 va
= r600_resource(buffer
)->gpu_address
+ offset
;
1358 switch (element_size
) {
1360 assert(!"Unsupported ring buffer element size");
1376 switch (index_stride
) {
1378 assert(!"Unsupported ring buffer index stride");
1394 if (sctx
->b
.chip_class
>= VI
&& stride
)
1395 num_records
*= stride
;
1397 /* Set the descriptor. */
1398 uint32_t *desc
= descs
->list
+ slot
*4;
1400 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1401 S_008F04_STRIDE(stride
) |
1402 S_008F04_SWIZZLE_ENABLE(swizzle
);
1403 desc
[2] = num_records
;
1404 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1405 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1406 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1407 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1408 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1409 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
) |
1410 S_008F0C_INDEX_STRIDE(index_stride
) |
1411 S_008F0C_ADD_TID_ENABLE(add_tid
);
1413 if (sctx
->b
.chip_class
>= GFX9
)
1414 assert(!swizzle
|| element_size
== 1); /* always 4 bytes on GFX9 */
1416 desc
[3] |= S_008F0C_ELEMENT_SIZE(element_size
);
1418 pipe_resource_reference(&buffers
->buffers
[slot
], buffer
);
1419 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
1420 (struct r600_resource
*)buffer
,
1421 buffers
->shader_usage
, buffers
->priority
);
1422 buffers
->enabled_mask
|= 1u << slot
;
1424 /* Clear the descriptor. */
1425 memset(descs
->list
+ slot
*4, 0, sizeof(uint32_t) * 4);
1426 buffers
->enabled_mask
&= ~(1u << slot
);
1429 descs
->dirty_mask
|= 1u << slot
;
1430 sctx
->descriptors_dirty
|= 1u << SI_DESCS_RW_BUFFERS
;
1433 /* STREAMOUT BUFFERS */
1435 static void si_set_streamout_targets(struct pipe_context
*ctx
,
1436 unsigned num_targets
,
1437 struct pipe_stream_output_target
**targets
,
1438 const unsigned *offsets
)
1440 struct si_context
*sctx
= (struct si_context
*)ctx
;
1441 struct si_buffer_resources
*buffers
= &sctx
->rw_buffers
;
1442 struct si_descriptors
*descs
= &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
1443 unsigned old_num_targets
= sctx
->b
.streamout
.num_targets
;
1446 /* We are going to unbind the buffers. Mark which caches need to be flushed. */
1447 if (sctx
->b
.streamout
.num_targets
&& sctx
->b
.streamout
.begin_emitted
) {
1448 /* Since streamout uses vector writes which go through TC L2
1449 * and most other clients can use TC L2 as well, we don't need
1452 * The only cases which requires flushing it is VGT DMA index
1453 * fetching (on <= CIK) and indirect draw data, which are rare
1454 * cases. Thus, flag the TC L2 dirtiness in the resource and
1455 * handle it at draw call time.
1457 for (i
= 0; i
< sctx
->b
.streamout
.num_targets
; i
++)
1458 if (sctx
->b
.streamout
.targets
[i
])
1459 r600_resource(sctx
->b
.streamout
.targets
[i
]->b
.buffer
)->TC_L2_dirty
= true;
1461 /* Invalidate the scalar cache in case a streamout buffer is
1462 * going to be used as a constant buffer.
1464 * Invalidate TC L1, because streamout bypasses it (done by
1465 * setting GLC=1 in the store instruction), but it can contain
1466 * outdated data of streamout buffers.
1468 * VS_PARTIAL_FLUSH is required if the buffers are going to be
1469 * used as an input immediately.
1471 sctx
->b
.flags
|= SI_CONTEXT_INV_SMEM_L1
|
1472 SI_CONTEXT_INV_VMEM_L1
|
1473 SI_CONTEXT_VS_PARTIAL_FLUSH
;
1476 /* All readers of the streamout targets need to be finished before we can
1477 * start writing to the targets.
1480 sctx
->b
.flags
|= SI_CONTEXT_PS_PARTIAL_FLUSH
|
1481 SI_CONTEXT_CS_PARTIAL_FLUSH
;
1483 /* Streamout buffers must be bound in 2 places:
1484 * 1) in VGT by setting the VGT_STRMOUT registers
1485 * 2) as shader resources
1488 /* Set the VGT regs. */
1489 r600_set_streamout_targets(ctx
, num_targets
, targets
, offsets
);
1491 /* Set the shader resources.*/
1492 for (i
= 0; i
< num_targets
; i
++) {
1493 bufidx
= SI_VS_STREAMOUT_BUF0
+ i
;
1496 struct pipe_resource
*buffer
= targets
[i
]->buffer
;
1497 uint64_t va
= r600_resource(buffer
)->gpu_address
;
1499 /* Set the descriptor.
1501 * On VI, the format must be non-INVALID, otherwise
1502 * the buffer will be considered not bound and store
1503 * instructions will be no-ops.
1505 uint32_t *desc
= descs
->list
+ bufidx
*4;
1507 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32);
1508 desc
[2] = 0xffffffff;
1509 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1510 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1511 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1512 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1513 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
1515 /* Set the resource. */
1516 pipe_resource_reference(&buffers
->buffers
[bufidx
],
1518 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
1519 (struct r600_resource
*)buffer
,
1520 buffers
->shader_usage
,
1521 RADEON_PRIO_SHADER_RW_BUFFER
,
1523 r600_resource(buffer
)->bind_history
|= PIPE_BIND_STREAM_OUTPUT
;
1525 buffers
->enabled_mask
|= 1u << bufidx
;
1527 /* Clear the descriptor and unset the resource. */
1528 memset(descs
->list
+ bufidx
*4, 0,
1529 sizeof(uint32_t) * 4);
1530 pipe_resource_reference(&buffers
->buffers
[bufidx
],
1532 buffers
->enabled_mask
&= ~(1u << bufidx
);
1534 descs
->dirty_mask
|= 1u << bufidx
;
1536 for (; i
< old_num_targets
; i
++) {
1537 bufidx
= SI_VS_STREAMOUT_BUF0
+ i
;
1538 /* Clear the descriptor and unset the resource. */
1539 memset(descs
->list
+ bufidx
*4, 0, sizeof(uint32_t) * 4);
1540 pipe_resource_reference(&buffers
->buffers
[bufidx
], NULL
);
1541 buffers
->enabled_mask
&= ~(1u << bufidx
);
1542 descs
->dirty_mask
|= 1u << bufidx
;
1545 sctx
->descriptors_dirty
|= 1u << SI_DESCS_RW_BUFFERS
;
1548 static void si_desc_reset_buffer_offset(struct pipe_context
*ctx
,
1549 uint32_t *desc
, uint64_t old_buf_va
,
1550 struct pipe_resource
*new_buf
)
1552 /* Retrieve the buffer offset from the descriptor. */
1553 uint64_t old_desc_va
=
1554 desc
[0] | ((uint64_t)G_008F04_BASE_ADDRESS_HI(desc
[1]) << 32);
1556 assert(old_buf_va
<= old_desc_va
);
1557 uint64_t offset_within_buffer
= old_desc_va
- old_buf_va
;
1559 /* Update the descriptor. */
1560 si_set_buf_desc_address(r600_resource(new_buf
), offset_within_buffer
,
1564 /* INTERNAL CONST BUFFERS */
1566 static void si_set_polygon_stipple(struct pipe_context
*ctx
,
1567 const struct pipe_poly_stipple
*state
)
1569 struct si_context
*sctx
= (struct si_context
*)ctx
;
1570 struct pipe_constant_buffer cb
= {};
1571 unsigned stipple
[32];
1574 for (i
= 0; i
< 32; i
++)
1575 stipple
[i
] = util_bitreverse(state
->stipple
[i
]);
1577 cb
.user_buffer
= stipple
;
1578 cb
.buffer_size
= sizeof(stipple
);
1580 si_set_rw_buffer(sctx
, SI_PS_CONST_POLY_STIPPLE
, &cb
);
1583 /* TEXTURE METADATA ENABLE/DISABLE */
1585 /* CMASK can be enabled (for fast clear) and disabled (for texture export)
1586 * while the texture is bound, possibly by a different context. In that case,
1587 * call this function to update compressed_colortex_masks.
1589 void si_update_compressed_colortex_masks(struct si_context
*sctx
)
1591 for (int i
= 0; i
< SI_NUM_SHADERS
; ++i
) {
1592 si_samplers_update_compressed_colortex_mask(&sctx
->samplers
[i
]);
1593 si_images_update_compressed_colortex_mask(&sctx
->images
[i
]);
1594 si_update_compressed_tex_shader_mask(sctx
, i
);
1598 /* BUFFER DISCARD/INVALIDATION */
1600 /** Reset descriptors of buffer resources after \p buf has been invalidated. */
1601 static void si_reset_buffer_resources(struct si_context
*sctx
,
1602 struct si_buffer_resources
*buffers
,
1603 unsigned descriptors_idx
,
1605 struct pipe_resource
*buf
,
1607 enum radeon_bo_usage usage
,
1608 enum radeon_bo_priority priority
)
1610 struct si_descriptors
*descs
= &sctx
->descriptors
[descriptors_idx
];
1611 unsigned mask
= buffers
->enabled_mask
& slot_mask
;
1614 unsigned i
= u_bit_scan(&mask
);
1615 if (buffers
->buffers
[i
] == buf
) {
1616 si_desc_reset_buffer_offset(&sctx
->b
.b
,
1619 descs
->dirty_mask
|= 1u << i
;
1620 sctx
->descriptors_dirty
|= 1u << descriptors_idx
;
1622 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
1623 (struct r600_resource
*)buf
,
1624 usage
, priority
, true);
1629 static void si_rebind_buffer(struct pipe_context
*ctx
, struct pipe_resource
*buf
,
1632 struct si_context
*sctx
= (struct si_context
*)ctx
;
1633 struct r600_resource
*rbuffer
= r600_resource(buf
);
1635 unsigned num_elems
= sctx
->vertex_elements
?
1636 sctx
->vertex_elements
->count
: 0;
1638 /* We changed the buffer, now we need to bind it where the old one
1639 * was bound. This consists of 2 things:
1640 * 1) Updating the resource descriptor and dirtying it.
1641 * 2) Adding a relocation to the CS, so that it's usable.
1644 /* Vertex buffers. */
1645 if (rbuffer
->bind_history
& PIPE_BIND_VERTEX_BUFFER
) {
1646 for (i
= 0; i
< num_elems
; i
++) {
1647 int vb
= sctx
->vertex_elements
->elements
[i
].vertex_buffer_index
;
1649 if (vb
>= ARRAY_SIZE(sctx
->vertex_buffer
))
1651 if (!sctx
->vertex_buffer
[vb
].buffer
.resource
)
1654 if (sctx
->vertex_buffer
[vb
].buffer
.resource
== buf
) {
1655 sctx
->vertex_buffers_dirty
= true;
1661 /* Streamout buffers. (other internal buffers can't be invalidated) */
1662 if (rbuffer
->bind_history
& PIPE_BIND_STREAM_OUTPUT
) {
1663 for (i
= SI_VS_STREAMOUT_BUF0
; i
<= SI_VS_STREAMOUT_BUF3
; i
++) {
1664 struct si_buffer_resources
*buffers
= &sctx
->rw_buffers
;
1665 struct si_descriptors
*descs
=
1666 &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
1668 if (buffers
->buffers
[i
] != buf
)
1671 si_desc_reset_buffer_offset(ctx
, descs
->list
+ i
*4,
1673 descs
->dirty_mask
|= 1u << i
;
1674 sctx
->descriptors_dirty
|= 1u << SI_DESCS_RW_BUFFERS
;
1676 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
1677 rbuffer
, buffers
->shader_usage
,
1678 RADEON_PRIO_SHADER_RW_BUFFER
,
1681 /* Update the streamout state. */
1682 if (sctx
->b
.streamout
.begin_emitted
)
1683 r600_emit_streamout_end(&sctx
->b
);
1684 sctx
->b
.streamout
.append_bitmask
=
1685 sctx
->b
.streamout
.enabled_mask
;
1686 r600_streamout_buffers_dirty(&sctx
->b
);
1690 /* Constant and shader buffers. */
1691 if (rbuffer
->bind_history
& PIPE_BIND_CONSTANT_BUFFER
) {
1692 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++)
1693 si_reset_buffer_resources(sctx
, &sctx
->const_and_shader_buffers
[shader
],
1694 si_const_and_shader_buffer_descriptors_idx(shader
),
1695 u_bit_consecutive(SI_NUM_SHADER_BUFFERS
, SI_NUM_CONST_BUFFERS
),
1697 sctx
->const_and_shader_buffers
[shader
].shader_usage_constbuf
,
1698 sctx
->const_and_shader_buffers
[shader
].priority_constbuf
);
1701 if (rbuffer
->bind_history
& PIPE_BIND_SHADER_BUFFER
) {
1702 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++)
1703 si_reset_buffer_resources(sctx
, &sctx
->const_and_shader_buffers
[shader
],
1704 si_const_and_shader_buffer_descriptors_idx(shader
),
1705 u_bit_consecutive(0, SI_NUM_SHADER_BUFFERS
),
1707 sctx
->const_and_shader_buffers
[shader
].shader_usage
,
1708 sctx
->const_and_shader_buffers
[shader
].priority
);
1711 if (rbuffer
->bind_history
& PIPE_BIND_SAMPLER_VIEW
) {
1712 /* Texture buffers - update bindings. */
1713 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++) {
1714 struct si_sampler_views
*views
= &sctx
->samplers
[shader
].views
;
1715 struct si_descriptors
*descs
=
1716 si_sampler_and_image_descriptors(sctx
, shader
);
1717 unsigned mask
= views
->enabled_mask
;
1720 unsigned i
= u_bit_scan(&mask
);
1721 if (views
->views
[i
]->texture
== buf
) {
1722 unsigned desc_slot
= si_get_sampler_slot(i
);
1724 si_desc_reset_buffer_offset(ctx
,
1728 descs
->dirty_mask
|= 1ull << desc_slot
;
1729 sctx
->descriptors_dirty
|=
1730 1u << si_sampler_and_image_descriptors_idx(shader
);
1732 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
1733 rbuffer
, RADEON_USAGE_READ
,
1734 RADEON_PRIO_SAMPLER_BUFFER
,
1742 if (rbuffer
->bind_history
& PIPE_BIND_SHADER_IMAGE
) {
1743 for (shader
= 0; shader
< SI_NUM_SHADERS
; ++shader
) {
1744 struct si_images_info
*images
= &sctx
->images
[shader
];
1745 struct si_descriptors
*descs
=
1746 si_sampler_and_image_descriptors(sctx
, shader
);
1747 unsigned mask
= images
->enabled_mask
;
1750 unsigned i
= u_bit_scan(&mask
);
1752 if (images
->views
[i
].resource
== buf
) {
1753 unsigned desc_slot
= si_get_image_slot(i
);
1755 if (images
->views
[i
].access
& PIPE_IMAGE_ACCESS_WRITE
)
1756 si_mark_image_range_valid(&images
->views
[i
]);
1758 si_desc_reset_buffer_offset(
1759 ctx
, descs
->list
+ desc_slot
* 8 + 4,
1761 /* two 8-byte images share one 16-byte slot */
1762 descs
->dirty_mask
|= 1u << (desc_slot
/ 2);
1763 sctx
->descriptors_dirty
|=
1764 1u << si_sampler_and_image_descriptors_idx(shader
);
1766 radeon_add_to_buffer_list_check_mem(
1767 &sctx
->b
, &sctx
->b
.gfx
, rbuffer
,
1768 RADEON_USAGE_READWRITE
,
1769 RADEON_PRIO_SAMPLER_BUFFER
, true);
1776 /* Reallocate a buffer a update all resource bindings where the buffer is
1779 * This is used to avoid CPU-GPU synchronizations, because it makes the buffer
1780 * idle by discarding its contents. Apps usually tell us when to do this using
1781 * map_buffer flags, for example.
1783 static void si_invalidate_buffer(struct pipe_context
*ctx
, struct pipe_resource
*buf
)
1785 struct si_context
*sctx
= (struct si_context
*)ctx
;
1786 struct r600_resource
*rbuffer
= r600_resource(buf
);
1787 uint64_t old_va
= rbuffer
->gpu_address
;
1789 /* Reallocate the buffer in the same pipe_resource. */
1790 r600_alloc_resource(&sctx
->screen
->b
, rbuffer
);
1792 si_rebind_buffer(ctx
, buf
, old_va
);
1795 /* Update mutable image descriptor fields of all bound textures. */
1796 void si_update_all_texture_descriptors(struct si_context
*sctx
)
1800 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++) {
1801 struct si_sampler_views
*samplers
= &sctx
->samplers
[shader
].views
;
1802 struct si_images_info
*images
= &sctx
->images
[shader
];
1806 mask
= images
->enabled_mask
;
1808 unsigned i
= u_bit_scan(&mask
);
1809 struct pipe_image_view
*view
= &images
->views
[i
];
1811 if (!view
->resource
||
1812 view
->resource
->target
== PIPE_BUFFER
)
1815 si_set_shader_image(sctx
, shader
, i
, view
, true);
1818 /* Sampler views. */
1819 mask
= samplers
->enabled_mask
;
1821 unsigned i
= u_bit_scan(&mask
);
1822 struct pipe_sampler_view
*view
= samplers
->views
[i
];
1826 view
->texture
->target
== PIPE_BUFFER
)
1829 si_set_sampler_view(sctx
, shader
, i
,
1830 samplers
->views
[i
], true);
1833 si_update_compressed_tex_shader_mask(sctx
, shader
);
1837 /* SHADER USER DATA */
1839 static void si_mark_shader_pointers_dirty(struct si_context
*sctx
,
1842 sctx
->shader_pointers_dirty
|=
1843 u_bit_consecutive(SI_DESCS_FIRST_SHADER
+ shader
* SI_NUM_SHADER_DESCS
,
1844 SI_NUM_SHADER_DESCS
);
1846 if (shader
== PIPE_SHADER_VERTEX
)
1847 sctx
->vertex_buffer_pointer_dirty
= sctx
->vertex_buffers
.buffer
!= NULL
;
1849 si_mark_atom_dirty(sctx
, &sctx
->shader_userdata
.atom
);
1852 static void si_shader_userdata_begin_new_cs(struct si_context
*sctx
)
1854 sctx
->shader_pointers_dirty
= u_bit_consecutive(0, SI_NUM_DESCS
);
1855 sctx
->vertex_buffer_pointer_dirty
= sctx
->vertex_buffers
.buffer
!= NULL
;
1856 si_mark_atom_dirty(sctx
, &sctx
->shader_userdata
.atom
);
1859 /* Set a base register address for user data constants in the given shader.
1860 * This assigns a mapping from PIPE_SHADER_* to SPI_SHADER_USER_DATA_*.
1862 static void si_set_user_data_base(struct si_context
*sctx
,
1863 unsigned shader
, uint32_t new_base
)
1865 uint32_t *base
= &sctx
->shader_userdata
.sh_base
[shader
];
1867 if (*base
!= new_base
) {
1871 si_mark_shader_pointers_dirty(sctx
, shader
);
1873 if (shader
== PIPE_SHADER_VERTEX
)
1874 sctx
->last_vs_state
= ~0;
1879 /* This must be called when these shaders are changed from non-NULL to NULL
1882 * - tessellation control shader
1883 * - tessellation evaluation shader
1885 void si_shader_change_notify(struct si_context
*sctx
)
1887 /* VS can be bound as VS, ES, or LS. */
1888 if (sctx
->tes_shader
.cso
) {
1889 if (sctx
->b
.chip_class
>= GFX9
) {
1890 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
1891 R_00B430_SPI_SHADER_USER_DATA_LS_0
);
1893 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
1894 R_00B530_SPI_SHADER_USER_DATA_LS_0
);
1896 } else if (sctx
->gs_shader
.cso
) {
1897 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
1898 R_00B330_SPI_SHADER_USER_DATA_ES_0
);
1900 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
1901 R_00B130_SPI_SHADER_USER_DATA_VS_0
);
1904 /* TES can be bound as ES, VS, or not bound. */
1905 if (sctx
->tes_shader
.cso
) {
1906 if (sctx
->gs_shader
.cso
)
1907 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_EVAL
,
1908 R_00B330_SPI_SHADER_USER_DATA_ES_0
);
1910 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_EVAL
,
1911 R_00B130_SPI_SHADER_USER_DATA_VS_0
);
1913 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_EVAL
, 0);
1917 static void si_emit_shader_pointer(struct si_context
*sctx
,
1918 struct si_descriptors
*desc
,
1921 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
1925 return; /* the pointer is not used by current shaders */
1927 va
= desc
->buffer
->gpu_address
+
1928 desc
->buffer_offset
;
1930 radeon_emit(cs
, PKT3(PKT3_SET_SH_REG
, 2, 0));
1931 radeon_emit(cs
, (sh_base
+ desc
->shader_userdata_offset
- SI_SH_REG_OFFSET
) >> 2);
1932 radeon_emit(cs
, va
);
1933 radeon_emit(cs
, va
>> 32);
1936 void si_emit_graphics_shader_userdata(struct si_context
*sctx
,
1937 struct r600_atom
*atom
)
1940 uint32_t *sh_base
= sctx
->shader_userdata
.sh_base
;
1941 struct si_descriptors
*descs
;
1943 descs
= &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
1945 if (sctx
->shader_pointers_dirty
& (1 << SI_DESCS_RW_BUFFERS
)) {
1946 si_emit_shader_pointer(sctx
, descs
,
1947 R_00B030_SPI_SHADER_USER_DATA_PS_0
);
1948 si_emit_shader_pointer(sctx
, descs
,
1949 R_00B130_SPI_SHADER_USER_DATA_VS_0
);
1951 if (sctx
->b
.chip_class
>= GFX9
) {
1952 /* GFX9 merged LS-HS and ES-GS.
1953 * Set RW_BUFFERS in the special registers, so that
1954 * it's preloaded into s[0:1] instead of s[8:9].
1956 si_emit_shader_pointer(sctx
, descs
,
1957 R_00B208_SPI_SHADER_USER_DATA_ADDR_LO_GS
);
1958 si_emit_shader_pointer(sctx
, descs
,
1959 R_00B408_SPI_SHADER_USER_DATA_ADDR_LO_HS
);
1961 si_emit_shader_pointer(sctx
, descs
,
1962 R_00B230_SPI_SHADER_USER_DATA_GS_0
);
1963 si_emit_shader_pointer(sctx
, descs
,
1964 R_00B330_SPI_SHADER_USER_DATA_ES_0
);
1965 si_emit_shader_pointer(sctx
, descs
,
1966 R_00B430_SPI_SHADER_USER_DATA_HS_0
);
1970 mask
= sctx
->shader_pointers_dirty
&
1971 u_bit_consecutive(SI_DESCS_FIRST_SHADER
,
1972 SI_DESCS_FIRST_COMPUTE
- SI_DESCS_FIRST_SHADER
);
1975 unsigned i
= u_bit_scan(&mask
);
1976 unsigned shader
= (i
- SI_DESCS_FIRST_SHADER
) / SI_NUM_SHADER_DESCS
;
1977 unsigned base
= sh_base
[shader
];
1980 si_emit_shader_pointer(sctx
, descs
+ i
, base
);
1982 sctx
->shader_pointers_dirty
&=
1983 ~u_bit_consecutive(SI_DESCS_RW_BUFFERS
, SI_DESCS_FIRST_COMPUTE
);
1985 if (sctx
->vertex_buffer_pointer_dirty
) {
1986 si_emit_shader_pointer(sctx
, &sctx
->vertex_buffers
,
1987 sh_base
[PIPE_SHADER_VERTEX
]);
1988 sctx
->vertex_buffer_pointer_dirty
= false;
1992 void si_emit_compute_shader_userdata(struct si_context
*sctx
)
1994 unsigned base
= R_00B900_COMPUTE_USER_DATA_0
;
1995 struct si_descriptors
*descs
= sctx
->descriptors
;
1996 unsigned compute_mask
=
1997 u_bit_consecutive(SI_DESCS_FIRST_COMPUTE
, SI_NUM_SHADER_DESCS
);
1998 unsigned mask
= sctx
->shader_pointers_dirty
& compute_mask
;
2001 unsigned i
= u_bit_scan(&mask
);
2003 si_emit_shader_pointer(sctx
, descs
+ i
, base
);
2005 sctx
->shader_pointers_dirty
&= ~compute_mask
;
2008 /* INIT/DEINIT/UPLOAD */
2010 /* GFX9 has only 4KB of CE, while previous chips had 32KB. In order
2011 * to make CE RAM as useful as possible, this defines limits
2012 * for the number slots that can be in CE RAM on GFX9. If a shader
2013 * is using more, descriptors will be uploaded to memory directly and
2016 * These numbers are based on shader-db.
2018 static unsigned gfx9_max_ce_samplers
[SI_NUM_SHADERS
] = {
2019 [PIPE_SHADER_VERTEX
] = 0,
2020 [PIPE_SHADER_TESS_CTRL
] = 0,
2021 [PIPE_SHADER_TESS_EVAL
] = 1,
2022 [PIPE_SHADER_GEOMETRY
] = 0,
2023 [PIPE_SHADER_FRAGMENT
] = 24,
2024 [PIPE_SHADER_COMPUTE
] = 16,
2026 static unsigned gfx9_max_ce_images
[SI_NUM_SHADERS
] = {
2027 /* these must be even due to slot alignment */
2028 [PIPE_SHADER_VERTEX
] = 0,
2029 [PIPE_SHADER_TESS_CTRL
] = 0,
2030 [PIPE_SHADER_TESS_EVAL
] = 0,
2031 [PIPE_SHADER_GEOMETRY
] = 0,
2032 [PIPE_SHADER_FRAGMENT
] = 2,
2033 [PIPE_SHADER_COMPUTE
] = 8,
2035 static unsigned gfx9_max_ce_const_buffers
[SI_NUM_SHADERS
] = {
2036 [PIPE_SHADER_VERTEX
] = 9,
2037 [PIPE_SHADER_TESS_CTRL
] = 3,
2038 [PIPE_SHADER_TESS_EVAL
] = 5,
2039 [PIPE_SHADER_GEOMETRY
] = 0,
2040 [PIPE_SHADER_FRAGMENT
] = 8,
2041 [PIPE_SHADER_COMPUTE
] = 6,
2043 static unsigned gfx9_max_ce_shader_buffers
[SI_NUM_SHADERS
] = {
2044 [PIPE_SHADER_VERTEX
] = 0,
2045 [PIPE_SHADER_TESS_CTRL
] = 0,
2046 [PIPE_SHADER_TESS_EVAL
] = 0,
2047 [PIPE_SHADER_GEOMETRY
] = 0,
2048 [PIPE_SHADER_FRAGMENT
] = 12,
2049 [PIPE_SHADER_COMPUTE
] = 13,
2052 void si_init_all_descriptors(struct si_context
*sctx
)
2055 unsigned ce_offset
= 0;
2057 STATIC_ASSERT(GFX9_SGPR_TCS_CONST_AND_SHADER_BUFFERS
% 2 == 0);
2058 STATIC_ASSERT(GFX9_SGPR_GS_CONST_AND_SHADER_BUFFERS
% 2 == 0);
2060 for (i
= 0; i
< SI_NUM_SHADERS
; i
++) {
2061 bool gfx9_tcs
= false;
2062 bool gfx9_gs
= false;
2063 unsigned num_sampler_slots
= SI_NUM_IMAGES
/ 2 + SI_NUM_SAMPLERS
;
2064 unsigned num_buffer_slots
= SI_NUM_SHADER_BUFFERS
+ SI_NUM_CONST_BUFFERS
;
2066 unsigned first_sampler_ce_slot
= 0;
2067 unsigned num_sampler_ce_slots
= num_sampler_slots
;
2069 unsigned first_buffer_ce_slot
= 0;
2070 unsigned num_buffer_ce_slots
= num_buffer_slots
;
2072 /* Adjust CE slot ranges based on GFX9 CE RAM limits. */
2073 if (sctx
->b
.chip_class
>= GFX9
) {
2074 gfx9_tcs
= i
== PIPE_SHADER_TESS_CTRL
;
2075 gfx9_gs
= i
== PIPE_SHADER_GEOMETRY
;
2077 first_sampler_ce_slot
=
2078 si_get_image_slot(gfx9_max_ce_images
[i
] - 1) / 2;
2079 num_sampler_ce_slots
= gfx9_max_ce_images
[i
] / 2 +
2080 gfx9_max_ce_samplers
[i
];
2082 first_buffer_ce_slot
=
2083 si_get_shaderbuf_slot(gfx9_max_ce_shader_buffers
[i
] - 1);
2084 num_buffer_ce_slots
= gfx9_max_ce_shader_buffers
[i
] +
2085 gfx9_max_ce_const_buffers
[i
];
2088 si_init_buffer_resources(sctx
, &sctx
->const_and_shader_buffers
[i
],
2089 si_const_and_shader_buffer_descriptors(sctx
, i
),
2091 first_buffer_ce_slot
, num_buffer_ce_slots
,
2092 gfx9_tcs
? GFX9_SGPR_TCS_CONST_AND_SHADER_BUFFERS
:
2093 gfx9_gs
? GFX9_SGPR_GS_CONST_AND_SHADER_BUFFERS
:
2094 SI_SGPR_CONST_AND_SHADER_BUFFERS
,
2095 RADEON_USAGE_READWRITE
,
2097 RADEON_PRIO_SHADER_RW_BUFFER
,
2098 RADEON_PRIO_CONST_BUFFER
,
2101 struct si_descriptors
*desc
= si_sampler_and_image_descriptors(sctx
, i
);
2102 si_init_descriptors(sctx
, desc
,
2103 gfx9_tcs
? GFX9_SGPR_TCS_SAMPLERS_AND_IMAGES
:
2104 gfx9_gs
? GFX9_SGPR_GS_SAMPLERS_AND_IMAGES
:
2105 SI_SGPR_SAMPLERS_AND_IMAGES
,
2106 16, num_sampler_slots
,
2107 first_sampler_ce_slot
, num_sampler_ce_slots
,
2111 for (j
= 0; j
< SI_NUM_IMAGES
; j
++)
2112 memcpy(desc
->list
+ j
* 8, null_image_descriptor
, 8 * 4);
2113 for (; j
< SI_NUM_IMAGES
+ SI_NUM_SAMPLERS
* 2; j
++)
2114 memcpy(desc
->list
+ j
* 8, null_texture_descriptor
, 8 * 4);
2117 si_init_buffer_resources(sctx
, &sctx
->rw_buffers
,
2118 &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
],
2119 SI_NUM_RW_BUFFERS
, 0, SI_NUM_RW_BUFFERS
,
2121 /* The second set of usage/priority is used by
2122 * const buffers in RW buffer slots. */
2123 RADEON_USAGE_READWRITE
, RADEON_USAGE_READ
,
2124 RADEON_PRIO_SHADER_RINGS
, RADEON_PRIO_CONST_BUFFER
,
2126 sctx
->descriptors
[SI_DESCS_RW_BUFFERS
].num_active_slots
= SI_NUM_RW_BUFFERS
;
2128 si_init_descriptors(sctx
, &sctx
->vertex_buffers
, SI_SGPR_VERTEX_BUFFERS
,
2129 4, SI_NUM_VERTEX_BUFFERS
, 0, 0, NULL
);
2131 sctx
->descriptors_dirty
= u_bit_consecutive(0, SI_NUM_DESCS
);
2132 sctx
->total_ce_ram_allocated
= ce_offset
;
2134 if (sctx
->b
.chip_class
>= GFX9
)
2135 assert(ce_offset
<= 4096);
2137 assert(ce_offset
<= 32768);
2139 /* Set pipe_context functions. */
2140 sctx
->b
.b
.bind_sampler_states
= si_bind_sampler_states
;
2141 sctx
->b
.b
.set_shader_images
= si_set_shader_images
;
2142 sctx
->b
.b
.set_constant_buffer
= si_pipe_set_constant_buffer
;
2143 sctx
->b
.b
.set_polygon_stipple
= si_set_polygon_stipple
;
2144 sctx
->b
.b
.set_shader_buffers
= si_set_shader_buffers
;
2145 sctx
->b
.b
.set_sampler_views
= si_set_sampler_views
;
2146 sctx
->b
.b
.set_stream_output_targets
= si_set_streamout_targets
;
2147 sctx
->b
.invalidate_buffer
= si_invalidate_buffer
;
2148 sctx
->b
.rebind_buffer
= si_rebind_buffer
;
2150 /* Shader user data. */
2151 si_init_atom(sctx
, &sctx
->shader_userdata
.atom
, &sctx
->atoms
.s
.shader_userdata
,
2152 si_emit_graphics_shader_userdata
);
2154 /* Set default and immutable mappings. */
2155 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
, R_00B130_SPI_SHADER_USER_DATA_VS_0
);
2157 if (sctx
->b
.chip_class
>= GFX9
) {
2158 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_CTRL
,
2159 R_00B430_SPI_SHADER_USER_DATA_LS_0
);
2160 si_set_user_data_base(sctx
, PIPE_SHADER_GEOMETRY
,
2161 R_00B330_SPI_SHADER_USER_DATA_ES_0
);
2163 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_CTRL
,
2164 R_00B430_SPI_SHADER_USER_DATA_HS_0
);
2165 si_set_user_data_base(sctx
, PIPE_SHADER_GEOMETRY
,
2166 R_00B230_SPI_SHADER_USER_DATA_GS_0
);
2168 si_set_user_data_base(sctx
, PIPE_SHADER_FRAGMENT
, R_00B030_SPI_SHADER_USER_DATA_PS_0
);
2171 bool si_upload_graphics_shader_descriptors(struct si_context
*sctx
)
2173 const unsigned mask
= u_bit_consecutive(0, SI_DESCS_FIRST_COMPUTE
);
2174 unsigned dirty
= sctx
->descriptors_dirty
& mask
;
2176 /* Assume nothing will go wrong: */
2177 sctx
->shader_pointers_dirty
|= dirty
;
2180 unsigned i
= u_bit_scan(&dirty
);
2182 if (!si_upload_descriptors(sctx
, &sctx
->descriptors
[i
],
2183 &sctx
->shader_userdata
.atom
))
2187 sctx
->descriptors_dirty
&= ~mask
;
2191 bool si_upload_compute_shader_descriptors(struct si_context
*sctx
)
2193 /* Does not update rw_buffers as that is not needed for compute shaders
2194 * and the input buffer is using the same SGPR's anyway.
2196 const unsigned mask
= u_bit_consecutive(SI_DESCS_FIRST_COMPUTE
,
2197 SI_NUM_DESCS
- SI_DESCS_FIRST_COMPUTE
);
2198 unsigned dirty
= sctx
->descriptors_dirty
& mask
;
2200 /* Assume nothing will go wrong: */
2201 sctx
->shader_pointers_dirty
|= dirty
;
2204 unsigned i
= u_bit_scan(&dirty
);
2206 if (!si_upload_descriptors(sctx
, &sctx
->descriptors
[i
], NULL
))
2210 sctx
->descriptors_dirty
&= ~mask
;
2215 void si_release_all_descriptors(struct si_context
*sctx
)
2219 for (i
= 0; i
< SI_NUM_SHADERS
; i
++) {
2220 si_release_buffer_resources(&sctx
->const_and_shader_buffers
[i
],
2221 si_const_and_shader_buffer_descriptors(sctx
, i
));
2222 si_release_sampler_views(&sctx
->samplers
[i
].views
);
2223 si_release_image_views(&sctx
->images
[i
]);
2225 si_release_buffer_resources(&sctx
->rw_buffers
,
2226 &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
]);
2228 for (i
= 0; i
< SI_NUM_DESCS
; ++i
)
2229 si_release_descriptors(&sctx
->descriptors
[i
]);
2230 si_release_descriptors(&sctx
->vertex_buffers
);
2233 void si_all_descriptors_begin_new_cs(struct si_context
*sctx
)
2237 for (i
= 0; i
< SI_NUM_SHADERS
; i
++) {
2238 si_buffer_resources_begin_new_cs(sctx
, &sctx
->const_and_shader_buffers
[i
]);
2239 si_sampler_views_begin_new_cs(sctx
, &sctx
->samplers
[i
].views
);
2240 si_image_views_begin_new_cs(sctx
, &sctx
->images
[i
]);
2242 si_buffer_resources_begin_new_cs(sctx
, &sctx
->rw_buffers
);
2243 si_vertex_buffers_begin_new_cs(sctx
);
2245 for (i
= 0; i
< SI_NUM_DESCS
; ++i
)
2246 si_descriptors_begin_new_cs(sctx
, &sctx
->descriptors
[i
]);
2248 si_shader_userdata_begin_new_cs(sctx
);
2251 void si_set_active_descriptors(struct si_context
*sctx
, unsigned desc_idx
,
2252 uint64_t new_active_mask
)
2254 struct si_descriptors
*desc
= &sctx
->descriptors
[desc_idx
];
2256 /* Ignore no-op updates and updates that disable all slots. */
2257 if (!new_active_mask
||
2258 new_active_mask
== u_bit_consecutive64(desc
->first_active_slot
,
2259 desc
->num_active_slots
))
2263 u_bit_scan_consecutive_range64(&new_active_mask
, &first
, &count
);
2264 assert(new_active_mask
== 0);
2266 /* Upload/dump descriptors if slots are being enabled. */
2267 if (first
< desc
->first_active_slot
||
2268 first
+ count
> desc
->first_active_slot
+ desc
->num_active_slots
)
2269 sctx
->descriptors_dirty
|= 1u << desc_idx
;
2271 /* Enable or disable CE for this descriptor array. */
2272 bool used_ce
= desc
->uses_ce
;
2273 desc
->uses_ce
= desc
->first_ce_slot
<= first
&&
2274 desc
->first_ce_slot
+ desc
->num_ce_slots
>= first
+ count
;
2276 if (desc
->uses_ce
!= used_ce
) {
2277 /* Upload or dump descriptors if we're disabling or enabling CE,
2279 sctx
->descriptors_dirty
|= 1u << desc_idx
;
2281 /* If we're enabling CE, re-upload all descriptors to CE RAM.
2282 * When CE was disabled, uploads to CE RAM stopped.
2284 if (desc
->uses_ce
) {
2286 u_bit_consecutive64(desc
->first_ce_slot
,
2287 desc
->num_ce_slots
);
2291 desc
->first_active_slot
= first
;
2292 desc
->num_active_slots
= count
;
2295 void si_set_active_descriptors_for_shader(struct si_context
*sctx
,
2296 struct si_shader_selector
*sel
)
2301 si_set_active_descriptors(sctx
,
2302 si_const_and_shader_buffer_descriptors_idx(sel
->type
),
2303 sel
->active_const_and_shader_buffers
);
2304 si_set_active_descriptors(sctx
,
2305 si_sampler_and_image_descriptors_idx(sel
->type
),
2306 sel
->active_samplers_and_images
);