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