1 /**************************************************************************
3 * Copyright 2010 VMware.
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 **************************************************************************/
29 #include "util/u_math.h"
30 #include "util/u_memory.h"
31 #include "util/u_simple_list.h"
32 #include "os/os_time.h"
33 #include "gallivm/lp_bld_arit.h"
34 #include "gallivm/lp_bld_const.h"
35 #include "gallivm/lp_bld_debug.h"
36 #include "gallivm/lp_bld_init.h"
37 #include "gallivm/lp_bld_logic.h"
38 #include "gallivm/lp_bld_intr.h"
39 #include "gallivm/lp_bld_flow.h"
40 #include "gallivm/lp_bld_type.h"
45 #include "lp_screen.h"
46 #include "lp_context.h"
48 #include "lp_state_fs.h"
49 #include "lp_state_setup.h"
53 /* currently organized to interpolate full float[4] attributes even
54 * when some elements are unused. Later, can pack vertex data more
61 /* Function arguments:
66 LLVMValueRef facing
; /* boolean */
73 LLVMValueRef x0_center
;
74 LLVMValueRef y0_center
;
75 LLVMValueRef dy20_ooa
;
76 LLVMValueRef dy01_ooa
;
77 LLVMValueRef dx20_ooa
;
78 LLVMValueRef dx01_ooa
;
84 type4f(struct gallivm_state
*gallivm
)
86 return LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), 4);
90 /* Equivalent of _mm_setr_ps(a,b,c,d)
93 vec4f(struct gallivm_state
*gallivm
,
94 LLVMValueRef a
, LLVMValueRef b
, LLVMValueRef c
, LLVMValueRef d
,
97 LLVMBuilderRef bld
= gallivm
->builder
;
98 LLVMValueRef i0
= lp_build_const_int32(gallivm
, 0);
99 LLVMValueRef i1
= lp_build_const_int32(gallivm
, 1);
100 LLVMValueRef i2
= lp_build_const_int32(gallivm
, 2);
101 LLVMValueRef i3
= lp_build_const_int32(gallivm
, 3);
103 LLVMValueRef res
= LLVMGetUndef(type4f(gallivm
));
105 res
= LLVMBuildInsertElement(bld
, res
, a
, i0
, "");
106 res
= LLVMBuildInsertElement(bld
, res
, b
, i1
, "");
107 res
= LLVMBuildInsertElement(bld
, res
, c
, i2
, "");
108 res
= LLVMBuildInsertElement(bld
, res
, d
, i3
, name
);
113 /* Equivalent of _mm_set1_ps(a)
116 vec4f_from_scalar(struct gallivm_state
*gallivm
,
120 LLVMBuilderRef bld
= gallivm
->builder
;
121 LLVMValueRef res
= LLVMGetUndef(type4f(gallivm
));
124 for(i
= 0; i
< 4; ++i
) {
125 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
126 res
= LLVMBuildInsertElement(bld
, res
, a
, index
, i
== 3 ? name
: "");
133 store_coef(struct gallivm_state
*gallivm
,
134 struct lp_setup_args
*args
,
140 LLVMBuilderRef builder
= gallivm
->builder
;
141 LLVMValueRef idx
= lp_build_const_int32(gallivm
, slot
);
143 LLVMBuildStore(builder
,
145 LLVMBuildGEP(builder
, args
->a0
, &idx
, 1, ""));
147 LLVMBuildStore(builder
,
149 LLVMBuildGEP(builder
, args
->dadx
, &idx
, 1, ""));
151 LLVMBuildStore(builder
,
153 LLVMBuildGEP(builder
, args
->dady
, &idx
, 1, ""));
159 emit_constant_coef4(struct gallivm_state
*gallivm
,
160 struct lp_setup_args
*args
,
164 LLVMValueRef zero
= lp_build_const_float(gallivm
, 0.0);
165 LLVMValueRef zerovec
= vec4f_from_scalar(gallivm
, zero
, "zero");
166 store_coef(gallivm
, args
, slot
, vert
, zerovec
, zerovec
);
172 * Setup the fragment input attribute with the front-facing value.
173 * \param frontface is the triangle front facing?
176 emit_facing_coef(struct gallivm_state
*gallivm
,
177 struct lp_setup_args
*args
,
180 LLVMBuilderRef builder
= gallivm
->builder
;
181 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
182 LLVMValueRef a0_0
= args
->facing
;
183 LLVMValueRef a0_0f
= LLVMBuildSIToFP(builder
, a0_0
, float_type
, "");
184 LLVMValueRef zero
= lp_build_const_float(gallivm
, 0.0);
185 /* Our face val is either 1 or 0 so we do
186 * face = (val * 2) - 1
189 LLVMValueRef face_val
=
190 LLVMBuildFAdd(builder
,
191 LLVMBuildFMul(builder
, a0_0f
,
192 lp_build_const_float(gallivm
, 2.0),
194 lp_build_const_float(gallivm
, -1.0),
196 LLVMValueRef a0
= vec4f(gallivm
, face_val
, zero
, zero
, zero
, "facing");
197 LLVMValueRef zerovec
= vec4f_from_scalar(gallivm
, zero
, "zero");
199 store_coef(gallivm
, args
, slot
, a0
, zerovec
, zerovec
);
204 vert_attrib(struct gallivm_state
*gallivm
,
210 LLVMBuilderRef b
= gallivm
->builder
;
212 idx
[0] = lp_build_const_int32(gallivm
, attr
);
213 idx
[1] = lp_build_const_int32(gallivm
, elem
);
214 return LLVMBuildLoad(b
, LLVMBuildGEP(b
, vert
, idx
, 2, ""), name
);
219 lp_twoside(struct gallivm_state
*gallivm
,
220 struct lp_setup_args
*args
,
221 const struct lp_setup_variant_key
*key
,
223 LLVMValueRef attribv
[3])
225 LLVMBuilderRef b
= gallivm
->builder
;
226 LLVMValueRef a0_back
, a1_back
, a2_back
;
227 LLVMValueRef idx2
= lp_build_const_int32(gallivm
, bcolor_slot
);
229 LLVMValueRef facing
= args
->facing
;
230 LLVMValueRef front_facing
= LLVMBuildICmp(b
, LLVMIntEQ
, facing
, lp_build_const_int32(gallivm
, 0), ""); /** need i1 for if condition */
232 a0_back
= LLVMBuildLoad(b
, LLVMBuildGEP(b
, args
->v0
, &idx2
, 1, ""), "v0a_back");
233 a1_back
= LLVMBuildLoad(b
, LLVMBuildGEP(b
, args
->v1
, &idx2
, 1, ""), "v1a_back");
234 a2_back
= LLVMBuildLoad(b
, LLVMBuildGEP(b
, args
->v2
, &idx2
, 1, ""), "v2a_back");
236 /* Possibly swap the front and back attrib values,
238 * Prefer select to if so we don't have to worry about phis or
241 attribv
[0] = LLVMBuildSelect(b
, front_facing
, a0_back
, attribv
[0], "");
242 attribv
[1] = LLVMBuildSelect(b
, front_facing
, a1_back
, attribv
[1], "");
243 attribv
[2] = LLVMBuildSelect(b
, front_facing
, a2_back
, attribv
[2], "");
248 lp_do_offset_tri(struct gallivm_state
*gallivm
,
249 struct lp_setup_args
*args
,
250 const struct lp_setup_variant_key
*key
,
251 LLVMValueRef inv_det
,
254 LLVMValueRef attribv
[3])
256 LLVMBuilderRef b
= gallivm
->builder
;
257 struct lp_build_context bld
;
258 struct lp_build_context flt_scalar_bld
;
259 struct lp_build_context int_scalar_bld
;
260 LLVMValueRef zoffset
, mult
;
261 LLVMValueRef z0_new
, z1_new
, z2_new
;
262 LLVMValueRef dzdxdzdy
, dzdx
, dzdy
, dzxyz20
, dyzzx01
, dyzzx01_dzxyz20
, dzx01_dyz20
;
263 LLVMValueRef z0z1
, z0z1z2
;
264 LLVMValueRef max
, max_value
, res12
;
265 LLVMValueRef shuffles
[4];
266 LLVMTypeRef shuf_type
= LLVMInt32TypeInContext(gallivm
->context
);
267 LLVMValueRef onei
= lp_build_const_int32(gallivm
, 1);
268 LLVMValueRef zeroi
= lp_build_const_int32(gallivm
, 0);
269 LLVMValueRef twoi
= lp_build_const_int32(gallivm
, 2);
270 LLVMValueRef threei
= lp_build_const_int32(gallivm
, 3);
271 LLVMValueRef mantissa_bits
, exp
, bias
;
272 LLVMValueRef maxz_value
, maxz0z1_value
;
274 /* (res12) = cross(e,f).xy */
279 dzxyz20
= LLVMBuildShuffleVector(b
, dxyz20
, dxyz20
, LLVMConstVector(shuffles
, 4), "");
285 dyzzx01
= LLVMBuildShuffleVector(b
, dxyz01
, dxyz01
, LLVMConstVector(shuffles
, 4), "");
287 dyzzx01_dzxyz20
= LLVMBuildFMul(b
, dzxyz20
, dyzzx01
, "dyzzx01_dzxyz20");
290 shuffles
[1] = threei
;
291 shuffles
[2] = LLVMGetUndef(shuf_type
);
292 shuffles
[3] = LLVMGetUndef(shuf_type
);
293 dzx01_dyz20
= LLVMBuildShuffleVector(b
, dyzzx01_dzxyz20
, dyzzx01_dzxyz20
,
294 LLVMConstVector(shuffles
, 4), "");
296 res12
= LLVMBuildFSub(b
, dyzzx01_dzxyz20
, dzx01_dyz20
, "res12");
298 /* dzdx = fabsf(res1 * inv_det), dydx = fabsf(res2 * inv_det)*/
299 lp_build_context_init(&bld
, gallivm
, lp_type_float_vec(32, 128));
300 dzdxdzdy
= LLVMBuildFMul(b
, res12
, inv_det
, "dzdxdzdy");
301 dzdxdzdy
= lp_build_abs(&bld
, dzdxdzdy
);
303 dzdx
= LLVMBuildExtractElement(b
, dzdxdzdy
, zeroi
, "");
304 dzdy
= LLVMBuildExtractElement(b
, dzdxdzdy
, onei
, "");
306 /* mult = MAX2(dzdx, dzdy) * pgon_offset_scale */
307 max
= LLVMBuildFCmp(b
, LLVMRealUGT
, dzdx
, dzdy
, "");
308 max_value
= LLVMBuildSelect(b
, max
, dzdx
, dzdy
, "max");
310 mult
= LLVMBuildFMul(b
, max_value
,
311 lp_build_const_float(gallivm
, key
->pgon_offset_scale
), "");
313 lp_build_context_init(&flt_scalar_bld
, gallivm
, lp_type_float_vec(32, 32));
315 if (key
->floating_point_depth
) {
317 * bias = pgon_offset_units * 2^(exponent(max(z0, z1, z2)) - mantissa_bits) +
318 * MAX2(dzdx, dzdy) * pgon_offset_scale
320 * NOTE: Assumes IEEE float32.
322 lp_build_context_init(&int_scalar_bld
, gallivm
, lp_type_int_vec(32, 32));
324 mantissa_bits
= lp_build_const_int32(gallivm
, 23);
326 maxz0z1_value
= lp_build_max(&flt_scalar_bld
,
327 LLVMBuildExtractElement(b
, attribv
[0], twoi
, ""),
328 LLVMBuildExtractElement(b
, attribv
[1], twoi
, ""));
330 maxz_value
= lp_build_max(&flt_scalar_bld
,
331 LLVMBuildExtractElement(b
, attribv
[2], twoi
, ""),
335 * XXX: TODO optimize this to quickly resolve a pow2 number through
336 * an exponent only operation.
338 exp
= lp_build_extract_exponent(&flt_scalar_bld
, maxz_value
, 0);
339 exp
= lp_build_sub(&int_scalar_bld
, exp
, mantissa_bits
);
340 exp
= lp_build_int_to_float(&flt_scalar_bld
, exp
);
342 bias
= LLVMBuildFMul(b
, lp_build_exp2(&flt_scalar_bld
, exp
),
343 lp_build_const_float(gallivm
, key
->pgon_offset_units
),
346 zoffset
= LLVMBuildFAdd(b
, bias
, mult
, "zoffset");
349 * bias = pgon_offset_units + MAX2(dzdx, dzdy) * pgon_offset_scale
351 zoffset
= LLVMBuildFAdd(b
,
352 lp_build_const_float(gallivm
, key
->pgon_offset_units
),
356 if (key
->pgon_offset_clamp
> 0) {
357 zoffset
= lp_build_min(&flt_scalar_bld
,
358 lp_build_const_float(gallivm
, key
->pgon_offset_clamp
),
361 else if (key
->pgon_offset_clamp
< 0) {
362 zoffset
= lp_build_max(&flt_scalar_bld
,
363 lp_build_const_float(gallivm
, key
->pgon_offset_clamp
),
369 shuffles
[1] = lp_build_const_int32(gallivm
, 6);
370 shuffles
[2] = LLVMGetUndef(shuf_type
);
371 shuffles
[3] = LLVMGetUndef(shuf_type
);
372 z0z1
= LLVMBuildShuffleVector(b
, attribv
[0], attribv
[1], LLVMConstVector(shuffles
, 4), "");
375 shuffles
[2] = lp_build_const_int32(gallivm
, 6);
376 shuffles
[3] = LLVMGetUndef(shuf_type
);
377 z0z1z2
= LLVMBuildShuffleVector(b
, z0z1
, attribv
[2], LLVMConstVector(shuffles
, 4), "");
378 zoffset
= vec4f_from_scalar(gallivm
, zoffset
, "");
380 /* clamp and do offset */
382 * FIXME I suspect the clamp (is that even right to always clamp to fixed
383 * 0.0/1.0?) should really be per fragment?
385 z0z1z2
= lp_build_clamp(&bld
, LLVMBuildFAdd(b
, z0z1z2
, zoffset
, ""), bld
.zero
, bld
.one
);
387 /* insert into args->a0.z, a1.z, a2.z:
389 z0_new
= LLVMBuildExtractElement(b
, z0z1z2
, zeroi
, "");
390 z1_new
= LLVMBuildExtractElement(b
, z0z1z2
, onei
, "");
391 z2_new
= LLVMBuildExtractElement(b
, z0z1z2
, twoi
, "");
392 attribv
[0] = LLVMBuildInsertElement(b
, attribv
[0], z0_new
, twoi
, "");
393 attribv
[1] = LLVMBuildInsertElement(b
, attribv
[1], z1_new
, twoi
, "");
394 attribv
[2] = LLVMBuildInsertElement(b
, attribv
[2], z2_new
, twoi
, "");
398 load_attribute(struct gallivm_state
*gallivm
,
399 struct lp_setup_args
*args
,
400 const struct lp_setup_variant_key
*key
,
402 LLVMValueRef attribv
[3])
404 LLVMBuilderRef b
= gallivm
->builder
;
405 LLVMValueRef idx
= lp_build_const_int32(gallivm
, vert_attr
);
407 /* Load the vertex data
409 attribv
[0] = LLVMBuildLoad(b
, LLVMBuildGEP(b
, args
->v0
, &idx
, 1, ""), "v0a");
410 attribv
[1] = LLVMBuildLoad(b
, LLVMBuildGEP(b
, args
->v1
, &idx
, 1, ""), "v1a");
411 attribv
[2] = LLVMBuildLoad(b
, LLVMBuildGEP(b
, args
->v2
, &idx
, 1, ""), "v2a");
414 /* Potentially modify it according to twoside, etc:
417 if (vert_attr
== key
->color_slot
&& key
->bcolor_slot
>= 0)
418 lp_twoside(gallivm
, args
, key
, key
->bcolor_slot
, attribv
);
419 else if (vert_attr
== key
->spec_slot
&& key
->bspec_slot
>= 0)
420 lp_twoside(gallivm
, args
, key
, key
->bspec_slot
, attribv
);
425 emit_coef4( struct gallivm_state
*gallivm
,
426 struct lp_setup_args
*args
,
432 LLVMBuilderRef b
= gallivm
->builder
;
433 LLVMValueRef dy20_ooa
= args
->dy20_ooa
;
434 LLVMValueRef dy01_ooa
= args
->dy01_ooa
;
435 LLVMValueRef dx20_ooa
= args
->dx20_ooa
;
436 LLVMValueRef dx01_ooa
= args
->dx01_ooa
;
437 LLVMValueRef x0_center
= args
->x0_center
;
438 LLVMValueRef y0_center
= args
->y0_center
;
440 LLVMValueRef da01
= LLVMBuildFSub(b
, a0
, a1
, "da01");
441 LLVMValueRef da20
= LLVMBuildFSub(b
, a2
, a0
, "da20");
443 /* Calculate dadx (vec4f)
445 LLVMValueRef da01_dy20_ooa
= LLVMBuildFMul(b
, da01
, dy20_ooa
, "da01_dy20_ooa");
446 LLVMValueRef da20_dy01_ooa
= LLVMBuildFMul(b
, da20
, dy01_ooa
, "da20_dy01_ooa");
447 LLVMValueRef dadx
= LLVMBuildFSub(b
, da01_dy20_ooa
, da20_dy01_ooa
, "dadx");
449 /* Calculate dady (vec4f)
451 LLVMValueRef da01_dx20_ooa
= LLVMBuildFMul(b
, da01
, dx20_ooa
, "da01_dx20_ooa");
452 LLVMValueRef da20_dx01_ooa
= LLVMBuildFMul(b
, da20
, dx01_ooa
, "da20_dx01_ooa");
453 LLVMValueRef dady
= LLVMBuildFSub(b
, da20_dx01_ooa
, da01_dx20_ooa
, "dady");
455 /* Calculate a0 - the attribute value at the origin
457 LLVMValueRef dadx_x0
= LLVMBuildFMul(b
, dadx
, x0_center
, "dadx_x0");
458 LLVMValueRef dady_y0
= LLVMBuildFMul(b
, dady
, y0_center
, "dady_y0");
459 LLVMValueRef attr_v0
= LLVMBuildFAdd(b
, dadx_x0
, dady_y0
, "attr_v0");
460 LLVMValueRef attr_0
= LLVMBuildFSub(b
, a0
, attr_v0
, "attr_0");
462 store_coef(gallivm
, args
, slot
, attr_0
, dadx
, dady
);
467 emit_linear_coef( struct gallivm_state
*gallivm
,
468 struct lp_setup_args
*args
,
470 LLVMValueRef attribv
[3])
472 /* nothing to do anymore */
482 * Compute a0, dadx and dady for a perspective-corrected interpolant,
484 * We basically multiply the vertex value by 1/w before computing
485 * the plane coefficients (a0, dadx, dady).
486 * Later, when we compute the value at a particular fragment position we'll
487 * divide the interpolated value by the interpolated W at that fragment.
490 apply_perspective_corr( struct gallivm_state
*gallivm
,
491 struct lp_setup_args
*args
,
493 LLVMValueRef attribv
[3])
495 LLVMBuilderRef b
= gallivm
->builder
;
497 /* premultiply by 1/w (v[0][3] is always 1/w):
499 LLVMValueRef v0_oow
= vec4f_from_scalar(gallivm
, vert_attrib(gallivm
, args
->v0
, 0, 3, ""), "v0_oow");
500 LLVMValueRef v1_oow
= vec4f_from_scalar(gallivm
, vert_attrib(gallivm
, args
->v1
, 0, 3, ""), "v1_oow");
501 LLVMValueRef v2_oow
= vec4f_from_scalar(gallivm
, vert_attrib(gallivm
, args
->v2
, 0, 3, ""), "v2_oow");
503 attribv
[0] = LLVMBuildFMul(b
, attribv
[0], v0_oow
, "v0_oow_v0a");
504 attribv
[1] = LLVMBuildFMul(b
, attribv
[1], v1_oow
, "v1_oow_v1a");
505 attribv
[2] = LLVMBuildFMul(b
, attribv
[2], v2_oow
, "v2_oow_v2a");
510 emit_position_coef( struct gallivm_state
*gallivm
,
511 struct lp_setup_args
*args
,
513 LLVMValueRef attribv
[3])
515 emit_linear_coef(gallivm
, args
, slot
, attribv
);
520 * Applys cylindrical wrapping to vertex attributes if enabled.
521 * Input coordinates must be in [0, 1] range, otherwise results are undefined.
523 * @param cyl_wrap TGSI_CYLINDRICAL_WRAP_x flags
526 emit_apply_cyl_wrap(struct gallivm_state
*gallivm
,
527 struct lp_setup_args
*args
,
529 LLVMValueRef attribv
[3])
532 LLVMBuilderRef builder
= gallivm
->builder
;
533 struct lp_type type
= lp_float32_vec4_type();
534 LLVMTypeRef float_vec_type
= lp_build_vec_type(gallivm
, type
);
535 LLVMValueRef pos_half
;
536 LLVMValueRef neg_half
;
537 LLVMValueRef cyl_mask
;
546 pos_half
= lp_build_const_vec(gallivm
, type
, +0.5f
);
547 neg_half
= lp_build_const_vec(gallivm
, type
, -0.5f
);
548 cyl_mask
= lp_build_const_mask_aos(gallivm
, type
, cyl_wrap
, 4);
550 one
= lp_build_const_vec(gallivm
, type
, 1.0f
);
551 one
= LLVMBuildBitCast(builder
, one
, lp_build_int_vec_type(gallivm
, type
), "");
552 one
= LLVMBuildAnd(builder
, one
, cyl_mask
, "");
555 delta
= LLVMBuildFSub(builder
, attribv
[1], attribv
[0], "");
557 offset
= lp_build_compare(gallivm
, type
, PIPE_FUNC_GREATER
, delta
, pos_half
);
558 offset
= LLVMBuildAnd(builder
, offset
, one
, "");
559 offset
= LLVMBuildBitCast(builder
, offset
, float_vec_type
, "");
560 attribv
[0] = LLVMBuildFAdd(builder
, attribv
[0], offset
, "");
562 offset
= lp_build_compare(gallivm
, type
, PIPE_FUNC_LESS
, delta
, neg_half
);
563 offset
= LLVMBuildAnd(builder
, offset
, one
, "");
564 offset
= LLVMBuildBitCast(builder
, offset
, float_vec_type
, "");
565 attribv
[1] = LLVMBuildFAdd(builder
, attribv
[1], offset
, "");
568 delta
= LLVMBuildFSub(builder
, attribv
[2], attribv
[1], "");
570 offset
= lp_build_compare(gallivm
, type
, PIPE_FUNC_GREATER
, delta
, pos_half
);
571 offset
= LLVMBuildAnd(builder
, offset
, one
, "");
572 offset
= LLVMBuildBitCast(builder
, offset
, float_vec_type
, "");
573 attribv
[1] = LLVMBuildFAdd(builder
, attribv
[1], offset
, "");
575 offset
= lp_build_compare(gallivm
, type
, PIPE_FUNC_LESS
, delta
, neg_half
);
576 offset
= LLVMBuildAnd(builder
, offset
, one
, "");
577 offset
= LLVMBuildBitCast(builder
, offset
, float_vec_type
, "");
578 attribv
[2] = LLVMBuildFAdd(builder
, attribv
[2], offset
, "");
581 delta
= LLVMBuildFSub(builder
, attribv
[0], attribv
[2], "");
583 offset
= lp_build_compare(gallivm
, type
, PIPE_FUNC_GREATER
, delta
, pos_half
);
584 offset
= LLVMBuildAnd(builder
, offset
, one
, "");
585 offset
= LLVMBuildBitCast(builder
, offset
, float_vec_type
, "");
586 attribv
[2] = LLVMBuildFAdd(builder
, attribv
[2], offset
, "");
588 offset
= lp_build_compare(gallivm
, type
, PIPE_FUNC_LESS
, delta
, neg_half
);
589 offset
= LLVMBuildAnd(builder
, offset
, one
, "");
590 offset
= LLVMBuildBitCast(builder
, offset
, float_vec_type
, "");
591 attribv
[0] = LLVMBuildFAdd(builder
, attribv
[0], offset
, "");
596 * Compute the inputs-> dadx, dady, a0 values.
599 emit_tri_coef( struct gallivm_state
*gallivm
,
600 const struct lp_setup_variant_key
*key
,
601 struct lp_setup_args
*args
)
605 LLVMValueRef attribs
[3];
607 /* setup interpolation for all the remaining attributes:
609 for (slot
= 0; slot
< key
->num_inputs
; slot
++) {
610 switch (key
->inputs
[slot
].interp
) {
611 case LP_INTERP_CONSTANT
:
612 load_attribute(gallivm
, args
, key
, key
->inputs
[slot
].src_index
, attribs
);
613 if (key
->flatshade_first
) {
614 emit_constant_coef4(gallivm
, args
, slot
+1, attribs
[0]);
617 emit_constant_coef4(gallivm
, args
, slot
+1, attribs
[2]);
621 case LP_INTERP_LINEAR
:
622 load_attribute(gallivm
, args
, key
, key
->inputs
[slot
].src_index
, attribs
);
623 emit_apply_cyl_wrap(gallivm
, args
, key
->inputs
[slot
].cyl_wrap
, attribs
);
624 emit_linear_coef(gallivm
, args
, slot
+1, attribs
);
627 case LP_INTERP_PERSPECTIVE
:
628 load_attribute(gallivm
, args
, key
, key
->inputs
[slot
].src_index
, attribs
);
629 emit_apply_cyl_wrap(gallivm
, args
, key
->inputs
[slot
].cyl_wrap
, attribs
);
630 apply_perspective_corr(gallivm
, args
, slot
+1, attribs
);
631 emit_linear_coef(gallivm
, args
, slot
+1, attribs
);
634 case LP_INTERP_POSITION
:
636 * The generated pixel interpolators will pick up the coeffs from
641 case LP_INTERP_FACING
:
642 emit_facing_coef(gallivm
, args
, slot
+1);
652 /* XXX: generic code:
655 set_noalias(LLVMBuilderRef builder
,
656 LLVMValueRef function
,
657 const LLVMTypeRef
*arg_types
,
661 for(i
= 0; i
< nr_args
; ++i
)
662 if(LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
663 LLVMAddAttribute(LLVMGetParam(function
, i
),
664 LLVMNoAliasAttribute
);
668 init_args(struct gallivm_state
*gallivm
,
669 const struct lp_setup_variant_key
*key
,
670 struct lp_setup_args
*args
)
672 LLVMBuilderRef b
= gallivm
->builder
;
673 LLVMTypeRef shuf_type
= LLVMInt32TypeInContext(gallivm
->context
);
674 LLVMValueRef onef
= lp_build_const_float(gallivm
, 1.0);
675 LLVMValueRef onei
= lp_build_const_int32(gallivm
, 1);
676 LLVMValueRef zeroi
= lp_build_const_int32(gallivm
, 0);
677 LLVMValueRef pixel_center
, xy0_center
, dxy01
, dxy20
, dyx20
;
678 LLVMValueRef e
, f
, ef
, ooa
;
679 LLVMValueRef shuffles
[4];
680 LLVMValueRef attr_pos
[3];
681 struct lp_type typef4
= lp_type_float_vec(32, 128);
683 /* The internal position input is in slot zero:
685 load_attribute(gallivm
, args
, key
, 0, attr_pos
);
687 pixel_center
= lp_build_const_vec(gallivm
, typef4
,
688 key
->pixel_center_half
? 0.5 : 0.0);
691 * xy are first two elems in v0a/v1a/v2a but just use vec4 arit
692 * also offset_tri uses actually xyz in them
694 xy0_center
= LLVMBuildFSub(b
, attr_pos
[0], pixel_center
, "xy0_center" );
696 dxy01
= LLVMBuildFSub(b
, attr_pos
[0], attr_pos
[1], "dxy01");
697 dxy20
= LLVMBuildFSub(b
, attr_pos
[2], attr_pos
[0], "dxy20");
701 shuffles
[2] = LLVMGetUndef(shuf_type
);
702 shuffles
[3] = LLVMGetUndef(shuf_type
);
704 dyx20
= LLVMBuildShuffleVector(b
, dxy20
, dxy20
, LLVMConstVector(shuffles
, 4), "");
706 ef
= LLVMBuildFMul(b
, dxy01
, dyx20
, "ef");
707 e
= LLVMBuildExtractElement(b
, ef
, zeroi
, "");
708 f
= LLVMBuildExtractElement(b
, ef
, onei
, "");
710 ooa
= LLVMBuildFDiv(b
, onef
, LLVMBuildFSub(b
, e
, f
, ""), "ooa");
712 ooa
= vec4f_from_scalar(gallivm
, ooa
, "");
714 /* tri offset calc shares a lot of arithmetic, do it here */
715 if (key
->pgon_offset_scale
!= 0.0f
|| key
->pgon_offset_units
!= 0.0f
) {
716 lp_do_offset_tri(gallivm
, args
, key
, ooa
, dxy01
, dxy20
, attr_pos
);
719 dxy20
= LLVMBuildFMul(b
, dxy20
, ooa
, "");
720 dxy01
= LLVMBuildFMul(b
, dxy01
, ooa
, "");
722 args
->dy20_ooa
= lp_build_extract_broadcast(gallivm
, typef4
, typef4
, dxy20
, onei
);
723 args
->dy01_ooa
= lp_build_extract_broadcast(gallivm
, typef4
, typef4
, dxy01
, onei
);
725 args
->dx20_ooa
= lp_build_extract_broadcast(gallivm
, typef4
, typef4
, dxy20
, zeroi
);
726 args
->dx01_ooa
= lp_build_extract_broadcast(gallivm
, typef4
, typef4
, dxy01
, zeroi
);
728 args
->x0_center
= lp_build_extract_broadcast(gallivm
, typef4
, typef4
, xy0_center
, zeroi
);
729 args
->y0_center
= lp_build_extract_broadcast(gallivm
, typef4
, typef4
, xy0_center
, onei
);
731 /* might want to merge that with other coef emit in the future */
732 emit_position_coef(gallivm
, args
, 0, attr_pos
);
736 * Generate the runtime callable function for the coefficient calculation.
739 static struct lp_setup_variant
*
740 generate_setup_variant(struct lp_setup_variant_key
*key
,
741 struct llvmpipe_context
*lp
)
743 struct lp_setup_variant
*variant
= NULL
;
744 struct gallivm_state
*gallivm
;
745 struct lp_setup_args args
;
747 LLVMTypeRef vec4f_type
;
748 LLVMTypeRef func_type
;
749 LLVMTypeRef arg_types
[7];
750 LLVMBasicBlockRef block
;
751 LLVMBuilderRef builder
;
757 variant
= CALLOC_STRUCT(lp_setup_variant
);
761 variant
->gallivm
= gallivm
= gallivm_create();
762 if (!variant
->gallivm
) {
766 builder
= gallivm
->builder
;
768 if (LP_DEBUG
& DEBUG_COUNTERS
) {
772 memcpy(&variant
->key
, key
, key
->size
);
773 variant
->list_item_global
.base
= variant
;
775 util_snprintf(func_name
, sizeof(func_name
), "fs%u_setup%u",
779 /* Currently always deal with full 4-wide vertex attributes from
783 vec4f_type
= LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), 4);
785 arg_types
[0] = LLVMPointerType(vec4f_type
, 0); /* v0 */
786 arg_types
[1] = LLVMPointerType(vec4f_type
, 0); /* v1 */
787 arg_types
[2] = LLVMPointerType(vec4f_type
, 0); /* v2 */
788 arg_types
[3] = LLVMInt32TypeInContext(gallivm
->context
); /* facing */
789 arg_types
[4] = LLVMPointerType(vec4f_type
, 0); /* a0, aligned */
790 arg_types
[5] = LLVMPointerType(vec4f_type
, 0); /* dadx, aligned */
791 arg_types
[6] = LLVMPointerType(vec4f_type
, 0); /* dady, aligned */
793 func_type
= LLVMFunctionType(LLVMVoidTypeInContext(gallivm
->context
),
794 arg_types
, Elements(arg_types
), 0);
796 variant
->function
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
797 if (!variant
->function
)
800 LLVMSetFunctionCallConv(variant
->function
, LLVMCCallConv
);
802 args
.v0
= LLVMGetParam(variant
->function
, 0);
803 args
.v1
= LLVMGetParam(variant
->function
, 1);
804 args
.v2
= LLVMGetParam(variant
->function
, 2);
805 args
.facing
= LLVMGetParam(variant
->function
, 3);
806 args
.a0
= LLVMGetParam(variant
->function
, 4);
807 args
.dadx
= LLVMGetParam(variant
->function
, 5);
808 args
.dady
= LLVMGetParam(variant
->function
, 6);
810 lp_build_name(args
.v0
, "in_v0");
811 lp_build_name(args
.v1
, "in_v1");
812 lp_build_name(args
.v2
, "in_v2");
813 lp_build_name(args
.facing
, "in_facing");
814 lp_build_name(args
.a0
, "out_a0");
815 lp_build_name(args
.dadx
, "out_dadx");
816 lp_build_name(args
.dady
, "out_dady");
821 block
= LLVMAppendBasicBlockInContext(gallivm
->context
,
822 variant
->function
, "entry");
823 LLVMPositionBuilderAtEnd(builder
, block
);
825 set_noalias(builder
, variant
->function
, arg_types
, Elements(arg_types
));
826 init_args(gallivm
, &variant
->key
, &args
);
827 emit_tri_coef(gallivm
, &variant
->key
, &args
);
829 LLVMBuildRetVoid(builder
);
831 gallivm_verify_function(gallivm
, variant
->function
);
833 gallivm_compile_module(gallivm
);
835 variant
->jit_function
= (lp_jit_setup_triangle
)
836 gallivm_jit_function(gallivm
, variant
->function
);
837 if (!variant
->jit_function
)
841 * Update timing information:
843 if (LP_DEBUG
& DEBUG_COUNTERS
) {
845 LP_COUNT_ADD(llvm_compile_time
, t1
- t0
);
846 LP_COUNT_ADD(nr_llvm_compiles
, 1);
853 if (variant
->function
) {
854 gallivm_free_function(gallivm
,
856 variant
->jit_function
);
858 if (variant
->gallivm
) {
859 gallivm_destroy(variant
->gallivm
);
870 lp_make_setup_variant_key(struct llvmpipe_context
*lp
,
871 struct lp_setup_variant_key
*key
)
873 struct lp_fragment_shader
*fs
= lp
->fs
;
876 assert(sizeof key
->inputs
[0] == sizeof(uint
));
878 key
->num_inputs
= fs
->info
.base
.num_inputs
;
879 key
->flatshade_first
= lp
->rasterizer
->flatshade_first
;
880 key
->pixel_center_half
= lp
->rasterizer
->half_pixel_center
;
881 key
->twoside
= lp
->rasterizer
->light_twoside
;
882 key
->size
= Offset(struct lp_setup_variant_key
,
883 inputs
[key
->num_inputs
]);
885 key
->color_slot
= lp
->color_slot
[0];
886 key
->bcolor_slot
= lp
->bcolor_slot
[0];
887 key
->spec_slot
= lp
->color_slot
[1];
888 key
->bspec_slot
= lp
->bcolor_slot
[1];
889 assert(key
->color_slot
== lp
->color_slot
[0]);
890 assert(key
->bcolor_slot
== lp
->bcolor_slot
[0]);
891 assert(key
->spec_slot
== lp
->color_slot
[1]);
892 assert(key
->bspec_slot
== lp
->bcolor_slot
[1]);
895 * If depth is floating point, depth bias is calculated with respect
896 * to the primitive's maximum Z value. Retain the original depth bias
897 * value until that stage.
899 key
->floating_point_depth
= lp
->floating_point_depth
;
901 if (key
->floating_point_depth
) {
902 key
->pgon_offset_units
= (float) lp
->rasterizer
->offset_units
;
904 key
->pgon_offset_units
=
905 (float) (lp
->rasterizer
->offset_units
* lp
->mrd
);
908 key
->pgon_offset_scale
= lp
->rasterizer
->offset_scale
;
909 key
->pgon_offset_clamp
= lp
->rasterizer
->offset_clamp
;
911 memcpy(key
->inputs
, fs
->inputs
, key
->num_inputs
* sizeof key
->inputs
[0]);
912 for (i
= 0; i
< key
->num_inputs
; i
++) {
913 if (key
->inputs
[i
].interp
== LP_INTERP_COLOR
) {
914 if (lp
->rasterizer
->flatshade
)
915 key
->inputs
[i
].interp
= LP_INTERP_CONSTANT
;
917 key
->inputs
[i
].interp
= LP_INTERP_PERSPECTIVE
;
925 remove_setup_variant(struct llvmpipe_context
*lp
,
926 struct lp_setup_variant
*variant
)
928 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
929 debug_printf("llvmpipe: del setup_variant #%u total %u\n",
930 variant
->no
, lp
->nr_setup_variants
);
933 if (variant
->function
) {
934 gallivm_free_function(variant
->gallivm
,
936 variant
->jit_function
);
939 if (variant
->gallivm
) {
940 gallivm_destroy(variant
->gallivm
);
943 remove_from_list(&variant
->list_item_global
);
944 lp
->nr_setup_variants
--;
950 /* When the number of setup variants exceeds a threshold, cull a
951 * fraction (currently a quarter) of them.
954 cull_setup_variants(struct llvmpipe_context
*lp
)
956 struct pipe_context
*pipe
= &lp
->pipe
;
960 * XXX: we need to flush the context until we have some sort of reference
961 * counting in fragment shaders as they may still be binned
962 * Flushing alone might not be sufficient we need to wait on it too.
964 llvmpipe_finish(pipe
, __FUNCTION__
);
966 for (i
= 0; i
< LP_MAX_SETUP_VARIANTS
/ 4; i
++) {
967 struct lp_setup_variant_list_item
*item
;
968 if (is_empty_list(&lp
->setup_variants_list
)) {
971 item
= last_elem(&lp
->setup_variants_list
);
974 remove_setup_variant(lp
, item
->base
);
980 * Update fragment/vertex shader linkage state. This is called just
981 * prior to drawing something when some fragment-related state has
985 llvmpipe_update_setup(struct llvmpipe_context
*lp
)
987 struct lp_setup_variant_key
*key
= &lp
->setup_variant
.key
;
988 struct lp_setup_variant
*variant
= NULL
;
989 struct lp_setup_variant_list_item
*li
;
991 lp_make_setup_variant_key(lp
, key
);
993 foreach(li
, &lp
->setup_variants_list
) {
994 if(li
->base
->key
.size
== key
->size
&&
995 memcmp(&li
->base
->key
, key
, key
->size
) == 0) {
1002 move_to_head(&lp
->setup_variants_list
, &variant
->list_item_global
);
1005 if (lp
->nr_setup_variants
>= LP_MAX_SETUP_VARIANTS
) {
1006 cull_setup_variants(lp
);
1009 variant
= generate_setup_variant(key
, lp
);
1011 insert_at_head(&lp
->setup_variants_list
, &variant
->list_item_global
);
1012 lp
->nr_setup_variants
++;
1013 llvmpipe_variant_count
++;
1017 lp_setup_set_setup_variant(lp
->setup
,
1022 lp_delete_setup_variants(struct llvmpipe_context
*lp
)
1024 struct lp_setup_variant_list_item
*li
;
1025 li
= first_elem(&lp
->setup_variants_list
);
1026 while(!at_end(&lp
->setup_variants_list
, li
)) {
1027 struct lp_setup_variant_list_item
*next
= next_elem(li
);
1028 remove_setup_variant(lp
, li
->base
);
1034 lp_dump_setup_coef( const struct lp_setup_variant_key
*key
,
1035 const float (*sa0
)[4],
1036 const float (*sdadx
)[4],
1037 const float (*sdady
)[4])
1041 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
1042 float a0
= sa0
[0][i
];
1043 float dadx
= sdadx
[0][i
];
1044 float dady
= sdady
[0][i
];
1046 debug_printf("POS.%c: a0 = %f, dadx = %f, dady = %f\n",
1051 for (slot
= 0; slot
< key
->num_inputs
; slot
++) {
1052 unsigned usage_mask
= key
->inputs
[slot
].usage_mask
;
1053 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
1054 if (usage_mask
& (1 << i
)) {
1055 float a0
= sa0
[1 + slot
][i
];
1056 float dadx
= sdadx
[1 + slot
][i
];
1057 float dady
= sdady
[1 + slot
][i
];
1059 debug_printf("IN[%u].%c: a0 = %f, dadx = %f, dady = %f\n",