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