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