iris: Disable fast clears when running with INTEL_DEBUG=nofc
[mesa.git] / src / gallium / drivers / iris / iris_clear.c
1 /*
2 * Copyright © 2017 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 #include <stdio.h>
24 #include <errno.h>
25 #include "pipe/p_defines.h"
26 #include "pipe/p_state.h"
27 #include "pipe/p_context.h"
28 #include "pipe/p_screen.h"
29 #include "util/u_inlines.h"
30 #include "util/u_format.h"
31 #include "util/u_upload_mgr.h"
32 #include "util/ralloc.h"
33 #include "iris_context.h"
34 #include "iris_resource.h"
35 #include "iris_screen.h"
36 #include "intel/compiler/brw_compiler.h"
37 #include "util/format_srgb.h"
38
39 static bool
40 iris_is_color_fast_clear_compatible(struct iris_context *ice,
41 enum isl_format format,
42 const union isl_color_value color)
43 {
44 struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
45 const struct gen_device_info *devinfo = &batch->screen->devinfo;
46
47 if (isl_format_has_int_channel(format)) {
48 perf_debug(&ice->dbg, "Integer fast clear not enabled for %s",
49 isl_format_get_name(format));
50 return false;
51 }
52
53 for (int i = 0; i < 4; i++) {
54 if (!isl_format_has_color_component(format, i)) {
55 continue;
56 }
57
58 if (devinfo->gen < 9 &&
59 color.f32[i] != 0.0f && color.f32[i] != 1.0f) {
60 return false;
61 }
62 }
63
64 return true;
65 }
66
67 static bool
68 can_fast_clear_color(struct iris_context *ice,
69 struct pipe_resource *p_res,
70 unsigned level,
71 const struct pipe_box *box,
72 enum isl_format format,
73 enum isl_format render_format,
74 union isl_color_value color)
75 {
76 struct iris_resource *res = (void *) p_res;
77
78 if (INTEL_DEBUG & DEBUG_NO_FAST_CLEAR)
79 return false;
80
81 if (res->aux.usage == ISL_AUX_USAGE_NONE)
82 return false;
83
84 /* Check for partial clear */
85 if (box->x > 0 || box->y > 0 ||
86 box->width < minify(p_res->width0, level) ||
87 box->height < minify(p_res->height0, level)) {
88 return false;
89 }
90
91 /* We store clear colors as floats or uints as needed. If there are
92 * texture views in play, the formats will not properly be respected
93 * during resolves because the resolve operations only know about the
94 * resource and not the renderbuffer.
95 */
96 if (isl_format_srgb_to_linear(render_format) !=
97 isl_format_srgb_to_linear(format)) {
98 return false;
99 }
100
101 /* XXX: if (irb->mt->supports_fast_clear)
102 * see intel_miptree_create_for_dri_image()
103 */
104
105 if (!iris_is_color_fast_clear_compatible(ice, format, color))
106 return false;
107
108 return true;
109 }
110
111 static union isl_color_value
112 convert_fast_clear_color(struct iris_context *ice,
113 struct iris_resource *res,
114 enum isl_format render_format,
115 const union isl_color_value color)
116 {
117 union isl_color_value override_color = color;
118 struct pipe_resource *p_res = (void *) res;
119
120 const enum pipe_format format = p_res->format;
121 const struct util_format_description *desc =
122 util_format_description(format);
123 unsigned colormask = util_format_colormask(desc);
124
125 if (util_format_is_intensity(format) ||
126 util_format_is_luminance(format) ||
127 util_format_is_luminance_alpha(format)) {
128 override_color.u32[1] = override_color.u32[0];
129 override_color.u32[2] = override_color.u32[0];
130 if (util_format_is_intensity(format))
131 override_color.u32[3] = override_color.u32[0];
132 } else {
133 for (int chan = 0; chan < 3; chan++) {
134 if (!(colormask & (1 << chan)))
135 override_color.u32[chan] = 0;
136 }
137 }
138
139 if (util_format_is_unorm(format)) {
140 for (int i = 0; i < 4; i++)
141 override_color.f32[i] = CLAMP(override_color.f32[i], 0.0f, 1.0f);
142 } else if (util_format_is_snorm(format)) {
143 for (int i = 0; i < 4; i++)
144 override_color.f32[i] = CLAMP(override_color.f32[i], -1.0f, 1.0f);
145 } else if (util_format_is_pure_uint(format)) {
146 for (int i = 0; i < 4; i++) {
147 unsigned bits = util_format_get_component_bits(
148 format, UTIL_FORMAT_COLORSPACE_RGB, i);
149 if (bits < 32) {
150 uint32_t max = (1u << bits) - 1;
151 override_color.u32[i] = MIN2(override_color.u32[i], max);
152 }
153 }
154 } else if (util_format_is_pure_sint(format)) {
155 for (int i = 0; i < 4; i++) {
156 unsigned bits = util_format_get_component_bits(
157 format, UTIL_FORMAT_COLORSPACE_RGB, i);
158 if (bits < 32) {
159 int32_t max = (1 << (bits - 1)) - 1;
160 int32_t min = -(1 << (bits - 1));
161 override_color.i32[i] = CLAMP(override_color.i32[i], min, max);
162 }
163 }
164 } else if (format == PIPE_FORMAT_R11G11B10_FLOAT ||
165 format == PIPE_FORMAT_R9G9B9E5_FLOAT) {
166 /* these packed float formats only store unsigned values */
167 for (int i = 0; i < 4; i++)
168 override_color.f32[i] = MAX2(override_color.f32[i], 0.0f);
169 }
170
171 if (!(colormask & 1 << 3)) {
172 if (util_format_is_pure_integer(format))
173 override_color.u32[3] = 1;
174 else
175 override_color.f32[3] = 1.0f;
176 }
177
178 /* Handle linear to SRGB conversion */
179 if (isl_format_is_srgb(render_format)) {
180 for (int i = 0; i < 3; i++) {
181 override_color.f32[i] =
182 util_format_linear_to_srgb_float(override_color.f32[i]);
183 }
184 }
185
186 return override_color;
187 }
188
189 static void
190 fast_clear_color(struct iris_context *ice,
191 struct iris_resource *res,
192 unsigned level,
193 const struct pipe_box *box,
194 enum isl_format format,
195 union isl_color_value color,
196 enum blorp_batch_flags blorp_flags)
197 {
198 struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
199 struct pipe_resource *p_res = (void *) res;
200 const enum isl_aux_state aux_state =
201 iris_resource_get_aux_state(res, level, box->z);
202
203 color = convert_fast_clear_color(ice, res, format, color);
204
205 bool color_changed = !!memcmp(&res->aux.clear_color, &color,
206 sizeof(color));
207
208 if (color_changed) {
209 /* We decided that we are going to fast clear, and the color is
210 * changing. But if we have a predicate bit set, the predication
211 * affects whether we should clear or not, and if we shouldn't, we
212 * also shouldn't update the clear color.
213 *
214 * However, we can't simply predicate-update the clear color (the
215 * commands don't support that). And we would lose track of the
216 * color, preventing us from doing some optimizations later.
217 *
218 * Since changing the clear color when the predication bit is enabled
219 * is not something that should happen often, we stall on the CPU here
220 * to resolve the predication, and then proceed.
221 */
222 ice->vtbl.resolve_conditional_render(ice);
223 if (ice->state.predicate == IRIS_PREDICATE_STATE_DONT_RENDER)
224 return;
225
226 /* If we are clearing to a new clear value, we need to resolve fast
227 * clears from other levels/layers first, since we can't have different
228 * levels/layers with different fast clear colors.
229 */
230 for (unsigned res_lvl = 0; res_lvl < res->surf.levels; res_lvl++) {
231 const unsigned level_layers =
232 iris_get_num_logical_layers(res, res_lvl);
233 for (unsigned layer = 0; layer < level_layers; layer++) {
234 if (res_lvl == level &&
235 layer >= box->z &&
236 layer < box->z + box->depth) {
237 /* We're going to clear this layer anyway. Leave it alone. */
238 continue;
239 }
240
241 enum isl_aux_state aux_state =
242 iris_resource_get_aux_state(res, res_lvl, layer);
243
244 if (aux_state != ISL_AUX_STATE_CLEAR &&
245 aux_state != ISL_AUX_STATE_PARTIAL_CLEAR &&
246 aux_state != ISL_AUX_STATE_COMPRESSED_CLEAR) {
247 /* This slice doesn't have any fast-cleared bits. */
248 continue;
249 }
250
251 /* If we got here, then the level may have fast-clear bits that use
252 * the old clear value. We need to do a color resolve to get rid
253 * of their use of the clear color before we can change it.
254 * Fortunately, few applications ever change their clear color at
255 * different levels/layers, so this shouldn't happen often.
256 */
257 iris_resource_prepare_access(ice, batch, res,
258 res_lvl, 1, layer, 1,
259 res->aux.usage,
260 false);
261 perf_debug(&ice->dbg,
262 "Resolving resource (%p) level %d, layer %d: color changing from "
263 "(%0.2f, %0.2f, %0.2f, %0.2f) to "
264 "(%0.2f, %0.2f, %0.2f, %0.2f)\n",
265 res, res_lvl, layer,
266 res->aux.clear_color.f32[0],
267 res->aux.clear_color.f32[1],
268 res->aux.clear_color.f32[2],
269 res->aux.clear_color.f32[3],
270 color.f32[0], color.f32[1], color.f32[2], color.f32[3]);
271 }
272 }
273 }
274
275 iris_resource_set_clear_color(ice, res, color);
276
277 /* If the buffer is already in ISL_AUX_STATE_CLEAR, and the color hasn't
278 * changed, the clear is redundant and can be skipped.
279 */
280 if (!color_changed && aux_state == ISL_AUX_STATE_CLEAR)
281 return;
282
283 /* Ivybrigde PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
284 *
285 * "Any transition from any value in {Clear, Render, Resolve} to a
286 * different value in {Clear, Render, Resolve} requires end of pipe
287 * synchronization."
288 *
289 * In other words, fast clear ops are not properly synchronized with
290 * other drawing. We need to use a PIPE_CONTROL to ensure that the
291 * contents of the previous draw hit the render target before we resolve
292 * and again afterwards to ensure that the resolve is complete before we
293 * do any more regular drawing.
294 */
295 iris_emit_end_of_pipe_sync(batch,
296 "fast clear: pre-flush",
297 PIPE_CONTROL_RENDER_TARGET_FLUSH);
298
299 /* If we reach this point, we need to fast clear to change the state to
300 * ISL_AUX_STATE_CLEAR, or to update the fast clear color (or both).
301 */
302 blorp_flags |= color_changed ? 0 : BLORP_BATCH_NO_UPDATE_CLEAR_COLOR;
303
304 struct blorp_batch blorp_batch;
305 blorp_batch_init(&ice->blorp, &blorp_batch, batch, blorp_flags);
306
307 struct blorp_surf surf;
308 iris_blorp_surf_for_resource(&ice->vtbl, &surf, p_res, res->aux.usage,
309 level, true);
310
311 /* In newer gens (> 9), the hardware will do a linear -> sRGB conversion of
312 * the clear color during the fast clear, if the surface format is of sRGB
313 * type. We use the linear version of the surface format here to prevent
314 * that from happening, since we already do our own linear -> sRGB
315 * conversion in convert_fast_clear_color().
316 */
317 blorp_fast_clear(&blorp_batch, &surf, isl_format_srgb_to_linear(format),
318 level, box->z, box->depth,
319 box->x, box->y, box->x + box->width,
320 box->y + box->height);
321 blorp_batch_finish(&blorp_batch);
322 iris_emit_end_of_pipe_sync(batch,
323 "fast clear: post flush",
324 PIPE_CONTROL_RENDER_TARGET_FLUSH);
325
326 iris_resource_set_aux_state(ice, res, level, box->z,
327 box->depth, ISL_AUX_STATE_CLEAR);
328 ice->state.dirty |= IRIS_ALL_DIRTY_BINDINGS;
329 return;
330 }
331
332 static void
333 clear_color(struct iris_context *ice,
334 struct pipe_resource *p_res,
335 unsigned level,
336 const struct pipe_box *box,
337 bool render_condition_enabled,
338 enum isl_format format,
339 struct isl_swizzle swizzle,
340 union isl_color_value color)
341 {
342 struct iris_resource *res = (void *) p_res;
343
344 struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
345 const struct gen_device_info *devinfo = &batch->screen->devinfo;
346 enum blorp_batch_flags blorp_flags = 0;
347
348 if (render_condition_enabled) {
349 if (ice->state.predicate == IRIS_PREDICATE_STATE_DONT_RENDER)
350 return;
351
352 if (ice->state.predicate == IRIS_PREDICATE_STATE_USE_BIT)
353 blorp_flags |= BLORP_BATCH_PREDICATE_ENABLE;
354 }
355
356 if (p_res->target == PIPE_BUFFER)
357 util_range_add(&res->base, &res->valid_buffer_range, box->x, box->x + box->width);
358
359 iris_batch_maybe_flush(batch, 1500);
360
361 bool can_fast_clear = can_fast_clear_color(ice, p_res, level, box,
362 res->surf.format, format, color);
363 if (can_fast_clear) {
364 fast_clear_color(ice, res, level, box, format, color,
365 blorp_flags);
366 return;
367 }
368
369 bool color_write_disable[4] = { false, false, false, false };
370 enum isl_aux_usage aux_usage =
371 iris_resource_render_aux_usage(ice, res, format,
372 false, false);
373
374 iris_resource_prepare_render(ice, batch, res, level,
375 box->z, box->depth, aux_usage);
376
377 struct blorp_surf surf;
378 iris_blorp_surf_for_resource(&ice->vtbl, &surf, p_res, aux_usage, level,
379 true);
380
381 struct blorp_batch blorp_batch;
382 blorp_batch_init(&ice->blorp, &blorp_batch, batch, blorp_flags);
383
384 if (!isl_format_supports_rendering(devinfo, format) &&
385 isl_format_is_rgbx(format))
386 format = isl_format_rgbx_to_rgba(format);
387
388 blorp_clear(&blorp_batch, &surf, format, swizzle,
389 level, box->z, box->depth, box->x, box->y,
390 box->x + box->width, box->y + box->height,
391 color, color_write_disable);
392
393 blorp_batch_finish(&blorp_batch);
394 iris_flush_and_dirty_for_history(ice, batch, res,
395 PIPE_CONTROL_RENDER_TARGET_FLUSH,
396 "cache history: post color clear");
397
398 iris_resource_finish_render(ice, res, level,
399 box->z, box->depth, aux_usage);
400 }
401
402 static bool
403 can_fast_clear_depth(struct iris_context *ice,
404 struct iris_resource *res,
405 unsigned level,
406 const struct pipe_box *box,
407 float depth)
408 {
409 struct pipe_resource *p_res = (void *) res;
410
411 if (INTEL_DEBUG & DEBUG_NO_FAST_CLEAR)
412 return false;
413
414 /* Check for partial clears */
415 if (box->x > 0 || box->y > 0 ||
416 box->width < u_minify(p_res->width0, level) ||
417 box->height < u_minify(p_res->height0, level)) {
418 return false;
419 }
420
421 if (!(res->aux.has_hiz & (1 << level)))
422 return false;
423
424 return true;
425 }
426
427 static void
428 fast_clear_depth(struct iris_context *ice,
429 struct iris_resource *res,
430 unsigned level,
431 const struct pipe_box *box,
432 float depth)
433 {
434 struct pipe_resource *p_res = (void *) res;
435 struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
436
437 /* Quantize the clear value to what can be stored in the actual depth
438 * buffer. This makes the following check more accurate because it now
439 * checks if the actual depth bits will match. It also prevents us from
440 * getting a too-accurate depth value during depth testing or when sampling
441 * with HiZ enabled.
442 */
443 const unsigned nbits = p_res->format == PIPE_FORMAT_Z16_UNORM ? 16 : 24;
444 const uint32_t depth_max = (1 << nbits) - 1;
445 depth = p_res->format == PIPE_FORMAT_Z32_FLOAT ? depth :
446 (unsigned)(depth * depth_max) / (float)depth_max;
447
448 bool update_clear_depth = false;
449
450 /* If we're clearing to a new clear value, then we need to resolve any clear
451 * flags out of the HiZ buffer into the real depth buffer.
452 */
453 if (res->aux.clear_color.f32[0] != depth) {
454 /* We decided that we are going to fast clear, and the color is
455 * changing. But if we have a predicate bit set, the predication
456 * affects whether we should clear or not, and if we shouldn't, we
457 * also shouldn't update the clear color.
458 *
459 * However, we can't simply predicate-update the clear color (the
460 * commands don't support that). And we would lose track of the
461 * color, preventing us from doing some optimizations later.
462 *
463 * For depth clears, things are even more complicated, because here we
464 * resolve the other levels/layers if they have a different color than
465 * the current one. That resolve can be predicated, but we also set those
466 * layers as ISL_AUX_STATE_RESOLVED, and this can't be predicated.
467 * Keeping track of the aux state when predication is involved is just
468 * even more complex, so the easiest thing to do when the fast clear
469 * depth is changing is to stall on the CPU and resolve the predication.
470 */
471 ice->vtbl.resolve_conditional_render(ice);
472 if (ice->state.predicate == IRIS_PREDICATE_STATE_DONT_RENDER)
473 return;
474
475 for (unsigned res_level = 0; res_level < res->surf.levels; res_level++) {
476 if (!(res->aux.has_hiz & (1 << res_level)))
477 continue;
478
479 const unsigned level_layers =
480 iris_get_num_logical_layers(res, res_level);
481 for (unsigned layer = 0; layer < level_layers; layer++) {
482 if (res_level == level &&
483 layer >= box->z &&
484 layer < box->z + box->depth) {
485 /* We're going to clear this layer anyway. Leave it alone. */
486 continue;
487 }
488
489 enum isl_aux_state aux_state =
490 iris_resource_get_aux_state(res, res_level, layer);
491
492 if (aux_state != ISL_AUX_STATE_CLEAR &&
493 aux_state != ISL_AUX_STATE_COMPRESSED_CLEAR) {
494 /* This slice doesn't have any fast-cleared bits. */
495 continue;
496 }
497
498 /* If we got here, then the level may have fast-clear bits that
499 * use the old clear value. We need to do a depth resolve to get
500 * rid of their use of the clear value before we can change it.
501 * Fortunately, few applications ever change their depth clear
502 * value so this shouldn't happen often.
503 */
504 iris_hiz_exec(ice, batch, res, res_level, layer, 1,
505 ISL_AUX_OP_FULL_RESOLVE, false);
506 iris_resource_set_aux_state(ice, res, res_level, layer, 1,
507 ISL_AUX_STATE_RESOLVED);
508 }
509 }
510 const union isl_color_value clear_value = { .f32 = {depth, } };
511 iris_resource_set_clear_color(ice, res, clear_value);
512 update_clear_depth = true;
513 }
514
515 for (unsigned l = 0; l < box->depth; l++) {
516 enum isl_aux_state aux_state =
517 iris_resource_get_aux_state(res, level, box->z + l);
518 if (aux_state != ISL_AUX_STATE_CLEAR) {
519 iris_hiz_exec(ice, batch, res, level,
520 box->z + l, 1, ISL_AUX_OP_FAST_CLEAR,
521 update_clear_depth);
522 }
523 }
524
525 iris_resource_set_aux_state(ice, res, level, box->z, box->depth,
526 ISL_AUX_STATE_CLEAR);
527 ice->state.dirty |= IRIS_DIRTY_DEPTH_BUFFER;
528 }
529
530 static void
531 clear_depth_stencil(struct iris_context *ice,
532 struct pipe_resource *p_res,
533 unsigned level,
534 const struct pipe_box *box,
535 bool render_condition_enabled,
536 bool clear_depth,
537 bool clear_stencil,
538 float depth,
539 uint8_t stencil)
540 {
541 struct iris_resource *res = (void *) p_res;
542
543 struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
544 enum blorp_batch_flags blorp_flags = 0;
545
546 if (render_condition_enabled) {
547 if (ice->state.predicate == IRIS_PREDICATE_STATE_DONT_RENDER)
548 return;
549
550 if (ice->state.predicate == IRIS_PREDICATE_STATE_USE_BIT)
551 blorp_flags |= BLORP_BATCH_PREDICATE_ENABLE;
552 }
553
554 iris_batch_maybe_flush(batch, 1500);
555
556 struct iris_resource *z_res;
557 struct iris_resource *stencil_res;
558 struct blorp_surf z_surf;
559 struct blorp_surf stencil_surf;
560
561 iris_get_depth_stencil_resources(p_res, &z_res, &stencil_res);
562 if (z_res && clear_depth &&
563 can_fast_clear_depth(ice, z_res, level, box, depth)) {
564 fast_clear_depth(ice, z_res, level, box, depth);
565 iris_flush_and_dirty_for_history(ice, batch, res, 0,
566 "cache history: post fast Z clear");
567 clear_depth = false;
568 z_res = false;
569 }
570
571 /* At this point, we might have fast cleared the depth buffer. So if there's
572 * no stencil clear pending, return early.
573 */
574 if (!(clear_depth || clear_stencil)) {
575 return;
576 }
577
578 if (z_res) {
579 iris_resource_prepare_depth(ice, batch, z_res, level, box->z, box->depth);
580 iris_blorp_surf_for_resource(&ice->vtbl, &z_surf, &z_res->base,
581 z_res->aux.usage, level, true);
582 }
583
584 struct blorp_batch blorp_batch;
585 blorp_batch_init(&ice->blorp, &blorp_batch, batch, blorp_flags);
586
587 if (stencil_res) {
588 iris_blorp_surf_for_resource(&ice->vtbl, &stencil_surf,
589 &stencil_res->base, stencil_res->aux.usage,
590 level, true);
591 }
592
593 blorp_clear_depth_stencil(&blorp_batch, &z_surf, &stencil_surf,
594 level, box->z, box->depth,
595 box->x, box->y,
596 box->x + box->width,
597 box->y + box->height,
598 clear_depth && z_res, depth,
599 clear_stencil && stencil_res ? 0xff : 0, stencil);
600
601 blorp_batch_finish(&blorp_batch);
602 iris_flush_and_dirty_for_history(ice, batch, res, 0,
603 "cache history: post slow ZS clear");
604
605 if (z_res) {
606 iris_resource_finish_depth(ice, z_res, level,
607 box->z, box->depth, true);
608 }
609 }
610
611 /**
612 * The pipe->clear() driver hook.
613 *
614 * This clears buffers attached to the current draw framebuffer.
615 */
616 static void
617 iris_clear(struct pipe_context *ctx,
618 unsigned buffers,
619 const union pipe_color_union *p_color,
620 double depth,
621 unsigned stencil)
622 {
623 struct iris_context *ice = (void *) ctx;
624 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
625
626 assert(buffers != 0);
627
628 if (buffers & PIPE_CLEAR_DEPTHSTENCIL) {
629 struct pipe_surface *psurf = cso_fb->zsbuf;
630 struct pipe_box box = {
631 .width = cso_fb->width,
632 .height = cso_fb->height,
633 .depth = psurf->u.tex.last_layer - psurf->u.tex.first_layer + 1,
634 .z = psurf->u.tex.first_layer,
635 };
636
637 clear_depth_stencil(ice, psurf->texture, psurf->u.tex.level, &box, true,
638 buffers & PIPE_CLEAR_DEPTH,
639 buffers & PIPE_CLEAR_STENCIL,
640 depth, stencil);
641 }
642
643 if (buffers & PIPE_CLEAR_COLOR) {
644 /* pipe_color_union and isl_color_value are interchangeable */
645 union isl_color_value *color = (void *) p_color;
646
647 for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
648 if (buffers & (PIPE_CLEAR_COLOR0 << i)) {
649 struct pipe_surface *psurf = cso_fb->cbufs[i];
650 struct iris_surface *isurf = (void *) psurf;
651 struct pipe_box box = {
652 .width = cso_fb->width,
653 .height = cso_fb->height,
654 .depth = psurf->u.tex.last_layer - psurf->u.tex.first_layer + 1,
655 .z = psurf->u.tex.first_layer,
656 };
657
658 clear_color(ice, psurf->texture, psurf->u.tex.level, &box,
659 true, isurf->view.format, isurf->view.swizzle,
660 *color);
661 }
662 }
663 }
664 }
665
666 /**
667 * The pipe->clear_texture() driver hook.
668 *
669 * This clears the given texture resource.
670 */
671 static void
672 iris_clear_texture(struct pipe_context *ctx,
673 struct pipe_resource *p_res,
674 unsigned level,
675 const struct pipe_box *box,
676 const void *data)
677 {
678 struct iris_context *ice = (void *) ctx;
679 struct iris_screen *screen = (void *) ctx->screen;
680 const struct gen_device_info *devinfo = &screen->devinfo;
681
682 if (util_format_is_depth_or_stencil(p_res->format)) {
683 const struct util_format_description *fmt_desc =
684 util_format_description(p_res->format);
685
686 float depth = 0.0;
687 uint8_t stencil = 0;
688
689 if (fmt_desc->unpack_z_float)
690 fmt_desc->unpack_z_float(&depth, 0, data, 0, 1, 1);
691
692 if (fmt_desc->unpack_s_8uint)
693 fmt_desc->unpack_s_8uint(&stencil, 0, data, 0, 1, 1);
694
695 clear_depth_stencil(ice, p_res, level, box, true, true, true,
696 depth, stencil);
697 } else {
698 union isl_color_value color;
699 struct iris_resource *res = (void *) p_res;
700 enum isl_format format = res->surf.format;
701
702 if (!isl_format_supports_rendering(devinfo, format)) {
703 const struct isl_format_layout *fmtl = isl_format_get_layout(format);
704 // XXX: actually just get_copy_format_for_bpb from BLORP
705 // XXX: don't cut and paste this
706 switch (fmtl->bpb) {
707 case 8: format = ISL_FORMAT_R8_UINT; break;
708 case 16: format = ISL_FORMAT_R8G8_UINT; break;
709 case 24: format = ISL_FORMAT_R8G8B8_UINT; break;
710 case 32: format = ISL_FORMAT_R8G8B8A8_UINT; break;
711 case 48: format = ISL_FORMAT_R16G16B16_UINT; break;
712 case 64: format = ISL_FORMAT_R16G16B16A16_UINT; break;
713 case 96: format = ISL_FORMAT_R32G32B32_UINT; break;
714 case 128: format = ISL_FORMAT_R32G32B32A32_UINT; break;
715 default:
716 unreachable("Unknown format bpb");
717 }
718
719 /* No aux surfaces for non-renderable surfaces */
720 assert(res->aux.usage == ISL_AUX_USAGE_NONE);
721 }
722
723 isl_color_value_unpack(&color, format, data);
724
725 clear_color(ice, p_res, level, box, true, format,
726 ISL_SWIZZLE_IDENTITY, color);
727 }
728 }
729
730 /**
731 * The pipe->clear_render_target() driver hook.
732 *
733 * This clears the given render target surface.
734 */
735 static void
736 iris_clear_render_target(struct pipe_context *ctx,
737 struct pipe_surface *psurf,
738 const union pipe_color_union *p_color,
739 unsigned dst_x, unsigned dst_y,
740 unsigned width, unsigned height,
741 bool render_condition_enabled)
742 {
743 struct iris_context *ice = (void *) ctx;
744 struct iris_surface *isurf = (void *) psurf;
745 struct pipe_box box = {
746 .x = dst_x,
747 .y = dst_y,
748 .z = psurf->u.tex.first_layer,
749 .width = width,
750 .height = height,
751 .depth = psurf->u.tex.last_layer - psurf->u.tex.first_layer + 1
752 };
753
754 /* pipe_color_union and isl_color_value are interchangeable */
755 union isl_color_value *color = (void *) p_color;
756
757 clear_color(ice, psurf->texture, psurf->u.tex.level, &box,
758 render_condition_enabled,
759 isurf->view.format, isurf->view.swizzle, *color);
760 }
761
762 /**
763 * The pipe->clear_depth_stencil() driver hook.
764 *
765 * This clears the given depth/stencil surface.
766 */
767 static void
768 iris_clear_depth_stencil(struct pipe_context *ctx,
769 struct pipe_surface *psurf,
770 unsigned flags,
771 double depth,
772 unsigned stencil,
773 unsigned dst_x, unsigned dst_y,
774 unsigned width, unsigned height,
775 bool render_condition_enabled)
776 {
777 struct iris_context *ice = (void *) ctx;
778 struct pipe_box box = {
779 .x = dst_x,
780 .y = dst_y,
781 .z = psurf->u.tex.first_layer,
782 .width = width,
783 .height = height,
784 .depth = psurf->u.tex.last_layer - psurf->u.tex.first_layer + 1
785 };
786
787 assert(util_format_is_depth_or_stencil(psurf->texture->format));
788
789 clear_depth_stencil(ice, psurf->texture, psurf->u.tex.level, &box,
790 render_condition_enabled,
791 flags & PIPE_CLEAR_DEPTH, flags & PIPE_CLEAR_STENCIL,
792 depth, stencil);
793 }
794
795 void
796 iris_init_clear_functions(struct pipe_context *ctx)
797 {
798 ctx->clear = iris_clear;
799 ctx->clear_texture = iris_clear_texture;
800 ctx->clear_render_target = iris_clear_render_target;
801 ctx->clear_depth_stencil = iris_clear_depth_stencil;
802 }