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 -- SoA layout.
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 of
37 * the fragment pipeline.
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.
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 ...
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 ...
49 * ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
51 * will actually be stored in memory as
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 * ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
57 * NOTE: Run lp_blend_test after any change to this file.
59 * You can also run lp_blend_test to obtain AoS vs SoA benchmarks. Invoking it
62 * lp_blend_test -o blend.tsv
64 * will generate a tab-seperated-file with the test results and performance
67 * @author Jose Fonseca <jfonseca@vmware.com>
71 #include "pipe/p_state.h"
73 #include "lp_bld_type.h"
74 #include "lp_bld_const.h"
75 #include "lp_bld_arit.h"
76 #include "lp_bld_blend.h"
80 * We may the same values several times, so we keep them here to avoid
81 * recomputing them. Also reusing the values allows us to do simplifications
82 * that LLVM optimization passes wouldn't normally be able to do.
84 struct lp_build_blend_soa_context
86 struct lp_build_context base
;
92 LLVMValueRef inv_src
[4];
93 LLVMValueRef inv_dst
[4];
94 LLVMValueRef inv_con
[4];
96 LLVMValueRef src_alpha_saturate
;
99 * We store all factors in a table in order to eliminate redundant
100 * multiplications later.
102 LLVMValueRef factor
[2][2][4];
105 * Table with all terms.
107 LLVMValueRef term
[2][4];
112 lp_build_blend_soa_factor(struct lp_build_blend_soa_context
*bld
,
113 unsigned factor
, unsigned i
)
116 * Compute src/first term RGB
119 case PIPE_BLENDFACTOR_ONE
:
120 return bld
->base
.one
;
121 case PIPE_BLENDFACTOR_SRC_COLOR
:
123 case PIPE_BLENDFACTOR_SRC_ALPHA
:
125 case PIPE_BLENDFACTOR_DST_COLOR
:
127 case PIPE_BLENDFACTOR_DST_ALPHA
:
129 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
131 return bld
->base
.one
;
134 bld
->inv_dst
[3] = lp_build_comp(&bld
->base
, bld
->dst
[3]);
135 if(!bld
->src_alpha_saturate
)
136 bld
->src_alpha_saturate
= lp_build_min(&bld
->base
, bld
->src
[3], bld
->inv_dst
[3]);
137 return bld
->src_alpha_saturate
;
139 case PIPE_BLENDFACTOR_CONST_COLOR
:
141 case PIPE_BLENDFACTOR_CONST_ALPHA
:
143 case PIPE_BLENDFACTOR_SRC1_COLOR
:
146 return bld
->base
.zero
;
147 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
150 return bld
->base
.zero
;
151 case PIPE_BLENDFACTOR_ZERO
:
152 return bld
->base
.zero
;
153 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
155 bld
->inv_src
[i
] = lp_build_comp(&bld
->base
, bld
->src
[i
]);
156 return bld
->inv_src
[i
];
157 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
159 bld
->inv_src
[3] = lp_build_comp(&bld
->base
, bld
->src
[3]);
160 return bld
->inv_src
[3];
161 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
163 bld
->inv_dst
[i
] = lp_build_comp(&bld
->base
, bld
->dst
[i
]);
164 return bld
->inv_dst
[i
];
165 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
167 bld
->inv_dst
[3] = lp_build_comp(&bld
->base
, bld
->dst
[3]);
168 return bld
->inv_dst
[3];
169 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
171 bld
->inv_con
[i
] = lp_build_comp(&bld
->base
, bld
->con
[i
]);
172 return bld
->inv_con
[i
];
173 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
175 bld
->inv_con
[3] = lp_build_comp(&bld
->base
, bld
->con
[3]);
176 return bld
->inv_con
[3];
177 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
180 return bld
->base
.zero
;
181 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
184 return bld
->base
.zero
;
187 return bld
->base
.zero
;
193 * Generate blend code in SOA mode.
194 * \param src src/fragment color
195 * \param dst dst/framebuffer color
196 * \param con constant blend color
197 * \param res the result/output
200 lp_build_blend_soa(LLVMBuilderRef builder
,
201 const struct pipe_blend_state
*blend
,
208 struct lp_build_blend_soa_context bld
;
211 /* Setup build context */
212 memset(&bld
, 0, sizeof bld
);
213 lp_build_context_init(&bld
.base
, builder
, type
);
214 for (i
= 0; i
< 4; ++i
) {
220 for (i
= 0; i
< 4; ++i
) {
221 if (blend
->colormask
& (1 << i
)) {
222 if (blend
->logicop_enable
) {
224 res
[i
] = lp_build_logicop(builder
, blend
->logicop_func
, src
[i
], dst
[i
]);
229 else if (blend
->blend_enable
) {
230 unsigned src_factor
= i
< 3 ? blend
->rgb_src_factor
: blend
->alpha_src_factor
;
231 unsigned dst_factor
= i
< 3 ? blend
->rgb_dst_factor
: blend
->alpha_dst_factor
;
232 unsigned func
= i
< 3 ? blend
->rgb_func
: blend
->alpha_func
;
233 boolean func_commutative
= lp_build_blend_func_commutative(func
);
235 /* It makes no sense to blend unless values are normalized */
239 * Compute src/dst factors.
242 bld
.factor
[0][0][i
] = src
[i
];
243 bld
.factor
[0][1][i
] = lp_build_blend_soa_factor(&bld
, src_factor
, i
);
244 bld
.factor
[1][0][i
] = dst
[i
];
245 bld
.factor
[1][1][i
] = lp_build_blend_soa_factor(&bld
, dst_factor
, i
);
248 * Compute src/dst terms
251 for(k
= 0; k
< 2; ++k
) {
252 /* See if this multiplication has been previously computed */
253 for(j
= 0; j
< i
; ++j
) {
254 if((bld
.factor
[k
][0][j
] == bld
.factor
[k
][0][i
] &&
255 bld
.factor
[k
][1][j
] == bld
.factor
[k
][1][i
]) ||
256 (bld
.factor
[k
][0][j
] == bld
.factor
[k
][1][i
] &&
257 bld
.factor
[k
][1][j
] == bld
.factor
[k
][0][i
]))
262 bld
.term
[k
][i
] = bld
.term
[k
][j
];
264 bld
.term
[k
][i
] = lp_build_mul(&bld
.base
, bld
.factor
[k
][0][i
], bld
.factor
[k
][1][i
]);
271 /* See if this function has been previously applied */
272 for(j
= 0; j
< i
; ++j
) {
273 unsigned prev_func
= j
< 3 ? blend
->rgb_func
: blend
->alpha_func
;
274 unsigned func_reverse
= lp_build_blend_func_reverse(func
, prev_func
);
277 bld
.term
[0][j
] == bld
.term
[0][i
] &&
278 bld
.term
[1][j
] == bld
.term
[1][i
]) ||
279 ((func_commutative
|| func_reverse
) &&
280 bld
.term
[0][j
] == bld
.term
[1][i
] &&
281 bld
.term
[1][j
] == bld
.term
[0][i
]))
288 res
[i
] = lp_build_blend_func(&bld
.base
, func
, bld
.term
[0][i
], bld
.term
[1][i
]);