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
;
76 LLVMValueRef inv_const
;
77 LLVMValueRef saturate
;
79 LLVMValueRef rgb_src_factor
;
80 LLVMValueRef alpha_src_factor
;
81 LLVMValueRef rgb_dst_factor
;
82 LLVMValueRef alpha_dst_factor
;
87 lp_build_blend_factor_unswizzled(struct lp_build_blend_aos_context
*bld
,
92 case PIPE_BLENDFACTOR_ZERO
:
93 return bld
->base
.zero
;
94 case PIPE_BLENDFACTOR_ONE
:
96 case PIPE_BLENDFACTOR_SRC_COLOR
:
97 case PIPE_BLENDFACTOR_SRC_ALPHA
:
99 case PIPE_BLENDFACTOR_DST_COLOR
:
100 case PIPE_BLENDFACTOR_DST_ALPHA
:
102 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
104 return bld
->base
.one
;
107 bld
->inv_dst
= lp_build_comp(&bld
->base
, bld
->dst
);
109 bld
->saturate
= lp_build_min(&bld
->base
, bld
->src
, bld
->inv_dst
);
110 return bld
->saturate
;
112 case PIPE_BLENDFACTOR_CONST_COLOR
:
113 case PIPE_BLENDFACTOR_CONST_ALPHA
:
115 case PIPE_BLENDFACTOR_SRC1_COLOR
:
116 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
119 return bld
->base
.zero
;
120 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
121 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
123 bld
->inv_src
= lp_build_comp(&bld
->base
, bld
->src
);
125 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
126 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
128 bld
->inv_dst
= lp_build_comp(&bld
->base
, bld
->dst
);
130 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
131 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
133 bld
->inv_const
= lp_build_comp(&bld
->base
, bld
->const_
);
134 return bld
->inv_const
;
135 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
136 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
139 return bld
->base
.zero
;
142 return bld
->base
.zero
;
147 enum lp_build_blend_swizzle
{
148 LP_BUILD_BLEND_SWIZZLE_RGBA
= 0,
149 LP_BUILD_BLEND_SWIZZLE_AAAA
= 1
154 * How should we shuffle the base factor.
156 static enum lp_build_blend_swizzle
157 lp_build_blend_factor_swizzle(unsigned factor
)
160 case PIPE_BLENDFACTOR_ONE
:
161 case PIPE_BLENDFACTOR_ZERO
:
162 case PIPE_BLENDFACTOR_SRC_COLOR
:
163 case PIPE_BLENDFACTOR_DST_COLOR
:
164 case PIPE_BLENDFACTOR_CONST_COLOR
:
165 case PIPE_BLENDFACTOR_SRC1_COLOR
:
166 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
167 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
168 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
169 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
170 return LP_BUILD_BLEND_SWIZZLE_RGBA
;
171 case PIPE_BLENDFACTOR_SRC_ALPHA
:
172 case PIPE_BLENDFACTOR_DST_ALPHA
:
173 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
174 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
175 case PIPE_BLENDFACTOR_CONST_ALPHA
:
176 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
177 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
178 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
179 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
180 return LP_BUILD_BLEND_SWIZZLE_AAAA
;
183 return LP_BUILD_BLEND_SWIZZLE_RGBA
;
189 lp_build_blend_swizzle(struct lp_build_blend_aos_context
*bld
,
192 enum lp_build_blend_swizzle rgb_swizzle
,
193 unsigned alpha_swizzle
)
195 LLVMValueRef swizzled_rgb
;
197 switch (rgb_swizzle
) {
198 case LP_BUILD_BLEND_SWIZZLE_RGBA
:
201 case LP_BUILD_BLEND_SWIZZLE_AAAA
:
202 swizzled_rgb
= lp_build_swizzle_scalar_aos(&bld
->base
, rgb
, alpha_swizzle
);
206 swizzled_rgb
= bld
->base
.undef
;
210 swizzled_rgb
= lp_build_select_aos(&bld
->base
, 1 << alpha_swizzle
,
211 alpha
, swizzled_rgb
);
219 * @sa http://www.opengl.org/sdk/docs/man/xhtml/glBlendFuncSeparate.xml
222 lp_build_blend_factor(struct lp_build_blend_aos_context
*bld
,
224 unsigned alpha_factor
,
225 unsigned alpha_swizzle
)
227 LLVMValueRef rgb_factor_
, alpha_factor_
;
228 enum lp_build_blend_swizzle rgb_swizzle
;
230 rgb_factor_
= lp_build_blend_factor_unswizzled(bld
, rgb_factor
, FALSE
);
232 if (alpha_swizzle
!= UTIL_FORMAT_SWIZZLE_NONE
) {
233 rgb_swizzle
= lp_build_blend_factor_swizzle(rgb_factor
);
234 alpha_factor_
= lp_build_blend_factor_unswizzled(bld
, alpha_factor
, TRUE
);
235 return lp_build_blend_swizzle(bld
, rgb_factor_
, alpha_factor_
, rgb_swizzle
, alpha_swizzle
);
243 * Performs blending of src and dst pixels
245 * @param blend the blend state of the shader variant
246 * @param cbuf_format format of the colour buffer
247 * @param type data type of the pixel vector
248 * @param rt rt number
249 * @param src blend src
250 * @param dst blend dst
251 * @param mask optional mask to apply to the blending result
252 * @param const_ const blend color
253 * @param swizzle swizzle values for RGBA
255 * @return the result of blending src and dst
258 lp_build_blend_aos(struct gallivm_state
*gallivm
,
259 const struct pipe_blend_state
*blend
,
260 const enum pipe_format
*cbuf_format
,
267 const unsigned char swizzle
[4])
269 const struct pipe_rt_blend_state
* state
= &blend
->rt
[rt
];
270 const struct util_format_description
* desc
;
271 struct lp_build_blend_aos_context bld
;
272 LLVMValueRef src_factor
, dst_factor
;
274 unsigned alpha_swizzle
= swizzle
[3];
275 boolean fullcolormask
;
277 desc
= util_format_description(cbuf_format
[rt
]);
279 /* Setup build context */
280 memset(&bld
, 0, sizeof bld
);
281 lp_build_context_init(&bld
.base
, gallivm
, type
);
286 if (swizzle
[3] > UTIL_FORMAT_SWIZZLE_W
|| swizzle
[3] == swizzle
[0])
287 alpha_swizzle
= UTIL_FORMAT_SWIZZLE_NONE
;
289 if (!state
->blend_enable
) {
292 boolean rgb_alpha_same
= state
->rgb_src_factor
== state
->rgb_dst_factor
&& state
->alpha_src_factor
== state
->alpha_dst_factor
;
293 assert(rgb_alpha_same
|| alpha_swizzle
!= UTIL_FORMAT_SWIZZLE_NONE
);
295 src_factor
= lp_build_blend_factor(&bld
, state
->rgb_src_factor
,
296 state
->alpha_src_factor
, alpha_swizzle
);
297 dst_factor
= lp_build_blend_factor(&bld
, state
->rgb_dst_factor
,
298 state
->alpha_dst_factor
, alpha_swizzle
);
300 result
= lp_build_blend(&bld
.base
,
302 state
->rgb_src_factor
,
303 state
->rgb_dst_factor
,
311 if(state
->rgb_func
!= state
->alpha_func
&& alpha_swizzle
!= UTIL_FORMAT_SWIZZLE_NONE
) {
314 alpha
= lp_build_blend(&bld
.base
,
316 state
->alpha_src_factor
,
317 state
->alpha_dst_factor
,
325 result
= lp_build_blend_swizzle(&bld
,
328 LP_BUILD_BLEND_SWIZZLE_RGBA
,
333 /* Check if color mask is necessary */
334 fullcolormask
= util_format_colormask_full(util_format_description(cbuf_format
[rt
]), state
->colormask
);
336 if (!fullcolormask
) {
337 LLVMValueRef color_mask
;
339 color_mask
= lp_build_const_mask_aos_swizzled(gallivm
, bld
.base
.type
, state
.colormask
, desc
->nr_channels
, swizzle
);
340 lp_build_name(color_mask
, "color_mask");
342 /* Combine with input mask if necessary */
344 mask
= lp_build_and(&bld
.base
, color_mask
, mask
);
350 /* Apply mask, if one exists */
352 result
= lp_build_select(&bld
.base
, mask
, result
, dst
);