i965: Mark all EGLimages as non-coherent.
[mesa.git] / src / mesa / drivers / dri / i965 / intel_mipmap_tree.c
1 /*
2 * Copyright 2006 VMware, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include <GL/gl.h>
27 #include <GL/internal/dri_interface.h>
28 #include <drm_fourcc.h>
29
30 #include "intel_batchbuffer.h"
31 #include "intel_image.h"
32 #include "intel_mipmap_tree.h"
33 #include "intel_tex.h"
34 #include "intel_blit.h"
35 #include "intel_fbo.h"
36
37 #include "brw_blorp.h"
38 #include "brw_context.h"
39 #include "brw_state.h"
40
41 #include "main/enums.h"
42 #include "main/fbobject.h"
43 #include "main/formats.h"
44 #include "main/glformats.h"
45 #include "main/texcompress_etc.h"
46 #include "main/teximage.h"
47 #include "main/streaming-load-memcpy.h"
48 #include "x86/common_x86_asm.h"
49
50 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
51
52 static void *intel_miptree_map_raw(struct brw_context *brw,
53 struct intel_mipmap_tree *mt,
54 GLbitfield mode);
55
56 static void intel_miptree_unmap_raw(struct intel_mipmap_tree *mt);
57
58 static bool
59 intel_miptree_alloc_aux(struct brw_context *brw,
60 struct intel_mipmap_tree *mt);
61
62 static bool
63 intel_miptree_supports_mcs(struct brw_context *brw,
64 const struct intel_mipmap_tree *mt)
65 {
66 /* MCS compression only applies to multisampled miptrees */
67 if (mt->surf.samples <= 1)
68 return false;
69
70 /* Prior to Gen7, all MSAA surfaces used IMS layout. */
71 if (brw->gen < 7)
72 return false;
73
74 /* In Gen7, IMS layout is only used for depth and stencil buffers. */
75 switch (_mesa_get_format_base_format(mt->format)) {
76 case GL_DEPTH_COMPONENT:
77 case GL_STENCIL_INDEX:
78 case GL_DEPTH_STENCIL:
79 return false;
80 default:
81 /* From the Ivy Bridge PRM, Vol4 Part1 p77 ("MCS Enable"):
82 *
83 * This field must be set to 0 for all SINT MSRTs when all RT channels
84 * are not written
85 *
86 * In practice this means that we have to disable MCS for all signed
87 * integer MSAA buffers. The alternative, to disable MCS only when one
88 * of the render target channels is disabled, is impractical because it
89 * would require converting between CMS and UMS MSAA layouts on the fly,
90 * which is expensive.
91 */
92 if (brw->gen == 7 && _mesa_get_format_datatype(mt->format) == GL_INT) {
93 return false;
94 } else {
95 return true;
96 }
97 }
98 }
99
100 static bool
101 intel_tiling_supports_ccs(const struct brw_context *brw,
102 enum isl_tiling tiling)
103 {
104 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
105 * Target(s)", beneath the "Fast Color Clear" bullet (p326):
106 *
107 * - Support is limited to tiled render targets.
108 *
109 * Gen9 changes the restriction to Y-tile only.
110 */
111 if (brw->gen >= 9)
112 return tiling == ISL_TILING_Y0;
113 else if (brw->gen >= 7)
114 return tiling != ISL_TILING_LINEAR;
115 else
116 return false;
117 }
118
119 /**
120 * For a single-sampled render target ("non-MSRT"), determine if an MCS buffer
121 * can be used. This doesn't (and should not) inspect any of the properties of
122 * the miptree's BO.
123 *
124 * From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render Target(s)",
125 * beneath the "Fast Color Clear" bullet (p326):
126 *
127 * - Support is for non-mip-mapped and non-array surface types only.
128 *
129 * And then later, on p327:
130 *
131 * - MCS buffer for non-MSRT is supported only for RT formats 32bpp,
132 * 64bpp, and 128bpp.
133 *
134 * From the Skylake documentation, it is made clear that X-tiling is no longer
135 * supported:
136 *
137 * - MCS and Lossless compression is supported for TiledY/TileYs/TileYf
138 * non-MSRTs only.
139 */
140 static bool
141 intel_miptree_supports_ccs(struct brw_context *brw,
142 const struct intel_mipmap_tree *mt)
143 {
144 /* MCS support does not exist prior to Gen7 */
145 if (brw->gen < 7)
146 return false;
147
148 /* This function applies only to non-multisampled render targets. */
149 if (mt->surf.samples > 1)
150 return false;
151
152 /* MCS is only supported for color buffers */
153 switch (_mesa_get_format_base_format(mt->format)) {
154 case GL_DEPTH_COMPONENT:
155 case GL_DEPTH_STENCIL:
156 case GL_STENCIL_INDEX:
157 return false;
158 }
159
160 if (mt->cpp != 4 && mt->cpp != 8 && mt->cpp != 16)
161 return false;
162
163 const bool mip_mapped = mt->first_level != 0 || mt->last_level != 0;
164 const bool arrayed = mt->surf.logical_level0_px.array_len > 1 ||
165 mt->surf.logical_level0_px.depth > 1;
166
167 if (arrayed) {
168 /* Multisample surfaces with the CMS layout are not layered surfaces,
169 * yet still have physical_depth0 > 1. Assert that we don't
170 * accidentally reject a multisampled surface here. We should have
171 * rejected it earlier by explicitly checking the sample count.
172 */
173 assert(mt->surf.samples == 1);
174 }
175
176 /* Handle the hardware restrictions...
177 *
178 * All GENs have the following restriction: "MCS buffer for non-MSRT is
179 * supported only for RT formats 32bpp, 64bpp, and 128bpp."
180 *
181 * From the HSW PRM Volume 7: 3D-Media-GPGPU, page 652: (Color Clear of
182 * Non-MultiSampler Render Target Restrictions) Support is for
183 * non-mip-mapped and non-array surface types only.
184 *
185 * From the BDW PRM Volume 7: 3D-Media-GPGPU, page 649: (Color Clear of
186 * Non-MultiSampler Render Target Restriction). Mip-mapped and arrayed
187 * surfaces are supported with MCS buffer layout with these alignments in
188 * the RT space: Horizontal Alignment = 256 and Vertical Alignment = 128.
189 *
190 * From the SKL PRM Volume 7: 3D-Media-GPGPU, page 632: (Color Clear of
191 * Non-MultiSampler Render Target Restriction). Mip-mapped and arrayed
192 * surfaces are supported with MCS buffer layout with these alignments in
193 * the RT space: Horizontal Alignment = 128 and Vertical Alignment = 64.
194 */
195 if (brw->gen < 8 && (mip_mapped || arrayed))
196 return false;
197
198 /* There's no point in using an MCS buffer if the surface isn't in a
199 * renderable format.
200 */
201 if (!brw->mesa_format_supports_render[mt->format])
202 return false;
203
204 return true;
205 }
206
207 static bool
208 intel_tiling_supports_hiz(const struct brw_context *brw,
209 enum isl_tiling tiling)
210 {
211 if (brw->gen < 6)
212 return false;
213
214 return tiling == ISL_TILING_Y0;
215 }
216
217 static bool
218 intel_miptree_supports_hiz(const struct brw_context *brw,
219 const struct intel_mipmap_tree *mt)
220 {
221 if (!brw->has_hiz)
222 return false;
223
224 switch (mt->format) {
225 case MESA_FORMAT_Z_FLOAT32:
226 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
227 case MESA_FORMAT_Z24_UNORM_X8_UINT:
228 case MESA_FORMAT_Z24_UNORM_S8_UINT:
229 case MESA_FORMAT_Z_UNORM16:
230 return true;
231 default:
232 return false;
233 }
234 }
235
236 static bool
237 intel_miptree_supports_ccs_e(struct brw_context *brw,
238 const struct intel_mipmap_tree *mt)
239 {
240 if (brw->gen < 9)
241 return false;
242
243 /* For now compression is only enabled for integer formats even though
244 * there exist supported floating point formats also. This is a heuristic
245 * decision based on current public benchmarks. In none of the cases these
246 * formats provided any improvement but a few cases were seen to regress.
247 * Hence these are left to to be enabled in the future when they are known
248 * to improve things.
249 */
250 if (_mesa_get_format_datatype(mt->format) == GL_FLOAT)
251 return false;
252
253 if (!intel_miptree_supports_ccs(brw, mt))
254 return false;
255
256 /* Many window system buffers are sRGB even if they are never rendered as
257 * sRGB. For those, we want CCS_E for when sRGBEncode is false. When the
258 * surface is used as sRGB, we fall back to CCS_D.
259 */
260 mesa_format linear_format = _mesa_get_srgb_format_linear(mt->format);
261 enum isl_format isl_format = brw_isl_format_for_mesa_format(linear_format);
262 return isl_format_supports_ccs_e(&brw->screen->devinfo, isl_format);
263 }
264
265 /**
266 * Determine depth format corresponding to a depth+stencil format,
267 * for separate stencil.
268 */
269 mesa_format
270 intel_depth_format_for_depthstencil_format(mesa_format format) {
271 switch (format) {
272 case MESA_FORMAT_Z24_UNORM_S8_UINT:
273 return MESA_FORMAT_Z24_UNORM_X8_UINT;
274 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
275 return MESA_FORMAT_Z_FLOAT32;
276 default:
277 return format;
278 }
279 }
280
281 static bool
282 create_mapping_table(GLenum target, unsigned first_level, unsigned last_level,
283 unsigned depth0, struct intel_mipmap_level *table)
284 {
285 for (unsigned level = first_level; level <= last_level; level++) {
286 const unsigned d =
287 target == GL_TEXTURE_3D ? minify(depth0, level) : depth0;
288
289 table[level].slice = calloc(d, sizeof(*table[0].slice));
290 if (!table[level].slice)
291 goto unwind;
292 }
293
294 return true;
295
296 unwind:
297 for (unsigned level = first_level; level <= last_level; level++)
298 free(table[level].slice);
299
300 return false;
301 }
302
303 static bool
304 needs_separate_stencil(const struct brw_context *brw,
305 struct intel_mipmap_tree *mt,
306 mesa_format format)
307 {
308 if (_mesa_get_format_base_format(format) != GL_DEPTH_STENCIL)
309 return false;
310
311 if (brw->must_use_separate_stencil)
312 return true;
313
314 return brw->has_separate_stencil &&
315 intel_miptree_supports_hiz(brw, mt);
316 }
317
318 /**
319 * Choose the aux usage for this miptree. This function must be called fairly
320 * late in the miptree create process after we have a tiling.
321 */
322 static void
323 intel_miptree_choose_aux_usage(struct brw_context *brw,
324 struct intel_mipmap_tree *mt)
325 {
326 assert(mt->aux_usage == ISL_AUX_USAGE_NONE);
327
328 if (intel_miptree_supports_mcs(brw, mt)) {
329 assert(mt->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY);
330 mt->aux_usage = ISL_AUX_USAGE_MCS;
331 } else if (intel_tiling_supports_ccs(brw, mt->surf.tiling) &&
332 intel_miptree_supports_ccs(brw, mt)) {
333 if (!unlikely(INTEL_DEBUG & DEBUG_NO_RBC) &&
334 intel_miptree_supports_ccs_e(brw, mt)) {
335 mt->aux_usage = ISL_AUX_USAGE_CCS_E;
336 } else {
337 mt->aux_usage = ISL_AUX_USAGE_CCS_D;
338 }
339 } else if (intel_tiling_supports_hiz(brw, mt->surf.tiling) &&
340 intel_miptree_supports_hiz(brw, mt)) {
341 mt->aux_usage = ISL_AUX_USAGE_HIZ;
342 }
343
344 /* We can do fast-clear on all auxiliary surface types that are
345 * allocated through the normal texture creation paths.
346 */
347 if (mt->aux_usage != ISL_AUX_USAGE_NONE)
348 mt->supports_fast_clear = true;
349 }
350
351
352 /**
353 * Choose an appropriate uncompressed format for a requested
354 * compressed format, if unsupported.
355 */
356 mesa_format
357 intel_lower_compressed_format(struct brw_context *brw, mesa_format format)
358 {
359 /* No need to lower ETC formats on these platforms,
360 * they are supported natively.
361 */
362 if (brw->gen >= 8 || brw->is_baytrail)
363 return format;
364
365 switch (format) {
366 case MESA_FORMAT_ETC1_RGB8:
367 return MESA_FORMAT_R8G8B8X8_UNORM;
368 case MESA_FORMAT_ETC2_RGB8:
369 return MESA_FORMAT_R8G8B8X8_UNORM;
370 case MESA_FORMAT_ETC2_SRGB8:
371 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
372 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
373 return MESA_FORMAT_B8G8R8A8_SRGB;
374 case MESA_FORMAT_ETC2_RGBA8_EAC:
375 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
376 return MESA_FORMAT_R8G8B8A8_UNORM;
377 case MESA_FORMAT_ETC2_R11_EAC:
378 return MESA_FORMAT_R_UNORM16;
379 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
380 return MESA_FORMAT_R_SNORM16;
381 case MESA_FORMAT_ETC2_RG11_EAC:
382 return MESA_FORMAT_R16G16_UNORM;
383 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
384 return MESA_FORMAT_R16G16_SNORM;
385 default:
386 /* Non ETC1 / ETC2 format */
387 return format;
388 }
389 }
390
391 unsigned
392 brw_get_num_logical_layers(const struct intel_mipmap_tree *mt, unsigned level)
393 {
394 if (mt->surf.dim == ISL_SURF_DIM_3D)
395 return minify(mt->surf.logical_level0_px.depth, level);
396 else
397 return mt->surf.logical_level0_px.array_len;
398 }
399
400 static unsigned
401 get_num_phys_layers(const struct isl_surf *surf, unsigned level)
402 {
403 /* In case of physical dimensions one needs to consider also the layout.
404 * See isl_calc_phys_level0_extent_sa().
405 */
406 if (surf->dim != ISL_SURF_DIM_3D)
407 return surf->phys_level0_sa.array_len;
408
409 if (surf->dim_layout == ISL_DIM_LAYOUT_GEN4_2D)
410 return minify(surf->phys_level0_sa.array_len, level);
411
412 return minify(surf->phys_level0_sa.depth, level);
413 }
414
415 /** \brief Assert that the level and layer are valid for the miptree. */
416 void
417 intel_miptree_check_level_layer(const struct intel_mipmap_tree *mt,
418 uint32_t level,
419 uint32_t layer)
420 {
421 (void) mt;
422 (void) level;
423 (void) layer;
424
425 assert(level >= mt->first_level);
426 assert(level <= mt->last_level);
427 assert(layer < get_num_phys_layers(&mt->surf, level));
428 }
429
430 static enum isl_aux_state **
431 create_aux_state_map(struct intel_mipmap_tree *mt,
432 enum isl_aux_state initial)
433 {
434 const uint32_t levels = mt->last_level + 1;
435
436 uint32_t total_slices = 0;
437 for (uint32_t level = 0; level < levels; level++)
438 total_slices += brw_get_num_logical_layers(mt, level);
439
440 const size_t per_level_array_size = levels * sizeof(enum isl_aux_state *);
441
442 /* We're going to allocate a single chunk of data for both the per-level
443 * reference array and the arrays of aux_state. This makes cleanup
444 * significantly easier.
445 */
446 const size_t total_size = per_level_array_size +
447 total_slices * sizeof(enum isl_aux_state);
448 void *data = malloc(total_size);
449 if (data == NULL)
450 return NULL;
451
452 enum isl_aux_state **per_level_arr = data;
453 enum isl_aux_state *s = data + per_level_array_size;
454 for (uint32_t level = 0; level < levels; level++) {
455 per_level_arr[level] = s;
456 const unsigned level_layers = brw_get_num_logical_layers(mt, level);
457 for (uint32_t a = 0; a < level_layers; a++)
458 *(s++) = initial;
459 }
460 assert((void *)s == data + total_size);
461
462 return per_level_arr;
463 }
464
465 static void
466 free_aux_state_map(enum isl_aux_state **state)
467 {
468 free(state);
469 }
470
471 static bool
472 need_to_retile_as_linear(struct brw_context *brw, unsigned row_pitch,
473 enum isl_tiling tiling, unsigned samples)
474 {
475 if (samples > 1)
476 return false;
477
478 if (tiling == ISL_TILING_LINEAR)
479 return false;
480
481 /* If the width is much smaller than a tile, don't bother tiling. */
482 if (row_pitch < 64)
483 return true;
484
485 if (ALIGN(row_pitch, 512) >= 32768) {
486 perf_debug("row pitch %u too large to blit, falling back to untiled",
487 row_pitch);
488 return true;
489 }
490
491 return false;
492 }
493
494 static bool
495 need_to_retile_as_x(const struct brw_context *brw, uint64_t size,
496 enum isl_tiling tiling)
497 {
498 /* If the BO is too large to fit in the aperture, we need to use the
499 * BLT engine to support it. Prior to Sandybridge, the BLT paths can't
500 * handle Y-tiling, so we need to fall back to X.
501 */
502 if (brw->gen < 6 && size >= brw->max_gtt_map_object_size &&
503 tiling == ISL_TILING_Y0)
504 return true;
505
506 return false;
507 }
508
509 static struct intel_mipmap_tree *
510 make_surface(struct brw_context *brw, GLenum target, mesa_format format,
511 unsigned first_level, unsigned last_level,
512 unsigned width0, unsigned height0, unsigned depth0,
513 unsigned num_samples, isl_tiling_flags_t tiling_flags,
514 isl_surf_usage_flags_t isl_usage_flags, uint32_t alloc_flags,
515 unsigned row_pitch, struct brw_bo *bo)
516 {
517 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
518 if (!mt)
519 return NULL;
520
521 if (!create_mapping_table(target, first_level, last_level, depth0,
522 mt->level)) {
523 free(mt);
524 return NULL;
525 }
526
527 mt->refcount = 1;
528
529 if (target == GL_TEXTURE_CUBE_MAP ||
530 target == GL_TEXTURE_CUBE_MAP_ARRAY)
531 isl_usage_flags |= ISL_SURF_USAGE_CUBE_BIT;
532
533 DBG("%s: %s %s %ux %u:%u:%u %d..%d <-- %p\n",
534 __func__,
535 _mesa_enum_to_string(target),
536 _mesa_get_format_name(format),
537 num_samples, width0, height0, depth0,
538 first_level, last_level, mt);
539
540 struct isl_surf_init_info init_info = {
541 .dim = get_isl_surf_dim(target),
542 .format = translate_tex_format(brw, format, false),
543 .width = width0,
544 .height = height0,
545 .depth = target == GL_TEXTURE_3D ? depth0 : 1,
546 .levels = last_level - first_level + 1,
547 .array_len = target == GL_TEXTURE_3D ? 1 : depth0,
548 .samples = num_samples,
549 .row_pitch = row_pitch,
550 .usage = isl_usage_flags,
551 .tiling_flags = tiling_flags,
552 };
553
554 if (!isl_surf_init_s(&brw->isl_dev, &mt->surf, &init_info))
555 goto fail;
556
557 /* In case caller doesn't specifically request Y-tiling (needed
558 * unconditionally for depth), check for corner cases needing special
559 * treatment.
560 */
561 if (tiling_flags & ~ISL_TILING_Y0_BIT) {
562 if (need_to_retile_as_linear(brw, mt->surf.row_pitch,
563 mt->surf.tiling, mt->surf.samples)) {
564 init_info.tiling_flags = 1u << ISL_TILING_LINEAR;
565 if (!isl_surf_init_s(&brw->isl_dev, &mt->surf, &init_info))
566 goto fail;
567 } else if (need_to_retile_as_x(brw, mt->surf.size, mt->surf.tiling)) {
568 init_info.tiling_flags = 1u << ISL_TILING_X;
569 if (!isl_surf_init_s(&brw->isl_dev, &mt->surf, &init_info))
570 goto fail;
571 }
572 }
573
574 /* In case of linear the buffer gets padded by fixed 64 bytes and therefore
575 * the size may not be multiple of row_pitch.
576 * See isl_apply_surface_padding().
577 */
578 if (mt->surf.tiling != ISL_TILING_LINEAR)
579 assert(mt->surf.size % mt->surf.row_pitch == 0);
580
581 if (!bo) {
582 mt->bo = brw_bo_alloc_tiled(brw->bufmgr, "isl-miptree",
583 mt->surf.size,
584 isl_tiling_to_i915_tiling(
585 mt->surf.tiling),
586 mt->surf.row_pitch, alloc_flags);
587 if (!mt->bo)
588 goto fail;
589 } else {
590 mt->bo = bo;
591 }
592
593 mt->first_level = first_level;
594 mt->last_level = last_level;
595 mt->target = target;
596 mt->format = format;
597 mt->aux_state = NULL;
598 mt->cpp = isl_format_get_layout(mt->surf.format)->bpb / 8;
599 mt->compressed = _mesa_is_format_compressed(format);
600 mt->drm_modifier = DRM_FORMAT_MOD_INVALID;
601
602 return mt;
603
604 fail:
605 intel_miptree_release(&mt);
606 return NULL;
607 }
608
609 static bool
610 make_separate_stencil_surface(struct brw_context *brw,
611 struct intel_mipmap_tree *mt)
612 {
613 mt->stencil_mt = make_surface(brw, mt->target, MESA_FORMAT_S_UINT8,
614 0, mt->surf.levels - 1,
615 mt->surf.logical_level0_px.width,
616 mt->surf.logical_level0_px.height,
617 mt->surf.dim == ISL_SURF_DIM_3D ?
618 mt->surf.logical_level0_px.depth :
619 mt->surf.logical_level0_px.array_len,
620 mt->surf.samples, ISL_TILING_W_BIT,
621 ISL_SURF_USAGE_STENCIL_BIT |
622 ISL_SURF_USAGE_TEXTURE_BIT,
623 BO_ALLOC_FOR_RENDER, 0, NULL);
624
625 if (!mt->stencil_mt)
626 return false;
627
628 mt->stencil_mt->r8stencil_needs_update = true;
629
630 return true;
631 }
632
633 static struct intel_mipmap_tree *
634 miptree_create(struct brw_context *brw,
635 GLenum target,
636 mesa_format format,
637 GLuint first_level,
638 GLuint last_level,
639 GLuint width0,
640 GLuint height0,
641 GLuint depth0,
642 GLuint num_samples,
643 enum intel_miptree_create_flags flags)
644 {
645 if (format == MESA_FORMAT_S_UINT8)
646 return make_surface(brw, target, format, first_level, last_level,
647 width0, height0, depth0, num_samples,
648 ISL_TILING_W_BIT,
649 ISL_SURF_USAGE_STENCIL_BIT |
650 ISL_SURF_USAGE_TEXTURE_BIT,
651 BO_ALLOC_FOR_RENDER,
652 0,
653 NULL);
654
655 const GLenum base_format = _mesa_get_format_base_format(format);
656 if ((base_format == GL_DEPTH_COMPONENT ||
657 base_format == GL_DEPTH_STENCIL) &&
658 !(flags & MIPTREE_CREATE_LINEAR)) {
659 /* Fix up the Z miptree format for how we're splitting out separate
660 * stencil. Gen7 expects there to be no stencil bits in its depth buffer.
661 */
662 const mesa_format depth_only_format =
663 intel_depth_format_for_depthstencil_format(format);
664 struct intel_mipmap_tree *mt = make_surface(
665 brw, target, brw->gen >= 6 ? depth_only_format : format,
666 first_level, last_level,
667 width0, height0, depth0, num_samples, ISL_TILING_Y0_BIT,
668 ISL_SURF_USAGE_DEPTH_BIT | ISL_SURF_USAGE_TEXTURE_BIT,
669 BO_ALLOC_FOR_RENDER, 0, NULL);
670
671 if (needs_separate_stencil(brw, mt, format) &&
672 !make_separate_stencil_surface(brw, mt)) {
673 intel_miptree_release(&mt);
674 return NULL;
675 }
676
677 if (!(flags & MIPTREE_CREATE_NO_AUX))
678 intel_miptree_choose_aux_usage(brw, mt);
679
680 return mt;
681 }
682
683 mesa_format tex_format = format;
684 mesa_format etc_format = MESA_FORMAT_NONE;
685 uint32_t alloc_flags = 0;
686
687 format = intel_lower_compressed_format(brw, format);
688
689 etc_format = (format != tex_format) ? tex_format : MESA_FORMAT_NONE;
690
691 if (flags & MIPTREE_CREATE_BUSY)
692 alloc_flags |= BO_ALLOC_FOR_RENDER;
693
694 isl_tiling_flags_t tiling_flags = (flags & MIPTREE_CREATE_LINEAR) ?
695 ISL_TILING_LINEAR_BIT : ISL_TILING_ANY_MASK;
696
697 /* TODO: This used to be because there wasn't BLORP to handle Y-tiling. */
698 if (brw->gen < 6)
699 tiling_flags &= ~ISL_TILING_Y0_BIT;
700
701 struct intel_mipmap_tree *mt = make_surface(
702 brw, target, format,
703 first_level, last_level,
704 width0, height0, depth0,
705 num_samples, tiling_flags,
706 ISL_SURF_USAGE_RENDER_TARGET_BIT |
707 ISL_SURF_USAGE_TEXTURE_BIT,
708 alloc_flags, 0, NULL);
709 if (!mt)
710 return NULL;
711
712 mt->etc_format = etc_format;
713
714 if (!(flags & MIPTREE_CREATE_NO_AUX))
715 intel_miptree_choose_aux_usage(brw, mt);
716
717 return mt;
718 }
719
720 struct intel_mipmap_tree *
721 intel_miptree_create(struct brw_context *brw,
722 GLenum target,
723 mesa_format format,
724 GLuint first_level,
725 GLuint last_level,
726 GLuint width0,
727 GLuint height0,
728 GLuint depth0,
729 GLuint num_samples,
730 enum intel_miptree_create_flags flags)
731 {
732 assert(num_samples > 0);
733
734 struct intel_mipmap_tree *mt = miptree_create(
735 brw, target, format,
736 first_level, last_level,
737 width0, height0, depth0, num_samples,
738 flags);
739 if (!mt)
740 return NULL;
741
742 mt->offset = 0;
743
744 if (!intel_miptree_alloc_aux(brw, mt)) {
745 intel_miptree_release(&mt);
746 return NULL;
747 }
748
749 return mt;
750 }
751
752 struct intel_mipmap_tree *
753 intel_miptree_create_for_bo(struct brw_context *brw,
754 struct brw_bo *bo,
755 mesa_format format,
756 uint32_t offset,
757 uint32_t width,
758 uint32_t height,
759 uint32_t depth,
760 int pitch,
761 enum intel_miptree_create_flags flags)
762 {
763 struct intel_mipmap_tree *mt;
764 uint32_t tiling, swizzle;
765 const GLenum target = depth > 1 ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D;
766 const GLenum base_format = _mesa_get_format_base_format(format);
767
768 if ((base_format == GL_DEPTH_COMPONENT ||
769 base_format == GL_DEPTH_STENCIL)) {
770 const mesa_format depth_only_format =
771 intel_depth_format_for_depthstencil_format(format);
772 mt = make_surface(brw, target,
773 brw->gen >= 6 ? depth_only_format : format,
774 0, 0, width, height, depth, 1, ISL_TILING_Y0_BIT,
775 ISL_SURF_USAGE_DEPTH_BIT | ISL_SURF_USAGE_TEXTURE_BIT,
776 BO_ALLOC_FOR_RENDER, pitch, bo);
777 if (!mt)
778 return NULL;
779
780 brw_bo_reference(bo);
781
782 if (!(flags & MIPTREE_CREATE_NO_AUX))
783 intel_miptree_choose_aux_usage(brw, mt);
784
785 return mt;
786 } else if (format == MESA_FORMAT_S_UINT8) {
787 mt = make_surface(brw, target, MESA_FORMAT_S_UINT8,
788 0, 0, width, height, depth, 1,
789 ISL_TILING_W_BIT,
790 ISL_SURF_USAGE_STENCIL_BIT |
791 ISL_SURF_USAGE_TEXTURE_BIT,
792 BO_ALLOC_FOR_RENDER, pitch, bo);
793 if (!mt)
794 return NULL;
795
796 assert(bo->size >= mt->surf.size);
797
798 brw_bo_reference(bo);
799 return mt;
800 }
801
802 brw_bo_get_tiling(bo, &tiling, &swizzle);
803
804 /* Nothing will be able to use this miptree with the BO if the offset isn't
805 * aligned.
806 */
807 if (tiling != I915_TILING_NONE)
808 assert(offset % 4096 == 0);
809
810 /* miptrees can't handle negative pitch. If you need flipping of images,
811 * that's outside of the scope of the mt.
812 */
813 assert(pitch >= 0);
814
815 /* The BO already has a tiling format and we shouldn't confuse the lower
816 * layers by making it try to find a tiling format again.
817 */
818 assert((flags & MIPTREE_CREATE_LINEAR) == 0);
819
820 mt = make_surface(brw, target, format,
821 0, 0, width, height, depth, 1,
822 1lu << isl_tiling_from_i915_tiling(tiling),
823 ISL_SURF_USAGE_RENDER_TARGET_BIT |
824 ISL_SURF_USAGE_TEXTURE_BIT,
825 0, pitch, bo);
826 if (!mt)
827 return NULL;
828
829 brw_bo_reference(bo);
830 mt->bo = bo;
831 mt->offset = offset;
832
833 if (!(flags & MIPTREE_CREATE_NO_AUX)) {
834 intel_miptree_choose_aux_usage(brw, mt);
835
836 if (!intel_miptree_alloc_aux(brw, mt)) {
837 intel_miptree_release(&mt);
838 return NULL;
839 }
840 }
841
842 return mt;
843 }
844
845 static struct intel_mipmap_tree *
846 miptree_create_for_planar_image(struct brw_context *brw,
847 __DRIimage *image, GLenum target)
848 {
849 const struct intel_image_format *f = image->planar_format;
850 struct intel_mipmap_tree *planar_mt = NULL;
851
852 for (int i = 0; i < f->nplanes; i++) {
853 const int index = f->planes[i].buffer_index;
854 const uint32_t dri_format = f->planes[i].dri_format;
855 const mesa_format format = driImageFormatToGLFormat(dri_format);
856 const uint32_t width = image->width >> f->planes[i].width_shift;
857 const uint32_t height = image->height >> f->planes[i].height_shift;
858
859 /* Disable creation of the texture's aux buffers because the driver
860 * exposes no EGL API to manage them. That is, there is no API for
861 * resolving the aux buffer's content to the main buffer nor for
862 * invalidating the aux buffer's content.
863 */
864 struct intel_mipmap_tree *mt =
865 intel_miptree_create_for_bo(brw, image->bo, format,
866 image->offsets[index],
867 width, height, 1,
868 image->strides[index],
869 MIPTREE_CREATE_NO_AUX);
870 if (mt == NULL)
871 return NULL;
872
873 mt->target = target;
874
875 if (i == 0)
876 planar_mt = mt;
877 else
878 planar_mt->plane[i - 1] = mt;
879 }
880
881 planar_mt->drm_modifier = image->modifier;
882
883 return planar_mt;
884 }
885
886 static bool
887 create_ccs_buf_for_image(struct brw_context *brw,
888 __DRIimage *image,
889 struct intel_mipmap_tree *mt,
890 enum isl_aux_state initial_state)
891 {
892 struct isl_surf temp_ccs_surf;
893
894 /* CCS is only supported for very simple miptrees */
895 assert(image->aux_offset != 0 && image->aux_pitch != 0);
896 assert(image->tile_x == 0 && image->tile_y == 0);
897 assert(mt->surf.samples == 1);
898 assert(mt->surf.levels == 1);
899 assert(mt->surf.logical_level0_px.depth == 1);
900 assert(mt->surf.logical_level0_px.array_len == 1);
901 assert(mt->first_level == 0);
902 assert(mt->last_level == 0);
903
904 /* We shouldn't already have a CCS */
905 assert(!mt->mcs_buf);
906
907 if (!isl_surf_get_ccs_surf(&brw->isl_dev, &mt->surf, &temp_ccs_surf,
908 image->aux_pitch))
909 return false;
910
911 assert(image->aux_offset < image->bo->size);
912 assert(temp_ccs_surf.size <= image->bo->size - image->aux_offset);
913
914 mt->mcs_buf = calloc(sizeof(*mt->mcs_buf), 1);
915 if (mt->mcs_buf == NULL)
916 return false;
917
918 mt->aux_state = create_aux_state_map(mt, initial_state);
919 if (!mt->aux_state) {
920 free(mt->mcs_buf);
921 mt->mcs_buf = NULL;
922 return false;
923 }
924
925 mt->mcs_buf->bo = image->bo;
926 brw_bo_reference(image->bo);
927
928 mt->mcs_buf->offset = image->aux_offset;
929 mt->mcs_buf->size = image->bo->size - image->aux_offset;
930 mt->mcs_buf->pitch = image->aux_pitch;
931 mt->mcs_buf->qpitch = 0;
932 mt->mcs_buf->surf = temp_ccs_surf;
933
934 return true;
935 }
936
937 struct intel_mipmap_tree *
938 intel_miptree_create_for_dri_image(struct brw_context *brw,
939 __DRIimage *image, GLenum target,
940 enum isl_colorspace colorspace,
941 bool is_winsys_image)
942 {
943 if (image->planar_format && image->planar_format->nplanes > 1) {
944 assert(colorspace == ISL_COLORSPACE_NONE ||
945 colorspace == ISL_COLORSPACE_YUV);
946 return miptree_create_for_planar_image(brw, image, target);
947 }
948
949 if (image->planar_format)
950 assert(image->planar_format->planes[0].dri_format == image->dri_format);
951
952 mesa_format format = image->format;
953 switch (colorspace) {
954 case ISL_COLORSPACE_NONE:
955 /* Keep the image format unmodified */
956 break;
957
958 case ISL_COLORSPACE_LINEAR:
959 format =_mesa_get_srgb_format_linear(format);
960 break;
961
962 case ISL_COLORSPACE_SRGB:
963 format =_mesa_get_linear_format_srgb(format);
964 break;
965
966 default:
967 unreachable("Inalid colorspace for non-planar image");
968 }
969
970 if (!brw->ctx.TextureFormatSupported[format]) {
971 /* The texture storage paths in core Mesa detect if the driver does not
972 * support the user-requested format, and then searches for a
973 * fallback format. The DRIimage code bypasses core Mesa, though. So we
974 * do the fallbacks here for important formats.
975 *
976 * We must support DRM_FOURCC_XBGR8888 textures because the Android
977 * framework produces HAL_PIXEL_FORMAT_RGBX8888 winsys surfaces, which
978 * the Chrome OS compositor consumes as dma_buf EGLImages.
979 */
980 format = _mesa_format_fallback_rgbx_to_rgba(format);
981 }
982
983 if (!brw->ctx.TextureFormatSupported[format])
984 return NULL;
985
986 const struct isl_drm_modifier_info *mod_info =
987 isl_drm_modifier_get_info(image->modifier);
988
989 enum intel_miptree_create_flags mt_create_flags = 0;
990
991 /* If this image comes in from a window system, we have different
992 * requirements than if it comes in via an EGL import operation. Window
993 * system images can use any form of auxiliary compression we wish because
994 * they get "flushed" before being handed off to the window system and we
995 * have the opportunity to do resolves. Non window-system images, on the
996 * other hand, have no resolve point so we can't have aux without a
997 * modifier.
998 */
999 if (!is_winsys_image)
1000 mt_create_flags |= MIPTREE_CREATE_NO_AUX;
1001
1002 /* If we have a modifier which specifies aux, don't create one yet */
1003 if (mod_info && mod_info->aux_usage != ISL_AUX_USAGE_NONE)
1004 mt_create_flags |= MIPTREE_CREATE_NO_AUX;
1005
1006 /* Disable creation of the texture's aux buffers because the driver exposes
1007 * no EGL API to manage them. That is, there is no API for resolving the aux
1008 * buffer's content to the main buffer nor for invalidating the aux buffer's
1009 * content.
1010 */
1011 struct intel_mipmap_tree *mt =
1012 intel_miptree_create_for_bo(brw, image->bo, format,
1013 image->offset, image->width, image->height, 1,
1014 image->pitch, mt_create_flags);
1015 if (mt == NULL)
1016 return NULL;
1017
1018 mt->target = target;
1019 mt->level[0].level_x = image->tile_x;
1020 mt->level[0].level_y = image->tile_y;
1021 mt->drm_modifier = image->modifier;
1022
1023 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
1024 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
1025 * trouble resolving back to destination image due to alignment issues.
1026 */
1027 if (!brw->has_surface_tile_offset) {
1028 uint32_t draw_x, draw_y;
1029 intel_miptree_get_tile_offsets(mt, 0, 0, &draw_x, &draw_y);
1030
1031 if (draw_x != 0 || draw_y != 0) {
1032 _mesa_error(&brw->ctx, GL_INVALID_OPERATION, __func__);
1033 intel_miptree_release(&mt);
1034 return NULL;
1035 }
1036 }
1037
1038 if (mod_info && mod_info->aux_usage != ISL_AUX_USAGE_NONE) {
1039 assert(mod_info->aux_usage == ISL_AUX_USAGE_CCS_E);
1040
1041 mt->aux_usage = mod_info->aux_usage;
1042 /* If we are a window system buffer, then we can support fast-clears
1043 * even if the modifier doesn't support them by doing a partial resolve
1044 * as part of the flush operation.
1045 */
1046 mt->supports_fast_clear =
1047 is_winsys_image || mod_info->supports_clear_color;
1048
1049 /* We don't know the actual state of the surface when we get it but we
1050 * can make a pretty good guess based on the modifier. What we do know
1051 * for sure is that it isn't in the AUX_INVALID state, so we just assume
1052 * a worst case of compression.
1053 */
1054 enum isl_aux_state initial_state =
1055 mod_info->supports_clear_color ? ISL_AUX_STATE_COMPRESSED_CLEAR :
1056 ISL_AUX_STATE_COMPRESSED_NO_CLEAR;
1057
1058 if (!create_ccs_buf_for_image(brw, image, mt, initial_state)) {
1059 intel_miptree_release(&mt);
1060 return NULL;
1061 }
1062 }
1063
1064 /* Don't assume coherency for imported EGLimages. We don't know what
1065 * external clients are going to do with it. They may scan it out.
1066 */
1067 image->bo->cache_coherent = false;
1068
1069 return mt;
1070 }
1071
1072 /**
1073 * For a singlesample renderbuffer, this simply wraps the given BO with a
1074 * miptree.
1075 *
1076 * For a multisample renderbuffer, this wraps the window system's
1077 * (singlesample) BO with a singlesample miptree attached to the
1078 * intel_renderbuffer, then creates a multisample miptree attached to irb->mt
1079 * that will contain the actual rendering (which is lazily resolved to
1080 * irb->singlesample_mt).
1081 */
1082 bool
1083 intel_update_winsys_renderbuffer_miptree(struct brw_context *intel,
1084 struct intel_renderbuffer *irb,
1085 struct intel_mipmap_tree *singlesample_mt,
1086 uint32_t width, uint32_t height,
1087 uint32_t pitch)
1088 {
1089 struct intel_mipmap_tree *multisample_mt = NULL;
1090 struct gl_renderbuffer *rb = &irb->Base.Base;
1091 mesa_format format = rb->Format;
1092 const unsigned num_samples = MAX2(rb->NumSamples, 1);
1093
1094 /* Only the front and back buffers, which are color buffers, are allocated
1095 * through the image loader.
1096 */
1097 assert(_mesa_get_format_base_format(format) == GL_RGB ||
1098 _mesa_get_format_base_format(format) == GL_RGBA);
1099
1100 assert(singlesample_mt);
1101
1102 if (num_samples == 1) {
1103 intel_miptree_release(&irb->mt);
1104 irb->mt = singlesample_mt;
1105
1106 assert(!irb->singlesample_mt);
1107 } else {
1108 intel_miptree_release(&irb->singlesample_mt);
1109 irb->singlesample_mt = singlesample_mt;
1110
1111 if (!irb->mt ||
1112 irb->mt->surf.logical_level0_px.width != width ||
1113 irb->mt->surf.logical_level0_px.height != height) {
1114 multisample_mt = intel_miptree_create_for_renderbuffer(intel,
1115 format,
1116 width,
1117 height,
1118 num_samples);
1119 if (!multisample_mt)
1120 goto fail;
1121
1122 irb->need_downsample = false;
1123 intel_miptree_release(&irb->mt);
1124 irb->mt = multisample_mt;
1125 }
1126 }
1127 return true;
1128
1129 fail:
1130 intel_miptree_release(&irb->mt);
1131 return false;
1132 }
1133
1134 struct intel_mipmap_tree*
1135 intel_miptree_create_for_renderbuffer(struct brw_context *brw,
1136 mesa_format format,
1137 uint32_t width,
1138 uint32_t height,
1139 uint32_t num_samples)
1140 {
1141 struct intel_mipmap_tree *mt;
1142 uint32_t depth = 1;
1143 GLenum target = num_samples > 1 ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
1144
1145 mt = intel_miptree_create(brw, target, format, 0, 0,
1146 width, height, depth, num_samples,
1147 MIPTREE_CREATE_BUSY);
1148 if (!mt)
1149 goto fail;
1150
1151 return mt;
1152
1153 fail:
1154 intel_miptree_release(&mt);
1155 return NULL;
1156 }
1157
1158 void
1159 intel_miptree_reference(struct intel_mipmap_tree **dst,
1160 struct intel_mipmap_tree *src)
1161 {
1162 if (*dst == src)
1163 return;
1164
1165 intel_miptree_release(dst);
1166
1167 if (src) {
1168 src->refcount++;
1169 DBG("%s %p refcount now %d\n", __func__, src, src->refcount);
1170 }
1171
1172 *dst = src;
1173 }
1174
1175 static void
1176 intel_miptree_aux_buffer_free(struct intel_miptree_aux_buffer *aux_buf)
1177 {
1178 if (aux_buf == NULL)
1179 return;
1180
1181 brw_bo_unreference(aux_buf->bo);
1182
1183 free(aux_buf);
1184 }
1185
1186 void
1187 intel_miptree_release(struct intel_mipmap_tree **mt)
1188 {
1189 if (!*mt)
1190 return;
1191
1192 DBG("%s %p refcount will be %d\n", __func__, *mt, (*mt)->refcount - 1);
1193 if (--(*mt)->refcount <= 0) {
1194 GLuint i;
1195
1196 DBG("%s deleting %p\n", __func__, *mt);
1197
1198 brw_bo_unreference((*mt)->bo);
1199 intel_miptree_release(&(*mt)->stencil_mt);
1200 intel_miptree_release(&(*mt)->r8stencil_mt);
1201 intel_miptree_aux_buffer_free((*mt)->hiz_buf);
1202 intel_miptree_aux_buffer_free((*mt)->mcs_buf);
1203 free_aux_state_map((*mt)->aux_state);
1204
1205 intel_miptree_release(&(*mt)->plane[0]);
1206 intel_miptree_release(&(*mt)->plane[1]);
1207
1208 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
1209 free((*mt)->level[i].slice);
1210 }
1211
1212 free(*mt);
1213 }
1214 *mt = NULL;
1215 }
1216
1217
1218 void
1219 intel_get_image_dims(struct gl_texture_image *image,
1220 int *width, int *height, int *depth)
1221 {
1222 switch (image->TexObject->Target) {
1223 case GL_TEXTURE_1D_ARRAY:
1224 /* For a 1D Array texture the OpenGL API will treat the image height as
1225 * the number of array slices. For Intel hardware, we treat the 1D array
1226 * as a 2D Array with a height of 1. So, here we want to swap image
1227 * height and depth.
1228 */
1229 assert(image->Depth == 1);
1230 *width = image->Width;
1231 *height = 1;
1232 *depth = image->Height;
1233 break;
1234 case GL_TEXTURE_CUBE_MAP:
1235 /* For Cube maps, the mesa/main api layer gives us a depth of 1 even
1236 * though we really have 6 slices.
1237 */
1238 assert(image->Depth == 1);
1239 *width = image->Width;
1240 *height = image->Height;
1241 *depth = 6;
1242 break;
1243 default:
1244 *width = image->Width;
1245 *height = image->Height;
1246 *depth = image->Depth;
1247 break;
1248 }
1249 }
1250
1251 /**
1252 * Can the image be pulled into a unified mipmap tree? This mirrors
1253 * the completeness test in a lot of ways.
1254 *
1255 * Not sure whether I want to pass gl_texture_image here.
1256 */
1257 bool
1258 intel_miptree_match_image(struct intel_mipmap_tree *mt,
1259 struct gl_texture_image *image)
1260 {
1261 struct intel_texture_image *intelImage = intel_texture_image(image);
1262 GLuint level = intelImage->base.Base.Level;
1263 int width, height, depth;
1264
1265 /* glTexImage* choose the texture object based on the target passed in, and
1266 * objects can't change targets over their lifetimes, so this should be
1267 * true.
1268 */
1269 assert(image->TexObject->Target == mt->target);
1270
1271 mesa_format mt_format = mt->format;
1272 if (mt->format == MESA_FORMAT_Z24_UNORM_X8_UINT && mt->stencil_mt)
1273 mt_format = MESA_FORMAT_Z24_UNORM_S8_UINT;
1274 if (mt->format == MESA_FORMAT_Z_FLOAT32 && mt->stencil_mt)
1275 mt_format = MESA_FORMAT_Z32_FLOAT_S8X24_UINT;
1276 if (mt->etc_format != MESA_FORMAT_NONE)
1277 mt_format = mt->etc_format;
1278
1279 if (image->TexFormat != mt_format)
1280 return false;
1281
1282 intel_get_image_dims(image, &width, &height, &depth);
1283
1284 if (mt->target == GL_TEXTURE_CUBE_MAP)
1285 depth = 6;
1286
1287 if (level >= mt->surf.levels)
1288 return false;
1289
1290 const unsigned level_depth =
1291 mt->surf.dim == ISL_SURF_DIM_3D ?
1292 minify(mt->surf.logical_level0_px.depth, level) :
1293 mt->surf.logical_level0_px.array_len;
1294
1295 return width == minify(mt->surf.logical_level0_px.width, level) &&
1296 height == minify(mt->surf.logical_level0_px.height, level) &&
1297 depth == level_depth &&
1298 MAX2(image->NumSamples, 1) == mt->surf.samples;
1299 }
1300
1301 void
1302 intel_miptree_get_image_offset(const struct intel_mipmap_tree *mt,
1303 GLuint level, GLuint slice,
1304 GLuint *x, GLuint *y)
1305 {
1306 if (level == 0 && slice == 0) {
1307 *x = mt->level[0].level_x;
1308 *y = mt->level[0].level_y;
1309 return;
1310 }
1311
1312 uint32_t x_offset_sa, y_offset_sa;
1313
1314 /* Miptree itself can have an offset only if it represents a single
1315 * slice in an imported buffer object.
1316 * See intel_miptree_create_for_dri_image().
1317 */
1318 assert(mt->level[0].level_x == 0);
1319 assert(mt->level[0].level_y == 0);
1320
1321 /* Given level is relative to level zero while the miptree may be
1322 * represent just a subset of all levels starting from 'first_level'.
1323 */
1324 assert(level >= mt->first_level);
1325 level -= mt->first_level;
1326
1327 const unsigned z = mt->surf.dim == ISL_SURF_DIM_3D ? slice : 0;
1328 slice = mt->surf.dim == ISL_SURF_DIM_3D ? 0 : slice;
1329 isl_surf_get_image_offset_el(&mt->surf, level, slice, z,
1330 &x_offset_sa, &y_offset_sa);
1331
1332 *x = x_offset_sa;
1333 *y = y_offset_sa;
1334 }
1335
1336
1337 /**
1338 * This function computes the tile_w (in bytes) and tile_h (in rows) of
1339 * different tiling patterns. If the BO is untiled, tile_w is set to cpp
1340 * and tile_h is set to 1.
1341 */
1342 void
1343 intel_get_tile_dims(enum isl_tiling tiling, uint32_t cpp,
1344 uint32_t *tile_w, uint32_t *tile_h)
1345 {
1346 switch (tiling) {
1347 case ISL_TILING_X:
1348 *tile_w = 512;
1349 *tile_h = 8;
1350 break;
1351 case ISL_TILING_Y0:
1352 *tile_w = 128;
1353 *tile_h = 32;
1354 break;
1355 case ISL_TILING_LINEAR:
1356 *tile_w = cpp;
1357 *tile_h = 1;
1358 break;
1359 default:
1360 unreachable("not reached");
1361 }
1362 }
1363
1364
1365 /**
1366 * This function computes masks that may be used to select the bits of the X
1367 * and Y coordinates that indicate the offset within a tile. If the BO is
1368 * untiled, the masks are set to 0.
1369 */
1370 void
1371 intel_get_tile_masks(enum isl_tiling tiling, uint32_t cpp,
1372 uint32_t *mask_x, uint32_t *mask_y)
1373 {
1374 uint32_t tile_w_bytes, tile_h;
1375
1376 intel_get_tile_dims(tiling, cpp, &tile_w_bytes, &tile_h);
1377
1378 *mask_x = tile_w_bytes / cpp - 1;
1379 *mask_y = tile_h - 1;
1380 }
1381
1382 /**
1383 * Compute the offset (in bytes) from the start of the BO to the given x
1384 * and y coordinate. For tiled BOs, caller must ensure that x and y are
1385 * multiples of the tile size.
1386 */
1387 uint32_t
1388 intel_miptree_get_aligned_offset(const struct intel_mipmap_tree *mt,
1389 uint32_t x, uint32_t y)
1390 {
1391 int cpp = mt->cpp;
1392 uint32_t pitch = mt->surf.row_pitch;
1393
1394 switch (mt->surf.tiling) {
1395 default:
1396 unreachable("not reached");
1397 case ISL_TILING_LINEAR:
1398 return y * pitch + x * cpp;
1399 case ISL_TILING_X:
1400 assert((x % (512 / cpp)) == 0);
1401 assert((y % 8) == 0);
1402 return y * pitch + x / (512 / cpp) * 4096;
1403 case ISL_TILING_Y0:
1404 assert((x % (128 / cpp)) == 0);
1405 assert((y % 32) == 0);
1406 return y * pitch + x / (128 / cpp) * 4096;
1407 }
1408 }
1409
1410 /**
1411 * Rendering with tiled buffers requires that the base address of the buffer
1412 * be aligned to a page boundary. For renderbuffers, and sometimes with
1413 * textures, we may want the surface to point at a texture image level that
1414 * isn't at a page boundary.
1415 *
1416 * This function returns an appropriately-aligned base offset
1417 * according to the tiling restrictions, plus any required x/y offset
1418 * from there.
1419 */
1420 uint32_t
1421 intel_miptree_get_tile_offsets(const struct intel_mipmap_tree *mt,
1422 GLuint level, GLuint slice,
1423 uint32_t *tile_x,
1424 uint32_t *tile_y)
1425 {
1426 uint32_t x, y;
1427 uint32_t mask_x, mask_y;
1428
1429 intel_get_tile_masks(mt->surf.tiling, mt->cpp, &mask_x, &mask_y);
1430 intel_miptree_get_image_offset(mt, level, slice, &x, &y);
1431
1432 *tile_x = x & mask_x;
1433 *tile_y = y & mask_y;
1434
1435 return intel_miptree_get_aligned_offset(mt, x & ~mask_x, y & ~mask_y);
1436 }
1437
1438 static void
1439 intel_miptree_copy_slice_sw(struct brw_context *brw,
1440 struct intel_mipmap_tree *src_mt,
1441 unsigned src_level, unsigned src_layer,
1442 struct intel_mipmap_tree *dst_mt,
1443 unsigned dst_level, unsigned dst_layer,
1444 unsigned width, unsigned height)
1445 {
1446 void *src, *dst;
1447 ptrdiff_t src_stride, dst_stride;
1448 const unsigned cpp = (isl_format_get_layout(dst_mt->surf.format)->bpb / 8);
1449
1450 intel_miptree_map(brw, src_mt,
1451 src_level, src_layer,
1452 0, 0,
1453 width, height,
1454 GL_MAP_READ_BIT | BRW_MAP_DIRECT_BIT,
1455 &src, &src_stride);
1456
1457 intel_miptree_map(brw, dst_mt,
1458 dst_level, dst_layer,
1459 0, 0,
1460 width, height,
1461 GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT |
1462 BRW_MAP_DIRECT_BIT,
1463 &dst, &dst_stride);
1464
1465 DBG("sw blit %s mt %p %p/%"PRIdPTR" -> %s mt %p %p/%"PRIdPTR" (%dx%d)\n",
1466 _mesa_get_format_name(src_mt->format),
1467 src_mt, src, src_stride,
1468 _mesa_get_format_name(dst_mt->format),
1469 dst_mt, dst, dst_stride,
1470 width, height);
1471
1472 int row_size = cpp * width;
1473 if (src_stride == row_size &&
1474 dst_stride == row_size) {
1475 memcpy(dst, src, row_size * height);
1476 } else {
1477 for (int i = 0; i < height; i++) {
1478 memcpy(dst, src, row_size);
1479 dst += dst_stride;
1480 src += src_stride;
1481 }
1482 }
1483
1484 intel_miptree_unmap(brw, dst_mt, dst_level, dst_layer);
1485 intel_miptree_unmap(brw, src_mt, src_level, src_layer);
1486
1487 /* Don't forget to copy the stencil data over, too. We could have skipped
1488 * passing BRW_MAP_DIRECT_BIT, but that would have meant intel_miptree_map
1489 * shuffling the two data sources in/out of temporary storage instead of
1490 * the direct mapping we get this way.
1491 */
1492 if (dst_mt->stencil_mt) {
1493 assert(src_mt->stencil_mt);
1494 intel_miptree_copy_slice_sw(brw,
1495 src_mt->stencil_mt, src_level, src_layer,
1496 dst_mt->stencil_mt, dst_level, dst_layer,
1497 width, height);
1498 }
1499 }
1500
1501 void
1502 intel_miptree_copy_slice(struct brw_context *brw,
1503 struct intel_mipmap_tree *src_mt,
1504 unsigned src_level, unsigned src_layer,
1505 struct intel_mipmap_tree *dst_mt,
1506 unsigned dst_level, unsigned dst_layer)
1507
1508 {
1509 mesa_format format = src_mt->format;
1510 unsigned width = minify(src_mt->surf.phys_level0_sa.width,
1511 src_level - src_mt->first_level);
1512 unsigned height = minify(src_mt->surf.phys_level0_sa.height,
1513 src_level - src_mt->first_level);
1514
1515 assert(src_layer < get_num_phys_layers(&src_mt->surf,
1516 src_level - src_mt->first_level));
1517
1518 assert(src_mt->format == dst_mt->format);
1519
1520 if (dst_mt->compressed) {
1521 unsigned int i, j;
1522 _mesa_get_format_block_size(dst_mt->format, &i, &j);
1523 height = ALIGN_NPOT(height, j) / j;
1524 width = ALIGN_NPOT(width, i) / i;
1525 }
1526
1527 /* If it's a packed depth/stencil buffer with separate stencil, the blit
1528 * below won't apply since we can't do the depth's Y tiling or the
1529 * stencil's W tiling in the blitter.
1530 */
1531 if (src_mt->stencil_mt) {
1532 intel_miptree_copy_slice_sw(brw,
1533 src_mt, src_level, src_layer,
1534 dst_mt, dst_level, dst_layer,
1535 width, height);
1536 return;
1537 }
1538
1539 uint32_t dst_x, dst_y, src_x, src_y;
1540 intel_miptree_get_image_offset(dst_mt, dst_level, dst_layer,
1541 &dst_x, &dst_y);
1542 intel_miptree_get_image_offset(src_mt, src_level, src_layer,
1543 &src_x, &src_y);
1544
1545 DBG("validate blit mt %s %p %d,%d/%d -> mt %s %p %d,%d/%d (%dx%d)\n",
1546 _mesa_get_format_name(src_mt->format),
1547 src_mt, src_x, src_y, src_mt->surf.row_pitch,
1548 _mesa_get_format_name(dst_mt->format),
1549 dst_mt, dst_x, dst_y, dst_mt->surf.row_pitch,
1550 width, height);
1551
1552 if (!intel_miptree_blit(brw,
1553 src_mt, src_level, src_layer, 0, 0, false,
1554 dst_mt, dst_level, dst_layer, 0, 0, false,
1555 width, height, GL_COPY)) {
1556 perf_debug("miptree validate blit for %s failed\n",
1557 _mesa_get_format_name(format));
1558
1559 intel_miptree_copy_slice_sw(brw,
1560 src_mt, src_level, src_layer,
1561 dst_mt, dst_level, dst_layer,
1562 width, height);
1563 }
1564 }
1565
1566 /**
1567 * Copies the image's current data to the given miptree, and associates that
1568 * miptree with the image.
1569 *
1570 * If \c invalidate is true, then the actual image data does not need to be
1571 * copied, but the image still needs to be associated to the new miptree (this
1572 * is set to true if we're about to clear the image).
1573 */
1574 void
1575 intel_miptree_copy_teximage(struct brw_context *brw,
1576 struct intel_texture_image *intelImage,
1577 struct intel_mipmap_tree *dst_mt,
1578 bool invalidate)
1579 {
1580 struct intel_mipmap_tree *src_mt = intelImage->mt;
1581 struct intel_texture_object *intel_obj =
1582 intel_texture_object(intelImage->base.Base.TexObject);
1583 int level = intelImage->base.Base.Level;
1584 const unsigned face = intelImage->base.Base.Face;
1585 unsigned start_layer, end_layer;
1586
1587 if (intel_obj->base.Target == GL_TEXTURE_1D_ARRAY) {
1588 assert(face == 0);
1589 assert(intelImage->base.Base.Height);
1590 start_layer = 0;
1591 end_layer = intelImage->base.Base.Height - 1;
1592 } else if (face > 0) {
1593 start_layer = face;
1594 end_layer = face;
1595 } else {
1596 assert(intelImage->base.Base.Depth);
1597 start_layer = 0;
1598 end_layer = intelImage->base.Base.Depth - 1;
1599 }
1600
1601 if (!invalidate) {
1602 for (unsigned i = start_layer; i <= end_layer; i++) {
1603 intel_miptree_copy_slice(brw,
1604 src_mt, level, i,
1605 dst_mt, level, i);
1606 }
1607 }
1608
1609 intel_miptree_reference(&intelImage->mt, dst_mt);
1610 intel_obj->needs_validate = true;
1611 }
1612
1613 static void
1614 intel_miptree_init_mcs(struct brw_context *brw,
1615 struct intel_mipmap_tree *mt,
1616 int init_value)
1617 {
1618 assert(mt->mcs_buf != NULL);
1619
1620 /* From the Ivy Bridge PRM, Vol 2 Part 1 p326:
1621 *
1622 * When MCS buffer is enabled and bound to MSRT, it is required that it
1623 * is cleared prior to any rendering.
1624 *
1625 * Since we don't use the MCS buffer for any purpose other than rendering,
1626 * it makes sense to just clear it immediately upon allocation.
1627 *
1628 * Note: the clear value for MCS buffers is all 1's, so we memset to 0xff.
1629 */
1630 void *map = brw_bo_map(brw, mt->mcs_buf->bo, MAP_WRITE);
1631 if (unlikely(map == NULL)) {
1632 fprintf(stderr, "Failed to map mcs buffer into GTT\n");
1633 brw_bo_unreference(mt->mcs_buf->bo);
1634 free(mt->mcs_buf);
1635 return;
1636 }
1637 void *data = map;
1638 memset(data, init_value, mt->mcs_buf->size);
1639 brw_bo_unmap(mt->mcs_buf->bo);
1640 }
1641
1642 static struct intel_miptree_aux_buffer *
1643 intel_alloc_aux_buffer(struct brw_context *brw,
1644 const char *name,
1645 const struct isl_surf *aux_surf,
1646 uint32_t alloc_flags,
1647 struct intel_mipmap_tree *mt)
1648 {
1649 struct intel_miptree_aux_buffer *buf = calloc(sizeof(*buf), 1);
1650 if (!buf)
1651 return false;
1652
1653 buf->size = aux_surf->size;
1654 buf->pitch = aux_surf->row_pitch;
1655 buf->qpitch = isl_surf_get_array_pitch_sa_rows(aux_surf);
1656
1657 /* ISL has stricter set of alignment rules then the drm allocator.
1658 * Therefore one can pass the ISL dimensions in terms of bytes instead of
1659 * trying to recalculate based on different format block sizes.
1660 */
1661 buf->bo = brw_bo_alloc_tiled(brw->bufmgr, name, buf->size,
1662 I915_TILING_Y, buf->pitch, alloc_flags);
1663 if (!buf->bo) {
1664 free(buf);
1665 return NULL;
1666 }
1667
1668 buf->surf = *aux_surf;
1669
1670 return buf;
1671 }
1672
1673 static bool
1674 intel_miptree_alloc_mcs(struct brw_context *brw,
1675 struct intel_mipmap_tree *mt,
1676 GLuint num_samples)
1677 {
1678 assert(brw->gen >= 7); /* MCS only used on Gen7+ */
1679 assert(mt->mcs_buf == NULL);
1680 assert(mt->aux_usage == ISL_AUX_USAGE_MCS);
1681
1682 /* Multisampled miptrees are only supported for single level. */
1683 assert(mt->first_level == 0);
1684 enum isl_aux_state **aux_state =
1685 create_aux_state_map(mt, ISL_AUX_STATE_CLEAR);
1686 if (!aux_state)
1687 return false;
1688
1689 struct isl_surf temp_mcs_surf;
1690
1691 MAYBE_UNUSED bool ok =
1692 isl_surf_get_mcs_surf(&brw->isl_dev, &mt->surf, &temp_mcs_surf);
1693 assert(ok);
1694
1695 /* Buffer needs to be initialised requiring the buffer to be immediately
1696 * mapped to cpu space for writing. Therefore do not use the gpu access
1697 * flag which can cause an unnecessary delay if the backing pages happened
1698 * to be just used by the GPU.
1699 */
1700 const uint32_t alloc_flags = 0;
1701 mt->mcs_buf = intel_alloc_aux_buffer(brw, "mcs-miptree",
1702 &temp_mcs_surf, alloc_flags, mt);
1703 if (!mt->mcs_buf) {
1704 free(aux_state);
1705 return false;
1706 }
1707
1708 mt->aux_state = aux_state;
1709
1710 intel_miptree_init_mcs(brw, mt, 0xFF);
1711
1712 return true;
1713 }
1714
1715 bool
1716 intel_miptree_alloc_ccs(struct brw_context *brw,
1717 struct intel_mipmap_tree *mt)
1718 {
1719 assert(mt->mcs_buf == NULL);
1720 assert(mt->aux_usage == ISL_AUX_USAGE_CCS_E ||
1721 mt->aux_usage == ISL_AUX_USAGE_CCS_D);
1722
1723 struct isl_surf temp_ccs_surf;
1724
1725 if (!isl_surf_get_ccs_surf(&brw->isl_dev, &mt->surf, &temp_ccs_surf, 0))
1726 return false;
1727
1728 assert(temp_ccs_surf.size &&
1729 (temp_ccs_surf.size % temp_ccs_surf.row_pitch == 0));
1730
1731 enum isl_aux_state **aux_state =
1732 create_aux_state_map(mt, ISL_AUX_STATE_PASS_THROUGH);
1733 if (!aux_state)
1734 return false;
1735
1736 /* When CCS_E is used, we need to ensure that the CCS starts off in a valid
1737 * state. From the Sky Lake PRM, "MCS Buffer for Render Target(s)":
1738 *
1739 * "If Software wants to enable Color Compression without Fast clear,
1740 * Software needs to initialize MCS with zeros."
1741 *
1742 * A CCS value of 0 indicates that the corresponding block is in the
1743 * pass-through state which is what we want.
1744 *
1745 * For CCS_D, on the other hand, we don't care as we're about to perform a
1746 * fast-clear operation. In that case, being hot in caches more useful.
1747 */
1748 const uint32_t alloc_flags = mt->aux_usage == ISL_AUX_USAGE_CCS_E ?
1749 BO_ALLOC_ZEROED : BO_ALLOC_FOR_RENDER;
1750 mt->mcs_buf = intel_alloc_aux_buffer(brw, "ccs-miptree",
1751 &temp_ccs_surf, alloc_flags, mt);
1752 if (!mt->mcs_buf) {
1753 free(aux_state);
1754 return false;
1755 }
1756
1757 mt->aux_state = aux_state;
1758
1759 return true;
1760 }
1761
1762 /**
1763 * Helper for intel_miptree_alloc_hiz() that sets
1764 * \c mt->level[level].has_hiz. Return true if and only if
1765 * \c has_hiz was set.
1766 */
1767 static bool
1768 intel_miptree_level_enable_hiz(struct brw_context *brw,
1769 struct intel_mipmap_tree *mt,
1770 uint32_t level)
1771 {
1772 assert(mt->hiz_buf);
1773 assert(mt->surf.size > 0);
1774
1775 if (brw->gen >= 8 || brw->is_haswell) {
1776 uint32_t width = minify(mt->surf.phys_level0_sa.width, level);
1777 uint32_t height = minify(mt->surf.phys_level0_sa.height, level);
1778
1779 /* Disable HiZ for LOD > 0 unless the width is 8 aligned
1780 * and the height is 4 aligned. This allows our HiZ support
1781 * to fulfill Haswell restrictions for HiZ ops. For LOD == 0,
1782 * we can grow the width & height to allow the HiZ op to
1783 * force the proper size alignments.
1784 */
1785 if (level > 0 && ((width & 7) || (height & 3))) {
1786 DBG("mt %p level %d: HiZ DISABLED\n", mt, level);
1787 return false;
1788 }
1789 }
1790
1791 DBG("mt %p level %d: HiZ enabled\n", mt, level);
1792 mt->level[level].has_hiz = true;
1793 return true;
1794 }
1795
1796 bool
1797 intel_miptree_alloc_hiz(struct brw_context *brw,
1798 struct intel_mipmap_tree *mt)
1799 {
1800 assert(mt->hiz_buf == NULL);
1801 assert(mt->aux_usage == ISL_AUX_USAGE_HIZ);
1802
1803 enum isl_aux_state **aux_state =
1804 create_aux_state_map(mt, ISL_AUX_STATE_AUX_INVALID);
1805 if (!aux_state)
1806 return false;
1807
1808 struct isl_surf temp_hiz_surf;
1809
1810 MAYBE_UNUSED bool ok =
1811 isl_surf_get_hiz_surf(&brw->isl_dev, &mt->surf, &temp_hiz_surf);
1812 assert(ok);
1813
1814 const uint32_t alloc_flags = BO_ALLOC_FOR_RENDER;
1815 mt->hiz_buf = intel_alloc_aux_buffer(brw, "hiz-miptree",
1816 &temp_hiz_surf, alloc_flags, mt);
1817
1818 if (!mt->hiz_buf) {
1819 free(aux_state);
1820 return false;
1821 }
1822
1823 for (unsigned level = mt->first_level; level <= mt->last_level; ++level)
1824 intel_miptree_level_enable_hiz(brw, mt, level);
1825
1826 mt->aux_state = aux_state;
1827
1828 return true;
1829 }
1830
1831
1832 /**
1833 * Allocate the initial aux surface for a miptree based on mt->aux_usage
1834 *
1835 * Since MCS, HiZ, and CCS_E can compress more than just clear color, we
1836 * create the auxiliary surfaces up-front. CCS_D, on the other hand, can only
1837 * compress clear color so we wait until an actual fast-clear to allocate it.
1838 */
1839 static bool
1840 intel_miptree_alloc_aux(struct brw_context *brw,
1841 struct intel_mipmap_tree *mt)
1842 {
1843 switch (mt->aux_usage) {
1844 case ISL_AUX_USAGE_NONE:
1845 return true;
1846
1847 case ISL_AUX_USAGE_HIZ:
1848 assert(!_mesa_is_format_color_format(mt->format));
1849 if (!intel_miptree_alloc_hiz(brw, mt))
1850 return false;
1851 return true;
1852
1853 case ISL_AUX_USAGE_MCS:
1854 assert(_mesa_is_format_color_format(mt->format));
1855 assert(mt->surf.samples > 1);
1856 if (!intel_miptree_alloc_mcs(brw, mt, mt->surf.samples))
1857 return false;
1858 return true;
1859
1860 case ISL_AUX_USAGE_CCS_D:
1861 /* Since CCS_D can only compress clear color so we wait until an actual
1862 * fast-clear to allocate it.
1863 */
1864 return true;
1865
1866 case ISL_AUX_USAGE_CCS_E:
1867 assert(_mesa_is_format_color_format(mt->format));
1868 assert(mt->surf.samples == 1);
1869 if (!intel_miptree_alloc_ccs(brw, mt))
1870 return false;
1871 return true;
1872 }
1873
1874 unreachable("Invalid aux usage");
1875 }
1876
1877
1878 /**
1879 * Can the miptree sample using the hiz buffer?
1880 */
1881 bool
1882 intel_miptree_sample_with_hiz(struct brw_context *brw,
1883 struct intel_mipmap_tree *mt)
1884 {
1885 /* It's unclear how well supported sampling from the hiz buffer is on GEN8,
1886 * so keep things conservative for now and never enable it unless we're SKL+.
1887 */
1888 if (brw->gen < 9) {
1889 return false;
1890 }
1891
1892 if (!mt->hiz_buf) {
1893 return false;
1894 }
1895
1896 /* It seems the hardware won't fallback to the depth buffer if some of the
1897 * mipmap levels aren't available in the HiZ buffer. So we need all levels
1898 * of the texture to be HiZ enabled.
1899 */
1900 for (unsigned level = 0; level < mt->surf.levels; ++level) {
1901 if (!intel_miptree_level_has_hiz(mt, level))
1902 return false;
1903 }
1904
1905 /* If compressed multisampling is enabled, then we use it for the auxiliary
1906 * buffer instead.
1907 *
1908 * From the BDW PRM (Volume 2d: Command Reference: Structures
1909 * RENDER_SURFACE_STATE.AuxiliarySurfaceMode):
1910 *
1911 * "If this field is set to AUX_HIZ, Number of Multisamples must be
1912 * MULTISAMPLECOUNT_1, and Surface Type cannot be SURFTYPE_3D.
1913 *
1914 * There is no such blurb for 1D textures, but there is sufficient evidence
1915 * that this is broken on SKL+.
1916 */
1917 return (mt->surf.samples == 1 &&
1918 mt->target != GL_TEXTURE_3D &&
1919 mt->target != GL_TEXTURE_1D /* gen9+ restriction */);
1920 }
1921
1922 /**
1923 * Does the miptree slice have hiz enabled?
1924 */
1925 bool
1926 intel_miptree_level_has_hiz(const struct intel_mipmap_tree *mt, uint32_t level)
1927 {
1928 intel_miptree_check_level_layer(mt, level, 0);
1929 return mt->level[level].has_hiz;
1930 }
1931
1932 static inline uint32_t
1933 miptree_level_range_length(const struct intel_mipmap_tree *mt,
1934 uint32_t start_level, uint32_t num_levels)
1935 {
1936 assert(start_level >= mt->first_level);
1937 assert(start_level <= mt->last_level);
1938
1939 if (num_levels == INTEL_REMAINING_LAYERS)
1940 num_levels = mt->last_level - start_level + 1;
1941 /* Check for overflow */
1942 assert(start_level + num_levels >= start_level);
1943 assert(start_level + num_levels <= mt->last_level + 1);
1944
1945 return num_levels;
1946 }
1947
1948 static inline uint32_t
1949 miptree_layer_range_length(const struct intel_mipmap_tree *mt, uint32_t level,
1950 uint32_t start_layer, uint32_t num_layers)
1951 {
1952 assert(level <= mt->last_level);
1953
1954 const uint32_t total_num_layers = brw_get_num_logical_layers(mt, level);
1955 assert(start_layer < total_num_layers);
1956 if (num_layers == INTEL_REMAINING_LAYERS)
1957 num_layers = total_num_layers - start_layer;
1958 /* Check for overflow */
1959 assert(start_layer + num_layers >= start_layer);
1960 assert(start_layer + num_layers <= total_num_layers);
1961
1962 return num_layers;
1963 }
1964
1965 bool
1966 intel_miptree_has_color_unresolved(const struct intel_mipmap_tree *mt,
1967 unsigned start_level, unsigned num_levels,
1968 unsigned start_layer, unsigned num_layers)
1969 {
1970 assert(_mesa_is_format_color_format(mt->format));
1971
1972 if (!mt->mcs_buf)
1973 return false;
1974
1975 /* Clamp the level range to fit the miptree */
1976 num_levels = miptree_level_range_length(mt, start_level, num_levels);
1977
1978 for (uint32_t l = 0; l < num_levels; l++) {
1979 const uint32_t level = start_level + l;
1980 const uint32_t level_layers =
1981 miptree_layer_range_length(mt, level, start_layer, num_layers);
1982 for (unsigned a = 0; a < level_layers; a++) {
1983 enum isl_aux_state aux_state =
1984 intel_miptree_get_aux_state(mt, level, start_layer + a);
1985 assert(aux_state != ISL_AUX_STATE_AUX_INVALID);
1986 if (aux_state != ISL_AUX_STATE_PASS_THROUGH)
1987 return true;
1988 }
1989 }
1990
1991 return false;
1992 }
1993
1994 static void
1995 intel_miptree_check_color_resolve(const struct brw_context *brw,
1996 const struct intel_mipmap_tree *mt,
1997 unsigned level, unsigned layer)
1998 {
1999
2000 if (!mt->mcs_buf)
2001 return;
2002
2003 /* Fast color clear is supported for mipmapped surfaces only on Gen8+. */
2004 assert(brw->gen >= 8 ||
2005 (level == 0 && mt->first_level == 0 && mt->last_level == 0));
2006
2007 /* Compression of arrayed msaa surfaces is supported. */
2008 if (mt->surf.samples > 1)
2009 return;
2010
2011 /* Fast color clear is supported for non-msaa arrays only on Gen8+. */
2012 assert(brw->gen >= 8 ||
2013 (layer == 0 &&
2014 mt->surf.logical_level0_px.depth == 1 &&
2015 mt->surf.logical_level0_px.array_len == 1));
2016
2017 (void)level;
2018 (void)layer;
2019 }
2020
2021 static enum blorp_fast_clear_op
2022 get_ccs_d_resolve_op(enum isl_aux_state aux_state,
2023 enum isl_aux_usage aux_usage,
2024 bool fast_clear_supported)
2025 {
2026 assert(aux_usage == ISL_AUX_USAGE_NONE || aux_usage == ISL_AUX_USAGE_CCS_D);
2027
2028 const bool ccs_supported = aux_usage == ISL_AUX_USAGE_CCS_D;
2029
2030 assert(ccs_supported == fast_clear_supported);
2031
2032 switch (aux_state) {
2033 case ISL_AUX_STATE_CLEAR:
2034 case ISL_AUX_STATE_PARTIAL_CLEAR:
2035 if (!ccs_supported)
2036 return BLORP_FAST_CLEAR_OP_RESOLVE_FULL;
2037 else
2038 return BLORP_FAST_CLEAR_OP_NONE;
2039
2040 case ISL_AUX_STATE_PASS_THROUGH:
2041 return BLORP_FAST_CLEAR_OP_NONE;
2042
2043 case ISL_AUX_STATE_RESOLVED:
2044 case ISL_AUX_STATE_AUX_INVALID:
2045 case ISL_AUX_STATE_COMPRESSED_CLEAR:
2046 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
2047 break;
2048 }
2049
2050 unreachable("Invalid aux state for CCS_D");
2051 }
2052
2053 static enum blorp_fast_clear_op
2054 get_ccs_e_resolve_op(enum isl_aux_state aux_state,
2055 enum isl_aux_usage aux_usage,
2056 bool fast_clear_supported)
2057 {
2058 /* CCS_E surfaces can be accessed as CCS_D if we're careful. */
2059 assert(aux_usage == ISL_AUX_USAGE_NONE ||
2060 aux_usage == ISL_AUX_USAGE_CCS_D ||
2061 aux_usage == ISL_AUX_USAGE_CCS_E);
2062
2063 if (aux_usage == ISL_AUX_USAGE_CCS_D)
2064 assert(fast_clear_supported);
2065
2066 switch (aux_state) {
2067 case ISL_AUX_STATE_CLEAR:
2068 case ISL_AUX_STATE_PARTIAL_CLEAR:
2069 if (fast_clear_supported)
2070 return BLORP_FAST_CLEAR_OP_NONE;
2071 else if (aux_usage == ISL_AUX_USAGE_CCS_E)
2072 return BLORP_FAST_CLEAR_OP_RESOLVE_PARTIAL;
2073 else
2074 return BLORP_FAST_CLEAR_OP_RESOLVE_FULL;
2075
2076 case ISL_AUX_STATE_COMPRESSED_CLEAR:
2077 if (aux_usage != ISL_AUX_USAGE_CCS_E)
2078 return BLORP_FAST_CLEAR_OP_RESOLVE_FULL;
2079 else if (!fast_clear_supported)
2080 return BLORP_FAST_CLEAR_OP_RESOLVE_PARTIAL;
2081 else
2082 return BLORP_FAST_CLEAR_OP_NONE;
2083
2084 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
2085 if (aux_usage != ISL_AUX_USAGE_CCS_E)
2086 return BLORP_FAST_CLEAR_OP_RESOLVE_FULL;
2087 else
2088 return BLORP_FAST_CLEAR_OP_NONE;
2089
2090 case ISL_AUX_STATE_PASS_THROUGH:
2091 return BLORP_FAST_CLEAR_OP_NONE;
2092
2093 case ISL_AUX_STATE_RESOLVED:
2094 case ISL_AUX_STATE_AUX_INVALID:
2095 break;
2096 }
2097
2098 unreachable("Invalid aux state for CCS_E");
2099 }
2100
2101 static void
2102 intel_miptree_prepare_ccs_access(struct brw_context *brw,
2103 struct intel_mipmap_tree *mt,
2104 uint32_t level, uint32_t layer,
2105 enum isl_aux_usage aux_usage,
2106 bool fast_clear_supported)
2107 {
2108 enum isl_aux_state aux_state = intel_miptree_get_aux_state(mt, level, layer);
2109
2110 enum blorp_fast_clear_op resolve_op;
2111 if (mt->aux_usage == ISL_AUX_USAGE_CCS_E) {
2112 resolve_op = get_ccs_e_resolve_op(aux_state, aux_usage,
2113 fast_clear_supported);
2114 } else {
2115 assert(mt->aux_usage == ISL_AUX_USAGE_CCS_D);
2116 resolve_op = get_ccs_d_resolve_op(aux_state, aux_usage,
2117 fast_clear_supported);
2118 }
2119
2120 if (resolve_op != BLORP_FAST_CLEAR_OP_NONE) {
2121 intel_miptree_check_color_resolve(brw, mt, level, layer);
2122 brw_blorp_resolve_color(brw, mt, level, layer, resolve_op);
2123
2124 switch (resolve_op) {
2125 case BLORP_FAST_CLEAR_OP_RESOLVE_FULL:
2126 /* The CCS full resolve operation destroys the CCS and sets it to the
2127 * pass-through state. (You can also think of this as being both a
2128 * resolve and an ambiguate in one operation.)
2129 */
2130 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2131 ISL_AUX_STATE_PASS_THROUGH);
2132 break;
2133
2134 case BLORP_FAST_CLEAR_OP_RESOLVE_PARTIAL:
2135 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2136 ISL_AUX_STATE_COMPRESSED_NO_CLEAR);
2137 break;
2138
2139 default:
2140 unreachable("Invalid resolve op");
2141 }
2142 }
2143 }
2144
2145 static void
2146 intel_miptree_finish_ccs_write(struct brw_context *brw,
2147 struct intel_mipmap_tree *mt,
2148 uint32_t level, uint32_t layer,
2149 enum isl_aux_usage aux_usage)
2150 {
2151 assert(aux_usage == ISL_AUX_USAGE_NONE ||
2152 aux_usage == ISL_AUX_USAGE_CCS_D ||
2153 aux_usage == ISL_AUX_USAGE_CCS_E);
2154
2155 enum isl_aux_state aux_state = intel_miptree_get_aux_state(mt, level, layer);
2156
2157 if (mt->aux_usage == ISL_AUX_USAGE_CCS_E) {
2158 switch (aux_state) {
2159 case ISL_AUX_STATE_CLEAR:
2160 case ISL_AUX_STATE_PARTIAL_CLEAR:
2161 assert(aux_usage == ISL_AUX_USAGE_CCS_E ||
2162 aux_usage == ISL_AUX_USAGE_CCS_D);
2163
2164 if (aux_usage == ISL_AUX_USAGE_CCS_E) {
2165 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2166 ISL_AUX_STATE_COMPRESSED_CLEAR);
2167 } else if (aux_state != ISL_AUX_STATE_PARTIAL_CLEAR) {
2168 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2169 ISL_AUX_STATE_PARTIAL_CLEAR);
2170 }
2171 break;
2172
2173 case ISL_AUX_STATE_COMPRESSED_CLEAR:
2174 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
2175 assert(aux_usage == ISL_AUX_USAGE_CCS_E);
2176 break; /* Nothing to do */
2177
2178 case ISL_AUX_STATE_PASS_THROUGH:
2179 if (aux_usage == ISL_AUX_USAGE_CCS_E) {
2180 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2181 ISL_AUX_STATE_COMPRESSED_NO_CLEAR);
2182 } else {
2183 /* Nothing to do */
2184 }
2185 break;
2186
2187 case ISL_AUX_STATE_RESOLVED:
2188 case ISL_AUX_STATE_AUX_INVALID:
2189 unreachable("Invalid aux state for CCS_E");
2190 }
2191 } else {
2192 assert(mt->aux_usage == ISL_AUX_USAGE_CCS_D);
2193 /* CCS_D is a bit simpler */
2194 switch (aux_state) {
2195 case ISL_AUX_STATE_CLEAR:
2196 assert(aux_usage == ISL_AUX_USAGE_CCS_D);
2197 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2198 ISL_AUX_STATE_PARTIAL_CLEAR);
2199 break;
2200
2201 case ISL_AUX_STATE_PARTIAL_CLEAR:
2202 assert(aux_usage == ISL_AUX_USAGE_CCS_D);
2203 break; /* Nothing to do */
2204
2205 case ISL_AUX_STATE_PASS_THROUGH:
2206 /* Nothing to do */
2207 break;
2208
2209 case ISL_AUX_STATE_COMPRESSED_CLEAR:
2210 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
2211 case ISL_AUX_STATE_RESOLVED:
2212 case ISL_AUX_STATE_AUX_INVALID:
2213 unreachable("Invalid aux state for CCS_D");
2214 }
2215 }
2216 }
2217
2218 static void
2219 intel_miptree_prepare_mcs_access(struct brw_context *brw,
2220 struct intel_mipmap_tree *mt,
2221 uint32_t layer,
2222 enum isl_aux_usage aux_usage,
2223 bool fast_clear_supported)
2224 {
2225 assert(aux_usage == ISL_AUX_USAGE_MCS);
2226
2227 switch (intel_miptree_get_aux_state(mt, 0, layer)) {
2228 case ISL_AUX_STATE_CLEAR:
2229 case ISL_AUX_STATE_COMPRESSED_CLEAR:
2230 if (!fast_clear_supported) {
2231 brw_blorp_mcs_partial_resolve(brw, mt, layer, 1);
2232 intel_miptree_set_aux_state(brw, mt, 0, layer, 1,
2233 ISL_AUX_STATE_COMPRESSED_NO_CLEAR);
2234 }
2235 break;
2236
2237 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
2238 break; /* Nothing to do */
2239
2240 case ISL_AUX_STATE_RESOLVED:
2241 case ISL_AUX_STATE_PASS_THROUGH:
2242 case ISL_AUX_STATE_AUX_INVALID:
2243 case ISL_AUX_STATE_PARTIAL_CLEAR:
2244 unreachable("Invalid aux state for MCS");
2245 }
2246 }
2247
2248 static void
2249 intel_miptree_finish_mcs_write(struct brw_context *brw,
2250 struct intel_mipmap_tree *mt,
2251 uint32_t layer,
2252 enum isl_aux_usage aux_usage)
2253 {
2254 assert(aux_usage == ISL_AUX_USAGE_MCS);
2255
2256 switch (intel_miptree_get_aux_state(mt, 0, layer)) {
2257 case ISL_AUX_STATE_CLEAR:
2258 intel_miptree_set_aux_state(brw, mt, 0, layer, 1,
2259 ISL_AUX_STATE_COMPRESSED_CLEAR);
2260 break;
2261
2262 case ISL_AUX_STATE_COMPRESSED_CLEAR:
2263 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
2264 break; /* Nothing to do */
2265
2266 case ISL_AUX_STATE_RESOLVED:
2267 case ISL_AUX_STATE_PASS_THROUGH:
2268 case ISL_AUX_STATE_AUX_INVALID:
2269 case ISL_AUX_STATE_PARTIAL_CLEAR:
2270 unreachable("Invalid aux state for MCS");
2271 }
2272 }
2273
2274 static void
2275 intel_miptree_prepare_hiz_access(struct brw_context *brw,
2276 struct intel_mipmap_tree *mt,
2277 uint32_t level, uint32_t layer,
2278 enum isl_aux_usage aux_usage,
2279 bool fast_clear_supported)
2280 {
2281 assert(aux_usage == ISL_AUX_USAGE_NONE || aux_usage == ISL_AUX_USAGE_HIZ);
2282
2283 enum blorp_hiz_op hiz_op = BLORP_HIZ_OP_NONE;
2284 switch (intel_miptree_get_aux_state(mt, level, layer)) {
2285 case ISL_AUX_STATE_CLEAR:
2286 case ISL_AUX_STATE_COMPRESSED_CLEAR:
2287 if (aux_usage != ISL_AUX_USAGE_HIZ || !fast_clear_supported)
2288 hiz_op = BLORP_HIZ_OP_DEPTH_RESOLVE;
2289 break;
2290
2291 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
2292 if (aux_usage != ISL_AUX_USAGE_HIZ)
2293 hiz_op = BLORP_HIZ_OP_DEPTH_RESOLVE;
2294 break;
2295
2296 case ISL_AUX_STATE_PASS_THROUGH:
2297 case ISL_AUX_STATE_RESOLVED:
2298 break;
2299
2300 case ISL_AUX_STATE_AUX_INVALID:
2301 if (aux_usage == ISL_AUX_USAGE_HIZ)
2302 hiz_op = BLORP_HIZ_OP_HIZ_RESOLVE;
2303 break;
2304
2305 case ISL_AUX_STATE_PARTIAL_CLEAR:
2306 unreachable("Invalid HiZ state");
2307 }
2308
2309 if (hiz_op != BLORP_HIZ_OP_NONE) {
2310 intel_hiz_exec(brw, mt, level, layer, 1, hiz_op);
2311
2312 switch (hiz_op) {
2313 case BLORP_HIZ_OP_DEPTH_RESOLVE:
2314 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2315 ISL_AUX_STATE_RESOLVED);
2316 break;
2317
2318 case BLORP_HIZ_OP_HIZ_RESOLVE:
2319 /* The HiZ resolve operation is actually an ambiguate */
2320 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2321 ISL_AUX_STATE_PASS_THROUGH);
2322 break;
2323
2324 default:
2325 unreachable("Invalid HiZ op");
2326 }
2327 }
2328 }
2329
2330 static void
2331 intel_miptree_finish_hiz_write(struct brw_context *brw,
2332 struct intel_mipmap_tree *mt,
2333 uint32_t level, uint32_t layer,
2334 enum isl_aux_usage aux_usage)
2335 {
2336 assert(aux_usage == ISL_AUX_USAGE_NONE || aux_usage == ISL_AUX_USAGE_HIZ);
2337
2338 switch (intel_miptree_get_aux_state(mt, level, layer)) {
2339 case ISL_AUX_STATE_CLEAR:
2340 assert(aux_usage == ISL_AUX_USAGE_HIZ);
2341 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2342 ISL_AUX_STATE_COMPRESSED_CLEAR);
2343 break;
2344
2345 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
2346 case ISL_AUX_STATE_COMPRESSED_CLEAR:
2347 assert(aux_usage == ISL_AUX_USAGE_HIZ);
2348 break; /* Nothing to do */
2349
2350 case ISL_AUX_STATE_RESOLVED:
2351 if (aux_usage == ISL_AUX_USAGE_HIZ) {
2352 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2353 ISL_AUX_STATE_COMPRESSED_NO_CLEAR);
2354 } else {
2355 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2356 ISL_AUX_STATE_AUX_INVALID);
2357 }
2358 break;
2359
2360 case ISL_AUX_STATE_PASS_THROUGH:
2361 if (aux_usage == ISL_AUX_USAGE_HIZ) {
2362 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2363 ISL_AUX_STATE_COMPRESSED_NO_CLEAR);
2364 }
2365 break;
2366
2367 case ISL_AUX_STATE_AUX_INVALID:
2368 assert(aux_usage != ISL_AUX_USAGE_HIZ);
2369 break;
2370
2371 case ISL_AUX_STATE_PARTIAL_CLEAR:
2372 unreachable("Invalid HiZ state");
2373 }
2374 }
2375
2376 void
2377 intel_miptree_prepare_access(struct brw_context *brw,
2378 struct intel_mipmap_tree *mt,
2379 uint32_t start_level, uint32_t num_levels,
2380 uint32_t start_layer, uint32_t num_layers,
2381 enum isl_aux_usage aux_usage,
2382 bool fast_clear_supported)
2383 {
2384 num_levels = miptree_level_range_length(mt, start_level, num_levels);
2385
2386 switch (mt->aux_usage) {
2387 case ISL_AUX_USAGE_NONE:
2388 /* Nothing to do */
2389 break;
2390
2391 case ISL_AUX_USAGE_MCS:
2392 assert(mt->mcs_buf);
2393 assert(start_level == 0 && num_levels == 1);
2394 const uint32_t level_layers =
2395 miptree_layer_range_length(mt, 0, start_layer, num_layers);
2396 for (uint32_t a = 0; a < level_layers; a++) {
2397 intel_miptree_prepare_mcs_access(brw, mt, start_layer + a,
2398 aux_usage, fast_clear_supported);
2399 }
2400 break;
2401
2402 case ISL_AUX_USAGE_CCS_D:
2403 case ISL_AUX_USAGE_CCS_E:
2404 if (!mt->mcs_buf)
2405 return;
2406
2407 for (uint32_t l = 0; l < num_levels; l++) {
2408 const uint32_t level = start_level + l;
2409 const uint32_t level_layers =
2410 miptree_layer_range_length(mt, level, start_layer, num_layers);
2411 for (uint32_t a = 0; a < level_layers; a++) {
2412 intel_miptree_prepare_ccs_access(brw, mt, level,
2413 start_layer + a,
2414 aux_usage, fast_clear_supported);
2415 }
2416 }
2417 break;
2418
2419 case ISL_AUX_USAGE_HIZ:
2420 assert(mt->hiz_buf);
2421 for (uint32_t l = 0; l < num_levels; l++) {
2422 const uint32_t level = start_level + l;
2423 if (!intel_miptree_level_has_hiz(mt, level))
2424 continue;
2425
2426 const uint32_t level_layers =
2427 miptree_layer_range_length(mt, level, start_layer, num_layers);
2428 for (uint32_t a = 0; a < level_layers; a++) {
2429 intel_miptree_prepare_hiz_access(brw, mt, level, start_layer + a,
2430 aux_usage, fast_clear_supported);
2431 }
2432 }
2433 break;
2434
2435 default:
2436 unreachable("Invalid aux usage");
2437 }
2438 }
2439
2440 void
2441 intel_miptree_finish_write(struct brw_context *brw,
2442 struct intel_mipmap_tree *mt, uint32_t level,
2443 uint32_t start_layer, uint32_t num_layers,
2444 enum isl_aux_usage aux_usage)
2445 {
2446 num_layers = miptree_layer_range_length(mt, level, start_layer, num_layers);
2447
2448 switch (mt->aux_usage) {
2449 case ISL_AUX_USAGE_NONE:
2450 /* Nothing to do */
2451 break;
2452
2453 case ISL_AUX_USAGE_MCS:
2454 assert(mt->mcs_buf);
2455 for (uint32_t a = 0; a < num_layers; a++) {
2456 intel_miptree_finish_mcs_write(brw, mt, start_layer + a,
2457 aux_usage);
2458 }
2459 break;
2460
2461 case ISL_AUX_USAGE_CCS_D:
2462 case ISL_AUX_USAGE_CCS_E:
2463 if (!mt->mcs_buf)
2464 return;
2465
2466 for (uint32_t a = 0; a < num_layers; a++) {
2467 intel_miptree_finish_ccs_write(brw, mt, level, start_layer + a,
2468 aux_usage);
2469 }
2470 break;
2471
2472 case ISL_AUX_USAGE_HIZ:
2473 if (!intel_miptree_level_has_hiz(mt, level))
2474 return;
2475
2476 for (uint32_t a = 0; a < num_layers; a++) {
2477 intel_miptree_finish_hiz_write(brw, mt, level, start_layer + a,
2478 aux_usage);
2479 }
2480 break;
2481
2482 default:
2483 unreachable("Invavlid aux usage");
2484 }
2485 }
2486
2487 enum isl_aux_state
2488 intel_miptree_get_aux_state(const struct intel_mipmap_tree *mt,
2489 uint32_t level, uint32_t layer)
2490 {
2491 intel_miptree_check_level_layer(mt, level, layer);
2492
2493 if (_mesa_is_format_color_format(mt->format)) {
2494 assert(mt->mcs_buf != NULL);
2495 assert(mt->surf.samples == 1 ||
2496 mt->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY);
2497 } else if (mt->format == MESA_FORMAT_S_UINT8) {
2498 unreachable("Cannot get aux state for stencil");
2499 } else {
2500 assert(intel_miptree_level_has_hiz(mt, level));
2501 }
2502
2503 return mt->aux_state[level][layer];
2504 }
2505
2506 void
2507 intel_miptree_set_aux_state(struct brw_context *brw,
2508 struct intel_mipmap_tree *mt, uint32_t level,
2509 uint32_t start_layer, uint32_t num_layers,
2510 enum isl_aux_state aux_state)
2511 {
2512 num_layers = miptree_layer_range_length(mt, level, start_layer, num_layers);
2513
2514 if (_mesa_is_format_color_format(mt->format)) {
2515 assert(mt->mcs_buf != NULL);
2516 assert(mt->surf.samples == 1 ||
2517 mt->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY);
2518 } else if (mt->format == MESA_FORMAT_S_UINT8) {
2519 unreachable("Cannot get aux state for stencil");
2520 } else {
2521 assert(intel_miptree_level_has_hiz(mt, level));
2522 }
2523
2524 for (unsigned a = 0; a < num_layers; a++)
2525 mt->aux_state[level][start_layer + a] = aux_state;
2526 }
2527
2528 /* On Gen9 color buffers may be compressed by the hardware (lossless
2529 * compression). There are, however, format restrictions and care needs to be
2530 * taken that the sampler engine is capable for re-interpreting a buffer with
2531 * format different the buffer was originally written with.
2532 *
2533 * For example, SRGB formats are not compressible and the sampler engine isn't
2534 * capable of treating RGBA_UNORM as SRGB_ALPHA. In such a case the underlying
2535 * color buffer needs to be resolved so that the sampling surface can be
2536 * sampled as non-compressed (i.e., without the auxiliary MCS buffer being
2537 * set).
2538 */
2539 static bool
2540 can_texture_with_ccs(struct brw_context *brw,
2541 struct intel_mipmap_tree *mt,
2542 enum isl_format view_format)
2543 {
2544 if (mt->aux_usage != ISL_AUX_USAGE_CCS_E)
2545 return false;
2546
2547 if (!isl_formats_are_ccs_e_compatible(&brw->screen->devinfo,
2548 mt->surf.format, view_format)) {
2549 perf_debug("Incompatible sampling format (%s) for rbc (%s)\n",
2550 isl_format_get_layout(view_format)->name,
2551 _mesa_get_format_name(mt->format));
2552 return false;
2553 }
2554
2555 return true;
2556 }
2557
2558 enum isl_aux_usage
2559 intel_miptree_texture_aux_usage(struct brw_context *brw,
2560 struct intel_mipmap_tree *mt,
2561 enum isl_format view_format)
2562 {
2563 switch (mt->aux_usage) {
2564 case ISL_AUX_USAGE_HIZ:
2565 if (intel_miptree_sample_with_hiz(brw, mt))
2566 return ISL_AUX_USAGE_HIZ;
2567 break;
2568
2569 case ISL_AUX_USAGE_MCS:
2570 return ISL_AUX_USAGE_MCS;
2571
2572 case ISL_AUX_USAGE_CCS_D:
2573 case ISL_AUX_USAGE_CCS_E:
2574 if (mt->mcs_buf && can_texture_with_ccs(brw, mt, view_format))
2575 return ISL_AUX_USAGE_CCS_E;
2576 break;
2577
2578 default:
2579 break;
2580 }
2581
2582 return ISL_AUX_USAGE_NONE;
2583 }
2584
2585 static bool
2586 isl_formats_are_fast_clear_compatible(enum isl_format a, enum isl_format b)
2587 {
2588 /* On gen8 and earlier, the hardware was only capable of handling 0/1 clear
2589 * values so sRGB curve application was a no-op for all fast-clearable
2590 * formats.
2591 *
2592 * On gen9+, the hardware supports arbitrary clear values. For sRGB clear
2593 * values, the hardware interprets the floats, not as what would be
2594 * returned from the sampler (or written by the shader), but as being
2595 * between format conversion and sRGB curve application. This means that
2596 * we can switch between sRGB and UNORM without having to whack the clear
2597 * color.
2598 */
2599 return isl_format_srgb_to_linear(a) == isl_format_srgb_to_linear(b);
2600 }
2601
2602 static void
2603 intel_miptree_prepare_texture_slices(struct brw_context *brw,
2604 struct intel_mipmap_tree *mt,
2605 enum isl_format view_format,
2606 uint32_t start_level, uint32_t num_levels,
2607 uint32_t start_layer, uint32_t num_layers,
2608 bool *aux_supported_out)
2609 {
2610 enum isl_aux_usage aux_usage =
2611 intel_miptree_texture_aux_usage(brw, mt, view_format);
2612 bool clear_supported = aux_usage != ISL_AUX_USAGE_NONE;
2613
2614 /* Clear color is specified as ints or floats and the conversion is done by
2615 * the sampler. If we have a texture view, we would have to perform the
2616 * clear color conversion manually. Just disable clear color.
2617 */
2618 if (!isl_formats_are_fast_clear_compatible(mt->surf.format, view_format))
2619 clear_supported = false;
2620
2621 intel_miptree_prepare_access(brw, mt, start_level, num_levels,
2622 start_layer, num_layers,
2623 aux_usage, clear_supported);
2624 if (aux_supported_out)
2625 *aux_supported_out = aux_usage != ISL_AUX_USAGE_NONE;
2626 }
2627
2628 void
2629 intel_miptree_prepare_texture(struct brw_context *brw,
2630 struct intel_mipmap_tree *mt,
2631 enum isl_format view_format,
2632 bool *aux_supported_out)
2633 {
2634 intel_miptree_prepare_texture_slices(brw, mt, view_format,
2635 0, INTEL_REMAINING_LEVELS,
2636 0, INTEL_REMAINING_LAYERS,
2637 aux_supported_out);
2638 }
2639
2640 void
2641 intel_miptree_prepare_image(struct brw_context *brw,
2642 struct intel_mipmap_tree *mt)
2643 {
2644 /* The data port doesn't understand any compression */
2645 intel_miptree_prepare_access(brw, mt, 0, INTEL_REMAINING_LEVELS,
2646 0, INTEL_REMAINING_LAYERS,
2647 ISL_AUX_USAGE_NONE, false);
2648 }
2649
2650 void
2651 intel_miptree_prepare_fb_fetch(struct brw_context *brw,
2652 struct intel_mipmap_tree *mt, uint32_t level,
2653 uint32_t start_layer, uint32_t num_layers)
2654 {
2655 intel_miptree_prepare_texture_slices(brw, mt, mt->surf.format, level, 1,
2656 start_layer, num_layers, NULL);
2657 }
2658
2659 enum isl_aux_usage
2660 intel_miptree_render_aux_usage(struct brw_context *brw,
2661 struct intel_mipmap_tree *mt,
2662 bool srgb_enabled, bool blend_enabled)
2663 {
2664 switch (mt->aux_usage) {
2665 case ISL_AUX_USAGE_MCS:
2666 assert(mt->mcs_buf);
2667 return ISL_AUX_USAGE_MCS;
2668
2669 case ISL_AUX_USAGE_CCS_D:
2670 return mt->mcs_buf ? ISL_AUX_USAGE_CCS_D : ISL_AUX_USAGE_NONE;
2671
2672 case ISL_AUX_USAGE_CCS_E: {
2673 mesa_format mesa_format =
2674 srgb_enabled ? mt->format :_mesa_get_srgb_format_linear(mt->format);
2675 enum isl_format isl_format = brw_isl_format_for_mesa_format(mesa_format);
2676
2677 /* If the format supports CCS_E, then we can just use it */
2678 if (isl_format_supports_ccs_e(&brw->screen->devinfo, isl_format))
2679 return ISL_AUX_USAGE_CCS_E;
2680
2681 /* Otherwise, we have to fall back to CCS_D */
2682
2683 /* gen9 hardware technically supports non-0/1 clear colors with sRGB
2684 * formats. However, there are issues with blending where it doesn't
2685 * properly apply the sRGB curve to the clear color when blending.
2686 */
2687 if (blend_enabled && isl_format_is_srgb(isl_format) &&
2688 !isl_color_value_is_zero_one(mt->fast_clear_color, isl_format))
2689 return ISL_AUX_USAGE_NONE;
2690
2691 return ISL_AUX_USAGE_CCS_D;
2692 }
2693
2694 default:
2695 return ISL_AUX_USAGE_NONE;
2696 }
2697 }
2698
2699 void
2700 intel_miptree_prepare_render(struct brw_context *brw,
2701 struct intel_mipmap_tree *mt, uint32_t level,
2702 uint32_t start_layer, uint32_t layer_count,
2703 bool srgb_enabled, bool blend_enabled)
2704 {
2705 enum isl_aux_usage aux_usage =
2706 intel_miptree_render_aux_usage(brw, mt, srgb_enabled, blend_enabled);
2707 intel_miptree_prepare_access(brw, mt, level, 1, start_layer, layer_count,
2708 aux_usage, aux_usage != ISL_AUX_USAGE_NONE);
2709 }
2710
2711 void
2712 intel_miptree_finish_render(struct brw_context *brw,
2713 struct intel_mipmap_tree *mt, uint32_t level,
2714 uint32_t start_layer, uint32_t layer_count,
2715 bool srgb_enabled, bool blend_enabled)
2716 {
2717 assert(_mesa_is_format_color_format(mt->format));
2718
2719 enum isl_aux_usage aux_usage =
2720 intel_miptree_render_aux_usage(brw, mt, srgb_enabled, blend_enabled);
2721 intel_miptree_finish_write(brw, mt, level, start_layer, layer_count,
2722 aux_usage);
2723 }
2724
2725 void
2726 intel_miptree_prepare_depth(struct brw_context *brw,
2727 struct intel_mipmap_tree *mt, uint32_t level,
2728 uint32_t start_layer, uint32_t layer_count)
2729 {
2730 intel_miptree_prepare_access(brw, mt, level, 1, start_layer, layer_count,
2731 mt->aux_usage, mt->hiz_buf != NULL);
2732 }
2733
2734 void
2735 intel_miptree_finish_depth(struct brw_context *brw,
2736 struct intel_mipmap_tree *mt, uint32_t level,
2737 uint32_t start_layer, uint32_t layer_count,
2738 bool depth_written)
2739 {
2740 if (depth_written) {
2741 intel_miptree_finish_write(brw, mt, level, start_layer, layer_count,
2742 mt->hiz_buf != NULL);
2743 }
2744 }
2745
2746 void
2747 intel_miptree_prepare_external(struct brw_context *brw,
2748 struct intel_mipmap_tree *mt)
2749 {
2750 enum isl_aux_usage aux_usage = ISL_AUX_USAGE_NONE;
2751 bool supports_fast_clear = false;
2752
2753 const struct isl_drm_modifier_info *mod_info =
2754 isl_drm_modifier_get_info(mt->drm_modifier);
2755
2756 if (mod_info && mod_info->aux_usage != ISL_AUX_USAGE_NONE) {
2757 /* CCS_E is the only supported aux for external images and it's only
2758 * supported on very simple images.
2759 */
2760 assert(mod_info->aux_usage == ISL_AUX_USAGE_CCS_E);
2761 assert(_mesa_is_format_color_format(mt->format));
2762 assert(mt->first_level == 0 && mt->last_level == 0);
2763 assert(mt->surf.logical_level0_px.depth == 1);
2764 assert(mt->surf.logical_level0_px.array_len == 1);
2765 assert(mt->surf.samples == 1);
2766 assert(mt->mcs_buf != NULL);
2767
2768 aux_usage = mod_info->aux_usage;
2769 supports_fast_clear = mod_info->supports_clear_color;
2770 }
2771
2772 intel_miptree_prepare_access(brw, mt, 0, INTEL_REMAINING_LEVELS,
2773 0, INTEL_REMAINING_LAYERS,
2774 aux_usage, supports_fast_clear);
2775 }
2776
2777 /**
2778 * Make it possible to share the BO backing the given miptree with another
2779 * process or another miptree.
2780 *
2781 * Fast color clears are unsafe with shared buffers, so we need to resolve and
2782 * then discard the MCS buffer, if present. We also set the no_ccs flag to
2783 * ensure that no MCS buffer gets allocated in the future.
2784 *
2785 * HiZ is similarly unsafe with shared buffers.
2786 */
2787 void
2788 intel_miptree_make_shareable(struct brw_context *brw,
2789 struct intel_mipmap_tree *mt)
2790 {
2791 /* MCS buffers are also used for multisample buffers, but we can't resolve
2792 * away a multisample MCS buffer because it's an integral part of how the
2793 * pixel data is stored. Fortunately this code path should never be
2794 * reached for multisample buffers.
2795 */
2796 assert(mt->surf.msaa_layout == ISL_MSAA_LAYOUT_NONE ||
2797 mt->surf.samples == 1);
2798
2799 intel_miptree_prepare_access(brw, mt, 0, INTEL_REMAINING_LEVELS,
2800 0, INTEL_REMAINING_LAYERS,
2801 ISL_AUX_USAGE_NONE, false);
2802
2803 if (mt->mcs_buf) {
2804 brw_bo_unreference(mt->mcs_buf->bo);
2805 free(mt->mcs_buf);
2806 mt->mcs_buf = NULL;
2807
2808 /* Any pending MCS/CCS operations are no longer needed. Trying to
2809 * execute any will likely crash due to the missing aux buffer. So let's
2810 * delete all pending ops.
2811 */
2812 free(mt->aux_state);
2813 mt->aux_state = NULL;
2814 }
2815
2816 if (mt->hiz_buf) {
2817 intel_miptree_aux_buffer_free(mt->hiz_buf);
2818 mt->hiz_buf = NULL;
2819
2820 for (uint32_t l = mt->first_level; l <= mt->last_level; ++l) {
2821 mt->level[l].has_hiz = false;
2822 }
2823
2824 /* Any pending HiZ operations are no longer needed. Trying to execute
2825 * any will likely crash due to the missing aux buffer. So let's delete
2826 * all pending ops.
2827 */
2828 free(mt->aux_state);
2829 mt->aux_state = NULL;
2830 }
2831
2832 mt->aux_usage = ISL_AUX_USAGE_NONE;
2833 mt->supports_fast_clear = false;
2834 }
2835
2836
2837 /**
2838 * \brief Get pointer offset into stencil buffer.
2839 *
2840 * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
2841 * must decode the tile's layout in software.
2842 *
2843 * See
2844 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
2845 * Format.
2846 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
2847 *
2848 * Even though the returned offset is always positive, the return type is
2849 * signed due to
2850 * commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
2851 * mesa: Fix return type of _mesa_get_format_bytes() (#37351)
2852 */
2853 static intptr_t
2854 intel_offset_S8(uint32_t stride, uint32_t x, uint32_t y, bool swizzled)
2855 {
2856 uint32_t tile_size = 4096;
2857 uint32_t tile_width = 64;
2858 uint32_t tile_height = 64;
2859 uint32_t row_size = 64 * stride / 2; /* Two rows are interleaved. */
2860
2861 uint32_t tile_x = x / tile_width;
2862 uint32_t tile_y = y / tile_height;
2863
2864 /* The byte's address relative to the tile's base addres. */
2865 uint32_t byte_x = x % tile_width;
2866 uint32_t byte_y = y % tile_height;
2867
2868 uintptr_t u = tile_y * row_size
2869 + tile_x * tile_size
2870 + 512 * (byte_x / 8)
2871 + 64 * (byte_y / 8)
2872 + 32 * ((byte_y / 4) % 2)
2873 + 16 * ((byte_x / 4) % 2)
2874 + 8 * ((byte_y / 2) % 2)
2875 + 4 * ((byte_x / 2) % 2)
2876 + 2 * (byte_y % 2)
2877 + 1 * (byte_x % 2);
2878
2879 if (swizzled) {
2880 /* adjust for bit6 swizzling */
2881 if (((byte_x / 8) % 2) == 1) {
2882 if (((byte_y / 8) % 2) == 0) {
2883 u += 64;
2884 } else {
2885 u -= 64;
2886 }
2887 }
2888 }
2889
2890 return u;
2891 }
2892
2893 void
2894 intel_miptree_updownsample(struct brw_context *brw,
2895 struct intel_mipmap_tree *src,
2896 struct intel_mipmap_tree *dst)
2897 {
2898 unsigned src_w = src->surf.logical_level0_px.width;
2899 unsigned src_h = src->surf.logical_level0_px.height;
2900 unsigned dst_w = dst->surf.logical_level0_px.width;
2901 unsigned dst_h = dst->surf.logical_level0_px.height;
2902
2903 brw_blorp_blit_miptrees(brw,
2904 src, 0 /* level */, 0 /* layer */,
2905 src->format, SWIZZLE_XYZW,
2906 dst, 0 /* level */, 0 /* layer */, dst->format,
2907 0, 0, src_w, src_h,
2908 0, 0, dst_w, dst_h,
2909 GL_NEAREST, false, false /*mirror x, y*/,
2910 false, false);
2911
2912 if (src->stencil_mt) {
2913 src_w = src->stencil_mt->surf.logical_level0_px.width;
2914 src_h = src->stencil_mt->surf.logical_level0_px.height;
2915 dst_w = dst->stencil_mt->surf.logical_level0_px.width;
2916 dst_h = dst->stencil_mt->surf.logical_level0_px.height;
2917
2918 brw_blorp_blit_miptrees(brw,
2919 src->stencil_mt, 0 /* level */, 0 /* layer */,
2920 src->stencil_mt->format, SWIZZLE_XYZW,
2921 dst->stencil_mt, 0 /* level */, 0 /* layer */,
2922 dst->stencil_mt->format,
2923 0, 0, src_w, src_h,
2924 0, 0, dst_w, dst_h,
2925 GL_NEAREST, false, false /*mirror x, y*/,
2926 false, false /* decode/encode srgb */);
2927 }
2928 }
2929
2930 void
2931 intel_update_r8stencil(struct brw_context *brw,
2932 struct intel_mipmap_tree *mt)
2933 {
2934 assert(brw->gen >= 7);
2935 struct intel_mipmap_tree *src =
2936 mt->format == MESA_FORMAT_S_UINT8 ? mt : mt->stencil_mt;
2937 if (!src || brw->gen >= 8 || !src->r8stencil_needs_update)
2938 return;
2939
2940 assert(src->surf.size > 0);
2941
2942 if (!mt->r8stencil_mt) {
2943 assert(brw->gen > 6); /* Handle MIPTREE_LAYOUT_GEN6_HIZ_STENCIL */
2944 mt->r8stencil_mt = make_surface(
2945 brw,
2946 src->target,
2947 MESA_FORMAT_R_UINT8,
2948 src->first_level, src->last_level,
2949 src->surf.logical_level0_px.width,
2950 src->surf.logical_level0_px.height,
2951 src->surf.dim == ISL_SURF_DIM_3D ?
2952 src->surf.logical_level0_px.depth :
2953 src->surf.logical_level0_px.array_len,
2954 src->surf.samples,
2955 ISL_TILING_Y0_BIT,
2956 ISL_SURF_USAGE_TEXTURE_BIT,
2957 BO_ALLOC_FOR_RENDER, 0, NULL);
2958 assert(mt->r8stencil_mt);
2959 }
2960
2961 struct intel_mipmap_tree *dst = mt->r8stencil_mt;
2962
2963 for (int level = src->first_level; level <= src->last_level; level++) {
2964 const unsigned depth = src->surf.dim == ISL_SURF_DIM_3D ?
2965 minify(src->surf.phys_level0_sa.depth, level) :
2966 src->surf.phys_level0_sa.array_len;
2967
2968 for (unsigned layer = 0; layer < depth; layer++) {
2969 brw_blorp_copy_miptrees(brw,
2970 src, level, layer,
2971 dst, level, layer,
2972 0, 0, 0, 0,
2973 minify(src->surf.logical_level0_px.width,
2974 level),
2975 minify(src->surf.logical_level0_px.height,
2976 level));
2977 }
2978 }
2979
2980 brw_render_cache_set_check_flush(brw, dst->bo);
2981 src->r8stencil_needs_update = false;
2982 }
2983
2984 static void *
2985 intel_miptree_map_raw(struct brw_context *brw,
2986 struct intel_mipmap_tree *mt,
2987 GLbitfield mode)
2988 {
2989 struct brw_bo *bo = mt->bo;
2990
2991 if (brw_batch_references(&brw->batch, bo))
2992 intel_batchbuffer_flush(brw);
2993
2994 return brw_bo_map(brw, bo, mode);
2995 }
2996
2997 static void
2998 intel_miptree_unmap_raw(struct intel_mipmap_tree *mt)
2999 {
3000 brw_bo_unmap(mt->bo);
3001 }
3002
3003 static void
3004 intel_miptree_map_gtt(struct brw_context *brw,
3005 struct intel_mipmap_tree *mt,
3006 struct intel_miptree_map *map,
3007 unsigned int level, unsigned int slice)
3008 {
3009 unsigned int bw, bh;
3010 void *base;
3011 unsigned int image_x, image_y;
3012 intptr_t x = map->x;
3013 intptr_t y = map->y;
3014
3015 /* For compressed formats, the stride is the number of bytes per
3016 * row of blocks. intel_miptree_get_image_offset() already does
3017 * the divide.
3018 */
3019 _mesa_get_format_block_size(mt->format, &bw, &bh);
3020 assert(y % bh == 0);
3021 assert(x % bw == 0);
3022 y /= bh;
3023 x /= bw;
3024
3025 base = intel_miptree_map_raw(brw, mt, map->mode);
3026
3027 if (base == NULL)
3028 map->ptr = NULL;
3029 else {
3030 base += mt->offset;
3031
3032 /* Note that in the case of cube maps, the caller must have passed the
3033 * slice number referencing the face.
3034 */
3035 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
3036 x += image_x;
3037 y += image_y;
3038
3039 map->stride = mt->surf.row_pitch;
3040 map->ptr = base + y * map->stride + x * mt->cpp;
3041 }
3042
3043 DBG("%s: %d,%d %dx%d from mt %p (%s) "
3044 "%"PRIiPTR",%"PRIiPTR" = %p/%d\n", __func__,
3045 map->x, map->y, map->w, map->h,
3046 mt, _mesa_get_format_name(mt->format),
3047 x, y, map->ptr, map->stride);
3048 }
3049
3050 static void
3051 intel_miptree_unmap_gtt(struct intel_mipmap_tree *mt)
3052 {
3053 intel_miptree_unmap_raw(mt);
3054 }
3055
3056 static void
3057 intel_miptree_map_blit(struct brw_context *brw,
3058 struct intel_mipmap_tree *mt,
3059 struct intel_miptree_map *map,
3060 unsigned int level, unsigned int slice)
3061 {
3062 map->linear_mt = intel_miptree_create(brw, GL_TEXTURE_2D, mt->format,
3063 /* first_level */ 0,
3064 /* last_level */ 0,
3065 map->w, map->h, 1,
3066 /* samples */ 1,
3067 MIPTREE_CREATE_LINEAR);
3068
3069 if (!map->linear_mt) {
3070 fprintf(stderr, "Failed to allocate blit temporary\n");
3071 goto fail;
3072 }
3073 map->stride = map->linear_mt->surf.row_pitch;
3074
3075 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
3076 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
3077 * invalidate is set, since we'll be writing the whole rectangle from our
3078 * temporary buffer back out.
3079 */
3080 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
3081 if (!intel_miptree_copy(brw,
3082 mt, level, slice, map->x, map->y,
3083 map->linear_mt, 0, 0, 0, 0,
3084 map->w, map->h)) {
3085 fprintf(stderr, "Failed to blit\n");
3086 goto fail;
3087 }
3088 }
3089
3090 map->ptr = intel_miptree_map_raw(brw, map->linear_mt, map->mode);
3091
3092 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__,
3093 map->x, map->y, map->w, map->h,
3094 mt, _mesa_get_format_name(mt->format),
3095 level, slice, map->ptr, map->stride);
3096
3097 return;
3098
3099 fail:
3100 intel_miptree_release(&map->linear_mt);
3101 map->ptr = NULL;
3102 map->stride = 0;
3103 }
3104
3105 static void
3106 intel_miptree_unmap_blit(struct brw_context *brw,
3107 struct intel_mipmap_tree *mt,
3108 struct intel_miptree_map *map,
3109 unsigned int level,
3110 unsigned int slice)
3111 {
3112 struct gl_context *ctx = &brw->ctx;
3113
3114 intel_miptree_unmap_raw(map->linear_mt);
3115
3116 if (map->mode & GL_MAP_WRITE_BIT) {
3117 bool ok = intel_miptree_copy(brw,
3118 map->linear_mt, 0, 0, 0, 0,
3119 mt, level, slice, map->x, map->y,
3120 map->w, map->h);
3121 WARN_ONCE(!ok, "Failed to blit from linear temporary mapping");
3122 }
3123
3124 intel_miptree_release(&map->linear_mt);
3125 }
3126
3127 /**
3128 * "Map" a buffer by copying it to an untiled temporary using MOVNTDQA.
3129 */
3130 #if defined(USE_SSE41)
3131 static void
3132 intel_miptree_map_movntdqa(struct brw_context *brw,
3133 struct intel_mipmap_tree *mt,
3134 struct intel_miptree_map *map,
3135 unsigned int level, unsigned int slice)
3136 {
3137 assert(map->mode & GL_MAP_READ_BIT);
3138 assert(!(map->mode & GL_MAP_WRITE_BIT));
3139
3140 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__,
3141 map->x, map->y, map->w, map->h,
3142 mt, _mesa_get_format_name(mt->format),
3143 level, slice, map->ptr, map->stride);
3144
3145 /* Map the original image */
3146 uint32_t image_x;
3147 uint32_t image_y;
3148 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
3149 image_x += map->x;
3150 image_y += map->y;
3151
3152 void *src = intel_miptree_map_raw(brw, mt, map->mode);
3153 if (!src)
3154 return;
3155
3156 src += mt->offset;
3157
3158 src += image_y * mt->surf.row_pitch;
3159 src += image_x * mt->cpp;
3160
3161 /* Due to the pixel offsets for the particular image being mapped, our
3162 * src pointer may not be 16-byte aligned. However, if the pitch is
3163 * divisible by 16, then the amount by which it's misaligned will remain
3164 * consistent from row to row.
3165 */
3166 assert((mt->surf.row_pitch % 16) == 0);
3167 const int misalignment = ((uintptr_t) src) & 15;
3168
3169 /* Create an untiled temporary buffer for the mapping. */
3170 const unsigned width_bytes = _mesa_format_row_stride(mt->format, map->w);
3171
3172 map->stride = ALIGN(misalignment + width_bytes, 16);
3173
3174 map->buffer = _mesa_align_malloc(map->stride * map->h, 16);
3175 /* Offset the destination so it has the same misalignment as src. */
3176 map->ptr = map->buffer + misalignment;
3177
3178 assert((((uintptr_t) map->ptr) & 15) == misalignment);
3179
3180 for (uint32_t y = 0; y < map->h; y++) {
3181 void *dst_ptr = map->ptr + y * map->stride;
3182 void *src_ptr = src + y * mt->surf.row_pitch;
3183
3184 _mesa_streaming_load_memcpy(dst_ptr, src_ptr, width_bytes);
3185 }
3186
3187 intel_miptree_unmap_raw(mt);
3188 }
3189
3190 static void
3191 intel_miptree_unmap_movntdqa(struct brw_context *brw,
3192 struct intel_mipmap_tree *mt,
3193 struct intel_miptree_map *map,
3194 unsigned int level,
3195 unsigned int slice)
3196 {
3197 _mesa_align_free(map->buffer);
3198 map->buffer = NULL;
3199 map->ptr = NULL;
3200 }
3201 #endif
3202
3203 static void
3204 intel_miptree_map_s8(struct brw_context *brw,
3205 struct intel_mipmap_tree *mt,
3206 struct intel_miptree_map *map,
3207 unsigned int level, unsigned int slice)
3208 {
3209 map->stride = map->w;
3210 map->buffer = map->ptr = malloc(map->stride * map->h);
3211 if (!map->buffer)
3212 return;
3213
3214 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
3215 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
3216 * invalidate is set, since we'll be writing the whole rectangle from our
3217 * temporary buffer back out.
3218 */
3219 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
3220 uint8_t *untiled_s8_map = map->ptr;
3221 uint8_t *tiled_s8_map = intel_miptree_map_raw(brw, mt, GL_MAP_READ_BIT);
3222 unsigned int image_x, image_y;
3223
3224 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
3225
3226 for (uint32_t y = 0; y < map->h; y++) {
3227 for (uint32_t x = 0; x < map->w; x++) {
3228 ptrdiff_t offset = intel_offset_S8(mt->surf.row_pitch,
3229 x + image_x + map->x,
3230 y + image_y + map->y,
3231 brw->has_swizzling);
3232 untiled_s8_map[y * map->w + x] = tiled_s8_map[offset];
3233 }
3234 }
3235
3236 intel_miptree_unmap_raw(mt);
3237
3238 DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __func__,
3239 map->x, map->y, map->w, map->h,
3240 mt, map->x + image_x, map->y + image_y, map->ptr, map->stride);
3241 } else {
3242 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__,
3243 map->x, map->y, map->w, map->h,
3244 mt, map->ptr, map->stride);
3245 }
3246 }
3247
3248 static void
3249 intel_miptree_unmap_s8(struct brw_context *brw,
3250 struct intel_mipmap_tree *mt,
3251 struct intel_miptree_map *map,
3252 unsigned int level,
3253 unsigned int slice)
3254 {
3255 if (map->mode & GL_MAP_WRITE_BIT) {
3256 unsigned int image_x, image_y;
3257 uint8_t *untiled_s8_map = map->ptr;
3258 uint8_t *tiled_s8_map = intel_miptree_map_raw(brw, mt, GL_MAP_WRITE_BIT);
3259
3260 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
3261
3262 for (uint32_t y = 0; y < map->h; y++) {
3263 for (uint32_t x = 0; x < map->w; x++) {
3264 ptrdiff_t offset = intel_offset_S8(mt->surf.row_pitch,
3265 image_x + x + map->x,
3266 image_y + y + map->y,
3267 brw->has_swizzling);
3268 tiled_s8_map[offset] = untiled_s8_map[y * map->w + x];
3269 }
3270 }
3271
3272 intel_miptree_unmap_raw(mt);
3273 }
3274
3275 free(map->buffer);
3276 }
3277
3278 static void
3279 intel_miptree_map_etc(struct brw_context *brw,
3280 struct intel_mipmap_tree *mt,
3281 struct intel_miptree_map *map,
3282 unsigned int level,
3283 unsigned int slice)
3284 {
3285 assert(mt->etc_format != MESA_FORMAT_NONE);
3286 if (mt->etc_format == MESA_FORMAT_ETC1_RGB8) {
3287 assert(mt->format == MESA_FORMAT_R8G8B8X8_UNORM);
3288 }
3289
3290 assert(map->mode & GL_MAP_WRITE_BIT);
3291 assert(map->mode & GL_MAP_INVALIDATE_RANGE_BIT);
3292
3293 map->stride = _mesa_format_row_stride(mt->etc_format, map->w);
3294 map->buffer = malloc(_mesa_format_image_size(mt->etc_format,
3295 map->w, map->h, 1));
3296 map->ptr = map->buffer;
3297 }
3298
3299 static void
3300 intel_miptree_unmap_etc(struct brw_context *brw,
3301 struct intel_mipmap_tree *mt,
3302 struct intel_miptree_map *map,
3303 unsigned int level,
3304 unsigned int slice)
3305 {
3306 uint32_t image_x;
3307 uint32_t image_y;
3308 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
3309
3310 image_x += map->x;
3311 image_y += map->y;
3312
3313 uint8_t *dst = intel_miptree_map_raw(brw, mt, GL_MAP_WRITE_BIT)
3314 + image_y * mt->surf.row_pitch
3315 + image_x * mt->cpp;
3316
3317 if (mt->etc_format == MESA_FORMAT_ETC1_RGB8)
3318 _mesa_etc1_unpack_rgba8888(dst, mt->surf.row_pitch,
3319 map->ptr, map->stride,
3320 map->w, map->h);
3321 else
3322 _mesa_unpack_etc2_format(dst, mt->surf.row_pitch,
3323 map->ptr, map->stride,
3324 map->w, map->h, mt->etc_format);
3325
3326 intel_miptree_unmap_raw(mt);
3327 free(map->buffer);
3328 }
3329
3330 /**
3331 * Mapping function for packed depth/stencil miptrees backed by real separate
3332 * miptrees for depth and stencil.
3333 *
3334 * On gen7, and to support HiZ pre-gen7, we have to have the stencil buffer
3335 * separate from the depth buffer. Yet at the GL API level, we have to expose
3336 * packed depth/stencil textures and FBO attachments, and Mesa core expects to
3337 * be able to map that memory for texture storage and glReadPixels-type
3338 * operations. We give Mesa core that access by mallocing a temporary and
3339 * copying the data between the actual backing store and the temporary.
3340 */
3341 static void
3342 intel_miptree_map_depthstencil(struct brw_context *brw,
3343 struct intel_mipmap_tree *mt,
3344 struct intel_miptree_map *map,
3345 unsigned int level, unsigned int slice)
3346 {
3347 struct intel_mipmap_tree *z_mt = mt;
3348 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
3349 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z_FLOAT32;
3350 int packed_bpp = map_z32f_x24s8 ? 8 : 4;
3351
3352 map->stride = map->w * packed_bpp;
3353 map->buffer = map->ptr = malloc(map->stride * map->h);
3354 if (!map->buffer)
3355 return;
3356
3357 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
3358 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
3359 * invalidate is set, since we'll be writing the whole rectangle from our
3360 * temporary buffer back out.
3361 */
3362 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
3363 uint32_t *packed_map = map->ptr;
3364 uint8_t *s_map = intel_miptree_map_raw(brw, s_mt, GL_MAP_READ_BIT);
3365 uint32_t *z_map = intel_miptree_map_raw(brw, z_mt, GL_MAP_READ_BIT);
3366 unsigned int s_image_x, s_image_y;
3367 unsigned int z_image_x, z_image_y;
3368
3369 intel_miptree_get_image_offset(s_mt, level, slice,
3370 &s_image_x, &s_image_y);
3371 intel_miptree_get_image_offset(z_mt, level, slice,
3372 &z_image_x, &z_image_y);
3373
3374 for (uint32_t y = 0; y < map->h; y++) {
3375 for (uint32_t x = 0; x < map->w; x++) {
3376 int map_x = map->x + x, map_y = map->y + y;
3377 ptrdiff_t s_offset = intel_offset_S8(s_mt->surf.row_pitch,
3378 map_x + s_image_x,
3379 map_y + s_image_y,
3380 brw->has_swizzling);
3381 ptrdiff_t z_offset = ((map_y + z_image_y) *
3382 (z_mt->surf.row_pitch / 4) +
3383 (map_x + z_image_x));
3384 uint8_t s = s_map[s_offset];
3385 uint32_t z = z_map[z_offset];
3386
3387 if (map_z32f_x24s8) {
3388 packed_map[(y * map->w + x) * 2 + 0] = z;
3389 packed_map[(y * map->w + x) * 2 + 1] = s;
3390 } else {
3391 packed_map[y * map->w + x] = (s << 24) | (z & 0x00ffffff);
3392 }
3393 }
3394 }
3395
3396 intel_miptree_unmap_raw(s_mt);
3397 intel_miptree_unmap_raw(z_mt);
3398
3399 DBG("%s: %d,%d %dx%d from z mt %p %d,%d, s mt %p %d,%d = %p/%d\n",
3400 __func__,
3401 map->x, map->y, map->w, map->h,
3402 z_mt, map->x + z_image_x, map->y + z_image_y,
3403 s_mt, map->x + s_image_x, map->y + s_image_y,
3404 map->ptr, map->stride);
3405 } else {
3406 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__,
3407 map->x, map->y, map->w, map->h,
3408 mt, map->ptr, map->stride);
3409 }
3410 }
3411
3412 static void
3413 intel_miptree_unmap_depthstencil(struct brw_context *brw,
3414 struct intel_mipmap_tree *mt,
3415 struct intel_miptree_map *map,
3416 unsigned int level,
3417 unsigned int slice)
3418 {
3419 struct intel_mipmap_tree *z_mt = mt;
3420 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
3421 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z_FLOAT32;
3422
3423 if (map->mode & GL_MAP_WRITE_BIT) {
3424 uint32_t *packed_map = map->ptr;
3425 uint8_t *s_map = intel_miptree_map_raw(brw, s_mt, GL_MAP_WRITE_BIT);
3426 uint32_t *z_map = intel_miptree_map_raw(brw, z_mt, GL_MAP_WRITE_BIT);
3427 unsigned int s_image_x, s_image_y;
3428 unsigned int z_image_x, z_image_y;
3429
3430 intel_miptree_get_image_offset(s_mt, level, slice,
3431 &s_image_x, &s_image_y);
3432 intel_miptree_get_image_offset(z_mt, level, slice,
3433 &z_image_x, &z_image_y);
3434
3435 for (uint32_t y = 0; y < map->h; y++) {
3436 for (uint32_t x = 0; x < map->w; x++) {
3437 ptrdiff_t s_offset = intel_offset_S8(s_mt->surf.row_pitch,
3438 x + s_image_x + map->x,
3439 y + s_image_y + map->y,
3440 brw->has_swizzling);
3441 ptrdiff_t z_offset = ((y + z_image_y + map->y) *
3442 (z_mt->surf.row_pitch / 4) +
3443 (x + z_image_x + map->x));
3444
3445 if (map_z32f_x24s8) {
3446 z_map[z_offset] = packed_map[(y * map->w + x) * 2 + 0];
3447 s_map[s_offset] = packed_map[(y * map->w + x) * 2 + 1];
3448 } else {
3449 uint32_t packed = packed_map[y * map->w + x];
3450 s_map[s_offset] = packed >> 24;
3451 z_map[z_offset] = packed;
3452 }
3453 }
3454 }
3455
3456 intel_miptree_unmap_raw(s_mt);
3457 intel_miptree_unmap_raw(z_mt);
3458
3459 DBG("%s: %d,%d %dx%d from z mt %p (%s) %d,%d, s mt %p %d,%d = %p/%d\n",
3460 __func__,
3461 map->x, map->y, map->w, map->h,
3462 z_mt, _mesa_get_format_name(z_mt->format),
3463 map->x + z_image_x, map->y + z_image_y,
3464 s_mt, map->x + s_image_x, map->y + s_image_y,
3465 map->ptr, map->stride);
3466 }
3467
3468 free(map->buffer);
3469 }
3470
3471 /**
3472 * Create and attach a map to the miptree at (level, slice). Return the
3473 * attached map.
3474 */
3475 static struct intel_miptree_map*
3476 intel_miptree_attach_map(struct intel_mipmap_tree *mt,
3477 unsigned int level,
3478 unsigned int slice,
3479 unsigned int x,
3480 unsigned int y,
3481 unsigned int w,
3482 unsigned int h,
3483 GLbitfield mode)
3484 {
3485 struct intel_miptree_map *map = calloc(1, sizeof(*map));
3486
3487 if (!map)
3488 return NULL;
3489
3490 assert(mt->level[level].slice[slice].map == NULL);
3491 mt->level[level].slice[slice].map = map;
3492
3493 map->mode = mode;
3494 map->x = x;
3495 map->y = y;
3496 map->w = w;
3497 map->h = h;
3498
3499 return map;
3500 }
3501
3502 /**
3503 * Release the map at (level, slice).
3504 */
3505 static void
3506 intel_miptree_release_map(struct intel_mipmap_tree *mt,
3507 unsigned int level,
3508 unsigned int slice)
3509 {
3510 struct intel_miptree_map **map;
3511
3512 map = &mt->level[level].slice[slice].map;
3513 free(*map);
3514 *map = NULL;
3515 }
3516
3517 static bool
3518 can_blit_slice(struct intel_mipmap_tree *mt,
3519 unsigned int level, unsigned int slice)
3520 {
3521 /* See intel_miptree_blit() for details on the 32k pitch limit. */
3522 if (mt->surf.row_pitch >= 32768)
3523 return false;
3524
3525 return true;
3526 }
3527
3528 static bool
3529 use_intel_mipree_map_blit(struct brw_context *brw,
3530 struct intel_mipmap_tree *mt,
3531 GLbitfield mode,
3532 unsigned int level,
3533 unsigned int slice)
3534 {
3535 if (brw->has_llc &&
3536 /* It's probably not worth swapping to the blit ring because of
3537 * all the overhead involved.
3538 */
3539 !(mode & GL_MAP_WRITE_BIT) &&
3540 !mt->compressed &&
3541 (mt->surf.tiling == ISL_TILING_X ||
3542 /* Prior to Sandybridge, the blitter can't handle Y tiling */
3543 (brw->gen >= 6 && mt->surf.tiling == ISL_TILING_Y0) ||
3544 /* Fast copy blit on skl+ supports all tiling formats. */
3545 brw->gen >= 9) &&
3546 can_blit_slice(mt, level, slice))
3547 return true;
3548
3549 if (mt->surf.tiling != ISL_TILING_LINEAR &&
3550 mt->bo->size >= brw->max_gtt_map_object_size) {
3551 assert(can_blit_slice(mt, level, slice));
3552 return true;
3553 }
3554
3555 return false;
3556 }
3557
3558 /**
3559 * Parameter \a out_stride has type ptrdiff_t not because the buffer stride may
3560 * exceed 32 bits but to diminish the likelihood subtle bugs in pointer
3561 * arithmetic overflow.
3562 *
3563 * If you call this function and use \a out_stride, then you're doing pointer
3564 * arithmetic on \a out_ptr. The type of \a out_stride doesn't prevent all
3565 * bugs. The caller must still take care to avoid 32-bit overflow errors in
3566 * all arithmetic expressions that contain buffer offsets and pixel sizes,
3567 * which usually have type uint32_t or GLuint.
3568 */
3569 void
3570 intel_miptree_map(struct brw_context *brw,
3571 struct intel_mipmap_tree *mt,
3572 unsigned int level,
3573 unsigned int slice,
3574 unsigned int x,
3575 unsigned int y,
3576 unsigned int w,
3577 unsigned int h,
3578 GLbitfield mode,
3579 void **out_ptr,
3580 ptrdiff_t *out_stride)
3581 {
3582 struct intel_miptree_map *map;
3583
3584 assert(mt->surf.samples == 1);
3585
3586 map = intel_miptree_attach_map(mt, level, slice, x, y, w, h, mode);
3587 if (!map){
3588 *out_ptr = NULL;
3589 *out_stride = 0;
3590 return;
3591 }
3592
3593 intel_miptree_access_raw(brw, mt, level, slice,
3594 map->mode & GL_MAP_WRITE_BIT);
3595
3596 if (mt->format == MESA_FORMAT_S_UINT8) {
3597 intel_miptree_map_s8(brw, mt, map, level, slice);
3598 } else if (mt->etc_format != MESA_FORMAT_NONE &&
3599 !(mode & BRW_MAP_DIRECT_BIT)) {
3600 intel_miptree_map_etc(brw, mt, map, level, slice);
3601 } else if (mt->stencil_mt && !(mode & BRW_MAP_DIRECT_BIT)) {
3602 intel_miptree_map_depthstencil(brw, mt, map, level, slice);
3603 } else if (use_intel_mipree_map_blit(brw, mt, mode, level, slice)) {
3604 intel_miptree_map_blit(brw, mt, map, level, slice);
3605 #if defined(USE_SSE41)
3606 } else if (!(mode & GL_MAP_WRITE_BIT) &&
3607 !mt->compressed && cpu_has_sse4_1 &&
3608 (mt->surf.row_pitch % 16 == 0)) {
3609 intel_miptree_map_movntdqa(brw, mt, map, level, slice);
3610 #endif
3611 } else {
3612 intel_miptree_map_gtt(brw, mt, map, level, slice);
3613 }
3614
3615 *out_ptr = map->ptr;
3616 *out_stride = map->stride;
3617
3618 if (map->ptr == NULL)
3619 intel_miptree_release_map(mt, level, slice);
3620 }
3621
3622 void
3623 intel_miptree_unmap(struct brw_context *brw,
3624 struct intel_mipmap_tree *mt,
3625 unsigned int level,
3626 unsigned int slice)
3627 {
3628 struct intel_miptree_map *map = mt->level[level].slice[slice].map;
3629
3630 assert(mt->surf.samples == 1);
3631
3632 if (!map)
3633 return;
3634
3635 DBG("%s: mt %p (%s) level %d slice %d\n", __func__,
3636 mt, _mesa_get_format_name(mt->format), level, slice);
3637
3638 if (mt->format == MESA_FORMAT_S_UINT8) {
3639 intel_miptree_unmap_s8(brw, mt, map, level, slice);
3640 } else if (mt->etc_format != MESA_FORMAT_NONE &&
3641 !(map->mode & BRW_MAP_DIRECT_BIT)) {
3642 intel_miptree_unmap_etc(brw, mt, map, level, slice);
3643 } else if (mt->stencil_mt && !(map->mode & BRW_MAP_DIRECT_BIT)) {
3644 intel_miptree_unmap_depthstencil(brw, mt, map, level, slice);
3645 } else if (map->linear_mt) {
3646 intel_miptree_unmap_blit(brw, mt, map, level, slice);
3647 #if defined(USE_SSE41)
3648 } else if (map->buffer && cpu_has_sse4_1) {
3649 intel_miptree_unmap_movntdqa(brw, mt, map, level, slice);
3650 #endif
3651 } else {
3652 intel_miptree_unmap_gtt(mt);
3653 }
3654
3655 intel_miptree_release_map(mt, level, slice);
3656 }
3657
3658 enum isl_surf_dim
3659 get_isl_surf_dim(GLenum target)
3660 {
3661 switch (target) {
3662 case GL_TEXTURE_1D:
3663 case GL_TEXTURE_1D_ARRAY:
3664 return ISL_SURF_DIM_1D;
3665
3666 case GL_TEXTURE_2D:
3667 case GL_TEXTURE_2D_ARRAY:
3668 case GL_TEXTURE_RECTANGLE:
3669 case GL_TEXTURE_CUBE_MAP:
3670 case GL_TEXTURE_CUBE_MAP_ARRAY:
3671 case GL_TEXTURE_2D_MULTISAMPLE:
3672 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
3673 case GL_TEXTURE_EXTERNAL_OES:
3674 return ISL_SURF_DIM_2D;
3675
3676 case GL_TEXTURE_3D:
3677 return ISL_SURF_DIM_3D;
3678 }
3679
3680 unreachable("Invalid texture target");
3681 }
3682
3683 enum isl_dim_layout
3684 get_isl_dim_layout(const struct gen_device_info *devinfo,
3685 enum isl_tiling tiling, GLenum target)
3686 {
3687 switch (target) {
3688 case GL_TEXTURE_1D:
3689 case GL_TEXTURE_1D_ARRAY:
3690 return (devinfo->gen >= 9 && tiling == ISL_TILING_LINEAR ?
3691 ISL_DIM_LAYOUT_GEN9_1D : ISL_DIM_LAYOUT_GEN4_2D);
3692
3693 case GL_TEXTURE_2D:
3694 case GL_TEXTURE_2D_ARRAY:
3695 case GL_TEXTURE_RECTANGLE:
3696 case GL_TEXTURE_2D_MULTISAMPLE:
3697 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
3698 case GL_TEXTURE_EXTERNAL_OES:
3699 return ISL_DIM_LAYOUT_GEN4_2D;
3700
3701 case GL_TEXTURE_CUBE_MAP:
3702 case GL_TEXTURE_CUBE_MAP_ARRAY:
3703 return (devinfo->gen == 4 ? ISL_DIM_LAYOUT_GEN4_3D :
3704 ISL_DIM_LAYOUT_GEN4_2D);
3705
3706 case GL_TEXTURE_3D:
3707 return (devinfo->gen >= 9 ?
3708 ISL_DIM_LAYOUT_GEN4_2D : ISL_DIM_LAYOUT_GEN4_3D);
3709 }
3710
3711 unreachable("Invalid texture target");
3712 }
3713
3714 enum isl_aux_usage
3715 intel_miptree_get_aux_isl_usage(const struct brw_context *brw,
3716 const struct intel_mipmap_tree *mt)
3717 {
3718 if (mt->hiz_buf)
3719 return ISL_AUX_USAGE_HIZ;
3720
3721 if (!mt->mcs_buf)
3722 return ISL_AUX_USAGE_NONE;
3723
3724 return mt->aux_usage;
3725 }