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