lp_bld_tgsi_soa.c revision 914244e59d6ad08ec2dd815129c6e75b32843d80
1/************************************************************************** 2 * 3 * Copyright 2009 VMware, Inc. 4 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas. 5 * All Rights Reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sub license, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice (including the 16 * next paragraph) shall be included in all copies or substantial portions 17 * of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 * 27 **************************************************************************/ 28 29/** 30 * @file 31 * TGSI to LLVM IR translation -- SoA. 32 * 33 * @author Jose Fonseca <jfonseca@vmware.com> 34 * 35 * Based on tgsi_sse2.c code written by Michal Krol, Keith Whitwell, 36 * Brian Paul, and others. 37 */ 38 39#include "pipe/p_config.h" 40#include "pipe/p_shader_tokens.h" 41#include "util/u_debug.h" 42#include "util/u_math.h" 43#include "util/u_memory.h" 44#include "tgsi/tgsi_dump.h" 45#include "tgsi/tgsi_exec.h" 46#include "tgsi/tgsi_info.h" 47#include "tgsi/tgsi_parse.h" 48#include "tgsi/tgsi_util.h" 49#include "tgsi/tgsi_scan.h" 50#include "lp_bld_tgsi_action.h" 51#include "lp_bld_type.h" 52#include "lp_bld_const.h" 53#include "lp_bld_arit.h" 54#include "lp_bld_bitarit.h" 55#include "lp_bld_gather.h" 56#include "lp_bld_init.h" 57#include "lp_bld_logic.h" 58#include "lp_bld_swizzle.h" 59#include "lp_bld_flow.h" 60#include "lp_bld_quad.h" 61#include "lp_bld_tgsi.h" 62#include "lp_bld_limits.h" 63#include "lp_bld_debug.h" 64#include "lp_bld_printf.h" 65 66 67static void lp_exec_mask_init(struct lp_exec_mask *mask, struct lp_build_context *bld) 68{ 69 LLVMTypeRef int_type = LLVMInt32TypeInContext(bld->gallivm->context); 70 LLVMBuilderRef builder = bld->gallivm->builder; 71 72 mask->bld = bld; 73 mask->has_mask = FALSE; 74 mask->cond_stack_size = 0; 75 mask->loop_stack_size = 0; 76 mask->call_stack_size = 0; 77 78 mask->int_vec_type = lp_build_int_vec_type(bld->gallivm, mask->bld->type); 79 mask->exec_mask = mask->ret_mask = mask->break_mask = mask->cont_mask = mask->cond_mask = 80 LLVMConstAllOnes(mask->int_vec_type); 81 82 mask->loop_limiter = lp_build_alloca(bld->gallivm, int_type, "looplimiter"); 83 84 LLVMBuildStore( 85 builder, 86 LLVMConstInt(int_type, LP_MAX_TGSI_LOOP_ITERATIONS, false), 87 mask->loop_limiter); 88} 89 90static void lp_exec_mask_update(struct lp_exec_mask *mask) 91{ 92 LLVMBuilderRef builder = mask->bld->gallivm->builder; 93 94 if (mask->loop_stack_size) { 95 /*for loops we need to update the entire mask at runtime */ 96 LLVMValueRef tmp; 97 assert(mask->break_mask); 98 tmp = LLVMBuildAnd(builder, 99 mask->cont_mask, 100 mask->break_mask, 101 "maskcb"); 102 mask->exec_mask = LLVMBuildAnd(builder, 103 mask->cond_mask, 104 tmp, 105 "maskfull"); 106 } else 107 mask->exec_mask = mask->cond_mask; 108 109 if (mask->call_stack_size) { 110 mask->exec_mask = LLVMBuildAnd(builder, 111 mask->exec_mask, 112 mask->ret_mask, 113 "callmask"); 114 } 115 116 mask->has_mask = (mask->cond_stack_size > 0 || 117 mask->loop_stack_size > 0 || 118 mask->call_stack_size > 0); 119} 120 121static void lp_exec_mask_cond_push(struct lp_exec_mask *mask, 122 LLVMValueRef val) 123{ 124 LLVMBuilderRef builder = mask->bld->gallivm->builder; 125 126 assert(mask->cond_stack_size < LP_MAX_TGSI_NESTING); 127 if (mask->cond_stack_size == 0) { 128 assert(mask->cond_mask == LLVMConstAllOnes(mask->int_vec_type)); 129 } 130 mask->cond_stack[mask->cond_stack_size++] = mask->cond_mask; 131 assert(LLVMTypeOf(val) == mask->int_vec_type); 132 mask->cond_mask = LLVMBuildAnd(builder, 133 mask->cond_mask, 134 val, 135 ""); 136 lp_exec_mask_update(mask); 137} 138 139static void lp_exec_mask_cond_invert(struct lp_exec_mask *mask) 140{ 141 LLVMBuilderRef builder = mask->bld->gallivm->builder; 142 LLVMValueRef prev_mask; 143 LLVMValueRef inv_mask; 144 145 assert(mask->cond_stack_size); 146 prev_mask = mask->cond_stack[mask->cond_stack_size - 1]; 147 if (mask->cond_stack_size == 1) { 148 assert(prev_mask == LLVMConstAllOnes(mask->int_vec_type)); 149 } 150 151 inv_mask = LLVMBuildNot(builder, mask->cond_mask, ""); 152 153 mask->cond_mask = LLVMBuildAnd(builder, 154 inv_mask, 155 prev_mask, ""); 156 lp_exec_mask_update(mask); 157} 158 159static void lp_exec_mask_cond_pop(struct lp_exec_mask *mask) 160{ 161 assert(mask->cond_stack_size); 162 mask->cond_mask = mask->cond_stack[--mask->cond_stack_size]; 163 lp_exec_mask_update(mask); 164} 165 166static void lp_exec_bgnloop(struct lp_exec_mask *mask) 167{ 168 LLVMBuilderRef builder = mask->bld->gallivm->builder; 169 170 if (mask->loop_stack_size == 0) { 171 assert(mask->loop_block == NULL); 172 assert(mask->cont_mask == LLVMConstAllOnes(mask->int_vec_type)); 173 assert(mask->break_mask == LLVMConstAllOnes(mask->int_vec_type)); 174 assert(mask->break_var == NULL); 175 } 176 177 assert(mask->loop_stack_size < LP_MAX_TGSI_NESTING); 178 179 mask->loop_stack[mask->loop_stack_size].loop_block = mask->loop_block; 180 mask->loop_stack[mask->loop_stack_size].cont_mask = mask->cont_mask; 181 mask->loop_stack[mask->loop_stack_size].break_mask = mask->break_mask; 182 mask->loop_stack[mask->loop_stack_size].break_var = mask->break_var; 183 ++mask->loop_stack_size; 184 185 mask->break_var = lp_build_alloca(mask->bld->gallivm, mask->int_vec_type, ""); 186 LLVMBuildStore(builder, mask->break_mask, mask->break_var); 187 188 mask->loop_block = lp_build_insert_new_block(mask->bld->gallivm, "bgnloop"); 189 190 LLVMBuildBr(builder, mask->loop_block); 191 LLVMPositionBuilderAtEnd(builder, mask->loop_block); 192 193 mask->break_mask = LLVMBuildLoad(builder, mask->break_var, ""); 194 195 lp_exec_mask_update(mask); 196} 197 198static void lp_exec_break(struct lp_exec_mask *mask) 199{ 200 LLVMBuilderRef builder = mask->bld->gallivm->builder; 201 LLVMValueRef exec_mask = LLVMBuildNot(builder, 202 mask->exec_mask, 203 "break"); 204 205 mask->break_mask = LLVMBuildAnd(builder, 206 mask->break_mask, 207 exec_mask, "break_full"); 208 209 lp_exec_mask_update(mask); 210} 211 212static void lp_exec_continue(struct lp_exec_mask *mask) 213{ 214 LLVMBuilderRef builder = mask->bld->gallivm->builder; 215 LLVMValueRef exec_mask = LLVMBuildNot(builder, 216 mask->exec_mask, 217 ""); 218 219 mask->cont_mask = LLVMBuildAnd(builder, 220 mask->cont_mask, 221 exec_mask, ""); 222 223 lp_exec_mask_update(mask); 224} 225 226 227static void lp_exec_endloop(struct gallivm_state *gallivm, 228 struct lp_exec_mask *mask) 229{ 230 LLVMBuilderRef builder = mask->bld->gallivm->builder; 231 LLVMBasicBlockRef endloop; 232 LLVMTypeRef int_type = LLVMInt32TypeInContext(mask->bld->gallivm->context); 233 LLVMTypeRef reg_type = LLVMIntTypeInContext(gallivm->context, 234 mask->bld->type.width * 235 mask->bld->type.length); 236 LLVMValueRef i1cond, i2cond, icond, limiter; 237 238 assert(mask->break_mask); 239 240 /* 241 * Restore the cont_mask, but don't pop 242 */ 243 assert(mask->loop_stack_size); 244 mask->cont_mask = mask->loop_stack[mask->loop_stack_size - 1].cont_mask; 245 lp_exec_mask_update(mask); 246 247 /* 248 * Unlike the continue mask, the break_mask must be preserved across loop 249 * iterations 250 */ 251 LLVMBuildStore(builder, mask->break_mask, mask->break_var); 252 253 /* Decrement the loop limiter */ 254 limiter = LLVMBuildLoad(builder, mask->loop_limiter, ""); 255 256 limiter = LLVMBuildSub( 257 builder, 258 limiter, 259 LLVMConstInt(int_type, 1, false), 260 ""); 261 262 LLVMBuildStore(builder, limiter, mask->loop_limiter); 263 264 /* i1cond = (mask != 0) */ 265 i1cond = LLVMBuildICmp( 266 builder, 267 LLVMIntNE, 268 LLVMBuildBitCast(builder, mask->exec_mask, reg_type, ""), 269 LLVMConstNull(reg_type), ""); 270 271 /* i2cond = (looplimiter > 0) */ 272 i2cond = LLVMBuildICmp( 273 builder, 274 LLVMIntSGT, 275 limiter, 276 LLVMConstNull(int_type), ""); 277 278 /* if( i1cond && i2cond ) */ 279 icond = LLVMBuildAnd(builder, i1cond, i2cond, ""); 280 281 endloop = lp_build_insert_new_block(mask->bld->gallivm, "endloop"); 282 283 LLVMBuildCondBr(builder, 284 icond, mask->loop_block, endloop); 285 286 LLVMPositionBuilderAtEnd(builder, endloop); 287 288 assert(mask->loop_stack_size); 289 --mask->loop_stack_size; 290 mask->loop_block = mask->loop_stack[mask->loop_stack_size].loop_block; 291 mask->cont_mask = mask->loop_stack[mask->loop_stack_size].cont_mask; 292 mask->break_mask = mask->loop_stack[mask->loop_stack_size].break_mask; 293 mask->break_var = mask->loop_stack[mask->loop_stack_size].break_var; 294 295 lp_exec_mask_update(mask); 296} 297 298/* stores val into an address pointed to by dst. 299 * mask->exec_mask is used to figure out which bits of val 300 * should be stored into the address 301 * (0 means don't store this bit, 1 means do store). 302 */ 303static void lp_exec_mask_store(struct lp_exec_mask *mask, 304 struct lp_build_context *bld_store, 305 LLVMValueRef pred, 306 LLVMValueRef val, 307 LLVMValueRef dst) 308{ 309 LLVMBuilderRef builder = mask->bld->gallivm->builder; 310 311 /* Mix the predicate and execution mask */ 312 if (mask->has_mask) { 313 if (pred) { 314 pred = LLVMBuildAnd(builder, pred, mask->exec_mask, ""); 315 } else { 316 pred = mask->exec_mask; 317 } 318 } 319 320 if (pred) { 321 LLVMValueRef real_val, dst_val; 322 323 dst_val = LLVMBuildLoad(builder, dst, ""); 324 real_val = lp_build_select(bld_store, 325 pred, 326 val, dst_val); 327 328 LLVMBuildStore(builder, real_val, dst); 329 } else 330 LLVMBuildStore(builder, val, dst); 331} 332 333static void lp_exec_mask_call(struct lp_exec_mask *mask, 334 int func, 335 int *pc) 336{ 337 assert(mask->call_stack_size < LP_MAX_TGSI_NESTING); 338 mask->call_stack[mask->call_stack_size].pc = *pc; 339 mask->call_stack[mask->call_stack_size].ret_mask = mask->ret_mask; 340 mask->call_stack_size++; 341 *pc = func; 342} 343 344static void lp_exec_mask_ret(struct lp_exec_mask *mask, int *pc) 345{ 346 LLVMBuilderRef builder = mask->bld->gallivm->builder; 347 LLVMValueRef exec_mask; 348 349 if (mask->call_stack_size == 0) { 350 /* returning from main() */ 351 *pc = -1; 352 return; 353 } 354 exec_mask = LLVMBuildNot(builder, 355 mask->exec_mask, 356 "ret"); 357 358 mask->ret_mask = LLVMBuildAnd(builder, 359 mask->ret_mask, 360 exec_mask, "ret_full"); 361 362 lp_exec_mask_update(mask); 363} 364 365static void lp_exec_mask_bgnsub(struct lp_exec_mask *mask) 366{ 367} 368 369static void lp_exec_mask_endsub(struct lp_exec_mask *mask, int *pc) 370{ 371 assert(mask->call_stack_size); 372 mask->call_stack_size--; 373 *pc = mask->call_stack[mask->call_stack_size].pc; 374 mask->ret_mask = mask->call_stack[mask->call_stack_size].ret_mask; 375 lp_exec_mask_update(mask); 376} 377 378 379/** 380 * Return pointer to a temporary register channel (src or dest). 381 * Note that indirect addressing cannot be handled here. 382 * \param index which temporary register 383 * \param chan which channel of the temp register. 384 */ 385LLVMValueRef 386lp_get_temp_ptr_soa(struct lp_build_tgsi_soa_context *bld, 387 unsigned index, 388 unsigned chan) 389{ 390 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder; 391 assert(chan < 4); 392 if (bld->indirect_files & (1 << TGSI_FILE_TEMPORARY)) { 393 LLVMValueRef lindex = lp_build_const_int32(bld->bld_base.base.gallivm, index * 4 + chan); 394 return LLVMBuildGEP(builder, bld->temps_array, &lindex, 1, ""); 395 } 396 else { 397 return bld->temps[index][chan]; 398 } 399} 400 401/** 402 * Return pointer to a output register channel (src or dest). 403 * Note that indirect addressing cannot be handled here. 404 * \param index which output register 405 * \param chan which channel of the output register. 406 */ 407LLVMValueRef 408lp_get_output_ptr(struct lp_build_tgsi_soa_context *bld, 409 unsigned index, 410 unsigned chan) 411{ 412 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder; 413 assert(chan < 4); 414 if (bld->indirect_files & (1 << TGSI_FILE_OUTPUT)) { 415 LLVMValueRef lindex = lp_build_const_int32(bld->bld_base.base.gallivm, 416 index * 4 + chan); 417 return LLVMBuildGEP(builder, bld->outputs_array, &lindex, 1, ""); 418 } 419 else { 420 return bld->outputs[index][chan]; 421 } 422} 423 424/** 425 * Gather vector. 426 * XXX the lp_build_gather() function should be capable of doing this 427 * with a little work. 428 */ 429static LLVMValueRef 430build_gather(struct lp_build_context *bld, 431 LLVMValueRef base_ptr, 432 LLVMValueRef indexes) 433{ 434 LLVMBuilderRef builder = bld->gallivm->builder; 435 LLVMValueRef res = bld->undef; 436 unsigned i; 437 438 /* 439 * Loop over elements of index_vec, load scalar value, insert it into 'res'. 440 */ 441 for (i = 0; i < bld->type.length; i++) { 442 LLVMValueRef ii = lp_build_const_int32(bld->gallivm, i); 443 LLVMValueRef index = LLVMBuildExtractElement(builder, 444 indexes, ii, ""); 445 LLVMValueRef scalar_ptr = LLVMBuildGEP(builder, base_ptr, 446 &index, 1, "gather_ptr"); 447 LLVMValueRef scalar = LLVMBuildLoad(builder, scalar_ptr, ""); 448 449 res = LLVMBuildInsertElement(builder, res, scalar, ii, ""); 450 } 451 452 return res; 453} 454 455 456/** 457 * Scatter/store vector. 458 */ 459static void 460emit_mask_scatter(struct lp_build_tgsi_soa_context *bld, 461 LLVMValueRef base_ptr, 462 LLVMValueRef indexes, 463 LLVMValueRef values, 464 struct lp_exec_mask *mask, 465 LLVMValueRef pred) 466{ 467 struct gallivm_state *gallivm = bld->bld_base.base.gallivm; 468 LLVMBuilderRef builder = gallivm->builder; 469 unsigned i; 470 471 /* Mix the predicate and execution mask */ 472 if (mask->has_mask) { 473 if (pred) { 474 pred = LLVMBuildAnd(builder, pred, mask->exec_mask, ""); 475 } 476 else { 477 pred = mask->exec_mask; 478 } 479 } 480 481 /* 482 * Loop over elements of index_vec, store scalar value. 483 */ 484 for (i = 0; i < bld->bld_base.base.type.length; i++) { 485 LLVMValueRef ii = lp_build_const_int32(gallivm, i); 486 LLVMValueRef index = LLVMBuildExtractElement(builder, indexes, ii, ""); 487 LLVMValueRef scalar_ptr = LLVMBuildGEP(builder, base_ptr, &index, 1, "scatter_ptr"); 488 LLVMValueRef val = LLVMBuildExtractElement(builder, values, ii, "scatter_val"); 489 LLVMValueRef scalar_pred = pred ? 490 LLVMBuildExtractElement(builder, pred, ii, "scatter_pred") : NULL; 491 492 if (0) 493 lp_build_printf(gallivm, "scatter %d: val %f at %d %p\n", 494 ii, val, index, scalar_ptr); 495 496 if (scalar_pred) { 497 LLVMValueRef real_val, dst_val; 498 dst_val = LLVMBuildLoad(builder, scalar_ptr, ""); 499 real_val = lp_build_select(&bld->elem_bld, scalar_pred, val, dst_val); 500 LLVMBuildStore(builder, real_val, scalar_ptr); 501 } 502 else { 503 LLVMBuildStore(builder, val, scalar_ptr); 504 } 505 } 506} 507 508 509/** 510 * Read the current value of the ADDR register, convert the floats to 511 * ints, add the base index and return the vector of offsets. 512 * The offsets will be used to index into the constant buffer or 513 * temporary register file. 514 */ 515static LLVMValueRef 516get_indirect_index(struct lp_build_tgsi_soa_context *bld, 517 unsigned reg_file, unsigned reg_index, 518 const struct tgsi_src_register *indirect_reg) 519{ 520 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder; 521 struct lp_build_context *uint_bld = &bld->bld_base.uint_bld; 522 /* always use X component of address register */ 523 unsigned swizzle = indirect_reg->SwizzleX; 524 LLVMValueRef base; 525 LLVMValueRef rel; 526 LLVMValueRef max_index; 527 LLVMValueRef index; 528 529 assert(bld->indirect_files & (1 << reg_file)); 530 531 base = lp_build_const_int_vec(bld->bld_base.base.gallivm, uint_bld->type, reg_index); 532 533 assert(swizzle < 4); 534 rel = LLVMBuildLoad(builder, 535 bld->addr[indirect_reg->Index][swizzle], 536 "load addr reg"); 537 538 index = lp_build_add(uint_bld, base, rel); 539 540 max_index = lp_build_const_int_vec(bld->bld_base.base.gallivm, 541 uint_bld->type, 542 bld->bld_base.info->file_max[reg_file]); 543 544 assert(!uint_bld->type.sign); 545 index = lp_build_min(uint_bld, index, max_index); 546 547 return index; 548} 549 550static struct lp_build_context * 551stype_to_fetch(struct lp_build_tgsi_context * bld_base, 552 enum tgsi_opcode_type stype) 553{ 554 struct lp_build_context *bld_fetch; 555 556 switch (stype) { 557 case TGSI_TYPE_FLOAT: 558 case TGSI_TYPE_UNTYPED: 559 bld_fetch = &bld_base->base; 560 break; 561 case TGSI_TYPE_UNSIGNED: 562 bld_fetch = &bld_base->uint_bld; 563 break; 564 case TGSI_TYPE_SIGNED: 565 bld_fetch = &bld_base->int_bld; 566 break; 567 case TGSI_TYPE_VOID: 568 case TGSI_TYPE_DOUBLE: 569 default: 570 assert(0); 571 bld_fetch = NULL; 572 break; 573 } 574 return bld_fetch; 575} 576 577static LLVMValueRef 578emit_fetch_constant( 579 struct lp_build_tgsi_context * bld_base, 580 const struct tgsi_full_src_register * reg, 581 enum tgsi_opcode_type stype, 582 unsigned swizzle) 583{ 584 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 585 struct gallivm_state *gallivm = bld_base->base.gallivm; 586 LLVMBuilderRef builder = gallivm->builder; 587 struct lp_build_context *uint_bld = &bld_base->uint_bld; 588 LLVMValueRef indirect_index = NULL; 589 struct lp_build_context *bld_fetch = stype_to_fetch(bld_base, stype); 590 591 /* XXX: Handle fetching xyzw components as a vector */ 592 assert(swizzle != ~0); 593 594 if (reg->Register.Indirect) { 595 indirect_index = get_indirect_index(bld, 596 reg->Register.File, 597 reg->Register.Index, 598 ®->Indirect); 599 } 600 601 if (reg->Register.Indirect) { 602 LLVMValueRef swizzle_vec = 603 lp_build_const_int_vec(bld->bld_base.base.gallivm, uint_bld->type, swizzle); 604 LLVMValueRef index_vec; /* index into the const buffer */ 605 606 /* index_vec = indirect_index * 4 + swizzle */ 607 index_vec = lp_build_shl_imm(uint_bld, indirect_index, 2); 608 index_vec = lp_build_add(uint_bld, index_vec, swizzle_vec); 609 610 /* Gather values from the constant buffer */ 611 return build_gather(bld_fetch, bld->consts_ptr, index_vec); 612 } 613 else { 614 LLVMValueRef index; /* index into the const buffer */ 615 LLVMValueRef scalar, scalar_ptr; 616 617 index = lp_build_const_int32(gallivm, reg->Register.Index*4 + swizzle); 618 619 scalar_ptr = LLVMBuildGEP(builder, bld->consts_ptr, 620 &index, 1, ""); 621 622 if (stype != TGSI_TYPE_FLOAT && stype != TGSI_TYPE_UNTYPED) { 623 LLVMTypeRef ivtype = LLVMPointerType(LLVMInt32TypeInContext(gallivm->context), 0); 624 LLVMValueRef temp_ptr; 625 temp_ptr = LLVMBuildBitCast(builder, scalar_ptr, ivtype, ""); 626 scalar = LLVMBuildLoad(builder, temp_ptr, ""); 627 } else 628 scalar = LLVMBuildLoad(builder, scalar_ptr, ""); 629 630 return lp_build_broadcast_scalar(bld_fetch, scalar); 631 } 632} 633 634static LLVMValueRef 635emit_fetch_immediate( 636 struct lp_build_tgsi_context * bld_base, 637 const struct tgsi_full_src_register * reg, 638 enum tgsi_opcode_type stype, 639 unsigned swizzle) 640{ 641 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 642 LLVMValueRef res = bld->immediates[reg->Register.Index][swizzle]; 643 assert(res); 644 645 if (stype == TGSI_TYPE_UNSIGNED) { 646 res = LLVMConstBitCast(res, bld_base->uint_bld.vec_type); 647 } else if (stype == TGSI_TYPE_SIGNED) { 648 res = LLVMConstBitCast(res, bld_base->int_bld.vec_type); 649 } 650 return res; 651} 652 653static LLVMValueRef 654emit_fetch_input( 655 struct lp_build_tgsi_context * bld_base, 656 const struct tgsi_full_src_register * reg, 657 enum tgsi_opcode_type stype, 658 unsigned swizzle) 659{ 660 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 661 struct gallivm_state *gallivm = bld->bld_base.base.gallivm; 662 LLVMBuilderRef builder = gallivm->builder; 663 struct lp_build_context *uint_bld = &bld_base->uint_bld; 664 LLVMValueRef indirect_index = NULL; 665 LLVMValueRef res; 666 667 if (reg->Register.Indirect) { 668 indirect_index = get_indirect_index(bld, 669 reg->Register.File, 670 reg->Register.Index, 671 ®->Indirect); 672 } 673 674 if (reg->Register.Indirect) { 675 LLVMValueRef swizzle_vec = 676 lp_build_const_int_vec(gallivm, uint_bld->type, swizzle); 677 LLVMValueRef length_vec = 678 lp_build_const_int_vec(gallivm, uint_bld->type, bld->bld_base.base.type.length); 679 LLVMValueRef index_vec; /* index into the const buffer */ 680 LLVMValueRef inputs_array; 681 LLVMTypeRef float4_ptr_type; 682 683 /* index_vec = (indirect_index * 4 + swizzle) * length */ 684 index_vec = lp_build_shl_imm(uint_bld, indirect_index, 2); 685 index_vec = lp_build_add(uint_bld, index_vec, swizzle_vec); 686 index_vec = lp_build_mul(uint_bld, index_vec, length_vec); 687 688 /* cast inputs_array pointer to float* */ 689 float4_ptr_type = LLVMPointerType(LLVMFloatTypeInContext(gallivm->context), 0); 690 inputs_array = LLVMBuildBitCast(builder, bld->inputs_array, 691 float4_ptr_type, ""); 692 693 /* Gather values from the temporary register array */ 694 res = build_gather(&bld_base->base, inputs_array, index_vec); 695 } else { 696 if (bld->indirect_files & (1 << TGSI_FILE_INPUT)) { 697 LLVMValueRef lindex = lp_build_const_int32(gallivm, 698 reg->Register.Index * 4 + swizzle); 699 LLVMValueRef input_ptr = LLVMBuildGEP(builder, 700 bld->inputs_array, &lindex, 1, ""); 701 res = LLVMBuildLoad(builder, input_ptr, ""); 702 } 703 else { 704 res = bld->inputs[reg->Register.Index][swizzle]; 705 } 706 } 707 assert(res); 708 return res; 709} 710 711static LLVMValueRef 712emit_fetch_temporary( 713 struct lp_build_tgsi_context * bld_base, 714 const struct tgsi_full_src_register * reg, 715 enum tgsi_opcode_type stype, 716 unsigned swizzle) 717{ 718 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 719 struct gallivm_state *gallivm = bld->bld_base.base.gallivm; 720 LLVMBuilderRef builder = gallivm->builder; 721 struct lp_build_context *uint_bld = &bld_base->uint_bld; 722 LLVMValueRef indirect_index = NULL; 723 LLVMValueRef res; 724 725 if (reg->Register.Indirect) { 726 indirect_index = get_indirect_index(bld, 727 reg->Register.File, 728 reg->Register.Index, 729 ®->Indirect); 730 } 731 732 if (reg->Register.Indirect) { 733 LLVMValueRef swizzle_vec = 734 lp_build_const_int_vec(bld->bld_base.base.gallivm, uint_bld->type, swizzle); 735 LLVMValueRef length_vec = 736 lp_build_const_int_vec(bld->bld_base.base.gallivm, uint_bld->type, 737 bld->bld_base.base.type.length); 738 LLVMValueRef index_vec; /* index into the const buffer */ 739 LLVMValueRef temps_array; 740 LLVMTypeRef float4_ptr_type; 741 742 /* index_vec = (indirect_index * 4 + swizzle) * length */ 743 index_vec = lp_build_shl_imm(uint_bld, indirect_index, 2); 744 index_vec = lp_build_add(uint_bld, index_vec, swizzle_vec); 745 index_vec = lp_build_mul(uint_bld, index_vec, length_vec); 746 747 /* cast temps_array pointer to float* */ 748 float4_ptr_type = LLVMPointerType(LLVMFloatTypeInContext(bld->bld_base.base.gallivm->context), 0); 749 temps_array = LLVMBuildBitCast(builder, bld->temps_array, 750 float4_ptr_type, ""); 751 752 /* Gather values from the temporary register array */ 753 res = build_gather(&bld_base->base, temps_array, index_vec); 754 } 755 else { 756 LLVMValueRef temp_ptr; 757 if (stype != TGSI_TYPE_FLOAT && stype != TGSI_TYPE_UNTYPED) { 758 LLVMTypeRef itype = LLVMPointerType(LLVMVectorType(LLVMInt32TypeInContext(gallivm->context), 4), 0); 759 LLVMValueRef tint_ptr = lp_get_temp_ptr_soa(bld, reg->Register.Index, 760 swizzle); 761 temp_ptr = LLVMBuildBitCast(builder, tint_ptr, itype, ""); 762 } else 763 temp_ptr = lp_get_temp_ptr_soa(bld, reg->Register.Index, swizzle); 764 res = LLVMBuildLoad(builder, temp_ptr, ""); 765 if (!res) 766 return bld->bld_base.base.undef; 767 } 768 769 return res; 770} 771 772static LLVMValueRef 773emit_fetch_system_value( 774 struct lp_build_tgsi_context * bld_base, 775 const struct tgsi_full_src_register * reg, 776 enum tgsi_opcode_type stype, 777 unsigned swizzle) 778{ 779 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 780 struct gallivm_state *gallivm = bld->bld_base.base.gallivm; 781 LLVMBuilderRef builder = gallivm->builder; 782 LLVMValueRef index; /* index into the system value array */ 783 LLVMValueRef scalar, scalar_ptr; 784 785 assert(!reg->Register.Indirect); 786 787 index = lp_build_const_int32(gallivm, reg->Register.Index * 4 + swizzle); 788 789 scalar_ptr = LLVMBuildGEP(builder, bld->system_values_array, &index, 1, ""); 790 scalar = LLVMBuildLoad(builder, scalar_ptr, ""); 791 792 return lp_build_broadcast_scalar(&bld->bld_base.base, scalar); 793} 794 795/** 796 * Register fetch with derivatives. 797 */ 798static void 799emit_fetch_deriv( 800 struct lp_build_tgsi_soa_context *bld, 801 LLVMValueRef src, 802 LLVMValueRef *res, 803 LLVMValueRef *ddx, 804 LLVMValueRef *ddy) 805{ 806 if(res) 807 *res = src; 808 809 /* TODO: use interpolation coeffs for inputs */ 810 811 if(ddx) 812 *ddx = lp_build_ddx(&bld->bld_base.base, src); 813 814 if(ddy) 815 *ddy = lp_build_ddy(&bld->bld_base.base, src); 816} 817 818 819/** 820 * Predicate. 821 */ 822static void 823emit_fetch_predicate( 824 struct lp_build_tgsi_soa_context *bld, 825 const struct tgsi_full_instruction *inst, 826 LLVMValueRef *pred) 827{ 828 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder; 829 unsigned index; 830 unsigned char swizzles[4]; 831 LLVMValueRef unswizzled[4] = {NULL, NULL, NULL, NULL}; 832 LLVMValueRef value; 833 unsigned chan; 834 835 if (!inst->Instruction.Predicate) { 836 TGSI_FOR_EACH_CHANNEL( chan ) { 837 pred[chan] = NULL; 838 } 839 return; 840 } 841 842 swizzles[0] = inst->Predicate.SwizzleX; 843 swizzles[1] = inst->Predicate.SwizzleY; 844 swizzles[2] = inst->Predicate.SwizzleZ; 845 swizzles[3] = inst->Predicate.SwizzleW; 846 847 index = inst->Predicate.Index; 848 assert(index < LP_MAX_TGSI_PREDS); 849 850 TGSI_FOR_EACH_CHANNEL( chan ) { 851 unsigned swizzle = swizzles[chan]; 852 853 /* 854 * Only fetch the predicate register channels that are actually listed 855 * in the swizzles 856 */ 857 if (!unswizzled[swizzle]) { 858 value = LLVMBuildLoad(builder, 859 bld->preds[index][swizzle], ""); 860 861 /* 862 * Convert the value to an integer mask. 863 * 864 * TODO: Short-circuit this comparison -- a D3D setp_xx instructions 865 * is needlessly causing two comparisons due to storing the intermediate 866 * result as float vector instead of an integer mask vector. 867 */ 868 value = lp_build_compare(bld->bld_base.base.gallivm, 869 bld->bld_base.base.type, 870 PIPE_FUNC_NOTEQUAL, 871 value, 872 bld->bld_base.base.zero); 873 if (inst->Predicate.Negate) { 874 value = LLVMBuildNot(builder, value, ""); 875 } 876 877 unswizzled[swizzle] = value; 878 } else { 879 value = unswizzled[swizzle]; 880 } 881 882 pred[chan] = value; 883 } 884} 885 886/** 887 * Register store. 888 */ 889static void 890emit_store_chan( 891 struct lp_build_tgsi_context *bld_base, 892 const struct tgsi_full_instruction *inst, 893 unsigned index, 894 unsigned chan_index, 895 LLVMValueRef pred, 896 LLVMValueRef value) 897{ 898 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 899 struct gallivm_state *gallivm = bld->bld_base.base.gallivm; 900 LLVMBuilderRef builder = gallivm->builder; 901 const struct tgsi_full_dst_register *reg = &inst->Dst[index]; 902 struct lp_build_context *uint_bld = &bld_base->uint_bld; 903 LLVMValueRef indirect_index = NULL; 904 struct lp_build_context *bld_store; 905 enum tgsi_opcode_type dtype = tgsi_opcode_infer_dst_type(inst->Instruction.Opcode); 906 907 switch (dtype) { 908 default: 909 case TGSI_TYPE_FLOAT: 910 case TGSI_TYPE_UNTYPED: 911 bld_store = &bld_base->base; 912 break; 913 case TGSI_TYPE_UNSIGNED: 914 bld_store = &bld_base->uint_bld; 915 break; 916 case TGSI_TYPE_SIGNED: 917 bld_store = &bld_base->int_bld; 918 break; 919 case TGSI_TYPE_DOUBLE: 920 case TGSI_TYPE_VOID: 921 assert(0); 922 bld_store = NULL; 923 break; 924 } 925 926 switch( inst->Instruction.Saturate ) { 927 case TGSI_SAT_NONE: 928 break; 929 930 case TGSI_SAT_ZERO_ONE: 931 value = lp_build_max(&bld->bld_base.base, value, bld->bld_base.base.zero); 932 value = lp_build_min(&bld->bld_base.base, value, bld->bld_base.base.one); 933 break; 934 935 case TGSI_SAT_MINUS_PLUS_ONE: 936 value = lp_build_max(&bld->bld_base.base, value, lp_build_const_vec(bld->bld_base.base.gallivm, bld->bld_base.base.type, -1.0)); 937 value = lp_build_min(&bld->bld_base.base, value, bld->bld_base.base.one); 938 break; 939 940 default: 941 assert(0); 942 } 943 944 if (reg->Register.Indirect) { 945 indirect_index = get_indirect_index(bld, 946 reg->Register.File, 947 reg->Register.Index, 948 ®->Indirect); 949 } else { 950 assert(reg->Register.Index <= 951 bld->bld_base.info->file_max[reg->Register.File]); 952 } 953 954 switch( reg->Register.File ) { 955 case TGSI_FILE_OUTPUT: 956 if (reg->Register.Indirect) { 957 LLVMValueRef chan_vec = 958 lp_build_const_int_vec(gallivm, uint_bld->type, chan_index); 959 LLVMValueRef length_vec = 960 lp_build_const_int_vec(gallivm, uint_bld->type, bld->bld_base.base.type.length); 961 LLVMValueRef index_vec; /* indexes into the temp registers */ 962 LLVMValueRef outputs_array; 963 LLVMValueRef pixel_offsets; 964 LLVMTypeRef float_ptr_type; 965 int i; 966 967 /* build pixel offset vector: {0, 1, 2, 3, ...} */ 968 pixel_offsets = uint_bld->undef; 969 for (i = 0; i < bld->bld_base.base.type.length; i++) { 970 LLVMValueRef ii = lp_build_const_int32(gallivm, i); 971 pixel_offsets = LLVMBuildInsertElement(builder, pixel_offsets, 972 ii, ii, ""); 973 } 974 975 /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */ 976 index_vec = lp_build_shl_imm(uint_bld, indirect_index, 2); 977 index_vec = lp_build_add(uint_bld, index_vec, chan_vec); 978 index_vec = lp_build_mul(uint_bld, index_vec, length_vec); 979 index_vec = lp_build_add(uint_bld, index_vec, pixel_offsets); 980 981 float_ptr_type = 982 LLVMPointerType(LLVMFloatTypeInContext(gallivm->context), 0); 983 outputs_array = LLVMBuildBitCast(builder, bld->outputs_array, 984 float_ptr_type, ""); 985 986 /* Scatter store values into temp registers */ 987 emit_mask_scatter(bld, outputs_array, index_vec, value, 988 &bld->exec_mask, pred); 989 } 990 else { 991 LLVMValueRef out_ptr = lp_get_output_ptr(bld, reg->Register.Index, 992 chan_index); 993 lp_exec_mask_store(&bld->exec_mask, bld_store, pred, value, out_ptr); 994 } 995 break; 996 997 case TGSI_FILE_TEMPORARY: 998 if (reg->Register.Indirect) { 999 LLVMValueRef chan_vec = 1000 lp_build_const_int_vec(gallivm, uint_bld->type, chan_index); 1001 LLVMValueRef length_vec = 1002 lp_build_const_int_vec(gallivm, uint_bld->type, 1003 bld->bld_base.base.type.length); 1004 LLVMValueRef index_vec; /* indexes into the temp registers */ 1005 LLVMValueRef temps_array; 1006 LLVMValueRef pixel_offsets; 1007 LLVMTypeRef float_ptr_type; 1008 int i; 1009 1010 /* build pixel offset vector: {0, 1, 2, 3, ...} */ 1011 pixel_offsets = uint_bld->undef; 1012 for (i = 0; i < bld->bld_base.base.type.length; i++) { 1013 LLVMValueRef ii = lp_build_const_int32(gallivm, i); 1014 pixel_offsets = LLVMBuildInsertElement(builder, pixel_offsets, 1015 ii, ii, ""); 1016 } 1017 1018 /* index_vec = (indirect_index * 4 + chan_index) * length + offsets */ 1019 index_vec = lp_build_shl_imm(uint_bld, indirect_index, 2); 1020 index_vec = lp_build_add(uint_bld, index_vec, chan_vec); 1021 index_vec = lp_build_mul(uint_bld, index_vec, length_vec); 1022 index_vec = lp_build_add(uint_bld, index_vec, pixel_offsets); 1023 1024 float_ptr_type = 1025 LLVMPointerType(LLVMFloatTypeInContext(gallivm->context), 0); 1026 temps_array = LLVMBuildBitCast(builder, bld->temps_array, 1027 float_ptr_type, ""); 1028 1029 /* Scatter store values into temp registers */ 1030 emit_mask_scatter(bld, temps_array, index_vec, value, 1031 &bld->exec_mask, pred); 1032 } 1033 else { 1034 LLVMValueRef temp_ptr; 1035 1036 switch (dtype) { 1037 case TGSI_TYPE_UNSIGNED: 1038 case TGSI_TYPE_SIGNED: { 1039 LLVMTypeRef itype = LLVMVectorType(LLVMInt32TypeInContext(gallivm->context), 4); 1040 LLVMTypeRef ivtype = LLVMPointerType(itype, 0); 1041 LLVMValueRef tint_ptr = lp_get_temp_ptr_soa(bld, reg->Register.Index, 1042 chan_index); 1043 LLVMValueRef temp_value_ptr; 1044 1045 temp_ptr = LLVMBuildBitCast(builder, tint_ptr, ivtype, ""); 1046 temp_value_ptr = LLVMBuildBitCast(builder, value, itype, ""); 1047 value = temp_value_ptr; 1048 break; 1049 } 1050 default: 1051 case TGSI_TYPE_FLOAT: 1052 case TGSI_TYPE_UNTYPED: 1053 temp_ptr = lp_get_temp_ptr_soa(bld, reg->Register.Index, 1054 chan_index); 1055 break; 1056 } 1057 1058 lp_exec_mask_store(&bld->exec_mask, bld_store, pred, value, temp_ptr); 1059 } 1060 break; 1061 1062 case TGSI_FILE_ADDRESS: 1063 assert(dtype == TGSI_TYPE_SIGNED); 1064 assert(LLVMTypeOf(value) == bld_base->base.int_vec_type); 1065 lp_exec_mask_store(&bld->exec_mask, bld_store, pred, value, 1066 bld->addr[reg->Register.Index][chan_index]); 1067 break; 1068 1069 case TGSI_FILE_PREDICATE: 1070 lp_exec_mask_store(&bld->exec_mask, bld_store, pred, value, 1071 bld->preds[reg->Register.Index][chan_index]); 1072 break; 1073 1074 default: 1075 assert( 0 ); 1076 } 1077} 1078 1079static void 1080emit_store( 1081 struct lp_build_tgsi_context * bld_base, 1082 const struct tgsi_full_instruction * inst, 1083 const struct tgsi_opcode_info * info, 1084 LLVMValueRef dst[4]) 1085 1086{ 1087 unsigned chan_index; 1088 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1089 1090 if(info->num_dst) { 1091 LLVMValueRef pred[TGSI_NUM_CHANNELS]; 1092 1093 emit_fetch_predicate( bld, inst, pred ); 1094 1095 TGSI_FOR_EACH_DST0_ENABLED_CHANNEL( inst, chan_index ) { 1096 emit_store_chan(bld_base, inst, 0, chan_index, pred[chan_index], dst[chan_index]); 1097 } 1098 } 1099} 1100 1101/** 1102 * High-level instruction translators. 1103 */ 1104 1105static void 1106emit_tex( struct lp_build_tgsi_soa_context *bld, 1107 const struct tgsi_full_instruction *inst, 1108 enum lp_build_tex_modifier modifier, 1109 LLVMValueRef *texel) 1110{ 1111 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder; 1112 unsigned unit; 1113 LLVMValueRef lod_bias, explicit_lod; 1114 LLVMValueRef oow = NULL; 1115 LLVMValueRef coords[3]; 1116 LLVMValueRef ddx[3]; 1117 LLVMValueRef ddy[3]; 1118 unsigned num_coords; 1119 unsigned i; 1120 1121 if (!bld->sampler) { 1122 _debug_printf("warning: found texture instruction but no sampler generator supplied\n"); 1123 for (i = 0; i < 4; i++) { 1124 texel[i] = bld->bld_base.base.undef; 1125 } 1126 return; 1127 } 1128 1129 switch (inst->Texture.Texture) { 1130 case TGSI_TEXTURE_1D: 1131 num_coords = 1; 1132 break; 1133 case TGSI_TEXTURE_1D_ARRAY: 1134 case TGSI_TEXTURE_2D: 1135 case TGSI_TEXTURE_RECT: 1136 num_coords = 2; 1137 break; 1138 case TGSI_TEXTURE_SHADOW1D: 1139 case TGSI_TEXTURE_SHADOW1D_ARRAY: 1140 case TGSI_TEXTURE_SHADOW2D: 1141 case TGSI_TEXTURE_SHADOWRECT: 1142 case TGSI_TEXTURE_2D_ARRAY: 1143 case TGSI_TEXTURE_3D: 1144 case TGSI_TEXTURE_CUBE: 1145 num_coords = 3; 1146 break; 1147 case TGSI_TEXTURE_SHADOW2D_ARRAY: 1148 num_coords = 4; 1149 break; 1150 default: 1151 assert(0); 1152 return; 1153 } 1154 1155 if (modifier == LP_BLD_TEX_MODIFIER_LOD_BIAS) { 1156 lod_bias = lp_build_emit_fetch( &bld->bld_base, inst, 0, 3 ); 1157 explicit_lod = NULL; 1158 } 1159 else if (modifier == LP_BLD_TEX_MODIFIER_EXPLICIT_LOD) { 1160 lod_bias = NULL; 1161 explicit_lod = lp_build_emit_fetch( &bld->bld_base, inst, 0, 3 ); 1162 } 1163 else { 1164 lod_bias = NULL; 1165 explicit_lod = NULL; 1166 } 1167 1168 if (modifier == LP_BLD_TEX_MODIFIER_PROJECTED) { 1169 oow = lp_build_emit_fetch( &bld->bld_base, inst, 0, 3 ); 1170 oow = lp_build_rcp(&bld->bld_base.base, oow); 1171 } 1172 1173 for (i = 0; i < num_coords; i++) { 1174 coords[i] = lp_build_emit_fetch( &bld->bld_base, inst, 0, i ); 1175 if (modifier == LP_BLD_TEX_MODIFIER_PROJECTED) 1176 coords[i] = lp_build_mul(&bld->bld_base.base, coords[i], oow); 1177 } 1178 for (i = num_coords; i < 3; i++) { 1179 coords[i] = bld->bld_base.base.undef; 1180 } 1181 1182 if (modifier == LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV) { 1183 LLVMValueRef index0 = lp_build_const_int32(bld->bld_base.base.gallivm, 0); 1184 for (i = 0; i < num_coords; i++) { 1185 LLVMValueRef src1 = lp_build_emit_fetch( &bld->bld_base, inst, 1, i ); 1186 LLVMValueRef src2 = lp_build_emit_fetch( &bld->bld_base, inst, 2, i ); 1187 ddx[i] = LLVMBuildExtractElement(builder, src1, index0, ""); 1188 ddy[i] = LLVMBuildExtractElement(builder, src2, index0, ""); 1189 } 1190 unit = inst->Src[3].Register.Index; 1191 } else { 1192 for (i = 0; i < num_coords; i++) { 1193 ddx[i] = lp_build_scalar_ddx( &bld->bld_base.base, coords[i] ); 1194 ddy[i] = lp_build_scalar_ddy( &bld->bld_base.base, coords[i] ); 1195 } 1196 unit = inst->Src[1].Register.Index; 1197 } 1198 for (i = num_coords; i < 3; i++) { 1199 ddx[i] = LLVMGetUndef(bld->bld_base.base.elem_type); 1200 ddy[i] = LLVMGetUndef(bld->bld_base.base.elem_type); 1201 } 1202 1203 bld->sampler->emit_fetch_texel(bld->sampler, 1204 bld->bld_base.base.gallivm, 1205 bld->bld_base.base.type, 1206 unit, num_coords, coords, 1207 ddx, ddy, 1208 lod_bias, explicit_lod, 1209 texel); 1210} 1211 1212static boolean 1213near_end_of_shader(struct lp_build_tgsi_soa_context *bld, 1214 int pc) 1215{ 1216 int i; 1217 1218 for (i = 0; i < 5; i++) { 1219 unsigned opcode; 1220 1221 if (pc + i >= bld->bld_base.info->num_instructions) 1222 return TRUE; 1223 1224 opcode = bld->bld_base.instructions[pc + i].Instruction.Opcode; 1225 1226 if (opcode == TGSI_OPCODE_END) 1227 return TRUE; 1228 1229 if (opcode == TGSI_OPCODE_TEX || 1230 opcode == TGSI_OPCODE_TXP || 1231 opcode == TGSI_OPCODE_TXD || 1232 opcode == TGSI_OPCODE_TXB || 1233 opcode == TGSI_OPCODE_TXL || 1234 opcode == TGSI_OPCODE_TXF || 1235 opcode == TGSI_OPCODE_TXQ || 1236 opcode == TGSI_OPCODE_CAL || 1237 opcode == TGSI_OPCODE_CALLNZ || 1238 opcode == TGSI_OPCODE_IF || 1239 opcode == TGSI_OPCODE_IFC || 1240 opcode == TGSI_OPCODE_BGNLOOP || 1241 opcode == TGSI_OPCODE_SWITCH) 1242 return FALSE; 1243 } 1244 1245 return TRUE; 1246} 1247 1248 1249 1250/** 1251 * Kill fragment if any of the src register values are negative. 1252 */ 1253static void 1254emit_kil( 1255 struct lp_build_tgsi_soa_context *bld, 1256 const struct tgsi_full_instruction *inst, 1257 int pc) 1258{ 1259 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder; 1260 const struct tgsi_full_src_register *reg = &inst->Src[0]; 1261 LLVMValueRef terms[TGSI_NUM_CHANNELS]; 1262 LLVMValueRef mask; 1263 unsigned chan_index; 1264 1265 memset(&terms, 0, sizeof terms); 1266 1267 TGSI_FOR_EACH_CHANNEL( chan_index ) { 1268 unsigned swizzle; 1269 1270 /* Unswizzle channel */ 1271 swizzle = tgsi_util_get_full_src_register_swizzle( reg, chan_index ); 1272 1273 /* Check if the component has not been already tested. */ 1274 assert(swizzle < TGSI_NUM_CHANNELS); 1275 if( !terms[swizzle] ) 1276 /* TODO: change the comparison operator instead of setting the sign */ 1277 terms[swizzle] = lp_build_emit_fetch(&bld->bld_base, inst, 0, chan_index ); 1278 } 1279 1280 mask = NULL; 1281 TGSI_FOR_EACH_CHANNEL( chan_index ) { 1282 if(terms[chan_index]) { 1283 LLVMValueRef chan_mask; 1284 1285 /* 1286 * If term < 0 then mask = 0 else mask = ~0. 1287 */ 1288 chan_mask = lp_build_cmp(&bld->bld_base.base, PIPE_FUNC_GEQUAL, terms[chan_index], bld->bld_base.base.zero); 1289 1290 if(mask) 1291 mask = LLVMBuildAnd(builder, mask, chan_mask, ""); 1292 else 1293 mask = chan_mask; 1294 } 1295 } 1296 1297 if(mask) { 1298 lp_build_mask_update(bld->mask, mask); 1299 1300 if (!near_end_of_shader(bld, pc)) 1301 lp_build_mask_check(bld->mask); 1302 } 1303} 1304 1305 1306/** 1307 * Predicated fragment kill. 1308 * XXX Actually, we do an unconditional kill (as in tgsi_exec.c). 1309 * The only predication is the execution mask which will apply if 1310 * we're inside a loop or conditional. 1311 */ 1312static void 1313emit_kilp(struct lp_build_tgsi_soa_context *bld, 1314 int pc) 1315{ 1316 LLVMBuilderRef builder = bld->bld_base.base.gallivm->builder; 1317 LLVMValueRef mask; 1318 1319 /* For those channels which are "alive", disable fragment shader 1320 * execution. 1321 */ 1322 if (bld->exec_mask.has_mask) { 1323 mask = LLVMBuildNot(builder, bld->exec_mask.exec_mask, "kilp"); 1324 } 1325 else { 1326 LLVMValueRef zero = LLVMConstNull(bld->bld_base.base.int_vec_type); 1327 mask = zero; 1328 } 1329 1330 lp_build_mask_update(bld->mask, mask); 1331 1332 if (!near_end_of_shader(bld, pc)) 1333 lp_build_mask_check(bld->mask); 1334} 1335 1336 1337/** 1338 * Emit code which will dump the value of all the temporary registers 1339 * to stdout. 1340 */ 1341static void 1342emit_dump_temps(struct lp_build_tgsi_soa_context *bld) 1343{ 1344 struct gallivm_state *gallivm = bld->bld_base.base.gallivm; 1345 LLVMBuilderRef builder = gallivm->builder; 1346 LLVMValueRef temp_ptr; 1347 LLVMValueRef i0 = lp_build_const_int32(gallivm, 0); 1348 LLVMValueRef i1 = lp_build_const_int32(gallivm, 1); 1349 LLVMValueRef i2 = lp_build_const_int32(gallivm, 2); 1350 LLVMValueRef i3 = lp_build_const_int32(gallivm, 3); 1351 int index; 1352 int n = bld->bld_base.info->file_max[TGSI_FILE_TEMPORARY]; 1353 1354 for (index = 0; index < n; index++) { 1355 LLVMValueRef idx = lp_build_const_int32(gallivm, index); 1356 LLVMValueRef v[4][4], res; 1357 int chan; 1358 1359 lp_build_printf(gallivm, "TEMP[%d]:\n", idx); 1360 1361 for (chan = 0; chan < 4; chan++) { 1362 temp_ptr = lp_get_temp_ptr_soa(bld, index, chan); 1363 res = LLVMBuildLoad(builder, temp_ptr, ""); 1364 v[chan][0] = LLVMBuildExtractElement(builder, res, i0, ""); 1365 v[chan][1] = LLVMBuildExtractElement(builder, res, i1, ""); 1366 v[chan][2] = LLVMBuildExtractElement(builder, res, i2, ""); 1367 v[chan][3] = LLVMBuildExtractElement(builder, res, i3, ""); 1368 } 1369 1370 lp_build_printf(gallivm, " X: %f %f %f %f\n", 1371 v[0][0], v[0][1], v[0][2], v[0][3]); 1372 lp_build_printf(gallivm, " Y: %f %f %f %f\n", 1373 v[1][0], v[1][1], v[1][2], v[1][3]); 1374 lp_build_printf(gallivm, " Z: %f %f %f %f\n", 1375 v[2][0], v[2][1], v[2][2], v[2][3]); 1376 lp_build_printf(gallivm, " W: %f %f %f %f\n", 1377 v[3][0], v[3][1], v[3][2], v[3][3]); 1378 } 1379} 1380 1381 1382 1383void 1384lp_emit_declaration_soa( 1385 struct lp_build_tgsi_context *bld_base, 1386 const struct tgsi_full_declaration *decl) 1387{ 1388 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base); 1389 struct gallivm_state *gallivm = bld->bld_base.base.gallivm; 1390 LLVMTypeRef vec_type = bld->bld_base.base.vec_type; 1391 const unsigned first = decl->Range.First; 1392 const unsigned last = decl->Range.Last; 1393 unsigned idx, i; 1394 1395 for (idx = first; idx <= last; ++idx) { 1396 assert(last <= bld->bld_base.info->file_max[decl->Declaration.File]); 1397 switch (decl->Declaration.File) { 1398 case TGSI_FILE_TEMPORARY: 1399 assert(idx < LP_MAX_TGSI_TEMPS); 1400 if (!(bld->indirect_files & (1 << TGSI_FILE_TEMPORARY))) { 1401 for (i = 0; i < TGSI_NUM_CHANNELS; i++) 1402 bld->temps[idx][i] = lp_build_alloca(gallivm, vec_type, "temp"); 1403 } 1404 break; 1405 1406 case TGSI_FILE_OUTPUT: 1407 if (!(bld->indirect_files & (1 << TGSI_FILE_OUTPUT))) { 1408 for (i = 0; i < TGSI_NUM_CHANNELS; i++) 1409 bld->outputs[idx][i] = lp_build_alloca(gallivm, 1410 vec_type, "output"); 1411 } 1412 break; 1413 1414 case TGSI_FILE_ADDRESS: 1415 /* ADDR registers are the only allocated with an integer LLVM IR type, 1416 * as they are guaranteed to always have integers. 1417 * XXX: Not sure if this exception is worthwhile (or the whole idea of 1418 * an ADDR register for that matter). 1419 */ 1420 assert(idx < LP_MAX_TGSI_ADDRS); 1421 for (i = 0; i < TGSI_NUM_CHANNELS; i++) 1422 bld->addr[idx][i] = lp_build_alloca(gallivm, bld_base->base.int_vec_type, "addr"); 1423 break; 1424 1425 case TGSI_FILE_PREDICATE: 1426 assert(idx < LP_MAX_TGSI_PREDS); 1427 for (i = 0; i < TGSI_NUM_CHANNELS; i++) 1428 bld->preds[idx][i] = lp_build_alloca(gallivm, vec_type, 1429 "predicate"); 1430 break; 1431 1432 default: 1433 /* don't need to declare other vars */ 1434 break; 1435 } 1436 } 1437} 1438 1439 1440void lp_emit_immediate_soa( 1441 struct lp_build_tgsi_context *bld_base, 1442 const struct tgsi_full_immediate *imm) 1443{ 1444 struct lp_build_tgsi_soa_context *bld = lp_soa_context(bld_base); 1445 struct gallivm_state * gallivm = bld_base->base.gallivm; 1446 1447 /* simply copy the immediate values into the next immediates[] slot */ 1448 unsigned i; 1449 const uint size = imm->Immediate.NrTokens - 1; 1450 assert(size <= 4); 1451 assert(bld->num_immediates < LP_MAX_TGSI_IMMEDIATES); 1452 switch (imm->Immediate.DataType) { 1453 case TGSI_IMM_FLOAT32: 1454 for( i = 0; i < size; ++i ) 1455 bld->immediates[bld->num_immediates][i] = 1456 lp_build_const_vec(gallivm, bld_base->base.type, imm->u[i].Float); 1457 1458 break; 1459 case TGSI_IMM_UINT32: 1460 for( i = 0; i < size; ++i ) { 1461 LLVMValueRef tmp = lp_build_const_vec(gallivm, bld_base->uint_bld.type, imm->u[i].Uint); 1462 bld->immediates[bld->num_immediates][i] = 1463 LLVMConstBitCast(tmp, bld_base->base.vec_type); 1464 } 1465 1466 break; 1467 case TGSI_IMM_INT32: 1468 for( i = 0; i < size; ++i ) { 1469 LLVMValueRef tmp = lp_build_const_vec(gallivm, bld_base->int_bld.type, imm->u[i].Int); 1470 bld->immediates[bld->num_immediates][i] = 1471 LLVMConstBitCast(tmp, bld_base->base.vec_type); 1472 } 1473 1474 break; 1475 } 1476 for( i = size; i < 4; ++i ) 1477 bld->immediates[bld->num_immediates][i] = bld_base->base.undef; 1478 1479 bld->num_immediates++; 1480} 1481 1482static void 1483ddx_emit( 1484 const struct lp_build_tgsi_action * action, 1485 struct lp_build_tgsi_context * bld_base, 1486 struct lp_build_emit_data * emit_data) 1487{ 1488 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1489 1490 emit_fetch_deriv(bld, emit_data->args[0], NULL, 1491 &emit_data->output[emit_data->chan], NULL); 1492} 1493 1494static void 1495ddy_emit( 1496 const struct lp_build_tgsi_action * action, 1497 struct lp_build_tgsi_context * bld_base, 1498 struct lp_build_emit_data * emit_data) 1499{ 1500 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1501 1502 emit_fetch_deriv(bld, emit_data->args[0], NULL, NULL, 1503 &emit_data->output[emit_data->chan]); 1504} 1505 1506static void 1507kilp_emit( 1508 const struct lp_build_tgsi_action * action, 1509 struct lp_build_tgsi_context * bld_base, 1510 struct lp_build_emit_data * emit_data) 1511{ 1512 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1513 1514 emit_kilp(bld, bld_base->pc - 1); 1515} 1516 1517static void 1518kil_emit( 1519 const struct lp_build_tgsi_action * action, 1520 struct lp_build_tgsi_context * bld_base, 1521 struct lp_build_emit_data * emit_data) 1522{ 1523 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1524 1525 emit_kil(bld, emit_data->inst, bld_base->pc - 1); 1526} 1527 1528static void 1529tex_emit( 1530 const struct lp_build_tgsi_action * action, 1531 struct lp_build_tgsi_context * bld_base, 1532 struct lp_build_emit_data * emit_data) 1533{ 1534 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1535 1536 emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_NONE, emit_data->output); 1537} 1538 1539static void 1540txb_emit( 1541 const struct lp_build_tgsi_action * action, 1542 struct lp_build_tgsi_context * bld_base, 1543 struct lp_build_emit_data * emit_data) 1544{ 1545 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1546 1547 emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_LOD_BIAS, 1548 emit_data->output); 1549} 1550 1551static void 1552txd_emit( 1553 const struct lp_build_tgsi_action * action, 1554 struct lp_build_tgsi_context * bld_base, 1555 struct lp_build_emit_data * emit_data) 1556{ 1557 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1558 1559 emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV, 1560 emit_data->output); 1561} 1562 1563static void 1564txl_emit( 1565 const struct lp_build_tgsi_action * action, 1566 struct lp_build_tgsi_context * bld_base, 1567 struct lp_build_emit_data * emit_data) 1568{ 1569 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1570 1571 emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_EXPLICIT_LOD, 1572 emit_data->output); 1573} 1574 1575static void 1576txp_emit( 1577 const struct lp_build_tgsi_action * action, 1578 struct lp_build_tgsi_context * bld_base, 1579 struct lp_build_emit_data * emit_data) 1580{ 1581 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1582 1583 emit_tex(bld, emit_data->inst, LP_BLD_TEX_MODIFIER_PROJECTED, 1584 emit_data->output); 1585} 1586 1587static void 1588cal_emit( 1589 const struct lp_build_tgsi_action * action, 1590 struct lp_build_tgsi_context * bld_base, 1591 struct lp_build_emit_data * emit_data) 1592{ 1593 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1594 1595 lp_exec_mask_call(&bld->exec_mask, emit_data->inst->Label.Label, 1596 &bld_base->pc); 1597} 1598 1599static void 1600ret_emit( 1601 const struct lp_build_tgsi_action * action, 1602 struct lp_build_tgsi_context * bld_base, 1603 struct lp_build_emit_data * emit_data) 1604{ 1605 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1606 1607 lp_exec_mask_ret(&bld->exec_mask, &bld_base->pc); 1608} 1609 1610static void 1611brk_emit( 1612 const struct lp_build_tgsi_action * action, 1613 struct lp_build_tgsi_context * bld_base, 1614 struct lp_build_emit_data * emit_data) 1615{ 1616 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1617 1618 lp_exec_break(&bld->exec_mask); 1619} 1620 1621static void 1622if_emit( 1623 const struct lp_build_tgsi_action * action, 1624 struct lp_build_tgsi_context * bld_base, 1625 struct lp_build_emit_data * emit_data) 1626{ 1627 LLVMValueRef tmp; 1628 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1629 1630 tmp = lp_build_cmp(&bld_base->base, PIPE_FUNC_NOTEQUAL, 1631 emit_data->args[0], bld->bld_base.base.zero); 1632 lp_exec_mask_cond_push(&bld->exec_mask, tmp); 1633} 1634 1635static void 1636bgnloop_emit( 1637 const struct lp_build_tgsi_action * action, 1638 struct lp_build_tgsi_context * bld_base, 1639 struct lp_build_emit_data * emit_data) 1640{ 1641 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1642 1643 lp_exec_bgnloop(&bld->exec_mask); 1644} 1645 1646static void 1647bgnsub_emit( 1648 const struct lp_build_tgsi_action * action, 1649 struct lp_build_tgsi_context * bld_base, 1650 struct lp_build_emit_data * emit_data) 1651{ 1652 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1653 1654 lp_exec_mask_bgnsub(&bld->exec_mask); 1655} 1656 1657static void 1658else_emit( 1659 const struct lp_build_tgsi_action * action, 1660 struct lp_build_tgsi_context * bld_base, 1661 struct lp_build_emit_data * emit_data) 1662{ 1663 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1664 1665 lp_exec_mask_cond_invert(&bld->exec_mask); 1666} 1667 1668static void 1669endif_emit( 1670 const struct lp_build_tgsi_action * action, 1671 struct lp_build_tgsi_context * bld_base, 1672 struct lp_build_emit_data * emit_data) 1673{ 1674 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1675 1676 lp_exec_mask_cond_pop(&bld->exec_mask); 1677} 1678 1679static void 1680endloop_emit( 1681 const struct lp_build_tgsi_action * action, 1682 struct lp_build_tgsi_context * bld_base, 1683 struct lp_build_emit_data * emit_data) 1684{ 1685 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1686 1687 lp_exec_endloop(bld_base->base.gallivm, &bld->exec_mask); 1688} 1689 1690static void 1691endsub_emit( 1692 const struct lp_build_tgsi_action * action, 1693 struct lp_build_tgsi_context * bld_base, 1694 struct lp_build_emit_data * emit_data) 1695{ 1696 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1697 1698 lp_exec_mask_endsub(&bld->exec_mask, &bld_base->pc); 1699} 1700 1701static void 1702cont_emit( 1703 const struct lp_build_tgsi_action * action, 1704 struct lp_build_tgsi_context * bld_base, 1705 struct lp_build_emit_data * emit_data) 1706{ 1707 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1708 1709 lp_exec_continue(&bld->exec_mask); 1710} 1711 1712/* XXX: Refactor and move it to lp_bld_tgsi_action.c 1713 * 1714 * XXX: What do the comments about xmm registers mean? Maybe they are left over 1715 * from old code, but there is no garauntee that LLVM will use those registers 1716 * for this code. 1717 * 1718 * XXX: There should be no calls to lp_build_emit_fetch in this function. This 1719 * should be handled by the emit_data->fetch_args function. */ 1720static void 1721nrm_emit( 1722 const struct lp_build_tgsi_action * action, 1723 struct lp_build_tgsi_context * bld_base, 1724 struct lp_build_emit_data * emit_data) 1725{ 1726 LLVMValueRef tmp0, tmp1; 1727 LLVMValueRef tmp4 = NULL; 1728 LLVMValueRef tmp5 = NULL; 1729 LLVMValueRef tmp6 = NULL; 1730 LLVMValueRef tmp7 = NULL; 1731 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1732 1733 uint dims = (emit_data->inst->Instruction.Opcode == TGSI_OPCODE_NRM) ? 3 : 4; 1734 1735 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_X) || 1736 TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_Y) || 1737 TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_Z) || 1738 (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_W) && dims == 4)) { 1739 1740 /* NOTE: Cannot use xmm regs 2/3 here (see emit_rsqrt() above). */ 1741 1742 /* xmm4 = src.x */ 1743 /* xmm0 = src.x * src.x */ 1744 tmp0 = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 0, TGSI_CHAN_X); 1745 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_X)) { 1746 tmp4 = tmp0; 1747 } 1748 tmp0 = lp_build_mul( &bld->bld_base.base, tmp0, tmp0); 1749 1750 /* xmm5 = src.y */ 1751 /* xmm0 = xmm0 + src.y * src.y */ 1752 tmp1 = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 0, TGSI_CHAN_Y); 1753 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_Y)) { 1754 tmp5 = tmp1; 1755 } 1756 tmp1 = lp_build_mul( &bld->bld_base.base, tmp1, tmp1); 1757 tmp0 = lp_build_add( &bld->bld_base.base, tmp0, tmp1); 1758 1759 /* xmm6 = src.z */ 1760 /* xmm0 = xmm0 + src.z * src.z */ 1761 tmp1 = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 0, TGSI_CHAN_Z); 1762 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_Z)) { 1763 tmp6 = tmp1; 1764 } 1765 tmp1 = lp_build_mul( &bld->bld_base.base, tmp1, tmp1); 1766 tmp0 = lp_build_add( &bld->bld_base.base, tmp0, tmp1); 1767 1768 if (dims == 4) { 1769 /* xmm7 = src.w */ 1770 /* xmm0 = xmm0 + src.w * src.w */ 1771 tmp1 = lp_build_emit_fetch(&bld->bld_base, emit_data->inst, 0, TGSI_CHAN_W); 1772 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_W)) { 1773 tmp7 = tmp1; 1774 } 1775 tmp1 = lp_build_mul( &bld->bld_base.base, tmp1, tmp1); 1776 tmp0 = lp_build_add( &bld->bld_base.base, tmp0, tmp1); 1777 } 1778 /* xmm1 = 1 / sqrt(xmm0) */ 1779 tmp1 = lp_build_rsqrt( &bld->bld_base.base, tmp0); 1780 /* dst.x = xmm1 * src.x */ 1781 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_X)) { 1782 emit_data->output[TGSI_CHAN_X] = lp_build_mul( &bld->bld_base.base, tmp4, tmp1); 1783 } 1784 /* dst.y = xmm1 * src.y */ 1785 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_Y)) { 1786 emit_data->output[TGSI_CHAN_Y] = lp_build_mul( &bld->bld_base.base, tmp5, tmp1); 1787 } 1788 1789 /* dst.z = xmm1 * src.z */ 1790 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_Z)) { 1791 emit_data->output[TGSI_CHAN_Z] = lp_build_mul( &bld->bld_base.base, tmp6, tmp1); 1792 } 1793 /* dst.w = xmm1 * src.w */ 1794 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_X) && dims == 4) { 1795 emit_data->output[TGSI_CHAN_W] = lp_build_mul( &bld->bld_base.base, tmp7, tmp1); 1796 } 1797 } 1798 1799 /* dst.w = 1.0 */ 1800 if (TGSI_IS_DST0_CHANNEL_ENABLED(emit_data->inst, TGSI_CHAN_W) && dims == 3) { 1801 emit_data->output[TGSI_CHAN_W] = bld->bld_base.base.one; 1802 } 1803} 1804 1805static void emit_prologue(struct lp_build_tgsi_context * bld_base) 1806{ 1807 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1808 struct gallivm_state * gallivm = bld_base->base.gallivm; 1809 1810 if (bld->indirect_files & (1 << TGSI_FILE_TEMPORARY)) { 1811 LLVMValueRef array_size = 1812 lp_build_const_int32(gallivm, 1813 bld_base->info->file_max[TGSI_FILE_TEMPORARY] * 4 + 4); 1814 bld->temps_array = lp_build_array_alloca(gallivm, 1815 bld_base->base.vec_type, array_size, 1816 "temp_array"); 1817 } 1818 1819 if (bld->indirect_files & (1 << TGSI_FILE_OUTPUT)) { 1820 LLVMValueRef array_size = 1821 lp_build_const_int32(gallivm, 1822 bld_base->info->file_max[TGSI_FILE_OUTPUT] * 4 + 4); 1823 bld->outputs_array = lp_build_array_alloca(gallivm, 1824 bld_base->base.vec_type, array_size, 1825 "output_array"); 1826 } 1827 1828 /* If we have indirect addressing in inputs we need to copy them into 1829 * our alloca array to be able to iterate over them */ 1830 if (bld->indirect_files & (1 << TGSI_FILE_INPUT)) { 1831 unsigned index, chan; 1832 LLVMTypeRef vec_type = bld_base->base.vec_type; 1833 LLVMValueRef array_size = lp_build_const_int32(gallivm, 1834 bld_base->info->file_max[TGSI_FILE_INPUT]*4 + 4); 1835 bld->inputs_array = lp_build_array_alloca(gallivm, 1836 vec_type, array_size, 1837 "input_array"); 1838 1839 assert(bld_base->info->num_inputs 1840 <= bld_base->info->file_max[TGSI_FILE_INPUT] + 1); 1841 1842 for (index = 0; index < bld_base->info->num_inputs; ++index) { 1843 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) { 1844 LLVMValueRef lindex = 1845 lp_build_const_int32(gallivm, index * 4 + chan); 1846 LLVMValueRef input_ptr = 1847 LLVMBuildGEP(gallivm->builder, bld->inputs_array, 1848 &lindex, 1, ""); 1849 LLVMValueRef value = bld->inputs[index][chan]; 1850 if (value) 1851 LLVMBuildStore(gallivm->builder, value, input_ptr); 1852 } 1853 } 1854 } 1855} 1856 1857static void emit_epilogue(struct lp_build_tgsi_context * bld_base) 1858{ 1859 struct lp_build_tgsi_soa_context * bld = lp_soa_context(bld_base); 1860 1861 if (0) { 1862 /* for debugging */ 1863 emit_dump_temps(bld); 1864 } 1865 1866 /* If we have indirect addressing in outputs we need to copy our alloca array 1867 * to the outputs slots specified by the called */ 1868 if (bld->indirect_files & (1 << TGSI_FILE_OUTPUT)) { 1869 unsigned index, chan; 1870 assert(bld_base->info->num_outputs <= 1871 bld_base->info->file_max[TGSI_FILE_OUTPUT] + 1); 1872 for (index = 0; index < bld_base->info->num_outputs; ++index) { 1873 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) { 1874 bld->outputs[index][chan] = lp_get_output_ptr(bld, index, chan); 1875 } 1876 } 1877 } 1878} 1879 1880void 1881lp_build_tgsi_soa(struct gallivm_state *gallivm, 1882 const struct tgsi_token *tokens, 1883 struct lp_type type, 1884 struct lp_build_mask_context *mask, 1885 LLVMValueRef consts_ptr, 1886 LLVMValueRef system_values_array, 1887 const LLVMValueRef *pos, 1888 const LLVMValueRef (*inputs)[TGSI_NUM_CHANNELS], 1889 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS], 1890 struct lp_build_sampler_soa *sampler, 1891 const struct tgsi_shader_info *info) 1892{ 1893 struct lp_build_tgsi_soa_context bld; 1894 1895 struct lp_type res_type; 1896 1897 assert(type.length <= LP_MAX_VECTOR_LENGTH); 1898 memset(&res_type, 0, sizeof res_type); 1899 res_type.width = type.width; 1900 res_type.length = type.length; 1901 res_type.sign = 1; 1902 1903 /* Setup build context */ 1904 memset(&bld, 0, sizeof bld); 1905 lp_build_context_init(&bld.bld_base.base, gallivm, type); 1906 lp_build_context_init(&bld.bld_base.uint_bld, gallivm, lp_uint_type(type)); 1907 lp_build_context_init(&bld.bld_base.int_bld, gallivm, lp_int_type(type)); 1908 lp_build_context_init(&bld.elem_bld, gallivm, lp_elem_type(type)); 1909 bld.mask = mask; 1910 bld.pos = pos; 1911 bld.inputs = inputs; 1912 bld.outputs = outputs; 1913 bld.consts_ptr = consts_ptr; 1914 bld.sampler = sampler; 1915 bld.bld_base.info = info; 1916 bld.indirect_files = info->indirect_files; 1917 1918 bld.bld_base.soa = TRUE; 1919 bld.bld_base.emit_fetch_funcs[TGSI_FILE_CONSTANT] = emit_fetch_constant; 1920 bld.bld_base.emit_fetch_funcs[TGSI_FILE_IMMEDIATE] = emit_fetch_immediate; 1921 bld.bld_base.emit_fetch_funcs[TGSI_FILE_INPUT] = emit_fetch_input; 1922 bld.bld_base.emit_fetch_funcs[TGSI_FILE_TEMPORARY] = emit_fetch_temporary; 1923 bld.bld_base.emit_fetch_funcs[TGSI_FILE_SYSTEM_VALUE] = emit_fetch_system_value; 1924 bld.bld_base.emit_store = emit_store; 1925 1926 bld.bld_base.emit_declaration = lp_emit_declaration_soa; 1927 bld.bld_base.emit_immediate = lp_emit_immediate_soa; 1928 1929 bld.bld_base.emit_prologue = emit_prologue; 1930 bld.bld_base.emit_epilogue = emit_epilogue; 1931 1932 /* Set opcode actions */ 1933 lp_set_default_actions_cpu(&bld.bld_base); 1934 1935 bld.bld_base.op_actions[TGSI_OPCODE_BGNLOOP].emit = bgnloop_emit; 1936 bld.bld_base.op_actions[TGSI_OPCODE_BGNSUB].emit = bgnsub_emit; 1937 bld.bld_base.op_actions[TGSI_OPCODE_BRK].emit = brk_emit; 1938 bld.bld_base.op_actions[TGSI_OPCODE_CAL].emit = cal_emit; 1939 bld.bld_base.op_actions[TGSI_OPCODE_CONT].emit = cont_emit; 1940 bld.bld_base.op_actions[TGSI_OPCODE_DDX].emit = ddx_emit; 1941 bld.bld_base.op_actions[TGSI_OPCODE_DDY].emit = ddy_emit; 1942 bld.bld_base.op_actions[TGSI_OPCODE_ELSE].emit = else_emit; 1943 bld.bld_base.op_actions[TGSI_OPCODE_ENDIF].emit = endif_emit; 1944 bld.bld_base.op_actions[TGSI_OPCODE_ENDLOOP].emit = endloop_emit; 1945 bld.bld_base.op_actions[TGSI_OPCODE_ENDSUB].emit = endsub_emit; 1946 bld.bld_base.op_actions[TGSI_OPCODE_IF].emit = if_emit; 1947 bld.bld_base.op_actions[TGSI_OPCODE_KIL].emit = kil_emit; 1948 bld.bld_base.op_actions[TGSI_OPCODE_KILP].emit = kilp_emit; 1949 bld.bld_base.op_actions[TGSI_OPCODE_NRM].emit = nrm_emit; 1950 bld.bld_base.op_actions[TGSI_OPCODE_NRM4].emit = nrm_emit; 1951 bld.bld_base.op_actions[TGSI_OPCODE_RET].emit = ret_emit; 1952 bld.bld_base.op_actions[TGSI_OPCODE_TEX].emit = tex_emit; 1953 bld.bld_base.op_actions[TGSI_OPCODE_TXB].emit = txb_emit; 1954 bld.bld_base.op_actions[TGSI_OPCODE_TXD].emit = txd_emit; 1955 bld.bld_base.op_actions[TGSI_OPCODE_TXL].emit = txl_emit; 1956 bld.bld_base.op_actions[TGSI_OPCODE_TXP].emit = txp_emit; 1957 1958 lp_exec_mask_init(&bld.exec_mask, &bld.bld_base.base); 1959 1960 1961 bld.system_values_array = system_values_array; 1962 1963 lp_build_tgsi_llvm(&bld.bld_base, tokens); 1964 1965 if (0) { 1966 LLVMBasicBlockRef block = LLVMGetInsertBlock(gallivm->builder); 1967 LLVMValueRef function = LLVMGetBasicBlockParent(block); 1968 debug_printf("11111111111111111111111111111 \n"); 1969 tgsi_dump(tokens, 0); 1970 lp_debug_dump_value(function); 1971 debug_printf("2222222222222222222222222222 \n"); 1972 } 1973 1974 if (0) { 1975 LLVMModuleRef module = LLVMGetGlobalParent( 1976 LLVMGetBasicBlockParent(LLVMGetInsertBlock(gallivm->builder))); 1977 LLVMDumpModule(module); 1978 1979 } 1980} 1981 1982 1983/** 1984 * Build up the system values array out of individual values such as 1985 * the instance ID, front-face, primitive ID, etc. The shader info is 1986 * used to determine which system values are needed and where to put 1987 * them in the system values array. 1988 * 1989 * XXX only instance ID is implemented at this time. 1990 * 1991 * The system values register file is similar to the constants buffer. 1992 * Example declaration: 1993 * DCL SV[0], INSTANCEID 1994 * Example instruction: 1995 * MOVE foo, SV[0].xxxx; 1996 * 1997 * \return LLVM float array (interpreted as float [][4]) 1998 */ 1999LLVMValueRef 2000lp_build_system_values_array(struct gallivm_state *gallivm, 2001 const struct tgsi_shader_info *info, 2002 LLVMValueRef instance_id, 2003 LLVMValueRef facing) 2004{ 2005 LLVMValueRef size = lp_build_const_int32(gallivm, 4 * info->num_system_values); 2006 LLVMTypeRef float_t = LLVMFloatTypeInContext(gallivm->context); 2007 LLVMValueRef array = lp_build_array_alloca(gallivm, float_t, 2008 size, "sysvals_array"); 2009 unsigned i; 2010 2011 for (i = 0; i < info->num_system_values; i++) { 2012 LLVMValueRef index = lp_build_const_int32(gallivm, i * 4); 2013 LLVMValueRef ptr, value = 0; 2014 2015 switch (info->system_value_semantic_name[i]) { 2016 case TGSI_SEMANTIC_INSTANCEID: 2017 /* convert instance ID from int to float */ 2018 value = LLVMBuildSIToFP(gallivm->builder, instance_id, float_t, 2019 "sysval_instanceid"); 2020 break; 2021 case TGSI_SEMANTIC_FACE: 2022 /* fall-through */ 2023 default: 2024 assert(0 && "unexpected semantic in build_system_values_array()"); 2025 } 2026 2027 ptr = LLVMBuildGEP(gallivm->builder, array, &index, 1, ""); 2028 LLVMBuildStore(gallivm->builder, value, ptr); 2029 } 2030 2031 return array; 2032} 2033