17ec681f3Smrg/*
27ec681f3Smrg * Copyright (c) 2018 Intel Corporation
37ec681f3Smrg *
47ec681f3Smrg *  Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg *  copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg *  to deal in the Software without restriction, including without limitation
77ec681f3Smrg *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg *  and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg *  Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg *  The above copyright notice and this permission notice (including the next
127ec681f3Smrg *  paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg *  Software.
147ec681f3Smrg *
157ec681f3Smrg *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg *  IN THE SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg#include "isl_gfx9.h"
257ec681f3Smrg#include "isl_gfx12.h"
267ec681f3Smrg#include "isl_priv.h"
277ec681f3Smrg
287ec681f3Smrg/**
297ec681f3Smrg * @brief Filter out tiling flags that are incompatible with the surface.
307ec681f3Smrg *
317ec681f3Smrg * The resultant outgoing @a flags is a subset of the incoming @a flags. The
327ec681f3Smrg * outgoing flags may be empty (0x0) if the incoming flags were too
337ec681f3Smrg * restrictive.
347ec681f3Smrg *
357ec681f3Smrg * For example, if the surface will be used for a display
367ec681f3Smrg * (ISL_SURF_USAGE_DISPLAY_BIT), then this function filters out all tiling
377ec681f3Smrg * flags except ISL_TILING_4_BIT, ISL_TILING_X_BIT, and ISL_TILING_LINEAR_BIT.
387ec681f3Smrg */
397ec681f3Smrgvoid
407ec681f3Smrgisl_gfx125_filter_tiling(const struct isl_device *dev,
417ec681f3Smrg                         const struct isl_surf_init_info *restrict info,
427ec681f3Smrg                         isl_tiling_flags_t *flags)
437ec681f3Smrg{
447ec681f3Smrg   /* Clear flags unsupported on this hardware */
457ec681f3Smrg   assert(ISL_GFX_VERX10(dev) >= 125);
467ec681f3Smrg   *flags &= ISL_TILING_LINEAR_BIT |
477ec681f3Smrg             ISL_TILING_X_BIT |
487ec681f3Smrg             ISL_TILING_4_BIT |
497ec681f3Smrg             ISL_TILING_64_BIT;
507ec681f3Smrg
517ec681f3Smrg   if (isl_surf_usage_is_depth_or_stencil(info->usage))
527ec681f3Smrg      *flags &= ISL_TILING_4_BIT | ISL_TILING_64_BIT;
537ec681f3Smrg
547ec681f3Smrg   if (info->usage & ISL_SURF_USAGE_DISPLAY_BIT)
557ec681f3Smrg      *flags &= ~ISL_TILING_64_BIT;
567ec681f3Smrg
577ec681f3Smrg   /* From RENDER_SURFACE_STATE::TileMode,
587ec681f3Smrg    *
597ec681f3Smrg    *    TILEMODE_XMAJOR is only allowed if Surface Type is SURFTYPE_2D.
607ec681f3Smrg    *
617ec681f3Smrg    * X-tiling is only allowed for 2D surfaces.
627ec681f3Smrg    */
637ec681f3Smrg   if (info->dim != ISL_SURF_DIM_2D)
647ec681f3Smrg      *flags &= ~ISL_TILING_X_BIT;
657ec681f3Smrg
667ec681f3Smrg   /* ISL only implements Tile64 support for 2D surfaces. */
677ec681f3Smrg   if (info->dim != ISL_SURF_DIM_2D)
687ec681f3Smrg      *flags &= ~ISL_TILING_64_BIT;
697ec681f3Smrg
707ec681f3Smrg   /* From RENDER_SURFACE_STATE::NumberofMultisamples,
717ec681f3Smrg    *
727ec681f3Smrg    *    This field must not be programmed to anything other than
737ec681f3Smrg    *    [MULTISAMPLECOUNT_1] unless the Tile Mode field is programmed to
747ec681f3Smrg    *    Tile64.
757ec681f3Smrg    *
767ec681f3Smrg    * Tile64 is required for multisampling.
777ec681f3Smrg    */
787ec681f3Smrg   if (info->samples > 1)
797ec681f3Smrg      *flags &= ISL_TILING_64_BIT;
807ec681f3Smrg
817ec681f3Smrg   /* Tile64 is not defined for format sizes that are 24, 48, and 96 bpb. */
827ec681f3Smrg   if (isl_format_get_layout(info->format)->bpb % 3 == 0)
837ec681f3Smrg      *flags &= ~ISL_TILING_64_BIT;
847ec681f3Smrg}
857ec681f3Smrg
867ec681f3Smrgvoid
877ec681f3Smrgisl_gfx125_choose_image_alignment_el(const struct isl_device *dev,
887ec681f3Smrg                                     const struct isl_surf_init_info *restrict info,
897ec681f3Smrg                                     enum isl_tiling tiling,
907ec681f3Smrg                                     enum isl_dim_layout dim_layout,
917ec681f3Smrg                                     enum isl_msaa_layout msaa_layout,
927ec681f3Smrg                                     struct isl_extent3d *image_align_el)
937ec681f3Smrg{
947ec681f3Smrg   const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
957ec681f3Smrg
967ec681f3Smrg   if (tiling == ISL_TILING_64) {
977ec681f3Smrg      /* From RENDER_SURFACE_STATE::SurfaceHorizontalAlignment,
987ec681f3Smrg       *
997ec681f3Smrg       *   This field is ignored for Tile64 surface formats because horizontal
1007ec681f3Smrg       *   alignment is always to the start of the next tile in that case.
1017ec681f3Smrg       *
1027ec681f3Smrg       * From RENDER_SURFACE_STATE::SurfaceQPitch,
1037ec681f3Smrg       *
1047ec681f3Smrg       *   Because MSAA is only supported for Tile64, QPitch must also be
1057ec681f3Smrg       *   programmed to an aligned tile boundary for MSAA surfaces.
1067ec681f3Smrg       *
1077ec681f3Smrg       * Images in this surface must be tile-aligned.  The table on the Bspec
1087ec681f3Smrg       * page, "2D/CUBE Alignment Requirement", shows that the vertical
1097ec681f3Smrg       * alignment is also a tile height for non-MSAA as well.
1107ec681f3Smrg       */
1117ec681f3Smrg      struct isl_tile_info tile_info;
1127ec681f3Smrg      isl_tiling_get_info(tiling, info->dim, msaa_layout, fmtl->bpb,
1137ec681f3Smrg                          info->samples, &tile_info);
1147ec681f3Smrg
1157ec681f3Smrg      *image_align_el = isl_extent3d(tile_info.logical_extent_el.w,
1167ec681f3Smrg                                     tile_info.logical_extent_el.h,
1177ec681f3Smrg                                     1);
1187ec681f3Smrg   } else if (isl_surf_usage_is_depth(info->usage)) {
1197ec681f3Smrg      /* From RENDER_SURFACE_STATE::SurfaceHorizontalAlignment,
1207ec681f3Smrg       *
1217ec681f3Smrg       *    - 16b Depth Surfaces Must Be HALIGN=16Bytes (8texels)
1227ec681f3Smrg       *    - 32b Depth Surfaces Must Be HALIGN=32Bytes (8texels)
1237ec681f3Smrg       *
1247ec681f3Smrg       * From RENDER_SURFACE_STATE::SurfaceVerticalAlignment,
1257ec681f3Smrg       *
1267ec681f3Smrg       *    This field is intended to be set to VALIGN_4 if the surface
1277ec681f3Smrg       *    was rendered as a depth buffer [...]
1287ec681f3Smrg       *
1297ec681f3Smrg       * and
1307ec681f3Smrg       *
1317ec681f3Smrg       *    This field should also be set to VALIGN_8 if the surface was
1327ec681f3Smrg       *    rendered as a D16_UNORM depth buffer [...]
1337ec681f3Smrg       */
1347ec681f3Smrg      *image_align_el =
1357ec681f3Smrg         info->format != ISL_FORMAT_R16_UNORM ?
1367ec681f3Smrg         isl_extent3d(8, 4, 1) :
1377ec681f3Smrg         isl_extent3d(8, 8, 1);
1387ec681f3Smrg   } else if (isl_surf_usage_is_stencil(info->usage)) {
1397ec681f3Smrg      /* From RENDER_SURFACE_STATE::SurfaceHorizontalAlignment,
1407ec681f3Smrg       *
1417ec681f3Smrg       *    - Stencil Surfaces (8b) Must be HALIGN=16Bytes (16texels)
1427ec681f3Smrg       *
1437ec681f3Smrg       * From RENDER_SURFACE_STATE::SurfaceVerticalAlignment,
1447ec681f3Smrg       *
1457ec681f3Smrg       *    This field is intended to be set to VALIGN_8 only if
1467ec681f3Smrg       *    the surface was rendered as a stencil buffer, since stencil buffer
1477ec681f3Smrg       *    surfaces support only alignment of 8.
1487ec681f3Smrg       */
1497ec681f3Smrg      *image_align_el = isl_extent3d(16, 8, 1);
1507ec681f3Smrg   } else if (!isl_is_pow2(fmtl->bpb)) {
1517ec681f3Smrg      /* From RENDER_SURFACE_STATE::SurfaceHorizontalAlignment,
1527ec681f3Smrg       *
1537ec681f3Smrg       *    - Linear Surfaces surfaces must use HALIGN=128, including 1D which
1547ec681f3Smrg       *      is always Linear. For 24,48 and 96bpp this means 128texels.
1557ec681f3Smrg       *    - Tiled 24bpp, 48bpp and 96bpp surfaces must use HALIGN=16
1567ec681f3Smrg       */
1577ec681f3Smrg      *image_align_el = tiling == ISL_TILING_LINEAR ?
1587ec681f3Smrg         isl_extent3d(128, 4, 1) :
1597ec681f3Smrg         isl_extent3d(16, 4, 1);
1607ec681f3Smrg   } else {
1617ec681f3Smrg      /* From RENDER_SURFACE_STATE::SurfaceHorizontalAlignment,
1627ec681f3Smrg       *
1637ec681f3Smrg       *    - Losslessly Compressed Surfaces Must be HALIGN=128 for all
1647ec681f3Smrg       *      supported Bpp
1657ec681f3Smrg       *    - 64bpe and 128bpe Surfaces Must Be HALIGN=64Bytes or 128Bytes (4,
1667ec681f3Smrg       *      8 texels or 16 texels)
1677ec681f3Smrg       *    - Linear Surfaces surfaces must use HALIGN=128, including 1D which
1687ec681f3Smrg       *      is always Linear.
1697ec681f3Smrg       *
1707ec681f3Smrg       * Even though we could choose a horizontal alignment of 64B for certain
1717ec681f3Smrg       * 64 and 128-bit formats, we want to be able to enable CCS whenever
1727ec681f3Smrg       * possible and CCS requires 128B horizontal alignment.
1737ec681f3Smrg       */
1747ec681f3Smrg      *image_align_el = isl_extent3d(128 * 8 / fmtl->bpb, 4, 1);
1757ec681f3Smrg   }
1767ec681f3Smrg}
1777ec681f3Smrg
1787ec681f3Smrgvoid
1797ec681f3Smrgisl_gfx12_choose_image_alignment_el(const struct isl_device *dev,
1807ec681f3Smrg                                    const struct isl_surf_init_info *restrict info,
1817ec681f3Smrg                                    enum isl_tiling tiling,
1827ec681f3Smrg                                    enum isl_dim_layout dim_layout,
1837ec681f3Smrg                                    enum isl_msaa_layout msaa_layout,
1847ec681f3Smrg                                    struct isl_extent3d *image_align_el)
1857ec681f3Smrg{
1867ec681f3Smrg   /* Handled by isl_choose_image_alignment_el */
1877ec681f3Smrg   assert(info->format != ISL_FORMAT_HIZ);
1887ec681f3Smrg
1897ec681f3Smrg   const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
1907ec681f3Smrg   if (fmtl->txc == ISL_TXC_CCS) {
1917ec681f3Smrg      /* This CCS compresses a 2D-view of the entire surface. */
1927ec681f3Smrg      assert(info->levels == 1 && info->array_len == 1 && info->depth == 1);
1937ec681f3Smrg      *image_align_el = isl_extent3d(1, 1, 1);
1947ec681f3Smrg      return;
1957ec681f3Smrg   }
1967ec681f3Smrg
1977ec681f3Smrg   if (isl_surf_usage_is_depth(info->usage)) {
1987ec681f3Smrg      /* The alignment parameters for depth buffers are summarized in the
1997ec681f3Smrg       * following table:
2007ec681f3Smrg       *
2017ec681f3Smrg       *     Surface Format  |    MSAA     | Align Width | Align Height
2027ec681f3Smrg       *    -----------------+-------------+-------------+--------------
2037ec681f3Smrg       *       D16_UNORM     | 1x, 4x, 16x |      8      |      8
2047ec681f3Smrg       *     ----------------+-------------+-------------+--------------
2057ec681f3Smrg       *       D16_UNORM     |   2x, 8x    |     16      |      4
2067ec681f3Smrg       *     ----------------+-------------+-------------+--------------
2077ec681f3Smrg       *         other       |     any     |      8      |      4
2087ec681f3Smrg       *    -----------------+-------------+-------------+--------------
2097ec681f3Smrg       */
2107ec681f3Smrg      assert(isl_is_pow2(info->samples));
2117ec681f3Smrg      *image_align_el =
2127ec681f3Smrg         info->format != ISL_FORMAT_R16_UNORM ?
2137ec681f3Smrg         isl_extent3d(8, 4, 1) :
2147ec681f3Smrg         (info->samples == 2 || info->samples == 8 ?
2157ec681f3Smrg          isl_extent3d(16, 4, 1) : isl_extent3d(8, 8, 1));
2167ec681f3Smrg   } else if (isl_surf_usage_is_stencil(info->usage)) {
2177ec681f3Smrg      *image_align_el = isl_extent3d(16, 8, 1);
2187ec681f3Smrg   } else {
2197ec681f3Smrg      isl_gfx9_choose_image_alignment_el(dev, info, tiling, dim_layout,
2207ec681f3Smrg                                         msaa_layout, image_align_el);
2217ec681f3Smrg   }
2227ec681f3Smrg}
223