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 <stdbool.h>
32 #include "drm-uapi/drm_fourcc.h"
33 #include "util/format/u_format.h"
34 #include "compiler/shader_enums.h"
35 #include "midgard_pack.h"
36 #include "pan_bo.h"
37 
38 #define PAN_MODIFIER_COUNT 4
39 extern uint64_t pan_best_modifiers[PAN_MODIFIER_COUNT];
40 
41 struct panfrost_slice {
42         unsigned offset;
43         unsigned stride;
44         unsigned size0;
45 
46         /* If there is a header preceding each slice, how big is
47          * that header? Used for AFBC */
48         unsigned header_size;
49 
50         /* If checksumming is enabled following the slice, what
51          * is its offset/stride? */
52         unsigned checksum_offset;
53         unsigned checksum_stride;
54         struct panfrost_bo *checksum_bo;
55 
56         /* Has anything been written to this slice? */
57         bool initialized;
58 };
59 
60 struct pan_image {
61         /* Format and size */
62         uint16_t width0, height0, depth0, array_size;
63         enum pipe_format format;
64         enum mali_texture_dimension dim;
65         unsigned first_level, last_level;
66         unsigned first_layer, last_layer;
67         unsigned nr_samples;
68         struct panfrost_bo *bo;
69         struct panfrost_slice *slices;
70         unsigned cubemap_stride;
71         uint64_t modifier;
72 };
73 
74 unsigned
75 panfrost_compute_checksum_size(
76         struct panfrost_slice *slice,
77         unsigned width,
78         unsigned height);
79 
80 /* AFBC */
81 
82 bool
83 panfrost_format_supports_afbc(enum pipe_format format);
84 
85 unsigned
86 panfrost_afbc_header_size(unsigned width, unsigned height);
87 
88 bool
89 panfrost_afbc_can_ytr(enum pipe_format format);
90 
91 unsigned
92 panfrost_estimate_texture_payload_size(
93                 unsigned first_level, unsigned last_level,
94                 unsigned first_layer, unsigned last_layer,
95                 unsigned nr_samples,
96                 enum mali_texture_dimension dim, uint64_t modifier);
97 
98 void
99 panfrost_new_texture(
100         void *out,
101         uint16_t width, uint16_t height,
102         uint16_t depth, uint16_t array_size,
103         enum pipe_format format,
104         enum mali_texture_dimension dim,
105         uint64_t modifier,
106         unsigned first_level, unsigned last_level,
107         unsigned first_layer, unsigned last_layer,
108         unsigned nr_samples,
109         unsigned cube_stride,
110         unsigned swizzle,
111         mali_ptr base,
112         struct panfrost_slice *slices);
113 
114 void
115 panfrost_new_texture_bifrost(
116         const struct panfrost_device *dev,
117         struct mali_bifrost_texture_packed *out,
118         uint16_t width, uint16_t height,
119         uint16_t depth, uint16_t array_size,
120         enum pipe_format format,
121         enum mali_texture_dimension dim,
122         uint64_t modifier,
123         unsigned first_level, unsigned last_level,
124         unsigned first_layer, unsigned last_layer,
125         unsigned nr_samples,
126         unsigned cube_stride,
127         unsigned swizzle,
128         mali_ptr base,
129         struct panfrost_slice *slices,
130         const struct panfrost_ptr *payload);
131 
132 
133 unsigned
134 panfrost_get_layer_stride(struct panfrost_slice *slices, bool is_3d, unsigned cube_stride, unsigned level);
135 
136 unsigned
137 panfrost_texture_offset(struct panfrost_slice *slices, bool is_3d, unsigned cube_stride, unsigned level, unsigned face, unsigned sample);
138 
139 /* Formats */
140 
141 struct pan_blendable_format {
142         enum mali_color_buffer_internal_format internal;
143         enum mali_mfbd_color_format writeback;
144 };
145 
146 struct pan_blendable_format
147 panfrost_blend_format(enum pipe_format format);
148 
149 extern const struct panfrost_format panfrost_pipe_format_v6[PIPE_FORMAT_COUNT];
150 extern const struct panfrost_format panfrost_pipe_format_v7[PIPE_FORMAT_COUNT];
151 
152 enum mali_z_internal_format
153 panfrost_get_z_internal_format(enum pipe_format fmt);
154 
155 unsigned
156 panfrost_translate_swizzle_4(const unsigned char swizzle[4]);
157 
158 void
159 panfrost_invert_swizzle(const unsigned char *in, unsigned char *out);
160 
161 /* Helpers to construct swizzles */
162 
163 #define PAN_V6_SWIZZLE(R, G, B, A) ( \
164         ((MALI_CHANNEL_ ## R) << 0) | \
165         ((MALI_CHANNEL_ ## G) << 3) | \
166         ((MALI_CHANNEL_ ## B) << 6) | \
167         ((MALI_CHANNEL_ ## A) << 9))
168 
169 static inline unsigned
panfrost_get_default_swizzle(unsigned components)170 panfrost_get_default_swizzle(unsigned components)
171 {
172         switch (components) {
173         case 1:
174                 return PAN_V6_SWIZZLE(R, 0, 0, 1);
175         case 2:
176                 return PAN_V6_SWIZZLE(R, G, 0, 1);
177         case 3:
178                 return PAN_V6_SWIZZLE(R, G, B, 1);
179         case 4:
180                 return PAN_V6_SWIZZLE(R, G, B, A);
181         default:
182                 unreachable("Invalid number of components");
183         }
184 }
185 
186 static inline unsigned
panfrost_bifrost_swizzle(unsigned components)187 panfrost_bifrost_swizzle(unsigned components)
188 {
189         /* Set all components to 0 and force w if needed */
190         return components < 4 ? 0x10 : 0x00;
191 }
192 
193 enum mali_format
194 panfrost_format_to_bifrost_blend(const struct util_format_description *desc, bool dither);
195 
196 struct pan_pool;
197 struct pan_scoreboard;
198 
199 void
200 panfrost_init_blit_shaders(struct panfrost_device *dev);
201 
202 void
203 panfrost_load_midg(
204                 struct pan_pool *pool,
205                 struct pan_scoreboard *scoreboard,
206                 mali_ptr blend_shader,
207                 mali_ptr fbd,
208                 mali_ptr coordinates, unsigned vertex_count,
209                 struct pan_image *image,
210                 unsigned loc);
211 
212 void
213 panfrost_load_bifrost(struct pan_pool *pool,
214                       struct pan_scoreboard *scoreboard,
215                       mali_ptr blend_shader,
216                       mali_ptr thread_storage,
217                       mali_ptr tiler,
218                       mali_ptr coordinates, unsigned vertex_count,
219                       struct pan_image *image,
220                       unsigned loc);
221 
222 /* DRM modifier helper */
223 
224 #define drm_is_afbc(mod) \
225         ((mod >> 52) == (DRM_FORMAT_MOD_ARM_TYPE_AFBC | \
226                 (DRM_FORMAT_MOD_VENDOR_ARM << 4)))
227 
228 #endif
229