• 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    /* On gfx12.0, CCS fast clears don't seem to cover the correct portion of
151     * the aux buffer when the pitch is not 512B-aligned.
152     */
153    if (devinfo->verx10 == 120 &&
154        res->surf.samples == 1 &&
155        res->surf.row_pitch_B % 512) {
156       perf_debug(&ice->dbg, "Pitch not 512B-aligned. Slow clearing surface.");
157       return false;
158    }
159 
160    /* Wa_16021232440: Disable fast clear when height is 16k */
161    if (intel_needs_workaround(devinfo, 16021232440) &&
162        res->surf.logical_level0_px.h == 16 * 1024) {
163       return false;
164    }
165 
166    return true;
167 }
168 
169 static union isl_color_value
convert_clear_color(enum pipe_format format,const union pipe_color_union * color)170 convert_clear_color(enum pipe_format format,
171                     const union pipe_color_union *color)
172 {
173    uint32_t pixel[4];
174    util_format_pack_rgba(format, pixel, color, 1);
175 
176    union isl_color_value converted_color;
177    util_format_unpack_rgba(format, &converted_color, pixel, 1);
178 
179    /* The converted clear color has channels that are:
180     *   - clamped
181     *   - quantized
182     *   - filled with 0/1 if missing from the format
183     *   - swizzled for luminance and intensity formats
184     */
185    return converted_color;
186 }
187 
188 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)189 fast_clear_color(struct iris_context *ice,
190                  struct iris_resource *res,
191                  unsigned level,
192                  const struct pipe_box *box,
193                  union isl_color_value color)
194 {
195    struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
196    const struct intel_device_info *devinfo = batch->screen->devinfo;
197    struct pipe_resource *p_res = (void *) res;
198 
199    bool color_changed = res->aux.clear_color_unknown ||
200       memcmp(&res->aux.clear_color, &color, sizeof(color)) != 0;
201 
202    if (color_changed) {
203       /* If we are clearing to a new clear value, we need to resolve fast
204        * clears from other levels/layers first, since we can't have different
205        * levels/layers with different fast clear colors.
206        */
207       for (unsigned res_lvl = 0; res_lvl < res->surf.levels; res_lvl++) {
208          const unsigned level_layers =
209             iris_get_num_logical_layers(res, res_lvl);
210          for (unsigned layer = 0; layer < level_layers; layer++) {
211             if (res_lvl == level &&
212                 layer >= box->z &&
213                 layer < box->z + box->depth) {
214                /* We're going to clear this layer anyway.  Leave it alone. */
215                continue;
216             }
217 
218             enum isl_aux_state aux_state =
219                iris_resource_get_aux_state(res, res_lvl, layer);
220 
221             if (aux_state != ISL_AUX_STATE_CLEAR &&
222                 aux_state != ISL_AUX_STATE_PARTIAL_CLEAR &&
223                 aux_state != ISL_AUX_STATE_COMPRESSED_CLEAR) {
224                /* This slice doesn't have any fast-cleared bits. */
225                continue;
226             }
227 
228             /* If we got here, then the level may have fast-clear bits that use
229              * the old clear value.  We need to do a color resolve to get rid
230              * of their use of the clear color before we can change it.
231              * Fortunately, few applications ever change their clear color at
232              * different levels/layers, so this shouldn't happen often.
233              */
234             iris_resource_prepare_access(ice, res,
235                                          res_lvl, 1, layer, 1,
236                                          res->aux.usage,
237                                          false);
238             if (res->aux.clear_color_unknown) {
239                perf_debug(&ice->dbg,
240                           "Resolving resource (%p) level %d, layer %d: color changing from "
241                           "(unknown) to (%0.2f, %0.2f, %0.2f, %0.2f)\n",
242                           res, res_lvl, layer,
243                           color.f32[0], color.f32[1], color.f32[2], color.f32[3]);
244             } else {
245                perf_debug(&ice->dbg,
246                           "Resolving resource (%p) level %d, layer %d: color changing from "
247                           "(%0.2f, %0.2f, %0.2f, %0.2f) to "
248                           "(%0.2f, %0.2f, %0.2f, %0.2f)\n",
249                           res, res_lvl, layer,
250                           res->aux.clear_color.f32[0],
251                           res->aux.clear_color.f32[1],
252                           res->aux.clear_color.f32[2],
253                           res->aux.clear_color.f32[3],
254                           color.f32[0], color.f32[1], color.f32[2], color.f32[3]);
255             }
256          }
257       }
258    }
259 
260    iris_resource_set_clear_color(ice, res, color);
261 
262    /* Ivybridge PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
263     *
264     *    "Any transition from any value in {Clear, Render, Resolve} to a
265     *    different value in {Clear, Render, Resolve} requires end of pipe
266     *    synchronization."
267     *
268     * In other words, fast clear ops are not properly synchronized with
269     * other drawing.  We need to use a PIPE_CONTROL to ensure that the
270     * contents of the previous draw hit the render target before we resolve
271     * and again afterwards to ensure that the resolve is complete before we
272     * do any more regular drawing.
273     */
274    iris_emit_end_of_pipe_sync(batch, "fast clear: pre-flush",
275       PIPE_CONTROL_RENDER_TARGET_FLUSH |
276       (devinfo->ver    == 12  ? PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
277                                 PIPE_CONTROL_TILE_CACHE_FLUSH : 0) |
278       (devinfo->verx10 == 120 ? PIPE_CONTROL_DEPTH_STALL : 0) |
279       (devinfo->verx10 == 125 ? PIPE_CONTROL_FLUSH_HDC |
280                                 PIPE_CONTROL_DATA_CACHE_FLUSH : 0) |
281       PIPE_CONTROL_PSS_STALL_SYNC);
282 
283    /* Update the clear color now that previous rendering is complete. */
284    if (color_changed && res->aux.clear_color_bo)
285       iris_resource_update_indirect_color(batch, res);
286 
287    /* If the buffer is already in ISL_AUX_STATE_CLEAR, the clear is redundant
288     * and can be skipped.
289     */
290    const enum isl_aux_state aux_state =
291       iris_resource_get_aux_state(res, level, box->z);
292    if (box->depth == 1 && aux_state == ISL_AUX_STATE_CLEAR)
293       return;
294 
295    iris_batch_sync_region_start(batch);
296 
297    struct blorp_batch blorp_batch;
298    blorp_batch_init(&ice->blorp, &blorp_batch, batch, 0);
299 
300    struct blorp_surf surf;
301    iris_blorp_surf_for_resource(batch, &surf, p_res, res->aux.usage,
302                                 level, true);
303 
304    blorp_fast_clear(&blorp_batch, &surf, res->surf.format,
305                     ISL_SWIZZLE_IDENTITY,
306                     level, box->z, box->depth,
307                     box->x, box->y, box->x + box->width,
308                     box->y + box->height);
309    blorp_batch_finish(&blorp_batch);
310 
311    if (devinfo->verx10 >= 125) {
312       /* From the ACM PRM Vol. 9, "Color Fast Clear Synchronization":
313        *
314        *    Postamble post fast clear synchronization
315        *
316        *    PIPE_CONTROL:
317        *    PS sync stall = 1
318        *    RT flush = 1
319        */
320       iris_emit_pipe_control_flush(batch, "fast clear: post flush",
321                                    PIPE_CONTROL_RENDER_TARGET_FLUSH |
322                                    PIPE_CONTROL_PSS_STALL_SYNC);
323    } else if (devinfo->verx10 == 120) {
324       /* From the TGL PRM Vol. 9, "Color Fast Clear Synchronization":
325        *
326        *    Postamble post fast clear synchronization
327        *
328        *    PIPE_CONTROL:
329        *    Depth Stall = 1
330        *    Tile Cache Flush = 1
331        *    RT Write Flush = 1
332        *
333        * From the TGL PRM Vol. 2a, "PIPE_CONTROL::L3 Fabric Flush":
334        *
335        *    For a sequence of color fast clears. A single PIPE_CONTROL
336        *    command with Render Target Cache Flush, L3 Fabric Flush and Depth
337        *    Stall set at the end of the sequence suffices.
338        *
339        * Replace the Tile Cache flush with an L3 fabric flush.
340        */
341       iris_emit_pipe_control_flush(batch, "fast clear: post flush",
342                                    PIPE_CONTROL_DEPTH_STALL |
343                                    PIPE_CONTROL_L3_FABRIC_FLUSH |
344                                    PIPE_CONTROL_RENDER_TARGET_FLUSH);
345    } else {
346       /* From the Sky Lake PRM Vol. 7, "Render Target Fast Clear":
347        *
348        *    After Render target fast clear, pipe-control with color cache
349        *    write-flush must be issued before sending any DRAW commands on
350        *    that render target.
351        *
352        * From the Sky Lake PRM Vol. 7, "MCS Buffer for Render Target(s)":
353        *
354        *    Any transition from any value in {Clear, Render, Resolve} to a
355        *    different value in {Clear, Render, Resolve} requires end of pipe
356        *    synchronization.
357        */
358       iris_emit_end_of_pipe_sync(batch, "fast clear: post flush",
359                                  PIPE_CONTROL_RENDER_TARGET_FLUSH);
360    }
361 
362    iris_batch_sync_region_end(batch);
363 
364    iris_resource_set_aux_state(ice, res, level, box->z,
365                                box->depth, devinfo->ver < 20 ?
366                                ISL_AUX_STATE_CLEAR :
367                                ISL_AUX_STATE_COMPRESSED_NO_CLEAR);
368    ice->state.dirty |= IRIS_DIRTY_RENDER_BUFFER;
369    ice->state.stage_dirty |= IRIS_ALL_STAGE_DIRTY_BINDINGS;
370    return;
371 }
372 
373 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)374 clear_color(struct iris_context *ice,
375             struct pipe_resource *p_res,
376             unsigned level,
377             const struct pipe_box *box,
378             bool render_condition_enabled,
379             enum isl_format format,
380             struct isl_swizzle swizzle,
381             union isl_color_value color)
382 {
383    struct iris_resource *res = (void *) p_res;
384 
385    struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
386    const struct intel_device_info *devinfo = batch->screen->devinfo;
387    enum blorp_batch_flags blorp_flags = iris_blorp_flags_for_batch(batch);
388 
389    if (render_condition_enabled) {
390       if (ice->state.predicate == IRIS_PREDICATE_STATE_DONT_RENDER)
391          return;
392 
393       if (ice->state.predicate == IRIS_PREDICATE_STATE_USE_BIT)
394          blorp_flags |= BLORP_BATCH_PREDICATE_ENABLE;
395    }
396 
397    if (p_res->target == PIPE_BUFFER)
398       util_range_add(&res->base.b, &res->valid_buffer_range, box->x, box->x + box->width);
399 
400    iris_batch_maybe_flush(batch, 1500);
401 
402    bool can_fast_clear = can_fast_clear_color(ice, p_res, level, box,
403                                               render_condition_enabled,
404                                               format, color);
405    if (can_fast_clear) {
406       fast_clear_color(ice, res, level, box, color);
407       return;
408    }
409 
410    enum isl_aux_usage aux_usage =
411       iris_resource_render_aux_usage(ice, res, format, level, false);
412 
413    iris_resource_prepare_render(ice, res, format, level, box->z, box->depth,
414                                 aux_usage);
415    iris_emit_buffer_barrier_for(batch, res->bo, IRIS_DOMAIN_RENDER_WRITE);
416 
417    struct blorp_surf surf;
418    iris_blorp_surf_for_resource(batch, &surf, p_res, aux_usage, level, true);
419 
420    iris_batch_sync_region_start(batch);
421 
422    struct blorp_batch blorp_batch;
423    blorp_batch_init(&ice->blorp, &blorp_batch, batch, blorp_flags);
424 
425    if (!isl_format_supports_rendering(devinfo, format) &&
426        isl_format_is_rgbx(format))
427       format = isl_format_rgbx_to_rgba(format);
428 
429    blorp_clear(&blorp_batch, &surf, format, swizzle,
430                level, box->z, box->depth, box->x, box->y,
431                box->x + box->width, box->y + box->height,
432                color, 0 /* color_write_disable */);
433 
434    blorp_batch_finish(&blorp_batch);
435    iris_batch_sync_region_end(batch);
436 
437    iris_dirty_for_history(ice, res);
438 
439    iris_resource_finish_render(ice, res, level,
440                                box->z, box->depth, aux_usage);
441 }
442 
443 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)444 can_fast_clear_depth(struct iris_context *ice,
445                      struct iris_resource *res,
446                      unsigned level,
447                      const struct pipe_box *box,
448                      bool render_condition_enabled,
449                      float depth)
450 {
451    struct pipe_resource *p_res = (void *) res;
452    struct pipe_context *ctx = (void *) ice;
453    struct iris_screen *screen = (void *) ctx->screen;
454    const struct intel_device_info *devinfo = screen->devinfo;
455 
456    if (INTEL_DEBUG(DEBUG_NO_FAST_CLEAR))
457       return false;
458 
459    /* Check for partial clears */
460    if (box->x > 0 || box->y > 0 ||
461        box->width < u_minify(p_res->width0, level) ||
462        box->height < u_minify(p_res->height0, level)) {
463       return false;
464    }
465 
466    /* Avoid conditional fast clears to maintain correct tracking of the aux
467     * state (see iris_resource_finish_write for more info). Note that partial
468     * fast clears would not pose a problem with conditional rendering.
469     */
470    if (render_condition_enabled &&
471        ice->state.predicate == IRIS_PREDICATE_STATE_USE_BIT) {
472       return false;
473    }
474 
475    if (!iris_resource_level_has_hiz(devinfo, res, level))
476       return false;
477 
478    /* From the TGL PRM, Vol 9, "Compressed Depth Buffers" (under the
479     * "Texture performant" and "ZCS" columns):
480     *
481     *    Update with clear at either 16x8 or 8x4 granularity, based on
482     *    fs_clr or otherwise.
483     *
484     * When fast-clearing, hardware behaves in unexpected ways if the clear
485     * rectangle, aligned to 16x8, could cover neighboring LODs. Fortunately,
486     * ISL guarantees that LOD0 will be 8-row aligned and LOD0's height seems
487     * to not matter. Also, few applications ever clear LOD1+. Only allow
488     * fast-clearing upper LODs if no overlap can occur.
489     */
490    if (res->aux.usage == ISL_AUX_USAGE_HIZ_CCS_WT && level >= 1 &&
491        (p_res->width0 % 32 != 0 || res->surf.image_alignment_el.h % 8 != 0)) {
492       return false;
493    }
494 
495    return true;
496 }
497 
498 static void
fast_clear_depth(struct iris_context * ice,struct iris_resource * res,unsigned level,const struct pipe_box * box,float depth)499 fast_clear_depth(struct iris_context *ice,
500                  struct iris_resource *res,
501                  unsigned level,
502                  const struct pipe_box *box,
503                  float depth)
504 {
505    struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
506    const struct intel_device_info *devinfo = batch->screen->devinfo;
507 
508    if (res->aux.usage == ISL_AUX_USAGE_HIZ_CCS_WT) {
509       /* From Bspec 47010 (Depth Buffer Clear):
510        *
511        *    Since the fast clear cycles to CCS are not cached in TileCache,
512        *    any previous depth buffer writes to overlapping pixels must be
513        *    flushed out of TileCache before a succeeding Depth Buffer Clear.
514        *    This restriction only applies to Depth Buffer with write-thru
515        *    enabled, since fast clears to CCS only occur for write-thru mode.
516        *
517        * There may have been a write to this depth buffer. Flush it from the
518        * tile cache just in case.
519        *
520        * Set CS stall bit to guarantee that the fast clear starts the execution
521        * after the tile cache flush completed.
522        */
523       iris_emit_pipe_control_flush(batch, "hiz_ccs_wt: before fast clear",
524                                    PIPE_CONTROL_DEPTH_CACHE_FLUSH |
525                                    PIPE_CONTROL_CS_STALL |
526                                    PIPE_CONTROL_TILE_CACHE_FLUSH);
527    }
528 
529    /* If we're clearing to a new clear value, then we need to resolve any clear
530     * flags out of the HiZ buffer into the real depth buffer.
531     */
532    if (res->aux.clear_color_unknown || res->aux.clear_color.f32[0] != depth) {
533       for (unsigned res_level = 0; res_level < res->surf.levels; res_level++) {
534          const unsigned level_layers =
535             iris_get_num_logical_layers(res, res_level);
536          for (unsigned layer = 0; layer < level_layers; layer++) {
537             if (res_level == level &&
538                 layer >= box->z &&
539                 layer < box->z + box->depth) {
540                /* We're going to clear this layer anyway.  Leave it alone. */
541                continue;
542             }
543 
544             enum isl_aux_state aux_state =
545                iris_resource_get_aux_state(res, res_level, layer);
546 
547             if (aux_state != ISL_AUX_STATE_CLEAR &&
548                 aux_state != ISL_AUX_STATE_COMPRESSED_CLEAR) {
549                /* This slice doesn't have any fast-cleared bits. */
550                continue;
551             }
552 
553             /* If we got here, then the level may have fast-clear bits that
554              * use the old clear value.  We need to do a depth resolve to get
555              * rid of their use of the clear value before we can change it.
556              * Fortunately, few applications ever change their depth clear
557              * value so this shouldn't happen often.
558              */
559             iris_hiz_exec(ice, batch, res, res_level, layer, 1,
560                           ISL_AUX_OP_FULL_RESOLVE);
561             iris_resource_set_aux_state(ice, res, res_level, layer, 1,
562                                         ISL_AUX_STATE_RESOLVED);
563          }
564       }
565       const union isl_color_value clear_value = { .f32 = {depth, } };
566       iris_resource_set_clear_color(ice, res, clear_value);
567 
568       /* Also set the indirect clear color if it exists. */
569       if (res->aux.clear_color_bo) {
570          uint32_t packed_depth[4] = {};
571          isl_color_value_pack(&clear_value, res->surf.format, packed_depth);
572 
573          const uint64_t clear_pixel_offset = res->aux.clear_color_offset +
574             isl_get_sampler_clear_field_offset(devinfo, res->surf.format);
575 
576          iris_emit_pipe_control_write(batch, "update fast clear value (Z)",
577                                       PIPE_CONTROL_WRITE_IMMEDIATE,
578                                       res->aux.clear_color_bo,
579                                       clear_pixel_offset, packed_depth[0]);
580 
581          /* From the TGL PRMs, Volume 9: Render Engine, State Caching :
582           *
583           *    "Any values referenced by pointers within the
584           *    RENDER_SURFACE_STATE or SAMPLER_STATE (e.g. Clear Color
585           *    Pointer, Border Color or Indirect State Pointer) are considered
586           *    to be part of that state and any changes to these referenced
587           *    values requires an invalidation of the L1 state cache to ensure
588           *    the new values are being used as part of the state."
589           *
590           * Invalidate the state cache as suggested.
591           */
592          iris_emit_pipe_control_flush(batch, "flush fast clear values (z)",
593                                       PIPE_CONTROL_FLUSH_ENABLE |
594                                       PIPE_CONTROL_STATE_CACHE_INVALIDATE);
595       }
596    }
597 
598    for (unsigned l = 0; l < box->depth; l++) {
599       enum isl_aux_state aux_state =
600          iris_resource_get_aux_state(res, level, box->z + l);
601       if (aux_state != ISL_AUX_STATE_CLEAR) {
602          iris_hiz_exec(ice, batch, res, level,
603                        box->z + l, 1, ISL_AUX_OP_FAST_CLEAR);
604       }
605    }
606 
607    iris_resource_set_aux_state(ice, res, level, box->z, box->depth,
608                                devinfo->ver < 20 ? ISL_AUX_STATE_CLEAR :
609                                ISL_AUX_STATE_COMPRESSED_NO_CLEAR);
610    ice->state.dirty |= IRIS_DIRTY_DEPTH_BUFFER;
611    ice->state.stage_dirty |= IRIS_ALL_STAGE_DIRTY_BINDINGS;
612 }
613 
614 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)615 clear_depth_stencil(struct iris_context *ice,
616                     struct pipe_resource *p_res,
617                     unsigned level,
618                     const struct pipe_box *box,
619                     bool render_condition_enabled,
620                     bool clear_depth,
621                     bool clear_stencil,
622                     float depth,
623                     uint8_t stencil)
624 {
625    struct iris_resource *res = (void *) p_res;
626 
627    struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
628    enum blorp_batch_flags blorp_flags = 0;
629 
630    if (render_condition_enabled) {
631       if (ice->state.predicate == IRIS_PREDICATE_STATE_DONT_RENDER)
632          return;
633 
634       if (ice->state.predicate == IRIS_PREDICATE_STATE_USE_BIT)
635          blorp_flags |= BLORP_BATCH_PREDICATE_ENABLE;
636    }
637 
638    iris_batch_maybe_flush(batch, 1500);
639 
640    struct iris_resource *z_res;
641    struct iris_resource *stencil_res;
642    struct blorp_surf z_surf;
643    struct blorp_surf stencil_surf;
644 
645    iris_get_depth_stencil_resources(p_res, &z_res, &stencil_res);
646    if (z_res && clear_depth &&
647        can_fast_clear_depth(ice, z_res, level, box, render_condition_enabled,
648                             depth)) {
649       fast_clear_depth(ice, z_res, level, box, depth);
650       iris_dirty_for_history(ice, res);
651       clear_depth = false;
652       z_res = false;
653    }
654 
655    /* At this point, we might have fast cleared the depth buffer. So if there's
656     * no stencil clear pending, return early.
657     */
658    if (!(clear_depth || (clear_stencil && stencil_res))) {
659       return;
660    }
661 
662    if (clear_depth && z_res) {
663       const enum isl_aux_usage aux_usage =
664          iris_resource_render_aux_usage(ice, z_res, z_res->surf.format, level,
665                                         false);
666       iris_resource_prepare_render(ice, z_res, z_res->surf.format, level,
667                                    box->z, box->depth, aux_usage);
668       iris_emit_buffer_barrier_for(batch, z_res->bo, IRIS_DOMAIN_DEPTH_WRITE);
669       iris_blorp_surf_for_resource(batch, &z_surf, &z_res->base.b,
670                                    aux_usage, level, true);
671    }
672 
673    uint8_t stencil_mask = clear_stencil && stencil_res ? 0xff : 0;
674    if (stencil_mask) {
675       iris_resource_prepare_access(ice, stencil_res, level, 1, box->z,
676                                    box->depth, stencil_res->aux.usage, false);
677       iris_emit_buffer_barrier_for(batch, stencil_res->bo,
678                                    IRIS_DOMAIN_DEPTH_WRITE);
679       iris_blorp_surf_for_resource(batch, &stencil_surf, &stencil_res->base.b,
680                                    stencil_res->aux.usage, level, true);
681    }
682 
683    iris_batch_sync_region_start(batch);
684 
685    struct blorp_batch blorp_batch;
686    blorp_batch_init(&ice->blorp, &blorp_batch, batch, blorp_flags);
687 
688    blorp_clear_depth_stencil(&blorp_batch, &z_surf, &stencil_surf,
689                              level, box->z, box->depth,
690                              box->x, box->y,
691                              box->x + box->width,
692                              box->y + box->height,
693                              clear_depth && z_res, depth,
694                              stencil_mask, stencil);
695 
696    blorp_batch_finish(&blorp_batch);
697    iris_batch_sync_region_end(batch);
698 
699    iris_dirty_for_history(ice, res);
700 
701    if (clear_depth && z_res) {
702       iris_resource_finish_render(ice, z_res, level, box->z, box->depth,
703                                   z_surf.aux_usage);
704    }
705 
706    if (stencil_mask) {
707       iris_resource_finish_write(ice, stencil_res, level, box->z, box->depth,
708                                  stencil_res->aux.usage);
709    }
710 }
711 
712 /**
713  * The pipe->clear() driver hook.
714  *
715  * This clears buffers attached to the current draw framebuffer.
716  */
717 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)718 iris_clear(struct pipe_context *ctx,
719            unsigned buffers,
720            const struct pipe_scissor_state *scissor_state,
721            const union pipe_color_union *p_color,
722            double depth,
723            unsigned stencil)
724 {
725    struct iris_context *ice = (void *) ctx;
726    struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
727 
728    assert(buffers != 0);
729 
730    struct pipe_box box = {
731       .width = cso_fb->width,
732       .height = cso_fb->height,
733    };
734 
735    if (scissor_state) {
736       box.x = scissor_state->minx;
737       box.y = scissor_state->miny;
738       box.width = MIN2(box.width, scissor_state->maxx - scissor_state->minx);
739       box.height = MIN2(box.height, scissor_state->maxy - scissor_state->miny);
740    }
741 
742    if (buffers & PIPE_CLEAR_DEPTHSTENCIL) {
743       struct pipe_surface *psurf = cso_fb->zsbuf;
744 
745       box.depth = psurf->u.tex.last_layer - psurf->u.tex.first_layer + 1;
746       box.z = psurf->u.tex.first_layer,
747       clear_depth_stencil(ice, psurf->texture, psurf->u.tex.level, &box, true,
748                           buffers & PIPE_CLEAR_DEPTH,
749                           buffers & PIPE_CLEAR_STENCIL,
750                           depth, stencil);
751    }
752 
753    if (buffers & PIPE_CLEAR_COLOR) {
754       for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
755          if (buffers & (PIPE_CLEAR_COLOR0 << i)) {
756             struct pipe_surface *psurf = cso_fb->cbufs[i];
757             struct iris_surface *isurf = (void *) psurf;
758             box.depth = psurf->u.tex.last_layer - psurf->u.tex.first_layer + 1,
759             box.z = psurf->u.tex.first_layer,
760 
761             clear_color(ice, psurf->texture, psurf->u.tex.level, &box,
762                         true, isurf->view.format, isurf->view.swizzle,
763                         convert_clear_color(psurf->format, p_color));
764          }
765       }
766    }
767 }
768 
769 /**
770  * The pipe->clear_texture() driver hook.
771  *
772  * This clears the given texture resource.
773  */
774 static void
iris_clear_texture(struct pipe_context * ctx,struct pipe_resource * p_res,unsigned level,const struct pipe_box * box,const void * data)775 iris_clear_texture(struct pipe_context *ctx,
776                    struct pipe_resource *p_res,
777                    unsigned level,
778                    const struct pipe_box *box,
779                    const void *data)
780 {
781    struct iris_context *ice = (void *) ctx;
782    struct iris_screen *screen = (void *) ctx->screen;
783    const struct intel_device_info *devinfo = screen->devinfo;
784 
785    if (util_format_is_depth_or_stencil(p_res->format)) {
786       const struct util_format_unpack_description *unpack =
787          util_format_unpack_description(p_res->format);
788 
789       float depth = 0.0;
790       uint8_t stencil = 0;
791 
792       if (unpack->unpack_z_float)
793          util_format_unpack_z_float(p_res->format, &depth, data, 1);
794 
795       if (unpack->unpack_s_8uint)
796          util_format_unpack_s_8uint(p_res->format, &stencil, data, 1);
797 
798       clear_depth_stencil(ice, p_res, level, box, true, true, true,
799                           depth, stencil);
800    } else {
801       union isl_color_value color;
802       struct iris_resource *res = (void *) p_res;
803       enum isl_format format = res->surf.format;
804 
805       if (!isl_format_supports_rendering(devinfo, format)) {
806          const struct isl_format_layout *fmtl = isl_format_get_layout(format);
807          // XXX: actually just get_copy_format_for_bpb from BLORP
808          // XXX: don't cut and paste this
809          switch (fmtl->bpb) {
810          case 8:   format = ISL_FORMAT_R8_UINT;           break;
811          case 16:  format = ISL_FORMAT_R8G8_UINT;         break;
812          case 24:  format = ISL_FORMAT_R8G8B8_UINT;       break;
813          case 32:  format = ISL_FORMAT_R8G8B8A8_UINT;     break;
814          case 48:  format = ISL_FORMAT_R16G16B16_UINT;    break;
815          case 64:  format = ISL_FORMAT_R16G16B16A16_UINT; break;
816          case 96:  format = ISL_FORMAT_R32G32B32_UINT;    break;
817          case 128: format = ISL_FORMAT_R32G32B32A32_UINT; break;
818          default:
819             unreachable("Unknown format bpb");
820          }
821 
822          /* No aux surfaces for non-renderable surfaces */
823          assert(res->aux.usage == ISL_AUX_USAGE_NONE);
824       }
825 
826       isl_color_value_unpack(&color, format, data);
827 
828       clear_color(ice, p_res, level, box, true, format,
829                   ISL_SWIZZLE_IDENTITY, color);
830    }
831 }
832 
833 /**
834  * The pipe->clear_render_target() driver hook.
835  *
836  * This clears the given render target surface.
837  */
838 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)839 iris_clear_render_target(struct pipe_context *ctx,
840                          struct pipe_surface *psurf,
841                          const union pipe_color_union *p_color,
842                          unsigned dst_x, unsigned dst_y,
843                          unsigned width, unsigned height,
844                          bool render_condition_enabled)
845 {
846    struct iris_context *ice = (void *) ctx;
847    struct iris_surface *isurf = (void *) psurf;
848    struct pipe_box box = {
849       .x = dst_x,
850       .y = dst_y,
851       .z = psurf->u.tex.first_layer,
852       .width = width,
853       .height = height,
854       .depth = psurf->u.tex.last_layer - psurf->u.tex.first_layer + 1
855    };
856 
857    clear_color(ice, psurf->texture, psurf->u.tex.level, &box,
858                render_condition_enabled,
859                isurf->view.format, isurf->view.swizzle,
860                convert_clear_color(psurf->format, p_color));
861 }
862 
863 /**
864  * The pipe->clear_depth_stencil() driver hook.
865  *
866  * This clears the given depth/stencil surface.
867  */
868 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)869 iris_clear_depth_stencil(struct pipe_context *ctx,
870                          struct pipe_surface *psurf,
871                          unsigned flags,
872                          double depth,
873                          unsigned stencil,
874                          unsigned dst_x, unsigned dst_y,
875                          unsigned width, unsigned height,
876                          bool render_condition_enabled)
877 {
878    struct iris_context *ice = (void *) ctx;
879    struct pipe_box box = {
880       .x = dst_x,
881       .y = dst_y,
882       .z = psurf->u.tex.first_layer,
883       .width = width,
884       .height = height,
885       .depth = psurf->u.tex.last_layer - psurf->u.tex.first_layer + 1
886    };
887 
888    assert(util_format_is_depth_or_stencil(psurf->texture->format));
889 
890    clear_depth_stencil(ice, psurf->texture, psurf->u.tex.level, &box,
891                        render_condition_enabled,
892                        flags & PIPE_CLEAR_DEPTH, flags & PIPE_CLEAR_STENCIL,
893                        depth, stencil);
894 }
895 
896 void
iris_init_clear_functions(struct pipe_context * ctx)897 iris_init_clear_functions(struct pipe_context *ctx)
898 {
899    ctx->clear = iris_clear;
900    ctx->clear_texture = iris_clear_texture;
901    ctx->clear_render_target = iris_clear_render_target;
902    ctx->clear_depth_stencil = iris_clear_depth_stencil;
903 }
904