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"
48 #include "util/u_format.h"
50 #include "gallivm/lp_bld_type.h"
51 #include "gallivm/lp_bld_const.h"
52 #include "gallivm/lp_bld_arit.h"
53 #include "gallivm/lp_bld_logic.h"
54 #include "gallivm/lp_bld_swizzle.h"
55 #include "gallivm/lp_bld_bitarit.h"
56 #include "gallivm/lp_bld_debug.h"
58 #include "lp_bld_blend.h"
62 * We may the same values several times, so we keep them here to avoid
63 * recomputing them. Also reusing the values allows us to do simplifications
64 * that LLVM optimization passes wouldn't normally be able to do.
66 struct lp_build_blend_aos_context
68 struct lp_build_context base
;
71 LLVMValueRef src_alpha
;
73 LLVMValueRef src1_alpha
;
76 LLVMValueRef const_alpha
;
79 LLVMValueRef inv_src_alpha
;
81 LLVMValueRef inv_const
;
82 LLVMValueRef inv_const_alpha
;
83 LLVMValueRef saturate
;
85 LLVMValueRef rgb_src_factor
;
86 LLVMValueRef alpha_src_factor
;
87 LLVMValueRef rgb_dst_factor
;
88 LLVMValueRef alpha_dst_factor
;
93 lp_build_blend_factor_unswizzled(struct lp_build_blend_aos_context
*bld
,
97 LLVMValueRef src_alpha
= bld
->src_alpha
? bld
->src_alpha
: bld
->src
;
98 LLVMValueRef src1_alpha
= bld
->src1_alpha
? bld
->src1_alpha
: bld
->src1
;
99 LLVMValueRef const_alpha
= bld
->const_alpha
? bld
->const_alpha
: bld
->const_
;
102 case PIPE_BLENDFACTOR_ZERO
:
103 return bld
->base
.zero
;
104 case PIPE_BLENDFACTOR_ONE
:
105 return bld
->base
.one
;
106 case PIPE_BLENDFACTOR_SRC_COLOR
:
108 case PIPE_BLENDFACTOR_SRC_ALPHA
:
110 case PIPE_BLENDFACTOR_DST_COLOR
:
111 case PIPE_BLENDFACTOR_DST_ALPHA
:
113 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
115 return bld
->base
.one
;
118 bld
->inv_dst
= lp_build_comp(&bld
->base
, bld
->dst
);
120 bld
->saturate
= lp_build_min(&bld
->base
, src_alpha
, bld
->inv_dst
);
121 return bld
->saturate
;
123 case PIPE_BLENDFACTOR_CONST_COLOR
:
125 case PIPE_BLENDFACTOR_CONST_ALPHA
:
127 case PIPE_BLENDFACTOR_SRC1_COLOR
:
129 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
131 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
133 bld
->inv_src
= lp_build_comp(&bld
->base
, bld
->src
);
135 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
136 if(!bld
->inv_src_alpha
)
137 bld
->inv_src_alpha
= lp_build_comp(&bld
->base
, src_alpha
);
138 return bld
->inv_src_alpha
;
139 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
140 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
142 bld
->inv_dst
= lp_build_comp(&bld
->base
, bld
->dst
);
144 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
146 bld
->inv_const
= lp_build_comp(&bld
->base
, bld
->const_
);
147 return bld
->inv_const
;
148 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
149 if(!bld
->inv_const_alpha
)
150 bld
->inv_const_alpha
= lp_build_comp(&bld
->base
, const_alpha
);
151 return bld
->inv_const_alpha
;
152 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
153 return lp_build_comp(&bld
->base
, bld
->src1
);
154 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
155 return lp_build_comp(&bld
->base
, src1_alpha
);
158 return bld
->base
.zero
;
163 enum lp_build_blend_swizzle
{
164 LP_BUILD_BLEND_SWIZZLE_RGBA
= 0,
165 LP_BUILD_BLEND_SWIZZLE_AAAA
= 1
170 * How should we shuffle the base factor.
172 static enum lp_build_blend_swizzle
173 lp_build_blend_factor_swizzle(unsigned factor
)
176 case PIPE_BLENDFACTOR_ONE
:
177 case PIPE_BLENDFACTOR_ZERO
:
178 case PIPE_BLENDFACTOR_SRC_COLOR
:
179 case PIPE_BLENDFACTOR_DST_COLOR
:
180 case PIPE_BLENDFACTOR_CONST_COLOR
:
181 case PIPE_BLENDFACTOR_SRC1_COLOR
:
182 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
183 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
184 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
185 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
186 return LP_BUILD_BLEND_SWIZZLE_RGBA
;
187 case PIPE_BLENDFACTOR_SRC_ALPHA
:
188 case PIPE_BLENDFACTOR_DST_ALPHA
:
189 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
190 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
191 case PIPE_BLENDFACTOR_CONST_ALPHA
:
192 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
193 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
194 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
195 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
196 return LP_BUILD_BLEND_SWIZZLE_AAAA
;
199 return LP_BUILD_BLEND_SWIZZLE_RGBA
;
205 lp_build_blend_swizzle(struct lp_build_blend_aos_context
*bld
,
208 enum lp_build_blend_swizzle rgb_swizzle
,
209 unsigned alpha_swizzle
,
210 unsigned num_channels
)
212 LLVMValueRef swizzled_rgb
;
214 switch (rgb_swizzle
) {
215 case LP_BUILD_BLEND_SWIZZLE_RGBA
:
218 case LP_BUILD_BLEND_SWIZZLE_AAAA
:
219 swizzled_rgb
= lp_build_swizzle_scalar_aos(&bld
->base
, rgb
, alpha_swizzle
, num_channels
);
223 swizzled_rgb
= bld
->base
.undef
;
227 swizzled_rgb
= lp_build_select_aos(&bld
->base
, 1 << alpha_swizzle
,
236 * @sa http://www.opengl.org/sdk/docs/man/xhtml/glBlendFuncSeparate.xml
239 lp_build_blend_factor(struct lp_build_blend_aos_context
*bld
,
241 unsigned alpha_factor
,
242 unsigned alpha_swizzle
,
243 unsigned num_channels
)
245 LLVMValueRef rgb_factor_
, alpha_factor_
;
246 enum lp_build_blend_swizzle rgb_swizzle
;
248 if (alpha_swizzle
== 0) {
249 return lp_build_blend_factor_unswizzled(bld
, alpha_factor
, TRUE
);
252 rgb_factor_
= lp_build_blend_factor_unswizzled(bld
, rgb_factor
, FALSE
);
254 if (alpha_swizzle
!= UTIL_FORMAT_SWIZZLE_NONE
) {
255 rgb_swizzle
= lp_build_blend_factor_swizzle(rgb_factor
);
256 alpha_factor_
= lp_build_blend_factor_unswizzled(bld
, alpha_factor
, TRUE
);
257 return lp_build_blend_swizzle(bld
, rgb_factor_
, alpha_factor_
, rgb_swizzle
, alpha_swizzle
, num_channels
);
265 * Performs blending of src and dst pixels
267 * @param blend the blend state of the shader variant
268 * @param cbuf_format format of the colour buffer
269 * @param type data type of the pixel vector
270 * @param rt render target index
271 * @param src blend src
272 * @param src_alpha blend src alpha (if not included in src)
273 * @param src1 second blend src (for dual source blend)
274 * @param src1_alpha second blend src alpha (if not included in src1)
275 * @param dst blend dst
276 * @param mask optional mask to apply to the blending result
277 * @param const_ const blend color
278 * @param const_alpha const blend color alpha (if not included in const_)
279 * @param swizzle swizzle values for RGBA
281 * @return the result of blending src and dst
284 lp_build_blend_aos(struct gallivm_state
*gallivm
,
285 const struct pipe_blend_state
*blend
,
286 enum pipe_format cbuf_format
,
290 LLVMValueRef src_alpha
,
292 LLVMValueRef src1_alpha
,
296 LLVMValueRef const_alpha
,
297 const unsigned char swizzle
[4],
300 const struct pipe_rt_blend_state
* state
= &blend
->rt
[rt
];
301 const struct util_format_description
* desc
;
302 struct lp_build_blend_aos_context bld
;
303 LLVMValueRef src_factor
, dst_factor
;
305 unsigned alpha_swizzle
= UTIL_FORMAT_SWIZZLE_NONE
;
308 desc
= util_format_description(cbuf_format
);
310 /* Setup build context */
311 memset(&bld
, 0, sizeof bld
);
312 lp_build_context_init(&bld
.base
, gallivm
, type
);
317 bld
.src_alpha
= src_alpha
;
318 bld
.src1_alpha
= src1_alpha
;
319 bld
.const_alpha
= const_alpha
;
321 /* Find the alpha channel if not provided seperately */
323 for (i
= 0; i
< 4; ++i
) {
324 if (swizzle
[i
] == 3) {
330 if (blend
->logicop_enable
) {
332 result
= lp_build_logicop(gallivm
->builder
, blend
->logicop_func
, src
, dst
);
337 } else if (!state
->blend_enable
) {
340 boolean rgb_alpha_same
= (state
->rgb_src_factor
== state
->rgb_dst_factor
&& state
->alpha_src_factor
== state
->alpha_dst_factor
) || nr_channels
== 1;
342 src_factor
= lp_build_blend_factor(&bld
, state
->rgb_src_factor
,
343 state
->alpha_src_factor
,
347 dst_factor
= lp_build_blend_factor(&bld
, state
->rgb_dst_factor
,
348 state
->alpha_dst_factor
,
352 result
= lp_build_blend(&bld
.base
,
354 state
->rgb_src_factor
,
355 state
->rgb_dst_factor
,
363 if(state
->rgb_func
!= state
->alpha_func
&& nr_channels
> 1 && alpha_swizzle
!= UTIL_FORMAT_SWIZZLE_NONE
) {
366 alpha
= lp_build_blend(&bld
.base
,
368 state
->alpha_src_factor
,
369 state
->alpha_dst_factor
,
377 result
= lp_build_blend_swizzle(&bld
,
380 LP_BUILD_BLEND_SWIZZLE_RGBA
,
386 /* Check if color mask is necessary */
387 if (!util_format_colormask_full(desc
, state
->colormask
)) {
388 LLVMValueRef color_mask
;
390 color_mask
= lp_build_const_mask_aos_swizzled(gallivm
, bld
.base
.type
, state
->colormask
, nr_channels
, swizzle
);
391 lp_build_name(color_mask
, "color_mask");
393 /* Combine with input mask if necessary */
395 /* We can be blending floating values but masks are always integer... */
396 unsigned floating
= bld
.base
.type
.floating
;
397 bld
.base
.type
.floating
= 0;
399 mask
= lp_build_and(&bld
.base
, color_mask
, mask
);
401 bld
.base
.type
.floating
= floating
;
407 /* Apply mask, if one exists */
409 result
= lp_build_select(&bld
.base
, mask
, result
, dst
);