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 void (*make_texture_descriptor
)(
461 struct si_screen
*screen
,
462 struct si_texture
*tex
,
464 enum pipe_texture_target target
,
465 enum pipe_format pipe_format
,
466 const unsigned char state_swizzle
[4],
467 unsigned first_level
, unsigned last_level
,
468 unsigned first_layer
, unsigned last_layer
,
469 unsigned width
, unsigned height
, unsigned depth
,
471 uint32_t *fmask_state
);
473 unsigned pa_sc_raster_config
;
474 unsigned pa_sc_raster_config_1
;
475 unsigned se_tile_repeat
;
476 unsigned gs_table_depth
;
477 unsigned tess_offchip_block_dw_size
;
478 unsigned tess_offchip_ring_size
;
479 unsigned tess_factor_ring_size
;
480 unsigned vgt_hs_offchip_param
;
481 unsigned eqaa_force_coverage_samples
;
482 unsigned eqaa_force_z_samples
;
483 unsigned eqaa_force_color_samples
;
484 bool has_clear_state
;
485 bool has_distributed_tess
;
486 bool has_draw_indirect_multi
;
487 bool has_out_of_order_rast
;
488 bool assume_no_z_fights
;
489 bool commutative_blend_add
;
490 bool has_gfx9_scissor_bug
;
491 bool has_msaa_sample_loc_bug
;
492 bool has_ls_vgpr_init_bug
;
493 bool has_dcc_constant_encode
;
496 bool llvm_has_working_vgpr_indexing
;
499 #define OPT_BOOL(name, dflt, description) bool name:1;
500 #include "si_debug_options.h"
503 /* Whether shaders are monolithic (1-part) or separate (3-part). */
504 bool use_monolithic_shaders
;
506 bool has_rbplus
; /* if RB+ registers exist */
507 bool rbplus_allowed
; /* if RB+ is allowed */
508 bool dcc_msaa_allowed
;
509 bool cpdma_prefetch_writes_memory
;
511 struct slab_parent_pool pool_transfers
;
513 /* Texture filter settings. */
514 int force_aniso
; /* -1 = disabled */
516 /* Auxiliary context. Mainly used to initialize resources.
517 * It must be locked prior to using and flushed before unlocking. */
518 struct pipe_context
*aux_context
;
519 mtx_t aux_context_lock
;
521 /* This must be in the screen, because UE4 uses one context for
522 * compilation and another one for rendering.
524 unsigned num_compilations
;
525 /* Along with ST_DEBUG=precompile, this should show if applications
526 * are loading shaders on demand. This is a monotonic counter.
528 unsigned num_shaders_created
;
529 unsigned num_shader_cache_hits
;
531 /* GPU load thread. */
532 mtx_t gpu_load_mutex
;
533 thrd_t gpu_load_thread
;
534 union si_mmio_counters mmio_counters
;
535 volatile unsigned gpu_load_stop_thread
; /* bool */
537 /* Performance counters. */
538 struct si_perfcounters
*perfcounters
;
540 /* If pipe_screen wants to recompute and re-emit the framebuffer,
541 * sampler, and image states of all contexts, it should atomically
544 * Each context will compare this with its own last known value of
545 * the counter before drawing and re-emit the states accordingly.
547 unsigned dirty_tex_counter
;
548 unsigned dirty_buf_counter
;
550 /* Atomically increment this counter when an existing texture's
551 * metadata is enabled or disabled in a way that requires changing
552 * contexts' compressed texture binding masks.
554 unsigned compressed_colortex_counter
;
557 /* Context flags to set so that all writes from earlier jobs
558 * in the CP are seen by L2 clients.
562 /* Context flags to set so that all writes from earlier jobs
563 * that end in L2 are seen by CP.
568 mtx_t shader_parts_mutex
;
569 struct si_shader_part
*vs_prologs
;
570 struct si_shader_part
*tcs_epilogs
;
571 struct si_shader_part
*gs_prologs
;
572 struct si_shader_part
*ps_prologs
;
573 struct si_shader_part
*ps_epilogs
;
575 /* Shader cache in memory.
577 * Design & limitations:
578 * - The shader cache is per screen (= per process), never saved to
579 * disk, and skips redundant shader compilations from TGSI to bytecode.
580 * - It can only be used with one-variant-per-shader support, in which
581 * case only the main (typically middle) part of shaders is cached.
582 * - Only VS, TCS, TES, PS are cached, out of which only the hw VS
583 * variants of VS and TES are cached, so LS and ES aren't.
584 * - GS and CS aren't cached, but it's certainly possible to cache
587 mtx_t shader_cache_mutex
;
588 struct hash_table
*shader_cache
;
590 /* Shader compiler queue for multithreaded compilation. */
591 struct util_queue shader_compiler_queue
;
592 /* Use at most 3 normal compiler threads on quadcore and better.
593 * Hyperthreaded CPUs report the number of threads, but we want
594 * the number of cores. We only need this many threads for shader-db. */
595 struct ac_llvm_compiler compiler
[24]; /* used by the queue only */
597 struct util_queue shader_compiler_queue_low_priority
;
598 /* Use at most 2 low priority threads on quadcore and better.
599 * We want to minimize the impact on multithreaded Mesa. */
600 struct ac_llvm_compiler compiler_lowp
[10];
603 struct si_blend_color
{
604 struct pipe_blend_color state
;
608 struct si_sampler_view
{
609 struct pipe_sampler_view base
;
610 /* [0..7] = image descriptor
611 * [4..7] = buffer descriptor */
613 uint32_t fmask_state
[8];
614 const struct legacy_surf_level
*base_level_info
;
617 bool is_stencil_sampler
;
619 bool dcc_incompatible
;
622 #define SI_SAMPLER_STATE_MAGIC 0x34f1c35a
624 struct si_sampler_state
{
629 uint32_t integer_val
[4];
630 uint32_t upgraded_depth_val
[4];
633 struct si_cs_shader_state
{
634 struct si_compute
*program
;
635 struct si_compute
*emitted_program
;
642 struct pipe_sampler_view
*views
[SI_NUM_SAMPLERS
];
643 struct si_sampler_state
*sampler_states
[SI_NUM_SAMPLERS
];
645 /* The i-th bit is set if that element is enabled (non-NULL resource). */
646 unsigned enabled_mask
;
647 uint32_t needs_depth_decompress_mask
;
648 uint32_t needs_color_decompress_mask
;
652 struct pipe_image_view views
[SI_NUM_IMAGES
];
653 uint32_t needs_color_decompress_mask
;
654 unsigned enabled_mask
;
657 struct si_framebuffer
{
658 struct pipe_framebuffer_state state
;
659 unsigned colorbuf_enabled_4bit
;
660 unsigned spi_shader_col_format
;
661 unsigned spi_shader_col_format_alpha
;
662 unsigned spi_shader_col_format_blend
;
663 unsigned spi_shader_col_format_blend_alpha
;
664 ubyte nr_samples
:5; /* at most 16xAA */
665 ubyte log_samples
:3; /* at most 4 = 16xAA */
666 ubyte nr_color_samples
; /* at most 8xAA */
667 ubyte compressed_cb_mask
;
668 ubyte uncompressed_cb_mask
;
670 ubyte color_is_int10
;
672 ubyte dcc_overwrite_combiner_watermark
;
675 bool CB_has_shader_readable_metadata
;
676 bool DB_has_shader_readable_metadata
;
677 bool all_DCC_pipe_aligned
;
681 /* This is the list we want to support. */
682 SI_QUANT_MODE_16_8_FIXED_POINT_1_256TH
,
683 SI_QUANT_MODE_14_10_FIXED_POINT_1_1024TH
,
684 SI_QUANT_MODE_12_12_FIXED_POINT_1_4096TH
,
687 struct si_signed_scissor
{
692 enum si_quant_mode quant_mode
;
695 struct si_viewports
{
696 struct pipe_viewport_state states
[SI_MAX_VIEWPORTS
];
697 struct si_signed_scissor as_scissor
[SI_MAX_VIEWPORTS
];
701 struct si_clip_state
{
702 struct pipe_clip_state state
;
706 struct si_streamout_target
{
707 struct pipe_stream_output_target b
;
709 /* The buffer where BUFFER_FILLED_SIZE is stored. */
710 struct si_resource
*buf_filled_size
;
711 unsigned buf_filled_size_offset
;
712 bool buf_filled_size_valid
;
714 unsigned stride_in_dw
;
717 struct si_streamout
{
720 unsigned enabled_mask
;
721 unsigned num_targets
;
722 struct si_streamout_target
*targets
[PIPE_MAX_SO_BUFFERS
];
724 unsigned append_bitmask
;
727 /* External state which comes from the vertex shader,
728 * it must be set explicitly when binding a shader. */
729 uint16_t *stride_in_dw
;
730 unsigned enabled_stream_buffers_mask
; /* stream0 buffers0-3 in 4 LSB */
732 /* The state of VGT_STRMOUT_BUFFER_(CONFIG|EN). */
733 unsigned hw_enabled_mask
;
735 /* The state of VGT_STRMOUT_(CONFIG|EN). */
736 bool streamout_enabled
;
737 bool prims_gen_query_enabled
;
738 int num_prims_gen_queries
;
741 /* A shader state consists of the shader selector, which is a constant state
742 * object shared by multiple contexts and shouldn't be modified, and
743 * the current shader variant selected for this context.
745 struct si_shader_ctx_state
{
746 struct si_shader_selector
*cso
;
747 struct si_shader
*current
;
750 #define SI_NUM_VGT_PARAM_KEY_BITS 12
751 #define SI_NUM_VGT_PARAM_STATES (1 << SI_NUM_VGT_PARAM_KEY_BITS)
753 /* The IA_MULTI_VGT_PARAM key used to index the table of precomputed values.
754 * Some fields are set by state-change calls, most are set by draw_vbo.
756 union si_vgt_param_key
{
758 #ifdef PIPE_ARCH_LITTLE_ENDIAN
760 unsigned uses_instancing
:1;
761 unsigned multi_instances_smaller_than_primgroup
:1;
762 unsigned primitive_restart
:1;
763 unsigned count_from_stream_output
:1;
764 unsigned line_stipple_enabled
:1;
765 unsigned uses_tess
:1;
766 unsigned tess_uses_prim_id
:1;
768 unsigned _pad
:32 - SI_NUM_VGT_PARAM_KEY_BITS
;
769 #else /* PIPE_ARCH_BIG_ENDIAN */
770 unsigned _pad
:32 - SI_NUM_VGT_PARAM_KEY_BITS
;
772 unsigned tess_uses_prim_id
:1;
773 unsigned uses_tess
:1;
774 unsigned line_stipple_enabled
:1;
775 unsigned count_from_stream_output
:1;
776 unsigned primitive_restart
:1;
777 unsigned multi_instances_smaller_than_primgroup
:1;
778 unsigned uses_instancing
:1;
785 #define SI_NUM_VGT_STAGES_KEY_BITS 2
786 #define SI_NUM_VGT_STAGES_STATES (1 << SI_NUM_VGT_STAGES_KEY_BITS)
788 /* The VGT_SHADER_STAGES key used to index the table of precomputed values.
789 * Some fields are set by state-change calls, most are set by draw_vbo.
791 union si_vgt_stages_key
{
793 #ifdef PIPE_ARCH_LITTLE_ENDIAN
796 unsigned _pad
:32 - SI_NUM_VGT_STAGES_KEY_BITS
;
797 #else /* PIPE_ARCH_BIG_ENDIAN */
798 unsigned _pad
:32 - SI_NUM_VGT_STAGES_KEY_BITS
;
806 struct si_texture_handle
810 struct pipe_sampler_view
*view
;
811 struct si_sampler_state sstate
;
814 struct si_image_handle
818 struct pipe_image_view view
;
822 struct pipe_reference reference
;
823 struct si_context
*ctx
;
824 struct radeon_saved_cs gfx
;
825 struct radeon_saved_cs compute
;
826 struct si_resource
*trace_buf
;
829 unsigned gfx_last_dw
;
830 unsigned compute_last_dw
;
835 struct si_sdma_upload
{
836 struct si_resource
*dst
;
837 struct si_resource
*src
;
844 struct pipe_context b
; /* base class */
846 enum radeon_family family
;
847 enum chip_class chip_class
;
849 struct radeon_winsys
*ws
;
850 struct radeon_winsys_ctx
*ctx
;
851 struct radeon_cmdbuf
*gfx_cs
; /* compute IB if graphics is disabled */
852 struct radeon_cmdbuf
*dma_cs
;
853 struct pipe_fence_handle
*last_gfx_fence
;
854 struct pipe_fence_handle
*last_sdma_fence
;
855 struct si_resource
*eop_bug_scratch
;
856 struct u_upload_mgr
*cached_gtt_allocator
;
857 struct threaded_context
*tc
;
858 struct u_suballocator
*allocator_zeroed_memory
;
859 struct slab_child_pool pool_transfers
;
860 struct slab_child_pool pool_transfers_unsync
; /* for threaded_context */
861 struct pipe_device_reset_callback device_reset_callback
;
862 struct u_log_context
*log
;
863 void *query_result_shader
;
864 struct blitter_context
*blitter
;
865 void *custom_dsa_flush
;
866 void *custom_blend_resolve
;
867 void *custom_blend_fmask_decompress
;
868 void *custom_blend_eliminate_fastclear
;
869 void *custom_blend_dcc_decompress
;
871 void *vs_blit_pos_layered
;
873 void *vs_blit_color_layered
;
874 void *vs_blit_texcoord
;
875 void *cs_clear_buffer
;
876 void *cs_copy_buffer
;
878 void *cs_copy_image_1d_array
;
879 void *cs_clear_render_target
;
880 void *cs_clear_render_target_1d_array
;
882 struct si_screen
*screen
;
883 struct pipe_debug_callback debug
;
884 struct ac_llvm_compiler compiler
; /* only non-threaded compilation */
885 struct si_shader_ctx_state fixed_func_tcs_shader
;
886 /* Offset 0: EOP flush number; Offset 4: GDS prim restart counter */
887 struct si_resource
*wait_mem_scratch
;
888 unsigned wait_mem_number
;
889 uint16_t prefetch_L2_mask
;
892 bool gfx_flush_in_progress
:1;
893 bool gfx_last_ib_is_busy
:1;
894 bool compute_is_busy
:1;
896 unsigned num_gfx_cs_flushes
;
897 unsigned initial_gfx_cs_size
;
898 unsigned last_dirty_tex_counter
;
899 unsigned last_dirty_buf_counter
;
900 unsigned last_compressed_colortex_counter
;
901 unsigned last_num_draw_calls
;
902 unsigned flags
; /* flush flags */
903 /* Current unaccounted memory usage. */
907 /* Compute-based primitive discard. */
908 unsigned prim_discard_vertex_count_threshold
;
909 struct pb_buffer
*gds
;
910 struct pb_buffer
*gds_oa
;
911 struct radeon_cmdbuf
*prim_discard_compute_cs
;
912 unsigned compute_gds_offset
;
913 struct si_shader
*compute_ib_last_shader
;
914 uint32_t compute_rewind_va
;
915 unsigned compute_num_prims_in_batch
;
916 bool preserve_prim_restart_gds_at_flush
;
917 /* index_ring is divided into 2 halves for doublebuffering. */
918 struct si_resource
*index_ring
;
919 unsigned index_ring_base
; /* offset of a per-IB portion */
920 unsigned index_ring_offset
; /* offset within a per-IB portion */
921 unsigned index_ring_size_per_ib
; /* max available size per IB */
922 bool prim_discard_compute_ib_initialized
;
923 /* For tracking the last execution barrier - it can be either
924 * a WRITE_DATA packet or a fence. */
925 uint32_t *last_pkt3_write_data
;
926 struct si_resource
*barrier_buf
;
927 unsigned barrier_buf_offset
;
928 struct pipe_fence_handle
*last_ib_barrier_fence
;
929 struct si_resource
*last_ib_barrier_buf
;
930 unsigned last_ib_barrier_buf_offset
;
932 /* Atoms (direct states). */
933 union si_state_atoms atoms
;
934 unsigned dirty_atoms
; /* mask */
935 /* PM4 states (precomputed immutable states) */
936 unsigned dirty_states
;
937 union si_state queued
;
938 union si_state emitted
;
940 /* Atom declarations. */
941 struct si_framebuffer framebuffer
;
942 unsigned sample_locs_num_samples
;
943 uint16_t sample_mask
;
944 unsigned last_cb_target_mask
;
945 struct si_blend_color blend_color
;
946 struct si_clip_state clip_state
;
947 struct si_shader_data shader_pointers
;
948 struct si_stencil_ref stencil_ref
;
949 struct pipe_scissor_state scissors
[SI_MAX_VIEWPORTS
];
950 struct si_streamout streamout
;
951 struct si_viewports viewports
;
952 unsigned num_window_rectangles
;
953 bool window_rectangles_include
;
954 struct pipe_scissor_state window_rectangles
[4];
956 /* Precomputed states. */
957 struct si_pm4_state
*init_config
;
958 struct si_pm4_state
*init_config_gs_rings
;
959 bool init_config_has_vgt_flush
;
960 struct si_pm4_state
*vgt_shader_config
[SI_NUM_VGT_STAGES_STATES
];
963 struct si_shader_ctx_state ps_shader
;
964 struct si_shader_ctx_state gs_shader
;
965 struct si_shader_ctx_state vs_shader
;
966 struct si_shader_ctx_state tcs_shader
;
967 struct si_shader_ctx_state tes_shader
;
968 struct si_shader_ctx_state cs_prim_discard_state
;
969 struct si_cs_shader_state cs_shader_state
;
971 /* shader information */
972 struct si_vertex_elements
*vertex_elements
;
973 unsigned sprite_coord_enable
;
974 unsigned cs_max_waves_per_sh
;
976 bool do_update_shaders
;
978 /* vertex buffer descriptors */
979 uint32_t *vb_descriptors_gpu_list
;
980 struct si_resource
*vb_descriptors_buffer
;
981 unsigned vb_descriptors_offset
;
983 /* shader descriptors */
984 struct si_descriptors descriptors
[SI_NUM_DESCS
];
985 unsigned descriptors_dirty
;
986 unsigned shader_pointers_dirty
;
987 unsigned shader_needs_decompress_mask
;
988 struct si_buffer_resources rw_buffers
;
989 struct si_buffer_resources const_and_shader_buffers
[SI_NUM_SHADERS
];
990 struct si_samplers samplers
[SI_NUM_SHADERS
];
991 struct si_images images
[SI_NUM_SHADERS
];
992 bool bo_list_add_all_resident_resources
;
993 bool bo_list_add_all_gfx_resources
;
994 bool bo_list_add_all_compute_resources
;
996 /* other shader resources */
997 struct pipe_constant_buffer null_const_buf
; /* used for set_constant_buffer(NULL) on GFX7 */
998 struct pipe_resource
*esgs_ring
;
999 struct pipe_resource
*gsvs_ring
;
1000 struct pipe_resource
*tess_rings
;
1001 union pipe_color_union
*border_color_table
; /* in CPU memory, any endian */
1002 struct si_resource
*border_color_buffer
;
1003 union pipe_color_union
*border_color_map
; /* in VRAM (slow access), little endian */
1004 unsigned border_color_count
;
1005 unsigned num_vs_blit_sgprs
;
1006 uint32_t vs_blit_sh_data
[SI_VS_BLIT_SGPRS_POS_TEXCOORD
];
1007 uint32_t cs_user_data
[4];
1009 /* Vertex and index buffers. */
1010 bool vertex_buffers_dirty
;
1011 bool vertex_buffer_pointer_dirty
;
1012 struct pipe_vertex_buffer vertex_buffer
[SI_NUM_VERTEX_BUFFERS
];
1013 uint16_t vertex_buffer_unaligned
; /* bitmask of not dword-aligned buffers */
1015 /* MSAA config state. */
1016 int ps_iter_samples
;
1017 bool ps_uses_fbfetch
;
1018 bool smoothing_enabled
;
1020 /* DB render state. */
1021 unsigned ps_db_shader_control
;
1022 unsigned dbcb_copy_sample
;
1023 bool dbcb_depth_copy_enabled
:1;
1024 bool dbcb_stencil_copy_enabled
:1;
1025 bool db_flush_depth_inplace
:1;
1026 bool db_flush_stencil_inplace
:1;
1027 bool db_depth_clear
:1;
1028 bool db_depth_disable_expclear
:1;
1029 bool db_stencil_clear
:1;
1030 bool db_stencil_disable_expclear
:1;
1031 bool occlusion_queries_disabled
:1;
1032 bool generate_mipmap_for_depth
:1;
1034 /* Emitted draw state. */
1035 bool gs_tri_strip_adj_fix
:1;
1037 bool prim_discard_cs_instancing
:1;
1038 int last_index_size
;
1039 int last_base_vertex
;
1040 int last_start_instance
;
1041 int last_instance_count
;
1043 int last_sh_base_reg
;
1044 int last_primitive_restart_en
;
1045 int last_restart_index
;
1047 int last_multi_vgt_param
;
1049 unsigned last_sc_line_stipple
;
1050 unsigned current_vs_state
;
1051 unsigned last_vs_state
;
1052 enum pipe_prim_type current_rast_prim
; /* primitive type after TES, GS */
1054 /* Scratch buffer */
1055 struct si_resource
*scratch_buffer
;
1056 unsigned scratch_waves
;
1057 unsigned spi_tmpring_size
;
1059 struct si_resource
*compute_scratch_buffer
;
1061 /* Emitted derived tessellation state. */
1062 /* Local shader (VS), or HS if LS-HS are merged. */
1063 struct si_shader
*last_ls
;
1064 struct si_shader_selector
*last_tcs
;
1065 int last_num_tcs_input_cp
;
1066 int last_tes_sh_base
;
1067 bool last_tess_uses_primid
;
1068 unsigned last_num_patches
;
1069 int last_ls_hs_config
;
1073 struct si_saved_cs
*current_saved_cs
;
1074 uint64_t dmesg_timestamp
;
1075 unsigned apitrace_call_number
;
1078 bool need_check_render_feedback
;
1079 bool decompression_enabled
;
1080 bool dpbb_force_off
;
1081 bool vs_writes_viewport_index
;
1082 bool vs_disables_clipping_viewport
;
1084 /* Precomputed IA_MULTI_VGT_PARAM */
1085 union si_vgt_param_key ia_multi_vgt_param_key
;
1086 unsigned ia_multi_vgt_param
[SI_NUM_VGT_PARAM_STATES
];
1088 /* Bindless descriptors. */
1089 struct si_descriptors bindless_descriptors
;
1090 struct util_idalloc bindless_used_slots
;
1091 unsigned num_bindless_descriptors
;
1092 bool bindless_descriptors_dirty
;
1093 bool graphics_bindless_pointer_dirty
;
1094 bool compute_bindless_pointer_dirty
;
1096 /* Allocated bindless handles */
1097 struct hash_table
*tex_handles
;
1098 struct hash_table
*img_handles
;
1100 /* Resident bindless handles */
1101 struct util_dynarray resident_tex_handles
;
1102 struct util_dynarray resident_img_handles
;
1104 /* Resident bindless handles which need decompression */
1105 struct util_dynarray resident_tex_needs_color_decompress
;
1106 struct util_dynarray resident_img_needs_color_decompress
;
1107 struct util_dynarray resident_tex_needs_depth_decompress
;
1109 /* Bindless state */
1110 bool uses_bindless_samplers
;
1111 bool uses_bindless_images
;
1113 /* MSAA sample locations.
1114 * The first index is the sample index.
1115 * The second index is the coordinate: X, Y. */
1123 struct pipe_resource
*sample_pos_buffer
;
1126 unsigned num_draw_calls
;
1127 unsigned num_decompress_calls
;
1128 unsigned num_mrt_draw_calls
;
1129 unsigned num_prim_restart_calls
;
1130 unsigned num_spill_draw_calls
;
1131 unsigned num_compute_calls
;
1132 unsigned num_spill_compute_calls
;
1133 unsigned num_dma_calls
;
1134 unsigned num_cp_dma_calls
;
1135 unsigned num_vs_flushes
;
1136 unsigned num_ps_flushes
;
1137 unsigned num_cs_flushes
;
1138 unsigned num_cb_cache_flushes
;
1139 unsigned num_db_cache_flushes
;
1140 unsigned num_L2_invalidates
;
1141 unsigned num_L2_writebacks
;
1142 unsigned num_resident_handles
;
1143 uint64_t num_alloc_tex_transfer_bytes
;
1144 unsigned last_tex_ps_draw_ratio
; /* for query */
1145 unsigned compute_num_verts_accepted
;
1146 unsigned compute_num_verts_rejected
;
1147 unsigned compute_num_verts_ineligible
; /* due to low vertex count */
1148 unsigned context_roll
;
1151 /* Maintain the list of active queries for pausing between IBs. */
1152 int num_occlusion_queries
;
1153 int num_perfect_occlusion_queries
;
1154 int num_pipeline_stat_queries
;
1155 struct list_head active_queries
;
1156 unsigned num_cs_dw_queries_suspend
;
1158 /* Render condition. */
1159 struct pipe_query
*render_cond
;
1160 unsigned render_cond_mode
;
1161 bool render_cond_invert
;
1162 bool render_cond_force_off
; /* for u_blitter */
1164 /* For uploading data via GTT and copy to VRAM on context flush via SDMA. */
1165 bool sdma_uploads_in_progress
;
1166 struct si_sdma_upload
*sdma_uploads
;
1167 unsigned num_sdma_uploads
;
1168 unsigned max_sdma_uploads
;
1170 /* Statistics gathering for the DCC enablement heuristic. It can't be
1171 * in si_texture because si_texture can be shared by multiple
1172 * contexts. This is for back buffers only. We shouldn't get too many
1175 * X11 DRI3 rotates among a finite set of back buffers. They should
1176 * all fit in this array. If they don't, separate DCC might never be
1177 * enabled by DCC stat gathering.
1180 struct si_texture
*tex
;
1181 /* Query queue: 0 = usually active, 1 = waiting, 2 = readback. */
1182 struct pipe_query
*ps_stats
[3];
1183 /* If all slots are used and another slot is needed,
1184 * the least recently used slot is evicted based on this. */
1185 int64_t last_use_timestamp
;
1189 /* Copy one resource to another using async DMA. */
1190 void (*dma_copy
)(struct pipe_context
*ctx
,
1191 struct pipe_resource
*dst
,
1193 unsigned dst_x
, unsigned dst_y
, unsigned dst_z
,
1194 struct pipe_resource
*src
,
1196 const struct pipe_box
*src_box
);
1198 struct si_tracked_regs tracked_regs
;
1202 void cik_init_sdma_functions(struct si_context
*sctx
);
1205 enum si_blitter_op
/* bitmask */
1207 SI_SAVE_TEXTURES
= 1,
1208 SI_SAVE_FRAMEBUFFER
= 2,
1209 SI_SAVE_FRAGMENT_STATE
= 4,
1210 SI_DISABLE_RENDER_COND
= 8,
1213 void si_blitter_begin(struct si_context
*sctx
, enum si_blitter_op op
);
1214 void si_blitter_end(struct si_context
*sctx
);
1215 void si_init_blit_functions(struct si_context
*sctx
);
1216 void si_decompress_textures(struct si_context
*sctx
, unsigned shader_mask
);
1217 void si_resource_copy_region(struct pipe_context
*ctx
,
1218 struct pipe_resource
*dst
,
1220 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1221 struct pipe_resource
*src
,
1223 const struct pipe_box
*src_box
);
1224 void si_decompress_dcc(struct si_context
*sctx
, struct si_texture
*tex
);
1227 bool si_rings_is_buffer_referenced(struct si_context
*sctx
,
1228 struct pb_buffer
*buf
,
1229 enum radeon_bo_usage usage
);
1230 void *si_buffer_map_sync_with_rings(struct si_context
*sctx
,
1231 struct si_resource
*resource
,
1233 void si_init_resource_fields(struct si_screen
*sscreen
,
1234 struct si_resource
*res
,
1235 uint64_t size
, unsigned alignment
);
1236 bool si_alloc_resource(struct si_screen
*sscreen
,
1237 struct si_resource
*res
);
1238 struct pipe_resource
*pipe_aligned_buffer_create(struct pipe_screen
*screen
,
1239 unsigned flags
, unsigned usage
,
1240 unsigned size
, unsigned alignment
);
1241 struct si_resource
*si_aligned_buffer_create(struct pipe_screen
*screen
,
1242 unsigned flags
, unsigned usage
,
1243 unsigned size
, unsigned alignment
);
1244 void si_replace_buffer_storage(struct pipe_context
*ctx
,
1245 struct pipe_resource
*dst
,
1246 struct pipe_resource
*src
);
1247 void si_init_screen_buffer_functions(struct si_screen
*sscreen
);
1248 void si_init_buffer_functions(struct si_context
*sctx
);
1251 enum pipe_format
si_simplify_cb_format(enum pipe_format format
);
1252 bool vi_alpha_is_on_msb(enum pipe_format format
);
1253 void vi_dcc_clear_level(struct si_context
*sctx
,
1254 struct si_texture
*tex
,
1255 unsigned level
, unsigned clear_value
);
1256 void si_init_clear_functions(struct si_context
*sctx
);
1258 /* si_compute_blit.c */
1259 unsigned si_get_flush_flags(struct si_context
*sctx
, enum si_coherency coher
,
1260 enum si_cache_policy cache_policy
);
1261 void si_clear_buffer(struct si_context
*sctx
, struct pipe_resource
*dst
,
1262 uint64_t offset
, uint64_t size
, uint32_t *clear_value
,
1263 uint32_t clear_value_size
, enum si_coherency coher
,
1265 void si_copy_buffer(struct si_context
*sctx
,
1266 struct pipe_resource
*dst
, struct pipe_resource
*src
,
1267 uint64_t dst_offset
, uint64_t src_offset
, unsigned size
);
1268 void si_compute_copy_image(struct si_context
*sctx
,
1269 struct pipe_resource
*dst
,
1271 struct pipe_resource
*src
,
1273 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1274 const struct pipe_box
*src_box
);
1275 void si_compute_clear_render_target(struct pipe_context
*ctx
,
1276 struct pipe_surface
*dstsurf
,
1277 const union pipe_color_union
*color
,
1278 unsigned dstx
, unsigned dsty
,
1279 unsigned width
, unsigned height
,
1280 bool render_condition_enabled
);
1281 void si_retile_dcc(struct si_context
*sctx
, struct si_texture
*tex
);
1282 void si_init_compute_blit_functions(struct si_context
*sctx
);
1285 #define SI_CPDMA_SKIP_CHECK_CS_SPACE (1 << 0) /* don't call need_cs_space */
1286 #define SI_CPDMA_SKIP_SYNC_AFTER (1 << 1) /* don't wait for DMA after the copy */
1287 #define SI_CPDMA_SKIP_SYNC_BEFORE (1 << 2) /* don't wait for DMA before the copy (RAW hazards) */
1288 #define SI_CPDMA_SKIP_GFX_SYNC (1 << 3) /* don't flush caches and don't wait for PS/CS */
1289 #define SI_CPDMA_SKIP_BO_LIST_UPDATE (1 << 4) /* don't update the BO list */
1290 #define SI_CPDMA_SKIP_ALL (SI_CPDMA_SKIP_CHECK_CS_SPACE | \
1291 SI_CPDMA_SKIP_SYNC_AFTER | \
1292 SI_CPDMA_SKIP_SYNC_BEFORE | \
1293 SI_CPDMA_SKIP_GFX_SYNC | \
1294 SI_CPDMA_SKIP_BO_LIST_UPDATE)
1296 void si_cp_dma_wait_for_idle(struct si_context
*sctx
);
1297 void si_cp_dma_clear_buffer(struct si_context
*sctx
, struct radeon_cmdbuf
*cs
,
1298 struct pipe_resource
*dst
, uint64_t offset
,
1299 uint64_t size
, unsigned value
, unsigned user_flags
,
1300 enum si_coherency coher
, enum si_cache_policy cache_policy
);
1301 void si_cp_dma_copy_buffer(struct si_context
*sctx
,
1302 struct pipe_resource
*dst
, struct pipe_resource
*src
,
1303 uint64_t dst_offset
, uint64_t src_offset
, unsigned size
,
1304 unsigned user_flags
, enum si_coherency coher
,
1305 enum si_cache_policy cache_policy
);
1306 void cik_prefetch_TC_L2_async(struct si_context
*sctx
, struct pipe_resource
*buf
,
1307 uint64_t offset
, unsigned size
);
1308 void cik_emit_prefetch_L2(struct si_context
*sctx
, bool vertex_stage_only
);
1309 void si_test_gds(struct si_context
*sctx
);
1310 void si_cp_write_data(struct si_context
*sctx
, struct si_resource
*buf
,
1311 unsigned offset
, unsigned size
, unsigned dst_sel
,
1312 unsigned engine
, const void *data
);
1313 void si_cp_copy_data(struct si_context
*sctx
, struct radeon_cmdbuf
*cs
,
1314 unsigned dst_sel
, struct si_resource
*dst
, unsigned dst_offset
,
1315 unsigned src_sel
, struct si_resource
*src
, unsigned src_offset
);
1318 void si_save_cs(struct radeon_winsys
*ws
, struct radeon_cmdbuf
*cs
,
1319 struct radeon_saved_cs
*saved
, bool get_buffer_list
);
1320 void si_clear_saved_cs(struct radeon_saved_cs
*saved
);
1321 void si_destroy_saved_cs(struct si_saved_cs
*scs
);
1322 void si_auto_log_cs(void *data
, struct u_log_context
*log
);
1323 void si_log_hw_flush(struct si_context
*sctx
);
1324 void si_log_draw_state(struct si_context
*sctx
, struct u_log_context
*log
);
1325 void si_log_compute_state(struct si_context
*sctx
, struct u_log_context
*log
);
1326 void si_init_debug_functions(struct si_context
*sctx
);
1327 void si_check_vm_faults(struct si_context
*sctx
,
1328 struct radeon_saved_cs
*saved
, enum ring_type ring
);
1329 bool si_replace_shader(unsigned num
, struct si_shader_binary
*binary
);
1332 void si_init_dma_functions(struct si_context
*sctx
);
1335 void si_dma_emit_timestamp(struct si_context
*sctx
, struct si_resource
*dst
,
1337 void si_sdma_clear_buffer(struct si_context
*sctx
, struct pipe_resource
*dst
,
1338 uint64_t offset
, uint64_t size
, unsigned clear_value
);
1339 void si_need_dma_space(struct si_context
*ctx
, unsigned num_dw
,
1340 struct si_resource
*dst
, struct si_resource
*src
);
1341 void si_flush_dma_cs(struct si_context
*ctx
, unsigned flags
,
1342 struct pipe_fence_handle
**fence
);
1343 void si_screen_clear_buffer(struct si_screen
*sscreen
, struct pipe_resource
*dst
,
1344 uint64_t offset
, uint64_t size
, unsigned value
);
1347 void si_cp_release_mem(struct si_context
*ctx
, struct radeon_cmdbuf
*cs
,
1348 unsigned event
, unsigned event_flags
,
1349 unsigned dst_sel
, unsigned int_sel
, unsigned data_sel
,
1350 struct si_resource
*buf
, uint64_t va
,
1351 uint32_t new_fence
, unsigned query_type
);
1352 unsigned si_cp_write_fence_dwords(struct si_screen
*screen
);
1353 void si_cp_wait_mem(struct si_context
*ctx
, struct radeon_cmdbuf
*cs
,
1354 uint64_t va
, uint32_t ref
, uint32_t mask
, unsigned flags
);
1355 void si_init_fence_functions(struct si_context
*ctx
);
1356 void si_init_screen_fence_functions(struct si_screen
*screen
);
1357 struct pipe_fence_handle
*si_create_fence(struct pipe_context
*ctx
,
1358 struct tc_unflushed_batch_token
*tc_token
);
1361 void si_init_screen_get_functions(struct si_screen
*sscreen
);
1364 void si_flush_gfx_cs(struct si_context
*ctx
, unsigned flags
,
1365 struct pipe_fence_handle
**fence
);
1366 void si_begin_new_gfx_cs(struct si_context
*ctx
);
1367 void si_need_gfx_cs_space(struct si_context
*ctx
);
1368 void si_unref_sdma_uploads(struct si_context
*sctx
);
1371 void si_gpu_load_kill_thread(struct si_screen
*sscreen
);
1372 uint64_t si_begin_counter(struct si_screen
*sscreen
, unsigned type
);
1373 unsigned si_end_counter(struct si_screen
*sscreen
, unsigned type
,
1377 void si_emit_initial_compute_regs(struct si_context
*sctx
, struct radeon_cmdbuf
*cs
);
1378 unsigned si_get_compute_resource_limits(struct si_screen
*sscreen
,
1379 unsigned waves_per_threadgroup
,
1380 unsigned max_waves_per_sh
,
1381 unsigned threadgroups_per_cu
);
1382 void si_init_compute_functions(struct si_context
*sctx
);
1384 /* si_compute_prim_discard.c */
1385 enum si_prim_discard_outcome
{
1386 SI_PRIM_DISCARD_ENABLED
,
1387 SI_PRIM_DISCARD_DISABLED
,
1388 SI_PRIM_DISCARD_DRAW_SPLIT
,
1391 void si_build_prim_discard_compute_shader(struct si_shader_context
*ctx
);
1392 enum si_prim_discard_outcome
1393 si_prepare_prim_discard_or_split_draw(struct si_context
*sctx
,
1394 const struct pipe_draw_info
*info
,
1395 bool primitive_restart
);
1396 void si_compute_signal_gfx(struct si_context
*sctx
);
1397 void si_dispatch_prim_discard_cs_and_draw(struct si_context
*sctx
,
1398 const struct pipe_draw_info
*info
,
1399 unsigned index_size
,
1400 unsigned base_vertex
,
1401 uint64_t input_indexbuf_va
,
1402 unsigned input_indexbuf_max_elements
);
1403 void si_initialize_prim_discard_tunables(struct si_context
*sctx
);
1405 /* si_perfcounters.c */
1406 void si_init_perfcounters(struct si_screen
*screen
);
1407 void si_destroy_perfcounters(struct si_screen
*screen
);
1410 bool si_check_device_reset(struct si_context
*sctx
);
1413 void si_init_screen_query_functions(struct si_screen
*sscreen
);
1414 void si_init_query_functions(struct si_context
*sctx
);
1415 void si_suspend_queries(struct si_context
*sctx
);
1416 void si_resume_queries(struct si_context
*sctx
);
1418 /* si_shaderlib_tgsi.c */
1419 void *si_get_blitter_vs(struct si_context
*sctx
, enum blitter_attrib_type type
,
1420 unsigned num_layers
);
1421 void *si_create_fixed_func_tcs(struct si_context
*sctx
);
1422 void *si_create_dma_compute_shader(struct pipe_context
*ctx
,
1423 unsigned num_dwords_per_thread
,
1424 bool dst_stream_cache_policy
, bool is_copy
);
1425 void *si_create_copy_image_compute_shader(struct pipe_context
*ctx
);
1426 void *si_create_copy_image_compute_shader_1d_array(struct pipe_context
*ctx
);
1427 void *si_clear_render_target_shader(struct pipe_context
*ctx
);
1428 void *si_clear_render_target_shader_1d_array(struct pipe_context
*ctx
);
1429 void *si_create_dcc_retile_cs(struct pipe_context
*ctx
);
1430 void *si_create_query_result_cs(struct si_context
*sctx
);
1433 void si_test_dma(struct si_screen
*sscreen
);
1435 /* si_test_clearbuffer.c */
1436 void si_test_dma_perf(struct si_screen
*sscreen
);
1439 struct pipe_video_codec
*si_uvd_create_decoder(struct pipe_context
*context
,
1440 const struct pipe_video_codec
*templ
);
1442 struct pipe_video_buffer
*si_video_buffer_create(struct pipe_context
*pipe
,
1443 const struct pipe_video_buffer
*tmpl
);
1446 void si_update_vs_viewport_state(struct si_context
*ctx
);
1447 void si_init_viewport_functions(struct si_context
*ctx
);
1450 bool si_prepare_for_dma_blit(struct si_context
*sctx
,
1451 struct si_texture
*dst
,
1452 unsigned dst_level
, unsigned dstx
,
1453 unsigned dsty
, unsigned dstz
,
1454 struct si_texture
*src
,
1456 const struct pipe_box
*src_box
);
1457 void si_eliminate_fast_color_clear(struct si_context
*sctx
,
1458 struct si_texture
*tex
);
1459 void si_texture_discard_cmask(struct si_screen
*sscreen
,
1460 struct si_texture
*tex
);
1461 bool si_init_flushed_depth_texture(struct pipe_context
*ctx
,
1462 struct pipe_resource
*texture
);
1463 void si_print_texture_info(struct si_screen
*sscreen
,
1464 struct si_texture
*tex
, struct u_log_context
*log
);
1465 struct pipe_resource
*si_texture_create(struct pipe_screen
*screen
,
1466 const struct pipe_resource
*templ
);
1467 bool vi_dcc_formats_compatible(enum pipe_format format1
,
1468 enum pipe_format format2
);
1469 bool vi_dcc_formats_are_incompatible(struct pipe_resource
*tex
,
1471 enum pipe_format view_format
);
1472 void vi_disable_dcc_if_incompatible_format(struct si_context
*sctx
,
1473 struct pipe_resource
*tex
,
1475 enum pipe_format view_format
);
1476 struct pipe_surface
*si_create_surface_custom(struct pipe_context
*pipe
,
1477 struct pipe_resource
*texture
,
1478 const struct pipe_surface
*templ
,
1479 unsigned width0
, unsigned height0
,
1480 unsigned width
, unsigned height
);
1481 unsigned si_translate_colorswap(enum pipe_format format
, bool do_endian_swap
);
1482 void vi_separate_dcc_try_enable(struct si_context
*sctx
,
1483 struct si_texture
*tex
);
1484 void vi_separate_dcc_start_query(struct si_context
*sctx
,
1485 struct si_texture
*tex
);
1486 void vi_separate_dcc_stop_query(struct si_context
*sctx
,
1487 struct si_texture
*tex
);
1488 void vi_separate_dcc_process_and_reset_stats(struct pipe_context
*ctx
,
1489 struct si_texture
*tex
);
1490 bool si_texture_disable_dcc(struct si_context
*sctx
,
1491 struct si_texture
*tex
);
1492 void si_init_screen_texture_functions(struct si_screen
*sscreen
);
1493 void si_init_context_texture_functions(struct si_context
*sctx
);
1500 static inline struct si_resource
*si_resource(struct pipe_resource
*r
)
1502 return (struct si_resource
*)r
;
1506 si_resource_reference(struct si_resource
**ptr
, struct si_resource
*res
)
1508 pipe_resource_reference((struct pipe_resource
**)ptr
,
1509 (struct pipe_resource
*)res
);
1513 si_texture_reference(struct si_texture
**ptr
, struct si_texture
*res
)
1515 pipe_resource_reference((struct pipe_resource
**)ptr
, &res
->buffer
.b
.b
);
1519 vi_dcc_enabled(struct si_texture
*tex
, unsigned level
)
1521 return tex
->dcc_offset
&& level
< tex
->surface
.num_dcc_levels
;
1524 static inline unsigned
1525 si_tile_mode_index(struct si_texture
*tex
, unsigned level
, bool stencil
)
1528 return tex
->surface
.u
.legacy
.stencil_tiling_index
[level
];
1530 return tex
->surface
.u
.legacy
.tiling_index
[level
];
1533 static inline unsigned
1534 si_get_minimum_num_gfx_cs_dwords(struct si_context
*sctx
)
1536 /* Don't count the needed CS space exactly and just use an upper bound.
1538 * Also reserve space for stopping queries at the end of IB, because
1539 * the number of active queries is unlimited in theory.
1541 return 2048 + sctx
->num_cs_dw_queries_suspend
;
1545 si_context_add_resource_size(struct si_context
*sctx
, struct pipe_resource
*r
)
1548 /* Add memory usage for need_gfx_cs_space */
1549 sctx
->vram
+= si_resource(r
)->vram_usage
;
1550 sctx
->gtt
+= si_resource(r
)->gart_usage
;
1555 si_invalidate_draw_sh_constants(struct si_context
*sctx
)
1557 sctx
->last_base_vertex
= SI_BASE_VERTEX_UNKNOWN
;
1558 sctx
->last_instance_count
= SI_INSTANCE_COUNT_UNKNOWN
;
1561 static inline unsigned
1562 si_get_atom_bit(struct si_context
*sctx
, struct si_atom
*atom
)
1564 return 1 << (atom
- sctx
->atoms
.array
);
1568 si_set_atom_dirty(struct si_context
*sctx
, struct si_atom
*atom
, bool dirty
)
1570 unsigned bit
= si_get_atom_bit(sctx
, atom
);
1573 sctx
->dirty_atoms
|= bit
;
1575 sctx
->dirty_atoms
&= ~bit
;
1579 si_is_atom_dirty(struct si_context
*sctx
, struct si_atom
*atom
)
1581 return (sctx
->dirty_atoms
& si_get_atom_bit(sctx
, atom
)) != 0;
1585 si_mark_atom_dirty(struct si_context
*sctx
, struct si_atom
*atom
)
1587 si_set_atom_dirty(sctx
, atom
, true);
1590 static inline struct si_shader_ctx_state
*si_get_vs(struct si_context
*sctx
)
1592 if (sctx
->gs_shader
.cso
)
1593 return &sctx
->gs_shader
;
1594 if (sctx
->tes_shader
.cso
)
1595 return &sctx
->tes_shader
;
1597 return &sctx
->vs_shader
;
1600 static inline struct tgsi_shader_info
*si_get_vs_info(struct si_context
*sctx
)
1602 struct si_shader_ctx_state
*vs
= si_get_vs(sctx
);
1604 return vs
->cso
? &vs
->cso
->info
: NULL
;
1607 static inline struct si_shader
* si_get_vs_state(struct si_context
*sctx
)
1609 if (sctx
->gs_shader
.cso
)
1610 return sctx
->gs_shader
.cso
->gs_copy_shader
;
1612 struct si_shader_ctx_state
*vs
= si_get_vs(sctx
);
1613 return vs
->current
? vs
->current
: NULL
;
1616 static inline bool si_can_dump_shader(struct si_screen
*sscreen
,
1619 return sscreen
->debug_flags
& (1 << processor
);
1622 static inline bool si_get_strmout_en(struct si_context
*sctx
)
1624 return sctx
->streamout
.streamout_enabled
||
1625 sctx
->streamout
.prims_gen_query_enabled
;
1628 static inline unsigned
1629 si_optimal_tcc_alignment(struct si_context
*sctx
, unsigned upload_size
)
1631 unsigned alignment
, tcc_cache_line_size
;
1633 /* If the upload size is less than the cache line size (e.g. 16, 32),
1634 * the whole thing will fit into a cache line if we align it to its size.
1635 * The idea is that multiple small uploads can share a cache line.
1636 * If the upload size is greater, align it to the cache line size.
1638 alignment
= util_next_power_of_two(upload_size
);
1639 tcc_cache_line_size
= sctx
->screen
->info
.tcc_cache_line_size
;
1640 return MIN2(alignment
, tcc_cache_line_size
);
1644 si_saved_cs_reference(struct si_saved_cs
**dst
, struct si_saved_cs
*src
)
1646 if (pipe_reference(&(*dst
)->reference
, &src
->reference
))
1647 si_destroy_saved_cs(*dst
);
1653 si_make_CB_shader_coherent(struct si_context
*sctx
, unsigned num_samples
,
1654 bool shaders_read_metadata
, bool dcc_pipe_aligned
)
1656 sctx
->flags
|= SI_CONTEXT_FLUSH_AND_INV_CB
|
1657 SI_CONTEXT_INV_VCACHE
;
1659 if (sctx
->chip_class
>= GFX9
) {
1660 /* Single-sample color is coherent with shaders on GFX9, but
1661 * L2 metadata must be flushed if shaders read metadata.
1664 if (num_samples
>= 2 ||
1665 (shaders_read_metadata
&& !dcc_pipe_aligned
))
1666 sctx
->flags
|= SI_CONTEXT_INV_L2
;
1667 else if (shaders_read_metadata
)
1668 sctx
->flags
|= SI_CONTEXT_INV_L2_METADATA
;
1671 sctx
->flags
|= SI_CONTEXT_INV_L2
;
1676 si_make_DB_shader_coherent(struct si_context
*sctx
, unsigned num_samples
,
1677 bool include_stencil
, bool shaders_read_metadata
)
1679 sctx
->flags
|= SI_CONTEXT_FLUSH_AND_INV_DB
|
1680 SI_CONTEXT_INV_VCACHE
;
1682 if (sctx
->chip_class
>= GFX9
) {
1683 /* Single-sample depth (not stencil) is coherent with shaders
1684 * on GFX9, but L2 metadata must be flushed if shaders read
1687 if (num_samples
>= 2 || include_stencil
)
1688 sctx
->flags
|= SI_CONTEXT_INV_L2
;
1689 else if (shaders_read_metadata
)
1690 sctx
->flags
|= SI_CONTEXT_INV_L2_METADATA
;
1693 sctx
->flags
|= SI_CONTEXT_INV_L2
;
1698 si_can_sample_zs(struct si_texture
*tex
, bool stencil_sampler
)
1700 return (stencil_sampler
&& tex
->can_sample_s
) ||
1701 (!stencil_sampler
&& tex
->can_sample_z
);
1705 si_htile_enabled(struct si_texture
*tex
, unsigned level
)
1707 return tex
->htile_offset
&& level
== 0;
1711 vi_tc_compat_htile_enabled(struct si_texture
*tex
, unsigned level
)
1713 assert(!tex
->tc_compatible_htile
|| tex
->htile_offset
);
1714 return tex
->tc_compatible_htile
&& level
== 0;
1717 static inline unsigned si_get_ps_iter_samples(struct si_context
*sctx
)
1719 if (sctx
->ps_uses_fbfetch
)
1720 return sctx
->framebuffer
.nr_color_samples
;
1722 return MIN2(sctx
->ps_iter_samples
, sctx
->framebuffer
.nr_color_samples
);
1725 static inline unsigned si_get_total_colormask(struct si_context
*sctx
)
1727 if (sctx
->queued
.named
.rasterizer
->rasterizer_discard
)
1730 struct si_shader_selector
*ps
= sctx
->ps_shader
.cso
;
1734 unsigned colormask
= sctx
->framebuffer
.colorbuf_enabled_4bit
&
1735 sctx
->queued
.named
.blend
->cb_target_mask
;
1737 if (!ps
->info
.properties
[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
])
1738 colormask
&= ps
->colors_written_4bit
;
1739 else if (!ps
->colors_written_4bit
)
1740 colormask
= 0; /* color0 writes all cbufs, but it's not written */
1745 #define UTIL_ALL_PRIM_LINE_MODES ((1 << PIPE_PRIM_LINES) | \
1746 (1 << PIPE_PRIM_LINE_LOOP) | \
1747 (1 << PIPE_PRIM_LINE_STRIP) | \
1748 (1 << PIPE_PRIM_LINES_ADJACENCY) | \
1749 (1 << PIPE_PRIM_LINE_STRIP_ADJACENCY))
1751 static inline bool util_prim_is_lines(unsigned prim
)
1753 return ((1 << prim
) & UTIL_ALL_PRIM_LINE_MODES
) != 0;
1756 static inline bool util_prim_is_points_or_lines(unsigned prim
)
1758 return ((1 << prim
) & (UTIL_ALL_PRIM_LINE_MODES
|
1759 (1 << PIPE_PRIM_POINTS
))) != 0;
1763 * Return true if there is enough memory in VRAM and GTT for the buffers
1766 * \param vram VRAM memory size not added to the buffer list yet
1767 * \param gtt GTT memory size not added to the buffer list yet
1770 radeon_cs_memory_below_limit(struct si_screen
*screen
,
1771 struct radeon_cmdbuf
*cs
,
1772 uint64_t vram
, uint64_t gtt
)
1774 vram
+= cs
->used_vram
;
1775 gtt
+= cs
->used_gart
;
1777 /* Anything that goes above the VRAM size should go to GTT. */
1778 if (vram
> screen
->info
.vram_size
)
1779 gtt
+= vram
- screen
->info
.vram_size
;
1781 /* Now we just need to check if we have enough GTT. */
1782 return gtt
< screen
->info
.gart_size
* 0.7;
1786 * Add a buffer to the buffer list for the given command stream (CS).
1788 * All buffers used by a CS must be added to the list. This tells the kernel
1789 * driver which buffers are used by GPU commands. Other buffers can
1790 * be swapped out (not accessible) during execution.
1792 * The buffer list becomes empty after every context flush and must be
1795 static inline void radeon_add_to_buffer_list(struct si_context
*sctx
,
1796 struct radeon_cmdbuf
*cs
,
1797 struct si_resource
*bo
,
1798 enum radeon_bo_usage usage
,
1799 enum radeon_bo_priority priority
)
1802 sctx
->ws
->cs_add_buffer(
1804 (enum radeon_bo_usage
)(usage
| RADEON_USAGE_SYNCHRONIZED
),
1805 bo
->domains
, priority
);
1809 * Same as above, but also checks memory usage and flushes the context
1812 * When this SHOULD NOT be used:
1814 * - if si_context_add_resource_size has been called for the buffer
1815 * followed by *_need_cs_space for checking the memory usage
1817 * - if si_need_dma_space has been called for the buffer
1819 * - when emitting state packets and draw packets (because preceding packets
1820 * can't be re-emitted at that point)
1822 * - if shader resource "enabled_mask" is not up-to-date or there is
1823 * a different constraint disallowing a context flush
1826 radeon_add_to_gfx_buffer_list_check_mem(struct si_context
*sctx
,
1827 struct si_resource
*bo
,
1828 enum radeon_bo_usage usage
,
1829 enum radeon_bo_priority priority
,
1833 !radeon_cs_memory_below_limit(sctx
->screen
, sctx
->gfx_cs
,
1834 sctx
->vram
+ bo
->vram_usage
,
1835 sctx
->gtt
+ bo
->gart_usage
))
1836 si_flush_gfx_cs(sctx
, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW
, NULL
);
1838 radeon_add_to_buffer_list(sctx
, sctx
->gfx_cs
, bo
, usage
, priority
);
1841 static inline bool si_compute_prim_discard_enabled(struct si_context
*sctx
)
1843 return sctx
->prim_discard_vertex_count_threshold
!= UINT_MAX
;
1846 #define PRINT_ERR(fmt, args...) \
1847 fprintf(stderr, "EE %s:%d %s - " fmt, __FILE__, __LINE__, __func__, ##args)