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_intr.h"
38 #include "gallivm/lp_bld_flow.h"
39 #include "gallivm/lp_bld_type.h"
40 #include <llvm-c/Analysis.h> /* for LLVMVerifyFunction */
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;
80 /* Temporary, per-attribute:
90 type4f(struct gallivm_state *gallivm)
92 return LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), 4);
96 /* Equivalent of _mm_setr_ps(a,b,c,d)
99 vec4f(struct gallivm_state *gallivm,
100 LLVMValueRef a, LLVMValueRef b, LLVMValueRef c, LLVMValueRef d,
103 LLVMBuilderRef bld = gallivm->builder;
104 LLVMValueRef i0 = lp_build_const_int32(gallivm, 0);
105 LLVMValueRef i1 = lp_build_const_int32(gallivm, 1);
106 LLVMValueRef i2 = lp_build_const_int32(gallivm, 2);
107 LLVMValueRef i3 = lp_build_const_int32(gallivm, 3);
109 LLVMValueRef res = LLVMGetUndef(type4f(gallivm));
111 res = LLVMBuildInsertElement(bld, res, a, i0, "");
112 res = LLVMBuildInsertElement(bld, res, b, i1, "");
113 res = LLVMBuildInsertElement(bld, res, c, i2, "");
114 res = LLVMBuildInsertElement(bld, res, d, i3, name);
119 /* Equivalent of _mm_set1_ps(a)
122 vec4f_from_scalar(struct gallivm_state *gallivm,
126 LLVMBuilderRef bld = gallivm->builder;
127 LLVMValueRef res = LLVMGetUndef(type4f(gallivm));
130 for(i = 0; i < 4; ++i) {
131 LLVMValueRef index = lp_build_const_int32(gallivm, i);
132 res = LLVMBuildInsertElement(bld, res, a, index, i == 3 ? name : "");
139 store_coef(struct gallivm_state *gallivm,
140 struct lp_setup_args *args,
146 LLVMBuilderRef builder = gallivm->builder;
147 LLVMValueRef idx = lp_build_const_int32(gallivm, slot);
149 LLVMBuildStore(builder,
151 LLVMBuildGEP(builder, args->a0, &idx, 1, ""));
153 LLVMBuildStore(builder,
155 LLVMBuildGEP(builder, args->dadx, &idx, 1, ""));
157 LLVMBuildStore(builder,
159 LLVMBuildGEP(builder, args->dady, &idx, 1, ""));
165 emit_constant_coef4(struct gallivm_state *gallivm,
166 struct lp_setup_args *args,
170 LLVMValueRef zero = lp_build_const_float(gallivm, 0.0);
171 LLVMValueRef zerovec = vec4f_from_scalar(gallivm, zero, "zero");
172 store_coef(gallivm, args, slot, vert, zerovec, zerovec);
178 * Setup the fragment input attribute with the front-facing value.
179 * \param frontface is the triangle front facing?
182 emit_facing_coef(struct gallivm_state *gallivm,
183 struct lp_setup_args *args,
186 LLVMBuilderRef builder = gallivm->builder;
187 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context);
188 LLVMValueRef a0_0 = args->facing;
189 LLVMValueRef a0_0f = LLVMBuildSIToFP(builder, a0_0, float_type, "");
190 LLVMValueRef zero = lp_build_const_float(gallivm, 0.0);
191 LLVMValueRef a0 = vec4f(gallivm, a0_0f, zero, zero, zero, "facing");
192 LLVMValueRef zerovec = vec4f_from_scalar(gallivm, zero, "zero");
194 store_coef(gallivm, args, slot, a0, zerovec, zerovec);
199 vert_attrib(struct gallivm_state *gallivm,
205 LLVMBuilderRef b = gallivm->builder;
207 idx[0] = lp_build_const_int32(gallivm, attr);
208 idx[1] = lp_build_const_int32(gallivm, elem);
209 return LLVMBuildLoad(b, LLVMBuildGEP(b, vert, idx, 2, ""), name);
213 vert_clamp(LLVMBuilderRef b,
218 LLVMValueRef min_result = LLVMBuildFCmp(b, LLVMRealUGT, min, x, "");
219 LLVMValueRef max_result = LLVMBuildFCmp(b, LLVMRealUGT, x, max, "");
220 LLVMValueRef clamp_value;
222 clamp_value = LLVMBuildSelect(b, min_result, min, x, "");
223 clamp_value = LLVMBuildSelect(b, max_result, max, x, "");
229 lp_twoside(struct gallivm_state *gallivm,
230 struct lp_setup_args *args,
231 const struct lp_setup_variant_key *key,
234 LLVMBuilderRef b = gallivm->builder;
235 LLVMValueRef a0_back, a1_back, a2_back;
236 LLVMValueRef idx2 = lp_build_const_int32(gallivm, bcolor_slot);
238 LLVMValueRef facing = args->facing;
239 LLVMValueRef front_facing = LLVMBuildICmp(b, LLVMIntEQ, facing, lp_build_const_int32(gallivm, 0), ""); /** need i1 for if condition */
241 a0_back = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v0, &idx2, 1, ""), "v0a_back");
242 a1_back = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v1, &idx2, 1, ""), "v1a_back");
243 a2_back = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v2, &idx2, 1, ""), "v2a_back");
245 /* Possibly swap the front and back attrib values,
247 * Prefer select to if so we don't have to worry about phis or
250 args->v0a = LLVMBuildSelect(b, front_facing, a0_back, args->v0a, "");
251 args->v1a = LLVMBuildSelect(b, front_facing, a1_back, args->v1a, "");
252 args->v2a = LLVMBuildSelect(b, front_facing, a2_back, args->v2a, "");
257 lp_do_offset_tri(struct gallivm_state *gallivm,
258 struct lp_setup_args *args,
259 const struct lp_setup_variant_key *key)
261 LLVMBuilderRef b = gallivm->builder;
262 struct lp_build_context bld;
263 LLVMValueRef zoffset, mult;
264 LLVMValueRef z0_new, z1_new, z2_new;
265 LLVMValueRef dzdx0, dzdx, dzdy0, dzdy;
266 LLVMValueRef max, max_value;
268 LLVMValueRef one = lp_build_const_float(gallivm, 1.0);
269 LLVMValueRef zero = lp_build_const_float(gallivm, 0.0);
270 LLVMValueRef two = lp_build_const_int32(gallivm, 2);
272 /* edge vectors: e = v0 - v2, f = v1 - v2 */
273 LLVMValueRef v0_x = vert_attrib(gallivm, args->v0, 0, 0, "v0_x");
274 LLVMValueRef v1_x = vert_attrib(gallivm, args->v1, 0, 0, "v1_x");
275 LLVMValueRef v2_x = vert_attrib(gallivm, args->v2, 0, 0, "v2_x");
276 LLVMValueRef v0_y = vert_attrib(gallivm, args->v0, 0, 1, "v0_y");
277 LLVMValueRef v1_y = vert_attrib(gallivm, args->v1, 0, 1, "v1_y");
278 LLVMValueRef v2_y = vert_attrib(gallivm, args->v2, 0, 1, "v2_y");
279 LLVMValueRef v0_z = vert_attrib(gallivm, args->v0, 0, 2, "v0_z");
280 LLVMValueRef v1_z = vert_attrib(gallivm, args->v1, 0, 2, "v1_z");
281 LLVMValueRef v2_z = vert_attrib(gallivm, args->v2, 0, 2, "v2_z");
283 /* edge vectors: e = v0 - v2, f = v1 - v2 */
284 LLVMValueRef dx02 = LLVMBuildFSub(b, v0_x, v2_x, "dx02");
285 LLVMValueRef dy02 = LLVMBuildFSub(b, v0_y, v2_y, "dy02");
286 LLVMValueRef dz02 = LLVMBuildFSub(b, v0_z, v2_z, "dz02");
287 LLVMValueRef dx12 = LLVMBuildFSub(b, v1_x, v2_x, "dx12");
288 LLVMValueRef dy12 = LLVMBuildFSub(b, v1_y, v2_y, "dy12");
289 LLVMValueRef dz12 = LLVMBuildFSub(b, v1_z, v2_z, "dz12");
291 /* det = cross(e,f).z */
292 LLVMValueRef dx02_dy12 = LLVMBuildFMul(b, dx02, dy12, "dx02_dy12");
293 LLVMValueRef dy02_dx12 = LLVMBuildFMul(b, dy02, dx12, "dy02_dx12");
294 LLVMValueRef det = LLVMBuildFSub(b, dx02_dy12, dy02_dx12, "det");
295 LLVMValueRef inv_det = LLVMBuildFDiv(b, one, det, "inv_det");
297 /* (res1,res2) = cross(e,f).xy */
298 LLVMValueRef dy02_dz12 = LLVMBuildFMul(b, dy02, dz12, "dy02_dz12");
299 LLVMValueRef dz02_dy12 = LLVMBuildFMul(b, dz02, dy12, "dz02_dy12");
300 LLVMValueRef dz02_dx12 = LLVMBuildFMul(b, dz02, dx12, "dz02_dx12");
301 LLVMValueRef dx02_dz12 = LLVMBuildFMul(b, dx02, dz12, "dx02_dz12");
302 LLVMValueRef res1 = LLVMBuildFSub(b, dy02_dz12, dz02_dy12, "res1");
303 LLVMValueRef res2 = LLVMBuildFSub(b, dz02_dx12, dx02_dz12, "res2");
305 /* dzdx = fabsf(res1 * inv_det), dydx = fabsf(res2 * inv_det)*/
306 lp_build_context_init(&bld, gallivm, lp_type_float(32));
307 dzdx0 = LLVMBuildFMul(b, res1, inv_det, "dzdx");
308 dzdx = lp_build_abs(&bld, dzdx0);
309 dzdy0 = LLVMBuildFMul(b, res2, inv_det, "dzdy");
310 dzdy = lp_build_abs(&bld, dzdy0);
312 /* zoffset = offset->units + MAX2(dzdx, dzdy) * offset->scale */
313 max = LLVMBuildFCmp(b, LLVMRealUGT, dzdx, dzdy, "");
314 max_value = LLVMBuildSelect(b, max, dzdx, dzdy, "max");
316 mult = LLVMBuildFMul(b, max_value, lp_build_const_float(gallivm, key->scale), "");
317 zoffset = LLVMBuildFAdd(b, lp_build_const_float(gallivm, key->units), mult, "zoffset");
319 /* clamp and do offset */
320 z0_new = vert_clamp(b, LLVMBuildFAdd(b, v0_z, zoffset, ""), zero, one);
321 z1_new = vert_clamp(b, LLVMBuildFAdd(b, v1_z, zoffset, ""), zero, one);
322 z2_new = vert_clamp(b, LLVMBuildFAdd(b, v2_z, zoffset, ""), zero, one);
324 /* insert into args->a0.z, a1.z, a2.z:
326 args->v0a = LLVMBuildInsertElement(b, args->v0a, z0_new, two, "");
327 args->v1a = LLVMBuildInsertElement(b, args->v1a, z1_new, two, "");
328 args->v2a = LLVMBuildInsertElement(b, args->v2a, z2_new, two, "");
332 load_attribute(struct gallivm_state *gallivm,
333 struct lp_setup_args *args,
334 const struct lp_setup_variant_key *key,
337 LLVMBuilderRef b = gallivm->builder;
338 LLVMValueRef idx = lp_build_const_int32(gallivm, vert_attr);
340 /* Load the vertex data
342 args->v0a = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v0, &idx, 1, ""), "v0a");
343 args->v1a = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v1, &idx, 1, ""), "v1a");
344 args->v2a = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v2, &idx, 1, ""), "v2a");
347 /* Potentially modify it according to twoside, offset, etc:
349 if (vert_attr == 0 && (key->scale != 0.0f || key->units != 0.0f)) {
350 lp_do_offset_tri(gallivm, args, key);
354 if (vert_attr == key->color_slot && key->bcolor_slot != ~0)
355 lp_twoside(gallivm, args, key, key->bcolor_slot);
356 else if (vert_attr == key->spec_slot && key->bspec_slot != ~0)
357 lp_twoside(gallivm, args, key, key->bspec_slot);
362 emit_coef4( struct gallivm_state *gallivm,
363 struct lp_setup_args *args,
369 LLVMBuilderRef b = gallivm->builder;
370 LLVMValueRef dy20_ooa = args->dy20_ooa;
371 LLVMValueRef dy01_ooa = args->dy01_ooa;
372 LLVMValueRef dx20_ooa = args->dx20_ooa;
373 LLVMValueRef dx01_ooa = args->dx01_ooa;
374 LLVMValueRef x0_center = args->x0_center;
375 LLVMValueRef y0_center = args->y0_center;
377 /* XXX: using fsub, fmul on vector types -- does this work??
379 LLVMValueRef da01 = LLVMBuildFSub(b, a0, a1, "da01");
380 LLVMValueRef da20 = LLVMBuildFSub(b, a2, a0, "da20");
382 /* Calculate dadx (vec4f)
384 LLVMValueRef da01_dy20_ooa = LLVMBuildFMul(b, da01, dy20_ooa, "da01_dy20_ooa");
385 LLVMValueRef da20_dy01_ooa = LLVMBuildFMul(b, da20, dy01_ooa, "da20_dy01_ooa");
386 LLVMValueRef dadx = LLVMBuildFSub(b, da01_dy20_ooa, da20_dy01_ooa, "dadx");
388 /* Calculate dady (vec4f)
390 LLVMValueRef da01_dx20_ooa = LLVMBuildFMul(b, da01, dx20_ooa, "da01_dx20_ooa");
391 LLVMValueRef da20_dx01_ooa = LLVMBuildFMul(b, da20, dx01_ooa, "da20_dx01_ooa");
392 LLVMValueRef dady = LLVMBuildFSub(b, da20_dx01_ooa, da01_dx20_ooa, "dady");
394 /* Calculate a0 - the attribute value at the origin
396 LLVMValueRef dadx_x0 = LLVMBuildFMul(b, dadx, x0_center, "dadx_x0");
397 LLVMValueRef dady_y0 = LLVMBuildFMul(b, dady, y0_center, "dady_y0");
398 LLVMValueRef attr_v0 = LLVMBuildFAdd(b, dadx_x0, dady_y0, "attr_v0");
399 LLVMValueRef attr_0 = LLVMBuildFSub(b, a0, attr_v0, "attr_0");
401 store_coef(gallivm, args, slot, attr_0, dadx, dady);
406 emit_linear_coef( struct gallivm_state *gallivm,
407 struct lp_setup_args *args,
410 /* nothing to do anymore */
420 * Compute a0, dadx and dady for a perspective-corrected interpolant,
422 * We basically multiply the vertex value by 1/w before computing
423 * the plane coefficients (a0, dadx, dady).
424 * Later, when we compute the value at a particular fragment position we'll
425 * divide the interpolated value by the interpolated W at that fragment.
428 emit_perspective_coef( struct gallivm_state *gallivm,
429 struct lp_setup_args *args,
432 LLVMBuilderRef b = gallivm->builder;
434 /* premultiply by 1/w (v[0][3] is always 1/w):
436 LLVMValueRef v0_oow = vec4f_from_scalar(gallivm, vert_attrib(gallivm, args->v0, 0, 3, ""), "v0_oow");
437 LLVMValueRef v1_oow = vec4f_from_scalar(gallivm, vert_attrib(gallivm, args->v1, 0, 3, ""), "v1_oow");
438 LLVMValueRef v2_oow = vec4f_from_scalar(gallivm, vert_attrib(gallivm, args->v2, 0, 3, ""), "v2_oow");
440 LLVMValueRef v0_oow_v0a = LLVMBuildFMul(b, args->v0a, v0_oow, "v0_oow_v0a");
441 LLVMValueRef v1_oow_v1a = LLVMBuildFMul(b, args->v1a, v1_oow, "v1_oow_v1a");
442 LLVMValueRef v2_oow_v2a = LLVMBuildFMul(b, args->v2a, v2_oow, "v2_oow_v2a");
444 emit_coef4(gallivm, args, slot, v0_oow_v0a, v1_oow_v1a, v2_oow_v2a);
449 emit_position_coef( struct gallivm_state *gallivm,
450 struct lp_setup_args *args,
453 emit_linear_coef(gallivm, args, slot);
460 * Compute the inputs-> dadx, dady, a0 values.
463 emit_tri_coef( struct gallivm_state *gallivm,
464 const struct lp_setup_variant_key *key,
465 struct lp_setup_args *args )
469 /* The internal position input is in slot zero:
471 load_attribute(gallivm, args, key, 0);
472 emit_position_coef(gallivm, args, 0);
474 /* setup interpolation for all the remaining attributes:
476 for (slot = 0; slot < key->num_inputs; slot++) {
478 if (key->inputs[slot].interp == LP_INTERP_CONSTANT ||
479 key->inputs[slot].interp == LP_INTERP_LINEAR ||
480 key->inputs[slot].interp == LP_INTERP_PERSPECTIVE)
481 load_attribute(gallivm, args, key, key->inputs[slot].src_index);
483 switch (key->inputs[slot].interp) {
484 case LP_INTERP_CONSTANT:
485 if (key->flatshade_first) {
486 emit_constant_coef4(gallivm, args, slot+1, args->v0a);
489 emit_constant_coef4(gallivm, args, slot+1, args->v2a);
493 case LP_INTERP_LINEAR:
494 emit_linear_coef(gallivm, args, slot+1);
497 case LP_INTERP_PERSPECTIVE:
498 emit_perspective_coef(gallivm, args, slot+1);
501 case LP_INTERP_POSITION:
503 * The generated pixel interpolators will pick up the coeffs from
508 case LP_INTERP_FACING:
509 emit_facing_coef(gallivm, args, slot+1);
519 /* XXX: This is generic code, share with fs/vs codegen:
521 static lp_jit_setup_triangle
522 finalize_function(struct gallivm_state *gallivm,
523 LLVMBuilderRef builder,
524 LLVMValueRef function)
528 /* Verify the LLVM IR. If invalid, dump and abort */
530 if (LLVMVerifyFunction(function, LLVMPrintMessageAction)) {
532 lp_debug_dump_value(function);
537 /* Apply optimizations to LLVM IR */
538 LLVMRunFunctionPassManager(gallivm->passmgr, function);
540 if (gallivm_debug & GALLIVM_DEBUG_IR)
542 /* Print the LLVM IR to stderr */
543 lp_debug_dump_value(function);
548 * Translate the LLVM IR into machine code.
550 f = LLVMGetPointerToGlobal(gallivm->engine, function);
552 if (gallivm_debug & GALLIVM_DEBUG_ASM)
557 lp_func_delete_body(function);
562 /* XXX: Generic code:
565 lp_emit_emms(struct gallivm_state *gallivm)
568 /* Avoid corrupting the FPU stack on 32bit OSes. */
569 lp_build_intrinsic(gallivm->builder, "llvm.x86.mmx.emms",
570 LLVMVoidTypeInContext(gallivm->context), NULL, 0);
575 /* XXX: generic code:
578 set_noalias(LLVMBuilderRef builder,
579 LLVMValueRef function,
580 const LLVMTypeRef *arg_types,
584 for(i = 0; i < Elements(arg_types); ++i)
585 if(LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind)
586 LLVMAddAttribute(LLVMGetParam(function, i),
587 LLVMNoAliasAttribute);
591 init_args(struct gallivm_state *gallivm,
592 struct lp_setup_args *args,
593 const struct lp_setup_variant *variant)
595 LLVMBuilderRef b = gallivm->builder;
597 LLVMValueRef v0_x = vert_attrib(gallivm, args->v0, 0, 0, "v0_x");
598 LLVMValueRef v0_y = vert_attrib(gallivm, args->v0, 0, 1, "v0_y");
600 LLVMValueRef v1_x = vert_attrib(gallivm, args->v1, 0, 0, "v1_x");
601 LLVMValueRef v1_y = vert_attrib(gallivm, args->v1, 0, 1, "v1_y");
603 LLVMValueRef v2_x = vert_attrib(gallivm, args->v2, 0, 0, "v2_x");
604 LLVMValueRef v2_y = vert_attrib(gallivm, args->v2, 0, 1, "v2_y");
606 LLVMValueRef pixel_center = lp_build_const_float(gallivm,
607 variant->key.pixel_center_half ? 0.5 : 0);
609 LLVMValueRef x0_center = LLVMBuildFSub(b, v0_x, pixel_center, "x0_center" );
610 LLVMValueRef y0_center = LLVMBuildFSub(b, v0_y, pixel_center, "y0_center" );
612 LLVMValueRef dx01 = LLVMBuildFSub(b, v0_x, v1_x, "dx01");
613 LLVMValueRef dy01 = LLVMBuildFSub(b, v0_y, v1_y, "dy01");
614 LLVMValueRef dx20 = LLVMBuildFSub(b, v2_x, v0_x, "dx20");
615 LLVMValueRef dy20 = LLVMBuildFSub(b, v2_y, v0_y, "dy20");
617 LLVMValueRef one = lp_build_const_float(gallivm, 1.0);
618 LLVMValueRef e = LLVMBuildFMul(b, dx01, dy20, "e");
619 LLVMValueRef f = LLVMBuildFMul(b, dx20, dy01, "f");
620 LLVMValueRef ooa = LLVMBuildFDiv(b, one, LLVMBuildFSub(b, e, f, ""), "ooa");
622 LLVMValueRef dy20_ooa = LLVMBuildFMul(b, dy20, ooa, "dy20_ooa");
623 LLVMValueRef dy01_ooa = LLVMBuildFMul(b, dy01, ooa, "dy01_ooa");
624 LLVMValueRef dx20_ooa = LLVMBuildFMul(b, dx20, ooa, "dx20_ooa");
625 LLVMValueRef dx01_ooa = LLVMBuildFMul(b, dx01, ooa, "dx01_ooa");
627 args->dy20_ooa = vec4f_from_scalar(gallivm, dy20_ooa, "dy20_ooa_4f");
628 args->dy01_ooa = vec4f_from_scalar(gallivm, dy01_ooa, "dy01_ooa_4f");
630 args->dx20_ooa = vec4f_from_scalar(gallivm, dx20_ooa, "dx20_ooa_4f");
631 args->dx01_ooa = vec4f_from_scalar(gallivm, dx01_ooa, "dx01_ooa_4f");
633 args->x0_center = vec4f_from_scalar(gallivm, x0_center, "x0_center_4f");
634 args->y0_center = vec4f_from_scalar(gallivm, y0_center, "y0_center_4f");
638 * Generate the runtime callable function for the coefficient calculation.
641 static struct lp_setup_variant *
642 generate_setup_variant(struct gallivm_state *gallivm,
643 struct lp_setup_variant_key *key,
644 struct llvmpipe_context *lp)
646 struct lp_setup_variant *variant = NULL;
647 struct lp_setup_args args;
649 LLVMTypeRef vec4f_type;
650 LLVMTypeRef func_type;
651 LLVMTypeRef arg_types[7];
652 LLVMBasicBlockRef block;
653 LLVMBuilderRef builder = gallivm->builder;
659 variant = CALLOC_STRUCT(lp_setup_variant);
663 if (LP_DEBUG & DEBUG_COUNTERS) {
667 memcpy(&variant->key, key, key->size);
668 variant->list_item_global.base = variant;
670 util_snprintf(func_name, sizeof(func_name), "fs%u_setup%u",
674 /* Currently always deal with full 4-wide vertex attributes from
678 vec4f_type = LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), 4);
680 arg_types[0] = LLVMPointerType(vec4f_type, 0); /* v0 */
681 arg_types[1] = LLVMPointerType(vec4f_type, 0); /* v1 */
682 arg_types[2] = LLVMPointerType(vec4f_type, 0); /* v2 */
683 arg_types[3] = LLVMInt32TypeInContext(gallivm->context); /* facing */
684 arg_types[4] = LLVMPointerType(vec4f_type, 0); /* a0, aligned */
685 arg_types[5] = LLVMPointerType(vec4f_type, 0); /* dadx, aligned */
686 arg_types[6] = LLVMPointerType(vec4f_type, 0); /* dady, aligned */
688 func_type = LLVMFunctionType(LLVMVoidTypeInContext(gallivm->context),
689 arg_types, Elements(arg_types), 0);
691 variant->function = LLVMAddFunction(gallivm->module, func_name, func_type);
692 if (!variant->function)
695 LLVMSetFunctionCallConv(variant->function, LLVMCCallConv);
697 args.v0 = LLVMGetParam(variant->function, 0);
698 args.v1 = LLVMGetParam(variant->function, 1);
699 args.v2 = LLVMGetParam(variant->function, 2);
700 args.facing = LLVMGetParam(variant->function, 3);
701 args.a0 = LLVMGetParam(variant->function, 4);
702 args.dadx = LLVMGetParam(variant->function, 5);
703 args.dady = LLVMGetParam(variant->function, 6);
705 lp_build_name(args.v0, "in_v0");
706 lp_build_name(args.v1, "in_v1");
707 lp_build_name(args.v2, "in_v2");
708 lp_build_name(args.facing, "in_facing");
709 lp_build_name(args.a0, "out_a0");
710 lp_build_name(args.dadx, "out_dadx");
711 lp_build_name(args.dady, "out_dady");
716 block = LLVMAppendBasicBlockInContext(gallivm->context,
717 variant->function, "entry");
718 LLVMPositionBuilderAtEnd(builder, block);
720 set_noalias(builder, variant->function, arg_types, Elements(arg_types));
721 init_args(gallivm, &args, variant);
722 emit_tri_coef(gallivm, &variant->key, &args);
724 lp_emit_emms(gallivm);
725 LLVMBuildRetVoid(builder);
727 variant->jit_function = finalize_function(gallivm, builder,
729 if (!variant->jit_function)
733 * Update timing information:
735 if (LP_DEBUG & DEBUG_COUNTERS) {
737 LP_COUNT_ADD(llvm_compile_time, t1 - t0);
738 LP_COUNT_ADD(nr_llvm_compiles, 1);
745 if (variant->function) {
746 if (variant->jit_function)
747 LLVMFreeMachineCodeForFunction(gallivm->engine,
749 LLVMDeleteFunction(variant->function);
760 lp_make_setup_variant_key(struct llvmpipe_context *lp,
761 struct lp_setup_variant_key *key)
763 struct lp_fragment_shader *fs = lp->fs;
766 assert(sizeof key->inputs[0] == sizeof(ushort));
768 key->num_inputs = fs->info.base.num_inputs;
769 key->flatshade_first = lp->rasterizer->flatshade_first;
770 key->pixel_center_half = lp->rasterizer->gl_rasterization_rules;
771 key->twoside = lp->rasterizer->light_twoside;
772 key->size = Offset(struct lp_setup_variant_key,
773 inputs[key->num_inputs]);
774 key->color_slot = lp->color_slot[0];
775 key->bcolor_slot = lp->bcolor_slot[0];
776 key->spec_slot = lp->color_slot[1];
777 key->bspec_slot = lp->bcolor_slot[1];
778 key->units = (float) (lp->rasterizer->offset_units * lp->mrd);
779 key->scale = lp->rasterizer->offset_scale;
781 memcpy(key->inputs, fs->inputs, key->num_inputs * sizeof key->inputs[0]);
782 for (i = 0; i < key->num_inputs; i++) {
783 if (key->inputs[i].interp == LP_INTERP_COLOR) {
784 if (lp->rasterizer->flatshade)
785 key->inputs[i].interp = LP_INTERP_CONSTANT;
787 key->inputs[i].interp = LP_INTERP_LINEAR;
795 remove_setup_variant(struct llvmpipe_context *lp,
796 struct lp_setup_variant *variant)
798 if (gallivm_debug & GALLIVM_DEBUG_IR) {
799 debug_printf("llvmpipe: del setup_variant #%u total %u\n",
800 variant->no, lp->nr_setup_variants);
803 if (variant->function) {
804 if (variant->jit_function)
805 LLVMFreeMachineCodeForFunction(lp->gallivm->engine,
807 LLVMDeleteFunction(variant->function);
810 remove_from_list(&variant->list_item_global);
811 lp->nr_setup_variants--;
817 /* When the number of setup variants exceeds a threshold, cull a
818 * fraction (currently a quarter) of them.
821 cull_setup_variants(struct llvmpipe_context *lp)
823 struct pipe_context *pipe = &lp->pipe;
827 * XXX: we need to flush the context until we have some sort of reference
828 * counting in fragment shaders as they may still be binned
829 * Flushing alone might not be sufficient we need to wait on it too.
831 llvmpipe_finish(pipe, __FUNCTION__);
833 for (i = 0; i < LP_MAX_SETUP_VARIANTS / 4; i++) {
834 struct lp_setup_variant_list_item *item = last_elem(&lp->setup_variants_list);
835 remove_setup_variant(lp, item->base);
841 * Update fragment/vertex shader linkage state. This is called just
842 * prior to drawing something when some fragment-related state has
846 llvmpipe_update_setup(struct llvmpipe_context *lp)
848 struct lp_setup_variant_key *key = &lp->setup_variant.key;
849 struct lp_setup_variant *variant = NULL;
850 struct lp_setup_variant_list_item *li;
852 lp_make_setup_variant_key(lp, key);
854 foreach(li, &lp->setup_variants_list) {
855 if(li->base->key.size == key->size &&
856 memcmp(&li->base->key, key, key->size) == 0) {
863 move_to_head(&lp->setup_variants_list, &variant->list_item_global);
866 if (lp->nr_setup_variants >= LP_MAX_SETUP_VARIANTS) {
867 cull_setup_variants(lp);
870 variant = generate_setup_variant(lp->gallivm, key, lp);
871 insert_at_head(&lp->setup_variants_list, &variant->list_item_global);
872 lp->nr_setup_variants++;
874 llvmpipe_variant_count++;
877 lp_setup_set_setup_variant(lp->setup,
882 lp_delete_setup_variants(struct llvmpipe_context *lp)
884 struct lp_setup_variant_list_item *li;
885 li = first_elem(&lp->setup_variants_list);
886 while(!at_end(&lp->setup_variants_list, li)) {
887 struct lp_setup_variant_list_item *next = next_elem(li);
888 remove_setup_variant(lp, li->base);
894 lp_dump_setup_coef( const struct lp_setup_variant_key *key,
895 const float (*sa0)[4],
896 const float (*sdadx)[4],
897 const float (*sdady)[4])
901 for (i = 0; i < NUM_CHANNELS; i++) {
902 float a0 = sa0 [0][i];
903 float dadx = sdadx[0][i];
904 float dady = sdady[0][i];
906 debug_printf("POS.%c: a0 = %f, dadx = %f, dady = %f\n",
911 for (slot = 0; slot < key->num_inputs; slot++) {
912 unsigned usage_mask = key->inputs[slot].usage_mask;
913 for (i = 0; i < NUM_CHANNELS; i++) {
914 if (usage_mask & (1 << i)) {
915 float a0 = sa0 [1 + slot][i];
916 float dadx = sdadx[1 + slot][i];
917 float dady = sdady[1 + slot][i];
919 debug_printf("IN[%u].%c: a0 = %f, dadx = %f, dady = %f\n",