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