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_bitarit.h"
35 #include "gallivm/lp_bld_const.h"
36 #include "gallivm/lp_bld_debug.h"
37 #include "gallivm/lp_bld_init.h"
38 #include "gallivm/lp_bld_logic.h"
39 #include "gallivm/lp_bld_intr.h"
40 #include "gallivm/lp_bld_flow.h"
41 #include "gallivm/lp_bld_type.h"
46 #include "lp_screen.h"
47 #include "lp_context.h"
49 #include "lp_state_fs.h"
50 #include "lp_state_setup.h"
54 /* currently organized to interpolate full float[4] attributes even
55 * when some elements are unused. Later, can pack vertex data more
62 /* Function arguments:
67 LLVMValueRef facing
; /* boolean */
74 LLVMValueRef x0_center
;
75 LLVMValueRef y0_center
;
76 LLVMValueRef dy20_ooa
;
77 LLVMValueRef dy01_ooa
;
78 LLVMValueRef dx20_ooa
;
79 LLVMValueRef dx01_ooa
;
85 type4f(struct gallivm_state
*gallivm
)
87 return LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), 4);
91 /* Equivalent of _mm_setr_ps(a,b,c,d)
94 vec4f(struct gallivm_state
*gallivm
,
95 LLVMValueRef a
, LLVMValueRef b
, LLVMValueRef c
, LLVMValueRef d
,
98 LLVMBuilderRef bld
= gallivm
->builder
;
99 LLVMValueRef i0
= lp_build_const_int32(gallivm
, 0);
100 LLVMValueRef i1
= lp_build_const_int32(gallivm
, 1);
101 LLVMValueRef i2
= lp_build_const_int32(gallivm
, 2);
102 LLVMValueRef i3
= lp_build_const_int32(gallivm
, 3);
104 LLVMValueRef res
= LLVMGetUndef(type4f(gallivm
));
106 res
= LLVMBuildInsertElement(bld
, res
, a
, i0
, "");
107 res
= LLVMBuildInsertElement(bld
, res
, b
, i1
, "");
108 res
= LLVMBuildInsertElement(bld
, res
, c
, i2
, "");
109 res
= LLVMBuildInsertElement(bld
, res
, d
, i3
, name
);
114 /* Equivalent of _mm_set1_ps(a)
117 vec4f_from_scalar(struct gallivm_state
*gallivm
,
121 LLVMBuilderRef bld
= gallivm
->builder
;
122 LLVMValueRef res
= LLVMGetUndef(type4f(gallivm
));
125 for(i
= 0; i
< 4; ++i
) {
126 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
127 res
= LLVMBuildInsertElement(bld
, res
, a
, index
, i
== 3 ? name
: "");
134 store_coef(struct gallivm_state
*gallivm
,
135 struct lp_setup_args
*args
,
141 LLVMBuilderRef builder
= gallivm
->builder
;
142 LLVMValueRef idx
= lp_build_const_int32(gallivm
, slot
);
144 LLVMBuildStore(builder
,
146 LLVMBuildGEP(builder
, args
->a0
, &idx
, 1, ""));
148 LLVMBuildStore(builder
,
150 LLVMBuildGEP(builder
, args
->dadx
, &idx
, 1, ""));
152 LLVMBuildStore(builder
,
154 LLVMBuildGEP(builder
, args
->dady
, &idx
, 1, ""));
160 emit_constant_coef4(struct gallivm_state
*gallivm
,
161 struct lp_setup_args
*args
,
165 LLVMValueRef zero
= lp_build_const_float(gallivm
, 0.0);
166 LLVMValueRef zerovec
= vec4f_from_scalar(gallivm
, zero
, "zero");
167 store_coef(gallivm
, args
, slot
, vert
, zerovec
, zerovec
);
173 * Setup the fragment input attribute with the front-facing value.
174 * \param frontface is the triangle front facing?
177 emit_facing_coef(struct gallivm_state
*gallivm
,
178 struct lp_setup_args
*args
,
181 LLVMBuilderRef builder
= gallivm
->builder
;
182 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
183 LLVMValueRef a0_0
= args
->facing
;
184 LLVMValueRef a0_0f
= LLVMBuildSIToFP(builder
, a0_0
, float_type
, "");
185 LLVMValueRef zero
= lp_build_const_float(gallivm
, 0.0);
186 /* Our face val is either 1 or 0 so we do
187 * face = (val * 2) - 1
190 LLVMValueRef face_val
=
191 LLVMBuildFAdd(builder
,
192 LLVMBuildFMul(builder
, a0_0f
,
193 lp_build_const_float(gallivm
, 2.0),
195 lp_build_const_float(gallivm
, -1.0),
197 LLVMValueRef a0
= vec4f(gallivm
, face_val
, zero
, zero
, zero
, "facing");
198 LLVMValueRef zerovec
= vec4f_from_scalar(gallivm
, zero
, "zero");
200 store_coef(gallivm
, args
, slot
, a0
, zerovec
, zerovec
);
205 vert_attrib(struct gallivm_state
*gallivm
,
211 LLVMBuilderRef b
= gallivm
->builder
;
213 idx
[0] = lp_build_const_int32(gallivm
, attr
);
214 idx
[1] = lp_build_const_int32(gallivm
, elem
);
215 return LLVMBuildLoad(b
, LLVMBuildGEP(b
, vert
, idx
, 2, ""), name
);
220 lp_twoside(struct gallivm_state
*gallivm
,
221 struct lp_setup_args
*args
,
222 const struct lp_setup_variant_key
*key
,
224 LLVMValueRef attribv
[3])
226 LLVMBuilderRef b
= gallivm
->builder
;
227 LLVMValueRef a0_back
, a1_back
, a2_back
;
228 LLVMValueRef idx2
= lp_build_const_int32(gallivm
, bcolor_slot
);
230 LLVMValueRef facing
= args
->facing
;
231 LLVMValueRef front_facing
= LLVMBuildICmp(b
, LLVMIntEQ
, facing
, lp_build_const_int32(gallivm
, 0), ""); /** need i1 for if condition */
233 a0_back
= LLVMBuildLoad(b
, LLVMBuildGEP(b
, args
->v0
, &idx2
, 1, ""), "v0a_back");
234 a1_back
= LLVMBuildLoad(b
, LLVMBuildGEP(b
, args
->v1
, &idx2
, 1, ""), "v1a_back");
235 a2_back
= LLVMBuildLoad(b
, LLVMBuildGEP(b
, args
->v2
, &idx2
, 1, ""), "v2a_back");
237 /* Possibly swap the front and back attrib values,
239 * Prefer select to if so we don't have to worry about phis or
242 attribv
[0] = LLVMBuildSelect(b
, front_facing
, a0_back
, attribv
[0], "");
243 attribv
[1] = LLVMBuildSelect(b
, front_facing
, a1_back
, attribv
[1], "");
244 attribv
[2] = LLVMBuildSelect(b
, front_facing
, a2_back
, attribv
[2], "");
249 lp_do_offset_tri(struct gallivm_state
*gallivm
,
250 struct lp_setup_args
*args
,
251 const struct lp_setup_variant_key
*key
,
252 LLVMValueRef inv_det
,
255 LLVMValueRef attribv
[3])
257 LLVMBuilderRef b
= gallivm
->builder
;
258 struct lp_build_context bld
;
259 struct lp_build_context flt_scalar_bld
;
260 struct lp_build_context int_scalar_bld
;
261 LLVMValueRef zoffset
, mult
;
262 LLVMValueRef z0_new
, z1_new
, z2_new
;
263 LLVMValueRef dzdxdzdy
, dzdx
, dzdy
, dzxyz20
, dyzzx01
, dyzzx01_dzxyz20
, dzx01_dyz20
;
264 LLVMValueRef z0z1
, z0z1z2
;
265 LLVMValueRef max
, max_value
, res12
;
266 LLVMValueRef shuffles
[4];
267 LLVMTypeRef shuf_type
= LLVMInt32TypeInContext(gallivm
->context
);
268 LLVMValueRef onei
= lp_build_const_int32(gallivm
, 1);
269 LLVMValueRef zeroi
= lp_build_const_int32(gallivm
, 0);
270 LLVMValueRef twoi
= lp_build_const_int32(gallivm
, 2);
271 LLVMValueRef threei
= lp_build_const_int32(gallivm
, 3);
273 /* (res12) = cross(e,f).xy */
278 dzxyz20
= LLVMBuildShuffleVector(b
, dxyz20
, dxyz20
, LLVMConstVector(shuffles
, 4), "");
284 dyzzx01
= LLVMBuildShuffleVector(b
, dxyz01
, dxyz01
, LLVMConstVector(shuffles
, 4), "");
286 dyzzx01_dzxyz20
= LLVMBuildFMul(b
, dzxyz20
, dyzzx01
, "dyzzx01_dzxyz20");
289 shuffles
[1] = threei
;
290 shuffles
[2] = LLVMGetUndef(shuf_type
);
291 shuffles
[3] = LLVMGetUndef(shuf_type
);
292 dzx01_dyz20
= LLVMBuildShuffleVector(b
, dyzzx01_dzxyz20
, dyzzx01_dzxyz20
,
293 LLVMConstVector(shuffles
, 4), "");
295 res12
= LLVMBuildFSub(b
, dyzzx01_dzxyz20
, dzx01_dyz20
, "res12");
297 /* dzdx = fabsf(res1 * inv_det), dydx = fabsf(res2 * inv_det)*/
298 lp_build_context_init(&bld
, gallivm
, lp_type_float_vec(32, 128));
299 dzdxdzdy
= LLVMBuildFMul(b
, res12
, inv_det
, "dzdxdzdy");
300 dzdxdzdy
= lp_build_abs(&bld
, dzdxdzdy
);
302 dzdx
= LLVMBuildExtractElement(b
, dzdxdzdy
, zeroi
, "");
303 dzdy
= LLVMBuildExtractElement(b
, dzdxdzdy
, onei
, "");
305 /* mult = MAX2(dzdx, dzdy) * pgon_offset_scale */
306 max
= LLVMBuildFCmp(b
, LLVMRealUGT
, dzdx
, dzdy
, "");
307 max_value
= LLVMBuildSelect(b
, max
, dzdx
, dzdy
, "max");
309 mult
= LLVMBuildFMul(b
, max_value
,
310 lp_build_const_float(gallivm
, key
->pgon_offset_scale
), "");
312 lp_build_context_init(&flt_scalar_bld
, gallivm
, lp_type_float_vec(32, 32));
314 if (key
->floating_point_depth
) {
316 * bias = pgon_offset_units * 2^(exponent(max(z0, z1, z2)) - mantissa_bits) +
317 * MAX2(dzdx, dzdy) * pgon_offset_scale
319 * NOTE: Assumes IEEE float32.
321 LLVMValueRef c23_shifted
, exp_mask
, bias
, exp
;
322 LLVMValueRef maxz_value
, maxz0z1_value
;
324 lp_build_context_init(&int_scalar_bld
, gallivm
, lp_type_int_vec(32, 32));
326 c23_shifted
= lp_build_const_int32(gallivm
, 23 << 23);
327 exp_mask
= lp_build_const_int32(gallivm
, 0xff << 23);
329 maxz0z1_value
= lp_build_max(&flt_scalar_bld
,
330 LLVMBuildExtractElement(b
, attribv
[0], twoi
, ""),
331 LLVMBuildExtractElement(b
, attribv
[1], twoi
, ""));
333 maxz_value
= lp_build_max(&flt_scalar_bld
,
334 LLVMBuildExtractElement(b
, attribv
[2], twoi
, ""),
337 exp
= LLVMBuildBitCast(b
, maxz_value
, int_scalar_bld
.vec_type
, "");
338 exp
= lp_build_and(&int_scalar_bld
, exp
, exp_mask
);
339 exp
= lp_build_sub(&int_scalar_bld
, exp
, c23_shifted
);
340 /* Clamping to zero means mrd will be zero for very small numbers,
341 * but specs do not indicate this should be prevented by clamping
342 * mrd to smallest normal number instead. */
343 exp
= lp_build_max(&int_scalar_bld
, exp
, int_scalar_bld
.zero
);
344 exp
= LLVMBuildBitCast(b
, exp
, flt_scalar_bld
.vec_type
, "");
346 bias
= LLVMBuildFMul(b
, exp
,
347 lp_build_const_float(gallivm
, key
->pgon_offset_units
),
350 zoffset
= LLVMBuildFAdd(b
, bias
, mult
, "zoffset");
353 * bias = pgon_offset_units + MAX2(dzdx, dzdy) * pgon_offset_scale
355 zoffset
= LLVMBuildFAdd(b
,
356 lp_build_const_float(gallivm
, key
->pgon_offset_units
),
360 if (key
->pgon_offset_clamp
> 0) {
361 zoffset
= lp_build_min(&flt_scalar_bld
,
362 lp_build_const_float(gallivm
, key
->pgon_offset_clamp
),
365 else if (key
->pgon_offset_clamp
< 0) {
366 zoffset
= lp_build_max(&flt_scalar_bld
,
367 lp_build_const_float(gallivm
, key
->pgon_offset_clamp
),
373 shuffles
[1] = lp_build_const_int32(gallivm
, 6);
374 shuffles
[2] = LLVMGetUndef(shuf_type
);
375 shuffles
[3] = LLVMGetUndef(shuf_type
);
376 z0z1
= LLVMBuildShuffleVector(b
, attribv
[0], attribv
[1], LLVMConstVector(shuffles
, 4), "");
379 shuffles
[2] = lp_build_const_int32(gallivm
, 6);
380 shuffles
[3] = LLVMGetUndef(shuf_type
);
381 z0z1z2
= LLVMBuildShuffleVector(b
, z0z1
, attribv
[2], LLVMConstVector(shuffles
, 4), "");
382 zoffset
= vec4f_from_scalar(gallivm
, zoffset
, "");
384 /* clamp and do offset */
386 * FIXME I suspect the clamp (is that even right to always clamp to fixed
387 * 0.0/1.0?) should really be per fragment?
389 z0z1z2
= lp_build_clamp(&bld
, LLVMBuildFAdd(b
, z0z1z2
, zoffset
, ""), bld
.zero
, bld
.one
);
391 /* insert into args->a0.z, a1.z, a2.z:
393 z0_new
= LLVMBuildExtractElement(b
, z0z1z2
, zeroi
, "");
394 z1_new
= LLVMBuildExtractElement(b
, z0z1z2
, onei
, "");
395 z2_new
= LLVMBuildExtractElement(b
, z0z1z2
, twoi
, "");
396 attribv
[0] = LLVMBuildInsertElement(b
, attribv
[0], z0_new
, twoi
, "");
397 attribv
[1] = LLVMBuildInsertElement(b
, attribv
[1], z1_new
, twoi
, "");
398 attribv
[2] = LLVMBuildInsertElement(b
, attribv
[2], z2_new
, twoi
, "");
402 load_attribute(struct gallivm_state
*gallivm
,
403 struct lp_setup_args
*args
,
404 const struct lp_setup_variant_key
*key
,
406 LLVMValueRef attribv
[3])
408 LLVMBuilderRef b
= gallivm
->builder
;
409 LLVMValueRef idx
= lp_build_const_int32(gallivm
, vert_attr
);
411 /* Load the vertex data
413 attribv
[0] = LLVMBuildLoad(b
, LLVMBuildGEP(b
, args
->v0
, &idx
, 1, ""), "v0a");
414 attribv
[1] = LLVMBuildLoad(b
, LLVMBuildGEP(b
, args
->v1
, &idx
, 1, ""), "v1a");
415 attribv
[2] = LLVMBuildLoad(b
, LLVMBuildGEP(b
, args
->v2
, &idx
, 1, ""), "v2a");
418 /* Potentially modify it according to twoside, etc:
421 if (vert_attr
== key
->color_slot
&& key
->bcolor_slot
>= 0)
422 lp_twoside(gallivm
, args
, key
, key
->bcolor_slot
, attribv
);
423 else if (vert_attr
== key
->spec_slot
&& key
->bspec_slot
>= 0)
424 lp_twoside(gallivm
, args
, key
, key
->bspec_slot
, attribv
);
429 emit_coef4( struct gallivm_state
*gallivm
,
430 struct lp_setup_args
*args
,
436 LLVMBuilderRef b
= gallivm
->builder
;
437 LLVMValueRef dy20_ooa
= args
->dy20_ooa
;
438 LLVMValueRef dy01_ooa
= args
->dy01_ooa
;
439 LLVMValueRef dx20_ooa
= args
->dx20_ooa
;
440 LLVMValueRef dx01_ooa
= args
->dx01_ooa
;
441 LLVMValueRef x0_center
= args
->x0_center
;
442 LLVMValueRef y0_center
= args
->y0_center
;
444 LLVMValueRef da01
= LLVMBuildFSub(b
, a0
, a1
, "da01");
445 LLVMValueRef da20
= LLVMBuildFSub(b
, a2
, a0
, "da20");
447 /* Calculate dadx (vec4f)
449 LLVMValueRef da01_dy20_ooa
= LLVMBuildFMul(b
, da01
, dy20_ooa
, "da01_dy20_ooa");
450 LLVMValueRef da20_dy01_ooa
= LLVMBuildFMul(b
, da20
, dy01_ooa
, "da20_dy01_ooa");
451 LLVMValueRef dadx
= LLVMBuildFSub(b
, da01_dy20_ooa
, da20_dy01_ooa
, "dadx");
453 /* Calculate dady (vec4f)
455 LLVMValueRef da01_dx20_ooa
= LLVMBuildFMul(b
, da01
, dx20_ooa
, "da01_dx20_ooa");
456 LLVMValueRef da20_dx01_ooa
= LLVMBuildFMul(b
, da20
, dx01_ooa
, "da20_dx01_ooa");
457 LLVMValueRef dady
= LLVMBuildFSub(b
, da20_dx01_ooa
, da01_dx20_ooa
, "dady");
459 /* Calculate a0 - the attribute value at the origin
461 LLVMValueRef dadx_x0
= LLVMBuildFMul(b
, dadx
, x0_center
, "dadx_x0");
462 LLVMValueRef dady_y0
= LLVMBuildFMul(b
, dady
, y0_center
, "dady_y0");
463 LLVMValueRef attr_v0
= LLVMBuildFAdd(b
, dadx_x0
, dady_y0
, "attr_v0");
464 LLVMValueRef attr_0
= LLVMBuildFSub(b
, a0
, attr_v0
, "attr_0");
466 store_coef(gallivm
, args
, slot
, attr_0
, dadx
, dady
);
471 emit_linear_coef( struct gallivm_state
*gallivm
,
472 struct lp_setup_args
*args
,
474 LLVMValueRef attribv
[3])
476 /* nothing to do anymore */
486 * Compute a0, dadx and dady for a perspective-corrected interpolant,
488 * We basically multiply the vertex value by 1/w before computing
489 * the plane coefficients (a0, dadx, dady).
490 * Later, when we compute the value at a particular fragment position we'll
491 * divide the interpolated value by the interpolated W at that fragment.
494 apply_perspective_corr( struct gallivm_state
*gallivm
,
495 struct lp_setup_args
*args
,
497 LLVMValueRef attribv
[3])
499 LLVMBuilderRef b
= gallivm
->builder
;
501 /* premultiply by 1/w (v[0][3] is always 1/w):
503 LLVMValueRef v0_oow
= vec4f_from_scalar(gallivm
, vert_attrib(gallivm
, args
->v0
, 0, 3, ""), "v0_oow");
504 LLVMValueRef v1_oow
= vec4f_from_scalar(gallivm
, vert_attrib(gallivm
, args
->v1
, 0, 3, ""), "v1_oow");
505 LLVMValueRef v2_oow
= vec4f_from_scalar(gallivm
, vert_attrib(gallivm
, args
->v2
, 0, 3, ""), "v2_oow");
507 attribv
[0] = LLVMBuildFMul(b
, attribv
[0], v0_oow
, "v0_oow_v0a");
508 attribv
[1] = LLVMBuildFMul(b
, attribv
[1], v1_oow
, "v1_oow_v1a");
509 attribv
[2] = LLVMBuildFMul(b
, attribv
[2], v2_oow
, "v2_oow_v2a");
514 emit_position_coef( struct gallivm_state
*gallivm
,
515 struct lp_setup_args
*args
,
517 LLVMValueRef attribv
[3])
519 emit_linear_coef(gallivm
, args
, slot
, attribv
);
524 * Applys cylindrical wrapping to vertex attributes if enabled.
525 * Input coordinates must be in [0, 1] range, otherwise results are undefined.
527 * @param cyl_wrap TGSI_CYLINDRICAL_WRAP_x flags
530 emit_apply_cyl_wrap(struct gallivm_state
*gallivm
,
531 struct lp_setup_args
*args
,
533 LLVMValueRef attribv
[3])
536 LLVMBuilderRef builder
= gallivm
->builder
;
537 struct lp_type type
= lp_float32_vec4_type();
538 LLVMTypeRef float_vec_type
= lp_build_vec_type(gallivm
, type
);
539 LLVMValueRef pos_half
;
540 LLVMValueRef neg_half
;
541 LLVMValueRef cyl_mask
;
550 pos_half
= lp_build_const_vec(gallivm
, type
, +0.5f
);
551 neg_half
= lp_build_const_vec(gallivm
, type
, -0.5f
);
552 cyl_mask
= lp_build_const_mask_aos(gallivm
, type
, cyl_wrap
, 4);
554 one
= lp_build_const_vec(gallivm
, type
, 1.0f
);
555 one
= LLVMBuildBitCast(builder
, one
, lp_build_int_vec_type(gallivm
, type
), "");
556 one
= LLVMBuildAnd(builder
, one
, cyl_mask
, "");
559 delta
= LLVMBuildFSub(builder
, attribv
[1], attribv
[0], "");
561 offset
= lp_build_compare(gallivm
, type
, PIPE_FUNC_GREATER
, delta
, pos_half
);
562 offset
= LLVMBuildAnd(builder
, offset
, one
, "");
563 offset
= LLVMBuildBitCast(builder
, offset
, float_vec_type
, "");
564 attribv
[0] = LLVMBuildFAdd(builder
, attribv
[0], offset
, "");
566 offset
= lp_build_compare(gallivm
, type
, PIPE_FUNC_LESS
, delta
, neg_half
);
567 offset
= LLVMBuildAnd(builder
, offset
, one
, "");
568 offset
= LLVMBuildBitCast(builder
, offset
, float_vec_type
, "");
569 attribv
[1] = LLVMBuildFAdd(builder
, attribv
[1], offset
, "");
572 delta
= LLVMBuildFSub(builder
, attribv
[2], attribv
[1], "");
574 offset
= lp_build_compare(gallivm
, type
, PIPE_FUNC_GREATER
, delta
, pos_half
);
575 offset
= LLVMBuildAnd(builder
, offset
, one
, "");
576 offset
= LLVMBuildBitCast(builder
, offset
, float_vec_type
, "");
577 attribv
[1] = LLVMBuildFAdd(builder
, attribv
[1], offset
, "");
579 offset
= lp_build_compare(gallivm
, type
, PIPE_FUNC_LESS
, delta
, neg_half
);
580 offset
= LLVMBuildAnd(builder
, offset
, one
, "");
581 offset
= LLVMBuildBitCast(builder
, offset
, float_vec_type
, "");
582 attribv
[2] = LLVMBuildFAdd(builder
, attribv
[2], offset
, "");
585 delta
= LLVMBuildFSub(builder
, attribv
[0], attribv
[2], "");
587 offset
= lp_build_compare(gallivm
, type
, PIPE_FUNC_GREATER
, delta
, pos_half
);
588 offset
= LLVMBuildAnd(builder
, offset
, one
, "");
589 offset
= LLVMBuildBitCast(builder
, offset
, float_vec_type
, "");
590 attribv
[2] = LLVMBuildFAdd(builder
, attribv
[2], offset
, "");
592 offset
= lp_build_compare(gallivm
, type
, PIPE_FUNC_LESS
, delta
, neg_half
);
593 offset
= LLVMBuildAnd(builder
, offset
, one
, "");
594 offset
= LLVMBuildBitCast(builder
, offset
, float_vec_type
, "");
595 attribv
[0] = LLVMBuildFAdd(builder
, attribv
[0], offset
, "");
600 * Compute the inputs-> dadx, dady, a0 values.
603 emit_tri_coef( struct gallivm_state
*gallivm
,
604 const struct lp_setup_variant_key
*key
,
605 struct lp_setup_args
*args
)
609 LLVMValueRef attribs
[3];
611 /* setup interpolation for all the remaining attributes:
613 for (slot
= 0; slot
< key
->num_inputs
; slot
++) {
614 switch (key
->inputs
[slot
].interp
) {
615 case LP_INTERP_CONSTANT
:
616 load_attribute(gallivm
, args
, key
, key
->inputs
[slot
].src_index
, attribs
);
617 if (key
->flatshade_first
) {
618 emit_constant_coef4(gallivm
, args
, slot
+1, attribs
[0]);
621 emit_constant_coef4(gallivm
, args
, slot
+1, attribs
[2]);
625 case LP_INTERP_LINEAR
:
626 load_attribute(gallivm
, args
, key
, key
->inputs
[slot
].src_index
, attribs
);
627 emit_apply_cyl_wrap(gallivm
, args
, key
->inputs
[slot
].cyl_wrap
, attribs
);
628 emit_linear_coef(gallivm
, args
, slot
+1, attribs
);
631 case LP_INTERP_PERSPECTIVE
:
632 load_attribute(gallivm
, args
, key
, key
->inputs
[slot
].src_index
, attribs
);
633 emit_apply_cyl_wrap(gallivm
, args
, key
->inputs
[slot
].cyl_wrap
, attribs
);
634 apply_perspective_corr(gallivm
, args
, slot
+1, attribs
);
635 emit_linear_coef(gallivm
, args
, slot
+1, attribs
);
638 case LP_INTERP_POSITION
:
640 * The generated pixel interpolators will pick up the coeffs from
645 case LP_INTERP_FACING
:
646 emit_facing_coef(gallivm
, args
, slot
+1);
656 /* XXX: generic code:
659 set_noalias(LLVMBuilderRef builder
,
660 LLVMValueRef function
,
661 const LLVMTypeRef
*arg_types
,
665 for(i
= 0; i
< nr_args
; ++i
)
666 if(LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
667 LLVMAddAttribute(LLVMGetParam(function
, i
),
668 LLVMNoAliasAttribute
);
672 init_args(struct gallivm_state
*gallivm
,
673 const struct lp_setup_variant_key
*key
,
674 struct lp_setup_args
*args
)
676 LLVMBuilderRef b
= gallivm
->builder
;
677 LLVMTypeRef shuf_type
= LLVMInt32TypeInContext(gallivm
->context
);
678 LLVMValueRef onef
= lp_build_const_float(gallivm
, 1.0);
679 LLVMValueRef onei
= lp_build_const_int32(gallivm
, 1);
680 LLVMValueRef zeroi
= lp_build_const_int32(gallivm
, 0);
681 LLVMValueRef pixel_center
, xy0_center
, dxy01
, dxy20
, dyx20
;
682 LLVMValueRef e
, f
, ef
, ooa
;
683 LLVMValueRef shuffles
[4];
684 LLVMValueRef attr_pos
[3];
685 struct lp_type typef4
= lp_type_float_vec(32, 128);
687 /* The internal position input is in slot zero:
689 load_attribute(gallivm
, args
, key
, 0, attr_pos
);
691 pixel_center
= lp_build_const_vec(gallivm
, typef4
,
692 key
->pixel_center_half
? 0.5 : 0.0);
695 * xy are first two elems in v0a/v1a/v2a but just use vec4 arit
696 * also offset_tri uses actually xyz in them
698 xy0_center
= LLVMBuildFSub(b
, attr_pos
[0], pixel_center
, "xy0_center" );
700 dxy01
= LLVMBuildFSub(b
, attr_pos
[0], attr_pos
[1], "dxy01");
701 dxy20
= LLVMBuildFSub(b
, attr_pos
[2], attr_pos
[0], "dxy20");
705 shuffles
[2] = LLVMGetUndef(shuf_type
);
706 shuffles
[3] = LLVMGetUndef(shuf_type
);
708 dyx20
= LLVMBuildShuffleVector(b
, dxy20
, dxy20
, LLVMConstVector(shuffles
, 4), "");
710 ef
= LLVMBuildFMul(b
, dxy01
, dyx20
, "ef");
711 e
= LLVMBuildExtractElement(b
, ef
, zeroi
, "");
712 f
= LLVMBuildExtractElement(b
, ef
, onei
, "");
714 ooa
= LLVMBuildFDiv(b
, onef
, LLVMBuildFSub(b
, e
, f
, ""), "ooa");
716 ooa
= vec4f_from_scalar(gallivm
, ooa
, "");
718 /* tri offset calc shares a lot of arithmetic, do it here */
719 if (key
->pgon_offset_scale
!= 0.0f
|| key
->pgon_offset_units
!= 0.0f
) {
720 lp_do_offset_tri(gallivm
, args
, key
, ooa
, dxy01
, dxy20
, attr_pos
);
723 dxy20
= LLVMBuildFMul(b
, dxy20
, ooa
, "");
724 dxy01
= LLVMBuildFMul(b
, dxy01
, ooa
, "");
726 args
->dy20_ooa
= lp_build_extract_broadcast(gallivm
, typef4
, typef4
, dxy20
, onei
);
727 args
->dy01_ooa
= lp_build_extract_broadcast(gallivm
, typef4
, typef4
, dxy01
, onei
);
729 args
->dx20_ooa
= lp_build_extract_broadcast(gallivm
, typef4
, typef4
, dxy20
, zeroi
);
730 args
->dx01_ooa
= lp_build_extract_broadcast(gallivm
, typef4
, typef4
, dxy01
, zeroi
);
732 args
->x0_center
= lp_build_extract_broadcast(gallivm
, typef4
, typef4
, xy0_center
, zeroi
);
733 args
->y0_center
= lp_build_extract_broadcast(gallivm
, typef4
, typef4
, xy0_center
, onei
);
735 /* might want to merge that with other coef emit in the future */
736 emit_position_coef(gallivm
, args
, 0, attr_pos
);
740 * Generate the runtime callable function for the coefficient calculation.
743 static struct lp_setup_variant
*
744 generate_setup_variant(struct lp_setup_variant_key
*key
,
745 struct llvmpipe_context
*lp
)
747 struct lp_setup_variant
*variant
= NULL
;
748 struct gallivm_state
*gallivm
;
749 struct lp_setup_args args
;
751 LLVMTypeRef vec4f_type
;
752 LLVMTypeRef func_type
;
753 LLVMTypeRef arg_types
[7];
754 LLVMBasicBlockRef block
;
755 LLVMBuilderRef builder
;
761 variant
= CALLOC_STRUCT(lp_setup_variant
);
765 variant
->gallivm
= gallivm
= gallivm_create();
766 if (!variant
->gallivm
) {
770 builder
= gallivm
->builder
;
772 if (LP_DEBUG
& DEBUG_COUNTERS
) {
776 memcpy(&variant
->key
, key
, key
->size
);
777 variant
->list_item_global
.base
= variant
;
779 util_snprintf(func_name
, sizeof(func_name
), "fs%u_setup%u",
783 /* Currently always deal with full 4-wide vertex attributes from
787 vec4f_type
= LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), 4);
789 arg_types
[0] = LLVMPointerType(vec4f_type
, 0); /* v0 */
790 arg_types
[1] = LLVMPointerType(vec4f_type
, 0); /* v1 */
791 arg_types
[2] = LLVMPointerType(vec4f_type
, 0); /* v2 */
792 arg_types
[3] = LLVMInt32TypeInContext(gallivm
->context
); /* facing */
793 arg_types
[4] = LLVMPointerType(vec4f_type
, 0); /* a0, aligned */
794 arg_types
[5] = LLVMPointerType(vec4f_type
, 0); /* dadx, aligned */
795 arg_types
[6] = LLVMPointerType(vec4f_type
, 0); /* dady, aligned */
797 func_type
= LLVMFunctionType(LLVMVoidTypeInContext(gallivm
->context
),
798 arg_types
, Elements(arg_types
), 0);
800 variant
->function
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
801 if (!variant
->function
)
804 LLVMSetFunctionCallConv(variant
->function
, LLVMCCallConv
);
806 args
.v0
= LLVMGetParam(variant
->function
, 0);
807 args
.v1
= LLVMGetParam(variant
->function
, 1);
808 args
.v2
= LLVMGetParam(variant
->function
, 2);
809 args
.facing
= LLVMGetParam(variant
->function
, 3);
810 args
.a0
= LLVMGetParam(variant
->function
, 4);
811 args
.dadx
= LLVMGetParam(variant
->function
, 5);
812 args
.dady
= LLVMGetParam(variant
->function
, 6);
814 lp_build_name(args
.v0
, "in_v0");
815 lp_build_name(args
.v1
, "in_v1");
816 lp_build_name(args
.v2
, "in_v2");
817 lp_build_name(args
.facing
, "in_facing");
818 lp_build_name(args
.a0
, "out_a0");
819 lp_build_name(args
.dadx
, "out_dadx");
820 lp_build_name(args
.dady
, "out_dady");
825 block
= LLVMAppendBasicBlockInContext(gallivm
->context
,
826 variant
->function
, "entry");
827 LLVMPositionBuilderAtEnd(builder
, block
);
829 set_noalias(builder
, variant
->function
, arg_types
, Elements(arg_types
));
830 init_args(gallivm
, &variant
->key
, &args
);
831 emit_tri_coef(gallivm
, &variant
->key
, &args
);
833 LLVMBuildRetVoid(builder
);
835 gallivm_verify_function(gallivm
, variant
->function
);
837 gallivm_compile_module(gallivm
);
839 variant
->jit_function
= (lp_jit_setup_triangle
)
840 gallivm_jit_function(gallivm
, variant
->function
);
841 if (!variant
->jit_function
)
845 * Update timing information:
847 if (LP_DEBUG
& DEBUG_COUNTERS
) {
849 LP_COUNT_ADD(llvm_compile_time
, t1
- t0
);
850 LP_COUNT_ADD(nr_llvm_compiles
, 1);
857 if (variant
->function
) {
858 gallivm_free_function(gallivm
,
860 variant
->jit_function
);
862 if (variant
->gallivm
) {
863 gallivm_destroy(variant
->gallivm
);
874 lp_make_setup_variant_key(struct llvmpipe_context
*lp
,
875 struct lp_setup_variant_key
*key
)
877 struct lp_fragment_shader
*fs
= lp
->fs
;
880 assert(sizeof key
->inputs
[0] == sizeof(uint
));
882 key
->num_inputs
= fs
->info
.base
.num_inputs
;
883 key
->flatshade_first
= lp
->rasterizer
->flatshade_first
;
884 key
->pixel_center_half
= lp
->rasterizer
->half_pixel_center
;
885 key
->twoside
= lp
->rasterizer
->light_twoside
;
886 key
->size
= Offset(struct lp_setup_variant_key
,
887 inputs
[key
->num_inputs
]);
889 key
->color_slot
= lp
->color_slot
[0];
890 key
->bcolor_slot
= lp
->bcolor_slot
[0];
891 key
->spec_slot
= lp
->color_slot
[1];
892 key
->bspec_slot
= lp
->bcolor_slot
[1];
893 assert(key
->color_slot
== lp
->color_slot
[0]);
894 assert(key
->bcolor_slot
== lp
->bcolor_slot
[0]);
895 assert(key
->spec_slot
== lp
->color_slot
[1]);
896 assert(key
->bspec_slot
== lp
->bcolor_slot
[1]);
899 * If depth is floating point, depth bias is calculated with respect
900 * to the primitive's maximum Z value. Retain the original depth bias
901 * value until that stage.
903 key
->floating_point_depth
= lp
->floating_point_depth
;
905 if (key
->floating_point_depth
) {
906 key
->pgon_offset_units
= (float) lp
->rasterizer
->offset_units
;
908 key
->pgon_offset_units
=
909 (float) (lp
->rasterizer
->offset_units
* lp
->mrd
);
912 key
->pgon_offset_scale
= lp
->rasterizer
->offset_scale
;
913 key
->pgon_offset_clamp
= lp
->rasterizer
->offset_clamp
;
915 memcpy(key
->inputs
, fs
->inputs
, key
->num_inputs
* sizeof key
->inputs
[0]);
916 for (i
= 0; i
< key
->num_inputs
; i
++) {
917 if (key
->inputs
[i
].interp
== LP_INTERP_COLOR
) {
918 if (lp
->rasterizer
->flatshade
)
919 key
->inputs
[i
].interp
= LP_INTERP_CONSTANT
;
921 key
->inputs
[i
].interp
= LP_INTERP_PERSPECTIVE
;
929 remove_setup_variant(struct llvmpipe_context
*lp
,
930 struct lp_setup_variant
*variant
)
932 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
933 debug_printf("llvmpipe: del setup_variant #%u total %u\n",
934 variant
->no
, lp
->nr_setup_variants
);
937 if (variant
->function
) {
938 gallivm_free_function(variant
->gallivm
,
940 variant
->jit_function
);
943 if (variant
->gallivm
) {
944 gallivm_destroy(variant
->gallivm
);
947 remove_from_list(&variant
->list_item_global
);
948 lp
->nr_setup_variants
--;
954 /* When the number of setup variants exceeds a threshold, cull a
955 * fraction (currently a quarter) of them.
958 cull_setup_variants(struct llvmpipe_context
*lp
)
960 struct pipe_context
*pipe
= &lp
->pipe
;
964 * XXX: we need to flush the context until we have some sort of reference
965 * counting in fragment shaders as they may still be binned
966 * Flushing alone might not be sufficient we need to wait on it too.
968 llvmpipe_finish(pipe
, __FUNCTION__
);
970 for (i
= 0; i
< LP_MAX_SETUP_VARIANTS
/ 4; i
++) {
971 struct lp_setup_variant_list_item
*item
;
972 if (is_empty_list(&lp
->setup_variants_list
)) {
975 item
= last_elem(&lp
->setup_variants_list
);
978 remove_setup_variant(lp
, item
->base
);
984 * Update fragment/vertex shader linkage state. This is called just
985 * prior to drawing something when some fragment-related state has
989 llvmpipe_update_setup(struct llvmpipe_context
*lp
)
991 struct lp_setup_variant_key
*key
= &lp
->setup_variant
.key
;
992 struct lp_setup_variant
*variant
= NULL
;
993 struct lp_setup_variant_list_item
*li
;
995 lp_make_setup_variant_key(lp
, key
);
997 foreach(li
, &lp
->setup_variants_list
) {
998 if(li
->base
->key
.size
== key
->size
&&
999 memcmp(&li
->base
->key
, key
, key
->size
) == 0) {
1006 move_to_head(&lp
->setup_variants_list
, &variant
->list_item_global
);
1009 if (lp
->nr_setup_variants
>= LP_MAX_SETUP_VARIANTS
) {
1010 cull_setup_variants(lp
);
1013 variant
= generate_setup_variant(key
, lp
);
1015 insert_at_head(&lp
->setup_variants_list
, &variant
->list_item_global
);
1016 lp
->nr_setup_variants
++;
1017 llvmpipe_variant_count
++;
1021 lp_setup_set_setup_variant(lp
->setup
,
1026 lp_delete_setup_variants(struct llvmpipe_context
*lp
)
1028 struct lp_setup_variant_list_item
*li
;
1029 li
= first_elem(&lp
->setup_variants_list
);
1030 while(!at_end(&lp
->setup_variants_list
, li
)) {
1031 struct lp_setup_variant_list_item
*next
= next_elem(li
);
1032 remove_setup_variant(lp
, li
->base
);
1038 lp_dump_setup_coef( const struct lp_setup_variant_key
*key
,
1039 const float (*sa0
)[4],
1040 const float (*sdadx
)[4],
1041 const float (*sdady
)[4])
1045 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
1046 float a0
= sa0
[0][i
];
1047 float dadx
= sdadx
[0][i
];
1048 float dady
= sdady
[0][i
];
1050 debug_printf("POS.%c: a0 = %f, dadx = %f, dady = %f\n",
1055 for (slot
= 0; slot
< key
->num_inputs
; slot
++) {
1056 unsigned usage_mask
= key
->inputs
[slot
].usage_mask
;
1057 for (i
= 0; i
< TGSI_NUM_CHANNELS
; i
++) {
1058 if (usage_mask
& (1 << i
)) {
1059 float a0
= sa0
[1 + slot
][i
];
1060 float dadx
= sdadx
[1 + slot
][i
];
1061 float dady
= sdady
[1 + slot
][i
];
1063 debug_printf("IN[%u].%c: a0 = %f, dadx = %f, dady = %f\n",