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