2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 * Copyright 2018 Advanced Micro Devices, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * on the rights to use, copy, modify, merge, publish, distribute, sub
10 * license, and/or sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23 * USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include "si_shader.h"
31 #include "util/u_dynarray.h"
32 #include "util/u_idalloc.h"
33 #include "util/u_threaded_context.h"
35 #ifdef PIPE_ARCH_BIG_ENDIAN
36 #define SI_BIG_ENDIAN 1
38 #define SI_BIG_ENDIAN 0
41 #define ATI_VENDOR_ID 0x1002
42 #define SI_PRIM_DISCARD_DEBUG 0
43 #define SI_NOT_QUERY 0xffffffff
45 /* The base vertex and primitive restart can be any number, but we must pick
46 * one which will mean "unknown" for the purpose of state tracking and
47 * the number shouldn't be a commonly-used one. */
48 #define SI_BASE_VERTEX_UNKNOWN INT_MIN
49 #define SI_RESTART_INDEX_UNKNOWN INT_MIN
50 #define SI_INSTANCE_COUNT_UNKNOWN INT_MIN
51 #define SI_NUM_SMOOTH_AA_SAMPLES 8
52 #define SI_MAX_POINT_SIZE 2048
53 #define SI_GS_PER_ES 128
54 /* Alignment for optimal CP DMA performance. */
55 #define SI_CPDMA_ALIGNMENT 32
57 /* Tunables for compute-based clear_buffer and copy_buffer: */
58 #define SI_COMPUTE_CLEAR_DW_PER_THREAD 4
59 #define SI_COMPUTE_COPY_DW_PER_THREAD 4
60 #define SI_COMPUTE_DST_CACHE_POLICY L2_STREAM
62 /* Pipeline & streamout query controls. */
63 #define SI_CONTEXT_START_PIPELINE_STATS (1 << 0)
64 #define SI_CONTEXT_STOP_PIPELINE_STATS (1 << 1)
65 #define SI_CONTEXT_FLUSH_FOR_RENDER_COND (1 << 2)
66 /* Instruction cache. */
67 #define SI_CONTEXT_INV_ICACHE (1 << 3)
68 /* Scalar L1 cache. */
69 #define SI_CONTEXT_INV_SCACHE (1 << 4)
70 /* Vector L1 cache. */
71 #define SI_CONTEXT_INV_VCACHE (1 << 5)
72 /* L2 cache + L2 metadata cache writeback & invalidate.
73 * GFX6-8: Used by shaders only. GFX9-10: Used by everything. */
74 #define SI_CONTEXT_INV_L2 (1 << 6)
75 /* L2 writeback (write dirty L2 lines to memory for non-L2 clients).
76 * Only used for coherency with non-L2 clients like CB, DB, CP on GFX6-8.
77 * GFX6-7 will do complete invalidation, because the writeback is unsupported. */
78 #define SI_CONTEXT_WB_L2 (1 << 7)
79 /* Writeback & invalidate the L2 metadata cache only. It can only be coupled with
80 * a CB or DB flush. */
81 #define SI_CONTEXT_INV_L2_METADATA (1 << 8)
82 /* Framebuffer caches. */
83 #define SI_CONTEXT_FLUSH_AND_INV_DB (1 << 9)
84 #define SI_CONTEXT_FLUSH_AND_INV_DB_META (1 << 10)
85 #define SI_CONTEXT_FLUSH_AND_INV_CB (1 << 11)
86 /* Engine synchronization. */
87 #define SI_CONTEXT_VS_PARTIAL_FLUSH (1 << 12)
88 #define SI_CONTEXT_PS_PARTIAL_FLUSH (1 << 13)
89 #define SI_CONTEXT_CS_PARTIAL_FLUSH (1 << 14)
90 #define SI_CONTEXT_VGT_FLUSH (1 << 15)
91 #define SI_CONTEXT_VGT_STREAMOUT_SYNC (1 << 16)
93 #define SI_PREFETCH_VBO_DESCRIPTORS (1 << 0)
94 #define SI_PREFETCH_LS (1 << 1)
95 #define SI_PREFETCH_HS (1 << 2)
96 #define SI_PREFETCH_ES (1 << 3)
97 #define SI_PREFETCH_GS (1 << 4)
98 #define SI_PREFETCH_VS (1 << 5)
99 #define SI_PREFETCH_PS (1 << 6)
101 #define SI_MAX_BORDER_COLORS 4096
102 #define SI_MAX_VIEWPORTS 16
103 #define SIX_BITS 0x3F
104 #define SI_MAP_BUFFER_ALIGNMENT 64
105 #define SI_MAX_VARIABLE_THREADS_PER_BLOCK 1024
107 #define SI_RESOURCE_FLAG_TRANSFER (PIPE_RESOURCE_FLAG_DRV_PRIV << 0)
108 #define SI_RESOURCE_FLAG_FLUSHED_DEPTH (PIPE_RESOURCE_FLAG_DRV_PRIV << 1)
109 #define SI_RESOURCE_FLAG_FORCE_MSAA_TILING (PIPE_RESOURCE_FLAG_DRV_PRIV << 2)
110 #define SI_RESOURCE_FLAG_DISABLE_DCC (PIPE_RESOURCE_FLAG_DRV_PRIV << 3)
111 #define SI_RESOURCE_FLAG_UNMAPPABLE (PIPE_RESOURCE_FLAG_DRV_PRIV << 4)
112 #define SI_RESOURCE_FLAG_READ_ONLY (PIPE_RESOURCE_FLAG_DRV_PRIV << 5)
113 #define SI_RESOURCE_FLAG_32BIT (PIPE_RESOURCE_FLAG_DRV_PRIV << 6)
114 #define SI_RESOURCE_FLAG_CLEAR (PIPE_RESOURCE_FLAG_DRV_PRIV << 7)
115 /* For const_uploader, upload data via GTT and copy to VRAM on context flush via SDMA. */
116 #define SI_RESOURCE_FLAG_UPLOAD_FLUSH_EXPLICIT_VIA_SDMA (PIPE_RESOURCE_FLAG_DRV_PRIV << 8)
120 DCC_CLEAR_COLOR_0000
= 0x00000000,
121 DCC_CLEAR_COLOR_0001
= 0x40404040,
122 DCC_CLEAR_COLOR_1110
= 0x80808080,
123 DCC_CLEAR_COLOR_1111
= 0xC0C0C0C0,
124 DCC_CLEAR_COLOR_REG
= 0x20202020,
125 DCC_UNCOMPRESSED
= 0xFFFFFFFF,
128 #define SI_IMAGE_ACCESS_AS_BUFFER (1 << 7)
132 /* Shader logging options: */
133 DBG_VS
= PIPE_SHADER_VERTEX
,
134 DBG_PS
= PIPE_SHADER_FRAGMENT
,
135 DBG_GS
= PIPE_SHADER_GEOMETRY
,
136 DBG_TCS
= PIPE_SHADER_TESS_CTRL
,
137 DBG_TES
= PIPE_SHADER_TESS_EVAL
,
138 DBG_CS
= PIPE_SHADER_COMPUTE
,
144 /* Shader compiler options the shader cache should be aware of: */
145 DBG_FS_CORRECT_DERIVS_AFTER_KILL
,
150 /* Shader compiler options (with no effect on the shader cache): */
152 DBG_MONOLITHIC_SHADERS
,
155 /* Information logging options: */
161 /* Driver options: */
169 /* 3D engine options: */
192 DBG_TEST_VMFAULT_SDMA
,
193 DBG_TEST_VMFAULT_SHADER
,
200 #define DBG_ALL_SHADERS (((1 << (DBG_CS + 1)) - 1))
201 #define DBG(name) (1ull << DBG_##name)
203 enum si_cache_policy
{
205 L2_STREAM
, /* same as SLC=1 */
206 L2_LRU
, /* same as SLC=0 */
210 SI_COHERENCY_NONE
, /* no cache flushes needed */
212 SI_COHERENCY_CB_META
,
217 struct si_shader_context
;
219 struct u_suballocator
;
221 /* Only 32-bit buffer allocations are supported, gallium doesn't support more
225 struct threaded_resource b
;
227 /* Winsys objects. */
228 struct pb_buffer
*buf
;
229 uint64_t gpu_address
;
230 /* Memory usage if the buffer placement is optimal. */
234 /* Resource properties. */
236 unsigned bo_alignment
;
237 enum radeon_bo_domain domains
;
238 enum radeon_bo_flag flags
;
239 unsigned bind_history
;
240 int max_forced_staging_uploads
;
242 /* The buffer range which is initialized (with a write transfer,
243 * streamout, DMA, or as a random access target). The rest of
244 * the buffer is considered invalid and can be mapped unsynchronized.
246 * This allows unsychronized mapping of a buffer range which hasn't
247 * been used yet. It's for applications which forget to use
248 * the unsynchronized map flag and expect the driver to figure it out.
250 struct util_range valid_buffer_range
;
252 /* For buffers only. This indicates that a write operation has been
253 * performed by TC L2, but the cache hasn't been flushed.
254 * Any hw block which doesn't use or bypasses TC L2 should check this
255 * flag and flush the cache before using the buffer.
257 * For example, TC L2 must be flushed if a buffer which has been
258 * modified by a shader store instruction is about to be used as
259 * an index buffer. The reason is that VGT DMA index fetching doesn't
264 /* Whether this resource is referenced by bindless handles. */
265 bool texture_handle_allocated
;
266 bool image_handle_allocated
;
268 /* Whether the resource has been exported via resource_get_handle. */
269 unsigned external_usage
; /* PIPE_HANDLE_USAGE_* */
273 struct threaded_transfer b
;
274 struct si_resource
*staging
;
279 struct si_resource buffer
;
281 struct radeon_surf surface
;
283 struct si_texture
*flushed_depth_texture
;
285 /* One texture allocation can contain these buffers:
286 * - image (pixel data)
287 * - FMASK buffer (MSAA compression)
288 * - CMASK buffer (MSAA compression and/or legacy fast color clear)
289 * - HTILE buffer (Z/S compression and fast Z/S clear)
290 * - DCC buffer (color compression and new fast color clear)
291 * - displayable DCC buffer (if the DCC buffer is not displayable)
292 * - DCC retile mapping buffer (if the DCC buffer is not displayable)
294 uint64_t fmask_offset
;
295 uint64_t cmask_offset
;
296 uint64_t cmask_base_address_reg
;
297 struct si_resource
*cmask_buffer
;
298 uint64_t dcc_offset
; /* 0 = disabled */
299 uint64_t display_dcc_offset
;
300 uint64_t dcc_retile_map_offset
;
301 unsigned cb_color_info
; /* fast clear enable bit */
302 unsigned color_clear_value
[2];
303 unsigned last_msaa_resolve_target_micro_mode
;
304 unsigned num_level0_transfers
;
306 /* Depth buffer compression and fast clear. */
307 uint64_t htile_offset
;
308 float depth_clear_value
;
309 uint16_t dirty_level_mask
; /* each bit says if that mipmap is compressed */
310 uint16_t stencil_dirty_level_mask
; /* each bit says if that mipmap is compressed */
311 enum pipe_format db_render_format
:16;
312 uint8_t stencil_clear_value
;
313 bool tc_compatible_htile
:1;
314 bool depth_cleared
:1; /* if it was cleared at least once */
315 bool stencil_cleared
:1; /* if it was cleared at least once */
316 bool upgraded_depth
:1; /* upgraded from unorm to Z32_FLOAT */
318 bool db_compatible
:1;
322 /* We need to track DCC dirtiness, because st/dri usually calls
323 * flush_resource twice per frame (not a bug) and we don't wanna
324 * decompress DCC twice. Also, the dirty tracking must be done even
325 * if DCC isn't used, because it's required by the DCC usage analysis
326 * for a possible future enablement.
328 bool separate_dcc_dirty
:1;
329 /* Statistics gathering for the DCC enablement heuristic. */
330 bool dcc_gather_statistics
:1;
331 /* Counter that should be non-zero if the texture is bound to a
334 unsigned framebuffers_bound
;
335 /* Whether the texture is a displayable back buffer and needs DCC
336 * decompression, which is expensive. Therefore, it's enabled only
337 * if statistics suggest that it will pay off and it's allocated
338 * separately. It can't be bound as a sampler by apps. Limited to
339 * target == 2D and last_level == 0. If enabled, dcc_offset contains
340 * the absolute GPUVM address, not the relative one.
342 struct si_resource
*dcc_separate_buffer
;
343 /* When DCC is temporarily disabled, the separate buffer is here. */
344 struct si_resource
*last_dcc_separate_buffer
;
345 /* Estimate of how much this color buffer is written to in units of
346 * full-screen draws: ps_invocations / (width * height)
347 * Shader kills, late Z, and blending with trivial discards make it
348 * inaccurate (we need to count CB updates, not PS invocations).
350 unsigned ps_draw_ratio
;
351 /* The number of clears since the last DCC usage analysis. */
352 unsigned num_slow_clears
;
356 struct pipe_surface base
;
358 /* These can vary with block-compressed textures. */
362 bool color_initialized
:1;
363 bool depth_initialized
:1;
365 /* Misc. color flags. */
366 bool color_is_int8
:1;
367 bool color_is_int10
:1;
368 bool dcc_incompatible
:1;
370 /* Color registers. */
371 unsigned cb_color_info
;
372 unsigned cb_color_view
;
373 unsigned cb_color_attrib
;
374 unsigned cb_color_attrib2
; /* GFX9 and later */
375 unsigned cb_dcc_control
; /* GFX8 and later */
376 unsigned spi_shader_col_format
:8; /* no blending, no alpha-to-coverage. */
377 unsigned spi_shader_col_format_alpha
:8; /* alpha-to-coverage */
378 unsigned spi_shader_col_format_blend
:8; /* blending without alpha. */
379 unsigned spi_shader_col_format_blend_alpha
:8; /* blending with alpha. */
382 uint64_t db_depth_base
; /* DB_Z_READ/WRITE_BASE */
383 uint64_t db_stencil_base
;
384 uint64_t db_htile_data_base
;
385 unsigned db_depth_info
;
387 unsigned db_z_info2
; /* GFX9+ */
388 unsigned db_depth_view
;
389 unsigned db_depth_size
;
390 unsigned db_depth_slice
;
391 unsigned db_stencil_info
;
392 unsigned db_stencil_info2
; /* GFX9+ */
393 unsigned db_htile_surface
;
396 struct si_mmio_counter
{
401 union si_mmio_counters
{
403 /* For global GPU load including SDMA. */
404 struct si_mmio_counter gpu
;
407 struct si_mmio_counter spi
;
408 struct si_mmio_counter gui
;
409 struct si_mmio_counter ta
;
410 struct si_mmio_counter gds
;
411 struct si_mmio_counter vgt
;
412 struct si_mmio_counter ia
;
413 struct si_mmio_counter sx
;
414 struct si_mmio_counter wd
;
415 struct si_mmio_counter bci
;
416 struct si_mmio_counter sc
;
417 struct si_mmio_counter pa
;
418 struct si_mmio_counter db
;
419 struct si_mmio_counter cp
;
420 struct si_mmio_counter cb
;
423 struct si_mmio_counter sdma
;
426 struct si_mmio_counter pfp
;
427 struct si_mmio_counter meq
;
428 struct si_mmio_counter me
;
429 struct si_mmio_counter surf_sync
;
430 struct si_mmio_counter cp_dma
;
431 struct si_mmio_counter scratch_ram
;
436 struct si_memory_object
{
437 struct pipe_memory_object b
;
438 struct pb_buffer
*buf
;
442 /* Saved CS data for debugging features. */
443 struct radeon_saved_cs
{
447 struct radeon_bo_list_item
*bo_list
;
452 struct pipe_screen b
;
453 struct radeon_winsys
*ws
;
454 struct disk_cache
*disk_shader_cache
;
456 struct radeon_info info
;
457 uint64_t debug_flags
;
458 char renderer_string
[183];
460 unsigned pa_sc_raster_config
;
461 unsigned pa_sc_raster_config_1
;
462 unsigned se_tile_repeat
;
463 unsigned gs_table_depth
;
464 unsigned tess_offchip_block_dw_size
;
465 unsigned tess_offchip_ring_size
;
466 unsigned tess_factor_ring_size
;
467 unsigned vgt_hs_offchip_param
;
468 unsigned eqaa_force_coverage_samples
;
469 unsigned eqaa_force_z_samples
;
470 unsigned eqaa_force_color_samples
;
471 bool has_clear_state
;
472 bool has_distributed_tess
;
473 bool has_draw_indirect_multi
;
474 bool has_out_of_order_rast
;
475 bool assume_no_z_fights
;
476 bool commutative_blend_add
;
477 bool has_gfx9_scissor_bug
;
478 bool has_msaa_sample_loc_bug
;
479 bool has_ls_vgpr_init_bug
;
480 bool has_dcc_constant_encode
;
483 bool llvm_has_working_vgpr_indexing
;
486 #define OPT_BOOL(name, dflt, description) bool name:1;
487 #include "si_debug_options.h"
490 /* Whether shaders are monolithic (1-part) or separate (3-part). */
491 bool use_monolithic_shaders
;
493 bool has_rbplus
; /* if RB+ registers exist */
494 bool rbplus_allowed
; /* if RB+ is allowed */
495 bool dcc_msaa_allowed
;
496 bool cpdma_prefetch_writes_memory
;
498 struct slab_parent_pool pool_transfers
;
500 /* Texture filter settings. */
501 int force_aniso
; /* -1 = disabled */
503 /* Auxiliary context. Mainly used to initialize resources.
504 * It must be locked prior to using and flushed before unlocking. */
505 struct pipe_context
*aux_context
;
506 mtx_t aux_context_lock
;
508 /* This must be in the screen, because UE4 uses one context for
509 * compilation and another one for rendering.
511 unsigned num_compilations
;
512 /* Along with ST_DEBUG=precompile, this should show if applications
513 * are loading shaders on demand. This is a monotonic counter.
515 unsigned num_shaders_created
;
516 unsigned num_shader_cache_hits
;
518 /* GPU load thread. */
519 mtx_t gpu_load_mutex
;
520 thrd_t gpu_load_thread
;
521 union si_mmio_counters mmio_counters
;
522 volatile unsigned gpu_load_stop_thread
; /* bool */
524 /* Performance counters. */
525 struct si_perfcounters
*perfcounters
;
527 /* If pipe_screen wants to recompute and re-emit the framebuffer,
528 * sampler, and image states of all contexts, it should atomically
531 * Each context will compare this with its own last known value of
532 * the counter before drawing and re-emit the states accordingly.
534 unsigned dirty_tex_counter
;
535 unsigned dirty_buf_counter
;
537 /* Atomically increment this counter when an existing texture's
538 * metadata is enabled or disabled in a way that requires changing
539 * contexts' compressed texture binding masks.
541 unsigned compressed_colortex_counter
;
544 /* Context flags to set so that all writes from earlier jobs
545 * in the CP are seen by L2 clients.
549 /* Context flags to set so that all writes from earlier jobs
550 * that end in L2 are seen by CP.
555 mtx_t shader_parts_mutex
;
556 struct si_shader_part
*vs_prologs
;
557 struct si_shader_part
*tcs_epilogs
;
558 struct si_shader_part
*gs_prologs
;
559 struct si_shader_part
*ps_prologs
;
560 struct si_shader_part
*ps_epilogs
;
562 /* Shader cache in memory.
564 * Design & limitations:
565 * - The shader cache is per screen (= per process), never saved to
566 * disk, and skips redundant shader compilations from TGSI to bytecode.
567 * - It can only be used with one-variant-per-shader support, in which
568 * case only the main (typically middle) part of shaders is cached.
569 * - Only VS, TCS, TES, PS are cached, out of which only the hw VS
570 * variants of VS and TES are cached, so LS and ES aren't.
571 * - GS and CS aren't cached, but it's certainly possible to cache
574 mtx_t shader_cache_mutex
;
575 struct hash_table
*shader_cache
;
577 /* Shader compiler queue for multithreaded compilation. */
578 struct util_queue shader_compiler_queue
;
579 /* Use at most 3 normal compiler threads on quadcore and better.
580 * Hyperthreaded CPUs report the number of threads, but we want
581 * the number of cores. We only need this many threads for shader-db. */
582 struct ac_llvm_compiler compiler
[24]; /* used by the queue only */
584 struct util_queue shader_compiler_queue_low_priority
;
585 /* Use at most 2 low priority threads on quadcore and better.
586 * We want to minimize the impact on multithreaded Mesa. */
587 struct ac_llvm_compiler compiler_lowp
[10];
590 struct si_blend_color
{
591 struct pipe_blend_color state
;
595 struct si_sampler_view
{
596 struct pipe_sampler_view base
;
597 /* [0..7] = image descriptor
598 * [4..7] = buffer descriptor */
600 uint32_t fmask_state
[8];
601 const struct legacy_surf_level
*base_level_info
;
604 bool is_stencil_sampler
;
606 bool dcc_incompatible
;
609 #define SI_SAMPLER_STATE_MAGIC 0x34f1c35a
611 struct si_sampler_state
{
616 uint32_t integer_val
[4];
617 uint32_t upgraded_depth_val
[4];
620 struct si_cs_shader_state
{
621 struct si_compute
*program
;
622 struct si_compute
*emitted_program
;
629 struct pipe_sampler_view
*views
[SI_NUM_SAMPLERS
];
630 struct si_sampler_state
*sampler_states
[SI_NUM_SAMPLERS
];
632 /* The i-th bit is set if that element is enabled (non-NULL resource). */
633 unsigned enabled_mask
;
634 uint32_t needs_depth_decompress_mask
;
635 uint32_t needs_color_decompress_mask
;
639 struct pipe_image_view views
[SI_NUM_IMAGES
];
640 uint32_t needs_color_decompress_mask
;
641 unsigned enabled_mask
;
644 struct si_framebuffer
{
645 struct pipe_framebuffer_state state
;
646 unsigned colorbuf_enabled_4bit
;
647 unsigned spi_shader_col_format
;
648 unsigned spi_shader_col_format_alpha
;
649 unsigned spi_shader_col_format_blend
;
650 unsigned spi_shader_col_format_blend_alpha
;
651 ubyte nr_samples
:5; /* at most 16xAA */
652 ubyte log_samples
:3; /* at most 4 = 16xAA */
653 ubyte nr_color_samples
; /* at most 8xAA */
654 ubyte compressed_cb_mask
;
655 ubyte uncompressed_cb_mask
;
657 ubyte color_is_int10
;
659 ubyte dcc_overwrite_combiner_watermark
;
662 bool CB_has_shader_readable_metadata
;
663 bool DB_has_shader_readable_metadata
;
664 bool all_DCC_pipe_aligned
;
668 /* This is the list we want to support. */
669 SI_QUANT_MODE_16_8_FIXED_POINT_1_256TH
,
670 SI_QUANT_MODE_14_10_FIXED_POINT_1_1024TH
,
671 SI_QUANT_MODE_12_12_FIXED_POINT_1_4096TH
,
674 struct si_signed_scissor
{
679 enum si_quant_mode quant_mode
;
682 struct si_viewports
{
683 struct pipe_viewport_state states
[SI_MAX_VIEWPORTS
];
684 struct si_signed_scissor as_scissor
[SI_MAX_VIEWPORTS
];
688 struct si_clip_state
{
689 struct pipe_clip_state state
;
693 struct si_streamout_target
{
694 struct pipe_stream_output_target b
;
696 /* The buffer where BUFFER_FILLED_SIZE is stored. */
697 struct si_resource
*buf_filled_size
;
698 unsigned buf_filled_size_offset
;
699 bool buf_filled_size_valid
;
701 unsigned stride_in_dw
;
704 struct si_streamout
{
707 unsigned enabled_mask
;
708 unsigned num_targets
;
709 struct si_streamout_target
*targets
[PIPE_MAX_SO_BUFFERS
];
711 unsigned append_bitmask
;
714 /* External state which comes from the vertex shader,
715 * it must be set explicitly when binding a shader. */
716 uint16_t *stride_in_dw
;
717 unsigned enabled_stream_buffers_mask
; /* stream0 buffers0-3 in 4 LSB */
719 /* The state of VGT_STRMOUT_BUFFER_(CONFIG|EN). */
720 unsigned hw_enabled_mask
;
722 /* The state of VGT_STRMOUT_(CONFIG|EN). */
723 bool streamout_enabled
;
724 bool prims_gen_query_enabled
;
725 int num_prims_gen_queries
;
728 /* A shader state consists of the shader selector, which is a constant state
729 * object shared by multiple contexts and shouldn't be modified, and
730 * the current shader variant selected for this context.
732 struct si_shader_ctx_state
{
733 struct si_shader_selector
*cso
;
734 struct si_shader
*current
;
737 #define SI_NUM_VGT_PARAM_KEY_BITS 12
738 #define SI_NUM_VGT_PARAM_STATES (1 << SI_NUM_VGT_PARAM_KEY_BITS)
740 /* The IA_MULTI_VGT_PARAM key used to index the table of precomputed values.
741 * Some fields are set by state-change calls, most are set by draw_vbo.
743 union si_vgt_param_key
{
745 #ifdef PIPE_ARCH_LITTLE_ENDIAN
747 unsigned uses_instancing
:1;
748 unsigned multi_instances_smaller_than_primgroup
:1;
749 unsigned primitive_restart
:1;
750 unsigned count_from_stream_output
:1;
751 unsigned line_stipple_enabled
:1;
752 unsigned uses_tess
:1;
753 unsigned tess_uses_prim_id
:1;
755 unsigned _pad
:32 - SI_NUM_VGT_PARAM_KEY_BITS
;
756 #else /* PIPE_ARCH_BIG_ENDIAN */
757 unsigned _pad
:32 - SI_NUM_VGT_PARAM_KEY_BITS
;
759 unsigned tess_uses_prim_id
:1;
760 unsigned uses_tess
:1;
761 unsigned line_stipple_enabled
:1;
762 unsigned count_from_stream_output
:1;
763 unsigned primitive_restart
:1;
764 unsigned multi_instances_smaller_than_primgroup
:1;
765 unsigned uses_instancing
:1;
772 #define SI_NUM_VGT_STAGES_KEY_BITS 2
773 #define SI_NUM_VGT_STAGES_STATES (1 << SI_NUM_VGT_STAGES_KEY_BITS)
775 /* The VGT_SHADER_STAGES key used to index the table of precomputed values.
776 * Some fields are set by state-change calls, most are set by draw_vbo.
778 union si_vgt_stages_key
{
780 #ifdef PIPE_ARCH_LITTLE_ENDIAN
783 unsigned _pad
:32 - SI_NUM_VGT_STAGES_KEY_BITS
;
784 #else /* PIPE_ARCH_BIG_ENDIAN */
785 unsigned _pad
:32 - SI_NUM_VGT_STAGES_KEY_BITS
;
793 struct si_texture_handle
797 struct pipe_sampler_view
*view
;
798 struct si_sampler_state sstate
;
801 struct si_image_handle
805 struct pipe_image_view view
;
809 struct pipe_reference reference
;
810 struct si_context
*ctx
;
811 struct radeon_saved_cs gfx
;
812 struct radeon_saved_cs compute
;
813 struct si_resource
*trace_buf
;
816 unsigned gfx_last_dw
;
817 unsigned compute_last_dw
;
822 struct si_sdma_upload
{
823 struct si_resource
*dst
;
824 struct si_resource
*src
;
831 struct pipe_context b
; /* base class */
833 enum radeon_family family
;
834 enum chip_class chip_class
;
836 struct radeon_winsys
*ws
;
837 struct radeon_winsys_ctx
*ctx
;
838 struct radeon_cmdbuf
*gfx_cs
; /* compute IB if graphics is disabled */
839 struct radeon_cmdbuf
*dma_cs
;
840 struct pipe_fence_handle
*last_gfx_fence
;
841 struct pipe_fence_handle
*last_sdma_fence
;
842 struct si_resource
*eop_bug_scratch
;
843 struct u_upload_mgr
*cached_gtt_allocator
;
844 struct threaded_context
*tc
;
845 struct u_suballocator
*allocator_zeroed_memory
;
846 struct slab_child_pool pool_transfers
;
847 struct slab_child_pool pool_transfers_unsync
; /* for threaded_context */
848 struct pipe_device_reset_callback device_reset_callback
;
849 struct u_log_context
*log
;
850 void *query_result_shader
;
851 struct blitter_context
*blitter
;
852 void *custom_dsa_flush
;
853 void *custom_blend_resolve
;
854 void *custom_blend_fmask_decompress
;
855 void *custom_blend_eliminate_fastclear
;
856 void *custom_blend_dcc_decompress
;
858 void *vs_blit_pos_layered
;
860 void *vs_blit_color_layered
;
861 void *vs_blit_texcoord
;
862 void *cs_clear_buffer
;
863 void *cs_copy_buffer
;
865 void *cs_copy_image_1d_array
;
866 void *cs_clear_render_target
;
867 void *cs_clear_render_target_1d_array
;
869 struct si_screen
*screen
;
870 struct pipe_debug_callback debug
;
871 struct ac_llvm_compiler compiler
; /* only non-threaded compilation */
872 struct si_shader_ctx_state fixed_func_tcs_shader
;
873 /* Offset 0: EOP flush number; Offset 4: GDS prim restart counter */
874 struct si_resource
*wait_mem_scratch
;
875 unsigned wait_mem_number
;
876 uint16_t prefetch_L2_mask
;
879 bool gfx_flush_in_progress
:1;
880 bool gfx_last_ib_is_busy
:1;
881 bool compute_is_busy
:1;
883 unsigned num_gfx_cs_flushes
;
884 unsigned initial_gfx_cs_size
;
885 unsigned last_dirty_tex_counter
;
886 unsigned last_dirty_buf_counter
;
887 unsigned last_compressed_colortex_counter
;
888 unsigned last_num_draw_calls
;
889 unsigned flags
; /* flush flags */
890 /* Current unaccounted memory usage. */
894 /* Compute-based primitive discard. */
895 unsigned prim_discard_vertex_count_threshold
;
896 struct pb_buffer
*gds
;
897 struct pb_buffer
*gds_oa
;
898 struct radeon_cmdbuf
*prim_discard_compute_cs
;
899 unsigned compute_gds_offset
;
900 struct si_shader
*compute_ib_last_shader
;
901 uint32_t compute_rewind_va
;
902 unsigned compute_num_prims_in_batch
;
903 bool preserve_prim_restart_gds_at_flush
;
904 /* index_ring is divided into 2 halves for doublebuffering. */
905 struct si_resource
*index_ring
;
906 unsigned index_ring_base
; /* offset of a per-IB portion */
907 unsigned index_ring_offset
; /* offset within a per-IB portion */
908 unsigned index_ring_size_per_ib
; /* max available size per IB */
909 bool prim_discard_compute_ib_initialized
;
910 /* For tracking the last execution barrier - it can be either
911 * a WRITE_DATA packet or a fence. */
912 uint32_t *last_pkt3_write_data
;
913 struct si_resource
*barrier_buf
;
914 unsigned barrier_buf_offset
;
915 struct pipe_fence_handle
*last_ib_barrier_fence
;
916 struct si_resource
*last_ib_barrier_buf
;
917 unsigned last_ib_barrier_buf_offset
;
919 /* Atoms (direct states). */
920 union si_state_atoms atoms
;
921 unsigned dirty_atoms
; /* mask */
922 /* PM4 states (precomputed immutable states) */
923 unsigned dirty_states
;
924 union si_state queued
;
925 union si_state emitted
;
927 /* Atom declarations. */
928 struct si_framebuffer framebuffer
;
929 unsigned sample_locs_num_samples
;
930 uint16_t sample_mask
;
931 unsigned last_cb_target_mask
;
932 struct si_blend_color blend_color
;
933 struct si_clip_state clip_state
;
934 struct si_shader_data shader_pointers
;
935 struct si_stencil_ref stencil_ref
;
936 struct pipe_scissor_state scissors
[SI_MAX_VIEWPORTS
];
937 struct si_streamout streamout
;
938 struct si_viewports viewports
;
939 unsigned num_window_rectangles
;
940 bool window_rectangles_include
;
941 struct pipe_scissor_state window_rectangles
[4];
943 /* Precomputed states. */
944 struct si_pm4_state
*init_config
;
945 struct si_pm4_state
*init_config_gs_rings
;
946 bool init_config_has_vgt_flush
;
947 struct si_pm4_state
*vgt_shader_config
[SI_NUM_VGT_STAGES_STATES
];
950 struct si_shader_ctx_state ps_shader
;
951 struct si_shader_ctx_state gs_shader
;
952 struct si_shader_ctx_state vs_shader
;
953 struct si_shader_ctx_state tcs_shader
;
954 struct si_shader_ctx_state tes_shader
;
955 struct si_shader_ctx_state cs_prim_discard_state
;
956 struct si_cs_shader_state cs_shader_state
;
958 /* shader information */
959 struct si_vertex_elements
*vertex_elements
;
960 unsigned sprite_coord_enable
;
961 unsigned cs_max_waves_per_sh
;
963 bool do_update_shaders
;
965 /* vertex buffer descriptors */
966 uint32_t *vb_descriptors_gpu_list
;
967 struct si_resource
*vb_descriptors_buffer
;
968 unsigned vb_descriptors_offset
;
970 /* shader descriptors */
971 struct si_descriptors descriptors
[SI_NUM_DESCS
];
972 unsigned descriptors_dirty
;
973 unsigned shader_pointers_dirty
;
974 unsigned shader_needs_decompress_mask
;
975 struct si_buffer_resources rw_buffers
;
976 struct si_buffer_resources const_and_shader_buffers
[SI_NUM_SHADERS
];
977 struct si_samplers samplers
[SI_NUM_SHADERS
];
978 struct si_images images
[SI_NUM_SHADERS
];
979 bool bo_list_add_all_resident_resources
;
980 bool bo_list_add_all_gfx_resources
;
981 bool bo_list_add_all_compute_resources
;
983 /* other shader resources */
984 struct pipe_constant_buffer null_const_buf
; /* used for set_constant_buffer(NULL) on GFX7 */
985 struct pipe_resource
*esgs_ring
;
986 struct pipe_resource
*gsvs_ring
;
987 struct pipe_resource
*tess_rings
;
988 union pipe_color_union
*border_color_table
; /* in CPU memory, any endian */
989 struct si_resource
*border_color_buffer
;
990 union pipe_color_union
*border_color_map
; /* in VRAM (slow access), little endian */
991 unsigned border_color_count
;
992 unsigned num_vs_blit_sgprs
;
993 uint32_t vs_blit_sh_data
[SI_VS_BLIT_SGPRS_POS_TEXCOORD
];
994 uint32_t cs_user_data
[4];
996 /* Vertex and index buffers. */
997 bool vertex_buffers_dirty
;
998 bool vertex_buffer_pointer_dirty
;
999 struct pipe_vertex_buffer vertex_buffer
[SI_NUM_VERTEX_BUFFERS
];
1000 uint16_t vertex_buffer_unaligned
; /* bitmask of not dword-aligned buffers */
1002 /* MSAA config state. */
1003 int ps_iter_samples
;
1004 bool ps_uses_fbfetch
;
1005 bool smoothing_enabled
;
1007 /* DB render state. */
1008 unsigned ps_db_shader_control
;
1009 unsigned dbcb_copy_sample
;
1010 bool dbcb_depth_copy_enabled
:1;
1011 bool dbcb_stencil_copy_enabled
:1;
1012 bool db_flush_depth_inplace
:1;
1013 bool db_flush_stencil_inplace
:1;
1014 bool db_depth_clear
:1;
1015 bool db_depth_disable_expclear
:1;
1016 bool db_stencil_clear
:1;
1017 bool db_stencil_disable_expclear
:1;
1018 bool occlusion_queries_disabled
:1;
1019 bool generate_mipmap_for_depth
:1;
1021 /* Emitted draw state. */
1022 bool gs_tri_strip_adj_fix
:1;
1024 bool prim_discard_cs_instancing
:1;
1025 int last_index_size
;
1026 int last_base_vertex
;
1027 int last_start_instance
;
1028 int last_instance_count
;
1030 int last_sh_base_reg
;
1031 int last_primitive_restart_en
;
1032 int last_restart_index
;
1034 int last_multi_vgt_param
;
1036 unsigned last_sc_line_stipple
;
1037 unsigned current_vs_state
;
1038 unsigned last_vs_state
;
1039 enum pipe_prim_type current_rast_prim
; /* primitive type after TES, GS */
1041 /* Scratch buffer */
1042 struct si_resource
*scratch_buffer
;
1043 unsigned scratch_waves
;
1044 unsigned spi_tmpring_size
;
1046 struct si_resource
*compute_scratch_buffer
;
1048 /* Emitted derived tessellation state. */
1049 /* Local shader (VS), or HS if LS-HS are merged. */
1050 struct si_shader
*last_ls
;
1051 struct si_shader_selector
*last_tcs
;
1052 int last_num_tcs_input_cp
;
1053 int last_tes_sh_base
;
1054 bool last_tess_uses_primid
;
1055 unsigned last_num_patches
;
1056 int last_ls_hs_config
;
1060 struct si_saved_cs
*current_saved_cs
;
1061 uint64_t dmesg_timestamp
;
1062 unsigned apitrace_call_number
;
1065 bool need_check_render_feedback
;
1066 bool decompression_enabled
;
1067 bool dpbb_force_off
;
1068 bool vs_writes_viewport_index
;
1069 bool vs_disables_clipping_viewport
;
1071 /* Precomputed IA_MULTI_VGT_PARAM */
1072 union si_vgt_param_key ia_multi_vgt_param_key
;
1073 unsigned ia_multi_vgt_param
[SI_NUM_VGT_PARAM_STATES
];
1075 /* Bindless descriptors. */
1076 struct si_descriptors bindless_descriptors
;
1077 struct util_idalloc bindless_used_slots
;
1078 unsigned num_bindless_descriptors
;
1079 bool bindless_descriptors_dirty
;
1080 bool graphics_bindless_pointer_dirty
;
1081 bool compute_bindless_pointer_dirty
;
1083 /* Allocated bindless handles */
1084 struct hash_table
*tex_handles
;
1085 struct hash_table
*img_handles
;
1087 /* Resident bindless handles */
1088 struct util_dynarray resident_tex_handles
;
1089 struct util_dynarray resident_img_handles
;
1091 /* Resident bindless handles which need decompression */
1092 struct util_dynarray resident_tex_needs_color_decompress
;
1093 struct util_dynarray resident_img_needs_color_decompress
;
1094 struct util_dynarray resident_tex_needs_depth_decompress
;
1096 /* Bindless state */
1097 bool uses_bindless_samplers
;
1098 bool uses_bindless_images
;
1100 /* MSAA sample locations.
1101 * The first index is the sample index.
1102 * The second index is the coordinate: X, Y. */
1110 struct pipe_resource
*sample_pos_buffer
;
1113 unsigned num_draw_calls
;
1114 unsigned num_decompress_calls
;
1115 unsigned num_mrt_draw_calls
;
1116 unsigned num_prim_restart_calls
;
1117 unsigned num_spill_draw_calls
;
1118 unsigned num_compute_calls
;
1119 unsigned num_spill_compute_calls
;
1120 unsigned num_dma_calls
;
1121 unsigned num_cp_dma_calls
;
1122 unsigned num_vs_flushes
;
1123 unsigned num_ps_flushes
;
1124 unsigned num_cs_flushes
;
1125 unsigned num_cb_cache_flushes
;
1126 unsigned num_db_cache_flushes
;
1127 unsigned num_L2_invalidates
;
1128 unsigned num_L2_writebacks
;
1129 unsigned num_resident_handles
;
1130 uint64_t num_alloc_tex_transfer_bytes
;
1131 unsigned last_tex_ps_draw_ratio
; /* for query */
1132 unsigned compute_num_verts_accepted
;
1133 unsigned compute_num_verts_rejected
;
1134 unsigned compute_num_verts_ineligible
; /* due to low vertex count */
1135 unsigned context_roll
;
1138 /* Maintain the list of active queries for pausing between IBs. */
1139 int num_occlusion_queries
;
1140 int num_perfect_occlusion_queries
;
1141 int num_pipeline_stat_queries
;
1142 struct list_head active_queries
;
1143 unsigned num_cs_dw_queries_suspend
;
1145 /* Render condition. */
1146 struct pipe_query
*render_cond
;
1147 unsigned render_cond_mode
;
1148 bool render_cond_invert
;
1149 bool render_cond_force_off
; /* for u_blitter */
1151 /* For uploading data via GTT and copy to VRAM on context flush via SDMA. */
1152 bool sdma_uploads_in_progress
;
1153 struct si_sdma_upload
*sdma_uploads
;
1154 unsigned num_sdma_uploads
;
1155 unsigned max_sdma_uploads
;
1157 /* Statistics gathering for the DCC enablement heuristic. It can't be
1158 * in si_texture because si_texture can be shared by multiple
1159 * contexts. This is for back buffers only. We shouldn't get too many
1162 * X11 DRI3 rotates among a finite set of back buffers. They should
1163 * all fit in this array. If they don't, separate DCC might never be
1164 * enabled by DCC stat gathering.
1167 struct si_texture
*tex
;
1168 /* Query queue: 0 = usually active, 1 = waiting, 2 = readback. */
1169 struct pipe_query
*ps_stats
[3];
1170 /* If all slots are used and another slot is needed,
1171 * the least recently used slot is evicted based on this. */
1172 int64_t last_use_timestamp
;
1176 /* Copy one resource to another using async DMA. */
1177 void (*dma_copy
)(struct pipe_context
*ctx
,
1178 struct pipe_resource
*dst
,
1180 unsigned dst_x
, unsigned dst_y
, unsigned dst_z
,
1181 struct pipe_resource
*src
,
1183 const struct pipe_box
*src_box
);
1185 struct si_tracked_regs tracked_regs
;
1189 void cik_init_sdma_functions(struct si_context
*sctx
);
1192 enum si_blitter_op
/* bitmask */
1194 SI_SAVE_TEXTURES
= 1,
1195 SI_SAVE_FRAMEBUFFER
= 2,
1196 SI_SAVE_FRAGMENT_STATE
= 4,
1197 SI_DISABLE_RENDER_COND
= 8,
1200 void si_blitter_begin(struct si_context
*sctx
, enum si_blitter_op op
);
1201 void si_blitter_end(struct si_context
*sctx
);
1202 void si_init_blit_functions(struct si_context
*sctx
);
1203 void si_decompress_textures(struct si_context
*sctx
, unsigned shader_mask
);
1204 void si_resource_copy_region(struct pipe_context
*ctx
,
1205 struct pipe_resource
*dst
,
1207 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1208 struct pipe_resource
*src
,
1210 const struct pipe_box
*src_box
);
1211 void si_decompress_dcc(struct si_context
*sctx
, struct si_texture
*tex
);
1214 bool si_rings_is_buffer_referenced(struct si_context
*sctx
,
1215 struct pb_buffer
*buf
,
1216 enum radeon_bo_usage usage
);
1217 void *si_buffer_map_sync_with_rings(struct si_context
*sctx
,
1218 struct si_resource
*resource
,
1220 void si_init_resource_fields(struct si_screen
*sscreen
,
1221 struct si_resource
*res
,
1222 uint64_t size
, unsigned alignment
);
1223 bool si_alloc_resource(struct si_screen
*sscreen
,
1224 struct si_resource
*res
);
1225 struct pipe_resource
*pipe_aligned_buffer_create(struct pipe_screen
*screen
,
1226 unsigned flags
, unsigned usage
,
1227 unsigned size
, unsigned alignment
);
1228 struct si_resource
*si_aligned_buffer_create(struct pipe_screen
*screen
,
1229 unsigned flags
, unsigned usage
,
1230 unsigned size
, unsigned alignment
);
1231 void si_replace_buffer_storage(struct pipe_context
*ctx
,
1232 struct pipe_resource
*dst
,
1233 struct pipe_resource
*src
);
1234 void si_init_screen_buffer_functions(struct si_screen
*sscreen
);
1235 void si_init_buffer_functions(struct si_context
*sctx
);
1238 enum pipe_format
si_simplify_cb_format(enum pipe_format format
);
1239 bool vi_alpha_is_on_msb(enum pipe_format format
);
1240 void vi_dcc_clear_level(struct si_context
*sctx
,
1241 struct si_texture
*tex
,
1242 unsigned level
, unsigned clear_value
);
1243 void si_init_clear_functions(struct si_context
*sctx
);
1245 /* si_compute_blit.c */
1246 unsigned si_get_flush_flags(struct si_context
*sctx
, enum si_coherency coher
,
1247 enum si_cache_policy cache_policy
);
1248 void si_clear_buffer(struct si_context
*sctx
, struct pipe_resource
*dst
,
1249 uint64_t offset
, uint64_t size
, uint32_t *clear_value
,
1250 uint32_t clear_value_size
, enum si_coherency coher
,
1252 void si_copy_buffer(struct si_context
*sctx
,
1253 struct pipe_resource
*dst
, struct pipe_resource
*src
,
1254 uint64_t dst_offset
, uint64_t src_offset
, unsigned size
);
1255 void si_compute_copy_image(struct si_context
*sctx
,
1256 struct pipe_resource
*dst
,
1258 struct pipe_resource
*src
,
1260 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1261 const struct pipe_box
*src_box
);
1262 void si_compute_clear_render_target(struct pipe_context
*ctx
,
1263 struct pipe_surface
*dstsurf
,
1264 const union pipe_color_union
*color
,
1265 unsigned dstx
, unsigned dsty
,
1266 unsigned width
, unsigned height
,
1267 bool render_condition_enabled
);
1268 void si_retile_dcc(struct si_context
*sctx
, struct si_texture
*tex
);
1269 void si_init_compute_blit_functions(struct si_context
*sctx
);
1272 #define SI_CPDMA_SKIP_CHECK_CS_SPACE (1 << 0) /* don't call need_cs_space */
1273 #define SI_CPDMA_SKIP_SYNC_AFTER (1 << 1) /* don't wait for DMA after the copy */
1274 #define SI_CPDMA_SKIP_SYNC_BEFORE (1 << 2) /* don't wait for DMA before the copy (RAW hazards) */
1275 #define SI_CPDMA_SKIP_GFX_SYNC (1 << 3) /* don't flush caches and don't wait for PS/CS */
1276 #define SI_CPDMA_SKIP_BO_LIST_UPDATE (1 << 4) /* don't update the BO list */
1277 #define SI_CPDMA_SKIP_ALL (SI_CPDMA_SKIP_CHECK_CS_SPACE | \
1278 SI_CPDMA_SKIP_SYNC_AFTER | \
1279 SI_CPDMA_SKIP_SYNC_BEFORE | \
1280 SI_CPDMA_SKIP_GFX_SYNC | \
1281 SI_CPDMA_SKIP_BO_LIST_UPDATE)
1283 void si_cp_dma_wait_for_idle(struct si_context
*sctx
);
1284 void si_cp_dma_clear_buffer(struct si_context
*sctx
, struct radeon_cmdbuf
*cs
,
1285 struct pipe_resource
*dst
, uint64_t offset
,
1286 uint64_t size
, unsigned value
, unsigned user_flags
,
1287 enum si_coherency coher
, enum si_cache_policy cache_policy
);
1288 void si_cp_dma_copy_buffer(struct si_context
*sctx
,
1289 struct pipe_resource
*dst
, struct pipe_resource
*src
,
1290 uint64_t dst_offset
, uint64_t src_offset
, unsigned size
,
1291 unsigned user_flags
, enum si_coherency coher
,
1292 enum si_cache_policy cache_policy
);
1293 void cik_prefetch_TC_L2_async(struct si_context
*sctx
, struct pipe_resource
*buf
,
1294 uint64_t offset
, unsigned size
);
1295 void cik_emit_prefetch_L2(struct si_context
*sctx
, bool vertex_stage_only
);
1296 void si_test_gds(struct si_context
*sctx
);
1297 void si_cp_write_data(struct si_context
*sctx
, struct si_resource
*buf
,
1298 unsigned offset
, unsigned size
, unsigned dst_sel
,
1299 unsigned engine
, const void *data
);
1300 void si_cp_copy_data(struct si_context
*sctx
, struct radeon_cmdbuf
*cs
,
1301 unsigned dst_sel
, struct si_resource
*dst
, unsigned dst_offset
,
1302 unsigned src_sel
, struct si_resource
*src
, unsigned src_offset
);
1305 void si_save_cs(struct radeon_winsys
*ws
, struct radeon_cmdbuf
*cs
,
1306 struct radeon_saved_cs
*saved
, bool get_buffer_list
);
1307 void si_clear_saved_cs(struct radeon_saved_cs
*saved
);
1308 void si_destroy_saved_cs(struct si_saved_cs
*scs
);
1309 void si_auto_log_cs(void *data
, struct u_log_context
*log
);
1310 void si_log_hw_flush(struct si_context
*sctx
);
1311 void si_log_draw_state(struct si_context
*sctx
, struct u_log_context
*log
);
1312 void si_log_compute_state(struct si_context
*sctx
, struct u_log_context
*log
);
1313 void si_init_debug_functions(struct si_context
*sctx
);
1314 void si_check_vm_faults(struct si_context
*sctx
,
1315 struct radeon_saved_cs
*saved
, enum ring_type ring
);
1316 bool si_replace_shader(unsigned num
, struct si_shader_binary
*binary
);
1319 void si_init_dma_functions(struct si_context
*sctx
);
1322 void si_dma_emit_timestamp(struct si_context
*sctx
, struct si_resource
*dst
,
1324 void si_sdma_clear_buffer(struct si_context
*sctx
, struct pipe_resource
*dst
,
1325 uint64_t offset
, uint64_t size
, unsigned clear_value
);
1326 void si_need_dma_space(struct si_context
*ctx
, unsigned num_dw
,
1327 struct si_resource
*dst
, struct si_resource
*src
);
1328 void si_flush_dma_cs(struct si_context
*ctx
, unsigned flags
,
1329 struct pipe_fence_handle
**fence
);
1330 void si_screen_clear_buffer(struct si_screen
*sscreen
, struct pipe_resource
*dst
,
1331 uint64_t offset
, uint64_t size
, unsigned value
);
1334 void si_cp_release_mem(struct si_context
*ctx
, struct radeon_cmdbuf
*cs
,
1335 unsigned event
, unsigned event_flags
,
1336 unsigned dst_sel
, unsigned int_sel
, unsigned data_sel
,
1337 struct si_resource
*buf
, uint64_t va
,
1338 uint32_t new_fence
, unsigned query_type
);
1339 unsigned si_cp_write_fence_dwords(struct si_screen
*screen
);
1340 void si_cp_wait_mem(struct si_context
*ctx
, struct radeon_cmdbuf
*cs
,
1341 uint64_t va
, uint32_t ref
, uint32_t mask
, unsigned flags
);
1342 void si_init_fence_functions(struct si_context
*ctx
);
1343 void si_init_screen_fence_functions(struct si_screen
*screen
);
1344 struct pipe_fence_handle
*si_create_fence(struct pipe_context
*ctx
,
1345 struct tc_unflushed_batch_token
*tc_token
);
1348 void si_init_screen_get_functions(struct si_screen
*sscreen
);
1351 void si_flush_gfx_cs(struct si_context
*ctx
, unsigned flags
,
1352 struct pipe_fence_handle
**fence
);
1353 void si_begin_new_gfx_cs(struct si_context
*ctx
);
1354 void si_need_gfx_cs_space(struct si_context
*ctx
);
1355 void si_unref_sdma_uploads(struct si_context
*sctx
);
1358 void si_gpu_load_kill_thread(struct si_screen
*sscreen
);
1359 uint64_t si_begin_counter(struct si_screen
*sscreen
, unsigned type
);
1360 unsigned si_end_counter(struct si_screen
*sscreen
, unsigned type
,
1364 void si_emit_initial_compute_regs(struct si_context
*sctx
, struct radeon_cmdbuf
*cs
);
1365 unsigned si_get_compute_resource_limits(struct si_screen
*sscreen
,
1366 unsigned waves_per_threadgroup
,
1367 unsigned max_waves_per_sh
,
1368 unsigned threadgroups_per_cu
);
1369 void si_init_compute_functions(struct si_context
*sctx
);
1371 /* si_compute_prim_discard.c */
1372 enum si_prim_discard_outcome
{
1373 SI_PRIM_DISCARD_ENABLED
,
1374 SI_PRIM_DISCARD_DISABLED
,
1375 SI_PRIM_DISCARD_DRAW_SPLIT
,
1378 void si_build_prim_discard_compute_shader(struct si_shader_context
*ctx
);
1379 enum si_prim_discard_outcome
1380 si_prepare_prim_discard_or_split_draw(struct si_context
*sctx
,
1381 const struct pipe_draw_info
*info
,
1382 bool primitive_restart
);
1383 void si_compute_signal_gfx(struct si_context
*sctx
);
1384 void si_dispatch_prim_discard_cs_and_draw(struct si_context
*sctx
,
1385 const struct pipe_draw_info
*info
,
1386 unsigned index_size
,
1387 unsigned base_vertex
,
1388 uint64_t input_indexbuf_va
,
1389 unsigned input_indexbuf_max_elements
);
1390 void si_initialize_prim_discard_tunables(struct si_context
*sctx
);
1392 /* si_perfcounters.c */
1393 void si_init_perfcounters(struct si_screen
*screen
);
1394 void si_destroy_perfcounters(struct si_screen
*screen
);
1397 bool si_check_device_reset(struct si_context
*sctx
);
1400 void si_init_screen_query_functions(struct si_screen
*sscreen
);
1401 void si_init_query_functions(struct si_context
*sctx
);
1402 void si_suspend_queries(struct si_context
*sctx
);
1403 void si_resume_queries(struct si_context
*sctx
);
1405 /* si_shaderlib_tgsi.c */
1406 void *si_get_blitter_vs(struct si_context
*sctx
, enum blitter_attrib_type type
,
1407 unsigned num_layers
);
1408 void *si_create_fixed_func_tcs(struct si_context
*sctx
);
1409 void *si_create_dma_compute_shader(struct pipe_context
*ctx
,
1410 unsigned num_dwords_per_thread
,
1411 bool dst_stream_cache_policy
, bool is_copy
);
1412 void *si_create_copy_image_compute_shader(struct pipe_context
*ctx
);
1413 void *si_create_copy_image_compute_shader_1d_array(struct pipe_context
*ctx
);
1414 void *si_clear_render_target_shader(struct pipe_context
*ctx
);
1415 void *si_clear_render_target_shader_1d_array(struct pipe_context
*ctx
);
1416 void *si_create_dcc_retile_cs(struct pipe_context
*ctx
);
1417 void *si_create_query_result_cs(struct si_context
*sctx
);
1420 void si_test_dma(struct si_screen
*sscreen
);
1422 /* si_test_clearbuffer.c */
1423 void si_test_dma_perf(struct si_screen
*sscreen
);
1426 struct pipe_video_codec
*si_uvd_create_decoder(struct pipe_context
*context
,
1427 const struct pipe_video_codec
*templ
);
1429 struct pipe_video_buffer
*si_video_buffer_create(struct pipe_context
*pipe
,
1430 const struct pipe_video_buffer
*tmpl
);
1433 void si_update_vs_viewport_state(struct si_context
*ctx
);
1434 void si_init_viewport_functions(struct si_context
*ctx
);
1437 bool si_prepare_for_dma_blit(struct si_context
*sctx
,
1438 struct si_texture
*dst
,
1439 unsigned dst_level
, unsigned dstx
,
1440 unsigned dsty
, unsigned dstz
,
1441 struct si_texture
*src
,
1443 const struct pipe_box
*src_box
);
1444 void si_eliminate_fast_color_clear(struct si_context
*sctx
,
1445 struct si_texture
*tex
);
1446 void si_texture_discard_cmask(struct si_screen
*sscreen
,
1447 struct si_texture
*tex
);
1448 bool si_init_flushed_depth_texture(struct pipe_context
*ctx
,
1449 struct pipe_resource
*texture
);
1450 void si_print_texture_info(struct si_screen
*sscreen
,
1451 struct si_texture
*tex
, struct u_log_context
*log
);
1452 struct pipe_resource
*si_texture_create(struct pipe_screen
*screen
,
1453 const struct pipe_resource
*templ
);
1454 bool vi_dcc_formats_compatible(enum pipe_format format1
,
1455 enum pipe_format format2
);
1456 bool vi_dcc_formats_are_incompatible(struct pipe_resource
*tex
,
1458 enum pipe_format view_format
);
1459 void vi_disable_dcc_if_incompatible_format(struct si_context
*sctx
,
1460 struct pipe_resource
*tex
,
1462 enum pipe_format view_format
);
1463 struct pipe_surface
*si_create_surface_custom(struct pipe_context
*pipe
,
1464 struct pipe_resource
*texture
,
1465 const struct pipe_surface
*templ
,
1466 unsigned width0
, unsigned height0
,
1467 unsigned width
, unsigned height
);
1468 unsigned si_translate_colorswap(enum pipe_format format
, bool do_endian_swap
);
1469 void vi_separate_dcc_try_enable(struct si_context
*sctx
,
1470 struct si_texture
*tex
);
1471 void vi_separate_dcc_start_query(struct si_context
*sctx
,
1472 struct si_texture
*tex
);
1473 void vi_separate_dcc_stop_query(struct si_context
*sctx
,
1474 struct si_texture
*tex
);
1475 void vi_separate_dcc_process_and_reset_stats(struct pipe_context
*ctx
,
1476 struct si_texture
*tex
);
1477 bool si_texture_disable_dcc(struct si_context
*sctx
,
1478 struct si_texture
*tex
);
1479 void si_init_screen_texture_functions(struct si_screen
*sscreen
);
1480 void si_init_context_texture_functions(struct si_context
*sctx
);
1487 static inline struct si_resource
*si_resource(struct pipe_resource
*r
)
1489 return (struct si_resource
*)r
;
1493 si_resource_reference(struct si_resource
**ptr
, struct si_resource
*res
)
1495 pipe_resource_reference((struct pipe_resource
**)ptr
,
1496 (struct pipe_resource
*)res
);
1500 si_texture_reference(struct si_texture
**ptr
, struct si_texture
*res
)
1502 pipe_resource_reference((struct pipe_resource
**)ptr
, &res
->buffer
.b
.b
);
1506 vi_dcc_enabled(struct si_texture
*tex
, unsigned level
)
1508 return tex
->dcc_offset
&& level
< tex
->surface
.num_dcc_levels
;
1511 static inline unsigned
1512 si_tile_mode_index(struct si_texture
*tex
, unsigned level
, bool stencil
)
1515 return tex
->surface
.u
.legacy
.stencil_tiling_index
[level
];
1517 return tex
->surface
.u
.legacy
.tiling_index
[level
];
1520 static inline unsigned
1521 si_get_minimum_num_gfx_cs_dwords(struct si_context
*sctx
)
1523 /* Don't count the needed CS space exactly and just use an upper bound.
1525 * Also reserve space for stopping queries at the end of IB, because
1526 * the number of active queries is unlimited in theory.
1528 return 2048 + sctx
->num_cs_dw_queries_suspend
;
1532 si_context_add_resource_size(struct si_context
*sctx
, struct pipe_resource
*r
)
1535 /* Add memory usage for need_gfx_cs_space */
1536 sctx
->vram
+= si_resource(r
)->vram_usage
;
1537 sctx
->gtt
+= si_resource(r
)->gart_usage
;
1542 si_invalidate_draw_sh_constants(struct si_context
*sctx
)
1544 sctx
->last_base_vertex
= SI_BASE_VERTEX_UNKNOWN
;
1545 sctx
->last_instance_count
= SI_INSTANCE_COUNT_UNKNOWN
;
1548 static inline unsigned
1549 si_get_atom_bit(struct si_context
*sctx
, struct si_atom
*atom
)
1551 return 1 << (atom
- sctx
->atoms
.array
);
1555 si_set_atom_dirty(struct si_context
*sctx
, struct si_atom
*atom
, bool dirty
)
1557 unsigned bit
= si_get_atom_bit(sctx
, atom
);
1560 sctx
->dirty_atoms
|= bit
;
1562 sctx
->dirty_atoms
&= ~bit
;
1566 si_is_atom_dirty(struct si_context
*sctx
, struct si_atom
*atom
)
1568 return (sctx
->dirty_atoms
& si_get_atom_bit(sctx
, atom
)) != 0;
1572 si_mark_atom_dirty(struct si_context
*sctx
, struct si_atom
*atom
)
1574 si_set_atom_dirty(sctx
, atom
, true);
1577 static inline struct si_shader_ctx_state
*si_get_vs(struct si_context
*sctx
)
1579 if (sctx
->gs_shader
.cso
)
1580 return &sctx
->gs_shader
;
1581 if (sctx
->tes_shader
.cso
)
1582 return &sctx
->tes_shader
;
1584 return &sctx
->vs_shader
;
1587 static inline struct tgsi_shader_info
*si_get_vs_info(struct si_context
*sctx
)
1589 struct si_shader_ctx_state
*vs
= si_get_vs(sctx
);
1591 return vs
->cso
? &vs
->cso
->info
: NULL
;
1594 static inline struct si_shader
* si_get_vs_state(struct si_context
*sctx
)
1596 if (sctx
->gs_shader
.cso
)
1597 return sctx
->gs_shader
.cso
->gs_copy_shader
;
1599 struct si_shader_ctx_state
*vs
= si_get_vs(sctx
);
1600 return vs
->current
? vs
->current
: NULL
;
1603 static inline bool si_can_dump_shader(struct si_screen
*sscreen
,
1606 return sscreen
->debug_flags
& (1 << processor
);
1609 static inline bool si_get_strmout_en(struct si_context
*sctx
)
1611 return sctx
->streamout
.streamout_enabled
||
1612 sctx
->streamout
.prims_gen_query_enabled
;
1615 static inline unsigned
1616 si_optimal_tcc_alignment(struct si_context
*sctx
, unsigned upload_size
)
1618 unsigned alignment
, tcc_cache_line_size
;
1620 /* If the upload size is less than the cache line size (e.g. 16, 32),
1621 * the whole thing will fit into a cache line if we align it to its size.
1622 * The idea is that multiple small uploads can share a cache line.
1623 * If the upload size is greater, align it to the cache line size.
1625 alignment
= util_next_power_of_two(upload_size
);
1626 tcc_cache_line_size
= sctx
->screen
->info
.tcc_cache_line_size
;
1627 return MIN2(alignment
, tcc_cache_line_size
);
1631 si_saved_cs_reference(struct si_saved_cs
**dst
, struct si_saved_cs
*src
)
1633 if (pipe_reference(&(*dst
)->reference
, &src
->reference
))
1634 si_destroy_saved_cs(*dst
);
1640 si_make_CB_shader_coherent(struct si_context
*sctx
, unsigned num_samples
,
1641 bool shaders_read_metadata
, bool dcc_pipe_aligned
)
1643 sctx
->flags
|= SI_CONTEXT_FLUSH_AND_INV_CB
|
1644 SI_CONTEXT_INV_VCACHE
;
1646 if (sctx
->chip_class
>= GFX9
) {
1647 /* Single-sample color is coherent with shaders on GFX9, but
1648 * L2 metadata must be flushed if shaders read metadata.
1651 if (num_samples
>= 2 ||
1652 (shaders_read_metadata
&& !dcc_pipe_aligned
))
1653 sctx
->flags
|= SI_CONTEXT_INV_L2
;
1654 else if (shaders_read_metadata
)
1655 sctx
->flags
|= SI_CONTEXT_INV_L2_METADATA
;
1658 sctx
->flags
|= SI_CONTEXT_INV_L2
;
1663 si_make_DB_shader_coherent(struct si_context
*sctx
, unsigned num_samples
,
1664 bool include_stencil
, bool shaders_read_metadata
)
1666 sctx
->flags
|= SI_CONTEXT_FLUSH_AND_INV_DB
|
1667 SI_CONTEXT_INV_VCACHE
;
1669 if (sctx
->chip_class
>= GFX9
) {
1670 /* Single-sample depth (not stencil) is coherent with shaders
1671 * on GFX9, but L2 metadata must be flushed if shaders read
1674 if (num_samples
>= 2 || include_stencil
)
1675 sctx
->flags
|= SI_CONTEXT_INV_L2
;
1676 else if (shaders_read_metadata
)
1677 sctx
->flags
|= SI_CONTEXT_INV_L2_METADATA
;
1680 sctx
->flags
|= SI_CONTEXT_INV_L2
;
1685 si_can_sample_zs(struct si_texture
*tex
, bool stencil_sampler
)
1687 return (stencil_sampler
&& tex
->can_sample_s
) ||
1688 (!stencil_sampler
&& tex
->can_sample_z
);
1692 si_htile_enabled(struct si_texture
*tex
, unsigned level
)
1694 return tex
->htile_offset
&& level
== 0;
1698 vi_tc_compat_htile_enabled(struct si_texture
*tex
, unsigned level
)
1700 assert(!tex
->tc_compatible_htile
|| tex
->htile_offset
);
1701 return tex
->tc_compatible_htile
&& level
== 0;
1704 static inline unsigned si_get_ps_iter_samples(struct si_context
*sctx
)
1706 if (sctx
->ps_uses_fbfetch
)
1707 return sctx
->framebuffer
.nr_color_samples
;
1709 return MIN2(sctx
->ps_iter_samples
, sctx
->framebuffer
.nr_color_samples
);
1712 static inline unsigned si_get_total_colormask(struct si_context
*sctx
)
1714 if (sctx
->queued
.named
.rasterizer
->rasterizer_discard
)
1717 struct si_shader_selector
*ps
= sctx
->ps_shader
.cso
;
1721 unsigned colormask
= sctx
->framebuffer
.colorbuf_enabled_4bit
&
1722 sctx
->queued
.named
.blend
->cb_target_mask
;
1724 if (!ps
->info
.properties
[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
])
1725 colormask
&= ps
->colors_written_4bit
;
1726 else if (!ps
->colors_written_4bit
)
1727 colormask
= 0; /* color0 writes all cbufs, but it's not written */
1732 #define UTIL_ALL_PRIM_LINE_MODES ((1 << PIPE_PRIM_LINES) | \
1733 (1 << PIPE_PRIM_LINE_LOOP) | \
1734 (1 << PIPE_PRIM_LINE_STRIP) | \
1735 (1 << PIPE_PRIM_LINES_ADJACENCY) | \
1736 (1 << PIPE_PRIM_LINE_STRIP_ADJACENCY))
1738 static inline bool util_prim_is_lines(unsigned prim
)
1740 return ((1 << prim
) & UTIL_ALL_PRIM_LINE_MODES
) != 0;
1743 static inline bool util_prim_is_points_or_lines(unsigned prim
)
1745 return ((1 << prim
) & (UTIL_ALL_PRIM_LINE_MODES
|
1746 (1 << PIPE_PRIM_POINTS
))) != 0;
1750 * Return true if there is enough memory in VRAM and GTT for the buffers
1753 * \param vram VRAM memory size not added to the buffer list yet
1754 * \param gtt GTT memory size not added to the buffer list yet
1757 radeon_cs_memory_below_limit(struct si_screen
*screen
,
1758 struct radeon_cmdbuf
*cs
,
1759 uint64_t vram
, uint64_t gtt
)
1761 vram
+= cs
->used_vram
;
1762 gtt
+= cs
->used_gart
;
1764 /* Anything that goes above the VRAM size should go to GTT. */
1765 if (vram
> screen
->info
.vram_size
)
1766 gtt
+= vram
- screen
->info
.vram_size
;
1768 /* Now we just need to check if we have enough GTT. */
1769 return gtt
< screen
->info
.gart_size
* 0.7;
1773 * Add a buffer to the buffer list for the given command stream (CS).
1775 * All buffers used by a CS must be added to the list. This tells the kernel
1776 * driver which buffers are used by GPU commands. Other buffers can
1777 * be swapped out (not accessible) during execution.
1779 * The buffer list becomes empty after every context flush and must be
1782 static inline void radeon_add_to_buffer_list(struct si_context
*sctx
,
1783 struct radeon_cmdbuf
*cs
,
1784 struct si_resource
*bo
,
1785 enum radeon_bo_usage usage
,
1786 enum radeon_bo_priority priority
)
1789 sctx
->ws
->cs_add_buffer(
1791 (enum radeon_bo_usage
)(usage
| RADEON_USAGE_SYNCHRONIZED
),
1792 bo
->domains
, priority
);
1796 * Same as above, but also checks memory usage and flushes the context
1799 * When this SHOULD NOT be used:
1801 * - if si_context_add_resource_size has been called for the buffer
1802 * followed by *_need_cs_space for checking the memory usage
1804 * - if si_need_dma_space has been called for the buffer
1806 * - when emitting state packets and draw packets (because preceding packets
1807 * can't be re-emitted at that point)
1809 * - if shader resource "enabled_mask" is not up-to-date or there is
1810 * a different constraint disallowing a context flush
1813 radeon_add_to_gfx_buffer_list_check_mem(struct si_context
*sctx
,
1814 struct si_resource
*bo
,
1815 enum radeon_bo_usage usage
,
1816 enum radeon_bo_priority priority
,
1820 !radeon_cs_memory_below_limit(sctx
->screen
, sctx
->gfx_cs
,
1821 sctx
->vram
+ bo
->vram_usage
,
1822 sctx
->gtt
+ bo
->gart_usage
))
1823 si_flush_gfx_cs(sctx
, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW
, NULL
);
1825 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
, bo
, usage
, priority
);
1828 static inline bool si_compute_prim_discard_enabled(struct si_context
*sctx
)
1830 return sctx
->prim_discard_vertex_count_threshold
!= UINT_MAX
;
1833 #define PRINT_ERR(fmt, args...) \
1834 fprintf(stderr, "EE %s:%d %s - " fmt, __FILE__, __LINE__, __func__, ##args)