lp_bld_depth.c revision efc82aef35a2aac5d2ed9774f6d28f2626796416
1/************************************************************************** 2 * 3 * Copyright 2009-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/** 29 * @file 30 * Depth/stencil testing to LLVM IR translation. 31 * 32 * To be done accurately/efficiently the depth/stencil test must be done with 33 * the same type/format of the depth/stencil buffer, which implies massaging 34 * the incoming depths to fit into place. Using a more straightforward 35 * type/format for depth/stencil values internally and only convert when 36 * flushing would avoid this, but it would most likely result in depth fighting 37 * artifacts. 38 * 39 * We are free to use a different pixel layout though. Since our basic 40 * processing unit is a quad (2x2 pixel block) we store the depth/stencil 41 * values tiled, a quad at time. That is, a depth buffer containing 42 * 43 * Z11 Z12 Z13 Z14 ... 44 * Z21 Z22 Z23 Z24 ... 45 * Z31 Z32 Z33 Z34 ... 46 * Z41 Z42 Z43 Z44 ... 47 * ... ... ... ... ... 48 * 49 * will actually be stored in memory as 50 * 51 * Z11 Z12 Z21 Z22 Z13 Z14 Z23 Z24 ... 52 * Z31 Z32 Z41 Z42 Z33 Z34 Z43 Z44 ... 53 * ... ... ... ... ... ... ... ... ... 54 * 55 * 56 * @author Jose Fonseca <jfonseca@vmware.com> 57 * @author Brian Paul <jfonseca@vmware.com> 58 */ 59 60#include "pipe/p_state.h" 61#include "util/u_format.h" 62 63#include "gallivm/lp_bld_type.h" 64#include "gallivm/lp_bld_arit.h" 65#include "gallivm/lp_bld_bitarit.h" 66#include "gallivm/lp_bld_const.h" 67#include "gallivm/lp_bld_conv.h" 68#include "gallivm/lp_bld_logic.h" 69#include "gallivm/lp_bld_flow.h" 70#include "gallivm/lp_bld_intr.h" 71#include "gallivm/lp_bld_debug.h" 72#include "gallivm/lp_bld_swizzle.h" 73 74#include "lp_bld_depth.h" 75 76 77/** Used to select fields from pipe_stencil_state */ 78enum stencil_op { 79 S_FAIL_OP, 80 Z_FAIL_OP, 81 Z_PASS_OP 82}; 83 84 85 86/** 87 * Do the stencil test comparison (compare FB stencil values against ref value). 88 * This will be used twice when generating two-sided stencil code. 89 * \param stencil the front/back stencil state 90 * \param stencilRef the stencil reference value, replicated as a vector 91 * \param stencilVals vector of stencil values from framebuffer 92 * \return vector mask of pass/fail values (~0 or 0) 93 */ 94static LLVMValueRef 95lp_build_stencil_test_single(struct lp_build_context *bld, 96 const struct pipe_stencil_state *stencil, 97 LLVMValueRef stencilRef, 98 LLVMValueRef stencilVals) 99{ 100 const unsigned stencilMax = 255; /* XXX fix */ 101 struct lp_type type = bld->type; 102 LLVMValueRef res; 103 104 assert(type.sign); 105 106 assert(stencil->enabled); 107 108 if (stencil->valuemask != stencilMax) { 109 /* compute stencilRef = stencilRef & valuemask */ 110 LLVMValueRef valuemask = lp_build_const_int_vec(bld->gallivm, type, stencil->valuemask); 111 stencilRef = LLVMBuildAnd(bld->builder, stencilRef, valuemask, ""); 112 /* compute stencilVals = stencilVals & valuemask */ 113 stencilVals = LLVMBuildAnd(bld->builder, stencilVals, valuemask, ""); 114 } 115 116 res = lp_build_cmp(bld, stencil->func, stencilRef, stencilVals); 117 118 return res; 119} 120 121 122/** 123 * Do the one or two-sided stencil test comparison. 124 * \sa lp_build_stencil_test_single 125 * \param front_facing an integer vector mask, indicating front (~0) or back 126 * (0) facing polygon. If NULL, assume front-facing. 127 */ 128static LLVMValueRef 129lp_build_stencil_test(struct lp_build_context *bld, 130 const struct pipe_stencil_state stencil[2], 131 LLVMValueRef stencilRefs[2], 132 LLVMValueRef stencilVals, 133 LLVMValueRef front_facing) 134{ 135 LLVMValueRef res; 136 137 assert(stencil[0].enabled); 138 139 /* do front face test */ 140 res = lp_build_stencil_test_single(bld, &stencil[0], 141 stencilRefs[0], stencilVals); 142 143 if (stencil[1].enabled && front_facing) { 144 /* do back face test */ 145 LLVMValueRef back_res; 146 147 back_res = lp_build_stencil_test_single(bld, &stencil[1], 148 stencilRefs[1], stencilVals); 149 150 res = lp_build_select(bld, front_facing, res, back_res); 151 } 152 153 return res; 154} 155 156 157/** 158 * Apply the stencil operator (add/sub/keep/etc) to the given vector 159 * of stencil values. 160 * \return new stencil values vector 161 */ 162static LLVMValueRef 163lp_build_stencil_op_single(struct lp_build_context *bld, 164 const struct pipe_stencil_state *stencil, 165 enum stencil_op op, 166 LLVMValueRef stencilRef, 167 LLVMValueRef stencilVals) 168 169{ 170 struct lp_type type = bld->type; 171 LLVMValueRef res; 172 LLVMValueRef max = lp_build_const_int_vec(bld->gallivm, type, 0xff); 173 unsigned stencil_op; 174 175 assert(type.sign); 176 177 switch (op) { 178 case S_FAIL_OP: 179 stencil_op = stencil->fail_op; 180 break; 181 case Z_FAIL_OP: 182 stencil_op = stencil->zfail_op; 183 break; 184 case Z_PASS_OP: 185 stencil_op = stencil->zpass_op; 186 break; 187 default: 188 assert(0 && "Invalid stencil_op mode"); 189 stencil_op = PIPE_STENCIL_OP_KEEP; 190 } 191 192 switch (stencil_op) { 193 case PIPE_STENCIL_OP_KEEP: 194 res = stencilVals; 195 /* we can return early for this case */ 196 return res; 197 case PIPE_STENCIL_OP_ZERO: 198 res = bld->zero; 199 break; 200 case PIPE_STENCIL_OP_REPLACE: 201 res = stencilRef; 202 break; 203 case PIPE_STENCIL_OP_INCR: 204 res = lp_build_add(bld, stencilVals, bld->one); 205 res = lp_build_min(bld, res, max); 206 break; 207 case PIPE_STENCIL_OP_DECR: 208 res = lp_build_sub(bld, stencilVals, bld->one); 209 res = lp_build_max(bld, res, bld->zero); 210 break; 211 case PIPE_STENCIL_OP_INCR_WRAP: 212 res = lp_build_add(bld, stencilVals, bld->one); 213 res = LLVMBuildAnd(bld->builder, res, max, ""); 214 break; 215 case PIPE_STENCIL_OP_DECR_WRAP: 216 res = lp_build_sub(bld, stencilVals, bld->one); 217 res = LLVMBuildAnd(bld->builder, res, max, ""); 218 break; 219 case PIPE_STENCIL_OP_INVERT: 220 res = LLVMBuildNot(bld->builder, stencilVals, ""); 221 res = LLVMBuildAnd(bld->builder, res, max, ""); 222 break; 223 default: 224 assert(0 && "bad stencil op mode"); 225 res = bld->undef; 226 } 227 228 return res; 229} 230 231 232/** 233 * Do the one or two-sided stencil test op/update. 234 */ 235static LLVMValueRef 236lp_build_stencil_op(struct lp_build_context *bld, 237 const struct pipe_stencil_state stencil[2], 238 enum stencil_op op, 239 LLVMValueRef stencilRefs[2], 240 LLVMValueRef stencilVals, 241 LLVMValueRef mask, 242 LLVMValueRef front_facing) 243 244{ 245 LLVMValueRef res; 246 247 assert(stencil[0].enabled); 248 249 /* do front face op */ 250 res = lp_build_stencil_op_single(bld, &stencil[0], op, 251 stencilRefs[0], stencilVals); 252 253 if (stencil[1].enabled && front_facing) { 254 /* do back face op */ 255 LLVMValueRef back_res; 256 257 back_res = lp_build_stencil_op_single(bld, &stencil[1], op, 258 stencilRefs[1], stencilVals); 259 260 res = lp_build_select(bld, front_facing, res, back_res); 261 } 262 263 if (stencil->writemask != 0xff) { 264 /* mask &= stencil->writemask */ 265 LLVMValueRef writemask = lp_build_const_int_vec(bld->gallivm, bld->type, 266 stencil->writemask); 267 mask = LLVMBuildAnd(bld->builder, mask, writemask, ""); 268 /* res = (res & mask) | (stencilVals & ~mask) */ 269 res = lp_build_select_bitwise(bld, writemask, res, stencilVals); 270 } 271 else { 272 /* res = mask ? res : stencilVals */ 273 res = lp_build_select(bld, mask, res, stencilVals); 274 } 275 276 return res; 277} 278 279 280 281/** 282 * Return a type appropriate for depth/stencil testing. 283 */ 284struct lp_type 285lp_depth_type(const struct util_format_description *format_desc, 286 unsigned length) 287{ 288 struct lp_type type; 289 unsigned swizzle; 290 291 assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS); 292 assert(format_desc->block.width == 1); 293 assert(format_desc->block.height == 1); 294 295 swizzle = format_desc->swizzle[0]; 296 assert(swizzle < 4); 297 298 memset(&type, 0, sizeof type); 299 type.width = format_desc->block.bits; 300 301 if(format_desc->channel[swizzle].type == UTIL_FORMAT_TYPE_FLOAT) { 302 type.floating = TRUE; 303 assert(swizzle == 0); 304 assert(format_desc->channel[swizzle].size == format_desc->block.bits); 305 } 306 else if(format_desc->channel[swizzle].type == UTIL_FORMAT_TYPE_UNSIGNED) { 307 assert(format_desc->block.bits <= 32); 308 assert(format_desc->channel[swizzle].normalized); 309 if (format_desc->channel[swizzle].size < format_desc->block.bits) { 310 /* Prefer signed integers when possible, as SSE has less support 311 * for unsigned comparison; 312 */ 313 type.sign = TRUE; 314 } 315 } 316 else 317 assert(0); 318 319 assert(type.width <= length); 320 type.length = length / type.width; 321 322 return type; 323} 324 325 326/** 327 * Compute bitmask and bit shift to apply to the incoming fragment Z values 328 * and the Z buffer values needed before doing the Z comparison. 329 * 330 * Note that we leave the Z bits in the position that we find them 331 * in the Z buffer (typically 0xffffff00 or 0x00ffffff). That lets us 332 * get by with fewer bit twiddling steps. 333 */ 334static boolean 335get_z_shift_and_mask(const struct util_format_description *format_desc, 336 unsigned *shift, unsigned *width, unsigned *mask) 337{ 338 const unsigned total_bits = format_desc->block.bits; 339 unsigned z_swizzle; 340 unsigned chan; 341 unsigned padding_left, padding_right; 342 343 assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS); 344 assert(format_desc->block.width == 1); 345 assert(format_desc->block.height == 1); 346 347 z_swizzle = format_desc->swizzle[0]; 348 349 if (z_swizzle == UTIL_FORMAT_SWIZZLE_NONE) 350 return FALSE; 351 352 *width = format_desc->channel[z_swizzle].size; 353 354 padding_right = 0; 355 for (chan = 0; chan < z_swizzle; ++chan) 356 padding_right += format_desc->channel[chan].size; 357 358 padding_left = 359 total_bits - (padding_right + *width); 360 361 if (padding_left || padding_right) { 362 unsigned long long mask_left = (1ULL << (total_bits - padding_left)) - 1; 363 unsigned long long mask_right = (1ULL << (padding_right)) - 1; 364 *mask = mask_left ^ mask_right; 365 } 366 else { 367 *mask = 0xffffffff; 368 } 369 370 *shift = padding_right; 371 372 return TRUE; 373} 374 375 376/** 377 * Compute bitmask and bit shift to apply to the framebuffer pixel values 378 * to put the stencil bits in the least significant position. 379 * (i.e. 0x000000ff) 380 */ 381static boolean 382get_s_shift_and_mask(const struct util_format_description *format_desc, 383 unsigned *shift, unsigned *mask) 384{ 385 unsigned s_swizzle; 386 unsigned chan, sz; 387 388 s_swizzle = format_desc->swizzle[1]; 389 390 if (s_swizzle == UTIL_FORMAT_SWIZZLE_NONE) 391 return FALSE; 392 393 *shift = 0; 394 for (chan = 0; chan < s_swizzle; chan++) 395 *shift += format_desc->channel[chan].size; 396 397 sz = format_desc->channel[s_swizzle].size; 398 *mask = (1U << sz) - 1U; 399 400 return TRUE; 401} 402 403 404/** 405 * Perform the occlusion test and increase the counter. 406 * Test the depth mask. Add the number of channel which has none zero mask 407 * into the occlusion counter. e.g. maskvalue is {-1, -1, -1, -1}. 408 * The counter will add 4. 409 * 410 * \param type holds element type of the mask vector. 411 * \param maskvalue is the depth test mask. 412 * \param counter is a pointer of the uint32 counter. 413 */ 414void 415lp_build_occlusion_count(struct gallivm_state *gallivm, 416 struct lp_type type, 417 LLVMValueRef maskvalue, 418 LLVMValueRef counter) 419{ 420 LLVMBuilderRef builder = gallivm->builder; 421 LLVMContextRef context = gallivm->context; 422 LLVMValueRef countmask = lp_build_const_int_vec(gallivm, type, 1); 423 LLVMValueRef countv = LLVMBuildAnd(builder, maskvalue, countmask, "countv"); 424 LLVMTypeRef i8v16 = LLVMVectorType(LLVMInt8TypeInContext(context), 16); 425 LLVMValueRef counti = LLVMBuildBitCast(builder, countv, i8v16, "counti"); 426 LLVMValueRef maskarray[4] = { 427 lp_build_const_int32(gallivm, 0), 428 lp_build_const_int32(gallivm, 4), 429 lp_build_const_int32(gallivm, 8), 430 lp_build_const_int32(gallivm, 12) 431 }; 432 LLVMValueRef shufflemask = LLVMConstVector(maskarray, 4); 433 LLVMValueRef shufflev = LLVMBuildShuffleVector(builder, counti, LLVMGetUndef(i8v16), shufflemask, "shufflev"); 434 LLVMValueRef shuffle = LLVMBuildBitCast(builder, shufflev, LLVMInt32TypeInContext(context), "shuffle"); 435 LLVMValueRef count = lp_build_intrinsic_unary(builder, "llvm.ctpop.i32", LLVMInt32TypeInContext(context), shuffle); 436 LLVMValueRef orig = LLVMBuildLoad(builder, counter, "orig"); 437 LLVMValueRef incr = LLVMBuildAdd(builder, orig, count, "incr"); 438 LLVMBuildStore(builder, incr, counter); 439} 440 441 442 443/** 444 * Generate code for performing depth and/or stencil tests. 445 * We operate on a vector of values (typically a 2x2 quad). 446 * 447 * \param depth the depth test state 448 * \param stencil the front/back stencil state 449 * \param type the data type of the fragment depth/stencil values 450 * \param format_desc description of the depth/stencil surface 451 * \param mask the alive/dead pixel mask for the quad (vector) 452 * \param stencil_refs the front/back stencil ref values (scalar) 453 * \param z_src the incoming depth/stencil values (a 2x2 quad, float32) 454 * \param zs_dst_ptr pointer to depth/stencil values in framebuffer 455 * \param facing contains boolean value indicating front/back facing polygon 456 */ 457void 458lp_build_depth_stencil_test(struct gallivm_state *gallivm, 459 const struct pipe_depth_state *depth, 460 const struct pipe_stencil_state stencil[2], 461 struct lp_type z_src_type, 462 const struct util_format_description *format_desc, 463 struct lp_build_mask_context *mask, 464 LLVMValueRef stencil_refs[2], 465 LLVMValueRef z_src, 466 LLVMValueRef zs_dst_ptr, 467 LLVMValueRef face, 468 LLVMValueRef *zs_value, 469 boolean do_branch) 470{ 471 LLVMBuilderRef builder = gallivm->builder; 472 struct lp_type z_type; 473 struct lp_build_context z_bld; 474 struct lp_build_context s_bld; 475 struct lp_type s_type; 476 unsigned z_shift = 0, z_width = 0, z_mask = 0; 477 LLVMValueRef zs_dst, z_dst = NULL; 478 LLVMValueRef stencil_vals = NULL; 479 LLVMValueRef z_bitmask = NULL, stencil_shift = NULL; 480 LLVMValueRef z_pass = NULL, s_pass_mask = NULL; 481 LLVMValueRef orig_mask = lp_build_mask_value(mask); 482 LLVMValueRef front_facing = NULL; 483 484 485 /* 486 * Depths are expected to be between 0 and 1, even if they are stored in 487 * floats. Setting these bits here will ensure that the lp_build_conv() call 488 * below won't try to unnecessarily clamp the incoming values. 489 */ 490 if(z_src_type.floating) { 491 z_src_type.sign = FALSE; 492 z_src_type.norm = TRUE; 493 } 494 else { 495 assert(!z_src_type.sign); 496 assert(z_src_type.norm); 497 } 498 499 /* Pick the depth type. */ 500 z_type = lp_depth_type(format_desc, z_src_type.width*z_src_type.length); 501 502 /* FIXME: Cope with a depth test type with a different bit width. */ 503 assert(z_type.width == z_src_type.width); 504 assert(z_type.length == z_src_type.length); 505 506 /* Sanity checking */ 507 { 508 const unsigned z_swizzle = format_desc->swizzle[0]; 509 const unsigned s_swizzle = format_desc->swizzle[1]; 510 511 assert(z_swizzle != UTIL_FORMAT_SWIZZLE_NONE || 512 s_swizzle != UTIL_FORMAT_SWIZZLE_NONE); 513 514 assert(depth->enabled || stencil[0].enabled); 515 516 assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS); 517 assert(format_desc->block.width == 1); 518 assert(format_desc->block.height == 1); 519 520 if (stencil[0].enabled) { 521 assert(format_desc->format == PIPE_FORMAT_Z24_UNORM_S8_USCALED || 522 format_desc->format == PIPE_FORMAT_S8_USCALED_Z24_UNORM); 523 } 524 525 assert(z_swizzle < 4); 526 assert(format_desc->block.bits == z_type.width); 527 if (z_type.floating) { 528 assert(z_swizzle == 0); 529 assert(format_desc->channel[z_swizzle].type == 530 UTIL_FORMAT_TYPE_FLOAT); 531 assert(format_desc->channel[z_swizzle].size == 532 format_desc->block.bits); 533 } 534 else { 535 assert(format_desc->channel[z_swizzle].type == 536 UTIL_FORMAT_TYPE_UNSIGNED); 537 assert(format_desc->channel[z_swizzle].normalized); 538 assert(!z_type.fixed); 539 } 540 } 541 542 543 /* Setup build context for Z vals */ 544 lp_build_context_init(&z_bld, gallivm, z_type); 545 546 /* Setup build context for stencil vals */ 547 s_type = lp_type_int_vec(z_type.width); 548 lp_build_context_init(&s_bld, gallivm, s_type); 549 550 /* Load current z/stencil value from z/stencil buffer */ 551 zs_dst_ptr = LLVMBuildBitCast(builder, 552 zs_dst_ptr, 553 LLVMPointerType(z_bld.vec_type, 0), ""); 554 zs_dst = LLVMBuildLoad(builder, zs_dst_ptr, ""); 555 556 lp_build_name(zs_dst, "zs_dst"); 557 558 559 /* Compute and apply the Z/stencil bitmasks and shifts. 560 */ 561 { 562 unsigned s_shift, s_mask; 563 564 if (get_z_shift_and_mask(format_desc, &z_shift, &z_width, &z_mask)) { 565 if (z_mask != 0xffffffff) { 566 z_bitmask = lp_build_const_int_vec(gallivm, z_type, z_mask); 567 } 568 569 /* 570 * Align the framebuffer Z 's LSB to the right. 571 */ 572 if (z_shift) { 573 LLVMValueRef shift = lp_build_const_int_vec(gallivm, z_type, z_shift); 574 z_dst = LLVMBuildLShr(builder, zs_dst, shift, "z_dst"); 575 } else if (z_bitmask) { 576 /* TODO: Instead of loading a mask from memory and ANDing, it's 577 * probably faster to just shake the bits with two shifts. */ 578 z_dst = LLVMBuildAnd(builder, zs_dst, z_bitmask, "z_dst"); 579 } else { 580 z_dst = zs_dst; 581 lp_build_name(z_dst, "z_dst"); 582 } 583 } 584 585 if (get_s_shift_and_mask(format_desc, &s_shift, &s_mask)) { 586 if (s_shift) { 587 LLVMValueRef shift = lp_build_const_int_vec(gallivm, s_type, s_shift); 588 stencil_vals = LLVMBuildLShr(builder, zs_dst, shift, ""); 589 stencil_shift = shift; /* used below */ 590 } 591 else { 592 stencil_vals = zs_dst; 593 } 594 595 if (s_mask != 0xffffffff) { 596 LLVMValueRef mask = lp_build_const_int_vec(gallivm, s_type, s_mask); 597 stencil_vals = LLVMBuildAnd(builder, stencil_vals, mask, ""); 598 } 599 600 lp_build_name(stencil_vals, "s_dst"); 601 } 602 } 603 604 if (stencil[0].enabled) { 605 606 if (face) { 607 LLVMValueRef zero = lp_build_const_int32(gallivm, 0); 608 609 /* front_facing = face != 0 ? ~0 : 0 */ 610 front_facing = LLVMBuildICmp(builder, LLVMIntNE, face, zero, ""); 611 front_facing = LLVMBuildSExt(builder, front_facing, 612 LLVMIntTypeInContext(gallivm->context, 613 s_bld.type.length*s_bld.type.width), 614 ""); 615 front_facing = LLVMBuildBitCast(builder, front_facing, 616 s_bld.int_vec_type, ""); 617 } 618 619 /* convert scalar stencil refs into vectors */ 620 stencil_refs[0] = lp_build_broadcast_scalar(&s_bld, stencil_refs[0]); 621 stencil_refs[1] = lp_build_broadcast_scalar(&s_bld, stencil_refs[1]); 622 623 s_pass_mask = lp_build_stencil_test(&s_bld, stencil, 624 stencil_refs, stencil_vals, 625 front_facing); 626 627 /* apply stencil-fail operator */ 628 { 629 LLVMValueRef s_fail_mask = lp_build_andnot(&s_bld, orig_mask, s_pass_mask); 630 stencil_vals = lp_build_stencil_op(&s_bld, stencil, S_FAIL_OP, 631 stencil_refs, stencil_vals, 632 s_fail_mask, front_facing); 633 } 634 } 635 636 if (depth->enabled) { 637 /* 638 * Convert fragment Z to the desired type, aligning the LSB to the right. 639 */ 640 641 assert(z_type.width == z_src_type.width); 642 assert(z_type.length == z_src_type.length); 643 assert(lp_check_value(z_src_type, z_src)); 644 if (z_src_type.floating) { 645 /* 646 * Convert from floating point values 647 */ 648 649 if (!z_type.floating) { 650 z_src = lp_build_clamped_float_to_unsigned_norm(gallivm, 651 z_src_type, 652 z_width, 653 z_src); 654 } 655 } else { 656 /* 657 * Convert from unsigned normalized values. 658 */ 659 660 assert(!z_src_type.sign); 661 assert(!z_src_type.fixed); 662 assert(z_src_type.norm); 663 assert(!z_type.floating); 664 if (z_src_type.width > z_width) { 665 LLVMValueRef shift = lp_build_const_int_vec(gallivm, z_src_type, 666 z_src_type.width - z_width); 667 z_src = LLVMBuildLShr(builder, z_src, shift, ""); 668 } 669 } 670 assert(lp_check_value(z_type, z_src)); 671 672 lp_build_name(z_src, "z_src"); 673 674 /* compare src Z to dst Z, returning 'pass' mask */ 675 z_pass = lp_build_cmp(&z_bld, depth->func, z_src, z_dst); 676 677 if (!stencil[0].enabled) { 678 /* We can potentially skip all remaining operations here, but only 679 * if stencil is disabled because we still need to update the stencil 680 * buffer values. Don't need to update Z buffer values. 681 */ 682 lp_build_mask_update(mask, z_pass); 683 684 if (do_branch) { 685 lp_build_mask_check(mask); 686 do_branch = FALSE; 687 } 688 } 689 690 if (depth->writemask) { 691 LLVMValueRef zselectmask; 692 693 /* mask off bits that failed Z test */ 694 zselectmask = LLVMBuildAnd(builder, orig_mask, z_pass, ""); 695 696 /* mask off bits that failed stencil test */ 697 if (s_pass_mask) { 698 zselectmask = LLVMBuildAnd(builder, zselectmask, s_pass_mask, ""); 699 } 700 701 /* Mix the old and new Z buffer values. 702 * z_dst[i] = zselectmask[i] ? z_src[i] : z_dst[i] 703 */ 704 z_dst = lp_build_select(&z_bld, zselectmask, z_src, z_dst); 705 } 706 707 if (stencil[0].enabled) { 708 /* update stencil buffer values according to z pass/fail result */ 709 LLVMValueRef z_fail_mask, z_pass_mask; 710 711 /* apply Z-fail operator */ 712 z_fail_mask = lp_build_andnot(&z_bld, orig_mask, z_pass); 713 stencil_vals = lp_build_stencil_op(&s_bld, stencil, Z_FAIL_OP, 714 stencil_refs, stencil_vals, 715 z_fail_mask, front_facing); 716 717 /* apply Z-pass operator */ 718 z_pass_mask = LLVMBuildAnd(z_bld.builder, orig_mask, z_pass, ""); 719 stencil_vals = lp_build_stencil_op(&s_bld, stencil, Z_PASS_OP, 720 stencil_refs, stencil_vals, 721 z_pass_mask, front_facing); 722 } 723 } 724 else { 725 /* No depth test: apply Z-pass operator to stencil buffer values which 726 * passed the stencil test. 727 */ 728 s_pass_mask = LLVMBuildAnd(s_bld.builder, orig_mask, s_pass_mask, ""); 729 stencil_vals = lp_build_stencil_op(&s_bld, stencil, Z_PASS_OP, 730 stencil_refs, stencil_vals, 731 s_pass_mask, front_facing); 732 } 733 734 /* Put Z and ztencil bits in the right place */ 735 if (z_dst && z_shift) { 736 LLVMValueRef shift = lp_build_const_int_vec(gallivm, z_type, z_shift); 737 z_dst = LLVMBuildShl(builder, z_dst, shift, ""); 738 } 739 if (stencil_vals && stencil_shift) 740 stencil_vals = LLVMBuildShl(s_bld.builder, stencil_vals, 741 stencil_shift, ""); 742 743 /* Finally, merge/store the z/stencil values */ 744 if ((depth->enabled && depth->writemask) || 745 (stencil[0].enabled && stencil[0].writemask)) { 746 747 if (z_dst && stencil_vals) 748 zs_dst = LLVMBuildOr(z_bld.builder, z_dst, stencil_vals, ""); 749 else if (z_dst) 750 zs_dst = z_dst; 751 else 752 zs_dst = stencil_vals; 753 754 *zs_value = zs_dst; 755 } 756 757 if (s_pass_mask) 758 lp_build_mask_update(mask, s_pass_mask); 759 760 if (depth->enabled && stencil[0].enabled) 761 lp_build_mask_update(mask, z_pass); 762 763 if (do_branch) 764 lp_build_mask_check(mask); 765 766} 767 768 769void 770lp_build_depth_write(LLVMBuilderRef builder, 771 const struct util_format_description *format_desc, 772 LLVMValueRef zs_dst_ptr, 773 LLVMValueRef zs_value) 774{ 775 zs_dst_ptr = LLVMBuildBitCast(builder, zs_dst_ptr, 776 LLVMPointerType(LLVMTypeOf(zs_value), 0), ""); 777 778 LLVMBuildStore(builder, zs_value, zs_dst_ptr); 779} 780 781 782void 783lp_build_deferred_depth_write(struct gallivm_state *gallivm, 784 struct lp_type z_src_type, 785 const struct util_format_description *format_desc, 786 struct lp_build_mask_context *mask, 787 LLVMValueRef zs_dst_ptr, 788 LLVMValueRef zs_value) 789{ 790 struct lp_type z_type; 791 struct lp_build_context z_bld; 792 LLVMValueRef z_dst; 793 LLVMBuilderRef builder = gallivm->builder; 794 795 /* XXX: pointlessly redo type logic: 796 */ 797 z_type = lp_depth_type(format_desc, z_src_type.width*z_src_type.length); 798 lp_build_context_init(&z_bld, gallivm, z_type); 799 800 zs_dst_ptr = LLVMBuildBitCast(builder, zs_dst_ptr, 801 LLVMPointerType(z_bld.vec_type, 0), ""); 802 803 z_dst = LLVMBuildLoad(builder, zs_dst_ptr, "zsbufval"); 804 z_dst = lp_build_select(&z_bld, lp_build_mask_value(mask), zs_value, z_dst); 805 806 LLVMBuildStore(builder, z_dst, zs_dst_ptr); 807} 808