2 * Copyright © 2019 Google, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #ifndef FREEDRENO_LAYOUT_H_
25 #define FREEDRENO_LAYOUT_H_
30 #include "util/u_debug.h"
31 #include "util/u_math.h"
32 #include "util/format/u_format.h"
34 /* Shared freedreno mipmap layout helper
36 * It does *not* attempt to track surface transitions, in particular
37 * about UBWC state. Possibly it should, but
38 * (a) I'm not sure if in all cases we can transparently do in-
39 * place transitions (ie. a5xx textures with interleaved
41 * (b) Even if we can, we probably can't assume that we have
42 * figured out yet how to do in-place transition for every
46 /* Texture Layout on a3xx:
47 * -----------------------
49 * Each mipmap-level contains all of it's layers (ie. all cubmap
50 * faces, all 1d/2d array elements, etc). The texture sampler is
51 * programmed with the start address of each mipmap level, and hw
52 * derives the layer offset within the level.
55 * Texture Layout on a4xx+:
56 * -----------------------
58 * For cubemap and 2d array, each layer contains all of it's mipmap
59 * levels (layer_first layout).
61 * 3d textures are laid out as on a3xx.
63 * In either case, the slice represents the per-miplevel information,
64 * but in layer_first layout it only includes the first layer, and
65 * an additional offset of (rsc->layer_size * layer) must be added.
68 * UBWC Color Compressions (a5xx+):
69 * -------------------------------
71 * Color compression is only supported for tiled layouts. In general
72 * the meta "flag" buffer (ie. what holds the compression state for
73 * each block) can be separate from the color data, except for textures
74 * on a5xx where it needs to be interleaved with layers/levels of a
78 #define FDL_MAX_MIP_LEVELS 15
81 uint32_t offset
; /* offset of first layer in slice */
82 uint32_t size0
; /* size of first layer in slice */
85 /* parameters for explicit (imported) layout */
86 struct fdl_explicit_layout
{
92 * Encapsulates the layout of a resource, including position of given 2d
93 * surface (layer, level) within. Or rather all the information needed
97 struct fdl_slice slices
[FDL_MAX_MIP_LEVELS
];
98 struct fdl_slice ubwc_slices
[FDL_MAX_MIP_LEVELS
];
100 uint32_t ubwc_width0
;
102 uint32_t ubwc_layer_size
; /* in bytes */
104 bool layer_first
: 1; /* see above description */
106 /* Note that for tiled textures, beyond a certain mipmap level (ie.
107 * when width is less than block size) things switch to linear. In
108 * general you should not directly look at fdl_layout::tile_mode,
109 * but instead use fdl_surface::tile_mode which will correctly take
112 uint32_t tile_mode
: 2;
113 /* Bytes per pixel (where a "pixel" is a single row of a block in the case
114 * of compression), including each sample in the case of multisample
120 * Left shift necessary to multiply by cpp. Invalid for NPOT cpp, please
121 * use fdl_cpp_shift() to sanity check you aren't hitting that case.
125 uint32_t width0
, height0
, depth0
;
127 enum pipe_format format
;
129 uint32_t size
; /* Size of the whole image, in bytes. */
130 uint32_t base_align
; /* Alignment of the base address, in bytes. */
131 uint8_t pitchalign
; /* log2(pitchalign) */
134 static inline uint32_t
135 fdl_cpp_shift(const struct fdl_layout
*layout
)
137 assert(util_is_power_of_two_or_zero(layout
->cpp
));
138 return layout
->cpp_shift
;
141 static inline uint32_t
142 fdl_pitch(const struct fdl_layout
*layout
, unsigned level
)
144 return align(u_minify(layout
->pitch0
, level
), 1 << layout
->pitchalign
);
147 #define RGB_TILE_WIDTH_ALIGNMENT 64
148 #define RGB_TILE_HEIGHT_ALIGNMENT 16
149 #define UBWC_PLANE_SIZE_ALIGNMENT 4096
151 static inline uint32_t
152 fdl_ubwc_pitch(const struct fdl_layout
*layout
, unsigned level
)
156 return align(u_minify(layout
->ubwc_width0
, level
), RGB_TILE_WIDTH_ALIGNMENT
);
159 static inline uint32_t
160 fdl_layer_stride(const struct fdl_layout
*layout
, unsigned level
)
162 if (layout
->layer_first
)
163 return layout
->layer_size
;
165 return layout
->slices
[level
].size0
;
168 /* a2xx is special and needs PoT alignment for mipmaps: */
169 static inline uint32_t
170 fdl2_pitch(const struct fdl_layout
*layout
, unsigned level
)
172 uint32_t pitch
= fdl_pitch(layout
, level
);
174 pitch
= util_next_power_of_two(pitch
);
178 static inline uint32_t
179 fdl2_pitch_pixels(const struct fdl_layout
*layout
, unsigned level
)
181 return fdl2_pitch(layout
, level
) >> fdl_cpp_shift(layout
);
184 static inline uint32_t
185 fdl_surface_offset(const struct fdl_layout
*layout
, unsigned level
, unsigned layer
)
187 const struct fdl_slice
*slice
= &layout
->slices
[level
];
188 return slice
->offset
+ fdl_layer_stride(layout
, level
) * layer
;
191 static inline uint32_t
192 fdl_ubwc_offset(const struct fdl_layout
*layout
, unsigned level
, unsigned layer
)
194 const struct fdl_slice
*slice
= &layout
->ubwc_slices
[level
];
195 return slice
->offset
+ layer
* layout
->ubwc_layer_size
;
199 fdl_level_linear(const struct fdl_layout
*layout
, int level
)
204 unsigned w
= u_minify(layout
->width0
, level
);
211 static inline uint32_t
212 fdl_tile_mode(const struct fdl_layout
*layout
, int level
)
214 if (layout
->tile_mode
&& fdl_level_linear(layout
, level
))
215 return 0; /* linear */
217 return layout
->tile_mode
;
221 fdl_ubwc_enabled(const struct fdl_layout
*layout
, int level
)
227 fdl_layout_buffer(struct fdl_layout
*layout
, uint32_t size
);
230 fdl5_layout(struct fdl_layout
*layout
,
231 enum pipe_format format
, uint32_t nr_samples
,
232 uint32_t width0
, uint32_t height0
, uint32_t depth0
,
233 uint32_t mip_levels
, uint32_t array_size
, bool is_3d
);
236 fdl6_layout(struct fdl_layout
*layout
,
237 enum pipe_format format
, uint32_t nr_samples
,
238 uint32_t width0
, uint32_t height0
, uint32_t depth0
,
239 uint32_t mip_levels
, uint32_t array_size
, bool is_3d
,
240 struct fdl_explicit_layout
*plane_layout
);
243 fdl_set_pitchalign(struct fdl_layout
*layout
, unsigned pitchalign
)
245 uint32_t nblocksx
= util_format_get_nblocksx(layout
->format
, layout
->width0
);
246 layout
->pitchalign
= pitchalign
;
247 layout
->pitch0
= align(nblocksx
* layout
->cpp
, 1 << pitchalign
);
251 fdl_dump_layout(struct fdl_layout
*layout
);
254 fdl6_get_ubwc_blockwidth(struct fdl_layout
*layout
,
255 uint32_t *blockwidth
, uint32_t *blockheight
);
257 #endif /* FREEDRENO_LAYOUT_H_ */