radeonsi: move r600_query.c/h files to si_query.c/h
[mesa.git] / src / gallium / drivers / radeonsi / si_pipe.h
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 * Copyright 2018 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
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:
12 *
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
15 * Software.
16 *
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.
24 */
25 #ifndef SI_PIPE_H
26 #define SI_PIPE_H
27
28 #include "si_shader.h"
29
30 #include "util/u_dynarray.h"
31 #include "util/u_idalloc.h"
32
33 #ifdef PIPE_ARCH_BIG_ENDIAN
34 #define SI_BIG_ENDIAN 1
35 #else
36 #define SI_BIG_ENDIAN 0
37 #endif
38
39 #define ATI_VENDOR_ID 0x1002
40
41 #define SI_NOT_QUERY 0xffffffff
42
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
52
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)
81
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)
89
90 #define SI_MAX_BORDER_COLORS 4096
91 #define SI_MAX_VIEWPORTS 16
92 #define SIX_BITS 0x3F
93 #define SI_MAP_BUFFER_ALIGNMENT 64
94 #define SI_MAX_VARIABLE_THREADS_PER_BLOCK 1024
95
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)
103
104 /* Debug flags. */
105 enum {
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,
113 DBG_NO_IR,
114 DBG_NO_TGSI,
115 DBG_NO_ASM,
116 DBG_PREOPT_IR,
117
118 /* Shader compiler options the shader cache should be aware of: */
119 DBG_FS_CORRECT_DERIVS_AFTER_KILL,
120 DBG_UNSAFE_MATH,
121 DBG_SI_SCHED,
122
123 /* Shader compiler options (with no effect on the shader cache): */
124 DBG_CHECK_IR,
125 DBG_NIR,
126 DBG_MONOLITHIC_SHADERS,
127 DBG_NO_OPT_VARIANT,
128
129 /* Information logging options: */
130 DBG_INFO,
131 DBG_TEX,
132 DBG_COMPUTE,
133 DBG_VM,
134
135 /* Driver options: */
136 DBG_FORCE_DMA,
137 DBG_NO_ASYNC_DMA,
138 DBG_NO_WC,
139 DBG_CHECK_VM,
140 DBG_RESERVE_VMID,
141
142 /* 3D engine options: */
143 DBG_SWITCH_ON_EOP,
144 DBG_NO_OUT_OF_ORDER,
145 DBG_NO_DPBB,
146 DBG_NO_DFSM,
147 DBG_DPBB,
148 DBG_DFSM,
149 DBG_NO_HYPERZ,
150 DBG_NO_RB_PLUS,
151 DBG_NO_2D_TILING,
152 DBG_NO_TILING,
153 DBG_NO_DCC,
154 DBG_NO_DCC_CLEAR,
155 DBG_NO_DCC_FB,
156 DBG_NO_DCC_MSAA,
157 DBG_DCC_MSAA,
158 DBG_NO_FMASK,
159
160 /* Tests: */
161 DBG_TEST_DMA,
162 DBG_TEST_VMFAULT_CP,
163 DBG_TEST_VMFAULT_SDMA,
164 DBG_TEST_VMFAULT_SHADER,
165 };
166
167 #define DBG_ALL_SHADERS (((1 << (DBG_CS + 1)) - 1))
168 #define DBG(name) (1ull << DBG_##name)
169
170 struct si_compute;
171 struct hash_table;
172 struct u_suballocator;
173
174 struct si_screen {
175 struct pipe_screen b;
176 struct radeon_winsys *ws;
177 struct disk_cache *disk_shader_cache;
178
179 struct radeon_info info;
180 uint64_t debug_flags;
181 char renderer_string[100];
182
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;
197 bool dpbb_allowed;
198 bool dfsm_allowed;
199 bool llvm_has_working_vgpr_indexing;
200
201 /* Whether shaders are monolithic (1-part) or separate (3-part). */
202 bool use_monolithic_shaders;
203 bool record_llvm_ir;
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;
208
209 struct slab_parent_pool pool_transfers;
210
211 /* Texture filter settings. */
212 int force_aniso; /* -1 = disabled */
213
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;
218
219 /* This must be in the screen, because UE4 uses one context for
220 * compilation and another one for rendering.
221 */
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.
225 */
226 unsigned num_shaders_created;
227 unsigned num_shader_cache_hits;
228
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 */
234
235 /* Performance counters. */
236 struct si_perfcounters *perfcounters;
237
238 /* If pipe_screen wants to recompute and re-emit the framebuffer,
239 * sampler, and image states of all contexts, it should atomically
240 * increment this.
241 *
242 * Each context will compare this with its own last known value of
243 * the counter before drawing and re-emit the states accordingly.
244 */
245 unsigned dirty_tex_counter;
246
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.
250 */
251 unsigned compressed_colortex_counter;
252
253 struct {
254 /* Context flags to set so that all writes from earlier jobs
255 * in the CP are seen by L2 clients.
256 */
257 unsigned cp_to_L2;
258
259 /* Context flags to set so that all writes from earlier jobs
260 * that end in L2 are seen by CP.
261 */
262 unsigned L2_to_cp;
263 } barrier_flags;
264
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;
271
272 /* Shader cache in memory.
273 *
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
282 * those as well.
283 */
284 mtx_t shader_cache_mutex;
285 struct hash_table *shader_cache;
286
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 */
293
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 */
298 };
299
300 struct si_blend_color {
301 struct r600_atom atom;
302 struct pipe_blend_color state;
303 bool any_nonzeros;
304 };
305
306 struct si_sampler_view {
307 struct pipe_sampler_view base;
308 /* [0..7] = image descriptor
309 * [4..7] = buffer descriptor */
310 uint32_t state[8];
311 uint32_t fmask_state[8];
312 const struct legacy_surf_level *base_level_info;
313 ubyte base_level;
314 ubyte block_width;
315 bool is_stencil_sampler;
316 bool is_integer;
317 bool dcc_incompatible;
318 };
319
320 #define SI_SAMPLER_STATE_MAGIC 0x34f1c35a
321
322 struct si_sampler_state {
323 #ifdef DEBUG
324 unsigned magic;
325 #endif
326 uint32_t val[4];
327 uint32_t integer_val[4];
328 uint32_t upgraded_depth_val[4];
329 };
330
331 struct si_cs_shader_state {
332 struct si_compute *program;
333 struct si_compute *emitted_program;
334 unsigned offset;
335 bool initialized;
336 bool uses_scratch;
337 };
338
339 struct si_samplers {
340 struct pipe_sampler_view *views[SI_NUM_SAMPLERS];
341 struct si_sampler_state *sampler_states[SI_NUM_SAMPLERS];
342
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;
347 };
348
349 struct si_images {
350 struct pipe_image_view views[SI_NUM_IMAGES];
351 uint32_t needs_color_decompress_mask;
352 unsigned enabled_mask;
353 };
354
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;
367 ubyte color_is_int8;
368 ubyte color_is_int10;
369 ubyte dirty_cbufs;
370 bool dirty_zsbuf;
371 bool any_dst_linear;
372 bool CB_has_shader_readable_metadata;
373 bool DB_has_shader_readable_metadata;
374 };
375
376 struct si_signed_scissor {
377 int minx;
378 int miny;
379 int maxx;
380 int maxy;
381 };
382
383 struct si_scissors {
384 struct r600_atom atom;
385 unsigned dirty_mask;
386 struct pipe_scissor_state states[SI_MAX_VIEWPORTS];
387 };
388
389 struct si_viewports {
390 struct r600_atom atom;
391 unsigned dirty_mask;
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];
395 };
396
397 struct si_clip_state {
398 struct r600_atom atom;
399 struct pipe_clip_state state;
400 bool any_nonzeros;
401 };
402
403 struct si_sample_locs {
404 struct r600_atom atom;
405 unsigned nr_samples;
406 };
407
408 struct si_sample_mask {
409 struct r600_atom atom;
410 uint16_t sample_mask;
411 };
412
413 struct si_streamout_target {
414 struct pipe_stream_output_target b;
415
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;
420
421 unsigned stride_in_dw;
422 };
423
424 struct si_streamout {
425 struct r600_atom begin_atom;
426 bool begin_emitted;
427
428 unsigned enabled_mask;
429 unsigned num_targets;
430 struct si_streamout_target *targets[PIPE_MAX_SO_BUFFERS];
431
432 unsigned append_bitmask;
433 bool suspended;
434
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 */
439
440 /* The state of VGT_STRMOUT_BUFFER_(CONFIG|EN). */
441 unsigned hw_enabled_mask;
442
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;
448 };
449
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.
453 */
454 struct si_shader_ctx_state {
455 struct si_shader_selector *cso;
456 struct si_shader *current;
457 };
458
459 #define SI_NUM_VGT_PARAM_KEY_BITS 12
460 #define SI_NUM_VGT_PARAM_STATES (1 << SI_NUM_VGT_PARAM_KEY_BITS)
461
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.
464 */
465 union si_vgt_param_key {
466 struct {
467 unsigned prim:4;
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;
475 unsigned uses_gs:1;
476 unsigned _pad:32 - SI_NUM_VGT_PARAM_KEY_BITS;
477 } u;
478 uint32_t index;
479 };
480
481 struct si_texture_handle
482 {
483 unsigned desc_slot;
484 bool desc_dirty;
485 struct pipe_sampler_view *view;
486 struct si_sampler_state sstate;
487 };
488
489 struct si_image_handle
490 {
491 unsigned desc_slot;
492 bool desc_dirty;
493 struct pipe_image_view view;
494 };
495
496 struct si_saved_cs {
497 struct pipe_reference reference;
498 struct si_context *ctx;
499 struct radeon_saved_cs gfx;
500 struct r600_resource *trace_buf;
501 unsigned trace_id;
502
503 unsigned gfx_last_dw;
504 bool flushed;
505 int64_t time_flush;
506 };
507
508 struct si_context {
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;
516 void *vs_blit_pos;
517 void *vs_blit_pos_layered;
518 void *vs_blit_color;
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;
528
529 bool gfx_flush_in_progress:1;
530 bool compute_is_busy:1;
531
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;
539
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;
558
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];
564
565 /* shaders */
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;
572
573 /* shader information */
574 struct si_vertex_elements *vertex_elements;
575 unsigned sprite_coord_enable;
576 bool flatshade;
577 bool do_update_shaders;
578
579 /* vertex buffer descriptors */
580 uint32_t *vb_descriptors_gpu_list;
581 struct r600_resource *vb_descriptors_buffer;
582 unsigned vb_descriptors_offset;
583
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];
593
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];
605
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];
610
611 /* MSAA config state. */
612 int ps_iter_samples;
613 bool ps_uses_fbfetch;
614 bool smoothing_enabled;
615
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;
629
630 /* Emitted draw state. */
631 bool gs_tri_strip_adj_fix:1;
632 bool ls_vgpr_fix:1;
633 int last_index_size;
634 int last_base_vertex;
635 int last_start_instance;
636 int last_drawid;
637 int last_sh_base_reg;
638 int last_primitive_restart_en;
639 int last_restart_index;
640 int last_gs_out_prim;
641 int last_prim;
642 int last_multi_vgt_param;
643 int last_rast_prim;
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 */
648
649 /* Scratch buffer */
650 struct r600_atom scratch_state;
651 struct r600_resource *scratch_buffer;
652 unsigned scratch_waves;
653 unsigned spi_tmpring_size;
654
655 struct r600_resource *compute_scratch_buffer;
656
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;
665
666 /* Debug state. */
667 bool is_debug;
668 struct si_saved_cs *current_saved_cs;
669 uint64_t dmesg_timestamp;
670 unsigned apitrace_call_number;
671
672 /* Other state */
673 bool need_check_render_feedback;
674 bool decompression_enabled;
675
676 bool vs_writes_viewport_index;
677 bool vs_disables_clipping_viewport;
678
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];
682
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;
690
691 /* Allocated bindless handles */
692 struct hash_table *tex_handles;
693 struct hash_table *img_handles;
694
695 /* Resident bindless handles */
696 struct util_dynarray resident_tex_handles;
697 struct util_dynarray resident_img_handles;
698
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;
703
704 /* Bindless state */
705 bool uses_bindless_samplers;
706 bool uses_bindless_images;
707
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];
716 };
717
718 /* cik_sdma.c */
719 void cik_init_sdma_functions(struct si_context *sctx);
720
721 /* si_blit.c */
722 enum si_blitter_op /* bitmask */
723 {
724 SI_SAVE_TEXTURES = 1,
725 SI_SAVE_FRAMEBUFFER = 2,
726 SI_SAVE_FRAGMENT_STATE = 4,
727 SI_DISABLE_RENDER_COND = 8,
728 };
729
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,
736 unsigned dst_level,
737 unsigned dstx, unsigned dsty, unsigned dstz,
738 struct pipe_resource *src,
739 unsigned src_level,
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);
748
749 /* si_clear.c */
750 void vi_dcc_clear_level(struct si_context *sctx,
751 struct r600_texture *rtex,
752 unsigned level, unsigned clear_value);
753 void si_init_clear_functions(struct si_context *sctx);
754
755 /* si_cp_dma.c */
756 #define SI_CPDMA_SKIP_CHECK_CS_SPACE (1 << 0) /* don't call need_cs_space */
757 #define SI_CPDMA_SKIP_SYNC_AFTER (1 << 1) /* don't wait for DMA after the copy */
758 #define SI_CPDMA_SKIP_SYNC_BEFORE (1 << 2) /* don't wait for DMA before the copy (RAW hazards) */
759 #define SI_CPDMA_SKIP_GFX_SYNC (1 << 3) /* don't flush caches and don't wait for PS/CS */
760 #define SI_CPDMA_SKIP_BO_LIST_UPDATE (1 << 4) /* don't update the BO list */
761 #define SI_CPDMA_SKIP_ALL (SI_CPDMA_SKIP_CHECK_CS_SPACE | \
762 SI_CPDMA_SKIP_SYNC_AFTER | \
763 SI_CPDMA_SKIP_SYNC_BEFORE | \
764 SI_CPDMA_SKIP_GFX_SYNC | \
765 SI_CPDMA_SKIP_BO_LIST_UPDATE)
766
767 enum si_coherency {
768 SI_COHERENCY_NONE, /* no cache flushes needed */
769 SI_COHERENCY_SHADER,
770 SI_COHERENCY_CB_META,
771 };
772
773 void si_clear_buffer(struct si_context *sctx, struct pipe_resource *dst,
774 uint64_t offset, uint64_t size, unsigned value,
775 enum si_coherency coher);
776 void si_copy_buffer(struct si_context *sctx,
777 struct pipe_resource *dst, struct pipe_resource *src,
778 uint64_t dst_offset, uint64_t src_offset, unsigned size,
779 unsigned user_flags);
780 void cik_prefetch_TC_L2_async(struct si_context *sctx, struct pipe_resource *buf,
781 uint64_t offset, unsigned size);
782 void cik_emit_prefetch_L2(struct si_context *sctx);
783 void si_init_cp_dma_functions(struct si_context *sctx);
784
785 /* si_debug.c */
786 void si_save_cs(struct radeon_winsys *ws, struct radeon_winsys_cs *cs,
787 struct radeon_saved_cs *saved, bool get_buffer_list);
788 void si_clear_saved_cs(struct radeon_saved_cs *saved);
789 void si_destroy_saved_cs(struct si_saved_cs *scs);
790 void si_auto_log_cs(void *data, struct u_log_context *log);
791 void si_log_hw_flush(struct si_context *sctx);
792 void si_log_draw_state(struct si_context *sctx, struct u_log_context *log);
793 void si_log_compute_state(struct si_context *sctx, struct u_log_context *log);
794 void si_init_debug_functions(struct si_context *sctx);
795 void si_check_vm_faults(struct si_context *sctx,
796 struct radeon_saved_cs *saved, enum ring_type ring);
797 bool si_replace_shader(unsigned num, struct ac_shader_binary *binary);
798
799 /* si_dma.c */
800 void si_init_dma_functions(struct si_context *sctx);
801
802 /* si_dma_cs.c */
803 void si_need_dma_space(struct si_context *ctx, unsigned num_dw,
804 struct r600_resource *dst, struct r600_resource *src);
805 void si_flush_dma_cs(struct si_context *ctx, unsigned flags,
806 struct pipe_fence_handle **fence);
807 void si_screen_clear_buffer(struct si_screen *sscreen, struct pipe_resource *dst,
808 uint64_t offset, uint64_t size, unsigned value);
809
810 /* si_fence.c */
811 void si_gfx_write_event_eop(struct si_context *ctx,
812 unsigned event, unsigned event_flags,
813 unsigned data_sel,
814 struct r600_resource *buf, uint64_t va,
815 uint32_t new_fence, unsigned query_type);
816 unsigned si_gfx_write_fence_dwords(struct si_screen *screen);
817 void si_gfx_wait_fence(struct si_context *ctx,
818 uint64_t va, uint32_t ref, uint32_t mask);
819 void si_init_fence_functions(struct si_context *ctx);
820 void si_init_screen_fence_functions(struct si_screen *screen);
821 struct pipe_fence_handle *si_create_fence(struct pipe_context *ctx,
822 struct tc_unflushed_batch_token *tc_token);
823
824 /* si_get.c */
825 const char *si_get_family_name(const struct si_screen *sscreen);
826 void si_init_screen_get_functions(struct si_screen *sscreen);
827
828 /* si_gfx_cs.c */
829 void si_flush_gfx_cs(struct si_context *ctx, unsigned flags,
830 struct pipe_fence_handle **fence);
831 void si_begin_new_gfx_cs(struct si_context *ctx);
832 void si_need_gfx_cs_space(struct si_context *ctx);
833
834 /* si_compute.c */
835 void si_init_compute_functions(struct si_context *sctx);
836
837 /* si_perfcounters.c */
838 void si_init_perfcounters(struct si_screen *screen);
839
840 /* si_pipe.c */
841 bool si_check_device_reset(struct si_context *sctx);
842
843 /* si_query.c */
844 void si_init_screen_query_functions(struct si_screen *sscreen);
845 void si_init_query_functions(struct si_context *sctx);
846 void si_suspend_queries(struct si_context *sctx);
847 void si_resume_queries(struct si_context *sctx);
848
849 /* si_test_dma.c */
850 void si_test_dma(struct si_screen *sscreen);
851
852 /* si_uvd.c */
853 struct pipe_video_codec *si_uvd_create_decoder(struct pipe_context *context,
854 const struct pipe_video_codec *templ);
855
856 struct pipe_video_buffer *si_video_buffer_create(struct pipe_context *pipe,
857 const struct pipe_video_buffer *tmpl);
858
859 /* si_viewport.c */
860 void si_update_vs_viewport_state(struct si_context *ctx);
861 void si_init_viewport_functions(struct si_context *ctx);
862
863
864 /*
865 * common helpers
866 */
867
868 static inline void
869 si_context_add_resource_size(struct si_context *sctx, struct pipe_resource *r)
870 {
871 struct r600_resource *res = (struct r600_resource *)r;
872
873 if (res) {
874 /* Add memory usage for need_gfx_cs_space */
875 sctx->b.vram += res->vram_usage;
876 sctx->b.gtt += res->gart_usage;
877 }
878 }
879
880 static inline void
881 si_invalidate_draw_sh_constants(struct si_context *sctx)
882 {
883 sctx->last_base_vertex = SI_BASE_VERTEX_UNKNOWN;
884 }
885
886 static inline void
887 si_set_atom_dirty(struct si_context *sctx,
888 struct r600_atom *atom, bool dirty)
889 {
890 unsigned bit = 1 << atom->id;
891
892 if (dirty)
893 sctx->dirty_atoms |= bit;
894 else
895 sctx->dirty_atoms &= ~bit;
896 }
897
898 static inline bool
899 si_is_atom_dirty(struct si_context *sctx,
900 struct r600_atom *atom)
901 {
902 unsigned bit = 1 << atom->id;
903
904 return sctx->dirty_atoms & bit;
905 }
906
907 static inline void
908 si_mark_atom_dirty(struct si_context *sctx,
909 struct r600_atom *atom)
910 {
911 si_set_atom_dirty(sctx, atom, true);
912 }
913
914 static inline struct si_shader_ctx_state *si_get_vs(struct si_context *sctx)
915 {
916 if (sctx->gs_shader.cso)
917 return &sctx->gs_shader;
918 if (sctx->tes_shader.cso)
919 return &sctx->tes_shader;
920
921 return &sctx->vs_shader;
922 }
923
924 static inline struct tgsi_shader_info *si_get_vs_info(struct si_context *sctx)
925 {
926 struct si_shader_ctx_state *vs = si_get_vs(sctx);
927
928 return vs->cso ? &vs->cso->info : NULL;
929 }
930
931 static inline struct si_shader* si_get_vs_state(struct si_context *sctx)
932 {
933 if (sctx->gs_shader.cso)
934 return sctx->gs_shader.cso->gs_copy_shader;
935
936 struct si_shader_ctx_state *vs = si_get_vs(sctx);
937 return vs->current ? vs->current : NULL;
938 }
939
940 static inline bool si_can_dump_shader(struct si_screen *sscreen,
941 unsigned processor)
942 {
943 return sscreen->debug_flags & (1 << processor);
944 }
945
946 static inline bool si_extra_shader_checks(struct si_screen *sscreen,
947 unsigned processor)
948 {
949 return (sscreen->debug_flags & DBG(CHECK_IR)) ||
950 si_can_dump_shader(sscreen, processor);
951 }
952
953 static inline bool si_get_strmout_en(struct si_context *sctx)
954 {
955 return sctx->streamout.streamout_enabled ||
956 sctx->streamout.prims_gen_query_enabled;
957 }
958
959 static inline unsigned
960 si_optimal_tcc_alignment(struct si_context *sctx, unsigned upload_size)
961 {
962 unsigned alignment, tcc_cache_line_size;
963
964 /* If the upload size is less than the cache line size (e.g. 16, 32),
965 * the whole thing will fit into a cache line if we align it to its size.
966 * The idea is that multiple small uploads can share a cache line.
967 * If the upload size is greater, align it to the cache line size.
968 */
969 alignment = util_next_power_of_two(upload_size);
970 tcc_cache_line_size = sctx->screen->info.tcc_cache_line_size;
971 return MIN2(alignment, tcc_cache_line_size);
972 }
973
974 static inline void
975 si_saved_cs_reference(struct si_saved_cs **dst, struct si_saved_cs *src)
976 {
977 if (pipe_reference(&(*dst)->reference, &src->reference))
978 si_destroy_saved_cs(*dst);
979
980 *dst = src;
981 }
982
983 static inline void
984 si_make_CB_shader_coherent(struct si_context *sctx, unsigned num_samples,
985 bool shaders_read_metadata)
986 {
987 sctx->b.flags |= SI_CONTEXT_FLUSH_AND_INV_CB |
988 SI_CONTEXT_INV_VMEM_L1;
989
990 if (sctx->b.chip_class >= GFX9) {
991 /* Single-sample color is coherent with shaders on GFX9, but
992 * L2 metadata must be flushed if shaders read metadata.
993 * (DCC, CMASK).
994 */
995 if (num_samples >= 2)
996 sctx->b.flags |= SI_CONTEXT_INV_GLOBAL_L2;
997 else if (shaders_read_metadata)
998 sctx->b.flags |= SI_CONTEXT_INV_L2_METADATA;
999 } else {
1000 /* SI-CI-VI */
1001 sctx->b.flags |= SI_CONTEXT_INV_GLOBAL_L2;
1002 }
1003 }
1004
1005 static inline void
1006 si_make_DB_shader_coherent(struct si_context *sctx, unsigned num_samples,
1007 bool include_stencil, bool shaders_read_metadata)
1008 {
1009 sctx->b.flags |= SI_CONTEXT_FLUSH_AND_INV_DB |
1010 SI_CONTEXT_INV_VMEM_L1;
1011
1012 if (sctx->b.chip_class >= GFX9) {
1013 /* Single-sample depth (not stencil) is coherent with shaders
1014 * on GFX9, but L2 metadata must be flushed if shaders read
1015 * metadata.
1016 */
1017 if (num_samples >= 2 || include_stencil)
1018 sctx->b.flags |= SI_CONTEXT_INV_GLOBAL_L2;
1019 else if (shaders_read_metadata)
1020 sctx->b.flags |= SI_CONTEXT_INV_L2_METADATA;
1021 } else {
1022 /* SI-CI-VI */
1023 sctx->b.flags |= SI_CONTEXT_INV_GLOBAL_L2;
1024 }
1025 }
1026
1027 static inline bool
1028 si_can_sample_zs(struct r600_texture *tex, bool stencil_sampler)
1029 {
1030 return (stencil_sampler && tex->can_sample_s) ||
1031 (!stencil_sampler && tex->can_sample_z);
1032 }
1033
1034 static inline bool
1035 si_htile_enabled(struct r600_texture *tex, unsigned level)
1036 {
1037 return tex->htile_offset && level == 0;
1038 }
1039
1040 static inline bool
1041 vi_tc_compat_htile_enabled(struct r600_texture *tex, unsigned level)
1042 {
1043 assert(!tex->tc_compatible_htile || tex->htile_offset);
1044 return tex->tc_compatible_htile && level == 0;
1045 }
1046
1047 static inline unsigned si_get_ps_iter_samples(struct si_context *sctx)
1048 {
1049 if (sctx->ps_uses_fbfetch)
1050 return sctx->framebuffer.nr_samples;
1051
1052 return sctx->ps_iter_samples;
1053 }
1054
1055 static inline unsigned si_get_total_colormask(struct si_context *sctx)
1056 {
1057 if (sctx->queued.named.rasterizer->rasterizer_discard)
1058 return 0;
1059
1060 struct si_shader_selector *ps = sctx->ps_shader.cso;
1061 unsigned colormask = sctx->framebuffer.colorbuf_enabled_4bit &
1062 sctx->queued.named.blend->cb_target_mask;
1063
1064 if (!ps->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS])
1065 colormask &= ps->colors_written_4bit;
1066 else if (!ps->colors_written_4bit)
1067 colormask = 0; /* color0 writes all cbufs, but it's not written */
1068
1069 return colormask;
1070 }
1071
1072 /**
1073 * Return true if there is enough memory in VRAM and GTT for the buffers
1074 * added so far.
1075 *
1076 * \param vram VRAM memory size not added to the buffer list yet
1077 * \param gtt GTT memory size not added to the buffer list yet
1078 */
1079 static inline bool
1080 radeon_cs_memory_below_limit(struct si_screen *screen,
1081 struct radeon_winsys_cs *cs,
1082 uint64_t vram, uint64_t gtt)
1083 {
1084 vram += cs->used_vram;
1085 gtt += cs->used_gart;
1086
1087 /* Anything that goes above the VRAM size should go to GTT. */
1088 if (vram > screen->info.vram_size)
1089 gtt += vram - screen->info.vram_size;
1090
1091 /* Now we just need to check if we have enough GTT. */
1092 return gtt < screen->info.gart_size * 0.7;
1093 }
1094
1095 /**
1096 * Add a buffer to the buffer list for the given command stream (CS).
1097 *
1098 * All buffers used by a CS must be added to the list. This tells the kernel
1099 * driver which buffers are used by GPU commands. Other buffers can
1100 * be swapped out (not accessible) during execution.
1101 *
1102 * The buffer list becomes empty after every context flush and must be
1103 * rebuilt.
1104 */
1105 static inline void radeon_add_to_buffer_list(struct si_context *sctx,
1106 struct radeon_winsys_cs *cs,
1107 struct r600_resource *rbo,
1108 enum radeon_bo_usage usage,
1109 enum radeon_bo_priority priority)
1110 {
1111 assert(usage);
1112 sctx->b.ws->cs_add_buffer(
1113 cs, rbo->buf,
1114 (enum radeon_bo_usage)(usage | RADEON_USAGE_SYNCHRONIZED),
1115 rbo->domains, priority);
1116 }
1117
1118 /**
1119 * Same as above, but also checks memory usage and flushes the context
1120 * accordingly.
1121 *
1122 * When this SHOULD NOT be used:
1123 *
1124 * - if si_context_add_resource_size has been called for the buffer
1125 * followed by *_need_cs_space for checking the memory usage
1126 *
1127 * - if si_need_dma_space has been called for the buffer
1128 *
1129 * - when emitting state packets and draw packets (because preceding packets
1130 * can't be re-emitted at that point)
1131 *
1132 * - if shader resource "enabled_mask" is not up-to-date or there is
1133 * a different constraint disallowing a context flush
1134 */
1135 static inline void
1136 radeon_add_to_gfx_buffer_list_check_mem(struct si_context *sctx,
1137 struct r600_resource *rbo,
1138 enum radeon_bo_usage usage,
1139 enum radeon_bo_priority priority,
1140 bool check_mem)
1141 {
1142 if (check_mem &&
1143 !radeon_cs_memory_below_limit(sctx->screen, sctx->b.gfx_cs,
1144 sctx->b.vram + rbo->vram_usage,
1145 sctx->b.gtt + rbo->gart_usage))
1146 si_flush_gfx_cs(sctx, PIPE_FLUSH_ASYNC, NULL);
1147
1148 radeon_add_to_buffer_list(sctx, sctx->b.gfx_cs, rbo, usage, priority);
1149 }
1150
1151 #define PRINT_ERR(fmt, args...) \
1152 fprintf(stderr, "EE %s:%d %s - " fmt, __FILE__, __LINE__, __func__, ##args)
1153
1154 #endif