1848b8605Smrg/*
2848b8605Smrg * Copyright 2012 Advanced Micro Devices, Inc.
3b8e80941Smrg * All Rights Reserved.
4848b8605Smrg *
5848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
6848b8605Smrg * copy of this software and associated documentation files (the "Software"),
7848b8605Smrg * to deal in the Software without restriction, including without limitation
8848b8605Smrg * on the rights to use, copy, modify, merge, publish, distribute, sub
9848b8605Smrg * license, and/or sell copies of the Software, and to permit persons to whom
10848b8605Smrg * the Software is furnished to do so, subject to the following conditions:
11848b8605Smrg *
12848b8605Smrg * The above copyright notice and this permission notice (including the next
13848b8605Smrg * paragraph) shall be included in all copies or substantial portions of the
14848b8605Smrg * Software.
15848b8605Smrg *
16848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17848b8605Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19848b8605Smrg * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20848b8605Smrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21848b8605Smrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22848b8605Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE.
23848b8605Smrg */
24848b8605Smrg
25848b8605Smrg#ifndef SI_STATE_H
26848b8605Smrg#define SI_STATE_H
27848b8605Smrg
28848b8605Smrg#include "si_pm4.h"
29b8e80941Smrg
30b8e80941Smrg#include "pipebuffer/pb_slab.h"
31b8e80941Smrg#include "util/u_blitter.h"
32b8e80941Smrg
33b8e80941Smrg#define SI_NUM_GRAPHICS_SHADERS (PIPE_SHADER_TESS_EVAL+1)
34b8e80941Smrg#define SI_NUM_SHADERS (PIPE_SHADER_COMPUTE+1)
35b8e80941Smrg
36b8e80941Smrg#define SI_NUM_VERTEX_BUFFERS		SI_MAX_ATTRIBS
37b8e80941Smrg#define SI_NUM_SAMPLERS			32 /* OpenGL textures units per shader */
38b8e80941Smrg#define SI_NUM_CONST_BUFFERS		16
39b8e80941Smrg#define SI_NUM_IMAGES			16
40b8e80941Smrg#define SI_NUM_SHADER_BUFFERS		16
41848b8605Smrg
42848b8605Smrgstruct si_screen;
43b8e80941Smrgstruct si_shader;
44b8e80941Smrgstruct si_shader_selector;
45b8e80941Smrgstruct si_texture;
46b8e80941Smrgstruct si_qbo_state;
47848b8605Smrg
48848b8605Smrgstruct si_state_blend {
49848b8605Smrg	struct si_pm4_state	pm4;
50848b8605Smrg	uint32_t		cb_target_mask;
51b8e80941Smrg	/* Set 0xf or 0x0 (4 bits) per render target if the following is
52b8e80941Smrg	 * true. ANDed with spi_shader_col_format.
53b8e80941Smrg	 */
54b8e80941Smrg	unsigned		cb_target_enabled_4bit;
55b8e80941Smrg	unsigned		blend_enable_4bit;
56b8e80941Smrg	unsigned		need_src_alpha_4bit;
57b8e80941Smrg	unsigned		commutative_4bit;
58b8e80941Smrg	bool			alpha_to_coverage:1;
59b8e80941Smrg	bool			alpha_to_one:1;
60b8e80941Smrg	bool			dual_src_blend:1;
61b8e80941Smrg	bool			logicop_enable:1;
62848b8605Smrg};
63848b8605Smrg
64b8e80941Smrgstruct si_state_rasterizer {
65848b8605Smrg	struct si_pm4_state	pm4;
66b8e80941Smrg	/* poly offset states for 16-bit, 24-bit, and 32-bit zbuffers */
67b8e80941Smrg	struct si_pm4_state	*pm4_poly_offset;
68b8e80941Smrg	unsigned		pa_sc_line_stipple;
69b8e80941Smrg	unsigned		pa_cl_clip_cntl;
70b8e80941Smrg	float			line_width;
71b8e80941Smrg	float			max_point_size;
72b8e80941Smrg	unsigned		sprite_coord_enable:8;
73b8e80941Smrg	unsigned		clip_plane_enable:8;
74b8e80941Smrg	unsigned		half_pixel_center:1;
75b8e80941Smrg	unsigned		flatshade:1;
76b8e80941Smrg	unsigned		two_side:1;
77b8e80941Smrg	unsigned		multisample_enable:1;
78b8e80941Smrg	unsigned		force_persample_interp:1;
79b8e80941Smrg	unsigned		line_stipple_enable:1;
80b8e80941Smrg	unsigned		poly_stipple_enable:1;
81b8e80941Smrg	unsigned		line_smooth:1;
82b8e80941Smrg	unsigned		poly_smooth:1;
83b8e80941Smrg	unsigned		uses_poly_offset:1;
84b8e80941Smrg	unsigned		clamp_fragment_color:1;
85b8e80941Smrg	unsigned		clamp_vertex_color:1;
86b8e80941Smrg	unsigned		rasterizer_discard:1;
87b8e80941Smrg	unsigned		scissor_enable:1;
88b8e80941Smrg	unsigned		clip_halfz:1;
89848b8605Smrg};
90848b8605Smrg
91b8e80941Smrgstruct si_dsa_stencil_ref_part {
92b8e80941Smrg	uint8_t			valuemask[2];
93b8e80941Smrg	uint8_t			writemask[2];
94848b8605Smrg};
95848b8605Smrg
96b8e80941Smrgstruct si_dsa_order_invariance {
97b8e80941Smrg	/** Whether the final result in Z/S buffers is guaranteed to be
98b8e80941Smrg	 * invariant under changes to the order in which fragments arrive. */
99b8e80941Smrg	bool zs:1;
100848b8605Smrg
101b8e80941Smrg	/** Whether the set of fragments that pass the combined Z/S test is
102b8e80941Smrg	 * guaranteed to be invariant under changes to the order in which
103b8e80941Smrg	 * fragments arrive. */
104b8e80941Smrg	bool pass_set:1;
105b8e80941Smrg
106b8e80941Smrg	/** Whether the last fragment that passes the combined Z/S test at each
107b8e80941Smrg	 * sample is guaranteed to be invariant under changes to the order in
108b8e80941Smrg	 * which fragments arrive. */
109b8e80941Smrg	bool pass_last:1;
110848b8605Smrg};
111848b8605Smrg
112848b8605Smrgstruct si_state_dsa {
113b8e80941Smrg	struct si_pm4_state		pm4;
114b8e80941Smrg	struct si_dsa_stencil_ref_part	stencil_ref;
115b8e80941Smrg
116b8e80941Smrg	/* 0 = without stencil buffer, 1 = when both Z and S buffers are present */
117b8e80941Smrg	struct si_dsa_order_invariance	order_invariance[2];
118b8e80941Smrg
119b8e80941Smrg	ubyte				alpha_func:3;
120b8e80941Smrg	bool				depth_enabled:1;
121b8e80941Smrg	bool				depth_write_enabled:1;
122b8e80941Smrg	bool				stencil_enabled:1;
123b8e80941Smrg	bool				stencil_write_enabled:1;
124b8e80941Smrg	bool				db_can_write:1;
125b8e80941Smrg
126b8e80941Smrg};
127b8e80941Smrg
128b8e80941Smrgstruct si_stencil_ref {
129b8e80941Smrg	struct pipe_stencil_ref		state;
130b8e80941Smrg	struct si_dsa_stencil_ref_part	dsa_part;
131848b8605Smrg};
132848b8605Smrg
133b8e80941Smrgstruct si_vertex_elements
134848b8605Smrg{
135b8e80941Smrg	struct si_resource		*instance_divisor_factor_buffer;
136b8e80941Smrg	uint32_t			rsrc_word3[SI_MAX_ATTRIBS];
137b8e80941Smrg	uint16_t			src_offset[SI_MAX_ATTRIBS];
138b8e80941Smrg	uint8_t				fix_fetch[SI_MAX_ATTRIBS];
139b8e80941Smrg	uint8_t				format_size[SI_MAX_ATTRIBS];
140b8e80941Smrg	uint8_t				vertex_buffer_index[SI_MAX_ATTRIBS];
141b8e80941Smrg
142b8e80941Smrg	uint8_t				count;
143b8e80941Smrg	bool				uses_instance_divisors;
144b8e80941Smrg
145b8e80941Smrg	uint16_t			first_vb_use_mask;
146b8e80941Smrg	/* Vertex buffer descriptor list size aligned for optimal prefetch. */
147b8e80941Smrg	uint16_t			desc_list_byte_size;
148b8e80941Smrg	uint16_t			instance_divisor_is_one; /* bitmask of inputs */
149b8e80941Smrg	uint16_t			instance_divisor_is_fetched;  /* bitmask of inputs */
150848b8605Smrg};
151848b8605Smrg
152848b8605Smrgunion si_state {
153848b8605Smrg	struct {
154848b8605Smrg		struct si_state_blend		*blend;
155848b8605Smrg		struct si_state_rasterizer	*rasterizer;
156848b8605Smrg		struct si_state_dsa		*dsa;
157b8e80941Smrg		struct si_pm4_state		*poly_offset;
158b8e80941Smrg		struct si_pm4_state		*ls;
159b8e80941Smrg		struct si_pm4_state		*hs;
160848b8605Smrg		struct si_pm4_state		*es;
161848b8605Smrg		struct si_pm4_state		*gs;
162b8e80941Smrg		struct si_pm4_state		*vgt_shader_config;
163848b8605Smrg		struct si_pm4_state		*vs;
164848b8605Smrg		struct si_pm4_state		*ps;
165848b8605Smrg	} named;
166848b8605Smrg	struct si_pm4_state	*array[0];
167848b8605Smrg};
168848b8605Smrg
169b8e80941Smrg#define SI_STATE_IDX(name) \
170b8e80941Smrg	(offsetof(union si_state, named.name) / sizeof(struct si_pm4_state *))
171b8e80941Smrg#define SI_STATE_BIT(name) (1 << SI_STATE_IDX(name))
172b8e80941Smrg#define SI_NUM_STATES (sizeof(union si_state) / sizeof(struct si_pm4_state *))
173848b8605Smrg
174b8e80941Smrgstatic inline unsigned si_states_that_always_roll_context(void)
175b8e80941Smrg{
176b8e80941Smrg	return (SI_STATE_BIT(blend) |
177b8e80941Smrg		SI_STATE_BIT(rasterizer) |
178b8e80941Smrg		SI_STATE_BIT(dsa) |
179b8e80941Smrg		SI_STATE_BIT(poly_offset) |
180b8e80941Smrg		SI_STATE_BIT(vgt_shader_config));
181b8e80941Smrg}
182b8e80941Smrg
183b8e80941Smrgunion si_state_atoms {
184b8e80941Smrg	struct {
185b8e80941Smrg		/* The order matters. */
186b8e80941Smrg		struct si_atom render_cond;
187b8e80941Smrg		struct si_atom streamout_begin;
188b8e80941Smrg		struct si_atom streamout_enable; /* must be after streamout_begin */
189b8e80941Smrg		struct si_atom framebuffer;
190b8e80941Smrg		struct si_atom msaa_sample_locs;
191b8e80941Smrg		struct si_atom db_render_state;
192b8e80941Smrg		struct si_atom dpbb_state;
193b8e80941Smrg		struct si_atom msaa_config;
194b8e80941Smrg		struct si_atom sample_mask;
195b8e80941Smrg		struct si_atom cb_render_state;
196b8e80941Smrg		struct si_atom blend_color;
197b8e80941Smrg		struct si_atom clip_regs;
198b8e80941Smrg		struct si_atom clip_state;
199b8e80941Smrg		struct si_atom shader_pointers;
200b8e80941Smrg		struct si_atom guardband;
201b8e80941Smrg		struct si_atom scissors;
202b8e80941Smrg		struct si_atom viewports;
203b8e80941Smrg		struct si_atom stencil_ref;
204b8e80941Smrg		struct si_atom spi_map;
205b8e80941Smrg		struct si_atom scratch_state;
206b8e80941Smrg		struct si_atom window_rectangles;
207b8e80941Smrg	} s;
208b8e80941Smrg	struct si_atom array[0];
209b8e80941Smrg};
210848b8605Smrg
211b8e80941Smrg#define SI_ATOM_BIT(name) (1 << (offsetof(union si_state_atoms, s.name) / \
212b8e80941Smrg			         sizeof(struct si_atom)))
213b8e80941Smrg#define SI_NUM_ATOMS (sizeof(union si_state_atoms)/sizeof(struct si_atom*))
214848b8605Smrg
215b8e80941Smrgstatic inline unsigned si_atoms_that_always_roll_context(void)
216b8e80941Smrg{
217b8e80941Smrg	return (SI_ATOM_BIT(streamout_begin) |
218b8e80941Smrg		SI_ATOM_BIT(streamout_enable) |
219b8e80941Smrg		SI_ATOM_BIT(framebuffer) |
220b8e80941Smrg		SI_ATOM_BIT(msaa_sample_locs) |
221b8e80941Smrg		SI_ATOM_BIT(sample_mask) |
222b8e80941Smrg		SI_ATOM_BIT(blend_color) |
223b8e80941Smrg		SI_ATOM_BIT(clip_state) |
224b8e80941Smrg		SI_ATOM_BIT(scissors) |
225b8e80941Smrg		SI_ATOM_BIT(viewports) |
226b8e80941Smrg		SI_ATOM_BIT(stencil_ref) |
227b8e80941Smrg		SI_ATOM_BIT(scratch_state) |
228b8e80941Smrg		SI_ATOM_BIT(window_rectangles));
229b8e80941Smrg}
230b8e80941Smrg
231b8e80941Smrgstruct si_shader_data {
232b8e80941Smrg	uint32_t		sh_base[SI_NUM_SHADERS];
233b8e80941Smrg};
234848b8605Smrg
235b8e80941Smrg/* The list of registers whose emitted values are remembered by si_context. */
236b8e80941Smrgenum si_tracked_reg {
237b8e80941Smrg	SI_TRACKED_DB_RENDER_CONTROL, /* 2 consecutive registers */
238b8e80941Smrg	SI_TRACKED_DB_COUNT_CONTROL,
239848b8605Smrg
240b8e80941Smrg	SI_TRACKED_DB_RENDER_OVERRIDE2,
241b8e80941Smrg	SI_TRACKED_DB_SHADER_CONTROL,
242848b8605Smrg
243b8e80941Smrg	SI_TRACKED_CB_TARGET_MASK,
244b8e80941Smrg	SI_TRACKED_CB_DCC_CONTROL,
245848b8605Smrg
246b8e80941Smrg	SI_TRACKED_SX_PS_DOWNCONVERT, /* 3 consecutive registers */
247b8e80941Smrg	SI_TRACKED_SX_BLEND_OPT_EPSILON,
248b8e80941Smrg	SI_TRACKED_SX_BLEND_OPT_CONTROL,
249848b8605Smrg
250b8e80941Smrg	SI_TRACKED_PA_SC_LINE_CNTL, /* 2 consecutive registers */
251b8e80941Smrg	SI_TRACKED_PA_SC_AA_CONFIG,
252848b8605Smrg
253b8e80941Smrg	SI_TRACKED_DB_EQAA,
254b8e80941Smrg	SI_TRACKED_PA_SC_MODE_CNTL_1,
255b8e80941Smrg
256b8e80941Smrg	SI_TRACKED_PA_SU_PRIM_FILTER_CNTL,
257b8e80941Smrg	SI_TRACKED_PA_SU_SMALL_PRIM_FILTER_CNTL,
258b8e80941Smrg
259b8e80941Smrg	SI_TRACKED_PA_CL_VS_OUT_CNTL,
260b8e80941Smrg	SI_TRACKED_PA_CL_CLIP_CNTL,
261b8e80941Smrg
262b8e80941Smrg	SI_TRACKED_PA_SC_BINNER_CNTL_0,
263b8e80941Smrg	SI_TRACKED_DB_DFSM_CONTROL,
264b8e80941Smrg
265b8e80941Smrg	SI_TRACKED_PA_CL_GB_VERT_CLIP_ADJ, /* 4 consecutive registers */
266b8e80941Smrg	SI_TRACKED_PA_CL_GB_VERT_DISC_ADJ,
267b8e80941Smrg	SI_TRACKED_PA_CL_GB_HORZ_CLIP_ADJ,
268b8e80941Smrg	SI_TRACKED_PA_CL_GB_HORZ_DISC_ADJ,
269b8e80941Smrg
270b8e80941Smrg	SI_TRACKED_PA_SU_HARDWARE_SCREEN_OFFSET,
271b8e80941Smrg	SI_TRACKED_PA_SU_VTX_CNTL,
272b8e80941Smrg
273b8e80941Smrg	SI_TRACKED_PA_SC_CLIPRECT_RULE,
274b8e80941Smrg
275b8e80941Smrg	SI_TRACKED_VGT_ESGS_RING_ITEMSIZE,
276b8e80941Smrg
277b8e80941Smrg	SI_TRACKED_VGT_GSVS_RING_OFFSET_1, /* 4 consecutive registers */
278b8e80941Smrg	SI_TRACKED_VGT_GSVS_RING_OFFSET_2,
279b8e80941Smrg	SI_TRACKED_VGT_GSVS_RING_OFFSET_3,
280b8e80941Smrg	SI_TRACKED_VGT_GS_OUT_PRIM_TYPE,
281b8e80941Smrg
282b8e80941Smrg	SI_TRACKED_VGT_GSVS_RING_ITEMSIZE,
283b8e80941Smrg	SI_TRACKED_VGT_GS_MAX_VERT_OUT,
284b8e80941Smrg
285b8e80941Smrg	SI_TRACKED_VGT_GS_VERT_ITEMSIZE, /* 4 consecutive registers */
286b8e80941Smrg	SI_TRACKED_VGT_GS_VERT_ITEMSIZE_1,
287b8e80941Smrg	SI_TRACKED_VGT_GS_VERT_ITEMSIZE_2,
288b8e80941Smrg	SI_TRACKED_VGT_GS_VERT_ITEMSIZE_3,
289b8e80941Smrg
290b8e80941Smrg	SI_TRACKED_VGT_GS_INSTANCE_CNT,
291b8e80941Smrg	SI_TRACKED_VGT_GS_ONCHIP_CNTL,
292b8e80941Smrg	SI_TRACKED_VGT_GS_MAX_PRIMS_PER_SUBGROUP,
293b8e80941Smrg	SI_TRACKED_VGT_GS_MODE,
294b8e80941Smrg	SI_TRACKED_VGT_PRIMITIVEID_EN,
295b8e80941Smrg	SI_TRACKED_VGT_REUSE_OFF,
296b8e80941Smrg	SI_TRACKED_SPI_VS_OUT_CONFIG,
297b8e80941Smrg	SI_TRACKED_SPI_SHADER_POS_FORMAT,
298b8e80941Smrg	SI_TRACKED_PA_CL_VTE_CNTL,
299b8e80941Smrg
300b8e80941Smrg	SI_TRACKED_SPI_PS_INPUT_ENA, /* 2 consecutive registers */
301b8e80941Smrg	SI_TRACKED_SPI_PS_INPUT_ADDR,
302b8e80941Smrg
303b8e80941Smrg	SI_TRACKED_SPI_BARYC_CNTL,
304b8e80941Smrg	SI_TRACKED_SPI_PS_IN_CONTROL,
305b8e80941Smrg
306b8e80941Smrg	SI_TRACKED_SPI_SHADER_Z_FORMAT, /* 2 consecutive registers */
307b8e80941Smrg	SI_TRACKED_SPI_SHADER_COL_FORMAT,
308b8e80941Smrg
309b8e80941Smrg	SI_TRACKED_CB_SHADER_MASK,
310b8e80941Smrg	SI_TRACKED_VGT_TF_PARAM,
311b8e80941Smrg	SI_TRACKED_VGT_VERTEX_REUSE_BLOCK_CNTL,
312b8e80941Smrg
313b8e80941Smrg	SI_NUM_TRACKED_REGS,
314848b8605Smrg};
315848b8605Smrg
316b8e80941Smrgstruct si_tracked_regs {
317b8e80941Smrg	uint64_t		reg_saved;
318b8e80941Smrg	uint32_t		reg_value[SI_NUM_TRACKED_REGS];
319b8e80941Smrg	uint32_t		spi_ps_input_cntl[32];
320848b8605Smrg};
321848b8605Smrg
322b8e80941Smrg/* Private read-write buffer slots. */
323b8e80941Smrgenum {
324b8e80941Smrg	SI_ES_RING_ESGS,
325b8e80941Smrg	SI_GS_RING_ESGS,
326b8e80941Smrg
327b8e80941Smrg	SI_RING_GSVS,
328b8e80941Smrg
329b8e80941Smrg	SI_VS_STREAMOUT_BUF0,
330b8e80941Smrg	SI_VS_STREAMOUT_BUF1,
331b8e80941Smrg	SI_VS_STREAMOUT_BUF2,
332b8e80941Smrg	SI_VS_STREAMOUT_BUF3,
333b8e80941Smrg
334b8e80941Smrg	SI_HS_CONST_DEFAULT_TESS_LEVELS,
335b8e80941Smrg	SI_VS_CONST_INSTANCE_DIVISORS,
336b8e80941Smrg	SI_VS_CONST_CLIP_PLANES,
337b8e80941Smrg	SI_PS_CONST_POLY_STIPPLE,
338b8e80941Smrg	SI_PS_CONST_SAMPLE_POSITIONS,
339b8e80941Smrg
340b8e80941Smrg	/* Image descriptor of color buffer 0 for KHR_blend_equation_advanced. */
341b8e80941Smrg	SI_PS_IMAGE_COLORBUF0,
342b8e80941Smrg	SI_PS_IMAGE_COLORBUF0_HI,
343b8e80941Smrg	SI_PS_IMAGE_COLORBUF0_FMASK,
344b8e80941Smrg	SI_PS_IMAGE_COLORBUF0_FMASK_HI,
345b8e80941Smrg
346b8e80941Smrg	SI_NUM_RW_BUFFERS,
347b8e80941Smrg};
348b8e80941Smrg
349b8e80941Smrg/* Indices into sctx->descriptors, laid out so that gfx and compute pipelines
350b8e80941Smrg * are contiguous:
351b8e80941Smrg *
352b8e80941Smrg *  0 - rw buffers
353b8e80941Smrg *  1 - vertex const and shader buffers
354b8e80941Smrg *  2 - vertex samplers and images
355b8e80941Smrg *  3 - fragment const and shader buffer
356b8e80941Smrg *   ...
357b8e80941Smrg *  11 - compute const and shader buffers
358b8e80941Smrg *  12 - compute samplers and images
359b8e80941Smrg */
360b8e80941Smrgenum {
361b8e80941Smrg	SI_SHADER_DESCS_CONST_AND_SHADER_BUFFERS,
362b8e80941Smrg	SI_SHADER_DESCS_SAMPLERS_AND_IMAGES,
363b8e80941Smrg	SI_NUM_SHADER_DESCS,
364b8e80941Smrg};
365b8e80941Smrg
366b8e80941Smrg#define SI_DESCS_RW_BUFFERS            0
367b8e80941Smrg#define SI_DESCS_FIRST_SHADER          1
368b8e80941Smrg#define SI_DESCS_FIRST_COMPUTE         (SI_DESCS_FIRST_SHADER + \
369b8e80941Smrg                                        PIPE_SHADER_COMPUTE * SI_NUM_SHADER_DESCS)
370b8e80941Smrg#define SI_NUM_DESCS                   (SI_DESCS_FIRST_SHADER + \
371b8e80941Smrg                                        SI_NUM_SHADERS * SI_NUM_SHADER_DESCS)
372b8e80941Smrg
373b8e80941Smrg#define SI_DESCS_SHADER_MASK(name) \
374b8e80941Smrg	u_bit_consecutive(SI_DESCS_FIRST_SHADER + \
375b8e80941Smrg			  PIPE_SHADER_##name * SI_NUM_SHADER_DESCS, \
376b8e80941Smrg			  SI_NUM_SHADER_DESCS)
377b8e80941Smrg
378b8e80941Smrg/* This represents descriptors in memory, such as buffer resources,
379b8e80941Smrg * image resources, and sampler states.
380b8e80941Smrg */
381b8e80941Smrgstruct si_descriptors {
382b8e80941Smrg	/* The list of descriptors in malloc'd memory. */
383b8e80941Smrg	uint32_t *list;
384b8e80941Smrg	/* The list in mapped GPU memory. */
385b8e80941Smrg	uint32_t *gpu_list;
386b8e80941Smrg
387b8e80941Smrg	/* The buffer where the descriptors have been uploaded. */
388b8e80941Smrg	struct si_resource *buffer;
389b8e80941Smrg	uint64_t gpu_address;
390b8e80941Smrg
391b8e80941Smrg	/* The maximum number of descriptors. */
392b8e80941Smrg	uint32_t num_elements;
393b8e80941Smrg
394b8e80941Smrg	/* Slots that are used by currently-bound shaders.
395b8e80941Smrg	 * It determines which slots are uploaded.
396b8e80941Smrg	 */
397b8e80941Smrg	uint32_t first_active_slot;
398b8e80941Smrg	uint32_t num_active_slots;
399b8e80941Smrg
400b8e80941Smrg	/* The SH register offset relative to USER_DATA*_0 where the pointer
401b8e80941Smrg	 * to the descriptor array will be stored. */
402b8e80941Smrg	short shader_userdata_offset;
403b8e80941Smrg	/* The size of one descriptor. */
404b8e80941Smrg	ubyte element_dw_size;
405b8e80941Smrg	/* If there is only one slot enabled, bind it directly instead of
406b8e80941Smrg	 * uploading descriptors. -1 if disabled. */
407b8e80941Smrg	signed char slot_index_to_bind_directly;
408848b8605Smrg};
409848b8605Smrg
410848b8605Smrgstruct si_buffer_resources {
411848b8605Smrg	struct pipe_resource		**buffers; /* this has num_buffers elements */
412b8e80941Smrg	unsigned			*offsets; /* this has num_buffers elements */
413848b8605Smrg
414b8e80941Smrg	enum radeon_bo_priority		priority:6;
415b8e80941Smrg	enum radeon_bo_priority		priority_constbuf:6;
416b8e80941Smrg
417b8e80941Smrg	/* The i-th bit is set if that element is enabled (non-NULL resource). */
418b8e80941Smrg	unsigned			enabled_mask;
419b8e80941Smrg	unsigned			writable_mask;
420b8e80941Smrg};
421848b8605Smrg
422848b8605Smrg#define si_pm4_state_changed(sctx, member) \
423848b8605Smrg	((sctx)->queued.named.member != (sctx)->emitted.named.member)
424848b8605Smrg
425b8e80941Smrg#define si_pm4_state_enabled_and_changed(sctx, member) \
426b8e80941Smrg	((sctx)->queued.named.member && si_pm4_state_changed(sctx, member))
427b8e80941Smrg
428848b8605Smrg#define si_pm4_bind_state(sctx, member, value) \
429848b8605Smrg	do { \
430848b8605Smrg		(sctx)->queued.named.member = (value); \
431b8e80941Smrg		(sctx)->dirty_states |= SI_STATE_BIT(member); \
432848b8605Smrg	} while(0)
433848b8605Smrg
434848b8605Smrg#define si_pm4_delete_state(sctx, member, value) \
435848b8605Smrg	do { \
436848b8605Smrg		if ((sctx)->queued.named.member == (value)) { \
437848b8605Smrg			(sctx)->queued.named.member = NULL; \
438848b8605Smrg		} \
439848b8605Smrg		si_pm4_free_state(sctx, (struct si_pm4_state *)(value), \
440b8e80941Smrg				  SI_STATE_IDX(member)); \
441848b8605Smrg	} while(0)
442848b8605Smrg
443848b8605Smrg/* si_descriptors.c */
444b8e80941Smrgvoid si_set_mutable_tex_desc_fields(struct si_screen *sscreen,
445b8e80941Smrg				    struct si_texture *tex,
446b8e80941Smrg				    const struct legacy_surf_level *base_level_info,
447b8e80941Smrg				    unsigned base_level, unsigned first_level,
448b8e80941Smrg				    unsigned block_width, bool is_stencil,
449b8e80941Smrg				    uint32_t *state);
450b8e80941Smrgvoid si_update_ps_colorbuf0_slot(struct si_context *sctx);
451b8e80941Smrgvoid si_get_pipe_constant_buffer(struct si_context *sctx, uint shader,
452b8e80941Smrg				 uint slot, struct pipe_constant_buffer *cbuf);
453b8e80941Smrgvoid si_get_shader_buffers(struct si_context *sctx,
454b8e80941Smrg			   enum pipe_shader_type shader,
455b8e80941Smrg			   uint start_slot, uint count,
456b8e80941Smrg			   struct pipe_shader_buffer *sbuf);
457b8e80941Smrgvoid si_set_ring_buffer(struct si_context *sctx, uint slot,
458b8e80941Smrg			struct pipe_resource *buffer,
459848b8605Smrg			unsigned stride, unsigned num_records,
460848b8605Smrg			bool add_tid, bool swizzle,
461b8e80941Smrg			unsigned element_size, unsigned index_stride, uint64_t offset);
462848b8605Smrgvoid si_init_all_descriptors(struct si_context *sctx);
463b8e80941Smrgbool si_upload_vertex_buffer_descriptors(struct si_context *sctx);
464b8e80941Smrgbool si_upload_graphics_shader_descriptors(struct si_context *sctx);
465b8e80941Smrgbool si_upload_compute_shader_descriptors(struct si_context *sctx);
466848b8605Smrgvoid si_release_all_descriptors(struct si_context *sctx);
467b8e80941Smrgvoid si_gfx_resources_add_all_to_bo_list(struct si_context *sctx);
468b8e80941Smrgvoid si_compute_resources_add_all_to_bo_list(struct si_context *sctx);
469848b8605Smrgvoid si_all_descriptors_begin_new_cs(struct si_context *sctx);
470b8e80941Smrgvoid si_upload_const_buffer(struct si_context *sctx, struct si_resource **buf,
471848b8605Smrg			    const uint8_t *ptr, unsigned size, uint32_t *const_offset);
472b8e80941Smrgvoid si_update_all_texture_descriptors(struct si_context *sctx);
473b8e80941Smrgvoid si_shader_change_notify(struct si_context *sctx);
474b8e80941Smrgvoid si_update_needs_color_decompress_masks(struct si_context *sctx);
475b8e80941Smrgvoid si_emit_graphics_shader_pointers(struct si_context *sctx);
476b8e80941Smrgvoid si_emit_compute_shader_pointers(struct si_context *sctx);
477b8e80941Smrgvoid si_set_rw_buffer(struct si_context *sctx,
478b8e80941Smrg		      uint slot, const struct pipe_constant_buffer *input);
479b8e80941Smrgvoid si_set_rw_shader_buffer(struct si_context *sctx, uint slot,
480b8e80941Smrg			     const struct pipe_shader_buffer *sbuffer);
481b8e80941Smrgvoid si_set_active_descriptors(struct si_context *sctx, unsigned desc_idx,
482b8e80941Smrg			       uint64_t new_active_mask);
483b8e80941Smrgvoid si_set_active_descriptors_for_shader(struct si_context *sctx,
484b8e80941Smrg					  struct si_shader_selector *sel);
485b8e80941Smrgbool si_bindless_descriptor_can_reclaim_slab(void *priv,
486b8e80941Smrg					     struct pb_slab_entry *entry);
487b8e80941Smrgstruct pb_slab *si_bindless_descriptor_slab_alloc(void *priv, unsigned heap,
488b8e80941Smrg						  unsigned entry_size,
489b8e80941Smrg						  unsigned group_index);
490b8e80941Smrgvoid si_bindless_descriptor_slab_free(void *priv, struct pb_slab *pslab);
491b8e80941Smrgvoid si_rebind_buffer(struct si_context *sctx, struct pipe_resource *buf);
492848b8605Smrg/* si_state.c */
493b8e80941Smrgvoid si_init_state_compute_functions(struct si_context *sctx);
494848b8605Smrgvoid si_init_state_functions(struct si_context *sctx);
495b8e80941Smrgvoid si_init_screen_state_functions(struct si_screen *sscreen);
496b8e80941Smrgvoid
497b8e80941Smrgsi_make_buffer_descriptor(struct si_screen *screen, struct si_resource *buf,
498b8e80941Smrg			  enum pipe_format format,
499b8e80941Smrg			  unsigned offset, unsigned size,
500b8e80941Smrg			  uint32_t *state);
501b8e80941Smrgvoid
502b8e80941Smrgsi_make_texture_descriptor(struct si_screen *screen,
503b8e80941Smrg			   struct si_texture *tex,
504b8e80941Smrg			   bool sampler,
505b8e80941Smrg			   enum pipe_texture_target target,
506b8e80941Smrg			   enum pipe_format pipe_format,
507b8e80941Smrg			   const unsigned char state_swizzle[4],
508b8e80941Smrg			   unsigned first_level, unsigned last_level,
509b8e80941Smrg			   unsigned first_layer, unsigned last_layer,
510b8e80941Smrg			   unsigned width, unsigned height, unsigned depth,
511b8e80941Smrg			   uint32_t *state,
512b8e80941Smrg			   uint32_t *fmask_state);
513b8e80941Smrgstruct pipe_sampler_view *
514b8e80941Smrgsi_create_sampler_view_custom(struct pipe_context *ctx,
515b8e80941Smrg			      struct pipe_resource *texture,
516b8e80941Smrg			      const struct pipe_sampler_view *state,
517b8e80941Smrg			      unsigned width0, unsigned height0,
518b8e80941Smrg			      unsigned force_level);
519b8e80941Smrgvoid si_update_fb_dirtiness_after_rendering(struct si_context *sctx);
520b8e80941Smrgvoid si_update_ps_iter_samples(struct si_context *sctx);
521b8e80941Smrgvoid si_save_qbo_state(struct si_context *sctx, struct si_qbo_state *st);
522b8e80941Smrgvoid si_set_occlusion_query_state(struct si_context *sctx,
523b8e80941Smrg				  bool old_perfect_enable);
524b8e80941Smrg
525b8e80941Smrg/* si_state_binning.c */
526b8e80941Smrgvoid si_emit_dpbb_state(struct si_context *sctx);
527b8e80941Smrg
528b8e80941Smrg/* si_state_shaders.c */
529b8e80941Smrgvoid *si_get_ir_binary(struct si_shader_selector *sel);
530b8e80941Smrgbool si_shader_cache_load_shader(struct si_screen *sscreen, void *ir_binary,
531b8e80941Smrg				 struct si_shader *shader);
532b8e80941Smrgbool si_shader_cache_insert_shader(struct si_screen *sscreen, void *ir_binary,
533b8e80941Smrg				   struct si_shader *shader,
534b8e80941Smrg				   bool insert_into_disk_cache);
535b8e80941Smrgbool si_update_shaders(struct si_context *sctx);
536b8e80941Smrgvoid si_init_shader_functions(struct si_context *sctx);
537b8e80941Smrgbool si_init_shader_cache(struct si_screen *sscreen);
538b8e80941Smrgvoid si_destroy_shader_cache(struct si_screen *sscreen);
539b8e80941Smrgvoid si_schedule_initial_compile(struct si_context *sctx, unsigned processor,
540b8e80941Smrg				 struct util_queue_fence *ready_fence,
541b8e80941Smrg				 struct si_compiler_ctx_state *compiler_ctx_state,
542b8e80941Smrg				 void *job, util_queue_execute_func execute);
543b8e80941Smrgvoid si_get_active_slot_masks(const struct tgsi_shader_info *info,
544b8e80941Smrg			      uint32_t *const_and_shader_buffers,
545b8e80941Smrg			      uint64_t *samplers_and_images);
546848b8605Smrg
547848b8605Smrg/* si_state_draw.c */
548b8e80941Smrgvoid si_emit_cache_flush(struct si_context *sctx);
549b8e80941Smrgvoid si_trace_emit(struct si_context *sctx);
550b8e80941Smrgvoid si_init_draw_functions(struct si_context *sctx);
551b8e80941Smrg
552b8e80941Smrg/* si_state_msaa.c */
553b8e80941Smrgvoid si_init_msaa_functions(struct si_context *sctx);
554b8e80941Smrgvoid si_emit_sample_locations(struct radeon_cmdbuf *cs, int nr_samples);
555b8e80941Smrg
556b8e80941Smrg/* si_state_streamout.c */
557b8e80941Smrgvoid si_streamout_buffers_dirty(struct si_context *sctx);
558b8e80941Smrgvoid si_emit_streamout_end(struct si_context *sctx);
559b8e80941Smrgvoid si_update_prims_generated_query_state(struct si_context *sctx,
560b8e80941Smrg					   unsigned type, int diff);
561b8e80941Smrgvoid si_init_streamout_functions(struct si_context *sctx);
562b8e80941Smrg
563b8e80941Smrg
564b8e80941Smrgstatic inline unsigned si_get_constbuf_slot(unsigned slot)
565b8e80941Smrg{
566b8e80941Smrg	/* Constant buffers are in slots [16..31], ascending */
567b8e80941Smrg	return SI_NUM_SHADER_BUFFERS + slot;
568b8e80941Smrg}
569b8e80941Smrg
570b8e80941Smrgstatic inline unsigned si_get_shaderbuf_slot(unsigned slot)
571b8e80941Smrg{
572b8e80941Smrg	/* shader buffers are in slots [15..0], descending */
573b8e80941Smrg	return SI_NUM_SHADER_BUFFERS - 1 - slot;
574b8e80941Smrg}
575b8e80941Smrg
576b8e80941Smrgstatic inline unsigned si_get_sampler_slot(unsigned slot)
577b8e80941Smrg{
578b8e80941Smrg	/* samplers are in slots [8..39], ascending */
579b8e80941Smrg	return SI_NUM_IMAGES / 2 + slot;
580b8e80941Smrg}
581b8e80941Smrg
582b8e80941Smrgstatic inline unsigned si_get_image_slot(unsigned slot)
583b8e80941Smrg{
584b8e80941Smrg	/* images are in slots [15..0] (sampler slots [7..0]), descending */
585b8e80941Smrg	return SI_NUM_IMAGES - 1 - slot;
586b8e80941Smrg}
587848b8605Smrg
588848b8605Smrg#endif
589