blob: 7efb10c5a0f36e7ab7e007ce27466b70d7539094 [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"
Brian Paul25e5a6f2009-09-01 16:05:07 -060030#include "imports.h"
Brian Paul5b5c9312008-05-07 18:51:44 -060031#include "shader/program.h"
Brianc223c6b2007-07-04 13:15:20 -060032#include "shader/prog_parameter.h"
Keith Whitwell32ef6e72008-09-20 08:26:11 -070033#include "shader/prog_cache.h"
Brianc223c6b2007-07-04 13:15:20 -060034#include "shader/prog_instruction.h"
35#include "shader/prog_print.h"
36#include "shader/prog_statevars.h"
Brianfb3c41f2007-09-25 15:20:04 -060037#include "shader/programopt.h"
Keith Whitwell15e75e02005-04-29 15:11:39 +000038#include "texenvprogram.h"
39
Brian Paul5b5c9312008-05-07 18:51:44 -060040
Brian Paule9b34882008-12-31 11:54:02 -070041/*
42 * Note on texture units:
43 *
44 * The number of texture units supported by fixed-function fragment
45 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
46 * That's because there's a one-to-one correspondence between texture
47 * coordinates and samplers in fixed-function processing.
48 *
49 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
50 * sets of texcoords, so is fixed-function fragment processing.
51 *
52 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
53 */
54
55
Brian Paul5b5c9312008-05-07 18:51:44 -060056struct texenvprog_cache_item
57{
58 GLuint hash;
59 void *key;
60 struct gl_fragment_program *data;
61 struct texenvprog_cache_item *next;
62};
63
Eric Anholt9cea84b2009-07-16 18:41:03 -070064static GLboolean
65texenv_doing_secondary_color(GLcontext *ctx)
66{
67 if (ctx->Light.Enabled &&
68 (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR))
69 return GL_TRUE;
70
71 if (ctx->Fog.ColorSumEnabled)
72 return GL_TRUE;
73
74 return GL_FALSE;
75}
Brian Paul5b5c9312008-05-07 18:51:44 -060076
Brian Paule998c342006-10-29 21:17:18 +000077/**
Brian Paulb5e1a932008-09-25 18:40:16 -060078 * Up to nine instructions per tex unit, plus fog, specular color.
Brian Paule998c342006-10-29 21:17:18 +000079 */
Brian Paul0815ebc2009-01-02 16:32:26 -070080#define MAX_INSTRUCTIONS ((MAX_TEXTURE_COORD_UNITS * 9) + 12)
Keith Whitwell15e75e02005-04-29 15:11:39 +000081
Keith Whitwell269e3892005-05-12 10:28:43 +000082#define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
Keith Whitwell15e75e02005-04-29 15:11:39 +000083
Keith Whitwellce721142005-07-11 10:10:38 +000084struct mode_opt {
Brian Paulf337e2c2009-09-01 15:49:55 -060085 GLuint Source:4; /**< SRC_x */
86 GLuint Operand:3; /**< OPR_x */
Keith Whitwellce721142005-07-11 10:10:38 +000087};
88
89struct state_key {
Keith Whitwell6280e332008-10-03 13:51:56 +010090 GLuint nr_enabled_units:8;
91 GLuint enabled_units:8;
Brian Paul5d7b49f2005-11-19 23:12:20 +000092 GLuint separate_specular:1;
93 GLuint fog_enabled:1;
Brian Paul9ed03152009-09-01 15:57:36 -060094 GLuint fog_mode:2; /**< FOG_x */
Keith Whitwell6280e332008-10-03 13:51:56 +010095 GLuint inputs_available:12;
Keith Whitwellce721142005-07-11 10:10:38 +000096
97 struct {
Brian Paul5d7b49f2005-11-19 23:12:20 +000098 GLuint enabled:1;
Brian Paul9ed03152009-09-01 15:57:36 -060099 GLuint source_index:3; /**< TEXTURE_x_INDEX */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +0200100 GLuint shadow:1;
Brian Paul5d7b49f2005-11-19 23:12:20 +0000101 GLuint ScaleShiftRGB:2;
102 GLuint ScaleShiftA:2;
Keith Whitwellce721142005-07-11 10:10:38 +0000103
Brian Paul9ed03152009-09-01 15:57:36 -0600104 GLuint NumArgsRGB:3; /**< up to MAX_COMBINER_TERMS */
Brian Paulf337e2c2009-09-01 15:49:55 -0600105 GLuint ModeRGB:5; /**< MODE_x */
Brian Paulbd9b2be2009-03-08 17:58:54 -0600106 struct mode_opt OptRGB[MAX_COMBINER_TERMS];
Keith Whitwellce721142005-07-11 10:10:38 +0000107
Brian Paul9ed03152009-09-01 15:57:36 -0600108 GLuint NumArgsA:3; /**< up to MAX_COMBINER_TERMS */
Brian Paulf337e2c2009-09-01 15:49:55 -0600109 GLuint ModeA:5; /**< MODE_x */
Brian Paulbd9b2be2009-03-08 17:58:54 -0600110 struct mode_opt OptA[MAX_COMBINER_TERMS];
Brian Pauld55a28e2009-09-01 15:34:16 -0600111 } unit[MAX_TEXTURE_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000112};
113
114#define FOG_LINEAR 0
115#define FOG_EXP 1
116#define FOG_EXP2 2
117#define FOG_UNKNOWN 3
118
119static GLuint translate_fog_mode( GLenum mode )
120{
121 switch (mode) {
122 case GL_LINEAR: return FOG_LINEAR;
123 case GL_EXP: return FOG_EXP;
124 case GL_EXP2: return FOG_EXP2;
125 default: return FOG_UNKNOWN;
126 }
127}
128
129#define OPR_SRC_COLOR 0
130#define OPR_ONE_MINUS_SRC_COLOR 1
131#define OPR_SRC_ALPHA 2
132#define OPR_ONE_MINUS_SRC_ALPHA 3
133#define OPR_ZERO 4
134#define OPR_ONE 5
135#define OPR_UNKNOWN 7
136
137static GLuint translate_operand( GLenum operand )
138{
139 switch (operand) {
140 case GL_SRC_COLOR: return OPR_SRC_COLOR;
141 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
142 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
143 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
144 case GL_ZERO: return OPR_ZERO;
145 case GL_ONE: return OPR_ONE;
Brian Paul6947f852009-01-23 17:32:09 -0700146 default:
147 assert(0);
148 return OPR_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000149 }
150}
151
152#define SRC_TEXTURE 0
153#define SRC_TEXTURE0 1
154#define SRC_TEXTURE1 2
155#define SRC_TEXTURE2 3
156#define SRC_TEXTURE3 4
157#define SRC_TEXTURE4 5
158#define SRC_TEXTURE5 6
159#define SRC_TEXTURE6 7
160#define SRC_TEXTURE7 8
161#define SRC_CONSTANT 9
162#define SRC_PRIMARY_COLOR 10
163#define SRC_PREVIOUS 11
Brian Paul6947f852009-01-23 17:32:09 -0700164#define SRC_ZERO 12
Keith Whitwellce721142005-07-11 10:10:38 +0000165#define SRC_UNKNOWN 15
166
167static GLuint translate_source( GLenum src )
168{
169 switch (src) {
170 case GL_TEXTURE: return SRC_TEXTURE;
171 case GL_TEXTURE0:
172 case GL_TEXTURE1:
173 case GL_TEXTURE2:
174 case GL_TEXTURE3:
175 case GL_TEXTURE4:
176 case GL_TEXTURE5:
177 case GL_TEXTURE6:
178 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
179 case GL_CONSTANT: return SRC_CONSTANT;
180 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
181 case GL_PREVIOUS: return SRC_PREVIOUS;
Brian Paul6947f852009-01-23 17:32:09 -0700182 case GL_ZERO:
183 return SRC_ZERO;
184 default:
185 assert(0);
186 return SRC_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000187 }
188}
189
Brian Paul6947f852009-01-23 17:32:09 -0700190#define MODE_REPLACE 0 /* r = a0 */
191#define MODE_MODULATE 1 /* r = a0 * a1 */
192#define MODE_ADD 2 /* r = a0 + a1 */
193#define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
194#define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
195#define MODE_SUBTRACT 5 /* r = a0 - a1 */
196#define MODE_DOT3_RGB 6 /* r = a0 . a1 */
197#define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
198#define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
199#define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
200#define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
201#define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
202#define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
203#define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
204#define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
Roland Scheidegger114152e2009-03-12 15:01:16 +0100205#define MODE_BUMP_ENVMAP_ATI 15 /* special */
206#define MODE_UNKNOWN 16
Keith Whitwellce721142005-07-11 10:10:38 +0000207
Brian Paul6947f852009-01-23 17:32:09 -0700208/**
209 * Translate GL combiner state into a MODE_x value
210 */
211static GLuint translate_mode( GLenum envMode, GLenum mode )
Keith Whitwellce721142005-07-11 10:10:38 +0000212{
213 switch (mode) {
214 case GL_REPLACE: return MODE_REPLACE;
215 case GL_MODULATE: return MODE_MODULATE;
Brian Paul6947f852009-01-23 17:32:09 -0700216 case GL_ADD:
217 if (envMode == GL_COMBINE4_NV)
218 return MODE_ADD_PRODUCTS;
219 else
220 return MODE_ADD;
221 case GL_ADD_SIGNED:
222 if (envMode == GL_COMBINE4_NV)
223 return MODE_ADD_PRODUCTS_SIGNED;
224 else
225 return MODE_ADD_SIGNED;
Keith Whitwellce721142005-07-11 10:10:38 +0000226 case GL_INTERPOLATE: return MODE_INTERPOLATE;
227 case GL_SUBTRACT: return MODE_SUBTRACT;
228 case GL_DOT3_RGB: return MODE_DOT3_RGB;
229 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
230 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
231 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
232 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
233 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
234 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
Roland Scheidegger114152e2009-03-12 15:01:16 +0100235 case GL_BUMP_ENVMAP_ATI: return MODE_BUMP_ENVMAP_ATI;
Brian Paul6947f852009-01-23 17:32:09 -0700236 default:
237 assert(0);
238 return MODE_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000239 }
240}
241
Brian Paulf337e2c2009-09-01 15:49:55 -0600242
Brian Paulf337e2c2009-09-01 15:49:55 -0600243/**
244 * Translate TEXTURE_x_BIT to TEXTURE_x_INDEX.
245 */
Brian Paule00ac112005-09-15 05:00:45 +0000246static GLuint translate_tex_src_bit( GLbitfield bit )
Keith Whitwellce721142005-07-11 10:10:38 +0000247{
Brian Paulb5ec0a62009-09-01 15:51:23 -0600248 ASSERT(bit);
249 return _mesa_ffs(bit) - 1;
Keith Whitwellce721142005-07-11 10:10:38 +0000250}
251
Brian Paulf337e2c2009-09-01 15:49:55 -0600252
Keith Whitwell1680ef82008-10-03 17:30:59 +0100253#define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
254#define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
255
Brian Paul239617f2008-10-07 11:22:47 -0600256/**
257 * Identify all possible varying inputs. The fragment program will
Keith Whitwell1680ef82008-10-03 17:30:59 +0100258 * never reference non-varying inputs, but will track them via state
259 * constants instead.
260 *
261 * This function figures out all the inputs that the fragment program
262 * has access to. The bitmask is later reduced to just those which
263 * are actually referenced.
264 */
Brian Paul239617f2008-10-07 11:22:47 -0600265static GLbitfield get_fp_input_mask( GLcontext *ctx )
Keith Whitwell1680ef82008-10-03 17:30:59 +0100266{
Eric Anholte5f63c42009-06-02 07:47:20 -0700267 /* _NEW_PROGRAM */
Brian Paulf0b07942008-12-17 14:04:03 -0700268 const GLboolean vertexShader = (ctx->Shader.CurrentProgram &&
Eric Anholt40990d92009-08-03 12:36:52 -0700269 ctx->Shader.CurrentProgram->LinkStatus &&
Brian Paulf0b07942008-12-17 14:04:03 -0700270 ctx->Shader.CurrentProgram->VertexProgram);
271 const GLboolean vertexProgram = ctx->VertexProgram._Enabled;
Brian Paul239617f2008-10-07 11:22:47 -0600272 GLbitfield fp_inputs = 0x0;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100273
Brian Paul6d4d51d2008-10-10 13:39:14 -0600274 if (ctx->VertexProgram._Overriden) {
275 /* Somebody's messing with the vertex program and we don't have
276 * a clue what's happening. Assume that it could be producing
277 * all possible outputs.
278 */
279 fp_inputs = ~0;
280 }
281 else if (ctx->RenderMode == GL_FEEDBACK) {
Eric Anholte5f63c42009-06-02 07:47:20 -0700282 /* _NEW_RENDERMODE */
Brian Paul6d4d51d2008-10-10 13:39:14 -0600283 fp_inputs = (FRAG_BIT_COL0 | FRAG_BIT_TEX0);
284 }
Brian Paulf0b07942008-12-17 14:04:03 -0700285 else if (!(vertexProgram || vertexShader) ||
Brian Pauld7296a12008-12-17 11:29:06 -0700286 !ctx->VertexProgram._Current) {
Brian Paulf0b07942008-12-17 14:04:03 -0700287 /* Fixed function vertex logic */
Eric Anholte5f63c42009-06-02 07:47:20 -0700288 /* _NEW_ARRAY */
Brian Paul239617f2008-10-07 11:22:47 -0600289 GLbitfield varying_inputs = ctx->varying_vp_inputs;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100290
Keith Whitwell97e63432008-10-20 13:03:45 +0100291 /* These get generated in the setup routine regardless of the
292 * vertex program:
293 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700294 /* _NEW_POINT */
Keith Whitwell97e63432008-10-20 13:03:45 +0100295 if (ctx->Point.PointSprite)
296 varying_inputs |= FRAG_BITS_TEX_ANY;
297
Keith Whitwell1680ef82008-10-03 17:30:59 +0100298 /* First look at what values may be computed by the generated
299 * vertex program:
300 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700301 /* _NEW_LIGHT */
Keith Whitwell1680ef82008-10-03 17:30:59 +0100302 if (ctx->Light.Enabled) {
303 fp_inputs |= FRAG_BIT_COL0;
304
Eric Anholt9cea84b2009-07-16 18:41:03 -0700305 if (texenv_doing_secondary_color(ctx))
Keith Whitwell1680ef82008-10-03 17:30:59 +0100306 fp_inputs |= FRAG_BIT_COL1;
307 }
308
Eric Anholte5f63c42009-06-02 07:47:20 -0700309 /* _NEW_TEXTURE */
Keith Whitwell1680ef82008-10-03 17:30:59 +0100310 fp_inputs |= (ctx->Texture._TexGenEnabled |
311 ctx->Texture._TexMatEnabled) << FRAG_ATTRIB_TEX0;
312
313 /* Then look at what might be varying as a result of enabled
314 * arrays, etc:
315 */
Brian Paul6807d962009-08-07 09:42:28 -0600316 if (varying_inputs & VERT_BIT_COLOR0)
317 fp_inputs |= FRAG_BIT_COL0;
318 if (varying_inputs & VERT_BIT_COLOR1)
319 fp_inputs |= FRAG_BIT_COL1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100320
321 fp_inputs |= (((varying_inputs & VERT_BIT_TEX_ANY) >> VERT_ATTRIB_TEX0)
322 << FRAG_ATTRIB_TEX0);
323
324 }
325 else {
326 /* calculate from vp->outputs */
Brian Paul2389c052008-12-17 19:01:34 -0700327 struct gl_vertex_program *vprog;
328 GLbitfield vp_outputs;
329
330 /* Choose GLSL vertex shader over ARB vertex program. Need this
331 * since vertex shader state validation comes after fragment state
332 * validation (see additional comments in state.c).
333 */
334 if (vertexShader)
335 vprog = ctx->Shader.CurrentProgram->VertexProgram;
336 else
Eric Anholta9ba1bf2009-08-03 12:38:56 -0700337 vprog = ctx->VertexProgram.Current;
Brian Paul2389c052008-12-17 19:01:34 -0700338
339 vp_outputs = vprog->Base.OutputsWritten;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100340
Keith Whitwell97e63432008-10-20 13:03:45 +0100341 /* These get generated in the setup routine regardless of the
342 * vertex program:
343 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700344 /* _NEW_POINT */
Keith Whitwell97e63432008-10-20 13:03:45 +0100345 if (ctx->Point.PointSprite)
346 vp_outputs |= FRAG_BITS_TEX_ANY;
347
Brian Paul6807d962009-08-07 09:42:28 -0600348 if (vp_outputs & (1 << VERT_RESULT_COL0))
349 fp_inputs |= FRAG_BIT_COL0;
350 if (vp_outputs & (1 << VERT_RESULT_COL1))
351 fp_inputs |= FRAG_BIT_COL1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100352
Keith Whitwell0370d6b2008-10-04 12:41:56 +0100353 fp_inputs |= (((vp_outputs & VERT_RESULT_TEX_ANY) >> VERT_RESULT_TEX0)
354 << FRAG_ATTRIB_TEX0);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100355 }
356
357 return fp_inputs;
358}
359
360
Brian Paul22db5352005-11-19 23:45:10 +0000361/**
362 * Examine current texture environment state and generate a unique
363 * key to identify it.
364 */
Keith Whitwell8065c122006-05-22 16:09:27 +0000365static void make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000366{
Keith Whitwellce721142005-07-11 10:10:38 +0000367 GLuint i, j;
Brian Paul239617f2008-10-07 11:22:47 -0600368 GLbitfield inputs_referenced = FRAG_BIT_COL0;
Brian Paulf337e2c2009-09-01 15:49:55 -0600369 const GLbitfield inputs_available = get_fp_input_mask( ctx );
Keith Whitwell1680ef82008-10-03 17:30:59 +0100370
Keith Whitwell8065c122006-05-22 16:09:27 +0000371 memset(key, 0, sizeof(*key));
372
Eric Anholte5f63c42009-06-02 07:47:20 -0700373 /* _NEW_TEXTURE */
Brian Paule9b34882008-12-31 11:54:02 -0700374 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000375 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Brian Paulf337e2c2009-09-01 15:49:55 -0600376 const struct gl_texture_object *texObj = texUnit->_Current;
377 const struct gl_tex_env_combine_state *comb = texUnit->_CurrentCombine;
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800378 GLenum format;
Roland Scheideggerbf4a0fa2008-02-15 17:26:06 +0100379
380 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
Keith Whitwellce721142005-07-11 10:10:38 +0000381 continue;
382
Brian Paulf337e2c2009-09-01 15:49:55 -0600383 format = texObj->Image[0][texObj->BaseLevel]->_BaseFormat;
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800384
Keith Whitwellce721142005-07-11 10:10:38 +0000385 key->unit[i].enabled = 1;
386 key->enabled_units |= (1<<i);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100387 key->nr_enabled_units = i+1;
388 inputs_referenced |= FRAG_BIT_TEX(i);
Keith Whitwellce721142005-07-11 10:10:38 +0000389
Brian Paulf337e2c2009-09-01 15:49:55 -0600390 key->unit[i].source_index =
391 translate_tex_src_bit(texUnit->_ReallyEnabled);
392
393 key->unit[i].shadow = ((texObj->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800394 ((format == GL_DEPTH_COMPONENT) ||
395 (format == GL_DEPTH_STENCIL_EXT)));
Keith Whitwellce721142005-07-11 10:10:38 +0000396
Brian Paulf337e2c2009-09-01 15:49:55 -0600397 key->unit[i].NumArgsRGB = comb->_NumArgsRGB;
398 key->unit[i].NumArgsA = comb->_NumArgsA;
Keith Whitwellce721142005-07-11 10:10:38 +0000399
400 key->unit[i].ModeRGB =
Brian Paulf337e2c2009-09-01 15:49:55 -0600401 translate_mode(texUnit->EnvMode, comb->ModeRGB);
Keith Whitwellce721142005-07-11 10:10:38 +0000402 key->unit[i].ModeA =
Brian Paulf337e2c2009-09-01 15:49:55 -0600403 translate_mode(texUnit->EnvMode, comb->ModeA);
Roland Scheidegger114152e2009-03-12 15:01:16 +0100404
Brian Paulf337e2c2009-09-01 15:49:55 -0600405 key->unit[i].ScaleShiftRGB = comb->ScaleShiftRGB;
406 key->unit[i].ScaleShiftA = comb->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000407
Brian Paulbd9b2be2009-03-08 17:58:54 -0600408 for (j = 0; j < MAX_COMBINER_TERMS; j++) {
Brian Paulf337e2c2009-09-01 15:49:55 -0600409 key->unit[i].OptRGB[j].Operand = translate_operand(comb->OperandRGB[j]);
410 key->unit[i].OptA[j].Operand = translate_operand(comb->OperandA[j]);
411 key->unit[i].OptRGB[j].Source = translate_source(comb->SourceRGB[j]);
412 key->unit[i].OptA[j].Source = translate_source(comb->SourceA[j]);
Keith Whitwellce721142005-07-11 10:10:38 +0000413 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100414
415 if (key->unit[i].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
416 /* requires some special translation */
417 key->unit[i].NumArgsRGB = 2;
418 key->unit[i].ScaleShiftRGB = 0;
419 key->unit[i].OptRGB[0].Operand = OPR_SRC_COLOR;
420 key->unit[i].OptRGB[0].Source = SRC_TEXTURE;
421 key->unit[i].OptRGB[1].Operand = OPR_SRC_COLOR;
422 key->unit[i].OptRGB[1].Source = texUnit->BumpTarget - GL_TEXTURE0 + SRC_TEXTURE0;
423 }
Keith Whitwellce721142005-07-11 10:10:38 +0000424 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100425
Eric Anholt9cea84b2009-07-16 18:41:03 -0700426 /* _NEW_LIGHT | _NEW_FOG */
427 if (texenv_doing_secondary_color(ctx)) {
Keith Whitwellce721142005-07-11 10:10:38 +0000428 key->separate_specular = 1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100429 inputs_referenced |= FRAG_BIT_COL1;
430 }
Keith Whitwellce721142005-07-11 10:10:38 +0000431
Eric Anholte5f63c42009-06-02 07:47:20 -0700432 /* _NEW_FOG */
Keith Whitwellce721142005-07-11 10:10:38 +0000433 if (ctx->Fog.Enabled) {
434 key->fog_enabled = 1;
435 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100436 inputs_referenced |= FRAG_BIT_FOGC; /* maybe */
Keith Whitwellce721142005-07-11 10:10:38 +0000437 }
Keith Whitwell1680ef82008-10-03 17:30:59 +0100438
439 key->inputs_available = (inputs_available & inputs_referenced);
Keith Whitwellce721142005-07-11 10:10:38 +0000440}
441
Brian Paul239617f2008-10-07 11:22:47 -0600442/**
443 * Use uregs to represent registers internally, translate to Mesa's
Keith Whitwell15e75e02005-04-29 15:11:39 +0000444 * expected formats on emit.
445 *
446 * NOTE: These are passed by value extensively in this file rather
447 * than as usual by pointer reference. If this disturbs you, try
448 * remembering they are just 32bits in size.
449 *
450 * GCC is smart enough to deal with these dword-sized structures in
451 * much the same way as if I had defined them as dwords and was using
452 * macros to access and set the fields. This is much nicer and easier
453 * to evolve.
454 */
455struct ureg {
456 GLuint file:4;
457 GLuint idx:8;
458 GLuint negatebase:1;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000459 GLuint swz:12;
Brian Paul1480bca2009-09-01 16:01:12 -0600460 GLuint pad:7;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000461};
462
Brian Paul13abf912006-04-13 19:17:13 +0000463static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000464 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000465 ~0,
466 0,
467 0,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000468 0
469};
470
Keith Whitwell15e75e02005-04-29 15:11:39 +0000471
Brian Paul239617f2008-10-07 11:22:47 -0600472/** State used to build the fragment program:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000473 */
474struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000475 struct gl_fragment_program *program;
Keith Whitwellce721142005-07-11 10:10:38 +0000476 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000477
Brian Paul239617f2008-10-07 11:22:47 -0600478 GLbitfield alu_temps; /**< Track texture indirections, see spec. */
479 GLbitfield temps_output; /**< Track texture indirections, see spec. */
480 GLbitfield temp_in_use; /**< Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000481 GLboolean error;
482
Brian Paule9b34882008-12-31 11:54:02 -0700483 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000484 /* Reg containing each texture unit's sampled texture color,
485 * else undef.
486 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000487
Roland Scheidegger114152e2009-03-12 15:01:16 +0100488 struct ureg texcoord_tex[MAX_TEXTURE_COORD_UNITS];
489 /* Reg containing texcoord for a texture unit,
490 * needed for bump mapping, else undef.
491 */
492
Brian Paul239617f2008-10-07 11:22:47 -0600493 struct ureg src_previous; /**< Reg containing color from previous
Keith Whitwell15e75e02005-04-29 15:11:39 +0000494 * stage. May need to be decl'd.
495 */
496
Brian Paul239617f2008-10-07 11:22:47 -0600497 GLuint last_tex_stage; /**< Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000498
499 struct ureg half;
500 struct ureg one;
501 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000502};
503
504
505
506static struct ureg make_ureg(GLuint file, GLuint idx)
507{
508 struct ureg reg;
509 reg.file = file;
510 reg.idx = idx;
511 reg.negatebase = 0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000512 reg.swz = SWIZZLE_NOOP;
513 reg.pad = 0;
514 return reg;
515}
516
517static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
518{
519 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
520 GET_SWZ(reg.swz, y),
521 GET_SWZ(reg.swz, z),
522 GET_SWZ(reg.swz, w));
523
524 return reg;
525}
526
527static struct ureg swizzle1( struct ureg reg, int x )
528{
529 return swizzle(reg, x, x, x, x);
530}
531
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000532static struct ureg negate( struct ureg reg )
533{
534 reg.negatebase ^= 1;
535 return reg;
536}
537
Keith Whitwell15e75e02005-04-29 15:11:39 +0000538static GLboolean is_undef( struct ureg reg )
539{
Alan Hourihane8d972652006-08-10 13:12:00 +0000540 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000541}
542
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000543
Keith Whitwell15e75e02005-04-29 15:11:39 +0000544static struct ureg get_temp( struct texenv_fragment_program *p )
545{
Brian Paul13abf912006-04-13 19:17:13 +0000546 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000547
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000548 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000549 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000550 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000551
552 /* Then any unused temporary:
553 */
554 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000555 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000556
Keith Whitwell15e75e02005-04-29 15:11:39 +0000557 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000558 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000559 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000560 }
561
Brian Paul13abf912006-04-13 19:17:13 +0000562 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000563 p->program->Base.NumTemporaries = bit;
564
Keith Whitwell93cd9232005-05-11 08:30:23 +0000565 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000566 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
567}
568
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000569static struct ureg get_tex_temp( struct texenv_fragment_program *p )
570{
571 int bit;
572
Brian Pauld01269a2008-09-25 18:27:22 -0600573 /* First try to find available temp not previously used (to avoid
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000574 * starting a new texture indirection). According to the spec, the
575 * ~p->temps_output isn't necessary, but will keep it there for
576 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000577 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000578 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000579
580 /* Then any unused temporary:
581 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000582 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000583 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000584
585 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000586 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000587 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000588 }
589
Brian Paul13abf912006-04-13 19:17:13 +0000590 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000591 p->program->Base.NumTemporaries = bit;
592
Keith Whitwell93cd9232005-05-11 08:30:23 +0000593 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000594 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
595}
596
Keith Whitwell15e75e02005-04-29 15:11:39 +0000597
Brian Paul5620c202008-09-26 11:18:06 -0600598/** Mark a temp reg as being no longer allocatable. */
599static void reserve_temp( struct texenv_fragment_program *p, struct ureg r )
600{
601 if (r.file == PROGRAM_TEMPORARY)
602 p->temps_output |= (1 << r.idx);
603}
604
605
Brian93fef222007-10-29 12:25:46 -0600606static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000607{
Brian93fef222007-10-29 12:25:46 -0600608 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000609
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000610 /* KW: To support tex_env_crossbar, don't release the registers in
611 * temps_output.
612 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000613 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000614 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000615 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000616 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000617}
618
619
Brian9fe3e2e2007-02-23 11:44:14 -0700620static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000621 GLint s0,
622 GLint s1,
623 GLint s2,
624 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700625 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000626{
Brian9fe3e2e2007-02-23 11:44:14 -0700627 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000628 GLuint idx;
629 tokens[0] = s0;
630 tokens[1] = s1;
631 tokens[2] = s2;
632 tokens[3] = s3;
633 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000634 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000635 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000636}
637
Keith Whitwell47b29f52005-05-04 11:44:44 +0000638
Brian9fe3e2e2007-02-23 11:44:14 -0700639#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
640#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
641#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
642#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000643
Keith Whitwell1680ef82008-10-03 17:30:59 +0100644static GLuint frag_to_vert_attrib( GLuint attrib )
645{
646 switch (attrib) {
647 case FRAG_ATTRIB_COL0: return VERT_ATTRIB_COLOR0;
648 case FRAG_ATTRIB_COL1: return VERT_ATTRIB_COLOR1;
649 default:
650 assert(attrib >= FRAG_ATTRIB_TEX0);
651 assert(attrib <= FRAG_ATTRIB_TEX7);
652 return attrib - FRAG_ATTRIB_TEX0 + VERT_ATTRIB_TEX0;
653 }
654}
655
Keith Whitwell47b29f52005-05-04 11:44:44 +0000656
657static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
658{
Keith Whitwell1680ef82008-10-03 17:30:59 +0100659 if (p->state->inputs_available & (1<<input)) {
660 p->program->Base.InputsRead |= (1 << input);
661 return make_ureg(PROGRAM_INPUT, input);
662 }
663 else {
664 GLuint idx = frag_to_vert_attrib( input );
665 return register_param3( p, STATE_INTERNAL, STATE_CURRENT_ATTRIB, idx );
666 }
Keith Whitwell47b29f52005-05-04 11:44:44 +0000667}
668
669
Brian Paul7e807512005-11-05 17:10:45 +0000670static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000671 struct ureg ureg )
672{
673 reg->File = ureg.file;
674 reg->Index = ureg.idx;
675 reg->Swizzle = ureg.swz;
Brian Paul7db7ff82009-04-14 22:14:30 -0600676 reg->Negate = ureg.negatebase ? NEGATE_XYZW : NEGATE_NONE;
Brian Paul1480bca2009-09-01 16:01:12 -0600677 reg->Abs = GL_FALSE;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000678}
679
Brian Paul7e807512005-11-05 17:10:45 +0000680static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000681 struct ureg ureg, GLuint mask )
682{
683 dst->File = ureg.file;
684 dst->Index = ureg.idx;
685 dst->WriteMask = mask;
Brianc9d495c2007-10-23 10:55:24 -0600686 dst->CondMask = COND_TR; /* always pass cond test */
687 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000688}
689
Brian Paul7e807512005-11-05 17:10:45 +0000690static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000691emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000692 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000693 struct ureg dest,
694 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000695 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000696 struct ureg src0,
697 struct ureg src1,
698 struct ureg src2 )
699{
Brian Paul9ed03152009-09-01 15:57:36 -0600700 const GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000701 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Brian2a8e9bb2007-10-23 10:24:53 -0600702
703 assert(nr < MAX_INSTRUCTIONS);
704
Brian Pauld6272e02006-10-29 18:03:16 +0000705 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000706 inst->Opcode = op;
707
Keith Whitwell47b29f52005-05-04 11:44:44 +0000708 emit_arg( &inst->SrcReg[0], src0 );
709 emit_arg( &inst->SrcReg[1], src1 );
710 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000711
Brian Paule31ac052005-11-20 17:52:40 +0000712 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000713
714 emit_dst( &inst->DstReg, dest, mask );
715
Brian Paul5620c202008-09-26 11:18:06 -0600716#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000717 /* Accounting for indirection tracking:
718 */
719 if (dest.file == PROGRAM_TEMPORARY)
720 p->temps_output |= 1 << dest.idx;
Brian Paul5620c202008-09-26 11:18:06 -0600721#endif
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000722
Keith Whitwell15e75e02005-04-29 15:11:39 +0000723 return inst;
724}
725
726
727static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000728 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000729 struct ureg dest,
730 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000731 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000732 struct ureg src0,
733 struct ureg src1,
734 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000735{
736 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
737
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000738 /* Accounting for indirection tracking:
739 */
740 if (src0.file == PROGRAM_TEMPORARY)
741 p->alu_temps |= 1 << src0.idx;
742
743 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
744 p->alu_temps |= 1 << src1.idx;
745
746 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
747 p->alu_temps |= 1 << src2.idx;
748
749 if (dest.file == PROGRAM_TEMPORARY)
750 p->alu_temps |= 1 << dest.idx;
751
Brian21f99792007-01-09 11:00:21 -0700752 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000753 return dest;
754}
755
756static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000757 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000758 struct ureg dest,
759 GLuint destmask,
760 GLuint tex_unit,
761 GLuint tex_idx,
Brian Paul44e018c2009-02-20 13:42:08 -0700762 GLuint tex_shadow,
Keith Whitwellce721142005-07-11 10:10:38 +0000763 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000764{
Brian Paul7e807512005-11-05 17:10:45 +0000765 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000766 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000767 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000768 coord, /* arg 0? */
769 undef,
770 undef);
771
Brian Paul7e807512005-11-05 17:10:45 +0000772 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000773 inst->TexSrcUnit = tex_unit;
Brian Paul44e018c2009-02-20 13:42:08 -0700774 inst->TexShadow = tex_shadow;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000775
Brian21f99792007-01-09 11:00:21 -0700776 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000777
Brian Paul5620c202008-09-26 11:18:06 -0600778 /* Accounting for indirection tracking:
779 */
780 reserve_temp(p, dest);
781
Roland Scheidegger114152e2009-03-12 15:01:16 +0100782#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000783 /* Is this a texture indirection?
784 */
785 if ((coord.file == PROGRAM_TEMPORARY &&
786 (p->temps_output & (1<<coord.idx))) ||
787 (dest.file == PROGRAM_TEMPORARY &&
788 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700789 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000790 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000791 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000792 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000793 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100794#endif
Keith Whitwell15e75e02005-04-29 15:11:39 +0000795
796 return dest;
797}
798
799
Keith Whitwell47b29f52005-05-04 11:44:44 +0000800static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000801 GLfloat s0,
802 GLfloat s1,
803 GLfloat s2,
804 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000805{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000806 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700807 GLuint idx, swizzle;
Brian Pauld01269a2008-09-25 18:27:22 -0600808 struct ureg r;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000809 values[0] = s0;
810 values[1] = s1;
811 values[2] = s2;
812 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700813 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
814 &swizzle );
Brian Pauld01269a2008-09-25 18:27:22 -0600815 r = make_ureg(PROGRAM_CONSTANT, idx);
816 r.swz = swizzle;
817 return r;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000818}
819
Keith Whitwelle4902422005-05-11 15:16:35 +0000820#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000821#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
822#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
823#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000824
825
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000826static struct ureg get_one( struct texenv_fragment_program *p )
827{
828 if (is_undef(p->one))
829 p->one = register_scalar_const(p, 1.0);
830 return p->one;
831}
832
833static struct ureg get_half( struct texenv_fragment_program *p )
834{
835 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000836 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000837 return p->half;
838}
839
840static struct ureg get_zero( struct texenv_fragment_program *p )
841{
842 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000843 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000844 return p->zero;
845}
846
Keith Whitwell15e75e02005-04-29 15:11:39 +0000847
Keith Whitwell15e75e02005-04-29 15:11:39 +0000848static void program_error( struct texenv_fragment_program *p, const char *msg )
849{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000850 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000851 p->error = 1;
852}
853
Keith Whitwell15e75e02005-04-29 15:11:39 +0000854static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000855 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000856{
857 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000858 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000859 assert(!is_undef(p->src_texture[unit]));
860 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000861
Keith Whitwellce721142005-07-11 10:10:38 +0000862 case SRC_TEXTURE0:
863 case SRC_TEXTURE1:
864 case SRC_TEXTURE2:
865 case SRC_TEXTURE3:
866 case SRC_TEXTURE4:
867 case SRC_TEXTURE5:
868 case SRC_TEXTURE6:
869 case SRC_TEXTURE7:
870 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
871 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000872
Keith Whitwellce721142005-07-11 10:10:38 +0000873 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000874 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000875
Keith Whitwellce721142005-07-11 10:10:38 +0000876 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000877 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000878
Brian Paul6947f852009-01-23 17:32:09 -0700879 case SRC_ZERO:
880 return get_zero(p);
881
Keith Whitwellce721142005-07-11 10:10:38 +0000882 case SRC_PREVIOUS:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000883 if (is_undef(p->src_previous))
884 return register_input(p, FRAG_ATTRIB_COL0);
885 else
886 return p->src_previous;
Brian Paul6947f852009-01-23 17:32:09 -0700887
888 default:
889 assert(0);
José Fonsecac6af9b22009-06-15 19:20:25 +0100890 return undef;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000891 }
892}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000893
Keith Whitwell15e75e02005-04-29 15:11:39 +0000894static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000895 GLuint mask,
896 GLuint unit,
897 GLuint source,
898 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000899{
900 struct ureg arg, src, one;
901
902 src = get_source(p, source, unit);
903
904 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000905 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000906 /* Get unused tmp,
907 * Emit tmp = 1.0 - arg.xyzw
908 */
909 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000910 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000911 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000912
Keith Whitwellce721142005-07-11 10:10:38 +0000913 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000914 if (mask == WRITEMASK_W)
915 return src;
916 else
Brian Paul22db5352005-11-19 23:45:10 +0000917 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000918 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000919 /* Get unused tmp,
920 * Emit tmp = 1.0 - arg.wwww
921 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000922 arg = get_temp(p);
923 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000924 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000925 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000926 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000927 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000928 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000929 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000930 case OPR_SRC_COLOR:
Brian Paul6947f852009-01-23 17:32:09 -0700931 return src;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000932 default:
Brian Paul6947f852009-01-23 17:32:09 -0700933 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000934 return src;
935 }
936}
937
Brian Paulf337e2c2009-09-01 15:49:55 -0600938/**
939 * Check if the RGB and Alpha sources and operands match for the given
940 * texture unit's combinder state. When the RGB and A sources and
941 * operands match, we can emit fewer instructions.
942 */
Brian Paula5e70032009-08-31 11:17:59 -0600943static GLboolean args_match( const struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000944{
Brian Paul9ed03152009-09-01 15:57:36 -0600945 GLuint i, numArgs = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000946
Brian Paul9ed03152009-09-01 15:57:36 -0600947 for (i = 0 ; i < numArgs ; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000948 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000949 return GL_FALSE;
950
Brian Paul9ed03152009-09-01 15:57:36 -0600951 switch (key->unit[unit].OptA[i].Operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000952 case OPR_SRC_ALPHA:
Brian Paul9ed03152009-09-01 15:57:36 -0600953 switch (key->unit[unit].OptRGB[i].Operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000954 case OPR_SRC_COLOR:
955 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000956 break;
957 default:
958 return GL_FALSE;
959 }
960 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000961 case OPR_ONE_MINUS_SRC_ALPHA:
Brian Paul9ed03152009-09-01 15:57:36 -0600962 switch (key->unit[unit].OptRGB[i].Operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000963 case OPR_ONE_MINUS_SRC_COLOR:
964 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000965 break;
966 default:
967 return GL_FALSE;
968 }
969 break;
970 default:
971 return GL_FALSE; /* impossible */
972 }
973 }
974
975 return GL_TRUE;
976}
977
Keith Whitwell15e75e02005-04-29 15:11:39 +0000978static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000979 struct ureg dest,
980 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000981 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000982 GLuint unit,
983 GLuint nr,
984 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +0000985 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000986{
Brian Paulbd9b2be2009-03-08 17:58:54 -0600987 struct ureg src[MAX_COMBINER_TERMS];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000988 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +0000989 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000990
Brian Paulbd9b2be2009-03-08 17:58:54 -0600991 assert(nr <= MAX_COMBINER_TERMS);
Brian Paul6947f852009-01-23 17:32:09 -0700992
Brian Paul00630842005-12-12 15:27:55 +0000993 tmp = undef; /* silence warning (bug 5318) */
994
Keith Whitwell15e75e02005-04-29 15:11:39 +0000995 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +0000996 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000997
998 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +0000999 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001000 if (mask == WRITEMASK_XYZW && !saturate)
1001 return src[0];
1002 else
Brian Paul7e807512005-11-05 17:10:45 +00001003 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001004 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +00001005 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001006 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001007 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00001008 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001009 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001010 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001011 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001012 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +00001013 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001014 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +00001015 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +00001016 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
1017 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001018 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +00001019 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001020 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
1021 */
Brian Paul7e807512005-11-05 17:10:45 +00001022 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001023
Keith Whitwellce721142005-07-11 10:10:38 +00001024 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +00001025 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001026
Keith Whitwellce721142005-07-11 10:10:38 +00001027 case MODE_DOT3_RGBA:
1028 case MODE_DOT3_RGBA_EXT:
1029 case MODE_DOT3_RGB_EXT:
1030 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001031 struct ureg tmp0 = get_temp( p );
1032 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +00001033 struct ureg neg1 = register_scalar_const(p, -1);
1034 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001035
1036 /* tmp0 = 2*src0 - 1
1037 * tmp1 = 2*src1 - 1
1038 *
1039 * dst = tmp0 dot3 tmp1
1040 */
Brian Paul7e807512005-11-05 17:10:45 +00001041 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001042 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001043
Brian Paulaa206952005-09-16 18:14:24 +00001044 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001045 tmp1 = tmp0;
1046 else
Brian Paul7e807512005-11-05 17:10:45 +00001047 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001048 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +00001049 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001050 return dest;
1051 }
Keith Whitwellce721142005-07-11 10:10:38 +00001052 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001053 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001054 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001055 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +00001056 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001057 /* Arg0 * Arg2 + Arg1 - 0.5 */
1058 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001059 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +00001060 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
1061 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001062 return dest;
1063 }
Keith Whitwellce721142005-07-11 10:10:38 +00001064 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001065 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001066 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001067 return dest;
Brian Paul6947f852009-01-23 17:32:09 -07001068 case MODE_ADD_PRODUCTS:
1069 /* Arg0 * Arg1 + Arg2 * Arg3 */
1070 {
1071 struct ureg tmp0 = get_temp(p);
1072 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1073 emit_arith( p, OPCODE_MAD, dest, mask, saturate, src[2], src[3], tmp0 );
1074 }
1075 return dest;
1076 case MODE_ADD_PRODUCTS_SIGNED:
1077 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1078 {
1079 struct ureg tmp0 = get_temp(p);
1080 half = get_half(p);
1081 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1082 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[2], src[3], tmp0 );
1083 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1084 }
1085 return dest;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001086 case MODE_BUMP_ENVMAP_ATI:
1087 /* special - not handled here */
1088 assert(0);
1089 return src[0];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001090 default:
Brian Paul6947f852009-01-23 17:32:09 -07001091 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001092 return src[0];
1093 }
1094}
1095
Keith Whitwell15e75e02005-04-29 15:11:39 +00001096
Brian Paul22db5352005-11-19 23:45:10 +00001097/**
1098 * Generate instructions for one texture unit's env/combiner mode.
1099 */
1100static struct ureg
1101emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001102{
Brian Paula5e70032009-08-31 11:17:59 -06001103 const struct state_key *key = p->state;
Brian Paul956e6c32009-08-31 11:14:16 -06001104 GLboolean saturate;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001105 GLuint rgb_shift, alpha_shift;
Brian Paula5e70032009-08-31 11:17:59 -06001106 struct ureg out, dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001107
Keith Whitwellce721142005-07-11 10:10:38 +00001108 if (!key->unit[unit].enabled) {
1109 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001110 }
Roland Scheidegger114152e2009-03-12 15:01:16 +01001111 if (key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1112 /* this isn't really a env stage delivering a color and handled elsewhere */
1113 return get_source(p, SRC_PREVIOUS, 0);
1114 }
Keith Whitwellce721142005-07-11 10:10:38 +00001115
1116 switch (key->unit[unit].ModeRGB) {
1117 case MODE_DOT3_RGB_EXT:
1118 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001119 rgb_shift = 0;
1120 break;
Keith Whitwellce721142005-07-11 10:10:38 +00001121 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001122 alpha_shift = 0;
1123 rgb_shift = 0;
1124 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001125 default:
Keith Whitwellce721142005-07-11 10:10:38 +00001126 rgb_shift = key->unit[unit].ScaleShiftRGB;
1127 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001128 break;
1129 }
Keith Whitwellce721142005-07-11 10:10:38 +00001130
Brian Paul956e6c32009-08-31 11:14:16 -06001131 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
1132 * We don't want to clamp twice.
1133 */
1134 saturate = !(rgb_shift || alpha_shift);
1135
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001136 /* If this is the very last calculation, emit direct to output reg:
1137 */
Keith Whitwellce721142005-07-11 10:10:38 +00001138 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001139 unit != p->last_tex_stage ||
1140 alpha_shift ||
1141 rgb_shift)
1142 dest = get_temp( p );
1143 else
Brian Paul8d475822009-02-28 11:49:46 -07001144 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLOR);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001145
1146 /* Emit the RGB and A combine ops
1147 */
Keith Whitwellce721142005-07-11 10:10:38 +00001148 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
1149 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001150 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1151 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001152 key->unit[unit].NumArgsRGB,
1153 key->unit[unit].ModeRGB,
1154 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001155 }
Keith Whitwellce721142005-07-11 10:10:38 +00001156 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +02001157 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001158 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1159 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001160 key->unit[unit].NumArgsRGB,
1161 key->unit[unit].ModeRGB,
1162 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001163 }
1164 else {
1165 /* Need to do something to stop from re-emitting identical
1166 * argument calculations here:
1167 */
1168 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
1169 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001170 key->unit[unit].NumArgsRGB,
1171 key->unit[unit].ModeRGB,
1172 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001173 out = emit_combine( p, dest, WRITEMASK_W, saturate,
1174 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001175 key->unit[unit].NumArgsA,
1176 key->unit[unit].ModeA,
1177 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001178 }
1179
1180 /* Deal with the final shift:
1181 */
1182 if (alpha_shift || rgb_shift) {
Brian Paula5e70032009-08-31 11:17:59 -06001183 struct ureg shift;
1184
Brian Paul956e6c32009-08-31 11:14:16 -06001185 saturate = GL_TRUE; /* always saturate at this point */
Brian Paula5e70032009-08-31 11:17:59 -06001186
Keith Whitwell15e75e02005-04-29 15:11:39 +00001187 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 Paulb5ec0a62009-09-01 15:51:23 -06001211 const 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
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001223 /* TODO: Use D0_MASK_XY where possible.
1224 */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +02001225 if (p->state->unit[unit].enabled) {
Brian Paul44e018c2009-02-20 13:42:08 -07001226 GLboolean shadow = GL_FALSE;
1227
1228 if (p->state->unit[unit].shadow) {
1229 p->program->Base.ShadowSamplers |= 1 << unit;
1230 shadow = GL_TRUE;
1231 }
1232
Aapo Tahkolab8915342006-03-28 10:26:34 +00001233 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1234 tmp, WRITEMASK_XYZW,
Brian Paul44e018c2009-02-20 13:42:08 -07001235 unit, texTarget, shadow,
1236 texcoord );
Brian9b7e5a52007-12-14 11:16:49 -07001237
1238 p->program->Base.SamplersUsed |= (1 << unit);
Brian1fe385f2007-12-14 11:42:28 -07001239 /* This identity mapping should already be in place
1240 * (see _mesa_init_program_struct()) but let's be safe.
1241 */
1242 p->program->Base.SamplerUnits[unit] = unit;
Brian9b7e5a52007-12-14 11:16:49 -07001243 }
1244 else
Aapo Tahkolab8915342006-03-28 10:26:34 +00001245 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001246 }
1247}
1248
Keith Whitwell241b6b72005-05-23 09:50:34 +00001249static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +00001250 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001251{
1252 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +00001253 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001254 load_texture(p, unit);
1255 break;
1256
Keith Whitwellce721142005-07-11 10:10:38 +00001257 case SRC_TEXTURE0:
1258 case SRC_TEXTURE1:
1259 case SRC_TEXTURE2:
1260 case SRC_TEXTURE3:
1261 case SRC_TEXTURE4:
1262 case SRC_TEXTURE5:
1263 case SRC_TEXTURE6:
1264 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +00001265 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001266 break;
1267
1268 default:
Brian Paul6947f852009-01-23 17:32:09 -07001269 /* not a texture src - do nothing */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001270 break;
1271 }
Keith Whitwell241b6b72005-05-23 09:50:34 +00001272
1273 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001274}
1275
Brian Paul22db5352005-11-19 23:45:10 +00001276
1277/**
1278 * Generate instructions for loading all texture source terms.
1279 */
1280static GLboolean
1281load_texunit_sources( struct texenv_fragment_program *p, int unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001282{
Brian Paula5e70032009-08-31 11:17:59 -06001283 const struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +00001284 GLuint i;
1285
1286 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001287 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001288 }
Aapo Tahkolab8915342006-03-28 10:26:34 +00001289
1290 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1291 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1292 }
1293
Keith Whitwell241b6b72005-05-23 09:50:34 +00001294 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001295}
1296
Roland Scheidegger114152e2009-03-12 15:01:16 +01001297/**
1298 * Generate instructions for loading bump map textures.
1299 */
1300static GLboolean
1301load_texunit_bumpmap( struct texenv_fragment_program *p, int unit )
1302{
Brian Paula5e70032009-08-31 11:17:59 -06001303 const struct state_key *key = p->state;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001304 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1305 struct ureg texcDst, bumpMapRes;
1306 struct ureg constdudvcolor = register_const4f(p, 0.0, 0.0, 0.0, 1.0);
1307 struct ureg texcSrc = register_input(p, FRAG_ATTRIB_TEX0 + bumpedUnitNr);
1308 struct ureg rotMat0 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_0, unit );
1309 struct ureg rotMat1 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_1, unit );
1310
1311 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1312
1313 bumpMapRes = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1314 texcDst = get_tex_temp( p );
1315 p->texcoord_tex[bumpedUnitNr] = texcDst;
1316
1317 /* apply rot matrix and add coords to be available in next phase */
1318 /* dest = (Arg0.xxxx * rotMat0 + Arg1) + (Arg0.yyyy * rotMat1) */
1319 /* note only 2 coords are affected the rest are left unchanged (mul by 0) */
1320 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1321 swizzle1(bumpMapRes, SWIZZLE_X), rotMat0, texcSrc );
1322 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1323 swizzle1(bumpMapRes, SWIZZLE_Y), rotMat1, texcDst );
1324
1325 /* move 0,0,0,1 into bumpmap src if someone (crossbar) is foolish
1326 enough to access this later, should optimize away */
1327 emit_arith( p, OPCODE_MOV, bumpMapRes, WRITEMASK_XYZW, 0, constdudvcolor, undef, undef );
1328
1329 return GL_TRUE;
1330}
Brian Paul22db5352005-11-19 23:45:10 +00001331
1332/**
1333 * Generate a new fragment program which implements the context's
1334 * current texture env/combine mode.
1335 */
1336static void
Brian Paule998c342006-10-29 21:17:18 +00001337create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001338 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001339{
Brian Paule998c342006-10-29 21:17:18 +00001340 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001341 struct texenv_fragment_program p;
1342 GLuint unit;
1343 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001344
Keith Whitwelle4902422005-05-11 15:16:35 +00001345 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwellce721142005-07-11 10:10:38 +00001346 p.state = key;
1347 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001348
Brian Paule998c342006-10-29 21:17:18 +00001349 /* During code generation, use locally-allocated instruction buffer,
1350 * then alloc dynamic storage below.
1351 */
1352 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001353 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001354 p.program->Base.NumTexIndirections = 1;
Brian21f99792007-01-09 11:00:21 -07001355 p.program->Base.NumTexInstructions = 0;
1356 p.program->Base.NumAluInstructions = 0;
Brian1240eb22007-03-22 08:50:20 -06001357 p.program->Base.String = NULL;
Keith Whitwell9ca88152005-05-10 09:56:02 +00001358 p.program->Base.NumInstructions =
Brian Paule998c342006-10-29 21:17:18 +00001359 p.program->Base.NumTemporaries =
1360 p.program->Base.NumParameters =
1361 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001362 p.program->Base.Parameters = _mesa_new_parameter_list();
Keith Whitwelldbeea252005-05-10 13:57:50 +00001363
Brian Paulde997602005-11-12 17:53:14 +00001364 p.program->Base.InputsRead = 0;
Brian Paul8d475822009-02-28 11:49:46 -07001365 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLOR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001366
Roland Scheidegger114152e2009-03-12 15:01:16 +01001367 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001368 p.src_texture[unit] = undef;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001369 p.texcoord_tex[unit] = undef;
1370 }
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001371
Keith Whitwell47b29f52005-05-04 11:44:44 +00001372 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001373 p.half = undef;
1374 p.zero = undef;
1375 p.one = undef;
1376
Keith Whitwell15e75e02005-04-29 15:11:39 +00001377 p.last_tex_stage = 0;
Brian93fef222007-10-29 12:25:46 -06001378 release_temps(ctx, &p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001379
Keith Whitwellce721142005-07-11 10:10:38 +00001380 if (key->enabled_units) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001381 GLboolean needbumpstage = GL_FALSE;
1382 /* Zeroth pass - bump map textures first */
1383 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
1384 if (key->unit[unit].enabled && key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1385 needbumpstage = GL_TRUE;
1386 load_texunit_bumpmap( &p, unit );
1387 }
1388 if (needbumpstage)
1389 p.program->Base.NumTexIndirections++;
1390
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001391 /* First pass - to support texture_env_crossbar, first identify
1392 * all referenced texture sources and emit texld instructions
1393 * for each:
1394 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001395 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001396 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001397 load_texunit_sources( &p, unit );
1398 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001399 }
1400
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001401 /* Second pass - emit combine instructions to build final color:
1402 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001403 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001404 if (key->enabled_units & (1<<unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001405 p.src_previous = emit_texenv( &p, unit );
Brian Paul5620c202008-09-26 11:18:06 -06001406 reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
Brian93fef222007-10-29 12:25:46 -06001407 release_temps(ctx, &p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001408 }
1409 }
1410
Keith Whitwellce721142005-07-11 10:10:38 +00001411 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul8d475822009-02-28 11:49:46 -07001412 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLOR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001413
Keith Whitwellce721142005-07-11 10:10:38 +00001414 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001415 /* Emit specular add.
1416 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001417 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001418 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1419 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001420 }
Brian Paulaa206952005-09-16 18:14:24 +00001421 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001422 /* Will wind up in here if no texture enabled or a couple of
1423 * other scenarios (GL_REPLACE for instance).
1424 */
Brian Paul7e807512005-11-05 17:10:45 +00001425 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001426 }
1427
Keith Whitwell47b29f52005-05-04 11:44:44 +00001428 /* Finish up:
1429 */
Brian Paul7e807512005-11-05 17:10:45 +00001430 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001431
Keith Whitwellce721142005-07-11 10:10:38 +00001432 if (key->fog_enabled) {
1433 /* Pull fog mode from GLcontext, the value in the state key is
1434 * a reduced value and not what is expected in FogOption
1435 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001436 p.program->FogOption = ctx->Fog.Mode;
Brian19d77d62007-09-18 19:29:26 -06001437 p.program->Base.InputsRead |= FRAG_BIT_FOGC; /* XXX new */
Keith Whitwellce721142005-07-11 10:10:38 +00001438 } else
Keith Whitwell276330b2005-05-09 17:58:13 +00001439 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001440
Brian21f99792007-01-09 11:00:21 -07001441 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001442 program_error(&p, "Exceeded max nr indirect texture lookups");
1443
Brian21f99792007-01-09 11:00:21 -07001444 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001445 program_error(&p, "Exceeded max TEX instructions");
1446
Brian21f99792007-01-09 11:00:21 -07001447 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001448 program_error(&p, "Exceeded max ALU instructions");
1449
Brian Paul5d7b49f2005-11-19 23:12:20 +00001450 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001451
Brian Paule998c342006-10-29 21:17:18 +00001452 /* Allocate final instruction array */
Brianc81cce72007-09-25 15:18:51 -06001453 p.program->Base.Instructions
1454 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1455 if (!p.program->Base.Instructions) {
Brian Paule998c342006-10-29 21:17:18 +00001456 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1457 "generating tex env program");
1458 return;
1459 }
Brianc81cce72007-09-25 15:18:51 -06001460 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1461 p.program->Base.NumInstructions);
1462
1463 if (p.program->FogOption) {
1464 _mesa_append_fog_code(ctx, p.program);
1465 p.program->FogOption = GL_NONE;
1466 }
1467
Brian Paule998c342006-10-29 21:17:18 +00001468
Keith Whitwelldbeea252005-05-10 13:57:50 +00001469 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001470 */
Brian Paule998c342006-10-29 21:17:18 +00001471 if (ctx->Driver.ProgramStringNotify) {
1472 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1473 &p.program->Base );
1474 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001475
Brian Paule998c342006-10-29 21:17:18 +00001476 if (DISASSEM) {
1477 _mesa_print_program(&p.program->Base);
1478 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001479 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001480}
1481
Brian Paul5d7b49f2005-11-19 23:12:20 +00001482
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001483/**
1484 * Return a fragment program which implements the current
1485 * fixed-function texture, fog and color-sum operations.
1486 */
1487struct gl_fragment_program *
1488_mesa_get_fixed_func_fragment_program(GLcontext *ctx)
Keith Whitwellce721142005-07-11 10:10:38 +00001489{
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001490 struct gl_fragment_program *prog;
1491 struct state_key key;
1492
1493 make_state_key(ctx, &key);
1494
1495 prog = (struct gl_fragment_program *)
1496 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1497 &key, sizeof(key));
Keith Whitwellce721142005-07-11 10:10:38 +00001498
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001499 if (!prog) {
1500 prog = (struct gl_fragment_program *)
1501 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1502
1503 create_new_program(ctx, &key, prog);
1504
1505 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1506 &key, sizeof(key), &prog->Base);
Keith Whitwellce721142005-07-11 10:10:38 +00001507 }
1508
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001509 return prog;
Keith Whitwellce721142005-07-11 10:10:38 +00001510}