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
;
61 /* Translate a Gallium swizzle quad to a 12-bit Mali swizzle code */
64 panfrost_translate_swizzle_4(const unsigned char swizzle
[4])
68 for (unsigned i
= 0; i
< 4; ++i
) {
69 unsigned translated
= panfrost_translate_swizzle(swizzle
[i
]);
70 out
|= (translated
<< (3*i
));
77 panfrost_get_default_swizzle(unsigned components
)
79 unsigned char default_swizzles
[4][4] = {
80 {PIPE_SWIZZLE_X
, PIPE_SWIZZLE_0
, PIPE_SWIZZLE_0
, PIPE_SWIZZLE_1
},
81 {PIPE_SWIZZLE_X
, PIPE_SWIZZLE_Y
, PIPE_SWIZZLE_0
, PIPE_SWIZZLE_1
},
82 {PIPE_SWIZZLE_X
, PIPE_SWIZZLE_Y
, PIPE_SWIZZLE_Z
, PIPE_SWIZZLE_1
},
83 {PIPE_SWIZZLE_X
, PIPE_SWIZZLE_Y
, PIPE_SWIZZLE_Z
, PIPE_SWIZZLE_W
},
86 assert(components
>= 1 && components
<= 4);
87 return panfrost_translate_swizzle_4(default_swizzles
[components
- 1]);
91 panfrost_translate_channel_width(unsigned size
)
95 return MALI_CHANNEL_4
;
97 return MALI_CHANNEL_8
;
99 return MALI_CHANNEL_16
;
101 return MALI_CHANNEL_32
;
103 fprintf(stderr
, "Unknown width %d\n", size
);
110 panfrost_translate_channel_type(unsigned type
, unsigned size
, bool norm
) {
112 case UTIL_FORMAT_TYPE_UNSIGNED
:
113 return norm
? MALI_FORMAT_UNORM
: MALI_FORMAT_UINT
;
115 case UTIL_FORMAT_TYPE_SIGNED
:
116 return norm
? MALI_FORMAT_SNORM
: MALI_FORMAT_SINT
;
118 case UTIL_FORMAT_TYPE_FLOAT
:
120 /* With FLOAT, fp16 */
121 return MALI_FORMAT_SINT
;
122 } else if (size
== 32) {
123 /* With FLOAT< fp32 */
124 return MALI_FORMAT_UNORM
;
136 /* Constructs a mali_format satisfying the specified Gallium format
140 panfrost_find_format(const struct util_format_description
*desc
)
142 /* Find first non-VOID channel */
143 struct util_format_channel_description chan
= desc
->channel
[0];
145 for (unsigned c
= 0; c
< 4; ++c
) {
146 if (desc
->channel
[c
].type
== UTIL_FORMAT_TYPE_VOID
)
149 chan
= desc
->channel
[c
];
153 /* Check for special formats */
154 switch (desc
->format
) {
155 case PIPE_FORMAT_YV12
:
156 case PIPE_FORMAT_YV16
:
157 case PIPE_FORMAT_IYUV
:
158 case PIPE_FORMAT_NV21
:
159 fprintf(stderr
, "YUV format type %s (%d) is not yet supported, but it's probably close to NV12!\n", desc
->name
, desc
->format
);
163 case PIPE_FORMAT_NV12
:
166 case PIPE_FORMAT_R10G10B10X2_UNORM
:
167 case PIPE_FORMAT_B10G10R10X2_UNORM
:
168 case PIPE_FORMAT_R10G10B10A2_UNORM
:
169 case PIPE_FORMAT_B10G10R10A2_UNORM
:
170 return MALI_RGB10_A2_UNORM
;
172 case PIPE_FORMAT_R10G10B10X2_SNORM
:
173 case PIPE_FORMAT_R10G10B10A2_SNORM
:
174 case PIPE_FORMAT_B10G10R10A2_SNORM
:
175 return MALI_RGB10_A2_SNORM
;
177 case PIPE_FORMAT_R10G10B10A2_UINT
:
178 case PIPE_FORMAT_B10G10R10A2_UINT
:
179 return MALI_RGB10_A2UI
;
181 /* TODO: ZS isn't really special case */
182 case PIPE_FORMAT_Z32_UNORM
:
183 return MALI_Z32_UNORM
;
186 /* Fallthrough to default */
190 /* Formats must match in channel count */
191 assert(desc
->nr_channels
>= 1 && desc
->nr_channels
<= 4);
192 unsigned format
= MALI_NR_CHANNELS(desc
->nr_channels
);
195 case UTIL_FORMAT_TYPE_UNSIGNED
:
196 case UTIL_FORMAT_TYPE_SIGNED
:
197 case UTIL_FORMAT_TYPE_FIXED
:
199 format
|= panfrost_translate_channel_width(chan
.size
);
202 format
|= panfrost_translate_channel_type(chan
.type
, chan
.size
, chan
.normalized
);
205 case UTIL_FORMAT_TYPE_FLOAT
:
206 /* Float formats use a special width and encode width
209 format
|= MALI_CHANNEL_FLOAT
;
210 format
|= panfrost_translate_channel_type(chan
.type
, chan
.size
, chan
.normalized
);
214 fprintf(stderr
, "Unknown format type in %s\n", desc
->name
);
219 return (enum mali_format
) format
;