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