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#define PAN_MODIFIER_COUNT 4
44extern uint64_t pan_best_modifiers[PAN_MODIFIER_COUNT];
45
46struct pan_image_slice_layout {
47        unsigned offset;
48        unsigned line_stride;
49        unsigned row_stride;
50        unsigned surface_stride;
51
52        struct {
53                /* Size of the AFBC header preceding each slice */
54                unsigned header_size;
55
56                /* Size of the AFBC body */
57                unsigned body_size;
58
59                /* Stride between two rows of AFBC headers */
60                unsigned row_stride;
61
62                /* Stride between AFBC headers of two consecutive surfaces.
63                 * For 3D textures, this must be set to header size since
64                 * AFBC headers are allocated together, for 2D arrays this
65                 * should be set to size0, since AFBC headers are placed at
66                 * the beginning of each layer
67                 */
68                unsigned surface_stride;
69        } afbc;
70
71        /* If checksumming is enabled following the slice, what
72         * is its offset/stride? */
73        struct {
74                unsigned offset;
75                unsigned stride;
76                unsigned size;
77        } crc;
78
79        unsigned size;
80};
81
82enum pan_image_crc_mode {
83      PAN_IMAGE_CRC_NONE,
84      PAN_IMAGE_CRC_INBAND,
85      PAN_IMAGE_CRC_OOB,
86};
87
88struct pan_image_layout {
89        uint64_t modifier;
90        enum pipe_format format;
91        unsigned width, height, depth;
92        unsigned nr_samples;
93        enum mali_texture_dimension dim;
94        unsigned nr_slices;
95        struct pan_image_slice_layout slices[MAX_MIP_LEVELS];
96        unsigned array_size;
97        unsigned array_stride;
98        unsigned data_size;
99
100        enum pan_image_crc_mode crc_mode;
101        /* crc_size != 0 only if crc_mode == OOB otherwise CRC words are
102         * counted in data_size */
103        unsigned crc_size;
104};
105
106struct pan_image_mem {
107        struct panfrost_bo *bo;
108        unsigned offset;
109};
110
111struct pan_image {
112        struct pan_image_mem data;
113        struct pan_image_mem crc;
114        struct pan_image_layout layout;
115};
116
117struct pan_image_view {
118        /* Format, dimension and sample count of the view might differ from
119         * those of the image (2D view of a 3D image surface for instance).
120         */
121        enum pipe_format format;
122        enum mali_texture_dimension dim;
123        unsigned first_level, last_level;
124        unsigned first_layer, last_layer;
125        unsigned char swizzle[4];
126        const struct pan_image *image;
127
128        /* If EXT_multisampled_render_to_texture is used, this may be
129         * greater than image->layout.nr_samples. */
130        unsigned nr_samples;
131
132        /* Only valid if dim == 1D, needed to implement buffer views */
133        struct {
134                unsigned offset;
135                unsigned size;
136        } buf;
137};
138
139unsigned
140panfrost_compute_checksum_size(
141        struct pan_image_slice_layout *slice,
142        unsigned width,
143        unsigned height);
144
145/* AFBC */
146
147bool
148panfrost_format_supports_afbc(const struct panfrost_device *dev,
149                enum pipe_format format);
150
151enum pipe_format
152panfrost_afbc_format(const struct panfrost_device *dev, enum pipe_format format);
153
154#define AFBC_HEADER_BYTES_PER_TILE 16
155
156unsigned
157panfrost_afbc_header_size(unsigned width, unsigned height);
158
159bool
160panfrost_afbc_can_ytr(enum pipe_format format);
161
162unsigned
163panfrost_block_dim(uint64_t modifier, bool width, unsigned plane);
164
165#ifdef PAN_ARCH
166unsigned
167GENX(panfrost_estimate_texture_payload_size)(const struct pan_image_view *iview);
168
169void
170GENX(panfrost_new_texture)(const struct panfrost_device *dev,
171                           const struct pan_image_view *iview,
172                           void *out,
173                           const struct panfrost_ptr *payload);
174#endif
175
176unsigned
177panfrost_get_layer_stride(const struct pan_image_layout *layout,
178                          unsigned level);
179
180unsigned
181panfrost_texture_offset(const struct pan_image_layout *layout,
182                        unsigned level, unsigned array_idx,
183                        unsigned surface_idx);
184
185struct pan_pool;
186struct pan_scoreboard;
187
188/* DRM modifier helper */
189
190#define drm_is_afbc(mod) \
191        ((mod >> 52) == (DRM_FORMAT_MOD_ARM_TYPE_AFBC | \
192                (DRM_FORMAT_MOD_VENDOR_ARM << 4)))
193
194struct pan_image_explicit_layout {
195        unsigned offset;
196        unsigned line_stride;
197};
198
199bool
200pan_image_layout_init(const struct panfrost_device *dev,
201                      struct pan_image_layout *layout,
202                      uint64_t modifier,
203                      enum pipe_format format,
204                      enum mali_texture_dimension dim,
205                      unsigned width, unsigned height, unsigned depth,
206                      unsigned array_size, unsigned nr_samples,
207                      unsigned nr_slices,
208                      enum pan_image_crc_mode crc_mode,
209                      const struct pan_image_explicit_layout *explicit_layout);
210
211struct pan_surface {
212        union {
213                mali_ptr data;
214                struct {
215                        mali_ptr header;
216                        mali_ptr body;
217                } afbc;
218        };
219};
220
221void
222pan_iview_get_surface(const struct pan_image_view *iview,
223                      unsigned level, unsigned layer, unsigned sample,
224                      struct pan_surface *surf);
225
226#endif
227