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 pipe_vertex_element velem
;
44 struct pipe_viewport_state vp
;
46 if (blitter
->initialized
)
49 /* only vertex X and Y */
50 memset(&velem
, 0, sizeof(velem
));
51 velem
.src_format
= PIPE_FORMAT_R32G32_FLOAT
;
52 ilo_gpe_init_ve(blitter
->ilo
->dev
, 1, &velem
, &blitter
->ve
);
54 /* generate VUE header */
55 ilo_gpe_init_ve_nosrc(blitter
->ilo
->dev
,
56 GEN6_VFCOMP_STORE_0
, /* Reserved */
57 GEN6_VFCOMP_STORE_0
, /* Render Target Array Index */
58 GEN6_VFCOMP_STORE_0
, /* Viewport Index */
59 GEN6_VFCOMP_STORE_0
, /* Point Width */
60 &blitter
->ve
.nosrc_cso
);
61 blitter
->ve
.prepend_nosrc_cso
= true;
63 /* a rectangle has 3 vertices in a RECTLIST */
64 util_draw_init_info(&blitter
->draw
);
65 blitter
->draw
.mode
= ILO_PRIM_RECTANGLES
;
66 blitter
->draw
.count
= 3;
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 memset(&vp
, 0, sizeof(vp
));
81 ilo_gpe_set_viewport_cso(blitter
->ilo
->dev
, &vp
, &blitter
->viewport
);
83 blitter
->initialized
= true;
89 ilo_blitter_set_op(struct ilo_blitter
*blitter
,
90 enum ilo_blitter_rectlist_op op
)
96 * Set the rectangle primitive.
99 ilo_blitter_set_rectlist(struct ilo_blitter
*blitter
,
100 unsigned x
, unsigned y
,
101 unsigned width
, unsigned height
)
104 * From the Sandy Bridge PRM, volume 2 part 1, page 11:
106 * "(RECTLIST) A list of independent rectangles, where only 3 vertices
107 * are provided per rectangle object, with the fourth vertex implied
108 * by the definition of a rectangle. V0=LowerRight, V1=LowerLeft,
109 * V2=UpperLeft. Implied V3 = V0- V1+V2."
111 blitter
->vertices
[0][0] = (float) (x
+ width
);
112 blitter
->vertices
[0][1] = (float) (y
+ height
);
113 blitter
->vertices
[1][0] = (float) x
;
114 blitter
->vertices
[1][1] = (float) (y
+ height
);
115 blitter
->vertices
[2][0] = (float) x
;
116 blitter
->vertices
[2][1] = (float) y
;
120 ilo_blitter_set_clear_values(struct ilo_blitter
*blitter
,
121 uint32_t depth
, ubyte stencil
)
123 blitter
->depth_clear_value
= depth
;
124 blitter
->cc
.stencil_ref
.ref_value
[0] = stencil
;
128 ilo_blitter_set_dsa(struct ilo_blitter
*blitter
,
129 const struct pipe_depth_stencil_alpha_state
*state
)
131 ilo_gpe_init_dsa(blitter
->ilo
->dev
, state
, &blitter
->dsa
);
135 ilo_blitter_set_fb(struct ilo_blitter
*blitter
,
136 struct pipe_resource
*res
, unsigned level
,
137 const struct ilo_surface_cso
*cso
)
139 struct ilo_texture
*tex
= ilo_texture(res
);
141 blitter
->fb
.width
= u_minify(tex
->image
.width0
, level
);
142 blitter
->fb
.height
= u_minify(tex
->image
.height0
, level
);
144 blitter
->fb
.num_samples
= res
->nr_samples
;
145 if (!blitter
->fb
.num_samples
)
146 blitter
->fb
.num_samples
= 1;
148 memcpy(&blitter
->fb
.dst
, cso
, sizeof(*cso
));
152 ilo_blitter_set_fb_from_surface(struct ilo_blitter
*blitter
,
153 struct pipe_surface
*surf
)
155 ilo_blitter_set_fb(blitter
, surf
->texture
, surf
->u
.tex
.level
,
156 (const struct ilo_surface_cso
*) surf
);
160 ilo_blitter_set_fb_from_resource(struct ilo_blitter
*blitter
,
161 struct pipe_resource
*res
,
162 enum pipe_format format
,
163 unsigned level
, unsigned slice
)
165 struct pipe_surface templ
, *surf
;
167 memset(&templ
, 0, sizeof(templ
));
168 templ
.format
= format
;
169 templ
.u
.tex
.level
= level
;
170 templ
.u
.tex
.first_layer
= slice
;
171 templ
.u
.tex
.last_layer
= slice
;
173 /* if we did not call create_surface(), it would never fail */
174 surf
= blitter
->ilo
->base
.create_surface(&blitter
->ilo
->base
, res
, &templ
);
177 ilo_blitter_set_fb(blitter
, res
, level
,
178 (const struct ilo_surface_cso
*) surf
);
180 pipe_surface_reference(&surf
, NULL
);
184 ilo_blitter_set_uses(struct ilo_blitter
*blitter
, uint32_t uses
)
186 blitter
->uses
= uses
;
190 hiz_align_fb(struct ilo_blitter
*blitter
)
192 unsigned align_w
, align_h
;
194 switch (blitter
->op
) {
195 case ILO_BLITTER_RECTLIST_CLEAR_ZS
:
196 case ILO_BLITTER_RECTLIST_RESOLVE_Z
:
204 * From the Sandy Bridge PRM, volume 2 part 1, page 313-314:
206 * "A rectangle primitive representing the clear area is delivered. The
207 * primitive must adhere to the following restrictions on size:
209 * - If Number of Multisamples is NUMSAMPLES_1, the rectangle must be
210 * aligned to an 8x4 pixel block relative to the upper left corner
211 * of the depth buffer, and contain an integer number of these pixel
212 * blocks, and all 8x4 pixels must be lit.
214 * - If Number of Multisamples is NUMSAMPLES_4, the rectangle must be
215 * aligned to a 4x2 pixel block (8x4 sample block) relative to the
216 * upper left corner of the depth buffer, and contain an integer
217 * number of these pixel blocks, and all samples of the 4x2 pixels
220 * - If Number of Multisamples is NUMSAMPLES_8, the rectangle must be
221 * aligned to a 2x2 pixel block (8x4 sample block) relative to the
222 * upper left corner of the depth buffer, and contain an integer
223 * number of these pixel blocks, and all samples of the 2x2 pixels
226 * "The following is required when performing a depth buffer resolve:
228 * - A rectangle primitive of the same size as the previous depth
229 * buffer clear operation must be delivered, and depth buffer state
230 * cannot have changed since the previous depth buffer clear
233 switch (blitter
->fb
.num_samples
) {
253 if (blitter
->fb
.width
% align_w
|| blitter
->fb
.height
% align_h
) {
254 blitter
->fb
.width
= align(blitter
->fb
.width
, align_w
);
255 blitter
->fb
.height
= align(blitter
->fb
.height
, align_h
);
260 hiz_emit_rectlist(struct ilo_blitter
*blitter
)
262 hiz_align_fb(blitter
);
264 ilo_blitter_set_rectlist(blitter
, 0, 0,
265 blitter
->fb
.width
, blitter
->fb
.height
);
267 ilo_draw_rectlist(blitter
->ilo
);
271 hiz_can_clear_zs(const struct ilo_blitter
*blitter
,
272 const struct ilo_texture
*tex
)
275 * From the Sandy Bridge PRM, volume 2 part 1, page 314:
277 * "Several cases exist where Depth Buffer Clear cannot be enabled (the
278 * legacy method of clearing must be performed):
280 * - If the depth buffer format is D32_FLOAT_S8X24_UINT or
283 * - If stencil test is enabled but the separate stencil buffer is
286 * - [DevSNB-A{W/A}]: ...
288 * - [DevSNB{W/A}]: When depth buffer format is D16_UNORM and the
289 * width of the map (LOD0) is not multiple of 16, fast clear
290 * optimization must be disabled."
292 * From the Ivy Bridge PRM, volume 2 part 1, page 313:
294 * "Several cases exist where Depth Buffer Clear cannot be enabled (the
295 * legacy method of clearing must be performed):
297 * - If the depth buffer format is D32_FLOAT_S8X24_UINT or
300 * - If stencil test is enabled but the separate stencil buffer is
303 * The truth is when HiZ is enabled, separate stencil is also enabled on
304 * all GENs. The depth buffer format cannot be combined depth/stencil.
306 switch (tex
->image
.format
) {
307 case PIPE_FORMAT_Z16_UNORM
:
308 if (ilo_dev_gen(blitter
->ilo
->dev
) == ILO_GEN(6) &&
309 tex
->base
.width0
% 16)
312 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
313 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
314 assert(!"HiZ with combined depth/stencil");
325 ilo_blitter_rectlist_clear_zs(struct ilo_blitter
*blitter
,
326 struct pipe_surface
*zs
,
327 unsigned clear_flags
,
328 double depth
, unsigned stencil
)
330 struct ilo_texture
*tex
= ilo_texture(zs
->texture
);
331 struct pipe_depth_stencil_alpha_state dsa_state
;
332 uint32_t uses
, clear_value
;
334 if (!ilo_image_can_enable_aux(&tex
->image
, zs
->u
.tex
.level
))
337 if (!hiz_can_clear_zs(blitter
, tex
))
340 if (ilo_dev_gen(blitter
->ilo
->dev
) >= ILO_GEN(8))
341 clear_value
= fui(depth
);
343 clear_value
= util_pack_z(tex
->image
.format
, depth
);
345 ilo_blit_resolve_surface(blitter
->ilo
, zs
,
346 ILO_TEXTURE_RENDER_WRITE
| ILO_TEXTURE_CLEAR
);
347 ilo_texture_set_slice_clear_value(tex
, zs
->u
.tex
.level
,
348 zs
->u
.tex
.first_layer
,
349 zs
->u
.tex
.last_layer
- zs
->u
.tex
.first_layer
+ 1,
353 * From the Sandy Bridge PRM, volume 2 part 1, page 313-314:
355 * "- Depth Test Enable must be disabled and Depth Buffer Write Enable
356 * must be enabled (if depth is being cleared).
358 * - Stencil buffer clear can be performed at the same time by
359 * enabling Stencil Buffer Write Enable. Stencil Test Enable must
360 * be enabled and Stencil Pass Depth Pass Op set to REPLACE, and the
361 * clear value that is placed in the stencil buffer is the Stencil
362 * Reference Value from COLOR_CALC_STATE.
364 * - Note also that stencil buffer clear can be performed without
365 * depth buffer clear. For stencil only clear, Depth Test Enable and
366 * Depth Buffer Write Enable must be disabled.
368 * - [DevSNB] errata: For stencil buffer only clear, the previous
369 * depth clear value must be delivered during the clear."
371 memset(&dsa_state
, 0, sizeof(dsa_state
));
373 if (clear_flags
& PIPE_CLEAR_DEPTH
)
374 dsa_state
.depth
.writemask
= true;
376 if (clear_flags
& PIPE_CLEAR_STENCIL
) {
377 dsa_state
.stencil
[0].enabled
= true;
378 dsa_state
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
379 dsa_state
.stencil
[0].fail_op
= PIPE_STENCIL_OP_KEEP
;
380 dsa_state
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
381 dsa_state
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_KEEP
;
384 * From the Ivy Bridge PRM, volume 2 part 1, page 277:
386 * "Additionally the following must be set to the correct values.
388 * - DEPTH_STENCIL_STATE::Stencil Write Mask must be 0xFF
389 * - DEPTH_STENCIL_STATE::Stencil Test Mask must be 0xFF
390 * - DEPTH_STENCIL_STATE::Back Face Stencil Write Mask must be 0xFF
391 * - DEPTH_STENCIL_STATE::Back Face Stencil Test Mask must be 0xFF"
393 dsa_state
.stencil
[0].valuemask
= 0xff;
394 dsa_state
.stencil
[0].writemask
= 0xff;
395 dsa_state
.stencil
[1].valuemask
= 0xff;
396 dsa_state
.stencil
[1].writemask
= 0xff;
399 ilo_blitter_set_invariants(blitter
);
400 ilo_blitter_set_op(blitter
, ILO_BLITTER_RECTLIST_CLEAR_ZS
);
402 ilo_blitter_set_dsa(blitter
, &dsa_state
);
403 ilo_blitter_set_clear_values(blitter
, clear_value
, (ubyte
) stencil
);
404 ilo_blitter_set_fb_from_surface(blitter
, zs
);
406 uses
= ILO_BLITTER_USE_DSA
;
407 if (clear_flags
& PIPE_CLEAR_DEPTH
)
408 uses
|= ILO_BLITTER_USE_VIEWPORT
| ILO_BLITTER_USE_FB_DEPTH
;
409 if (clear_flags
& PIPE_CLEAR_STENCIL
)
410 uses
|= ILO_BLITTER_USE_CC
| ILO_BLITTER_USE_FB_STENCIL
;
411 ilo_blitter_set_uses(blitter
, uses
);
413 hiz_emit_rectlist(blitter
);
419 ilo_blitter_rectlist_resolve_z(struct ilo_blitter
*blitter
,
420 struct pipe_resource
*res
,
421 unsigned level
, unsigned slice
)
423 struct ilo_texture
*tex
= ilo_texture(res
);
424 struct pipe_depth_stencil_alpha_state dsa_state
;
425 const struct ilo_texture_slice
*s
=
426 ilo_texture_get_slice(tex
, level
, slice
);
428 if (!ilo_image_can_enable_aux(&tex
->image
, level
))
432 * From the Sandy Bridge PRM, volume 2 part 1, page 314:
434 * "Depth Test Enable must be enabled with the Depth Test Function set
435 * to NEVER. Depth Buffer Write Enable must be enabled. Stencil Test
436 * Enable and Stencil Buffer Write Enable must be disabled."
438 memset(&dsa_state
, 0, sizeof(dsa_state
));
439 dsa_state
.depth
.writemask
= true;
440 dsa_state
.depth
.enabled
= true;
441 dsa_state
.depth
.func
= PIPE_FUNC_NEVER
;
443 ilo_blitter_set_invariants(blitter
);
444 ilo_blitter_set_op(blitter
, ILO_BLITTER_RECTLIST_RESOLVE_Z
);
446 ilo_blitter_set_dsa(blitter
, &dsa_state
);
447 ilo_blitter_set_clear_values(blitter
, s
->clear_value
, 0);
448 ilo_blitter_set_fb_from_resource(blitter
, res
, res
->format
, level
, slice
);
449 ilo_blitter_set_uses(blitter
,
450 ILO_BLITTER_USE_DSA
| ILO_BLITTER_USE_FB_DEPTH
);
452 hiz_emit_rectlist(blitter
);
456 ilo_blitter_rectlist_resolve_hiz(struct ilo_blitter
*blitter
,
457 struct pipe_resource
*res
,
458 unsigned level
, unsigned slice
)
460 struct ilo_texture
*tex
= ilo_texture(res
);
461 struct pipe_depth_stencil_alpha_state dsa_state
;
463 if (!ilo_image_can_enable_aux(&tex
->image
, level
))
467 * From the Sandy Bridge PRM, volume 2 part 1, page 315:
469 * "(Hierarchical Depth Buffer Resolve) Depth Test Enable must be
470 * disabled. Depth Buffer Write Enable must be enabled. Stencil Test
471 * Enable and Stencil Buffer Write Enable must be disabled."
473 memset(&dsa_state
, 0, sizeof(dsa_state
));
474 dsa_state
.depth
.writemask
= true;
476 ilo_blitter_set_invariants(blitter
);
477 ilo_blitter_set_op(blitter
, ILO_BLITTER_RECTLIST_RESOLVE_HIZ
);
479 ilo_blitter_set_dsa(blitter
, &dsa_state
);
480 ilo_blitter_set_fb_from_resource(blitter
, res
, res
->format
, level
, slice
);
481 ilo_blitter_set_uses(blitter
,
482 ILO_BLITTER_USE_DSA
| ILO_BLITTER_USE_FB_DEPTH
);
484 hiz_emit_rectlist(blitter
);