freedreno: Move UBWC layout into a slices array like the non-UBWC slices.
[mesa.git] / src / freedreno / fdl / freedreno_layout.h
1 /*
2 * Copyright © 2019 Google, Inc.
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef FREEDRENO_LAYOUT_H_
25 #define FREEDRENO_LAYOUT_H_
26
27 /* Shared freedreno mipmap layout helper
28 *
29 * It does *not* attempt to track surface transitions, in particular
30 * about UBWC state. Possibly it should, but
31 * (a) I'm not sure if in all cases we can transparently do in-
32 * place transitions (ie. a5xx textures with interleaved
33 * meta and pixel data
34 * (b) Even if we can, we probably can't assume that we have
35 * figured out yet how to do in-place transition for every
36 * generation.
37 */
38
39 /* Texture Layout on a3xx:
40 * -----------------------
41 *
42 * Each mipmap-level contains all of it's layers (ie. all cubmap
43 * faces, all 1d/2d array elements, etc). The texture sampler is
44 * programmed with the start address of each mipmap level, and hw
45 * derives the layer offset within the level.
46 *
47 *
48 * Texture Layout on a4xx+:
49 * -----------------------
50 *
51 * For cubemap and 2d array, each layer contains all of it's mipmap
52 * levels (layer_first layout).
53 *
54 * 3d textures are laid out as on a3xx.
55 *
56 * In either case, the slice represents the per-miplevel information,
57 * but in layer_first layout it only includes the first layer, and
58 * an additional offset of (rsc->layer_size * layer) must be added.
59 *
60 *
61 * UBWC Color Compressions (a5xx+):
62 * -------------------------------
63 *
64 * Color compression is only supported for tiled layouts. In general
65 * the meta "flag" buffer (ie. what holds the compression state for
66 * each block) can be separate from the color data, except for textures
67 * on a5xx where it needs to be interleaved with layers/levels of a
68 * texture.
69 */
70
71 struct fdl_slice {
72 uint32_t offset; /* offset of first layer in slice */
73 uint32_t pitch;
74 uint32_t size0; /* size of first layer in slice */
75 };
76
77 /**
78 * Encapsulates the layout of a resource, including position of given 2d
79 * surface (layer, level) within. Or rather all the information needed
80 * to derive this.
81 */
82 struct fdl_layout {
83 struct fdl_slice slices[MAX_MIP_LEVELS];
84 struct fdl_slice ubwc_slices[MAX_MIP_LEVELS];
85 uint32_t layer_size;
86 bool layer_first : 1; /* see above description */
87
88 /* Note that for tiled textures, beyond a certain mipmap level (ie.
89 * when width is less than block size) things switch to linear. In
90 * general you should not directly look at fdl_layout::tile_mode,
91 * but instead use fdl_surface::tile_mode which will correctly take
92 * this into account.
93 */
94 uint32_t tile_mode : 2;
95 /* Bytes per pixel (where a "pixel" is a single row of a block in the case
96 * of compression), including each sample in the case of multisample
97 * layouts.
98 */
99 uint8_t cpp;
100
101 uint32_t width0, height0, depth0;
102
103 uint32_t ubwc_size;
104 };
105
106 static inline uint32_t
107 fdl_layer_stride(const struct fdl_layout *layout, unsigned level)
108 {
109 if (layout->layer_first)
110 return layout->layer_size;
111 else
112 return layout->slices[level].size0;
113 }
114
115 static inline uint32_t
116 fdl_surface_offset(const struct fdl_layout *layout, unsigned level, unsigned layer)
117 {
118 const struct fdl_slice *slice = &layout->slices[level];
119 return slice->offset + fdl_layer_stride(layout, level) * layer;
120 }
121
122 static inline uint32_t
123 fdl_ubwc_offset(const struct fdl_layout *layout, unsigned level, unsigned layer)
124 {
125 /* for now this doesn't do anything clever, but when UBWC is enabled
126 * for multi layer/level images, it will.
127 */
128 if (layout->ubwc_size) {
129 debug_assert(level == 0);
130 debug_assert(layer == 0);
131 }
132 return layout->ubwc_slices[0].offset;
133 }
134
135 static inline bool
136 fdl_level_linear(const struct fdl_layout *layout, int level)
137 {
138 unsigned w = u_minify(layout->width0, level);
139 if (w < 16)
140 return true;
141 return false;
142 }
143
144 static inline uint32_t
145 fdl_tile_mode(const struct fdl_layout *layout, int level)
146 {
147 if (layout->tile_mode && fdl_level_linear(layout, level))
148 return 0; /* linear */
149 else
150 return layout->tile_mode;
151 }
152
153 static inline bool
154 fdl_ubwc_enabled(const struct fdl_layout *layout, int level)
155 {
156 return layout->ubwc_size && fdl_tile_mode(layout, level);
157 }
158
159 #endif /* FREEDRENO_LAYOUT_H_ */