1 /**************************************************************************
3 * Copyright 2010-2018 VMware, Inc.
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:
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.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
26 **************************************************************************/
31 * s3tc pixel format manipulation.
33 * @author Roland Scheidegger <sroland@vmware.com>
37 #include "util/u_format.h"
38 #include "util/u_math.h"
39 #include "util/u_string.h"
40 #include "util/u_cpu_detect.h"
41 #include "util/u_debug.h"
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_printf.h"
53 #include "lp_bld_struct.h"
54 #include "lp_bld_swizzle.h"
55 #include "lp_bld_init.h"
56 #include "lp_bld_debug.h"
57 #include "lp_bld_intr.h"
61 * Reverse an interleave2_half
62 * (ie. pick every second element, independent lower/upper halfs)
63 * sse2 can only do that with 32bit (shufps) or larger elements
64 * natively. (Otherwise, and/pack (even) or shift/pack (odd)
65 * could be used, ideally llvm would do that for us.)
66 * XXX: Unfortunately, this does NOT translate to a shufps if those
67 * are int vectors (and casting will not help, llvm needs to recognize it
68 * as "real" float). Instead, llvm will use a pshufd/pshufd/punpcklqdq
69 * sequence which I'm pretty sure is a lot worse despite domain transition
70 * penalties with shufps (except maybe on Nehalem).
73 lp_build_uninterleave2_half(struct gallivm_state
*gallivm
,
79 LLVMValueRef shuffle
, elems
[LP_MAX_VECTOR_LENGTH
];
82 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
85 if (type
.length
* type
.width
== 256) {
86 assert(type
.length
>= 4);
87 for (i
= 0, j
= 0; i
< type
.length
; ++i
) {
88 if (i
== type
.length
/ 4) {
90 } else if (i
== type
.length
/ 2) {
92 } else if (i
== 3 * type
.length
/ 4) {
93 j
= 3 * type
.length
/ 4;
97 elems
[i
] = lp_build_const_int32(gallivm
, j
+ lo_hi
);
100 for (i
= 0; i
< type
.length
; ++i
) {
101 elems
[i
] = lp_build_const_int32(gallivm
, 2*i
+ lo_hi
);
105 shuffle
= LLVMConstVector(elems
, type
.length
);
107 return LLVMBuildShuffleVector(gallivm
->builder
, a
, b
, shuffle
, "");
113 * Build shuffle for extending vectors.
116 lp_build_const_extend_shuffle(struct gallivm_state
*gallivm
,
117 unsigned n
, unsigned length
)
119 LLVMValueRef elems
[LP_MAX_VECTOR_LENGTH
];
123 assert(length
<= LP_MAX_VECTOR_LENGTH
);
125 /* TODO: cache results in a static table */
127 for(i
= 0; i
< n
; i
++) {
128 elems
[i
] = lp_build_const_int32(gallivm
, i
);
130 for (i
= n
; i
< length
; i
++) {
131 elems
[i
] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
134 return LLVMConstVector(elems
, length
);
138 lp_build_const_unpackx2_shuffle(struct gallivm_state
*gallivm
, unsigned n
)
140 LLVMValueRef elems
[LP_MAX_VECTOR_LENGTH
];
143 assert(n
<= LP_MAX_VECTOR_LENGTH
);
145 /* TODO: cache results in a static table */
147 for(i
= 0, j
= 0; i
< n
; i
+= 2, ++j
) {
148 elems
[i
+ 0] = lp_build_const_int32(gallivm
, 0 + j
);
149 elems
[i
+ 1] = lp_build_const_int32(gallivm
, n
+ j
);
150 elems
[n
+ i
+ 0] = lp_build_const_int32(gallivm
, 0 + n
/2 + j
);
151 elems
[n
+ i
+ 1] = lp_build_const_int32(gallivm
, n
+ n
/2 + j
);
154 return LLVMConstVector(elems
, n
* 2);
158 * broadcast 1 element to all elements
161 lp_build_const_shuffle1(struct gallivm_state
*gallivm
,
162 unsigned index
, unsigned n
)
164 LLVMValueRef elems
[LP_MAX_VECTOR_LENGTH
];
167 assert(n
<= LP_MAX_VECTOR_LENGTH
);
169 /* TODO: cache results in a static table */
171 for (i
= 0; i
< n
; i
++) {
172 elems
[i
] = lp_build_const_int32(gallivm
, index
);
175 return LLVMConstVector(elems
, n
);
179 * move 1 element to pos 0, rest undef
182 lp_build_shuffle1undef(struct gallivm_state
*gallivm
,
183 LLVMValueRef a
, unsigned index
, unsigned n
)
185 LLVMValueRef elems
[LP_MAX_VECTOR_LENGTH
], shuf
;
188 assert(n
<= LP_MAX_VECTOR_LENGTH
);
190 elems
[0] = lp_build_const_int32(gallivm
, index
);
192 for (i
= 1; i
< n
; i
++) {
193 elems
[i
] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
195 shuf
= LLVMConstVector(elems
, n
);
197 return LLVMBuildShuffleVector(gallivm
->builder
, a
, a
, shuf
, "");
201 format_dxt1_variant(enum pipe_format format
)
203 return format
== PIPE_FORMAT_DXT1_RGB
||
204 format
== PIPE_FORMAT_DXT1_RGBA
||
205 format
== PIPE_FORMAT_DXT1_SRGB
||
206 format
== PIPE_FORMAT_DXT1_SRGBA
;
211 * Gather elements from scatter positions in memory into vectors.
212 * This is customised for fetching texels from s3tc textures.
213 * For SSE, typical value is length=4.
215 * @param length length of the offsets
216 * @param colors the stored colors of the blocks will be extracted into this.
217 * @param codewords the codewords of the blocks will be extracted into this.
218 * @param alpha_lo used for storing lower 32bit of alpha components for dxt3/5
219 * @param alpha_hi used for storing higher 32bit of alpha components for dxt3/5
220 * @param base_ptr base pointer, should be a i8 pointer type.
221 * @param offsets vector with offsets
224 lp_build_gather_s3tc(struct gallivm_state
*gallivm
,
226 const struct util_format_description
*format_desc
,
227 LLVMValueRef
*colors
,
228 LLVMValueRef
*codewords
,
229 LLVMValueRef
*alpha_lo
,
230 LLVMValueRef
*alpha_hi
,
231 LLVMValueRef base_ptr
,
232 LLVMValueRef offsets
)
234 LLVMBuilderRef builder
= gallivm
->builder
;
235 unsigned block_bits
= format_desc
->block
.bits
;
237 LLVMValueRef elems
[8];
238 LLVMTypeRef type32
= LLVMInt32TypeInContext(gallivm
->context
);
239 LLVMTypeRef type64
= LLVMInt64TypeInContext(gallivm
->context
);
240 LLVMTypeRef type32dxt
;
241 struct lp_type lp_type32dxt
;
243 memset(&lp_type32dxt
, 0, sizeof lp_type32dxt
);
244 lp_type32dxt
.width
= 32;
245 lp_type32dxt
.length
= block_bits
/ 32;
246 type32dxt
= lp_build_vec_type(gallivm
, lp_type32dxt
);
248 assert(block_bits
== 64 || block_bits
== 128);
249 assert(length
== 1 || length
== 4 || length
== 8);
251 for (i
= 0; i
< length
; ++i
) {
252 elems
[i
] = lp_build_gather_elem(gallivm
, length
,
253 block_bits
, block_bits
, TRUE
,
254 base_ptr
, offsets
, i
, FALSE
);
255 elems
[i
] = LLVMBuildBitCast(builder
, elems
[i
], type32dxt
, "");
258 LLVMValueRef elem
= elems
[0];
259 if (block_bits
== 128) {
260 *alpha_lo
= LLVMBuildExtractElement(builder
, elem
,
261 lp_build_const_int32(gallivm
, 0), "");
262 *alpha_hi
= LLVMBuildExtractElement(builder
, elem
,
263 lp_build_const_int32(gallivm
, 1), "");
264 *colors
= LLVMBuildExtractElement(builder
, elem
,
265 lp_build_const_int32(gallivm
, 2), "");
266 *codewords
= LLVMBuildExtractElement(builder
, elem
,
267 lp_build_const_int32(gallivm
, 3), "");
270 *alpha_lo
= LLVMGetUndef(type32
);
271 *alpha_hi
= LLVMGetUndef(type32
);
272 *colors
= LLVMBuildExtractElement(builder
, elem
,
273 lp_build_const_int32(gallivm
, 0), "");
274 *codewords
= LLVMBuildExtractElement(builder
, elem
,
275 lp_build_const_int32(gallivm
, 1), "");
279 LLVMValueRef tmp
[4], cc01
, cc23
;
280 struct lp_type lp_type32
, lp_type64
, lp_type32dxt
;
281 memset(&lp_type32
, 0, sizeof lp_type32
);
282 lp_type32
.width
= 32;
283 lp_type32
.length
= length
;
284 memset(&lp_type64
, 0, sizeof lp_type64
);
285 lp_type64
.width
= 64;
286 lp_type64
.length
= length
/2;
288 if (block_bits
== 128) {
290 for (i
= 0; i
< 4; ++i
) {
293 elems
[i
] = lp_build_concat(gallivm
, tmp
, lp_type32dxt
, 2);
296 lp_build_transpose_aos(gallivm
, lp_type32
, elems
, tmp
);
302 LLVMTypeRef type64_vec
= LLVMVectorType(type64
, length
/2);
303 LLVMTypeRef type32_vec
= LLVMVectorType(type32
, length
);
305 for (i
= 0; i
< length
; ++i
) {
307 elems
[i
] = LLVMBuildShuffleVector(builder
, elems
[i
],
308 LLVMGetUndef(type32dxt
),
309 lp_build_const_extend_shuffle(gallivm
, 2, 4), "");
312 for (i
= 0; i
< 4; ++i
) {
315 elems
[i
] = lp_build_concat(gallivm
, tmp
, lp_type32
, 2);
318 cc01
= lp_build_interleave2_half(gallivm
, lp_type32
, elems
[0], elems
[1], 0);
319 cc23
= lp_build_interleave2_half(gallivm
, lp_type32
, elems
[2], elems
[3], 0);
320 cc01
= LLVMBuildBitCast(builder
, cc01
, type64_vec
, "");
321 cc23
= LLVMBuildBitCast(builder
, cc23
, type64_vec
, "");
322 *colors
= lp_build_interleave2_half(gallivm
, lp_type64
, cc01
, cc23
, 0);
323 *codewords
= lp_build_interleave2_half(gallivm
, lp_type64
, cc01
, cc23
, 1);
324 *colors
= LLVMBuildBitCast(builder
, *colors
, type32_vec
, "");
325 *codewords
= LLVMBuildBitCast(builder
, *codewords
, type32_vec
, "");
330 /** Convert from <n x i32> containing 2 x n rgb565 colors
331 * to 2 <n x i32> rgba8888 colors
332 * This is the most optimized version I can think of
333 * should be nearly as fast as decoding only one color
334 * NOTE: alpha channel will be set to 0
335 * @param colors is a <n x i32> vector containing the rgb565 colors
338 color_expand2_565_to_8888(struct gallivm_state
*gallivm
,
341 LLVMValueRef
*color0
,
342 LLVMValueRef
*color1
)
344 LLVMBuilderRef builder
= gallivm
->builder
;
345 LLVMValueRef r
, g
, b
, rblo
, glo
;
346 LLVMValueRef rgblomask
, rb
, rgb0
, rgb1
;
347 struct lp_type type
, type16
, type8
;
351 memset(&type
, 0, sizeof type
);
355 memset(&type16
, 0, sizeof type16
);
357 type16
.length
= 2 * n
;
359 memset(&type8
, 0, sizeof type8
);
361 type8
.length
= 4 * n
;
363 rgblomask
= lp_build_const_int_vec(gallivm
, type16
, 0x0707);
364 colors
= LLVMBuildBitCast(builder
, colors
,
365 lp_build_vec_type(gallivm
, type16
), "");
366 /* move r into low 8 bits, b into high 8 bits, g into another reg (low bits)
367 * make sure low bits of r are zero - could use AND but requires constant */
368 r
= LLVMBuildLShr(builder
, colors
, lp_build_const_int_vec(gallivm
, type16
, 11), "");
369 r
= LLVMBuildShl(builder
, r
, lp_build_const_int_vec(gallivm
, type16
, 3), "");
370 b
= LLVMBuildShl(builder
, colors
, lp_build_const_int_vec(gallivm
, type16
, 11), "");
371 rb
= LLVMBuildOr(builder
, r
, b
, "");
372 rblo
= LLVMBuildLShr(builder
, rb
, lp_build_const_int_vec(gallivm
, type16
, 5), "");
373 /* don't have byte shift hence need mask */
374 rblo
= LLVMBuildAnd(builder
, rblo
, rgblomask
, "");
375 rb
= LLVMBuildOr(builder
, rb
, rblo
, "");
377 /* make sure low bits of g are zero */
378 g
= LLVMBuildAnd(builder
, colors
, lp_build_const_int_vec(gallivm
, type16
, 0x07e0), "");
379 g
= LLVMBuildLShr(builder
, g
, lp_build_const_int_vec(gallivm
, type16
, 3), "");
380 glo
= LLVMBuildLShr(builder
, g
, lp_build_const_int_vec(gallivm
, type16
, 6), "");
381 g
= LLVMBuildOr(builder
, g
, glo
, "");
383 rb
= LLVMBuildBitCast(builder
, rb
, lp_build_vec_type(gallivm
, type8
), "");
384 g
= LLVMBuildBitCast(builder
, g
, lp_build_vec_type(gallivm
, type8
), "");
385 rgb0
= lp_build_interleave2_half(gallivm
, type8
, rb
, g
, 0);
386 rgb1
= lp_build_interleave2_half(gallivm
, type8
, rb
, g
, 1);
388 rgb0
= LLVMBuildBitCast(builder
, rgb0
, lp_build_vec_type(gallivm
, type
), "");
389 rgb1
= LLVMBuildBitCast(builder
, rgb1
, lp_build_vec_type(gallivm
, type
), "");
391 /* rgb0 is rgb00, rgb01, rgb10, rgb11
392 * instead of rgb00, rgb10, rgb20, rgb30 hence need reshuffle
393 * on x86 this _should_ just generate one shufps...
395 *color0
= lp_build_uninterleave2_half(gallivm
, type
, rgb0
, rgb1
, 0);
396 *color1
= lp_build_uninterleave2_half(gallivm
, type
, rgb0
, rgb1
, 1);
400 /** Convert from <n x i32> containing rgb565 colors
401 * (in first 16 bits) to <n x i32> rgba8888 colors
403 * NOTE: alpha channel will be set to 0
404 * @param colors is a <n x i32> vector containing the rgb565 colors
407 color_expand_565_to_8888(struct gallivm_state
*gallivm
,
411 LLVMBuilderRef builder
= gallivm
->builder
;
412 LLVMValueRef rgba
, r
, g
, b
, rgblo
, glo
;
413 LLVMValueRef rbhimask
, g6mask
, rgblomask
;
415 memset(&type
, 0, sizeof type
);
420 * first extract and shift colors into their final locations
421 * (high bits - low bits zero at this point)
422 * then replicate highest bits to the lowest bits
423 * note rb replication can be done in parallel but not g
425 * r5mask = 0xf800, g6mask = 0x07e0, b5mask = 0x001f
426 * rhigh = 8, ghigh = 5, bhigh = 19
427 * rblow = 5, glow = 6
428 * rgblowmask = 0x00070307
429 * r = colors >> rhigh
430 * b = colors << bhigh
431 * g = (colors & g6mask) << ghigh
432 * rb = (r | b) rbhimask
433 * rbtmp = rb >> rblow
435 * rbtmp = rbtmp | gtmp
436 * rbtmp = rbtmp & rgblowmask
437 * rgb = rb | g | rbtmp
439 g6mask
= lp_build_const_int_vec(gallivm
, type
, 0x07e0);
440 rbhimask
= lp_build_const_int_vec(gallivm
, type
, 0x00f800f8);
441 rgblomask
= lp_build_const_int_vec(gallivm
, type
, 0x00070307);
443 r
= LLVMBuildLShr(builder
, colors
, lp_build_const_int_vec(gallivm
, type
, 8), "");
444 b
= LLVMBuildShl(builder
, colors
, lp_build_const_int_vec(gallivm
, type
, 19), "");
445 g
= LLVMBuildAnd(builder
, colors
, g6mask
, "");
446 g
= LLVMBuildShl(builder
, g
, lp_build_const_int_vec(gallivm
, type
, 5), "");
447 rgba
= LLVMBuildOr(builder
, r
, b
, "");
448 rgba
= LLVMBuildAnd(builder
, rgba
, rbhimask
, "");
449 rgblo
= LLVMBuildLShr(builder
, rgba
, lp_build_const_int_vec(gallivm
, type
, 5), "");
450 glo
= LLVMBuildLShr(builder
, g
, lp_build_const_int_vec(gallivm
, type
, 6), "");
451 rgblo
= LLVMBuildOr(builder
, rgblo
, glo
, "");
452 rgblo
= LLVMBuildAnd(builder
, rgblo
, rgblomask
, "");
453 rgba
= LLVMBuildOr(builder
, rgba
, g
, "");
454 rgba
= LLVMBuildOr(builder
, rgba
, rgblo
, "");
461 * Calculate 1/3(v1-v0) + v0
462 * and 2*1/3(v1-v0) + v0
465 lp_build_lerp23(struct lp_build_context
*bld
,
471 struct gallivm_state
*gallivm
= bld
->gallivm
;
472 LLVMValueRef x
, x_lo
, x_hi
, delta_lo
, delta_hi
;
473 LLVMValueRef mul_lo
, mul_hi
, v0_lo
, v0_hi
, v1_lo
, v1_hi
, tmp
;
474 const struct lp_type type
= bld
->type
;
475 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
476 struct lp_type i16_type
= lp_wider_type(type
);
477 struct lp_build_context bld2
;
479 assert(lp_check_value(type
, v0
));
480 assert(lp_check_value(type
, v1
));
481 assert(!type
.floating
&& !type
.fixed
&& !type
.norm
&& type
.width
== 8);
483 lp_build_context_init(&bld2
, gallivm
, i16_type
);
484 bld2
.type
.sign
= TRUE
;
485 x
= lp_build_const_int_vec(gallivm
, bld
->type
, 255*1/3);
487 /* FIXME: use native avx256 unpack/pack */
488 lp_build_unpack2(gallivm
, type
, i16_type
, x
, &x_lo
, &x_hi
);
489 lp_build_unpack2(gallivm
, type
, i16_type
, v0
, &v0_lo
, &v0_hi
);
490 lp_build_unpack2(gallivm
, type
, i16_type
, v1
, &v1_lo
, &v1_hi
);
491 delta_lo
= lp_build_sub(&bld2
, v1_lo
, v0_lo
);
492 delta_hi
= lp_build_sub(&bld2
, v1_hi
, v0_hi
);
494 mul_lo
= LLVMBuildMul(builder
, x_lo
, delta_lo
, "");
495 mul_hi
= LLVMBuildMul(builder
, x_hi
, delta_hi
, "");
497 x_lo
= LLVMBuildLShr(builder
, mul_lo
, lp_build_const_int_vec(gallivm
, i16_type
, 8), "");
498 x_hi
= LLVMBuildLShr(builder
, mul_hi
, lp_build_const_int_vec(gallivm
, i16_type
, 8), "");
499 /* lerp optimization: pack now, do add afterwards */
500 tmp
= lp_build_pack2(gallivm
, i16_type
, type
, x_lo
, x_hi
);
501 *res0
= lp_build_add(bld
, tmp
, v0
);
503 x_lo
= LLVMBuildLShr(builder
, mul_lo
, lp_build_const_int_vec(gallivm
, i16_type
, 7), "");
504 x_hi
= LLVMBuildLShr(builder
, mul_hi
, lp_build_const_int_vec(gallivm
, i16_type
, 7), "");
505 /* unlike above still need mask (but add still afterwards). */
506 x_lo
= LLVMBuildAnd(builder
, x_lo
, lp_build_const_int_vec(gallivm
, i16_type
, 0xff), "");
507 x_hi
= LLVMBuildAnd(builder
, x_hi
, lp_build_const_int_vec(gallivm
, i16_type
, 0xff), "");
508 tmp
= lp_build_pack2(gallivm
, i16_type
, type
, x_lo
, x_hi
);
509 *res1
= lp_build_add(bld
, tmp
, v0
);
513 * Convert from <n x i64> s3tc dxt1 to <4n x i8> RGBA AoS
514 * @param colors is a <n x i32> vector with n x 2x16bit colors
515 * @param codewords is a <n x i32> vector containing the codewords
516 * @param i is a <n x i32> vector with the x pixel coordinate (0 to 3)
517 * @param j is a <n x i32> vector with the y pixel coordinate (0 to 3)
520 s3tc_dxt1_full_to_rgba_aos(struct gallivm_state
*gallivm
,
522 enum pipe_format format
,
524 LLVMValueRef codewords
,
528 LLVMBuilderRef builder
= gallivm
->builder
;
529 LLVMValueRef color0
, color1
, color2
, color3
, color2_2
, color3_2
;
530 LLVMValueRef rgba
, a
, colors0
, colors1
, col0
, col1
, const2
;
531 LLVMValueRef bit_pos
, sel_mask
, sel_lo
, sel_hi
, indices
;
532 struct lp_type type
, type8
;
533 struct lp_build_context bld8
, bld32
;
534 boolean is_dxt1_variant
= format_dxt1_variant(format
);
536 memset(&type
, 0, sizeof type
);
540 memset(&type8
, 0, sizeof type8
);
544 assert(lp_check_value(type
, i
));
545 assert(lp_check_value(type
, j
));
547 a
= lp_build_const_int_vec(gallivm
, type
, 0xff000000);
549 lp_build_context_init(&bld32
, gallivm
, type
);
550 lp_build_context_init(&bld8
, gallivm
, type8
);
554 * - expand color0/color1 to rgba8888
555 * - calculate color2/3 (interpolation) according to color0 < color1 rules
556 * - calculate color2/3 according to color0 >= color1 rules
557 * - do selection of color2/3 according to comparison of color0/1
558 * - extract indices (vector shift).
559 * - use compare/select to select the correct color. Since we have 2bit
560 * indices (and 4 colors), needs at least three compare/selects.
563 * expand the two colors
565 col0
= LLVMBuildAnd(builder
, colors
, lp_build_const_int_vec(gallivm
, type
, 0x0000ffff), "");
566 col1
= LLVMBuildLShr(builder
, colors
, lp_build_const_int_vec(gallivm
, type
, 16), "");
568 color_expand2_565_to_8888(gallivm
, n
, colors
, &color0
, &color1
);
571 color0
= color_expand_565_to_8888(gallivm
, n
, col0
);
572 color1
= color_expand_565_to_8888(gallivm
, n
, col1
);
577 * color2_1 is 2/3 color0 + 1/3 color1
578 * color3_1 is 1/3 color0 + 2/3 color1
579 * color2_2 is 1/2 color0 + 1/2 color1
583 colors0
= LLVMBuildBitCast(builder
, color0
, bld8
.vec_type
, "");
584 colors1
= LLVMBuildBitCast(builder
, color1
, bld8
.vec_type
, "");
585 /* can combine 2 lerps into one mostly - still looks expensive enough. */
586 lp_build_lerp23(&bld8
, colors0
, colors1
, &color2
, &color3
);
587 color2
= LLVMBuildBitCast(builder
, color2
, bld32
.vec_type
, "");
588 color3
= LLVMBuildBitCast(builder
, color3
, bld32
.vec_type
, "");
590 /* dxt3/5 always use 4-color encoding */
591 if (is_dxt1_variant
) {
593 if (format
== PIPE_FORMAT_DXT1_RGBA
||
594 format
== PIPE_FORMAT_DXT1_SRGBA
) {
595 color0
= LLVMBuildOr(builder
, color0
, a
, "");
596 color1
= LLVMBuildOr(builder
, color1
, a
, "");
597 color3
= LLVMBuildOr(builder
, color3
, a
, "");
600 * XXX with sse2 and 16x8 vectors, should use pavgb even when n == 1.
601 * Much cheaper (but we don't care that much if n == 1).
603 if ((util_cpu_caps
.has_sse2
&& n
== 4) ||
604 (util_cpu_caps
.has_avx2
&& n
== 8)) {
605 LLVMValueRef intrargs
[2];
606 char *intr_name
= n
== 8 ? "llvm.x86.avx2.pavg.b" :
607 "llvm.x86.sse2.pavg.b";
608 intrargs
[0] = colors0
;
609 intrargs
[1] = colors1
;
610 color2_2
= lp_build_intrinsic(builder
, intr_name
,
611 bld8
.vec_type
, intrargs
, 2, 0);
612 color2_2
= LLVMBuildBitCast(builder
, color2_2
, bld32
.vec_type
, "");
615 struct lp_type i16_type
= lp_wider_type(type8
);
616 struct lp_build_context bld2
;
617 LLVMValueRef v0_lo
, v0_hi
, v1_lo
, v1_hi
, addlo
, addhi
;
619 lp_build_context_init(&bld2
, gallivm
, i16_type
);
620 bld2
.type
.sign
= TRUE
;
623 * This isn't as expensive as it looks (the unpack is the same as
624 * for lerp23), with correct rounding.
625 * (Note that while rounding is correct, this will always round down,
626 * whereas pavgb will always round up.)
628 /* FIXME: use native avx256 unpack/pack */
629 lp_build_unpack2(gallivm
, type8
, i16_type
, colors0
, &v0_lo
, &v0_hi
);
630 lp_build_unpack2(gallivm
, type8
, i16_type
, colors1
, &v1_lo
, &v1_hi
);
632 addlo
= lp_build_add(&bld2
, v0_lo
, v1_lo
);
633 addhi
= lp_build_add(&bld2
, v0_hi
, v1_hi
);
634 addlo
= LLVMBuildLShr(builder
, addlo
,
635 lp_build_const_int_vec(gallivm
, i16_type
, 1), "");
636 addhi
= LLVMBuildLShr(builder
, addhi
,
637 lp_build_const_int_vec(gallivm
, i16_type
, 1), "");
638 color2_2
= lp_build_pack2(gallivm
, i16_type
, type8
, addlo
, addhi
);
639 color2_2
= LLVMBuildBitCast(builder
, color2_2
, bld32
.vec_type
, "");
641 color3_2
= lp_build_const_int_vec(gallivm
, type
, 0);
643 /* select between colors2/3 */
644 /* signed compare is faster saves some xors */
646 sel_mask
= lp_build_compare(gallivm
, type
, PIPE_FUNC_GREATER
, col0
, col1
);
647 color2
= lp_build_select(&bld32
, sel_mask
, color2
, color2_2
);
648 color3
= lp_build_select(&bld32
, sel_mask
, color3
, color3_2
);
651 if (format
== PIPE_FORMAT_DXT1_RGBA
||
652 format
== PIPE_FORMAT_DXT1_SRGBA
) {
653 color2
= LLVMBuildOr(builder
, color2
, a
, "");
657 const2
= lp_build_const_int_vec(gallivm
, type
, 2);
658 /* extract 2-bit index values */
659 bit_pos
= LLVMBuildShl(builder
, j
, const2
, "");
660 bit_pos
= LLVMBuildAdd(builder
, bit_pos
, i
, "");
661 bit_pos
= LLVMBuildAdd(builder
, bit_pos
, bit_pos
, "");
663 * NOTE: This innocent looking shift is very expensive with x86/ssex.
664 * Shifts with per-elemnent shift count get roughly translated to
665 * extract (count), extract (value), shift, move (back to xmm), unpack
667 * So about 20 instructions here for 4xi32.
668 * Newer llvm versions (3.7+) will not do extract/insert but use a
669 * a couple constant count vector shifts plus shuffles. About same
670 * amount of instructions unfortunately...
671 * Would get much worse with 8xi16 even...
672 * We could actually do better here:
673 * - subtract bit_pos from 128+30, shl 23, convert float to int...
674 * - now do mul with codewords followed by shr 30...
675 * But requires 32bit->32bit mul, sse41 only (well that's emulatable
676 * with 2 32bit->64bit muls...) and not exactly cheap
677 * AVX2, of course, fixes this nonsense.
679 indices
= LLVMBuildLShr(builder
, codewords
, bit_pos
, "");
681 /* finally select the colors */
682 sel_lo
= LLVMBuildAnd(builder
, indices
, bld32
.one
, "");
683 sel_lo
= lp_build_compare(gallivm
, type
, PIPE_FUNC_EQUAL
, sel_lo
, bld32
.one
);
684 color0
= lp_build_select(&bld32
, sel_lo
, color1
, color0
);
685 color2
= lp_build_select(&bld32
, sel_lo
, color3
, color2
);
686 sel_hi
= LLVMBuildAnd(builder
, indices
, const2
, "");
687 sel_hi
= lp_build_compare(gallivm
, type
, PIPE_FUNC_EQUAL
, sel_hi
, const2
);
688 rgba
= lp_build_select(&bld32
, sel_hi
, color2
, color0
);
691 if (format
== PIPE_FORMAT_DXT1_RGB
||
692 format
== PIPE_FORMAT_DXT1_SRGB
) {
693 rgba
= LLVMBuildOr(builder
, rgba
, a
, "");
695 return LLVMBuildBitCast(builder
, rgba
, bld8
.vec_type
, "");
700 s3tc_dxt1_to_rgba_aos(struct gallivm_state
*gallivm
,
702 enum pipe_format format
,
704 LLVMValueRef codewords
,
708 return s3tc_dxt1_full_to_rgba_aos(gallivm
, n
, format
,
709 colors
, codewords
, i
, j
);
714 * Convert from <n x i128> s3tc dxt3 to <4n x i8> RGBA AoS
715 * @param colors is a <n x i32> vector with n x 2x16bit colors
716 * @param codewords is a <n x i32> vector containing the codewords
717 * @param alphas is a <n x i64> vector containing the alpha values
718 * @param i is a <n x i32> vector with the x pixel coordinate (0 to 3)
719 * @param j is a <n x i32> vector with the y pixel coordinate (0 to 3)
722 s3tc_dxt3_to_rgba_aos(struct gallivm_state
*gallivm
,
724 enum pipe_format format
,
726 LLVMValueRef codewords
,
727 LLVMValueRef alpha_low
,
728 LLVMValueRef alpha_hi
,
732 LLVMBuilderRef builder
= gallivm
->builder
;
733 LLVMValueRef rgba
, tmp
, tmp2
;
734 LLVMValueRef bit_pos
, sel_mask
;
735 struct lp_type type
, type8
;
736 struct lp_build_context bld
;
738 memset(&type
, 0, sizeof type
);
742 memset(&type8
, 0, sizeof type8
);
746 assert(lp_check_value(type
, i
));
747 assert(lp_check_value(type
, j
));
749 lp_build_context_init(&bld
, gallivm
, type
);
751 rgba
= s3tc_dxt1_to_rgba_aos(gallivm
, n
, format
,
752 colors
, codewords
, i
, j
);
754 rgba
= LLVMBuildBitCast(builder
, rgba
, bld
.vec_type
, "");
757 * Extract alpha values. Since we now need to select from
758 * which 32bit vector values are fetched, construct selection
759 * mask from highest bit of bit_pos, and use select, then shift
760 * according to the bit_pos (without the highest bit).
761 * Note this is pointless for n == 1 case. Could just
762 * directly use 64bit arithmetic if we'd extract 64bit
763 * alpha value instead of 2x32...
766 bit_pos
= LLVMBuildShl(builder
, j
, lp_build_const_int_vec(gallivm
, type
, 2), "");
767 bit_pos
= LLVMBuildAdd(builder
, bit_pos
, i
, "");
768 bit_pos
= LLVMBuildShl(builder
, bit_pos
,
769 lp_build_const_int_vec(gallivm
, type
, 2), "");
770 sel_mask
= LLVMBuildLShr(builder
, bit_pos
,
771 lp_build_const_int_vec(gallivm
, type
, 5), "");
772 sel_mask
= LLVMBuildSub(builder
, sel_mask
, bld
.one
, "");
773 tmp
= lp_build_select(&bld
, sel_mask
, alpha_low
, alpha_hi
);
774 bit_pos
= LLVMBuildAnd(builder
, bit_pos
,
775 lp_build_const_int_vec(gallivm
, type
, 0xffffffdf), "");
776 /* Warning: slow shift with per element count */
778 * Could do pshufb here as well - just use appropriate 2 bits in bit_pos
779 * to select the right byte with pshufb. Then for the remaining one bit
780 * just do shift/select.
782 tmp
= LLVMBuildLShr(builder
, tmp
, bit_pos
, "");
784 /* combined expand from a4 to a8 and shift into position */
785 tmp
= LLVMBuildShl(builder
, tmp
, lp_build_const_int_vec(gallivm
, type
, 28), "");
786 tmp2
= LLVMBuildLShr(builder
, tmp
, lp_build_const_int_vec(gallivm
, type
, 4), "");
787 tmp
= LLVMBuildOr(builder
, tmp
, tmp2
, "");
789 rgba
= LLVMBuildOr(builder
, tmp
, rgba
, "");
791 return LLVMBuildBitCast(builder
, rgba
, lp_build_vec_type(gallivm
, type8
), "");
795 lp_build_lerpdxta(struct gallivm_state
*gallivm
,
799 LLVMValueRef sel_mask
,
803 * note we're doing lerp in 16bit since 32bit pmulld is only available in sse41
804 * (plus pmullw is actually faster...)
805 * we just pretend our 32bit values (which are really only 8bit) are 16bits.
806 * Note that this is obviously a disaster for the scalar case.
808 LLVMBuilderRef builder
= gallivm
->builder
;
809 LLVMValueRef delta
, ainterp
;
810 LLVMValueRef weight5
, weight7
, weight
;
811 struct lp_type type32
, type16
, type8
;
812 struct lp_build_context bld16
;
814 memset(&type32
, 0, sizeof type32
);
817 memset(&type16
, 0, sizeof type16
);
821 memset(&type8
, 0, sizeof type8
);
825 lp_build_context_init(&bld16
, gallivm
, type16
);
826 /* 255/7 is a bit off - increase accuracy at the expense of shift later */
827 sel_mask
= LLVMBuildBitCast(builder
, sel_mask
, bld16
.vec_type
, "");
828 weight5
= lp_build_const_int_vec(gallivm
, type16
, 255*64/5);
829 weight7
= lp_build_const_int_vec(gallivm
, type16
, 255*64/7);
830 weight
= lp_build_select(&bld16
, sel_mask
, weight7
, weight5
);
832 alpha0
= LLVMBuildBitCast(builder
, alpha0
, bld16
.vec_type
, "");
833 alpha1
= LLVMBuildBitCast(builder
, alpha1
, bld16
.vec_type
, "");
834 code
= LLVMBuildBitCast(builder
, code
, bld16
.vec_type
, "");
835 /* we'll get garbage in the elements which had code 0 (or larger than 5 or 7)
837 code
= LLVMBuildSub(builder
, code
, bld16
.one
, "");
839 weight
= LLVMBuildMul(builder
, weight
, code
, "");
840 weight
= LLVMBuildLShr(builder
, weight
,
841 lp_build_const_int_vec(gallivm
, type16
, 6), "");
843 delta
= LLVMBuildSub(builder
, alpha1
, alpha0
, "");
845 ainterp
= LLVMBuildMul(builder
, delta
, weight
, "");
846 ainterp
= LLVMBuildLShr(builder
, ainterp
,
847 lp_build_const_int_vec(gallivm
, type16
, 8), "");
849 ainterp
= LLVMBuildBitCast(builder
, ainterp
, lp_build_vec_type(gallivm
, type8
), "");
850 alpha0
= LLVMBuildBitCast(builder
, alpha0
, lp_build_vec_type(gallivm
, type8
), "");
851 ainterp
= LLVMBuildAdd(builder
, alpha0
, ainterp
, "");
852 ainterp
= LLVMBuildBitCast(builder
, ainterp
, lp_build_vec_type(gallivm
, type32
), "");
858 * Convert from <n x i128> s3tc dxt5 to <4n x i8> RGBA AoS
859 * @param colors is a <n x i32> vector with n x 2x16bit colors
860 * @param codewords is a <n x i32> vector containing the codewords
861 * @param alphas is a <n x i64> vector containing the alpha values
862 * @param i is a <n x i32> vector with the x pixel coordinate (0 to 3)
863 * @param j is a <n x i32> vector with the y pixel coordinate (0 to 3)
866 s3tc_dxt5_full_to_rgba_aos(struct gallivm_state
*gallivm
,
868 enum pipe_format format
,
870 LLVMValueRef codewords
,
871 LLVMValueRef alpha_lo
,
872 LLVMValueRef alpha_hi
,
876 LLVMBuilderRef builder
= gallivm
->builder
;
877 LLVMValueRef rgba
, tmp
, alpha0
, alpha1
, alphac
, alphac0
, bit_pos
, shift
;
878 LLVMValueRef sel_mask
, tmp_mask
, alpha
, alpha64
, code_s
;
879 LLVMValueRef mask6
, mask7
, ainterp
;
880 LLVMTypeRef i64t
= LLVMInt64TypeInContext(gallivm
->context
);
881 LLVMTypeRef i32t
= LLVMInt32TypeInContext(gallivm
->context
);
882 struct lp_type type
, type8
;
883 struct lp_build_context bld32
;
885 memset(&type
, 0, sizeof type
);
889 memset(&type8
, 0, sizeof type8
);
893 assert(lp_check_value(type
, i
));
894 assert(lp_check_value(type
, j
));
896 lp_build_context_init(&bld32
, gallivm
, type
);
898 assert(lp_check_value(type
, i
));
899 assert(lp_check_value(type
, j
));
901 rgba
= s3tc_dxt1_to_rgba_aos(gallivm
, n
, format
,
902 colors
, codewords
, i
, j
);
904 rgba
= LLVMBuildBitCast(builder
, rgba
, bld32
.vec_type
, "");
906 /* this looks pretty complex for vectorization:
907 * extract a0/a1 values
909 * select weights for interpolation depending on a0 > a1
910 * mul weights by code - 1
912 * use selects for getting either a0, a1, interp a, interp a/0.0, interp a/1.0
915 alpha0
= LLVMBuildAnd(builder
, alpha_lo
,
916 lp_build_const_int_vec(gallivm
, type
, 0xff), "");
917 alpha1
= LLVMBuildLShr(builder
, alpha_lo
,
918 lp_build_const_int_vec(gallivm
, type
, 8), "");
919 alpha1
= LLVMBuildAnd(builder
, alpha1
,
920 lp_build_const_int_vec(gallivm
, type
, 0xff), "");
923 bit_pos
= LLVMBuildShl(builder
, j
, lp_build_const_int_vec(gallivm
, type
, 2), "");
924 bit_pos
= LLVMBuildAdd(builder
, bit_pos
, i
, "");
925 tmp
= LLVMBuildAdd(builder
, bit_pos
, bit_pos
, "");
926 bit_pos
= LLVMBuildAdd(builder
, bit_pos
, tmp
, "");
927 /* get rid of first 2 bytes - saves shifts of alpha_lo/hi */
928 bit_pos
= LLVMBuildAdd(builder
, bit_pos
,
929 lp_build_const_int_vec(gallivm
, type
, 16), "");
932 struct lp_type type64
;
933 memset(&type64
, 0, sizeof type64
);
936 /* This is pretty pointless could avoid by just directly extracting
937 64bit in the first place but makes it more complicated elsewhere */
938 alpha_lo
= LLVMBuildZExt(builder
, alpha_lo
, i64t
, "");
939 alpha_hi
= LLVMBuildZExt(builder
, alpha_hi
, i64t
, "");
940 alphac0
= LLVMBuildShl(builder
, alpha_hi
,
941 lp_build_const_int_vec(gallivm
, type64
, 32), "");
942 alphac0
= LLVMBuildOr(builder
, alpha_lo
, alphac0
, "");
944 shift
= LLVMBuildZExt(builder
, bit_pos
, i64t
, "");
945 alphac0
= LLVMBuildLShr(builder
, alphac0
, shift
, "");
946 alphac0
= LLVMBuildTrunc(builder
, alphac0
, i32t
, "");
947 alphac
= LLVMBuildAnd(builder
, alphac0
,
948 lp_build_const_int_vec(gallivm
, type
, 0x7), "");
952 * Using non-native vector length here (actually, with avx2 and
953 * n == 4 llvm will indeed expand to ymm regs...)
954 * At least newer llvm versions handle that ok.
955 * llvm 3.7+ will even handle the emulated 64bit shift with variable
956 * shift count without extraction (and it's actually easier to
957 * emulate than the 32bit one).
959 alpha64
= LLVMBuildShuffleVector(builder
, alpha_lo
, alpha_hi
,
960 lp_build_const_unpackx2_shuffle(gallivm
, n
), "");
962 alpha64
= LLVMBuildBitCast(builder
, alpha64
, LLVMVectorType(i64t
, n
), "");
963 shift
= LLVMBuildZExt(builder
, bit_pos
, LLVMVectorType(i64t
, n
), "");
964 alphac
= LLVMBuildLShr(builder
, alpha64
, shift
, "");
965 alphac
= LLVMBuildTrunc(builder
, alphac
, bld32
.vec_type
, "");
967 alphac
= LLVMBuildAnd(builder
, alphac
,
968 lp_build_const_int_vec(gallivm
, type
, 0x7), "");
971 /* signed compare is faster saves some xors */
973 /* alpha0 > alpha1 selection */
974 sel_mask
= lp_build_compare(gallivm
, type
, PIPE_FUNC_GREATER
,
976 ainterp
= lp_build_lerpdxta(gallivm
, alpha0
, alpha1
, alphac
, sel_mask
, n
);
979 * if a0 > a1 then we select a0 for case 0, a1 for case 1, interp otherwise.
980 * else we select a0 for case 0, a1 for case 1,
981 * interp for case 2-5, 00 for 6 and 0xff(ffffff) for 7
982 * a = (c == 0) ? a0 : a1
983 * a = (c > 1) ? ainterp : a
984 * Finally handle case 6/7 for !(a0 > a1)
985 * a = (!(a0 > a1) && c == 6) ? 0 : a (andnot with mask)
986 * a = (!(a0 > a1) && c == 7) ? 0xffffffff : a (or with mask)
988 tmp_mask
= lp_build_compare(gallivm
, type
, PIPE_FUNC_EQUAL
,
990 alpha
= lp_build_select(&bld32
, tmp_mask
, alpha0
, alpha1
);
991 tmp_mask
= lp_build_compare(gallivm
, type
, PIPE_FUNC_GREATER
,
993 alpha
= lp_build_select(&bld32
, tmp_mask
, ainterp
, alpha
);
995 code_s
= LLVMBuildAnd(builder
, alphac
,
996 LLVMBuildNot(builder
, sel_mask
, ""), "");
997 mask6
= lp_build_compare(gallivm
, type
, PIPE_FUNC_EQUAL
,
998 code_s
, lp_build_const_int_vec(gallivm
, type
, 6));
999 mask7
= lp_build_compare(gallivm
, type
, PIPE_FUNC_EQUAL
,
1000 code_s
, lp_build_const_int_vec(gallivm
, type
, 7));
1001 alpha
= LLVMBuildAnd(builder
, alpha
, LLVMBuildNot(builder
, mask6
, ""), "");
1002 alpha
= LLVMBuildOr(builder
, alpha
, mask7
, "");
1004 alpha
= LLVMBuildShl(builder
, alpha
, lp_build_const_int_vec(gallivm
, type
, 24), "");
1005 rgba
= LLVMBuildOr(builder
, alpha
, rgba
, "");
1007 return LLVMBuildBitCast(builder
, rgba
, lp_build_vec_type(gallivm
, type8
), "");
1012 lp_build_gather_s3tc_simple_scalar(struct gallivm_state
*gallivm
,
1013 const struct util_format_description
*format_desc
,
1014 LLVMValueRef
*dxt_block
,
1017 LLVMBuilderRef builder
= gallivm
->builder
;
1018 unsigned block_bits
= format_desc
->block
.bits
;
1019 LLVMValueRef elem
, shuf
;
1020 LLVMTypeRef type32
= LLVMIntTypeInContext(gallivm
->context
, 32);
1021 LLVMTypeRef src_type
= LLVMIntTypeInContext(gallivm
->context
, block_bits
);
1022 LLVMTypeRef src_ptr_type
= LLVMPointerType(src_type
, 0);
1023 LLVMTypeRef type32_4
= LLVMVectorType(type32
, 4);
1025 assert(block_bits
== 64 || block_bits
== 128);
1027 ptr
= LLVMBuildBitCast(builder
, ptr
, src_ptr_type
, "");
1028 elem
= LLVMBuildLoad(builder
, ptr
, "");
1030 if (block_bits
== 128) {
1031 /* just return block as is */
1032 *dxt_block
= LLVMBuildBitCast(builder
, elem
, type32_4
, "");
1035 LLVMTypeRef type32_2
= LLVMVectorType(type32
, 2);
1036 shuf
= lp_build_const_extend_shuffle(gallivm
, 2, 4);
1037 elem
= LLVMBuildBitCast(builder
, elem
, type32_2
, "");
1038 *dxt_block
= LLVMBuildShuffleVector(builder
, elem
,
1039 LLVMGetUndef(type32_2
), shuf
, "");
1045 s3tc_store_cached_block(struct gallivm_state
*gallivm
,
1047 LLVMValueRef tag_value
,
1048 LLVMValueRef hash_index
,
1051 LLVMBuilderRef builder
= gallivm
->builder
;
1052 LLVMValueRef ptr
, indices
[3];
1053 LLVMTypeRef type_ptr4x32
;
1056 type_ptr4x32
= LLVMPointerType(LLVMVectorType(LLVMInt32TypeInContext(gallivm
->context
), 4), 0);
1057 indices
[0] = lp_build_const_int32(gallivm
, 0);
1058 indices
[1] = lp_build_const_int32(gallivm
, LP_BUILD_FORMAT_CACHE_MEMBER_TAGS
);
1059 indices
[2] = hash_index
;
1060 ptr
= LLVMBuildGEP(builder
, cache
, indices
, ARRAY_SIZE(indices
), "");
1061 LLVMBuildStore(builder
, tag_value
, ptr
);
1063 indices
[1] = lp_build_const_int32(gallivm
, LP_BUILD_FORMAT_CACHE_MEMBER_DATA
);
1064 hash_index
= LLVMBuildMul(builder
, hash_index
,
1065 lp_build_const_int32(gallivm
, 16), "");
1066 for (count
= 0; count
< 4; count
++) {
1067 indices
[2] = hash_index
;
1068 ptr
= LLVMBuildGEP(builder
, cache
, indices
, ARRAY_SIZE(indices
), "");
1069 ptr
= LLVMBuildBitCast(builder
, ptr
, type_ptr4x32
, "");
1070 LLVMBuildStore(builder
, col
[count
], ptr
);
1071 hash_index
= LLVMBuildAdd(builder
, hash_index
,
1072 lp_build_const_int32(gallivm
, 4), "");
1077 s3tc_lookup_cached_pixel(struct gallivm_state
*gallivm
,
1081 LLVMBuilderRef builder
= gallivm
->builder
;
1082 LLVMValueRef member_ptr
, indices
[3];
1084 indices
[0] = lp_build_const_int32(gallivm
, 0);
1085 indices
[1] = lp_build_const_int32(gallivm
, LP_BUILD_FORMAT_CACHE_MEMBER_DATA
);
1087 member_ptr
= LLVMBuildGEP(builder
, ptr
, indices
, ARRAY_SIZE(indices
), "");
1088 return LLVMBuildLoad(builder
, member_ptr
, "cache_data");
1092 s3tc_lookup_tag_data(struct gallivm_state
*gallivm
,
1096 LLVMBuilderRef builder
= gallivm
->builder
;
1097 LLVMValueRef member_ptr
, indices
[3];
1099 indices
[0] = lp_build_const_int32(gallivm
, 0);
1100 indices
[1] = lp_build_const_int32(gallivm
, LP_BUILD_FORMAT_CACHE_MEMBER_TAGS
);
1102 member_ptr
= LLVMBuildGEP(builder
, ptr
, indices
, ARRAY_SIZE(indices
), "");
1103 return LLVMBuildLoad(builder
, member_ptr
, "tag_data");
1106 #if LP_BUILD_FORMAT_CACHE_DEBUG
1108 s3tc_update_cache_access(struct gallivm_state
*gallivm
,
1113 LLVMBuilderRef builder
= gallivm
->builder
;
1114 LLVMValueRef member_ptr
, cache_access
;
1116 assert(index
== LP_BUILD_FORMAT_CACHE_MEMBER_ACCESS_TOTAL
||
1117 index
== LP_BUILD_FORMAT_CACHE_MEMBER_ACCESS_MISS
);
1119 member_ptr
= lp_build_struct_get_ptr(gallivm
, ptr
, index
, "");
1120 cache_access
= LLVMBuildLoad(builder
, member_ptr
, "cache_access");
1121 cache_access
= LLVMBuildAdd(builder
, cache_access
,
1122 LLVMConstInt(LLVMInt64TypeInContext(gallivm
->context
),
1124 LLVMBuildStore(builder
, cache_access
, member_ptr
);
1129 * Calculate 1/3(v1-v0) + v0 and 2*1/3(v1-v0) + v0.
1130 * The lerp is performed between the first 2 32bit colors
1131 * in the source vector, both results are returned packed in result vector.
1134 lp_build_lerp23_single(struct lp_build_context
*bld
,
1137 struct gallivm_state
*gallivm
= bld
->gallivm
;
1138 LLVMValueRef x
, mul
, delta
, res
, v0
, v1
, elems
[8];
1139 const struct lp_type type
= bld
->type
;
1140 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
1141 struct lp_type i16_type
= lp_wider_type(type
);
1142 struct lp_type i32_type
= lp_wider_type(i16_type
);
1143 struct lp_build_context bld2
;
1145 assert(!type
.floating
&& !type
.fixed
&& !type
.norm
&& type
.width
== 8);
1147 lp_build_context_init(&bld2
, gallivm
, i16_type
);
1148 bld2
.type
.sign
= TRUE
;
1150 /* weights 256/3, 256*2/3, with correct rounding */
1151 elems
[0] = elems
[1] = elems
[2] = elems
[3] =
1152 lp_build_const_elem(gallivm
, i16_type
, 255*1/3);
1153 elems
[4] = elems
[5] = elems
[6] = elems
[7] =
1154 lp_build_const_elem(gallivm
, i16_type
, 171);
1155 x
= LLVMConstVector(elems
, 8);
1158 * v01 has col0 in 32bit elem 0, col1 in elem 1.
1159 * Interleave/unpack will give us separate v0/v1 vectors.
1161 v01
= lp_build_interleave2(gallivm
, i32_type
, v01
, v01
, 0);
1162 v01
= LLVMBuildBitCast(builder
, v01
, bld
->vec_type
, "");
1164 lp_build_unpack2(gallivm
, type
, i16_type
, v01
, &v0
, &v1
);
1165 delta
= lp_build_sub(&bld2
, v1
, v0
);
1167 mul
= LLVMBuildMul(builder
, x
, delta
, "");
1169 mul
= LLVMBuildLShr(builder
, mul
, lp_build_const_int_vec(gallivm
, i16_type
, 8), "");
1170 /* lerp optimization: pack now, do add afterwards */
1171 res
= lp_build_pack2(gallivm
, i16_type
, type
, mul
, bld2
.undef
);
1172 /* only lower 2 elems are valid - for these v0 is really v0 */
1173 return lp_build_add(bld
, res
, v01
);
1177 * decode one dxt1 block.
1180 s3tc_decode_block_dxt1(struct gallivm_state
*gallivm
,
1181 enum pipe_format format
,
1182 LLVMValueRef dxt_block
,
1185 LLVMBuilderRef builder
= gallivm
->builder
;
1186 LLVMValueRef color01
, color23
, color01_16
, color0123
;
1187 LLVMValueRef rgba
, tmp
, a
, sel_mask
, indices
, code
, const2
;
1188 struct lp_type type8
, type32
, type16
, type64
;
1189 struct lp_build_context bld8
, bld32
, bld16
, bld64
;
1191 boolean is_dxt1_variant
= format_dxt1_variant(format
);
1193 memset(&type32
, 0, sizeof type32
);
1198 memset(&type8
, 0, sizeof type8
);
1202 memset(&type16
, 0, sizeof type16
);
1206 memset(&type64
, 0, sizeof type64
);
1210 a
= lp_build_const_int_vec(gallivm
, type32
, 0xff000000);
1211 const2
= lp_build_const_int_vec(gallivm
, type32
, 2);
1213 lp_build_context_init(&bld32
, gallivm
, type32
);
1214 lp_build_context_init(&bld16
, gallivm
, type16
);
1215 lp_build_context_init(&bld8
, gallivm
, type8
);
1216 lp_build_context_init(&bld64
, gallivm
, type64
);
1218 if (is_dxt1_variant
) {
1219 color01
= lp_build_shuffle1undef(gallivm
, dxt_block
, 0, 4);
1220 code
= lp_build_shuffle1undef(gallivm
, dxt_block
, 1, 4);
1222 color01
= lp_build_shuffle1undef(gallivm
, dxt_block
, 2, 4);
1223 code
= lp_build_shuffle1undef(gallivm
, dxt_block
, 3, 4);
1225 code
= LLVMBuildBitCast(builder
, code
, bld8
.vec_type
, "");
1226 /* expand bytes to dwords */
1227 code
= lp_build_interleave2(gallivm
, type8
, code
, code
, 0);
1228 code
= lp_build_interleave2(gallivm
, type8
, code
, code
, 0);
1233 * - expand color0/color1 to rgba8888
1234 * - calculate color2/3 (interpolation) according to color0 < color1 rules
1235 * - calculate color2/3 according to color0 >= color1 rules
1236 * - do selection of color2/3 according to comparison of color0/1
1237 * - extract indices.
1238 * - use compare/select to select the correct color. Since we have 2bit
1239 * indices (and 4 colors), needs at least three compare/selects.
1243 * expand the two colors
1245 color01
= LLVMBuildBitCast(builder
, color01
, bld16
.vec_type
, "");
1246 color01
= lp_build_interleave2(gallivm
, type16
, color01
,
1248 color01_16
= LLVMBuildBitCast(builder
, color01
, bld32
.vec_type
, "");
1249 color01
= color_expand_565_to_8888(gallivm
, 4, color01_16
);
1252 * interpolate colors
1253 * color2_1 is 2/3 color0 + 1/3 color1
1254 * color3_1 is 1/3 color0 + 2/3 color1
1255 * color2_2 is 1/2 color0 + 1/2 color1
1259 /* TODO: since this is now always scalar, should
1260 * probably just use control flow here instead of calculating
1261 * both cases and then selection
1263 if (format
== PIPE_FORMAT_DXT1_RGBA
||
1264 format
== PIPE_FORMAT_DXT1_SRGBA
) {
1265 color01
= LLVMBuildOr(builder
, color01
, a
, "");
1267 /* can combine 2 lerps into one mostly */
1268 color23
= lp_build_lerp23_single(&bld8
, color01
);
1269 color23
= LLVMBuildBitCast(builder
, color23
, bld32
.vec_type
, "");
1271 /* dxt3/5 always use 4-color encoding */
1272 if (is_dxt1_variant
) {
1273 LLVMValueRef color23_2
, color2_2
;
1275 if (util_cpu_caps
.has_sse2
) {
1276 LLVMValueRef intrargs
[2];
1277 intrargs
[0] = LLVMBuildBitCast(builder
, color01
, bld8
.vec_type
, "");
1278 /* same interleave as for lerp23 - correct result in 2nd element */
1279 intrargs
[1] = lp_build_interleave2(gallivm
, type32
, color01
, color01
, 0);
1280 intrargs
[1] = LLVMBuildBitCast(builder
, intrargs
[1], bld8
.vec_type
, "");
1281 color2_2
= lp_build_intrinsic(builder
, "llvm.x86.sse2.pavg.b",
1282 bld8
.vec_type
, intrargs
, 2, 0);
1285 LLVMValueRef v01
, v0
, v1
, vhalf
;
1287 * This isn't as expensive as it looks (the unpack is the same as
1288 * for lerp23, which is the reason why we do the pointless
1289 * interleave2 too), with correct rounding (the two lower elements
1290 * will be the same).
1292 v01
= lp_build_interleave2(gallivm
, type32
, color01
, color01
, 0);
1293 v01
= LLVMBuildBitCast(builder
, v01
, bld8
.vec_type
, "");
1294 lp_build_unpack2(gallivm
, type8
, type16
, v01
, &v0
, &v1
);
1295 vhalf
= lp_build_add(&bld16
, v0
, v1
);
1296 vhalf
= LLVMBuildLShr(builder
, vhalf
, bld16
.one
, "");
1297 color2_2
= lp_build_pack2(gallivm
, type16
, type8
, vhalf
, bld16
.undef
);
1299 /* shuffle in color 3 as elem 2 zero, color 2 elem 1 */
1300 color23_2
= LLVMBuildBitCast(builder
, color2_2
, bld64
.vec_type
, "");
1301 color23_2
= LLVMBuildLShr(builder
, color23_2
,
1302 lp_build_const_int_vec(gallivm
, type64
, 32), "");
1303 color23_2
= LLVMBuildBitCast(builder
, color23_2
, bld32
.vec_type
, "");
1305 tmp
= LLVMBuildBitCast(builder
, color01_16
, bld64
.vec_type
, "");
1306 tmp
= LLVMBuildLShr(builder
, tmp
,
1307 lp_build_const_int_vec(gallivm
, type64
, 32), "");
1308 tmp
= LLVMBuildBitCast(builder
, tmp
, bld32
.vec_type
, "");
1309 sel_mask
= lp_build_compare(gallivm
, type32
, PIPE_FUNC_GREATER
,
1311 sel_mask
= lp_build_interleave2(gallivm
, type32
, sel_mask
, sel_mask
, 0);
1312 color23
= lp_build_select(&bld32
, sel_mask
, color23
, color23_2
);
1315 if (util_cpu_caps
.has_ssse3
) {
1317 * Use pshufb as mini-lut. (Only doable with intrinsics as the
1318 * final shuffles are non-constant. pshufb is awesome!)
1320 LLVMValueRef shuf
[16], low2mask
;
1321 LLVMValueRef intrargs
[2], lut_ind
, lut_adj
;
1323 color01
= LLVMBuildBitCast(builder
, color01
, bld64
.vec_type
, "");
1324 color23
= LLVMBuildBitCast(builder
, color23
, bld64
.vec_type
, "");
1325 color0123
= lp_build_interleave2(gallivm
, type64
, color01
, color23
, 0);
1326 color0123
= LLVMBuildBitCast(builder
, color0123
, bld32
.vec_type
, "");
1328 if (format
== PIPE_FORMAT_DXT1_RGB
||
1329 format
== PIPE_FORMAT_DXT1_SRGB
) {
1330 color0123
= LLVMBuildOr(builder
, color0123
, a
, "");
1333 /* shuffle as r0r1r2r3g0g1... */
1334 for (i
= 0; i
< 4; i
++) {
1335 shuf
[4*i
] = lp_build_const_int32(gallivm
, 0 + i
);
1336 shuf
[4*i
+1] = lp_build_const_int32(gallivm
, 4 + i
);
1337 shuf
[4*i
+2] = lp_build_const_int32(gallivm
, 8 + i
);
1338 shuf
[4*i
+3] = lp_build_const_int32(gallivm
, 12 + i
);
1340 color0123
= LLVMBuildBitCast(builder
, color0123
, bld8
.vec_type
, "");
1341 color0123
= LLVMBuildShuffleVector(builder
, color0123
, bld8
.undef
,
1342 LLVMConstVector(shuf
, 16), "");
1344 /* lowest 2 bits of each 8 bit value contain index into "LUT" */
1345 low2mask
= lp_build_const_int_vec(gallivm
, type8
, 3);
1346 /* add 0/4/8/12 for r/g/b/a */
1347 lut_adj
= lp_build_const_int_vec(gallivm
, type32
, 0x0c080400);
1348 lut_adj
= LLVMBuildBitCast(builder
, lut_adj
, bld8
.vec_type
, "");
1349 intrargs
[0] = color0123
;
1350 for (i
= 0; i
< 4; i
++) {
1351 lut_ind
= LLVMBuildAnd(builder
, code
, low2mask
, "");
1352 lut_ind
= LLVMBuildOr(builder
, lut_ind
, lut_adj
, "");
1353 intrargs
[1] = lut_ind
;
1354 col
[i
] = lp_build_intrinsic(builder
, "llvm.x86.ssse3.pshuf.b.128",
1355 bld8
.vec_type
, intrargs
, 2, 0);
1356 col
[i
] = LLVMBuildBitCast(builder
, col
[i
], bld32
.vec_type
, "");
1357 code
= LLVMBuildBitCast(builder
, code
, bld32
.vec_type
, "");
1358 code
= LLVMBuildLShr(builder
, code
, const2
, "");
1359 code
= LLVMBuildBitCast(builder
, code
, bld8
.vec_type
, "");
1363 /* Thanks to vectorization can do 4 texels in parallel */
1364 LLVMValueRef color0
, color1
, color2
, color3
;
1365 if (format
== PIPE_FORMAT_DXT1_RGB
||
1366 format
== PIPE_FORMAT_DXT1_SRGB
) {
1367 color01
= LLVMBuildOr(builder
, color01
, a
, "");
1368 color23
= LLVMBuildOr(builder
, color23
, a
, "");
1370 color0
= LLVMBuildShuffleVector(builder
, color01
, bld32
.undef
,
1371 lp_build_const_shuffle1(gallivm
, 0, 4), "");
1372 color1
= LLVMBuildShuffleVector(builder
, color01
, bld32
.undef
,
1373 lp_build_const_shuffle1(gallivm
, 1, 4), "");
1374 color2
= LLVMBuildShuffleVector(builder
, color23
, bld32
.undef
,
1375 lp_build_const_shuffle1(gallivm
, 0, 4), "");
1376 color3
= LLVMBuildShuffleVector(builder
, color23
, bld32
.undef
,
1377 lp_build_const_shuffle1(gallivm
, 1, 4), "");
1378 code
= LLVMBuildBitCast(builder
, code
, bld32
.vec_type
, "");
1380 for (i
= 0; i
< 4; i
++) {
1381 /* select the colors */
1382 LLVMValueRef selmasklo
, rgba01
, rgba23
, bitlo
;
1384 indices
= LLVMBuildAnd(builder
, code
, bitlo
, "");
1385 selmasklo
= lp_build_compare(gallivm
, type32
, PIPE_FUNC_EQUAL
,
1387 rgba01
= lp_build_select(&bld32
, selmasklo
, color1
, color0
);
1389 LLVMValueRef selmaskhi
;
1390 indices
= LLVMBuildAnd(builder
, code
, const2
, "");
1391 selmaskhi
= lp_build_compare(gallivm
, type32
, PIPE_FUNC_EQUAL
,
1393 rgba23
= lp_build_select(&bld32
, selmasklo
, color3
, color2
);
1394 rgba
= lp_build_select(&bld32
, selmaskhi
, rgba23
, rgba01
);
1397 * Note that this will give "wrong" order.
1398 * col0 will be rgba0, rgba4, rgba8, rgba12, col1 rgba1, rgba5, ...
1399 * This would be easily fixable by using different shuffle, bitlo/hi
1400 * vectors above (and different shift), but seems slightly easier to
1401 * deal with for dxt3/dxt5 alpha too. So instead change lookup.
1404 code
= LLVMBuildLShr(builder
, code
, const2
, "");
1410 * decode one dxt3 block.
1413 s3tc_decode_block_dxt3(struct gallivm_state
*gallivm
,
1414 enum pipe_format format
,
1415 LLVMValueRef dxt_block
,
1418 LLVMBuilderRef builder
= gallivm
->builder
;
1419 LLVMValueRef alpha
, alphas0
, alphas1
, shift4_16
, a
[4], mask8hi
;
1420 struct lp_type type32
, type8
, type16
;
1423 memset(&type32
, 0, sizeof type32
);
1427 memset(&type8
, 0, sizeof type8
);
1431 memset(&type16
, 0, sizeof type16
);
1435 s3tc_decode_block_dxt1(gallivm
, format
, dxt_block
, col
);
1437 shift4_16
= lp_build_const_int_vec(gallivm
, type16
, 4);
1438 mask8hi
= lp_build_const_int_vec(gallivm
, type32
, 0xff000000);
1440 alpha
= LLVMBuildBitCast(builder
, dxt_block
,
1441 lp_build_vec_type(gallivm
, type8
), "");
1442 alpha
= lp_build_interleave2(gallivm
, type8
, alpha
, alpha
, 0);
1443 alpha
= LLVMBuildBitCast(builder
, alpha
,
1444 lp_build_vec_type(gallivm
, type16
), "");
1445 alpha
= LLVMBuildAnd(builder
, alpha
,
1446 lp_build_const_int_vec(gallivm
, type16
, 0xf00f), "");
1447 alphas0
= LLVMBuildLShr(builder
, alpha
, shift4_16
, "");
1448 alphas1
= LLVMBuildShl(builder
, alpha
, shift4_16
, "");
1449 alpha
= LLVMBuildOr(builder
, alphas0
, alpha
, "");
1450 alpha
= LLVMBuildOr(builder
, alphas1
, alpha
, "");
1451 alpha
= LLVMBuildBitCast(builder
, alpha
,
1452 lp_build_vec_type(gallivm
, type32
), "");
1454 * alpha now contains elems 0,1,2,3,... (ubytes)
1455 * we need 0,4,8,12, 1,5,9,13 etc. in dwords to match color (which
1456 * is just as easy as "natural" order - 3 shift/and instead of 6 unpack).
1458 a
[0] = LLVMBuildShl(builder
, alpha
,
1459 lp_build_const_int_vec(gallivm
, type32
, 24), "");
1460 a
[1] = LLVMBuildShl(builder
, alpha
,
1461 lp_build_const_int_vec(gallivm
, type32
, 16), "");
1462 a
[1] = LLVMBuildAnd(builder
, a
[1], mask8hi
, "");
1463 a
[2] = LLVMBuildShl(builder
, alpha
,
1464 lp_build_const_int_vec(gallivm
, type32
, 8), "");
1465 a
[2] = LLVMBuildAnd(builder
, a
[2], mask8hi
, "");
1466 a
[3] = LLVMBuildAnd(builder
, alpha
, mask8hi
, "");
1468 for (i
= 0; i
< 4; i
++) {
1469 col
[i
] = LLVMBuildOr(builder
, col
[i
], a
[i
], "");
1475 lp_build_lerpdxta_block(struct gallivm_state
*gallivm
,
1476 LLVMValueRef alpha0
,
1477 LLVMValueRef alpha1
,
1479 LLVMValueRef sel_mask
)
1481 LLVMBuilderRef builder
= gallivm
->builder
;
1482 LLVMValueRef delta
, ainterp
;
1483 LLVMValueRef weight5
, weight7
, weight
;
1484 struct lp_type type16
;
1485 struct lp_build_context bld
;
1487 memset(&type16
, 0, sizeof type16
);
1492 lp_build_context_init(&bld
, gallivm
, type16
);
1494 * 256/7 is only 36.57 so we'd lose quite some precision. Since it would
1495 * actually be desirable to do this here with even higher accuracy than
1496 * even 8 bit (more or less required for rgtc, albeit that's not handled
1497 * here right now), shift the weights after multiplication by code.
1499 weight5
= lp_build_const_int_vec(gallivm
, type16
, 256*64/5);
1500 weight7
= lp_build_const_int_vec(gallivm
, type16
, 256*64/7);
1501 weight
= lp_build_select(&bld
, sel_mask
, weight7
, weight5
);
1504 * we'll get garbage in the elements which had code 0 (or larger than
1505 * 5 or 7) but we don't care (or rather, need to fix up anyway).
1507 code
= LLVMBuildSub(builder
, code
, bld
.one
, "");
1509 weight
= LLVMBuildMul(builder
, weight
, code
, "");
1510 weight
= LLVMBuildLShr(builder
, weight
,
1511 lp_build_const_int_vec(gallivm
, type16
, 6), "");
1513 delta
= LLVMBuildSub(builder
, alpha1
, alpha0
, "");
1515 ainterp
= LLVMBuildMul(builder
, delta
, weight
, "");
1516 ainterp
= LLVMBuildLShr(builder
, ainterp
,
1517 lp_build_const_int_vec(gallivm
, type16
, 8), "");
1519 /* lerp is done later (with packed values) */
1526 * decode one dxt5 block.
1529 s3tc_decode_block_dxt5(struct gallivm_state
*gallivm
,
1530 enum pipe_format format
,
1531 LLVMValueRef dxt_block
,
1534 LLVMBuilderRef builder
= gallivm
->builder
;
1535 LLVMValueRef alpha
, alpha0
, alpha1
, ares
;
1536 LLVMValueRef ainterp
, ainterp0
, ainterp1
, shuffle1
, sel_mask
, sel_mask2
;
1537 LLVMValueRef a
[4], acode
, tmp0
, tmp1
;
1538 LLVMTypeRef i64t
, i32t
;
1539 struct lp_type type32
, type64
, type8
, type16
;
1540 struct lp_build_context bld16
, bld8
;
1543 memset(&type32
, 0, sizeof type32
);
1547 memset(&type64
, 0, sizeof type64
);
1551 memset(&type8
, 0, sizeof type8
);
1555 memset(&type16
, 0, sizeof type16
);
1559 lp_build_context_init(&bld16
, gallivm
, type16
);
1560 lp_build_context_init(&bld8
, gallivm
, type8
);
1562 i64t
= lp_build_vec_type(gallivm
, type64
);
1563 i32t
= lp_build_vec_type(gallivm
, type32
);
1565 s3tc_decode_block_dxt1(gallivm
, format
, dxt_block
, col
);
1568 * three possible strategies for vectorizing alpha:
1569 * 1) compute all 8 values then use scalar extraction
1570 * (i.e. have all 8 alpha values packed in one 64bit scalar
1571 * and do something like ax = vals >> (codex * 8) followed
1572 * by inserting these values back into color)
1573 * 2) same as 8 but just use pshufb as a mini-LUT for selection.
1574 * (without pshufb would need boatloads of cmp/selects trying to
1575 * keep things vectorized for essentially scalar selection).
1576 * 3) do something similar to the uncached case
1577 * needs more calculations (need to calc 16 values instead of 8 though
1578 * that's only an issue for the lerp which we need to do twice otherwise
1579 * everything still fits into 128bit) but keeps things vectorized mostly.
1580 * Trying 3) here though not sure it's really faster...
1581 * With pshufb, we try 2) (cheaper and more accurate)
1585 * Ideally, we'd use 2 variable 16bit shifts here (byte shifts wouldn't
1586 * help since code crosses 8bit boundaries). But variable shifts are
1587 * AVX2 only, and even then only dword/quadword (intel _really_ hates
1588 * shifts!). Instead, emulate by 16bit muls.
1589 * Also, the required byte shuffles are essentially non-emulatable, so
1590 * require ssse3 (albeit other archs might do them fine).
1591 * This is not directly tied to ssse3 - just need sane byte shuffles.
1592 * But ordering is going to be different below so use same condition.
1596 /* vectorize alpha */
1597 alpha
= LLVMBuildBitCast(builder
, dxt_block
, i64t
, "");
1598 alpha0
= LLVMBuildAnd(builder
, alpha
,
1599 lp_build_const_int_vec(gallivm
, type64
, 0xff), "");
1600 alpha0
= LLVMBuildBitCast(builder
, alpha0
, bld16
.vec_type
, "");
1601 alpha
= LLVMBuildBitCast(builder
, alpha
, bld16
.vec_type
, "");
1602 alpha1
= LLVMBuildLShr(builder
, alpha
,
1603 lp_build_const_int_vec(gallivm
, type16
, 8), "");
1604 alpha
= LLVMBuildBitCast(builder
, alpha
, i64t
, "");
1605 shuffle1
= lp_build_const_shuffle1(gallivm
, 0, 8);
1606 /* XXX this shuffle broken with LLVM 2.8 */
1607 alpha0
= LLVMBuildShuffleVector(builder
, alpha0
, alpha0
, shuffle1
, "");
1608 alpha1
= LLVMBuildShuffleVector(builder
, alpha1
, alpha1
, shuffle1
, "");
1611 sel_mask
= lp_build_compare(gallivm
, type16
, PIPE_FUNC_GREATER
,
1613 type16
.sign
= FALSE
;
1614 sel_mask
= LLVMBuildBitCast(builder
, sel_mask
, bld8
.vec_type
, "");
1616 if (!util_cpu_caps
.has_ssse3
) {
1617 LLVMValueRef acodeg
, mask1
, acode0
, acode1
;
1619 /* extraction of the 3 bit values into something more useful is HARD */
1620 /* first steps are actually scalar */
1621 acode
= LLVMBuildLShr(builder
, alpha
,
1622 lp_build_const_int_vec(gallivm
, type64
, 16), "");
1623 tmp0
= LLVMBuildAnd(builder
, acode
,
1624 lp_build_const_int_vec(gallivm
, type64
, 0xffffff), "");
1625 tmp1
= LLVMBuildLShr(builder
, acode
,
1626 lp_build_const_int_vec(gallivm
, type64
, 24), "");
1627 tmp0
= LLVMBuildBitCast(builder
, tmp0
, i32t
, "");
1628 tmp1
= LLVMBuildBitCast(builder
, tmp1
, i32t
, "");
1629 acode
= lp_build_interleave2(gallivm
, type32
, tmp0
, tmp1
, 0);
1630 /* now have 2x24bit in 4x32bit, order 01234567, 89..., undef, undef */
1631 tmp0
= LLVMBuildAnd(builder
, acode
,
1632 lp_build_const_int_vec(gallivm
, type32
, 0xfff), "");
1633 tmp1
= LLVMBuildLShr(builder
, acode
,
1634 lp_build_const_int_vec(gallivm
, type32
, 12), "");
1635 acode
= lp_build_interleave2(gallivm
, type32
, tmp0
, tmp1
, 0);
1636 /* now have 4x12bit in 4x32bit, order 0123, 4567, ,,, */
1637 tmp0
= LLVMBuildAnd(builder
, acode
,
1638 lp_build_const_int_vec(gallivm
, type32
, 0x3f), "");
1639 tmp1
= LLVMBuildLShr(builder
, acode
,
1640 lp_build_const_int_vec(gallivm
, type32
, 6), "");
1641 /* use signed pack doesn't matter and otherwise need sse41 */
1642 type32
.sign
= type16
.sign
= TRUE
;
1643 acode
= lp_build_pack2(gallivm
, type32
, type16
, tmp0
, tmp1
);
1644 type32
.sign
= type16
.sign
= FALSE
;
1645 /* now have 8x6bit in 8x16bit, 01, 45, 89, ..., 23, 67, ... */
1646 acode0
= LLVMBuildAnd(builder
, acode
,
1647 lp_build_const_int_vec(gallivm
, type16
, 0x7), "");
1648 acode1
= LLVMBuildLShr(builder
, acode
,
1649 lp_build_const_int_vec(gallivm
, type16
, 3), "");
1650 acode
= lp_build_pack2(gallivm
, type16
, type8
, acode0
, acode1
);
1651 /* acode0 contains elems 0,4,8,12,2,6,10,14, acode1 1,5,9,... */
1653 acodeg
= LLVMBuildAnd(builder
, acode
,
1654 LLVMBuildNot(builder
, sel_mask
, ""), "");
1655 mask1
= lp_build_compare(gallivm
, type8
, PIPE_FUNC_EQUAL
,
1658 sel_mask
= LLVMBuildBitCast(builder
, sel_mask
, bld16
.vec_type
, "");
1659 ainterp0
= lp_build_lerpdxta_block(gallivm
, alpha0
, alpha1
, acode0
, sel_mask
);
1660 ainterp1
= lp_build_lerpdxta_block(gallivm
, alpha0
, alpha1
, acode1
, sel_mask
);
1661 sel_mask
= LLVMBuildBitCast(builder
, sel_mask
, bld8
.vec_type
, "");
1662 ainterp
= lp_build_pack2(gallivm
, type16
, type8
, ainterp0
, ainterp1
);
1663 alpha0
= lp_build_pack2(gallivm
, type16
, type8
, alpha0
, alpha0
);
1664 alpha1
= lp_build_pack2(gallivm
, type16
, type8
, alpha1
, alpha1
);
1665 ainterp
= LLVMBuildAdd(builder
, ainterp
, alpha0
, "");
1667 sel_mask2
= lp_build_compare(gallivm
, type8
, PIPE_FUNC_EQUAL
,
1669 ainterp
= lp_build_select(&bld8
, sel_mask2
, alpha0
, ainterp
);
1670 ainterp
= lp_build_select(&bld8
, mask1
, alpha1
, ainterp
);
1672 /* fix up val67 if a0 <= a1 */
1673 sel_mask2
= lp_build_compare(gallivm
, type8
, PIPE_FUNC_EQUAL
,
1674 acodeg
, lp_build_const_int_vec(gallivm
, type8
, 6));
1675 ares
= LLVMBuildAnd(builder
, ainterp
, LLVMBuildNot(builder
, sel_mask2
, ""), "");
1676 sel_mask2
= lp_build_compare(gallivm
, type8
, PIPE_FUNC_EQUAL
,
1677 acodeg
, lp_build_const_int_vec(gallivm
, type8
, 7));
1678 ares
= LLVMBuildOr(builder
, ares
, sel_mask2
, "");
1680 /* unpack in right order (0,4,8,12,1,5,..) */
1681 /* this gives us zero, a0, zero, a4, zero, a8, ... for tmp0 */
1682 tmp0
= lp_build_interleave2(gallivm
, type8
, bld8
.zero
, ares
, 0);
1683 tmp1
= lp_build_interleave2(gallivm
, type8
, bld8
.zero
, ares
, 1);
1684 tmp0
= LLVMBuildBitCast(builder
, tmp0
, bld16
.vec_type
, "");
1685 tmp1
= LLVMBuildBitCast(builder
, tmp1
, bld16
.vec_type
, "");
1687 a
[0] = lp_build_interleave2(gallivm
, type16
, bld16
.zero
, tmp0
, 0);
1688 a
[1] = lp_build_interleave2(gallivm
, type16
, bld16
.zero
, tmp1
, 0);
1689 a
[2] = lp_build_interleave2(gallivm
, type16
, bld16
.zero
, tmp0
, 1);
1690 a
[3] = lp_build_interleave2(gallivm
, type16
, bld16
.zero
, tmp1
, 1);
1693 LLVMValueRef elems
[16], intrargs
[2], shufa
, mulclo
, mulchi
, mask8hi
;
1694 LLVMTypeRef type16s
= LLVMInt16TypeInContext(gallivm
->context
);
1695 LLVMTypeRef type8s
= LLVMInt8TypeInContext(gallivm
->context
);
1698 * Ideally, we'd use 2 variable 16bit shifts here (byte shifts wouldn't
1699 * help since code crosses 8bit boundaries). But variable shifts are
1700 * AVX2 only, and even then only dword/quadword (intel _really_ hates
1701 * shifts!). Instead, emulate by 16bit muls.
1702 * Also, the required byte shuffles are essentially non-emulatable, so
1703 * require ssse3 (albeit other archs might do them fine, but the
1704 * complete path is ssse3 only for now).
1706 for (i
= 0, j
= 0; i
< 16; i
+= 8, j
+= 3) {
1707 elems
[i
+0] = elems
[i
+1] = elems
[i
+2] = lp_build_const_int32(gallivm
, j
+2);
1708 elems
[i
+3] = elems
[i
+4] = lp_build_const_int32(gallivm
, j
+3);
1709 elems
[i
+5] = elems
[i
+6] = elems
[i
+7] = lp_build_const_int32(gallivm
, j
+4);
1711 shufa
= LLVMConstVector(elems
, 16);
1712 alpha
= LLVMBuildBitCast(builder
, alpha
, bld8
.vec_type
, "");
1713 acode
= LLVMBuildShuffleVector(builder
, alpha
, bld8
.undef
, shufa
, "");
1714 acode
= LLVMBuildBitCast(builder
, acode
, bld16
.vec_type
, "");
1716 * Put 0/2/4/6 into high 3 bits of 16 bits (save AND mask)
1717 * Do the same for 1/3/5/7 (albeit still need mask there - ideally
1718 * we'd place them into bits 4-7 so could save shift but impossible.)
1720 for (i
= 0; i
< 8; i
+= 4) {
1721 elems
[i
+0] = LLVMConstInt(type16s
, 1 << (13-0), 0);
1722 elems
[i
+1] = LLVMConstInt(type16s
, 1 << (13-6), 0);
1723 elems
[i
+2] = LLVMConstInt(type16s
, 1 << (13-4), 0);
1724 elems
[i
+3] = LLVMConstInt(type16s
, 1 << (13-2), 0);
1726 mulclo
= LLVMConstVector(elems
, 8);
1727 for (i
= 0; i
< 8; i
+= 4) {
1728 elems
[i
+0] = LLVMConstInt(type16s
, 1 << (13-3), 0);
1729 elems
[i
+1] = LLVMConstInt(type16s
, 1 << (13-9), 0);
1730 elems
[i
+2] = LLVMConstInt(type16s
, 1 << (13-7), 0);
1731 elems
[i
+3] = LLVMConstInt(type16s
, 1 << (13-5), 0);
1733 mulchi
= LLVMConstVector(elems
, 8);
1735 tmp0
= LLVMBuildMul(builder
, acode
, mulclo
, "");
1736 tmp1
= LLVMBuildMul(builder
, acode
, mulchi
, "");
1737 tmp0
= LLVMBuildLShr(builder
, tmp0
,
1738 lp_build_const_int_vec(gallivm
, type16
, 13), "");
1739 tmp1
= LLVMBuildLShr(builder
, tmp1
,
1740 lp_build_const_int_vec(gallivm
, type16
, 5), "");
1741 tmp1
= LLVMBuildAnd(builder
, tmp1
,
1742 lp_build_const_int_vec(gallivm
, type16
, 0x700), "");
1743 acode
= LLVMBuildOr(builder
, tmp0
, tmp1
, "");
1744 acode
= LLVMBuildBitCast(builder
, acode
, bld8
.vec_type
, "");
1747 * Note that ordering is different here to non-ssse3 path:
1751 LLVMValueRef weight0
, weight1
, weight
, delta
;
1752 LLVMValueRef constff_elem7
, const0_elem6
;
1753 /* weights, correctly rounded (round(256*x/7)) */
1754 elems
[0] = LLVMConstInt(type16s
, 256, 0);
1755 elems
[1] = LLVMConstInt(type16s
, 0, 0);
1756 elems
[2] = LLVMConstInt(type16s
, 219, 0);
1757 elems
[3] = LLVMConstInt(type16s
, 183, 0);
1758 elems
[4] = LLVMConstInt(type16s
, 146, 0);
1759 elems
[5] = LLVMConstInt(type16s
, 110, 0);
1760 elems
[6] = LLVMConstInt(type16s
, 73, 0);
1761 elems
[7] = LLVMConstInt(type16s
, 37, 0);
1762 weight0
= LLVMConstVector(elems
, 8);
1764 elems
[0] = LLVMConstInt(type16s
, 256, 0);
1765 elems
[1] = LLVMConstInt(type16s
, 0, 0);
1766 elems
[2] = LLVMConstInt(type16s
, 205, 0);
1767 elems
[3] = LLVMConstInt(type16s
, 154, 0);
1768 elems
[4] = LLVMConstInt(type16s
, 102, 0);
1769 elems
[5] = LLVMConstInt(type16s
, 51, 0);
1770 elems
[6] = LLVMConstInt(type16s
, 0, 0);
1771 elems
[7] = LLVMConstInt(type16s
, 0, 0);
1772 weight1
= LLVMConstVector(elems
, 8);
1774 weight0
= LLVMBuildBitCast(builder
, weight0
, bld8
.vec_type
, "");
1775 weight1
= LLVMBuildBitCast(builder
, weight1
, bld8
.vec_type
, "");
1776 weight
= lp_build_select(&bld8
, sel_mask
, weight0
, weight1
);
1777 weight
= LLVMBuildBitCast(builder
, weight
, bld16
.vec_type
, "");
1779 for (i
= 0; i
< 16; i
++) {
1780 elems
[i
] = LLVMConstNull(type8s
);
1782 elems
[7] = LLVMConstInt(type8s
, 255, 0);
1783 constff_elem7
= LLVMConstVector(elems
, 16);
1785 for (i
= 0; i
< 16; i
++) {
1786 elems
[i
] = LLVMConstInt(type8s
, 255, 0);
1788 elems
[6] = LLVMConstInt(type8s
, 0, 0);
1789 const0_elem6
= LLVMConstVector(elems
, 16);
1791 /* standard simple lerp - but the version we need isn't available */
1792 delta
= LLVMBuildSub(builder
, alpha0
, alpha1
, "");
1793 ainterp
= LLVMBuildMul(builder
, delta
, weight
, "");
1794 ainterp
= LLVMBuildLShr(builder
, ainterp
,
1795 lp_build_const_int_vec(gallivm
, type16
, 8), "");
1796 ainterp
= LLVMBuildBitCast(builder
, ainterp
, bld8
.vec_type
, "");
1797 alpha1
= LLVMBuildBitCast(builder
, alpha1
, bld8
.vec_type
, "");
1798 ainterp
= LLVMBuildAdd(builder
, ainterp
, alpha1
, "");
1799 ainterp
= LLVMBuildBitCast(builder
, ainterp
, bld16
.vec_type
, "");
1800 ainterp
= lp_build_pack2(gallivm
, type16
, type8
, ainterp
, bld16
.undef
);
1802 /* fixing 0/0xff case is slightly more complex */
1803 constff_elem7
= LLVMBuildAnd(builder
, constff_elem7
,
1804 LLVMBuildNot(builder
, sel_mask
, ""), "");
1805 const0_elem6
= LLVMBuildOr(builder
, const0_elem6
, sel_mask
, "");
1806 ainterp
= LLVMBuildOr(builder
, ainterp
, constff_elem7
, "");
1807 ainterp
= LLVMBuildAnd(builder
, ainterp
, const0_elem6
, "");
1809 /* now pick all 16 elements at once! */
1810 intrargs
[0] = ainterp
;
1811 intrargs
[1] = acode
;
1812 ares
= lp_build_intrinsic(builder
, "llvm.x86.ssse3.pshuf.b.128",
1813 bld8
.vec_type
, intrargs
, 2, 0);
1815 ares
= LLVMBuildBitCast(builder
, ares
, i32t
, "");
1816 mask8hi
= lp_build_const_int_vec(gallivm
, type32
, 0xff000000);
1817 a
[0] = LLVMBuildShl(builder
, ares
,
1818 lp_build_const_int_vec(gallivm
, type32
, 24), "");
1819 a
[1] = LLVMBuildShl(builder
, ares
,
1820 lp_build_const_int_vec(gallivm
, type32
, 16), "");
1821 a
[1] = LLVMBuildAnd(builder
, a
[1], mask8hi
, "");
1822 a
[2] = LLVMBuildShl(builder
, ares
,
1823 lp_build_const_int_vec(gallivm
, type32
, 8), "");
1824 a
[2] = LLVMBuildAnd(builder
, a
[2], mask8hi
, "");
1825 a
[3] = LLVMBuildAnd(builder
, ares
, mask8hi
, "");
1828 for (i
= 0; i
< 4; i
++) {
1829 a
[i
] = LLVMBuildBitCast(builder
, a
[i
], i32t
, "");
1830 col
[i
] = LLVMBuildOr(builder
, col
[i
], a
[i
], "");
1836 generate_update_cache_one_block(struct gallivm_state
*gallivm
,
1837 LLVMValueRef function
,
1838 const struct util_format_description
*format_desc
)
1840 LLVMBasicBlockRef block
;
1841 LLVMBuilderRef old_builder
;
1842 LLVMValueRef ptr_addr
;
1843 LLVMValueRef hash_index
;
1845 LLVMValueRef dxt_block
, tag_value
;
1846 LLVMValueRef col
[LP_MAX_VECTOR_LENGTH
];
1848 ptr_addr
= LLVMGetParam(function
, 0);
1849 hash_index
= LLVMGetParam(function
, 1);
1850 cache
= LLVMGetParam(function
, 2);
1852 lp_build_name(ptr_addr
, "ptr_addr" );
1853 lp_build_name(hash_index
, "hash_index");
1854 lp_build_name(cache
, "cache_addr");
1860 old_builder
= gallivm
->builder
;
1861 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, function
, "entry");
1862 gallivm
->builder
= LLVMCreateBuilderInContext(gallivm
->context
);
1863 LLVMPositionBuilderAtEnd(gallivm
->builder
, block
);
1865 lp_build_gather_s3tc_simple_scalar(gallivm
, format_desc
, &dxt_block
,
1868 switch (format_desc
->format
) {
1869 case PIPE_FORMAT_DXT1_RGB
:
1870 case PIPE_FORMAT_DXT1_RGBA
:
1871 case PIPE_FORMAT_DXT1_SRGB
:
1872 case PIPE_FORMAT_DXT1_SRGBA
:
1873 s3tc_decode_block_dxt1(gallivm
, format_desc
->format
, dxt_block
, col
);
1875 case PIPE_FORMAT_DXT3_RGBA
:
1876 case PIPE_FORMAT_DXT3_SRGBA
:
1877 s3tc_decode_block_dxt3(gallivm
, format_desc
->format
, dxt_block
, col
);
1879 case PIPE_FORMAT_DXT5_RGBA
:
1880 case PIPE_FORMAT_DXT5_SRGBA
:
1881 s3tc_decode_block_dxt5(gallivm
, format_desc
->format
, dxt_block
, col
);
1885 s3tc_decode_block_dxt1(gallivm
, format_desc
->format
, dxt_block
, col
);
1889 tag_value
= LLVMBuildPtrToInt(gallivm
->builder
, ptr_addr
,
1890 LLVMInt64TypeInContext(gallivm
->context
), "");
1891 s3tc_store_cached_block(gallivm
, col
, tag_value
, hash_index
, cache
);
1893 LLVMBuildRetVoid(gallivm
->builder
);
1895 LLVMDisposeBuilder(gallivm
->builder
);
1896 gallivm
->builder
= old_builder
;
1898 gallivm_verify_function(gallivm
, function
);
1903 update_cached_block(struct gallivm_state
*gallivm
,
1904 const struct util_format_description
*format_desc
,
1905 LLVMValueRef ptr_addr
,
1906 LLVMValueRef hash_index
,
1910 LLVMBuilderRef builder
= gallivm
->builder
;
1911 LLVMModuleRef module
= gallivm
->module
;
1913 LLVMTypeRef i8t
= LLVMInt8TypeInContext(gallivm
->context
);
1914 LLVMTypeRef pi8t
= LLVMPointerType(i8t
, 0);
1915 LLVMValueRef function
, inst
;
1916 LLVMBasicBlockRef bb
;
1917 LLVMValueRef args
[3];
1919 util_snprintf(name
, sizeof name
, "%s_update_cache_one_block",
1920 format_desc
->short_name
);
1921 function
= LLVMGetNamedFunction(module
, name
);
1924 LLVMTypeRef ret_type
;
1925 LLVMTypeRef arg_types
[3];
1926 LLVMTypeRef function_type
;
1930 * Generate the function prototype.
1933 ret_type
= LLVMVoidTypeInContext(gallivm
->context
);
1934 arg_types
[0] = pi8t
;
1935 arg_types
[1] = LLVMInt32TypeInContext(gallivm
->context
);
1936 arg_types
[2] = LLVMTypeOf(cache
); // XXX: put right type here
1937 function_type
= LLVMFunctionType(ret_type
, arg_types
, ARRAY_SIZE(arg_types
), 0);
1938 function
= LLVMAddFunction(module
, name
, function_type
);
1940 for (arg
= 0; arg
< ARRAY_SIZE(arg_types
); ++arg
)
1941 if (LLVMGetTypeKind(arg_types
[arg
]) == LLVMPointerTypeKind
)
1942 lp_add_function_attr(function
, arg
+ 1, LP_FUNC_ATTR_NOALIAS
);
1944 LLVMSetFunctionCallConv(function
, LLVMFastCallConv
);
1945 LLVMSetVisibility(function
, LLVMHiddenVisibility
);
1946 generate_update_cache_one_block(gallivm
, function
, format_desc
);
1950 args
[1] = hash_index
;
1953 LLVMBuildCall(builder
, function
, args
, ARRAY_SIZE(args
), "");
1954 bb
= LLVMGetInsertBlock(builder
);
1955 inst
= LLVMGetLastInstruction(bb
);
1956 LLVMSetInstructionCallConv(inst
, LLVMFastCallConv
);
1963 compressed_fetch_cached(struct gallivm_state
*gallivm
,
1964 const struct util_format_description
*format_desc
,
1966 LLVMValueRef base_ptr
,
1967 LLVMValueRef offset
,
1973 LLVMBuilderRef builder
= gallivm
->builder
;
1974 unsigned count
, low_bit
, log2size
;
1975 LLVMValueRef color
, offset_stored
, addr
, ptr_addrtrunc
, tmp
;
1976 LLVMValueRef ij_index
, hash_index
, hash_mask
, block_index
;
1977 LLVMTypeRef i8t
= LLVMInt8TypeInContext(gallivm
->context
);
1978 LLVMTypeRef i32t
= LLVMInt32TypeInContext(gallivm
->context
);
1979 LLVMTypeRef i64t
= LLVMInt64TypeInContext(gallivm
->context
);
1980 struct lp_type type
;
1981 struct lp_build_context bld32
;
1982 memset(&type
, 0, sizeof type
);
1986 lp_build_context_init(&bld32
, gallivm
, type
);
1989 * compute hash - we use direct mapped cache, the hash function could
1990 * be better but it needs to be simple
1992 * compare offset with offset stored at tag (hash)
1993 * if not equal extract block, store block, update tag
1994 * extract color from cache
1998 low_bit
= util_logbase2(format_desc
->block
.bits
/ 8);
1999 log2size
= util_logbase2(LP_BUILD_FORMAT_CACHE_SIZE
);
2000 addr
= LLVMBuildPtrToInt(builder
, base_ptr
, i64t
, "");
2001 ptr_addrtrunc
= LLVMBuildPtrToInt(builder
, base_ptr
, i32t
, "");
2002 ptr_addrtrunc
= lp_build_broadcast_scalar(&bld32
, ptr_addrtrunc
);
2003 /* For the hash function, first mask off the unused lowest bits. Then just
2004 do some xor with address bits - only use lower 32bits */
2005 ptr_addrtrunc
= LLVMBuildAdd(builder
, offset
, ptr_addrtrunc
, "");
2006 ptr_addrtrunc
= LLVMBuildLShr(builder
, ptr_addrtrunc
,
2007 lp_build_const_int_vec(gallivm
, type
, low_bit
), "");
2008 /* This only really makes sense for size 64,128,256 */
2009 hash_index
= ptr_addrtrunc
;
2010 ptr_addrtrunc
= LLVMBuildLShr(builder
, ptr_addrtrunc
,
2011 lp_build_const_int_vec(gallivm
, type
, 2*log2size
), "");
2012 hash_index
= LLVMBuildXor(builder
, ptr_addrtrunc
, hash_index
, "");
2013 tmp
= LLVMBuildLShr(builder
, hash_index
,
2014 lp_build_const_int_vec(gallivm
, type
, log2size
), "");
2015 hash_index
= LLVMBuildXor(builder
, hash_index
, tmp
, "");
2017 hash_mask
= lp_build_const_int_vec(gallivm
, type
, LP_BUILD_FORMAT_CACHE_SIZE
- 1);
2018 hash_index
= LLVMBuildAnd(builder
, hash_index
, hash_mask
, "");
2019 ij_index
= LLVMBuildShl(builder
, i
, lp_build_const_int_vec(gallivm
, type
, 2), "");
2020 ij_index
= LLVMBuildAdd(builder
, ij_index
, j
, "");
2021 block_index
= LLVMBuildShl(builder
, hash_index
,
2022 lp_build_const_int_vec(gallivm
, type
, 4), "");
2023 block_index
= LLVMBuildAdd(builder
, ij_index
, block_index
, "");
2026 color
= bld32
.undef
;
2027 for (count
= 0; count
< n
; count
++) {
2028 LLVMValueRef index
, cond
, colorx
;
2029 LLVMValueRef block_indexx
, hash_indexx
, addrx
, offsetx
, ptr_addrx
;
2030 struct lp_build_if_state if_ctx
;
2032 index
= lp_build_const_int32(gallivm
, count
);
2033 offsetx
= LLVMBuildExtractElement(builder
, offset
, index
, "");
2034 addrx
= LLVMBuildZExt(builder
, offsetx
, i64t
, "");
2035 addrx
= LLVMBuildAdd(builder
, addrx
, addr
, "");
2036 block_indexx
= LLVMBuildExtractElement(builder
, block_index
, index
, "");
2037 hash_indexx
= LLVMBuildLShr(builder
, block_indexx
,
2038 lp_build_const_int32(gallivm
, 4), "");
2039 offset_stored
= s3tc_lookup_tag_data(gallivm
, cache
, hash_indexx
);
2040 cond
= LLVMBuildICmp(builder
, LLVMIntNE
, offset_stored
, addrx
, "");
2042 lp_build_if(&if_ctx
, gallivm
, cond
);
2044 ptr_addrx
= LLVMBuildIntToPtr(builder
, addrx
,
2045 LLVMPointerType(i8t
, 0), "");
2046 update_cached_block(gallivm
, format_desc
, ptr_addrx
, hash_indexx
, cache
);
2047 #if LP_BUILD_FORMAT_CACHE_DEBUG
2048 s3tc_update_cache_access(gallivm
, cache
, 1,
2049 LP_BUILD_FORMAT_CACHE_MEMBER_ACCESS_MISS
);
2052 lp_build_endif(&if_ctx
);
2054 colorx
= s3tc_lookup_cached_pixel(gallivm
, cache
, block_indexx
);
2056 color
= LLVMBuildInsertElement(builder
, color
, colorx
,
2057 lp_build_const_int32(gallivm
, count
), "");
2062 struct lp_build_if_state if_ctx
;
2064 tmp
= LLVMBuildZExt(builder
, offset
, i64t
, "");
2065 addr
= LLVMBuildAdd(builder
, tmp
, addr
, "");
2066 offset_stored
= s3tc_lookup_tag_data(gallivm
, cache
, hash_index
);
2067 cond
= LLVMBuildICmp(builder
, LLVMIntNE
, offset_stored
, addr
, "");
2069 lp_build_if(&if_ctx
, gallivm
, cond
);
2071 tmp
= LLVMBuildIntToPtr(builder
, addr
, LLVMPointerType(i8t
, 0), "");
2072 update_cached_block(gallivm
, format_desc
, tmp
, hash_index
, cache
);
2073 #if LP_BUILD_FORMAT_CACHE_DEBUG
2074 s3tc_update_cache_access(gallivm
, cache
, 1,
2075 LP_BUILD_FORMAT_CACHE_MEMBER_ACCESS_MISS
);
2078 lp_build_endif(&if_ctx
);
2080 color
= s3tc_lookup_cached_pixel(gallivm
, cache
, block_index
);
2082 #if LP_BUILD_FORMAT_CACHE_DEBUG
2083 s3tc_update_cache_access(gallivm
, cache
, n
,
2084 LP_BUILD_FORMAT_CACHE_MEMBER_ACCESS_TOTAL
);
2086 return LLVMBuildBitCast(builder
, color
, LLVMVectorType(i8t
, n
* 4), "");
2091 s3tc_dxt5_to_rgba_aos(struct gallivm_state
*gallivm
,
2093 enum pipe_format format
,
2094 LLVMValueRef colors
,
2095 LLVMValueRef codewords
,
2096 LLVMValueRef alpha_lo
,
2097 LLVMValueRef alpha_hi
,
2101 return s3tc_dxt5_full_to_rgba_aos(gallivm
, n
, format
, colors
,
2102 codewords
, alpha_lo
, alpha_hi
, i
, j
);
2107 * @param n number of pixels processed (usually n=4, but it should also work with n=1
2108 * and multiples of 4)
2109 * @param base_ptr base pointer (32bit or 64bit pointer depending on the architecture)
2110 * @param offset <n x i32> vector with the relative offsets of the S3TC blocks
2111 * @param i is a <n x i32> vector with the x subpixel coordinate (0..3)
2112 * @param j is a <n x i32> vector with the y subpixel coordinate (0..3)
2113 * @return a <4*n x i8> vector with the pixel RGBA values in AoS
2116 lp_build_fetch_s3tc_rgba_aos(struct gallivm_state
*gallivm
,
2117 const struct util_format_description
*format_desc
,
2119 LLVMValueRef base_ptr
,
2120 LLVMValueRef offset
,
2126 LLVMTypeRef i8t
= LLVMInt8TypeInContext(gallivm
->context
);
2127 LLVMBuilderRef builder
= gallivm
->builder
;
2129 assert(format_desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
);
2130 assert(format_desc
->block
.width
== 4);
2131 assert(format_desc
->block
.height
== 4);
2133 assert((n
== 1) || (n
% 4 == 0));
2135 /* debug_printf("format = %d\n", format_desc->format);*/
2137 rgba
= compressed_fetch_cached(gallivm
, format_desc
, n
,
2138 base_ptr
, offset
, i
, j
, cache
);
2144 LLVMTypeRef i8_vectype
= LLVMVectorType(i8t
, 4 * n
);
2145 LLVMTypeRef i128_type
= LLVMIntTypeInContext(gallivm
->context
, 128);
2146 LLVMTypeRef i128_vectype
= LLVMVectorType(i128_type
, n
/ 4);
2147 LLVMTypeRef i324_vectype
= LLVMVectorType(LLVMInt32TypeInContext(
2148 gallivm
->context
), 4);
2149 LLVMValueRef offset4
, i4
, j4
, rgba4
[LP_MAX_VECTOR_LENGTH
/16];
2150 struct lp_type lp_324_vectype
= lp_type_uint_vec(32, 128);
2152 assert(n
/ 4 <= ARRAY_SIZE(rgba4
));
2154 rgba
= LLVMGetUndef(i128_vectype
);
2156 for (count
= 0; count
< n
/ 4; count
++) {
2157 LLVMValueRef colors
, codewords
, alpha_lo
, alpha_hi
;
2159 i4
= lp_build_extract_range(gallivm
, i
, count
* 4, 4);
2160 j4
= lp_build_extract_range(gallivm
, j
, count
* 4, 4);
2161 offset4
= lp_build_extract_range(gallivm
, offset
, count
* 4, 4);
2163 lp_build_gather_s3tc(gallivm
, 4, format_desc
, &colors
, &codewords
,
2164 &alpha_lo
, &alpha_hi
, base_ptr
, offset4
);
2166 switch (format_desc
->format
) {
2167 case PIPE_FORMAT_DXT1_RGB
:
2168 case PIPE_FORMAT_DXT1_RGBA
:
2169 case PIPE_FORMAT_DXT1_SRGB
:
2170 case PIPE_FORMAT_DXT1_SRGBA
:
2171 rgba4
[count
] = s3tc_dxt1_to_rgba_aos(gallivm
, 4, format_desc
->format
,
2172 colors
, codewords
, i4
, j4
);
2174 case PIPE_FORMAT_DXT3_RGBA
:
2175 case PIPE_FORMAT_DXT3_SRGBA
:
2176 rgba4
[count
] = s3tc_dxt3_to_rgba_aos(gallivm
, 4, format_desc
->format
, colors
,
2177 codewords
, alpha_lo
, alpha_hi
, i4
, j4
);
2179 case PIPE_FORMAT_DXT5_RGBA
:
2180 case PIPE_FORMAT_DXT5_SRGBA
:
2181 rgba4
[count
] = s3tc_dxt5_to_rgba_aos(gallivm
, 4, format_desc
->format
, colors
,
2182 codewords
, alpha_lo
, alpha_hi
, i4
, j4
);
2186 rgba4
[count
] = LLVMGetUndef(LLVMVectorType(i8t
, 4));
2189 /* shuffles typically give best results with dword elements...*/
2190 rgba4
[count
] = LLVMBuildBitCast(builder
, rgba4
[count
], i324_vectype
, "");
2192 rgba
= lp_build_concat(gallivm
, rgba4
, lp_324_vectype
, n
/ 4);
2193 rgba
= LLVMBuildBitCast(builder
, rgba
, i8_vectype
, "");
2196 LLVMValueRef colors
, codewords
, alpha_lo
, alpha_hi
;
2198 lp_build_gather_s3tc(gallivm
, n
, format_desc
, &colors
, &codewords
,
2199 &alpha_lo
, &alpha_hi
, base_ptr
, offset
);
2201 switch (format_desc
->format
) {
2202 case PIPE_FORMAT_DXT1_RGB
:
2203 case PIPE_FORMAT_DXT1_RGBA
:
2204 case PIPE_FORMAT_DXT1_SRGB
:
2205 case PIPE_FORMAT_DXT1_SRGBA
:
2206 rgba
= s3tc_dxt1_to_rgba_aos(gallivm
, n
, format_desc
->format
,
2207 colors
, codewords
, i
, j
);
2209 case PIPE_FORMAT_DXT3_RGBA
:
2210 case PIPE_FORMAT_DXT3_SRGBA
:
2211 rgba
= s3tc_dxt3_to_rgba_aos(gallivm
, n
, format_desc
->format
, colors
,
2212 codewords
, alpha_lo
, alpha_hi
, i
, j
);
2214 case PIPE_FORMAT_DXT5_RGBA
:
2215 case PIPE_FORMAT_DXT5_SRGBA
:
2216 rgba
= s3tc_dxt5_to_rgba_aos(gallivm
, n
, format_desc
->format
, colors
,
2217 codewords
, alpha_lo
, alpha_hi
, i
, j
);
2221 rgba
= LLVMGetUndef(LLVMVectorType(i8t
, 4*n
));
2226 /* always return just decompressed values - srgb conversion is done later */