lp_state_fs.c revision efc82aef35a2aac5d2ed9774f6d28f2626796416
1/************************************************************************** 2 * 3 * Copyright 2009 VMware, Inc. 4 * Copyright 2007 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 * Code generate the whole fragment pipeline. 32 * 33 * The fragment pipeline consists of the following stages: 34 * - early depth test 35 * - fragment shader 36 * - alpha test 37 * - depth/stencil test 38 * - blending 39 * 40 * This file has only the glue to assemble the fragment pipeline. The actual 41 * plumbing of converting Gallium state into LLVM IR is done elsewhere, in the 42 * lp_bld_*.[ch] files, and in a complete generic and reusable way. Here we 43 * muster the LLVM JIT execution engine to create a function that follows an 44 * established binary interface and that can be called from C directly. 45 * 46 * A big source of complexity here is that we often want to run different 47 * stages with different precisions and data types and precisions. For example, 48 * the fragment shader needs typically to be done in floats, but the 49 * depth/stencil test and blending is better done in the type that most closely 50 * matches the depth/stencil and color buffer respectively. 51 * 52 * Since the width of a SIMD vector register stays the same regardless of the 53 * element type, different types imply different number of elements, so we must 54 * code generate more instances of the stages with larger types to be able to 55 * feed/consume the stages with smaller types. 56 * 57 * @author Jose Fonseca <jfonseca@vmware.com> 58 */ 59 60#include <limits.h> 61#include "pipe/p_defines.h" 62#include "util/u_inlines.h" 63#include "util/u_memory.h" 64#include "util/u_pointer.h" 65#include "util/u_format.h" 66#include "util/u_dump.h" 67#include "util/u_string.h" 68#include "util/u_simple_list.h" 69#include "os/os_time.h" 70#include "pipe/p_shader_tokens.h" 71#include "draw/draw_context.h" 72#include "tgsi/tgsi_dump.h" 73#include "tgsi/tgsi_scan.h" 74#include "tgsi/tgsi_parse.h" 75#include "gallivm/lp_bld_type.h" 76#include "gallivm/lp_bld_const.h" 77#include "gallivm/lp_bld_conv.h" 78#include "gallivm/lp_bld_init.h" 79#include "gallivm/lp_bld_intr.h" 80#include "gallivm/lp_bld_logic.h" 81#include "gallivm/lp_bld_tgsi.h" 82#include "gallivm/lp_bld_swizzle.h" 83#include "gallivm/lp_bld_flow.h" 84#include "gallivm/lp_bld_debug.h" 85 86#include "lp_bld_alpha.h" 87#include "lp_bld_blend.h" 88#include "lp_bld_depth.h" 89#include "lp_bld_interp.h" 90#include "lp_context.h" 91#include "lp_debug.h" 92#include "lp_perf.h" 93#include "lp_screen.h" 94#include "lp_setup.h" 95#include "lp_state.h" 96#include "lp_tex_sample.h" 97#include "lp_flush.h" 98#include "lp_state_fs.h" 99 100 101#include <llvm-c/Analysis.h> 102#include <llvm-c/BitWriter.h> 103 104 105/** Fragment shader number (for debugging) */ 106static unsigned fs_no = 0; 107 108 109/** 110 * Expand the relevent bits of mask_input to a 4-dword mask for the 111 * four pixels in a 2x2 quad. This will set the four elements of the 112 * quad mask vector to 0 or ~0. 113 * 114 * \param quad which quad of the quad group to test, in [0,3] 115 * \param mask_input bitwise mask for the whole 4x4 stamp 116 */ 117static LLVMValueRef 118generate_quad_mask(struct gallivm_state *gallivm, 119 struct lp_type fs_type, 120 unsigned quad, 121 LLVMValueRef mask_input) /* int32 */ 122{ 123 LLVMBuilderRef builder = gallivm->builder; 124 struct lp_type mask_type; 125 LLVMTypeRef i32t = LLVMInt32TypeInContext(gallivm->context); 126 LLVMValueRef bits[4]; 127 LLVMValueRef mask; 128 int shift; 129 130 /* 131 * XXX: We'll need a different path for 16 x u8 132 */ 133 assert(fs_type.width == 32); 134 assert(fs_type.length == 4); 135 mask_type = lp_int_type(fs_type); 136 137 /* 138 * mask_input >>= (quad * 4) 139 */ 140 switch (quad) { 141 case 0: 142 shift = 0; 143 break; 144 case 1: 145 shift = 2; 146 break; 147 case 2: 148 shift = 8; 149 break; 150 case 3: 151 shift = 10; 152 break; 153 default: 154 assert(0); 155 shift = 0; 156 } 157 158 mask_input = LLVMBuildLShr(builder, 159 mask_input, 160 LLVMConstInt(i32t, shift, 0), 161 ""); 162 163 /* 164 * mask = { mask_input & (1 << i), for i in [0,3] } 165 */ 166 mask = lp_build_broadcast(gallivm, 167 lp_build_vec_type(gallivm, mask_type), 168 mask_input); 169 170 bits[0] = LLVMConstInt(i32t, 1 << 0, 0); 171 bits[1] = LLVMConstInt(i32t, 1 << 1, 0); 172 bits[2] = LLVMConstInt(i32t, 1 << 4, 0); 173 bits[3] = LLVMConstInt(i32t, 1 << 5, 0); 174 175 mask = LLVMBuildAnd(builder, mask, LLVMConstVector(bits, 4), ""); 176 177 /* 178 * mask = mask != 0 ? ~0 : 0 179 */ 180 mask = lp_build_compare(gallivm, 181 mask_type, PIPE_FUNC_NOTEQUAL, 182 mask, 183 lp_build_const_int_vec(gallivm, mask_type, 0)); 184 185 return mask; 186} 187 188 189#define EARLY_DEPTH_TEST 0x1 190#define LATE_DEPTH_TEST 0x2 191#define EARLY_DEPTH_WRITE 0x4 192#define LATE_DEPTH_WRITE 0x8 193 194static int 195find_output_by_semantic( const struct tgsi_shader_info *info, 196 unsigned semantic, 197 unsigned index ) 198{ 199 int i; 200 201 for (i = 0; i < info->num_outputs; i++) 202 if (info->output_semantic_name[i] == semantic && 203 info->output_semantic_index[i] == index) 204 return i; 205 206 return -1; 207} 208 209 210/** 211 * Generate the fragment shader, depth/stencil test, and alpha tests. 212 * \param i which quad in the tile, in range [0,3] 213 * \param partial_mask if 1, do mask_input testing 214 */ 215static void 216generate_fs(struct gallivm_state *gallivm, 217 struct lp_fragment_shader *shader, 218 const struct lp_fragment_shader_variant_key *key, 219 LLVMBuilderRef builder, 220 struct lp_type type, 221 LLVMValueRef context_ptr, 222 unsigned i, 223 struct lp_build_interp_soa_context *interp, 224 struct lp_build_sampler_soa *sampler, 225 LLVMValueRef *pmask, 226 LLVMValueRef (*color)[4], 227 LLVMValueRef depth_ptr, 228 LLVMValueRef facing, 229 unsigned partial_mask, 230 LLVMValueRef mask_input, 231 LLVMValueRef counter) 232{ 233 const struct util_format_description *zs_format_desc = NULL; 234 const struct tgsi_token *tokens = shader->base.tokens; 235 LLVMTypeRef vec_type; 236 LLVMValueRef consts_ptr; 237 LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][NUM_CHANNELS]; 238 LLVMValueRef z; 239 LLVMValueRef zs_value = NULL; 240 LLVMValueRef stencil_refs[2]; 241 struct lp_build_mask_context mask; 242 boolean simple_shader = (shader->info.base.file_count[TGSI_FILE_SAMPLER] == 0 && 243 shader->info.base.num_inputs < 3 && 244 shader->info.base.num_instructions < 8); 245 unsigned attrib; 246 unsigned chan; 247 unsigned cbuf; 248 unsigned depth_mode; 249 250 if (key->depth.enabled || 251 key->stencil[0].enabled || 252 key->stencil[1].enabled) { 253 254 zs_format_desc = util_format_description(key->zsbuf_format); 255 assert(zs_format_desc); 256 257 if (!shader->info.base.writes_z) { 258 if (key->alpha.enabled || shader->info.base.uses_kill) 259 /* With alpha test and kill, can do the depth test early 260 * and hopefully eliminate some quads. But need to do a 261 * special deferred depth write once the final mask value 262 * is known. 263 */ 264 depth_mode = EARLY_DEPTH_TEST | LATE_DEPTH_WRITE; 265 else 266 depth_mode = EARLY_DEPTH_TEST | EARLY_DEPTH_WRITE; 267 } 268 else { 269 depth_mode = LATE_DEPTH_TEST | LATE_DEPTH_WRITE; 270 } 271 272 if (!(key->depth.enabled && key->depth.writemask) && 273 !(key->stencil[0].enabled && key->stencil[0].writemask)) 274 depth_mode &= ~(LATE_DEPTH_WRITE | EARLY_DEPTH_WRITE); 275 } 276 else { 277 depth_mode = 0; 278 } 279 280 assert(i < 4); 281 282 stencil_refs[0] = lp_jit_context_stencil_ref_front_value(gallivm, context_ptr); 283 stencil_refs[1] = lp_jit_context_stencil_ref_back_value(gallivm, context_ptr); 284 285 vec_type = lp_build_vec_type(gallivm, type); 286 287 consts_ptr = lp_jit_context_constants(gallivm, context_ptr); 288 289 memset(outputs, 0, sizeof outputs); 290 291 /* Declare the color and z variables */ 292 for(cbuf = 0; cbuf < key->nr_cbufs; cbuf++) { 293 for(chan = 0; chan < NUM_CHANNELS; ++chan) { 294 color[cbuf][chan] = lp_build_alloca(gallivm, vec_type, "color"); 295 } 296 } 297 298 /* do triangle edge testing */ 299 if (partial_mask) { 300 *pmask = generate_quad_mask(gallivm, type, 301 i, mask_input); 302 } 303 else { 304 *pmask = lp_build_const_int_vec(gallivm, type, ~0); 305 } 306 307 /* 'mask' will control execution based on quad's pixel alive/killed state */ 308 lp_build_mask_begin(&mask, gallivm, type, *pmask); 309 310 if (!(depth_mode & EARLY_DEPTH_TEST) && !simple_shader) 311 lp_build_mask_check(&mask); 312 313 lp_build_interp_soa_update_pos(interp, gallivm, i); 314 z = interp->pos[2]; 315 316 if (depth_mode & EARLY_DEPTH_TEST) { 317 lp_build_depth_stencil_test(gallivm, 318 &key->depth, 319 key->stencil, 320 type, 321 zs_format_desc, 322 &mask, 323 stencil_refs, 324 z, 325 depth_ptr, facing, 326 &zs_value, 327 !simple_shader); 328 329 if (depth_mode & EARLY_DEPTH_WRITE) { 330 lp_build_depth_write(builder, zs_format_desc, depth_ptr, zs_value); 331 } 332 } 333 334 lp_build_interp_soa_update_inputs(interp, gallivm, i); 335 336 /* Build the actual shader */ 337 lp_build_tgsi_soa(gallivm, tokens, type, &mask, 338 consts_ptr, interp->pos, interp->inputs, 339 outputs, sampler, &shader->info.base); 340 341 /* Alpha test */ 342 if (key->alpha.enabled) { 343 int color0 = find_output_by_semantic(&shader->info.base, 344 TGSI_SEMANTIC_COLOR, 345 0); 346 347 if (color0 != -1 && outputs[color0][3]) { 348 LLVMValueRef alpha = LLVMBuildLoad(builder, outputs[color0][3], "alpha"); 349 LLVMValueRef alpha_ref_value; 350 351 alpha_ref_value = lp_jit_context_alpha_ref_value(gallivm, context_ptr); 352 alpha_ref_value = lp_build_broadcast(gallivm, vec_type, alpha_ref_value); 353 354 lp_build_alpha_test(gallivm, key->alpha.func, type, 355 &mask, alpha, alpha_ref_value, 356 (depth_mode & LATE_DEPTH_TEST) != 0); 357 } 358 } 359 360 /* Late Z test */ 361 if (depth_mode & LATE_DEPTH_TEST) { 362 int pos0 = find_output_by_semantic(&shader->info.base, 363 TGSI_SEMANTIC_POSITION, 364 0); 365 366 if (pos0 != -1 && outputs[pos0][2]) { 367 z = LLVMBuildLoad(builder, outputs[pos0][2], "output.z"); 368 } 369 370 lp_build_depth_stencil_test(gallivm, 371 &key->depth, 372 key->stencil, 373 type, 374 zs_format_desc, 375 &mask, 376 stencil_refs, 377 z, 378 depth_ptr, facing, 379 &zs_value, 380 !simple_shader); 381 /* Late Z write */ 382 if (depth_mode & LATE_DEPTH_WRITE) { 383 lp_build_depth_write(builder, zs_format_desc, depth_ptr, zs_value); 384 } 385 } 386 else if ((depth_mode & EARLY_DEPTH_TEST) && 387 (depth_mode & LATE_DEPTH_WRITE)) 388 { 389 /* Need to apply a reduced mask to the depth write. Reload the 390 * depth value, update from zs_value with the new mask value and 391 * write that out. 392 */ 393 lp_build_deferred_depth_write(gallivm, 394 type, 395 zs_format_desc, 396 &mask, 397 depth_ptr, 398 zs_value); 399 } 400 401 402 /* Color write */ 403 for (attrib = 0; attrib < shader->info.base.num_outputs; ++attrib) 404 { 405 if (shader->info.base.output_semantic_name[attrib] == TGSI_SEMANTIC_COLOR && 406 shader->info.base.output_semantic_index[attrib] < key->nr_cbufs) 407 { 408 unsigned cbuf = shader->info.base.output_semantic_index[attrib]; 409 for(chan = 0; chan < NUM_CHANNELS; ++chan) { 410 if(outputs[attrib][chan]) { 411 /* XXX: just initialize outputs to point at colors[] and 412 * skip this. 413 */ 414 LLVMValueRef out = LLVMBuildLoad(builder, outputs[attrib][chan], ""); 415 lp_build_name(out, "color%u.%u.%c", i, attrib, "rgba"[chan]); 416 LLVMBuildStore(builder, out, color[cbuf][chan]); 417 } 418 } 419 } 420 } 421 422 if (counter) 423 lp_build_occlusion_count(gallivm, type, 424 lp_build_mask_value(&mask), counter); 425 426 *pmask = lp_build_mask_end(&mask); 427} 428 429 430/** 431 * Generate color blending and color output. 432 * \param rt the render target index (to index blend, colormask state) 433 * \param type the pixel color type 434 * \param context_ptr pointer to the runtime JIT context 435 * \param mask execution mask (active fragment/pixel mask) 436 * \param src colors from the fragment shader 437 * \param dst_ptr the destination color buffer pointer 438 */ 439static void 440generate_blend(struct gallivm_state *gallivm, 441 const struct pipe_blend_state *blend, 442 unsigned rt, 443 LLVMBuilderRef builder, 444 struct lp_type type, 445 LLVMValueRef context_ptr, 446 LLVMValueRef mask, 447 LLVMValueRef *src, 448 LLVMValueRef dst_ptr, 449 boolean do_branch) 450{ 451 struct lp_build_context bld; 452 struct lp_build_mask_context mask_ctx; 453 LLVMTypeRef vec_type; 454 LLVMValueRef const_ptr; 455 LLVMValueRef con[4]; 456 LLVMValueRef dst[4]; 457 LLVMValueRef res[4]; 458 unsigned chan; 459 460 lp_build_context_init(&bld, gallivm, type); 461 462 lp_build_mask_begin(&mask_ctx, gallivm, type, mask); 463 if (do_branch) 464 lp_build_mask_check(&mask_ctx); 465 466 vec_type = lp_build_vec_type(gallivm, type); 467 468 const_ptr = lp_jit_context_blend_color(gallivm, context_ptr); 469 const_ptr = LLVMBuildBitCast(builder, const_ptr, 470 LLVMPointerType(vec_type, 0), ""); 471 472 /* load constant blend color and colors from the dest color buffer */ 473 for(chan = 0; chan < 4; ++chan) { 474 LLVMValueRef index = lp_build_const_int32(gallivm, chan); 475 con[chan] = LLVMBuildLoad(builder, LLVMBuildGEP(builder, const_ptr, &index, 1, ""), ""); 476 477 dst[chan] = LLVMBuildLoad(builder, LLVMBuildGEP(builder, dst_ptr, &index, 1, ""), ""); 478 479 lp_build_name(con[chan], "con.%c", "rgba"[chan]); 480 lp_build_name(dst[chan], "dst.%c", "rgba"[chan]); 481 } 482 483 /* do blend */ 484 lp_build_blend_soa(gallivm, blend, type, rt, src, dst, con, res); 485 486 /* store results to color buffer */ 487 for(chan = 0; chan < 4; ++chan) { 488 if(blend->rt[rt].colormask & (1 << chan)) { 489 LLVMValueRef index = lp_build_const_int32(gallivm, chan); 490 lp_build_name(res[chan], "res.%c", "rgba"[chan]); 491 res[chan] = lp_build_select(&bld, mask, res[chan], dst[chan]); 492 LLVMBuildStore(builder, res[chan], LLVMBuildGEP(builder, dst_ptr, &index, 1, "")); 493 } 494 } 495 496 lp_build_mask_end(&mask_ctx); 497} 498 499 500/** 501 * Generate the runtime callable function for the whole fragment pipeline. 502 * Note that the function which we generate operates on a block of 16 503 * pixels at at time. The block contains 2x2 quads. Each quad contains 504 * 2x2 pixels. 505 */ 506static void 507generate_fragment(struct llvmpipe_context *lp, 508 struct lp_fragment_shader *shader, 509 struct lp_fragment_shader_variant *variant, 510 unsigned partial_mask) 511{ 512 struct gallivm_state *gallivm = lp->gallivm; 513 const struct lp_fragment_shader_variant_key *key = &variant->key; 514 struct lp_shader_input inputs[PIPE_MAX_SHADER_INPUTS]; 515 char func_name[256]; 516 struct lp_type fs_type; 517 struct lp_type blend_type; 518 LLVMTypeRef fs_elem_type; 519 LLVMTypeRef fs_int_vec_type; 520 LLVMTypeRef blend_vec_type; 521 LLVMTypeRef arg_types[11]; 522 LLVMTypeRef func_type; 523 LLVMTypeRef int32_type = LLVMInt32TypeInContext(gallivm->context); 524 LLVMTypeRef int8_type = LLVMInt8TypeInContext(gallivm->context); 525 LLVMValueRef context_ptr; 526 LLVMValueRef x; 527 LLVMValueRef y; 528 LLVMValueRef a0_ptr; 529 LLVMValueRef dadx_ptr; 530 LLVMValueRef dady_ptr; 531 LLVMValueRef color_ptr_ptr; 532 LLVMValueRef depth_ptr; 533 LLVMValueRef mask_input; 534 LLVMValueRef counter = NULL; 535 LLVMBasicBlockRef block; 536 LLVMBuilderRef builder; 537 struct lp_build_sampler_soa *sampler; 538 struct lp_build_interp_soa_context interp; 539 LLVMValueRef fs_mask[LP_MAX_VECTOR_LENGTH]; 540 LLVMValueRef fs_out_color[PIPE_MAX_COLOR_BUFS][NUM_CHANNELS][LP_MAX_VECTOR_LENGTH]; 541 LLVMValueRef blend_mask; 542 LLVMValueRef function; 543 LLVMValueRef facing; 544 const struct util_format_description *zs_format_desc; 545 unsigned num_fs; 546 unsigned i; 547 unsigned chan; 548 unsigned cbuf; 549 550 /* Adjust color input interpolation according to flatshade state: 551 */ 552 memcpy(inputs, shader->inputs, shader->info.base.num_inputs * sizeof inputs[0]); 553 for (i = 0; i < shader->info.base.num_inputs; i++) { 554 if (inputs[i].interp == LP_INTERP_COLOR) { 555 if (key->flatshade) 556 inputs[i].interp = LP_INTERP_CONSTANT; 557 else 558 inputs[i].interp = LP_INTERP_LINEAR; 559 } 560 } 561 562 563 /* TODO: actually pick these based on the fs and color buffer 564 * characteristics. */ 565 566 memset(&fs_type, 0, sizeof fs_type); 567 fs_type.floating = TRUE; /* floating point values */ 568 fs_type.sign = TRUE; /* values are signed */ 569 fs_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */ 570 fs_type.width = 32; /* 32-bit float */ 571 fs_type.length = 4; /* 4 elements per vector */ 572 num_fs = 4; /* number of quads per block */ 573 574 memset(&blend_type, 0, sizeof blend_type); 575 blend_type.floating = FALSE; /* values are integers */ 576 blend_type.sign = FALSE; /* values are unsigned */ 577 blend_type.norm = TRUE; /* values are in [0,1] or [-1,1] */ 578 blend_type.width = 8; /* 8-bit ubyte values */ 579 blend_type.length = 16; /* 16 elements per vector */ 580 581 /* 582 * Generate the function prototype. Any change here must be reflected in 583 * lp_jit.h's lp_jit_frag_func function pointer type, and vice-versa. 584 */ 585 586 fs_elem_type = lp_build_elem_type(gallivm, fs_type); 587 fs_int_vec_type = lp_build_int_vec_type(gallivm, fs_type); 588 589 blend_vec_type = lp_build_vec_type(gallivm, blend_type); 590 591 util_snprintf(func_name, sizeof(func_name), "fs%u_variant%u_%s", 592 shader->no, variant->no, partial_mask ? "partial" : "whole"); 593 594 arg_types[0] = lp_jit_get_context_type(lp); /* context */ 595 arg_types[1] = int32_type; /* x */ 596 arg_types[2] = int32_type; /* y */ 597 arg_types[3] = int32_type; /* facing */ 598 arg_types[4] = LLVMPointerType(fs_elem_type, 0); /* a0 */ 599 arg_types[5] = LLVMPointerType(fs_elem_type, 0); /* dadx */ 600 arg_types[6] = LLVMPointerType(fs_elem_type, 0); /* dady */ 601 arg_types[7] = LLVMPointerType(LLVMPointerType(blend_vec_type, 0), 0); /* color */ 602 arg_types[8] = LLVMPointerType(int8_type, 0); /* depth */ 603 arg_types[9] = int32_type; /* mask_input */ 604 arg_types[10] = LLVMPointerType(int32_type, 0); /* counter */ 605 606 func_type = LLVMFunctionType(LLVMVoidTypeInContext(gallivm->context), 607 arg_types, Elements(arg_types), 0); 608 609 function = LLVMAddFunction(gallivm->module, func_name, func_type); 610 LLVMSetFunctionCallConv(function, LLVMCCallConv); 611 612 variant->function[partial_mask] = function; 613 614 /* XXX: need to propagate noalias down into color param now we are 615 * passing a pointer-to-pointer? 616 */ 617 for(i = 0; i < Elements(arg_types); ++i) 618 if(LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind) 619 LLVMAddAttribute(LLVMGetParam(function, i), LLVMNoAliasAttribute); 620 621 context_ptr = LLVMGetParam(function, 0); 622 x = LLVMGetParam(function, 1); 623 y = LLVMGetParam(function, 2); 624 facing = LLVMGetParam(function, 3); 625 a0_ptr = LLVMGetParam(function, 4); 626 dadx_ptr = LLVMGetParam(function, 5); 627 dady_ptr = LLVMGetParam(function, 6); 628 color_ptr_ptr = LLVMGetParam(function, 7); 629 depth_ptr = LLVMGetParam(function, 8); 630 mask_input = LLVMGetParam(function, 9); 631 632 lp_build_name(context_ptr, "context"); 633 lp_build_name(x, "x"); 634 lp_build_name(y, "y"); 635 lp_build_name(a0_ptr, "a0"); 636 lp_build_name(dadx_ptr, "dadx"); 637 lp_build_name(dady_ptr, "dady"); 638 lp_build_name(color_ptr_ptr, "color_ptr_ptr"); 639 lp_build_name(depth_ptr, "depth"); 640 lp_build_name(mask_input, "mask_input"); 641 642 if (key->occlusion_count) { 643 counter = LLVMGetParam(function, 10); 644 lp_build_name(counter, "counter"); 645 } 646 647 /* 648 * Function body 649 */ 650 651 block = LLVMAppendBasicBlockInContext(gallivm->context, function, "entry"); 652 builder = gallivm->builder; 653 assert(builder); 654 LLVMPositionBuilderAtEnd(builder, block); 655 656 /* 657 * The shader input interpolation info is not explicitely baked in the 658 * shader key, but everything it derives from (TGSI, and flatshade) is 659 * already included in the shader key. 660 */ 661 lp_build_interp_soa_init(&interp, 662 gallivm, 663 shader->info.base.num_inputs, 664 inputs, 665 builder, fs_type, 666 a0_ptr, dadx_ptr, dady_ptr, 667 x, y); 668 669 /* code generated texture sampling */ 670 sampler = lp_llvm_sampler_soa_create(key->sampler, context_ptr); 671 672 /* loop over quads in the block */ 673 zs_format_desc = util_format_description(key->zsbuf_format); 674 675 for(i = 0; i < num_fs; ++i) { 676 LLVMValueRef depth_offset = LLVMConstInt(int32_type, 677 i*fs_type.length*zs_format_desc->block.bits/8, 678 0); 679 LLVMValueRef out_color[PIPE_MAX_COLOR_BUFS][NUM_CHANNELS]; 680 LLVMValueRef depth_ptr_i; 681 682 depth_ptr_i = LLVMBuildGEP(builder, depth_ptr, &depth_offset, 1, ""); 683 684 generate_fs(gallivm, 685 shader, key, 686 builder, 687 fs_type, 688 context_ptr, 689 i, 690 &interp, 691 sampler, 692 &fs_mask[i], /* output */ 693 out_color, 694 depth_ptr_i, 695 facing, 696 partial_mask, 697 mask_input, 698 counter); 699 700 for(cbuf = 0; cbuf < key->nr_cbufs; cbuf++) 701 for(chan = 0; chan < NUM_CHANNELS; ++chan) 702 fs_out_color[cbuf][chan][i] = out_color[cbuf][chan]; 703 } 704 705 sampler->destroy(sampler); 706 707 /* Loop over color outputs / color buffers to do blending. 708 */ 709 for(cbuf = 0; cbuf < key->nr_cbufs; cbuf++) { 710 LLVMValueRef color_ptr; 711 LLVMValueRef index = lp_build_const_int32(gallivm, cbuf); 712 LLVMValueRef blend_in_color[NUM_CHANNELS]; 713 unsigned rt; 714 715 /* 716 * Convert the fs's output color and mask to fit to the blending type. 717 */ 718 for(chan = 0; chan < NUM_CHANNELS; ++chan) { 719 LLVMValueRef fs_color_vals[LP_MAX_VECTOR_LENGTH]; 720 721 for (i = 0; i < num_fs; i++) { 722 fs_color_vals[i] = 723 LLVMBuildLoad(builder, fs_out_color[cbuf][chan][i], "fs_color_vals"); 724 } 725 726 lp_build_conv(gallivm, fs_type, blend_type, 727 fs_color_vals, 728 num_fs, 729 &blend_in_color[chan], 1); 730 731 lp_build_name(blend_in_color[chan], "color%d.%c", cbuf, "rgba"[chan]); 732 } 733 734 if (partial_mask || !variant->opaque) { 735 lp_build_conv_mask(lp->gallivm, fs_type, blend_type, 736 fs_mask, num_fs, 737 &blend_mask, 1); 738 } else { 739 blend_mask = lp_build_const_int_vec(lp->gallivm, blend_type, ~0); 740 } 741 742 color_ptr = LLVMBuildLoad(builder, 743 LLVMBuildGEP(builder, color_ptr_ptr, &index, 1, ""), 744 ""); 745 lp_build_name(color_ptr, "color_ptr%d", cbuf); 746 747 /* which blend/colormask state to use */ 748 rt = key->blend.independent_blend_enable ? cbuf : 0; 749 750 /* 751 * Blending. 752 */ 753 { 754 /* Could the 4x4 have been killed? 755 */ 756 boolean do_branch = ((key->depth.enabled || key->stencil[0].enabled) && 757 !key->alpha.enabled && 758 !shader->info.base.uses_kill); 759 760 generate_blend(lp->gallivm, 761 &key->blend, 762 rt, 763 builder, 764 blend_type, 765 context_ptr, 766 blend_mask, 767 blend_in_color, 768 color_ptr, 769 do_branch); 770 } 771 } 772 773 LLVMBuildRetVoid(builder); 774 775 /* Verify the LLVM IR. If invalid, dump and abort */ 776#ifdef DEBUG 777 if(LLVMVerifyFunction(function, LLVMPrintMessageAction)) { 778 if (1) 779 lp_debug_dump_value(function); 780 abort(); 781 } 782#endif 783 784 /* Apply optimizations to LLVM IR */ 785 LLVMRunFunctionPassManager(gallivm->passmgr, function); 786 787 if ((gallivm_debug & GALLIVM_DEBUG_IR) || (LP_DEBUG & DEBUG_FS)) { 788 /* Print the LLVM IR to stderr */ 789 lp_debug_dump_value(function); 790 debug_printf("\n"); 791 } 792 793 /* Dump byte code to a file */ 794 if (0) { 795 LLVMWriteBitcodeToFile(gallivm->module, "llvmpipe.bc"); 796 } 797 798 /* 799 * Translate the LLVM IR into machine code. 800 */ 801 { 802 void *f = LLVMGetPointerToGlobal(gallivm->engine, function); 803 804 variant->jit_function[partial_mask] = (lp_jit_frag_func)pointer_to_func(f); 805 806 if ((gallivm_debug & GALLIVM_DEBUG_ASM) || (LP_DEBUG & DEBUG_FS)) { 807 lp_disassemble(f); 808 } 809 lp_func_delete_body(function); 810 } 811} 812 813 814static void 815dump_fs_variant_key(const struct lp_fragment_shader_variant_key *key) 816{ 817 unsigned i; 818 819 debug_printf("fs variant %p:\n", (void *) key); 820 821 if (key->flatshade) { 822 debug_printf("flatshade = 1\n"); 823 } 824 for (i = 0; i < key->nr_cbufs; ++i) { 825 debug_printf("cbuf_format[%u] = %s\n", i, util_format_name(key->cbuf_format[i])); 826 } 827 if (key->depth.enabled) { 828 debug_printf("depth.format = %s\n", util_format_name(key->zsbuf_format)); 829 debug_printf("depth.func = %s\n", util_dump_func(key->depth.func, TRUE)); 830 debug_printf("depth.writemask = %u\n", key->depth.writemask); 831 } 832 833 for (i = 0; i < 2; ++i) { 834 if (key->stencil[i].enabled) { 835 debug_printf("stencil[%u].func = %s\n", i, util_dump_func(key->stencil[i].func, TRUE)); 836 debug_printf("stencil[%u].fail_op = %s\n", i, util_dump_stencil_op(key->stencil[i].fail_op, TRUE)); 837 debug_printf("stencil[%u].zpass_op = %s\n", i, util_dump_stencil_op(key->stencil[i].zpass_op, TRUE)); 838 debug_printf("stencil[%u].zfail_op = %s\n", i, util_dump_stencil_op(key->stencil[i].zfail_op, TRUE)); 839 debug_printf("stencil[%u].valuemask = 0x%x\n", i, key->stencil[i].valuemask); 840 debug_printf("stencil[%u].writemask = 0x%x\n", i, key->stencil[i].writemask); 841 } 842 } 843 844 if (key->alpha.enabled) { 845 debug_printf("alpha.func = %s\n", util_dump_func(key->alpha.func, TRUE)); 846 } 847 848 if (key->occlusion_count) { 849 debug_printf("occlusion_count = 1\n"); 850 } 851 852 if (key->blend.logicop_enable) { 853 debug_printf("blend.logicop_func = %s\n", util_dump_logicop(key->blend.logicop_func, TRUE)); 854 } 855 else if (key->blend.rt[0].blend_enable) { 856 debug_printf("blend.rgb_func = %s\n", util_dump_blend_func (key->blend.rt[0].rgb_func, TRUE)); 857 debug_printf("blend.rgb_src_factor = %s\n", util_dump_blend_factor(key->blend.rt[0].rgb_src_factor, TRUE)); 858 debug_printf("blend.rgb_dst_factor = %s\n", util_dump_blend_factor(key->blend.rt[0].rgb_dst_factor, TRUE)); 859 debug_printf("blend.alpha_func = %s\n", util_dump_blend_func (key->blend.rt[0].alpha_func, TRUE)); 860 debug_printf("blend.alpha_src_factor = %s\n", util_dump_blend_factor(key->blend.rt[0].alpha_src_factor, TRUE)); 861 debug_printf("blend.alpha_dst_factor = %s\n", util_dump_blend_factor(key->blend.rt[0].alpha_dst_factor, TRUE)); 862 } 863 debug_printf("blend.colormask = 0x%x\n", key->blend.rt[0].colormask); 864 for (i = 0; i < key->nr_samplers; ++i) { 865 debug_printf("sampler[%u] = \n", i); 866 debug_printf(" .format = %s\n", 867 util_format_name(key->sampler[i].format)); 868 debug_printf(" .target = %s\n", 869 util_dump_tex_target(key->sampler[i].target, TRUE)); 870 debug_printf(" .pot = %u %u %u\n", 871 key->sampler[i].pot_width, 872 key->sampler[i].pot_height, 873 key->sampler[i].pot_depth); 874 debug_printf(" .wrap = %s %s %s\n", 875 util_dump_tex_wrap(key->sampler[i].wrap_s, TRUE), 876 util_dump_tex_wrap(key->sampler[i].wrap_t, TRUE), 877 util_dump_tex_wrap(key->sampler[i].wrap_r, TRUE)); 878 debug_printf(" .min_img_filter = %s\n", 879 util_dump_tex_filter(key->sampler[i].min_img_filter, TRUE)); 880 debug_printf(" .min_mip_filter = %s\n", 881 util_dump_tex_mipfilter(key->sampler[i].min_mip_filter, TRUE)); 882 debug_printf(" .mag_img_filter = %s\n", 883 util_dump_tex_filter(key->sampler[i].mag_img_filter, TRUE)); 884 if (key->sampler[i].compare_mode != PIPE_TEX_COMPARE_NONE) 885 debug_printf(" .compare_func = %s\n", util_dump_func(key->sampler[i].compare_func, TRUE)); 886 debug_printf(" .normalized_coords = %u\n", key->sampler[i].normalized_coords); 887 debug_printf(" .min_max_lod_equal = %u\n", key->sampler[i].min_max_lod_equal); 888 debug_printf(" .lod_bias_non_zero = %u\n", key->sampler[i].lod_bias_non_zero); 889 debug_printf(" .apply_min_lod = %u\n", key->sampler[i].apply_min_lod); 890 debug_printf(" .apply_max_lod = %u\n", key->sampler[i].apply_max_lod); 891 } 892} 893 894 895void 896lp_debug_fs_variant(const struct lp_fragment_shader_variant *variant) 897{ 898 debug_printf("llvmpipe: Fragment shader #%u variant #%u:\n", 899 variant->shader->no, variant->no); 900 tgsi_dump(variant->shader->base.tokens, 0); 901 dump_fs_variant_key(&variant->key); 902 debug_printf("variant->opaque = %u\n", variant->opaque); 903 debug_printf("\n"); 904} 905 906 907/** 908 * Generate a new fragment shader variant from the shader code and 909 * other state indicated by the key. 910 */ 911static struct lp_fragment_shader_variant * 912generate_variant(struct llvmpipe_context *lp, 913 struct lp_fragment_shader *shader, 914 const struct lp_fragment_shader_variant_key *key) 915{ 916 struct lp_fragment_shader_variant *variant; 917 boolean fullcolormask; 918 919 variant = CALLOC_STRUCT(lp_fragment_shader_variant); 920 if(!variant) 921 return NULL; 922 923 variant->shader = shader; 924 variant->list_item_global.base = variant; 925 variant->list_item_local.base = variant; 926 variant->no = shader->variants_created++; 927 928 memcpy(&variant->key, key, shader->variant_key_size); 929 930 /* 931 * Determine whether we are touching all channels in the color buffer. 932 */ 933 fullcolormask = FALSE; 934 if (key->nr_cbufs == 1) { 935 const struct util_format_description *format_desc; 936 format_desc = util_format_description(key->cbuf_format[0]); 937 if ((~key->blend.rt[0].colormask & 938 util_format_colormask(format_desc)) == 0) { 939 fullcolormask = TRUE; 940 } 941 } 942 943 variant->opaque = 944 !key->blend.logicop_enable && 945 !key->blend.rt[0].blend_enable && 946 fullcolormask && 947 !key->stencil[0].enabled && 948 !key->alpha.enabled && 949 !key->depth.enabled && 950 !shader->info.base.uses_kill 951 ? TRUE : FALSE; 952 953 954 if ((LP_DEBUG & DEBUG_FS) || (gallivm_debug & GALLIVM_DEBUG_IR)) { 955 lp_debug_fs_variant(variant); 956 } 957 958 generate_fragment(lp, shader, variant, RAST_EDGE_TEST); 959 960 if (variant->opaque) { 961 /* Specialized shader, which doesn't need to read the color buffer. */ 962 generate_fragment(lp, shader, variant, RAST_WHOLE); 963 } else { 964 variant->jit_function[RAST_WHOLE] = variant->jit_function[RAST_EDGE_TEST]; 965 } 966 967 return variant; 968} 969 970 971static void * 972llvmpipe_create_fs_state(struct pipe_context *pipe, 973 const struct pipe_shader_state *templ) 974{ 975 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe); 976 struct lp_fragment_shader *shader; 977 int nr_samplers; 978 int i; 979 980 shader = CALLOC_STRUCT(lp_fragment_shader); 981 if (!shader) 982 return NULL; 983 984 shader->no = fs_no++; 985 make_empty_list(&shader->variants); 986 987 /* get/save the summary info for this shader */ 988 lp_build_tgsi_info(templ->tokens, &shader->info); 989 990 /* we need to keep a local copy of the tokens */ 991 shader->base.tokens = tgsi_dup_tokens(templ->tokens); 992 993 shader->draw_data = draw_create_fragment_shader(llvmpipe->draw, templ); 994 if (shader->draw_data == NULL) { 995 FREE((void *) shader->base.tokens); 996 FREE(shader); 997 return NULL; 998 } 999 1000 nr_samplers = shader->info.base.file_max[TGSI_FILE_SAMPLER] + 1; 1001 1002 shader->variant_key_size = Offset(struct lp_fragment_shader_variant_key, 1003 sampler[nr_samplers]); 1004 1005 for (i = 0; i < shader->info.base.num_inputs; i++) { 1006 shader->inputs[i].usage_mask = shader->info.base.input_usage_mask[i]; 1007 1008 switch (shader->info.base.input_interpolate[i]) { 1009 case TGSI_INTERPOLATE_CONSTANT: 1010 shader->inputs[i].interp = LP_INTERP_CONSTANT; 1011 break; 1012 case TGSI_INTERPOLATE_LINEAR: 1013 shader->inputs[i].interp = LP_INTERP_LINEAR; 1014 break; 1015 case TGSI_INTERPOLATE_PERSPECTIVE: 1016 shader->inputs[i].interp = LP_INTERP_PERSPECTIVE; 1017 break; 1018 default: 1019 assert(0); 1020 break; 1021 } 1022 1023 switch (shader->info.base.input_semantic_name[i]) { 1024 case TGSI_SEMANTIC_COLOR: 1025 /* Colors may be either linearly or constant interpolated in 1026 * the fragment shader, but that information isn't available 1027 * here. Mark color inputs and fix them up later. 1028 */ 1029 shader->inputs[i].interp = LP_INTERP_COLOR; 1030 break; 1031 case TGSI_SEMANTIC_FACE: 1032 shader->inputs[i].interp = LP_INTERP_FACING; 1033 break; 1034 case TGSI_SEMANTIC_POSITION: 1035 /* Position was already emitted above 1036 */ 1037 shader->inputs[i].interp = LP_INTERP_POSITION; 1038 shader->inputs[i].src_index = 0; 1039 continue; 1040 } 1041 1042 shader->inputs[i].src_index = i+1; 1043 } 1044 1045 if (LP_DEBUG & DEBUG_TGSI) { 1046 unsigned attrib; 1047 debug_printf("llvmpipe: Create fragment shader #%u %p:\n", 1048 shader->no, (void *) shader); 1049 tgsi_dump(templ->tokens, 0); 1050 debug_printf("usage masks:\n"); 1051 for (attrib = 0; attrib < shader->info.base.num_inputs; ++attrib) { 1052 unsigned usage_mask = shader->info.base.input_usage_mask[attrib]; 1053 debug_printf(" IN[%u].%s%s%s%s\n", 1054 attrib, 1055 usage_mask & TGSI_WRITEMASK_X ? "x" : "", 1056 usage_mask & TGSI_WRITEMASK_Y ? "y" : "", 1057 usage_mask & TGSI_WRITEMASK_Z ? "z" : "", 1058 usage_mask & TGSI_WRITEMASK_W ? "w" : ""); 1059 } 1060 debug_printf("\n"); 1061 } 1062 1063 return shader; 1064} 1065 1066 1067static void 1068llvmpipe_bind_fs_state(struct pipe_context *pipe, void *fs) 1069{ 1070 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe); 1071 1072 if (llvmpipe->fs == fs) 1073 return; 1074 1075 draw_flush(llvmpipe->draw); 1076 1077 llvmpipe->fs = (struct lp_fragment_shader *) fs; 1078 1079 draw_bind_fragment_shader(llvmpipe->draw, 1080 (llvmpipe->fs ? llvmpipe->fs->draw_data : NULL)); 1081 1082 llvmpipe->dirty |= LP_NEW_FS; 1083} 1084 1085 1086/** 1087 * Remove shader variant from two lists: the shader's variant list 1088 * and the context's variant list. 1089 */ 1090void 1091llvmpipe_remove_shader_variant(struct llvmpipe_context *lp, 1092 struct lp_fragment_shader_variant *variant) 1093{ 1094 unsigned i; 1095 1096 if (gallivm_debug & GALLIVM_DEBUG_IR) { 1097 debug_printf("llvmpipe: del fs #%u var #%u v created #%u v cached" 1098 " #%u v total cached #%u\n", 1099 variant->shader->no, 1100 variant->no, 1101 variant->shader->variants_created, 1102 variant->shader->variants_cached, 1103 lp->nr_fs_variants); 1104 } 1105 1106 /* free all the variant's JIT'd functions */ 1107 for (i = 0; i < Elements(variant->function); i++) { 1108 if (variant->function[i]) { 1109 if (variant->jit_function[i]) 1110 LLVMFreeMachineCodeForFunction(lp->gallivm->engine, 1111 variant->function[i]); 1112 LLVMDeleteFunction(variant->function[i]); 1113 } 1114 } 1115 1116 /* remove from shader's list */ 1117 remove_from_list(&variant->list_item_local); 1118 variant->shader->variants_cached--; 1119 1120 /* remove from context's list */ 1121 remove_from_list(&variant->list_item_global); 1122 lp->nr_fs_variants--; 1123 1124 FREE(variant); 1125} 1126 1127 1128static void 1129llvmpipe_delete_fs_state(struct pipe_context *pipe, void *fs) 1130{ 1131 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe); 1132 struct lp_fragment_shader *shader = fs; 1133 struct lp_fs_variant_list_item *li; 1134 1135 assert(fs != llvmpipe->fs); 1136 1137 /* 1138 * XXX: we need to flush the context until we have some sort of reference 1139 * counting in fragment shaders as they may still be binned 1140 * Flushing alone might not sufficient we need to wait on it too. 1141 */ 1142 llvmpipe_finish(pipe, __FUNCTION__); 1143 1144 /* Delete all the variants */ 1145 li = first_elem(&shader->variants); 1146 while(!at_end(&shader->variants, li)) { 1147 struct lp_fs_variant_list_item *next = next_elem(li); 1148 llvmpipe_remove_shader_variant(llvmpipe, li->base); 1149 li = next; 1150 } 1151 1152 /* Delete draw module's data */ 1153 draw_delete_fragment_shader(llvmpipe->draw, shader->draw_data); 1154 1155 assert(shader->variants_cached == 0); 1156 FREE((void *) shader->base.tokens); 1157 FREE(shader); 1158} 1159 1160 1161 1162static void 1163llvmpipe_set_constant_buffer(struct pipe_context *pipe, 1164 uint shader, uint index, 1165 struct pipe_resource *constants) 1166{ 1167 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe); 1168 unsigned size = constants ? constants->width0 : 0; 1169 const void *data = constants ? llvmpipe_resource_data(constants) : NULL; 1170 1171 assert(shader < PIPE_SHADER_TYPES); 1172 assert(index < PIPE_MAX_CONSTANT_BUFFERS); 1173 1174 if(llvmpipe->constants[shader][index] == constants) 1175 return; 1176 1177 draw_flush(llvmpipe->draw); 1178 1179 /* note: reference counting */ 1180 pipe_resource_reference(&llvmpipe->constants[shader][index], constants); 1181 1182 if(shader == PIPE_SHADER_VERTEX || 1183 shader == PIPE_SHADER_GEOMETRY) { 1184 draw_set_mapped_constant_buffer(llvmpipe->draw, shader, 1185 index, data, size); 1186 } 1187 1188 llvmpipe->dirty |= LP_NEW_CONSTANTS; 1189} 1190 1191 1192/** 1193 * Return the blend factor equivalent to a destination alpha of one. 1194 */ 1195static INLINE unsigned 1196force_dst_alpha_one(unsigned factor) 1197{ 1198 switch(factor) { 1199 case PIPE_BLENDFACTOR_DST_ALPHA: 1200 return PIPE_BLENDFACTOR_ONE; 1201 case PIPE_BLENDFACTOR_INV_DST_ALPHA: 1202 return PIPE_BLENDFACTOR_ZERO; 1203 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE: 1204 return PIPE_BLENDFACTOR_ZERO; 1205 } 1206 1207 return factor; 1208} 1209 1210 1211/** 1212 * We need to generate several variants of the fragment pipeline to match 1213 * all the combinations of the contributing state atoms. 1214 * 1215 * TODO: there is actually no reason to tie this to context state -- the 1216 * generated code could be cached globally in the screen. 1217 */ 1218static void 1219make_variant_key(struct llvmpipe_context *lp, 1220 struct lp_fragment_shader *shader, 1221 struct lp_fragment_shader_variant_key *key) 1222{ 1223 unsigned i; 1224 1225 memset(key, 0, shader->variant_key_size); 1226 1227 if (lp->framebuffer.zsbuf) { 1228 if (lp->depth_stencil->depth.enabled) { 1229 key->zsbuf_format = lp->framebuffer.zsbuf->format; 1230 memcpy(&key->depth, &lp->depth_stencil->depth, sizeof key->depth); 1231 } 1232 if (lp->depth_stencil->stencil[0].enabled) { 1233 key->zsbuf_format = lp->framebuffer.zsbuf->format; 1234 memcpy(&key->stencil, &lp->depth_stencil->stencil, sizeof key->stencil); 1235 } 1236 } 1237 1238 key->alpha.enabled = lp->depth_stencil->alpha.enabled; 1239 if(key->alpha.enabled) 1240 key->alpha.func = lp->depth_stencil->alpha.func; 1241 /* alpha.ref_value is passed in jit_context */ 1242 1243 key->flatshade = lp->rasterizer->flatshade; 1244 if (lp->active_query_count) { 1245 key->occlusion_count = TRUE; 1246 } 1247 1248 if (lp->framebuffer.nr_cbufs) { 1249 memcpy(&key->blend, lp->blend, sizeof key->blend); 1250 } 1251 1252 key->nr_cbufs = lp->framebuffer.nr_cbufs; 1253 for (i = 0; i < lp->framebuffer.nr_cbufs; i++) { 1254 enum pipe_format format = lp->framebuffer.cbufs[i]->format; 1255 struct pipe_rt_blend_state *blend_rt = &key->blend.rt[i]; 1256 const struct util_format_description *format_desc; 1257 1258 key->cbuf_format[i] = format; 1259 1260 format_desc = util_format_description(format); 1261 assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB || 1262 format_desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB); 1263 1264 blend_rt->colormask = lp->blend->rt[i].colormask; 1265 1266 /* 1267 * Mask out color channels not present in the color buffer. 1268 */ 1269 blend_rt->colormask &= util_format_colormask(format_desc); 1270 1271 /* 1272 * Our swizzled render tiles always have an alpha channel, but the linear 1273 * render target format often does not, so force here the dst alpha to be 1274 * one. 1275 * 1276 * This is not a mere optimization. Wrong results will be produced if the 1277 * dst alpha is used, the dst format does not have alpha, and the previous 1278 * rendering was not flushed from the swizzled to linear buffer. For 1279 * example, NonPowTwo DCT. 1280 * 1281 * TODO: This should be generalized to all channels for better 1282 * performance, but only alpha causes correctness issues. 1283 * 1284 * Also, force rgb/alpha func/factors match, to make AoS blending easier. 1285 */ 1286 if (format_desc->swizzle[3] > UTIL_FORMAT_SWIZZLE_W) { 1287 blend_rt->rgb_src_factor = force_dst_alpha_one(blend_rt->rgb_src_factor); 1288 blend_rt->rgb_dst_factor = force_dst_alpha_one(blend_rt->rgb_dst_factor); 1289 blend_rt->alpha_func = blend_rt->rgb_func; 1290 blend_rt->alpha_src_factor = blend_rt->rgb_src_factor; 1291 blend_rt->alpha_dst_factor = blend_rt->rgb_dst_factor; 1292 } 1293 } 1294 1295 /* This value will be the same for all the variants of a given shader: 1296 */ 1297 key->nr_samplers = shader->info.base.file_max[TGSI_FILE_SAMPLER] + 1; 1298 1299 for(i = 0; i < key->nr_samplers; ++i) { 1300 if(shader->info.base.file_mask[TGSI_FILE_SAMPLER] & (1 << i)) { 1301 lp_sampler_static_state(&key->sampler[i], 1302 lp->fragment_sampler_views[i], 1303 lp->sampler[i]); 1304 } 1305 } 1306} 1307 1308 1309 1310/** 1311 * Update fragment shader state. This is called just prior to drawing 1312 * something when some fragment-related state has changed. 1313 */ 1314void 1315llvmpipe_update_fs(struct llvmpipe_context *lp) 1316{ 1317 struct lp_fragment_shader *shader = lp->fs; 1318 struct lp_fragment_shader_variant_key key; 1319 struct lp_fragment_shader_variant *variant = NULL; 1320 struct lp_fs_variant_list_item *li; 1321 1322 make_variant_key(lp, shader, &key); 1323 1324 /* Search the variants for one which matches the key */ 1325 li = first_elem(&shader->variants); 1326 while(!at_end(&shader->variants, li)) { 1327 if(memcmp(&li->base->key, &key, shader->variant_key_size) == 0) { 1328 variant = li->base; 1329 break; 1330 } 1331 li = next_elem(li); 1332 } 1333 1334 if (variant) { 1335 /* Move this variant to the head of the list to implement LRU 1336 * deletion of shader's when we have too many. 1337 */ 1338 move_to_head(&lp->fs_variants_list, &variant->list_item_global); 1339 } 1340 else { 1341 /* variant not found, create it now */ 1342 int64_t t0, t1, dt; 1343 unsigned i; 1344 1345 /* First, check if we've exceeded the max number of shader variants. 1346 * If so, free 25% of them (the least recently used ones). 1347 */ 1348 if (lp->nr_fs_variants >= LP_MAX_SHADER_VARIANTS) { 1349 struct pipe_context *pipe = &lp->pipe; 1350 1351 /* 1352 * XXX: we need to flush the context until we have some sort of 1353 * reference counting in fragment shaders as they may still be binned 1354 * Flushing alone might not be sufficient we need to wait on it too. 1355 */ 1356 llvmpipe_finish(pipe, __FUNCTION__); 1357 1358 for (i = 0; i < LP_MAX_SHADER_VARIANTS / 4; i++) { 1359 struct lp_fs_variant_list_item *item; 1360 item = last_elem(&lp->fs_variants_list); 1361 llvmpipe_remove_shader_variant(lp, item->base); 1362 } 1363 } 1364 1365 /* 1366 * Generate the new variant. 1367 */ 1368 t0 = os_time_get(); 1369 variant = generate_variant(lp, shader, &key); 1370 t1 = os_time_get(); 1371 dt = t1 - t0; 1372 LP_COUNT_ADD(llvm_compile_time, dt); 1373 LP_COUNT_ADD(nr_llvm_compiles, 2); /* emit vs. omit in/out test */ 1374 1375 llvmpipe_variant_count++; 1376 1377 /* Put the new variant into the list */ 1378 if (variant) { 1379 insert_at_head(&shader->variants, &variant->list_item_local); 1380 insert_at_head(&lp->fs_variants_list, &variant->list_item_global); 1381 lp->nr_fs_variants++; 1382 shader->variants_cached++; 1383 } 1384 } 1385 1386 /* Bind this variant */ 1387 lp_setup_set_fs_variant(lp->setup, variant); 1388} 1389 1390 1391 1392 1393 1394 1395 1396void 1397llvmpipe_init_fs_funcs(struct llvmpipe_context *llvmpipe) 1398{ 1399 llvmpipe->pipe.create_fs_state = llvmpipe_create_fs_state; 1400 llvmpipe->pipe.bind_fs_state = llvmpipe_bind_fs_state; 1401 llvmpipe->pipe.delete_fs_state = llvmpipe_delete_fs_state; 1402 1403 llvmpipe->pipe.set_constant_buffer = llvmpipe_set_constant_buffer; 1404} 1405