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 * 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 unsigned mask = bld->mask[attrib];
104 unsigned mode = bld->mode[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 unsigned mask = bld->mask[attrib];
163 unsigned mode = bld->mode[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 (mode != 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 (mode == 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 unsigned mask = bld->mask[attrib];
215 unsigned mode = bld->mode[attrib];
217 if (mode != 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 //XXX bld->attribs_pre[attrib][chan] = res;
242 if (mode == TGSI_INTERPOLATE_PERSPECTIVE) {
243 LLVMValueRef w = bld->pos[3];
246 oow = lp_build_rcp(&bld->base, w);
247 res = lp_build_mul(&bld->base, res, oow);
250 attrib_name(res, attrib, chan, "");
252 bld->attribs[attrib][chan] = res;
261 * Generate the position vectors.
263 * Parameter x0, y0 are the integer values with the quad upper left coordinates.
266 pos_init(struct lp_build_interp_soa_context *bld,
270 lp_build_name(x0, "pos.x");
271 lp_build_name(y0, "pos.y");
273 bld->attribs[0][0] = x0;
274 bld->attribs[0][1] = y0;
279 * Update quad position values when moving to the next quad.
282 pos_update(struct lp_build_interp_soa_context *bld, int quad_index)
284 LLVMValueRef x = bld->attribs[0][0];
285 LLVMValueRef y = bld->attribs[0][1];
286 const int xstep = 2, ystep = 2;
288 if (quad_index == 1 || quad_index == 3) {
289 /* top-right or bottom-right quad in block */
290 /* build x += xstep */
291 x = lp_build_add(&bld->base, x,
292 lp_build_const_scalar(bld->base.type, xstep));
295 if (quad_index == 2) {
296 /* bottom-left quad in block */
297 /* build y += ystep */
298 y = lp_build_add(&bld->base, y,
299 lp_build_const_scalar(bld->base.type, ystep));
300 /* build x -= xstep */
301 x = lp_build_sub(&bld->base, x,
302 lp_build_const_scalar(bld->base.type, xstep));
305 lp_build_name(x, "pos.x");
306 lp_build_name(y, "pos.y");
308 bld->attribs[0][0] = x;
309 bld->attribs[0][1] = y;
314 * Initialize fragment shader input attribute info.
317 lp_build_interp_soa_init(struct lp_build_interp_soa_context *bld,
318 const struct tgsi_token *tokens,
319 LLVMBuilderRef builder,
322 LLVMValueRef dadx_ptr,
323 LLVMValueRef dady_ptr,
327 struct tgsi_parse_context parse;
328 struct tgsi_full_declaration *decl;
330 memset(bld, 0, sizeof *bld);
332 lp_build_context_init(&bld->base, builder, type);
334 /* For convenience */
335 bld->pos = bld->attribs[0];
336 bld->inputs = (const LLVMValueRef (*)[NUM_CHANNELS]) bld->attribs[1];
339 bld->num_attribs = 1;
340 bld->mask[0] = TGSI_WRITEMASK_ZW;
341 bld->mode[0] = TGSI_INTERPOLATE_LINEAR;
344 tgsi_parse_init( &parse, tokens );
345 while( !tgsi_parse_end_of_tokens( &parse ) ) {
346 tgsi_parse_token( &parse );
348 switch( parse.FullToken.Token.Type ) {
349 case TGSI_TOKEN_TYPE_DECLARATION:
350 decl = &parse.FullToken.FullDeclaration;
351 if( decl->Declaration.File == TGSI_FILE_INPUT ) {
352 unsigned first, last, mask;
355 first = decl->Range.First;
356 last = decl->Range.Last;
357 mask = decl->Declaration.UsageMask;
359 for( attrib = first; attrib <= last; ++attrib ) {
360 bld->mask[1 + attrib] = mask;
361 bld->mode[1 + attrib] = decl->Declaration.Interpolate;
364 bld->num_attribs = MAX2(bld->num_attribs, 1 + last + 1);
368 case TGSI_TOKEN_TYPE_INSTRUCTION:
369 case TGSI_TOKEN_TYPE_IMMEDIATE:
376 tgsi_parse_free( &parse );
378 coeffs_init(bld, a0_ptr, dadx_ptr, dady_ptr);
380 pos_init(bld, x0, y0);
387 * Advance the position and inputs to the given quad within the block.
390 lp_build_interp_soa_update(struct lp_build_interp_soa_context *bld,
393 assert(quad_index < 4);
395 pos_update(bld, quad_index);
397 attribs_update(bld, quad_index);