2 * Copyright 2006 VMware, Inc.
3 * Copyright © 2006 Intel Corporation
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a 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
15 * portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * \file brw_tex_layout.cpp
29 * Code to lay out images in a mipmap tree.
31 * \author Keith Whitwell <keithw@vmware.com>
32 * \author Michel Dänzer <daenzer@vmware.com>
35 #include "intel_mipmap_tree.h"
36 #include "brw_context.h"
37 #include "main/macros.h"
38 #include "main/glformats.h"
40 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
43 intel_horizontal_texture_alignment_unit(struct brw_context
*brw
,
44 struct intel_mipmap_tree
*mt
,
45 uint32_t layout_flags
)
47 if (layout_flags
& MIPTREE_LAYOUT_FORCE_HALIGN16
)
51 * +----------------------------------------------------------------------+
52 * | | alignment unit width ("i") |
53 * | Surface Property |-----------------------------|
54 * | | 915 | 965 | ILK | SNB | IVB |
55 * +----------------------------------------------------------------------+
56 * | YUV 4:2:2 format | 8 | 4 | 4 | 4 | 4 |
57 * | BC1-5 compressed format (DXTn/S3TC) | 4 | 4 | 4 | 4 | 4 |
58 * | FXT1 compressed format | 8 | 8 | 8 | 8 | 8 |
59 * | Depth Buffer (16-bit) | 4 | 4 | 4 | 4 | 8 |
60 * | Depth Buffer (other) | 4 | 4 | 4 | 4 | 4 |
61 * | Separate Stencil Buffer | N/A | N/A | 8 | 8 | 8 |
62 * | All Others | 4 | 4 | 4 | 4 | 4 |
63 * +----------------------------------------------------------------------+
65 * On IVB+, non-special cases can be overridden by setting the SURFACE_STATE
66 * "Surface Horizontal Alignment" field to HALIGN_4 or HALIGN_8.
69 if (brw
->gen
>= 7 && mt
->format
== MESA_FORMAT_Z_UNORM16
)
76 intel_vertical_texture_alignment_unit(struct brw_context
*brw
,
77 const struct intel_mipmap_tree
*mt
)
80 * +----------------------------------------------------------------------+
81 * | | alignment unit height ("j") |
82 * | Surface Property |-----------------------------|
83 * | | 915 | 965 | ILK | SNB | IVB |
84 * +----------------------------------------------------------------------+
85 * | BC1-5 compressed format (DXTn/S3TC) | 4 | 4 | 4 | 4 | 4 |
86 * | FXT1 compressed format | 4 | 4 | 4 | 4 | 4 |
87 * | Depth Buffer | 2 | 2 | 2 | 4 | 4 |
88 * | Separate Stencil Buffer | N/A | N/A | N/A | 4 | 8 |
89 * | Multisampled (4x or 8x) render target | N/A | N/A | N/A | 4 | 4 |
90 * | All Others | 2 | 2 | 2 | * | * |
91 * +----------------------------------------------------------------------+
93 * Where "*" means either VALIGN_2 or VALIGN_4 depending on the setting of
94 * the SURFACE_STATE "Surface Vertical Alignment" field.
97 /* Broadwell only supports VALIGN of 4, 8, and 16. The BSpec says 4
98 * should always be used, except for stencil buffers, which should be 8.
103 if (mt
->num_samples
> 1)
106 GLenum base_format
= _mesa_get_format_base_format(mt
->format
);
109 (base_format
== GL_DEPTH_COMPONENT
||
110 base_format
== GL_DEPTH_STENCIL
)) {
115 /* On Gen7, we prefer a vertical alignment of 4 when possible, because
116 * that allows Y tiled render targets.
118 * From the Ivy Bridge PRM, Vol4 Part1 2.12.2.1 (SURFACE_STATE for most
119 * messages), on p64, under the heading "Surface Vertical Alignment":
121 * Value of 1 [VALIGN_4] is not supported for format YCRCB_NORMAL
122 * (0x182), YCRCB_SWAPUVY (0x183), YCRCB_SWAPUV (0x18f), YCRCB_SWAPY
125 * VALIGN_4 is not supported for surface format R32G32B32_FLOAT.
127 if (base_format
== GL_YCBCR_MESA
|| mt
->format
== MESA_FORMAT_RGB_FLOAT32
)
137 gen9_miptree_layout_1d(struct intel_mipmap_tree
*mt
)
140 unsigned width
= mt
->physical_width0
;
141 unsigned depth
= mt
->physical_depth0
; /* number of array layers. */
143 /* When this layout is used the horizontal alignment is fixed at 64 and the
144 * hardware ignores the value given in the surface state
146 const unsigned int halign
= 64;
148 mt
->total_height
= mt
->physical_height0
;
151 for (unsigned level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
154 intel_miptree_set_level_info(mt
, level
, x
, 0, depth
);
156 img_width
= ALIGN(width
, halign
);
158 mt
->total_width
= MAX2(mt
->total_width
, x
+ img_width
);
162 width
= minify(width
, 1);
167 brw_miptree_layout_2d(struct intel_mipmap_tree
*mt
)
171 unsigned width
= mt
->physical_width0
;
172 unsigned height
= mt
->physical_height0
;
173 /* Number of layers of array texture or slices of 3d texture (gen9+). */
174 unsigned depth
= mt
->physical_depth0
;
177 _mesa_get_format_block_size(mt
->format
, &bw
, &bh
);
179 mt
->total_width
= mt
->physical_width0
;
180 mt
->total_width
= ALIGN_NPOT(mt
->total_width
, bw
);
182 /* May need to adjust width to accommodate the placement of
183 * the 2nd mipmap. This occurs when the alignment
184 * constraints of mipmap placement push the right edge of the
185 * 2nd mipmap out past the width of its parent.
187 if (mt
->first_level
!= mt
->last_level
) {
190 mip1_width
= ALIGN_NPOT(minify(mt
->physical_width0
, 1), mt
->halign
) +
191 ALIGN_NPOT(minify(mt
->physical_width0
, 2), bw
);
193 if (mip1_width
> mt
->total_width
)
194 mt
->total_width
= mip1_width
;
197 mt
->total_width
/= bw
;
198 mt
->total_height
= 0;
200 for (unsigned level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
203 intel_miptree_set_level_info(mt
, level
, x
, y
, depth
);
205 img_height
= ALIGN_NPOT(height
, mt
->valign
);
208 if (mt
->array_layout
== ALL_SLICES_AT_EACH_LOD
) {
209 /* Compact arrays with separated miplevels */
213 /* Because the images are packed better, the final offset
214 * might not be the maximal one:
216 mt
->total_height
= MAX2(mt
->total_height
, y
+ img_height
);
218 /* Layout_below: step right after second mipmap.
220 if (level
== mt
->first_level
+ 1) {
221 x
+= ALIGN_NPOT(width
, mt
->halign
) / bw
;
226 width
= minify(width
, 1);
227 height
= minify(height
, 1);
229 if (mt
->target
== GL_TEXTURE_3D
)
230 depth
= minify(depth
, 1);
235 brw_miptree_get_horizontal_slice_pitch(const struct brw_context
*brw
,
236 const struct intel_mipmap_tree
*mt
,
239 if ((brw
->gen
< 9 && mt
->target
== GL_TEXTURE_3D
) ||
240 (brw
->gen
== 4 && mt
->target
== GL_TEXTURE_CUBE_MAP
)) {
241 return ALIGN_NPOT(minify(mt
->physical_width0
, level
), mt
->halign
);
248 brw_miptree_get_vertical_slice_pitch(const struct brw_context
*brw
,
249 const struct intel_mipmap_tree
*mt
,
253 /* ALL_SLICES_AT_EACH_LOD isn't supported on Gen8+ but this code will
254 * effectively end up with a packed qpitch anyway whenever
255 * mt->first_level == mt->last_level.
257 assert(mt
->array_layout
!= ALL_SLICES_AT_EACH_LOD
);
259 /* On Gen9 we can pick whatever qpitch we like as long as it's aligned
260 * to the vertical alignment so we don't need to add any extra rows.
262 unsigned qpitch
= mt
->total_height
;
264 /* If the surface might be used as a stencil buffer or HiZ buffer then
265 * it needs to be a multiple of 8.
267 const GLenum base_format
= _mesa_get_format_base_format(mt
->format
);
268 if (_mesa_is_depth_or_stencil_format(base_format
))
269 qpitch
= ALIGN(qpitch
, 8);
271 /* 3D textures need to be aligned to the tile height. At this point we
272 * don't know which tiling will be used so let's just align it to 32
274 if (mt
->target
== GL_TEXTURE_3D
)
275 qpitch
= ALIGN(qpitch
, 32);
279 } else if (mt
->target
== GL_TEXTURE_3D
||
280 (brw
->gen
== 4 && mt
->target
== GL_TEXTURE_CUBE_MAP
) ||
281 mt
->array_layout
== ALL_SLICES_AT_EACH_LOD
) {
282 return ALIGN_NPOT(minify(mt
->physical_height0
, level
), mt
->valign
);
285 const unsigned h0
= ALIGN_NPOT(mt
->physical_height0
, mt
->valign
);
286 const unsigned h1
= ALIGN_NPOT(minify(mt
->physical_height0
, 1), mt
->valign
);
288 return h0
+ h1
+ (brw
->gen
>= 7 ? 12 : 11) * mt
->valign
;
293 align_cube(struct intel_mipmap_tree
*mt
)
295 /* The 965's sampler lays cachelines out according to how accesses
296 * in the texture surfaces run, so they may be "vertical" through
297 * memory. As a result, the docs say in Surface Padding Requirements:
298 * Sampling Engine Surfaces that two extra rows of padding are required.
300 if (mt
->target
== GL_TEXTURE_CUBE_MAP
)
301 mt
->total_height
+= 2;
305 gen9_use_linear_1d_layout(const struct brw_context
*brw
,
306 const struct intel_mipmap_tree
*mt
)
308 /* On Gen9+ the mipmap levels of a 1D surface are all laid out in a
309 * horizontal line. This isn't done for depth/stencil buffers however
310 * because those will be using a tiled layout
313 (mt
->target
== GL_TEXTURE_1D
||
314 mt
->target
== GL_TEXTURE_1D_ARRAY
)) {
315 GLenum base_format
= _mesa_get_format_base_format(mt
->format
);
317 if (base_format
!= GL_DEPTH_COMPONENT
&&
318 base_format
!= GL_DEPTH_STENCIL
&&
319 base_format
!= GL_STENCIL_INDEX
)
327 brw_miptree_layout_texture_array(struct brw_context
*brw
,
328 struct intel_mipmap_tree
*mt
)
330 unsigned height
= mt
->physical_height0
;
331 bool layout_1d
= gen9_use_linear_1d_layout(brw
, mt
);
335 gen9_miptree_layout_1d(mt
);
337 brw_miptree_layout_2d(mt
);
341 /* When using the horizontal layout the qpitch specifies the distance in
342 * pixels between array slices. The total_width is forced to be a
343 * multiple of the horizontal alignment in brw_miptree_layout_1d (in
344 * this case it's always 64). The vertical alignment is ignored.
346 mt
->qpitch
= mt
->total_width
;
348 mt
->qpitch
= brw_miptree_get_vertical_slice_pitch(brw
, mt
, 0);
349 /* Unlike previous generations the qpitch is a multiple of the
350 * compressed block size on Gen9 so physical_qpitch matches mt->qpitch.
352 physical_qpitch
= (mt
->compressed
&& brw
->gen
< 9 ? mt
->qpitch
/ 4 :
356 for (unsigned level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
358 img_height
= ALIGN_NPOT(height
, mt
->valign
);
360 img_height
/= mt
->valign
;
362 for (unsigned q
= 0; q
< mt
->level
[level
].depth
; q
++) {
363 if (mt
->array_layout
== ALL_SLICES_AT_EACH_LOD
) {
364 intel_miptree_set_image_offset(mt
, level
, q
, 0, q
* img_height
);
366 intel_miptree_set_image_offset(mt
, level
, q
, 0, q
* physical_qpitch
);
369 height
= minify(height
, 1);
371 if (mt
->array_layout
== ALL_LOD_IN_EACH_SLICE
)
372 mt
->total_height
= physical_qpitch
* mt
->physical_depth0
;
378 brw_miptree_layout_texture_3d(struct brw_context
*brw
,
379 struct intel_mipmap_tree
*mt
)
382 mt
->total_height
= 0;
387 _mesa_get_format_block_size(mt
->format
, &bw
, &bh
);
389 for (unsigned level
= mt
->first_level
; level
<= mt
->last_level
; level
++) {
390 unsigned WL
= MAX2(mt
->physical_width0
>> level
, 1);
391 unsigned HL
= MAX2(mt
->physical_height0
>> level
, 1);
392 unsigned DL
= MAX2(mt
->physical_depth0
>> level
, 1);
393 unsigned wL
= ALIGN_NPOT(WL
, mt
->halign
);
394 unsigned hL
= ALIGN_NPOT(HL
, mt
->valign
);
396 if (mt
->target
== GL_TEXTURE_CUBE_MAP
)
399 intel_miptree_set_level_info(mt
, level
, 0, 0, DL
);
401 for (unsigned q
= 0; q
< DL
; q
++) {
402 unsigned x
= (q
% (1 << level
)) * wL
;
403 unsigned y
= ysum
+ (q
>> level
) * hL
;
405 intel_miptree_set_image_offset(mt
, level
, q
, x
/ bw
, y
/ bh
);
406 mt
->total_width
= MAX2(mt
->total_width
, (x
+ wL
) / bw
);
407 mt
->total_height
= MAX2(mt
->total_height
, (y
+ hL
) / bh
);
410 ysum
+= ALIGN(DL
, 1 << level
) / (1 << level
) * hL
;
417 * \brief Helper function for intel_miptree_create().
420 brw_miptree_choose_tiling(struct brw_context
*brw
,
421 const struct intel_mipmap_tree
*mt
,
422 uint32_t layout_flags
)
424 if (mt
->format
== MESA_FORMAT_S_UINT8
) {
425 /* The stencil buffer is W tiled. However, we request from the kernel a
426 * non-tiled buffer because the GTT is incapable of W fencing.
428 return I915_TILING_NONE
;
431 /* Do not support changing the tiling for miptrees with pre-allocated BOs. */
432 assert((layout_flags
& MIPTREE_LAYOUT_FOR_BO
) == 0);
434 /* Some usages may want only one type of tiling, like depth miptrees (Y
435 * tiled), or temporary BOs for uploading data once (linear).
437 switch (layout_flags
& MIPTREE_LAYOUT_TILING_ANY
) {
438 case MIPTREE_LAYOUT_TILING_ANY
:
440 case MIPTREE_LAYOUT_TILING_Y
:
441 return I915_TILING_Y
;
442 case MIPTREE_LAYOUT_TILING_NONE
:
443 return I915_TILING_NONE
;
446 if (mt
->num_samples
> 1) {
447 /* From p82 of the Sandy Bridge PRM, dw3[1] of SURFACE_STATE ("Tiled
450 * [DevSNB+]: For multi-sample render targets, this field must be
451 * 1. MSRTs can only be tiled.
453 * Our usual reason for preferring X tiling (fast blits using the
454 * blitting engine) doesn't apply to MSAA, since we'll generally be
455 * downsampling or upsampling when blitting between the MSAA buffer
456 * and another buffer, and the blitting engine doesn't support that.
457 * So use Y tiling, since it makes better use of the cache.
459 return I915_TILING_Y
;
462 GLenum base_format
= _mesa_get_format_base_format(mt
->format
);
463 if (base_format
== GL_DEPTH_COMPONENT
||
464 base_format
== GL_DEPTH_STENCIL_EXT
)
465 return I915_TILING_Y
;
467 /* 1D textures (and 1D array textures) don't get any benefit from tiling,
468 * in fact it leads to a less efficient use of memory space and bandwidth
469 * due to tile alignment.
471 if (mt
->logical_height0
== 1)
472 return I915_TILING_NONE
;
474 int minimum_pitch
= mt
->total_width
* mt
->cpp
;
476 /* If the width is much smaller than a tile, don't bother tiling. */
477 if (minimum_pitch
< 64)
478 return I915_TILING_NONE
;
480 if (ALIGN(minimum_pitch
, 512) >= 32768) {
481 perf_debug("%dx%d miptree too large to blit, falling back to untiled",
482 mt
->total_width
, mt
->total_height
);
483 return I915_TILING_NONE
;
486 /* Pre-gen6 doesn't have BLORP to handle Y-tiling, so use X-tiling. */
488 return I915_TILING_X
;
490 /* From the Sandybridge PRM, Volume 1, Part 2, page 32:
491 * "NOTE: 128BPE Format Color Buffer ( render target ) MUST be either TileX
493 * 128 bits per pixel translates to 16 bytes per pixel. This is necessary
494 * all the way back to 965, but is permitted on Gen7+.
496 if (brw
->gen
< 7 && mt
->cpp
>= 16)
497 return I915_TILING_X
;
499 /* From the Ivy Bridge PRM, Vol4 Part1 2.12.2.1 (SURFACE_STATE for most
500 * messages), on p64, under the heading "Surface Vertical Alignment":
502 * This field must be set to VALIGN_4 for all tiled Y Render Target
505 * So if the surface is renderable and uses a vertical alignment of 2,
506 * force it to be X tiled. This is somewhat conservative (it's possible
507 * that the client won't ever render to this surface), but it's difficult
508 * to know that ahead of time. And besides, since we use a vertical
509 * alignment of 4 as often as we can, this shouldn't happen very often.
511 if (brw
->gen
== 7 && mt
->valign
== 2 &&
512 brw
->format_supported_as_render_target
[mt
->format
]) {
513 return I915_TILING_X
;
516 return I915_TILING_Y
| I915_TILING_X
;
520 intel_miptree_set_total_width_height(struct brw_context
*brw
,
521 struct intel_mipmap_tree
*mt
)
523 switch (mt
->target
) {
524 case GL_TEXTURE_CUBE_MAP
:
526 /* Gen4 stores cube maps as 3D textures. */
527 assert(mt
->physical_depth0
== 6);
528 brw_miptree_layout_texture_3d(brw
, mt
);
530 /* All other hardware stores cube maps as 2D arrays. */
531 brw_miptree_layout_texture_array(brw
, mt
);
537 brw_miptree_layout_texture_array(brw
, mt
);
539 brw_miptree_layout_texture_3d(brw
, mt
);
542 case GL_TEXTURE_1D_ARRAY
:
543 case GL_TEXTURE_2D_ARRAY
:
544 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
545 case GL_TEXTURE_CUBE_MAP_ARRAY
:
546 brw_miptree_layout_texture_array(brw
, mt
);
550 switch (mt
->msaa_layout
) {
551 case INTEL_MSAA_LAYOUT_UMS
:
552 case INTEL_MSAA_LAYOUT_CMS
:
553 brw_miptree_layout_texture_array(brw
, mt
);
555 case INTEL_MSAA_LAYOUT_NONE
:
556 case INTEL_MSAA_LAYOUT_IMS
:
557 if (gen9_use_linear_1d_layout(brw
, mt
))
558 gen9_miptree_layout_1d(mt
);
560 brw_miptree_layout_2d(mt
);
566 DBG("%s: %dx%dx%d\n", __func__
,
567 mt
->total_width
, mt
->total_height
, mt
->cpp
);
571 intel_miptree_set_alignment(struct brw_context
*brw
,
572 struct intel_mipmap_tree
*mt
,
573 uint32_t layout_flags
)
576 * From the "Alignment Unit Size" section of various specs, namely:
577 * - Gen3 Spec: "Memory Data Formats" Volume, Section 1.20.1.4
578 * - i965 and G45 PRMs: Volume 1, Section 6.17.3.4.
579 * - Ironlake and Sandybridge PRMs: Volume 1, Part 1, Section 7.18.3.4
580 * - BSpec (for Ivybridge and slight variations in separate stencil)
582 bool gen6_hiz_or_stencil
= false;
584 if (brw
->gen
== 6 && mt
->array_layout
== ALL_SLICES_AT_EACH_LOD
) {
585 const GLenum base_format
= _mesa_get_format_base_format(mt
->format
);
586 gen6_hiz_or_stencil
= _mesa_is_depth_or_stencil_format(base_format
);
589 if (gen6_hiz_or_stencil
) {
590 /* On gen6, we use ALL_SLICES_AT_EACH_LOD for stencil/hiz because the
591 * hardware doesn't support multiple mip levels on stencil/hiz.
593 * PRM Vol 2, Part 1, 7.5.3 Hierarchical Depth Buffer:
594 * "The hierarchical depth buffer does not support the LOD field"
596 * PRM Vol 2, Part 1, 7.5.4.1 Separate Stencil Buffer:
597 * "The stencil depth buffer does not support the LOD field"
599 if (mt
->format
== MESA_FORMAT_S_UINT8
) {
600 /* Stencil uses W tiling, so we force W tiling alignment for the
601 * ALL_SLICES_AT_EACH_LOD miptree layout.
605 assert((layout_flags
& MIPTREE_LAYOUT_FORCE_HALIGN16
) == 0);
607 /* Depth uses Y tiling, so we force need Y tiling alignment for the
608 * ALL_SLICES_AT_EACH_LOD miptree layout.
610 mt
->halign
= 128 / mt
->cpp
;
613 } else if (mt
->compressed
) {
614 /* The hardware alignment requirements for compressed textures
615 * happen to match the block boundaries.
617 _mesa_get_format_block_size(mt
->format
, &mt
->halign
, &mt
->valign
);
619 /* On Gen9+ we can pick our own alignment for compressed textures but it
620 * has to be a multiple of the block size. The minimum alignment we can
621 * pick is 4 so we effectively have to align to 4 times the block
628 } else if (mt
->format
== MESA_FORMAT_S_UINT8
) {
630 mt
->valign
= brw
->gen
>= 7 ? 8 : 4;
633 intel_horizontal_texture_alignment_unit(brw
, mt
, layout_flags
);
634 mt
->valign
= intel_vertical_texture_alignment_unit(brw
, mt
);
639 brw_miptree_layout(struct brw_context
*brw
,
640 struct intel_mipmap_tree
*mt
,
641 uint32_t layout_flags
)
643 intel_miptree_set_alignment(brw
, mt
, layout_flags
);
644 intel_miptree_set_total_width_height(brw
, mt
);
646 if (!mt
->total_width
|| !mt
->total_height
)
649 /* On Gen9+ the alignment values are expressed in multiples of the block
654 _mesa_get_format_block_size(mt
->format
, &i
, &j
);
659 if ((layout_flags
& MIPTREE_LAYOUT_FOR_BO
) == 0)
660 mt
->tiling
= brw_miptree_choose_tiling(brw
, mt
, layout_flags
);