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 "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 "lp_bld_interp.h"
49 * The shader JIT function operates on blocks of quads.
50 * Each block has 2x2 quads and each quad has 2x2 pixels.
52 * We iterate over the quads in order 0, 1, 2, 3:
64 * Within each quad, we have four pixels which are represented in SOA
73 * So the green channel (for example) of the four pixels is stored in
74 * a single vector register: {g0, g1, g2, g3}.
79 attrib_name(LLVMValueRef val
, unsigned attrib
, unsigned chan
, const char *suffix
)
82 lp_build_name(val
, "pos.%c%s", "xyzw"[chan
], suffix
);
84 lp_build_name(val
, "input%u.%c%s", attrib
- 1, "xyzw"[chan
], suffix
);
89 * Initialize the bld->a0, dadx, dady fields. This involves fetching
90 * those values from the arrays which are passed into the JIT function.
93 coeffs_init(struct lp_build_interp_soa_context
*bld
,
95 LLVMValueRef dadx_ptr
,
96 LLVMValueRef dady_ptr
)
98 LLVMBuilderRef builder
= bld
->base
.builder
;
102 for(attrib
= 0; attrib
< bld
->num_attribs
; ++attrib
) {
103 const unsigned mask
= bld
->mask
[attrib
];
104 const unsigned interp
= bld
->interp
[attrib
];
105 for(chan
= 0; chan
< NUM_CHANNELS
; ++chan
) {
106 if(mask
& (1 << chan
)) {
107 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), attrib
*NUM_CHANNELS
+ chan
, 0);
108 LLVMValueRef a0
= NULL
;
109 LLVMValueRef dadx
= NULL
;
110 LLVMValueRef dady
= NULL
;
113 case TGSI_INTERPOLATE_PERSPECTIVE
:
116 case TGSI_INTERPOLATE_LINEAR
:
117 dadx
= LLVMBuildLoad(builder
, LLVMBuildGEP(builder
, dadx_ptr
, &index
, 1, ""), "");
118 dady
= LLVMBuildLoad(builder
, LLVMBuildGEP(builder
, dady_ptr
, &index
, 1, ""), "");
119 dadx
= lp_build_broadcast_scalar(&bld
->base
, dadx
);
120 dady
= lp_build_broadcast_scalar(&bld
->base
, dady
);
121 attrib_name(dadx
, attrib
, chan
, ".dadx");
122 attrib_name(dady
, attrib
, chan
, ".dady");
125 case TGSI_INTERPOLATE_CONSTANT
:
126 a0
= LLVMBuildLoad(builder
, LLVMBuildGEP(builder
, a0_ptr
, &index
, 1, ""), "");
127 a0
= lp_build_broadcast_scalar(&bld
->base
, a0
);
128 attrib_name(a0
, attrib
, chan
, ".a0");
136 bld
->a0
[attrib
][chan
] = a0
;
137 bld
->dadx
[attrib
][chan
] = dadx
;
138 bld
->dady
[attrib
][chan
] = dady
;
146 * Emit LLVM code to compute the fragment shader input attribute values.
147 * For example, for a color input, we'll compute red, green, blue and alpha
148 * values for the four pixels in a quad.
149 * Recall that we're operating on 4-element vectors so each arithmetic
150 * operation is operating on the four pixels in a quad.
153 attribs_init(struct lp_build_interp_soa_context
*bld
)
155 LLVMValueRef x
= bld
->pos
[0];
156 LLVMValueRef y
= bld
->pos
[1];
157 LLVMValueRef oow
= NULL
;
161 for(attrib
= 0; attrib
< bld
->num_attribs
; ++attrib
) {
162 const unsigned mask
= bld
->mask
[attrib
];
163 const unsigned interp
= bld
->interp
[attrib
];
164 for(chan
= 0; chan
< NUM_CHANNELS
; ++chan
) {
165 if(mask
& (1 << chan
)) {
166 LLVMValueRef a0
= bld
->a0
[attrib
][chan
];
167 LLVMValueRef dadx
= bld
->dadx
[attrib
][chan
];
168 LLVMValueRef dady
= bld
->dady
[attrib
][chan
];
173 if (interp
!= TGSI_INTERPOLATE_CONSTANT
) {
174 /* res = res + x * dadx */
175 res
= lp_build_add(&bld
->base
, res
, lp_build_mul(&bld
->base
, x
, dadx
));
176 /* res = res + y * dady */
177 res
= lp_build_add(&bld
->base
, res
, lp_build_mul(&bld
->base
, y
, dady
));
180 /* Keep the value of the attribue before perspective divide for faster updates */
181 bld
->attribs_pre
[attrib
][chan
] = res
;
183 if (interp
== TGSI_INTERPOLATE_PERSPECTIVE
) {
184 LLVMValueRef w
= bld
->pos
[3];
187 oow
= lp_build_rcp(&bld
->base
, w
);
188 res
= lp_build_mul(&bld
->base
, res
, oow
);
191 attrib_name(res
, attrib
, chan
, "");
193 bld
->attribs
[attrib
][chan
] = res
;
201 * Increment the shader input attribute values.
202 * This is called when we move from one quad to the next.
205 attribs_update(struct lp_build_interp_soa_context
*bld
, int quad_index
)
207 LLVMValueRef oow
= NULL
;
211 assert(quad_index
< 4);
213 for(attrib
= 0; attrib
< bld
->num_attribs
; ++attrib
) {
214 const unsigned mask
= bld
->mask
[attrib
];
215 const unsigned interp
= bld
->interp
[attrib
];
217 if (interp
!= TGSI_INTERPOLATE_CONSTANT
) {
218 for(chan
= 0; chan
< NUM_CHANNELS
; ++chan
) {
219 if(mask
& (1 << chan
)) {
220 LLVMValueRef dadx
= bld
->dadx
[attrib
][chan
];
221 LLVMValueRef dady
= bld
->dady
[attrib
][chan
];
224 res
= bld
->attribs_pre
[attrib
][chan
];
226 if (quad_index
== 1 || quad_index
== 3) {
227 /* top-right or bottom-right quad */
228 /* build res = res + dadx + dadx */
229 res
= lp_build_add(&bld
->base
, res
, dadx
);
230 res
= lp_build_add(&bld
->base
, res
, dadx
);
233 if (quad_index
== 2 || quad_index
== 3) {
234 /* bottom-left or bottom-right quad */
235 /* build res = res + dady + dady */
236 res
= lp_build_add(&bld
->base
, res
, dady
);
237 res
= lp_build_add(&bld
->base
, res
, dady
);
240 if (interp
== TGSI_INTERPOLATE_PERSPECTIVE
) {
241 LLVMValueRef w
= bld
->pos
[3];
244 oow
= lp_build_rcp(&bld
->base
, w
);
245 res
= lp_build_mul(&bld
->base
, res
, oow
);
248 attrib_name(res
, attrib
, chan
, "");
250 bld
->attribs
[attrib
][chan
] = res
;
259 * Generate the position vectors.
261 * Parameter x0, y0 are the integer values with the quad upper left coordinates.
264 pos_init(struct lp_build_interp_soa_context
*bld
,
268 lp_build_name(x0
, "pos.x");
269 lp_build_name(y0
, "pos.y");
271 bld
->attribs
[0][0] = x0
;
272 bld
->attribs
[0][1] = y0
;
277 * Update quad position values when moving to the next quad.
280 pos_update(struct lp_build_interp_soa_context
*bld
, int quad_index
)
282 LLVMValueRef x
= bld
->attribs
[0][0];
283 LLVMValueRef y
= bld
->attribs
[0][1];
284 const int xstep
= 2, ystep
= 2;
286 if (quad_index
== 1 || quad_index
== 3) {
287 /* top-right or bottom-right quad in block */
288 /* build x += xstep */
289 x
= lp_build_add(&bld
->base
, x
,
290 lp_build_const_vec(bld
->base
.type
, xstep
));
293 if (quad_index
== 2) {
294 /* bottom-left quad in block */
295 /* build y += ystep */
296 y
= lp_build_add(&bld
->base
, y
,
297 lp_build_const_vec(bld
->base
.type
, ystep
));
298 /* build x -= xstep */
299 x
= lp_build_sub(&bld
->base
, x
,
300 lp_build_const_vec(bld
->base
.type
, xstep
));
303 lp_build_name(x
, "pos.x");
304 lp_build_name(y
, "pos.y");
306 bld
->attribs
[0][0] = x
;
307 bld
->attribs
[0][1] = y
;
312 * Initialize fragment shader input attribute info.
315 lp_build_interp_soa_init(struct lp_build_interp_soa_context
*bld
,
316 const struct tgsi_token
*tokens
,
318 LLVMBuilderRef builder
,
321 LLVMValueRef dadx_ptr
,
322 LLVMValueRef dady_ptr
,
326 struct tgsi_parse_context parse
;
327 struct tgsi_full_declaration
*decl
;
329 memset(bld
, 0, sizeof *bld
);
331 lp_build_context_init(&bld
->base
, builder
, type
);
333 /* For convenience */
334 bld
->pos
= bld
->attribs
[0];
335 bld
->inputs
= (const LLVMValueRef (*)[NUM_CHANNELS
]) bld
->attribs
[1];
338 bld
->num_attribs
= 1;
339 bld
->mask
[0] = TGSI_WRITEMASK_ZW
;
340 bld
->interp
[0] = TGSI_INTERPOLATE_LINEAR
;
343 tgsi_parse_init( &parse
, tokens
);
344 while( !tgsi_parse_end_of_tokens( &parse
) ) {
345 tgsi_parse_token( &parse
);
347 switch( parse
.FullToken
.Token
.Type
) {
348 case TGSI_TOKEN_TYPE_DECLARATION
:
349 decl
= &parse
.FullToken
.FullDeclaration
;
350 if( decl
->Declaration
.File
== TGSI_FILE_INPUT
) {
351 unsigned first
, last
, mask
;
354 first
= decl
->Range
.First
;
355 last
= decl
->Range
.Last
;
356 mask
= decl
->Declaration
.UsageMask
;
358 for( attrib
= first
; attrib
<= last
; ++attrib
) {
359 bld
->mask
[1 + attrib
] = mask
;
361 /* XXX: have mesa set INTERP_CONSTANT in the fragment
364 if (decl
->Semantic
.Name
== TGSI_SEMANTIC_COLOR
&&
366 bld
->interp
[1 + attrib
] = TGSI_INTERPOLATE_CONSTANT
;
368 bld
->interp
[1 + attrib
] = decl
->Declaration
.Interpolate
;
371 bld
->num_attribs
= MAX2(bld
->num_attribs
, 1 + last
+ 1);
375 case TGSI_TOKEN_TYPE_INSTRUCTION
:
376 case TGSI_TOKEN_TYPE_IMMEDIATE
:
377 case TGSI_TOKEN_TYPE_PROPERTY
:
384 tgsi_parse_free( &parse
);
386 coeffs_init(bld
, a0_ptr
, dadx_ptr
, dady_ptr
);
388 pos_init(bld
, x0
, y0
);
395 * Advance the position and inputs to the given quad within the block.
398 lp_build_interp_soa_update(struct lp_build_interp_soa_context
*bld
,
401 assert(quad_index
< 4);
403 pos_update(bld
, quad_index
);
405 attribs_update(bld
, quad_index
);