Path: blob/21.2-virgl/src/gallium/drivers/llvmpipe/lp_bld_blend_aos.c
4570 views
/**************************************************************************1*2* Copyright 2009 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/262728/**29* @file30* Blend LLVM IR generation -- AoS layout.31*32* AoS blending is in general much slower than SoA, but there are some cases33* where it might be faster. In particular, if a pixel is rendered only once34* then the overhead of tiling and untiling will dominate over the speedup that35* SoA gives. So we might want to detect such cases and fallback to AoS in the36* future, but for now this function is here for historical/benchmarking37* purposes.38*39* Run lp_blend_test after any change to this file.40*41* @author Jose Fonseca <[email protected]>42*/434445#include "pipe/p_state.h"46#include "util/u_debug.h"47#include "util/format/u_format.h"4849#include "gallivm/lp_bld_type.h"50#include "gallivm/lp_bld_const.h"51#include "gallivm/lp_bld_arit.h"52#include "gallivm/lp_bld_logic.h"53#include "gallivm/lp_bld_swizzle.h"54#include "gallivm/lp_bld_bitarit.h"55#include "gallivm/lp_bld_debug.h"5657#include "lp_bld_blend.h"585960/**61* We may the same values several times, so we keep them here to avoid62* recomputing them. Also reusing the values allows us to do simplifications63* that LLVM optimization passes wouldn't normally be able to do.64*/65struct lp_build_blend_aos_context66{67struct lp_build_context base;6869LLVMValueRef src;70LLVMValueRef src_alpha;71LLVMValueRef src1;72LLVMValueRef src1_alpha;73LLVMValueRef dst;74LLVMValueRef const_;75LLVMValueRef const_alpha;76boolean has_dst_alpha;7778LLVMValueRef inv_src;79LLVMValueRef inv_src_alpha;80LLVMValueRef inv_dst;81LLVMValueRef inv_const;82LLVMValueRef inv_const_alpha;83LLVMValueRef saturate;8485LLVMValueRef rgb_src_factor;86LLVMValueRef alpha_src_factor;87LLVMValueRef rgb_dst_factor;88LLVMValueRef alpha_dst_factor;89};909192static LLVMValueRef93lp_build_blend_factor_unswizzled(struct lp_build_blend_aos_context *bld,94unsigned factor,95boolean alpha)96{97LLVMValueRef src_alpha = bld->src_alpha ? bld->src_alpha : bld->src;98LLVMValueRef src1_alpha = bld->src1_alpha ? bld->src1_alpha : bld->src1;99LLVMValueRef const_alpha = bld->const_alpha ? bld->const_alpha : bld->const_;100101switch (factor) {102case PIPE_BLENDFACTOR_ZERO:103return bld->base.zero;104case PIPE_BLENDFACTOR_ONE:105return bld->base.one;106case PIPE_BLENDFACTOR_SRC_COLOR:107return bld->src;108case PIPE_BLENDFACTOR_SRC_ALPHA:109return src_alpha;110case PIPE_BLENDFACTOR_DST_COLOR:111case PIPE_BLENDFACTOR_DST_ALPHA:112return bld->dst;113case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:114if (alpha)115return bld->base.one;116else {117/*118* If there's no dst alpha the complement is zero but for unclamped119* float inputs (or snorm inputs) min can be non-zero (negative).120*/121if (!bld->saturate) {122if (!bld->has_dst_alpha) {123bld->saturate = lp_build_min(&bld->base, src_alpha, bld->base.zero);124}125else if (bld->base.type.norm && bld->base.type.sign) {126/*127* The complement/min totally doesn't work, since128* the complement is in range [0,2] but the other129* min input is [-1,1]. However, we can just clamp to 0130* before doing the complement...131*/132LLVMValueRef inv_dst;133inv_dst = lp_build_max(&bld->base, bld->base.zero, bld->dst);134inv_dst = lp_build_comp(&bld->base, inv_dst);135bld->saturate = lp_build_min(&bld->base, src_alpha, inv_dst);136} else {137if (!bld->inv_dst) {138bld->inv_dst = lp_build_comp(&bld->base, bld->dst);139}140bld->saturate = lp_build_min(&bld->base, src_alpha, bld->inv_dst);141}142}143return bld->saturate;144}145case PIPE_BLENDFACTOR_CONST_COLOR:146return bld->const_;147case PIPE_BLENDFACTOR_CONST_ALPHA:148return const_alpha;149case PIPE_BLENDFACTOR_SRC1_COLOR:150return bld->src1;151case PIPE_BLENDFACTOR_SRC1_ALPHA:152return src1_alpha;153case PIPE_BLENDFACTOR_INV_SRC_COLOR:154if (!bld->inv_src)155bld->inv_src = lp_build_comp(&bld->base, bld->src);156return bld->inv_src;157case PIPE_BLENDFACTOR_INV_SRC_ALPHA:158if (!bld->inv_src_alpha)159bld->inv_src_alpha = lp_build_comp(&bld->base, src_alpha);160return bld->inv_src_alpha;161case PIPE_BLENDFACTOR_INV_DST_COLOR:162case PIPE_BLENDFACTOR_INV_DST_ALPHA:163if (!bld->inv_dst)164bld->inv_dst = lp_build_comp(&bld->base, bld->dst);165return bld->inv_dst;166case PIPE_BLENDFACTOR_INV_CONST_COLOR:167if (!bld->inv_const)168bld->inv_const = lp_build_comp(&bld->base, bld->const_);169return bld->inv_const;170case PIPE_BLENDFACTOR_INV_CONST_ALPHA:171if (!bld->inv_const_alpha)172bld->inv_const_alpha = lp_build_comp(&bld->base, const_alpha);173return bld->inv_const_alpha;174case PIPE_BLENDFACTOR_INV_SRC1_COLOR:175return lp_build_comp(&bld->base, bld->src1);176case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:177return lp_build_comp(&bld->base, src1_alpha);178default:179assert(0);180return bld->base.zero;181}182}183184185enum lp_build_blend_swizzle {186LP_BUILD_BLEND_SWIZZLE_RGBA = 0,187LP_BUILD_BLEND_SWIZZLE_AAAA = 1188};189190191/**192* How should we shuffle the base factor.193*/194static enum lp_build_blend_swizzle195lp_build_blend_factor_swizzle(unsigned factor)196{197switch (factor) {198case PIPE_BLENDFACTOR_ONE:199case PIPE_BLENDFACTOR_ZERO:200case PIPE_BLENDFACTOR_SRC_COLOR:201case PIPE_BLENDFACTOR_DST_COLOR:202case PIPE_BLENDFACTOR_CONST_COLOR:203case PIPE_BLENDFACTOR_SRC1_COLOR:204case PIPE_BLENDFACTOR_INV_SRC_COLOR:205case PIPE_BLENDFACTOR_INV_DST_COLOR:206case PIPE_BLENDFACTOR_INV_CONST_COLOR:207case PIPE_BLENDFACTOR_INV_SRC1_COLOR:208return LP_BUILD_BLEND_SWIZZLE_RGBA;209case PIPE_BLENDFACTOR_SRC_ALPHA:210case PIPE_BLENDFACTOR_DST_ALPHA:211case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:212case PIPE_BLENDFACTOR_SRC1_ALPHA:213case PIPE_BLENDFACTOR_CONST_ALPHA:214case PIPE_BLENDFACTOR_INV_SRC_ALPHA:215case PIPE_BLENDFACTOR_INV_DST_ALPHA:216case PIPE_BLENDFACTOR_INV_CONST_ALPHA:217case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:218return LP_BUILD_BLEND_SWIZZLE_AAAA;219default:220assert(0);221return LP_BUILD_BLEND_SWIZZLE_RGBA;222}223}224225226static LLVMValueRef227lp_build_blend_swizzle(struct lp_build_blend_aos_context *bld,228LLVMValueRef rgb,229LLVMValueRef alpha,230enum lp_build_blend_swizzle rgb_swizzle,231unsigned alpha_swizzle,232unsigned num_channels)233{234LLVMValueRef swizzled_rgb;235236switch (rgb_swizzle) {237case LP_BUILD_BLEND_SWIZZLE_RGBA:238swizzled_rgb = rgb;239break;240case LP_BUILD_BLEND_SWIZZLE_AAAA:241swizzled_rgb = lp_build_swizzle_scalar_aos(&bld->base, rgb, alpha_swizzle, num_channels);242break;243default:244assert(0);245swizzled_rgb = bld->base.undef;246}247248if (rgb != alpha) {249swizzled_rgb = lp_build_select_aos(&bld->base, 1 << alpha_swizzle,250alpha, swizzled_rgb,251num_channels);252}253254return swizzled_rgb;255}256257/**258* @sa http://www.opengl.org/sdk/docs/man/xhtml/glBlendFuncSeparate.xml259*/260static LLVMValueRef261lp_build_blend_factor(struct lp_build_blend_aos_context *bld,262unsigned rgb_factor,263unsigned alpha_factor,264unsigned alpha_swizzle,265unsigned num_channels)266{267LLVMValueRef rgb_factor_, alpha_factor_;268enum lp_build_blend_swizzle rgb_swizzle;269270if (alpha_swizzle == PIPE_SWIZZLE_X && num_channels == 1) {271return lp_build_blend_factor_unswizzled(bld, alpha_factor, TRUE);272}273274rgb_factor_ = lp_build_blend_factor_unswizzled(bld, rgb_factor, FALSE);275276if (alpha_swizzle != PIPE_SWIZZLE_NONE) {277rgb_swizzle = lp_build_blend_factor_swizzle(rgb_factor);278alpha_factor_ = lp_build_blend_factor_unswizzled(bld, alpha_factor, TRUE);279return lp_build_blend_swizzle(bld, rgb_factor_, alpha_factor_, rgb_swizzle,280alpha_swizzle, num_channels);281} else {282return rgb_factor_;283}284}285286287/**288* Performs blending of src and dst pixels289*290* @param blend the blend state of the shader variant291* @param cbuf_format format of the colour buffer292* @param type data type of the pixel vector293* @param rt render target index294* @param src blend src295* @param src_alpha blend src alpha (if not included in src)296* @param src1 second blend src (for dual source blend)297* @param src1_alpha second blend src alpha (if not included in src1)298* @param dst blend dst299* @param mask optional mask to apply to the blending result300* @param const_ const blend color301* @param const_alpha const blend color alpha (if not included in const_)302* @param swizzle swizzle values for RGBA303*304* @return the result of blending src and dst305*/306LLVMValueRef307lp_build_blend_aos(struct gallivm_state *gallivm,308const struct pipe_blend_state *blend,309enum pipe_format cbuf_format,310struct lp_type type,311unsigned rt,312LLVMValueRef src,313LLVMValueRef src_alpha,314LLVMValueRef src1,315LLVMValueRef src1_alpha,316LLVMValueRef dst,317LLVMValueRef mask,318LLVMValueRef const_,319LLVMValueRef const_alpha,320const unsigned char swizzle[4],321int nr_channels)322{323const struct pipe_rt_blend_state * state = &blend->rt[rt];324const struct util_format_description * desc;325struct lp_build_blend_aos_context bld;326LLVMValueRef src_factor, dst_factor;327LLVMValueRef result;328unsigned alpha_swizzle = PIPE_SWIZZLE_NONE;329unsigned i;330331desc = util_format_description(cbuf_format);332333/* Setup build context */334memset(&bld, 0, sizeof bld);335lp_build_context_init(&bld.base, gallivm, type);336bld.src = src;337bld.src1 = src1;338bld.dst = dst;339bld.const_ = const_;340bld.src_alpha = src_alpha;341bld.src1_alpha = src1_alpha;342bld.const_alpha = const_alpha;343bld.has_dst_alpha = FALSE;344345/* Find the alpha channel if not provided separately */346if (!src_alpha) {347for (i = 0; i < 4; ++i) {348if (swizzle[i] == 3) {349alpha_swizzle = i;350}351}352/*353* Note that we may get src_alpha included from source (and 4 channels)354* even if the destination doesn't have an alpha channel (for rgbx355* formats). Generally this shouldn't make much of a difference (we're356* relying on blend factors being sanitized already if there's no357* dst alpha).358*/359bld.has_dst_alpha = desc->swizzle[3] <= PIPE_SWIZZLE_W;360}361362if (blend->logicop_enable) {363if (!type.floating) {364result = lp_build_logicop(gallivm->builder, blend->logicop_func, src, dst);365}366else {367result = src;368}369} else if (!state->blend_enable) {370result = src;371} else {372boolean rgb_alpha_same = (state->rgb_src_factor == state->rgb_dst_factor &&373state->alpha_src_factor == state->alpha_dst_factor) ||374nr_channels == 1;375boolean alpha_only = nr_channels == 1 && alpha_swizzle == PIPE_SWIZZLE_X;376377src_factor = lp_build_blend_factor(&bld, state->rgb_src_factor,378state->alpha_src_factor,379alpha_swizzle,380nr_channels);381382dst_factor = lp_build_blend_factor(&bld, state->rgb_dst_factor,383state->alpha_dst_factor,384alpha_swizzle,385nr_channels);386387result = lp_build_blend(&bld.base,388state->rgb_func,389alpha_only ? state->alpha_src_factor : state->rgb_src_factor,390alpha_only ? state->alpha_dst_factor : state->rgb_dst_factor,391src,392dst,393src_factor,394dst_factor,395rgb_alpha_same,396false);397398if (state->rgb_func != state->alpha_func && nr_channels > 1 &&399alpha_swizzle != PIPE_SWIZZLE_NONE) {400LLVMValueRef alpha;401402alpha = lp_build_blend(&bld.base,403state->alpha_func,404state->alpha_src_factor,405state->alpha_dst_factor,406src,407dst,408src_factor,409dst_factor,410rgb_alpha_same,411false);412413result = lp_build_blend_swizzle(&bld,414result,415alpha,416LP_BUILD_BLEND_SWIZZLE_RGBA,417alpha_swizzle,418nr_channels);419}420}421422/* Check if color mask is necessary */423if (!util_format_colormask_full(desc, state->colormask)) {424LLVMValueRef color_mask;425426color_mask = lp_build_const_mask_aos_swizzled(gallivm, bld.base.type,427state->colormask, nr_channels, swizzle);428lp_build_name(color_mask, "color_mask");429430/* Combine with input mask if necessary */431if (mask) {432/* We can be blending floating values but masks are always integer... */433unsigned floating = bld.base.type.floating;434bld.base.type.floating = 0;435436mask = lp_build_and(&bld.base, color_mask, mask);437438bld.base.type.floating = floating;439} else {440mask = color_mask;441}442}443444/* Apply mask, if one exists */445if (mask) {446result = lp_build_select(&bld.base, mask, result, dst);447}448449return result;450}451452453