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 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 *
26 */
27
28#ifndef __PAN_TEXTURE_H
29#define __PAN_TEXTURE_H
30
31#include "genxml/gen_macros.h"
32
33#include <stdbool.h>
34#include "drm-uapi/drm_fourcc.h"
35#include "util/format/u_format.h"
36#include "compiler/shader_enums.h"
37#include "genxml/gen_macros.h"
38#include "pan_bo.h"
39#include "pan_device.h"
40#include "pan_util.h"
41#include "pan_format.h"
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
47#define PAN_MODIFIER_COUNT 6
48extern uint64_t pan_best_modifiers[PAN_MODIFIER_COUNT];
49
50struct pan_image_slice_layout {
51        unsigned offset;
52
53        /* For AFBC images, the number of bytes between two rows of AFBC
54         * headers.
55         *
56         * For non-AFBC images, the number of bytes between two rows of texels.
57         * For linear images, this will equal the logical stride. For
58         * images that are compressed or interleaved, this will be greater than
59         * the logical stride.
60         */
61        unsigned row_stride;
62
63        unsigned surface_stride;
64
65        struct {
66                /* Size of the AFBC header preceding each slice */
67                unsigned header_size;
68
69                /* Size of the AFBC body */
70                unsigned body_size;
71
72                /* Stride between AFBC headers of two consecutive surfaces.
73                 * For 3D textures, this must be set to header size since
74                 * AFBC headers are allocated together, for 2D arrays this
75                 * should be set to size0, since AFBC headers are placed at
76                 * the beginning of each layer
77                 */
78                unsigned surface_stride;
79        } afbc;
80
81        /* If checksumming is enabled following the slice, what
82         * is its offset/stride? */
83        struct {
84                unsigned offset;
85                unsigned stride;
86                unsigned size;
87        } crc;
88
89        unsigned size;
90};
91
92enum pan_image_crc_mode {
93      PAN_IMAGE_CRC_NONE,
94      PAN_IMAGE_CRC_INBAND,
95      PAN_IMAGE_CRC_OOB,
96};
97
98struct pan_image_layout {
99        uint64_t modifier;
100        enum pipe_format format;
101        unsigned width, height, depth;
102        unsigned nr_samples;
103        enum mali_texture_dimension dim;
104        unsigned nr_slices;
105        unsigned array_size;
106        enum pan_image_crc_mode crc_mode;
107
108        /* The remaining fields may be derived from the above by calling
109         * pan_image_layout_init
110         */
111
112        struct pan_image_slice_layout slices[MAX_MIP_LEVELS];
113
114        /* crc_size != 0 only if crc_mode == OOB otherwise CRC words are
115         * counted in data_size */
116        unsigned crc_size;
117        unsigned data_size;
118        unsigned array_stride;
119};
120
121struct pan_image_mem {
122        struct panfrost_bo *bo;
123        unsigned offset;
124};
125
126struct pan_image {
127        struct pan_image_mem data;
128        struct pan_image_mem crc;
129        struct pan_image_layout layout;
130};
131
132struct pan_image_view {
133        /* Format, dimension and sample count of the view might differ from
134         * those of the image (2D view of a 3D image surface for instance).
135         */
136        enum pipe_format format;
137        enum mali_texture_dimension dim;
138        unsigned first_level, last_level;
139        unsigned first_layer, last_layer;
140        unsigned char swizzle[4];
141        const struct pan_image *image;
142
143        /* If EXT_multisampled_render_to_texture is used, this may be
144         * greater than image->layout.nr_samples. */
145        unsigned nr_samples;
146
147        /* Only valid if dim == 1D, needed to implement buffer views */
148        struct {
149                unsigned offset;
150                unsigned size;
151        } buf;
152};
153
154unsigned
155panfrost_compute_checksum_size(
156        struct pan_image_slice_layout *slice,
157        unsigned width,
158        unsigned height);
159
160/* AFBC */
161
162bool
163panfrost_format_supports_afbc(const struct panfrost_device *dev,
164                enum pipe_format format);
165
166enum pipe_format
167panfrost_afbc_format(unsigned arch, enum pipe_format format);
168
169#define AFBC_HEADER_BYTES_PER_TILE 16
170
171bool
172panfrost_afbc_can_ytr(enum pipe_format format);
173
174bool
175panfrost_afbc_can_tile(const struct panfrost_device *dev);
176
177/*
178 * Represents the block size of a single plane. For AFBC, this represents the
179 * superblock size. For u-interleaving, this represents the tile size.
180 */
181struct pan_block_size {
182        /** Width of block */
183        unsigned width;
184
185        /** Height of blocks */
186        unsigned height;
187};
188
189struct pan_block_size panfrost_afbc_superblock_size(uint64_t modifier);
190
191unsigned panfrost_afbc_superblock_width(uint64_t modifier);
192
193unsigned panfrost_afbc_superblock_height(uint64_t modifier);
194
195bool panfrost_afbc_is_wide(uint64_t modifier);
196
197uint32_t pan_afbc_row_stride(uint64_t modifier, uint32_t width);
198
199uint32_t pan_afbc_stride_blocks(uint64_t modifier, uint32_t row_stride_bytes);
200
201struct pan_block_size
202panfrost_block_size(uint64_t modifier, enum pipe_format format);
203
204#ifdef PAN_ARCH
205unsigned
206GENX(panfrost_estimate_texture_payload_size)(const struct pan_image_view *iview);
207
208void
209GENX(panfrost_new_texture)(const struct panfrost_device *dev,
210                           const struct pan_image_view *iview,
211                           void *out,
212                           const struct panfrost_ptr *payload);
213#endif
214
215unsigned
216panfrost_get_layer_stride(const struct pan_image_layout *layout,
217                          unsigned level);
218
219unsigned
220panfrost_texture_offset(const struct pan_image_layout *layout,
221                        unsigned level, unsigned array_idx,
222                        unsigned surface_idx);
223
224struct pan_pool;
225struct pan_scoreboard;
226
227/* DRM modifier helper */
228
229#define drm_is_afbc(mod) \
230        ((mod >> 52) == (DRM_FORMAT_MOD_ARM_TYPE_AFBC | \
231                (DRM_FORMAT_MOD_VENDOR_ARM << 4)))
232
233struct pan_image_explicit_layout {
234        unsigned offset;
235        unsigned row_stride;
236};
237
238bool
239pan_image_layout_init(struct pan_image_layout *layout,
240                      const struct pan_image_explicit_layout *explicit_layout);
241
242unsigned
243panfrost_get_legacy_stride(const struct pan_image_layout *layout,
244                           unsigned level);
245
246unsigned
247panfrost_from_legacy_stride(unsigned legacy_stride,
248                            enum pipe_format format,
249                            uint64_t modifier);
250
251uint32_t
252pan_stride_align_B(enum pipe_format format);
253
254bool
255pan_is_stride_aligned(enum pipe_format format, unsigned stride_B);
256
257struct pan_surface {
258        union {
259                mali_ptr data;
260                struct {
261                        mali_ptr header;
262                        mali_ptr body;
263                } afbc;
264        };
265};
266
267void
268pan_iview_get_surface(const struct pan_image_view *iview,
269                      unsigned level, unsigned layer, unsigned sample,
270                      struct pan_surface *surf);
271
272
273#if PAN_ARCH >= 9
274enum mali_afbc_compression_mode
275pan_afbc_compression_mode(enum pipe_format format);
276#endif
277
278#ifdef __cplusplus
279} /* extern C */
280#endif
281
282#endif
283