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