2 * Mesa 3-D graphics library
4 * Copyright (C) 2014 LunarG, Inc.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
25 * Chia-I Wu <olv@lunarg.com>
28 #include "util/u_draw.h"
29 #include "util/u_pack_color.h"
32 #include "ilo_state.h"
34 #include "ilo_blitter.h"
37 * Set the states that are invariant between all ops.
40 ilo_blitter_set_invariants(struct ilo_blitter
*blitter
)
42 struct ilo_state_vf_element_info elem
;
44 if (blitter
->initialized
)
47 /* a rectangle has 3 vertices in a RECTLIST */
48 util_draw_init_info(&blitter
->draw
);
49 blitter
->draw
.mode
= ILO_PRIM_RECTANGLES
;
50 blitter
->draw
.count
= 3;
52 memset(&elem
, 0, sizeof(elem
));
53 /* only vertex X and Y */
54 elem
.format
= GEN6_FORMAT_R32G32_FLOAT
;
56 elem
.component_count
= 2;
58 ilo_state_vf_init_for_rectlist(&blitter
->vf
, blitter
->ilo
->dev
,
59 blitter
->vf_data
, sizeof(blitter
->vf_data
), &elem
, 1);
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);
68 * From the Haswell PRM, volume 7, page 615:
70 * "The clear value must be between the min and max depth values
71 * (inclusive) defined in the CC_VIEWPORT."
73 * Even though clipping and viewport transformation will be disabled, we
74 * still need to set up the viewport states.
76 ilo_state_viewport_init_for_rectlist(&blitter
->vp
, blitter
->ilo
->dev
,
77 blitter
->vp_data
, sizeof(blitter
->vp_data
));
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
);
82 ilo_state_urb_init_for_rectlist(&blitter
->urb
, blitter
->ilo
->dev
,
83 ilo_state_vf_get_attr_count(&blitter
->vf
));
85 blitter
->initialized
= true;
91 ilo_blitter_set_earlyz_op(struct ilo_blitter
*blitter
,
92 enum ilo_state_raster_earlyz_op op
,
93 bool earlyz_stencil_clear
)
95 blitter
->earlyz_op
= op
;
96 blitter
->earlyz_stencil_clear
= earlyz_stencil_clear
;
100 * Set the rectangle primitive.
103 ilo_blitter_set_rectlist(struct ilo_blitter
*blitter
,
104 unsigned x
, unsigned y
,
105 unsigned width
, unsigned height
)
108 * From the Sandy Bridge PRM, volume 2 part 1, page 11:
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."
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
;
124 ilo_blitter_set_depth_clear_value(struct ilo_blitter
*blitter
,
127 blitter
->depth_clear_value
= depth
;
131 ilo_blitter_set_cc(struct ilo_blitter
*blitter
,
132 const struct ilo_state_cc_info
*info
)
134 memset(&blitter
->cc
, 0, sizeof(blitter
->cc
));
135 ilo_state_cc_init(&blitter
->cc
, blitter
->ilo
->dev
, info
);
139 ilo_blitter_set_fb_rs(struct ilo_blitter
*blitter
)
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
);
148 ilo_blitter_set_fb(struct ilo_blitter
*blitter
,
149 struct pipe_resource
*res
, unsigned level
,
150 const struct ilo_surface_cso
*cso
)
152 struct ilo_texture
*tex
= ilo_texture(res
);
154 blitter
->fb
.width
= u_minify(tex
->image
.width0
, level
);
155 blitter
->fb
.height
= u_minify(tex
->image
.height0
, level
);
157 blitter
->fb
.num_samples
= res
->nr_samples
;
158 if (!blitter
->fb
.num_samples
)
159 blitter
->fb
.num_samples
= 1;
161 memcpy(&blitter
->fb
.dst
, cso
, sizeof(*cso
));
163 ilo_blitter_set_fb_rs(blitter
);
167 ilo_blitter_set_fb_from_surface(struct ilo_blitter
*blitter
,
168 struct pipe_surface
*surf
)
170 ilo_blitter_set_fb(blitter
, surf
->texture
, surf
->u
.tex
.level
,
171 (const struct ilo_surface_cso
*) surf
);
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
)
180 struct pipe_surface templ
, *surf
;
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
;
188 /* if we did not call create_surface(), it would never fail */
189 surf
= blitter
->ilo
->base
.create_surface(&blitter
->ilo
->base
, res
, &templ
);
192 ilo_blitter_set_fb(blitter
, res
, level
,
193 (const struct ilo_surface_cso
*) surf
);
195 pipe_surface_reference(&surf
, NULL
);
199 ilo_blitter_set_uses(struct ilo_blitter
*blitter
, uint32_t uses
)
201 blitter
->uses
= uses
;
205 hiz_align_fb(struct ilo_blitter
*blitter
)
207 unsigned align_w
, align_h
;
209 switch (blitter
->earlyz_op
) {
210 case ILO_STATE_RASTER_EARLYZ_DEPTH_CLEAR
:
211 case ILO_STATE_RASTER_EARLYZ_DEPTH_RESOLVE
:
219 * From the Sandy Bridge PRM, volume 2 part 1, page 313-314:
221 * "A rectangle primitive representing the clear area is delivered. The
222 * primitive must adhere to the following restrictions on size:
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.
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
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
241 * "The following is required when performing a depth buffer resolve:
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
248 switch (blitter
->fb
.num_samples
) {
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
);
275 hiz_emit_rectlist(struct ilo_blitter
*blitter
)
277 hiz_align_fb(blitter
);
279 ilo_blitter_set_rectlist(blitter
, 0, 0,
280 blitter
->fb
.width
, blitter
->fb
.height
);
282 ilo_draw_rectlist(blitter
->ilo
);
286 hiz_can_clear_zs(const struct ilo_blitter
*blitter
,
287 const struct ilo_texture
*tex
)
290 * From the Sandy Bridge PRM, volume 2 part 1, page 314:
292 * "Several cases exist where Depth Buffer Clear cannot be enabled (the
293 * legacy method of clearing must be performed):
295 * - If the depth buffer format is D32_FLOAT_S8X24_UINT or
298 * - If stencil test is enabled but the separate stencil buffer is
301 * - [DevSNB-A{W/A}]: ...
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."
307 * From the Ivy Bridge PRM, volume 2 part 1, page 313:
309 * "Several cases exist where Depth Buffer Clear cannot be enabled (the
310 * legacy method of clearing must be performed):
312 * - If the depth buffer format is D32_FLOAT_S8X24_UINT or
315 * - If stencil test is enabled but the separate stencil buffer is
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.
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)
327 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
328 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
329 assert(!"HiZ with combined depth/stencil");
340 ilo_blitter_rectlist_clear_zs(struct ilo_blitter
*blitter
,
341 struct pipe_surface
*zs
,
342 unsigned clear_flags
,
343 double depth
, unsigned stencil
)
345 struct ilo_texture
*tex
= ilo_texture(zs
->texture
);
346 struct ilo_state_cc_info info
;
347 uint32_t uses
, clear_value
;
349 if (!ilo_image_can_enable_aux(&tex
->image
, zs
->u
.tex
.level
))
352 if (!hiz_can_clear_zs(blitter
, tex
))
355 if (ilo_dev_gen(blitter
->ilo
->dev
) >= ILO_GEN(8))
356 clear_value
= fui(depth
);
358 clear_value
= util_pack_z(tex
->image
.format
, depth
);
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,
368 * From the Sandy Bridge PRM, volume 2 part 1, page 313-314:
370 * "- Depth Test Enable must be disabled and Depth Buffer Write Enable
371 * must be enabled (if depth is being cleared).
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.
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.
383 * - [DevSNB] errata: For stencil buffer only clear, the previous
384 * depth clear value must be delivered during the clear."
386 memset(&info
, 0, sizeof(info
));
388 if (clear_flags
& PIPE_CLEAR_DEPTH
) {
389 info
.depth
.cv_has_buffer
= true;
390 info
.depth
.write_enable
= true;
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
;
402 * From the Ivy Bridge PRM, volume 2 part 1, page 277:
404 * "Additionally the following must be set to the correct values.
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"
411 * Back frace masks will be copied from front face masks.
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;
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
);
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
);
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
);
434 hiz_emit_rectlist(blitter
);
440 ilo_blitter_rectlist_resolve_z(struct ilo_blitter
*blitter
,
441 struct pipe_resource
*res
,
442 unsigned level
, unsigned slice
)
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
);
449 if (!ilo_image_can_enable_aux(&tex
->image
, level
))
453 * From the Sandy Bridge PRM, volume 2 part 1, page 314:
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."
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
;
465 ilo_blitter_set_invariants(blitter
);
466 ilo_blitter_set_earlyz_op(blitter
,
467 ILO_STATE_RASTER_EARLYZ_DEPTH_RESOLVE
, false);
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
);
475 hiz_emit_rectlist(blitter
);
479 ilo_blitter_rectlist_resolve_hiz(struct ilo_blitter
*blitter
,
480 struct pipe_resource
*res
,
481 unsigned level
, unsigned slice
)
483 struct ilo_texture
*tex
= ilo_texture(res
);
484 struct ilo_state_cc_info info
;
486 if (!ilo_image_can_enable_aux(&tex
->image
, level
))
490 * From the Sandy Bridge PRM, volume 2 part 1, page 315:
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."
496 memset(&info
, 0, sizeof(info
));
497 info
.depth
.cv_has_buffer
= true;
498 info
.depth
.write_enable
= true;
500 ilo_blitter_set_invariants(blitter
);
501 ilo_blitter_set_earlyz_op(blitter
,
502 ILO_STATE_RASTER_EARLYZ_HIZ_RESOLVE
, false);
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
);
509 hiz_emit_rectlist(blitter
);