blob: c64d88faf9ccc646d1b84a91c16c19178f5c0653 [file] [log] [blame]
Keith Whitwell15e75e02005-04-29 15:11:39 +00001/**************************************************************************
2 *
Brianf4a5ea22007-10-31 12:45:32 -06003 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
Keith Whitwell15e75e02005-04-29 15:11:39 +00004 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
Keith Whitwell15e75e02005-04-29 15:11:39 +000028#include "glheader.h"
29#include "macros.h"
30#include "enums.h"
Brianc223c6b2007-07-04 13:15:20 -060031#include "shader/prog_parameter.h"
Brianf4a5ea22007-10-31 12:45:32 -060032#include "shader/prog_cache.h"
Brianc223c6b2007-07-04 13:15:20 -060033#include "shader/prog_instruction.h"
34#include "shader/prog_print.h"
35#include "shader/prog_statevars.h"
Brian83fad682007-09-25 15:20:04 -060036#include "shader/programopt.h"
Keith Whitwell15e75e02005-04-29 15:11:39 +000037#include "texenvprogram.h"
38
Brian Paule998c342006-10-29 21:17:18 +000039/**
Brian Paul3f99f502008-09-25 18:40:16 -060040 * Up to nine instructions per tex unit, plus fog, specular color.
Brian Paule998c342006-10-29 21:17:18 +000041 */
Brian Paul3f99f502008-09-25 18:40:16 -060042#define MAX_INSTRUCTIONS ((MAX_TEXTURE_UNITS * 9) + 12)
Keith Whitwell15e75e02005-04-29 15:11:39 +000043
Keith Whitwell269e3892005-05-12 10:28:43 +000044#define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
Keith Whitwell15e75e02005-04-29 15:11:39 +000045
Keith Whitwellce721142005-07-11 10:10:38 +000046struct mode_opt {
Brian Paul5d7b49f2005-11-19 23:12:20 +000047 GLuint Source:4;
48 GLuint Operand:3;
Keith Whitwellce721142005-07-11 10:10:38 +000049};
50
51struct state_key {
Brian Paul5d7b49f2005-11-19 23:12:20 +000052 GLbitfield enabled_units;
53 GLuint separate_specular:1;
54 GLuint fog_enabled:1;
55 GLuint fog_mode:2;
Keith Whitwellce721142005-07-11 10:10:38 +000056
57 struct {
Brian Paul5d7b49f2005-11-19 23:12:20 +000058 GLuint enabled:1;
59 GLuint source_index:3; /* one of TEXTURE_1D/2D/3D/CUBE/RECT_INDEX */
60 GLuint ScaleShiftRGB:2;
61 GLuint ScaleShiftA:2;
Keith Whitwellce721142005-07-11 10:10:38 +000062
Brian Paul5d7b49f2005-11-19 23:12:20 +000063 GLuint NumArgsRGB:2;
64 GLuint ModeRGB:4;
Keith Whitwellce721142005-07-11 10:10:38 +000065 struct mode_opt OptRGB[3];
66
Brian Paul5d7b49f2005-11-19 23:12:20 +000067 GLuint NumArgsA:2;
68 GLuint ModeA:4;
Keith Whitwellce721142005-07-11 10:10:38 +000069 struct mode_opt OptA[3];
70 } unit[8];
71};
72
73#define FOG_LINEAR 0
74#define FOG_EXP 1
75#define FOG_EXP2 2
76#define FOG_UNKNOWN 3
77
78static GLuint translate_fog_mode( GLenum mode )
79{
80 switch (mode) {
81 case GL_LINEAR: return FOG_LINEAR;
82 case GL_EXP: return FOG_EXP;
83 case GL_EXP2: return FOG_EXP2;
84 default: return FOG_UNKNOWN;
85 }
86}
87
88#define OPR_SRC_COLOR 0
89#define OPR_ONE_MINUS_SRC_COLOR 1
90#define OPR_SRC_ALPHA 2
91#define OPR_ONE_MINUS_SRC_ALPHA 3
92#define OPR_ZERO 4
93#define OPR_ONE 5
94#define OPR_UNKNOWN 7
95
96static GLuint translate_operand( GLenum operand )
97{
98 switch (operand) {
99 case GL_SRC_COLOR: return OPR_SRC_COLOR;
100 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
101 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
102 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
103 case GL_ZERO: return OPR_ZERO;
104 case GL_ONE: return OPR_ONE;
105 default: return OPR_UNKNOWN;
106 }
107}
108
109#define SRC_TEXTURE 0
110#define SRC_TEXTURE0 1
111#define SRC_TEXTURE1 2
112#define SRC_TEXTURE2 3
113#define SRC_TEXTURE3 4
114#define SRC_TEXTURE4 5
115#define SRC_TEXTURE5 6
116#define SRC_TEXTURE6 7
117#define SRC_TEXTURE7 8
118#define SRC_CONSTANT 9
119#define SRC_PRIMARY_COLOR 10
120#define SRC_PREVIOUS 11
121#define SRC_UNKNOWN 15
122
123static GLuint translate_source( GLenum src )
124{
125 switch (src) {
126 case GL_TEXTURE: return SRC_TEXTURE;
127 case GL_TEXTURE0:
128 case GL_TEXTURE1:
129 case GL_TEXTURE2:
130 case GL_TEXTURE3:
131 case GL_TEXTURE4:
132 case GL_TEXTURE5:
133 case GL_TEXTURE6:
134 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
135 case GL_CONSTANT: return SRC_CONSTANT;
136 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
137 case GL_PREVIOUS: return SRC_PREVIOUS;
138 default: return SRC_UNKNOWN;
139 }
140}
141
142#define MODE_REPLACE 0
143#define MODE_MODULATE 1
144#define MODE_ADD 2
145#define MODE_ADD_SIGNED 3
146#define MODE_INTERPOLATE 4
147#define MODE_SUBTRACT 5
148#define MODE_DOT3_RGB 6
149#define MODE_DOT3_RGB_EXT 7
150#define MODE_DOT3_RGBA 8
151#define MODE_DOT3_RGBA_EXT 9
152#define MODE_MODULATE_ADD_ATI 10
153#define MODE_MODULATE_SIGNED_ADD_ATI 11
154#define MODE_MODULATE_SUBTRACT_ATI 12
155#define MODE_UNKNOWN 15
156
157static GLuint translate_mode( GLenum mode )
158{
159 switch (mode) {
160 case GL_REPLACE: return MODE_REPLACE;
161 case GL_MODULATE: return MODE_MODULATE;
162 case GL_ADD: return MODE_ADD;
163 case GL_ADD_SIGNED: return MODE_ADD_SIGNED;
164 case GL_INTERPOLATE: return MODE_INTERPOLATE;
165 case GL_SUBTRACT: return MODE_SUBTRACT;
166 case GL_DOT3_RGB: return MODE_DOT3_RGB;
167 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
168 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
169 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
170 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
171 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
172 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
173 default: return MODE_UNKNOWN;
174 }
175}
176
177#define TEXTURE_UNKNOWN_INDEX 7
Brian Paule00ac112005-09-15 05:00:45 +0000178static GLuint translate_tex_src_bit( GLbitfield bit )
Keith Whitwellce721142005-07-11 10:10:38 +0000179{
180 switch (bit) {
181 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
182 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
183 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
184 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
185 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
186 default: return TEXTURE_UNKNOWN_INDEX;
187 }
188}
189
Brian Paul22db5352005-11-19 23:45:10 +0000190/**
191 * Examine current texture environment state and generate a unique
192 * key to identify it.
193 */
Keith Whitwell8065c122006-05-22 16:09:27 +0000194static void make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000195{
Keith Whitwellce721142005-07-11 10:10:38 +0000196 GLuint i, j;
197
Keith Whitwell8065c122006-05-22 16:09:27 +0000198 memset(key, 0, sizeof(*key));
199
Keith Whitwellce721142005-07-11 10:10:38 +0000200 for (i=0;i<MAX_TEXTURE_UNITS;i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000201 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Keith Whitwellce721142005-07-11 10:10:38 +0000202
203 if (!texUnit->_ReallyEnabled)
204 continue;
205
206 key->unit[i].enabled = 1;
207 key->enabled_units |= (1<<i);
208
209 key->unit[i].source_index =
210 translate_tex_src_bit(texUnit->_ReallyEnabled);
211
212 key->unit[i].NumArgsRGB = texUnit->_CurrentCombine->_NumArgsRGB;
213 key->unit[i].NumArgsA = texUnit->_CurrentCombine->_NumArgsA;
214
215 key->unit[i].ModeRGB =
216 translate_mode(texUnit->_CurrentCombine->ModeRGB);
217 key->unit[i].ModeA =
218 translate_mode(texUnit->_CurrentCombine->ModeA);
219
220 key->unit[i].ScaleShiftRGB = texUnit->_CurrentCombine->ScaleShiftRGB;
Keith Whitwell64da1612006-05-22 14:30:58 +0000221 key->unit[i].ScaleShiftA = texUnit->_CurrentCombine->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000222
223 for (j=0;j<3;j++) {
224 key->unit[i].OptRGB[j].Operand =
225 translate_operand(texUnit->_CurrentCombine->OperandRGB[j]);
226 key->unit[i].OptA[j].Operand =
227 translate_operand(texUnit->_CurrentCombine->OperandA[j]);
228 key->unit[i].OptRGB[j].Source =
229 translate_source(texUnit->_CurrentCombine->SourceRGB[j]);
230 key->unit[i].OptA[j].Source =
231 translate_source(texUnit->_CurrentCombine->SourceA[j]);
232 }
233 }
234
235 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
236 key->separate_specular = 1;
237
238 if (ctx->Fog.Enabled) {
239 key->fog_enabled = 1;
240 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
241 }
Keith Whitwellce721142005-07-11 10:10:38 +0000242}
243
Keith Whitwell15e75e02005-04-29 15:11:39 +0000244/* Use uregs to represent registers internally, translate to Mesa's
245 * expected formats on emit.
246 *
247 * NOTE: These are passed by value extensively in this file rather
248 * than as usual by pointer reference. If this disturbs you, try
249 * remembering they are just 32bits in size.
250 *
251 * GCC is smart enough to deal with these dword-sized structures in
252 * much the same way as if I had defined them as dwords and was using
253 * macros to access and set the fields. This is much nicer and easier
254 * to evolve.
255 */
256struct ureg {
257 GLuint file:4;
258 GLuint idx:8;
259 GLuint negatebase:1;
260 GLuint abs:1;
261 GLuint negateabs:1;
262 GLuint swz:12;
263 GLuint pad:5;
264};
265
Brian Paul13abf912006-04-13 19:17:13 +0000266static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000267 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000268 ~0,
269 0,
270 0,
271 0,
272 0,
273 0
274};
275
Keith Whitwell15e75e02005-04-29 15:11:39 +0000276
Keith Whitwell15e75e02005-04-29 15:11:39 +0000277/* State used to build the fragment program:
278 */
279struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000280 struct gl_fragment_program *program;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000281 GLcontext *ctx;
Keith Whitwellce721142005-07-11 10:10:38 +0000282 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000283
Brian Paulc8d17412005-12-14 03:06:16 +0000284 GLbitfield alu_temps; /* Track texture indirections, see spec. */
285 GLbitfield temps_output; /* Track texture indirections, see spec. */
286 GLbitfield temp_in_use; /* Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000287 GLboolean error;
288
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000289 struct ureg src_texture[MAX_TEXTURE_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000290 /* Reg containing each texture unit's sampled texture color,
291 * else undef.
292 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000293
294 struct ureg src_previous; /* Reg containing color from previous
295 * stage. May need to be decl'd.
296 */
297
298 GLuint last_tex_stage; /* Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000299
300 struct ureg half;
301 struct ureg one;
302 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000303};
304
305
306
307static struct ureg make_ureg(GLuint file, GLuint idx)
308{
309 struct ureg reg;
310 reg.file = file;
311 reg.idx = idx;
312 reg.negatebase = 0;
313 reg.abs = 0;
314 reg.negateabs = 0;
315 reg.swz = SWIZZLE_NOOP;
316 reg.pad = 0;
317 return reg;
318}
319
320static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
321{
322 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
323 GET_SWZ(reg.swz, y),
324 GET_SWZ(reg.swz, z),
325 GET_SWZ(reg.swz, w));
326
327 return reg;
328}
329
330static struct ureg swizzle1( struct ureg reg, int x )
331{
332 return swizzle(reg, x, x, x, x);
333}
334
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000335static struct ureg negate( struct ureg reg )
336{
337 reg.negatebase ^= 1;
338 return reg;
339}
340
Keith Whitwell15e75e02005-04-29 15:11:39 +0000341static GLboolean is_undef( struct ureg reg )
342{
Alan Hourihane8d972652006-08-10 13:12:00 +0000343 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000344}
345
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000346
Keith Whitwell15e75e02005-04-29 15:11:39 +0000347static struct ureg get_temp( struct texenv_fragment_program *p )
348{
Brian Paul13abf912006-04-13 19:17:13 +0000349 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000350
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000351 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000352 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000353 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000354
355 /* Then any unused temporary:
356 */
357 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000358 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000359
Keith Whitwell15e75e02005-04-29 15:11:39 +0000360 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000361 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000362 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000363 }
364
Brian Paul13abf912006-04-13 19:17:13 +0000365 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000366 p->program->Base.NumTemporaries = bit;
367
Keith Whitwell93cd9232005-05-11 08:30:23 +0000368 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000369 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
370}
371
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000372static struct ureg get_tex_temp( struct texenv_fragment_program *p )
373{
374 int bit;
375
Brian Paul006fb632008-09-25 18:27:22 -0600376 /* First try to find available temp not previously used (to avoid
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000377 * starting a new texture indirection). According to the spec, the
378 * ~p->temps_output isn't necessary, but will keep it there for
379 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000380 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000381 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000382
383 /* Then any unused temporary:
384 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000385 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000386 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000387
388 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000389 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000390 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000391 }
392
Brian Paul13abf912006-04-13 19:17:13 +0000393 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000394 p->program->Base.NumTemporaries = bit;
395
Keith Whitwell93cd9232005-05-11 08:30:23 +0000396 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000397 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
398}
399
Keith Whitwell15e75e02005-04-29 15:11:39 +0000400
Brianf18d4e02007-10-29 12:25:46 -0600401static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000402{
Brianf18d4e02007-10-29 12:25:46 -0600403 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000404
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000405 /* KW: To support tex_env_crossbar, don't release the registers in
406 * temps_output.
407 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000408 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000409 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000410 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000411 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000412}
413
414
Brian9fe3e2e2007-02-23 11:44:14 -0700415static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000416 GLint s0,
417 GLint s1,
418 GLint s2,
419 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700420 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000421{
Brian9fe3e2e2007-02-23 11:44:14 -0700422 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000423 GLuint idx;
424 tokens[0] = s0;
425 tokens[1] = s1;
426 tokens[2] = s2;
427 tokens[3] = s3;
428 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000429 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000430 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000431}
432
Keith Whitwell47b29f52005-05-04 11:44:44 +0000433
Brian9fe3e2e2007-02-23 11:44:14 -0700434#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
435#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
436#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
437#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000438
439
440static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
441{
Brian Paulde997602005-11-12 17:53:14 +0000442 p->program->Base.InputsRead |= (1 << input);
Keith Whitwell47b29f52005-05-04 11:44:44 +0000443 return make_ureg(PROGRAM_INPUT, input);
444}
445
446
Brian Paul7e807512005-11-05 17:10:45 +0000447static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000448 struct ureg ureg )
449{
450 reg->File = ureg.file;
451 reg->Index = ureg.idx;
452 reg->Swizzle = ureg.swz;
Keith Whitwellf2802c42005-10-07 09:55:26 +0000453 reg->NegateBase = ureg.negatebase ? 0xf : 0x0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000454 reg->Abs = ureg.abs;
455 reg->NegateAbs = ureg.negateabs;
456}
457
Brian Paul7e807512005-11-05 17:10:45 +0000458static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000459 struct ureg ureg, GLuint mask )
460{
461 dst->File = ureg.file;
462 dst->Index = ureg.idx;
463 dst->WriteMask = mask;
Brian112a1582007-10-23 10:54:50 -0600464 dst->CondMask = COND_TR; /* always pass cond test */
465 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000466}
467
Brian Paul7e807512005-11-05 17:10:45 +0000468static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000469emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000470 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000471 struct ureg dest,
472 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000473 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000474 struct ureg src0,
475 struct ureg src1,
476 struct ureg src2 )
477{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000478 GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000479 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000480
Briane69943e2007-10-23 10:23:01 -0600481 assert(nr < MAX_INSTRUCTIONS);
482
Brian Pauld6272e02006-10-29 18:03:16 +0000483 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000484 inst->Opcode = op;
485
Keith Whitwell47b29f52005-05-04 11:44:44 +0000486 emit_arg( &inst->SrcReg[0], src0 );
487 emit_arg( &inst->SrcReg[1], src1 );
488 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000489
Brian Paule31ac052005-11-20 17:52:40 +0000490 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000491
492 emit_dst( &inst->DstReg, dest, mask );
493
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000494 /* Accounting for indirection tracking:
495 */
496 if (dest.file == PROGRAM_TEMPORARY)
497 p->temps_output |= 1 << dest.idx;
498
Keith Whitwell15e75e02005-04-29 15:11:39 +0000499 return inst;
500}
501
502
503static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000504 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000505 struct ureg dest,
506 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000507 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000508 struct ureg src0,
509 struct ureg src1,
510 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000511{
512 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
513
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000514 /* Accounting for indirection tracking:
515 */
516 if (src0.file == PROGRAM_TEMPORARY)
517 p->alu_temps |= 1 << src0.idx;
518
519 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
520 p->alu_temps |= 1 << src1.idx;
521
522 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
523 p->alu_temps |= 1 << src2.idx;
524
525 if (dest.file == PROGRAM_TEMPORARY)
526 p->alu_temps |= 1 << dest.idx;
527
Brian21f99792007-01-09 11:00:21 -0700528 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000529 return dest;
530}
531
532static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000533 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000534 struct ureg dest,
535 GLuint destmask,
536 GLuint tex_unit,
537 GLuint tex_idx,
538 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000539{
Brian Paul7e807512005-11-05 17:10:45 +0000540 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000541 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000542 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000543 coord, /* arg 0? */
544 undef,
545 undef);
546
Brian Paul7e807512005-11-05 17:10:45 +0000547 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000548 inst->TexSrcUnit = tex_unit;
549
Brian21f99792007-01-09 11:00:21 -0700550 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000551
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000552 /* Is this a texture indirection?
553 */
554 if ((coord.file == PROGRAM_TEMPORARY &&
555 (p->temps_output & (1<<coord.idx))) ||
556 (dest.file == PROGRAM_TEMPORARY &&
557 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700558 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000559 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000560 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000561 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000562 }
563
564 return dest;
565}
566
567
Keith Whitwell47b29f52005-05-04 11:44:44 +0000568static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000569 GLfloat s0,
570 GLfloat s1,
571 GLfloat s2,
572 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000573{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000574 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700575 GLuint idx, swizzle;
Brian Paul006fb632008-09-25 18:27:22 -0600576 struct ureg r;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000577 values[0] = s0;
578 values[1] = s1;
579 values[2] = s2;
580 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700581 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
582 &swizzle );
Brian Paul006fb632008-09-25 18:27:22 -0600583 r = make_ureg(PROGRAM_CONSTANT, idx);
584 r.swz = swizzle;
585 return r;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000586}
587
Keith Whitwelle4902422005-05-11 15:16:35 +0000588#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000589#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
590#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
591#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000592
593
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000594static struct ureg get_one( struct texenv_fragment_program *p )
595{
596 if (is_undef(p->one))
597 p->one = register_scalar_const(p, 1.0);
598 return p->one;
599}
600
601static struct ureg get_half( struct texenv_fragment_program *p )
602{
603 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000604 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000605 return p->half;
606}
607
608static struct ureg get_zero( struct texenv_fragment_program *p )
609{
610 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000611 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000612 return p->zero;
613}
614
Keith Whitwell15e75e02005-04-29 15:11:39 +0000615
Keith Whitwell15e75e02005-04-29 15:11:39 +0000616static void program_error( struct texenv_fragment_program *p, const char *msg )
617{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000618 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000619 p->error = 1;
620}
621
Keith Whitwell15e75e02005-04-29 15:11:39 +0000622static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000623 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000624{
625 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000626 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000627 assert(!is_undef(p->src_texture[unit]));
628 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000629
Keith Whitwellce721142005-07-11 10:10:38 +0000630 case SRC_TEXTURE0:
631 case SRC_TEXTURE1:
632 case SRC_TEXTURE2:
633 case SRC_TEXTURE3:
634 case SRC_TEXTURE4:
635 case SRC_TEXTURE5:
636 case SRC_TEXTURE6:
637 case SRC_TEXTURE7:
638 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
639 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000640
Keith Whitwellce721142005-07-11 10:10:38 +0000641 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000642 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000643
Keith Whitwellce721142005-07-11 10:10:38 +0000644 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000645 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000646
Keith Whitwellce721142005-07-11 10:10:38 +0000647 case SRC_PREVIOUS:
648 default:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000649 if (is_undef(p->src_previous))
650 return register_input(p, FRAG_ATTRIB_COL0);
651 else
652 return p->src_previous;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000653 }
654}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000655
Keith Whitwell15e75e02005-04-29 15:11:39 +0000656static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000657 GLuint mask,
658 GLuint unit,
659 GLuint source,
660 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000661{
662 struct ureg arg, src, one;
663
664 src = get_source(p, source, unit);
665
666 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000667 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000668 /* Get unused tmp,
669 * Emit tmp = 1.0 - arg.xyzw
670 */
671 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000672 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000673 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000674
Keith Whitwellce721142005-07-11 10:10:38 +0000675 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000676 if (mask == WRITEMASK_W)
677 return src;
678 else
Brian Paul22db5352005-11-19 23:45:10 +0000679 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000680 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000681 /* Get unused tmp,
682 * Emit tmp = 1.0 - arg.wwww
683 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000684 arg = get_temp(p);
685 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000686 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000687 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000688 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000689 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000690 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000691 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000692 case OPR_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000693 default:
694 return src;
695 }
696}
697
Keith Whitwellce721142005-07-11 10:10:38 +0000698static GLboolean args_match( struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000699{
Brian Paul22db5352005-11-19 23:45:10 +0000700 GLuint i, nr = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000701
702 for (i = 0 ; i < nr ; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000703 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000704 return GL_FALSE;
705
Keith Whitwellce721142005-07-11 10:10:38 +0000706 switch(key->unit[unit].OptA[i].Operand) {
707 case OPR_SRC_ALPHA:
708 switch(key->unit[unit].OptRGB[i].Operand) {
709 case OPR_SRC_COLOR:
710 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000711 break;
712 default:
713 return GL_FALSE;
714 }
715 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000716 case OPR_ONE_MINUS_SRC_ALPHA:
717 switch(key->unit[unit].OptRGB[i].Operand) {
718 case OPR_ONE_MINUS_SRC_COLOR:
719 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000720 break;
721 default:
722 return GL_FALSE;
723 }
724 break;
725 default:
726 return GL_FALSE; /* impossible */
727 }
728 }
729
730 return GL_TRUE;
731}
732
Keith Whitwell15e75e02005-04-29 15:11:39 +0000733static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000734 struct ureg dest,
735 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000736 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000737 GLuint unit,
738 GLuint nr,
739 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +0000740 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000741{
Keith Whitwell15e75e02005-04-29 15:11:39 +0000742 struct ureg src[3];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000743 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +0000744 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000745
Brian Paul00630842005-12-12 15:27:55 +0000746 tmp = undef; /* silence warning (bug 5318) */
747
Keith Whitwell15e75e02005-04-29 15:11:39 +0000748 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +0000749 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000750
751 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +0000752 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000753 if (mask == WRITEMASK_XYZW && !saturate)
754 return src[0];
755 else
Brian Paul7e807512005-11-05 17:10:45 +0000756 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000757 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +0000758 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000759 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000760 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +0000761 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000762 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000763 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000764 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000765 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +0000766 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000767 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +0000768 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +0000769 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
770 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000771 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +0000772 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000773 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
774 */
Brian Paul7e807512005-11-05 17:10:45 +0000775 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000776
Keith Whitwellce721142005-07-11 10:10:38 +0000777 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +0000778 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000779
Keith Whitwellce721142005-07-11 10:10:38 +0000780 case MODE_DOT3_RGBA:
781 case MODE_DOT3_RGBA_EXT:
782 case MODE_DOT3_RGB_EXT:
783 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000784 struct ureg tmp0 = get_temp( p );
785 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +0000786 struct ureg neg1 = register_scalar_const(p, -1);
787 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000788
789 /* tmp0 = 2*src0 - 1
790 * tmp1 = 2*src1 - 1
791 *
792 * dst = tmp0 dot3 tmp1
793 */
Brian Paul7e807512005-11-05 17:10:45 +0000794 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000795 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000796
Brian Paulaa206952005-09-16 18:14:24 +0000797 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000798 tmp1 = tmp0;
799 else
Brian Paul7e807512005-11-05 17:10:45 +0000800 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000801 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +0000802 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000803 return dest;
804 }
Keith Whitwellce721142005-07-11 10:10:38 +0000805 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000806 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +0000807 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000808 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +0000809 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000810 /* Arg0 * Arg2 + Arg1 - 0.5 */
811 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000812 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +0000813 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
814 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000815 return dest;
816 }
Keith Whitwellce721142005-07-11 10:10:38 +0000817 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000818 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +0000819 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000820 return dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000821 default:
822 return src[0];
823 }
824}
825
Keith Whitwell15e75e02005-04-29 15:11:39 +0000826
Brian Paul22db5352005-11-19 23:45:10 +0000827/**
828 * Generate instructions for one texture unit's env/combiner mode.
829 */
830static struct ureg
831emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000832{
Keith Whitwellce721142005-07-11 10:10:38 +0000833 struct state_key *key = p->state;
Brian Paul22db5352005-11-19 23:45:10 +0000834 GLboolean saturate = (unit < p->last_tex_stage);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000835 GLuint rgb_shift, alpha_shift;
836 struct ureg out, shift;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000837 struct ureg dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000838
Keith Whitwellce721142005-07-11 10:10:38 +0000839 if (!key->unit[unit].enabled) {
840 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000841 }
Keith Whitwellce721142005-07-11 10:10:38 +0000842
843 switch (key->unit[unit].ModeRGB) {
844 case MODE_DOT3_RGB_EXT:
845 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000846 rgb_shift = 0;
847 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000848 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000849 alpha_shift = 0;
850 rgb_shift = 0;
851 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000852 default:
Keith Whitwellce721142005-07-11 10:10:38 +0000853 rgb_shift = key->unit[unit].ScaleShiftRGB;
854 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000855 break;
856 }
Keith Whitwellce721142005-07-11 10:10:38 +0000857
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000858 /* If this is the very last calculation, emit direct to output reg:
859 */
Keith Whitwellce721142005-07-11 10:10:38 +0000860 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000861 unit != p->last_tex_stage ||
862 alpha_shift ||
863 rgb_shift)
864 dest = get_temp( p );
865 else
Brian Paul90ebb582005-11-02 18:06:12 +0000866 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLR);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000867
868 /* Emit the RGB and A combine ops
869 */
Keith Whitwellce721142005-07-11 10:10:38 +0000870 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
871 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000872 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
873 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000874 key->unit[unit].NumArgsRGB,
875 key->unit[unit].ModeRGB,
876 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000877 }
Keith Whitwellce721142005-07-11 10:10:38 +0000878 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +0200879 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000880
881 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
882 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000883 key->unit[unit].NumArgsRGB,
884 key->unit[unit].ModeRGB,
885 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000886 }
887 else {
888 /* Need to do something to stop from re-emitting identical
889 * argument calculations here:
890 */
891 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
892 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000893 key->unit[unit].NumArgsRGB,
894 key->unit[unit].ModeRGB,
895 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000896 out = emit_combine( p, dest, WRITEMASK_W, saturate,
897 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000898 key->unit[unit].NumArgsA,
899 key->unit[unit].ModeA,
900 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000901 }
902
903 /* Deal with the final shift:
904 */
905 if (alpha_shift || rgb_shift) {
906 if (rgb_shift == alpha_shift) {
José Fonseca53174af2008-05-31 18:14:09 +0900907 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +0000908 }
909 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000910 shift = register_const4f(p,
José Fonseca53174af2008-05-31 18:14:09 +0900911 (GLfloat)(1<<rgb_shift),
912 (GLfloat)(1<<rgb_shift),
913 (GLfloat)(1<<rgb_shift),
914 (GLfloat)(1<<alpha_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +0000915 }
Brian Paul7e807512005-11-05 17:10:45 +0000916 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000917 saturate, out, shift, undef );
918 }
919 else
920 return out;
921}
922
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000923
Brian Paul22db5352005-11-19 23:45:10 +0000924/**
925 * Generate instruction for getting a texture source term.
926 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000927static void load_texture( struct texenv_fragment_program *p, GLuint unit )
928{
929 if (is_undef(p->src_texture[unit])) {
Keith Whitwellce721142005-07-11 10:10:38 +0000930 GLuint dim = p->state->unit[unit].source_index;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000931 struct ureg texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
932 struct ureg tmp = get_tex_temp( p );
933
Brian Paulbfb5ea32005-07-19 18:20:04 +0000934 if (dim == TEXTURE_UNKNOWN_INDEX)
935 program_error(p, "TexSrcBit");
Keith Whitwellce721142005-07-11 10:10:38 +0000936
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000937 /* TODO: Use D0_MASK_XY where possible.
938 */
Brian1e3b07f2007-12-14 11:16:49 -0700939 if (p->state->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +0000940 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
941 tmp, WRITEMASK_XYZW,
942 unit, dim, texcoord );
Brian1e3b07f2007-12-14 11:16:49 -0700943 p->program->Base.SamplersUsed |= (1 << unit);
Brianfce46122007-12-14 11:42:28 -0700944 /* This identity mapping should already be in place
945 * (see _mesa_init_program_struct()) but let's be safe.
946 */
947 p->program->Base.SamplerUnits[unit] = unit;
Brian1e3b07f2007-12-14 11:16:49 -0700948 }
Aapo Tahkolab8915342006-03-28 10:26:34 +0000949 else
950 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000951 }
952}
953
Keith Whitwell241b6b72005-05-23 09:50:34 +0000954static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000955 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000956{
957 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000958 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000959 load_texture(p, unit);
960 break;
961
Keith Whitwellce721142005-07-11 10:10:38 +0000962 case SRC_TEXTURE0:
963 case SRC_TEXTURE1:
964 case SRC_TEXTURE2:
965 case SRC_TEXTURE3:
966 case SRC_TEXTURE4:
967 case SRC_TEXTURE5:
968 case SRC_TEXTURE6:
969 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +0000970 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000971 break;
972
973 default:
974 break;
975 }
Keith Whitwell241b6b72005-05-23 09:50:34 +0000976
977 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000978}
979
Brian Paul22db5352005-11-19 23:45:10 +0000980
981/**
982 * Generate instructions for loading all texture source terms.
983 */
984static GLboolean
985load_texunit_sources( struct texenv_fragment_program *p, int unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000986{
Keith Whitwellce721142005-07-11 10:10:38 +0000987 struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +0000988 GLuint i;
989
990 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
991 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000992 }
Aapo Tahkolab8915342006-03-28 10:26:34 +0000993
994 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
995 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
996 }
997
Keith Whitwell241b6b72005-05-23 09:50:34 +0000998 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000999}
1000
Brian Paul22db5352005-11-19 23:45:10 +00001001
1002/**
1003 * Generate a new fragment program which implements the context's
1004 * current texture env/combine mode.
1005 */
1006static void
Brian Paule998c342006-10-29 21:17:18 +00001007create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001008 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001009{
Brian Paule998c342006-10-29 21:17:18 +00001010 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001011 struct texenv_fragment_program p;
1012 GLuint unit;
1013 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001014
Keith Whitwelle4902422005-05-11 15:16:35 +00001015 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +00001016 p.ctx = ctx;
Keith Whitwellce721142005-07-11 10:10:38 +00001017 p.state = key;
1018 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001019
Brian Paule998c342006-10-29 21:17:18 +00001020 /* During code generation, use locally-allocated instruction buffer,
1021 * then alloc dynamic storage below.
1022 */
1023 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001024 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Brian21f99792007-01-09 11:00:21 -07001025 p.program->Base.NumTexIndirections = 1; /* correct? */
1026 p.program->Base.NumTexInstructions = 0;
1027 p.program->Base.NumAluInstructions = 0;
Brian1240eb22007-03-22 08:50:20 -06001028 p.program->Base.String = NULL;
Keith Whitwell9ca88152005-05-10 09:56:02 +00001029 p.program->Base.NumInstructions =
Brian Paule998c342006-10-29 21:17:18 +00001030 p.program->Base.NumTemporaries =
1031 p.program->Base.NumParameters =
1032 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001033 p.program->Base.Parameters = _mesa_new_parameter_list();
Keith Whitwelldbeea252005-05-10 13:57:50 +00001034
Brian Paulde997602005-11-12 17:53:14 +00001035 p.program->Base.InputsRead = 0;
1036 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001037
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001038 for (unit = 0; unit < MAX_TEXTURE_UNITS; unit++)
1039 p.src_texture[unit] = undef;
1040
Keith Whitwell47b29f52005-05-04 11:44:44 +00001041 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001042 p.half = undef;
1043 p.zero = undef;
1044 p.one = undef;
1045
Keith Whitwell15e75e02005-04-29 15:11:39 +00001046 p.last_tex_stage = 0;
Brianf18d4e02007-10-29 12:25:46 -06001047 release_temps(ctx, &p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001048
Keith Whitwellce721142005-07-11 10:10:38 +00001049 if (key->enabled_units) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001050 /* First pass - to support texture_env_crossbar, first identify
1051 * all referenced texture sources and emit texld instructions
1052 * for each:
1053 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001054 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001055 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001056 load_texunit_sources( &p, unit );
1057 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001058 }
1059
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001060 /* Second pass - emit combine instructions to build final color:
1061 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001062 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001063 if (key->enabled_units & (1<<unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001064 p.src_previous = emit_texenv( &p, unit );
Brianf18d4e02007-10-29 12:25:46 -06001065 release_temps(ctx, &p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001066 }
1067 }
1068
Keith Whitwellce721142005-07-11 10:10:38 +00001069 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul90ebb582005-11-02 18:06:12 +00001070 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001071
Keith Whitwellce721142005-07-11 10:10:38 +00001072 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001073 /* Emit specular add.
1074 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001075 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001076 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1077 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001078 }
Brian Paulaa206952005-09-16 18:14:24 +00001079 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001080 /* Will wind up in here if no texture enabled or a couple of
1081 * other scenarios (GL_REPLACE for instance).
1082 */
Brian Paul7e807512005-11-05 17:10:45 +00001083 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001084 }
1085
Keith Whitwell47b29f52005-05-04 11:44:44 +00001086 /* Finish up:
1087 */
Brian Paul7e807512005-11-05 17:10:45 +00001088 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001089
Keith Whitwellce721142005-07-11 10:10:38 +00001090 if (key->fog_enabled) {
1091 /* Pull fog mode from GLcontext, the value in the state key is
1092 * a reduced value and not what is expected in FogOption
1093 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001094 p.program->FogOption = ctx->Fog.Mode;
Brian63be96b2007-09-18 19:29:26 -06001095 p.program->Base.InputsRead |= FRAG_BIT_FOGC; /* XXX new */
Keith Whitwellce721142005-07-11 10:10:38 +00001096 } else
Keith Whitwell276330b2005-05-09 17:58:13 +00001097 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001098
Brian21f99792007-01-09 11:00:21 -07001099 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001100 program_error(&p, "Exceeded max nr indirect texture lookups");
1101
Brian21f99792007-01-09 11:00:21 -07001102 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001103 program_error(&p, "Exceeded max TEX instructions");
1104
Brian21f99792007-01-09 11:00:21 -07001105 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001106 program_error(&p, "Exceeded max ALU instructions");
1107
Brian Paul5d7b49f2005-11-19 23:12:20 +00001108 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001109
Brian Paule998c342006-10-29 21:17:18 +00001110 /* Allocate final instruction array */
Brian3bf8d2a2007-09-25 15:18:51 -06001111 p.program->Base.Instructions
1112 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1113 if (!p.program->Base.Instructions) {
Brian Paule998c342006-10-29 21:17:18 +00001114 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1115 "generating tex env program");
1116 return;
1117 }
Brian3bf8d2a2007-09-25 15:18:51 -06001118 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1119 p.program->Base.NumInstructions);
1120
1121 if (p.program->FogOption) {
1122 _mesa_append_fog_code(ctx, p.program);
1123 p.program->FogOption = GL_NONE;
1124 }
1125
Brian Paule998c342006-10-29 21:17:18 +00001126
Keith Whitwelldbeea252005-05-10 13:57:50 +00001127 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001128 */
Brian Paule998c342006-10-29 21:17:18 +00001129 if (ctx->Driver.ProgramStringNotify) {
1130 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1131 &p.program->Base );
1132 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001133
Brian Paule998c342006-10-29 21:17:18 +00001134 if (DISASSEM) {
1135 _mesa_print_program(&p.program->Base);
1136 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001137 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001138}
1139
Brian Paul5d7b49f2005-11-19 23:12:20 +00001140
Briana90046f2006-12-15 10:07:26 -07001141/**
Brian83ce6c52007-10-29 11:23:02 -06001142 * Return a fragment program which implements the current
1143 * fixed-function texture, fog and color-sum operations.
1144 */
1145struct gl_fragment_program *
1146_mesa_get_fixed_func_fragment_program(GLcontext *ctx)
1147{
1148 struct gl_fragment_program *prog;
1149 struct state_key key;
Brian83ce6c52007-10-29 11:23:02 -06001150
1151 make_state_key(ctx, &key);
Brian83ce6c52007-10-29 11:23:02 -06001152
Brianf4a5ea22007-10-31 12:45:32 -06001153 prog = (struct gl_fragment_program *)
1154 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1155 &key, sizeof(key));
Brian83ce6c52007-10-29 11:23:02 -06001156
1157 if (!prog) {
Brian83ce6c52007-10-29 11:23:02 -06001158 prog = (struct gl_fragment_program *)
1159 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1160
1161 create_new_program(ctx, &key, prog);
1162
Brianf4a5ea22007-10-31 12:45:32 -06001163 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1164 &key, sizeof(key), &prog->Base);
Brian83ce6c52007-10-29 11:23:02 -06001165 }
1166
1167 return prog;
1168}
1169
1170
1171
1172/**
Briana90046f2006-12-15 10:07:26 -07001173 * If _MaintainTexEnvProgram is set we'll generate a fragment program that
1174 * implements the current texture env/combine mode.
1175 * This function generates that program and puts it into effect.
1176 */
1177void
1178_mesa_UpdateTexEnvProgram( GLcontext *ctx )
Keith Whitwellce721142005-07-11 10:10:38 +00001179{
Brian Paul122629f2006-07-20 16:49:57 +00001180 const struct gl_fragment_program *prev = ctx->FragmentProgram._Current;
Keith Whitwellce721142005-07-11 10:10:38 +00001181
Briana90046f2006-12-15 10:07:26 -07001182 ASSERT(ctx->FragmentProgram._MaintainTexEnvProgram);
1183
1184 /* If a conventional fragment program/shader isn't in effect... */
Brianefcfdbd2007-02-24 15:51:41 -07001185 if (!ctx->FragmentProgram._Enabled &&
Briand781cdc2007-10-02 16:55:21 -06001186 (!ctx->Shader.CurrentProgram ||
1187 !ctx->Shader.CurrentProgram->FragmentProgram) ) {
Keith Whitwellce721142005-07-11 10:10:38 +00001188
Brian83ce6c52007-10-29 11:23:02 -06001189 ctx->FragmentProgram._Current
1190 = ctx->FragmentProgram._TexEnvProgram
1191 = _mesa_get_fixed_func_fragment_program(ctx);
Keith Whitwellc3626a92005-11-01 17:25:49 +00001192 }
Keith Whitwellc3626a92005-11-01 17:25:49 +00001193
1194 /* Tell the driver about the change. Could define a new target for
1195 * this?
1196 */
Brian Paul5d7b49f2005-11-19 23:12:20 +00001197 if (ctx->FragmentProgram._Current != prev && ctx->Driver.BindProgram) {
1198 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
Briana90046f2006-12-15 10:07:26 -07001199 (struct gl_program *) ctx->FragmentProgram._Current);
Brian Paul5d7b49f2005-11-19 23:12:20 +00001200 }
Keith Whitwellce721142005-07-11 10:10:38 +00001201}