ilo: add ilo_state_ps to ilo_shader_cso
[mesa.git] / src / gallium / drivers / ilo / ilo_blitter_rectlist.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2014 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 #include "core/ilo_state_3d.h"
29 #include "util/u_draw.h"
30 #include "util/u_pack_color.h"
31
32 #include "ilo_draw.h"
33 #include "ilo_state.h"
34 #include "ilo_blit.h"
35 #include "ilo_blitter.h"
36
37 /**
38 * Set the states that are invariant between all ops.
39 */
40 static bool
41 ilo_blitter_set_invariants(struct ilo_blitter *blitter)
42 {
43 struct ilo_state_vf_element_info elem;
44
45 if (blitter->initialized)
46 return true;
47
48 /* a rectangle has 3 vertices in a RECTLIST */
49 util_draw_init_info(&blitter->draw);
50 blitter->draw.mode = ILO_PRIM_RECTANGLES;
51 blitter->draw.count = 3;
52
53 memset(&elem, 0, sizeof(elem));
54 /* only vertex X and Y */
55 elem.format = GEN6_FORMAT_R32G32_FLOAT;
56 elem.format_size = 8;
57 elem.component_count = 2;
58
59 ilo_state_vf_init_for_rectlist(&blitter->vf, blitter->ilo->dev,
60 blitter->vf_data, sizeof(blitter->vf_data), &elem, 1);
61
62 ilo_state_vs_init_disabled(&blitter->vs, blitter->ilo->dev);
63 ilo_state_hs_init_disabled(&blitter->hs, blitter->ilo->dev);
64 ilo_state_ds_init_disabled(&blitter->ds, blitter->ilo->dev);
65 ilo_state_gs_init_disabled(&blitter->gs, blitter->ilo->dev);
66 ilo_state_sol_init_disabled(&blitter->sol, blitter->ilo->dev, false);
67
68 /**
69 * From the Haswell PRM, volume 7, page 615:
70 *
71 * "The clear value must be between the min and max depth values
72 * (inclusive) defined in the CC_VIEWPORT."
73 *
74 * Even though clipping and viewport transformation will be disabled, we
75 * still need to set up the viewport states.
76 */
77 ilo_state_viewport_init_for_rectlist(&blitter->vp, blitter->ilo->dev,
78 blitter->vp_data, sizeof(blitter->vp_data));
79
80 ilo_state_sbe_init_for_rectlist(&blitter->sbe, blitter->ilo->dev, 0, 0);
81 ilo_state_ps_init_disabled(&blitter->ps, blitter->ilo->dev);
82
83 ilo_state_urb_init_for_rectlist(&blitter->urb, blitter->ilo->dev,
84 ilo_state_vf_get_attr_count(&blitter->vf));
85
86 blitter->initialized = true;
87
88 return true;
89 }
90
91 static void
92 ilo_blitter_set_earlyz_op(struct ilo_blitter *blitter,
93 enum ilo_state_raster_earlyz_op op,
94 bool earlyz_stencil_clear)
95 {
96 blitter->earlyz_op = op;
97 blitter->earlyz_stencil_clear = earlyz_stencil_clear;
98 }
99
100 /**
101 * Set the rectangle primitive.
102 */
103 static void
104 ilo_blitter_set_rectlist(struct ilo_blitter *blitter,
105 unsigned x, unsigned y,
106 unsigned width, unsigned height)
107 {
108 /*
109 * From the Sandy Bridge PRM, volume 2 part 1, page 11:
110 *
111 * "(RECTLIST) A list of independent rectangles, where only 3 vertices
112 * are provided per rectangle object, with the fourth vertex implied
113 * by the definition of a rectangle. V0=LowerRight, V1=LowerLeft,
114 * V2=UpperLeft. Implied V3 = V0- V1+V2."
115 */
116 blitter->vertices[0][0] = (float) (x + width);
117 blitter->vertices[0][1] = (float) (y + height);
118 blitter->vertices[1][0] = (float) x;
119 blitter->vertices[1][1] = (float) (y + height);
120 blitter->vertices[2][0] = (float) x;
121 blitter->vertices[2][1] = (float) y;
122 }
123
124 static void
125 ilo_blitter_set_depth_clear_value(struct ilo_blitter *blitter,
126 uint32_t depth)
127 {
128 blitter->depth_clear_value = depth;
129 }
130
131 static void
132 ilo_blitter_set_cc(struct ilo_blitter *blitter,
133 const struct ilo_state_cc_info *info)
134 {
135 memset(&blitter->cc, 0, sizeof(blitter->cc));
136 ilo_state_cc_init(&blitter->cc, blitter->ilo->dev, info);
137 }
138
139 static void
140 ilo_blitter_set_fb_rs(struct ilo_blitter *blitter)
141 {
142 memset(&blitter->fb.rs, 0, sizeof(blitter->fb.rs));
143 ilo_state_raster_init_for_rectlist(&blitter->fb.rs, blitter->ilo->dev,
144 blitter->fb.num_samples, blitter->earlyz_op,
145 blitter->earlyz_stencil_clear);
146 }
147
148 static void
149 ilo_blitter_set_fb(struct ilo_blitter *blitter,
150 struct pipe_resource *res, unsigned level,
151 const struct ilo_surface_cso *cso)
152 {
153 struct ilo_texture *tex = ilo_texture(res);
154
155 blitter->fb.width = u_minify(tex->image.width0, level);
156 blitter->fb.height = u_minify(tex->image.height0, level);
157
158 blitter->fb.num_samples = res->nr_samples;
159 if (!blitter->fb.num_samples)
160 blitter->fb.num_samples = 1;
161
162 memcpy(&blitter->fb.dst, cso, sizeof(*cso));
163
164 ilo_blitter_set_fb_rs(blitter);
165 }
166
167 static void
168 ilo_blitter_set_fb_from_surface(struct ilo_blitter *blitter,
169 struct pipe_surface *surf)
170 {
171 ilo_blitter_set_fb(blitter, surf->texture, surf->u.tex.level,
172 (const struct ilo_surface_cso *) surf);
173 }
174
175 static void
176 ilo_blitter_set_fb_from_resource(struct ilo_blitter *blitter,
177 struct pipe_resource *res,
178 enum pipe_format format,
179 unsigned level, unsigned slice)
180 {
181 struct pipe_surface templ, *surf;
182
183 memset(&templ, 0, sizeof(templ));
184 templ.format = format;
185 templ.u.tex.level = level;
186 templ.u.tex.first_layer = slice;
187 templ.u.tex.last_layer = slice;
188
189 /* if we did not call create_surface(), it would never fail */
190 surf = blitter->ilo->base.create_surface(&blitter->ilo->base, res, &templ);
191 assert(surf);
192
193 ilo_blitter_set_fb(blitter, res, level,
194 (const struct ilo_surface_cso *) surf);
195
196 pipe_surface_reference(&surf, NULL);
197 }
198
199 static void
200 ilo_blitter_set_uses(struct ilo_blitter *blitter, uint32_t uses)
201 {
202 blitter->uses = uses;
203 }
204
205 static void
206 hiz_align_fb(struct ilo_blitter *blitter)
207 {
208 unsigned align_w, align_h;
209
210 switch (blitter->earlyz_op) {
211 case ILO_STATE_RASTER_EARLYZ_DEPTH_CLEAR:
212 case ILO_STATE_RASTER_EARLYZ_DEPTH_RESOLVE:
213 break;
214 default:
215 return;
216 break;
217 }
218
219 /*
220 * From the Sandy Bridge PRM, volume 2 part 1, page 313-314:
221 *
222 * "A rectangle primitive representing the clear area is delivered. The
223 * primitive must adhere to the following restrictions on size:
224 *
225 * - If Number of Multisamples is NUMSAMPLES_1, the rectangle must be
226 * aligned to an 8x4 pixel block relative to the upper left corner
227 * of the depth buffer, and contain an integer number of these pixel
228 * blocks, and all 8x4 pixels must be lit.
229 *
230 * - If Number of Multisamples is NUMSAMPLES_4, the rectangle must be
231 * aligned to a 4x2 pixel block (8x4 sample block) relative to the
232 * upper left corner of the depth buffer, and contain an integer
233 * number of these pixel blocks, and all samples of the 4x2 pixels
234 * must be lit
235 *
236 * - If Number of Multisamples is NUMSAMPLES_8, the rectangle must be
237 * aligned to a 2x2 pixel block (8x4 sample block) relative to the
238 * upper left corner of the depth buffer, and contain an integer
239 * number of these pixel blocks, and all samples of the 2x2 pixels
240 * must be list."
241 *
242 * "The following is required when performing a depth buffer resolve:
243 *
244 * - A rectangle primitive of the same size as the previous depth
245 * buffer clear operation must be delivered, and depth buffer state
246 * cannot have changed since the previous depth buffer clear
247 * operation."
248 */
249 switch (blitter->fb.num_samples) {
250 case 1:
251 align_w = 8;
252 align_h = 4;
253 break;
254 case 2:
255 align_w = 4;
256 align_h = 4;
257 break;
258 case 4:
259 align_w = 4;
260 align_h = 2;
261 break;
262 case 8:
263 default:
264 align_w = 2;
265 align_h = 2;
266 break;
267 }
268
269 if (blitter->fb.width % align_w || blitter->fb.height % align_h) {
270 blitter->fb.width = align(blitter->fb.width, align_w);
271 blitter->fb.height = align(blitter->fb.height, align_h);
272 }
273 }
274
275 static void
276 hiz_emit_rectlist(struct ilo_blitter *blitter)
277 {
278 hiz_align_fb(blitter);
279
280 ilo_blitter_set_rectlist(blitter, 0, 0,
281 blitter->fb.width, blitter->fb.height);
282
283 ilo_draw_rectlist(blitter->ilo);
284 }
285
286 static bool
287 hiz_can_clear_zs(const struct ilo_blitter *blitter,
288 const struct ilo_texture *tex)
289 {
290 /*
291 * From the Sandy Bridge PRM, volume 2 part 1, page 314:
292 *
293 * "Several cases exist where Depth Buffer Clear cannot be enabled (the
294 * legacy method of clearing must be performed):
295 *
296 * - If the depth buffer format is D32_FLOAT_S8X24_UINT or
297 * D24_UNORM_S8_UINT.
298 *
299 * - If stencil test is enabled but the separate stencil buffer is
300 * disabled.
301 *
302 * - [DevSNB-A{W/A}]: ...
303 *
304 * - [DevSNB{W/A}]: When depth buffer format is D16_UNORM and the
305 * width of the map (LOD0) is not multiple of 16, fast clear
306 * optimization must be disabled."
307 *
308 * From the Ivy Bridge PRM, volume 2 part 1, page 313:
309 *
310 * "Several cases exist where Depth Buffer Clear cannot be enabled (the
311 * legacy method of clearing must be performed):
312 *
313 * - If the depth buffer format is D32_FLOAT_S8X24_UINT or
314 * D24_UNORM_S8_UINT.
315 *
316 * - If stencil test is enabled but the separate stencil buffer is
317 * disabled."
318 *
319 * The truth is when HiZ is enabled, separate stencil is also enabled on
320 * all GENs. The depth buffer format cannot be combined depth/stencil.
321 */
322 switch (tex->image.format) {
323 case PIPE_FORMAT_Z16_UNORM:
324 if (ilo_dev_gen(blitter->ilo->dev) == ILO_GEN(6) &&
325 tex->base.width0 % 16)
326 return false;
327 break;
328 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
329 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
330 assert(!"HiZ with combined depth/stencil");
331 return false;
332 break;
333 default:
334 break;
335 }
336
337 return true;
338 }
339
340 bool
341 ilo_blitter_rectlist_clear_zs(struct ilo_blitter *blitter,
342 struct pipe_surface *zs,
343 unsigned clear_flags,
344 double depth, unsigned stencil)
345 {
346 struct ilo_texture *tex = ilo_texture(zs->texture);
347 struct ilo_state_cc_info info;
348 uint32_t uses, clear_value;
349
350 if (!ilo_image_can_enable_aux(&tex->image, zs->u.tex.level))
351 return false;
352
353 if (!hiz_can_clear_zs(blitter, tex))
354 return false;
355
356 if (ilo_dev_gen(blitter->ilo->dev) >= ILO_GEN(8))
357 clear_value = fui(depth);
358 else
359 clear_value = util_pack_z(tex->image.format, depth);
360
361 ilo_blit_resolve_surface(blitter->ilo, zs,
362 ILO_TEXTURE_RENDER_WRITE | ILO_TEXTURE_CLEAR);
363 ilo_texture_set_slice_clear_value(tex, zs->u.tex.level,
364 zs->u.tex.first_layer,
365 zs->u.tex.last_layer - zs->u.tex.first_layer + 1,
366 clear_value);
367
368 /*
369 * From the Sandy Bridge PRM, volume 2 part 1, page 313-314:
370 *
371 * "- Depth Test Enable must be disabled and Depth Buffer Write Enable
372 * must be enabled (if depth is being cleared).
373 *
374 * - Stencil buffer clear can be performed at the same time by
375 * enabling Stencil Buffer Write Enable. Stencil Test Enable must
376 * be enabled and Stencil Pass Depth Pass Op set to REPLACE, and the
377 * clear value that is placed in the stencil buffer is the Stencil
378 * Reference Value from COLOR_CALC_STATE.
379 *
380 * - Note also that stencil buffer clear can be performed without
381 * depth buffer clear. For stencil only clear, Depth Test Enable and
382 * Depth Buffer Write Enable must be disabled.
383 *
384 * - [DevSNB] errata: For stencil buffer only clear, the previous
385 * depth clear value must be delivered during the clear."
386 */
387 memset(&info, 0, sizeof(info));
388
389 if (clear_flags & PIPE_CLEAR_DEPTH) {
390 info.depth.cv_has_buffer = true;
391 info.depth.write_enable = true;
392 }
393
394 if (clear_flags & PIPE_CLEAR_STENCIL) {
395 info.stencil.cv_has_buffer = true;
396 info.stencil.test_enable = true;
397 info.stencil.front.test_func = GEN6_COMPAREFUNCTION_ALWAYS;
398 info.stencil.front.fail_op = GEN6_STENCILOP_KEEP;
399 info.stencil.front.zfail_op = GEN6_STENCILOP_KEEP;
400 info.stencil.front.zpass_op = GEN6_STENCILOP_REPLACE;
401
402 /*
403 * From the Ivy Bridge PRM, volume 2 part 1, page 277:
404 *
405 * "Additionally the following must be set to the correct values.
406 *
407 * - DEPTH_STENCIL_STATE::Stencil Write Mask must be 0xFF
408 * - DEPTH_STENCIL_STATE::Stencil Test Mask must be 0xFF
409 * - DEPTH_STENCIL_STATE::Back Face Stencil Write Mask must be 0xFF
410 * - DEPTH_STENCIL_STATE::Back Face Stencil Test Mask must be 0xFF"
411 *
412 * Back frace masks will be copied from front face masks.
413 */
414 info.params.stencil_front.test_ref = (uint8_t) stencil;
415 info.params.stencil_front.test_mask = 0xff;
416 info.params.stencil_front.write_mask = 0xff;
417 }
418
419 ilo_blitter_set_invariants(blitter);
420 ilo_blitter_set_earlyz_op(blitter,
421 ILO_STATE_RASTER_EARLYZ_DEPTH_CLEAR,
422 clear_flags & PIPE_CLEAR_STENCIL);
423
424 ilo_blitter_set_cc(blitter, &info);
425 ilo_blitter_set_depth_clear_value(blitter, clear_value);
426 ilo_blitter_set_fb_from_surface(blitter, zs);
427
428 uses = ILO_BLITTER_USE_DSA;
429 if (clear_flags & PIPE_CLEAR_DEPTH)
430 uses |= ILO_BLITTER_USE_VIEWPORT | ILO_BLITTER_USE_FB_DEPTH;
431 if (clear_flags & PIPE_CLEAR_STENCIL)
432 uses |= ILO_BLITTER_USE_CC | ILO_BLITTER_USE_FB_STENCIL;
433 ilo_blitter_set_uses(blitter, uses);
434
435 hiz_emit_rectlist(blitter);
436
437 return true;
438 }
439
440 void
441 ilo_blitter_rectlist_resolve_z(struct ilo_blitter *blitter,
442 struct pipe_resource *res,
443 unsigned level, unsigned slice)
444 {
445 struct ilo_texture *tex = ilo_texture(res);
446 struct ilo_state_cc_info info;
447 const struct ilo_texture_slice *s =
448 ilo_texture_get_slice(tex, level, slice);
449
450 if (!ilo_image_can_enable_aux(&tex->image, level))
451 return;
452
453 /*
454 * From the Sandy Bridge PRM, volume 2 part 1, page 314:
455 *
456 * "Depth Test Enable must be enabled with the Depth Test Function set
457 * to NEVER. Depth Buffer Write Enable must be enabled. Stencil Test
458 * Enable and Stencil Buffer Write Enable must be disabled."
459 */
460 memset(&info, 0, sizeof(info));
461 info.depth.cv_has_buffer = true;
462 info.depth.test_enable = true;
463 info.depth.write_enable = true;
464 info.depth.test_func = GEN6_COMPAREFUNCTION_NEVER;
465
466 ilo_blitter_set_invariants(blitter);
467 ilo_blitter_set_earlyz_op(blitter,
468 ILO_STATE_RASTER_EARLYZ_DEPTH_RESOLVE, false);
469
470 ilo_blitter_set_cc(blitter, &info);
471 ilo_blitter_set_depth_clear_value(blitter, s->clear_value);
472 ilo_blitter_set_fb_from_resource(blitter, res, res->format, level, slice);
473 ilo_blitter_set_uses(blitter,
474 ILO_BLITTER_USE_DSA | ILO_BLITTER_USE_FB_DEPTH);
475
476 hiz_emit_rectlist(blitter);
477 }
478
479 void
480 ilo_blitter_rectlist_resolve_hiz(struct ilo_blitter *blitter,
481 struct pipe_resource *res,
482 unsigned level, unsigned slice)
483 {
484 struct ilo_texture *tex = ilo_texture(res);
485 struct ilo_state_cc_info info;
486
487 if (!ilo_image_can_enable_aux(&tex->image, level))
488 return;
489
490 /*
491 * From the Sandy Bridge PRM, volume 2 part 1, page 315:
492 *
493 * "(Hierarchical Depth Buffer Resolve) Depth Test Enable must be
494 * disabled. Depth Buffer Write Enable must be enabled. Stencil Test
495 * Enable and Stencil Buffer Write Enable must be disabled."
496 */
497 memset(&info, 0, sizeof(info));
498 info.depth.cv_has_buffer = true;
499 info.depth.write_enable = true;
500
501 ilo_blitter_set_invariants(blitter);
502 ilo_blitter_set_earlyz_op(blitter,
503 ILO_STATE_RASTER_EARLYZ_HIZ_RESOLVE, false);
504
505 ilo_blitter_set_cc(blitter, &info);
506 ilo_blitter_set_fb_from_resource(blitter, res, res->format, level, slice);
507 ilo_blitter_set_uses(blitter,
508 ILO_BLITTER_USE_DSA | ILO_BLITTER_USE_FB_DEPTH);
509
510 hiz_emit_rectlist(blitter);
511 }