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