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_debug.h"
35 #include "gallivm/lp_bld_init.h"
36 #include "gallivm/lp_bld_intr.h"
37 #include "gallivm/lp_bld_flow.h"
38 #include "gallivm/lp_bld_type.h"
39 #include <llvm-c/Analysis.h> /* for LLVMVerifyFunction */
44 #include "lp_screen.h"
45 #include "lp_context.h"
46 #include "lp_setup_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
;
81 /* Temporary, per-attribute:
88 static LLVMTypeRef
type4f(void)
90 return LLVMVectorType(LLVMFloatType(), 4);
94 /* Equivalent of _mm_setr_ps(a,b,c,d)
96 static LLVMValueRef
vec4f(LLVMBuilderRef bld
,
97 LLVMValueRef a
, LLVMValueRef b
, LLVMValueRef c
, LLVMValueRef d
,
100 LLVMValueRef i0
= LLVMConstInt(LLVMInt32Type(), 0, 0);
101 LLVMValueRef i1
= LLVMConstInt(LLVMInt32Type(), 1, 0);
102 LLVMValueRef i2
= LLVMConstInt(LLVMInt32Type(), 2, 0);
103 LLVMValueRef i3
= LLVMConstInt(LLVMInt32Type(), 3, 0);
105 LLVMValueRef res
= LLVMGetUndef(type4f());
107 res
= LLVMBuildInsertElement(bld
, res
, a
, i0
, "");
108 res
= LLVMBuildInsertElement(bld
, res
, b
, i1
, "");
109 res
= LLVMBuildInsertElement(bld
, res
, c
, i2
, "");
110 res
= LLVMBuildInsertElement(bld
, res
, d
, i3
, name
);
115 /* Equivalent of _mm_set1_ps(a)
117 static LLVMValueRef
vec4f_from_scalar(LLVMBuilderRef bld
,
121 LLVMValueRef res
= LLVMGetUndef(type4f());
124 for(i
= 0; i
< 4; ++i
) {
125 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
126 res
= LLVMBuildInsertElement(bld
, res
, a
, index
, i
== 3 ? name
: "");
133 store_coef(LLVMBuilderRef builder
,
134 struct lp_setup_args
*args
,
140 LLVMValueRef idx
= LLVMConstInt(LLVMInt32Type(), slot
, 0);
142 LLVMBuildStore(builder
,
144 LLVMBuildGEP(builder
, args
->a0
, &idx
, 1, ""));
146 LLVMBuildStore(builder
,
148 LLVMBuildGEP(builder
, args
->dadx
, &idx
, 1, ""));
150 LLVMBuildStore(builder
,
152 LLVMBuildGEP(builder
, args
->dady
, &idx
, 1, ""));
158 emit_constant_coef4( LLVMBuilderRef builder
,
159 struct lp_setup_args
*args
,
163 LLVMValueRef zero
= LLVMConstReal(LLVMFloatType(), 0.0);
164 LLVMValueRef zerovec
= vec4f_from_scalar(builder
, zero
, "zero");
165 store_coef(builder
, args
, slot
, vert
, zerovec
, zerovec
);
171 * Setup the fragment input attribute with the front-facing value.
172 * \param frontface is the triangle front facing?
175 emit_facing_coef( LLVMBuilderRef builder
,
176 struct lp_setup_args
*args
,
179 LLVMValueRef a0_0
= args
->facing
;
180 LLVMValueRef a0_0f
= LLVMBuildSIToFP(builder
, a0_0
, LLVMFloatType(), "");
181 LLVMValueRef zero
= LLVMConstReal(LLVMFloatType(), 0.0);
182 LLVMValueRef a0
= vec4f(builder
, a0_0f
, zero
, zero
, zero
, "facing");
183 LLVMValueRef zerovec
= vec4f_from_scalar(builder
, zero
, "zero");
185 store_coef(builder
, args
, slot
, a0
, zerovec
, zerovec
);
190 vert_attrib(LLVMBuilderRef b
,
197 idx
[0] = LLVMConstInt(LLVMInt32Type(), attr
, 0);
198 idx
[1] = LLVMConstInt(LLVMInt32Type(), elem
, 0);
199 return LLVMBuildLoad(b
, LLVMBuildGEP(b
, vert
, idx
, 2, ""), name
);
203 vert_clamp(LLVMBuilderRef b
,
208 LLVMValueRef min_result
= LLVMBuildFCmp(b
, LLVMRealUGT
, min
, x
, "");
209 LLVMValueRef max_result
= LLVMBuildFCmp(b
, LLVMRealUGT
, x
, max
, "");
210 LLVMValueRef clamp_value
;
212 clamp_value
= LLVMBuildSelect(b
, min_result
, min
, x
, "");
213 clamp_value
= LLVMBuildSelect(b
, max_result
, max
, x
, "");
219 lp_twoside(LLVMBuilderRef b
,
220 struct lp_setup_args
*args
,
221 const struct lp_setup_variant_key
*key
)
223 LLVMValueRef a0_back
, a1_back
, a2_back
;
224 LLVMValueRef idx2
= LLVMConstInt(LLVMInt32Type(), key
->bcolor_slot
, 0);
226 LLVMValueRef facing
= args
->facing
;
227 LLVMValueRef front_facing
= LLVMBuildICmp(b
, LLVMIntEQ
, facing
, LLVMConstInt(LLVMInt32Type(), 0, 0), ""); /** need i1 for if condition */
229 a0_back
= LLVMBuildLoad(b
, LLVMBuildGEP(b
, args
->v0
, &idx2
, 1, ""), "v0a_back");
230 a1_back
= LLVMBuildLoad(b
, LLVMBuildGEP(b
, args
->v1
, &idx2
, 1, ""), "v1a_back");
231 a2_back
= LLVMBuildLoad(b
, LLVMBuildGEP(b
, args
->v2
, &idx2
, 1, ""), "v2a_back");
233 /* Possibly swap the front and back attrib values,
235 * Prefer select to if so we don't have to worry about phis or
238 args
->v0a
= LLVMBuildSelect(b
, front_facing
, a0_back
, args
->v0a
, "");
239 args
->v1a
= LLVMBuildSelect(b
, front_facing
, a1_back
, args
->v1a
, "");
240 args
->v2a
= LLVMBuildSelect(b
, front_facing
, a2_back
, args
->v2a
, "");
245 lp_do_offset_tri(LLVMBuilderRef b
,
246 struct lp_setup_args
*args
,
247 const struct lp_setup_variant_key
*key
)
249 struct lp_build_context bld
;
250 LLVMValueRef zoffset
, mult
;
251 LLVMValueRef z0_new
, z1_new
, z2_new
;
252 LLVMValueRef dzdx0
, dzdx
, dzdy0
, dzdy
;
253 LLVMValueRef max
, max_value
;
255 LLVMValueRef one
= LLVMConstReal(LLVMFloatType(), 1.0);
256 LLVMValueRef zero
= LLVMConstReal(LLVMFloatType(), 0.0);
258 /* edge vectors: e = v0 - v2, f = v1 - v2 */
259 LLVMValueRef v0_x
= vert_attrib(b
, args
->v0
, 0, 0, "v0_x");
260 LLVMValueRef v1_x
= vert_attrib(b
, args
->v1
, 0, 0, "v1_x");
261 LLVMValueRef v2_x
= vert_attrib(b
, args
->v2
, 0, 0, "v2_x");
262 LLVMValueRef v0_y
= vert_attrib(b
, args
->v0
, 0, 1, "v0_y");
263 LLVMValueRef v1_y
= vert_attrib(b
, args
->v1
, 0, 1, "v1_y");
264 LLVMValueRef v2_y
= vert_attrib(b
, args
->v2
, 0, 1, "v2_y");
265 LLVMValueRef v0_z
= vert_attrib(b
, args
->v0
, 0, 2, "v0_z");
266 LLVMValueRef v1_z
= vert_attrib(b
, args
->v1
, 0, 2, "v1_z");
267 LLVMValueRef v2_z
= vert_attrib(b
, args
->v2
, 0, 2, "v2_z");
269 /* edge vectors: e = v0 - v2, f = v1 - v2 */
270 LLVMValueRef dx02
= LLVMBuildFSub(b
, v0_x
, v2_x
, "dx02");
271 LLVMValueRef dy02
= LLVMBuildFSub(b
, v0_y
, v2_y
, "dy02");
272 LLVMValueRef dz02
= LLVMBuildFSub(b
, v0_z
, v2_z
, "dz02");
273 LLVMValueRef dx12
= LLVMBuildFSub(b
, v1_x
, v2_x
, "dx12");
274 LLVMValueRef dy12
= LLVMBuildFSub(b
, v1_y
, v2_y
, "dy12");
275 LLVMValueRef dz12
= LLVMBuildFSub(b
, v1_z
, v2_z
, "dz12");
277 /* det = cross(e,f).z */
278 LLVMValueRef dx02_dy12
= LLVMBuildFMul(b
, dx02
, dy12
, "dx02_dy12");
279 LLVMValueRef dy02_dx12
= LLVMBuildFMul(b
, dy02
, dx12
, "dy02_dx12");
280 LLVMValueRef det
= LLVMBuildFSub(b
, dx02_dy12
, dy02_dx12
, "det");
281 LLVMValueRef inv_det
= LLVMBuildFDiv(b
, one
, det
, "inv_det");
283 /* (res1,res2) = cross(e,f).xy */
284 LLVMValueRef dy02_dz12
= LLVMBuildFMul(b
, dy02
, dz12
, "dy02_dz12");
285 LLVMValueRef dz02_dy12
= LLVMBuildFMul(b
, dz02
, dy12
, "dz02_dy12");
286 LLVMValueRef dz02_dx12
= LLVMBuildFMul(b
, dz02
, dx12
, "dz02_dx12");
287 LLVMValueRef dx02_dz12
= LLVMBuildFMul(b
, dx02
, dz12
, "dx02_dz12");
288 LLVMValueRef res1
= LLVMBuildFSub(b
, dy02_dz12
, dz02_dy12
, "res1");
289 LLVMValueRef res2
= LLVMBuildFSub(b
, dz02_dx12
, dx02_dz12
, "res2");
291 /* dzdx = fabsf(res1 * inv_det), dydx = fabsf(res2 * inv_det)*/
292 lp_build_context_init(&bld
, b
, lp_type_float(32));
293 dzdx0
= LLVMBuildFMul(b
, res1
, inv_det
, "dzdx");
294 dzdx
= lp_build_abs(&bld
, dzdx0
);
295 dzdy0
= LLVMBuildFMul(b
, res2
, inv_det
, "dzdy");
296 dzdy
= lp_build_abs(&bld
, dzdy0
);
298 /* zoffset = offset->units + MAX2(dzdx, dzdy) * offset->scale */
299 max
= LLVMBuildFCmp(b
, LLVMRealUGT
, dzdx
, dzdy
, "");
300 max_value
= LLVMBuildSelect(b
, max
, dzdx
, dzdy
, "max");
302 mult
= LLVMBuildFMul(b
, max_value
, LLVMConstReal(LLVMFloatType(), key
->scale
), "");
303 zoffset
= LLVMBuildFAdd(b
, LLVMConstReal(LLVMFloatType(), key
->units
), mult
, "zoffset");
305 /* clamp and do offset */
306 z0_new
= vert_clamp(b
, LLVMBuildFAdd(b
, v0_z
, zoffset
, ""), zero
, one
);
307 z1_new
= vert_clamp(b
, LLVMBuildFAdd(b
, v1_z
, zoffset
, ""), zero
, one
);
308 z2_new
= vert_clamp(b
, LLVMBuildFAdd(b
, v2_z
, zoffset
, ""), zero
, one
);
310 /* insert into args->a0.z, a1.z, a2.z:
312 args
->v0a
= LLVMBuildInsertElement(b
, args
->v0a
, z0_new
, LLVMConstInt(LLVMInt32Type(), 2, 0), "");
313 args
->v1a
= LLVMBuildInsertElement(b
, args
->v1a
, z1_new
, LLVMConstInt(LLVMInt32Type(), 2, 0), "");
314 args
->v2a
= LLVMBuildInsertElement(b
, args
->v2a
, z2_new
, LLVMConstInt(LLVMInt32Type(), 2, 0), "");
318 load_attribute(LLVMBuilderRef b
,
319 struct lp_setup_args
*args
,
320 const struct lp_setup_variant_key
*key
,
323 LLVMValueRef idx
= LLVMConstInt(LLVMInt32Type(), vert_attr
, 0);
325 /* Load the vertex data
327 args
->v0a
= LLVMBuildLoad(b
, LLVMBuildGEP(b
, args
->v0
, &idx
, 1, ""), "v0a");
328 args
->v1a
= LLVMBuildLoad(b
, LLVMBuildGEP(b
, args
->v1
, &idx
, 1, ""), "v1a");
329 args
->v2a
= LLVMBuildLoad(b
, LLVMBuildGEP(b
, args
->v2
, &idx
, 1, ""), "v2a");
332 /* Potentially modify it according to twoside, offset, etc:
334 if (vert_attr
== 0 && (key
->scale
!= 0.0f
|| key
->units
!= 0.0f
)) {
335 lp_do_offset_tri(b
, args
, key
);
338 if (key
->twoside
&& vert_attr
== key
->color_slot
) {
339 lp_twoside(b
, args
, key
);
344 emit_coef4( LLVMBuilderRef b
,
345 struct lp_setup_args
*args
,
351 LLVMValueRef dy20_ooa
= args
->dy20_ooa
;
352 LLVMValueRef dy01_ooa
= args
->dy01_ooa
;
353 LLVMValueRef dx20_ooa
= args
->dx20_ooa
;
354 LLVMValueRef dx01_ooa
= args
->dx01_ooa
;
355 LLVMValueRef x0_center
= args
->x0_center
;
356 LLVMValueRef y0_center
= args
->y0_center
;
358 /* XXX: using fsub, fmul on vector types -- does this work??
360 LLVMValueRef da01
= LLVMBuildFSub(b
, a0
, a1
, "da01");
361 LLVMValueRef da20
= LLVMBuildFSub(b
, a2
, a0
, "da20");
363 /* Calculate dadx (vec4f)
365 LLVMValueRef da01_dy20_ooa
= LLVMBuildFMul(b
, da01
, dy20_ooa
, "da01_dy20_ooa");
366 LLVMValueRef da20_dy01_ooa
= LLVMBuildFMul(b
, da20
, dy01_ooa
, "da20_dy01_ooa");
367 LLVMValueRef dadx
= LLVMBuildFSub(b
, da01_dy20_ooa
, da20_dy01_ooa
, "dadx");
369 /* Calculate dady (vec4f)
371 LLVMValueRef da01_dx20_ooa
= LLVMBuildFMul(b
, da01
, dx20_ooa
, "da01_dx20_ooa");
372 LLVMValueRef da20_dx01_ooa
= LLVMBuildFMul(b
, da20
, dx01_ooa
, "da20_dx01_ooa");
373 LLVMValueRef dady
= LLVMBuildFSub(b
, da20_dx01_ooa
, da01_dx20_ooa
, "dady");
375 /* Calculate a0 - the attribute value at the origin
377 LLVMValueRef dadx_x0
= LLVMBuildFMul(b
, dadx
, x0_center
, "dadx_x0");
378 LLVMValueRef dady_y0
= LLVMBuildFMul(b
, dady
, y0_center
, "dady_y0");
379 LLVMValueRef attr_v0
= LLVMBuildFAdd(b
, dadx_x0
, dady_y0
, "attr_v0");
380 LLVMValueRef attr_0
= LLVMBuildFSub(b
, a0
, attr_v0
, "attr_0");
382 store_coef(b
, args
, slot
, attr_0
, dadx
, dady
);
387 emit_linear_coef( LLVMBuilderRef b
,
388 struct lp_setup_args
*args
,
391 /* nothing to do anymore */
401 * Compute a0, dadx and dady for a perspective-corrected interpolant,
403 * We basically multiply the vertex value by 1/w before computing
404 * the plane coefficients (a0, dadx, dady).
405 * Later, when we compute the value at a particular fragment position we'll
406 * divide the interpolated value by the interpolated W at that fragment.
409 emit_perspective_coef( LLVMBuilderRef b
,
410 struct lp_setup_args
*args
,
413 /* premultiply by 1/w (v[0][3] is always 1/w):
415 LLVMValueRef v0_oow
= vec4f_from_scalar(b
, vert_attrib(b
, args
->v0
, 0, 3, ""), "v0_oow");
416 LLVMValueRef v1_oow
= vec4f_from_scalar(b
, vert_attrib(b
, args
->v1
, 0, 3, ""), "v1_oow");
417 LLVMValueRef v2_oow
= vec4f_from_scalar(b
, vert_attrib(b
, args
->v2
, 0, 3, ""), "v2_oow");
419 LLVMValueRef v0_oow_v0a
= LLVMBuildFMul(b
, args
->v0a
, v0_oow
, "v0_oow_v0a");
420 LLVMValueRef v1_oow_v1a
= LLVMBuildFMul(b
, args
->v1a
, v1_oow
, "v1_oow_v1a");
421 LLVMValueRef v2_oow_v2a
= LLVMBuildFMul(b
, args
->v2a
, v2_oow
, "v2_oow_v2a");
423 emit_coef4(b
, args
, slot
, v0_oow_v0a
, v1_oow_v1a
, v2_oow_v2a
);
428 emit_position_coef( LLVMBuilderRef builder
,
429 struct lp_setup_args
*args
,
432 emit_linear_coef(builder
, args
, slot
);
439 * Compute the inputs-> dadx, dady, a0 values.
442 emit_tri_coef( LLVMBuilderRef builder
,
443 const struct lp_setup_variant_key
*key
,
444 struct lp_setup_args
*args
)
448 /* The internal position input is in slot zero:
450 load_attribute(builder
, args
, key
, 0);
451 emit_position_coef(builder
, args
, 0);
453 /* setup interpolation for all the remaining attributes:
455 for (slot
= 0; slot
< key
->num_inputs
; slot
++) {
457 if (key
->inputs
[slot
].interp
== LP_INTERP_CONSTANT
||
458 key
->inputs
[slot
].interp
== LP_INTERP_LINEAR
||
459 key
->inputs
[slot
].interp
== LP_INTERP_PERSPECTIVE
)
460 load_attribute(builder
, args
, key
, key
->inputs
[slot
].src_index
);
462 switch (key
->inputs
[slot
].interp
) {
463 case LP_INTERP_CONSTANT
:
464 if (key
->flatshade_first
) {
465 emit_constant_coef4(builder
, args
, slot
+1, args
->v0a
);
468 emit_constant_coef4(builder
, args
, slot
+1, args
->v2a
);
472 case LP_INTERP_LINEAR
:
473 emit_linear_coef(builder
, args
, slot
+1);
476 case LP_INTERP_PERSPECTIVE
:
477 emit_perspective_coef(builder
, args
, slot
+1);
480 case LP_INTERP_POSITION
:
482 * The generated pixel interpolators will pick up the coeffs from
487 case LP_INTERP_FACING
:
488 emit_facing_coef(builder
, args
, slot
+1);
498 /* XXX: This is generic code, share with fs/vs codegen:
500 static lp_jit_setup_triangle
501 finalize_function(struct llvmpipe_screen
*screen
,
502 LLVMBuilderRef builder
,
503 LLVMValueRef function
)
507 /* Verify the LLVM IR. If invalid, dump and abort */
509 if (LLVMVerifyFunction(function
, LLVMPrintMessageAction
)) {
511 lp_debug_dump_value(function
);
516 /* Apply optimizations to LLVM IR */
517 LLVMRunFunctionPassManager(screen
->pass
, function
);
519 if (gallivm_debug
& GALLIVM_DEBUG_IR
)
521 /* Print the LLVM IR to stderr */
522 lp_debug_dump_value(function
);
527 * Translate the LLVM IR into machine code.
529 f
= LLVMGetPointerToGlobal(screen
->engine
, function
);
531 if (gallivm_debug
& GALLIVM_DEBUG_ASM
)
536 lp_func_delete_body(function
);
541 /* XXX: Generic code:
544 lp_emit_emms(LLVMBuilderRef builder
)
547 /* Avoid corrupting the FPU stack on 32bit OSes. */
548 lp_build_intrinsic(builder
, "llvm.x86.mmx.emms", LLVMVoidType(), NULL
, 0);
553 /* XXX: generic code:
556 set_noalias(LLVMBuilderRef builder
,
557 LLVMValueRef function
,
558 const LLVMTypeRef
*arg_types
,
562 for(i
= 0; i
< Elements(arg_types
); ++i
)
563 if(LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
564 LLVMAddAttribute(LLVMGetParam(function
, i
),
565 LLVMNoAliasAttribute
);
569 init_args(LLVMBuilderRef b
,
570 struct lp_setup_args
*args
,
571 const struct lp_setup_variant
*variant
)
573 LLVMValueRef v0_x
= vert_attrib(b
, args
->v0
, 0, 0, "v0_x");
574 LLVMValueRef v0_y
= vert_attrib(b
, args
->v0
, 0, 1, "v0_y");
576 LLVMValueRef v1_x
= vert_attrib(b
, args
->v1
, 0, 0, "v1_x");
577 LLVMValueRef v1_y
= vert_attrib(b
, args
->v1
, 0, 1, "v1_y");
579 LLVMValueRef v2_x
= vert_attrib(b
, args
->v2
, 0, 0, "v2_x");
580 LLVMValueRef v2_y
= vert_attrib(b
, args
->v2
, 0, 1, "v2_y");
582 LLVMValueRef pixel_center
= LLVMConstReal(LLVMFloatType(),
583 variant
->key
.pixel_center_half
? 0.5 : 0);
585 LLVMValueRef x0_center
= LLVMBuildFSub(b
, v0_x
, pixel_center
, "x0_center" );
586 LLVMValueRef y0_center
= LLVMBuildFSub(b
, v0_y
, pixel_center
, "y0_center" );
588 LLVMValueRef dx01
= LLVMBuildFSub(b
, v0_x
, v1_x
, "dx01");
589 LLVMValueRef dy01
= LLVMBuildFSub(b
, v0_y
, v1_y
, "dy01");
590 LLVMValueRef dx20
= LLVMBuildFSub(b
, v2_x
, v0_x
, "dx20");
591 LLVMValueRef dy20
= LLVMBuildFSub(b
, v2_y
, v0_y
, "dy20");
593 LLVMValueRef one
= LLVMConstReal(LLVMFloatType(), 1.0);
594 LLVMValueRef e
= LLVMBuildFMul(b
, dx01
, dy20
, "e");
595 LLVMValueRef f
= LLVMBuildFMul(b
, dx20
, dy01
, "f");
596 LLVMValueRef ooa
= LLVMBuildFDiv(b
, one
, LLVMBuildFSub(b
, e
, f
, ""), "ooa");
598 LLVMValueRef dy20_ooa
= LLVMBuildFMul(b
, dy20
, ooa
, "dy20_ooa");
599 LLVMValueRef dy01_ooa
= LLVMBuildFMul(b
, dy01
, ooa
, "dy01_ooa");
600 LLVMValueRef dx20_ooa
= LLVMBuildFMul(b
, dx20
, ooa
, "dx20_ooa");
601 LLVMValueRef dx01_ooa
= LLVMBuildFMul(b
, dx01
, ooa
, "dx01_ooa");
603 args
->dy20_ooa
= vec4f_from_scalar(b
, dy20_ooa
, "dy20_ooa_4f");
604 args
->dy01_ooa
= vec4f_from_scalar(b
, dy01_ooa
, "dy01_ooa_4f");
606 args
->dx20_ooa
= vec4f_from_scalar(b
, dx20_ooa
, "dx20_ooa_4f");
607 args
->dx01_ooa
= vec4f_from_scalar(b
, dx01_ooa
, "dx01_ooa_4f");
609 args
->x0_center
= vec4f_from_scalar(b
, x0_center
, "x0_center_4f");
610 args
->y0_center
= vec4f_from_scalar(b
, y0_center
, "y0_center_4f");
614 * Generate the runtime callable function for the coefficient calculation.
617 static struct lp_setup_variant
*
618 generate_setup_variant(struct llvmpipe_screen
*screen
,
619 struct lp_setup_variant_key
*key
,
620 struct llvmpipe_context
*lp
)
622 struct lp_setup_variant
*variant
= NULL
;
623 struct lp_setup_args args
;
625 LLVMTypeRef vec4f_type
;
626 LLVMTypeRef func_type
;
627 LLVMTypeRef arg_types
[7];
628 LLVMBasicBlockRef block
;
629 LLVMBuilderRef builder
;
635 variant
= CALLOC_STRUCT(lp_setup_variant
);
639 if (LP_DEBUG
& DEBUG_COUNTERS
) {
643 memcpy(&variant
->key
, key
, key
->size
);
644 variant
->list_item_global
.base
= variant
;
646 util_snprintf(func_name
, sizeof(func_name
), "fs%u_setup%u",
650 /* Currently always deal with full 4-wide vertex attributes from
654 vec4f_type
= LLVMVectorType(LLVMFloatType(), 4);
656 arg_types
[0] = LLVMPointerType(vec4f_type
, 0); /* v0 */
657 arg_types
[1] = LLVMPointerType(vec4f_type
, 0); /* v1 */
658 arg_types
[2] = LLVMPointerType(vec4f_type
, 0); /* v2 */
659 arg_types
[3] = LLVMInt32Type(); /* facing */
660 arg_types
[4] = LLVMPointerType(vec4f_type
, 0); /* a0, aligned */
661 arg_types
[5] = LLVMPointerType(vec4f_type
, 0); /* dadx, aligned */
662 arg_types
[6] = LLVMPointerType(vec4f_type
, 0); /* dady, aligned */
664 func_type
= LLVMFunctionType(LLVMVoidType(), arg_types
, Elements(arg_types
), 0);
666 variant
->function
= LLVMAddFunction(screen
->module
, func_name
, func_type
);
667 if (!variant
->function
)
670 LLVMSetFunctionCallConv(variant
->function
, LLVMCCallConv
);
672 args
.v0
= LLVMGetParam(variant
->function
, 0);
673 args
.v1
= LLVMGetParam(variant
->function
, 1);
674 args
.v2
= LLVMGetParam(variant
->function
, 2);
675 args
.facing
= LLVMGetParam(variant
->function
, 3);
676 args
.a0
= LLVMGetParam(variant
->function
, 4);
677 args
.dadx
= LLVMGetParam(variant
->function
, 5);
678 args
.dady
= LLVMGetParam(variant
->function
, 6);
680 lp_build_name(args
.v0
, "in_v0");
681 lp_build_name(args
.v1
, "in_v1");
682 lp_build_name(args
.v2
, "in_v2");
683 lp_build_name(args
.facing
, "in_facing");
684 lp_build_name(args
.a0
, "out_a0");
685 lp_build_name(args
.dadx
, "out_dadx");
686 lp_build_name(args
.dady
, "out_dady");
691 block
= LLVMAppendBasicBlock(variant
->function
, "entry");
692 builder
= LLVMCreateBuilder();
693 LLVMPositionBuilderAtEnd(builder
, block
);
695 set_noalias(builder
, variant
->function
, arg_types
, Elements(arg_types
));
696 init_args(builder
, &args
, variant
);
697 emit_tri_coef(builder
, &variant
->key
, &args
);
699 lp_emit_emms(builder
);
700 LLVMBuildRetVoid(builder
);
701 LLVMDisposeBuilder(builder
);
703 variant
->jit_function
= finalize_function(screen
, builder
,
705 if (!variant
->jit_function
)
709 * Update timing information:
711 if (LP_DEBUG
& DEBUG_COUNTERS
) {
713 LP_COUNT_ADD(llvm_compile_time
, t1
- t0
);
714 LP_COUNT_ADD(nr_llvm_compiles
, 1);
721 if (variant
->function
) {
722 if (variant
->jit_function
)
723 LLVMFreeMachineCodeForFunction(screen
->engine
,
725 LLVMDeleteFunction(variant
->function
);
736 lp_make_setup_variant_key(struct llvmpipe_context
*lp
,
737 struct lp_setup_variant_key
*key
)
739 struct lp_fragment_shader
*fs
= lp
->fs
;
742 assert(sizeof key
->inputs
[0] == sizeof(ushort
));
744 key
->num_inputs
= fs
->info
.base
.num_inputs
;
745 key
->flatshade_first
= lp
->rasterizer
->flatshade_first
;
746 key
->pixel_center_half
= lp
->rasterizer
->gl_rasterization_rules
;
747 key
->twoside
= lp
->rasterizer
->light_twoside
;
748 key
->size
= Offset(struct lp_setup_variant_key
,
749 inputs
[key
->num_inputs
]);
750 key
->color_slot
= lp
->color_slot
;
751 key
->bcolor_slot
= lp
->bcolor_slot
;
752 key
->units
= (float) (lp
->rasterizer
->offset_units
* lp
->mrd
);
753 key
->scale
= lp
->rasterizer
->offset_scale
;
755 memcpy(key
->inputs
, fs
->inputs
, key
->num_inputs
* sizeof key
->inputs
[0]);
756 for (i
= 0; i
< key
->num_inputs
; i
++) {
757 if (key
->inputs
[i
].interp
== LP_INTERP_COLOR
) {
758 if (lp
->rasterizer
->flatshade
)
759 key
->inputs
[i
].interp
= LP_INTERP_CONSTANT
;
761 key
->inputs
[i
].interp
= LP_INTERP_LINEAR
;
769 remove_setup_variant(struct llvmpipe_context
*lp
,
770 struct lp_setup_variant
*variant
)
772 struct llvmpipe_screen
*screen
= llvmpipe_screen(lp
->pipe
.screen
);
774 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
775 debug_printf("llvmpipe: del setup_variant #%u total %u\n",
776 variant
->no
, lp
->nr_setup_variants
);
779 if (variant
->function
) {
780 if (variant
->jit_function
)
781 LLVMFreeMachineCodeForFunction(screen
->engine
,
783 LLVMDeleteFunction(variant
->function
);
786 remove_from_list(&variant
->list_item_global
);
787 lp
->nr_setup_variants
--;
793 /* When the number of setup variants exceeds a threshold, cull a
794 * fraction (currently a quarter) of them.
797 cull_setup_variants(struct llvmpipe_context
*lp
)
799 struct pipe_context
*pipe
= &lp
->pipe
;
803 * XXX: we need to flush the context until we have some sort of reference
804 * counting in fragment shaders as they may still be binned
805 * Flushing alone might not be sufficient we need to wait on it too.
807 llvmpipe_finish(pipe
, __FUNCTION__
);
809 for (i
= 0; i
< LP_MAX_SETUP_VARIANTS
/ 4; i
++) {
810 struct lp_setup_variant_list_item
*item
= last_elem(&lp
->setup_variants_list
);
811 remove_setup_variant(lp
, item
->base
);
817 * Update fragment/vertex shader linkage state. This is called just
818 * prior to drawing something when some fragment-related state has
822 llvmpipe_update_setup(struct llvmpipe_context
*lp
)
824 struct llvmpipe_screen
*screen
= llvmpipe_screen(lp
->pipe
.screen
);
826 struct lp_setup_variant_key
*key
= &lp
->setup_variant
.key
;
827 struct lp_setup_variant
*variant
= NULL
;
828 struct lp_setup_variant_list_item
*li
;
830 lp_make_setup_variant_key(lp
, key
);
832 foreach(li
, &lp
->setup_variants_list
) {
833 if(li
->base
->key
.size
== key
->size
&&
834 memcmp(&li
->base
->key
, key
, key
->size
) == 0) {
841 move_to_head(&lp
->setup_variants_list
, &variant
->list_item_global
);
844 if (lp
->nr_setup_variants
>= LP_MAX_SETUP_VARIANTS
) {
845 cull_setup_variants(lp
);
848 variant
= generate_setup_variant(screen
, key
, lp
);
849 insert_at_head(&lp
->setup_variants_list
, &variant
->list_item_global
);
850 lp
->nr_setup_variants
++;
853 lp_setup_set_setup_variant(lp
->setup
,
858 lp_delete_setup_variants(struct llvmpipe_context
*lp
)
860 struct lp_setup_variant_list_item
*li
;
861 li
= first_elem(&lp
->setup_variants_list
);
862 while(!at_end(&lp
->setup_variants_list
, li
)) {
863 struct lp_setup_variant_list_item
*next
= next_elem(li
);
864 remove_setup_variant(lp
, li
->base
);
870 lp_dump_setup_coef( const struct lp_setup_variant_key
*key
,
871 const float (*sa0
)[4],
872 const float (*sdadx
)[4],
873 const float (*sdady
)[4])
877 for (i
= 0; i
< NUM_CHANNELS
; i
++) {
878 float a0
= sa0
[0][i
];
879 float dadx
= sdadx
[0][i
];
880 float dady
= sdady
[0][i
];
882 debug_printf("POS.%c: a0 = %f, dadx = %f, dady = %f\n",
887 for (slot
= 0; slot
< key
->num_inputs
; slot
++) {
888 unsigned usage_mask
= key
->inputs
[slot
].usage_mask
;
889 for (i
= 0; i
< NUM_CHANNELS
; i
++) {
890 if (usage_mask
& (1 << i
)) {
891 float a0
= sa0
[1 + slot
][i
];
892 float dadx
= sdadx
[1 + slot
][i
];
893 float dady
= sdady
[1 + slot
][i
];
895 debug_printf("IN[%u].%c: a0 = %f, dadx = %f, dady = %f\n",