2 * © Copyright 2018 Alyssa Rosenzweig
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:
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
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #include "pan_format.h"
27 /* From panwrap/panwrap-decoder, but we don't want to bring in all those headers */
28 char *panwrap_format_name(enum mali_format format
);
30 /* Construct a default swizzle based on the number of components */
33 panfrost_translate_swizzle(enum pipe_swizzle s
)
37 return MALI_CHANNEL_RED
;
40 return MALI_CHANNEL_GREEN
;
43 return MALI_CHANNEL_BLUE
;
46 return MALI_CHANNEL_ALPHA
;
49 case PIPE_SWIZZLE_NONE
:
50 return MALI_CHANNEL_ZERO
;
53 return MALI_CHANNEL_ONE
;
56 unreachable("INvalid swizzle");
60 /* Translate a Gallium swizzle quad to a 12-bit Mali swizzle code */
63 panfrost_translate_swizzle_4(const unsigned char swizzle
[4])
67 for (unsigned i
= 0; i
< 4; ++i
) {
68 unsigned translated
= panfrost_translate_swizzle(swizzle
[i
]);
69 out
|= (translated
<< (3*i
));
76 panfrost_get_default_swizzle(unsigned components
)
78 unsigned char default_swizzles
[4][4] = {
79 {PIPE_SWIZZLE_X
, PIPE_SWIZZLE_0
, PIPE_SWIZZLE_0
, PIPE_SWIZZLE_1
},
80 {PIPE_SWIZZLE_X
, PIPE_SWIZZLE_Y
, PIPE_SWIZZLE_0
, PIPE_SWIZZLE_1
},
81 {PIPE_SWIZZLE_X
, PIPE_SWIZZLE_Y
, PIPE_SWIZZLE_Z
, PIPE_SWIZZLE_1
},
82 {PIPE_SWIZZLE_X
, PIPE_SWIZZLE_Y
, PIPE_SWIZZLE_Z
, PIPE_SWIZZLE_W
},
85 assert(components
>= 1 && components
<= 4);
86 return panfrost_translate_swizzle_4(default_swizzles
[components
- 1]);
90 panfrost_translate_channel_width(unsigned size
)
94 return MALI_CHANNEL_4
;
96 return MALI_CHANNEL_8
;
98 return MALI_CHANNEL_16
;
100 return MALI_CHANNEL_32
;
102 unreachable("Invalid width");
107 panfrost_translate_channel_type(unsigned type
, unsigned size
, bool norm
)
110 case UTIL_FORMAT_TYPE_UNSIGNED
:
111 return norm
? MALI_FORMAT_UNORM
: MALI_FORMAT_UINT
;
113 case UTIL_FORMAT_TYPE_SIGNED
:
114 return norm
? MALI_FORMAT_SNORM
: MALI_FORMAT_SINT
;
116 case UTIL_FORMAT_TYPE_FLOAT
:
118 /* With FLOAT, fp16 */
119 return MALI_FORMAT_SINT
;
120 } else if (size
== 32) {
121 /* With FLOAT< fp32 */
122 return MALI_FORMAT_UNORM
;
129 unreachable("Invalid type");
133 /* Constructs a mali_format satisfying the specified Gallium format
137 panfrost_find_format(const struct util_format_description
*desc
) {
138 /* Find first non-VOID channel */
139 struct util_format_channel_description chan
= desc
->channel
[0];
141 for (unsigned c
= 0; c
< 4; ++c
)
143 if (desc
->channel
[c
].type
== UTIL_FORMAT_TYPE_VOID
)
146 chan
= desc
->channel
[c
];
150 /* Check for special formats */
151 switch (desc
->format
)
153 case PIPE_FORMAT_YV12
:
154 case PIPE_FORMAT_YV16
:
155 case PIPE_FORMAT_IYUV
:
156 case PIPE_FORMAT_NV21
:
157 fprintf(stderr
, "YUV format type %s (%d) is not yet supported, but it's probably close to NV12!\n", desc
->name
, desc
->format
);
161 case PIPE_FORMAT_NV12
:
164 case PIPE_FORMAT_R10G10B10X2_UNORM
:
165 case PIPE_FORMAT_B10G10R10X2_UNORM
:
166 case PIPE_FORMAT_R10G10B10A2_UNORM
:
167 case PIPE_FORMAT_B10G10R10A2_UNORM
:
168 return MALI_RGB10_A2_UNORM
;
170 case PIPE_FORMAT_R10G10B10X2_SNORM
:
171 case PIPE_FORMAT_R10G10B10A2_SNORM
:
172 case PIPE_FORMAT_B10G10R10A2_SNORM
:
173 return MALI_RGB10_A2_SNORM
;
175 case PIPE_FORMAT_R10G10B10A2_UINT
:
176 case PIPE_FORMAT_B10G10R10A2_UINT
:
177 return MALI_RGB10_A2UI
;
179 case PIPE_FORMAT_R10G10B10A2_SSCALED
:
180 case PIPE_FORMAT_B10G10R10A2_SSCALED
:
181 return MALI_RGB10_A2I
;
183 case PIPE_FORMAT_Z32_UNORM
:
184 case PIPE_FORMAT_Z24X8_UNORM
:
185 return MALI_Z32_UNORM
;
187 case PIPE_FORMAT_B5G6R5_UNORM
:
190 case PIPE_FORMAT_B5G5R5A1_UNORM
:
191 return MALI_RGB5_A1_UNORM
;
193 case PIPE_FORMAT_A1B5G5R5_UNORM
:
194 case PIPE_FORMAT_X1B5G5R5_UNORM
:
195 /* Not supported - this is backwards from OpenGL! */
199 case PIPE_FORMAT_R32_FIXED
:
200 return MALI_R32_FIXED
;
201 case PIPE_FORMAT_R32G32_FIXED
:
202 return MALI_RG32_FIXED
;
203 case PIPE_FORMAT_R32G32B32_FIXED
:
204 return MALI_RGB32_FIXED
;
205 case PIPE_FORMAT_R32G32B32A32_FIXED
:
206 return MALI_RGBA32_FIXED
;
208 case PIPE_FORMAT_R11G11B10_FLOAT
:
209 return MALI_R11F_G11F_B10F
;
210 case PIPE_FORMAT_R9G9B9E5_FLOAT
:
211 return MALI_R9F_G9F_B9F_E5F
;
214 /* Fallthrough to default */
218 /* Formats must match in channel count */
219 assert(desc
->nr_channels
>= 1 && desc
->nr_channels
<= 4);
220 unsigned format
= MALI_NR_CHANNELS(desc
->nr_channels
);
224 case UTIL_FORMAT_TYPE_UNSIGNED
:
225 case UTIL_FORMAT_TYPE_SIGNED
:
226 case UTIL_FORMAT_TYPE_FIXED
:
228 format
|= panfrost_translate_channel_width(chan
.size
);
231 format
|= panfrost_translate_channel_type(chan
.type
, chan
.size
, chan
.normalized
);
234 case UTIL_FORMAT_TYPE_FLOAT
:
235 /* Float formats use a special width and encode width
238 format
|= MALI_CHANNEL_FLOAT
;
239 format
|= panfrost_translate_channel_type(chan
.type
, chan
.size
, chan
.normalized
);
243 printf("%s\n", util_format_name(desc
->format
));
244 unreachable("Invalid format type");
247 return (enum mali_format
) format
;