draw_llvm.c revision c89b471f8cd3da6c07eb437caabb2e648027f8bb
1/************************************************************************** 2 * 3 * Copyright 2010 VMware, Inc. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28#include "draw_llvm.h" 29 30#include "draw_context.h" 31#include "draw_vs.h" 32 33#include "gallivm/lp_bld_arit.h" 34#include "gallivm/lp_bld_logic.h" 35#include "gallivm/lp_bld_const.h" 36#include "gallivm/lp_bld_swizzle.h" 37#include "gallivm/lp_bld_struct.h" 38#include "gallivm/lp_bld_type.h" 39#include "gallivm/lp_bld_flow.h" 40#include "gallivm/lp_bld_debug.h" 41#include "gallivm/lp_bld_tgsi.h" 42#include "gallivm/lp_bld_printf.h" 43#include "gallivm/lp_bld_intr.h" 44#include "gallivm/lp_bld_init.h" 45#include "gallivm/lp_bld_type.h" 46 47#include "tgsi/tgsi_exec.h" 48#include "tgsi/tgsi_dump.h" 49 50#include "util/u_math.h" 51#include "util/u_pointer.h" 52#include "util/u_string.h" 53#include "util/u_simple_list.h" 54 55 56#define DEBUG_STORE 0 57 58 59/** 60 * This function is called by the gallivm "garbage collector" when 61 * the LLVM global data structures are freed. We must free all LLVM-related 62 * data. Specifically, all JIT'd shader variants. 63 */ 64static void 65draw_llvm_garbage_collect_callback(void *cb_data) 66{ 67 struct draw_llvm *llvm = (struct draw_llvm *) cb_data; 68 struct draw_context *draw = llvm->draw; 69 struct draw_llvm_variant_list_item *li; 70 71 /* Ensure prepare will be run and shaders recompiled */ 72 assert(!draw->suspend_flushing); 73 draw_do_flush(draw, DRAW_FLUSH_STATE_CHANGE); 74 75 /* free all shader variants */ 76 li = first_elem(&llvm->vs_variants_list); 77 while (!at_end(&llvm->vs_variants_list, li)) { 78 struct draw_llvm_variant_list_item *next = next_elem(li); 79 draw_llvm_destroy_variant(li->base); 80 li = next; 81 } 82 83 /* Null-out these pointers so they get remade next time they're needed. 84 * See the accessor functions below. 85 */ 86 llvm->context_ptr_type = NULL; 87 llvm->buffer_ptr_type = NULL; 88 llvm->vb_ptr_type = NULL; 89 llvm->vertex_header_ptr_type = NULL; 90} 91 92 93static void 94draw_llvm_generate(struct draw_llvm *llvm, struct draw_llvm_variant *var, 95 boolean elts); 96 97 98/** 99 * Create LLVM type for struct draw_jit_texture 100 */ 101static LLVMTypeRef 102create_jit_texture_type(struct gallivm_state *gallivm, const char *struct_name) 103{ 104 LLVMTargetDataRef target = gallivm->target; 105 LLVMTypeRef texture_type; 106 LLVMTypeRef elem_types[DRAW_JIT_TEXTURE_NUM_FIELDS]; 107 LLVMTypeRef int32_type = LLVMInt32TypeInContext(gallivm->context); 108 109 elem_types[DRAW_JIT_TEXTURE_WIDTH] = 110 elem_types[DRAW_JIT_TEXTURE_HEIGHT] = 111 elem_types[DRAW_JIT_TEXTURE_DEPTH] = 112 elem_types[DRAW_JIT_TEXTURE_FIRST_LEVEL] = 113 elem_types[DRAW_JIT_TEXTURE_LAST_LEVEL] = int32_type; 114 elem_types[DRAW_JIT_TEXTURE_ROW_STRIDE] = 115 elem_types[DRAW_JIT_TEXTURE_IMG_STRIDE] = 116 LLVMArrayType(int32_type, PIPE_MAX_TEXTURE_LEVELS); 117 elem_types[DRAW_JIT_TEXTURE_DATA] = 118 LLVMArrayType(LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0), 119 PIPE_MAX_TEXTURE_LEVELS); 120 elem_types[DRAW_JIT_TEXTURE_MIN_LOD] = 121 elem_types[DRAW_JIT_TEXTURE_MAX_LOD] = 122 elem_types[DRAW_JIT_TEXTURE_LOD_BIAS] = LLVMFloatTypeInContext(gallivm->context); 123 elem_types[DRAW_JIT_TEXTURE_BORDER_COLOR] = 124 LLVMArrayType(LLVMFloatTypeInContext(gallivm->context), 4); 125 126#if HAVE_LLVM >= 0x0300 127 texture_type = LLVMStructCreateNamed(gallivm->context, struct_name); 128 LLVMStructSetBody(texture_type, elem_types, 129 Elements(elem_types), 0); 130#else 131 texture_type = LLVMStructTypeInContext(gallivm->context, elem_types, 132 Elements(elem_types), 0); 133 134 LLVMAddTypeName(gallivm->module, struct_name, texture_type); 135 136 /* Make sure the target's struct layout cache doesn't return 137 * stale/invalid data. 138 */ 139 LLVMInvalidateStructLayout(gallivm->target, texture_type); 140#endif 141 142 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, width, 143 target, texture_type, 144 DRAW_JIT_TEXTURE_WIDTH); 145 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, height, 146 target, texture_type, 147 DRAW_JIT_TEXTURE_HEIGHT); 148 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, depth, 149 target, texture_type, 150 DRAW_JIT_TEXTURE_DEPTH); 151 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, first_level, 152 target, texture_type, 153 DRAW_JIT_TEXTURE_FIRST_LEVEL); 154 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, last_level, 155 target, texture_type, 156 DRAW_JIT_TEXTURE_LAST_LEVEL); 157 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, row_stride, 158 target, texture_type, 159 DRAW_JIT_TEXTURE_ROW_STRIDE); 160 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, img_stride, 161 target, texture_type, 162 DRAW_JIT_TEXTURE_IMG_STRIDE); 163 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, data, 164 target, texture_type, 165 DRAW_JIT_TEXTURE_DATA); 166 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, min_lod, 167 target, texture_type, 168 DRAW_JIT_TEXTURE_MIN_LOD); 169 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, max_lod, 170 target, texture_type, 171 DRAW_JIT_TEXTURE_MAX_LOD); 172 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, lod_bias, 173 target, texture_type, 174 DRAW_JIT_TEXTURE_LOD_BIAS); 175 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, border_color, 176 target, texture_type, 177 DRAW_JIT_TEXTURE_BORDER_COLOR); 178 179 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture, target, texture_type); 180 181 return texture_type; 182} 183 184 185/** 186 * Create LLVM type for struct draw_jit_texture 187 */ 188static LLVMTypeRef 189create_jit_context_type(struct gallivm_state *gallivm, 190 LLVMTypeRef texture_type, const char *struct_name) 191{ 192 LLVMTargetDataRef target = gallivm->target; 193 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context); 194 LLVMTypeRef elem_types[5]; 195 LLVMTypeRef context_type; 196 197 elem_types[0] = LLVMPointerType(float_type, 0); /* vs_constants */ 198 elem_types[1] = LLVMPointerType(float_type, 0); /* gs_constants */ 199 elem_types[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type, 4), 200 DRAW_TOTAL_CLIP_PLANES), 0); 201 elem_types[3] = LLVMPointerType(float_type, 0); /* viewport */ 202 elem_types[4] = LLVMArrayType(texture_type, 203 PIPE_MAX_VERTEX_SAMPLERS); /* textures */ 204#if HAVE_LLVM >= 0x0300 205 context_type = LLVMStructCreateNamed(gallivm->context, struct_name); 206 LLVMStructSetBody(context_type, elem_types, 207 Elements(elem_types), 0); 208#else 209 context_type = LLVMStructTypeInContext(gallivm->context, elem_types, 210 Elements(elem_types), 0); 211 LLVMAddTypeName(gallivm->module, struct_name, context_type); 212 213 LLVMInvalidateStructLayout(gallivm->target, context_type); 214#endif 215 216 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, vs_constants, 217 target, context_type, 0); 218 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, gs_constants, 219 target, context_type, 1); 220 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, planes, 221 target, context_type, 2); 222 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, textures, 223 target, context_type, 224 DRAW_JIT_CTX_TEXTURES); 225 LP_CHECK_STRUCT_SIZE(struct draw_jit_context, 226 target, context_type); 227 228 return context_type; 229} 230 231 232/** 233 * Create LLVM type for struct pipe_vertex_buffer 234 */ 235static LLVMTypeRef 236create_jit_vertex_buffer_type(struct gallivm_state *gallivm, const char *struct_name) 237{ 238 LLVMTargetDataRef target = gallivm->target; 239 LLVMTypeRef elem_types[3]; 240 LLVMTypeRef vb_type; 241 242 elem_types[0] = 243 elem_types[1] = LLVMInt32TypeInContext(gallivm->context); 244 elem_types[2] = LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0); /* vs_constants */ 245 246#if HAVE_LLVM >= 0x0300 247 vb_type = LLVMStructCreateNamed(gallivm->context, struct_name); 248 LLVMStructSetBody(vb_type, elem_types, 249 Elements(elem_types), 0); 250#else 251 vb_type = LLVMStructTypeInContext(gallivm->context, elem_types, 252 Elements(elem_types), 0); 253 LLVMAddTypeName(gallivm->module, struct_name, vb_type); 254 255 LLVMInvalidateStructLayout(gallivm->target, vb_type); 256#endif 257 258 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, stride, 259 target, vb_type, 0); 260 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, buffer_offset, 261 target, vb_type, 1); 262 263 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer, target, vb_type); 264 265 return vb_type; 266} 267 268 269/** 270 * Create LLVM type for struct vertex_header; 271 */ 272static LLVMTypeRef 273create_jit_vertex_header(struct gallivm_state *gallivm, int data_elems) 274{ 275 LLVMTargetDataRef target = gallivm->target; 276 LLVMTypeRef elem_types[4]; 277 LLVMTypeRef vertex_header; 278 char struct_name[24]; 279 280 util_snprintf(struct_name, 23, "vertex_header%d", data_elems); 281 282 elem_types[DRAW_JIT_VERTEX_VERTEX_ID] = LLVMIntTypeInContext(gallivm->context, 32); 283 elem_types[DRAW_JIT_VERTEX_CLIP] = LLVMArrayType(LLVMFloatTypeInContext(gallivm->context), 4); 284 elem_types[DRAW_JIT_VERTEX_PRE_CLIP_POS] = LLVMArrayType(LLVMFloatTypeInContext(gallivm->context), 4); 285 elem_types[DRAW_JIT_VERTEX_DATA] = LLVMArrayType(elem_types[1], data_elems); 286 287#if HAVE_LLVM >= 0x0300 288 vertex_header = LLVMStructCreateNamed(gallivm->context, struct_name); 289 LLVMStructSetBody(vertex_header, elem_types, 290 Elements(elem_types), 0); 291#else 292 vertex_header = LLVMStructTypeInContext(gallivm->context, elem_types, 293 Elements(elem_types), 0); 294 LLVMAddTypeName(gallivm->module, struct_name, vertex_header); 295 296 LLVMInvalidateStructLayout(gallivm->target, vertex_header); 297#endif 298 299 /* these are bit-fields and we can't take address of them 300 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask, 301 target, vertex_header, 302 DRAW_JIT_VERTEX_CLIPMASK); 303 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag, 304 target, vertex_header, 305 DRAW_JIT_VERTEX_EDGEFLAG); 306 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad, 307 target, vertex_header, 308 DRAW_JIT_VERTEX_PAD); 309 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id, 310 target, vertex_header, 311 DRAW_JIT_VERTEX_VERTEX_ID); 312 */ 313 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clip, 314 target, vertex_header, 315 DRAW_JIT_VERTEX_CLIP); 316 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pre_clip_pos, 317 target, vertex_header, 318 DRAW_JIT_VERTEX_PRE_CLIP_POS); 319 LP_CHECK_MEMBER_OFFSET(struct vertex_header, data, 320 target, vertex_header, 321 DRAW_JIT_VERTEX_DATA); 322 323 return vertex_header; 324} 325 326 327/** 328 * Create LLVM types for various structures. 329 */ 330static void 331create_jit_types(struct draw_llvm *llvm) 332{ 333 struct gallivm_state *gallivm = llvm->gallivm; 334 LLVMTypeRef texture_type, context_type, buffer_type, vb_type; 335 336 texture_type = create_jit_texture_type(gallivm, "texture"); 337 338 context_type = create_jit_context_type(gallivm, texture_type, "draw_jit_context"); 339 llvm->context_ptr_type = LLVMPointerType(context_type, 0); 340 341 buffer_type = LLVMPointerType(LLVMIntTypeInContext(gallivm->context, 8), 0); 342 llvm->buffer_ptr_type = LLVMPointerType(buffer_type, 0); 343 344 vb_type = create_jit_vertex_buffer_type(gallivm, "pipe_vertex_buffer"); 345 llvm->vb_ptr_type = LLVMPointerType(vb_type, 0); 346} 347 348 349static LLVMTypeRef 350get_context_ptr_type(struct draw_llvm *llvm) 351{ 352 if (!llvm->context_ptr_type) 353 create_jit_types(llvm); 354 return llvm->context_ptr_type; 355} 356 357 358static LLVMTypeRef 359get_buffer_ptr_type(struct draw_llvm *llvm) 360{ 361 if (!llvm->buffer_ptr_type) 362 create_jit_types(llvm); 363 return llvm->buffer_ptr_type; 364} 365 366 367static LLVMTypeRef 368get_vb_ptr_type(struct draw_llvm *llvm) 369{ 370 if (!llvm->vb_ptr_type) 371 create_jit_types(llvm); 372 return llvm->vb_ptr_type; 373} 374 375static LLVMTypeRef 376get_vertex_header_ptr_type(struct draw_llvm *llvm) 377{ 378 if (!llvm->vertex_header_ptr_type) 379 create_jit_types(llvm); 380 return llvm->vertex_header_ptr_type; 381} 382 383 384/** 385 * Create per-context LLVM info. 386 */ 387struct draw_llvm * 388draw_llvm_create(struct draw_context *draw, struct gallivm_state *gallivm) 389{ 390 struct draw_llvm *llvm; 391 392 llvm = CALLOC_STRUCT( draw_llvm ); 393 if (!llvm) 394 return NULL; 395 396 lp_build_init(); 397 398 llvm->draw = draw; 399 llvm->gallivm = gallivm; 400 401 if (gallivm_debug & GALLIVM_DEBUG_IR) { 402 LLVMDumpModule(llvm->gallivm->module); 403 } 404 405 llvm->nr_variants = 0; 406 make_empty_list(&llvm->vs_variants_list); 407 408 gallivm_register_garbage_collector_callback( 409 draw_llvm_garbage_collect_callback, llvm); 410 411 return llvm; 412} 413 414 415/** 416 * Free per-context LLVM info. 417 */ 418void 419draw_llvm_destroy(struct draw_llvm *llvm) 420{ 421 gallivm_remove_garbage_collector_callback( 422 draw_llvm_garbage_collect_callback, llvm); 423 424 /* XXX free other draw_llvm data? */ 425 FREE(llvm); 426} 427 428 429/** 430 * Create LLVM-generated code for a vertex shader. 431 */ 432struct draw_llvm_variant * 433draw_llvm_create_variant(struct draw_llvm *llvm, 434 unsigned num_inputs, 435 const struct draw_llvm_variant_key *key) 436{ 437 struct draw_llvm_variant *variant; 438 struct llvm_vertex_shader *shader = 439 llvm_vertex_shader(llvm->draw->vs.vertex_shader); 440 LLVMTypeRef vertex_header; 441 442 variant = MALLOC(sizeof *variant + 443 shader->variant_key_size - 444 sizeof variant->key); 445 if (variant == NULL) 446 return NULL; 447 448 variant->llvm = llvm; 449 450 memcpy(&variant->key, key, shader->variant_key_size); 451 452 vertex_header = create_jit_vertex_header(llvm->gallivm, num_inputs); 453 454 llvm->vertex_header_ptr_type = LLVMPointerType(vertex_header, 0); 455 456 draw_llvm_generate(llvm, variant, FALSE); /* linear */ 457 draw_llvm_generate(llvm, variant, TRUE); /* elts */ 458 459 variant->shader = shader; 460 variant->list_item_global.base = variant; 461 variant->list_item_local.base = variant; 462 /*variant->no = */shader->variants_created++; 463 variant->list_item_global.base = variant; 464 465 return variant; 466} 467 468 469static void 470generate_vs(struct draw_llvm *llvm, 471 LLVMBuilderRef builder, 472 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS], 473 const LLVMValueRef (*inputs)[TGSI_NUM_CHANNELS], 474 LLVMValueRef system_values_array, 475 LLVMValueRef context_ptr, 476 struct lp_build_sampler_soa *draw_sampler, 477 boolean clamp_vertex_color) 478{ 479 const struct tgsi_token *tokens = llvm->draw->vs.vertex_shader->state.tokens; 480 struct lp_type vs_type; 481 LLVMValueRef consts_ptr = draw_jit_context_vs_constants(llvm->gallivm, context_ptr); 482 struct lp_build_sampler_soa *sampler = 0; 483 484 memset(&vs_type, 0, sizeof vs_type); 485 vs_type.floating = TRUE; /* floating point values */ 486 vs_type.sign = TRUE; /* values are signed */ 487 vs_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */ 488 vs_type.width = 32; /* 32-bit float */ 489 vs_type.length = 4; /* 4 elements per vector */ 490#if 0 491 num_vs = 4; /* number of vertices per block */ 492#endif 493 494 if (gallivm_debug & GALLIVM_DEBUG_IR) { 495 tgsi_dump(tokens, 0); 496 } 497 498 if (llvm->draw->num_sampler_views && llvm->draw->num_samplers) 499 sampler = draw_sampler; 500 501 lp_build_tgsi_soa(llvm->gallivm, 502 tokens, 503 vs_type, 504 NULL /*struct lp_build_mask_context *mask*/, 505 consts_ptr, 506 system_values_array, 507 NULL /*pos*/, 508 inputs, 509 outputs, 510 sampler, 511 &llvm->draw->vs.vertex_shader->info); 512 513 { 514 LLVMValueRef out; 515 unsigned chan, attrib; 516 struct lp_build_context bld; 517 struct tgsi_shader_info* info = &llvm->draw->vs.vertex_shader->info; 518 lp_build_context_init(&bld, llvm->gallivm, vs_type); 519 520 for (attrib = 0; attrib < info->num_outputs; ++attrib) { 521 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) { 522 if (outputs[attrib][chan]) { 523 switch (info->output_semantic_name[attrib]) { 524 case TGSI_SEMANTIC_COLOR: 525 case TGSI_SEMANTIC_BCOLOR: 526 if (clamp_vertex_color) { 527 out = LLVMBuildLoad(builder, outputs[attrib][chan], ""); 528 out = lp_build_clamp(&bld, out, bld.zero, bld.one); 529 LLVMBuildStore(builder, out, outputs[attrib][chan]); 530 } 531 break; 532 case TGSI_SEMANTIC_FOG: 533 if (chan == 1 || chan == 2) 534 LLVMBuildStore(builder, bld.zero, outputs[attrib][chan]); 535 else if (chan == 3) 536 LLVMBuildStore(builder, bld.one, outputs[attrib][chan]); 537 break; 538 } 539 } 540 } 541 } 542 } 543} 544 545 546#if DEBUG_STORE 547static void print_vectorf(LLVMBuilderRef builder, 548 LLVMValueRef vec) 549{ 550 LLVMValueRef val[4]; 551 val[0] = LLVMBuildExtractElement(builder, vec, 552 lp_build_const_int32(gallivm, 0), ""); 553 val[1] = LLVMBuildExtractElement(builder, vec, 554 lp_build_const_int32(gallivm, 1), ""); 555 val[2] = LLVMBuildExtractElement(builder, vec, 556 lp_build_const_int32(gallivm, 2), ""); 557 val[3] = LLVMBuildExtractElement(builder, vec, 558 lp_build_const_int32(gallivm, 3), ""); 559 lp_build_printf(builder, "vector = [%f, %f, %f, %f]\n", 560 val[0], val[1], val[2], val[3]); 561} 562#endif 563 564 565static void 566generate_fetch(struct gallivm_state *gallivm, 567 LLVMValueRef vbuffers_ptr, 568 LLVMValueRef *res, 569 struct pipe_vertex_element *velem, 570 LLVMValueRef vbuf, 571 LLVMValueRef index, 572 LLVMValueRef instance_id) 573{ 574 LLVMBuilderRef builder = gallivm->builder; 575 LLVMValueRef indices = 576 LLVMConstInt(LLVMInt64TypeInContext(gallivm->context), 577 velem->vertex_buffer_index, 0); 578 LLVMValueRef vbuffer_ptr = LLVMBuildGEP(builder, vbuffers_ptr, 579 &indices, 1, ""); 580 LLVMValueRef vb_stride = draw_jit_vbuffer_stride(gallivm, vbuf); 581 LLVMValueRef vb_buffer_offset = draw_jit_vbuffer_offset(gallivm, vbuf); 582 LLVMValueRef stride; 583 584 if (velem->instance_divisor) { 585 /* array index = instance_id / instance_divisor */ 586 index = LLVMBuildUDiv(builder, instance_id, 587 lp_build_const_int32(gallivm, velem->instance_divisor), 588 "instance_divisor"); 589 } 590 591 stride = LLVMBuildMul(builder, vb_stride, index, ""); 592 593 vbuffer_ptr = LLVMBuildLoad(builder, vbuffer_ptr, "vbuffer"); 594 595 stride = LLVMBuildAdd(builder, stride, 596 vb_buffer_offset, 597 ""); 598 stride = LLVMBuildAdd(builder, stride, 599 lp_build_const_int32(gallivm, velem->src_offset), 600 ""); 601 602 /*lp_build_printf(builder, "vbuf index = %d, stride is %d\n", indices, stride);*/ 603 vbuffer_ptr = LLVMBuildGEP(builder, vbuffer_ptr, &stride, 1, ""); 604 605 *res = draw_llvm_translate_from(gallivm, vbuffer_ptr, velem->src_format); 606} 607 608 609static LLVMValueRef 610aos_to_soa(struct gallivm_state *gallivm, 611 LLVMValueRef val0, 612 LLVMValueRef val1, 613 LLVMValueRef val2, 614 LLVMValueRef val3, 615 LLVMValueRef channel) 616{ 617 LLVMBuilderRef builder = gallivm->builder; 618 LLVMValueRef ex, res; 619 620 ex = LLVMBuildExtractElement(builder, val0, 621 channel, ""); 622 res = LLVMBuildInsertElement(builder, 623 LLVMConstNull(LLVMTypeOf(val0)), 624 ex, 625 lp_build_const_int32(gallivm, 0), 626 ""); 627 628 ex = LLVMBuildExtractElement(builder, val1, 629 channel, ""); 630 res = LLVMBuildInsertElement(builder, 631 res, ex, 632 lp_build_const_int32(gallivm, 1), 633 ""); 634 635 ex = LLVMBuildExtractElement(builder, val2, 636 channel, ""); 637 res = LLVMBuildInsertElement(builder, 638 res, ex, 639 lp_build_const_int32(gallivm, 2), 640 ""); 641 642 ex = LLVMBuildExtractElement(builder, val3, 643 channel, ""); 644 res = LLVMBuildInsertElement(builder, 645 res, ex, 646 lp_build_const_int32(gallivm, 3), 647 ""); 648 649 return res; 650} 651 652 653static void 654soa_to_aos(struct gallivm_state *gallivm, 655 LLVMValueRef soa[TGSI_NUM_CHANNELS], 656 LLVMValueRef aos[TGSI_NUM_CHANNELS]) 657{ 658 LLVMBuilderRef builder = gallivm->builder; 659 LLVMValueRef comp; 660 int i = 0; 661 662 debug_assert(TGSI_NUM_CHANNELS == 4); 663 664 aos[0] = LLVMConstNull(LLVMTypeOf(soa[0])); 665 aos[1] = aos[2] = aos[3] = aos[0]; 666 667 for (i = 0; i < TGSI_NUM_CHANNELS; ++i) { 668 LLVMValueRef channel = lp_build_const_int32(gallivm, i); 669 670 comp = LLVMBuildExtractElement(builder, soa[i], 671 lp_build_const_int32(gallivm, 0), ""); 672 aos[0] = LLVMBuildInsertElement(builder, aos[0], comp, channel, ""); 673 674 comp = LLVMBuildExtractElement(builder, soa[i], 675 lp_build_const_int32(gallivm, 1), ""); 676 aos[1] = LLVMBuildInsertElement(builder, aos[1], comp, channel, ""); 677 678 comp = LLVMBuildExtractElement(builder, soa[i], 679 lp_build_const_int32(gallivm, 2), ""); 680 aos[2] = LLVMBuildInsertElement(builder, aos[2], comp, channel, ""); 681 682 comp = LLVMBuildExtractElement(builder, soa[i], 683 lp_build_const_int32(gallivm, 3), ""); 684 aos[3] = LLVMBuildInsertElement(builder, aos[3], comp, channel, ""); 685 686 } 687} 688 689 690static void 691convert_to_soa(struct gallivm_state *gallivm, 692 LLVMValueRef (*aos)[TGSI_NUM_CHANNELS], 693 LLVMValueRef (*soa)[TGSI_NUM_CHANNELS], 694 int num_attribs) 695{ 696 int i; 697 698 debug_assert(TGSI_NUM_CHANNELS == 4); 699 700 for (i = 0; i < num_attribs; ++i) { 701 LLVMValueRef val0 = aos[i][0]; 702 LLVMValueRef val1 = aos[i][1]; 703 LLVMValueRef val2 = aos[i][2]; 704 LLVMValueRef val3 = aos[i][3]; 705 706 soa[i][0] = aos_to_soa(gallivm, val0, val1, val2, val3, 707 lp_build_const_int32(gallivm, 0)); 708 soa[i][1] = aos_to_soa(gallivm, val0, val1, val2, val3, 709 lp_build_const_int32(gallivm, 1)); 710 soa[i][2] = aos_to_soa(gallivm, val0, val1, val2, val3, 711 lp_build_const_int32(gallivm, 2)); 712 soa[i][3] = aos_to_soa(gallivm, val0, val1, val2, val3, 713 lp_build_const_int32(gallivm, 3)); 714 } 715} 716 717 718static void 719store_aos(struct gallivm_state *gallivm, 720 LLVMValueRef io_ptr, 721 LLVMValueRef index, 722 LLVMValueRef value, 723 LLVMValueRef clipmask) 724{ 725 LLVMBuilderRef builder = gallivm->builder; 726 LLVMValueRef id_ptr = draw_jit_header_id(gallivm, io_ptr); 727 LLVMValueRef data_ptr = draw_jit_header_data(gallivm, io_ptr); 728 LLVMValueRef indices[3]; 729 LLVMValueRef val; 730 int vertex_id_pad_edgeflag; 731 732 indices[0] = lp_build_const_int32(gallivm, 0); 733 indices[1] = index; 734 indices[2] = lp_build_const_int32(gallivm, 0); 735 736 /* If this assertion fails, it means we need to update the bit twidding 737 * code here. See struct vertex_header in draw_private.h. 738 */ 739 assert(DRAW_TOTAL_CLIP_PLANES==14); 740 /* initialize vertex id:16 = 0xffff, pad:1 = 0, edgeflag:1 = 1 */ 741 vertex_id_pad_edgeflag = (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES); 742 val = lp_build_const_int32(gallivm, vertex_id_pad_edgeflag); 743 /* OR with the clipmask */ 744 val = LLVMBuildOr(builder, val, clipmask, ""); 745 746 /* store vertex header */ 747 LLVMBuildStore(builder, val, id_ptr); 748 749 750#if DEBUG_STORE 751 lp_build_printf(builder, " ---- %p storing attribute %d (io = %p)\n", data_ptr, index, io_ptr); 752#endif 753#if 0 754 /*lp_build_printf(builder, " ---- %p storing at %d (%p) ", io_ptr, index, data_ptr); 755 print_vectorf(builder, value);*/ 756 data_ptr = LLVMBuildBitCast(builder, data_ptr, 757 LLVMPointerType(LLVMArrayType(LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), 4), 0), 0), 758 "datavec"); 759 data_ptr = LLVMBuildGEP(builder, data_ptr, indices, 2, ""); 760 761 LLVMBuildStore(builder, value, data_ptr); 762#else 763 { 764 LLVMValueRef x, y, z, w; 765 LLVMValueRef idx0, idx1, idx2, idx3; 766 LLVMValueRef gep0, gep1, gep2, gep3; 767 data_ptr = LLVMBuildGEP(builder, data_ptr, indices, 3, ""); 768 769 idx0 = lp_build_const_int32(gallivm, 0); 770 idx1 = lp_build_const_int32(gallivm, 1); 771 idx2 = lp_build_const_int32(gallivm, 2); 772 idx3 = lp_build_const_int32(gallivm, 3); 773 774 x = LLVMBuildExtractElement(builder, value, 775 idx0, ""); 776 y = LLVMBuildExtractElement(builder, value, 777 idx1, ""); 778 z = LLVMBuildExtractElement(builder, value, 779 idx2, ""); 780 w = LLVMBuildExtractElement(builder, value, 781 idx3, ""); 782 783 gep0 = LLVMBuildGEP(builder, data_ptr, &idx0, 1, ""); 784 gep1 = LLVMBuildGEP(builder, data_ptr, &idx1, 1, ""); 785 gep2 = LLVMBuildGEP(builder, data_ptr, &idx2, 1, ""); 786 gep3 = LLVMBuildGEP(builder, data_ptr, &idx3, 1, ""); 787 788 /*lp_build_printf(builder, "##### x = %f (%p), y = %f (%p), z = %f (%p), w = %f (%p)\n", 789 x, gep0, y, gep1, z, gep2, w, gep3);*/ 790 LLVMBuildStore(builder, x, gep0); 791 LLVMBuildStore(builder, y, gep1); 792 LLVMBuildStore(builder, z, gep2); 793 LLVMBuildStore(builder, w, gep3); 794 } 795#endif 796} 797 798 799static void 800store_aos_array(struct gallivm_state *gallivm, 801 LLVMValueRef io_ptr, 802 LLVMValueRef aos[TGSI_NUM_CHANNELS], 803 int attrib, 804 int num_outputs, 805 LLVMValueRef clipmask) 806{ 807 LLVMBuilderRef builder = gallivm->builder; 808 LLVMValueRef attr_index = lp_build_const_int32(gallivm, attrib); 809 LLVMValueRef ind0 = lp_build_const_int32(gallivm, 0); 810 LLVMValueRef ind1 = lp_build_const_int32(gallivm, 1); 811 LLVMValueRef ind2 = lp_build_const_int32(gallivm, 2); 812 LLVMValueRef ind3 = lp_build_const_int32(gallivm, 3); 813 LLVMValueRef io0_ptr, io1_ptr, io2_ptr, io3_ptr; 814 LLVMValueRef clipmask0, clipmask1, clipmask2, clipmask3; 815 816 debug_assert(TGSI_NUM_CHANNELS == 4); 817 818 io0_ptr = LLVMBuildGEP(builder, io_ptr, 819 &ind0, 1, ""); 820 io1_ptr = LLVMBuildGEP(builder, io_ptr, 821 &ind1, 1, ""); 822 io2_ptr = LLVMBuildGEP(builder, io_ptr, 823 &ind2, 1, ""); 824 io3_ptr = LLVMBuildGEP(builder, io_ptr, 825 &ind3, 1, ""); 826 827 clipmask0 = LLVMBuildExtractElement(builder, clipmask, 828 ind0, ""); 829 clipmask1 = LLVMBuildExtractElement(builder, clipmask, 830 ind1, ""); 831 clipmask2 = LLVMBuildExtractElement(builder, clipmask, 832 ind2, ""); 833 clipmask3 = LLVMBuildExtractElement(builder, clipmask, 834 ind3, ""); 835 836#if DEBUG_STORE 837 lp_build_printf(builder, "io = %p, indexes[%d, %d, %d, %d]\n, clipmask0 = %x, clipmask1 = %x, clipmask2 = %x, clipmask3 = %x\n", 838 io_ptr, ind0, ind1, ind2, ind3, clipmask0, clipmask1, clipmask2, clipmask3); 839#endif 840 /* store for each of the 4 vertices */ 841 store_aos(gallivm, io0_ptr, attr_index, aos[0], clipmask0); 842 store_aos(gallivm, io1_ptr, attr_index, aos[1], clipmask1); 843 store_aos(gallivm, io2_ptr, attr_index, aos[2], clipmask2); 844 store_aos(gallivm, io3_ptr, attr_index, aos[3], clipmask3); 845} 846 847 848static void 849convert_to_aos(struct gallivm_state *gallivm, 850 LLVMValueRef io, 851 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS], 852 LLVMValueRef clipmask, 853 int num_outputs, 854 int max_vertices) 855{ 856 LLVMBuilderRef builder = gallivm->builder; 857 unsigned chan, attrib; 858 859#if DEBUG_STORE 860 lp_build_printf(builder, " # storing begin\n"); 861#endif 862 for (attrib = 0; attrib < num_outputs; ++attrib) { 863 LLVMValueRef soa[4]; 864 LLVMValueRef aos[4]; 865 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) { 866 if (outputs[attrib][chan]) { 867 LLVMValueRef out = LLVMBuildLoad(builder, outputs[attrib][chan], ""); 868 lp_build_name(out, "output%u.%c", attrib, "xyzw"[chan]); 869 /*lp_build_printf(builder, "output %d : %d ", 870 LLVMConstInt(LLVMInt32Type(), attrib, 0), 871 LLVMConstInt(LLVMInt32Type(), chan, 0)); 872 print_vectorf(builder, out);*/ 873 soa[chan] = out; 874 } 875 else { 876 soa[chan] = 0; 877 } 878 } 879 soa_to_aos(gallivm, soa, aos); 880 store_aos_array(gallivm, 881 io, 882 aos, 883 attrib, 884 num_outputs, 885 clipmask); 886 } 887#if DEBUG_STORE 888 lp_build_printf(builder, " # storing end\n"); 889#endif 890} 891 892 893/** 894 * Stores original vertex positions in clip coordinates 895 * There is probably a more efficient way to do this, 4 floats at once 896 * rather than extracting each element one by one. 897 */ 898static void 899store_clip(struct gallivm_state *gallivm, 900 LLVMValueRef io_ptr, 901 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS], 902 boolean pre_clip_pos) 903{ 904 LLVMBuilderRef builder = gallivm->builder; 905 LLVMValueRef out[4]; 906 LLVMValueRef indices[2]; 907 LLVMValueRef io0_ptr, io1_ptr, io2_ptr, io3_ptr; 908 LLVMValueRef clip_ptr0, clip_ptr1, clip_ptr2, clip_ptr3; 909 LLVMValueRef clip0_ptr, clip1_ptr, clip2_ptr, clip3_ptr; 910 LLVMValueRef out0elem, out1elem, out2elem, out3elem; 911 int i; 912 913 LLVMValueRef ind0 = lp_build_const_int32(gallivm, 0); 914 LLVMValueRef ind1 = lp_build_const_int32(gallivm, 1); 915 LLVMValueRef ind2 = lp_build_const_int32(gallivm, 2); 916 LLVMValueRef ind3 = lp_build_const_int32(gallivm, 3); 917 918 indices[0] = 919 indices[1] = lp_build_const_int32(gallivm, 0); 920 921 out[0] = LLVMBuildLoad(builder, outputs[0][0], ""); /*x0 x1 x2 x3*/ 922 out[1] = LLVMBuildLoad(builder, outputs[0][1], ""); /*y0 y1 y2 y3*/ 923 out[2] = LLVMBuildLoad(builder, outputs[0][2], ""); /*z0 z1 z2 z3*/ 924 out[3] = LLVMBuildLoad(builder, outputs[0][3], ""); /*w0 w1 w2 w3*/ 925 926 io0_ptr = LLVMBuildGEP(builder, io_ptr, &ind0, 1, ""); 927 io1_ptr = LLVMBuildGEP(builder, io_ptr, &ind1, 1, ""); 928 io2_ptr = LLVMBuildGEP(builder, io_ptr, &ind2, 1, ""); 929 io3_ptr = LLVMBuildGEP(builder, io_ptr, &ind3, 1, ""); 930 931 /* FIXME: this needs updating for clip vertex support */ 932 if (!pre_clip_pos) { 933 clip_ptr0 = draw_jit_header_clip(gallivm, io0_ptr); 934 clip_ptr1 = draw_jit_header_clip(gallivm, io1_ptr); 935 clip_ptr2 = draw_jit_header_clip(gallivm, io2_ptr); 936 clip_ptr3 = draw_jit_header_clip(gallivm, io3_ptr); 937 } else { 938 clip_ptr0 = draw_jit_header_pre_clip_pos(gallivm, io0_ptr); 939 clip_ptr1 = draw_jit_header_pre_clip_pos(gallivm, io1_ptr); 940 clip_ptr2 = draw_jit_header_pre_clip_pos(gallivm, io2_ptr); 941 clip_ptr3 = draw_jit_header_pre_clip_pos(gallivm, io3_ptr); 942 } 943 944 for (i = 0; i<4; i++) { 945 clip0_ptr = LLVMBuildGEP(builder, clip_ptr0, indices, 2, ""); /* x0 */ 946 clip1_ptr = LLVMBuildGEP(builder, clip_ptr1, indices, 2, ""); /* x1 */ 947 clip2_ptr = LLVMBuildGEP(builder, clip_ptr2, indices, 2, ""); /* x2 */ 948 clip3_ptr = LLVMBuildGEP(builder, clip_ptr3, indices, 2, ""); /* x3 */ 949 950 out0elem = LLVMBuildExtractElement(builder, out[i], ind0, ""); /* x0 */ 951 out1elem = LLVMBuildExtractElement(builder, out[i], ind1, ""); /* x1 */ 952 out2elem = LLVMBuildExtractElement(builder, out[i], ind2, ""); /* x2 */ 953 out3elem = LLVMBuildExtractElement(builder, out[i], ind3, ""); /* x3 */ 954 955 LLVMBuildStore(builder, out0elem, clip0_ptr); 956 LLVMBuildStore(builder, out1elem, clip1_ptr); 957 LLVMBuildStore(builder, out2elem, clip2_ptr); 958 LLVMBuildStore(builder, out3elem, clip3_ptr); 959 960 indices[1]= LLVMBuildAdd(builder, indices[1], ind1, ""); 961 } 962 963} 964 965 966/** 967 * Equivalent of _mm_set1_ps(a) 968 */ 969static LLVMValueRef 970vec4f_from_scalar(struct gallivm_state *gallivm, 971 LLVMValueRef a, 972 const char *name) 973{ 974 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context); 975 LLVMValueRef res = LLVMGetUndef(LLVMVectorType(float_type, 4)); 976 int i; 977 978 for (i = 0; i < 4; ++i) { 979 LLVMValueRef index = lp_build_const_int32(gallivm, i); 980 res = LLVMBuildInsertElement(gallivm->builder, res, a, 981 index, i == 3 ? name : ""); 982 } 983 984 return res; 985} 986 987 988/** 989 * Transforms the outputs for viewport mapping 990 */ 991static void 992generate_viewport(struct draw_llvm *llvm, 993 LLVMBuilderRef builder, 994 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS], 995 LLVMValueRef context_ptr) 996{ 997 int i; 998 struct gallivm_state *gallivm = llvm->gallivm; 999 struct lp_type f32_type = lp_type_float_vec(32); 1000 LLVMValueRef out3 = LLVMBuildLoad(builder, outputs[0][3], ""); /*w0 w1 w2 w3*/ 1001 LLVMValueRef const1 = lp_build_const_vec(gallivm, f32_type, 1.0); /*1.0 1.0 1.0 1.0*/ 1002 LLVMValueRef vp_ptr = draw_jit_context_viewport(gallivm, context_ptr); 1003 1004 /* for 1/w convention*/ 1005 out3 = LLVMBuildFDiv(builder, const1, out3, ""); 1006 LLVMBuildStore(builder, out3, outputs[0][3]); 1007 1008 /* Viewport Mapping */ 1009 for (i=0; i<3; i++) { 1010 LLVMValueRef out = LLVMBuildLoad(builder, outputs[0][i], ""); /*x0 x1 x2 x3*/ 1011 LLVMValueRef scale; 1012 LLVMValueRef trans; 1013 LLVMValueRef scale_i; 1014 LLVMValueRef trans_i; 1015 LLVMValueRef index; 1016 1017 index = lp_build_const_int32(gallivm, i); 1018 scale_i = LLVMBuildGEP(builder, vp_ptr, &index, 1, ""); 1019 1020 index = lp_build_const_int32(gallivm, i+4); 1021 trans_i = LLVMBuildGEP(builder, vp_ptr, &index, 1, ""); 1022 1023 scale = vec4f_from_scalar(gallivm, LLVMBuildLoad(builder, scale_i, ""), "scale"); 1024 trans = vec4f_from_scalar(gallivm, LLVMBuildLoad(builder, trans_i, ""), "trans"); 1025 1026 /* divide by w */ 1027 out = LLVMBuildFMul(builder, out, out3, ""); 1028 /* mult by scale */ 1029 out = LLVMBuildFMul(builder, out, scale, ""); 1030 /* add translation */ 1031 out = LLVMBuildFAdd(builder, out, trans, ""); 1032 1033 /* store transformed outputs */ 1034 LLVMBuildStore(builder, out, outputs[0][i]); 1035 } 1036 1037} 1038 1039 1040/** 1041 * Returns clipmask as 4xi32 bitmask for the 4 vertices 1042 */ 1043static LLVMValueRef 1044generate_clipmask(struct gallivm_state *gallivm, 1045 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS], 1046 boolean clip_xy, 1047 boolean clip_z, 1048 boolean clip_user, 1049 boolean clip_halfz, 1050 unsigned ucp_enable, 1051 LLVMValueRef context_ptr) 1052{ 1053 LLVMBuilderRef builder = gallivm->builder; 1054 LLVMValueRef mask; /* stores the <4xi32> clipmasks */ 1055 LLVMValueRef test, temp; 1056 LLVMValueRef zero, shift; 1057 LLVMValueRef pos_x, pos_y, pos_z, pos_w; 1058 LLVMValueRef plane1, planes, plane_ptr, sum; 1059 struct lp_type f32_type = lp_type_float_vec(32); 1060 1061 mask = lp_build_const_int_vec(gallivm, lp_type_int_vec(32), 0); 1062 temp = lp_build_const_int_vec(gallivm, lp_type_int_vec(32), 0); 1063 zero = lp_build_const_vec(gallivm, f32_type, 0); /* 0.0f 0.0f 0.0f 0.0f */ 1064 shift = lp_build_const_int_vec(gallivm, lp_type_int_vec(32), 1); /* 1 1 1 1 */ 1065 1066 /* Assuming position stored at output[0] */ 1067 pos_x = LLVMBuildLoad(builder, outputs[0][0], ""); /*x0 x1 x2 x3*/ 1068 pos_y = LLVMBuildLoad(builder, outputs[0][1], ""); /*y0 y1 y2 y3*/ 1069 pos_z = LLVMBuildLoad(builder, outputs[0][2], ""); /*z0 z1 z2 z3*/ 1070 pos_w = LLVMBuildLoad(builder, outputs[0][3], ""); /*w0 w1 w2 w3*/ 1071 1072 /* Cliptest, for hardwired planes */ 1073 if (clip_xy) { 1074 /* plane 1 */ 1075 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_x , pos_w); 1076 temp = shift; 1077 test = LLVMBuildAnd(builder, test, temp, ""); 1078 mask = test; 1079 1080 /* plane 2 */ 1081 test = LLVMBuildFAdd(builder, pos_x, pos_w, ""); 1082 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test); 1083 temp = LLVMBuildShl(builder, temp, shift, ""); 1084 test = LLVMBuildAnd(builder, test, temp, ""); 1085 mask = LLVMBuildOr(builder, mask, test, ""); 1086 1087 /* plane 3 */ 1088 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_y, pos_w); 1089 temp = LLVMBuildShl(builder, temp, shift, ""); 1090 test = LLVMBuildAnd(builder, test, temp, ""); 1091 mask = LLVMBuildOr(builder, mask, test, ""); 1092 1093 /* plane 4 */ 1094 test = LLVMBuildFAdd(builder, pos_y, pos_w, ""); 1095 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test); 1096 temp = LLVMBuildShl(builder, temp, shift, ""); 1097 test = LLVMBuildAnd(builder, test, temp, ""); 1098 mask = LLVMBuildOr(builder, mask, test, ""); 1099 } 1100 1101 if (clip_z) { 1102 temp = lp_build_const_int_vec(gallivm, lp_type_int_vec(32), 16); 1103 if (clip_halfz) { 1104 /* plane 5 */ 1105 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, pos_z); 1106 test = LLVMBuildAnd(builder, test, temp, ""); 1107 mask = LLVMBuildOr(builder, mask, test, ""); 1108 } 1109 else { 1110 /* plane 5 */ 1111 test = LLVMBuildFAdd(builder, pos_z, pos_w, ""); 1112 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test); 1113 test = LLVMBuildAnd(builder, test, temp, ""); 1114 mask = LLVMBuildOr(builder, mask, test, ""); 1115 } 1116 /* plane 6 */ 1117 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_z, pos_w); 1118 temp = LLVMBuildShl(builder, temp, shift, ""); 1119 test = LLVMBuildAnd(builder, test, temp, ""); 1120 mask = LLVMBuildOr(builder, mask, test, ""); 1121 } 1122 1123 if (clip_user) { 1124 LLVMValueRef planes_ptr = draw_jit_context_planes(gallivm, context_ptr); 1125 LLVMValueRef indices[3]; 1126 1127 /* userclip planes */ 1128 while (ucp_enable) { 1129 unsigned plane_idx = ffs(ucp_enable)-1; 1130 ucp_enable &= ~(1 << plane_idx); 1131 plane_idx += 6; 1132 1133 indices[0] = lp_build_const_int32(gallivm, 0); 1134 indices[1] = lp_build_const_int32(gallivm, plane_idx); 1135 1136 indices[2] = lp_build_const_int32(gallivm, 0); 1137 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, ""); 1138 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_x"); 1139 planes = vec4f_from_scalar(gallivm, plane1, "plane4_x"); 1140 sum = LLVMBuildFMul(builder, planes, pos_x, ""); 1141 1142 indices[2] = lp_build_const_int32(gallivm, 1); 1143 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, ""); 1144 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_y"); 1145 planes = vec4f_from_scalar(gallivm, plane1, "plane4_y"); 1146 test = LLVMBuildFMul(builder, planes, pos_y, ""); 1147 sum = LLVMBuildFAdd(builder, sum, test, ""); 1148 1149 indices[2] = lp_build_const_int32(gallivm, 2); 1150 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, ""); 1151 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_z"); 1152 planes = vec4f_from_scalar(gallivm, plane1, "plane4_z"); 1153 test = LLVMBuildFMul(builder, planes, pos_z, ""); 1154 sum = LLVMBuildFAdd(builder, sum, test, ""); 1155 1156 indices[2] = lp_build_const_int32(gallivm, 3); 1157 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, ""); 1158 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_w"); 1159 planes = vec4f_from_scalar(gallivm, plane1, "plane4_w"); 1160 test = LLVMBuildFMul(builder, planes, pos_w, ""); 1161 sum = LLVMBuildFAdd(builder, sum, test, ""); 1162 1163 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, sum); 1164 temp = lp_build_const_int_vec(gallivm, lp_type_int_vec(32), 1 << plane_idx); 1165 test = LLVMBuildAnd(builder, test, temp, ""); 1166 mask = LLVMBuildOr(builder, mask, test, ""); 1167 } 1168 } 1169 return mask; 1170} 1171 1172 1173/** 1174 * Returns boolean if any clipping has occurred 1175 * Used zero/non-zero i32 value to represent boolean 1176 */ 1177static void 1178clipmask_bool(struct gallivm_state *gallivm, 1179 LLVMValueRef clipmask, 1180 LLVMValueRef ret_ptr) 1181{ 1182 LLVMBuilderRef builder = gallivm->builder; 1183 LLVMValueRef ret = LLVMBuildLoad(builder, ret_ptr, ""); 1184 LLVMValueRef temp; 1185 int i; 1186 1187 for (i=0; i<4; i++) { 1188 temp = LLVMBuildExtractElement(builder, clipmask, 1189 lp_build_const_int32(gallivm, i) , ""); 1190 ret = LLVMBuildOr(builder, ret, temp, ""); 1191 } 1192 1193 LLVMBuildStore(builder, ret, ret_ptr); 1194} 1195 1196 1197static void 1198draw_llvm_generate(struct draw_llvm *llvm, struct draw_llvm_variant *variant, 1199 boolean elts) 1200{ 1201 struct gallivm_state *gallivm = llvm->gallivm; 1202 LLVMContextRef context = gallivm->context; 1203 LLVMTypeRef int32_type = LLVMInt32TypeInContext(context); 1204 LLVMTypeRef arg_types[8]; 1205 LLVMTypeRef func_type; 1206 LLVMValueRef context_ptr; 1207 LLVMBasicBlockRef block; 1208 LLVMBuilderRef builder; 1209 LLVMValueRef end, start; 1210 LLVMValueRef count, fetch_elts, fetch_count; 1211 LLVMValueRef stride, step, io_itr; 1212 LLVMValueRef io_ptr, vbuffers_ptr, vb_ptr; 1213 LLVMValueRef instance_id; 1214 LLVMValueRef system_values_array; 1215 LLVMValueRef zero = lp_build_const_int32(gallivm, 0); 1216 LLVMValueRef one = lp_build_const_int32(gallivm, 1); 1217 struct draw_context *draw = llvm->draw; 1218 const struct tgsi_shader_info *vs_info = &draw->vs.vertex_shader->info; 1219 unsigned i, j; 1220 struct lp_build_context bld; 1221 struct lp_build_loop_state lp_loop; 1222 const int max_vertices = 4; 1223 LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS]; 1224 LLVMValueRef fetch_max; 1225 void *code; 1226 struct lp_build_sampler_soa *sampler = 0; 1227 LLVMValueRef ret, ret_ptr; 1228 const boolean bypass_viewport = variant->key.bypass_viewport; 1229 const boolean enable_cliptest = variant->key.clip_xy || 1230 variant->key.clip_z || 1231 variant->key.clip_user; 1232 LLVMValueRef variant_func; 1233 1234 arg_types[0] = get_context_ptr_type(llvm); /* context */ 1235 arg_types[1] = get_vertex_header_ptr_type(llvm); /* vertex_header */ 1236 arg_types[2] = get_buffer_ptr_type(llvm); /* vbuffers */ 1237 if (elts) 1238 arg_types[3] = LLVMPointerType(int32_type, 0);/* fetch_elts * */ 1239 else 1240 arg_types[3] = int32_type; /* start */ 1241 arg_types[4] = int32_type; /* fetch_count / count */ 1242 arg_types[5] = int32_type; /* stride */ 1243 arg_types[6] = get_vb_ptr_type(llvm); /* pipe_vertex_buffer's */ 1244 arg_types[7] = int32_type; /* instance_id */ 1245 1246 func_type = LLVMFunctionType(int32_type, arg_types, Elements(arg_types), 0); 1247 1248 variant_func = LLVMAddFunction(gallivm->module, 1249 elts ? "draw_llvm_shader_elts" : "draw_llvm_shader", 1250 func_type); 1251 1252 if (elts) 1253 variant->function_elts = variant_func; 1254 else 1255 variant->function = variant_func; 1256 1257 LLVMSetFunctionCallConv(variant_func, LLVMCCallConv); 1258 for (i = 0; i < Elements(arg_types); ++i) 1259 if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind) 1260 LLVMAddAttribute(LLVMGetParam(variant_func, i), 1261 LLVMNoAliasAttribute); 1262 1263 context_ptr = LLVMGetParam(variant_func, 0); 1264 io_ptr = LLVMGetParam(variant_func, 1); 1265 vbuffers_ptr = LLVMGetParam(variant_func, 2); 1266 stride = LLVMGetParam(variant_func, 5); 1267 vb_ptr = LLVMGetParam(variant_func, 6); 1268 instance_id = LLVMGetParam(variant_func, 7); 1269 1270 lp_build_name(context_ptr, "context"); 1271 lp_build_name(io_ptr, "io"); 1272 lp_build_name(vbuffers_ptr, "vbuffers"); 1273 lp_build_name(stride, "stride"); 1274 lp_build_name(vb_ptr, "vb"); 1275 lp_build_name(instance_id, "instance_id"); 1276 1277 if (elts) { 1278 fetch_elts = LLVMGetParam(variant_func, 3); 1279 fetch_count = LLVMGetParam(variant_func, 4); 1280 lp_build_name(fetch_elts, "fetch_elts"); 1281 lp_build_name(fetch_count, "fetch_count"); 1282 start = count = NULL; 1283 } 1284 else { 1285 start = LLVMGetParam(variant_func, 3); 1286 count = LLVMGetParam(variant_func, 4); 1287 lp_build_name(start, "start"); 1288 lp_build_name(count, "count"); 1289 fetch_elts = fetch_count = NULL; 1290 } 1291 1292 /* 1293 * Function body 1294 */ 1295 1296 block = LLVMAppendBasicBlockInContext(gallivm->context, variant_func, "entry"); 1297 builder = gallivm->builder; 1298 LLVMPositionBuilderAtEnd(builder, block); 1299 1300 lp_build_context_init(&bld, gallivm, lp_type_int(32)); 1301 1302 system_values_array = lp_build_system_values_array(gallivm, vs_info, 1303 instance_id, NULL); 1304 1305 /* function will return non-zero i32 value if any clipped vertices */ 1306 ret_ptr = lp_build_alloca(gallivm, int32_type, ""); 1307 LLVMBuildStore(builder, zero, ret_ptr); 1308 1309 /* code generated texture sampling */ 1310 sampler = draw_llvm_sampler_soa_create( 1311 draw_llvm_variant_key_samplers(&variant->key), 1312 context_ptr); 1313 1314 if (elts) { 1315 start = zero; 1316 end = fetch_count; 1317 } 1318 else { 1319 end = lp_build_add(&bld, start, count); 1320 } 1321 1322 step = lp_build_const_int32(gallivm, max_vertices); 1323 1324 fetch_max = LLVMBuildSub(builder, end, one, "fetch_max"); 1325 1326 lp_build_loop_begin(&lp_loop, gallivm, start); 1327 { 1328 LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS]; 1329 LLVMValueRef aos_attribs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS] = { { 0 } }; 1330 LLVMValueRef io; 1331 LLVMValueRef clipmask; /* holds the clipmask value */ 1332 const LLVMValueRef (*ptr_aos)[TGSI_NUM_CHANNELS]; 1333 1334 if (elts) 1335 io_itr = lp_loop.counter; 1336 else 1337 io_itr = LLVMBuildSub(builder, lp_loop.counter, start, ""); 1338 1339 io = LLVMBuildGEP(builder, io_ptr, &io_itr, 1, ""); 1340#if DEBUG_STORE 1341 lp_build_printf(builder, " --- io %d = %p, loop counter %d\n", 1342 io_itr, io, lp_loop.counter); 1343#endif 1344 for (i = 0; i < TGSI_NUM_CHANNELS; ++i) { 1345 LLVMValueRef true_index = 1346 LLVMBuildAdd(builder, 1347 lp_loop.counter, 1348 lp_build_const_int32(gallivm, i), ""); 1349 1350 /* make sure we're not out of bounds which can happen 1351 * if fetch_count % 4 != 0, because on the last iteration 1352 * a few of the 4 vertex fetches will be out of bounds */ 1353 true_index = lp_build_min(&bld, true_index, fetch_max); 1354 1355 if (elts) { 1356 LLVMValueRef fetch_ptr; 1357 fetch_ptr = LLVMBuildGEP(builder, fetch_elts, 1358 &true_index, 1, ""); 1359 true_index = LLVMBuildLoad(builder, fetch_ptr, "fetch_elt"); 1360 } 1361 1362 for (j = 0; j < draw->pt.nr_vertex_elements; ++j) { 1363 struct pipe_vertex_element *velem = &draw->pt.vertex_element[j]; 1364 LLVMValueRef vb_index = 1365 lp_build_const_int32(gallivm, velem->vertex_buffer_index); 1366 LLVMValueRef vb = LLVMBuildGEP(builder, vb_ptr, &vb_index, 1, ""); 1367 generate_fetch(gallivm, vbuffers_ptr, 1368 &aos_attribs[j][i], velem, vb, true_index, 1369 instance_id); 1370 } 1371 } 1372 convert_to_soa(gallivm, aos_attribs, inputs, 1373 draw->pt.nr_vertex_elements); 1374 1375 ptr_aos = (const LLVMValueRef (*)[TGSI_NUM_CHANNELS]) inputs; 1376 generate_vs(llvm, 1377 builder, 1378 outputs, 1379 ptr_aos, 1380 system_values_array, 1381 context_ptr, 1382 sampler, 1383 variant->key.clamp_vertex_color); 1384 1385 /* store original positions in clip before further manipulation */ 1386 store_clip(gallivm, io, outputs, 0); 1387 store_clip(gallivm, io, outputs, 1); 1388 1389 /* do cliptest */ 1390 if (enable_cliptest) { 1391 /* allocate clipmask, assign it integer type */ 1392 clipmask = generate_clipmask(gallivm, outputs, 1393 variant->key.clip_xy, 1394 variant->key.clip_z, 1395 variant->key.clip_user, 1396 variant->key.clip_halfz, 1397 variant->key.ucp_enable, 1398 context_ptr); 1399 /* return clipping boolean value for function */ 1400 clipmask_bool(gallivm, clipmask, ret_ptr); 1401 } 1402 else { 1403 clipmask = lp_build_const_int_vec(gallivm, lp_type_int_vec(32), 0); 1404 } 1405 1406 /* do viewport mapping */ 1407 if (!bypass_viewport) { 1408 generate_viewport(llvm, builder, outputs, context_ptr); 1409 } 1410 1411 /* store clipmask in vertex header, 1412 * original positions in clip 1413 * and transformed positions in data 1414 */ 1415 convert_to_aos(gallivm, io, outputs, clipmask, 1416 vs_info->num_outputs, max_vertices); 1417 } 1418 1419 lp_build_loop_end_cond(&lp_loop, end, step, LLVMIntUGE); 1420 1421 sampler->destroy(sampler); 1422 1423 ret = LLVMBuildLoad(builder, ret_ptr, ""); 1424 LLVMBuildRet(builder, ret); 1425 1426 /* 1427 * Translate the LLVM IR into machine code. 1428 */ 1429#ifdef DEBUG 1430 if (LLVMVerifyFunction(variant_func, LLVMPrintMessageAction)) { 1431 lp_debug_dump_value(variant_func); 1432 assert(0); 1433 } 1434#endif 1435 1436 LLVMRunFunctionPassManager(gallivm->passmgr, variant_func); 1437 1438 if (gallivm_debug & GALLIVM_DEBUG_IR) { 1439 lp_debug_dump_value(variant_func); 1440 debug_printf("\n"); 1441 } 1442 1443 code = LLVMGetPointerToGlobal(gallivm->engine, variant_func); 1444 if (elts) 1445 variant->jit_func_elts = (draw_jit_vert_func_elts) pointer_to_func(code); 1446 else 1447 variant->jit_func = (draw_jit_vert_func) pointer_to_func(code); 1448 1449 if (gallivm_debug & GALLIVM_DEBUG_ASM) { 1450 lp_disassemble(code); 1451 } 1452 lp_func_delete_body(variant_func); 1453} 1454 1455 1456struct draw_llvm_variant_key * 1457draw_llvm_make_variant_key(struct draw_llvm *llvm, char *store) 1458{ 1459 unsigned i; 1460 struct draw_llvm_variant_key *key; 1461 struct lp_sampler_static_state *sampler; 1462 1463 key = (struct draw_llvm_variant_key *)store; 1464 1465 key->clamp_vertex_color = llvm->draw->rasterizer->clamp_vertex_color; /**/ 1466 1467 /* Presumably all variants of the shader should have the same 1468 * number of vertex elements - ie the number of shader inputs. 1469 */ 1470 key->nr_vertex_elements = llvm->draw->pt.nr_vertex_elements; 1471 1472 /* will have to rig this up properly later */ 1473 key->clip_xy = llvm->draw->clip_xy; 1474 key->clip_z = llvm->draw->clip_z; 1475 key->clip_user = llvm->draw->clip_user; 1476 key->bypass_viewport = llvm->draw->identity_viewport; 1477 key->clip_halfz = !llvm->draw->rasterizer->gl_rasterization_rules; 1478 key->need_edgeflags = (llvm->draw->vs.edgeflag_output ? TRUE : FALSE); 1479 key->ucp_enable = llvm->draw->rasterizer->clip_plane_enable; 1480 key->pad = 0; 1481 1482 /* All variants of this shader will have the same value for 1483 * nr_samplers. Not yet trying to compact away holes in the 1484 * sampler array. 1485 */ 1486 key->nr_samplers = llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_SAMPLER] + 1; 1487 1488 sampler = draw_llvm_variant_key_samplers(key); 1489 1490 memcpy(key->vertex_element, 1491 llvm->draw->pt.vertex_element, 1492 sizeof(struct pipe_vertex_element) * key->nr_vertex_elements); 1493 1494 memset(sampler, 0, key->nr_samplers * sizeof *sampler); 1495 1496 for (i = 0 ; i < key->nr_samplers; i++) { 1497 lp_sampler_static_state(&sampler[i], 1498 llvm->draw->sampler_views[i], 1499 llvm->draw->samplers[i]); 1500 } 1501 1502 return key; 1503} 1504 1505 1506void 1507draw_llvm_set_mapped_texture(struct draw_context *draw, 1508 unsigned sampler_idx, 1509 uint32_t width, uint32_t height, uint32_t depth, 1510 uint32_t first_level, uint32_t last_level, 1511 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS], 1512 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS], 1513 const void *data[PIPE_MAX_TEXTURE_LEVELS]) 1514{ 1515 unsigned j; 1516 struct draw_jit_texture *jit_tex; 1517 1518 assert(sampler_idx < PIPE_MAX_VERTEX_SAMPLERS); 1519 1520 jit_tex = &draw->llvm->jit_context.textures[sampler_idx]; 1521 1522 jit_tex->width = width; 1523 jit_tex->height = height; 1524 jit_tex->depth = depth; 1525 jit_tex->first_level = first_level; 1526 jit_tex->last_level = last_level; 1527 1528 for (j = first_level; j <= last_level; j++) { 1529 jit_tex->data[j] = data[j]; 1530 jit_tex->row_stride[j] = row_stride[j]; 1531 jit_tex->img_stride[j] = img_stride[j]; 1532 } 1533} 1534 1535 1536void 1537draw_llvm_set_sampler_state(struct draw_context *draw) 1538{ 1539 unsigned i; 1540 1541 for (i = 0; i < draw->num_samplers; i++) { 1542 struct draw_jit_texture *jit_tex = &draw->llvm->jit_context.textures[i]; 1543 1544 if (draw->samplers[i]) { 1545 jit_tex->min_lod = draw->samplers[i]->min_lod; 1546 jit_tex->max_lod = draw->samplers[i]->max_lod; 1547 jit_tex->lod_bias = draw->samplers[i]->lod_bias; 1548 COPY_4V(jit_tex->border_color, draw->samplers[i]->border_color.f); 1549 } 1550 } 1551} 1552 1553 1554void 1555draw_llvm_destroy_variant(struct draw_llvm_variant *variant) 1556{ 1557 struct draw_llvm *llvm = variant->llvm; 1558 1559 if (variant->function_elts) { 1560 LLVMFreeMachineCodeForFunction(llvm->gallivm->engine, 1561 variant->function_elts); 1562 LLVMDeleteFunction(variant->function_elts); 1563 } 1564 1565 if (variant->function) { 1566 LLVMFreeMachineCodeForFunction(llvm->gallivm->engine, 1567 variant->function); 1568 LLVMDeleteFunction(variant->function); 1569 } 1570 1571 remove_from_list(&variant->list_item_local); 1572 variant->shader->variants_cached--; 1573 remove_from_list(&variant->list_item_global); 1574 llvm->nr_variants--; 1575 FREE(variant); 1576} 1577