Merge branch 'mesa_7_6_branch'
[mesa.git] / src / gallium / drivers / llvmpipe / lp_bld_interp.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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_parse.h"
41 #include "lp_bld_debug.h"
42 #include "lp_bld_const.h"
43 #include "lp_bld_arit.h"
44 #include "lp_bld_swizzle.h"
45 #include "lp_bld_interp.h"
46
47
48 static void
49 attrib_name(LLVMValueRef val, unsigned attrib, unsigned chan, const char *suffix)
50 {
51 if(attrib == 0)
52 lp_build_name(val, "pos.%c%s", "xyzw"[chan], suffix);
53 else
54 lp_build_name(val, "input%u.%c%s", attrib - 1, "xyzw"[chan], suffix);
55 }
56
57
58 static void
59 coeffs_init(struct lp_build_interp_soa_context *bld,
60 LLVMValueRef a0_ptr,
61 LLVMValueRef dadx_ptr,
62 LLVMValueRef dady_ptr)
63 {
64 LLVMBuilderRef builder = bld->base.builder;
65 unsigned attrib;
66 unsigned chan;
67
68 for(attrib = 0; attrib < bld->num_attribs; ++attrib) {
69 unsigned mask = bld->mask[attrib];
70 unsigned mode = bld->mode[attrib];
71 for(chan = 0; chan < NUM_CHANNELS; ++chan) {
72 if(mask & (1 << chan)) {
73 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), attrib*NUM_CHANNELS + chan, 0);
74 LLVMValueRef a0 = NULL;
75 LLVMValueRef dadx = NULL;
76 LLVMValueRef dady = NULL;
77
78 switch( mode ) {
79 case TGSI_INTERPOLATE_PERSPECTIVE:
80 /* fall-through */
81
82 case TGSI_INTERPOLATE_LINEAR:
83 dadx = LLVMBuildLoad(builder, LLVMBuildGEP(builder, dadx_ptr, &index, 1, ""), "");
84 dady = LLVMBuildLoad(builder, LLVMBuildGEP(builder, dady_ptr, &index, 1, ""), "");
85 dadx = lp_build_broadcast_scalar(&bld->base, dadx);
86 dady = lp_build_broadcast_scalar(&bld->base, dady);
87 attrib_name(dadx, attrib, chan, ".dadx");
88 attrib_name(dady, attrib, chan, ".dady");
89 /* fall-through */
90
91 case TGSI_INTERPOLATE_CONSTANT:
92 a0 = LLVMBuildLoad(builder, LLVMBuildGEP(builder, a0_ptr, &index, 1, ""), "");
93 a0 = lp_build_broadcast_scalar(&bld->base, a0);
94 attrib_name(a0, attrib, chan, ".dady");
95 break;
96
97 default:
98 assert(0);
99 break;
100 }
101
102 bld->a0 [attrib][chan] = a0;
103 bld->dadx[attrib][chan] = dadx;
104 bld->dady[attrib][chan] = dady;
105 }
106 }
107 }
108 }
109
110
111 /**
112 * Small vector x scale multiplication optimization.
113 *
114 * TODO: Should be elsewhere.
115 */
116 static LLVMValueRef
117 coeff_multiply(struct lp_build_interp_soa_context *bld,
118 LLVMValueRef coeff,
119 int step)
120 {
121 LLVMValueRef factor;
122
123 switch(step) {
124 case 0:
125 return bld->base.zero;
126 case 1:
127 return coeff;
128 case 2:
129 return lp_build_add(&bld->base, coeff, coeff);
130 default:
131 factor = lp_build_const_scalar(bld->base.type, (double)step);
132 return lp_build_mul(&bld->base, coeff, factor);
133 }
134 }
135
136
137 /**
138 * Multiply the dadx and dady with the xstep and ystep respectively.
139 */
140 static void
141 coeffs_update(struct lp_build_interp_soa_context *bld)
142 {
143 unsigned attrib;
144 unsigned chan;
145
146 for(attrib = 0; attrib < bld->num_attribs; ++attrib) {
147 unsigned mask = bld->mask[attrib];
148 unsigned mode = bld->mode[attrib];
149 if (mode != TGSI_INTERPOLATE_CONSTANT) {
150 for(chan = 0; chan < NUM_CHANNELS; ++chan) {
151 if(mask & (1 << chan)) {
152 bld->dadx[attrib][chan] = coeff_multiply(bld, bld->dadx[attrib][chan], bld->xstep);
153 bld->dady[attrib][chan] = coeff_multiply(bld, bld->dady[attrib][chan], bld->ystep);
154 }
155 }
156 }
157 }
158 }
159
160
161 static void
162 attribs_init(struct lp_build_interp_soa_context *bld)
163 {
164 LLVMValueRef x = bld->pos[0];
165 LLVMValueRef y = bld->pos[1];
166 LLVMValueRef oow = NULL;
167 unsigned attrib;
168 unsigned chan;
169
170 for(attrib = 0; attrib < bld->num_attribs; ++attrib) {
171 unsigned mask = bld->mask[attrib];
172 unsigned mode = bld->mode[attrib];
173 for(chan = 0; chan < NUM_CHANNELS; ++chan) {
174 if(mask & (1 << chan)) {
175 LLVMValueRef a0 = bld->a0 [attrib][chan];
176 LLVMValueRef dadx = bld->dadx[attrib][chan];
177 LLVMValueRef dady = bld->dady[attrib][chan];
178 LLVMValueRef res;
179
180 res = a0;
181
182 if (mode != TGSI_INTERPOLATE_CONSTANT) {
183 res = lp_build_add(&bld->base, res, lp_build_mul(&bld->base, x, dadx));
184 res = lp_build_add(&bld->base, res, lp_build_mul(&bld->base, y, dady));
185 }
186
187 /* Keep the value of the attribue before perspective divide for faster updates */
188 bld->attribs_pre[attrib][chan] = res;
189
190 if (mode == TGSI_INTERPOLATE_PERSPECTIVE) {
191 LLVMValueRef w = bld->pos[3];
192 assert(attrib != 0);
193 if(!oow)
194 oow = lp_build_rcp(&bld->base, w);
195 res = lp_build_mul(&bld->base, res, oow);
196 }
197
198 attrib_name(res, attrib, chan, "");
199
200 bld->attribs[attrib][chan] = res;
201 }
202 }
203 }
204 }
205
206
207 static void
208 attribs_update(struct lp_build_interp_soa_context *bld)
209 {
210 LLVMValueRef oow = NULL;
211 unsigned attrib;
212 unsigned chan;
213
214 for(attrib = 0; attrib < bld->num_attribs; ++attrib) {
215 unsigned mask = bld->mask[attrib];
216 unsigned mode = bld->mode[attrib];
217
218 if (mode != TGSI_INTERPOLATE_CONSTANT) {
219 for(chan = 0; chan < NUM_CHANNELS; ++chan) {
220 if(mask & (1 << chan)) {
221 LLVMValueRef dadx = bld->dadx[attrib][chan];
222 LLVMValueRef dady = bld->dady[attrib][chan];
223 LLVMValueRef res;
224
225 res = bld->attribs_pre[attrib][chan];
226
227 if(bld->xstep)
228 res = lp_build_add(&bld->base, res, dadx);
229
230 if(bld->ystep)
231 res = lp_build_add(&bld->base, res, dady);
232
233 bld->attribs_pre[attrib][chan] = res;
234
235 if (mode == TGSI_INTERPOLATE_PERSPECTIVE) {
236 LLVMValueRef w = bld->pos[3];
237 assert(attrib != 0);
238 if(!oow)
239 oow = lp_build_rcp(&bld->base, w);
240 res = lp_build_mul(&bld->base, res, oow);
241 }
242
243 attrib_name(res, attrib, chan, "");
244
245 bld->attribs[attrib][chan] = res;
246 }
247 }
248 }
249 }
250 }
251
252
253 /**
254 * Generate the position vectors.
255 *
256 * Parameter x0, y0 are the integer values with the quad upper left coordinates.
257 */
258 static void
259 pos_init(struct lp_build_interp_soa_context *bld,
260 LLVMValueRef x0,
261 LLVMValueRef y0)
262 {
263 lp_build_name(x0, "pos.x");
264 lp_build_name(y0, "pos.y");
265
266 bld->attribs[0][0] = x0;
267 bld->attribs[0][1] = y0;
268 }
269
270
271 static void
272 pos_update(struct lp_build_interp_soa_context *bld)
273 {
274 LLVMValueRef x = bld->attribs[0][0];
275 LLVMValueRef y = bld->attribs[0][1];
276
277 if(bld->xstep)
278 x = lp_build_add(&bld->base, x, lp_build_const_scalar(bld->base.type, bld->xstep));
279
280 if(bld->ystep)
281 y = lp_build_add(&bld->base, y, lp_build_const_scalar(bld->base.type, bld->ystep));
282
283 lp_build_name(x, "pos.x");
284 lp_build_name(y, "pos.y");
285
286 bld->attribs[0][0] = x;
287 bld->attribs[0][1] = y;
288 }
289
290
291 void
292 lp_build_interp_soa_init(struct lp_build_interp_soa_context *bld,
293 const struct tgsi_token *tokens,
294 LLVMBuilderRef builder,
295 struct lp_type type,
296 LLVMValueRef a0_ptr,
297 LLVMValueRef dadx_ptr,
298 LLVMValueRef dady_ptr,
299 LLVMValueRef x0,
300 LLVMValueRef y0,
301 int xstep,
302 int ystep)
303 {
304 struct tgsi_parse_context parse;
305 struct tgsi_full_declaration *decl;
306
307 memset(bld, 0, sizeof *bld);
308
309 lp_build_context_init(&bld->base, builder, type);
310
311 /* For convenience */
312 bld->pos = bld->attribs[0];
313 bld->inputs = (const LLVMValueRef (*)[NUM_CHANNELS]) bld->attribs[1];
314
315 /* Position */
316 bld->num_attribs = 1;
317 bld->mask[0] = TGSI_WRITEMASK_ZW;
318 bld->mode[0] = TGSI_INTERPOLATE_LINEAR;
319
320 /* Inputs */
321 tgsi_parse_init( &parse, tokens );
322 while( !tgsi_parse_end_of_tokens( &parse ) ) {
323 tgsi_parse_token( &parse );
324
325 switch( parse.FullToken.Token.Type ) {
326 case TGSI_TOKEN_TYPE_DECLARATION:
327 decl = &parse.FullToken.FullDeclaration;
328 if( decl->Declaration.File == TGSI_FILE_INPUT ) {
329 unsigned first, last, mask;
330 unsigned attrib;
331
332 first = decl->DeclarationRange.First;
333 last = decl->DeclarationRange.Last;
334 mask = decl->Declaration.UsageMask;
335
336 for( attrib = first; attrib <= last; ++attrib ) {
337 bld->mask[1 + attrib] = mask;
338 bld->mode[1 + attrib] = decl->Declaration.Interpolate;
339 }
340
341 bld->num_attribs = MAX2(bld->num_attribs, 1 + last + 1);
342 }
343 break;
344
345 case TGSI_TOKEN_TYPE_INSTRUCTION:
346 case TGSI_TOKEN_TYPE_IMMEDIATE:
347 break;
348
349 default:
350 assert( 0 );
351 }
352 }
353 tgsi_parse_free( &parse );
354
355 coeffs_init(bld, a0_ptr, dadx_ptr, dady_ptr);
356
357 pos_init(bld, x0, y0);
358
359 attribs_init(bld);
360
361 bld->xstep = xstep;
362 bld->ystep = ystep;
363
364 coeffs_update(bld);
365 }
366
367
368 /**
369 * Advance the position and inputs with the xstep and ystep.
370 */
371 void
372 lp_build_interp_soa_update(struct lp_build_interp_soa_context *bld)
373 {
374 pos_update(bld);
375
376 attribs_update(bld);
377 }