818c0e943e3dacbf4722111f3ccd40662b4ca4b9
[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 * Multiply the dadx and dady with the xstep and ystep respectively.
113 */
114 static void
115 coeffs_update(struct lp_build_interp_soa_context *bld)
116 {
117 unsigned attrib;
118 unsigned chan;
119
120 for(attrib = 0; attrib < bld->num_attribs; ++attrib) {
121 unsigned mask = bld->mask[attrib];
122 unsigned mode = bld->mode[attrib];
123 if (mode != TGSI_INTERPOLATE_CONSTANT) {
124 for(chan = 0; chan < NUM_CHANNELS; ++chan) {
125 if(mask & (1 << chan)) {
126 bld->dadx[attrib][chan] = lp_build_mul_imm(&bld->base, bld->dadx[attrib][chan], bld->xstep);
127 bld->dady[attrib][chan] = lp_build_mul_imm(&bld->base, bld->dady[attrib][chan], bld->ystep);
128 }
129 }
130 }
131 }
132 }
133
134
135 static void
136 attribs_init(struct lp_build_interp_soa_context *bld)
137 {
138 LLVMValueRef x = bld->pos[0];
139 LLVMValueRef y = bld->pos[1];
140 LLVMValueRef oow = NULL;
141 unsigned attrib;
142 unsigned chan;
143
144 for(attrib = 0; attrib < bld->num_attribs; ++attrib) {
145 unsigned mask = bld->mask[attrib];
146 unsigned mode = bld->mode[attrib];
147 for(chan = 0; chan < NUM_CHANNELS; ++chan) {
148 if(mask & (1 << chan)) {
149 LLVMValueRef a0 = bld->a0 [attrib][chan];
150 LLVMValueRef dadx = bld->dadx[attrib][chan];
151 LLVMValueRef dady = bld->dady[attrib][chan];
152 LLVMValueRef res;
153
154 res = a0;
155
156 if (mode != TGSI_INTERPOLATE_CONSTANT) {
157 res = lp_build_add(&bld->base, res, lp_build_mul(&bld->base, x, dadx));
158 res = lp_build_add(&bld->base, res, lp_build_mul(&bld->base, y, dady));
159 }
160
161 /* Keep the value of the attribue before perspective divide for faster updates */
162 bld->attribs_pre[attrib][chan] = res;
163
164 if (mode == TGSI_INTERPOLATE_PERSPECTIVE) {
165 LLVMValueRef w = bld->pos[3];
166 assert(attrib != 0);
167 if(!oow)
168 oow = lp_build_rcp(&bld->base, w);
169 res = lp_build_mul(&bld->base, res, oow);
170 }
171
172 attrib_name(res, attrib, chan, "");
173
174 bld->attribs[attrib][chan] = res;
175 }
176 }
177 }
178 }
179
180
181 static void
182 attribs_update(struct lp_build_interp_soa_context *bld)
183 {
184 LLVMValueRef oow = NULL;
185 unsigned attrib;
186 unsigned chan;
187
188 for(attrib = 0; attrib < bld->num_attribs; ++attrib) {
189 unsigned mask = bld->mask[attrib];
190 unsigned mode = bld->mode[attrib];
191
192 if (mode != TGSI_INTERPOLATE_CONSTANT) {
193 for(chan = 0; chan < NUM_CHANNELS; ++chan) {
194 if(mask & (1 << chan)) {
195 LLVMValueRef dadx = bld->dadx[attrib][chan];
196 LLVMValueRef dady = bld->dady[attrib][chan];
197 LLVMValueRef res;
198
199 res = bld->attribs_pre[attrib][chan];
200
201 if(bld->xstep)
202 res = lp_build_add(&bld->base, res, dadx);
203
204 if(bld->ystep)
205 res = lp_build_add(&bld->base, res, dady);
206
207 bld->attribs_pre[attrib][chan] = res;
208
209 if (mode == TGSI_INTERPOLATE_PERSPECTIVE) {
210 LLVMValueRef w = bld->pos[3];
211 assert(attrib != 0);
212 if(!oow)
213 oow = lp_build_rcp(&bld->base, w);
214 res = lp_build_mul(&bld->base, res, oow);
215 }
216
217 attrib_name(res, attrib, chan, "");
218
219 bld->attribs[attrib][chan] = res;
220 }
221 }
222 }
223 }
224 }
225
226
227 /**
228 * Generate the position vectors.
229 *
230 * Parameter x0, y0 are the integer values with the quad upper left coordinates.
231 */
232 static void
233 pos_init(struct lp_build_interp_soa_context *bld,
234 LLVMValueRef x0,
235 LLVMValueRef y0)
236 {
237 lp_build_name(x0, "pos.x");
238 lp_build_name(y0, "pos.y");
239
240 bld->attribs[0][0] = x0;
241 bld->attribs[0][1] = y0;
242 }
243
244
245 static void
246 pos_update(struct lp_build_interp_soa_context *bld)
247 {
248 LLVMValueRef x = bld->attribs[0][0];
249 LLVMValueRef y = bld->attribs[0][1];
250
251 if(bld->xstep)
252 x = lp_build_add(&bld->base, x, lp_build_const_scalar(bld->base.type, bld->xstep));
253
254 if(bld->ystep)
255 y = lp_build_add(&bld->base, y, lp_build_const_scalar(bld->base.type, bld->ystep));
256
257 lp_build_name(x, "pos.x");
258 lp_build_name(y, "pos.y");
259
260 bld->attribs[0][0] = x;
261 bld->attribs[0][1] = y;
262 }
263
264
265 void
266 lp_build_interp_soa_init(struct lp_build_interp_soa_context *bld,
267 const struct tgsi_token *tokens,
268 LLVMBuilderRef builder,
269 struct lp_type type,
270 LLVMValueRef a0_ptr,
271 LLVMValueRef dadx_ptr,
272 LLVMValueRef dady_ptr,
273 LLVMValueRef x0,
274 LLVMValueRef y0,
275 int xstep,
276 int ystep)
277 {
278 struct tgsi_parse_context parse;
279 struct tgsi_full_declaration *decl;
280
281 memset(bld, 0, sizeof *bld);
282
283 lp_build_context_init(&bld->base, builder, type);
284
285 /* For convenience */
286 bld->pos = bld->attribs[0];
287 bld->inputs = (const LLVMValueRef (*)[NUM_CHANNELS]) bld->attribs[1];
288
289 /* Position */
290 bld->num_attribs = 1;
291 bld->mask[0] = TGSI_WRITEMASK_ZW;
292 bld->mode[0] = TGSI_INTERPOLATE_LINEAR;
293
294 /* Inputs */
295 tgsi_parse_init( &parse, tokens );
296 while( !tgsi_parse_end_of_tokens( &parse ) ) {
297 tgsi_parse_token( &parse );
298
299 switch( parse.FullToken.Token.Type ) {
300 case TGSI_TOKEN_TYPE_DECLARATION:
301 decl = &parse.FullToken.FullDeclaration;
302 if( decl->Declaration.File == TGSI_FILE_INPUT ) {
303 unsigned first, last, mask;
304 unsigned attrib;
305
306 first = decl->DeclarationRange.First;
307 last = decl->DeclarationRange.Last;
308 mask = decl->Declaration.UsageMask;
309
310 for( attrib = first; attrib <= last; ++attrib ) {
311 bld->mask[1 + attrib] = mask;
312 bld->mode[1 + attrib] = decl->Declaration.Interpolate;
313 }
314
315 bld->num_attribs = MAX2(bld->num_attribs, 1 + last + 1);
316 }
317 break;
318
319 case TGSI_TOKEN_TYPE_INSTRUCTION:
320 case TGSI_TOKEN_TYPE_IMMEDIATE:
321 break;
322
323 default:
324 assert( 0 );
325 }
326 }
327 tgsi_parse_free( &parse );
328
329 coeffs_init(bld, a0_ptr, dadx_ptr, dady_ptr);
330
331 pos_init(bld, x0, y0);
332
333 attribs_init(bld);
334
335 bld->xstep = xstep;
336 bld->ystep = ystep;
337
338 coeffs_update(bld);
339 }
340
341
342 /**
343 * Advance the position and inputs with the xstep and ystep.
344 */
345 void
346 lp_build_interp_soa_update(struct lp_build_interp_soa_context *bld)
347 {
348 pos_update(bld);
349
350 attribs_update(bld);
351 }