Debian Codegraph @

src/gallium/auxiliary/gallivm/lp_bld_format_s3tc.c

raw symbols SCIP
1 /**************************************************************************
2 *
3 * Copyright 2010-2018 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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 **************************************************************************/
27
28
29 /**
30 * @file
31 * s3tc pixel format manipulation.
32 *
33 * @author Roland Scheidegger <[email protected]>
34 */
35
36
37 #include <llvm/Config/llvm-config.h>
38
39 #include "util/format/u_format.h"
40 #include "util/u_math.h"
41 #include "util/u_cpu_detect.h"
42
43 #include "lp_bld_arit.h"
44 #include "lp_bld_type.h"
45 #include "lp_bld_const.h"
46 #include "lp_bld_conv.h"
47 #include "lp_bld_gather.h"
48 #include "lp_bld_format.h"
49 #include "lp_bld_logic.h"
50 #include "lp_bld_pack.h"
51 #include "lp_bld_flow.h"
52 #include "lp_bld_swizzle.h"
53 #include "lp_bld_init.h"
54 #include "lp_bld_debug.h"
55 #include "lp_bld_intr.h"
56
57
58 /**
59 * Reverse an interleave2_half
60 * (ie. pick every second element, independent lower/upper halfs)
61 * sse2 can only do that with 32bit (shufps) or larger elements
62 * natively. (Otherwise, and/pack (even) or shift/pack (odd)
63 * could be used, ideally llvm would do that for us.)
64 * XXX: Unfortunately, this does NOT translate to a shufps if those
65 * are int vectors (and casting will not help, llvm needs to recognize it
66 * as "real" float). Instead, llvm will use a pshufd/pshufd/punpcklqdq
67 * sequence which I'm pretty sure is a lot worse despite domain transition
68 * penalties with shufps (except maybe on Nehalem).
69 */
70 static LLVMValueRef
71 lp_build_uninterleave2_half(struct gallivm_state *gallivm,
72 struct lp_type type,
73 LLVMValueRef a,
74 LLVMValueRef b,
75 unsigned lo_hi)
76 {
77 LLVMValueRef shuffle, elems[LP_MAX_VECTOR_LENGTH];
78 unsigned i;
79
80 assert(type.length <= LP_MAX_VECTOR_LENGTH);
81 assert(lo_hi < 2);
82
83 if (type.length * type.width == 256) {
84 assert(type.length == 8);
85 assert(type.width == 32);
86 static const unsigned shufvals[8] = {0, 2, 8, 10, 4, 6, 12, 14};
87 for (i = 0; i < type.length; ++i) {
88 elems[i] = lp_build_const_int32(gallivm, shufvals[i] + lo_hi);
89 }
90 } else {
91 for (i = 0; i < type.length; ++i) {
92 elems[i] = lp_build_const_int32(gallivm, 2*i + lo_hi);
93 }
94 }
95
96 shuffle = LLVMConstVector(elems, type.length);
97
98 return LLVMBuildShuffleVector(gallivm->builder, a, b, shuffle, "");
99
100 }
101
102
103 /**
104 * Build shuffle for extending vectors.
105 */
106 static LLVMValueRef
107 lp_build_const_extend_shuffle(struct gallivm_state *gallivm,
108 unsigned n, unsigned length)
109 {
110 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
111 unsigned i;
112
113 assert(n <= length);
114 assert(length <= LP_MAX_VECTOR_LENGTH);
115
116 /* TODO: cache results in a static table */
117
118 for(i = 0; i < n; i++) {
119 elems[i] = lp_build_const_int32(gallivm, i);
120 }
121 for (i = n; i < length; i++) {
122 elems[i] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
123 }
124
125 return LLVMConstVector(elems, length);
126 }
127
128 static LLVMValueRef
129 lp_build_const_unpackx2_shuffle(struct gallivm_state *gallivm, unsigned n)
130 {
131 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
132 unsigned i, j;
133
134 assert(n <= LP_MAX_VECTOR_LENGTH);
135
136 /* TODO: cache results in a static table */
137
138 for(i = 0, j = 0; i < n; i += 2, ++j) {
139 elems[i + 0] = lp_build_const_int32(gallivm, 0 + j);
140 elems[i + 1] = lp_build_const_int32(gallivm, n + j);
141 elems[n + i + 0] = lp_build_const_int32(gallivm, 0 + n/2 + j);
142 elems[n + i + 1] = lp_build_const_int32(gallivm, n + n/2 + j);
143 }
144
145 return LLVMConstVector(elems, n * 2);
146 }
147
148 /*
149 * broadcast 1 element to all elements
150 */
151 static LLVMValueRef
152 lp_build_const_shuffle1(struct gallivm_state *gallivm,
153 unsigned index, unsigned n)
154 {
155 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
156 unsigned i;
157
158 assert(n <= LP_MAX_VECTOR_LENGTH);
159
160 /* TODO: cache results in a static table */
161
162 for (i = 0; i < n; i++) {
163 elems[i] = lp_build_const_int32(gallivm, index);
164 }
165
166 return LLVMConstVector(elems, n);
167 }
168
169 /*
170 * move 1 element to pos 0, rest undef
171 */
172 static LLVMValueRef
173 lp_build_shuffle1undef(struct gallivm_state *gallivm,
174 LLVMValueRef a, unsigned index, unsigned n)
175 {
176 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH], shuf;
177 unsigned i;
178
179 assert(n <= LP_MAX_VECTOR_LENGTH);
180
181 elems[0] = lp_build_const_int32(gallivm, index);
182
183 for (i = 1; i < n; i++) {
184 elems[i] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
185 }
186 shuf = LLVMConstVector(elems, n);
187
188 return LLVMBuildShuffleVector(gallivm->builder, a, a, shuf, "");
189 }
190
191 static bool
192 format_dxt1_variant(enum pipe_format format)
193 {
194 return format == PIPE_FORMAT_DXT1_RGB ||
195 format == PIPE_FORMAT_DXT1_RGBA ||
196 format == PIPE_FORMAT_DXT1_SRGB ||
197 format == PIPE_FORMAT_DXT1_SRGBA;
198
199 }
200
201 /**
202 * Gather elements from scatter positions in memory into vectors.
203 * This is customised for fetching texels from s3tc textures.
204 * For SSE, typical value is length=4.
205 *
206 * @param length length of the offsets
207 * @param colors the stored colors of the blocks will be extracted into this.
208 * @param codewords the codewords of the blocks will be extracted into this.
209 * @param alpha_lo used for storing lower 32bit of alpha components for dxt3/5
210 * @param alpha_hi used for storing higher 32bit of alpha components for dxt3/5
211 * @param base_ptr base pointer, should be a i8 pointer type.
212 * @param offsets vector with offsets
213 */
214 static void
215 lp_build_gather_s3tc(struct gallivm_state *gallivm,
216 unsigned length,
217 const struct util_format_description *format_desc,
218 LLVMValueRef *colors,
219 LLVMValueRef *codewords,
220 LLVMValueRef *alpha_lo,
221 LLVMValueRef *alpha_hi,
222 LLVMValueRef base_ptr,
223 LLVMValueRef offsets)
224 {
225 LLVMBuilderRef builder = gallivm->builder;
226 unsigned block_bits = format_desc->block.bits;
227 unsigned i;
228 LLVMValueRef elems[8];
229 LLVMTypeRef type32 = LLVMInt32TypeInContext(gallivm->context);
230 LLVMTypeRef type64 = LLVMInt64TypeInContext(gallivm->context);
231 LLVMTypeRef type32dxt;
232 struct lp_type lp_type32dxt;
233
234 memset(&lp_type32dxt, 0, sizeof lp_type32dxt);
235 lp_type32dxt.width = 32;
236 lp_type32dxt.length = block_bits / 32;
237 type32dxt = lp_build_vec_type(gallivm, lp_type32dxt);
238
239 assert(block_bits == 64 || block_bits == 128);
240 assert(length == 1 || length == 4 || length == 8);
241
242 for (i = 0; i < length; ++i) {
243 elems[i] = lp_build_gather_elem(gallivm, length,
244 block_bits, block_bits, true,
245 base_ptr, offsets, i, false);
246 elems[i] = LLVMBuildBitCast(builder, elems[i], type32dxt, "");
247 }
248 if (length == 1) {
249 LLVMValueRef elem = elems[0];
250 if (block_bits == 128) {
251 *alpha_lo = LLVMBuildExtractElement(builder, elem,
252 lp_build_const_int32(gallivm, 0), "");
253 *alpha_hi = LLVMBuildExtractElement(builder, elem,
254 lp_build_const_int32(gallivm, 1), "");
255 *colors = LLVMBuildExtractElement(builder, elem,
256 lp_build_const_int32(gallivm, 2), "");
257 *codewords = LLVMBuildExtractElement(builder, elem,
258 lp_build_const_int32(gallivm, 3), "");
259 }
260 else {
261 *alpha_lo = LLVMGetUndef(type32);
262 *alpha_hi = LLVMGetUndef(type32);
263 *colors = LLVMBuildExtractElement(builder, elem,
264 lp_build_const_int32(gallivm, 0), "");
265 *codewords = LLVMBuildExtractElement(builder, elem,
266 lp_build_const_int32(gallivm, 1), "");
267 }
268 }
269 else {
270 LLVMValueRef tmp[4], cc01, cc23;
271 struct lp_type lp_type32, lp_type64;
272 memset(&lp_type32, 0, sizeof lp_type32);
273 lp_type32.width = 32;
274 lp_type32.length = length;
275 memset(&lp_type64, 0, sizeof lp_type64);
276 lp_type64.width = 64;
277 lp_type64.length = length/2;
278
279 if (block_bits == 128) {
280 if (length == 8) {
281 for (i = 0; i < 4; ++i) {
282 tmp[0] = elems[i];
283 tmp[1] = elems[i+4];
284 elems[i] = lp_build_concat(gallivm, tmp, lp_type32dxt, 2);
285 }
286 }
287 lp_build_transpose_aos(gallivm, lp_type32, elems, tmp);
288 *colors = tmp[2];
289 *codewords = tmp[3];
290 *alpha_lo = tmp[0];
291 *alpha_hi = tmp[1];
292 } else {
293 LLVMTypeRef type64_vec = LLVMVectorType(type64, length/2);
294 LLVMTypeRef type32_vec = LLVMVectorType(type32, length);
295
296 for (i = 0; i < length; ++i) {
297 /* no-op shuffle */
298 elems[i] = LLVMBuildShuffleVector(builder, elems[i],
299 LLVMGetUndef(type32dxt),
300 lp_build_const_extend_shuffle(gallivm, 2, 4), "");
301 }
302 if (length == 8) {
303 struct lp_type lp_type32_4 = {0};
304 lp_type32_4.width = 32;
305 lp_type32_4.length = 4;
306 for (i = 0; i < 4; ++i) {
307 tmp[0] = elems[i];
308 tmp[1] = elems[i+4];
309 elems[i] = lp_build_concat(gallivm, tmp, lp_type32_4, 2);
310 }
311 }
312 cc01 = lp_build_interleave2_half(gallivm, lp_type32, elems[0], elems[1], 0);
313 cc23 = lp_build_interleave2_half(gallivm, lp_type32, elems[2], elems[3], 0);
314 cc01 = LLVMBuildBitCast(builder, cc01, type64_vec, "");
315 cc23 = LLVMBuildBitCast(builder, cc23, type64_vec, "");
316 *colors = lp_build_interleave2_half(gallivm, lp_type64, cc01, cc23, 0);
317 *codewords = lp_build_interleave2_half(gallivm, lp_type64, cc01, cc23, 1);
318 *colors = LLVMBuildBitCast(builder, *colors, type32_vec, "");
319 *codewords = LLVMBuildBitCast(builder, *codewords, type32_vec, "");
320 }
321 }
322 }
323
324 /** Convert from <n x i32> containing 2 x n rgb565 colors
325 * to 2 <n x i32> rgba8888 colors
326 * This is the most optimized version I can think of
327 * should be nearly as fast as decoding only one color
328 * NOTE: alpha channel will be set to 0
329 * @param colors is a <n x i32> vector containing the rgb565 colors
330 */
331 static void
332 color_expand2_565_to_8888(struct gallivm_state *gallivm,
333 unsigned n,
334 LLVMValueRef colors,
335 LLVMValueRef *color0,
336 LLVMValueRef *color1)
337 {
338 LLVMBuilderRef builder = gallivm->builder;
339 LLVMValueRef r, g, b, rblo, glo;
340 LLVMValueRef rgblomask, rb, rgb0, rgb1;
341 struct lp_type type, type16, type8;
342
343 assert(n > 1);
344
345 memset(&type, 0, sizeof type);
346 type.width = 32;
347 type.length = n;
348
349 memset(&type16, 0, sizeof type16);
350 type16.width = 16;
351 type16.length = 2 * n;
352
353 memset(&type8, 0, sizeof type8);
354 type8.width = 8;
355 type8.length = 4 * n;
356
357 rgblomask = lp_build_const_int_vec(gallivm, type16, 0x0707);
358 colors = LLVMBuildBitCast(builder, colors,
359 lp_build_vec_type(gallivm, type16), "");
360 /* move r into low 8 bits, b into high 8 bits, g into another reg (low bits)
361 * make sure low bits of r are zero - could use AND but requires constant */
362 r = LLVMBuildLShr(builder, colors, lp_build_const_int_vec(gallivm, type16, 11), "");
363 r = LLVMBuildShl(builder, r, lp_build_const_int_vec(gallivm, type16, 3), "");
364 b = LLVMBuildShl(builder, colors, lp_build_const_int_vec(gallivm, type16, 11), "");
365 rb = LLVMBuildOr(builder, r, b, "");
366 rblo = LLVMBuildLShr(builder, rb, lp_build_const_int_vec(gallivm, type16, 5), "");
367 /* don't have byte shift hence need mask */
368 rblo = LLVMBuildAnd(builder, rblo, rgblomask, "");
369 rb = LLVMBuildOr(builder, rb, rblo, "");
370
371 /* make sure low bits of g are zero */
372 g = LLVMBuildAnd(builder, colors, lp_build_const_int_vec(gallivm, type16, 0x07e0), "");
373 g = LLVMBuildLShr(builder, g, lp_build_const_int_vec(gallivm, type16, 3), "");
374 glo = LLVMBuildLShr(builder, g, lp_build_const_int_vec(gallivm, type16, 6), "");
375 g = LLVMBuildOr(builder, g, glo, "");
376
377 rb = LLVMBuildBitCast(builder, rb, lp_build_vec_type(gallivm, type8), "");
378 g = LLVMBuildBitCast(builder, g, lp_build_vec_type(gallivm, type8), "");
379 rgb0 = lp_build_interleave2_half(gallivm, type8, rb, g, 0);
380 rgb1 = lp_build_interleave2_half(gallivm, type8, rb, g, 1);
381
382 rgb0 = LLVMBuildBitCast(builder, rgb0, lp_build_vec_type(gallivm, type), "");
383 rgb1 = LLVMBuildBitCast(builder, rgb1, lp_build_vec_type(gallivm, type), "");
384
385 /* rgb0 is rgb00, rgb01, rgb10, rgb11
386 * instead of rgb00, rgb10, rgb20, rgb30 hence need reshuffle
387 * on x86 this _should_ just generate one shufps...
388 */
389 *color0 = lp_build_uninterleave2_half(gallivm, type, rgb0, rgb1, 0);
390 *color1 = lp_build_uninterleave2_half(gallivm, type, rgb0, rgb1, 1);
391 }
392
393
394 /** Convert from <n x i32> containing rgb565 colors
395 * (in first 16 bits) to <n x i32> rgba8888 colors
396 * bits 16-31 MBZ
397 * NOTE: alpha channel will be set to 0
398 * @param colors is a <n x i32> vector containing the rgb565 colors
399 */
400 static LLVMValueRef
401 color_expand_565_to_8888(struct gallivm_state *gallivm,
402 unsigned n,
403 LLVMValueRef colors)
404 {
405 LLVMBuilderRef builder = gallivm->builder;
406 LLVMValueRef rgba, r, g, b, rgblo, glo;
407 LLVMValueRef rbhimask, g6mask, rgblomask;
408 struct lp_type type;
409 memset(&type, 0, sizeof type);
410 type.width = 32;
411 type.length = n;
412
413 /* color expansion:
414 * first extract and shift colors into their final locations
415 * (high bits - low bits zero at this point)
416 * then replicate highest bits to the lowest bits
417 * note rb replication can be done in parallel but not g
418 * (different shift)
419 * r5mask = 0xf800, g6mask = 0x07e0, b5mask = 0x001f
420 * rhigh = 8, ghigh = 5, bhigh = 19
421 * rblow = 5, glow = 6
422 * rgblowmask = 0x00070307
423 * r = colors >> rhigh
424 * b = colors << bhigh
425 * g = (colors & g6mask) << ghigh
426 * rb = (r | b) rbhimask
427 * rbtmp = rb >> rblow
428 * gtmp = rb >> glow
429 * rbtmp = rbtmp | gtmp
430 * rbtmp = rbtmp & rgblowmask
431 * rgb = rb | g | rbtmp
432 */
433 g6mask = lp_build_const_int_vec(gallivm, type, 0x07e0);
434 rbhimask = lp_build_const_int_vec(gallivm, type, 0x00f800f8);
435 rgblomask = lp_build_const_int_vec(gallivm, type, 0x00070307);
436
437 r = LLVMBuildLShr(builder, colors, lp_build_const_int_vec(gallivm, type, 8), "");
438 b = LLVMBuildShl(builder, colors, lp_build_const_int_vec(gallivm, type, 19), "");
439 g = LLVMBuildAnd(builder, colors, g6mask, "");
440 g = LLVMBuildShl(builder, g, lp_build_const_int_vec(gallivm, type, 5), "");
441 rgba = LLVMBuildOr(builder, r, b, "");
442 rgba = LLVMBuildAnd(builder, rgba, rbhimask, "");
443 rgblo = LLVMBuildLShr(builder, rgba, lp_build_const_int_vec(gallivm, type, 5), "");
444 glo = LLVMBuildLShr(builder, g, lp_build_const_int_vec(gallivm, type, 6), "");
445 rgblo = LLVMBuildOr(builder, rgblo, glo, "");
446 rgblo = LLVMBuildAnd(builder, rgblo, rgblomask, "");
447 rgba = LLVMBuildOr(builder, rgba, g, "");
448 rgba = LLVMBuildOr(builder, rgba, rgblo, "");
449
450 return rgba;
451 }
452
453
454 /*
455 * Average two byte vectors. (Will always round up.)
456 */
457 static LLVMValueRef
458 lp_build_pavgb(struct lp_build_context *bld8,
459 LLVMValueRef v0,
460 LLVMValueRef v1)
461 {
462 struct gallivm_state *gallivm = bld8->gallivm;
463 LLVMBuilderRef builder = gallivm->builder;
464 assert(bld8->type.width == 8);
465 assert(bld8->type.length == 16 || bld8->type.length == 32);
466
467 /*
468 * Must match llvm's autoupgrade of pavg.b intrinsic to be useful.
469 * You better hope the backend code manages to detect the pattern, and
470 * the pattern doesn't change there...
471 */
472 struct lp_type type_ext = bld8->type;
473 LLVMTypeRef vec_type_ext;
474 LLVMValueRef res;
475 LLVMValueRef ext_one;
476 type_ext.width = 16;
477 vec_type_ext = lp_build_vec_type(gallivm, type_ext);
478 ext_one = lp_build_const_vec(gallivm, type_ext, 1);
479
480 v0 = LLVMBuildZExt(builder, v0, vec_type_ext, "");
481 v1 = LLVMBuildZExt(builder, v1, vec_type_ext, "");
482 res = LLVMBuildAdd(builder, v0, v1, "");
483 res = LLVMBuildAdd(builder, res, ext_one, "");
484 res = LLVMBuildLShr(builder, res, ext_one, "");
485 res = LLVMBuildTrunc(builder, res, bld8->vec_type, "");
486 return res;
487 }
488
489 /**
490 * Calculate 1/3(v1-v0) + v0
491 * and 2*1/3(v1-v0) + v0
492 */
493 static void
494 lp_build_lerp23(struct lp_build_context *bld,
495 LLVMValueRef v0,
496 LLVMValueRef v1,
497 LLVMValueRef *res0,
498 LLVMValueRef *res1)
499 {
500 struct gallivm_state *gallivm = bld->gallivm;
501 LLVMValueRef x, x_lo, x_hi, delta_lo, delta_hi;
502 LLVMValueRef mul_lo, mul_hi, v0_lo, v0_hi, v1_lo, v1_hi, tmp;
503 const struct lp_type type = bld->type;
504 LLVMBuilderRef builder = bld->gallivm->builder;
505 struct lp_type i16_type = lp_wider_type(type);
506 struct lp_build_context bld2;
507
508 assert(lp_check_value(type, v0));
509 assert(lp_check_value(type, v1));
510 assert(!type.floating && !type.fixed && !type.norm && type.width == 8);
511
512 lp_build_context_init(&bld2, gallivm, i16_type);
513 bld2.type.sign = true;
514 x = lp_build_const_int_vec(gallivm, bld->type, 255*1/3);
515
516 /* FIXME: use native avx256 unpack/pack */
517 lp_build_unpack2(gallivm, type, i16_type, x, &x_lo, &x_hi);
518 lp_build_unpack2(gallivm, type, i16_type, v0, &v0_lo, &v0_hi);
519 lp_build_unpack2(gallivm, type, i16_type, v1, &v1_lo, &v1_hi);
520 delta_lo = lp_build_sub(&bld2, v1_lo, v0_lo);
521 delta_hi = lp_build_sub(&bld2, v1_hi, v0_hi);
522
523 mul_lo = LLVMBuildMul(builder, x_lo, delta_lo, "");
524 mul_hi = LLVMBuildMul(builder, x_hi, delta_hi, "");
525
526 x_lo = LLVMBuildLShr(builder, mul_lo, lp_build_const_int_vec(gallivm, i16_type, 8), "");
527 x_hi = LLVMBuildLShr(builder, mul_hi, lp_build_const_int_vec(gallivm, i16_type, 8), "");
528 /* lerp optimization: pack now, do add afterwards */
529 tmp = lp_build_pack2(gallivm, i16_type, type, x_lo, x_hi);
530 *res0 = lp_build_add(bld, tmp, v0);
531
532 x_lo = LLVMBuildLShr(builder, mul_lo, lp_build_const_int_vec(gallivm, i16_type, 7), "");
533 x_hi = LLVMBuildLShr(builder, mul_hi, lp_build_const_int_vec(gallivm, i16_type, 7), "");
534 /* unlike above still need mask (but add still afterwards). */
535 x_lo = LLVMBuildAnd(builder, x_lo, lp_build_const_int_vec(gallivm, i16_type, 0xff), "");
536 x_hi = LLVMBuildAnd(builder, x_hi, lp_build_const_int_vec(gallivm, i16_type, 0xff), "");
537 tmp = lp_build_pack2(gallivm, i16_type, type, x_lo, x_hi);
538 *res1 = lp_build_add(bld, tmp, v0);
539 }
540
541 /**
542 * Convert from <n x i64> s3tc dxt1 to <4n x i8> RGBA AoS
543 * @param colors is a <n x i32> vector with n x 2x16bit colors
544 * @param codewords is a <n x i32> vector containing the codewords
545 * @param i is a <n x i32> vector with the x pixel coordinate (0 to 3)
546 * @param j is a <n x i32> vector with the y pixel coordinate (0 to 3)
547 */
548 static LLVMValueRef
549 s3tc_dxt1_full_to_rgba_aos(struct gallivm_state *gallivm,
550 unsigned n,
551 enum pipe_format format,
552 LLVMValueRef colors,
553 LLVMValueRef codewords,
554 LLVMValueRef i,
555 LLVMValueRef j)
556 {
557 LLVMBuilderRef builder = gallivm->builder;
558 LLVMValueRef color0, color1, color2, color3, color2_2, color3_2;
559 LLVMValueRef rgba, a, colors0, colors1, col0, col1, const2;
560 LLVMValueRef bit_pos, sel_mask, sel_lo, sel_hi, indices;
561 struct lp_type type, type8;
562 struct lp_build_context bld8, bld32;
563 bool is_dxt1_variant = format_dxt1_variant(format);
564
565 memset(&type, 0, sizeof type);
566 type.width = 32;
567 type.length = n;
568
569 memset(&type8, 0, sizeof type8);
570 type8.width = 8;
571 type8.length = 4*n;
572
573 assert(lp_check_value(type, i));
574 assert(lp_check_value(type, j));
575
576 a = lp_build_const_int_vec(gallivm, type, 0xff000000);
577
578 lp_build_context_init(&bld32, gallivm, type);
579 lp_build_context_init(&bld8, gallivm, type8);
580
581 /*
582 * works as follows:
583 * - expand color0/color1 to rgba8888
584 * - calculate color2/3 (interpolation) according to color0 < color1 rules
585 * - calculate color2/3 according to color0 >= color1 rules
586 * - do selection of color2/3 according to comparison of color0/1
587 * - extract indices (vector shift).
588 * - use compare/select to select the correct color. Since we have 2bit
589 * indices (and 4 colors), needs at least three compare/selects.
590 */
591 /*
592 * expand the two colors
593 */
594 col0 = LLVMBuildAnd(builder, colors, lp_build_const_int_vec(gallivm, type, 0x0000ffff), "");
595 col1 = LLVMBuildLShr(builder, colors, lp_build_const_int_vec(gallivm, type, 16), "");
596 if (n > 1) {
597 color_expand2_565_to_8888(gallivm, n, colors, &color0, &color1);
598 }
599 else {
600 color0 = color_expand_565_to_8888(gallivm, n, col0);
601 color1 = color_expand_565_to_8888(gallivm, n, col1);
602 }
603
604 /*
605 * interpolate colors
606 * color2_1 is 2/3 color0 + 1/3 color1
607 * color3_1 is 1/3 color0 + 2/3 color1
608 * color2_2 is 1/2 color0 + 1/2 color1
609 * color3_2 is 0
610 */
611
612 colors0 = LLVMBuildBitCast(builder, color0, bld8.vec_type, "");
613 colors1 = LLVMBuildBitCast(builder, color1, bld8.vec_type, "");
614 /* can combine 2 lerps into one mostly - still looks expensive enough. */
615 lp_build_lerp23(&bld8, colors0, colors1, &color2, &color3);
616 color2 = LLVMBuildBitCast(builder, color2, bld32.vec_type, "");
617 color3 = LLVMBuildBitCast(builder, color3, bld32.vec_type, "");
618
619 /* dxt3/5 always use 4-color encoding */
620 if (is_dxt1_variant) {
621 /* fix up alpha */
622 if (format == PIPE_FORMAT_DXT1_RGBA ||
623 format == PIPE_FORMAT_DXT1_SRGBA) {
624 color0 = LLVMBuildOr(builder, color0, a, "");
625 color1 = LLVMBuildOr(builder, color1, a, "");
626 color3 = LLVMBuildOr(builder, color3, a, "");
627 }
628 /*
629 * XXX with sse2 and 16x8 vectors, should use pavgb even when n == 1.
630 * Much cheaper (but we don't care that much if n == 1).
631 */
632 if ((util_get_cpu_caps()->has_sse2 && n == 4) ||
633 (util_get_cpu_caps()->has_avx2 && n == 8)) {
634 color2_2 = lp_build_pavgb(&bld8, colors0, colors1);
635 color2_2 = LLVMBuildBitCast(builder, color2_2, bld32.vec_type, "");
636 }
637 else {
638 struct lp_type i16_type = lp_wider_type(type8);
639 struct lp_build_context bld2;
640 LLVMValueRef v0_lo, v0_hi, v1_lo, v1_hi, addlo, addhi;
641
642 lp_build_context_init(&bld2, gallivm, i16_type);
643 bld2.type.sign = true;
644
645 /*
646 * This isn't as expensive as it looks (the unpack is the same as
647 * for lerp23), with correct rounding.
648 * (Note that while rounding is correct, this will always round down,
649 * whereas pavgb will always round up.)
650 */
651 /* FIXME: use native avx256 unpack/pack */
652 lp_build_unpack2(gallivm, type8, i16_type, colors0, &v0_lo, &v0_hi);
653 lp_build_unpack2(gallivm, type8, i16_type, colors1, &v1_lo, &v1_hi);
654
655 addlo = lp_build_add(&bld2, v0_lo, v1_lo);
656 addhi = lp_build_add(&bld2, v0_hi, v1_hi);
657 addlo = LLVMBuildLShr(builder, addlo,
658 lp_build_const_int_vec(gallivm, i16_type, 1), "");
659 addhi = LLVMBuildLShr(builder, addhi,
660 lp_build_const_int_vec(gallivm, i16_type, 1), "");
661 color2_2 = lp_build_pack2(gallivm, i16_type, type8, addlo, addhi);
662 color2_2 = LLVMBuildBitCast(builder, color2_2, bld32.vec_type, "");
663 }
664 color3_2 = lp_build_const_int_vec(gallivm, type, 0);
665
666 /* select between colors2/3 */
667 /* signed compare is faster saves some xors */
668 type.sign = true;
669 sel_mask = lp_build_compare(gallivm, type, PIPE_FUNC_GREATER, col0, col1);
670 color2 = lp_build_select(&bld32, sel_mask, color2, color2_2);
671 color3 = lp_build_select(&bld32, sel_mask, color3, color3_2);
672 type.sign = false;
673
674 if (format == PIPE_FORMAT_DXT1_RGBA ||
675 format == PIPE_FORMAT_DXT1_SRGBA) {
676 color2 = LLVMBuildOr(builder, color2, a, "");
677 }
678 }
679
680 const2 = lp_build_const_int_vec(gallivm, type, 2);
681 /* extract 2-bit index values */
682 bit_pos = LLVMBuildShl(builder, j, const2, "");
683 bit_pos = LLVMBuildAdd(builder, bit_pos, i, "");
684 bit_pos = LLVMBuildAdd(builder, bit_pos, bit_pos, "");
685 /*
686 * NOTE: This innocent looking shift is very expensive with x86/ssex.
687 * Shifts with per-elemnent shift count get roughly translated to
688 * extract (count), extract (value), shift, move (back to xmm), unpack
689 * per element!
690 * So about 20 instructions here for 4xi32.
691 * Newer llvm versions (3.7+) will not do extract/insert but use a
692 * a couple constant count vector shifts plus shuffles. About same
693 * amount of instructions unfortunately...
694 * Would get much worse with 8xi16 even...
695 * We could actually do better here:
696 * - subtract bit_pos from 128+30, shl 23, convert float to int...
697 * - now do mul with codewords followed by shr 30...
698 * But requires 32bit->32bit mul, sse41 only (well that's emulatable
699 * with 2 32bit->64bit muls...) and not exactly cheap
700 * AVX2, of course, fixes this nonsense.
701 */
702 indices = LLVMBuildLShr(builder, codewords, bit_pos, "");
703
704 /* finally select the colors */
705 sel_lo = LLVMBuildAnd(builder, indices, bld32.one, "");
706 sel_lo = lp_build_compare(gallivm, type, PIPE_FUNC_EQUAL, sel_lo, bld32.one);
707 color0 = lp_build_select(&bld32, sel_lo, color1, color0);
708 color2 = lp_build_select(&bld32, sel_lo, color3, color2);
709 sel_hi = LLVMBuildAnd(builder, indices, const2, "");
710 sel_hi = lp_build_compare(gallivm, type, PIPE_FUNC_EQUAL, sel_hi, const2);
711 rgba = lp_build_select(&bld32, sel_hi, color2, color0);
712
713 /* fix up alpha */
714 if (format == PIPE_FORMAT_DXT1_RGB ||
715 format == PIPE_FORMAT_DXT1_SRGB) {
716 rgba = LLVMBuildOr(builder, rgba, a, "");
717 }
718 return LLVMBuildBitCast(builder, rgba, bld8.vec_type, "");
719 }
720
721
722 static LLVMValueRef
723 s3tc_dxt1_to_rgba_aos(struct gallivm_state *gallivm,
724 unsigned n,
725 enum pipe_format format,
726 LLVMValueRef colors,
727 LLVMValueRef codewords,
728 LLVMValueRef i,
729 LLVMValueRef j)
730 {
731 return s3tc_dxt1_full_to_rgba_aos(gallivm, n, format,
732 colors, codewords, i, j);
733 }
734
735
736 /**
737 * Convert from <n x i128> s3tc dxt3 to <4n x i8> RGBA AoS
738 * @param colors is a <n x i32> vector with n x 2x16bit colors
739 * @param codewords is a <n x i32> vector containing the codewords
740 * @param alphas is a <n x i64> vector containing the alpha values
741 * @param i is a <n x i32> vector with the x pixel coordinate (0 to 3)
742 * @param j is a <n x i32> vector with the y pixel coordinate (0 to 3)
743 */
744 static LLVMValueRef
745 s3tc_dxt3_to_rgba_aos(struct gallivm_state *gallivm,
746 unsigned n,
747 enum pipe_format format,
748 LLVMValueRef colors,
749 LLVMValueRef codewords,
750 LLVMValueRef alpha_low,
751 LLVMValueRef alpha_hi,
752 LLVMValueRef i,
753 LLVMValueRef j)
754 {
755 LLVMBuilderRef builder = gallivm->builder;
756 LLVMValueRef rgba, tmp, tmp2;
757 LLVMValueRef bit_pos, sel_mask;
758 struct lp_type type, type8;
759 struct lp_build_context bld;
760
761 memset(&type, 0, sizeof type);
762 type.width = 32;
763 type.length = n;
764
765 memset(&type8, 0, sizeof type8);
766 type8.width = 8;
767 type8.length = n*4;
768
769 assert(lp_check_value(type, i));
770 assert(lp_check_value(type, j));
771
772 lp_build_context_init(&bld, gallivm, type);
773
774 rgba = s3tc_dxt1_to_rgba_aos(gallivm, n, format,
775 colors, codewords, i, j);
776
777 rgba = LLVMBuildBitCast(builder, rgba, bld.vec_type, "");
778
779 /*
780 * Extract alpha values. Since we now need to select from
781 * which 32bit vector values are fetched, construct selection
782 * mask from highest bit of bit_pos, and use select, then shift
783 * according to the bit_pos (without the highest bit).
784 * Note this is pointless for n == 1 case. Could just
785 * directly use 64bit arithmetic if we'd extract 64bit
786 * alpha value instead of 2x32...
787 */
788 /* pos = 4*(4j+i) */
789 bit_pos = LLVMBuildShl(builder, j, lp_build_const_int_vec(gallivm, type, 2), "");
790 bit_pos = LLVMBuildAdd(builder, bit_pos, i, "");
791 bit_pos = LLVMBuildShl(builder, bit_pos,
792 lp_build_const_int_vec(gallivm, type, 2), "");
793 sel_mask = LLVMBuildLShr(builder, bit_pos,
794 lp_build_const_int_vec(gallivm, type, 5), "");
795 sel_mask = LLVMBuildSub(builder, sel_mask, bld.one, "");
796 tmp = lp_build_select(&bld, sel_mask, alpha_low, alpha_hi);
797 bit_pos = LLVMBuildAnd(builder, bit_pos,
798 lp_build_const_int_vec(gallivm, type, 0xffffffdf), "");
799 /* Warning: slow shift with per element count (without avx2) */
800 /*
801 * Could do pshufb here as well - just use appropriate 2 bits in bit_pos
802 * to select the right byte with pshufb. Then for the remaining one bit
803 * just do shift/select.
804 */
805 tmp = LLVMBuildLShr(builder, tmp, bit_pos, "");
806
807 /* combined expand from a4 to a8 and shift into position */
808 tmp = LLVMBuildShl(builder, tmp, lp_build_const_int_vec(gallivm, type, 28), "");
809 tmp2 = LLVMBuildLShr(builder, tmp, lp_build_const_int_vec(gallivm, type, 4), "");
810 tmp = LLVMBuildOr(builder, tmp, tmp2, "");
811
812 rgba = LLVMBuildOr(builder, tmp, rgba, "");
813
814 return LLVMBuildBitCast(builder, rgba, lp_build_vec_type(gallivm, type8), "");
815 }
816
817 static LLVMValueRef
818 lp_build_lerpdxta(struct gallivm_state *gallivm,
819 LLVMValueRef alpha0,
820 LLVMValueRef alpha1,
821 LLVMValueRef code,
822 LLVMValueRef sel_mask,
823 unsigned n)
824 {
825 /*
826 * note we're doing lerp in 16bit since 32bit pmulld is only available in sse41
827 * (plus pmullw is actually faster...)
828 * we just pretend our 32bit values (which are really only 8bit) are 16bits.
829 * Note that this is obviously a disaster for the scalar case.
830 */
831 LLVMBuilderRef builder = gallivm->builder;
832 LLVMValueRef delta, ainterp;
833 LLVMValueRef weight5, weight7, weight;
834 struct lp_type type32, type16, type8;
835 struct lp_build_context bld16;
836
837 memset(&type32, 0, sizeof type32);
838 type32.width = 32;
839 type32.length = n;
840 memset(&type16, 0, sizeof type16);
841 type16.width = 16;
842 type16.length = 2*n;
843 type16.sign = true;
844 memset(&type8, 0, sizeof type8);
845 type8.width = 8;
846 type8.length = 4*n;
847
848 lp_build_context_init(&bld16, gallivm, type16);
849 /* 255/7 is a bit off - increase accuracy at the expense of shift later */
850 sel_mask = LLVMBuildBitCast(builder, sel_mask, bld16.vec_type, "");
851 weight5 = lp_build_const_int_vec(gallivm, type16, 255*64/5);
852 weight7 = lp_build_const_int_vec(gallivm, type16, 255*64/7);
853 weight = lp_build_select(&bld16, sel_mask, weight7, weight5);
854
855 alpha0 = LLVMBuildBitCast(builder, alpha0, bld16.vec_type, "");
856 alpha1 = LLVMBuildBitCast(builder, alpha1, bld16.vec_type, "");
857 code = LLVMBuildBitCast(builder, code, bld16.vec_type, "");
858 /* we'll get garbage in the elements which had code 0 (or larger than 5 or 7)
859 but we don't care */
860 code = LLVMBuildSub(builder, code, bld16.one, "");
861
862 weight = LLVMBuildMul(builder, weight, code, "");
863 weight = LLVMBuildLShr(builder, weight,
864 lp_build_const_int_vec(gallivm, type16, 6), "");
865
866 delta = LLVMBuildSub(builder, alpha1, alpha0, "");
867
868 ainterp = LLVMBuildMul(builder, delta, weight, "");
869 ainterp = LLVMBuildLShr(builder, ainterp,
870 lp_build_const_int_vec(gallivm, type16, 8), "");
871
872 ainterp = LLVMBuildBitCast(builder, ainterp, lp_build_vec_type(gallivm, type8), "");
873 alpha0 = LLVMBuildBitCast(builder, alpha0, lp_build_vec_type(gallivm, type8), "");
874 ainterp = LLVMBuildAdd(builder, alpha0, ainterp, "");
875 ainterp = LLVMBuildBitCast(builder, ainterp, lp_build_vec_type(gallivm, type32), "");
876
877 return ainterp;
878 }
879
880 static LLVMValueRef
881 s3tc_dxt5_alpha_channel(struct gallivm_state *gallivm,
882 bool is_signed,
883 unsigned n,
884 LLVMValueRef alpha_hi, LLVMValueRef alpha_lo,
885 LLVMValueRef i, LLVMValueRef j)
886 {
887 LLVMBuilderRef builder = gallivm->builder;
888 struct lp_type type, type8;
889 LLVMValueRef tmp, alpha0, alpha1, alphac, alphac0, bit_pos, shift;
890 LLVMValueRef sel_mask, tmp_mask, alpha, alpha64, code_s;
891 LLVMValueRef mask6, mask7, ainterp;
892 LLVMTypeRef i64t = LLVMInt64TypeInContext(gallivm->context);
893 LLVMTypeRef i32t = LLVMInt32TypeInContext(gallivm->context);
894 struct lp_build_context bld32;
895
896 memset(&type, 0, sizeof type);
897 type.width = 32;
898 type.length = n;
899
900 memset(&type8, 0, sizeof type8);
901 type8.width = 8;
902 type8.length = n;
903 type8.sign = is_signed;
904
905 lp_build_context_init(&bld32, gallivm, type);
906 /* this looks pretty complex for vectorization:
907 * extract a0/a1 values
908 * extract code
909 * select weights for interpolation depending on a0 > a1
910 * mul weights by code - 1
911 * lerp a0/a1/weights
912 * use selects for getting either a0, a1, interp a, interp a/0.0, interp a/1.0
913 */
914
915 alpha0 = LLVMBuildAnd(builder, alpha_lo,
916 lp_build_const_int_vec(gallivm, type, 0xff), "");
917 if (is_signed) {
918 alpha0 = LLVMBuildTrunc(builder, alpha0, lp_build_vec_type(gallivm, type8), "");
919 alpha0 = LLVMBuildSExt(builder, alpha0, lp_build_vec_type(gallivm, type), "");
920 }
921
922 alpha1 = LLVMBuildLShr(builder, alpha_lo,
923 lp_build_const_int_vec(gallivm, type, 8), "");
924 alpha1 = LLVMBuildAnd(builder, alpha1,
925 lp_build_const_int_vec(gallivm, type, 0xff), "");
926 if (is_signed) {
927 alpha1 = LLVMBuildTrunc(builder, alpha1, lp_build_vec_type(gallivm, type8), "");
928 alpha1 = LLVMBuildSExt(builder, alpha1, lp_build_vec_type(gallivm, type), "");
929 }
930
931 /* pos = 3*(4j+i) */
932 bit_pos = LLVMBuildShl(builder, j, lp_build_const_int_vec(gallivm, type, 2), "");
933 bit_pos = LLVMBuildAdd(builder, bit_pos, i, "");
934 tmp = LLVMBuildAdd(builder, bit_pos, bit_pos, "");
935 bit_pos = LLVMBuildAdd(builder, bit_pos, tmp, "");
936 /* get rid of first 2 bytes - saves shifts of alpha_lo/hi */
937 bit_pos = LLVMBuildAdd(builder, bit_pos,
938 lp_build_const_int_vec(gallivm, type, 16), "");
939
940 if (n == 1) {
941 struct lp_type type64;
942 memset(&type64, 0, sizeof type64);
943 type64.width = 64;
944 type64.length = 1;
945 /* This is pretty pointless could avoid by just directly extracting
946 64bit in the first place but makes it more complicated elsewhere */
947 alpha_lo = LLVMBuildZExt(builder, alpha_lo, i64t, "");
948 alpha_hi = LLVMBuildZExt(builder, alpha_hi, i64t, "");
949 alphac0 = LLVMBuildShl(builder, alpha_hi,
950 lp_build_const_int_vec(gallivm, type64, 32), "");
951 alphac0 = LLVMBuildOr(builder, alpha_lo, alphac0, "");
952
953 shift = LLVMBuildZExt(builder, bit_pos, i64t, "");
954 alphac0 = LLVMBuildLShr(builder, alphac0, shift, "");
955 alphac0 = LLVMBuildTrunc(builder, alphac0, i32t, "");
956 alphac = LLVMBuildAnd(builder, alphac0,
957 lp_build_const_int_vec(gallivm, type, 0x7), "");
958 }
959 else {
960 /*
961 * Using non-native vector length here (actually, with avx2 and
962 * n == 4 llvm will indeed expand to ymm regs...)
963 * At least newer llvm versions handle that ok.
964 * llvm 3.7+ will even handle the emulated 64bit shift with variable
965 * shift count without extraction (and it's actually easier to
966 * emulate than the 32bit one).
967 */
968 alpha64 = LLVMBuildShuffleVector(builder, alpha_lo, alpha_hi,
969 lp_build_const_unpackx2_shuffle(gallivm, n), "");
970
971 alpha64 = LLVMBuildBitCast(builder, alpha64, LLVMVectorType(i64t, n), "");
972 shift = LLVMBuildZExt(builder, bit_pos, LLVMVectorType(i64t, n), "");
973 alphac = LLVMBuildLShr(builder, alpha64, shift, "");
974 alphac = LLVMBuildTrunc(builder, alphac, bld32.vec_type, "");
975
976 alphac = LLVMBuildAnd(builder, alphac,
977 lp_build_const_int_vec(gallivm, type, 0x7), "");
978 }
979
980 /* signed compare is faster saves some xors */
981 type.sign = true;
982 /* alpha0 > alpha1 selection */
983 sel_mask = lp_build_compare(gallivm, type, PIPE_FUNC_GREATER,
984 alpha0, alpha1);
985 ainterp = lp_build_lerpdxta(gallivm, alpha0, alpha1, alphac, sel_mask, n);
986
987 /*
988 * if a0 > a1 then we select a0 for case 0, a1 for case 1, interp otherwise.
989 * else we select a0 for case 0, a1 for case 1,
990 * interp for case 2-5, 00 for 6 and 0xff(ffffff) for 7
991 * a = (c == 0) ? a0 : a1
992 * a = (c > 1) ? ainterp : a
993 * Finally handle case 6/7 for !(a0 > a1)
994 * a = (!(a0 > a1) && c == 6) ? 0 : a (andnot with mask)
995 * a = (!(a0 > a1) && c == 7) ? 0xffffffff : a (or with mask)
996 */
997 tmp_mask = lp_build_compare(gallivm, type, PIPE_FUNC_EQUAL,
998 alphac, bld32.zero);
999 alpha = lp_build_select(&bld32, tmp_mask, alpha0, alpha1);
1000 tmp_mask = lp_build_compare(gallivm, type, PIPE_FUNC_GREATER,
1001 alphac, bld32.one);
1002 alpha = lp_build_select(&bld32, tmp_mask, ainterp, alpha);
1003
1004 code_s = LLVMBuildAnd(builder, alphac,
1005 LLVMBuildNot(builder, sel_mask, ""), "");
1006 mask6 = lp_build_compare(gallivm, type, PIPE_FUNC_EQUAL,
1007 code_s, lp_build_const_int_vec(gallivm, type, 6));
1008 mask7 = lp_build_compare(gallivm, type, PIPE_FUNC_EQUAL,
1009 code_s, lp_build_const_int_vec(gallivm, type, 7));
1010 if (is_signed) {
1011 alpha = lp_build_select(&bld32, mask6, lp_build_const_int_vec(gallivm, type, -127), alpha);
1012 alpha = lp_build_select(&bld32, mask7, lp_build_const_int_vec(gallivm, type, 127), alpha);
1013 } else {
1014 alpha = LLVMBuildAnd(builder, alpha, LLVMBuildNot(builder, mask6, ""), "");
1015 alpha = LLVMBuildOr(builder, alpha, mask7, "");
1016 }
1017 /* There can be garbage in upper bits, mask them off for rgtc formats */
1018 alpha = LLVMBuildAnd(builder, alpha, lp_build_const_int_vec(gallivm, type, 0xff), "");
1019
1020 return alpha;
1021 }
1022
1023 /**
1024 * Convert from <n x i128> s3tc dxt5 to <4n x i8> RGBA AoS
1025 * @param colors is a <n x i32> vector with n x 2x16bit colors
1026 * @param codewords is a <n x i32> vector containing the codewords
1027 * @param alphas is a <n x i64> vector containing the alpha values
1028 * @param i is a <n x i32> vector with the x pixel coordinate (0 to 3)
1029 * @param j is a <n x i32> vector with the y pixel coordinate (0 to 3)
1030 */
1031 static LLVMValueRef
1032 s3tc_dxt5_full_to_rgba_aos(struct gallivm_state *gallivm,
1033 unsigned n,
1034 enum pipe_format format,
1035 LLVMValueRef colors,
1036 LLVMValueRef codewords,
1037 LLVMValueRef alpha_lo,
1038 LLVMValueRef alpha_hi,
1039 LLVMValueRef i,
1040 LLVMValueRef j)
1041 {
1042 LLVMBuilderRef builder = gallivm->builder;
1043 LLVMValueRef rgba, alpha;
1044 struct lp_type type, type8;
1045 struct lp_build_context bld32;
1046
1047 memset(&type, 0, sizeof type);
1048 type.width = 32;
1049 type.length = n;
1050
1051 memset(&type8, 0, sizeof type8);
1052 type8.width = 8;
1053 type8.length = n*4;
1054
1055 assert(lp_check_value(type, i));
1056 assert(lp_check_value(type, j));
1057
1058 lp_build_context_init(&bld32, gallivm, type);
1059
1060 assert(lp_check_value(type, i));
1061 assert(lp_check_value(type, j));
1062
1063 rgba = s3tc_dxt1_to_rgba_aos(gallivm, n, format,
1064 colors, codewords, i, j);
1065
1066 rgba = LLVMBuildBitCast(builder, rgba, bld32.vec_type, "");
1067
1068 alpha = s3tc_dxt5_alpha_channel(gallivm, false, n, alpha_hi, alpha_lo, i, j);
1069 alpha = LLVMBuildShl(builder, alpha, lp_build_const_int_vec(gallivm, type, 24), "");
1070 rgba = LLVMBuildOr(builder, alpha, rgba, "");
1071
1072 return LLVMBuildBitCast(builder, rgba, lp_build_vec_type(gallivm, type8), "");
1073 }
1074
1075
1076 static void
1077 lp_build_gather_s3tc_simple_scalar(struct gallivm_state *gallivm,
1078 const struct util_format_description *format_desc,
1079 LLVMValueRef *dxt_block,
1080 LLVMValueRef ptr)
1081 {
1082 LLVMBuilderRef builder = gallivm->builder;
1083 unsigned block_bits = format_desc->block.bits;
1084 LLVMValueRef elem, shuf;
1085 LLVMTypeRef type32 = LLVMIntTypeInContext(gallivm->context, 32);
1086 LLVMTypeRef src_type = LLVMIntTypeInContext(gallivm->context, block_bits);
1087 LLVMTypeRef type32_4 = LLVMVectorType(type32, 4);
1088
1089 assert(block_bits == 64 || block_bits == 128);
1090
1091 ptr = LLVMBuildBitCast(builder, ptr, LLVMPointerType(src_type, 0), "");
1092 elem = LLVMBuildLoad2(builder, src_type, ptr, "");
1093
1094 if (block_bits == 128) {
1095 /* just return block as is */
1096 *dxt_block = LLVMBuildBitCast(builder, elem, type32_4, "");
1097 }
1098 else {
1099 LLVMTypeRef type32_2 = LLVMVectorType(type32, 2);
1100 shuf = lp_build_const_extend_shuffle(gallivm, 2, 4);
1101 elem = LLVMBuildBitCast(builder, elem, type32_2, "");
1102 *dxt_block = LLVMBuildShuffleVector(builder, elem,
1103 LLVMGetUndef(type32_2), shuf, "");
1104 }
1105 }
1106
1107
1108 static void
1109 s3tc_store_cached_block(struct gallivm_state *gallivm,
1110 LLVMValueRef *col,
1111 LLVMValueRef tag_value,
1112 LLVMValueRef hash_index,
1113 LLVMValueRef cache)
1114 {
1115 LLVMBuilderRef builder = gallivm->builder;
1116 LLVMValueRef ptr, indices[3];
1117 LLVMTypeRef type_ptr4x32;
1118 unsigned count;
1119
1120 type_ptr4x32 = LLVMPointerType(LLVMVectorType(LLVMInt32TypeInContext(gallivm->context), 4), 0);
1121 indices[0] = lp_build_const_int32(gallivm, 0);
1122 indices[1] = lp_build_const_int32(gallivm, LP_BUILD_FORMAT_CACHE_MEMBER_TAGS);
1123 indices[2] = hash_index;
1124 LLVMTypeRef cache_type = lp_build_format_cache_type(gallivm);
1125 ptr = LLVMBuildGEP2(builder, cache_type, cache, indices, ARRAY_SIZE(indices), "");
1126 LLVMBuildStore(builder, tag_value, ptr);
1127
1128 indices[1] = lp_build_const_int32(gallivm, LP_BUILD_FORMAT_CACHE_MEMBER_DATA);
1129 hash_index = LLVMBuildMul(builder, hash_index, lp_build_const_int32(gallivm, 16), "");
1130 for (count = 0; count < 4; count++) {
1131 indices[2] = hash_index;
1132 ptr = LLVMBuildGEP2(builder, cache_type, cache, indices, ARRAY_SIZE(indices), "");
1133 ptr = LLVMBuildBitCast(builder, ptr, type_ptr4x32, "");
1134 LLVMBuildStore(builder, col[count], ptr);
1135 hash_index = LLVMBuildAdd(builder, hash_index, lp_build_const_int32(gallivm, 4), "");
1136 }
1137 }
1138
1139 static LLVMValueRef
1140 lookup_cache_member(struct gallivm_state *gallivm, LLVMValueRef cache, enum cache_member member, LLVMValueRef index) {
1141 assert(member == LP_BUILD_FORMAT_CACHE_MEMBER_DATA || member == LP_BUILD_FORMAT_CACHE_MEMBER_TAGS);
1142 LLVMBuilderRef builder = gallivm->builder;
1143 LLVMValueRef member_ptr, indices[3];
1144
1145 indices[0] = lp_build_const_int32(gallivm, 0);
1146 indices[1] = lp_build_const_int32(gallivm, member);
1147 indices[2] = index;
1148
1149 const char *name =
1150 member == LP_BUILD_FORMAT_CACHE_MEMBER_DATA ? "cache_data" :
1151 member == LP_BUILD_FORMAT_CACHE_MEMBER_TAGS ? "tag_data" : "";
1152
1153 member_ptr = LLVMBuildGEP2(builder, lp_build_format_cache_type(gallivm),
1154 cache, indices, ARRAY_SIZE(indices), "cache_gep");
1155
1156 return LLVMBuildLoad2(builder, lp_build_format_cache_elem_type(gallivm, member), member_ptr, name);
1157 }
1158
1159 static LLVMValueRef
1160 s3tc_lookup_cached_pixel(struct gallivm_state *gallivm,
1161 LLVMValueRef cache,
1162 LLVMValueRef index)
1163 {
1164 return lookup_cache_member(gallivm, cache, LP_BUILD_FORMAT_CACHE_MEMBER_DATA, index);
1165 }
1166
1167 static LLVMValueRef
1168 s3tc_lookup_tag_data(struct gallivm_state *gallivm,
1169 LLVMValueRef cache,
1170 LLVMValueRef index)
1171 {
1172 return lookup_cache_member(gallivm, cache, LP_BUILD_FORMAT_CACHE_MEMBER_TAGS, index);
1173 }
1174
1175 #if LP_BUILD_FORMAT_CACHE_DEBUG
1176 static void
1177 s3tc_update_cache_access(struct gallivm_state *gallivm,
1178 LLVMValueRef ptr,
1179 unsigned count,
1180 unsigned index)
1181 {
1182 LLVMBuilderRef builder = gallivm->builder;
1183 LLVMValueRef member_ptr, cache_access;
1184
1185 assert(index == LP_BUILD_FORMAT_CACHE_MEMBER_ACCESS_TOTAL ||
1186 index == LP_BUILD_FORMAT_CACHE_MEMBER_ACCESS_MISS);
1187 LLVMTypeRef cache_type = lp_build_format_cache_type(gallivm);
1188 member_ptr = lp_build_struct_get_ptr2(gallivm, cache_type, ptr, index, "");
1189 cache_access = LLVMBuildLoad2(builder, LLVMInt64TypeInContext(gallivm->context), member_ptr, "cache_access");
1190 cache_access = LLVMBuildAdd(builder, cache_access,
1191 LLVMConstInt(LLVMInt64TypeInContext(gallivm->context), count, 0), "");
1192 LLVMBuildStore(builder, cache_access, member_ptr);
1193 }
1194 #endif
1195
1196 /**
1197 * Calculate 1/3(v1-v0) + v0 and 2*1/3(v1-v0) + v0.
1198 * The lerp is performed between the first 2 32bit colors
1199 * in the source vector, both results are returned packed in result vector.
1200 */
1201 static LLVMValueRef
1202 lp_build_lerp23_single(struct lp_build_context *bld,
1203 LLVMValueRef v01)
1204 {
1205 struct gallivm_state *gallivm = bld->gallivm;
1206 LLVMValueRef x, mul, delta, res, v0, v1, elems[8];
1207 const struct lp_type type = bld->type;
1208 LLVMBuilderRef builder = bld->gallivm->builder;
1209 struct lp_type i16_type = lp_wider_type(type);
1210 struct lp_type i32_type = lp_wider_type(i16_type);
1211 struct lp_build_context bld2;
1212
1213 assert(!type.floating && !type.fixed && !type.norm && type.width == 8);
1214
1215 lp_build_context_init(&bld2, gallivm, i16_type);
1216 bld2.type.sign = true;
1217
1218 /* weights 256/3, 256*2/3, with correct rounding */
1219 elems[0] = elems[1] = elems[2] = elems[3] =
1220 lp_build_const_elem(gallivm, i16_type, 255*1/3);
1221 elems[4] = elems[5] = elems[6] = elems[7] =
1222 lp_build_const_elem(gallivm, i16_type, 171);
1223 x = LLVMConstVector(elems, 8);
1224
1225 /*
1226 * v01 has col0 in 32bit elem 0, col1 in elem 1.
1227 * Interleave/unpack will give us separate v0/v1 vectors.
1228 */
1229 v01 = lp_build_interleave2(gallivm, i32_type, v01, v01, 0);
1230 v01 = LLVMBuildBitCast(builder, v01, bld->vec_type, "");
1231
1232 lp_build_unpack2(gallivm, type, i16_type, v01, &v0, &v1);
1233 delta = lp_build_sub(&bld2, v1, v0);
1234
1235 mul = LLVMBuildMul(builder, x, delta, "");
1236
1237 mul = LLVMBuildLShr(builder, mul, lp_build_const_int_vec(gallivm, i16_type, 8), "");
1238 /* lerp optimization: pack now, do add afterwards */
1239 res = lp_build_pack2(gallivm, i16_type, type, mul, bld2.undef);
1240 /* only lower 2 elems are valid - for these v0 is really v0 */
1241 return lp_build_add(bld, res, v01);
1242 }
1243
1244 /*
1245 * decode one dxt1 block.
1246 */
1247 static void
1248 s3tc_decode_block_dxt1(struct gallivm_state *gallivm,
1249 enum pipe_format format,
1250 LLVMValueRef dxt_block,
1251 LLVMValueRef *col)
1252 {
1253 LLVMBuilderRef builder = gallivm->builder;
1254 LLVMValueRef color01, color23, color01_16, color0123;
1255 LLVMValueRef rgba, tmp, a, sel_mask, indices, code, const2;
1256 struct lp_type type8, type32, type16, type64;
1257 struct lp_build_context bld8, bld32, bld16, bld64;
1258 unsigned i;
1259 bool is_dxt1_variant = format_dxt1_variant(format);
1260
1261 memset(&type32, 0, sizeof type32);
1262 type32.width = 32;
1263 type32.length = 4;
1264 type32.sign = true;
1265
1266 memset(&type8, 0, sizeof type8);
1267 type8.width = 8;
1268 type8.length = 16;
1269
1270 memset(&type16, 0, sizeof type16);
1271 type16.width = 16;
1272 type16.length = 8;
1273
1274 memset(&type64, 0, sizeof type64);
1275 type64.width = 64;
1276 type64.length = 2;
1277
1278 a = lp_build_const_int_vec(gallivm, type32, 0xff000000);
1279 const2 = lp_build_const_int_vec(gallivm, type32, 2);
1280
1281 lp_build_context_init(&bld32, gallivm, type32);
1282 lp_build_context_init(&bld16, gallivm, type16);
1283 lp_build_context_init(&bld8, gallivm, type8);
1284 lp_build_context_init(&bld64, gallivm, type64);
1285
1286 if (is_dxt1_variant) {
1287 color01 = lp_build_shuffle1undef(gallivm, dxt_block, 0, 4);
1288 code = lp_build_shuffle1undef(gallivm, dxt_block, 1, 4);
1289 } else {
1290 color01 = lp_build_shuffle1undef(gallivm, dxt_block, 2, 4);
1291 code = lp_build_shuffle1undef(gallivm, dxt_block, 3, 4);
1292 }
1293 code = LLVMBuildBitCast(builder, code, bld8.vec_type, "");
1294 /* expand bytes to dwords */
1295 code = lp_build_interleave2(gallivm, type8, code, code, 0);
1296 code = lp_build_interleave2(gallivm, type8, code, code, 0);
1297
1298
1299 /*
1300 * works as follows:
1301 * - expand color0/color1 to rgba8888
1302 * - calculate color2/3 (interpolation) according to color0 < color1 rules
1303 * - calculate color2/3 according to color0 >= color1 rules
1304 * - do selection of color2/3 according to comparison of color0/1
1305 * - extract indices.
1306 * - use compare/select to select the correct color. Since we have 2bit
1307 * indices (and 4 colors), needs at least three compare/selects.
1308 */
1309
1310 /*
1311 * expand the two colors
1312 */
1313 color01 = LLVMBuildBitCast(builder, color01, bld16.vec_type, "");
1314 color01 = lp_build_interleave2(gallivm, type16, color01,
1315 bld16.zero, 0);
1316 color01_16 = LLVMBuildBitCast(builder, color01, bld32.vec_type, "");
1317 color01 = color_expand_565_to_8888(gallivm, 4, color01_16);
1318
1319 /*
1320 * interpolate colors
1321 * color2_1 is 2/3 color0 + 1/3 color1
1322 * color3_1 is 1/3 color0 + 2/3 color1
1323 * color2_2 is 1/2 color0 + 1/2 color1
1324 * color3_2 is 0
1325 */
1326
1327 /* TODO: since this is now always scalar, should
1328 * probably just use control flow here instead of calculating
1329 * both cases and then selection
1330 */
1331 if (format == PIPE_FORMAT_DXT1_RGBA ||
1332 format == PIPE_FORMAT_DXT1_SRGBA) {
1333 color01 = LLVMBuildOr(builder, color01, a, "");
1334 }
1335 /* can combine 2 lerps into one mostly */
1336 color23 = lp_build_lerp23_single(&bld8, color01);
1337 color23 = LLVMBuildBitCast(builder, color23, bld32.vec_type, "");
1338
1339 /* dxt3/5 always use 4-color encoding */
1340 if (is_dxt1_variant) {
1341 LLVMValueRef color23_2, color2_2;
1342
1343 if (util_get_cpu_caps()->has_sse2) {
1344 LLVMValueRef intrargs[2];
1345 intrargs[0] = LLVMBuildBitCast(builder, color01, bld8.vec_type, "");
1346 /* same interleave as for lerp23 - correct result in 2nd element */
1347 intrargs[1] = lp_build_interleave2(gallivm, type32, color01, color01, 0);
1348 intrargs[1] = LLVMBuildBitCast(builder, intrargs[1], bld8.vec_type, "");
1349 color2_2 = lp_build_pavgb(&bld8, intrargs[0], intrargs[1]);
1350 }
1351 else {
1352 LLVMValueRef v01, v0, v1, vhalf;
1353 /*
1354 * This isn't as expensive as it looks (the unpack is the same as
1355 * for lerp23, which is the reason why we do the pointless
1356 * interleave2 too), with correct rounding (the two lower elements
1357 * will be the same).
1358 */
1359 v01 = lp_build_interleave2(gallivm, type32, color01, color01, 0);
1360 v01 = LLVMBuildBitCast(builder, v01, bld8.vec_type, "");
1361 lp_build_unpack2(gallivm, type8, type16, v01, &v0, &v1);
1362 vhalf = lp_build_add(&bld16, v0, v1);
1363 vhalf = LLVMBuildLShr(builder, vhalf, bld16.one, "");
1364 color2_2 = lp_build_pack2(gallivm, type16, type8, vhalf, bld16.undef);
1365 }
1366 /* shuffle in color 3 as elem 2 zero, color 2 elem 1 */
1367 color23_2 = LLVMBuildBitCast(builder, color2_2, bld64.vec_type, "");
1368 color23_2 = LLVMBuildLShr(builder, color23_2,
1369 lp_build_const_int_vec(gallivm, type64, 32), "");
1370 color23_2 = LLVMBuildBitCast(builder, color23_2, bld32.vec_type, "");
1371
1372 tmp = LLVMBuildBitCast(builder, color01_16, bld64.vec_type, "");
1373 tmp = LLVMBuildLShr(builder, tmp,
1374 lp_build_const_int_vec(gallivm, type64, 32), "");
1375 tmp = LLVMBuildBitCast(builder, tmp, bld32.vec_type, "");
1376 sel_mask = lp_build_compare(gallivm, type32, PIPE_FUNC_GREATER,
1377 color01_16, tmp);
1378 sel_mask = lp_build_interleave2(gallivm, type32, sel_mask, sel_mask, 0);
1379 color23 = lp_build_select(&bld32, sel_mask, color23, color23_2);
1380 }
1381
1382 if (util_get_cpu_caps()->has_ssse3) {
1383 /*
1384 * Use pshufb as mini-lut. (Only doable with intrinsics as the
1385 * final shuffles are non-constant. pshufb is awesome!)
1386 */
1387 LLVMValueRef shuf[16], low2mask;
1388 LLVMValueRef intrargs[2], lut_ind, lut_adj;
1389
1390 color01 = LLVMBuildBitCast(builder, color01, bld64.vec_type, "");
1391 color23 = LLVMBuildBitCast(builder, color23, bld64.vec_type, "");
1392 color0123 = lp_build_interleave2(gallivm, type64, color01, color23, 0);
1393 color0123 = LLVMBuildBitCast(builder, color0123, bld32.vec_type, "");
1394
1395 if (format == PIPE_FORMAT_DXT1_RGB ||
1396 format == PIPE_FORMAT_DXT1_SRGB) {
1397 color0123 = LLVMBuildOr(builder, color0123, a, "");
1398 }
1399
1400 /* shuffle as r0r1r2r3g0g1... */
1401 for (i = 0; i < 4; i++) {
1402 shuf[4*i] = lp_build_const_int32(gallivm, 0 + i);
1403 shuf[4*i+1] = lp_build_const_int32(gallivm, 4 + i);
1404 shuf[4*i+2] = lp_build_const_int32(gallivm, 8 + i);
1405 shuf[4*i+3] = lp_build_const_int32(gallivm, 12 + i);
1406 }
1407 color0123 = LLVMBuildBitCast(builder, color0123, bld8.vec_type, "");
1408 color0123 = LLVMBuildShuffleVector(builder, color0123, bld8.undef,
1409 LLVMConstVector(shuf, 16), "");
1410
1411 /* lowest 2 bits of each 8 bit value contain index into "LUT" */
1412 low2mask = lp_build_const_int_vec(gallivm, type8, 3);
1413 /* add 0/4/8/12 for r/g/b/a */
1414 lut_adj = lp_build_const_int_vec(gallivm, type32, 0x0c080400);
1415 lut_adj = LLVMBuildBitCast(builder, lut_adj, bld8.vec_type, "");
1416 intrargs[0] = color0123;
1417 for (i = 0; i < 4; i++) {
1418 lut_ind = LLVMBuildAnd(builder, code, low2mask, "");
1419 lut_ind = LLVMBuildOr(builder, lut_ind, lut_adj, "");
1420 intrargs[1] = lut_ind;
1421 col[i] = lp_build_intrinsic(builder, "llvm.x86.ssse3.pshuf.b.128",
1422 bld8.vec_type, intrargs, 2, 0);
1423 col[i] = LLVMBuildBitCast(builder, col[i], bld32.vec_type, "");
1424 code = LLVMBuildBitCast(builder, code, bld32.vec_type, "");
1425 code = LLVMBuildLShr(builder, code, const2, "");
1426 code = LLVMBuildBitCast(builder, code, bld8.vec_type, "");
1427 }
1428 }
1429 else {
1430 /* Thanks to vectorization can do 4 texels in parallel */
1431 LLVMValueRef color0, color1, color2, color3;
1432 if (format == PIPE_FORMAT_DXT1_RGB ||
1433 format == PIPE_FORMAT_DXT1_SRGB) {
1434 color01 = LLVMBuildOr(builder, color01, a, "");
1435 color23 = LLVMBuildOr(builder, color23, a, "");
1436 }
1437 color0 = LLVMBuildShuffleVector(builder, color01, bld32.undef,
1438 lp_build_const_shuffle1(gallivm, 0, 4), "");
1439 color1 = LLVMBuildShuffleVector(builder, color01, bld32.undef,
1440 lp_build_const_shuffle1(gallivm, 1, 4), "");
1441 color2 = LLVMBuildShuffleVector(builder, color23, bld32.undef,
1442 lp_build_const_shuffle1(gallivm, 0, 4), "");
1443 color3 = LLVMBuildShuffleVector(builder, color23, bld32.undef,
1444 lp_build_const_shuffle1(gallivm, 1, 4), "");
1445 code = LLVMBuildBitCast(builder, code, bld32.vec_type, "");
1446
1447 for (i = 0; i < 4; i++) {
1448 /* select the colors */
1449 LLVMValueRef selmasklo, rgba01, rgba23, bitlo;
1450 bitlo = bld32.one;
1451 indices = LLVMBuildAnd(builder, code, bitlo, "");
1452 selmasklo = lp_build_compare(gallivm, type32, PIPE_FUNC_EQUAL,
1453 indices, bitlo);
1454 rgba01 = lp_build_select(&bld32, selmasklo, color1, color0);
1455
1456 LLVMValueRef selmaskhi;
1457 indices = LLVMBuildAnd(builder, code, const2, "");
1458 selmaskhi = lp_build_compare(gallivm, type32, PIPE_FUNC_EQUAL,
1459 indices, const2);
1460 rgba23 = lp_build_select(&bld32, selmasklo, color3, color2);
1461 rgba = lp_build_select(&bld32, selmaskhi, rgba23, rgba01);
1462
1463 /*
1464 * Note that this will give "wrong" order.
1465 * col0 will be rgba0, rgba4, rgba8, rgba12, col1 rgba1, rgba5, ...
1466 * This would be easily fixable by using different shuffle, bitlo/hi
1467 * vectors above (and different shift), but seems slightly easier to
1468 * deal with for dxt3/dxt5 alpha too. So instead change lookup.
1469 */
1470 col[i] = rgba;
1471 code = LLVMBuildLShr(builder, code, const2, "");
1472 }
1473 }
1474 }
1475
1476 /*
1477 * decode one dxt3 block.
1478 */
1479 static void
1480 s3tc_decode_block_dxt3(struct gallivm_state *gallivm,
1481 enum pipe_format format,
1482 LLVMValueRef dxt_block,
1483 LLVMValueRef *col)
1484 {
1485 LLVMBuilderRef builder = gallivm->builder;
1486 LLVMValueRef alpha, alphas0, alphas1, shift4_16, a[4], mask8hi;
1487 struct lp_type type32, type8, type16;
1488 unsigned i;
1489
1490 memset(&type32, 0, sizeof type32);
1491 type32.width = 32;
1492 type32.length = 4;
1493
1494 memset(&type8, 0, sizeof type8);
1495 type8.width = 8;
1496 type8.length = 16;
1497
1498 memset(&type16, 0, sizeof type16);
1499 type16.width = 16;
1500 type16.length = 8;
1501
1502 s3tc_decode_block_dxt1(gallivm, format, dxt_block, col);
1503
1504 shift4_16 = lp_build_const_int_vec(gallivm, type16, 4);
1505 mask8hi = lp_build_const_int_vec(gallivm, type32, 0xff000000);
1506
1507 alpha = LLVMBuildBitCast(builder, dxt_block,
1508 lp_build_vec_type(gallivm, type8), "");
1509 alpha = lp_build_interleave2(gallivm, type8, alpha, alpha, 0);
1510 alpha = LLVMBuildBitCast(builder, alpha,
1511 lp_build_vec_type(gallivm, type16), "");
1512 alpha = LLVMBuildAnd(builder, alpha,
1513 lp_build_const_int_vec(gallivm, type16, 0xf00f), "");
1514 alphas0 = LLVMBuildLShr(builder, alpha, shift4_16, "");
1515 alphas1 = LLVMBuildShl(builder, alpha, shift4_16, "");
1516 alpha = LLVMBuildOr(builder, alphas0, alpha, "");
1517 alpha = LLVMBuildOr(builder, alphas1, alpha, "");
1518 alpha = LLVMBuildBitCast(builder, alpha,
1519 lp_build_vec_type(gallivm, type32), "");
1520 /*
1521 * alpha now contains elems 0,1,2,3,... (ubytes)
1522 * we need 0,4,8,12, 1,5,9,13 etc. in dwords to match color (which
1523 * is just as easy as "natural" order - 3 shift/and instead of 6 unpack).
1524 */
1525 a[0] = LLVMBuildShl(builder, alpha,
1526 lp_build_const_int_vec(gallivm, type32, 24), "");
1527 a[1] = LLVMBuildShl(builder, alpha,
1528 lp_build_const_int_vec(gallivm, type32, 16), "");
1529 a[1] = LLVMBuildAnd(builder, a[1], mask8hi, "");
1530 a[2] = LLVMBuildShl(builder, alpha,
1531 lp_build_const_int_vec(gallivm, type32, 8), "");
1532 a[2] = LLVMBuildAnd(builder, a[2], mask8hi, "");
1533 a[3] = LLVMBuildAnd(builder, alpha, mask8hi, "");
1534
1535 for (i = 0; i < 4; i++) {
1536 col[i] = LLVMBuildOr(builder, col[i], a[i], "");
1537 }
1538 }
1539
1540
1541 static LLVMValueRef
1542 lp_build_lerpdxta_block(struct gallivm_state *gallivm,
1543 LLVMValueRef alpha0,
1544 LLVMValueRef alpha1,
1545 LLVMValueRef code,
1546 LLVMValueRef sel_mask)
1547 {
1548 LLVMBuilderRef builder = gallivm->builder;
1549 LLVMValueRef delta, ainterp;
1550 LLVMValueRef weight5, weight7, weight;
1551 struct lp_type type16;
1552 struct lp_build_context bld;
1553
1554 memset(&type16, 0, sizeof type16);
1555 type16.width = 16;
1556 type16.length = 8;
1557 type16.sign = true;
1558
1559 lp_build_context_init(&bld, gallivm, type16);
1560 /*
1561 * 256/7 is only 36.57 so we'd lose quite some precision. Since it would
1562 * actually be desirable to do this here with even higher accuracy than
1563 * even 8 bit (more or less required for rgtc, albeit that's not handled
1564 * here right now), shift the weights after multiplication by code.
1565 */
1566 weight5 = lp_build_const_int_vec(gallivm, type16, 256*64/5);
1567 weight7 = lp_build_const_int_vec(gallivm, type16, 256*64/7);
1568 weight = lp_build_select(&bld, sel_mask, weight7, weight5);
1569
1570 /*
1571 * we'll get garbage in the elements which had code 0 (or larger than
1572 * 5 or 7) but we don't care (or rather, need to fix up anyway).
1573 */
1574 code = LLVMBuildSub(builder, code, bld.one, "");
1575
1576 weight = LLVMBuildMul(builder, weight, code, "");
1577 weight = LLVMBuildLShr(builder, weight,
1578 lp_build_const_int_vec(gallivm, type16, 6), "");
1579
1580 delta = LLVMBuildSub(builder, alpha1, alpha0, "");
1581
1582 ainterp = LLVMBuildMul(builder, delta, weight, "");
1583 ainterp = LLVMBuildLShr(builder, ainterp,
1584 lp_build_const_int_vec(gallivm, type16, 8), "");
1585
1586 /* lerp is done later (with packed values) */
1587
1588 return ainterp;
1589 }
1590
1591
1592 /*
1593 * decode one dxt5 block.
1594 */
1595 static void
1596 s3tc_decode_block_dxt5(struct gallivm_state *gallivm,
1597 enum pipe_format format,
1598 LLVMValueRef dxt_block,
1599 LLVMValueRef *col)
1600 {
1601 LLVMBuilderRef builder = gallivm->builder;
1602 LLVMValueRef alpha, alpha0, alpha1, ares;
1603 LLVMValueRef ainterp, ainterp0, ainterp1, shuffle1, sel_mask, sel_mask2;
1604 LLVMValueRef a[4], acode, tmp0, tmp1;
1605 LLVMTypeRef i64t, i32t;
1606 struct lp_type type32, type64, type8, type16;
1607 struct lp_build_context bld16, bld8;
1608 unsigned i;
1609
1610 memset(&type32, 0, sizeof type32);
1611 type32.width = 32;
1612 type32.length = 4;
1613
1614 memset(&type64, 0, sizeof type64);
1615 type64.width = 64;
1616 type64.length = 2;
1617
1618 memset(&type8, 0, sizeof type8);
1619 type8.width = 8;
1620 type8.length = 16;
1621
1622 memset(&type16, 0, sizeof type16);
1623 type16.width = 16;
1624 type16.length = 8;
1625
1626 lp_build_context_init(&bld16, gallivm, type16);
1627 lp_build_context_init(&bld8, gallivm, type8);
1628
1629 i64t = lp_build_vec_type(gallivm, type64);
1630 i32t = lp_build_vec_type(gallivm, type32);
1631
1632 s3tc_decode_block_dxt1(gallivm, format, dxt_block, col);
1633
1634 /*
1635 * three possible strategies for vectorizing alpha:
1636 * 1) compute all 8 values then use scalar extraction
1637 * (i.e. have all 8 alpha values packed in one 64bit scalar
1638 * and do something like ax = vals >> (codex * 8) followed
1639 * by inserting these values back into color)
1640 * 2) same as 8 but just use pshufb as a mini-LUT for selection.
1641 * (without pshufb would need boatloads of cmp/selects trying to
1642 * keep things vectorized for essentially scalar selection).
1643 * 3) do something similar to the uncached case
1644 * needs more calculations (need to calc 16 values instead of 8 though
1645 * that's only an issue for the lerp which we need to do twice otherwise
1646 * everything still fits into 128bit) but keeps things vectorized mostly.
1647 * Trying 3) here though not sure it's really faster...
1648 * With pshufb, we try 2) (cheaper and more accurate)
1649 */
1650
1651 /*
1652 * Ideally, we'd use 2 variable 16bit shifts here (byte shifts wouldn't
1653 * help since code crosses 8bit boundaries). But variable shifts are
1654 * AVX2 only, and even then only dword/quadword (intel _really_ hates
1655 * shifts!). Instead, emulate by 16bit muls.
1656 * Also, the required byte shuffles are essentially non-emulatable, so
1657 * require ssse3 (albeit other archs might do them fine).
1658 * This is not directly tied to ssse3 - just need sane byte shuffles.
1659 * But ordering is going to be different below so use same condition.
1660 */
1661
1662
1663 /* vectorize alpha */
1664 alpha = LLVMBuildBitCast(builder, dxt_block, i64t, "");
1665 alpha0 = LLVMBuildAnd(builder, alpha,
1666 lp_build_const_int_vec(gallivm, type64, 0xff), "");
1667 alpha0 = LLVMBuildBitCast(builder, alpha0, bld16.vec_type, "");
1668 alpha = LLVMBuildBitCast(builder, alpha, bld16.vec_type, "");
1669 alpha1 = LLVMBuildLShr(builder, alpha,
1670 lp_build_const_int_vec(gallivm, type16, 8), "");
1671 alpha = LLVMBuildBitCast(builder, alpha, i64t, "");
1672 shuffle1 = lp_build_const_shuffle1(gallivm, 0, 8);
1673 alpha0 = LLVMBuildShuffleVector(builder, alpha0, alpha0, shuffle1, "");
1674 alpha1 = LLVMBuildShuffleVector(builder, alpha1, alpha1, shuffle1, "");
1675
1676 type16.sign = true;
1677 sel_mask = lp_build_compare(gallivm, type16, PIPE_FUNC_GREATER,
1678 alpha0, alpha1);
1679 type16.sign = false;
1680 sel_mask = LLVMBuildBitCast(builder, sel_mask, bld8.vec_type, "");
1681
1682 if (!util_get_cpu_caps()->has_ssse3) {
1683 LLVMValueRef acodeg, mask1, acode0, acode1;
1684
1685 /* extraction of the 3 bit values into something more useful is HARD */
1686 /* first steps are actually scalar */
1687 acode = LLVMBuildLShr(builder, alpha,
1688 lp_build_const_int_vec(gallivm, type64, 16), "");
1689 tmp0 = LLVMBuildAnd(builder, acode,
1690 lp_build_const_int_vec(gallivm, type64, 0xffffff), "");
1691 tmp1 = LLVMBuildLShr(builder, acode,
1692 lp_build_const_int_vec(gallivm, type64, 24), "");
1693 tmp0 = LLVMBuildBitCast(builder, tmp0, i32t, "");
1694 tmp1 = LLVMBuildBitCast(builder, tmp1, i32t, "");
1695 acode = lp_build_interleave2(gallivm, type32, tmp0, tmp1, 0);
1696 /* now have 2x24bit in 4x32bit, order 01234567, 89..., undef, undef */
1697 tmp0 = LLVMBuildAnd(builder, acode,
1698 lp_build_const_int_vec(gallivm, type32, 0xfff), "");
1699 tmp1 = LLVMBuildLShr(builder, acode,
1700 lp_build_const_int_vec(gallivm, type32, 12), "");
1701 acode = lp_build_interleave2(gallivm, type32, tmp0, tmp1, 0);
1702 /* now have 4x12bit in 4x32bit, order 0123, 4567, ,,, */
1703 tmp0 = LLVMBuildAnd(builder, acode,
1704 lp_build_const_int_vec(gallivm, type32, 0x3f), "");
1705 tmp1 = LLVMBuildLShr(builder, acode,
1706 lp_build_const_int_vec(gallivm, type32, 6), "");
1707 /* use signed pack doesn't matter and otherwise need sse41 */
1708 type32.sign = type16.sign = true;
1709 acode = lp_build_pack2(gallivm, type32, type16, tmp0, tmp1);
1710 type32.sign = type16.sign = false;
1711 /* now have 8x6bit in 8x16bit, 01, 45, 89, ..., 23, 67, ... */
1712 acode0 = LLVMBuildAnd(builder, acode,
1713 lp_build_const_int_vec(gallivm, type16, 0x7), "");
1714 acode1 = LLVMBuildLShr(builder, acode,
1715 lp_build_const_int_vec(gallivm, type16, 3), "");
1716 acode = lp_build_pack2(gallivm, type16, type8, acode0, acode1);
1717 /* acode0 contains elems 0,4,8,12,2,6,10,14, acode1 1,5,9,... */
1718
1719 acodeg = LLVMBuildAnd(builder, acode,
1720 LLVMBuildNot(builder, sel_mask, ""), "");
1721 mask1 = lp_build_compare(gallivm, type8, PIPE_FUNC_EQUAL,
1722 acode, bld8.one);
1723
1724 sel_mask = LLVMBuildBitCast(builder, sel_mask, bld16.vec_type, "");
1725 ainterp0 = lp_build_lerpdxta_block(gallivm, alpha0, alpha1, acode0, sel_mask);
1726 ainterp1 = lp_build_lerpdxta_block(gallivm, alpha0, alpha1, acode1, sel_mask);
1727 sel_mask = LLVMBuildBitCast(builder, sel_mask, bld8.vec_type, "");
1728 ainterp = lp_build_pack2(gallivm, type16, type8, ainterp0, ainterp1);
1729 alpha0 = lp_build_pack2(gallivm, type16, type8, alpha0, alpha0);
1730 alpha1 = lp_build_pack2(gallivm, type16, type8, alpha1, alpha1);
1731 ainterp = LLVMBuildAdd(builder, ainterp, alpha0, "");
1732 /* Fix up val01 */
1733 sel_mask2 = lp_build_compare(gallivm, type8, PIPE_FUNC_EQUAL,
1734 acode, bld8.zero);
1735 ainterp = lp_build_select(&bld8, sel_mask2, alpha0, ainterp);
1736 ainterp = lp_build_select(&bld8, mask1, alpha1, ainterp);
1737
1738 /* fix up val67 if a0 <= a1 */
1739 sel_mask2 = lp_build_compare(gallivm, type8, PIPE_FUNC_EQUAL,
1740 acodeg, lp_build_const_int_vec(gallivm, type8, 6));
1741 ares = LLVMBuildAnd(builder, ainterp, LLVMBuildNot(builder, sel_mask2, ""), "");
1742 sel_mask2 = lp_build_compare(gallivm, type8, PIPE_FUNC_EQUAL,
1743 acodeg, lp_build_const_int_vec(gallivm, type8, 7));
1744 ares = LLVMBuildOr(builder, ares, sel_mask2, "");
1745
1746 /* unpack in right order (0,4,8,12,1,5,..) */
1747 /* this gives us zero, a0, zero, a4, zero, a8, ... for tmp0 */
1748 tmp0 = lp_build_interleave2(gallivm, type8, bld8.zero, ares, 0);
1749 tmp1 = lp_build_interleave2(gallivm, type8, bld8.zero, ares, 1);
1750 tmp0 = LLVMBuildBitCast(builder, tmp0, bld16.vec_type, "");
1751 tmp1 = LLVMBuildBitCast(builder, tmp1, bld16.vec_type, "");
1752
1753 a[0] = lp_build_interleave2(gallivm, type16, bld16.zero, tmp0, 0);
1754 a[1] = lp_build_interleave2(gallivm, type16, bld16.zero, tmp1, 0);
1755 a[2] = lp_build_interleave2(gallivm, type16, bld16.zero, tmp0, 1);
1756 a[3] = lp_build_interleave2(gallivm, type16, bld16.zero, tmp1, 1);
1757 }
1758 else {
1759 LLVMValueRef elems[16], intrargs[2], shufa, mulclo, mulchi, mask8hi;
1760 LLVMTypeRef type16s = LLVMInt16TypeInContext(gallivm->context);
1761 LLVMTypeRef type8s = LLVMInt8TypeInContext(gallivm->context);
1762 unsigned i, j;
1763 /*
1764 * Ideally, we'd use 2 variable 16bit shifts here (byte shifts wouldn't
1765 * help since code crosses 8bit boundaries). But variable shifts are
1766 * AVX2 only, and even then only dword/quadword (intel _really_ hates
1767 * shifts!). Instead, emulate by 16bit muls.
1768 * Also, the required byte shuffles are essentially non-emulatable, so
1769 * require ssse3 (albeit other archs might do them fine, but the
1770 * complete path is ssse3 only for now).
1771 */
1772 for (i = 0, j = 0; i < 16; i += 8, j += 3) {
1773 elems[i+0] = elems[i+1] = elems[i+2] = lp_build_const_int32(gallivm, j+2);
1774 elems[i+3] = elems[i+4] = lp_build_const_int32(gallivm, j+3);
1775 elems[i+5] = elems[i+6] = elems[i+7] = lp_build_const_int32(gallivm, j+4);
1776 }
1777 shufa = LLVMConstVector(elems, 16);
1778 alpha = LLVMBuildBitCast(builder, alpha, bld8.vec_type, "");
1779 acode = LLVMBuildShuffleVector(builder, alpha, bld8.undef, shufa, "");
1780 acode = LLVMBuildBitCast(builder, acode, bld16.vec_type, "");
1781 /*
1782 * Put 0/2/4/6 into high 3 bits of 16 bits (save AND mask)
1783 * Do the same for 1/3/5/7 (albeit still need mask there - ideally
1784 * we'd place them into bits 4-7 so could save shift but impossible.)
1785 */
1786 for (i = 0; i < 8; i += 4) {
1787 elems[i+0] = LLVMConstInt(type16s, 1 << (13-0), 0);
1788 elems[i+1] = LLVMConstInt(type16s, 1 << (13-6), 0);
1789 elems[i+2] = LLVMConstInt(type16s, 1 << (13-4), 0);
1790 elems[i+3] = LLVMConstInt(type16s, 1 << (13-2), 0);
1791 }
1792 mulclo = LLVMConstVector(elems, 8);
1793 for (i = 0; i < 8; i += 4) {
1794 elems[i+0] = LLVMConstInt(type16s, 1 << (13-3), 0);
1795 elems[i+1] = LLVMConstInt(type16s, 1 << (13-9), 0);
1796 elems[i+2] = LLVMConstInt(type16s, 1 << (13-7), 0);
1797 elems[i+3] = LLVMConstInt(type16s, 1 << (13-5), 0);
1798 }
1799 mulchi = LLVMConstVector(elems, 8);
1800
1801 tmp0 = LLVMBuildMul(builder, acode, mulclo, "");
1802 tmp1 = LLVMBuildMul(builder, acode, mulchi, "");
1803 tmp0 = LLVMBuildLShr(builder, tmp0,
1804 lp_build_const_int_vec(gallivm, type16, 13), "");
1805 tmp1 = LLVMBuildLShr(builder, tmp1,
1806 lp_build_const_int_vec(gallivm, type16, 5), "");
1807 tmp1 = LLVMBuildAnd(builder, tmp1,
1808 lp_build_const_int_vec(gallivm, type16, 0x700), "");
1809 acode = LLVMBuildOr(builder, tmp0, tmp1, "");
1810 acode = LLVMBuildBitCast(builder, acode, bld8.vec_type, "");
1811
1812 /*
1813 * Note that ordering is different here to non-ssse3 path:
1814 * 0/1/2/3/4/5...
1815 */
1816
1817 LLVMValueRef weight0, weight1, weight, delta;
1818 LLVMValueRef constff_elem7, const0_elem6;
1819 /* weights, correctly rounded (round(256*x/7)) */
1820 elems[0] = LLVMConstInt(type16s, 256, 0);
1821 elems[1] = LLVMConstInt(type16s, 0, 0);
1822 elems[2] = LLVMConstInt(type16s, 219, 0);
1823 elems[3] = LLVMConstInt(type16s, 183, 0);
1824 elems[4] = LLVMConstInt(type16s, 146, 0);
1825 elems[5] = LLVMConstInt(type16s, 110, 0);
1826 elems[6] = LLVMConstInt(type16s, 73, 0);
1827 elems[7] = LLVMConstInt(type16s, 37, 0);
1828 weight0 = LLVMConstVector(elems, 8);
1829
1830 elems[0] = LLVMConstInt(type16s, 256, 0);
1831 elems[1] = LLVMConstInt(type16s, 0, 0);
1832 elems[2] = LLVMConstInt(type16s, 205, 0);
1833 elems[3] = LLVMConstInt(type16s, 154, 0);
1834 elems[4] = LLVMConstInt(type16s, 102, 0);
1835 elems[5] = LLVMConstInt(type16s, 51, 0);
1836 elems[6] = LLVMConstInt(type16s, 0, 0);
1837 elems[7] = LLVMConstInt(type16s, 0, 0);
1838 weight1 = LLVMConstVector(elems, 8);
1839
1840 weight0 = LLVMBuildBitCast(builder, weight0, bld8.vec_type, "");
1841 weight1 = LLVMBuildBitCast(builder, weight1, bld8.vec_type, "");
1842 weight = lp_build_select(&bld8, sel_mask, weight0, weight1);
1843 weight = LLVMBuildBitCast(builder, weight, bld16.vec_type, "");
1844
1845 for (i = 0; i < 16; i++) {
1846 elems[i] = LLVMConstNull(type8s);
1847 }
1848 elems[7] = LLVMConstInt(type8s, 255, 0);
1849 constff_elem7 = LLVMConstVector(elems, 16);
1850
1851 for (i = 0; i < 16; i++) {
1852 elems[i] = LLVMConstInt(type8s, 255, 0);
1853 }
1854 elems[6] = LLVMConstInt(type8s, 0, 0);
1855 const0_elem6 = LLVMConstVector(elems, 16);
1856
1857 /* standard simple lerp - but the version we need isn't available */
1858 delta = LLVMBuildSub(builder, alpha0, alpha1, "");
1859 ainterp = LLVMBuildMul(builder, delta, weight, "");
1860 ainterp = LLVMBuildLShr(builder, ainterp,
1861 lp_build_const_int_vec(gallivm, type16, 8), "");
1862 ainterp = LLVMBuildBitCast(builder, ainterp, bld8.vec_type, "");
1863 alpha1 = LLVMBuildBitCast(builder, alpha1, bld8.vec_type, "");
1864 ainterp = LLVMBuildAdd(builder, ainterp, alpha1, "");
1865 ainterp = LLVMBuildBitCast(builder, ainterp, bld16.vec_type, "");
1866 ainterp = lp_build_pack2(gallivm, type16, type8, ainterp, bld16.undef);
1867
1868 /* fixing 0/0xff case is slightly more complex */
1869 constff_elem7 = LLVMBuildAnd(builder, constff_elem7,
1870 LLVMBuildNot(builder, sel_mask, ""), "");
1871 const0_elem6 = LLVMBuildOr(builder, const0_elem6, sel_mask, "");
1872 ainterp = LLVMBuildOr(builder, ainterp, constff_elem7, "");
1873 ainterp = LLVMBuildAnd(builder, ainterp, const0_elem6, "");
1874
1875 /* now pick all 16 elements at once! */
1876 intrargs[0] = ainterp;
1877 intrargs[1] = acode;
1878 ares = lp_build_intrinsic(builder, "llvm.x86.ssse3.pshuf.b.128",
1879 bld8.vec_type, intrargs, 2, 0);
1880
1881 ares = LLVMBuildBitCast(builder, ares, i32t, "");
1882 mask8hi = lp_build_const_int_vec(gallivm, type32, 0xff000000);
1883 a[0] = LLVMBuildShl(builder, ares,
1884 lp_build_const_int_vec(gallivm, type32, 24), "");
1885 a[1] = LLVMBuildShl(builder, ares,
1886 lp_build_const_int_vec(gallivm, type32, 16), "");
1887 a[1] = LLVMBuildAnd(builder, a[1], mask8hi, "");
1888 a[2] = LLVMBuildShl(builder, ares,
1889 lp_build_const_int_vec(gallivm, type32, 8), "");
1890 a[2] = LLVMBuildAnd(builder, a[2], mask8hi, "");
1891 a[3] = LLVMBuildAnd(builder, ares, mask8hi, "");
1892 }
1893
1894 for (i = 0; i < 4; i++) {
1895 a[i] = LLVMBuildBitCast(builder, a[i], i32t, "");
1896 col[i] = LLVMBuildOr(builder, col[i], a[i], "");
1897 }
1898 }
1899
1900
1901 static void
1902 generate_update_cache_one_block(struct gallivm_state *gallivm,
1903 LLVMValueRef function,
1904 const struct util_format_description *format_desc)
1905 {
1906 LLVMBasicBlockRef block;
1907 LLVMBuilderRef old_builder;
1908 LLVMValueRef ptr_addr;
1909 LLVMValueRef hash_index;
1910 LLVMValueRef cache;
1911 LLVMValueRef dxt_block, tag_value;
1912 LLVMValueRef col[LP_MAX_VECTOR_LENGTH];
1913
1914 ptr_addr = LLVMGetParam(function, 0);
1915 hash_index = LLVMGetParam(function, 1);
1916 cache = LLVMGetParam(function, 2);
1917
1918 lp_build_name(ptr_addr, "ptr_addr" );
1919 lp_build_name(hash_index, "hash_index");
1920 lp_build_name(cache, "cache_addr");
1921
1922 /*
1923 * Function body
1924 */
1925
1926 old_builder = gallivm->builder;
1927 block = LLVMAppendBasicBlockInContext(gallivm->context, function, "entry");
1928 gallivm->builder = LLVMCreateBuilderInContext(gallivm->context);
1929 LLVMPositionBuilderAtEnd(gallivm->builder, block);
1930
1931 lp_build_gather_s3tc_simple_scalar(gallivm, format_desc, &dxt_block,
1932 ptr_addr);
1933
1934 switch (format_desc->format) {
1935 case PIPE_FORMAT_DXT1_RGB:
1936 case PIPE_FORMAT_DXT1_RGBA:
1937 case PIPE_FORMAT_DXT1_SRGB:
1938 case PIPE_FORMAT_DXT1_SRGBA:
1939 s3tc_decode_block_dxt1(gallivm, format_desc->format, dxt_block, col);
1940 break;
1941 case PIPE_FORMAT_DXT3_RGBA:
1942 case PIPE_FORMAT_DXT3_SRGBA:
1943 s3tc_decode_block_dxt3(gallivm, format_desc->format, dxt_block, col);
1944 break;
1945 case PIPE_FORMAT_DXT5_RGBA:
1946 case PIPE_FORMAT_DXT5_SRGBA:
1947 s3tc_decode_block_dxt5(gallivm, format_desc->format, dxt_block, col);
1948 break;
1949 default:
1950 assert(0);
1951 s3tc_decode_block_dxt1(gallivm, format_desc->format, dxt_block, col);
1952 break;
1953 }
1954
1955 tag_value = LLVMBuildPtrToInt(gallivm->builder, ptr_addr,
1956 LLVMInt64TypeInContext(gallivm->context), "");
1957 s3tc_store_cached_block(gallivm, col, tag_value, hash_index, cache);
1958
1959 LLVMBuildRetVoid(gallivm->builder);
1960
1961 LLVMDisposeBuilder(gallivm->builder);
1962 gallivm->builder = old_builder;
1963
1964 gallivm_verify_function(gallivm, function);
1965 }
1966
1967
1968 static void
1969 update_cached_block(struct gallivm_state *gallivm,
1970 const struct util_format_description *format_desc,
1971 LLVMValueRef ptr_addr,
1972 LLVMValueRef hash_index,
1973 LLVMValueRef cache)
1974
1975 {
1976 LLVMBuilderRef builder = gallivm->builder;
1977 LLVMModuleRef module = gallivm->module;
1978 char name[256];
1979 LLVMTypeRef i8t = LLVMInt8TypeInContext(gallivm->context);
1980 LLVMTypeRef pi8t = LLVMPointerType(i8t, 0);
1981 LLVMValueRef function, inst;
1982 LLVMBasicBlockRef bb;
1983 LLVMValueRef args[3];
1984
1985 snprintf(name, sizeof name, "%s_update_cache_one_block",
1986 format_desc->short_name);
1987 function = LLVMGetNamedFunction(module, name);
1988
1989 LLVMTypeRef ret_type = LLVMVoidTypeInContext(gallivm->context);
1990 LLVMTypeRef arg_types[3];
1991 arg_types[0] = pi8t;
1992 arg_types[1] = LLVMInt32TypeInContext(gallivm->context);
1993 arg_types[2] = LLVMTypeOf(cache); // XXX: put right type here
1994 LLVMTypeRef function_type = LLVMFunctionType(ret_type, arg_types, ARRAY_SIZE(arg_types), 0);
1995
1996 if (!function) {
1997 function = LLVMAddFunction(module, name, function_type);
1998
1999 for (unsigned arg = 0; arg < ARRAY_SIZE(arg_types); ++arg)
2000 if (LLVMGetTypeKind(arg_types[arg]) == LLVMPointerTypeKind)
2001 lp_add_function_attr(function, arg + 1, LP_FUNC_ATTR_NOALIAS);
2002
2003 LLVMSetFunctionCallConv(function, LLVMFastCallConv);
2004 LLVMSetVisibility(function, LLVMHiddenVisibility);
2005 generate_update_cache_one_block(gallivm, function, format_desc);
2006 }
2007
2008 args[0] = ptr_addr;
2009 args[1] = hash_index;
2010 args[2] = cache;
2011
2012 LLVMBuildCall2(builder, function_type, function, args, ARRAY_SIZE(args), "");
2013 bb = LLVMGetInsertBlock(builder);
2014 inst = LLVMGetLastInstruction(bb);
2015 LLVMSetInstructionCallConv(inst, LLVMFastCallConv);
2016 }
2017
2018 /*
2019 * cached lookup
2020 */
2021 static LLVMValueRef
2022 compressed_fetch_cached(struct gallivm_state *gallivm,
2023 const struct util_format_description *format_desc,
2024 unsigned n,
2025 LLVMValueRef base_ptr,
2026 LLVMValueRef offset,
2027 LLVMValueRef i,
2028 LLVMValueRef j,
2029 LLVMValueRef cache)
2030
2031 {
2032 LLVMBuilderRef builder = gallivm->builder;
2033 unsigned count, low_bit, log2size;
2034 LLVMValueRef color, offset_stored, addr, ptr_addrtrunc, tmp;
2035 LLVMValueRef ij_index, hash_index, hash_mask, block_index;
2036 LLVMTypeRef i8t = LLVMInt8TypeInContext(gallivm->context);
2037 LLVMTypeRef i32t = LLVMInt32TypeInContext(gallivm->context);
2038 LLVMTypeRef i64t = LLVMInt64TypeInContext(gallivm->context);
2039 struct lp_type type;
2040 struct lp_build_context bld32;
2041 memset(&type, 0, sizeof type);
2042 type.width = 32;
2043 type.length = n;
2044
2045 lp_build_context_init(&bld32, gallivm, type);
2046
2047 /*
2048 * compute hash - we use direct mapped cache, the hash function could
2049 * be better but it needs to be simple
2050 * per-element:
2051 * compare offset with offset stored at tag (hash)
2052 * if not equal extract block, store block, update tag
2053 * extract color from cache
2054 * assemble colors
2055 */
2056
2057 low_bit = util_logbase2(format_desc->block.bits / 8);
2058 log2size = util_logbase2(LP_BUILD_FORMAT_CACHE_SIZE);
2059 addr = LLVMBuildPtrToInt(builder, base_ptr, i64t, "");
2060 ptr_addrtrunc = LLVMBuildPtrToInt(builder, base_ptr, i32t, "");
2061 ptr_addrtrunc = lp_build_broadcast_scalar(&bld32, ptr_addrtrunc);
2062 /* For the hash function, first mask off the unused lowest bits. Then just
2063 do some xor with address bits - only use lower 32bits */
2064 ptr_addrtrunc = LLVMBuildAdd(builder, offset, ptr_addrtrunc, "");
2065 ptr_addrtrunc = LLVMBuildLShr(builder, ptr_addrtrunc,
2066 lp_build_const_int_vec(gallivm, type, low_bit), "");
2067 /* This only really makes sense for size 64,128,256 */
2068 hash_index = ptr_addrtrunc;
2069 ptr_addrtrunc = LLVMBuildLShr(builder, ptr_addrtrunc,
2070 lp_build_const_int_vec(gallivm, type, 2*log2size), "");
2071 hash_index = LLVMBuildXor(builder, ptr_addrtrunc, hash_index, "");
2072 tmp = LLVMBuildLShr(builder, hash_index,
2073 lp_build_const_int_vec(gallivm, type, log2size), "");
2074 hash_index = LLVMBuildXor(builder, hash_index, tmp, "");
2075
2076 hash_mask = lp_build_const_int_vec(gallivm, type, LP_BUILD_FORMAT_CACHE_SIZE - 1);
2077 hash_index = LLVMBuildAnd(builder, hash_index, hash_mask, "");
2078 ij_index = LLVMBuildShl(builder, i, lp_build_const_int_vec(gallivm, type, 2), "");
2079 ij_index = LLVMBuildAdd(builder, ij_index, j, "");
2080 block_index = LLVMBuildShl(builder, hash_index,
2081 lp_build_const_int_vec(gallivm, type, 4), "");
2082 block_index = LLVMBuildAdd(builder, ij_index, block_index, "");
2083
2084 if (n > 1) {
2085 color = bld32.undef;
2086 for (count = 0; count < n; count++) {
2087 LLVMValueRef index, cond, colorx;
2088 LLVMValueRef block_indexx, hash_indexx, addrx, offsetx, ptr_addrx;
2089 struct lp_build_if_state if_ctx;
2090
2091 index = lp_build_const_int32(gallivm, count);
2092 offsetx = LLVMBuildExtractElement(builder, offset, index, "");
2093 addrx = LLVMBuildZExt(builder, offsetx, i64t, "");
2094 addrx = LLVMBuildAdd(builder, addrx, addr, "");
2095 block_indexx = LLVMBuildExtractElement(builder, block_index, index, "");
2096 hash_indexx = LLVMBuildLShr(builder, block_indexx,
2097 lp_build_const_int32(gallivm, 4), "");
2098 offset_stored = s3tc_lookup_tag_data(gallivm, cache, hash_indexx);
2099 cond = LLVMBuildICmp(builder, LLVMIntNE, offset_stored, addrx, "");
2100
2101 lp_build_if(&if_ctx, gallivm, cond);
2102 {
2103 ptr_addrx = LLVMBuildIntToPtr(builder, addrx,
2104 LLVMPointerType(i8t, 0), "");
2105 update_cached_block(gallivm, format_desc, ptr_addrx, hash_indexx, cache);
2106 #if LP_BUILD_FORMAT_CACHE_DEBUG
2107 s3tc_update_cache_access(gallivm, cache, 1,
2108 LP_BUILD_FORMAT_CACHE_MEMBER_ACCESS_MISS);
2109 #endif
2110 }
2111 lp_build_endif(&if_ctx);
2112
2113 colorx = s3tc_lookup_cached_pixel(gallivm, cache, block_indexx);
2114
2115 color = LLVMBuildInsertElement(builder, color, colorx,
2116 lp_build_const_int32(gallivm, count), "");
2117 }
2118 }
2119 else {
2120 LLVMValueRef cond;
2121 struct lp_build_if_state if_ctx;
2122
2123 tmp = LLVMBuildZExt(builder, offset, i64t, "");
2124 addr = LLVMBuildAdd(builder, tmp, addr, "");
2125 offset_stored = s3tc_lookup_tag_data(gallivm, cache, hash_index);
2126 cond = LLVMBuildICmp(builder, LLVMIntNE, offset_stored, addr, "");
2127
2128 lp_build_if(&if_ctx, gallivm, cond);
2129 {
2130 tmp = LLVMBuildIntToPtr(builder, addr, LLVMPointerType(i8t, 0), "");
2131 update_cached_block(gallivm, format_desc, tmp, hash_index, cache);
2132 #if LP_BUILD_FORMAT_CACHE_DEBUG
2133 s3tc_update_cache_access(gallivm, cache, 1,
2134 LP_BUILD_FORMAT_CACHE_MEMBER_ACCESS_MISS);
2135 #endif
2136 }
2137 lp_build_endif(&if_ctx);
2138
2139 color = s3tc_lookup_cached_pixel(gallivm, cache, block_index);
2140 }
2141 #if LP_BUILD_FORMAT_CACHE_DEBUG
2142 s3tc_update_cache_access(gallivm, cache, n,
2143 LP_BUILD_FORMAT_CACHE_MEMBER_ACCESS_TOTAL);
2144 #endif
2145 return LLVMBuildBitCast(builder, color, LLVMVectorType(i8t, n * 4), "");
2146 }
2147
2148
2149 static LLVMValueRef
2150 s3tc_dxt5_to_rgba_aos(struct gallivm_state *gallivm,
2151 unsigned n,
2152 enum pipe_format format,
2153 LLVMValueRef colors,
2154 LLVMValueRef codewords,
2155 LLVMValueRef alpha_lo,
2156 LLVMValueRef alpha_hi,
2157 LLVMValueRef i,
2158 LLVMValueRef j)
2159 {
2160 return s3tc_dxt5_full_to_rgba_aos(gallivm, n, format, colors,
2161 codewords, alpha_lo, alpha_hi, i, j);
2162 }
2163
2164
2165 /**
2166 * @param n number of pixels processed (usually n=4, but it should also work with n=1
2167 * and multiples of 4)
2168 * @param base_ptr base pointer (32bit or 64bit pointer depending on the architecture)
2169 * @param offset <n x i32> vector with the relative offsets of the S3TC blocks
2170 * @param i is a <n x i32> vector with the x subpixel coordinate (0..3)
2171 * @param j is a <n x i32> vector with the y subpixel coordinate (0..3)
2172 * @return a <4*n x i8> vector with the pixel RGBA values in AoS
2173 */
2174 LLVMValueRef
2175 lp_build_fetch_s3tc_rgba_aos(struct gallivm_state *gallivm,
2176 const struct util_format_description *format_desc,
2177 unsigned n,
2178 LLVMValueRef base_ptr,
2179 LLVMValueRef offset,
2180 LLVMValueRef i,
2181 LLVMValueRef j,
2182 LLVMValueRef cache)
2183 {
2184 LLVMValueRef rgba;
2185 LLVMTypeRef i8t = LLVMInt8TypeInContext(gallivm->context);
2186 LLVMBuilderRef builder = gallivm->builder;
2187
2188 assert(format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC);
2189 assert(format_desc->block.width == 4);
2190 assert(format_desc->block.height == 4);
2191
2192 assert((n == 1) || (n % 4 == 0));
2193
2194 /* debug_printf("format = %d\n", format_desc->format);*/
2195 if (cache) {
2196 rgba = compressed_fetch_cached(gallivm, format_desc, n,
2197 base_ptr, offset, i, j, cache);
2198 return rgba;
2199 }
2200
2201 /*
2202 * Could use n > 8 here with avx2, but doesn't seem faster.
2203 */
2204 if (n > 4) {
2205 unsigned count;
2206 LLVMTypeRef i8_vectype = LLVMVectorType(i8t, 4 * n);
2207 LLVMTypeRef i128_type = LLVMIntTypeInContext(gallivm->context, 128);
2208 LLVMTypeRef i128_vectype = LLVMVectorType(i128_type, n / 4);
2209 LLVMTypeRef i324_vectype = LLVMVectorType(LLVMInt32TypeInContext(
2210 gallivm->context), 4);
2211 LLVMValueRef offset4, i4, j4, rgba4[LP_MAX_VECTOR_LENGTH/16];
2212 struct lp_type lp_324_vectype = lp_type_uint_vec(32, 128);
2213
2214 assert(n / 4 <= ARRAY_SIZE(rgba4));
2215
2216 rgba = LLVMGetUndef(i128_vectype);
2217
2218 for (count = 0; count < n / 4; count++) {
2219 LLVMValueRef colors, codewords, alpha_lo = NULL, alpha_hi = NULL;
2220
2221 i4 = lp_build_extract_range(gallivm, i, count * 4, 4);
2222 j4 = lp_build_extract_range(gallivm, j, count * 4, 4);
2223 offset4 = lp_build_extract_range(gallivm, offset, count * 4, 4);
2224
2225 lp_build_gather_s3tc(gallivm, 4, format_desc, &colors, &codewords,
2226 &alpha_lo, &alpha_hi, base_ptr, offset4);
2227
2228 switch (format_desc->format) {
2229 case PIPE_FORMAT_DXT1_RGB:
2230 case PIPE_FORMAT_DXT1_RGBA:
2231 case PIPE_FORMAT_DXT1_SRGB:
2232 case PIPE_FORMAT_DXT1_SRGBA:
2233 rgba4[count] = s3tc_dxt1_to_rgba_aos(gallivm, 4, format_desc->format,
2234 colors, codewords, i4, j4);
2235 break;
2236 case PIPE_FORMAT_DXT3_RGBA:
2237 case PIPE_FORMAT_DXT3_SRGBA:
2238 rgba4[count] = s3tc_dxt3_to_rgba_aos(gallivm, 4, format_desc->format, colors,
2239 codewords, alpha_lo, alpha_hi, i4, j4);
2240 break;
2241 case PIPE_FORMAT_DXT5_RGBA:
2242 case PIPE_FORMAT_DXT5_SRGBA:
2243 rgba4[count] = s3tc_dxt5_to_rgba_aos(gallivm, 4, format_desc->format, colors,
2244 codewords, alpha_lo, alpha_hi, i4, j4);
2245 break;
2246 default:
2247 assert(0);
2248 rgba4[count] = LLVMGetUndef(LLVMVectorType(i8t, 4));
2249 break;
2250 }
2251 /* shuffles typically give best results with dword elements...*/
2252 rgba4[count] = LLVMBuildBitCast(builder, rgba4[count], i324_vectype, "");
2253 }
2254 rgba = lp_build_concat(gallivm, rgba4, lp_324_vectype, n / 4);
2255 rgba = LLVMBuildBitCast(builder, rgba, i8_vectype, "");
2256 }
2257 else {
2258 LLVMValueRef colors, codewords, alpha_lo = NULL, alpha_hi = NULL;
2259
2260 lp_build_gather_s3tc(gallivm, n, format_desc, &colors, &codewords,
2261 &alpha_lo, &alpha_hi, base_ptr, offset);
2262
2263 switch (format_desc->format) {
2264 case PIPE_FORMAT_DXT1_RGB:
2265 case PIPE_FORMAT_DXT1_RGBA:
2266 case PIPE_FORMAT_DXT1_SRGB:
2267 case PIPE_FORMAT_DXT1_SRGBA:
2268 rgba = s3tc_dxt1_to_rgba_aos(gallivm, n, format_desc->format,
2269 colors, codewords, i, j);
2270 break;
2271 case PIPE_FORMAT_DXT3_RGBA:
2272 case PIPE_FORMAT_DXT3_SRGBA:
2273 rgba = s3tc_dxt3_to_rgba_aos(gallivm, n, format_desc->format, colors,
2274 codewords, alpha_lo, alpha_hi, i, j);
2275 break;
2276 case PIPE_FORMAT_DXT5_RGBA:
2277 case PIPE_FORMAT_DXT5_SRGBA:
2278 rgba = s3tc_dxt5_to_rgba_aos(gallivm, n, format_desc->format, colors,
2279 codewords, alpha_lo, alpha_hi, i, j);
2280 break;
2281 default:
2282 assert(0);
2283 rgba = LLVMGetUndef(LLVMVectorType(i8t, 4*n));
2284 break;
2285 }
2286 }
2287
2288 /* always return just decompressed values - srgb conversion is done later */
2289
2290 return rgba;
2291 }
2292
2293 /**
2294 * Gather elements from scatter positions in memory into vectors.
2295 * This is customised for fetching texels from s3tc textures.
2296 * For SSE, typical value is length=4.
2297 *
2298 * @param length length of the offsets
2299 * @param colors the stored colors of the blocks will be extracted into this.
2300 * @param codewords the codewords of the blocks will be extracted into this.
2301 * @param alpha_lo used for storing lower 32bit of alpha components for dxt3/5
2302 * @param alpha_hi used for storing higher 32bit of alpha components for dxt3/5
2303 * @param base_ptr base pointer, should be a i8 pointer type.
2304 * @param offsets vector with offsets
2305 */
2306 static void
2307 lp_build_gather_rgtc(struct gallivm_state *gallivm,
2308 unsigned length,
2309 const struct util_format_description *format_desc,
2310 LLVMValueRef *red_lo, LLVMValueRef *red_hi,
2311 LLVMValueRef *green_lo, LLVMValueRef *green_hi,
2312 LLVMValueRef base_ptr,
2313 LLVMValueRef offsets)
2314 {
2315 LLVMBuilderRef builder = gallivm->builder;
2316 unsigned block_bits = format_desc->block.bits;
2317 unsigned i;
2318 LLVMValueRef elems[8];
2319 LLVMTypeRef type32 = LLVMInt32TypeInContext(gallivm->context);
2320 LLVMTypeRef type64 = LLVMInt64TypeInContext(gallivm->context);
2321 LLVMTypeRef type32dxt;
2322 struct lp_type lp_type32dxt;
2323
2324 memset(&lp_type32dxt, 0, sizeof lp_type32dxt);
2325 lp_type32dxt.width = 32;
2326 lp_type32dxt.length = block_bits / 32;
2327 type32dxt = lp_build_vec_type(gallivm, lp_type32dxt);
2328
2329 assert(block_bits == 64 || block_bits == 128);
2330 assert(length == 1 || length == 4 || length == 8);
2331
2332 for (i = 0; i < length; ++i) {
2333 elems[i] = lp_build_gather_elem(gallivm, length,
2334 block_bits, block_bits, true,
2335 base_ptr, offsets, i, false);
2336 elems[i] = LLVMBuildBitCast(builder, elems[i], type32dxt, "");
2337 }
2338 if (length == 1) {
2339 LLVMValueRef elem = elems[0];
2340
2341 *red_lo = LLVMBuildExtractElement(builder, elem,
2342 lp_build_const_int32(gallivm, 0), "");
2343 *red_hi = LLVMBuildExtractElement(builder, elem,
2344 lp_build_const_int32(gallivm, 1), "");
2345
2346 if (block_bits == 128) {
2347 *green_lo = LLVMBuildExtractElement(builder, elem,
2348 lp_build_const_int32(gallivm, 2), "");
2349 *green_hi = LLVMBuildExtractElement(builder, elem,
2350 lp_build_const_int32(gallivm, 3), "");
2351 } else {
2352 *green_lo = NULL;
2353 *green_hi = NULL;
2354 }
2355 } else {
2356 LLVMValueRef tmp[4];
2357 struct lp_type lp_type32, lp_type64;
2358 memset(&lp_type32, 0, sizeof lp_type32);
2359 lp_type32.width = 32;
2360 lp_type32.length = length;
2361 lp_type32.sign = lp_type32dxt.sign;
2362 memset(&lp_type64, 0, sizeof lp_type64);
2363 lp_type64.width = 64;
2364 lp_type64.length = length/2;
2365 if (block_bits == 128) {
2366 if (length == 8) {
2367 for (i = 0; i < 4; ++i) {
2368 tmp[0] = elems[i];
2369 tmp[1] = elems[i+4];
2370 elems[i] = lp_build_concat(gallivm, tmp, lp_type32dxt, 2);
2371 }
2372 }
2373 lp_build_transpose_aos(gallivm, lp_type32, elems, tmp);
2374 *green_lo = tmp[2];
2375 *green_hi = tmp[3];
2376 *red_lo = tmp[0];
2377 *red_hi = tmp[1];
2378 } else {
2379 LLVMValueRef red01, red23;
2380 LLVMTypeRef type64_vec = LLVMVectorType(type64, length/2);
2381 LLVMTypeRef type32_vec = LLVMVectorType(type32, length);
2382
2383 for (i = 0; i < length; ++i) {
2384 /* no-op shuffle */
2385 elems[i] = LLVMBuildShuffleVector(builder, elems[i],
2386 LLVMGetUndef(type32dxt),
2387 lp_build_const_extend_shuffle(gallivm, 2, 4), "");
2388 }
2389 if (length == 8) {
2390 struct lp_type lp_type32_4 = {0};
2391 lp_type32_4.width = 32;
2392 lp_type32_4.length = 4;
2393 for (i = 0; i < 4; ++i) {
2394 tmp[0] = elems[i];
2395 tmp[1] = elems[i+4];
2396 elems[i] = lp_build_concat(gallivm, tmp, lp_type32_4, 2);
2397 }
2398 }
2399 red01 = lp_build_interleave2_half(gallivm, lp_type32, elems[0], elems[1], 0);
2400 red23 = lp_build_interleave2_half(gallivm, lp_type32, elems[2], elems[3], 0);
2401 red01 = LLVMBuildBitCast(builder, red01, type64_vec, "");
2402 red23 = LLVMBuildBitCast(builder, red23, type64_vec, "");
2403 *red_lo = lp_build_interleave2_half(gallivm, lp_type64, red01, red23, 0);
2404 *red_hi = lp_build_interleave2_half(gallivm, lp_type64, red01, red23, 1);
2405 *red_lo = LLVMBuildBitCast(builder, *red_lo, type32_vec, "");
2406 *red_hi = LLVMBuildBitCast(builder, *red_hi, type32_vec, "");
2407 *green_lo = NULL;
2408 *green_hi = NULL;
2409 }
2410 }
2411 }
2412
2413 static LLVMValueRef
2414 rgtc1_to_rgba_aos(struct gallivm_state *gallivm,
2415 unsigned n,
2416 enum pipe_format format,
2417 LLVMValueRef red_lo,
2418 LLVMValueRef red_hi,
2419 LLVMValueRef i,
2420 LLVMValueRef j)
2421 {
2422 LLVMBuilderRef builder = gallivm->builder;
2423 bool is_signed = (format == PIPE_FORMAT_RGTC1_SNORM);
2424 LLVMValueRef red = s3tc_dxt5_alpha_channel(gallivm, is_signed, n, red_hi, red_lo, i, j);
2425 LLVMValueRef rgba;
2426 struct lp_type type, type8;
2427 memset(&type, 0, sizeof type);
2428 type.width = 32;
2429 type.length = n;
2430 memset(&type8, 0, sizeof type8);
2431 type8.width = 8;
2432 type8.length = n*4;
2433 rgba = lp_build_const_int_vec(gallivm, type, is_signed ? (0x7f << 24) : (0xffu << 24));
2434 rgba = LLVMBuildOr(builder, rgba, red, "");
2435 return LLVMBuildBitCast(builder, rgba, lp_build_vec_type(gallivm, type8), "");
2436 }
2437
2438 static LLVMValueRef
2439 rgtc2_to_rgba_aos(struct gallivm_state *gallivm,
2440 unsigned n,
2441 enum pipe_format format,
2442 LLVMValueRef red_lo,
2443 LLVMValueRef red_hi,
2444 LLVMValueRef green_lo,
2445 LLVMValueRef green_hi,
2446 LLVMValueRef i,
2447 LLVMValueRef j)
2448 {
2449 LLVMBuilderRef builder = gallivm->builder;
2450 bool is_signed = (format == PIPE_FORMAT_RGTC2_SNORM);
2451 LLVMValueRef red = s3tc_dxt5_alpha_channel(gallivm, is_signed, n, red_hi, red_lo, i, j);
2452 LLVMValueRef green = s3tc_dxt5_alpha_channel(gallivm, is_signed, n, green_hi, green_lo, i, j);
2453 LLVMValueRef rgba;
2454 struct lp_type type, type8;
2455 memset(&type, 0, sizeof type);
2456 type.width = 32;
2457 type.length = n;
2458 memset(&type8, 0, sizeof type8);
2459 type8.width = 8;
2460 type8.length = n*4;
2461 rgba = lp_build_const_int_vec(gallivm, type, is_signed ? (0x7f << 24) : (0xffu << 24));
2462 rgba = LLVMBuildOr(builder, rgba, red, "");
2463 green = LLVMBuildShl(builder, green, lp_build_const_int_vec(gallivm, type, 8), "");
2464 rgba = LLVMBuildOr(builder, rgba, green, "");
2465 return LLVMBuildBitCast(builder, rgba, lp_build_vec_type(gallivm, type8), "");
2466 }
2467
2468 static LLVMValueRef
2469 latc1_to_rgba_aos(struct gallivm_state *gallivm,
2470 unsigned n,
2471 enum pipe_format format,
2472 LLVMValueRef red_lo,
2473 LLVMValueRef red_hi,
2474 LLVMValueRef i,
2475 LLVMValueRef j)
2476 {
2477 LLVMBuilderRef builder = gallivm->builder;
2478 bool is_signed = (format == PIPE_FORMAT_LATC1_SNORM);
2479 LLVMValueRef red = s3tc_dxt5_alpha_channel(gallivm, is_signed, n, red_hi, red_lo, i, j);
2480 LLVMValueRef rgba, temp;
2481 struct lp_type type, type8;
2482 memset(&type, 0, sizeof type);
2483 type.width = 32;
2484 type.length = n;
2485 memset(&type8, 0, sizeof type8);
2486 type8.width = 8;
2487 type8.length = n*4;
2488 rgba = lp_build_const_int_vec(gallivm, type, is_signed ? (0x7f << 24) : (0xffu << 24));
2489 rgba = LLVMBuildOr(builder, rgba, red, "");
2490 temp = LLVMBuildShl(builder, red, lp_build_const_int_vec(gallivm, type, 8), "");
2491 rgba = LLVMBuildOr(builder, rgba, temp, "");
2492 temp = LLVMBuildShl(builder, red, lp_build_const_int_vec(gallivm, type, 16), "");
2493 rgba = LLVMBuildOr(builder, rgba, temp, "");
2494 return LLVMBuildBitCast(builder, rgba, lp_build_vec_type(gallivm, type8), "");
2495 }
2496
2497 static LLVMValueRef
2498 latc2_to_rgba_aos(struct gallivm_state *gallivm,
2499 unsigned n,
2500 enum pipe_format format,
2501 LLVMValueRef red_lo,
2502 LLVMValueRef red_hi,
2503 LLVMValueRef green_lo,
2504 LLVMValueRef green_hi,
2505 LLVMValueRef i,
2506 LLVMValueRef j)
2507 {
2508 LLVMBuilderRef builder = gallivm->builder;
2509 bool is_signed = (format == PIPE_FORMAT_LATC2_SNORM);
2510 LLVMValueRef red = s3tc_dxt5_alpha_channel(gallivm, is_signed, n, red_hi, red_lo, i, j);
2511 LLVMValueRef green = s3tc_dxt5_alpha_channel(gallivm, is_signed, n, green_hi, green_lo, i, j);
2512 LLVMValueRef rgba, temp;
2513 struct lp_type type, type8;
2514 memset(&type, 0, sizeof type);
2515 type.width = 32;
2516 type.length = n;
2517 memset(&type8, 0, sizeof type8);
2518 type8.width = 8;
2519 type8.length = n*4;
2520
2521 temp = LLVMBuildShl(builder, red, lp_build_const_int_vec(gallivm, type, 8), "");
2522 rgba = LLVMBuildOr(builder, red, temp, "");
2523 temp = LLVMBuildShl(builder, red, lp_build_const_int_vec(gallivm, type, 16), "");
2524 rgba = LLVMBuildOr(builder, rgba, temp, "");
2525 temp = LLVMBuildShl(builder, green, lp_build_const_int_vec(gallivm, type, 24), "");
2526 rgba = LLVMBuildOr(builder, rgba, temp, "");
2527 return LLVMBuildBitCast(builder, rgba, lp_build_vec_type(gallivm, type8), "");
2528 }
2529
2530 /**
2531 * @param n number of pixels processed (usually n=4, but it should also work with n=1
2532 * and multiples of 4)
2533 * @param base_ptr base pointer (32bit or 64bit pointer depending on the architecture)
2534 * @param offset <n x i32> vector with the relative offsets of the S3TC blocks
2535 * @param i is a <n x i32> vector with the x subpixel coordinate (0..3)
2536 * @param j is a <n x i32> vector with the y subpixel coordinate (0..3)
2537 * @return a <4*n x i8> vector with the pixel RGBA values in AoS
2538 */
2539 LLVMValueRef
2540 lp_build_fetch_rgtc_rgba_aos(struct gallivm_state *gallivm,
2541 const struct util_format_description *format_desc,
2542 unsigned n,
2543 LLVMValueRef base_ptr,
2544 LLVMValueRef offset,
2545 LLVMValueRef i,
2546 LLVMValueRef j,
2547 LLVMValueRef cache)
2548 {
2549 LLVMValueRef rgba;
2550 LLVMTypeRef i8t = LLVMInt8TypeInContext(gallivm->context);
2551 LLVMBuilderRef builder = gallivm->builder;
2552 LLVMValueRef red_lo, red_hi, green_lo, green_hi;
2553 assert(format_desc->layout == UTIL_FORMAT_LAYOUT_RGTC);
2554 assert(format_desc->block.width == 4);
2555 assert(format_desc->block.height == 4);
2556
2557 assert((n == 1) || (n % 4 == 0));
2558
2559 if (n > 4) {
2560 unsigned count;
2561 LLVMTypeRef i128_type = LLVMIntTypeInContext(gallivm->context, 128);
2562 LLVMTypeRef i128_vectype = LLVMVectorType(i128_type, n / 4);
2563 LLVMTypeRef i8_vectype = LLVMVectorType(i8t, 4 * n);
2564 LLVMTypeRef i324_vectype = LLVMVectorType(LLVMInt32TypeInContext(
2565 gallivm->context), 4);
2566 LLVMValueRef offset4, i4, j4, rgba4[LP_MAX_VECTOR_LENGTH/16];
2567 struct lp_type lp_324_vectype = lp_type_uint_vec(32, 128);
2568
2569 rgba = LLVMGetUndef(i128_vectype);
2570
2571 for (count = 0; count < n / 4; count++) {
2572
2573 i4 = lp_build_extract_range(gallivm, i, count * 4, 4);
2574 j4 = lp_build_extract_range(gallivm, j, count * 4, 4);
2575 offset4 = lp_build_extract_range(gallivm, offset, count * 4, 4);
2576
2577 lp_build_gather_rgtc(gallivm, 4, format_desc, &red_lo, &red_hi,
2578 &green_lo, &green_hi, base_ptr, offset4);
2579
2580 switch (format_desc->format) {
2581 case PIPE_FORMAT_RGTC1_UNORM:
2582 case PIPE_FORMAT_RGTC1_SNORM:
2583 rgba4[count] = rgtc1_to_rgba_aos(gallivm, 4, format_desc->format,
2584 red_lo, red_hi, i4, j4);
2585 break;
2586 case PIPE_FORMAT_RGTC2_UNORM:
2587 case PIPE_FORMAT_RGTC2_SNORM:
2588 rgba4[count] = rgtc2_to_rgba_aos(gallivm, 4, format_desc->format,
2589 red_lo, red_hi, green_lo, green_hi, i4, j4);
2590 break;
2591 case PIPE_FORMAT_LATC1_UNORM:
2592 case PIPE_FORMAT_LATC1_SNORM:
2593 rgba4[count] = latc1_to_rgba_aos(gallivm, 4, format_desc->format,
2594 red_lo, red_hi, i4, j4);
2595 break;
2596 case PIPE_FORMAT_LATC2_UNORM:
2597 case PIPE_FORMAT_LATC2_SNORM:
2598 rgba4[count] = latc2_to_rgba_aos(gallivm, 4, format_desc->format,
2599 red_lo, red_hi, green_lo, green_hi, i4, j4);
2600 break;
2601 default:
2602 assert(0);
2603 rgba4[count] = LLVMGetUndef(LLVMVectorType(i8t, 4));
2604 break;
2605 }
2606 /* shuffles typically give best results with dword elements...*/
2607 rgba4[count] = LLVMBuildBitCast(builder, rgba4[count], i324_vectype, "");
2608 }
2609 rgba = lp_build_concat(gallivm, rgba4, lp_324_vectype, n / 4);
2610 rgba = LLVMBuildBitCast(builder, rgba, i8_vectype, "");
2611 } else {
2612 LLVMValueRef red_lo, red_hi, green_lo, green_hi;
2613
2614 lp_build_gather_rgtc(gallivm, n, format_desc, &red_lo, &red_hi,
2615 &green_lo, &green_hi, base_ptr, offset);
2616
2617 switch (format_desc->format) {
2618 case PIPE_FORMAT_RGTC1_UNORM:
2619 case PIPE_FORMAT_RGTC1_SNORM:
2620 rgba = rgtc1_to_rgba_aos(gallivm, n, format_desc->format,
2621 red_lo, red_hi, i, j);
2622 break;
2623 case PIPE_FORMAT_RGTC2_UNORM:
2624 case PIPE_FORMAT_RGTC2_SNORM:
2625 rgba = rgtc2_to_rgba_aos(gallivm, n, format_desc->format,
2626 red_lo, red_hi, green_lo, green_hi, i, j);
2627 break;
2628 case PIPE_FORMAT_LATC1_UNORM:
2629 case PIPE_FORMAT_LATC1_SNORM:
2630 rgba = latc1_to_rgba_aos(gallivm, n, format_desc->format,
2631 red_lo, red_hi, i, j);
2632 break;
2633 case PIPE_FORMAT_LATC2_UNORM:
2634 case PIPE_FORMAT_LATC2_SNORM:
2635 rgba = latc2_to_rgba_aos(gallivm, n, format_desc->format,
2636 red_lo, red_hi, green_lo, green_hi, i, j);
2637 break;
2638 default:
2639 assert(0);
2640 rgba = LLVMGetUndef(LLVMVectorType(i8t, 4*n));
2641 break;
2642 }
2643 }
2644 return rgba;
2645 }