gallium: remove unused pipe_viewport_state::translate[3] and scale[3]
[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_state_3d.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 pipe_vertex_element velem;
44 struct pipe_viewport_state vp;
45
46 if (blitter->initialized)
47 return true;
48
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);
53
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;
62
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;
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 memset(&vp, 0, sizeof(vp));
78 vp.scale[0] = 1.0f;
79 vp.scale[1] = 1.0f;
80 vp.scale[2] = 1.0f;
81 ilo_gpe_set_viewport_cso(blitter->ilo->dev, &vp, &blitter->viewport);
82
83 blitter->initialized = true;
84
85 return true;
86 }
87
88 static void
89 ilo_blitter_set_op(struct ilo_blitter *blitter,
90 enum ilo_blitter_rectlist_op op)
91 {
92 blitter->op = op;
93 }
94
95 /**
96 * Set the rectangle primitive.
97 */
98 static void
99 ilo_blitter_set_rectlist(struct ilo_blitter *blitter,
100 unsigned x, unsigned y,
101 unsigned width, unsigned height)
102 {
103 /*
104 * From the Sandy Bridge PRM, volume 2 part 1, page 11:
105 *
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."
110 */
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;
117 }
118
119 static void
120 ilo_blitter_set_clear_values(struct ilo_blitter *blitter,
121 uint32_t depth, ubyte stencil)
122 {
123 blitter->depth_clear_value = depth;
124 blitter->cc.stencil_ref.ref_value[0] = stencil;
125 }
126
127 static void
128 ilo_blitter_set_dsa(struct ilo_blitter *blitter,
129 const struct pipe_depth_stencil_alpha_state *state)
130 {
131 ilo_gpe_init_dsa(blitter->ilo->dev, state, &blitter->dsa);
132 }
133
134 static void
135 ilo_blitter_set_fb(struct ilo_blitter *blitter,
136 struct pipe_resource *res, unsigned level,
137 const struct ilo_surface_cso *cso)
138 {
139 struct ilo_texture *tex = ilo_texture(res);
140
141 blitter->fb.width = u_minify(tex->layout.width0, level);
142 blitter->fb.height = u_minify(tex->layout.height0, level);
143
144 blitter->fb.num_samples = res->nr_samples;
145 if (!blitter->fb.num_samples)
146 blitter->fb.num_samples = 1;
147
148 memcpy(&blitter->fb.dst, cso, sizeof(*cso));
149 }
150
151 static void
152 ilo_blitter_set_fb_from_surface(struct ilo_blitter *blitter,
153 struct pipe_surface *surf)
154 {
155 ilo_blitter_set_fb(blitter, surf->texture, surf->u.tex.level,
156 (const struct ilo_surface_cso *) surf);
157 }
158
159 static void
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)
164 {
165 struct pipe_surface templ, *surf;
166
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;
172
173 /* if we did not call create_surface(), it would never fail */
174 surf = blitter->ilo->base.create_surface(&blitter->ilo->base, res, &templ);
175 assert(surf);
176
177 ilo_blitter_set_fb(blitter, res, level,
178 (const struct ilo_surface_cso *) surf);
179
180 pipe_surface_reference(&surf, NULL);
181 }
182
183 static void
184 ilo_blitter_set_uses(struct ilo_blitter *blitter, uint32_t uses)
185 {
186 blitter->uses = uses;
187 }
188
189 static void
190 hiz_align_fb(struct ilo_blitter *blitter)
191 {
192 unsigned align_w, align_h;
193
194 switch (blitter->op) {
195 case ILO_BLITTER_RECTLIST_CLEAR_ZS:
196 case ILO_BLITTER_RECTLIST_RESOLVE_Z:
197 break;
198 default:
199 return;
200 break;
201 }
202
203 /*
204 * From the Sandy Bridge PRM, volume 2 part 1, page 313-314:
205 *
206 * "A rectangle primitive representing the clear area is delivered. The
207 * primitive must adhere to the following restrictions on size:
208 *
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.
213 *
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
218 * must be lit
219 *
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
224 * must be list."
225 *
226 * "The following is required when performing a depth buffer resolve:
227 *
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
231 * operation."
232 */
233 switch (blitter->fb.num_samples) {
234 case 1:
235 align_w = 8;
236 align_h = 4;
237 break;
238 case 2:
239 align_w = 4;
240 align_h = 4;
241 break;
242 case 4:
243 align_w = 4;
244 align_h = 2;
245 break;
246 case 8:
247 default:
248 align_w = 2;
249 align_h = 2;
250 break;
251 }
252
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);
256 }
257 }
258
259 static void
260 hiz_emit_rectlist(struct ilo_blitter *blitter)
261 {
262 hiz_align_fb(blitter);
263
264 ilo_blitter_set_rectlist(blitter, 0, 0,
265 blitter->fb.width, blitter->fb.height);
266
267 ilo_draw_rectlist(blitter->ilo);
268 }
269
270 static bool
271 hiz_can_clear_zs(const struct ilo_blitter *blitter,
272 const struct ilo_texture *tex)
273 {
274 /*
275 * From the Sandy Bridge PRM, volume 2 part 1, page 314:
276 *
277 * "Several cases exist where Depth Buffer Clear cannot be enabled (the
278 * legacy method of clearing must be performed):
279 *
280 * - If the depth buffer format is D32_FLOAT_S8X24_UINT or
281 * D24_UNORM_S8_UINT.
282 *
283 * - If stencil test is enabled but the separate stencil buffer is
284 * disabled.
285 *
286 * - [DevSNB-A{W/A}]: ...
287 *
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."
291 *
292 * From the Ivy Bridge PRM, volume 2 part 1, page 313:
293 *
294 * "Several cases exist where Depth Buffer Clear cannot be enabled (the
295 * legacy method of clearing must be performed):
296 *
297 * - If the depth buffer format is D32_FLOAT_S8X24_UINT or
298 * D24_UNORM_S8_UINT.
299 *
300 * - If stencil test is enabled but the separate stencil buffer is
301 * disabled."
302 *
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.
305 */
306 switch (tex->layout.format) {
307 case PIPE_FORMAT_Z16_UNORM:
308 if (ilo_dev_gen(blitter->ilo->dev) == ILO_GEN(6) &&
309 tex->base.width0 % 16)
310 return false;
311 break;
312 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
313 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
314 assert(!"HiZ with combined depth/stencil");
315 return false;
316 break;
317 default:
318 break;
319 }
320
321 return true;
322 }
323
324 bool
325 ilo_blitter_rectlist_clear_zs(struct ilo_blitter *blitter,
326 struct pipe_surface *zs,
327 unsigned clear_flags,
328 double depth, unsigned stencil)
329 {
330 struct ilo_texture *tex = ilo_texture(zs->texture);
331 struct pipe_depth_stencil_alpha_state dsa_state;
332 uint32_t uses, clear_value;
333
334 if (!ilo_texture_can_enable_hiz(tex,
335 zs->u.tex.level, zs->u.tex.first_layer,
336 zs->u.tex.last_layer - zs->u.tex.first_layer + 1))
337 return false;
338
339 if (!hiz_can_clear_zs(blitter, tex))
340 return false;
341
342 clear_value = util_pack_z(tex->layout.format, depth);
343
344 ilo_blit_resolve_surface(blitter->ilo, zs,
345 ILO_TEXTURE_RENDER_WRITE | ILO_TEXTURE_CLEAR);
346 ilo_texture_set_slice_clear_value(tex, zs->u.tex.level,
347 zs->u.tex.first_layer,
348 zs->u.tex.last_layer - zs->u.tex.first_layer + 1,
349 clear_value);
350
351 /*
352 * From the Sandy Bridge PRM, volume 2 part 1, page 313-314:
353 *
354 * "- Depth Test Enable must be disabled and Depth Buffer Write Enable
355 * must be enabled (if depth is being cleared).
356 *
357 * - Stencil buffer clear can be performed at the same time by
358 * enabling Stencil Buffer Write Enable. Stencil Test Enable must
359 * be enabled and Stencil Pass Depth Pass Op set to REPLACE, and the
360 * clear value that is placed in the stencil buffer is the Stencil
361 * Reference Value from COLOR_CALC_STATE.
362 *
363 * - Note also that stencil buffer clear can be performed without
364 * depth buffer clear. For stencil only clear, Depth Test Enable and
365 * Depth Buffer Write Enable must be disabled.
366 *
367 * - [DevSNB] errata: For stencil buffer only clear, the previous
368 * depth clear value must be delivered during the clear."
369 */
370 memset(&dsa_state, 0, sizeof(dsa_state));
371
372 if (clear_flags & PIPE_CLEAR_DEPTH)
373 dsa_state.depth.writemask = true;
374
375 if (clear_flags & PIPE_CLEAR_STENCIL) {
376 dsa_state.stencil[0].enabled = true;
377 dsa_state.stencil[0].func = PIPE_FUNC_ALWAYS;
378 dsa_state.stencil[0].fail_op = PIPE_STENCIL_OP_KEEP;
379 dsa_state.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
380 dsa_state.stencil[0].zfail_op = PIPE_STENCIL_OP_KEEP;
381
382 /*
383 * From the Ivy Bridge PRM, volume 2 part 1, page 277:
384 *
385 * "Additionally the following must be set to the correct values.
386 *
387 * - DEPTH_STENCIL_STATE::Stencil Write Mask must be 0xFF
388 * - DEPTH_STENCIL_STATE::Stencil Test Mask must be 0xFF
389 * - DEPTH_STENCIL_STATE::Back Face Stencil Write Mask must be 0xFF
390 * - DEPTH_STENCIL_STATE::Back Face Stencil Test Mask must be 0xFF"
391 */
392 dsa_state.stencil[0].valuemask = 0xff;
393 dsa_state.stencil[0].writemask = 0xff;
394 dsa_state.stencil[1].valuemask = 0xff;
395 dsa_state.stencil[1].writemask = 0xff;
396 }
397
398 ilo_blitter_set_invariants(blitter);
399 ilo_blitter_set_op(blitter, ILO_BLITTER_RECTLIST_CLEAR_ZS);
400
401 ilo_blitter_set_dsa(blitter, &dsa_state);
402 ilo_blitter_set_clear_values(blitter, clear_value, (ubyte) stencil);
403 ilo_blitter_set_fb_from_surface(blitter, zs);
404
405 uses = ILO_BLITTER_USE_DSA;
406 if (clear_flags & PIPE_CLEAR_DEPTH)
407 uses |= ILO_BLITTER_USE_VIEWPORT | ILO_BLITTER_USE_FB_DEPTH;
408 if (clear_flags & PIPE_CLEAR_STENCIL)
409 uses |= ILO_BLITTER_USE_CC | ILO_BLITTER_USE_FB_STENCIL;
410 ilo_blitter_set_uses(blitter, uses);
411
412 hiz_emit_rectlist(blitter);
413
414 return true;
415 }
416
417 void
418 ilo_blitter_rectlist_resolve_z(struct ilo_blitter *blitter,
419 struct pipe_resource *res,
420 unsigned level, unsigned slice)
421 {
422 struct ilo_texture *tex = ilo_texture(res);
423 struct pipe_depth_stencil_alpha_state dsa_state;
424 const struct ilo_texture_slice *s =
425 ilo_texture_get_slice(tex, level, slice);
426
427 if (!ilo_texture_can_enable_hiz(tex, level, slice, 1))
428 return;
429
430 /*
431 * From the Sandy Bridge PRM, volume 2 part 1, page 314:
432 *
433 * "Depth Test Enable must be enabled with the Depth Test Function set
434 * to NEVER. Depth Buffer Write Enable must be enabled. Stencil Test
435 * Enable and Stencil Buffer Write Enable must be disabled."
436 */
437 memset(&dsa_state, 0, sizeof(dsa_state));
438 dsa_state.depth.writemask = true;
439 dsa_state.depth.enabled = true;
440 dsa_state.depth.func = PIPE_FUNC_NEVER;
441
442 ilo_blitter_set_invariants(blitter);
443 ilo_blitter_set_op(blitter, ILO_BLITTER_RECTLIST_RESOLVE_Z);
444
445 ilo_blitter_set_dsa(blitter, &dsa_state);
446 ilo_blitter_set_clear_values(blitter, s->clear_value, 0);
447 ilo_blitter_set_fb_from_resource(blitter, res, res->format, level, slice);
448 ilo_blitter_set_uses(blitter,
449 ILO_BLITTER_USE_DSA | ILO_BLITTER_USE_FB_DEPTH);
450
451 hiz_emit_rectlist(blitter);
452 }
453
454 void
455 ilo_blitter_rectlist_resolve_hiz(struct ilo_blitter *blitter,
456 struct pipe_resource *res,
457 unsigned level, unsigned slice)
458 {
459 struct ilo_texture *tex = ilo_texture(res);
460 struct pipe_depth_stencil_alpha_state dsa_state;
461
462 if (!ilo_texture_can_enable_hiz(tex, level, slice, 1))
463 return;
464
465 /*
466 * From the Sandy Bridge PRM, volume 2 part 1, page 315:
467 *
468 * "(Hierarchical Depth Buffer Resolve) Depth Test Enable must be
469 * disabled. Depth Buffer Write Enable must be enabled. Stencil Test
470 * Enable and Stencil Buffer Write Enable must be disabled."
471 */
472 memset(&dsa_state, 0, sizeof(dsa_state));
473 dsa_state.depth.writemask = true;
474
475 ilo_blitter_set_invariants(blitter);
476 ilo_blitter_set_op(blitter, ILO_BLITTER_RECTLIST_RESOLVE_HIZ);
477
478 ilo_blitter_set_dsa(blitter, &dsa_state);
479 ilo_blitter_set_fb_from_resource(blitter, res, res->format, level, slice);
480 ilo_blitter_set_uses(blitter,
481 ILO_BLITTER_USE_DSA | ILO_BLITTER_USE_FB_DEPTH);
482
483 hiz_emit_rectlist(blitter);
484 }