1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
4 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
31 * Position and shader input interpolation.
33 * @author Jose Fonseca <jfonseca@vmware.com>
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"
49 attrib_name(LLVMValueRef val
, unsigned attrib
, unsigned chan
, const char *suffix
)
52 lp_build_name(val
, "pos.%c%s", "xyzw"[chan
], suffix
);
54 lp_build_name(val
, "input%u.%c%s", attrib
- 1, "xyzw"[chan
], suffix
);
59 coeffs_init(struct lp_build_interp_soa_context
*bld
,
61 LLVMValueRef dadx_ptr
,
62 LLVMValueRef dady_ptr
)
64 LLVMBuilderRef builder
= bld
->base
.builder
;
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
;
79 case TGSI_INTERPOLATE_PERSPECTIVE
:
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");
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");
102 bld
->a0
[attrib
][chan
] = a0
;
103 bld
->dadx
[attrib
][chan
] = dadx
;
104 bld
->dady
[attrib
][chan
] = dady
;
112 * Small vector x scale multiplication optimization.
114 * TODO: Should be elsewhere.
117 coeff_multiply(struct lp_build_interp_soa_context
*bld
,
125 return bld
->base
.zero
;
129 return lp_build_add(&bld
->base
, coeff
, coeff
);
131 factor
= lp_build_const_scalar(bld
->base
.type
, (double)step
);
132 return lp_build_mul(&bld
->base
, coeff
, factor
);
138 * Multiply the dadx and dady with the xstep and ystep respectively.
141 coeffs_update(struct lp_build_interp_soa_context
*bld
)
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
);
162 attribs_init(struct lp_build_interp_soa_context
*bld
)
164 LLVMValueRef x
= bld
->pos
[0];
165 LLVMValueRef y
= bld
->pos
[1];
166 LLVMValueRef oow
= NULL
;
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
];
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
));
187 /* Keep the value of the attribue before perspective divide for faster updates */
188 bld
->attribs_pre
[attrib
][chan
] = res
;
190 if (mode
== TGSI_INTERPOLATE_PERSPECTIVE
) {
191 LLVMValueRef w
= bld
->pos
[3];
194 oow
= lp_build_rcp(&bld
->base
, w
);
195 res
= lp_build_mul(&bld
->base
, res
, oow
);
198 attrib_name(res
, attrib
, chan
, "");
200 bld
->attribs
[attrib
][chan
] = res
;
208 attribs_update(struct lp_build_interp_soa_context
*bld
)
210 LLVMValueRef oow
= NULL
;
214 for(attrib
= 0; attrib
< bld
->num_attribs
; ++attrib
) {
215 unsigned mask
= bld
->mask
[attrib
];
216 unsigned mode
= bld
->mode
[attrib
];
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
];
225 res
= bld
->attribs_pre
[attrib
][chan
];
228 res
= lp_build_add(&bld
->base
, res
, dadx
);
231 res
= lp_build_add(&bld
->base
, res
, dady
);
233 bld
->attribs_pre
[attrib
][chan
] = res
;
235 if (mode
== TGSI_INTERPOLATE_PERSPECTIVE
) {
236 LLVMValueRef w
= bld
->pos
[3];
239 oow
= lp_build_rcp(&bld
->base
, w
);
240 res
= lp_build_mul(&bld
->base
, res
, oow
);
243 attrib_name(res
, attrib
, chan
, "");
245 bld
->attribs
[attrib
][chan
] = res
;
254 * Generate the position vectors.
256 * Parameter x0, y0 are the integer values with the quad upper left coordinates.
259 pos_init(struct lp_build_interp_soa_context
*bld
,
263 lp_build_name(x0
, "pos.x");
264 lp_build_name(y0
, "pos.y");
266 bld
->attribs
[0][0] = x0
;
267 bld
->attribs
[0][1] = y0
;
272 pos_update(struct lp_build_interp_soa_context
*bld
)
274 LLVMValueRef x
= bld
->attribs
[0][0];
275 LLVMValueRef y
= bld
->attribs
[0][1];
278 x
= lp_build_add(&bld
->base
, x
, lp_build_const_scalar(bld
->base
.type
, bld
->xstep
));
281 y
= lp_build_add(&bld
->base
, y
, lp_build_const_scalar(bld
->base
.type
, bld
->ystep
));
283 lp_build_name(x
, "pos.x");
284 lp_build_name(y
, "pos.y");
286 bld
->attribs
[0][0] = x
;
287 bld
->attribs
[0][1] = y
;
292 lp_build_interp_soa_init(struct lp_build_interp_soa_context
*bld
,
293 const struct tgsi_token
*tokens
,
294 LLVMBuilderRef builder
,
297 LLVMValueRef dadx_ptr
,
298 LLVMValueRef dady_ptr
,
304 struct tgsi_parse_context parse
;
305 struct tgsi_full_declaration
*decl
;
307 memset(bld
, 0, sizeof *bld
);
309 lp_build_context_init(&bld
->base
, builder
, type
);
311 /* For convenience */
312 bld
->pos
= bld
->attribs
[0];
313 bld
->inputs
= (const LLVMValueRef (*)[NUM_CHANNELS
]) bld
->attribs
[1];
316 bld
->num_attribs
= 1;
317 bld
->mask
[0] = TGSI_WRITEMASK_ZW
;
318 bld
->mode
[0] = TGSI_INTERPOLATE_LINEAR
;
321 tgsi_parse_init( &parse
, tokens
);
322 while( !tgsi_parse_end_of_tokens( &parse
) ) {
323 tgsi_parse_token( &parse
);
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
;
332 first
= decl
->DeclarationRange
.First
;
333 last
= decl
->DeclarationRange
.Last
;
334 mask
= decl
->Declaration
.UsageMask
;
336 for( attrib
= first
; attrib
<= last
; ++attrib
) {
337 bld
->mask
[1 + attrib
] = mask
;
338 bld
->mode
[1 + attrib
] = decl
->Declaration
.Interpolate
;
341 bld
->num_attribs
= MAX2(bld
->num_attribs
, 1 + last
+ 1);
345 case TGSI_TOKEN_TYPE_INSTRUCTION
:
346 case TGSI_TOKEN_TYPE_IMMEDIATE
:
353 tgsi_parse_free( &parse
);
355 coeffs_init(bld
, a0_ptr
, dadx_ptr
, dady_ptr
);
357 pos_init(bld
, x0
, y0
);
369 * Advance the position and inputs with the xstep and ystep.
372 lp_build_interp_soa_update(struct lp_build_interp_soa_context
*bld
)