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
) {
109 case UTIL_FORMAT_TYPE_UNSIGNED
:
110 return norm
? MALI_FORMAT_UNORM
: MALI_FORMAT_UINT
;
112 case UTIL_FORMAT_TYPE_SIGNED
:
113 return norm
? MALI_FORMAT_SNORM
: MALI_FORMAT_SINT
;
115 case UTIL_FORMAT_TYPE_FLOAT
:
117 /* With FLOAT, fp16 */
118 return MALI_FORMAT_SINT
;
119 } else if (size
== 32) {
120 /* With FLOAT< fp32 */
121 return MALI_FORMAT_UNORM
;
128 unreachable("Invalid type");
132 /* Constructs a mali_format satisfying the specified Gallium format
136 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
) {
142 if (desc
->channel
[c
].type
== UTIL_FORMAT_TYPE_VOID
)
145 chan
= desc
->channel
[c
];
149 /* Check for special formats */
150 switch (desc
->format
) {
151 case PIPE_FORMAT_YV12
:
152 case PIPE_FORMAT_YV16
:
153 case PIPE_FORMAT_IYUV
:
154 case PIPE_FORMAT_NV21
:
155 fprintf(stderr
, "YUV format type %s (%d) is not yet supported, but it's probably close to NV12!\n", desc
->name
, desc
->format
);
159 case PIPE_FORMAT_NV12
:
162 case PIPE_FORMAT_R10G10B10X2_UNORM
:
163 case PIPE_FORMAT_B10G10R10X2_UNORM
:
164 case PIPE_FORMAT_R10G10B10A2_UNORM
:
165 case PIPE_FORMAT_B10G10R10A2_UNORM
:
166 return MALI_RGB10_A2_UNORM
;
168 case PIPE_FORMAT_R10G10B10X2_SNORM
:
169 case PIPE_FORMAT_R10G10B10A2_SNORM
:
170 case PIPE_FORMAT_B10G10R10A2_SNORM
:
171 return MALI_RGB10_A2_SNORM
;
173 case PIPE_FORMAT_R10G10B10A2_UINT
:
174 case PIPE_FORMAT_B10G10R10A2_UINT
:
175 return MALI_RGB10_A2UI
;
177 /* TODO: ZS isn't really special case */
178 case PIPE_FORMAT_Z32_UNORM
:
179 return MALI_Z32_UNORM
;
181 case PIPE_FORMAT_B5G6R5_UNORM
:
184 case PIPE_FORMAT_B5G5R5A1_UNORM
:
185 return MALI_RGB5_A1_UNORM
;
187 case PIPE_FORMAT_A1B5G5R5_UNORM
:
188 case PIPE_FORMAT_X1B5G5R5_UNORM
:
189 /* Not supported - this is backwards from OpenGL! */
193 case PIPE_FORMAT_R32_FIXED
:
194 return MALI_R32_FIXED
;
195 case PIPE_FORMAT_R32G32_FIXED
:
196 return MALI_RG32_FIXED
;
197 case PIPE_FORMAT_R32G32B32_FIXED
:
198 return MALI_RGB32_FIXED
;
199 case PIPE_FORMAT_R32G32B32A32_FIXED
:
200 return MALI_RGBA32_FIXED
;
203 /* Fallthrough to default */
207 /* Formats must match in channel count */
208 assert(desc
->nr_channels
>= 1 && desc
->nr_channels
<= 4);
209 unsigned format
= MALI_NR_CHANNELS(desc
->nr_channels
);
212 case UTIL_FORMAT_TYPE_UNSIGNED
:
213 case UTIL_FORMAT_TYPE_SIGNED
:
214 case UTIL_FORMAT_TYPE_FIXED
:
216 format
|= panfrost_translate_channel_width(chan
.size
);
219 format
|= panfrost_translate_channel_type(chan
.type
, chan
.size
, chan
.normalized
);
222 case UTIL_FORMAT_TYPE_FLOAT
:
223 /* Float formats use a special width and encode width
226 format
|= MALI_CHANNEL_FLOAT
;
227 format
|= panfrost_translate_channel_type(chan
.type
, chan
.size
, chan
.normalized
);
231 unreachable("Invalid format type");
234 return (enum mali_format
) format
;