radeonsi/gfx10: add si_context::emit_cache_flush
[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 #include "si_state.h"
30
31 #include "util/u_dynarray.h"
32 #include "util/u_idalloc.h"
33 #include "util/u_threaded_context.h"
34
35 #ifdef PIPE_ARCH_BIG_ENDIAN
36 #define SI_BIG_ENDIAN 1
37 #else
38 #define SI_BIG_ENDIAN 0
39 #endif
40
41 #define ATI_VENDOR_ID 0x1002
42 #define SI_PRIM_DISCARD_DEBUG 0
43 #define SI_NOT_QUERY 0xffffffff
44
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
56
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
61
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)
92
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)
100
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
106
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)
117
118 enum si_clear_code
119 {
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,
126 };
127
128 #define SI_IMAGE_ACCESS_AS_BUFFER (1 << 7)
129
130 /* Debug flags. */
131 enum {
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,
139 DBG_NO_IR,
140 DBG_NO_TGSI,
141 DBG_NO_ASM,
142 DBG_PREOPT_IR,
143
144 /* Shader compiler options the shader cache should be aware of: */
145 DBG_FS_CORRECT_DERIVS_AFTER_KILL,
146 DBG_UNSAFE_MATH,
147 DBG_SI_SCHED,
148 DBG_GISEL,
149
150 /* Shader compiler options (with no effect on the shader cache): */
151 DBG_CHECK_IR,
152 DBG_MONOLITHIC_SHADERS,
153 DBG_NO_OPT_VARIANT,
154
155 /* Information logging options: */
156 DBG_INFO,
157 DBG_TEX,
158 DBG_COMPUTE,
159 DBG_VM,
160
161 /* Driver options: */
162 DBG_FORCE_DMA,
163 DBG_NO_ASYNC_DMA,
164 DBG_NO_WC,
165 DBG_CHECK_VM,
166 DBG_RESERVE_VMID,
167 DBG_ZERO_VRAM,
168
169 /* 3D engine options: */
170 DBG_ALWAYS_PD,
171 DBG_PD,
172 DBG_NO_PD,
173 DBG_SWITCH_ON_EOP,
174 DBG_NO_OUT_OF_ORDER,
175 DBG_NO_DPBB,
176 DBG_NO_DFSM,
177 DBG_DPBB,
178 DBG_DFSM,
179 DBG_NO_HYPERZ,
180 DBG_NO_RB_PLUS,
181 DBG_NO_2D_TILING,
182 DBG_NO_TILING,
183 DBG_NO_DCC,
184 DBG_NO_DCC_CLEAR,
185 DBG_NO_DCC_FB,
186 DBG_NO_DCC_MSAA,
187 DBG_NO_FMASK,
188
189 /* Tests: */
190 DBG_TEST_DMA,
191 DBG_TEST_VMFAULT_CP,
192 DBG_TEST_VMFAULT_SDMA,
193 DBG_TEST_VMFAULT_SHADER,
194 DBG_TEST_DMA_PERF,
195 DBG_TEST_GDS,
196 DBG_TEST_GDS_MM,
197 DBG_TEST_GDS_OA_MM,
198 };
199
200 #define DBG_ALL_SHADERS (((1 << (DBG_CS + 1)) - 1))
201 #define DBG(name) (1ull << DBG_##name)
202
203 enum si_cache_policy {
204 L2_BYPASS,
205 L2_STREAM, /* same as SLC=1 */
206 L2_LRU, /* same as SLC=0 */
207 };
208
209 enum si_coherency {
210 SI_COHERENCY_NONE, /* no cache flushes needed */
211 SI_COHERENCY_SHADER,
212 SI_COHERENCY_CB_META,
213 SI_COHERENCY_CP,
214 };
215
216 struct si_compute;
217 struct si_shader_context;
218 struct hash_table;
219 struct u_suballocator;
220
221 /* Only 32-bit buffer allocations are supported, gallium doesn't support more
222 * at the moment.
223 */
224 struct si_resource {
225 struct threaded_resource b;
226
227 /* Winsys objects. */
228 struct pb_buffer *buf;
229 uint64_t gpu_address;
230 /* Memory usage if the buffer placement is optimal. */
231 uint64_t vram_usage;
232 uint64_t gart_usage;
233
234 /* Resource properties. */
235 uint64_t bo_size;
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;
241
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.
245 *
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.
249 */
250 struct util_range valid_buffer_range;
251
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.
256 *
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
260 * use TC L2.
261 */
262 bool TC_L2_dirty;
263
264 /* Whether this resource is referenced by bindless handles. */
265 bool texture_handle_allocated;
266 bool image_handle_allocated;
267
268 /* Whether the resource has been exported via resource_get_handle. */
269 unsigned external_usage; /* PIPE_HANDLE_USAGE_* */
270 };
271
272 struct si_transfer {
273 struct threaded_transfer b;
274 struct si_resource *staging;
275 unsigned offset;
276 };
277
278 struct si_texture {
279 struct si_resource buffer;
280
281 struct radeon_surf surface;
282 uint64_t size;
283 struct si_texture *flushed_depth_texture;
284
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)
293 */
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;
305
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 */
317 bool is_depth:1;
318 bool db_compatible:1;
319 bool can_sample_z:1;
320 bool can_sample_s:1;
321
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.
327 */
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
332 * framebuffer.
333 */
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.
341 */
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).
349 */
350 unsigned ps_draw_ratio;
351 /* The number of clears since the last DCC usage analysis. */
352 unsigned num_slow_clears;
353 };
354
355 struct si_surface {
356 struct pipe_surface base;
357
358 /* These can vary with block-compressed textures. */
359 uint16_t width0;
360 uint16_t height0;
361
362 bool color_initialized:1;
363 bool depth_initialized:1;
364
365 /* Misc. color flags. */
366 bool color_is_int8:1;
367 bool color_is_int10:1;
368 bool dcc_incompatible:1;
369
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_color_attrib3; /* GFX10 and later */
376 unsigned cb_dcc_control; /* GFX8 and later */
377 unsigned spi_shader_col_format:8; /* no blending, no alpha-to-coverage. */
378 unsigned spi_shader_col_format_alpha:8; /* alpha-to-coverage */
379 unsigned spi_shader_col_format_blend:8; /* blending without alpha. */
380 unsigned spi_shader_col_format_blend_alpha:8; /* blending with alpha. */
381
382 /* DB registers. */
383 uint64_t db_depth_base; /* DB_Z_READ/WRITE_BASE */
384 uint64_t db_stencil_base;
385 uint64_t db_htile_data_base;
386 unsigned db_depth_info;
387 unsigned db_z_info;
388 unsigned db_z_info2; /* GFX9 only */
389 unsigned db_depth_view;
390 unsigned db_depth_size;
391 unsigned db_depth_slice;
392 unsigned db_stencil_info;
393 unsigned db_stencil_info2; /* GFX9 only */
394 unsigned db_htile_surface;
395 };
396
397 struct si_mmio_counter {
398 unsigned busy;
399 unsigned idle;
400 };
401
402 union si_mmio_counters {
403 struct {
404 /* For global GPU load including SDMA. */
405 struct si_mmio_counter gpu;
406
407 /* GRBM_STATUS */
408 struct si_mmio_counter spi;
409 struct si_mmio_counter gui;
410 struct si_mmio_counter ta;
411 struct si_mmio_counter gds;
412 struct si_mmio_counter vgt;
413 struct si_mmio_counter ia;
414 struct si_mmio_counter sx;
415 struct si_mmio_counter wd;
416 struct si_mmio_counter bci;
417 struct si_mmio_counter sc;
418 struct si_mmio_counter pa;
419 struct si_mmio_counter db;
420 struct si_mmio_counter cp;
421 struct si_mmio_counter cb;
422
423 /* SRBM_STATUS2 */
424 struct si_mmio_counter sdma;
425
426 /* CP_STAT */
427 struct si_mmio_counter pfp;
428 struct si_mmio_counter meq;
429 struct si_mmio_counter me;
430 struct si_mmio_counter surf_sync;
431 struct si_mmio_counter cp_dma;
432 struct si_mmio_counter scratch_ram;
433 } named;
434 unsigned array[0];
435 };
436
437 struct si_memory_object {
438 struct pipe_memory_object b;
439 struct pb_buffer *buf;
440 uint32_t stride;
441 };
442
443 /* Saved CS data for debugging features. */
444 struct radeon_saved_cs {
445 uint32_t *ib;
446 unsigned num_dw;
447
448 struct radeon_bo_list_item *bo_list;
449 unsigned bo_count;
450 };
451
452 struct si_screen {
453 struct pipe_screen b;
454 struct radeon_winsys *ws;
455 struct disk_cache *disk_shader_cache;
456
457 struct radeon_info info;
458 uint64_t debug_flags;
459 char renderer_string[183];
460
461 void (*make_texture_descriptor)(
462 struct si_screen *screen,
463 struct si_texture *tex,
464 bool sampler,
465 enum pipe_texture_target target,
466 enum pipe_format pipe_format,
467 const unsigned char state_swizzle[4],
468 unsigned first_level, unsigned last_level,
469 unsigned first_layer, unsigned last_layer,
470 unsigned width, unsigned height, unsigned depth,
471 uint32_t *state,
472 uint32_t *fmask_state);
473
474 unsigned pa_sc_raster_config;
475 unsigned pa_sc_raster_config_1;
476 unsigned se_tile_repeat;
477 unsigned gs_table_depth;
478 unsigned tess_offchip_block_dw_size;
479 unsigned tess_offchip_ring_size;
480 unsigned tess_factor_ring_size;
481 unsigned vgt_hs_offchip_param;
482 unsigned eqaa_force_coverage_samples;
483 unsigned eqaa_force_z_samples;
484 unsigned eqaa_force_color_samples;
485 bool has_clear_state;
486 bool has_distributed_tess;
487 bool has_draw_indirect_multi;
488 bool has_out_of_order_rast;
489 bool assume_no_z_fights;
490 bool commutative_blend_add;
491 bool has_gfx9_scissor_bug;
492 bool has_msaa_sample_loc_bug;
493 bool has_ls_vgpr_init_bug;
494 bool has_dcc_constant_encode;
495 bool dpbb_allowed;
496 bool dfsm_allowed;
497 bool llvm_has_working_vgpr_indexing;
498
499 struct {
500 #define OPT_BOOL(name, dflt, description) bool name:1;
501 #include "si_debug_options.h"
502 } options;
503
504 /* Whether shaders are monolithic (1-part) or separate (3-part). */
505 bool use_monolithic_shaders;
506 bool record_llvm_ir;
507 bool has_rbplus; /* if RB+ registers exist */
508 bool rbplus_allowed; /* if RB+ is allowed */
509 bool dcc_msaa_allowed;
510 bool cpdma_prefetch_writes_memory;
511
512 struct slab_parent_pool pool_transfers;
513
514 /* Texture filter settings. */
515 int force_aniso; /* -1 = disabled */
516
517 /* Auxiliary context. Mainly used to initialize resources.
518 * It must be locked prior to using and flushed before unlocking. */
519 struct pipe_context *aux_context;
520 mtx_t aux_context_lock;
521
522 /* This must be in the screen, because UE4 uses one context for
523 * compilation and another one for rendering.
524 */
525 unsigned num_compilations;
526 /* Along with ST_DEBUG=precompile, this should show if applications
527 * are loading shaders on demand. This is a monotonic counter.
528 */
529 unsigned num_shaders_created;
530 unsigned num_shader_cache_hits;
531
532 /* GPU load thread. */
533 mtx_t gpu_load_mutex;
534 thrd_t gpu_load_thread;
535 union si_mmio_counters mmio_counters;
536 volatile unsigned gpu_load_stop_thread; /* bool */
537
538 /* Performance counters. */
539 struct si_perfcounters *perfcounters;
540
541 /* If pipe_screen wants to recompute and re-emit the framebuffer,
542 * sampler, and image states of all contexts, it should atomically
543 * increment this.
544 *
545 * Each context will compare this with its own last known value of
546 * the counter before drawing and re-emit the states accordingly.
547 */
548 unsigned dirty_tex_counter;
549 unsigned dirty_buf_counter;
550
551 /* Atomically increment this counter when an existing texture's
552 * metadata is enabled or disabled in a way that requires changing
553 * contexts' compressed texture binding masks.
554 */
555 unsigned compressed_colortex_counter;
556
557 struct {
558 /* Context flags to set so that all writes from earlier jobs
559 * in the CP are seen by L2 clients.
560 */
561 unsigned cp_to_L2;
562
563 /* Context flags to set so that all writes from earlier jobs
564 * that end in L2 are seen by CP.
565 */
566 unsigned L2_to_cp;
567 } barrier_flags;
568
569 mtx_t shader_parts_mutex;
570 struct si_shader_part *vs_prologs;
571 struct si_shader_part *tcs_epilogs;
572 struct si_shader_part *gs_prologs;
573 struct si_shader_part *ps_prologs;
574 struct si_shader_part *ps_epilogs;
575
576 /* Shader cache in memory.
577 *
578 * Design & limitations:
579 * - The shader cache is per screen (= per process), never saved to
580 * disk, and skips redundant shader compilations from TGSI to bytecode.
581 * - It can only be used with one-variant-per-shader support, in which
582 * case only the main (typically middle) part of shaders is cached.
583 * - Only VS, TCS, TES, PS are cached, out of which only the hw VS
584 * variants of VS and TES are cached, so LS and ES aren't.
585 * - GS and CS aren't cached, but it's certainly possible to cache
586 * those as well.
587 */
588 mtx_t shader_cache_mutex;
589 struct hash_table *shader_cache;
590
591 /* Shader compiler queue for multithreaded compilation. */
592 struct util_queue shader_compiler_queue;
593 /* Use at most 3 normal compiler threads on quadcore and better.
594 * Hyperthreaded CPUs report the number of threads, but we want
595 * the number of cores. We only need this many threads for shader-db. */
596 struct ac_llvm_compiler compiler[24]; /* used by the queue only */
597
598 struct util_queue shader_compiler_queue_low_priority;
599 /* Use at most 2 low priority threads on quadcore and better.
600 * We want to minimize the impact on multithreaded Mesa. */
601 struct ac_llvm_compiler compiler_lowp[10];
602 };
603
604 struct si_blend_color {
605 struct pipe_blend_color state;
606 bool any_nonzeros;
607 };
608
609 struct si_sampler_view {
610 struct pipe_sampler_view base;
611 /* [0..7] = image descriptor
612 * [4..7] = buffer descriptor */
613 uint32_t state[8];
614 uint32_t fmask_state[8];
615 const struct legacy_surf_level *base_level_info;
616 ubyte base_level;
617 ubyte block_width;
618 bool is_stencil_sampler;
619 bool is_integer;
620 bool dcc_incompatible;
621 };
622
623 #define SI_SAMPLER_STATE_MAGIC 0x34f1c35a
624
625 struct si_sampler_state {
626 #ifndef NDEBUG
627 unsigned magic;
628 #endif
629 uint32_t val[4];
630 uint32_t integer_val[4];
631 uint32_t upgraded_depth_val[4];
632 };
633
634 struct si_cs_shader_state {
635 struct si_compute *program;
636 struct si_compute *emitted_program;
637 unsigned offset;
638 bool initialized;
639 bool uses_scratch;
640 };
641
642 struct si_samplers {
643 struct pipe_sampler_view *views[SI_NUM_SAMPLERS];
644 struct si_sampler_state *sampler_states[SI_NUM_SAMPLERS];
645
646 /* The i-th bit is set if that element is enabled (non-NULL resource). */
647 unsigned enabled_mask;
648 uint32_t needs_depth_decompress_mask;
649 uint32_t needs_color_decompress_mask;
650 };
651
652 struct si_images {
653 struct pipe_image_view views[SI_NUM_IMAGES];
654 uint32_t needs_color_decompress_mask;
655 unsigned enabled_mask;
656 };
657
658 struct si_framebuffer {
659 struct pipe_framebuffer_state state;
660 unsigned colorbuf_enabled_4bit;
661 unsigned spi_shader_col_format;
662 unsigned spi_shader_col_format_alpha;
663 unsigned spi_shader_col_format_blend;
664 unsigned spi_shader_col_format_blend_alpha;
665 ubyte nr_samples:5; /* at most 16xAA */
666 ubyte log_samples:3; /* at most 4 = 16xAA */
667 ubyte nr_color_samples; /* at most 8xAA */
668 ubyte compressed_cb_mask;
669 ubyte uncompressed_cb_mask;
670 ubyte color_is_int8;
671 ubyte color_is_int10;
672 ubyte dirty_cbufs;
673 ubyte dcc_overwrite_combiner_watermark;
674 bool dirty_zsbuf;
675 bool any_dst_linear;
676 bool CB_has_shader_readable_metadata;
677 bool DB_has_shader_readable_metadata;
678 bool all_DCC_pipe_aligned;
679 };
680
681 enum si_quant_mode {
682 /* This is the list we want to support. */
683 SI_QUANT_MODE_16_8_FIXED_POINT_1_256TH,
684 SI_QUANT_MODE_14_10_FIXED_POINT_1_1024TH,
685 SI_QUANT_MODE_12_12_FIXED_POINT_1_4096TH,
686 };
687
688 struct si_signed_scissor {
689 int minx;
690 int miny;
691 int maxx;
692 int maxy;
693 enum si_quant_mode quant_mode;
694 };
695
696 struct si_viewports {
697 struct pipe_viewport_state states[SI_MAX_VIEWPORTS];
698 struct si_signed_scissor as_scissor[SI_MAX_VIEWPORTS];
699 bool y_inverted;
700 };
701
702 struct si_clip_state {
703 struct pipe_clip_state state;
704 bool any_nonzeros;
705 };
706
707 struct si_streamout_target {
708 struct pipe_stream_output_target b;
709
710 /* The buffer where BUFFER_FILLED_SIZE is stored. */
711 struct si_resource *buf_filled_size;
712 unsigned buf_filled_size_offset;
713 bool buf_filled_size_valid;
714
715 unsigned stride_in_dw;
716 };
717
718 struct si_streamout {
719 bool begin_emitted;
720
721 unsigned enabled_mask;
722 unsigned num_targets;
723 struct si_streamout_target *targets[PIPE_MAX_SO_BUFFERS];
724
725 unsigned append_bitmask;
726 bool suspended;
727
728 /* External state which comes from the vertex shader,
729 * it must be set explicitly when binding a shader. */
730 uint16_t *stride_in_dw;
731 unsigned enabled_stream_buffers_mask; /* stream0 buffers0-3 in 4 LSB */
732
733 /* The state of VGT_STRMOUT_BUFFER_(CONFIG|EN). */
734 unsigned hw_enabled_mask;
735
736 /* The state of VGT_STRMOUT_(CONFIG|EN). */
737 bool streamout_enabled;
738 bool prims_gen_query_enabled;
739 int num_prims_gen_queries;
740 };
741
742 /* A shader state consists of the shader selector, which is a constant state
743 * object shared by multiple contexts and shouldn't be modified, and
744 * the current shader variant selected for this context.
745 */
746 struct si_shader_ctx_state {
747 struct si_shader_selector *cso;
748 struct si_shader *current;
749 };
750
751 #define SI_NUM_VGT_PARAM_KEY_BITS 12
752 #define SI_NUM_VGT_PARAM_STATES (1 << SI_NUM_VGT_PARAM_KEY_BITS)
753
754 /* The IA_MULTI_VGT_PARAM key used to index the table of precomputed values.
755 * Some fields are set by state-change calls, most are set by draw_vbo.
756 */
757 union si_vgt_param_key {
758 struct {
759 #ifdef PIPE_ARCH_LITTLE_ENDIAN
760 unsigned prim:4;
761 unsigned uses_instancing:1;
762 unsigned multi_instances_smaller_than_primgroup:1;
763 unsigned primitive_restart:1;
764 unsigned count_from_stream_output:1;
765 unsigned line_stipple_enabled:1;
766 unsigned uses_tess:1;
767 unsigned tess_uses_prim_id:1;
768 unsigned uses_gs:1;
769 unsigned _pad:32 - SI_NUM_VGT_PARAM_KEY_BITS;
770 #else /* PIPE_ARCH_BIG_ENDIAN */
771 unsigned _pad:32 - SI_NUM_VGT_PARAM_KEY_BITS;
772 unsigned uses_gs:1;
773 unsigned tess_uses_prim_id:1;
774 unsigned uses_tess:1;
775 unsigned line_stipple_enabled:1;
776 unsigned count_from_stream_output:1;
777 unsigned primitive_restart:1;
778 unsigned multi_instances_smaller_than_primgroup:1;
779 unsigned uses_instancing:1;
780 unsigned prim:4;
781 #endif
782 } u;
783 uint32_t index;
784 };
785
786 #define SI_NUM_VGT_STAGES_KEY_BITS 2
787 #define SI_NUM_VGT_STAGES_STATES (1 << SI_NUM_VGT_STAGES_KEY_BITS)
788
789 /* The VGT_SHADER_STAGES key used to index the table of precomputed values.
790 * Some fields are set by state-change calls, most are set by draw_vbo.
791 */
792 union si_vgt_stages_key {
793 struct {
794 #ifdef PIPE_ARCH_LITTLE_ENDIAN
795 unsigned tess:1;
796 unsigned gs:1;
797 unsigned _pad:32 - SI_NUM_VGT_STAGES_KEY_BITS;
798 #else /* PIPE_ARCH_BIG_ENDIAN */
799 unsigned _pad:32 - SI_NUM_VGT_STAGES_KEY_BITS;
800 unsigned gs:1;
801 unsigned tess:1;
802 #endif
803 } u;
804 uint32_t index;
805 };
806
807 struct si_texture_handle
808 {
809 unsigned desc_slot;
810 bool desc_dirty;
811 struct pipe_sampler_view *view;
812 struct si_sampler_state sstate;
813 };
814
815 struct si_image_handle
816 {
817 unsigned desc_slot;
818 bool desc_dirty;
819 struct pipe_image_view view;
820 };
821
822 struct si_saved_cs {
823 struct pipe_reference reference;
824 struct si_context *ctx;
825 struct radeon_saved_cs gfx;
826 struct radeon_saved_cs compute;
827 struct si_resource *trace_buf;
828 unsigned trace_id;
829
830 unsigned gfx_last_dw;
831 unsigned compute_last_dw;
832 bool flushed;
833 int64_t time_flush;
834 };
835
836 struct si_sdma_upload {
837 struct si_resource *dst;
838 struct si_resource *src;
839 unsigned src_offset;
840 unsigned dst_offset;
841 unsigned size;
842 };
843
844 struct si_context {
845 struct pipe_context b; /* base class */
846
847 enum radeon_family family;
848 enum chip_class chip_class;
849
850 struct radeon_winsys *ws;
851 struct radeon_winsys_ctx *ctx;
852 struct radeon_cmdbuf *gfx_cs; /* compute IB if graphics is disabled */
853 struct radeon_cmdbuf *dma_cs;
854 struct pipe_fence_handle *last_gfx_fence;
855 struct pipe_fence_handle *last_sdma_fence;
856 struct si_resource *eop_bug_scratch;
857 struct u_upload_mgr *cached_gtt_allocator;
858 struct threaded_context *tc;
859 struct u_suballocator *allocator_zeroed_memory;
860 struct slab_child_pool pool_transfers;
861 struct slab_child_pool pool_transfers_unsync; /* for threaded_context */
862 struct pipe_device_reset_callback device_reset_callback;
863 struct u_log_context *log;
864 void *query_result_shader;
865
866 void (*emit_cache_flush)(struct si_context *ctx);
867
868 struct blitter_context *blitter;
869 void *custom_dsa_flush;
870 void *custom_blend_resolve;
871 void *custom_blend_fmask_decompress;
872 void *custom_blend_eliminate_fastclear;
873 void *custom_blend_dcc_decompress;
874 void *vs_blit_pos;
875 void *vs_blit_pos_layered;
876 void *vs_blit_color;
877 void *vs_blit_color_layered;
878 void *vs_blit_texcoord;
879 void *cs_clear_buffer;
880 void *cs_copy_buffer;
881 void *cs_copy_image;
882 void *cs_copy_image_1d_array;
883 void *cs_clear_render_target;
884 void *cs_clear_render_target_1d_array;
885 void *cs_dcc_retile;
886 struct si_screen *screen;
887 struct pipe_debug_callback debug;
888 struct ac_llvm_compiler compiler; /* only non-threaded compilation */
889 struct si_shader_ctx_state fixed_func_tcs_shader;
890 /* Offset 0: EOP flush number; Offset 4: GDS prim restart counter */
891 struct si_resource *wait_mem_scratch;
892 unsigned wait_mem_number;
893 uint16_t prefetch_L2_mask;
894
895 bool has_graphics;
896 bool gfx_flush_in_progress:1;
897 bool gfx_last_ib_is_busy:1;
898 bool compute_is_busy:1;
899
900 unsigned num_gfx_cs_flushes;
901 unsigned initial_gfx_cs_size;
902 unsigned last_dirty_tex_counter;
903 unsigned last_dirty_buf_counter;
904 unsigned last_compressed_colortex_counter;
905 unsigned last_num_draw_calls;
906 unsigned flags; /* flush flags */
907 /* Current unaccounted memory usage. */
908 uint64_t vram;
909 uint64_t gtt;
910
911 /* Compute-based primitive discard. */
912 unsigned prim_discard_vertex_count_threshold;
913 struct pb_buffer *gds;
914 struct pb_buffer *gds_oa;
915 struct radeon_cmdbuf *prim_discard_compute_cs;
916 unsigned compute_gds_offset;
917 struct si_shader *compute_ib_last_shader;
918 uint32_t compute_rewind_va;
919 unsigned compute_num_prims_in_batch;
920 bool preserve_prim_restart_gds_at_flush;
921 /* index_ring is divided into 2 halves for doublebuffering. */
922 struct si_resource *index_ring;
923 unsigned index_ring_base; /* offset of a per-IB portion */
924 unsigned index_ring_offset; /* offset within a per-IB portion */
925 unsigned index_ring_size_per_ib; /* max available size per IB */
926 bool prim_discard_compute_ib_initialized;
927 /* For tracking the last execution barrier - it can be either
928 * a WRITE_DATA packet or a fence. */
929 uint32_t *last_pkt3_write_data;
930 struct si_resource *barrier_buf;
931 unsigned barrier_buf_offset;
932 struct pipe_fence_handle *last_ib_barrier_fence;
933 struct si_resource *last_ib_barrier_buf;
934 unsigned last_ib_barrier_buf_offset;
935
936 /* Atoms (direct states). */
937 union si_state_atoms atoms;
938 unsigned dirty_atoms; /* mask */
939 /* PM4 states (precomputed immutable states) */
940 unsigned dirty_states;
941 union si_state queued;
942 union si_state emitted;
943
944 /* Atom declarations. */
945 struct si_framebuffer framebuffer;
946 unsigned sample_locs_num_samples;
947 uint16_t sample_mask;
948 unsigned last_cb_target_mask;
949 struct si_blend_color blend_color;
950 struct si_clip_state clip_state;
951 struct si_shader_data shader_pointers;
952 struct si_stencil_ref stencil_ref;
953 struct pipe_scissor_state scissors[SI_MAX_VIEWPORTS];
954 struct si_streamout streamout;
955 struct si_viewports viewports;
956 unsigned num_window_rectangles;
957 bool window_rectangles_include;
958 struct pipe_scissor_state window_rectangles[4];
959
960 /* Precomputed states. */
961 struct si_pm4_state *init_config;
962 struct si_pm4_state *init_config_gs_rings;
963 bool init_config_has_vgt_flush;
964 struct si_pm4_state *vgt_shader_config[SI_NUM_VGT_STAGES_STATES];
965
966 /* shaders */
967 struct si_shader_ctx_state ps_shader;
968 struct si_shader_ctx_state gs_shader;
969 struct si_shader_ctx_state vs_shader;
970 struct si_shader_ctx_state tcs_shader;
971 struct si_shader_ctx_state tes_shader;
972 struct si_shader_ctx_state cs_prim_discard_state;
973 struct si_cs_shader_state cs_shader_state;
974
975 /* shader information */
976 struct si_vertex_elements *vertex_elements;
977 unsigned sprite_coord_enable;
978 unsigned cs_max_waves_per_sh;
979 bool flatshade;
980 bool do_update_shaders;
981
982 /* vertex buffer descriptors */
983 uint32_t *vb_descriptors_gpu_list;
984 struct si_resource *vb_descriptors_buffer;
985 unsigned vb_descriptors_offset;
986
987 /* shader descriptors */
988 struct si_descriptors descriptors[SI_NUM_DESCS];
989 unsigned descriptors_dirty;
990 unsigned shader_pointers_dirty;
991 unsigned shader_needs_decompress_mask;
992 struct si_buffer_resources rw_buffers;
993 struct si_buffer_resources const_and_shader_buffers[SI_NUM_SHADERS];
994 struct si_samplers samplers[SI_NUM_SHADERS];
995 struct si_images images[SI_NUM_SHADERS];
996 bool bo_list_add_all_resident_resources;
997 bool bo_list_add_all_gfx_resources;
998 bool bo_list_add_all_compute_resources;
999
1000 /* other shader resources */
1001 struct pipe_constant_buffer null_const_buf; /* used for set_constant_buffer(NULL) on GFX7 */
1002 struct pipe_resource *esgs_ring;
1003 struct pipe_resource *gsvs_ring;
1004 struct pipe_resource *tess_rings;
1005 union pipe_color_union *border_color_table; /* in CPU memory, any endian */
1006 struct si_resource *border_color_buffer;
1007 union pipe_color_union *border_color_map; /* in VRAM (slow access), little endian */
1008 unsigned border_color_count;
1009 unsigned num_vs_blit_sgprs;
1010 uint32_t vs_blit_sh_data[SI_VS_BLIT_SGPRS_POS_TEXCOORD];
1011 uint32_t cs_user_data[4];
1012
1013 /* Vertex and index buffers. */
1014 bool vertex_buffers_dirty;
1015 bool vertex_buffer_pointer_dirty;
1016 struct pipe_vertex_buffer vertex_buffer[SI_NUM_VERTEX_BUFFERS];
1017 uint16_t vertex_buffer_unaligned; /* bitmask of not dword-aligned buffers */
1018
1019 /* MSAA config state. */
1020 int ps_iter_samples;
1021 bool ps_uses_fbfetch;
1022 bool smoothing_enabled;
1023
1024 /* DB render state. */
1025 unsigned ps_db_shader_control;
1026 unsigned dbcb_copy_sample;
1027 bool dbcb_depth_copy_enabled:1;
1028 bool dbcb_stencil_copy_enabled:1;
1029 bool db_flush_depth_inplace:1;
1030 bool db_flush_stencil_inplace:1;
1031 bool db_depth_clear:1;
1032 bool db_depth_disable_expclear:1;
1033 bool db_stencil_clear:1;
1034 bool db_stencil_disable_expclear:1;
1035 bool occlusion_queries_disabled:1;
1036 bool generate_mipmap_for_depth:1;
1037
1038 /* Emitted draw state. */
1039 bool gs_tri_strip_adj_fix:1;
1040 bool ls_vgpr_fix:1;
1041 bool prim_discard_cs_instancing:1;
1042 int last_index_size;
1043 int last_base_vertex;
1044 int last_start_instance;
1045 int last_instance_count;
1046 int last_drawid;
1047 int last_sh_base_reg;
1048 int last_primitive_restart_en;
1049 int last_restart_index;
1050 int last_prim;
1051 int last_multi_vgt_param;
1052 int last_rast_prim;
1053 unsigned last_sc_line_stipple;
1054 unsigned current_vs_state;
1055 unsigned last_vs_state;
1056 enum pipe_prim_type current_rast_prim; /* primitive type after TES, GS */
1057
1058 /* Scratch buffer */
1059 struct si_resource *scratch_buffer;
1060 unsigned scratch_waves;
1061 unsigned spi_tmpring_size;
1062
1063 struct si_resource *compute_scratch_buffer;
1064
1065 /* Emitted derived tessellation state. */
1066 /* Local shader (VS), or HS if LS-HS are merged. */
1067 struct si_shader *last_ls;
1068 struct si_shader_selector *last_tcs;
1069 int last_num_tcs_input_cp;
1070 int last_tes_sh_base;
1071 bool last_tess_uses_primid;
1072 unsigned last_num_patches;
1073 int last_ls_hs_config;
1074
1075 /* Debug state. */
1076 bool is_debug;
1077 struct si_saved_cs *current_saved_cs;
1078 uint64_t dmesg_timestamp;
1079 unsigned apitrace_call_number;
1080
1081 /* Other state */
1082 bool need_check_render_feedback;
1083 bool decompression_enabled;
1084 bool dpbb_force_off;
1085 bool vs_writes_viewport_index;
1086 bool vs_disables_clipping_viewport;
1087
1088 /* Precomputed IA_MULTI_VGT_PARAM */
1089 union si_vgt_param_key ia_multi_vgt_param_key;
1090 unsigned ia_multi_vgt_param[SI_NUM_VGT_PARAM_STATES];
1091
1092 /* Bindless descriptors. */
1093 struct si_descriptors bindless_descriptors;
1094 struct util_idalloc bindless_used_slots;
1095 unsigned num_bindless_descriptors;
1096 bool bindless_descriptors_dirty;
1097 bool graphics_bindless_pointer_dirty;
1098 bool compute_bindless_pointer_dirty;
1099
1100 /* Allocated bindless handles */
1101 struct hash_table *tex_handles;
1102 struct hash_table *img_handles;
1103
1104 /* Resident bindless handles */
1105 struct util_dynarray resident_tex_handles;
1106 struct util_dynarray resident_img_handles;
1107
1108 /* Resident bindless handles which need decompression */
1109 struct util_dynarray resident_tex_needs_color_decompress;
1110 struct util_dynarray resident_img_needs_color_decompress;
1111 struct util_dynarray resident_tex_needs_depth_decompress;
1112
1113 /* Bindless state */
1114 bool uses_bindless_samplers;
1115 bool uses_bindless_images;
1116
1117 /* MSAA sample locations.
1118 * The first index is the sample index.
1119 * The second index is the coordinate: X, Y. */
1120 struct {
1121 float x1[1][2];
1122 float x2[2][2];
1123 float x4[4][2];
1124 float x8[8][2];
1125 float x16[16][2];
1126 } sample_positions;
1127 struct pipe_resource *sample_pos_buffer;
1128
1129 /* Misc stats. */
1130 unsigned num_draw_calls;
1131 unsigned num_decompress_calls;
1132 unsigned num_mrt_draw_calls;
1133 unsigned num_prim_restart_calls;
1134 unsigned num_spill_draw_calls;
1135 unsigned num_compute_calls;
1136 unsigned num_spill_compute_calls;
1137 unsigned num_dma_calls;
1138 unsigned num_cp_dma_calls;
1139 unsigned num_vs_flushes;
1140 unsigned num_ps_flushes;
1141 unsigned num_cs_flushes;
1142 unsigned num_cb_cache_flushes;
1143 unsigned num_db_cache_flushes;
1144 unsigned num_L2_invalidates;
1145 unsigned num_L2_writebacks;
1146 unsigned num_resident_handles;
1147 uint64_t num_alloc_tex_transfer_bytes;
1148 unsigned last_tex_ps_draw_ratio; /* for query */
1149 unsigned compute_num_verts_accepted;
1150 unsigned compute_num_verts_rejected;
1151 unsigned compute_num_verts_ineligible; /* due to low vertex count */
1152 unsigned context_roll;
1153
1154 /* Queries. */
1155 /* Maintain the list of active queries for pausing between IBs. */
1156 int num_occlusion_queries;
1157 int num_perfect_occlusion_queries;
1158 int num_pipeline_stat_queries;
1159 struct list_head active_queries;
1160 unsigned num_cs_dw_queries_suspend;
1161
1162 /* Render condition. */
1163 struct pipe_query *render_cond;
1164 unsigned render_cond_mode;
1165 bool render_cond_invert;
1166 bool render_cond_force_off; /* for u_blitter */
1167
1168 /* For uploading data via GTT and copy to VRAM on context flush via SDMA. */
1169 bool sdma_uploads_in_progress;
1170 struct si_sdma_upload *sdma_uploads;
1171 unsigned num_sdma_uploads;
1172 unsigned max_sdma_uploads;
1173
1174 /* Statistics gathering for the DCC enablement heuristic. It can't be
1175 * in si_texture because si_texture can be shared by multiple
1176 * contexts. This is for back buffers only. We shouldn't get too many
1177 * of those.
1178 *
1179 * X11 DRI3 rotates among a finite set of back buffers. They should
1180 * all fit in this array. If they don't, separate DCC might never be
1181 * enabled by DCC stat gathering.
1182 */
1183 struct {
1184 struct si_texture *tex;
1185 /* Query queue: 0 = usually active, 1 = waiting, 2 = readback. */
1186 struct pipe_query *ps_stats[3];
1187 /* If all slots are used and another slot is needed,
1188 * the least recently used slot is evicted based on this. */
1189 int64_t last_use_timestamp;
1190 bool query_active;
1191 } dcc_stats[5];
1192
1193 /* Copy one resource to another using async DMA. */
1194 void (*dma_copy)(struct pipe_context *ctx,
1195 struct pipe_resource *dst,
1196 unsigned dst_level,
1197 unsigned dst_x, unsigned dst_y, unsigned dst_z,
1198 struct pipe_resource *src,
1199 unsigned src_level,
1200 const struct pipe_box *src_box);
1201
1202 struct si_tracked_regs tracked_regs;
1203 };
1204
1205 /* cik_sdma.c */
1206 void cik_init_sdma_functions(struct si_context *sctx);
1207
1208 /* si_blit.c */
1209 enum si_blitter_op /* bitmask */
1210 {
1211 SI_SAVE_TEXTURES = 1,
1212 SI_SAVE_FRAMEBUFFER = 2,
1213 SI_SAVE_FRAGMENT_STATE = 4,
1214 SI_DISABLE_RENDER_COND = 8,
1215 };
1216
1217 void si_blitter_begin(struct si_context *sctx, enum si_blitter_op op);
1218 void si_blitter_end(struct si_context *sctx);
1219 void si_init_blit_functions(struct si_context *sctx);
1220 void si_decompress_textures(struct si_context *sctx, unsigned shader_mask);
1221 void si_resource_copy_region(struct pipe_context *ctx,
1222 struct pipe_resource *dst,
1223 unsigned dst_level,
1224 unsigned dstx, unsigned dsty, unsigned dstz,
1225 struct pipe_resource *src,
1226 unsigned src_level,
1227 const struct pipe_box *src_box);
1228 void si_decompress_dcc(struct si_context *sctx, struct si_texture *tex);
1229
1230 /* si_buffer.c */
1231 bool si_rings_is_buffer_referenced(struct si_context *sctx,
1232 struct pb_buffer *buf,
1233 enum radeon_bo_usage usage);
1234 void *si_buffer_map_sync_with_rings(struct si_context *sctx,
1235 struct si_resource *resource,
1236 unsigned usage);
1237 void si_init_resource_fields(struct si_screen *sscreen,
1238 struct si_resource *res,
1239 uint64_t size, unsigned alignment);
1240 bool si_alloc_resource(struct si_screen *sscreen,
1241 struct si_resource *res);
1242 struct pipe_resource *pipe_aligned_buffer_create(struct pipe_screen *screen,
1243 unsigned flags, unsigned usage,
1244 unsigned size, unsigned alignment);
1245 struct si_resource *si_aligned_buffer_create(struct pipe_screen *screen,
1246 unsigned flags, unsigned usage,
1247 unsigned size, unsigned alignment);
1248 void si_replace_buffer_storage(struct pipe_context *ctx,
1249 struct pipe_resource *dst,
1250 struct pipe_resource *src);
1251 void si_init_screen_buffer_functions(struct si_screen *sscreen);
1252 void si_init_buffer_functions(struct si_context *sctx);
1253
1254 /* si_clear.c */
1255 enum pipe_format si_simplify_cb_format(enum pipe_format format);
1256 bool vi_alpha_is_on_msb(enum pipe_format format);
1257 void vi_dcc_clear_level(struct si_context *sctx,
1258 struct si_texture *tex,
1259 unsigned level, unsigned clear_value);
1260 void si_init_clear_functions(struct si_context *sctx);
1261
1262 /* si_compute_blit.c */
1263 unsigned si_get_flush_flags(struct si_context *sctx, enum si_coherency coher,
1264 enum si_cache_policy cache_policy);
1265 void si_clear_buffer(struct si_context *sctx, struct pipe_resource *dst,
1266 uint64_t offset, uint64_t size, uint32_t *clear_value,
1267 uint32_t clear_value_size, enum si_coherency coher,
1268 bool force_cpdma);
1269 void si_copy_buffer(struct si_context *sctx,
1270 struct pipe_resource *dst, struct pipe_resource *src,
1271 uint64_t dst_offset, uint64_t src_offset, unsigned size);
1272 void si_compute_copy_image(struct si_context *sctx,
1273 struct pipe_resource *dst,
1274 unsigned dst_level,
1275 struct pipe_resource *src,
1276 unsigned src_level,
1277 unsigned dstx, unsigned dsty, unsigned dstz,
1278 const struct pipe_box *src_box);
1279 void si_compute_clear_render_target(struct pipe_context *ctx,
1280 struct pipe_surface *dstsurf,
1281 const union pipe_color_union *color,
1282 unsigned dstx, unsigned dsty,
1283 unsigned width, unsigned height,
1284 bool render_condition_enabled);
1285 void si_retile_dcc(struct si_context *sctx, struct si_texture *tex);
1286 void si_init_compute_blit_functions(struct si_context *sctx);
1287
1288 /* si_cp_dma.c */
1289 #define SI_CPDMA_SKIP_CHECK_CS_SPACE (1 << 0) /* don't call need_cs_space */
1290 #define SI_CPDMA_SKIP_SYNC_AFTER (1 << 1) /* don't wait for DMA after the copy */
1291 #define SI_CPDMA_SKIP_SYNC_BEFORE (1 << 2) /* don't wait for DMA before the copy (RAW hazards) */
1292 #define SI_CPDMA_SKIP_GFX_SYNC (1 << 3) /* don't flush caches and don't wait for PS/CS */
1293 #define SI_CPDMA_SKIP_BO_LIST_UPDATE (1 << 4) /* don't update the BO list */
1294 #define SI_CPDMA_SKIP_ALL (SI_CPDMA_SKIP_CHECK_CS_SPACE | \
1295 SI_CPDMA_SKIP_SYNC_AFTER | \
1296 SI_CPDMA_SKIP_SYNC_BEFORE | \
1297 SI_CPDMA_SKIP_GFX_SYNC | \
1298 SI_CPDMA_SKIP_BO_LIST_UPDATE)
1299
1300 void si_cp_dma_wait_for_idle(struct si_context *sctx);
1301 void si_cp_dma_clear_buffer(struct si_context *sctx, struct radeon_cmdbuf *cs,
1302 struct pipe_resource *dst, uint64_t offset,
1303 uint64_t size, unsigned value, unsigned user_flags,
1304 enum si_coherency coher, enum si_cache_policy cache_policy);
1305 void si_cp_dma_copy_buffer(struct si_context *sctx,
1306 struct pipe_resource *dst, struct pipe_resource *src,
1307 uint64_t dst_offset, uint64_t src_offset, unsigned size,
1308 unsigned user_flags, enum si_coherency coher,
1309 enum si_cache_policy cache_policy);
1310 void cik_prefetch_TC_L2_async(struct si_context *sctx, struct pipe_resource *buf,
1311 uint64_t offset, unsigned size);
1312 void cik_emit_prefetch_L2(struct si_context *sctx, bool vertex_stage_only);
1313 void si_test_gds(struct si_context *sctx);
1314 void si_cp_write_data(struct si_context *sctx, struct si_resource *buf,
1315 unsigned offset, unsigned size, unsigned dst_sel,
1316 unsigned engine, const void *data);
1317 void si_cp_copy_data(struct si_context *sctx, struct radeon_cmdbuf *cs,
1318 unsigned dst_sel, struct si_resource *dst, unsigned dst_offset,
1319 unsigned src_sel, struct si_resource *src, unsigned src_offset);
1320
1321 /* si_debug.c */
1322 void si_save_cs(struct radeon_winsys *ws, struct radeon_cmdbuf *cs,
1323 struct radeon_saved_cs *saved, bool get_buffer_list);
1324 void si_clear_saved_cs(struct radeon_saved_cs *saved);
1325 void si_destroy_saved_cs(struct si_saved_cs *scs);
1326 void si_auto_log_cs(void *data, struct u_log_context *log);
1327 void si_log_hw_flush(struct si_context *sctx);
1328 void si_log_draw_state(struct si_context *sctx, struct u_log_context *log);
1329 void si_log_compute_state(struct si_context *sctx, struct u_log_context *log);
1330 void si_init_debug_functions(struct si_context *sctx);
1331 void si_check_vm_faults(struct si_context *sctx,
1332 struct radeon_saved_cs *saved, enum ring_type ring);
1333 bool si_replace_shader(unsigned num, struct si_shader_binary *binary);
1334
1335 /* si_dma.c */
1336 void si_init_dma_functions(struct si_context *sctx);
1337
1338 /* si_dma_cs.c */
1339 void si_dma_emit_timestamp(struct si_context *sctx, struct si_resource *dst,
1340 uint64_t offset);
1341 void si_sdma_clear_buffer(struct si_context *sctx, struct pipe_resource *dst,
1342 uint64_t offset, uint64_t size, unsigned clear_value);
1343 void si_need_dma_space(struct si_context *ctx, unsigned num_dw,
1344 struct si_resource *dst, struct si_resource *src);
1345 void si_flush_dma_cs(struct si_context *ctx, unsigned flags,
1346 struct pipe_fence_handle **fence);
1347 void si_screen_clear_buffer(struct si_screen *sscreen, struct pipe_resource *dst,
1348 uint64_t offset, uint64_t size, unsigned value);
1349
1350 /* si_fence.c */
1351 void si_cp_release_mem(struct si_context *ctx, struct radeon_cmdbuf *cs,
1352 unsigned event, unsigned event_flags,
1353 unsigned dst_sel, unsigned int_sel, unsigned data_sel,
1354 struct si_resource *buf, uint64_t va,
1355 uint32_t new_fence, unsigned query_type);
1356 unsigned si_cp_write_fence_dwords(struct si_screen *screen);
1357 void si_cp_wait_mem(struct si_context *ctx, struct radeon_cmdbuf *cs,
1358 uint64_t va, uint32_t ref, uint32_t mask, unsigned flags);
1359 void si_init_fence_functions(struct si_context *ctx);
1360 void si_init_screen_fence_functions(struct si_screen *screen);
1361 struct pipe_fence_handle *si_create_fence(struct pipe_context *ctx,
1362 struct tc_unflushed_batch_token *tc_token);
1363
1364 /* si_get.c */
1365 void si_init_screen_get_functions(struct si_screen *sscreen);
1366
1367 /* si_gfx_cs.c */
1368 void si_flush_gfx_cs(struct si_context *ctx, unsigned flags,
1369 struct pipe_fence_handle **fence);
1370 void si_begin_new_gfx_cs(struct si_context *ctx);
1371 void si_need_gfx_cs_space(struct si_context *ctx);
1372 void si_unref_sdma_uploads(struct si_context *sctx);
1373
1374 /* si_gpu_load.c */
1375 void si_gpu_load_kill_thread(struct si_screen *sscreen);
1376 uint64_t si_begin_counter(struct si_screen *sscreen, unsigned type);
1377 unsigned si_end_counter(struct si_screen *sscreen, unsigned type,
1378 uint64_t begin);
1379
1380 /* si_compute.c */
1381 void si_emit_initial_compute_regs(struct si_context *sctx, struct radeon_cmdbuf *cs);
1382 unsigned si_get_compute_resource_limits(struct si_screen *sscreen,
1383 unsigned waves_per_threadgroup,
1384 unsigned max_waves_per_sh,
1385 unsigned threadgroups_per_cu);
1386 void si_init_compute_functions(struct si_context *sctx);
1387
1388 /* si_compute_prim_discard.c */
1389 enum si_prim_discard_outcome {
1390 SI_PRIM_DISCARD_ENABLED,
1391 SI_PRIM_DISCARD_DISABLED,
1392 SI_PRIM_DISCARD_DRAW_SPLIT,
1393 };
1394
1395 void si_build_prim_discard_compute_shader(struct si_shader_context *ctx);
1396 enum si_prim_discard_outcome
1397 si_prepare_prim_discard_or_split_draw(struct si_context *sctx,
1398 const struct pipe_draw_info *info,
1399 bool primitive_restart);
1400 void si_compute_signal_gfx(struct si_context *sctx);
1401 void si_dispatch_prim_discard_cs_and_draw(struct si_context *sctx,
1402 const struct pipe_draw_info *info,
1403 unsigned index_size,
1404 unsigned base_vertex,
1405 uint64_t input_indexbuf_va,
1406 unsigned input_indexbuf_max_elements);
1407 void si_initialize_prim_discard_tunables(struct si_context *sctx);
1408
1409 /* si_perfcounters.c */
1410 void si_init_perfcounters(struct si_screen *screen);
1411 void si_destroy_perfcounters(struct si_screen *screen);
1412
1413 /* si_pipe.c */
1414 bool si_check_device_reset(struct si_context *sctx);
1415
1416 /* si_query.c */
1417 void si_init_screen_query_functions(struct si_screen *sscreen);
1418 void si_init_query_functions(struct si_context *sctx);
1419 void si_suspend_queries(struct si_context *sctx);
1420 void si_resume_queries(struct si_context *sctx);
1421
1422 /* si_shaderlib_tgsi.c */
1423 void *si_get_blitter_vs(struct si_context *sctx, enum blitter_attrib_type type,
1424 unsigned num_layers);
1425 void *si_create_fixed_func_tcs(struct si_context *sctx);
1426 void *si_create_dma_compute_shader(struct pipe_context *ctx,
1427 unsigned num_dwords_per_thread,
1428 bool dst_stream_cache_policy, bool is_copy);
1429 void *si_create_copy_image_compute_shader(struct pipe_context *ctx);
1430 void *si_create_copy_image_compute_shader_1d_array(struct pipe_context *ctx);
1431 void *si_clear_render_target_shader(struct pipe_context *ctx);
1432 void *si_clear_render_target_shader_1d_array(struct pipe_context *ctx);
1433 void *si_create_dcc_retile_cs(struct pipe_context *ctx);
1434 void *si_create_query_result_cs(struct si_context *sctx);
1435
1436 /* si_test_dma.c */
1437 void si_test_dma(struct si_screen *sscreen);
1438
1439 /* si_test_clearbuffer.c */
1440 void si_test_dma_perf(struct si_screen *sscreen);
1441
1442 /* si_uvd.c */
1443 struct pipe_video_codec *si_uvd_create_decoder(struct pipe_context *context,
1444 const struct pipe_video_codec *templ);
1445
1446 struct pipe_video_buffer *si_video_buffer_create(struct pipe_context *pipe,
1447 const struct pipe_video_buffer *tmpl);
1448
1449 /* si_viewport.c */
1450 void si_update_vs_viewport_state(struct si_context *ctx);
1451 void si_init_viewport_functions(struct si_context *ctx);
1452
1453 /* si_texture.c */
1454 bool si_prepare_for_dma_blit(struct si_context *sctx,
1455 struct si_texture *dst,
1456 unsigned dst_level, unsigned dstx,
1457 unsigned dsty, unsigned dstz,
1458 struct si_texture *src,
1459 unsigned src_level,
1460 const struct pipe_box *src_box);
1461 void si_eliminate_fast_color_clear(struct si_context *sctx,
1462 struct si_texture *tex);
1463 void si_texture_discard_cmask(struct si_screen *sscreen,
1464 struct si_texture *tex);
1465 bool si_init_flushed_depth_texture(struct pipe_context *ctx,
1466 struct pipe_resource *texture);
1467 void si_print_texture_info(struct si_screen *sscreen,
1468 struct si_texture *tex, struct u_log_context *log);
1469 struct pipe_resource *si_texture_create(struct pipe_screen *screen,
1470 const struct pipe_resource *templ);
1471 bool vi_dcc_formats_compatible(enum pipe_format format1,
1472 enum pipe_format format2);
1473 bool vi_dcc_formats_are_incompatible(struct pipe_resource *tex,
1474 unsigned level,
1475 enum pipe_format view_format);
1476 void vi_disable_dcc_if_incompatible_format(struct si_context *sctx,
1477 struct pipe_resource *tex,
1478 unsigned level,
1479 enum pipe_format view_format);
1480 struct pipe_surface *si_create_surface_custom(struct pipe_context *pipe,
1481 struct pipe_resource *texture,
1482 const struct pipe_surface *templ,
1483 unsigned width0, unsigned height0,
1484 unsigned width, unsigned height);
1485 unsigned si_translate_colorswap(enum pipe_format format, bool do_endian_swap);
1486 void vi_separate_dcc_try_enable(struct si_context *sctx,
1487 struct si_texture *tex);
1488 void vi_separate_dcc_start_query(struct si_context *sctx,
1489 struct si_texture *tex);
1490 void vi_separate_dcc_stop_query(struct si_context *sctx,
1491 struct si_texture *tex);
1492 void vi_separate_dcc_process_and_reset_stats(struct pipe_context *ctx,
1493 struct si_texture *tex);
1494 bool si_texture_disable_dcc(struct si_context *sctx,
1495 struct si_texture *tex);
1496 void si_init_screen_texture_functions(struct si_screen *sscreen);
1497 void si_init_context_texture_functions(struct si_context *sctx);
1498
1499
1500 /*
1501 * common helpers
1502 */
1503
1504 static inline struct si_resource *si_resource(struct pipe_resource *r)
1505 {
1506 return (struct si_resource*)r;
1507 }
1508
1509 static inline void
1510 si_resource_reference(struct si_resource **ptr, struct si_resource *res)
1511 {
1512 pipe_resource_reference((struct pipe_resource **)ptr,
1513 (struct pipe_resource *)res);
1514 }
1515
1516 static inline void
1517 si_texture_reference(struct si_texture **ptr, struct si_texture *res)
1518 {
1519 pipe_resource_reference((struct pipe_resource **)ptr, &res->buffer.b.b);
1520 }
1521
1522 static inline bool
1523 vi_dcc_enabled(struct si_texture *tex, unsigned level)
1524 {
1525 return tex->dcc_offset && level < tex->surface.num_dcc_levels;
1526 }
1527
1528 static inline unsigned
1529 si_tile_mode_index(struct si_texture *tex, unsigned level, bool stencil)
1530 {
1531 if (stencil)
1532 return tex->surface.u.legacy.stencil_tiling_index[level];
1533 else
1534 return tex->surface.u.legacy.tiling_index[level];
1535 }
1536
1537 static inline unsigned
1538 si_get_minimum_num_gfx_cs_dwords(struct si_context *sctx)
1539 {
1540 /* Don't count the needed CS space exactly and just use an upper bound.
1541 *
1542 * Also reserve space for stopping queries at the end of IB, because
1543 * the number of active queries is unlimited in theory.
1544 */
1545 return 2048 + sctx->num_cs_dw_queries_suspend;
1546 }
1547
1548 static inline void
1549 si_context_add_resource_size(struct si_context *sctx, struct pipe_resource *r)
1550 {
1551 if (r) {
1552 /* Add memory usage for need_gfx_cs_space */
1553 sctx->vram += si_resource(r)->vram_usage;
1554 sctx->gtt += si_resource(r)->gart_usage;
1555 }
1556 }
1557
1558 static inline void
1559 si_invalidate_draw_sh_constants(struct si_context *sctx)
1560 {
1561 sctx->last_base_vertex = SI_BASE_VERTEX_UNKNOWN;
1562 sctx->last_instance_count = SI_INSTANCE_COUNT_UNKNOWN;
1563 }
1564
1565 static inline unsigned
1566 si_get_atom_bit(struct si_context *sctx, struct si_atom *atom)
1567 {
1568 return 1 << (atom - sctx->atoms.array);
1569 }
1570
1571 static inline void
1572 si_set_atom_dirty(struct si_context *sctx, struct si_atom *atom, bool dirty)
1573 {
1574 unsigned bit = si_get_atom_bit(sctx, atom);
1575
1576 if (dirty)
1577 sctx->dirty_atoms |= bit;
1578 else
1579 sctx->dirty_atoms &= ~bit;
1580 }
1581
1582 static inline bool
1583 si_is_atom_dirty(struct si_context *sctx, struct si_atom *atom)
1584 {
1585 return (sctx->dirty_atoms & si_get_atom_bit(sctx, atom)) != 0;
1586 }
1587
1588 static inline void
1589 si_mark_atom_dirty(struct si_context *sctx, struct si_atom *atom)
1590 {
1591 si_set_atom_dirty(sctx, atom, true);
1592 }
1593
1594 static inline struct si_shader_ctx_state *si_get_vs(struct si_context *sctx)
1595 {
1596 if (sctx->gs_shader.cso)
1597 return &sctx->gs_shader;
1598 if (sctx->tes_shader.cso)
1599 return &sctx->tes_shader;
1600
1601 return &sctx->vs_shader;
1602 }
1603
1604 static inline struct tgsi_shader_info *si_get_vs_info(struct si_context *sctx)
1605 {
1606 struct si_shader_ctx_state *vs = si_get_vs(sctx);
1607
1608 return vs->cso ? &vs->cso->info : NULL;
1609 }
1610
1611 static inline struct si_shader* si_get_vs_state(struct si_context *sctx)
1612 {
1613 if (sctx->gs_shader.cso)
1614 return sctx->gs_shader.cso->gs_copy_shader;
1615
1616 struct si_shader_ctx_state *vs = si_get_vs(sctx);
1617 return vs->current ? vs->current : NULL;
1618 }
1619
1620 static inline bool si_can_dump_shader(struct si_screen *sscreen,
1621 unsigned processor)
1622 {
1623 return sscreen->debug_flags & (1 << processor);
1624 }
1625
1626 static inline bool si_get_strmout_en(struct si_context *sctx)
1627 {
1628 return sctx->streamout.streamout_enabled ||
1629 sctx->streamout.prims_gen_query_enabled;
1630 }
1631
1632 static inline unsigned
1633 si_optimal_tcc_alignment(struct si_context *sctx, unsigned upload_size)
1634 {
1635 unsigned alignment, tcc_cache_line_size;
1636
1637 /* If the upload size is less than the cache line size (e.g. 16, 32),
1638 * the whole thing will fit into a cache line if we align it to its size.
1639 * The idea is that multiple small uploads can share a cache line.
1640 * If the upload size is greater, align it to the cache line size.
1641 */
1642 alignment = util_next_power_of_two(upload_size);
1643 tcc_cache_line_size = sctx->screen->info.tcc_cache_line_size;
1644 return MIN2(alignment, tcc_cache_line_size);
1645 }
1646
1647 static inline void
1648 si_saved_cs_reference(struct si_saved_cs **dst, struct si_saved_cs *src)
1649 {
1650 if (pipe_reference(&(*dst)->reference, &src->reference))
1651 si_destroy_saved_cs(*dst);
1652
1653 *dst = src;
1654 }
1655
1656 static inline void
1657 si_make_CB_shader_coherent(struct si_context *sctx, unsigned num_samples,
1658 bool shaders_read_metadata, bool dcc_pipe_aligned)
1659 {
1660 sctx->flags |= SI_CONTEXT_FLUSH_AND_INV_CB |
1661 SI_CONTEXT_INV_VCACHE;
1662
1663 if (sctx->chip_class >= GFX9) {
1664 /* Single-sample color is coherent with shaders on GFX9, but
1665 * L2 metadata must be flushed if shaders read metadata.
1666 * (DCC, CMASK).
1667 */
1668 if (num_samples >= 2 ||
1669 (shaders_read_metadata && !dcc_pipe_aligned))
1670 sctx->flags |= SI_CONTEXT_INV_L2;
1671 else if (shaders_read_metadata)
1672 sctx->flags |= SI_CONTEXT_INV_L2_METADATA;
1673 } else {
1674 /* GFX6-GFX8 */
1675 sctx->flags |= SI_CONTEXT_INV_L2;
1676 }
1677 }
1678
1679 static inline void
1680 si_make_DB_shader_coherent(struct si_context *sctx, unsigned num_samples,
1681 bool include_stencil, bool shaders_read_metadata)
1682 {
1683 sctx->flags |= SI_CONTEXT_FLUSH_AND_INV_DB |
1684 SI_CONTEXT_INV_VCACHE;
1685
1686 if (sctx->chip_class >= GFX9) {
1687 /* Single-sample depth (not stencil) is coherent with shaders
1688 * on GFX9, but L2 metadata must be flushed if shaders read
1689 * metadata.
1690 */
1691 if (num_samples >= 2 || include_stencil)
1692 sctx->flags |= SI_CONTEXT_INV_L2;
1693 else if (shaders_read_metadata)
1694 sctx->flags |= SI_CONTEXT_INV_L2_METADATA;
1695 } else {
1696 /* GFX6-GFX8 */
1697 sctx->flags |= SI_CONTEXT_INV_L2;
1698 }
1699 }
1700
1701 static inline bool
1702 si_can_sample_zs(struct si_texture *tex, bool stencil_sampler)
1703 {
1704 return (stencil_sampler && tex->can_sample_s) ||
1705 (!stencil_sampler && tex->can_sample_z);
1706 }
1707
1708 static inline bool
1709 si_htile_enabled(struct si_texture *tex, unsigned level)
1710 {
1711 return tex->htile_offset && level == 0;
1712 }
1713
1714 static inline bool
1715 vi_tc_compat_htile_enabled(struct si_texture *tex, unsigned level)
1716 {
1717 assert(!tex->tc_compatible_htile || tex->htile_offset);
1718 return tex->tc_compatible_htile && level == 0;
1719 }
1720
1721 static inline unsigned si_get_ps_iter_samples(struct si_context *sctx)
1722 {
1723 if (sctx->ps_uses_fbfetch)
1724 return sctx->framebuffer.nr_color_samples;
1725
1726 return MIN2(sctx->ps_iter_samples, sctx->framebuffer.nr_color_samples);
1727 }
1728
1729 static inline unsigned si_get_total_colormask(struct si_context *sctx)
1730 {
1731 if (sctx->queued.named.rasterizer->rasterizer_discard)
1732 return 0;
1733
1734 struct si_shader_selector *ps = sctx->ps_shader.cso;
1735 if (!ps)
1736 return 0;
1737
1738 unsigned colormask = sctx->framebuffer.colorbuf_enabled_4bit &
1739 sctx->queued.named.blend->cb_target_mask;
1740
1741 if (!ps->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS])
1742 colormask &= ps->colors_written_4bit;
1743 else if (!ps->colors_written_4bit)
1744 colormask = 0; /* color0 writes all cbufs, but it's not written */
1745
1746 return colormask;
1747 }
1748
1749 #define UTIL_ALL_PRIM_LINE_MODES ((1 << PIPE_PRIM_LINES) | \
1750 (1 << PIPE_PRIM_LINE_LOOP) | \
1751 (1 << PIPE_PRIM_LINE_STRIP) | \
1752 (1 << PIPE_PRIM_LINES_ADJACENCY) | \
1753 (1 << PIPE_PRIM_LINE_STRIP_ADJACENCY))
1754
1755 static inline bool util_prim_is_lines(unsigned prim)
1756 {
1757 return ((1 << prim) & UTIL_ALL_PRIM_LINE_MODES) != 0;
1758 }
1759
1760 static inline bool util_prim_is_points_or_lines(unsigned prim)
1761 {
1762 return ((1 << prim) & (UTIL_ALL_PRIM_LINE_MODES |
1763 (1 << PIPE_PRIM_POINTS))) != 0;
1764 }
1765
1766 /**
1767 * Return true if there is enough memory in VRAM and GTT for the buffers
1768 * added so far.
1769 *
1770 * \param vram VRAM memory size not added to the buffer list yet
1771 * \param gtt GTT memory size not added to the buffer list yet
1772 */
1773 static inline bool
1774 radeon_cs_memory_below_limit(struct si_screen *screen,
1775 struct radeon_cmdbuf *cs,
1776 uint64_t vram, uint64_t gtt)
1777 {
1778 vram += cs->used_vram;
1779 gtt += cs->used_gart;
1780
1781 /* Anything that goes above the VRAM size should go to GTT. */
1782 if (vram > screen->info.vram_size)
1783 gtt += vram - screen->info.vram_size;
1784
1785 /* Now we just need to check if we have enough GTT. */
1786 return gtt < screen->info.gart_size * 0.7;
1787 }
1788
1789 /**
1790 * Add a buffer to the buffer list for the given command stream (CS).
1791 *
1792 * All buffers used by a CS must be added to the list. This tells the kernel
1793 * driver which buffers are used by GPU commands. Other buffers can
1794 * be swapped out (not accessible) during execution.
1795 *
1796 * The buffer list becomes empty after every context flush and must be
1797 * rebuilt.
1798 */
1799 static inline void radeon_add_to_buffer_list(struct si_context *sctx,
1800 struct radeon_cmdbuf *cs,
1801 struct si_resource *bo,
1802 enum radeon_bo_usage usage,
1803 enum radeon_bo_priority priority)
1804 {
1805 assert(usage);
1806 sctx->ws->cs_add_buffer(
1807 cs, bo->buf,
1808 (enum radeon_bo_usage)(usage | RADEON_USAGE_SYNCHRONIZED),
1809 bo->domains, priority);
1810 }
1811
1812 /**
1813 * Same as above, but also checks memory usage and flushes the context
1814 * accordingly.
1815 *
1816 * When this SHOULD NOT be used:
1817 *
1818 * - if si_context_add_resource_size has been called for the buffer
1819 * followed by *_need_cs_space for checking the memory usage
1820 *
1821 * - if si_need_dma_space has been called for the buffer
1822 *
1823 * - when emitting state packets and draw packets (because preceding packets
1824 * can't be re-emitted at that point)
1825 *
1826 * - if shader resource "enabled_mask" is not up-to-date or there is
1827 * a different constraint disallowing a context flush
1828 */
1829 static inline void
1830 radeon_add_to_gfx_buffer_list_check_mem(struct si_context *sctx,
1831 struct si_resource *bo,
1832 enum radeon_bo_usage usage,
1833 enum radeon_bo_priority priority,
1834 bool check_mem)
1835 {
1836 if (check_mem &&
1837 !radeon_cs_memory_below_limit(sctx->screen, sctx->gfx_cs,
1838 sctx->vram + bo->vram_usage,
1839 sctx->gtt + bo->gart_usage))
1840 si_flush_gfx_cs(sctx, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW, NULL);
1841
1842 radeon_add_to_buffer_list(sctx, sctx->gfx_cs, bo, usage, priority);
1843 }
1844
1845 static inline bool si_compute_prim_discard_enabled(struct si_context *sctx)
1846 {
1847 return sctx->prim_discard_vertex_count_threshold != UINT_MAX;
1848 }
1849
1850 #define PRINT_ERR(fmt, args...) \
1851 fprintf(stderr, "EE %s:%d %s - " fmt, __FILE__, __LINE__, __func__, ##args)
1852
1853 #endif