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