ilo: update ilo_zs_surface for Gen8
[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[3];
223 uint32_t dw_msaa;
224
225 /* Global Depth Offset Constant/Scale/Clamp */
226 uint32_t dw_depth_offset_const;
227 uint32_t dw_depth_offset_scale;
228 uint32_t dw_depth_offset_clamp;
229
230 /* Gen8+ 3DSTATE_RASTER */
231 uint32_t dw_raster;
232 };
233
234 struct ilo_rasterizer_wm {
235 /* 3DSTATE_WM */
236 uint32_t payload[2];
237 uint32_t dw_msaa_rast;
238 uint32_t dw_msaa_disp;
239 };
240
241 struct ilo_rasterizer_state {
242 struct pipe_rasterizer_state state;
243
244 struct ilo_rasterizer_clip clip;
245 struct ilo_rasterizer_sf sf;
246 struct ilo_rasterizer_wm wm;
247 };
248
249 struct ilo_dsa_state {
250 /* DEPTH_STENCIL_STATE or Gen8+ 3DSTATE_WM_DEPTH_STENCIL */
251 uint32_t payload[3];
252
253 uint32_t dw_blend_alpha;
254 uint32_t dw_ps_blend_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
266 struct ilo_blend_state {
267 struct ilo_blend_cso cso[ILO_MAX_DRAW_BUFFERS];
268
269 bool dual_blend;
270 bool alpha_to_coverage;
271
272 uint32_t dw_shared;
273 uint32_t dw_alpha_mod;
274 uint32_t dw_logicop;
275
276 /* a part of 3DSTATE_PS_BLEND */
277 uint32_t dw_ps_blend;
278 uint32_t dw_ps_blend_dst_alpha_forced_one;
279 };
280
281 struct ilo_sampler_cso {
282 /* SAMPLER_STATE and SAMPLER_BORDER_COLOR_STATE */
283 uint32_t payload[15];
284
285 uint32_t dw_filter;
286 uint32_t dw_filter_aniso;
287 uint32_t dw_wrap;
288 uint32_t dw_wrap_1d;
289 uint32_t dw_wrap_cube;
290
291 bool anisotropic;
292 bool saturate_r;
293 bool saturate_s;
294 bool saturate_t;
295 };
296
297 struct ilo_sampler_state {
298 const struct ilo_sampler_cso *cso[ILO_MAX_SAMPLERS];
299 };
300
301 struct ilo_view_surface {
302 /* SURFACE_STATE */
303 uint32_t payload[13];
304 struct intel_bo *bo;
305 };
306
307 struct ilo_view_cso {
308 struct pipe_sampler_view base;
309
310 struct ilo_view_surface surface;
311 };
312
313 struct ilo_view_state {
314 struct pipe_sampler_view *states[ILO_MAX_SAMPLER_VIEWS];
315 unsigned count;
316 };
317
318 struct ilo_cbuf_cso {
319 struct pipe_resource *resource;
320 struct ilo_view_surface surface;
321
322 /*
323 * this CSO is not so constant because user buffer needs to be uploaded in
324 * finalize_constant_buffers()
325 */
326 const void *user_buffer;
327 unsigned user_buffer_size;
328 };
329
330 struct ilo_cbuf_state {
331 struct ilo_cbuf_cso cso[ILO_MAX_CONST_BUFFERS];
332 uint32_t enabled_mask;
333 };
334
335 struct ilo_resource_state {
336 struct pipe_surface *states[PIPE_MAX_SHADER_RESOURCES];
337 unsigned count;
338 };
339
340 struct ilo_surface_cso {
341 struct pipe_surface base;
342
343 bool is_rt;
344 union {
345 struct ilo_view_surface rt;
346 struct ilo_zs_surface {
347 uint32_t payload[12];
348 uint32_t dw_aligned_8x4;
349
350 struct intel_bo *bo;
351 struct intel_bo *hiz_bo;
352 struct intel_bo *separate_s8_bo;
353 } zs;
354 } u;
355 };
356
357 struct ilo_fb_state {
358 struct pipe_framebuffer_state state;
359
360 struct ilo_view_surface null_rt;
361 struct ilo_zs_surface null_zs;
362
363 struct ilo_fb_blend_caps {
364 bool can_logicop;
365 bool can_blend;
366 bool can_alpha_test;
367 bool dst_alpha_forced_one;
368 } blend_caps[PIPE_MAX_COLOR_BUFS];
369
370 unsigned num_samples;
371 };
372
373 struct ilo_global_binding_cso {
374 struct pipe_resource *resource;
375 uint32_t *handle;
376 };
377
378 /*
379 * In theory, we would like a "virtual" bo that serves as the global memory
380 * region. The virtual bo would reserve a region in the GTT aperture, but the
381 * pages of it would come from those of the global bindings.
382 *
383 * The virtual bo would be created in launch_grid(). The global bindings
384 * would be added to the virtual bo. A SURFACE_STATE for the virtual bo would
385 * be created. The handles returned by set_global_binding() would be offsets
386 * into the virtual bo.
387 *
388 * But for now, we will create a SURFACE_STATE for each of the bindings. The
389 * handle of a global binding consists of the offset and the binding table
390 * index.
391 */
392 struct ilo_global_binding {
393 struct util_dynarray bindings;
394 unsigned count;
395 };
396
397 struct ilo_shader_cso {
398 uint32_t payload[5];
399 };
400
401 struct ilo_state_vector {
402 const struct pipe_draw_info *draw;
403
404 uint32_t dirty;
405
406 struct ilo_vb_state vb;
407 struct ilo_ve_state *ve;
408 struct ilo_ib_state ib;
409
410 struct ilo_shader_state *vs;
411 struct ilo_shader_state *gs;
412
413 struct ilo_so_state so;
414
415 struct pipe_clip_state clip;
416 struct ilo_viewport_state viewport;
417 struct ilo_scissor_state scissor;
418
419 const struct ilo_rasterizer_state *rasterizer;
420 struct pipe_poly_stipple poly_stipple;
421 unsigned sample_mask;
422
423 struct ilo_shader_state *fs;
424
425 const struct ilo_dsa_state *dsa;
426 struct pipe_stencil_ref stencil_ref;
427 const struct ilo_blend_state *blend;
428 struct pipe_blend_color blend_color;
429 struct ilo_fb_state fb;
430
431 /* shader resources */
432 struct ilo_sampler_state sampler[PIPE_SHADER_TYPES];
433 struct ilo_view_state view[PIPE_SHADER_TYPES];
434 struct ilo_cbuf_state cbuf[PIPE_SHADER_TYPES];
435 struct ilo_resource_state resource;
436
437 /* GPGPU */
438 struct ilo_shader_state *cs;
439 struct ilo_resource_state cs_resource;
440 struct ilo_global_binding global_binding;
441 };
442
443 void
444 ilo_init_state_functions(struct ilo_context *ilo);
445
446 void
447 ilo_finalize_3d_states(struct ilo_context *ilo,
448 const struct pipe_draw_info *draw);
449
450 void
451 ilo_finalize_compute_states(struct ilo_context *ilo);
452
453 void
454 ilo_state_vector_init(const struct ilo_dev_info *dev,
455 struct ilo_state_vector *vec);
456
457 void
458 ilo_state_vector_cleanup(struct ilo_state_vector *vec);
459
460 void
461 ilo_state_vector_resource_renamed(struct ilo_state_vector *vec,
462 struct pipe_resource *res);
463
464 void
465 ilo_state_vector_dump_dirty(const struct ilo_state_vector *vec);
466
467 #endif /* ILO_STATE_H */