blob: 6b090ff399be1d2fc49dbc6c5691a12f0f504a94 [file] [log] [blame]
Keith Whitwell15e75e02005-04-29 15:11:39 +00001/**************************************************************************
2 *
Keith Whitwell32ef6e72008-09-20 08:26:11 -07003 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
Keith Whitwell15e75e02005-04-29 15:11:39 +00004 * All Rights Reserved.
Brian Paul6947f852009-01-23 17:32:09 -07005 * Copyright 2009 VMware, Inc. All Rights Reserved.
Keith Whitwell15e75e02005-04-29 15:11:39 +00006 *
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:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
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.
26 *
27 **************************************************************************/
28
Keith Whitwell15e75e02005-04-29 15:11:39 +000029#include "glheader.h"
30#include "macros.h"
31#include "enums.h"
Brian Paul5b5c9312008-05-07 18:51:44 -060032#include "shader/program.h"
Brianc223c6b2007-07-04 13:15:20 -060033#include "shader/prog_parameter.h"
Keith Whitwell32ef6e72008-09-20 08:26:11 -070034#include "shader/prog_cache.h"
Brianc223c6b2007-07-04 13:15:20 -060035#include "shader/prog_instruction.h"
36#include "shader/prog_print.h"
37#include "shader/prog_statevars.h"
Brianfb3c41f2007-09-25 15:20:04 -060038#include "shader/programopt.h"
Keith Whitwell15e75e02005-04-29 15:11:39 +000039#include "texenvprogram.h"
40
Brian Paul5b5c9312008-05-07 18:51:44 -060041
Brian Paule9b34882008-12-31 11:54:02 -070042/*
43 * Note on texture units:
44 *
45 * The number of texture units supported by fixed-function fragment
46 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
47 * That's because there's a one-to-one correspondence between texture
48 * coordinates and samplers in fixed-function processing.
49 *
50 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
51 * sets of texcoords, so is fixed-function fragment processing.
52 *
53 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
54 */
55
56
Brian Paul5b5c9312008-05-07 18:51:44 -060057struct texenvprog_cache_item
58{
59 GLuint hash;
60 void *key;
61 struct gl_fragment_program *data;
62 struct texenvprog_cache_item *next;
63};
64
Eric Anholt9cea84b2009-07-16 18:41:03 -070065static GLboolean
66texenv_doing_secondary_color(GLcontext *ctx)
67{
68 if (ctx->Light.Enabled &&
69 (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR))
70 return GL_TRUE;
71
72 if (ctx->Fog.ColorSumEnabled)
73 return GL_TRUE;
74
75 return GL_FALSE;
76}
Brian Paul5b5c9312008-05-07 18:51:44 -060077
Brian Paule998c342006-10-29 21:17:18 +000078/**
Brian Paulb5e1a932008-09-25 18:40:16 -060079 * Up to nine instructions per tex unit, plus fog, specular color.
Brian Paule998c342006-10-29 21:17:18 +000080 */
Brian Paul0815ebc2009-01-02 16:32:26 -070081#define MAX_INSTRUCTIONS ((MAX_TEXTURE_COORD_UNITS * 9) + 12)
Keith Whitwell15e75e02005-04-29 15:11:39 +000082
Keith Whitwell269e3892005-05-12 10:28:43 +000083#define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
Keith Whitwell15e75e02005-04-29 15:11:39 +000084
Keith Whitwellce721142005-07-11 10:10:38 +000085struct mode_opt {
Brian Paul5d7b49f2005-11-19 23:12:20 +000086 GLuint Source:4;
87 GLuint Operand:3;
Keith Whitwellce721142005-07-11 10:10:38 +000088};
89
90struct state_key {
Keith Whitwell6280e332008-10-03 13:51:56 +010091 GLuint nr_enabled_units:8;
92 GLuint enabled_units:8;
Brian Paul5d7b49f2005-11-19 23:12:20 +000093 GLuint separate_specular:1;
94 GLuint fog_enabled:1;
95 GLuint fog_mode:2;
Keith Whitwell6280e332008-10-03 13:51:56 +010096 GLuint inputs_available:12;
Keith Whitwellce721142005-07-11 10:10:38 +000097
98 struct {
Brian Paul5d7b49f2005-11-19 23:12:20 +000099 GLuint enabled:1;
100 GLuint source_index:3; /* one of TEXTURE_1D/2D/3D/CUBE/RECT_INDEX */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +0200101 GLuint shadow:1;
Brian Paul5d7b49f2005-11-19 23:12:20 +0000102 GLuint ScaleShiftRGB:2;
103 GLuint ScaleShiftA:2;
Keith Whitwellce721142005-07-11 10:10:38 +0000104
Brian Paul6947f852009-01-23 17:32:09 -0700105 GLuint NumArgsRGB:3;
Roland Scheidegger114152e2009-03-12 15:01:16 +0100106 GLuint ModeRGB:5;
Brian Paulbd9b2be2009-03-08 17:58:54 -0600107 struct mode_opt OptRGB[MAX_COMBINER_TERMS];
Keith Whitwellce721142005-07-11 10:10:38 +0000108
Brian Paul6947f852009-01-23 17:32:09 -0700109 GLuint NumArgsA:3;
Roland Scheidegger114152e2009-03-12 15:01:16 +0100110 GLuint ModeA:5;
Brian Paulbd9b2be2009-03-08 17:58:54 -0600111 struct mode_opt OptA[MAX_COMBINER_TERMS];
Keith Whitwellce721142005-07-11 10:10:38 +0000112 } unit[8];
113};
114
115#define FOG_LINEAR 0
116#define FOG_EXP 1
117#define FOG_EXP2 2
118#define FOG_UNKNOWN 3
119
120static GLuint translate_fog_mode( GLenum mode )
121{
122 switch (mode) {
123 case GL_LINEAR: return FOG_LINEAR;
124 case GL_EXP: return FOG_EXP;
125 case GL_EXP2: return FOG_EXP2;
126 default: return FOG_UNKNOWN;
127 }
128}
129
130#define OPR_SRC_COLOR 0
131#define OPR_ONE_MINUS_SRC_COLOR 1
132#define OPR_SRC_ALPHA 2
133#define OPR_ONE_MINUS_SRC_ALPHA 3
134#define OPR_ZERO 4
135#define OPR_ONE 5
136#define OPR_UNKNOWN 7
137
138static GLuint translate_operand( GLenum operand )
139{
140 switch (operand) {
141 case GL_SRC_COLOR: return OPR_SRC_COLOR;
142 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
143 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
144 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
145 case GL_ZERO: return OPR_ZERO;
146 case GL_ONE: return OPR_ONE;
Brian Paul6947f852009-01-23 17:32:09 -0700147 default:
148 assert(0);
149 return OPR_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000150 }
151}
152
153#define SRC_TEXTURE 0
154#define SRC_TEXTURE0 1
155#define SRC_TEXTURE1 2
156#define SRC_TEXTURE2 3
157#define SRC_TEXTURE3 4
158#define SRC_TEXTURE4 5
159#define SRC_TEXTURE5 6
160#define SRC_TEXTURE6 7
161#define SRC_TEXTURE7 8
162#define SRC_CONSTANT 9
163#define SRC_PRIMARY_COLOR 10
164#define SRC_PREVIOUS 11
Brian Paul6947f852009-01-23 17:32:09 -0700165#define SRC_ZERO 12
Keith Whitwellce721142005-07-11 10:10:38 +0000166#define SRC_UNKNOWN 15
167
168static GLuint translate_source( GLenum src )
169{
170 switch (src) {
171 case GL_TEXTURE: return SRC_TEXTURE;
172 case GL_TEXTURE0:
173 case GL_TEXTURE1:
174 case GL_TEXTURE2:
175 case GL_TEXTURE3:
176 case GL_TEXTURE4:
177 case GL_TEXTURE5:
178 case GL_TEXTURE6:
179 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
180 case GL_CONSTANT: return SRC_CONSTANT;
181 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
182 case GL_PREVIOUS: return SRC_PREVIOUS;
Brian Paul6947f852009-01-23 17:32:09 -0700183 case GL_ZERO:
184 return SRC_ZERO;
185 default:
186 assert(0);
187 return SRC_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000188 }
189}
190
Brian Paul6947f852009-01-23 17:32:09 -0700191#define MODE_REPLACE 0 /* r = a0 */
192#define MODE_MODULATE 1 /* r = a0 * a1 */
193#define MODE_ADD 2 /* r = a0 + a1 */
194#define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
195#define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
196#define MODE_SUBTRACT 5 /* r = a0 - a1 */
197#define MODE_DOT3_RGB 6 /* r = a0 . a1 */
198#define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
199#define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
200#define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
201#define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
202#define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
203#define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
204#define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
205#define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
Roland Scheidegger114152e2009-03-12 15:01:16 +0100206#define MODE_BUMP_ENVMAP_ATI 15 /* special */
207#define MODE_UNKNOWN 16
Keith Whitwellce721142005-07-11 10:10:38 +0000208
Brian Paul6947f852009-01-23 17:32:09 -0700209/**
210 * Translate GL combiner state into a MODE_x value
211 */
212static GLuint translate_mode( GLenum envMode, GLenum mode )
Keith Whitwellce721142005-07-11 10:10:38 +0000213{
214 switch (mode) {
215 case GL_REPLACE: return MODE_REPLACE;
216 case GL_MODULATE: return MODE_MODULATE;
Brian Paul6947f852009-01-23 17:32:09 -0700217 case GL_ADD:
218 if (envMode == GL_COMBINE4_NV)
219 return MODE_ADD_PRODUCTS;
220 else
221 return MODE_ADD;
222 case GL_ADD_SIGNED:
223 if (envMode == GL_COMBINE4_NV)
224 return MODE_ADD_PRODUCTS_SIGNED;
225 else
226 return MODE_ADD_SIGNED;
Keith Whitwellce721142005-07-11 10:10:38 +0000227 case GL_INTERPOLATE: return MODE_INTERPOLATE;
228 case GL_SUBTRACT: return MODE_SUBTRACT;
229 case GL_DOT3_RGB: return MODE_DOT3_RGB;
230 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
231 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
232 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
233 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
234 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
235 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
Roland Scheidegger114152e2009-03-12 15:01:16 +0100236 case GL_BUMP_ENVMAP_ATI: return MODE_BUMP_ENVMAP_ATI;
Brian Paul6947f852009-01-23 17:32:09 -0700237 default:
238 assert(0);
239 return MODE_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000240 }
241}
242
243#define TEXTURE_UNKNOWN_INDEX 7
Brian Paule00ac112005-09-15 05:00:45 +0000244static GLuint translate_tex_src_bit( GLbitfield bit )
Keith Whitwellce721142005-07-11 10:10:38 +0000245{
Brian Paul1519b932008-12-17 13:17:15 -0700246 /* make sure number of switch cases is correct */
247 assert(NUM_TEXTURE_TARGETS == 7);
Keith Whitwellce721142005-07-11 10:10:38 +0000248 switch (bit) {
249 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
250 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
251 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
252 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
253 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
Brian Paul6947f852009-01-23 17:32:09 -0700254 case TEXTURE_1D_ARRAY_BIT: return TEXTURE_1D_ARRAY_INDEX;
255 case TEXTURE_2D_ARRAY_BIT: return TEXTURE_2D_ARRAY_INDEX;
256 default:
257 assert(0);
258 return TEXTURE_UNKNOWN_INDEX;
Keith Whitwellce721142005-07-11 10:10:38 +0000259 }
260}
261
Keith Whitwell1680ef82008-10-03 17:30:59 +0100262#define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
263#define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
264
Brian Paul239617f2008-10-07 11:22:47 -0600265/**
266 * Identify all possible varying inputs. The fragment program will
Keith Whitwell1680ef82008-10-03 17:30:59 +0100267 * never reference non-varying inputs, but will track them via state
268 * constants instead.
269 *
270 * This function figures out all the inputs that the fragment program
271 * has access to. The bitmask is later reduced to just those which
272 * are actually referenced.
273 */
Brian Paul239617f2008-10-07 11:22:47 -0600274static GLbitfield get_fp_input_mask( GLcontext *ctx )
Keith Whitwell1680ef82008-10-03 17:30:59 +0100275{
Eric Anholte5f63c42009-06-02 07:47:20 -0700276 /* _NEW_PROGRAM */
Brian Paulf0b07942008-12-17 14:04:03 -0700277 const GLboolean vertexShader = (ctx->Shader.CurrentProgram &&
278 ctx->Shader.CurrentProgram->VertexProgram);
279 const GLboolean vertexProgram = ctx->VertexProgram._Enabled;
Brian Paul239617f2008-10-07 11:22:47 -0600280 GLbitfield fp_inputs = 0x0;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100281
Brian Paul6d4d51d2008-10-10 13:39:14 -0600282 if (ctx->VertexProgram._Overriden) {
283 /* Somebody's messing with the vertex program and we don't have
284 * a clue what's happening. Assume that it could be producing
285 * all possible outputs.
286 */
287 fp_inputs = ~0;
288 }
289 else if (ctx->RenderMode == GL_FEEDBACK) {
Eric Anholte5f63c42009-06-02 07:47:20 -0700290 /* _NEW_RENDERMODE */
Brian Paul6d4d51d2008-10-10 13:39:14 -0600291 fp_inputs = (FRAG_BIT_COL0 | FRAG_BIT_TEX0);
292 }
Brian Paulf0b07942008-12-17 14:04:03 -0700293 else if (!(vertexProgram || vertexShader) ||
Brian Pauld7296a12008-12-17 11:29:06 -0700294 !ctx->VertexProgram._Current) {
Brian Paulf0b07942008-12-17 14:04:03 -0700295 /* Fixed function vertex logic */
Eric Anholte5f63c42009-06-02 07:47:20 -0700296 /* _NEW_ARRAY */
Brian Paul239617f2008-10-07 11:22:47 -0600297 GLbitfield varying_inputs = ctx->varying_vp_inputs;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100298
Keith Whitwell97e63432008-10-20 13:03:45 +0100299 /* These get generated in the setup routine regardless of the
300 * vertex program:
301 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700302 /* _NEW_POINT */
Keith Whitwell97e63432008-10-20 13:03:45 +0100303 if (ctx->Point.PointSprite)
304 varying_inputs |= FRAG_BITS_TEX_ANY;
305
Keith Whitwell1680ef82008-10-03 17:30:59 +0100306 /* First look at what values may be computed by the generated
307 * vertex program:
308 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700309 /* _NEW_LIGHT */
Keith Whitwell1680ef82008-10-03 17:30:59 +0100310 if (ctx->Light.Enabled) {
311 fp_inputs |= FRAG_BIT_COL0;
312
Eric Anholt9cea84b2009-07-16 18:41:03 -0700313 if (texenv_doing_secondary_color(ctx))
Keith Whitwell1680ef82008-10-03 17:30:59 +0100314 fp_inputs |= FRAG_BIT_COL1;
315 }
316
Eric Anholte5f63c42009-06-02 07:47:20 -0700317 /* _NEW_TEXTURE */
Keith Whitwell1680ef82008-10-03 17:30:59 +0100318 fp_inputs |= (ctx->Texture._TexGenEnabled |
319 ctx->Texture._TexMatEnabled) << FRAG_ATTRIB_TEX0;
320
321 /* Then look at what might be varying as a result of enabled
322 * arrays, etc:
323 */
324 if (varying_inputs & VERT_BIT_COLOR0) fp_inputs |= FRAG_BIT_COL0;
325 if (varying_inputs & VERT_BIT_COLOR1) fp_inputs |= FRAG_BIT_COL1;
326
327 fp_inputs |= (((varying_inputs & VERT_BIT_TEX_ANY) >> VERT_ATTRIB_TEX0)
328 << FRAG_ATTRIB_TEX0);
329
330 }
331 else {
332 /* calculate from vp->outputs */
Brian Paul2389c052008-12-17 19:01:34 -0700333 struct gl_vertex_program *vprog;
334 GLbitfield vp_outputs;
335
336 /* Choose GLSL vertex shader over ARB vertex program. Need this
337 * since vertex shader state validation comes after fragment state
338 * validation (see additional comments in state.c).
339 */
340 if (vertexShader)
341 vprog = ctx->Shader.CurrentProgram->VertexProgram;
342 else
343 vprog = ctx->VertexProgram._Current;
344
345 vp_outputs = vprog->Base.OutputsWritten;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100346
Keith Whitwell97e63432008-10-20 13:03:45 +0100347 /* These get generated in the setup routine regardless of the
348 * vertex program:
349 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700350 /* _NEW_POINT */
Keith Whitwell97e63432008-10-20 13:03:45 +0100351 if (ctx->Point.PointSprite)
352 vp_outputs |= FRAG_BITS_TEX_ANY;
353
Keith Whitwell1680ef82008-10-03 17:30:59 +0100354 if (vp_outputs & (1 << VERT_RESULT_COL0)) fp_inputs |= FRAG_BIT_COL0;
355 if (vp_outputs & (1 << VERT_RESULT_COL1)) fp_inputs |= FRAG_BIT_COL1;
356
Keith Whitwell0370d6b2008-10-04 12:41:56 +0100357 fp_inputs |= (((vp_outputs & VERT_RESULT_TEX_ANY) >> VERT_RESULT_TEX0)
358 << FRAG_ATTRIB_TEX0);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100359 }
360
361 return fp_inputs;
362}
363
364
Brian Paul22db5352005-11-19 23:45:10 +0000365/**
366 * Examine current texture environment state and generate a unique
367 * key to identify it.
368 */
Keith Whitwell8065c122006-05-22 16:09:27 +0000369static void make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000370{
Keith Whitwellce721142005-07-11 10:10:38 +0000371 GLuint i, j;
Brian Paul239617f2008-10-07 11:22:47 -0600372 GLbitfield inputs_referenced = FRAG_BIT_COL0;
373 GLbitfield inputs_available = get_fp_input_mask( ctx );
Keith Whitwell1680ef82008-10-03 17:30:59 +0100374
Keith Whitwell8065c122006-05-22 16:09:27 +0000375 memset(key, 0, sizeof(*key));
376
Eric Anholte5f63c42009-06-02 07:47:20 -0700377 /* _NEW_TEXTURE */
Brian Paule9b34882008-12-31 11:54:02 -0700378 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000379 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800380 GLenum format;
Roland Scheideggerbf4a0fa2008-02-15 17:26:06 +0100381
382 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
Keith Whitwellce721142005-07-11 10:10:38 +0000383 continue;
384
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800385 format = texUnit->_Current->Image[0][texUnit->_Current->BaseLevel]->_BaseFormat;
386
Keith Whitwellce721142005-07-11 10:10:38 +0000387 key->unit[i].enabled = 1;
388 key->enabled_units |= (1<<i);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100389 key->nr_enabled_units = i+1;
390 inputs_referenced |= FRAG_BIT_TEX(i);
Keith Whitwellce721142005-07-11 10:10:38 +0000391
392 key->unit[i].source_index =
393 translate_tex_src_bit(texUnit->_ReallyEnabled);
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800394 key->unit[i].shadow = ((texUnit->_Current->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
395 ((format == GL_DEPTH_COMPONENT) ||
396 (format == GL_DEPTH_STENCIL_EXT)));
Keith Whitwellce721142005-07-11 10:10:38 +0000397
398 key->unit[i].NumArgsRGB = texUnit->_CurrentCombine->_NumArgsRGB;
399 key->unit[i].NumArgsA = texUnit->_CurrentCombine->_NumArgsA;
400
401 key->unit[i].ModeRGB =
Brian Paul6947f852009-01-23 17:32:09 -0700402 translate_mode(texUnit->EnvMode, texUnit->_CurrentCombine->ModeRGB);
Keith Whitwellce721142005-07-11 10:10:38 +0000403 key->unit[i].ModeA =
Brian Paul6947f852009-01-23 17:32:09 -0700404 translate_mode(texUnit->EnvMode, texUnit->_CurrentCombine->ModeA);
Roland Scheidegger114152e2009-03-12 15:01:16 +0100405
Keith Whitwellce721142005-07-11 10:10:38 +0000406 key->unit[i].ScaleShiftRGB = texUnit->_CurrentCombine->ScaleShiftRGB;
Keith Whitwell64da1612006-05-22 14:30:58 +0000407 key->unit[i].ScaleShiftA = texUnit->_CurrentCombine->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000408
Brian Paulbd9b2be2009-03-08 17:58:54 -0600409 for (j = 0; j < MAX_COMBINER_TERMS; j++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000410 key->unit[i].OptRGB[j].Operand =
411 translate_operand(texUnit->_CurrentCombine->OperandRGB[j]);
412 key->unit[i].OptA[j].Operand =
413 translate_operand(texUnit->_CurrentCombine->OperandA[j]);
414 key->unit[i].OptRGB[j].Source =
415 translate_source(texUnit->_CurrentCombine->SourceRGB[j]);
416 key->unit[i].OptA[j].Source =
417 translate_source(texUnit->_CurrentCombine->SourceA[j]);
418 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100419
420 if (key->unit[i].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
421 /* requires some special translation */
422 key->unit[i].NumArgsRGB = 2;
423 key->unit[i].ScaleShiftRGB = 0;
424 key->unit[i].OptRGB[0].Operand = OPR_SRC_COLOR;
425 key->unit[i].OptRGB[0].Source = SRC_TEXTURE;
426 key->unit[i].OptRGB[1].Operand = OPR_SRC_COLOR;
427 key->unit[i].OptRGB[1].Source = texUnit->BumpTarget - GL_TEXTURE0 + SRC_TEXTURE0;
428 }
Keith Whitwellce721142005-07-11 10:10:38 +0000429 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100430
Eric Anholt9cea84b2009-07-16 18:41:03 -0700431 /* _NEW_LIGHT | _NEW_FOG */
432 if (texenv_doing_secondary_color(ctx)) {
Keith Whitwellce721142005-07-11 10:10:38 +0000433 key->separate_specular = 1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100434 inputs_referenced |= FRAG_BIT_COL1;
435 }
Keith Whitwellce721142005-07-11 10:10:38 +0000436
Eric Anholte5f63c42009-06-02 07:47:20 -0700437 /* _NEW_FOG */
Keith Whitwellce721142005-07-11 10:10:38 +0000438 if (ctx->Fog.Enabled) {
439 key->fog_enabled = 1;
440 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100441 inputs_referenced |= FRAG_BIT_FOGC; /* maybe */
Keith Whitwellce721142005-07-11 10:10:38 +0000442 }
Keith Whitwell1680ef82008-10-03 17:30:59 +0100443
444 key->inputs_available = (inputs_available & inputs_referenced);
Keith Whitwellce721142005-07-11 10:10:38 +0000445}
446
Brian Paul239617f2008-10-07 11:22:47 -0600447/**
448 * Use uregs to represent registers internally, translate to Mesa's
Keith Whitwell15e75e02005-04-29 15:11:39 +0000449 * expected formats on emit.
450 *
451 * NOTE: These are passed by value extensively in this file rather
452 * than as usual by pointer reference. If this disturbs you, try
453 * remembering they are just 32bits in size.
454 *
455 * GCC is smart enough to deal with these dword-sized structures in
456 * much the same way as if I had defined them as dwords and was using
457 * macros to access and set the fields. This is much nicer and easier
458 * to evolve.
459 */
460struct ureg {
461 GLuint file:4;
462 GLuint idx:8;
463 GLuint negatebase:1;
464 GLuint abs:1;
465 GLuint negateabs:1;
466 GLuint swz:12;
467 GLuint pad:5;
468};
469
Brian Paul13abf912006-04-13 19:17:13 +0000470static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000471 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000472 ~0,
473 0,
474 0,
475 0,
476 0,
477 0
478};
479
Keith Whitwell15e75e02005-04-29 15:11:39 +0000480
Brian Paul239617f2008-10-07 11:22:47 -0600481/** State used to build the fragment program:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000482 */
483struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000484 struct gl_fragment_program *program;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000485 GLcontext *ctx;
Keith Whitwellce721142005-07-11 10:10:38 +0000486 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000487
Brian Paul239617f2008-10-07 11:22:47 -0600488 GLbitfield alu_temps; /**< Track texture indirections, see spec. */
489 GLbitfield temps_output; /**< Track texture indirections, see spec. */
490 GLbitfield temp_in_use; /**< Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000491 GLboolean error;
492
Brian Paule9b34882008-12-31 11:54:02 -0700493 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000494 /* Reg containing each texture unit's sampled texture color,
495 * else undef.
496 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000497
Roland Scheidegger114152e2009-03-12 15:01:16 +0100498 struct ureg texcoord_tex[MAX_TEXTURE_COORD_UNITS];
499 /* Reg containing texcoord for a texture unit,
500 * needed for bump mapping, else undef.
501 */
502
Brian Paul239617f2008-10-07 11:22:47 -0600503 struct ureg src_previous; /**< Reg containing color from previous
Keith Whitwell15e75e02005-04-29 15:11:39 +0000504 * stage. May need to be decl'd.
505 */
506
Brian Paul239617f2008-10-07 11:22:47 -0600507 GLuint last_tex_stage; /**< Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000508
509 struct ureg half;
510 struct ureg one;
511 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000512};
513
514
515
516static struct ureg make_ureg(GLuint file, GLuint idx)
517{
518 struct ureg reg;
519 reg.file = file;
520 reg.idx = idx;
521 reg.negatebase = 0;
522 reg.abs = 0;
523 reg.negateabs = 0;
524 reg.swz = SWIZZLE_NOOP;
525 reg.pad = 0;
526 return reg;
527}
528
529static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
530{
531 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
532 GET_SWZ(reg.swz, y),
533 GET_SWZ(reg.swz, z),
534 GET_SWZ(reg.swz, w));
535
536 return reg;
537}
538
539static struct ureg swizzle1( struct ureg reg, int x )
540{
541 return swizzle(reg, x, x, x, x);
542}
543
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000544static struct ureg negate( struct ureg reg )
545{
546 reg.negatebase ^= 1;
547 return reg;
548}
549
Keith Whitwell15e75e02005-04-29 15:11:39 +0000550static GLboolean is_undef( struct ureg reg )
551{
Alan Hourihane8d972652006-08-10 13:12:00 +0000552 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000553}
554
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000555
Keith Whitwell15e75e02005-04-29 15:11:39 +0000556static struct ureg get_temp( struct texenv_fragment_program *p )
557{
Brian Paul13abf912006-04-13 19:17:13 +0000558 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000559
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000560 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000561 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000562 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000563
564 /* Then any unused temporary:
565 */
566 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000567 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000568
Keith Whitwell15e75e02005-04-29 15:11:39 +0000569 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000570 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000571 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000572 }
573
Brian Paul13abf912006-04-13 19:17:13 +0000574 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000575 p->program->Base.NumTemporaries = bit;
576
Keith Whitwell93cd9232005-05-11 08:30:23 +0000577 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000578 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
579}
580
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000581static struct ureg get_tex_temp( struct texenv_fragment_program *p )
582{
583 int bit;
584
Brian Pauld01269a2008-09-25 18:27:22 -0600585 /* First try to find available temp not previously used (to avoid
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000586 * starting a new texture indirection). According to the spec, the
587 * ~p->temps_output isn't necessary, but will keep it there for
588 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000589 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000590 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000591
592 /* Then any unused temporary:
593 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000594 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000595 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000596
597 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000598 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000599 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000600 }
601
Brian Paul13abf912006-04-13 19:17:13 +0000602 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000603 p->program->Base.NumTemporaries = bit;
604
Keith Whitwell93cd9232005-05-11 08:30:23 +0000605 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000606 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
607}
608
Keith Whitwell15e75e02005-04-29 15:11:39 +0000609
Brian Paul5620c202008-09-26 11:18:06 -0600610/** Mark a temp reg as being no longer allocatable. */
611static void reserve_temp( struct texenv_fragment_program *p, struct ureg r )
612{
613 if (r.file == PROGRAM_TEMPORARY)
614 p->temps_output |= (1 << r.idx);
615}
616
617
Brian93fef222007-10-29 12:25:46 -0600618static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000619{
Brian93fef222007-10-29 12:25:46 -0600620 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000621
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000622 /* KW: To support tex_env_crossbar, don't release the registers in
623 * temps_output.
624 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000625 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000626 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000627 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000628 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000629}
630
631
Brian9fe3e2e2007-02-23 11:44:14 -0700632static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000633 GLint s0,
634 GLint s1,
635 GLint s2,
636 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700637 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000638{
Brian9fe3e2e2007-02-23 11:44:14 -0700639 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000640 GLuint idx;
641 tokens[0] = s0;
642 tokens[1] = s1;
643 tokens[2] = s2;
644 tokens[3] = s3;
645 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000646 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000647 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000648}
649
Keith Whitwell47b29f52005-05-04 11:44:44 +0000650
Brian9fe3e2e2007-02-23 11:44:14 -0700651#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
652#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
653#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
654#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000655
Keith Whitwell1680ef82008-10-03 17:30:59 +0100656static GLuint frag_to_vert_attrib( GLuint attrib )
657{
658 switch (attrib) {
659 case FRAG_ATTRIB_COL0: return VERT_ATTRIB_COLOR0;
660 case FRAG_ATTRIB_COL1: return VERT_ATTRIB_COLOR1;
661 default:
662 assert(attrib >= FRAG_ATTRIB_TEX0);
663 assert(attrib <= FRAG_ATTRIB_TEX7);
664 return attrib - FRAG_ATTRIB_TEX0 + VERT_ATTRIB_TEX0;
665 }
666}
667
Keith Whitwell47b29f52005-05-04 11:44:44 +0000668
669static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
670{
Keith Whitwell1680ef82008-10-03 17:30:59 +0100671 if (p->state->inputs_available & (1<<input)) {
672 p->program->Base.InputsRead |= (1 << input);
673 return make_ureg(PROGRAM_INPUT, input);
674 }
675 else {
676 GLuint idx = frag_to_vert_attrib( input );
677 return register_param3( p, STATE_INTERNAL, STATE_CURRENT_ATTRIB, idx );
678 }
Keith Whitwell47b29f52005-05-04 11:44:44 +0000679}
680
681
Brian Paul7e807512005-11-05 17:10:45 +0000682static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000683 struct ureg ureg )
684{
685 reg->File = ureg.file;
686 reg->Index = ureg.idx;
687 reg->Swizzle = ureg.swz;
Brian Paul7db7ff82009-04-14 22:14:30 -0600688 reg->Negate = ureg.negatebase ? NEGATE_XYZW : NEGATE_NONE;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000689 reg->Abs = ureg.abs;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000690}
691
Brian Paul7e807512005-11-05 17:10:45 +0000692static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000693 struct ureg ureg, GLuint mask )
694{
695 dst->File = ureg.file;
696 dst->Index = ureg.idx;
697 dst->WriteMask = mask;
Brianc9d495c2007-10-23 10:55:24 -0600698 dst->CondMask = COND_TR; /* always pass cond test */
699 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000700}
701
Brian Paul7e807512005-11-05 17:10:45 +0000702static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000703emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000704 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000705 struct ureg dest,
706 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000707 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000708 struct ureg src0,
709 struct ureg src1,
710 struct ureg src2 )
711{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000712 GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000713 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Brian2a8e9bb2007-10-23 10:24:53 -0600714
715 assert(nr < MAX_INSTRUCTIONS);
716
Brian Pauld6272e02006-10-29 18:03:16 +0000717 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000718 inst->Opcode = op;
719
Keith Whitwell47b29f52005-05-04 11:44:44 +0000720 emit_arg( &inst->SrcReg[0], src0 );
721 emit_arg( &inst->SrcReg[1], src1 );
722 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000723
Brian Paule31ac052005-11-20 17:52:40 +0000724 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000725
726 emit_dst( &inst->DstReg, dest, mask );
727
Brian Paul5620c202008-09-26 11:18:06 -0600728#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000729 /* Accounting for indirection tracking:
730 */
731 if (dest.file == PROGRAM_TEMPORARY)
732 p->temps_output |= 1 << dest.idx;
Brian Paul5620c202008-09-26 11:18:06 -0600733#endif
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000734
Keith Whitwell15e75e02005-04-29 15:11:39 +0000735 return inst;
736}
737
738
739static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000740 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000741 struct ureg dest,
742 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000743 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000744 struct ureg src0,
745 struct ureg src1,
746 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000747{
748 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
749
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000750 /* Accounting for indirection tracking:
751 */
752 if (src0.file == PROGRAM_TEMPORARY)
753 p->alu_temps |= 1 << src0.idx;
754
755 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
756 p->alu_temps |= 1 << src1.idx;
757
758 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
759 p->alu_temps |= 1 << src2.idx;
760
761 if (dest.file == PROGRAM_TEMPORARY)
762 p->alu_temps |= 1 << dest.idx;
763
Brian21f99792007-01-09 11:00:21 -0700764 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000765 return dest;
766}
767
768static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000769 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000770 struct ureg dest,
771 GLuint destmask,
772 GLuint tex_unit,
773 GLuint tex_idx,
Brian Paul44e018c2009-02-20 13:42:08 -0700774 GLuint tex_shadow,
Keith Whitwellce721142005-07-11 10:10:38 +0000775 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000776{
Brian Paul7e807512005-11-05 17:10:45 +0000777 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000778 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000779 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000780 coord, /* arg 0? */
781 undef,
782 undef);
783
Brian Paul7e807512005-11-05 17:10:45 +0000784 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000785 inst->TexSrcUnit = tex_unit;
Brian Paul44e018c2009-02-20 13:42:08 -0700786 inst->TexShadow = tex_shadow;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000787
Brian21f99792007-01-09 11:00:21 -0700788 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000789
Brian Paul5620c202008-09-26 11:18:06 -0600790 /* Accounting for indirection tracking:
791 */
792 reserve_temp(p, dest);
793
Roland Scheidegger114152e2009-03-12 15:01:16 +0100794#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000795 /* Is this a texture indirection?
796 */
797 if ((coord.file == PROGRAM_TEMPORARY &&
798 (p->temps_output & (1<<coord.idx))) ||
799 (dest.file == PROGRAM_TEMPORARY &&
800 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700801 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000802 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000803 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000804 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000805 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100806#endif
Keith Whitwell15e75e02005-04-29 15:11:39 +0000807
808 return dest;
809}
810
811
Keith Whitwell47b29f52005-05-04 11:44:44 +0000812static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000813 GLfloat s0,
814 GLfloat s1,
815 GLfloat s2,
816 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000817{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000818 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700819 GLuint idx, swizzle;
Brian Pauld01269a2008-09-25 18:27:22 -0600820 struct ureg r;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000821 values[0] = s0;
822 values[1] = s1;
823 values[2] = s2;
824 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700825 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
826 &swizzle );
Brian Pauld01269a2008-09-25 18:27:22 -0600827 r = make_ureg(PROGRAM_CONSTANT, idx);
828 r.swz = swizzle;
829 return r;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000830}
831
Keith Whitwelle4902422005-05-11 15:16:35 +0000832#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000833#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
834#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
835#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000836
837
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000838static struct ureg get_one( struct texenv_fragment_program *p )
839{
840 if (is_undef(p->one))
841 p->one = register_scalar_const(p, 1.0);
842 return p->one;
843}
844
845static struct ureg get_half( struct texenv_fragment_program *p )
846{
847 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000848 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000849 return p->half;
850}
851
852static struct ureg get_zero( struct texenv_fragment_program *p )
853{
854 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000855 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000856 return p->zero;
857}
858
Keith Whitwell15e75e02005-04-29 15:11:39 +0000859
Keith Whitwell15e75e02005-04-29 15:11:39 +0000860static void program_error( struct texenv_fragment_program *p, const char *msg )
861{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000862 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000863 p->error = 1;
864}
865
Keith Whitwell15e75e02005-04-29 15:11:39 +0000866static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000867 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000868{
869 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000870 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000871 assert(!is_undef(p->src_texture[unit]));
872 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000873
Keith Whitwellce721142005-07-11 10:10:38 +0000874 case SRC_TEXTURE0:
875 case SRC_TEXTURE1:
876 case SRC_TEXTURE2:
877 case SRC_TEXTURE3:
878 case SRC_TEXTURE4:
879 case SRC_TEXTURE5:
880 case SRC_TEXTURE6:
881 case SRC_TEXTURE7:
882 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
883 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000884
Keith Whitwellce721142005-07-11 10:10:38 +0000885 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000886 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000887
Keith Whitwellce721142005-07-11 10:10:38 +0000888 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000889 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000890
Brian Paul6947f852009-01-23 17:32:09 -0700891 case SRC_ZERO:
892 return get_zero(p);
893
Keith Whitwellce721142005-07-11 10:10:38 +0000894 case SRC_PREVIOUS:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000895 if (is_undef(p->src_previous))
896 return register_input(p, FRAG_ATTRIB_COL0);
897 else
898 return p->src_previous;
Brian Paul6947f852009-01-23 17:32:09 -0700899
900 default:
901 assert(0);
José Fonsecac6af9b22009-06-15 19:20:25 +0100902 return undef;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000903 }
904}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000905
Keith Whitwell15e75e02005-04-29 15:11:39 +0000906static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000907 GLuint mask,
908 GLuint unit,
909 GLuint source,
910 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000911{
912 struct ureg arg, src, one;
913
914 src = get_source(p, source, unit);
915
916 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000917 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000918 /* Get unused tmp,
919 * Emit tmp = 1.0 - arg.xyzw
920 */
921 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000922 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000923 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000924
Keith Whitwellce721142005-07-11 10:10:38 +0000925 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000926 if (mask == WRITEMASK_W)
927 return src;
928 else
Brian Paul22db5352005-11-19 23:45:10 +0000929 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000930 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000931 /* Get unused tmp,
932 * Emit tmp = 1.0 - arg.wwww
933 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000934 arg = get_temp(p);
935 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000936 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000937 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000938 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000939 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000940 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000941 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000942 case OPR_SRC_COLOR:
Brian Paul6947f852009-01-23 17:32:09 -0700943 return src;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000944 default:
Brian Paul6947f852009-01-23 17:32:09 -0700945 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000946 return src;
947 }
948}
949
Keith Whitwellce721142005-07-11 10:10:38 +0000950static GLboolean args_match( struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000951{
Brian Paul22db5352005-11-19 23:45:10 +0000952 GLuint i, nr = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000953
954 for (i = 0 ; i < nr ; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000955 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000956 return GL_FALSE;
957
Keith Whitwellce721142005-07-11 10:10:38 +0000958 switch(key->unit[unit].OptA[i].Operand) {
959 case OPR_SRC_ALPHA:
960 switch(key->unit[unit].OptRGB[i].Operand) {
961 case OPR_SRC_COLOR:
962 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000963 break;
964 default:
965 return GL_FALSE;
966 }
967 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000968 case OPR_ONE_MINUS_SRC_ALPHA:
969 switch(key->unit[unit].OptRGB[i].Operand) {
970 case OPR_ONE_MINUS_SRC_COLOR:
971 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000972 break;
973 default:
974 return GL_FALSE;
975 }
976 break;
977 default:
978 return GL_FALSE; /* impossible */
979 }
980 }
981
982 return GL_TRUE;
983}
984
Keith Whitwell15e75e02005-04-29 15:11:39 +0000985static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000986 struct ureg dest,
987 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000988 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000989 GLuint unit,
990 GLuint nr,
991 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +0000992 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000993{
Brian Paulbd9b2be2009-03-08 17:58:54 -0600994 struct ureg src[MAX_COMBINER_TERMS];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000995 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +0000996 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000997
Brian Paulbd9b2be2009-03-08 17:58:54 -0600998 assert(nr <= MAX_COMBINER_TERMS);
Brian Paul6947f852009-01-23 17:32:09 -0700999
Brian Paul00630842005-12-12 15:27:55 +00001000 tmp = undef; /* silence warning (bug 5318) */
1001
Keith Whitwell15e75e02005-04-29 15:11:39 +00001002 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +00001003 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001004
1005 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +00001006 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001007 if (mask == WRITEMASK_XYZW && !saturate)
1008 return src[0];
1009 else
Brian Paul7e807512005-11-05 17:10:45 +00001010 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001011 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +00001012 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001013 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001014 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00001015 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001016 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001017 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001018 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001019 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +00001020 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001021 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +00001022 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +00001023 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
1024 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001025 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +00001026 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001027 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
1028 */
Brian Paul7e807512005-11-05 17:10:45 +00001029 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001030
Keith Whitwellce721142005-07-11 10:10:38 +00001031 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +00001032 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001033
Keith Whitwellce721142005-07-11 10:10:38 +00001034 case MODE_DOT3_RGBA:
1035 case MODE_DOT3_RGBA_EXT:
1036 case MODE_DOT3_RGB_EXT:
1037 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001038 struct ureg tmp0 = get_temp( p );
1039 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +00001040 struct ureg neg1 = register_scalar_const(p, -1);
1041 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001042
1043 /* tmp0 = 2*src0 - 1
1044 * tmp1 = 2*src1 - 1
1045 *
1046 * dst = tmp0 dot3 tmp1
1047 */
Brian Paul7e807512005-11-05 17:10:45 +00001048 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001049 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001050
Brian Paulaa206952005-09-16 18:14:24 +00001051 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001052 tmp1 = tmp0;
1053 else
Brian Paul7e807512005-11-05 17:10:45 +00001054 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001055 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +00001056 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001057 return dest;
1058 }
Keith Whitwellce721142005-07-11 10:10:38 +00001059 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001060 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001061 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001062 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +00001063 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001064 /* Arg0 * Arg2 + Arg1 - 0.5 */
1065 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001066 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +00001067 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
1068 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001069 return dest;
1070 }
Keith Whitwellce721142005-07-11 10:10:38 +00001071 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001072 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001073 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001074 return dest;
Brian Paul6947f852009-01-23 17:32:09 -07001075 case MODE_ADD_PRODUCTS:
1076 /* Arg0 * Arg1 + Arg2 * Arg3 */
1077 {
1078 struct ureg tmp0 = get_temp(p);
1079 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1080 emit_arith( p, OPCODE_MAD, dest, mask, saturate, src[2], src[3], tmp0 );
1081 }
1082 return dest;
1083 case MODE_ADD_PRODUCTS_SIGNED:
1084 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1085 {
1086 struct ureg tmp0 = get_temp(p);
1087 half = get_half(p);
1088 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1089 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[2], src[3], tmp0 );
1090 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1091 }
1092 return dest;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001093 case MODE_BUMP_ENVMAP_ATI:
1094 /* special - not handled here */
1095 assert(0);
1096 return src[0];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001097 default:
Brian Paul6947f852009-01-23 17:32:09 -07001098 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001099 return src[0];
1100 }
1101}
1102
Keith Whitwell15e75e02005-04-29 15:11:39 +00001103
Brian Paul22db5352005-11-19 23:45:10 +00001104/**
1105 * Generate instructions for one texture unit's env/combiner mode.
1106 */
1107static struct ureg
1108emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001109{
Keith Whitwellce721142005-07-11 10:10:38 +00001110 struct state_key *key = p->state;
Brian Paul22db5352005-11-19 23:45:10 +00001111 GLboolean saturate = (unit < p->last_tex_stage);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001112 GLuint rgb_shift, alpha_shift;
1113 struct ureg out, shift;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001114 struct ureg dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001115
Keith Whitwellce721142005-07-11 10:10:38 +00001116 if (!key->unit[unit].enabled) {
1117 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001118 }
Roland Scheidegger114152e2009-03-12 15:01:16 +01001119 if (key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1120 /* this isn't really a env stage delivering a color and handled elsewhere */
1121 return get_source(p, SRC_PREVIOUS, 0);
1122 }
Keith Whitwellce721142005-07-11 10:10:38 +00001123
1124 switch (key->unit[unit].ModeRGB) {
1125 case MODE_DOT3_RGB_EXT:
1126 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001127 rgb_shift = 0;
1128 break;
Keith Whitwellce721142005-07-11 10:10:38 +00001129 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001130 alpha_shift = 0;
1131 rgb_shift = 0;
1132 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001133 default:
Keith Whitwellce721142005-07-11 10:10:38 +00001134 rgb_shift = key->unit[unit].ScaleShiftRGB;
1135 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001136 break;
1137 }
Keith Whitwellce721142005-07-11 10:10:38 +00001138
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001139 /* If this is the very last calculation, emit direct to output reg:
1140 */
Keith Whitwellce721142005-07-11 10:10:38 +00001141 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001142 unit != p->last_tex_stage ||
1143 alpha_shift ||
1144 rgb_shift)
1145 dest = get_temp( p );
1146 else
Brian Paul8d475822009-02-28 11:49:46 -07001147 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLOR);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001148
1149 /* Emit the RGB and A combine ops
1150 */
Keith Whitwellce721142005-07-11 10:10:38 +00001151 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
1152 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001153 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1154 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001155 key->unit[unit].NumArgsRGB,
1156 key->unit[unit].ModeRGB,
1157 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001158 }
Keith Whitwellce721142005-07-11 10:10:38 +00001159 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +02001160 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001161
1162 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1163 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001164 key->unit[unit].NumArgsRGB,
1165 key->unit[unit].ModeRGB,
1166 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001167 }
1168 else {
1169 /* Need to do something to stop from re-emitting identical
1170 * argument calculations here:
1171 */
1172 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
1173 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001174 key->unit[unit].NumArgsRGB,
1175 key->unit[unit].ModeRGB,
1176 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001177 out = emit_combine( p, dest, WRITEMASK_W, saturate,
1178 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001179 key->unit[unit].NumArgsA,
1180 key->unit[unit].ModeA,
1181 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001182 }
1183
1184 /* Deal with the final shift:
1185 */
1186 if (alpha_shift || rgb_shift) {
1187 if (rgb_shift == alpha_shift) {
José Fonseca452a5922008-05-31 18:14:09 +09001188 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001189 }
1190 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001191 shift = register_const4f(p,
José Fonseca452a5922008-05-31 18:14:09 +09001192 (GLfloat)(1<<rgb_shift),
1193 (GLfloat)(1<<rgb_shift),
1194 (GLfloat)(1<<rgb_shift),
1195 (GLfloat)(1<<alpha_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001196 }
Brian Paul7e807512005-11-05 17:10:45 +00001197 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001198 saturate, out, shift, undef );
1199 }
1200 else
1201 return out;
1202}
1203
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001204
Brian Paul22db5352005-11-19 23:45:10 +00001205/**
1206 * Generate instruction for getting a texture source term.
1207 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001208static void load_texture( struct texenv_fragment_program *p, GLuint unit )
1209{
1210 if (is_undef(p->src_texture[unit])) {
Brian Paul44e018c2009-02-20 13:42:08 -07001211 GLuint texTarget = p->state->unit[unit].source_index;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001212 struct ureg texcoord;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001213 struct ureg tmp = get_tex_temp( p );
1214
Roland Scheidegger114152e2009-03-12 15:01:16 +01001215 if (is_undef(p->texcoord_tex[unit])) {
1216 texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
1217 }
1218 else {
1219 /* might want to reuse this reg for tex output actually */
1220 texcoord = p->texcoord_tex[unit];
1221 }
1222
Brian Paul44e018c2009-02-20 13:42:08 -07001223 if (texTarget == TEXTURE_UNKNOWN_INDEX)
Brian Paulbfb5ea32005-07-19 18:20:04 +00001224 program_error(p, "TexSrcBit");
Keith Whitwellce721142005-07-11 10:10:38 +00001225
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001226 /* TODO: Use D0_MASK_XY where possible.
1227 */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +02001228 if (p->state->unit[unit].enabled) {
Brian Paul44e018c2009-02-20 13:42:08 -07001229 GLboolean shadow = GL_FALSE;
1230
1231 if (p->state->unit[unit].shadow) {
1232 p->program->Base.ShadowSamplers |= 1 << unit;
1233 shadow = GL_TRUE;
1234 }
1235
Aapo Tahkolab8915342006-03-28 10:26:34 +00001236 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1237 tmp, WRITEMASK_XYZW,
Brian Paul44e018c2009-02-20 13:42:08 -07001238 unit, texTarget, shadow,
1239 texcoord );
Brian9b7e5a52007-12-14 11:16:49 -07001240
1241 p->program->Base.SamplersUsed |= (1 << unit);
Brian1fe385f2007-12-14 11:42:28 -07001242 /* This identity mapping should already be in place
1243 * (see _mesa_init_program_struct()) but let's be safe.
1244 */
1245 p->program->Base.SamplerUnits[unit] = unit;
Brian9b7e5a52007-12-14 11:16:49 -07001246 }
1247 else
Aapo Tahkolab8915342006-03-28 10:26:34 +00001248 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001249 }
1250}
1251
Keith Whitwell241b6b72005-05-23 09:50:34 +00001252static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +00001253 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001254{
1255 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +00001256 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001257 load_texture(p, unit);
1258 break;
1259
Keith Whitwellce721142005-07-11 10:10:38 +00001260 case SRC_TEXTURE0:
1261 case SRC_TEXTURE1:
1262 case SRC_TEXTURE2:
1263 case SRC_TEXTURE3:
1264 case SRC_TEXTURE4:
1265 case SRC_TEXTURE5:
1266 case SRC_TEXTURE6:
1267 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +00001268 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001269 break;
1270
1271 default:
Brian Paul6947f852009-01-23 17:32:09 -07001272 /* not a texture src - do nothing */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001273 break;
1274 }
Keith Whitwell241b6b72005-05-23 09:50:34 +00001275
1276 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001277}
1278
Brian Paul22db5352005-11-19 23:45:10 +00001279
1280/**
1281 * Generate instructions for loading all texture source terms.
1282 */
1283static GLboolean
1284load_texunit_sources( struct texenv_fragment_program *p, int unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001285{
Keith Whitwellce721142005-07-11 10:10:38 +00001286 struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +00001287 GLuint i;
1288
1289 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001290 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001291 }
Aapo Tahkolab8915342006-03-28 10:26:34 +00001292
1293 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1294 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1295 }
1296
Keith Whitwell241b6b72005-05-23 09:50:34 +00001297 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001298}
1299
Roland Scheidegger114152e2009-03-12 15:01:16 +01001300/**
1301 * Generate instructions for loading bump map textures.
1302 */
1303static GLboolean
1304load_texunit_bumpmap( struct texenv_fragment_program *p, int unit )
1305{
1306 struct state_key *key = p->state;
1307 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1308 struct ureg texcDst, bumpMapRes;
1309 struct ureg constdudvcolor = register_const4f(p, 0.0, 0.0, 0.0, 1.0);
1310 struct ureg texcSrc = register_input(p, FRAG_ATTRIB_TEX0 + bumpedUnitNr);
1311 struct ureg rotMat0 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_0, unit );
1312 struct ureg rotMat1 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_1, unit );
1313
1314 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1315
1316 bumpMapRes = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1317 texcDst = get_tex_temp( p );
1318 p->texcoord_tex[bumpedUnitNr] = texcDst;
1319
1320 /* apply rot matrix and add coords to be available in next phase */
1321 /* dest = (Arg0.xxxx * rotMat0 + Arg1) + (Arg0.yyyy * rotMat1) */
1322 /* note only 2 coords are affected the rest are left unchanged (mul by 0) */
1323 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1324 swizzle1(bumpMapRes, SWIZZLE_X), rotMat0, texcSrc );
1325 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1326 swizzle1(bumpMapRes, SWIZZLE_Y), rotMat1, texcDst );
1327
1328 /* move 0,0,0,1 into bumpmap src if someone (crossbar) is foolish
1329 enough to access this later, should optimize away */
1330 emit_arith( p, OPCODE_MOV, bumpMapRes, WRITEMASK_XYZW, 0, constdudvcolor, undef, undef );
1331
1332 return GL_TRUE;
1333}
Brian Paul22db5352005-11-19 23:45:10 +00001334
1335/**
1336 * Generate a new fragment program which implements the context's
1337 * current texture env/combine mode.
1338 */
1339static void
Brian Paule998c342006-10-29 21:17:18 +00001340create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001341 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001342{
Brian Paule998c342006-10-29 21:17:18 +00001343 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001344 struct texenv_fragment_program p;
1345 GLuint unit;
1346 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001347
Keith Whitwelle4902422005-05-11 15:16:35 +00001348 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +00001349 p.ctx = ctx;
Keith Whitwellce721142005-07-11 10:10:38 +00001350 p.state = key;
1351 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001352
Brian Paule998c342006-10-29 21:17:18 +00001353 /* During code generation, use locally-allocated instruction buffer,
1354 * then alloc dynamic storage below.
1355 */
1356 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001357 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001358 p.program->Base.NumTexIndirections = 1;
Brian21f99792007-01-09 11:00:21 -07001359 p.program->Base.NumTexInstructions = 0;
1360 p.program->Base.NumAluInstructions = 0;
Brian1240eb22007-03-22 08:50:20 -06001361 p.program->Base.String = NULL;
Keith Whitwell9ca88152005-05-10 09:56:02 +00001362 p.program->Base.NumInstructions =
Brian Paule998c342006-10-29 21:17:18 +00001363 p.program->Base.NumTemporaries =
1364 p.program->Base.NumParameters =
1365 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001366 p.program->Base.Parameters = _mesa_new_parameter_list();
Keith Whitwelldbeea252005-05-10 13:57:50 +00001367
Brian Paulde997602005-11-12 17:53:14 +00001368 p.program->Base.InputsRead = 0;
Brian Paul8d475822009-02-28 11:49:46 -07001369 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLOR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001370
Roland Scheidegger114152e2009-03-12 15:01:16 +01001371 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001372 p.src_texture[unit] = undef;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001373 p.texcoord_tex[unit] = undef;
1374 }
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001375
Keith Whitwell47b29f52005-05-04 11:44:44 +00001376 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001377 p.half = undef;
1378 p.zero = undef;
1379 p.one = undef;
1380
Keith Whitwell15e75e02005-04-29 15:11:39 +00001381 p.last_tex_stage = 0;
Brian93fef222007-10-29 12:25:46 -06001382 release_temps(ctx, &p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001383
Keith Whitwellce721142005-07-11 10:10:38 +00001384 if (key->enabled_units) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001385 GLboolean needbumpstage = GL_FALSE;
1386 /* Zeroth pass - bump map textures first */
1387 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
1388 if (key->unit[unit].enabled && key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1389 needbumpstage = GL_TRUE;
1390 load_texunit_bumpmap( &p, unit );
1391 }
1392 if (needbumpstage)
1393 p.program->Base.NumTexIndirections++;
1394
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001395 /* First pass - to support texture_env_crossbar, first identify
1396 * all referenced texture sources and emit texld instructions
1397 * for each:
1398 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001399 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001400 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001401 load_texunit_sources( &p, unit );
1402 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001403 }
1404
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001405 /* Second pass - emit combine instructions to build final color:
1406 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001407 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001408 if (key->enabled_units & (1<<unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001409 p.src_previous = emit_texenv( &p, unit );
Brian Paul5620c202008-09-26 11:18:06 -06001410 reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
Brian93fef222007-10-29 12:25:46 -06001411 release_temps(ctx, &p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001412 }
1413 }
1414
Keith Whitwellce721142005-07-11 10:10:38 +00001415 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul8d475822009-02-28 11:49:46 -07001416 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLOR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001417
Keith Whitwellce721142005-07-11 10:10:38 +00001418 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001419 /* Emit specular add.
1420 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001421 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001422 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1423 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001424 }
Brian Paulaa206952005-09-16 18:14:24 +00001425 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001426 /* Will wind up in here if no texture enabled or a couple of
1427 * other scenarios (GL_REPLACE for instance).
1428 */
Brian Paul7e807512005-11-05 17:10:45 +00001429 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001430 }
1431
Keith Whitwell47b29f52005-05-04 11:44:44 +00001432 /* Finish up:
1433 */
Brian Paul7e807512005-11-05 17:10:45 +00001434 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001435
Keith Whitwellce721142005-07-11 10:10:38 +00001436 if (key->fog_enabled) {
1437 /* Pull fog mode from GLcontext, the value in the state key is
1438 * a reduced value and not what is expected in FogOption
1439 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001440 p.program->FogOption = ctx->Fog.Mode;
Brian19d77d62007-09-18 19:29:26 -06001441 p.program->Base.InputsRead |= FRAG_BIT_FOGC; /* XXX new */
Keith Whitwellce721142005-07-11 10:10:38 +00001442 } else
Keith Whitwell276330b2005-05-09 17:58:13 +00001443 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001444
Brian21f99792007-01-09 11:00:21 -07001445 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001446 program_error(&p, "Exceeded max nr indirect texture lookups");
1447
Brian21f99792007-01-09 11:00:21 -07001448 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001449 program_error(&p, "Exceeded max TEX instructions");
1450
Brian21f99792007-01-09 11:00:21 -07001451 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001452 program_error(&p, "Exceeded max ALU instructions");
1453
Brian Paul5d7b49f2005-11-19 23:12:20 +00001454 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001455
Brian Paule998c342006-10-29 21:17:18 +00001456 /* Allocate final instruction array */
Brianc81cce72007-09-25 15:18:51 -06001457 p.program->Base.Instructions
1458 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1459 if (!p.program->Base.Instructions) {
Brian Paule998c342006-10-29 21:17:18 +00001460 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1461 "generating tex env program");
1462 return;
1463 }
Brianc81cce72007-09-25 15:18:51 -06001464 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1465 p.program->Base.NumInstructions);
1466
1467 if (p.program->FogOption) {
1468 _mesa_append_fog_code(ctx, p.program);
1469 p.program->FogOption = GL_NONE;
1470 }
1471
Brian Paule998c342006-10-29 21:17:18 +00001472
Keith Whitwelldbeea252005-05-10 13:57:50 +00001473 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001474 */
Brian Paule998c342006-10-29 21:17:18 +00001475 if (ctx->Driver.ProgramStringNotify) {
1476 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1477 &p.program->Base );
1478 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001479
Brian Paule998c342006-10-29 21:17:18 +00001480 if (DISASSEM) {
1481 _mesa_print_program(&p.program->Base);
1482 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001483 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001484}
1485
Brian Paul5d7b49f2005-11-19 23:12:20 +00001486
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001487/**
1488 * Return a fragment program which implements the current
1489 * fixed-function texture, fog and color-sum operations.
1490 */
1491struct gl_fragment_program *
1492_mesa_get_fixed_func_fragment_program(GLcontext *ctx)
Keith Whitwellce721142005-07-11 10:10:38 +00001493{
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001494 struct gl_fragment_program *prog;
1495 struct state_key key;
1496
1497 make_state_key(ctx, &key);
1498
1499 prog = (struct gl_fragment_program *)
1500 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1501 &key, sizeof(key));
Keith Whitwellce721142005-07-11 10:10:38 +00001502
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001503 if (!prog) {
1504 prog = (struct gl_fragment_program *)
1505 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1506
1507 create_new_program(ctx, &key, prog);
1508
1509 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1510 &key, sizeof(key), &prog->Base);
Keith Whitwellce721142005-07-11 10:10:38 +00001511 }
1512
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001513 return prog;
Keith Whitwellce721142005-07-11 10:10:38 +00001514}