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_scan.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
= bld
->base
.undef
;
109 LLVMValueRef dadx
= bld
->base
.undef
;
110 LLVMValueRef dady
= bld
->base
.undef
;
113 case LP_INTERP_PERSPECTIVE
:
116 case LP_INTERP_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 LP_INTERP_CONSTANT
:
126 case LP_INTERP_FACING
:
127 a0
= LLVMBuildLoad(builder
, LLVMBuildGEP(builder
, a0_ptr
, &index
, 1, ""), "");
128 a0
= lp_build_broadcast_scalar(&bld
->base
, a0
);
129 attrib_name(a0
, attrib
, chan
, ".a0");
132 case LP_INTERP_POSITION
:
133 /* Nothing to do as the position coeffs are already setup in slot 0 */
141 bld
->a0
[attrib
][chan
] = a0
;
142 bld
->dadx
[attrib
][chan
] = dadx
;
143 bld
->dady
[attrib
][chan
] = dady
;
151 * Emit LLVM code to compute the fragment shader input attribute values.
152 * For example, for a color input, we'll compute red, green, blue and alpha
153 * values for the four pixels in a quad.
154 * Recall that we're operating on 4-element vectors so each arithmetic
155 * operation is operating on the four pixels in a quad.
158 attribs_init(struct lp_build_interp_soa_context
*bld
)
160 LLVMValueRef x
= bld
->pos
[0];
161 LLVMValueRef y
= bld
->pos
[1];
162 LLVMValueRef oow
= NULL
;
166 for(attrib
= 0; attrib
< bld
->num_attribs
; ++attrib
) {
167 const unsigned mask
= bld
->mask
[attrib
];
168 const unsigned interp
= bld
->interp
[attrib
];
169 for(chan
= 0; chan
< NUM_CHANNELS
; ++chan
) {
170 if(mask
& (1 << chan
)) {
171 if (interp
== LP_INTERP_POSITION
) {
173 bld
->attribs
[attrib
][chan
] = bld
->attribs
[0][chan
];
176 LLVMValueRef a0
= bld
->a0
[attrib
][chan
];
177 LLVMValueRef dadx
= bld
->dadx
[attrib
][chan
];
178 LLVMValueRef dady
= bld
->dady
[attrib
][chan
];
183 if (interp
!= LP_INTERP_CONSTANT
&&
184 interp
!= LP_INTERP_FACING
) {
185 /* res = res + x * dadx */
186 res
= lp_build_add(&bld
->base
, res
, lp_build_mul(&bld
->base
, x
, dadx
));
187 /* res = res + y * dady */
188 res
= lp_build_add(&bld
->base
, res
, lp_build_mul(&bld
->base
, y
, dady
));
191 /* Keep the value of the attribute before perspective divide
192 * for faster updates.
194 bld
->attribs_pre
[attrib
][chan
] = res
;
196 if (interp
== LP_INTERP_PERSPECTIVE
) {
197 LLVMValueRef w
= bld
->pos
[3];
199 assert(bld
->mask
[0] & TGSI_WRITEMASK_W
);
201 oow
= lp_build_rcp(&bld
->base
, w
);
202 res
= lp_build_mul(&bld
->base
, res
, oow
);
205 attrib_name(res
, attrib
, chan
, "");
207 bld
->attribs
[attrib
][chan
] = res
;
216 * Increment the shader input attribute values.
217 * This is called when we move from one quad to the next.
220 attribs_update(struct lp_build_interp_soa_context
*bld
, int quad_index
)
222 LLVMValueRef oow
= NULL
;
226 assert(quad_index
< 4);
228 for(attrib
= 0; attrib
< bld
->num_attribs
; ++attrib
) {
229 const unsigned mask
= bld
->mask
[attrib
];
230 const unsigned interp
= bld
->interp
[attrib
];
232 if (interp
!= LP_INTERP_CONSTANT
&&
233 interp
!= LP_INTERP_FACING
) {
234 for(chan
= 0; chan
< NUM_CHANNELS
; ++chan
) {
235 if(mask
& (1 << chan
)) {
236 if (interp
== LP_INTERP_POSITION
) {
238 bld
->attribs
[attrib
][chan
] = bld
->attribs
[0][chan
];
241 LLVMValueRef dadx
= bld
->dadx
[attrib
][chan
];
242 LLVMValueRef dady
= bld
->dady
[attrib
][chan
];
245 res
= bld
->attribs_pre
[attrib
][chan
];
247 if (quad_index
== 1 || quad_index
== 3) {
248 /* top-right or bottom-right quad */
249 /* build res = res + dadx + dadx */
250 res
= lp_build_add(&bld
->base
, res
, dadx
);
251 res
= lp_build_add(&bld
->base
, res
, dadx
);
254 if (quad_index
== 2 || quad_index
== 3) {
255 /* bottom-left or bottom-right quad */
256 /* build res = res + dady + dady */
257 res
= lp_build_add(&bld
->base
, res
, dady
);
258 res
= lp_build_add(&bld
->base
, res
, dady
);
261 if (interp
== LP_INTERP_PERSPECTIVE
) {
262 LLVMValueRef w
= bld
->pos
[3];
264 assert(bld
->mask
[0] & TGSI_WRITEMASK_W
);
266 oow
= lp_build_rcp(&bld
->base
, w
);
267 res
= lp_build_mul(&bld
->base
, res
, oow
);
270 attrib_name(res
, attrib
, chan
, "");
272 bld
->attribs
[attrib
][chan
] = res
;
282 * Generate the position vectors.
284 * Parameter x0, y0 are the integer values with the quad upper left coordinates.
287 pos_init(struct lp_build_interp_soa_context
*bld
,
291 lp_build_name(x0
, "pos.x");
292 lp_build_name(y0
, "pos.y");
294 bld
->attribs
[0][0] = x0
;
295 bld
->attribs
[0][1] = y0
;
300 * Update quad position values when moving to the next quad.
303 pos_update(struct lp_build_interp_soa_context
*bld
, int quad_index
)
305 LLVMValueRef x
= bld
->attribs
[0][0];
306 LLVMValueRef y
= bld
->attribs
[0][1];
307 const int xstep
= 2, ystep
= 2;
309 if (quad_index
== 1 || quad_index
== 3) {
310 /* top-right or bottom-right quad in block */
311 /* build x += xstep */
312 x
= lp_build_add(&bld
->base
, x
,
313 lp_build_const_vec(bld
->base
.type
, xstep
));
316 if (quad_index
== 2) {
317 /* bottom-left quad in block */
318 /* build y += ystep */
319 y
= lp_build_add(&bld
->base
, y
,
320 lp_build_const_vec(bld
->base
.type
, ystep
));
321 /* build x -= xstep */
322 x
= lp_build_sub(&bld
->base
, x
,
323 lp_build_const_vec(bld
->base
.type
, xstep
));
326 lp_build_name(x
, "pos.x");
327 lp_build_name(y
, "pos.y");
329 bld
->attribs
[0][0] = x
;
330 bld
->attribs
[0][1] = y
;
335 * Initialize fragment shader input attribute info.
338 lp_build_interp_soa_init(struct lp_build_interp_soa_context
*bld
,
340 const struct lp_shader_input
*inputs
,
341 LLVMBuilderRef builder
,
344 LLVMValueRef dadx_ptr
,
345 LLVMValueRef dady_ptr
,
352 memset(bld
, 0, sizeof *bld
);
354 lp_build_context_init(&bld
->base
, builder
, type
);
356 /* For convenience */
357 bld
->pos
= bld
->attribs
[0];
358 bld
->inputs
= (const LLVMValueRef (*)[NUM_CHANNELS
]) bld
->attribs
[1];
361 bld
->num_attribs
= 1;
362 bld
->mask
[0] = TGSI_WRITEMASK_ZW
;
363 bld
->interp
[0] = LP_INTERP_LINEAR
;
366 for (attrib
= 0; attrib
< num_inputs
; ++attrib
) {
367 bld
->mask
[1 + attrib
] = inputs
[attrib
].usage_mask
;
368 bld
->interp
[1 + attrib
] = inputs
[attrib
].interp
;
370 bld
->num_attribs
= 1 + num_inputs
;
372 /* Ensure all masked out input channels have a valid value */
373 for (attrib
= 0; attrib
< bld
->num_attribs
; ++attrib
) {
374 for (chan
= 0; chan
< NUM_CHANNELS
; ++chan
) {
375 bld
->attribs
[attrib
][chan
] = bld
->base
.undef
;
379 coeffs_init(bld
, a0_ptr
, dadx_ptr
, dady_ptr
);
381 pos_init(bld
, x0
, y0
);
388 * Advance the position and inputs to the given quad within the block.
391 lp_build_interp_soa_update(struct lp_build_interp_soa_context
*bld
,
394 assert(quad_index
< 4);
396 pos_update(bld
, quad_index
);
398 attribs_update(bld
, quad_index
);