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"
30 #include "util/u_dynarray.h"
31 #include "util/u_idalloc.h"
33 #ifdef PIPE_ARCH_BIG_ENDIAN
34 #define SI_BIG_ENDIAN 1
36 #define SI_BIG_ENDIAN 0
39 #define ATI_VENDOR_ID 0x1002
41 #define SI_NOT_QUERY 0xffffffff
43 /* The base vertex and primitive restart can be any number, but we must pick
44 * one which will mean "unknown" for the purpose of state tracking and
45 * the number shouldn't be a commonly-used one. */
46 #define SI_BASE_VERTEX_UNKNOWN INT_MIN
47 #define SI_RESTART_INDEX_UNKNOWN INT_MIN
48 #define SI_NUM_SMOOTH_AA_SAMPLES 8
49 #define SI_GS_PER_ES 128
50 /* Alignment for optimal CP DMA performance. */
51 #define SI_CPDMA_ALIGNMENT 32
53 /* Pipeline & streamout query controls. */
54 #define SI_CONTEXT_START_PIPELINE_STATS (1 << 0)
55 #define SI_CONTEXT_STOP_PIPELINE_STATS (1 << 1)
56 #define SI_CONTEXT_FLUSH_FOR_RENDER_COND (1 << 2)
57 /* Instruction cache. */
58 #define SI_CONTEXT_INV_ICACHE (1 << 3)
59 /* SMEM L1, other names: KCACHE, constant cache, DCACHE, data cache */
60 #define SI_CONTEXT_INV_SMEM_L1 (1 << 4)
61 /* VMEM L1 can optionally be bypassed (GLC=1). Other names: TC L1 */
62 #define SI_CONTEXT_INV_VMEM_L1 (1 << 5)
63 /* Used by everything except CB/DB, can be bypassed (SLC=1). Other names: TC L2 */
64 #define SI_CONTEXT_INV_GLOBAL_L2 (1 << 6)
65 /* Write dirty L2 lines back to memory (shader and CP DMA stores), but don't
66 * invalidate L2. SI-CIK can't do it, so they will do complete invalidation. */
67 #define SI_CONTEXT_WRITEBACK_GLOBAL_L2 (1 << 7)
68 /* Writeback & invalidate the L2 metadata cache. It can only be coupled with
69 * a CB or DB flush. */
70 #define SI_CONTEXT_INV_L2_METADATA (1 << 8)
71 /* Framebuffer caches. */
72 #define SI_CONTEXT_FLUSH_AND_INV_DB (1 << 9)
73 #define SI_CONTEXT_FLUSH_AND_INV_DB_META (1 << 10)
74 #define SI_CONTEXT_FLUSH_AND_INV_CB (1 << 11)
75 /* Engine synchronization. */
76 #define SI_CONTEXT_VS_PARTIAL_FLUSH (1 << 12)
77 #define SI_CONTEXT_PS_PARTIAL_FLUSH (1 << 13)
78 #define SI_CONTEXT_CS_PARTIAL_FLUSH (1 << 14)
79 #define SI_CONTEXT_VGT_FLUSH (1 << 15)
80 #define SI_CONTEXT_VGT_STREAMOUT_SYNC (1 << 16)
82 #define SI_PREFETCH_VBO_DESCRIPTORS (1 << 0)
83 #define SI_PREFETCH_LS (1 << 1)
84 #define SI_PREFETCH_HS (1 << 2)
85 #define SI_PREFETCH_ES (1 << 3)
86 #define SI_PREFETCH_GS (1 << 4)
87 #define SI_PREFETCH_VS (1 << 5)
88 #define SI_PREFETCH_PS (1 << 6)
90 #define SI_MAX_BORDER_COLORS 4096
91 #define SI_MAX_VIEWPORTS 16
93 #define SI_MAP_BUFFER_ALIGNMENT 64
94 #define SI_MAX_VARIABLE_THREADS_PER_BLOCK 1024
96 #define SI_RESOURCE_FLAG_TRANSFER (PIPE_RESOURCE_FLAG_DRV_PRIV << 0)
97 #define SI_RESOURCE_FLAG_FLUSHED_DEPTH (PIPE_RESOURCE_FLAG_DRV_PRIV << 1)
98 #define SI_RESOURCE_FLAG_FORCE_TILING (PIPE_RESOURCE_FLAG_DRV_PRIV << 2)
99 #define SI_RESOURCE_FLAG_DISABLE_DCC (PIPE_RESOURCE_FLAG_DRV_PRIV << 3)
100 #define SI_RESOURCE_FLAG_UNMAPPABLE (PIPE_RESOURCE_FLAG_DRV_PRIV << 4)
101 #define SI_RESOURCE_FLAG_READ_ONLY (PIPE_RESOURCE_FLAG_DRV_PRIV << 5)
102 #define SI_RESOURCE_FLAG_32BIT (PIPE_RESOURCE_FLAG_DRV_PRIV << 6)
106 /* Shader logging options: */
107 DBG_VS
= PIPE_SHADER_VERTEX
,
108 DBG_PS
= PIPE_SHADER_FRAGMENT
,
109 DBG_GS
= PIPE_SHADER_GEOMETRY
,
110 DBG_TCS
= PIPE_SHADER_TESS_CTRL
,
111 DBG_TES
= PIPE_SHADER_TESS_EVAL
,
112 DBG_CS
= PIPE_SHADER_COMPUTE
,
118 /* Shader compiler options the shader cache should be aware of: */
119 DBG_FS_CORRECT_DERIVS_AFTER_KILL
,
123 /* Shader compiler options (with no effect on the shader cache): */
126 DBG_MONOLITHIC_SHADERS
,
129 /* Information logging options: */
135 /* Driver options: */
142 /* 3D engine options: */
163 DBG_TEST_VMFAULT_SDMA
,
164 DBG_TEST_VMFAULT_SHADER
,
167 #define DBG_ALL_SHADERS (((1 << (DBG_CS + 1)) - 1))
168 #define DBG(name) (1ull << DBG_##name)
172 struct u_suballocator
;
175 struct pipe_screen b
;
176 struct radeon_winsys
*ws
;
177 struct disk_cache
*disk_shader_cache
;
179 struct radeon_info info
;
180 uint64_t debug_flags
;
181 char renderer_string
[100];
183 unsigned gs_table_depth
;
184 unsigned tess_offchip_block_dw_size
;
185 unsigned tess_offchip_ring_size
;
186 unsigned tess_factor_ring_size
;
187 unsigned vgt_hs_offchip_param
;
188 bool has_clear_state
;
189 bool has_distributed_tess
;
190 bool has_draw_indirect_multi
;
191 bool has_out_of_order_rast
;
192 bool assume_no_z_fights
;
193 bool commutative_blend_add
;
194 bool clear_db_cache_before_clear
;
195 bool has_msaa_sample_loc_bug
;
196 bool has_ls_vgpr_init_bug
;
199 bool llvm_has_working_vgpr_indexing
;
201 /* Whether shaders are monolithic (1-part) or separate (3-part). */
202 bool use_monolithic_shaders
;
204 bool has_rbplus
; /* if RB+ registers exist */
205 bool rbplus_allowed
; /* if RB+ is allowed */
206 bool dcc_msaa_allowed
;
207 bool cpdma_prefetch_writes_memory
;
209 struct slab_parent_pool pool_transfers
;
211 /* Texture filter settings. */
212 int force_aniso
; /* -1 = disabled */
214 /* Auxiliary context. Mainly used to initialize resources.
215 * It must be locked prior to using and flushed before unlocking. */
216 struct pipe_context
*aux_context
;
217 mtx_t aux_context_lock
;
219 /* This must be in the screen, because UE4 uses one context for
220 * compilation and another one for rendering.
222 unsigned num_compilations
;
223 /* Along with ST_DEBUG=precompile, this should show if applications
224 * are loading shaders on demand. This is a monotonic counter.
226 unsigned num_shaders_created
;
227 unsigned num_shader_cache_hits
;
229 /* GPU load thread. */
230 mtx_t gpu_load_mutex
;
231 thrd_t gpu_load_thread
;
232 union si_mmio_counters mmio_counters
;
233 volatile unsigned gpu_load_stop_thread
; /* bool */
235 /* Performance counters. */
236 struct si_perfcounters
*perfcounters
;
238 /* If pipe_screen wants to recompute and re-emit the framebuffer,
239 * sampler, and image states of all contexts, it should atomically
242 * Each context will compare this with its own last known value of
243 * the counter before drawing and re-emit the states accordingly.
245 unsigned dirty_tex_counter
;
247 /* Atomically increment this counter when an existing texture's
248 * metadata is enabled or disabled in a way that requires changing
249 * contexts' compressed texture binding masks.
251 unsigned compressed_colortex_counter
;
254 /* Context flags to set so that all writes from earlier jobs
255 * in the CP are seen by L2 clients.
259 /* Context flags to set so that all writes from earlier jobs
260 * that end in L2 are seen by CP.
265 mtx_t shader_parts_mutex
;
266 struct si_shader_part
*vs_prologs
;
267 struct si_shader_part
*tcs_epilogs
;
268 struct si_shader_part
*gs_prologs
;
269 struct si_shader_part
*ps_prologs
;
270 struct si_shader_part
*ps_epilogs
;
272 /* Shader cache in memory.
274 * Design & limitations:
275 * - The shader cache is per screen (= per process), never saved to
276 * disk, and skips redundant shader compilations from TGSI to bytecode.
277 * - It can only be used with one-variant-per-shader support, in which
278 * case only the main (typically middle) part of shaders is cached.
279 * - Only VS, TCS, TES, PS are cached, out of which only the hw VS
280 * variants of VS and TES are cached, so LS and ES aren't.
281 * - GS and CS aren't cached, but it's certainly possible to cache
284 mtx_t shader_cache_mutex
;
285 struct hash_table
*shader_cache
;
287 /* Shader compiler queue for multithreaded compilation. */
288 struct util_queue shader_compiler_queue
;
289 /* Use at most 3 normal compiler threads on quadcore and better.
290 * Hyperthreaded CPUs report the number of threads, but we want
291 * the number of cores. */
292 LLVMTargetMachineRef tm
[3]; /* used by the queue only */
294 struct util_queue shader_compiler_queue_low_priority
;
295 /* Use at most 2 low priority threads on quadcore and better.
296 * We want to minimize the impact on multithreaded Mesa. */
297 LLVMTargetMachineRef tm_low_priority
[2]; /* at most 2 threads */
300 struct si_blend_color
{
301 struct r600_atom atom
;
302 struct pipe_blend_color state
;
306 struct si_sampler_view
{
307 struct pipe_sampler_view base
;
308 /* [0..7] = image descriptor
309 * [4..7] = buffer descriptor */
311 uint32_t fmask_state
[8];
312 const struct legacy_surf_level
*base_level_info
;
315 bool is_stencil_sampler
;
317 bool dcc_incompatible
;
320 #define SI_SAMPLER_STATE_MAGIC 0x34f1c35a
322 struct si_sampler_state
{
327 uint32_t integer_val
[4];
328 uint32_t upgraded_depth_val
[4];
331 struct si_cs_shader_state
{
332 struct si_compute
*program
;
333 struct si_compute
*emitted_program
;
340 struct pipe_sampler_view
*views
[SI_NUM_SAMPLERS
];
341 struct si_sampler_state
*sampler_states
[SI_NUM_SAMPLERS
];
343 /* The i-th bit is set if that element is enabled (non-NULL resource). */
344 unsigned enabled_mask
;
345 uint32_t needs_depth_decompress_mask
;
346 uint32_t needs_color_decompress_mask
;
350 struct pipe_image_view views
[SI_NUM_IMAGES
];
351 uint32_t needs_color_decompress_mask
;
352 unsigned enabled_mask
;
355 struct si_framebuffer
{
356 struct r600_atom atom
;
357 struct pipe_framebuffer_state state
;
358 unsigned colorbuf_enabled_4bit
;
359 unsigned spi_shader_col_format
;
360 unsigned spi_shader_col_format_alpha
;
361 unsigned spi_shader_col_format_blend
;
362 unsigned spi_shader_col_format_blend_alpha
;
363 ubyte nr_samples
:5; /* at most 16xAA */
364 ubyte log_samples
:3; /* at most 4 = 16xAA */
365 ubyte compressed_cb_mask
;
366 ubyte uncompressed_cb_mask
;
368 ubyte color_is_int10
;
372 bool CB_has_shader_readable_metadata
;
373 bool DB_has_shader_readable_metadata
;
376 struct si_signed_scissor
{
384 struct r600_atom atom
;
386 struct pipe_scissor_state states
[SI_MAX_VIEWPORTS
];
389 struct si_viewports
{
390 struct r600_atom atom
;
392 unsigned depth_range_dirty_mask
;
393 struct pipe_viewport_state states
[SI_MAX_VIEWPORTS
];
394 struct si_signed_scissor as_scissor
[SI_MAX_VIEWPORTS
];
397 struct si_clip_state
{
398 struct r600_atom atom
;
399 struct pipe_clip_state state
;
403 struct si_sample_locs
{
404 struct r600_atom atom
;
408 struct si_sample_mask
{
409 struct r600_atom atom
;
410 uint16_t sample_mask
;
413 struct si_streamout_target
{
414 struct pipe_stream_output_target b
;
416 /* The buffer where BUFFER_FILLED_SIZE is stored. */
417 struct r600_resource
*buf_filled_size
;
418 unsigned buf_filled_size_offset
;
419 bool buf_filled_size_valid
;
421 unsigned stride_in_dw
;
424 struct si_streamout
{
425 struct r600_atom begin_atom
;
428 unsigned enabled_mask
;
429 unsigned num_targets
;
430 struct si_streamout_target
*targets
[PIPE_MAX_SO_BUFFERS
];
432 unsigned append_bitmask
;
435 /* External state which comes from the vertex shader,
436 * it must be set explicitly when binding a shader. */
437 uint16_t *stride_in_dw
;
438 unsigned enabled_stream_buffers_mask
; /* stream0 buffers0-3 in 4 LSB */
440 /* The state of VGT_STRMOUT_BUFFER_(CONFIG|EN). */
441 unsigned hw_enabled_mask
;
443 /* The state of VGT_STRMOUT_(CONFIG|EN). */
444 struct r600_atom enable_atom
;
445 bool streamout_enabled
;
446 bool prims_gen_query_enabled
;
447 int num_prims_gen_queries
;
450 /* A shader state consists of the shader selector, which is a constant state
451 * object shared by multiple contexts and shouldn't be modified, and
452 * the current shader variant selected for this context.
454 struct si_shader_ctx_state
{
455 struct si_shader_selector
*cso
;
456 struct si_shader
*current
;
459 #define SI_NUM_VGT_PARAM_KEY_BITS 12
460 #define SI_NUM_VGT_PARAM_STATES (1 << SI_NUM_VGT_PARAM_KEY_BITS)
462 /* The IA_MULTI_VGT_PARAM key used to index the table of precomputed values.
463 * Some fields are set by state-change calls, most are set by draw_vbo.
465 union si_vgt_param_key
{
468 unsigned uses_instancing
:1;
469 unsigned multi_instances_smaller_than_primgroup
:1;
470 unsigned primitive_restart
:1;
471 unsigned count_from_stream_output
:1;
472 unsigned line_stipple_enabled
:1;
473 unsigned uses_tess
:1;
474 unsigned tess_uses_prim_id
:1;
476 unsigned _pad
:32 - SI_NUM_VGT_PARAM_KEY_BITS
;
481 struct si_texture_handle
485 struct pipe_sampler_view
*view
;
486 struct si_sampler_state sstate
;
489 struct si_image_handle
493 struct pipe_image_view view
;
497 struct pipe_reference reference
;
498 struct si_context
*ctx
;
499 struct radeon_saved_cs gfx
;
500 struct r600_resource
*trace_buf
;
503 unsigned gfx_last_dw
;
509 struct r600_common_context b
;
510 struct blitter_context
*blitter
;
511 void *custom_dsa_flush
;
512 void *custom_blend_resolve
;
513 void *custom_blend_fmask_decompress
;
514 void *custom_blend_eliminate_fastclear
;
515 void *custom_blend_dcc_decompress
;
517 void *vs_blit_pos_layered
;
519 void *vs_blit_color_layered
;
520 void *vs_blit_texcoord
;
521 struct si_screen
*screen
;
522 struct pipe_debug_callback debug
;
523 LLVMTargetMachineRef tm
; /* only non-threaded compilation */
524 struct si_shader_ctx_state fixed_func_tcs_shader
;
525 struct r600_resource
*wait_mem_scratch
;
526 unsigned wait_mem_number
;
527 uint16_t prefetch_L2_mask
;
529 bool gfx_flush_in_progress
:1;
530 bool compute_is_busy
:1;
532 /* Atoms (direct states). */
533 union si_state_atoms atoms
;
534 unsigned dirty_atoms
; /* mask */
535 /* PM4 states (precomputed immutable states) */
536 unsigned dirty_states
;
537 union si_state queued
;
538 union si_state emitted
;
540 /* Atom declarations. */
541 struct si_framebuffer framebuffer
;
542 struct si_sample_locs msaa_sample_locs
;
543 struct r600_atom db_render_state
;
544 struct r600_atom dpbb_state
;
545 struct r600_atom msaa_config
;
546 struct si_sample_mask sample_mask
;
547 struct r600_atom cb_render_state
;
548 unsigned last_cb_target_mask
;
549 struct si_blend_color blend_color
;
550 struct r600_atom clip_regs
;
551 struct si_clip_state clip_state
;
552 struct si_shader_data shader_pointers
;
553 struct si_stencil_ref stencil_ref
;
554 struct r600_atom spi_map
;
555 struct si_scissors scissors
;
556 struct si_streamout streamout
;
557 struct si_viewports viewports
;
559 /* Precomputed states. */
560 struct si_pm4_state
*init_config
;
561 struct si_pm4_state
*init_config_gs_rings
;
562 bool init_config_has_vgt_flush
;
563 struct si_pm4_state
*vgt_shader_config
[4];
566 struct si_shader_ctx_state ps_shader
;
567 struct si_shader_ctx_state gs_shader
;
568 struct si_shader_ctx_state vs_shader
;
569 struct si_shader_ctx_state tcs_shader
;
570 struct si_shader_ctx_state tes_shader
;
571 struct si_cs_shader_state cs_shader_state
;
573 /* shader information */
574 struct si_vertex_elements
*vertex_elements
;
575 unsigned sprite_coord_enable
;
577 bool do_update_shaders
;
579 /* vertex buffer descriptors */
580 uint32_t *vb_descriptors_gpu_list
;
581 struct r600_resource
*vb_descriptors_buffer
;
582 unsigned vb_descriptors_offset
;
584 /* shader descriptors */
585 struct si_descriptors descriptors
[SI_NUM_DESCS
];
586 unsigned descriptors_dirty
;
587 unsigned shader_pointers_dirty
;
588 unsigned shader_needs_decompress_mask
;
589 struct si_buffer_resources rw_buffers
;
590 struct si_buffer_resources const_and_shader_buffers
[SI_NUM_SHADERS
];
591 struct si_samplers samplers
[SI_NUM_SHADERS
];
592 struct si_images images
[SI_NUM_SHADERS
];
594 /* other shader resources */
595 struct pipe_constant_buffer null_const_buf
; /* used for set_constant_buffer(NULL) on CIK */
596 struct pipe_resource
*esgs_ring
;
597 struct pipe_resource
*gsvs_ring
;
598 struct pipe_resource
*tess_rings
;
599 union pipe_color_union
*border_color_table
; /* in CPU memory, any endian */
600 struct r600_resource
*border_color_buffer
;
601 union pipe_color_union
*border_color_map
; /* in VRAM (slow access), little endian */
602 unsigned border_color_count
;
603 unsigned num_vs_blit_sgprs
;
604 uint32_t vs_blit_sh_data
[SI_VS_BLIT_SGPRS_POS_TEXCOORD
];
606 /* Vertex and index buffers. */
607 bool vertex_buffers_dirty
;
608 bool vertex_buffer_pointer_dirty
;
609 struct pipe_vertex_buffer vertex_buffer
[SI_NUM_VERTEX_BUFFERS
];
611 /* MSAA config state. */
613 bool ps_uses_fbfetch
;
614 bool smoothing_enabled
;
616 /* DB render state. */
617 unsigned ps_db_shader_control
;
618 unsigned dbcb_copy_sample
;
619 bool dbcb_depth_copy_enabled
:1;
620 bool dbcb_stencil_copy_enabled
:1;
621 bool db_flush_depth_inplace
:1;
622 bool db_flush_stencil_inplace
:1;
623 bool db_depth_clear
:1;
624 bool db_depth_disable_expclear
:1;
625 bool db_stencil_clear
:1;
626 bool db_stencil_disable_expclear
:1;
627 bool occlusion_queries_disabled
:1;
628 bool generate_mipmap_for_depth
:1;
630 /* Emitted draw state. */
631 bool gs_tri_strip_adj_fix
:1;
634 int last_base_vertex
;
635 int last_start_instance
;
637 int last_sh_base_reg
;
638 int last_primitive_restart_en
;
639 int last_restart_index
;
640 int last_gs_out_prim
;
642 int last_multi_vgt_param
;
644 unsigned last_sc_line_stipple
;
645 unsigned current_vs_state
;
646 unsigned last_vs_state
;
647 enum pipe_prim_type current_rast_prim
; /* primitive type after TES, GS */
650 struct r600_atom scratch_state
;
651 struct r600_resource
*scratch_buffer
;
652 unsigned scratch_waves
;
653 unsigned spi_tmpring_size
;
655 struct r600_resource
*compute_scratch_buffer
;
657 /* Emitted derived tessellation state. */
658 /* Local shader (VS), or HS if LS-HS are merged. */
659 struct si_shader
*last_ls
;
660 struct si_shader_selector
*last_tcs
;
661 int last_num_tcs_input_cp
;
662 int last_tes_sh_base
;
663 bool last_tess_uses_primid
;
664 unsigned last_num_patches
;
668 struct si_saved_cs
*current_saved_cs
;
669 uint64_t dmesg_timestamp
;
670 unsigned apitrace_call_number
;
673 bool need_check_render_feedback
;
674 bool decompression_enabled
;
676 bool vs_writes_viewport_index
;
677 bool vs_disables_clipping_viewport
;
679 /* Precomputed IA_MULTI_VGT_PARAM */
680 union si_vgt_param_key ia_multi_vgt_param_key
;
681 unsigned ia_multi_vgt_param
[SI_NUM_VGT_PARAM_STATES
];
683 /* Bindless descriptors. */
684 struct si_descriptors bindless_descriptors
;
685 struct util_idalloc bindless_used_slots
;
686 unsigned num_bindless_descriptors
;
687 bool bindless_descriptors_dirty
;
688 bool graphics_bindless_pointer_dirty
;
689 bool compute_bindless_pointer_dirty
;
691 /* Allocated bindless handles */
692 struct hash_table
*tex_handles
;
693 struct hash_table
*img_handles
;
695 /* Resident bindless handles */
696 struct util_dynarray resident_tex_handles
;
697 struct util_dynarray resident_img_handles
;
699 /* Resident bindless handles which need decompression */
700 struct util_dynarray resident_tex_needs_color_decompress
;
701 struct util_dynarray resident_img_needs_color_decompress
;
702 struct util_dynarray resident_tex_needs_depth_decompress
;
705 bool uses_bindless_samplers
;
706 bool uses_bindless_images
;
708 /* MSAA sample locations.
709 * The first index is the sample index.
710 * The second index is the coordinate: X, Y. */
711 float sample_locations_1x
[1][2];
712 float sample_locations_2x
[2][2];
713 float sample_locations_4x
[4][2];
714 float sample_locations_8x
[8][2];
715 float sample_locations_16x
[16][2];
719 void cik_init_sdma_functions(struct si_context
*sctx
);
722 enum si_blitter_op
/* bitmask */
724 SI_SAVE_TEXTURES
= 1,
725 SI_SAVE_FRAMEBUFFER
= 2,
726 SI_SAVE_FRAGMENT_STATE
= 4,
727 SI_DISABLE_RENDER_COND
= 8,
730 void si_blitter_begin(struct si_context
*sctx
, enum si_blitter_op op
);
731 void si_blitter_end(struct si_context
*sctx
);
732 void si_init_blit_functions(struct si_context
*sctx
);
733 void si_decompress_textures(struct si_context
*sctx
, unsigned shader_mask
);
734 void si_resource_copy_region(struct pipe_context
*ctx
,
735 struct pipe_resource
*dst
,
737 unsigned dstx
, unsigned dsty
, unsigned dstz
,
738 struct pipe_resource
*src
,
740 const struct pipe_box
*src_box
);
741 void si_decompress_dcc(struct si_context
*sctx
, struct r600_texture
*rtex
);
742 void si_blit_decompress_depth(struct pipe_context
*ctx
,
743 struct r600_texture
*texture
,
744 struct r600_texture
*staging
,
745 unsigned first_level
, unsigned last_level
,
746 unsigned first_layer
, unsigned last_layer
,
747 unsigned first_sample
, unsigned last_sample
);
750 bool si_rings_is_buffer_referenced(struct si_context
*sctx
,
751 struct pb_buffer
*buf
,
752 enum radeon_bo_usage usage
);
753 void *si_buffer_map_sync_with_rings(struct si_context
*sctx
,
754 struct r600_resource
*resource
,
756 void si_init_resource_fields(struct si_screen
*sscreen
,
757 struct r600_resource
*res
,
758 uint64_t size
, unsigned alignment
);
759 bool si_alloc_resource(struct si_screen
*sscreen
,
760 struct r600_resource
*res
);
761 struct pipe_resource
*si_aligned_buffer_create(struct pipe_screen
*screen
,
766 void si_replace_buffer_storage(struct pipe_context
*ctx
,
767 struct pipe_resource
*dst
,
768 struct pipe_resource
*src
);
769 void si_init_screen_buffer_functions(struct si_screen
*sscreen
);
770 void si_init_buffer_functions(struct si_context
*sctx
);
773 void vi_dcc_clear_level(struct si_context
*sctx
,
774 struct r600_texture
*rtex
,
775 unsigned level
, unsigned clear_value
);
776 void si_init_clear_functions(struct si_context
*sctx
);
779 #define SI_CPDMA_SKIP_CHECK_CS_SPACE (1 << 0) /* don't call need_cs_space */
780 #define SI_CPDMA_SKIP_SYNC_AFTER (1 << 1) /* don't wait for DMA after the copy */
781 #define SI_CPDMA_SKIP_SYNC_BEFORE (1 << 2) /* don't wait for DMA before the copy (RAW hazards) */
782 #define SI_CPDMA_SKIP_GFX_SYNC (1 << 3) /* don't flush caches and don't wait for PS/CS */
783 #define SI_CPDMA_SKIP_BO_LIST_UPDATE (1 << 4) /* don't update the BO list */
784 #define SI_CPDMA_SKIP_ALL (SI_CPDMA_SKIP_CHECK_CS_SPACE | \
785 SI_CPDMA_SKIP_SYNC_AFTER | \
786 SI_CPDMA_SKIP_SYNC_BEFORE | \
787 SI_CPDMA_SKIP_GFX_SYNC | \
788 SI_CPDMA_SKIP_BO_LIST_UPDATE)
791 SI_COHERENCY_NONE
, /* no cache flushes needed */
793 SI_COHERENCY_CB_META
,
796 void si_clear_buffer(struct si_context
*sctx
, struct pipe_resource
*dst
,
797 uint64_t offset
, uint64_t size
, unsigned value
,
798 enum si_coherency coher
);
799 void si_copy_buffer(struct si_context
*sctx
,
800 struct pipe_resource
*dst
, struct pipe_resource
*src
,
801 uint64_t dst_offset
, uint64_t src_offset
, unsigned size
,
802 unsigned user_flags
);
803 void cik_prefetch_TC_L2_async(struct si_context
*sctx
, struct pipe_resource
*buf
,
804 uint64_t offset
, unsigned size
);
805 void cik_emit_prefetch_L2(struct si_context
*sctx
);
806 void si_init_cp_dma_functions(struct si_context
*sctx
);
809 void si_save_cs(struct radeon_winsys
*ws
, struct radeon_winsys_cs
*cs
,
810 struct radeon_saved_cs
*saved
, bool get_buffer_list
);
811 void si_clear_saved_cs(struct radeon_saved_cs
*saved
);
812 void si_destroy_saved_cs(struct si_saved_cs
*scs
);
813 void si_auto_log_cs(void *data
, struct u_log_context
*log
);
814 void si_log_hw_flush(struct si_context
*sctx
);
815 void si_log_draw_state(struct si_context
*sctx
, struct u_log_context
*log
);
816 void si_log_compute_state(struct si_context
*sctx
, struct u_log_context
*log
);
817 void si_init_debug_functions(struct si_context
*sctx
);
818 void si_check_vm_faults(struct si_context
*sctx
,
819 struct radeon_saved_cs
*saved
, enum ring_type ring
);
820 bool si_replace_shader(unsigned num
, struct ac_shader_binary
*binary
);
823 void si_init_dma_functions(struct si_context
*sctx
);
826 void si_need_dma_space(struct si_context
*ctx
, unsigned num_dw
,
827 struct r600_resource
*dst
, struct r600_resource
*src
);
828 void si_flush_dma_cs(struct si_context
*ctx
, unsigned flags
,
829 struct pipe_fence_handle
**fence
);
830 void si_screen_clear_buffer(struct si_screen
*sscreen
, struct pipe_resource
*dst
,
831 uint64_t offset
, uint64_t size
, unsigned value
);
834 void si_gfx_write_event_eop(struct si_context
*ctx
,
835 unsigned event
, unsigned event_flags
,
837 struct r600_resource
*buf
, uint64_t va
,
838 uint32_t new_fence
, unsigned query_type
);
839 unsigned si_gfx_write_fence_dwords(struct si_screen
*screen
);
840 void si_gfx_wait_fence(struct si_context
*ctx
,
841 uint64_t va
, uint32_t ref
, uint32_t mask
);
842 void si_init_fence_functions(struct si_context
*ctx
);
843 void si_init_screen_fence_functions(struct si_screen
*screen
);
844 struct pipe_fence_handle
*si_create_fence(struct pipe_context
*ctx
,
845 struct tc_unflushed_batch_token
*tc_token
);
848 const char *si_get_family_name(const struct si_screen
*sscreen
);
849 void si_init_screen_get_functions(struct si_screen
*sscreen
);
852 void si_flush_gfx_cs(struct si_context
*ctx
, unsigned flags
,
853 struct pipe_fence_handle
**fence
);
854 void si_begin_new_gfx_cs(struct si_context
*ctx
);
855 void si_need_gfx_cs_space(struct si_context
*ctx
);
857 /* r600_gpu_load.c */
858 void si_gpu_load_kill_thread(struct si_screen
*sscreen
);
859 uint64_t si_begin_counter(struct si_screen
*sscreen
, unsigned type
);
860 unsigned si_end_counter(struct si_screen
*sscreen
, unsigned type
,
864 void si_init_compute_functions(struct si_context
*sctx
);
866 /* si_perfcounters.c */
867 void si_init_perfcounters(struct si_screen
*screen
);
870 bool si_check_device_reset(struct si_context
*sctx
);
873 void si_init_screen_query_functions(struct si_screen
*sscreen
);
874 void si_init_query_functions(struct si_context
*sctx
);
875 void si_suspend_queries(struct si_context
*sctx
);
876 void si_resume_queries(struct si_context
*sctx
);
879 void si_test_dma(struct si_screen
*sscreen
);
882 struct pipe_video_codec
*si_uvd_create_decoder(struct pipe_context
*context
,
883 const struct pipe_video_codec
*templ
);
885 struct pipe_video_buffer
*si_video_buffer_create(struct pipe_context
*pipe
,
886 const struct pipe_video_buffer
*tmpl
);
889 void si_update_vs_viewport_state(struct si_context
*ctx
);
890 void si_init_viewport_functions(struct si_context
*ctx
);
893 bool si_prepare_for_dma_blit(struct si_context
*sctx
,
894 struct r600_texture
*rdst
,
895 unsigned dst_level
, unsigned dstx
,
896 unsigned dsty
, unsigned dstz
,
897 struct r600_texture
*rsrc
,
899 const struct pipe_box
*src_box
);
900 void si_texture_get_fmask_info(struct si_screen
*sscreen
,
901 struct r600_texture
*rtex
,
903 struct r600_fmask_info
*out
);
904 void si_texture_get_cmask_info(struct si_screen
*sscreen
,
905 struct r600_texture
*rtex
,
906 struct r600_cmask_info
*out
);
907 void si_eliminate_fast_color_clear(struct si_context
*sctx
,
908 struct r600_texture
*rtex
);
909 void si_texture_discard_cmask(struct si_screen
*sscreen
,
910 struct r600_texture
*rtex
);
911 bool si_init_flushed_depth_texture(struct pipe_context
*ctx
,
912 struct pipe_resource
*texture
,
913 struct r600_texture
**staging
);
914 void si_print_texture_info(struct si_screen
*sscreen
,
915 struct r600_texture
*rtex
, struct u_log_context
*log
);
916 struct pipe_resource
*si_texture_create(struct pipe_screen
*screen
,
917 const struct pipe_resource
*templ
);
918 bool vi_dcc_formats_compatible(enum pipe_format format1
,
919 enum pipe_format format2
);
920 bool vi_dcc_formats_are_incompatible(struct pipe_resource
*tex
,
922 enum pipe_format view_format
);
923 void vi_disable_dcc_if_incompatible_format(struct si_context
*sctx
,
924 struct pipe_resource
*tex
,
926 enum pipe_format view_format
);
927 struct pipe_surface
*si_create_surface_custom(struct pipe_context
*pipe
,
928 struct pipe_resource
*texture
,
929 const struct pipe_surface
*templ
,
930 unsigned width0
, unsigned height0
,
931 unsigned width
, unsigned height
);
932 unsigned si_translate_colorswap(enum pipe_format format
, bool do_endian_swap
);
933 void vi_separate_dcc_try_enable(struct si_context
*sctx
,
934 struct r600_texture
*tex
);
935 void vi_separate_dcc_start_query(struct si_context
*sctx
,
936 struct r600_texture
*tex
);
937 void vi_separate_dcc_stop_query(struct si_context
*sctx
,
938 struct r600_texture
*tex
);
939 void vi_separate_dcc_process_and_reset_stats(struct pipe_context
*ctx
,
940 struct r600_texture
*tex
);
941 bool si_texture_disable_dcc(struct si_context
*sctx
,
942 struct r600_texture
*rtex
);
943 void si_init_screen_texture_functions(struct si_screen
*sscreen
);
944 void si_init_context_texture_functions(struct si_context
*sctx
);
952 si_context_add_resource_size(struct si_context
*sctx
, struct pipe_resource
*r
)
954 struct r600_resource
*res
= (struct r600_resource
*)r
;
957 /* Add memory usage for need_gfx_cs_space */
958 sctx
->b
.vram
+= res
->vram_usage
;
959 sctx
->b
.gtt
+= res
->gart_usage
;
964 si_invalidate_draw_sh_constants(struct si_context
*sctx
)
966 sctx
->last_base_vertex
= SI_BASE_VERTEX_UNKNOWN
;
970 si_set_atom_dirty(struct si_context
*sctx
,
971 struct r600_atom
*atom
, bool dirty
)
973 unsigned bit
= 1 << atom
->id
;
976 sctx
->dirty_atoms
|= bit
;
978 sctx
->dirty_atoms
&= ~bit
;
982 si_is_atom_dirty(struct si_context
*sctx
,
983 struct r600_atom
*atom
)
985 unsigned bit
= 1 << atom
->id
;
987 return sctx
->dirty_atoms
& bit
;
991 si_mark_atom_dirty(struct si_context
*sctx
,
992 struct r600_atom
*atom
)
994 si_set_atom_dirty(sctx
, atom
, true);
997 static inline struct si_shader_ctx_state
*si_get_vs(struct si_context
*sctx
)
999 if (sctx
->gs_shader
.cso
)
1000 return &sctx
->gs_shader
;
1001 if (sctx
->tes_shader
.cso
)
1002 return &sctx
->tes_shader
;
1004 return &sctx
->vs_shader
;
1007 static inline struct tgsi_shader_info
*si_get_vs_info(struct si_context
*sctx
)
1009 struct si_shader_ctx_state
*vs
= si_get_vs(sctx
);
1011 return vs
->cso
? &vs
->cso
->info
: NULL
;
1014 static inline struct si_shader
* si_get_vs_state(struct si_context
*sctx
)
1016 if (sctx
->gs_shader
.cso
)
1017 return sctx
->gs_shader
.cso
->gs_copy_shader
;
1019 struct si_shader_ctx_state
*vs
= si_get_vs(sctx
);
1020 return vs
->current
? vs
->current
: NULL
;
1023 static inline bool si_can_dump_shader(struct si_screen
*sscreen
,
1026 return sscreen
->debug_flags
& (1 << processor
);
1029 static inline bool si_extra_shader_checks(struct si_screen
*sscreen
,
1032 return (sscreen
->debug_flags
& DBG(CHECK_IR
)) ||
1033 si_can_dump_shader(sscreen
, processor
);
1036 static inline bool si_get_strmout_en(struct si_context
*sctx
)
1038 return sctx
->streamout
.streamout_enabled
||
1039 sctx
->streamout
.prims_gen_query_enabled
;
1042 static inline unsigned
1043 si_optimal_tcc_alignment(struct si_context
*sctx
, unsigned upload_size
)
1045 unsigned alignment
, tcc_cache_line_size
;
1047 /* If the upload size is less than the cache line size (e.g. 16, 32),
1048 * the whole thing will fit into a cache line if we align it to its size.
1049 * The idea is that multiple small uploads can share a cache line.
1050 * If the upload size is greater, align it to the cache line size.
1052 alignment
= util_next_power_of_two(upload_size
);
1053 tcc_cache_line_size
= sctx
->screen
->info
.tcc_cache_line_size
;
1054 return MIN2(alignment
, tcc_cache_line_size
);
1058 si_saved_cs_reference(struct si_saved_cs
**dst
, struct si_saved_cs
*src
)
1060 if (pipe_reference(&(*dst
)->reference
, &src
->reference
))
1061 si_destroy_saved_cs(*dst
);
1067 si_make_CB_shader_coherent(struct si_context
*sctx
, unsigned num_samples
,
1068 bool shaders_read_metadata
)
1070 sctx
->b
.flags
|= SI_CONTEXT_FLUSH_AND_INV_CB
|
1071 SI_CONTEXT_INV_VMEM_L1
;
1073 if (sctx
->b
.chip_class
>= GFX9
) {
1074 /* Single-sample color is coherent with shaders on GFX9, but
1075 * L2 metadata must be flushed if shaders read metadata.
1078 if (num_samples
>= 2)
1079 sctx
->b
.flags
|= SI_CONTEXT_INV_GLOBAL_L2
;
1080 else if (shaders_read_metadata
)
1081 sctx
->b
.flags
|= SI_CONTEXT_INV_L2_METADATA
;
1084 sctx
->b
.flags
|= SI_CONTEXT_INV_GLOBAL_L2
;
1089 si_make_DB_shader_coherent(struct si_context
*sctx
, unsigned num_samples
,
1090 bool include_stencil
, bool shaders_read_metadata
)
1092 sctx
->b
.flags
|= SI_CONTEXT_FLUSH_AND_INV_DB
|
1093 SI_CONTEXT_INV_VMEM_L1
;
1095 if (sctx
->b
.chip_class
>= GFX9
) {
1096 /* Single-sample depth (not stencil) is coherent with shaders
1097 * on GFX9, but L2 metadata must be flushed if shaders read
1100 if (num_samples
>= 2 || include_stencil
)
1101 sctx
->b
.flags
|= SI_CONTEXT_INV_GLOBAL_L2
;
1102 else if (shaders_read_metadata
)
1103 sctx
->b
.flags
|= SI_CONTEXT_INV_L2_METADATA
;
1106 sctx
->b
.flags
|= SI_CONTEXT_INV_GLOBAL_L2
;
1111 si_can_sample_zs(struct r600_texture
*tex
, bool stencil_sampler
)
1113 return (stencil_sampler
&& tex
->can_sample_s
) ||
1114 (!stencil_sampler
&& tex
->can_sample_z
);
1118 si_htile_enabled(struct r600_texture
*tex
, unsigned level
)
1120 return tex
->htile_offset
&& level
== 0;
1124 vi_tc_compat_htile_enabled(struct r600_texture
*tex
, unsigned level
)
1126 assert(!tex
->tc_compatible_htile
|| tex
->htile_offset
);
1127 return tex
->tc_compatible_htile
&& level
== 0;
1130 static inline unsigned si_get_ps_iter_samples(struct si_context
*sctx
)
1132 if (sctx
->ps_uses_fbfetch
)
1133 return sctx
->framebuffer
.nr_samples
;
1135 return sctx
->ps_iter_samples
;
1138 static inline unsigned si_get_total_colormask(struct si_context
*sctx
)
1140 if (sctx
->queued
.named
.rasterizer
->rasterizer_discard
)
1143 struct si_shader_selector
*ps
= sctx
->ps_shader
.cso
;
1144 unsigned colormask
= sctx
->framebuffer
.colorbuf_enabled_4bit
&
1145 sctx
->queued
.named
.blend
->cb_target_mask
;
1147 if (!ps
->info
.properties
[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
])
1148 colormask
&= ps
->colors_written_4bit
;
1149 else if (!ps
->colors_written_4bit
)
1150 colormask
= 0; /* color0 writes all cbufs, but it's not written */
1156 * Return true if there is enough memory in VRAM and GTT for the buffers
1159 * \param vram VRAM memory size not added to the buffer list yet
1160 * \param gtt GTT memory size not added to the buffer list yet
1163 radeon_cs_memory_below_limit(struct si_screen
*screen
,
1164 struct radeon_winsys_cs
*cs
,
1165 uint64_t vram
, uint64_t gtt
)
1167 vram
+= cs
->used_vram
;
1168 gtt
+= cs
->used_gart
;
1170 /* Anything that goes above the VRAM size should go to GTT. */
1171 if (vram
> screen
->info
.vram_size
)
1172 gtt
+= vram
- screen
->info
.vram_size
;
1174 /* Now we just need to check if we have enough GTT. */
1175 return gtt
< screen
->info
.gart_size
* 0.7;
1179 * Add a buffer to the buffer list for the given command stream (CS).
1181 * All buffers used by a CS must be added to the list. This tells the kernel
1182 * driver which buffers are used by GPU commands. Other buffers can
1183 * be swapped out (not accessible) during execution.
1185 * The buffer list becomes empty after every context flush and must be
1188 static inline void radeon_add_to_buffer_list(struct si_context
*sctx
,
1189 struct radeon_winsys_cs
*cs
,
1190 struct r600_resource
*rbo
,
1191 enum radeon_bo_usage usage
,
1192 enum radeon_bo_priority priority
)
1195 sctx
->b
.ws
->cs_add_buffer(
1197 (enum radeon_bo_usage
)(usage
| RADEON_USAGE_SYNCHRONIZED
),
1198 rbo
->domains
, priority
);
1202 * Same as above, but also checks memory usage and flushes the context
1205 * When this SHOULD NOT be used:
1207 * - if si_context_add_resource_size has been called for the buffer
1208 * followed by *_need_cs_space for checking the memory usage
1210 * - if si_need_dma_space has been called for the buffer
1212 * - when emitting state packets and draw packets (because preceding packets
1213 * can't be re-emitted at that point)
1215 * - if shader resource "enabled_mask" is not up-to-date or there is
1216 * a different constraint disallowing a context flush
1219 radeon_add_to_gfx_buffer_list_check_mem(struct si_context
*sctx
,
1220 struct r600_resource
*rbo
,
1221 enum radeon_bo_usage usage
,
1222 enum radeon_bo_priority priority
,
1226 !radeon_cs_memory_below_limit(sctx
->screen
, sctx
->b
.gfx_cs
,
1227 sctx
->b
.vram
+ rbo
->vram_usage
,
1228 sctx
->b
.gtt
+ rbo
->gart_usage
))
1229 si_flush_gfx_cs(sctx
, PIPE_FLUSH_ASYNC
, NULL
);
1231 radeon_add_to_buffer_list(sctx
, sctx
->b
.gfx_cs
, rbo
, usage
, priority
);
1234 #define PRINT_ERR(fmt, args...) \
1235 fprintf(stderr, "EE %s:%d %s - " fmt, __FILE__, __LINE__, __func__, ##args)