clover: Wrap event::wait_count in a method taking care of the required locking.
[mesa.git] / src / gallium / drivers / llvmpipe / lp_bld_blend_aos.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 -- AoS layout.
32 *
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
38 * purposes.
39 *
40 * Run lp_blend_test after any change to this file.
41 *
42 * @author Jose Fonseca <jfonseca@vmware.com>
43 */
44
45
46 #include "pipe/p_state.h"
47 #include "util/u_debug.h"
48 #include "util/u_format.h"
49
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"
57
58 #include "lp_bld_blend.h"
59
60
61 /**
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.
65 */
66 struct lp_build_blend_aos_context
67 {
68 struct lp_build_context base;
69
70 LLVMValueRef src;
71 LLVMValueRef src_alpha;
72 LLVMValueRef src1;
73 LLVMValueRef src1_alpha;
74 LLVMValueRef dst;
75 LLVMValueRef const_;
76 LLVMValueRef const_alpha;
77 boolean has_dst_alpha;
78
79 LLVMValueRef inv_src;
80 LLVMValueRef inv_src_alpha;
81 LLVMValueRef inv_dst;
82 LLVMValueRef inv_const;
83 LLVMValueRef inv_const_alpha;
84 LLVMValueRef saturate;
85
86 LLVMValueRef rgb_src_factor;
87 LLVMValueRef alpha_src_factor;
88 LLVMValueRef rgb_dst_factor;
89 LLVMValueRef alpha_dst_factor;
90 };
91
92
93 static LLVMValueRef
94 lp_build_blend_factor_unswizzled(struct lp_build_blend_aos_context *bld,
95 unsigned factor,
96 boolean alpha)
97 {
98 LLVMValueRef src_alpha = bld->src_alpha ? bld->src_alpha : bld->src;
99 LLVMValueRef src1_alpha = bld->src1_alpha ? bld->src1_alpha : bld->src1;
100 LLVMValueRef const_alpha = bld->const_alpha ? bld->const_alpha : bld->const_;
101
102 switch (factor) {
103 case PIPE_BLENDFACTOR_ZERO:
104 return bld->base.zero;
105 case PIPE_BLENDFACTOR_ONE:
106 return bld->base.one;
107 case PIPE_BLENDFACTOR_SRC_COLOR:
108 return bld->src;
109 case PIPE_BLENDFACTOR_SRC_ALPHA:
110 return src_alpha;
111 case PIPE_BLENDFACTOR_DST_COLOR:
112 case PIPE_BLENDFACTOR_DST_ALPHA:
113 return bld->dst;
114 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
115 if(alpha)
116 return bld->base.one;
117 else {
118 /*
119 * If there's no dst alpha the complement is zero but for unclamped
120 * float inputs min can be non-zero (negative).
121 */
122 if (!bld->has_dst_alpha) {
123 if (!bld->saturate)
124 bld->saturate = lp_build_min(&bld->base, src_alpha, bld->base.zero);
125 }
126 else {
127 if(!bld->inv_dst)
128 bld->inv_dst = lp_build_comp(&bld->base, bld->dst);
129 if(!bld->saturate)
130 bld->saturate = lp_build_min(&bld->base, src_alpha, bld->inv_dst);
131 }
132 return bld->saturate;
133 }
134 case PIPE_BLENDFACTOR_CONST_COLOR:
135 return bld->const_;
136 case PIPE_BLENDFACTOR_CONST_ALPHA:
137 return const_alpha;
138 case PIPE_BLENDFACTOR_SRC1_COLOR:
139 return bld->src1;
140 case PIPE_BLENDFACTOR_SRC1_ALPHA:
141 return src1_alpha;
142 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
143 if(!bld->inv_src)
144 bld->inv_src = lp_build_comp(&bld->base, bld->src);
145 return bld->inv_src;
146 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
147 if(!bld->inv_src_alpha)
148 bld->inv_src_alpha = lp_build_comp(&bld->base, src_alpha);
149 return bld->inv_src_alpha;
150 case PIPE_BLENDFACTOR_INV_DST_COLOR:
151 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
152 if(!bld->inv_dst)
153 bld->inv_dst = lp_build_comp(&bld->base, bld->dst);
154 return bld->inv_dst;
155 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
156 if(!bld->inv_const)
157 bld->inv_const = lp_build_comp(&bld->base, bld->const_);
158 return bld->inv_const;
159 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
160 if(!bld->inv_const_alpha)
161 bld->inv_const_alpha = lp_build_comp(&bld->base, const_alpha);
162 return bld->inv_const_alpha;
163 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
164 return lp_build_comp(&bld->base, bld->src1);
165 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
166 return lp_build_comp(&bld->base, src1_alpha);
167 default:
168 assert(0);
169 return bld->base.zero;
170 }
171 }
172
173
174 enum lp_build_blend_swizzle {
175 LP_BUILD_BLEND_SWIZZLE_RGBA = 0,
176 LP_BUILD_BLEND_SWIZZLE_AAAA = 1
177 };
178
179
180 /**
181 * How should we shuffle the base factor.
182 */
183 static enum lp_build_blend_swizzle
184 lp_build_blend_factor_swizzle(unsigned factor)
185 {
186 switch (factor) {
187 case PIPE_BLENDFACTOR_ONE:
188 case PIPE_BLENDFACTOR_ZERO:
189 case PIPE_BLENDFACTOR_SRC_COLOR:
190 case PIPE_BLENDFACTOR_DST_COLOR:
191 case PIPE_BLENDFACTOR_CONST_COLOR:
192 case PIPE_BLENDFACTOR_SRC1_COLOR:
193 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
194 case PIPE_BLENDFACTOR_INV_DST_COLOR:
195 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
196 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
197 return LP_BUILD_BLEND_SWIZZLE_RGBA;
198 case PIPE_BLENDFACTOR_SRC_ALPHA:
199 case PIPE_BLENDFACTOR_DST_ALPHA:
200 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
201 case PIPE_BLENDFACTOR_SRC1_ALPHA:
202 case PIPE_BLENDFACTOR_CONST_ALPHA:
203 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
204 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
205 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
206 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
207 return LP_BUILD_BLEND_SWIZZLE_AAAA;
208 default:
209 assert(0);
210 return LP_BUILD_BLEND_SWIZZLE_RGBA;
211 }
212 }
213
214
215 static LLVMValueRef
216 lp_build_blend_swizzle(struct lp_build_blend_aos_context *bld,
217 LLVMValueRef rgb,
218 LLVMValueRef alpha,
219 enum lp_build_blend_swizzle rgb_swizzle,
220 unsigned alpha_swizzle,
221 unsigned num_channels)
222 {
223 LLVMValueRef swizzled_rgb;
224
225 switch (rgb_swizzle) {
226 case LP_BUILD_BLEND_SWIZZLE_RGBA:
227 swizzled_rgb = rgb;
228 break;
229 case LP_BUILD_BLEND_SWIZZLE_AAAA:
230 swizzled_rgb = lp_build_swizzle_scalar_aos(&bld->base, rgb, alpha_swizzle, num_channels);
231 break;
232 default:
233 assert(0);
234 swizzled_rgb = bld->base.undef;
235 }
236
237 if (rgb != alpha) {
238 swizzled_rgb = lp_build_select_aos(&bld->base, 1 << alpha_swizzle,
239 alpha, swizzled_rgb,
240 num_channels);
241 }
242
243 return swizzled_rgb;
244 }
245
246 /**
247 * @sa http://www.opengl.org/sdk/docs/man/xhtml/glBlendFuncSeparate.xml
248 */
249 static LLVMValueRef
250 lp_build_blend_factor(struct lp_build_blend_aos_context *bld,
251 unsigned rgb_factor,
252 unsigned alpha_factor,
253 unsigned alpha_swizzle,
254 unsigned num_channels)
255 {
256 LLVMValueRef rgb_factor_, alpha_factor_;
257 enum lp_build_blend_swizzle rgb_swizzle;
258
259 if (alpha_swizzle == PIPE_SWIZZLE_X && num_channels == 1) {
260 return lp_build_blend_factor_unswizzled(bld, alpha_factor, TRUE);
261 }
262
263 rgb_factor_ = lp_build_blend_factor_unswizzled(bld, rgb_factor, FALSE);
264
265 if (alpha_swizzle != PIPE_SWIZZLE_NONE) {
266 rgb_swizzle = lp_build_blend_factor_swizzle(rgb_factor);
267 alpha_factor_ = lp_build_blend_factor_unswizzled(bld, alpha_factor, TRUE);
268 return lp_build_blend_swizzle(bld, rgb_factor_, alpha_factor_, rgb_swizzle,
269 alpha_swizzle, num_channels);
270 } else {
271 return rgb_factor_;
272 }
273 }
274
275
276 /**
277 * Performs blending of src and dst pixels
278 *
279 * @param blend the blend state of the shader variant
280 * @param cbuf_format format of the colour buffer
281 * @param type data type of the pixel vector
282 * @param rt render target index
283 * @param src blend src
284 * @param src_alpha blend src alpha (if not included in src)
285 * @param src1 second blend src (for dual source blend)
286 * @param src1_alpha second blend src alpha (if not included in src1)
287 * @param dst blend dst
288 * @param mask optional mask to apply to the blending result
289 * @param const_ const blend color
290 * @param const_alpha const blend color alpha (if not included in const_)
291 * @param swizzle swizzle values for RGBA
292 *
293 * @return the result of blending src and dst
294 */
295 LLVMValueRef
296 lp_build_blend_aos(struct gallivm_state *gallivm,
297 const struct pipe_blend_state *blend,
298 enum pipe_format cbuf_format,
299 struct lp_type type,
300 unsigned rt,
301 LLVMValueRef src,
302 LLVMValueRef src_alpha,
303 LLVMValueRef src1,
304 LLVMValueRef src1_alpha,
305 LLVMValueRef dst,
306 LLVMValueRef mask,
307 LLVMValueRef const_,
308 LLVMValueRef const_alpha,
309 const unsigned char swizzle[4],
310 int nr_channels)
311 {
312 const struct pipe_rt_blend_state * state = &blend->rt[rt];
313 const struct util_format_description * desc;
314 struct lp_build_blend_aos_context bld;
315 LLVMValueRef src_factor, dst_factor;
316 LLVMValueRef result;
317 unsigned alpha_swizzle = PIPE_SWIZZLE_NONE;
318 unsigned i;
319
320 desc = util_format_description(cbuf_format);
321
322 /* Setup build context */
323 memset(&bld, 0, sizeof bld);
324 lp_build_context_init(&bld.base, gallivm, type);
325 bld.src = src;
326 bld.src1 = src1;
327 bld.dst = dst;
328 bld.const_ = const_;
329 bld.src_alpha = src_alpha;
330 bld.src1_alpha = src1_alpha;
331 bld.const_alpha = const_alpha;
332 bld.has_dst_alpha = FALSE;
333
334 /* Find the alpha channel if not provided seperately */
335 if (!src_alpha) {
336 for (i = 0; i < 4; ++i) {
337 if (swizzle[i] == 3) {
338 alpha_swizzle = i;
339 }
340 }
341 /*
342 * Note that we may get src_alpha included from source (and 4 channels)
343 * even if the destination doesn't have an alpha channel (for rgbx
344 * formats). Generally this shouldn't make much of a difference (we're
345 * relying on blend factors being sanitized already if there's no
346 * dst alpha).
347 */
348 bld.has_dst_alpha = desc->swizzle[3] <= PIPE_SWIZZLE_W;
349 }
350
351 if (blend->logicop_enable) {
352 if(!type.floating) {
353 result = lp_build_logicop(gallivm->builder, blend->logicop_func, src, dst);
354 }
355 else {
356 result = src;
357 }
358 } else if (!state->blend_enable) {
359 result = src;
360 } else {
361 boolean rgb_alpha_same = (state->rgb_src_factor == state->rgb_dst_factor &&
362 state->alpha_src_factor == state->alpha_dst_factor) ||
363 nr_channels == 1;
364
365 src_factor = lp_build_blend_factor(&bld, state->rgb_src_factor,
366 state->alpha_src_factor,
367 alpha_swizzle,
368 nr_channels);
369
370 dst_factor = lp_build_blend_factor(&bld, state->rgb_dst_factor,
371 state->alpha_dst_factor,
372 alpha_swizzle,
373 nr_channels);
374
375 result = lp_build_blend(&bld.base,
376 state->rgb_func,
377 state->rgb_src_factor,
378 state->rgb_dst_factor,
379 src,
380 dst,
381 src_factor,
382 dst_factor,
383 rgb_alpha_same,
384 false);
385
386 if(state->rgb_func != state->alpha_func && nr_channels > 1 &&
387 alpha_swizzle != PIPE_SWIZZLE_NONE) {
388 LLVMValueRef alpha;
389
390 alpha = lp_build_blend(&bld.base,
391 state->alpha_func,
392 state->alpha_src_factor,
393 state->alpha_dst_factor,
394 src,
395 dst,
396 src_factor,
397 dst_factor,
398 rgb_alpha_same,
399 false);
400
401 result = lp_build_blend_swizzle(&bld,
402 result,
403 alpha,
404 LP_BUILD_BLEND_SWIZZLE_RGBA,
405 alpha_swizzle,
406 nr_channels);
407 }
408 }
409
410 /* Check if color mask is necessary */
411 if (!util_format_colormask_full(desc, state->colormask)) {
412 LLVMValueRef color_mask;
413
414 color_mask = lp_build_const_mask_aos_swizzled(gallivm, bld.base.type,
415 state->colormask, nr_channels, swizzle);
416 lp_build_name(color_mask, "color_mask");
417
418 /* Combine with input mask if necessary */
419 if (mask) {
420 /* We can be blending floating values but masks are always integer... */
421 unsigned floating = bld.base.type.floating;
422 bld.base.type.floating = 0;
423
424 mask = lp_build_and(&bld.base, color_mask, mask);
425
426 bld.base.type.floating = floating;
427 } else {
428 mask = color_mask;
429 }
430 }
431
432 /* Apply mask, if one exists */
433 if (mask) {
434 result = lp_build_select(&bld.base, mask, result, dst);
435 }
436
437 return result;
438 }