iris: Support sRGB fast clears even if the colorspaces differ.
[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 struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
79 const struct gen_device_info *devinfo = &batch->screen->devinfo;
80
81 if (devinfo->gen > 9)
82 return false;
83
84 if (res->aux.usage == ISL_AUX_USAGE_NONE)
85 return false;
86
87 /* Surface state can only record one fast clear color value. Therefore
88 * unless different levels/layers agree on the color it can be used to
89 * represent only single level/layer. Here it will be reserved for the
90 * first slice (level 0, layer 0).
91 */
92 if (level > 0 || box->z > 0 || box->depth > 1)
93 return false;
94
95 /* Check for partial clear */
96 if (box->x > 0 || box->y > 0 ||
97 box->width < p_res->width0 ||
98 box->height < p_res->height0) {
99 return false;
100 }
101
102 /* We store clear colors as floats or uints as needed. If there are
103 * texture views in play, the formats will not properly be respected
104 * during resolves because the resolve operations only know about the
105 * resource and not the renderbuffer.
106 */
107 if (isl_format_srgb_to_linear(render_format) !=
108 isl_format_srgb_to_linear(format)) {
109 return false;
110 }
111
112 /* XXX: if (irb->mt->supports_fast_clear)
113 * see intel_miptree_create_for_dri_image()
114 */
115
116 if (!iris_is_color_fast_clear_compatible(ice, format, color))
117 return false;
118
119 return true;
120 }
121
122 static union isl_color_value
123 convert_fast_clear_color(struct iris_context *ice,
124 struct iris_resource *res,
125 enum isl_format render_format,
126 const union isl_color_value color)
127 {
128 union isl_color_value override_color = color;
129 struct pipe_resource *p_res = (void *) res;
130
131 const enum pipe_format format = p_res->format;
132 const struct util_format_description *desc =
133 util_format_description(format);
134 unsigned colormask = util_format_colormask(desc);
135
136 if (util_format_is_intensity(format) ||
137 util_format_is_luminance(format) ||
138 util_format_is_luminance_alpha(format)) {
139 override_color.u32[1] = override_color.u32[0];
140 override_color.u32[2] = override_color.u32[0];
141 if (util_format_is_intensity(format))
142 override_color.u32[3] = override_color.u32[0];
143 } else {
144 for (int chan = 0; chan < 3; chan++) {
145 if (!(colormask & (1 << chan)))
146 override_color.u32[chan] = 0;
147 }
148 }
149
150 if (util_format_is_unorm(format)) {
151 for (int i = 0; i < 4; i++)
152 override_color.f32[i] = CLAMP(override_color.f32[i], 0.0f, 1.0f);
153 } else if (util_format_is_snorm(format)) {
154 for (int i = 0; i < 4; i++)
155 override_color.f32[i] = CLAMP(override_color.f32[i], -1.0f, 1.0f);
156 } else if (util_format_is_pure_uint(format)) {
157 for (int i = 0; i < 4; i++) {
158 unsigned bits = util_format_get_component_bits(
159 format, UTIL_FORMAT_COLORSPACE_RGB, i);
160 if (bits < 32) {
161 uint32_t max = (1u << bits) - 1;
162 override_color.u32[i] = MIN2(override_color.u32[i], max);
163 }
164 }
165 } else if (util_format_is_pure_sint(format)) {
166 for (int i = 0; i < 4; i++) {
167 unsigned bits = util_format_get_component_bits(
168 format, UTIL_FORMAT_COLORSPACE_RGB, i);
169 if (bits < 32) {
170 int32_t max = (1 << (bits - 1)) - 1;
171 int32_t min = -(1 << (bits - 1));
172 override_color.i32[i] = CLAMP(override_color.i32[i], min, max);
173 }
174 }
175 } else if (format == PIPE_FORMAT_R11G11B10_FLOAT ||
176 format == PIPE_FORMAT_R9G9B9E5_FLOAT) {
177 /* these packed float formats only store unsigned values */
178 for (int i = 0; i < 4; i++)
179 override_color.f32[i] = MAX2(override_color.f32[i], 0.0f);
180 }
181
182 if (!(colormask & 1 << 3)) {
183 if (util_format_is_pure_integer(format))
184 override_color.u32[3] = 1;
185 else
186 override_color.f32[3] = 1.0f;
187 }
188
189 /* Handle linear to SRGB conversion */
190 if (isl_format_is_srgb(render_format)) {
191 for (int i = 0; i < 3; i++) {
192 override_color.f32[i] =
193 util_format_linear_to_srgb_float(override_color.f32[i]);
194 }
195 }
196
197 return override_color;
198 }
199
200 static void
201 fast_clear_color(struct iris_context *ice,
202 struct iris_resource *res,
203 unsigned level,
204 const struct pipe_box *box,
205 enum isl_format format,
206 union isl_color_value color,
207 enum blorp_batch_flags blorp_flags)
208 {
209 struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
210 struct pipe_resource *p_res = (void *) res;
211 const enum isl_aux_state aux_state =
212 iris_resource_get_aux_state(res, level, box->z);
213
214 color = convert_fast_clear_color(ice, res, format, color);
215
216 bool color_changed = !!memcmp(&res->aux.clear_color, &color,
217 sizeof(color));
218
219 if (color_changed) {
220 /* We decided that we are going to fast clear, and the color is
221 * changing. But if we have a predicate bit set, the predication
222 * affects whether we should clear or not, and if we shouldn't, we
223 * also shouldn't update the clear color.
224 *
225 * However, we can't simply predicate-update the clear color (the
226 * commands don't support that). And we would lose track of the
227 * color, preventing us from doing some optimizations later.
228 *
229 * Since changing the clear color when the predication bit is enabled
230 * is not something that should happen often, we stall on the CPU here
231 * to resolve the predication, and then proceed.
232 */
233 iris_resolve_conditional_render(ice);
234 if (ice->state.predicate == IRIS_PREDICATE_STATE_DONT_RENDER)
235 return;
236 }
237
238 iris_resource_set_clear_color(ice, res, color);
239
240 /* If the buffer is already in ISL_AUX_STATE_CLEAR, and the color hasn't
241 * changed, the clear is redundant and can be skipped.
242 */
243 if (!color_changed && aux_state == ISL_AUX_STATE_CLEAR)
244 return;
245
246 /* Ivybrigde PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
247 *
248 * "Any transition from any value in {Clear, Render, Resolve} to a
249 * different value in {Clear, Render, Resolve} requires end of pipe
250 * synchronization."
251 *
252 * In other words, fast clear ops are not properly synchronized with
253 * other drawing. We need to use a PIPE_CONTROL to ensure that the
254 * contents of the previous draw hit the render target before we resolve
255 * and again afterwards to ensure that the resolve is complete before we
256 * do any more regular drawing.
257 */
258 iris_emit_end_of_pipe_sync(batch, PIPE_CONTROL_RENDER_TARGET_FLUSH);
259
260 /* If we reach this point, we need to fast clear to change the state to
261 * ISL_AUX_STATE_CLEAR, or to update the fast clear color (or both).
262 */
263 blorp_flags |= color_changed ? 0 : BLORP_BATCH_NO_UPDATE_CLEAR_COLOR;
264
265 struct blorp_batch blorp_batch;
266 blorp_batch_init(&ice->blorp, &blorp_batch, batch, blorp_flags);
267
268 struct blorp_surf surf;
269 iris_blorp_surf_for_resource(&ice->vtbl, &surf, p_res, res->aux.usage,
270 level, true);
271
272 blorp_fast_clear(&blorp_batch, &surf, format,
273 level, box->z, box->depth,
274 box->x, box->y, box->x + box->width,
275 box->y + box->height);
276 blorp_batch_finish(&blorp_batch);
277 iris_emit_end_of_pipe_sync(batch, PIPE_CONTROL_RENDER_TARGET_FLUSH);
278
279 iris_resource_set_aux_state(ice, res, level, box->z,
280 box->depth, ISL_AUX_STATE_CLEAR);
281 ice->state.dirty |= IRIS_ALL_DIRTY_BINDINGS;
282 return;
283 }
284
285 static void
286 clear_color(struct iris_context *ice,
287 struct pipe_resource *p_res,
288 unsigned level,
289 const struct pipe_box *box,
290 bool render_condition_enabled,
291 enum isl_format format,
292 struct isl_swizzle swizzle,
293 union isl_color_value color)
294 {
295 struct iris_resource *res = (void *) p_res;
296
297 struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
298 const struct gen_device_info *devinfo = &batch->screen->devinfo;
299 enum blorp_batch_flags blorp_flags = 0;
300
301 if (render_condition_enabled) {
302 if (ice->state.predicate == IRIS_PREDICATE_STATE_DONT_RENDER)
303 return;
304
305 if (ice->state.predicate == IRIS_PREDICATE_STATE_USE_BIT)
306 blorp_flags |= BLORP_BATCH_PREDICATE_ENABLE;
307 }
308
309 if (p_res->target == PIPE_BUFFER)
310 util_range_add(&res->valid_buffer_range, box->x, box->x + box->width);
311
312 iris_batch_maybe_flush(batch, 1500);
313
314 bool can_fast_clear = can_fast_clear_color(ice, p_res, level, box,
315 res->surf.format, format, color);
316 if (can_fast_clear) {
317 fast_clear_color(ice, res, level, box, format, color,
318 blorp_flags);
319 return;
320 }
321
322 bool color_write_disable[4] = { false, false, false, false };
323 enum isl_aux_usage aux_usage =
324 iris_resource_render_aux_usage(ice, res, format,
325 false, false);
326
327 iris_resource_prepare_render(ice, batch, res, level,
328 box->z, box->depth, aux_usage);
329
330 struct blorp_surf surf;
331 iris_blorp_surf_for_resource(&ice->vtbl, &surf, p_res, aux_usage, level,
332 true);
333
334 struct blorp_batch blorp_batch;
335 blorp_batch_init(&ice->blorp, &blorp_batch, batch, blorp_flags);
336
337 if (!isl_format_supports_rendering(devinfo, format) &&
338 isl_format_is_rgbx(format))
339 format = isl_format_rgbx_to_rgba(format);
340
341 blorp_clear(&blorp_batch, &surf, format, swizzle,
342 level, box->z, box->depth, box->x, box->y,
343 box->x + box->width, box->y + box->height,
344 color, color_write_disable);
345
346 blorp_batch_finish(&blorp_batch);
347 iris_flush_and_dirty_for_history(ice, batch, res);
348
349 iris_resource_finish_render(ice, res, level,
350 box->z, box->depth, aux_usage);
351 }
352
353 static bool
354 can_fast_clear_depth(struct iris_context *ice,
355 struct iris_resource *res,
356 unsigned level,
357 const struct pipe_box *box,
358 float depth)
359 {
360 struct pipe_resource *p_res = (void *) res;
361
362 /* Check for partial clears */
363 if (box->x > 0 || box->y > 0 ||
364 box->width < u_minify(p_res->width0, level) ||
365 box->height < u_minify(p_res->height0, level)) {
366 return false;
367 }
368
369 if (!(res->aux.has_hiz & (1 << level)))
370 return false;
371
372 return true;
373 }
374
375 static void
376 fast_clear_depth(struct iris_context *ice,
377 struct iris_resource *res,
378 unsigned level,
379 const struct pipe_box *box,
380 float depth)
381 {
382 struct pipe_resource *p_res = (void *) res;
383 struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
384
385 /* Quantize the clear value to what can be stored in the actual depth
386 * buffer. This makes the following check more accurate because it now
387 * checks if the actual depth bits will match. It also prevents us from
388 * getting a too-accurate depth value during depth testing or when sampling
389 * with HiZ enabled.
390 */
391 const unsigned nbits = p_res->format == PIPE_FORMAT_Z16_UNORM ? 16 : 24;
392 const uint32_t depth_max = (1 << nbits) - 1;
393 depth = p_res->format == PIPE_FORMAT_Z32_FLOAT ? depth :
394 (unsigned)(depth * depth_max) / (float)depth_max;
395
396 bool update_clear_depth = false;
397
398 /* If we're clearing to a new clear value, then we need to resolve any clear
399 * flags out of the HiZ buffer into the real depth buffer.
400 */
401 if (res->aux.clear_color.f32[0] != depth) {
402 /* We decided that we are going to fast clear, and the color is
403 * changing. But if we have a predicate bit set, the predication
404 * affects whether we should clear or not, and if we shouldn't, we
405 * also shouldn't update the clear color.
406 *
407 * However, we can't simply predicate-update the clear color (the
408 * commands don't support that). And we would lose track of the
409 * color, preventing us from doing some optimizations later.
410 *
411 * For depth clears, things are even more complicated, because here we
412 * resolve the other levels/layers if they have a different color than
413 * the current one. That resolve can be predicated, but we also set those
414 * layers as ISL_AUX_STATE_RESOLVED, and this can't be predicated.
415 * Keeping track of the aux state when predication is involved is just
416 * even more complex, so the easiest thing to do when the fast clear
417 * depth is changing is to stall on the CPU and resolve the predication.
418 */
419 iris_resolve_conditional_render(ice);
420 if (ice->state.predicate == IRIS_PREDICATE_STATE_DONT_RENDER)
421 return;
422
423 for (unsigned res_level = 0; res_level < res->surf.levels; res_level++) {
424 if (!(res->aux.has_hiz & (1 << res_level)))
425 continue;
426
427 const unsigned level_layers =
428 iris_get_num_logical_layers(res, res_level);
429 for (unsigned layer = 0; layer < level_layers; layer++) {
430 if (res_level == level &&
431 layer >= box->z &&
432 layer < box->z + box->depth) {
433 /* We're going to clear this layer anyway. Leave it alone. */
434 continue;
435 }
436
437 enum isl_aux_state aux_state =
438 iris_resource_get_aux_state(res, res_level, layer);
439
440 if (aux_state != ISL_AUX_STATE_CLEAR &&
441 aux_state != ISL_AUX_STATE_COMPRESSED_CLEAR) {
442 /* This slice doesn't have any fast-cleared bits. */
443 continue;
444 }
445
446 /* If we got here, then the level may have fast-clear bits that
447 * use the old clear value. We need to do a depth resolve to get
448 * rid of their use of the clear value before we can change it.
449 * Fortunately, few applications ever change their depth clear
450 * value so this shouldn't happen often.
451 */
452 iris_hiz_exec(ice, batch, res, res_level, layer, 1,
453 ISL_AUX_OP_FULL_RESOLVE, false);
454 iris_resource_set_aux_state(ice, res, res_level, layer, 1,
455 ISL_AUX_STATE_RESOLVED);
456 }
457 }
458 const union isl_color_value clear_value = { .f32 = {depth, } };
459 iris_resource_set_clear_color(ice, res, clear_value);
460 update_clear_depth = true;
461 }
462
463 for (unsigned l = 0; l < box->depth; l++) {
464 enum isl_aux_state aux_state =
465 iris_resource_get_aux_state(res, level, box->z + l);
466 if (aux_state != ISL_AUX_STATE_CLEAR) {
467 iris_hiz_exec(ice, batch, res, level,
468 box->z + l, 1, ISL_AUX_OP_FAST_CLEAR,
469 update_clear_depth);
470 }
471 }
472
473 iris_resource_set_aux_state(ice, res, level, box->z, box->depth,
474 ISL_AUX_STATE_CLEAR);
475 ice->state.dirty |= IRIS_DIRTY_DEPTH_BUFFER;
476 }
477
478 static void
479 clear_depth_stencil(struct iris_context *ice,
480 struct pipe_resource *p_res,
481 unsigned level,
482 const struct pipe_box *box,
483 bool render_condition_enabled,
484 bool clear_depth,
485 bool clear_stencil,
486 float depth,
487 uint8_t stencil)
488 {
489 struct iris_resource *res = (void *) p_res;
490
491 struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
492 enum blorp_batch_flags blorp_flags = 0;
493
494 if (render_condition_enabled) {
495 if (ice->state.predicate == IRIS_PREDICATE_STATE_DONT_RENDER)
496 return;
497
498 if (ice->state.predicate == IRIS_PREDICATE_STATE_USE_BIT)
499 blorp_flags |= BLORP_BATCH_PREDICATE_ENABLE;
500 }
501
502 iris_batch_maybe_flush(batch, 1500);
503
504 struct iris_resource *z_res;
505 struct iris_resource *stencil_res;
506 struct blorp_surf z_surf;
507 struct blorp_surf stencil_surf;
508
509 iris_get_depth_stencil_resources(p_res, &z_res, &stencil_res);
510 if (z_res && clear_depth &&
511 can_fast_clear_depth(ice, z_res, level, box, depth)) {
512 fast_clear_depth(ice, z_res, level, box, depth);
513 iris_flush_and_dirty_for_history(ice, batch, res);
514 clear_depth = false;
515 z_res = false;
516 }
517
518 /* At this point, we might have fast cleared the depth buffer. So if there's
519 * no stencil clear pending, return early.
520 */
521 if (!(clear_depth || clear_stencil)) {
522 return;
523 }
524
525 if (z_res) {
526 iris_resource_prepare_depth(ice, batch, z_res, level, box->z, box->depth);
527 iris_blorp_surf_for_resource(&ice->vtbl, &z_surf, &z_res->base,
528 z_res->aux.usage, level, true);
529 }
530
531 struct blorp_batch blorp_batch;
532 blorp_batch_init(&ice->blorp, &blorp_batch, batch, blorp_flags);
533
534 if (stencil_res) {
535 iris_blorp_surf_for_resource(&ice->vtbl, &stencil_surf,
536 &stencil_res->base, stencil_res->aux.usage,
537 level, true);
538 }
539
540 blorp_clear_depth_stencil(&blorp_batch, &z_surf, &stencil_surf,
541 level, box->z, box->depth,
542 box->x, box->y,
543 box->x + box->width,
544 box->y + box->height,
545 clear_depth && z_res, depth,
546 clear_stencil && stencil_res ? 0xff : 0, stencil);
547
548 blorp_batch_finish(&blorp_batch);
549 iris_flush_and_dirty_for_history(ice, batch, res);
550
551 if (z_res) {
552 iris_resource_finish_depth(ice, z_res, level,
553 box->z, box->depth, true);
554 }
555 }
556
557 /**
558 * The pipe->clear() driver hook.
559 *
560 * This clears buffers attached to the current draw framebuffer.
561 */
562 static void
563 iris_clear(struct pipe_context *ctx,
564 unsigned buffers,
565 const union pipe_color_union *p_color,
566 double depth,
567 unsigned stencil)
568 {
569 struct iris_context *ice = (void *) ctx;
570 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
571
572 assert(buffers != 0);
573
574 if (buffers & PIPE_CLEAR_DEPTHSTENCIL) {
575 struct pipe_surface *psurf = cso_fb->zsbuf;
576 struct pipe_box box = {
577 .width = cso_fb->width,
578 .height = cso_fb->height,
579 .depth = psurf->u.tex.last_layer - psurf->u.tex.first_layer + 1,
580 .z = psurf->u.tex.first_layer,
581 };
582
583 clear_depth_stencil(ice, psurf->texture, psurf->u.tex.level, &box, true,
584 buffers & PIPE_CLEAR_DEPTH,
585 buffers & PIPE_CLEAR_STENCIL,
586 depth, stencil);
587 }
588
589 if (buffers & PIPE_CLEAR_COLOR) {
590 /* pipe_color_union and isl_color_value are interchangeable */
591 union isl_color_value *color = (void *) p_color;
592
593 for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
594 if (buffers & (PIPE_CLEAR_COLOR0 << i)) {
595 struct pipe_surface *psurf = cso_fb->cbufs[i];
596 struct iris_surface *isurf = (void *) psurf;
597 struct pipe_box box = {
598 .width = cso_fb->width,
599 .height = cso_fb->height,
600 .depth = psurf->u.tex.last_layer - psurf->u.tex.first_layer + 1,
601 .z = psurf->u.tex.first_layer,
602 };
603
604 clear_color(ice, psurf->texture, psurf->u.tex.level, &box,
605 true, isurf->view.format, isurf->view.swizzle,
606 *color);
607 }
608 }
609 }
610 }
611
612 /**
613 * The pipe->clear_texture() driver hook.
614 *
615 * This clears the given texture resource.
616 */
617 static void
618 iris_clear_texture(struct pipe_context *ctx,
619 struct pipe_resource *p_res,
620 unsigned level,
621 const struct pipe_box *box,
622 const void *data)
623 {
624 struct iris_context *ice = (void *) ctx;
625 struct iris_screen *screen = (void *) ctx->screen;
626 const struct gen_device_info *devinfo = &screen->devinfo;
627
628 if (util_format_is_depth_or_stencil(p_res->format)) {
629 const struct util_format_description *fmt_desc =
630 util_format_description(p_res->format);
631
632 float depth = 0.0;
633 uint8_t stencil = 0;
634
635 if (fmt_desc->unpack_z_float)
636 fmt_desc->unpack_z_float(&depth, 0, data, 0, 1, 1);
637
638 if (fmt_desc->unpack_s_8uint)
639 fmt_desc->unpack_s_8uint(&stencil, 0, data, 0, 1, 1);
640
641 clear_depth_stencil(ice, p_res, level, box, true, true, true,
642 depth, stencil);
643 } else {
644 union isl_color_value color;
645 struct iris_resource *res = (void *) p_res;
646 enum isl_format format = res->surf.format;
647
648 if (!isl_format_supports_rendering(devinfo, format)) {
649 const struct isl_format_layout *fmtl = isl_format_get_layout(format);
650 // XXX: actually just get_copy_format_for_bpb from BLORP
651 // XXX: don't cut and paste this
652 switch (fmtl->bpb) {
653 case 8: format = ISL_FORMAT_R8_UINT; break;
654 case 16: format = ISL_FORMAT_R8G8_UINT; break;
655 case 24: format = ISL_FORMAT_R8G8B8_UINT; break;
656 case 32: format = ISL_FORMAT_R8G8B8A8_UINT; break;
657 case 48: format = ISL_FORMAT_R16G16B16_UINT; break;
658 case 64: format = ISL_FORMAT_R16G16B16A16_UINT; break;
659 case 96: format = ISL_FORMAT_R32G32B32_UINT; break;
660 case 128: format = ISL_FORMAT_R32G32B32A32_UINT; break;
661 default:
662 unreachable("Unknown format bpb");
663 }
664
665 /* No aux surfaces for non-renderable surfaces */
666 assert(res->aux.usage == ISL_AUX_USAGE_NONE);
667 }
668
669 isl_color_value_unpack(&color, format, data);
670
671 clear_color(ice, p_res, level, box, true, format,
672 ISL_SWIZZLE_IDENTITY, color);
673 }
674 }
675
676 /**
677 * The pipe->clear_render_target() driver hook.
678 *
679 * This clears the given render target surface.
680 */
681 static void
682 iris_clear_render_target(struct pipe_context *ctx,
683 struct pipe_surface *psurf,
684 const union pipe_color_union *p_color,
685 unsigned dst_x, unsigned dst_y,
686 unsigned width, unsigned height,
687 bool render_condition_enabled)
688 {
689 struct iris_context *ice = (void *) ctx;
690 struct iris_surface *isurf = (void *) psurf;
691 struct pipe_box box = {
692 .x = dst_x,
693 .y = dst_y,
694 .z = psurf->u.tex.first_layer,
695 .width = width,
696 .height = height,
697 .depth = psurf->u.tex.last_layer - psurf->u.tex.first_layer + 1
698 };
699
700 /* pipe_color_union and isl_color_value are interchangeable */
701 union isl_color_value *color = (void *) p_color;
702
703 clear_color(ice, psurf->texture, psurf->u.tex.level, &box,
704 render_condition_enabled,
705 isurf->view.format, isurf->view.swizzle, *color);
706 }
707
708 /**
709 * The pipe->clear_depth_stencil() driver hook.
710 *
711 * This clears the given depth/stencil surface.
712 */
713 static void
714 iris_clear_depth_stencil(struct pipe_context *ctx,
715 struct pipe_surface *psurf,
716 unsigned flags,
717 double depth,
718 unsigned stencil,
719 unsigned dst_x, unsigned dst_y,
720 unsigned width, unsigned height,
721 bool render_condition_enabled)
722 {
723 struct iris_context *ice = (void *) ctx;
724 struct pipe_box box = {
725 .x = dst_x,
726 .y = dst_y,
727 .z = psurf->u.tex.first_layer,
728 .width = width,
729 .height = height,
730 .depth = psurf->u.tex.last_layer - psurf->u.tex.first_layer + 1
731 };
732
733 assert(util_format_is_depth_or_stencil(psurf->texture->format));
734
735 clear_depth_stencil(ice, psurf->texture, psurf->u.tex.level, &box,
736 render_condition_enabled,
737 flags & PIPE_CLEAR_DEPTH, flags & PIPE_CLEAR_STENCIL,
738 depth, stencil);
739 }
740
741 void
742 iris_init_clear_functions(struct pipe_context *ctx)
743 {
744 ctx->clear = iris_clear;
745 ctx->clear_texture = iris_clear_texture;
746 ctx->clear_render_target = iris_clear_render_target;
747 ctx->clear_depth_stencil = iris_clear_depth_stencil;
748 }