101e04c3fSmrg/*
201e04c3fSmrg * Copyright 2016 Intel Corporation
301e04c3fSmrg *
401e04c3fSmrg *  Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg *  copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg *  to deal in the Software without restriction, including without limitation
701e04c3fSmrg *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg *  and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg *  Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg *  The above copyright notice and this permission notice (including the next
1201e04c3fSmrg *  paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg *  Software.
1401e04c3fSmrg *
1501e04c3fSmrg *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2101e04c3fSmrg *  IN THE SOFTWARE.
2201e04c3fSmrg */
2301e04c3fSmrg
2401e04c3fSmrg#include <stdint.h>
2501e04c3fSmrg
2601e04c3fSmrg#define __gen_address_type uint64_t
2701e04c3fSmrg#define __gen_user_data void
2801e04c3fSmrg
2901e04c3fSmrgstatic uint64_t
3001e04c3fSmrg__gen_combine_address(__attribute__((unused)) void *data,
3101e04c3fSmrg                      __attribute__((unused)) void *loc, uint64_t addr,
3201e04c3fSmrg                      uint32_t delta)
3301e04c3fSmrg{
3401e04c3fSmrg   return addr + delta;
3501e04c3fSmrg}
3601e04c3fSmrg
3701e04c3fSmrg#include "genxml/gen_macros.h"
3801e04c3fSmrg#include "genxml/genX_pack.h"
3901e04c3fSmrg
4001e04c3fSmrg#include "isl_priv.h"
4101e04c3fSmrg
427ec681f3Smrg#if GFX_VER >= 7
437ec681f3Smrgstatic const uint8_t isl_encode_halign(uint8_t halign)
447ec681f3Smrg{
457ec681f3Smrg   switch (halign) {
467ec681f3Smrg#if GFX_VERx10 >= 125
477ec681f3Smrg   case  16: return HALIGN_16;
487ec681f3Smrg   case  32: return HALIGN_32;
497ec681f3Smrg   case  64: return HALIGN_64;
507ec681f3Smrg   case 128: return HALIGN_128;
517ec681f3Smrg#elif GFX_VER >= 8
527ec681f3Smrg   case   4: return HALIGN_4;
537ec681f3Smrg   case   8: return HALIGN_8;
547ec681f3Smrg   case  16: return HALIGN_16;
557ec681f3Smrg#else
567ec681f3Smrg   case   4: return HALIGN_4;
577ec681f3Smrg   case   8: return HALIGN_8;
587ec681f3Smrg#endif
597ec681f3Smrg   default: unreachable("Invalid halign");
607ec681f3Smrg   }
617ec681f3Smrg}
6201e04c3fSmrg#endif
6301e04c3fSmrg
647ec681f3Smrg#if GFX_VER >= 6
657ec681f3Smrgstatic const uint8_t isl_encode_valign(uint8_t valign)
667ec681f3Smrg{
677ec681f3Smrg   switch (valign) {
687ec681f3Smrg#if GFX_VER >= 8
697ec681f3Smrg   case   4: return VALIGN_4;
707ec681f3Smrg   case   8: return VALIGN_8;
717ec681f3Smrg   case  16: return VALIGN_16;
727ec681f3Smrg#else
737ec681f3Smrg   case   2: return VALIGN_2;
747ec681f3Smrg   case   4: return VALIGN_4;
757ec681f3Smrg#endif
767ec681f3Smrg   default: unreachable("Invalid valign");
777ec681f3Smrg   }
787ec681f3Smrg}
7901e04c3fSmrg#endif
8001e04c3fSmrg
817ec681f3Smrg#if GFX_VER >= 8
827ec681f3Smrgstatic const uint8_t isl_encode_tiling[] = {
8301e04c3fSmrg   [ISL_TILING_LINEAR]  = LINEAR,
8401e04c3fSmrg   [ISL_TILING_X]       = XMAJOR,
857ec681f3Smrg#if GFX_VERx10 >= 125
867ec681f3Smrg   [ISL_TILING_4]       = TILE4,
877ec681f3Smrg   [ISL_TILING_64]      = TILE64,
887ec681f3Smrg#else
8901e04c3fSmrg   [ISL_TILING_Y0]      = YMAJOR,
9001e04c3fSmrg   [ISL_TILING_Yf]      = YMAJOR,
9101e04c3fSmrg   [ISL_TILING_Ys]      = YMAJOR,
927ec681f3Smrg#endif
937ec681f3Smrg#if GFX_VER <= 11
9401e04c3fSmrg   [ISL_TILING_W]       = WMAJOR,
957ec681f3Smrg#endif
9601e04c3fSmrg};
9701e04c3fSmrg#endif
9801e04c3fSmrg
997ec681f3Smrg#if GFX_VER >= 7
1007ec681f3Smrgstatic const uint32_t isl_encode_multisample_layout[] = {
10101e04c3fSmrg   [ISL_MSAA_LAYOUT_NONE]           = MSFMT_MSS,
10201e04c3fSmrg   [ISL_MSAA_LAYOUT_INTERLEAVED]    = MSFMT_DEPTH_STENCIL,
10301e04c3fSmrg   [ISL_MSAA_LAYOUT_ARRAY]          = MSFMT_MSS,
10401e04c3fSmrg};
10501e04c3fSmrg#endif
10601e04c3fSmrg
1077ec681f3Smrg#if GFX_VER >= 12
1087ec681f3Smrgstatic const uint32_t isl_encode_aux_mode[] = {
1097ec681f3Smrg   [ISL_AUX_USAGE_NONE] = AUX_NONE,
1107ec681f3Smrg   [ISL_AUX_USAGE_MC] = AUX_NONE,
1117ec681f3Smrg   [ISL_AUX_USAGE_MCS] = AUX_CCS_E,
1127ec681f3Smrg   [ISL_AUX_USAGE_GFX12_CCS_E] = AUX_CCS_E,
1137ec681f3Smrg   [ISL_AUX_USAGE_CCS_E] = AUX_CCS_E,
1147ec681f3Smrg   [ISL_AUX_USAGE_HIZ_CCS_WT] = AUX_CCS_E,
1157ec681f3Smrg   [ISL_AUX_USAGE_MCS_CCS] = AUX_MCS_LCE,
1167ec681f3Smrg   [ISL_AUX_USAGE_STC_CCS] = AUX_CCS_E,
1177ec681f3Smrg};
1187ec681f3Smrg#elif GFX_VER >= 9
1197ec681f3Smrgstatic const uint32_t isl_encode_aux_mode[] = {
12001e04c3fSmrg   [ISL_AUX_USAGE_NONE] = AUX_NONE,
12101e04c3fSmrg   [ISL_AUX_USAGE_HIZ] = AUX_HIZ,
12201e04c3fSmrg   [ISL_AUX_USAGE_MCS] = AUX_CCS_D,
12301e04c3fSmrg   [ISL_AUX_USAGE_CCS_D] = AUX_CCS_D,
12401e04c3fSmrg   [ISL_AUX_USAGE_CCS_E] = AUX_CCS_E,
12501e04c3fSmrg};
1267ec681f3Smrg#elif GFX_VER >= 8
1277ec681f3Smrgstatic const uint32_t isl_encode_aux_mode[] = {
12801e04c3fSmrg   [ISL_AUX_USAGE_NONE] = AUX_NONE,
12901e04c3fSmrg   [ISL_AUX_USAGE_HIZ] = AUX_HIZ,
13001e04c3fSmrg   [ISL_AUX_USAGE_MCS] = AUX_MCS,
13101e04c3fSmrg   [ISL_AUX_USAGE_CCS_D] = AUX_MCS,
13201e04c3fSmrg};
13301e04c3fSmrg#endif
13401e04c3fSmrg
13501e04c3fSmrgstatic uint8_t
13601e04c3fSmrgget_surftype(enum isl_surf_dim dim, isl_surf_usage_flags_t usage)
13701e04c3fSmrg{
13801e04c3fSmrg   switch (dim) {
13901e04c3fSmrg   default:
14001e04c3fSmrg      unreachable("bad isl_surf_dim");
14101e04c3fSmrg   case ISL_SURF_DIM_1D:
14201e04c3fSmrg      assert(!(usage & ISL_SURF_USAGE_CUBE_BIT));
14301e04c3fSmrg      return SURFTYPE_1D;
14401e04c3fSmrg   case ISL_SURF_DIM_2D:
14501e04c3fSmrg      if ((usage & ISL_SURF_USAGE_CUBE_BIT) &&
14601e04c3fSmrg          (usage & ISL_SURF_USAGE_TEXTURE_BIT)) {
14701e04c3fSmrg         /* We need SURFTYPE_CUBE to make cube sampling work */
14801e04c3fSmrg         return SURFTYPE_CUBE;
14901e04c3fSmrg      } else {
15001e04c3fSmrg         /* Everything else (render and storage) treat cubes as plain
15101e04c3fSmrg          * 2D array textures
15201e04c3fSmrg          */
15301e04c3fSmrg         return SURFTYPE_2D;
15401e04c3fSmrg      }
15501e04c3fSmrg   case ISL_SURF_DIM_3D:
15601e04c3fSmrg      assert(!(usage & ISL_SURF_USAGE_CUBE_BIT));
15701e04c3fSmrg      return SURFTYPE_3D;
15801e04c3fSmrg   }
15901e04c3fSmrg}
16001e04c3fSmrg
16101e04c3fSmrg/**
16201e04c3fSmrg * Get the horizontal and vertical alignment in the units expected by the
16301e04c3fSmrg * hardware.  Note that this does NOT give you the actual hardware enum values
1647ec681f3Smrg * but an index into the isl_encode_[hv]align arrays above.
16501e04c3fSmrg */
16601e04c3fSmrgUNUSED static struct isl_extent3d
16701e04c3fSmrgget_image_alignment(const struct isl_surf *surf)
16801e04c3fSmrg{
1697ec681f3Smrg   if (GFX_VERx10 >= 125) {
1707ec681f3Smrg      if (surf->tiling == ISL_TILING_64) {
1717ec681f3Smrg         /* The hardware ignores the alignment values. Anyway, the surface's
1727ec681f3Smrg          * true alignment is likely outside the enum range of HALIGN* and
1737ec681f3Smrg          * VALIGN*.
1747ec681f3Smrg          */
1757ec681f3Smrg         return isl_extent3d(128, 4, 1);
1767ec681f3Smrg      } else if (isl_format_get_layout(surf->format)->bpb % 3 == 0) {
1777ec681f3Smrg         /* On XeHP, RENDER_SURFACE_STATE.SurfaceHorizontalAlignment is in
1787ec681f3Smrg          * units of elements for 24, 48, and 96 bpb formats.
1797ec681f3Smrg          */
1807ec681f3Smrg         return isl_surf_get_image_alignment_el(surf);
1817ec681f3Smrg      } else {
1827ec681f3Smrg         /* On XeHP, RENDER_SURFACE_STATE.SurfaceHorizontalAlignment is in
1837ec681f3Smrg          * units of bytes for formats that are powers of two.
1847ec681f3Smrg          */
1857ec681f3Smrg         const uint32_t bs = isl_format_get_layout(surf->format)->bpb / 8;
1867ec681f3Smrg         return isl_extent3d(surf->image_alignment_el.w * bs,
1877ec681f3Smrg                             surf->image_alignment_el.h,
1887ec681f3Smrg                             surf->image_alignment_el.d);
1897ec681f3Smrg      }
1907ec681f3Smrg   } else if (GFX_VER >= 9) {
19101e04c3fSmrg      if (isl_tiling_is_std_y(surf->tiling) ||
1927ec681f3Smrg          surf->dim_layout == ISL_DIM_LAYOUT_GFX9_1D) {
19301e04c3fSmrg         /* The hardware ignores the alignment values. Anyway, the surface's
19401e04c3fSmrg          * true alignment is likely outside the enum range of HALIGN* and
19501e04c3fSmrg          * VALIGN*.
19601e04c3fSmrg          */
19701e04c3fSmrg         return isl_extent3d(4, 4, 1);
19801e04c3fSmrg      } else {
19901e04c3fSmrg         /* In Skylake, RENDER_SUFFACE_STATE.SurfaceVerticalAlignment is in units
20001e04c3fSmrg          * of surface elements (not pixels nor samples). For compressed formats,
20101e04c3fSmrg          * a "surface element" is defined as a compression block.  For example,
20201e04c3fSmrg          * if SurfaceVerticalAlignment is VALIGN_4 and SurfaceFormat is an ETC2
20301e04c3fSmrg          * format (ETC2 has a block height of 4), then the vertical alignment is
20401e04c3fSmrg          * 4 compression blocks or, equivalently, 16 pixels.
20501e04c3fSmrg          */
20601e04c3fSmrg         return isl_surf_get_image_alignment_el(surf);
20701e04c3fSmrg      }
20801e04c3fSmrg   } else {
20901e04c3fSmrg      /* Pre-Skylake, RENDER_SUFFACE_STATE.SurfaceVerticalAlignment is in
21001e04c3fSmrg       * units of surface samples.  For example, if SurfaceVerticalAlignment
21101e04c3fSmrg       * is VALIGN_4 and the surface is singlesampled, then for any surface
21201e04c3fSmrg       * format (compressed or not) the vertical alignment is
21301e04c3fSmrg       * 4 pixels.
21401e04c3fSmrg       */
21501e04c3fSmrg      return isl_surf_get_image_alignment_sa(surf);
21601e04c3fSmrg   }
21701e04c3fSmrg}
21801e04c3fSmrg
2197ec681f3Smrg#if GFX_VER >= 8
22001e04c3fSmrgstatic uint32_t
22101e04c3fSmrgget_qpitch(const struct isl_surf *surf)
22201e04c3fSmrg{
22301e04c3fSmrg   switch (surf->dim_layout) {
22401e04c3fSmrg   default:
22501e04c3fSmrg      unreachable("Bad isl_surf_dim");
2267ec681f3Smrg   case ISL_DIM_LAYOUT_GFX4_2D:
2277ec681f3Smrg      if (GFX_VER >= 9) {
22801e04c3fSmrg         if (surf->dim == ISL_SURF_DIM_3D && surf->tiling == ISL_TILING_W) {
22901e04c3fSmrg            /* This is rather annoying and completely undocumented.  It
23001e04c3fSmrg             * appears that the hardware has a bug (or undocumented feature)
23101e04c3fSmrg             * regarding stencil buffers most likely related to the way
23201e04c3fSmrg             * W-tiling is handled as modified Y-tiling.  If you bind a 3-D
23301e04c3fSmrg             * stencil buffer normally, and use texelFetch on it, the z or
23401e04c3fSmrg             * array index will get implicitly multiplied by 2 for no obvious
23501e04c3fSmrg             * reason.  The fix appears to be to divide qpitch by 2 for
23601e04c3fSmrg             * W-tiled surfaces.
23701e04c3fSmrg             */
23801e04c3fSmrg            return isl_surf_get_array_pitch_el_rows(surf) / 2;
23901e04c3fSmrg         } else {
24001e04c3fSmrg            return isl_surf_get_array_pitch_el_rows(surf);
24101e04c3fSmrg         }
24201e04c3fSmrg      } else {
24301e04c3fSmrg         /* From the Broadwell PRM for RENDER_SURFACE_STATE.QPitch
24401e04c3fSmrg          *
24501e04c3fSmrg          *    "This field must be set to an integer multiple of the Surface
24601e04c3fSmrg          *    Vertical Alignment. For compressed textures (BC*, FXT1,
24701e04c3fSmrg          *    ETC*, and EAC* Surface Formats), this field is in units of
24801e04c3fSmrg          *    rows in the uncompressed surface, and must be set to an
24901e04c3fSmrg          *    integer multiple of the vertical alignment parameter "j"
25001e04c3fSmrg          *    defined in the Common Surface Formats section."
25101e04c3fSmrg          */
25201e04c3fSmrg         return isl_surf_get_array_pitch_sa_rows(surf);
25301e04c3fSmrg      }
2547ec681f3Smrg   case ISL_DIM_LAYOUT_GFX9_1D:
25501e04c3fSmrg      /* QPitch is usually expressed as rows of surface elements (where
25601e04c3fSmrg       * a surface element is an compression block or a single surface
25701e04c3fSmrg       * sample). Skylake 1D is an outlier.
25801e04c3fSmrg       *
25901e04c3fSmrg       * From the Skylake BSpec >> Memory Views >> Common Surface
26001e04c3fSmrg       * Formats >> Surface Layout and Tiling >> 1D Surfaces:
26101e04c3fSmrg       *
26201e04c3fSmrg       *    Surface QPitch specifies the distance in pixels between array
26301e04c3fSmrg       *    slices.
26401e04c3fSmrg       */
26501e04c3fSmrg      return isl_surf_get_array_pitch_el(surf);
2667ec681f3Smrg   case ISL_DIM_LAYOUT_GFX4_3D:
2677ec681f3Smrg      /* QPitch doesn't make sense for ISL_DIM_LAYOUT_GFX4_3D since it uses a
26801e04c3fSmrg       * different pitch at each LOD.  Also, the QPitch field is ignored for
26901e04c3fSmrg       * these surfaces.  From the Broadwell PRM documentation for QPitch:
27001e04c3fSmrg       *
27101e04c3fSmrg       *    This field specifies the distance in rows between array slices. It
27201e04c3fSmrg       *    is used only in the following cases:
27301e04c3fSmrg       *     - Surface Array is enabled OR
27401e04c3fSmrg       *     - Number of Mulitsamples is not NUMSAMPLES_1 and Multisampled
27501e04c3fSmrg       *       Surface Storage Format set to MSFMT_MSS OR
27601e04c3fSmrg       *     - Surface Type is SURFTYPE_CUBE
27701e04c3fSmrg       *
27801e04c3fSmrg       * None of the three conditions above can possibly apply to a 3D surface
27901e04c3fSmrg       * so it is safe to just set QPitch to 0.
28001e04c3fSmrg       */
28101e04c3fSmrg      return 0;
28201e04c3fSmrg   }
28301e04c3fSmrg}
2847ec681f3Smrg#endif /* GFX_VER >= 8 */
28501e04c3fSmrg
28601e04c3fSmrgvoid
28701e04c3fSmrgisl_genX(surf_fill_state_s)(const struct isl_device *dev, void *state,
28801e04c3fSmrg                            const struct isl_surf_fill_state_info *restrict info)
28901e04c3fSmrg{
29001e04c3fSmrg   struct GENX(RENDER_SURFACE_STATE) s = { 0 };
29101e04c3fSmrg
29201e04c3fSmrg   s.SurfaceType = get_surftype(info->surf->dim, info->view->usage);
29301e04c3fSmrg
29401e04c3fSmrg   if (info->view->usage & ISL_SURF_USAGE_RENDER_TARGET_BIT)
29501e04c3fSmrg      assert(isl_format_supports_rendering(dev->info, info->view->format));
29601e04c3fSmrg   else if (info->view->usage & ISL_SURF_USAGE_TEXTURE_BIT)
29701e04c3fSmrg      assert(isl_format_supports_sampling(dev->info, info->view->format));
29801e04c3fSmrg
29901e04c3fSmrg   /* From the Sky Lake PRM Vol. 2d, RENDER_SURFACE_STATE::SurfaceFormat
30001e04c3fSmrg    *
30101e04c3fSmrg    *    This field cannot be a compressed (BC*, DXT*, FXT*, ETC*, EAC*)
30201e04c3fSmrg    *    format if the Surface Type is SURFTYPE_1D
30301e04c3fSmrg    */
30401e04c3fSmrg   if (info->surf->dim == ISL_SURF_DIM_1D)
30501e04c3fSmrg      assert(!isl_format_is_compressed(info->view->format));
30601e04c3fSmrg
30701e04c3fSmrg   if (isl_format_is_compressed(info->surf->format)) {
30801e04c3fSmrg      /* You're not allowed to make a view of a compressed format with any
30901e04c3fSmrg       * format other than the surface format.  None of the userspace APIs
31001e04c3fSmrg       * allow for this directly and doing so would mess up a number of
31101e04c3fSmrg       * surface parameters such as Width, Height, and alignments.  Ideally,
31201e04c3fSmrg       * we'd like to assert that the two formats match.  However, we have an
31301e04c3fSmrg       * S3TC workaround that requires us to do reinterpretation.  So assert
31401e04c3fSmrg       * that they're at least the same bpb and block size.
31501e04c3fSmrg       */
3167ec681f3Smrg      ASSERTED const struct isl_format_layout *surf_fmtl =
31701e04c3fSmrg         isl_format_get_layout(info->surf->format);
3187ec681f3Smrg      ASSERTED const struct isl_format_layout *view_fmtl =
31901e04c3fSmrg         isl_format_get_layout(info->surf->format);
32001e04c3fSmrg      assert(surf_fmtl->bpb == view_fmtl->bpb);
32101e04c3fSmrg      assert(surf_fmtl->bw == view_fmtl->bw);
32201e04c3fSmrg      assert(surf_fmtl->bh == view_fmtl->bh);
32301e04c3fSmrg   }
32401e04c3fSmrg
32501e04c3fSmrg   s.SurfaceFormat = info->view->format;
32601e04c3fSmrg
3277ec681f3Smrg#if GFX_VER >= 12
3287ec681f3Smrg   /* The BSpec description of this field says:
3297ec681f3Smrg    *
3307ec681f3Smrg    *    "This bit field, when set, indicates if the resource is created as
3317ec681f3Smrg    *    Depth/Stencil resource."
3327ec681f3Smrg    *
3337ec681f3Smrg    *    "SW must set this bit for any resource that was created with
3347ec681f3Smrg    *    Depth/Stencil resource flag. Setting this bit allows HW to properly
3357ec681f3Smrg    *    interpret the data-layout for various cases. For any resource that's
3367ec681f3Smrg    *    created without Depth/Stencil resource flag, it must be reset."
3377ec681f3Smrg    *
3387ec681f3Smrg    * Even though the docs for this bit seem to imply that it's required for
3397ec681f3Smrg    * anything which might have been used for depth/stencil, empirical
3407ec681f3Smrg    * evidence suggests that it only affects CCS compression usage.  There are
3417ec681f3Smrg    * a few things which back this up:
3427ec681f3Smrg    *
3437ec681f3Smrg    *  1. The docs are also pretty clear that this bit was added as part
3447ec681f3Smrg    *     of enabling Gfx12 depth/stencil lossless compression.
3457ec681f3Smrg    *
3467ec681f3Smrg    *  2. The only new difference between depth/stencil and color images on
3477ec681f3Smrg    *     Gfx12 (where the bit was added) is how they treat CCS compression.
3487ec681f3Smrg    *     All other differences such as alignment requirements and MSAA layout
3497ec681f3Smrg    *     are already covered by other bits.
3507ec681f3Smrg    *
3517ec681f3Smrg    * Under these assumptions, it makes sense for ISL to model this bit as
3527ec681f3Smrg    * being an extension of AuxiliarySurfaceMode where STC_CCS and HIZ_CCS_WT
3537ec681f3Smrg    * are indicated by AuxiliarySurfaceMode == CCS_E and DepthStencilResource
3547ec681f3Smrg    * == true.
3557ec681f3Smrg    */
3567ec681f3Smrg   s.DepthStencilResource = info->aux_usage == ISL_AUX_USAGE_HIZ_CCS_WT ||
3577ec681f3Smrg                            info->aux_usage == ISL_AUX_USAGE_STC_CCS;
3587ec681f3Smrg#endif
3597ec681f3Smrg
3607ec681f3Smrg#if GFX_VER <= 5
36101e04c3fSmrg   s.ColorBufferComponentWriteDisables = info->write_disables;
3627ec681f3Smrg   s.ColorBlendEnable = info->blend_enable;
36301e04c3fSmrg#else
36401e04c3fSmrg   assert(info->write_disables == 0);
36501e04c3fSmrg#endif
36601e04c3fSmrg
3677ec681f3Smrg#if GFX_VERx10 == 75
36801e04c3fSmrg   s.IntegerSurfaceFormat =
36901e04c3fSmrg      isl_format_has_int_channel((enum isl_format) s.SurfaceFormat);
37001e04c3fSmrg#endif
37101e04c3fSmrg
37201e04c3fSmrg   assert(info->surf->logical_level0_px.width > 0 &&
37301e04c3fSmrg          info->surf->logical_level0_px.height > 0);
37401e04c3fSmrg
37501e04c3fSmrg   s.Width = info->surf->logical_level0_px.width - 1;
37601e04c3fSmrg   s.Height = info->surf->logical_level0_px.height - 1;
37701e04c3fSmrg
3787ec681f3Smrg   /* In the gfx6 PRM Volume 1 Part 1: Graphics Core, Section 7.18.3.7.1
37901e04c3fSmrg    * (Surface Arrays For all surfaces other than separate stencil buffer):
38001e04c3fSmrg    *
38101e04c3fSmrg    * "[DevSNB] Errata: Sampler MSAA Qpitch will be 4 greater than the value
38201e04c3fSmrg    *  calculated in the equation above , for every other odd Surface Height
38301e04c3fSmrg    *  starting from 1 i.e. 1,5,9,13"
38401e04c3fSmrg    *
38501e04c3fSmrg    * Since this Qpitch errata only impacts the sampler, we have to adjust the
38601e04c3fSmrg    * input for the rendering surface to achieve the same qpitch. For the
38701e04c3fSmrg    * affected heights, we increment the height by 1 for the rendering
38801e04c3fSmrg    * surface.
38901e04c3fSmrg    */
3907ec681f3Smrg   if (GFX_VER == 6 && (info->view->usage & ISL_SURF_USAGE_RENDER_TARGET_BIT) &&
39101e04c3fSmrg       info->surf->samples > 1 &&
39201e04c3fSmrg       (info->surf->logical_level0_px.height % 4) == 1)
39301e04c3fSmrg      s.Height++;
39401e04c3fSmrg
39501e04c3fSmrg   switch (s.SurfaceType) {
39601e04c3fSmrg   case SURFTYPE_1D:
39701e04c3fSmrg   case SURFTYPE_2D:
39801e04c3fSmrg      /* From the Ivy Bridge PRM >> RENDER_SURFACE_STATE::MinimumArrayElement:
39901e04c3fSmrg       *
40001e04c3fSmrg       *    "If Number of Multisamples is not MULTISAMPLECOUNT_1, this field
40101e04c3fSmrg       *    must be set to zero if this surface is used with sampling engine
40201e04c3fSmrg       *    messages."
40301e04c3fSmrg       *
40401e04c3fSmrg       * This restriction appears to exist only on Ivy Bridge.
40501e04c3fSmrg       */
4067ec681f3Smrg      if (GFX_VERx10 == 70 && !ISL_DEV_IS_BAYTRAIL(dev) &&
40701e04c3fSmrg          (info->view->usage & ISL_SURF_USAGE_TEXTURE_BIT) &&
40801e04c3fSmrg          info->surf->samples > 1)
40901e04c3fSmrg         assert(info->view->base_array_layer == 0);
41001e04c3fSmrg
41101e04c3fSmrg      s.MinimumArrayElement = info->view->base_array_layer;
41201e04c3fSmrg
41301e04c3fSmrg      /* From the Broadwell PRM >> RENDER_SURFACE_STATE::Depth:
41401e04c3fSmrg       *
41501e04c3fSmrg       *    For SURFTYPE_1D, 2D, and CUBE: The range of this field is reduced
41601e04c3fSmrg       *    by one for each increase from zero of Minimum Array Element. For
41701e04c3fSmrg       *    example, if Minimum Array Element is set to 1024 on a 2D surface,
41801e04c3fSmrg       *    the range of this field is reduced to [0,1023].
41901e04c3fSmrg       *
42001e04c3fSmrg       * In other words, 'Depth' is the number of array layers.
42101e04c3fSmrg       */
42201e04c3fSmrg      s.Depth = info->view->array_len - 1;
42301e04c3fSmrg
42401e04c3fSmrg      /* From the Broadwell PRM >> RENDER_SURFACE_STATE::RenderTargetViewExtent:
42501e04c3fSmrg       *
42601e04c3fSmrg       *    For Render Target and Typed Dataport 1D and 2D Surfaces:
42701e04c3fSmrg       *    This field must be set to the same value as the Depth field.
42801e04c3fSmrg       */
42901e04c3fSmrg      if (info->view->usage & (ISL_SURF_USAGE_RENDER_TARGET_BIT |
43001e04c3fSmrg                               ISL_SURF_USAGE_STORAGE_BIT))
43101e04c3fSmrg         s.RenderTargetViewExtent = s.Depth;
43201e04c3fSmrg      break;
43301e04c3fSmrg   case SURFTYPE_CUBE:
43401e04c3fSmrg      s.MinimumArrayElement = info->view->base_array_layer;
43501e04c3fSmrg      /* Same as SURFTYPE_2D, but divided by 6 */
43601e04c3fSmrg      s.Depth = info->view->array_len / 6 - 1;
43701e04c3fSmrg      if (info->view->usage & (ISL_SURF_USAGE_RENDER_TARGET_BIT |
43801e04c3fSmrg                               ISL_SURF_USAGE_STORAGE_BIT))
43901e04c3fSmrg         s.RenderTargetViewExtent = s.Depth;
44001e04c3fSmrg      break;
44101e04c3fSmrg   case SURFTYPE_3D:
44201e04c3fSmrg      /* From the Broadwell PRM >> RENDER_SURFACE_STATE::Depth:
44301e04c3fSmrg       *
44401e04c3fSmrg       *    If the volume texture is MIP-mapped, this field specifies the
44501e04c3fSmrg       *    depth of the base MIP level.
44601e04c3fSmrg       */
44701e04c3fSmrg      s.Depth = info->surf->logical_level0_px.depth - 1;
44801e04c3fSmrg
44901e04c3fSmrg      /* From the Broadwell PRM >> RENDER_SURFACE_STATE::RenderTargetViewExtent:
45001e04c3fSmrg       *
45101e04c3fSmrg       *    For Render Target and Typed Dataport 3D Surfaces: This field
45201e04c3fSmrg       *    indicates the extent of the accessible 'R' coordinates minus 1 on
45301e04c3fSmrg       *    the LOD currently being rendered to.
45401e04c3fSmrg       *
45501e04c3fSmrg       * The docs specify that this only matters for render targets and
45601e04c3fSmrg       * surfaces used with typed dataport messages.  Prior to Ivy Bridge, the
45701e04c3fSmrg       * Depth field has more bits than RenderTargetViewExtent so we can have
45801e04c3fSmrg       * textures with more levels than we can render to.  In order to prevent
45901e04c3fSmrg       * assert-failures in the packing function below, we only set the field
46001e04c3fSmrg       * when it's actually going to be used by the hardware.
46101e04c3fSmrg       *
46201e04c3fSmrg       * Similaraly, the MinimumArrayElement field is ignored by all hardware
46301e04c3fSmrg       * prior to Sky Lake when texturing and we want it set to 0 anyway.
46401e04c3fSmrg       * Since it's already initialized to 0, we can just leave it alone for
46501e04c3fSmrg       * texture surfaces.
46601e04c3fSmrg       */
46701e04c3fSmrg      if (info->view->usage & (ISL_SURF_USAGE_RENDER_TARGET_BIT |
46801e04c3fSmrg                               ISL_SURF_USAGE_STORAGE_BIT)) {
46901e04c3fSmrg         s.MinimumArrayElement = info->view->base_array_layer;
47001e04c3fSmrg         s.RenderTargetViewExtent = info->view->array_len - 1;
47101e04c3fSmrg      }
47201e04c3fSmrg      break;
47301e04c3fSmrg   default:
47401e04c3fSmrg      unreachable("bad SurfaceType");
47501e04c3fSmrg   }
47601e04c3fSmrg
4777ec681f3Smrg#if GFX_VER >= 12
4787ec681f3Smrg   /* Wa_1806565034: Only set SurfaceArray if arrayed surface is > 1. */
4797ec681f3Smrg   s.SurfaceArray = info->surf->dim != ISL_SURF_DIM_3D &&
4807ec681f3Smrg      info->view->array_len > 1;
4817ec681f3Smrg#elif GFX_VER >= 7
48201e04c3fSmrg   s.SurfaceArray = info->surf->dim != ISL_SURF_DIM_3D;
48301e04c3fSmrg#endif
48401e04c3fSmrg
48501e04c3fSmrg   if (info->view->usage & ISL_SURF_USAGE_RENDER_TARGET_BIT) {
48601e04c3fSmrg      /* For render target surfaces, the hardware interprets field
48701e04c3fSmrg       * MIPCount/LOD as LOD. The Broadwell PRM says:
48801e04c3fSmrg       *
48901e04c3fSmrg       *    MIPCountLOD defines the LOD that will be rendered into.
49001e04c3fSmrg       *    SurfaceMinLOD is ignored.
49101e04c3fSmrg       */
49201e04c3fSmrg      s.MIPCountLOD = info->view->base_level;
49301e04c3fSmrg      s.SurfaceMinLOD = 0;
49401e04c3fSmrg   } else {
49501e04c3fSmrg      /* For non render target surfaces, the hardware interprets field
49601e04c3fSmrg       * MIPCount/LOD as MIPCount.  The range of levels accessible by the
49701e04c3fSmrg       * sampler engine is [SurfaceMinLOD, SurfaceMinLOD + MIPCountLOD].
49801e04c3fSmrg       */
49901e04c3fSmrg      s.SurfaceMinLOD = info->view->base_level;
50001e04c3fSmrg      s.MIPCountLOD = MAX(info->view->levels, 1) - 1;
50101e04c3fSmrg   }
50201e04c3fSmrg
5037ec681f3Smrg#if GFX_VER >= 9
50401e04c3fSmrg   /* We don't use miptails yet.  The PRM recommends that you set "Mip Tail
50501e04c3fSmrg    * Start LOD" to 15 to prevent the hardware from trying to use them.
50601e04c3fSmrg    */
50701e04c3fSmrg   s.TiledResourceMode = NONE;
50801e04c3fSmrg   s.MipTailStartLOD = 15;
50901e04c3fSmrg#endif
51001e04c3fSmrg
5117ec681f3Smrg#if GFX_VER >= 6
51201e04c3fSmrg   const struct isl_extent3d image_align = get_image_alignment(info->surf);
5137ec681f3Smrg   s.SurfaceVerticalAlignment = isl_encode_valign(image_align.height);
5147ec681f3Smrg#if GFX_VER >= 7
5157ec681f3Smrg   s.SurfaceHorizontalAlignment = isl_encode_halign(image_align.width);
51601e04c3fSmrg#endif
51701e04c3fSmrg#endif
51801e04c3fSmrg
5197ec681f3Smrg   if (info->surf->dim_layout == ISL_DIM_LAYOUT_GFX9_1D) {
5207ec681f3Smrg      /* For gfx9 1-D textures, surface pitch is ignored */
52101e04c3fSmrg      s.SurfacePitch = 0;
52201e04c3fSmrg   } else {
52301e04c3fSmrg      s.SurfacePitch = info->surf->row_pitch_B - 1;
52401e04c3fSmrg   }
52501e04c3fSmrg
5267ec681f3Smrg#if GFX_VER >= 8
52701e04c3fSmrg   s.SurfaceQPitch = get_qpitch(info->surf) >> 2;
5287ec681f3Smrg#elif GFX_VER == 7
52901e04c3fSmrg   s.SurfaceArraySpacing = info->surf->array_pitch_span ==
53001e04c3fSmrg                           ISL_ARRAY_PITCH_SPAN_COMPACT;
53101e04c3fSmrg#endif
53201e04c3fSmrg
5337ec681f3Smrg#if GFX_VER >= 8
5347ec681f3Smrg   assert(GFX_VER < 12 || info->surf->tiling != ISL_TILING_W);
5357ec681f3Smrg   s.TileMode = isl_encode_tiling[info->surf->tiling];
53601e04c3fSmrg#else
53701e04c3fSmrg   s.TiledSurface = info->surf->tiling != ISL_TILING_LINEAR,
53801e04c3fSmrg   s.TileWalk = info->surf->tiling == ISL_TILING_Y0 ? TILEWALK_YMAJOR :
53901e04c3fSmrg                                                      TILEWALK_XMAJOR,
54001e04c3fSmrg#endif
54101e04c3fSmrg
5427ec681f3Smrg#if GFX_VER >= 8
54301e04c3fSmrg   s.RenderCacheReadWriteMode = WriteOnlyCache;
54401e04c3fSmrg#else
54501e04c3fSmrg   s.RenderCacheReadWriteMode = 0;
54601e04c3fSmrg#endif
54701e04c3fSmrg
5487ec681f3Smrg#if GFX_VER >= 11
5497ec681f3Smrg   /* We've seen dEQP failures when enabling this bit with UINT formats,
5507ec681f3Smrg    * which particularly affects blorp_copy() operations.  It shouldn't
5517ec681f3Smrg    * have any effect on UINT textures anyway, so disable it for them.
5527ec681f3Smrg    */
5537ec681f3Smrg   s.EnableUnormPathInColorPipe =
5547ec681f3Smrg      !isl_format_has_int_channel(info->view->format);
5557ec681f3Smrg#endif
5567ec681f3Smrg
55701e04c3fSmrg   s.CubeFaceEnablePositiveZ = 1;
55801e04c3fSmrg   s.CubeFaceEnableNegativeZ = 1;
55901e04c3fSmrg   s.CubeFaceEnablePositiveY = 1;
56001e04c3fSmrg   s.CubeFaceEnableNegativeY = 1;
56101e04c3fSmrg   s.CubeFaceEnablePositiveX = 1;
56201e04c3fSmrg   s.CubeFaceEnableNegativeX = 1;
56301e04c3fSmrg
5647ec681f3Smrg#if GFX_VER >= 6
56501e04c3fSmrg   s.NumberofMultisamples = ffs(info->surf->samples) - 1;
5667ec681f3Smrg#if GFX_VER >= 7
56701e04c3fSmrg   s.MultisampledSurfaceStorageFormat =
5687ec681f3Smrg      isl_encode_multisample_layout[info->surf->msaa_layout];
56901e04c3fSmrg#endif
57001e04c3fSmrg#endif
57101e04c3fSmrg
5727ec681f3Smrg#if (GFX_VERx10 >= 75)
57301e04c3fSmrg   if (info->view->usage & ISL_SURF_USAGE_RENDER_TARGET_BIT)
57401e04c3fSmrg      assert(isl_swizzle_supports_rendering(dev->info, info->view->swizzle));
57501e04c3fSmrg
57601e04c3fSmrg   s.ShaderChannelSelectRed = (enum GENX(ShaderChannelSelect)) info->view->swizzle.r;
57701e04c3fSmrg   s.ShaderChannelSelectGreen = (enum GENX(ShaderChannelSelect)) info->view->swizzle.g;
57801e04c3fSmrg   s.ShaderChannelSelectBlue = (enum GENX(ShaderChannelSelect)) info->view->swizzle.b;
57901e04c3fSmrg   s.ShaderChannelSelectAlpha = (enum GENX(ShaderChannelSelect)) info->view->swizzle.a;
58001e04c3fSmrg#else
58101e04c3fSmrg   assert(isl_swizzle_is_identity(info->view->swizzle));
58201e04c3fSmrg#endif
58301e04c3fSmrg
58401e04c3fSmrg   s.SurfaceBaseAddress = info->address;
58501e04c3fSmrg
5867ec681f3Smrg#if GFX_VER >= 6
58701e04c3fSmrg   s.MOCS = info->mocs;
58801e04c3fSmrg#endif
58901e04c3fSmrg
5907ec681f3Smrg#if GFX_VERx10 >= 45
59101e04c3fSmrg   if (info->x_offset_sa != 0 || info->y_offset_sa != 0) {
59201e04c3fSmrg      /* There are fairly strict rules about when the offsets can be used.
59301e04c3fSmrg       * These are mostly taken from the Sky Lake PRM documentation for
59401e04c3fSmrg       * RENDER_SURFACE_STATE.
59501e04c3fSmrg       */
59601e04c3fSmrg      assert(info->surf->tiling != ISL_TILING_LINEAR);
59701e04c3fSmrg      assert(info->surf->dim == ISL_SURF_DIM_2D);
59801e04c3fSmrg      assert(isl_is_pow2(isl_format_get_layout(info->view->format)->bpb));
59901e04c3fSmrg      assert(info->surf->levels == 1);
60001e04c3fSmrg      assert(info->surf->logical_level0_px.array_len == 1);
60101e04c3fSmrg      assert(info->aux_usage == ISL_AUX_USAGE_NONE);
60201e04c3fSmrg
6037ec681f3Smrg      if (GFX_VER >= 8) {
60401e04c3fSmrg         /* Broadwell added more rules. */
60501e04c3fSmrg         assert(info->surf->samples == 1);
60601e04c3fSmrg         if (isl_format_get_layout(info->view->format)->bpb == 8)
60701e04c3fSmrg            assert(info->x_offset_sa % 16 == 0);
60801e04c3fSmrg         if (isl_format_get_layout(info->view->format)->bpb == 16)
60901e04c3fSmrg            assert(info->x_offset_sa % 8 == 0);
61001e04c3fSmrg      }
61101e04c3fSmrg
6127ec681f3Smrg#if GFX_VER >= 7
61301e04c3fSmrg      s.SurfaceArray = false;
61401e04c3fSmrg#endif
61501e04c3fSmrg   }
61601e04c3fSmrg
61701e04c3fSmrg   const unsigned x_div = 4;
6187ec681f3Smrg   const unsigned y_div = GFX_VER >= 8 ? 4 : 2;
61901e04c3fSmrg   assert(info->x_offset_sa % x_div == 0);
62001e04c3fSmrg   assert(info->y_offset_sa % y_div == 0);
62101e04c3fSmrg   s.XOffset = info->x_offset_sa / x_div;
62201e04c3fSmrg   s.YOffset = info->y_offset_sa / y_div;
62301e04c3fSmrg#else
62401e04c3fSmrg   assert(info->x_offset_sa == 0);
62501e04c3fSmrg   assert(info->y_offset_sa == 0);
62601e04c3fSmrg#endif
62701e04c3fSmrg
6287ec681f3Smrg#if GFX_VER >= 7
6297ec681f3Smrg   if (info->aux_usage != ISL_AUX_USAGE_NONE) {
6307ec681f3Smrg      /* Check valid aux usages per-gen */
6317ec681f3Smrg      if (GFX_VER >= 12) {
6327ec681f3Smrg         assert(info->aux_usage == ISL_AUX_USAGE_MCS ||
6337ec681f3Smrg                info->aux_usage == ISL_AUX_USAGE_CCS_E ||
6347ec681f3Smrg                info->aux_usage == ISL_AUX_USAGE_GFX12_CCS_E ||
6357ec681f3Smrg                info->aux_usage == ISL_AUX_USAGE_MC ||
6367ec681f3Smrg                info->aux_usage == ISL_AUX_USAGE_HIZ_CCS_WT ||
6377ec681f3Smrg                info->aux_usage == ISL_AUX_USAGE_MCS_CCS ||
6387ec681f3Smrg                info->aux_usage == ISL_AUX_USAGE_STC_CCS);
6397ec681f3Smrg      } else if (GFX_VER >= 9) {
6407ec681f3Smrg         assert(info->aux_usage == ISL_AUX_USAGE_HIZ ||
6417ec681f3Smrg                info->aux_usage == ISL_AUX_USAGE_MCS ||
6427ec681f3Smrg                info->aux_usage == ISL_AUX_USAGE_CCS_D ||
6437ec681f3Smrg                info->aux_usage == ISL_AUX_USAGE_CCS_E);
6447ec681f3Smrg      } else if (GFX_VER >= 8) {
6457ec681f3Smrg         assert(info->aux_usage == ISL_AUX_USAGE_HIZ ||
6467ec681f3Smrg                info->aux_usage == ISL_AUX_USAGE_MCS ||
6477ec681f3Smrg                info->aux_usage == ISL_AUX_USAGE_CCS_D);
6487ec681f3Smrg      } else if (GFX_VER >= 7) {
6497ec681f3Smrg         assert(info->aux_usage == ISL_AUX_USAGE_MCS ||
6507ec681f3Smrg                info->aux_usage == ISL_AUX_USAGE_CCS_D);
6517ec681f3Smrg      }
6527ec681f3Smrg
65301e04c3fSmrg      /* The docs don't appear to say anything whatsoever about compression
65401e04c3fSmrg       * and the data port.  Testing seems to indicate that the data port
65501e04c3fSmrg       * completely ignores the AuxiliarySurfaceMode field.
6567ec681f3Smrg       *
6577ec681f3Smrg       * On gfx12 HDC supports compression.
65801e04c3fSmrg       */
6597ec681f3Smrg      if (GFX_VER < 12)
6607ec681f3Smrg         assert(!(info->view->usage & ISL_SURF_USAGE_STORAGE_BIT));
66101e04c3fSmrg
6627ec681f3Smrg      if (isl_surf_usage_is_depth(info->surf->usage))
6637ec681f3Smrg         assert(isl_aux_usage_has_hiz(info->aux_usage));
66401e04c3fSmrg
6657ec681f3Smrg      if (isl_surf_usage_is_stencil(info->surf->usage))
6667ec681f3Smrg         assert(info->aux_usage == ISL_AUX_USAGE_STC_CCS);
66701e04c3fSmrg
6687ec681f3Smrg      if (isl_aux_usage_has_hiz(info->aux_usage)) {
6697ec681f3Smrg         /* For Gfx8-10, there are some restrictions around sampling from HiZ.
6707ec681f3Smrg          * The Skylake PRM docs for RENDER_SURFACE_STATE::AuxiliarySurfaceMode
6717ec681f3Smrg          * say:
6727ec681f3Smrg          *
6737ec681f3Smrg          *    "If this field is set to AUX_HIZ, Number of Multisamples must
6747ec681f3Smrg          *    be MULTISAMPLECOUNT_1, and Surface Type cannot be SURFTYPE_3D."
6757ec681f3Smrg          *
6767ec681f3Smrg          * On Gfx12, the docs are a bit less obvious but the restriction is
6777ec681f3Smrg          * the same.  The limitation isn't called out explicitly but the docs
6787ec681f3Smrg          * for the CCS_E value of RENDER_SURFACE_STATE::AuxiliarySurfaceMode
6797ec681f3Smrg          * say:
6807ec681f3Smrg          *
6817ec681f3Smrg          *    "If Number of multisamples > 1, programming this value means
6827ec681f3Smrg          *    MSAA compression is enabled for that surface. Auxillary surface
6837ec681f3Smrg          *    is MSC with tile y."
6847ec681f3Smrg          *
6857ec681f3Smrg          * Since this interpretation ignores whether the surface is
6867ec681f3Smrg          * depth/stencil or not and since multisampled depth buffers use
6877ec681f3Smrg          * ISL_MSAA_LAYOUT_INTERLEAVED which is incompatible with MCS
6887ec681f3Smrg          * compression, this means that we can't even specify MSAA depth CCS
6897ec681f3Smrg          * in RENDER_SURFACE_STATE::AuxiliarySurfaceMode.
6907ec681f3Smrg          */
69101e04c3fSmrg         assert(info->surf->samples == 1);
69201e04c3fSmrg
69301e04c3fSmrg         /* The dimension must not be 3D */
69401e04c3fSmrg         assert(info->surf->dim != ISL_SURF_DIM_3D);
69501e04c3fSmrg
69601e04c3fSmrg         /* The format must be one of the following: */
69701e04c3fSmrg         switch (info->view->format) {
69801e04c3fSmrg         case ISL_FORMAT_R32_FLOAT:
69901e04c3fSmrg         case ISL_FORMAT_R24_UNORM_X8_TYPELESS:
70001e04c3fSmrg         case ISL_FORMAT_R16_UNORM:
70101e04c3fSmrg            break;
70201e04c3fSmrg         default:
70301e04c3fSmrg            assert(!"Incompatible HiZ Sampling format");
70401e04c3fSmrg            break;
70501e04c3fSmrg         }
70601e04c3fSmrg      }
70701e04c3fSmrg
7087ec681f3Smrg#if GFX_VERx10 >= 125
7097ec681f3Smrg      s.RenderCompressionFormat =
7107ec681f3Smrg         isl_get_render_compression_format(info->surf->format);
7117ec681f3Smrg#endif
7127ec681f3Smrg#if GFX_VER >= 12
7137ec681f3Smrg      s.MemoryCompressionEnable = info->aux_usage == ISL_AUX_USAGE_MC;
7147ec681f3Smrg#endif
7157ec681f3Smrg#if GFX_VER >= 8
7167ec681f3Smrg      s.AuxiliarySurfaceMode = isl_encode_aux_mode[info->aux_usage];
71701e04c3fSmrg#else
71801e04c3fSmrg      s.MCSEnable = true;
7197ec681f3Smrg#endif
7207ec681f3Smrg   }
7217ec681f3Smrg
7227ec681f3Smrg   /* The auxiliary buffer info is filled when it's useable by the HW.
7237ec681f3Smrg    *
7247ec681f3Smrg    * Starting with Gfx12, the only form of compression that can be used
7257ec681f3Smrg    * with RENDER_SURFACE_STATE which requires an aux surface is MCS.
7267ec681f3Smrg    * HiZ still requires a surface but the HiZ surface can only be
7277ec681f3Smrg    * accessed through 3DSTATE_HIER_DEPTH_BUFFER.
7287ec681f3Smrg    *
7297ec681f3Smrg    * On all earlier hardware, an aux surface is required for all forms
7307ec681f3Smrg    * of compression.
7317ec681f3Smrg    */
7327ec681f3Smrg   if ((GFX_VER < 12 && info->aux_usage != ISL_AUX_USAGE_NONE) ||
7337ec681f3Smrg       (GFX_VER >= 12 && isl_aux_usage_has_mcs(info->aux_usage))) {
7347ec681f3Smrg
7357ec681f3Smrg      assert(info->aux_surf != NULL);
7367ec681f3Smrg
7377ec681f3Smrg      struct isl_tile_info tile_info;
7387ec681f3Smrg      isl_surf_get_tile_info(info->aux_surf, &tile_info);
7397ec681f3Smrg      uint32_t pitch_in_tiles =
7407ec681f3Smrg         info->aux_surf->row_pitch_B / tile_info.phys_extent_B.width;
7417ec681f3Smrg
7427ec681f3Smrg      s.AuxiliarySurfaceBaseAddress = info->aux_address;
7437ec681f3Smrg      s.AuxiliarySurfacePitch = pitch_in_tiles - 1;
7447ec681f3Smrg
7457ec681f3Smrg#if GFX_VER >= 8
7467ec681f3Smrg      /* Auxiliary surfaces in ISL have compressed formats but the hardware
7477ec681f3Smrg       * doesn't expect our definition of the compression, it expects qpitch
7487ec681f3Smrg       * in units of samples on the main surface.
7497ec681f3Smrg       */
7507ec681f3Smrg      s.AuxiliarySurfaceQPitch =
7517ec681f3Smrg         isl_surf_get_array_pitch_sa_rows(info->aux_surf) >> 2;
75201e04c3fSmrg#endif
75301e04c3fSmrg   }
75401e04c3fSmrg#endif
75501e04c3fSmrg
7567ec681f3Smrg#if GFX_VER >= 8 && GFX_VER < 11
75701e04c3fSmrg   /* From the CHV PRM, Volume 2d, page 321 (RENDER_SURFACE_STATE dword 0
75801e04c3fSmrg    * bit 9 "Sampler L2 Bypass Mode Disable" Programming Notes):
75901e04c3fSmrg    *
76001e04c3fSmrg    *    This bit must be set for the following surface types: BC2_UNORM
76101e04c3fSmrg    *    BC3_UNORM BC5_UNORM BC5_SNORM BC7_UNORM
76201e04c3fSmrg    */
7637ec681f3Smrg   if (GFX_VER >= 9 || dev->info->is_cherryview) {
76401e04c3fSmrg      switch (info->view->format) {
76501e04c3fSmrg      case ISL_FORMAT_BC2_UNORM:
76601e04c3fSmrg      case ISL_FORMAT_BC3_UNORM:
76701e04c3fSmrg      case ISL_FORMAT_BC5_UNORM:
76801e04c3fSmrg      case ISL_FORMAT_BC5_SNORM:
76901e04c3fSmrg      case ISL_FORMAT_BC7_UNORM:
77001e04c3fSmrg         s.SamplerL2BypassModeDisable = true;
77101e04c3fSmrg         break;
77201e04c3fSmrg      default:
77301e04c3fSmrg         /* From the SKL PRM, Programming Note under Sampler Output Channel
77401e04c3fSmrg          * Mapping:
77501e04c3fSmrg          *
77601e04c3fSmrg          *    If a surface has an associated HiZ Auxilliary surface, the
77701e04c3fSmrg          *    Sampler L2 Bypass Mode Disable field in the RENDER_SURFACE_STATE
77801e04c3fSmrg          *    must be set.
77901e04c3fSmrg          */
7807ec681f3Smrg         if (GFX_VER >= 9 && info->aux_usage == ISL_AUX_USAGE_HIZ)
78101e04c3fSmrg            s.SamplerL2BypassModeDisable = true;
78201e04c3fSmrg         break;
78301e04c3fSmrg      }
78401e04c3fSmrg   }
78501e04c3fSmrg#endif
78601e04c3fSmrg
7877ec681f3Smrg   if (isl_aux_usage_has_fast_clears(info->aux_usage)) {
78801e04c3fSmrg      if (info->use_clear_address) {
7897ec681f3Smrg#if GFX_VER >= 10
79001e04c3fSmrg         s.ClearValueAddressEnable = true;
79101e04c3fSmrg         s.ClearValueAddress = info->clear_address;
79201e04c3fSmrg#else
7937ec681f3Smrg         unreachable("Gfx9 and earlier do not support indirect clear colors");
79401e04c3fSmrg#endif
79501e04c3fSmrg      }
7969f464c52Smaya
7977ec681f3Smrg#if GFX_VER == 11
7989f464c52Smaya      /*
7999f464c52Smaya       * From BXML > GT > Shared Functions > vol5c Shared Functions >
8009f464c52Smaya       * [Structure] RENDER_SURFACE_STATE [BDW+] > ClearColorConversionEnable:
8019f464c52Smaya       *
8027ec681f3Smrg       *   Project: Gfx11
8039f464c52Smaya       *
8049f464c52Smaya       *   "Enables Pixel backend hw to convert clear values into native format
8059f464c52Smaya       *    and write back to clear address, so that display and sampler can use
8069f464c52Smaya       *    the converted value for resolving fast cleared RTs."
8079f464c52Smaya       *
8089f464c52Smaya       * Summary:
8099f464c52Smaya       *   Clear color conversion must be enabled if the clear color is stored
8109f464c52Smaya       *   indirectly and fast color clears are enabled.
8119f464c52Smaya       */
8129f464c52Smaya      if (info->use_clear_address) {
8139f464c52Smaya         s.ClearColorConversionEnable = true;
8149f464c52Smaya      }
8159f464c52Smaya#endif
8169f464c52Smaya
8177ec681f3Smrg#if GFX_VER >= 12
8187ec681f3Smrg      assert(info->use_clear_address);
8197ec681f3Smrg#elif GFX_VER >= 9
82001e04c3fSmrg      if (!info->use_clear_address) {
82101e04c3fSmrg         s.RedClearColor = info->clear_color.u32[0];
82201e04c3fSmrg         s.GreenClearColor = info->clear_color.u32[1];
82301e04c3fSmrg         s.BlueClearColor = info->clear_color.u32[2];
82401e04c3fSmrg         s.AlphaClearColor = info->clear_color.u32[3];
82501e04c3fSmrg      }
8267ec681f3Smrg#elif GFX_VER >= 7
82701e04c3fSmrg      /* Prior to Sky Lake, we only have one bit for the clear color which
82801e04c3fSmrg       * gives us 0 or 1 in whatever the surface's format happens to be.
82901e04c3fSmrg       */
83001e04c3fSmrg      if (isl_format_has_int_channel(info->view->format)) {
83101e04c3fSmrg         for (unsigned i = 0; i < 4; i++) {
83201e04c3fSmrg            assert(info->clear_color.u32[i] == 0 ||
83301e04c3fSmrg                   info->clear_color.u32[i] == 1);
83401e04c3fSmrg         }
83501e04c3fSmrg         s.RedClearColor = info->clear_color.u32[0] != 0;
83601e04c3fSmrg         s.GreenClearColor = info->clear_color.u32[1] != 0;
83701e04c3fSmrg         s.BlueClearColor = info->clear_color.u32[2] != 0;
83801e04c3fSmrg         s.AlphaClearColor = info->clear_color.u32[3] != 0;
83901e04c3fSmrg      } else {
84001e04c3fSmrg         for (unsigned i = 0; i < 4; i++) {
84101e04c3fSmrg            assert(info->clear_color.f32[i] == 0.0f ||
84201e04c3fSmrg                   info->clear_color.f32[i] == 1.0f);
84301e04c3fSmrg         }
84401e04c3fSmrg         s.RedClearColor = info->clear_color.f32[0] != 0.0f;
84501e04c3fSmrg         s.GreenClearColor = info->clear_color.f32[1] != 0.0f;
84601e04c3fSmrg         s.BlueClearColor = info->clear_color.f32[2] != 0.0f;
84701e04c3fSmrg         s.AlphaClearColor = info->clear_color.f32[3] != 0.0f;
84801e04c3fSmrg      }
84901e04c3fSmrg#endif
85001e04c3fSmrg   }
85101e04c3fSmrg
85201e04c3fSmrg   GENX(RENDER_SURFACE_STATE_pack)(NULL, state, &s);
85301e04c3fSmrg}
85401e04c3fSmrg
85501e04c3fSmrgvoid
8567ec681f3Smrgisl_genX(buffer_fill_state_s)(const struct isl_device *dev, void *state,
85701e04c3fSmrg                              const struct isl_buffer_fill_state_info *restrict info)
85801e04c3fSmrg{
85901e04c3fSmrg   uint64_t buffer_size = info->size_B;
86001e04c3fSmrg
86101e04c3fSmrg   /* Uniform and Storage buffers need to have surface size not less that the
86201e04c3fSmrg    * aligned 32-bit size of the buffer. To calculate the array lenght on
86301e04c3fSmrg    * unsized arrays in StorageBuffer the last 2 bits store the padding size
86401e04c3fSmrg    * added to the surface, so we can calculate latter the original buffer
86501e04c3fSmrg    * size to know the number of elements.
86601e04c3fSmrg    *
86701e04c3fSmrg    *  surface_size = isl_align(buffer_size, 4) +
86801e04c3fSmrg    *                 (isl_align(buffer_size) - buffer_size)
86901e04c3fSmrg    *
87001e04c3fSmrg    *  buffer_size = (surface_size & ~3) - (surface_size & 3)
87101e04c3fSmrg    */
8727ec681f3Smrg   if ((info->format == ISL_FORMAT_RAW  ||
8737ec681f3Smrg        info->stride_B < isl_format_get_layout(info->format)->bpb / 8) &&
8747ec681f3Smrg       !info->is_scratch) {
87501e04c3fSmrg      assert(info->stride_B == 1);
87601e04c3fSmrg      uint64_t aligned_size = isl_align(buffer_size, 4);
87701e04c3fSmrg      buffer_size = aligned_size + (aligned_size - buffer_size);
87801e04c3fSmrg   }
87901e04c3fSmrg
88001e04c3fSmrg   uint32_t num_elements = buffer_size / info->stride_B;
88101e04c3fSmrg
8827ec681f3Smrg   assert(num_elements > 0);
8837ec681f3Smrg   if (info->format == ISL_FORMAT_RAW) {
8847ec681f3Smrg      assert(num_elements <= dev->max_buffer_size);
8857ec681f3Smrg   } else {
88601e04c3fSmrg      /* From the IVB PRM, SURFACE_STATE::Height,
88701e04c3fSmrg       *
88801e04c3fSmrg       *    For typed buffer and structured buffer surfaces, the number
8897ec681f3Smrg       *    of entries in the buffer ranges from 1 to 2^27.
89001e04c3fSmrg       */
89101e04c3fSmrg      assert(num_elements <= (1ull << 27));
89201e04c3fSmrg   }
89301e04c3fSmrg
89401e04c3fSmrg   struct GENX(RENDER_SURFACE_STATE) s = { 0, };
89501e04c3fSmrg
89601e04c3fSmrg   s.SurfaceFormat = info->format;
89701e04c3fSmrg
8987ec681f3Smrg   s.SurfaceType = SURFTYPE_BUFFER;
8997ec681f3Smrg#if GFX_VERx10 >= 125
9007ec681f3Smrg   if (info->is_scratch) {
9017ec681f3Smrg      /* From the BSpec:
9027ec681f3Smrg       *
9037ec681f3Smrg       *    "For surfaces of type SURFTYPE_SCRATCH, valid range of pitch is:
9047ec681f3Smrg       *    [63,262143] -> [64B, 256KB].  Also, for SURFTYPE_SCRATCH, the
9057ec681f3Smrg       *    pitch must be a multiple of 64bytes."
9067ec681f3Smrg       */
9077ec681f3Smrg      assert(info->format == ISL_FORMAT_RAW);
9087ec681f3Smrg      assert(info->stride_B % 64 == 0);
9097ec681f3Smrg      assert(info->stride_B <= 256 * 1024);
9107ec681f3Smrg      s.SurfaceType = SURFTYPE_SCRATCH;
9117ec681f3Smrg   }
9127ec681f3Smrg#else
9137ec681f3Smrg   assert(!info->is_scratch);
9147ec681f3Smrg#endif
9157ec681f3Smrg
9167ec681f3Smrg   s.SurfacePitch = info->stride_B - 1;
9177ec681f3Smrg
9187ec681f3Smrg#if GFX_VER >= 6
9197ec681f3Smrg   s.SurfaceVerticalAlignment = isl_encode_valign(4);
9207ec681f3Smrg#if GFX_VERx10 >= 125
9217ec681f3Smrg   s.SurfaceHorizontalAlignment = isl_encode_halign(128);
9227ec681f3Smrg#elif GFX_VER >= 7
9237ec681f3Smrg   s.SurfaceHorizontalAlignment = isl_encode_halign(4);
92401e04c3fSmrg   s.SurfaceArray = false;
92501e04c3fSmrg#endif
92601e04c3fSmrg#endif
92701e04c3fSmrg
9287ec681f3Smrg#if GFX_VER >= 7
92901e04c3fSmrg   s.Height = ((num_elements - 1) >> 7) & 0x3fff;
93001e04c3fSmrg   s.Width = (num_elements - 1) & 0x7f;
93101e04c3fSmrg   s.Depth = ((num_elements - 1) >> 21) & 0x3ff;
93201e04c3fSmrg#else
93301e04c3fSmrg   s.Height = ((num_elements - 1) >> 7) & 0x1fff;
93401e04c3fSmrg   s.Width = (num_elements - 1) & 0x7f;
93501e04c3fSmrg   s.Depth = ((num_elements - 1) >> 20) & 0x7f;
93601e04c3fSmrg#endif
93701e04c3fSmrg
9387ec681f3Smrg   if (GFX_VER == 12 && dev->info->revision == 0) {
9397ec681f3Smrg      /* TGL-LP A0 has a HW bug (fixed in later HW) which causes buffer
9407ec681f3Smrg       * textures with very close base addresses (delta < 64B) to corrupt each
9417ec681f3Smrg       * other.  We can sort-of work around this by making small buffer
9427ec681f3Smrg       * textures 1D textures instead.  This doesn't fix the problem for large
9437ec681f3Smrg       * buffer textures but the liklihood of large, overlapping, and very
9447ec681f3Smrg       * close buffer textures is fairly low and the point is to hack around
9457ec681f3Smrg       * the bug so we can run apps and tests.
9467ec681f3Smrg       */
9477ec681f3Smrg       if (info->format != ISL_FORMAT_RAW &&
9487ec681f3Smrg           info->stride_B == isl_format_get_layout(info->format)->bpb / 8 &&
9497ec681f3Smrg           num_elements <= (1 << 14)) {
9507ec681f3Smrg         s.SurfaceType = SURFTYPE_1D;
9517ec681f3Smrg         s.Width = num_elements - 1;
9527ec681f3Smrg         s.Height = 0;
9537ec681f3Smrg         s.Depth = 0;
9547ec681f3Smrg      }
9557ec681f3Smrg   }
95601e04c3fSmrg
9577ec681f3Smrg#if GFX_VER >= 6
95801e04c3fSmrg   s.NumberofMultisamples = MULTISAMPLECOUNT_1;
95901e04c3fSmrg#endif
96001e04c3fSmrg
9617ec681f3Smrg#if (GFX_VER >= 8)
96201e04c3fSmrg   s.TileMode = LINEAR;
96301e04c3fSmrg#else
96401e04c3fSmrg   s.TiledSurface = false;
96501e04c3fSmrg#endif
96601e04c3fSmrg
9677ec681f3Smrg#if (GFX_VER >= 8)
96801e04c3fSmrg   s.RenderCacheReadWriteMode = WriteOnlyCache;
96901e04c3fSmrg#else
97001e04c3fSmrg   s.RenderCacheReadWriteMode = 0;
97101e04c3fSmrg#endif
97201e04c3fSmrg
97301e04c3fSmrg   s.SurfaceBaseAddress = info->address;
9747ec681f3Smrg#if GFX_VER >= 6
97501e04c3fSmrg   s.MOCS = info->mocs;
97601e04c3fSmrg#endif
97701e04c3fSmrg
9787ec681f3Smrg#if (GFX_VERx10 >= 75)
9799f464c52Smaya   s.ShaderChannelSelectRed = (enum GENX(ShaderChannelSelect)) info->swizzle.r;
9809f464c52Smaya   s.ShaderChannelSelectGreen = (enum GENX(ShaderChannelSelect)) info->swizzle.g;
9819f464c52Smaya   s.ShaderChannelSelectBlue = (enum GENX(ShaderChannelSelect)) info->swizzle.b;
9829f464c52Smaya   s.ShaderChannelSelectAlpha = (enum GENX(ShaderChannelSelect)) info->swizzle.a;
98301e04c3fSmrg#endif
98401e04c3fSmrg
98501e04c3fSmrg   GENX(RENDER_SURFACE_STATE_pack)(NULL, state, &s);
98601e04c3fSmrg}
98701e04c3fSmrg
98801e04c3fSmrgvoid
9897ec681f3Smrgisl_genX(null_fill_state)(void *state,
9907ec681f3Smrg                          const struct isl_null_fill_state_info *restrict info)
99101e04c3fSmrg{
99201e04c3fSmrg   struct GENX(RENDER_SURFACE_STATE) s = {
99301e04c3fSmrg      .SurfaceType = SURFTYPE_NULL,
9947ec681f3Smrg      /* We previously had this format set to B8G8R8A8_UNORM but ran into
9957ec681f3Smrg       * hangs on IVB. R32_UINT seems to work for everybody.
9967ec681f3Smrg       *
9977ec681f3Smrg       * https://gitlab.freedesktop.org/mesa/mesa/-/issues/1872
9987ec681f3Smrg       */
9997ec681f3Smrg      .SurfaceFormat = ISL_FORMAT_R32_UINT,
10007ec681f3Smrg#if GFX_VER >= 7
10017ec681f3Smrg      .SurfaceArray = info->size.depth > 1,
100201e04c3fSmrg#endif
10037ec681f3Smrg#if GFX_VERx10 >= 125
10047ec681f3Smrg      .TileMode = TILE4,
10057ec681f3Smrg#elif GFX_VER >= 8
100601e04c3fSmrg      .TileMode = YMAJOR,
100701e04c3fSmrg#else
100801e04c3fSmrg      .TiledSurface = true,
100901e04c3fSmrg      .TileWalk = TILEWALK_YMAJOR,
101001e04c3fSmrg#endif
10117ec681f3Smrg#if GFX_VER == 7
10127ec681f3Smrg      /* According to PRMs: "Volume 4 Part 1: Subsystem and Cores – Shared
10137ec681f3Smrg       * Functions"
10147ec681f3Smrg       *
10157ec681f3Smrg       * RENDER_SURFACE_STATE::Surface Vertical Alignment
10167ec681f3Smrg       *
10177ec681f3Smrg       *    "This field must be set to VALIGN_4 for all tiled Y Render Target
10187ec681f3Smrg       *     surfaces."
10197ec681f3Smrg       *
10207ec681f3Smrg       * Affect IVB, HSW.
10217ec681f3Smrg       */
10227ec681f3Smrg      .SurfaceVerticalAlignment = VALIGN_4,
10237ec681f3Smrg#endif
10247ec681f3Smrg      .MIPCountLOD = info->levels,
10257ec681f3Smrg      .Width = info->size.width - 1,
10267ec681f3Smrg      .Height = info->size.height - 1,
10277ec681f3Smrg      .Depth = info->size.depth - 1,
10287ec681f3Smrg      .RenderTargetViewExtent = info->size.depth - 1,
10297ec681f3Smrg#if GFX_VER <= 5
10307ec681f3Smrg      .MinimumArrayElement = info->minimum_array_element,
103101e04c3fSmrg      .ColorBufferComponentWriteDisables = 0xf,
103201e04c3fSmrg#endif
103301e04c3fSmrg   };
103401e04c3fSmrg   GENX(RENDER_SURFACE_STATE_pack)(NULL, state, &s);
103501e04c3fSmrg}
1036