8e4f029fc8188fe16ebf0ca297ff17b45b4884c3
[mesa.git] / src / gallium / drivers / llvmpipe / lp_bld_interp.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * Copyright 2007-2008 VMware, Inc.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 /**
30 * @file
31 * Position and shader input interpolation.
32 *
33 * @author Jose Fonseca <jfonseca@vmware.com>
34 */
35
36 #include "pipe/p_shader_tokens.h"
37 #include "util/u_debug.h"
38 #include "util/u_memory.h"
39 #include "util/u_math.h"
40 #include "tgsi/tgsi_scan.h"
41 #include "gallivm/lp_bld_debug.h"
42 #include "gallivm/lp_bld_const.h"
43 #include "gallivm/lp_bld_arit.h"
44 #include "gallivm/lp_bld_swizzle.h"
45 #include "gallivm/lp_bld_flow.h"
46 #include "lp_bld_interp.h"
47
48
49 /*
50 * The shader JIT function operates on blocks of quads.
51 * Each block has 2x2 quads and each quad has 2x2 pixels.
52 *
53 * We iterate over the quads in order 0, 1, 2, 3:
54 *
55 * #################
56 * # | # | #
57 * #---0---#---1---#
58 * # | # | #
59 * #################
60 * # | # | #
61 * #---2---#---3---#
62 * # | # | #
63 * #################
64 *
65 * If we iterate over multiple quads at once, quads 01 and 23 are processed
66 * together.
67 *
68 * Within each quad, we have four pixels which are represented in SOA
69 * order:
70 *
71 * #########
72 * # 0 | 1 #
73 * #---+---#
74 * # 2 | 3 #
75 * #########
76 *
77 * So the green channel (for example) of the four pixels is stored in
78 * a single vector register: {g0, g1, g2, g3}.
79 * The order stays the same even with multiple quads:
80 * 0 1 4 5
81 * 2 3 6 7
82 * is stored as g0..g7
83 */
84
85
86 /**
87 * Do one perspective divide per quad.
88 *
89 * For perspective interpolation, the final attribute value is given
90 *
91 * a' = a/w = a * oow
92 *
93 * where
94 *
95 * a = a0 + dadx*x + dady*y
96 * w = w0 + dwdx*x + dwdy*y
97 * oow = 1/w = 1/(w0 + dwdx*x + dwdy*y)
98 *
99 * Instead of computing the division per pixel, with this macro we compute the
100 * division on the upper left pixel of each quad, and use a linear
101 * approximation in the remaining pixels, given by:
102 *
103 * da'dx = (dadx - dwdx*a)*oow
104 * da'dy = (dady - dwdy*a)*oow
105 *
106 * Ironically, this actually makes things slower -- probably because the
107 * divide hardware unit is rarely used, whereas the multiply unit is typically
108 * already saturated.
109 */
110 #define PERSPECTIVE_DIVIDE_PER_QUAD 0
111
112
113 static const unsigned char quad_offset_x[16] = {0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3};
114 static const unsigned char quad_offset_y[16] = {0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3};
115
116
117 static void
118 attrib_name(LLVMValueRef val, unsigned attrib, unsigned chan, const char *suffix)
119 {
120 if(attrib == 0)
121 lp_build_name(val, "pos.%c%s", "xyzw"[chan], suffix);
122 else
123 lp_build_name(val, "input%u.%c%s", attrib - 1, "xyzw"[chan], suffix);
124 }
125
126 static void
127 calc_offsets(struct lp_build_context *coeff_bld,
128 unsigned quad_start_index,
129 LLVMValueRef *pixoffx,
130 LLVMValueRef *pixoffy)
131 {
132 unsigned i;
133 unsigned num_pix = coeff_bld->type.length;
134 struct gallivm_state *gallivm = coeff_bld->gallivm;
135 LLVMBuilderRef builder = coeff_bld->gallivm->builder;
136 LLVMValueRef nr, pixxf, pixyf;
137
138 *pixoffx = coeff_bld->undef;
139 *pixoffy = coeff_bld->undef;
140
141 for (i = 0; i < num_pix; i++) {
142 nr = lp_build_const_int32(gallivm, i);
143 pixxf = lp_build_const_float(gallivm, quad_offset_x[i % num_pix] +
144 (quad_start_index & 1) * 2);
145 pixyf = lp_build_const_float(gallivm, quad_offset_y[i % num_pix] +
146 (quad_start_index & 2));
147 *pixoffx = LLVMBuildInsertElement(builder, *pixoffx, pixxf, nr, "");
148 *pixoffy = LLVMBuildInsertElement(builder, *pixoffy, pixyf, nr, "");
149 }
150 }
151
152
153 /* Much easier, and significantly less instructions in the per-stamp
154 * part (less than half) but overall more instructions so a loss if
155 * most quads are active. Might be a win though with larger vectors.
156 * No ability to do per-quad divide (doable but not implemented)
157 * Could be made to work with passed in pixel offsets (i.e. active quad merging).
158 */
159 static void
160 coeffs_init_simple(struct lp_build_interp_soa_context *bld,
161 LLVMValueRef a0_ptr,
162 LLVMValueRef dadx_ptr,
163 LLVMValueRef dady_ptr)
164 {
165 struct lp_build_context *coeff_bld = &bld->coeff_bld;
166 struct lp_build_context *setup_bld = &bld->setup_bld;
167 struct gallivm_state *gallivm = coeff_bld->gallivm;
168 LLVMBuilderRef builder = gallivm->builder;
169 unsigned attrib;
170
171 for (attrib = 0; attrib < bld->num_attribs; ++attrib) {
172 /*
173 * always fetch all 4 values for performance/simplicity
174 * Note: we do that here because it seems to generate better
175 * code. It generates a lot of moves initially but less
176 * moves later. As far as I can tell this looks like a
177 * llvm issue, instead of simply reloading the values from
178 * the passed in pointers it if it runs out of registers
179 * it spills/reloads them. Maybe some optimization passes
180 * would help.
181 * Might want to investigate this again later.
182 */
183 const unsigned interp = bld->interp[attrib];
184 LLVMValueRef index = lp_build_const_int32(gallivm,
185 attrib * TGSI_NUM_CHANNELS);
186 LLVMValueRef ptr;
187 LLVMValueRef dadxaos = setup_bld->zero;
188 LLVMValueRef dadyaos = setup_bld->zero;
189 LLVMValueRef a0aos = setup_bld->zero;
190
191 switch (interp) {
192 case LP_INTERP_PERSPECTIVE:
193 /* fall-through */
194
195 case LP_INTERP_LINEAR:
196 ptr = LLVMBuildGEP(builder, dadx_ptr, &index, 1, "");
197 ptr = LLVMBuildBitCast(builder, ptr,
198 LLVMPointerType(setup_bld->vec_type, 0), "");
199 dadxaos = LLVMBuildLoad(builder, ptr, "");
200
201 ptr = LLVMBuildGEP(builder, dady_ptr, &index, 1, "");
202 ptr = LLVMBuildBitCast(builder, ptr,
203 LLVMPointerType(setup_bld->vec_type, 0), "");
204 dadyaos = LLVMBuildLoad(builder, ptr, "");
205
206 attrib_name(dadxaos, attrib, 0, ".dadxaos");
207 attrib_name(dadyaos, attrib, 0, ".dadyaos");
208 /* fall-through */
209
210 case LP_INTERP_CONSTANT:
211 case LP_INTERP_FACING:
212 ptr = LLVMBuildGEP(builder, a0_ptr, &index, 1, "");
213 ptr = LLVMBuildBitCast(builder, ptr,
214 LLVMPointerType(setup_bld->vec_type, 0), "");
215 a0aos = LLVMBuildLoad(builder, ptr, "");
216 attrib_name(a0aos, attrib, 0, ".a0aos");
217 break;
218
219 case LP_INTERP_POSITION:
220 /* Nothing to do as the position coeffs are already setup in slot 0 */
221 continue;
222
223 default:
224 assert(0);
225 break;
226 }
227 bld->a0aos[attrib] = a0aos;
228 bld->dadxaos[attrib] = dadxaos;
229 bld->dadyaos[attrib] = dadyaos;
230 }
231 }
232
233 /**
234 * Interpolate the shader input attribute values.
235 * This is called for each (group of) quad(s).
236 */
237 static void
238 attribs_update_simple(struct lp_build_interp_soa_context *bld,
239 struct gallivm_state *gallivm,
240 LLVMValueRef loop_iter,
241 int start,
242 int end)
243 {
244 LLVMBuilderRef builder = gallivm->builder;
245 struct lp_build_context *coeff_bld = &bld->coeff_bld;
246 struct lp_build_context *setup_bld = &bld->setup_bld;
247 LLVMValueRef oow = NULL;
248 unsigned attrib;
249 LLVMValueRef pixoffx;
250 LLVMValueRef pixoffy;
251 LLVMValueRef ptr;
252
253 /* could do this with code-generated passed in pixel offsets too */
254
255 assert(loop_iter);
256 ptr = LLVMBuildGEP(builder, bld->xoffset_store, &loop_iter, 1, "");
257 pixoffx = LLVMBuildLoad(builder, ptr, "");
258 ptr = LLVMBuildGEP(builder, bld->yoffset_store, &loop_iter, 1, "");
259 pixoffy = LLVMBuildLoad(builder, ptr, "");
260
261 pixoffx = LLVMBuildFAdd(builder, pixoffx,
262 lp_build_broadcast_scalar(coeff_bld, bld->x), "");
263 pixoffy = LLVMBuildFAdd(builder, pixoffy,
264 lp_build_broadcast_scalar(coeff_bld, bld->y), "");
265
266 for (attrib = start; attrib < end; attrib++) {
267 const unsigned mask = bld->mask[attrib];
268 const unsigned interp = bld->interp[attrib];
269 unsigned chan;
270
271 for (chan = 0; chan < TGSI_NUM_CHANNELS; chan++) {
272 if (mask & (1 << chan)) {
273 LLVMValueRef index;
274 LLVMValueRef dadx = coeff_bld->zero;
275 LLVMValueRef dady = coeff_bld->zero;
276 LLVMValueRef a = coeff_bld->zero;
277
278 index = lp_build_const_int32(gallivm, chan);
279 switch (interp) {
280 case LP_INTERP_PERSPECTIVE:
281 /* fall-through */
282
283 case LP_INTERP_LINEAR:
284 if (attrib == 0 && chan == 0) {
285 dadx = coeff_bld->one;
286 if (bld->pos_offset) {
287 a = lp_build_const_vec(gallivm, coeff_bld->type, bld->pos_offset);
288 }
289 }
290 else if (attrib == 0 && chan == 1) {
291 dady = coeff_bld->one;
292 if (bld->pos_offset) {
293 a = lp_build_const_vec(gallivm, coeff_bld->type, bld->pos_offset);
294 }
295 }
296 else {
297 dadx = lp_build_extract_broadcast(gallivm, setup_bld->type,
298 coeff_bld->type, bld->dadxaos[attrib],
299 index);
300 dady = lp_build_extract_broadcast(gallivm, setup_bld->type,
301 coeff_bld->type, bld->dadyaos[attrib],
302 index);
303 a = lp_build_extract_broadcast(gallivm, setup_bld->type,
304 coeff_bld->type, bld->a0aos[attrib],
305 index);
306 }
307 /*
308 * a = a0 + (x * dadx + y * dady)
309 */
310 a = lp_build_fmuladd(builder, dadx, pixoffx, a);
311 a = lp_build_fmuladd(builder, dady, pixoffy, a);
312
313 if (interp == LP_INTERP_PERSPECTIVE) {
314 if (oow == NULL) {
315 LLVMValueRef w = bld->attribs[0][3];
316 assert(attrib != 0);
317 assert(bld->mask[0] & TGSI_WRITEMASK_W);
318 oow = lp_build_rcp(coeff_bld, w);
319 }
320 a = lp_build_mul(coeff_bld, a, oow);
321 }
322 break;
323
324 case LP_INTERP_CONSTANT:
325 case LP_INTERP_FACING:
326 a = lp_build_extract_broadcast(gallivm, setup_bld->type,
327 coeff_bld->type, bld->a0aos[attrib],
328 index);
329 break;
330
331 case LP_INTERP_POSITION:
332 assert(attrib > 0);
333 a = bld->attribs[0][chan];
334 break;
335
336 default:
337 assert(0);
338 break;
339 }
340
341 if ((attrib == 0) && (chan == 2)){
342 /* FIXME: Depth values can exceed 1.0, due to the fact that
343 * setup interpolation coefficients refer to (0,0) which causes
344 * precision loss. So we must clamp to 1.0 here to avoid artifacts
345 */
346 a = lp_build_min(coeff_bld, a, coeff_bld->one);
347 }
348 bld->attribs[attrib][chan] = a;
349 }
350 }
351 }
352 }
353
354 /**
355 * Initialize the bld->a, dadq fields. This involves fetching
356 * those values from the arrays which are passed into the JIT function.
357 */
358 static void
359 coeffs_init(struct lp_build_interp_soa_context *bld,
360 LLVMValueRef a0_ptr,
361 LLVMValueRef dadx_ptr,
362 LLVMValueRef dady_ptr)
363 {
364 struct lp_build_context *coeff_bld = &bld->coeff_bld;
365 struct lp_build_context *setup_bld = &bld->setup_bld;
366 struct gallivm_state *gallivm = coeff_bld->gallivm;
367 LLVMBuilderRef builder = gallivm->builder;
368 LLVMValueRef pixoffx, pixoffy;
369 unsigned attrib;
370 unsigned chan;
371 unsigned i;
372
373 pixoffx = coeff_bld->undef;
374 pixoffy = coeff_bld->undef;
375 for (i = 0; i < coeff_bld->type.length; i++) {
376 LLVMValueRef nr = lp_build_const_int32(gallivm, i);
377 LLVMValueRef pixxf = lp_build_const_float(gallivm, quad_offset_x[i]);
378 LLVMValueRef pixyf = lp_build_const_float(gallivm, quad_offset_y[i]);
379 pixoffx = LLVMBuildInsertElement(builder, pixoffx, pixxf, nr, "");
380 pixoffy = LLVMBuildInsertElement(builder, pixoffy, pixyf, nr, "");
381 }
382
383
384 for (attrib = 0; attrib < bld->num_attribs; ++attrib) {
385 const unsigned mask = bld->mask[attrib];
386 const unsigned interp = bld->interp[attrib];
387 LLVMValueRef index = lp_build_const_int32(gallivm,
388 attrib * TGSI_NUM_CHANNELS);
389 LLVMValueRef ptr;
390 LLVMValueRef dadxaos = setup_bld->zero;
391 LLVMValueRef dadyaos = setup_bld->zero;
392 LLVMValueRef a0aos = setup_bld->zero;
393
394 /* always fetch all 4 values for performance/simplicity */
395 switch (interp) {
396 case LP_INTERP_PERSPECTIVE:
397 /* fall-through */
398
399 case LP_INTERP_LINEAR:
400 ptr = LLVMBuildGEP(builder, dadx_ptr, &index, 1, "");
401 ptr = LLVMBuildBitCast(builder, ptr,
402 LLVMPointerType(setup_bld->vec_type, 0), "");
403 dadxaos = LLVMBuildLoad(builder, ptr, "");
404
405 ptr = LLVMBuildGEP(builder, dady_ptr, &index, 1, "");
406 ptr = LLVMBuildBitCast(builder, ptr,
407 LLVMPointerType(setup_bld->vec_type, 0), "");
408 dadyaos = LLVMBuildLoad(builder, ptr, "");
409
410 attrib_name(dadxaos, attrib, 0, ".dadxaos");
411 attrib_name(dadyaos, attrib, 0, ".dadyaos");
412 /* fall-through */
413
414 case LP_INTERP_CONSTANT:
415 case LP_INTERP_FACING:
416 ptr = LLVMBuildGEP(builder, a0_ptr, &index, 1, "");
417 ptr = LLVMBuildBitCast(builder, ptr,
418 LLVMPointerType(setup_bld->vec_type, 0), "");
419 a0aos = LLVMBuildLoad(builder, ptr, "");
420 attrib_name(a0aos, attrib, 0, ".a0aos");
421 break;
422
423 case LP_INTERP_POSITION:
424 /* Nothing to do as the position coeffs are already setup in slot 0 */
425 continue;
426
427 default:
428 assert(0);
429 break;
430 }
431
432 /*
433 * a = a0 + (x * dadx + y * dady)
434 * a0aos is the attrib value at top left corner of stamp
435 */
436 if (interp != LP_INTERP_CONSTANT &&
437 interp != LP_INTERP_FACING) {
438 LLVMValueRef x = lp_build_broadcast_scalar(setup_bld, bld->x);
439 LLVMValueRef y = lp_build_broadcast_scalar(setup_bld, bld->y);
440 a0aos = lp_build_fmuladd(builder, x, dadxaos, a0aos);
441 a0aos = lp_build_fmuladd(builder, y, dadyaos, a0aos);
442 }
443
444 /*
445 * dadq = {0, dadx, dady, dadx + dady}
446 * for two quads (side by side) this is:
447 * {0, dadx, dady, dadx+dady, 2*dadx, 2*dadx+dady, 3*dadx+dady}
448 */
449 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
450 /* this generates a CRAPLOAD of shuffles... */
451 if (mask & (1 << chan)) {
452 LLVMValueRef dadx, dady;
453 LLVMValueRef dadq, dadq2;
454 LLVMValueRef a;
455 LLVMValueRef chan_index = lp_build_const_int32(gallivm, chan);
456
457 if (attrib == 0 && chan == 0) {
458 a = bld->x;
459 if (bld->pos_offset) {
460 a = LLVMBuildFAdd(builder, a, lp_build_const_float(gallivm, bld->pos_offset), "");
461 }
462 a = lp_build_broadcast_scalar(coeff_bld, a);
463 dadx = coeff_bld->one;
464 dady = coeff_bld->zero;
465 }
466 else if (attrib == 0 && chan == 1) {
467 a = bld->y;
468 if (bld->pos_offset) {
469 a = LLVMBuildFAdd(builder, a, lp_build_const_float(gallivm, bld->pos_offset), "");
470 }
471 a = lp_build_broadcast_scalar(coeff_bld, a);
472 dady = coeff_bld->one;
473 dadx = coeff_bld->zero;
474 }
475 else {
476 dadx = lp_build_extract_broadcast(gallivm, setup_bld->type,
477 coeff_bld->type, dadxaos, chan_index);
478 dady = lp_build_extract_broadcast(gallivm, setup_bld->type,
479 coeff_bld->type, dadyaos, chan_index);
480
481 /*
482 * a = {a, a, a, a}
483 */
484 a = lp_build_extract_broadcast(gallivm, setup_bld->type,
485 coeff_bld->type, a0aos, chan_index);
486 }
487
488 dadx = LLVMBuildFMul(builder, dadx, pixoffx, "");
489 dady = LLVMBuildFMul(builder, dady, pixoffy, "");
490 dadq = LLVMBuildFAdd(builder, dadx, dady, "");
491
492 /*
493 * Compute the attrib values on the upper-left corner of each
494 * group of quads.
495 * Note that if we process 2 quads at once this doesn't
496 * really exactly to what we want.
497 * We need to access elem 0 and 2 respectively later if we process
498 * 2 quads at once.
499 */
500
501 if (interp != LP_INTERP_CONSTANT &&
502 interp != LP_INTERP_FACING) {
503 dadq2 = LLVMBuildFAdd(builder, dadq, dadq, "");
504 a = LLVMBuildFAdd(builder, a, dadq2, "");
505 }
506
507 #if PERSPECTIVE_DIVIDE_PER_QUAD
508 /*
509 * a *= 1 / w
510 */
511
512 /*
513 * XXX since we're only going to access elements 0,2 out of 8
514 * if we have 8-wide vectors we should do the division only 4-wide.
515 * a is really a 2-elements in a 4-wide vector disguised as 8-wide
516 * in this case.
517 */
518 if (interp == LP_INTERP_PERSPECTIVE) {
519 LLVMValueRef w = bld->a[0][3];
520 assert(attrib != 0);
521 assert(bld->mask[0] & TGSI_WRITEMASK_W);
522 if (!bld->oow) {
523 bld->oow = lp_build_rcp(coeff_bld, w);
524 lp_build_name(bld->oow, "oow");
525 }
526 a = lp_build_mul(coeff_bld, a, bld->oow);
527 }
528 #endif
529
530 attrib_name(a, attrib, chan, ".a");
531 attrib_name(dadq, attrib, chan, ".dadq");
532
533 bld->a[attrib][chan] = lp_build_alloca(gallivm,
534 LLVMTypeOf(a), "");
535 LLVMBuildStore(builder, a, bld->a[attrib][chan]);
536 bld->dadq[attrib][chan] = dadq;
537 }
538 }
539 }
540 }
541
542
543 /**
544 * Increment the shader input attribute values.
545 * This is called when we move from one quad to the next.
546 */
547 static void
548 attribs_update(struct lp_build_interp_soa_context *bld,
549 struct gallivm_state *gallivm,
550 LLVMValueRef loop_iter,
551 int start,
552 int end)
553 {
554 LLVMBuilderRef builder = gallivm->builder;
555 struct lp_build_context *coeff_bld = &bld->coeff_bld;
556 LLVMValueRef oow = NULL;
557 unsigned attrib;
558 unsigned chan;
559
560 for(attrib = start; attrib < end; ++attrib) {
561 const unsigned mask = bld->mask[attrib];
562 const unsigned interp = bld->interp[attrib];
563 for(chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
564 if(mask & (1 << chan)) {
565 LLVMValueRef a;
566 if (interp == LP_INTERP_CONSTANT ||
567 interp == LP_INTERP_FACING) {
568 a = LLVMBuildLoad(builder, bld->a[attrib][chan], "");
569 }
570 else if (interp == LP_INTERP_POSITION) {
571 assert(attrib > 0);
572 a = bld->attribs[0][chan];
573 }
574 else {
575 LLVMValueRef dadq;
576
577 a = bld->a[attrib][chan];
578
579 /*
580 * Broadcast the attribute value for this quad into all elements
581 */
582
583 {
584 /* stored as vector load as float */
585 LLVMTypeRef ptr_type = LLVMPointerType(LLVMFloatTypeInContext(
586 gallivm->context), 0);
587 LLVMValueRef ptr;
588 a = LLVMBuildBitCast(builder, a, ptr_type, "");
589 ptr = LLVMBuildGEP(builder, a, &loop_iter, 1, "");
590 a = LLVMBuildLoad(builder, ptr, "");
591 a = lp_build_broadcast_scalar(&bld->coeff_bld, a);
592 }
593
594 /*
595 * Get the derivatives.
596 */
597
598 dadq = bld->dadq[attrib][chan];
599
600 #if PERSPECTIVE_DIVIDE_PER_QUAD
601 if (interp == LP_INTERP_PERSPECTIVE) {
602 LLVMValueRef dwdq = bld->dadq[0][3];
603
604 if (oow == NULL) {
605 assert(bld->oow);
606 oow = LLVMBuildShuffleVector(coeff_bld->builder,
607 bld->oow, coeff_bld->undef,
608 shuffle, "");
609 }
610
611 dadq = lp_build_sub(coeff_bld,
612 dadq,
613 lp_build_mul(coeff_bld, a, dwdq));
614 dadq = lp_build_mul(coeff_bld, dadq, oow);
615 }
616 #endif
617
618 /*
619 * Add the derivatives
620 */
621
622 a = lp_build_add(coeff_bld, a, dadq);
623
624 #if !PERSPECTIVE_DIVIDE_PER_QUAD
625 if (interp == LP_INTERP_PERSPECTIVE) {
626 if (oow == NULL) {
627 LLVMValueRef w = bld->attribs[0][3];
628 assert(attrib != 0);
629 assert(bld->mask[0] & TGSI_WRITEMASK_W);
630 oow = lp_build_rcp(coeff_bld, w);
631 }
632 a = lp_build_mul(coeff_bld, a, oow);
633 }
634 #endif
635
636 if (attrib == 0 && chan == 2) {
637 /* FIXME: Depth values can exceed 1.0, due to the fact that
638 * setup interpolation coefficients refer to (0,0) which causes
639 * precision loss. So we must clamp to 1.0 here to avoid artifacts
640 */
641 a = lp_build_min(coeff_bld, a, coeff_bld->one);
642 }
643
644 attrib_name(a, attrib, chan, "");
645 }
646 bld->attribs[attrib][chan] = a;
647 }
648 }
649 }
650 }
651
652
653 /**
654 * Generate the position vectors.
655 *
656 * Parameter x0, y0 are the integer values with upper left coordinates.
657 */
658 static void
659 pos_init(struct lp_build_interp_soa_context *bld,
660 LLVMValueRef x0,
661 LLVMValueRef y0)
662 {
663 LLVMBuilderRef builder = bld->coeff_bld.gallivm->builder;
664 struct lp_build_context *coeff_bld = &bld->coeff_bld;
665
666 bld->x = LLVMBuildSIToFP(builder, x0, coeff_bld->elem_type, "");
667 bld->y = LLVMBuildSIToFP(builder, y0, coeff_bld->elem_type, "");
668 }
669
670
671 /**
672 * Initialize fragment shader input attribute info.
673 */
674 void
675 lp_build_interp_soa_init(struct lp_build_interp_soa_context *bld,
676 struct gallivm_state *gallivm,
677 unsigned num_inputs,
678 const struct lp_shader_input *inputs,
679 boolean pixel_center_integer,
680 LLVMBuilderRef builder,
681 struct lp_type type,
682 LLVMValueRef a0_ptr,
683 LLVMValueRef dadx_ptr,
684 LLVMValueRef dady_ptr,
685 LLVMValueRef x0,
686 LLVMValueRef y0)
687 {
688 struct lp_type coeff_type;
689 struct lp_type setup_type;
690 unsigned attrib;
691 unsigned chan;
692
693 memset(bld, 0, sizeof *bld);
694
695 memset(&coeff_type, 0, sizeof coeff_type);
696 coeff_type.floating = TRUE;
697 coeff_type.sign = TRUE;
698 coeff_type.width = 32;
699 coeff_type.length = type.length;
700
701 memset(&setup_type, 0, sizeof setup_type);
702 setup_type.floating = TRUE;
703 setup_type.sign = TRUE;
704 setup_type.width = 32;
705 setup_type.length = TGSI_NUM_CHANNELS;
706
707
708 /* XXX: we don't support interpolating into any other types */
709 assert(memcmp(&coeff_type, &type, sizeof coeff_type) == 0);
710
711 lp_build_context_init(&bld->coeff_bld, gallivm, coeff_type);
712 lp_build_context_init(&bld->setup_bld, gallivm, setup_type);
713
714 /* For convenience */
715 bld->pos = bld->attribs[0];
716 bld->inputs = (const LLVMValueRef (*)[TGSI_NUM_CHANNELS]) bld->attribs[1];
717
718 /* Position */
719 bld->mask[0] = TGSI_WRITEMASK_XYZW;
720 bld->interp[0] = LP_INTERP_LINEAR;
721
722 /* Inputs */
723 for (attrib = 0; attrib < num_inputs; ++attrib) {
724 bld->mask[1 + attrib] = inputs[attrib].usage_mask;
725 bld->interp[1 + attrib] = inputs[attrib].interp;
726 }
727 bld->num_attribs = 1 + num_inputs;
728
729 /* Ensure all masked out input channels have a valid value */
730 for (attrib = 0; attrib < bld->num_attribs; ++attrib) {
731 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
732 bld->attribs[attrib][chan] = bld->coeff_bld.undef;
733 }
734 }
735
736 if (pixel_center_integer) {
737 bld->pos_offset = 0.0;
738 } else {
739 bld->pos_offset = 0.5;
740 }
741
742 pos_init(bld, x0, y0);
743
744 /*
745 * Simple method (single step interpolation) may be slower if vector length
746 * is just 4, but the results are different (generally less accurate) with
747 * the other method, so always use more accurate version.
748 */
749 if (1) {
750 bld->simple_interp = TRUE;
751 {
752 /* XXX this should use a global static table */
753 unsigned i;
754 unsigned num_loops = 16 / type.length;
755 LLVMValueRef pixoffx, pixoffy, index;
756 LLVMValueRef ptr;
757
758 bld->xoffset_store = lp_build_array_alloca(gallivm,
759 lp_build_vec_type(gallivm, type),
760 lp_build_const_int32(gallivm, num_loops),
761 "");
762 bld->yoffset_store = lp_build_array_alloca(gallivm,
763 lp_build_vec_type(gallivm, type),
764 lp_build_const_int32(gallivm, num_loops),
765 "");
766 for (i = 0; i < num_loops; i++) {
767 index = lp_build_const_int32(gallivm, i);
768 calc_offsets(&bld->coeff_bld, i*type.length/4, &pixoffx, &pixoffy);
769 ptr = LLVMBuildGEP(builder, bld->xoffset_store, &index, 1, "");
770 LLVMBuildStore(builder, pixoffx, ptr);
771 ptr = LLVMBuildGEP(builder, bld->yoffset_store, &index, 1, "");
772 LLVMBuildStore(builder, pixoffy, ptr);
773 }
774 }
775 coeffs_init_simple(bld, a0_ptr, dadx_ptr, dady_ptr);
776 }
777 else {
778 bld->simple_interp = FALSE;
779 coeffs_init(bld, a0_ptr, dadx_ptr, dady_ptr);
780 }
781
782 }
783
784
785 /*
786 * Advance the position and inputs to the given quad within the block.
787 */
788
789 void
790 lp_build_interp_soa_update_inputs_dyn(struct lp_build_interp_soa_context *bld,
791 struct gallivm_state *gallivm,
792 LLVMValueRef quad_start_index)
793 {
794 if (bld->simple_interp) {
795 attribs_update_simple(bld, gallivm, quad_start_index, 1, bld->num_attribs);
796 }
797 else {
798 attribs_update(bld, gallivm, quad_start_index, 1, bld->num_attribs);
799 }
800 }
801
802 void
803 lp_build_interp_soa_update_pos_dyn(struct lp_build_interp_soa_context *bld,
804 struct gallivm_state *gallivm,
805 LLVMValueRef quad_start_index)
806 {
807 if (bld->simple_interp) {
808 attribs_update_simple(bld, gallivm, quad_start_index, 0, 1);
809 }
810 else {
811 attribs_update(bld, gallivm, quad_start_index, 0, 1);
812 }
813 }
814