DRI2: report swap events correctly in direct rendered case
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_format_aos.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * AoS pixel format manipulation.
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
35
36 #include "util/u_cpu_detect.h"
37 #include "util/u_format.h"
38
39 #include "lp_bld_type.h"
40 #include "lp_bld_const.h"
41 #include "lp_bld_swizzle.h"
42 #include "lp_bld_format.h"
43
44
45 /**
46 * Unpack a single pixel into its RGBA components.
47 *
48 * @param packed integer.
49 *
50 * @return RGBA in a 4 floats vector.
51 *
52 * XXX: This is mostly for reference and testing -- operating a single pixel at
53 * a time is rarely if ever needed.
54 */
55 LLVMValueRef
56 lp_build_unpack_rgba_aos(LLVMBuilderRef builder,
57 const struct util_format_description *desc,
58 LLVMValueRef packed)
59 {
60 LLVMTypeRef type;
61 LLVMValueRef shifted, casted, scaled, masked;
62 LLVMValueRef shifts[4];
63 LLVMValueRef masks[4];
64 LLVMValueRef scales[4];
65 LLVMValueRef swizzles[4];
66 LLVMValueRef aux[4];
67 bool normalized;
68 int empty_channel;
69 unsigned shift;
70 unsigned i;
71
72 /* FIXME: Support more formats */
73 assert(desc->layout == UTIL_FORMAT_LAYOUT_PLAIN);
74 assert(desc->block.width == 1);
75 assert(desc->block.height == 1);
76 assert(desc->block.bits <= 32);
77
78 type = LLVMIntType(desc->block.bits);
79
80 /* Do the intermediate integer computations with 32bit integers since it
81 * matches floating point size */
82 if (desc->block.bits < 32)
83 packed = LLVMBuildZExt(builder, packed, LLVMInt32Type(), "");
84
85 /* Broadcast the packed value to all four channels */
86 packed = LLVMBuildInsertElement(builder,
87 LLVMGetUndef(LLVMVectorType(LLVMInt32Type(), 4)),
88 packed,
89 LLVMConstNull(LLVMInt32Type()),
90 "");
91 packed = LLVMBuildShuffleVector(builder,
92 packed,
93 LLVMGetUndef(LLVMVectorType(LLVMInt32Type(), 4)),
94 LLVMConstNull(LLVMVectorType(LLVMInt32Type(), 4)),
95 "");
96
97 /* Initialize vector constants */
98 normalized = FALSE;
99 empty_channel = -1;
100 shift = 0;
101 for (i = 0; i < 4; ++i) {
102 unsigned bits = desc->channel[i].size;
103
104 if (desc->channel[i].type == UTIL_FORMAT_TYPE_VOID) {
105 shifts[i] = LLVMGetUndef(LLVMInt32Type());
106 masks[i] = LLVMConstNull(LLVMInt32Type());
107 scales[i] = LLVMConstNull(LLVMFloatType());
108 empty_channel = i;
109 }
110 else {
111 unsigned mask = (1 << bits) - 1;
112
113 assert(desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED);
114 assert(bits < 32);
115
116 shifts[i] = LLVMConstInt(LLVMInt32Type(), shift, 0);
117 masks[i] = LLVMConstInt(LLVMInt32Type(), mask, 0);
118
119 if (desc->channel[i].normalized) {
120 scales[i] = LLVMConstReal(LLVMFloatType(), 1.0/mask);
121 normalized = TRUE;
122 }
123 else
124 scales[i] = LLVMConstReal(LLVMFloatType(), 1.0);
125 }
126
127 shift += bits;
128 }
129
130 shifted = LLVMBuildLShr(builder, packed, LLVMConstVector(shifts, 4), "");
131 masked = LLVMBuildAnd(builder, shifted, LLVMConstVector(masks, 4), "");
132 /* UIToFP can't be expressed in SSE2 */
133 casted = LLVMBuildSIToFP(builder, masked, LLVMVectorType(LLVMFloatType(), 4), "");
134
135 if (normalized)
136 scaled = LLVMBuildMul(builder, casted, LLVMConstVector(scales, 4), "");
137 else
138 scaled = casted;
139
140 for (i = 0; i < 4; ++i)
141 aux[i] = LLVMGetUndef(LLVMFloatType());
142
143 for (i = 0; i < 4; ++i) {
144 enum util_format_swizzle swizzle = desc->swizzle[i];
145
146 switch (swizzle) {
147 case UTIL_FORMAT_SWIZZLE_X:
148 case UTIL_FORMAT_SWIZZLE_Y:
149 case UTIL_FORMAT_SWIZZLE_Z:
150 case UTIL_FORMAT_SWIZZLE_W:
151 swizzles[i] = LLVMConstInt(LLVMInt32Type(), swizzle, 0);
152 break;
153 case UTIL_FORMAT_SWIZZLE_0:
154 assert(empty_channel >= 0);
155 swizzles[i] = LLVMConstInt(LLVMInt32Type(), empty_channel, 0);
156 break;
157 case UTIL_FORMAT_SWIZZLE_1:
158 swizzles[i] = LLVMConstInt(LLVMInt32Type(), 4, 0);
159 aux[0] = LLVMConstReal(LLVMFloatType(), 1.0);
160 break;
161 case UTIL_FORMAT_SWIZZLE_NONE:
162 swizzles[i] = LLVMGetUndef(LLVMFloatType());
163 assert(0);
164 break;
165 }
166 }
167
168 return LLVMBuildShuffleVector(builder, scaled, LLVMConstVector(aux, 4), LLVMConstVector(swizzles, 4), "");
169 }
170
171
172 /**
173 * Take a vector with packed pixels and unpack into a rgba8 vector.
174 *
175 * Formats with bit depth smaller than 32bits are accepted, but they must be
176 * padded to 32bits.
177 */
178 LLVMValueRef
179 lp_build_unpack_rgba8_aos(LLVMBuilderRef builder,
180 const struct util_format_description *desc,
181 struct lp_type type,
182 LLVMValueRef packed)
183 {
184 struct lp_build_context bld;
185 bool rgba8;
186 LLVMValueRef res;
187 unsigned i;
188
189 lp_build_context_init(&bld, builder, type);
190
191 /* FIXME: Support more formats */
192 assert(desc->layout == UTIL_FORMAT_LAYOUT_PLAIN);
193 assert(desc->block.width == 1);
194 assert(desc->block.height == 1);
195 assert(desc->block.bits <= 32);
196
197 assert(!type.floating);
198 assert(!type.fixed);
199 assert(type.norm);
200 assert(type.width == 8);
201 assert(type.length % 4 == 0);
202
203 rgba8 = TRUE;
204 for(i = 0; i < 4; ++i) {
205 assert(desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED ||
206 desc->channel[i].type == UTIL_FORMAT_TYPE_VOID);
207 if(desc->channel[0].size != 8)
208 rgba8 = FALSE;
209 }
210
211 if(rgba8) {
212 /*
213 * The pixel is already in a rgba8 format variant. All it is necessary
214 * is to swizzle the channels.
215 */
216
217 unsigned char swizzles[4];
218 boolean zeros[4]; /* bitwise AND mask */
219 boolean ones[4]; /* bitwise OR mask */
220 boolean swizzles_needed = FALSE;
221 boolean zeros_needed = FALSE;
222 boolean ones_needed = FALSE;
223
224 for(i = 0; i < 4; ++i) {
225 enum util_format_swizzle swizzle = desc->swizzle[i];
226
227 /* Initialize with the no-op case */
228 swizzles[i] = util_cpu_caps.little_endian ? 3 - i : i;
229 zeros[i] = TRUE;
230 ones[i] = FALSE;
231
232 switch (swizzle) {
233 case UTIL_FORMAT_SWIZZLE_X:
234 case UTIL_FORMAT_SWIZZLE_Y:
235 case UTIL_FORMAT_SWIZZLE_Z:
236 case UTIL_FORMAT_SWIZZLE_W:
237 if(swizzle != swizzles[i]) {
238 swizzles[i] = swizzle;
239 swizzles_needed = TRUE;
240 }
241 break;
242 case UTIL_FORMAT_SWIZZLE_0:
243 zeros[i] = FALSE;
244 zeros_needed = TRUE;
245 break;
246 case UTIL_FORMAT_SWIZZLE_1:
247 ones[i] = TRUE;
248 ones_needed = TRUE;
249 break;
250 case UTIL_FORMAT_SWIZZLE_NONE:
251 assert(0);
252 break;
253 }
254 }
255
256 res = packed;
257
258 if(swizzles_needed)
259 res = lp_build_swizzle1_aos(&bld, res, swizzles);
260
261 if(zeros_needed) {
262 /* Mask out zero channels */
263 LLVMValueRef mask = lp_build_const_mask_aos(type, zeros);
264 res = LLVMBuildAnd(builder, res, mask, "");
265 }
266
267 if(ones_needed) {
268 /* Or one channels */
269 LLVMValueRef mask = lp_build_const_mask_aos(type, ones);
270 res = LLVMBuildOr(builder, res, mask, "");
271 }
272 }
273 else {
274 /* FIXME */
275 assert(0);
276 res = lp_build_undef(type);
277 }
278
279 return res;
280 }
281
282
283 /**
284 * Pack a single pixel.
285 *
286 * @param rgba 4 float vector with the unpacked components.
287 *
288 * XXX: This is mostly for reference and testing -- operating a single pixel at
289 * a time is rarely if ever needed.
290 */
291 LLVMValueRef
292 lp_build_pack_rgba_aos(LLVMBuilderRef builder,
293 const struct util_format_description *desc,
294 LLVMValueRef rgba)
295 {
296 LLVMTypeRef type;
297 LLVMValueRef packed = NULL;
298 LLVMValueRef swizzles[4];
299 LLVMValueRef shifted, casted, scaled, unswizzled;
300 LLVMValueRef shifts[4];
301 LLVMValueRef scales[4];
302 bool normalized;
303 unsigned shift;
304 unsigned i, j;
305
306 assert(desc->layout == UTIL_FORMAT_LAYOUT_PLAIN);
307 assert(desc->block.width == 1);
308 assert(desc->block.height == 1);
309
310 type = LLVMIntType(desc->block.bits);
311
312 /* Unswizzle the color components into the source vector. */
313 for (i = 0; i < 4; ++i) {
314 for (j = 0; j < 4; ++j) {
315 if (desc->swizzle[j] == i)
316 break;
317 }
318 if (j < 4)
319 swizzles[i] = LLVMConstInt(LLVMInt32Type(), j, 0);
320 else
321 swizzles[i] = LLVMGetUndef(LLVMInt32Type());
322 }
323
324 unswizzled = LLVMBuildShuffleVector(builder, rgba,
325 LLVMGetUndef(LLVMVectorType(LLVMFloatType(), 4)),
326 LLVMConstVector(swizzles, 4), "");
327
328 normalized = FALSE;
329 shift = 0;
330 for (i = 0; i < 4; ++i) {
331 unsigned bits = desc->channel[i].size;
332
333 if (desc->channel[i].type == UTIL_FORMAT_TYPE_VOID) {
334 shifts[i] = LLVMGetUndef(LLVMInt32Type());
335 scales[i] = LLVMGetUndef(LLVMFloatType());
336 }
337 else {
338 unsigned mask = (1 << bits) - 1;
339
340 assert(desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED);
341 assert(bits < 32);
342
343 shifts[i] = LLVMConstInt(LLVMInt32Type(), shift, 0);
344
345 if (desc->channel[i].normalized) {
346 scales[i] = LLVMConstReal(LLVMFloatType(), mask);
347 normalized = TRUE;
348 }
349 else
350 scales[i] = LLVMConstReal(LLVMFloatType(), 1.0);
351 }
352
353 shift += bits;
354 }
355
356 if (normalized)
357 scaled = LLVMBuildMul(builder, unswizzled, LLVMConstVector(scales, 4), "");
358 else
359 scaled = unswizzled;
360
361 casted = LLVMBuildFPToSI(builder, scaled, LLVMVectorType(LLVMInt32Type(), 4), "");
362
363 shifted = LLVMBuildShl(builder, casted, LLVMConstVector(shifts, 4), "");
364
365 /* Bitwise or all components */
366 for (i = 0; i < 4; ++i) {
367 if (desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED) {
368 LLVMValueRef component = LLVMBuildExtractElement(builder, shifted, LLVMConstInt(LLVMInt32Type(), i, 0), "");
369 if (packed)
370 packed = LLVMBuildOr(builder, packed, component, "");
371 else
372 packed = component;
373 }
374 }
375
376 if (!packed)
377 packed = LLVMGetUndef(LLVMInt32Type());
378
379 if (desc->block.bits < 32)
380 packed = LLVMBuildTrunc(builder, packed, type, "");
381
382 return packed;
383 }