90514d522242e975bcc9792a591a668835862ff1
[mesa.git] / src / gallium / drivers / ilo / ilo_state.h
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2012-2013 LunarG, Inc.
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the 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 NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28 #ifndef ILO_STATE_H
29 #define ILO_STATE_H
30
31 #include "core/ilo_state_cc.h"
32 #include "core/ilo_state_raster.h"
33 #include "core/ilo_state_sampler.h"
34 #include "core/ilo_state_sbe.h"
35 #include "core/ilo_state_shader.h"
36 #include "core/ilo_state_sol.h"
37 #include "core/ilo_state_surface.h"
38 #include "core/ilo_state_urb.h"
39 #include "core/ilo_state_vf.h"
40 #include "core/ilo_state_viewport.h"
41 #include "core/ilo_state_zs.h"
42 #include "pipe/p_state.h"
43 #include "util/u_dynarray.h"
44
45 #include "ilo_common.h"
46
47 /**
48 * \see brw_context.h
49 */
50 #define ILO_MAX_DRAW_BUFFERS 8
51 #define ILO_MAX_CONST_BUFFERS (1 + 12)
52 #define ILO_MAX_SAMPLER_VIEWS 16
53 #define ILO_MAX_SAMPLERS 16
54 #define ILO_MAX_SO_BINDINGS 64
55 #define ILO_MAX_SO_BUFFERS 4
56 #define ILO_MAX_VIEWPORTS 1
57
58 #define ILO_MAX_SURFACES 256
59
60 /**
61 * States that we track.
62 *
63 * XXX Do we want to count each sampler or vertex buffer as a state? If that
64 * is the case, there are simply not enough bits.
65 *
66 * XXX We want to treat primitive type and depth clear value as states, but
67 * there are not enough bits.
68 */
69 enum ilo_state {
70 ILO_STATE_VB,
71 ILO_STATE_VE,
72 ILO_STATE_IB,
73 ILO_STATE_VS,
74 ILO_STATE_GS,
75 ILO_STATE_SO,
76 ILO_STATE_CLIP,
77 ILO_STATE_VIEWPORT,
78 ILO_STATE_SCISSOR,
79 ILO_STATE_RASTERIZER,
80 ILO_STATE_POLY_STIPPLE,
81 ILO_STATE_SAMPLE_MASK,
82 ILO_STATE_FS,
83 ILO_STATE_DSA,
84 ILO_STATE_STENCIL_REF,
85 ILO_STATE_BLEND,
86 ILO_STATE_BLEND_COLOR,
87 ILO_STATE_FB,
88
89 ILO_STATE_SAMPLER_VS,
90 ILO_STATE_SAMPLER_GS,
91 ILO_STATE_SAMPLER_FS,
92 ILO_STATE_SAMPLER_CS,
93 ILO_STATE_VIEW_VS,
94 ILO_STATE_VIEW_GS,
95 ILO_STATE_VIEW_FS,
96 ILO_STATE_VIEW_CS,
97 ILO_STATE_CBUF,
98 ILO_STATE_RESOURCE,
99
100 ILO_STATE_CS,
101 ILO_STATE_CS_RESOURCE,
102 ILO_STATE_GLOBAL_BINDING,
103
104 ILO_STATE_COUNT,
105 };
106
107 /**
108 * Dirty flags of the states.
109 */
110 enum ilo_dirty_flags {
111 ILO_DIRTY_VB = 1 << ILO_STATE_VB,
112 ILO_DIRTY_VE = 1 << ILO_STATE_VE,
113 ILO_DIRTY_IB = 1 << ILO_STATE_IB,
114 ILO_DIRTY_VS = 1 << ILO_STATE_VS,
115 ILO_DIRTY_GS = 1 << ILO_STATE_GS,
116 ILO_DIRTY_SO = 1 << ILO_STATE_SO,
117 ILO_DIRTY_CLIP = 1 << ILO_STATE_CLIP,
118 ILO_DIRTY_VIEWPORT = 1 << ILO_STATE_VIEWPORT,
119 ILO_DIRTY_SCISSOR = 1 << ILO_STATE_SCISSOR,
120 ILO_DIRTY_RASTERIZER = 1 << ILO_STATE_RASTERIZER,
121 ILO_DIRTY_POLY_STIPPLE = 1 << ILO_STATE_POLY_STIPPLE,
122 ILO_DIRTY_SAMPLE_MASK = 1 << ILO_STATE_SAMPLE_MASK,
123 ILO_DIRTY_FS = 1 << ILO_STATE_FS,
124 ILO_DIRTY_DSA = 1 << ILO_STATE_DSA,
125 ILO_DIRTY_STENCIL_REF = 1 << ILO_STATE_STENCIL_REF,
126 ILO_DIRTY_BLEND = 1 << ILO_STATE_BLEND,
127 ILO_DIRTY_BLEND_COLOR = 1 << ILO_STATE_BLEND_COLOR,
128 ILO_DIRTY_FB = 1 << ILO_STATE_FB,
129 ILO_DIRTY_SAMPLER_VS = 1 << ILO_STATE_SAMPLER_VS,
130 ILO_DIRTY_SAMPLER_GS = 1 << ILO_STATE_SAMPLER_GS,
131 ILO_DIRTY_SAMPLER_FS = 1 << ILO_STATE_SAMPLER_FS,
132 ILO_DIRTY_SAMPLER_CS = 1 << ILO_STATE_SAMPLER_CS,
133 ILO_DIRTY_VIEW_VS = 1 << ILO_STATE_VIEW_VS,
134 ILO_DIRTY_VIEW_GS = 1 << ILO_STATE_VIEW_GS,
135 ILO_DIRTY_VIEW_FS = 1 << ILO_STATE_VIEW_FS,
136 ILO_DIRTY_VIEW_CS = 1 << ILO_STATE_VIEW_CS,
137 ILO_DIRTY_CBUF = 1 << ILO_STATE_CBUF,
138 ILO_DIRTY_RESOURCE = 1 << ILO_STATE_RESOURCE,
139 ILO_DIRTY_CS = 1 << ILO_STATE_CS,
140 ILO_DIRTY_CS_RESOURCE = 1 << ILO_STATE_CS_RESOURCE,
141 ILO_DIRTY_GLOBAL_BINDING = 1 << ILO_STATE_GLOBAL_BINDING,
142 ILO_DIRTY_ALL = 0xffffffff,
143 };
144
145 struct ilo_context;
146 struct ilo_shader_state;
147
148 struct ilo_ve_state {
149 unsigned vb_mapping[PIPE_MAX_ATTRIBS];
150 unsigned instance_divisors[PIPE_MAX_ATTRIBS];
151 unsigned vb_count;
152
153 /* these are not valid until the state is finalized */
154 uint32_t vf_data[PIPE_MAX_ATTRIBS][2];
155 struct ilo_state_vf_params_info vf_params;
156 struct ilo_state_vf vf;
157 };
158
159 struct ilo_vb_state {
160 struct pipe_vertex_buffer states[PIPE_MAX_ATTRIBS];
161 uint32_t enabled_mask;
162 };
163
164 struct ilo_ib_state {
165 struct pipe_resource *buffer;
166 const void *user_buffer;
167 unsigned offset;
168 unsigned index_size;
169
170 /* these are not valid until the state is finalized */
171 struct pipe_resource *hw_resource;
172 unsigned hw_index_size;
173 /* an offset to be added to pipe_draw_info::start */
174 int64_t draw_start_offset;
175 };
176
177 struct ilo_cbuf_cso {
178 struct pipe_resource *resource;
179 struct ilo_state_surface_buffer_info info;
180 struct ilo_state_surface surface;
181
182 /*
183 * this CSO is not so constant because user buffer needs to be uploaded in
184 * finalize_constant_buffers()
185 */
186 const void *user_buffer;
187 };
188
189 struct ilo_sampler_cso {
190 struct ilo_state_sampler sampler;
191 struct ilo_state_sampler_border border;
192 bool saturate_s;
193 bool saturate_t;
194 bool saturate_r;
195 };
196
197 struct ilo_sampler_state {
198 const struct ilo_sampler_cso *cso[ILO_MAX_SAMPLERS];
199 };
200
201 struct ilo_cbuf_state {
202 struct ilo_cbuf_cso cso[ILO_MAX_CONST_BUFFERS];
203 uint32_t enabled_mask;
204 };
205
206 struct ilo_resource_state {
207 struct pipe_surface *states[PIPE_MAX_SHADER_RESOURCES];
208 unsigned count;
209 };
210
211 struct ilo_view_cso {
212 struct pipe_sampler_view base;
213
214 struct ilo_state_surface surface;
215 };
216
217 struct ilo_view_state {
218 struct pipe_sampler_view *states[ILO_MAX_SAMPLER_VIEWS];
219 unsigned count;
220 };
221
222 struct ilo_so_state {
223 struct pipe_stream_output_target *states[ILO_MAX_SO_BUFFERS];
224 unsigned count;
225 unsigned append_bitmask;
226
227 bool enabled;
228 };
229
230 struct ilo_rasterizer_state {
231 struct pipe_rasterizer_state state;
232
233 /* these are invalid until finalize_rasterizer() */
234 struct ilo_state_raster_info info;
235 struct ilo_state_raster rs;
236 };
237
238 struct ilo_viewport_state {
239 struct ilo_state_viewport_matrix_info matrices[ILO_MAX_VIEWPORTS];
240 struct ilo_state_viewport_scissor_info scissors[ILO_MAX_VIEWPORTS];
241 struct ilo_state_viewport_params_info params;
242
243 struct pipe_viewport_state viewport0;
244 struct pipe_scissor_state scissor0;
245
246 struct ilo_state_viewport vp;
247 uint32_t vp_data[20 * ILO_MAX_VIEWPORTS];
248 };
249
250 struct ilo_surface_cso {
251 struct pipe_surface base;
252
253 bool is_rt;
254 union {
255 struct ilo_state_surface rt;
256 struct ilo_state_zs zs;
257 } u;
258 };
259
260 struct ilo_fb_state {
261 struct pipe_framebuffer_state state;
262
263 struct ilo_state_surface null_rt;
264 struct ilo_state_zs null_zs;
265
266 struct ilo_fb_blend_caps {
267 bool is_unorm;
268 bool is_integer;
269 bool force_dst_alpha_one;
270
271 bool can_logicop;
272 bool can_blend;
273 bool can_alpha_test;
274 } blend_caps[PIPE_MAX_COLOR_BUFS];
275
276 unsigned num_samples;
277
278 bool has_integer_rt;
279 bool has_hiz;
280 enum gen_depth_format depth_offset_format;
281 };
282
283 struct ilo_dsa_state {
284 struct ilo_state_cc_depth_info depth;
285
286 struct ilo_state_cc_stencil_info stencil;
287 struct {
288 uint8_t test_mask;
289 uint8_t write_mask;
290 } stencil_front, stencil_back;
291
292 bool alpha_test;
293 float alpha_ref;
294 enum gen_compare_function alpha_func;
295 };
296
297 struct ilo_blend_state {
298 struct ilo_state_cc_blend_rt_info rt[PIPE_MAX_COLOR_BUFS];
299 struct ilo_state_cc_blend_rt_info dummy_rt;
300 bool dual_blend;
301
302 /* these are invalid until finalize_blend() */
303 struct ilo_state_cc_blend_rt_info effective_rt[PIPE_MAX_COLOR_BUFS];
304 struct ilo_state_cc_info info;
305 struct ilo_state_cc cc;
306 bool alpha_may_kill;
307 };
308
309 struct ilo_global_binding_cso {
310 struct pipe_resource *resource;
311 uint32_t *handle;
312 };
313
314 /*
315 * In theory, we would like a "virtual" bo that serves as the global memory
316 * region. The virtual bo would reserve a region in the GTT aperture, but the
317 * pages of it would come from those of the global bindings.
318 *
319 * The virtual bo would be created in launch_grid(). The global bindings
320 * would be added to the virtual bo. A SURFACE_STATE for the virtual bo would
321 * be created. The handles returned by set_global_binding() would be offsets
322 * into the virtual bo.
323 *
324 * But for now, we will create a SURFACE_STATE for each of the bindings. The
325 * handle of a global binding consists of the offset and the binding table
326 * index.
327 */
328 struct ilo_global_binding {
329 struct util_dynarray bindings;
330 unsigned count;
331 };
332
333 struct ilo_state_vector {
334 const struct pipe_draw_info *draw;
335
336 uint32_t dirty;
337
338 struct ilo_vb_state vb;
339 struct ilo_ve_state *ve;
340 struct ilo_ib_state ib;
341
342 struct ilo_shader_state *vs;
343 struct ilo_shader_state *gs;
344
345 struct ilo_state_hs disabled_hs;
346 struct ilo_state_ds disabled_ds;
347 struct ilo_state_gs disabled_gs;
348
349 struct ilo_so_state so;
350
351 struct pipe_clip_state clip;
352
353 struct ilo_viewport_state viewport;
354
355 struct ilo_rasterizer_state *rasterizer;
356
357 struct pipe_poly_stipple poly_stipple;
358 unsigned sample_mask;
359
360 struct ilo_shader_state *fs;
361
362 struct ilo_state_cc_params_info cc_params;
363 struct pipe_stencil_ref stencil_ref;
364 const struct ilo_dsa_state *dsa;
365 struct ilo_blend_state *blend;
366
367 struct ilo_fb_state fb;
368
369 struct ilo_state_urb urb;
370
371 /* shader resources */
372 struct ilo_sampler_state sampler[PIPE_SHADER_TYPES];
373 struct ilo_view_state view[PIPE_SHADER_TYPES];
374 struct ilo_cbuf_state cbuf[PIPE_SHADER_TYPES];
375 struct ilo_resource_state resource;
376
377 struct ilo_state_sampler disabled_sampler;
378
379 /* GPGPU */
380 struct ilo_shader_state *cs;
381 struct ilo_resource_state cs_resource;
382 struct ilo_global_binding global_binding;
383 };
384
385 void
386 ilo_init_state_functions(struct ilo_context *ilo);
387
388 void
389 ilo_finalize_3d_states(struct ilo_context *ilo,
390 const struct pipe_draw_info *draw);
391
392 void
393 ilo_finalize_compute_states(struct ilo_context *ilo);
394
395 void
396 ilo_state_vector_init(const struct ilo_dev *dev,
397 struct ilo_state_vector *vec);
398
399 void
400 ilo_state_vector_cleanup(struct ilo_state_vector *vec);
401
402 void
403 ilo_state_vector_resource_renamed(struct ilo_state_vector *vec,
404 struct pipe_resource *res);
405
406 void
407 ilo_state_vector_dump_dirty(const struct ilo_state_vector *vec);
408
409 #endif /* ILO_STATE_H */