2 * Copyright 2006 VMware, Inc.
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:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
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.
27 #include <GL/internal/dri_interface.h>
29 #include "intel_batchbuffer.h"
30 #include "intel_mipmap_tree.h"
31 #include "intel_resolve_map.h"
32 #include "intel_tex.h"
33 #include "intel_blit.h"
34 #include "intel_fbo.h"
36 #include "brw_blorp.h"
37 #include "brw_context.h"
38 #include "brw_state.h"
40 #include "main/enums.h"
41 #include "main/fbobject.h"
42 #include "main/formats.h"
43 #include "main/glformats.h"
44 #include "main/texcompress_etc.h"
45 #include "main/teximage.h"
46 #include "main/streaming-load-memcpy.h"
47 #include "x86/common_x86_asm.h"
49 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
51 static void *intel_miptree_map_raw(struct brw_context
*brw
,
52 struct intel_mipmap_tree
*mt
,
55 static void intel_miptree_unmap_raw(struct intel_mipmap_tree
*mt
);
58 intel_miptree_alloc_mcs(struct brw_context
*brw
,
59 struct intel_mipmap_tree
*mt
,
63 * Determine which MSAA layout should be used by the MSAA surface being
64 * created, based on the chip generation and the surface type.
66 static enum intel_msaa_layout
67 compute_msaa_layout(struct brw_context
*brw
, mesa_format format
,
68 enum intel_aux_disable aux_disable
)
70 /* Prior to Gen7, all MSAA surfaces used IMS layout. */
72 return INTEL_MSAA_LAYOUT_IMS
;
74 /* In Gen7, IMS layout is only used for depth and stencil buffers. */
75 switch (_mesa_get_format_base_format(format
)) {
76 case GL_DEPTH_COMPONENT
:
77 case GL_STENCIL_INDEX
:
78 case GL_DEPTH_STENCIL
:
79 return INTEL_MSAA_LAYOUT_IMS
;
81 /* From the Ivy Bridge PRM, Vol4 Part1 p77 ("MCS Enable"):
83 * This field must be set to 0 for all SINT MSRTs when all RT channels
86 * In practice this means that we have to disable MCS for all signed
87 * integer MSAA buffers. The alternative, to disable MCS only when one
88 * of the render target channels is disabled, is impractical because it
89 * would require converting between CMS and UMS MSAA layouts on the fly,
92 if (brw
->gen
== 7 && _mesa_get_format_datatype(format
) == GL_INT
) {
93 return INTEL_MSAA_LAYOUT_UMS
;
94 } else if (aux_disable
& INTEL_AUX_DISABLE_MCS
) {
95 /* We can't use the CMS layout because it uses an aux buffer, the MCS
96 * buffer. So fallback to UMS, which is identical to CMS without the
98 return INTEL_MSAA_LAYOUT_UMS
;
100 return INTEL_MSAA_LAYOUT_CMS
;
106 intel_tiling_supports_non_msrt_mcs(const struct brw_context
*brw
,
109 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
110 * Target(s)", beneath the "Fast Color Clear" bullet (p326):
112 * - Support is limited to tiled render targets.
114 * Gen9 changes the restriction to Y-tile only.
117 return tiling
== I915_TILING_Y
;
118 else if (brw
->gen
>= 7)
119 return tiling
!= I915_TILING_NONE
;
125 * For a single-sampled render target ("non-MSRT"), determine if an MCS buffer
126 * can be used. This doesn't (and should not) inspect any of the properties of
129 * From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render Target(s)",
130 * beneath the "Fast Color Clear" bullet (p326):
132 * - Support is for non-mip-mapped and non-array surface types only.
134 * And then later, on p327:
136 * - MCS buffer for non-MSRT is supported only for RT formats 32bpp,
139 * From the Skylake documentation, it is made clear that X-tiling is no longer
142 * - MCS and Lossless compression is supported for TiledY/TileYs/TileYf
146 intel_miptree_supports_non_msrt_fast_clear(struct brw_context
*brw
,
147 const struct intel_mipmap_tree
*mt
)
149 /* MCS support does not exist prior to Gen7 */
153 if (mt
->aux_disable
& INTEL_AUX_DISABLE_MCS
)
156 /* This function applies only to non-multisampled render targets. */
157 if (mt
->num_samples
> 1)
160 /* MCS is only supported for color buffers */
161 switch (_mesa_get_format_base_format(mt
->format
)) {
162 case GL_DEPTH_COMPONENT
:
163 case GL_DEPTH_STENCIL
:
164 case GL_STENCIL_INDEX
:
168 if (mt
->cpp
!= 4 && mt
->cpp
!= 8 && mt
->cpp
!= 16)
171 const bool mip_mapped
= mt
->first_level
!= 0 || mt
->last_level
!= 0;
172 const bool arrayed
= mt
->physical_depth0
!= 1;
175 /* Multisample surfaces with the CMS layout are not layered surfaces,
176 * yet still have physical_depth0 > 1. Assert that we don't
177 * accidentally reject a multisampled surface here. We should have
178 * rejected it earlier by explicitly checking the sample count.
180 assert(mt
->num_samples
<= 1);
183 /* Handle the hardware restrictions...
185 * All GENs have the following restriction: "MCS buffer for non-MSRT is
186 * supported only for RT formats 32bpp, 64bpp, and 128bpp."
188 * From the HSW PRM Volume 7: 3D-Media-GPGPU, page 652: (Color Clear of
189 * Non-MultiSampler Render Target Restrictions) Support is for
190 * non-mip-mapped and non-array surface types only.
192 * From the BDW PRM Volume 7: 3D-Media-GPGPU, page 649: (Color Clear of
193 * Non-MultiSampler Render Target Restriction). Mip-mapped and arrayed
194 * surfaces are supported with MCS buffer layout with these alignments in
195 * the RT space: Horizontal Alignment = 256 and Vertical Alignment = 128.
197 * From the SKL PRM Volume 7: 3D-Media-GPGPU, page 632: (Color Clear of
198 * Non-MultiSampler Render Target Restriction). Mip-mapped and arrayed
199 * surfaces are supported with MCS buffer layout with these alignments in
200 * the RT space: Horizontal Alignment = 128 and Vertical Alignment = 64.
202 if (brw
->gen
< 8 && (mip_mapped
|| arrayed
))
205 /* There's no point in using an MCS buffer if the surface isn't in a
208 if (!brw
->format_supported_as_render_target
[mt
->format
])
212 mesa_format linear_format
= _mesa_get_srgb_format_linear(mt
->format
);
213 const enum isl_format isl_format
=
214 brw_isl_format_for_mesa_format(linear_format
);
215 return isl_format_supports_ccs_e(&brw
->screen
->devinfo
, isl_format
);
220 /* On Gen9 support for color buffer compression was extended to single
221 * sampled surfaces. This is a helper considering both auxiliary buffer
222 * type and number of samples telling if the given miptree represents
223 * the new single sampled case - also called lossless compression.
226 intel_miptree_is_lossless_compressed(const struct brw_context
*brw
,
227 const struct intel_mipmap_tree
*mt
)
229 /* Only available from Gen9 onwards. */
233 /* Compression always requires auxiliary buffer. */
237 /* Single sample compression is represented re-using msaa compression
238 * layout type: "Compressed Multisampled Surfaces".
240 if (mt
->msaa_layout
!= INTEL_MSAA_LAYOUT_CMS
)
243 /* And finally distinguish between msaa and single sample case. */
244 return mt
->num_samples
<= 1;
248 intel_miptree_supports_lossless_compressed(struct brw_context
*brw
,
249 const struct intel_mipmap_tree
*mt
)
251 /* For now compression is only enabled for integer formats even though
252 * there exist supported floating point formats also. This is a heuristic
253 * decision based on current public benchmarks. In none of the cases these
254 * formats provided any improvement but a few cases were seen to regress.
255 * Hence these are left to to be enabled in the future when they are known
258 if (_mesa_get_format_datatype(mt
->format
) == GL_FLOAT
)
261 /* Fast clear mechanism and lossless compression go hand in hand. */
262 if (!intel_miptree_supports_non_msrt_fast_clear(brw
, mt
))
265 /* Fast clear can be also used to clear srgb surfaces by using equivalent
266 * linear format. This trick, however, can't be extended to be used with
267 * lossless compression and therefore a check is needed to see if the format
270 return _mesa_get_srgb_format_linear(mt
->format
) == mt
->format
;
274 * Determine depth format corresponding to a depth+stencil format,
275 * for separate stencil.
278 intel_depth_format_for_depthstencil_format(mesa_format format
) {
280 case MESA_FORMAT_Z24_UNORM_S8_UINT
:
281 return MESA_FORMAT_Z24_UNORM_X8_UINT
;
282 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT
:
283 return MESA_FORMAT_Z_FLOAT32
;
291 * @param for_bo Indicates that the caller is
292 * intel_miptree_create_for_bo(). If true, then do not create
295 static struct intel_mipmap_tree
*
296 intel_miptree_create_layout(struct brw_context
*brw
,
305 uint32_t layout_flags
)
307 struct intel_mipmap_tree
*mt
= calloc(sizeof(*mt
), 1);
311 DBG("%s target %s format %s level %d..%d slices %d <-- %p\n", __func__
,
312 _mesa_enum_to_string(target
),
313 _mesa_get_format_name(format
),
314 first_level
, last_level
, depth0
, mt
);
316 if (target
== GL_TEXTURE_1D_ARRAY
)
317 assert(height0
== 1);
321 mt
->first_level
= first_level
;
322 mt
->last_level
= last_level
;
323 mt
->logical_width0
= width0
;
324 mt
->logical_height0
= height0
;
325 mt
->logical_depth0
= depth0
;
326 mt
->aux_disable
= (layout_flags
& MIPTREE_LAYOUT_DISABLE_AUX
) != 0 ?
327 INTEL_AUX_DISABLE_ALL
: INTEL_AUX_DISABLE_NONE
;
328 mt
->aux_disable
|= INTEL_AUX_DISABLE_CCS
;
329 mt
->is_scanout
= (layout_flags
& MIPTREE_LAYOUT_FOR_SCANOUT
) != 0;
330 exec_list_make_empty(&mt
->hiz_map
);
331 exec_list_make_empty(&mt
->color_resolve_map
);
332 mt
->cpp
= _mesa_get_format_bytes(format
);
333 mt
->num_samples
= num_samples
;
334 mt
->compressed
= _mesa_is_format_compressed(format
);
335 mt
->msaa_layout
= INTEL_MSAA_LAYOUT_NONE
;
338 int depth_multiply
= 1;
339 if (num_samples
> 1) {
340 /* Adjust width/height/depth for MSAA */
341 mt
->msaa_layout
= compute_msaa_layout(brw
, format
, mt
->aux_disable
);
342 if (mt
->msaa_layout
== INTEL_MSAA_LAYOUT_IMS
) {
343 /* From the Ivybridge PRM, Volume 1, Part 1, page 108:
344 * "If the surface is multisampled and it is a depth or stencil
345 * surface or Multisampled Surface StorageFormat in SURFACE_STATE is
346 * MSFMT_DEPTH_STENCIL, WL and HL must be adjusted as follows before
349 * +----------------------------------------------------------------+
350 * | Num Multisamples | W_l = | H_l = |
351 * +----------------------------------------------------------------+
352 * | 2 | ceiling(W_l / 2) * 4 | H_l (no adjustment) |
353 * | 4 | ceiling(W_l / 2) * 4 | ceiling(H_l / 2) * 4 |
354 * | 8 | ceiling(W_l / 2) * 8 | ceiling(H_l / 2) * 4 |
355 * | 16 | ceiling(W_l / 2) * 8 | ceiling(H_l / 2) * 8 |
356 * +----------------------------------------------------------------+
359 * Note that MSFMT_DEPTH_STENCIL just means the IMS (interleaved)
360 * format rather than UMS/CMS (array slices). The Sandybridge PRM,
361 * Volume 1, Part 1, Page 111 has the same formula for 4x MSAA.
363 * Another more complicated explanation for these adjustments comes
364 * from the Sandybridge PRM, volume 4, part 1, page 31:
366 * "Any of the other messages (sample*, LOD, load4) used with a
367 * (4x) multisampled surface will in-effect sample a surface with
368 * double the height and width as that indicated in the surface
369 * state. Each pixel position on the original-sized surface is
370 * replaced with a 2x2 of samples with the following arrangement:
375 * Thus, when sampling from a multisampled texture, it behaves as
376 * though the layout in memory for (x,y,sample) is:
378 * (0,0,0) (0,0,2) (1,0,0) (1,0,2)
379 * (0,0,1) (0,0,3) (1,0,1) (1,0,3)
381 * (0,1,0) (0,1,2) (1,1,0) (1,1,2)
382 * (0,1,1) (0,1,3) (1,1,1) (1,1,3)
384 * However, the actual layout of multisampled data in memory is:
386 * (0,0,0) (1,0,0) (0,0,1) (1,0,1)
387 * (0,1,0) (1,1,0) (0,1,1) (1,1,1)
389 * (0,0,2) (1,0,2) (0,0,3) (1,0,3)
390 * (0,1,2) (1,1,2) (0,1,3) (1,1,3)
392 * This pattern repeats for each 2x2 pixel block.
394 * As a result, when calculating the size of our 4-sample buffer for
395 * an odd width or height, we have to align before scaling up because
396 * sample 3 is in that bottom right 2x2 block.
398 switch (num_samples
) {
400 assert(brw
->gen
>= 8);
401 width0
= ALIGN(width0
, 2) * 2;
402 height0
= ALIGN(height0
, 2);
405 width0
= ALIGN(width0
, 2) * 2;
406 height0
= ALIGN(height0
, 2) * 2;
409 width0
= ALIGN(width0
, 2) * 4;
410 height0
= ALIGN(height0
, 2) * 2;
413 width0
= ALIGN(width0
, 2) * 4;
414 height0
= ALIGN(height0
, 2) * 4;
417 /* num_samples should already have been quantized to 0, 1, 2, 4, 8
420 unreachable("not reached");
423 /* Non-interleaved */
424 depth_multiply
= num_samples
;
425 depth0
*= depth_multiply
;
429 /* Set array_layout to ALL_SLICES_AT_EACH_LOD when array_spacing_lod0 can
430 * be used. array_spacing_lod0 is only used for non-IMS MSAA surfaces on
431 * Gen 7 and 8. On Gen 8 and 9 this layout is not available but it is still
432 * used on Gen8 to make it pick a qpitch value which doesn't include space
433 * for the mipmaps. On Gen9 this is not necessary because it will
434 * automatically pick a packed qpitch value whenever mt->first_level ==
436 * TODO: can we use it elsewhere?
437 * TODO: also disable this on Gen8 and pick the qpitch value like Gen9
440 mt
->array_layout
= ALL_LOD_IN_EACH_SLICE
;
442 switch (mt
->msaa_layout
) {
443 case INTEL_MSAA_LAYOUT_NONE
:
444 case INTEL_MSAA_LAYOUT_IMS
:
445 mt
->array_layout
= ALL_LOD_IN_EACH_SLICE
;
447 case INTEL_MSAA_LAYOUT_UMS
:
448 case INTEL_MSAA_LAYOUT_CMS
:
449 mt
->array_layout
= ALL_SLICES_AT_EACH_LOD
;
454 if (target
== GL_TEXTURE_CUBE_MAP
)
455 assert(depth0
== 6 * depth_multiply
);
457 mt
->physical_width0
= width0
;
458 mt
->physical_height0
= height0
;
459 mt
->physical_depth0
= depth0
;
461 if (!(layout_flags
& MIPTREE_LAYOUT_FOR_BO
) &&
462 _mesa_get_format_base_format(format
) == GL_DEPTH_STENCIL
&&
463 (brw
->must_use_separate_stencil
||
464 (brw
->has_separate_stencil
&&
465 intel_miptree_wants_hiz_buffer(brw
, mt
)))) {
466 uint32_t stencil_flags
= MIPTREE_LAYOUT_ACCELERATED_UPLOAD
;
468 stencil_flags
|= MIPTREE_LAYOUT_GEN6_HIZ_STENCIL
|
469 MIPTREE_LAYOUT_TILING_ANY
;
472 mt
->stencil_mt
= intel_miptree_create(brw
,
483 if (!mt
->stencil_mt
) {
484 intel_miptree_release(&mt
);
487 mt
->stencil_mt
->r8stencil_needs_update
= true;
489 /* Fix up the Z miptree format for how we're splitting out separate
490 * stencil. Gen7 expects there to be no stencil bits in its depth buffer.
492 mt
->format
= intel_depth_format_for_depthstencil_format(mt
->format
);
495 if (format
== mt
->format
) {
496 _mesa_problem(NULL
, "Unknown format %s in separate stencil mt\n",
497 _mesa_get_format_name(mt
->format
));
501 if (layout_flags
& MIPTREE_LAYOUT_GEN6_HIZ_STENCIL
)
502 mt
->array_layout
= GEN6_HIZ_STENCIL
;
505 * Obey HALIGN_16 constraints for Gen8 and Gen9 buffers which are
506 * multisampled or have an AUX buffer attached to it.
508 * GEN | MSRT | AUX_CCS_* or AUX_MCS
509 * -------------------------------------------
510 * 9 | HALIGN_16 | HALIGN_16
511 * 8 | HALIGN_ANY | HALIGN_16
515 if (intel_miptree_supports_non_msrt_fast_clear(brw
, mt
)) {
516 if (brw
->gen
>= 9 || (brw
->gen
== 8 && num_samples
<= 1))
517 layout_flags
|= MIPTREE_LAYOUT_FORCE_HALIGN16
;
518 } else if (brw
->gen
>= 9 && num_samples
> 1) {
519 layout_flags
|= MIPTREE_LAYOUT_FORCE_HALIGN16
;
521 const UNUSED
bool is_lossless_compressed_aux
=
522 brw
->gen
>= 9 && num_samples
== 1 &&
523 mt
->format
== MESA_FORMAT_R_UINT32
;
525 /* For now, nothing else has this requirement */
526 assert(is_lossless_compressed_aux
||
527 (layout_flags
& MIPTREE_LAYOUT_FORCE_HALIGN16
) == 0);
530 if (!brw_miptree_layout(brw
, mt
, layout_flags
)) {
531 intel_miptree_release(&mt
);
535 if (mt
->aux_disable
& INTEL_AUX_DISABLE_MCS
)
536 assert(mt
->msaa_layout
!= INTEL_MSAA_LAYOUT_CMS
);
543 * Choose an appropriate uncompressed format for a requested
544 * compressed format, if unsupported.
547 intel_lower_compressed_format(struct brw_context
*brw
, mesa_format format
)
549 /* No need to lower ETC formats on these platforms,
550 * they are supported natively.
552 if (brw
->gen
>= 8 || brw
->is_baytrail
)
556 case MESA_FORMAT_ETC1_RGB8
:
557 return MESA_FORMAT_R8G8B8X8_UNORM
;
558 case MESA_FORMAT_ETC2_RGB8
:
559 return MESA_FORMAT_R8G8B8X8_UNORM
;
560 case MESA_FORMAT_ETC2_SRGB8
:
561 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC
:
562 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1
:
563 return MESA_FORMAT_B8G8R8A8_SRGB
;
564 case MESA_FORMAT_ETC2_RGBA8_EAC
:
565 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1
:
566 return MESA_FORMAT_R8G8B8A8_UNORM
;
567 case MESA_FORMAT_ETC2_R11_EAC
:
568 return MESA_FORMAT_R_UNORM16
;
569 case MESA_FORMAT_ETC2_SIGNED_R11_EAC
:
570 return MESA_FORMAT_R_SNORM16
;
571 case MESA_FORMAT_ETC2_RG11_EAC
:
572 return MESA_FORMAT_R16G16_UNORM
;
573 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC
:
574 return MESA_FORMAT_R16G16_SNORM
;
576 /* Non ETC1 / ETC2 format */
581 static struct intel_mipmap_tree
*
582 miptree_create(struct brw_context
*brw
,
591 uint32_t layout_flags
)
593 struct intel_mipmap_tree
*mt
;
594 mesa_format tex_format
= format
;
595 mesa_format etc_format
= MESA_FORMAT_NONE
;
596 uint32_t alloc_flags
= 0;
598 format
= intel_lower_compressed_format(brw
, format
);
600 etc_format
= (format
!= tex_format
) ? tex_format
: MESA_FORMAT_NONE
;
602 assert((layout_flags
& MIPTREE_LAYOUT_FOR_BO
) == 0);
603 mt
= intel_miptree_create_layout(brw
, target
, format
,
604 first_level
, last_level
, width0
,
605 height0
, depth0
, num_samples
,
610 if (mt
->tiling
== (I915_TILING_Y
| I915_TILING_X
))
611 mt
->tiling
= I915_TILING_Y
;
613 if (layout_flags
& MIPTREE_LAYOUT_ACCELERATED_UPLOAD
)
614 alloc_flags
|= BO_ALLOC_FOR_RENDER
;
616 mt
->etc_format
= etc_format
;
618 if (format
== MESA_FORMAT_S_UINT8
) {
619 /* Align to size of W tile, 64x64. */
620 mt
->bo
= brw_bo_alloc_tiled(brw
->bufmgr
, "miptree",
621 ALIGN(mt
->total_width
, 64),
622 ALIGN(mt
->total_height
, 64),
623 mt
->cpp
, mt
->tiling
, &mt
->pitch
,
626 mt
->bo
= brw_bo_alloc_tiled(brw
->bufmgr
, "miptree",
627 mt
->total_width
, mt
->total_height
,
628 mt
->cpp
, mt
->tiling
, &mt
->pitch
,
632 if (layout_flags
& MIPTREE_LAYOUT_FOR_SCANOUT
)
633 mt
->bo
->cache_coherent
= false;
638 struct intel_mipmap_tree
*
639 intel_miptree_create(struct brw_context
*brw
,
648 uint32_t layout_flags
)
650 struct intel_mipmap_tree
*mt
= miptree_create(
652 first_level
, last_level
,
653 width0
, height0
, depth0
, num_samples
,
656 /* If the BO is too large to fit in the aperture, we need to use the
657 * BLT engine to support it. Prior to Sandybridge, the BLT paths can't
658 * handle Y-tiling, so we need to fall back to X.
660 if (brw
->gen
< 6 && mt
->bo
->size
>= brw
->max_gtt_map_object_size
&&
661 mt
->tiling
== I915_TILING_Y
) {
662 const uint32_t alloc_flags
=
663 (layout_flags
& MIPTREE_LAYOUT_ACCELERATED_UPLOAD
) ?
664 BO_ALLOC_FOR_RENDER
: 0;
665 perf_debug("%dx%d miptree larger than aperture; falling back to X-tiled\n",
666 mt
->total_width
, mt
->total_height
);
668 mt
->tiling
= I915_TILING_X
;
669 brw_bo_unreference(mt
->bo
);
670 mt
->bo
= brw_bo_alloc_tiled(brw
->bufmgr
, "miptree",
671 mt
->total_width
, mt
->total_height
, mt
->cpp
,
672 mt
->tiling
, &mt
->pitch
, alloc_flags
);
678 intel_miptree_release(&mt
);
683 if (mt
->msaa_layout
== INTEL_MSAA_LAYOUT_CMS
) {
684 assert(mt
->num_samples
> 1);
685 if (!intel_miptree_alloc_mcs(brw
, mt
, num_samples
)) {
686 intel_miptree_release(&mt
);
691 /* If this miptree is capable of supporting fast color clears, set
692 * fast_clear_state appropriately to ensure that fast clears will occur.
693 * Allocation of the MCS miptree will be deferred until the first fast
694 * clear actually occurs or when compressed single sampled buffer is
695 * written by the GPU for the first time.
697 if (intel_tiling_supports_non_msrt_mcs(brw
, mt
->tiling
) &&
698 intel_miptree_supports_non_msrt_fast_clear(brw
, mt
)) {
699 mt
->aux_disable
&= ~INTEL_AUX_DISABLE_CCS
;
700 assert(brw
->gen
< 8 || mt
->halign
== 16 || num_samples
<= 1);
702 /* On Gen9+ clients are not currently capable of consuming compressed
703 * single-sampled buffers. Disabling compression allows us to skip
706 const bool lossless_compression_disabled
= INTEL_DEBUG
& DEBUG_NO_RBC
;
707 const bool is_lossless_compressed
=
708 unlikely(!lossless_compression_disabled
) &&
709 brw
->gen
>= 9 && !mt
->is_scanout
&&
710 intel_miptree_supports_lossless_compressed(brw
, mt
);
712 if (is_lossless_compressed
) {
713 intel_miptree_alloc_non_msrt_mcs(brw
, mt
, is_lossless_compressed
);
720 struct intel_mipmap_tree
*
721 intel_miptree_create_for_bo(struct brw_context
*brw
,
729 uint32_t layout_flags
)
731 struct intel_mipmap_tree
*mt
;
732 uint32_t tiling
, swizzle
;
735 brw_bo_get_tiling(bo
, &tiling
, &swizzle
);
737 /* Nothing will be able to use this miptree with the BO if the offset isn't
740 if (tiling
!= I915_TILING_NONE
)
741 assert(offset
% 4096 == 0);
743 /* miptrees can't handle negative pitch. If you need flipping of images,
744 * that's outside of the scope of the mt.
748 target
= depth
> 1 ? GL_TEXTURE_2D_ARRAY
: GL_TEXTURE_2D
;
750 /* The BO already has a tiling format and we shouldn't confuse the lower
751 * layers by making it try to find a tiling format again.
753 assert((layout_flags
& MIPTREE_LAYOUT_TILING_ANY
) == 0);
754 assert((layout_flags
& MIPTREE_LAYOUT_TILING_NONE
) == 0);
756 layout_flags
|= MIPTREE_LAYOUT_FOR_BO
;
757 mt
= intel_miptree_create_layout(brw
, target
, format
,
759 width
, height
, depth
, 0,
764 brw_bo_reference(bo
);
774 * For a singlesample renderbuffer, this simply wraps the given BO with a
777 * For a multisample renderbuffer, this wraps the window system's
778 * (singlesample) BO with a singlesample miptree attached to the
779 * intel_renderbuffer, then creates a multisample miptree attached to irb->mt
780 * that will contain the actual rendering (which is lazily resolved to
781 * irb->singlesample_mt).
784 intel_update_winsys_renderbuffer_miptree(struct brw_context
*intel
,
785 struct intel_renderbuffer
*irb
,
787 uint32_t width
, uint32_t height
,
790 struct intel_mipmap_tree
*singlesample_mt
= NULL
;
791 struct intel_mipmap_tree
*multisample_mt
= NULL
;
792 struct gl_renderbuffer
*rb
= &irb
->Base
.Base
;
793 mesa_format format
= rb
->Format
;
794 int num_samples
= rb
->NumSamples
;
796 /* Only the front and back buffers, which are color buffers, are allocated
797 * through the image loader.
799 assert(_mesa_get_format_base_format(format
) == GL_RGB
||
800 _mesa_get_format_base_format(format
) == GL_RGBA
);
802 singlesample_mt
= intel_miptree_create_for_bo(intel
,
810 MIPTREE_LAYOUT_FOR_SCANOUT
);
811 if (!singlesample_mt
)
814 /* If this miptree is capable of supporting fast color clears, set
815 * mcs_state appropriately to ensure that fast clears will occur.
816 * Allocation of the MCS miptree will be deferred until the first fast
817 * clear actually occurs.
819 if (intel_tiling_supports_non_msrt_mcs(intel
, singlesample_mt
->tiling
) &&
820 intel_miptree_supports_non_msrt_fast_clear(intel
, singlesample_mt
)) {
821 singlesample_mt
->aux_disable
&= ~INTEL_AUX_DISABLE_CCS
;
824 if (num_samples
== 0) {
825 intel_miptree_release(&irb
->mt
);
826 irb
->mt
= singlesample_mt
;
828 assert(!irb
->singlesample_mt
);
830 intel_miptree_release(&irb
->singlesample_mt
);
831 irb
->singlesample_mt
= singlesample_mt
;
834 irb
->mt
->logical_width0
!= width
||
835 irb
->mt
->logical_height0
!= height
) {
836 multisample_mt
= intel_miptree_create_for_renderbuffer(intel
,
844 irb
->need_downsample
= false;
845 intel_miptree_release(&irb
->mt
);
846 irb
->mt
= multisample_mt
;
852 intel_miptree_release(&irb
->singlesample_mt
);
853 intel_miptree_release(&irb
->mt
);
857 struct intel_mipmap_tree
*
858 intel_miptree_create_for_renderbuffer(struct brw_context
*brw
,
862 uint32_t num_samples
)
864 struct intel_mipmap_tree
*mt
;
867 GLenum target
= num_samples
> 1 ? GL_TEXTURE_2D_MULTISAMPLE
: GL_TEXTURE_2D
;
868 const uint32_t layout_flags
= MIPTREE_LAYOUT_ACCELERATED_UPLOAD
|
869 MIPTREE_LAYOUT_TILING_ANY
|
870 MIPTREE_LAYOUT_FOR_SCANOUT
;
872 mt
= intel_miptree_create(brw
, target
, format
, 0, 0,
873 width
, height
, depth
, num_samples
,
878 if (intel_miptree_wants_hiz_buffer(brw
, mt
)) {
879 ok
= intel_miptree_alloc_hiz(brw
, mt
);
887 intel_miptree_release(&mt
);
892 intel_miptree_reference(struct intel_mipmap_tree
**dst
,
893 struct intel_mipmap_tree
*src
)
898 intel_miptree_release(dst
);
902 DBG("%s %p refcount now %d\n", __func__
, src
, src
->refcount
);
909 intel_miptree_hiz_buffer_free(struct intel_miptree_hiz_buffer
*hiz_buf
)
915 intel_miptree_release(&hiz_buf
->mt
);
917 brw_bo_unreference(hiz_buf
->aux_base
.bo
);
923 intel_miptree_release(struct intel_mipmap_tree
**mt
)
928 DBG("%s %p refcount will be %d\n", __func__
, *mt
, (*mt
)->refcount
- 1);
929 if (--(*mt
)->refcount
<= 0) {
932 DBG("%s deleting %p\n", __func__
, *mt
);
934 brw_bo_unreference((*mt
)->bo
);
935 intel_miptree_release(&(*mt
)->stencil_mt
);
936 intel_miptree_release(&(*mt
)->r8stencil_mt
);
937 intel_miptree_hiz_buffer_free((*mt
)->hiz_buf
);
938 if ((*mt
)->mcs_buf
) {
939 brw_bo_unreference((*mt
)->mcs_buf
->bo
);
940 free((*mt
)->mcs_buf
);
942 intel_resolve_map_clear(&(*mt
)->hiz_map
);
943 intel_resolve_map_clear(&(*mt
)->color_resolve_map
);
945 intel_miptree_release(&(*mt
)->plane
[0]);
946 intel_miptree_release(&(*mt
)->plane
[1]);
948 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
949 free((*mt
)->level
[i
].slice
);
959 intel_get_image_dims(struct gl_texture_image
*image
,
960 int *width
, int *height
, int *depth
)
962 switch (image
->TexObject
->Target
) {
963 case GL_TEXTURE_1D_ARRAY
:
964 /* For a 1D Array texture the OpenGL API will treat the image height as
965 * the number of array slices. For Intel hardware, we treat the 1D array
966 * as a 2D Array with a height of 1. So, here we want to swap image
969 assert(image
->Depth
== 1);
970 *width
= image
->Width
;
972 *depth
= image
->Height
;
974 case GL_TEXTURE_CUBE_MAP
:
975 /* For Cube maps, the mesa/main api layer gives us a depth of 1 even
976 * though we really have 6 slices.
978 assert(image
->Depth
== 1);
979 *width
= image
->Width
;
980 *height
= image
->Height
;
984 *width
= image
->Width
;
985 *height
= image
->Height
;
986 *depth
= image
->Depth
;
992 * Can the image be pulled into a unified mipmap tree? This mirrors
993 * the completeness test in a lot of ways.
995 * Not sure whether I want to pass gl_texture_image here.
998 intel_miptree_match_image(struct intel_mipmap_tree
*mt
,
999 struct gl_texture_image
*image
)
1001 struct intel_texture_image
*intelImage
= intel_texture_image(image
);
1002 GLuint level
= intelImage
->base
.Base
.Level
;
1003 int width
, height
, depth
;
1005 /* glTexImage* choose the texture object based on the target passed in, and
1006 * objects can't change targets over their lifetimes, so this should be
1009 assert(image
->TexObject
->Target
== mt
->target
);
1011 mesa_format mt_format
= mt
->format
;
1012 if (mt
->format
== MESA_FORMAT_Z24_UNORM_X8_UINT
&& mt
->stencil_mt
)
1013 mt_format
= MESA_FORMAT_Z24_UNORM_S8_UINT
;
1014 if (mt
->format
== MESA_FORMAT_Z_FLOAT32
&& mt
->stencil_mt
)
1015 mt_format
= MESA_FORMAT_Z32_FLOAT_S8X24_UINT
;
1016 if (mt
->etc_format
!= MESA_FORMAT_NONE
)
1017 mt_format
= mt
->etc_format
;
1019 if (image
->TexFormat
!= mt_format
)
1022 intel_get_image_dims(image
, &width
, &height
, &depth
);
1024 if (mt
->target
== GL_TEXTURE_CUBE_MAP
)
1027 int level_depth
= mt
->level
[level
].depth
;
1028 if (mt
->num_samples
> 1) {
1029 switch (mt
->msaa_layout
) {
1030 case INTEL_MSAA_LAYOUT_NONE
:
1031 case INTEL_MSAA_LAYOUT_IMS
:
1033 case INTEL_MSAA_LAYOUT_UMS
:
1034 case INTEL_MSAA_LAYOUT_CMS
:
1035 level_depth
/= mt
->num_samples
;
1040 /* Test image dimensions against the base level image adjusted for
1041 * minification. This will also catch images not present in the
1042 * tree, changed targets, etc.
1044 if (width
!= minify(mt
->logical_width0
, level
- mt
->first_level
) ||
1045 height
!= minify(mt
->logical_height0
, level
- mt
->first_level
) ||
1046 depth
!= level_depth
) {
1050 if (image
->NumSamples
!= mt
->num_samples
)
1058 intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
1060 GLuint x
, GLuint y
, GLuint d
)
1062 mt
->level
[level
].depth
= d
;
1063 mt
->level
[level
].level_x
= x
;
1064 mt
->level
[level
].level_y
= y
;
1066 DBG("%s level %d, depth %d, offset %d,%d\n", __func__
,
1069 assert(mt
->level
[level
].slice
== NULL
);
1071 mt
->level
[level
].slice
= calloc(d
, sizeof(*mt
->level
[0].slice
));
1072 mt
->level
[level
].slice
[0].x_offset
= mt
->level
[level
].level_x
;
1073 mt
->level
[level
].slice
[0].y_offset
= mt
->level
[level
].level_y
;
1078 intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
1079 GLuint level
, GLuint img
,
1082 if (img
== 0 && level
== 0)
1083 assert(x
== 0 && y
== 0);
1085 assert(img
< mt
->level
[level
].depth
);
1087 mt
->level
[level
].slice
[img
].x_offset
= mt
->level
[level
].level_x
+ x
;
1088 mt
->level
[level
].slice
[img
].y_offset
= mt
->level
[level
].level_y
+ y
;
1090 DBG("%s level %d img %d pos %d,%d\n",
1091 __func__
, level
, img
,
1092 mt
->level
[level
].slice
[img
].x_offset
,
1093 mt
->level
[level
].slice
[img
].y_offset
);
1097 intel_miptree_get_image_offset(const struct intel_mipmap_tree
*mt
,
1098 GLuint level
, GLuint slice
,
1099 GLuint
*x
, GLuint
*y
)
1101 assert(slice
< mt
->level
[level
].depth
);
1103 *x
= mt
->level
[level
].slice
[slice
].x_offset
;
1104 *y
= mt
->level
[level
].slice
[slice
].y_offset
;
1109 * This function computes the tile_w (in bytes) and tile_h (in rows) of
1110 * different tiling patterns. If the BO is untiled, tile_w is set to cpp
1111 * and tile_h is set to 1.
1114 intel_get_tile_dims(uint32_t tiling
, uint32_t cpp
,
1115 uint32_t *tile_w
, uint32_t *tile_h
)
1126 case I915_TILING_NONE
:
1131 unreachable("not reached");
1137 * This function computes masks that may be used to select the bits of the X
1138 * and Y coordinates that indicate the offset within a tile. If the BO is
1139 * untiled, the masks are set to 0.
1142 intel_get_tile_masks(uint32_t tiling
, uint32_t cpp
,
1143 uint32_t *mask_x
, uint32_t *mask_y
)
1145 uint32_t tile_w_bytes
, tile_h
;
1147 intel_get_tile_dims(tiling
, cpp
, &tile_w_bytes
, &tile_h
);
1149 *mask_x
= tile_w_bytes
/ cpp
- 1;
1150 *mask_y
= tile_h
- 1;
1154 * Compute the offset (in bytes) from the start of the BO to the given x
1155 * and y coordinate. For tiled BOs, caller must ensure that x and y are
1156 * multiples of the tile size.
1159 intel_miptree_get_aligned_offset(const struct intel_mipmap_tree
*mt
,
1160 uint32_t x
, uint32_t y
)
1163 uint32_t pitch
= mt
->pitch
;
1164 uint32_t tiling
= mt
->tiling
;
1168 unreachable("not reached");
1169 case I915_TILING_NONE
:
1170 return y
* pitch
+ x
* cpp
;
1172 assert((x
% (512 / cpp
)) == 0);
1173 assert((y
% 8) == 0);
1174 return y
* pitch
+ x
/ (512 / cpp
) * 4096;
1176 assert((x
% (128 / cpp
)) == 0);
1177 assert((y
% 32) == 0);
1178 return y
* pitch
+ x
/ (128 / cpp
) * 4096;
1183 * Rendering with tiled buffers requires that the base address of the buffer
1184 * be aligned to a page boundary. For renderbuffers, and sometimes with
1185 * textures, we may want the surface to point at a texture image level that
1186 * isn't at a page boundary.
1188 * This function returns an appropriately-aligned base offset
1189 * according to the tiling restrictions, plus any required x/y offset
1193 intel_miptree_get_tile_offsets(const struct intel_mipmap_tree
*mt
,
1194 GLuint level
, GLuint slice
,
1199 uint32_t mask_x
, mask_y
;
1201 intel_get_tile_masks(mt
->tiling
, mt
->cpp
, &mask_x
, &mask_y
);
1202 intel_miptree_get_image_offset(mt
, level
, slice
, &x
, &y
);
1204 *tile_x
= x
& mask_x
;
1205 *tile_y
= y
& mask_y
;
1207 return intel_miptree_get_aligned_offset(mt
, x
& ~mask_x
, y
& ~mask_y
);
1211 intel_miptree_copy_slice_sw(struct brw_context
*brw
,
1212 struct intel_mipmap_tree
*dst_mt
,
1213 struct intel_mipmap_tree
*src_mt
,
1220 ptrdiff_t src_stride
, dst_stride
;
1221 int cpp
= dst_mt
->cpp
;
1223 intel_miptree_map(brw
, src_mt
,
1227 GL_MAP_READ_BIT
| BRW_MAP_DIRECT_BIT
,
1230 intel_miptree_map(brw
, dst_mt
,
1234 GL_MAP_WRITE_BIT
| GL_MAP_INVALIDATE_RANGE_BIT
|
1238 DBG("sw blit %s mt %p %p/%"PRIdPTR
" -> %s mt %p %p/%"PRIdPTR
" (%dx%d)\n",
1239 _mesa_get_format_name(src_mt
->format
),
1240 src_mt
, src
, src_stride
,
1241 _mesa_get_format_name(dst_mt
->format
),
1242 dst_mt
, dst
, dst_stride
,
1245 int row_size
= cpp
* width
;
1246 if (src_stride
== row_size
&&
1247 dst_stride
== row_size
) {
1248 memcpy(dst
, src
, row_size
* height
);
1250 for (int i
= 0; i
< height
; i
++) {
1251 memcpy(dst
, src
, row_size
);
1257 intel_miptree_unmap(brw
, dst_mt
, level
, slice
);
1258 intel_miptree_unmap(brw
, src_mt
, level
, slice
);
1260 /* Don't forget to copy the stencil data over, too. We could have skipped
1261 * passing BRW_MAP_DIRECT_BIT, but that would have meant intel_miptree_map
1262 * shuffling the two data sources in/out of temporary storage instead of
1263 * the direct mapping we get this way.
1265 if (dst_mt
->stencil_mt
) {
1266 assert(src_mt
->stencil_mt
);
1267 intel_miptree_copy_slice_sw(brw
, dst_mt
->stencil_mt
, src_mt
->stencil_mt
,
1268 level
, slice
, width
, height
);
1273 intel_miptree_copy_slice(struct brw_context
*brw
,
1274 struct intel_mipmap_tree
*dst_mt
,
1275 struct intel_mipmap_tree
*src_mt
,
1281 mesa_format format
= src_mt
->format
;
1282 uint32_t width
= minify(src_mt
->physical_width0
, level
- src_mt
->first_level
);
1283 uint32_t height
= minify(src_mt
->physical_height0
, level
- src_mt
->first_level
);
1291 assert(depth
< src_mt
->level
[level
].depth
);
1292 assert(src_mt
->format
== dst_mt
->format
);
1294 if (dst_mt
->compressed
) {
1296 _mesa_get_format_block_size(dst_mt
->format
, &i
, &j
);
1297 height
= ALIGN_NPOT(height
, j
) / j
;
1298 width
= ALIGN_NPOT(width
, i
) / i
;
1301 /* If it's a packed depth/stencil buffer with separate stencil, the blit
1302 * below won't apply since we can't do the depth's Y tiling or the
1303 * stencil's W tiling in the blitter.
1305 if (src_mt
->stencil_mt
) {
1306 intel_miptree_copy_slice_sw(brw
,
1313 uint32_t dst_x
, dst_y
, src_x
, src_y
;
1314 intel_miptree_get_image_offset(dst_mt
, level
, slice
, &dst_x
, &dst_y
);
1315 intel_miptree_get_image_offset(src_mt
, level
, slice
, &src_x
, &src_y
);
1317 DBG("validate blit mt %s %p %d,%d/%d -> mt %s %p %d,%d/%d (%dx%d)\n",
1318 _mesa_get_format_name(src_mt
->format
),
1319 src_mt
, src_x
, src_y
, src_mt
->pitch
,
1320 _mesa_get_format_name(dst_mt
->format
),
1321 dst_mt
, dst_x
, dst_y
, dst_mt
->pitch
,
1324 if (!intel_miptree_blit(brw
,
1325 src_mt
, level
, slice
, 0, 0, false,
1326 dst_mt
, level
, slice
, 0, 0, false,
1327 width
, height
, GL_COPY
)) {
1328 perf_debug("miptree validate blit for %s failed\n",
1329 _mesa_get_format_name(format
));
1331 intel_miptree_copy_slice_sw(brw
, dst_mt
, src_mt
, level
, slice
,
1337 * Copies the image's current data to the given miptree, and associates that
1338 * miptree with the image.
1340 * If \c invalidate is true, then the actual image data does not need to be
1341 * copied, but the image still needs to be associated to the new miptree (this
1342 * is set to true if we're about to clear the image).
1345 intel_miptree_copy_teximage(struct brw_context
*brw
,
1346 struct intel_texture_image
*intelImage
,
1347 struct intel_mipmap_tree
*dst_mt
,
1350 struct intel_mipmap_tree
*src_mt
= intelImage
->mt
;
1351 struct intel_texture_object
*intel_obj
=
1352 intel_texture_object(intelImage
->base
.Base
.TexObject
);
1353 int level
= intelImage
->base
.Base
.Level
;
1354 int face
= intelImage
->base
.Base
.Face
;
1357 if (intel_obj
->base
.Target
== GL_TEXTURE_1D_ARRAY
)
1358 depth
= intelImage
->base
.Base
.Height
;
1360 depth
= intelImage
->base
.Base
.Depth
;
1363 for (int slice
= 0; slice
< depth
; slice
++) {
1364 intel_miptree_copy_slice(brw
, dst_mt
, src_mt
, level
, face
, slice
);
1368 intel_miptree_reference(&intelImage
->mt
, dst_mt
);
1369 intel_obj
->needs_validate
= true;
1373 intel_miptree_init_mcs(struct brw_context
*brw
,
1374 struct intel_mipmap_tree
*mt
,
1377 assert(mt
->mcs_buf
!= NULL
);
1379 /* From the Ivy Bridge PRM, Vol 2 Part 1 p326:
1381 * When MCS buffer is enabled and bound to MSRT, it is required that it
1382 * is cleared prior to any rendering.
1384 * Since we don't use the MCS buffer for any purpose other than rendering,
1385 * it makes sense to just clear it immediately upon allocation.
1387 * Note: the clear value for MCS buffers is all 1's, so we memset to 0xff.
1389 void *map
= brw_bo_map(brw
, mt
->mcs_buf
->bo
, MAP_WRITE
);
1390 if (unlikely(map
== NULL
)) {
1391 fprintf(stderr
, "Failed to map mcs buffer into GTT\n");
1392 brw_bo_unreference(mt
->mcs_buf
->bo
);
1397 memset(data
, init_value
, mt
->mcs_buf
->size
);
1398 brw_bo_unmap(mt
->mcs_buf
->bo
);
1401 static struct intel_miptree_aux_buffer
*
1402 intel_mcs_miptree_buf_create(struct brw_context
*brw
,
1403 struct intel_mipmap_tree
*mt
,
1406 unsigned mcs_height
,
1407 uint32_t layout_flags
)
1409 struct intel_miptree_aux_buffer
*buf
= calloc(sizeof(*buf
), 1);
1410 struct intel_mipmap_tree
*temp_mt
;
1415 /* From the Ivy Bridge PRM, Vol4 Part1 p76, "MCS Base Address":
1417 * "The MCS surface must be stored as Tile Y."
1419 layout_flags
|= MIPTREE_LAYOUT_TILING_Y
;
1420 temp_mt
= miptree_create(brw
,
1428 0 /* num_samples */,
1435 buf
->bo
= temp_mt
->bo
;
1436 buf
->offset
= temp_mt
->offset
;
1437 buf
->size
= temp_mt
->total_height
* temp_mt
->pitch
;
1438 buf
->pitch
= temp_mt
->pitch
;
1439 buf
->qpitch
= temp_mt
->qpitch
;
1441 /* Just hang on to the BO which backs the AUX buffer; the rest of the miptree
1442 * structure should go away. We use miptree create simply as a means to make
1443 * sure all the constraints for the buffer are satisfied.
1445 brw_bo_reference(temp_mt
->bo
);
1446 intel_miptree_release(&temp_mt
);
1452 intel_miptree_alloc_mcs(struct brw_context
*brw
,
1453 struct intel_mipmap_tree
*mt
,
1456 assert(brw
->gen
>= 7); /* MCS only used on Gen7+ */
1457 assert(mt
->mcs_buf
== NULL
);
1458 assert((mt
->aux_disable
& INTEL_AUX_DISABLE_MCS
) == 0);
1460 /* Choose the correct format for the MCS buffer. All that really matters
1461 * is that we allocate the right buffer size, since we'll always be
1462 * accessing this miptree using MCS-specific hardware mechanisms, which
1463 * infer the correct format based on num_samples.
1466 switch (num_samples
) {
1469 /* 8 bits/pixel are required for MCS data when using 4x MSAA (2 bits for
1472 format
= MESA_FORMAT_R_UNORM8
;
1475 /* 32 bits/pixel are required for MCS data when using 8x MSAA (3 bits
1476 * for each sample, plus 8 padding bits).
1478 format
= MESA_FORMAT_R_UINT32
;
1481 /* 64 bits/pixel are required for MCS data when using 16x MSAA (4 bits
1484 format
= MESA_FORMAT_RG_UINT32
;
1487 unreachable("Unrecognized sample count in intel_miptree_alloc_mcs");
1491 intel_mcs_miptree_buf_create(brw
, mt
,
1494 mt
->logical_height0
,
1495 MIPTREE_LAYOUT_ACCELERATED_UPLOAD
);
1499 intel_miptree_init_mcs(brw
, mt
, 0xFF);
1501 /* Multisampled miptrees are only supported for single level. */
1502 assert(mt
->first_level
== 0);
1503 intel_miptree_set_fast_clear_state(brw
, mt
, mt
->first_level
, 0,
1505 INTEL_FAST_CLEAR_STATE_CLEAR
);
1512 intel_miptree_alloc_non_msrt_mcs(struct brw_context
*brw
,
1513 struct intel_mipmap_tree
*mt
,
1514 bool is_lossless_compressed
)
1516 assert(mt
->mcs_buf
== NULL
);
1517 assert(!(mt
->aux_disable
& (INTEL_AUX_DISABLE_MCS
| INTEL_AUX_DISABLE_CCS
)));
1519 struct isl_surf temp_main_surf
;
1520 struct isl_surf temp_ccs_surf
;
1522 /* Create first an ISL presentation for the main color surface and let ISL
1523 * calculate equivalent CCS surface against it.
1525 intel_miptree_get_isl_surf(brw
, mt
, &temp_main_surf
);
1526 if (!isl_surf_get_ccs_surf(&brw
->isl_dev
, &temp_main_surf
, &temp_ccs_surf
))
1529 assert(temp_ccs_surf
.size
&&
1530 (temp_ccs_surf
.size
% temp_ccs_surf
.row_pitch
== 0));
1532 struct intel_miptree_aux_buffer
*buf
= calloc(sizeof(*buf
), 1);
1536 buf
->size
= temp_ccs_surf
.size
;
1537 buf
->pitch
= temp_ccs_surf
.row_pitch
;
1538 buf
->qpitch
= isl_surf_get_array_pitch_sa_rows(&temp_ccs_surf
);
1540 /* In case of compression mcs buffer needs to be initialised requiring the
1541 * buffer to be immediately mapped to cpu space for writing. Therefore do
1542 * not use the gpu access flag which can cause an unnecessary delay if the
1543 * backing pages happened to be just used by the GPU.
1545 const uint32_t alloc_flags
=
1546 is_lossless_compressed
? 0 : BO_ALLOC_FOR_RENDER
;
1548 /* ISL has stricter set of alignment rules then the drm allocator.
1549 * Therefore one can pass the ISL dimensions in terms of bytes instead of
1550 * trying to recalculate based on different format block sizes.
1552 buf
->bo
= brw_bo_alloc_tiled(brw
->bufmgr
, "ccs-miptree",
1553 buf
->pitch
, buf
->size
/ buf
->pitch
,
1554 1, I915_TILING_Y
, &buf
->pitch
, alloc_flags
);
1562 /* From Gen9 onwards single-sampled (non-msrt) auxiliary buffers are
1563 * used for lossless compression which requires similar initialisation
1564 * as multi-sample compression.
1566 if (is_lossless_compressed
) {
1567 /* Hardware sets the auxiliary buffer to all zeroes when it does full
1568 * resolve. Initialize it accordingly in case the first renderer is
1569 * cpu (or other none compression aware party).
1571 * This is also explicitly stated in the spec (MCS Buffer for Render
1573 * "If Software wants to enable Color Compression without Fast clear,
1574 * Software needs to initialize MCS with zeros."
1576 intel_miptree_init_mcs(brw
, mt
, 0);
1577 mt
->msaa_layout
= INTEL_MSAA_LAYOUT_CMS
;
1584 * Helper for intel_miptree_alloc_hiz() that sets
1585 * \c mt->level[level].has_hiz. Return true if and only if
1586 * \c has_hiz was set.
1589 intel_miptree_level_enable_hiz(struct brw_context
*brw
,
1590 struct intel_mipmap_tree
*mt
,
1593 assert(mt
->hiz_buf
);
1595 if (brw
->gen
>= 8 || brw
->is_haswell
) {
1596 uint32_t width
= minify(mt
->physical_width0
, level
);
1597 uint32_t height
= minify(mt
->physical_height0
, level
);
1599 /* Disable HiZ for LOD > 0 unless the width is 8 aligned
1600 * and the height is 4 aligned. This allows our HiZ support
1601 * to fulfill Haswell restrictions for HiZ ops. For LOD == 0,
1602 * we can grow the width & height to allow the HiZ op to
1603 * force the proper size alignments.
1605 if (level
> 0 && ((width
& 7) || (height
& 3))) {
1606 DBG("mt %p level %d: HiZ DISABLED\n", mt
, level
);
1611 DBG("mt %p level %d: HiZ enabled\n", mt
, level
);
1612 mt
->level
[level
].has_hiz
= true;
1618 * Helper for intel_miptree_alloc_hiz() that determines the required hiz
1619 * buffer dimensions and allocates a bo for the hiz buffer.
1621 static struct intel_miptree_hiz_buffer
*
1622 intel_gen7_hiz_buf_create(struct brw_context
*brw
,
1623 struct intel_mipmap_tree
*mt
)
1625 unsigned z_width
= mt
->logical_width0
;
1626 unsigned z_height
= mt
->logical_height0
;
1627 const unsigned z_depth
= MAX2(mt
->logical_depth0
, 1);
1628 unsigned hz_width
, hz_height
;
1629 struct intel_miptree_hiz_buffer
*buf
= calloc(sizeof(*buf
), 1);
1634 /* Gen7 PRM Volume 2, Part 1, 11.5.3 "Hierarchical Depth Buffer" documents
1635 * adjustments required for Z_Height and Z_Width based on multisampling.
1637 switch (mt
->num_samples
) {
1651 unreachable("unsupported sample count");
1654 const unsigned vertical_align
= 8; /* 'j' in the docs */
1655 const unsigned H0
= z_height
;
1656 const unsigned h0
= ALIGN(H0
, vertical_align
);
1657 const unsigned h1
= ALIGN(minify(H0
, 1), vertical_align
);
1658 const unsigned Z0
= z_depth
;
1660 /* HZ_Width (bytes) = ceiling(Z_Width / 16) * 16 */
1661 hz_width
= ALIGN(z_width
, 16);
1663 if (mt
->target
== GL_TEXTURE_3D
) {
1667 for (unsigned level
= mt
->first_level
; level
<= mt
->last_level
; ++level
) {
1668 unsigned h_i
= ALIGN(H_i
, vertical_align
);
1669 /* sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i))) */
1670 hz_height
+= h_i
* Z_i
;
1671 H_i
= minify(H_i
, 1);
1672 Z_i
= minify(Z_i
, 1);
1675 * (1/2) * sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i)))
1677 hz_height
= DIV_ROUND_UP(hz_height
, 2);
1679 const unsigned hz_qpitch
= h0
+ h1
+ (12 * vertical_align
);
1680 /* HZ_Height (rows) = Ceiling ( ( Q_pitch * Z_depth/2) /8 ) * 8 */
1681 hz_height
= DIV_ROUND_UP(hz_qpitch
* Z0
, 2 * 8) * 8;
1684 buf
->aux_base
.bo
= brw_bo_alloc_tiled(brw
->bufmgr
, "hiz",
1685 hz_width
, hz_height
, 1,
1686 I915_TILING_Y
, &buf
->aux_base
.pitch
,
1687 BO_ALLOC_FOR_RENDER
);
1688 if (!buf
->aux_base
.bo
) {
1693 buf
->aux_base
.size
= hz_width
* hz_height
;
1700 * Helper for intel_miptree_alloc_hiz() that determines the required hiz
1701 * buffer dimensions and allocates a bo for the hiz buffer.
1703 static struct intel_miptree_hiz_buffer
*
1704 intel_gen8_hiz_buf_create(struct brw_context
*brw
,
1705 struct intel_mipmap_tree
*mt
)
1707 unsigned z_width
= mt
->logical_width0
;
1708 unsigned z_height
= mt
->logical_height0
;
1709 const unsigned z_depth
= MAX2(mt
->logical_depth0
, 1);
1710 unsigned hz_width
, hz_height
;
1711 struct intel_miptree_hiz_buffer
*buf
= calloc(sizeof(*buf
), 1);
1716 /* Gen7 PRM Volume 2, Part 1, 11.5.3 "Hierarchical Depth Buffer" documents
1717 * adjustments required for Z_Height and Z_Width based on multisampling.
1720 switch (mt
->num_samples
) {
1734 unreachable("unsupported sample count");
1738 const unsigned vertical_align
= 8; /* 'j' in the docs */
1739 const unsigned H0
= z_height
;
1740 const unsigned h0
= ALIGN(H0
, vertical_align
);
1741 const unsigned h1
= ALIGN(minify(H0
, 1), vertical_align
);
1742 const unsigned Z0
= z_depth
;
1744 /* HZ_Width (bytes) = ceiling(Z_Width / 16) * 16 */
1745 hz_width
= ALIGN(z_width
, 16);
1749 unsigned sum_h_i
= 0;
1750 unsigned hz_height_3d_sum
= 0;
1751 for (unsigned level
= mt
->first_level
; level
<= mt
->last_level
; ++level
) {
1752 unsigned i
= level
- mt
->first_level
;
1753 unsigned h_i
= ALIGN(H_i
, vertical_align
);
1754 /* sum(i=2 to m; h_i) */
1758 /* sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i))) */
1759 hz_height_3d_sum
+= h_i
* Z_i
;
1760 H_i
= minify(H_i
, 1);
1761 Z_i
= minify(Z_i
, 1);
1763 /* HZ_QPitch = h0 + max(h1, sum(i=2 to m; h_i)) */
1764 buf
->aux_base
.qpitch
= h0
+ MAX2(h1
, sum_h_i
);
1766 if (mt
->target
== GL_TEXTURE_3D
) {
1767 /* (1/2) * sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i))) */
1768 hz_height
= DIV_ROUND_UP(hz_height_3d_sum
, 2);
1770 /* HZ_Height (rows) = ceiling( (HZ_QPitch/2)/8) *8 * Z_Depth */
1771 hz_height
= DIV_ROUND_UP(buf
->aux_base
.qpitch
, 2 * 8) * 8 * Z0
;
1774 buf
->aux_base
.bo
= brw_bo_alloc_tiled(brw
->bufmgr
, "hiz",
1775 hz_width
, hz_height
, 1,
1776 I915_TILING_Y
, &buf
->aux_base
.pitch
,
1777 BO_ALLOC_FOR_RENDER
);
1778 if (!buf
->aux_base
.bo
) {
1783 buf
->aux_base
.size
= hz_width
* hz_height
;
1789 static struct intel_miptree_hiz_buffer
*
1790 intel_hiz_miptree_buf_create(struct brw_context
*brw
,
1791 struct intel_mipmap_tree
*mt
)
1793 struct intel_miptree_hiz_buffer
*buf
= calloc(sizeof(*buf
), 1);
1794 uint32_t layout_flags
= MIPTREE_LAYOUT_ACCELERATED_UPLOAD
;
1797 layout_flags
|= MIPTREE_LAYOUT_GEN6_HIZ_STENCIL
;
1802 layout_flags
|= MIPTREE_LAYOUT_TILING_ANY
;
1803 buf
->mt
= intel_miptree_create(brw
,
1809 mt
->logical_height0
,
1818 buf
->aux_base
.bo
= buf
->mt
->bo
;
1819 buf
->aux_base
.size
= buf
->mt
->total_height
* buf
->mt
->pitch
;
1820 buf
->aux_base
.pitch
= buf
->mt
->pitch
;
1821 buf
->aux_base
.qpitch
= buf
->mt
->qpitch
* 2;
1827 intel_miptree_wants_hiz_buffer(struct brw_context
*brw
,
1828 struct intel_mipmap_tree
*mt
)
1833 if (mt
->hiz_buf
!= NULL
)
1836 if (mt
->aux_disable
& INTEL_AUX_DISABLE_HIZ
)
1839 switch (mt
->format
) {
1840 case MESA_FORMAT_Z_FLOAT32
:
1841 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT
:
1842 case MESA_FORMAT_Z24_UNORM_X8_UINT
:
1843 case MESA_FORMAT_Z24_UNORM_S8_UINT
:
1844 case MESA_FORMAT_Z_UNORM16
:
1852 intel_miptree_alloc_hiz(struct brw_context
*brw
,
1853 struct intel_mipmap_tree
*mt
)
1855 assert(mt
->hiz_buf
== NULL
);
1856 assert((mt
->aux_disable
& INTEL_AUX_DISABLE_HIZ
) == 0);
1858 if (brw
->gen
== 7) {
1859 mt
->hiz_buf
= intel_gen7_hiz_buf_create(brw
, mt
);
1860 } else if (brw
->gen
>= 8) {
1861 mt
->hiz_buf
= intel_gen8_hiz_buf_create(brw
, mt
);
1863 mt
->hiz_buf
= intel_hiz_miptree_buf_create(brw
, mt
);
1869 /* Mark that all slices need a HiZ resolve. */
1870 for (unsigned level
= mt
->first_level
; level
<= mt
->last_level
; ++level
) {
1871 if (!intel_miptree_level_enable_hiz(brw
, mt
, level
))
1874 for (unsigned layer
= 0; layer
< mt
->level
[level
].depth
; ++layer
) {
1875 struct intel_resolve_map
*m
= malloc(sizeof(struct intel_resolve_map
));
1876 exec_node_init(&m
->link
);
1879 m
->need
= BLORP_HIZ_OP_HIZ_RESOLVE
;
1881 exec_list_push_tail(&mt
->hiz_map
, &m
->link
);
1889 * Can the miptree sample using the hiz buffer?
1892 intel_miptree_sample_with_hiz(struct brw_context
*brw
,
1893 struct intel_mipmap_tree
*mt
)
1895 /* It's unclear how well supported sampling from the hiz buffer is on GEN8,
1896 * so keep things conservative for now and never enable it unless we're SKL+.
1906 /* It seems the hardware won't fallback to the depth buffer if some of the
1907 * mipmap levels aren't available in the HiZ buffer. So we need all levels
1908 * of the texture to be HiZ enabled.
1910 for (unsigned level
= mt
->first_level
; level
<= mt
->last_level
; ++level
) {
1911 if (!intel_miptree_level_has_hiz(mt
, level
))
1915 /* If compressed multisampling is enabled, then we use it for the auxiliary
1918 * From the BDW PRM (Volume 2d: Command Reference: Structures
1919 * RENDER_SURFACE_STATE.AuxiliarySurfaceMode):
1921 * "If this field is set to AUX_HIZ, Number of Multisamples must be
1922 * MULTISAMPLECOUNT_1, and Surface Type cannot be SURFTYPE_3D.
1924 * There is no such blurb for 1D textures, but there is sufficient evidence
1925 * that this is broken on SKL+.
1927 return (mt
->num_samples
<= 1 &&
1928 mt
->target
!= GL_TEXTURE_3D
&&
1929 mt
->target
!= GL_TEXTURE_1D
/* gen9+ restriction */);
1933 * Does the miptree slice have hiz enabled?
1936 intel_miptree_level_has_hiz(struct intel_mipmap_tree
*mt
, uint32_t level
)
1938 intel_miptree_check_level_layer(mt
, level
, 0);
1939 return mt
->level
[level
].has_hiz
;
1943 intel_miptree_depth_hiz_resolve(struct brw_context
*brw
,
1944 struct intel_mipmap_tree
*mt
,
1945 uint32_t start_level
, uint32_t num_levels
,
1946 uint32_t start_layer
, uint32_t num_layers
,
1947 enum blorp_hiz_op need
)
1949 bool did_resolve
= false;
1951 foreach_list_typed_safe(struct intel_resolve_map
, map
, link
, &mt
->hiz_map
) {
1952 if (map
->level
< start_level
||
1953 map
->level
>= (start_level
+ num_levels
) ||
1954 map
->layer
< start_layer
||
1955 map
->layer
>= (start_layer
+ num_layers
))
1958 if (map
->need
!= need
)
1961 intel_hiz_exec(brw
, mt
, map
->level
, map
->layer
, 1, need
);
1962 intel_resolve_map_remove(map
);
1970 intel_miptree_all_slices_resolve_depth(struct brw_context
*brw
,
1971 struct intel_mipmap_tree
*mt
)
1973 return intel_miptree_depth_hiz_resolve(brw
, mt
,
1974 0, UINT32_MAX
, 0, UINT32_MAX
,
1975 BLORP_HIZ_OP_DEPTH_RESOLVE
);
1978 enum intel_fast_clear_state
1979 intel_miptree_get_fast_clear_state(const struct intel_mipmap_tree
*mt
,
1980 unsigned level
, unsigned layer
)
1982 intel_miptree_check_level_layer(mt
, level
, layer
);
1984 const struct intel_resolve_map
*item
=
1985 intel_resolve_map_const_get(&mt
->color_resolve_map
, level
, layer
);
1988 return INTEL_FAST_CLEAR_STATE_RESOLVED
;
1990 return item
->fast_clear_state
;
1994 intel_miptree_check_color_resolve(const struct brw_context
*brw
,
1995 const struct intel_mipmap_tree
*mt
,
1996 unsigned level
, unsigned layer
)
1999 if ((mt
->aux_disable
& INTEL_AUX_DISABLE_CCS
) || !mt
->mcs_buf
)
2002 /* Fast color clear is supported for mipmapped surfaces only on Gen8+. */
2003 assert(brw
->gen
>= 8 ||
2004 (level
== 0 && mt
->first_level
== 0 && mt
->last_level
== 0));
2006 /* Compression of arrayed msaa surfaces is supported. */
2007 if (mt
->num_samples
> 1)
2010 /* Fast color clear is supported for non-msaa arrays only on Gen8+. */
2011 assert(brw
->gen
>= 8 || (layer
== 0 && mt
->logical_depth0
== 1));
2018 intel_miptree_set_fast_clear_state(const struct brw_context
*brw
,
2019 struct intel_mipmap_tree
*mt
,
2021 unsigned first_layer
,
2022 unsigned num_layers
,
2023 enum intel_fast_clear_state new_state
)
2025 /* Setting the state to resolved means removing the item from the list
2028 assert(new_state
!= INTEL_FAST_CLEAR_STATE_RESOLVED
);
2030 intel_miptree_check_color_resolve(brw
, mt
, level
, first_layer
);
2032 assert(first_layer
+ num_layers
<= mt
->physical_depth0
);
2034 for (unsigned i
= 0; i
< num_layers
; i
++)
2035 intel_resolve_map_set(&mt
->color_resolve_map
, level
,
2036 first_layer
+ i
, new_state
);
2040 intel_miptree_has_color_unresolved(const struct intel_mipmap_tree
*mt
,
2041 unsigned start_level
, unsigned num_levels
,
2042 unsigned start_layer
, unsigned num_layers
)
2044 return intel_resolve_map_find_any(&mt
->color_resolve_map
,
2045 start_level
, num_levels
,
2046 start_layer
, num_layers
) != NULL
;
2050 intel_miptree_used_for_rendering(const struct brw_context
*brw
,
2051 struct intel_mipmap_tree
*mt
, unsigned level
,
2052 unsigned start_layer
, unsigned num_layers
)
2054 const bool is_lossless_compressed
=
2055 intel_miptree_is_lossless_compressed(brw
, mt
);
2057 for (unsigned i
= 0; i
< num_layers
; ++i
) {
2058 const enum intel_fast_clear_state fast_clear_state
=
2059 intel_miptree_get_fast_clear_state(mt
, level
, start_layer
+ i
);
2061 /* If the buffer was previously in fast clear state, change it to
2062 * unresolved state, since it won't be guaranteed to be clear after
2065 if (is_lossless_compressed
||
2066 fast_clear_state
== INTEL_FAST_CLEAR_STATE_CLEAR
) {
2067 intel_miptree_set_fast_clear_state(
2068 brw
, mt
, level
, start_layer
+ i
, 1,
2069 INTEL_FAST_CLEAR_STATE_UNRESOLVED
);
2075 intel_miptree_needs_color_resolve(const struct brw_context
*brw
,
2076 const struct intel_mipmap_tree
*mt
,
2079 if (mt
->aux_disable
& INTEL_AUX_DISABLE_CCS
)
2082 const bool is_lossless_compressed
=
2083 intel_miptree_is_lossless_compressed(brw
, mt
);
2085 /* From gen9 onwards there is new compression scheme for single sampled
2086 * surfaces called "lossless compressed". These don't need to be always
2089 if ((flags
& INTEL_MIPTREE_IGNORE_CCS_E
) && is_lossless_compressed
)
2092 /* Fast color clear resolves only make sense for non-MSAA buffers. */
2093 if (mt
->msaa_layout
!= INTEL_MSAA_LAYOUT_NONE
&& !is_lossless_compressed
)
2100 intel_miptree_resolve_color(struct brw_context
*brw
,
2101 struct intel_mipmap_tree
*mt
,
2102 uint32_t start_level
, uint32_t num_levels
,
2103 uint32_t start_layer
, uint32_t num_layers
,
2106 intel_miptree_check_color_resolve(brw
, mt
, start_level
, start_layer
);
2108 if (!intel_miptree_needs_color_resolve(brw
, mt
, flags
))
2111 enum blorp_fast_clear_op resolve_op
;
2112 if (brw
->gen
>= 9) {
2113 if (intel_miptree_is_lossless_compressed(brw
, mt
)) {
2114 resolve_op
= BLORP_FAST_CLEAR_OP_RESOLVE_FULL
;
2116 resolve_op
= BLORP_FAST_CLEAR_OP_RESOLVE_PARTIAL
;
2119 /* Broadwell and earlier do not have a partial resolve */
2120 assert(!intel_miptree_is_lossless_compressed(brw
, mt
));
2121 resolve_op
= BLORP_FAST_CLEAR_OP_RESOLVE_FULL
;
2124 bool resolved
= false;
2125 foreach_list_typed_safe(struct intel_resolve_map
, map
, link
,
2126 &mt
->color_resolve_map
) {
2127 if (map
->level
< start_level
||
2128 map
->level
>= (start_level
+ num_levels
) ||
2129 map
->layer
< start_layer
||
2130 map
->layer
>= (start_layer
+ num_layers
))
2133 /* Arrayed and mip-mapped fast clear is only supported for gen8+. */
2134 assert(brw
->gen
>= 8 || (map
->level
== 0 && map
->layer
== 0));
2136 intel_miptree_check_level_layer(mt
, map
->level
, map
->layer
);
2138 assert(map
->fast_clear_state
!= INTEL_FAST_CLEAR_STATE_RESOLVED
);
2140 brw_blorp_resolve_color(brw
, mt
, map
->level
, map
->layer
, resolve_op
);
2141 intel_resolve_map_remove(map
);
2148 static inline uint32_t
2149 miptree_level_range_length(const struct intel_mipmap_tree
*mt
,
2150 uint32_t start_level
, uint32_t num_levels
)
2152 assert(start_level
>= mt
->first_level
);
2153 assert(start_level
<= mt
->last_level
);
2155 if (num_levels
== INTEL_REMAINING_LAYERS
)
2156 num_levels
= mt
->last_level
- start_level
+ 1;
2157 /* Check for overflow */
2158 assert(start_level
+ num_levels
>= start_level
);
2159 assert(start_level
+ num_levels
<= mt
->last_level
+ 1);
2164 static inline uint32_t
2165 miptree_layer_range_length(const struct intel_mipmap_tree
*mt
, uint32_t level
,
2166 uint32_t start_layer
, uint32_t num_layers
)
2168 assert(level
<= mt
->last_level
);
2169 uint32_t total_num_layers
= mt
->level
[level
].depth
;
2171 assert(start_layer
< total_num_layers
);
2172 if (num_layers
== INTEL_REMAINING_LAYERS
)
2173 num_layers
= total_num_layers
- start_layer
;
2174 /* Check for overflow */
2175 assert(start_layer
+ num_layers
>= start_layer
);
2176 assert(start_layer
+ num_layers
<= total_num_layers
);
2182 intel_miptree_prepare_access(struct brw_context
*brw
,
2183 struct intel_mipmap_tree
*mt
,
2184 uint32_t start_level
, uint32_t num_levels
,
2185 uint32_t start_layer
, uint32_t num_layers
,
2186 bool aux_supported
, bool fast_clear_supported
)
2188 num_levels
= miptree_level_range_length(mt
, start_level
, num_levels
);
2190 if (_mesa_is_format_color_format(mt
->format
)) {
2194 if (mt
->num_samples
> 1) {
2195 /* Nothing to do for MSAA */
2197 /* TODO: This is fairly terrible. We can do better. */
2198 if (!aux_supported
|| !fast_clear_supported
) {
2199 intel_miptree_resolve_color(brw
, mt
, start_level
, num_levels
,
2200 start_layer
, num_layers
, 0);
2203 } else if (mt
->format
== MESA_FORMAT_S_UINT8
) {
2204 /* Nothing to do for stencil */
2209 if (aux_supported
) {
2210 assert(fast_clear_supported
);
2211 intel_miptree_depth_hiz_resolve(brw
, mt
, start_level
, num_levels
,
2212 start_layer
, num_layers
,
2213 BLORP_HIZ_OP_HIZ_RESOLVE
);
2215 assert(!fast_clear_supported
);
2216 intel_miptree_depth_hiz_resolve(brw
, mt
, start_level
, num_levels
,
2217 start_layer
, num_layers
,
2218 BLORP_HIZ_OP_DEPTH_RESOLVE
);
2224 intel_miptree_finish_write(struct brw_context
*brw
,
2225 struct intel_mipmap_tree
*mt
, uint32_t level
,
2226 uint32_t start_layer
, uint32_t num_layers
,
2227 bool written_with_aux
)
2229 num_layers
= miptree_layer_range_length(mt
, level
, start_layer
, num_layers
);
2231 if (_mesa_is_format_color_format(mt
->format
)) {
2232 if (mt
->num_samples
> 1) {
2233 /* Nothing to do for MSAA */
2235 if (written_with_aux
) {
2236 intel_miptree_used_for_rendering(brw
, mt
, level
,
2237 start_layer
, num_layers
);
2240 } else if (mt
->format
== MESA_FORMAT_S_UINT8
) {
2241 /* Nothing to do for stencil */
2243 if (!intel_miptree_level_has_hiz(mt
, level
))
2246 if (written_with_aux
) {
2247 for (unsigned a
= 0; a
< num_layers
; a
++) {
2248 intel_miptree_check_level_layer(mt
, level
, start_layer
);
2249 intel_resolve_map_set(&mt
->hiz_map
, level
, start_layer
+ a
,
2250 BLORP_HIZ_OP_DEPTH_RESOLVE
);
2253 for (unsigned a
= 0; a
< num_layers
; a
++) {
2254 intel_miptree_check_level_layer(mt
, level
, start_layer
);
2255 intel_resolve_map_set(&mt
->hiz_map
, level
, start_layer
+ a
,
2256 BLORP_HIZ_OP_HIZ_RESOLVE
);
2263 intel_miptree_get_aux_state(const struct intel_mipmap_tree
*mt
,
2264 uint32_t level
, uint32_t layer
)
2266 if (_mesa_is_format_color_format(mt
->format
)) {
2267 assert(mt
->mcs_buf
!= NULL
);
2268 if (mt
->num_samples
> 1) {
2269 return ISL_AUX_STATE_COMPRESSED_CLEAR
;
2271 switch (intel_miptree_get_fast_clear_state(mt
, level
, layer
)) {
2272 case INTEL_FAST_CLEAR_STATE_RESOLVED
:
2273 return ISL_AUX_STATE_RESOLVED
;
2274 case INTEL_FAST_CLEAR_STATE_UNRESOLVED
:
2275 return ISL_AUX_STATE_COMPRESSED_CLEAR
;
2276 case INTEL_FAST_CLEAR_STATE_CLEAR
:
2277 return ISL_AUX_STATE_CLEAR
;
2279 unreachable("Invalid fast clear state");
2282 } else if (mt
->format
== MESA_FORMAT_S_UINT8
) {
2283 unreachable("Cannot get aux state for stencil");
2285 assert(mt
->hiz_buf
!= NULL
);
2286 const struct intel_resolve_map
*map
=
2287 intel_resolve_map_const_get(&mt
->hiz_map
, level
, layer
);
2289 return ISL_AUX_STATE_RESOLVED
;
2290 switch (map
->need
) {
2291 case BLORP_HIZ_OP_DEPTH_RESOLVE
:
2292 return ISL_AUX_STATE_COMPRESSED_CLEAR
;
2293 case BLORP_HIZ_OP_HIZ_RESOLVE
:
2294 return ISL_AUX_STATE_AUX_INVALID
;
2296 unreachable("Invalid hiz op");
2302 intel_miptree_set_aux_state(struct brw_context
*brw
,
2303 struct intel_mipmap_tree
*mt
, uint32_t level
,
2304 uint32_t start_layer
, uint32_t num_layers
,
2305 enum isl_aux_state aux_state
)
2307 num_layers
= miptree_layer_range_length(mt
, level
, start_layer
, num_layers
);
2309 /* Right now, this only applies to clears. */
2310 assert(aux_state
== ISL_AUX_STATE_CLEAR
);
2312 if (_mesa_is_format_color_format(mt
->format
)) {
2313 if (mt
->num_samples
> 1)
2314 assert(mt
->msaa_layout
== INTEL_MSAA_LAYOUT_CMS
);
2316 assert(level
== 0 && start_layer
== 0 && num_layers
== 1);
2317 intel_miptree_set_fast_clear_state(brw
, mt
, 0, 0, 1,
2318 INTEL_FAST_CLEAR_STATE_CLEAR
);
2319 } else if (mt
->format
== MESA_FORMAT_S_UINT8
) {
2320 assert(!"Cannot set aux state for stencil");
2322 for (unsigned a
= 0; a
< num_layers
; a
++) {
2323 intel_miptree_check_level_layer(mt
, level
, start_layer
);
2324 intel_resolve_map_set(&mt
->hiz_map
, level
, start_layer
+ a
,
2325 BLORP_HIZ_OP_DEPTH_RESOLVE
);
2330 /* On Gen9 color buffers may be compressed by the hardware (lossless
2331 * compression). There are, however, format restrictions and care needs to be
2332 * taken that the sampler engine is capable for re-interpreting a buffer with
2333 * format different the buffer was originally written with.
2335 * For example, SRGB formats are not compressible and the sampler engine isn't
2336 * capable of treating RGBA_UNORM as SRGB_ALPHA. In such a case the underlying
2337 * color buffer needs to be resolved so that the sampling surface can be
2338 * sampled as non-compressed (i.e., without the auxiliary MCS buffer being
2342 intel_texture_view_requires_resolve(struct brw_context
*brw
,
2343 struct intel_mipmap_tree
*mt
,
2347 !intel_miptree_is_lossless_compressed(brw
, mt
))
2350 const enum isl_format isl_format
= brw_isl_format_for_mesa_format(format
);
2352 if (isl_format_supports_ccs_e(&brw
->screen
->devinfo
, isl_format
))
2355 perf_debug("Incompatible sampling format (%s) for rbc (%s)\n",
2356 _mesa_get_format_name(format
),
2357 _mesa_get_format_name(mt
->format
));
2363 intel_miptree_prepare_texture_slices(struct brw_context
*brw
,
2364 struct intel_mipmap_tree
*mt
,
2365 mesa_format view_format
,
2366 uint32_t start_level
, uint32_t num_levels
,
2367 uint32_t start_layer
, uint32_t num_layers
,
2368 bool *aux_supported_out
)
2371 if (_mesa_is_format_color_format(mt
->format
)) {
2372 aux_supported
= intel_miptree_is_lossless_compressed(brw
, mt
) &&
2373 !intel_texture_view_requires_resolve(brw
, mt
, view_format
);
2374 } else if (mt
->format
== MESA_FORMAT_S_UINT8
) {
2375 aux_supported
= false;
2377 aux_supported
= intel_miptree_sample_with_hiz(brw
, mt
);
2380 intel_miptree_prepare_access(brw
, mt
, start_level
, num_levels
,
2381 start_layer
, num_layers
,
2382 aux_supported
, aux_supported
);
2383 if (aux_supported_out
)
2384 *aux_supported_out
= aux_supported
;
2388 intel_miptree_prepare_texture(struct brw_context
*brw
,
2389 struct intel_mipmap_tree
*mt
,
2390 mesa_format view_format
,
2391 bool *aux_supported_out
)
2393 intel_miptree_prepare_texture_slices(brw
, mt
, view_format
,
2394 0, INTEL_REMAINING_LEVELS
,
2395 0, INTEL_REMAINING_LAYERS
,
2400 intel_miptree_prepare_image(struct brw_context
*brw
,
2401 struct intel_mipmap_tree
*mt
)
2403 /* The data port doesn't understand any compression */
2404 intel_miptree_prepare_access(brw
, mt
, 0, INTEL_REMAINING_LEVELS
,
2405 0, INTEL_REMAINING_LAYERS
, false, false);
2409 intel_miptree_prepare_fb_fetch(struct brw_context
*brw
,
2410 struct intel_mipmap_tree
*mt
, uint32_t level
,
2411 uint32_t start_layer
, uint32_t num_layers
)
2413 intel_miptree_prepare_texture_slices(brw
, mt
, mt
->format
, level
, 1,
2414 start_layer
, num_layers
, NULL
);
2418 intel_miptree_prepare_render(struct brw_context
*brw
,
2419 struct intel_mipmap_tree
*mt
, uint32_t level
,
2420 uint32_t start_layer
, uint32_t layer_count
,
2423 /* If FRAMEBUFFER_SRGB is used on Gen9+ then we need to resolve any of
2424 * the single-sampled color renderbuffers because the CCS buffer isn't
2425 * supported for SRGB formats. This only matters if FRAMEBUFFER_SRGB is
2426 * enabled because otherwise the surface state will be programmed with
2427 * the linear equivalent format anyway.
2429 if (brw
->gen
>= 9 && srgb_enabled
&& mt
->num_samples
<= 1 &&
2430 _mesa_get_srgb_format_linear(mt
->format
) != mt
->format
) {
2432 /* Lossless compression is not supported for SRGB formats, it
2433 * should be impossible to get here with such surfaces.
2435 assert(!intel_miptree_is_lossless_compressed(brw
, mt
));
2436 intel_miptree_prepare_access(brw
, mt
, level
, 1, start_layer
, layer_count
,
2440 /* For layered rendering non-compressed fast cleared buffers need to be
2441 * resolved. Surface state can carry only one fast color clear value
2442 * while each layer may have its own fast clear color value. For
2443 * compressed buffers color value is available in the color buffer.
2445 if (layer_count
> 1 &&
2446 !(mt
->aux_disable
& INTEL_AUX_DISABLE_CCS
) &&
2447 !intel_miptree_is_lossless_compressed(brw
, mt
)) {
2448 assert(brw
->gen
>= 8);
2450 intel_miptree_prepare_access(brw
, mt
, level
, 1, start_layer
, layer_count
,
2456 intel_miptree_finish_render(struct brw_context
*brw
,
2457 struct intel_mipmap_tree
*mt
, uint32_t level
,
2458 uint32_t start_layer
, uint32_t layer_count
)
2460 assert(_mesa_is_format_color_format(mt
->format
));
2461 intel_miptree_finish_write(brw
, mt
, level
, start_layer
, layer_count
,
2462 mt
->mcs_buf
!= NULL
);
2466 intel_miptree_prepare_depth(struct brw_context
*brw
,
2467 struct intel_mipmap_tree
*mt
, uint32_t level
,
2468 uint32_t start_layer
, uint32_t layer_count
)
2470 intel_miptree_prepare_access(brw
, mt
, level
, 1, start_layer
, layer_count
,
2471 mt
->hiz_buf
!= NULL
, mt
->hiz_buf
!= NULL
);
2475 intel_miptree_finish_depth(struct brw_context
*brw
,
2476 struct intel_mipmap_tree
*mt
, uint32_t level
,
2477 uint32_t start_layer
, uint32_t layer_count
,
2480 if (depth_written
) {
2481 intel_miptree_finish_write(brw
, mt
, level
, start_layer
, layer_count
,
2482 mt
->hiz_buf
!= NULL
);
2487 * Make it possible to share the BO backing the given miptree with another
2488 * process or another miptree.
2490 * Fast color clears are unsafe with shared buffers, so we need to resolve and
2491 * then discard the MCS buffer, if present. We also set the no_ccs flag to
2492 * ensure that no MCS buffer gets allocated in the future.
2494 * HiZ is similarly unsafe with shared buffers.
2497 intel_miptree_make_shareable(struct brw_context
*brw
,
2498 struct intel_mipmap_tree
*mt
)
2500 /* MCS buffers are also used for multisample buffers, but we can't resolve
2501 * away a multisample MCS buffer because it's an integral part of how the
2502 * pixel data is stored. Fortunately this code path should never be
2503 * reached for multisample buffers.
2505 assert(mt
->msaa_layout
== INTEL_MSAA_LAYOUT_NONE
|| mt
->num_samples
<= 1);
2507 intel_miptree_prepare_access(brw
, mt
, 0, INTEL_REMAINING_LEVELS
,
2508 0, INTEL_REMAINING_LAYERS
, false, false);
2511 mt
->aux_disable
|= (INTEL_AUX_DISABLE_CCS
| INTEL_AUX_DISABLE_MCS
);
2512 brw_bo_unreference(mt
->mcs_buf
->bo
);
2516 /* Any pending MCS/CCS operations are no longer needed. Trying to
2517 * execute any will likely crash due to the missing aux buffer. So let's
2518 * delete all pending ops.
2520 exec_list_make_empty(&mt
->color_resolve_map
);
2524 mt
->aux_disable
|= INTEL_AUX_DISABLE_HIZ
;
2525 intel_miptree_hiz_buffer_free(mt
->hiz_buf
);
2528 for (uint32_t l
= mt
->first_level
; l
<= mt
->last_level
; ++l
) {
2529 mt
->level
[l
].has_hiz
= false;
2532 /* Any pending HiZ operations are no longer needed. Trying to execute
2533 * any will likely crash due to the missing aux buffer. So let's delete
2536 exec_list_make_empty(&mt
->hiz_map
);
2542 * \brief Get pointer offset into stencil buffer.
2544 * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
2545 * must decode the tile's layout in software.
2548 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
2550 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
2552 * Even though the returned offset is always positive, the return type is
2554 * commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
2555 * mesa: Fix return type of _mesa_get_format_bytes() (#37351)
2558 intel_offset_S8(uint32_t stride
, uint32_t x
, uint32_t y
, bool swizzled
)
2560 uint32_t tile_size
= 4096;
2561 uint32_t tile_width
= 64;
2562 uint32_t tile_height
= 64;
2563 uint32_t row_size
= 64 * stride
;
2565 uint32_t tile_x
= x
/ tile_width
;
2566 uint32_t tile_y
= y
/ tile_height
;
2568 /* The byte's address relative to the tile's base addres. */
2569 uint32_t byte_x
= x
% tile_width
;
2570 uint32_t byte_y
= y
% tile_height
;
2572 uintptr_t u
= tile_y
* row_size
2573 + tile_x
* tile_size
2574 + 512 * (byte_x
/ 8)
2576 + 32 * ((byte_y
/ 4) % 2)
2577 + 16 * ((byte_x
/ 4) % 2)
2578 + 8 * ((byte_y
/ 2) % 2)
2579 + 4 * ((byte_x
/ 2) % 2)
2584 /* adjust for bit6 swizzling */
2585 if (((byte_x
/ 8) % 2) == 1) {
2586 if (((byte_y
/ 8) % 2) == 0) {
2598 intel_miptree_updownsample(struct brw_context
*brw
,
2599 struct intel_mipmap_tree
*src
,
2600 struct intel_mipmap_tree
*dst
)
2602 brw_blorp_blit_miptrees(brw
,
2603 src
, 0 /* level */, 0 /* layer */,
2604 src
->format
, SWIZZLE_XYZW
,
2605 dst
, 0 /* level */, 0 /* layer */, dst
->format
,
2607 src
->logical_width0
, src
->logical_height0
,
2609 dst
->logical_width0
, dst
->logical_height0
,
2610 GL_NEAREST
, false, false /*mirror x, y*/,
2613 if (src
->stencil_mt
) {
2614 brw_blorp_blit_miptrees(brw
,
2615 src
->stencil_mt
, 0 /* level */, 0 /* layer */,
2616 src
->stencil_mt
->format
, SWIZZLE_XYZW
,
2617 dst
->stencil_mt
, 0 /* level */, 0 /* layer */,
2618 dst
->stencil_mt
->format
,
2620 src
->logical_width0
, src
->logical_height0
,
2622 dst
->logical_width0
, dst
->logical_height0
,
2623 GL_NEAREST
, false, false /*mirror x, y*/,
2624 false, false /* decode/encode srgb */);
2629 intel_update_r8stencil(struct brw_context
*brw
,
2630 struct intel_mipmap_tree
*mt
)
2632 assert(brw
->gen
>= 7);
2633 struct intel_mipmap_tree
*src
=
2634 mt
->format
== MESA_FORMAT_S_UINT8
? mt
: mt
->stencil_mt
;
2635 if (!src
|| brw
->gen
>= 8 || !src
->r8stencil_needs_update
)
2638 if (!mt
->r8stencil_mt
) {
2639 const uint32_t r8stencil_flags
=
2640 MIPTREE_LAYOUT_ACCELERATED_UPLOAD
| MIPTREE_LAYOUT_TILING_Y
|
2641 MIPTREE_LAYOUT_DISABLE_AUX
;
2642 assert(brw
->gen
> 6); /* Handle MIPTREE_LAYOUT_GEN6_HIZ_STENCIL */
2643 mt
->r8stencil_mt
= intel_miptree_create(brw
,
2645 MESA_FORMAT_R_UINT8
,
2648 src
->logical_width0
,
2649 src
->logical_height0
,
2650 src
->logical_depth0
,
2653 assert(mt
->r8stencil_mt
);
2656 struct intel_mipmap_tree
*dst
= mt
->r8stencil_mt
;
2658 for (int level
= src
->first_level
; level
<= src
->last_level
; level
++) {
2659 const unsigned depth
= src
->level
[level
].depth
;
2661 for (unsigned layer
= 0; layer
< depth
; layer
++) {
2662 brw_blorp_copy_miptrees(brw
,
2666 minify(src
->logical_width0
, level
),
2667 minify(src
->logical_height0
, level
));
2671 brw_render_cache_set_check_flush(brw
, dst
->bo
);
2672 src
->r8stencil_needs_update
= false;
2676 intel_miptree_map_raw(struct brw_context
*brw
,
2677 struct intel_mipmap_tree
*mt
,
2680 struct brw_bo
*bo
= mt
->bo
;
2682 if (brw_batch_references(&brw
->batch
, bo
))
2683 intel_batchbuffer_flush(brw
);
2685 return brw_bo_map(brw
, bo
, mode
);
2689 intel_miptree_unmap_raw(struct intel_mipmap_tree
*mt
)
2691 brw_bo_unmap(mt
->bo
);
2695 intel_miptree_map_gtt(struct brw_context
*brw
,
2696 struct intel_mipmap_tree
*mt
,
2697 struct intel_miptree_map
*map
,
2698 unsigned int level
, unsigned int slice
)
2700 unsigned int bw
, bh
;
2702 unsigned int image_x
, image_y
;
2703 intptr_t x
= map
->x
;
2704 intptr_t y
= map
->y
;
2706 /* For compressed formats, the stride is the number of bytes per
2707 * row of blocks. intel_miptree_get_image_offset() already does
2710 _mesa_get_format_block_size(mt
->format
, &bw
, &bh
);
2711 assert(y
% bh
== 0);
2712 assert(x
% bw
== 0);
2716 base
= intel_miptree_map_raw(brw
, mt
, map
->mode
) + mt
->offset
;
2721 /* Note that in the case of cube maps, the caller must have passed the
2722 * slice number referencing the face.
2724 intel_miptree_get_image_offset(mt
, level
, slice
, &image_x
, &image_y
);
2728 map
->stride
= mt
->pitch
;
2729 map
->ptr
= base
+ y
* map
->stride
+ x
* mt
->cpp
;
2732 DBG("%s: %d,%d %dx%d from mt %p (%s) "
2733 "%"PRIiPTR
",%"PRIiPTR
" = %p/%d\n", __func__
,
2734 map
->x
, map
->y
, map
->w
, map
->h
,
2735 mt
, _mesa_get_format_name(mt
->format
),
2736 x
, y
, map
->ptr
, map
->stride
);
2740 intel_miptree_unmap_gtt(struct intel_mipmap_tree
*mt
)
2742 intel_miptree_unmap_raw(mt
);
2746 intel_miptree_map_blit(struct brw_context
*brw
,
2747 struct intel_mipmap_tree
*mt
,
2748 struct intel_miptree_map
*map
,
2749 unsigned int level
, unsigned int slice
)
2751 map
->linear_mt
= intel_miptree_create(brw
, GL_TEXTURE_2D
, mt
->format
,
2752 /* first_level */ 0,
2756 MIPTREE_LAYOUT_TILING_NONE
);
2758 if (!map
->linear_mt
) {
2759 fprintf(stderr
, "Failed to allocate blit temporary\n");
2762 map
->stride
= map
->linear_mt
->pitch
;
2764 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2765 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2766 * invalidate is set, since we'll be writing the whole rectangle from our
2767 * temporary buffer back out.
2769 if (!(map
->mode
& GL_MAP_INVALIDATE_RANGE_BIT
)) {
2770 if (!intel_miptree_copy(brw
,
2771 mt
, level
, slice
, map
->x
, map
->y
,
2772 map
->linear_mt
, 0, 0, 0, 0,
2774 fprintf(stderr
, "Failed to blit\n");
2779 map
->ptr
= intel_miptree_map_raw(brw
, map
->linear_mt
, map
->mode
);
2781 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__
,
2782 map
->x
, map
->y
, map
->w
, map
->h
,
2783 mt
, _mesa_get_format_name(mt
->format
),
2784 level
, slice
, map
->ptr
, map
->stride
);
2789 intel_miptree_release(&map
->linear_mt
);
2795 intel_miptree_unmap_blit(struct brw_context
*brw
,
2796 struct intel_mipmap_tree
*mt
,
2797 struct intel_miptree_map
*map
,
2801 struct gl_context
*ctx
= &brw
->ctx
;
2803 intel_miptree_unmap_raw(map
->linear_mt
);
2805 if (map
->mode
& GL_MAP_WRITE_BIT
) {
2806 bool ok
= intel_miptree_copy(brw
,
2807 map
->linear_mt
, 0, 0, 0, 0,
2808 mt
, level
, slice
, map
->x
, map
->y
,
2810 WARN_ONCE(!ok
, "Failed to blit from linear temporary mapping");
2813 intel_miptree_release(&map
->linear_mt
);
2817 * "Map" a buffer by copying it to an untiled temporary using MOVNTDQA.
2819 #if defined(USE_SSE41)
2821 intel_miptree_map_movntdqa(struct brw_context
*brw
,
2822 struct intel_mipmap_tree
*mt
,
2823 struct intel_miptree_map
*map
,
2824 unsigned int level
, unsigned int slice
)
2826 assert(map
->mode
& GL_MAP_READ_BIT
);
2827 assert(!(map
->mode
& GL_MAP_WRITE_BIT
));
2829 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__
,
2830 map
->x
, map
->y
, map
->w
, map
->h
,
2831 mt
, _mesa_get_format_name(mt
->format
),
2832 level
, slice
, map
->ptr
, map
->stride
);
2834 /* Map the original image */
2837 intel_miptree_get_image_offset(mt
, level
, slice
, &image_x
, &image_y
);
2841 void *src
= intel_miptree_map_raw(brw
, mt
, map
->mode
);
2847 src
+= image_y
* mt
->pitch
;
2848 src
+= image_x
* mt
->cpp
;
2850 /* Due to the pixel offsets for the particular image being mapped, our
2851 * src pointer may not be 16-byte aligned. However, if the pitch is
2852 * divisible by 16, then the amount by which it's misaligned will remain
2853 * consistent from row to row.
2855 assert((mt
->pitch
% 16) == 0);
2856 const int misalignment
= ((uintptr_t) src
) & 15;
2858 /* Create an untiled temporary buffer for the mapping. */
2859 const unsigned width_bytes
= _mesa_format_row_stride(mt
->format
, map
->w
);
2861 map
->stride
= ALIGN(misalignment
+ width_bytes
, 16);
2863 map
->buffer
= _mesa_align_malloc(map
->stride
* map
->h
, 16);
2864 /* Offset the destination so it has the same misalignment as src. */
2865 map
->ptr
= map
->buffer
+ misalignment
;
2867 assert((((uintptr_t) map
->ptr
) & 15) == misalignment
);
2869 for (uint32_t y
= 0; y
< map
->h
; y
++) {
2870 void *dst_ptr
= map
->ptr
+ y
* map
->stride
;
2871 void *src_ptr
= src
+ y
* mt
->pitch
;
2873 _mesa_streaming_load_memcpy(dst_ptr
, src_ptr
, width_bytes
);
2876 intel_miptree_unmap_raw(mt
);
2880 intel_miptree_unmap_movntdqa(struct brw_context
*brw
,
2881 struct intel_mipmap_tree
*mt
,
2882 struct intel_miptree_map
*map
,
2886 _mesa_align_free(map
->buffer
);
2893 intel_miptree_map_s8(struct brw_context
*brw
,
2894 struct intel_mipmap_tree
*mt
,
2895 struct intel_miptree_map
*map
,
2896 unsigned int level
, unsigned int slice
)
2898 map
->stride
= map
->w
;
2899 map
->buffer
= map
->ptr
= malloc(map
->stride
* map
->h
);
2903 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2904 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2905 * invalidate is set, since we'll be writing the whole rectangle from our
2906 * temporary buffer back out.
2908 if (!(map
->mode
& GL_MAP_INVALIDATE_RANGE_BIT
)) {
2909 uint8_t *untiled_s8_map
= map
->ptr
;
2910 uint8_t *tiled_s8_map
= intel_miptree_map_raw(brw
, mt
, GL_MAP_READ_BIT
);
2911 unsigned int image_x
, image_y
;
2913 intel_miptree_get_image_offset(mt
, level
, slice
, &image_x
, &image_y
);
2915 for (uint32_t y
= 0; y
< map
->h
; y
++) {
2916 for (uint32_t x
= 0; x
< map
->w
; x
++) {
2917 ptrdiff_t offset
= intel_offset_S8(mt
->pitch
,
2918 x
+ image_x
+ map
->x
,
2919 y
+ image_y
+ map
->y
,
2920 brw
->has_swizzling
);
2921 untiled_s8_map
[y
* map
->w
+ x
] = tiled_s8_map
[offset
];
2925 intel_miptree_unmap_raw(mt
);
2927 DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __func__
,
2928 map
->x
, map
->y
, map
->w
, map
->h
,
2929 mt
, map
->x
+ image_x
, map
->y
+ image_y
, map
->ptr
, map
->stride
);
2931 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__
,
2932 map
->x
, map
->y
, map
->w
, map
->h
,
2933 mt
, map
->ptr
, map
->stride
);
2938 intel_miptree_unmap_s8(struct brw_context
*brw
,
2939 struct intel_mipmap_tree
*mt
,
2940 struct intel_miptree_map
*map
,
2944 if (map
->mode
& GL_MAP_WRITE_BIT
) {
2945 unsigned int image_x
, image_y
;
2946 uint8_t *untiled_s8_map
= map
->ptr
;
2947 uint8_t *tiled_s8_map
= intel_miptree_map_raw(brw
, mt
, GL_MAP_WRITE_BIT
);
2949 intel_miptree_get_image_offset(mt
, level
, slice
, &image_x
, &image_y
);
2951 for (uint32_t y
= 0; y
< map
->h
; y
++) {
2952 for (uint32_t x
= 0; x
< map
->w
; x
++) {
2953 ptrdiff_t offset
= intel_offset_S8(mt
->pitch
,
2954 image_x
+ x
+ map
->x
,
2955 image_y
+ y
+ map
->y
,
2956 brw
->has_swizzling
);
2957 tiled_s8_map
[offset
] = untiled_s8_map
[y
* map
->w
+ x
];
2961 intel_miptree_unmap_raw(mt
);
2968 intel_miptree_map_etc(struct brw_context
*brw
,
2969 struct intel_mipmap_tree
*mt
,
2970 struct intel_miptree_map
*map
,
2974 assert(mt
->etc_format
!= MESA_FORMAT_NONE
);
2975 if (mt
->etc_format
== MESA_FORMAT_ETC1_RGB8
) {
2976 assert(mt
->format
== MESA_FORMAT_R8G8B8X8_UNORM
);
2979 assert(map
->mode
& GL_MAP_WRITE_BIT
);
2980 assert(map
->mode
& GL_MAP_INVALIDATE_RANGE_BIT
);
2982 map
->stride
= _mesa_format_row_stride(mt
->etc_format
, map
->w
);
2983 map
->buffer
= malloc(_mesa_format_image_size(mt
->etc_format
,
2984 map
->w
, map
->h
, 1));
2985 map
->ptr
= map
->buffer
;
2989 intel_miptree_unmap_etc(struct brw_context
*brw
,
2990 struct intel_mipmap_tree
*mt
,
2991 struct intel_miptree_map
*map
,
2997 intel_miptree_get_image_offset(mt
, level
, slice
, &image_x
, &image_y
);
3002 uint8_t *dst
= intel_miptree_map_raw(brw
, mt
, GL_MAP_WRITE_BIT
)
3003 + image_y
* mt
->pitch
3004 + image_x
* mt
->cpp
;
3006 if (mt
->etc_format
== MESA_FORMAT_ETC1_RGB8
)
3007 _mesa_etc1_unpack_rgba8888(dst
, mt
->pitch
,
3008 map
->ptr
, map
->stride
,
3011 _mesa_unpack_etc2_format(dst
, mt
->pitch
,
3012 map
->ptr
, map
->stride
,
3013 map
->w
, map
->h
, mt
->etc_format
);
3015 intel_miptree_unmap_raw(mt
);
3020 * Mapping function for packed depth/stencil miptrees backed by real separate
3021 * miptrees for depth and stencil.
3023 * On gen7, and to support HiZ pre-gen7, we have to have the stencil buffer
3024 * separate from the depth buffer. Yet at the GL API level, we have to expose
3025 * packed depth/stencil textures and FBO attachments, and Mesa core expects to
3026 * be able to map that memory for texture storage and glReadPixels-type
3027 * operations. We give Mesa core that access by mallocing a temporary and
3028 * copying the data between the actual backing store and the temporary.
3031 intel_miptree_map_depthstencil(struct brw_context
*brw
,
3032 struct intel_mipmap_tree
*mt
,
3033 struct intel_miptree_map
*map
,
3034 unsigned int level
, unsigned int slice
)
3036 struct intel_mipmap_tree
*z_mt
= mt
;
3037 struct intel_mipmap_tree
*s_mt
= mt
->stencil_mt
;
3038 bool map_z32f_x24s8
= mt
->format
== MESA_FORMAT_Z_FLOAT32
;
3039 int packed_bpp
= map_z32f_x24s8
? 8 : 4;
3041 map
->stride
= map
->w
* packed_bpp
;
3042 map
->buffer
= map
->ptr
= malloc(map
->stride
* map
->h
);
3046 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
3047 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
3048 * invalidate is set, since we'll be writing the whole rectangle from our
3049 * temporary buffer back out.
3051 if (!(map
->mode
& GL_MAP_INVALIDATE_RANGE_BIT
)) {
3052 uint32_t *packed_map
= map
->ptr
;
3053 uint8_t *s_map
= intel_miptree_map_raw(brw
, s_mt
, GL_MAP_READ_BIT
);
3054 uint32_t *z_map
= intel_miptree_map_raw(brw
, z_mt
, GL_MAP_READ_BIT
);
3055 unsigned int s_image_x
, s_image_y
;
3056 unsigned int z_image_x
, z_image_y
;
3058 intel_miptree_get_image_offset(s_mt
, level
, slice
,
3059 &s_image_x
, &s_image_y
);
3060 intel_miptree_get_image_offset(z_mt
, level
, slice
,
3061 &z_image_x
, &z_image_y
);
3063 for (uint32_t y
= 0; y
< map
->h
; y
++) {
3064 for (uint32_t x
= 0; x
< map
->w
; x
++) {
3065 int map_x
= map
->x
+ x
, map_y
= map
->y
+ y
;
3066 ptrdiff_t s_offset
= intel_offset_S8(s_mt
->pitch
,
3069 brw
->has_swizzling
);
3070 ptrdiff_t z_offset
= ((map_y
+ z_image_y
) *
3072 (map_x
+ z_image_x
));
3073 uint8_t s
= s_map
[s_offset
];
3074 uint32_t z
= z_map
[z_offset
];
3076 if (map_z32f_x24s8
) {
3077 packed_map
[(y
* map
->w
+ x
) * 2 + 0] = z
;
3078 packed_map
[(y
* map
->w
+ x
) * 2 + 1] = s
;
3080 packed_map
[y
* map
->w
+ x
] = (s
<< 24) | (z
& 0x00ffffff);
3085 intel_miptree_unmap_raw(s_mt
);
3086 intel_miptree_unmap_raw(z_mt
);
3088 DBG("%s: %d,%d %dx%d from z mt %p %d,%d, s mt %p %d,%d = %p/%d\n",
3090 map
->x
, map
->y
, map
->w
, map
->h
,
3091 z_mt
, map
->x
+ z_image_x
, map
->y
+ z_image_y
,
3092 s_mt
, map
->x
+ s_image_x
, map
->y
+ s_image_y
,
3093 map
->ptr
, map
->stride
);
3095 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__
,
3096 map
->x
, map
->y
, map
->w
, map
->h
,
3097 mt
, map
->ptr
, map
->stride
);
3102 intel_miptree_unmap_depthstencil(struct brw_context
*brw
,
3103 struct intel_mipmap_tree
*mt
,
3104 struct intel_miptree_map
*map
,
3108 struct intel_mipmap_tree
*z_mt
= mt
;
3109 struct intel_mipmap_tree
*s_mt
= mt
->stencil_mt
;
3110 bool map_z32f_x24s8
= mt
->format
== MESA_FORMAT_Z_FLOAT32
;
3112 if (map
->mode
& GL_MAP_WRITE_BIT
) {
3113 uint32_t *packed_map
= map
->ptr
;
3114 uint8_t *s_map
= intel_miptree_map_raw(brw
, s_mt
, GL_MAP_WRITE_BIT
);
3115 uint32_t *z_map
= intel_miptree_map_raw(brw
, z_mt
, GL_MAP_WRITE_BIT
);
3116 unsigned int s_image_x
, s_image_y
;
3117 unsigned int z_image_x
, z_image_y
;
3119 intel_miptree_get_image_offset(s_mt
, level
, slice
,
3120 &s_image_x
, &s_image_y
);
3121 intel_miptree_get_image_offset(z_mt
, level
, slice
,
3122 &z_image_x
, &z_image_y
);
3124 for (uint32_t y
= 0; y
< map
->h
; y
++) {
3125 for (uint32_t x
= 0; x
< map
->w
; x
++) {
3126 ptrdiff_t s_offset
= intel_offset_S8(s_mt
->pitch
,
3127 x
+ s_image_x
+ map
->x
,
3128 y
+ s_image_y
+ map
->y
,
3129 brw
->has_swizzling
);
3130 ptrdiff_t z_offset
= ((y
+ z_image_y
+ map
->y
) *
3132 (x
+ z_image_x
+ map
->x
));
3134 if (map_z32f_x24s8
) {
3135 z_map
[z_offset
] = packed_map
[(y
* map
->w
+ x
) * 2 + 0];
3136 s_map
[s_offset
] = packed_map
[(y
* map
->w
+ x
) * 2 + 1];
3138 uint32_t packed
= packed_map
[y
* map
->w
+ x
];
3139 s_map
[s_offset
] = packed
>> 24;
3140 z_map
[z_offset
] = packed
;
3145 intel_miptree_unmap_raw(s_mt
);
3146 intel_miptree_unmap_raw(z_mt
);
3148 DBG("%s: %d,%d %dx%d from z mt %p (%s) %d,%d, s mt %p %d,%d = %p/%d\n",
3150 map
->x
, map
->y
, map
->w
, map
->h
,
3151 z_mt
, _mesa_get_format_name(z_mt
->format
),
3152 map
->x
+ z_image_x
, map
->y
+ z_image_y
,
3153 s_mt
, map
->x
+ s_image_x
, map
->y
+ s_image_y
,
3154 map
->ptr
, map
->stride
);
3161 * Create and attach a map to the miptree at (level, slice). Return the
3164 static struct intel_miptree_map
*
3165 intel_miptree_attach_map(struct intel_mipmap_tree
*mt
,
3174 struct intel_miptree_map
*map
= calloc(1, sizeof(*map
));
3179 assert(mt
->level
[level
].slice
[slice
].map
== NULL
);
3180 mt
->level
[level
].slice
[slice
].map
= map
;
3192 * Release the map at (level, slice).
3195 intel_miptree_release_map(struct intel_mipmap_tree
*mt
,
3199 struct intel_miptree_map
**map
;
3201 map
= &mt
->level
[level
].slice
[slice
].map
;
3207 can_blit_slice(struct intel_mipmap_tree
*mt
,
3208 unsigned int level
, unsigned int slice
)
3210 /* See intel_miptree_blit() for details on the 32k pitch limit. */
3211 if (mt
->pitch
>= 32768)
3218 use_intel_mipree_map_blit(struct brw_context
*brw
,
3219 struct intel_mipmap_tree
*mt
,
3225 /* It's probably not worth swapping to the blit ring because of
3226 * all the overhead involved.
3228 !(mode
& GL_MAP_WRITE_BIT
) &&
3230 (mt
->tiling
== I915_TILING_X
||
3231 /* Prior to Sandybridge, the blitter can't handle Y tiling */
3232 (brw
->gen
>= 6 && mt
->tiling
== I915_TILING_Y
) ||
3233 /* Fast copy blit on skl+ supports all tiling formats. */
3235 can_blit_slice(mt
, level
, slice
))
3238 if (mt
->tiling
!= I915_TILING_NONE
&&
3239 mt
->bo
->size
>= brw
->max_gtt_map_object_size
) {
3240 assert(can_blit_slice(mt
, level
, slice
));
3248 * Parameter \a out_stride has type ptrdiff_t not because the buffer stride may
3249 * exceed 32 bits but to diminish the likelihood subtle bugs in pointer
3250 * arithmetic overflow.
3252 * If you call this function and use \a out_stride, then you're doing pointer
3253 * arithmetic on \a out_ptr. The type of \a out_stride doesn't prevent all
3254 * bugs. The caller must still take care to avoid 32-bit overflow errors in
3255 * all arithmetic expressions that contain buffer offsets and pixel sizes,
3256 * which usually have type uint32_t or GLuint.
3259 intel_miptree_map(struct brw_context
*brw
,
3260 struct intel_mipmap_tree
*mt
,
3269 ptrdiff_t *out_stride
)
3271 struct intel_miptree_map
*map
;
3273 assert(mt
->num_samples
<= 1);
3275 map
= intel_miptree_attach_map(mt
, level
, slice
, x
, y
, w
, h
, mode
);
3282 intel_miptree_access_raw(brw
, mt
, level
, slice
,
3283 map
->mode
& GL_MAP_WRITE_BIT
);
3285 if (mt
->format
== MESA_FORMAT_S_UINT8
) {
3286 intel_miptree_map_s8(brw
, mt
, map
, level
, slice
);
3287 } else if (mt
->etc_format
!= MESA_FORMAT_NONE
&&
3288 !(mode
& BRW_MAP_DIRECT_BIT
)) {
3289 intel_miptree_map_etc(brw
, mt
, map
, level
, slice
);
3290 } else if (mt
->stencil_mt
&& !(mode
& BRW_MAP_DIRECT_BIT
)) {
3291 intel_miptree_map_depthstencil(brw
, mt
, map
, level
, slice
);
3292 } else if (use_intel_mipree_map_blit(brw
, mt
, mode
, level
, slice
)) {
3293 intel_miptree_map_blit(brw
, mt
, map
, level
, slice
);
3294 #if defined(USE_SSE41)
3295 } else if (!(mode
& GL_MAP_WRITE_BIT
) &&
3296 !mt
->compressed
&& cpu_has_sse4_1
&&
3297 (mt
->pitch
% 16 == 0)) {
3298 intel_miptree_map_movntdqa(brw
, mt
, map
, level
, slice
);
3301 intel_miptree_map_gtt(brw
, mt
, map
, level
, slice
);
3304 *out_ptr
= map
->ptr
;
3305 *out_stride
= map
->stride
;
3307 if (map
->ptr
== NULL
)
3308 intel_miptree_release_map(mt
, level
, slice
);
3312 intel_miptree_unmap(struct brw_context
*brw
,
3313 struct intel_mipmap_tree
*mt
,
3317 struct intel_miptree_map
*map
= mt
->level
[level
].slice
[slice
].map
;
3319 assert(mt
->num_samples
<= 1);
3324 DBG("%s: mt %p (%s) level %d slice %d\n", __func__
,
3325 mt
, _mesa_get_format_name(mt
->format
), level
, slice
);
3327 if (mt
->format
== MESA_FORMAT_S_UINT8
) {
3328 intel_miptree_unmap_s8(brw
, mt
, map
, level
, slice
);
3329 } else if (mt
->etc_format
!= MESA_FORMAT_NONE
&&
3330 !(map
->mode
& BRW_MAP_DIRECT_BIT
)) {
3331 intel_miptree_unmap_etc(brw
, mt
, map
, level
, slice
);
3332 } else if (mt
->stencil_mt
&& !(map
->mode
& BRW_MAP_DIRECT_BIT
)) {
3333 intel_miptree_unmap_depthstencil(brw
, mt
, map
, level
, slice
);
3334 } else if (map
->linear_mt
) {
3335 intel_miptree_unmap_blit(brw
, mt
, map
, level
, slice
);
3336 #if defined(USE_SSE41)
3337 } else if (map
->buffer
&& cpu_has_sse4_1
) {
3338 intel_miptree_unmap_movntdqa(brw
, mt
, map
, level
, slice
);
3341 intel_miptree_unmap_gtt(mt
);
3344 intel_miptree_release_map(mt
, level
, slice
);
3348 get_isl_surf_dim(GLenum target
)
3352 case GL_TEXTURE_1D_ARRAY
:
3353 return ISL_SURF_DIM_1D
;
3356 case GL_TEXTURE_2D_ARRAY
:
3357 case GL_TEXTURE_RECTANGLE
:
3358 case GL_TEXTURE_CUBE_MAP
:
3359 case GL_TEXTURE_CUBE_MAP_ARRAY
:
3360 case GL_TEXTURE_2D_MULTISAMPLE
:
3361 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
3362 case GL_TEXTURE_EXTERNAL_OES
:
3363 return ISL_SURF_DIM_2D
;
3366 return ISL_SURF_DIM_3D
;
3369 unreachable("Invalid texture target");
3373 get_isl_dim_layout(const struct gen_device_info
*devinfo
, uint32_t tiling
,
3374 GLenum target
, enum miptree_array_layout array_layout
)
3376 if (array_layout
== GEN6_HIZ_STENCIL
)
3377 return ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ
;
3381 case GL_TEXTURE_1D_ARRAY
:
3382 return (devinfo
->gen
>= 9 && tiling
== I915_TILING_NONE
?
3383 ISL_DIM_LAYOUT_GEN9_1D
: ISL_DIM_LAYOUT_GEN4_2D
);
3386 case GL_TEXTURE_2D_ARRAY
:
3387 case GL_TEXTURE_RECTANGLE
:
3388 case GL_TEXTURE_2D_MULTISAMPLE
:
3389 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
3390 case GL_TEXTURE_EXTERNAL_OES
:
3391 return ISL_DIM_LAYOUT_GEN4_2D
;
3393 case GL_TEXTURE_CUBE_MAP
:
3394 case GL_TEXTURE_CUBE_MAP_ARRAY
:
3395 return (devinfo
->gen
== 4 ? ISL_DIM_LAYOUT_GEN4_3D
:
3396 ISL_DIM_LAYOUT_GEN4_2D
);
3399 return (devinfo
->gen
>= 9 ?
3400 ISL_DIM_LAYOUT_GEN4_2D
: ISL_DIM_LAYOUT_GEN4_3D
);
3403 unreachable("Invalid texture target");
3407 intel_miptree_get_isl_tiling(const struct intel_mipmap_tree
*mt
)
3409 if (mt
->format
== MESA_FORMAT_S_UINT8
) {
3410 return ISL_TILING_W
;
3412 switch (mt
->tiling
) {
3413 case I915_TILING_NONE
:
3414 return ISL_TILING_LINEAR
;
3416 return ISL_TILING_X
;
3418 return ISL_TILING_Y0
;
3420 unreachable("Invalid tiling mode");
3426 intel_miptree_get_isl_surf(struct brw_context
*brw
,
3427 const struct intel_mipmap_tree
*mt
,
3428 struct isl_surf
*surf
)
3430 surf
->dim
= get_isl_surf_dim(mt
->target
);
3431 surf
->dim_layout
= get_isl_dim_layout(&brw
->screen
->devinfo
,
3432 mt
->tiling
, mt
->target
,
3435 if (mt
->num_samples
> 1) {
3436 switch (mt
->msaa_layout
) {
3437 case INTEL_MSAA_LAYOUT_IMS
:
3438 surf
->msaa_layout
= ISL_MSAA_LAYOUT_INTERLEAVED
;
3440 case INTEL_MSAA_LAYOUT_UMS
:
3441 case INTEL_MSAA_LAYOUT_CMS
:
3442 surf
->msaa_layout
= ISL_MSAA_LAYOUT_ARRAY
;
3445 unreachable("Invalid MSAA layout");
3448 surf
->msaa_layout
= ISL_MSAA_LAYOUT_NONE
;
3451 surf
->tiling
= intel_miptree_get_isl_tiling(mt
);
3453 if (mt
->format
== MESA_FORMAT_S_UINT8
) {
3454 /* The ISL definition of row_pitch matches the surface state pitch field
3455 * a bit better than intel_mipmap_tree. In particular, ISL incorporates
3456 * the factor of 2 for W-tiling in row_pitch.
3458 surf
->row_pitch
= 2 * mt
->pitch
;
3460 surf
->row_pitch
= mt
->pitch
;
3463 surf
->format
= translate_tex_format(brw
, mt
->format
, false);
3465 if (brw
->gen
>= 9) {
3466 if (surf
->dim
== ISL_SURF_DIM_1D
&& surf
->tiling
== ISL_TILING_LINEAR
) {
3467 /* For gen9 1-D surfaces, intel_mipmap_tree has a bogus alignment. */
3468 surf
->image_alignment_el
= isl_extent3d(64, 1, 1);
3470 /* On gen9+, intel_mipmap_tree stores the horizontal and vertical
3471 * alignment in terms of surface elements like we want.
3473 surf
->image_alignment_el
= isl_extent3d(mt
->halign
, mt
->valign
, 1);
3476 /* On earlier gens it's stored in pixels. */
3478 _mesa_get_format_block_size(mt
->format
, &bw
, &bh
);
3479 surf
->image_alignment_el
=
3480 isl_extent3d(mt
->halign
/ bw
, mt
->valign
/ bh
, 1);
3483 surf
->logical_level0_px
.width
= mt
->logical_width0
;
3484 surf
->logical_level0_px
.height
= mt
->logical_height0
;
3485 if (surf
->dim
== ISL_SURF_DIM_3D
) {
3486 surf
->logical_level0_px
.depth
= mt
->logical_depth0
;
3487 surf
->logical_level0_px
.array_len
= 1;
3489 surf
->logical_level0_px
.depth
= 1;
3490 surf
->logical_level0_px
.array_len
= mt
->logical_depth0
;
3493 surf
->phys_level0_sa
.width
= mt
->physical_width0
;
3494 surf
->phys_level0_sa
.height
= mt
->physical_height0
;
3495 if (surf
->dim
== ISL_SURF_DIM_3D
) {
3496 surf
->phys_level0_sa
.depth
= mt
->physical_depth0
;
3497 surf
->phys_level0_sa
.array_len
= 1;
3499 surf
->phys_level0_sa
.depth
= 1;
3500 surf
->phys_level0_sa
.array_len
= mt
->physical_depth0
;
3503 surf
->levels
= mt
->last_level
- mt
->first_level
+ 1;
3504 surf
->samples
= MAX2(mt
->num_samples
, 1);
3506 surf
->size
= 0; /* TODO */
3507 surf
->alignment
= 0; /* TODO */
3509 switch (surf
->dim_layout
) {
3510 case ISL_DIM_LAYOUT_GEN4_2D
:
3511 case ISL_DIM_LAYOUT_GEN4_3D
:
3512 case ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ
:
3513 if (brw
->gen
>= 9) {
3514 surf
->array_pitch_el_rows
= mt
->qpitch
;
3517 _mesa_get_format_block_size(mt
->format
, &bw
, &bh
);
3518 assert(mt
->qpitch
% bh
== 0);
3519 surf
->array_pitch_el_rows
= mt
->qpitch
/ bh
;
3522 case ISL_DIM_LAYOUT_GEN9_1D
:
3523 surf
->array_pitch_el_rows
= 1;
3527 switch (mt
->array_layout
) {
3528 case ALL_LOD_IN_EACH_SLICE
:
3529 surf
->array_pitch_span
= ISL_ARRAY_PITCH_SPAN_FULL
;
3531 case ALL_SLICES_AT_EACH_LOD
:
3532 case GEN6_HIZ_STENCIL
:
3533 surf
->array_pitch_span
= ISL_ARRAY_PITCH_SPAN_COMPACT
;
3536 unreachable("Invalid array layout");
3539 GLenum base_format
= _mesa_get_format_base_format(mt
->format
);
3540 switch (base_format
) {
3541 case GL_DEPTH_COMPONENT
:
3542 surf
->usage
= ISL_SURF_USAGE_DEPTH_BIT
| ISL_SURF_USAGE_TEXTURE_BIT
;
3544 case GL_STENCIL_INDEX
:
3545 surf
->usage
= ISL_SURF_USAGE_STENCIL_BIT
;
3547 surf
->usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
3549 case GL_DEPTH_STENCIL
:
3550 /* In this case we only texture from the depth part */
3551 surf
->usage
= ISL_SURF_USAGE_DEPTH_BIT
| ISL_SURF_USAGE_STENCIL_BIT
|
3552 ISL_SURF_USAGE_TEXTURE_BIT
;
3555 surf
->usage
= ISL_SURF_USAGE_TEXTURE_BIT
;
3556 if (brw
->format_supported_as_render_target
[mt
->format
])
3557 surf
->usage
= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
3561 if (_mesa_is_cube_map_texture(mt
->target
))
3562 surf
->usage
|= ISL_SURF_USAGE_CUBE_BIT
;
3565 /* WARNING: THE SURFACE CREATED BY THIS FUNCTION IS NOT COMPLETE AND CANNOT BE
3566 * USED FOR ANY REAL CALCULATIONS. THE ONLY VALID USE OF SUCH A SURFACE IS TO
3567 * PASS IT INTO isl_surf_fill_state.
3570 intel_miptree_get_aux_isl_surf(struct brw_context
*brw
,
3571 const struct intel_mipmap_tree
*mt
,
3572 struct isl_surf
*surf
,
3573 enum isl_aux_usage
*usage
)
3575 uint32_t aux_pitch
, aux_qpitch
;
3577 aux_pitch
= mt
->mcs_buf
->pitch
;
3578 aux_qpitch
= mt
->mcs_buf
->qpitch
;
3580 if (mt
->num_samples
> 1) {
3581 assert(mt
->msaa_layout
== INTEL_MSAA_LAYOUT_CMS
);
3582 *usage
= ISL_AUX_USAGE_MCS
;
3583 } else if (intel_miptree_is_lossless_compressed(brw
, mt
)) {
3584 assert(brw
->gen
>= 9);
3585 *usage
= ISL_AUX_USAGE_CCS_E
;
3586 } else if ((mt
->aux_disable
& INTEL_AUX_DISABLE_CCS
) == 0) {
3587 *usage
= ISL_AUX_USAGE_CCS_D
;
3589 unreachable("Invalid MCS miptree");
3591 } else if (mt
->hiz_buf
) {
3592 aux_pitch
= mt
->hiz_buf
->aux_base
.pitch
;
3593 aux_qpitch
= mt
->hiz_buf
->aux_base
.qpitch
;
3595 *usage
= ISL_AUX_USAGE_HIZ
;
3597 *usage
= ISL_AUX_USAGE_NONE
;
3601 /* Start with a copy of the original surface. */
3602 intel_miptree_get_isl_surf(brw
, mt
, surf
);
3604 /* Figure out the format and tiling of the auxiliary surface */
3606 case ISL_AUX_USAGE_NONE
:
3607 unreachable("Invalid auxiliary usage");
3609 case ISL_AUX_USAGE_HIZ
:
3610 isl_surf_get_hiz_surf(&brw
->isl_dev
, surf
, surf
);
3613 case ISL_AUX_USAGE_MCS
:
3616 * "When Auxiliary Surface Mode is set to AUX_CCS_D or AUX_CCS_E,
3617 * HALIGN 16 must be used."
3620 assert(mt
->halign
== 16);
3622 isl_surf_get_mcs_surf(&brw
->isl_dev
, surf
, surf
);
3625 case ISL_AUX_USAGE_CCS_D
:
3626 case ISL_AUX_USAGE_CCS_E
:
3628 * From the BDW PRM, Volume 2d, page 260 (RENDER_SURFACE_STATE):
3630 * "When MCS is enabled for non-MSRT, HALIGN_16 must be used"
3632 * From the hardware spec for GEN9:
3634 * "When Auxiliary Surface Mode is set to AUX_CCS_D or AUX_CCS_E,
3635 * HALIGN 16 must be used."
3637 assert(mt
->num_samples
<= 1);
3639 assert(mt
->halign
== 16);
3641 isl_surf_get_ccs_surf(&brw
->isl_dev
, surf
, surf
);
3645 /* We want the pitch of the actual aux buffer. */
3646 surf
->row_pitch
= aux_pitch
;
3648 /* Auxiliary surfaces in ISL have compressed formats and array_pitch_el_rows
3649 * is in elements. This doesn't match intel_mipmap_tree::qpitch which is
3650 * in elements of the primary color surface so we have to divide by the
3651 * compression block height.
3653 surf
->array_pitch_el_rows
=
3654 aux_qpitch
/ isl_format_get_layout(surf
->format
)->bh
;