• 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/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    /* If we reach this point, we need to fast clear to change the state to
292     * ISL_AUX_STATE_CLEAR, or to update the fast clear color (or both).
293     */
294    blorp_flags |= color_changed ? 0 : BLORP_BATCH_NO_UPDATE_CLEAR_COLOR;
295 
296    struct blorp_batch blorp_batch;
297    blorp_batch_init(&ice->blorp, &blorp_batch, batch, blorp_flags);
298 
299    struct blorp_surf surf;
300    crocus_blorp_surf_for_resource(&screen->vtbl, &batch->screen->isl_dev, &surf,
301                                   p_res, res->aux.usage, level, true);
302 
303    /* In newer gens (> 9), the hardware will do a linear -> sRGB conversion of
304     * the clear color during the fast clear, if the surface format is of sRGB
305     * type. We use the linear version of the surface format here to prevent
306     * that from happening, since we already do our own linear -> sRGB
307     * conversion in convert_fast_clear_color().
308     */
309    blorp_fast_clear(&blorp_batch, &surf, isl_format_srgb_to_linear(format),
310                     ISL_SWIZZLE_IDENTITY,
311                     level, box->z, box->depth,
312                     box->x, box->y, box->x + box->width,
313                     box->y + box->height);
314    blorp_batch_finish(&blorp_batch);
315    crocus_emit_end_of_pipe_sync(batch,
316                                 "fast clear: post flush",
317                                 PIPE_CONTROL_RENDER_TARGET_FLUSH);
318 
319    crocus_resource_set_aux_state(ice, res, level, box->z,
320                                  box->depth, ISL_AUX_STATE_CLEAR);
321    ice->state.stage_dirty |= CROCUS_ALL_STAGE_DIRTY_BINDINGS;
322    return;
323 }
324 
325 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)326 clear_color(struct crocus_context *ice,
327             struct pipe_resource *p_res,
328             unsigned level,
329             const struct pipe_box *box,
330             bool render_condition_enabled,
331             enum isl_format format,
332             struct isl_swizzle swizzle,
333             union isl_color_value color)
334 {
335    struct crocus_resource *res = (void *) p_res;
336    struct crocus_batch *batch = &ice->batches[CROCUS_BATCH_RENDER];
337    struct crocus_screen *screen = batch->screen;
338    const struct intel_device_info *devinfo = &batch->screen->devinfo;
339    enum blorp_batch_flags blorp_flags = 0;
340 
341    if (render_condition_enabled) {
342       if (!crocus_check_conditional_render(ice))
343          return;
344 
345       if (ice->state.predicate == CROCUS_PREDICATE_STATE_USE_BIT)
346          blorp_flags |= BLORP_BATCH_PREDICATE_ENABLE;
347    }
348 
349    if (p_res->target == PIPE_BUFFER)
350       util_range_add(&res->base.b, &res->valid_buffer_range, box->x, box->x + box->width);
351 
352    crocus_batch_maybe_flush(batch, 1500);
353 
354    bool can_fast_clear = can_fast_clear_color(ice, p_res, level, box,
355                                               render_condition_enabled,
356                                               res->surf.format, format, color);
357    if (can_fast_clear) {
358       fast_clear_color(ice, res, level, box, format, color,
359                        blorp_flags);
360       return;
361    }
362 
363    enum isl_aux_usage aux_usage =
364       crocus_resource_render_aux_usage(ice, res, level, format, false);
365 
366    crocus_resource_prepare_render(ice, res, level,
367                                   box->z, box->depth, aux_usage);
368 
369    struct blorp_surf surf;
370    crocus_blorp_surf_for_resource(&screen->vtbl, &batch->screen->isl_dev, &surf,
371                                   p_res, aux_usage, level, true);
372 
373    struct blorp_batch blorp_batch;
374    blorp_batch_init(&ice->blorp, &blorp_batch, batch, blorp_flags);
375 
376    if (!isl_format_supports_rendering(devinfo, format) &&
377        isl_format_is_rgbx(format))
378       format = isl_format_rgbx_to_rgba(format);
379 
380    blorp_clear(&blorp_batch, &surf, format, swizzle,
381                level, box->z, box->depth, box->x, box->y,
382                box->x + box->width, box->y + box->height,
383                color, 0 /* color_write_disable */);
384 
385    blorp_batch_finish(&blorp_batch);
386    crocus_flush_and_dirty_for_history(ice, batch, res,
387                                       PIPE_CONTROL_RENDER_TARGET_FLUSH,
388                                       "cache history: post color clear");
389 
390    crocus_resource_finish_render(ice, res, level,
391                                  box->z, box->depth, aux_usage);
392 }
393 
394 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)395 can_fast_clear_depth(struct crocus_context *ice,
396                      struct crocus_resource *res,
397                      unsigned level,
398                      const struct pipe_box *box,
399                      bool render_condition_enabled,
400                      float depth)
401 {
402    struct pipe_resource *p_res = (void *) res;
403    struct pipe_context *ctx = (void *) ice;
404    struct crocus_screen *screen = (void *) ctx->screen;
405    const struct intel_device_info *devinfo = &screen->devinfo;
406 
407    if (devinfo->ver < 6)
408       return false;
409 
410    if (INTEL_DEBUG(DEBUG_NO_FAST_CLEAR))
411       return false;
412 
413    /* Check for partial clears */
414    if (box->x > 0 || box->y > 0 ||
415        box->width < u_minify(p_res->width0, level) ||
416        box->height < u_minify(p_res->height0, level)) {
417       return false;
418    }
419 
420    /* Avoid conditional fast clears to maintain correct tracking of the aux
421     * state (see iris_resource_finish_write for more info). Note that partial
422     * fast clears would not pose a problem with conditional rendering.
423     */
424    if (render_condition_enabled &&
425        ice->state.predicate == CROCUS_PREDICATE_STATE_USE_BIT) {
426       return false;
427    }
428 
429    if (!crocus_resource_level_has_hiz(res, level))
430       return false;
431 
432    if (res->base.b.format == PIPE_FORMAT_Z16_UNORM) {
433       /* From the Sandy Bridge PRM, volume 2 part 1, page 314:
434        *
435        *     "[DevSNB+]: Several cases exist where Depth Buffer Clear cannot be
436        *      enabled (the legacy method of clearing must be performed):
437        *
438        *      - DevSNB{W/A}]: When depth buffer format is D16_UNORM and the
439        *        width of the map (LOD0) is not multiple of 16, fast clear
440        *        optimization must be disabled.
441        */
442       if (devinfo->ver == 6 &&
443           (u_minify(res->surf.phys_level0_sa.width,
444                     level) % 16) != 0)
445          return false;
446    }
447    return true;
448 }
449 
450 static void
fast_clear_depth(struct crocus_context * ice,struct crocus_resource * res,unsigned level,const struct pipe_box * box,float depth)451 fast_clear_depth(struct crocus_context *ice,
452                  struct crocus_resource *res,
453                  unsigned level,
454                  const struct pipe_box *box,
455                  float depth)
456 {
457    struct crocus_batch *batch = &ice->batches[CROCUS_BATCH_RENDER];
458 
459    bool update_clear_depth = false;
460 
461    /* If we're clearing to a new clear value, then we need to resolve any clear
462     * flags out of the HiZ buffer into the real depth buffer.
463     */
464    if (res->aux.clear_color.f32[0] != depth) {
465       for (unsigned res_level = 0; res_level < res->surf.levels; res_level++) {
466          if (!crocus_resource_level_has_hiz(res, res_level))
467             continue;
468 
469          const unsigned level_layers =
470             crocus_get_num_logical_layers(res, res_level);
471          for (unsigned layer = 0; layer < level_layers; layer++) {
472             if (res_level == level &&
473                 layer >= box->z &&
474                 layer < box->z + box->depth) {
475                /* We're going to clear this layer anyway.  Leave it alone. */
476                continue;
477             }
478 
479             enum isl_aux_state aux_state =
480                crocus_resource_get_aux_state(res, res_level, layer);
481 
482             if (aux_state != ISL_AUX_STATE_CLEAR &&
483                 aux_state != ISL_AUX_STATE_COMPRESSED_CLEAR) {
484                /* This slice doesn't have any fast-cleared bits. */
485                continue;
486             }
487 
488             /* If we got here, then the level may have fast-clear bits that
489              * use the old clear value.  We need to do a depth resolve to get
490              * rid of their use of the clear value before we can change it.
491              * Fortunately, few applications ever change their depth clear
492              * value so this shouldn't happen often.
493              */
494             crocus_hiz_exec(ice, batch, res, res_level, layer, 1,
495                             ISL_AUX_OP_FULL_RESOLVE, false);
496             crocus_resource_set_aux_state(ice, res, res_level, layer, 1,
497                                           ISL_AUX_STATE_RESOLVED);
498          }
499       }
500       const union isl_color_value clear_value = { .f32 = {depth, } };
501       crocus_resource_set_clear_color(ice, res, clear_value);
502       update_clear_depth = true;
503    }
504 
505    for (unsigned l = 0; l < box->depth; l++) {
506       enum isl_aux_state aux_state =
507          crocus_resource_level_has_hiz(res, level) ?
508          crocus_resource_get_aux_state(res, level, box->z + l) :
509          ISL_AUX_STATE_AUX_INVALID;
510       if (update_clear_depth || aux_state != ISL_AUX_STATE_CLEAR) {
511          if (aux_state == ISL_AUX_STATE_CLEAR) {
512             perf_debug(&ice->dbg, "Performing HiZ clear just to update the "
513                        "depth clear value\n");
514          }
515          crocus_hiz_exec(ice, batch, res, level,
516                          box->z + l, 1, ISL_AUX_OP_FAST_CLEAR,
517                          update_clear_depth);
518       }
519    }
520 
521    crocus_resource_set_aux_state(ice, res, level, box->z, box->depth,
522                                  ISL_AUX_STATE_CLEAR);
523    ice->state.dirty |= CROCUS_DIRTY_DEPTH_BUFFER;
524 }
525 
526 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)527 clear_depth_stencil(struct crocus_context *ice,
528                     struct pipe_resource *p_res,
529                     unsigned level,
530                     const struct pipe_box *box,
531                     bool render_condition_enabled,
532                     bool clear_depth,
533                     bool clear_stencil,
534                     float depth,
535                     uint8_t stencil)
536 {
537    struct crocus_resource *res = (void *) p_res;
538    struct crocus_batch *batch = &ice->batches[CROCUS_BATCH_RENDER];
539    struct crocus_screen *screen = batch->screen;
540    enum blorp_batch_flags blorp_flags = 0;
541 
542    if (render_condition_enabled) {
543       if (!crocus_check_conditional_render(ice))
544          return;
545 
546       if (ice->state.predicate == CROCUS_PREDICATE_STATE_USE_BIT)
547          blorp_flags |= BLORP_BATCH_PREDICATE_ENABLE;
548    }
549 
550    crocus_batch_maybe_flush(batch, 1500);
551 
552    struct crocus_resource *z_res;
553    struct crocus_resource *stencil_res;
554    struct blorp_surf z_surf;
555    struct blorp_surf stencil_surf;
556 
557    crocus_get_depth_stencil_resources(&batch->screen->devinfo, p_res, &z_res, &stencil_res);
558    if (z_res && clear_depth &&
559        can_fast_clear_depth(ice, z_res, level, box, render_condition_enabled,
560                             depth)) {
561       fast_clear_depth(ice, z_res, level, box, depth);
562       crocus_flush_and_dirty_for_history(ice, batch, res, 0,
563                                          "cache history: post fast Z clear");
564       clear_depth = false;
565       z_res = NULL;
566    }
567 
568    /* At this point, we might have fast cleared the depth buffer. So if there's
569     * no stencil clear pending, return early.
570     */
571    if (!(clear_depth || (clear_stencil && stencil_res))) {
572       return;
573    }
574 
575    if (clear_depth && z_res) {
576       const enum isl_aux_usage aux_usage =
577          crocus_resource_render_aux_usage(ice, z_res, level, z_res->surf.format,
578                                           false);
579       crocus_resource_prepare_render(ice, z_res, level, box->z, box->depth,
580                                      aux_usage);
581       crocus_blorp_surf_for_resource(&screen->vtbl, &batch->screen->isl_dev,
582                                      &z_surf, &z_res->base.b, aux_usage,
583                                      level, true);
584    }
585 
586    struct blorp_batch blorp_batch;
587    blorp_batch_init(&ice->blorp, &blorp_batch, batch, blorp_flags);
588 
589    uint8_t stencil_mask = clear_stencil && stencil_res ? 0xff : 0;
590    if (stencil_mask) {
591       crocus_resource_prepare_access(ice, stencil_res, level, 1, box->z,
592                                      box->depth, stencil_res->aux.usage, false);
593       crocus_blorp_surf_for_resource(&screen->vtbl, &batch->screen->isl_dev,
594                                      &stencil_surf, &stencil_res->base.b,
595                                      stencil_res->aux.usage, level, true);
596    }
597 
598    blorp_clear_depth_stencil(&blorp_batch, &z_surf, &stencil_surf,
599                              level, box->z, box->depth,
600                              box->x, box->y,
601                              box->x + box->width,
602                              box->y + box->height,
603                              clear_depth && z_res, depth,
604                              stencil_mask, stencil);
605 
606    blorp_batch_finish(&blorp_batch);
607    crocus_flush_and_dirty_for_history(ice, batch, res, 0,
608                                       "cache history: post slow ZS clear");
609 
610    if (clear_depth && z_res) {
611       crocus_resource_finish_render(ice, z_res, level,
612                                     box->z, box->depth, z_surf.aux_usage);
613    }
614 
615    if (stencil_mask) {
616       crocus_resource_finish_write(ice, stencil_res, level, box->z, box->depth,
617                                    stencil_res->aux.usage);
618    }
619 }
620 
621 /**
622  * The pipe->clear() driver hook.
623  *
624  * This clears buffers attached to the current draw framebuffer.
625  */
626 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)627 crocus_clear(struct pipe_context *ctx,
628              unsigned buffers,
629              const struct pipe_scissor_state *scissor_state,
630              const union pipe_color_union *p_color,
631              double depth,
632              unsigned stencil)
633 {
634    struct crocus_context *ice = (void *) ctx;
635    struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
636    struct crocus_screen *screen = (void *) ctx->screen;
637    const struct intel_device_info *devinfo = &screen->devinfo;
638    assert(buffers != 0);
639 
640    struct pipe_box box = {
641       .width = cso_fb->width,
642       .height = cso_fb->height,
643    };
644 
645    if (scissor_state) {
646       box.x = scissor_state->minx;
647       box.y = scissor_state->miny;
648       box.width = MIN2(box.width, scissor_state->maxx - scissor_state->minx);
649       box.height = MIN2(box.height, scissor_state->maxy - scissor_state->miny);
650    }
651 
652    if (buffers & PIPE_CLEAR_DEPTHSTENCIL) {
653       if (devinfo->ver < 6) {
654          crocus_blitter_begin(ice, CROCUS_SAVE_FRAGMENT_STATE, true);
655          util_blitter_clear(ice->blitter, cso_fb->width, cso_fb->height,
656                             util_framebuffer_get_num_layers(cso_fb),
657                             buffers & PIPE_CLEAR_DEPTHSTENCIL, p_color, depth, stencil, false);
658       } else {
659          struct pipe_surface *psurf = cso_fb->zsbuf;
660          box.depth = psurf->u.tex.last_layer - psurf->u.tex.first_layer + 1;
661          box.z = psurf->u.tex.first_layer;
662 
663          clear_depth_stencil(ice, psurf->texture, psurf->u.tex.level, &box, true,
664                              buffers & PIPE_CLEAR_DEPTH,
665                              buffers & PIPE_CLEAR_STENCIL,
666                              depth, stencil);
667       }
668       buffers &= ~PIPE_CLEAR_DEPTHSTENCIL;
669    }
670 
671    if (buffers & PIPE_CLEAR_COLOR) {
672       /* pipe_color_union and isl_color_value are interchangeable */
673       union isl_color_value *color = (void *) p_color;
674 
675       for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
676          if (buffers & (PIPE_CLEAR_COLOR0 << i)) {
677             struct pipe_surface *psurf = cso_fb->cbufs[i];
678             struct crocus_surface *isurf = (void *) psurf;
679             box.depth = psurf->u.tex.last_layer - psurf->u.tex.first_layer + 1,
680             box.z = psurf->u.tex.first_layer,
681 
682             clear_color(ice, psurf->texture, psurf->u.tex.level, &box,
683                         true, isurf->view.format, isurf->view.swizzle,
684                         *color);
685          }
686       }
687    }
688 }
689 
690 /**
691  * The pipe->clear_texture() driver hook.
692  *
693  * This clears the given texture resource.
694  */
695 static void
crocus_clear_texture(struct pipe_context * ctx,struct pipe_resource * p_res,unsigned level,const struct pipe_box * box,const void * data)696 crocus_clear_texture(struct pipe_context *ctx,
697                      struct pipe_resource *p_res,
698                      unsigned level,
699                      const struct pipe_box *box,
700                      const void *data)
701 {
702    struct crocus_context *ice = (void *) ctx;
703    struct crocus_screen *screen = (void *) ctx->screen;
704    const struct intel_device_info *devinfo = &screen->devinfo;
705 
706    if (devinfo->ver < 6) {
707       u_default_clear_texture(ctx, p_res, level, box, data);
708       return;
709    }
710 
711    if (util_format_is_depth_or_stencil(p_res->format)) {
712       const struct util_format_unpack_description *fmt_unpack =
713          util_format_unpack_description(p_res->format);
714 
715       float depth = 0.0;
716       uint8_t stencil = 0;
717 
718       if (fmt_unpack->unpack_z_float)
719          fmt_unpack->unpack_z_float(&depth, 0, data, 0, 1, 1);
720 
721       if (fmt_unpack->unpack_s_8uint)
722          fmt_unpack->unpack_s_8uint(&stencil, 0, data, 0, 1, 1);
723 
724       clear_depth_stencil(ice, p_res, level, box, true, true, true,
725                           depth, stencil);
726    } else {
727       union isl_color_value color;
728       struct crocus_resource *res = (void *) p_res;
729       enum isl_format format = res->surf.format;
730 
731       if (!isl_format_supports_rendering(devinfo, format)) {
732          const struct isl_format_layout *fmtl = isl_format_get_layout(format);
733          // XXX: actually just get_copy_format_for_bpb from BLORP
734          // XXX: don't cut and paste this
735          switch (fmtl->bpb) {
736          case 8:   format = ISL_FORMAT_R8_UINT;           break;
737          case 16:  format = ISL_FORMAT_R8G8_UINT;         break;
738          case 24:  format = ISL_FORMAT_R8G8B8_UINT;       break;
739          case 32:  format = ISL_FORMAT_R8G8B8A8_UINT;     break;
740          case 48:  format = ISL_FORMAT_R16G16B16_UINT;    break;
741          case 64:  format = ISL_FORMAT_R16G16B16A16_UINT; break;
742          case 96:  format = ISL_FORMAT_R32G32B32_UINT;    break;
743          case 128: format = ISL_FORMAT_R32G32B32A32_UINT; break;
744          default:
745             unreachable("Unknown format bpb");
746          }
747 
748          /* No aux surfaces for non-renderable surfaces */
749          assert(res->aux.usage == ISL_AUX_USAGE_NONE);
750       }
751 
752       isl_color_value_unpack(&color, format, data);
753 
754       clear_color(ice, p_res, level, box, true, format,
755                   ISL_SWIZZLE_IDENTITY, color);
756    }
757 }
758 
759 /**
760  * The pipe->clear_render_target() driver hook.
761  *
762  * This clears the given render target surface.
763  */
764 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)765 crocus_clear_render_target(struct pipe_context *ctx,
766                            struct pipe_surface *psurf,
767                            const union pipe_color_union *p_color,
768                            unsigned dst_x, unsigned dst_y,
769                            unsigned width, unsigned height,
770                            bool render_condition_enabled)
771 {
772    struct crocus_context *ice = (void *) ctx;
773    struct crocus_surface *isurf = (void *) psurf;
774    struct pipe_box box = {
775       .x = dst_x,
776       .y = dst_y,
777       .z = psurf->u.tex.first_layer,
778       .width = width,
779       .height = height,
780       .depth = psurf->u.tex.last_layer - psurf->u.tex.first_layer + 1
781    };
782 
783    /* pipe_color_union and isl_color_value are interchangeable */
784    union isl_color_value *color = (void *) p_color;
785 
786    clear_color(ice, psurf->texture, psurf->u.tex.level, &box,
787                render_condition_enabled,
788                isurf->view.format, isurf->view.swizzle, *color);
789 }
790 
791 /**
792  * The pipe->clear_depth_stencil() driver hook.
793  *
794  * This clears the given depth/stencil surface.
795  */
796 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)797 crocus_clear_depth_stencil(struct pipe_context *ctx,
798                            struct pipe_surface *psurf,
799                            unsigned flags,
800                            double depth,
801                            unsigned stencil,
802                            unsigned dst_x, unsigned dst_y,
803                            unsigned width, unsigned height,
804                            bool render_condition_enabled)
805 {
806    return;
807 #if 0
808    struct crocus_context *ice = (void *) ctx;
809    struct pipe_box box = {
810       .x = dst_x,
811       .y = dst_y,
812       .z = psurf->u.tex.first_layer,
813       .width = width,
814       .height = height,
815       .depth = psurf->u.tex.last_layer - psurf->u.tex.first_layer + 1
816    };
817    uint32_t blit_flags = 0;
818 
819    assert(util_format_is_depth_or_stencil(psurf->texture->format));
820 
821    crocus_blitter_begin(ice, CROCUS_SAVE_FRAGMENT_STATE);
822    util_blitter_clear(ice->blitter, width, height,
823                       1, flags, NULL, depth, stencil, render_condition_enabled);
824 #if 0
825    clear_depth_stencil(ice, psurf->texture, psurf->u.tex.level, &box,
826                        render_condition_enabled,
827                        flags & PIPE_CLEAR_DEPTH, flags & PIPE_CLEAR_STENCIL,
828                        depth, stencil);
829 #endif
830 #endif
831 }
832 
833 void
crocus_init_clear_functions(struct pipe_context * ctx)834 crocus_init_clear_functions(struct pipe_context *ctx)
835 {
836    ctx->clear = crocus_clear;
837    ctx->clear_texture = crocus_clear_texture;
838    ctx->clear_render_target = crocus_clear_render_target;
839    ctx->clear_depth_stencil = crocus_clear_depth_stencil;
840 }
841