llvmpipe: remove unused arg from jit_setup_tri function
[mesa.git] / src / gallium / drivers / llvmpipe / lp_state_setup.c
1 /**************************************************************************
2 *
3 * Copyright 2010 VMware.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
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_debug.h"
34 #include "gallivm/lp_bld_init.h"
35 #include "gallivm/lp_bld_intr.h"
36 #include <llvm-c/Analysis.h> /* for LLVMVerifyFunction */
37
38 #include "lp_perf.h"
39 #include "lp_debug.h"
40 #include "lp_flush.h"
41 #include "lp_screen.h"
42 #include "lp_context.h"
43 #include "lp_setup_context.h"
44 #include "lp_rast.h"
45 #include "lp_state.h"
46 #include "lp_state_fs.h"
47 #include "lp_state_setup.h"
48
49
50
51 /* currently organized to interpolate full float[4] attributes even
52 * when some elements are unused. Later, can pack vertex data more
53 * closely.
54 */
55
56
57 struct lp_setup_args
58 {
59 /* Function arguments:
60 */
61 LLVMValueRef v0;
62 LLVMValueRef v1;
63 LLVMValueRef v2;
64 LLVMValueRef facing; /* boolean */
65 LLVMValueRef a0;
66 LLVMValueRef dadx;
67 LLVMValueRef dady;
68
69 /* Derived:
70 */
71 LLVMValueRef x0_center;
72 LLVMValueRef y0_center;
73 LLVMValueRef dy20_ooa;
74 LLVMValueRef dy01_ooa;
75 LLVMValueRef dx20_ooa;
76 LLVMValueRef dx01_ooa;
77 };
78
79 static LLVMTypeRef type4f(void)
80 {
81 return LLVMVectorType(LLVMFloatType(), 4);
82 }
83
84
85 /* Equivalent of _mm_setr_ps(a,b,c,d)
86 */
87 static LLVMValueRef vec4f(LLVMBuilderRef bld,
88 LLVMValueRef a, LLVMValueRef b, LLVMValueRef c, LLVMValueRef d,
89 const char *name)
90 {
91 LLVMValueRef i0 = LLVMConstInt(LLVMInt32Type(), 0, 0);
92 LLVMValueRef i1 = LLVMConstInt(LLVMInt32Type(), 1, 0);
93 LLVMValueRef i2 = LLVMConstInt(LLVMInt32Type(), 2, 0);
94 LLVMValueRef i3 = LLVMConstInt(LLVMInt32Type(), 3, 0);
95
96 LLVMValueRef res = LLVMGetUndef(type4f());
97
98 res = LLVMBuildInsertElement(bld, res, a, i0, "");
99 res = LLVMBuildInsertElement(bld, res, b, i1, "");
100 res = LLVMBuildInsertElement(bld, res, c, i2, "");
101 res = LLVMBuildInsertElement(bld, res, d, i3, name);
102
103 return res;
104 }
105
106 /* Equivalent of _mm_set1_ps(a)
107 */
108 static LLVMValueRef vec4f_from_scalar(LLVMBuilderRef bld,
109 LLVMValueRef a,
110 const char *name)
111 {
112 LLVMValueRef res = LLVMGetUndef(type4f());
113 int i;
114
115 for(i = 0; i < 4; ++i) {
116 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
117 res = LLVMBuildInsertElement(bld, res, a, index, i == 3 ? name : "");
118 }
119
120 return res;
121 }
122
123 static void
124 store_coef(LLVMBuilderRef builder,
125 struct lp_setup_args *args,
126 unsigned slot,
127 LLVMValueRef a0,
128 LLVMValueRef dadx,
129 LLVMValueRef dady)
130 {
131 LLVMValueRef idx = LLVMConstInt(LLVMInt32Type(), slot, 0);
132
133 LLVMBuildStore(builder,
134 a0,
135 LLVMBuildGEP(builder, args->a0, &idx, 1, ""));
136
137 LLVMBuildStore(builder,
138 dadx,
139 LLVMBuildGEP(builder, args->dadx, &idx, 1, ""));
140
141 LLVMBuildStore(builder,
142 dady,
143 LLVMBuildGEP(builder, args->dady, &idx, 1, ""));
144 }
145
146
147
148 static void
149 emit_constant_coef4( LLVMBuilderRef builder,
150 struct lp_setup_args *args,
151 unsigned slot,
152 LLVMValueRef vert,
153 unsigned attr)
154 {
155 LLVMValueRef zero = LLVMConstReal(LLVMFloatType(), 0.0);
156 LLVMValueRef zerovec = vec4f_from_scalar(builder, zero, "zero");
157 LLVMValueRef idx = LLVMConstInt(LLVMInt32Type(), attr, 0);
158 LLVMValueRef attr_ptr = LLVMBuildGEP(builder, vert, &idx, 1, "attr_ptr");
159 LLVMValueRef vert_attr = LLVMBuildLoad(builder, attr_ptr, "vert_attr");
160
161 store_coef(builder, args, slot, vert_attr, zerovec, zerovec);
162 }
163
164
165
166 /**
167 * Setup the fragment input attribute with the front-facing value.
168 * \param frontface is the triangle front facing?
169 */
170 static void
171 emit_facing_coef( LLVMBuilderRef builder,
172 struct lp_setup_args *args,
173 unsigned slot )
174 {
175 LLVMValueRef a0_0 = args->facing;
176 LLVMValueRef a0_0f = LLVMBuildSIToFP(builder, a0_0, LLVMFloatType(), "");
177 LLVMValueRef zero = LLVMConstReal(LLVMFloatType(), 0.0);
178 LLVMValueRef a0 = vec4f(builder, a0_0f, zero, zero, zero, "facing");
179 LLVMValueRef zerovec = vec4f_from_scalar(builder, zero, "zero");
180
181 store_coef(builder, args, slot, a0, zerovec, zerovec);
182 }
183
184
185 static LLVMValueRef
186 vert_attrib(LLVMBuilderRef b,
187 LLVMValueRef vert,
188 int attr,
189 int elem,
190 const char *name)
191 {
192 LLVMValueRef idx[2];
193 idx[0] = LLVMConstInt(LLVMInt32Type(), attr, 0);
194 idx[1] = LLVMConstInt(LLVMInt32Type(), elem, 0);
195 return LLVMBuildLoad(b, LLVMBuildGEP(b, vert, idx, 2, ""), name);
196 }
197
198
199
200 static void
201 emit_coef4( LLVMBuilderRef b,
202 struct lp_setup_args *args,
203 unsigned slot,
204 LLVMValueRef a0,
205 LLVMValueRef a1,
206 LLVMValueRef a2)
207 {
208 LLVMValueRef dy20_ooa = args->dy20_ooa;
209 LLVMValueRef dy01_ooa = args->dy01_ooa;
210 LLVMValueRef dx20_ooa = args->dx20_ooa;
211 LLVMValueRef dx01_ooa = args->dx01_ooa;
212 LLVMValueRef x0_center = args->x0_center;
213 LLVMValueRef y0_center = args->y0_center;
214
215 /* XXX: using fsub, fmul on vector types -- does this work??
216 */
217 LLVMValueRef da01 = LLVMBuildFSub(b, a0, a1, "da01");
218 LLVMValueRef da20 = LLVMBuildFSub(b, a2, a0, "da20");
219
220 /* Calculate dadx (vec4f)
221 */
222 LLVMValueRef da01_dy20_ooa = LLVMBuildFMul(b, da01, dy20_ooa, "da01_dy20_ooa");
223 LLVMValueRef da20_dy01_ooa = LLVMBuildFMul(b, da20, dy01_ooa, "da20_dy01_ooa");
224 LLVMValueRef dadx = LLVMBuildFSub(b, da01_dy20_ooa, da20_dy01_ooa, "dadx");
225
226 /* Calculate dady (vec4f)
227 */
228 LLVMValueRef da01_dx20_ooa = LLVMBuildFMul(b, da01, dx20_ooa, "da01_dx20_ooa");
229 LLVMValueRef da20_dx01_ooa = LLVMBuildFMul(b, da20, dx01_ooa, "da20_dx01_ooa");
230 LLVMValueRef dady = LLVMBuildFSub(b, da20_dx01_ooa, da01_dx20_ooa, "dady");
231
232 /* Calculate a0 - the attribute value at the origin
233 */
234 LLVMValueRef dadx_x0 = LLVMBuildFMul(b, dadx, x0_center, "dadx_x0");
235 LLVMValueRef dady_y0 = LLVMBuildFMul(b, dady, y0_center, "dady_y0");
236 LLVMValueRef attr_v0 = LLVMBuildFAdd(b, dadx_x0, dady_y0, "attr_v0");
237 LLVMValueRef attr_0 = LLVMBuildFSub(b, a0, attr_v0, "attr_0");
238
239 store_coef(b, args, slot, attr_0, dadx, dady);
240 }
241
242
243 static void
244 emit_linear_coef( LLVMBuilderRef b,
245 struct lp_setup_args *args,
246 unsigned slot,
247 unsigned vert_attr)
248 {
249 LLVMValueRef idx = LLVMConstInt(LLVMInt32Type(), vert_attr, 0);
250
251 LLVMValueRef a0 = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v0, &idx, 1, ""), "v0a");
252 LLVMValueRef a1 = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v1, &idx, 1, ""), "v1a");
253 LLVMValueRef a2 = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v2, &idx, 1, ""), "v2a");
254
255 emit_coef4(b, args, slot, a0, a1, a2);
256 }
257
258
259
260 /**
261 * Compute a0, dadx and dady for a perspective-corrected interpolant,
262 * for a triangle.
263 * We basically multiply the vertex value by 1/w before computing
264 * the plane coefficients (a0, dadx, dady).
265 * Later, when we compute the value at a particular fragment position we'll
266 * divide the interpolated value by the interpolated W at that fragment.
267 */
268 static void
269 emit_perspective_coef( LLVMBuilderRef b,
270 struct lp_setup_args *args,
271 unsigned slot,
272 unsigned vert_attr)
273 {
274 /* premultiply by 1/w (v[0][3] is always 1/w):
275 */
276 LLVMValueRef idx = LLVMConstInt(LLVMInt32Type(), vert_attr, 0);
277
278 LLVMValueRef v0a = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v0, &idx, 1, ""), "v0a");
279 LLVMValueRef v1a = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v1, &idx, 1, ""), "v1a");
280 LLVMValueRef v2a = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v2, &idx, 1, ""), "v2a");
281
282 LLVMValueRef v0_oow = vec4f_from_scalar(b, vert_attrib(b, args->v0, 0, 3, ""), "v0_oow");
283 LLVMValueRef v1_oow = vec4f_from_scalar(b, vert_attrib(b, args->v1, 0, 3, ""), "v1_oow");
284 LLVMValueRef v2_oow = vec4f_from_scalar(b, vert_attrib(b, args->v2, 0, 3, ""), "v2_oow");
285
286 LLVMValueRef v0_oow_v0a = LLVMBuildFMul(b, v0a, v0_oow, "v0_oow_v0a");
287 LLVMValueRef v1_oow_v1a = LLVMBuildFMul(b, v1a, v1_oow, "v1_oow_v1a");
288 LLVMValueRef v2_oow_v2a = LLVMBuildFMul(b, v2a, v2_oow, "v2_oow_v2a");
289
290 emit_coef4(b, args, slot, v0_oow_v0a, v1_oow_v1a, v2_oow_v2a);
291 }
292
293
294 static void
295 emit_position_coef( LLVMBuilderRef builder,
296 struct lp_setup_args *args,
297 int slot, int attrib )
298 {
299 emit_linear_coef(builder, args, slot, attrib);
300 }
301
302
303
304
305 /**
306 * Compute the inputs-> dadx, dady, a0 values.
307 */
308 static void
309 emit_tri_coef( LLVMBuilderRef builder,
310 const struct lp_setup_variant_key *key,
311 struct lp_setup_args *args )
312 {
313 unsigned slot;
314
315 /* The internal position input is in slot zero:
316 */
317 emit_position_coef(builder, args, 0, 0);
318
319 /* setup interpolation for all the remaining attributes:
320 */
321 for (slot = 0; slot < key->num_inputs; slot++) {
322 unsigned vert_attr = key->inputs[slot].src_index;
323
324 switch (key->inputs[slot].interp) {
325 case LP_INTERP_CONSTANT:
326 if (key->flatshade_first) {
327 emit_constant_coef4(builder, args, slot+1, args->v0, vert_attr);
328 }
329 else {
330 emit_constant_coef4(builder, args, slot+1, args->v2, vert_attr);
331 }
332 break;
333
334 case LP_INTERP_LINEAR:
335 emit_linear_coef(builder, args, slot+1, vert_attr);
336 break;
337
338 case LP_INTERP_PERSPECTIVE:
339 emit_perspective_coef(builder, args, slot+1, vert_attr);
340 break;
341
342 case LP_INTERP_POSITION:
343 /*
344 * The generated pixel interpolators will pick up the coeffs from
345 * slot 0.
346 */
347 break;
348
349 case LP_INTERP_FACING:
350 emit_facing_coef(builder, args, slot+1);
351 break;
352
353 default:
354 assert(0);
355 }
356 }
357 }
358
359
360 /* XXX: This is generic code, share with fs/vs codegen:
361 */
362 static lp_jit_setup_triangle
363 finalize_function(struct llvmpipe_screen *screen,
364 LLVMBuilderRef builder,
365 LLVMValueRef function)
366 {
367 void *f;
368
369 /* Verify the LLVM IR. If invalid, dump and abort */
370 #ifdef DEBUG
371 if (LLVMVerifyFunction(function, LLVMPrintMessageAction)) {
372 if (1)
373 lp_debug_dump_value(function);
374 abort();
375 }
376 #endif
377
378 /* Apply optimizations to LLVM IR */
379 LLVMRunFunctionPassManager(screen->pass, function);
380
381 if (gallivm_debug & GALLIVM_DEBUG_IR)
382 {
383 /* Print the LLVM IR to stderr */
384 lp_debug_dump_value(function);
385 debug_printf("\n");
386 }
387
388 /*
389 * Translate the LLVM IR into machine code.
390 */
391 f = LLVMGetPointerToGlobal(screen->engine, function);
392
393 if (gallivm_debug & GALLIVM_DEBUG_ASM)
394 {
395 lp_disassemble(f);
396 }
397
398 lp_func_delete_body(function);
399
400 return f;
401 }
402
403 /* XXX: Generic code:
404 */
405 static void
406 lp_emit_emms(LLVMBuilderRef builder)
407 {
408 #ifdef PIPE_ARCH_X86
409 /* Avoid corrupting the FPU stack on 32bit OSes. */
410 lp_build_intrinsic(builder, "llvm.x86.mmx.emms", LLVMVoidType(), NULL, 0);
411 #endif
412 }
413
414
415 /* XXX: generic code:
416 */
417 static void
418 set_noalias(LLVMBuilderRef builder,
419 LLVMValueRef function,
420 const LLVMTypeRef *arg_types,
421 int nr_args)
422 {
423 int i;
424 for(i = 0; i < Elements(arg_types); ++i)
425 if(LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind)
426 LLVMAddAttribute(LLVMGetParam(function, i),
427 LLVMNoAliasAttribute);
428 }
429
430 static void
431 init_args(LLVMBuilderRef b,
432 struct lp_setup_args *args,
433 const struct lp_setup_variant *variant)
434 {
435 LLVMValueRef v0_x = vert_attrib(b, args->v0, 0, 0, "v0_x");
436 LLVMValueRef v0_y = vert_attrib(b, args->v0, 0, 1, "v0_y");
437
438 LLVMValueRef v1_x = vert_attrib(b, args->v1, 0, 0, "v1_x");
439 LLVMValueRef v1_y = vert_attrib(b, args->v1, 0, 1, "v1_y");
440
441 LLVMValueRef v2_x = vert_attrib(b, args->v2, 0, 0, "v2_x");
442 LLVMValueRef v2_y = vert_attrib(b, args->v2, 0, 1, "v2_y");
443
444 LLVMValueRef pixel_center = LLVMConstReal(LLVMFloatType(),
445 variant->key.pixel_center_half ? 0.5 : 0);
446
447 LLVMValueRef x0_center = LLVMBuildFSub(b, v0_x, pixel_center, "x0_center" );
448 LLVMValueRef y0_center = LLVMBuildFSub(b, v0_y, pixel_center, "y0_center" );
449
450 LLVMValueRef dx01 = LLVMBuildFSub(b, v0_x, v1_x, "dx01");
451 LLVMValueRef dy01 = LLVMBuildFSub(b, v0_y, v1_y, "dy01");
452 LLVMValueRef dx20 = LLVMBuildFSub(b, v2_x, v0_x, "dx20");
453 LLVMValueRef dy20 = LLVMBuildFSub(b, v2_y, v0_y, "dy20");
454
455 LLVMValueRef one = LLVMConstReal(LLVMFloatType(), 1.0);
456 LLVMValueRef e = LLVMBuildFMul(b, dx01, dy20, "e");
457 LLVMValueRef f = LLVMBuildFMul(b, dx20, dy01, "f");
458 LLVMValueRef ooa = LLVMBuildFDiv(b, one, LLVMBuildFSub(b, e, f, ""), "ooa");
459
460 LLVMValueRef dy20_ooa = LLVMBuildFMul(b, dy20, ooa, "dy20_ooa");
461 LLVMValueRef dy01_ooa = LLVMBuildFMul(b, dy01, ooa, "dy01_ooa");
462 LLVMValueRef dx20_ooa = LLVMBuildFMul(b, dx20, ooa, "dx20_ooa");
463 LLVMValueRef dx01_ooa = LLVMBuildFMul(b, dx01, ooa, "dx01_ooa");
464
465 args->dy20_ooa = vec4f_from_scalar(b, dy20_ooa, "dy20_ooa_4f");
466 args->dy01_ooa = vec4f_from_scalar(b, dy01_ooa, "dy01_ooa_4f");
467
468 args->dx20_ooa = vec4f_from_scalar(b, dx20_ooa, "dx20_ooa_4f");
469 args->dx01_ooa = vec4f_from_scalar(b, dx01_ooa, "dx01_ooa_4f");
470
471 args->x0_center = vec4f_from_scalar(b, x0_center, "x0_center_4f");
472 args->y0_center = vec4f_from_scalar(b, y0_center, "y0_center_4f");
473 }
474
475 /**
476 * Generate the runtime callable function for the coefficient calculation.
477 *
478 */
479 static struct lp_setup_variant *
480 generate_setup_variant(struct llvmpipe_screen *screen,
481 struct lp_setup_variant_key *key)
482 {
483 struct lp_setup_variant *variant = NULL;
484 struct lp_setup_args args;
485 char func_name[256];
486 LLVMTypeRef vec4f_type;
487 LLVMTypeRef func_type;
488 LLVMTypeRef arg_types[7];
489 LLVMBasicBlockRef block;
490 LLVMBuilderRef builder;
491 int64_t t0, t1;
492
493 if (0)
494 goto fail;
495
496 variant = CALLOC_STRUCT(lp_setup_variant);
497 if (variant == NULL)
498 goto fail;
499
500 if (LP_DEBUG & DEBUG_COUNTERS) {
501 t0 = os_time_get();
502 }
503
504 memcpy(&variant->key, key, key->size);
505 variant->list_item_global.base = variant;
506
507 util_snprintf(func_name, sizeof(func_name), "fs%u_setup%u",
508 0,
509 variant->no);
510
511 /* Currently always deal with full 4-wide vertex attributes from
512 * the vertices.
513 */
514
515 vec4f_type = LLVMVectorType(LLVMFloatType(), 4);
516
517 arg_types[0] = LLVMPointerType(vec4f_type, 0); /* v0 */
518 arg_types[1] = LLVMPointerType(vec4f_type, 0); /* v1 */
519 arg_types[2] = LLVMPointerType(vec4f_type, 0); /* v2 */
520 arg_types[3] = LLVMInt32Type(); /* facing */
521 arg_types[4] = LLVMPointerType(vec4f_type, 0); /* a0, aligned */
522 arg_types[5] = LLVMPointerType(vec4f_type, 0); /* dadx, aligned */
523 arg_types[6] = LLVMPointerType(vec4f_type, 0); /* dady, aligned */
524
525 func_type = LLVMFunctionType(LLVMVoidType(), arg_types, Elements(arg_types), 0);
526
527 variant->function = LLVMAddFunction(screen->module, func_name, func_type);
528 if (!variant->function)
529 goto fail;
530
531 LLVMSetFunctionCallConv(variant->function, LLVMCCallConv);
532
533 args.v0 = LLVMGetParam(variant->function, 0);
534 args.v1 = LLVMGetParam(variant->function, 1);
535 args.v2 = LLVMGetParam(variant->function, 2);
536 args.facing = LLVMGetParam(variant->function, 3);
537 args.a0 = LLVMGetParam(variant->function, 4);
538 args.dadx = LLVMGetParam(variant->function, 5);
539 args.dady = LLVMGetParam(variant->function, 6);
540
541 lp_build_name(args.v0, "in_v0");
542 lp_build_name(args.v1, "in_v1");
543 lp_build_name(args.v2, "in_v2");
544 lp_build_name(args.facing, "in_facing");
545 lp_build_name(args.a0, "out_a0");
546 lp_build_name(args.dadx, "out_dadx");
547 lp_build_name(args.dady, "out_dady");
548
549 /*
550 * Function body
551 */
552 block = LLVMAppendBasicBlock(variant->function, "entry");
553 builder = LLVMCreateBuilder();
554 LLVMPositionBuilderAtEnd(builder, block);
555
556 set_noalias(builder, variant->function, arg_types, Elements(arg_types));
557 init_args(builder, &args, variant);
558 emit_tri_coef(builder, &variant->key, &args);
559
560 lp_emit_emms(builder);
561 LLVMBuildRetVoid(builder);
562 LLVMDisposeBuilder(builder);
563
564 variant->jit_function = finalize_function(screen, builder,
565 variant->function);
566 if (!variant->jit_function)
567 goto fail;
568
569 /*
570 * Update timing information:
571 */
572 if (LP_DEBUG & DEBUG_COUNTERS) {
573 t1 = os_time_get();
574 LP_COUNT_ADD(llvm_compile_time, t1 - t0);
575 LP_COUNT_ADD(nr_llvm_compiles, 1);
576 }
577
578 return variant;
579
580 fail:
581 if (variant) {
582 if (variant->function) {
583 if (variant->jit_function)
584 LLVMFreeMachineCodeForFunction(screen->engine,
585 variant->function);
586 LLVMDeleteFunction(variant->function);
587 }
588 FREE(variant);
589 }
590
591 return NULL;
592 }
593
594
595
596 static void
597 lp_make_setup_variant_key(struct llvmpipe_context *lp,
598 struct lp_setup_variant_key *key)
599 {
600 struct lp_fragment_shader *fs = lp->fs;
601 unsigned i;
602
603 assert(sizeof key->inputs[0] == sizeof(ushort));
604
605 key->num_inputs = fs->info.base.num_inputs;
606 key->flatshade_first = lp->rasterizer->flatshade_first;
607 key->pixel_center_half = lp->rasterizer->gl_rasterization_rules;
608 key->size = Offset(struct lp_setup_variant_key,
609 inputs[key->num_inputs]);
610 key->pad = 0;
611
612 memcpy(key->inputs, fs->inputs, key->num_inputs * sizeof key->inputs[0]);
613 for (i = 0; i < key->num_inputs; i++) {
614 if (key->inputs[i].interp == LP_INTERP_COLOR) {
615 if (lp->rasterizer->flatshade)
616 key->inputs[i].interp = LP_INTERP_CONSTANT;
617 else
618 key->inputs[i].interp = LP_INTERP_LINEAR;
619 }
620 }
621
622 }
623
624
625 static void
626 remove_setup_variant(struct llvmpipe_context *lp,
627 struct lp_setup_variant *variant)
628 {
629 struct llvmpipe_screen *screen = llvmpipe_screen(lp->pipe.screen);
630
631 if (gallivm_debug & GALLIVM_DEBUG_IR) {
632 debug_printf("llvmpipe: del setup_variant #%u total %u\n",
633 variant->no, lp->nr_setup_variants);
634 }
635
636 if (variant->function) {
637 if (variant->jit_function)
638 LLVMFreeMachineCodeForFunction(screen->engine,
639 variant->function);
640 LLVMDeleteFunction(variant->function);
641 }
642
643 remove_from_list(&variant->list_item_global);
644 lp->nr_setup_variants--;
645 FREE(variant);
646 }
647
648
649
650 /* When the number of setup variants exceeds a threshold, cull a
651 * fraction (currently a quarter) of them.
652 */
653 static void
654 cull_setup_variants(struct llvmpipe_context *lp)
655 {
656 struct pipe_context *pipe = &lp->pipe;
657 int i;
658
659 /*
660 * XXX: we need to flush the context until we have some sort of reference
661 * counting in fragment shaders as they may still be binned
662 * Flushing alone might not be sufficient we need to wait on it too.
663 */
664 llvmpipe_finish(pipe, __FUNCTION__);
665
666 for (i = 0; i < LP_MAX_SETUP_VARIANTS / 4; i++) {
667 struct lp_setup_variant_list_item *item = last_elem(&lp->setup_variants_list);
668 remove_setup_variant(lp, item->base);
669 }
670 }
671
672
673 /**
674 * Update fragment/vertex shader linkage state. This is called just
675 * prior to drawing something when some fragment-related state has
676 * changed.
677 */
678 void
679 llvmpipe_update_setup(struct llvmpipe_context *lp)
680 {
681 struct llvmpipe_screen *screen = llvmpipe_screen(lp->pipe.screen);
682
683 struct lp_setup_variant_key *key = &lp->setup_variant.key;
684 struct lp_setup_variant *variant = NULL;
685 struct lp_setup_variant_list_item *li;
686
687 lp_make_setup_variant_key(lp, key);
688
689 foreach(li, &lp->setup_variants_list) {
690 if(li->base->key.size == key->size &&
691 memcmp(&li->base->key, key, key->size) == 0) {
692 variant = li->base;
693 break;
694 }
695 }
696
697 if (variant) {
698 move_to_head(&lp->setup_variants_list, &variant->list_item_global);
699 }
700 else {
701 if (lp->nr_setup_variants >= LP_MAX_SETUP_VARIANTS) {
702 cull_setup_variants(lp);
703 }
704
705 variant = generate_setup_variant(screen, key);
706 insert_at_head(&lp->setup_variants_list, &variant->list_item_global);
707 lp->nr_setup_variants++;
708 }
709
710 lp_setup_set_setup_variant(lp->setup,
711 variant);
712 }
713
714 void
715 lp_delete_setup_variants(struct llvmpipe_context *lp)
716 {
717 struct lp_setup_variant_list_item *li;
718 li = first_elem(&lp->setup_variants_list);
719 while(!at_end(&lp->setup_variants_list, li)) {
720 struct lp_setup_variant_list_item *next = next_elem(li);
721 remove_setup_variant(lp, li->base);
722 li = next;
723 }
724 }
725
726 void
727 lp_dump_setup_coef( const struct lp_setup_variant_key *key,
728 const float (*sa0)[4],
729 const float (*sdadx)[4],
730 const float (*sdady)[4])
731 {
732 int i, slot;
733
734 for (i = 0; i < NUM_CHANNELS; i++) {
735 float a0 = sa0 [0][i];
736 float dadx = sdadx[0][i];
737 float dady = sdady[0][i];
738
739 debug_printf("POS.%c: a0 = %f, dadx = %f, dady = %f\n",
740 "xyzw"[i],
741 a0, dadx, dady);
742 }
743
744 for (slot = 0; slot < key->num_inputs; slot++) {
745 unsigned usage_mask = key->inputs[slot].usage_mask;
746 for (i = 0; i < NUM_CHANNELS; i++) {
747 if (usage_mask & (1 << i)) {
748 float a0 = sa0 [1 + slot][i];
749 float dadx = sdadx[1 + slot][i];
750 float dady = sdady[1 + slot][i];
751
752 debug_printf("IN[%u].%c: a0 = %f, dadx = %f, dady = %f\n",
753 slot,
754 "xyzw"[i],
755 a0, dadx, dady);
756 }
757 }
758 }
759 }