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