2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 #include "radv_private.h"
29 #include "radv_radeon_winsys.h"
30 #include "radv_shader.h"
33 #include "vk_format.h"
35 #include "radv_debug.h"
36 #include "radv_meta.h"
41 RADV_PREFETCH_VBO_DESCRIPTORS
= (1 << 0),
42 RADV_PREFETCH_VS
= (1 << 1),
43 RADV_PREFETCH_TCS
= (1 << 2),
44 RADV_PREFETCH_TES
= (1 << 3),
45 RADV_PREFETCH_GS
= (1 << 4),
46 RADV_PREFETCH_PS
= (1 << 5),
47 RADV_PREFETCH_SHADERS
= (RADV_PREFETCH_VS
|
54 static void radv_handle_image_transition(struct radv_cmd_buffer
*cmd_buffer
,
55 struct radv_image
*image
,
56 VkImageLayout src_layout
,
58 VkImageLayout dst_layout
,
62 const VkImageSubresourceRange
*range
,
63 struct radv_sample_locations_state
*sample_locs
);
65 const struct radv_dynamic_state default_dynamic_state
= {
78 .blend_constants
= { 0.0f
, 0.0f
, 0.0f
, 0.0f
},
83 .stencil_compare_mask
= {
87 .stencil_write_mask
= {
91 .stencil_reference
= {
98 radv_bind_dynamic_state(struct radv_cmd_buffer
*cmd_buffer
,
99 const struct radv_dynamic_state
*src
)
101 struct radv_dynamic_state
*dest
= &cmd_buffer
->state
.dynamic
;
102 uint32_t copy_mask
= src
->mask
;
103 uint32_t dest_mask
= 0;
105 /* Make sure to copy the number of viewports/scissors because they can
106 * only be specified at pipeline creation time.
108 dest
->viewport
.count
= src
->viewport
.count
;
109 dest
->scissor
.count
= src
->scissor
.count
;
110 dest
->discard_rectangle
.count
= src
->discard_rectangle
.count
;
111 dest
->sample_location
.count
= src
->sample_location
.count
;
113 if (copy_mask
& RADV_DYNAMIC_VIEWPORT
) {
114 if (memcmp(&dest
->viewport
.viewports
, &src
->viewport
.viewports
,
115 src
->viewport
.count
* sizeof(VkViewport
))) {
116 typed_memcpy(dest
->viewport
.viewports
,
117 src
->viewport
.viewports
,
118 src
->viewport
.count
);
119 dest_mask
|= RADV_DYNAMIC_VIEWPORT
;
123 if (copy_mask
& RADV_DYNAMIC_SCISSOR
) {
124 if (memcmp(&dest
->scissor
.scissors
, &src
->scissor
.scissors
,
125 src
->scissor
.count
* sizeof(VkRect2D
))) {
126 typed_memcpy(dest
->scissor
.scissors
,
127 src
->scissor
.scissors
, src
->scissor
.count
);
128 dest_mask
|= RADV_DYNAMIC_SCISSOR
;
132 if (copy_mask
& RADV_DYNAMIC_LINE_WIDTH
) {
133 if (dest
->line_width
!= src
->line_width
) {
134 dest
->line_width
= src
->line_width
;
135 dest_mask
|= RADV_DYNAMIC_LINE_WIDTH
;
139 if (copy_mask
& RADV_DYNAMIC_DEPTH_BIAS
) {
140 if (memcmp(&dest
->depth_bias
, &src
->depth_bias
,
141 sizeof(src
->depth_bias
))) {
142 dest
->depth_bias
= src
->depth_bias
;
143 dest_mask
|= RADV_DYNAMIC_DEPTH_BIAS
;
147 if (copy_mask
& RADV_DYNAMIC_BLEND_CONSTANTS
) {
148 if (memcmp(&dest
->blend_constants
, &src
->blend_constants
,
149 sizeof(src
->blend_constants
))) {
150 typed_memcpy(dest
->blend_constants
,
151 src
->blend_constants
, 4);
152 dest_mask
|= RADV_DYNAMIC_BLEND_CONSTANTS
;
156 if (copy_mask
& RADV_DYNAMIC_DEPTH_BOUNDS
) {
157 if (memcmp(&dest
->depth_bounds
, &src
->depth_bounds
,
158 sizeof(src
->depth_bounds
))) {
159 dest
->depth_bounds
= src
->depth_bounds
;
160 dest_mask
|= RADV_DYNAMIC_DEPTH_BOUNDS
;
164 if (copy_mask
& RADV_DYNAMIC_STENCIL_COMPARE_MASK
) {
165 if (memcmp(&dest
->stencil_compare_mask
,
166 &src
->stencil_compare_mask
,
167 sizeof(src
->stencil_compare_mask
))) {
168 dest
->stencil_compare_mask
= src
->stencil_compare_mask
;
169 dest_mask
|= RADV_DYNAMIC_STENCIL_COMPARE_MASK
;
173 if (copy_mask
& RADV_DYNAMIC_STENCIL_WRITE_MASK
) {
174 if (memcmp(&dest
->stencil_write_mask
, &src
->stencil_write_mask
,
175 sizeof(src
->stencil_write_mask
))) {
176 dest
->stencil_write_mask
= src
->stencil_write_mask
;
177 dest_mask
|= RADV_DYNAMIC_STENCIL_WRITE_MASK
;
181 if (copy_mask
& RADV_DYNAMIC_STENCIL_REFERENCE
) {
182 if (memcmp(&dest
->stencil_reference
, &src
->stencil_reference
,
183 sizeof(src
->stencil_reference
))) {
184 dest
->stencil_reference
= src
->stencil_reference
;
185 dest_mask
|= RADV_DYNAMIC_STENCIL_REFERENCE
;
189 if (copy_mask
& RADV_DYNAMIC_DISCARD_RECTANGLE
) {
190 if (memcmp(&dest
->discard_rectangle
.rectangles
, &src
->discard_rectangle
.rectangles
,
191 src
->discard_rectangle
.count
* sizeof(VkRect2D
))) {
192 typed_memcpy(dest
->discard_rectangle
.rectangles
,
193 src
->discard_rectangle
.rectangles
,
194 src
->discard_rectangle
.count
);
195 dest_mask
|= RADV_DYNAMIC_DISCARD_RECTANGLE
;
199 if (copy_mask
& RADV_DYNAMIC_SAMPLE_LOCATIONS
) {
200 if (dest
->sample_location
.per_pixel
!= src
->sample_location
.per_pixel
||
201 dest
->sample_location
.grid_size
.width
!= src
->sample_location
.grid_size
.width
||
202 dest
->sample_location
.grid_size
.height
!= src
->sample_location
.grid_size
.height
||
203 memcmp(&dest
->sample_location
.locations
,
204 &src
->sample_location
.locations
,
205 src
->sample_location
.count
* sizeof(VkSampleLocationEXT
))) {
206 dest
->sample_location
.per_pixel
= src
->sample_location
.per_pixel
;
207 dest
->sample_location
.grid_size
= src
->sample_location
.grid_size
;
208 typed_memcpy(dest
->sample_location
.locations
,
209 src
->sample_location
.locations
,
210 src
->sample_location
.count
);
211 dest_mask
|= RADV_DYNAMIC_SAMPLE_LOCATIONS
;
215 cmd_buffer
->state
.dirty
|= dest_mask
;
219 radv_bind_streamout_state(struct radv_cmd_buffer
*cmd_buffer
,
220 struct radv_pipeline
*pipeline
)
222 struct radv_streamout_state
*so
= &cmd_buffer
->state
.streamout
;
223 struct radv_shader_info
*info
;
225 if (!pipeline
->streamout_shader
||
226 cmd_buffer
->device
->physical_device
->use_ngg_streamout
)
229 info
= &pipeline
->streamout_shader
->info
;
230 for (int i
= 0; i
< MAX_SO_BUFFERS
; i
++)
231 so
->stride_in_dw
[i
] = info
->so
.strides
[i
];
233 so
->enabled_stream_buffers_mask
= info
->so
.enabled_stream_buffers_mask
;
236 bool radv_cmd_buffer_uses_mec(struct radv_cmd_buffer
*cmd_buffer
)
238 return cmd_buffer
->queue_family_index
== RADV_QUEUE_COMPUTE
&&
239 cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX7
;
242 enum ring_type
radv_queue_family_to_ring(int f
) {
244 case RADV_QUEUE_GENERAL
:
246 case RADV_QUEUE_COMPUTE
:
248 case RADV_QUEUE_TRANSFER
:
251 unreachable("Unknown queue family");
255 static VkResult
radv_create_cmd_buffer(
256 struct radv_device
* device
,
257 struct radv_cmd_pool
* pool
,
258 VkCommandBufferLevel level
,
259 VkCommandBuffer
* pCommandBuffer
)
261 struct radv_cmd_buffer
*cmd_buffer
;
263 cmd_buffer
= vk_zalloc(&pool
->alloc
, sizeof(*cmd_buffer
), 8,
264 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
265 if (cmd_buffer
== NULL
)
266 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
268 cmd_buffer
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
269 cmd_buffer
->device
= device
;
270 cmd_buffer
->pool
= pool
;
271 cmd_buffer
->level
= level
;
274 list_addtail(&cmd_buffer
->pool_link
, &pool
->cmd_buffers
);
275 cmd_buffer
->queue_family_index
= pool
->queue_family_index
;
278 /* Init the pool_link so we can safely call list_del when we destroy
281 list_inithead(&cmd_buffer
->pool_link
);
282 cmd_buffer
->queue_family_index
= RADV_QUEUE_GENERAL
;
285 ring
= radv_queue_family_to_ring(cmd_buffer
->queue_family_index
);
287 cmd_buffer
->cs
= device
->ws
->cs_create(device
->ws
, ring
);
288 if (!cmd_buffer
->cs
) {
289 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
);
290 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
293 *pCommandBuffer
= radv_cmd_buffer_to_handle(cmd_buffer
);
295 list_inithead(&cmd_buffer
->upload
.list
);
301 radv_cmd_buffer_destroy(struct radv_cmd_buffer
*cmd_buffer
)
303 list_del(&cmd_buffer
->pool_link
);
305 list_for_each_entry_safe(struct radv_cmd_buffer_upload
, up
,
306 &cmd_buffer
->upload
.list
, list
) {
307 cmd_buffer
->device
->ws
->buffer_destroy(up
->upload_bo
);
312 if (cmd_buffer
->upload
.upload_bo
)
313 cmd_buffer
->device
->ws
->buffer_destroy(cmd_buffer
->upload
.upload_bo
);
314 cmd_buffer
->device
->ws
->cs_destroy(cmd_buffer
->cs
);
316 for (unsigned i
= 0; i
< VK_PIPELINE_BIND_POINT_RANGE_SIZE
; i
++)
317 free(cmd_buffer
->descriptors
[i
].push_set
.set
.mapped_ptr
);
319 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
);
323 radv_reset_cmd_buffer(struct radv_cmd_buffer
*cmd_buffer
)
325 cmd_buffer
->device
->ws
->cs_reset(cmd_buffer
->cs
);
327 list_for_each_entry_safe(struct radv_cmd_buffer_upload
, up
,
328 &cmd_buffer
->upload
.list
, list
) {
329 cmd_buffer
->device
->ws
->buffer_destroy(up
->upload_bo
);
334 cmd_buffer
->push_constant_stages
= 0;
335 cmd_buffer
->scratch_size_per_wave_needed
= 0;
336 cmd_buffer
->scratch_waves_wanted
= 0;
337 cmd_buffer
->compute_scratch_size_per_wave_needed
= 0;
338 cmd_buffer
->compute_scratch_waves_wanted
= 0;
339 cmd_buffer
->esgs_ring_size_needed
= 0;
340 cmd_buffer
->gsvs_ring_size_needed
= 0;
341 cmd_buffer
->tess_rings_needed
= false;
342 cmd_buffer
->gds_needed
= false;
343 cmd_buffer
->gds_oa_needed
= false;
344 cmd_buffer
->sample_positions_needed
= false;
346 if (cmd_buffer
->upload
.upload_bo
)
347 radv_cs_add_buffer(cmd_buffer
->device
->ws
, cmd_buffer
->cs
,
348 cmd_buffer
->upload
.upload_bo
);
349 cmd_buffer
->upload
.offset
= 0;
351 cmd_buffer
->record_result
= VK_SUCCESS
;
353 memset(cmd_buffer
->vertex_bindings
, 0, sizeof(cmd_buffer
->vertex_bindings
));
355 for (unsigned i
= 0; i
< VK_PIPELINE_BIND_POINT_RANGE_SIZE
; i
++) {
356 cmd_buffer
->descriptors
[i
].dirty
= 0;
357 cmd_buffer
->descriptors
[i
].valid
= 0;
358 cmd_buffer
->descriptors
[i
].push_dirty
= false;
361 if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX9
&&
362 cmd_buffer
->queue_family_index
== RADV_QUEUE_GENERAL
) {
363 unsigned num_db
= cmd_buffer
->device
->physical_device
->rad_info
.num_render_backends
;
364 unsigned fence_offset
, eop_bug_offset
;
367 radv_cmd_buffer_upload_alloc(cmd_buffer
, 8, 8, &fence_offset
,
370 cmd_buffer
->gfx9_fence_va
=
371 radv_buffer_get_va(cmd_buffer
->upload
.upload_bo
);
372 cmd_buffer
->gfx9_fence_va
+= fence_offset
;
374 if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
== GFX9
) {
375 /* Allocate a buffer for the EOP bug on GFX9. */
376 radv_cmd_buffer_upload_alloc(cmd_buffer
, 16 * num_db
, 8,
377 &eop_bug_offset
, &fence_ptr
);
378 cmd_buffer
->gfx9_eop_bug_va
=
379 radv_buffer_get_va(cmd_buffer
->upload
.upload_bo
);
380 cmd_buffer
->gfx9_eop_bug_va
+= eop_bug_offset
;
384 cmd_buffer
->status
= RADV_CMD_BUFFER_STATUS_INITIAL
;
386 return cmd_buffer
->record_result
;
390 radv_cmd_buffer_resize_upload_buf(struct radv_cmd_buffer
*cmd_buffer
,
394 struct radeon_winsys_bo
*bo
;
395 struct radv_cmd_buffer_upload
*upload
;
396 struct radv_device
*device
= cmd_buffer
->device
;
398 new_size
= MAX2(min_needed
, 16 * 1024);
399 new_size
= MAX2(new_size
, 2 * cmd_buffer
->upload
.size
);
401 bo
= device
->ws
->buffer_create(device
->ws
,
404 RADEON_FLAG_CPU_ACCESS
|
405 RADEON_FLAG_NO_INTERPROCESS_SHARING
|
407 RADV_BO_PRIORITY_UPLOAD_BUFFER
);
410 cmd_buffer
->record_result
= VK_ERROR_OUT_OF_DEVICE_MEMORY
;
414 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
, bo
);
415 if (cmd_buffer
->upload
.upload_bo
) {
416 upload
= malloc(sizeof(*upload
));
419 cmd_buffer
->record_result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
420 device
->ws
->buffer_destroy(bo
);
424 memcpy(upload
, &cmd_buffer
->upload
, sizeof(*upload
));
425 list_add(&upload
->list
, &cmd_buffer
->upload
.list
);
428 cmd_buffer
->upload
.upload_bo
= bo
;
429 cmd_buffer
->upload
.size
= new_size
;
430 cmd_buffer
->upload
.offset
= 0;
431 cmd_buffer
->upload
.map
= device
->ws
->buffer_map(cmd_buffer
->upload
.upload_bo
);
433 if (!cmd_buffer
->upload
.map
) {
434 cmd_buffer
->record_result
= VK_ERROR_OUT_OF_DEVICE_MEMORY
;
442 radv_cmd_buffer_upload_alloc(struct radv_cmd_buffer
*cmd_buffer
,
445 unsigned *out_offset
,
448 assert(util_is_power_of_two_nonzero(alignment
));
450 uint64_t offset
= align(cmd_buffer
->upload
.offset
, alignment
);
451 if (offset
+ size
> cmd_buffer
->upload
.size
) {
452 if (!radv_cmd_buffer_resize_upload_buf(cmd_buffer
, size
))
457 *out_offset
= offset
;
458 *ptr
= cmd_buffer
->upload
.map
+ offset
;
460 cmd_buffer
->upload
.offset
= offset
+ size
;
465 radv_cmd_buffer_upload_data(struct radv_cmd_buffer
*cmd_buffer
,
466 unsigned size
, unsigned alignment
,
467 const void *data
, unsigned *out_offset
)
471 if (!radv_cmd_buffer_upload_alloc(cmd_buffer
, size
, alignment
,
472 out_offset
, (void **)&ptr
))
476 memcpy(ptr
, data
, size
);
482 radv_emit_write_data_packet(struct radv_cmd_buffer
*cmd_buffer
, uint64_t va
,
483 unsigned count
, const uint32_t *data
)
485 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
487 radeon_check_space(cmd_buffer
->device
->ws
, cs
, 4 + count
);
489 radeon_emit(cs
, PKT3(PKT3_WRITE_DATA
, 2 + count
, 0));
490 radeon_emit(cs
, S_370_DST_SEL(V_370_MEM
) |
491 S_370_WR_CONFIRM(1) |
492 S_370_ENGINE_SEL(V_370_ME
));
494 radeon_emit(cs
, va
>> 32);
495 radeon_emit_array(cs
, data
, count
);
498 void radv_cmd_buffer_trace_emit(struct radv_cmd_buffer
*cmd_buffer
)
500 struct radv_device
*device
= cmd_buffer
->device
;
501 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
504 va
= radv_buffer_get_va(device
->trace_bo
);
505 if (cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
)
508 ++cmd_buffer
->state
.trace_id
;
509 radv_emit_write_data_packet(cmd_buffer
, va
, 1,
510 &cmd_buffer
->state
.trace_id
);
512 radeon_check_space(cmd_buffer
->device
->ws
, cs
, 2);
514 radeon_emit(cs
, PKT3(PKT3_NOP
, 0, 0));
515 radeon_emit(cs
, AC_ENCODE_TRACE_POINT(cmd_buffer
->state
.trace_id
));
519 radv_cmd_buffer_after_draw(struct radv_cmd_buffer
*cmd_buffer
,
520 enum radv_cmd_flush_bits flags
)
522 if (cmd_buffer
->device
->instance
->debug_flags
& RADV_DEBUG_SYNC_SHADERS
) {
523 assert(flags
& (RADV_CMD_FLAG_PS_PARTIAL_FLUSH
|
524 RADV_CMD_FLAG_CS_PARTIAL_FLUSH
));
526 radeon_check_space(cmd_buffer
->device
->ws
, cmd_buffer
->cs
, 4);
528 /* Force wait for graphics or compute engines to be idle. */
529 si_cs_emit_cache_flush(cmd_buffer
->cs
,
530 cmd_buffer
->device
->physical_device
->rad_info
.chip_class
,
531 &cmd_buffer
->gfx9_fence_idx
,
532 cmd_buffer
->gfx9_fence_va
,
533 radv_cmd_buffer_uses_mec(cmd_buffer
),
534 flags
, cmd_buffer
->gfx9_eop_bug_va
);
537 if (unlikely(cmd_buffer
->device
->trace_bo
))
538 radv_cmd_buffer_trace_emit(cmd_buffer
);
542 radv_save_pipeline(struct radv_cmd_buffer
*cmd_buffer
,
543 struct radv_pipeline
*pipeline
, enum ring_type ring
)
545 struct radv_device
*device
= cmd_buffer
->device
;
549 va
= radv_buffer_get_va(device
->trace_bo
);
559 assert(!"invalid ring type");
562 uint64_t pipeline_address
= (uintptr_t)pipeline
;
563 data
[0] = pipeline_address
;
564 data
[1] = pipeline_address
>> 32;
566 radv_emit_write_data_packet(cmd_buffer
, va
, 2, data
);
569 void radv_set_descriptor_set(struct radv_cmd_buffer
*cmd_buffer
,
570 VkPipelineBindPoint bind_point
,
571 struct radv_descriptor_set
*set
,
574 struct radv_descriptor_state
*descriptors_state
=
575 radv_get_descriptors_state(cmd_buffer
, bind_point
);
577 descriptors_state
->sets
[idx
] = set
;
579 descriptors_state
->valid
|= (1u << idx
); /* active descriptors */
580 descriptors_state
->dirty
|= (1u << idx
);
584 radv_save_descriptors(struct radv_cmd_buffer
*cmd_buffer
,
585 VkPipelineBindPoint bind_point
)
587 struct radv_descriptor_state
*descriptors_state
=
588 radv_get_descriptors_state(cmd_buffer
, bind_point
);
589 struct radv_device
*device
= cmd_buffer
->device
;
590 uint32_t data
[MAX_SETS
* 2] = {};
593 va
= radv_buffer_get_va(device
->trace_bo
) + 24;
595 for_each_bit(i
, descriptors_state
->valid
) {
596 struct radv_descriptor_set
*set
= descriptors_state
->sets
[i
];
597 data
[i
* 2] = (uint64_t)(uintptr_t)set
;
598 data
[i
* 2 + 1] = (uint64_t)(uintptr_t)set
>> 32;
601 radv_emit_write_data_packet(cmd_buffer
, va
, MAX_SETS
* 2, data
);
604 struct radv_userdata_info
*
605 radv_lookup_user_sgpr(struct radv_pipeline
*pipeline
,
606 gl_shader_stage stage
,
609 struct radv_shader_variant
*shader
= radv_get_shader(pipeline
, stage
);
610 return &shader
->info
.user_sgprs_locs
.shader_data
[idx
];
614 radv_emit_userdata_address(struct radv_cmd_buffer
*cmd_buffer
,
615 struct radv_pipeline
*pipeline
,
616 gl_shader_stage stage
,
617 int idx
, uint64_t va
)
619 struct radv_userdata_info
*loc
= radv_lookup_user_sgpr(pipeline
, stage
, idx
);
620 uint32_t base_reg
= pipeline
->user_data_0
[stage
];
621 if (loc
->sgpr_idx
== -1)
624 assert(loc
->num_sgprs
== 1);
626 radv_emit_shader_pointer(cmd_buffer
->device
, cmd_buffer
->cs
,
627 base_reg
+ loc
->sgpr_idx
* 4, va
, false);
631 radv_emit_descriptor_pointers(struct radv_cmd_buffer
*cmd_buffer
,
632 struct radv_pipeline
*pipeline
,
633 struct radv_descriptor_state
*descriptors_state
,
634 gl_shader_stage stage
)
636 struct radv_device
*device
= cmd_buffer
->device
;
637 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
638 uint32_t sh_base
= pipeline
->user_data_0
[stage
];
639 struct radv_userdata_locations
*locs
=
640 &pipeline
->shaders
[stage
]->info
.user_sgprs_locs
;
641 unsigned mask
= locs
->descriptor_sets_enabled
;
643 mask
&= descriptors_state
->dirty
& descriptors_state
->valid
;
648 u_bit_scan_consecutive_range(&mask
, &start
, &count
);
650 struct radv_userdata_info
*loc
= &locs
->descriptor_sets
[start
];
651 unsigned sh_offset
= sh_base
+ loc
->sgpr_idx
* 4;
653 radv_emit_shader_pointer_head(cs
, sh_offset
, count
, true);
654 for (int i
= 0; i
< count
; i
++) {
655 struct radv_descriptor_set
*set
=
656 descriptors_state
->sets
[start
+ i
];
658 radv_emit_shader_pointer_body(device
, cs
, set
->va
, true);
664 * Convert the user sample locations to hardware sample locations (the values
665 * that will be emitted by PA_SC_AA_SAMPLE_LOCS_PIXEL_*).
668 radv_convert_user_sample_locs(struct radv_sample_locations_state
*state
,
669 uint32_t x
, uint32_t y
, VkOffset2D
*sample_locs
)
671 uint32_t x_offset
= x
% state
->grid_size
.width
;
672 uint32_t y_offset
= y
% state
->grid_size
.height
;
673 uint32_t num_samples
= (uint32_t)state
->per_pixel
;
674 VkSampleLocationEXT
*user_locs
;
675 uint32_t pixel_offset
;
677 pixel_offset
= (x_offset
+ y_offset
* state
->grid_size
.width
) * num_samples
;
679 assert(pixel_offset
<= MAX_SAMPLE_LOCATIONS
);
680 user_locs
= &state
->locations
[pixel_offset
];
682 for (uint32_t i
= 0; i
< num_samples
; i
++) {
683 float shifted_pos_x
= user_locs
[i
].x
- 0.5;
684 float shifted_pos_y
= user_locs
[i
].y
- 0.5;
686 int32_t scaled_pos_x
= floor(shifted_pos_x
* 16);
687 int32_t scaled_pos_y
= floor(shifted_pos_y
* 16);
689 sample_locs
[i
].x
= CLAMP(scaled_pos_x
, -8, 7);
690 sample_locs
[i
].y
= CLAMP(scaled_pos_y
, -8, 7);
695 * Compute the PA_SC_AA_SAMPLE_LOCS_PIXEL_* mask based on hardware sample
699 radv_compute_sample_locs_pixel(uint32_t num_samples
, VkOffset2D
*sample_locs
,
700 uint32_t *sample_locs_pixel
)
702 for (uint32_t i
= 0; i
< num_samples
; i
++) {
703 uint32_t sample_reg_idx
= i
/ 4;
704 uint32_t sample_loc_idx
= i
% 4;
705 int32_t pos_x
= sample_locs
[i
].x
;
706 int32_t pos_y
= sample_locs
[i
].y
;
708 uint32_t shift_x
= 8 * sample_loc_idx
;
709 uint32_t shift_y
= shift_x
+ 4;
711 sample_locs_pixel
[sample_reg_idx
] |= (pos_x
& 0xf) << shift_x
;
712 sample_locs_pixel
[sample_reg_idx
] |= (pos_y
& 0xf) << shift_y
;
717 * Compute the PA_SC_CENTROID_PRIORITY_* mask based on the top left hardware
721 radv_compute_centroid_priority(struct radv_cmd_buffer
*cmd_buffer
,
722 VkOffset2D
*sample_locs
,
723 uint32_t num_samples
)
725 uint32_t centroid_priorities
[num_samples
];
726 uint32_t sample_mask
= num_samples
- 1;
727 uint32_t distances
[num_samples
];
728 uint64_t centroid_priority
= 0;
730 /* Compute the distances from center for each sample. */
731 for (int i
= 0; i
< num_samples
; i
++) {
732 distances
[i
] = (sample_locs
[i
].x
* sample_locs
[i
].x
) +
733 (sample_locs
[i
].y
* sample_locs
[i
].y
);
736 /* Compute the centroid priorities by looking at the distances array. */
737 for (int i
= 0; i
< num_samples
; i
++) {
738 uint32_t min_idx
= 0;
740 for (int j
= 1; j
< num_samples
; j
++) {
741 if (distances
[j
] < distances
[min_idx
])
745 centroid_priorities
[i
] = min_idx
;
746 distances
[min_idx
] = 0xffffffff;
749 /* Compute the final centroid priority. */
750 for (int i
= 0; i
< 8; i
++) {
752 centroid_priorities
[i
& sample_mask
] << (i
* 4);
755 return centroid_priority
<< 32 | centroid_priority
;
759 * Emit the sample locations that are specified with VK_EXT_sample_locations.
762 radv_emit_sample_locations(struct radv_cmd_buffer
*cmd_buffer
)
764 struct radv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
765 struct radv_multisample_state
*ms
= &pipeline
->graphics
.ms
;
766 struct radv_sample_locations_state
*sample_location
=
767 &cmd_buffer
->state
.dynamic
.sample_location
;
768 uint32_t num_samples
= (uint32_t)sample_location
->per_pixel
;
769 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
770 uint32_t sample_locs_pixel
[4][2] = {};
771 VkOffset2D sample_locs
[4][8]; /* 8 is the max. sample count supported */
772 uint32_t max_sample_dist
= 0;
773 uint64_t centroid_priority
;
775 if (!cmd_buffer
->state
.dynamic
.sample_location
.count
)
778 /* Convert the user sample locations to hardware sample locations. */
779 radv_convert_user_sample_locs(sample_location
, 0, 0, sample_locs
[0]);
780 radv_convert_user_sample_locs(sample_location
, 1, 0, sample_locs
[1]);
781 radv_convert_user_sample_locs(sample_location
, 0, 1, sample_locs
[2]);
782 radv_convert_user_sample_locs(sample_location
, 1, 1, sample_locs
[3]);
784 /* Compute the PA_SC_AA_SAMPLE_LOCS_PIXEL_* mask. */
785 for (uint32_t i
= 0; i
< 4; i
++) {
786 radv_compute_sample_locs_pixel(num_samples
, sample_locs
[i
],
787 sample_locs_pixel
[i
]);
790 /* Compute the PA_SC_CENTROID_PRIORITY_* mask. */
792 radv_compute_centroid_priority(cmd_buffer
, sample_locs
[0],
795 /* Compute the maximum sample distance from the specified locations. */
796 for (uint32_t i
= 0; i
< num_samples
; i
++) {
797 VkOffset2D offset
= sample_locs
[0][i
];
798 max_sample_dist
= MAX2(max_sample_dist
,
799 MAX2(abs(offset
.x
), abs(offset
.y
)));
802 /* Emit the specified user sample locations. */
803 switch (num_samples
) {
806 radeon_set_context_reg(cs
, R_028BF8_PA_SC_AA_SAMPLE_LOCS_PIXEL_X0Y0_0
, sample_locs_pixel
[0][0]);
807 radeon_set_context_reg(cs
, R_028C08_PA_SC_AA_SAMPLE_LOCS_PIXEL_X1Y0_0
, sample_locs_pixel
[1][0]);
808 radeon_set_context_reg(cs
, R_028C18_PA_SC_AA_SAMPLE_LOCS_PIXEL_X0Y1_0
, sample_locs_pixel
[2][0]);
809 radeon_set_context_reg(cs
, R_028C28_PA_SC_AA_SAMPLE_LOCS_PIXEL_X1Y1_0
, sample_locs_pixel
[3][0]);
812 radeon_set_context_reg(cs
, R_028BF8_PA_SC_AA_SAMPLE_LOCS_PIXEL_X0Y0_0
, sample_locs_pixel
[0][0]);
813 radeon_set_context_reg(cs
, R_028C08_PA_SC_AA_SAMPLE_LOCS_PIXEL_X1Y0_0
, sample_locs_pixel
[1][0]);
814 radeon_set_context_reg(cs
, R_028C18_PA_SC_AA_SAMPLE_LOCS_PIXEL_X0Y1_0
, sample_locs_pixel
[2][0]);
815 radeon_set_context_reg(cs
, R_028C28_PA_SC_AA_SAMPLE_LOCS_PIXEL_X1Y1_0
, sample_locs_pixel
[3][0]);
816 radeon_set_context_reg(cs
, R_028BFC_PA_SC_AA_SAMPLE_LOCS_PIXEL_X0Y0_1
, sample_locs_pixel
[0][1]);
817 radeon_set_context_reg(cs
, R_028C0C_PA_SC_AA_SAMPLE_LOCS_PIXEL_X1Y0_1
, sample_locs_pixel
[1][1]);
818 radeon_set_context_reg(cs
, R_028C1C_PA_SC_AA_SAMPLE_LOCS_PIXEL_X0Y1_1
, sample_locs_pixel
[2][1]);
819 radeon_set_context_reg(cs
, R_028C2C_PA_SC_AA_SAMPLE_LOCS_PIXEL_X1Y1_1
, sample_locs_pixel
[3][1]);
822 unreachable("invalid number of samples");
825 /* Emit the maximum sample distance and the centroid priority. */
826 uint32_t pa_sc_aa_config
= ms
->pa_sc_aa_config
;
828 pa_sc_aa_config
&= C_028BE0_MAX_SAMPLE_DIST
;
829 pa_sc_aa_config
|= S_028BE0_MAX_SAMPLE_DIST(max_sample_dist
);
831 radeon_set_context_reg_seq(cs
, R_028BE0_PA_SC_AA_CONFIG
, 1);
832 radeon_emit(cs
, pa_sc_aa_config
);
834 radeon_set_context_reg_seq(cs
, R_028BD4_PA_SC_CENTROID_PRIORITY_0
, 2);
835 radeon_emit(cs
, centroid_priority
);
836 radeon_emit(cs
, centroid_priority
>> 32);
838 /* GFX9: Flush DFSM when the AA mode changes. */
839 if (cmd_buffer
->device
->dfsm_allowed
) {
840 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 0, 0));
841 radeon_emit(cs
, EVENT_TYPE(V_028A90_FLUSH_DFSM
) | EVENT_INDEX(0));
844 cmd_buffer
->state
.context_roll_without_scissor_emitted
= true;
848 radv_emit_inline_push_consts(struct radv_cmd_buffer
*cmd_buffer
,
849 struct radv_pipeline
*pipeline
,
850 gl_shader_stage stage
,
851 int idx
, int count
, uint32_t *values
)
853 struct radv_userdata_info
*loc
= radv_lookup_user_sgpr(pipeline
, stage
, idx
);
854 uint32_t base_reg
= pipeline
->user_data_0
[stage
];
855 if (loc
->sgpr_idx
== -1)
858 assert(loc
->num_sgprs
== count
);
860 radeon_set_sh_reg_seq(cmd_buffer
->cs
, base_reg
+ loc
->sgpr_idx
* 4, count
);
861 radeon_emit_array(cmd_buffer
->cs
, values
, count
);
865 radv_update_multisample_state(struct radv_cmd_buffer
*cmd_buffer
,
866 struct radv_pipeline
*pipeline
)
868 int num_samples
= pipeline
->graphics
.ms
.num_samples
;
869 struct radv_pipeline
*old_pipeline
= cmd_buffer
->state
.emitted_pipeline
;
871 if (pipeline
->shaders
[MESA_SHADER_FRAGMENT
]->info
.ps
.needs_sample_positions
)
872 cmd_buffer
->sample_positions_needed
= true;
874 if (old_pipeline
&& num_samples
== old_pipeline
->graphics
.ms
.num_samples
)
877 radv_emit_default_sample_locations(cmd_buffer
->cs
, num_samples
);
879 cmd_buffer
->state
.context_roll_without_scissor_emitted
= true;
883 radv_update_binning_state(struct radv_cmd_buffer
*cmd_buffer
,
884 struct radv_pipeline
*pipeline
)
886 const struct radv_pipeline
*old_pipeline
= cmd_buffer
->state
.emitted_pipeline
;
889 if (pipeline
->device
->physical_device
->rad_info
.chip_class
< GFX9
)
893 old_pipeline
->graphics
.binning
.pa_sc_binner_cntl_0
== pipeline
->graphics
.binning
.pa_sc_binner_cntl_0
&&
894 old_pipeline
->graphics
.binning
.db_dfsm_control
== pipeline
->graphics
.binning
.db_dfsm_control
)
897 bool binning_flush
= false;
898 if (cmd_buffer
->device
->physical_device
->rad_info
.family
== CHIP_VEGA12
||
899 cmd_buffer
->device
->physical_device
->rad_info
.family
== CHIP_VEGA20
||
900 cmd_buffer
->device
->physical_device
->rad_info
.family
== CHIP_RAVEN2
||
901 cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX10
) {
902 binning_flush
= !old_pipeline
||
903 G_028C44_BINNING_MODE(old_pipeline
->graphics
.binning
.pa_sc_binner_cntl_0
) !=
904 G_028C44_BINNING_MODE(pipeline
->graphics
.binning
.pa_sc_binner_cntl_0
);
907 radeon_set_context_reg(cmd_buffer
->cs
, R_028C44_PA_SC_BINNER_CNTL_0
,
908 pipeline
->graphics
.binning
.pa_sc_binner_cntl_0
|
909 S_028C44_FLUSH_ON_BINNING_TRANSITION(!!binning_flush
));
911 if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX10
) {
912 radeon_set_context_reg(cmd_buffer
->cs
, R_028038_DB_DFSM_CONTROL
,
913 pipeline
->graphics
.binning
.db_dfsm_control
);
915 radeon_set_context_reg(cmd_buffer
->cs
, R_028060_DB_DFSM_CONTROL
,
916 pipeline
->graphics
.binning
.db_dfsm_control
);
919 cmd_buffer
->state
.context_roll_without_scissor_emitted
= true;
924 radv_emit_shader_prefetch(struct radv_cmd_buffer
*cmd_buffer
,
925 struct radv_shader_variant
*shader
)
932 va
= radv_buffer_get_va(shader
->bo
) + shader
->bo_offset
;
934 si_cp_dma_prefetch(cmd_buffer
, va
, shader
->code_size
);
938 radv_emit_prefetch_L2(struct radv_cmd_buffer
*cmd_buffer
,
939 struct radv_pipeline
*pipeline
,
940 bool vertex_stage_only
)
942 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
943 uint32_t mask
= state
->prefetch_L2_mask
;
945 if (vertex_stage_only
) {
946 /* Fast prefetch path for starting draws as soon as possible.
948 mask
= state
->prefetch_L2_mask
& (RADV_PREFETCH_VS
|
949 RADV_PREFETCH_VBO_DESCRIPTORS
);
952 if (mask
& RADV_PREFETCH_VS
)
953 radv_emit_shader_prefetch(cmd_buffer
,
954 pipeline
->shaders
[MESA_SHADER_VERTEX
]);
956 if (mask
& RADV_PREFETCH_VBO_DESCRIPTORS
)
957 si_cp_dma_prefetch(cmd_buffer
, state
->vb_va
, state
->vb_size
);
959 if (mask
& RADV_PREFETCH_TCS
)
960 radv_emit_shader_prefetch(cmd_buffer
,
961 pipeline
->shaders
[MESA_SHADER_TESS_CTRL
]);
963 if (mask
& RADV_PREFETCH_TES
)
964 radv_emit_shader_prefetch(cmd_buffer
,
965 pipeline
->shaders
[MESA_SHADER_TESS_EVAL
]);
967 if (mask
& RADV_PREFETCH_GS
) {
968 radv_emit_shader_prefetch(cmd_buffer
,
969 pipeline
->shaders
[MESA_SHADER_GEOMETRY
]);
970 if (radv_pipeline_has_gs_copy_shader(pipeline
))
971 radv_emit_shader_prefetch(cmd_buffer
, pipeline
->gs_copy_shader
);
974 if (mask
& RADV_PREFETCH_PS
)
975 radv_emit_shader_prefetch(cmd_buffer
,
976 pipeline
->shaders
[MESA_SHADER_FRAGMENT
]);
978 state
->prefetch_L2_mask
&= ~mask
;
982 radv_emit_rbplus_state(struct radv_cmd_buffer
*cmd_buffer
)
984 if (!cmd_buffer
->device
->physical_device
->rad_info
.rbplus_allowed
)
987 struct radv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
988 const struct radv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
990 unsigned sx_ps_downconvert
= 0;
991 unsigned sx_blend_opt_epsilon
= 0;
992 unsigned sx_blend_opt_control
= 0;
994 if (!cmd_buffer
->state
.attachments
|| !subpass
)
997 for (unsigned i
= 0; i
< subpass
->color_count
; ++i
) {
998 if (subpass
->color_attachments
[i
].attachment
== VK_ATTACHMENT_UNUSED
) {
999 sx_blend_opt_control
|= S_02875C_MRT0_COLOR_OPT_DISABLE(1) << (i
* 4);
1000 sx_blend_opt_control
|= S_02875C_MRT0_ALPHA_OPT_DISABLE(1) << (i
* 4);
1004 int idx
= subpass
->color_attachments
[i
].attachment
;
1005 struct radv_color_buffer_info
*cb
= &cmd_buffer
->state
.attachments
[idx
].cb
;
1007 unsigned format
= G_028C70_FORMAT(cb
->cb_color_info
);
1008 unsigned swap
= G_028C70_COMP_SWAP(cb
->cb_color_info
);
1009 uint32_t spi_format
= (pipeline
->graphics
.col_format
>> (i
* 4)) & 0xf;
1010 uint32_t colormask
= (pipeline
->graphics
.cb_target_mask
>> (i
* 4)) & 0xf;
1012 bool has_alpha
, has_rgb
;
1014 /* Set if RGB and A are present. */
1015 has_alpha
= !G_028C74_FORCE_DST_ALPHA_1(cb
->cb_color_attrib
);
1017 if (format
== V_028C70_COLOR_8
||
1018 format
== V_028C70_COLOR_16
||
1019 format
== V_028C70_COLOR_32
)
1020 has_rgb
= !has_alpha
;
1024 /* Check the colormask and export format. */
1025 if (!(colormask
& 0x7))
1027 if (!(colormask
& 0x8))
1030 if (spi_format
== V_028714_SPI_SHADER_ZERO
) {
1035 /* Disable value checking for disabled channels. */
1037 sx_blend_opt_control
|= S_02875C_MRT0_COLOR_OPT_DISABLE(1) << (i
* 4);
1039 sx_blend_opt_control
|= S_02875C_MRT0_ALPHA_OPT_DISABLE(1) << (i
* 4);
1041 /* Enable down-conversion for 32bpp and smaller formats. */
1043 case V_028C70_COLOR_8
:
1044 case V_028C70_COLOR_8_8
:
1045 case V_028C70_COLOR_8_8_8_8
:
1046 /* For 1 and 2-channel formats, use the superset thereof. */
1047 if (spi_format
== V_028714_SPI_SHADER_FP16_ABGR
||
1048 spi_format
== V_028714_SPI_SHADER_UINT16_ABGR
||
1049 spi_format
== V_028714_SPI_SHADER_SINT16_ABGR
) {
1050 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_8_8_8_8
<< (i
* 4);
1051 sx_blend_opt_epsilon
|= V_028758_8BIT_FORMAT
<< (i
* 4);
1055 case V_028C70_COLOR_5_6_5
:
1056 if (spi_format
== V_028714_SPI_SHADER_FP16_ABGR
) {
1057 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_5_6_5
<< (i
* 4);
1058 sx_blend_opt_epsilon
|= V_028758_6BIT_FORMAT
<< (i
* 4);
1062 case V_028C70_COLOR_1_5_5_5
:
1063 if (spi_format
== V_028714_SPI_SHADER_FP16_ABGR
) {
1064 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_1_5_5_5
<< (i
* 4);
1065 sx_blend_opt_epsilon
|= V_028758_5BIT_FORMAT
<< (i
* 4);
1069 case V_028C70_COLOR_4_4_4_4
:
1070 if (spi_format
== V_028714_SPI_SHADER_FP16_ABGR
) {
1071 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_4_4_4_4
<< (i
* 4);
1072 sx_blend_opt_epsilon
|= V_028758_4BIT_FORMAT
<< (i
* 4);
1076 case V_028C70_COLOR_32
:
1077 if (swap
== V_028C70_SWAP_STD
&&
1078 spi_format
== V_028714_SPI_SHADER_32_R
)
1079 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_32_R
<< (i
* 4);
1080 else if (swap
== V_028C70_SWAP_ALT_REV
&&
1081 spi_format
== V_028714_SPI_SHADER_32_AR
)
1082 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_32_A
<< (i
* 4);
1085 case V_028C70_COLOR_16
:
1086 case V_028C70_COLOR_16_16
:
1087 /* For 1-channel formats, use the superset thereof. */
1088 if (spi_format
== V_028714_SPI_SHADER_UNORM16_ABGR
||
1089 spi_format
== V_028714_SPI_SHADER_SNORM16_ABGR
||
1090 spi_format
== V_028714_SPI_SHADER_UINT16_ABGR
||
1091 spi_format
== V_028714_SPI_SHADER_SINT16_ABGR
) {
1092 if (swap
== V_028C70_SWAP_STD
||
1093 swap
== V_028C70_SWAP_STD_REV
)
1094 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_16_16_GR
<< (i
* 4);
1096 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_16_16_AR
<< (i
* 4);
1100 case V_028C70_COLOR_10_11_11
:
1101 if (spi_format
== V_028714_SPI_SHADER_FP16_ABGR
) {
1102 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_10_11_11
<< (i
* 4);
1103 sx_blend_opt_epsilon
|= V_028758_11BIT_FORMAT
<< (i
* 4);
1107 case V_028C70_COLOR_2_10_10_10
:
1108 if (spi_format
== V_028714_SPI_SHADER_FP16_ABGR
) {
1109 sx_ps_downconvert
|= V_028754_SX_RT_EXPORT_2_10_10_10
<< (i
* 4);
1110 sx_blend_opt_epsilon
|= V_028758_10BIT_FORMAT
<< (i
* 4);
1116 for (unsigned i
= subpass
->color_count
; i
< 8; ++i
) {
1117 sx_blend_opt_control
|= S_02875C_MRT0_COLOR_OPT_DISABLE(1) << (i
* 4);
1118 sx_blend_opt_control
|= S_02875C_MRT0_ALPHA_OPT_DISABLE(1) << (i
* 4);
1120 /* TODO: avoid redundantly setting context registers */
1121 radeon_set_context_reg_seq(cmd_buffer
->cs
, R_028754_SX_PS_DOWNCONVERT
, 3);
1122 radeon_emit(cmd_buffer
->cs
, sx_ps_downconvert
);
1123 radeon_emit(cmd_buffer
->cs
, sx_blend_opt_epsilon
);
1124 radeon_emit(cmd_buffer
->cs
, sx_blend_opt_control
);
1126 cmd_buffer
->state
.context_roll_without_scissor_emitted
= true;
1130 radv_emit_batch_break_on_new_ps(struct radv_cmd_buffer
*cmd_buffer
)
1132 if (!cmd_buffer
->device
->pbb_allowed
)
1135 struct radv_binning_settings settings
=
1136 radv_get_binning_settings(cmd_buffer
->device
->physical_device
);
1137 bool break_for_new_ps
=
1138 (!cmd_buffer
->state
.emitted_pipeline
||
1139 cmd_buffer
->state
.emitted_pipeline
->shaders
[MESA_SHADER_FRAGMENT
] !=
1140 cmd_buffer
->state
.pipeline
->shaders
[MESA_SHADER_FRAGMENT
]) &&
1141 (settings
.context_states_per_bin
> 1 ||
1142 settings
.persistent_states_per_bin
> 1);
1143 bool break_for_new_cb_target_mask
=
1144 (!cmd_buffer
->state
.emitted_pipeline
||
1145 cmd_buffer
->state
.emitted_pipeline
->graphics
.cb_target_mask
!=
1146 cmd_buffer
->state
.pipeline
->graphics
.cb_target_mask
) &&
1147 settings
.context_states_per_bin
> 1;
1149 if (!break_for_new_ps
&& !break_for_new_cb_target_mask
)
1152 radeon_emit(cmd_buffer
->cs
, PKT3(PKT3_EVENT_WRITE
, 0, 0));
1153 radeon_emit(cmd_buffer
->cs
, EVENT_TYPE(V_028A90_BREAK_BATCH
) | EVENT_INDEX(0));
1157 radv_emit_graphics_pipeline(struct radv_cmd_buffer
*cmd_buffer
)
1159 struct radv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
1161 if (!pipeline
|| cmd_buffer
->state
.emitted_pipeline
== pipeline
)
1164 radv_update_multisample_state(cmd_buffer
, pipeline
);
1165 radv_update_binning_state(cmd_buffer
, pipeline
);
1167 cmd_buffer
->scratch_size_per_wave_needed
= MAX2(cmd_buffer
->scratch_size_per_wave_needed
,
1168 pipeline
->scratch_bytes_per_wave
);
1169 cmd_buffer
->scratch_waves_wanted
= MAX2(cmd_buffer
->scratch_waves_wanted
,
1170 pipeline
->max_waves
);
1172 if (!cmd_buffer
->state
.emitted_pipeline
||
1173 cmd_buffer
->state
.emitted_pipeline
->graphics
.can_use_guardband
!=
1174 pipeline
->graphics
.can_use_guardband
)
1175 cmd_buffer
->state
.dirty
|= RADV_CMD_DIRTY_DYNAMIC_SCISSOR
;
1177 radeon_emit_array(cmd_buffer
->cs
, pipeline
->cs
.buf
, pipeline
->cs
.cdw
);
1179 if (!cmd_buffer
->state
.emitted_pipeline
||
1180 cmd_buffer
->state
.emitted_pipeline
->ctx_cs
.cdw
!= pipeline
->ctx_cs
.cdw
||
1181 cmd_buffer
->state
.emitted_pipeline
->ctx_cs_hash
!= pipeline
->ctx_cs_hash
||
1182 memcmp(cmd_buffer
->state
.emitted_pipeline
->ctx_cs
.buf
,
1183 pipeline
->ctx_cs
.buf
, pipeline
->ctx_cs
.cdw
* 4)) {
1184 radeon_emit_array(cmd_buffer
->cs
, pipeline
->ctx_cs
.buf
, pipeline
->ctx_cs
.cdw
);
1185 cmd_buffer
->state
.context_roll_without_scissor_emitted
= true;
1188 radv_emit_batch_break_on_new_ps(cmd_buffer
);
1190 for (unsigned i
= 0; i
< MESA_SHADER_COMPUTE
; i
++) {
1191 if (!pipeline
->shaders
[i
])
1194 radv_cs_add_buffer(cmd_buffer
->device
->ws
, cmd_buffer
->cs
,
1195 pipeline
->shaders
[i
]->bo
);
1198 if (radv_pipeline_has_gs_copy_shader(pipeline
))
1199 radv_cs_add_buffer(cmd_buffer
->device
->ws
, cmd_buffer
->cs
,
1200 pipeline
->gs_copy_shader
->bo
);
1202 if (unlikely(cmd_buffer
->device
->trace_bo
))
1203 radv_save_pipeline(cmd_buffer
, pipeline
, RING_GFX
);
1205 cmd_buffer
->state
.emitted_pipeline
= pipeline
;
1207 cmd_buffer
->state
.dirty
&= ~RADV_CMD_DIRTY_PIPELINE
;
1211 radv_emit_viewport(struct radv_cmd_buffer
*cmd_buffer
)
1213 si_write_viewport(cmd_buffer
->cs
, 0, cmd_buffer
->state
.dynamic
.viewport
.count
,
1214 cmd_buffer
->state
.dynamic
.viewport
.viewports
);
1218 radv_emit_scissor(struct radv_cmd_buffer
*cmd_buffer
)
1220 uint32_t count
= cmd_buffer
->state
.dynamic
.scissor
.count
;
1222 si_write_scissors(cmd_buffer
->cs
, 0, count
,
1223 cmd_buffer
->state
.dynamic
.scissor
.scissors
,
1224 cmd_buffer
->state
.dynamic
.viewport
.viewports
,
1225 cmd_buffer
->state
.emitted_pipeline
->graphics
.can_use_guardband
);
1227 cmd_buffer
->state
.context_roll_without_scissor_emitted
= false;
1231 radv_emit_discard_rectangle(struct radv_cmd_buffer
*cmd_buffer
)
1233 if (!cmd_buffer
->state
.dynamic
.discard_rectangle
.count
)
1236 radeon_set_context_reg_seq(cmd_buffer
->cs
, R_028210_PA_SC_CLIPRECT_0_TL
,
1237 cmd_buffer
->state
.dynamic
.discard_rectangle
.count
* 2);
1238 for (unsigned i
= 0; i
< cmd_buffer
->state
.dynamic
.discard_rectangle
.count
; ++i
) {
1239 VkRect2D rect
= cmd_buffer
->state
.dynamic
.discard_rectangle
.rectangles
[i
];
1240 radeon_emit(cmd_buffer
->cs
, S_028210_TL_X(rect
.offset
.x
) | S_028210_TL_Y(rect
.offset
.y
));
1241 radeon_emit(cmd_buffer
->cs
, S_028214_BR_X(rect
.offset
.x
+ rect
.extent
.width
) |
1242 S_028214_BR_Y(rect
.offset
.y
+ rect
.extent
.height
));
1247 radv_emit_line_width(struct radv_cmd_buffer
*cmd_buffer
)
1249 unsigned width
= cmd_buffer
->state
.dynamic
.line_width
* 8;
1251 radeon_set_context_reg(cmd_buffer
->cs
, R_028A08_PA_SU_LINE_CNTL
,
1252 S_028A08_WIDTH(CLAMP(width
, 0, 0xFFF)));
1256 radv_emit_blend_constants(struct radv_cmd_buffer
*cmd_buffer
)
1258 struct radv_dynamic_state
*d
= &cmd_buffer
->state
.dynamic
;
1260 radeon_set_context_reg_seq(cmd_buffer
->cs
, R_028414_CB_BLEND_RED
, 4);
1261 radeon_emit_array(cmd_buffer
->cs
, (uint32_t *)d
->blend_constants
, 4);
1265 radv_emit_stencil(struct radv_cmd_buffer
*cmd_buffer
)
1267 struct radv_dynamic_state
*d
= &cmd_buffer
->state
.dynamic
;
1269 radeon_set_context_reg_seq(cmd_buffer
->cs
,
1270 R_028430_DB_STENCILREFMASK
, 2);
1271 radeon_emit(cmd_buffer
->cs
,
1272 S_028430_STENCILTESTVAL(d
->stencil_reference
.front
) |
1273 S_028430_STENCILMASK(d
->stencil_compare_mask
.front
) |
1274 S_028430_STENCILWRITEMASK(d
->stencil_write_mask
.front
) |
1275 S_028430_STENCILOPVAL(1));
1276 radeon_emit(cmd_buffer
->cs
,
1277 S_028434_STENCILTESTVAL_BF(d
->stencil_reference
.back
) |
1278 S_028434_STENCILMASK_BF(d
->stencil_compare_mask
.back
) |
1279 S_028434_STENCILWRITEMASK_BF(d
->stencil_write_mask
.back
) |
1280 S_028434_STENCILOPVAL_BF(1));
1284 radv_emit_depth_bounds(struct radv_cmd_buffer
*cmd_buffer
)
1286 struct radv_dynamic_state
*d
= &cmd_buffer
->state
.dynamic
;
1288 radeon_set_context_reg(cmd_buffer
->cs
, R_028020_DB_DEPTH_BOUNDS_MIN
,
1289 fui(d
->depth_bounds
.min
));
1290 radeon_set_context_reg(cmd_buffer
->cs
, R_028024_DB_DEPTH_BOUNDS_MAX
,
1291 fui(d
->depth_bounds
.max
));
1295 radv_emit_depth_bias(struct radv_cmd_buffer
*cmd_buffer
)
1297 struct radv_dynamic_state
*d
= &cmd_buffer
->state
.dynamic
;
1298 unsigned slope
= fui(d
->depth_bias
.slope
* 16.0f
);
1299 unsigned bias
= fui(d
->depth_bias
.bias
* cmd_buffer
->state
.offset_scale
);
1302 radeon_set_context_reg_seq(cmd_buffer
->cs
,
1303 R_028B7C_PA_SU_POLY_OFFSET_CLAMP
, 5);
1304 radeon_emit(cmd_buffer
->cs
, fui(d
->depth_bias
.clamp
)); /* CLAMP */
1305 radeon_emit(cmd_buffer
->cs
, slope
); /* FRONT SCALE */
1306 radeon_emit(cmd_buffer
->cs
, bias
); /* FRONT OFFSET */
1307 radeon_emit(cmd_buffer
->cs
, slope
); /* BACK SCALE */
1308 radeon_emit(cmd_buffer
->cs
, bias
); /* BACK OFFSET */
1312 radv_emit_fb_color_state(struct radv_cmd_buffer
*cmd_buffer
,
1314 struct radv_color_buffer_info
*cb
,
1315 struct radv_image_view
*iview
,
1316 VkImageLayout layout
,
1317 bool in_render_loop
)
1319 bool is_vi
= cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX8
;
1320 uint32_t cb_color_info
= cb
->cb_color_info
;
1321 struct radv_image
*image
= iview
->image
;
1323 if (!radv_layout_dcc_compressed(cmd_buffer
->device
, image
, layout
, in_render_loop
,
1324 radv_image_queue_family_mask(image
,
1325 cmd_buffer
->queue_family_index
,
1326 cmd_buffer
->queue_family_index
))) {
1327 cb_color_info
&= C_028C70_DCC_ENABLE
;
1330 if (radv_image_is_tc_compat_cmask(image
) &&
1331 (radv_is_fmask_decompress_pipeline(cmd_buffer
) ||
1332 radv_is_dcc_decompress_pipeline(cmd_buffer
))) {
1333 /* If this bit is set, the FMASK decompression operation
1334 * doesn't occur (DCC_COMPRESS also implies FMASK_DECOMPRESS).
1336 cb_color_info
&= C_028C70_FMASK_COMPRESS_1FRAG_ONLY
;
1339 if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX10
) {
1340 radeon_set_context_reg_seq(cmd_buffer
->cs
, R_028C60_CB_COLOR0_BASE
+ index
* 0x3c, 11);
1341 radeon_emit(cmd_buffer
->cs
, cb
->cb_color_base
);
1342 radeon_emit(cmd_buffer
->cs
, 0);
1343 radeon_emit(cmd_buffer
->cs
, 0);
1344 radeon_emit(cmd_buffer
->cs
, cb
->cb_color_view
);
1345 radeon_emit(cmd_buffer
->cs
, cb_color_info
);
1346 radeon_emit(cmd_buffer
->cs
, cb
->cb_color_attrib
);
1347 radeon_emit(cmd_buffer
->cs
, cb
->cb_dcc_control
);
1348 radeon_emit(cmd_buffer
->cs
, cb
->cb_color_cmask
);
1349 radeon_emit(cmd_buffer
->cs
, 0);
1350 radeon_emit(cmd_buffer
->cs
, cb
->cb_color_fmask
);
1351 radeon_emit(cmd_buffer
->cs
, 0);
1353 radeon_set_context_reg_seq(cmd_buffer
->cs
, R_028C94_CB_COLOR0_DCC_BASE
+ index
* 0x3c, 1);
1354 radeon_emit(cmd_buffer
->cs
, cb
->cb_dcc_base
);
1356 radeon_set_context_reg(cmd_buffer
->cs
, R_028E40_CB_COLOR0_BASE_EXT
+ index
* 4,
1357 cb
->cb_color_base
>> 32);
1358 radeon_set_context_reg(cmd_buffer
->cs
, R_028E60_CB_COLOR0_CMASK_BASE_EXT
+ index
* 4,
1359 cb
->cb_color_cmask
>> 32);
1360 radeon_set_context_reg(cmd_buffer
->cs
, R_028E80_CB_COLOR0_FMASK_BASE_EXT
+ index
* 4,
1361 cb
->cb_color_fmask
>> 32);
1362 radeon_set_context_reg(cmd_buffer
->cs
, R_028EA0_CB_COLOR0_DCC_BASE_EXT
+ index
* 4,
1363 cb
->cb_dcc_base
>> 32);
1364 radeon_set_context_reg(cmd_buffer
->cs
, R_028EC0_CB_COLOR0_ATTRIB2
+ index
* 4,
1365 cb
->cb_color_attrib2
);
1366 radeon_set_context_reg(cmd_buffer
->cs
, R_028EE0_CB_COLOR0_ATTRIB3
+ index
* 4,
1367 cb
->cb_color_attrib3
);
1368 } else if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
== GFX9
) {
1369 radeon_set_context_reg_seq(cmd_buffer
->cs
, R_028C60_CB_COLOR0_BASE
+ index
* 0x3c, 11);
1370 radeon_emit(cmd_buffer
->cs
, cb
->cb_color_base
);
1371 radeon_emit(cmd_buffer
->cs
, S_028C64_BASE_256B(cb
->cb_color_base
>> 32));
1372 radeon_emit(cmd_buffer
->cs
, cb
->cb_color_attrib2
);
1373 radeon_emit(cmd_buffer
->cs
, cb
->cb_color_view
);
1374 radeon_emit(cmd_buffer
->cs
, cb_color_info
);
1375 radeon_emit(cmd_buffer
->cs
, cb
->cb_color_attrib
);
1376 radeon_emit(cmd_buffer
->cs
, cb
->cb_dcc_control
);
1377 radeon_emit(cmd_buffer
->cs
, cb
->cb_color_cmask
);
1378 radeon_emit(cmd_buffer
->cs
, S_028C80_BASE_256B(cb
->cb_color_cmask
>> 32));
1379 radeon_emit(cmd_buffer
->cs
, cb
->cb_color_fmask
);
1380 radeon_emit(cmd_buffer
->cs
, S_028C88_BASE_256B(cb
->cb_color_fmask
>> 32));
1382 radeon_set_context_reg_seq(cmd_buffer
->cs
, R_028C94_CB_COLOR0_DCC_BASE
+ index
* 0x3c, 2);
1383 radeon_emit(cmd_buffer
->cs
, cb
->cb_dcc_base
);
1384 radeon_emit(cmd_buffer
->cs
, S_028C98_BASE_256B(cb
->cb_dcc_base
>> 32));
1386 radeon_set_context_reg(cmd_buffer
->cs
, R_0287A0_CB_MRT0_EPITCH
+ index
* 4,
1389 radeon_set_context_reg_seq(cmd_buffer
->cs
, R_028C60_CB_COLOR0_BASE
+ index
* 0x3c, 11);
1390 radeon_emit(cmd_buffer
->cs
, cb
->cb_color_base
);
1391 radeon_emit(cmd_buffer
->cs
, cb
->cb_color_pitch
);
1392 radeon_emit(cmd_buffer
->cs
, cb
->cb_color_slice
);
1393 radeon_emit(cmd_buffer
->cs
, cb
->cb_color_view
);
1394 radeon_emit(cmd_buffer
->cs
, cb_color_info
);
1395 radeon_emit(cmd_buffer
->cs
, cb
->cb_color_attrib
);
1396 radeon_emit(cmd_buffer
->cs
, cb
->cb_dcc_control
);
1397 radeon_emit(cmd_buffer
->cs
, cb
->cb_color_cmask
);
1398 radeon_emit(cmd_buffer
->cs
, cb
->cb_color_cmask_slice
);
1399 radeon_emit(cmd_buffer
->cs
, cb
->cb_color_fmask
);
1400 radeon_emit(cmd_buffer
->cs
, cb
->cb_color_fmask_slice
);
1402 if (is_vi
) { /* DCC BASE */
1403 radeon_set_context_reg(cmd_buffer
->cs
, R_028C94_CB_COLOR0_DCC_BASE
+ index
* 0x3c, cb
->cb_dcc_base
);
1407 if (radv_dcc_enabled(image
, iview
->base_mip
)) {
1408 /* Drawing with DCC enabled also compresses colorbuffers. */
1409 VkImageSubresourceRange range
= {
1410 .aspectMask
= iview
->aspect_mask
,
1411 .baseMipLevel
= iview
->base_mip
,
1412 .levelCount
= iview
->level_count
,
1413 .baseArrayLayer
= iview
->base_layer
,
1414 .layerCount
= iview
->layer_count
,
1417 radv_update_dcc_metadata(cmd_buffer
, image
, &range
, true);
1422 radv_update_zrange_precision(struct radv_cmd_buffer
*cmd_buffer
,
1423 struct radv_ds_buffer_info
*ds
,
1424 const struct radv_image_view
*iview
,
1425 VkImageLayout layout
,
1426 bool in_render_loop
, bool requires_cond_exec
)
1428 const struct radv_image
*image
= iview
->image
;
1429 uint32_t db_z_info
= ds
->db_z_info
;
1430 uint32_t db_z_info_reg
;
1432 if (!cmd_buffer
->device
->physical_device
->rad_info
.has_tc_compat_zrange_bug
||
1433 !radv_image_is_tc_compat_htile(image
))
1436 if (!radv_layout_has_htile(image
, layout
, in_render_loop
,
1437 radv_image_queue_family_mask(image
,
1438 cmd_buffer
->queue_family_index
,
1439 cmd_buffer
->queue_family_index
))) {
1440 db_z_info
&= C_028040_TILE_SURFACE_ENABLE
;
1443 db_z_info
&= C_028040_ZRANGE_PRECISION
;
1445 if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
== GFX9
) {
1446 db_z_info_reg
= R_028038_DB_Z_INFO
;
1448 db_z_info_reg
= R_028040_DB_Z_INFO
;
1451 /* When we don't know the last fast clear value we need to emit a
1452 * conditional packet that will eventually skip the following
1453 * SET_CONTEXT_REG packet.
1455 if (requires_cond_exec
) {
1456 uint64_t va
= radv_get_tc_compat_zrange_va(image
, iview
->base_mip
);
1458 radeon_emit(cmd_buffer
->cs
, PKT3(PKT3_COND_EXEC
, 3, 0));
1459 radeon_emit(cmd_buffer
->cs
, va
);
1460 radeon_emit(cmd_buffer
->cs
, va
>> 32);
1461 radeon_emit(cmd_buffer
->cs
, 0);
1462 radeon_emit(cmd_buffer
->cs
, 3); /* SET_CONTEXT_REG size */
1465 radeon_set_context_reg(cmd_buffer
->cs
, db_z_info_reg
, db_z_info
);
1469 radv_emit_fb_ds_state(struct radv_cmd_buffer
*cmd_buffer
,
1470 struct radv_ds_buffer_info
*ds
,
1471 struct radv_image_view
*iview
,
1472 VkImageLayout layout
,
1473 bool in_render_loop
)
1475 const struct radv_image
*image
= iview
->image
;
1476 uint32_t db_z_info
= ds
->db_z_info
;
1477 uint32_t db_stencil_info
= ds
->db_stencil_info
;
1479 if (!radv_layout_has_htile(image
, layout
, in_render_loop
,
1480 radv_image_queue_family_mask(image
,
1481 cmd_buffer
->queue_family_index
,
1482 cmd_buffer
->queue_family_index
))) {
1483 db_z_info
&= C_028040_TILE_SURFACE_ENABLE
;
1484 db_stencil_info
|= S_028044_TILE_STENCIL_DISABLE(1);
1487 radeon_set_context_reg(cmd_buffer
->cs
, R_028008_DB_DEPTH_VIEW
, ds
->db_depth_view
);
1488 radeon_set_context_reg(cmd_buffer
->cs
, R_028ABC_DB_HTILE_SURFACE
, ds
->db_htile_surface
);
1490 if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX10
) {
1491 radeon_set_context_reg(cmd_buffer
->cs
, R_028014_DB_HTILE_DATA_BASE
, ds
->db_htile_data_base
);
1492 radeon_set_context_reg(cmd_buffer
->cs
, R_02801C_DB_DEPTH_SIZE_XY
, ds
->db_depth_size
);
1494 radeon_set_context_reg_seq(cmd_buffer
->cs
, R_02803C_DB_DEPTH_INFO
, 7);
1495 radeon_emit(cmd_buffer
->cs
, S_02803C_RESOURCE_LEVEL(1));
1496 radeon_emit(cmd_buffer
->cs
, db_z_info
);
1497 radeon_emit(cmd_buffer
->cs
, db_stencil_info
);
1498 radeon_emit(cmd_buffer
->cs
, ds
->db_z_read_base
);
1499 radeon_emit(cmd_buffer
->cs
, ds
->db_stencil_read_base
);
1500 radeon_emit(cmd_buffer
->cs
, ds
->db_z_read_base
);
1501 radeon_emit(cmd_buffer
->cs
, ds
->db_stencil_read_base
);
1503 radeon_set_context_reg_seq(cmd_buffer
->cs
, R_028068_DB_Z_READ_BASE_HI
, 5);
1504 radeon_emit(cmd_buffer
->cs
, ds
->db_z_read_base
>> 32);
1505 radeon_emit(cmd_buffer
->cs
, ds
->db_stencil_read_base
>> 32);
1506 radeon_emit(cmd_buffer
->cs
, ds
->db_z_read_base
>> 32);
1507 radeon_emit(cmd_buffer
->cs
, ds
->db_stencil_read_base
>> 32);
1508 radeon_emit(cmd_buffer
->cs
, ds
->db_htile_data_base
>> 32);
1509 } else if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
== GFX9
) {
1510 radeon_set_context_reg_seq(cmd_buffer
->cs
, R_028014_DB_HTILE_DATA_BASE
, 3);
1511 radeon_emit(cmd_buffer
->cs
, ds
->db_htile_data_base
);
1512 radeon_emit(cmd_buffer
->cs
, S_028018_BASE_HI(ds
->db_htile_data_base
>> 32));
1513 radeon_emit(cmd_buffer
->cs
, ds
->db_depth_size
);
1515 radeon_set_context_reg_seq(cmd_buffer
->cs
, R_028038_DB_Z_INFO
, 10);
1516 radeon_emit(cmd_buffer
->cs
, db_z_info
); /* DB_Z_INFO */
1517 radeon_emit(cmd_buffer
->cs
, db_stencil_info
); /* DB_STENCIL_INFO */
1518 radeon_emit(cmd_buffer
->cs
, ds
->db_z_read_base
); /* DB_Z_READ_BASE */
1519 radeon_emit(cmd_buffer
->cs
, S_028044_BASE_HI(ds
->db_z_read_base
>> 32)); /* DB_Z_READ_BASE_HI */
1520 radeon_emit(cmd_buffer
->cs
, ds
->db_stencil_read_base
); /* DB_STENCIL_READ_BASE */
1521 radeon_emit(cmd_buffer
->cs
, S_02804C_BASE_HI(ds
->db_stencil_read_base
>> 32)); /* DB_STENCIL_READ_BASE_HI */
1522 radeon_emit(cmd_buffer
->cs
, ds
->db_z_write_base
); /* DB_Z_WRITE_BASE */
1523 radeon_emit(cmd_buffer
->cs
, S_028054_BASE_HI(ds
->db_z_write_base
>> 32)); /* DB_Z_WRITE_BASE_HI */
1524 radeon_emit(cmd_buffer
->cs
, ds
->db_stencil_write_base
); /* DB_STENCIL_WRITE_BASE */
1525 radeon_emit(cmd_buffer
->cs
, S_02805C_BASE_HI(ds
->db_stencil_write_base
>> 32)); /* DB_STENCIL_WRITE_BASE_HI */
1527 radeon_set_context_reg_seq(cmd_buffer
->cs
, R_028068_DB_Z_INFO2
, 2);
1528 radeon_emit(cmd_buffer
->cs
, ds
->db_z_info2
);
1529 radeon_emit(cmd_buffer
->cs
, ds
->db_stencil_info2
);
1531 radeon_set_context_reg(cmd_buffer
->cs
, R_028014_DB_HTILE_DATA_BASE
, ds
->db_htile_data_base
);
1533 radeon_set_context_reg_seq(cmd_buffer
->cs
, R_02803C_DB_DEPTH_INFO
, 9);
1534 radeon_emit(cmd_buffer
->cs
, ds
->db_depth_info
); /* R_02803C_DB_DEPTH_INFO */
1535 radeon_emit(cmd_buffer
->cs
, db_z_info
); /* R_028040_DB_Z_INFO */
1536 radeon_emit(cmd_buffer
->cs
, db_stencil_info
); /* R_028044_DB_STENCIL_INFO */
1537 radeon_emit(cmd_buffer
->cs
, ds
->db_z_read_base
); /* R_028048_DB_Z_READ_BASE */
1538 radeon_emit(cmd_buffer
->cs
, ds
->db_stencil_read_base
); /* R_02804C_DB_STENCIL_READ_BASE */
1539 radeon_emit(cmd_buffer
->cs
, ds
->db_z_write_base
); /* R_028050_DB_Z_WRITE_BASE */
1540 radeon_emit(cmd_buffer
->cs
, ds
->db_stencil_write_base
); /* R_028054_DB_STENCIL_WRITE_BASE */
1541 radeon_emit(cmd_buffer
->cs
, ds
->db_depth_size
); /* R_028058_DB_DEPTH_SIZE */
1542 radeon_emit(cmd_buffer
->cs
, ds
->db_depth_slice
); /* R_02805C_DB_DEPTH_SLICE */
1546 /* Update the ZRANGE_PRECISION value for the TC-compat bug. */
1547 radv_update_zrange_precision(cmd_buffer
, ds
, iview
, layout
,
1548 in_render_loop
, true);
1550 radeon_set_context_reg(cmd_buffer
->cs
, R_028B78_PA_SU_POLY_OFFSET_DB_FMT_CNTL
,
1551 ds
->pa_su_poly_offset_db_fmt_cntl
);
1555 * Update the fast clear depth/stencil values if the image is bound as a
1556 * depth/stencil buffer.
1559 radv_update_bound_fast_clear_ds(struct radv_cmd_buffer
*cmd_buffer
,
1560 const struct radv_image_view
*iview
,
1561 VkClearDepthStencilValue ds_clear_value
,
1562 VkImageAspectFlags aspects
)
1564 const struct radv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
1565 const struct radv_image
*image
= iview
->image
;
1566 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
1569 if (!cmd_buffer
->state
.attachments
|| !subpass
)
1572 if (!subpass
->depth_stencil_attachment
)
1575 att_idx
= subpass
->depth_stencil_attachment
->attachment
;
1576 if (cmd_buffer
->state
.attachments
[att_idx
].iview
->image
!= image
)
1579 if (aspects
== (VK_IMAGE_ASPECT_DEPTH_BIT
|
1580 VK_IMAGE_ASPECT_STENCIL_BIT
)) {
1581 radeon_set_context_reg_seq(cs
, R_028028_DB_STENCIL_CLEAR
, 2);
1582 radeon_emit(cs
, ds_clear_value
.stencil
);
1583 radeon_emit(cs
, fui(ds_clear_value
.depth
));
1584 } else if (aspects
== VK_IMAGE_ASPECT_DEPTH_BIT
) {
1585 radeon_set_context_reg_seq(cs
, R_02802C_DB_DEPTH_CLEAR
, 1);
1586 radeon_emit(cs
, fui(ds_clear_value
.depth
));
1588 assert(aspects
== VK_IMAGE_ASPECT_STENCIL_BIT
);
1589 radeon_set_context_reg_seq(cs
, R_028028_DB_STENCIL_CLEAR
, 1);
1590 radeon_emit(cs
, ds_clear_value
.stencil
);
1593 /* Update the ZRANGE_PRECISION value for the TC-compat bug. This is
1594 * only needed when clearing Z to 0.0.
1596 if ((aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) &&
1597 ds_clear_value
.depth
== 0.0) {
1598 VkImageLayout layout
= subpass
->depth_stencil_attachment
->layout
;
1599 bool in_render_loop
= subpass
->depth_stencil_attachment
->in_render_loop
;
1601 radv_update_zrange_precision(cmd_buffer
, &cmd_buffer
->state
.attachments
[att_idx
].ds
,
1602 iview
, layout
, in_render_loop
, false);
1605 cmd_buffer
->state
.context_roll_without_scissor_emitted
= true;
1609 * Set the clear depth/stencil values to the image's metadata.
1612 radv_set_ds_clear_metadata(struct radv_cmd_buffer
*cmd_buffer
,
1613 struct radv_image
*image
,
1614 const VkImageSubresourceRange
*range
,
1615 VkClearDepthStencilValue ds_clear_value
,
1616 VkImageAspectFlags aspects
)
1618 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
1619 uint64_t va
= radv_get_ds_clear_value_va(image
, range
->baseMipLevel
);
1620 uint32_t level_count
= radv_get_levelCount(image
, range
);
1622 if (aspects
== (VK_IMAGE_ASPECT_DEPTH_BIT
|
1623 VK_IMAGE_ASPECT_STENCIL_BIT
)) {
1624 /* Use the fastest way when both aspects are used. */
1625 radeon_emit(cs
, PKT3(PKT3_WRITE_DATA
, 2 + 2 * level_count
, cmd_buffer
->state
.predicating
));
1626 radeon_emit(cs
, S_370_DST_SEL(V_370_MEM
) |
1627 S_370_WR_CONFIRM(1) |
1628 S_370_ENGINE_SEL(V_370_PFP
));
1629 radeon_emit(cs
, va
);
1630 radeon_emit(cs
, va
>> 32);
1632 for (uint32_t l
= 0; l
< level_count
; l
++) {
1633 radeon_emit(cs
, ds_clear_value
.stencil
);
1634 radeon_emit(cs
, fui(ds_clear_value
.depth
));
1637 /* Otherwise we need one WRITE_DATA packet per level. */
1638 for (uint32_t l
= 0; l
< level_count
; l
++) {
1639 uint64_t va
= radv_get_ds_clear_value_va(image
, range
->baseMipLevel
+ l
);
1642 if (aspects
== VK_IMAGE_ASPECT_DEPTH_BIT
) {
1643 value
= fui(ds_clear_value
.depth
);
1646 assert(aspects
== VK_IMAGE_ASPECT_STENCIL_BIT
);
1647 value
= ds_clear_value
.stencil
;
1650 radeon_emit(cs
, PKT3(PKT3_WRITE_DATA
, 3, cmd_buffer
->state
.predicating
));
1651 radeon_emit(cs
, S_370_DST_SEL(V_370_MEM
) |
1652 S_370_WR_CONFIRM(1) |
1653 S_370_ENGINE_SEL(V_370_PFP
));
1654 radeon_emit(cs
, va
);
1655 radeon_emit(cs
, va
>> 32);
1656 radeon_emit(cs
, value
);
1662 * Update the TC-compat metadata value for this image.
1665 radv_set_tc_compat_zrange_metadata(struct radv_cmd_buffer
*cmd_buffer
,
1666 struct radv_image
*image
,
1667 const VkImageSubresourceRange
*range
,
1670 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
1672 if (!cmd_buffer
->device
->physical_device
->rad_info
.has_tc_compat_zrange_bug
)
1675 uint64_t va
= radv_get_tc_compat_zrange_va(image
, range
->baseMipLevel
);
1676 uint32_t level_count
= radv_get_levelCount(image
, range
);
1678 radeon_emit(cs
, PKT3(PKT3_WRITE_DATA
, 2 + level_count
, cmd_buffer
->state
.predicating
));
1679 radeon_emit(cs
, S_370_DST_SEL(V_370_MEM
) |
1680 S_370_WR_CONFIRM(1) |
1681 S_370_ENGINE_SEL(V_370_PFP
));
1682 radeon_emit(cs
, va
);
1683 radeon_emit(cs
, va
>> 32);
1685 for (uint32_t l
= 0; l
< level_count
; l
++)
1686 radeon_emit(cs
, value
);
1690 radv_update_tc_compat_zrange_metadata(struct radv_cmd_buffer
*cmd_buffer
,
1691 const struct radv_image_view
*iview
,
1692 VkClearDepthStencilValue ds_clear_value
)
1694 VkImageSubresourceRange range
= {
1695 .aspectMask
= iview
->aspect_mask
,
1696 .baseMipLevel
= iview
->base_mip
,
1697 .levelCount
= iview
->level_count
,
1698 .baseArrayLayer
= iview
->base_layer
,
1699 .layerCount
= iview
->layer_count
,
1703 /* Conditionally set DB_Z_INFO.ZRANGE_PRECISION to 0 when the last
1704 * depth clear value is 0.0f.
1706 cond_val
= ds_clear_value
.depth
== 0.0f
? UINT_MAX
: 0;
1708 radv_set_tc_compat_zrange_metadata(cmd_buffer
, iview
->image
, &range
,
1713 * Update the clear depth/stencil values for this image.
1716 radv_update_ds_clear_metadata(struct radv_cmd_buffer
*cmd_buffer
,
1717 const struct radv_image_view
*iview
,
1718 VkClearDepthStencilValue ds_clear_value
,
1719 VkImageAspectFlags aspects
)
1721 VkImageSubresourceRange range
= {
1722 .aspectMask
= iview
->aspect_mask
,
1723 .baseMipLevel
= iview
->base_mip
,
1724 .levelCount
= iview
->level_count
,
1725 .baseArrayLayer
= iview
->base_layer
,
1726 .layerCount
= iview
->layer_count
,
1728 struct radv_image
*image
= iview
->image
;
1730 assert(radv_image_has_htile(image
));
1732 radv_set_ds_clear_metadata(cmd_buffer
, iview
->image
, &range
,
1733 ds_clear_value
, aspects
);
1735 if (radv_image_is_tc_compat_htile(image
) &&
1736 (aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
)) {
1737 radv_update_tc_compat_zrange_metadata(cmd_buffer
, iview
,
1741 radv_update_bound_fast_clear_ds(cmd_buffer
, iview
, ds_clear_value
,
1746 * Load the clear depth/stencil values from the image's metadata.
1749 radv_load_ds_clear_metadata(struct radv_cmd_buffer
*cmd_buffer
,
1750 const struct radv_image_view
*iview
)
1752 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
1753 const struct radv_image
*image
= iview
->image
;
1754 VkImageAspectFlags aspects
= vk_format_aspects(image
->vk_format
);
1755 uint64_t va
= radv_get_ds_clear_value_va(image
, iview
->base_mip
);
1756 unsigned reg_offset
= 0, reg_count
= 0;
1758 if (!radv_image_has_htile(image
))
1761 if (aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
1767 if (aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
)
1770 uint32_t reg
= R_028028_DB_STENCIL_CLEAR
+ 4 * reg_offset
;
1772 if (cmd_buffer
->device
->physical_device
->rad_info
.has_load_ctx_reg_pkt
) {
1773 radeon_emit(cs
, PKT3(PKT3_LOAD_CONTEXT_REG
, 3, 0));
1774 radeon_emit(cs
, va
);
1775 radeon_emit(cs
, va
>> 32);
1776 radeon_emit(cs
, (reg
- SI_CONTEXT_REG_OFFSET
) >> 2);
1777 radeon_emit(cs
, reg_count
);
1779 radeon_emit(cs
, PKT3(PKT3_COPY_DATA
, 4, 0));
1780 radeon_emit(cs
, COPY_DATA_SRC_SEL(COPY_DATA_SRC_MEM
) |
1781 COPY_DATA_DST_SEL(COPY_DATA_REG
) |
1782 (reg_count
== 2 ? COPY_DATA_COUNT_SEL
: 0));
1783 radeon_emit(cs
, va
);
1784 radeon_emit(cs
, va
>> 32);
1785 radeon_emit(cs
, reg
>> 2);
1788 radeon_emit(cs
, PKT3(PKT3_PFP_SYNC_ME
, 0, 0));
1794 * With DCC some colors don't require CMASK elimination before being
1795 * used as a texture. This sets a predicate value to determine if the
1796 * cmask eliminate is required.
1799 radv_update_fce_metadata(struct radv_cmd_buffer
*cmd_buffer
,
1800 struct radv_image
*image
,
1801 const VkImageSubresourceRange
*range
, bool value
)
1803 uint64_t pred_val
= value
;
1804 uint64_t va
= radv_image_get_fce_pred_va(image
, range
->baseMipLevel
);
1805 uint32_t level_count
= radv_get_levelCount(image
, range
);
1806 uint32_t count
= 2 * level_count
;
1808 assert(radv_dcc_enabled(image
, range
->baseMipLevel
));
1810 radeon_emit(cmd_buffer
->cs
, PKT3(PKT3_WRITE_DATA
, 2 + count
, 0));
1811 radeon_emit(cmd_buffer
->cs
, S_370_DST_SEL(V_370_MEM
) |
1812 S_370_WR_CONFIRM(1) |
1813 S_370_ENGINE_SEL(V_370_PFP
));
1814 radeon_emit(cmd_buffer
->cs
, va
);
1815 radeon_emit(cmd_buffer
->cs
, va
>> 32);
1817 for (uint32_t l
= 0; l
< level_count
; l
++) {
1818 radeon_emit(cmd_buffer
->cs
, pred_val
);
1819 radeon_emit(cmd_buffer
->cs
, pred_val
>> 32);
1824 * Update the DCC predicate to reflect the compression state.
1827 radv_update_dcc_metadata(struct radv_cmd_buffer
*cmd_buffer
,
1828 struct radv_image
*image
,
1829 const VkImageSubresourceRange
*range
, bool value
)
1831 uint64_t pred_val
= value
;
1832 uint64_t va
= radv_image_get_dcc_pred_va(image
, range
->baseMipLevel
);
1833 uint32_t level_count
= radv_get_levelCount(image
, range
);
1834 uint32_t count
= 2 * level_count
;
1836 assert(radv_dcc_enabled(image
, range
->baseMipLevel
));
1838 radeon_emit(cmd_buffer
->cs
, PKT3(PKT3_WRITE_DATA
, 2 + count
, 0));
1839 radeon_emit(cmd_buffer
->cs
, S_370_DST_SEL(V_370_MEM
) |
1840 S_370_WR_CONFIRM(1) |
1841 S_370_ENGINE_SEL(V_370_PFP
));
1842 radeon_emit(cmd_buffer
->cs
, va
);
1843 radeon_emit(cmd_buffer
->cs
, va
>> 32);
1845 for (uint32_t l
= 0; l
< level_count
; l
++) {
1846 radeon_emit(cmd_buffer
->cs
, pred_val
);
1847 radeon_emit(cmd_buffer
->cs
, pred_val
>> 32);
1852 * Update the fast clear color values if the image is bound as a color buffer.
1855 radv_update_bound_fast_clear_color(struct radv_cmd_buffer
*cmd_buffer
,
1856 struct radv_image
*image
,
1858 uint32_t color_values
[2])
1860 const struct radv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
1861 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
1864 if (!cmd_buffer
->state
.attachments
|| !subpass
)
1867 att_idx
= subpass
->color_attachments
[cb_idx
].attachment
;
1868 if (att_idx
== VK_ATTACHMENT_UNUSED
)
1871 if (cmd_buffer
->state
.attachments
[att_idx
].iview
->image
!= image
)
1874 radeon_set_context_reg_seq(cs
, R_028C8C_CB_COLOR0_CLEAR_WORD0
+ cb_idx
* 0x3c, 2);
1875 radeon_emit(cs
, color_values
[0]);
1876 radeon_emit(cs
, color_values
[1]);
1878 cmd_buffer
->state
.context_roll_without_scissor_emitted
= true;
1882 * Set the clear color values to the image's metadata.
1885 radv_set_color_clear_metadata(struct radv_cmd_buffer
*cmd_buffer
,
1886 struct radv_image
*image
,
1887 const VkImageSubresourceRange
*range
,
1888 uint32_t color_values
[2])
1890 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
1891 uint64_t va
= radv_image_get_fast_clear_va(image
, range
->baseMipLevel
);
1892 uint32_t level_count
= radv_get_levelCount(image
, range
);
1893 uint32_t count
= 2 * level_count
;
1895 assert(radv_image_has_cmask(image
) ||
1896 radv_dcc_enabled(image
, range
->baseMipLevel
));
1898 radeon_emit(cs
, PKT3(PKT3_WRITE_DATA
, 2 + count
, cmd_buffer
->state
.predicating
));
1899 radeon_emit(cs
, S_370_DST_SEL(V_370_MEM
) |
1900 S_370_WR_CONFIRM(1) |
1901 S_370_ENGINE_SEL(V_370_PFP
));
1902 radeon_emit(cs
, va
);
1903 radeon_emit(cs
, va
>> 32);
1905 for (uint32_t l
= 0; l
< level_count
; l
++) {
1906 radeon_emit(cs
, color_values
[0]);
1907 radeon_emit(cs
, color_values
[1]);
1912 * Update the clear color values for this image.
1915 radv_update_color_clear_metadata(struct radv_cmd_buffer
*cmd_buffer
,
1916 const struct radv_image_view
*iview
,
1918 uint32_t color_values
[2])
1920 struct radv_image
*image
= iview
->image
;
1921 VkImageSubresourceRange range
= {
1922 .aspectMask
= iview
->aspect_mask
,
1923 .baseMipLevel
= iview
->base_mip
,
1924 .levelCount
= iview
->level_count
,
1925 .baseArrayLayer
= iview
->base_layer
,
1926 .layerCount
= iview
->layer_count
,
1929 assert(radv_image_has_cmask(image
) ||
1930 radv_dcc_enabled(image
, iview
->base_mip
));
1932 radv_set_color_clear_metadata(cmd_buffer
, image
, &range
, color_values
);
1934 radv_update_bound_fast_clear_color(cmd_buffer
, image
, cb_idx
,
1939 * Load the clear color values from the image's metadata.
1942 radv_load_color_clear_metadata(struct radv_cmd_buffer
*cmd_buffer
,
1943 struct radv_image_view
*iview
,
1946 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
1947 struct radv_image
*image
= iview
->image
;
1948 uint64_t va
= radv_image_get_fast_clear_va(image
, iview
->base_mip
);
1950 if (!radv_image_has_cmask(image
) &&
1951 !radv_dcc_enabled(image
, iview
->base_mip
))
1954 uint32_t reg
= R_028C8C_CB_COLOR0_CLEAR_WORD0
+ cb_idx
* 0x3c;
1956 if (cmd_buffer
->device
->physical_device
->rad_info
.has_load_ctx_reg_pkt
) {
1957 radeon_emit(cs
, PKT3(PKT3_LOAD_CONTEXT_REG
, 3, cmd_buffer
->state
.predicating
));
1958 radeon_emit(cs
, va
);
1959 radeon_emit(cs
, va
>> 32);
1960 radeon_emit(cs
, (reg
- SI_CONTEXT_REG_OFFSET
) >> 2);
1963 radeon_emit(cs
, PKT3(PKT3_COPY_DATA
, 4, cmd_buffer
->state
.predicating
));
1964 radeon_emit(cs
, COPY_DATA_SRC_SEL(COPY_DATA_SRC_MEM
) |
1965 COPY_DATA_DST_SEL(COPY_DATA_REG
) |
1966 COPY_DATA_COUNT_SEL
);
1967 radeon_emit(cs
, va
);
1968 radeon_emit(cs
, va
>> 32);
1969 radeon_emit(cs
, reg
>> 2);
1972 radeon_emit(cs
, PKT3(PKT3_PFP_SYNC_ME
, 0, cmd_buffer
->state
.predicating
));
1978 radv_emit_framebuffer_state(struct radv_cmd_buffer
*cmd_buffer
)
1981 struct radv_framebuffer
*framebuffer
= cmd_buffer
->state
.framebuffer
;
1982 const struct radv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
1984 /* this may happen for inherited secondary recording */
1988 for (i
= 0; i
< 8; ++i
) {
1989 if (i
>= subpass
->color_count
|| subpass
->color_attachments
[i
].attachment
== VK_ATTACHMENT_UNUSED
) {
1990 radeon_set_context_reg(cmd_buffer
->cs
, R_028C70_CB_COLOR0_INFO
+ i
* 0x3C,
1991 S_028C70_FORMAT(V_028C70_COLOR_INVALID
));
1995 int idx
= subpass
->color_attachments
[i
].attachment
;
1996 struct radv_image_view
*iview
= cmd_buffer
->state
.attachments
[idx
].iview
;
1997 VkImageLayout layout
= subpass
->color_attachments
[i
].layout
;
1998 bool in_render_loop
= subpass
->color_attachments
[i
].in_render_loop
;
2000 radv_cs_add_buffer(cmd_buffer
->device
->ws
, cmd_buffer
->cs
, iview
->bo
);
2002 assert(iview
->aspect_mask
& (VK_IMAGE_ASPECT_COLOR_BIT
| VK_IMAGE_ASPECT_PLANE_0_BIT
|
2003 VK_IMAGE_ASPECT_PLANE_1_BIT
| VK_IMAGE_ASPECT_PLANE_2_BIT
));
2004 radv_emit_fb_color_state(cmd_buffer
, i
, &cmd_buffer
->state
.attachments
[idx
].cb
, iview
, layout
, in_render_loop
);
2006 radv_load_color_clear_metadata(cmd_buffer
, iview
, i
);
2009 if (subpass
->depth_stencil_attachment
) {
2010 int idx
= subpass
->depth_stencil_attachment
->attachment
;
2011 VkImageLayout layout
= subpass
->depth_stencil_attachment
->layout
;
2012 bool in_render_loop
= subpass
->depth_stencil_attachment
->in_render_loop
;
2013 struct radv_image_view
*iview
= cmd_buffer
->state
.attachments
[idx
].iview
;
2014 struct radv_image
*image
= iview
->image
;
2015 radv_cs_add_buffer(cmd_buffer
->device
->ws
, cmd_buffer
->cs
, cmd_buffer
->state
.attachments
[idx
].iview
->bo
);
2016 ASSERTED
uint32_t queue_mask
= radv_image_queue_family_mask(image
,
2017 cmd_buffer
->queue_family_index
,
2018 cmd_buffer
->queue_family_index
);
2019 /* We currently don't support writing decompressed HTILE */
2020 assert(radv_layout_has_htile(image
, layout
, in_render_loop
, queue_mask
) ==
2021 radv_layout_is_htile_compressed(image
, layout
, in_render_loop
, queue_mask
));
2023 radv_emit_fb_ds_state(cmd_buffer
, &cmd_buffer
->state
.attachments
[idx
].ds
, iview
, layout
, in_render_loop
);
2025 if (cmd_buffer
->state
.attachments
[idx
].ds
.offset_scale
!= cmd_buffer
->state
.offset_scale
) {
2026 cmd_buffer
->state
.dirty
|= RADV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS
;
2027 cmd_buffer
->state
.offset_scale
= cmd_buffer
->state
.attachments
[idx
].ds
.offset_scale
;
2029 radv_load_ds_clear_metadata(cmd_buffer
, iview
);
2031 if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
== GFX9
)
2032 radeon_set_context_reg_seq(cmd_buffer
->cs
, R_028038_DB_Z_INFO
, 2);
2034 radeon_set_context_reg_seq(cmd_buffer
->cs
, R_028040_DB_Z_INFO
, 2);
2036 radeon_emit(cmd_buffer
->cs
, S_028040_FORMAT(V_028040_Z_INVALID
)); /* DB_Z_INFO */
2037 radeon_emit(cmd_buffer
->cs
, S_028044_FORMAT(V_028044_STENCIL_INVALID
)); /* DB_STENCIL_INFO */
2039 radeon_set_context_reg(cmd_buffer
->cs
, R_028208_PA_SC_WINDOW_SCISSOR_BR
,
2040 S_028208_BR_X(framebuffer
->width
) |
2041 S_028208_BR_Y(framebuffer
->height
));
2043 if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX8
) {
2044 bool disable_constant_encode
=
2045 cmd_buffer
->device
->physical_device
->rad_info
.has_dcc_constant_encode
;
2046 enum chip_class chip_class
=
2047 cmd_buffer
->device
->physical_device
->rad_info
.chip_class
;
2048 uint8_t watermark
= chip_class
>= GFX10
? 6 : 4;
2050 radeon_set_context_reg(cmd_buffer
->cs
, R_028424_CB_DCC_CONTROL
,
2051 S_028424_OVERWRITE_COMBINER_MRT_SHARING_DISABLE(chip_class
<= GFX9
) |
2052 S_028424_OVERWRITE_COMBINER_WATERMARK(watermark
) |
2053 S_028424_DISABLE_CONSTANT_ENCODE_REG(disable_constant_encode
));
2056 if (cmd_buffer
->device
->dfsm_allowed
) {
2057 radeon_emit(cmd_buffer
->cs
, PKT3(PKT3_EVENT_WRITE
, 0, 0));
2058 radeon_emit(cmd_buffer
->cs
, EVENT_TYPE(V_028A90_BREAK_BATCH
) | EVENT_INDEX(0));
2061 cmd_buffer
->state
.dirty
&= ~RADV_CMD_DIRTY_FRAMEBUFFER
;
2065 radv_emit_index_buffer(struct radv_cmd_buffer
*cmd_buffer
)
2067 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
2068 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
2070 if (state
->index_type
!= state
->last_index_type
) {
2071 if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
2072 radeon_set_uconfig_reg_idx(cmd_buffer
->device
->physical_device
,
2073 cs
, R_03090C_VGT_INDEX_TYPE
,
2074 2, state
->index_type
);
2076 radeon_emit(cs
, PKT3(PKT3_INDEX_TYPE
, 0, 0));
2077 radeon_emit(cs
, state
->index_type
);
2080 state
->last_index_type
= state
->index_type
;
2083 radeon_emit(cs
, PKT3(PKT3_INDEX_BASE
, 1, 0));
2084 radeon_emit(cs
, state
->index_va
);
2085 radeon_emit(cs
, state
->index_va
>> 32);
2087 radeon_emit(cs
, PKT3(PKT3_INDEX_BUFFER_SIZE
, 0, 0));
2088 radeon_emit(cs
, state
->max_index_count
);
2090 cmd_buffer
->state
.dirty
&= ~RADV_CMD_DIRTY_INDEX_BUFFER
;
2093 void radv_set_db_count_control(struct radv_cmd_buffer
*cmd_buffer
)
2095 bool has_perfect_queries
= cmd_buffer
->state
.perfect_occlusion_queries_enabled
;
2096 struct radv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
2097 uint32_t pa_sc_mode_cntl_1
=
2098 pipeline
? pipeline
->graphics
.ms
.pa_sc_mode_cntl_1
: 0;
2099 uint32_t db_count_control
;
2101 if(!cmd_buffer
->state
.active_occlusion_queries
) {
2102 if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX7
) {
2103 if (G_028A4C_OUT_OF_ORDER_PRIMITIVE_ENABLE(pa_sc_mode_cntl_1
) &&
2104 pipeline
->graphics
.disable_out_of_order_rast_for_occlusion
&&
2105 has_perfect_queries
) {
2106 /* Re-enable out-of-order rasterization if the
2107 * bound pipeline supports it and if it's has
2108 * been disabled before starting any perfect
2109 * occlusion queries.
2111 radeon_set_context_reg(cmd_buffer
->cs
,
2112 R_028A4C_PA_SC_MODE_CNTL_1
,
2116 db_count_control
= S_028004_ZPASS_INCREMENT_DISABLE(1);
2118 const struct radv_subpass
*subpass
= cmd_buffer
->state
.subpass
;
2119 uint32_t sample_rate
= subpass
? util_logbase2(subpass
->max_sample_count
) : 0;
2120 bool gfx10_perfect
= cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX10
&& has_perfect_queries
;
2122 if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX7
) {
2124 S_028004_PERFECT_ZPASS_COUNTS(has_perfect_queries
) |
2125 S_028004_DISABLE_CONSERVATIVE_ZPASS_COUNTS(gfx10_perfect
) |
2126 S_028004_SAMPLE_RATE(sample_rate
) |
2127 S_028004_ZPASS_ENABLE(1) |
2128 S_028004_SLICE_EVEN_ENABLE(1) |
2129 S_028004_SLICE_ODD_ENABLE(1);
2131 if (G_028A4C_OUT_OF_ORDER_PRIMITIVE_ENABLE(pa_sc_mode_cntl_1
) &&
2132 pipeline
->graphics
.disable_out_of_order_rast_for_occlusion
&&
2133 has_perfect_queries
) {
2134 /* If the bound pipeline has enabled
2135 * out-of-order rasterization, we should
2136 * disable it before starting any perfect
2137 * occlusion queries.
2139 pa_sc_mode_cntl_1
&= C_028A4C_OUT_OF_ORDER_PRIMITIVE_ENABLE
;
2141 radeon_set_context_reg(cmd_buffer
->cs
,
2142 R_028A4C_PA_SC_MODE_CNTL_1
,
2146 db_count_control
= S_028004_PERFECT_ZPASS_COUNTS(1) |
2147 S_028004_SAMPLE_RATE(sample_rate
);
2151 radeon_set_context_reg(cmd_buffer
->cs
, R_028004_DB_COUNT_CONTROL
, db_count_control
);
2153 cmd_buffer
->state
.context_roll_without_scissor_emitted
= true;
2157 radv_cmd_buffer_flush_dynamic_state(struct radv_cmd_buffer
*cmd_buffer
)
2159 uint32_t states
= cmd_buffer
->state
.dirty
& cmd_buffer
->state
.emitted_pipeline
->graphics
.needed_dynamic_state
;
2161 if (states
& (RADV_CMD_DIRTY_DYNAMIC_VIEWPORT
))
2162 radv_emit_viewport(cmd_buffer
);
2164 if (states
& (RADV_CMD_DIRTY_DYNAMIC_SCISSOR
| RADV_CMD_DIRTY_DYNAMIC_VIEWPORT
) &&
2165 !cmd_buffer
->device
->physical_device
->rad_info
.has_gfx9_scissor_bug
)
2166 radv_emit_scissor(cmd_buffer
);
2168 if (states
& RADV_CMD_DIRTY_DYNAMIC_LINE_WIDTH
)
2169 radv_emit_line_width(cmd_buffer
);
2171 if (states
& RADV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS
)
2172 radv_emit_blend_constants(cmd_buffer
);
2174 if (states
& (RADV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE
|
2175 RADV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK
|
2176 RADV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK
))
2177 radv_emit_stencil(cmd_buffer
);
2179 if (states
& RADV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS
)
2180 radv_emit_depth_bounds(cmd_buffer
);
2182 if (states
& RADV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS
)
2183 radv_emit_depth_bias(cmd_buffer
);
2185 if (states
& RADV_CMD_DIRTY_DYNAMIC_DISCARD_RECTANGLE
)
2186 radv_emit_discard_rectangle(cmd_buffer
);
2188 if (states
& RADV_CMD_DIRTY_DYNAMIC_SAMPLE_LOCATIONS
)
2189 radv_emit_sample_locations(cmd_buffer
);
2191 cmd_buffer
->state
.dirty
&= ~states
;
2195 radv_flush_push_descriptors(struct radv_cmd_buffer
*cmd_buffer
,
2196 VkPipelineBindPoint bind_point
)
2198 struct radv_descriptor_state
*descriptors_state
=
2199 radv_get_descriptors_state(cmd_buffer
, bind_point
);
2200 struct radv_descriptor_set
*set
= &descriptors_state
->push_set
.set
;
2203 if (!radv_cmd_buffer_upload_data(cmd_buffer
, set
->size
, 32,
2208 set
->va
= radv_buffer_get_va(cmd_buffer
->upload
.upload_bo
);
2209 set
->va
+= bo_offset
;
2213 radv_flush_indirect_descriptor_sets(struct radv_cmd_buffer
*cmd_buffer
,
2214 VkPipelineBindPoint bind_point
)
2216 struct radv_descriptor_state
*descriptors_state
=
2217 radv_get_descriptors_state(cmd_buffer
, bind_point
);
2218 uint32_t size
= MAX_SETS
* 4;
2222 if (!radv_cmd_buffer_upload_alloc(cmd_buffer
, size
,
2223 256, &offset
, &ptr
))
2226 for (unsigned i
= 0; i
< MAX_SETS
; i
++) {
2227 uint32_t *uptr
= ((uint32_t *)ptr
) + i
;
2228 uint64_t set_va
= 0;
2229 struct radv_descriptor_set
*set
= descriptors_state
->sets
[i
];
2230 if (descriptors_state
->valid
& (1u << i
))
2232 uptr
[0] = set_va
& 0xffffffff;
2235 uint64_t va
= radv_buffer_get_va(cmd_buffer
->upload
.upload_bo
);
2238 if (cmd_buffer
->state
.pipeline
) {
2239 if (cmd_buffer
->state
.pipeline
->shaders
[MESA_SHADER_VERTEX
])
2240 radv_emit_userdata_address(cmd_buffer
, cmd_buffer
->state
.pipeline
, MESA_SHADER_VERTEX
,
2241 AC_UD_INDIRECT_DESCRIPTOR_SETS
, va
);
2243 if (cmd_buffer
->state
.pipeline
->shaders
[MESA_SHADER_FRAGMENT
])
2244 radv_emit_userdata_address(cmd_buffer
, cmd_buffer
->state
.pipeline
, MESA_SHADER_FRAGMENT
,
2245 AC_UD_INDIRECT_DESCRIPTOR_SETS
, va
);
2247 if (radv_pipeline_has_gs(cmd_buffer
->state
.pipeline
))
2248 radv_emit_userdata_address(cmd_buffer
, cmd_buffer
->state
.pipeline
, MESA_SHADER_GEOMETRY
,
2249 AC_UD_INDIRECT_DESCRIPTOR_SETS
, va
);
2251 if (radv_pipeline_has_tess(cmd_buffer
->state
.pipeline
))
2252 radv_emit_userdata_address(cmd_buffer
, cmd_buffer
->state
.pipeline
, MESA_SHADER_TESS_CTRL
,
2253 AC_UD_INDIRECT_DESCRIPTOR_SETS
, va
);
2255 if (radv_pipeline_has_tess(cmd_buffer
->state
.pipeline
))
2256 radv_emit_userdata_address(cmd_buffer
, cmd_buffer
->state
.pipeline
, MESA_SHADER_TESS_EVAL
,
2257 AC_UD_INDIRECT_DESCRIPTOR_SETS
, va
);
2260 if (cmd_buffer
->state
.compute_pipeline
)
2261 radv_emit_userdata_address(cmd_buffer
, cmd_buffer
->state
.compute_pipeline
, MESA_SHADER_COMPUTE
,
2262 AC_UD_INDIRECT_DESCRIPTOR_SETS
, va
);
2266 radv_flush_descriptors(struct radv_cmd_buffer
*cmd_buffer
,
2267 VkShaderStageFlags stages
)
2269 VkPipelineBindPoint bind_point
= stages
& VK_SHADER_STAGE_COMPUTE_BIT
?
2270 VK_PIPELINE_BIND_POINT_COMPUTE
:
2271 VK_PIPELINE_BIND_POINT_GRAPHICS
;
2272 struct radv_descriptor_state
*descriptors_state
=
2273 radv_get_descriptors_state(cmd_buffer
, bind_point
);
2274 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
2275 bool flush_indirect_descriptors
;
2277 if (!descriptors_state
->dirty
)
2280 if (descriptors_state
->push_dirty
)
2281 radv_flush_push_descriptors(cmd_buffer
, bind_point
);
2283 flush_indirect_descriptors
=
2284 (bind_point
== VK_PIPELINE_BIND_POINT_GRAPHICS
&&
2285 state
->pipeline
&& state
->pipeline
->need_indirect_descriptor_sets
) ||
2286 (bind_point
== VK_PIPELINE_BIND_POINT_COMPUTE
&&
2287 state
->compute_pipeline
&& state
->compute_pipeline
->need_indirect_descriptor_sets
);
2289 if (flush_indirect_descriptors
)
2290 radv_flush_indirect_descriptor_sets(cmd_buffer
, bind_point
);
2292 ASSERTED
unsigned cdw_max
= radeon_check_space(cmd_buffer
->device
->ws
,
2294 MAX_SETS
* MESA_SHADER_STAGES
* 4);
2296 if (cmd_buffer
->state
.pipeline
) {
2297 radv_foreach_stage(stage
, stages
) {
2298 if (!cmd_buffer
->state
.pipeline
->shaders
[stage
])
2301 radv_emit_descriptor_pointers(cmd_buffer
,
2302 cmd_buffer
->state
.pipeline
,
2303 descriptors_state
, stage
);
2307 if (cmd_buffer
->state
.compute_pipeline
&&
2308 (stages
& VK_SHADER_STAGE_COMPUTE_BIT
)) {
2309 radv_emit_descriptor_pointers(cmd_buffer
,
2310 cmd_buffer
->state
.compute_pipeline
,
2312 MESA_SHADER_COMPUTE
);
2315 descriptors_state
->dirty
= 0;
2316 descriptors_state
->push_dirty
= false;
2318 assert(cmd_buffer
->cs
->cdw
<= cdw_max
);
2320 if (unlikely(cmd_buffer
->device
->trace_bo
))
2321 radv_save_descriptors(cmd_buffer
, bind_point
);
2325 radv_flush_constants(struct radv_cmd_buffer
*cmd_buffer
,
2326 VkShaderStageFlags stages
)
2328 struct radv_pipeline
*pipeline
= stages
& VK_SHADER_STAGE_COMPUTE_BIT
2329 ? cmd_buffer
->state
.compute_pipeline
2330 : cmd_buffer
->state
.pipeline
;
2331 VkPipelineBindPoint bind_point
= stages
& VK_SHADER_STAGE_COMPUTE_BIT
?
2332 VK_PIPELINE_BIND_POINT_COMPUTE
:
2333 VK_PIPELINE_BIND_POINT_GRAPHICS
;
2334 struct radv_descriptor_state
*descriptors_state
=
2335 radv_get_descriptors_state(cmd_buffer
, bind_point
);
2336 struct radv_pipeline_layout
*layout
= pipeline
->layout
;
2337 struct radv_shader_variant
*shader
, *prev_shader
;
2338 bool need_push_constants
= false;
2343 stages
&= cmd_buffer
->push_constant_stages
;
2345 (!layout
->push_constant_size
&& !layout
->dynamic_offset_count
))
2348 radv_foreach_stage(stage
, stages
) {
2349 shader
= radv_get_shader(pipeline
, stage
);
2353 need_push_constants
|= shader
->info
.loads_push_constants
;
2354 need_push_constants
|= shader
->info
.loads_dynamic_offsets
;
2356 uint8_t base
= shader
->info
.base_inline_push_consts
;
2357 uint8_t count
= shader
->info
.num_inline_push_consts
;
2359 radv_emit_inline_push_consts(cmd_buffer
, pipeline
, stage
,
2360 AC_UD_INLINE_PUSH_CONSTANTS
,
2362 (uint32_t *)&cmd_buffer
->push_constants
[base
* 4]);
2365 if (need_push_constants
) {
2366 if (!radv_cmd_buffer_upload_alloc(cmd_buffer
, layout
->push_constant_size
+
2367 16 * layout
->dynamic_offset_count
,
2368 256, &offset
, &ptr
))
2371 memcpy(ptr
, cmd_buffer
->push_constants
, layout
->push_constant_size
);
2372 memcpy((char*)ptr
+ layout
->push_constant_size
,
2373 descriptors_state
->dynamic_buffers
,
2374 16 * layout
->dynamic_offset_count
);
2376 va
= radv_buffer_get_va(cmd_buffer
->upload
.upload_bo
);
2379 ASSERTED
unsigned cdw_max
=
2380 radeon_check_space(cmd_buffer
->device
->ws
,
2381 cmd_buffer
->cs
, MESA_SHADER_STAGES
* 4);
2384 radv_foreach_stage(stage
, stages
) {
2385 shader
= radv_get_shader(pipeline
, stage
);
2387 /* Avoid redundantly emitting the address for merged stages. */
2388 if (shader
&& shader
!= prev_shader
) {
2389 radv_emit_userdata_address(cmd_buffer
, pipeline
, stage
,
2390 AC_UD_PUSH_CONSTANTS
, va
);
2392 prev_shader
= shader
;
2395 assert(cmd_buffer
->cs
->cdw
<= cdw_max
);
2398 cmd_buffer
->push_constant_stages
&= ~stages
;
2402 radv_flush_vertex_descriptors(struct radv_cmd_buffer
*cmd_buffer
,
2403 bool pipeline_is_dirty
)
2405 if ((pipeline_is_dirty
||
2406 (cmd_buffer
->state
.dirty
& RADV_CMD_DIRTY_VERTEX_BUFFER
)) &&
2407 cmd_buffer
->state
.pipeline
->num_vertex_bindings
&&
2408 radv_get_shader(cmd_buffer
->state
.pipeline
, MESA_SHADER_VERTEX
)->info
.vs
.has_vertex_buffers
) {
2412 uint32_t count
= cmd_buffer
->state
.pipeline
->num_vertex_bindings
;
2415 /* allocate some descriptor state for vertex buffers */
2416 if (!radv_cmd_buffer_upload_alloc(cmd_buffer
, count
* 16, 256,
2417 &vb_offset
, &vb_ptr
))
2420 for (i
= 0; i
< count
; i
++) {
2421 uint32_t *desc
= &((uint32_t *)vb_ptr
)[i
* 4];
2423 struct radv_buffer
*buffer
= cmd_buffer
->vertex_bindings
[i
].buffer
;
2424 uint32_t stride
= cmd_buffer
->state
.pipeline
->binding_stride
[i
];
2425 unsigned num_records
;
2430 va
= radv_buffer_get_va(buffer
->bo
);
2432 offset
= cmd_buffer
->vertex_bindings
[i
].offset
;
2433 va
+= offset
+ buffer
->offset
;
2435 num_records
= buffer
->size
- offset
;
2436 if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
!= GFX8
&& stride
)
2437 num_records
/= stride
;
2440 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) | S_008F04_STRIDE(stride
);
2441 desc
[2] = num_records
;
2442 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
2443 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
2444 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
2445 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
2447 if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX10
) {
2448 /* OOB_SELECT chooses the out-of-bounds check:
2449 * - 1: index >= NUM_RECORDS (Structured)
2450 * - 3: offset >= NUM_RECORDS (Raw)
2452 int oob_select
= stride
? V_008F0C_OOB_SELECT_STRUCTURED
: V_008F0C_OOB_SELECT_RAW
;
2454 desc
[3] |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_UINT
) |
2455 S_008F0C_OOB_SELECT(oob_select
) |
2456 S_008F0C_RESOURCE_LEVEL(1);
2458 desc
[3] |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_UINT
) |
2459 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
2463 va
= radv_buffer_get_va(cmd_buffer
->upload
.upload_bo
);
2466 radv_emit_userdata_address(cmd_buffer
, cmd_buffer
->state
.pipeline
, MESA_SHADER_VERTEX
,
2467 AC_UD_VS_VERTEX_BUFFERS
, va
);
2469 cmd_buffer
->state
.vb_va
= va
;
2470 cmd_buffer
->state
.vb_size
= count
* 16;
2471 cmd_buffer
->state
.prefetch_L2_mask
|= RADV_PREFETCH_VBO_DESCRIPTORS
;
2473 cmd_buffer
->state
.dirty
&= ~RADV_CMD_DIRTY_VERTEX_BUFFER
;
2477 radv_emit_streamout_buffers(struct radv_cmd_buffer
*cmd_buffer
, uint64_t va
)
2479 struct radv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
2480 struct radv_userdata_info
*loc
;
2483 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; ++stage
) {
2484 if (!radv_get_shader(pipeline
, stage
))
2487 loc
= radv_lookup_user_sgpr(pipeline
, stage
,
2488 AC_UD_STREAMOUT_BUFFERS
);
2489 if (loc
->sgpr_idx
== -1)
2492 base_reg
= pipeline
->user_data_0
[stage
];
2494 radv_emit_shader_pointer(cmd_buffer
->device
, cmd_buffer
->cs
,
2495 base_reg
+ loc
->sgpr_idx
* 4, va
, false);
2498 if (radv_pipeline_has_gs_copy_shader(pipeline
)) {
2499 loc
= &pipeline
->gs_copy_shader
->info
.user_sgprs_locs
.shader_data
[AC_UD_STREAMOUT_BUFFERS
];
2500 if (loc
->sgpr_idx
!= -1) {
2501 base_reg
= R_00B130_SPI_SHADER_USER_DATA_VS_0
;
2503 radv_emit_shader_pointer(cmd_buffer
->device
, cmd_buffer
->cs
,
2504 base_reg
+ loc
->sgpr_idx
* 4, va
, false);
2510 radv_flush_streamout_descriptors(struct radv_cmd_buffer
*cmd_buffer
)
2512 if (cmd_buffer
->state
.dirty
& RADV_CMD_DIRTY_STREAMOUT_BUFFER
) {
2513 struct radv_streamout_binding
*sb
= cmd_buffer
->streamout_bindings
;
2514 struct radv_streamout_state
*so
= &cmd_buffer
->state
.streamout
;
2519 /* Allocate some descriptor state for streamout buffers. */
2520 if (!radv_cmd_buffer_upload_alloc(cmd_buffer
,
2521 MAX_SO_BUFFERS
* 16, 256,
2522 &so_offset
, &so_ptr
))
2525 for (uint32_t i
= 0; i
< MAX_SO_BUFFERS
; i
++) {
2526 struct radv_buffer
*buffer
= sb
[i
].buffer
;
2527 uint32_t *desc
= &((uint32_t *)so_ptr
)[i
* 4];
2529 if (!(so
->enabled_mask
& (1 << i
)))
2532 va
= radv_buffer_get_va(buffer
->bo
) + buffer
->offset
;
2536 /* Set the descriptor.
2538 * On GFX8, the format must be non-INVALID, otherwise
2539 * the buffer will be considered not bound and store
2540 * instructions will be no-ops.
2542 uint32_t size
= 0xffffffff;
2544 /* Compute the correct buffer size for NGG streamout
2545 * because it's used to determine the max emit per
2548 if (cmd_buffer
->device
->physical_device
->use_ngg_streamout
)
2549 size
= buffer
->size
- sb
[i
].offset
;
2552 desc
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32);
2554 desc
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
2555 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
2556 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
2557 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
2559 if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX10
) {
2560 desc
[3] |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
2561 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW
) |
2562 S_008F0C_RESOURCE_LEVEL(1);
2564 desc
[3] |= S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
2568 va
= radv_buffer_get_va(cmd_buffer
->upload
.upload_bo
);
2571 radv_emit_streamout_buffers(cmd_buffer
, va
);
2574 cmd_buffer
->state
.dirty
&= ~RADV_CMD_DIRTY_STREAMOUT_BUFFER
;
2578 radv_flush_ngg_gs_state(struct radv_cmd_buffer
*cmd_buffer
)
2580 struct radv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
2581 struct radv_userdata_info
*loc
;
2582 uint32_t ngg_gs_state
= 0;
2585 if (!radv_pipeline_has_gs(pipeline
) ||
2586 !radv_pipeline_has_ngg(pipeline
))
2589 /* By default NGG GS queries are disabled but they are enabled if the
2590 * command buffer has active GDS queries or if it's a secondary command
2591 * buffer that inherits the number of generated primitives.
2593 if (cmd_buffer
->state
.active_pipeline_gds_queries
||
2594 (cmd_buffer
->state
.inherited_pipeline_statistics
& VK_QUERY_PIPELINE_STATISTIC_GEOMETRY_SHADER_PRIMITIVES_BIT
))
2597 loc
= radv_lookup_user_sgpr(pipeline
, MESA_SHADER_GEOMETRY
,
2598 AC_UD_NGG_GS_STATE
);
2599 base_reg
= pipeline
->user_data_0
[MESA_SHADER_GEOMETRY
];
2600 assert(loc
->sgpr_idx
!= -1);
2602 radeon_set_sh_reg(cmd_buffer
->cs
, base_reg
+ loc
->sgpr_idx
* 4,
2607 radv_upload_graphics_shader_descriptors(struct radv_cmd_buffer
*cmd_buffer
, bool pipeline_is_dirty
)
2609 radv_flush_vertex_descriptors(cmd_buffer
, pipeline_is_dirty
);
2610 radv_flush_streamout_descriptors(cmd_buffer
);
2611 radv_flush_descriptors(cmd_buffer
, VK_SHADER_STAGE_ALL_GRAPHICS
);
2612 radv_flush_constants(cmd_buffer
, VK_SHADER_STAGE_ALL_GRAPHICS
);
2613 radv_flush_ngg_gs_state(cmd_buffer
);
2616 struct radv_draw_info
{
2618 * Number of vertices.
2623 * Index of the first vertex.
2625 int32_t vertex_offset
;
2628 * First instance id.
2630 uint32_t first_instance
;
2633 * Number of instances.
2635 uint32_t instance_count
;
2638 * First index (indexed draws only).
2640 uint32_t first_index
;
2643 * Whether it's an indexed draw.
2648 * Indirect draw parameters resource.
2650 struct radv_buffer
*indirect
;
2651 uint64_t indirect_offset
;
2655 * Draw count parameters resource.
2657 struct radv_buffer
*count_buffer
;
2658 uint64_t count_buffer_offset
;
2661 * Stream output parameters resource.
2663 struct radv_buffer
*strmout_buffer
;
2664 uint64_t strmout_buffer_offset
;
2668 radv_get_primitive_reset_index(struct radv_cmd_buffer
*cmd_buffer
)
2670 switch (cmd_buffer
->state
.index_type
) {
2671 case V_028A7C_VGT_INDEX_8
:
2673 case V_028A7C_VGT_INDEX_16
:
2675 case V_028A7C_VGT_INDEX_32
:
2678 unreachable("invalid index type");
2683 si_emit_ia_multi_vgt_param(struct radv_cmd_buffer
*cmd_buffer
,
2684 bool instanced_draw
, bool indirect_draw
,
2685 bool count_from_stream_output
,
2686 uint32_t draw_vertex_count
)
2688 struct radeon_info
*info
= &cmd_buffer
->device
->physical_device
->rad_info
;
2689 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
2690 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
2691 unsigned ia_multi_vgt_param
;
2693 ia_multi_vgt_param
=
2694 si_get_ia_multi_vgt_param(cmd_buffer
, instanced_draw
,
2696 count_from_stream_output
,
2699 if (state
->last_ia_multi_vgt_param
!= ia_multi_vgt_param
) {
2700 if (info
->chip_class
== GFX9
) {
2701 radeon_set_uconfig_reg_idx(cmd_buffer
->device
->physical_device
,
2703 R_030960_IA_MULTI_VGT_PARAM
,
2704 4, ia_multi_vgt_param
);
2705 } else if (info
->chip_class
>= GFX7
) {
2706 radeon_set_context_reg_idx(cs
,
2707 R_028AA8_IA_MULTI_VGT_PARAM
,
2708 1, ia_multi_vgt_param
);
2710 radeon_set_context_reg(cs
, R_028AA8_IA_MULTI_VGT_PARAM
,
2711 ia_multi_vgt_param
);
2713 state
->last_ia_multi_vgt_param
= ia_multi_vgt_param
;
2718 radv_emit_draw_registers(struct radv_cmd_buffer
*cmd_buffer
,
2719 const struct radv_draw_info
*draw_info
)
2721 struct radeon_info
*info
= &cmd_buffer
->device
->physical_device
->rad_info
;
2722 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
2723 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
2724 int32_t primitive_reset_en
;
2727 if (info
->chip_class
< GFX10
) {
2728 si_emit_ia_multi_vgt_param(cmd_buffer
, draw_info
->instance_count
> 1,
2729 draw_info
->indirect
,
2730 !!draw_info
->strmout_buffer
,
2731 draw_info
->indirect
? 0 : draw_info
->count
);
2734 /* Primitive restart. */
2735 primitive_reset_en
=
2736 draw_info
->indexed
&& state
->pipeline
->graphics
.prim_restart_enable
;
2738 if (primitive_reset_en
!= state
->last_primitive_reset_en
) {
2739 state
->last_primitive_reset_en
= primitive_reset_en
;
2740 if (info
->chip_class
>= GFX9
) {
2741 radeon_set_uconfig_reg(cs
,
2742 R_03092C_VGT_MULTI_PRIM_IB_RESET_EN
,
2743 primitive_reset_en
);
2745 radeon_set_context_reg(cs
,
2746 R_028A94_VGT_MULTI_PRIM_IB_RESET_EN
,
2747 primitive_reset_en
);
2751 if (primitive_reset_en
) {
2752 uint32_t primitive_reset_index
=
2753 radv_get_primitive_reset_index(cmd_buffer
);
2755 if (primitive_reset_index
!= state
->last_primitive_reset_index
) {
2756 radeon_set_context_reg(cs
,
2757 R_02840C_VGT_MULTI_PRIM_IB_RESET_INDX
,
2758 primitive_reset_index
);
2759 state
->last_primitive_reset_index
= primitive_reset_index
;
2763 if (draw_info
->strmout_buffer
) {
2764 uint64_t va
= radv_buffer_get_va(draw_info
->strmout_buffer
->bo
);
2766 va
+= draw_info
->strmout_buffer
->offset
+
2767 draw_info
->strmout_buffer_offset
;
2769 radeon_set_context_reg(cs
, R_028B30_VGT_STRMOUT_DRAW_OPAQUE_VERTEX_STRIDE
,
2772 radeon_emit(cs
, PKT3(PKT3_COPY_DATA
, 4, 0));
2773 radeon_emit(cs
, COPY_DATA_SRC_SEL(COPY_DATA_SRC_MEM
) |
2774 COPY_DATA_DST_SEL(COPY_DATA_REG
) |
2775 COPY_DATA_WR_CONFIRM
);
2776 radeon_emit(cs
, va
);
2777 radeon_emit(cs
, va
>> 32);
2778 radeon_emit(cs
, R_028B2C_VGT_STRMOUT_DRAW_OPAQUE_BUFFER_FILLED_SIZE
>> 2);
2779 radeon_emit(cs
, 0); /* unused */
2781 radv_cs_add_buffer(cmd_buffer
->device
->ws
, cs
, draw_info
->strmout_buffer
->bo
);
2785 static void radv_stage_flush(struct radv_cmd_buffer
*cmd_buffer
,
2786 VkPipelineStageFlags src_stage_mask
)
2788 if (src_stage_mask
& (VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT
|
2789 VK_PIPELINE_STAGE_TRANSFER_BIT
|
2790 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT
|
2791 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
)) {
2792 cmd_buffer
->state
.flush_bits
|= RADV_CMD_FLAG_CS_PARTIAL_FLUSH
;
2795 if (src_stage_mask
& (VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
|
2796 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT
|
2797 VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT
|
2798 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT
|
2799 VK_PIPELINE_STAGE_TRANSFER_BIT
|
2800 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT
|
2801 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT
|
2802 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
)) {
2803 cmd_buffer
->state
.flush_bits
|= RADV_CMD_FLAG_PS_PARTIAL_FLUSH
;
2804 } else if (src_stage_mask
& (VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT
|
2805 VK_PIPELINE_STAGE_VERTEX_INPUT_BIT
|
2806 VK_PIPELINE_STAGE_VERTEX_SHADER_BIT
|
2807 VK_PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT
|
2808 VK_PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT
|
2809 VK_PIPELINE_STAGE_GEOMETRY_SHADER_BIT
|
2810 VK_PIPELINE_STAGE_TRANSFORM_FEEDBACK_BIT_EXT
)) {
2811 cmd_buffer
->state
.flush_bits
|= RADV_CMD_FLAG_VS_PARTIAL_FLUSH
;
2815 static enum radv_cmd_flush_bits
2816 radv_src_access_flush(struct radv_cmd_buffer
*cmd_buffer
,
2817 VkAccessFlags src_flags
,
2818 struct radv_image
*image
)
2820 bool flush_CB_meta
= true, flush_DB_meta
= true;
2821 enum radv_cmd_flush_bits flush_bits
= 0;
2825 if (!radv_image_has_CB_metadata(image
))
2826 flush_CB_meta
= false;
2827 if (!radv_image_has_htile(image
))
2828 flush_DB_meta
= false;
2831 for_each_bit(b
, src_flags
) {
2832 switch ((VkAccessFlagBits
)(1 << b
)) {
2833 case VK_ACCESS_SHADER_WRITE_BIT
:
2834 case VK_ACCESS_TRANSFORM_FEEDBACK_WRITE_BIT_EXT
:
2835 case VK_ACCESS_TRANSFORM_FEEDBACK_COUNTER_WRITE_BIT_EXT
:
2836 flush_bits
|= RADV_CMD_FLAG_WB_L2
;
2838 case VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT
:
2839 flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_CB
;
2841 flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_CB_META
;
2843 case VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT
:
2844 flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_DB
;
2846 flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_DB_META
;
2848 case VK_ACCESS_TRANSFER_WRITE_BIT
:
2849 flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_CB
|
2850 RADV_CMD_FLAG_FLUSH_AND_INV_DB
|
2851 RADV_CMD_FLAG_INV_L2
;
2854 flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_CB_META
;
2856 flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_DB_META
;
2865 static enum radv_cmd_flush_bits
2866 radv_dst_access_flush(struct radv_cmd_buffer
*cmd_buffer
,
2867 VkAccessFlags dst_flags
,
2868 struct radv_image
*image
)
2870 bool flush_CB_meta
= true, flush_DB_meta
= true;
2871 enum radv_cmd_flush_bits flush_bits
= 0;
2872 bool flush_CB
= true, flush_DB
= true;
2873 bool image_is_coherent
= false;
2877 if (!(image
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
)) {
2882 if (!radv_image_has_CB_metadata(image
))
2883 flush_CB_meta
= false;
2884 if (!radv_image_has_htile(image
))
2885 flush_DB_meta
= false;
2887 /* TODO: implement shader coherent for GFX10 */
2889 if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
== GFX9
) {
2890 if (image
->info
.samples
== 1 &&
2891 (image
->usage
& (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
|
2892 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
)) &&
2893 !vk_format_is_stencil(image
->vk_format
)) {
2894 /* Single-sample color and single-sample depth
2895 * (not stencil) are coherent with shaders on
2898 image_is_coherent
= true;
2903 for_each_bit(b
, dst_flags
) {
2904 switch ((VkAccessFlagBits
)(1 << b
)) {
2905 case VK_ACCESS_INDIRECT_COMMAND_READ_BIT
:
2906 case VK_ACCESS_INDEX_READ_BIT
:
2907 case VK_ACCESS_TRANSFORM_FEEDBACK_COUNTER_WRITE_BIT_EXT
:
2909 case VK_ACCESS_UNIFORM_READ_BIT
:
2910 flush_bits
|= RADV_CMD_FLAG_INV_VCACHE
| RADV_CMD_FLAG_INV_SCACHE
;
2912 case VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT
:
2913 case VK_ACCESS_TRANSFER_READ_BIT
:
2914 case VK_ACCESS_INPUT_ATTACHMENT_READ_BIT
:
2915 flush_bits
|= RADV_CMD_FLAG_INV_VCACHE
|
2916 RADV_CMD_FLAG_INV_L2
;
2918 case VK_ACCESS_SHADER_READ_BIT
:
2919 flush_bits
|= RADV_CMD_FLAG_INV_VCACHE
;
2920 /* Unlike LLVM, ACO uses SMEM for SSBOs and we have to
2921 * invalidate the scalar cache. */
2922 if (cmd_buffer
->device
->physical_device
->use_aco
&&
2923 cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX8
)
2924 flush_bits
|= RADV_CMD_FLAG_INV_SCACHE
;
2926 if (!image_is_coherent
)
2927 flush_bits
|= RADV_CMD_FLAG_INV_L2
;
2929 case VK_ACCESS_COLOR_ATTACHMENT_READ_BIT
:
2931 flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_CB
;
2933 flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_CB_META
;
2935 case VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT
:
2937 flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_DB
;
2939 flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_DB_META
;
2948 void radv_subpass_barrier(struct radv_cmd_buffer
*cmd_buffer
,
2949 const struct radv_subpass_barrier
*barrier
)
2951 cmd_buffer
->state
.flush_bits
|= radv_src_access_flush(cmd_buffer
, barrier
->src_access_mask
,
2953 radv_stage_flush(cmd_buffer
, barrier
->src_stage_mask
);
2954 cmd_buffer
->state
.flush_bits
|= radv_dst_access_flush(cmd_buffer
, barrier
->dst_access_mask
,
2959 radv_get_subpass_id(struct radv_cmd_buffer
*cmd_buffer
)
2961 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
2962 uint32_t subpass_id
= state
->subpass
- state
->pass
->subpasses
;
2964 /* The id of this subpass shouldn't exceed the number of subpasses in
2965 * this render pass minus 1.
2967 assert(subpass_id
< state
->pass
->subpass_count
);
2971 static struct radv_sample_locations_state
*
2972 radv_get_attachment_sample_locations(struct radv_cmd_buffer
*cmd_buffer
,
2976 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
2977 uint32_t subpass_id
= radv_get_subpass_id(cmd_buffer
);
2978 struct radv_image_view
*view
= state
->attachments
[att_idx
].iview
;
2980 if (view
->image
->info
.samples
== 1)
2983 if (state
->pass
->attachments
[att_idx
].first_subpass_idx
== subpass_id
) {
2984 /* Return the initial sample locations if this is the initial
2985 * layout transition of the given subpass attachemnt.
2987 if (state
->attachments
[att_idx
].sample_location
.count
> 0)
2988 return &state
->attachments
[att_idx
].sample_location
;
2990 /* Otherwise return the subpass sample locations if defined. */
2991 if (state
->subpass_sample_locs
) {
2992 /* Because the driver sets the current subpass before
2993 * initial layout transitions, we should use the sample
2994 * locations from the previous subpass to avoid an
2995 * off-by-one problem. Otherwise, use the sample
2996 * locations for the current subpass for final layout
3002 for (uint32_t i
= 0; i
< state
->num_subpass_sample_locs
; i
++) {
3003 if (state
->subpass_sample_locs
[i
].subpass_idx
== subpass_id
)
3004 return &state
->subpass_sample_locs
[i
].sample_location
;
3012 static void radv_handle_subpass_image_transition(struct radv_cmd_buffer
*cmd_buffer
,
3013 struct radv_subpass_attachment att
,
3016 unsigned idx
= att
.attachment
;
3017 struct radv_image_view
*view
= cmd_buffer
->state
.attachments
[idx
].iview
;
3018 struct radv_sample_locations_state
*sample_locs
;
3019 VkImageSubresourceRange range
;
3020 range
.aspectMask
= view
->aspect_mask
;
3021 range
.baseMipLevel
= view
->base_mip
;
3022 range
.levelCount
= 1;
3023 range
.baseArrayLayer
= view
->base_layer
;
3024 range
.layerCount
= cmd_buffer
->state
.framebuffer
->layers
;
3026 if (cmd_buffer
->state
.subpass
->view_mask
) {
3027 /* If the current subpass uses multiview, the driver might have
3028 * performed a fast color/depth clear to the whole image
3029 * (including all layers). To make sure the driver will
3030 * decompress the image correctly (if needed), we have to
3031 * account for the "real" number of layers. If the view mask is
3032 * sparse, this will decompress more layers than needed.
3034 range
.layerCount
= util_last_bit(cmd_buffer
->state
.subpass
->view_mask
);
3037 /* Get the subpass sample locations for the given attachment, if NULL
3038 * is returned the driver will use the default HW locations.
3040 sample_locs
= radv_get_attachment_sample_locations(cmd_buffer
, idx
,
3043 /* Determine if the subpass uses separate depth/stencil layouts. */
3044 bool uses_separate_depth_stencil_layouts
= false;
3045 if ((cmd_buffer
->state
.attachments
[idx
].current_layout
!=
3046 cmd_buffer
->state
.attachments
[idx
].current_stencil_layout
) ||
3047 (att
.layout
!= att
.stencil_layout
)) {
3048 uses_separate_depth_stencil_layouts
= true;
3051 /* For separate layouts, perform depth and stencil transitions
3054 if (uses_separate_depth_stencil_layouts
&&
3055 (range
.aspectMask
== (VK_IMAGE_ASPECT_DEPTH_BIT
|
3056 VK_IMAGE_ASPECT_STENCIL_BIT
))) {
3057 /* Depth-only transitions. */
3058 range
.aspectMask
= VK_IMAGE_ASPECT_DEPTH_BIT
;
3059 radv_handle_image_transition(cmd_buffer
,
3061 cmd_buffer
->state
.attachments
[idx
].current_layout
,
3062 cmd_buffer
->state
.attachments
[idx
].current_in_render_loop
,
3063 att
.layout
, att
.in_render_loop
,
3064 0, 0, &range
, sample_locs
);
3066 /* Stencil-only transitions. */
3067 range
.aspectMask
= VK_IMAGE_ASPECT_STENCIL_BIT
;
3068 radv_handle_image_transition(cmd_buffer
,
3070 cmd_buffer
->state
.attachments
[idx
].current_stencil_layout
,
3071 cmd_buffer
->state
.attachments
[idx
].current_in_render_loop
,
3072 att
.stencil_layout
, att
.in_render_loop
,
3073 0, 0, &range
, sample_locs
);
3075 radv_handle_image_transition(cmd_buffer
,
3077 cmd_buffer
->state
.attachments
[idx
].current_layout
,
3078 cmd_buffer
->state
.attachments
[idx
].current_in_render_loop
,
3079 att
.layout
, att
.in_render_loop
,
3080 0, 0, &range
, sample_locs
);
3083 cmd_buffer
->state
.attachments
[idx
].current_layout
= att
.layout
;
3084 cmd_buffer
->state
.attachments
[idx
].current_stencil_layout
= att
.stencil_layout
;
3085 cmd_buffer
->state
.attachments
[idx
].current_in_render_loop
= att
.in_render_loop
;
3091 radv_cmd_buffer_set_subpass(struct radv_cmd_buffer
*cmd_buffer
,
3092 const struct radv_subpass
*subpass
)
3094 cmd_buffer
->state
.subpass
= subpass
;
3096 cmd_buffer
->state
.dirty
|= RADV_CMD_DIRTY_FRAMEBUFFER
;
3100 radv_cmd_state_setup_sample_locations(struct radv_cmd_buffer
*cmd_buffer
,
3101 struct radv_render_pass
*pass
,
3102 const VkRenderPassBeginInfo
*info
)
3104 const struct VkRenderPassSampleLocationsBeginInfoEXT
*sample_locs
=
3105 vk_find_struct_const(info
->pNext
,
3106 RENDER_PASS_SAMPLE_LOCATIONS_BEGIN_INFO_EXT
);
3107 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
3110 state
->subpass_sample_locs
= NULL
;
3114 for (uint32_t i
= 0; i
< sample_locs
->attachmentInitialSampleLocationsCount
; i
++) {
3115 const VkAttachmentSampleLocationsEXT
*att_sample_locs
=
3116 &sample_locs
->pAttachmentInitialSampleLocations
[i
];
3117 uint32_t att_idx
= att_sample_locs
->attachmentIndex
;
3118 struct radv_image
*image
= cmd_buffer
->state
.attachments
[att_idx
].iview
->image
;
3120 assert(vk_format_is_depth_or_stencil(image
->vk_format
));
3122 /* From the Vulkan spec 1.1.108:
3124 * "If the image referenced by the framebuffer attachment at
3125 * index attachmentIndex was not created with
3126 * VK_IMAGE_CREATE_SAMPLE_LOCATIONS_COMPATIBLE_DEPTH_BIT_EXT
3127 * then the values specified in sampleLocationsInfo are
3130 if (!(image
->flags
& VK_IMAGE_CREATE_SAMPLE_LOCATIONS_COMPATIBLE_DEPTH_BIT_EXT
))
3133 const VkSampleLocationsInfoEXT
*sample_locs_info
=
3134 &att_sample_locs
->sampleLocationsInfo
;
3136 state
->attachments
[att_idx
].sample_location
.per_pixel
=
3137 sample_locs_info
->sampleLocationsPerPixel
;
3138 state
->attachments
[att_idx
].sample_location
.grid_size
=
3139 sample_locs_info
->sampleLocationGridSize
;
3140 state
->attachments
[att_idx
].sample_location
.count
=
3141 sample_locs_info
->sampleLocationsCount
;
3142 typed_memcpy(&state
->attachments
[att_idx
].sample_location
.locations
[0],
3143 sample_locs_info
->pSampleLocations
,
3144 sample_locs_info
->sampleLocationsCount
);
3147 state
->subpass_sample_locs
= vk_alloc(&cmd_buffer
->pool
->alloc
,
3148 sample_locs
->postSubpassSampleLocationsCount
*
3149 sizeof(state
->subpass_sample_locs
[0]),
3150 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
3151 if (state
->subpass_sample_locs
== NULL
) {
3152 cmd_buffer
->record_result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
3153 return cmd_buffer
->record_result
;
3156 state
->num_subpass_sample_locs
= sample_locs
->postSubpassSampleLocationsCount
;
3158 for (uint32_t i
= 0; i
< sample_locs
->postSubpassSampleLocationsCount
; i
++) {
3159 const VkSubpassSampleLocationsEXT
*subpass_sample_locs_info
=
3160 &sample_locs
->pPostSubpassSampleLocations
[i
];
3161 const VkSampleLocationsInfoEXT
*sample_locs_info
=
3162 &subpass_sample_locs_info
->sampleLocationsInfo
;
3164 state
->subpass_sample_locs
[i
].subpass_idx
=
3165 subpass_sample_locs_info
->subpassIndex
;
3166 state
->subpass_sample_locs
[i
].sample_location
.per_pixel
=
3167 sample_locs_info
->sampleLocationsPerPixel
;
3168 state
->subpass_sample_locs
[i
].sample_location
.grid_size
=
3169 sample_locs_info
->sampleLocationGridSize
;
3170 state
->subpass_sample_locs
[i
].sample_location
.count
=
3171 sample_locs_info
->sampleLocationsCount
;
3172 typed_memcpy(&state
->subpass_sample_locs
[i
].sample_location
.locations
[0],
3173 sample_locs_info
->pSampleLocations
,
3174 sample_locs_info
->sampleLocationsCount
);
3181 radv_cmd_state_setup_attachments(struct radv_cmd_buffer
*cmd_buffer
,
3182 struct radv_render_pass
*pass
,
3183 const VkRenderPassBeginInfo
*info
)
3185 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
3186 const struct VkRenderPassAttachmentBeginInfo
*attachment_info
= NULL
;
3189 attachment_info
= vk_find_struct_const(info
->pNext
,
3190 RENDER_PASS_ATTACHMENT_BEGIN_INFO
);
3194 if (pass
->attachment_count
== 0) {
3195 state
->attachments
= NULL
;
3199 state
->attachments
= vk_alloc(&cmd_buffer
->pool
->alloc
,
3200 pass
->attachment_count
*
3201 sizeof(state
->attachments
[0]),
3202 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
3203 if (state
->attachments
== NULL
) {
3204 cmd_buffer
->record_result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
3205 return cmd_buffer
->record_result
;
3208 for (uint32_t i
= 0; i
< pass
->attachment_count
; ++i
) {
3209 struct radv_render_pass_attachment
*att
= &pass
->attachments
[i
];
3210 VkImageAspectFlags att_aspects
= vk_format_aspects(att
->format
);
3211 VkImageAspectFlags clear_aspects
= 0;
3213 if (att_aspects
== VK_IMAGE_ASPECT_COLOR_BIT
) {
3214 /* color attachment */
3215 if (att
->load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
3216 clear_aspects
|= VK_IMAGE_ASPECT_COLOR_BIT
;
3219 /* depthstencil attachment */
3220 if ((att_aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) &&
3221 att
->load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
3222 clear_aspects
|= VK_IMAGE_ASPECT_DEPTH_BIT
;
3223 if ((att_aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) &&
3224 att
->stencil_load_op
== VK_ATTACHMENT_LOAD_OP_DONT_CARE
)
3225 clear_aspects
|= VK_IMAGE_ASPECT_STENCIL_BIT
;
3227 if ((att_aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) &&
3228 att
->stencil_load_op
== VK_ATTACHMENT_LOAD_OP_CLEAR
) {
3229 clear_aspects
|= VK_IMAGE_ASPECT_STENCIL_BIT
;
3233 state
->attachments
[i
].pending_clear_aspects
= clear_aspects
;
3234 state
->attachments
[i
].cleared_views
= 0;
3235 if (clear_aspects
&& info
) {
3236 assert(info
->clearValueCount
> i
);
3237 state
->attachments
[i
].clear_value
= info
->pClearValues
[i
];
3240 state
->attachments
[i
].current_layout
= att
->initial_layout
;
3241 state
->attachments
[i
].current_stencil_layout
= att
->stencil_initial_layout
;
3242 state
->attachments
[i
].sample_location
.count
= 0;
3244 struct radv_image_view
*iview
;
3245 if (attachment_info
&& attachment_info
->attachmentCount
> i
) {
3246 iview
= radv_image_view_from_handle(attachment_info
->pAttachments
[i
]);
3248 iview
= state
->framebuffer
->attachments
[i
];
3251 state
->attachments
[i
].iview
= iview
;
3252 if (iview
->aspect_mask
& (VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
)) {
3253 radv_initialise_ds_surface(cmd_buffer
->device
, &state
->attachments
[i
].ds
, iview
);
3255 radv_initialise_color_surface(cmd_buffer
->device
, &state
->attachments
[i
].cb
, iview
);
3262 VkResult
radv_AllocateCommandBuffers(
3264 const VkCommandBufferAllocateInfo
*pAllocateInfo
,
3265 VkCommandBuffer
*pCommandBuffers
)
3267 RADV_FROM_HANDLE(radv_device
, device
, _device
);
3268 RADV_FROM_HANDLE(radv_cmd_pool
, pool
, pAllocateInfo
->commandPool
);
3270 VkResult result
= VK_SUCCESS
;
3273 for (i
= 0; i
< pAllocateInfo
->commandBufferCount
; i
++) {
3275 if (!list_is_empty(&pool
->free_cmd_buffers
)) {
3276 struct radv_cmd_buffer
*cmd_buffer
= list_first_entry(&pool
->free_cmd_buffers
, struct radv_cmd_buffer
, pool_link
);
3278 list_del(&cmd_buffer
->pool_link
);
3279 list_addtail(&cmd_buffer
->pool_link
, &pool
->cmd_buffers
);
3281 result
= radv_reset_cmd_buffer(cmd_buffer
);
3282 cmd_buffer
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
3283 cmd_buffer
->level
= pAllocateInfo
->level
;
3285 pCommandBuffers
[i
] = radv_cmd_buffer_to_handle(cmd_buffer
);
3287 result
= radv_create_cmd_buffer(device
, pool
, pAllocateInfo
->level
,
3288 &pCommandBuffers
[i
]);
3290 if (result
!= VK_SUCCESS
)
3294 if (result
!= VK_SUCCESS
) {
3295 radv_FreeCommandBuffers(_device
, pAllocateInfo
->commandPool
,
3296 i
, pCommandBuffers
);
3298 /* From the Vulkan 1.0.66 spec:
3300 * "vkAllocateCommandBuffers can be used to create multiple
3301 * command buffers. If the creation of any of those command
3302 * buffers fails, the implementation must destroy all
3303 * successfully created command buffer objects from this
3304 * command, set all entries of the pCommandBuffers array to
3305 * NULL and return the error."
3307 memset(pCommandBuffers
, 0,
3308 sizeof(*pCommandBuffers
) * pAllocateInfo
->commandBufferCount
);
3314 void radv_FreeCommandBuffers(
3316 VkCommandPool commandPool
,
3317 uint32_t commandBufferCount
,
3318 const VkCommandBuffer
*pCommandBuffers
)
3320 for (uint32_t i
= 0; i
< commandBufferCount
; i
++) {
3321 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, pCommandBuffers
[i
]);
3324 if (cmd_buffer
->pool
) {
3325 list_del(&cmd_buffer
->pool_link
);
3326 list_addtail(&cmd_buffer
->pool_link
, &cmd_buffer
->pool
->free_cmd_buffers
);
3328 radv_cmd_buffer_destroy(cmd_buffer
);
3334 VkResult
radv_ResetCommandBuffer(
3335 VkCommandBuffer commandBuffer
,
3336 VkCommandBufferResetFlags flags
)
3338 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3339 return radv_reset_cmd_buffer(cmd_buffer
);
3342 VkResult
radv_BeginCommandBuffer(
3343 VkCommandBuffer commandBuffer
,
3344 const VkCommandBufferBeginInfo
*pBeginInfo
)
3346 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3347 VkResult result
= VK_SUCCESS
;
3349 if (cmd_buffer
->status
!= RADV_CMD_BUFFER_STATUS_INITIAL
) {
3350 /* If the command buffer has already been resetted with
3351 * vkResetCommandBuffer, no need to do it again.
3353 result
= radv_reset_cmd_buffer(cmd_buffer
);
3354 if (result
!= VK_SUCCESS
)
3358 memset(&cmd_buffer
->state
, 0, sizeof(cmd_buffer
->state
));
3359 cmd_buffer
->state
.last_primitive_reset_en
= -1;
3360 cmd_buffer
->state
.last_index_type
= -1;
3361 cmd_buffer
->state
.last_num_instances
= -1;
3362 cmd_buffer
->state
.last_vertex_offset
= -1;
3363 cmd_buffer
->state
.last_first_instance
= -1;
3364 cmd_buffer
->state
.predication_type
= -1;
3365 cmd_buffer
->usage_flags
= pBeginInfo
->flags
;
3367 if (cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
&&
3368 (pBeginInfo
->flags
& VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
)) {
3369 assert(pBeginInfo
->pInheritanceInfo
);
3370 cmd_buffer
->state
.framebuffer
= radv_framebuffer_from_handle(pBeginInfo
->pInheritanceInfo
->framebuffer
);
3371 cmd_buffer
->state
.pass
= radv_render_pass_from_handle(pBeginInfo
->pInheritanceInfo
->renderPass
);
3373 struct radv_subpass
*subpass
=
3374 &cmd_buffer
->state
.pass
->subpasses
[pBeginInfo
->pInheritanceInfo
->subpass
];
3376 if (cmd_buffer
->state
.framebuffer
) {
3377 result
= radv_cmd_state_setup_attachments(cmd_buffer
, cmd_buffer
->state
.pass
, NULL
);
3378 if (result
!= VK_SUCCESS
)
3382 cmd_buffer
->state
.inherited_pipeline_statistics
=
3383 pBeginInfo
->pInheritanceInfo
->pipelineStatistics
;
3385 radv_cmd_buffer_set_subpass(cmd_buffer
, subpass
);
3388 if (unlikely(cmd_buffer
->device
->trace_bo
)) {
3389 struct radv_device
*device
= cmd_buffer
->device
;
3391 radv_cs_add_buffer(device
->ws
, cmd_buffer
->cs
,
3394 radv_cmd_buffer_trace_emit(cmd_buffer
);
3397 cmd_buffer
->status
= RADV_CMD_BUFFER_STATUS_RECORDING
;
3402 void radv_CmdBindVertexBuffers(
3403 VkCommandBuffer commandBuffer
,
3404 uint32_t firstBinding
,
3405 uint32_t bindingCount
,
3406 const VkBuffer
* pBuffers
,
3407 const VkDeviceSize
* pOffsets
)
3409 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3410 struct radv_vertex_binding
*vb
= cmd_buffer
->vertex_bindings
;
3411 bool changed
= false;
3413 /* We have to defer setting up vertex buffer since we need the buffer
3414 * stride from the pipeline. */
3416 assert(firstBinding
+ bindingCount
<= MAX_VBS
);
3417 for (uint32_t i
= 0; i
< bindingCount
; i
++) {
3418 uint32_t idx
= firstBinding
+ i
;
3421 (vb
[idx
].buffer
!= radv_buffer_from_handle(pBuffers
[i
]) ||
3422 vb
[idx
].offset
!= pOffsets
[i
])) {
3426 vb
[idx
].buffer
= radv_buffer_from_handle(pBuffers
[i
]);
3427 vb
[idx
].offset
= pOffsets
[i
];
3429 radv_cs_add_buffer(cmd_buffer
->device
->ws
, cmd_buffer
->cs
,
3430 vb
[idx
].buffer
->bo
);
3434 /* No state changes. */
3438 cmd_buffer
->state
.dirty
|= RADV_CMD_DIRTY_VERTEX_BUFFER
;
3442 vk_to_index_type(VkIndexType type
)
3445 case VK_INDEX_TYPE_UINT8_EXT
:
3446 return V_028A7C_VGT_INDEX_8
;
3447 case VK_INDEX_TYPE_UINT16
:
3448 return V_028A7C_VGT_INDEX_16
;
3449 case VK_INDEX_TYPE_UINT32
:
3450 return V_028A7C_VGT_INDEX_32
;
3452 unreachable("invalid index type");
3457 radv_get_vgt_index_size(uint32_t type
)
3460 case V_028A7C_VGT_INDEX_8
:
3462 case V_028A7C_VGT_INDEX_16
:
3464 case V_028A7C_VGT_INDEX_32
:
3467 unreachable("invalid index type");
3471 void radv_CmdBindIndexBuffer(
3472 VkCommandBuffer commandBuffer
,
3474 VkDeviceSize offset
,
3475 VkIndexType indexType
)
3477 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3478 RADV_FROM_HANDLE(radv_buffer
, index_buffer
, buffer
);
3480 if (cmd_buffer
->state
.index_buffer
== index_buffer
&&
3481 cmd_buffer
->state
.index_offset
== offset
&&
3482 cmd_buffer
->state
.index_type
== indexType
) {
3483 /* No state changes. */
3487 cmd_buffer
->state
.index_buffer
= index_buffer
;
3488 cmd_buffer
->state
.index_offset
= offset
;
3489 cmd_buffer
->state
.index_type
= vk_to_index_type(indexType
);
3490 cmd_buffer
->state
.index_va
= radv_buffer_get_va(index_buffer
->bo
);
3491 cmd_buffer
->state
.index_va
+= index_buffer
->offset
+ offset
;
3493 int index_size
= radv_get_vgt_index_size(vk_to_index_type(indexType
));
3494 cmd_buffer
->state
.max_index_count
= (index_buffer
->size
- offset
) / index_size
;
3495 cmd_buffer
->state
.dirty
|= RADV_CMD_DIRTY_INDEX_BUFFER
;
3496 radv_cs_add_buffer(cmd_buffer
->device
->ws
, cmd_buffer
->cs
, index_buffer
->bo
);
3501 radv_bind_descriptor_set(struct radv_cmd_buffer
*cmd_buffer
,
3502 VkPipelineBindPoint bind_point
,
3503 struct radv_descriptor_set
*set
, unsigned idx
)
3505 struct radeon_winsys
*ws
= cmd_buffer
->device
->ws
;
3507 radv_set_descriptor_set(cmd_buffer
, bind_point
, set
, idx
);
3510 assert(!(set
->layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
));
3512 if (!cmd_buffer
->device
->use_global_bo_list
) {
3513 for (unsigned j
= 0; j
< set
->layout
->buffer_count
; ++j
)
3514 if (set
->descriptors
[j
])
3515 radv_cs_add_buffer(ws
, cmd_buffer
->cs
, set
->descriptors
[j
]);
3519 radv_cs_add_buffer(ws
, cmd_buffer
->cs
, set
->bo
);
3522 void radv_CmdBindDescriptorSets(
3523 VkCommandBuffer commandBuffer
,
3524 VkPipelineBindPoint pipelineBindPoint
,
3525 VkPipelineLayout _layout
,
3527 uint32_t descriptorSetCount
,
3528 const VkDescriptorSet
* pDescriptorSets
,
3529 uint32_t dynamicOffsetCount
,
3530 const uint32_t* pDynamicOffsets
)
3532 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3533 RADV_FROM_HANDLE(radv_pipeline_layout
, layout
, _layout
);
3534 unsigned dyn_idx
= 0;
3536 const bool no_dynamic_bounds
= cmd_buffer
->device
->instance
->debug_flags
& RADV_DEBUG_NO_DYNAMIC_BOUNDS
;
3537 struct radv_descriptor_state
*descriptors_state
=
3538 radv_get_descriptors_state(cmd_buffer
, pipelineBindPoint
);
3540 for (unsigned i
= 0; i
< descriptorSetCount
; ++i
) {
3541 unsigned idx
= i
+ firstSet
;
3542 RADV_FROM_HANDLE(radv_descriptor_set
, set
, pDescriptorSets
[i
]);
3544 /* If the set is already bound we only need to update the
3545 * (potentially changed) dynamic offsets. */
3546 if (descriptors_state
->sets
[idx
] != set
||
3547 !(descriptors_state
->valid
& (1u << idx
))) {
3548 radv_bind_descriptor_set(cmd_buffer
, pipelineBindPoint
, set
, idx
);
3551 for(unsigned j
= 0; j
< set
->layout
->dynamic_offset_count
; ++j
, ++dyn_idx
) {
3552 unsigned idx
= j
+ layout
->set
[i
+ firstSet
].dynamic_offset_start
;
3553 uint32_t *dst
= descriptors_state
->dynamic_buffers
+ idx
* 4;
3554 assert(dyn_idx
< dynamicOffsetCount
);
3556 struct radv_descriptor_range
*range
= set
->dynamic_descriptors
+ j
;
3557 uint64_t va
= range
->va
+ pDynamicOffsets
[dyn_idx
];
3559 dst
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32);
3560 dst
[2] = no_dynamic_bounds
? 0xffffffffu
: range
->size
;
3561 dst
[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
3562 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
3563 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
3564 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
);
3566 if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX10
) {
3567 dst
[3] |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT
) |
3568 S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW
) |
3569 S_008F0C_RESOURCE_LEVEL(1);
3571 dst
[3] |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
3572 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
3575 cmd_buffer
->push_constant_stages
|=
3576 set
->layout
->dynamic_shader_stages
;
3581 static bool radv_init_push_descriptor_set(struct radv_cmd_buffer
*cmd_buffer
,
3582 struct radv_descriptor_set
*set
,
3583 struct radv_descriptor_set_layout
*layout
,
3584 VkPipelineBindPoint bind_point
)
3586 struct radv_descriptor_state
*descriptors_state
=
3587 radv_get_descriptors_state(cmd_buffer
, bind_point
);
3588 set
->size
= layout
->size
;
3589 set
->layout
= layout
;
3591 if (descriptors_state
->push_set
.capacity
< set
->size
) {
3592 size_t new_size
= MAX2(set
->size
, 1024);
3593 new_size
= MAX2(new_size
, 2 * descriptors_state
->push_set
.capacity
);
3594 new_size
= MIN2(new_size
, 96 * MAX_PUSH_DESCRIPTORS
);
3596 free(set
->mapped_ptr
);
3597 set
->mapped_ptr
= malloc(new_size
);
3599 if (!set
->mapped_ptr
) {
3600 descriptors_state
->push_set
.capacity
= 0;
3601 cmd_buffer
->record_result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
3605 descriptors_state
->push_set
.capacity
= new_size
;
3611 void radv_meta_push_descriptor_set(
3612 struct radv_cmd_buffer
* cmd_buffer
,
3613 VkPipelineBindPoint pipelineBindPoint
,
3614 VkPipelineLayout _layout
,
3616 uint32_t descriptorWriteCount
,
3617 const VkWriteDescriptorSet
* pDescriptorWrites
)
3619 RADV_FROM_HANDLE(radv_pipeline_layout
, layout
, _layout
);
3620 struct radv_descriptor_set
*push_set
= &cmd_buffer
->meta_push_descriptors
;
3624 assert(layout
->set
[set
].layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
);
3626 push_set
->size
= layout
->set
[set
].layout
->size
;
3627 push_set
->layout
= layout
->set
[set
].layout
;
3629 if (!radv_cmd_buffer_upload_alloc(cmd_buffer
, push_set
->size
, 32,
3631 (void**) &push_set
->mapped_ptr
))
3634 push_set
->va
= radv_buffer_get_va(cmd_buffer
->upload
.upload_bo
);
3635 push_set
->va
+= bo_offset
;
3637 radv_update_descriptor_sets(cmd_buffer
->device
, cmd_buffer
,
3638 radv_descriptor_set_to_handle(push_set
),
3639 descriptorWriteCount
, pDescriptorWrites
, 0, NULL
);
3641 radv_set_descriptor_set(cmd_buffer
, pipelineBindPoint
, push_set
, set
);
3644 void radv_CmdPushDescriptorSetKHR(
3645 VkCommandBuffer commandBuffer
,
3646 VkPipelineBindPoint pipelineBindPoint
,
3647 VkPipelineLayout _layout
,
3649 uint32_t descriptorWriteCount
,
3650 const VkWriteDescriptorSet
* pDescriptorWrites
)
3652 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3653 RADV_FROM_HANDLE(radv_pipeline_layout
, layout
, _layout
);
3654 struct radv_descriptor_state
*descriptors_state
=
3655 radv_get_descriptors_state(cmd_buffer
, pipelineBindPoint
);
3656 struct radv_descriptor_set
*push_set
= &descriptors_state
->push_set
.set
;
3658 assert(layout
->set
[set
].layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
);
3660 if (!radv_init_push_descriptor_set(cmd_buffer
, push_set
,
3661 layout
->set
[set
].layout
,
3665 /* Check that there are no inline uniform block updates when calling vkCmdPushDescriptorSetKHR()
3666 * because it is invalid, according to Vulkan spec.
3668 for (int i
= 0; i
< descriptorWriteCount
; i
++) {
3669 ASSERTED
const VkWriteDescriptorSet
*writeset
= &pDescriptorWrites
[i
];
3670 assert(writeset
->descriptorType
!= VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
);
3673 radv_update_descriptor_sets(cmd_buffer
->device
, cmd_buffer
,
3674 radv_descriptor_set_to_handle(push_set
),
3675 descriptorWriteCount
, pDescriptorWrites
, 0, NULL
);
3677 radv_set_descriptor_set(cmd_buffer
, pipelineBindPoint
, push_set
, set
);
3678 descriptors_state
->push_dirty
= true;
3681 void radv_CmdPushDescriptorSetWithTemplateKHR(
3682 VkCommandBuffer commandBuffer
,
3683 VkDescriptorUpdateTemplate descriptorUpdateTemplate
,
3684 VkPipelineLayout _layout
,
3688 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3689 RADV_FROM_HANDLE(radv_pipeline_layout
, layout
, _layout
);
3690 RADV_FROM_HANDLE(radv_descriptor_update_template
, templ
, descriptorUpdateTemplate
);
3691 struct radv_descriptor_state
*descriptors_state
=
3692 radv_get_descriptors_state(cmd_buffer
, templ
->bind_point
);
3693 struct radv_descriptor_set
*push_set
= &descriptors_state
->push_set
.set
;
3695 assert(layout
->set
[set
].layout
->flags
& VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR
);
3697 if (!radv_init_push_descriptor_set(cmd_buffer
, push_set
,
3698 layout
->set
[set
].layout
,
3702 radv_update_descriptor_set_with_template(cmd_buffer
->device
, cmd_buffer
, push_set
,
3703 descriptorUpdateTemplate
, pData
);
3705 radv_set_descriptor_set(cmd_buffer
, templ
->bind_point
, push_set
, set
);
3706 descriptors_state
->push_dirty
= true;
3709 void radv_CmdPushConstants(VkCommandBuffer commandBuffer
,
3710 VkPipelineLayout layout
,
3711 VkShaderStageFlags stageFlags
,
3714 const void* pValues
)
3716 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3717 memcpy(cmd_buffer
->push_constants
+ offset
, pValues
, size
);
3718 cmd_buffer
->push_constant_stages
|= stageFlags
;
3721 VkResult
radv_EndCommandBuffer(
3722 VkCommandBuffer commandBuffer
)
3724 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3726 if (cmd_buffer
->queue_family_index
!= RADV_QUEUE_TRANSFER
) {
3727 if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
== GFX6
)
3728 cmd_buffer
->state
.flush_bits
|= RADV_CMD_FLAG_CS_PARTIAL_FLUSH
| RADV_CMD_FLAG_PS_PARTIAL_FLUSH
| RADV_CMD_FLAG_WB_L2
;
3730 /* Make sure to sync all pending active queries at the end of
3733 cmd_buffer
->state
.flush_bits
|= cmd_buffer
->active_query_flush_bits
;
3735 /* Since NGG streamout uses GDS, we need to make GDS idle when
3736 * we leave the IB, otherwise another process might overwrite
3737 * it while our shaders are busy.
3739 if (cmd_buffer
->gds_needed
)
3740 cmd_buffer
->state
.flush_bits
|= RADV_CMD_FLAG_PS_PARTIAL_FLUSH
;
3742 si_emit_cache_flush(cmd_buffer
);
3745 /* Make sure CP DMA is idle at the end of IBs because the kernel
3746 * doesn't wait for it.
3748 si_cp_dma_wait_for_idle(cmd_buffer
);
3750 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
->state
.attachments
);
3751 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
->state
.subpass_sample_locs
);
3753 if (!cmd_buffer
->device
->ws
->cs_finalize(cmd_buffer
->cs
))
3754 return vk_error(cmd_buffer
->device
->instance
, VK_ERROR_OUT_OF_DEVICE_MEMORY
);
3756 cmd_buffer
->status
= RADV_CMD_BUFFER_STATUS_EXECUTABLE
;
3758 return cmd_buffer
->record_result
;
3762 radv_emit_compute_pipeline(struct radv_cmd_buffer
*cmd_buffer
)
3764 struct radv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
3766 if (!pipeline
|| pipeline
== cmd_buffer
->state
.emitted_compute_pipeline
)
3769 assert(!pipeline
->ctx_cs
.cdw
);
3771 cmd_buffer
->state
.emitted_compute_pipeline
= pipeline
;
3773 radeon_check_space(cmd_buffer
->device
->ws
, cmd_buffer
->cs
, pipeline
->cs
.cdw
);
3774 radeon_emit_array(cmd_buffer
->cs
, pipeline
->cs
.buf
, pipeline
->cs
.cdw
);
3776 cmd_buffer
->compute_scratch_size_per_wave_needed
= MAX2(cmd_buffer
->compute_scratch_size_per_wave_needed
,
3777 pipeline
->scratch_bytes_per_wave
);
3778 cmd_buffer
->compute_scratch_waves_wanted
= MAX2(cmd_buffer
->compute_scratch_waves_wanted
,
3779 pipeline
->max_waves
);
3781 radv_cs_add_buffer(cmd_buffer
->device
->ws
, cmd_buffer
->cs
,
3782 pipeline
->shaders
[MESA_SHADER_COMPUTE
]->bo
);
3784 if (unlikely(cmd_buffer
->device
->trace_bo
))
3785 radv_save_pipeline(cmd_buffer
, pipeline
, RING_COMPUTE
);
3788 static void radv_mark_descriptor_sets_dirty(struct radv_cmd_buffer
*cmd_buffer
,
3789 VkPipelineBindPoint bind_point
)
3791 struct radv_descriptor_state
*descriptors_state
=
3792 radv_get_descriptors_state(cmd_buffer
, bind_point
);
3794 descriptors_state
->dirty
|= descriptors_state
->valid
;
3797 void radv_CmdBindPipeline(
3798 VkCommandBuffer commandBuffer
,
3799 VkPipelineBindPoint pipelineBindPoint
,
3800 VkPipeline _pipeline
)
3802 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3803 RADV_FROM_HANDLE(radv_pipeline
, pipeline
, _pipeline
);
3805 switch (pipelineBindPoint
) {
3806 case VK_PIPELINE_BIND_POINT_COMPUTE
:
3807 if (cmd_buffer
->state
.compute_pipeline
== pipeline
)
3809 radv_mark_descriptor_sets_dirty(cmd_buffer
, pipelineBindPoint
);
3811 cmd_buffer
->state
.compute_pipeline
= pipeline
;
3812 cmd_buffer
->push_constant_stages
|= VK_SHADER_STAGE_COMPUTE_BIT
;
3814 case VK_PIPELINE_BIND_POINT_GRAPHICS
:
3815 if (cmd_buffer
->state
.pipeline
== pipeline
)
3817 radv_mark_descriptor_sets_dirty(cmd_buffer
, pipelineBindPoint
);
3819 cmd_buffer
->state
.pipeline
= pipeline
;
3823 cmd_buffer
->state
.dirty
|= RADV_CMD_DIRTY_PIPELINE
;
3824 cmd_buffer
->push_constant_stages
|= pipeline
->active_stages
;
3826 /* the new vertex shader might not have the same user regs */
3827 cmd_buffer
->state
.last_first_instance
= -1;
3828 cmd_buffer
->state
.last_vertex_offset
= -1;
3830 /* Prefetch all pipeline shaders at first draw time. */
3831 cmd_buffer
->state
.prefetch_L2_mask
|= RADV_PREFETCH_SHADERS
;
3833 if ((cmd_buffer
->device
->physical_device
->rad_info
.family
== CHIP_NAVI10
||
3834 cmd_buffer
->device
->physical_device
->rad_info
.family
== CHIP_NAVI12
||
3835 cmd_buffer
->device
->physical_device
->rad_info
.family
== CHIP_NAVI14
) &&
3836 cmd_buffer
->state
.emitted_pipeline
&&
3837 radv_pipeline_has_ngg(cmd_buffer
->state
.emitted_pipeline
) &&
3838 !radv_pipeline_has_ngg(cmd_buffer
->state
.pipeline
)) {
3839 /* Transitioning from NGG to legacy GS requires
3840 * VGT_FLUSH on Navi10-14. VGT_FLUSH is also emitted
3841 * at the beginning of IBs when legacy GS ring pointers
3844 cmd_buffer
->state
.flush_bits
|= RADV_CMD_FLAG_VGT_FLUSH
;
3847 radv_bind_dynamic_state(cmd_buffer
, &pipeline
->dynamic_state
);
3848 radv_bind_streamout_state(cmd_buffer
, pipeline
);
3850 if (pipeline
->graphics
.esgs_ring_size
> cmd_buffer
->esgs_ring_size_needed
)
3851 cmd_buffer
->esgs_ring_size_needed
= pipeline
->graphics
.esgs_ring_size
;
3852 if (pipeline
->graphics
.gsvs_ring_size
> cmd_buffer
->gsvs_ring_size_needed
)
3853 cmd_buffer
->gsvs_ring_size_needed
= pipeline
->graphics
.gsvs_ring_size
;
3855 if (radv_pipeline_has_tess(pipeline
))
3856 cmd_buffer
->tess_rings_needed
= true;
3859 assert(!"invalid bind point");
3864 void radv_CmdSetViewport(
3865 VkCommandBuffer commandBuffer
,
3866 uint32_t firstViewport
,
3867 uint32_t viewportCount
,
3868 const VkViewport
* pViewports
)
3870 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3871 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
3872 ASSERTED
const uint32_t total_count
= firstViewport
+ viewportCount
;
3874 assert(firstViewport
< MAX_VIEWPORTS
);
3875 assert(total_count
>= 1 && total_count
<= MAX_VIEWPORTS
);
3877 if (!memcmp(state
->dynamic
.viewport
.viewports
+ firstViewport
,
3878 pViewports
, viewportCount
* sizeof(*pViewports
))) {
3882 memcpy(state
->dynamic
.viewport
.viewports
+ firstViewport
, pViewports
,
3883 viewportCount
* sizeof(*pViewports
));
3885 state
->dirty
|= RADV_CMD_DIRTY_DYNAMIC_VIEWPORT
;
3888 void radv_CmdSetScissor(
3889 VkCommandBuffer commandBuffer
,
3890 uint32_t firstScissor
,
3891 uint32_t scissorCount
,
3892 const VkRect2D
* pScissors
)
3894 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3895 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
3896 ASSERTED
const uint32_t total_count
= firstScissor
+ scissorCount
;
3898 assert(firstScissor
< MAX_SCISSORS
);
3899 assert(total_count
>= 1 && total_count
<= MAX_SCISSORS
);
3901 if (!memcmp(state
->dynamic
.scissor
.scissors
+ firstScissor
, pScissors
,
3902 scissorCount
* sizeof(*pScissors
))) {
3906 memcpy(state
->dynamic
.scissor
.scissors
+ firstScissor
, pScissors
,
3907 scissorCount
* sizeof(*pScissors
));
3909 state
->dirty
|= RADV_CMD_DIRTY_DYNAMIC_SCISSOR
;
3912 void radv_CmdSetLineWidth(
3913 VkCommandBuffer commandBuffer
,
3916 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3918 if (cmd_buffer
->state
.dynamic
.line_width
== lineWidth
)
3921 cmd_buffer
->state
.dynamic
.line_width
= lineWidth
;
3922 cmd_buffer
->state
.dirty
|= RADV_CMD_DIRTY_DYNAMIC_LINE_WIDTH
;
3925 void radv_CmdSetDepthBias(
3926 VkCommandBuffer commandBuffer
,
3927 float depthBiasConstantFactor
,
3928 float depthBiasClamp
,
3929 float depthBiasSlopeFactor
)
3931 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3932 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
3934 if (state
->dynamic
.depth_bias
.bias
== depthBiasConstantFactor
&&
3935 state
->dynamic
.depth_bias
.clamp
== depthBiasClamp
&&
3936 state
->dynamic
.depth_bias
.slope
== depthBiasSlopeFactor
) {
3940 state
->dynamic
.depth_bias
.bias
= depthBiasConstantFactor
;
3941 state
->dynamic
.depth_bias
.clamp
= depthBiasClamp
;
3942 state
->dynamic
.depth_bias
.slope
= depthBiasSlopeFactor
;
3944 state
->dirty
|= RADV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS
;
3947 void radv_CmdSetBlendConstants(
3948 VkCommandBuffer commandBuffer
,
3949 const float blendConstants
[4])
3951 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3952 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
3954 if (!memcmp(state
->dynamic
.blend_constants
, blendConstants
, sizeof(float) * 4))
3957 memcpy(state
->dynamic
.blend_constants
, blendConstants
, sizeof(float) * 4);
3959 state
->dirty
|= RADV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS
;
3962 void radv_CmdSetDepthBounds(
3963 VkCommandBuffer commandBuffer
,
3964 float minDepthBounds
,
3965 float maxDepthBounds
)
3967 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3968 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
3970 if (state
->dynamic
.depth_bounds
.min
== minDepthBounds
&&
3971 state
->dynamic
.depth_bounds
.max
== maxDepthBounds
) {
3975 state
->dynamic
.depth_bounds
.min
= minDepthBounds
;
3976 state
->dynamic
.depth_bounds
.max
= maxDepthBounds
;
3978 state
->dirty
|= RADV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS
;
3981 void radv_CmdSetStencilCompareMask(
3982 VkCommandBuffer commandBuffer
,
3983 VkStencilFaceFlags faceMask
,
3984 uint32_t compareMask
)
3986 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
3987 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
3988 bool front_same
= state
->dynamic
.stencil_compare_mask
.front
== compareMask
;
3989 bool back_same
= state
->dynamic
.stencil_compare_mask
.back
== compareMask
;
3991 if ((!(faceMask
& VK_STENCIL_FACE_FRONT_BIT
) || front_same
) &&
3992 (!(faceMask
& VK_STENCIL_FACE_BACK_BIT
) || back_same
)) {
3996 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
3997 state
->dynamic
.stencil_compare_mask
.front
= compareMask
;
3998 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
3999 state
->dynamic
.stencil_compare_mask
.back
= compareMask
;
4001 state
->dirty
|= RADV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK
;
4004 void radv_CmdSetStencilWriteMask(
4005 VkCommandBuffer commandBuffer
,
4006 VkStencilFaceFlags faceMask
,
4009 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
4010 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
4011 bool front_same
= state
->dynamic
.stencil_write_mask
.front
== writeMask
;
4012 bool back_same
= state
->dynamic
.stencil_write_mask
.back
== writeMask
;
4014 if ((!(faceMask
& VK_STENCIL_FACE_FRONT_BIT
) || front_same
) &&
4015 (!(faceMask
& VK_STENCIL_FACE_BACK_BIT
) || back_same
)) {
4019 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
4020 state
->dynamic
.stencil_write_mask
.front
= writeMask
;
4021 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
4022 state
->dynamic
.stencil_write_mask
.back
= writeMask
;
4024 state
->dirty
|= RADV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK
;
4027 void radv_CmdSetStencilReference(
4028 VkCommandBuffer commandBuffer
,
4029 VkStencilFaceFlags faceMask
,
4032 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
4033 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
4034 bool front_same
= state
->dynamic
.stencil_reference
.front
== reference
;
4035 bool back_same
= state
->dynamic
.stencil_reference
.back
== reference
;
4037 if ((!(faceMask
& VK_STENCIL_FACE_FRONT_BIT
) || front_same
) &&
4038 (!(faceMask
& VK_STENCIL_FACE_BACK_BIT
) || back_same
)) {
4042 if (faceMask
& VK_STENCIL_FACE_FRONT_BIT
)
4043 cmd_buffer
->state
.dynamic
.stencil_reference
.front
= reference
;
4044 if (faceMask
& VK_STENCIL_FACE_BACK_BIT
)
4045 cmd_buffer
->state
.dynamic
.stencil_reference
.back
= reference
;
4047 cmd_buffer
->state
.dirty
|= RADV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE
;
4050 void radv_CmdSetDiscardRectangleEXT(
4051 VkCommandBuffer commandBuffer
,
4052 uint32_t firstDiscardRectangle
,
4053 uint32_t discardRectangleCount
,
4054 const VkRect2D
* pDiscardRectangles
)
4056 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
4057 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
4058 ASSERTED
const uint32_t total_count
= firstDiscardRectangle
+ discardRectangleCount
;
4060 assert(firstDiscardRectangle
< MAX_DISCARD_RECTANGLES
);
4061 assert(total_count
>= 1 && total_count
<= MAX_DISCARD_RECTANGLES
);
4063 if (!memcmp(state
->dynamic
.discard_rectangle
.rectangles
+ firstDiscardRectangle
,
4064 pDiscardRectangles
, discardRectangleCount
* sizeof(*pDiscardRectangles
))) {
4068 typed_memcpy(&state
->dynamic
.discard_rectangle
.rectangles
[firstDiscardRectangle
],
4069 pDiscardRectangles
, discardRectangleCount
);
4071 state
->dirty
|= RADV_CMD_DIRTY_DYNAMIC_DISCARD_RECTANGLE
;
4074 void radv_CmdSetSampleLocationsEXT(
4075 VkCommandBuffer commandBuffer
,
4076 const VkSampleLocationsInfoEXT
* pSampleLocationsInfo
)
4078 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
4079 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
4081 assert(pSampleLocationsInfo
->sampleLocationsCount
<= MAX_SAMPLE_LOCATIONS
);
4083 state
->dynamic
.sample_location
.per_pixel
= pSampleLocationsInfo
->sampleLocationsPerPixel
;
4084 state
->dynamic
.sample_location
.grid_size
= pSampleLocationsInfo
->sampleLocationGridSize
;
4085 state
->dynamic
.sample_location
.count
= pSampleLocationsInfo
->sampleLocationsCount
;
4086 typed_memcpy(&state
->dynamic
.sample_location
.locations
[0],
4087 pSampleLocationsInfo
->pSampleLocations
,
4088 pSampleLocationsInfo
->sampleLocationsCount
);
4090 state
->dirty
|= RADV_CMD_DIRTY_DYNAMIC_SAMPLE_LOCATIONS
;
4093 void radv_CmdExecuteCommands(
4094 VkCommandBuffer commandBuffer
,
4095 uint32_t commandBufferCount
,
4096 const VkCommandBuffer
* pCmdBuffers
)
4098 RADV_FROM_HANDLE(radv_cmd_buffer
, primary
, commandBuffer
);
4100 assert(commandBufferCount
> 0);
4102 /* Emit pending flushes on primary prior to executing secondary */
4103 si_emit_cache_flush(primary
);
4105 for (uint32_t i
= 0; i
< commandBufferCount
; i
++) {
4106 RADV_FROM_HANDLE(radv_cmd_buffer
, secondary
, pCmdBuffers
[i
]);
4108 primary
->scratch_size_per_wave_needed
= MAX2(primary
->scratch_size_per_wave_needed
,
4109 secondary
->scratch_size_per_wave_needed
);
4110 primary
->scratch_waves_wanted
= MAX2(primary
->scratch_waves_wanted
,
4111 secondary
->scratch_waves_wanted
);
4112 primary
->compute_scratch_size_per_wave_needed
= MAX2(primary
->compute_scratch_size_per_wave_needed
,
4113 secondary
->compute_scratch_size_per_wave_needed
);
4114 primary
->compute_scratch_waves_wanted
= MAX2(primary
->compute_scratch_waves_wanted
,
4115 secondary
->compute_scratch_waves_wanted
);
4117 if (secondary
->esgs_ring_size_needed
> primary
->esgs_ring_size_needed
)
4118 primary
->esgs_ring_size_needed
= secondary
->esgs_ring_size_needed
;
4119 if (secondary
->gsvs_ring_size_needed
> primary
->gsvs_ring_size_needed
)
4120 primary
->gsvs_ring_size_needed
= secondary
->gsvs_ring_size_needed
;
4121 if (secondary
->tess_rings_needed
)
4122 primary
->tess_rings_needed
= true;
4123 if (secondary
->sample_positions_needed
)
4124 primary
->sample_positions_needed
= true;
4125 if (secondary
->gds_needed
)
4126 primary
->gds_needed
= true;
4128 if (!secondary
->state
.framebuffer
&&
4129 (primary
->state
.dirty
& RADV_CMD_DIRTY_FRAMEBUFFER
)) {
4130 /* Emit the framebuffer state from primary if secondary
4131 * has been recorded without a framebuffer, otherwise
4132 * fast color/depth clears can't work.
4134 radv_emit_framebuffer_state(primary
);
4137 primary
->device
->ws
->cs_execute_secondary(primary
->cs
, secondary
->cs
);
4140 /* When the secondary command buffer is compute only we don't
4141 * need to re-emit the current graphics pipeline.
4143 if (secondary
->state
.emitted_pipeline
) {
4144 primary
->state
.emitted_pipeline
=
4145 secondary
->state
.emitted_pipeline
;
4148 /* When the secondary command buffer is graphics only we don't
4149 * need to re-emit the current compute pipeline.
4151 if (secondary
->state
.emitted_compute_pipeline
) {
4152 primary
->state
.emitted_compute_pipeline
=
4153 secondary
->state
.emitted_compute_pipeline
;
4156 /* Only re-emit the draw packets when needed. */
4157 if (secondary
->state
.last_primitive_reset_en
!= -1) {
4158 primary
->state
.last_primitive_reset_en
=
4159 secondary
->state
.last_primitive_reset_en
;
4162 if (secondary
->state
.last_primitive_reset_index
) {
4163 primary
->state
.last_primitive_reset_index
=
4164 secondary
->state
.last_primitive_reset_index
;
4167 if (secondary
->state
.last_ia_multi_vgt_param
) {
4168 primary
->state
.last_ia_multi_vgt_param
=
4169 secondary
->state
.last_ia_multi_vgt_param
;
4172 primary
->state
.last_first_instance
= secondary
->state
.last_first_instance
;
4173 primary
->state
.last_num_instances
= secondary
->state
.last_num_instances
;
4174 primary
->state
.last_vertex_offset
= secondary
->state
.last_vertex_offset
;
4176 if (secondary
->state
.last_index_type
!= -1) {
4177 primary
->state
.last_index_type
=
4178 secondary
->state
.last_index_type
;
4182 /* After executing commands from secondary buffers we have to dirty
4185 primary
->state
.dirty
|= RADV_CMD_DIRTY_PIPELINE
|
4186 RADV_CMD_DIRTY_INDEX_BUFFER
|
4187 RADV_CMD_DIRTY_DYNAMIC_ALL
;
4188 radv_mark_descriptor_sets_dirty(primary
, VK_PIPELINE_BIND_POINT_GRAPHICS
);
4189 radv_mark_descriptor_sets_dirty(primary
, VK_PIPELINE_BIND_POINT_COMPUTE
);
4192 VkResult
radv_CreateCommandPool(
4194 const VkCommandPoolCreateInfo
* pCreateInfo
,
4195 const VkAllocationCallbacks
* pAllocator
,
4196 VkCommandPool
* pCmdPool
)
4198 RADV_FROM_HANDLE(radv_device
, device
, _device
);
4199 struct radv_cmd_pool
*pool
;
4201 pool
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*pool
), 8,
4202 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
4204 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
4207 pool
->alloc
= *pAllocator
;
4209 pool
->alloc
= device
->alloc
;
4211 list_inithead(&pool
->cmd_buffers
);
4212 list_inithead(&pool
->free_cmd_buffers
);
4214 pool
->queue_family_index
= pCreateInfo
->queueFamilyIndex
;
4216 *pCmdPool
= radv_cmd_pool_to_handle(pool
);
4222 void radv_DestroyCommandPool(
4224 VkCommandPool commandPool
,
4225 const VkAllocationCallbacks
* pAllocator
)
4227 RADV_FROM_HANDLE(radv_device
, device
, _device
);
4228 RADV_FROM_HANDLE(radv_cmd_pool
, pool
, commandPool
);
4233 list_for_each_entry_safe(struct radv_cmd_buffer
, cmd_buffer
,
4234 &pool
->cmd_buffers
, pool_link
) {
4235 radv_cmd_buffer_destroy(cmd_buffer
);
4238 list_for_each_entry_safe(struct radv_cmd_buffer
, cmd_buffer
,
4239 &pool
->free_cmd_buffers
, pool_link
) {
4240 radv_cmd_buffer_destroy(cmd_buffer
);
4243 vk_free2(&device
->alloc
, pAllocator
, pool
);
4246 VkResult
radv_ResetCommandPool(
4248 VkCommandPool commandPool
,
4249 VkCommandPoolResetFlags flags
)
4251 RADV_FROM_HANDLE(radv_cmd_pool
, pool
, commandPool
);
4254 list_for_each_entry(struct radv_cmd_buffer
, cmd_buffer
,
4255 &pool
->cmd_buffers
, pool_link
) {
4256 result
= radv_reset_cmd_buffer(cmd_buffer
);
4257 if (result
!= VK_SUCCESS
)
4264 void radv_TrimCommandPool(
4266 VkCommandPool commandPool
,
4267 VkCommandPoolTrimFlags flags
)
4269 RADV_FROM_HANDLE(radv_cmd_pool
, pool
, commandPool
);
4274 list_for_each_entry_safe(struct radv_cmd_buffer
, cmd_buffer
,
4275 &pool
->free_cmd_buffers
, pool_link
) {
4276 radv_cmd_buffer_destroy(cmd_buffer
);
4281 radv_cmd_buffer_begin_subpass(struct radv_cmd_buffer
*cmd_buffer
,
4282 uint32_t subpass_id
)
4284 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
4285 struct radv_subpass
*subpass
= &state
->pass
->subpasses
[subpass_id
];
4287 ASSERTED
unsigned cdw_max
= radeon_check_space(cmd_buffer
->device
->ws
,
4288 cmd_buffer
->cs
, 4096);
4290 radv_subpass_barrier(cmd_buffer
, &subpass
->start_barrier
);
4292 radv_cmd_buffer_set_subpass(cmd_buffer
, subpass
);
4294 for (uint32_t i
= 0; i
< subpass
->attachment_count
; ++i
) {
4295 const uint32_t a
= subpass
->attachments
[i
].attachment
;
4296 if (a
== VK_ATTACHMENT_UNUSED
)
4299 radv_handle_subpass_image_transition(cmd_buffer
,
4300 subpass
->attachments
[i
],
4304 radv_cmd_buffer_clear_subpass(cmd_buffer
);
4306 assert(cmd_buffer
->cs
->cdw
<= cdw_max
);
4310 radv_cmd_buffer_end_subpass(struct radv_cmd_buffer
*cmd_buffer
)
4312 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
4313 const struct radv_subpass
*subpass
= state
->subpass
;
4314 uint32_t subpass_id
= radv_get_subpass_id(cmd_buffer
);
4316 radv_cmd_buffer_resolve_subpass(cmd_buffer
);
4318 for (uint32_t i
= 0; i
< subpass
->attachment_count
; ++i
) {
4319 const uint32_t a
= subpass
->attachments
[i
].attachment
;
4320 if (a
== VK_ATTACHMENT_UNUSED
)
4323 if (state
->pass
->attachments
[a
].last_subpass_idx
!= subpass_id
)
4326 VkImageLayout layout
= state
->pass
->attachments
[a
].final_layout
;
4327 VkImageLayout stencil_layout
= state
->pass
->attachments
[a
].stencil_final_layout
;
4328 struct radv_subpass_attachment att
= { a
, layout
, stencil_layout
};
4329 radv_handle_subpass_image_transition(cmd_buffer
, att
, false);
4333 void radv_CmdBeginRenderPass(
4334 VkCommandBuffer commandBuffer
,
4335 const VkRenderPassBeginInfo
* pRenderPassBegin
,
4336 VkSubpassContents contents
)
4338 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
4339 RADV_FROM_HANDLE(radv_render_pass
, pass
, pRenderPassBegin
->renderPass
);
4340 RADV_FROM_HANDLE(radv_framebuffer
, framebuffer
, pRenderPassBegin
->framebuffer
);
4343 cmd_buffer
->state
.framebuffer
= framebuffer
;
4344 cmd_buffer
->state
.pass
= pass
;
4345 cmd_buffer
->state
.render_area
= pRenderPassBegin
->renderArea
;
4347 result
= radv_cmd_state_setup_attachments(cmd_buffer
, pass
, pRenderPassBegin
);
4348 if (result
!= VK_SUCCESS
)
4351 result
= radv_cmd_state_setup_sample_locations(cmd_buffer
, pass
, pRenderPassBegin
);
4352 if (result
!= VK_SUCCESS
)
4355 radv_cmd_buffer_begin_subpass(cmd_buffer
, 0);
4358 void radv_CmdBeginRenderPass2(
4359 VkCommandBuffer commandBuffer
,
4360 const VkRenderPassBeginInfo
* pRenderPassBeginInfo
,
4361 const VkSubpassBeginInfo
* pSubpassBeginInfo
)
4363 radv_CmdBeginRenderPass(commandBuffer
, pRenderPassBeginInfo
,
4364 pSubpassBeginInfo
->contents
);
4367 void radv_CmdNextSubpass(
4368 VkCommandBuffer commandBuffer
,
4369 VkSubpassContents contents
)
4371 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
4373 uint32_t prev_subpass
= radv_get_subpass_id(cmd_buffer
);
4374 radv_cmd_buffer_end_subpass(cmd_buffer
);
4375 radv_cmd_buffer_begin_subpass(cmd_buffer
, prev_subpass
+ 1);
4378 void radv_CmdNextSubpass2(
4379 VkCommandBuffer commandBuffer
,
4380 const VkSubpassBeginInfo
* pSubpassBeginInfo
,
4381 const VkSubpassEndInfo
* pSubpassEndInfo
)
4383 radv_CmdNextSubpass(commandBuffer
, pSubpassBeginInfo
->contents
);
4386 static void radv_emit_view_index(struct radv_cmd_buffer
*cmd_buffer
, unsigned index
)
4388 struct radv_pipeline
*pipeline
= cmd_buffer
->state
.pipeline
;
4389 for (unsigned stage
= 0; stage
< MESA_SHADER_STAGES
; ++stage
) {
4390 if (!radv_get_shader(pipeline
, stage
))
4393 struct radv_userdata_info
*loc
= radv_lookup_user_sgpr(pipeline
, stage
, AC_UD_VIEW_INDEX
);
4394 if (loc
->sgpr_idx
== -1)
4396 uint32_t base_reg
= pipeline
->user_data_0
[stage
];
4397 radeon_set_sh_reg(cmd_buffer
->cs
, base_reg
+ loc
->sgpr_idx
* 4, index
);
4400 if (radv_pipeline_has_gs_copy_shader(pipeline
)) {
4401 struct radv_userdata_info
*loc
= &pipeline
->gs_copy_shader
->info
.user_sgprs_locs
.shader_data
[AC_UD_VIEW_INDEX
];
4402 if (loc
->sgpr_idx
!= -1) {
4403 uint32_t base_reg
= R_00B130_SPI_SHADER_USER_DATA_VS_0
;
4404 radeon_set_sh_reg(cmd_buffer
->cs
, base_reg
+ loc
->sgpr_idx
* 4, index
);
4410 radv_cs_emit_draw_packet(struct radv_cmd_buffer
*cmd_buffer
,
4411 uint32_t vertex_count
,
4414 radeon_emit(cmd_buffer
->cs
, PKT3(PKT3_DRAW_INDEX_AUTO
, 1, cmd_buffer
->state
.predicating
));
4415 radeon_emit(cmd_buffer
->cs
, vertex_count
);
4416 radeon_emit(cmd_buffer
->cs
, V_0287F0_DI_SRC_SEL_AUTO_INDEX
|
4417 S_0287F0_USE_OPAQUE(use_opaque
));
4421 radv_cs_emit_draw_indexed_packet(struct radv_cmd_buffer
*cmd_buffer
,
4423 uint32_t index_count
)
4425 radeon_emit(cmd_buffer
->cs
, PKT3(PKT3_DRAW_INDEX_2
, 4, cmd_buffer
->state
.predicating
));
4426 radeon_emit(cmd_buffer
->cs
, cmd_buffer
->state
.max_index_count
);
4427 radeon_emit(cmd_buffer
->cs
, index_va
);
4428 radeon_emit(cmd_buffer
->cs
, index_va
>> 32);
4429 radeon_emit(cmd_buffer
->cs
, index_count
);
4430 radeon_emit(cmd_buffer
->cs
, V_0287F0_DI_SRC_SEL_DMA
);
4434 radv_cs_emit_indirect_draw_packet(struct radv_cmd_buffer
*cmd_buffer
,
4436 uint32_t draw_count
,
4440 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
4441 unsigned di_src_sel
= indexed
? V_0287F0_DI_SRC_SEL_DMA
4442 : V_0287F0_DI_SRC_SEL_AUTO_INDEX
;
4443 bool draw_id_enable
= radv_get_shader(cmd_buffer
->state
.pipeline
, MESA_SHADER_VERTEX
)->info
.vs
.needs_draw_id
;
4444 uint32_t base_reg
= cmd_buffer
->state
.pipeline
->graphics
.vtx_base_sgpr
;
4445 bool predicating
= cmd_buffer
->state
.predicating
;
4448 /* just reset draw state for vertex data */
4449 cmd_buffer
->state
.last_first_instance
= -1;
4450 cmd_buffer
->state
.last_num_instances
= -1;
4451 cmd_buffer
->state
.last_vertex_offset
= -1;
4453 if (draw_count
== 1 && !count_va
&& !draw_id_enable
) {
4454 radeon_emit(cs
, PKT3(indexed
? PKT3_DRAW_INDEX_INDIRECT
:
4455 PKT3_DRAW_INDIRECT
, 3, predicating
));
4457 radeon_emit(cs
, (base_reg
- SI_SH_REG_OFFSET
) >> 2);
4458 radeon_emit(cs
, ((base_reg
+ 4) - SI_SH_REG_OFFSET
) >> 2);
4459 radeon_emit(cs
, di_src_sel
);
4461 radeon_emit(cs
, PKT3(indexed
? PKT3_DRAW_INDEX_INDIRECT_MULTI
:
4462 PKT3_DRAW_INDIRECT_MULTI
,
4465 radeon_emit(cs
, (base_reg
- SI_SH_REG_OFFSET
) >> 2);
4466 radeon_emit(cs
, ((base_reg
+ 4) - SI_SH_REG_OFFSET
) >> 2);
4467 radeon_emit(cs
, (((base_reg
+ 8) - SI_SH_REG_OFFSET
) >> 2) |
4468 S_2C3_DRAW_INDEX_ENABLE(draw_id_enable
) |
4469 S_2C3_COUNT_INDIRECT_ENABLE(!!count_va
));
4470 radeon_emit(cs
, draw_count
); /* count */
4471 radeon_emit(cs
, count_va
); /* count_addr */
4472 radeon_emit(cs
, count_va
>> 32);
4473 radeon_emit(cs
, stride
); /* stride */
4474 radeon_emit(cs
, di_src_sel
);
4479 radv_emit_draw_packets(struct radv_cmd_buffer
*cmd_buffer
,
4480 const struct radv_draw_info
*info
)
4482 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
4483 struct radeon_winsys
*ws
= cmd_buffer
->device
->ws
;
4484 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
4486 if (info
->indirect
) {
4487 uint64_t va
= radv_buffer_get_va(info
->indirect
->bo
);
4488 uint64_t count_va
= 0;
4490 va
+= info
->indirect
->offset
+ info
->indirect_offset
;
4492 radv_cs_add_buffer(ws
, cs
, info
->indirect
->bo
);
4494 radeon_emit(cs
, PKT3(PKT3_SET_BASE
, 2, 0));
4496 radeon_emit(cs
, va
);
4497 radeon_emit(cs
, va
>> 32);
4499 if (info
->count_buffer
) {
4500 count_va
= radv_buffer_get_va(info
->count_buffer
->bo
);
4501 count_va
+= info
->count_buffer
->offset
+
4502 info
->count_buffer_offset
;
4504 radv_cs_add_buffer(ws
, cs
, info
->count_buffer
->bo
);
4507 if (!state
->subpass
->view_mask
) {
4508 radv_cs_emit_indirect_draw_packet(cmd_buffer
,
4515 for_each_bit(i
, state
->subpass
->view_mask
) {
4516 radv_emit_view_index(cmd_buffer
, i
);
4518 radv_cs_emit_indirect_draw_packet(cmd_buffer
,
4526 assert(state
->pipeline
->graphics
.vtx_base_sgpr
);
4528 if (info
->vertex_offset
!= state
->last_vertex_offset
||
4529 info
->first_instance
!= state
->last_first_instance
) {
4530 radeon_set_sh_reg_seq(cs
, state
->pipeline
->graphics
.vtx_base_sgpr
,
4531 state
->pipeline
->graphics
.vtx_emit_num
);
4533 radeon_emit(cs
, info
->vertex_offset
);
4534 radeon_emit(cs
, info
->first_instance
);
4535 if (state
->pipeline
->graphics
.vtx_emit_num
== 3)
4537 state
->last_first_instance
= info
->first_instance
;
4538 state
->last_vertex_offset
= info
->vertex_offset
;
4541 if (state
->last_num_instances
!= info
->instance_count
) {
4542 radeon_emit(cs
, PKT3(PKT3_NUM_INSTANCES
, 0, false));
4543 radeon_emit(cs
, info
->instance_count
);
4544 state
->last_num_instances
= info
->instance_count
;
4547 if (info
->indexed
) {
4548 int index_size
= radv_get_vgt_index_size(state
->index_type
);
4551 /* Skip draw calls with 0-sized index buffers. They
4552 * cause a hang on some chips, like Navi10-14.
4554 if (!cmd_buffer
->state
.max_index_count
)
4557 index_va
= state
->index_va
;
4558 index_va
+= info
->first_index
* index_size
;
4560 if (!state
->subpass
->view_mask
) {
4561 radv_cs_emit_draw_indexed_packet(cmd_buffer
,
4566 for_each_bit(i
, state
->subpass
->view_mask
) {
4567 radv_emit_view_index(cmd_buffer
, i
);
4569 radv_cs_emit_draw_indexed_packet(cmd_buffer
,
4575 if (!state
->subpass
->view_mask
) {
4576 radv_cs_emit_draw_packet(cmd_buffer
,
4578 !!info
->strmout_buffer
);
4581 for_each_bit(i
, state
->subpass
->view_mask
) {
4582 radv_emit_view_index(cmd_buffer
, i
);
4584 radv_cs_emit_draw_packet(cmd_buffer
,
4586 !!info
->strmout_buffer
);
4594 * Vega and raven have a bug which triggers if there are multiple context
4595 * register contexts active at the same time with different scissor values.
4597 * There are two possible workarounds:
4598 * 1) Wait for PS_PARTIAL_FLUSH every time the scissor is changed. That way
4599 * there is only ever 1 active set of scissor values at the same time.
4601 * 2) Whenever the hardware switches contexts we have to set the scissor
4602 * registers again even if it is a noop. That way the new context gets
4603 * the correct scissor values.
4605 * This implements option 2. radv_need_late_scissor_emission needs to
4606 * return true on affected HW if radv_emit_all_graphics_states sets
4607 * any context registers.
4609 static bool radv_need_late_scissor_emission(struct radv_cmd_buffer
*cmd_buffer
,
4610 const struct radv_draw_info
*info
)
4612 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
4614 if (!cmd_buffer
->device
->physical_device
->rad_info
.has_gfx9_scissor_bug
)
4617 if (cmd_buffer
->state
.context_roll_without_scissor_emitted
|| info
->strmout_buffer
)
4620 uint32_t used_states
= cmd_buffer
->state
.pipeline
->graphics
.needed_dynamic_state
| ~RADV_CMD_DIRTY_DYNAMIC_ALL
;
4622 /* Index, vertex and streamout buffers don't change context regs, and
4623 * pipeline is already handled.
4625 used_states
&= ~(RADV_CMD_DIRTY_INDEX_BUFFER
|
4626 RADV_CMD_DIRTY_VERTEX_BUFFER
|
4627 RADV_CMD_DIRTY_STREAMOUT_BUFFER
|
4628 RADV_CMD_DIRTY_PIPELINE
);
4630 if (cmd_buffer
->state
.dirty
& used_states
)
4633 uint32_t primitive_reset_index
=
4634 radv_get_primitive_reset_index(cmd_buffer
);
4636 if (info
->indexed
&& state
->pipeline
->graphics
.prim_restart_enable
&&
4637 primitive_reset_index
!= state
->last_primitive_reset_index
)
4644 radv_emit_all_graphics_states(struct radv_cmd_buffer
*cmd_buffer
,
4645 const struct radv_draw_info
*info
)
4647 bool late_scissor_emission
;
4649 if ((cmd_buffer
->state
.dirty
& RADV_CMD_DIRTY_FRAMEBUFFER
) ||
4650 cmd_buffer
->state
.emitted_pipeline
!= cmd_buffer
->state
.pipeline
)
4651 radv_emit_rbplus_state(cmd_buffer
);
4653 if (cmd_buffer
->state
.dirty
& RADV_CMD_DIRTY_PIPELINE
)
4654 radv_emit_graphics_pipeline(cmd_buffer
);
4656 /* This should be before the cmd_buffer->state.dirty is cleared
4657 * (excluding RADV_CMD_DIRTY_PIPELINE) and after
4658 * cmd_buffer->state.context_roll_without_scissor_emitted is set. */
4659 late_scissor_emission
=
4660 radv_need_late_scissor_emission(cmd_buffer
, info
);
4662 if (cmd_buffer
->state
.dirty
& RADV_CMD_DIRTY_FRAMEBUFFER
)
4663 radv_emit_framebuffer_state(cmd_buffer
);
4665 if (info
->indexed
) {
4666 if (cmd_buffer
->state
.dirty
& RADV_CMD_DIRTY_INDEX_BUFFER
)
4667 radv_emit_index_buffer(cmd_buffer
);
4669 /* On GFX7 and later, non-indexed draws overwrite VGT_INDEX_TYPE,
4670 * so the state must be re-emitted before the next indexed
4673 if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX7
) {
4674 cmd_buffer
->state
.last_index_type
= -1;
4675 cmd_buffer
->state
.dirty
|= RADV_CMD_DIRTY_INDEX_BUFFER
;
4679 radv_cmd_buffer_flush_dynamic_state(cmd_buffer
);
4681 radv_emit_draw_registers(cmd_buffer
, info
);
4683 if (late_scissor_emission
)
4684 radv_emit_scissor(cmd_buffer
);
4688 radv_draw(struct radv_cmd_buffer
*cmd_buffer
,
4689 const struct radv_draw_info
*info
)
4691 struct radeon_info
*rad_info
=
4692 &cmd_buffer
->device
->physical_device
->rad_info
;
4694 cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX7
;
4695 bool pipeline_is_dirty
=
4696 (cmd_buffer
->state
.dirty
& RADV_CMD_DIRTY_PIPELINE
) &&
4697 cmd_buffer
->state
.pipeline
!= cmd_buffer
->state
.emitted_pipeline
;
4699 ASSERTED
unsigned cdw_max
=
4700 radeon_check_space(cmd_buffer
->device
->ws
,
4701 cmd_buffer
->cs
, 4096);
4703 if (likely(!info
->indirect
)) {
4704 /* GFX6-GFX7 treat instance_count==0 as instance_count==1. There is
4705 * no workaround for indirect draws, but we can at least skip
4708 if (unlikely(!info
->instance_count
))
4711 /* Handle count == 0. */
4712 if (unlikely(!info
->count
&& !info
->strmout_buffer
))
4716 /* Use optimal packet order based on whether we need to sync the
4719 if (cmd_buffer
->state
.flush_bits
& (RADV_CMD_FLAG_FLUSH_AND_INV_CB
|
4720 RADV_CMD_FLAG_FLUSH_AND_INV_DB
|
4721 RADV_CMD_FLAG_PS_PARTIAL_FLUSH
|
4722 RADV_CMD_FLAG_CS_PARTIAL_FLUSH
)) {
4723 /* If we have to wait for idle, set all states first, so that
4724 * all SET packets are processed in parallel with previous draw
4725 * calls. Then upload descriptors, set shader pointers, and
4726 * draw, and prefetch at the end. This ensures that the time
4727 * the CUs are idle is very short. (there are only SET_SH
4728 * packets between the wait and the draw)
4730 radv_emit_all_graphics_states(cmd_buffer
, info
);
4731 si_emit_cache_flush(cmd_buffer
);
4732 /* <-- CUs are idle here --> */
4734 radv_upload_graphics_shader_descriptors(cmd_buffer
, pipeline_is_dirty
);
4736 radv_emit_draw_packets(cmd_buffer
, info
);
4737 /* <-- CUs are busy here --> */
4739 /* Start prefetches after the draw has been started. Both will
4740 * run in parallel, but starting the draw first is more
4743 if (has_prefetch
&& cmd_buffer
->state
.prefetch_L2_mask
) {
4744 radv_emit_prefetch_L2(cmd_buffer
,
4745 cmd_buffer
->state
.pipeline
, false);
4748 /* If we don't wait for idle, start prefetches first, then set
4749 * states, and draw at the end.
4751 si_emit_cache_flush(cmd_buffer
);
4753 if (has_prefetch
&& cmd_buffer
->state
.prefetch_L2_mask
) {
4754 /* Only prefetch the vertex shader and VBO descriptors
4755 * in order to start the draw as soon as possible.
4757 radv_emit_prefetch_L2(cmd_buffer
,
4758 cmd_buffer
->state
.pipeline
, true);
4761 radv_upload_graphics_shader_descriptors(cmd_buffer
, pipeline_is_dirty
);
4763 radv_emit_all_graphics_states(cmd_buffer
, info
);
4764 radv_emit_draw_packets(cmd_buffer
, info
);
4766 /* Prefetch the remaining shaders after the draw has been
4769 if (has_prefetch
&& cmd_buffer
->state
.prefetch_L2_mask
) {
4770 radv_emit_prefetch_L2(cmd_buffer
,
4771 cmd_buffer
->state
.pipeline
, false);
4775 /* Workaround for a VGT hang when streamout is enabled.
4776 * It must be done after drawing.
4778 if (cmd_buffer
->state
.streamout
.streamout_enabled
&&
4779 (rad_info
->family
== CHIP_HAWAII
||
4780 rad_info
->family
== CHIP_TONGA
||
4781 rad_info
->family
== CHIP_FIJI
)) {
4782 cmd_buffer
->state
.flush_bits
|= RADV_CMD_FLAG_VGT_STREAMOUT_SYNC
;
4785 assert(cmd_buffer
->cs
->cdw
<= cdw_max
);
4786 radv_cmd_buffer_after_draw(cmd_buffer
, RADV_CMD_FLAG_PS_PARTIAL_FLUSH
);
4790 VkCommandBuffer commandBuffer
,
4791 uint32_t vertexCount
,
4792 uint32_t instanceCount
,
4793 uint32_t firstVertex
,
4794 uint32_t firstInstance
)
4796 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
4797 struct radv_draw_info info
= {};
4799 info
.count
= vertexCount
;
4800 info
.instance_count
= instanceCount
;
4801 info
.first_instance
= firstInstance
;
4802 info
.vertex_offset
= firstVertex
;
4804 radv_draw(cmd_buffer
, &info
);
4807 void radv_CmdDrawIndexed(
4808 VkCommandBuffer commandBuffer
,
4809 uint32_t indexCount
,
4810 uint32_t instanceCount
,
4811 uint32_t firstIndex
,
4812 int32_t vertexOffset
,
4813 uint32_t firstInstance
)
4815 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
4816 struct radv_draw_info info
= {};
4818 info
.indexed
= true;
4819 info
.count
= indexCount
;
4820 info
.instance_count
= instanceCount
;
4821 info
.first_index
= firstIndex
;
4822 info
.vertex_offset
= vertexOffset
;
4823 info
.first_instance
= firstInstance
;
4825 radv_draw(cmd_buffer
, &info
);
4828 void radv_CmdDrawIndirect(
4829 VkCommandBuffer commandBuffer
,
4831 VkDeviceSize offset
,
4835 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
4836 RADV_FROM_HANDLE(radv_buffer
, buffer
, _buffer
);
4837 struct radv_draw_info info
= {};
4839 info
.count
= drawCount
;
4840 info
.indirect
= buffer
;
4841 info
.indirect_offset
= offset
;
4842 info
.stride
= stride
;
4844 radv_draw(cmd_buffer
, &info
);
4847 void radv_CmdDrawIndexedIndirect(
4848 VkCommandBuffer commandBuffer
,
4850 VkDeviceSize offset
,
4854 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
4855 RADV_FROM_HANDLE(radv_buffer
, buffer
, _buffer
);
4856 struct radv_draw_info info
= {};
4858 info
.indexed
= true;
4859 info
.count
= drawCount
;
4860 info
.indirect
= buffer
;
4861 info
.indirect_offset
= offset
;
4862 info
.stride
= stride
;
4864 radv_draw(cmd_buffer
, &info
);
4867 void radv_CmdDrawIndirectCount(
4868 VkCommandBuffer commandBuffer
,
4870 VkDeviceSize offset
,
4871 VkBuffer _countBuffer
,
4872 VkDeviceSize countBufferOffset
,
4873 uint32_t maxDrawCount
,
4876 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
4877 RADV_FROM_HANDLE(radv_buffer
, buffer
, _buffer
);
4878 RADV_FROM_HANDLE(radv_buffer
, count_buffer
, _countBuffer
);
4879 struct radv_draw_info info
= {};
4881 info
.count
= maxDrawCount
;
4882 info
.indirect
= buffer
;
4883 info
.indirect_offset
= offset
;
4884 info
.count_buffer
= count_buffer
;
4885 info
.count_buffer_offset
= countBufferOffset
;
4886 info
.stride
= stride
;
4888 radv_draw(cmd_buffer
, &info
);
4891 void radv_CmdDrawIndexedIndirectCount(
4892 VkCommandBuffer commandBuffer
,
4894 VkDeviceSize offset
,
4895 VkBuffer _countBuffer
,
4896 VkDeviceSize countBufferOffset
,
4897 uint32_t maxDrawCount
,
4900 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
4901 RADV_FROM_HANDLE(radv_buffer
, buffer
, _buffer
);
4902 RADV_FROM_HANDLE(radv_buffer
, count_buffer
, _countBuffer
);
4903 struct radv_draw_info info
= {};
4905 info
.indexed
= true;
4906 info
.count
= maxDrawCount
;
4907 info
.indirect
= buffer
;
4908 info
.indirect_offset
= offset
;
4909 info
.count_buffer
= count_buffer
;
4910 info
.count_buffer_offset
= countBufferOffset
;
4911 info
.stride
= stride
;
4913 radv_draw(cmd_buffer
, &info
);
4916 struct radv_dispatch_info
{
4918 * Determine the layout of the grid (in block units) to be used.
4923 * A starting offset for the grid. If unaligned is set, the offset
4924 * must still be aligned.
4926 uint32_t offsets
[3];
4928 * Whether it's an unaligned compute dispatch.
4933 * Indirect compute parameters resource.
4935 struct radv_buffer
*indirect
;
4936 uint64_t indirect_offset
;
4940 radv_emit_dispatch_packets(struct radv_cmd_buffer
*cmd_buffer
,
4941 const struct radv_dispatch_info
*info
)
4943 struct radv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
4944 struct radv_shader_variant
*compute_shader
= pipeline
->shaders
[MESA_SHADER_COMPUTE
];
4945 unsigned dispatch_initiator
= cmd_buffer
->device
->dispatch_initiator
;
4946 struct radeon_winsys
*ws
= cmd_buffer
->device
->ws
;
4947 bool predicating
= cmd_buffer
->state
.predicating
;
4948 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
4949 struct radv_userdata_info
*loc
;
4951 loc
= radv_lookup_user_sgpr(pipeline
, MESA_SHADER_COMPUTE
,
4952 AC_UD_CS_GRID_SIZE
);
4954 ASSERTED
unsigned cdw_max
= radeon_check_space(ws
, cs
, 25);
4956 if (compute_shader
->info
.wave_size
== 32) {
4957 assert(cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX10
);
4958 dispatch_initiator
|= S_00B800_CS_W32_EN(1);
4961 if (info
->indirect
) {
4962 uint64_t va
= radv_buffer_get_va(info
->indirect
->bo
);
4964 va
+= info
->indirect
->offset
+ info
->indirect_offset
;
4966 radv_cs_add_buffer(ws
, cs
, info
->indirect
->bo
);
4968 if (loc
->sgpr_idx
!= -1) {
4969 for (unsigned i
= 0; i
< 3; ++i
) {
4970 radeon_emit(cs
, PKT3(PKT3_COPY_DATA
, 4, 0));
4971 radeon_emit(cs
, COPY_DATA_SRC_SEL(COPY_DATA_SRC_MEM
) |
4972 COPY_DATA_DST_SEL(COPY_DATA_REG
));
4973 radeon_emit(cs
, (va
+ 4 * i
));
4974 radeon_emit(cs
, (va
+ 4 * i
) >> 32);
4975 radeon_emit(cs
, ((R_00B900_COMPUTE_USER_DATA_0
4976 + loc
->sgpr_idx
* 4) >> 2) + i
);
4981 if (radv_cmd_buffer_uses_mec(cmd_buffer
)) {
4982 radeon_emit(cs
, PKT3(PKT3_DISPATCH_INDIRECT
, 2, predicating
) |
4983 PKT3_SHADER_TYPE_S(1));
4984 radeon_emit(cs
, va
);
4985 radeon_emit(cs
, va
>> 32);
4986 radeon_emit(cs
, dispatch_initiator
);
4988 radeon_emit(cs
, PKT3(PKT3_SET_BASE
, 2, 0) |
4989 PKT3_SHADER_TYPE_S(1));
4991 radeon_emit(cs
, va
);
4992 radeon_emit(cs
, va
>> 32);
4994 radeon_emit(cs
, PKT3(PKT3_DISPATCH_INDIRECT
, 1, predicating
) |
4995 PKT3_SHADER_TYPE_S(1));
4997 radeon_emit(cs
, dispatch_initiator
);
5000 unsigned blocks
[3] = { info
->blocks
[0], info
->blocks
[1], info
->blocks
[2] };
5001 unsigned offsets
[3] = { info
->offsets
[0], info
->offsets
[1], info
->offsets
[2] };
5003 if (info
->unaligned
) {
5004 unsigned *cs_block_size
= compute_shader
->info
.cs
.block_size
;
5005 unsigned remainder
[3];
5007 /* If aligned, these should be an entire block size,
5010 remainder
[0] = blocks
[0] + cs_block_size
[0] -
5011 align_u32_npot(blocks
[0], cs_block_size
[0]);
5012 remainder
[1] = blocks
[1] + cs_block_size
[1] -
5013 align_u32_npot(blocks
[1], cs_block_size
[1]);
5014 remainder
[2] = blocks
[2] + cs_block_size
[2] -
5015 align_u32_npot(blocks
[2], cs_block_size
[2]);
5017 blocks
[0] = round_up_u32(blocks
[0], cs_block_size
[0]);
5018 blocks
[1] = round_up_u32(blocks
[1], cs_block_size
[1]);
5019 blocks
[2] = round_up_u32(blocks
[2], cs_block_size
[2]);
5021 for(unsigned i
= 0; i
< 3; ++i
) {
5022 assert(offsets
[i
] % cs_block_size
[i
] == 0);
5023 offsets
[i
] /= cs_block_size
[i
];
5026 radeon_set_sh_reg_seq(cs
, R_00B81C_COMPUTE_NUM_THREAD_X
, 3);
5028 S_00B81C_NUM_THREAD_FULL(cs_block_size
[0]) |
5029 S_00B81C_NUM_THREAD_PARTIAL(remainder
[0]));
5031 S_00B81C_NUM_THREAD_FULL(cs_block_size
[1]) |
5032 S_00B81C_NUM_THREAD_PARTIAL(remainder
[1]));
5034 S_00B81C_NUM_THREAD_FULL(cs_block_size
[2]) |
5035 S_00B81C_NUM_THREAD_PARTIAL(remainder
[2]));
5037 dispatch_initiator
|= S_00B800_PARTIAL_TG_EN(1);
5040 if (loc
->sgpr_idx
!= -1) {
5041 assert(loc
->num_sgprs
== 3);
5043 radeon_set_sh_reg_seq(cs
, R_00B900_COMPUTE_USER_DATA_0
+
5044 loc
->sgpr_idx
* 4, 3);
5045 radeon_emit(cs
, blocks
[0]);
5046 radeon_emit(cs
, blocks
[1]);
5047 radeon_emit(cs
, blocks
[2]);
5050 if (offsets
[0] || offsets
[1] || offsets
[2]) {
5051 radeon_set_sh_reg_seq(cs
, R_00B810_COMPUTE_START_X
, 3);
5052 radeon_emit(cs
, offsets
[0]);
5053 radeon_emit(cs
, offsets
[1]);
5054 radeon_emit(cs
, offsets
[2]);
5056 /* The blocks in the packet are not counts but end values. */
5057 for (unsigned i
= 0; i
< 3; ++i
)
5058 blocks
[i
] += offsets
[i
];
5060 dispatch_initiator
|= S_00B800_FORCE_START_AT_000(1);
5063 radeon_emit(cs
, PKT3(PKT3_DISPATCH_DIRECT
, 3, predicating
) |
5064 PKT3_SHADER_TYPE_S(1));
5065 radeon_emit(cs
, blocks
[0]);
5066 radeon_emit(cs
, blocks
[1]);
5067 radeon_emit(cs
, blocks
[2]);
5068 radeon_emit(cs
, dispatch_initiator
);
5071 assert(cmd_buffer
->cs
->cdw
<= cdw_max
);
5075 radv_upload_compute_shader_descriptors(struct radv_cmd_buffer
*cmd_buffer
)
5077 radv_flush_descriptors(cmd_buffer
, VK_SHADER_STAGE_COMPUTE_BIT
);
5078 radv_flush_constants(cmd_buffer
, VK_SHADER_STAGE_COMPUTE_BIT
);
5082 radv_dispatch(struct radv_cmd_buffer
*cmd_buffer
,
5083 const struct radv_dispatch_info
*info
)
5085 struct radv_pipeline
*pipeline
= cmd_buffer
->state
.compute_pipeline
;
5087 cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX7
;
5088 bool pipeline_is_dirty
= pipeline
&&
5089 pipeline
!= cmd_buffer
->state
.emitted_compute_pipeline
;
5091 if (cmd_buffer
->state
.flush_bits
& (RADV_CMD_FLAG_FLUSH_AND_INV_CB
|
5092 RADV_CMD_FLAG_FLUSH_AND_INV_DB
|
5093 RADV_CMD_FLAG_PS_PARTIAL_FLUSH
|
5094 RADV_CMD_FLAG_CS_PARTIAL_FLUSH
)) {
5095 /* If we have to wait for idle, set all states first, so that
5096 * all SET packets are processed in parallel with previous draw
5097 * calls. Then upload descriptors, set shader pointers, and
5098 * dispatch, and prefetch at the end. This ensures that the
5099 * time the CUs are idle is very short. (there are only SET_SH
5100 * packets between the wait and the draw)
5102 radv_emit_compute_pipeline(cmd_buffer
);
5103 si_emit_cache_flush(cmd_buffer
);
5104 /* <-- CUs are idle here --> */
5106 radv_upload_compute_shader_descriptors(cmd_buffer
);
5108 radv_emit_dispatch_packets(cmd_buffer
, info
);
5109 /* <-- CUs are busy here --> */
5111 /* Start prefetches after the dispatch has been started. Both
5112 * will run in parallel, but starting the dispatch first is
5115 if (has_prefetch
&& pipeline_is_dirty
) {
5116 radv_emit_shader_prefetch(cmd_buffer
,
5117 pipeline
->shaders
[MESA_SHADER_COMPUTE
]);
5120 /* If we don't wait for idle, start prefetches first, then set
5121 * states, and dispatch at the end.
5123 si_emit_cache_flush(cmd_buffer
);
5125 if (has_prefetch
&& pipeline_is_dirty
) {
5126 radv_emit_shader_prefetch(cmd_buffer
,
5127 pipeline
->shaders
[MESA_SHADER_COMPUTE
]);
5130 radv_upload_compute_shader_descriptors(cmd_buffer
);
5132 radv_emit_compute_pipeline(cmd_buffer
);
5133 radv_emit_dispatch_packets(cmd_buffer
, info
);
5136 radv_cmd_buffer_after_draw(cmd_buffer
, RADV_CMD_FLAG_CS_PARTIAL_FLUSH
);
5139 void radv_CmdDispatchBase(
5140 VkCommandBuffer commandBuffer
,
5148 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5149 struct radv_dispatch_info info
= {};
5155 info
.offsets
[0] = base_x
;
5156 info
.offsets
[1] = base_y
;
5157 info
.offsets
[2] = base_z
;
5158 radv_dispatch(cmd_buffer
, &info
);
5161 void radv_CmdDispatch(
5162 VkCommandBuffer commandBuffer
,
5167 radv_CmdDispatchBase(commandBuffer
, 0, 0, 0, x
, y
, z
);
5170 void radv_CmdDispatchIndirect(
5171 VkCommandBuffer commandBuffer
,
5173 VkDeviceSize offset
)
5175 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5176 RADV_FROM_HANDLE(radv_buffer
, buffer
, _buffer
);
5177 struct radv_dispatch_info info
= {};
5179 info
.indirect
= buffer
;
5180 info
.indirect_offset
= offset
;
5182 radv_dispatch(cmd_buffer
, &info
);
5185 void radv_unaligned_dispatch(
5186 struct radv_cmd_buffer
*cmd_buffer
,
5191 struct radv_dispatch_info info
= {};
5198 radv_dispatch(cmd_buffer
, &info
);
5201 void radv_CmdEndRenderPass(
5202 VkCommandBuffer commandBuffer
)
5204 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5206 radv_subpass_barrier(cmd_buffer
, &cmd_buffer
->state
.pass
->end_barrier
);
5208 radv_cmd_buffer_end_subpass(cmd_buffer
);
5210 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
->state
.attachments
);
5211 vk_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
->state
.subpass_sample_locs
);
5213 cmd_buffer
->state
.pass
= NULL
;
5214 cmd_buffer
->state
.subpass
= NULL
;
5215 cmd_buffer
->state
.attachments
= NULL
;
5216 cmd_buffer
->state
.framebuffer
= NULL
;
5217 cmd_buffer
->state
.subpass_sample_locs
= NULL
;
5220 void radv_CmdEndRenderPass2(
5221 VkCommandBuffer commandBuffer
,
5222 const VkSubpassEndInfo
* pSubpassEndInfo
)
5224 radv_CmdEndRenderPass(commandBuffer
);
5228 * For HTILE we have the following interesting clear words:
5229 * 0xfffff30f: Uncompressed, full depth range, for depth+stencil HTILE
5230 * 0xfffc000f: Uncompressed, full depth range, for depth only HTILE.
5231 * 0xfffffff0: Clear depth to 1.0
5232 * 0x00000000: Clear depth to 0.0
5234 static void radv_initialize_htile(struct radv_cmd_buffer
*cmd_buffer
,
5235 struct radv_image
*image
,
5236 const VkImageSubresourceRange
*range
)
5238 assert(range
->baseMipLevel
== 0);
5239 assert(range
->levelCount
== 1 || range
->levelCount
== VK_REMAINING_ARRAY_LAYERS
);
5240 VkImageAspectFlags aspects
= VK_IMAGE_ASPECT_DEPTH_BIT
;
5241 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
5242 uint32_t htile_value
= vk_format_is_stencil(image
->vk_format
) ? 0xfffff30f : 0xfffc000f;
5243 VkClearDepthStencilValue value
= {};
5245 state
->flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_DB
|
5246 RADV_CMD_FLAG_FLUSH_AND_INV_DB_META
;
5248 state
->flush_bits
|= radv_clear_htile(cmd_buffer
, image
, range
, htile_value
);
5250 state
->flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_DB_META
;
5252 if (vk_format_is_stencil(image
->vk_format
))
5253 aspects
|= VK_IMAGE_ASPECT_STENCIL_BIT
;
5255 radv_set_ds_clear_metadata(cmd_buffer
, image
, range
, value
, aspects
);
5257 if (radv_image_is_tc_compat_htile(image
)) {
5258 /* Initialize the TC-compat metada value to 0 because by
5259 * default DB_Z_INFO.RANGE_PRECISION is set to 1, and we only
5260 * need have to conditionally update its value when performing
5261 * a fast depth clear.
5263 radv_set_tc_compat_zrange_metadata(cmd_buffer
, image
, range
, 0);
5267 static void radv_handle_depth_image_transition(struct radv_cmd_buffer
*cmd_buffer
,
5268 struct radv_image
*image
,
5269 VkImageLayout src_layout
,
5270 bool src_render_loop
,
5271 VkImageLayout dst_layout
,
5272 bool dst_render_loop
,
5273 unsigned src_queue_mask
,
5274 unsigned dst_queue_mask
,
5275 const VkImageSubresourceRange
*range
,
5276 struct radv_sample_locations_state
*sample_locs
)
5278 if (!radv_image_has_htile(image
))
5281 if (src_layout
== VK_IMAGE_LAYOUT_UNDEFINED
) {
5282 radv_initialize_htile(cmd_buffer
, image
, range
);
5283 } else if (!radv_layout_is_htile_compressed(image
, src_layout
, src_render_loop
, src_queue_mask
) &&
5284 radv_layout_is_htile_compressed(image
, dst_layout
, dst_render_loop
, dst_queue_mask
)) {
5285 radv_initialize_htile(cmd_buffer
, image
, range
);
5286 } else if (radv_layout_is_htile_compressed(image
, src_layout
, src_render_loop
, src_queue_mask
) &&
5287 !radv_layout_is_htile_compressed(image
, dst_layout
, dst_render_loop
, dst_queue_mask
)) {
5288 cmd_buffer
->state
.flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_DB
|
5289 RADV_CMD_FLAG_FLUSH_AND_INV_DB_META
;
5291 radv_decompress_depth_image_inplace(cmd_buffer
, image
, range
,
5294 cmd_buffer
->state
.flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_DB
|
5295 RADV_CMD_FLAG_FLUSH_AND_INV_DB_META
;
5299 static void radv_initialise_cmask(struct radv_cmd_buffer
*cmd_buffer
,
5300 struct radv_image
*image
,
5301 const VkImageSubresourceRange
*range
,
5304 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
5306 state
->flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_CB
|
5307 RADV_CMD_FLAG_FLUSH_AND_INV_CB_META
;
5309 state
->flush_bits
|= radv_clear_cmask(cmd_buffer
, image
, range
, value
);
5311 state
->flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_CB_META
;
5314 void radv_initialize_fmask(struct radv_cmd_buffer
*cmd_buffer
,
5315 struct radv_image
*image
,
5316 const VkImageSubresourceRange
*range
)
5318 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
5319 static const uint32_t fmask_clear_values
[4] = {
5325 uint32_t log2_samples
= util_logbase2(image
->info
.samples
);
5326 uint32_t value
= fmask_clear_values
[log2_samples
];
5328 state
->flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_CB
|
5329 RADV_CMD_FLAG_FLUSH_AND_INV_CB_META
;
5331 state
->flush_bits
|= radv_clear_fmask(cmd_buffer
, image
, range
, value
);
5333 state
->flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_CB_META
;
5336 void radv_initialize_dcc(struct radv_cmd_buffer
*cmd_buffer
,
5337 struct radv_image
*image
,
5338 const VkImageSubresourceRange
*range
, uint32_t value
)
5340 struct radv_cmd_state
*state
= &cmd_buffer
->state
;
5343 state
->flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_CB
|
5344 RADV_CMD_FLAG_FLUSH_AND_INV_CB_META
;
5346 state
->flush_bits
|= radv_clear_dcc(cmd_buffer
, image
, range
, value
);
5348 if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
== GFX8
) {
5349 /* When DCC is enabled with mipmaps, some levels might not
5350 * support fast clears and we have to initialize them as "fully
5353 /* Compute the size of all fast clearable DCC levels. */
5354 for (unsigned i
= 0; i
< image
->planes
[0].surface
.num_dcc_levels
; i
++) {
5355 struct legacy_surf_level
*surf_level
=
5356 &image
->planes
[0].surface
.u
.legacy
.level
[i
];
5357 unsigned dcc_fast_clear_size
=
5358 surf_level
->dcc_slice_fast_clear_size
* image
->info
.array_size
;
5360 if (!dcc_fast_clear_size
)
5363 size
= surf_level
->dcc_offset
+ dcc_fast_clear_size
;
5366 /* Initialize the mipmap levels without DCC. */
5367 if (size
!= image
->planes
[0].surface
.dcc_size
) {
5368 state
->flush_bits
|=
5369 radv_fill_buffer(cmd_buffer
, image
->bo
,
5370 image
->offset
+ image
->dcc_offset
+ size
,
5371 image
->planes
[0].surface
.dcc_size
- size
,
5376 state
->flush_bits
|= RADV_CMD_FLAG_FLUSH_AND_INV_CB
|
5377 RADV_CMD_FLAG_FLUSH_AND_INV_CB_META
;
5381 * Initialize DCC/FMASK/CMASK metadata for a color image.
5383 static void radv_init_color_image_metadata(struct radv_cmd_buffer
*cmd_buffer
,
5384 struct radv_image
*image
,
5385 VkImageLayout src_layout
,
5386 bool src_render_loop
,
5387 VkImageLayout dst_layout
,
5388 bool dst_render_loop
,
5389 unsigned src_queue_mask
,
5390 unsigned dst_queue_mask
,
5391 const VkImageSubresourceRange
*range
)
5393 if (radv_image_has_cmask(image
)) {
5394 uint32_t value
= 0xffffffffu
; /* Fully expanded mode. */
5396 /* TODO: clarify this. */
5397 if (radv_image_has_fmask(image
)) {
5398 value
= 0xccccccccu
;
5401 radv_initialise_cmask(cmd_buffer
, image
, range
, value
);
5404 if (radv_image_has_fmask(image
)) {
5405 radv_initialize_fmask(cmd_buffer
, image
, range
);
5408 if (radv_dcc_enabled(image
, range
->baseMipLevel
)) {
5409 uint32_t value
= 0xffffffffu
; /* Fully expanded mode. */
5410 bool need_decompress_pass
= false;
5412 if (radv_layout_dcc_compressed(cmd_buffer
->device
, image
, dst_layout
,
5415 value
= 0x20202020u
;
5416 need_decompress_pass
= true;
5419 radv_initialize_dcc(cmd_buffer
, image
, range
, value
);
5421 radv_update_fce_metadata(cmd_buffer
, image
, range
,
5422 need_decompress_pass
);
5425 if (radv_image_has_cmask(image
) ||
5426 radv_dcc_enabled(image
, range
->baseMipLevel
)) {
5427 uint32_t color_values
[2] = {};
5428 radv_set_color_clear_metadata(cmd_buffer
, image
, range
,
5434 * Handle color image transitions for DCC/FMASK/CMASK.
5436 static void radv_handle_color_image_transition(struct radv_cmd_buffer
*cmd_buffer
,
5437 struct radv_image
*image
,
5438 VkImageLayout src_layout
,
5439 bool src_render_loop
,
5440 VkImageLayout dst_layout
,
5441 bool dst_render_loop
,
5442 unsigned src_queue_mask
,
5443 unsigned dst_queue_mask
,
5444 const VkImageSubresourceRange
*range
)
5446 if (src_layout
== VK_IMAGE_LAYOUT_UNDEFINED
) {
5447 radv_init_color_image_metadata(cmd_buffer
, image
,
5448 src_layout
, src_render_loop
,
5449 dst_layout
, dst_render_loop
,
5450 src_queue_mask
, dst_queue_mask
,
5455 if (radv_dcc_enabled(image
, range
->baseMipLevel
)) {
5456 if (src_layout
== VK_IMAGE_LAYOUT_PREINITIALIZED
) {
5457 radv_initialize_dcc(cmd_buffer
, image
, range
, 0xffffffffu
);
5458 } else if (radv_layout_dcc_compressed(cmd_buffer
->device
, image
, src_layout
, src_render_loop
, src_queue_mask
) &&
5459 !radv_layout_dcc_compressed(cmd_buffer
->device
, image
, dst_layout
, dst_render_loop
, dst_queue_mask
)) {
5460 radv_decompress_dcc(cmd_buffer
, image
, range
);
5461 } else if (radv_layout_can_fast_clear(image
, src_layout
, src_render_loop
, src_queue_mask
) &&
5462 !radv_layout_can_fast_clear(image
, dst_layout
, dst_render_loop
, dst_queue_mask
)) {
5463 radv_fast_clear_flush_image_inplace(cmd_buffer
, image
, range
);
5465 } else if (radv_image_has_cmask(image
) || radv_image_has_fmask(image
)) {
5466 bool fce_eliminate
= false, fmask_expand
= false;
5468 if (radv_layout_can_fast_clear(image
, src_layout
, src_render_loop
, src_queue_mask
) &&
5469 !radv_layout_can_fast_clear(image
, dst_layout
, dst_render_loop
, dst_queue_mask
)) {
5470 fce_eliminate
= true;
5473 if (radv_image_has_fmask(image
)) {
5474 if (src_layout
!= VK_IMAGE_LAYOUT_GENERAL
&&
5475 dst_layout
== VK_IMAGE_LAYOUT_GENERAL
) {
5476 /* A FMASK decompress is required before doing
5477 * a MSAA decompress using FMASK.
5479 fmask_expand
= true;
5483 if (fce_eliminate
|| fmask_expand
)
5484 radv_fast_clear_flush_image_inplace(cmd_buffer
, image
, range
);
5487 radv_expand_fmask_image_inplace(cmd_buffer
, image
, range
);
5491 static void radv_handle_image_transition(struct radv_cmd_buffer
*cmd_buffer
,
5492 struct radv_image
*image
,
5493 VkImageLayout src_layout
,
5494 bool src_render_loop
,
5495 VkImageLayout dst_layout
,
5496 bool dst_render_loop
,
5497 uint32_t src_family
,
5498 uint32_t dst_family
,
5499 const VkImageSubresourceRange
*range
,
5500 struct radv_sample_locations_state
*sample_locs
)
5502 if (image
->exclusive
&& src_family
!= dst_family
) {
5503 /* This is an acquire or a release operation and there will be
5504 * a corresponding release/acquire. Do the transition in the
5505 * most flexible queue. */
5507 assert(src_family
== cmd_buffer
->queue_family_index
||
5508 dst_family
== cmd_buffer
->queue_family_index
);
5510 if (src_family
== VK_QUEUE_FAMILY_EXTERNAL
||
5511 src_family
== VK_QUEUE_FAMILY_FOREIGN_EXT
)
5514 if (cmd_buffer
->queue_family_index
== RADV_QUEUE_TRANSFER
)
5517 if (cmd_buffer
->queue_family_index
== RADV_QUEUE_COMPUTE
&&
5518 (src_family
== RADV_QUEUE_GENERAL
||
5519 dst_family
== RADV_QUEUE_GENERAL
))
5523 if (src_layout
== dst_layout
)
5526 unsigned src_queue_mask
=
5527 radv_image_queue_family_mask(image
, src_family
,
5528 cmd_buffer
->queue_family_index
);
5529 unsigned dst_queue_mask
=
5530 radv_image_queue_family_mask(image
, dst_family
,
5531 cmd_buffer
->queue_family_index
);
5533 if (vk_format_is_depth(image
->vk_format
)) {
5534 radv_handle_depth_image_transition(cmd_buffer
, image
,
5535 src_layout
, src_render_loop
,
5536 dst_layout
, dst_render_loop
,
5537 src_queue_mask
, dst_queue_mask
,
5538 range
, sample_locs
);
5540 radv_handle_color_image_transition(cmd_buffer
, image
,
5541 src_layout
, src_render_loop
,
5542 dst_layout
, dst_render_loop
,
5543 src_queue_mask
, dst_queue_mask
,
5548 struct radv_barrier_info
{
5549 uint32_t eventCount
;
5550 const VkEvent
*pEvents
;
5551 VkPipelineStageFlags srcStageMask
;
5552 VkPipelineStageFlags dstStageMask
;
5556 radv_barrier(struct radv_cmd_buffer
*cmd_buffer
,
5557 uint32_t memoryBarrierCount
,
5558 const VkMemoryBarrier
*pMemoryBarriers
,
5559 uint32_t bufferMemoryBarrierCount
,
5560 const VkBufferMemoryBarrier
*pBufferMemoryBarriers
,
5561 uint32_t imageMemoryBarrierCount
,
5562 const VkImageMemoryBarrier
*pImageMemoryBarriers
,
5563 const struct radv_barrier_info
*info
)
5565 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
5566 enum radv_cmd_flush_bits src_flush_bits
= 0;
5567 enum radv_cmd_flush_bits dst_flush_bits
= 0;
5569 for (unsigned i
= 0; i
< info
->eventCount
; ++i
) {
5570 RADV_FROM_HANDLE(radv_event
, event
, info
->pEvents
[i
]);
5571 uint64_t va
= radv_buffer_get_va(event
->bo
);
5573 radv_cs_add_buffer(cmd_buffer
->device
->ws
, cs
, event
->bo
);
5575 ASSERTED
unsigned cdw_max
= radeon_check_space(cmd_buffer
->device
->ws
, cs
, 7);
5577 radv_cp_wait_mem(cs
, WAIT_REG_MEM_EQUAL
, va
, 1, 0xffffffff);
5578 assert(cmd_buffer
->cs
->cdw
<= cdw_max
);
5581 for (uint32_t i
= 0; i
< memoryBarrierCount
; i
++) {
5582 src_flush_bits
|= radv_src_access_flush(cmd_buffer
, pMemoryBarriers
[i
].srcAccessMask
,
5584 dst_flush_bits
|= radv_dst_access_flush(cmd_buffer
, pMemoryBarriers
[i
].dstAccessMask
,
5588 for (uint32_t i
= 0; i
< bufferMemoryBarrierCount
; i
++) {
5589 src_flush_bits
|= radv_src_access_flush(cmd_buffer
, pBufferMemoryBarriers
[i
].srcAccessMask
,
5591 dst_flush_bits
|= radv_dst_access_flush(cmd_buffer
, pBufferMemoryBarriers
[i
].dstAccessMask
,
5595 for (uint32_t i
= 0; i
< imageMemoryBarrierCount
; i
++) {
5596 RADV_FROM_HANDLE(radv_image
, image
, pImageMemoryBarriers
[i
].image
);
5598 src_flush_bits
|= radv_src_access_flush(cmd_buffer
, pImageMemoryBarriers
[i
].srcAccessMask
,
5600 dst_flush_bits
|= radv_dst_access_flush(cmd_buffer
, pImageMemoryBarriers
[i
].dstAccessMask
,
5604 /* The Vulkan spec 1.1.98 says:
5606 * "An execution dependency with only
5607 * VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT in the destination stage mask
5608 * will only prevent that stage from executing in subsequently
5609 * submitted commands. As this stage does not perform any actual
5610 * execution, this is not observable - in effect, it does not delay
5611 * processing of subsequent commands. Similarly an execution dependency
5612 * with only VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT in the source stage mask
5613 * will effectively not wait for any prior commands to complete."
5615 if (info
->dstStageMask
!= VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT
)
5616 radv_stage_flush(cmd_buffer
, info
->srcStageMask
);
5617 cmd_buffer
->state
.flush_bits
|= src_flush_bits
;
5619 for (uint32_t i
= 0; i
< imageMemoryBarrierCount
; i
++) {
5620 RADV_FROM_HANDLE(radv_image
, image
, pImageMemoryBarriers
[i
].image
);
5622 const struct VkSampleLocationsInfoEXT
*sample_locs_info
=
5623 vk_find_struct_const(pImageMemoryBarriers
[i
].pNext
,
5624 SAMPLE_LOCATIONS_INFO_EXT
);
5625 struct radv_sample_locations_state sample_locations
= {};
5627 if (sample_locs_info
) {
5628 assert(image
->flags
& VK_IMAGE_CREATE_SAMPLE_LOCATIONS_COMPATIBLE_DEPTH_BIT_EXT
);
5629 sample_locations
.per_pixel
= sample_locs_info
->sampleLocationsPerPixel
;
5630 sample_locations
.grid_size
= sample_locs_info
->sampleLocationGridSize
;
5631 sample_locations
.count
= sample_locs_info
->sampleLocationsCount
;
5632 typed_memcpy(&sample_locations
.locations
[0],
5633 sample_locs_info
->pSampleLocations
,
5634 sample_locs_info
->sampleLocationsCount
);
5637 radv_handle_image_transition(cmd_buffer
, image
,
5638 pImageMemoryBarriers
[i
].oldLayout
,
5639 false, /* Outside of a renderpass we are never in a renderloop */
5640 pImageMemoryBarriers
[i
].newLayout
,
5641 false, /* Outside of a renderpass we are never in a renderloop */
5642 pImageMemoryBarriers
[i
].srcQueueFamilyIndex
,
5643 pImageMemoryBarriers
[i
].dstQueueFamilyIndex
,
5644 &pImageMemoryBarriers
[i
].subresourceRange
,
5645 sample_locs_info
? &sample_locations
: NULL
);
5648 /* Make sure CP DMA is idle because the driver might have performed a
5649 * DMA operation for copying or filling buffers/images.
5651 if (info
->srcStageMask
& (VK_PIPELINE_STAGE_TRANSFER_BIT
|
5652 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT
))
5653 si_cp_dma_wait_for_idle(cmd_buffer
);
5655 cmd_buffer
->state
.flush_bits
|= dst_flush_bits
;
5658 void radv_CmdPipelineBarrier(
5659 VkCommandBuffer commandBuffer
,
5660 VkPipelineStageFlags srcStageMask
,
5661 VkPipelineStageFlags destStageMask
,
5663 uint32_t memoryBarrierCount
,
5664 const VkMemoryBarrier
* pMemoryBarriers
,
5665 uint32_t bufferMemoryBarrierCount
,
5666 const VkBufferMemoryBarrier
* pBufferMemoryBarriers
,
5667 uint32_t imageMemoryBarrierCount
,
5668 const VkImageMemoryBarrier
* pImageMemoryBarriers
)
5670 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5671 struct radv_barrier_info info
;
5673 info
.eventCount
= 0;
5674 info
.pEvents
= NULL
;
5675 info
.srcStageMask
= srcStageMask
;
5676 info
.dstStageMask
= destStageMask
;
5678 radv_barrier(cmd_buffer
, memoryBarrierCount
, pMemoryBarriers
,
5679 bufferMemoryBarrierCount
, pBufferMemoryBarriers
,
5680 imageMemoryBarrierCount
, pImageMemoryBarriers
, &info
);
5684 static void write_event(struct radv_cmd_buffer
*cmd_buffer
,
5685 struct radv_event
*event
,
5686 VkPipelineStageFlags stageMask
,
5689 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
5690 uint64_t va
= radv_buffer_get_va(event
->bo
);
5692 si_emit_cache_flush(cmd_buffer
);
5694 radv_cs_add_buffer(cmd_buffer
->device
->ws
, cs
, event
->bo
);
5696 ASSERTED
unsigned cdw_max
= radeon_check_space(cmd_buffer
->device
->ws
, cs
, 21);
5698 /* Flags that only require a top-of-pipe event. */
5699 VkPipelineStageFlags top_of_pipe_flags
=
5700 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT
;
5702 /* Flags that only require a post-index-fetch event. */
5703 VkPipelineStageFlags post_index_fetch_flags
=
5705 VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT
|
5706 VK_PIPELINE_STAGE_VERTEX_INPUT_BIT
;
5708 /* Make sure CP DMA is idle because the driver might have performed a
5709 * DMA operation for copying or filling buffers/images.
5711 if (stageMask
& (VK_PIPELINE_STAGE_TRANSFER_BIT
|
5712 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT
))
5713 si_cp_dma_wait_for_idle(cmd_buffer
);
5715 /* TODO: Emit EOS events for syncing PS/CS stages. */
5717 if (!(stageMask
& ~top_of_pipe_flags
)) {
5718 /* Just need to sync the PFP engine. */
5719 radeon_emit(cs
, PKT3(PKT3_WRITE_DATA
, 3, 0));
5720 radeon_emit(cs
, S_370_DST_SEL(V_370_MEM
) |
5721 S_370_WR_CONFIRM(1) |
5722 S_370_ENGINE_SEL(V_370_PFP
));
5723 radeon_emit(cs
, va
);
5724 radeon_emit(cs
, va
>> 32);
5725 radeon_emit(cs
, value
);
5726 } else if (!(stageMask
& ~post_index_fetch_flags
)) {
5727 /* Sync ME because PFP reads index and indirect buffers. */
5728 radeon_emit(cs
, PKT3(PKT3_WRITE_DATA
, 3, 0));
5729 radeon_emit(cs
, S_370_DST_SEL(V_370_MEM
) |
5730 S_370_WR_CONFIRM(1) |
5731 S_370_ENGINE_SEL(V_370_ME
));
5732 radeon_emit(cs
, va
);
5733 radeon_emit(cs
, va
>> 32);
5734 radeon_emit(cs
, value
);
5736 /* Otherwise, sync all prior GPU work using an EOP event. */
5737 si_cs_emit_write_event_eop(cs
,
5738 cmd_buffer
->device
->physical_device
->rad_info
.chip_class
,
5739 radv_cmd_buffer_uses_mec(cmd_buffer
),
5740 V_028A90_BOTTOM_OF_PIPE_TS
, 0,
5742 EOP_DATA_SEL_VALUE_32BIT
, va
, value
,
5743 cmd_buffer
->gfx9_eop_bug_va
);
5746 assert(cmd_buffer
->cs
->cdw
<= cdw_max
);
5749 void radv_CmdSetEvent(VkCommandBuffer commandBuffer
,
5751 VkPipelineStageFlags stageMask
)
5753 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5754 RADV_FROM_HANDLE(radv_event
, event
, _event
);
5756 write_event(cmd_buffer
, event
, stageMask
, 1);
5759 void radv_CmdResetEvent(VkCommandBuffer commandBuffer
,
5761 VkPipelineStageFlags stageMask
)
5763 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5764 RADV_FROM_HANDLE(radv_event
, event
, _event
);
5766 write_event(cmd_buffer
, event
, stageMask
, 0);
5769 void radv_CmdWaitEvents(VkCommandBuffer commandBuffer
,
5770 uint32_t eventCount
,
5771 const VkEvent
* pEvents
,
5772 VkPipelineStageFlags srcStageMask
,
5773 VkPipelineStageFlags dstStageMask
,
5774 uint32_t memoryBarrierCount
,
5775 const VkMemoryBarrier
* pMemoryBarriers
,
5776 uint32_t bufferMemoryBarrierCount
,
5777 const VkBufferMemoryBarrier
* pBufferMemoryBarriers
,
5778 uint32_t imageMemoryBarrierCount
,
5779 const VkImageMemoryBarrier
* pImageMemoryBarriers
)
5781 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5782 struct radv_barrier_info info
;
5784 info
.eventCount
= eventCount
;
5785 info
.pEvents
= pEvents
;
5786 info
.srcStageMask
= 0;
5788 radv_barrier(cmd_buffer
, memoryBarrierCount
, pMemoryBarriers
,
5789 bufferMemoryBarrierCount
, pBufferMemoryBarriers
,
5790 imageMemoryBarrierCount
, pImageMemoryBarriers
, &info
);
5794 void radv_CmdSetDeviceMask(VkCommandBuffer commandBuffer
,
5795 uint32_t deviceMask
)
5800 /* VK_EXT_conditional_rendering */
5801 void radv_CmdBeginConditionalRenderingEXT(
5802 VkCommandBuffer commandBuffer
,
5803 const VkConditionalRenderingBeginInfoEXT
* pConditionalRenderingBegin
)
5805 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5806 RADV_FROM_HANDLE(radv_buffer
, buffer
, pConditionalRenderingBegin
->buffer
);
5807 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
5808 bool draw_visible
= true;
5809 uint64_t pred_value
= 0;
5810 uint64_t va
, new_va
;
5811 unsigned pred_offset
;
5813 va
= radv_buffer_get_va(buffer
->bo
) + pConditionalRenderingBegin
->offset
;
5815 /* By default, if the 32-bit value at offset in buffer memory is zero,
5816 * then the rendering commands are discarded, otherwise they are
5817 * executed as normal. If the inverted flag is set, all commands are
5818 * discarded if the value is non zero.
5820 if (pConditionalRenderingBegin
->flags
&
5821 VK_CONDITIONAL_RENDERING_INVERTED_BIT_EXT
) {
5822 draw_visible
= false;
5825 si_emit_cache_flush(cmd_buffer
);
5827 /* From the Vulkan spec 1.1.107:
5829 * "If the 32-bit value at offset in buffer memory is zero, then the
5830 * rendering commands are discarded, otherwise they are executed as
5831 * normal. If the value of the predicate in buffer memory changes while
5832 * conditional rendering is active, the rendering commands may be
5833 * discarded in an implementation-dependent way. Some implementations
5834 * may latch the value of the predicate upon beginning conditional
5835 * rendering while others may read it before every rendering command."
5837 * But, the AMD hardware treats the predicate as a 64-bit value which
5838 * means we need a workaround in the driver. Luckily, it's not required
5839 * to support if the value changes when predication is active.
5841 * The workaround is as follows:
5842 * 1) allocate a 64-value in the upload BO and initialize it to 0
5843 * 2) copy the 32-bit predicate value to the upload BO
5844 * 3) use the new allocated VA address for predication
5846 * Based on the conditionalrender demo, it's faster to do the COPY_DATA
5847 * in ME (+ sync PFP) instead of PFP.
5849 radv_cmd_buffer_upload_data(cmd_buffer
, 8, 16, &pred_value
, &pred_offset
);
5851 new_va
= radv_buffer_get_va(cmd_buffer
->upload
.upload_bo
) + pred_offset
;
5853 radeon_emit(cs
, PKT3(PKT3_COPY_DATA
, 4, 0));
5854 radeon_emit(cs
, COPY_DATA_SRC_SEL(COPY_DATA_SRC_MEM
) |
5855 COPY_DATA_DST_SEL(COPY_DATA_DST_MEM
) |
5856 COPY_DATA_WR_CONFIRM
);
5857 radeon_emit(cs
, va
);
5858 radeon_emit(cs
, va
>> 32);
5859 radeon_emit(cs
, new_va
);
5860 radeon_emit(cs
, new_va
>> 32);
5862 radeon_emit(cs
, PKT3(PKT3_PFP_SYNC_ME
, 0, 0));
5865 /* Enable predication for this command buffer. */
5866 si_emit_set_predication_state(cmd_buffer
, draw_visible
, new_va
);
5867 cmd_buffer
->state
.predicating
= true;
5869 /* Store conditional rendering user info. */
5870 cmd_buffer
->state
.predication_type
= draw_visible
;
5871 cmd_buffer
->state
.predication_va
= new_va
;
5874 void radv_CmdEndConditionalRenderingEXT(
5875 VkCommandBuffer commandBuffer
)
5877 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5879 /* Disable predication for this command buffer. */
5880 si_emit_set_predication_state(cmd_buffer
, false, 0);
5881 cmd_buffer
->state
.predicating
= false;
5883 /* Reset conditional rendering user info. */
5884 cmd_buffer
->state
.predication_type
= -1;
5885 cmd_buffer
->state
.predication_va
= 0;
5888 /* VK_EXT_transform_feedback */
5889 void radv_CmdBindTransformFeedbackBuffersEXT(
5890 VkCommandBuffer commandBuffer
,
5891 uint32_t firstBinding
,
5892 uint32_t bindingCount
,
5893 const VkBuffer
* pBuffers
,
5894 const VkDeviceSize
* pOffsets
,
5895 const VkDeviceSize
* pSizes
)
5897 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
5898 struct radv_streamout_binding
*sb
= cmd_buffer
->streamout_bindings
;
5899 uint8_t enabled_mask
= 0;
5901 assert(firstBinding
+ bindingCount
<= MAX_SO_BUFFERS
);
5902 for (uint32_t i
= 0; i
< bindingCount
; i
++) {
5903 uint32_t idx
= firstBinding
+ i
;
5905 sb
[idx
].buffer
= radv_buffer_from_handle(pBuffers
[i
]);
5906 sb
[idx
].offset
= pOffsets
[i
];
5907 sb
[idx
].size
= pSizes
[i
];
5909 radv_cs_add_buffer(cmd_buffer
->device
->ws
, cmd_buffer
->cs
,
5910 sb
[idx
].buffer
->bo
);
5912 enabled_mask
|= 1 << idx
;
5915 cmd_buffer
->state
.streamout
.enabled_mask
|= enabled_mask
;
5917 cmd_buffer
->state
.dirty
|= RADV_CMD_DIRTY_STREAMOUT_BUFFER
;
5921 radv_emit_streamout_enable(struct radv_cmd_buffer
*cmd_buffer
)
5923 struct radv_streamout_state
*so
= &cmd_buffer
->state
.streamout
;
5924 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
5926 radeon_set_context_reg_seq(cs
, R_028B94_VGT_STRMOUT_CONFIG
, 2);
5928 S_028B94_STREAMOUT_0_EN(so
->streamout_enabled
) |
5929 S_028B94_RAST_STREAM(0) |
5930 S_028B94_STREAMOUT_1_EN(so
->streamout_enabled
) |
5931 S_028B94_STREAMOUT_2_EN(so
->streamout_enabled
) |
5932 S_028B94_STREAMOUT_3_EN(so
->streamout_enabled
));
5933 radeon_emit(cs
, so
->hw_enabled_mask
&
5934 so
->enabled_stream_buffers_mask
);
5936 cmd_buffer
->state
.context_roll_without_scissor_emitted
= true;
5940 radv_set_streamout_enable(struct radv_cmd_buffer
*cmd_buffer
, bool enable
)
5942 struct radv_streamout_state
*so
= &cmd_buffer
->state
.streamout
;
5943 bool old_streamout_enabled
= so
->streamout_enabled
;
5944 uint32_t old_hw_enabled_mask
= so
->hw_enabled_mask
;
5946 so
->streamout_enabled
= enable
;
5948 so
->hw_enabled_mask
= so
->enabled_mask
|
5949 (so
->enabled_mask
<< 4) |
5950 (so
->enabled_mask
<< 8) |
5951 (so
->enabled_mask
<< 12);
5953 if (!cmd_buffer
->device
->physical_device
->use_ngg_streamout
&&
5954 ((old_streamout_enabled
!= so
->streamout_enabled
) ||
5955 (old_hw_enabled_mask
!= so
->hw_enabled_mask
)))
5956 radv_emit_streamout_enable(cmd_buffer
);
5958 if (cmd_buffer
->device
->physical_device
->use_ngg_streamout
) {
5959 cmd_buffer
->gds_needed
= true;
5960 cmd_buffer
->gds_oa_needed
= true;
5964 static void radv_flush_vgt_streamout(struct radv_cmd_buffer
*cmd_buffer
)
5966 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
5967 unsigned reg_strmout_cntl
;
5969 /* The register is at different places on different ASICs. */
5970 if (cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX7
) {
5971 reg_strmout_cntl
= R_0300FC_CP_STRMOUT_CNTL
;
5972 radeon_set_uconfig_reg(cs
, reg_strmout_cntl
, 0);
5974 reg_strmout_cntl
= R_0084FC_CP_STRMOUT_CNTL
;
5975 radeon_set_config_reg(cs
, reg_strmout_cntl
, 0);
5978 radeon_emit(cs
, PKT3(PKT3_EVENT_WRITE
, 0, 0));
5979 radeon_emit(cs
, EVENT_TYPE(EVENT_TYPE_SO_VGTSTREAMOUT_FLUSH
) | EVENT_INDEX(0));
5981 radeon_emit(cs
, PKT3(PKT3_WAIT_REG_MEM
, 5, 0));
5982 radeon_emit(cs
, WAIT_REG_MEM_EQUAL
); /* wait until the register is equal to the reference value */
5983 radeon_emit(cs
, reg_strmout_cntl
>> 2); /* register */
5985 radeon_emit(cs
, S_0084FC_OFFSET_UPDATE_DONE(1)); /* reference value */
5986 radeon_emit(cs
, S_0084FC_OFFSET_UPDATE_DONE(1)); /* mask */
5987 radeon_emit(cs
, 4); /* poll interval */
5991 radv_emit_streamout_begin(struct radv_cmd_buffer
*cmd_buffer
,
5992 uint32_t firstCounterBuffer
,
5993 uint32_t counterBufferCount
,
5994 const VkBuffer
*pCounterBuffers
,
5995 const VkDeviceSize
*pCounterBufferOffsets
)
5998 struct radv_streamout_binding
*sb
= cmd_buffer
->streamout_bindings
;
5999 struct radv_streamout_state
*so
= &cmd_buffer
->state
.streamout
;
6000 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
6003 radv_flush_vgt_streamout(cmd_buffer
);
6005 assert(firstCounterBuffer
+ counterBufferCount
<= MAX_SO_BUFFERS
);
6006 for_each_bit(i
, so
->enabled_mask
) {
6007 int32_t counter_buffer_idx
= i
- firstCounterBuffer
;
6008 if (counter_buffer_idx
>= 0 && counter_buffer_idx
>= counterBufferCount
)
6009 counter_buffer_idx
= -1;
6011 /* AMD GCN binds streamout buffers as shader resources.
6012 * VGT only counts primitives and tells the shader through
6015 radeon_set_context_reg_seq(cs
, R_028AD0_VGT_STRMOUT_BUFFER_SIZE_0
+ 16*i
, 2);
6016 radeon_emit(cs
, sb
[i
].size
>> 2); /* BUFFER_SIZE (in DW) */
6017 radeon_emit(cs
, so
->stride_in_dw
[i
]); /* VTX_STRIDE (in DW) */
6019 cmd_buffer
->state
.context_roll_without_scissor_emitted
= true;
6021 if (counter_buffer_idx
>= 0 && pCounterBuffers
&& pCounterBuffers
[counter_buffer_idx
]) {
6022 /* The array of counter buffers is optional. */
6023 RADV_FROM_HANDLE(radv_buffer
, buffer
, pCounterBuffers
[counter_buffer_idx
]);
6024 uint64_t va
= radv_buffer_get_va(buffer
->bo
);
6026 va
+= buffer
->offset
+ pCounterBufferOffsets
[counter_buffer_idx
];
6029 radeon_emit(cs
, PKT3(PKT3_STRMOUT_BUFFER_UPDATE
, 4, 0));
6030 radeon_emit(cs
, STRMOUT_SELECT_BUFFER(i
) |
6031 STRMOUT_DATA_TYPE(1) | /* offset in bytes */
6032 STRMOUT_OFFSET_SOURCE(STRMOUT_OFFSET_FROM_MEM
)); /* control */
6033 radeon_emit(cs
, 0); /* unused */
6034 radeon_emit(cs
, 0); /* unused */
6035 radeon_emit(cs
, va
); /* src address lo */
6036 radeon_emit(cs
, va
>> 32); /* src address hi */
6038 radv_cs_add_buffer(cmd_buffer
->device
->ws
, cs
, buffer
->bo
);
6040 /* Start from the beginning. */
6041 radeon_emit(cs
, PKT3(PKT3_STRMOUT_BUFFER_UPDATE
, 4, 0));
6042 radeon_emit(cs
, STRMOUT_SELECT_BUFFER(i
) |
6043 STRMOUT_DATA_TYPE(1) | /* offset in bytes */
6044 STRMOUT_OFFSET_SOURCE(STRMOUT_OFFSET_FROM_PACKET
)); /* control */
6045 radeon_emit(cs
, 0); /* unused */
6046 radeon_emit(cs
, 0); /* unused */
6047 radeon_emit(cs
, 0); /* unused */
6048 radeon_emit(cs
, 0); /* unused */
6052 radv_set_streamout_enable(cmd_buffer
, true);
6056 gfx10_emit_streamout_begin(struct radv_cmd_buffer
*cmd_buffer
,
6057 uint32_t firstCounterBuffer
,
6058 uint32_t counterBufferCount
,
6059 const VkBuffer
*pCounterBuffers
,
6060 const VkDeviceSize
*pCounterBufferOffsets
)
6062 struct radv_streamout_state
*so
= &cmd_buffer
->state
.streamout
;
6063 unsigned last_target
= util_last_bit(so
->enabled_mask
) - 1;
6064 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
6067 assert(cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX10
);
6068 assert(firstCounterBuffer
+ counterBufferCount
<= MAX_SO_BUFFERS
);
6070 /* Sync because the next streamout operation will overwrite GDS and we
6071 * have to make sure it's idle.
6072 * TODO: Improve by tracking if there is a streamout operation in
6075 cmd_buffer
->state
.flush_bits
|= RADV_CMD_FLAG_VS_PARTIAL_FLUSH
;
6076 si_emit_cache_flush(cmd_buffer
);
6078 for_each_bit(i
, so
->enabled_mask
) {
6079 int32_t counter_buffer_idx
= i
- firstCounterBuffer
;
6080 if (counter_buffer_idx
>= 0 && counter_buffer_idx
>= counterBufferCount
)
6081 counter_buffer_idx
= -1;
6083 bool append
= counter_buffer_idx
>= 0 &&
6084 pCounterBuffers
&& pCounterBuffers
[counter_buffer_idx
];
6088 RADV_FROM_HANDLE(radv_buffer
, buffer
, pCounterBuffers
[counter_buffer_idx
]);
6090 va
+= radv_buffer_get_va(buffer
->bo
);
6091 va
+= buffer
->offset
+ pCounterBufferOffsets
[counter_buffer_idx
];
6093 radv_cs_add_buffer(cmd_buffer
->device
->ws
, cs
, buffer
->bo
);
6096 radeon_emit(cs
, PKT3(PKT3_DMA_DATA
, 5, 0));
6097 radeon_emit(cs
, S_411_SRC_SEL(append
? V_411_SRC_ADDR_TC_L2
: V_411_DATA
) |
6098 S_411_DST_SEL(V_411_GDS
) |
6099 S_411_CP_SYNC(i
== last_target
));
6100 radeon_emit(cs
, va
);
6101 radeon_emit(cs
, va
>> 32);
6102 radeon_emit(cs
, 4 * i
); /* destination in GDS */
6104 radeon_emit(cs
, S_414_BYTE_COUNT_GFX9(4) |
6105 S_414_DISABLE_WR_CONFIRM_GFX9(i
!= last_target
));
6108 radv_set_streamout_enable(cmd_buffer
, true);
6111 void radv_CmdBeginTransformFeedbackEXT(
6112 VkCommandBuffer commandBuffer
,
6113 uint32_t firstCounterBuffer
,
6114 uint32_t counterBufferCount
,
6115 const VkBuffer
* pCounterBuffers
,
6116 const VkDeviceSize
* pCounterBufferOffsets
)
6118 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
6120 if (cmd_buffer
->device
->physical_device
->use_ngg_streamout
) {
6121 gfx10_emit_streamout_begin(cmd_buffer
,
6122 firstCounterBuffer
, counterBufferCount
,
6123 pCounterBuffers
, pCounterBufferOffsets
);
6125 radv_emit_streamout_begin(cmd_buffer
,
6126 firstCounterBuffer
, counterBufferCount
,
6127 pCounterBuffers
, pCounterBufferOffsets
);
6132 radv_emit_streamout_end(struct radv_cmd_buffer
*cmd_buffer
,
6133 uint32_t firstCounterBuffer
,
6134 uint32_t counterBufferCount
,
6135 const VkBuffer
*pCounterBuffers
,
6136 const VkDeviceSize
*pCounterBufferOffsets
)
6138 struct radv_streamout_state
*so
= &cmd_buffer
->state
.streamout
;
6139 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
6142 radv_flush_vgt_streamout(cmd_buffer
);
6144 assert(firstCounterBuffer
+ counterBufferCount
<= MAX_SO_BUFFERS
);
6145 for_each_bit(i
, so
->enabled_mask
) {
6146 int32_t counter_buffer_idx
= i
- firstCounterBuffer
;
6147 if (counter_buffer_idx
>= 0 && counter_buffer_idx
>= counterBufferCount
)
6148 counter_buffer_idx
= -1;
6150 if (counter_buffer_idx
>= 0 && pCounterBuffers
&& pCounterBuffers
[counter_buffer_idx
]) {
6151 /* The array of counters buffer is optional. */
6152 RADV_FROM_HANDLE(radv_buffer
, buffer
, pCounterBuffers
[counter_buffer_idx
]);
6153 uint64_t va
= radv_buffer_get_va(buffer
->bo
);
6155 va
+= buffer
->offset
+ pCounterBufferOffsets
[counter_buffer_idx
];
6157 radeon_emit(cs
, PKT3(PKT3_STRMOUT_BUFFER_UPDATE
, 4, 0));
6158 radeon_emit(cs
, STRMOUT_SELECT_BUFFER(i
) |
6159 STRMOUT_DATA_TYPE(1) | /* offset in bytes */
6160 STRMOUT_OFFSET_SOURCE(STRMOUT_OFFSET_NONE
) |
6161 STRMOUT_STORE_BUFFER_FILLED_SIZE
); /* control */
6162 radeon_emit(cs
, va
); /* dst address lo */
6163 radeon_emit(cs
, va
>> 32); /* dst address hi */
6164 radeon_emit(cs
, 0); /* unused */
6165 radeon_emit(cs
, 0); /* unused */
6167 radv_cs_add_buffer(cmd_buffer
->device
->ws
, cs
, buffer
->bo
);
6170 /* Deactivate transform feedback by zeroing the buffer size.
6171 * The counters (primitives generated, primitives emitted) may
6172 * be enabled even if there is not buffer bound. This ensures
6173 * that the primitives-emitted query won't increment.
6175 radeon_set_context_reg(cs
, R_028AD0_VGT_STRMOUT_BUFFER_SIZE_0
+ 16*i
, 0);
6177 cmd_buffer
->state
.context_roll_without_scissor_emitted
= true;
6180 radv_set_streamout_enable(cmd_buffer
, false);
6184 gfx10_emit_streamout_end(struct radv_cmd_buffer
*cmd_buffer
,
6185 uint32_t firstCounterBuffer
,
6186 uint32_t counterBufferCount
,
6187 const VkBuffer
*pCounterBuffers
,
6188 const VkDeviceSize
*pCounterBufferOffsets
)
6190 struct radv_streamout_state
*so
= &cmd_buffer
->state
.streamout
;
6191 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
6194 assert(cmd_buffer
->device
->physical_device
->rad_info
.chip_class
>= GFX10
);
6195 assert(firstCounterBuffer
+ counterBufferCount
<= MAX_SO_BUFFERS
);
6197 for_each_bit(i
, so
->enabled_mask
) {
6198 int32_t counter_buffer_idx
= i
- firstCounterBuffer
;
6199 if (counter_buffer_idx
>= 0 && counter_buffer_idx
>= counterBufferCount
)
6200 counter_buffer_idx
= -1;
6202 if (counter_buffer_idx
>= 0 && pCounterBuffers
&& pCounterBuffers
[counter_buffer_idx
]) {
6203 /* The array of counters buffer is optional. */
6204 RADV_FROM_HANDLE(radv_buffer
, buffer
, pCounterBuffers
[counter_buffer_idx
]);
6205 uint64_t va
= radv_buffer_get_va(buffer
->bo
);
6207 va
+= buffer
->offset
+ pCounterBufferOffsets
[counter_buffer_idx
];
6209 si_cs_emit_write_event_eop(cs
,
6210 cmd_buffer
->device
->physical_device
->rad_info
.chip_class
,
6211 radv_cmd_buffer_uses_mec(cmd_buffer
),
6212 V_028A90_PS_DONE
, 0,
6215 va
, EOP_DATA_GDS(i
, 1), 0);
6217 radv_cs_add_buffer(cmd_buffer
->device
->ws
, cs
, buffer
->bo
);
6221 radv_set_streamout_enable(cmd_buffer
, false);
6224 void radv_CmdEndTransformFeedbackEXT(
6225 VkCommandBuffer commandBuffer
,
6226 uint32_t firstCounterBuffer
,
6227 uint32_t counterBufferCount
,
6228 const VkBuffer
* pCounterBuffers
,
6229 const VkDeviceSize
* pCounterBufferOffsets
)
6231 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
6233 if (cmd_buffer
->device
->physical_device
->use_ngg_streamout
) {
6234 gfx10_emit_streamout_end(cmd_buffer
,
6235 firstCounterBuffer
, counterBufferCount
,
6236 pCounterBuffers
, pCounterBufferOffsets
);
6238 radv_emit_streamout_end(cmd_buffer
,
6239 firstCounterBuffer
, counterBufferCount
,
6240 pCounterBuffers
, pCounterBufferOffsets
);
6244 void radv_CmdDrawIndirectByteCountEXT(
6245 VkCommandBuffer commandBuffer
,
6246 uint32_t instanceCount
,
6247 uint32_t firstInstance
,
6248 VkBuffer _counterBuffer
,
6249 VkDeviceSize counterBufferOffset
,
6250 uint32_t counterOffset
,
6251 uint32_t vertexStride
)
6253 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
6254 RADV_FROM_HANDLE(radv_buffer
, counterBuffer
, _counterBuffer
);
6255 struct radv_draw_info info
= {};
6257 info
.instance_count
= instanceCount
;
6258 info
.first_instance
= firstInstance
;
6259 info
.strmout_buffer
= counterBuffer
;
6260 info
.strmout_buffer_offset
= counterBufferOffset
;
6261 info
.stride
= vertexStride
;
6263 radv_draw(cmd_buffer
, &info
);
6266 /* VK_AMD_buffer_marker */
6267 void radv_CmdWriteBufferMarkerAMD(
6268 VkCommandBuffer commandBuffer
,
6269 VkPipelineStageFlagBits pipelineStage
,
6271 VkDeviceSize dstOffset
,
6274 RADV_FROM_HANDLE(radv_cmd_buffer
, cmd_buffer
, commandBuffer
);
6275 RADV_FROM_HANDLE(radv_buffer
, buffer
, dstBuffer
);
6276 struct radeon_cmdbuf
*cs
= cmd_buffer
->cs
;
6277 uint64_t va
= radv_buffer_get_va(buffer
->bo
) + dstOffset
;
6279 si_emit_cache_flush(cmd_buffer
);
6281 ASSERTED
unsigned cdw_max
= radeon_check_space(cmd_buffer
->device
->ws
, cmd_buffer
->cs
, 12);
6283 if (!(pipelineStage
& ~VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT
)) {
6284 radeon_emit(cs
, PKT3(PKT3_COPY_DATA
, 4, 0));
6285 radeon_emit(cs
, COPY_DATA_SRC_SEL(COPY_DATA_IMM
) |
6286 COPY_DATA_DST_SEL(COPY_DATA_DST_MEM
) |
6287 COPY_DATA_WR_CONFIRM
);
6288 radeon_emit(cs
, marker
);
6290 radeon_emit(cs
, va
);
6291 radeon_emit(cs
, va
>> 32);
6293 si_cs_emit_write_event_eop(cs
,
6294 cmd_buffer
->device
->physical_device
->rad_info
.chip_class
,
6295 radv_cmd_buffer_uses_mec(cmd_buffer
),
6296 V_028A90_BOTTOM_OF_PIPE_TS
, 0,
6298 EOP_DATA_SEL_VALUE_32BIT
,
6300 cmd_buffer
->gfx9_eop_bug_va
);
6303 assert(cmd_buffer
->cs
->cdw
<= cdw_max
);