radeonsi: Simplify shader PM4 state handling
[mesa.git] / src / gallium / drivers / radeonsi / si_state.h
1 /*
2 * Copyright 2012 Advanced Micro Devices, Inc.
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 * Christian König <christian.koenig@amd.com>
25 */
26
27 #ifndef SI_STATE_H
28 #define SI_STATE_H
29
30 #include "si_pm4.h"
31 #include "../radeon/r600_pipe_common.h"
32
33 struct si_state_blend {
34 struct si_pm4_state pm4;
35 uint32_t cb_target_mask;
36 bool alpha_to_one;
37 };
38
39 struct si_state_viewport {
40 struct si_pm4_state pm4;
41 struct pipe_viewport_state viewport;
42 };
43
44 struct si_state_rasterizer {
45 struct si_pm4_state pm4;
46 bool flatshade;
47 bool two_side;
48 bool multisample_enable;
49 bool line_stipple_enable;
50 unsigned sprite_coord_enable;
51 unsigned pa_sc_line_stipple;
52 unsigned pa_su_sc_mode_cntl;
53 unsigned pa_cl_clip_cntl;
54 unsigned pa_cl_vs_out_cntl;
55 unsigned clip_plane_enable;
56 float offset_units;
57 float offset_scale;
58 };
59
60 struct si_state_dsa {
61 struct si_pm4_state pm4;
62 float alpha_ref;
63 unsigned alpha_func;
64 unsigned db_render_control;
65 uint8_t valuemask[2];
66 uint8_t writemask[2];
67 };
68
69 struct si_vertex_element
70 {
71 unsigned count;
72 uint32_t rsrc_word3[PIPE_MAX_ATTRIBS];
73 struct pipe_vertex_element elements[PIPE_MAX_ATTRIBS];
74 };
75
76 union si_state {
77 struct {
78 struct si_pm4_state *init;
79 struct si_state_blend *blend;
80 struct si_pm4_state *blend_color;
81 struct si_pm4_state *clip;
82 struct si_pm4_state *sample_mask;
83 struct si_pm4_state *scissor;
84 struct si_state_viewport *viewport;
85 struct si_pm4_state *framebuffer;
86 struct si_state_rasterizer *rasterizer;
87 struct si_state_dsa *dsa;
88 struct si_pm4_state *fb_rs;
89 struct si_pm4_state *fb_blend;
90 struct si_pm4_state *dsa_stencil_ref;
91 struct si_pm4_state *es;
92 struct si_pm4_state *gs;
93 struct si_pm4_state *gs_rings;
94 struct si_pm4_state *gs_sampler;
95 struct si_pm4_state *gs_onoff;
96 struct si_pm4_state *vs;
97 struct si_pm4_state *vs_sampler;
98 struct si_pm4_state *ps;
99 struct si_pm4_state *ps_sampler;
100 struct si_pm4_state *spi;
101 struct si_pm4_state *vertex_buffers;
102 struct si_pm4_state *draw_info;
103 struct si_pm4_state *draw;
104 } named;
105 struct si_pm4_state *array[0];
106 };
107
108 #define NUM_TEX_UNITS 16
109
110 /* User sampler views: 0..15
111 * FMASK sampler views: 16..31 (no sampler states)
112 */
113 #define FMASK_TEX_OFFSET NUM_TEX_UNITS
114 #define NUM_SAMPLER_VIEWS (FMASK_TEX_OFFSET+NUM_TEX_UNITS)
115 #define NUM_SAMPLER_STATES NUM_TEX_UNITS
116
117 #define NUM_PIPE_CONST_BUFFERS 16
118 #define NUM_CONST_BUFFERS 19
119
120 /* This represents resource descriptors in memory, such as buffer resources,
121 * image resources, and sampler states.
122 */
123 struct si_descriptors {
124 struct r600_atom atom;
125
126 /* The size of one resource descriptor. */
127 unsigned element_dw_size;
128 /* The maximum number of resource descriptors. */
129 unsigned num_elements;
130
131 /* The buffer where resource descriptors are stored. */
132 struct r600_resource *buffer;
133
134 /* The i-th bit is set if that element is dirty (changed but not emitted). */
135 unsigned dirty_mask;
136 /* The i-th bit is set if that element is enabled (non-NULL resource). */
137 unsigned enabled_mask;
138
139 /* We can't update descriptors directly because the GPU might be
140 * reading them at the same time, so we have to update them
141 * in a copy-on-write manner. Each such copy is called a context,
142 * which is just another array descriptors in the same buffer. */
143 unsigned current_context_id;
144 /* The size of a context, should be equal to 4*element_dw_size*num_elements. */
145 unsigned context_size;
146
147 /* The shader userdata register where the 64-bit pointer to the descriptor
148 * array will be stored. */
149 unsigned shader_userdata_reg;
150 };
151
152 struct si_sampler_views {
153 struct si_descriptors desc;
154 struct pipe_sampler_view *views[NUM_SAMPLER_VIEWS];
155 uint32_t *desc_data[NUM_SAMPLER_VIEWS];
156 };
157
158 struct si_buffer_resources {
159 struct si_descriptors desc;
160 unsigned num_buffers;
161 enum radeon_bo_usage shader_usage; /* READ, WRITE, or READWRITE */
162 struct pipe_resource **buffers; /* this has num_buffers elements */
163 uint32_t *desc_storage; /* this has num_buffers*4 elements */
164 uint32_t **desc_data; /* an array of pointers pointing to desc_storage */
165 };
166
167 #define si_pm4_block_idx(member) \
168 (offsetof(union si_state, named.member) / sizeof(struct si_pm4_state *))
169
170 #define si_pm4_state_changed(sctx, member) \
171 ((sctx)->queued.named.member != (sctx)->emitted.named.member)
172
173 #define si_pm4_bind_state(sctx, member, value) \
174 do { \
175 (sctx)->queued.named.member = (value); \
176 } while(0)
177
178 #define si_pm4_delete_state(sctx, member, value) \
179 do { \
180 if ((sctx)->queued.named.member == (value)) { \
181 (sctx)->queued.named.member = NULL; \
182 } \
183 si_pm4_free_state(sctx, (struct si_pm4_state *)(value), \
184 si_pm4_block_idx(member)); \
185 } while(0)
186
187 #define si_pm4_set_state(sctx, member, value) \
188 do { \
189 if ((sctx)->queued.named.member != (value)) { \
190 si_pm4_free_state(sctx, \
191 (struct si_pm4_state *)(sctx)->queued.named.member, \
192 si_pm4_block_idx(member)); \
193 (sctx)->queued.named.member = (value); \
194 } \
195 } while(0)
196
197 /* si_descriptors.c */
198 void si_set_sampler_view(struct si_context *sctx, unsigned shader,
199 unsigned slot, struct pipe_sampler_view *view,
200 unsigned *view_desc);
201 void si_set_ring_buffer(struct pipe_context *ctx, uint shader, uint slot,
202 struct pipe_constant_buffer *input,
203 unsigned stride, unsigned num_records,
204 bool add_tid, bool swizzle,
205 unsigned element_size, unsigned index_stride);
206 void si_init_all_descriptors(struct si_context *sctx);
207 void si_release_all_descriptors(struct si_context *sctx);
208 void si_all_descriptors_begin_new_cs(struct si_context *sctx);
209 void si_copy_buffer(struct si_context *sctx,
210 struct pipe_resource *dst, struct pipe_resource *src,
211 uint64_t dst_offset, uint64_t src_offset, unsigned size);
212 void si_upload_const_buffer(struct si_context *sctx, struct r600_resource **rbuffer,
213 const uint8_t *ptr, unsigned size, uint32_t *const_offset);
214
215 /* si_state.c */
216 struct si_pipe_shader_selector;
217 struct si_surface;
218
219 boolean si_is_format_supported(struct pipe_screen *screen,
220 enum pipe_format format,
221 enum pipe_texture_target target,
222 unsigned sample_count,
223 unsigned usage);
224 int si_shader_select(struct pipe_context *ctx,
225 struct si_pipe_shader_selector *sel);
226 void si_init_state_functions(struct si_context *sctx);
227 void si_init_config(struct si_context *sctx);
228
229 /* si_state_draw.c */
230 extern const struct r600_atom si_atom_cache_flush;
231 void si_emit_cache_flush(struct r600_common_context *sctx, struct r600_atom *atom);
232 void si_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *dinfo);
233
234 /* si_commands.c */
235 void si_cmd_context_control(struct si_pm4_state *pm4);
236 void si_cmd_draw_index_2(struct si_pm4_state *pm4, uint32_t max_size,
237 uint64_t index_base, uint32_t index_count,
238 uint32_t initiator, bool predicate);
239 void si_cmd_draw_index_auto(struct si_pm4_state *pm4, uint32_t count,
240 uint32_t initiator, bool predicate);
241 void si_cmd_surface_sync(struct si_pm4_state *pm4, uint32_t cp_coher_cntl);
242
243 #endif