a9d17536bbf324631ded9a3421ee1632a428a916
[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_bitarit.h"
35 #include "gallivm/lp_bld_const.h"
36 #include "gallivm/lp_bld_debug.h"
37 #include "gallivm/lp_bld_init.h"
38 #include "gallivm/lp_bld_logic.h"
39 #include "gallivm/lp_bld_intr.h"
40 #include "gallivm/lp_bld_flow.h"
41 #include "gallivm/lp_bld_type.h"
42
43 #include "lp_perf.h"
44 #include "lp_debug.h"
45 #include "lp_flush.h"
46 #include "lp_screen.h"
47 #include "lp_context.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 struct lp_build_context bld;
81 };
82
83
84 static void
85 store_coef(struct gallivm_state *gallivm,
86 struct lp_setup_args *args,
87 unsigned slot,
88 LLVMValueRef a0,
89 LLVMValueRef dadx,
90 LLVMValueRef dady)
91 {
92 LLVMBuilderRef builder = gallivm->builder;
93 LLVMValueRef idx = lp_build_const_int32(gallivm, slot);
94
95 LLVMBuildStore(builder,
96 a0,
97 LLVMBuildGEP(builder, args->a0, &idx, 1, ""));
98
99 LLVMBuildStore(builder,
100 dadx,
101 LLVMBuildGEP(builder, args->dadx, &idx, 1, ""));
102
103 LLVMBuildStore(builder,
104 dady,
105 LLVMBuildGEP(builder, args->dady, &idx, 1, ""));
106 }
107
108
109
110 static void
111 emit_constant_coef4(struct gallivm_state *gallivm,
112 struct lp_setup_args *args,
113 unsigned slot,
114 LLVMValueRef vert)
115 {
116 store_coef(gallivm, args, slot, vert, args->bld.zero, args->bld.zero);
117 }
118
119
120
121 /**
122 * Setup the fragment input attribute with the front-facing value.
123 * \param frontface is the triangle front facing?
124 */
125 static void
126 emit_facing_coef(struct gallivm_state *gallivm,
127 struct lp_setup_args *args,
128 unsigned slot )
129 {
130 LLVMBuilderRef builder = gallivm->builder;
131 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context);
132 LLVMValueRef a0_0 = args->facing;
133 LLVMValueRef a0_0f = LLVMBuildSIToFP(builder, a0_0, float_type, "");
134 const unsigned char swizzles[4] = { PIPE_SWIZZLE_RED, PIPE_SWIZZLE_ZERO,
135 PIPE_SWIZZLE_ZERO, PIPE_SWIZZLE_ZERO };
136 /* Our face val is either 1 or 0 so we do
137 * face = (val * 2) - 1
138 * to make it 1 or -1
139 */
140 LLVMValueRef face_val =
141 LLVMBuildFAdd(builder,
142 LLVMBuildFMul(builder, a0_0f,
143 lp_build_const_float(gallivm, 2.0),
144 ""),
145 lp_build_const_float(gallivm, -1.0),
146 "facing");
147 LLVMValueRef a0 = lp_build_swizzle_aos(&args->bld, face_val, swizzles);
148
149 store_coef(gallivm, args, slot, a0, args->bld.zero, args->bld.zero);
150 }
151
152
153 static LLVMValueRef
154 vert_attrib(struct gallivm_state *gallivm,
155 LLVMValueRef vert,
156 int attr,
157 int elem,
158 const char *name)
159 {
160 LLVMBuilderRef b = gallivm->builder;
161 LLVMValueRef idx[2];
162 idx[0] = lp_build_const_int32(gallivm, attr);
163 idx[1] = lp_build_const_int32(gallivm, elem);
164 return LLVMBuildLoad(b, LLVMBuildGEP(b, vert, idx, 2, ""), name);
165 }
166
167
168 static void
169 lp_twoside(struct gallivm_state *gallivm,
170 struct lp_setup_args *args,
171 const struct lp_setup_variant_key *key,
172 int bcolor_slot,
173 LLVMValueRef attribv[3])
174 {
175 LLVMBuilderRef b = gallivm->builder;
176 LLVMValueRef a0_back, a1_back, a2_back;
177 LLVMValueRef idx2 = lp_build_const_int32(gallivm, bcolor_slot);
178
179 LLVMValueRef facing = args->facing;
180 LLVMValueRef front_facing = LLVMBuildICmp(b, LLVMIntEQ, facing,
181 lp_build_const_int32(gallivm, 0), ""); /** need i1 for if condition */
182
183 a0_back = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v0, &idx2, 1, ""), "v0a_back");
184 a1_back = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v1, &idx2, 1, ""), "v1a_back");
185 a2_back = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v2, &idx2, 1, ""), "v2a_back");
186
187 /* Possibly swap the front and back attrib values,
188 *
189 * Prefer select to if so we don't have to worry about phis or
190 * allocas.
191 */
192 attribv[0] = LLVMBuildSelect(b, front_facing, a0_back, attribv[0], "");
193 attribv[1] = LLVMBuildSelect(b, front_facing, a1_back, attribv[1], "");
194 attribv[2] = LLVMBuildSelect(b, front_facing, a2_back, attribv[2], "");
195
196 }
197
198 static void
199 lp_do_offset_tri(struct gallivm_state *gallivm,
200 struct lp_setup_args *args,
201 const struct lp_setup_variant_key *key,
202 LLVMValueRef inv_det,
203 LLVMValueRef dxyz01,
204 LLVMValueRef dxyz20,
205 LLVMValueRef attribv[3])
206 {
207 LLVMBuilderRef b = gallivm->builder;
208 struct lp_build_context flt_scalar_bld;
209 struct lp_build_context int_scalar_bld;
210 struct lp_build_context *bld = &args->bld;
211 LLVMValueRef zoffset, mult;
212 LLVMValueRef z0_new, z1_new, z2_new;
213 LLVMValueRef dzdxdzdy, dzdx, dzdy, dzxyz20, dyzzx01, dyzzx01_dzxyz20, dzx01_dyz20;
214 LLVMValueRef z0z1, z0z1z2;
215 LLVMValueRef max, max_value, res12;
216 LLVMValueRef shuffles[4];
217 LLVMTypeRef shuf_type = LLVMInt32TypeInContext(gallivm->context);
218 LLVMValueRef onei = lp_build_const_int32(gallivm, 1);
219 LLVMValueRef zeroi = lp_build_const_int32(gallivm, 0);
220 LLVMValueRef twoi = lp_build_const_int32(gallivm, 2);
221 LLVMValueRef threei = lp_build_const_int32(gallivm, 3);
222
223 /* (res12) = cross(e,f).xy */
224 shuffles[0] = twoi;
225 shuffles[1] = zeroi;
226 shuffles[2] = onei;
227 shuffles[3] = twoi;
228 dzxyz20 = LLVMBuildShuffleVector(b, dxyz20, dxyz20, LLVMConstVector(shuffles, 4), "");
229
230 shuffles[0] = onei;
231 shuffles[1] = twoi;
232 shuffles[2] = twoi;
233 shuffles[3] = zeroi;
234 dyzzx01 = LLVMBuildShuffleVector(b, dxyz01, dxyz01, LLVMConstVector(shuffles, 4), "");
235
236 dyzzx01_dzxyz20 = LLVMBuildFMul(b, dzxyz20, dyzzx01, "dyzzx01_dzxyz20");
237
238 shuffles[0] = twoi;
239 shuffles[1] = threei;
240 shuffles[2] = LLVMGetUndef(shuf_type);
241 shuffles[3] = LLVMGetUndef(shuf_type);
242 dzx01_dyz20 = LLVMBuildShuffleVector(b, dyzzx01_dzxyz20, dyzzx01_dzxyz20,
243 LLVMConstVector(shuffles, 4), "");
244
245 res12 = LLVMBuildFSub(b, dyzzx01_dzxyz20, dzx01_dyz20, "res12");
246
247 /* dzdx = fabsf(res1 * inv_det), dydx = fabsf(res2 * inv_det)*/
248 dzdxdzdy = LLVMBuildFMul(b, res12, inv_det, "dzdxdzdy");
249 dzdxdzdy = lp_build_abs(bld, dzdxdzdy);
250
251 dzdx = LLVMBuildExtractElement(b, dzdxdzdy, zeroi, "");
252 dzdy = LLVMBuildExtractElement(b, dzdxdzdy, onei, "");
253
254 /* mult = MAX2(dzdx, dzdy) * pgon_offset_scale */
255 max = LLVMBuildFCmp(b, LLVMRealUGT, dzdx, dzdy, "");
256 max_value = LLVMBuildSelect(b, max, dzdx, dzdy, "max");
257
258 mult = LLVMBuildFMul(b, max_value,
259 lp_build_const_float(gallivm, key->pgon_offset_scale), "");
260
261 lp_build_context_init(&flt_scalar_bld, gallivm, lp_type_float_vec(32, 32));
262
263 if (key->floating_point_depth) {
264 /*
265 * bias = pgon_offset_units * 2^(exponent(max(z0, z1, z2)) - mantissa_bits) +
266 * MAX2(dzdx, dzdy) * pgon_offset_scale
267 *
268 * NOTE: Assumes IEEE float32.
269 */
270 LLVMValueRef c23_shifted, exp_mask, bias, exp;
271 LLVMValueRef maxz_value, maxz0z1_value;
272
273 lp_build_context_init(&int_scalar_bld, gallivm, lp_type_int_vec(32, 32));
274
275 c23_shifted = lp_build_const_int32(gallivm, 23 << 23);
276 exp_mask = lp_build_const_int32(gallivm, 0xff << 23);
277
278 maxz0z1_value = lp_build_max(&flt_scalar_bld,
279 LLVMBuildExtractElement(b, attribv[0], twoi, ""),
280 LLVMBuildExtractElement(b, attribv[1], twoi, ""));
281
282 maxz_value = lp_build_max(&flt_scalar_bld,
283 LLVMBuildExtractElement(b, attribv[2], twoi, ""),
284 maxz0z1_value);
285
286 exp = LLVMBuildBitCast(b, maxz_value, int_scalar_bld.vec_type, "");
287 exp = lp_build_and(&int_scalar_bld, exp, exp_mask);
288 exp = lp_build_sub(&int_scalar_bld, exp, c23_shifted);
289 /* Clamping to zero means mrd will be zero for very small numbers,
290 * but specs do not indicate this should be prevented by clamping
291 * mrd to smallest normal number instead. */
292 exp = lp_build_max(&int_scalar_bld, exp, int_scalar_bld.zero);
293 exp = LLVMBuildBitCast(b, exp, flt_scalar_bld.vec_type, "");
294
295 bias = LLVMBuildFMul(b, exp,
296 lp_build_const_float(gallivm, key->pgon_offset_units),
297 "bias");
298
299 zoffset = LLVMBuildFAdd(b, bias, mult, "zoffset");
300 } else {
301 /*
302 * bias = pgon_offset_units + MAX2(dzdx, dzdy) * pgon_offset_scale
303 */
304 zoffset = LLVMBuildFAdd(b,
305 lp_build_const_float(gallivm, key->pgon_offset_units),
306 mult, "zoffset");
307 }
308
309 if (key->pgon_offset_clamp > 0) {
310 zoffset = lp_build_min(&flt_scalar_bld,
311 lp_build_const_float(gallivm, key->pgon_offset_clamp),
312 zoffset);
313 }
314 else if (key->pgon_offset_clamp < 0) {
315 zoffset = lp_build_max(&flt_scalar_bld,
316 lp_build_const_float(gallivm, key->pgon_offset_clamp),
317 zoffset);
318 }
319
320 /* yuck */
321 shuffles[0] = twoi;
322 shuffles[1] = lp_build_const_int32(gallivm, 6);
323 shuffles[2] = LLVMGetUndef(shuf_type);
324 shuffles[3] = LLVMGetUndef(shuf_type);
325 z0z1 = LLVMBuildShuffleVector(b, attribv[0], attribv[1], LLVMConstVector(shuffles, 4), "");
326 shuffles[0] = zeroi;
327 shuffles[1] = onei;
328 shuffles[2] = lp_build_const_int32(gallivm, 6);
329 shuffles[3] = LLVMGetUndef(shuf_type);
330 z0z1z2 = LLVMBuildShuffleVector(b, z0z1, attribv[2], LLVMConstVector(shuffles, 4), "");
331 zoffset = lp_build_broadcast_scalar(bld, zoffset);
332
333 /* clamp and do offset */
334 /*
335 * FIXME I suspect the clamp (is that even right to always clamp to fixed
336 * 0.0/1.0?) should really be per fragment?
337 */
338 z0z1z2 = lp_build_clamp(bld, LLVMBuildFAdd(b, z0z1z2, zoffset, ""), bld->zero, bld->one);
339
340 /* insert into args->a0.z, a1.z, a2.z:
341 */
342 z0_new = LLVMBuildExtractElement(b, z0z1z2, zeroi, "");
343 z1_new = LLVMBuildExtractElement(b, z0z1z2, onei, "");
344 z2_new = LLVMBuildExtractElement(b, z0z1z2, twoi, "");
345 attribv[0] = LLVMBuildInsertElement(b, attribv[0], z0_new, twoi, "");
346 attribv[1] = LLVMBuildInsertElement(b, attribv[1], z1_new, twoi, "");
347 attribv[2] = LLVMBuildInsertElement(b, attribv[2], z2_new, twoi, "");
348 }
349
350 static void
351 load_attribute(struct gallivm_state *gallivm,
352 struct lp_setup_args *args,
353 const struct lp_setup_variant_key *key,
354 unsigned vert_attr,
355 LLVMValueRef attribv[3])
356 {
357 LLVMBuilderRef b = gallivm->builder;
358 LLVMValueRef idx = lp_build_const_int32(gallivm, vert_attr);
359
360 /* Load the vertex data
361 */
362 attribv[0] = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v0, &idx, 1, ""), "v0a");
363 attribv[1] = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v1, &idx, 1, ""), "v1a");
364 attribv[2] = LLVMBuildLoad(b, LLVMBuildGEP(b, args->v2, &idx, 1, ""), "v2a");
365
366
367 /* Potentially modify it according to twoside, etc:
368 */
369 if (key->twoside) {
370 if (vert_attr == key->color_slot && key->bcolor_slot >= 0)
371 lp_twoside(gallivm, args, key, key->bcolor_slot, attribv);
372 else if (vert_attr == key->spec_slot && key->bspec_slot >= 0)
373 lp_twoside(gallivm, args, key, key->bspec_slot, attribv);
374 }
375 }
376
377 static void
378 emit_coef4( struct gallivm_state *gallivm,
379 struct lp_setup_args *args,
380 unsigned slot,
381 LLVMValueRef a0,
382 LLVMValueRef a1,
383 LLVMValueRef a2)
384 {
385 LLVMBuilderRef b = gallivm->builder;
386 LLVMValueRef dy20_ooa = args->dy20_ooa;
387 LLVMValueRef dy01_ooa = args->dy01_ooa;
388 LLVMValueRef dx20_ooa = args->dx20_ooa;
389 LLVMValueRef dx01_ooa = args->dx01_ooa;
390 LLVMValueRef x0_center = args->x0_center;
391 LLVMValueRef y0_center = args->y0_center;
392 LLVMValueRef da01 = LLVMBuildFSub(b, a0, a1, "da01");
393 LLVMValueRef da20 = LLVMBuildFSub(b, a2, a0, "da20");
394
395 /* Calculate dadx (vec4f)
396 */
397 LLVMValueRef da01_dy20_ooa = LLVMBuildFMul(b, da01, dy20_ooa, "da01_dy20_ooa");
398 LLVMValueRef da20_dy01_ooa = LLVMBuildFMul(b, da20, dy01_ooa, "da20_dy01_ooa");
399 LLVMValueRef dadx = LLVMBuildFSub(b, da01_dy20_ooa, da20_dy01_ooa, "dadx");
400
401 /* Calculate dady (vec4f)
402 */
403 LLVMValueRef da01_dx20_ooa = LLVMBuildFMul(b, da01, dx20_ooa, "da01_dx20_ooa");
404 LLVMValueRef da20_dx01_ooa = LLVMBuildFMul(b, da20, dx01_ooa, "da20_dx01_ooa");
405 LLVMValueRef dady = LLVMBuildFSub(b, da20_dx01_ooa, da01_dx20_ooa, "dady");
406
407 /* Calculate a0 - the attribute value at the origin
408 */
409 LLVMValueRef dadx_x0 = LLVMBuildFMul(b, dadx, x0_center, "dadx_x0");
410 LLVMValueRef dady_y0 = LLVMBuildFMul(b, dady, y0_center, "dady_y0");
411 LLVMValueRef attr_v0 = LLVMBuildFAdd(b, dadx_x0, dady_y0, "attr_v0");
412 LLVMValueRef attr_0 = LLVMBuildFSub(b, a0, attr_v0, "attr_0");
413
414 store_coef(gallivm, args, slot, attr_0, dadx, dady);
415 }
416
417
418 static void
419 emit_linear_coef( struct gallivm_state *gallivm,
420 struct lp_setup_args *args,
421 unsigned slot,
422 LLVMValueRef attribv[3])
423 {
424 /* nothing to do anymore */
425 emit_coef4(gallivm,
426 args, slot,
427 attribv[0],
428 attribv[1],
429 attribv[2]);
430 }
431
432
433 /**
434 * Compute a0, dadx and dady for a perspective-corrected interpolant,
435 * for a triangle.
436 * We basically multiply the vertex value by 1/w before computing
437 * the plane coefficients (a0, dadx, dady).
438 * Later, when we compute the value at a particular fragment position we'll
439 * divide the interpolated value by the interpolated W at that fragment.
440 */
441 static void
442 apply_perspective_corr( struct gallivm_state *gallivm,
443 struct lp_setup_args *args,
444 unsigned slot,
445 LLVMValueRef attribv[3])
446 {
447 LLVMBuilderRef b = gallivm->builder;
448
449 /* premultiply by 1/w (v[0][3] is always 1/w):
450 */
451 LLVMValueRef v0_oow = lp_build_broadcast_scalar(&args->bld,
452 vert_attrib(gallivm, args->v0, 0, 3, "v0_oow"));
453 LLVMValueRef v1_oow = lp_build_broadcast_scalar(&args->bld,
454 vert_attrib(gallivm, args->v1, 0, 3, "v1_oow"));
455 LLVMValueRef v2_oow = lp_build_broadcast_scalar(&args->bld,
456 vert_attrib(gallivm, args->v2, 0, 3, "v2_oow"));
457
458 attribv[0] = LLVMBuildFMul(b, attribv[0], v0_oow, "v0_oow_v0a");
459 attribv[1] = LLVMBuildFMul(b, attribv[1], v1_oow, "v1_oow_v1a");
460 attribv[2] = LLVMBuildFMul(b, attribv[2], v2_oow, "v2_oow_v2a");
461 }
462
463
464 /**
465 * Applys cylindrical wrapping to vertex attributes if enabled.
466 * Input coordinates must be in [0, 1] range, otherwise results are undefined.
467 *
468 * @param cyl_wrap TGSI_CYLINDRICAL_WRAP_x flags
469 */
470 static void
471 emit_apply_cyl_wrap(struct gallivm_state *gallivm,
472 struct lp_setup_args *args,
473 uint cyl_wrap,
474 LLVMValueRef attribv[3])
475
476 {
477 LLVMBuilderRef builder = gallivm->builder;
478 struct lp_type type = args->bld.type;
479 LLVMTypeRef float_vec_type = args->bld.vec_type;
480 LLVMValueRef pos_half;
481 LLVMValueRef neg_half;
482 LLVMValueRef cyl_mask;
483 LLVMValueRef offset;
484 LLVMValueRef delta;
485 LLVMValueRef one;
486
487 if (!cyl_wrap)
488 return;
489
490 /* Constants */
491 pos_half = lp_build_const_vec(gallivm, type, +0.5f);
492 neg_half = lp_build_const_vec(gallivm, type, -0.5f);
493 cyl_mask = lp_build_const_mask_aos(gallivm, type, cyl_wrap, 4);
494
495 one = lp_build_const_vec(gallivm, type, 1.0f);
496 one = LLVMBuildBitCast(builder, one, lp_build_int_vec_type(gallivm, type), "");
497 one = LLVMBuildAnd(builder, one, cyl_mask, "");
498
499 /* Edge v0 -> v1 */
500 delta = LLVMBuildFSub(builder, attribv[1], attribv[0], "");
501
502 offset = lp_build_compare(gallivm, type, PIPE_FUNC_GREATER, delta, pos_half);
503 offset = LLVMBuildAnd(builder, offset, one, "");
504 offset = LLVMBuildBitCast(builder, offset, float_vec_type, "");
505 attribv[0] = LLVMBuildFAdd(builder, attribv[0], offset, "");
506
507 offset = lp_build_compare(gallivm, type, PIPE_FUNC_LESS, delta, neg_half);
508 offset = LLVMBuildAnd(builder, offset, one, "");
509 offset = LLVMBuildBitCast(builder, offset, float_vec_type, "");
510 attribv[1] = LLVMBuildFAdd(builder, attribv[1], offset, "");
511
512 /* Edge v1 -> v2 */
513 delta = LLVMBuildFSub(builder, attribv[2], attribv[1], "");
514
515 offset = lp_build_compare(gallivm, type, PIPE_FUNC_GREATER, delta, pos_half);
516 offset = LLVMBuildAnd(builder, offset, one, "");
517 offset = LLVMBuildBitCast(builder, offset, float_vec_type, "");
518 attribv[1] = LLVMBuildFAdd(builder, attribv[1], offset, "");
519
520 offset = lp_build_compare(gallivm, type, PIPE_FUNC_LESS, delta, neg_half);
521 offset = LLVMBuildAnd(builder, offset, one, "");
522 offset = LLVMBuildBitCast(builder, offset, float_vec_type, "");
523 attribv[2] = LLVMBuildFAdd(builder, attribv[2], offset, "");
524
525 /* Edge v2 -> v0 */
526 delta = LLVMBuildFSub(builder, attribv[0], attribv[2], "");
527
528 offset = lp_build_compare(gallivm, type, PIPE_FUNC_GREATER, delta, pos_half);
529 offset = LLVMBuildAnd(builder, offset, one, "");
530 offset = LLVMBuildBitCast(builder, offset, float_vec_type, "");
531 attribv[2] = LLVMBuildFAdd(builder, attribv[2], offset, "");
532
533 offset = lp_build_compare(gallivm, type, PIPE_FUNC_LESS, delta, neg_half);
534 offset = LLVMBuildAnd(builder, offset, one, "");
535 offset = LLVMBuildBitCast(builder, offset, float_vec_type, "");
536 attribv[0] = LLVMBuildFAdd(builder, attribv[0], offset, "");
537 }
538
539
540 /**
541 * Compute the inputs-> dadx, dady, a0 values.
542 */
543 static void
544 emit_tri_coef( struct gallivm_state *gallivm,
545 const struct lp_setup_variant_key *key,
546 struct lp_setup_args *args)
547 {
548 unsigned slot;
549
550 LLVMValueRef attribs[3];
551
552 /* setup interpolation for all the remaining attributes:
553 */
554 for (slot = 0; slot < key->num_inputs; slot++) {
555 switch (key->inputs[slot].interp) {
556 case LP_INTERP_CONSTANT:
557 load_attribute(gallivm, args, key, key->inputs[slot].src_index, attribs);
558 if (key->flatshade_first) {
559 emit_constant_coef4(gallivm, args, slot+1, attribs[0]);
560 }
561 else {
562 emit_constant_coef4(gallivm, args, slot+1, attribs[2]);
563 }
564 break;
565
566 case LP_INTERP_LINEAR:
567 load_attribute(gallivm, args, key, key->inputs[slot].src_index, attribs);
568 emit_apply_cyl_wrap(gallivm, args, key->inputs[slot].cyl_wrap, attribs);
569 emit_linear_coef(gallivm, args, slot+1, attribs);
570 break;
571
572 case LP_INTERP_PERSPECTIVE:
573 load_attribute(gallivm, args, key, key->inputs[slot].src_index, attribs);
574 emit_apply_cyl_wrap(gallivm, args, key->inputs[slot].cyl_wrap, attribs);
575 apply_perspective_corr(gallivm, args, slot+1, attribs);
576 emit_linear_coef(gallivm, args, slot+1, attribs);
577 break;
578
579 case LP_INTERP_POSITION:
580 /*
581 * The generated pixel interpolators will pick up the coeffs from
582 * slot 0.
583 */
584 break;
585
586 case LP_INTERP_FACING:
587 emit_facing_coef(gallivm, args, slot+1);
588 break;
589
590 default:
591 assert(0);
592 }
593 }
594 }
595
596
597 /* XXX: generic code:
598 */
599 static void
600 set_noalias(LLVMBuilderRef builder,
601 LLVMValueRef function,
602 const LLVMTypeRef *arg_types,
603 int nr_args)
604 {
605 int i;
606 for(i = 0; i < nr_args; ++i)
607 if(LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind)
608 LLVMAddAttribute(LLVMGetParam(function, i),
609 LLVMNoAliasAttribute);
610 }
611
612 static void
613 init_args(struct gallivm_state *gallivm,
614 const struct lp_setup_variant_key *key,
615 struct lp_setup_args *args)
616 {
617 LLVMBuilderRef b = gallivm->builder;
618 LLVMTypeRef shuf_type = LLVMInt32TypeInContext(gallivm->context);
619 LLVMValueRef onef = lp_build_const_float(gallivm, 1.0);
620 LLVMValueRef onei = lp_build_const_int32(gallivm, 1);
621 LLVMValueRef zeroi = lp_build_const_int32(gallivm, 0);
622 LLVMValueRef pixel_center, xy0_center, dxy01, dxy20, dyx20;
623 LLVMValueRef e, f, ef, ooa;
624 LLVMValueRef shuffles[4];
625 LLVMValueRef attr_pos[3];
626 struct lp_type typef4 = lp_type_float_vec(32, 128);
627 struct lp_build_context bld;
628
629 lp_build_context_init(&bld, gallivm, typef4);
630 args->bld = bld;
631
632 /* The internal position input is in slot zero:
633 */
634 load_attribute(gallivm, args, key, 0, attr_pos);
635
636 pixel_center = lp_build_const_vec(gallivm, typef4,
637 key->pixel_center_half ? 0.5 : 0.0);
638
639 /*
640 * xy are first two elems in v0a/v1a/v2a but just use vec4 arit
641 * also offset_tri uses actually xyz in them
642 */
643 xy0_center = LLVMBuildFSub(b, attr_pos[0], pixel_center, "xy0_center" );
644
645 dxy01 = LLVMBuildFSub(b, attr_pos[0], attr_pos[1], "dxy01");
646 dxy20 = LLVMBuildFSub(b, attr_pos[2], attr_pos[0], "dxy20");
647
648 shuffles[0] = onei;
649 shuffles[1] = zeroi;
650 shuffles[2] = LLVMGetUndef(shuf_type);
651 shuffles[3] = LLVMGetUndef(shuf_type);
652
653 dyx20 = LLVMBuildShuffleVector(b, dxy20, dxy20, LLVMConstVector(shuffles, 4), "");
654
655 ef = LLVMBuildFMul(b, dxy01, dyx20, "ef");
656 e = LLVMBuildExtractElement(b, ef, zeroi, "");
657 f = LLVMBuildExtractElement(b, ef, onei, "");
658
659 ooa = LLVMBuildFDiv(b, onef, LLVMBuildFSub(b, e, f, ""), "ooa");
660
661 ooa = lp_build_broadcast_scalar(&bld, ooa);
662
663 /* tri offset calc shares a lot of arithmetic, do it here */
664 if (key->pgon_offset_scale != 0.0f || key->pgon_offset_units != 0.0f) {
665 lp_do_offset_tri(gallivm, args, key, ooa, dxy01, dxy20, attr_pos);
666 }
667
668 dxy20 = LLVMBuildFMul(b, dxy20, ooa, "");
669 dxy01 = LLVMBuildFMul(b, dxy01, ooa, "");
670
671 args->dy20_ooa = lp_build_extract_broadcast(gallivm, typef4, typef4, dxy20, onei);
672 args->dy01_ooa = lp_build_extract_broadcast(gallivm, typef4, typef4, dxy01, onei);
673
674 args->dx20_ooa = lp_build_extract_broadcast(gallivm, typef4, typef4, dxy20, zeroi);
675 args->dx01_ooa = lp_build_extract_broadcast(gallivm, typef4, typef4, dxy01, zeroi);
676
677 args->x0_center = lp_build_extract_broadcast(gallivm, typef4, typef4, xy0_center, zeroi);
678 args->y0_center = lp_build_extract_broadcast(gallivm, typef4, typef4, xy0_center, onei);
679
680 emit_linear_coef(gallivm, args, 0, attr_pos);
681 }
682
683 /**
684 * Generate the runtime callable function for the coefficient calculation.
685 *
686 */
687 static struct lp_setup_variant *
688 generate_setup_variant(struct lp_setup_variant_key *key,
689 struct llvmpipe_context *lp)
690 {
691 struct lp_setup_variant *variant = NULL;
692 struct gallivm_state *gallivm;
693 struct lp_setup_args args;
694 char func_name[256];
695 LLVMTypeRef vec4f_type;
696 LLVMTypeRef func_type;
697 LLVMTypeRef arg_types[7];
698 LLVMBasicBlockRef block;
699 LLVMBuilderRef builder;
700 int64_t t0 = 0, t1;
701
702 if (0)
703 goto fail;
704
705 variant = CALLOC_STRUCT(lp_setup_variant);
706 if (variant == NULL)
707 goto fail;
708
709 variant->gallivm = gallivm = gallivm_create();
710 if (!variant->gallivm) {
711 goto fail;
712 }
713
714 builder = gallivm->builder;
715
716 if (LP_DEBUG & DEBUG_COUNTERS) {
717 t0 = os_time_get();
718 }
719
720 memcpy(&variant->key, key, key->size);
721 variant->list_item_global.base = variant;
722
723 util_snprintf(func_name, sizeof(func_name), "fs%u_setup%u",
724 0, variant->no);
725
726 /* Currently always deal with full 4-wide vertex attributes from
727 * the vertices.
728 */
729
730 vec4f_type = LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), 4);
731
732 arg_types[0] = LLVMPointerType(vec4f_type, 0); /* v0 */
733 arg_types[1] = LLVMPointerType(vec4f_type, 0); /* v1 */
734 arg_types[2] = LLVMPointerType(vec4f_type, 0); /* v2 */
735 arg_types[3] = LLVMInt32TypeInContext(gallivm->context); /* facing */
736 arg_types[4] = LLVMPointerType(vec4f_type, 0); /* a0, aligned */
737 arg_types[5] = LLVMPointerType(vec4f_type, 0); /* dadx, aligned */
738 arg_types[6] = LLVMPointerType(vec4f_type, 0); /* dady, aligned */
739
740 func_type = LLVMFunctionType(LLVMVoidTypeInContext(gallivm->context),
741 arg_types, Elements(arg_types), 0);
742
743 variant->function = LLVMAddFunction(gallivm->module, func_name, func_type);
744 if (!variant->function)
745 goto fail;
746
747 LLVMSetFunctionCallConv(variant->function, LLVMCCallConv);
748
749 args.v0 = LLVMGetParam(variant->function, 0);
750 args.v1 = LLVMGetParam(variant->function, 1);
751 args.v2 = LLVMGetParam(variant->function, 2);
752 args.facing = LLVMGetParam(variant->function, 3);
753 args.a0 = LLVMGetParam(variant->function, 4);
754 args.dadx = LLVMGetParam(variant->function, 5);
755 args.dady = LLVMGetParam(variant->function, 6);
756
757 lp_build_name(args.v0, "in_v0");
758 lp_build_name(args.v1, "in_v1");
759 lp_build_name(args.v2, "in_v2");
760 lp_build_name(args.facing, "in_facing");
761 lp_build_name(args.a0, "out_a0");
762 lp_build_name(args.dadx, "out_dadx");
763 lp_build_name(args.dady, "out_dady");
764
765 /*
766 * Function body
767 */
768 block = LLVMAppendBasicBlockInContext(gallivm->context,
769 variant->function, "entry");
770 LLVMPositionBuilderAtEnd(builder, block);
771
772 set_noalias(builder, variant->function, arg_types, Elements(arg_types));
773 init_args(gallivm, &variant->key, &args);
774 emit_tri_coef(gallivm, &variant->key, &args);
775
776 LLVMBuildRetVoid(builder);
777
778 gallivm_verify_function(gallivm, variant->function);
779
780 gallivm_compile_module(gallivm);
781
782 variant->jit_function = (lp_jit_setup_triangle)
783 gallivm_jit_function(gallivm, variant->function);
784 if (!variant->jit_function)
785 goto fail;
786
787 /*
788 * Update timing information:
789 */
790 if (LP_DEBUG & DEBUG_COUNTERS) {
791 t1 = os_time_get();
792 LP_COUNT_ADD(llvm_compile_time, t1 - t0);
793 LP_COUNT_ADD(nr_llvm_compiles, 1);
794 }
795
796 return variant;
797
798 fail:
799 if (variant) {
800 if (variant->function) {
801 gallivm_free_function(gallivm,
802 variant->function,
803 variant->jit_function);
804 }
805 if (variant->gallivm) {
806 gallivm_destroy(variant->gallivm);
807 }
808 FREE(variant);
809 }
810
811 return NULL;
812 }
813
814
815
816 static void
817 lp_make_setup_variant_key(struct llvmpipe_context *lp,
818 struct lp_setup_variant_key *key)
819 {
820 struct lp_fragment_shader *fs = lp->fs;
821 unsigned i;
822
823 assert(sizeof key->inputs[0] == sizeof(uint));
824
825 key->num_inputs = fs->info.base.num_inputs;
826 key->flatshade_first = lp->rasterizer->flatshade_first;
827 key->pixel_center_half = lp->rasterizer->half_pixel_center;
828 key->twoside = lp->rasterizer->light_twoside;
829 key->size = Offset(struct lp_setup_variant_key,
830 inputs[key->num_inputs]);
831
832 key->color_slot = lp->color_slot [0];
833 key->bcolor_slot = lp->bcolor_slot[0];
834 key->spec_slot = lp->color_slot [1];
835 key->bspec_slot = lp->bcolor_slot[1];
836 assert(key->color_slot == lp->color_slot [0]);
837 assert(key->bcolor_slot == lp->bcolor_slot[0]);
838 assert(key->spec_slot == lp->color_slot [1]);
839 assert(key->bspec_slot == lp->bcolor_slot[1]);
840
841 /*
842 * If depth is floating point, depth bias is calculated with respect
843 * to the primitive's maximum Z value. Retain the original depth bias
844 * value until that stage.
845 */
846 key->floating_point_depth = lp->floating_point_depth;
847
848 if (key->floating_point_depth) {
849 key->pgon_offset_units = (float) lp->rasterizer->offset_units;
850 } else {
851 key->pgon_offset_units =
852 (float) (lp->rasterizer->offset_units * lp->mrd);
853 }
854
855 key->pgon_offset_scale = lp->rasterizer->offset_scale;
856 key->pgon_offset_clamp = lp->rasterizer->offset_clamp;
857 key->pad = 0;
858 memcpy(key->inputs, fs->inputs, key->num_inputs * sizeof key->inputs[0]);
859 for (i = 0; i < key->num_inputs; i++) {
860 if (key->inputs[i].interp == LP_INTERP_COLOR) {
861 if (lp->rasterizer->flatshade)
862 key->inputs[i].interp = LP_INTERP_CONSTANT;
863 else
864 key->inputs[i].interp = LP_INTERP_PERSPECTIVE;
865 }
866 }
867
868 }
869
870
871 static void
872 remove_setup_variant(struct llvmpipe_context *lp,
873 struct lp_setup_variant *variant)
874 {
875 if (gallivm_debug & GALLIVM_DEBUG_IR) {
876 debug_printf("llvmpipe: del setup_variant #%u total %u\n",
877 variant->no, lp->nr_setup_variants);
878 }
879
880 if (variant->function) {
881 gallivm_free_function(variant->gallivm,
882 variant->function,
883 variant->jit_function);
884 }
885
886 if (variant->gallivm) {
887 gallivm_destroy(variant->gallivm);
888 }
889
890 remove_from_list(&variant->list_item_global);
891 lp->nr_setup_variants--;
892 FREE(variant);
893 }
894
895
896
897 /* When the number of setup variants exceeds a threshold, cull a
898 * fraction (currently a quarter) of them.
899 */
900 static void
901 cull_setup_variants(struct llvmpipe_context *lp)
902 {
903 struct pipe_context *pipe = &lp->pipe;
904 int i;
905
906 /*
907 * XXX: we need to flush the context until we have some sort of reference
908 * counting in fragment shaders as they may still be binned
909 * Flushing alone might not be sufficient we need to wait on it too.
910 */
911 llvmpipe_finish(pipe, __FUNCTION__);
912
913 for (i = 0; i < LP_MAX_SETUP_VARIANTS / 4; i++) {
914 struct lp_setup_variant_list_item *item;
915 if (is_empty_list(&lp->setup_variants_list)) {
916 break;
917 }
918 item = last_elem(&lp->setup_variants_list);
919 assert(item);
920 assert(item->base);
921 remove_setup_variant(lp, item->base);
922 }
923 }
924
925
926 /**
927 * Update fragment/vertex shader linkage state. This is called just
928 * prior to drawing something when some fragment-related state has
929 * changed.
930 */
931 void
932 llvmpipe_update_setup(struct llvmpipe_context *lp)
933 {
934 struct lp_setup_variant_key *key = &lp->setup_variant.key;
935 struct lp_setup_variant *variant = NULL;
936 struct lp_setup_variant_list_item *li;
937
938 lp_make_setup_variant_key(lp, key);
939
940 foreach(li, &lp->setup_variants_list) {
941 if(li->base->key.size == key->size &&
942 memcmp(&li->base->key, key, key->size) == 0) {
943 variant = li->base;
944 break;
945 }
946 }
947
948 if (variant) {
949 move_to_head(&lp->setup_variants_list, &variant->list_item_global);
950 }
951 else {
952 if (lp->nr_setup_variants >= LP_MAX_SETUP_VARIANTS) {
953 cull_setup_variants(lp);
954 }
955
956 variant = generate_setup_variant(key, lp);
957 if (variant) {
958 insert_at_head(&lp->setup_variants_list, &variant->list_item_global);
959 lp->nr_setup_variants++;
960 llvmpipe_variant_count++;
961 }
962 }
963
964 lp_setup_set_setup_variant(lp->setup,
965 variant);
966 }
967
968 void
969 lp_delete_setup_variants(struct llvmpipe_context *lp)
970 {
971 struct lp_setup_variant_list_item *li;
972 li = first_elem(&lp->setup_variants_list);
973 while(!at_end(&lp->setup_variants_list, li)) {
974 struct lp_setup_variant_list_item *next = next_elem(li);
975 remove_setup_variant(lp, li->base);
976 li = next;
977 }
978 }
979
980 void
981 lp_dump_setup_coef( const struct lp_setup_variant_key *key,
982 const float (*sa0)[4],
983 const float (*sdadx)[4],
984 const float (*sdady)[4])
985 {
986 int i, slot;
987
988 for (i = 0; i < TGSI_NUM_CHANNELS; i++) {
989 float a0 = sa0 [0][i];
990 float dadx = sdadx[0][i];
991 float dady = sdady[0][i];
992
993 debug_printf("POS.%c: a0 = %f, dadx = %f, dady = %f\n",
994 "xyzw"[i],
995 a0, dadx, dady);
996 }
997
998 for (slot = 0; slot < key->num_inputs; slot++) {
999 unsigned usage_mask = key->inputs[slot].usage_mask;
1000 for (i = 0; i < TGSI_NUM_CHANNELS; i++) {
1001 if (usage_mask & (1 << i)) {
1002 float a0 = sa0 [1 + slot][i];
1003 float dadx = sdadx[1 + slot][i];
1004 float dady = sdady[1 + slot][i];
1005
1006 debug_printf("IN[%u].%c: a0 = %f, dadx = %f, dady = %f\n",
1007 slot,
1008 "xyzw"[i],
1009 a0, dadx, dady);
1010 }
1011 }
1012 }
1013 }