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 * Multiply the dadx and dady with the xstep and ystep respectively.
115 coeffs_update(struct lp_build_interp_soa_context
*bld
)
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
);
136 attribs_init(struct lp_build_interp_soa_context
*bld
)
138 LLVMValueRef x
= bld
->pos
[0];
139 LLVMValueRef y
= bld
->pos
[1];
140 LLVMValueRef oow
= NULL
;
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
];
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
));
161 /* Keep the value of the attribue before perspective divide for faster updates */
162 bld
->attribs_pre
[attrib
][chan
] = res
;
164 if (mode
== TGSI_INTERPOLATE_PERSPECTIVE
) {
165 LLVMValueRef w
= bld
->pos
[3];
168 oow
= lp_build_rcp(&bld
->base
, w
);
169 res
= lp_build_mul(&bld
->base
, res
, oow
);
172 attrib_name(res
, attrib
, chan
, "");
174 bld
->attribs
[attrib
][chan
] = res
;
182 attribs_update(struct lp_build_interp_soa_context
*bld
)
184 LLVMValueRef oow
= NULL
;
188 for(attrib
= 0; attrib
< bld
->num_attribs
; ++attrib
) {
189 unsigned mask
= bld
->mask
[attrib
];
190 unsigned mode
= bld
->mode
[attrib
];
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
];
199 res
= bld
->attribs_pre
[attrib
][chan
];
202 res
= lp_build_add(&bld
->base
, res
, dadx
);
205 res
= lp_build_add(&bld
->base
, res
, dady
);
207 bld
->attribs_pre
[attrib
][chan
] = res
;
209 if (mode
== TGSI_INTERPOLATE_PERSPECTIVE
) {
210 LLVMValueRef w
= bld
->pos
[3];
213 oow
= lp_build_rcp(&bld
->base
, w
);
214 res
= lp_build_mul(&bld
->base
, res
, oow
);
217 attrib_name(res
, attrib
, chan
, "");
219 bld
->attribs
[attrib
][chan
] = res
;
228 * Generate the position vectors.
230 * Parameter x0, y0 are the integer values with the quad upper left coordinates.
233 pos_init(struct lp_build_interp_soa_context
*bld
,
237 lp_build_name(x0
, "pos.x");
238 lp_build_name(y0
, "pos.y");
240 bld
->attribs
[0][0] = x0
;
241 bld
->attribs
[0][1] = y0
;
246 pos_update(struct lp_build_interp_soa_context
*bld
)
248 LLVMValueRef x
= bld
->attribs
[0][0];
249 LLVMValueRef y
= bld
->attribs
[0][1];
252 x
= lp_build_add(&bld
->base
, x
, lp_build_const_scalar(bld
->base
.type
, bld
->xstep
));
255 y
= lp_build_add(&bld
->base
, y
, lp_build_const_scalar(bld
->base
.type
, bld
->ystep
));
257 lp_build_name(x
, "pos.x");
258 lp_build_name(y
, "pos.y");
260 bld
->attribs
[0][0] = x
;
261 bld
->attribs
[0][1] = y
;
266 lp_build_interp_soa_init(struct lp_build_interp_soa_context
*bld
,
267 const struct tgsi_token
*tokens
,
268 LLVMBuilderRef builder
,
271 LLVMValueRef dadx_ptr
,
272 LLVMValueRef dady_ptr
,
278 struct tgsi_parse_context parse
;
279 struct tgsi_full_declaration
*decl
;
281 memset(bld
, 0, sizeof *bld
);
283 lp_build_context_init(&bld
->base
, builder
, type
);
285 /* For convenience */
286 bld
->pos
= bld
->attribs
[0];
287 bld
->inputs
= (const LLVMValueRef (*)[NUM_CHANNELS
]) bld
->attribs
[1];
290 bld
->num_attribs
= 1;
291 bld
->mask
[0] = TGSI_WRITEMASK_ZW
;
292 bld
->mode
[0] = TGSI_INTERPOLATE_LINEAR
;
295 tgsi_parse_init( &parse
, tokens
);
296 while( !tgsi_parse_end_of_tokens( &parse
) ) {
297 tgsi_parse_token( &parse
);
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
;
306 first
= decl
->Range
.First
;
307 last
= decl
->Range
.Last
;
308 mask
= decl
->Declaration
.UsageMask
;
310 for( attrib
= first
; attrib
<= last
; ++attrib
) {
311 bld
->mask
[1 + attrib
] = mask
;
312 bld
->mode
[1 + attrib
] = decl
->Declaration
.Interpolate
;
315 bld
->num_attribs
= MAX2(bld
->num_attribs
, 1 + last
+ 1);
319 case TGSI_TOKEN_TYPE_INSTRUCTION
:
320 case TGSI_TOKEN_TYPE_IMMEDIATE
:
327 tgsi_parse_free( &parse
);
329 coeffs_init(bld
, a0_ptr
, dadx_ptr
, dady_ptr
);
331 pos_init(bld
, x0
, y0
);
343 * Advance the position and inputs with the xstep and ystep.
346 lp_build_interp_soa_update(struct lp_build_interp_soa_context
*bld
)