blob: 97aa87e58cf18f43c2ff40f96f091301b29336e3 [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
Brian Paul8fd329d2008-09-26 11:18:06 -0600401/** Mark a temp reg as being no longer allocatable. */
402static void reserve_temp( struct texenv_fragment_program *p, struct ureg r )
403{
404 if (r.file == PROGRAM_TEMPORARY)
405 p->temps_output |= (1 << r.idx);
406}
407
408
Brianf18d4e02007-10-29 12:25:46 -0600409static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000410{
Brianf18d4e02007-10-29 12:25:46 -0600411 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000412
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000413 /* KW: To support tex_env_crossbar, don't release the registers in
414 * temps_output.
415 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000416 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000417 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000418 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000419 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000420}
421
422
Brian9fe3e2e2007-02-23 11:44:14 -0700423static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000424 GLint s0,
425 GLint s1,
426 GLint s2,
427 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700428 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000429{
Brian9fe3e2e2007-02-23 11:44:14 -0700430 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000431 GLuint idx;
432 tokens[0] = s0;
433 tokens[1] = s1;
434 tokens[2] = s2;
435 tokens[3] = s3;
436 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000437 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000438 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000439}
440
Keith Whitwell47b29f52005-05-04 11:44:44 +0000441
Brian9fe3e2e2007-02-23 11:44:14 -0700442#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
443#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
444#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
445#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000446
447
448static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
449{
Brian Paulde997602005-11-12 17:53:14 +0000450 p->program->Base.InputsRead |= (1 << input);
Keith Whitwell47b29f52005-05-04 11:44:44 +0000451 return make_ureg(PROGRAM_INPUT, input);
452}
453
454
Brian Paul7e807512005-11-05 17:10:45 +0000455static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000456 struct ureg ureg )
457{
458 reg->File = ureg.file;
459 reg->Index = ureg.idx;
460 reg->Swizzle = ureg.swz;
Keith Whitwellf2802c42005-10-07 09:55:26 +0000461 reg->NegateBase = ureg.negatebase ? 0xf : 0x0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000462 reg->Abs = ureg.abs;
463 reg->NegateAbs = ureg.negateabs;
464}
465
Brian Paul7e807512005-11-05 17:10:45 +0000466static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000467 struct ureg ureg, GLuint mask )
468{
469 dst->File = ureg.file;
470 dst->Index = ureg.idx;
471 dst->WriteMask = mask;
Brian112a1582007-10-23 10:54:50 -0600472 dst->CondMask = COND_TR; /* always pass cond test */
473 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000474}
475
Brian Paul7e807512005-11-05 17:10:45 +0000476static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000477emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000478 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000479 struct ureg dest,
480 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000481 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000482 struct ureg src0,
483 struct ureg src1,
484 struct ureg src2 )
485{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000486 GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000487 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000488
Briane69943e2007-10-23 10:23:01 -0600489 assert(nr < MAX_INSTRUCTIONS);
490
Brian Pauld6272e02006-10-29 18:03:16 +0000491 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000492 inst->Opcode = op;
493
Keith Whitwell47b29f52005-05-04 11:44:44 +0000494 emit_arg( &inst->SrcReg[0], src0 );
495 emit_arg( &inst->SrcReg[1], src1 );
496 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000497
Brian Paule31ac052005-11-20 17:52:40 +0000498 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000499
500 emit_dst( &inst->DstReg, dest, mask );
501
Brian Paul8fd329d2008-09-26 11:18:06 -0600502#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000503 /* Accounting for indirection tracking:
504 */
505 if (dest.file == PROGRAM_TEMPORARY)
506 p->temps_output |= 1 << dest.idx;
Brian Paul8fd329d2008-09-26 11:18:06 -0600507#endif
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000508
Keith Whitwell15e75e02005-04-29 15:11:39 +0000509 return inst;
510}
511
512
513static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000514 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000515 struct ureg dest,
516 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000517 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000518 struct ureg src0,
519 struct ureg src1,
520 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000521{
522 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
523
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000524 /* Accounting for indirection tracking:
525 */
526 if (src0.file == PROGRAM_TEMPORARY)
527 p->alu_temps |= 1 << src0.idx;
528
529 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
530 p->alu_temps |= 1 << src1.idx;
531
532 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
533 p->alu_temps |= 1 << src2.idx;
534
535 if (dest.file == PROGRAM_TEMPORARY)
536 p->alu_temps |= 1 << dest.idx;
537
Brian21f99792007-01-09 11:00:21 -0700538 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000539 return dest;
540}
541
542static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000543 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000544 struct ureg dest,
545 GLuint destmask,
546 GLuint tex_unit,
547 GLuint tex_idx,
548 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000549{
Brian Paul7e807512005-11-05 17:10:45 +0000550 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000551 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000552 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000553 coord, /* arg 0? */
554 undef,
555 undef);
556
Brian Paul7e807512005-11-05 17:10:45 +0000557 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000558 inst->TexSrcUnit = tex_unit;
559
Brian21f99792007-01-09 11:00:21 -0700560 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000561
Brian Paul8fd329d2008-09-26 11:18:06 -0600562 /* Accounting for indirection tracking:
563 */
564 reserve_temp(p, dest);
565
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000566 /* Is this a texture indirection?
567 */
568 if ((coord.file == PROGRAM_TEMPORARY &&
569 (p->temps_output & (1<<coord.idx))) ||
570 (dest.file == PROGRAM_TEMPORARY &&
571 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700572 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000573 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000574 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000575 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000576 }
577
578 return dest;
579}
580
581
Keith Whitwell47b29f52005-05-04 11:44:44 +0000582static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000583 GLfloat s0,
584 GLfloat s1,
585 GLfloat s2,
586 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000587{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000588 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700589 GLuint idx, swizzle;
Brian Paul006fb632008-09-25 18:27:22 -0600590 struct ureg r;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000591 values[0] = s0;
592 values[1] = s1;
593 values[2] = s2;
594 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700595 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
596 &swizzle );
Brian Paul006fb632008-09-25 18:27:22 -0600597 r = make_ureg(PROGRAM_CONSTANT, idx);
598 r.swz = swizzle;
599 return r;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000600}
601
Keith Whitwelle4902422005-05-11 15:16:35 +0000602#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000603#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
604#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
605#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000606
607
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000608static struct ureg get_one( struct texenv_fragment_program *p )
609{
610 if (is_undef(p->one))
611 p->one = register_scalar_const(p, 1.0);
612 return p->one;
613}
614
615static struct ureg get_half( struct texenv_fragment_program *p )
616{
617 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000618 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000619 return p->half;
620}
621
622static struct ureg get_zero( struct texenv_fragment_program *p )
623{
624 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000625 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000626 return p->zero;
627}
628
Keith Whitwell15e75e02005-04-29 15:11:39 +0000629
Keith Whitwell15e75e02005-04-29 15:11:39 +0000630static void program_error( struct texenv_fragment_program *p, const char *msg )
631{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000632 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000633 p->error = 1;
634}
635
Keith Whitwell15e75e02005-04-29 15:11:39 +0000636static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000637 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000638{
639 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000640 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000641 assert(!is_undef(p->src_texture[unit]));
642 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000643
Keith Whitwellce721142005-07-11 10:10:38 +0000644 case SRC_TEXTURE0:
645 case SRC_TEXTURE1:
646 case SRC_TEXTURE2:
647 case SRC_TEXTURE3:
648 case SRC_TEXTURE4:
649 case SRC_TEXTURE5:
650 case SRC_TEXTURE6:
651 case SRC_TEXTURE7:
652 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
653 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000654
Keith Whitwellce721142005-07-11 10:10:38 +0000655 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000656 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000657
Keith Whitwellce721142005-07-11 10:10:38 +0000658 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000659 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000660
Keith Whitwellce721142005-07-11 10:10:38 +0000661 case SRC_PREVIOUS:
662 default:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000663 if (is_undef(p->src_previous))
664 return register_input(p, FRAG_ATTRIB_COL0);
665 else
666 return p->src_previous;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000667 }
668}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000669
Keith Whitwell15e75e02005-04-29 15:11:39 +0000670static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000671 GLuint mask,
672 GLuint unit,
673 GLuint source,
674 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000675{
676 struct ureg arg, src, one;
677
678 src = get_source(p, source, unit);
679
680 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000681 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000682 /* Get unused tmp,
683 * Emit tmp = 1.0 - arg.xyzw
684 */
685 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000686 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000687 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000688
Keith Whitwellce721142005-07-11 10:10:38 +0000689 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000690 if (mask == WRITEMASK_W)
691 return src;
692 else
Brian Paul22db5352005-11-19 23:45:10 +0000693 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000694 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000695 /* Get unused tmp,
696 * Emit tmp = 1.0 - arg.wwww
697 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000698 arg = get_temp(p);
699 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000700 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000701 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000702 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000703 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000704 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000705 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000706 case OPR_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000707 default:
708 return src;
709 }
710}
711
Keith Whitwellce721142005-07-11 10:10:38 +0000712static GLboolean args_match( struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000713{
Brian Paul22db5352005-11-19 23:45:10 +0000714 GLuint i, nr = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000715
716 for (i = 0 ; i < nr ; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000717 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000718 return GL_FALSE;
719
Keith Whitwellce721142005-07-11 10:10:38 +0000720 switch(key->unit[unit].OptA[i].Operand) {
721 case OPR_SRC_ALPHA:
722 switch(key->unit[unit].OptRGB[i].Operand) {
723 case OPR_SRC_COLOR:
724 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000725 break;
726 default:
727 return GL_FALSE;
728 }
729 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000730 case OPR_ONE_MINUS_SRC_ALPHA:
731 switch(key->unit[unit].OptRGB[i].Operand) {
732 case OPR_ONE_MINUS_SRC_COLOR:
733 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000734 break;
735 default:
736 return GL_FALSE;
737 }
738 break;
739 default:
740 return GL_FALSE; /* impossible */
741 }
742 }
743
744 return GL_TRUE;
745}
746
Keith Whitwell15e75e02005-04-29 15:11:39 +0000747static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000748 struct ureg dest,
749 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000750 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000751 GLuint unit,
752 GLuint nr,
753 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +0000754 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000755{
Keith Whitwell15e75e02005-04-29 15:11:39 +0000756 struct ureg src[3];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000757 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +0000758 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000759
Brian Paul00630842005-12-12 15:27:55 +0000760 tmp = undef; /* silence warning (bug 5318) */
761
Keith Whitwell15e75e02005-04-29 15:11:39 +0000762 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +0000763 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000764
765 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +0000766 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000767 if (mask == WRITEMASK_XYZW && !saturate)
768 return src[0];
769 else
Brian Paul7e807512005-11-05 17:10:45 +0000770 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000771 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +0000772 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000773 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000774 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +0000775 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000776 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000777 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000778 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000779 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +0000780 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000781 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +0000782 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +0000783 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
784 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000785 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +0000786 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000787 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
788 */
Brian Paul7e807512005-11-05 17:10:45 +0000789 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000790
Keith Whitwellce721142005-07-11 10:10:38 +0000791 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +0000792 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000793
Keith Whitwellce721142005-07-11 10:10:38 +0000794 case MODE_DOT3_RGBA:
795 case MODE_DOT3_RGBA_EXT:
796 case MODE_DOT3_RGB_EXT:
797 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000798 struct ureg tmp0 = get_temp( p );
799 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +0000800 struct ureg neg1 = register_scalar_const(p, -1);
801 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000802
803 /* tmp0 = 2*src0 - 1
804 * tmp1 = 2*src1 - 1
805 *
806 * dst = tmp0 dot3 tmp1
807 */
Brian Paul7e807512005-11-05 17:10:45 +0000808 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000809 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000810
Brian Paulaa206952005-09-16 18:14:24 +0000811 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000812 tmp1 = tmp0;
813 else
Brian Paul7e807512005-11-05 17:10:45 +0000814 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000815 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +0000816 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000817 return dest;
818 }
Keith Whitwellce721142005-07-11 10:10:38 +0000819 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000820 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +0000821 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000822 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +0000823 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000824 /* Arg0 * Arg2 + Arg1 - 0.5 */
825 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000826 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +0000827 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
828 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000829 return dest;
830 }
Keith Whitwellce721142005-07-11 10:10:38 +0000831 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000832 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +0000833 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000834 return dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000835 default:
836 return src[0];
837 }
838}
839
Keith Whitwell15e75e02005-04-29 15:11:39 +0000840
Brian Paul22db5352005-11-19 23:45:10 +0000841/**
842 * Generate instructions for one texture unit's env/combiner mode.
843 */
844static struct ureg
845emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000846{
Keith Whitwellce721142005-07-11 10:10:38 +0000847 struct state_key *key = p->state;
Brian Paul22db5352005-11-19 23:45:10 +0000848 GLboolean saturate = (unit < p->last_tex_stage);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000849 GLuint rgb_shift, alpha_shift;
850 struct ureg out, shift;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000851 struct ureg dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000852
Keith Whitwellce721142005-07-11 10:10:38 +0000853 if (!key->unit[unit].enabled) {
854 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000855 }
Keith Whitwellce721142005-07-11 10:10:38 +0000856
857 switch (key->unit[unit].ModeRGB) {
858 case MODE_DOT3_RGB_EXT:
859 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000860 rgb_shift = 0;
861 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000862 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000863 alpha_shift = 0;
864 rgb_shift = 0;
865 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000866 default:
Keith Whitwellce721142005-07-11 10:10:38 +0000867 rgb_shift = key->unit[unit].ScaleShiftRGB;
868 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000869 break;
870 }
Keith Whitwellce721142005-07-11 10:10:38 +0000871
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000872 /* If this is the very last calculation, emit direct to output reg:
873 */
Keith Whitwellce721142005-07-11 10:10:38 +0000874 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000875 unit != p->last_tex_stage ||
876 alpha_shift ||
877 rgb_shift)
878 dest = get_temp( p );
879 else
Brian Paul90ebb582005-11-02 18:06:12 +0000880 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLR);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000881
882 /* Emit the RGB and A combine ops
883 */
Keith Whitwellce721142005-07-11 10:10:38 +0000884 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
885 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000886 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
887 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000888 key->unit[unit].NumArgsRGB,
889 key->unit[unit].ModeRGB,
890 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000891 }
Keith Whitwellce721142005-07-11 10:10:38 +0000892 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +0200893 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000894
895 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
896 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000897 key->unit[unit].NumArgsRGB,
898 key->unit[unit].ModeRGB,
899 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000900 }
901 else {
902 /* Need to do something to stop from re-emitting identical
903 * argument calculations here:
904 */
905 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
906 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000907 key->unit[unit].NumArgsRGB,
908 key->unit[unit].ModeRGB,
909 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000910 out = emit_combine( p, dest, WRITEMASK_W, saturate,
911 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000912 key->unit[unit].NumArgsA,
913 key->unit[unit].ModeA,
914 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000915 }
916
917 /* Deal with the final shift:
918 */
919 if (alpha_shift || rgb_shift) {
920 if (rgb_shift == alpha_shift) {
José Fonseca53174af2008-05-31 18:14:09 +0900921 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +0000922 }
923 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000924 shift = register_const4f(p,
José Fonseca53174af2008-05-31 18:14:09 +0900925 (GLfloat)(1<<rgb_shift),
926 (GLfloat)(1<<rgb_shift),
927 (GLfloat)(1<<rgb_shift),
928 (GLfloat)(1<<alpha_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +0000929 }
Brian Paul7e807512005-11-05 17:10:45 +0000930 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000931 saturate, out, shift, undef );
932 }
933 else
934 return out;
935}
936
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000937
Brian Paul22db5352005-11-19 23:45:10 +0000938/**
939 * Generate instruction for getting a texture source term.
940 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000941static void load_texture( struct texenv_fragment_program *p, GLuint unit )
942{
943 if (is_undef(p->src_texture[unit])) {
Keith Whitwellce721142005-07-11 10:10:38 +0000944 GLuint dim = p->state->unit[unit].source_index;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000945 struct ureg texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
946 struct ureg tmp = get_tex_temp( p );
947
Brian Paulbfb5ea32005-07-19 18:20:04 +0000948 if (dim == TEXTURE_UNKNOWN_INDEX)
949 program_error(p, "TexSrcBit");
Keith Whitwellce721142005-07-11 10:10:38 +0000950
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000951 /* TODO: Use D0_MASK_XY where possible.
952 */
Brian1e3b07f2007-12-14 11:16:49 -0700953 if (p->state->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +0000954 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
955 tmp, WRITEMASK_XYZW,
956 unit, dim, texcoord );
Brian1e3b07f2007-12-14 11:16:49 -0700957 p->program->Base.SamplersUsed |= (1 << unit);
Brianfce46122007-12-14 11:42:28 -0700958 /* This identity mapping should already be in place
959 * (see _mesa_init_program_struct()) but let's be safe.
960 */
961 p->program->Base.SamplerUnits[unit] = unit;
Brian1e3b07f2007-12-14 11:16:49 -0700962 }
Aapo Tahkolab8915342006-03-28 10:26:34 +0000963 else
964 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000965 }
966}
967
Keith Whitwell241b6b72005-05-23 09:50:34 +0000968static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000969 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000970{
971 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000972 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000973 load_texture(p, unit);
974 break;
975
Keith Whitwellce721142005-07-11 10:10:38 +0000976 case SRC_TEXTURE0:
977 case SRC_TEXTURE1:
978 case SRC_TEXTURE2:
979 case SRC_TEXTURE3:
980 case SRC_TEXTURE4:
981 case SRC_TEXTURE5:
982 case SRC_TEXTURE6:
983 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +0000984 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000985 break;
986
987 default:
988 break;
989 }
Keith Whitwell241b6b72005-05-23 09:50:34 +0000990
991 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000992}
993
Brian Paul22db5352005-11-19 23:45:10 +0000994
995/**
996 * Generate instructions for loading all texture source terms.
997 */
998static GLboolean
999load_texunit_sources( struct texenv_fragment_program *p, int unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001000{
Keith Whitwellce721142005-07-11 10:10:38 +00001001 struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +00001002 GLuint i;
1003
1004 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
1005 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001006 }
Aapo Tahkolab8915342006-03-28 10:26:34 +00001007
1008 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1009 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1010 }
1011
Keith Whitwell241b6b72005-05-23 09:50:34 +00001012 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001013}
1014
Brian Paul22db5352005-11-19 23:45:10 +00001015
1016/**
1017 * Generate a new fragment program which implements the context's
1018 * current texture env/combine mode.
1019 */
1020static void
Brian Paule998c342006-10-29 21:17:18 +00001021create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001022 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001023{
Brian Paule998c342006-10-29 21:17:18 +00001024 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001025 struct texenv_fragment_program p;
1026 GLuint unit;
1027 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001028
Keith Whitwelle4902422005-05-11 15:16:35 +00001029 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +00001030 p.ctx = ctx;
Keith Whitwellce721142005-07-11 10:10:38 +00001031 p.state = key;
1032 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001033
Brian Paule998c342006-10-29 21:17:18 +00001034 /* During code generation, use locally-allocated instruction buffer,
1035 * then alloc dynamic storage below.
1036 */
1037 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001038 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Brian21f99792007-01-09 11:00:21 -07001039 p.program->Base.NumTexIndirections = 1; /* correct? */
1040 p.program->Base.NumTexInstructions = 0;
1041 p.program->Base.NumAluInstructions = 0;
Brian1240eb22007-03-22 08:50:20 -06001042 p.program->Base.String = NULL;
Keith Whitwell9ca88152005-05-10 09:56:02 +00001043 p.program->Base.NumInstructions =
Brian Paule998c342006-10-29 21:17:18 +00001044 p.program->Base.NumTemporaries =
1045 p.program->Base.NumParameters =
1046 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001047 p.program->Base.Parameters = _mesa_new_parameter_list();
Keith Whitwelldbeea252005-05-10 13:57:50 +00001048
Brian Paulde997602005-11-12 17:53:14 +00001049 p.program->Base.InputsRead = 0;
1050 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001051
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001052 for (unit = 0; unit < MAX_TEXTURE_UNITS; unit++)
1053 p.src_texture[unit] = undef;
1054
Keith Whitwell47b29f52005-05-04 11:44:44 +00001055 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001056 p.half = undef;
1057 p.zero = undef;
1058 p.one = undef;
1059
Keith Whitwell15e75e02005-04-29 15:11:39 +00001060 p.last_tex_stage = 0;
Brianf18d4e02007-10-29 12:25:46 -06001061 release_temps(ctx, &p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001062
Keith Whitwellce721142005-07-11 10:10:38 +00001063 if (key->enabled_units) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001064 /* First pass - to support texture_env_crossbar, first identify
1065 * all referenced texture sources and emit texld instructions
1066 * for each:
1067 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001068 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001069 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001070 load_texunit_sources( &p, unit );
1071 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001072 }
1073
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001074 /* Second pass - emit combine instructions to build final color:
1075 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001076 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001077 if (key->enabled_units & (1<<unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001078 p.src_previous = emit_texenv( &p, unit );
Brian Paul8fd329d2008-09-26 11:18:06 -06001079 reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
Brianf18d4e02007-10-29 12:25:46 -06001080 release_temps(ctx, &p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001081 }
1082 }
1083
Keith Whitwellce721142005-07-11 10:10:38 +00001084 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul90ebb582005-11-02 18:06:12 +00001085 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001086
Keith Whitwellce721142005-07-11 10:10:38 +00001087 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001088 /* Emit specular add.
1089 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001090 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001091 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1092 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001093 }
Brian Paulaa206952005-09-16 18:14:24 +00001094 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001095 /* Will wind up in here if no texture enabled or a couple of
1096 * other scenarios (GL_REPLACE for instance).
1097 */
Brian Paul7e807512005-11-05 17:10:45 +00001098 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001099 }
1100
Keith Whitwell47b29f52005-05-04 11:44:44 +00001101 /* Finish up:
1102 */
Brian Paul7e807512005-11-05 17:10:45 +00001103 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001104
Keith Whitwellce721142005-07-11 10:10:38 +00001105 if (key->fog_enabled) {
1106 /* Pull fog mode from GLcontext, the value in the state key is
1107 * a reduced value and not what is expected in FogOption
1108 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001109 p.program->FogOption = ctx->Fog.Mode;
Brian63be96b2007-09-18 19:29:26 -06001110 p.program->Base.InputsRead |= FRAG_BIT_FOGC; /* XXX new */
Keith Whitwellce721142005-07-11 10:10:38 +00001111 } else
Keith Whitwell276330b2005-05-09 17:58:13 +00001112 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001113
Brian21f99792007-01-09 11:00:21 -07001114 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001115 program_error(&p, "Exceeded max nr indirect texture lookups");
1116
Brian21f99792007-01-09 11:00:21 -07001117 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001118 program_error(&p, "Exceeded max TEX instructions");
1119
Brian21f99792007-01-09 11:00:21 -07001120 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001121 program_error(&p, "Exceeded max ALU instructions");
1122
Brian Paul5d7b49f2005-11-19 23:12:20 +00001123 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001124
Brian Paule998c342006-10-29 21:17:18 +00001125 /* Allocate final instruction array */
Brian3bf8d2a2007-09-25 15:18:51 -06001126 p.program->Base.Instructions
1127 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1128 if (!p.program->Base.Instructions) {
Brian Paule998c342006-10-29 21:17:18 +00001129 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1130 "generating tex env program");
1131 return;
1132 }
Brian3bf8d2a2007-09-25 15:18:51 -06001133 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1134 p.program->Base.NumInstructions);
1135
1136 if (p.program->FogOption) {
1137 _mesa_append_fog_code(ctx, p.program);
1138 p.program->FogOption = GL_NONE;
1139 }
1140
Brian Paule998c342006-10-29 21:17:18 +00001141
Keith Whitwelldbeea252005-05-10 13:57:50 +00001142 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001143 */
Brian Paule998c342006-10-29 21:17:18 +00001144 if (ctx->Driver.ProgramStringNotify) {
1145 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1146 &p.program->Base );
1147 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001148
Brian Paule998c342006-10-29 21:17:18 +00001149 if (DISASSEM) {
1150 _mesa_print_program(&p.program->Base);
1151 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001152 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001153}
1154
Brian Paul5d7b49f2005-11-19 23:12:20 +00001155
Briana90046f2006-12-15 10:07:26 -07001156/**
Brian83ce6c52007-10-29 11:23:02 -06001157 * Return a fragment program which implements the current
1158 * fixed-function texture, fog and color-sum operations.
1159 */
1160struct gl_fragment_program *
1161_mesa_get_fixed_func_fragment_program(GLcontext *ctx)
1162{
1163 struct gl_fragment_program *prog;
1164 struct state_key key;
Brian83ce6c52007-10-29 11:23:02 -06001165
1166 make_state_key(ctx, &key);
Brian83ce6c52007-10-29 11:23:02 -06001167
Brianf4a5ea22007-10-31 12:45:32 -06001168 prog = (struct gl_fragment_program *)
1169 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1170 &key, sizeof(key));
Brian83ce6c52007-10-29 11:23:02 -06001171
1172 if (!prog) {
Brian83ce6c52007-10-29 11:23:02 -06001173 prog = (struct gl_fragment_program *)
1174 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1175
1176 create_new_program(ctx, &key, prog);
1177
Brianf4a5ea22007-10-31 12:45:32 -06001178 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1179 &key, sizeof(key), &prog->Base);
Brian83ce6c52007-10-29 11:23:02 -06001180 }
1181
1182 return prog;
1183}
1184
1185
1186
1187/**
Briana90046f2006-12-15 10:07:26 -07001188 * If _MaintainTexEnvProgram is set we'll generate a fragment program that
1189 * implements the current texture env/combine mode.
1190 * This function generates that program and puts it into effect.
1191 */
1192void
1193_mesa_UpdateTexEnvProgram( GLcontext *ctx )
Keith Whitwellce721142005-07-11 10:10:38 +00001194{
Brian Paul122629f2006-07-20 16:49:57 +00001195 const struct gl_fragment_program *prev = ctx->FragmentProgram._Current;
Keith Whitwellce721142005-07-11 10:10:38 +00001196
Briana90046f2006-12-15 10:07:26 -07001197 ASSERT(ctx->FragmentProgram._MaintainTexEnvProgram);
1198
1199 /* If a conventional fragment program/shader isn't in effect... */
Brianefcfdbd2007-02-24 15:51:41 -07001200 if (!ctx->FragmentProgram._Enabled &&
Briand781cdc2007-10-02 16:55:21 -06001201 (!ctx->Shader.CurrentProgram ||
1202 !ctx->Shader.CurrentProgram->FragmentProgram) ) {
Keith Whitwellce721142005-07-11 10:10:38 +00001203
Brian83ce6c52007-10-29 11:23:02 -06001204 ctx->FragmentProgram._Current
1205 = ctx->FragmentProgram._TexEnvProgram
1206 = _mesa_get_fixed_func_fragment_program(ctx);
Keith Whitwellc3626a92005-11-01 17:25:49 +00001207 }
Keith Whitwellc3626a92005-11-01 17:25:49 +00001208
1209 /* Tell the driver about the change. Could define a new target for
1210 * this?
1211 */
Brian Paul5d7b49f2005-11-19 23:12:20 +00001212 if (ctx->FragmentProgram._Current != prev && ctx->Driver.BindProgram) {
1213 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
Briana90046f2006-12-15 10:07:26 -07001214 (struct gl_program *) ctx->FragmentProgram._Current);
Brian Paul5d7b49f2005-11-19 23:12:20 +00001215 }
Keith Whitwellce721142005-07-11 10:10:38 +00001216}