2 * Copyright 2006 VMware, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include <GL/internal/dri_interface.h>
29 #include "intel_batchbuffer.h"
30 #include "intel_mipmap_tree.h"
31 #include "intel_tex.h"
32 #include "intel_blit.h"
33 #include "intel_fbo.h"
35 #include "brw_blorp.h"
36 #include "brw_context.h"
37 #include "brw_state.h"
39 #include "main/enums.h"
40 #include "main/fbobject.h"
41 #include "main/formats.h"
42 #include "main/glformats.h"
43 #include "main/texcompress_etc.h"
44 #include "main/teximage.h"
45 #include "main/streaming-load-memcpy.h"
46 #include "x86/common_x86_asm.h"
48 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
50 static void *intel_miptree_map_raw(struct brw_context
*brw
,
51 struct intel_mipmap_tree
*mt
,
54 static void intel_miptree_unmap_raw(struct intel_mipmap_tree
*mt
);
57 intel_miptree_alloc_mcs(struct brw_context
*brw
,
58 struct intel_mipmap_tree
*mt
,
62 * Determine which MSAA layout should be used by the MSAA surface being
63 * created, based on the chip generation and the surface type.
65 static enum intel_msaa_layout
66 compute_msaa_layout(struct brw_context
*brw
, mesa_format format
,
67 enum intel_aux_disable aux_disable
)
69 /* Prior to Gen7, all MSAA surfaces used IMS layout. */
71 return INTEL_MSAA_LAYOUT_IMS
;
73 /* In Gen7, IMS layout is only used for depth and stencil buffers. */
74 switch (_mesa_get_format_base_format(format
)) {
75 case GL_DEPTH_COMPONENT
:
76 case GL_STENCIL_INDEX
:
77 case GL_DEPTH_STENCIL
:
78 return INTEL_MSAA_LAYOUT_IMS
;
80 /* From the Ivy Bridge PRM, Vol4 Part1 p77 ("MCS Enable"):
82 * This field must be set to 0 for all SINT MSRTs when all RT channels
85 * In practice this means that we have to disable MCS for all signed
86 * integer MSAA buffers. The alternative, to disable MCS only when one
87 * of the render target channels is disabled, is impractical because it
88 * would require converting between CMS and UMS MSAA layouts on the fly,
91 if (brw
->gen
== 7 && _mesa_get_format_datatype(format
) == GL_INT
) {
92 return INTEL_MSAA_LAYOUT_UMS
;
93 } else if (aux_disable
& INTEL_AUX_DISABLE_MCS
) {
94 /* We can't use the CMS layout because it uses an aux buffer, the MCS
95 * buffer. So fallback to UMS, which is identical to CMS without the
97 return INTEL_MSAA_LAYOUT_UMS
;
99 return INTEL_MSAA_LAYOUT_CMS
;
105 intel_tiling_supports_non_msrt_mcs(const struct brw_context
*brw
,
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_non_msrt_fast_clear(struct brw_context
*brw
,
146 const struct intel_mipmap_tree
*mt
)
148 /* MCS support does not exist prior to Gen7 */
152 if (mt
->aux_disable
& INTEL_AUX_DISABLE_MCS
)
155 /* This function applies only to non-multisampled render targets. */
156 if (mt
->num_samples
> 1)
159 /* MCS is only supported for color buffers */
160 switch (_mesa_get_format_base_format(mt
->format
)) {
161 case GL_DEPTH_COMPONENT
:
162 case GL_DEPTH_STENCIL
:
163 case GL_STENCIL_INDEX
:
167 if (mt
->cpp
!= 4 && mt
->cpp
!= 8 && mt
->cpp
!= 16)
170 const bool mip_mapped
= mt
->first_level
!= 0 || mt
->last_level
!= 0;
171 const bool arrayed
= mt
->physical_depth0
!= 1;
174 /* Multisample surfaces with the CMS layout are not layered surfaces,
175 * yet still have physical_depth0 > 1. Assert that we don't
176 * accidentally reject a multisampled surface here. We should have
177 * rejected it earlier by explicitly checking the sample count.
179 assert(mt
->num_samples
<= 1);
182 /* Handle the hardware restrictions...
184 * All GENs have the following restriction: "MCS buffer for non-MSRT is
185 * supported only for RT formats 32bpp, 64bpp, and 128bpp."
187 * From the HSW PRM Volume 7: 3D-Media-GPGPU, page 652: (Color Clear of
188 * Non-MultiSampler Render Target Restrictions) Support is for
189 * non-mip-mapped and non-array surface types only.
191 * From the BDW PRM Volume 7: 3D-Media-GPGPU, page 649: (Color Clear of
192 * Non-MultiSampler Render Target Restriction). Mip-mapped and arrayed
193 * surfaces are supported with MCS buffer layout with these alignments in
194 * the RT space: Horizontal Alignment = 256 and Vertical Alignment = 128.
196 * From the SKL PRM Volume 7: 3D-Media-GPGPU, page 632: (Color Clear of
197 * Non-MultiSampler Render Target Restriction). Mip-mapped and arrayed
198 * surfaces are supported with MCS buffer layout with these alignments in
199 * the RT space: Horizontal Alignment = 128 and Vertical Alignment = 64.
201 if (brw
->gen
< 8 && (mip_mapped
|| arrayed
))
204 /* There's no point in using an MCS buffer if the surface isn't in a
207 if (!brw
->format_supported_as_render_target
[mt
->format
])
211 mesa_format linear_format
= _mesa_get_srgb_format_linear(mt
->format
);
212 const enum isl_format isl_format
=
213 brw_isl_format_for_mesa_format(linear_format
);
214 return isl_format_supports_ccs_e(&brw
->screen
->devinfo
, isl_format
);
219 /* On Gen9 support for color buffer compression was extended to single
220 * sampled surfaces. This is a helper considering both auxiliary buffer
221 * type and number of samples telling if the given miptree represents
222 * the new single sampled case - also called lossless compression.
225 intel_miptree_is_lossless_compressed(const struct brw_context
*brw
,
226 const struct intel_mipmap_tree
*mt
)
228 /* Only available from Gen9 onwards. */
232 /* Compression always requires auxiliary buffer. */
236 /* Single sample compression is represented re-using msaa compression
237 * layout type: "Compressed Multisampled Surfaces".
239 if (mt
->msaa_layout
!= INTEL_MSAA_LAYOUT_CMS
)
242 /* And finally distinguish between msaa and single sample case. */
243 return mt
->num_samples
<= 1;
247 intel_miptree_supports_lossless_compressed(struct brw_context
*brw
,
248 const struct intel_mipmap_tree
*mt
)
250 /* For now compression is only enabled for integer formats even though
251 * there exist supported floating point formats also. This is a heuristic
252 * decision based on current public benchmarks. In none of the cases these
253 * formats provided any improvement but a few cases were seen to regress.
254 * Hence these are left to to be enabled in the future when they are known
257 if (_mesa_get_format_datatype(mt
->format
) == GL_FLOAT
)
260 /* Fast clear mechanism and lossless compression go hand in hand. */
261 if (!intel_miptree_supports_non_msrt_fast_clear(brw
, mt
))
264 /* Fast clear can be also used to clear srgb surfaces by using equivalent
265 * linear format. This trick, however, can't be extended to be used with
266 * lossless compression and therefore a check is needed to see if the format
269 return _mesa_get_srgb_format_linear(mt
->format
) == mt
->format
;
273 * Determine depth format corresponding to a depth+stencil format,
274 * for separate stencil.
277 intel_depth_format_for_depthstencil_format(mesa_format format
) {
279 case MESA_FORMAT_Z24_UNORM_S8_UINT
:
280 return MESA_FORMAT_Z24_UNORM_X8_UINT
;
281 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT
:
282 return MESA_FORMAT_Z_FLOAT32
;
289 create_mapping_table(GLenum target
, unsigned first_level
, unsigned last_level
,
290 unsigned depth0
, struct intel_mipmap_level
*table
)
292 for (unsigned level
= first_level
; level
<= last_level
; level
++) {
294 target
== GL_TEXTURE_3D
? minify(depth0
, level
) : depth0
;
296 table
[level
].slice
= calloc(d
, sizeof(*table
[0].slice
));
297 if (!table
[level
].slice
)
304 for (unsigned level
= first_level
; level
<= last_level
; level
++)
305 free(table
[level
].slice
);
311 * @param for_bo Indicates that the caller is
312 * intel_miptree_create_for_bo(). If true, then do not create
315 static struct intel_mipmap_tree
*
316 intel_miptree_create_layout(struct brw_context
*brw
,
325 uint32_t layout_flags
)
327 struct intel_mipmap_tree
*mt
= calloc(sizeof(*mt
), 1);
331 DBG("%s target %s format %s level %d..%d slices %d <-- %p\n", __func__
,
332 _mesa_enum_to_string(target
),
333 _mesa_get_format_name(format
),
334 first_level
, last_level
, depth0
, mt
);
336 if (target
== GL_TEXTURE_1D_ARRAY
)
337 assert(height0
== 1);
341 mt
->first_level
= first_level
;
342 mt
->last_level
= last_level
;
343 mt
->logical_width0
= width0
;
344 mt
->logical_height0
= height0
;
345 mt
->logical_depth0
= depth0
;
346 mt
->aux_disable
= (layout_flags
& MIPTREE_LAYOUT_DISABLE_AUX
) != 0 ?
347 INTEL_AUX_DISABLE_ALL
: INTEL_AUX_DISABLE_NONE
;
348 mt
->aux_disable
|= INTEL_AUX_DISABLE_CCS
;
349 mt
->is_scanout
= (layout_flags
& MIPTREE_LAYOUT_FOR_SCANOUT
) != 0;
350 mt
->aux_state
= NULL
;
351 mt
->cpp
= _mesa_get_format_bytes(format
);
352 mt
->num_samples
= num_samples
;
353 mt
->compressed
= _mesa_is_format_compressed(format
);
354 mt
->msaa_layout
= INTEL_MSAA_LAYOUT_NONE
;
357 if (brw
->gen
== 6 && format
== MESA_FORMAT_S_UINT8
)
358 layout_flags
|= MIPTREE_LAYOUT_GEN6_HIZ_STENCIL
;
360 int depth_multiply
= 1;
361 if (num_samples
> 1) {
362 /* Adjust width/height/depth for MSAA */
363 mt
->msaa_layout
= compute_msaa_layout(brw
, format
, mt
->aux_disable
);
364 if (mt
->msaa_layout
== INTEL_MSAA_LAYOUT_IMS
) {
365 /* From the Ivybridge PRM, Volume 1, Part 1, page 108:
366 * "If the surface is multisampled and it is a depth or stencil
367 * surface or Multisampled Surface StorageFormat in SURFACE_STATE is
368 * MSFMT_DEPTH_STENCIL, WL and HL must be adjusted as follows before
371 * +----------------------------------------------------------------+
372 * | Num Multisamples | W_l = | H_l = |
373 * +----------------------------------------------------------------+
374 * | 2 | ceiling(W_l / 2) * 4 | H_l (no adjustment) |
375 * | 4 | ceiling(W_l / 2) * 4 | ceiling(H_l / 2) * 4 |
376 * | 8 | ceiling(W_l / 2) * 8 | ceiling(H_l / 2) * 4 |
377 * | 16 | ceiling(W_l / 2) * 8 | ceiling(H_l / 2) * 8 |
378 * +----------------------------------------------------------------+
381 * Note that MSFMT_DEPTH_STENCIL just means the IMS (interleaved)
382 * format rather than UMS/CMS (array slices). The Sandybridge PRM,
383 * Volume 1, Part 1, Page 111 has the same formula for 4x MSAA.
385 * Another more complicated explanation for these adjustments comes
386 * from the Sandybridge PRM, volume 4, part 1, page 31:
388 * "Any of the other messages (sample*, LOD, load4) used with a
389 * (4x) multisampled surface will in-effect sample a surface with
390 * double the height and width as that indicated in the surface
391 * state. Each pixel position on the original-sized surface is
392 * replaced with a 2x2 of samples with the following arrangement:
397 * Thus, when sampling from a multisampled texture, it behaves as
398 * though the layout in memory for (x,y,sample) is:
400 * (0,0,0) (0,0,2) (1,0,0) (1,0,2)
401 * (0,0,1) (0,0,3) (1,0,1) (1,0,3)
403 * (0,1,0) (0,1,2) (1,1,0) (1,1,2)
404 * (0,1,1) (0,1,3) (1,1,1) (1,1,3)
406 * However, the actual layout of multisampled data in memory is:
408 * (0,0,0) (1,0,0) (0,0,1) (1,0,1)
409 * (0,1,0) (1,1,0) (0,1,1) (1,1,1)
411 * (0,0,2) (1,0,2) (0,0,3) (1,0,3)
412 * (0,1,2) (1,1,2) (0,1,3) (1,1,3)
414 * This pattern repeats for each 2x2 pixel block.
416 * As a result, when calculating the size of our 4-sample buffer for
417 * an odd width or height, we have to align before scaling up because
418 * sample 3 is in that bottom right 2x2 block.
420 switch (num_samples
) {
422 assert(brw
->gen
>= 8);
423 width0
= ALIGN(width0
, 2) * 2;
424 height0
= ALIGN(height0
, 2);
427 width0
= ALIGN(width0
, 2) * 2;
428 height0
= ALIGN(height0
, 2) * 2;
431 width0
= ALIGN(width0
, 2) * 4;
432 height0
= ALIGN(height0
, 2) * 2;
435 width0
= ALIGN(width0
, 2) * 4;
436 height0
= ALIGN(height0
, 2) * 4;
439 /* num_samples should already have been quantized to 0, 1, 2, 4, 8
442 unreachable("not reached");
445 /* Non-interleaved */
446 depth_multiply
= num_samples
;
447 depth0
*= depth_multiply
;
451 if (!create_mapping_table(target
, first_level
, last_level
, depth0
,
457 /* Set array_layout to ALL_SLICES_AT_EACH_LOD when array_spacing_lod0 can
458 * be used. array_spacing_lod0 is only used for non-IMS MSAA surfaces on
459 * Gen 7 and 8. On Gen 8 and 9 this layout is not available but it is still
460 * used on Gen8 to make it pick a qpitch value which doesn't include space
461 * for the mipmaps. On Gen9 this is not necessary because it will
462 * automatically pick a packed qpitch value whenever mt->first_level ==
464 * TODO: can we use it elsewhere?
465 * TODO: also disable this on Gen8 and pick the qpitch value like Gen9
468 mt
->array_layout
= ALL_LOD_IN_EACH_SLICE
;
470 switch (mt
->msaa_layout
) {
471 case INTEL_MSAA_LAYOUT_NONE
:
472 case INTEL_MSAA_LAYOUT_IMS
:
473 mt
->array_layout
= ALL_LOD_IN_EACH_SLICE
;
475 case INTEL_MSAA_LAYOUT_UMS
:
476 case INTEL_MSAA_LAYOUT_CMS
:
477 mt
->array_layout
= ALL_SLICES_AT_EACH_LOD
;
482 if (target
== GL_TEXTURE_CUBE_MAP
)
483 assert(depth0
== 6 * depth_multiply
);
485 mt
->physical_width0
= width0
;
486 mt
->physical_height0
= height0
;
487 mt
->physical_depth0
= depth0
;
489 if (!(layout_flags
& MIPTREE_LAYOUT_FOR_BO
) &&
490 _mesa_get_format_base_format(format
) == GL_DEPTH_STENCIL
&&
491 (brw
->must_use_separate_stencil
||
492 (brw
->has_separate_stencil
&&
493 intel_miptree_wants_hiz_buffer(brw
, mt
)))) {
494 uint32_t stencil_flags
= MIPTREE_LAYOUT_ACCELERATED_UPLOAD
;
496 stencil_flags
|= MIPTREE_LAYOUT_TILING_ANY
;
499 mt
->stencil_mt
= intel_miptree_create(brw
,
510 if (!mt
->stencil_mt
) {
511 intel_miptree_release(&mt
);
514 mt
->stencil_mt
->r8stencil_needs_update
= true;
516 /* Fix up the Z miptree format for how we're splitting out separate
517 * stencil. Gen7 expects there to be no stencil bits in its depth buffer.
519 mt
->format
= intel_depth_format_for_depthstencil_format(mt
->format
);
522 if (format
== mt
->format
) {
523 _mesa_problem(NULL
, "Unknown format %s in separate stencil mt\n",
524 _mesa_get_format_name(mt
->format
));
528 if (layout_flags
& MIPTREE_LAYOUT_GEN6_HIZ_STENCIL
)
529 mt
->array_layout
= GEN6_HIZ_STENCIL
;
532 * Obey HALIGN_16 constraints for Gen8 and Gen9 buffers which are
533 * multisampled or have an AUX buffer attached to it.
535 * GEN | MSRT | AUX_CCS_* or AUX_MCS
536 * -------------------------------------------
537 * 9 | HALIGN_16 | HALIGN_16
538 * 8 | HALIGN_ANY | HALIGN_16
542 if (intel_miptree_supports_non_msrt_fast_clear(brw
, mt
)) {
543 if (brw
->gen
>= 9 || (brw
->gen
== 8 && num_samples
<= 1))
544 layout_flags
|= MIPTREE_LAYOUT_FORCE_HALIGN16
;
545 } else if (brw
->gen
>= 9 && num_samples
> 1) {
546 layout_flags
|= MIPTREE_LAYOUT_FORCE_HALIGN16
;
548 const UNUSED
bool is_lossless_compressed_aux
=
549 brw
->gen
>= 9 && num_samples
== 1 &&
550 mt
->format
== MESA_FORMAT_R_UINT32
;
552 /* For now, nothing else has this requirement */
553 assert(is_lossless_compressed_aux
||
554 (layout_flags
& MIPTREE_LAYOUT_FORCE_HALIGN16
) == 0);
557 if (!brw_miptree_layout(brw
, mt
, layout_flags
)) {
558 intel_miptree_release(&mt
);
562 if (mt
->aux_disable
& INTEL_AUX_DISABLE_MCS
)
563 assert(mt
->msaa_layout
!= INTEL_MSAA_LAYOUT_CMS
);
570 * Choose an appropriate uncompressed format for a requested
571 * compressed format, if unsupported.
574 intel_lower_compressed_format(struct brw_context
*brw
, mesa_format format
)
576 /* No need to lower ETC formats on these platforms,
577 * they are supported natively.
579 if (brw
->gen
>= 8 || brw
->is_baytrail
)
583 case MESA_FORMAT_ETC1_RGB8
:
584 return MESA_FORMAT_R8G8B8X8_UNORM
;
585 case MESA_FORMAT_ETC2_RGB8
:
586 return MESA_FORMAT_R8G8B8X8_UNORM
;
587 case MESA_FORMAT_ETC2_SRGB8
:
588 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC
:
589 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1
:
590 return MESA_FORMAT_B8G8R8A8_SRGB
;
591 case MESA_FORMAT_ETC2_RGBA8_EAC
:
592 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1
:
593 return MESA_FORMAT_R8G8B8A8_UNORM
;
594 case MESA_FORMAT_ETC2_R11_EAC
:
595 return MESA_FORMAT_R_UNORM16
;
596 case MESA_FORMAT_ETC2_SIGNED_R11_EAC
:
597 return MESA_FORMAT_R_SNORM16
;
598 case MESA_FORMAT_ETC2_RG11_EAC
:
599 return MESA_FORMAT_R16G16_UNORM
;
600 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC
:
601 return MESA_FORMAT_R16G16_SNORM
;
603 /* Non ETC1 / ETC2 format */
608 /** \brief Assert that the level and layer are valid for the miptree. */
610 intel_miptree_check_level_layer(const struct intel_mipmap_tree
*mt
,
618 assert(level
>= mt
->first_level
);
619 assert(level
<= mt
->last_level
);
621 if (mt
->surf
.size
> 0)
622 assert(layer
< (mt
->surf
.dim
== ISL_SURF_DIM_3D
?
623 minify(mt
->surf
.phys_level0_sa
.depth
, level
) :
624 mt
->surf
.phys_level0_sa
.array_len
));
626 assert(layer
< mt
->level
[level
].depth
);
629 static enum isl_aux_state
**
630 create_aux_state_map(struct intel_mipmap_tree
*mt
,
631 enum isl_aux_state initial
)
633 const uint32_t levels
= mt
->last_level
+ 1;
635 uint32_t total_slices
= 0;
636 for (uint32_t level
= 0; level
< levels
; level
++)
637 total_slices
+= mt
->level
[level
].depth
;
639 const size_t per_level_array_size
= levels
* sizeof(enum isl_aux_state
*);
641 /* We're going to allocate a single chunk of data for both the per-level
642 * reference array and the arrays of aux_state. This makes cleanup
643 * significantly easier.
645 const size_t total_size
= per_level_array_size
+
646 total_slices
* sizeof(enum isl_aux_state
);
647 void *data
= malloc(total_size
);
651 enum isl_aux_state
**per_level_arr
= data
;
652 enum isl_aux_state
*s
= data
+ per_level_array_size
;
653 for (uint32_t level
= 0; level
< levels
; level
++) {
654 per_level_arr
[level
] = s
;
655 for (uint32_t a
= 0; a
< mt
->level
[level
].depth
; a
++)
658 assert((void *)s
== data
+ total_size
);
660 return per_level_arr
;
664 free_aux_state_map(enum isl_aux_state
**state
)
669 static struct intel_mipmap_tree
*
670 make_surface(struct brw_context
*brw
, GLenum target
, mesa_format format
,
671 unsigned first_level
, unsigned last_level
,
672 unsigned width0
, unsigned height0
, unsigned depth0
,
673 unsigned num_samples
, enum isl_tiling isl_tiling
,
674 isl_surf_usage_flags_t isl_usage_flags
, uint32_t alloc_flags
,
677 struct intel_mipmap_tree
*mt
= calloc(sizeof(*mt
), 1);
681 if (!create_mapping_table(target
, first_level
, last_level
, depth0
,
687 if (target
== GL_TEXTURE_CUBE_MAP
||
688 target
== GL_TEXTURE_CUBE_MAP_ARRAY
)
689 isl_usage_flags
|= ISL_SURF_USAGE_CUBE_BIT
;
691 DBG("%s: %s %s %ux %u:%u:%u %d..%d <-- %p\n",
693 _mesa_enum_to_string(target
),
694 _mesa_get_format_name(format
),
695 num_samples
, width0
, height0
, depth0
,
696 first_level
, last_level
, mt
);
698 struct isl_surf_init_info init_info
= {
699 .dim
= get_isl_surf_dim(target
),
700 .format
= translate_tex_format(brw
, format
, false),
703 .depth
= target
== GL_TEXTURE_3D
? depth0
: 1,
704 .levels
= last_level
- first_level
+ 1,
705 .array_len
= target
== GL_TEXTURE_3D
? 1 : depth0
,
706 .samples
= MAX2(num_samples
, 1),
707 .usage
= isl_usage_flags
,
708 .tiling_flags
= 1u << isl_tiling
711 if (!isl_surf_init_s(&brw
->isl_dev
, &mt
->surf
, &init_info
))
714 assert(mt
->surf
.size
% mt
->surf
.row_pitch
== 0);
717 mt
->bo
= brw_bo_alloc_tiled(brw
->bufmgr
, "isl-miptree",
719 isl_tiling_to_bufmgr_tiling(isl_tiling
),
720 mt
->surf
.row_pitch
, alloc_flags
);
727 mt
->first_level
= first_level
;
728 mt
->last_level
= last_level
;
732 mt
->aux_state
= NULL
;
737 intel_miptree_release(&mt
);
741 static struct intel_mipmap_tree
*
742 miptree_create(struct brw_context
*brw
,
751 uint32_t layout_flags
)
753 if (brw
->gen
== 6 && format
== MESA_FORMAT_S_UINT8
)
754 return make_surface(brw
, target
, format
, first_level
, last_level
,
755 width0
, height0
, depth0
, num_samples
, ISL_TILING_W
,
756 ISL_SURF_USAGE_STENCIL_BIT
|
757 ISL_SURF_USAGE_TEXTURE_BIT
,
758 BO_ALLOC_FOR_RENDER
, NULL
);
760 struct intel_mipmap_tree
*mt
;
761 mesa_format tex_format
= format
;
762 mesa_format etc_format
= MESA_FORMAT_NONE
;
763 uint32_t alloc_flags
= 0;
765 format
= intel_lower_compressed_format(brw
, format
);
767 etc_format
= (format
!= tex_format
) ? tex_format
: MESA_FORMAT_NONE
;
769 assert((layout_flags
& MIPTREE_LAYOUT_FOR_BO
) == 0);
770 mt
= intel_miptree_create_layout(brw
, target
, format
,
771 first_level
, last_level
, width0
,
772 height0
, depth0
, num_samples
,
777 if (mt
->tiling
== (I915_TILING_Y
| I915_TILING_X
))
778 mt
->tiling
= I915_TILING_Y
;
780 if (layout_flags
& MIPTREE_LAYOUT_ACCELERATED_UPLOAD
)
781 alloc_flags
|= BO_ALLOC_FOR_RENDER
;
783 mt
->etc_format
= etc_format
;
785 if (format
== MESA_FORMAT_S_UINT8
) {
786 /* Align to size of W tile, 64x64. */
787 mt
->bo
= brw_bo_alloc_tiled_2d(brw
->bufmgr
, "miptree",
788 ALIGN(mt
->total_width
, 64),
789 ALIGN(mt
->total_height
, 64),
790 mt
->cpp
, mt
->tiling
, &mt
->pitch
,
793 mt
->bo
= brw_bo_alloc_tiled_2d(brw
->bufmgr
, "miptree",
794 mt
->total_width
, mt
->total_height
,
795 mt
->cpp
, mt
->tiling
, &mt
->pitch
,
799 if (layout_flags
& MIPTREE_LAYOUT_FOR_SCANOUT
)
800 mt
->bo
->cache_coherent
= false;
805 struct intel_mipmap_tree
*
806 intel_miptree_create(struct brw_context
*brw
,
815 uint32_t layout_flags
)
817 struct intel_mipmap_tree
*mt
= miptree_create(
819 first_level
, last_level
,
820 width0
, height0
, depth0
, num_samples
,
823 /* If the BO is too large to fit in the aperture, we need to use the
824 * BLT engine to support it. Prior to Sandybridge, the BLT paths can't
825 * handle Y-tiling, so we need to fall back to X.
827 if (brw
->gen
< 6 && mt
->bo
->size
>= brw
->max_gtt_map_object_size
&&
828 mt
->tiling
== I915_TILING_Y
) {
829 const uint32_t alloc_flags
=
830 (layout_flags
& MIPTREE_LAYOUT_ACCELERATED_UPLOAD
) ?
831 BO_ALLOC_FOR_RENDER
: 0;
832 perf_debug("%dx%d miptree larger than aperture; falling back to X-tiled\n",
833 mt
->total_width
, mt
->total_height
);
835 mt
->tiling
= I915_TILING_X
;
836 brw_bo_unreference(mt
->bo
);
837 mt
->bo
= brw_bo_alloc_tiled_2d(brw
->bufmgr
, "miptree",
838 mt
->total_width
, mt
->total_height
, mt
->cpp
,
839 mt
->tiling
, &mt
->pitch
, alloc_flags
);
845 intel_miptree_release(&mt
);
850 if (mt
->msaa_layout
== INTEL_MSAA_LAYOUT_CMS
) {
851 assert(mt
->num_samples
> 1);
852 if (!intel_miptree_alloc_mcs(brw
, mt
, num_samples
)) {
853 intel_miptree_release(&mt
);
858 /* If this miptree is capable of supporting fast color clears, set
859 * fast_clear_state appropriately to ensure that fast clears will occur.
860 * Allocation of the MCS miptree will be deferred until the first fast
861 * clear actually occurs or when compressed single sampled buffer is
862 * written by the GPU for the first time.
864 if (intel_tiling_supports_non_msrt_mcs(brw
, mt
->tiling
) &&
865 intel_miptree_supports_non_msrt_fast_clear(brw
, mt
)) {
866 mt
->aux_disable
&= ~INTEL_AUX_DISABLE_CCS
;
867 assert(brw
->gen
< 8 || mt
->halign
== 16 || num_samples
<= 1);
869 /* On Gen9+ clients are not currently capable of consuming compressed
870 * single-sampled buffers. Disabling compression allows us to skip
873 const bool lossless_compression_disabled
= INTEL_DEBUG
& DEBUG_NO_RBC
;
874 const bool is_lossless_compressed
=
875 unlikely(!lossless_compression_disabled
) &&
876 brw
->gen
>= 9 && !mt
->is_scanout
&&
877 intel_miptree_supports_lossless_compressed(brw
, mt
);
879 if (is_lossless_compressed
) {
880 intel_miptree_alloc_non_msrt_mcs(brw
, mt
, is_lossless_compressed
);
887 struct intel_mipmap_tree
*
888 intel_miptree_create_for_bo(struct brw_context
*brw
,
896 uint32_t layout_flags
)
898 struct intel_mipmap_tree
*mt
;
899 uint32_t tiling
, swizzle
;
902 brw_bo_get_tiling(bo
, &tiling
, &swizzle
);
904 /* Nothing will be able to use this miptree with the BO if the offset isn't
907 if (tiling
!= I915_TILING_NONE
)
908 assert(offset
% 4096 == 0);
910 /* miptrees can't handle negative pitch. If you need flipping of images,
911 * that's outside of the scope of the mt.
915 target
= depth
> 1 ? GL_TEXTURE_2D_ARRAY
: GL_TEXTURE_2D
;
917 /* The BO already has a tiling format and we shouldn't confuse the lower
918 * layers by making it try to find a tiling format again.
920 assert((layout_flags
& MIPTREE_LAYOUT_TILING_ANY
) == 0);
921 assert((layout_flags
& MIPTREE_LAYOUT_TILING_NONE
) == 0);
923 layout_flags
|= MIPTREE_LAYOUT_FOR_BO
;
924 mt
= intel_miptree_create_layout(brw
, target
, format
,
926 width
, height
, depth
, 0,
931 brw_bo_reference(bo
);
941 * For a singlesample renderbuffer, this simply wraps the given BO with a
944 * For a multisample renderbuffer, this wraps the window system's
945 * (singlesample) BO with a singlesample miptree attached to the
946 * intel_renderbuffer, then creates a multisample miptree attached to irb->mt
947 * that will contain the actual rendering (which is lazily resolved to
948 * irb->singlesample_mt).
951 intel_update_winsys_renderbuffer_miptree(struct brw_context
*intel
,
952 struct intel_renderbuffer
*irb
,
954 uint32_t width
, uint32_t height
,
957 struct intel_mipmap_tree
*singlesample_mt
= NULL
;
958 struct intel_mipmap_tree
*multisample_mt
= NULL
;
959 struct gl_renderbuffer
*rb
= &irb
->Base
.Base
;
960 mesa_format format
= rb
->Format
;
961 int num_samples
= rb
->NumSamples
;
963 /* Only the front and back buffers, which are color buffers, are allocated
964 * through the image loader.
966 assert(_mesa_get_format_base_format(format
) == GL_RGB
||
967 _mesa_get_format_base_format(format
) == GL_RGBA
);
969 singlesample_mt
= intel_miptree_create_for_bo(intel
,
977 MIPTREE_LAYOUT_FOR_SCANOUT
);
978 if (!singlesample_mt
)
981 /* If this miptree is capable of supporting fast color clears, set
982 * mcs_state appropriately to ensure that fast clears will occur.
983 * Allocation of the MCS miptree will be deferred until the first fast
984 * clear actually occurs.
986 if (intel_tiling_supports_non_msrt_mcs(intel
, singlesample_mt
->tiling
) &&
987 intel_miptree_supports_non_msrt_fast_clear(intel
, singlesample_mt
)) {
988 singlesample_mt
->aux_disable
&= ~INTEL_AUX_DISABLE_CCS
;
991 if (num_samples
== 0) {
992 intel_miptree_release(&irb
->mt
);
993 irb
->mt
= singlesample_mt
;
995 assert(!irb
->singlesample_mt
);
997 intel_miptree_release(&irb
->singlesample_mt
);
998 irb
->singlesample_mt
= singlesample_mt
;
1001 irb
->mt
->logical_width0
!= width
||
1002 irb
->mt
->logical_height0
!= height
) {
1003 multisample_mt
= intel_miptree_create_for_renderbuffer(intel
,
1008 if (!multisample_mt
)
1011 irb
->need_downsample
= false;
1012 intel_miptree_release(&irb
->mt
);
1013 irb
->mt
= multisample_mt
;
1019 intel_miptree_release(&irb
->singlesample_mt
);
1020 intel_miptree_release(&irb
->mt
);
1024 struct intel_mipmap_tree
*
1025 intel_miptree_create_for_renderbuffer(struct brw_context
*brw
,
1029 uint32_t num_samples
)
1031 struct intel_mipmap_tree
*mt
;
1034 GLenum target
= num_samples
> 1 ? GL_TEXTURE_2D_MULTISAMPLE
: GL_TEXTURE_2D
;
1035 const uint32_t layout_flags
= MIPTREE_LAYOUT_ACCELERATED_UPLOAD
|
1036 MIPTREE_LAYOUT_TILING_ANY
|
1037 MIPTREE_LAYOUT_FOR_SCANOUT
;
1039 mt
= intel_miptree_create(brw
, target
, format
, 0, 0,
1040 width
, height
, depth
, num_samples
,
1045 if (intel_miptree_wants_hiz_buffer(brw
, mt
)) {
1046 ok
= intel_miptree_alloc_hiz(brw
, mt
);
1054 intel_miptree_release(&mt
);
1059 intel_miptree_reference(struct intel_mipmap_tree
**dst
,
1060 struct intel_mipmap_tree
*src
)
1065 intel_miptree_release(dst
);
1069 DBG("%s %p refcount now %d\n", __func__
, src
, src
->refcount
);
1076 intel_miptree_aux_buffer_free(struct intel_miptree_aux_buffer
*aux_buf
)
1078 if (aux_buf
== NULL
)
1081 brw_bo_unreference(aux_buf
->bo
);
1087 intel_miptree_release(struct intel_mipmap_tree
**mt
)
1092 DBG("%s %p refcount will be %d\n", __func__
, *mt
, (*mt
)->refcount
- 1);
1093 if (--(*mt
)->refcount
<= 0) {
1096 DBG("%s deleting %p\n", __func__
, *mt
);
1098 brw_bo_unreference((*mt
)->bo
);
1099 intel_miptree_release(&(*mt
)->stencil_mt
);
1100 intel_miptree_release(&(*mt
)->r8stencil_mt
);
1101 intel_miptree_aux_buffer_free((*mt
)->hiz_buf
);
1102 intel_miptree_aux_buffer_free((*mt
)->mcs_buf
);
1103 free_aux_state_map((*mt
)->aux_state
);
1105 intel_miptree_release(&(*mt
)->plane
[0]);
1106 intel_miptree_release(&(*mt
)->plane
[1]);
1108 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
1109 free((*mt
)->level
[i
].slice
);
1119 intel_get_image_dims(struct gl_texture_image
*image
,
1120 int *width
, int *height
, int *depth
)
1122 switch (image
->TexObject
->Target
) {
1123 case GL_TEXTURE_1D_ARRAY
:
1124 /* For a 1D Array texture the OpenGL API will treat the image height as
1125 * the number of array slices. For Intel hardware, we treat the 1D array
1126 * as a 2D Array with a height of 1. So, here we want to swap image
1129 assert(image
->Depth
== 1);
1130 *width
= image
->Width
;
1132 *depth
= image
->Height
;
1134 case GL_TEXTURE_CUBE_MAP
:
1135 /* For Cube maps, the mesa/main api layer gives us a depth of 1 even
1136 * though we really have 6 slices.
1138 assert(image
->Depth
== 1);
1139 *width
= image
->Width
;
1140 *height
= image
->Height
;
1144 *width
= image
->Width
;
1145 *height
= image
->Height
;
1146 *depth
= image
->Depth
;
1152 * Can the image be pulled into a unified mipmap tree? This mirrors
1153 * the completeness test in a lot of ways.
1155 * Not sure whether I want to pass gl_texture_image here.
1158 intel_miptree_match_image(struct intel_mipmap_tree
*mt
,
1159 struct gl_texture_image
*image
)
1161 struct intel_texture_image
*intelImage
= intel_texture_image(image
);
1162 GLuint level
= intelImage
->base
.Base
.Level
;
1163 int width
, height
, depth
;
1165 /* glTexImage* choose the texture object based on the target passed in, and
1166 * objects can't change targets over their lifetimes, so this should be
1169 assert(image
->TexObject
->Target
== mt
->target
);
1171 mesa_format mt_format
= mt
->format
;
1172 if (mt
->format
== MESA_FORMAT_Z24_UNORM_X8_UINT
&& mt
->stencil_mt
)
1173 mt_format
= MESA_FORMAT_Z24_UNORM_S8_UINT
;
1174 if (mt
->format
== MESA_FORMAT_Z_FLOAT32
&& mt
->stencil_mt
)
1175 mt_format
= MESA_FORMAT_Z32_FLOAT_S8X24_UINT
;
1176 if (mt
->etc_format
!= MESA_FORMAT_NONE
)
1177 mt_format
= mt
->etc_format
;
1179 if (image
->TexFormat
!= mt_format
)
1182 intel_get_image_dims(image
, &width
, &height
, &depth
);
1184 if (mt
->target
== GL_TEXTURE_CUBE_MAP
)
1187 if (mt
->surf
.size
> 0) {
1188 if (level
>= mt
->surf
.levels
)
1191 const unsigned level_depth
=
1192 mt
->surf
.dim
== ISL_SURF_DIM_3D
?
1193 minify(mt
->surf
.logical_level0_px
.depth
, level
) :
1194 mt
->surf
.logical_level0_px
.array_len
;
1196 return width
== minify(mt
->surf
.logical_level0_px
.width
, level
) &&
1197 height
== minify(mt
->surf
.logical_level0_px
.height
, level
) &&
1198 depth
== level_depth
&&
1199 MAX2(image
->NumSamples
, 1) == mt
->surf
.samples
;
1202 int level_depth
= mt
->level
[level
].depth
;
1203 if (mt
->num_samples
> 1) {
1204 switch (mt
->msaa_layout
) {
1205 case INTEL_MSAA_LAYOUT_NONE
:
1206 case INTEL_MSAA_LAYOUT_IMS
:
1208 case INTEL_MSAA_LAYOUT_UMS
:
1209 case INTEL_MSAA_LAYOUT_CMS
:
1210 level_depth
/= mt
->num_samples
;
1215 /* Test image dimensions against the base level image adjusted for
1216 * minification. This will also catch images not present in the
1217 * tree, changed targets, etc.
1219 if (width
!= minify(mt
->logical_width0
, level
- mt
->first_level
) ||
1220 height
!= minify(mt
->logical_height0
, level
- mt
->first_level
) ||
1221 depth
!= level_depth
) {
1225 if (image
->NumSamples
!= mt
->num_samples
)
1233 intel_miptree_set_level_info(struct intel_mipmap_tree
*mt
,
1235 GLuint x
, GLuint y
, GLuint d
)
1237 mt
->level
[level
].depth
= d
;
1238 mt
->level
[level
].level_x
= x
;
1239 mt
->level
[level
].level_y
= y
;
1241 DBG("%s level %d, depth %d, offset %d,%d\n", __func__
,
1244 assert(mt
->level
[level
].slice
);
1246 mt
->level
[level
].slice
[0].x_offset
= mt
->level
[level
].level_x
;
1247 mt
->level
[level
].slice
[0].y_offset
= mt
->level
[level
].level_y
;
1252 intel_miptree_set_image_offset(struct intel_mipmap_tree
*mt
,
1253 GLuint level
, GLuint img
,
1256 if (img
== 0 && level
== 0)
1257 assert(x
== 0 && y
== 0);
1259 assert(img
< mt
->level
[level
].depth
);
1261 mt
->level
[level
].slice
[img
].x_offset
= mt
->level
[level
].level_x
+ x
;
1262 mt
->level
[level
].slice
[img
].y_offset
= mt
->level
[level
].level_y
+ y
;
1264 DBG("%s level %d img %d pos %d,%d\n",
1265 __func__
, level
, img
,
1266 mt
->level
[level
].slice
[img
].x_offset
,
1267 mt
->level
[level
].slice
[img
].y_offset
);
1271 intel_miptree_get_image_offset(const struct intel_mipmap_tree
*mt
,
1272 GLuint level
, GLuint slice
,
1273 GLuint
*x
, GLuint
*y
)
1275 if (mt
->surf
.size
> 0) {
1276 uint32_t x_offset_sa
, y_offset_sa
;
1278 /* Given level is relative to level zero while the miptree may be
1279 * represent just a subset of all levels starting from 'first_level'.
1281 assert(level
>= mt
->first_level
);
1282 level
-= mt
->first_level
;
1284 const unsigned z
= mt
->surf
.dim
== ISL_SURF_DIM_3D
? slice
: 0;
1285 slice
= mt
->surf
.dim
== ISL_SURF_DIM_3D
? 0 : slice
;
1286 isl_surf_get_image_offset_sa(&mt
->surf
, level
, slice
, z
,
1287 &x_offset_sa
, &y_offset_sa
);
1294 assert(slice
< mt
->level
[level
].depth
);
1296 *x
= mt
->level
[level
].slice
[slice
].x_offset
;
1297 *y
= mt
->level
[level
].slice
[slice
].y_offset
;
1302 * This function computes the tile_w (in bytes) and tile_h (in rows) of
1303 * different tiling patterns. If the BO is untiled, tile_w is set to cpp
1304 * and tile_h is set to 1.
1307 intel_get_tile_dims(uint32_t tiling
, uint32_t cpp
,
1308 uint32_t *tile_w
, uint32_t *tile_h
)
1319 case I915_TILING_NONE
:
1324 unreachable("not reached");
1330 * This function computes masks that may be used to select the bits of the X
1331 * and Y coordinates that indicate the offset within a tile. If the BO is
1332 * untiled, the masks are set to 0.
1335 intel_get_tile_masks(uint32_t tiling
, uint32_t cpp
,
1336 uint32_t *mask_x
, uint32_t *mask_y
)
1338 uint32_t tile_w_bytes
, tile_h
;
1340 intel_get_tile_dims(tiling
, cpp
, &tile_w_bytes
, &tile_h
);
1342 *mask_x
= tile_w_bytes
/ cpp
- 1;
1343 *mask_y
= tile_h
- 1;
1347 * Compute the offset (in bytes) from the start of the BO to the given x
1348 * and y coordinate. For tiled BOs, caller must ensure that x and y are
1349 * multiples of the tile size.
1352 intel_miptree_get_aligned_offset(const struct intel_mipmap_tree
*mt
,
1353 uint32_t x
, uint32_t y
)
1356 uint32_t pitch
= mt
->pitch
;
1357 uint32_t tiling
= mt
->tiling
;
1361 unreachable("not reached");
1362 case I915_TILING_NONE
:
1363 return y
* pitch
+ x
* cpp
;
1365 assert((x
% (512 / cpp
)) == 0);
1366 assert((y
% 8) == 0);
1367 return y
* pitch
+ x
/ (512 / cpp
) * 4096;
1369 assert((x
% (128 / cpp
)) == 0);
1370 assert((y
% 32) == 0);
1371 return y
* pitch
+ x
/ (128 / cpp
) * 4096;
1376 * Rendering with tiled buffers requires that the base address of the buffer
1377 * be aligned to a page boundary. For renderbuffers, and sometimes with
1378 * textures, we may want the surface to point at a texture image level that
1379 * isn't at a page boundary.
1381 * This function returns an appropriately-aligned base offset
1382 * according to the tiling restrictions, plus any required x/y offset
1386 intel_miptree_get_tile_offsets(const struct intel_mipmap_tree
*mt
,
1387 GLuint level
, GLuint slice
,
1392 uint32_t mask_x
, mask_y
;
1394 intel_get_tile_masks(mt
->tiling
, mt
->cpp
, &mask_x
, &mask_y
);
1395 intel_miptree_get_image_offset(mt
, level
, slice
, &x
, &y
);
1397 *tile_x
= x
& mask_x
;
1398 *tile_y
= y
& mask_y
;
1400 return intel_miptree_get_aligned_offset(mt
, x
& ~mask_x
, y
& ~mask_y
);
1404 intel_miptree_copy_slice_sw(struct brw_context
*brw
,
1405 struct intel_mipmap_tree
*src_mt
,
1406 unsigned src_level
, unsigned src_layer
,
1407 struct intel_mipmap_tree
*dst_mt
,
1408 unsigned dst_level
, unsigned dst_layer
,
1409 unsigned width
, unsigned height
)
1412 ptrdiff_t src_stride
, dst_stride
;
1413 const unsigned cpp
= dst_mt
->surf
.size
> 0 ?
1414 (isl_format_get_layout(dst_mt
->surf
.format
)->bpb
/ 8) : dst_mt
->cpp
;
1416 intel_miptree_map(brw
, src_mt
,
1417 src_level
, src_layer
,
1420 GL_MAP_READ_BIT
| BRW_MAP_DIRECT_BIT
,
1423 intel_miptree_map(brw
, dst_mt
,
1424 dst_level
, dst_layer
,
1427 GL_MAP_WRITE_BIT
| GL_MAP_INVALIDATE_RANGE_BIT
|
1431 DBG("sw blit %s mt %p %p/%"PRIdPTR
" -> %s mt %p %p/%"PRIdPTR
" (%dx%d)\n",
1432 _mesa_get_format_name(src_mt
->format
),
1433 src_mt
, src
, src_stride
,
1434 _mesa_get_format_name(dst_mt
->format
),
1435 dst_mt
, dst
, dst_stride
,
1438 int row_size
= cpp
* width
;
1439 if (src_stride
== row_size
&&
1440 dst_stride
== row_size
) {
1441 memcpy(dst
, src
, row_size
* height
);
1443 for (int i
= 0; i
< height
; i
++) {
1444 memcpy(dst
, src
, row_size
);
1450 intel_miptree_unmap(brw
, dst_mt
, dst_level
, dst_layer
);
1451 intel_miptree_unmap(brw
, src_mt
, src_level
, src_layer
);
1453 /* Don't forget to copy the stencil data over, too. We could have skipped
1454 * passing BRW_MAP_DIRECT_BIT, but that would have meant intel_miptree_map
1455 * shuffling the two data sources in/out of temporary storage instead of
1456 * the direct mapping we get this way.
1458 if (dst_mt
->stencil_mt
) {
1459 assert(src_mt
->stencil_mt
);
1460 intel_miptree_copy_slice_sw(brw
,
1461 src_mt
->stencil_mt
, src_level
, src_layer
,
1462 dst_mt
->stencil_mt
, dst_level
, dst_layer
,
1468 intel_miptree_copy_slice(struct brw_context
*brw
,
1469 struct intel_mipmap_tree
*src_mt
,
1470 unsigned src_level
, unsigned src_layer
,
1471 struct intel_mipmap_tree
*dst_mt
,
1472 unsigned dst_level
, unsigned dst_layer
)
1475 mesa_format format
= src_mt
->format
;
1476 uint32_t width
, height
;
1478 if (src_mt
->surf
.size
> 0) {
1479 width
= minify(src_mt
->surf
.phys_level0_sa
.width
,
1480 src_level
- src_mt
->first_level
);
1481 height
= minify(src_mt
->surf
.phys_level0_sa
.height
,
1482 src_level
- src_mt
->first_level
);
1484 if (src_mt
->surf
.dim
== ISL_SURF_DIM_3D
)
1485 assert(src_layer
< minify(src_mt
->surf
.phys_level0_sa
.depth
,
1486 src_level
- src_mt
->first_level
));
1488 assert(src_layer
< src_mt
->surf
.phys_level0_sa
.array_len
);
1490 width
= minify(src_mt
->physical_width0
,
1491 src_level
- src_mt
->first_level
);
1492 height
= minify(src_mt
->physical_height0
,
1493 src_level
- src_mt
->first_level
);
1494 assert(src_layer
< src_mt
->level
[src_level
].depth
);
1497 assert(src_mt
->format
== dst_mt
->format
);
1499 if (dst_mt
->compressed
) {
1501 _mesa_get_format_block_size(dst_mt
->format
, &i
, &j
);
1502 height
= ALIGN_NPOT(height
, j
) / j
;
1503 width
= ALIGN_NPOT(width
, i
) / i
;
1506 /* If it's a packed depth/stencil buffer with separate stencil, the blit
1507 * below won't apply since we can't do the depth's Y tiling or the
1508 * stencil's W tiling in the blitter.
1510 if (src_mt
->stencil_mt
) {
1511 intel_miptree_copy_slice_sw(brw
,
1512 src_mt
, src_level
, src_layer
,
1513 dst_mt
, dst_level
, dst_layer
,
1518 uint32_t dst_x
, dst_y
, src_x
, src_y
;
1519 intel_miptree_get_image_offset(dst_mt
, dst_level
, dst_layer
,
1521 intel_miptree_get_image_offset(src_mt
, src_level
, src_layer
,
1524 DBG("validate blit mt %s %p %d,%d/%d -> mt %s %p %d,%d/%d (%dx%d)\n",
1525 _mesa_get_format_name(src_mt
->format
),
1526 src_mt
, src_x
, src_y
, src_mt
->pitch
,
1527 _mesa_get_format_name(dst_mt
->format
),
1528 dst_mt
, dst_x
, dst_y
, dst_mt
->pitch
,
1531 if (!intel_miptree_blit(brw
,
1532 src_mt
, src_level
, src_layer
, 0, 0, false,
1533 dst_mt
, dst_level
, dst_layer
, 0, 0, false,
1534 width
, height
, GL_COPY
)) {
1535 perf_debug("miptree validate blit for %s failed\n",
1536 _mesa_get_format_name(format
));
1538 intel_miptree_copy_slice_sw(brw
,
1539 src_mt
, src_level
, src_layer
,
1540 dst_mt
, dst_level
, dst_layer
,
1546 * Copies the image's current data to the given miptree, and associates that
1547 * miptree with the image.
1549 * If \c invalidate is true, then the actual image data does not need to be
1550 * copied, but the image still needs to be associated to the new miptree (this
1551 * is set to true if we're about to clear the image).
1554 intel_miptree_copy_teximage(struct brw_context
*brw
,
1555 struct intel_texture_image
*intelImage
,
1556 struct intel_mipmap_tree
*dst_mt
,
1559 struct intel_mipmap_tree
*src_mt
= intelImage
->mt
;
1560 struct intel_texture_object
*intel_obj
=
1561 intel_texture_object(intelImage
->base
.Base
.TexObject
);
1562 int level
= intelImage
->base
.Base
.Level
;
1563 const unsigned face
= intelImage
->base
.Base
.Face
;
1564 unsigned start_layer
, end_layer
;
1566 if (intel_obj
->base
.Target
== GL_TEXTURE_1D_ARRAY
) {
1568 assert(intelImage
->base
.Base
.Height
);
1570 end_layer
= intelImage
->base
.Base
.Height
- 1;
1571 } else if (face
> 0) {
1575 assert(intelImage
->base
.Base
.Depth
);
1577 end_layer
= intelImage
->base
.Base
.Depth
- 1;
1581 for (unsigned i
= start_layer
; i
<= end_layer
; i
++) {
1582 intel_miptree_copy_slice(brw
,
1588 intel_miptree_reference(&intelImage
->mt
, dst_mt
);
1589 intel_obj
->needs_validate
= true;
1593 intel_miptree_init_mcs(struct brw_context
*brw
,
1594 struct intel_mipmap_tree
*mt
,
1597 assert(mt
->mcs_buf
!= NULL
);
1599 /* From the Ivy Bridge PRM, Vol 2 Part 1 p326:
1601 * When MCS buffer is enabled and bound to MSRT, it is required that it
1602 * is cleared prior to any rendering.
1604 * Since we don't use the MCS buffer for any purpose other than rendering,
1605 * it makes sense to just clear it immediately upon allocation.
1607 * Note: the clear value for MCS buffers is all 1's, so we memset to 0xff.
1609 void *map
= brw_bo_map(brw
, mt
->mcs_buf
->bo
, MAP_WRITE
);
1610 if (unlikely(map
== NULL
)) {
1611 fprintf(stderr
, "Failed to map mcs buffer into GTT\n");
1612 brw_bo_unreference(mt
->mcs_buf
->bo
);
1617 memset(data
, init_value
, mt
->mcs_buf
->size
);
1618 brw_bo_unmap(mt
->mcs_buf
->bo
);
1621 static struct intel_miptree_aux_buffer
*
1622 intel_alloc_aux_buffer(struct brw_context
*brw
,
1624 const struct isl_surf
*aux_surf
,
1625 uint32_t alloc_flags
,
1626 struct intel_mipmap_tree
*mt
)
1628 struct intel_miptree_aux_buffer
*buf
= calloc(sizeof(*buf
), 1);
1632 buf
->size
= aux_surf
->size
;
1633 buf
->pitch
= aux_surf
->row_pitch
;
1634 buf
->qpitch
= isl_surf_get_array_pitch_sa_rows(aux_surf
);
1636 /* ISL has stricter set of alignment rules then the drm allocator.
1637 * Therefore one can pass the ISL dimensions in terms of bytes instead of
1638 * trying to recalculate based on different format block sizes.
1640 buf
->bo
= brw_bo_alloc_tiled(brw
->bufmgr
, name
, buf
->size
,
1641 I915_TILING_Y
, buf
->pitch
, alloc_flags
);
1647 buf
->surf
= *aux_surf
;
1653 intel_miptree_alloc_mcs(struct brw_context
*brw
,
1654 struct intel_mipmap_tree
*mt
,
1657 assert(brw
->gen
>= 7); /* MCS only used on Gen7+ */
1658 assert(mt
->mcs_buf
== NULL
);
1659 assert((mt
->aux_disable
& INTEL_AUX_DISABLE_MCS
) == 0);
1661 /* Multisampled miptrees are only supported for single level. */
1662 assert(mt
->first_level
== 0);
1663 enum isl_aux_state
**aux_state
=
1664 create_aux_state_map(mt
, ISL_AUX_STATE_CLEAR
);
1668 struct isl_surf temp_main_surf
;
1669 struct isl_surf temp_mcs_surf
;
1671 /* Create first an ISL presentation for the main color surface and let ISL
1672 * calculate equivalent MCS surface against it.
1674 intel_miptree_get_isl_surf(brw
, mt
, &temp_main_surf
);
1675 MAYBE_UNUSED
bool ok
=
1676 isl_surf_get_mcs_surf(&brw
->isl_dev
, &temp_main_surf
, &temp_mcs_surf
);
1679 /* Buffer needs to be initialised requiring the buffer to be immediately
1680 * mapped to cpu space for writing. Therefore do not use the gpu access
1681 * flag which can cause an unnecessary delay if the backing pages happened
1682 * to be just used by the GPU.
1684 const uint32_t alloc_flags
= 0;
1685 mt
->mcs_buf
= intel_alloc_aux_buffer(brw
, "mcs-miptree",
1686 &temp_mcs_surf
, alloc_flags
, mt
);
1692 mt
->aux_state
= aux_state
;
1694 intel_miptree_init_mcs(brw
, mt
, 0xFF);
1700 intel_miptree_alloc_non_msrt_mcs(struct brw_context
*brw
,
1701 struct intel_mipmap_tree
*mt
,
1702 bool is_lossless_compressed
)
1704 assert(mt
->mcs_buf
== NULL
);
1705 assert(!(mt
->aux_disable
& (INTEL_AUX_DISABLE_MCS
| INTEL_AUX_DISABLE_CCS
)));
1707 struct isl_surf temp_main_surf
;
1708 struct isl_surf temp_ccs_surf
;
1710 /* Create first an ISL presentation for the main color surface and let ISL
1711 * calculate equivalent CCS surface against it.
1713 intel_miptree_get_isl_surf(brw
, mt
, &temp_main_surf
);
1714 if (!isl_surf_get_ccs_surf(&brw
->isl_dev
, &temp_main_surf
, &temp_ccs_surf
))
1717 assert(temp_ccs_surf
.size
&&
1718 (temp_ccs_surf
.size
% temp_ccs_surf
.row_pitch
== 0));
1720 enum isl_aux_state
**aux_state
=
1721 create_aux_state_map(mt
, ISL_AUX_STATE_PASS_THROUGH
);
1725 /* In case of compression mcs buffer needs to be initialised requiring the
1726 * buffer to be immediately mapped to cpu space for writing. Therefore do
1727 * not use the gpu access flag which can cause an unnecessary delay if the
1728 * backing pages happened to be just used by the GPU.
1730 const uint32_t alloc_flags
=
1731 is_lossless_compressed
? 0 : BO_ALLOC_FOR_RENDER
;
1732 mt
->mcs_buf
= intel_alloc_aux_buffer(brw
, "ccs-miptree",
1733 &temp_ccs_surf
, alloc_flags
, mt
);
1739 mt
->aux_state
= aux_state
;
1741 /* From Gen9 onwards single-sampled (non-msrt) auxiliary buffers are
1742 * used for lossless compression which requires similar initialisation
1743 * as multi-sample compression.
1745 if (is_lossless_compressed
) {
1746 /* Hardware sets the auxiliary buffer to all zeroes when it does full
1747 * resolve. Initialize it accordingly in case the first renderer is
1748 * cpu (or other none compression aware party).
1750 * This is also explicitly stated in the spec (MCS Buffer for Render
1752 * "If Software wants to enable Color Compression without Fast clear,
1753 * Software needs to initialize MCS with zeros."
1755 intel_miptree_init_mcs(brw
, mt
, 0);
1756 mt
->msaa_layout
= INTEL_MSAA_LAYOUT_CMS
;
1763 * Helper for intel_miptree_alloc_hiz() that sets
1764 * \c mt->level[level].has_hiz. Return true if and only if
1765 * \c has_hiz was set.
1768 intel_miptree_level_enable_hiz(struct brw_context
*brw
,
1769 struct intel_mipmap_tree
*mt
,
1772 assert(mt
->hiz_buf
);
1774 if (brw
->gen
>= 8 || brw
->is_haswell
) {
1775 uint32_t width
= minify(mt
->physical_width0
, level
);
1776 uint32_t height
= minify(mt
->physical_height0
, level
);
1778 /* Disable HiZ for LOD > 0 unless the width is 8 aligned
1779 * and the height is 4 aligned. This allows our HiZ support
1780 * to fulfill Haswell restrictions for HiZ ops. For LOD == 0,
1781 * we can grow the width & height to allow the HiZ op to
1782 * force the proper size alignments.
1784 if (level
> 0 && ((width
& 7) || (height
& 3))) {
1785 DBG("mt %p level %d: HiZ DISABLED\n", mt
, level
);
1790 DBG("mt %p level %d: HiZ enabled\n", mt
, level
);
1791 mt
->level
[level
].has_hiz
= true;
1796 intel_miptree_wants_hiz_buffer(struct brw_context
*brw
,
1797 struct intel_mipmap_tree
*mt
)
1802 if (mt
->hiz_buf
!= NULL
)
1805 if (mt
->aux_disable
& INTEL_AUX_DISABLE_HIZ
)
1808 switch (mt
->format
) {
1809 case MESA_FORMAT_Z_FLOAT32
:
1810 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT
:
1811 case MESA_FORMAT_Z24_UNORM_X8_UINT
:
1812 case MESA_FORMAT_Z24_UNORM_S8_UINT
:
1813 case MESA_FORMAT_Z_UNORM16
:
1821 intel_miptree_alloc_hiz(struct brw_context
*brw
,
1822 struct intel_mipmap_tree
*mt
)
1824 assert(mt
->hiz_buf
== NULL
);
1825 assert((mt
->aux_disable
& INTEL_AUX_DISABLE_HIZ
) == 0);
1827 enum isl_aux_state
**aux_state
=
1828 create_aux_state_map(mt
, ISL_AUX_STATE_AUX_INVALID
);
1832 struct isl_surf temp_main_surf
;
1833 struct isl_surf temp_hiz_surf
;
1835 intel_miptree_get_isl_surf(brw
, mt
, &temp_main_surf
);
1836 MAYBE_UNUSED
bool ok
=
1837 isl_surf_get_hiz_surf(&brw
->isl_dev
, &temp_main_surf
, &temp_hiz_surf
);
1840 const uint32_t alloc_flags
= BO_ALLOC_FOR_RENDER
;
1841 mt
->hiz_buf
= intel_alloc_aux_buffer(brw
, "hiz-miptree",
1842 &temp_hiz_surf
, alloc_flags
, mt
);
1849 for (unsigned level
= mt
->first_level
; level
<= mt
->last_level
; ++level
)
1850 intel_miptree_level_enable_hiz(brw
, mt
, level
);
1852 mt
->aux_state
= aux_state
;
1858 * Can the miptree sample using the hiz buffer?
1861 intel_miptree_sample_with_hiz(struct brw_context
*brw
,
1862 struct intel_mipmap_tree
*mt
)
1864 /* It's unclear how well supported sampling from the hiz buffer is on GEN8,
1865 * so keep things conservative for now and never enable it unless we're SKL+.
1875 /* It seems the hardware won't fallback to the depth buffer if some of the
1876 * mipmap levels aren't available in the HiZ buffer. So we need all levels
1877 * of the texture to be HiZ enabled.
1879 for (unsigned level
= mt
->first_level
; level
<= mt
->last_level
; ++level
) {
1880 if (!intel_miptree_level_has_hiz(mt
, level
))
1884 /* If compressed multisampling is enabled, then we use it for the auxiliary
1887 * From the BDW PRM (Volume 2d: Command Reference: Structures
1888 * RENDER_SURFACE_STATE.AuxiliarySurfaceMode):
1890 * "If this field is set to AUX_HIZ, Number of Multisamples must be
1891 * MULTISAMPLECOUNT_1, and Surface Type cannot be SURFTYPE_3D.
1893 * There is no such blurb for 1D textures, but there is sufficient evidence
1894 * that this is broken on SKL+.
1896 return (mt
->num_samples
<= 1 &&
1897 mt
->target
!= GL_TEXTURE_3D
&&
1898 mt
->target
!= GL_TEXTURE_1D
/* gen9+ restriction */);
1902 * Does the miptree slice have hiz enabled?
1905 intel_miptree_level_has_hiz(const struct intel_mipmap_tree
*mt
, uint32_t level
)
1907 intel_miptree_check_level_layer(mt
, level
, 0);
1908 return mt
->level
[level
].has_hiz
;
1912 intel_miptree_has_color_unresolved(const struct intel_mipmap_tree
*mt
,
1913 unsigned start_level
, unsigned num_levels
,
1914 unsigned start_layer
, unsigned num_layers
)
1916 assert(_mesa_is_format_color_format(mt
->format
));
1921 /* Clamp the level range to fit the miptree */
1922 assert(start_level
+ num_levels
>= start_level
);
1923 const uint32_t last_level
=
1924 MIN2(mt
->last_level
, start_level
+ num_levels
- 1);
1925 start_level
= MAX2(mt
->first_level
, start_level
);
1926 num_levels
= last_level
- start_level
+ 1;
1928 for (uint32_t level
= start_level
; level
<= last_level
; level
++) {
1929 const uint32_t level_layers
= MIN2(num_layers
, mt
->level
[level
].depth
);
1930 for (unsigned a
= 0; a
< level_layers
; a
++) {
1931 enum isl_aux_state aux_state
=
1932 intel_miptree_get_aux_state(mt
, level
, start_layer
+ a
);
1933 assert(aux_state
!= ISL_AUX_STATE_AUX_INVALID
);
1934 if (aux_state
!= ISL_AUX_STATE_PASS_THROUGH
)
1943 intel_miptree_check_color_resolve(const struct brw_context
*brw
,
1944 const struct intel_mipmap_tree
*mt
,
1945 unsigned level
, unsigned layer
)
1948 if ((mt
->aux_disable
& INTEL_AUX_DISABLE_CCS
) || !mt
->mcs_buf
)
1951 /* Fast color clear is supported for mipmapped surfaces only on Gen8+. */
1952 assert(brw
->gen
>= 8 ||
1953 (level
== 0 && mt
->first_level
== 0 && mt
->last_level
== 0));
1955 /* Compression of arrayed msaa surfaces is supported. */
1956 if (mt
->num_samples
> 1)
1959 /* Fast color clear is supported for non-msaa arrays only on Gen8+. */
1960 assert(brw
->gen
>= 8 || (layer
== 0 && mt
->logical_depth0
== 1));
1966 static enum blorp_fast_clear_op
1967 get_ccs_d_resolve_op(enum isl_aux_state aux_state
,
1968 bool ccs_supported
, bool fast_clear_supported
)
1970 assert(ccs_supported
== fast_clear_supported
);
1972 switch (aux_state
) {
1973 case ISL_AUX_STATE_CLEAR
:
1974 case ISL_AUX_STATE_COMPRESSED_CLEAR
:
1976 return BLORP_FAST_CLEAR_OP_RESOLVE_FULL
;
1978 return BLORP_FAST_CLEAR_OP_NONE
;
1980 case ISL_AUX_STATE_PASS_THROUGH
:
1981 return BLORP_FAST_CLEAR_OP_NONE
;
1983 case ISL_AUX_STATE_RESOLVED
:
1984 case ISL_AUX_STATE_AUX_INVALID
:
1985 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR
:
1989 unreachable("Invalid aux state for CCS_D");
1992 static enum blorp_fast_clear_op
1993 get_ccs_e_resolve_op(enum isl_aux_state aux_state
,
1994 bool ccs_supported
, bool fast_clear_supported
)
1996 switch (aux_state
) {
1997 case ISL_AUX_STATE_CLEAR
:
1998 case ISL_AUX_STATE_COMPRESSED_CLEAR
:
2000 return BLORP_FAST_CLEAR_OP_RESOLVE_FULL
;
2001 else if (!fast_clear_supported
)
2002 return BLORP_FAST_CLEAR_OP_RESOLVE_PARTIAL
;
2004 return BLORP_FAST_CLEAR_OP_NONE
;
2006 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR
:
2008 return BLORP_FAST_CLEAR_OP_RESOLVE_FULL
;
2010 return BLORP_FAST_CLEAR_OP_NONE
;
2012 case ISL_AUX_STATE_PASS_THROUGH
:
2013 return BLORP_FAST_CLEAR_OP_NONE
;
2015 case ISL_AUX_STATE_RESOLVED
:
2016 case ISL_AUX_STATE_AUX_INVALID
:
2020 unreachable("Invalid aux state for CCS_E");
2024 intel_miptree_prepare_ccs_access(struct brw_context
*brw
,
2025 struct intel_mipmap_tree
*mt
,
2026 uint32_t level
, uint32_t layer
,
2028 bool fast_clear_supported
)
2030 enum isl_aux_state aux_state
= intel_miptree_get_aux_state(mt
, level
, layer
);
2032 enum blorp_fast_clear_op resolve_op
;
2033 if (intel_miptree_is_lossless_compressed(brw
, mt
)) {
2034 resolve_op
= get_ccs_e_resolve_op(aux_state
, aux_supported
,
2035 fast_clear_supported
);
2037 resolve_op
= get_ccs_d_resolve_op(aux_state
, aux_supported
,
2038 fast_clear_supported
);
2041 if (resolve_op
!= BLORP_FAST_CLEAR_OP_NONE
) {
2042 intel_miptree_check_color_resolve(brw
, mt
, level
, layer
);
2043 brw_blorp_resolve_color(brw
, mt
, level
, layer
, resolve_op
);
2045 switch (resolve_op
) {
2046 case BLORP_FAST_CLEAR_OP_RESOLVE_FULL
:
2047 /* The CCS full resolve operation destroys the CCS and sets it to the
2048 * pass-through state. (You can also think of this as being both a
2049 * resolve and an ambiguate in one operation.)
2051 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2052 ISL_AUX_STATE_PASS_THROUGH
);
2055 case BLORP_FAST_CLEAR_OP_RESOLVE_PARTIAL
:
2056 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2057 ISL_AUX_STATE_COMPRESSED_NO_CLEAR
);
2061 unreachable("Invalid resolve op");
2067 intel_miptree_finish_ccs_write(struct brw_context
*brw
,
2068 struct intel_mipmap_tree
*mt
,
2069 uint32_t level
, uint32_t layer
,
2070 bool written_with_ccs
)
2072 enum isl_aux_state aux_state
= intel_miptree_get_aux_state(mt
, level
, layer
);
2074 if (intel_miptree_is_lossless_compressed(brw
, mt
)) {
2075 switch (aux_state
) {
2076 case ISL_AUX_STATE_CLEAR
:
2077 assert(written_with_ccs
);
2078 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2079 ISL_AUX_STATE_COMPRESSED_CLEAR
);
2082 case ISL_AUX_STATE_COMPRESSED_CLEAR
:
2083 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR
:
2084 assert(written_with_ccs
);
2085 break; /* Nothing to do */
2087 case ISL_AUX_STATE_PASS_THROUGH
:
2088 if (written_with_ccs
) {
2089 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2090 ISL_AUX_STATE_COMPRESSED_NO_CLEAR
);
2096 case ISL_AUX_STATE_RESOLVED
:
2097 case ISL_AUX_STATE_AUX_INVALID
:
2098 unreachable("Invalid aux state for CCS_E");
2101 /* CCS_D is a bit simpler */
2102 switch (aux_state
) {
2103 case ISL_AUX_STATE_CLEAR
:
2104 assert(written_with_ccs
);
2105 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2106 ISL_AUX_STATE_COMPRESSED_CLEAR
);
2109 case ISL_AUX_STATE_COMPRESSED_CLEAR
:
2110 assert(written_with_ccs
);
2111 break; /* Nothing to do */
2113 case ISL_AUX_STATE_PASS_THROUGH
:
2117 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR
:
2118 case ISL_AUX_STATE_RESOLVED
:
2119 case ISL_AUX_STATE_AUX_INVALID
:
2120 unreachable("Invalid aux state for CCS_D");
2126 intel_miptree_finish_mcs_write(struct brw_context
*brw
,
2127 struct intel_mipmap_tree
*mt
,
2128 uint32_t level
, uint32_t layer
,
2129 bool written_with_aux
)
2131 switch (intel_miptree_get_aux_state(mt
, level
, layer
)) {
2132 case ISL_AUX_STATE_CLEAR
:
2133 assert(written_with_aux
);
2134 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2135 ISL_AUX_STATE_COMPRESSED_CLEAR
);
2138 case ISL_AUX_STATE_COMPRESSED_CLEAR
:
2139 assert(written_with_aux
);
2140 break; /* Nothing to do */
2142 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR
:
2143 case ISL_AUX_STATE_RESOLVED
:
2144 case ISL_AUX_STATE_PASS_THROUGH
:
2145 case ISL_AUX_STATE_AUX_INVALID
:
2146 unreachable("Invalid aux state for MCS");
2151 intel_miptree_prepare_hiz_access(struct brw_context
*brw
,
2152 struct intel_mipmap_tree
*mt
,
2153 uint32_t level
, uint32_t layer
,
2154 bool hiz_supported
, bool fast_clear_supported
)
2156 enum blorp_hiz_op hiz_op
= BLORP_HIZ_OP_NONE
;
2157 switch (intel_miptree_get_aux_state(mt
, level
, layer
)) {
2158 case ISL_AUX_STATE_CLEAR
:
2159 case ISL_AUX_STATE_COMPRESSED_CLEAR
:
2160 if (!hiz_supported
|| !fast_clear_supported
)
2161 hiz_op
= BLORP_HIZ_OP_DEPTH_RESOLVE
;
2164 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR
:
2166 hiz_op
= BLORP_HIZ_OP_DEPTH_RESOLVE
;
2169 case ISL_AUX_STATE_PASS_THROUGH
:
2170 case ISL_AUX_STATE_RESOLVED
:
2173 case ISL_AUX_STATE_AUX_INVALID
:
2175 hiz_op
= BLORP_HIZ_OP_HIZ_RESOLVE
;
2179 if (hiz_op
!= BLORP_HIZ_OP_NONE
) {
2180 intel_hiz_exec(brw
, mt
, level
, layer
, 1, hiz_op
);
2183 case BLORP_HIZ_OP_DEPTH_RESOLVE
:
2184 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2185 ISL_AUX_STATE_RESOLVED
);
2188 case BLORP_HIZ_OP_HIZ_RESOLVE
:
2189 /* The HiZ resolve operation is actually an ambiguate */
2190 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2191 ISL_AUX_STATE_PASS_THROUGH
);
2195 unreachable("Invalid HiZ op");
2201 intel_miptree_finish_hiz_write(struct brw_context
*brw
,
2202 struct intel_mipmap_tree
*mt
,
2203 uint32_t level
, uint32_t layer
,
2204 bool written_with_hiz
)
2206 switch (intel_miptree_get_aux_state(mt
, level
, layer
)) {
2207 case ISL_AUX_STATE_CLEAR
:
2208 assert(written_with_hiz
);
2209 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2210 ISL_AUX_STATE_COMPRESSED_CLEAR
);
2213 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR
:
2214 case ISL_AUX_STATE_COMPRESSED_CLEAR
:
2215 assert(written_with_hiz
);
2216 break; /* Nothing to do */
2218 case ISL_AUX_STATE_RESOLVED
:
2219 if (written_with_hiz
) {
2220 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2221 ISL_AUX_STATE_COMPRESSED_NO_CLEAR
);
2223 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2224 ISL_AUX_STATE_AUX_INVALID
);
2228 case ISL_AUX_STATE_PASS_THROUGH
:
2229 if (written_with_hiz
) {
2230 intel_miptree_set_aux_state(brw
, mt
, level
, layer
, 1,
2231 ISL_AUX_STATE_COMPRESSED_NO_CLEAR
);
2235 case ISL_AUX_STATE_AUX_INVALID
:
2236 assert(!written_with_hiz
);
2241 static inline uint32_t
2242 miptree_level_range_length(const struct intel_mipmap_tree
*mt
,
2243 uint32_t start_level
, uint32_t num_levels
)
2245 assert(start_level
>= mt
->first_level
);
2246 assert(start_level
<= mt
->last_level
);
2248 if (num_levels
== INTEL_REMAINING_LAYERS
)
2249 num_levels
= mt
->last_level
- start_level
+ 1;
2250 /* Check for overflow */
2251 assert(start_level
+ num_levels
>= start_level
);
2252 assert(start_level
+ num_levels
<= mt
->last_level
+ 1);
2257 static inline uint32_t
2258 miptree_layer_range_length(const struct intel_mipmap_tree
*mt
, uint32_t level
,
2259 uint32_t start_layer
, uint32_t num_layers
)
2261 assert(level
<= mt
->last_level
);
2262 uint32_t total_num_layers
;
2264 if (mt
->surf
.size
> 0)
2265 total_num_layers
= mt
->surf
.dim
== ISL_SURF_DIM_3D
?
2266 minify(mt
->surf
.phys_level0_sa
.depth
, level
) :
2267 mt
->surf
.phys_level0_sa
.array_len
;
2269 total_num_layers
= mt
->level
[level
].depth
;
2271 assert(start_layer
< total_num_layers
);
2272 if (num_layers
== INTEL_REMAINING_LAYERS
)
2273 num_layers
= total_num_layers
- start_layer
;
2274 /* Check for overflow */
2275 assert(start_layer
+ num_layers
>= start_layer
);
2276 assert(start_layer
+ num_layers
<= total_num_layers
);
2282 intel_miptree_prepare_access(struct brw_context
*brw
,
2283 struct intel_mipmap_tree
*mt
,
2284 uint32_t start_level
, uint32_t num_levels
,
2285 uint32_t start_layer
, uint32_t num_layers
,
2286 bool aux_supported
, bool fast_clear_supported
)
2288 num_levels
= miptree_level_range_length(mt
, start_level
, num_levels
);
2290 if (_mesa_is_format_color_format(mt
->format
)) {
2294 if (mt
->num_samples
> 1) {
2295 /* Nothing to do for MSAA */
2296 assert(aux_supported
&& fast_clear_supported
);
2298 for (uint32_t l
= 0; l
< num_levels
; l
++) {
2299 const uint32_t level
= start_level
+ l
;
2300 const uint32_t level_layers
=
2301 miptree_layer_range_length(mt
, level
, start_layer
, num_layers
);
2302 for (uint32_t a
= 0; a
< level_layers
; a
++) {
2303 intel_miptree_prepare_ccs_access(brw
, mt
, level
,
2304 start_layer
+ a
, aux_supported
,
2305 fast_clear_supported
);
2309 } else if (mt
->format
== MESA_FORMAT_S_UINT8
) {
2310 /* Nothing to do for stencil */
2315 for (uint32_t l
= 0; l
< num_levels
; l
++) {
2316 const uint32_t level
= start_level
+ l
;
2317 if (!intel_miptree_level_has_hiz(mt
, level
))
2320 const uint32_t level_layers
=
2321 miptree_layer_range_length(mt
, level
, start_layer
, num_layers
);
2322 for (uint32_t a
= 0; a
< level_layers
; a
++) {
2323 intel_miptree_prepare_hiz_access(brw
, mt
, level
, start_layer
+ a
,
2325 fast_clear_supported
);
2332 intel_miptree_finish_write(struct brw_context
*brw
,
2333 struct intel_mipmap_tree
*mt
, uint32_t level
,
2334 uint32_t start_layer
, uint32_t num_layers
,
2335 bool written_with_aux
)
2337 num_layers
= miptree_layer_range_length(mt
, level
, start_layer
, num_layers
);
2339 if (_mesa_is_format_color_format(mt
->format
)) {
2343 if (mt
->num_samples
> 1) {
2344 for (uint32_t a
= 0; a
< num_layers
; a
++) {
2345 intel_miptree_finish_mcs_write(brw
, mt
, level
, start_layer
+ a
,
2349 for (uint32_t a
= 0; a
< num_layers
; a
++) {
2350 intel_miptree_finish_ccs_write(brw
, mt
, level
, start_layer
+ a
,
2354 } else if (mt
->format
== MESA_FORMAT_S_UINT8
) {
2355 /* Nothing to do for stencil */
2357 if (!intel_miptree_level_has_hiz(mt
, level
))
2360 for (uint32_t a
= 0; a
< num_layers
; a
++) {
2361 intel_miptree_finish_hiz_write(brw
, mt
, level
, start_layer
+ a
,
2368 intel_miptree_get_aux_state(const struct intel_mipmap_tree
*mt
,
2369 uint32_t level
, uint32_t layer
)
2371 intel_miptree_check_level_layer(mt
, level
, layer
);
2373 if (_mesa_is_format_color_format(mt
->format
)) {
2374 assert(mt
->mcs_buf
!= NULL
);
2375 assert(mt
->num_samples
<= 1 || mt
->msaa_layout
== INTEL_MSAA_LAYOUT_CMS
);
2376 } else if (mt
->format
== MESA_FORMAT_S_UINT8
) {
2377 unreachable("Cannot get aux state for stencil");
2379 assert(intel_miptree_level_has_hiz(mt
, level
));
2382 return mt
->aux_state
[level
][layer
];
2386 intel_miptree_set_aux_state(struct brw_context
*brw
,
2387 struct intel_mipmap_tree
*mt
, uint32_t level
,
2388 uint32_t start_layer
, uint32_t num_layers
,
2389 enum isl_aux_state aux_state
)
2391 num_layers
= miptree_layer_range_length(mt
, level
, start_layer
, num_layers
);
2393 if (_mesa_is_format_color_format(mt
->format
)) {
2394 assert(mt
->mcs_buf
!= NULL
);
2395 assert(mt
->num_samples
<= 1 || mt
->msaa_layout
== INTEL_MSAA_LAYOUT_CMS
);
2396 } else if (mt
->format
== MESA_FORMAT_S_UINT8
) {
2397 unreachable("Cannot get aux state for stencil");
2399 assert(intel_miptree_level_has_hiz(mt
, level
));
2402 for (unsigned a
= 0; a
< num_layers
; a
++)
2403 mt
->aux_state
[level
][start_layer
+ a
] = aux_state
;
2406 /* On Gen9 color buffers may be compressed by the hardware (lossless
2407 * compression). There are, however, format restrictions and care needs to be
2408 * taken that the sampler engine is capable for re-interpreting a buffer with
2409 * format different the buffer was originally written with.
2411 * For example, SRGB formats are not compressible and the sampler engine isn't
2412 * capable of treating RGBA_UNORM as SRGB_ALPHA. In such a case the underlying
2413 * color buffer needs to be resolved so that the sampling surface can be
2414 * sampled as non-compressed (i.e., without the auxiliary MCS buffer being
2418 can_texture_with_ccs(struct brw_context
*brw
,
2419 struct intel_mipmap_tree
*mt
,
2420 mesa_format view_format
)
2422 if (!intel_miptree_is_lossless_compressed(brw
, mt
))
2425 enum isl_format isl_mt_format
= brw_isl_format_for_mesa_format(mt
->format
);
2426 enum isl_format isl_view_format
= brw_isl_format_for_mesa_format(view_format
);
2428 if (!isl_formats_are_ccs_e_compatible(&brw
->screen
->devinfo
,
2429 isl_mt_format
, isl_view_format
)) {
2430 perf_debug("Incompatible sampling format (%s) for rbc (%s)\n",
2431 _mesa_get_format_name(view_format
),
2432 _mesa_get_format_name(mt
->format
));
2440 intel_miptree_prepare_texture_slices(struct brw_context
*brw
,
2441 struct intel_mipmap_tree
*mt
,
2442 mesa_format view_format
,
2443 uint32_t start_level
, uint32_t num_levels
,
2444 uint32_t start_layer
, uint32_t num_layers
,
2445 bool *aux_supported_out
)
2447 bool aux_supported
, clear_supported
;
2448 if (_mesa_is_format_color_format(mt
->format
)) {
2449 if (mt
->num_samples
> 1) {
2450 aux_supported
= clear_supported
= true;
2452 aux_supported
= can_texture_with_ccs(brw
, mt
, view_format
);
2454 /* Clear color is specified as ints or floats and the conversion is
2455 * done by the sampler. If we have a texture view, we would have to
2456 * perform the clear color conversion manually. Just disable clear
2459 clear_supported
= aux_supported
&& (mt
->format
== view_format
);
2461 } else if (mt
->format
== MESA_FORMAT_S_UINT8
) {
2462 aux_supported
= clear_supported
= false;
2464 aux_supported
= clear_supported
= intel_miptree_sample_with_hiz(brw
, mt
);
2467 intel_miptree_prepare_access(brw
, mt
, start_level
, num_levels
,
2468 start_layer
, num_layers
,
2469 aux_supported
, clear_supported
);
2470 if (aux_supported_out
)
2471 *aux_supported_out
= aux_supported
;
2475 intel_miptree_prepare_texture(struct brw_context
*brw
,
2476 struct intel_mipmap_tree
*mt
,
2477 mesa_format view_format
,
2478 bool *aux_supported_out
)
2480 intel_miptree_prepare_texture_slices(brw
, mt
, view_format
,
2481 0, INTEL_REMAINING_LEVELS
,
2482 0, INTEL_REMAINING_LAYERS
,
2487 intel_miptree_prepare_image(struct brw_context
*brw
,
2488 struct intel_mipmap_tree
*mt
)
2490 /* The data port doesn't understand any compression */
2491 intel_miptree_prepare_access(brw
, mt
, 0, INTEL_REMAINING_LEVELS
,
2492 0, INTEL_REMAINING_LAYERS
, false, false);
2496 intel_miptree_prepare_fb_fetch(struct brw_context
*brw
,
2497 struct intel_mipmap_tree
*mt
, uint32_t level
,
2498 uint32_t start_layer
, uint32_t num_layers
)
2500 intel_miptree_prepare_texture_slices(brw
, mt
, mt
->format
, level
, 1,
2501 start_layer
, num_layers
, NULL
);
2505 intel_miptree_prepare_render(struct brw_context
*brw
,
2506 struct intel_mipmap_tree
*mt
, uint32_t level
,
2507 uint32_t start_layer
, uint32_t layer_count
,
2510 /* If FRAMEBUFFER_SRGB is used on Gen9+ then we need to resolve any of
2511 * the single-sampled color renderbuffers because the CCS buffer isn't
2512 * supported for SRGB formats. This only matters if FRAMEBUFFER_SRGB is
2513 * enabled because otherwise the surface state will be programmed with
2514 * the linear equivalent format anyway.
2516 if (brw
->gen
== 9 && srgb_enabled
&& mt
->num_samples
<= 1 &&
2517 _mesa_get_srgb_format_linear(mt
->format
) != mt
->format
) {
2519 /* Lossless compression is not supported for SRGB formats, it
2520 * should be impossible to get here with such surfaces.
2522 assert(!intel_miptree_is_lossless_compressed(brw
, mt
));
2523 intel_miptree_prepare_access(brw
, mt
, level
, 1, start_layer
, layer_count
,
2527 /* For layered rendering non-compressed fast cleared buffers need to be
2528 * resolved. Surface state can carry only one fast color clear value
2529 * while each layer may have its own fast clear color value. For
2530 * compressed buffers color value is available in the color buffer.
2532 if (layer_count
> 1 &&
2533 !(mt
->aux_disable
& INTEL_AUX_DISABLE_CCS
) &&
2534 !intel_miptree_is_lossless_compressed(brw
, mt
)) {
2535 assert(brw
->gen
>= 8);
2537 intel_miptree_prepare_access(brw
, mt
, level
, 1, start_layer
, layer_count
,
2543 intel_miptree_finish_render(struct brw_context
*brw
,
2544 struct intel_mipmap_tree
*mt
, uint32_t level
,
2545 uint32_t start_layer
, uint32_t layer_count
)
2547 assert(_mesa_is_format_color_format(mt
->format
));
2548 intel_miptree_finish_write(brw
, mt
, level
, start_layer
, layer_count
,
2549 mt
->mcs_buf
!= NULL
);
2553 intel_miptree_prepare_depth(struct brw_context
*brw
,
2554 struct intel_mipmap_tree
*mt
, uint32_t level
,
2555 uint32_t start_layer
, uint32_t layer_count
)
2557 intel_miptree_prepare_access(brw
, mt
, level
, 1, start_layer
, layer_count
,
2558 mt
->hiz_buf
!= NULL
, mt
->hiz_buf
!= NULL
);
2562 intel_miptree_finish_depth(struct brw_context
*brw
,
2563 struct intel_mipmap_tree
*mt
, uint32_t level
,
2564 uint32_t start_layer
, uint32_t layer_count
,
2567 if (depth_written
) {
2568 intel_miptree_finish_write(brw
, mt
, level
, start_layer
, layer_count
,
2569 mt
->hiz_buf
!= NULL
);
2574 * Make it possible to share the BO backing the given miptree with another
2575 * process or another miptree.
2577 * Fast color clears are unsafe with shared buffers, so we need to resolve and
2578 * then discard the MCS buffer, if present. We also set the no_ccs flag to
2579 * ensure that no MCS buffer gets allocated in the future.
2581 * HiZ is similarly unsafe with shared buffers.
2584 intel_miptree_make_shareable(struct brw_context
*brw
,
2585 struct intel_mipmap_tree
*mt
)
2587 /* MCS buffers are also used for multisample buffers, but we can't resolve
2588 * away a multisample MCS buffer because it's an integral part of how the
2589 * pixel data is stored. Fortunately this code path should never be
2590 * reached for multisample buffers.
2592 assert(mt
->msaa_layout
== INTEL_MSAA_LAYOUT_NONE
|| mt
->num_samples
<= 1);
2594 intel_miptree_prepare_access(brw
, mt
, 0, INTEL_REMAINING_LEVELS
,
2595 0, INTEL_REMAINING_LAYERS
, false, false);
2598 mt
->aux_disable
|= (INTEL_AUX_DISABLE_CCS
| INTEL_AUX_DISABLE_MCS
);
2599 brw_bo_unreference(mt
->mcs_buf
->bo
);
2603 /* Any pending MCS/CCS operations are no longer needed. Trying to
2604 * execute any will likely crash due to the missing aux buffer. So let's
2605 * delete all pending ops.
2607 free(mt
->aux_state
);
2608 mt
->aux_state
= NULL
;
2612 mt
->aux_disable
|= INTEL_AUX_DISABLE_HIZ
;
2613 intel_miptree_aux_buffer_free(mt
->hiz_buf
);
2616 for (uint32_t l
= mt
->first_level
; l
<= mt
->last_level
; ++l
) {
2617 mt
->level
[l
].has_hiz
= false;
2620 /* Any pending HiZ operations are no longer needed. Trying to execute
2621 * any will likely crash due to the missing aux buffer. So let's delete
2624 free(mt
->aux_state
);
2625 mt
->aux_state
= NULL
;
2631 * \brief Get pointer offset into stencil buffer.
2633 * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
2634 * must decode the tile's layout in software.
2637 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
2639 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
2641 * Even though the returned offset is always positive, the return type is
2643 * commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
2644 * mesa: Fix return type of _mesa_get_format_bytes() (#37351)
2647 intel_offset_S8(uint32_t stride
, uint32_t x
, uint32_t y
, bool swizzled
)
2649 uint32_t tile_size
= 4096;
2650 uint32_t tile_width
= 64;
2651 uint32_t tile_height
= 64;
2652 uint32_t row_size
= 64 * stride
;
2654 uint32_t tile_x
= x
/ tile_width
;
2655 uint32_t tile_y
= y
/ tile_height
;
2657 /* The byte's address relative to the tile's base addres. */
2658 uint32_t byte_x
= x
% tile_width
;
2659 uint32_t byte_y
= y
% tile_height
;
2661 uintptr_t u
= tile_y
* row_size
2662 + tile_x
* tile_size
2663 + 512 * (byte_x
/ 8)
2665 + 32 * ((byte_y
/ 4) % 2)
2666 + 16 * ((byte_x
/ 4) % 2)
2667 + 8 * ((byte_y
/ 2) % 2)
2668 + 4 * ((byte_x
/ 2) % 2)
2673 /* adjust for bit6 swizzling */
2674 if (((byte_x
/ 8) % 2) == 1) {
2675 if (((byte_y
/ 8) % 2) == 0) {
2687 intel_miptree_updownsample(struct brw_context
*brw
,
2688 struct intel_mipmap_tree
*src
,
2689 struct intel_mipmap_tree
*dst
)
2691 unsigned src_w
, src_h
, dst_w
, dst_h
;
2693 if (src
->surf
.size
> 0) {
2694 src_w
= src
->surf
.logical_level0_px
.width
;
2695 src_h
= src
->surf
.logical_level0_px
.height
;
2697 src_w
= src
->logical_width0
;
2698 src_h
= src
->logical_height0
;
2701 if (dst
->surf
.size
> 0) {
2702 dst_w
= dst
->surf
.logical_level0_px
.width
;
2703 dst_h
= dst
->surf
.logical_level0_px
.height
;
2705 dst_w
= dst
->logical_width0
;
2706 dst_h
= dst
->logical_height0
;
2709 brw_blorp_blit_miptrees(brw
,
2710 src
, 0 /* level */, 0 /* layer */,
2711 src
->format
, SWIZZLE_XYZW
,
2712 dst
, 0 /* level */, 0 /* layer */, dst
->format
,
2715 GL_NEAREST
, false, false /*mirror x, y*/,
2718 if (src
->stencil_mt
) {
2719 if (src
->stencil_mt
->surf
.size
> 0) {
2720 src_w
= src
->stencil_mt
->surf
.logical_level0_px
.width
;
2721 src_h
= src
->stencil_mt
->surf
.logical_level0_px
.height
;
2723 src_w
= src
->stencil_mt
->logical_width0
;
2724 src_h
= src
->stencil_mt
->logical_height0
;
2727 if (dst
->stencil_mt
->surf
.size
> 0) {
2728 dst_w
= dst
->stencil_mt
->surf
.logical_level0_px
.width
;
2729 dst_h
= dst
->stencil_mt
->surf
.logical_level0_px
.height
;
2731 dst_w
= dst
->stencil_mt
->logical_width0
;
2732 dst_h
= dst
->stencil_mt
->logical_height0
;
2735 brw_blorp_blit_miptrees(brw
,
2736 src
->stencil_mt
, 0 /* level */, 0 /* layer */,
2737 src
->stencil_mt
->format
, SWIZZLE_XYZW
,
2738 dst
->stencil_mt
, 0 /* level */, 0 /* layer */,
2739 dst
->stencil_mt
->format
,
2742 GL_NEAREST
, false, false /*mirror x, y*/,
2743 false, false /* decode/encode srgb */);
2748 intel_update_r8stencil(struct brw_context
*brw
,
2749 struct intel_mipmap_tree
*mt
)
2751 assert(brw
->gen
>= 7);
2752 struct intel_mipmap_tree
*src
=
2753 mt
->format
== MESA_FORMAT_S_UINT8
? mt
: mt
->stencil_mt
;
2754 if (!src
|| brw
->gen
>= 8 || !src
->r8stencil_needs_update
)
2757 if (!mt
->r8stencil_mt
) {
2758 const uint32_t r8stencil_flags
=
2759 MIPTREE_LAYOUT_ACCELERATED_UPLOAD
| MIPTREE_LAYOUT_TILING_Y
|
2760 MIPTREE_LAYOUT_DISABLE_AUX
;
2761 assert(brw
->gen
> 6); /* Handle MIPTREE_LAYOUT_GEN6_HIZ_STENCIL */
2762 mt
->r8stencil_mt
= intel_miptree_create(brw
,
2764 MESA_FORMAT_R_UINT8
,
2767 src
->logical_width0
,
2768 src
->logical_height0
,
2769 src
->logical_depth0
,
2772 assert(mt
->r8stencil_mt
);
2775 struct intel_mipmap_tree
*dst
= mt
->r8stencil_mt
;
2777 for (int level
= src
->first_level
; level
<= src
->last_level
; level
++) {
2778 const unsigned depth
= src
->level
[level
].depth
;
2780 for (unsigned layer
= 0; layer
< depth
; layer
++) {
2781 brw_blorp_copy_miptrees(brw
,
2785 minify(src
->logical_width0
, level
),
2786 minify(src
->logical_height0
, level
));
2790 brw_render_cache_set_check_flush(brw
, dst
->bo
);
2791 src
->r8stencil_needs_update
= false;
2795 intel_miptree_map_raw(struct brw_context
*brw
,
2796 struct intel_mipmap_tree
*mt
,
2799 struct brw_bo
*bo
= mt
->bo
;
2801 if (brw_batch_references(&brw
->batch
, bo
))
2802 intel_batchbuffer_flush(brw
);
2804 return brw_bo_map(brw
, bo
, mode
);
2808 intel_miptree_unmap_raw(struct intel_mipmap_tree
*mt
)
2810 brw_bo_unmap(mt
->bo
);
2814 intel_miptree_map_gtt(struct brw_context
*brw
,
2815 struct intel_mipmap_tree
*mt
,
2816 struct intel_miptree_map
*map
,
2817 unsigned int level
, unsigned int slice
)
2819 unsigned int bw
, bh
;
2821 unsigned int image_x
, image_y
;
2822 intptr_t x
= map
->x
;
2823 intptr_t y
= map
->y
;
2825 /* For compressed formats, the stride is the number of bytes per
2826 * row of blocks. intel_miptree_get_image_offset() already does
2829 _mesa_get_format_block_size(mt
->format
, &bw
, &bh
);
2830 assert(y
% bh
== 0);
2831 assert(x
% bw
== 0);
2835 base
= intel_miptree_map_raw(brw
, mt
, map
->mode
) + mt
->offset
;
2840 /* Note that in the case of cube maps, the caller must have passed the
2841 * slice number referencing the face.
2843 intel_miptree_get_image_offset(mt
, level
, slice
, &image_x
, &image_y
);
2847 map
->stride
= mt
->pitch
;
2848 map
->ptr
= base
+ y
* map
->stride
+ x
* mt
->cpp
;
2851 DBG("%s: %d,%d %dx%d from mt %p (%s) "
2852 "%"PRIiPTR
",%"PRIiPTR
" = %p/%d\n", __func__
,
2853 map
->x
, map
->y
, map
->w
, map
->h
,
2854 mt
, _mesa_get_format_name(mt
->format
),
2855 x
, y
, map
->ptr
, map
->stride
);
2859 intel_miptree_unmap_gtt(struct intel_mipmap_tree
*mt
)
2861 intel_miptree_unmap_raw(mt
);
2865 intel_miptree_map_blit(struct brw_context
*brw
,
2866 struct intel_mipmap_tree
*mt
,
2867 struct intel_miptree_map
*map
,
2868 unsigned int level
, unsigned int slice
)
2870 map
->linear_mt
= intel_miptree_create(brw
, GL_TEXTURE_2D
, mt
->format
,
2871 /* first_level */ 0,
2875 MIPTREE_LAYOUT_TILING_NONE
);
2877 if (!map
->linear_mt
) {
2878 fprintf(stderr
, "Failed to allocate blit temporary\n");
2881 map
->stride
= map
->linear_mt
->pitch
;
2883 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2884 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2885 * invalidate is set, since we'll be writing the whole rectangle from our
2886 * temporary buffer back out.
2888 if (!(map
->mode
& GL_MAP_INVALIDATE_RANGE_BIT
)) {
2889 if (!intel_miptree_copy(brw
,
2890 mt
, level
, slice
, map
->x
, map
->y
,
2891 map
->linear_mt
, 0, 0, 0, 0,
2893 fprintf(stderr
, "Failed to blit\n");
2898 map
->ptr
= intel_miptree_map_raw(brw
, map
->linear_mt
, map
->mode
);
2900 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__
,
2901 map
->x
, map
->y
, map
->w
, map
->h
,
2902 mt
, _mesa_get_format_name(mt
->format
),
2903 level
, slice
, map
->ptr
, map
->stride
);
2908 intel_miptree_release(&map
->linear_mt
);
2914 intel_miptree_unmap_blit(struct brw_context
*brw
,
2915 struct intel_mipmap_tree
*mt
,
2916 struct intel_miptree_map
*map
,
2920 struct gl_context
*ctx
= &brw
->ctx
;
2922 intel_miptree_unmap_raw(map
->linear_mt
);
2924 if (map
->mode
& GL_MAP_WRITE_BIT
) {
2925 bool ok
= intel_miptree_copy(brw
,
2926 map
->linear_mt
, 0, 0, 0, 0,
2927 mt
, level
, slice
, map
->x
, map
->y
,
2929 WARN_ONCE(!ok
, "Failed to blit from linear temporary mapping");
2932 intel_miptree_release(&map
->linear_mt
);
2936 * "Map" a buffer by copying it to an untiled temporary using MOVNTDQA.
2938 #if defined(USE_SSE41)
2940 intel_miptree_map_movntdqa(struct brw_context
*brw
,
2941 struct intel_mipmap_tree
*mt
,
2942 struct intel_miptree_map
*map
,
2943 unsigned int level
, unsigned int slice
)
2945 assert(map
->mode
& GL_MAP_READ_BIT
);
2946 assert(!(map
->mode
& GL_MAP_WRITE_BIT
));
2948 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__
,
2949 map
->x
, map
->y
, map
->w
, map
->h
,
2950 mt
, _mesa_get_format_name(mt
->format
),
2951 level
, slice
, map
->ptr
, map
->stride
);
2953 /* Map the original image */
2956 intel_miptree_get_image_offset(mt
, level
, slice
, &image_x
, &image_y
);
2960 void *src
= intel_miptree_map_raw(brw
, mt
, map
->mode
);
2966 src
+= image_y
* mt
->pitch
;
2967 src
+= image_x
* mt
->cpp
;
2969 /* Due to the pixel offsets for the particular image being mapped, our
2970 * src pointer may not be 16-byte aligned. However, if the pitch is
2971 * divisible by 16, then the amount by which it's misaligned will remain
2972 * consistent from row to row.
2974 assert((mt
->pitch
% 16) == 0);
2975 const int misalignment
= ((uintptr_t) src
) & 15;
2977 /* Create an untiled temporary buffer for the mapping. */
2978 const unsigned width_bytes
= _mesa_format_row_stride(mt
->format
, map
->w
);
2980 map
->stride
= ALIGN(misalignment
+ width_bytes
, 16);
2982 map
->buffer
= _mesa_align_malloc(map
->stride
* map
->h
, 16);
2983 /* Offset the destination so it has the same misalignment as src. */
2984 map
->ptr
= map
->buffer
+ misalignment
;
2986 assert((((uintptr_t) map
->ptr
) & 15) == misalignment
);
2988 for (uint32_t y
= 0; y
< map
->h
; y
++) {
2989 void *dst_ptr
= map
->ptr
+ y
* map
->stride
;
2990 void *src_ptr
= src
+ y
* mt
->pitch
;
2992 _mesa_streaming_load_memcpy(dst_ptr
, src_ptr
, width_bytes
);
2995 intel_miptree_unmap_raw(mt
);
2999 intel_miptree_unmap_movntdqa(struct brw_context
*brw
,
3000 struct intel_mipmap_tree
*mt
,
3001 struct intel_miptree_map
*map
,
3005 _mesa_align_free(map
->buffer
);
3012 intel_miptree_map_s8(struct brw_context
*brw
,
3013 struct intel_mipmap_tree
*mt
,
3014 struct intel_miptree_map
*map
,
3015 unsigned int level
, unsigned int slice
)
3017 map
->stride
= map
->w
;
3018 map
->buffer
= map
->ptr
= malloc(map
->stride
* map
->h
);
3022 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
3023 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
3024 * invalidate is set, since we'll be writing the whole rectangle from our
3025 * temporary buffer back out.
3027 if (!(map
->mode
& GL_MAP_INVALIDATE_RANGE_BIT
)) {
3028 /* ISL uses a stencil pitch value that is expected by hardware whereas
3029 * traditional miptree uses half of that. Below the value gets supplied
3030 * to intel_offset_S8() which expects the legacy interpretation.
3032 const unsigned pitch
= mt
->surf
.size
> 0 ?
3033 mt
->surf
.row_pitch
/ 2 : mt
->pitch
;
3034 uint8_t *untiled_s8_map
= map
->ptr
;
3035 uint8_t *tiled_s8_map
= intel_miptree_map_raw(brw
, mt
, GL_MAP_READ_BIT
);
3036 unsigned int image_x
, image_y
;
3038 intel_miptree_get_image_offset(mt
, level
, slice
, &image_x
, &image_y
);
3040 for (uint32_t y
= 0; y
< map
->h
; y
++) {
3041 for (uint32_t x
= 0; x
< map
->w
; x
++) {
3042 ptrdiff_t offset
= intel_offset_S8(pitch
,
3043 x
+ image_x
+ map
->x
,
3044 y
+ image_y
+ map
->y
,
3045 brw
->has_swizzling
);
3046 untiled_s8_map
[y
* map
->w
+ x
] = tiled_s8_map
[offset
];
3050 intel_miptree_unmap_raw(mt
);
3052 DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __func__
,
3053 map
->x
, map
->y
, map
->w
, map
->h
,
3054 mt
, map
->x
+ image_x
, map
->y
+ image_y
, map
->ptr
, map
->stride
);
3056 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__
,
3057 map
->x
, map
->y
, map
->w
, map
->h
,
3058 mt
, map
->ptr
, map
->stride
);
3063 intel_miptree_unmap_s8(struct brw_context
*brw
,
3064 struct intel_mipmap_tree
*mt
,
3065 struct intel_miptree_map
*map
,
3069 if (map
->mode
& GL_MAP_WRITE_BIT
) {
3070 /* ISL uses a stencil pitch value that is expected by hardware whereas
3071 * traditional miptree uses half of that. Below the value gets supplied
3072 * to intel_offset_S8() which expects the legacy interpretation.
3074 const unsigned pitch
= mt
->surf
.size
> 0 ?
3075 mt
->surf
.row_pitch
/ 2: mt
->pitch
;
3076 unsigned int image_x
, image_y
;
3077 uint8_t *untiled_s8_map
= map
->ptr
;
3078 uint8_t *tiled_s8_map
= intel_miptree_map_raw(brw
, mt
, GL_MAP_WRITE_BIT
);
3080 intel_miptree_get_image_offset(mt
, level
, slice
, &image_x
, &image_y
);
3082 for (uint32_t y
= 0; y
< map
->h
; y
++) {
3083 for (uint32_t x
= 0; x
< map
->w
; x
++) {
3084 ptrdiff_t offset
= intel_offset_S8(pitch
,
3085 image_x
+ x
+ map
->x
,
3086 image_y
+ y
+ map
->y
,
3087 brw
->has_swizzling
);
3088 tiled_s8_map
[offset
] = untiled_s8_map
[y
* map
->w
+ x
];
3092 intel_miptree_unmap_raw(mt
);
3099 intel_miptree_map_etc(struct brw_context
*brw
,
3100 struct intel_mipmap_tree
*mt
,
3101 struct intel_miptree_map
*map
,
3105 assert(mt
->etc_format
!= MESA_FORMAT_NONE
);
3106 if (mt
->etc_format
== MESA_FORMAT_ETC1_RGB8
) {
3107 assert(mt
->format
== MESA_FORMAT_R8G8B8X8_UNORM
);
3110 assert(map
->mode
& GL_MAP_WRITE_BIT
);
3111 assert(map
->mode
& GL_MAP_INVALIDATE_RANGE_BIT
);
3113 map
->stride
= _mesa_format_row_stride(mt
->etc_format
, map
->w
);
3114 map
->buffer
= malloc(_mesa_format_image_size(mt
->etc_format
,
3115 map
->w
, map
->h
, 1));
3116 map
->ptr
= map
->buffer
;
3120 intel_miptree_unmap_etc(struct brw_context
*brw
,
3121 struct intel_mipmap_tree
*mt
,
3122 struct intel_miptree_map
*map
,
3128 intel_miptree_get_image_offset(mt
, level
, slice
, &image_x
, &image_y
);
3133 uint8_t *dst
= intel_miptree_map_raw(brw
, mt
, GL_MAP_WRITE_BIT
)
3134 + image_y
* mt
->pitch
3135 + image_x
* mt
->cpp
;
3137 if (mt
->etc_format
== MESA_FORMAT_ETC1_RGB8
)
3138 _mesa_etc1_unpack_rgba8888(dst
, mt
->pitch
,
3139 map
->ptr
, map
->stride
,
3142 _mesa_unpack_etc2_format(dst
, mt
->pitch
,
3143 map
->ptr
, map
->stride
,
3144 map
->w
, map
->h
, mt
->etc_format
);
3146 intel_miptree_unmap_raw(mt
);
3151 * Mapping function for packed depth/stencil miptrees backed by real separate
3152 * miptrees for depth and stencil.
3154 * On gen7, and to support HiZ pre-gen7, we have to have the stencil buffer
3155 * separate from the depth buffer. Yet at the GL API level, we have to expose
3156 * packed depth/stencil textures and FBO attachments, and Mesa core expects to
3157 * be able to map that memory for texture storage and glReadPixels-type
3158 * operations. We give Mesa core that access by mallocing a temporary and
3159 * copying the data between the actual backing store and the temporary.
3162 intel_miptree_map_depthstencil(struct brw_context
*brw
,
3163 struct intel_mipmap_tree
*mt
,
3164 struct intel_miptree_map
*map
,
3165 unsigned int level
, unsigned int slice
)
3167 struct intel_mipmap_tree
*z_mt
= mt
;
3168 struct intel_mipmap_tree
*s_mt
= mt
->stencil_mt
;
3169 bool map_z32f_x24s8
= mt
->format
== MESA_FORMAT_Z_FLOAT32
;
3170 int packed_bpp
= map_z32f_x24s8
? 8 : 4;
3172 map
->stride
= map
->w
* packed_bpp
;
3173 map
->buffer
= map
->ptr
= malloc(map
->stride
* map
->h
);
3177 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
3178 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
3179 * invalidate is set, since we'll be writing the whole rectangle from our
3180 * temporary buffer back out.
3182 if (!(map
->mode
& GL_MAP_INVALIDATE_RANGE_BIT
)) {
3183 /* ISL uses a stencil pitch value that is expected by hardware whereas
3184 * traditional miptree uses half of that. Below the value gets supplied
3185 * to intel_offset_S8() which expects the legacy interpretation.
3187 const unsigned s_pitch
= s_mt
->surf
.size
> 0 ?
3188 s_mt
->surf
.row_pitch
/ 2 : s_mt
->pitch
;
3189 uint32_t *packed_map
= map
->ptr
;
3190 uint8_t *s_map
= intel_miptree_map_raw(brw
, s_mt
, GL_MAP_READ_BIT
);
3191 uint32_t *z_map
= intel_miptree_map_raw(brw
, z_mt
, GL_MAP_READ_BIT
);
3192 unsigned int s_image_x
, s_image_y
;
3193 unsigned int z_image_x
, z_image_y
;
3195 intel_miptree_get_image_offset(s_mt
, level
, slice
,
3196 &s_image_x
, &s_image_y
);
3197 intel_miptree_get_image_offset(z_mt
, level
, slice
,
3198 &z_image_x
, &z_image_y
);
3200 for (uint32_t y
= 0; y
< map
->h
; y
++) {
3201 for (uint32_t x
= 0; x
< map
->w
; x
++) {
3202 int map_x
= map
->x
+ x
, map_y
= map
->y
+ y
;
3203 ptrdiff_t s_offset
= intel_offset_S8(s_pitch
,
3206 brw
->has_swizzling
);
3207 ptrdiff_t z_offset
= ((map_y
+ z_image_y
) *
3209 (map_x
+ z_image_x
));
3210 uint8_t s
= s_map
[s_offset
];
3211 uint32_t z
= z_map
[z_offset
];
3213 if (map_z32f_x24s8
) {
3214 packed_map
[(y
* map
->w
+ x
) * 2 + 0] = z
;
3215 packed_map
[(y
* map
->w
+ x
) * 2 + 1] = s
;
3217 packed_map
[y
* map
->w
+ x
] = (s
<< 24) | (z
& 0x00ffffff);
3222 intel_miptree_unmap_raw(s_mt
);
3223 intel_miptree_unmap_raw(z_mt
);
3225 DBG("%s: %d,%d %dx%d from z mt %p %d,%d, s mt %p %d,%d = %p/%d\n",
3227 map
->x
, map
->y
, map
->w
, map
->h
,
3228 z_mt
, map
->x
+ z_image_x
, map
->y
+ z_image_y
,
3229 s_mt
, map
->x
+ s_image_x
, map
->y
+ s_image_y
,
3230 map
->ptr
, map
->stride
);
3232 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__
,
3233 map
->x
, map
->y
, map
->w
, map
->h
,
3234 mt
, map
->ptr
, map
->stride
);
3239 intel_miptree_unmap_depthstencil(struct brw_context
*brw
,
3240 struct intel_mipmap_tree
*mt
,
3241 struct intel_miptree_map
*map
,
3245 struct intel_mipmap_tree
*z_mt
= mt
;
3246 struct intel_mipmap_tree
*s_mt
= mt
->stencil_mt
;
3247 bool map_z32f_x24s8
= mt
->format
== MESA_FORMAT_Z_FLOAT32
;
3249 if (map
->mode
& GL_MAP_WRITE_BIT
) {
3250 /* ISL uses a stencil pitch value that is expected by hardware whereas
3251 * traditional miptree uses half of that. Below the value gets supplied
3252 * to intel_offset_S8() which expects the legacy interpretation.
3254 const unsigned s_pitch
= s_mt
->surf
.size
> 0 ?
3255 s_mt
->surf
.row_pitch
/ 2 : s_mt
->pitch
;
3256 uint32_t *packed_map
= map
->ptr
;
3257 uint8_t *s_map
= intel_miptree_map_raw(brw
, s_mt
, GL_MAP_WRITE_BIT
);
3258 uint32_t *z_map
= intel_miptree_map_raw(brw
, z_mt
, GL_MAP_WRITE_BIT
);
3259 unsigned int s_image_x
, s_image_y
;
3260 unsigned int z_image_x
, z_image_y
;
3262 intel_miptree_get_image_offset(s_mt
, level
, slice
,
3263 &s_image_x
, &s_image_y
);
3264 intel_miptree_get_image_offset(z_mt
, level
, slice
,
3265 &z_image_x
, &z_image_y
);
3267 for (uint32_t y
= 0; y
< map
->h
; y
++) {
3268 for (uint32_t x
= 0; x
< map
->w
; x
++) {
3269 ptrdiff_t s_offset
= intel_offset_S8(s_pitch
,
3270 x
+ s_image_x
+ map
->x
,
3271 y
+ s_image_y
+ map
->y
,
3272 brw
->has_swizzling
);
3273 ptrdiff_t z_offset
= ((y
+ z_image_y
+ map
->y
) *
3275 (x
+ z_image_x
+ map
->x
));
3277 if (map_z32f_x24s8
) {
3278 z_map
[z_offset
] = packed_map
[(y
* map
->w
+ x
) * 2 + 0];
3279 s_map
[s_offset
] = packed_map
[(y
* map
->w
+ x
) * 2 + 1];
3281 uint32_t packed
= packed_map
[y
* map
->w
+ x
];
3282 s_map
[s_offset
] = packed
>> 24;
3283 z_map
[z_offset
] = packed
;
3288 intel_miptree_unmap_raw(s_mt
);
3289 intel_miptree_unmap_raw(z_mt
);
3291 DBG("%s: %d,%d %dx%d from z mt %p (%s) %d,%d, s mt %p %d,%d = %p/%d\n",
3293 map
->x
, map
->y
, map
->w
, map
->h
,
3294 z_mt
, _mesa_get_format_name(z_mt
->format
),
3295 map
->x
+ z_image_x
, map
->y
+ z_image_y
,
3296 s_mt
, map
->x
+ s_image_x
, map
->y
+ s_image_y
,
3297 map
->ptr
, map
->stride
);
3304 * Create and attach a map to the miptree at (level, slice). Return the
3307 static struct intel_miptree_map
*
3308 intel_miptree_attach_map(struct intel_mipmap_tree
*mt
,
3317 struct intel_miptree_map
*map
= calloc(1, sizeof(*map
));
3322 assert(mt
->level
[level
].slice
[slice
].map
== NULL
);
3323 mt
->level
[level
].slice
[slice
].map
= map
;
3335 * Release the map at (level, slice).
3338 intel_miptree_release_map(struct intel_mipmap_tree
*mt
,
3342 struct intel_miptree_map
**map
;
3344 map
= &mt
->level
[level
].slice
[slice
].map
;
3350 can_blit_slice(struct intel_mipmap_tree
*mt
,
3351 unsigned int level
, unsigned int slice
)
3353 /* See intel_miptree_blit() for details on the 32k pitch limit. */
3354 if (mt
->pitch
>= 32768)
3361 use_intel_mipree_map_blit(struct brw_context
*brw
,
3362 struct intel_mipmap_tree
*mt
,
3368 /* It's probably not worth swapping to the blit ring because of
3369 * all the overhead involved.
3371 !(mode
& GL_MAP_WRITE_BIT
) &&
3373 (mt
->tiling
== I915_TILING_X
||
3374 /* Prior to Sandybridge, the blitter can't handle Y tiling */
3375 (brw
->gen
>= 6 && mt
->tiling
== I915_TILING_Y
) ||
3376 /* Fast copy blit on skl+ supports all tiling formats. */
3378 can_blit_slice(mt
, level
, slice
))
3381 if (mt
->tiling
!= I915_TILING_NONE
&&
3382 mt
->bo
->size
>= brw
->max_gtt_map_object_size
) {
3383 assert(can_blit_slice(mt
, level
, slice
));
3391 * Parameter \a out_stride has type ptrdiff_t not because the buffer stride may
3392 * exceed 32 bits but to diminish the likelihood subtle bugs in pointer
3393 * arithmetic overflow.
3395 * If you call this function and use \a out_stride, then you're doing pointer
3396 * arithmetic on \a out_ptr. The type of \a out_stride doesn't prevent all
3397 * bugs. The caller must still take care to avoid 32-bit overflow errors in
3398 * all arithmetic expressions that contain buffer offsets and pixel sizes,
3399 * which usually have type uint32_t or GLuint.
3402 intel_miptree_map(struct brw_context
*brw
,
3403 struct intel_mipmap_tree
*mt
,
3412 ptrdiff_t *out_stride
)
3414 struct intel_miptree_map
*map
;
3416 assert(mt
->num_samples
<= 1);
3418 map
= intel_miptree_attach_map(mt
, level
, slice
, x
, y
, w
, h
, mode
);
3425 intel_miptree_access_raw(brw
, mt
, level
, slice
,
3426 map
->mode
& GL_MAP_WRITE_BIT
);
3428 if (mt
->format
== MESA_FORMAT_S_UINT8
) {
3429 intel_miptree_map_s8(brw
, mt
, map
, level
, slice
);
3430 } else if (mt
->etc_format
!= MESA_FORMAT_NONE
&&
3431 !(mode
& BRW_MAP_DIRECT_BIT
)) {
3432 intel_miptree_map_etc(brw
, mt
, map
, level
, slice
);
3433 } else if (mt
->stencil_mt
&& !(mode
& BRW_MAP_DIRECT_BIT
)) {
3434 intel_miptree_map_depthstencil(brw
, mt
, map
, level
, slice
);
3435 } else if (use_intel_mipree_map_blit(brw
, mt
, mode
, level
, slice
)) {
3436 intel_miptree_map_blit(brw
, mt
, map
, level
, slice
);
3437 #if defined(USE_SSE41)
3438 } else if (!(mode
& GL_MAP_WRITE_BIT
) &&
3439 !mt
->compressed
&& cpu_has_sse4_1
&&
3440 (mt
->pitch
% 16 == 0)) {
3441 intel_miptree_map_movntdqa(brw
, mt
, map
, level
, slice
);
3444 intel_miptree_map_gtt(brw
, mt
, map
, level
, slice
);
3447 *out_ptr
= map
->ptr
;
3448 *out_stride
= map
->stride
;
3450 if (map
->ptr
== NULL
)
3451 intel_miptree_release_map(mt
, level
, slice
);
3455 intel_miptree_unmap(struct brw_context
*brw
,
3456 struct intel_mipmap_tree
*mt
,
3460 struct intel_miptree_map
*map
= mt
->level
[level
].slice
[slice
].map
;
3462 assert(mt
->num_samples
<= 1);
3467 DBG("%s: mt %p (%s) level %d slice %d\n", __func__
,
3468 mt
, _mesa_get_format_name(mt
->format
), level
, slice
);
3470 if (mt
->format
== MESA_FORMAT_S_UINT8
) {
3471 intel_miptree_unmap_s8(brw
, mt
, map
, level
, slice
);
3472 } else if (mt
->etc_format
!= MESA_FORMAT_NONE
&&
3473 !(map
->mode
& BRW_MAP_DIRECT_BIT
)) {
3474 intel_miptree_unmap_etc(brw
, mt
, map
, level
, slice
);
3475 } else if (mt
->stencil_mt
&& !(map
->mode
& BRW_MAP_DIRECT_BIT
)) {
3476 intel_miptree_unmap_depthstencil(brw
, mt
, map
, level
, slice
);
3477 } else if (map
->linear_mt
) {
3478 intel_miptree_unmap_blit(brw
, mt
, map
, level
, slice
);
3479 #if defined(USE_SSE41)
3480 } else if (map
->buffer
&& cpu_has_sse4_1
) {
3481 intel_miptree_unmap_movntdqa(brw
, mt
, map
, level
, slice
);
3484 intel_miptree_unmap_gtt(mt
);
3487 intel_miptree_release_map(mt
, level
, slice
);
3491 get_isl_surf_dim(GLenum target
)
3495 case GL_TEXTURE_1D_ARRAY
:
3496 return ISL_SURF_DIM_1D
;
3499 case GL_TEXTURE_2D_ARRAY
:
3500 case GL_TEXTURE_RECTANGLE
:
3501 case GL_TEXTURE_CUBE_MAP
:
3502 case GL_TEXTURE_CUBE_MAP_ARRAY
:
3503 case GL_TEXTURE_2D_MULTISAMPLE
:
3504 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
3505 case GL_TEXTURE_EXTERNAL_OES
:
3506 return ISL_SURF_DIM_2D
;
3509 return ISL_SURF_DIM_3D
;
3512 unreachable("Invalid texture target");
3516 get_isl_dim_layout(const struct gen_device_info
*devinfo
, uint32_t tiling
,
3517 GLenum target
, enum miptree_array_layout array_layout
)
3519 if (array_layout
== GEN6_HIZ_STENCIL
)
3520 return ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ
;
3524 case GL_TEXTURE_1D_ARRAY
:
3525 return (devinfo
->gen
>= 9 && tiling
== I915_TILING_NONE
?
3526 ISL_DIM_LAYOUT_GEN9_1D
: ISL_DIM_LAYOUT_GEN4_2D
);
3529 case GL_TEXTURE_2D_ARRAY
:
3530 case GL_TEXTURE_RECTANGLE
:
3531 case GL_TEXTURE_2D_MULTISAMPLE
:
3532 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
3533 case GL_TEXTURE_EXTERNAL_OES
:
3534 return ISL_DIM_LAYOUT_GEN4_2D
;
3536 case GL_TEXTURE_CUBE_MAP
:
3537 case GL_TEXTURE_CUBE_MAP_ARRAY
:
3538 return (devinfo
->gen
== 4 ? ISL_DIM_LAYOUT_GEN4_3D
:
3539 ISL_DIM_LAYOUT_GEN4_2D
);
3542 return (devinfo
->gen
>= 9 ?
3543 ISL_DIM_LAYOUT_GEN4_2D
: ISL_DIM_LAYOUT_GEN4_3D
);
3546 unreachable("Invalid texture target");
3550 intel_miptree_get_isl_tiling(const struct intel_mipmap_tree
*mt
)
3552 if (mt
->format
== MESA_FORMAT_S_UINT8
) {
3553 return ISL_TILING_W
;
3555 switch (mt
->tiling
) {
3556 case I915_TILING_NONE
:
3557 return ISL_TILING_LINEAR
;
3559 return ISL_TILING_X
;
3561 return ISL_TILING_Y0
;
3563 unreachable("Invalid tiling mode");
3569 intel_miptree_get_isl_surf(struct brw_context
*brw
,
3570 const struct intel_mipmap_tree
*mt
,
3571 struct isl_surf
*surf
)
3573 surf
->dim
= get_isl_surf_dim(mt
->target
);
3574 surf
->dim_layout
= get_isl_dim_layout(&brw
->screen
->devinfo
,
3575 mt
->tiling
, mt
->target
,
3578 if (mt
->num_samples
> 1) {
3579 switch (mt
->msaa_layout
) {
3580 case INTEL_MSAA_LAYOUT_IMS
:
3581 surf
->msaa_layout
= ISL_MSAA_LAYOUT_INTERLEAVED
;
3583 case INTEL_MSAA_LAYOUT_UMS
:
3584 case INTEL_MSAA_LAYOUT_CMS
:
3585 surf
->msaa_layout
= ISL_MSAA_LAYOUT_ARRAY
;
3588 unreachable("Invalid MSAA layout");
3591 surf
->msaa_layout
= ISL_MSAA_LAYOUT_NONE
;
3594 surf
->tiling
= intel_miptree_get_isl_tiling(mt
);
3596 if (mt
->format
== MESA_FORMAT_S_UINT8
) {
3597 /* The ISL definition of row_pitch matches the surface state pitch field
3598 * a bit better than intel_mipmap_tree. In particular, ISL incorporates
3599 * the factor of 2 for W-tiling in row_pitch.
3601 surf
->row_pitch
= 2 * mt
->pitch
;
3603 surf
->row_pitch
= mt
->pitch
;
3606 surf
->format
= translate_tex_format(brw
, mt
->format
, false);
3608 if (brw
->gen
>= 9) {
3609 if (surf
->dim
== ISL_SURF_DIM_1D
&& surf
->tiling
== ISL_TILING_LINEAR
) {
3610 /* For gen9 1-D surfaces, intel_mipmap_tree has a bogus alignment. */
3611 surf
->image_alignment_el
= isl_extent3d(64, 1, 1);
3613 /* On gen9+, intel_mipmap_tree stores the horizontal and vertical
3614 * alignment in terms of surface elements like we want.
3616 surf
->image_alignment_el
= isl_extent3d(mt
->halign
, mt
->valign
, 1);
3619 /* On earlier gens it's stored in pixels. */
3621 _mesa_get_format_block_size(mt
->format
, &bw
, &bh
);
3622 surf
->image_alignment_el
=
3623 isl_extent3d(mt
->halign
/ bw
, mt
->valign
/ bh
, 1);
3626 surf
->logical_level0_px
.width
= mt
->logical_width0
;
3627 surf
->logical_level0_px
.height
= mt
->logical_height0
;
3628 if (surf
->dim
== ISL_SURF_DIM_3D
) {
3629 surf
->logical_level0_px
.depth
= mt
->logical_depth0
;
3630 surf
->logical_level0_px
.array_len
= 1;
3632 surf
->logical_level0_px
.depth
= 1;
3633 surf
->logical_level0_px
.array_len
= mt
->logical_depth0
;
3636 surf
->phys_level0_sa
.width
= mt
->physical_width0
;
3637 surf
->phys_level0_sa
.height
= mt
->physical_height0
;
3638 if (surf
->dim
== ISL_SURF_DIM_3D
) {
3639 surf
->phys_level0_sa
.depth
= mt
->physical_depth0
;
3640 surf
->phys_level0_sa
.array_len
= 1;
3642 surf
->phys_level0_sa
.depth
= 1;
3643 surf
->phys_level0_sa
.array_len
= mt
->physical_depth0
;
3646 surf
->levels
= mt
->last_level
- mt
->first_level
+ 1;
3647 surf
->samples
= MAX2(mt
->num_samples
, 1);
3649 surf
->size
= 0; /* TODO */
3650 surf
->alignment
= 0; /* TODO */
3652 switch (surf
->dim_layout
) {
3653 case ISL_DIM_LAYOUT_GEN4_2D
:
3654 case ISL_DIM_LAYOUT_GEN4_3D
:
3655 case ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ
:
3656 if (brw
->gen
>= 9) {
3657 surf
->array_pitch_el_rows
= mt
->qpitch
;
3660 _mesa_get_format_block_size(mt
->format
, &bw
, &bh
);
3661 assert(mt
->qpitch
% bh
== 0);
3662 surf
->array_pitch_el_rows
= mt
->qpitch
/ bh
;
3665 case ISL_DIM_LAYOUT_GEN9_1D
:
3666 surf
->array_pitch_el_rows
= 1;
3670 switch (mt
->array_layout
) {
3671 case ALL_LOD_IN_EACH_SLICE
:
3672 surf
->array_pitch_span
= ISL_ARRAY_PITCH_SPAN_FULL
;
3674 case ALL_SLICES_AT_EACH_LOD
:
3675 case GEN6_HIZ_STENCIL
:
3676 surf
->array_pitch_span
= ISL_ARRAY_PITCH_SPAN_COMPACT
;
3679 unreachable("Invalid array layout");
3682 GLenum base_format
= _mesa_get_format_base_format(mt
->format
);
3683 switch (base_format
) {
3684 case GL_DEPTH_COMPONENT
:
3685 surf
->usage
= ISL_SURF_USAGE_DEPTH_BIT
| ISL_SURF_USAGE_TEXTURE_BIT
;
3687 case GL_STENCIL_INDEX
:
3688 surf
->usage
= ISL_SURF_USAGE_STENCIL_BIT
;
3690 surf
->usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
3692 case GL_DEPTH_STENCIL
:
3693 /* In this case we only texture from the depth part */
3694 surf
->usage
= ISL_SURF_USAGE_DEPTH_BIT
| ISL_SURF_USAGE_STENCIL_BIT
|
3695 ISL_SURF_USAGE_TEXTURE_BIT
;
3698 surf
->usage
= ISL_SURF_USAGE_TEXTURE_BIT
;
3699 if (brw
->format_supported_as_render_target
[mt
->format
])
3700 surf
->usage
= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
3704 if (_mesa_is_cube_map_texture(mt
->target
))
3705 surf
->usage
|= ISL_SURF_USAGE_CUBE_BIT
;
3709 intel_miptree_get_aux_isl_usage(const struct brw_context
*brw
,
3710 const struct intel_mipmap_tree
*mt
)
3713 return ISL_AUX_USAGE_HIZ
;
3716 return ISL_AUX_USAGE_NONE
;
3718 if (mt
->num_samples
> 1) {
3719 assert(mt
->msaa_layout
== INTEL_MSAA_LAYOUT_CMS
);
3720 return ISL_AUX_USAGE_MCS
;
3723 if (intel_miptree_is_lossless_compressed(brw
, mt
)) {
3724 assert(brw
->gen
>= 9);
3725 return ISL_AUX_USAGE_CCS_E
;
3728 if ((mt
->aux_disable
& INTEL_AUX_DISABLE_CCS
) == 0)
3729 return ISL_AUX_USAGE_CCS_D
;
3731 unreachable("Invalid MCS miptree");