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