radeonsi: emit_guardband packets optimization
[mesa.git] / src / gallium / drivers / radeonsi / si_state.h
1 /*
2 * Copyright 2012 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #ifndef SI_STATE_H
26 #define SI_STATE_H
27
28 #include "si_pm4.h"
29
30 #include "pipebuffer/pb_slab.h"
31 #include "util/u_blitter.h"
32
33 #define SI_NUM_GRAPHICS_SHADERS (PIPE_SHADER_TESS_EVAL+1)
34 #define SI_NUM_SHADERS (PIPE_SHADER_COMPUTE+1)
35
36 #define SI_NUM_VERTEX_BUFFERS SI_MAX_ATTRIBS
37 #define SI_NUM_SAMPLERS 32 /* OpenGL textures units per shader */
38 #define SI_NUM_CONST_BUFFERS 16
39 #define SI_NUM_IMAGES 16
40 #define SI_NUM_SHADER_BUFFERS 16
41
42 struct si_screen;
43 struct si_shader;
44 struct si_shader_selector;
45 struct si_texture;
46 struct si_qbo_state;
47
48 /* State atoms are callbacks which write a sequence of packets into a GPU
49 * command buffer (AKA indirect buffer, AKA IB, AKA command stream, AKA CS).
50 */
51 struct si_atom {
52 void (*emit)(struct si_context *ctx);
53 };
54
55 struct si_state_blend {
56 struct si_pm4_state pm4;
57 uint32_t cb_target_mask;
58 /* Set 0xf or 0x0 (4 bits) per render target if the following is
59 * true. ANDed with spi_shader_col_format.
60 */
61 unsigned cb_target_enabled_4bit;
62 unsigned blend_enable_4bit;
63 unsigned need_src_alpha_4bit;
64 unsigned commutative_4bit;
65 bool alpha_to_coverage:1;
66 bool alpha_to_one:1;
67 bool dual_src_blend:1;
68 bool logicop_enable:1;
69 };
70
71 struct si_state_rasterizer {
72 struct si_pm4_state pm4;
73 /* poly offset states for 16-bit, 24-bit, and 32-bit zbuffers */
74 struct si_pm4_state *pm4_poly_offset;
75 unsigned pa_sc_line_stipple;
76 unsigned pa_cl_clip_cntl;
77 float line_width;
78 float max_point_size;
79 unsigned sprite_coord_enable:8;
80 unsigned clip_plane_enable:8;
81 unsigned flatshade:1;
82 unsigned two_side:1;
83 unsigned multisample_enable:1;
84 unsigned force_persample_interp:1;
85 unsigned line_stipple_enable:1;
86 unsigned poly_stipple_enable:1;
87 unsigned line_smooth:1;
88 unsigned poly_smooth:1;
89 unsigned uses_poly_offset:1;
90 unsigned clamp_fragment_color:1;
91 unsigned clamp_vertex_color:1;
92 unsigned rasterizer_discard:1;
93 unsigned scissor_enable:1;
94 unsigned clip_halfz:1;
95 };
96
97 struct si_dsa_stencil_ref_part {
98 uint8_t valuemask[2];
99 uint8_t writemask[2];
100 };
101
102 struct si_dsa_order_invariance {
103 /** Whether the final result in Z/S buffers is guaranteed to be
104 * invariant under changes to the order in which fragments arrive. */
105 bool zs:1;
106
107 /** Whether the set of fragments that pass the combined Z/S test is
108 * guaranteed to be invariant under changes to the order in which
109 * fragments arrive. */
110 bool pass_set:1;
111
112 /** Whether the last fragment that passes the combined Z/S test at each
113 * sample is guaranteed to be invariant under changes to the order in
114 * which fragments arrive. */
115 bool pass_last:1;
116 };
117
118 struct si_state_dsa {
119 struct si_pm4_state pm4;
120 struct si_dsa_stencil_ref_part stencil_ref;
121
122 /* 0 = without stencil buffer, 1 = when both Z and S buffers are present */
123 struct si_dsa_order_invariance order_invariance[2];
124
125 ubyte alpha_func:3;
126 bool depth_enabled:1;
127 bool depth_write_enabled:1;
128 bool stencil_enabled:1;
129 bool stencil_write_enabled:1;
130 bool db_can_write:1;
131
132 };
133
134 struct si_stencil_ref {
135 struct pipe_stencil_ref state;
136 struct si_dsa_stencil_ref_part dsa_part;
137 };
138
139 struct si_vertex_elements
140 {
141 uint32_t instance_divisors[SI_MAX_ATTRIBS];
142 uint32_t rsrc_word3[SI_MAX_ATTRIBS];
143 uint16_t src_offset[SI_MAX_ATTRIBS];
144 uint8_t fix_fetch[SI_MAX_ATTRIBS];
145 uint8_t format_size[SI_MAX_ATTRIBS];
146 uint8_t vertex_buffer_index[SI_MAX_ATTRIBS];
147
148 uint8_t count;
149 bool uses_instance_divisors;
150
151 uint16_t first_vb_use_mask;
152 /* Vertex buffer descriptor list size aligned for optimal prefetch. */
153 uint16_t desc_list_byte_size;
154 uint16_t instance_divisor_is_one; /* bitmask of inputs */
155 uint16_t instance_divisor_is_fetched; /* bitmask of inputs */
156 };
157
158 union si_state {
159 struct {
160 struct si_state_blend *blend;
161 struct si_state_rasterizer *rasterizer;
162 struct si_state_dsa *dsa;
163 struct si_pm4_state *poly_offset;
164 struct si_pm4_state *ls;
165 struct si_pm4_state *hs;
166 struct si_pm4_state *es;
167 struct si_pm4_state *gs;
168 struct si_pm4_state *vgt_shader_config;
169 struct si_pm4_state *vs;
170 struct si_pm4_state *ps;
171 } named;
172 struct si_pm4_state *array[0];
173 };
174
175 #define SI_STATE_IDX(name) \
176 (offsetof(union si_state, named.name) / sizeof(struct si_pm4_state *))
177 #define SI_STATE_BIT(name) (1 << SI_STATE_IDX(name))
178 #define SI_NUM_STATES (sizeof(union si_state) / sizeof(struct si_pm4_state *))
179
180 static inline unsigned si_states_that_roll_context(void)
181 {
182 return (SI_STATE_BIT(blend) |
183 SI_STATE_BIT(rasterizer) |
184 SI_STATE_BIT(dsa) |
185 SI_STATE_BIT(poly_offset) |
186 SI_STATE_BIT(es) |
187 SI_STATE_BIT(gs) |
188 SI_STATE_BIT(vgt_shader_config) |
189 SI_STATE_BIT(vs) |
190 SI_STATE_BIT(ps));
191 }
192
193 union si_state_atoms {
194 struct {
195 /* The order matters. */
196 struct si_atom render_cond;
197 struct si_atom streamout_begin;
198 struct si_atom streamout_enable; /* must be after streamout_begin */
199 struct si_atom framebuffer;
200 struct si_atom msaa_sample_locs;
201 struct si_atom db_render_state;
202 struct si_atom dpbb_state;
203 struct si_atom msaa_config;
204 struct si_atom sample_mask;
205 struct si_atom cb_render_state;
206 struct si_atom blend_color;
207 struct si_atom clip_regs;
208 struct si_atom clip_state;
209 struct si_atom shader_pointers;
210 struct si_atom guardband;
211 struct si_atom scissors;
212 struct si_atom viewports;
213 struct si_atom stencil_ref;
214 struct si_atom spi_map;
215 struct si_atom scratch_state;
216 } s;
217 struct si_atom array[0];
218 };
219
220 #define SI_ATOM_BIT(name) (1 << (offsetof(union si_state_atoms, s.name) / \
221 sizeof(struct si_atom)))
222 #define SI_NUM_ATOMS (sizeof(union si_state_atoms)/sizeof(struct si_atom*))
223
224 static inline unsigned si_atoms_that_roll_context(void)
225 {
226 return (SI_ATOM_BIT(streamout_begin) |
227 SI_ATOM_BIT(streamout_enable) |
228 SI_ATOM_BIT(framebuffer) |
229 SI_ATOM_BIT(msaa_sample_locs) |
230 SI_ATOM_BIT(db_render_state) |
231 SI_ATOM_BIT(dpbb_state) |
232 SI_ATOM_BIT(msaa_config) |
233 SI_ATOM_BIT(sample_mask) |
234 SI_ATOM_BIT(cb_render_state) |
235 SI_ATOM_BIT(blend_color) |
236 SI_ATOM_BIT(clip_regs) |
237 SI_ATOM_BIT(clip_state) |
238 SI_ATOM_BIT(guardband) |
239 SI_ATOM_BIT(scissors) |
240 SI_ATOM_BIT(viewports) |
241 SI_ATOM_BIT(stencil_ref) |
242 SI_ATOM_BIT(spi_map) |
243 SI_ATOM_BIT(scratch_state));
244 }
245
246 struct si_shader_data {
247 uint32_t sh_base[SI_NUM_SHADERS];
248 };
249
250 /* The list of registers whose emitted values are remembered by si_context. */
251 enum si_tracked_reg {
252 SI_TRACKED_DB_RENDER_CONTROL, /* 2 consecutive registers */
253 SI_TRACKED_DB_COUNT_CONTROL,
254
255 SI_TRACKED_DB_RENDER_OVERRIDE2,
256 SI_TRACKED_DB_SHADER_CONTROL,
257
258 SI_TRACKED_CB_TARGET_MASK,
259 SI_TRACKED_CB_DCC_CONTROL,
260
261 SI_TRACKED_SX_PS_DOWNCONVERT, /* 3 consecutive registers */
262 SI_TRACKED_SX_BLEND_OPT_EPSILON,
263 SI_TRACKED_SX_BLEND_OPT_CONTROL,
264
265 SI_TRACKED_PA_SC_LINE_CNTL, /* 2 consecutive registers */
266 SI_TRACKED_PA_SC_AA_CONFIG,
267
268 SI_TRACKED_DB_EQAA,
269 SI_TRACKED_PA_SC_MODE_CNTL_1,
270
271 SI_TRACKED_PA_SU_SMALL_PRIM_FILTER_CNTL,
272
273 SI_TRACKED_PA_CL_VS_OUT_CNTL,
274 SI_TRACKED_PA_CL_CLIP_CNTL,
275
276 SI_TRACKED_PA_SC_BINNER_CNTL_0,
277 SI_TRACKED_DB_DFSM_CONTROL,
278
279 SI_TRACKED_PA_CL_GB_VERT_CLIP_ADJ, /* 4 consecutive registers */
280 SI_TRACKED_PA_CL_GB_VERT_DISC_ADJ,
281 SI_TRACKED_PA_CL_GB_HORZ_CLIP_ADJ,
282 SI_TRACKED_PA_CL_GB_HORZ_DISC_ADJ,
283
284 SI_NUM_TRACKED_REGS,
285 };
286
287 struct si_tracked_regs {
288 uint32_t reg_saved;
289 uint32_t reg_value[SI_NUM_TRACKED_REGS];
290 };
291
292 /* Private read-write buffer slots. */
293 enum {
294 SI_ES_RING_ESGS,
295 SI_GS_RING_ESGS,
296
297 SI_RING_GSVS,
298
299 SI_VS_STREAMOUT_BUF0,
300 SI_VS_STREAMOUT_BUF1,
301 SI_VS_STREAMOUT_BUF2,
302 SI_VS_STREAMOUT_BUF3,
303
304 SI_HS_CONST_DEFAULT_TESS_LEVELS,
305 SI_VS_CONST_INSTANCE_DIVISORS,
306 SI_VS_CONST_CLIP_PLANES,
307 SI_PS_CONST_POLY_STIPPLE,
308 SI_PS_CONST_SAMPLE_POSITIONS,
309
310 /* Image descriptor of color buffer 0 for KHR_blend_equation_advanced. */
311 SI_PS_IMAGE_COLORBUF0,
312 SI_PS_IMAGE_COLORBUF0_HI,
313 SI_PS_IMAGE_COLORBUF0_FMASK,
314 SI_PS_IMAGE_COLORBUF0_FMASK_HI,
315
316 SI_NUM_RW_BUFFERS,
317 };
318
319 /* Indices into sctx->descriptors, laid out so that gfx and compute pipelines
320 * are contiguous:
321 *
322 * 0 - rw buffers
323 * 1 - vertex const and shader buffers
324 * 2 - vertex samplers and images
325 * 3 - fragment const and shader buffer
326 * ...
327 * 11 - compute const and shader buffers
328 * 12 - compute samplers and images
329 */
330 enum {
331 SI_SHADER_DESCS_CONST_AND_SHADER_BUFFERS,
332 SI_SHADER_DESCS_SAMPLERS_AND_IMAGES,
333 SI_NUM_SHADER_DESCS,
334 };
335
336 #define SI_DESCS_RW_BUFFERS 0
337 #define SI_DESCS_FIRST_SHADER 1
338 #define SI_DESCS_FIRST_COMPUTE (SI_DESCS_FIRST_SHADER + \
339 PIPE_SHADER_COMPUTE * SI_NUM_SHADER_DESCS)
340 #define SI_NUM_DESCS (SI_DESCS_FIRST_SHADER + \
341 SI_NUM_SHADERS * SI_NUM_SHADER_DESCS)
342
343 #define SI_DESCS_SHADER_MASK(name) \
344 u_bit_consecutive(SI_DESCS_FIRST_SHADER + \
345 PIPE_SHADER_##name * SI_NUM_SHADER_DESCS, \
346 SI_NUM_SHADER_DESCS)
347
348 /* This represents descriptors in memory, such as buffer resources,
349 * image resources, and sampler states.
350 */
351 struct si_descriptors {
352 /* The list of descriptors in malloc'd memory. */
353 uint32_t *list;
354 /* The list in mapped GPU memory. */
355 uint32_t *gpu_list;
356
357 /* The buffer where the descriptors have been uploaded. */
358 struct r600_resource *buffer;
359 uint64_t gpu_address;
360
361 /* The maximum number of descriptors. */
362 uint32_t num_elements;
363
364 /* Slots that are used by currently-bound shaders.
365 * It determines which slots are uploaded.
366 */
367 uint32_t first_active_slot;
368 uint32_t num_active_slots;
369
370 /* The SH register offset relative to USER_DATA*_0 where the pointer
371 * to the descriptor array will be stored. */
372 short shader_userdata_offset;
373 /* The size of one descriptor. */
374 ubyte element_dw_size;
375 /* If there is only one slot enabled, bind it directly instead of
376 * uploading descriptors. -1 if disabled. */
377 signed char slot_index_to_bind_directly;
378 };
379
380 struct si_buffer_resources {
381 struct pipe_resource **buffers; /* this has num_buffers elements */
382
383 enum radeon_bo_usage shader_usage:4; /* READ, WRITE, or READWRITE */
384 enum radeon_bo_usage shader_usage_constbuf:4;
385 enum radeon_bo_priority priority:6;
386 enum radeon_bo_priority priority_constbuf:6;
387
388 /* The i-th bit is set if that element is enabled (non-NULL resource). */
389 unsigned enabled_mask;
390 };
391
392 #define si_pm4_state_changed(sctx, member) \
393 ((sctx)->queued.named.member != (sctx)->emitted.named.member)
394
395 #define si_pm4_state_enabled_and_changed(sctx, member) \
396 ((sctx)->queued.named.member && si_pm4_state_changed(sctx, member))
397
398 #define si_pm4_bind_state(sctx, member, value) \
399 do { \
400 (sctx)->queued.named.member = (value); \
401 (sctx)->dirty_states |= SI_STATE_BIT(member); \
402 } while(0)
403
404 #define si_pm4_delete_state(sctx, member, value) \
405 do { \
406 if ((sctx)->queued.named.member == (value)) { \
407 (sctx)->queued.named.member = NULL; \
408 } \
409 si_pm4_free_state(sctx, (struct si_pm4_state *)(value), \
410 SI_STATE_IDX(member)); \
411 } while(0)
412
413 /* si_descriptors.c */
414 void si_set_mutable_tex_desc_fields(struct si_screen *sscreen,
415 struct si_texture *tex,
416 const struct legacy_surf_level *base_level_info,
417 unsigned base_level, unsigned first_level,
418 unsigned block_width, bool is_stencil,
419 uint32_t *state);
420 void si_update_ps_colorbuf0_slot(struct si_context *sctx);
421 void si_get_pipe_constant_buffer(struct si_context *sctx, uint shader,
422 uint slot, struct pipe_constant_buffer *cbuf);
423 void si_get_shader_buffers(struct si_context *sctx,
424 enum pipe_shader_type shader,
425 uint start_slot, uint count,
426 struct pipe_shader_buffer *sbuf);
427 void si_set_ring_buffer(struct si_context *sctx, uint slot,
428 struct pipe_resource *buffer,
429 unsigned stride, unsigned num_records,
430 bool add_tid, bool swizzle,
431 unsigned element_size, unsigned index_stride, uint64_t offset);
432 void si_init_all_descriptors(struct si_context *sctx);
433 bool si_upload_vertex_buffer_descriptors(struct si_context *sctx);
434 bool si_upload_graphics_shader_descriptors(struct si_context *sctx);
435 bool si_upload_compute_shader_descriptors(struct si_context *sctx);
436 void si_release_all_descriptors(struct si_context *sctx);
437 void si_all_descriptors_begin_new_cs(struct si_context *sctx);
438 void si_all_resident_buffers_begin_new_cs(struct si_context *sctx);
439 void si_upload_const_buffer(struct si_context *sctx, struct r600_resource **rbuffer,
440 const uint8_t *ptr, unsigned size, uint32_t *const_offset);
441 void si_update_all_texture_descriptors(struct si_context *sctx);
442 void si_shader_change_notify(struct si_context *sctx);
443 void si_update_needs_color_decompress_masks(struct si_context *sctx);
444 void si_emit_graphics_shader_pointers(struct si_context *sctx);
445 void si_emit_compute_shader_pointers(struct si_context *sctx);
446 void si_set_rw_buffer(struct si_context *sctx,
447 uint slot, const struct pipe_constant_buffer *input);
448 void si_set_active_descriptors(struct si_context *sctx, unsigned desc_idx,
449 uint64_t new_active_mask);
450 void si_set_active_descriptors_for_shader(struct si_context *sctx,
451 struct si_shader_selector *sel);
452 bool si_bindless_descriptor_can_reclaim_slab(void *priv,
453 struct pb_slab_entry *entry);
454 struct pb_slab *si_bindless_descriptor_slab_alloc(void *priv, unsigned heap,
455 unsigned entry_size,
456 unsigned group_index);
457 void si_bindless_descriptor_slab_free(void *priv, struct pb_slab *pslab);
458 void si_rebind_buffer(struct si_context *sctx, struct pipe_resource *buf,
459 uint64_t old_va);
460 /* si_state.c */
461 void si_init_state_functions(struct si_context *sctx);
462 void si_init_screen_state_functions(struct si_screen *sscreen);
463 void
464 si_make_buffer_descriptor(struct si_screen *screen, struct r600_resource *buf,
465 enum pipe_format format,
466 unsigned offset, unsigned size,
467 uint32_t *state);
468 void
469 si_make_texture_descriptor(struct si_screen *screen,
470 struct si_texture *tex,
471 bool sampler,
472 enum pipe_texture_target target,
473 enum pipe_format pipe_format,
474 const unsigned char state_swizzle[4],
475 unsigned first_level, unsigned last_level,
476 unsigned first_layer, unsigned last_layer,
477 unsigned width, unsigned height, unsigned depth,
478 uint32_t *state,
479 uint32_t *fmask_state);
480 struct pipe_sampler_view *
481 si_create_sampler_view_custom(struct pipe_context *ctx,
482 struct pipe_resource *texture,
483 const struct pipe_sampler_view *state,
484 unsigned width0, unsigned height0,
485 unsigned force_level);
486 void si_update_fb_dirtiness_after_rendering(struct si_context *sctx);
487 void si_update_ps_iter_samples(struct si_context *sctx);
488 void si_save_qbo_state(struct si_context *sctx, struct si_qbo_state *st);
489 void si_set_occlusion_query_state(struct si_context *sctx,
490 bool old_perfect_enable);
491
492 /* si_state_binning.c */
493 void si_emit_dpbb_state(struct si_context *sctx);
494
495 /* si_state_shaders.c */
496 void *si_get_ir_binary(struct si_shader_selector *sel);
497 bool si_shader_cache_load_shader(struct si_screen *sscreen, void *ir_binary,
498 struct si_shader *shader);
499 bool si_shader_cache_insert_shader(struct si_screen *sscreen, void *ir_binary,
500 struct si_shader *shader,
501 bool insert_into_disk_cache);
502 bool si_update_shaders(struct si_context *sctx);
503 void si_init_shader_functions(struct si_context *sctx);
504 bool si_init_shader_cache(struct si_screen *sscreen);
505 void si_destroy_shader_cache(struct si_screen *sscreen);
506 void si_schedule_initial_compile(struct si_context *sctx, unsigned processor,
507 struct util_queue_fence *ready_fence,
508 struct si_compiler_ctx_state *compiler_ctx_state,
509 void *job, util_queue_execute_func execute);
510 void si_get_active_slot_masks(const struct tgsi_shader_info *info,
511 uint32_t *const_and_shader_buffers,
512 uint64_t *samplers_and_images);
513 void *si_get_blit_vs(struct si_context *sctx, enum blitter_attrib_type type,
514 unsigned num_layers);
515
516 /* si_state_draw.c */
517 void si_init_ia_multi_vgt_param_table(struct si_context *sctx);
518 void si_emit_cache_flush(struct si_context *sctx);
519 void si_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *dinfo);
520 void si_draw_rectangle(struct blitter_context *blitter,
521 void *vertex_elements_cso,
522 blitter_get_vs_func get_vs,
523 int x1, int y1, int x2, int y2,
524 float depth, unsigned num_instances,
525 enum blitter_attrib_type type,
526 const union blitter_attrib *attrib);
527 void si_trace_emit(struct si_context *sctx);
528
529 /* si_state_msaa.c */
530 void si_init_msaa_functions(struct si_context *sctx);
531 void si_emit_sample_locations(struct radeon_cmdbuf *cs, int nr_samples);
532
533 /* si_state_streamout.c */
534 void si_streamout_buffers_dirty(struct si_context *sctx);
535 void si_emit_streamout_end(struct si_context *sctx);
536 void si_update_prims_generated_query_state(struct si_context *sctx,
537 unsigned type, int diff);
538 void si_init_streamout_functions(struct si_context *sctx);
539
540
541 static inline unsigned si_get_constbuf_slot(unsigned slot)
542 {
543 /* Constant buffers are in slots [16..31], ascending */
544 return SI_NUM_SHADER_BUFFERS + slot;
545 }
546
547 static inline unsigned si_get_shaderbuf_slot(unsigned slot)
548 {
549 /* shader buffers are in slots [15..0], descending */
550 return SI_NUM_SHADER_BUFFERS - 1 - slot;
551 }
552
553 static inline unsigned si_get_sampler_slot(unsigned slot)
554 {
555 /* samplers are in slots [8..39], ascending */
556 return SI_NUM_IMAGES / 2 + slot;
557 }
558
559 static inline unsigned si_get_image_slot(unsigned slot)
560 {
561 /* images are in slots [15..0] (sampler slots [7..0]), descending */
562 return SI_NUM_IMAGES - 1 - slot;
563 }
564
565 #endif