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