ilo: add ilo_finalize_compute_states()
[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 "pipe/p_state.h"
32 #include "util/u_dynarray.h"
33
34 #include "ilo_common.h"
35
36 /**
37 * \see brw_context.h
38 */
39 #define ILO_MAX_DRAW_BUFFERS 8
40 #define ILO_MAX_CONST_BUFFERS (1 + 12)
41 #define ILO_MAX_SAMPLER_VIEWS 16
42 #define ILO_MAX_SAMPLERS 16
43 #define ILO_MAX_SO_BINDINGS 64
44 #define ILO_MAX_SO_BUFFERS 4
45 #define ILO_MAX_VIEWPORTS 1
46
47 #define ILO_MAX_SURFACES 256
48
49 /**
50 * States that we track.
51 *
52 * XXX Do we want to count each sampler or vertex buffer as a state? If that
53 * is the case, there are simply not enough bits.
54 *
55 * XXX We want to treat primitive type and depth clear value as states, but
56 * there are not enough bits.
57 */
58 enum ilo_state {
59 ILO_STATE_VB,
60 ILO_STATE_VE,
61 ILO_STATE_IB,
62 ILO_STATE_VS,
63 ILO_STATE_GS,
64 ILO_STATE_SO,
65 ILO_STATE_CLIP,
66 ILO_STATE_VIEWPORT,
67 ILO_STATE_SCISSOR,
68 ILO_STATE_RASTERIZER,
69 ILO_STATE_POLY_STIPPLE,
70 ILO_STATE_SAMPLE_MASK,
71 ILO_STATE_FS,
72 ILO_STATE_DSA,
73 ILO_STATE_STENCIL_REF,
74 ILO_STATE_BLEND,
75 ILO_STATE_BLEND_COLOR,
76 ILO_STATE_FB,
77
78 ILO_STATE_SAMPLER_VS,
79 ILO_STATE_SAMPLER_GS,
80 ILO_STATE_SAMPLER_FS,
81 ILO_STATE_SAMPLER_CS,
82 ILO_STATE_VIEW_VS,
83 ILO_STATE_VIEW_GS,
84 ILO_STATE_VIEW_FS,
85 ILO_STATE_VIEW_CS,
86 ILO_STATE_CBUF,
87 ILO_STATE_RESOURCE,
88
89 ILO_STATE_CS,
90 ILO_STATE_CS_RESOURCE,
91 ILO_STATE_GLOBAL_BINDING,
92
93 ILO_STATE_COUNT,
94 };
95
96 /**
97 * Dirty flags of the states.
98 */
99 enum ilo_dirty_flags {
100 ILO_DIRTY_VB = 1 << ILO_STATE_VB,
101 ILO_DIRTY_VE = 1 << ILO_STATE_VE,
102 ILO_DIRTY_IB = 1 << ILO_STATE_IB,
103 ILO_DIRTY_VS = 1 << ILO_STATE_VS,
104 ILO_DIRTY_GS = 1 << ILO_STATE_GS,
105 ILO_DIRTY_SO = 1 << ILO_STATE_SO,
106 ILO_DIRTY_CLIP = 1 << ILO_STATE_CLIP,
107 ILO_DIRTY_VIEWPORT = 1 << ILO_STATE_VIEWPORT,
108 ILO_DIRTY_SCISSOR = 1 << ILO_STATE_SCISSOR,
109 ILO_DIRTY_RASTERIZER = 1 << ILO_STATE_RASTERIZER,
110 ILO_DIRTY_POLY_STIPPLE = 1 << ILO_STATE_POLY_STIPPLE,
111 ILO_DIRTY_SAMPLE_MASK = 1 << ILO_STATE_SAMPLE_MASK,
112 ILO_DIRTY_FS = 1 << ILO_STATE_FS,
113 ILO_DIRTY_DSA = 1 << ILO_STATE_DSA,
114 ILO_DIRTY_STENCIL_REF = 1 << ILO_STATE_STENCIL_REF,
115 ILO_DIRTY_BLEND = 1 << ILO_STATE_BLEND,
116 ILO_DIRTY_BLEND_COLOR = 1 << ILO_STATE_BLEND_COLOR,
117 ILO_DIRTY_FB = 1 << ILO_STATE_FB,
118 ILO_DIRTY_SAMPLER_VS = 1 << ILO_STATE_SAMPLER_VS,
119 ILO_DIRTY_SAMPLER_GS = 1 << ILO_STATE_SAMPLER_GS,
120 ILO_DIRTY_SAMPLER_FS = 1 << ILO_STATE_SAMPLER_FS,
121 ILO_DIRTY_SAMPLER_CS = 1 << ILO_STATE_SAMPLER_CS,
122 ILO_DIRTY_VIEW_VS = 1 << ILO_STATE_VIEW_VS,
123 ILO_DIRTY_VIEW_GS = 1 << ILO_STATE_VIEW_GS,
124 ILO_DIRTY_VIEW_FS = 1 << ILO_STATE_VIEW_FS,
125 ILO_DIRTY_VIEW_CS = 1 << ILO_STATE_VIEW_CS,
126 ILO_DIRTY_CBUF = 1 << ILO_STATE_CBUF,
127 ILO_DIRTY_RESOURCE = 1 << ILO_STATE_RESOURCE,
128 ILO_DIRTY_CS = 1 << ILO_STATE_CS,
129 ILO_DIRTY_CS_RESOURCE = 1 << ILO_STATE_CS_RESOURCE,
130 ILO_DIRTY_GLOBAL_BINDING = 1 << ILO_STATE_GLOBAL_BINDING,
131 ILO_DIRTY_ALL = 0xffffffff,
132 };
133
134 struct intel_bo;
135 struct ilo_buffer;
136 struct ilo_context;
137 struct ilo_shader_state;
138 struct ilo_texture;
139
140 struct ilo_vb_state {
141 struct pipe_vertex_buffer states[PIPE_MAX_ATTRIBS];
142 uint32_t enabled_mask;
143 };
144
145 struct ilo_ib_state {
146 struct pipe_resource *buffer;
147 const void *user_buffer;
148 unsigned offset;
149 unsigned index_size;
150
151 /* these are not valid until the state is finalized */
152 struct pipe_resource *hw_resource;
153 unsigned hw_index_size;
154 /* an offset to be added to pipe_draw_info::start */
155 int64_t draw_start_offset;
156 };
157
158 struct ilo_ve_cso {
159 /* VERTEX_ELEMENT_STATE */
160 uint32_t payload[2];
161 };
162
163 struct ilo_ve_state {
164 struct ilo_ve_cso cso[PIPE_MAX_ATTRIBS];
165 unsigned count;
166
167 unsigned instance_divisors[PIPE_MAX_ATTRIBS];
168 unsigned vb_mapping[PIPE_MAX_ATTRIBS];
169 unsigned vb_count;
170
171 /* these are not valid until the state is finalized */
172 struct ilo_ve_cso edgeflag_cso;
173 bool last_cso_edgeflag;
174
175 struct ilo_ve_cso nosrc_cso;
176 bool prepend_nosrc_cso;
177 };
178
179 struct ilo_so_state {
180 struct pipe_stream_output_target *states[ILO_MAX_SO_BUFFERS];
181 unsigned count;
182 unsigned append_bitmask;
183
184 bool enabled;
185 };
186
187 struct ilo_viewport_cso {
188 /* matrix form */
189 float m00, m11, m22, m30, m31, m32;
190
191 /* guardband in NDC space */
192 float min_gbx, min_gby, max_gbx, max_gby;
193
194 /* viewport in screen space */
195 float min_x, min_y, min_z;
196 float max_x, max_y, max_z;
197 };
198
199 struct ilo_viewport_state {
200 struct ilo_viewport_cso cso[ILO_MAX_VIEWPORTS];
201 unsigned count;
202
203 struct pipe_viewport_state viewport0;
204 };
205
206 struct ilo_scissor_state {
207 /* SCISSOR_RECT */
208 uint32_t payload[ILO_MAX_VIEWPORTS * 2];
209
210 struct pipe_scissor_state scissor0;
211 };
212
213 struct ilo_rasterizer_clip {
214 /* 3DSTATE_CLIP */
215 uint32_t payload[3];
216
217 uint32_t can_enable_guardband;
218 };
219
220 struct ilo_rasterizer_sf {
221 /* 3DSTATE_SF */
222 uint32_t payload[6];
223 uint32_t dw_msaa;
224 };
225
226 struct ilo_rasterizer_wm {
227 /* 3DSTATE_WM */
228 uint32_t payload[2];
229 uint32_t dw_msaa_rast;
230 uint32_t dw_msaa_disp;
231 };
232
233 struct ilo_rasterizer_state {
234 struct pipe_rasterizer_state state;
235
236 struct ilo_rasterizer_clip clip;
237 struct ilo_rasterizer_sf sf;
238 struct ilo_rasterizer_wm wm;
239 };
240
241 struct ilo_dsa_state {
242 /* DEPTH_STENCIL_STATE */
243 uint32_t payload[3];
244
245 uint32_t dw_alpha;
246 ubyte alpha_ref;
247 };
248
249 struct ilo_blend_cso {
250 /* BLEND_STATE */
251 uint32_t payload[2];
252
253 uint32_t dw_blend;
254 uint32_t dw_blend_dst_alpha_forced_one;
255
256 uint32_t dw_logicop;
257 uint32_t dw_alpha_mod;
258 };
259
260 struct ilo_blend_state {
261 struct ilo_blend_cso cso[ILO_MAX_DRAW_BUFFERS];
262
263 bool independent_blend_enable;
264 bool dual_blend;
265 bool alpha_to_coverage;
266 };
267
268 struct ilo_sampler_cso {
269 /* SAMPLER_STATE and SAMPLER_BORDER_COLOR_STATE */
270 uint32_t payload[15];
271
272 uint32_t dw_filter;
273 uint32_t dw_filter_aniso;
274 uint32_t dw_wrap;
275 uint32_t dw_wrap_1d;
276 uint32_t dw_wrap_cube;
277
278 bool anisotropic;
279 bool saturate_r;
280 bool saturate_s;
281 bool saturate_t;
282 };
283
284 struct ilo_sampler_state {
285 const struct ilo_sampler_cso *cso[ILO_MAX_SAMPLERS];
286 };
287
288 struct ilo_view_surface {
289 /* SURFACE_STATE */
290 uint32_t payload[8];
291 struct intel_bo *bo;
292 };
293
294 struct ilo_view_cso {
295 struct pipe_sampler_view base;
296
297 struct ilo_view_surface surface;
298 };
299
300 struct ilo_view_state {
301 struct pipe_sampler_view *states[ILO_MAX_SAMPLER_VIEWS];
302 unsigned count;
303 };
304
305 struct ilo_cbuf_cso {
306 struct pipe_resource *resource;
307 struct ilo_view_surface surface;
308
309 /*
310 * this CSO is not so constant because user buffer needs to be uploaded in
311 * finalize_constant_buffers()
312 */
313 const void *user_buffer;
314 unsigned user_buffer_size;
315 };
316
317 struct ilo_cbuf_state {
318 struct ilo_cbuf_cso cso[ILO_MAX_CONST_BUFFERS];
319 uint32_t enabled_mask;
320 };
321
322 struct ilo_resource_state {
323 struct pipe_surface *states[PIPE_MAX_SHADER_RESOURCES];
324 unsigned count;
325 };
326
327 struct ilo_surface_cso {
328 struct pipe_surface base;
329
330 bool is_rt;
331 union {
332 struct ilo_view_surface rt;
333 struct ilo_zs_surface {
334 uint32_t payload[10];
335 uint32_t dw_aligned_8x4;
336
337 struct intel_bo *bo;
338 struct intel_bo *hiz_bo;
339 struct intel_bo *separate_s8_bo;
340 } zs;
341 } u;
342 };
343
344 struct ilo_fb_state {
345 struct pipe_framebuffer_state state;
346
347 struct ilo_view_surface null_rt;
348 struct ilo_zs_surface null_zs;
349
350 unsigned num_samples;
351 };
352
353 struct ilo_global_binding_cso {
354 struct pipe_resource *resource;
355 uint32_t *handle;
356 };
357
358 /*
359 * In theory, we would like a "virtual" bo that serves as the global memory
360 * region. The virtual bo would reserve a region in the GTT aperture, but the
361 * pages of it would come from those of the global bindings.
362 *
363 * The virtual bo would be created in launch_grid(). The global bindings
364 * would be added to the virtual bo. A SURFACE_STATE for the virtual bo would
365 * be created. The handles returned by set_global_binding() would be offsets
366 * into the virtual bo.
367 *
368 * But for now, we will create a SURFACE_STATE for each of the bindings. The
369 * handle of a global binding consists of the offset and the binding table
370 * index.
371 */
372 struct ilo_global_binding {
373 struct util_dynarray bindings;
374 unsigned count;
375 };
376
377 struct ilo_shader_cso {
378 uint32_t payload[5];
379 };
380
381 struct ilo_state_vector {
382 const struct pipe_draw_info *draw;
383
384 uint32_t dirty;
385
386 struct ilo_vb_state vb;
387 struct ilo_ve_state *ve;
388 struct ilo_ib_state ib;
389
390 struct ilo_shader_state *vs;
391 struct ilo_shader_state *gs;
392
393 struct ilo_so_state so;
394
395 struct pipe_clip_state clip;
396 struct ilo_viewport_state viewport;
397 struct ilo_scissor_state scissor;
398
399 const struct ilo_rasterizer_state *rasterizer;
400 struct pipe_poly_stipple poly_stipple;
401 unsigned sample_mask;
402
403 struct ilo_shader_state *fs;
404
405 const struct ilo_dsa_state *dsa;
406 struct pipe_stencil_ref stencil_ref;
407 const struct ilo_blend_state *blend;
408 struct pipe_blend_color blend_color;
409 struct ilo_fb_state fb;
410
411 /* shader resources */
412 struct ilo_sampler_state sampler[PIPE_SHADER_TYPES];
413 struct ilo_view_state view[PIPE_SHADER_TYPES];
414 struct ilo_cbuf_state cbuf[PIPE_SHADER_TYPES];
415 struct ilo_resource_state resource;
416
417 /* GPGPU */
418 struct ilo_shader_state *cs;
419 struct ilo_resource_state cs_resource;
420 struct ilo_global_binding global_binding;
421 };
422
423 void
424 ilo_init_state_functions(struct ilo_context *ilo);
425
426 void
427 ilo_finalize_3d_states(struct ilo_context *ilo,
428 const struct pipe_draw_info *draw);
429
430 void
431 ilo_finalize_compute_states(struct ilo_context *ilo);
432
433 void
434 ilo_state_vector_init(const struct ilo_dev_info *dev,
435 struct ilo_state_vector *vec);
436
437 void
438 ilo_state_vector_cleanup(struct ilo_state_vector *vec);
439
440 void
441 ilo_state_vector_resource_renamed(struct ilo_state_vector *vec,
442 struct pipe_resource *res);
443
444 void
445 ilo_state_vector_dump_dirty(const struct ilo_state_vector *vec);
446
447 #endif /* ILO_STATE_H */