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