19ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca/**************************************************************************
29ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca *
39ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca * Copyright 2009 VMware, Inc.
49ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca * All Rights Reserved.
59ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca *
69ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca * Permission is hereby granted, free of charge, to any person obtaining a
79ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca * copy of this software and associated documentation files (the
89ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca * "Software"), to deal in the Software without restriction, including
99ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca * without limitation the rights to use, copy, modify, merge, publish,
109ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca * distribute, sub license, and/or sell copies of the Software, and to
119ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca * permit persons to whom the Software is furnished to do so, subject to
129ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca * the following conditions:
139ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca *
149ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca * The above copyright notice and this permission notice (including the
159ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca * next paragraph) shall be included in all copies or substantial portions
169ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca * of the Software.
179ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca *
189ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
199ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
209ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
219ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
229ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
239ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
249ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
259ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca *
269ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca **************************************************************************/
279ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca
289ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca/**
299ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca * Alpha testing to LLVM IR translation.
309ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca *
319ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca * @author Jose Fonseca <jfonseca@vmware.com>
329ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca */
339ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca
349ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca#include "pipe/p_state.h"
357a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca#include "util/u_format.h"
369ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca
377a05161278531db97212c704a2f0258adb7d3324Brian Paul#include "gallivm/lp_bld_type.h"
387a05161278531db97212c704a2f0258adb7d3324Brian Paul#include "gallivm/lp_bld_const.h"
397a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca#include "gallivm/lp_bld_arit.h"
407a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca#include "gallivm/lp_bld_conv.h"
417a05161278531db97212c704a2f0258adb7d3324Brian Paul#include "gallivm/lp_bld_logic.h"
427a05161278531db97212c704a2f0258adb7d3324Brian Paul#include "gallivm/lp_bld_flow.h"
437a05161278531db97212c704a2f0258adb7d3324Brian Paul#include "gallivm/lp_bld_debug.h"
447a05161278531db97212c704a2f0258adb7d3324Brian Paul
459ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca#include "lp_bld_alpha.h"
469ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca
479ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca
48c0472f9c34da78bccecb2c790b54b9dd9712a0b9José Fonsecavoid
49efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paullp_build_alpha_test(struct gallivm_state *gallivm,
50277d768d10d8a98a7cae8b709ff78bb8d1f695f1José Fonseca                    unsigned func,
51b4835ea03d64261da5a892f9590c9977b06920e8José Fonseca                    struct lp_type type,
527a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca                    const struct util_format_description *cbuf_format_desc,
533d7a88674f9eb3320eeff511968f041426e25023José Fonseca                    struct lp_build_mask_context *mask,
54635c37e118bbf51fc8c93a2c999281ee817a93e1José Fonseca                    LLVMValueRef alpha,
55aa4cb5e2d8d48c7dcc9653c61a9e25494e3e7b2aKeith Whitwell                    LLVMValueRef ref,
56aa4cb5e2d8d48c7dcc9653c61a9e25494e3e7b2aKeith Whitwell                    boolean do_branch)
579ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca{
589ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca   struct lp_build_context bld;
59277d768d10d8a98a7cae8b709ff78bb8d1f695f1José Fonseca   LLVMValueRef test;
609ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca
61efc82aef35a2aac5d2ed9774f6d28f2626796416Brian Paul   lp_build_context_init(&bld, gallivm, type);
629ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca
637a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca   /*
647a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca    * Alpha testing needs to be done in the color buffer precision.
657a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca    *
667a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca    * TODO: Ideally, instead of duplicating the color conversion code, we would do
677a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca    * alpha testing after converting the output colors, but that's not very
687a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca    * convenient, because it needs to be done before depth testing.  Hopefully
697a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca    * LLVM will detect and remove the duplicate expression.
707a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca    *
717a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca    * FIXME: This should be generalized to formats other than rgba8 variants.
727a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca    */
737a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca   if (type.floating &&
747a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca       util_format_is_rgba8_variant(cbuf_format_desc)) {
757a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca      const unsigned dst_width = 8;
767a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca
777a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca      alpha = lp_build_clamp(&bld, alpha, bld.zero, bld.one);
787a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca      ref   = lp_build_clamp(&bld, ref,   bld.zero, bld.one);
797a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca
807a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca      alpha = lp_build_clamped_float_to_unsigned_norm(gallivm, type, dst_width, alpha);
817a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca      ref   = lp_build_clamped_float_to_unsigned_norm(gallivm, type, dst_width, ref);
827a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca
837a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca      type.floating = 0;
847a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca      lp_build_context_init(&bld, gallivm, type);
857a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca   }
867a75e7d6e85d27e102ff7e15583c33b1ce282fe4José Fonseca
87277d768d10d8a98a7cae8b709ff78bb8d1f695f1José Fonseca   test = lp_build_cmp(&bld, func, alpha, ref);
889ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca
89277d768d10d8a98a7cae8b709ff78bb8d1f695f1José Fonseca   lp_build_name(test, "alpha_mask");
909ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca
91277d768d10d8a98a7cae8b709ff78bb8d1f695f1José Fonseca   lp_build_mask_update(mask, test);
92aa4cb5e2d8d48c7dcc9653c61a9e25494e3e7b2aKeith Whitwell
93aa4cb5e2d8d48c7dcc9653c61a9e25494e3e7b2aKeith Whitwell   if (do_branch)
94aa4cb5e2d8d48c7dcc9653c61a9e25494e3e7b2aKeith Whitwell      lp_build_mask_check(mask);
959ae47069b4a2b67e381b02d805f1ca74f31ea7b8José Fonseca}
96