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