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 "core/ilo_state_3d.h"
29 #include "util/u_draw.h"
30 #include "util/u_pack_color.h"
33 #include "ilo_state.h"
35 #include "ilo_blitter.h"
38 * Set the states that are invariant between all ops.
41 ilo_blitter_set_invariants(struct ilo_blitter
*blitter
)
43 struct ilo_state_vf_element_info elem
;
45 if (blitter
->initialized
)
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;
53 memset(&elem
, 0, sizeof(elem
));
54 /* only vertex X and Y */
55 elem
.format
= GEN6_FORMAT_R32G32_FLOAT
;
57 elem
.component_count
= 2;
59 ilo_state_vf_init_for_rectlist(&blitter
->vf
, blitter
->ilo
->dev
,
60 blitter
->vf_data
, sizeof(blitter
->vf_data
), &elem
, 1);
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);
69 * From the Haswell PRM, volume 7, page 615:
71 * "The clear value must be between the min and max depth values
72 * (inclusive) defined in the CC_VIEWPORT."
74 * Even though clipping and viewport transformation will be disabled, we
75 * still need to set up the viewport states.
77 ilo_state_viewport_init_for_rectlist(&blitter
->vp
, blitter
->ilo
->dev
,
78 blitter
->vp_data
, sizeof(blitter
->vp_data
));
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
);
83 ilo_state_urb_init_for_rectlist(&blitter
->urb
, blitter
->ilo
->dev
,
84 ilo_state_vf_get_attr_count(&blitter
->vf
));
86 blitter
->initialized
= true;
92 ilo_blitter_set_earlyz_op(struct ilo_blitter
*blitter
,
93 enum ilo_state_raster_earlyz_op op
,
94 bool earlyz_stencil_clear
)
96 blitter
->earlyz_op
= op
;
97 blitter
->earlyz_stencil_clear
= earlyz_stencil_clear
;
101 * Set the rectangle primitive.
104 ilo_blitter_set_rectlist(struct ilo_blitter
*blitter
,
105 unsigned x
, unsigned y
,
106 unsigned width
, unsigned height
)
109 * From the Sandy Bridge PRM, volume 2 part 1, page 11:
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."
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
;
125 ilo_blitter_set_depth_clear_value(struct ilo_blitter
*blitter
,
128 blitter
->depth_clear_value
= depth
;
132 ilo_blitter_set_cc(struct ilo_blitter
*blitter
,
133 const struct ilo_state_cc_info
*info
)
135 memset(&blitter
->cc
, 0, sizeof(blitter
->cc
));
136 ilo_state_cc_init(&blitter
->cc
, blitter
->ilo
->dev
, info
);
140 ilo_blitter_set_fb_rs(struct ilo_blitter
*blitter
)
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
);
149 ilo_blitter_set_fb(struct ilo_blitter
*blitter
,
150 struct pipe_resource
*res
, unsigned level
,
151 const struct ilo_surface_cso
*cso
)
153 struct ilo_texture
*tex
= ilo_texture(res
);
155 blitter
->fb
.width
= u_minify(tex
->image
.width0
, level
);
156 blitter
->fb
.height
= u_minify(tex
->image
.height0
, level
);
158 blitter
->fb
.num_samples
= res
->nr_samples
;
159 if (!blitter
->fb
.num_samples
)
160 blitter
->fb
.num_samples
= 1;
162 memcpy(&blitter
->fb
.dst
, cso
, sizeof(*cso
));
164 ilo_blitter_set_fb_rs(blitter
);
168 ilo_blitter_set_fb_from_surface(struct ilo_blitter
*blitter
,
169 struct pipe_surface
*surf
)
171 ilo_blitter_set_fb(blitter
, surf
->texture
, surf
->u
.tex
.level
,
172 (const struct ilo_surface_cso
*) surf
);
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
)
181 struct pipe_surface templ
, *surf
;
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
;
189 /* if we did not call create_surface(), it would never fail */
190 surf
= blitter
->ilo
->base
.create_surface(&blitter
->ilo
->base
, res
, &templ
);
193 ilo_blitter_set_fb(blitter
, res
, level
,
194 (const struct ilo_surface_cso
*) surf
);
196 pipe_surface_reference(&surf
, NULL
);
200 ilo_blitter_set_uses(struct ilo_blitter
*blitter
, uint32_t uses
)
202 blitter
->uses
= uses
;
206 hiz_align_fb(struct ilo_blitter
*blitter
)
208 unsigned align_w
, align_h
;
210 switch (blitter
->earlyz_op
) {
211 case ILO_STATE_RASTER_EARLYZ_DEPTH_CLEAR
:
212 case ILO_STATE_RASTER_EARLYZ_DEPTH_RESOLVE
:
220 * From the Sandy Bridge PRM, volume 2 part 1, page 313-314:
222 * "A rectangle primitive representing the clear area is delivered. The
223 * primitive must adhere to the following restrictions on size:
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.
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
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
242 * "The following is required when performing a depth buffer resolve:
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
249 switch (blitter
->fb
.num_samples
) {
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
);
276 hiz_emit_rectlist(struct ilo_blitter
*blitter
)
278 hiz_align_fb(blitter
);
280 ilo_blitter_set_rectlist(blitter
, 0, 0,
281 blitter
->fb
.width
, blitter
->fb
.height
);
283 ilo_draw_rectlist(blitter
->ilo
);
287 hiz_can_clear_zs(const struct ilo_blitter
*blitter
,
288 const struct ilo_texture
*tex
)
291 * From the Sandy Bridge PRM, volume 2 part 1, page 314:
293 * "Several cases exist where Depth Buffer Clear cannot be enabled (the
294 * legacy method of clearing must be performed):
296 * - If the depth buffer format is D32_FLOAT_S8X24_UINT or
299 * - If stencil test is enabled but the separate stencil buffer is
302 * - [DevSNB-A{W/A}]: ...
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."
308 * From the Ivy Bridge PRM, volume 2 part 1, page 313:
310 * "Several cases exist where Depth Buffer Clear cannot be enabled (the
311 * legacy method of clearing must be performed):
313 * - If the depth buffer format is D32_FLOAT_S8X24_UINT or
316 * - If stencil test is enabled but the separate stencil buffer is
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.
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)
328 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
329 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
330 assert(!"HiZ with combined depth/stencil");
341 ilo_blitter_rectlist_clear_zs(struct ilo_blitter
*blitter
,
342 struct pipe_surface
*zs
,
343 unsigned clear_flags
,
344 double depth
, unsigned stencil
)
346 struct ilo_texture
*tex
= ilo_texture(zs
->texture
);
347 struct ilo_state_cc_info info
;
348 uint32_t uses
, clear_value
;
350 if (!ilo_image_can_enable_aux(&tex
->image
, zs
->u
.tex
.level
))
353 if (!hiz_can_clear_zs(blitter
, tex
))
356 if (ilo_dev_gen(blitter
->ilo
->dev
) >= ILO_GEN(8))
357 clear_value
= fui(depth
);
359 clear_value
= util_pack_z(tex
->image
.format
, depth
);
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,
369 * From the Sandy Bridge PRM, volume 2 part 1, page 313-314:
371 * "- Depth Test Enable must be disabled and Depth Buffer Write Enable
372 * must be enabled (if depth is being cleared).
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.
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.
384 * - [DevSNB] errata: For stencil buffer only clear, the previous
385 * depth clear value must be delivered during the clear."
387 memset(&info
, 0, sizeof(info
));
389 if (clear_flags
& PIPE_CLEAR_DEPTH
) {
390 info
.depth
.cv_has_buffer
= true;
391 info
.depth
.write_enable
= true;
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
;
403 * From the Ivy Bridge PRM, volume 2 part 1, page 277:
405 * "Additionally the following must be set to the correct values.
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"
412 * Back frace masks will be copied from front face masks.
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;
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
);
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
);
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
);
435 hiz_emit_rectlist(blitter
);
441 ilo_blitter_rectlist_resolve_z(struct ilo_blitter
*blitter
,
442 struct pipe_resource
*res
,
443 unsigned level
, unsigned slice
)
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
);
450 if (!ilo_image_can_enable_aux(&tex
->image
, level
))
454 * From the Sandy Bridge PRM, volume 2 part 1, page 314:
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."
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
;
466 ilo_blitter_set_invariants(blitter
);
467 ilo_blitter_set_earlyz_op(blitter
,
468 ILO_STATE_RASTER_EARLYZ_DEPTH_RESOLVE
, false);
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
);
476 hiz_emit_rectlist(blitter
);
480 ilo_blitter_rectlist_resolve_hiz(struct ilo_blitter
*blitter
,
481 struct pipe_resource
*res
,
482 unsigned level
, unsigned slice
)
484 struct ilo_texture
*tex
= ilo_texture(res
);
485 struct ilo_state_cc_info info
;
487 if (!ilo_image_can_enable_aux(&tex
->image
, level
))
491 * From the Sandy Bridge PRM, volume 2 part 1, page 315:
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."
497 memset(&info
, 0, sizeof(info
));
498 info
.depth
.cv_has_buffer
= true;
499 info
.depth
.write_enable
= true;
501 ilo_blitter_set_invariants(blitter
);
502 ilo_blitter_set_earlyz_op(blitter
,
503 ILO_STATE_RASTER_EARLYZ_HIZ_RESOLVE
, false);
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
);
510 hiz_emit_rectlist(blitter
);