radeonsi: reduce overhead for resident textures which need depth decompression
[mesa.git] / src / gallium / drivers / radeonsi / si_pipe.h
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Jerome Glisse
25 */
26 #ifndef SI_PIPE_H
27 #define SI_PIPE_H
28
29 #include "si_shader.h"
30
31 #include "util/u_dynarray.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 /* The base vertex and primitive restart can be any number, but we must pick
40 * one which will mean "unknown" for the purpose of state tracking and
41 * the number shouldn't be a commonly-used one. */
42 #define SI_BASE_VERTEX_UNKNOWN INT_MIN
43 #define SI_RESTART_INDEX_UNKNOWN INT_MIN
44 #define SI_NUM_SMOOTH_AA_SAMPLES 8
45 #define SI_GS_PER_ES 128
46 /* Alignment for optimal CP DMA performance. */
47 #define SI_CPDMA_ALIGNMENT 32
48
49 /* Instruction cache. */
50 #define SI_CONTEXT_INV_ICACHE (R600_CONTEXT_PRIVATE_FLAG << 0)
51 /* SMEM L1, other names: KCACHE, constant cache, DCACHE, data cache */
52 #define SI_CONTEXT_INV_SMEM_L1 (R600_CONTEXT_PRIVATE_FLAG << 1)
53 /* VMEM L1 can optionally be bypassed (GLC=1). Other names: TC L1 */
54 #define SI_CONTEXT_INV_VMEM_L1 (R600_CONTEXT_PRIVATE_FLAG << 2)
55 /* Used by everything except CB/DB, can be bypassed (SLC=1). Other names: TC L2 */
56 #define SI_CONTEXT_INV_GLOBAL_L2 (R600_CONTEXT_PRIVATE_FLAG << 3)
57 /* Write dirty L2 lines back to memory (shader and CP DMA stores), but don't
58 * invalidate L2. SI-CIK can't do it, so they will do complete invalidation. */
59 #define SI_CONTEXT_WRITEBACK_GLOBAL_L2 (R600_CONTEXT_PRIVATE_FLAG << 4)
60 /* gaps */
61 /* Framebuffer caches. */
62 #define SI_CONTEXT_FLUSH_AND_INV_DB (R600_CONTEXT_PRIVATE_FLAG << 7)
63 #define SI_CONTEXT_FLUSH_AND_INV_CB (R600_CONTEXT_PRIVATE_FLAG << 8)
64 /* Engine synchronization. */
65 #define SI_CONTEXT_VS_PARTIAL_FLUSH (R600_CONTEXT_PRIVATE_FLAG << 9)
66 #define SI_CONTEXT_PS_PARTIAL_FLUSH (R600_CONTEXT_PRIVATE_FLAG << 10)
67 #define SI_CONTEXT_CS_PARTIAL_FLUSH (R600_CONTEXT_PRIVATE_FLAG << 11)
68 #define SI_CONTEXT_VGT_FLUSH (R600_CONTEXT_PRIVATE_FLAG << 12)
69 #define SI_CONTEXT_VGT_STREAMOUT_SYNC (R600_CONTEXT_PRIVATE_FLAG << 13)
70
71 #define SI_MAX_BORDER_COLORS 4096
72 #define SIX_BITS 0x3F
73
74 struct si_compute;
75 struct hash_table;
76 struct u_suballocator;
77
78 struct si_screen {
79 struct r600_common_screen b;
80 unsigned gs_table_depth;
81 unsigned tess_offchip_block_dw_size;
82 bool has_distributed_tess;
83 bool has_draw_indirect_multi;
84 bool has_ds_bpermute;
85 bool has_msaa_sample_loc_bug;
86
87 /* Whether shaders are monolithic (1-part) or separate (3-part). */
88 bool use_monolithic_shaders;
89 bool record_llvm_ir;
90
91 mtx_t shader_parts_mutex;
92 struct si_shader_part *vs_prologs;
93 struct si_shader_part *tcs_epilogs;
94 struct si_shader_part *gs_prologs;
95 struct si_shader_part *ps_prologs;
96 struct si_shader_part *ps_epilogs;
97
98 /* Shader cache in memory.
99 *
100 * Design & limitations:
101 * - The shader cache is per screen (= per process), never saved to
102 * disk, and skips redundant shader compilations from TGSI to bytecode.
103 * - It can only be used with one-variant-per-shader support, in which
104 * case only the main (typically middle) part of shaders is cached.
105 * - Only VS, TCS, TES, PS are cached, out of which only the hw VS
106 * variants of VS and TES are cached, so LS and ES aren't.
107 * - GS and CS aren't cached, but it's certainly possible to cache
108 * those as well.
109 */
110 mtx_t shader_cache_mutex;
111 struct hash_table *shader_cache;
112
113 /* Shader compiler queue for multithreaded compilation. */
114 struct util_queue shader_compiler_queue;
115 LLVMTargetMachineRef tm[4]; /* used by the queue only */
116
117 struct util_queue shader_compiler_queue_low_priority;
118 LLVMTargetMachineRef tm_low_priority[4];
119 };
120
121 struct si_blend_color {
122 struct r600_atom atom;
123 struct pipe_blend_color state;
124 };
125
126 struct si_sampler_view {
127 struct pipe_sampler_view base;
128 /* [0..7] = image descriptor
129 * [4..7] = buffer descriptor */
130 uint32_t state[8];
131 uint32_t fmask_state[8];
132 const struct legacy_surf_level *base_level_info;
133 ubyte base_level;
134 ubyte block_width;
135 bool is_stencil_sampler;
136 bool dcc_incompatible;
137 };
138
139 #define SI_SAMPLER_STATE_MAGIC 0x34f1c35a
140
141 struct si_sampler_state {
142 #ifdef DEBUG
143 unsigned magic;
144 #endif
145 uint32_t val[4];
146 };
147
148 struct si_cs_shader_state {
149 struct si_compute *program;
150 struct si_compute *emitted_program;
151 unsigned offset;
152 bool initialized;
153 bool uses_scratch;
154 };
155
156 struct si_textures_info {
157 struct si_sampler_views views;
158 uint32_t needs_depth_decompress_mask;
159 uint32_t needs_color_decompress_mask;
160 };
161
162 struct si_images_info {
163 struct pipe_image_view views[SI_NUM_IMAGES];
164 uint32_t needs_color_decompress_mask;
165 unsigned enabled_mask;
166 };
167
168 struct si_framebuffer {
169 struct r600_atom atom;
170 struct pipe_framebuffer_state state;
171 unsigned colorbuf_enabled_4bit;
172 unsigned spi_shader_col_format;
173 unsigned spi_shader_col_format_alpha;
174 unsigned spi_shader_col_format_blend;
175 unsigned spi_shader_col_format_blend_alpha;
176 ubyte nr_samples:5; /* at most 16xAA */
177 ubyte log_samples:3; /* at most 4 = 16xAA */
178 ubyte compressed_cb_mask;
179 ubyte color_is_int8;
180 ubyte color_is_int10;
181 ubyte dirty_cbufs;
182 bool dirty_zsbuf;
183 bool any_dst_linear;
184 bool do_update_surf_dirtiness;
185 };
186
187 struct si_clip_state {
188 struct r600_atom atom;
189 struct pipe_clip_state state;
190 };
191
192 struct si_sample_locs {
193 struct r600_atom atom;
194 unsigned nr_samples;
195 };
196
197 struct si_sample_mask {
198 struct r600_atom atom;
199 uint16_t sample_mask;
200 };
201
202 /* A shader state consists of the shader selector, which is a constant state
203 * object shared by multiple contexts and shouldn't be modified, and
204 * the current shader variant selected for this context.
205 */
206 struct si_shader_ctx_state {
207 struct si_shader_selector *cso;
208 struct si_shader *current;
209 };
210
211 #define SI_NUM_VGT_PARAM_KEY_BITS 12
212 #define SI_NUM_VGT_PARAM_STATES (1 << SI_NUM_VGT_PARAM_KEY_BITS)
213
214 /* The IA_MULTI_VGT_PARAM key used to index the table of precomputed values.
215 * Some fields are set by state-change calls, most are set by draw_vbo.
216 */
217 union si_vgt_param_key {
218 struct {
219 unsigned prim:4;
220 unsigned uses_instancing:1;
221 unsigned multi_instances_smaller_than_primgroup:1;
222 unsigned primitive_restart:1;
223 unsigned count_from_stream_output:1;
224 unsigned line_stipple_enabled:1;
225 unsigned uses_tess:1;
226 unsigned tess_uses_prim_id:1;
227 unsigned uses_gs:1;
228 unsigned _pad:32 - SI_NUM_VGT_PARAM_KEY_BITS;
229 } u;
230 uint32_t index;
231 };
232
233 struct si_bindless_descriptor
234 {
235 struct pb_slab_entry entry;
236 struct r600_resource *buffer;
237 unsigned offset;
238 uint32_t desc_list[16];
239 bool dirty;
240 };
241
242 struct si_texture_handle
243 {
244 struct si_bindless_descriptor *desc;
245 struct pipe_sampler_view *view;
246 bool needs_color_decompress;
247 };
248
249 struct si_image_handle
250 {
251 struct si_bindless_descriptor *desc;
252 struct pipe_image_view view;
253 bool needs_color_decompress;
254 };
255
256 struct si_context {
257 struct r600_common_context b;
258 struct blitter_context *blitter;
259 void *custom_dsa_flush;
260 void *custom_blend_resolve;
261 void *custom_blend_fmask_decompress;
262 void *custom_blend_eliminate_fastclear;
263 void *custom_blend_dcc_decompress;
264 struct si_screen *screen;
265 LLVMTargetMachineRef tm; /* only non-threaded compilation */
266 struct si_shader_ctx_state fixed_func_tcs_shader;
267
268 struct radeon_winsys_cs *ce_ib;
269 struct radeon_winsys_cs *ce_preamble_ib;
270 struct r600_resource *ce_ram_saved_buffer;
271 struct u_suballocator *ce_suballocator;
272 unsigned ce_ram_saved_offset;
273 uint16_t total_ce_ram_allocated;
274 bool ce_need_synchronization:1;
275
276 bool gfx_flush_in_progress:1;
277 bool compute_is_busy:1;
278
279 /* Atoms (direct states). */
280 union si_state_atoms atoms;
281 unsigned dirty_atoms; /* mask */
282 /* PM4 states (precomputed immutable states) */
283 unsigned dirty_states;
284 union si_state queued;
285 union si_state emitted;
286
287 /* Atom declarations. */
288 struct r600_atom prefetch_L2;
289 struct si_framebuffer framebuffer;
290 struct si_sample_locs msaa_sample_locs;
291 struct r600_atom db_render_state;
292 struct r600_atom msaa_config;
293 struct si_sample_mask sample_mask;
294 struct r600_atom cb_render_state;
295 unsigned last_cb_target_mask;
296 struct si_blend_color blend_color;
297 struct r600_atom clip_regs;
298 struct si_clip_state clip_state;
299 struct si_shader_data shader_userdata;
300 struct si_stencil_ref stencil_ref;
301 struct r600_atom spi_map;
302
303 /* Precomputed states. */
304 struct si_pm4_state *init_config;
305 struct si_pm4_state *init_config_gs_rings;
306 bool init_config_has_vgt_flush;
307 struct si_pm4_state *vgt_shader_config[4];
308
309 /* shaders */
310 struct si_shader_ctx_state ps_shader;
311 struct si_shader_ctx_state gs_shader;
312 struct si_shader_ctx_state vs_shader;
313 struct si_shader_ctx_state tcs_shader;
314 struct si_shader_ctx_state tes_shader;
315 struct si_cs_shader_state cs_shader_state;
316
317 /* shader information */
318 struct si_vertex_elements *vertex_elements;
319 unsigned sprite_coord_enable;
320 bool flatshade;
321 bool do_update_shaders;
322
323 /* shader descriptors */
324 struct si_descriptors vertex_buffers;
325 struct si_descriptors descriptors[SI_NUM_DESCS];
326 unsigned descriptors_dirty;
327 unsigned shader_pointers_dirty;
328 unsigned shader_needs_decompress_mask;
329 struct si_buffer_resources rw_buffers;
330 struct si_buffer_resources const_and_shader_buffers[SI_NUM_SHADERS];
331 struct si_textures_info samplers[SI_NUM_SHADERS];
332 struct si_images_info images[SI_NUM_SHADERS];
333
334 /* other shader resources */
335 struct pipe_constant_buffer null_const_buf; /* used for set_constant_buffer(NULL) on CIK */
336 struct pipe_resource *esgs_ring;
337 struct pipe_resource *gsvs_ring;
338 struct pipe_resource *tf_ring;
339 struct pipe_resource *tess_offchip_ring;
340 union pipe_color_union *border_color_table; /* in CPU memory, any endian */
341 struct r600_resource *border_color_buffer;
342 union pipe_color_union *border_color_map; /* in VRAM (slow access), little endian */
343 unsigned border_color_count;
344
345 /* Vertex and index buffers. */
346 bool vertex_buffers_dirty;
347 bool vertex_buffer_pointer_dirty;
348 struct pipe_vertex_buffer vertex_buffer[SI_NUM_VERTEX_BUFFERS];
349
350 /* MSAA config state. */
351 int ps_iter_samples;
352 bool smoothing_enabled;
353
354 /* DB render state. */
355 unsigned ps_db_shader_control;
356 unsigned dbcb_copy_sample;
357 bool dbcb_depth_copy_enabled:1;
358 bool dbcb_stencil_copy_enabled:1;
359 bool db_flush_depth_inplace:1;
360 bool db_flush_stencil_inplace:1;
361 bool db_depth_clear:1;
362 bool db_depth_disable_expclear:1;
363 bool db_stencil_clear:1;
364 bool db_stencil_disable_expclear:1;
365 bool occlusion_queries_disabled:1;
366
367 /* Emitted draw state. */
368 bool gs_tri_strip_adj_fix:1;
369 int last_index_size;
370 int last_base_vertex;
371 int last_start_instance;
372 int last_drawid;
373 int last_sh_base_reg;
374 int last_primitive_restart_en;
375 int last_restart_index;
376 int last_gs_out_prim;
377 int last_prim;
378 int last_multi_vgt_param;
379 int last_rast_prim;
380 unsigned last_sc_line_stipple;
381 unsigned current_vs_state;
382 unsigned last_vs_state;
383 enum pipe_prim_type current_rast_prim; /* primitive type after TES, GS */
384
385 /* Scratch buffer */
386 struct r600_atom scratch_state;
387 struct r600_resource *scratch_buffer;
388 unsigned scratch_waves;
389 unsigned spi_tmpring_size;
390
391 struct r600_resource *compute_scratch_buffer;
392
393 /* Emitted derived tessellation state. */
394 /* Local shader (VS), or HS if LS-HS are merged. */
395 struct si_shader *last_ls;
396 struct si_shader_selector *last_tcs;
397 int last_num_tcs_input_cp;
398 int last_tes_sh_base;
399 bool last_tess_uses_primid;
400 unsigned last_num_patches;
401
402 /* Debug state. */
403 bool is_debug;
404 struct radeon_saved_cs last_gfx;
405 struct r600_resource *last_trace_buf;
406 struct r600_resource *trace_buf;
407 unsigned trace_id;
408 uint64_t dmesg_timestamp;
409 unsigned apitrace_call_number;
410
411 /* Other state */
412 bool need_check_render_feedback;
413 bool decompression_enabled;
414
415 /* Precomputed IA_MULTI_VGT_PARAM */
416 union si_vgt_param_key ia_multi_vgt_param_key;
417 unsigned ia_multi_vgt_param[SI_NUM_VGT_PARAM_STATES];
418
419 /* Slab allocator for bindless descriptors. */
420 struct pb_slabs bindless_descriptor_slabs;
421
422 /* Bindless descriptors. */
423 struct util_dynarray bindless_descriptors;
424 bool bindless_descriptors_dirty;
425
426 /* Allocated bindless handles */
427 struct hash_table *tex_handles;
428 struct hash_table *img_handles;
429
430 /* Resident bindless handles */
431 struct util_dynarray resident_tex_handles;
432 struct util_dynarray resident_img_handles;
433
434 /* Resident bindless handles which need decompression */
435 struct util_dynarray resident_tex_needs_depth_decompress;
436
437 /* Bindless state */
438 bool uses_bindless_samplers;
439 bool uses_bindless_images;
440 };
441
442 /* cik_sdma.c */
443 void cik_init_sdma_functions(struct si_context *sctx);
444
445 /* si_blit.c */
446 void si_init_blit_functions(struct si_context *sctx);
447 void si_decompress_graphics_textures(struct si_context *sctx);
448 void si_decompress_compute_textures(struct si_context *sctx);
449 void si_resource_copy_region(struct pipe_context *ctx,
450 struct pipe_resource *dst,
451 unsigned dst_level,
452 unsigned dstx, unsigned dsty, unsigned dstz,
453 struct pipe_resource *src,
454 unsigned src_level,
455 const struct pipe_box *src_box);
456
457 /* si_cp_dma.c */
458 #define SI_CPDMA_SKIP_CHECK_CS_SPACE (1 << 0) /* don't call need_cs_space */
459 #define SI_CPDMA_SKIP_SYNC_AFTER (1 << 1) /* don't wait for DMA after the copy */
460 #define SI_CPDMA_SKIP_SYNC_BEFORE (1 << 2) /* don't wait for DMA before the copy (RAW hazards) */
461 #define SI_CPDMA_SKIP_GFX_SYNC (1 << 3) /* don't flush caches and don't wait for PS/CS */
462 #define SI_CPDMA_SKIP_BO_LIST_UPDATE (1 << 4) /* don't update the BO list */
463 #define SI_CPDMA_SKIP_ALL (SI_CPDMA_SKIP_CHECK_CS_SPACE | \
464 SI_CPDMA_SKIP_SYNC_AFTER | \
465 SI_CPDMA_SKIP_SYNC_BEFORE | \
466 SI_CPDMA_SKIP_GFX_SYNC | \
467 SI_CPDMA_SKIP_BO_LIST_UPDATE)
468
469 void si_copy_buffer(struct si_context *sctx,
470 struct pipe_resource *dst, struct pipe_resource *src,
471 uint64_t dst_offset, uint64_t src_offset, unsigned size,
472 unsigned user_flags);
473 void cik_prefetch_TC_L2_async(struct si_context *sctx, struct pipe_resource *buf,
474 uint64_t offset, unsigned size);
475 void si_init_cp_dma_functions(struct si_context *sctx);
476
477 /* si_debug.c */
478 void si_init_debug_functions(struct si_context *sctx);
479 void si_check_vm_faults(struct r600_common_context *ctx,
480 struct radeon_saved_cs *saved, enum ring_type ring);
481 bool si_replace_shader(unsigned num, struct ac_shader_binary *binary);
482
483 /* si_dma.c */
484 void si_init_dma_functions(struct si_context *sctx);
485
486 /* si_hw_context.c */
487 void si_context_gfx_flush(void *context, unsigned flags,
488 struct pipe_fence_handle **fence);
489 void si_begin_new_cs(struct si_context *ctx);
490 void si_need_cs_space(struct si_context *ctx);
491
492 /* si_compute.c */
493 void si_init_compute_functions(struct si_context *sctx);
494
495 /* si_perfcounters.c */
496 void si_init_perfcounters(struct si_screen *screen);
497
498 /* si_uvd.c */
499 struct pipe_video_codec *si_uvd_create_decoder(struct pipe_context *context,
500 const struct pipe_video_codec *templ);
501
502 struct pipe_video_buffer *si_video_buffer_create(struct pipe_context *pipe,
503 const struct pipe_video_buffer *tmpl);
504
505 /*
506 * common helpers
507 */
508
509 static inline void
510 si_invalidate_draw_sh_constants(struct si_context *sctx)
511 {
512 sctx->last_base_vertex = SI_BASE_VERTEX_UNKNOWN;
513 }
514
515 static inline void
516 si_set_atom_dirty(struct si_context *sctx,
517 struct r600_atom *atom, bool dirty)
518 {
519 unsigned bit = 1 << atom->id;
520
521 if (dirty)
522 sctx->dirty_atoms |= bit;
523 else
524 sctx->dirty_atoms &= ~bit;
525 }
526
527 static inline bool
528 si_is_atom_dirty(struct si_context *sctx,
529 struct r600_atom *atom)
530 {
531 unsigned bit = 1 << atom->id;
532
533 return sctx->dirty_atoms & bit;
534 }
535
536 static inline void
537 si_mark_atom_dirty(struct si_context *sctx,
538 struct r600_atom *atom)
539 {
540 si_set_atom_dirty(sctx, atom, true);
541 }
542
543 static inline struct si_shader_ctx_state *si_get_vs(struct si_context *sctx)
544 {
545 if (sctx->gs_shader.cso)
546 return &sctx->gs_shader;
547 if (sctx->tes_shader.cso)
548 return &sctx->tes_shader;
549
550 return &sctx->vs_shader;
551 }
552
553 static inline struct tgsi_shader_info *si_get_vs_info(struct si_context *sctx)
554 {
555 struct si_shader_ctx_state *vs = si_get_vs(sctx);
556
557 return vs->cso ? &vs->cso->info : NULL;
558 }
559
560 static inline struct si_shader* si_get_vs_state(struct si_context *sctx)
561 {
562 if (sctx->gs_shader.cso)
563 return sctx->gs_shader.cso->gs_copy_shader;
564
565 struct si_shader_ctx_state *vs = si_get_vs(sctx);
566 return vs->current ? vs->current : NULL;
567 }
568
569 static inline unsigned
570 si_optimal_tcc_alignment(struct si_context *sctx, unsigned upload_size)
571 {
572 unsigned alignment, tcc_cache_line_size;
573
574 /* If the upload size is less than the cache line size (e.g. 16, 32),
575 * the whole thing will fit into a cache line if we align it to its size.
576 * The idea is that multiple small uploads can share a cache line.
577 * If the upload size is greater, align it to the cache line size.
578 */
579 alignment = util_next_power_of_two(upload_size);
580 tcc_cache_line_size = sctx->screen->b.info.tcc_cache_line_size;
581 return MIN2(alignment, tcc_cache_line_size);
582 }
583
584 #endif