Merge branch 'lp-offset-twoside'
[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_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 */
40
41 #include "lp_perf.h"
42 #include "lp_debug.h"
43 #include "lp_flush.h"
44 #include "lp_screen.h"
45 #include "lp_context.h"
46 #include "lp_setup_context.h"
47 #include "lp_rast.h"
48 #include "lp_state.h"
49 #include "lp_state_fs.h"
50 #include "lp_state_setup.h"
51
52
53
54 /* currently organized to interpolate full float[4] attributes even
55 * when some elements are unused. Later, can pack vertex data more
56 * closely.
57 */
58
59
60 struct lp_setup_args
61 {
62 /* Function arguments:
63 */
64 LLVMValueRef v0;
65 LLVMValueRef v1;
66 LLVMValueRef v2;
67 LLVMValueRef facing; /* boolean */
68 LLVMValueRef a0;
69 LLVMValueRef dadx;
70 LLVMValueRef dady;
71
72 /* Derived:
73 */
74 LLVMValueRef x0_center;
75 LLVMValueRef y0_center;
76 LLVMValueRef dy20_ooa;
77 LLVMValueRef dy01_ooa;
78 LLVMValueRef dx20_ooa;
79 LLVMValueRef dx01_ooa;
80
81 /* Temporary, per-attribute:
82 */
83 LLVMValueRef v0a;
84 LLVMValueRef v1a;
85 LLVMValueRef v2a;
86 };
87
88 static LLVMTypeRef type4f(void)
89 {
90 return LLVMVectorType(LLVMFloatType(), 4);
91 }
92
93
94 /* Equivalent of _mm_setr_ps(a,b,c,d)
95 */
96 static LLVMValueRef vec4f(LLVMBuilderRef bld,
97 LLVMValueRef a, LLVMValueRef b, LLVMValueRef c, LLVMValueRef d,
98 const char *name)
99 {
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);
104
105 LLVMValueRef res = LLVMGetUndef(type4f());
106
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);
111
112 return res;
113 }
114
115 /* Equivalent of _mm_set1_ps(a)
116 */
117 static LLVMValueRef vec4f_from_scalar(LLVMBuilderRef bld,
118 LLVMValueRef a,
119 const char *name)
120 {
121 LLVMValueRef res = LLVMGetUndef(type4f());
122 int i;
123
124 for(i = 0; i < 4; ++i) {
125 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
126 res = LLVMBuildInsertElement(bld, res, a, index, i == 3 ? name : "");
127 }
128
129 return res;
130 }
131
132 static void
133 store_coef(LLVMBuilderRef builder,
134 struct lp_setup_args *args,
135 unsigned slot,
136 LLVMValueRef a0,
137 LLVMValueRef dadx,
138 LLVMValueRef dady)
139 {
140 LLVMValueRef idx = LLVMConstInt(LLVMInt32Type(), slot, 0);
141
142 LLVMBuildStore(builder,
143 a0,
144 LLVMBuildGEP(builder, args->a0, &idx, 1, ""));
145
146 LLVMBuildStore(builder,
147 dadx,
148 LLVMBuildGEP(builder, args->dadx, &idx, 1, ""));
149
150 LLVMBuildStore(builder,
151 dady,
152 LLVMBuildGEP(builder, args->dady, &idx, 1, ""));
153 }
154
155
156
157 static void
158 emit_constant_coef4( LLVMBuilderRef builder,
159 struct lp_setup_args *args,
160 unsigned slot,
161 LLVMValueRef vert)
162 {
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);
166 }
167
168
169
170 /**
171 * Setup the fragment input attribute with the front-facing value.
172 * \param frontface is the triangle front facing?
173 */
174 static void
175 emit_facing_coef( LLVMBuilderRef builder,
176 struct lp_setup_args *args,
177 unsigned slot )
178 {
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");
184
185 store_coef(builder, args, slot, a0, zerovec, zerovec);
186 }
187
188
189 static LLVMValueRef
190 vert_attrib(LLVMBuilderRef b,
191 LLVMValueRef vert,
192 int attr,
193 int elem,
194 const char *name)
195 {
196 LLVMValueRef idx[2];
197 idx[0] = LLVMConstInt(LLVMInt32Type(), attr, 0);
198 idx[1] = LLVMConstInt(LLVMInt32Type(), elem, 0);
199 return LLVMBuildLoad(b, LLVMBuildGEP(b, vert, idx, 2, ""), name);
200 }
201
202 static LLVMValueRef
203 vert_clamp(LLVMBuilderRef b,
204 LLVMValueRef x,
205 LLVMValueRef min,
206 LLVMValueRef max)
207 {
208 LLVMValueRef min_result = LLVMBuildFCmp(b, LLVMRealUGT, min, x, "");
209 LLVMValueRef max_result = LLVMBuildFCmp(b, LLVMRealUGT, x, max, "");
210 LLVMValueRef clamp_value;
211
212 clamp_value = LLVMBuildSelect(b, min_result, min, x, "");
213 clamp_value = LLVMBuildSelect(b, max_result, max, x, "");
214
215 return clamp_value;
216 }
217
218 static void
219 lp_twoside(LLVMBuilderRef b,
220 struct lp_setup_args *args,
221 const struct lp_setup_variant_key *key,
222 int bcolor_slot)
223 {
224 LLVMValueRef a0_back, a1_back, a2_back;
225 LLVMValueRef idx2 = LLVMConstInt(LLVMInt32Type(), bcolor_slot, 0);
226
227 LLVMValueRef facing = args->facing;
228 LLVMValueRef front_facing = LLVMBuildICmp(b, LLVMIntEQ, facing, LLVMConstInt(LLVMInt32Type(), 0, 0), ""); /** need i1 for if condition */
229
230 a0_back = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v0, &idx2, 1, ""), "v0a_back");
231 a1_back = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v1, &idx2, 1, ""), "v1a_back");
232 a2_back = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v2, &idx2, 1, ""), "v2a_back");
233
234 /* Possibly swap the front and back attrib values,
235 *
236 * Prefer select to if so we don't have to worry about phis or
237 * allocas.
238 */
239 args->v0a = LLVMBuildSelect(b, front_facing, a0_back, args->v0a, "");
240 args->v1a = LLVMBuildSelect(b, front_facing, a1_back, args->v1a, "");
241 args->v2a = LLVMBuildSelect(b, front_facing, a2_back, args->v2a, "");
242
243 }
244
245 static void
246 lp_do_offset_tri(LLVMBuilderRef b,
247 struct lp_setup_args *args,
248 const struct lp_setup_variant_key *key)
249 {
250 struct lp_build_context bld;
251 LLVMValueRef zoffset, mult;
252 LLVMValueRef z0_new, z1_new, z2_new;
253 LLVMValueRef dzdx0, dzdx, dzdy0, dzdy;
254 LLVMValueRef max, max_value;
255
256 LLVMValueRef one = LLVMConstReal(LLVMFloatType(), 1.0);
257 LLVMValueRef zero = LLVMConstReal(LLVMFloatType(), 0.0);
258
259 /* edge vectors: e = v0 - v2, f = v1 - v2 */
260 LLVMValueRef v0_x = vert_attrib(b, args->v0, 0, 0, "v0_x");
261 LLVMValueRef v1_x = vert_attrib(b, args->v1, 0, 0, "v1_x");
262 LLVMValueRef v2_x = vert_attrib(b, args->v2, 0, 0, "v2_x");
263 LLVMValueRef v0_y = vert_attrib(b, args->v0, 0, 1, "v0_y");
264 LLVMValueRef v1_y = vert_attrib(b, args->v1, 0, 1, "v1_y");
265 LLVMValueRef v2_y = vert_attrib(b, args->v2, 0, 1, "v2_y");
266 LLVMValueRef v0_z = vert_attrib(b, args->v0, 0, 2, "v0_z");
267 LLVMValueRef v1_z = vert_attrib(b, args->v1, 0, 2, "v1_z");
268 LLVMValueRef v2_z = vert_attrib(b, args->v2, 0, 2, "v2_z");
269
270 /* edge vectors: e = v0 - v2, f = v1 - v2 */
271 LLVMValueRef dx02 = LLVMBuildFSub(b, v0_x, v2_x, "dx02");
272 LLVMValueRef dy02 = LLVMBuildFSub(b, v0_y, v2_y, "dy02");
273 LLVMValueRef dz02 = LLVMBuildFSub(b, v0_z, v2_z, "dz02");
274 LLVMValueRef dx12 = LLVMBuildFSub(b, v1_x, v2_x, "dx12");
275 LLVMValueRef dy12 = LLVMBuildFSub(b, v1_y, v2_y, "dy12");
276 LLVMValueRef dz12 = LLVMBuildFSub(b, v1_z, v2_z, "dz12");
277
278 /* det = cross(e,f).z */
279 LLVMValueRef dx02_dy12 = LLVMBuildFMul(b, dx02, dy12, "dx02_dy12");
280 LLVMValueRef dy02_dx12 = LLVMBuildFMul(b, dy02, dx12, "dy02_dx12");
281 LLVMValueRef det = LLVMBuildFSub(b, dx02_dy12, dy02_dx12, "det");
282 LLVMValueRef inv_det = LLVMBuildFDiv(b, one, det, "inv_det");
283
284 /* (res1,res2) = cross(e,f).xy */
285 LLVMValueRef dy02_dz12 = LLVMBuildFMul(b, dy02, dz12, "dy02_dz12");
286 LLVMValueRef dz02_dy12 = LLVMBuildFMul(b, dz02, dy12, "dz02_dy12");
287 LLVMValueRef dz02_dx12 = LLVMBuildFMul(b, dz02, dx12, "dz02_dx12");
288 LLVMValueRef dx02_dz12 = LLVMBuildFMul(b, dx02, dz12, "dx02_dz12");
289 LLVMValueRef res1 = LLVMBuildFSub(b, dy02_dz12, dz02_dy12, "res1");
290 LLVMValueRef res2 = LLVMBuildFSub(b, dz02_dx12, dx02_dz12, "res2");
291
292 /* dzdx = fabsf(res1 * inv_det), dydx = fabsf(res2 * inv_det)*/
293 lp_build_context_init(&bld, b, lp_type_float(32));
294 dzdx0 = LLVMBuildFMul(b, res1, inv_det, "dzdx");
295 dzdx = lp_build_abs(&bld, dzdx0);
296 dzdy0 = LLVMBuildFMul(b, res2, inv_det, "dzdy");
297 dzdy = lp_build_abs(&bld, dzdy0);
298
299 /* zoffset = offset->units + MAX2(dzdx, dzdy) * offset->scale */
300 max = LLVMBuildFCmp(b, LLVMRealUGT, dzdx, dzdy, "");
301 max_value = LLVMBuildSelect(b, max, dzdx, dzdy, "max");
302
303 mult = LLVMBuildFMul(b, max_value, LLVMConstReal(LLVMFloatType(), key->scale), "");
304 zoffset = LLVMBuildFAdd(b, LLVMConstReal(LLVMFloatType(), key->units), mult, "zoffset");
305
306 /* clamp and do offset */
307 z0_new = vert_clamp(b, LLVMBuildFAdd(b, v0_z, zoffset, ""), zero, one);
308 z1_new = vert_clamp(b, LLVMBuildFAdd(b, v1_z, zoffset, ""), zero, one);
309 z2_new = vert_clamp(b, LLVMBuildFAdd(b, v2_z, zoffset, ""), zero, one);
310
311 /* insert into args->a0.z, a1.z, a2.z:
312 */
313 args->v0a = LLVMBuildInsertElement(b, args->v0a, z0_new, LLVMConstInt(LLVMInt32Type(), 2, 0), "");
314 args->v1a = LLVMBuildInsertElement(b, args->v1a, z1_new, LLVMConstInt(LLVMInt32Type(), 2, 0), "");
315 args->v2a = LLVMBuildInsertElement(b, args->v2a, z2_new, LLVMConstInt(LLVMInt32Type(), 2, 0), "");
316 }
317
318 static void
319 load_attribute(LLVMBuilderRef b,
320 struct lp_setup_args *args,
321 const struct lp_setup_variant_key *key,
322 unsigned vert_attr)
323 {
324 LLVMValueRef idx = LLVMConstInt(LLVMInt32Type(), vert_attr, 0);
325
326 /* Load the vertex data
327 */
328 args->v0a = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v0, &idx, 1, ""), "v0a");
329 args->v1a = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v1, &idx, 1, ""), "v1a");
330 args->v2a = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v2, &idx, 1, ""), "v2a");
331
332
333 /* Potentially modify it according to twoside, offset, etc:
334 */
335 if (vert_attr == 0 && (key->scale != 0.0f || key->units != 0.0f)) {
336 lp_do_offset_tri(b, args, key);
337 }
338
339 if (key->twoside) {
340 if (vert_attr == key->color_slot && key->bcolor_slot != ~0)
341 lp_twoside(b, args, key, key->bcolor_slot);
342 else if (vert_attr == key->spec_slot && key->bspec_slot != ~0)
343 lp_twoside(b, args, key, key->bspec_slot);
344 }
345 }
346
347 static void
348 emit_coef4( LLVMBuilderRef b,
349 struct lp_setup_args *args,
350 unsigned slot,
351 LLVMValueRef a0,
352 LLVMValueRef a1,
353 LLVMValueRef a2)
354 {
355 LLVMValueRef dy20_ooa = args->dy20_ooa;
356 LLVMValueRef dy01_ooa = args->dy01_ooa;
357 LLVMValueRef dx20_ooa = args->dx20_ooa;
358 LLVMValueRef dx01_ooa = args->dx01_ooa;
359 LLVMValueRef x0_center = args->x0_center;
360 LLVMValueRef y0_center = args->y0_center;
361
362 /* XXX: using fsub, fmul on vector types -- does this work??
363 */
364 LLVMValueRef da01 = LLVMBuildFSub(b, a0, a1, "da01");
365 LLVMValueRef da20 = LLVMBuildFSub(b, a2, a0, "da20");
366
367 /* Calculate dadx (vec4f)
368 */
369 LLVMValueRef da01_dy20_ooa = LLVMBuildFMul(b, da01, dy20_ooa, "da01_dy20_ooa");
370 LLVMValueRef da20_dy01_ooa = LLVMBuildFMul(b, da20, dy01_ooa, "da20_dy01_ooa");
371 LLVMValueRef dadx = LLVMBuildFSub(b, da01_dy20_ooa, da20_dy01_ooa, "dadx");
372
373 /* Calculate dady (vec4f)
374 */
375 LLVMValueRef da01_dx20_ooa = LLVMBuildFMul(b, da01, dx20_ooa, "da01_dx20_ooa");
376 LLVMValueRef da20_dx01_ooa = LLVMBuildFMul(b, da20, dx01_ooa, "da20_dx01_ooa");
377 LLVMValueRef dady = LLVMBuildFSub(b, da20_dx01_ooa, da01_dx20_ooa, "dady");
378
379 /* Calculate a0 - the attribute value at the origin
380 */
381 LLVMValueRef dadx_x0 = LLVMBuildFMul(b, dadx, x0_center, "dadx_x0");
382 LLVMValueRef dady_y0 = LLVMBuildFMul(b, dady, y0_center, "dady_y0");
383 LLVMValueRef attr_v0 = LLVMBuildFAdd(b, dadx_x0, dady_y0, "attr_v0");
384 LLVMValueRef attr_0 = LLVMBuildFSub(b, a0, attr_v0, "attr_0");
385
386 store_coef(b, args, slot, attr_0, dadx, dady);
387 }
388
389
390 static void
391 emit_linear_coef( LLVMBuilderRef b,
392 struct lp_setup_args *args,
393 unsigned slot)
394 {
395 /* nothing to do anymore */
396 emit_coef4(b,
397 args, slot,
398 args->v0a,
399 args->v1a,
400 args->v2a);
401 }
402
403
404 /**
405 * Compute a0, dadx and dady for a perspective-corrected interpolant,
406 * for a triangle.
407 * We basically multiply the vertex value by 1/w before computing
408 * the plane coefficients (a0, dadx, dady).
409 * Later, when we compute the value at a particular fragment position we'll
410 * divide the interpolated value by the interpolated W at that fragment.
411 */
412 static void
413 emit_perspective_coef( LLVMBuilderRef b,
414 struct lp_setup_args *args,
415 unsigned slot)
416 {
417 /* premultiply by 1/w (v[0][3] is always 1/w):
418 */
419 LLVMValueRef v0_oow = vec4f_from_scalar(b, vert_attrib(b, args->v0, 0, 3, ""), "v0_oow");
420 LLVMValueRef v1_oow = vec4f_from_scalar(b, vert_attrib(b, args->v1, 0, 3, ""), "v1_oow");
421 LLVMValueRef v2_oow = vec4f_from_scalar(b, vert_attrib(b, args->v2, 0, 3, ""), "v2_oow");
422
423 LLVMValueRef v0_oow_v0a = LLVMBuildFMul(b, args->v0a, v0_oow, "v0_oow_v0a");
424 LLVMValueRef v1_oow_v1a = LLVMBuildFMul(b, args->v1a, v1_oow, "v1_oow_v1a");
425 LLVMValueRef v2_oow_v2a = LLVMBuildFMul(b, args->v2a, v2_oow, "v2_oow_v2a");
426
427 emit_coef4(b, args, slot, v0_oow_v0a, v1_oow_v1a, v2_oow_v2a);
428 }
429
430
431 static void
432 emit_position_coef( LLVMBuilderRef builder,
433 struct lp_setup_args *args,
434 int slot )
435 {
436 emit_linear_coef(builder, args, slot);
437 }
438
439
440
441
442 /**
443 * Compute the inputs-> dadx, dady, a0 values.
444 */
445 static void
446 emit_tri_coef( LLVMBuilderRef builder,
447 const struct lp_setup_variant_key *key,
448 struct lp_setup_args *args )
449 {
450 unsigned slot;
451
452 /* The internal position input is in slot zero:
453 */
454 load_attribute(builder, args, key, 0);
455 emit_position_coef(builder, args, 0);
456
457 /* setup interpolation for all the remaining attributes:
458 */
459 for (slot = 0; slot < key->num_inputs; slot++) {
460
461 if (key->inputs[slot].interp == LP_INTERP_CONSTANT ||
462 key->inputs[slot].interp == LP_INTERP_LINEAR ||
463 key->inputs[slot].interp == LP_INTERP_PERSPECTIVE)
464 load_attribute(builder, args, key, key->inputs[slot].src_index);
465
466 switch (key->inputs[slot].interp) {
467 case LP_INTERP_CONSTANT:
468 if (key->flatshade_first) {
469 emit_constant_coef4(builder, args, slot+1, args->v0a);
470 }
471 else {
472 emit_constant_coef4(builder, args, slot+1, args->v2a);
473 }
474 break;
475
476 case LP_INTERP_LINEAR:
477 emit_linear_coef(builder, args, slot+1);
478 break;
479
480 case LP_INTERP_PERSPECTIVE:
481 emit_perspective_coef(builder, args, slot+1);
482 break;
483
484 case LP_INTERP_POSITION:
485 /*
486 * The generated pixel interpolators will pick up the coeffs from
487 * slot 0.
488 */
489 break;
490
491 case LP_INTERP_FACING:
492 emit_facing_coef(builder, args, slot+1);
493 break;
494
495 default:
496 assert(0);
497 }
498 }
499 }
500
501
502 /* XXX: This is generic code, share with fs/vs codegen:
503 */
504 static lp_jit_setup_triangle
505 finalize_function(struct llvmpipe_screen *screen,
506 LLVMBuilderRef builder,
507 LLVMValueRef function)
508 {
509 void *f;
510
511 /* Verify the LLVM IR. If invalid, dump and abort */
512 #ifdef DEBUG
513 if (LLVMVerifyFunction(function, LLVMPrintMessageAction)) {
514 if (1)
515 lp_debug_dump_value(function);
516 abort();
517 }
518 #endif
519
520 /* Apply optimizations to LLVM IR */
521 LLVMRunFunctionPassManager(screen->pass, function);
522
523 if (gallivm_debug & GALLIVM_DEBUG_IR)
524 {
525 /* Print the LLVM IR to stderr */
526 lp_debug_dump_value(function);
527 debug_printf("\n");
528 }
529
530 /*
531 * Translate the LLVM IR into machine code.
532 */
533 f = LLVMGetPointerToGlobal(screen->engine, function);
534
535 if (gallivm_debug & GALLIVM_DEBUG_ASM)
536 {
537 lp_disassemble(f);
538 }
539
540 lp_func_delete_body(function);
541
542 return f;
543 }
544
545 /* XXX: Generic code:
546 */
547 static void
548 lp_emit_emms(LLVMBuilderRef builder)
549 {
550 #ifdef PIPE_ARCH_X86
551 /* Avoid corrupting the FPU stack on 32bit OSes. */
552 lp_build_intrinsic(builder, "llvm.x86.mmx.emms", LLVMVoidType(), NULL, 0);
553 #endif
554 }
555
556
557 /* XXX: generic code:
558 */
559 static void
560 set_noalias(LLVMBuilderRef builder,
561 LLVMValueRef function,
562 const LLVMTypeRef *arg_types,
563 int nr_args)
564 {
565 int i;
566 for(i = 0; i < Elements(arg_types); ++i)
567 if(LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind)
568 LLVMAddAttribute(LLVMGetParam(function, i),
569 LLVMNoAliasAttribute);
570 }
571
572 static void
573 init_args(LLVMBuilderRef b,
574 struct lp_setup_args *args,
575 const struct lp_setup_variant *variant)
576 {
577 LLVMValueRef v0_x = vert_attrib(b, args->v0, 0, 0, "v0_x");
578 LLVMValueRef v0_y = vert_attrib(b, args->v0, 0, 1, "v0_y");
579
580 LLVMValueRef v1_x = vert_attrib(b, args->v1, 0, 0, "v1_x");
581 LLVMValueRef v1_y = vert_attrib(b, args->v1, 0, 1, "v1_y");
582
583 LLVMValueRef v2_x = vert_attrib(b, args->v2, 0, 0, "v2_x");
584 LLVMValueRef v2_y = vert_attrib(b, args->v2, 0, 1, "v2_y");
585
586 LLVMValueRef pixel_center = LLVMConstReal(LLVMFloatType(),
587 variant->key.pixel_center_half ? 0.5 : 0);
588
589 LLVMValueRef x0_center = LLVMBuildFSub(b, v0_x, pixel_center, "x0_center" );
590 LLVMValueRef y0_center = LLVMBuildFSub(b, v0_y, pixel_center, "y0_center" );
591
592 LLVMValueRef dx01 = LLVMBuildFSub(b, v0_x, v1_x, "dx01");
593 LLVMValueRef dy01 = LLVMBuildFSub(b, v0_y, v1_y, "dy01");
594 LLVMValueRef dx20 = LLVMBuildFSub(b, v2_x, v0_x, "dx20");
595 LLVMValueRef dy20 = LLVMBuildFSub(b, v2_y, v0_y, "dy20");
596
597 LLVMValueRef one = LLVMConstReal(LLVMFloatType(), 1.0);
598 LLVMValueRef e = LLVMBuildFMul(b, dx01, dy20, "e");
599 LLVMValueRef f = LLVMBuildFMul(b, dx20, dy01, "f");
600 LLVMValueRef ooa = LLVMBuildFDiv(b, one, LLVMBuildFSub(b, e, f, ""), "ooa");
601
602 LLVMValueRef dy20_ooa = LLVMBuildFMul(b, dy20, ooa, "dy20_ooa");
603 LLVMValueRef dy01_ooa = LLVMBuildFMul(b, dy01, ooa, "dy01_ooa");
604 LLVMValueRef dx20_ooa = LLVMBuildFMul(b, dx20, ooa, "dx20_ooa");
605 LLVMValueRef dx01_ooa = LLVMBuildFMul(b, dx01, ooa, "dx01_ooa");
606
607 args->dy20_ooa = vec4f_from_scalar(b, dy20_ooa, "dy20_ooa_4f");
608 args->dy01_ooa = vec4f_from_scalar(b, dy01_ooa, "dy01_ooa_4f");
609
610 args->dx20_ooa = vec4f_from_scalar(b, dx20_ooa, "dx20_ooa_4f");
611 args->dx01_ooa = vec4f_from_scalar(b, dx01_ooa, "dx01_ooa_4f");
612
613 args->x0_center = vec4f_from_scalar(b, x0_center, "x0_center_4f");
614 args->y0_center = vec4f_from_scalar(b, y0_center, "y0_center_4f");
615 }
616
617 /**
618 * Generate the runtime callable function for the coefficient calculation.
619 *
620 */
621 static struct lp_setup_variant *
622 generate_setup_variant(struct llvmpipe_screen *screen,
623 struct lp_setup_variant_key *key,
624 struct llvmpipe_context *lp)
625 {
626 struct lp_setup_variant *variant = NULL;
627 struct lp_setup_args args;
628 char func_name[256];
629 LLVMTypeRef vec4f_type;
630 LLVMTypeRef func_type;
631 LLVMTypeRef arg_types[7];
632 LLVMBasicBlockRef block;
633 LLVMBuilderRef builder;
634 int64_t t0, t1;
635
636 if (0)
637 goto fail;
638
639 variant = CALLOC_STRUCT(lp_setup_variant);
640 if (variant == NULL)
641 goto fail;
642
643 if (LP_DEBUG & DEBUG_COUNTERS) {
644 t0 = os_time_get();
645 }
646
647 memcpy(&variant->key, key, key->size);
648 variant->list_item_global.base = variant;
649
650 util_snprintf(func_name, sizeof(func_name), "fs%u_setup%u",
651 0,
652 variant->no);
653
654 /* Currently always deal with full 4-wide vertex attributes from
655 * the vertices.
656 */
657
658 vec4f_type = LLVMVectorType(LLVMFloatType(), 4);
659
660 arg_types[0] = LLVMPointerType(vec4f_type, 0); /* v0 */
661 arg_types[1] = LLVMPointerType(vec4f_type, 0); /* v1 */
662 arg_types[2] = LLVMPointerType(vec4f_type, 0); /* v2 */
663 arg_types[3] = LLVMInt32Type(); /* facing */
664 arg_types[4] = LLVMPointerType(vec4f_type, 0); /* a0, aligned */
665 arg_types[5] = LLVMPointerType(vec4f_type, 0); /* dadx, aligned */
666 arg_types[6] = LLVMPointerType(vec4f_type, 0); /* dady, aligned */
667
668 func_type = LLVMFunctionType(LLVMVoidType(), arg_types, Elements(arg_types), 0);
669
670 variant->function = LLVMAddFunction(screen->module, func_name, func_type);
671 if (!variant->function)
672 goto fail;
673
674 LLVMSetFunctionCallConv(variant->function, LLVMCCallConv);
675
676 args.v0 = LLVMGetParam(variant->function, 0);
677 args.v1 = LLVMGetParam(variant->function, 1);
678 args.v2 = LLVMGetParam(variant->function, 2);
679 args.facing = LLVMGetParam(variant->function, 3);
680 args.a0 = LLVMGetParam(variant->function, 4);
681 args.dadx = LLVMGetParam(variant->function, 5);
682 args.dady = LLVMGetParam(variant->function, 6);
683
684 lp_build_name(args.v0, "in_v0");
685 lp_build_name(args.v1, "in_v1");
686 lp_build_name(args.v2, "in_v2");
687 lp_build_name(args.facing, "in_facing");
688 lp_build_name(args.a0, "out_a0");
689 lp_build_name(args.dadx, "out_dadx");
690 lp_build_name(args.dady, "out_dady");
691
692 /*
693 * Function body
694 */
695 block = LLVMAppendBasicBlock(variant->function, "entry");
696 builder = LLVMCreateBuilder();
697 LLVMPositionBuilderAtEnd(builder, block);
698
699 set_noalias(builder, variant->function, arg_types, Elements(arg_types));
700 init_args(builder, &args, variant);
701 emit_tri_coef(builder, &variant->key, &args);
702
703 lp_emit_emms(builder);
704 LLVMBuildRetVoid(builder);
705 LLVMDisposeBuilder(builder);
706
707 variant->jit_function = finalize_function(screen, builder,
708 variant->function);
709 if (!variant->jit_function)
710 goto fail;
711
712 /*
713 * Update timing information:
714 */
715 if (LP_DEBUG & DEBUG_COUNTERS) {
716 t1 = os_time_get();
717 LP_COUNT_ADD(llvm_compile_time, t1 - t0);
718 LP_COUNT_ADD(nr_llvm_compiles, 1);
719 }
720
721 return variant;
722
723 fail:
724 if (variant) {
725 if (variant->function) {
726 if (variant->jit_function)
727 LLVMFreeMachineCodeForFunction(screen->engine,
728 variant->function);
729 LLVMDeleteFunction(variant->function);
730 }
731 FREE(variant);
732 }
733
734 return NULL;
735 }
736
737
738
739 static void
740 lp_make_setup_variant_key(struct llvmpipe_context *lp,
741 struct lp_setup_variant_key *key)
742 {
743 struct lp_fragment_shader *fs = lp->fs;
744 unsigned i;
745
746 assert(sizeof key->inputs[0] == sizeof(ushort));
747
748 key->num_inputs = fs->info.base.num_inputs;
749 key->flatshade_first = lp->rasterizer->flatshade_first;
750 key->pixel_center_half = lp->rasterizer->gl_rasterization_rules;
751 key->twoside = lp->rasterizer->light_twoside;
752 key->size = Offset(struct lp_setup_variant_key,
753 inputs[key->num_inputs]);
754 key->color_slot = lp->color_slot[0];
755 key->bcolor_slot = lp->bcolor_slot[0];
756 key->spec_slot = lp->color_slot[1];
757 key->bspec_slot = lp->bcolor_slot[1];
758 key->units = (float) (lp->rasterizer->offset_units * lp->mrd);
759 key->scale = lp->rasterizer->offset_scale;
760 key->pad = 0;
761 memcpy(key->inputs, fs->inputs, key->num_inputs * sizeof key->inputs[0]);
762 for (i = 0; i < key->num_inputs; i++) {
763 if (key->inputs[i].interp == LP_INTERP_COLOR) {
764 if (lp->rasterizer->flatshade)
765 key->inputs[i].interp = LP_INTERP_CONSTANT;
766 else
767 key->inputs[i].interp = LP_INTERP_LINEAR;
768 }
769 }
770
771 }
772
773
774 static void
775 remove_setup_variant(struct llvmpipe_context *lp,
776 struct lp_setup_variant *variant)
777 {
778 struct llvmpipe_screen *screen = llvmpipe_screen(lp->pipe.screen);
779
780 if (gallivm_debug & GALLIVM_DEBUG_IR) {
781 debug_printf("llvmpipe: del setup_variant #%u total %u\n",
782 variant->no, lp->nr_setup_variants);
783 }
784
785 if (variant->function) {
786 if (variant->jit_function)
787 LLVMFreeMachineCodeForFunction(screen->engine,
788 variant->function);
789 LLVMDeleteFunction(variant->function);
790 }
791
792 remove_from_list(&variant->list_item_global);
793 lp->nr_setup_variants--;
794 FREE(variant);
795 }
796
797
798
799 /* When the number of setup variants exceeds a threshold, cull a
800 * fraction (currently a quarter) of them.
801 */
802 static void
803 cull_setup_variants(struct llvmpipe_context *lp)
804 {
805 struct pipe_context *pipe = &lp->pipe;
806 int i;
807
808 /*
809 * XXX: we need to flush the context until we have some sort of reference
810 * counting in fragment shaders as they may still be binned
811 * Flushing alone might not be sufficient we need to wait on it too.
812 */
813 llvmpipe_finish(pipe, __FUNCTION__);
814
815 for (i = 0; i < LP_MAX_SETUP_VARIANTS / 4; i++) {
816 struct lp_setup_variant_list_item *item = last_elem(&lp->setup_variants_list);
817 remove_setup_variant(lp, item->base);
818 }
819 }
820
821
822 /**
823 * Update fragment/vertex shader linkage state. This is called just
824 * prior to drawing something when some fragment-related state has
825 * changed.
826 */
827 void
828 llvmpipe_update_setup(struct llvmpipe_context *lp)
829 {
830 struct llvmpipe_screen *screen = llvmpipe_screen(lp->pipe.screen);
831
832 struct lp_setup_variant_key *key = &lp->setup_variant.key;
833 struct lp_setup_variant *variant = NULL;
834 struct lp_setup_variant_list_item *li;
835
836 lp_make_setup_variant_key(lp, key);
837
838 foreach(li, &lp->setup_variants_list) {
839 if(li->base->key.size == key->size &&
840 memcmp(&li->base->key, key, key->size) == 0) {
841 variant = li->base;
842 break;
843 }
844 }
845
846 if (variant) {
847 move_to_head(&lp->setup_variants_list, &variant->list_item_global);
848 }
849 else {
850 if (lp->nr_setup_variants >= LP_MAX_SETUP_VARIANTS) {
851 cull_setup_variants(lp);
852 }
853
854 variant = generate_setup_variant(screen, key, lp);
855 insert_at_head(&lp->setup_variants_list, &variant->list_item_global);
856 lp->nr_setup_variants++;
857 }
858
859 lp_setup_set_setup_variant(lp->setup,
860 variant);
861 }
862
863 void
864 lp_delete_setup_variants(struct llvmpipe_context *lp)
865 {
866 struct lp_setup_variant_list_item *li;
867 li = first_elem(&lp->setup_variants_list);
868 while(!at_end(&lp->setup_variants_list, li)) {
869 struct lp_setup_variant_list_item *next = next_elem(li);
870 remove_setup_variant(lp, li->base);
871 li = next;
872 }
873 }
874
875 void
876 lp_dump_setup_coef( const struct lp_setup_variant_key *key,
877 const float (*sa0)[4],
878 const float (*sdadx)[4],
879 const float (*sdady)[4])
880 {
881 int i, slot;
882
883 for (i = 0; i < NUM_CHANNELS; i++) {
884 float a0 = sa0 [0][i];
885 float dadx = sdadx[0][i];
886 float dady = sdady[0][i];
887
888 debug_printf("POS.%c: a0 = %f, dadx = %f, dady = %f\n",
889 "xyzw"[i],
890 a0, dadx, dady);
891 }
892
893 for (slot = 0; slot < key->num_inputs; slot++) {
894 unsigned usage_mask = key->inputs[slot].usage_mask;
895 for (i = 0; i < NUM_CHANNELS; i++) {
896 if (usage_mask & (1 << i)) {
897 float a0 = sa0 [1 + slot][i];
898 float dadx = sdadx[1 + slot][i];
899 float dady = sdady[1 + slot][i];
900
901 debug_printf("IN[%u].%c: a0 = %f, dadx = %f, dady = %f\n",
902 slot,
903 "xyzw"[i],
904 a0, dadx, dady);
905 }
906 }
907 }
908 }