1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
31 * Blend LLVM IR generation -- AoS layout.
33 * AoS blending is in general much slower than SoA, but there are some cases
34 * where it might be faster. In particular, if a pixel is rendered only once
35 * then the overhead of tiling and untiling will dominate over the speedup that
36 * SoA gives. So we might want to detect such cases and fallback to AoS in the
37 * future, but for now this function is here for historical/benchmarking
40 * Run lp_blend_test after any change to this file.
42 * @author Jose Fonseca <jfonseca@vmware.com>
46 #include "pipe/p_state.h"
47 #include "util/u_debug.h"
49 #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_debug.h"
56 #include "lp_bld_blend.h"
60 * We may the same values several times, so we keep them here to avoid
61 * recomputing them. Also reusing the values allows us to do simplifications
62 * that LLVM optimization passes wouldn't normally be able to do.
64 struct lp_build_blend_aos_context
66 struct lp_build_context base;
74 LLVMValueRef inv_const;
75 LLVMValueRef saturate;
77 LLVMValueRef rgb_src_factor;
78 LLVMValueRef alpha_src_factor;
79 LLVMValueRef rgb_dst_factor;
80 LLVMValueRef alpha_dst_factor;
85 lp_build_blend_factor_unswizzled(struct lp_build_blend_aos_context *bld,
90 case PIPE_BLENDFACTOR_ZERO:
91 return bld->base.zero;
92 case PIPE_BLENDFACTOR_ONE:
94 case PIPE_BLENDFACTOR_SRC_COLOR:
95 case PIPE_BLENDFACTOR_SRC_ALPHA:
97 case PIPE_BLENDFACTOR_DST_COLOR:
98 case PIPE_BLENDFACTOR_DST_ALPHA:
100 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
102 return bld->base.one;
105 bld->inv_dst = lp_build_comp(&bld->base, bld->dst);
107 bld->saturate = lp_build_min(&bld->base, bld->src, bld->inv_dst);
108 return bld->saturate;
110 case PIPE_BLENDFACTOR_CONST_COLOR:
111 case PIPE_BLENDFACTOR_CONST_ALPHA:
113 case PIPE_BLENDFACTOR_SRC1_COLOR:
114 case PIPE_BLENDFACTOR_SRC1_ALPHA:
117 return bld->base.zero;
118 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
119 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
121 bld->inv_src = lp_build_comp(&bld->base, bld->src);
123 case PIPE_BLENDFACTOR_INV_DST_COLOR:
124 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
126 bld->inv_dst = lp_build_comp(&bld->base, bld->dst);
128 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
129 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
131 bld->inv_const = lp_build_comp(&bld->base, bld->const_);
132 return bld->inv_const;
133 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
134 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
137 return bld->base.zero;
140 return bld->base.zero;
145 enum lp_build_blend_swizzle {
146 LP_BUILD_BLEND_SWIZZLE_RGBA = 0,
147 LP_BUILD_BLEND_SWIZZLE_AAAA = 1
152 * How should we shuffle the base factor.
154 static enum lp_build_blend_swizzle
155 lp_build_blend_factor_swizzle(unsigned factor)
158 case PIPE_BLENDFACTOR_ONE:
159 case PIPE_BLENDFACTOR_ZERO:
160 case PIPE_BLENDFACTOR_SRC_COLOR:
161 case PIPE_BLENDFACTOR_DST_COLOR:
162 case PIPE_BLENDFACTOR_CONST_COLOR:
163 case PIPE_BLENDFACTOR_SRC1_COLOR:
164 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
165 case PIPE_BLENDFACTOR_INV_DST_COLOR:
166 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
167 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
168 return LP_BUILD_BLEND_SWIZZLE_RGBA;
169 case PIPE_BLENDFACTOR_SRC_ALPHA:
170 case PIPE_BLENDFACTOR_DST_ALPHA:
171 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
172 case PIPE_BLENDFACTOR_SRC1_ALPHA:
173 case PIPE_BLENDFACTOR_CONST_ALPHA:
174 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
175 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
176 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
177 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
178 return LP_BUILD_BLEND_SWIZZLE_AAAA;
181 return LP_BUILD_BLEND_SWIZZLE_RGBA;
187 lp_build_blend_swizzle(struct lp_build_blend_aos_context *bld,
190 enum lp_build_blend_swizzle rgb_swizzle,
191 unsigned alpha_swizzle)
193 LLVMValueRef swizzled_rgb;
195 switch (rgb_swizzle) {
196 case LP_BUILD_BLEND_SWIZZLE_RGBA:
199 case LP_BUILD_BLEND_SWIZZLE_AAAA:
200 swizzled_rgb = lp_build_swizzle_scalar_aos(&bld->base, rgb, alpha_swizzle);
204 swizzled_rgb = bld->base.undef;
208 swizzled_rgb = lp_build_select_aos(&bld->base, 1 << alpha_swizzle,
209 alpha, swizzled_rgb);
217 * @sa http://www.opengl.org/sdk/docs/man/xhtml/glBlendFuncSeparate.xml
220 lp_build_blend_factor(struct lp_build_blend_aos_context *bld,
221 LLVMValueRef factor1,
223 unsigned alpha_factor,
224 unsigned alpha_swizzle)
226 LLVMValueRef rgb_factor_;
227 LLVMValueRef alpha_factor_;
228 LLVMValueRef factor2;
229 enum lp_build_blend_swizzle rgb_swizzle;
231 rgb_factor_ = lp_build_blend_factor_unswizzled(bld, rgb_factor, FALSE);
232 alpha_factor_ = lp_build_blend_factor_unswizzled(bld, alpha_factor, TRUE);
234 rgb_swizzle = lp_build_blend_factor_swizzle(rgb_factor);
236 factor2 = lp_build_blend_swizzle(bld, rgb_factor_, alpha_factor_, rgb_swizzle, alpha_swizzle);
238 return lp_build_mul(&bld->base, factor1, factor2);
243 * Is (a OP b) == (b OP a)?
246 lp_build_blend_func_commutative(unsigned func)
253 case PIPE_BLEND_SUBTRACT:
254 case PIPE_BLEND_REVERSE_SUBTRACT:
264 lp_build_blend_func_reverse(unsigned rgb_func, unsigned alpha_func)
266 if(rgb_func == alpha_func)
268 if(rgb_func == PIPE_BLEND_SUBTRACT && alpha_func == PIPE_BLEND_REVERSE_SUBTRACT)
270 if(rgb_func == PIPE_BLEND_REVERSE_SUBTRACT && alpha_func == PIPE_BLEND_SUBTRACT)
277 * @sa http://www.opengl.org/sdk/docs/man/xhtml/glBlendEquationSeparate.xml
280 lp_build_blend_func(struct lp_build_context *bld,
287 return lp_build_add(bld, term1, term2);
288 case PIPE_BLEND_SUBTRACT:
289 return lp_build_sub(bld, term1, term2);
290 case PIPE_BLEND_REVERSE_SUBTRACT:
291 return lp_build_sub(bld, term2, term1);
293 return lp_build_min(bld, term1, term2);
295 return lp_build_max(bld, term1, term2);
304 lp_build_blend_aos(struct gallivm_state *gallivm,
305 const struct pipe_blend_state *blend,
311 unsigned alpha_swizzle)
313 struct lp_build_blend_aos_context bld;
314 LLVMValueRef src_term;
315 LLVMValueRef dst_term;
317 /* FIXME: color masking not implemented yet */
318 assert(blend->rt[rt].colormask == 0xf);
320 if(!blend->rt[rt].blend_enable)
323 /* Setup build context */
324 memset(&bld, 0, sizeof bld);
325 lp_build_context_init(&bld.base, gallivm, type);
330 /* TODO: There are still a few optimization opportunities here. For certain
331 * combinations it is possible to reorder the operations and therefore saving
332 * some instructions. */
334 src_term = lp_build_blend_factor(&bld, src, blend->rt[rt].rgb_src_factor,
335 blend->rt[rt].alpha_src_factor, alpha_swizzle);
336 dst_term = lp_build_blend_factor(&bld, dst, blend->rt[rt].rgb_dst_factor,
337 blend->rt[rt].alpha_dst_factor, alpha_swizzle);
339 lp_build_name(src_term, "src_term");
340 lp_build_name(dst_term, "dst_term");
342 if(blend->rt[rt].rgb_func == blend->rt[rt].alpha_func) {
343 return lp_build_blend_func(&bld.base, blend->rt[rt].rgb_func, src_term, dst_term);
346 /* Seperate RGB / A functions */
351 rgb = lp_build_blend_func(&bld.base, blend->rt[rt].rgb_func, src_term, dst_term);
352 alpha = lp_build_blend_func(&bld.base, blend->rt[rt].alpha_func, src_term, dst_term);
354 return lp_build_blend_swizzle(&bld, rgb, alpha, LP_BUILD_BLEND_SWIZZLE_RGBA, alpha_swizzle);