e070aac378df334049fe4ec2565746ff95e32235
[mesa.git] / src / gallium / drivers / llvmpipe / lp_bld_blend.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 /**
30 * @file
31 * Blend LLVM IR generation.
32 *
33 * This code is generic -- it should be able to cope both with floating point
34 * and integer inputs in AOS form.
35 *
36 * @author Jose Fonseca <jfonseca@vmware.com>
37 */
38
39
40 #include "pipe/p_state.h"
41
42 #include "lp_bld.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"
47
48
49 /**
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.
53 */
54 struct lp_build_blend_context
55 {
56 struct lp_build_context base;
57
58 LLVMValueRef src;
59 LLVMValueRef dst;
60 LLVMValueRef const_;
61
62 LLVMValueRef inv_src;
63 LLVMValueRef inv_dst;
64 LLVMValueRef inv_const;
65 LLVMValueRef saturate;
66
67 LLVMValueRef rgb_src_factor;
68 LLVMValueRef alpha_src_factor;
69 LLVMValueRef rgb_dst_factor;
70 LLVMValueRef alpha_dst_factor;
71 };
72
73
74 static LLVMValueRef
75 lp_build_blend_factor_unswizzled(struct lp_build_blend_context *bld,
76 unsigned factor,
77 boolean alpha)
78 {
79 switch (factor) {
80 case PIPE_BLENDFACTOR_ZERO:
81 return bld->base.zero;
82 case PIPE_BLENDFACTOR_ONE:
83 return bld->base.one;
84 case PIPE_BLENDFACTOR_SRC_COLOR:
85 case PIPE_BLENDFACTOR_SRC_ALPHA:
86 return bld->src;
87 case PIPE_BLENDFACTOR_DST_COLOR:
88 case PIPE_BLENDFACTOR_DST_ALPHA:
89 return bld->dst;
90 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
91 if(alpha)
92 return bld->base.one;
93 else {
94 if(!bld->inv_dst)
95 bld->inv_dst = lp_build_comp(&bld->base, bld->dst);
96 if(!bld->saturate)
97 bld->saturate = lp_build_min(&bld->base, bld->src, bld->inv_dst);
98 return bld->saturate;
99 }
100 case PIPE_BLENDFACTOR_CONST_COLOR:
101 case PIPE_BLENDFACTOR_CONST_ALPHA:
102 return bld->const_;
103 case PIPE_BLENDFACTOR_SRC1_COLOR:
104 case PIPE_BLENDFACTOR_SRC1_ALPHA:
105 /* TODO */
106 assert(0);
107 return bld->base.zero;
108 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
109 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
110 if(!bld->inv_src)
111 bld->inv_src = lp_build_comp(&bld->base, bld->src);
112 return bld->inv_src;
113 case PIPE_BLENDFACTOR_INV_DST_COLOR:
114 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
115 if(!bld->inv_dst)
116 bld->inv_dst = lp_build_comp(&bld->base, bld->dst);
117 return bld->inv_dst;
118 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
119 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
120 if(!bld->inv_const)
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:
125 /* TODO */
126 assert(0);
127 return bld->base.zero;
128 default:
129 assert(0);
130 return bld->base.zero;
131 }
132 }
133
134
135 enum lp_build_blend_swizzle {
136 LP_BUILD_BLEND_SWIZZLE_RGBA = 0,
137 LP_BUILD_BLEND_SWIZZLE_AAAA = 1,
138 };
139
140
141 /**
142 * How should we shuffle the base factor.
143 */
144 static enum lp_build_blend_swizzle
145 lp_build_blend_factor_swizzle(unsigned factor)
146 {
147 switch (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;
169 default:
170 assert(0);
171 return LP_BUILD_BLEND_SWIZZLE_RGBA;
172 }
173 }
174
175
176 static LLVMValueRef
177 lp_build_blend_swizzle(struct lp_build_blend_context *bld,
178 LLVMValueRef rgb,
179 LLVMValueRef alpha,
180 enum lp_build_blend_swizzle rgb_swizzle,
181 unsigned alpha_swizzle)
182 {
183 if(rgb == alpha) {
184 if(rgb_swizzle == LP_BUILD_BLEND_SWIZZLE_RGBA)
185 return rgb;
186 if(rgb_swizzle == LP_BUILD_BLEND_SWIZZLE_AAAA)
187 return lp_build_broadcast_aos(&bld->base, rgb, alpha_swizzle);
188 }
189 else {
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);
194 }
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);
203 }
204 }
205 assert(0);
206 return bld->base.undef;
207 }
208
209
210 /**
211 * @sa http://www.opengl.org/sdk/docs/man/xhtml/glBlendFuncSeparate.xml
212 */
213 static LLVMValueRef
214 lp_build_blend_factor(struct lp_build_blend_context *bld,
215 LLVMValueRef factor1,
216 unsigned rgb_factor,
217 unsigned alpha_factor,
218 unsigned alpha_swizzle)
219 {
220 LLVMValueRef rgb_factor_;
221 LLVMValueRef alpha_factor_;
222 LLVMValueRef factor2;
223 enum lp_build_blend_swizzle rgb_swizzle;
224
225 rgb_factor_ = lp_build_blend_factor_unswizzled(bld, rgb_factor, FALSE);
226 alpha_factor_ = lp_build_blend_factor_unswizzled(bld, alpha_factor, TRUE);
227
228 rgb_swizzle = lp_build_blend_factor_swizzle(rgb_factor);
229
230 factor2 = lp_build_blend_swizzle(bld, rgb_factor_, alpha_factor_, rgb_swizzle, alpha_swizzle);
231
232 return lp_build_mul(&bld->base, factor1, factor2);
233 }
234
235
236 /**
237 * @sa http://www.opengl.org/sdk/docs/man/xhtml/glBlendEquationSeparate.xml
238 */
239 static LLVMValueRef
240 lp_build_blend_func(struct lp_build_blend_context *bld,
241 unsigned func,
242 LLVMValueRef term1,
243 LLVMValueRef term2)
244 {
245 switch (func) {
246 case PIPE_BLEND_ADD:
247 return lp_build_add(&bld->base, term1, term2);
248 break;
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);
253 case PIPE_BLEND_MIN:
254 return lp_build_min(&bld->base, term1, term2);
255 case PIPE_BLEND_MAX:
256 return lp_build_max(&bld->base, term1, term2);
257 default:
258 assert(0);
259 return bld->base.zero;
260 }
261 }
262
263
264 LLVMValueRef
265 lp_build_blend(LLVMBuilderRef builder,
266 const struct pipe_blend_state *blend,
267 union lp_type type,
268 LLVMValueRef src,
269 LLVMValueRef dst,
270 LLVMValueRef const_,
271 unsigned alpha_swizzle)
272 {
273 struct lp_build_blend_context bld;
274 LLVMValueRef src_term;
275 LLVMValueRef dst_term;
276
277 /* It makes no sense to blend unless values are normalized */
278 assert(type.norm);
279
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);
287 bld.src = src;
288 bld.dst = dst;
289 bld.const_ = const_;
290
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. */
294
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);
297
298 #ifdef DEBUG
299 LLVMSetValueName(src_term, "src_term");
300 LLVMSetValueName(dst_term, "dst_term");
301 #endif
302
303 if(blend->rgb_func == blend->alpha_func) {
304 return lp_build_blend_func(&bld, blend->rgb_func, src_term, dst_term);
305 }
306 else {
307 /* Seperate RGB / A functions */
308
309 LLVMValueRef rgb;
310 LLVMValueRef alpha;
311
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);
314
315 return lp_build_blend_swizzle(&bld, rgb, alpha, LP_BUILD_BLEND_SWIZZLE_RGBA, alpha_swizzle);
316 }
317 }