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_image.h"
31 #include "intel_mipmap_tree.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 uint32_t layout_flags
)
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 (layout_flags
& MIPTREE_LAYOUT_DISABLE_AUX
) {
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_ccs(const struct brw_context
*brw
, unsigned tiling
)
108 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
109 * Target(s)", beneath the "Fast Color Clear" bullet (p326):
111 * - Support is limited to tiled render targets.
113 * Gen9 changes the restriction to Y-tile only.
116 return tiling
== I915_TILING_Y
;
117 else if (brw
->gen
>= 7)
118 return tiling
!= I915_TILING_NONE
;
124 * For a single-sampled render target ("non-MSRT"), determine if an MCS buffer
125 * can be used. This doesn't (and should not) inspect any of the properties of
128 * From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render Target(s)",
129 * beneath the "Fast Color Clear" bullet (p326):
131 * - Support is for non-mip-mapped and non-array surface types only.
133 * And then later, on p327:
135 * - MCS buffer for non-MSRT is supported only for RT formats 32bpp,
138 * From the Skylake documentation, it is made clear that X-tiling is no longer
141 * - MCS and Lossless compression is supported for TiledY/TileYs/TileYf
145 intel_miptree_supports_ccs(struct brw_context
*brw
,
146 const struct intel_mipmap_tree
*mt
)
148 /* MCS support does not exist prior to Gen7 */
152 /* This function applies only to non-multisampled render targets. */
153 if (mt
->num_samples
> 1)
156 /* MCS is only supported for color buffers */
157 switch (_mesa_get_format_base_format(mt
->format
)) {
158 case GL_DEPTH_COMPONENT
:
159 case GL_DEPTH_STENCIL
:
160 case GL_STENCIL_INDEX
:
164 if (mt
->cpp
!= 4 && mt
->cpp
!= 8 && mt
->cpp
!= 16)
167 const bool mip_mapped
= mt
->first_level
!= 0 || mt
->last_level
!= 0;
168 const bool arrayed
= mt
->physical_depth0
!= 1;
171 /* Multisample surfaces with the CMS layout are not layered surfaces,
172 * yet still have physical_depth0 > 1. Assert that we don't
173 * accidentally reject a multisampled surface here. We should have
174 * rejected it earlier by explicitly checking the sample count.
176 assert(mt
->num_samples
<= 1);
179 /* Handle the hardware restrictions...
181 * All GENs have the following restriction: "MCS buffer for non-MSRT is
182 * supported only for RT formats 32bpp, 64bpp, and 128bpp."
184 * From the HSW PRM Volume 7: 3D-Media-GPGPU, page 652: (Color Clear of
185 * Non-MultiSampler Render Target Restrictions) Support is for
186 * non-mip-mapped and non-array surface types only.
188 * From the BDW PRM Volume 7: 3D-Media-GPGPU, page 649: (Color Clear of
189 * Non-MultiSampler Render Target Restriction). Mip-mapped and arrayed
190 * surfaces are supported with MCS buffer layout with these alignments in
191 * the RT space: Horizontal Alignment = 256 and Vertical Alignment = 128.
193 * From the SKL PRM Volume 7: 3D-Media-GPGPU, page 632: (Color Clear of
194 * Non-MultiSampler Render Target Restriction). Mip-mapped and arrayed
195 * surfaces are supported with MCS buffer layout with these alignments in
196 * the RT space: Horizontal Alignment = 128 and Vertical Alignment = 64.
198 if (brw
->gen
< 8 && (mip_mapped
|| arrayed
))
201 /* There's no point in using an MCS buffer if the surface isn't in a
204 if (!brw
->mesa_format_supports_render
[mt
->format
])
208 mesa_format linear_format
= _mesa_get_srgb_format_linear(mt
->format
);
209 const enum isl_format isl_format
=
210 brw_isl_format_for_mesa_format(linear_format
);
211 return isl_format_supports_ccs_e(&brw
->screen
->devinfo
, isl_format
);
217 intel_miptree_supports_hiz(struct brw_context
*brw
,
218 struct intel_mipmap_tree
*mt
)
223 switch (mt
->format
) {
224 case MESA_FORMAT_Z_FLOAT32
:
225 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT
:
226 case MESA_FORMAT_Z24_UNORM_X8_UINT
:
227 case MESA_FORMAT_Z24_UNORM_S8_UINT
:
228 case MESA_FORMAT_Z_UNORM16
:
236 /* On Gen9 support for color buffer compression was extended to single
237 * sampled surfaces. This is a helper considering both auxiliary buffer
238 * type and number of samples telling if the given miptree represents
239 * the new single sampled case - also called lossless compression.
242 intel_miptree_is_lossless_compressed(const struct brw_context
*brw
,
243 const struct intel_mipmap_tree
*mt
)
245 /* Only available from Gen9 onwards. */
249 /* Compression always requires auxiliary buffer. */
253 /* Single sample compression is represented re-using msaa compression
254 * layout type: "Compressed Multisampled Surfaces".
256 if (mt
->msaa_layout
!= INTEL_MSAA_LAYOUT_CMS
)
259 /* And finally distinguish between msaa and single sample case. */
260 return mt
->num_samples
<= 1;
264 intel_miptree_supports_ccs_e(struct brw_context
*brw
,
265 const struct intel_mipmap_tree
*mt
)
267 /* For now compression is only enabled for integer formats even though
268 * there exist supported floating point formats also. This is a heuristic
269 * decision based on current public benchmarks. In none of the cases these
270 * formats provided any improvement but a few cases were seen to regress.
271 * Hence these are left to to be enabled in the future when they are known
274 if (_mesa_get_format_datatype(mt
->format
) == GL_FLOAT
)
277 if (!intel_miptree_supports_ccs(brw
, mt
))
280 /* Fast clear can be also used to clear srgb surfaces by using equivalent
281 * linear format. This trick, however, can't be extended to be used with
282 * lossless compression and therefore a check is needed to see if the format
285 return _mesa_get_srgb_format_linear(mt
->format
) == mt
->format
;
289 * Determine depth format corresponding to a depth+stencil format,
290 * for separate stencil.
293 intel_depth_format_for_depthstencil_format(mesa_format format
) {
295 case MESA_FORMAT_Z24_UNORM_S8_UINT
:
296 return MESA_FORMAT_Z24_UNORM_X8_UINT
;
297 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT
:
298 return MESA_FORMAT_Z_FLOAT32
;
305 create_mapping_table(GLenum target
, unsigned first_level
, unsigned last_level
,
306 unsigned depth0
, struct intel_mipmap_level
*table
)
308 for (unsigned level
= first_level
; level
<= last_level
; level
++) {
310 target
== GL_TEXTURE_3D
? minify(depth0
, level
) : depth0
;
312 table
[level
].slice
= calloc(d
, sizeof(*table
[0].slice
));
313 if (!table
[level
].slice
)
320 for (unsigned level
= first_level
; level
<= last_level
; level
++)
321 free(table
[level
].slice
);
327 * @param for_bo Indicates that the caller is
328 * intel_miptree_create_for_bo(). If true, then do not create
331 static struct intel_mipmap_tree
*
332 intel_miptree_create_layout(struct brw_context
*brw
,
341 uint32_t layout_flags
)
343 struct intel_mipmap_tree
*mt
= calloc(sizeof(*mt
), 1);
347 DBG("%s target %s format %s level %d..%d slices %d <-- %p\n", __func__
,
348 _mesa_enum_to_string(target
),
349 _mesa_get_format_name(format
),
350 first_level
, last_level
, depth0
, mt
);
352 if (target
== GL_TEXTURE_1D_ARRAY
)
353 assert(height0
== 1);
357 mt
->first_level
= first_level
;
358 mt
->last_level
= last_level
;
359 mt
->logical_width0
= width0
;
360 mt
->logical_height0
= height0
;
361 mt
->logical_depth0
= depth0
;
362 mt
->is_scanout
= (layout_flags
& MIPTREE_LAYOUT_FOR_SCANOUT
) != 0;
363 mt
->aux_usage
= ISL_AUX_USAGE_NONE
;
364 mt
->supports_fast_clear
= false;
365 mt
->aux_state
= NULL
;
366 mt
->cpp
= _mesa_get_format_bytes(format
);
367 mt
->num_samples
= num_samples
;
368 mt
->compressed
= _mesa_is_format_compressed(format
);
369 mt
->msaa_layout
= INTEL_MSAA_LAYOUT_NONE
;
372 if (brw
->gen
== 6 && format
== MESA_FORMAT_S_UINT8
)
373 layout_flags
|= MIPTREE_LAYOUT_GEN6_HIZ_STENCIL
;
375 int depth_multiply
= 1;
376 if (num_samples
> 1) {
377 /* Adjust width/height/depth for MSAA */
378 mt
->msaa_layout
= compute_msaa_layout(brw
, format
, layout_flags
);
379 if (mt
->msaa_layout
== INTEL_MSAA_LAYOUT_IMS
) {
380 /* From the Ivybridge PRM, Volume 1, Part 1, page 108:
381 * "If the surface is multisampled and it is a depth or stencil
382 * surface or Multisampled Surface StorageFormat in SURFACE_STATE is
383 * MSFMT_DEPTH_STENCIL, WL and HL must be adjusted as follows before
386 * +----------------------------------------------------------------+
387 * | Num Multisamples | W_l = | H_l = |
388 * +----------------------------------------------------------------+
389 * | 2 | ceiling(W_l / 2) * 4 | H_l (no adjustment) |
390 * | 4 | ceiling(W_l / 2) * 4 | ceiling(H_l / 2) * 4 |
391 * | 8 | ceiling(W_l / 2) * 8 | ceiling(H_l / 2) * 4 |
392 * | 16 | ceiling(W_l / 2) * 8 | ceiling(H_l / 2) * 8 |
393 * +----------------------------------------------------------------+
396 * Note that MSFMT_DEPTH_STENCIL just means the IMS (interleaved)
397 * format rather than UMS/CMS (array slices). The Sandybridge PRM,
398 * Volume 1, Part 1, Page 111 has the same formula for 4x MSAA.
400 * Another more complicated explanation for these adjustments comes
401 * from the Sandybridge PRM, volume 4, part 1, page 31:
403 * "Any of the other messages (sample*, LOD, load4) used with a
404 * (4x) multisampled surface will in-effect sample a surface with
405 * double the height and width as that indicated in the surface
406 * state. Each pixel position on the original-sized surface is
407 * replaced with a 2x2 of samples with the following arrangement:
412 * Thus, when sampling from a multisampled texture, it behaves as
413 * though the layout in memory for (x,y,sample) is:
415 * (0,0,0) (0,0,2) (1,0,0) (1,0,2)
416 * (0,0,1) (0,0,3) (1,0,1) (1,0,3)
418 * (0,1,0) (0,1,2) (1,1,0) (1,1,2)
419 * (0,1,1) (0,1,3) (1,1,1) (1,1,3)
421 * However, the actual layout of multisampled data in memory is:
423 * (0,0,0) (1,0,0) (0,0,1) (1,0,1)
424 * (0,1,0) (1,1,0) (0,1,1) (1,1,1)
426 * (0,0,2) (1,0,2) (0,0,3) (1,0,3)
427 * (0,1,2) (1,1,2) (0,1,3) (1,1,3)
429 * This pattern repeats for each 2x2 pixel block.
431 * As a result, when calculating the size of our 4-sample buffer for
432 * an odd width or height, we have to align before scaling up because
433 * sample 3 is in that bottom right 2x2 block.
435 switch (num_samples
) {
437 assert(brw
->gen
>= 8);
438 width0
= ALIGN(width0
, 2) * 2;
439 height0
= ALIGN(height0
, 2);
442 width0
= ALIGN(width0
, 2) * 2;
443 height0
= ALIGN(height0
, 2) * 2;
446 width0
= ALIGN(width0
, 2) * 4;
447 height0
= ALIGN(height0
, 2) * 2;
450 width0
= ALIGN(width0
, 2) * 4;
451 height0
= ALIGN(height0
, 2) * 4;
454 /* num_samples should already have been quantized to 0, 1, 2, 4, 8
457 unreachable("not reached");
460 /* Non-interleaved */
461 depth_multiply
= num_samples
;
462 depth0
*= depth_multiply
;
466 if (!create_mapping_table(target
, first_level
, last_level
, depth0
,
472 /* Set array_layout to ALL_SLICES_AT_EACH_LOD when array_spacing_lod0 can
473 * be used. array_spacing_lod0 is only used for non-IMS MSAA surfaces on
474 * Gen 7 and 8. On Gen 8 and 9 this layout is not available but it is still
475 * used on Gen8 to make it pick a qpitch value which doesn't include space
476 * for the mipmaps. On Gen9 this is not necessary because it will
477 * automatically pick a packed qpitch value whenever mt->first_level ==
479 * TODO: can we use it elsewhere?
480 * TODO: also disable this on Gen8 and pick the qpitch value like Gen9
483 mt
->array_layout
= ALL_LOD_IN_EACH_SLICE
;
485 switch (mt
->msaa_layout
) {
486 case INTEL_MSAA_LAYOUT_NONE
:
487 case INTEL_MSAA_LAYOUT_IMS
:
488 mt
->array_layout
= ALL_LOD_IN_EACH_SLICE
;
490 case INTEL_MSAA_LAYOUT_UMS
:
491 case INTEL_MSAA_LAYOUT_CMS
:
492 mt
->array_layout
= ALL_SLICES_AT_EACH_LOD
;
497 if (target
== GL_TEXTURE_CUBE_MAP
)
498 assert(depth0
== 6 * depth_multiply
);
500 mt
->physical_width0
= width0
;
501 mt
->physical_height0
= height0
;
502 mt
->physical_depth0
= depth0
;
504 if (!(layout_flags
& MIPTREE_LAYOUT_FOR_BO
) &&
505 _mesa_get_format_base_format(format
) == GL_DEPTH_STENCIL
&&
506 (brw
->must_use_separate_stencil
||
507 (brw
->has_separate_stencil
&& intel_miptree_supports_hiz(brw
, mt
)))) {
508 uint32_t stencil_flags
= MIPTREE_LAYOUT_ACCELERATED_UPLOAD
;
510 stencil_flags
|= MIPTREE_LAYOUT_TILING_ANY
;
513 mt
->stencil_mt
= intel_miptree_create(brw
,
524 if (!mt
->stencil_mt
) {
525 intel_miptree_release(&mt
);
528 mt
->stencil_mt
->r8stencil_needs_update
= true;
530 /* Fix up the Z miptree format for how we're splitting out separate
531 * stencil. Gen7 expects there to be no stencil bits in its depth buffer.
533 mt
->format
= intel_depth_format_for_depthstencil_format(mt
->format
);
536 if (format
== mt
->format
) {
537 _mesa_problem(NULL
, "Unknown format %s in separate stencil mt\n",
538 _mesa_get_format_name(mt
->format
));
542 if (layout_flags
& MIPTREE_LAYOUT_GEN6_HIZ_STENCIL
)
543 mt
->array_layout
= GEN6_HIZ_STENCIL
;
546 * Obey HALIGN_16 constraints for Gen8 and Gen9 buffers which are
547 * multisampled or have an AUX buffer attached to it.
549 * GEN | MSRT | AUX_CCS_* or AUX_MCS
550 * -------------------------------------------
551 * 9 | HALIGN_16 | HALIGN_16
552 * 8 | HALIGN_ANY | HALIGN_16
556 if (intel_miptree_supports_ccs(brw
, mt
)) {
557 if (brw
->gen
>= 9 || (brw
->gen
== 8 && num_samples
<= 1))
558 layout_flags
|= MIPTREE_LAYOUT_FORCE_HALIGN16
;
559 } else if (brw
->gen
>= 9 && num_samples
> 1) {
560 layout_flags
|= MIPTREE_LAYOUT_FORCE_HALIGN16
;
562 const UNUSED
bool is_lossless_compressed_aux
=
563 brw
->gen
>= 9 && num_samples
== 1 &&
564 mt
->format
== MESA_FORMAT_R_UINT32
;
566 /* For now, nothing else has this requirement */
567 assert(is_lossless_compressed_aux
||
568 (layout_flags
& MIPTREE_LAYOUT_FORCE_HALIGN16
) == 0);
571 if (!brw_miptree_layout(brw
, mt
, layout_flags
)) {
572 intel_miptree_release(&mt
);
581 * Choose the aux usage for this miptree. This function must be called fairly
582 * late in the miptree create process after we have a tiling.
585 intel_miptree_choose_aux_usage(struct brw_context
*brw
,
586 struct intel_mipmap_tree
*mt
)
588 assert(mt
->aux_usage
== ISL_AUX_USAGE_NONE
);
590 if (mt
->msaa_layout
== INTEL_MSAA_LAYOUT_CMS
) {
591 mt
->aux_usage
= ISL_AUX_USAGE_MCS
;
592 } else if (intel_tiling_supports_ccs(brw
, mt
->tiling
) &&
593 intel_miptree_supports_ccs(brw
, mt
)) {
594 if (!unlikely(INTEL_DEBUG
& DEBUG_NO_RBC
) &&
595 brw
->gen
>= 9 && !mt
->is_scanout
&&
596 intel_miptree_supports_ccs_e(brw
, mt
)) {
597 mt
->aux_usage
= ISL_AUX_USAGE_CCS_E
;
599 mt
->aux_usage
= ISL_AUX_USAGE_CCS_D
;
601 } else if (intel_miptree_supports_hiz(brw
, mt
)) {
602 mt
->aux_usage
= ISL_AUX_USAGE_HIZ
;
605 /* We can do fast-clear on all auxiliary surface types that are
606 * allocated through the normal texture creation paths.
608 if (mt
->aux_usage
!= ISL_AUX_USAGE_NONE
)
609 mt
->supports_fast_clear
= true;
614 * Choose an appropriate uncompressed format for a requested
615 * compressed format, if unsupported.
618 intel_lower_compressed_format(struct brw_context
*brw
, mesa_format format
)
620 /* No need to lower ETC formats on these platforms,
621 * they are supported natively.
623 if (brw
->gen
>= 8 || brw
->is_baytrail
)
627 case MESA_FORMAT_ETC1_RGB8
:
628 return MESA_FORMAT_R8G8B8X8_UNORM
;
629 case MESA_FORMAT_ETC2_RGB8
:
630 return MESA_FORMAT_R8G8B8X8_UNORM
;
631 case MESA_FORMAT_ETC2_SRGB8
:
632 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC
:
633 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1
:
634 return MESA_FORMAT_B8G8R8A8_SRGB
;
635 case MESA_FORMAT_ETC2_RGBA8_EAC
:
636 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1
:
637 return MESA_FORMAT_R8G8B8A8_UNORM
;
638 case MESA_FORMAT_ETC2_R11_EAC
:
639 return MESA_FORMAT_R_UNORM16
;
640 case MESA_FORMAT_ETC2_SIGNED_R11_EAC
:
641 return MESA_FORMAT_R_SNORM16
;
642 case MESA_FORMAT_ETC2_RG11_EAC
:
643 return MESA_FORMAT_R16G16_UNORM
;
644 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC
:
645 return MESA_FORMAT_R16G16_SNORM
;
647 /* Non ETC1 / ETC2 format */
652 /** \brief Assert that the level and layer are valid for the miptree. */
654 intel_miptree_check_level_layer(const struct intel_mipmap_tree
*mt
,
662 assert(level
>= mt
->first_level
);
663 assert(level
<= mt
->last_level
);
665 if (mt
->surf
.size
> 0)
666 assert(layer
< (mt
->surf
.dim
== ISL_SURF_DIM_3D
?
667 minify(mt
->surf
.phys_level0_sa
.depth
, level
) :
668 mt
->surf
.phys_level0_sa
.array_len
));
670 assert(layer
< mt
->level
[level
].depth
);
673 static enum isl_aux_state
**
674 create_aux_state_map(struct intel_mipmap_tree
*mt
,
675 enum isl_aux_state initial
)
677 const uint32_t levels
= mt
->last_level
+ 1;
679 uint32_t total_slices
= 0;
680 for (uint32_t level
= 0; level
< levels
; level
++)
681 total_slices
+= mt
->level
[level
].depth
;
683 const size_t per_level_array_size
= levels
* sizeof(enum isl_aux_state
*);
685 /* We're going to allocate a single chunk of data for both the per-level
686 * reference array and the arrays of aux_state. This makes cleanup
687 * significantly easier.
689 const size_t total_size
= per_level_array_size
+
690 total_slices
* sizeof(enum isl_aux_state
);
691 void *data
= malloc(total_size
);
695 enum isl_aux_state
**per_level_arr
= data
;
696 enum isl_aux_state
*s
= data
+ per_level_array_size
;
697 for (uint32_t level
= 0; level
< levels
; level
++) {
698 per_level_arr
[level
] = s
;
699 for (uint32_t a
= 0; a
< mt
->level
[level
].depth
; a
++)
702 assert((void *)s
== data
+ total_size
);
704 return per_level_arr
;
708 free_aux_state_map(enum isl_aux_state
**state
)
713 static struct intel_mipmap_tree
*
714 make_surface(struct brw_context
*brw
, GLenum target
, mesa_format format
,
715 unsigned first_level
, unsigned last_level
,
716 unsigned width0
, unsigned height0
, unsigned depth0
,
717 unsigned num_samples
, enum isl_tiling isl_tiling
,
718 isl_surf_usage_flags_t isl_usage_flags
, uint32_t alloc_flags
,
721 struct intel_mipmap_tree
*mt
= calloc(sizeof(*mt
), 1);
725 if (!create_mapping_table(target
, first_level
, last_level
, depth0
,
731 if (target
== GL_TEXTURE_CUBE_MAP
||
732 target
== GL_TEXTURE_CUBE_MAP_ARRAY
)
733 isl_usage_flags
|= ISL_SURF_USAGE_CUBE_BIT
;
735 DBG("%s: %s %s %ux %u:%u:%u %d..%d <-- %p\n",
737 _mesa_enum_to_string(target
),
738 _mesa_get_format_name(format
),
739 num_samples
, width0
, height0
, depth0
,
740 first_level
, last_level
, mt
);
742 struct isl_surf_init_info init_info
= {
743 .dim
= get_isl_surf_dim(target
),
744 .format
= translate_tex_format(brw
, format
, false),
747 .depth
= target
== GL_TEXTURE_3D
? depth0
: 1,
748 .levels
= last_level
- first_level
+ 1,
749 .array_len
= target
== GL_TEXTURE_3D
? 1 : depth0
,
750 .samples
= MAX2(num_samples
, 1),
751 .usage
= isl_usage_flags
,
752 .tiling_flags
= 1u << isl_tiling
755 if (!isl_surf_init_s(&brw
->isl_dev
, &mt
->surf
, &init_info
))
758 assert(mt
->surf
.size
% mt
->surf
.row_pitch
== 0);
761 mt
->bo
= brw_bo_alloc_tiled(brw
->bufmgr
, "isl-miptree",
763 isl_tiling_to_bufmgr_tiling(isl_tiling
),
764 mt
->surf
.row_pitch
, alloc_flags
);
771 mt
->first_level
= first_level
;
772 mt
->last_level
= last_level
;
776 mt
->aux_state
= NULL
;
781 intel_miptree_release(&mt
);
785 static struct intel_mipmap_tree
*
786 miptree_create(struct brw_context
*brw
,
795 uint32_t layout_flags
)
797 if (brw
->gen
== 6 && format
== MESA_FORMAT_S_UINT8
)
798 return make_surface(brw
, target
, format
, first_level
, last_level
,
799 width0
, height0
, depth0
, num_samples
, ISL_TILING_W
,
800 ISL_SURF_USAGE_STENCIL_BIT
|
801 ISL_SURF_USAGE_TEXTURE_BIT
,
802 BO_ALLOC_FOR_RENDER
, NULL
);
804 struct intel_mipmap_tree
*mt
;
805 mesa_format tex_format
= format
;
806 mesa_format etc_format
= MESA_FORMAT_NONE
;
807 uint32_t alloc_flags
= 0;
809 format
= intel_lower_compressed_format(brw
, format
);
811 etc_format
= (format
!= tex_format
) ? tex_format
: MESA_FORMAT_NONE
;
813 assert((layout_flags
& MIPTREE_LAYOUT_FOR_BO
) == 0);
814 mt
= intel_miptree_create_layout(brw
, target
, format
,
815 first_level
, last_level
, width0
,
816 height0
, depth0
, num_samples
,
821 if (mt
->tiling
== (I915_TILING_Y
| I915_TILING_X
))
822 mt
->tiling
= I915_TILING_Y
;
824 if (layout_flags
& MIPTREE_LAYOUT_ACCELERATED_UPLOAD
)
825 alloc_flags
|= BO_ALLOC_FOR_RENDER
;
827 mt
->etc_format
= etc_format
;
829 if (format
== MESA_FORMAT_S_UINT8
) {
830 /* Align to size of W tile, 64x64. */
831 mt
->bo
= brw_bo_alloc_tiled_2d(brw
->bufmgr
, "miptree",
832 ALIGN(mt
->total_width
, 64),
833 ALIGN(mt
->total_height
, 64),
834 mt
->cpp
, mt
->tiling
, &mt
->pitch
,
837 mt
->bo
= brw_bo_alloc_tiled_2d(brw
->bufmgr
, "miptree",
838 mt
->total_width
, mt
->total_height
,
839 mt
->cpp
, mt
->tiling
, &mt
->pitch
,
843 if (layout_flags
& MIPTREE_LAYOUT_FOR_SCANOUT
)
844 mt
->bo
->cache_coherent
= false;
846 if (!(layout_flags
& MIPTREE_LAYOUT_DISABLE_AUX
))
847 intel_miptree_choose_aux_usage(brw
, mt
);
852 struct intel_mipmap_tree
*
853 intel_miptree_create(struct brw_context
*brw
,
862 uint32_t layout_flags
)
864 struct intel_mipmap_tree
*mt
= miptree_create(
866 first_level
, last_level
,
867 width0
, height0
, depth0
, num_samples
,
870 /* If the BO is too large to fit in the aperture, we need to use the
871 * BLT engine to support it. Prior to Sandybridge, the BLT paths can't
872 * handle Y-tiling, so we need to fall back to X.
874 if (brw
->gen
< 6 && mt
->bo
->size
>= brw
->max_gtt_map_object_size
&&
875 mt
->tiling
== I915_TILING_Y
) {
876 const uint32_t alloc_flags
=
877 (layout_flags
& MIPTREE_LAYOUT_ACCELERATED_UPLOAD
) ?
878 BO_ALLOC_FOR_RENDER
: 0;
879 perf_debug("%dx%d miptree larger than aperture; falling back to X-tiled\n",
880 mt
->total_width
, mt
->total_height
);
882 mt
->tiling
= I915_TILING_X
;
883 brw_bo_unreference(mt
->bo
);
884 mt
->bo
= brw_bo_alloc_tiled_2d(brw
->bufmgr
, "miptree",
885 mt
->total_width
, mt
->total_height
, mt
->cpp
,
886 mt
->tiling
, &mt
->pitch
, alloc_flags
);
892 intel_miptree_release(&mt
);
897 if (mt
->msaa_layout
== INTEL_MSAA_LAYOUT_CMS
) {
898 assert(mt
->num_samples
> 1);
899 if (!intel_miptree_alloc_mcs(brw
, mt
, num_samples
)) {
900 intel_miptree_release(&mt
);
905 /* Since CCS_E can compress more than just clear color, we create the CCS
906 * for it up-front. For CCS_D which only compresses clears, we create the
907 * CCS on-demand when a clear occurs that wants one.
909 if (mt
->aux_usage
== ISL_AUX_USAGE_CCS_E
) {
910 if (!intel_miptree_alloc_ccs(brw
, mt
)) {
911 intel_miptree_release(&mt
);
919 struct intel_mipmap_tree
*
920 intel_miptree_create_for_bo(struct brw_context
*brw
,
928 uint32_t layout_flags
)
930 struct intel_mipmap_tree
*mt
;
931 uint32_t tiling
, swizzle
;
932 const GLenum target
= depth
> 1 ? GL_TEXTURE_2D_ARRAY
: GL_TEXTURE_2D
;
934 if (brw
->gen
== 6 && format
== MESA_FORMAT_S_UINT8
) {
935 mt
= make_surface(brw
, target
, MESA_FORMAT_S_UINT8
,
936 0, 0, width
, height
, depth
, 1, ISL_TILING_W
,
937 ISL_SURF_USAGE_STENCIL_BIT
|
938 ISL_SURF_USAGE_TEXTURE_BIT
,
939 BO_ALLOC_FOR_RENDER
, bo
);
943 assert(bo
->size
>= mt
->surf
.size
);
945 brw_bo_reference(bo
);
949 brw_bo_get_tiling(bo
, &tiling
, &swizzle
);
951 /* Nothing will be able to use this miptree with the BO if the offset isn't
954 if (tiling
!= I915_TILING_NONE
)
955 assert(offset
% 4096 == 0);
957 /* miptrees can't handle negative pitch. If you need flipping of images,
958 * that's outside of the scope of the mt.
962 /* The BO already has a tiling format and we shouldn't confuse the lower
963 * layers by making it try to find a tiling format again.
965 assert((layout_flags
& MIPTREE_LAYOUT_TILING_ANY
) == 0);
966 assert((layout_flags
& MIPTREE_LAYOUT_TILING_NONE
) == 0);
968 layout_flags
|= MIPTREE_LAYOUT_FOR_BO
;
969 mt
= intel_miptree_create_layout(brw
, target
, format
,
971 width
, height
, depth
, 0,
976 brw_bo_reference(bo
);
982 if (!(layout_flags
& MIPTREE_LAYOUT_DISABLE_AUX
)) {
983 intel_miptree_choose_aux_usage(brw
, mt
);
985 /* Since CCS_E can compress more than just clear color, we create the
986 * CCS for it up-front. For CCS_D which only compresses clears, we
987 * create the CCS on-demand when a clear occurs that wants one.
989 if (mt
->aux_usage
== ISL_AUX_USAGE_CCS_E
) {
990 if (!intel_miptree_alloc_ccs(brw
, mt
)) {
991 intel_miptree_release(&mt
);
1000 static struct intel_mipmap_tree
*
1001 miptree_create_for_planar_image(struct brw_context
*brw
,
1002 __DRIimage
*image
, GLenum target
)
1004 struct intel_image_format
*f
= image
->planar_format
;
1005 struct intel_mipmap_tree
*planar_mt
;
1007 for (int i
= 0; i
< f
->nplanes
; i
++) {
1008 const int index
= f
->planes
[i
].buffer_index
;
1009 const uint32_t dri_format
= f
->planes
[i
].dri_format
;
1010 const mesa_format format
= driImageFormatToGLFormat(dri_format
);
1011 const uint32_t width
= image
->width
>> f
->planes
[i
].width_shift
;
1012 const uint32_t height
= image
->height
>> f
->planes
[i
].height_shift
;
1014 /* Disable creation of the texture's aux buffers because the driver
1015 * exposes no EGL API to manage them. That is, there is no API for
1016 * resolving the aux buffer's content to the main buffer nor for
1017 * invalidating the aux buffer's content.
1019 struct intel_mipmap_tree
*mt
=
1020 intel_miptree_create_for_bo(brw
, image
->bo
, format
,
1021 image
->offsets
[index
],
1023 image
->strides
[index
],
1024 MIPTREE_LAYOUT_DISABLE_AUX
);
1028 mt
->target
= target
;
1029 mt
->total_width
= width
;
1030 mt
->total_height
= height
;
1035 planar_mt
->plane
[i
- 1] = mt
;
1041 struct intel_mipmap_tree
*
1042 intel_miptree_create_for_dri_image(struct brw_context
*brw
,
1043 __DRIimage
*image
, GLenum target
)
1045 if (image
->planar_format
&& image
->planar_format
->nplanes
> 0)
1046 return miptree_create_for_planar_image(brw
, image
, target
);
1048 mesa_format format
= image
->format
;
1050 if (!brw
->ctx
.TextureFormatSupported
[format
]) {
1051 /* The texture storage paths in core Mesa detect if the driver does not
1052 * support the user-requested format, and then searches for a
1053 * fallback format. The DRIimage code bypasses core Mesa, though. So we
1054 * do the fallbacks here for important formats.
1056 * We must support DRM_FOURCC_XBGR8888 textures because the Android
1057 * framework produces HAL_PIXEL_FORMAT_RGBX8888 winsys surfaces, which
1058 * the Chrome OS compositor consumes as dma_buf EGLImages.
1060 format
= _mesa_format_fallback_rgbx_to_rgba(format
);
1063 if (!brw
->ctx
.TextureFormatSupported
[format
])
1066 /* Disable creation of the texture's aux buffers because the driver exposes
1067 * no EGL API to manage them. That is, there is no API for resolving the aux
1068 * buffer's content to the main buffer nor for invalidating the aux buffer's
1071 struct intel_mipmap_tree
*mt
=
1072 intel_miptree_create_for_bo(brw
, image
->bo
, format
,
1073 image
->offset
, image
->width
, image
->height
, 1,
1075 MIPTREE_LAYOUT_DISABLE_AUX
);
1079 mt
->target
= target
;
1080 mt
->level
[0].level_x
= image
->tile_x
;
1081 mt
->level
[0].level_y
= image
->tile_y
;
1082 mt
->level
[0].slice
[0].x_offset
= image
->tile_x
;
1083 mt
->level
[0].slice
[0].y_offset
= image
->tile_y
;
1084 mt
->total_width
+= image
->tile_x
;
1085 mt
->total_height
+= image
->tile_y
;
1087 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
1088 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
1089 * trouble resolving back to destination image due to alignment issues.
1091 if (!brw
->has_surface_tile_offset
) {
1092 uint32_t draw_x
, draw_y
;
1093 intel_miptree_get_tile_offsets(mt
, 0, 0, &draw_x
, &draw_y
);
1095 if (draw_x
!= 0 || draw_y
!= 0) {
1096 _mesa_error(&brw
->ctx
, GL_INVALID_OPERATION
, __func__
);
1097 intel_miptree_release(&mt
);
1106 * For a singlesample renderbuffer, this simply wraps the given BO with a
1109 * For a multisample renderbuffer, this wraps the window system's
1110 * (singlesample) BO with a singlesample miptree attached to the
1111 * intel_renderbuffer, then creates a multisample miptree attached to irb->mt
1112 * that will contain the actual rendering (which is lazily resolved to
1113 * irb->singlesample_mt).
1116 intel_update_winsys_renderbuffer_miptree(struct brw_context
*intel
,
1117 struct intel_renderbuffer
*irb
,
1119 uint32_t width
, uint32_t height
,
1122 struct intel_mipmap_tree
*singlesample_mt
= NULL
;
1123 struct intel_mipmap_tree
*multisample_mt
= NULL
;
1124 struct gl_renderbuffer
*rb
= &irb
->Base
.Base
;
1125 mesa_format format
= rb
->Format
;
1126 int num_samples
= rb
->NumSamples
;
1128 /* Only the front and back buffers, which are color buffers, are allocated
1129 * through the image loader.
1131 assert(_mesa_get_format_base_format(format
) == GL_RGB
||
1132 _mesa_get_format_base_format(format
) == GL_RGBA
);
1134 singlesample_mt
= intel_miptree_create_for_bo(intel
,
1142 MIPTREE_LAYOUT_FOR_SCANOUT
);
1143 if (!singlesample_mt
)
1146 if (num_samples
== 0) {
1147 intel_miptree_release(&irb
->mt
);
1148 irb
->mt
= singlesample_mt
;
1150 assert(!irb
->singlesample_mt
);
1152 intel_miptree_release(&irb
->singlesample_mt
);
1153 irb
->singlesample_mt
= singlesample_mt
;
1156 irb
->mt
->logical_width0
!= width
||
1157 irb
->mt
->logical_height0
!= height
) {
1158 multisample_mt
= intel_miptree_create_for_renderbuffer(intel
,
1163 if (!multisample_mt
)
1166 irb
->need_downsample
= false;
1167 intel_miptree_release(&irb
->mt
);
1168 irb
->mt
= multisample_mt
;
1174 intel_miptree_release(&irb
->singlesample_mt
);
1175 intel_miptree_release(&irb
->mt
);
1179 struct intel_mipmap_tree
*
1180 intel_miptree_create_for_renderbuffer(struct brw_context
*brw
,
1184 uint32_t num_samples
)
1186 struct intel_mipmap_tree
*mt
;
1189 GLenum target
= num_samples
> 1 ? GL_TEXTURE_2D_MULTISAMPLE
: GL_TEXTURE_2D
;
1190 const uint32_t layout_flags
= MIPTREE_LAYOUT_ACCELERATED_UPLOAD
|
1191 MIPTREE_LAYOUT_TILING_ANY
|
1192 MIPTREE_LAYOUT_FOR_SCANOUT
;
1194 mt
= intel_miptree_create(brw
, target
, format
, 0, 0,
1195 width
, height
, depth
, num_samples
,
1200 if (mt
->aux_usage
== ISL_AUX_USAGE_HIZ
) {
1201 ok
= intel_miptree_alloc_hiz(brw
, mt
);
1209 intel_miptree_release(&mt
);
1214 intel_miptree_reference(struct intel_mipmap_tree
**dst
,
1215 struct intel_mipmap_tree
*src
)
1220 intel_miptree_release(dst
);
1224 DBG("%s %p refcount now %d\n", __func__
, src
, src
->refcount
);
1231 intel_miptree_aux_buffer_free(struct intel_miptree_aux_buffer
*aux_buf
)
1233 if (aux_buf
== NULL
)
1236 brw_bo_unreference(aux_buf
->bo
);
1242 intel_miptree_release(struct intel_mipmap_tree
**mt
)
1247 DBG("%s %p refcount will be %d\n", __func__
, *mt
, (*mt
)->refcount
- 1);
1248 if (--(*mt
)->refcount
<= 0) {
1251 DBG("%s deleting %p\n", __func__
, *mt
);
1253 brw_bo_unreference((*mt
)->bo
);
1254 intel_miptree_release(&(*mt
)->stencil_mt
);
1255 intel_miptree_release(&(*mt
)->r8stencil_mt
);
1256 intel_miptree_aux_buffer_free((*mt
)->hiz_buf
);
1257 intel_miptree_aux_buffer_free((*mt
)->mcs_buf
);
1258 free_aux_state_map((*mt
)->aux_state
);
1260 intel_miptree_release(&(*mt
)->plane
[0]);
1261 intel_miptree_release(&(*mt
)->plane
[1]);
1263 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
1264 free((*mt
)->level
[i
].slice
);
1274 intel_get_image_dims(struct gl_texture_image
*image
,
1275 int *width
, int *height
, int *depth
)
1277 switch (image
->TexObject
->Target
) {
1278 case GL_TEXTURE_1D_ARRAY
:
1279 /* For a 1D Array texture the OpenGL API will treat the image height as
1280 * the number of array slices. For Intel hardware, we treat the 1D array
1281 * as a 2D Array with a height of 1. So, here we want to swap image
1284 assert(image
->Depth
== 1);
1285 *width
= image
->Width
;
1287 *depth
= image
->Height
;
1289 case GL_TEXTURE_CUBE_MAP
:
1290 /* For Cube maps, the mesa/main api layer gives us a depth of 1 even
1291 * though we really have 6 slices.
1293 assert(image
->Depth
== 1);
1294 *width
= image
->Width
;
1295 *height
= image
->Height
;
1299 *width
= image
->Width
;
1300 *height
= image
->Height
;
1301 *depth
= image
->Depth
;
1307 * Can the image be pulled into a unified mipmap tree? This mirrors
1308 * the completeness test in a lot of ways.
1310 * Not sure whether I want to pass gl_texture_image here.
1313 intel_miptree_match_image(struct intel_mipmap_tree
*mt
,
1314 struct gl_texture_image
*image
)
1316 struct intel_texture_image
*intelImage
= intel_texture_image(image
);
1317 GLuint level
= intelImage
->base
.Base
.Level
;
1318 int width
, height
, depth
;
1320 /* glTexImage* choose the texture object based on the target passed in, and
1321 * objects can't change targets over their lifetimes, so this should be
1324 assert(image
->TexObject
->Target
== mt
->target
);
1326 mesa_format mt_format
= mt
->format
;
1327 if (mt
->format
== MESA_FORMAT_Z24_UNORM_X8_UINT
&& mt
->stencil_mt
)
1328 mt_format
= MESA_FORMAT_Z24_UNORM_S8_UINT
;
1329 if (mt
->format
== MESA_FORMAT_Z_FLOAT32
&& mt
->stencil_mt
)
1330 mt_format
= MESA_FORMAT_Z32_FLOAT_S8X24_UINT
;
1331 if (mt
->etc_format
!= MESA_FORMAT_NONE
)
1332 mt_format
= mt
->etc_format
;
1334 if (image
->TexFormat
!= mt_format
)
1337 intel_get_image_dims(image
, &width
, &height
, &depth
);
1339 if (mt
->target
== GL_TEXTURE_CUBE_MAP
)
1342 if (mt
->surf
.size
> 0) {
1343 if (level
>= mt
->surf
.levels
)
1346 const unsigned level_depth
=
1347 mt
->surf
.dim
== ISL_SURF_DIM_3D
?
1348 minify(mt
->surf
.logical_level0_px
.depth
, level
) :
1349 mt
->surf
.logical_level0_px
.array_len
;
1351 return width
== minify(mt
->surf
.logical_level0_px
.width
, level
) &&
1352 height
== minify(mt
->surf
.logical_level0_px
.height
, level
) &&
1353 depth
== level_depth
&&
1354 MAX2(image
->NumSamples
, 1) == mt
->surf
.samples
;
1357 int level_depth
= mt
->level
[level
].depth
;
1358 if (mt
->num_samples
> 1) {
1359 switch (mt
->msaa_layout
) {
1360 case INTEL_MSAA_LAYOUT_NONE
:
1361 case INTEL_MSAA_LAYOUT_IMS
:
1363 case INTEL_MSAA_LAYOUT_UMS
:
1364 case INTEL_MSAA_LAYOUT_CMS
:
1365 level_depth
/= mt
->num_samples
;
1370 /* Test image dimensions against the base level image adjusted for
1371 * minification. This will also catch images not present in the
1372 * tree, changed targets, etc.
1374 if (width
!= minify(mt
->logical_width0
, level
- mt
->first_level
) ||
1375 height
!= minify(mt
->logical_height0
, level
- mt
->first_level
) ||
1376 depth
!= level_depth
) {
1380 if (image
->NumSamples
!= mt
->num_samples
)
1388 intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
1390 GLuint x
, GLuint y
, GLuint d
)
1392 mt
->level
[level
].depth
= d
;
1393 mt
->level
[level
].level_x
= x
;
1394 mt
->level
[level
].level_y
= y
;
1396 DBG("%s level %d, depth %d, offset %d,%d\n", __func__
,
1399 assert(mt
->level
[level
].slice
);
1401 mt
->level
[level
].slice
[0].x_offset
= mt
->level
[level
].level_x
;
1402 mt
->level
[level
].slice
[0].y_offset
= mt
->level
[level
].level_y
;
1407 intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
1408 GLuint level
, GLuint img
,
1411 if (img
== 0 && level
== 0)
1412 assert(x
== 0 && y
== 0);
1414 assert(img
< mt
->level
[level
].depth
);
1416 mt
->level
[level
].slice
[img
].x_offset
= mt
->level
[level
].level_x
+ x
;
1417 mt
->level
[level
].slice
[img
].y_offset
= mt
->level
[level
].level_y
+ y
;
1419 DBG("%s level %d img %d pos %d,%d\n",
1420 __func__
, level
, img
,
1421 mt
->level
[level
].slice
[img
].x_offset
,
1422 mt
->level
[level
].slice
[img
].y_offset
);
1426 intel_miptree_get_image_offset(const struct intel_mipmap_tree
*mt
,
1427 GLuint level
, GLuint slice
,
1428 GLuint
*x
, GLuint
*y
)
1430 if (mt
->surf
.size
> 0) {
1431 uint32_t x_offset_sa
, y_offset_sa
;
1433 /* Given level is relative to level zero while the miptree may be
1434 * represent just a subset of all levels starting from 'first_level'.
1436 assert(level
>= mt
->first_level
);
1437 level
-= mt
->first_level
;
1439 const unsigned z
= mt
->surf
.dim
== ISL_SURF_DIM_3D
? slice
: 0;
1440 slice
= mt
->surf
.dim
== ISL_SURF_DIM_3D
? 0 : slice
;
1441 isl_surf_get_image_offset_sa(&mt
->surf
, level
, slice
, z
,
1442 &x_offset_sa
, &y_offset_sa
);
1449 assert(slice
< mt
->level
[level
].depth
);
1451 *x
= mt
->level
[level
].slice
[slice
].x_offset
;
1452 *y
= mt
->level
[level
].slice
[slice
].y_offset
;
1457 * This function computes the tile_w (in bytes) and tile_h (in rows) of
1458 * different tiling patterns. If the BO is untiled, tile_w is set to cpp
1459 * and tile_h is set to 1.
1462 intel_get_tile_dims(uint32_t tiling
, uint32_t cpp
,
1463 uint32_t *tile_w
, uint32_t *tile_h
)
1474 case I915_TILING_NONE
:
1479 unreachable("not reached");
1485 * This function computes masks that may be used to select the bits of the X
1486 * and Y coordinates that indicate the offset within a tile. If the BO is
1487 * untiled, the masks are set to 0.
1490 intel_get_tile_masks(uint32_t tiling
, uint32_t cpp
,
1491 uint32_t *mask_x
, uint32_t *mask_y
)
1493 uint32_t tile_w_bytes
, tile_h
;
1495 intel_get_tile_dims(tiling
, cpp
, &tile_w_bytes
, &tile_h
);
1497 *mask_x
= tile_w_bytes
/ cpp
- 1;
1498 *mask_y
= tile_h
- 1;
1502 * Compute the offset (in bytes) from the start of the BO to the given x
1503 * and y coordinate. For tiled BOs, caller must ensure that x and y are
1504 * multiples of the tile size.
1507 intel_miptree_get_aligned_offset(const struct intel_mipmap_tree
*mt
,
1508 uint32_t x
, uint32_t y
)
1511 uint32_t pitch
= mt
->pitch
;
1512 uint32_t tiling
= mt
->tiling
;
1516 unreachable("not reached");
1517 case I915_TILING_NONE
:
1518 return y
* pitch
+ x
* cpp
;
1520 assert((x
% (512 / cpp
)) == 0);
1521 assert((y
% 8) == 0);
1522 return y
* pitch
+ x
/ (512 / cpp
) * 4096;
1524 assert((x
% (128 / cpp
)) == 0);
1525 assert((y
% 32) == 0);
1526 return y
* pitch
+ x
/ (128 / cpp
) * 4096;
1531 * Rendering with tiled buffers requires that the base address of the buffer
1532 * be aligned to a page boundary. For renderbuffers, and sometimes with
1533 * textures, we may want the surface to point at a texture image level that
1534 * isn't at a page boundary.
1536 * This function returns an appropriately-aligned base offset
1537 * according to the tiling restrictions, plus any required x/y offset
1541 intel_miptree_get_tile_offsets(const struct intel_mipmap_tree
*mt
,
1542 GLuint level
, GLuint slice
,
1547 uint32_t mask_x
, mask_y
;
1549 intel_get_tile_masks(mt
->tiling
, mt
->cpp
, &mask_x
, &mask_y
);
1550 intel_miptree_get_image_offset(mt
, level
, slice
, &x
, &y
);
1552 *tile_x
= x
& mask_x
;
1553 *tile_y
= y
& mask_y
;
1555 return intel_miptree_get_aligned_offset(mt
, x
& ~mask_x
, y
& ~mask_y
);
1559 intel_miptree_copy_slice_sw(struct brw_context
*brw
,
1560 struct intel_mipmap_tree
*src_mt
,
1561 unsigned src_level
, unsigned src_layer
,
1562 struct intel_mipmap_tree
*dst_mt
,
1563 unsigned dst_level
, unsigned dst_layer
,
1564 unsigned width
, unsigned height
)
1567 ptrdiff_t src_stride
, dst_stride
;
1568 const unsigned cpp
= dst_mt
->surf
.size
> 0 ?
1569 (isl_format_get_layout(dst_mt
->surf
.format
)->bpb
/ 8) : dst_mt
->cpp
;
1571 intel_miptree_map(brw
, src_mt
,
1572 src_level
, src_layer
,
1575 GL_MAP_READ_BIT
| BRW_MAP_DIRECT_BIT
,
1578 intel_miptree_map(brw
, dst_mt
,
1579 dst_level
, dst_layer
,
1582 GL_MAP_WRITE_BIT
| GL_MAP_INVALIDATE_RANGE_BIT
|
1586 DBG("sw blit %s mt %p %p/%"PRIdPTR
" -> %s mt %p %p/%"PRIdPTR
" (%dx%d)\n",
1587 _mesa_get_format_name(src_mt
->format
),
1588 src_mt
, src
, src_stride
,
1589 _mesa_get_format_name(dst_mt
->format
),
1590 dst_mt
, dst
, dst_stride
,
1593 int row_size
= cpp
* width
;
1594 if (src_stride
== row_size
&&
1595 dst_stride
== row_size
) {
1596 memcpy(dst
, src
, row_size
* height
);
1598 for (int i
= 0; i
< height
; i
++) {
1599 memcpy(dst
, src
, row_size
);
1605 intel_miptree_unmap(brw
, dst_mt
, dst_level
, dst_layer
);
1606 intel_miptree_unmap(brw
, src_mt
, src_level
, src_layer
);
1608 /* Don't forget to copy the stencil data over, too. We could have skipped
1609 * passing BRW_MAP_DIRECT_BIT, but that would have meant intel_miptree_map
1610 * shuffling the two data sources in/out of temporary storage instead of
1611 * the direct mapping we get this way.
1613 if (dst_mt
->stencil_mt
) {
1614 assert(src_mt
->stencil_mt
);
1615 intel_miptree_copy_slice_sw(brw
,
1616 src_mt
->stencil_mt
, src_level
, src_layer
,
1617 dst_mt
->stencil_mt
, dst_level
, dst_layer
,
1623 intel_miptree_copy_slice(struct brw_context
*brw
,
1624 struct intel_mipmap_tree
*src_mt
,
1625 unsigned src_level
, unsigned src_layer
,
1626 struct intel_mipmap_tree
*dst_mt
,
1627 unsigned dst_level
, unsigned dst_layer
)
1630 mesa_format format
= src_mt
->format
;
1631 uint32_t width
, height
;
1633 if (src_mt
->surf
.size
> 0) {
1634 width
= minify(src_mt
->surf
.phys_level0_sa
.width
,
1635 src_level
- src_mt
->first_level
);
1636 height
= minify(src_mt
->surf
.phys_level0_sa
.height
,
1637 src_level
- src_mt
->first_level
);
1639 if (src_mt
->surf
.dim
== ISL_SURF_DIM_3D
)
1640 assert(src_layer
< minify(src_mt
->surf
.phys_level0_sa
.depth
,
1641 src_level
- src_mt
->first_level
));
1643 assert(src_layer
< src_mt
->surf
.phys_level0_sa
.array_len
);
1645 width
= minify(src_mt
->physical_width0
,
1646 src_level
- src_mt
->first_level
);
1647 height
= minify(src_mt
->physical_height0
,
1648 src_level
- src_mt
->first_level
);
1649 assert(src_layer
< src_mt
->level
[src_level
].depth
);
1652 assert(src_mt
->format
== dst_mt
->format
);
1654 if (dst_mt
->compressed
) {
1656 _mesa_get_format_block_size(dst_mt
->format
, &i
, &j
);
1657 height
= ALIGN_NPOT(height
, j
) / j
;
1658 width
= ALIGN_NPOT(width
, i
) / i
;
1661 /* If it's a packed depth/stencil buffer with separate stencil, the blit
1662 * below won't apply since we can't do the depth's Y tiling or the
1663 * stencil's W tiling in the blitter.
1665 if (src_mt
->stencil_mt
) {
1666 intel_miptree_copy_slice_sw(brw
,
1667 src_mt
, src_level
, src_layer
,
1668 dst_mt
, dst_level
, dst_layer
,
1673 uint32_t dst_x
, dst_y
, src_x
, src_y
;
1674 intel_miptree_get_image_offset(dst_mt
, dst_level
, dst_layer
,
1676 intel_miptree_get_image_offset(src_mt
, src_level
, src_layer
,
1679 DBG("validate blit mt %s %p %d,%d/%d -> mt %s %p %d,%d/%d (%dx%d)\n",
1680 _mesa_get_format_name(src_mt
->format
),
1681 src_mt
, src_x
, src_y
, src_mt
->pitch
,
1682 _mesa_get_format_name(dst_mt
->format
),
1683 dst_mt
, dst_x
, dst_y
, dst_mt
->pitch
,
1686 if (!intel_miptree_blit(brw
,
1687 src_mt
, src_level
, src_layer
, 0, 0, false,
1688 dst_mt
, dst_level
, dst_layer
, 0, 0, false,
1689 width
, height
, GL_COPY
)) {
1690 perf_debug("miptree validate blit for %s failed\n",
1691 _mesa_get_format_name(format
));
1693 intel_miptree_copy_slice_sw(brw
,
1694 src_mt
, src_level
, src_layer
,
1695 dst_mt
, dst_level
, dst_layer
,
1701 * Copies the image's current data to the given miptree, and associates that
1702 * miptree with the image.
1704 * If \c invalidate is true, then the actual image data does not need to be
1705 * copied, but the image still needs to be associated to the new miptree (this
1706 * is set to true if we're about to clear the image).
1709 intel_miptree_copy_teximage(struct brw_context
*brw
,
1710 struct intel_texture_image
*intelImage
,
1711 struct intel_mipmap_tree
*dst_mt
,
1714 struct intel_mipmap_tree
*src_mt
= intelImage
->mt
;
1715 struct intel_texture_object
*intel_obj
=
1716 intel_texture_object(intelImage
->base
.Base
.TexObject
);
1717 int level
= intelImage
->base
.Base
.Level
;
1718 const unsigned face
= intelImage
->base
.Base
.Face
;
1719 unsigned start_layer
, end_layer
;
1721 if (intel_obj
->base
.Target
== GL_TEXTURE_1D_ARRAY
) {
1723 assert(intelImage
->base
.Base
.Height
);
1725 end_layer
= intelImage
->base
.Base
.Height
- 1;
1726 } else if (face
> 0) {
1730 assert(intelImage
->base
.Base
.Depth
);
1732 end_layer
= intelImage
->base
.Base
.Depth
- 1;
1736 for (unsigned i
= start_layer
; i
<= end_layer
; i
++) {
1737 intel_miptree_copy_slice(brw
,
1743 intel_miptree_reference(&intelImage
->mt
, dst_mt
);
1744 intel_obj
->needs_validate
= true;
1748 intel_miptree_init_mcs(struct brw_context
*brw
,
1749 struct intel_mipmap_tree
*mt
,
1752 assert(mt
->mcs_buf
!= NULL
);
1754 /* From the Ivy Bridge PRM, Vol 2 Part 1 p326:
1756 * When MCS buffer is enabled and bound to MSRT, it is required that it
1757 * is cleared prior to any rendering.
1759 * Since we don't use the MCS buffer for any purpose other than rendering,
1760 * it makes sense to just clear it immediately upon allocation.
1762 * Note: the clear value for MCS buffers is all 1's, so we memset to 0xff.
1764 void *map
= brw_bo_map(brw
, mt
->mcs_buf
->bo
, MAP_WRITE
);
1765 if (unlikely(map
== NULL
)) {
1766 fprintf(stderr
, "Failed to map mcs buffer into GTT\n");
1767 brw_bo_unreference(mt
->mcs_buf
->bo
);
1772 memset(data
, init_value
, mt
->mcs_buf
->size
);
1773 brw_bo_unmap(mt
->mcs_buf
->bo
);
1776 static struct intel_miptree_aux_buffer
*
1777 intel_alloc_aux_buffer(struct brw_context
*brw
,
1779 const struct isl_surf
*aux_surf
,
1780 uint32_t alloc_flags
,
1781 struct intel_mipmap_tree
*mt
)
1783 struct intel_miptree_aux_buffer
*buf
= calloc(sizeof(*buf
), 1);
1787 buf
->size
= aux_surf
->size
;
1788 buf
->pitch
= aux_surf
->row_pitch
;
1789 buf
->qpitch
= isl_surf_get_array_pitch_sa_rows(aux_surf
);
1791 /* ISL has stricter set of alignment rules then the drm allocator.
1792 * Therefore one can pass the ISL dimensions in terms of bytes instead of
1793 * trying to recalculate based on different format block sizes.
1795 buf
->bo
= brw_bo_alloc_tiled(brw
->bufmgr
, name
, buf
->size
,
1796 I915_TILING_Y
, buf
->pitch
, alloc_flags
);
1802 buf
->surf
= *aux_surf
;
1808 intel_miptree_alloc_mcs(struct brw_context
*brw
,
1809 struct intel_mipmap_tree
*mt
,
1812 assert(brw
->gen
>= 7); /* MCS only used on Gen7+ */
1813 assert(mt
->mcs_buf
== NULL
);
1814 assert(mt
->aux_usage
== ISL_AUX_USAGE_MCS
);
1816 /* Multisampled miptrees are only supported for single level. */
1817 assert(mt
->first_level
== 0);
1818 enum isl_aux_state
**aux_state
=
1819 create_aux_state_map(mt
, ISL_AUX_STATE_CLEAR
);
1823 struct isl_surf temp_main_surf
;
1824 struct isl_surf temp_mcs_surf
;
1826 /* Create first an ISL presentation for the main color surface and let ISL
1827 * calculate equivalent MCS surface against it.
1829 intel_miptree_get_isl_surf(brw
, mt
, &temp_main_surf
);
1830 MAYBE_UNUSED
bool ok
=
1831 isl_surf_get_mcs_surf(&brw
->isl_dev
, &temp_main_surf
, &temp_mcs_surf
);
1834 /* Buffer needs to be initialised requiring the buffer to be immediately
1835 * mapped to cpu space for writing. Therefore do not use the gpu access
1836 * flag which can cause an unnecessary delay if the backing pages happened
1837 * to be just used by the GPU.
1839 const uint32_t alloc_flags
= 0;
1840 mt
->mcs_buf
= intel_alloc_aux_buffer(brw
, "mcs-miptree",
1841 &temp_mcs_surf
, alloc_flags
, mt
);
1847 mt
->aux_state
= aux_state
;
1849 intel_miptree_init_mcs(brw
, mt
, 0xFF);
1855 intel_miptree_alloc_ccs(struct brw_context
*brw
,
1856 struct intel_mipmap_tree
*mt
)
1858 assert(mt
->mcs_buf
== NULL
);
1859 assert(mt
->aux_usage
== ISL_AUX_USAGE_CCS_E
||
1860 mt
->aux_usage
== ISL_AUX_USAGE_CCS_D
);
1862 struct isl_surf temp_main_surf
;
1863 struct isl_surf temp_ccs_surf
;
1865 /* Create first an ISL presentation for the main color surface and let ISL
1866 * calculate equivalent CCS surface against it.
1868 intel_miptree_get_isl_surf(brw
, mt
, &temp_main_surf
);
1869 if (!isl_surf_get_ccs_surf(&brw
->isl_dev
, &temp_main_surf
, &temp_ccs_surf
))
1872 assert(temp_ccs_surf
.size
&&
1873 (temp_ccs_surf
.size
% temp_ccs_surf
.row_pitch
== 0));
1875 enum isl_aux_state
**aux_state
=
1876 create_aux_state_map(mt
, ISL_AUX_STATE_PASS_THROUGH
);
1880 /* In case of compression mcs buffer needs to be initialised requiring the
1881 * buffer to be immediately mapped to cpu space for writing. Therefore do
1882 * not use the gpu access flag which can cause an unnecessary delay if the
1883 * backing pages happened to be just used by the GPU.
1885 const uint32_t alloc_flags
=
1886 mt
->aux_usage
== ISL_AUX_USAGE_CCS_E
? 0 : BO_ALLOC_FOR_RENDER
;
1887 mt
->mcs_buf
= intel_alloc_aux_buffer(brw
, "ccs-miptree",
1888 &temp_ccs_surf
, alloc_flags
, mt
);
1894 mt
->aux_state
= aux_state
;
1896 /* From Gen9 onwards single-sampled (non-msrt) auxiliary buffers are
1897 * used for lossless compression which requires similar initialisation
1898 * as multi-sample compression.
1900 if (mt
->aux_usage
== ISL_AUX_USAGE_CCS_E
) {
1901 /* Hardware sets the auxiliary buffer to all zeroes when it does full
1902 * resolve. Initialize it accordingly in case the first renderer is
1903 * cpu (or other none compression aware party).
1905 * This is also explicitly stated in the spec (MCS Buffer for Render
1907 * "If Software wants to enable Color Compression without Fast clear,
1908 * Software needs to initialize MCS with zeros."
1910 intel_miptree_init_mcs(brw
, mt
, 0);
1911 mt
->msaa_layout
= INTEL_MSAA_LAYOUT_CMS
;
1918 * Helper for intel_miptree_alloc_hiz() that sets
1919 * \c mt->level[level].has_hiz. Return true if and only if
1920 * \c has_hiz was set.
1923 intel_miptree_level_enable_hiz(struct brw_context
*brw
,
1924 struct intel_mipmap_tree
*mt
,
1927 assert(mt
->hiz_buf
);
1929 if (brw
->gen
>= 8 || brw
->is_haswell
) {
1930 uint32_t width
= minify(mt
->physical_width0
, level
);
1931 uint32_t height
= minify(mt
->physical_height0
, level
);
1933 /* Disable HiZ for LOD > 0 unless the width is 8 aligned
1934 * and the height is 4 aligned. This allows our HiZ support
1935 * to fulfill Haswell restrictions for HiZ ops. For LOD == 0,
1936 * we can grow the width & height to allow the HiZ op to
1937 * force the proper size alignments.
1939 if (level
> 0 && ((width
& 7) || (height
& 3))) {
1940 DBG("mt %p level %d: HiZ DISABLED\n", mt
, level
);
1945 DBG("mt %p level %d: HiZ enabled\n", mt
, level
);
1946 mt
->level
[level
].has_hiz
= true;
1951 intel_miptree_alloc_hiz(struct brw_context
*brw
,
1952 struct intel_mipmap_tree
*mt
)
1954 assert(mt
->hiz_buf
== NULL
);
1955 assert(mt
->aux_usage
== ISL_AUX_USAGE_HIZ
);
1957 enum isl_aux_state
**aux_state
=
1958 create_aux_state_map(mt
, ISL_AUX_STATE_AUX_INVALID
);
1962 struct isl_surf temp_main_surf
;
1963 struct isl_surf temp_hiz_surf
;
1965 intel_miptree_get_isl_surf(brw
, mt
, &temp_main_surf
);
1966 MAYBE_UNUSED
bool ok
=
1967 isl_surf_get_hiz_surf(&brw
->isl_dev
, &temp_main_surf
, &temp_hiz_surf
);
1970 const uint32_t alloc_flags
= BO_ALLOC_FOR_RENDER
;
1971 mt
->hiz_buf
= intel_alloc_aux_buffer(brw
, "hiz-miptree",
1972 &temp_hiz_surf
, alloc_flags
, mt
);
1979 for (unsigned level
= mt
->first_level
; level
<= mt
->last_level
; ++level
)
1980 intel_miptree_level_enable_hiz(brw
, mt
, level
);
1982 mt
->aux_state
= aux_state
;
1988 * Can the miptree sample using the hiz buffer?
1991 intel_miptree_sample_with_hiz(struct brw_context
*brw
,
1992 struct intel_mipmap_tree
*mt
)
1994 /* It's unclear how well supported sampling from the hiz buffer is on GEN8,
1995 * so keep things conservative for now and never enable it unless we're SKL+.
2005 /* It seems the hardware won't fallback to the depth buffer if some of the
2006 * mipmap levels aren't available in the HiZ buffer. So we need all levels
2007 * of the texture to be HiZ enabled.
2009 for (unsigned level
= mt
->first_level
; level
<= mt
->last_level
; ++level
) {
2010 if (!intel_miptree_level_has_hiz(mt
, level
))
2014 /* If compressed multisampling is enabled, then we use it for the auxiliary
2017 * From the BDW PRM (Volume 2d: Command Reference: Structures
2018 * RENDER_SURFACE_STATE.AuxiliarySurfaceMode):
2020 * "If this field is set to AUX_HIZ, Number of Multisamples must be
2021 * MULTISAMPLECOUNT_1, and Surface Type cannot be SURFTYPE_3D.
2023 * There is no such blurb for 1D textures, but there is sufficient evidence
2024 * that this is broken on SKL+.
2026 return (mt
->num_samples
<= 1 &&
2027 mt
->target
!= GL_TEXTURE_3D
&&
2028 mt
->target
!= GL_TEXTURE_1D
/* gen9+ restriction */);
2032 * Does the miptree slice have hiz enabled?
2035 intel_miptree_level_has_hiz(const struct intel_mipmap_tree
*mt
, uint32_t level
)
2037 intel_miptree_check_level_layer(mt
, level
, 0);
2038 return mt
->level
[level
].has_hiz
;
2042 intel_miptree_has_color_unresolved(const struct intel_mipmap_tree
*mt
,
2043 unsigned start_level
, unsigned num_levels
,
2044 unsigned start_layer
, unsigned num_layers
)
2046 assert(_mesa_is_format_color_format(mt
->format
));
2051 /* Clamp the level range to fit the miptree */
2052 assert(start_level
+ num_levels
>= start_level
);
2053 const uint32_t last_level
=
2054 MIN2(mt
->last_level
, start_level
+ num_levels
- 1);
2055 start_level
= MAX2(mt
->first_level
, start_level
);
2056 num_levels
= last_level
- start_level
+ 1;
2058 for (uint32_t level
= start_level
; level
<= last_level
; level
++) {
2059 const uint32_t level_layers
= MIN2(num_layers
, mt
->level
[level
].depth
);
2060 for (unsigned a
= 0; a
< level_layers
; a
++) {
2061 enum isl_aux_state aux_state
=
2062 intel_miptree_get_aux_state(mt
, level
, start_layer
+ a
);
2063 assert(aux_state
!= ISL_AUX_STATE_AUX_INVALID
);
2064 if (aux_state
!= ISL_AUX_STATE_PASS_THROUGH
)
2073 intel_miptree_check_color_resolve(const struct brw_context
*brw
,
2074 const struct intel_mipmap_tree
*mt
,
2075 unsigned level
, unsigned layer
)
2081 /* Fast color clear is supported for mipmapped surfaces only on Gen8+. */
2082 assert(brw
->gen
>= 8 ||
2083 (level
== 0 && mt
->first_level
== 0 && mt
->last_level
== 0));
2085 /* Compression of arrayed msaa surfaces is supported. */
2086 if (mt
->num_samples
> 1)
2089 /* Fast color clear is supported for non-msaa arrays only on Gen8+. */
2090 assert(brw
->gen
>= 8 || (layer
== 0 && mt
->logical_depth0
== 1));
2096 static enum blorp_fast_clear_op
2097 get_ccs_d_resolve_op(enum isl_aux_state aux_state
,
2098 bool ccs_supported
, bool fast_clear_supported
)
2100 assert(ccs_supported
== fast_clear_supported
);
2102 switch (aux_state
) {
2103 case ISL_AUX_STATE_CLEAR
:
2104 case ISL_AUX_STATE_COMPRESSED_CLEAR
:
2106 return BLORP_FAST_CLEAR_OP_RESOLVE_FULL
;
2108 return BLORP_FAST_CLEAR_OP_NONE
;
2110 case ISL_AUX_STATE_PASS_THROUGH
:
2111 return BLORP_FAST_CLEAR_OP_NONE
;
2113 case ISL_AUX_STATE_RESOLVED
:
2114 case ISL_AUX_STATE_AUX_INVALID
:
2115 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR
:
2119 unreachable("Invalid aux state for CCS_D");
2122 static enum blorp_fast_clear_op
2123 get_ccs_e_resolve_op(enum isl_aux_state aux_state
,
2124 bool ccs_supported
, bool fast_clear_supported
)
2126 switch (aux_state
) {
2127 case ISL_AUX_STATE_CLEAR
:
2128 case ISL_AUX_STATE_COMPRESSED_CLEAR
:
2130 return BLORP_FAST_CLEAR_OP_RESOLVE_FULL
;
2131 else if (!fast_clear_supported
)
2132 return BLORP_FAST_CLEAR_OP_RESOLVE_PARTIAL
;
2134 return BLORP_FAST_CLEAR_OP_NONE
;
2136 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR
:
2138 return BLORP_FAST_CLEAR_OP_RESOLVE_FULL
;
2140 return BLORP_FAST_CLEAR_OP_NONE
;
2142 case ISL_AUX_STATE_PASS_THROUGH
:
2143 return BLORP_FAST_CLEAR_OP_NONE
;
2145 case ISL_AUX_STATE_RESOLVED
:
2146 case ISL_AUX_STATE_AUX_INVALID
:
2150 unreachable("Invalid aux state for CCS_E");
2154 intel_miptree_prepare_ccs_access(struct brw_context
*brw
,
2155 struct intel_mipmap_tree
*mt
,
2156 uint32_t level
, uint32_t layer
,
2158 bool fast_clear_supported
)
2160 enum isl_aux_state aux_state
= intel_miptree_get_aux_state(mt
, level
, layer
);
2162 enum blorp_fast_clear_op resolve_op
;
2163 if (intel_miptree_is_lossless_compressed(brw
, mt
)) {
2164 resolve_op
= get_ccs_e_resolve_op(aux_state
, aux_supported
,
2165 fast_clear_supported
);
2167 resolve_op
= get_ccs_d_resolve_op(aux_state
, aux_supported
,
2168 fast_clear_supported
);
2171 if (resolve_op
!= BLORP_FAST_CLEAR_OP_NONE
) {
2172 intel_miptree_check_color_resolve(brw
, mt
, level
, layer
);
2173 brw_blorp_resolve_color(brw
, mt
, level
, layer
, resolve_op
);
2175 switch (resolve_op
) {
2176 case BLORP_FAST_CLEAR_OP_RESOLVE_FULL
:
2177 /* The CCS full resolve operation destroys the CCS and sets it to the
2178 * pass-through state. (You can also think of this as being both a
2179 * resolve and an ambiguate in one operation.)
2181 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2182 ISL_AUX_STATE_PASS_THROUGH
);
2185 case BLORP_FAST_CLEAR_OP_RESOLVE_PARTIAL
:
2186 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2187 ISL_AUX_STATE_COMPRESSED_NO_CLEAR
);
2191 unreachable("Invalid resolve op");
2197 intel_miptree_finish_ccs_write(struct brw_context
*brw
,
2198 struct intel_mipmap_tree
*mt
,
2199 uint32_t level
, uint32_t layer
,
2200 bool written_with_ccs
)
2202 enum isl_aux_state aux_state
= intel_miptree_get_aux_state(mt
, level
, layer
);
2204 if (intel_miptree_is_lossless_compressed(brw
, mt
)) {
2205 switch (aux_state
) {
2206 case ISL_AUX_STATE_CLEAR
:
2207 assert(written_with_ccs
);
2208 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2209 ISL_AUX_STATE_COMPRESSED_CLEAR
);
2212 case ISL_AUX_STATE_COMPRESSED_CLEAR
:
2213 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR
:
2214 assert(written_with_ccs
);
2215 break; /* Nothing to do */
2217 case ISL_AUX_STATE_PASS_THROUGH
:
2218 if (written_with_ccs
) {
2219 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2220 ISL_AUX_STATE_COMPRESSED_NO_CLEAR
);
2226 case ISL_AUX_STATE_RESOLVED
:
2227 case ISL_AUX_STATE_AUX_INVALID
:
2228 unreachable("Invalid aux state for CCS_E");
2231 /* CCS_D is a bit simpler */
2232 switch (aux_state
) {
2233 case ISL_AUX_STATE_CLEAR
:
2234 assert(written_with_ccs
);
2235 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2236 ISL_AUX_STATE_COMPRESSED_CLEAR
);
2239 case ISL_AUX_STATE_COMPRESSED_CLEAR
:
2240 assert(written_with_ccs
);
2241 break; /* Nothing to do */
2243 case ISL_AUX_STATE_PASS_THROUGH
:
2247 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR
:
2248 case ISL_AUX_STATE_RESOLVED
:
2249 case ISL_AUX_STATE_AUX_INVALID
:
2250 unreachable("Invalid aux state for CCS_D");
2256 intel_miptree_finish_mcs_write(struct brw_context
*brw
,
2257 struct intel_mipmap_tree
*mt
,
2258 uint32_t level
, uint32_t layer
,
2259 bool written_with_aux
)
2261 switch (intel_miptree_get_aux_state(mt
, level
, layer
)) {
2262 case ISL_AUX_STATE_CLEAR
:
2263 assert(written_with_aux
);
2264 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2265 ISL_AUX_STATE_COMPRESSED_CLEAR
);
2268 case ISL_AUX_STATE_COMPRESSED_CLEAR
:
2269 assert(written_with_aux
);
2270 break; /* Nothing to do */
2272 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR
:
2273 case ISL_AUX_STATE_RESOLVED
:
2274 case ISL_AUX_STATE_PASS_THROUGH
:
2275 case ISL_AUX_STATE_AUX_INVALID
:
2276 unreachable("Invalid aux state for MCS");
2281 intel_miptree_prepare_hiz_access(struct brw_context
*brw
,
2282 struct intel_mipmap_tree
*mt
,
2283 uint32_t level
, uint32_t layer
,
2284 bool hiz_supported
, bool fast_clear_supported
)
2286 enum blorp_hiz_op hiz_op
= BLORP_HIZ_OP_NONE
;
2287 switch (intel_miptree_get_aux_state(mt
, level
, layer
)) {
2288 case ISL_AUX_STATE_CLEAR
:
2289 case ISL_AUX_STATE_COMPRESSED_CLEAR
:
2290 if (!hiz_supported
|| !fast_clear_supported
)
2291 hiz_op
= BLORP_HIZ_OP_DEPTH_RESOLVE
;
2294 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR
:
2296 hiz_op
= BLORP_HIZ_OP_DEPTH_RESOLVE
;
2299 case ISL_AUX_STATE_PASS_THROUGH
:
2300 case ISL_AUX_STATE_RESOLVED
:
2303 case ISL_AUX_STATE_AUX_INVALID
:
2305 hiz_op
= BLORP_HIZ_OP_HIZ_RESOLVE
;
2309 if (hiz_op
!= BLORP_HIZ_OP_NONE
) {
2310 intel_hiz_exec(brw
, mt
, level
, layer
, 1, hiz_op
);
2313 case BLORP_HIZ_OP_DEPTH_RESOLVE
:
2314 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2315 ISL_AUX_STATE_RESOLVED
);
2318 case BLORP_HIZ_OP_HIZ_RESOLVE
:
2319 /* The HiZ resolve operation is actually an ambiguate */
2320 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2321 ISL_AUX_STATE_PASS_THROUGH
);
2325 unreachable("Invalid HiZ op");
2331 intel_miptree_finish_hiz_write(struct brw_context
*brw
,
2332 struct intel_mipmap_tree
*mt
,
2333 uint32_t level
, uint32_t layer
,
2334 bool written_with_hiz
)
2336 switch (intel_miptree_get_aux_state(mt
, level
, layer
)) {
2337 case ISL_AUX_STATE_CLEAR
:
2338 assert(written_with_hiz
);
2339 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2340 ISL_AUX_STATE_COMPRESSED_CLEAR
);
2343 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR
:
2344 case ISL_AUX_STATE_COMPRESSED_CLEAR
:
2345 assert(written_with_hiz
);
2346 break; /* Nothing to do */
2348 case ISL_AUX_STATE_RESOLVED
:
2349 if (written_with_hiz
) {
2350 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2351 ISL_AUX_STATE_COMPRESSED_NO_CLEAR
);
2353 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2354 ISL_AUX_STATE_AUX_INVALID
);
2358 case ISL_AUX_STATE_PASS_THROUGH
:
2359 if (written_with_hiz
) {
2360 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2361 ISL_AUX_STATE_COMPRESSED_NO_CLEAR
);
2365 case ISL_AUX_STATE_AUX_INVALID
:
2366 assert(!written_with_hiz
);
2371 static inline uint32_t
2372 miptree_level_range_length(const struct intel_mipmap_tree
*mt
,
2373 uint32_t start_level
, uint32_t num_levels
)
2375 assert(start_level
>= mt
->first_level
);
2376 assert(start_level
<= mt
->last_level
);
2378 if (num_levels
== INTEL_REMAINING_LAYERS
)
2379 num_levels
= mt
->last_level
- start_level
+ 1;
2380 /* Check for overflow */
2381 assert(start_level
+ num_levels
>= start_level
);
2382 assert(start_level
+ num_levels
<= mt
->last_level
+ 1);
2387 static inline uint32_t
2388 miptree_layer_range_length(const struct intel_mipmap_tree
*mt
, uint32_t level
,
2389 uint32_t start_layer
, uint32_t num_layers
)
2391 assert(level
<= mt
->last_level
);
2392 uint32_t total_num_layers
;
2394 if (mt
->surf
.size
> 0)
2395 total_num_layers
= mt
->surf
.dim
== ISL_SURF_DIM_3D
?
2396 minify(mt
->surf
.phys_level0_sa
.depth
, level
) :
2397 mt
->surf
.phys_level0_sa
.array_len
;
2399 total_num_layers
= mt
->level
[level
].depth
;
2401 assert(start_layer
< total_num_layers
);
2402 if (num_layers
== INTEL_REMAINING_LAYERS
)
2403 num_layers
= total_num_layers
- start_layer
;
2404 /* Check for overflow */
2405 assert(start_layer
+ num_layers
>= start_layer
);
2406 assert(start_layer
+ num_layers
<= total_num_layers
);
2412 intel_miptree_prepare_access(struct brw_context
*brw
,
2413 struct intel_mipmap_tree
*mt
,
2414 uint32_t start_level
, uint32_t num_levels
,
2415 uint32_t start_layer
, uint32_t num_layers
,
2416 bool aux_supported
, bool fast_clear_supported
)
2418 num_levels
= miptree_level_range_length(mt
, start_level
, num_levels
);
2420 if (_mesa_is_format_color_format(mt
->format
)) {
2424 if (mt
->num_samples
> 1) {
2425 /* Nothing to do for MSAA */
2426 assert(aux_supported
&& fast_clear_supported
);
2428 for (uint32_t l
= 0; l
< num_levels
; l
++) {
2429 const uint32_t level
= start_level
+ l
;
2430 const uint32_t level_layers
=
2431 miptree_layer_range_length(mt
, level
, start_layer
, num_layers
);
2432 for (uint32_t a
= 0; a
< level_layers
; a
++) {
2433 intel_miptree_prepare_ccs_access(brw
, mt
, level
,
2434 start_layer
+ a
, aux_supported
,
2435 fast_clear_supported
);
2439 } else if (mt
->format
== MESA_FORMAT_S_UINT8
) {
2440 /* Nothing to do for stencil */
2445 for (uint32_t l
= 0; l
< num_levels
; l
++) {
2446 const uint32_t level
= start_level
+ l
;
2447 if (!intel_miptree_level_has_hiz(mt
, level
))
2450 const uint32_t level_layers
=
2451 miptree_layer_range_length(mt
, level
, start_layer
, num_layers
);
2452 for (uint32_t a
= 0; a
< level_layers
; a
++) {
2453 intel_miptree_prepare_hiz_access(brw
, mt
, level
, start_layer
+ a
,
2455 fast_clear_supported
);
2462 intel_miptree_finish_write(struct brw_context
*brw
,
2463 struct intel_mipmap_tree
*mt
, uint32_t level
,
2464 uint32_t start_layer
, uint32_t num_layers
,
2465 bool written_with_aux
)
2467 num_layers
= miptree_layer_range_length(mt
, level
, start_layer
, num_layers
);
2469 if (_mesa_is_format_color_format(mt
->format
)) {
2473 if (mt
->num_samples
> 1) {
2474 for (uint32_t a
= 0; a
< num_layers
; a
++) {
2475 intel_miptree_finish_mcs_write(brw
, mt
, level
, start_layer
+ a
,
2479 for (uint32_t a
= 0; a
< num_layers
; a
++) {
2480 intel_miptree_finish_ccs_write(brw
, mt
, level
, start_layer
+ a
,
2484 } else if (mt
->format
== MESA_FORMAT_S_UINT8
) {
2485 /* Nothing to do for stencil */
2487 if (!intel_miptree_level_has_hiz(mt
, level
))
2490 for (uint32_t a
= 0; a
< num_layers
; a
++) {
2491 intel_miptree_finish_hiz_write(brw
, mt
, level
, start_layer
+ a
,
2498 intel_miptree_get_aux_state(const struct intel_mipmap_tree
*mt
,
2499 uint32_t level
, uint32_t layer
)
2501 intel_miptree_check_level_layer(mt
, level
, layer
);
2503 if (_mesa_is_format_color_format(mt
->format
)) {
2504 assert(mt
->mcs_buf
!= NULL
);
2505 assert(mt
->num_samples
<= 1 || mt
->msaa_layout
== INTEL_MSAA_LAYOUT_CMS
);
2506 } else if (mt
->format
== MESA_FORMAT_S_UINT8
) {
2507 unreachable("Cannot get aux state for stencil");
2509 assert(intel_miptree_level_has_hiz(mt
, level
));
2512 return mt
->aux_state
[level
][layer
];
2516 intel_miptree_set_aux_state(struct brw_context
*brw
,
2517 struct intel_mipmap_tree
*mt
, uint32_t level
,
2518 uint32_t start_layer
, uint32_t num_layers
,
2519 enum isl_aux_state aux_state
)
2521 num_layers
= miptree_layer_range_length(mt
, level
, start_layer
, num_layers
);
2523 if (_mesa_is_format_color_format(mt
->format
)) {
2524 assert(mt
->mcs_buf
!= NULL
);
2525 assert(mt
->num_samples
<= 1 || mt
->msaa_layout
== INTEL_MSAA_LAYOUT_CMS
);
2526 } else if (mt
->format
== MESA_FORMAT_S_UINT8
) {
2527 unreachable("Cannot get aux state for stencil");
2529 assert(intel_miptree_level_has_hiz(mt
, level
));
2532 for (unsigned a
= 0; a
< num_layers
; a
++)
2533 mt
->aux_state
[level
][start_layer
+ a
] = aux_state
;
2536 /* On Gen9 color buffers may be compressed by the hardware (lossless
2537 * compression). There are, however, format restrictions and care needs to be
2538 * taken that the sampler engine is capable for re-interpreting a buffer with
2539 * format different the buffer was originally written with.
2541 * For example, SRGB formats are not compressible and the sampler engine isn't
2542 * capable of treating RGBA_UNORM as SRGB_ALPHA. In such a case the underlying
2543 * color buffer needs to be resolved so that the sampling surface can be
2544 * sampled as non-compressed (i.e., without the auxiliary MCS buffer being
2548 can_texture_with_ccs(struct brw_context
*brw
,
2549 struct intel_mipmap_tree
*mt
,
2550 mesa_format view_format
)
2552 if (!intel_miptree_is_lossless_compressed(brw
, mt
))
2555 enum isl_format isl_mt_format
= brw_isl_format_for_mesa_format(mt
->format
);
2556 enum isl_format isl_view_format
= brw_isl_format_for_mesa_format(view_format
);
2558 if (!isl_formats_are_ccs_e_compatible(&brw
->screen
->devinfo
,
2559 isl_mt_format
, isl_view_format
)) {
2560 perf_debug("Incompatible sampling format (%s) for rbc (%s)\n",
2561 _mesa_get_format_name(view_format
),
2562 _mesa_get_format_name(mt
->format
));
2570 intel_miptree_prepare_texture_slices(struct brw_context
*brw
,
2571 struct intel_mipmap_tree
*mt
,
2572 mesa_format view_format
,
2573 uint32_t start_level
, uint32_t num_levels
,
2574 uint32_t start_layer
, uint32_t num_layers
,
2575 bool *aux_supported_out
)
2577 bool aux_supported
, clear_supported
;
2578 if (_mesa_is_format_color_format(mt
->format
)) {
2579 if (mt
->num_samples
> 1) {
2580 aux_supported
= clear_supported
= true;
2582 aux_supported
= can_texture_with_ccs(brw
, mt
, view_format
);
2584 /* Clear color is specified as ints or floats and the conversion is
2585 * done by the sampler. If we have a texture view, we would have to
2586 * perform the clear color conversion manually. Just disable clear
2589 clear_supported
= aux_supported
&& (mt
->format
== view_format
);
2591 } else if (mt
->format
== MESA_FORMAT_S_UINT8
) {
2592 aux_supported
= clear_supported
= false;
2594 aux_supported
= clear_supported
= intel_miptree_sample_with_hiz(brw
, mt
);
2597 intel_miptree_prepare_access(brw
, mt
, start_level
, num_levels
,
2598 start_layer
, num_layers
,
2599 aux_supported
, clear_supported
);
2600 if (aux_supported_out
)
2601 *aux_supported_out
= aux_supported
;
2605 intel_miptree_prepare_texture(struct brw_context
*brw
,
2606 struct intel_mipmap_tree
*mt
,
2607 mesa_format view_format
,
2608 bool *aux_supported_out
)
2610 intel_miptree_prepare_texture_slices(brw
, mt
, view_format
,
2611 0, INTEL_REMAINING_LEVELS
,
2612 0, INTEL_REMAINING_LAYERS
,
2617 intel_miptree_prepare_image(struct brw_context
*brw
,
2618 struct intel_mipmap_tree
*mt
)
2620 /* The data port doesn't understand any compression */
2621 intel_miptree_prepare_access(brw
, mt
, 0, INTEL_REMAINING_LEVELS
,
2622 0, INTEL_REMAINING_LAYERS
, false, false);
2626 intel_miptree_prepare_fb_fetch(struct brw_context
*brw
,
2627 struct intel_mipmap_tree
*mt
, uint32_t level
,
2628 uint32_t start_layer
, uint32_t num_layers
)
2630 intel_miptree_prepare_texture_slices(brw
, mt
, mt
->format
, level
, 1,
2631 start_layer
, num_layers
, NULL
);
2635 intel_miptree_prepare_render(struct brw_context
*brw
,
2636 struct intel_mipmap_tree
*mt
, uint32_t level
,
2637 uint32_t start_layer
, uint32_t layer_count
,
2640 /* If FRAMEBUFFER_SRGB is used on Gen9+ then we need to resolve any of
2641 * the single-sampled color renderbuffers because the CCS buffer isn't
2642 * supported for SRGB formats. This only matters if FRAMEBUFFER_SRGB is
2643 * enabled because otherwise the surface state will be programmed with
2644 * the linear equivalent format anyway.
2646 if (brw
->gen
== 9 && srgb_enabled
&& mt
->num_samples
<= 1 &&
2647 _mesa_get_srgb_format_linear(mt
->format
) != mt
->format
) {
2649 /* Lossless compression is not supported for SRGB formats, it
2650 * should be impossible to get here with such surfaces.
2652 assert(!intel_miptree_is_lossless_compressed(brw
, mt
));
2653 intel_miptree_prepare_access(brw
, mt
, level
, 1, start_layer
, layer_count
,
2659 intel_miptree_finish_render(struct brw_context
*brw
,
2660 struct intel_mipmap_tree
*mt
, uint32_t level
,
2661 uint32_t start_layer
, uint32_t layer_count
)
2663 assert(_mesa_is_format_color_format(mt
->format
));
2664 intel_miptree_finish_write(brw
, mt
, level
, start_layer
, layer_count
,
2665 mt
->mcs_buf
!= NULL
);
2669 intel_miptree_prepare_depth(struct brw_context
*brw
,
2670 struct intel_mipmap_tree
*mt
, uint32_t level
,
2671 uint32_t start_layer
, uint32_t layer_count
)
2673 intel_miptree_prepare_access(brw
, mt
, level
, 1, start_layer
, layer_count
,
2674 mt
->hiz_buf
!= NULL
, mt
->hiz_buf
!= NULL
);
2678 intel_miptree_finish_depth(struct brw_context
*brw
,
2679 struct intel_mipmap_tree
*mt
, uint32_t level
,
2680 uint32_t start_layer
, uint32_t layer_count
,
2683 if (depth_written
) {
2684 intel_miptree_finish_write(brw
, mt
, level
, start_layer
, layer_count
,
2685 mt
->hiz_buf
!= NULL
);
2690 * Make it possible to share the BO backing the given miptree with another
2691 * process or another miptree.
2693 * Fast color clears are unsafe with shared buffers, so we need to resolve and
2694 * then discard the MCS buffer, if present. We also set the no_ccs flag to
2695 * ensure that no MCS buffer gets allocated in the future.
2697 * HiZ is similarly unsafe with shared buffers.
2700 intel_miptree_make_shareable(struct brw_context
*brw
,
2701 struct intel_mipmap_tree
*mt
)
2703 /* MCS buffers are also used for multisample buffers, but we can't resolve
2704 * away a multisample MCS buffer because it's an integral part of how the
2705 * pixel data is stored. Fortunately this code path should never be
2706 * reached for multisample buffers.
2708 assert(mt
->msaa_layout
== INTEL_MSAA_LAYOUT_NONE
|| mt
->num_samples
<= 1);
2710 intel_miptree_prepare_access(brw
, mt
, 0, INTEL_REMAINING_LEVELS
,
2711 0, INTEL_REMAINING_LAYERS
, false, false);
2714 brw_bo_unreference(mt
->mcs_buf
->bo
);
2718 /* Any pending MCS/CCS operations are no longer needed. Trying to
2719 * execute any will likely crash due to the missing aux buffer. So let's
2720 * delete all pending ops.
2722 free(mt
->aux_state
);
2723 mt
->aux_state
= NULL
;
2727 intel_miptree_aux_buffer_free(mt
->hiz_buf
);
2730 for (uint32_t l
= mt
->first_level
; l
<= mt
->last_level
; ++l
) {
2731 mt
->level
[l
].has_hiz
= false;
2734 /* Any pending HiZ operations are no longer needed. Trying to execute
2735 * any will likely crash due to the missing aux buffer. So let's delete
2738 free(mt
->aux_state
);
2739 mt
->aux_state
= NULL
;
2742 mt
->aux_usage
= ISL_AUX_USAGE_NONE
;
2747 * \brief Get pointer offset into stencil buffer.
2749 * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
2750 * must decode the tile's layout in software.
2753 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
2755 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
2757 * Even though the returned offset is always positive, the return type is
2759 * commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
2760 * mesa: Fix return type of _mesa_get_format_bytes() (#37351)
2763 intel_offset_S8(uint32_t stride
, uint32_t x
, uint32_t y
, bool swizzled
)
2765 uint32_t tile_size
= 4096;
2766 uint32_t tile_width
= 64;
2767 uint32_t tile_height
= 64;
2768 uint32_t row_size
= 64 * stride
;
2770 uint32_t tile_x
= x
/ tile_width
;
2771 uint32_t tile_y
= y
/ tile_height
;
2773 /* The byte's address relative to the tile's base addres. */
2774 uint32_t byte_x
= x
% tile_width
;
2775 uint32_t byte_y
= y
% tile_height
;
2777 uintptr_t u
= tile_y
* row_size
2778 + tile_x
* tile_size
2779 + 512 * (byte_x
/ 8)
2781 + 32 * ((byte_y
/ 4) % 2)
2782 + 16 * ((byte_x
/ 4) % 2)
2783 + 8 * ((byte_y
/ 2) % 2)
2784 + 4 * ((byte_x
/ 2) % 2)
2789 /* adjust for bit6 swizzling */
2790 if (((byte_x
/ 8) % 2) == 1) {
2791 if (((byte_y
/ 8) % 2) == 0) {
2803 intel_miptree_updownsample(struct brw_context
*brw
,
2804 struct intel_mipmap_tree
*src
,
2805 struct intel_mipmap_tree
*dst
)
2807 unsigned src_w
, src_h
, dst_w
, dst_h
;
2809 if (src
->surf
.size
> 0) {
2810 src_w
= src
->surf
.logical_level0_px
.width
;
2811 src_h
= src
->surf
.logical_level0_px
.height
;
2813 src_w
= src
->logical_width0
;
2814 src_h
= src
->logical_height0
;
2817 if (dst
->surf
.size
> 0) {
2818 dst_w
= dst
->surf
.logical_level0_px
.width
;
2819 dst_h
= dst
->surf
.logical_level0_px
.height
;
2821 dst_w
= dst
->logical_width0
;
2822 dst_h
= dst
->logical_height0
;
2825 brw_blorp_blit_miptrees(brw
,
2826 src
, 0 /* level */, 0 /* layer */,
2827 src
->format
, SWIZZLE_XYZW
,
2828 dst
, 0 /* level */, 0 /* layer */, dst
->format
,
2831 GL_NEAREST
, false, false /*mirror x, y*/,
2834 if (src
->stencil_mt
) {
2835 if (src
->stencil_mt
->surf
.size
> 0) {
2836 src_w
= src
->stencil_mt
->surf
.logical_level0_px
.width
;
2837 src_h
= src
->stencil_mt
->surf
.logical_level0_px
.height
;
2839 src_w
= src
->stencil_mt
->logical_width0
;
2840 src_h
= src
->stencil_mt
->logical_height0
;
2843 if (dst
->stencil_mt
->surf
.size
> 0) {
2844 dst_w
= dst
->stencil_mt
->surf
.logical_level0_px
.width
;
2845 dst_h
= dst
->stencil_mt
->surf
.logical_level0_px
.height
;
2847 dst_w
= dst
->stencil_mt
->logical_width0
;
2848 dst_h
= dst
->stencil_mt
->logical_height0
;
2851 brw_blorp_blit_miptrees(brw
,
2852 src
->stencil_mt
, 0 /* level */, 0 /* layer */,
2853 src
->stencil_mt
->format
, SWIZZLE_XYZW
,
2854 dst
->stencil_mt
, 0 /* level */, 0 /* layer */,
2855 dst
->stencil_mt
->format
,
2858 GL_NEAREST
, false, false /*mirror x, y*/,
2859 false, false /* decode/encode srgb */);
2864 intel_update_r8stencil(struct brw_context
*brw
,
2865 struct intel_mipmap_tree
*mt
)
2867 assert(brw
->gen
>= 7);
2868 struct intel_mipmap_tree
*src
=
2869 mt
->format
== MESA_FORMAT_S_UINT8
? mt
: mt
->stencil_mt
;
2870 if (!src
|| brw
->gen
>= 8 || !src
->r8stencil_needs_update
)
2873 if (!mt
->r8stencil_mt
) {
2874 const uint32_t r8stencil_flags
=
2875 MIPTREE_LAYOUT_ACCELERATED_UPLOAD
| MIPTREE_LAYOUT_TILING_Y
|
2876 MIPTREE_LAYOUT_DISABLE_AUX
;
2877 assert(brw
->gen
> 6); /* Handle MIPTREE_LAYOUT_GEN6_HIZ_STENCIL */
2878 mt
->r8stencil_mt
= intel_miptree_create(brw
,
2880 MESA_FORMAT_R_UINT8
,
2883 src
->logical_width0
,
2884 src
->logical_height0
,
2885 src
->logical_depth0
,
2888 assert(mt
->r8stencil_mt
);
2891 struct intel_mipmap_tree
*dst
= mt
->r8stencil_mt
;
2893 for (int level
= src
->first_level
; level
<= src
->last_level
; level
++) {
2894 const unsigned depth
= src
->level
[level
].depth
;
2896 for (unsigned layer
= 0; layer
< depth
; layer
++) {
2897 brw_blorp_copy_miptrees(brw
,
2901 minify(src
->logical_width0
, level
),
2902 minify(src
->logical_height0
, level
));
2906 brw_render_cache_set_check_flush(brw
, dst
->bo
);
2907 src
->r8stencil_needs_update
= false;
2911 intel_miptree_map_raw(struct brw_context
*brw
,
2912 struct intel_mipmap_tree
*mt
,
2915 struct brw_bo
*bo
= mt
->bo
;
2917 if (brw_batch_references(&brw
->batch
, bo
))
2918 intel_batchbuffer_flush(brw
);
2920 return brw_bo_map(brw
, bo
, mode
);
2924 intel_miptree_unmap_raw(struct intel_mipmap_tree
*mt
)
2926 brw_bo_unmap(mt
->bo
);
2930 intel_miptree_map_gtt(struct brw_context
*brw
,
2931 struct intel_mipmap_tree
*mt
,
2932 struct intel_miptree_map
*map
,
2933 unsigned int level
, unsigned int slice
)
2935 unsigned int bw
, bh
;
2937 unsigned int image_x
, image_y
;
2938 intptr_t x
= map
->x
;
2939 intptr_t y
= map
->y
;
2941 /* For compressed formats, the stride is the number of bytes per
2942 * row of blocks. intel_miptree_get_image_offset() already does
2945 _mesa_get_format_block_size(mt
->format
, &bw
, &bh
);
2946 assert(y
% bh
== 0);
2947 assert(x
% bw
== 0);
2951 base
= intel_miptree_map_raw(brw
, mt
, map
->mode
) + mt
->offset
;
2956 /* Note that in the case of cube maps, the caller must have passed the
2957 * slice number referencing the face.
2959 intel_miptree_get_image_offset(mt
, level
, slice
, &image_x
, &image_y
);
2963 map
->stride
= mt
->pitch
;
2964 map
->ptr
= base
+ y
* map
->stride
+ x
* mt
->cpp
;
2967 DBG("%s: %d,%d %dx%d from mt %p (%s) "
2968 "%"PRIiPTR
",%"PRIiPTR
" = %p/%d\n", __func__
,
2969 map
->x
, map
->y
, map
->w
, map
->h
,
2970 mt
, _mesa_get_format_name(mt
->format
),
2971 x
, y
, map
->ptr
, map
->stride
);
2975 intel_miptree_unmap_gtt(struct intel_mipmap_tree
*mt
)
2977 intel_miptree_unmap_raw(mt
);
2981 intel_miptree_map_blit(struct brw_context
*brw
,
2982 struct intel_mipmap_tree
*mt
,
2983 struct intel_miptree_map
*map
,
2984 unsigned int level
, unsigned int slice
)
2986 map
->linear_mt
= intel_miptree_create(brw
, GL_TEXTURE_2D
, mt
->format
,
2987 /* first_level */ 0,
2991 MIPTREE_LAYOUT_TILING_NONE
);
2993 if (!map
->linear_mt
) {
2994 fprintf(stderr
, "Failed to allocate blit temporary\n");
2997 map
->stride
= map
->linear_mt
->pitch
;
2999 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
3000 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
3001 * invalidate is set, since we'll be writing the whole rectangle from our
3002 * temporary buffer back out.
3004 if (!(map
->mode
& GL_MAP_INVALIDATE_RANGE_BIT
)) {
3005 if (!intel_miptree_copy(brw
,
3006 mt
, level
, slice
, map
->x
, map
->y
,
3007 map
->linear_mt
, 0, 0, 0, 0,
3009 fprintf(stderr
, "Failed to blit\n");
3014 map
->ptr
= intel_miptree_map_raw(brw
, map
->linear_mt
, map
->mode
);
3016 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__
,
3017 map
->x
, map
->y
, map
->w
, map
->h
,
3018 mt
, _mesa_get_format_name(mt
->format
),
3019 level
, slice
, map
->ptr
, map
->stride
);
3024 intel_miptree_release(&map
->linear_mt
);
3030 intel_miptree_unmap_blit(struct brw_context
*brw
,
3031 struct intel_mipmap_tree
*mt
,
3032 struct intel_miptree_map
*map
,
3036 struct gl_context
*ctx
= &brw
->ctx
;
3038 intel_miptree_unmap_raw(map
->linear_mt
);
3040 if (map
->mode
& GL_MAP_WRITE_BIT
) {
3041 bool ok
= intel_miptree_copy(brw
,
3042 map
->linear_mt
, 0, 0, 0, 0,
3043 mt
, level
, slice
, map
->x
, map
->y
,
3045 WARN_ONCE(!ok
, "Failed to blit from linear temporary mapping");
3048 intel_miptree_release(&map
->linear_mt
);
3052 * "Map" a buffer by copying it to an untiled temporary using MOVNTDQA.
3054 #if defined(USE_SSE41)
3056 intel_miptree_map_movntdqa(struct brw_context
*brw
,
3057 struct intel_mipmap_tree
*mt
,
3058 struct intel_miptree_map
*map
,
3059 unsigned int level
, unsigned int slice
)
3061 assert(map
->mode
& GL_MAP_READ_BIT
);
3062 assert(!(map
->mode
& GL_MAP_WRITE_BIT
));
3064 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__
,
3065 map
->x
, map
->y
, map
->w
, map
->h
,
3066 mt
, _mesa_get_format_name(mt
->format
),
3067 level
, slice
, map
->ptr
, map
->stride
);
3069 /* Map the original image */
3072 intel_miptree_get_image_offset(mt
, level
, slice
, &image_x
, &image_y
);
3076 void *src
= intel_miptree_map_raw(brw
, mt
, map
->mode
);
3082 src
+= image_y
* mt
->pitch
;
3083 src
+= image_x
* mt
->cpp
;
3085 /* Due to the pixel offsets for the particular image being mapped, our
3086 * src pointer may not be 16-byte aligned. However, if the pitch is
3087 * divisible by 16, then the amount by which it's misaligned will remain
3088 * consistent from row to row.
3090 assert((mt
->pitch
% 16) == 0);
3091 const int misalignment
= ((uintptr_t) src
) & 15;
3093 /* Create an untiled temporary buffer for the mapping. */
3094 const unsigned width_bytes
= _mesa_format_row_stride(mt
->format
, map
->w
);
3096 map
->stride
= ALIGN(misalignment
+ width_bytes
, 16);
3098 map
->buffer
= _mesa_align_malloc(map
->stride
* map
->h
, 16);
3099 /* Offset the destination so it has the same misalignment as src. */
3100 map
->ptr
= map
->buffer
+ misalignment
;
3102 assert((((uintptr_t) map
->ptr
) & 15) == misalignment
);
3104 for (uint32_t y
= 0; y
< map
->h
; y
++) {
3105 void *dst_ptr
= map
->ptr
+ y
* map
->stride
;
3106 void *src_ptr
= src
+ y
* mt
->pitch
;
3108 _mesa_streaming_load_memcpy(dst_ptr
, src_ptr
, width_bytes
);
3111 intel_miptree_unmap_raw(mt
);
3115 intel_miptree_unmap_movntdqa(struct brw_context
*brw
,
3116 struct intel_mipmap_tree
*mt
,
3117 struct intel_miptree_map
*map
,
3121 _mesa_align_free(map
->buffer
);
3128 intel_miptree_map_s8(struct brw_context
*brw
,
3129 struct intel_mipmap_tree
*mt
,
3130 struct intel_miptree_map
*map
,
3131 unsigned int level
, unsigned int slice
)
3133 map
->stride
= map
->w
;
3134 map
->buffer
= map
->ptr
= malloc(map
->stride
* map
->h
);
3138 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
3139 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
3140 * invalidate is set, since we'll be writing the whole rectangle from our
3141 * temporary buffer back out.
3143 if (!(map
->mode
& GL_MAP_INVALIDATE_RANGE_BIT
)) {
3144 /* ISL uses a stencil pitch value that is expected by hardware whereas
3145 * traditional miptree uses half of that. Below the value gets supplied
3146 * to intel_offset_S8() which expects the legacy interpretation.
3148 const unsigned pitch
= mt
->surf
.size
> 0 ?
3149 mt
->surf
.row_pitch
/ 2 : mt
->pitch
;
3150 uint8_t *untiled_s8_map
= map
->ptr
;
3151 uint8_t *tiled_s8_map
= intel_miptree_map_raw(brw
, mt
, GL_MAP_READ_BIT
);
3152 unsigned int image_x
, image_y
;
3154 intel_miptree_get_image_offset(mt
, level
, slice
, &image_x
, &image_y
);
3156 for (uint32_t y
= 0; y
< map
->h
; y
++) {
3157 for (uint32_t x
= 0; x
< map
->w
; x
++) {
3158 ptrdiff_t offset
= intel_offset_S8(pitch
,
3159 x
+ image_x
+ map
->x
,
3160 y
+ image_y
+ map
->y
,
3161 brw
->has_swizzling
);
3162 untiled_s8_map
[y
* map
->w
+ x
] = tiled_s8_map
[offset
];
3166 intel_miptree_unmap_raw(mt
);
3168 DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __func__
,
3169 map
->x
, map
->y
, map
->w
, map
->h
,
3170 mt
, map
->x
+ image_x
, map
->y
+ image_y
, map
->ptr
, map
->stride
);
3172 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__
,
3173 map
->x
, map
->y
, map
->w
, map
->h
,
3174 mt
, map
->ptr
, map
->stride
);
3179 intel_miptree_unmap_s8(struct brw_context
*brw
,
3180 struct intel_mipmap_tree
*mt
,
3181 struct intel_miptree_map
*map
,
3185 if (map
->mode
& GL_MAP_WRITE_BIT
) {
3186 /* ISL uses a stencil pitch value that is expected by hardware whereas
3187 * traditional miptree uses half of that. Below the value gets supplied
3188 * to intel_offset_S8() which expects the legacy interpretation.
3190 const unsigned pitch
= mt
->surf
.size
> 0 ?
3191 mt
->surf
.row_pitch
/ 2: mt
->pitch
;
3192 unsigned int image_x
, image_y
;
3193 uint8_t *untiled_s8_map
= map
->ptr
;
3194 uint8_t *tiled_s8_map
= intel_miptree_map_raw(brw
, mt
, GL_MAP_WRITE_BIT
);
3196 intel_miptree_get_image_offset(mt
, level
, slice
, &image_x
, &image_y
);
3198 for (uint32_t y
= 0; y
< map
->h
; y
++) {
3199 for (uint32_t x
= 0; x
< map
->w
; x
++) {
3200 ptrdiff_t offset
= intel_offset_S8(pitch
,
3201 image_x
+ x
+ map
->x
,
3202 image_y
+ y
+ map
->y
,
3203 brw
->has_swizzling
);
3204 tiled_s8_map
[offset
] = untiled_s8_map
[y
* map
->w
+ x
];
3208 intel_miptree_unmap_raw(mt
);
3215 intel_miptree_map_etc(struct brw_context
*brw
,
3216 struct intel_mipmap_tree
*mt
,
3217 struct intel_miptree_map
*map
,
3221 assert(mt
->etc_format
!= MESA_FORMAT_NONE
);
3222 if (mt
->etc_format
== MESA_FORMAT_ETC1_RGB8
) {
3223 assert(mt
->format
== MESA_FORMAT_R8G8B8X8_UNORM
);
3226 assert(map
->mode
& GL_MAP_WRITE_BIT
);
3227 assert(map
->mode
& GL_MAP_INVALIDATE_RANGE_BIT
);
3229 map
->stride
= _mesa_format_row_stride(mt
->etc_format
, map
->w
);
3230 map
->buffer
= malloc(_mesa_format_image_size(mt
->etc_format
,
3231 map
->w
, map
->h
, 1));
3232 map
->ptr
= map
->buffer
;
3236 intel_miptree_unmap_etc(struct brw_context
*brw
,
3237 struct intel_mipmap_tree
*mt
,
3238 struct intel_miptree_map
*map
,
3244 intel_miptree_get_image_offset(mt
, level
, slice
, &image_x
, &image_y
);
3249 uint8_t *dst
= intel_miptree_map_raw(brw
, mt
, GL_MAP_WRITE_BIT
)
3250 + image_y
* mt
->pitch
3251 + image_x
* mt
->cpp
;
3253 if (mt
->etc_format
== MESA_FORMAT_ETC1_RGB8
)
3254 _mesa_etc1_unpack_rgba8888(dst
, mt
->pitch
,
3255 map
->ptr
, map
->stride
,
3258 _mesa_unpack_etc2_format(dst
, mt
->pitch
,
3259 map
->ptr
, map
->stride
,
3260 map
->w
, map
->h
, mt
->etc_format
);
3262 intel_miptree_unmap_raw(mt
);
3267 * Mapping function for packed depth/stencil miptrees backed by real separate
3268 * miptrees for depth and stencil.
3270 * On gen7, and to support HiZ pre-gen7, we have to have the stencil buffer
3271 * separate from the depth buffer. Yet at the GL API level, we have to expose
3272 * packed depth/stencil textures and FBO attachments, and Mesa core expects to
3273 * be able to map that memory for texture storage and glReadPixels-type
3274 * operations. We give Mesa core that access by mallocing a temporary and
3275 * copying the data between the actual backing store and the temporary.
3278 intel_miptree_map_depthstencil(struct brw_context
*brw
,
3279 struct intel_mipmap_tree
*mt
,
3280 struct intel_miptree_map
*map
,
3281 unsigned int level
, unsigned int slice
)
3283 struct intel_mipmap_tree
*z_mt
= mt
;
3284 struct intel_mipmap_tree
*s_mt
= mt
->stencil_mt
;
3285 bool map_z32f_x24s8
= mt
->format
== MESA_FORMAT_Z_FLOAT32
;
3286 int packed_bpp
= map_z32f_x24s8
? 8 : 4;
3288 map
->stride
= map
->w
* packed_bpp
;
3289 map
->buffer
= map
->ptr
= malloc(map
->stride
* map
->h
);
3293 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
3294 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
3295 * invalidate is set, since we'll be writing the whole rectangle from our
3296 * temporary buffer back out.
3298 if (!(map
->mode
& GL_MAP_INVALIDATE_RANGE_BIT
)) {
3299 /* ISL uses a stencil pitch value that is expected by hardware whereas
3300 * traditional miptree uses half of that. Below the value gets supplied
3301 * to intel_offset_S8() which expects the legacy interpretation.
3303 const unsigned s_pitch
= s_mt
->surf
.size
> 0 ?
3304 s_mt
->surf
.row_pitch
/ 2 : s_mt
->pitch
;
3305 uint32_t *packed_map
= map
->ptr
;
3306 uint8_t *s_map
= intel_miptree_map_raw(brw
, s_mt
, GL_MAP_READ_BIT
);
3307 uint32_t *z_map
= intel_miptree_map_raw(brw
, z_mt
, GL_MAP_READ_BIT
);
3308 unsigned int s_image_x
, s_image_y
;
3309 unsigned int z_image_x
, z_image_y
;
3311 intel_miptree_get_image_offset(s_mt
, level
, slice
,
3312 &s_image_x
, &s_image_y
);
3313 intel_miptree_get_image_offset(z_mt
, level
, slice
,
3314 &z_image_x
, &z_image_y
);
3316 for (uint32_t y
= 0; y
< map
->h
; y
++) {
3317 for (uint32_t x
= 0; x
< map
->w
; x
++) {
3318 int map_x
= map
->x
+ x
, map_y
= map
->y
+ y
;
3319 ptrdiff_t s_offset
= intel_offset_S8(s_pitch
,
3322 brw
->has_swizzling
);
3323 ptrdiff_t z_offset
= ((map_y
+ z_image_y
) *
3325 (map_x
+ z_image_x
));
3326 uint8_t s
= s_map
[s_offset
];
3327 uint32_t z
= z_map
[z_offset
];
3329 if (map_z32f_x24s8
) {
3330 packed_map
[(y
* map
->w
+ x
) * 2 + 0] = z
;
3331 packed_map
[(y
* map
->w
+ x
) * 2 + 1] = s
;
3333 packed_map
[y
* map
->w
+ x
] = (s
<< 24) | (z
& 0x00ffffff);
3338 intel_miptree_unmap_raw(s_mt
);
3339 intel_miptree_unmap_raw(z_mt
);
3341 DBG("%s: %d,%d %dx%d from z mt %p %d,%d, s mt %p %d,%d = %p/%d\n",
3343 map
->x
, map
->y
, map
->w
, map
->h
,
3344 z_mt
, map
->x
+ z_image_x
, map
->y
+ z_image_y
,
3345 s_mt
, map
->x
+ s_image_x
, map
->y
+ s_image_y
,
3346 map
->ptr
, map
->stride
);
3348 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__
,
3349 map
->x
, map
->y
, map
->w
, map
->h
,
3350 mt
, map
->ptr
, map
->stride
);
3355 intel_miptree_unmap_depthstencil(struct brw_context
*brw
,
3356 struct intel_mipmap_tree
*mt
,
3357 struct intel_miptree_map
*map
,
3361 struct intel_mipmap_tree
*z_mt
= mt
;
3362 struct intel_mipmap_tree
*s_mt
= mt
->stencil_mt
;
3363 bool map_z32f_x24s8
= mt
->format
== MESA_FORMAT_Z_FLOAT32
;
3365 if (map
->mode
& GL_MAP_WRITE_BIT
) {
3366 /* ISL uses a stencil pitch value that is expected by hardware whereas
3367 * traditional miptree uses half of that. Below the value gets supplied
3368 * to intel_offset_S8() which expects the legacy interpretation.
3370 const unsigned s_pitch
= s_mt
->surf
.size
> 0 ?
3371 s_mt
->surf
.row_pitch
/ 2 : s_mt
->pitch
;
3372 uint32_t *packed_map
= map
->ptr
;
3373 uint8_t *s_map
= intel_miptree_map_raw(brw
, s_mt
, GL_MAP_WRITE_BIT
);
3374 uint32_t *z_map
= intel_miptree_map_raw(brw
, z_mt
, GL_MAP_WRITE_BIT
);
3375 unsigned int s_image_x
, s_image_y
;
3376 unsigned int z_image_x
, z_image_y
;
3378 intel_miptree_get_image_offset(s_mt
, level
, slice
,
3379 &s_image_x
, &s_image_y
);
3380 intel_miptree_get_image_offset(z_mt
, level
, slice
,
3381 &z_image_x
, &z_image_y
);
3383 for (uint32_t y
= 0; y
< map
->h
; y
++) {
3384 for (uint32_t x
= 0; x
< map
->w
; x
++) {
3385 ptrdiff_t s_offset
= intel_offset_S8(s_pitch
,
3386 x
+ s_image_x
+ map
->x
,
3387 y
+ s_image_y
+ map
->y
,
3388 brw
->has_swizzling
);
3389 ptrdiff_t z_offset
= ((y
+ z_image_y
+ map
->y
) *
3391 (x
+ z_image_x
+ map
->x
));
3393 if (map_z32f_x24s8
) {
3394 z_map
[z_offset
] = packed_map
[(y
* map
->w
+ x
) * 2 + 0];
3395 s_map
[s_offset
] = packed_map
[(y
* map
->w
+ x
) * 2 + 1];
3397 uint32_t packed
= packed_map
[y
* map
->w
+ x
];
3398 s_map
[s_offset
] = packed
>> 24;
3399 z_map
[z_offset
] = packed
;
3404 intel_miptree_unmap_raw(s_mt
);
3405 intel_miptree_unmap_raw(z_mt
);
3407 DBG("%s: %d,%d %dx%d from z mt %p (%s) %d,%d, s mt %p %d,%d = %p/%d\n",
3409 map
->x
, map
->y
, map
->w
, map
->h
,
3410 z_mt
, _mesa_get_format_name(z_mt
->format
),
3411 map
->x
+ z_image_x
, map
->y
+ z_image_y
,
3412 s_mt
, map
->x
+ s_image_x
, map
->y
+ s_image_y
,
3413 map
->ptr
, map
->stride
);
3420 * Create and attach a map to the miptree at (level, slice). Return the
3423 static struct intel_miptree_map
*
3424 intel_miptree_attach_map(struct intel_mipmap_tree
*mt
,
3433 struct intel_miptree_map
*map
= calloc(1, sizeof(*map
));
3438 assert(mt
->level
[level
].slice
[slice
].map
== NULL
);
3439 mt
->level
[level
].slice
[slice
].map
= map
;
3451 * Release the map at (level, slice).
3454 intel_miptree_release_map(struct intel_mipmap_tree
*mt
,
3458 struct intel_miptree_map
**map
;
3460 map
= &mt
->level
[level
].slice
[slice
].map
;
3466 can_blit_slice(struct intel_mipmap_tree
*mt
,
3467 unsigned int level
, unsigned int slice
)
3469 /* See intel_miptree_blit() for details on the 32k pitch limit. */
3470 if (mt
->pitch
>= 32768)
3477 use_intel_mipree_map_blit(struct brw_context
*brw
,
3478 struct intel_mipmap_tree
*mt
,
3484 /* It's probably not worth swapping to the blit ring because of
3485 * all the overhead involved.
3487 !(mode
& GL_MAP_WRITE_BIT
) &&
3489 (mt
->tiling
== I915_TILING_X
||
3490 /* Prior to Sandybridge, the blitter can't handle Y tiling */
3491 (brw
->gen
>= 6 && mt
->tiling
== I915_TILING_Y
) ||
3492 /* Fast copy blit on skl+ supports all tiling formats. */
3494 can_blit_slice(mt
, level
, slice
))
3497 if (mt
->tiling
!= I915_TILING_NONE
&&
3498 mt
->bo
->size
>= brw
->max_gtt_map_object_size
) {
3499 assert(can_blit_slice(mt
, level
, slice
));
3507 * Parameter \a out_stride has type ptrdiff_t not because the buffer stride may
3508 * exceed 32 bits but to diminish the likelihood subtle bugs in pointer
3509 * arithmetic overflow.
3511 * If you call this function and use \a out_stride, then you're doing pointer
3512 * arithmetic on \a out_ptr. The type of \a out_stride doesn't prevent all
3513 * bugs. The caller must still take care to avoid 32-bit overflow errors in
3514 * all arithmetic expressions that contain buffer offsets and pixel sizes,
3515 * which usually have type uint32_t or GLuint.
3518 intel_miptree_map(struct brw_context
*brw
,
3519 struct intel_mipmap_tree
*mt
,
3528 ptrdiff_t *out_stride
)
3530 struct intel_miptree_map
*map
;
3532 assert(mt
->num_samples
<= 1);
3534 map
= intel_miptree_attach_map(mt
, level
, slice
, x
, y
, w
, h
, mode
);
3541 intel_miptree_access_raw(brw
, mt
, level
, slice
,
3542 map
->mode
& GL_MAP_WRITE_BIT
);
3544 if (mt
->format
== MESA_FORMAT_S_UINT8
) {
3545 intel_miptree_map_s8(brw
, mt
, map
, level
, slice
);
3546 } else if (mt
->etc_format
!= MESA_FORMAT_NONE
&&
3547 !(mode
& BRW_MAP_DIRECT_BIT
)) {
3548 intel_miptree_map_etc(brw
, mt
, map
, level
, slice
);
3549 } else if (mt
->stencil_mt
&& !(mode
& BRW_MAP_DIRECT_BIT
)) {
3550 intel_miptree_map_depthstencil(brw
, mt
, map
, level
, slice
);
3551 } else if (use_intel_mipree_map_blit(brw
, mt
, mode
, level
, slice
)) {
3552 intel_miptree_map_blit(brw
, mt
, map
, level
, slice
);
3553 #if defined(USE_SSE41)
3554 } else if (!(mode
& GL_MAP_WRITE_BIT
) &&
3555 !mt
->compressed
&& cpu_has_sse4_1
&&
3556 (mt
->pitch
% 16 == 0)) {
3557 intel_miptree_map_movntdqa(brw
, mt
, map
, level
, slice
);
3560 intel_miptree_map_gtt(brw
, mt
, map
, level
, slice
);
3563 *out_ptr
= map
->ptr
;
3564 *out_stride
= map
->stride
;
3566 if (map
->ptr
== NULL
)
3567 intel_miptree_release_map(mt
, level
, slice
);
3571 intel_miptree_unmap(struct brw_context
*brw
,
3572 struct intel_mipmap_tree
*mt
,
3576 struct intel_miptree_map
*map
= mt
->level
[level
].slice
[slice
].map
;
3578 assert(mt
->num_samples
<= 1);
3583 DBG("%s: mt %p (%s) level %d slice %d\n", __func__
,
3584 mt
, _mesa_get_format_name(mt
->format
), level
, slice
);
3586 if (mt
->format
== MESA_FORMAT_S_UINT8
) {
3587 intel_miptree_unmap_s8(brw
, mt
, map
, level
, slice
);
3588 } else if (mt
->etc_format
!= MESA_FORMAT_NONE
&&
3589 !(map
->mode
& BRW_MAP_DIRECT_BIT
)) {
3590 intel_miptree_unmap_etc(brw
, mt
, map
, level
, slice
);
3591 } else if (mt
->stencil_mt
&& !(map
->mode
& BRW_MAP_DIRECT_BIT
)) {
3592 intel_miptree_unmap_depthstencil(brw
, mt
, map
, level
, slice
);
3593 } else if (map
->linear_mt
) {
3594 intel_miptree_unmap_blit(brw
, mt
, map
, level
, slice
);
3595 #if defined(USE_SSE41)
3596 } else if (map
->buffer
&& cpu_has_sse4_1
) {
3597 intel_miptree_unmap_movntdqa(brw
, mt
, map
, level
, slice
);
3600 intel_miptree_unmap_gtt(mt
);
3603 intel_miptree_release_map(mt
, level
, slice
);
3607 get_isl_surf_dim(GLenum target
)
3611 case GL_TEXTURE_1D_ARRAY
:
3612 return ISL_SURF_DIM_1D
;
3615 case GL_TEXTURE_2D_ARRAY
:
3616 case GL_TEXTURE_RECTANGLE
:
3617 case GL_TEXTURE_CUBE_MAP
:
3618 case GL_TEXTURE_CUBE_MAP_ARRAY
:
3619 case GL_TEXTURE_2D_MULTISAMPLE
:
3620 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
3621 case GL_TEXTURE_EXTERNAL_OES
:
3622 return ISL_SURF_DIM_2D
;
3625 return ISL_SURF_DIM_3D
;
3628 unreachable("Invalid texture target");
3632 get_isl_dim_layout(const struct gen_device_info
*devinfo
, uint32_t tiling
,
3633 GLenum target
, enum miptree_array_layout array_layout
)
3635 if (array_layout
== GEN6_HIZ_STENCIL
)
3636 return ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ
;
3640 case GL_TEXTURE_1D_ARRAY
:
3641 return (devinfo
->gen
>= 9 && tiling
== I915_TILING_NONE
?
3642 ISL_DIM_LAYOUT_GEN9_1D
: ISL_DIM_LAYOUT_GEN4_2D
);
3645 case GL_TEXTURE_2D_ARRAY
:
3646 case GL_TEXTURE_RECTANGLE
:
3647 case GL_TEXTURE_2D_MULTISAMPLE
:
3648 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
3649 case GL_TEXTURE_EXTERNAL_OES
:
3650 return ISL_DIM_LAYOUT_GEN4_2D
;
3652 case GL_TEXTURE_CUBE_MAP
:
3653 case GL_TEXTURE_CUBE_MAP_ARRAY
:
3654 return (devinfo
->gen
== 4 ? ISL_DIM_LAYOUT_GEN4_3D
:
3655 ISL_DIM_LAYOUT_GEN4_2D
);
3658 return (devinfo
->gen
>= 9 ?
3659 ISL_DIM_LAYOUT_GEN4_2D
: ISL_DIM_LAYOUT_GEN4_3D
);
3662 unreachable("Invalid texture target");
3666 intel_miptree_get_isl_tiling(const struct intel_mipmap_tree
*mt
)
3668 if (mt
->format
== MESA_FORMAT_S_UINT8
) {
3669 return ISL_TILING_W
;
3671 switch (mt
->tiling
) {
3672 case I915_TILING_NONE
:
3673 return ISL_TILING_LINEAR
;
3675 return ISL_TILING_X
;
3677 return ISL_TILING_Y0
;
3679 unreachable("Invalid tiling mode");
3685 intel_miptree_get_isl_surf(struct brw_context
*brw
,
3686 const struct intel_mipmap_tree
*mt
,
3687 struct isl_surf
*surf
)
3689 surf
->dim
= get_isl_surf_dim(mt
->target
);
3690 surf
->dim_layout
= get_isl_dim_layout(&brw
->screen
->devinfo
,
3691 mt
->tiling
, mt
->target
,
3694 if (mt
->num_samples
> 1) {
3695 switch (mt
->msaa_layout
) {
3696 case INTEL_MSAA_LAYOUT_IMS
:
3697 surf
->msaa_layout
= ISL_MSAA_LAYOUT_INTERLEAVED
;
3699 case INTEL_MSAA_LAYOUT_UMS
:
3700 case INTEL_MSAA_LAYOUT_CMS
:
3701 surf
->msaa_layout
= ISL_MSAA_LAYOUT_ARRAY
;
3704 unreachable("Invalid MSAA layout");
3707 surf
->msaa_layout
= ISL_MSAA_LAYOUT_NONE
;
3710 surf
->tiling
= intel_miptree_get_isl_tiling(mt
);
3712 if (mt
->format
== MESA_FORMAT_S_UINT8
) {
3713 /* The ISL definition of row_pitch matches the surface state pitch field
3714 * a bit better than intel_mipmap_tree. In particular, ISL incorporates
3715 * the factor of 2 for W-tiling in row_pitch.
3717 surf
->row_pitch
= 2 * mt
->pitch
;
3719 surf
->row_pitch
= mt
->pitch
;
3722 surf
->format
= translate_tex_format(brw
, mt
->format
, false);
3724 if (brw
->gen
>= 9) {
3725 if (surf
->dim
== ISL_SURF_DIM_1D
&& surf
->tiling
== ISL_TILING_LINEAR
) {
3726 /* For gen9 1-D surfaces, intel_mipmap_tree has a bogus alignment. */
3727 surf
->image_alignment_el
= isl_extent3d(64, 1, 1);
3729 /* On gen9+, intel_mipmap_tree stores the horizontal and vertical
3730 * alignment in terms of surface elements like we want.
3732 surf
->image_alignment_el
= isl_extent3d(mt
->halign
, mt
->valign
, 1);
3735 /* On earlier gens it's stored in pixels. */
3737 _mesa_get_format_block_size(mt
->format
, &bw
, &bh
);
3738 surf
->image_alignment_el
=
3739 isl_extent3d(mt
->halign
/ bw
, mt
->valign
/ bh
, 1);
3742 surf
->logical_level0_px
.width
= mt
->logical_width0
;
3743 surf
->logical_level0_px
.height
= mt
->logical_height0
;
3744 if (surf
->dim
== ISL_SURF_DIM_3D
) {
3745 surf
->logical_level0_px
.depth
= mt
->logical_depth0
;
3746 surf
->logical_level0_px
.array_len
= 1;
3748 surf
->logical_level0_px
.depth
= 1;
3749 surf
->logical_level0_px
.array_len
= mt
->logical_depth0
;
3752 surf
->phys_level0_sa
.width
= mt
->physical_width0
;
3753 surf
->phys_level0_sa
.height
= mt
->physical_height0
;
3754 if (surf
->dim
== ISL_SURF_DIM_3D
) {
3755 surf
->phys_level0_sa
.depth
= mt
->physical_depth0
;
3756 surf
->phys_level0_sa
.array_len
= 1;
3758 surf
->phys_level0_sa
.depth
= 1;
3759 surf
->phys_level0_sa
.array_len
= mt
->physical_depth0
;
3762 surf
->levels
= mt
->last_level
- mt
->first_level
+ 1;
3763 surf
->samples
= MAX2(mt
->num_samples
, 1);
3765 surf
->size
= 0; /* TODO */
3766 surf
->alignment
= 0; /* TODO */
3768 switch (surf
->dim_layout
) {
3769 case ISL_DIM_LAYOUT_GEN4_2D
:
3770 case ISL_DIM_LAYOUT_GEN4_3D
:
3771 case ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ
:
3772 if (brw
->gen
>= 9) {
3773 surf
->array_pitch_el_rows
= mt
->qpitch
;
3776 _mesa_get_format_block_size(mt
->format
, &bw
, &bh
);
3777 assert(mt
->qpitch
% bh
== 0);
3778 surf
->array_pitch_el_rows
= mt
->qpitch
/ bh
;
3781 case ISL_DIM_LAYOUT_GEN9_1D
:
3782 surf
->array_pitch_el_rows
= 1;
3786 switch (mt
->array_layout
) {
3787 case ALL_LOD_IN_EACH_SLICE
:
3788 surf
->array_pitch_span
= ISL_ARRAY_PITCH_SPAN_FULL
;
3790 case ALL_SLICES_AT_EACH_LOD
:
3791 case GEN6_HIZ_STENCIL
:
3792 surf
->array_pitch_span
= ISL_ARRAY_PITCH_SPAN_COMPACT
;
3795 unreachable("Invalid array layout");
3798 GLenum base_format
= _mesa_get_format_base_format(mt
->format
);
3799 switch (base_format
) {
3800 case GL_DEPTH_COMPONENT
:
3801 surf
->usage
= ISL_SURF_USAGE_DEPTH_BIT
| ISL_SURF_USAGE_TEXTURE_BIT
;
3803 case GL_STENCIL_INDEX
:
3804 surf
->usage
= ISL_SURF_USAGE_STENCIL_BIT
;
3806 surf
->usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
3808 case GL_DEPTH_STENCIL
:
3809 /* In this case we only texture from the depth part */
3810 surf
->usage
= ISL_SURF_USAGE_DEPTH_BIT
| ISL_SURF_USAGE_STENCIL_BIT
|
3811 ISL_SURF_USAGE_TEXTURE_BIT
;
3814 surf
->usage
= ISL_SURF_USAGE_TEXTURE_BIT
;
3815 if (brw
->mesa_format_supports_render
[mt
->format
])
3816 surf
->usage
= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
3820 if (_mesa_is_cube_map_texture(mt
->target
))
3821 surf
->usage
|= ISL_SURF_USAGE_CUBE_BIT
;
3825 intel_miptree_get_aux_isl_usage(const struct brw_context
*brw
,
3826 const struct intel_mipmap_tree
*mt
)
3829 return ISL_AUX_USAGE_HIZ
;
3832 return ISL_AUX_USAGE_NONE
;
3834 return mt
->aux_usage
;