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.
33 * This code is generic -- it should be able to cope both with floating point
34 * and integer inputs in AOS form.
36 * @author Jose Fonseca <jfonseca@vmware.com>
40 #include "pipe/p_state.h"
43 #include "lp_bld_type.h"
44 #include "lp_bld_const.h"
45 #include "lp_bld_arit.h"
46 #include "lp_bld_swizzle.h"
50 * We may the same values several times, so we keep them here to avoid
51 * recomputing them. Also reusing the values allows us to do simplifications
52 * that LLVM optimization passes wouldn't normally be able to do.
54 struct lp_build_blend_context
56 struct lp_build_context base
;
64 LLVMValueRef inv_const
;
65 LLVMValueRef saturate
;
67 LLVMValueRef rgb_src_factor
;
68 LLVMValueRef alpha_src_factor
;
69 LLVMValueRef rgb_dst_factor
;
70 LLVMValueRef alpha_dst_factor
;
75 lp_build_blend_factor_unswizzled(struct lp_build_blend_context
*bld
,
80 case PIPE_BLENDFACTOR_ZERO
:
81 return bld
->base
.zero
;
82 case PIPE_BLENDFACTOR_ONE
:
84 case PIPE_BLENDFACTOR_SRC_COLOR
:
85 case PIPE_BLENDFACTOR_SRC_ALPHA
:
87 case PIPE_BLENDFACTOR_DST_COLOR
:
88 case PIPE_BLENDFACTOR_DST_ALPHA
:
90 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
95 bld
->inv_dst
= lp_build_comp(&bld
->base
, bld
->dst
);
97 bld
->saturate
= lp_build_min(&bld
->base
, bld
->src
, bld
->inv_dst
);
100 case PIPE_BLENDFACTOR_CONST_COLOR
:
101 case PIPE_BLENDFACTOR_CONST_ALPHA
:
103 case PIPE_BLENDFACTOR_SRC1_COLOR
:
104 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
107 return bld
->base
.zero
;
108 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
109 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
111 bld
->inv_src
= lp_build_comp(&bld
->base
, bld
->src
);
113 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
114 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
116 bld
->inv_dst
= lp_build_comp(&bld
->base
, bld
->dst
);
118 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
119 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
121 bld
->inv_const
= lp_build_comp(&bld
->base
, bld
->const_
);
122 return bld
->inv_const
;
123 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
124 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
127 return bld
->base
.zero
;
130 return bld
->base
.zero
;
135 enum lp_build_blend_swizzle
{
136 LP_BUILD_BLEND_SWIZZLE_RGBA
= 0,
137 LP_BUILD_BLEND_SWIZZLE_AAAA
= 1,
142 * How should we shuffle the base factor.
144 static enum lp_build_blend_swizzle
145 lp_build_blend_factor_swizzle(unsigned factor
)
148 case PIPE_BLENDFACTOR_ONE
:
149 case PIPE_BLENDFACTOR_ZERO
:
150 case PIPE_BLENDFACTOR_SRC_COLOR
:
151 case PIPE_BLENDFACTOR_DST_COLOR
:
152 case PIPE_BLENDFACTOR_CONST_COLOR
:
153 case PIPE_BLENDFACTOR_SRC1_COLOR
:
154 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
155 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
156 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
157 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
158 return LP_BUILD_BLEND_SWIZZLE_RGBA
;
159 case PIPE_BLENDFACTOR_SRC_ALPHA
:
160 case PIPE_BLENDFACTOR_DST_ALPHA
:
161 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
162 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
163 case PIPE_BLENDFACTOR_CONST_ALPHA
:
164 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
165 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
166 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
167 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
168 return LP_BUILD_BLEND_SWIZZLE_AAAA
;
171 return LP_BUILD_BLEND_SWIZZLE_RGBA
;
177 lp_build_blend_swizzle(struct lp_build_blend_context
*bld
,
180 enum lp_build_blend_swizzle rgb_swizzle
,
181 unsigned alpha_swizzle
)
184 if(rgb_swizzle
== LP_BUILD_BLEND_SWIZZLE_RGBA
)
186 if(rgb_swizzle
== LP_BUILD_BLEND_SWIZZLE_AAAA
)
187 return lp_build_broadcast_aos(&bld
->base
, rgb
, alpha_swizzle
);
190 if(rgb_swizzle
== LP_BUILD_BLEND_SWIZZLE_RGBA
) {
191 boolean cond
[4] = {0, 0, 0, 0};
192 cond
[alpha_swizzle
] = 1;
193 return lp_build_select_aos(&bld
->base
, alpha
, rgb
, cond
);
195 if(rgb_swizzle
== LP_BUILD_BLEND_SWIZZLE_AAAA
) {
196 unsigned char swizzle
[4];
197 swizzle
[0] = alpha_swizzle
;
198 swizzle
[1] = alpha_swizzle
;
199 swizzle
[2] = alpha_swizzle
;
200 swizzle
[3] = alpha_swizzle
;
201 swizzle
[alpha_swizzle
] += 4;
202 return lp_build_swizzle2_aos(&bld
->base
, rgb
, alpha
, swizzle
);
206 return bld
->base
.undef
;
211 * @sa http://www.opengl.org/sdk/docs/man/xhtml/glBlendFuncSeparate.xml
214 lp_build_blend_factor(struct lp_build_blend_context
*bld
,
215 LLVMValueRef factor1
,
217 unsigned alpha_factor
,
218 unsigned alpha_swizzle
)
220 LLVMValueRef rgb_factor_
;
221 LLVMValueRef alpha_factor_
;
222 LLVMValueRef factor2
;
223 enum lp_build_blend_swizzle rgb_swizzle
;
225 rgb_factor_
= lp_build_blend_factor_unswizzled(bld
, rgb_factor
, FALSE
);
226 alpha_factor_
= lp_build_blend_factor_unswizzled(bld
, alpha_factor
, TRUE
);
228 rgb_swizzle
= lp_build_blend_factor_swizzle(rgb_factor
);
230 factor2
= lp_build_blend_swizzle(bld
, rgb_factor_
, alpha_factor_
, rgb_swizzle
, alpha_swizzle
);
232 return lp_build_mul(&bld
->base
, factor1
, factor2
);
237 * @sa http://www.opengl.org/sdk/docs/man/xhtml/glBlendEquationSeparate.xml
240 lp_build_blend_func(struct lp_build_blend_context
*bld
,
247 return lp_build_add(&bld
->base
, term1
, term2
);
249 case PIPE_BLEND_SUBTRACT
:
250 return lp_build_sub(&bld
->base
, term1
, term2
);
251 case PIPE_BLEND_REVERSE_SUBTRACT
:
252 return lp_build_sub(&bld
->base
, term2
, term1
);
254 return lp_build_min(&bld
->base
, term1
, term2
);
256 return lp_build_max(&bld
->base
, term1
, term2
);
259 return bld
->base
.zero
;
265 lp_build_blend(LLVMBuilderRef builder
,
266 const struct pipe_blend_state
*blend
,
271 unsigned alpha_swizzle
)
273 struct lp_build_blend_context bld
;
274 LLVMValueRef src_term
;
275 LLVMValueRef dst_term
;
277 /* It makes no sense to blend unless values are normalized */
280 /* Setup build context */
281 memset(&bld
, 0, sizeof bld
);
282 bld
.base
.builder
= builder
;
283 bld
.base
.type
= type
;
284 bld
.base
.undef
= lp_build_undef(type
);
285 bld
.base
.zero
= lp_build_zero(type
);
286 bld
.base
.one
= lp_build_one(type
);
291 /* TODO: There are still a few optimization oportunities here. For certain
292 * combinations it is possible to reorder the operations and therefor saving
293 * some instructions. */
295 src_term
= lp_build_blend_factor(&bld
, src
, blend
->rgb_src_factor
, blend
->alpha_src_factor
, alpha_swizzle
);
296 dst_term
= lp_build_blend_factor(&bld
, dst
, blend
->rgb_dst_factor
, blend
->alpha_dst_factor
, alpha_swizzle
);
299 LLVMSetValueName(src_term
, "src_term");
300 LLVMSetValueName(dst_term
, "dst_term");
303 if(blend
->rgb_func
== blend
->alpha_func
) {
304 return lp_build_blend_func(&bld
, blend
->rgb_func
, src_term
, dst_term
);
307 /* Seperate RGB / A functions */
312 rgb
= lp_build_blend_func(&bld
, blend
->rgb_func
, src_term
, dst_term
);
313 alpha
= lp_build_blend_func(&bld
, blend
->alpha_func
, src_term
, dst_term
);
315 return lp_build_blend_swizzle(&bld
, rgb
, alpha
, LP_BUILD_BLEND_SWIZZLE_RGBA
, alpha_swizzle
);