Debian Codegraph @

src/panfrost/lib/pan_image.h

raw
1 /*
2 * Copyright (C) 2008 VMware, Inc.
3 * Copyright (C) 2014 Broadcom
4 * Copyright (C) 2018-2019 Alyssa Rosenzweig
5 * Copyright (C) 2019-2020 Collabora, Ltd.
6 * SPDX-License-Identifier: MIT
7 */
8
9 #ifndef __PAN_IMAGE_H
10 #define __PAN_IMAGE_H
11
12 #include "genxml/gen_macros.h"
13
14 #include <stdbool.h>
15 #include "util/format/u_format.h"
16 #include "pan_format.h"
17 #include "pan_layout.h"
18 #include "pan_mod.h"
19 #include "pan_props.h"
20
21 #include "kmod/pan_kmod.h"
22
23 #include "util/log.h"
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 struct pan_mod_handler;
30
31 struct pan_image_plane {
32 struct pan_image_layout layout;
33 uint64_t base;
34 };
35
36 struct pan_image {
37 struct pan_image_props props;
38 const struct pan_mod_handler *mod_handler;
39 struct pan_image_plane *planes[MAX_IMAGE_PLANES];
40 };
41
42 struct pan_image_plane_ref {
43 struct pan_image *image;
44 uint32_t plane_idx;
45 };
46
47 struct pan_image_view {
48 /* Format, dimension and sample count of the view might differ from
49 * those of the image (2D view of a 3D image surface for instance).
50 */
51 enum pipe_format format;
52 enum mali_texture_dimension dim;
53 unsigned first_level, last_level;
54 unsigned first_layer, last_layer;
55 float min_lod;
56 unsigned char swizzle[4];
57
58 /* planes 1 and 2 are NULL for single plane formats */
59 struct pan_image_plane_ref planes[MAX_IMAGE_PLANES];
60
61 /* If EXT_multisampled_render_to_texture is used, this may be
62 * greater than image->layout.nr_samples. */
63 unsigned nr_samples;
64
65 struct {
66 unsigned narrow;
67 unsigned hdr;
68 } astc;
69 };
70
71 static inline struct pan_image_plane_ref
72 pan_image_view_get_plane(const struct pan_image_view *iview, uint32_t idx)
73 {
74 if (idx >= ARRAY_SIZE(iview->planes))
75 return (struct pan_image_plane_ref){0};
76
77 return iview->planes[idx];
78 }
79
80 static inline unsigned
81 pan_image_view_get_plane_mask(const struct pan_image_view *iview)
82 {
83 unsigned mask = 0;
84
85 for (unsigned i = 0; i < ARRAY_SIZE(iview->planes); i++) {
86 if (iview->planes[i].image)
87 mask |= BITFIELD_BIT(i);
88 }
89
90 return mask;
91 }
92
93 static inline unsigned
94 pan_image_view_get_first_plane_idx(const struct pan_image_view *iview)
95 {
96 unsigned mask = pan_image_view_get_plane_mask(iview);
97
98 assert(mask);
99 return ffs(mask) - 1;
100 }
101
102 static inline struct pan_image_plane_ref
103 pan_image_view_get_first_plane(const struct pan_image_view *iview)
104 {
105 unsigned first_plane_idx = pan_image_view_get_first_plane_idx(iview);
106 return pan_image_view_get_plane(iview, first_plane_idx);
107 }
108
109 static inline uint32_t
110 pan_image_view_get_nr_samples(const struct pan_image_view *iview)
111 {
112 const struct pan_image_plane_ref pref = pan_image_view_get_first_plane(iview);
113
114 if (!pref.image)
115 return 0;
116
117 return pref.image->props.nr_samples;
118 }
119
120 static inline uint32_t
121 pan_image_view_get_layer_count(const struct pan_image_view *iview)
122 {
123 const struct pan_image_plane_ref pref = pan_image_view_get_first_plane(iview);
124
125 if (!pref.image)
126 return 0;
127
128 return iview->dim == MALI_TEXTURE_DIMENSION_3D
129 ? pref.image->props.extent_px.depth
130 : iview->last_layer - iview->first_layer + 1;
131 }
132
133 static inline const struct pan_image_plane_ref
134 pan_image_view_get_color_plane(const struct pan_image_view *iview)
135 {
136 /* We only support rendering to plane 0 */
137 assert(pan_image_view_get_plane(iview, 1).image == NULL);
138 return pan_image_view_get_plane(iview, 0);
139 }
140
141 static inline bool
142 pan_image_view_has_crc(const struct pan_image_view *iview)
143 {
144 const struct pan_image_plane_ref p = pan_image_view_get_color_plane(iview);
145
146 if (!p.image)
147 return false;
148
149 return p.image->props.crc;
150 }
151
152 static inline struct pan_image_plane_ref
153 pan_image_view_get_s_plane(const struct pan_image_view *iview)
154 {
155 ASSERTED const struct util_format_description *fdesc =
156 util_format_description(iview->format);
157 assert(util_format_has_stencil(fdesc));
158
159 /* In case of multiplanar depth/stencil, the stencil is always on
160 * plane 1. Combined depth/stencil only has one plane, so depth
161 * will be on plane 0 in either case.
162 */
163 const struct pan_image_plane_ref pref =
164 iview->planes[1].image ? iview->planes[1] : iview->planes[0];
165
166 assert(pref.image);
167 fdesc = util_format_description(pref.image->props.format);
168 assert(util_format_has_stencil(fdesc));
169 return pref;
170 }
171
172 static inline struct pan_image_plane_ref
173 pan_image_view_get_zs_plane(const struct pan_image_view *iview)
174 {
175 assert(util_format_is_depth_or_stencil(iview->format));
176
177 /* Depth or combined depth-stencil is always on plane 0. */
178 return pan_image_view_get_plane(iview, 0);
179 }
180
181 static inline void
182 pan_image_view_check(const struct pan_image_view *iview)
183 {
184 #ifndef NDEBUG
185 unsigned nplanes = util_format_get_num_planes(iview->format);
186 struct pan_image_plane_ref pref;
187
188 for (unsigned i = 0; i < nplanes; i++) {
189 if (util_format_is_depth_or_stencil(iview->format)) {
190 const struct util_format_description *fdesc =
191 util_format_description(iview->format);
192
193 if (util_format_has_stencil(fdesc))
194 pref = pan_image_view_get_s_plane(iview);
195 else
196 pref = pan_image_view_get_zs_plane(iview);
197 } else {
198 pref = iview->planes[i];
199 }
200
201 /* Make sure we have an image and the plane we point to exists. */
202 assert(pref.image);
203 assert(pref.plane_idx <
204 util_format_get_num_planes(pref.image->props.format));
205
206 enum pipe_format view_format =
207 util_format_get_plane_format(iview->format, i);
208 enum pipe_format img_format =
209 util_format_get_plane_format(pref.image->props.format, pref.plane_idx);
210
211 /* View-based pixel re-interpretation only allowed if the formats
212 * blocksize match. */
213 assert(util_format_get_blocksize(view_format) ==
214 util_format_get_blocksize(img_format));
215 }
216 #endif
217 }
218
219 static inline uint64_t
220 pan_image_mip_level_size(const struct pan_image *image, unsigned plane_idx,
221 unsigned mip_level)
222 {
223 assert(plane_idx < ARRAY_SIZE(image->planes) &&
224 plane_idx < util_format_get_num_planes(image->props.format));
225 assert(mip_level < image->props.nr_slices);
226 assert(image->planes[plane_idx]);
227
228 uint64_t size = image->planes[plane_idx]->layout.slices[mip_level].size_B;
229
230 /* If this is an array, we need to cover the whole array. */
231 if (image->props.array_size > 1) {
232 size += image->planes[plane_idx]->layout.array_stride_B *
233 (image->props.array_size - 1);
234 }
235
236 return size;
237 }
238
239 static inline uint32_t
240 pan_image_get_wsi_row_pitch(const struct pan_image *image, unsigned plane_idx,
241 unsigned mip_level)
242 {
243 assert(image->mod_handler);
244 assert(image->mod_handler->get_wsi_row_pitch);
245 assert(plane_idx < ARRAY_SIZE(image->planes) &&
246 plane_idx < util_format_get_num_planes(image->props.format));
247 assert(image->planes[plane_idx]);
248
249 return image->mod_handler->get_wsi_row_pitch(image, plane_idx, mip_level);
250 }
251
252 static inline uint64_t
253 pan_image_get_wsi_offset(const struct pan_image *image, unsigned plane_idx,
254 unsigned mip_level)
255 {
256 assert(plane_idx < ARRAY_SIZE(image->planes) &&
257 plane_idx < util_format_get_num_planes(image->props.format));
258 assert(mip_level < image->props.nr_slices);
259 assert(image->planes[plane_idx]);
260
261 return image->planes[plane_idx]->layout.slices[mip_level].offset_B;
262 }
263
264 bool pan_image_layout_init(
265 unsigned arch, struct pan_image *image, unsigned plane_idx,
266 const struct pan_image_layout_constraints *explicit_layout_constraints);
267
268 struct pan_image_usage {
269 /* PAN_BIND_xxx flags. */
270 uint32_t bind;
271
272 /* Image needs to be mappable at standard sparse block granularity. */
273 bool standard_sparse_mapping_granularity;
274
275 /* Image filled directly from the CPU. */
276 bool host_copy;
277
278 /* Image frequently updated with host data. */
279 bool frequent_host_updates;
280
281 /* Legacy scanout image. */
282 bool legacy_scanout;
283
284 /* Image created by WSI. */
285 bool wsi;
286 };
287
288 static inline enum pan_mod_support
289 pan_image_test_props(const struct pan_kmod_dev_props *dprops,
290 const struct pan_image_props *iprops,
291 const struct pan_image_usage *iusage)
292 {
293 const unsigned arch = pan_arch(dprops->gpu_id);
294 struct pan_image image = {
295 .props = *iprops,
296 .mod_handler = pan_mod_get_handler(arch, iprops->modifier),
297 };
298
299 if (!image.mod_handler)
300 return PAN_MOD_NOT_SUPPORTED;
301
302 enum pan_mod_support ret =
303 image.mod_handler->test_props(dprops, &image.props, iusage);
304 if (ret == PAN_MOD_NOT_SUPPORTED)
305 return ret;
306
307 /* Now make sure the layout can be properly initialized on all planes. */
308 uint32_t plane_count = util_format_get_num_planes(image.props.format);
309 for (uint32_t p = 0; p < plane_count; p++) {
310 struct pan_image_plane plane;
311
312 memset(&plane, 0, sizeof(plane));
313 image.planes[p] = &plane;
314
315 if (!pan_image_layout_init(arch, &image, p, NULL))
316 return PAN_MOD_NOT_SUPPORTED;
317 }
318
319 return ret;
320 }
321
322 static inline bool
323 pan_image_test_modifier_with_format(const struct pan_kmod_dev_props *dprops,
324 uint64_t modifier, enum pipe_format format)
325 {
326 /* To check if a <modifier,format> pair is supported, we define the smallest
327 * possible 2D image (or 3D image if this is a 3D compressed format). */
328 const struct pan_image_props iprops = {
329 .modifier = modifier,
330 .format = format,
331 .extent_px = {
332 .width = util_format_get_blockwidth(format),
333 .height = util_format_get_blockheight(format),
334 .depth = util_format_get_blockdepth(format),
335 },
336 .nr_samples = 1,
337 .dim = util_format_get_blockdepth(format) > 1 ? MALI_TEXTURE_DIMENSION_3D
338 : MALI_TEXTURE_DIMENSION_2D,
339 .nr_slices = 1,
340 .array_size = 1,
341 };
342
343 return pan_image_test_props(dprops, &iprops, NULL) != PAN_MOD_NOT_SUPPORTED;
344 }
345
346 #ifdef __cplusplus
347 } /* extern C */
348 #endif
349
350 #endif