• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
37 static bool
iris_is_color_fast_clear_compatible(struct iris_context * ice,enum isl_format format,const union isl_color_value color)38 iris_is_color_fast_clear_compatible(struct iris_context *ice,
39                                     enum isl_format format,
40                                     const union isl_color_value color)
41 {
42    struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
43    const struct intel_device_info *devinfo = batch->screen->devinfo;
44 
45    if (isl_format_has_int_channel(format)) {
46       perf_debug(&ice->dbg, "Integer fast clear not enabled for %s\n",
47                  isl_format_get_name(format));
48       return false;
49    }
50 
51    for (int i = 0; i < 4; i++) {
52       if (!isl_format_has_color_component(format, i)) {
53          continue;
54       }
55 
56       if (devinfo->ver < 9 &&
57           color.f32[i] != 0.0f && color.f32[i] != 1.0f) {
58          return false;
59       }
60    }
61 
62    return true;
63 }
64 
65 static bool
can_fast_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 render_format,union isl_color_value color)66 can_fast_clear_color(struct iris_context *ice,
67                      struct pipe_resource *p_res,
68                      unsigned level,
69                      const struct pipe_box *box,
70                      bool render_condition_enabled,
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 < u_minify(p_res->width0, level) ||
85        box->height < u_minify(p_res->height0, level)) {
86       return false;
87    }
88 
89    /* Avoid conditional fast clears to maintain correct tracking of the aux
90     * state (see iris_resource_finish_write for more info). Note that partial
91     * fast clears (if they existed) would not pose a problem with conditional
92     * rendering.
93     */
94    if (render_condition_enabled &&
95        ice->state.predicate == IRIS_PREDICATE_STATE_USE_BIT) {
96       return false;
97    }
98 
99    /* Disable sRGB fast-clears for non-0/1 color values. For texturing and
100     * draw calls, HW expects the clear color to be in two different color
101     * spaces after sRGB fast-clears - sRGB in the former and linear in the
102     * latter. By limiting the allowable values to 0/1, both color space
103     * requirements are satisfied.
104     */
105    if (isl_format_is_srgb(render_format) &&
106        !isl_color_value_is_zero_one(color, render_format)) {
107       return false;
108    }
109 
110    /* We store clear colors as floats or uints as needed.  If there are
111     * texture views in play, the formats will not properly be respected
112     * during resolves because the resolve operations only know about the
113     * resource and not the renderbuffer.
114     */
115    if (!iris_render_formats_color_compatible(render_format, res->surf.format,
116                                              color, false)) {
117       return false;
118    }
119 
120    if (!iris_is_color_fast_clear_compatible(ice, res->surf.format, color))
121       return false;
122 
123    /* The RENDER_SURFACE_STATE page for TGL says:
124     *
125     *   For an 8 bpp surface with NUM_MULTISAMPLES = 1, Surface Width not
126     *   multiple of 64 pixels and more than 1 mip level in the view, Fast Clear
127     *   is not supported when AUX_CCS_E is set in this field.
128     *
129     * The granularity of a fast-clear is one CCS element. For an 8 bpp primary
130     * surface, this maps to 32px x 4rows. Due to the surface layout parameters,
131     * if LOD0's width isn't a multiple of 64px, LOD1 and LOD2+ will share CCS
132     * elements. Assuming LOD2 exists, don't fast-clear any level above LOD0
133     * to avoid stomping on other LODs.
134     */
135    if (level > 0 && util_format_get_blocksizebits(p_res->format) == 8 &&
136        p_res->width0 % 64) {
137       return false;
138    }
139 
140    /* Wa_18020603990 - slow clear surfaces up to 256x256, 32bpp. */
141    const struct intel_device_info *devinfo =
142       ((struct iris_screen *)ice->ctx.screen)->devinfo;
143    if (intel_needs_workaround(devinfo, 18020603990)) {
144       if (isl_format_get_layout(res->surf.format)->bpb <= 32 &&
145           res->surf.logical_level0_px.w <= 256 &&
146           res->surf.logical_level0_px.h <= 256)
147          return false;
148    }
149 
150    return true;
151 }
152 
153 static union isl_color_value
convert_clear_color(enum pipe_format format,const union pipe_color_union * color)154 convert_clear_color(enum pipe_format format,
155                     const union pipe_color_union *color)
156 {
157    uint32_t pixel[4];
158    util_format_pack_rgba(format, pixel, color, 1);
159 
160    union isl_color_value converted_color;
161    util_format_unpack_rgba(format, &converted_color, pixel, 1);
162 
163    /* The converted clear color has channels that are:
164     *   - clamped
165     *   - quantized
166     *   - filled with 0/1 if missing from the format
167     *   - swizzled for luminance and intensity formats
168     */
169    return converted_color;
170 }
171 
172 static void
fast_clear_color(struct iris_context * ice,struct iris_resource * res,unsigned level,const struct pipe_box * box,union isl_color_value color)173 fast_clear_color(struct iris_context *ice,
174                  struct iris_resource *res,
175                  unsigned level,
176                  const struct pipe_box *box,
177                  union isl_color_value color)
178 {
179    struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
180    const struct intel_device_info *devinfo = batch->screen->devinfo;
181    struct pipe_resource *p_res = (void *) res;
182 
183    bool color_changed = res->aux.clear_color_unknown ||
184       memcmp(&res->aux.clear_color, &color, sizeof(color)) != 0;
185 
186    if (color_changed) {
187       /* If we are clearing to a new clear value, we need to resolve fast
188        * clears from other levels/layers first, since we can't have different
189        * levels/layers with different fast clear colors.
190        */
191       for (unsigned res_lvl = 0; res_lvl < res->surf.levels; res_lvl++) {
192          const unsigned level_layers =
193             iris_get_num_logical_layers(res, res_lvl);
194          for (unsigned layer = 0; layer < level_layers; layer++) {
195             if (res_lvl == level &&
196                 layer >= box->z &&
197                 layer < box->z + box->depth) {
198                /* We're going to clear this layer anyway.  Leave it alone. */
199                continue;
200             }
201 
202             enum isl_aux_state aux_state =
203                iris_resource_get_aux_state(res, res_lvl, layer);
204 
205             if (aux_state != ISL_AUX_STATE_CLEAR &&
206                 aux_state != ISL_AUX_STATE_PARTIAL_CLEAR &&
207                 aux_state != ISL_AUX_STATE_COMPRESSED_CLEAR) {
208                /* This slice doesn't have any fast-cleared bits. */
209                continue;
210             }
211 
212             /* If we got here, then the level may have fast-clear bits that use
213              * the old clear value.  We need to do a color resolve to get rid
214              * of their use of the clear color before we can change it.
215              * Fortunately, few applications ever change their clear color at
216              * different levels/layers, so this shouldn't happen often.
217              */
218             iris_resource_prepare_access(ice, res,
219                                          res_lvl, 1, layer, 1,
220                                          res->aux.usage,
221                                          false);
222             if (res->aux.clear_color_unknown) {
223                perf_debug(&ice->dbg,
224                           "Resolving resource (%p) level %d, layer %d: color changing from "
225                           "(unknown) to (%0.2f, %0.2f, %0.2f, %0.2f)\n",
226                           res, res_lvl, layer,
227                           color.f32[0], color.f32[1], color.f32[2], color.f32[3]);
228             } else {
229                perf_debug(&ice->dbg,
230                           "Resolving resource (%p) level %d, layer %d: color changing from "
231                           "(%0.2f, %0.2f, %0.2f, %0.2f) to "
232                           "(%0.2f, %0.2f, %0.2f, %0.2f)\n",
233                           res, res_lvl, layer,
234                           res->aux.clear_color.f32[0],
235                           res->aux.clear_color.f32[1],
236                           res->aux.clear_color.f32[2],
237                           res->aux.clear_color.f32[3],
238                           color.f32[0], color.f32[1], color.f32[2], color.f32[3]);
239             }
240          }
241       }
242    }
243 
244    iris_resource_set_clear_color(ice, res, color);
245 
246    /* If the buffer is already in ISL_AUX_STATE_CLEAR, and the color hasn't
247     * changed, the clear is redundant and can be skipped.
248     */
249    const enum isl_aux_state aux_state =
250       iris_resource_get_aux_state(res, level, box->z);
251    if (!color_changed && box->depth == 1 && aux_state == ISL_AUX_STATE_CLEAR)
252       return;
253 
254    /* Ivybridge PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
255     *
256     *    "Any transition from any value in {Clear, Render, Resolve} to a
257     *    different value in {Clear, Render, Resolve} requires end of pipe
258     *    synchronization."
259     *
260     * In other words, fast clear ops are not properly synchronized with
261     * other drawing.  We need to use a PIPE_CONTROL to ensure that the
262     * contents of the previous draw hit the render target before we resolve
263     * and again afterwards to ensure that the resolve is complete before we
264     * do any more regular drawing.
265     */
266    iris_emit_end_of_pipe_sync(batch, "fast clear: pre-flush",
267       PIPE_CONTROL_RENDER_TARGET_FLUSH |
268       PIPE_CONTROL_TILE_CACHE_FLUSH |
269       (devinfo->verx10 == 120 ? PIPE_CONTROL_DEPTH_STALL : 0) |
270       (devinfo->verx10 == 125 ? PIPE_CONTROL_FLUSH_HDC |
271                                 PIPE_CONTROL_DATA_CACHE_FLUSH : 0) |
272       PIPE_CONTROL_PSS_STALL_SYNC);
273 
274    /* From the ICL PRMs, Volume 9: Render Engine, State Caching :
275     *
276     *    "Any values referenced by pointers within the RENDER_SURFACE_STATE or
277     *     SAMPLER_STATE (e.g. Clear Color Pointer, Border Color or Indirect
278     *     State Pointer) are considered to be part of that state and any
279     *     changes to these referenced values requires an invalidation of the
280     *     L1 state cache to ensure the new values are being used as part of
281     *     the state. In the case of surface data pointed to by the Surface
282     *     Base Address in RENDER SURFACE STATE, the Texture Cache must be
283     *     invalidated if the surface data changes."
284     *
285     * and From the Render Target Fast Clear section,
286     *
287     *   "HwManaged FastClear allows SW to store FastClearValue in separate
288     *   graphics allocation, instead of keeping them in RENDER_SURFACE_STATE.
289     *   This behavior can be enabled by setting ClearValueAddressEnable in
290     *   RENDER_SURFACE_STATE.
291     *
292     *    Proper sequence of commands is as follows:
293     *
294     *       1. Storing clear color to allocation.
295     *       2. Ensuring that step 1. is finished and visible for TextureCache.
296     *       3. Performing FastClear.
297     *
298     *    Step 2. is required on products with ClearColorConversion feature.
299     *    This feature is enabled by setting ClearColorConversionEnable. This
300     *    causes HW to read stored color from ClearColorAllocation and write
301     *    back with the native format or RenderTarget - and clear color needs
302     *    to be present and visible. Reading is done from TextureCache, writing
303     *    is done to RenderCache."
304     *
305     * We're going to change the clear color. Invalidate the texture cache now
306     * to ensure the clear color conversion feature works properly. Although
307     * the docs seem to require invalidating the texture cache after updating
308     * the clear color allocation, we can do this beforehand so long as we
309     * ensure:
310     *
311     *    1. Step 1 is complete before the texture cache is accessed in step 3.
312     *    2. We don't access the texture cache between invalidation and step 3.
313     *
314     * The second requirement is satisfied because we'll be performing step 1
315     * and 3 right after invalidating. The first is satisfied because BLORP
316     * updates the clear color before performing the fast clear and it performs
317     * the synchronizations suggested by the Render Target Fast Clear section
318     * (not quoted here) to ensure its completion.
319     *
320     * While we're here, also invalidate the state cache as suggested.
321     *
322     * Due to a corruption reported in
323     * https://gitlab.freedesktop.org/mesa/mesa/-/issues/8853#note_2015707 when
324     * the clear color doesn´t change, we invalidate both caches always.
325     */
326    if (devinfo->ver >= 11) {
327       iris_emit_pipe_control_flush(batch, "fast clear: pre-flush",
328          PIPE_CONTROL_STATE_CACHE_INVALIDATE |
329          PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
330    }
331 
332    iris_batch_sync_region_start(batch);
333 
334    /* If we reach this point, we need to fast clear to change the state to
335     * ISL_AUX_STATE_CLEAR, or to update the fast clear color (or both).
336     */
337    enum blorp_batch_flags blorp_flags = 0;
338    blorp_flags |= color_changed ? 0 : BLORP_BATCH_NO_UPDATE_CLEAR_COLOR;
339 
340    struct blorp_batch blorp_batch;
341    blorp_batch_init(&ice->blorp, &blorp_batch, batch, blorp_flags);
342 
343    struct blorp_surf surf;
344    iris_blorp_surf_for_resource(&batch->screen->isl_dev, &surf,
345                                 p_res, res->aux.usage, level, true);
346 
347    blorp_fast_clear(&blorp_batch, &surf, res->surf.format,
348                     ISL_SWIZZLE_IDENTITY,
349                     level, box->z, box->depth,
350                     box->x, box->y, box->x + box->width,
351                     box->y + box->height);
352    blorp_batch_finish(&blorp_batch);
353    iris_emit_end_of_pipe_sync(batch,
354                               "fast clear: post flush",
355                               PIPE_CONTROL_RENDER_TARGET_FLUSH |
356                               (devinfo->verx10 == 120 ?
357                                  PIPE_CONTROL_TILE_CACHE_FLUSH |
358                                  PIPE_CONTROL_DEPTH_STALL : 0) |
359                               PIPE_CONTROL_PSS_STALL_SYNC);
360    iris_batch_sync_region_end(batch);
361 
362    iris_resource_set_aux_state(ice, res, level, box->z,
363                                box->depth, ISL_AUX_STATE_CLEAR);
364    ice->state.dirty |= IRIS_DIRTY_RENDER_BUFFER;
365    ice->state.stage_dirty |= IRIS_ALL_STAGE_DIRTY_BINDINGS;
366    return;
367 }
368 
369 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)370 clear_color(struct iris_context *ice,
371             struct pipe_resource *p_res,
372             unsigned level,
373             const struct pipe_box *box,
374             bool render_condition_enabled,
375             enum isl_format format,
376             struct isl_swizzle swizzle,
377             union isl_color_value color)
378 {
379    struct iris_resource *res = (void *) p_res;
380 
381    struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
382    const struct intel_device_info *devinfo = batch->screen->devinfo;
383    enum blorp_batch_flags blorp_flags = iris_blorp_flags_for_batch(batch);
384 
385    if (render_condition_enabled) {
386       if (ice->state.predicate == IRIS_PREDICATE_STATE_DONT_RENDER)
387          return;
388 
389       if (ice->state.predicate == IRIS_PREDICATE_STATE_USE_BIT)
390          blorp_flags |= BLORP_BATCH_PREDICATE_ENABLE;
391    }
392 
393    if (p_res->target == PIPE_BUFFER)
394       util_range_add(&res->base.b, &res->valid_buffer_range, box->x, box->x + box->width);
395 
396    iris_batch_maybe_flush(batch, 1500);
397 
398    bool can_fast_clear = can_fast_clear_color(ice, p_res, level, box,
399                                               render_condition_enabled,
400                                               format, color);
401    if (can_fast_clear) {
402       fast_clear_color(ice, res, level, box, color);
403       return;
404    }
405 
406    enum isl_aux_usage aux_usage =
407       iris_resource_render_aux_usage(ice, res, format, level, false);
408 
409    iris_resource_prepare_render(ice, res, format, level, box->z, box->depth,
410                                 aux_usage);
411    iris_emit_buffer_barrier_for(batch, res->bo, IRIS_DOMAIN_RENDER_WRITE);
412 
413    struct blorp_surf surf;
414    iris_blorp_surf_for_resource(&batch->screen->isl_dev, &surf,
415                                 p_res, aux_usage, level, true);
416 
417    iris_batch_sync_region_start(batch);
418 
419    struct blorp_batch blorp_batch;
420    blorp_batch_init(&ice->blorp, &blorp_batch, batch, blorp_flags);
421 
422    if (!isl_format_supports_rendering(devinfo, format) &&
423        isl_format_is_rgbx(format))
424       format = isl_format_rgbx_to_rgba(format);
425 
426    blorp_clear(&blorp_batch, &surf, format, swizzle,
427                level, box->z, box->depth, box->x, box->y,
428                box->x + box->width, box->y + box->height,
429                color, 0 /* color_write_disable */);
430 
431    blorp_batch_finish(&blorp_batch);
432    iris_batch_sync_region_end(batch);
433 
434    iris_dirty_for_history(ice, res);
435 
436    iris_resource_finish_render(ice, res, level,
437                                box->z, box->depth, aux_usage);
438 }
439 
440 static bool
can_fast_clear_depth(struct iris_context * ice,struct iris_resource * res,unsigned level,const struct pipe_box * box,bool render_condition_enabled,float depth)441 can_fast_clear_depth(struct iris_context *ice,
442                      struct iris_resource *res,
443                      unsigned level,
444                      const struct pipe_box *box,
445                      bool render_condition_enabled,
446                      float depth)
447 {
448    struct pipe_resource *p_res = (void *) res;
449    struct pipe_context *ctx = (void *) ice;
450    struct iris_screen *screen = (void *) ctx->screen;
451    const struct intel_device_info *devinfo = screen->devinfo;
452 
453    if (INTEL_DEBUG(DEBUG_NO_FAST_CLEAR))
454       return false;
455 
456    /* Check for partial clears */
457    if (box->x > 0 || box->y > 0 ||
458        box->width < u_minify(p_res->width0, level) ||
459        box->height < u_minify(p_res->height0, level)) {
460       return false;
461    }
462 
463    /* Avoid conditional fast clears to maintain correct tracking of the aux
464     * state (see iris_resource_finish_write for more info). Note that partial
465     * fast clears would not pose a problem with conditional rendering.
466     */
467    if (render_condition_enabled &&
468        ice->state.predicate == IRIS_PREDICATE_STATE_USE_BIT) {
469       return false;
470    }
471 
472    if (!iris_resource_level_has_hiz(devinfo, res, level))
473       return false;
474 
475    if (!blorp_can_hiz_clear_depth(devinfo, &res->surf, res->aux.usage,
476                                   level, box->z, box->x, box->y,
477                                   box->x + box->width,
478                                   box->y + box->height)) {
479       return false;
480    }
481 
482    return true;
483 }
484 
485 static void
fast_clear_depth(struct iris_context * ice,struct iris_resource * res,unsigned level,const struct pipe_box * box,float depth)486 fast_clear_depth(struct iris_context *ice,
487                  struct iris_resource *res,
488                  unsigned level,
489                  const struct pipe_box *box,
490                  float depth)
491 {
492    struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
493 
494    bool update_clear_depth = false;
495 
496    /* If we're clearing to a new clear value, then we need to resolve any clear
497     * flags out of the HiZ buffer into the real depth buffer.
498     */
499    if (res->aux.clear_color_unknown || res->aux.clear_color.f32[0] != depth) {
500       for (unsigned res_level = 0; res_level < res->surf.levels; res_level++) {
501          const unsigned level_layers =
502             iris_get_num_logical_layers(res, res_level);
503          for (unsigned layer = 0; layer < level_layers; layer++) {
504             if (res_level == level &&
505                 layer >= box->z &&
506                 layer < box->z + box->depth) {
507                /* We're going to clear this layer anyway.  Leave it alone. */
508                continue;
509             }
510 
511             enum isl_aux_state aux_state =
512                iris_resource_get_aux_state(res, res_level, layer);
513 
514             if (aux_state != ISL_AUX_STATE_CLEAR &&
515                 aux_state != ISL_AUX_STATE_COMPRESSED_CLEAR) {
516                /* This slice doesn't have any fast-cleared bits. */
517                continue;
518             }
519 
520             /* If we got here, then the level may have fast-clear bits that
521              * use the old clear value.  We need to do a depth resolve to get
522              * rid of their use of the clear value before we can change it.
523              * Fortunately, few applications ever change their depth clear
524              * value so this shouldn't happen often.
525              */
526             iris_hiz_exec(ice, batch, res, res_level, layer, 1,
527                           ISL_AUX_OP_FULL_RESOLVE, false);
528             iris_resource_set_aux_state(ice, res, res_level, layer, 1,
529                                         ISL_AUX_STATE_RESOLVED);
530          }
531       }
532       const union isl_color_value clear_value = { .f32 = {depth, } };
533       iris_resource_set_clear_color(ice, res, clear_value);
534       update_clear_depth = true;
535    }
536 
537    if (res->aux.usage == ISL_AUX_USAGE_HIZ_CCS_WT) {
538       /* From Bspec 47010 (Depth Buffer Clear):
539        *
540        *    Since the fast clear cycles to CCS are not cached in TileCache,
541        *    any previous depth buffer writes to overlapping pixels must be
542        *    flushed out of TileCache before a succeeding Depth Buffer Clear.
543        *    This restriction only applies to Depth Buffer with write-thru
544        *    enabled, since fast clears to CCS only occur for write-thru mode.
545        *
546        * There may have been a write to this depth buffer. Flush it from the
547        * tile cache just in case.
548        *
549        * Set CS stall bit to guarantee that the fast clear starts the execution
550        * after the tile cache flush completed.
551        */
552       iris_emit_pipe_control_flush(batch, "hiz_ccs_wt: before fast clear",
553                                    PIPE_CONTROL_DEPTH_CACHE_FLUSH |
554                                    PIPE_CONTROL_CS_STALL |
555                                    PIPE_CONTROL_TILE_CACHE_FLUSH);
556    }
557 
558    for (unsigned l = 0; l < box->depth; l++) {
559       enum isl_aux_state aux_state =
560          iris_resource_get_aux_state(res, level, box->z + l);
561       if (update_clear_depth || aux_state != ISL_AUX_STATE_CLEAR) {
562          if (aux_state == ISL_AUX_STATE_CLEAR) {
563             perf_debug(&ice->dbg, "Performing HiZ clear just to update the "
564                                   "depth clear value\n");
565          }
566          iris_hiz_exec(ice, batch, res, level,
567                        box->z + l, 1, ISL_AUX_OP_FAST_CLEAR,
568                        update_clear_depth);
569       }
570    }
571 
572    iris_resource_set_aux_state(ice, res, level, box->z, box->depth,
573                                ISL_AUX_STATE_CLEAR);
574    ice->state.dirty |= IRIS_DIRTY_DEPTH_BUFFER;
575    ice->state.stage_dirty |= IRIS_ALL_STAGE_DIRTY_BINDINGS;
576 }
577 
578 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)579 clear_depth_stencil(struct iris_context *ice,
580                     struct pipe_resource *p_res,
581                     unsigned level,
582                     const struct pipe_box *box,
583                     bool render_condition_enabled,
584                     bool clear_depth,
585                     bool clear_stencil,
586                     float depth,
587                     uint8_t stencil)
588 {
589    struct iris_resource *res = (void *) p_res;
590 
591    struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
592    enum blorp_batch_flags blorp_flags = 0;
593 
594    if (render_condition_enabled) {
595       if (ice->state.predicate == IRIS_PREDICATE_STATE_DONT_RENDER)
596          return;
597 
598       if (ice->state.predicate == IRIS_PREDICATE_STATE_USE_BIT)
599          blorp_flags |= BLORP_BATCH_PREDICATE_ENABLE;
600    }
601 
602    iris_batch_maybe_flush(batch, 1500);
603 
604    struct iris_resource *z_res;
605    struct iris_resource *stencil_res;
606    struct blorp_surf z_surf;
607    struct blorp_surf stencil_surf;
608 
609    iris_get_depth_stencil_resources(p_res, &z_res, &stencil_res);
610    if (z_res && clear_depth &&
611        can_fast_clear_depth(ice, z_res, level, box, render_condition_enabled,
612                             depth)) {
613       fast_clear_depth(ice, z_res, level, box, depth);
614       iris_dirty_for_history(ice, res);
615       clear_depth = false;
616       z_res = false;
617    }
618 
619    /* At this point, we might have fast cleared the depth buffer. So if there's
620     * no stencil clear pending, return early.
621     */
622    if (!(clear_depth || (clear_stencil && stencil_res))) {
623       return;
624    }
625 
626    if (clear_depth && z_res) {
627       const enum isl_aux_usage aux_usage =
628          iris_resource_render_aux_usage(ice, z_res, z_res->surf.format, level,
629                                         false);
630       iris_resource_prepare_render(ice, z_res, z_res->surf.format, level,
631                                    box->z, box->depth, aux_usage);
632       iris_emit_buffer_barrier_for(batch, z_res->bo, IRIS_DOMAIN_DEPTH_WRITE);
633       iris_blorp_surf_for_resource(&batch->screen->isl_dev, &z_surf,
634                                    &z_res->base.b, aux_usage, level, true);
635    }
636 
637    uint8_t stencil_mask = clear_stencil && stencil_res ? 0xff : 0;
638    if (stencil_mask) {
639       iris_resource_prepare_access(ice, stencil_res, level, 1, box->z,
640                                    box->depth, stencil_res->aux.usage, false);
641       iris_emit_buffer_barrier_for(batch, stencil_res->bo,
642                                    IRIS_DOMAIN_DEPTH_WRITE);
643       iris_blorp_surf_for_resource(&batch->screen->isl_dev,
644                                    &stencil_surf, &stencil_res->base.b,
645                                    stencil_res->aux.usage, level, true);
646    }
647 
648    iris_batch_sync_region_start(batch);
649 
650    struct blorp_batch blorp_batch;
651    blorp_batch_init(&ice->blorp, &blorp_batch, batch, blorp_flags);
652 
653    blorp_clear_depth_stencil(&blorp_batch, &z_surf, &stencil_surf,
654                              level, box->z, box->depth,
655                              box->x, box->y,
656                              box->x + box->width,
657                              box->y + box->height,
658                              clear_depth && z_res, depth,
659                              stencil_mask, stencil);
660 
661    blorp_batch_finish(&blorp_batch);
662    iris_batch_sync_region_end(batch);
663 
664    iris_dirty_for_history(ice, res);
665 
666    if (clear_depth && z_res) {
667       iris_resource_finish_render(ice, z_res, level, box->z, box->depth,
668                                   z_surf.aux_usage);
669    }
670 
671    if (stencil_mask) {
672       iris_resource_finish_write(ice, stencil_res, level, box->z, box->depth,
673                                  stencil_res->aux.usage);
674    }
675 }
676 
677 /**
678  * The pipe->clear() driver hook.
679  *
680  * This clears buffers attached to the current draw framebuffer.
681  */
682 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)683 iris_clear(struct pipe_context *ctx,
684            unsigned buffers,
685            const struct pipe_scissor_state *scissor_state,
686            const union pipe_color_union *p_color,
687            double depth,
688            unsigned stencil)
689 {
690    struct iris_context *ice = (void *) ctx;
691    struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
692 
693    assert(buffers != 0);
694 
695    struct pipe_box box = {
696       .width = cso_fb->width,
697       .height = cso_fb->height,
698    };
699 
700    if (scissor_state) {
701       box.x = scissor_state->minx;
702       box.y = scissor_state->miny;
703       box.width = MIN2(box.width, scissor_state->maxx - scissor_state->minx);
704       box.height = MIN2(box.height, scissor_state->maxy - scissor_state->miny);
705    }
706 
707    if (buffers & PIPE_CLEAR_DEPTHSTENCIL) {
708       struct pipe_surface *psurf = cso_fb->zsbuf;
709 
710       box.depth = psurf->u.tex.last_layer - psurf->u.tex.first_layer + 1;
711       box.z = psurf->u.tex.first_layer,
712       clear_depth_stencil(ice, psurf->texture, psurf->u.tex.level, &box, true,
713                           buffers & PIPE_CLEAR_DEPTH,
714                           buffers & PIPE_CLEAR_STENCIL,
715                           depth, stencil);
716    }
717 
718    if (buffers & PIPE_CLEAR_COLOR) {
719       for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
720          if (buffers & (PIPE_CLEAR_COLOR0 << i)) {
721             struct pipe_surface *psurf = cso_fb->cbufs[i];
722             struct iris_surface *isurf = (void *) psurf;
723             box.depth = psurf->u.tex.last_layer - psurf->u.tex.first_layer + 1,
724             box.z = psurf->u.tex.first_layer,
725 
726             clear_color(ice, psurf->texture, psurf->u.tex.level, &box,
727                         true, isurf->view.format, isurf->view.swizzle,
728                         convert_clear_color(psurf->format, p_color));
729          }
730       }
731    }
732 }
733 
734 /**
735  * The pipe->clear_texture() driver hook.
736  *
737  * This clears the given texture resource.
738  */
739 static void
iris_clear_texture(struct pipe_context * ctx,struct pipe_resource * p_res,unsigned level,const struct pipe_box * box,const void * data)740 iris_clear_texture(struct pipe_context *ctx,
741                    struct pipe_resource *p_res,
742                    unsigned level,
743                    const struct pipe_box *box,
744                    const void *data)
745 {
746    struct iris_context *ice = (void *) ctx;
747    struct iris_screen *screen = (void *) ctx->screen;
748    const struct intel_device_info *devinfo = screen->devinfo;
749 
750    if (util_format_is_depth_or_stencil(p_res->format)) {
751       const struct util_format_unpack_description *unpack =
752          util_format_unpack_description(p_res->format);
753 
754       float depth = 0.0;
755       uint8_t stencil = 0;
756 
757       if (unpack->unpack_z_float)
758          util_format_unpack_z_float(p_res->format, &depth, data, 1);
759 
760       if (unpack->unpack_s_8uint)
761          util_format_unpack_s_8uint(p_res->format, &stencil, data, 1);
762 
763       clear_depth_stencil(ice, p_res, level, box, true, true, true,
764                           depth, stencil);
765    } else {
766       union isl_color_value color;
767       struct iris_resource *res = (void *) p_res;
768       enum isl_format format = res->surf.format;
769 
770       if (!isl_format_supports_rendering(devinfo, format)) {
771          const struct isl_format_layout *fmtl = isl_format_get_layout(format);
772          // XXX: actually just get_copy_format_for_bpb from BLORP
773          // XXX: don't cut and paste this
774          switch (fmtl->bpb) {
775          case 8:   format = ISL_FORMAT_R8_UINT;           break;
776          case 16:  format = ISL_FORMAT_R8G8_UINT;         break;
777          case 24:  format = ISL_FORMAT_R8G8B8_UINT;       break;
778          case 32:  format = ISL_FORMAT_R8G8B8A8_UINT;     break;
779          case 48:  format = ISL_FORMAT_R16G16B16_UINT;    break;
780          case 64:  format = ISL_FORMAT_R16G16B16A16_UINT; break;
781          case 96:  format = ISL_FORMAT_R32G32B32_UINT;    break;
782          case 128: format = ISL_FORMAT_R32G32B32A32_UINT; break;
783          default:
784             unreachable("Unknown format bpb");
785          }
786 
787          /* No aux surfaces for non-renderable surfaces */
788          assert(res->aux.usage == ISL_AUX_USAGE_NONE);
789       }
790 
791       isl_color_value_unpack(&color, format, data);
792 
793       clear_color(ice, p_res, level, box, true, format,
794                   ISL_SWIZZLE_IDENTITY, color);
795    }
796 }
797 
798 /**
799  * The pipe->clear_render_target() driver hook.
800  *
801  * This clears the given render target surface.
802  */
803 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)804 iris_clear_render_target(struct pipe_context *ctx,
805                          struct pipe_surface *psurf,
806                          const union pipe_color_union *p_color,
807                          unsigned dst_x, unsigned dst_y,
808                          unsigned width, unsigned height,
809                          bool render_condition_enabled)
810 {
811    struct iris_context *ice = (void *) ctx;
812    struct iris_surface *isurf = (void *) psurf;
813    struct pipe_box box = {
814       .x = dst_x,
815       .y = dst_y,
816       .z = psurf->u.tex.first_layer,
817       .width = width,
818       .height = height,
819       .depth = psurf->u.tex.last_layer - psurf->u.tex.first_layer + 1
820    };
821 
822    clear_color(ice, psurf->texture, psurf->u.tex.level, &box,
823                render_condition_enabled,
824                isurf->view.format, isurf->view.swizzle,
825                convert_clear_color(psurf->format, p_color));
826 }
827 
828 /**
829  * The pipe->clear_depth_stencil() driver hook.
830  *
831  * This clears the given depth/stencil surface.
832  */
833 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)834 iris_clear_depth_stencil(struct pipe_context *ctx,
835                          struct pipe_surface *psurf,
836                          unsigned flags,
837                          double depth,
838                          unsigned stencil,
839                          unsigned dst_x, unsigned dst_y,
840                          unsigned width, unsigned height,
841                          bool render_condition_enabled)
842 {
843    struct iris_context *ice = (void *) ctx;
844    struct pipe_box box = {
845       .x = dst_x,
846       .y = dst_y,
847       .z = psurf->u.tex.first_layer,
848       .width = width,
849       .height = height,
850       .depth = psurf->u.tex.last_layer - psurf->u.tex.first_layer + 1
851    };
852 
853    assert(util_format_is_depth_or_stencil(psurf->texture->format));
854 
855    clear_depth_stencil(ice, psurf->texture, psurf->u.tex.level, &box,
856                        render_condition_enabled,
857                        flags & PIPE_CLEAR_DEPTH, flags & PIPE_CLEAR_STENCIL,
858                        depth, stencil);
859 }
860 
861 void
iris_init_clear_functions(struct pipe_context * ctx)862 iris_init_clear_functions(struct pipe_context *ctx)
863 {
864    ctx->clear = iris_clear;
865    ctx->clear_texture = iris_clear_texture;
866    ctx->clear_render_target = iris_clear_render_target;
867    ctx->clear_depth_stencil = iris_clear_depth_stencil;
868 }
869