1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2019 Google, Inc.
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#ifndef FREEDRENO_LAYOUT_H_
25bf215546Sopenharmony_ci#define FREEDRENO_LAYOUT_H_
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include <stdbool.h>
28bf215546Sopenharmony_ci#include <stdint.h>
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "util/format/u_format.h"
31bf215546Sopenharmony_ci#include "util/u_debug.h"
32bf215546Sopenharmony_ci#include "util/u_math.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci/* Shared freedreno mipmap layout helper
35bf215546Sopenharmony_ci *
36bf215546Sopenharmony_ci * It does *not* attempt to track surface transitions, in particular
37bf215546Sopenharmony_ci * about UBWC state.  Possibly it should, but
38bf215546Sopenharmony_ci *  (a) I'm not sure if in all cases we can transparently do in-
39bf215546Sopenharmony_ci *      place transitions (ie. a5xx textures with interleaved
40bf215546Sopenharmony_ci *      meta and pixel data
41bf215546Sopenharmony_ci *  (b) Even if we can, we probably can't assume that we have
42bf215546Sopenharmony_ci *      figured out yet how to do in-place transition for every
43bf215546Sopenharmony_ci *      generation.
44bf215546Sopenharmony_ci */
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci/* Texture Layout on a3xx:
47bf215546Sopenharmony_ci * -----------------------
48bf215546Sopenharmony_ci *
49bf215546Sopenharmony_ci * Each mipmap-level contains all of it's layers (ie. all cubmap
50bf215546Sopenharmony_ci * faces, all 1d/2d array elements, etc).  The texture sampler is
51bf215546Sopenharmony_ci * programmed with the start address of each mipmap level, and hw
52bf215546Sopenharmony_ci * derives the layer offset within the level.
53bf215546Sopenharmony_ci *
54bf215546Sopenharmony_ci *
55bf215546Sopenharmony_ci * Texture Layout on a4xx+:
56bf215546Sopenharmony_ci * -----------------------
57bf215546Sopenharmony_ci *
58bf215546Sopenharmony_ci * For cubemap and 2d array, each layer contains all of it's mipmap
59bf215546Sopenharmony_ci * levels (layer_first layout).
60bf215546Sopenharmony_ci *
61bf215546Sopenharmony_ci * 3d textures are laid out as on a3xx.
62bf215546Sopenharmony_ci *
63bf215546Sopenharmony_ci * In either case, the slice represents the per-miplevel information,
64bf215546Sopenharmony_ci * but in layer_first layout it only includes the first layer, and
65bf215546Sopenharmony_ci * an additional offset of (rsc->layer_size * layer) must be added.
66bf215546Sopenharmony_ci *
67bf215546Sopenharmony_ci *
68bf215546Sopenharmony_ci * UBWC Color Compressions (a5xx+):
69bf215546Sopenharmony_ci * -------------------------------
70bf215546Sopenharmony_ci *
71bf215546Sopenharmony_ci * Color compression is only supported for tiled layouts.  In general
72bf215546Sopenharmony_ci * the meta "flag" buffer (ie. what holds the compression state for
73bf215546Sopenharmony_ci * each block) can be separate from the color data, except for textures
74bf215546Sopenharmony_ci * on a5xx where it needs to be interleaved with layers/levels of a
75bf215546Sopenharmony_ci * texture.
76bf215546Sopenharmony_ci */
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci#define FDL_MAX_MIP_LEVELS 15
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_cistruct fdl_slice {
81bf215546Sopenharmony_ci   uint32_t offset; /* offset of first layer in slice */
82bf215546Sopenharmony_ci   uint32_t size0;  /* size of first layer in slice */
83bf215546Sopenharmony_ci};
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci/* parameters for explicit (imported) layout */
86bf215546Sopenharmony_cistruct fdl_explicit_layout {
87bf215546Sopenharmony_ci   uint32_t offset;
88bf215546Sopenharmony_ci   uint32_t pitch;
89bf215546Sopenharmony_ci};
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci/**
92bf215546Sopenharmony_ci * Encapsulates the layout of a resource, including position of given 2d
93bf215546Sopenharmony_ci * surface (layer, level) within.  Or rather all the information needed
94bf215546Sopenharmony_ci * to derive this.
95bf215546Sopenharmony_ci */
96bf215546Sopenharmony_cistruct fdl_layout {
97bf215546Sopenharmony_ci   struct fdl_slice slices[FDL_MAX_MIP_LEVELS];
98bf215546Sopenharmony_ci   struct fdl_slice ubwc_slices[FDL_MAX_MIP_LEVELS];
99bf215546Sopenharmony_ci   uint32_t pitch0;
100bf215546Sopenharmony_ci   uint32_t ubwc_width0;
101bf215546Sopenharmony_ci   uint32_t layer_size;
102bf215546Sopenharmony_ci   uint32_t ubwc_layer_size; /* in bytes */
103bf215546Sopenharmony_ci   bool ubwc : 1;
104bf215546Sopenharmony_ci   bool layer_first : 1; /* see above description */
105bf215546Sopenharmony_ci   bool tile_all : 1;
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci   /* Note that for tiled textures, beyond a certain mipmap level (ie.
108bf215546Sopenharmony_ci    * when width is less than block size) things switch to linear.  In
109bf215546Sopenharmony_ci    * general you should not directly look at fdl_layout::tile_mode,
110bf215546Sopenharmony_ci    * but instead use fdl_surface::tile_mode which will correctly take
111bf215546Sopenharmony_ci    * this into account.
112bf215546Sopenharmony_ci    */
113bf215546Sopenharmony_ci   uint32_t tile_mode : 2;
114bf215546Sopenharmony_ci   /* Bytes per pixel (where a "pixel" is a single row of a block in the case
115bf215546Sopenharmony_ci    * of compression), including each sample in the case of multisample
116bf215546Sopenharmony_ci    * layouts.
117bf215546Sopenharmony_ci    */
118bf215546Sopenharmony_ci   uint8_t cpp;
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci   /**
121bf215546Sopenharmony_ci    * Left shift necessary to multiply by cpp.  Invalid for NPOT cpp, please
122bf215546Sopenharmony_ci    * use fdl_cpp_shift() to sanity check you aren't hitting that case.
123bf215546Sopenharmony_ci    */
124bf215546Sopenharmony_ci   uint8_t cpp_shift;
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci   uint32_t width0, height0, depth0;
127bf215546Sopenharmony_ci   uint32_t mip_levels;
128bf215546Sopenharmony_ci   uint32_t nr_samples;
129bf215546Sopenharmony_ci   enum pipe_format format;
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci   uint32_t size;       /* Size of the whole image, in bytes. */
132bf215546Sopenharmony_ci   uint32_t base_align; /* Alignment of the base address, in bytes. */
133bf215546Sopenharmony_ci   uint8_t pitchalign;  /* log2(pitchalign) */
134bf215546Sopenharmony_ci};
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_cistatic inline uint32_t
137bf215546Sopenharmony_cifdl_cpp_shift(const struct fdl_layout *layout)
138bf215546Sopenharmony_ci{
139bf215546Sopenharmony_ci   assert(util_is_power_of_two_or_zero(layout->cpp));
140bf215546Sopenharmony_ci   return layout->cpp_shift;
141bf215546Sopenharmony_ci}
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_cistatic inline uint32_t
144bf215546Sopenharmony_cifdl_pitch(const struct fdl_layout *layout, unsigned level)
145bf215546Sopenharmony_ci{
146bf215546Sopenharmony_ci   return align(u_minify(layout->pitch0, level), 1 << layout->pitchalign);
147bf215546Sopenharmony_ci}
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci#define RGB_TILE_WIDTH_ALIGNMENT  64
150bf215546Sopenharmony_ci#define RGB_TILE_HEIGHT_ALIGNMENT 16
151bf215546Sopenharmony_ci#define UBWC_PLANE_SIZE_ALIGNMENT 4096
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_cistatic inline uint32_t
154bf215546Sopenharmony_cifdl_ubwc_pitch(const struct fdl_layout *layout, unsigned level)
155bf215546Sopenharmony_ci{
156bf215546Sopenharmony_ci   if (!layout->ubwc)
157bf215546Sopenharmony_ci      return 0;
158bf215546Sopenharmony_ci   return align(u_minify(layout->ubwc_width0, level), RGB_TILE_WIDTH_ALIGNMENT);
159bf215546Sopenharmony_ci}
160bf215546Sopenharmony_ci
161bf215546Sopenharmony_cistatic inline uint32_t
162bf215546Sopenharmony_cifdl_layer_stride(const struct fdl_layout *layout, unsigned level)
163bf215546Sopenharmony_ci{
164bf215546Sopenharmony_ci   if (layout->layer_first)
165bf215546Sopenharmony_ci      return layout->layer_size;
166bf215546Sopenharmony_ci   else
167bf215546Sopenharmony_ci      return layout->slices[level].size0;
168bf215546Sopenharmony_ci}
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci/* a2xx is special and needs PoT alignment for mipmaps: */
171bf215546Sopenharmony_cistatic inline uint32_t
172bf215546Sopenharmony_cifdl2_pitch(const struct fdl_layout *layout, unsigned level)
173bf215546Sopenharmony_ci{
174bf215546Sopenharmony_ci   uint32_t pitch = fdl_pitch(layout, level);
175bf215546Sopenharmony_ci   if (level)
176bf215546Sopenharmony_ci      pitch = util_next_power_of_two(pitch);
177bf215546Sopenharmony_ci   return pitch;
178bf215546Sopenharmony_ci}
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_cistatic inline uint32_t
181bf215546Sopenharmony_cifdl2_pitch_pixels(const struct fdl_layout *layout, unsigned level)
182bf215546Sopenharmony_ci{
183bf215546Sopenharmony_ci   return fdl2_pitch(layout, level) >> fdl_cpp_shift(layout);
184bf215546Sopenharmony_ci}
185bf215546Sopenharmony_ci
186bf215546Sopenharmony_cistatic inline uint32_t
187bf215546Sopenharmony_cifdl_surface_offset(const struct fdl_layout *layout, unsigned level,
188bf215546Sopenharmony_ci                   unsigned layer)
189bf215546Sopenharmony_ci{
190bf215546Sopenharmony_ci   const struct fdl_slice *slice = &layout->slices[level];
191bf215546Sopenharmony_ci   return slice->offset + fdl_layer_stride(layout, level) * layer;
192bf215546Sopenharmony_ci}
193bf215546Sopenharmony_ci
194bf215546Sopenharmony_cistatic inline uint32_t
195bf215546Sopenharmony_cifdl_ubwc_offset(const struct fdl_layout *layout, unsigned level, unsigned layer)
196bf215546Sopenharmony_ci{
197bf215546Sopenharmony_ci   const struct fdl_slice *slice = &layout->ubwc_slices[level];
198bf215546Sopenharmony_ci   return slice->offset + layer * layout->ubwc_layer_size;
199bf215546Sopenharmony_ci}
200bf215546Sopenharmony_ci
201bf215546Sopenharmony_ci/* Minimum layout width to enable UBWC. */
202bf215546Sopenharmony_ci#define FDL_MIN_UBWC_WIDTH 16
203bf215546Sopenharmony_ci
204bf215546Sopenharmony_cistatic inline bool
205bf215546Sopenharmony_cifdl_level_linear(const struct fdl_layout *layout, int level)
206bf215546Sopenharmony_ci{
207bf215546Sopenharmony_ci   if (layout->tile_all)
208bf215546Sopenharmony_ci      return false;
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_ci   unsigned w = u_minify(layout->width0, level);
211bf215546Sopenharmony_ci   if (w < FDL_MIN_UBWC_WIDTH)
212bf215546Sopenharmony_ci      return true;
213bf215546Sopenharmony_ci
214bf215546Sopenharmony_ci   return false;
215bf215546Sopenharmony_ci}
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_cistatic inline uint32_t
218bf215546Sopenharmony_cifdl_tile_mode(const struct fdl_layout *layout, int level)
219bf215546Sopenharmony_ci{
220bf215546Sopenharmony_ci   if (layout->tile_mode && fdl_level_linear(layout, level))
221bf215546Sopenharmony_ci      return 0; /* linear */
222bf215546Sopenharmony_ci   else
223bf215546Sopenharmony_ci      return layout->tile_mode;
224bf215546Sopenharmony_ci}
225bf215546Sopenharmony_ci
226bf215546Sopenharmony_cistatic inline bool
227bf215546Sopenharmony_cifdl_ubwc_enabled(const struct fdl_layout *layout, int level)
228bf215546Sopenharmony_ci{
229bf215546Sopenharmony_ci   return layout->ubwc;
230bf215546Sopenharmony_ci}
231bf215546Sopenharmony_ci
232bf215546Sopenharmony_ciconst char *fdl_tile_mode_desc(const struct fdl_layout *layout, int level);
233bf215546Sopenharmony_ci
234bf215546Sopenharmony_civoid fdl_layout_buffer(struct fdl_layout *layout, uint32_t size);
235bf215546Sopenharmony_ci
236bf215546Sopenharmony_civoid fdl5_layout(struct fdl_layout *layout, enum pipe_format format,
237bf215546Sopenharmony_ci                 uint32_t nr_samples, uint32_t width0, uint32_t height0,
238bf215546Sopenharmony_ci                 uint32_t depth0, uint32_t mip_levels, uint32_t array_size,
239bf215546Sopenharmony_ci                 bool is_3d);
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_cibool fdl6_layout(struct fdl_layout *layout, enum pipe_format format,
242bf215546Sopenharmony_ci                 uint32_t nr_samples, uint32_t width0, uint32_t height0,
243bf215546Sopenharmony_ci                 uint32_t depth0, uint32_t mip_levels, uint32_t array_size,
244bf215546Sopenharmony_ci                 bool is_3d, struct fdl_explicit_layout *plane_layout);
245bf215546Sopenharmony_ci
246bf215546Sopenharmony_cistatic inline void
247bf215546Sopenharmony_cifdl_set_pitchalign(struct fdl_layout *layout, unsigned pitchalign)
248bf215546Sopenharmony_ci{
249bf215546Sopenharmony_ci   uint32_t nblocksx = util_format_get_nblocksx(layout->format, layout->width0);
250bf215546Sopenharmony_ci   layout->pitchalign = pitchalign;
251bf215546Sopenharmony_ci   layout->pitch0 = align(nblocksx * layout->cpp, 1 << pitchalign);
252bf215546Sopenharmony_ci}
253bf215546Sopenharmony_ci
254bf215546Sopenharmony_civoid fdl_dump_layout(struct fdl_layout *layout);
255bf215546Sopenharmony_ci
256bf215546Sopenharmony_civoid fdl6_get_ubwc_blockwidth(const struct fdl_layout *layout,
257bf215546Sopenharmony_ci                              uint32_t *blockwidth, uint32_t *blockheight);
258bf215546Sopenharmony_ci
259bf215546Sopenharmony_cienum fdl_view_type {
260bf215546Sopenharmony_ci   FDL_VIEW_TYPE_1D = 0,
261bf215546Sopenharmony_ci   FDL_VIEW_TYPE_2D = 1,
262bf215546Sopenharmony_ci   FDL_VIEW_TYPE_CUBE = 2,
263bf215546Sopenharmony_ci   FDL_VIEW_TYPE_3D = 3,
264bf215546Sopenharmony_ci   FDL_VIEW_TYPE_BUFFER = 4,
265bf215546Sopenharmony_ci};
266bf215546Sopenharmony_ci
267bf215546Sopenharmony_cienum fdl_chroma_location {
268bf215546Sopenharmony_ci   FDL_CHROMA_LOCATION_COSITED_EVEN = 0,
269bf215546Sopenharmony_ci   FDL_CHROMA_LOCATION_MIDPOINT = 1,
270bf215546Sopenharmony_ci};
271bf215546Sopenharmony_ci
272bf215546Sopenharmony_cistruct fdl_view_args {
273bf215546Sopenharmony_ci   uint64_t iova;
274bf215546Sopenharmony_ci   uint32_t base_array_layer, base_miplevel;
275bf215546Sopenharmony_ci   uint32_t layer_count, level_count;
276bf215546Sopenharmony_ci   float min_lod_clamp;
277bf215546Sopenharmony_ci   unsigned char swiz[4];
278bf215546Sopenharmony_ci   enum pipe_format format;
279bf215546Sopenharmony_ci   enum fdl_view_type type;
280bf215546Sopenharmony_ci   enum fdl_chroma_location chroma_offsets[2];
281bf215546Sopenharmony_ci};
282bf215546Sopenharmony_ci
283bf215546Sopenharmony_ci#define FDL6_TEX_CONST_DWORDS 16
284bf215546Sopenharmony_ci
285bf215546Sopenharmony_cistruct fdl6_view {
286bf215546Sopenharmony_ci   uint64_t base_addr;
287bf215546Sopenharmony_ci   uint64_t ubwc_addr;
288bf215546Sopenharmony_ci   uint32_t layer_size;
289bf215546Sopenharmony_ci   uint32_t ubwc_layer_size;
290bf215546Sopenharmony_ci
291bf215546Sopenharmony_ci   uint32_t width, height;
292bf215546Sopenharmony_ci   bool need_y2_align;
293bf215546Sopenharmony_ci
294bf215546Sopenharmony_ci   bool ubwc_enabled;
295bf215546Sopenharmony_ci
296bf215546Sopenharmony_ci   enum pipe_format format;
297bf215546Sopenharmony_ci
298bf215546Sopenharmony_ci   uint32_t descriptor[FDL6_TEX_CONST_DWORDS];
299bf215546Sopenharmony_ci
300bf215546Sopenharmony_ci   /* Descriptor for use as a storage image as opposed to a sampled image.
301bf215546Sopenharmony_ci    * This has a few differences for cube maps (e.g. type).
302bf215546Sopenharmony_ci    */
303bf215546Sopenharmony_ci   uint32_t storage_descriptor[FDL6_TEX_CONST_DWORDS];
304bf215546Sopenharmony_ci
305bf215546Sopenharmony_ci   /* pre-filled register values */
306bf215546Sopenharmony_ci   uint32_t PITCH;
307bf215546Sopenharmony_ci   uint32_t FLAG_BUFFER_PITCH;
308bf215546Sopenharmony_ci
309bf215546Sopenharmony_ci   uint32_t RB_MRT_BUF_INFO;
310bf215546Sopenharmony_ci   uint32_t SP_FS_MRT_REG;
311bf215546Sopenharmony_ci
312bf215546Sopenharmony_ci   uint32_t SP_PS_2D_SRC_INFO;
313bf215546Sopenharmony_ci   uint32_t SP_PS_2D_SRC_SIZE;
314bf215546Sopenharmony_ci
315bf215546Sopenharmony_ci   uint32_t RB_2D_DST_INFO;
316bf215546Sopenharmony_ci
317bf215546Sopenharmony_ci   uint32_t RB_BLIT_DST_INFO;
318bf215546Sopenharmony_ci
319bf215546Sopenharmony_ci   uint32_t GRAS_LRZ_DEPTH_VIEW;
320bf215546Sopenharmony_ci};
321bf215546Sopenharmony_ci
322bf215546Sopenharmony_civoid
323bf215546Sopenharmony_cifdl6_view_init(struct fdl6_view *view, const struct fdl_layout **layouts,
324bf215546Sopenharmony_ci               const struct fdl_view_args *args, bool has_z24uint_s8uint);
325bf215546Sopenharmony_civoid
326bf215546Sopenharmony_cifdl6_buffer_view_init(uint32_t *descriptor, enum pipe_format format,
327bf215546Sopenharmony_ci                      const uint8_t *swiz, uint64_t iova, uint32_t size);
328bf215546Sopenharmony_ci
329bf215546Sopenharmony_civoid
330bf215546Sopenharmony_cifdl6_format_swiz(enum pipe_format format, bool has_z24uint_s8uint,
331bf215546Sopenharmony_ci                 unsigned char *format_swiz);
332bf215546Sopenharmony_ci
333bf215546Sopenharmony_ci#endif /* FREEDRENO_LAYOUT_H_ */
334