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
,
302 struct r600_resource
*rres
;
303 struct r600_texture
*rtex
;
304 enum radeon_bo_priority priority
;
309 if (resource
->target
!= PIPE_BUFFER
) {
310 struct r600_texture
*tex
= (struct r600_texture
*)resource
;
312 if (tex
->is_depth
&& !r600_can_sample_zs(tex
, is_stencil_sampler
))
313 resource
= &tex
->flushed_depth_texture
->resource
.b
.b
;
316 rres
= (struct r600_resource
*)resource
;
317 priority
= r600_get_sampler_view_priority(rres
);
319 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
320 rres
, usage
, priority
,
323 if (resource
->target
== PIPE_BUFFER
)
326 /* Now add separate DCC if it's present. */
327 rtex
= (struct r600_texture
*)resource
;
328 if (!rtex
->dcc_separate_buffer
)
331 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
332 rtex
->dcc_separate_buffer
, usage
,
333 RADEON_PRIO_DCC
, check_mem
);
336 static void si_sampler_views_begin_new_cs(struct si_context
*sctx
,
337 struct si_sampler_views
*views
)
339 unsigned mask
= views
->enabled_mask
;
341 /* Add buffers to the CS. */
343 int i
= u_bit_scan(&mask
);
344 struct si_sampler_view
*sview
= (struct si_sampler_view
*)views
->views
[i
];
346 si_sampler_view_add_buffer(sctx
, sview
->base
.texture
,
348 sview
->is_stencil_sampler
, false);
352 /* Set texture descriptor fields that can be changed by reallocations.
355 * \param base_level_info information of the level of BASE_ADDRESS
356 * \param base_level the level of BASE_ADDRESS
357 * \param first_level pipe_sampler_view.u.tex.first_level
358 * \param block_width util_format_get_blockwidth()
359 * \param is_stencil select between separate Z & Stencil
360 * \param state descriptor to update
362 void si_set_mutable_tex_desc_fields(struct r600_texture
*tex
,
363 const struct radeon_surf_level
*base_level_info
,
364 unsigned base_level
, unsigned first_level
,
365 unsigned block_width
, bool is_stencil
,
369 unsigned pitch
= base_level_info
->nblk_x
* block_width
;
371 if (tex
->is_depth
&& !r600_can_sample_zs(tex
, is_stencil
)) {
372 tex
= tex
->flushed_depth_texture
;
376 va
= tex
->resource
.gpu_address
+ base_level_info
->offset
;
378 state
[1] &= C_008F14_BASE_ADDRESS_HI
;
379 state
[3] &= C_008F1C_TILING_INDEX
;
380 state
[4] &= C_008F20_PITCH
;
381 state
[6] &= C_008F28_COMPRESSION_EN
;
384 state
[1] |= S_008F14_BASE_ADDRESS_HI(va
>> 40);
385 state
[3] |= S_008F1C_TILING_INDEX(si_tile_mode_index(tex
, base_level
,
387 state
[4] |= S_008F20_PITCH(pitch
- 1);
389 if (tex
->dcc_offset
&& tex
->surface
.level
[first_level
].dcc_enabled
) {
390 state
[6] |= S_008F28_COMPRESSION_EN(1);
391 state
[7] = ((!tex
->dcc_separate_buffer
? tex
->resource
.gpu_address
: 0) +
393 base_level_info
->dcc_offset
) >> 8;
397 static void si_set_sampler_view(struct si_context
*sctx
,
399 unsigned slot
, struct pipe_sampler_view
*view
,
400 bool disallow_early_out
)
402 struct si_sampler_views
*views
= &sctx
->samplers
[shader
].views
;
403 struct si_sampler_view
*rview
= (struct si_sampler_view
*)view
;
404 struct si_descriptors
*descs
= si_sampler_descriptors(sctx
, shader
);
406 if (views
->views
[slot
] == view
&& !disallow_early_out
)
410 struct r600_texture
*rtex
= (struct r600_texture
*)view
->texture
;
411 uint32_t *desc
= descs
->list
+ slot
* 16;
413 si_sampler_view_add_buffer(sctx
, view
->texture
,
415 rview
->is_stencil_sampler
, true);
417 pipe_sampler_view_reference(&views
->views
[slot
], view
);
418 memcpy(desc
, rview
->state
, 8*4);
420 if (view
->texture
&& view
->texture
->target
!= PIPE_BUFFER
) {
421 bool is_separate_stencil
=
422 rtex
->db_compatible
&&
423 rview
->is_stencil_sampler
;
425 si_set_mutable_tex_desc_fields(rtex
,
426 rview
->base_level_info
,
428 rview
->base
.u
.tex
.first_level
,
434 if (view
->texture
&& view
->texture
->target
!= PIPE_BUFFER
&&
437 rview
->fmask_state
, 8*4);
439 /* Disable FMASK and bind sampler state in [12:15]. */
441 null_texture_descriptor
, 4*4);
443 if (views
->sampler_states
[slot
])
445 views
->sampler_states
[slot
], 4*4);
448 views
->enabled_mask
|= 1u << slot
;
450 pipe_sampler_view_reference(&views
->views
[slot
], NULL
);
451 memcpy(descs
->list
+ slot
*16, null_texture_descriptor
, 8*4);
452 /* Only clear the lower dwords of FMASK. */
453 memcpy(descs
->list
+ slot
*16 + 8, null_texture_descriptor
, 4*4);
454 views
->enabled_mask
&= ~(1u << slot
);
457 descs
->dirty_mask
|= 1u << slot
;
458 sctx
->descriptors_dirty
|= 1u << si_sampler_descriptors_idx(shader
);
461 static bool is_compressed_colortex(struct r600_texture
*rtex
)
463 return rtex
->cmask
.size
|| rtex
->fmask
.size
||
464 (rtex
->dcc_offset
&& rtex
->dirty_level_mask
);
467 static void si_set_sampler_views(struct pipe_context
*ctx
,
468 unsigned shader
, unsigned start
,
470 struct pipe_sampler_view
**views
)
472 struct si_context
*sctx
= (struct si_context
*)ctx
;
473 struct si_textures_info
*samplers
= &sctx
->samplers
[shader
];
476 if (!count
|| shader
>= SI_NUM_SHADERS
)
479 for (i
= 0; i
< count
; i
++) {
480 unsigned slot
= start
+ i
;
482 if (!views
|| !views
[i
]) {
483 samplers
->depth_texture_mask
&= ~(1u << slot
);
484 samplers
->compressed_colortex_mask
&= ~(1u << slot
);
485 si_set_sampler_view(sctx
, shader
, slot
, NULL
, false);
489 si_set_sampler_view(sctx
, shader
, slot
, views
[i
], false);
491 if (views
[i
]->texture
&& views
[i
]->texture
->target
!= PIPE_BUFFER
) {
492 struct r600_texture
*rtex
=
493 (struct r600_texture
*)views
[i
]->texture
;
495 if (rtex
->db_compatible
) {
496 samplers
->depth_texture_mask
|= 1u << slot
;
498 samplers
->depth_texture_mask
&= ~(1u << slot
);
500 if (is_compressed_colortex(rtex
)) {
501 samplers
->compressed_colortex_mask
|= 1u << slot
;
503 samplers
->compressed_colortex_mask
&= ~(1u << slot
);
506 if (rtex
->dcc_offset
&&
507 p_atomic_read(&rtex
->framebuffers_bound
))
508 sctx
->need_check_render_feedback
= true;
510 samplers
->depth_texture_mask
&= ~(1u << slot
);
511 samplers
->compressed_colortex_mask
&= ~(1u << slot
);
517 si_samplers_update_compressed_colortex_mask(struct si_textures_info
*samplers
)
519 unsigned mask
= samplers
->views
.enabled_mask
;
522 int i
= u_bit_scan(&mask
);
523 struct pipe_resource
*res
= samplers
->views
.views
[i
]->texture
;
525 if (res
&& res
->target
!= PIPE_BUFFER
) {
526 struct r600_texture
*rtex
= (struct r600_texture
*)res
;
528 if (is_compressed_colortex(rtex
)) {
529 samplers
->compressed_colortex_mask
|= 1u << i
;
531 samplers
->compressed_colortex_mask
&= ~(1u << i
);
540 si_image_descriptors_idx(unsigned shader
)
542 return SI_DESCS_FIRST_SHADER
+ shader
* SI_NUM_SHADER_DESCS
+
543 SI_SHADER_DESCS_IMAGES
;
546 static struct si_descriptors
*
547 si_image_descriptors(struct si_context
*sctx
, unsigned shader
)
549 return &sctx
->descriptors
[si_image_descriptors_idx(shader
)];
553 si_release_image_views(struct si_images_info
*images
)
557 for (i
= 0; i
< SI_NUM_IMAGES
; ++i
) {
558 struct pipe_image_view
*view
= &images
->views
[i
];
560 pipe_resource_reference(&view
->resource
, NULL
);
565 si_image_views_begin_new_cs(struct si_context
*sctx
, struct si_images_info
*images
)
567 uint mask
= images
->enabled_mask
;
569 /* Add buffers to the CS. */
571 int i
= u_bit_scan(&mask
);
572 struct pipe_image_view
*view
= &images
->views
[i
];
574 assert(view
->resource
);
576 si_sampler_view_add_buffer(sctx
, view
->resource
,
577 RADEON_USAGE_READWRITE
, false, false);
582 si_disable_shader_image(struct si_context
*ctx
, unsigned shader
, unsigned slot
)
584 struct si_images_info
*images
= &ctx
->images
[shader
];
586 if (images
->enabled_mask
& (1u << slot
)) {
587 struct si_descriptors
*descs
= si_image_descriptors(ctx
, shader
);
589 pipe_resource_reference(&images
->views
[slot
].resource
, NULL
);
590 images
->compressed_colortex_mask
&= ~(1 << slot
);
592 memcpy(descs
->list
+ slot
*8, null_image_descriptor
, 8*4);
593 images
->enabled_mask
&= ~(1u << slot
);
594 descs
->dirty_mask
|= 1u << slot
;
595 ctx
->descriptors_dirty
|= 1u << si_image_descriptors_idx(shader
);
600 si_mark_image_range_valid(const struct pipe_image_view
*view
)
602 struct r600_resource
*res
= (struct r600_resource
*)view
->resource
;
603 const struct util_format_description
*desc
;
606 assert(res
&& res
->b
.b
.target
== PIPE_BUFFER
);
608 desc
= util_format_description(view
->format
);
609 stride
= desc
->block
.bits
/ 8;
611 util_range_add(&res
->valid_buffer_range
,
612 stride
* (view
->u
.buf
.first_element
),
613 stride
* (view
->u
.buf
.last_element
+ 1));
616 static void si_set_shader_image(struct si_context
*ctx
,
618 unsigned slot
, const struct pipe_image_view
*view
)
620 struct si_screen
*screen
= ctx
->screen
;
621 struct si_images_info
*images
= &ctx
->images
[shader
];
622 struct si_descriptors
*descs
= si_image_descriptors(ctx
, shader
);
623 struct r600_resource
*res
;
625 if (!view
|| !view
->resource
) {
626 si_disable_shader_image(ctx
, shader
, slot
);
630 res
= (struct r600_resource
*)view
->resource
;
632 if (&images
->views
[slot
] != view
)
633 util_copy_image_view(&images
->views
[slot
], view
);
635 si_sampler_view_add_buffer(ctx
, &res
->b
.b
,
636 RADEON_USAGE_READWRITE
, false, true);
638 if (res
->b
.b
.target
== PIPE_BUFFER
) {
639 if (view
->access
& PIPE_IMAGE_ACCESS_WRITE
)
640 si_mark_image_range_valid(view
);
642 si_make_buffer_descriptor(screen
, res
,
644 view
->u
.buf
.first_element
*
645 util_format_get_blocksize(view
->format
),
646 (view
->u
.buf
.last_element
-
647 view
->u
.buf
.first_element
+ 1) *
648 util_format_get_blocksize(view
->format
),
649 descs
->list
+ slot
* 8);
650 images
->compressed_colortex_mask
&= ~(1 << slot
);
652 static const unsigned char swizzle
[4] = { 0, 1, 2, 3 };
653 struct r600_texture
*tex
= (struct r600_texture
*)res
;
654 unsigned level
= view
->u
.tex
.level
;
655 unsigned width
, height
, depth
;
656 uint32_t *desc
= descs
->list
+ slot
* 8;
657 bool uses_dcc
= tex
->dcc_offset
&&
658 tex
->surface
.level
[level
].dcc_enabled
;
660 assert(!tex
->is_depth
);
661 assert(tex
->fmask
.size
== 0);
664 view
->access
& PIPE_IMAGE_ACCESS_WRITE
) {
665 /* If DCC can't be disabled, at least decompress it.
666 * The decompression is relatively cheap if the surface
667 * has been decompressed already.
669 if (r600_texture_disable_dcc(&ctx
->b
, tex
))
672 ctx
->b
.decompress_dcc(&ctx
->b
.b
, tex
);
675 if (is_compressed_colortex(tex
)) {
676 images
->compressed_colortex_mask
|= 1 << slot
;
678 images
->compressed_colortex_mask
&= ~(1 << slot
);
682 p_atomic_read(&tex
->framebuffers_bound
))
683 ctx
->need_check_render_feedback
= true;
685 /* Always force the base level to the selected level.
687 * This is required for 3D textures, where otherwise
688 * selecting a single slice for non-layered bindings
689 * fails. It doesn't hurt the other targets.
691 width
= u_minify(res
->b
.b
.width0
, level
);
692 height
= u_minify(res
->b
.b
.height0
, level
);
693 depth
= u_minify(res
->b
.b
.depth0
, level
);
695 si_make_texture_descriptor(screen
, tex
,
696 false, res
->b
.b
.target
,
697 view
->format
, swizzle
,
699 view
->u
.tex
.first_layer
,
700 view
->u
.tex
.last_layer
,
701 width
, height
, depth
,
703 si_set_mutable_tex_desc_fields(tex
, &tex
->surface
.level
[level
],
705 util_format_get_blockwidth(view
->format
),
709 images
->enabled_mask
|= 1u << slot
;
710 descs
->dirty_mask
|= 1u << slot
;
711 ctx
->descriptors_dirty
|= 1u << si_image_descriptors_idx(shader
);
715 si_set_shader_images(struct pipe_context
*pipe
, unsigned shader
,
716 unsigned start_slot
, unsigned count
,
717 const struct pipe_image_view
*views
)
719 struct si_context
*ctx
= (struct si_context
*)pipe
;
722 assert(shader
< SI_NUM_SHADERS
);
727 assert(start_slot
+ count
<= SI_NUM_IMAGES
);
730 for (i
= 0, slot
= start_slot
; i
< count
; ++i
, ++slot
)
731 si_set_shader_image(ctx
, shader
, slot
, &views
[i
]);
733 for (i
= 0, slot
= start_slot
; i
< count
; ++i
, ++slot
)
734 si_set_shader_image(ctx
, shader
, slot
, NULL
);
739 si_images_update_compressed_colortex_mask(struct si_images_info
*images
)
741 unsigned mask
= images
->enabled_mask
;
744 int i
= u_bit_scan(&mask
);
745 struct pipe_resource
*res
= images
->views
[i
].resource
;
747 if (res
&& res
->target
!= PIPE_BUFFER
) {
748 struct r600_texture
*rtex
= (struct r600_texture
*)res
;
750 if (is_compressed_colortex(rtex
)) {
751 images
->compressed_colortex_mask
|= 1 << i
;
753 images
->compressed_colortex_mask
&= ~(1 << i
);
761 static void si_bind_sampler_states(struct pipe_context
*ctx
, unsigned shader
,
762 unsigned start
, unsigned count
, void **states
)
764 struct si_context
*sctx
= (struct si_context
*)ctx
;
765 struct si_textures_info
*samplers
= &sctx
->samplers
[shader
];
766 struct si_descriptors
*desc
= si_sampler_descriptors(sctx
, shader
);
767 struct si_sampler_state
**sstates
= (struct si_sampler_state
**)states
;
770 if (!count
|| shader
>= SI_NUM_SHADERS
)
773 for (i
= 0; i
< count
; i
++) {
774 unsigned slot
= start
+ i
;
777 sstates
[i
] == samplers
->views
.sampler_states
[slot
])
780 samplers
->views
.sampler_states
[slot
] = sstates
[i
];
782 /* If FMASK is bound, don't overwrite it.
783 * The sampler state will be set after FMASK is unbound.
785 if (samplers
->views
.views
[i
] &&
786 samplers
->views
.views
[i
]->texture
&&
787 samplers
->views
.views
[i
]->texture
->target
!= PIPE_BUFFER
&&
788 ((struct r600_texture
*)samplers
->views
.views
[i
]->texture
)->fmask
.size
)
791 memcpy(desc
->list
+ slot
* 16 + 12, sstates
[i
]->val
, 4*4);
792 desc
->dirty_mask
|= 1u << slot
;
793 sctx
->descriptors_dirty
|= 1u << si_sampler_descriptors_idx(shader
);
797 /* BUFFER RESOURCES */
799 static void si_init_buffer_resources(struct si_buffer_resources
*buffers
,
800 struct si_descriptors
*descs
,
801 unsigned num_buffers
,
802 unsigned shader_userdata_index
,
803 enum radeon_bo_usage shader_usage
,
804 enum radeon_bo_priority priority
,
807 buffers
->shader_usage
= shader_usage
;
808 buffers
->priority
= priority
;
809 buffers
->buffers
= CALLOC(num_buffers
, sizeof(struct pipe_resource
*));
811 si_init_descriptors(descs
, shader_userdata_index
, 4,
812 num_buffers
, NULL
, ce_offset
);
815 static void si_release_buffer_resources(struct si_buffer_resources
*buffers
,
816 struct si_descriptors
*descs
)
820 for (i
= 0; i
< descs
->num_elements
; i
++) {
821 pipe_resource_reference(&buffers
->buffers
[i
], NULL
);
824 FREE(buffers
->buffers
);
827 static void si_buffer_resources_begin_new_cs(struct si_context
*sctx
,
828 struct si_buffer_resources
*buffers
)
830 unsigned mask
= buffers
->enabled_mask
;
832 /* Add buffers to the CS. */
834 int i
= u_bit_scan(&mask
);
836 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
837 (struct r600_resource
*)buffers
->buffers
[i
],
838 buffers
->shader_usage
, buffers
->priority
);
844 static void si_vertex_buffers_begin_new_cs(struct si_context
*sctx
)
846 struct si_descriptors
*desc
= &sctx
->vertex_buffers
;
847 int count
= sctx
->vertex_elements
? sctx
->vertex_elements
->count
: 0;
850 for (i
= 0; i
< count
; i
++) {
851 int vb
= sctx
->vertex_elements
->elements
[i
].vertex_buffer_index
;
853 if (vb
>= ARRAY_SIZE(sctx
->vertex_buffer
))
855 if (!sctx
->vertex_buffer
[vb
].buffer
)
858 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
859 (struct r600_resource
*)sctx
->vertex_buffer
[vb
].buffer
,
860 RADEON_USAGE_READ
, RADEON_PRIO_VERTEX_BUFFER
);
865 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
866 desc
->buffer
, RADEON_USAGE_READ
,
867 RADEON_PRIO_DESCRIPTORS
);
870 bool si_upload_vertex_buffer_descriptors(struct si_context
*sctx
)
872 struct si_descriptors
*desc
= &sctx
->vertex_buffers
;
873 bool bound
[SI_NUM_VERTEX_BUFFERS
] = {};
874 unsigned i
, count
= sctx
->vertex_elements
->count
;
878 if (!sctx
->vertex_buffers_dirty
)
880 if (!count
|| !sctx
->vertex_elements
)
883 /* Vertex buffer descriptors are the only ones which are uploaded
884 * directly through a staging buffer and don't go through
885 * the fine-grained upload path.
887 u_upload_alloc(sctx
->b
.uploader
, 0, count
* 16, 256, &desc
->buffer_offset
,
888 (struct pipe_resource
**)&desc
->buffer
, (void**)&ptr
);
892 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
893 desc
->buffer
, RADEON_USAGE_READ
,
894 RADEON_PRIO_DESCRIPTORS
);
896 assert(count
<= SI_NUM_VERTEX_BUFFERS
);
898 for (i
= 0; i
< count
; i
++) {
899 struct pipe_vertex_element
*ve
= &sctx
->vertex_elements
->elements
[i
];
900 struct pipe_vertex_buffer
*vb
;
901 struct r600_resource
*rbuffer
;
903 uint32_t *desc
= &ptr
[i
*4];
905 if (ve
->vertex_buffer_index
>= ARRAY_SIZE(sctx
->vertex_buffer
)) {
910 vb
= &sctx
->vertex_buffer
[ve
->vertex_buffer_index
];
911 rbuffer
= (struct r600_resource
*)vb
->buffer
;
917 offset
= vb
->buffer_offset
+ ve
->src_offset
;
918 va
= rbuffer
->gpu_address
+ offset
;
920 /* Fill in T# buffer resource description */
922 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
923 S_008F04_STRIDE(vb
->stride
);
925 if (sctx
->b
.chip_class
<= CIK
&& vb
->stride
)
926 /* Round up by rounding down and adding 1 */
927 desc
[2] = (vb
->buffer
->width0
- offset
-
928 sctx
->vertex_elements
->format_size
[i
]) /
931 desc
[2] = vb
->buffer
->width0
- offset
;
933 desc
[3] = sctx
->vertex_elements
->rsrc_word3
[i
];
935 if (!bound
[ve
->vertex_buffer_index
]) {
936 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
937 (struct r600_resource
*)vb
->buffer
,
938 RADEON_USAGE_READ
, RADEON_PRIO_VERTEX_BUFFER
);
939 bound
[ve
->vertex_buffer_index
] = true;
943 /* Don't flush the const cache. It would have a very negative effect
944 * on performance (confirmed by testing). New descriptors are always
945 * uploaded to a fresh new buffer, so I don't think flushing the const
946 * cache is needed. */
947 desc
->pointer_dirty
= true;
948 si_mark_atom_dirty(sctx
, &sctx
->shader_userdata
.atom
);
949 sctx
->vertex_buffers_dirty
= false;
954 /* CONSTANT BUFFERS */
957 si_const_buffer_descriptors_idx(unsigned shader
)
959 return SI_DESCS_FIRST_SHADER
+ shader
* SI_NUM_SHADER_DESCS
+
960 SI_SHADER_DESCS_CONST_BUFFERS
;
963 static struct si_descriptors
*
964 si_const_buffer_descriptors(struct si_context
*sctx
, unsigned shader
)
966 return &sctx
->descriptors
[si_const_buffer_descriptors_idx(shader
)];
969 void si_upload_const_buffer(struct si_context
*sctx
, struct r600_resource
**rbuffer
,
970 const uint8_t *ptr
, unsigned size
, uint32_t *const_offset
)
974 u_upload_alloc(sctx
->b
.uploader
, 0, size
, 256, const_offset
,
975 (struct pipe_resource
**)rbuffer
, &tmp
);
977 util_memcpy_cpu_to_le32(tmp
, ptr
, size
);
980 static void si_set_constant_buffer(struct si_context
*sctx
,
981 struct si_buffer_resources
*buffers
,
982 unsigned descriptors_idx
,
983 uint slot
, const struct pipe_constant_buffer
*input
)
985 struct si_descriptors
*descs
= &sctx
->descriptors
[descriptors_idx
];
986 assert(slot
< descs
->num_elements
);
987 pipe_resource_reference(&buffers
->buffers
[slot
], NULL
);
989 /* CIK cannot unbind a constant buffer (S_BUFFER_LOAD is buggy
990 * with a NULL buffer). We need to use a dummy buffer instead. */
991 if (sctx
->b
.chip_class
== CIK
&&
992 (!input
|| (!input
->buffer
&& !input
->user_buffer
)))
993 input
= &sctx
->null_const_buf
;
995 if (input
&& (input
->buffer
|| input
->user_buffer
)) {
996 struct pipe_resource
*buffer
= NULL
;
999 /* Upload the user buffer if needed. */
1000 if (input
->user_buffer
) {
1001 unsigned buffer_offset
;
1003 si_upload_const_buffer(sctx
,
1004 (struct r600_resource
**)&buffer
, input
->user_buffer
,
1005 input
->buffer_size
, &buffer_offset
);
1007 /* Just unbind on failure. */
1008 si_set_constant_buffer(sctx
, buffers
, descriptors_idx
, slot
, NULL
);
1011 va
= r600_resource(buffer
)->gpu_address
+ buffer_offset
;
1013 pipe_resource_reference(&buffer
, input
->buffer
);
1014 va
= r600_resource(buffer
)->gpu_address
+ input
->buffer_offset
;
1017 /* Set the descriptor. */
1018 uint32_t *desc
= descs
->list
+ slot
*4;
1020 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1022 desc
[2] = input
->buffer_size
;
1023 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1024 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1025 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1026 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1027 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1028 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
1030 buffers
->buffers
[slot
] = buffer
;
1031 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
1032 (struct r600_resource
*)buffer
,
1033 buffers
->shader_usage
,
1034 buffers
->priority
, true);
1035 buffers
->enabled_mask
|= 1u << slot
;
1037 /* Clear the descriptor. */
1038 memset(descs
->list
+ slot
*4, 0, sizeof(uint32_t) * 4);
1039 buffers
->enabled_mask
&= ~(1u << slot
);
1042 descs
->dirty_mask
|= 1u << slot
;
1043 sctx
->descriptors_dirty
|= 1u << descriptors_idx
;
1046 void si_set_rw_buffer(struct si_context
*sctx
,
1047 uint slot
, const struct pipe_constant_buffer
*input
)
1049 si_set_constant_buffer(sctx
, &sctx
->rw_buffers
,
1050 SI_DESCS_RW_BUFFERS
, slot
, input
);
1053 static void si_pipe_set_constant_buffer(struct pipe_context
*ctx
,
1054 uint shader
, uint slot
,
1055 const struct pipe_constant_buffer
*input
)
1057 struct si_context
*sctx
= (struct si_context
*)ctx
;
1059 if (shader
>= SI_NUM_SHADERS
)
1062 si_set_constant_buffer(sctx
, &sctx
->const_buffers
[shader
],
1063 si_const_buffer_descriptors_idx(shader
),
1067 /* SHADER BUFFERS */
1070 si_shader_buffer_descriptors_idx(unsigned shader
)
1072 return SI_DESCS_FIRST_SHADER
+ shader
* SI_NUM_SHADER_DESCS
+
1073 SI_SHADER_DESCS_SHADER_BUFFERS
;
1076 static struct si_descriptors
*
1077 si_shader_buffer_descriptors(struct si_context
*sctx
, unsigned shader
)
1079 return &sctx
->descriptors
[si_shader_buffer_descriptors_idx(shader
)];
1082 static void si_set_shader_buffers(struct pipe_context
*ctx
, unsigned shader
,
1083 unsigned start_slot
, unsigned count
,
1084 const struct pipe_shader_buffer
*sbuffers
)
1086 struct si_context
*sctx
= (struct si_context
*)ctx
;
1087 struct si_buffer_resources
*buffers
= &sctx
->shader_buffers
[shader
];
1088 struct si_descriptors
*descs
= si_shader_buffer_descriptors(sctx
, shader
);
1091 assert(start_slot
+ count
<= SI_NUM_SHADER_BUFFERS
);
1093 for (i
= 0; i
< count
; ++i
) {
1094 const struct pipe_shader_buffer
*sbuffer
= sbuffers
? &sbuffers
[i
] : NULL
;
1095 struct r600_resource
*buf
;
1096 unsigned slot
= start_slot
+ i
;
1097 uint32_t *desc
= descs
->list
+ slot
* 4;
1100 if (!sbuffer
|| !sbuffer
->buffer
) {
1101 pipe_resource_reference(&buffers
->buffers
[slot
], NULL
);
1102 memset(desc
, 0, sizeof(uint32_t) * 4);
1103 buffers
->enabled_mask
&= ~(1u << slot
);
1104 descs
->dirty_mask
|= 1u << slot
;
1105 sctx
->descriptors_dirty
|=
1106 1u << si_shader_buffer_descriptors_idx(shader
);
1110 buf
= (struct r600_resource
*)sbuffer
->buffer
;
1111 va
= buf
->gpu_address
+ sbuffer
->buffer_offset
;
1114 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1116 desc
[2] = sbuffer
->buffer_size
;
1117 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1118 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1119 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1120 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1121 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1122 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
1124 pipe_resource_reference(&buffers
->buffers
[slot
], &buf
->b
.b
);
1125 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
, buf
,
1126 buffers
->shader_usage
,
1127 buffers
->priority
, true);
1128 buffers
->enabled_mask
|= 1u << slot
;
1129 descs
->dirty_mask
|= 1u << slot
;
1130 sctx
->descriptors_dirty
|=
1131 1u << si_shader_buffer_descriptors_idx(shader
);
1137 void si_set_ring_buffer(struct pipe_context
*ctx
, uint slot
,
1138 struct pipe_resource
*buffer
,
1139 unsigned stride
, unsigned num_records
,
1140 bool add_tid
, bool swizzle
,
1141 unsigned element_size
, unsigned index_stride
, uint64_t offset
)
1143 struct si_context
*sctx
= (struct si_context
*)ctx
;
1144 struct si_buffer_resources
*buffers
= &sctx
->rw_buffers
;
1145 struct si_descriptors
*descs
= &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
1147 /* The stride field in the resource descriptor has 14 bits */
1148 assert(stride
< (1 << 14));
1150 assert(slot
< descs
->num_elements
);
1151 pipe_resource_reference(&buffers
->buffers
[slot
], NULL
);
1156 va
= r600_resource(buffer
)->gpu_address
+ offset
;
1158 switch (element_size
) {
1160 assert(!"Unsupported ring buffer element size");
1176 switch (index_stride
) {
1178 assert(!"Unsupported ring buffer index stride");
1194 if (sctx
->b
.chip_class
>= VI
&& stride
)
1195 num_records
*= stride
;
1197 /* Set the descriptor. */
1198 uint32_t *desc
= descs
->list
+ slot
*4;
1200 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
1201 S_008F04_STRIDE(stride
) |
1202 S_008F04_SWIZZLE_ENABLE(swizzle
);
1203 desc
[2] = num_records
;
1204 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1205 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1206 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1207 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1208 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1209 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
) |
1210 S_008F0C_ELEMENT_SIZE(element_size
) |
1211 S_008F0C_INDEX_STRIDE(index_stride
) |
1212 S_008F0C_ADD_TID_ENABLE(add_tid
);
1214 pipe_resource_reference(&buffers
->buffers
[slot
], buffer
);
1215 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
1216 (struct r600_resource
*)buffer
,
1217 buffers
->shader_usage
, buffers
->priority
);
1218 buffers
->enabled_mask
|= 1u << slot
;
1220 /* Clear the descriptor. */
1221 memset(descs
->list
+ slot
*4, 0, sizeof(uint32_t) * 4);
1222 buffers
->enabled_mask
&= ~(1u << slot
);
1225 descs
->dirty_mask
|= 1u << slot
;
1226 sctx
->descriptors_dirty
|= 1u << SI_DESCS_RW_BUFFERS
;
1229 /* STREAMOUT BUFFERS */
1231 static void si_set_streamout_targets(struct pipe_context
*ctx
,
1232 unsigned num_targets
,
1233 struct pipe_stream_output_target
**targets
,
1234 const unsigned *offsets
)
1236 struct si_context
*sctx
= (struct si_context
*)ctx
;
1237 struct si_buffer_resources
*buffers
= &sctx
->rw_buffers
;
1238 struct si_descriptors
*descs
= &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
1239 unsigned old_num_targets
= sctx
->b
.streamout
.num_targets
;
1242 /* We are going to unbind the buffers. Mark which caches need to be flushed. */
1243 if (sctx
->b
.streamout
.num_targets
&& sctx
->b
.streamout
.begin_emitted
) {
1244 /* Since streamout uses vector writes which go through TC L2
1245 * and most other clients can use TC L2 as well, we don't need
1248 * The only cases which requires flushing it is VGT DMA index
1249 * fetching (on <= CIK) and indirect draw data, which are rare
1250 * cases. Thus, flag the TC L2 dirtiness in the resource and
1251 * handle it at draw call time.
1253 for (i
= 0; i
< sctx
->b
.streamout
.num_targets
; i
++)
1254 if (sctx
->b
.streamout
.targets
[i
])
1255 r600_resource(sctx
->b
.streamout
.targets
[i
]->b
.buffer
)->TC_L2_dirty
= true;
1257 /* Invalidate the scalar cache in case a streamout buffer is
1258 * going to be used as a constant buffer.
1260 * Invalidate TC L1, because streamout bypasses it (done by
1261 * setting GLC=1 in the store instruction), but it can contain
1262 * outdated data of streamout buffers.
1264 * VS_PARTIAL_FLUSH is required if the buffers are going to be
1265 * used as an input immediately.
1267 sctx
->b
.flags
|= SI_CONTEXT_INV_SMEM_L1
|
1268 SI_CONTEXT_INV_VMEM_L1
|
1269 SI_CONTEXT_VS_PARTIAL_FLUSH
;
1272 /* All readers of the streamout targets need to be finished before we can
1273 * start writing to the targets.
1276 sctx
->b
.flags
|= SI_CONTEXT_PS_PARTIAL_FLUSH
|
1277 SI_CONTEXT_CS_PARTIAL_FLUSH
;
1279 /* Streamout buffers must be bound in 2 places:
1280 * 1) in VGT by setting the VGT_STRMOUT registers
1281 * 2) as shader resources
1284 /* Set the VGT regs. */
1285 r600_set_streamout_targets(ctx
, num_targets
, targets
, offsets
);
1287 /* Set the shader resources.*/
1288 for (i
= 0; i
< num_targets
; i
++) {
1289 bufidx
= SI_VS_STREAMOUT_BUF0
+ i
;
1292 struct pipe_resource
*buffer
= targets
[i
]->buffer
;
1293 uint64_t va
= r600_resource(buffer
)->gpu_address
;
1295 /* Set the descriptor.
1297 * On VI, the format must be non-INVALID, otherwise
1298 * the buffer will be considered not bound and store
1299 * instructions will be no-ops.
1301 uint32_t *desc
= descs
->list
+ bufidx
*4;
1303 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32);
1304 desc
[2] = 0xffffffff;
1305 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1306 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1307 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1308 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1309 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
1311 /* Set the resource. */
1312 pipe_resource_reference(&buffers
->buffers
[bufidx
],
1314 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
1315 (struct r600_resource
*)buffer
,
1316 buffers
->shader_usage
,
1317 RADEON_PRIO_SHADER_RW_BUFFER
,
1319 buffers
->enabled_mask
|= 1u << bufidx
;
1321 /* Clear the descriptor and unset the resource. */
1322 memset(descs
->list
+ bufidx
*4, 0,
1323 sizeof(uint32_t) * 4);
1324 pipe_resource_reference(&buffers
->buffers
[bufidx
],
1326 buffers
->enabled_mask
&= ~(1u << bufidx
);
1328 descs
->dirty_mask
|= 1u << bufidx
;
1330 for (; i
< old_num_targets
; i
++) {
1331 bufidx
= SI_VS_STREAMOUT_BUF0
+ i
;
1332 /* Clear the descriptor and unset the resource. */
1333 memset(descs
->list
+ bufidx
*4, 0, sizeof(uint32_t) * 4);
1334 pipe_resource_reference(&buffers
->buffers
[bufidx
], NULL
);
1335 buffers
->enabled_mask
&= ~(1u << bufidx
);
1336 descs
->dirty_mask
|= 1u << bufidx
;
1339 sctx
->descriptors_dirty
|= 1u << SI_DESCS_RW_BUFFERS
;
1342 static void si_desc_reset_buffer_offset(struct pipe_context
*ctx
,
1343 uint32_t *desc
, uint64_t old_buf_va
,
1344 struct pipe_resource
*new_buf
)
1346 /* Retrieve the buffer offset from the descriptor. */
1347 uint64_t old_desc_va
=
1348 desc
[0] | ((uint64_t)G_008F04_BASE_ADDRESS_HI(desc
[1]) << 32);
1350 assert(old_buf_va
<= old_desc_va
);
1351 uint64_t offset_within_buffer
= old_desc_va
- old_buf_va
;
1353 /* Update the descriptor. */
1354 uint64_t va
= r600_resource(new_buf
)->gpu_address
+ offset_within_buffer
;
1357 desc
[1] = (desc
[1] & C_008F04_BASE_ADDRESS_HI
) |
1358 S_008F04_BASE_ADDRESS_HI(va
>> 32);
1361 /* INTERNAL CONST BUFFERS */
1363 static void si_set_polygon_stipple(struct pipe_context
*ctx
,
1364 const struct pipe_poly_stipple
*state
)
1366 struct si_context
*sctx
= (struct si_context
*)ctx
;
1367 struct pipe_constant_buffer cb
= {};
1368 unsigned stipple
[32];
1371 for (i
= 0; i
< 32; i
++)
1372 stipple
[i
] = util_bitreverse(state
->stipple
[i
]);
1374 cb
.user_buffer
= stipple
;
1375 cb
.buffer_size
= sizeof(stipple
);
1377 si_set_rw_buffer(sctx
, SI_PS_CONST_POLY_STIPPLE
, &cb
);
1380 /* TEXTURE METADATA ENABLE/DISABLE */
1382 /* CMASK can be enabled (for fast clear) and disabled (for texture export)
1383 * while the texture is bound, possibly by a different context. In that case,
1384 * call this function to update compressed_colortex_masks.
1386 void si_update_compressed_colortex_masks(struct si_context
*sctx
)
1388 for (int i
= 0; i
< SI_NUM_SHADERS
; ++i
) {
1389 si_samplers_update_compressed_colortex_mask(&sctx
->samplers
[i
]);
1390 si_images_update_compressed_colortex_mask(&sctx
->images
[i
]);
1394 /* BUFFER DISCARD/INVALIDATION */
1396 /** Reset descriptors of buffer resources after \p buf has been invalidated. */
1397 static void si_reset_buffer_resources(struct si_context
*sctx
,
1398 struct si_buffer_resources
*buffers
,
1399 unsigned descriptors_idx
,
1400 struct pipe_resource
*buf
,
1403 struct si_descriptors
*descs
= &sctx
->descriptors
[descriptors_idx
];
1404 unsigned mask
= buffers
->enabled_mask
;
1407 unsigned i
= u_bit_scan(&mask
);
1408 if (buffers
->buffers
[i
] == buf
) {
1409 si_desc_reset_buffer_offset(&sctx
->b
.b
,
1412 descs
->dirty_mask
|= 1u << i
;
1413 sctx
->descriptors_dirty
|= 1u << descriptors_idx
;
1415 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
1416 (struct r600_resource
*)buf
,
1417 buffers
->shader_usage
,
1418 buffers
->priority
, true);
1423 /* Reallocate a buffer a update all resource bindings where the buffer is
1426 * This is used to avoid CPU-GPU synchronizations, because it makes the buffer
1427 * idle by discarding its contents. Apps usually tell us when to do this using
1428 * map_buffer flags, for example.
1430 static void si_invalidate_buffer(struct pipe_context
*ctx
, struct pipe_resource
*buf
)
1432 struct si_context
*sctx
= (struct si_context
*)ctx
;
1433 struct r600_resource
*rbuffer
= r600_resource(buf
);
1434 unsigned i
, shader
, alignment
= rbuffer
->buf
->alignment
;
1435 uint64_t old_va
= rbuffer
->gpu_address
;
1436 unsigned num_elems
= sctx
->vertex_elements
?
1437 sctx
->vertex_elements
->count
: 0;
1438 struct si_sampler_view
*view
;
1440 /* Reallocate the buffer in the same pipe_resource. */
1441 r600_init_resource(&sctx
->screen
->b
, rbuffer
, rbuffer
->b
.b
.width0
,
1444 /* We changed the buffer, now we need to bind it where the old one
1445 * was bound. This consists of 2 things:
1446 * 1) Updating the resource descriptor and dirtying it.
1447 * 2) Adding a relocation to the CS, so that it's usable.
1450 /* Vertex buffers. */
1451 for (i
= 0; i
< num_elems
; i
++) {
1452 int vb
= sctx
->vertex_elements
->elements
[i
].vertex_buffer_index
;
1454 if (vb
>= ARRAY_SIZE(sctx
->vertex_buffer
))
1456 if (!sctx
->vertex_buffer
[vb
].buffer
)
1459 if (sctx
->vertex_buffer
[vb
].buffer
== buf
) {
1460 sctx
->vertex_buffers_dirty
= true;
1465 /* Streamout buffers. (other internal buffers can't be invalidated) */
1466 for (i
= SI_VS_STREAMOUT_BUF0
; i
<= SI_VS_STREAMOUT_BUF3
; i
++) {
1467 struct si_buffer_resources
*buffers
= &sctx
->rw_buffers
;
1468 struct si_descriptors
*descs
=
1469 &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
1471 if (buffers
->buffers
[i
] != buf
)
1474 si_desc_reset_buffer_offset(ctx
, descs
->list
+ i
*4,
1476 descs
->dirty_mask
|= 1u << i
;
1477 sctx
->descriptors_dirty
|= 1u << SI_DESCS_RW_BUFFERS
;
1479 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
1480 rbuffer
, buffers
->shader_usage
,
1481 RADEON_PRIO_SHADER_RW_BUFFER
,
1484 /* Update the streamout state. */
1485 if (sctx
->b
.streamout
.begin_emitted
)
1486 r600_emit_streamout_end(&sctx
->b
);
1487 sctx
->b
.streamout
.append_bitmask
=
1488 sctx
->b
.streamout
.enabled_mask
;
1489 r600_streamout_buffers_dirty(&sctx
->b
);
1492 /* Constant and shader buffers. */
1493 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++) {
1494 si_reset_buffer_resources(sctx
, &sctx
->const_buffers
[shader
],
1495 si_const_buffer_descriptors_idx(shader
),
1497 si_reset_buffer_resources(sctx
, &sctx
->shader_buffers
[shader
],
1498 si_shader_buffer_descriptors_idx(shader
),
1502 /* Texture buffers - update virtual addresses in sampler view descriptors. */
1503 LIST_FOR_EACH_ENTRY(view
, &sctx
->b
.texture_buffers
, list
) {
1504 if (view
->base
.texture
== buf
) {
1505 si_desc_reset_buffer_offset(ctx
, &view
->state
[4], old_va
, buf
);
1508 /* Texture buffers - update bindings. */
1509 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++) {
1510 struct si_sampler_views
*views
= &sctx
->samplers
[shader
].views
;
1511 struct si_descriptors
*descs
=
1512 si_sampler_descriptors(sctx
, shader
);
1513 unsigned mask
= views
->enabled_mask
;
1516 unsigned i
= u_bit_scan(&mask
);
1517 if (views
->views
[i
]->texture
== buf
) {
1518 si_desc_reset_buffer_offset(ctx
,
1522 descs
->dirty_mask
|= 1u << i
;
1523 sctx
->descriptors_dirty
|=
1524 1u << si_sampler_descriptors_idx(shader
);
1526 radeon_add_to_buffer_list_check_mem(&sctx
->b
, &sctx
->b
.gfx
,
1527 rbuffer
, RADEON_USAGE_READ
,
1528 RADEON_PRIO_SAMPLER_BUFFER
,
1535 for (shader
= 0; shader
< SI_NUM_SHADERS
; ++shader
) {
1536 struct si_images_info
*images
= &sctx
->images
[shader
];
1537 struct si_descriptors
*descs
=
1538 si_image_descriptors(sctx
, shader
);
1539 unsigned mask
= images
->enabled_mask
;
1542 unsigned i
= u_bit_scan(&mask
);
1544 if (images
->views
[i
].resource
== buf
) {
1545 if (images
->views
[i
].access
& PIPE_IMAGE_ACCESS_WRITE
)
1546 si_mark_image_range_valid(&images
->views
[i
]);
1548 si_desc_reset_buffer_offset(
1549 ctx
, descs
->list
+ i
* 8 + 4,
1551 descs
->dirty_mask
|= 1u << i
;
1552 sctx
->descriptors_dirty
|=
1553 1u << si_image_descriptors_idx(shader
);
1555 radeon_add_to_buffer_list_check_mem(
1556 &sctx
->b
, &sctx
->b
.gfx
, rbuffer
,
1557 RADEON_USAGE_READWRITE
,
1558 RADEON_PRIO_SAMPLER_BUFFER
, true);
1564 /* Update mutable image descriptor fields of all bound textures. */
1565 void si_update_all_texture_descriptors(struct si_context
*sctx
)
1569 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++) {
1570 struct si_sampler_views
*samplers
= &sctx
->samplers
[shader
].views
;
1571 struct si_images_info
*images
= &sctx
->images
[shader
];
1575 mask
= images
->enabled_mask
;
1577 unsigned i
= u_bit_scan(&mask
);
1578 struct pipe_image_view
*view
= &images
->views
[i
];
1580 if (!view
->resource
||
1581 view
->resource
->target
== PIPE_BUFFER
)
1584 si_set_shader_image(sctx
, shader
, i
, view
);
1587 /* Sampler views. */
1588 mask
= samplers
->enabled_mask
;
1590 unsigned i
= u_bit_scan(&mask
);
1591 struct pipe_sampler_view
*view
= samplers
->views
[i
];
1595 view
->texture
->target
== PIPE_BUFFER
)
1598 si_set_sampler_view(sctx
, shader
, i
,
1599 samplers
->views
[i
], true);
1604 /* SHADER USER DATA */
1606 static void si_mark_shader_pointers_dirty(struct si_context
*sctx
,
1609 struct si_descriptors
*descs
=
1610 &sctx
->descriptors
[SI_DESCS_FIRST_SHADER
+ shader
* SI_NUM_SHADER_DESCS
];
1612 for (unsigned i
= 0; i
< SI_NUM_SHADER_DESCS
; ++i
, ++descs
)
1613 descs
->pointer_dirty
= true;
1615 if (shader
== PIPE_SHADER_VERTEX
)
1616 sctx
->vertex_buffers
.pointer_dirty
= true;
1618 si_mark_atom_dirty(sctx
, &sctx
->shader_userdata
.atom
);
1621 static void si_shader_userdata_begin_new_cs(struct si_context
*sctx
)
1625 for (i
= 0; i
< SI_NUM_SHADERS
; i
++) {
1626 si_mark_shader_pointers_dirty(sctx
, i
);
1628 sctx
->descriptors
[SI_DESCS_RW_BUFFERS
].pointer_dirty
= true;
1631 /* Set a base register address for user data constants in the given shader.
1632 * This assigns a mapping from PIPE_SHADER_* to SPI_SHADER_USER_DATA_*.
1634 static void si_set_user_data_base(struct si_context
*sctx
,
1635 unsigned shader
, uint32_t new_base
)
1637 uint32_t *base
= &sctx
->shader_userdata
.sh_base
[shader
];
1639 if (*base
!= new_base
) {
1643 si_mark_shader_pointers_dirty(sctx
, shader
);
1647 /* This must be called when these shaders are changed from non-NULL to NULL
1650 * - tessellation control shader
1651 * - tessellation evaluation shader
1653 void si_shader_change_notify(struct si_context
*sctx
)
1655 /* VS can be bound as VS, ES, or LS. */
1656 if (sctx
->tes_shader
.cso
)
1657 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
1658 R_00B530_SPI_SHADER_USER_DATA_LS_0
);
1659 else if (sctx
->gs_shader
.cso
)
1660 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
1661 R_00B330_SPI_SHADER_USER_DATA_ES_0
);
1663 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
,
1664 R_00B130_SPI_SHADER_USER_DATA_VS_0
);
1666 /* TES can be bound as ES, VS, or not bound. */
1667 if (sctx
->tes_shader
.cso
) {
1668 if (sctx
->gs_shader
.cso
)
1669 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_EVAL
,
1670 R_00B330_SPI_SHADER_USER_DATA_ES_0
);
1672 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_EVAL
,
1673 R_00B130_SPI_SHADER_USER_DATA_VS_0
);
1675 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_EVAL
, 0);
1679 static void si_emit_shader_pointer(struct si_context
*sctx
,
1680 struct si_descriptors
*desc
,
1681 unsigned sh_base
, bool keep_dirty
)
1683 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
1686 if (!desc
->pointer_dirty
|| !desc
->buffer
)
1689 va
= desc
->buffer
->gpu_address
+
1690 desc
->buffer_offset
;
1692 radeon_emit(cs
, PKT3(PKT3_SET_SH_REG
, 2, 0));
1693 radeon_emit(cs
, (sh_base
+ desc
->shader_userdata_offset
- SI_SH_REG_OFFSET
) >> 2);
1694 radeon_emit(cs
, va
);
1695 radeon_emit(cs
, va
>> 32);
1697 desc
->pointer_dirty
= keep_dirty
;
1700 void si_emit_graphics_shader_userdata(struct si_context
*sctx
,
1701 struct r600_atom
*atom
)
1704 uint32_t *sh_base
= sctx
->shader_userdata
.sh_base
;
1705 struct si_descriptors
*descs
;
1707 descs
= &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
];
1709 if (descs
->pointer_dirty
) {
1710 si_emit_shader_pointer(sctx
, descs
,
1711 R_00B030_SPI_SHADER_USER_DATA_PS_0
, true);
1712 si_emit_shader_pointer(sctx
, descs
,
1713 R_00B130_SPI_SHADER_USER_DATA_VS_0
, true);
1714 si_emit_shader_pointer(sctx
, descs
,
1715 R_00B230_SPI_SHADER_USER_DATA_GS_0
, true);
1716 si_emit_shader_pointer(sctx
, descs
,
1717 R_00B330_SPI_SHADER_USER_DATA_ES_0
, true);
1718 si_emit_shader_pointer(sctx
, descs
,
1719 R_00B430_SPI_SHADER_USER_DATA_HS_0
, true);
1720 descs
->pointer_dirty
= false;
1723 descs
= &sctx
->descriptors
[SI_DESCS_FIRST_SHADER
];
1725 for (shader
= 0; shader
< SI_NUM_GRAPHICS_SHADERS
; shader
++) {
1726 unsigned base
= sh_base
[shader
];
1732 for (i
= 0; i
< SI_NUM_SHADER_DESCS
; i
++, descs
++)
1733 si_emit_shader_pointer(sctx
, descs
, base
, false);
1735 si_emit_shader_pointer(sctx
, &sctx
->vertex_buffers
, sh_base
[PIPE_SHADER_VERTEX
], false);
1738 void si_emit_compute_shader_userdata(struct si_context
*sctx
)
1740 unsigned base
= R_00B900_COMPUTE_USER_DATA_0
;
1741 struct si_descriptors
*descs
= &sctx
->descriptors
[SI_DESCS_FIRST_COMPUTE
];
1743 for (unsigned i
= 0; i
< SI_NUM_SHADER_DESCS
; ++i
, ++descs
)
1744 si_emit_shader_pointer(sctx
, descs
, base
, false);
1747 /* INIT/DEINIT/UPLOAD */
1749 void si_init_all_descriptors(struct si_context
*sctx
)
1752 unsigned ce_offset
= 0;
1754 for (i
= 0; i
< SI_NUM_SHADERS
; i
++) {
1755 si_init_buffer_resources(&sctx
->const_buffers
[i
],
1756 si_const_buffer_descriptors(sctx
, i
),
1757 SI_NUM_CONST_BUFFERS
, SI_SGPR_CONST_BUFFERS
,
1758 RADEON_USAGE_READ
, RADEON_PRIO_CONST_BUFFER
,
1760 si_init_buffer_resources(&sctx
->shader_buffers
[i
],
1761 si_shader_buffer_descriptors(sctx
, i
),
1762 SI_NUM_SHADER_BUFFERS
, SI_SGPR_SHADER_BUFFERS
,
1763 RADEON_USAGE_READWRITE
, RADEON_PRIO_SHADER_RW_BUFFER
,
1766 si_init_descriptors(si_sampler_descriptors(sctx
, i
),
1767 SI_SGPR_SAMPLERS
, 16, SI_NUM_SAMPLERS
,
1768 null_texture_descriptor
, &ce_offset
);
1770 si_init_descriptors(si_image_descriptors(sctx
, i
),
1771 SI_SGPR_IMAGES
, 8, SI_NUM_IMAGES
,
1772 null_image_descriptor
, &ce_offset
);
1775 si_init_buffer_resources(&sctx
->rw_buffers
,
1776 &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
],
1777 SI_NUM_RW_BUFFERS
, SI_SGPR_RW_BUFFERS
,
1778 RADEON_USAGE_READWRITE
, RADEON_PRIO_SHADER_RINGS
,
1780 si_init_descriptors(&sctx
->vertex_buffers
, SI_SGPR_VERTEX_BUFFERS
,
1781 4, SI_NUM_VERTEX_BUFFERS
, NULL
, NULL
);
1783 sctx
->descriptors_dirty
= u_bit_consecutive(0, SI_NUM_DESCS
);
1785 assert(ce_offset
<= 32768);
1787 /* Set pipe_context functions. */
1788 sctx
->b
.b
.bind_sampler_states
= si_bind_sampler_states
;
1789 sctx
->b
.b
.set_shader_images
= si_set_shader_images
;
1790 sctx
->b
.b
.set_constant_buffer
= si_pipe_set_constant_buffer
;
1791 sctx
->b
.b
.set_polygon_stipple
= si_set_polygon_stipple
;
1792 sctx
->b
.b
.set_shader_buffers
= si_set_shader_buffers
;
1793 sctx
->b
.b
.set_sampler_views
= si_set_sampler_views
;
1794 sctx
->b
.b
.set_stream_output_targets
= si_set_streamout_targets
;
1795 sctx
->b
.invalidate_buffer
= si_invalidate_buffer
;
1797 /* Shader user data. */
1798 si_init_atom(sctx
, &sctx
->shader_userdata
.atom
, &sctx
->atoms
.s
.shader_userdata
,
1799 si_emit_graphics_shader_userdata
);
1801 /* Set default and immutable mappings. */
1802 si_set_user_data_base(sctx
, PIPE_SHADER_VERTEX
, R_00B130_SPI_SHADER_USER_DATA_VS_0
);
1803 si_set_user_data_base(sctx
, PIPE_SHADER_TESS_CTRL
, R_00B430_SPI_SHADER_USER_DATA_HS_0
);
1804 si_set_user_data_base(sctx
, PIPE_SHADER_GEOMETRY
, R_00B230_SPI_SHADER_USER_DATA_GS_0
);
1805 si_set_user_data_base(sctx
, PIPE_SHADER_FRAGMENT
, R_00B030_SPI_SHADER_USER_DATA_PS_0
);
1808 bool si_upload_graphics_shader_descriptors(struct si_context
*sctx
)
1810 const unsigned mask
= u_bit_consecutive(0, SI_DESCS_FIRST_COMPUTE
);
1811 unsigned dirty
= sctx
->descriptors_dirty
& mask
;
1814 unsigned i
= u_bit_scan(&dirty
);
1816 if (!si_upload_descriptors(sctx
, &sctx
->descriptors
[i
],
1817 &sctx
->shader_userdata
.atom
))
1821 sctx
->descriptors_dirty
&= ~mask
;
1825 bool si_upload_compute_shader_descriptors(struct si_context
*sctx
)
1827 /* Does not update rw_buffers as that is not needed for compute shaders
1828 * and the input buffer is using the same SGPR's anyway.
1830 const unsigned mask
= u_bit_consecutive(SI_DESCS_FIRST_COMPUTE
,
1831 SI_NUM_DESCS
- SI_DESCS_FIRST_COMPUTE
);
1832 unsigned dirty
= sctx
->descriptors_dirty
& mask
;
1835 unsigned i
= u_bit_scan(&dirty
);
1837 if (!si_upload_descriptors(sctx
, &sctx
->descriptors
[i
], NULL
))
1841 sctx
->descriptors_dirty
&= ~mask
;
1846 void si_release_all_descriptors(struct si_context
*sctx
)
1850 for (i
= 0; i
< SI_NUM_SHADERS
; i
++) {
1851 si_release_buffer_resources(&sctx
->const_buffers
[i
],
1852 si_const_buffer_descriptors(sctx
, i
));
1853 si_release_buffer_resources(&sctx
->shader_buffers
[i
],
1854 si_shader_buffer_descriptors(sctx
, i
));
1855 si_release_sampler_views(&sctx
->samplers
[i
].views
);
1856 si_release_image_views(&sctx
->images
[i
]);
1858 si_release_buffer_resources(&sctx
->rw_buffers
,
1859 &sctx
->descriptors
[SI_DESCS_RW_BUFFERS
]);
1861 for (i
= 0; i
< SI_NUM_DESCS
; ++i
)
1862 si_release_descriptors(&sctx
->descriptors
[i
]);
1863 si_release_descriptors(&sctx
->vertex_buffers
);
1866 void si_all_descriptors_begin_new_cs(struct si_context
*sctx
)
1870 for (i
= 0; i
< SI_NUM_SHADERS
; i
++) {
1871 si_buffer_resources_begin_new_cs(sctx
, &sctx
->const_buffers
[i
]);
1872 si_buffer_resources_begin_new_cs(sctx
, &sctx
->shader_buffers
[i
]);
1873 si_sampler_views_begin_new_cs(sctx
, &sctx
->samplers
[i
].views
);
1874 si_image_views_begin_new_cs(sctx
, &sctx
->images
[i
]);
1876 si_buffer_resources_begin_new_cs(sctx
, &sctx
->rw_buffers
);
1877 si_vertex_buffers_begin_new_cs(sctx
);
1879 for (i
= 0; i
< SI_NUM_DESCS
; ++i
)
1880 si_descriptors_begin_new_cs(sctx
, &sctx
->descriptors
[i
]);
1882 si_shader_userdata_begin_new_cs(sctx
);