| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#include "util/format/u_format.h" |
| 37 |
#include "util/bitscan.h" |
| 38 |
#include "util/u_pointer.h" |
| 39 |
|
| 40 |
#include "lp_bld_arit.h" |
| 41 |
#include "lp_bld_init.h" |
| 42 |
#include "lp_bld_type.h" |
| 43 |
#include "lp_bld_flow.h" |
| 44 |
#include "lp_bld_const.h" |
| 45 |
#include "lp_bld_conv.h" |
| 46 |
#include "lp_bld_swizzle.h" |
| 47 |
#include "lp_bld_gather.h" |
| 48 |
#include "lp_bld_debug.h" |
| 49 |
#include "lp_bld_format.h" |
| 50 |
#include "lp_bld_pack.h" |
| 51 |
#include "lp_bld_intr.h" |
| 52 |
#include "lp_bld_logic.h" |
| 53 |
#include "lp_bld_bitarit.h" |
| 54 |
#include "lp_bld_misc.h" |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
LLVMValueRef |
| 63 |
lp_build_format_swizzle_aos(const struct util_format_description *desc, |
| 64 |
struct lp_build_context *bld, |
| 65 |
LLVMValueRef unswizzled) |
| 66 |
{ |
| 67 |
unsigned char swizzles[4]; |
| 68 |
unsigned chan; |
| 69 |
|
| 70 |
assert(bld->type.length % 4 == 0); |
| 71 |
|
| 72 |
for (chan = 0; chan < 4; ++chan) { |
| 73 |
enum pipe_swizzle swizzle; |
| 74 |
|
| 75 |
if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) { |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
if (chan == 3) { |
| 80 |
swizzle = PIPE_SWIZZLE_1; |
| 81 |
} else if (desc->swizzle[0] == PIPE_SWIZZLE_NONE) { |
| 82 |
swizzle = PIPE_SWIZZLE_0; |
| 83 |
} else { |
| 84 |
swizzle = desc->swizzle[0]; |
| 85 |
} |
| 86 |
} else { |
| 87 |
swizzle = desc->swizzle[chan]; |
| 88 |
} |
| 89 |
swizzles[chan] = swizzle; |
| 90 |
} |
| 91 |
|
| 92 |
return lp_build_swizzle_aos(bld, unswizzled, swizzles); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
static inline bool |
| 100 |
format_matches_type(const struct util_format_description *desc, |
| 101 |
struct lp_type type) |
| 102 |
{ |
| 103 |
enum util_format_type chan_type; |
| 104 |
unsigned chan; |
| 105 |
|
| 106 |
assert(type.length % 4 == 0); |
| 107 |
|
| 108 |
if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN || |
| 109 |
desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB || |
| 110 |
desc->block.width != 1 || |
| 111 |
desc->block.height != 1) { |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
if (type.floating) { |
| 116 |
chan_type = UTIL_FORMAT_TYPE_FLOAT; |
| 117 |
} else if (type.fixed) { |
| 118 |
chan_type = UTIL_FORMAT_TYPE_FIXED; |
| 119 |
} else if (type.sign) { |
| 120 |
chan_type = UTIL_FORMAT_TYPE_SIGNED; |
| 121 |
} else { |
| 122 |
chan_type = UTIL_FORMAT_TYPE_UNSIGNED; |
| 123 |
} |
| 124 |
|
| 125 |
for (chan = 0; chan < desc->nr_channels; ++chan) { |
| 126 |
if (desc->channel[chan].size != type.width) { |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
if (desc->channel[chan].type != UTIL_FORMAT_TYPE_VOID) { |
| 131 |
if (desc->channel[chan].type != chan_type || |
| 132 |
desc->channel[chan].normalized != type.norm) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
static inline LLVMValueRef |
| 148 |
scale_bits_up(struct gallivm_state *gallivm, |
| 149 |
int src_bits, |
| 150 |
int dst_bits, |
| 151 |
LLVMValueRef src, |
| 152 |
struct lp_type src_type) |
| 153 |
{ |
| 154 |
LLVMBuilderRef builder = gallivm->builder; |
| 155 |
LLVMValueRef result = src; |
| 156 |
|
| 157 |
if (src_bits == 1 && dst_bits > 1) { |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
struct lp_build_context bld; |
| 162 |
LLVMValueRef dst_mask; |
| 163 |
lp_build_context_init(&bld, gallivm, src_type); |
| 164 |
dst_mask = lp_build_const_int_vec(gallivm, src_type, |
| 165 |
(1 << dst_bits) - 1), |
| 166 |
result = lp_build_cmp(&bld, PIPE_FUNC_EQUAL, src, |
| 167 |
lp_build_const_int_vec(gallivm, src_type, 0)); |
| 168 |
result = lp_build_andnot(&bld, dst_mask, result); |
| 169 |
} |
| 170 |
else if (dst_bits > src_bits) { |
| 171 |
|
| 172 |
int db = dst_bits - src_bits; |
| 173 |
|
| 174 |
|
| 175 |
result = LLVMBuildShl(builder, |
| 176 |
src, |
| 177 |
lp_build_const_int_vec(gallivm, src_type, db), |
| 178 |
""); |
| 179 |
|
| 180 |
if (db <= src_bits) { |
| 181 |
|
| 182 |
LLVMValueRef lower = LLVMBuildLShr(builder, |
| 183 |
src, |
| 184 |
lp_build_const_int_vec(gallivm, src_type, |
| 185 |
src_bits - db), |
| 186 |
""); |
| 187 |
|
| 188 |
result = LLVMBuildOr(builder, result, lower, ""); |
| 189 |
} else if (db > src_bits) { |
| 190 |
|
| 191 |
unsigned n; |
| 192 |
|
| 193 |
for (n = src_bits; n < dst_bits; n *= 2) { |
| 194 |
LLVMValueRef shuv = lp_build_const_int_vec(gallivm, src_type, n); |
| 195 |
|
| 196 |
result = LLVMBuildOr(builder, |
| 197 |
result, |
| 198 |
LLVMBuildLShr(builder, result, shuv, ""), |
| 199 |
""); |
| 200 |
} |
| 201 |
} |
| 202 |
} else { |
| 203 |
assert (dst_bits == src_bits); |
| 204 |
} |
| 205 |
|
| 206 |
return result; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
static inline LLVMValueRef |
| 218 |
lp_build_unpack_arith_rgba_aos(struct gallivm_state *gallivm, |
| 219 |
const struct util_format_description *desc, |
| 220 |
LLVMValueRef packed) |
| 221 |
{ |
| 222 |
LLVMBuilderRef builder = gallivm->builder; |
| 223 |
LLVMValueRef shifted, casted, scaled, masked; |
| 224 |
LLVMValueRef shifts[4]; |
| 225 |
LLVMValueRef masks[4]; |
| 226 |
LLVMValueRef scales[4]; |
| 227 |
LLVMTypeRef vec32_type; |
| 228 |
|
| 229 |
bool normalized; |
| 230 |
bool needs_uitofp; |
| 231 |
unsigned i; |
| 232 |
|
| 233 |
|
| 234 |
assert(desc->layout == UTIL_FORMAT_LAYOUT_PLAIN); |
| 235 |
assert(desc->block.width == 1); |
| 236 |
assert(desc->block.height == 1); |
| 237 |
assert(desc->block.bits <= 32); |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
assert (LLVMTypeOf(packed) == LLVMInt32TypeInContext(gallivm->context)); |
| 242 |
|
| 243 |
vec32_type = LLVMVectorType(LLVMInt32TypeInContext(gallivm->context), 4); |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
packed = LLVMBuildInsertElement(builder, LLVMGetUndef(vec32_type), packed, |
| 250 |
LLVMConstNull(LLVMInt32TypeInContext(gallivm->context)), |
| 251 |
""); |
| 252 |
packed = LLVMBuildShuffleVector(builder, packed, LLVMGetUndef(vec32_type), |
| 253 |
LLVMConstNull(vec32_type), |
| 254 |
""); |
| 255 |
|
| 256 |
|
| 257 |
normalized = false; |
| 258 |
needs_uitofp = false; |
| 259 |
|
| 260 |
|
| 261 |
for (i = 0; i < 4; ++i) { |
| 262 |
unsigned bits = desc->channel[i].size; |
| 263 |
unsigned shift = desc->channel[i].shift; |
| 264 |
|
| 265 |
if (desc->channel[i].type == UTIL_FORMAT_TYPE_VOID) { |
| 266 |
shifts[i] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context)); |
| 267 |
masks[i] = LLVMConstNull(LLVMInt32TypeInContext(gallivm->context)); |
| 268 |
scales[i] = LLVMConstNull(LLVMFloatTypeInContext(gallivm->context)); |
| 269 |
} |
| 270 |
else { |
| 271 |
unsigned long long mask = (1ULL << bits) - 1; |
| 272 |
|
| 273 |
assert(desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED); |
| 274 |
|
| 275 |
if (bits == 32) { |
| 276 |
needs_uitofp = true; |
| 277 |
} |
| 278 |
|
| 279 |
shifts[i] = lp_build_const_int32(gallivm, shift); |
| 280 |
masks[i] = lp_build_const_int32(gallivm, mask); |
| 281 |
|
| 282 |
if (desc->channel[i].normalized) { |
| 283 |
scales[i] = lp_build_const_float(gallivm, 1.0 / mask); |
| 284 |
normalized = true; |
| 285 |
} |
| 286 |
else |
| 287 |
scales[i] = lp_build_const_float(gallivm, 1.0); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
if (desc->block.bits < 32 && normalized) { |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
for (i = 0; i < 4; ++i) { |
| 315 |
if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) { |
| 316 |
unsigned bits = desc->channel[i].size; |
| 317 |
unsigned shift = desc->channel[i].shift; |
| 318 |
unsigned long long mask = ((1ULL << bits) - 1) << shift; |
| 319 |
scales[i] = lp_build_const_float(gallivm, 1.0 / mask); |
| 320 |
masks[i] = lp_build_const_int32(gallivm, mask); |
| 321 |
} |
| 322 |
} |
| 323 |
masked = LLVMBuildAnd(builder, packed, LLVMConstVector(masks, 4), ""); |
| 324 |
} else { |
| 325 |
shifted = LLVMBuildLShr(builder, packed, LLVMConstVector(shifts, 4), ""); |
| 326 |
masked = LLVMBuildAnd(builder, shifted, LLVMConstVector(masks, 4), ""); |
| 327 |
} |
| 328 |
|
| 329 |
if (!needs_uitofp) { |
| 330 |
|
| 331 |
casted = LLVMBuildSIToFP(builder, masked, LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), 4), ""); |
| 332 |
} else { |
| 333 |
casted = LLVMBuildUIToFP(builder, masked, LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), 4), ""); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
if (normalized) |
| 344 |
scaled = LLVMBuildFMul(builder, casted, LLVMConstVector(scales, 4), ""); |
| 345 |
else |
| 346 |
scaled = casted; |
| 347 |
|
| 348 |
return scaled; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
LLVMValueRef |
| 361 |
lp_build_pack_rgba_aos(struct gallivm_state *gallivm, |
| 362 |
const struct util_format_description *desc, |
| 363 |
LLVMValueRef rgba) |
| 364 |
{ |
| 365 |
LLVMBuilderRef builder = gallivm->builder; |
| 366 |
LLVMTypeRef type; |
| 367 |
LLVMValueRef packed = NULL; |
| 368 |
LLVMValueRef swizzles[4]; |
| 369 |
LLVMValueRef shifted, casted, scaled, unswizzled; |
| 370 |
LLVMValueRef shifts[4]; |
| 371 |
LLVMValueRef scales[4]; |
| 372 |
bool normalized; |
| 373 |
unsigned i, j; |
| 374 |
|
| 375 |
assert(desc->layout == UTIL_FORMAT_LAYOUT_PLAIN); |
| 376 |
assert(desc->block.width == 1); |
| 377 |
assert(desc->block.height == 1); |
| 378 |
|
| 379 |
type = LLVMIntTypeInContext(gallivm->context, desc->block.bits); |
| 380 |
|
| 381 |
|
| 382 |
for (i = 0; i < 4; ++i) { |
| 383 |
for (j = 0; j < 4; ++j) { |
| 384 |
if (desc->swizzle[j] == i) |
| 385 |
break; |
| 386 |
} |
| 387 |
if (j < 4) |
| 388 |
swizzles[i] = lp_build_const_int32(gallivm, j); |
| 389 |
else |
| 390 |
swizzles[i] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context)); |
| 391 |
} |
| 392 |
|
| 393 |
unswizzled = LLVMBuildShuffleVector(builder, rgba, |
| 394 |
LLVMGetUndef(LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), 4)), |
| 395 |
LLVMConstVector(swizzles, 4), ""); |
| 396 |
|
| 397 |
normalized = false; |
| 398 |
for (i = 0; i < 4; ++i) { |
| 399 |
unsigned bits = desc->channel[i].size; |
| 400 |
unsigned shift = desc->channel[i].shift; |
| 401 |
|
| 402 |
if (desc->channel[i].type == UTIL_FORMAT_TYPE_VOID) { |
| 403 |
shifts[i] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context)); |
| 404 |
scales[i] = LLVMGetUndef(LLVMFloatTypeInContext(gallivm->context)); |
| 405 |
} |
| 406 |
else { |
| 407 |
unsigned mask = (1 << bits) - 1; |
| 408 |
|
| 409 |
assert(desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED); |
| 410 |
assert(bits < 32); |
| 411 |
|
| 412 |
shifts[i] = lp_build_const_int32(gallivm, shift); |
| 413 |
|
| 414 |
if (desc->channel[i].normalized) { |
| 415 |
scales[i] = lp_build_const_float(gallivm, mask); |
| 416 |
normalized = true; |
| 417 |
} |
| 418 |
else |
| 419 |
scales[i] = lp_build_const_float(gallivm, 1.0); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
if (normalized) |
| 424 |
scaled = LLVMBuildFMul(builder, unswizzled, LLVMConstVector(scales, 4), ""); |
| 425 |
else |
| 426 |
scaled = unswizzled; |
| 427 |
|
| 428 |
casted = LLVMBuildFPToSI(builder, scaled, LLVMVectorType(LLVMInt32TypeInContext(gallivm->context), 4), ""); |
| 429 |
|
| 430 |
shifted = LLVMBuildShl(builder, casted, LLVMConstVector(shifts, 4), ""); |
| 431 |
|
| 432 |
|
| 433 |
for (i = 0; i < 4; ++i) { |
| 434 |
if (desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED) { |
| 435 |
LLVMValueRef component = LLVMBuildExtractElement(builder, shifted, |
| 436 |
lp_build_const_int32(gallivm, i), ""); |
| 437 |
if (packed) |
| 438 |
packed = LLVMBuildOr(builder, packed, component, ""); |
| 439 |
else |
| 440 |
packed = component; |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
if (!packed) |
| 445 |
packed = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context)); |
| 446 |
|
| 447 |
if (desc->block.bits < 32) |
| 448 |
packed = LLVMBuildTrunc(builder, packed, type, ""); |
| 449 |
|
| 450 |
return packed; |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
LLVMValueRef |
| 468 |
lp_build_fetch_rgba_aos(struct gallivm_state *gallivm, |
| 469 |
const struct util_format_description *format_desc, |
| 470 |
struct lp_type type, |
| 471 |
bool aligned, |
| 472 |
LLVMValueRef base_ptr, |
| 473 |
LLVMValueRef offset, |
| 474 |
LLVMValueRef i, |
| 475 |
LLVMValueRef j, |
| 476 |
LLVMValueRef cache) |
| 477 |
{ |
| 478 |
const struct util_format_unpack_description *unpack = |
| 479 |
util_format_unpack_description(format_desc->format); |
| 480 |
LLVMBuilderRef builder = gallivm->builder; |
| 481 |
unsigned num_pixels = type.length / 4; |
| 482 |
struct lp_build_context bld; |
| 483 |
|
| 484 |
assert(type.length <= LP_MAX_VECTOR_LENGTH); |
| 485 |
assert(type.length % 4 == 0); |
| 486 |
|
| 487 |
lp_build_context_init(&bld, gallivm, type); |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
if (format_matches_type(format_desc, type) && |
| 497 |
format_desc->block.bits <= type.width * 4 && |
| 498 |
|
| 499 |
util_is_power_of_two_or_zero(format_desc->block.bits)) { |
| 500 |
LLVMValueRef packed; |
| 501 |
LLVMTypeRef dst_vec_type = lp_build_vec_type(gallivm, type); |
| 502 |
struct lp_type fetch_type; |
| 503 |
unsigned vec_len = type.width * type.length; |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
fetch_type = lp_type_uint(type.width*4); |
| 511 |
packed = lp_build_gather(gallivm, type.length/4, |
| 512 |
format_desc->block.bits, fetch_type, |
| 513 |
aligned, base_ptr, offset, true); |
| 514 |
|
| 515 |
assert(format_desc->block.bits <= vec_len); |
| 516 |
(void) vec_len; |
| 517 |
|
| 518 |
packed = LLVMBuildBitCast(gallivm->builder, packed, dst_vec_type, ""); |
| 519 |
return lp_build_format_swizzle_aos(format_desc, &bld, packed); |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
if (format_desc->layout == UTIL_FORMAT_LAYOUT_PLAIN && |
| 531 |
format_desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB && |
| 532 |
util_format_fits_8unorm(format_desc) && |
| 533 |
type.width == 8 && type.norm == 1 && type.sign == 0 && |
| 534 |
type.fixed == 0 && type.floating == 0) { |
| 535 |
LLVMValueRef packed, res = NULL, chans[4], rgba[4]; |
| 536 |
LLVMTypeRef dst_vec_type, conv_vec_type; |
| 537 |
struct lp_type fetch_type, conv_type; |
| 538 |
struct lp_build_context bld_conv; |
| 539 |
unsigned j; |
| 540 |
|
| 541 |
fetch_type = lp_type_uint(type.width*4); |
| 542 |
conv_type = lp_type_int_vec(type.width*4, type.width * type.length); |
| 543 |
dst_vec_type = lp_build_vec_type(gallivm, type); |
| 544 |
conv_vec_type = lp_build_vec_type(gallivm, conv_type); |
| 545 |
lp_build_context_init(&bld_conv, gallivm, conv_type); |
| 546 |
|
| 547 |
packed = lp_build_gather(gallivm, type.length/4, |
| 548 |
format_desc->block.bits, fetch_type, |
| 549 |
aligned, base_ptr, offset, true); |
| 550 |
|
| 551 |
assert(format_desc->block.bits * type.length / 4 <= |
| 552 |
type.width * type.length); |
| 553 |
|
| 554 |
packed = LLVMBuildBitCast(gallivm->builder, packed, conv_vec_type, ""); |
| 555 |
|
| 556 |
for (j = 0; j < format_desc->nr_channels; ++j) { |
| 557 |
unsigned mask = 0; |
| 558 |
unsigned sa = format_desc->channel[j].shift; |
| 559 |
|
| 560 |
mask = (1 << format_desc->channel[j].size) - 1; |
| 561 |
|
| 562 |
|
| 563 |
chans[j] = LLVMBuildLShr(builder, packed, |
| 564 |
lp_build_const_int_vec(gallivm, conv_type, sa), |
| 565 |
""); |
| 566 |
|
| 567 |
chans[j] = LLVMBuildAnd(builder, chans[j], |
| 568 |
lp_build_const_int_vec(gallivm, conv_type, mask), |
| 569 |
""); |
| 570 |
|
| 571 |
|
| 572 |
if (type.norm) { |
| 573 |
chans[j] = scale_bits_up(gallivm, format_desc->channel[j].size, |
| 574 |
type.width, chans[j], conv_type); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
for (j = 0; j < 4; ++j) { |
| 582 |
enum pipe_swizzle swizzle = format_desc->swizzle[j]; |
| 583 |
if (swizzle == PIPE_SWIZZLE_1) { |
| 584 |
rgba[j] = lp_build_const_int_vec(gallivm, conv_type, (1 << type.width) - 1); |
| 585 |
} else { |
| 586 |
rgba[j] = lp_build_swizzle_soa_channel(&bld_conv, chans, swizzle); |
| 587 |
} |
| 588 |
if (j == 0) { |
| 589 |
res = rgba[j]; |
| 590 |
} else { |
| 591 |
rgba[j] = LLVMBuildShl(builder, rgba[j], |
| 592 |
lp_build_const_int_vec(gallivm, conv_type, |
| 593 |
j * type.width), ""); |
| 594 |
res = LLVMBuildOr(builder, res, rgba[j], ""); |
| 595 |
} |
| 596 |
} |
| 597 |
res = LLVMBuildBitCast(gallivm->builder, res, dst_vec_type, ""); |
| 598 |
|
| 599 |
return res; |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
if (format_desc->layout == UTIL_FORMAT_LAYOUT_PLAIN && |
| 607 |
(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB || |
| 608 |
format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) && |
| 609 |
format_desc->block.width == 1 && |
| 610 |
format_desc->block.height == 1 && |
| 611 |
|
| 612 |
util_is_power_of_two_or_zero(format_desc->block.bits) && |
| 613 |
format_desc->block.bits <= 32 && |
| 614 |
format_desc->is_bitmask && |
| 615 |
!format_desc->is_mixed && |
| 616 |
(format_desc->channel[0].type == UTIL_FORMAT_TYPE_UNSIGNED || |
| 617 |
format_desc->channel[1].type == UTIL_FORMAT_TYPE_UNSIGNED) && |
| 618 |
!format_desc->channel[0].pure_integer) { |
| 619 |
|
| 620 |
LLVMValueRef tmps[LP_MAX_VECTOR_LENGTH/4]; |
| 621 |
LLVMValueRef res[LP_MAX_VECTOR_WIDTH / 128]; |
| 622 |
struct lp_type conv_type; |
| 623 |
unsigned k, num_conv_src, num_conv_dst; |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
for (k = 0; k < num_pixels; ++k) { |
| 636 |
LLVMValueRef packed; |
| 637 |
|
| 638 |
packed = lp_build_gather_elem(gallivm, num_pixels, |
| 639 |
format_desc->block.bits, 32, aligned, |
| 640 |
base_ptr, offset, k, false); |
| 641 |
|
| 642 |
tmps[k] = lp_build_unpack_arith_rgba_aos(gallivm, |
| 643 |
format_desc, |
| 644 |
packed); |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
if (gallivm_debug & GALLIVM_DEBUG_PERF && !type.floating) { |
| 655 |
debug_printf("%s: unpacking %s with floating point\n", |
| 656 |
__func__, format_desc->short_name); |
| 657 |
} |
| 658 |
|
| 659 |
conv_type = lp_float32_vec4_type(); |
| 660 |
num_conv_src = num_pixels; |
| 661 |
num_conv_dst = 1; |
| 662 |
|
| 663 |
if (num_pixels % 8 == 0) { |
| 664 |
lp_build_concat_n(gallivm, lp_float32_vec4_type(), |
| 665 |
tmps, num_pixels, tmps, num_pixels / 2); |
| 666 |
conv_type.length *= num_pixels / 4; |
| 667 |
num_conv_src = 4 * num_pixels / 8; |
| 668 |
if (type.width == 8 && type.floating == 0 && type.fixed == 0) { |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
type.length /= num_pixels / 4; |
| 678 |
num_conv_dst = num_pixels / 4; |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
lp_build_conv(gallivm, conv_type, type, |
| 683 |
tmps, num_conv_src, res, num_conv_dst); |
| 684 |
|
| 685 |
if (num_pixels % 8 == 0 && |
| 686 |
(type.width == 8 && type.floating == 0 && type.fixed == 0)) { |
| 687 |
lp_build_concat_n(gallivm, type, res, num_conv_dst, res, 1); |
| 688 |
} |
| 689 |
|
| 690 |
return lp_build_format_swizzle_aos(format_desc, &bld, res[0]); |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
if (format_desc->is_array && |
| 695 |
format_desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB) { |
| 696 |
assert(!format_desc->is_mixed); |
| 697 |
return lp_build_fetch_rgba_aos_array(gallivm, format_desc, type, base_ptr, offset); |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
if (format_desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) { |
| 705 |
struct lp_type tmp_type; |
| 706 |
LLVMValueRef tmp; |
| 707 |
|
| 708 |
memset(&tmp_type, 0, sizeof tmp_type); |
| 709 |
tmp_type.width = 8; |
| 710 |
tmp_type.length = num_pixels * 4; |
| 711 |
tmp_type.norm = true; |
| 712 |
|
| 713 |
tmp = lp_build_fetch_subsampled_rgba_aos(gallivm, |
| 714 |
format_desc, |
| 715 |
num_pixels, |
| 716 |
base_ptr, |
| 717 |
offset, |
| 718 |
i, j); |
| 719 |
|
| 720 |
lp_build_conv(gallivm, |
| 721 |
tmp_type, type, |
| 722 |
&tmp, 1, &tmp, 1); |
| 723 |
|
| 724 |
return tmp; |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) { |
| 732 |
struct lp_type tmp_type; |
| 733 |
LLVMValueRef tmp; |
| 734 |
|
| 735 |
memset(&tmp_type, 0, sizeof tmp_type); |
| 736 |
tmp_type.width = 8; |
| 737 |
tmp_type.length = num_pixels * 4; |
| 738 |
tmp_type.norm = true; |
| 739 |
|
| 740 |
tmp = lp_build_fetch_s3tc_rgba_aos(gallivm, |
| 741 |
format_desc, |
| 742 |
num_pixels, |
| 743 |
base_ptr, |
| 744 |
offset, |
| 745 |
i, j, |
| 746 |
cache); |
| 747 |
|
| 748 |
lp_build_conv(gallivm, |
| 749 |
tmp_type, type, |
| 750 |
&tmp, 1, &tmp, 1); |
| 751 |
|
| 752 |
return tmp; |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
if (format_desc->layout == UTIL_FORMAT_LAYOUT_RGTC) { |
| 760 |
struct lp_type tmp_type; |
| 761 |
LLVMValueRef tmp; |
| 762 |
|
| 763 |
memset(&tmp_type, 0, sizeof tmp_type); |
| 764 |
tmp_type.width = 8; |
| 765 |
tmp_type.length = num_pixels * 4; |
| 766 |
tmp_type.norm = true; |
| 767 |
tmp_type.sign = (format_desc->format == PIPE_FORMAT_RGTC1_SNORM || |
| 768 |
format_desc->format == PIPE_FORMAT_RGTC2_SNORM || |
| 769 |
format_desc->format == PIPE_FORMAT_LATC1_SNORM || |
| 770 |
format_desc->format == PIPE_FORMAT_LATC2_SNORM); |
| 771 |
|
| 772 |
tmp = lp_build_fetch_rgtc_rgba_aos(gallivm, |
| 773 |
format_desc, |
| 774 |
num_pixels, |
| 775 |
base_ptr, |
| 776 |
offset, |
| 777 |
i, j, |
| 778 |
cache); |
| 779 |
|
| 780 |
lp_build_conv(gallivm, |
| 781 |
tmp_type, type, |
| 782 |
&tmp, 1, &tmp, 1); |
| 783 |
|
| 784 |
return tmp; |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
if (unpack->fetch_rgba_8unorm && |
| 792 |
!type.floating && type.width == 8 && !type.sign && type.norm) { |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
LLVMTypeRef i8t = LLVMInt8TypeInContext(gallivm->context); |
| 803 |
LLVMTypeRef pi8t = LLVMPointerType(i8t, 0); |
| 804 |
LLVMTypeRef i32t = LLVMInt32TypeInContext(gallivm->context); |
| 805 |
LLVMValueRef function; |
| 806 |
LLVMValueRef tmp_ptr; |
| 807 |
LLVMValueRef tmp; |
| 808 |
LLVMValueRef res; |
| 809 |
unsigned k; |
| 810 |
|
| 811 |
if (gallivm_debug & GALLIVM_DEBUG_PERF) { |
| 812 |
debug_printf("%s: falling back to util_format_%s_fetch_rgba_8unorm\n", |
| 813 |
__func__, format_desc->short_name); |
| 814 |
} |
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
LLVMTypeRef function_type; |
| 821 |
{ |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
LLVMTypeRef ret_type; |
| 827 |
LLVMTypeRef arg_types[4]; |
| 828 |
|
| 829 |
ret_type = LLVMVoidTypeInContext(gallivm->context); |
| 830 |
arg_types[0] = pi8t; |
| 831 |
arg_types[1] = pi8t; |
| 832 |
arg_types[2] = i32t; |
| 833 |
arg_types[3] = i32t; |
| 834 |
function_type = LLVMFunctionType(ret_type, arg_types, |
| 835 |
ARRAY_SIZE(arg_types), 0); |
| 836 |
} |
| 837 |
|
| 838 |
if (gallivm->cache) |
| 839 |
gallivm->cache->dont_cache = true; |
| 840 |
|
| 841 |
function = lp_build_const_int_pointer(gallivm, |
| 842 |
func_to_pointer((func_pointer) unpack->fetch_rgba_8unorm)); |
| 843 |
|
| 844 |
function = LLVMBuildBitCast(builder, function, LLVMPointerType(function_type, 0), "cast callee"); |
| 845 |
|
| 846 |
tmp_ptr = lp_build_alloca(gallivm, i32t, ""); |
| 847 |
|
| 848 |
res = LLVMGetUndef(LLVMVectorType(i32t, num_pixels)); |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
for (k = 0; k < num_pixels; ++k) { |
| 856 |
LLVMValueRef index = lp_build_const_int32(gallivm, k); |
| 857 |
LLVMValueRef args[4]; |
| 858 |
|
| 859 |
args[0] = LLVMBuildBitCast(builder, tmp_ptr, pi8t, ""); |
| 860 |
args[1] = lp_build_gather_elem_ptr(gallivm, num_pixels, |
| 861 |
base_ptr, offset, k); |
| 862 |
|
| 863 |
if (num_pixels == 1) { |
| 864 |
args[2] = i; |
| 865 |
args[3] = j; |
| 866 |
} |
| 867 |
else { |
| 868 |
args[2] = LLVMBuildExtractElement(builder, i, index, ""); |
| 869 |
args[3] = LLVMBuildExtractElement(builder, j, index, ""); |
| 870 |
} |
| 871 |
|
| 872 |
LLVMBuildCall2(builder, function_type, function, args, ARRAY_SIZE(args), ""); |
| 873 |
|
| 874 |
tmp = LLVMBuildLoad2(builder, i32t, tmp_ptr, ""); |
| 875 |
|
| 876 |
if (num_pixels == 1) { |
| 877 |
res = tmp; |
| 878 |
} |
| 879 |
else { |
| 880 |
res = LLVMBuildInsertElement(builder, res, tmp, index, ""); |
| 881 |
} |
| 882 |
} |
| 883 |
|
| 884 |
|
| 885 |
res = LLVMBuildBitCast(builder, res, bld.vec_type, ""); |
| 886 |
|
| 887 |
return res; |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
util_format_fetch_rgba_func_ptr fetch_rgba = |
| 895 |
util_format_fetch_rgba_func(format_desc->format); |
| 896 |
if (fetch_rgba) { |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
LLVMTypeRef f32t = LLVMFloatTypeInContext(gallivm->context); |
| 907 |
LLVMTypeRef f32x4t = LLVMVectorType(f32t, 4); |
| 908 |
LLVMTypeRef pf32t = LLVMPointerType(f32t, 0); |
| 909 |
LLVMTypeRef pi8t = LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0); |
| 910 |
LLVMTypeRef i32t = LLVMInt32TypeInContext(gallivm->context); |
| 911 |
LLVMValueRef function; |
| 912 |
LLVMValueRef tmp_ptr; |
| 913 |
LLVMValueRef tmps[LP_MAX_VECTOR_LENGTH/4]; |
| 914 |
LLVMValueRef res; |
| 915 |
unsigned k; |
| 916 |
|
| 917 |
if (gallivm_debug & GALLIVM_DEBUG_PERF) { |
| 918 |
debug_printf("%s: falling back to util_format_%s_fetch_rgba_float\n", |
| 919 |
__func__, format_desc->short_name); |
| 920 |
} |
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
LLVMTypeRef function_type = NULL; |
| 927 |
{ |
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
LLVMTypeRef ret_type; |
| 933 |
LLVMTypeRef arg_types[4]; |
| 934 |
|
| 935 |
ret_type = LLVMVoidTypeInContext(gallivm->context); |
| 936 |
arg_types[0] = pf32t; |
| 937 |
arg_types[1] = pi8t; |
| 938 |
arg_types[2] = i32t; |
| 939 |
arg_types[3] = i32t; |
| 940 |
function_type = LLVMFunctionType(ret_type, arg_types, ARRAY_SIZE(arg_types), 0); |
| 941 |
} |
| 942 |
if (gallivm->cache) |
| 943 |
gallivm->cache->dont_cache = true; |
| 944 |
function = lp_build_const_func_pointer_from_type(gallivm, |
| 945 |
func_to_pointer((func_pointer) fetch_rgba), |
| 946 |
function_type, |
| 947 |
format_desc->short_name); |
| 948 |
|
| 949 |
tmp_ptr = lp_build_alloca(gallivm, f32x4t, ""); |
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
for (k = 0; k < num_pixels; ++k) { |
| 957 |
LLVMValueRef args[4]; |
| 958 |
|
| 959 |
args[0] = LLVMBuildBitCast(builder, tmp_ptr, pf32t, ""); |
| 960 |
args[1] = lp_build_gather_elem_ptr(gallivm, num_pixels, |
| 961 |
base_ptr, offset, k); |
| 962 |
|
| 963 |
if (num_pixels == 1) { |
| 964 |
args[2] = i; |
| 965 |
args[3] = j; |
| 966 |
} |
| 967 |
else { |
| 968 |
LLVMValueRef index = lp_build_const_int32(gallivm, k); |
| 969 |
args[2] = LLVMBuildExtractElement(builder, i, index, ""); |
| 970 |
args[3] = LLVMBuildExtractElement(builder, j, index, ""); |
| 971 |
} |
| 972 |
|
| 973 |
LLVMBuildCall2(builder, function_type, function, args, ARRAY_SIZE(args), ""); |
| 974 |
|
| 975 |
tmps[k] = LLVMBuildLoad2(builder, f32x4t, tmp_ptr, ""); |
| 976 |
} |
| 977 |
|
| 978 |
lp_build_conv(gallivm, |
| 979 |
lp_float32_vec4_type(), |
| 980 |
type, |
| 981 |
tmps, num_pixels, &res, 1); |
| 982 |
|
| 983 |
return res; |
| 984 |
} |
| 985 |
|
| 986 |
assert(!util_format_is_pure_integer(format_desc->format)); |
| 987 |
|
| 988 |
assert(0); |
| 989 |
return lp_build_undef(gallivm, type); |
| 990 |
} |