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"
60 #include "si_shader.h"
63 #include "util/u_format.h"
64 #include "util/u_math.h"
65 #include "util/u_memory.h"
66 #include "util/u_suballoc.h"
67 #include "util/u_upload_mgr.h"
70 /* NULL image and buffer descriptor for textures (alpha = 1) and images
73 * For images, all fields must be zero except for the swizzle, which
74 * supports arbitrary combinations of 0s and 1s. The texture type must be
75 * any valid type (e.g. 1D). If the texture type isn't set, the hw hangs.
77 * For buffers, all fields must be zero. If they are not, the hw hangs.
79 * This is the only reason why the buffer descriptor must be in words [4:7].
81 static uint32_t null_texture_descriptor
[8] = {
85 S_008F1C_DST_SEL_W(V_008F1C_SQ_SEL_1
) |
86 S_008F1C_TYPE(V_008F1C_SQ_RSRC_IMG_1D
)
87 /* the rest must contain zeros, which is also used by the buffer
91 static uint32_t null_image_descriptor
[8] = {
95 S_008F1C_TYPE(V_008F1C_SQ_RSRC_IMG_1D
)
96 /* the rest must contain zeros, which is also used by the buffer
100 static void si_init_descriptors(struct si_descriptors
*desc
,
101 unsigned shader_userdata_index
,
102 unsigned element_dw_size
,
103 unsigned num_elements
,
104 const uint32_t *null_descriptor
,
109 assert(num_elements
<= sizeof(desc
->dirty_mask
)*8);
111 desc
->list
= CALLOC(num_elements
, element_dw_size
* 4);
112 desc
->element_dw_size
= element_dw_size
;
113 desc
->num_elements
= num_elements
;
114 desc
->dirty_mask
= num_elements
== 32 ? ~0u : (1u << num_elements
) - 1;
115 desc
->shader_userdata_offset
= shader_userdata_index
* 4;
118 desc
->ce_offset
= *ce_offset
;
120 /* make sure that ce_offset stays 32 byte aligned */
121 *ce_offset
+= align(element_dw_size
* num_elements
* 4, 32);
124 /* Initialize the array to NULL descriptors if the element size is 8. */
125 if (null_descriptor
) {
126 assert(element_dw_size
% 8 == 0);
127 for (i
= 0; i
< num_elements
* element_dw_size
/ 8; i
++)
128 memcpy(desc
->list
+ i
* 8, null_descriptor
,
133 static void si_release_descriptors(struct si_descriptors
*desc
)
135 r600_resource_reference(&desc
->buffer
, NULL
);
139 static bool si_ce_upload(struct si_context
*sctx
, unsigned ce_offset
, unsigned size
,
140 unsigned *out_offset
, struct r600_resource
**out_buf
) {
143 u_suballocator_alloc(sctx
->ce_suballocator
, size
, 64, out_offset
,
144 (struct pipe_resource
**)out_buf
);
148 va
= (*out_buf
)->gpu_address
+ *out_offset
;
150 radeon_emit(sctx
->ce_ib
, PKT3(PKT3_DUMP_CONST_RAM
, 3, 0));
151 radeon_emit(sctx
->ce_ib
, ce_offset
);
152 radeon_emit(sctx
->ce_ib
, size
/ 4);
153 radeon_emit(sctx
->ce_ib
, va
);
154 radeon_emit(sctx
->ce_ib
, va
>> 32);
156 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, *out_buf
,
157 RADEON_USAGE_READWRITE
, RADEON_PRIO_DESCRIPTORS
);
159 sctx
->ce_need_synchronization
= true;
163 static void si_ce_reinitialize_descriptors(struct si_context
*sctx
,
164 struct si_descriptors
*desc
)
167 struct r600_resource
*buffer
= (struct r600_resource
*)desc
->buffer
;
168 unsigned list_size
= desc
->num_elements
* desc
->element_dw_size
* 4;
169 uint64_t va
= buffer
->gpu_address
+ desc
->buffer_offset
;
170 struct radeon_winsys_cs
*ib
= sctx
->ce_preamble_ib
;
175 list_size
= align(list_size
, 32);
177 radeon_emit(ib
, PKT3(PKT3_LOAD_CONST_RAM
, 3, 0));
179 radeon_emit(ib
, va
>> 32);
180 radeon_emit(ib
, list_size
/ 4);
181 radeon_emit(ib
, desc
->ce_offset
);
183 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, desc
->buffer
,
184 RADEON_USAGE_READ
, RADEON_PRIO_DESCRIPTORS
);
186 desc
->ce_ram_dirty
= false;
189 void si_ce_reinitialize_all_descriptors(struct si_context
*sctx
)
193 for (i
= 0; i
< SI_NUM_DESCS
; ++i
)
194 si_ce_reinitialize_descriptors(sctx
, &sctx
->descriptors
[i
]);
197 void si_ce_enable_loads(struct radeon_winsys_cs
*ib
)
199 radeon_emit(ib
, PKT3(PKT3_CONTEXT_CONTROL
, 1, 0));
200 radeon_emit(ib
, CONTEXT_CONTROL_LOAD_ENABLE(1) |
201 CONTEXT_CONTROL_LOAD_CE_RAM(1));
202 radeon_emit(ib
, CONTEXT_CONTROL_SHADOW_ENABLE(1));
205 static bool si_upload_descriptors(struct si_context
*sctx
,
206 struct si_descriptors
*desc
,
207 struct r600_atom
* atom
)
209 unsigned list_size
= desc
->num_elements
* desc
->element_dw_size
* 4;
211 if (!desc
->dirty_mask
)
215 uint32_t const* list
= (uint32_t const*)desc
->list
;
217 if (desc
->ce_ram_dirty
)
218 si_ce_reinitialize_descriptors(sctx
, desc
);
220 while(desc
->dirty_mask
) {
222 u_bit_scan_consecutive_range(&desc
->dirty_mask
, &begin
,
225 begin
*= desc
->element_dw_size
;
226 count
*= desc
->element_dw_size
;
228 radeon_emit(sctx
->ce_ib
,
229 PKT3(PKT3_WRITE_CONST_RAM
, count
, 0));
230 radeon_emit(sctx
->ce_ib
, desc
->ce_offset
+ begin
* 4);
231 radeon_emit_array(sctx
->ce_ib
, list
+ begin
, count
);
234 if (!si_ce_upload(sctx
, desc
->ce_offset
, list_size
,
235 &desc
->buffer_offset
, &desc
->buffer
))
240 u_upload_alloc(sctx
->b
.uploader
, 0, list_size
, 256,
241 &desc
->buffer_offset
,
242 (struct pipe_resource
**)&desc
->buffer
, &ptr
);
244 return false; /* skip the draw call */
246 util_memcpy_cpu_to_le32(ptr
, desc
->list
, list_size
);
248 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, desc
->buffer
,
249 RADEON_USAGE_READ
, RADEON_PRIO_DESCRIPTORS
);
251 desc
->pointer_dirty
= true;
252 desc
->dirty_mask
= 0;
255 si_mark_atom_dirty(sctx
, atom
);
261 si_descriptors_begin_new_cs(struct si_context
*sctx
, struct si_descriptors
*desc
)
263 desc
->ce_ram_dirty
= true;
268 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, desc
->buffer
,
269 RADEON_USAGE_READ
, RADEON_PRIO_DESCRIPTORS
);
275 si_sampler_descriptors_idx(unsigned shader
)
277 return SI_DESCS_FIRST_SHADER
+ shader
* SI_NUM_SHADER_DESCS
+
278 SI_SHADER_DESCS_SAMPLERS
;
281 static struct si_descriptors
*
282 si_sampler_descriptors(struct si_context
*sctx
, unsigned shader
)
284 return &sctx
->descriptors
[si_sampler_descriptors_idx(shader
)];
287 static void si_release_sampler_views(struct si_sampler_views
*views
)
291 for (i
= 0; i
< ARRAY_SIZE(views
->views
); i
++) {
292 pipe_sampler_view_reference(&views
->views
[i
], NULL
);
296 static void si_sampler_view_add_buffer(struct si_context
*sctx
,
297 struct pipe_resource
*resource
,
298 enum radeon_bo_usage usage
,
299 bool is_stencil_sampler
)
301 struct r600_resource
*rres
;
306 if (resource
->target
!= PIPE_BUFFER
) {
307 struct r600_texture
*tex
= (struct r600_texture
*)resource
;
309 if (tex
->is_depth
&& !r600_can_sample_zs(tex
, is_stencil_sampler
))
310 resource
= &tex
->flushed_depth_texture
->resource
.b
.b
;
313 rres
= (struct r600_resource
*)resource
;
314 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, rres
, usage
,
315 r600_get_sampler_view_priority(rres
));
317 if (resource
->target
!= PIPE_BUFFER
) {
318 struct r600_texture
*rtex
= (struct r600_texture
*)resource
;
320 if (rtex
->dcc_separate_buffer
)
321 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
322 rtex
->dcc_separate_buffer
, usage
,
327 static void si_sampler_views_begin_new_cs(struct si_context
*sctx
,
328 struct si_sampler_views
*views
)
330 unsigned mask
= views
->enabled_mask
;
332 /* Add buffers to the CS. */
334 int i
= u_bit_scan(&mask
);
335 struct si_sampler_view
*sview
= (struct si_sampler_view
*)views
->views
[i
];
337 si_sampler_view_add_buffer(sctx
, sview
->base
.texture
,
339 sview
->is_stencil_sampler
);
343 /* Set texture descriptor fields that can be changed by reallocations.
346 * \param base_level_info information of the level of BASE_ADDRESS
347 * \param base_level the level of BASE_ADDRESS
348 * \param first_level pipe_sampler_view.u.tex.first_level
349 * \param block_width util_format_get_blockwidth()
350 * \param is_stencil select between separate Z & Stencil
351 * \param state descriptor to update
353 void si_set_mutable_tex_desc_fields(struct r600_texture
*tex
,
354 const struct radeon_surf_level
*base_level_info
,
355 unsigned base_level
, unsigned first_level
,
356 unsigned block_width
, bool is_stencil
,
360 unsigned pitch
= base_level_info
->nblk_x
* block_width
;
362 if (tex
->is_depth
&& !r600_can_sample_zs(tex
, is_stencil
)) {
363 tex
= tex
->flushed_depth_texture
;
367 va
= tex
->resource
.gpu_address
+ base_level_info
->offset
;
369 state
[1] &= C_008F14_BASE_ADDRESS_HI
;
370 state
[3] &= C_008F1C_TILING_INDEX
;
371 state
[4] &= C_008F20_PITCH
;
372 state
[6] &= C_008F28_COMPRESSION_EN
;
375 state
[1] |= S_008F14_BASE_ADDRESS_HI(va
>> 40);
376 state
[3] |= S_008F1C_TILING_INDEX(si_tile_mode_index(tex
, base_level
,
378 state
[4] |= S_008F20_PITCH(pitch
- 1);
380 if (tex
->dcc_offset
&& tex
->surface
.level
[first_level
].dcc_enabled
) {
381 state
[6] |= S_008F28_COMPRESSION_EN(1);
382 state
[7] = ((!tex
->dcc_separate_buffer
? tex
->resource
.gpu_address
: 0) +
384 base_level_info
->dcc_offset
) >> 8;
388 static void si_set_sampler_view(struct si_context
*sctx
,
390 unsigned slot
, struct pipe_sampler_view
*view
,
391 bool disallow_early_out
)
393 struct si_sampler_views
*views
= &sctx
->samplers
[shader
].views
;
394 struct si_sampler_view
*rview
= (struct si_sampler_view
*)view
;
395 struct si_descriptors
*descs
= si_sampler_descriptors(sctx
, shader
);
397 if (views
->views
[slot
] == view
&& !disallow_early_out
)
401 struct r600_texture
*rtex
= (struct r600_texture
*)view
->texture
;
402 uint32_t *desc
= descs
->list
+ slot
* 16;
404 si_sampler_view_add_buffer(sctx
, view
->texture
,
406 rview
->is_stencil_sampler
);
408 pipe_sampler_view_reference(&views
->views
[slot
], view
);
409 memcpy(desc
, rview
->state
, 8*4);
411 if (view
->texture
&& view
->texture
->target
!= PIPE_BUFFER
) {
412 bool is_separate_stencil
=
413 rtex
->db_compatible
&&
414 rview
->is_stencil_sampler
;
416 si_set_mutable_tex_desc_fields(rtex
,
417 rview
->base_level_info
,
419 rview
->base
.u
.tex
.first_level
,
425 if (view
->texture
&& view
->texture
->target
!= PIPE_BUFFER
&&
428 rview
->fmask_state
, 8*4);
430 /* Disable FMASK and bind sampler state in [12:15]. */
432 null_texture_descriptor
, 4*4);
434 if (views
->sampler_states
[slot
])
436 views
->sampler_states
[slot
], 4*4);
439 views
->enabled_mask
|= 1u << slot
;
441 pipe_sampler_view_reference(&views
->views
[slot
], NULL
);
442 memcpy(descs
->list
+ slot
*16, null_texture_descriptor
, 8*4);
443 /* Only clear the lower dwords of FMASK. */
444 memcpy(descs
->list
+ slot
*16 + 8, null_texture_descriptor
, 4*4);
445 views
->enabled_mask
&= ~(1u << slot
);
448 descs
->dirty_mask
|= 1u << slot
;
449 sctx
->descriptors_dirty
|= 1u << si_sampler_descriptors_idx(shader
);
452 static bool is_compressed_colortex(struct r600_texture
*rtex
)
454 return rtex
->cmask
.size
|| rtex
->fmask
.size
||
455 (rtex
->dcc_offset
&& rtex
->dirty_level_mask
);
458 static void si_set_sampler_views(struct pipe_context
*ctx
,
459 unsigned shader
, unsigned start
,
461 struct pipe_sampler_view
**views
)
463 struct si_context
*sctx
= (struct si_context
*)ctx
;
464 struct si_textures_info
*samplers
= &sctx
->samplers
[shader
];
467 if (!count
|| shader
>= SI_NUM_SHADERS
)
470 for (i
= 0; i
< count
; i
++) {
471 unsigned slot
= start
+ i
;
473 if (!views
|| !views
[i
]) {
474 samplers
->depth_texture_mask
&= ~(1u << slot
);
475 samplers
->compressed_colortex_mask
&= ~(1u << slot
);
476 si_set_sampler_view(sctx
, shader
, slot
, NULL
, false);
480 si_set_sampler_view(sctx
, shader
, slot
, views
[i
], false);
482 if (views
[i
]->texture
&& views
[i
]->texture
->target
!= PIPE_BUFFER
) {
483 struct r600_texture
*rtex
=
484 (struct r600_texture
*)views
[i
]->texture
;
486 if (rtex
->db_compatible
) {
487 samplers
->depth_texture_mask
|= 1u << slot
;
489 samplers
->depth_texture_mask
&= ~(1u << slot
);
491 if (is_compressed_colortex(rtex
)) {
492 samplers
->compressed_colortex_mask
|= 1u << slot
;
494 samplers
->compressed_colortex_mask
&= ~(1u << slot
);
497 if (rtex
->dcc_offset
&&
498 p_atomic_read(&rtex
->framebuffers_bound
))
499 sctx
->need_check_render_feedback
= true;
501 samplers
->depth_texture_mask
&= ~(1u << slot
);
502 samplers
->compressed_colortex_mask
&= ~(1u << slot
);
508 si_samplers_update_compressed_colortex_mask(struct si_textures_info
*samplers
)
510 unsigned mask
= samplers
->views
.enabled_mask
;
513 int i
= u_bit_scan(&mask
);
514 struct pipe_resource
*res
= samplers
->views
.views
[i
]->texture
;
516 if (res
&& res
->target
!= PIPE_BUFFER
) {
517 struct r600_texture
*rtex
= (struct r600_texture
*)res
;
519 if (is_compressed_colortex(rtex
)) {
520 samplers
->compressed_colortex_mask
|= 1u << i
;
522 samplers
->compressed_colortex_mask
&= ~(1u << i
);
531 si_image_descriptors_idx(unsigned shader
)
533 return SI_DESCS_FIRST_SHADER
+ shader
* SI_NUM_SHADER_DESCS
+
534 SI_SHADER_DESCS_IMAGES
;
537 static struct si_descriptors
*
538 si_image_descriptors(struct si_context
*sctx
, unsigned shader
)
540 return &sctx
->descriptors
[si_image_descriptors_idx(shader
)];
544 si_release_image_views(struct si_images_info
*images
)
548 for (i
= 0; i
< SI_NUM_IMAGES
; ++i
) {
549 struct pipe_image_view
*view
= &images
->views
[i
];
551 pipe_resource_reference(&view
->resource
, NULL
);
556 si_image_views_begin_new_cs(struct si_context
*sctx
, struct si_images_info
*images
)
558 uint mask
= images
->enabled_mask
;
560 /* Add buffers to the CS. */
562 int i
= u_bit_scan(&mask
);
563 struct pipe_image_view
*view
= &images
->views
[i
];
565 assert(view
->resource
);
567 si_sampler_view_add_buffer(sctx
, view
->resource
,
568 RADEON_USAGE_READWRITE
, false);
573 si_disable_shader_image(struct si_context
*ctx
, unsigned shader
, unsigned slot
)
575 struct si_images_info
*images
= &ctx
->images
[shader
];
577 if (images
->enabled_mask
& (1u << slot
)) {
578 struct si_descriptors
*descs
= si_image_descriptors(ctx
, shader
);
580 pipe_resource_reference(&images
->views
[slot
].resource
, NULL
);
581 images
->compressed_colortex_mask
&= ~(1 << slot
);
583 memcpy(descs
->list
+ slot
*8, null_image_descriptor
, 8*4);
584 images
->enabled_mask
&= ~(1u << slot
);
585 descs
->dirty_mask
|= 1u << slot
;
586 ctx
->descriptors_dirty
|= 1u << si_image_descriptors_idx(shader
);
591 si_mark_image_range_valid(const struct pipe_image_view
*view
)
593 struct r600_resource
*res
= (struct r600_resource
*)view
->resource
;
594 const struct util_format_description
*desc
;
597 assert(res
&& res
->b
.b
.target
== PIPE_BUFFER
);
599 desc
= util_format_description(view
->format
);
600 stride
= desc
->block
.bits
/ 8;
602 util_range_add(&res
->valid_buffer_range
,
603 stride
* (view
->u
.buf
.first_element
),
604 stride
* (view
->u
.buf
.last_element
+ 1));
607 static void si_set_shader_image(struct si_context
*ctx
,
609 unsigned slot
, const struct pipe_image_view
*view
)
611 struct si_screen
*screen
= ctx
->screen
;
612 struct si_images_info
*images
= &ctx
->images
[shader
];
613 struct si_descriptors
*descs
= si_image_descriptors(ctx
, shader
);
614 struct r600_resource
*res
;
616 if (!view
|| !view
->resource
) {
617 si_disable_shader_image(ctx
, shader
, slot
);
621 res
= (struct r600_resource
*)view
->resource
;
623 if (&images
->views
[slot
] != view
)
624 util_copy_image_view(&images
->views
[slot
], view
);
626 si_sampler_view_add_buffer(ctx
, &res
->b
.b
,
627 RADEON_USAGE_READWRITE
, false);
629 if (res
->b
.b
.target
== PIPE_BUFFER
) {
630 if (view
->access
& PIPE_IMAGE_ACCESS_WRITE
)
631 si_mark_image_range_valid(view
);
633 si_make_buffer_descriptor(screen
, res
,
635 view
->u
.buf
.first_element
,
636 view
->u
.buf
.last_element
,
637 descs
->list
+ slot
* 8);
638 images
->compressed_colortex_mask
&= ~(1 << slot
);
640 static const unsigned char swizzle
[4] = { 0, 1, 2, 3 };
641 struct r600_texture
*tex
= (struct r600_texture
*)res
;
642 unsigned level
= view
->u
.tex
.level
;
643 unsigned width
, height
, depth
;
644 uint32_t *desc
= descs
->list
+ slot
* 8;
645 bool uses_dcc
= tex
->dcc_offset
&&
646 tex
->surface
.level
[level
].dcc_enabled
;
648 assert(!tex
->is_depth
);
649 assert(tex
->fmask
.size
== 0);
652 view
->access
& PIPE_IMAGE_ACCESS_WRITE
) {
653 /* If DCC can't be disabled, at least decompress it.
654 * The decompression is relatively cheap if the surface
655 * has been decompressed already.
657 if (r600_texture_disable_dcc(&screen
->b
, tex
))
660 ctx
->b
.decompress_dcc(&ctx
->b
.b
, tex
);
663 if (is_compressed_colortex(tex
)) {
664 images
->compressed_colortex_mask
|= 1 << slot
;
666 images
->compressed_colortex_mask
&= ~(1 << slot
);
670 p_atomic_read(&tex
->framebuffers_bound
))
671 ctx
->need_check_render_feedback
= true;
673 /* Always force the base level to the selected level.
675 * This is required for 3D textures, where otherwise
676 * selecting a single slice for non-layered bindings
677 * fails. It doesn't hurt the other targets.
679 width
= u_minify(res
->b
.b
.width0
, level
);
680 height
= u_minify(res
->b
.b
.height0
, level
);
681 depth
= u_minify(res
->b
.b
.depth0
, level
);
683 si_make_texture_descriptor(screen
, tex
,
684 false, res
->b
.b
.target
,
685 view
->format
, swizzle
,
687 view
->u
.tex
.first_layer
,
688 view
->u
.tex
.last_layer
,
689 width
, height
, depth
,
691 si_set_mutable_tex_desc_fields(tex
, &tex
->surface
.level
[level
],
693 util_format_get_blockwidth(view
->format
),
697 images
->enabled_mask
|= 1u << slot
;
698 descs
->dirty_mask
|= 1u << slot
;
699 ctx
->descriptors_dirty
|= 1u << si_image_descriptors_idx(shader
);
703 si_set_shader_images(struct pipe_context
*pipe
, unsigned shader
,
704 unsigned start_slot
, unsigned count
,
705 const struct pipe_image_view
*views
)
707 struct si_context
*ctx
= (struct si_context
*)pipe
;
710 assert(shader
< SI_NUM_SHADERS
);
715 assert(start_slot
+ count
<= SI_NUM_IMAGES
);
718 for (i
= 0, slot
= start_slot
; i
< count
; ++i
, ++slot
)
719 si_set_shader_image(ctx
, shader
, slot
, &views
[i
]);
721 for (i
= 0, slot
= start_slot
; i
< count
; ++i
, ++slot
)
722 si_set_shader_image(ctx
, shader
, slot
, NULL
);
727 si_images_update_compressed_colortex_mask(struct si_images_info
*images
)
729 unsigned mask
= images
->enabled_mask
;
732 int i
= u_bit_scan(&mask
);
733 struct pipe_resource
*res
= images
->views
[i
].resource
;
735 if (res
&& res
->target
!= PIPE_BUFFER
) {
736 struct r600_texture
*rtex
= (struct r600_texture
*)res
;
738 if (is_compressed_colortex(rtex
)) {
739 images
->compressed_colortex_mask
|= 1 << i
;
741 images
->compressed_colortex_mask
&= ~(1 << i
);
749 static void si_bind_sampler_states(struct pipe_context
*ctx
, unsigned shader
,
750 unsigned start
, unsigned count
, void **states
)
752 struct si_context
*sctx
= (struct si_context
*)ctx
;
753 struct si_textures_info
*samplers
= &sctx
->samplers
[shader
];
754 struct si_descriptors
*desc
= si_sampler_descriptors(sctx
, shader
);
755 struct si_sampler_state
**sstates
= (struct si_sampler_state
**)states
;
758 if (!count
|| shader
>= SI_NUM_SHADERS
)
761 for (i
= 0; i
< count
; i
++) {
762 unsigned slot
= start
+ i
;
765 sstates
[i
] == samplers
->views
.sampler_states
[slot
])
768 samplers
->views
.sampler_states
[slot
] = sstates
[i
];
770 /* If FMASK is bound, don't overwrite it.
771 * The sampler state will be set after FMASK is unbound.
773 if (samplers
->views
.views
[i
] &&
774 samplers
->views
.views
[i
]->texture
&&
775 samplers
->views
.views
[i
]->texture
->target
!= PIPE_BUFFER
&&
776 ((struct r600_texture
*)samplers
->views
.views
[i
]->texture
)->fmask
.size
)
779 memcpy(desc
->list
+ slot
* 16 + 12, sstates
[i
]->val
, 4*4);
780 desc
->dirty_mask
|= 1u << slot
;
781 sctx
->descriptors_dirty
|= 1u << si_sampler_descriptors_idx(shader
);
785 /* BUFFER RESOURCES */
787 static void si_init_buffer_resources(struct si_buffer_resources
*buffers
,
788 struct si_descriptors
*descs
,
789 unsigned num_buffers
,
790 unsigned shader_userdata_index
,
791 enum radeon_bo_usage shader_usage
,
792 enum radeon_bo_priority priority
,
795 buffers
->shader_usage
= shader_usage
;
796 buffers
->priority
= priority
;
797 buffers
->buffers
= CALLOC(num_buffers
, sizeof(struct pipe_resource
*));
799 si_init_descriptors(descs
, shader_userdata_index
, 4,
800 num_buffers
, NULL
, ce_offset
);
803 static void si_release_buffer_resources(struct si_buffer_resources
*buffers
,
804 struct si_descriptors
*descs
)
808 for (i
= 0; i
< descs
->num_elements
; i
++) {
809 pipe_resource_reference(&buffers
->buffers
[i
], NULL
);
812 FREE(buffers
->buffers
);
815 static void si_buffer_resources_begin_new_cs(struct si_context
*sctx
,
816 struct si_buffer_resources
*buffers
)
818 unsigned mask
= buffers
->enabled_mask
;
820 /* Add buffers to the CS. */
822 int i
= u_bit_scan(&mask
);
824 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
825 (struct r600_resource
*)buffers
->buffers
[i
],
826 buffers
->shader_usage
, buffers
->priority
);
832 static void si_vertex_buffers_begin_new_cs(struct si_context
*sctx
)
834 struct si_descriptors
*desc
= &sctx
->vertex_buffers
;
835 int count
= sctx
->vertex_elements
? sctx
->vertex_elements
->count
: 0;
838 for (i
= 0; i
< count
; i
++) {
839 int vb
= sctx
->vertex_elements
->elements
[i
].vertex_buffer_index
;
841 if (vb
>= ARRAY_SIZE(sctx
->vertex_buffer
))
843 if (!sctx
->vertex_buffer
[vb
].buffer
)
846 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
847 (struct r600_resource
*)sctx
->vertex_buffer
[vb
].buffer
,
848 RADEON_USAGE_READ
, RADEON_PRIO_VERTEX_BUFFER
);
853 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
854 desc
->buffer
, RADEON_USAGE_READ
,
855 RADEON_PRIO_DESCRIPTORS
);
858 static bool si_upload_vertex_buffer_descriptors(struct si_context
*sctx
)
860 struct si_descriptors
*desc
= &sctx
->vertex_buffers
;
861 bool bound
[SI_NUM_VERTEX_BUFFERS
] = {};
862 unsigned i
, count
= sctx
->vertex_elements
->count
;
866 if (!sctx
->vertex_buffers_dirty
)
868 if (!count
|| !sctx
->vertex_elements
)
871 /* Vertex buffer descriptors are the only ones which are uploaded
872 * directly through a staging buffer and don't go through
873 * the fine-grained upload path.
875 u_upload_alloc(sctx
->b
.uploader
, 0, count
* 16, 256, &desc
->buffer_offset
,
876 (struct pipe_resource
**)&desc
->buffer
, (void**)&ptr
);
880 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
881 desc
->buffer
, RADEON_USAGE_READ
,
882 RADEON_PRIO_DESCRIPTORS
);
884 assert(count
<= SI_NUM_VERTEX_BUFFERS
);
886 for (i
= 0; i
< count
; i
++) {
887 struct pipe_vertex_element
*ve
= &sctx
->vertex_elements
->elements
[i
];
888 struct pipe_vertex_buffer
*vb
;
889 struct r600_resource
*rbuffer
;
891 uint32_t *desc
= &ptr
[i
*4];
893 if (ve
->vertex_buffer_index
>= ARRAY_SIZE(sctx
->vertex_buffer
)) {
898 vb
= &sctx
->vertex_buffer
[ve
->vertex_buffer_index
];
899 rbuffer
= (struct r600_resource
*)vb
->buffer
;
905 offset
= vb
->buffer_offset
+ ve
->src_offset
;
906 va
= rbuffer
->gpu_address
+ offset
;
908 /* Fill in T# buffer resource description */
910 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
911 S_008F04_STRIDE(vb
->stride
);
913 if (sctx
->b
.chip_class
<= CIK
&& vb
->stride
)
914 /* Round up by rounding down and adding 1 */
915 desc
[2] = (vb
->buffer
->width0
- offset
-
916 sctx
->vertex_elements
->format_size
[i
]) /
919 desc
[2] = vb
->buffer
->width0
- offset
;
921 desc
[3] = sctx
->vertex_elements
->rsrc_word3
[i
];
923 if (!bound
[ve
->vertex_buffer_index
]) {
924 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
925 (struct r600_resource
*)vb
->buffer
,
926 RADEON_USAGE_READ
, RADEON_PRIO_VERTEX_BUFFER
);
927 bound
[ve
->vertex_buffer_index
] = true;
931 /* Don't flush the const cache. It would have a very negative effect
932 * on performance (confirmed by testing). New descriptors are always
933 * uploaded to a fresh new buffer, so I don't think flushing the const
934 * cache is needed. */
935 desc
->pointer_dirty
= true;
936 si_mark_atom_dirty(sctx
, &sctx
->shader_userdata
.atom
);
937 sctx
->vertex_buffers_dirty
= false;
942 /* CONSTANT BUFFERS */
945 si_const_buffer_descriptors_idx(unsigned shader
)
947 return SI_DESCS_FIRST_SHADER
+ shader
* SI_NUM_SHADER_DESCS
+
948 SI_SHADER_DESCS_CONST_BUFFERS
;
951 static struct si_descriptors
*
952 si_const_buffer_descriptors(struct si_context
*sctx
, unsigned shader
)
954 return &sctx
->descriptors
[si_const_buffer_descriptors_idx(shader
)];
957 void si_upload_const_buffer(struct si_context
*sctx
, struct r600_resource
**rbuffer
,
958 const uint8_t *ptr
, unsigned size
, uint32_t *const_offset
)
962 u_upload_alloc(sctx
->b
.uploader
, 0, size
, 256, const_offset
,
963 (struct pipe_resource
**)rbuffer
, &tmp
);
965 util_memcpy_cpu_to_le32(tmp
, ptr
, size
);
968 static void si_set_constant_buffer(struct si_context
*sctx
,
969 struct si_buffer_resources
*buffers
,
970 unsigned descriptors_idx
,
971 uint slot
, const struct pipe_constant_buffer
*input
)
973 struct si_descriptors
*descs
= &sctx
->descriptors
[descriptors_idx
];
974 assert(slot
< descs
->num_elements
);
975 pipe_resource_reference(&buffers
->buffers
[slot
], NULL
);
977 /* CIK cannot unbind a constant buffer (S_BUFFER_LOAD is buggy
978 * with a NULL buffer). We need to use a dummy buffer instead. */
979 if (sctx
->b
.chip_class
== CIK
&&
980 (!input
|| (!input
->buffer
&& !input
->user_buffer
)))
981 input
= &sctx
->null_const_buf
;
983 if (input
&& (input
->buffer
|| input
->user_buffer
)) {
984 struct pipe_resource
*buffer
= NULL
;
987 /* Upload the user buffer if needed. */
988 if (input
->user_buffer
) {
989 unsigned buffer_offset
;
991 si_upload_const_buffer(sctx
,
992 (struct r600_resource
**)&buffer
, input
->user_buffer
,
993 input
->buffer_size
, &buffer_offset
);
995 /* Just unbind on failure. */
996 si_set_constant_buffer(sctx
, buffers
, descriptors_idx
, slot
, NULL
);
999 va
= r600_resource(buffer
)->gpu_address
+ buffer_offset
;
1001 pipe_resource_reference(&buffer
, input
->buffer
);
1002 va
= r600_resource(buffer
)->gpu_address
+ input
->buffer_offset
;
1005 /* Set the descriptor. */
1006 uint32_t *desc
= descs
->list
+ slot
*4;
1008 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1010 desc
[2] = input
->buffer_size
;
1011 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1012 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1013 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1014 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1015 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1016 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
1018 buffers
->buffers
[slot
] = buffer
;
1019 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
1020 (struct r600_resource
*)buffer
,
1021 buffers
->shader_usage
, buffers
->priority
);
1022 buffers
->enabled_mask
|= 1u << slot
;
1024 /* Clear the descriptor. */
1025 memset(descs
->list
+ slot
*4, 0, sizeof(uint32_t) * 4);
1026 buffers
->enabled_mask
&= ~(1u << slot
);
1029 descs
->dirty_mask
|= 1u << slot
;
1030 sctx
->descriptors_dirty
|= 1u << descriptors_idx
;
1033 void si_set_rw_buffer(struct si_context
*sctx
,
1034 uint slot
, const struct pipe_constant_buffer
*input
)
1036 si_set_constant_buffer(sctx
, &sctx
->rw_buffers
,
1037 SI_DESCS_RW_BUFFERS
, slot
, input
);
1040 static void si_pipe_set_constant_buffer(struct pipe_context
*ctx
,
1041 uint shader
, uint slot
,
1042 const struct pipe_constant_buffer
*input
)
1044 struct si_context
*sctx
= (struct si_context
*)ctx
;
1046 if (shader
>= SI_NUM_SHADERS
)
1049 si_set_constant_buffer(sctx
, &sctx
->const_buffers
[shader
],
1050 si_const_buffer_descriptors_idx(shader
),
1054 /* SHADER BUFFERS */
1057 si_shader_buffer_descriptors_idx(unsigned shader
)
1059 return SI_DESCS_FIRST_SHADER
+ shader
* SI_NUM_SHADER_DESCS
+
1060 SI_SHADER_DESCS_SHADER_BUFFERS
;
1063 static struct si_descriptors
*
1064 si_shader_buffer_descriptors(struct si_context
*sctx
, unsigned shader
)
1066 return &sctx
->descriptors
[si_shader_buffer_descriptors_idx(shader
)];
1069 static void si_set_shader_buffers(struct pipe_context
*ctx
, unsigned shader
,
1070 unsigned start_slot
, unsigned count
,
1071 const struct pipe_shader_buffer
*sbuffers
)
1073 struct si_context
*sctx
= (struct si_context
*)ctx
;
1074 struct si_buffer_resources
*buffers
= &sctx
->shader_buffers
[shader
];
1075 struct si_descriptors
*descs
= si_shader_buffer_descriptors(sctx
, shader
);
1078 assert(start_slot
+ count
<= SI_NUM_SHADER_BUFFERS
);
1080 for (i
= 0; i
< count
; ++i
) {
1081 const struct pipe_shader_buffer
*sbuffer
= sbuffers
? &sbuffers
[i
] : NULL
;
1082 struct r600_resource
*buf
;
1083 unsigned slot
= start_slot
+ i
;
1084 uint32_t *desc
= descs
->list
+ slot
* 4;
1087 if (!sbuffer
|| !sbuffer
->buffer
) {
1088 pipe_resource_reference(&buffers
->buffers
[slot
], NULL
);
1089 memset(desc
, 0, sizeof(uint32_t) * 4);
1090 buffers
->enabled_mask
&= ~(1u << slot
);
1091 descs
->dirty_mask
|= 1u << slot
;
1092 sctx
->descriptors_dirty
|=
1093 1u << si_shader_buffer_descriptors_idx(shader
);
1097 buf
= (struct r600_resource
*)sbuffer
->buffer
;
1098 va
= buf
->gpu_address
+ sbuffer
->buffer_offset
;
1101 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1103 desc
[2] = sbuffer
->buffer_size
;
1104 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1105 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1106 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1107 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1108 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1109 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
1111 pipe_resource_reference(&buffers
->buffers
[slot
], &buf
->b
.b
);
1112 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
, buf
,
1113 buffers
->shader_usage
, buffers
->priority
);
1114 buffers
->enabled_mask
|= 1u << slot
;
1115 descs
->dirty_mask
|= 1u << slot
;
1116 sctx
->descriptors_dirty
|=
1117 1u << si_shader_buffer_descriptors_idx(shader
);
1123 void si_set_ring_buffer(struct pipe_context
*ctx
, uint slot
,
1124 struct pipe_resource
*buffer
,
1125 unsigned stride
, unsigned num_records
,
1126 bool add_tid
, bool swizzle
,
1127 unsigned element_size
, unsigned index_stride
, uint64_t offset
)
1129 struct si_context
*sctx
= (struct si_context
*)ctx
;
1130 struct si_buffer_resources
*buffers
= &sctx
->rw_buffers
;
1131 struct si_descriptors
*descs
= &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
1133 /* The stride field in the resource descriptor has 14 bits */
1134 assert(stride
< (1 << 14));
1136 assert(slot
< descs
->num_elements
);
1137 pipe_resource_reference(&buffers
->buffers
[slot
], NULL
);
1142 va
= r600_resource(buffer
)->gpu_address
+ offset
;
1144 switch (element_size
) {
1146 assert(!"Unsupported ring buffer element size");
1162 switch (index_stride
) {
1164 assert(!"Unsupported ring buffer index stride");
1180 if (sctx
->b
.chip_class
>= VI
&& stride
)
1181 num_records
*= stride
;
1183 /* Set the descriptor. */
1184 uint32_t *desc
= descs
->list
+ slot
*4;
1186 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1187 S_008F04_STRIDE(stride
) |
1188 S_008F04_SWIZZLE_ENABLE(swizzle
);
1189 desc
[2] = num_records
;
1190 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1191 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1192 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1193 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1194 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1195 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
) |
1196 S_008F0C_ELEMENT_SIZE(element_size
) |
1197 S_008F0C_INDEX_STRIDE(index_stride
) |
1198 S_008F0C_ADD_TID_ENABLE(add_tid
);
1200 pipe_resource_reference(&buffers
->buffers
[slot
], buffer
);
1201 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
1202 (struct r600_resource
*)buffer
,
1203 buffers
->shader_usage
, buffers
->priority
);
1204 buffers
->enabled_mask
|= 1u << slot
;
1206 /* Clear the descriptor. */
1207 memset(descs
->list
+ slot
*4, 0, sizeof(uint32_t) * 4);
1208 buffers
->enabled_mask
&= ~(1u << slot
);
1211 descs
->dirty_mask
|= 1u << slot
;
1212 sctx
->descriptors_dirty
|= 1u << SI_DESCS_RW_BUFFERS
;
1215 /* STREAMOUT BUFFERS */
1217 static void si_set_streamout_targets(struct pipe_context
*ctx
,
1218 unsigned num_targets
,
1219 struct pipe_stream_output_target
**targets
,
1220 const unsigned *offsets
)
1222 struct si_context
*sctx
= (struct si_context
*)ctx
;
1223 struct si_buffer_resources
*buffers
= &sctx
->rw_buffers
;
1224 struct si_descriptors
*descs
= &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
1225 unsigned old_num_targets
= sctx
->b
.streamout
.num_targets
;
1228 /* We are going to unbind the buffers. Mark which caches need to be flushed. */
1229 if (sctx
->b
.streamout
.num_targets
&& sctx
->b
.streamout
.begin_emitted
) {
1230 /* Since streamout uses vector writes which go through TC L2
1231 * and most other clients can use TC L2 as well, we don't need
1234 * The only case which requires flushing it is VGT DMA index
1235 * fetching, which is a rare case. Thus, flag the TC L2
1236 * dirtiness in the resource and handle it when index fetching
1239 for (i
= 0; i
< sctx
->b
.streamout
.num_targets
; i
++)
1240 if (sctx
->b
.streamout
.targets
[i
])
1241 r600_resource(sctx
->b
.streamout
.targets
[i
]->b
.buffer
)->TC_L2_dirty
= true;
1243 /* Invalidate the scalar cache in case a streamout buffer is
1244 * going to be used as a constant buffer.
1246 * Invalidate TC L1, because streamout bypasses it (done by
1247 * setting GLC=1 in the store instruction), but it can contain
1248 * outdated data of streamout buffers.
1250 * VS_PARTIAL_FLUSH is required if the buffers are going to be
1251 * used as an input immediately.
1253 sctx
->b
.flags
|= SI_CONTEXT_INV_SMEM_L1
|
1254 SI_CONTEXT_INV_VMEM_L1
|
1255 SI_CONTEXT_VS_PARTIAL_FLUSH
;
1258 /* All readers of the streamout targets need to be finished before we can
1259 * start writing to the targets.
1262 sctx
->b
.flags
|= SI_CONTEXT_PS_PARTIAL_FLUSH
|
1263 SI_CONTEXT_CS_PARTIAL_FLUSH
;
1265 /* Streamout buffers must be bound in 2 places:
1266 * 1) in VGT by setting the VGT_STRMOUT registers
1267 * 2) as shader resources
1270 /* Set the VGT regs. */
1271 r600_set_streamout_targets(ctx
, num_targets
, targets
, offsets
);
1273 /* Set the shader resources.*/
1274 for (i
= 0; i
< num_targets
; i
++) {
1275 bufidx
= SI_VS_STREAMOUT_BUF0
+ i
;
1278 struct pipe_resource
*buffer
= targets
[i
]->buffer
;
1279 uint64_t va
= r600_resource(buffer
)->gpu_address
;
1281 /* Set the descriptor.
1283 * On VI, the format must be non-INVALID, otherwise
1284 * the buffer will be considered not bound and store
1285 * instructions will be no-ops.
1287 uint32_t *desc
= descs
->list
+ bufidx
*4;
1289 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32);
1290 desc
[2] = 0xffffffff;
1291 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1292 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1293 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1294 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1295 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
1297 /* Set the resource. */
1298 pipe_resource_reference(&buffers
->buffers
[bufidx
],
1300 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
1301 (struct r600_resource
*)buffer
,
1302 buffers
->shader_usage
, buffers
->priority
);
1303 buffers
->enabled_mask
|= 1u << bufidx
;
1305 /* Clear the descriptor and unset the resource. */
1306 memset(descs
->list
+ bufidx
*4, 0,
1307 sizeof(uint32_t) * 4);
1308 pipe_resource_reference(&buffers
->buffers
[bufidx
],
1310 buffers
->enabled_mask
&= ~(1u << bufidx
);
1312 descs
->dirty_mask
|= 1u << bufidx
;
1314 for (; i
< old_num_targets
; i
++) {
1315 bufidx
= SI_VS_STREAMOUT_BUF0
+ i
;
1316 /* Clear the descriptor and unset the resource. */
1317 memset(descs
->list
+ bufidx
*4, 0, sizeof(uint32_t) * 4);
1318 pipe_resource_reference(&buffers
->buffers
[bufidx
], NULL
);
1319 buffers
->enabled_mask
&= ~(1u << bufidx
);
1320 descs
->dirty_mask
|= 1u << bufidx
;
1323 sctx
->descriptors_dirty
|= 1u << SI_DESCS_RW_BUFFERS
;
1326 static void si_desc_reset_buffer_offset(struct pipe_context
*ctx
,
1327 uint32_t *desc
, uint64_t old_buf_va
,
1328 struct pipe_resource
*new_buf
)
1330 /* Retrieve the buffer offset from the descriptor. */
1331 uint64_t old_desc_va
=
1332 desc
[0] | ((uint64_t)G_008F04_BASE_ADDRESS_HI(desc
[1]) << 32);
1334 assert(old_buf_va
<= old_desc_va
);
1335 uint64_t offset_within_buffer
= old_desc_va
- old_buf_va
;
1337 /* Update the descriptor. */
1338 uint64_t va
= r600_resource(new_buf
)->gpu_address
+ offset_within_buffer
;
1341 desc
[1] = (desc
[1] & C_008F04_BASE_ADDRESS_HI
) |
1342 S_008F04_BASE_ADDRESS_HI(va
>> 32);
1345 /* INTERNAL CONST BUFFERS */
1347 static void si_set_polygon_stipple(struct pipe_context
*ctx
,
1348 const struct pipe_poly_stipple
*state
)
1350 struct si_context
*sctx
= (struct si_context
*)ctx
;
1351 struct pipe_constant_buffer cb
= {};
1352 unsigned stipple
[32];
1355 for (i
= 0; i
< 32; i
++)
1356 stipple
[i
] = util_bitreverse(state
->stipple
[i
]);
1358 cb
.user_buffer
= stipple
;
1359 cb
.buffer_size
= sizeof(stipple
);
1361 si_set_rw_buffer(sctx
, SI_PS_CONST_POLY_STIPPLE
, &cb
);
1364 /* TEXTURE METADATA ENABLE/DISABLE */
1366 /* CMASK can be enabled (for fast clear) and disabled (for texture export)
1367 * while the texture is bound, possibly by a different context. In that case,
1368 * call this function to update compressed_colortex_masks.
1370 void si_update_compressed_colortex_masks(struct si_context
*sctx
)
1372 for (int i
= 0; i
< SI_NUM_SHADERS
; ++i
) {
1373 si_samplers_update_compressed_colortex_mask(&sctx
->samplers
[i
]);
1374 si_images_update_compressed_colortex_mask(&sctx
->images
[i
]);
1378 /* BUFFER DISCARD/INVALIDATION */
1380 /** Reset descriptors of buffer resources after \p buf has been invalidated. */
1381 static void si_reset_buffer_resources(struct si_context
*sctx
,
1382 struct si_buffer_resources
*buffers
,
1383 unsigned descriptors_idx
,
1384 struct pipe_resource
*buf
,
1387 struct si_descriptors
*descs
= &sctx
->descriptors
[descriptors_idx
];
1388 unsigned mask
= buffers
->enabled_mask
;
1391 unsigned i
= u_bit_scan(&mask
);
1392 if (buffers
->buffers
[i
] == buf
) {
1393 si_desc_reset_buffer_offset(&sctx
->b
.b
,
1396 descs
->dirty_mask
|= 1u << i
;
1397 sctx
->descriptors_dirty
|= 1u << descriptors_idx
;
1399 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
1400 (struct r600_resource
*)buf
,
1401 buffers
->shader_usage
,
1407 /* Reallocate a buffer a update all resource bindings where the buffer is
1410 * This is used to avoid CPU-GPU synchronizations, because it makes the buffer
1411 * idle by discarding its contents. Apps usually tell us when to do this using
1412 * map_buffer flags, for example.
1414 static void si_invalidate_buffer(struct pipe_context
*ctx
, struct pipe_resource
*buf
)
1416 struct si_context
*sctx
= (struct si_context
*)ctx
;
1417 struct r600_resource
*rbuffer
= r600_resource(buf
);
1418 unsigned i
, shader
, alignment
= rbuffer
->buf
->alignment
;
1419 uint64_t old_va
= rbuffer
->gpu_address
;
1420 unsigned num_elems
= sctx
->vertex_elements
?
1421 sctx
->vertex_elements
->count
: 0;
1422 struct si_sampler_view
*view
;
1424 /* Reallocate the buffer in the same pipe_resource. */
1425 r600_init_resource(&sctx
->screen
->b
, rbuffer
, rbuffer
->b
.b
.width0
,
1428 /* We changed the buffer, now we need to bind it where the old one
1429 * was bound. This consists of 2 things:
1430 * 1) Updating the resource descriptor and dirtying it.
1431 * 2) Adding a relocation to the CS, so that it's usable.
1434 /* Vertex buffers. */
1435 for (i
= 0; i
< num_elems
; i
++) {
1436 int vb
= sctx
->vertex_elements
->elements
[i
].vertex_buffer_index
;
1438 if (vb
>= ARRAY_SIZE(sctx
->vertex_buffer
))
1440 if (!sctx
->vertex_buffer
[vb
].buffer
)
1443 if (sctx
->vertex_buffer
[vb
].buffer
== buf
) {
1444 sctx
->vertex_buffers_dirty
= true;
1449 /* Streamout buffers. (other internal buffers can't be invalidated) */
1450 for (i
= SI_VS_STREAMOUT_BUF0
; i
<= SI_VS_STREAMOUT_BUF3
; i
++) {
1451 struct si_buffer_resources
*buffers
= &sctx
->rw_buffers
;
1452 struct si_descriptors
*descs
=
1453 &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
1455 if (buffers
->buffers
[i
] != buf
)
1458 si_desc_reset_buffer_offset(ctx
, descs
->list
+ i
*4,
1460 descs
->dirty_mask
|= 1u << i
;
1461 sctx
->descriptors_dirty
|= 1u << SI_DESCS_RW_BUFFERS
;
1463 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
1464 rbuffer
, buffers
->shader_usage
,
1467 /* Update the streamout state. */
1468 if (sctx
->b
.streamout
.begin_emitted
)
1469 r600_emit_streamout_end(&sctx
->b
);
1470 sctx
->b
.streamout
.append_bitmask
=
1471 sctx
->b
.streamout
.enabled_mask
;
1472 r600_streamout_buffers_dirty(&sctx
->b
);
1475 /* Constant and shader buffers. */
1476 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++) {
1477 si_reset_buffer_resources(sctx
, &sctx
->const_buffers
[shader
],
1478 si_const_buffer_descriptors_idx(shader
),
1480 si_reset_buffer_resources(sctx
, &sctx
->shader_buffers
[shader
],
1481 si_shader_buffer_descriptors_idx(shader
),
1485 /* Texture buffers - update virtual addresses in sampler view descriptors. */
1486 LIST_FOR_EACH_ENTRY(view
, &sctx
->b
.texture_buffers
, list
) {
1487 if (view
->base
.texture
== buf
) {
1488 si_desc_reset_buffer_offset(ctx
, &view
->state
[4], old_va
, buf
);
1491 /* Texture buffers - update bindings. */
1492 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++) {
1493 struct si_sampler_views
*views
= &sctx
->samplers
[shader
].views
;
1494 struct si_descriptors
*descs
=
1495 si_sampler_descriptors(sctx
, shader
);
1496 unsigned mask
= views
->enabled_mask
;
1499 unsigned i
= u_bit_scan(&mask
);
1500 if (views
->views
[i
]->texture
== buf
) {
1501 si_desc_reset_buffer_offset(ctx
,
1505 descs
->dirty_mask
|= 1u << i
;
1506 sctx
->descriptors_dirty
|=
1507 1u << si_sampler_descriptors_idx(shader
);
1509 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
1510 rbuffer
, RADEON_USAGE_READ
,
1511 RADEON_PRIO_SAMPLER_BUFFER
);
1517 for (shader
= 0; shader
< SI_NUM_SHADERS
; ++shader
) {
1518 struct si_images_info
*images
= &sctx
->images
[shader
];
1519 struct si_descriptors
*descs
=
1520 si_image_descriptors(sctx
, shader
);
1521 unsigned mask
= images
->enabled_mask
;
1524 unsigned i
= u_bit_scan(&mask
);
1526 if (images
->views
[i
].resource
== buf
) {
1527 if (images
->views
[i
].access
& PIPE_IMAGE_ACCESS_WRITE
)
1528 si_mark_image_range_valid(&images
->views
[i
]);
1530 si_desc_reset_buffer_offset(
1531 ctx
, descs
->list
+ i
* 8 + 4,
1533 descs
->dirty_mask
|= 1u << i
;
1534 sctx
->descriptors_dirty
|=
1535 1u << si_image_descriptors_idx(shader
);
1537 radeon_add_to_buffer_list(
1538 &sctx
->b
, &sctx
->b
.gfx
, rbuffer
,
1539 RADEON_USAGE_READWRITE
,
1540 RADEON_PRIO_SAMPLER_BUFFER
);
1546 /* Update mutable image descriptor fields of all bound textures. */
1547 void si_update_all_texture_descriptors(struct si_context
*sctx
)
1551 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++) {
1552 struct si_sampler_views
*samplers
= &sctx
->samplers
[shader
].views
;
1553 struct si_images_info
*images
= &sctx
->images
[shader
];
1557 mask
= images
->enabled_mask
;
1559 unsigned i
= u_bit_scan(&mask
);
1560 struct pipe_image_view
*view
= &images
->views
[i
];
1562 if (!view
->resource
||
1563 view
->resource
->target
== PIPE_BUFFER
)
1566 si_set_shader_image(sctx
, shader
, i
, view
);
1569 /* Sampler views. */
1570 mask
= samplers
->enabled_mask
;
1572 unsigned i
= u_bit_scan(&mask
);
1573 struct pipe_sampler_view
*view
= samplers
->views
[i
];
1577 view
->texture
->target
== PIPE_BUFFER
)
1580 si_set_sampler_view(sctx
, shader
, i
,
1581 samplers
->views
[i
], true);
1586 /* SHADER USER DATA */
1588 static void si_mark_shader_pointers_dirty(struct si_context
*sctx
,
1591 struct si_descriptors
*descs
=
1592 &sctx
->descriptors
[SI_DESCS_FIRST_SHADER
+ shader
* SI_NUM_SHADER_DESCS
];
1594 for (unsigned i
= 0; i
< SI_NUM_SHADER_DESCS
; ++i
, ++descs
)
1595 descs
->pointer_dirty
= true;
1597 if (shader
== PIPE_SHADER_VERTEX
)
1598 sctx
->vertex_buffers
.pointer_dirty
= true;
1600 si_mark_atom_dirty(sctx
, &sctx
->shader_userdata
.atom
);
1603 static void si_shader_userdata_begin_new_cs(struct si_context
*sctx
)
1607 for (i
= 0; i
< SI_NUM_SHADERS
; i
++) {
1608 si_mark_shader_pointers_dirty(sctx
, i
);
1610 sctx
->descriptors
[SI_DESCS_RW_BUFFERS
].pointer_dirty
= true;
1613 /* Set a base register address for user data constants in the given shader.
1614 * This assigns a mapping from PIPE_SHADER_* to SPI_SHADER_USER_DATA_*.
1616 static void si_set_user_data_base(struct si_context
*sctx
,
1617 unsigned shader
, uint32_t new_base
)
1619 uint32_t *base
= &sctx
->shader_userdata
.sh_base
[shader
];
1621 if (*base
!= new_base
) {
1625 si_mark_shader_pointers_dirty(sctx
, shader
);
1629 /* This must be called when these shaders are changed from non-NULL to NULL
1632 * - tessellation control shader
1633 * - tessellation evaluation shader
1635 void si_shader_change_notify(struct si_context
*sctx
)
1637 /* VS can be bound as VS, ES, or LS. */
1638 if (sctx
->tes_shader
.cso
)
1639 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
1640 R_00B530_SPI_SHADER_USER_DATA_LS_0
);
1641 else if (sctx
->gs_shader
.cso
)
1642 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
1643 R_00B330_SPI_SHADER_USER_DATA_ES_0
);
1645 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
1646 R_00B130_SPI_SHADER_USER_DATA_VS_0
);
1648 /* TES can be bound as ES, VS, or not bound. */
1649 if (sctx
->tes_shader
.cso
) {
1650 if (sctx
->gs_shader
.cso
)
1651 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_EVAL
,
1652 R_00B330_SPI_SHADER_USER_DATA_ES_0
);
1654 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_EVAL
,
1655 R_00B130_SPI_SHADER_USER_DATA_VS_0
);
1657 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_EVAL
, 0);
1661 static void si_emit_shader_pointer(struct si_context
*sctx
,
1662 struct si_descriptors
*desc
,
1663 unsigned sh_base
, bool keep_dirty
)
1665 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
1668 if (!desc
->pointer_dirty
|| !desc
->buffer
)
1671 va
= desc
->buffer
->gpu_address
+
1672 desc
->buffer_offset
;
1674 radeon_emit(cs
, PKT3(PKT3_SET_SH_REG
, 2, 0));
1675 radeon_emit(cs
, (sh_base
+ desc
->shader_userdata_offset
- SI_SH_REG_OFFSET
) >> 2);
1676 radeon_emit(cs
, va
);
1677 radeon_emit(cs
, va
>> 32);
1679 desc
->pointer_dirty
= keep_dirty
;
1682 void si_emit_graphics_shader_userdata(struct si_context
*sctx
,
1683 struct r600_atom
*atom
)
1686 uint32_t *sh_base
= sctx
->shader_userdata
.sh_base
;
1687 struct si_descriptors
*descs
;
1689 descs
= &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
1691 if (descs
->pointer_dirty
) {
1692 si_emit_shader_pointer(sctx
, descs
,
1693 R_00B030_SPI_SHADER_USER_DATA_PS_0
, true);
1694 si_emit_shader_pointer(sctx
, descs
,
1695 R_00B130_SPI_SHADER_USER_DATA_VS_0
, true);
1696 si_emit_shader_pointer(sctx
, descs
,
1697 R_00B230_SPI_SHADER_USER_DATA_GS_0
, true);
1698 si_emit_shader_pointer(sctx
, descs
,
1699 R_00B330_SPI_SHADER_USER_DATA_ES_0
, true);
1700 si_emit_shader_pointer(sctx
, descs
,
1701 R_00B430_SPI_SHADER_USER_DATA_HS_0
, true);
1702 descs
->pointer_dirty
= false;
1705 descs
= &sctx
->descriptors
[SI_DESCS_FIRST_SHADER
];
1707 for (shader
= 0; shader
< SI_NUM_GRAPHICS_SHADERS
; shader
++) {
1708 unsigned base
= sh_base
[shader
];
1714 for (i
= 0; i
< SI_NUM_SHADER_DESCS
; i
++, descs
++)
1715 si_emit_shader_pointer(sctx
, descs
, base
, false);
1717 si_emit_shader_pointer(sctx
, &sctx
->vertex_buffers
, sh_base
[PIPE_SHADER_VERTEX
], false);
1720 void si_emit_compute_shader_userdata(struct si_context
*sctx
)
1722 unsigned base
= R_00B900_COMPUTE_USER_DATA_0
;
1723 struct si_descriptors
*descs
= &sctx
->descriptors
[SI_DESCS_FIRST_COMPUTE
];
1725 for (unsigned i
= 0; i
< SI_NUM_SHADER_DESCS
; ++i
, ++descs
)
1726 si_emit_shader_pointer(sctx
, descs
, base
, false);
1729 /* INIT/DEINIT/UPLOAD */
1731 void si_init_all_descriptors(struct si_context
*sctx
)
1734 unsigned ce_offset
= 0;
1736 for (i
= 0; i
< SI_NUM_SHADERS
; i
++) {
1737 si_init_buffer_resources(&sctx
->const_buffers
[i
],
1738 si_const_buffer_descriptors(sctx
, i
),
1739 SI_NUM_CONST_BUFFERS
, SI_SGPR_CONST_BUFFERS
,
1740 RADEON_USAGE_READ
, RADEON_PRIO_CONST_BUFFER
,
1742 si_init_buffer_resources(&sctx
->shader_buffers
[i
],
1743 si_shader_buffer_descriptors(sctx
, i
),
1744 SI_NUM_SHADER_BUFFERS
, SI_SGPR_SHADER_BUFFERS
,
1745 RADEON_USAGE_READWRITE
, RADEON_PRIO_SHADER_RW_BUFFER
,
1748 si_init_descriptors(si_sampler_descriptors(sctx
, i
),
1749 SI_SGPR_SAMPLERS
, 16, SI_NUM_SAMPLERS
,
1750 null_texture_descriptor
, &ce_offset
);
1752 si_init_descriptors(si_image_descriptors(sctx
, i
),
1753 SI_SGPR_IMAGES
, 8, SI_NUM_IMAGES
,
1754 null_image_descriptor
, &ce_offset
);
1757 si_init_buffer_resources(&sctx
->rw_buffers
,
1758 &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
],
1759 SI_NUM_RW_BUFFERS
, SI_SGPR_RW_BUFFERS
,
1760 RADEON_USAGE_READWRITE
, RADEON_PRIO_RINGS_STREAMOUT
,
1762 si_init_descriptors(&sctx
->vertex_buffers
, SI_SGPR_VERTEX_BUFFERS
,
1763 4, SI_NUM_VERTEX_BUFFERS
, NULL
, NULL
);
1765 sctx
->descriptors_dirty
= u_bit_consecutive(0, SI_NUM_DESCS
);
1767 assert(ce_offset
<= 32768);
1769 /* Set pipe_context functions. */
1770 sctx
->b
.b
.bind_sampler_states
= si_bind_sampler_states
;
1771 sctx
->b
.b
.set_shader_images
= si_set_shader_images
;
1772 sctx
->b
.b
.set_constant_buffer
= si_pipe_set_constant_buffer
;
1773 sctx
->b
.b
.set_polygon_stipple
= si_set_polygon_stipple
;
1774 sctx
->b
.b
.set_shader_buffers
= si_set_shader_buffers
;
1775 sctx
->b
.b
.set_sampler_views
= si_set_sampler_views
;
1776 sctx
->b
.b
.set_stream_output_targets
= si_set_streamout_targets
;
1777 sctx
->b
.invalidate_buffer
= si_invalidate_buffer
;
1779 /* Shader user data. */
1780 si_init_atom(sctx
, &sctx
->shader_userdata
.atom
, &sctx
->atoms
.s
.shader_userdata
,
1781 si_emit_graphics_shader_userdata
);
1783 /* Set default and immutable mappings. */
1784 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
, R_00B130_SPI_SHADER_USER_DATA_VS_0
);
1785 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_CTRL
, R_00B430_SPI_SHADER_USER_DATA_HS_0
);
1786 si_set_user_data_base(sctx
, PIPE_SHADER_GEOMETRY
, R_00B230_SPI_SHADER_USER_DATA_GS_0
);
1787 si_set_user_data_base(sctx
, PIPE_SHADER_FRAGMENT
, R_00B030_SPI_SHADER_USER_DATA_PS_0
);
1790 bool si_upload_graphics_shader_descriptors(struct si_context
*sctx
)
1792 const unsigned mask
= u_bit_consecutive(0, SI_DESCS_FIRST_COMPUTE
);
1793 unsigned dirty
= sctx
->descriptors_dirty
& mask
;
1796 unsigned i
= u_bit_scan(&dirty
);
1798 if (!si_upload_descriptors(sctx
, &sctx
->descriptors
[i
],
1799 &sctx
->shader_userdata
.atom
))
1803 sctx
->descriptors_dirty
&= ~mask
;
1805 return si_upload_vertex_buffer_descriptors(sctx
);
1808 bool si_upload_compute_shader_descriptors(struct si_context
*sctx
)
1810 /* Does not update rw_buffers as that is not needed for compute shaders
1811 * and the input buffer is using the same SGPR's anyway.
1813 const unsigned mask
= u_bit_consecutive(SI_DESCS_FIRST_COMPUTE
,
1814 SI_NUM_DESCS
- SI_DESCS_FIRST_COMPUTE
);
1815 unsigned dirty
= sctx
->descriptors_dirty
& mask
;
1818 unsigned i
= u_bit_scan(&dirty
);
1820 if (!si_upload_descriptors(sctx
, &sctx
->descriptors
[i
], NULL
))
1824 sctx
->descriptors_dirty
&= ~mask
;
1829 void si_release_all_descriptors(struct si_context
*sctx
)
1833 for (i
= 0; i
< SI_NUM_SHADERS
; i
++) {
1834 si_release_buffer_resources(&sctx
->const_buffers
[i
],
1835 si_const_buffer_descriptors(sctx
, i
));
1836 si_release_buffer_resources(&sctx
->shader_buffers
[i
],
1837 si_shader_buffer_descriptors(sctx
, i
));
1838 si_release_sampler_views(&sctx
->samplers
[i
].views
);
1839 si_release_image_views(&sctx
->images
[i
]);
1841 si_release_buffer_resources(&sctx
->rw_buffers
,
1842 &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
]);
1844 for (i
= 0; i
< SI_NUM_DESCS
; ++i
)
1845 si_release_descriptors(&sctx
->descriptors
[i
]);
1846 si_release_descriptors(&sctx
->vertex_buffers
);
1849 void si_all_descriptors_begin_new_cs(struct si_context
*sctx
)
1853 for (i
= 0; i
< SI_NUM_SHADERS
; i
++) {
1854 si_buffer_resources_begin_new_cs(sctx
, &sctx
->const_buffers
[i
]);
1855 si_buffer_resources_begin_new_cs(sctx
, &sctx
->shader_buffers
[i
]);
1856 si_sampler_views_begin_new_cs(sctx
, &sctx
->samplers
[i
].views
);
1857 si_image_views_begin_new_cs(sctx
, &sctx
->images
[i
]);
1859 si_buffer_resources_begin_new_cs(sctx
, &sctx
->rw_buffers
);
1860 si_vertex_buffers_begin_new_cs(sctx
);
1862 for (i
= 0; i
< SI_NUM_DESCS
; ++i
)
1863 si_descriptors_begin_new_cs(sctx
, &sctx
->descriptors
[i
]);
1865 si_shader_userdata_begin_new_cs(sctx
);