1b8e80941Smrg/*
2b8e80941Smrg * Copyright 2015 Intel Corporation
3b8e80941Smrg *
4b8e80941Smrg *  Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg *  copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg *  to deal in the Software without restriction, including without limitation
7b8e80941Smrg *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg *  and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg *  Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg *  The above copyright notice and this permission notice (including the next
12b8e80941Smrg *  paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg *  Software.
14b8e80941Smrg *
15b8e80941Smrg *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20b8e80941Smrg *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21b8e80941Smrg *  IN THE SOFTWARE.
22b8e80941Smrg */
23b8e80941Smrg
24b8e80941Smrg#include "isl_gen8.h"
25b8e80941Smrg#include "isl_gen9.h"
26b8e80941Smrg#include "isl_priv.h"
27b8e80941Smrg
28b8e80941Smrg/**
29b8e80941Smrg * Calculate the surface's subimage alignment, in units of surface samples,
30b8e80941Smrg * for the standard tiling formats Yf and Ys.
31b8e80941Smrg */
32b8e80941Smrgstatic void
33b8e80941Smrggen9_calc_std_image_alignment_sa(const struct isl_device *dev,
34b8e80941Smrg                                 const struct isl_surf_init_info *restrict info,
35b8e80941Smrg                                 enum isl_tiling tiling,
36b8e80941Smrg                                 enum isl_msaa_layout msaa_layout,
37b8e80941Smrg                                 struct isl_extent3d *align_sa)
38b8e80941Smrg{
39b8e80941Smrg   const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
40b8e80941Smrg
41b8e80941Smrg   assert(isl_tiling_is_std_y(tiling));
42b8e80941Smrg
43b8e80941Smrg   const uint32_t bpb = fmtl->bpb;
44b8e80941Smrg   const uint32_t is_Ys = tiling == ISL_TILING_Ys;
45b8e80941Smrg
46b8e80941Smrg   switch (info->dim) {
47b8e80941Smrg   case ISL_SURF_DIM_1D:
48b8e80941Smrg      /* See the Skylake BSpec > Memory Views > Common Surface Formats > Surface
49b8e80941Smrg       * Layout and Tiling > 1D Surfaces > 1D Alignment Requirements.
50b8e80941Smrg       */
51b8e80941Smrg      *align_sa = (struct isl_extent3d) {
52b8e80941Smrg         .w = 1 << (12 - (ffs(bpb) - 4) + (4 * is_Ys)),
53b8e80941Smrg         .h = 1,
54b8e80941Smrg         .d = 1,
55b8e80941Smrg      };
56b8e80941Smrg      return;
57b8e80941Smrg   case ISL_SURF_DIM_2D:
58b8e80941Smrg      /* See the Skylake BSpec > Memory Views > Common Surface Formats >
59b8e80941Smrg       * Surface Layout and Tiling > 2D Surfaces > 2D/CUBE Alignment
60b8e80941Smrg       * Requirements.
61b8e80941Smrg       */
62b8e80941Smrg      *align_sa = (struct isl_extent3d) {
63b8e80941Smrg         .w = 1 << (6 - ((ffs(bpb) - 4) / 2) + (4 * is_Ys)),
64b8e80941Smrg         .h = 1 << (6 - ((ffs(bpb) - 3) / 2) + (4 * is_Ys)),
65b8e80941Smrg         .d = 1,
66b8e80941Smrg      };
67b8e80941Smrg
68b8e80941Smrg      if (is_Ys) {
69b8e80941Smrg         /* FINISHME(chadv): I don't trust this code. Untested. */
70b8e80941Smrg         isl_finishme("%s:%s: [SKL+] multisample TileYs", __FILE__, __func__);
71b8e80941Smrg
72b8e80941Smrg         switch (msaa_layout) {
73b8e80941Smrg         case ISL_MSAA_LAYOUT_NONE:
74b8e80941Smrg         case ISL_MSAA_LAYOUT_INTERLEAVED:
75b8e80941Smrg            break;
76b8e80941Smrg         case ISL_MSAA_LAYOUT_ARRAY:
77b8e80941Smrg            align_sa->w >>= (ffs(info->samples) - 0) / 2;
78b8e80941Smrg            align_sa->h >>= (ffs(info->samples) - 1) / 2;
79b8e80941Smrg            break;
80b8e80941Smrg         }
81b8e80941Smrg      }
82b8e80941Smrg      return;
83b8e80941Smrg
84b8e80941Smrg   case ISL_SURF_DIM_3D:
85b8e80941Smrg      /* See the Skylake BSpec > Memory Views > Common Surface Formats > Surface
86b8e80941Smrg       * Layout and Tiling > 1D Surfaces > 1D Alignment Requirements.
87b8e80941Smrg       */
88b8e80941Smrg      *align_sa = (struct isl_extent3d) {
89b8e80941Smrg         .w = 1 << (4 - ((ffs(bpb) - 2) / 3) + (4 * is_Ys)),
90b8e80941Smrg         .h = 1 << (4 - ((ffs(bpb) - 4) / 3) + (2 * is_Ys)),
91b8e80941Smrg         .d = 1 << (4 - ((ffs(bpb) - 3) / 3) + (2 * is_Ys)),
92b8e80941Smrg      };
93b8e80941Smrg      return;
94b8e80941Smrg   }
95b8e80941Smrg
96b8e80941Smrg   unreachable("bad isl_surface_type");
97b8e80941Smrg}
98b8e80941Smrg
99b8e80941Smrgvoid
100b8e80941Smrgisl_gen9_choose_image_alignment_el(const struct isl_device *dev,
101b8e80941Smrg                                   const struct isl_surf_init_info *restrict info,
102b8e80941Smrg                                   enum isl_tiling tiling,
103b8e80941Smrg                                   enum isl_dim_layout dim_layout,
104b8e80941Smrg                                   enum isl_msaa_layout msaa_layout,
105b8e80941Smrg                                   struct isl_extent3d *image_align_el)
106b8e80941Smrg{
107b8e80941Smrg   /* Handled by isl_choose_image_alignment_el */
108b8e80941Smrg   assert(info->format != ISL_FORMAT_HIZ);
109b8e80941Smrg
110b8e80941Smrg   const struct isl_format_layout *fmtl = isl_format_get_layout(info->format);
111b8e80941Smrg   if (fmtl->txc == ISL_TXC_CCS) {
112b8e80941Smrg      /* Sky Lake PRM Vol. 7, "MCS Buffer for Render Target(s)" (p. 632):
113b8e80941Smrg       *
114b8e80941Smrg       *    "Mip-mapped and arrayed surfaces are supported with MCS buffer
115b8e80941Smrg       *    layout with these alignments in the RT space: Horizontal
116b8e80941Smrg       *    Alignment = 128 and Vertical Alignment = 64."
117b8e80941Smrg       */
118b8e80941Smrg      *image_align_el = isl_extent3d(128 / fmtl->bw, 64 / fmtl->bh, 1);
119b8e80941Smrg      return;
120b8e80941Smrg   }
121b8e80941Smrg
122b8e80941Smrg   /* This BSpec text provides some insight into the hardware's alignment
123b8e80941Smrg    * requirements [Skylake BSpec > Memory Views > Common Surface Formats >
124b8e80941Smrg    * Surface Layout and Tiling > 2D Surfaces]:
125b8e80941Smrg    *
126b8e80941Smrg    *    An LOD must be aligned to a cache-line except for some special cases
127b8e80941Smrg    *    related to Planar YUV surfaces.  In general, the cache-alignment
128b8e80941Smrg    *    restriction implies there is a minimum height for an LOD of 4 texels.
129b8e80941Smrg    *    So, LODs which are smaller than 4 high are padded.
130b8e80941Smrg    *
131b8e80941Smrg    * From the Skylake BSpec, RENDER_SURFACE_STATE Surface Vertical Alignment:
132b8e80941Smrg    *
133b8e80941Smrg    *    - For Sampling Engine and Render Target Surfaces: This field
134b8e80941Smrg    *      specifies the vertical alignment requirement in elements for the
135b8e80941Smrg    *      surface. [...] An element is defined as a pixel in uncompresed
136b8e80941Smrg    *      surface formats, and as a compression block in compressed surface
137b8e80941Smrg    *      formats. For MSFMT_DEPTH_STENCIL type multisampled surfaces, an
138b8e80941Smrg    *      element is a sample.
139b8e80941Smrg    *
140b8e80941Smrg    *    - This field is used for 2D, CUBE, and 3D surface alignment when Tiled
141b8e80941Smrg    *      Resource Mode is TRMODE_NONE (Tiled Resource Mode is disabled).
142b8e80941Smrg    *      This field is ignored for 1D surfaces and also when Tiled Resource
143b8e80941Smrg    *      Mode is not TRMODE_NONE (e.g. Tiled Resource Mode is enabled).
144b8e80941Smrg    *
145b8e80941Smrg    *      See the appropriate Alignment  table in the "Surface Layout and
146b8e80941Smrg    *      Tiling" section under Common Surface Formats for the table of
147b8e80941Smrg    *      alignment values for Tiled Resrouces.
148b8e80941Smrg    *
149b8e80941Smrg    *    - For uncompressed surfaces, the units of "j" are rows of pixels on
150b8e80941Smrg    *      the physical surface. For compressed texture formats, the units of
151b8e80941Smrg    *      "j" are in compression blocks, thus each increment in "j" is equal
152b8e80941Smrg    *      to h pixels, where h is the height of the compression block in
153b8e80941Smrg    *      pixels.
154b8e80941Smrg    *
155b8e80941Smrg    *    - Valid Values: VALIGN_4, VALIGN_8, VALIGN_16
156b8e80941Smrg    *
157b8e80941Smrg    * From the Skylake BSpec, RENDER_SURFACE_STATE Surface Horizontal
158b8e80941Smrg    * Alignment:
159b8e80941Smrg    *
160b8e80941Smrg    *    -  For uncompressed surfaces, the units of "i" are pixels on the
161b8e80941Smrg    *       physical surface. For compressed texture formats, the units of "i"
162b8e80941Smrg    *       are in compression blocks, thus each increment in "i" is equal to
163b8e80941Smrg    *       w pixels, where w is the width of the compression block in pixels.
164b8e80941Smrg    *
165b8e80941Smrg    *    - Valid Values: HALIGN_4, HALIGN_8, HALIGN_16
166b8e80941Smrg    */
167b8e80941Smrg
168b8e80941Smrg   if (isl_tiling_is_std_y(tiling)) {
169b8e80941Smrg      struct isl_extent3d image_align_sa;
170b8e80941Smrg      gen9_calc_std_image_alignment_sa(dev, info, tiling, msaa_layout,
171b8e80941Smrg                                     &image_align_sa);
172b8e80941Smrg
173b8e80941Smrg      *image_align_el = isl_extent3d_sa_to_el(info->format, image_align_sa);
174b8e80941Smrg      return;
175b8e80941Smrg   }
176b8e80941Smrg
177b8e80941Smrg   if (dim_layout == ISL_DIM_LAYOUT_GEN9_1D) {
178b8e80941Smrg      /* See the Skylake BSpec > Memory Views > Common Surface Formats > Surface
179b8e80941Smrg       * Layout and Tiling > 1D Surfaces > 1D Alignment Requirements.
180b8e80941Smrg       */
181b8e80941Smrg      *image_align_el = isl_extent3d(64, 1, 1);
182b8e80941Smrg      return;
183b8e80941Smrg   }
184b8e80941Smrg
185b8e80941Smrg   if (isl_format_is_compressed(info->format)) {
186b8e80941Smrg      /* On Gen9, the meaning of RENDER_SURFACE_STATE's
187b8e80941Smrg       * SurfaceHorizontalAlignment and SurfaceVerticalAlignment changed for
188b8e80941Smrg       * compressed formats. They now indicate a multiple of the compression
189b8e80941Smrg       * block.  For example, if the compression mode is ETC2 then HALIGN_4
190b8e80941Smrg       * indicates a horizontal alignment of 16 pixels.
191b8e80941Smrg       *
192b8e80941Smrg       * To avoid wasting memory, choose the smallest alignment possible:
193b8e80941Smrg       * HALIGN_4 and VALIGN_4.
194b8e80941Smrg       */
195b8e80941Smrg      *image_align_el = isl_extent3d(4, 4, 1);
196b8e80941Smrg      return;
197b8e80941Smrg   }
198b8e80941Smrg
199b8e80941Smrg   isl_gen8_choose_image_alignment_el(dev, info, tiling, dim_layout,
200b8e80941Smrg                                      msaa_layout, image_align_el);
201b8e80941Smrg}
202