lima,panfrost: Move lima_tiling.c/h to /src/panfrost
[mesa.git] / src / gallium / drivers / panfrost / pan_format.c
1 /*
2 * © Copyright 2018 Alyssa Rosenzweig
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * SOFTWARE.
22 *
23 */
24
25 #include "pan_format.h"
26
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);
29
30 /* Construct a default swizzle based on the number of components */
31
32 static unsigned
33 panfrost_translate_swizzle(enum pipe_swizzle s)
34 {
35 switch (s) {
36 case PIPE_SWIZZLE_X:
37 return MALI_CHANNEL_RED;
38
39 case PIPE_SWIZZLE_Y:
40 return MALI_CHANNEL_GREEN;
41
42 case PIPE_SWIZZLE_Z:
43 return MALI_CHANNEL_BLUE;
44
45 case PIPE_SWIZZLE_W:
46 return MALI_CHANNEL_ALPHA;
47
48 case PIPE_SWIZZLE_0:
49 case PIPE_SWIZZLE_NONE:
50 return MALI_CHANNEL_ZERO;
51
52 case PIPE_SWIZZLE_1:
53 return MALI_CHANNEL_ONE;
54
55 default:
56 unreachable("INvalid swizzle");
57 }
58 }
59
60 /* Translate a Gallium swizzle quad to a 12-bit Mali swizzle code */
61
62 unsigned
63 panfrost_translate_swizzle_4(const unsigned char swizzle[4])
64 {
65 unsigned out = 0;
66
67 for (unsigned i = 0; i < 4; ++i) {
68 unsigned translated = panfrost_translate_swizzle(swizzle[i]);
69 out |= (translated << (3*i));
70 }
71
72 return out;
73 }
74
75 unsigned
76 panfrost_get_default_swizzle(unsigned components)
77 {
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},
83 };
84
85 assert(components >= 1 && components <= 4);
86 return panfrost_translate_swizzle_4(default_swizzles[components - 1]);
87 }
88
89 static unsigned
90 panfrost_translate_channel_width(unsigned size)
91 {
92 switch (size) {
93 case 4:
94 return MALI_CHANNEL_4;
95 case 8:
96 return MALI_CHANNEL_8;
97 case 16:
98 return MALI_CHANNEL_16;
99 case 32:
100 return MALI_CHANNEL_32;
101 default:
102 unreachable("Invalid width");
103 }
104 }
105
106 static unsigned
107 panfrost_translate_channel_type(unsigned type, unsigned size, bool norm) {
108 switch (type) {
109 case UTIL_FORMAT_TYPE_UNSIGNED:
110 return norm ? MALI_FORMAT_UNORM : MALI_FORMAT_UINT;
111
112 case UTIL_FORMAT_TYPE_SIGNED:
113 return norm ? MALI_FORMAT_SNORM : MALI_FORMAT_SINT;
114
115 case UTIL_FORMAT_TYPE_FLOAT:
116 if (size == 16) {
117 /* With FLOAT, fp16 */
118 return MALI_FORMAT_SINT;
119 } else if (size == 32) {
120 /* With FLOAT< fp32 */
121 return MALI_FORMAT_UNORM;
122 } else {
123 assert(0);
124 return 0;
125 }
126
127 default:
128 unreachable("Invalid type");
129 }
130 }
131
132 /* Constructs a mali_format satisfying the specified Gallium format
133 * description */
134
135 enum mali_format
136 panfrost_find_format(const struct util_format_description *desc)
137 {
138 /* Find first non-VOID channel */
139 struct util_format_channel_description chan = desc->channel[0];
140
141 for (unsigned c = 0; c < 4; ++c) {
142 if (desc->channel[c].type == UTIL_FORMAT_TYPE_VOID)
143 continue;
144
145 chan = desc->channel[c];
146 break;
147 }
148
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);
156 assert(0);
157 break;
158
159 case PIPE_FORMAT_NV12:
160 return MALI_NV12;
161
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;
167
168 case PIPE_FORMAT_R10G10B10X2_SNORM:
169 case PIPE_FORMAT_R10G10B10A2_SNORM:
170 case PIPE_FORMAT_B10G10R10A2_SNORM:
171 return MALI_RGB10_A2_SNORM;
172
173 case PIPE_FORMAT_R10G10B10A2_UINT:
174 case PIPE_FORMAT_B10G10R10A2_UINT:
175 return MALI_RGB10_A2UI;
176
177 /* TODO: ZS isn't really special case */
178 case PIPE_FORMAT_Z32_UNORM:
179 return MALI_Z32_UNORM;
180
181 case PIPE_FORMAT_B5G6R5_UNORM:
182 return MALI_RGB565;
183
184 case PIPE_FORMAT_B5G5R5A1_UNORM:
185 return MALI_RGB5_A1_UNORM;
186
187 case PIPE_FORMAT_A1B5G5R5_UNORM:
188 case PIPE_FORMAT_X1B5G5R5_UNORM:
189 /* Not supported - this is backwards from OpenGL! */
190 assert(0);
191 break;
192
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;
201
202 default:
203 /* Fallthrough to default */
204 break;
205 }
206
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);
210
211 switch (chan.type) {
212 case UTIL_FORMAT_TYPE_UNSIGNED:
213 case UTIL_FORMAT_TYPE_SIGNED:
214 case UTIL_FORMAT_TYPE_FIXED:
215 /* Channel width */
216 format |= panfrost_translate_channel_width(chan.size);
217
218 /* Channel type */
219 format |= panfrost_translate_channel_type(chan.type, chan.size, chan.normalized);
220 break;
221
222 case UTIL_FORMAT_TYPE_FLOAT:
223 /* Float formats use a special width and encode width
224 * with type mixed */
225
226 format |= MALI_CHANNEL_FLOAT;
227 format |= panfrost_translate_channel_type(chan.type, chan.size, chan.normalized);
228 break;
229
230 default:
231 unreachable("Invalid format type");
232 }
233
234 return (enum mali_format) format;
235 }
236
237