gallivm/llvmpipe: squash merge of the llvm-context branch
[mesa.git] / src / gallium / drivers / llvmpipe / lp_bld_blend_soa.c
1 /**************************************************************************
2 *
3 * Copyright 2009-2010 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 -- SoA layout.
32 *
33 * Blending in SoA is much faster than AoS, especially when separate rgb/alpha
34 * factors/functions are used, since no channel masking/shuffling is necessary
35 * and we can achieve the full throughput of the SIMD operations. Furthermore
36 * the fragment shader output is also in SoA, so it fits nicely with the rest
37 * of the fragment pipeline.
38 *
39 * The drawback is that to be displayed the color buffer needs to be in AoS
40 * layout, so we need to tile/untile the color buffer before/after rendering.
41 * A color buffer like
42 *
43 * R11 G11 B11 A11 R12 G12 B12 A12 R13 G13 B13 A13 R14 G14 B14 A14 ...
44 * R21 G21 B21 A21 R22 G22 B22 A22 R23 G23 B23 A23 R24 G24 B24 A24 ...
45 *
46 * R31 G31 B31 A31 R32 G32 B32 A32 R33 G33 B33 A33 R34 G34 B34 A34 ...
47 * R41 G41 B41 A41 R42 G42 B42 A42 R43 G43 B43 A43 R44 G44 B44 A44 ...
48 *
49 * ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
50 *
51 * will actually be stored in memory as
52 *
53 * R11 R12 R21 R22 R13 R14 R23 R24 ... G11 G12 G21 G22 G13 G14 G23 G24 ... B11 B12 B21 B22 B13 B14 B23 B24 ... A11 A12 A21 A22 A13 A14 A23 A24 ...
54 * R31 R32 R41 R42 R33 R34 R43 R44 ... G31 G32 G41 G42 G33 G34 G43 G44 ... B31 B32 B41 B42 B33 B34 B43 B44 ... A31 A32 A41 A42 A33 A34 A43 A44 ...
55 * ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
56 *
57 * NOTE: Run lp_blend_test after any change to this file.
58 *
59 * You can also run lp_blend_test to obtain AoS vs SoA benchmarks. Invoking it
60 * as:
61 *
62 * lp_blend_test -o blend.tsv
63 *
64 * will generate a tab-seperated-file with the test results and performance
65 * measurements.
66 *
67 * @author Jose Fonseca <jfonseca@vmware.com>
68 */
69
70
71 #include "pipe/p_state.h"
72 #include "util/u_debug.h"
73
74 #include "gallivm/lp_bld_type.h"
75 #include "gallivm/lp_bld_arit.h"
76 #include "gallivm/lp_bld_init.h"
77 #include "lp_bld_blend.h"
78
79
80 /**
81 * We may use the same values several times, so we keep them here to avoid
82 * recomputing them. Also reusing the values allows us to do simplifications
83 * that LLVM optimization passes wouldn't normally be able to do.
84 */
85 struct lp_build_blend_soa_context
86 {
87 struct lp_build_context base;
88
89 LLVMValueRef src[4];
90 LLVMValueRef dst[4];
91 LLVMValueRef con[4];
92
93 LLVMValueRef inv_src[4];
94 LLVMValueRef inv_dst[4];
95 LLVMValueRef inv_con[4];
96
97 LLVMValueRef src_alpha_saturate;
98
99 /**
100 * We store all factors in a table in order to eliminate redundant
101 * multiplications later.
102 * Indexes are: factor[src,dst][color,term][r,g,b,a]
103 */
104 LLVMValueRef factor[2][2][4];
105
106 /**
107 * Table with all terms.
108 * Indexes are: term[src,dst][r,g,b,a]
109 */
110 LLVMValueRef term[2][4];
111 };
112
113
114 /**
115 * Build a single SOA blend factor for a color channel.
116 * \param i the color channel in [0,3]
117 */
118 static LLVMValueRef
119 lp_build_blend_soa_factor(struct lp_build_blend_soa_context *bld,
120 unsigned factor, unsigned i)
121 {
122 /*
123 * Compute src/first term RGB
124 */
125 switch (factor) {
126 case PIPE_BLENDFACTOR_ONE:
127 return bld->base.one;
128 case PIPE_BLENDFACTOR_SRC_COLOR:
129 return bld->src[i];
130 case PIPE_BLENDFACTOR_SRC_ALPHA:
131 return bld->src[3];
132 case PIPE_BLENDFACTOR_DST_COLOR:
133 return bld->dst[i];
134 case PIPE_BLENDFACTOR_DST_ALPHA:
135 return bld->dst[3];
136 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
137 if(i == 3)
138 return bld->base.one;
139 else {
140 if(!bld->inv_dst[3])
141 bld->inv_dst[3] = lp_build_comp(&bld->base, bld->dst[3]);
142 if(!bld->src_alpha_saturate)
143 bld->src_alpha_saturate = lp_build_min(&bld->base, bld->src[3], bld->inv_dst[3]);
144 return bld->src_alpha_saturate;
145 }
146 case PIPE_BLENDFACTOR_CONST_COLOR:
147 return bld->con[i];
148 case PIPE_BLENDFACTOR_CONST_ALPHA:
149 return bld->con[3];
150 case PIPE_BLENDFACTOR_SRC1_COLOR:
151 /* TODO */
152 assert(0);
153 return bld->base.zero;
154 case PIPE_BLENDFACTOR_SRC1_ALPHA:
155 /* TODO */
156 assert(0);
157 return bld->base.zero;
158 case PIPE_BLENDFACTOR_ZERO:
159 return bld->base.zero;
160 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
161 if(!bld->inv_src[i])
162 bld->inv_src[i] = lp_build_comp(&bld->base, bld->src[i]);
163 return bld->inv_src[i];
164 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
165 if(!bld->inv_src[3])
166 bld->inv_src[3] = lp_build_comp(&bld->base, bld->src[3]);
167 return bld->inv_src[3];
168 case PIPE_BLENDFACTOR_INV_DST_COLOR:
169 if(!bld->inv_dst[i])
170 bld->inv_dst[i] = lp_build_comp(&bld->base, bld->dst[i]);
171 return bld->inv_dst[i];
172 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
173 if(!bld->inv_dst[3])
174 bld->inv_dst[3] = lp_build_comp(&bld->base, bld->dst[3]);
175 return bld->inv_dst[3];
176 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
177 if(!bld->inv_con[i])
178 bld->inv_con[i] = lp_build_comp(&bld->base, bld->con[i]);
179 return bld->inv_con[i];
180 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
181 if(!bld->inv_con[3])
182 bld->inv_con[3] = lp_build_comp(&bld->base, bld->con[3]);
183 return bld->inv_con[3];
184 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
185 /* TODO */
186 assert(0);
187 return bld->base.zero;
188 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
189 /* TODO */
190 assert(0);
191 return bld->base.zero;
192 default:
193 assert(0);
194 return bld->base.zero;
195 }
196 }
197
198
199 static boolean
200 lp_build_blend_factor_complementary(unsigned src_factor, unsigned dst_factor)
201 {
202 return dst_factor == (src_factor ^ 0x10);
203 }
204
205
206 /**
207 * Generate blend code in SOA mode.
208 * \param rt render target index (to index the blend / colormask state)
209 * \param src src/fragment color
210 * \param dst dst/framebuffer color
211 * \param con constant blend color
212 * \param res the result/output
213 */
214 void
215 lp_build_blend_soa(struct gallivm_state *gallivm,
216 const struct pipe_blend_state *blend,
217 struct lp_type type,
218 unsigned rt,
219 LLVMValueRef src[4],
220 LLVMValueRef dst[4],
221 LLVMValueRef con[4],
222 LLVMValueRef res[4])
223 {
224 LLVMBuilderRef builder = gallivm->builder;
225 struct lp_build_blend_soa_context bld;
226 unsigned i, j, k;
227
228 assert(rt < PIPE_MAX_COLOR_BUFS);
229
230 /* Setup build context */
231 memset(&bld, 0, sizeof bld);
232 lp_build_context_init(&bld.base, gallivm, type);
233 for (i = 0; i < 4; ++i) {
234 bld.src[i] = src[i];
235 bld.dst[i] = dst[i];
236 bld.con[i] = con[i];
237 }
238
239 for (i = 0; i < 4; ++i) {
240 /* only compute blending for the color channels enabled for writing */
241 if (blend->rt[rt].colormask & (1 << i)) {
242 if (blend->logicop_enable) {
243 if(!type.floating) {
244 res[i] = lp_build_logicop(builder, blend->logicop_func, src[i], dst[i]);
245 }
246 else
247 res[i] = dst[i];
248 }
249 else if (blend->rt[rt].blend_enable) {
250 unsigned src_factor = i < 3 ? blend->rt[rt].rgb_src_factor : blend->rt[rt].alpha_src_factor;
251 unsigned dst_factor = i < 3 ? blend->rt[rt].rgb_dst_factor : blend->rt[rt].alpha_dst_factor;
252 unsigned func = i < 3 ? blend->rt[rt].rgb_func : blend->rt[rt].alpha_func;
253 boolean func_commutative = lp_build_blend_func_commutative(func);
254
255 if (func == PIPE_BLEND_ADD &&
256 lp_build_blend_factor_complementary(src_factor, dst_factor) && 0) {
257 /*
258 * Special case linear interpolation, (i.e., complementary factors).
259 */
260
261 LLVMValueRef weight;
262 if (src_factor < dst_factor) {
263 weight = lp_build_blend_soa_factor(&bld, src_factor, i);
264 res[i] = lp_build_lerp(&bld.base, weight, dst[i], src[i]);
265 } else {
266 weight = lp_build_blend_soa_factor(&bld, dst_factor, i);
267 res[i] = lp_build_lerp(&bld.base, weight, src[i], dst[i]);
268 }
269 continue;
270 }
271
272 if ((func == PIPE_BLEND_ADD ||
273 func == PIPE_BLEND_SUBTRACT ||
274 func == PIPE_BLEND_REVERSE_SUBTRACT) &&
275 src_factor == dst_factor &&
276 type.floating) {
277 /*
278 * Special common factor.
279 *
280 * XXX: Only for floating points for now, since saturation will
281 * cause different results.
282 */
283
284 LLVMValueRef factor;
285 factor = lp_build_blend_soa_factor(&bld, src_factor, i);
286 res[i] = lp_build_blend_func(&bld.base, func, src[i], dst[i]);
287 res[i] = lp_build_mul(&bld.base, res[i], factor);
288 continue;
289 }
290
291 /*
292 * Compute src/dst factors.
293 */
294
295 bld.factor[0][0][i] = src[i];
296 bld.factor[0][1][i] = lp_build_blend_soa_factor(&bld, src_factor, i);
297 bld.factor[1][0][i] = dst[i];
298 bld.factor[1][1][i] = lp_build_blend_soa_factor(&bld, dst_factor, i);
299
300 /*
301 * Compute src/dst terms
302 */
303
304 for(k = 0; k < 2; ++k) {
305 /* See if this multiplication has been previously computed */
306 for(j = 0; j < i; ++j) {
307 if((bld.factor[k][0][j] == bld.factor[k][0][i] &&
308 bld.factor[k][1][j] == bld.factor[k][1][i]) ||
309 (bld.factor[k][0][j] == bld.factor[k][1][i] &&
310 bld.factor[k][1][j] == bld.factor[k][0][i]))
311 break;
312 }
313
314 if(j < i)
315 bld.term[k][i] = bld.term[k][j];
316 else
317 bld.term[k][i] = lp_build_mul(&bld.base, bld.factor[k][0][i], bld.factor[k][1][i]);
318
319 if (src_factor == PIPE_BLENDFACTOR_ZERO &&
320 (dst_factor == PIPE_BLENDFACTOR_DST_ALPHA ||
321 dst_factor == PIPE_BLENDFACTOR_INV_DST_ALPHA)) {
322 /* XXX special case these combos to work around an apparent
323 * bug in LLVM.
324 * This hack disables the check for multiplication by zero
325 * in lp_bld_mul(). When we optimize away the
326 * multiplication, something goes wrong during code
327 * generation and we segfault at runtime.
328 */
329 LLVMValueRef zeroSave = bld.base.zero;
330 bld.base.zero = NULL;
331 bld.term[k][i] = lp_build_mul(&bld.base, bld.factor[k][0][i],
332 bld.factor[k][1][i]);
333 bld.base.zero = zeroSave;
334 }
335 }
336
337 /*
338 * Combine terms
339 */
340
341 /* See if this function has been previously applied */
342 for(j = 0; j < i; ++j) {
343 unsigned prev_func = j < 3 ? blend->rt[rt].rgb_func : blend->rt[rt].alpha_func;
344 unsigned func_reverse = lp_build_blend_func_reverse(func, prev_func);
345
346 if((!func_reverse &&
347 bld.term[0][j] == bld.term[0][i] &&
348 bld.term[1][j] == bld.term[1][i]) ||
349 ((func_commutative || func_reverse) &&
350 bld.term[0][j] == bld.term[1][i] &&
351 bld.term[1][j] == bld.term[0][i]))
352 break;
353 }
354
355 if(j < i)
356 res[i] = res[j];
357 else
358 res[i] = lp_build_blend_func(&bld.base, func, bld.term[0][i], bld.term[1][i]);
359 }
360 else {
361 res[i] = src[i];
362 }
363 }
364 else {
365 res[i] = dst[i];
366 }
367 }
368 }