blob: 6a57d50193c76b433523ce1eaeab76980772d868 [file] [log] [blame]
Keith Whitwell15e75e02005-04-29 15:11:39 +00001/**************************************************************************
2 *
Keith Whitwell32ef6e72008-09-20 08:26:11 -07003 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
Keith Whitwell15e75e02005-04-29 15:11:39 +00004 * All Rights Reserved.
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"
Brian Paul5b5c9312008-05-07 18:51:44 -060031#include "shader/program.h"
Brianc223c6b2007-07-04 13:15:20 -060032#include "shader/prog_parameter.h"
Keith Whitwell32ef6e72008-09-20 08:26:11 -070033#include "shader/prog_cache.h"
Brianc223c6b2007-07-04 13:15:20 -060034#include "shader/prog_instruction.h"
35#include "shader/prog_print.h"
36#include "shader/prog_statevars.h"
Brianfb3c41f2007-09-25 15:20:04 -060037#include "shader/programopt.h"
Keith Whitwell15e75e02005-04-29 15:11:39 +000038#include "texenvprogram.h"
39
Brian Paul5b5c9312008-05-07 18:51:44 -060040
Brian Paule9b34882008-12-31 11:54:02 -070041/*
42 * Note on texture units:
43 *
44 * The number of texture units supported by fixed-function fragment
45 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
46 * That's because there's a one-to-one correspondence between texture
47 * coordinates and samplers in fixed-function processing.
48 *
49 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
50 * sets of texcoords, so is fixed-function fragment processing.
51 *
52 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
53 */
54
55
Brian Paul5b5c9312008-05-07 18:51:44 -060056struct texenvprog_cache_item
57{
58 GLuint hash;
59 void *key;
60 struct gl_fragment_program *data;
61 struct texenvprog_cache_item *next;
62};
63
64
Brian Paule998c342006-10-29 21:17:18 +000065/**
Brian2a8e9bb2007-10-23 10:24:53 -060066 * This MAX is probably a bit generous, but that's OK. There can be
67 * up to four instructions per texture unit (TEX + 3 for combine),
68 * then there's fog and specular add.
Brian Paule998c342006-10-29 21:17:18 +000069 */
Brian Paule9b34882008-12-31 11:54:02 -070070#define MAX_INSTRUCTIONS ((MAX_TEXTURE_COORD_UNITS * 4) + 12)
Keith Whitwell15e75e02005-04-29 15:11:39 +000071
Keith Whitwell269e3892005-05-12 10:28:43 +000072#define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
Keith Whitwell15e75e02005-04-29 15:11:39 +000073
Keith Whitwellce721142005-07-11 10:10:38 +000074struct mode_opt {
Brian Paul5d7b49f2005-11-19 23:12:20 +000075 GLuint Source:4;
76 GLuint Operand:3;
Keith Whitwellce721142005-07-11 10:10:38 +000077};
78
79struct state_key {
Brian Paul5d7b49f2005-11-19 23:12:20 +000080 GLbitfield enabled_units;
81 GLuint separate_specular:1;
82 GLuint fog_enabled:1;
83 GLuint fog_mode:2;
Keith Whitwellce721142005-07-11 10:10:38 +000084
85 struct {
Brian Paul5d7b49f2005-11-19 23:12:20 +000086 GLuint enabled:1;
87 GLuint source_index:3; /* one of TEXTURE_1D/2D/3D/CUBE/RECT_INDEX */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +020088 GLuint shadow:1;
Brian Paul5d7b49f2005-11-19 23:12:20 +000089 GLuint ScaleShiftRGB:2;
90 GLuint ScaleShiftA:2;
Keith Whitwellce721142005-07-11 10:10:38 +000091
Brian Paul5d7b49f2005-11-19 23:12:20 +000092 GLuint NumArgsRGB:2;
93 GLuint ModeRGB:4;
Keith Whitwellce721142005-07-11 10:10:38 +000094 struct mode_opt OptRGB[3];
95
Brian Paul5d7b49f2005-11-19 23:12:20 +000096 GLuint NumArgsA:2;
97 GLuint ModeA:4;
Keith Whitwellce721142005-07-11 10:10:38 +000098 struct mode_opt OptA[3];
99 } unit[8];
100};
101
102#define FOG_LINEAR 0
103#define FOG_EXP 1
104#define FOG_EXP2 2
105#define FOG_UNKNOWN 3
106
107static GLuint translate_fog_mode( GLenum mode )
108{
109 switch (mode) {
110 case GL_LINEAR: return FOG_LINEAR;
111 case GL_EXP: return FOG_EXP;
112 case GL_EXP2: return FOG_EXP2;
113 default: return FOG_UNKNOWN;
114 }
115}
116
117#define OPR_SRC_COLOR 0
118#define OPR_ONE_MINUS_SRC_COLOR 1
119#define OPR_SRC_ALPHA 2
120#define OPR_ONE_MINUS_SRC_ALPHA 3
121#define OPR_ZERO 4
122#define OPR_ONE 5
123#define OPR_UNKNOWN 7
124
125static GLuint translate_operand( GLenum operand )
126{
127 switch (operand) {
128 case GL_SRC_COLOR: return OPR_SRC_COLOR;
129 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
130 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
131 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
132 case GL_ZERO: return OPR_ZERO;
133 case GL_ONE: return OPR_ONE;
134 default: return OPR_UNKNOWN;
135 }
136}
137
138#define SRC_TEXTURE 0
139#define SRC_TEXTURE0 1
140#define SRC_TEXTURE1 2
141#define SRC_TEXTURE2 3
142#define SRC_TEXTURE3 4
143#define SRC_TEXTURE4 5
144#define SRC_TEXTURE5 6
145#define SRC_TEXTURE6 7
146#define SRC_TEXTURE7 8
147#define SRC_CONSTANT 9
148#define SRC_PRIMARY_COLOR 10
149#define SRC_PREVIOUS 11
150#define SRC_UNKNOWN 15
151
152static GLuint translate_source( GLenum src )
153{
154 switch (src) {
155 case GL_TEXTURE: return SRC_TEXTURE;
156 case GL_TEXTURE0:
157 case GL_TEXTURE1:
158 case GL_TEXTURE2:
159 case GL_TEXTURE3:
160 case GL_TEXTURE4:
161 case GL_TEXTURE5:
162 case GL_TEXTURE6:
163 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
164 case GL_CONSTANT: return SRC_CONSTANT;
165 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
166 case GL_PREVIOUS: return SRC_PREVIOUS;
167 default: return SRC_UNKNOWN;
168 }
169}
170
171#define MODE_REPLACE 0
172#define MODE_MODULATE 1
173#define MODE_ADD 2
174#define MODE_ADD_SIGNED 3
175#define MODE_INTERPOLATE 4
176#define MODE_SUBTRACT 5
177#define MODE_DOT3_RGB 6
178#define MODE_DOT3_RGB_EXT 7
179#define MODE_DOT3_RGBA 8
180#define MODE_DOT3_RGBA_EXT 9
181#define MODE_MODULATE_ADD_ATI 10
182#define MODE_MODULATE_SIGNED_ADD_ATI 11
183#define MODE_MODULATE_SUBTRACT_ATI 12
184#define MODE_UNKNOWN 15
185
186static GLuint translate_mode( GLenum mode )
187{
188 switch (mode) {
189 case GL_REPLACE: return MODE_REPLACE;
190 case GL_MODULATE: return MODE_MODULATE;
191 case GL_ADD: return MODE_ADD;
192 case GL_ADD_SIGNED: return MODE_ADD_SIGNED;
193 case GL_INTERPOLATE: return MODE_INTERPOLATE;
194 case GL_SUBTRACT: return MODE_SUBTRACT;
195 case GL_DOT3_RGB: return MODE_DOT3_RGB;
196 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
197 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
198 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
199 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
200 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
201 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
202 default: return MODE_UNKNOWN;
203 }
204}
205
206#define TEXTURE_UNKNOWN_INDEX 7
Brian Paule00ac112005-09-15 05:00:45 +0000207static GLuint translate_tex_src_bit( GLbitfield bit )
Keith Whitwellce721142005-07-11 10:10:38 +0000208{
209 switch (bit) {
210 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
211 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
212 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
213 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
214 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
215 default: return TEXTURE_UNKNOWN_INDEX;
216 }
217}
218
Brian Paul22db5352005-11-19 23:45:10 +0000219/**
220 * Examine current texture environment state and generate a unique
221 * key to identify it.
222 */
Keith Whitwell8065c122006-05-22 16:09:27 +0000223static void make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000224{
Keith Whitwellce721142005-07-11 10:10:38 +0000225 GLuint i, j;
226
Keith Whitwell8065c122006-05-22 16:09:27 +0000227 memset(key, 0, sizeof(*key));
228
Brian Paule9b34882008-12-31 11:54:02 -0700229 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000230 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800231 GLenum format;
Roland Scheideggerbf4a0fa2008-02-15 17:26:06 +0100232
233 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
Keith Whitwellce721142005-07-11 10:10:38 +0000234 continue;
235
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800236 format = texUnit->_Current->Image[0][texUnit->_Current->BaseLevel]->_BaseFormat;
237
Keith Whitwellce721142005-07-11 10:10:38 +0000238 key->unit[i].enabled = 1;
239 key->enabled_units |= (1<<i);
240
241 key->unit[i].source_index =
242 translate_tex_src_bit(texUnit->_ReallyEnabled);
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800243 key->unit[i].shadow = ((texUnit->_Current->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
244 ((format == GL_DEPTH_COMPONENT) ||
245 (format == GL_DEPTH_STENCIL_EXT)));
Keith Whitwellce721142005-07-11 10:10:38 +0000246
247 key->unit[i].NumArgsRGB = texUnit->_CurrentCombine->_NumArgsRGB;
248 key->unit[i].NumArgsA = texUnit->_CurrentCombine->_NumArgsA;
249
250 key->unit[i].ModeRGB =
251 translate_mode(texUnit->_CurrentCombine->ModeRGB);
252 key->unit[i].ModeA =
253 translate_mode(texUnit->_CurrentCombine->ModeA);
254
255 key->unit[i].ScaleShiftRGB = texUnit->_CurrentCombine->ScaleShiftRGB;
Keith Whitwell64da1612006-05-22 14:30:58 +0000256 key->unit[i].ScaleShiftA = texUnit->_CurrentCombine->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000257
258 for (j=0;j<3;j++) {
259 key->unit[i].OptRGB[j].Operand =
260 translate_operand(texUnit->_CurrentCombine->OperandRGB[j]);
261 key->unit[i].OptA[j].Operand =
262 translate_operand(texUnit->_CurrentCombine->OperandA[j]);
263 key->unit[i].OptRGB[j].Source =
264 translate_source(texUnit->_CurrentCombine->SourceRGB[j]);
265 key->unit[i].OptA[j].Source =
266 translate_source(texUnit->_CurrentCombine->SourceA[j]);
267 }
268 }
269
270 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
271 key->separate_specular = 1;
272
273 if (ctx->Fog.Enabled) {
274 key->fog_enabled = 1;
275 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
276 }
Keith Whitwellce721142005-07-11 10:10:38 +0000277}
278
Keith Whitwell15e75e02005-04-29 15:11:39 +0000279/* Use uregs to represent registers internally, translate to Mesa's
280 * expected formats on emit.
281 *
282 * NOTE: These are passed by value extensively in this file rather
283 * than as usual by pointer reference. If this disturbs you, try
284 * remembering they are just 32bits in size.
285 *
286 * GCC is smart enough to deal with these dword-sized structures in
287 * much the same way as if I had defined them as dwords and was using
288 * macros to access and set the fields. This is much nicer and easier
289 * to evolve.
290 */
291struct ureg {
292 GLuint file:4;
293 GLuint idx:8;
294 GLuint negatebase:1;
295 GLuint abs:1;
296 GLuint negateabs:1;
297 GLuint swz:12;
298 GLuint pad:5;
299};
300
Brian Paul13abf912006-04-13 19:17:13 +0000301static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000302 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000303 ~0,
304 0,
305 0,
306 0,
307 0,
308 0
309};
310
Keith Whitwell15e75e02005-04-29 15:11:39 +0000311
Keith Whitwell15e75e02005-04-29 15:11:39 +0000312/* State used to build the fragment program:
313 */
314struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000315 struct gl_fragment_program *program;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000316 GLcontext *ctx;
Keith Whitwellce721142005-07-11 10:10:38 +0000317 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000318
Brian Paulc8d17412005-12-14 03:06:16 +0000319 GLbitfield alu_temps; /* Track texture indirections, see spec. */
320 GLbitfield temps_output; /* Track texture indirections, see spec. */
321 GLbitfield temp_in_use; /* Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000322 GLboolean error;
323
Brian Paule9b34882008-12-31 11:54:02 -0700324 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000325 /* Reg containing each texture unit's sampled texture color,
326 * else undef.
327 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000328
329 struct ureg src_previous; /* Reg containing color from previous
330 * stage. May need to be decl'd.
331 */
332
333 GLuint last_tex_stage; /* Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000334
335 struct ureg half;
336 struct ureg one;
337 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000338};
339
340
341
342static struct ureg make_ureg(GLuint file, GLuint idx)
343{
344 struct ureg reg;
345 reg.file = file;
346 reg.idx = idx;
347 reg.negatebase = 0;
348 reg.abs = 0;
349 reg.negateabs = 0;
350 reg.swz = SWIZZLE_NOOP;
351 reg.pad = 0;
352 return reg;
353}
354
355static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
356{
357 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
358 GET_SWZ(reg.swz, y),
359 GET_SWZ(reg.swz, z),
360 GET_SWZ(reg.swz, w));
361
362 return reg;
363}
364
365static struct ureg swizzle1( struct ureg reg, int x )
366{
367 return swizzle(reg, x, x, x, x);
368}
369
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000370static struct ureg negate( struct ureg reg )
371{
372 reg.negatebase ^= 1;
373 return reg;
374}
375
Keith Whitwell15e75e02005-04-29 15:11:39 +0000376static GLboolean is_undef( struct ureg reg )
377{
Alan Hourihane8d972652006-08-10 13:12:00 +0000378 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000379}
380
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000381
Keith Whitwell15e75e02005-04-29 15:11:39 +0000382static struct ureg get_temp( struct texenv_fragment_program *p )
383{
Brian Paul13abf912006-04-13 19:17:13 +0000384 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000385
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000386 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000387 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000388 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000389
390 /* Then any unused temporary:
391 */
392 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000393 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000394
Keith Whitwell15e75e02005-04-29 15:11:39 +0000395 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000396 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000397 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000398 }
399
Brian Paul13abf912006-04-13 19:17:13 +0000400 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000401 p->program->Base.NumTemporaries = bit;
402
Keith Whitwell93cd9232005-05-11 08:30:23 +0000403 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000404 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
405}
406
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000407static struct ureg get_tex_temp( struct texenv_fragment_program *p )
408{
409 int bit;
410
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000411 /* First try to find availble temp not previously used (to avoid
412 * starting a new texture indirection). According to the spec, the
413 * ~p->temps_output isn't necessary, but will keep it there for
414 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000415 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000416 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000417
418 /* Then any unused temporary:
419 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000420 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000421 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000422
423 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000424 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000425 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000426 }
427
Brian Paul13abf912006-04-13 19:17:13 +0000428 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000429 p->program->Base.NumTemporaries = bit;
430
Keith Whitwell93cd9232005-05-11 08:30:23 +0000431 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000432 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
433}
434
Keith Whitwell15e75e02005-04-29 15:11:39 +0000435
Brian93fef222007-10-29 12:25:46 -0600436static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000437{
Brian93fef222007-10-29 12:25:46 -0600438 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000439
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000440 /* KW: To support tex_env_crossbar, don't release the registers in
441 * temps_output.
442 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000443 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000444 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000445 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000446 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000447}
448
449
Brian9fe3e2e2007-02-23 11:44:14 -0700450static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000451 GLint s0,
452 GLint s1,
453 GLint s2,
454 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700455 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000456{
Brian9fe3e2e2007-02-23 11:44:14 -0700457 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000458 GLuint idx;
459 tokens[0] = s0;
460 tokens[1] = s1;
461 tokens[2] = s2;
462 tokens[3] = s3;
463 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000464 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000465 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000466}
467
Keith Whitwell47b29f52005-05-04 11:44:44 +0000468
Brian9fe3e2e2007-02-23 11:44:14 -0700469#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
470#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
471#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
472#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000473
474
475static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
476{
Brian Paulde997602005-11-12 17:53:14 +0000477 p->program->Base.InputsRead |= (1 << input);
Keith Whitwell47b29f52005-05-04 11:44:44 +0000478 return make_ureg(PROGRAM_INPUT, input);
479}
480
481
Brian Paul7e807512005-11-05 17:10:45 +0000482static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000483 struct ureg ureg )
484{
485 reg->File = ureg.file;
486 reg->Index = ureg.idx;
487 reg->Swizzle = ureg.swz;
Keith Whitwellf2802c42005-10-07 09:55:26 +0000488 reg->NegateBase = ureg.negatebase ? 0xf : 0x0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000489 reg->Abs = ureg.abs;
490 reg->NegateAbs = ureg.negateabs;
491}
492
Brian Paul7e807512005-11-05 17:10:45 +0000493static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000494 struct ureg ureg, GLuint mask )
495{
496 dst->File = ureg.file;
497 dst->Index = ureg.idx;
498 dst->WriteMask = mask;
Brianc9d495c2007-10-23 10:55:24 -0600499 dst->CondMask = COND_TR; /* always pass cond test */
500 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000501}
502
Brian Paul7e807512005-11-05 17:10:45 +0000503static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000504emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000505 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000506 struct ureg dest,
507 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000508 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000509 struct ureg src0,
510 struct ureg src1,
511 struct ureg src2 )
512{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000513 GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000514 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Brian2a8e9bb2007-10-23 10:24:53 -0600515
516 assert(nr < MAX_INSTRUCTIONS);
517
Brian Pauld6272e02006-10-29 18:03:16 +0000518 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000519 inst->Opcode = op;
520
Keith Whitwell47b29f52005-05-04 11:44:44 +0000521 emit_arg( &inst->SrcReg[0], src0 );
522 emit_arg( &inst->SrcReg[1], src1 );
523 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000524
Brian Paule31ac052005-11-20 17:52:40 +0000525 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000526
527 emit_dst( &inst->DstReg, dest, mask );
528
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000529 /* Accounting for indirection tracking:
530 */
531 if (dest.file == PROGRAM_TEMPORARY)
532 p->temps_output |= 1 << dest.idx;
533
Keith Whitwell15e75e02005-04-29 15:11:39 +0000534 return inst;
535}
536
537
538static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000539 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000540 struct ureg dest,
541 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000542 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000543 struct ureg src0,
544 struct ureg src1,
545 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000546{
547 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
548
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000549 /* Accounting for indirection tracking:
550 */
551 if (src0.file == PROGRAM_TEMPORARY)
552 p->alu_temps |= 1 << src0.idx;
553
554 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
555 p->alu_temps |= 1 << src1.idx;
556
557 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
558 p->alu_temps |= 1 << src2.idx;
559
560 if (dest.file == PROGRAM_TEMPORARY)
561 p->alu_temps |= 1 << dest.idx;
562
Brian21f99792007-01-09 11:00:21 -0700563 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000564 return dest;
565}
566
567static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000568 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000569 struct ureg dest,
570 GLuint destmask,
571 GLuint tex_unit,
572 GLuint tex_idx,
573 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000574{
Brian Paul7e807512005-11-05 17:10:45 +0000575 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000576 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000577 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000578 coord, /* arg 0? */
579 undef,
580 undef);
581
Brian Paul7e807512005-11-05 17:10:45 +0000582 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000583 inst->TexSrcUnit = tex_unit;
584
Brian21f99792007-01-09 11:00:21 -0700585 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000586
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000587 /* Is this a texture indirection?
588 */
589 if ((coord.file == PROGRAM_TEMPORARY &&
590 (p->temps_output & (1<<coord.idx))) ||
591 (dest.file == PROGRAM_TEMPORARY &&
592 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700593 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000594 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000595 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000596 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000597 }
598
599 return dest;
600}
601
602
Keith Whitwell47b29f52005-05-04 11:44:44 +0000603static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000604 GLfloat s0,
605 GLfloat s1,
606 GLfloat s2,
607 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000608{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000609 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700610 GLuint idx, swizzle;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000611 values[0] = s0;
612 values[1] = s1;
613 values[2] = s2;
614 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700615 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
616 &swizzle );
617 ASSERT(swizzle == SWIZZLE_NOOP);
Briandff0b0e2008-01-18 12:45:27 -0700618 return make_ureg(PROGRAM_CONSTANT, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000619}
620
Keith Whitwelle4902422005-05-11 15:16:35 +0000621#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000622#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
623#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
624#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000625
626
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000627static struct ureg get_one( struct texenv_fragment_program *p )
628{
629 if (is_undef(p->one))
630 p->one = register_scalar_const(p, 1.0);
631 return p->one;
632}
633
634static struct ureg get_half( struct texenv_fragment_program *p )
635{
636 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000637 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000638 return p->half;
639}
640
641static struct ureg get_zero( struct texenv_fragment_program *p )
642{
643 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000644 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000645 return p->zero;
646}
647
Keith Whitwell15e75e02005-04-29 15:11:39 +0000648
Keith Whitwell15e75e02005-04-29 15:11:39 +0000649static void program_error( struct texenv_fragment_program *p, const char *msg )
650{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000651 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000652 p->error = 1;
653}
654
Keith Whitwell15e75e02005-04-29 15:11:39 +0000655static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000656 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000657{
658 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000659 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000660 assert(!is_undef(p->src_texture[unit]));
661 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000662
Keith Whitwellce721142005-07-11 10:10:38 +0000663 case SRC_TEXTURE0:
664 case SRC_TEXTURE1:
665 case SRC_TEXTURE2:
666 case SRC_TEXTURE3:
667 case SRC_TEXTURE4:
668 case SRC_TEXTURE5:
669 case SRC_TEXTURE6:
670 case SRC_TEXTURE7:
671 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
672 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000673
Keith Whitwellce721142005-07-11 10:10:38 +0000674 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000675 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000676
Keith Whitwellce721142005-07-11 10:10:38 +0000677 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000678 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000679
Keith Whitwellce721142005-07-11 10:10:38 +0000680 case SRC_PREVIOUS:
681 default:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000682 if (is_undef(p->src_previous))
683 return register_input(p, FRAG_ATTRIB_COL0);
684 else
685 return p->src_previous;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000686 }
687}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000688
Keith Whitwell15e75e02005-04-29 15:11:39 +0000689static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000690 GLuint mask,
691 GLuint unit,
692 GLuint source,
693 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000694{
695 struct ureg arg, src, one;
696
697 src = get_source(p, source, unit);
698
699 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000700 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000701 /* Get unused tmp,
702 * Emit tmp = 1.0 - arg.xyzw
703 */
704 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000705 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000706 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000707
Keith Whitwellce721142005-07-11 10:10:38 +0000708 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000709 if (mask == WRITEMASK_W)
710 return src;
711 else
Brian Paul22db5352005-11-19 23:45:10 +0000712 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000713 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000714 /* Get unused tmp,
715 * Emit tmp = 1.0 - arg.wwww
716 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000717 arg = get_temp(p);
718 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000719 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000720 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000721 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000722 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000723 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000724 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000725 case OPR_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000726 default:
727 return src;
728 }
729}
730
Keith Whitwellce721142005-07-11 10:10:38 +0000731static GLboolean args_match( struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000732{
Brian Paul22db5352005-11-19 23:45:10 +0000733 GLuint i, nr = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000734
735 for (i = 0 ; i < nr ; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000736 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000737 return GL_FALSE;
738
Keith Whitwellce721142005-07-11 10:10:38 +0000739 switch(key->unit[unit].OptA[i].Operand) {
740 case OPR_SRC_ALPHA:
741 switch(key->unit[unit].OptRGB[i].Operand) {
742 case OPR_SRC_COLOR:
743 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000744 break;
745 default:
746 return GL_FALSE;
747 }
748 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000749 case OPR_ONE_MINUS_SRC_ALPHA:
750 switch(key->unit[unit].OptRGB[i].Operand) {
751 case OPR_ONE_MINUS_SRC_COLOR:
752 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000753 break;
754 default:
755 return GL_FALSE;
756 }
757 break;
758 default:
759 return GL_FALSE; /* impossible */
760 }
761 }
762
763 return GL_TRUE;
764}
765
Keith Whitwell15e75e02005-04-29 15:11:39 +0000766static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000767 struct ureg dest,
768 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000769 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000770 GLuint unit,
771 GLuint nr,
772 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +0000773 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000774{
Keith Whitwell15e75e02005-04-29 15:11:39 +0000775 struct ureg src[3];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000776 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +0000777 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000778
Brian Paul00630842005-12-12 15:27:55 +0000779 tmp = undef; /* silence warning (bug 5318) */
780
Keith Whitwell15e75e02005-04-29 15:11:39 +0000781 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +0000782 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000783
784 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +0000785 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000786 if (mask == WRITEMASK_XYZW && !saturate)
787 return src[0];
788 else
Brian Paul7e807512005-11-05 17:10:45 +0000789 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000790 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +0000791 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000792 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000793 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +0000794 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000795 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000796 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000797 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000798 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +0000799 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000800 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +0000801 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +0000802 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
803 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000804 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +0000805 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000806 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
807 */
Brian Paul7e807512005-11-05 17:10:45 +0000808 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000809
Keith Whitwellce721142005-07-11 10:10:38 +0000810 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +0000811 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000812
Keith Whitwellce721142005-07-11 10:10:38 +0000813 case MODE_DOT3_RGBA:
814 case MODE_DOT3_RGBA_EXT:
815 case MODE_DOT3_RGB_EXT:
816 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000817 struct ureg tmp0 = get_temp( p );
818 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +0000819 struct ureg neg1 = register_scalar_const(p, -1);
820 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000821
822 /* tmp0 = 2*src0 - 1
823 * tmp1 = 2*src1 - 1
824 *
825 * dst = tmp0 dot3 tmp1
826 */
Brian Paul7e807512005-11-05 17:10:45 +0000827 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000828 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000829
Brian Paulaa206952005-09-16 18:14:24 +0000830 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000831 tmp1 = tmp0;
832 else
Brian Paul7e807512005-11-05 17:10:45 +0000833 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000834 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +0000835 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000836 return dest;
837 }
Keith Whitwellce721142005-07-11 10:10:38 +0000838 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000839 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +0000840 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000841 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +0000842 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000843 /* Arg0 * Arg2 + Arg1 - 0.5 */
844 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000845 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +0000846 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
847 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000848 return dest;
849 }
Keith Whitwellce721142005-07-11 10:10:38 +0000850 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000851 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +0000852 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000853 return dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000854 default:
855 return src[0];
856 }
857}
858
Keith Whitwell15e75e02005-04-29 15:11:39 +0000859
Brian Paul22db5352005-11-19 23:45:10 +0000860/**
861 * Generate instructions for one texture unit's env/combiner mode.
862 */
863static struct ureg
864emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000865{
Keith Whitwellce721142005-07-11 10:10:38 +0000866 struct state_key *key = p->state;
Brian Paul22db5352005-11-19 23:45:10 +0000867 GLboolean saturate = (unit < p->last_tex_stage);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000868 GLuint rgb_shift, alpha_shift;
869 struct ureg out, shift;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000870 struct ureg dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000871
Keith Whitwellce721142005-07-11 10:10:38 +0000872 if (!key->unit[unit].enabled) {
873 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000874 }
Keith Whitwellce721142005-07-11 10:10:38 +0000875
876 switch (key->unit[unit].ModeRGB) {
877 case MODE_DOT3_RGB_EXT:
878 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000879 rgb_shift = 0;
880 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000881 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000882 alpha_shift = 0;
883 rgb_shift = 0;
884 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000885 default:
Keith Whitwellce721142005-07-11 10:10:38 +0000886 rgb_shift = key->unit[unit].ScaleShiftRGB;
887 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000888 break;
889 }
Keith Whitwellce721142005-07-11 10:10:38 +0000890
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000891 /* If this is the very last calculation, emit direct to output reg:
892 */
Keith Whitwellce721142005-07-11 10:10:38 +0000893 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000894 unit != p->last_tex_stage ||
895 alpha_shift ||
896 rgb_shift)
897 dest = get_temp( p );
898 else
Brian Paul90ebb582005-11-02 18:06:12 +0000899 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLR);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000900
901 /* Emit the RGB and A combine ops
902 */
Keith Whitwellce721142005-07-11 10:10:38 +0000903 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
904 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000905 out = emit_combine( p, dest, WRITEMASK_XYZW, 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 }
Keith Whitwellce721142005-07-11 10:10:38 +0000911 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +0200912 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000913
914 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
915 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000916 key->unit[unit].NumArgsRGB,
917 key->unit[unit].ModeRGB,
918 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000919 }
920 else {
921 /* Need to do something to stop from re-emitting identical
922 * argument calculations here:
923 */
924 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
925 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000926 key->unit[unit].NumArgsRGB,
927 key->unit[unit].ModeRGB,
928 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000929 out = emit_combine( p, dest, WRITEMASK_W, saturate,
930 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000931 key->unit[unit].NumArgsA,
932 key->unit[unit].ModeA,
933 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000934 }
935
936 /* Deal with the final shift:
937 */
938 if (alpha_shift || rgb_shift) {
939 if (rgb_shift == alpha_shift) {
José Fonseca452a5922008-05-31 18:14:09 +0900940 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +0000941 }
942 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000943 shift = register_const4f(p,
José Fonseca452a5922008-05-31 18:14:09 +0900944 (GLfloat)(1<<rgb_shift),
945 (GLfloat)(1<<rgb_shift),
946 (GLfloat)(1<<rgb_shift),
947 (GLfloat)(1<<alpha_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +0000948 }
Brian Paul7e807512005-11-05 17:10:45 +0000949 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000950 saturate, out, shift, undef );
951 }
952 else
953 return out;
954}
955
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000956
Brian Paul22db5352005-11-19 23:45:10 +0000957/**
958 * Generate instruction for getting a texture source term.
959 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000960static void load_texture( struct texenv_fragment_program *p, GLuint unit )
961{
962 if (is_undef(p->src_texture[unit])) {
Keith Whitwellce721142005-07-11 10:10:38 +0000963 GLuint dim = p->state->unit[unit].source_index;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000964 struct ureg texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
965 struct ureg tmp = get_tex_temp( p );
966
Brian Paulbfb5ea32005-07-19 18:20:04 +0000967 if (dim == TEXTURE_UNKNOWN_INDEX)
968 program_error(p, "TexSrcBit");
Keith Whitwellce721142005-07-11 10:10:38 +0000969
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000970 /* TODO: Use D0_MASK_XY where possible.
971 */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +0200972 if (p->state->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +0000973 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
974 tmp, WRITEMASK_XYZW,
975 unit, dim, texcoord );
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +0200976 if (p->state->unit[unit].shadow)
977 p->program->Base.ShadowSamplers |= 1 << unit;
Brian9b7e5a52007-12-14 11:16:49 -0700978
979 p->program->Base.SamplersUsed |= (1 << unit);
Brian1fe385f2007-12-14 11:42:28 -0700980 /* This identity mapping should already be in place
981 * (see _mesa_init_program_struct()) but let's be safe.
982 */
983 p->program->Base.SamplerUnits[unit] = unit;
Brian9b7e5a52007-12-14 11:16:49 -0700984 }
985 else
Aapo Tahkolab8915342006-03-28 10:26:34 +0000986 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000987 }
988}
989
Keith Whitwell241b6b72005-05-23 09:50:34 +0000990static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000991 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000992{
993 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000994 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000995 load_texture(p, unit);
996 break;
997
Keith Whitwellce721142005-07-11 10:10:38 +0000998 case SRC_TEXTURE0:
999 case SRC_TEXTURE1:
1000 case SRC_TEXTURE2:
1001 case SRC_TEXTURE3:
1002 case SRC_TEXTURE4:
1003 case SRC_TEXTURE5:
1004 case SRC_TEXTURE6:
1005 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +00001006 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001007 break;
1008
1009 default:
1010 break;
1011 }
Keith Whitwell241b6b72005-05-23 09:50:34 +00001012
1013 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001014}
1015
Brian Paul22db5352005-11-19 23:45:10 +00001016
1017/**
1018 * Generate instructions for loading all texture source terms.
1019 */
1020static GLboolean
1021load_texunit_sources( struct texenv_fragment_program *p, int unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001022{
Keith Whitwellce721142005-07-11 10:10:38 +00001023 struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +00001024 GLuint i;
1025
1026 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
1027 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001028 }
Aapo Tahkolab8915342006-03-28 10:26:34 +00001029
1030 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1031 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1032 }
1033
Keith Whitwell241b6b72005-05-23 09:50:34 +00001034 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001035}
1036
Brian Paul22db5352005-11-19 23:45:10 +00001037
1038/**
1039 * Generate a new fragment program which implements the context's
1040 * current texture env/combine mode.
1041 */
1042static void
Brian Paule998c342006-10-29 21:17:18 +00001043create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001044 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001045{
Brian Paule998c342006-10-29 21:17:18 +00001046 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001047 struct texenv_fragment_program p;
1048 GLuint unit;
1049 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001050
Keith Whitwelle4902422005-05-11 15:16:35 +00001051 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +00001052 p.ctx = ctx;
Keith Whitwellce721142005-07-11 10:10:38 +00001053 p.state = key;
1054 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001055
Brian Paule998c342006-10-29 21:17:18 +00001056 /* During code generation, use locally-allocated instruction buffer,
1057 * then alloc dynamic storage below.
1058 */
1059 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001060 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Brian21f99792007-01-09 11:00:21 -07001061 p.program->Base.NumTexIndirections = 1; /* correct? */
1062 p.program->Base.NumTexInstructions = 0;
1063 p.program->Base.NumAluInstructions = 0;
Brian1240eb22007-03-22 08:50:20 -06001064 p.program->Base.String = NULL;
Keith Whitwell9ca88152005-05-10 09:56:02 +00001065 p.program->Base.NumInstructions =
Brian Paule998c342006-10-29 21:17:18 +00001066 p.program->Base.NumTemporaries =
1067 p.program->Base.NumParameters =
1068 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001069 p.program->Base.Parameters = _mesa_new_parameter_list();
Keith Whitwelldbeea252005-05-10 13:57:50 +00001070
Brian Paulde997602005-11-12 17:53:14 +00001071 p.program->Base.InputsRead = 0;
1072 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001073
Brian Paule9b34882008-12-31 11:54:02 -07001074 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001075 p.src_texture[unit] = undef;
1076
Keith Whitwell47b29f52005-05-04 11:44:44 +00001077 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001078 p.half = undef;
1079 p.zero = undef;
1080 p.one = undef;
1081
Keith Whitwell15e75e02005-04-29 15:11:39 +00001082 p.last_tex_stage = 0;
Brian93fef222007-10-29 12:25:46 -06001083 release_temps(ctx, &p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001084
Keith Whitwellce721142005-07-11 10:10:38 +00001085 if (key->enabled_units) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001086 /* First pass - to support texture_env_crossbar, first identify
1087 * all referenced texture sources and emit texld instructions
1088 * for each:
1089 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001090 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001091 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001092 load_texunit_sources( &p, unit );
1093 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001094 }
1095
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001096 /* Second pass - emit combine instructions to build final color:
1097 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001098 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001099 if (key->enabled_units & (1<<unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001100 p.src_previous = emit_texenv( &p, unit );
Brian93fef222007-10-29 12:25:46 -06001101 release_temps(ctx, &p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001102 }
1103 }
1104
Keith Whitwellce721142005-07-11 10:10:38 +00001105 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul90ebb582005-11-02 18:06:12 +00001106 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001107
Keith Whitwellce721142005-07-11 10:10:38 +00001108 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001109 /* Emit specular add.
1110 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001111 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001112 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1113 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001114 }
Brian Paulaa206952005-09-16 18:14:24 +00001115 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001116 /* Will wind up in here if no texture enabled or a couple of
1117 * other scenarios (GL_REPLACE for instance).
1118 */
Brian Paul7e807512005-11-05 17:10:45 +00001119 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001120 }
1121
Keith Whitwell47b29f52005-05-04 11:44:44 +00001122 /* Finish up:
1123 */
Brian Paul7e807512005-11-05 17:10:45 +00001124 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001125
Keith Whitwellce721142005-07-11 10:10:38 +00001126 if (key->fog_enabled) {
1127 /* Pull fog mode from GLcontext, the value in the state key is
1128 * a reduced value and not what is expected in FogOption
1129 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001130 p.program->FogOption = ctx->Fog.Mode;
Brian19d77d62007-09-18 19:29:26 -06001131 p.program->Base.InputsRead |= FRAG_BIT_FOGC; /* XXX new */
Keith Whitwellce721142005-07-11 10:10:38 +00001132 } else
Keith Whitwell276330b2005-05-09 17:58:13 +00001133 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001134
Brian21f99792007-01-09 11:00:21 -07001135 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001136 program_error(&p, "Exceeded max nr indirect texture lookups");
1137
Brian21f99792007-01-09 11:00:21 -07001138 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001139 program_error(&p, "Exceeded max TEX instructions");
1140
Brian21f99792007-01-09 11:00:21 -07001141 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001142 program_error(&p, "Exceeded max ALU instructions");
1143
Brian Paul5d7b49f2005-11-19 23:12:20 +00001144 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001145
Brian Paule998c342006-10-29 21:17:18 +00001146 /* Allocate final instruction array */
Brianc81cce72007-09-25 15:18:51 -06001147 p.program->Base.Instructions
1148 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1149 if (!p.program->Base.Instructions) {
Brian Paule998c342006-10-29 21:17:18 +00001150 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1151 "generating tex env program");
1152 return;
1153 }
Brianc81cce72007-09-25 15:18:51 -06001154 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1155 p.program->Base.NumInstructions);
1156
1157 if (p.program->FogOption) {
1158 _mesa_append_fog_code(ctx, p.program);
1159 p.program->FogOption = GL_NONE;
1160 }
1161
Brian Paule998c342006-10-29 21:17:18 +00001162
Keith Whitwelldbeea252005-05-10 13:57:50 +00001163 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001164 */
Brian Paule998c342006-10-29 21:17:18 +00001165 if (ctx->Driver.ProgramStringNotify) {
1166 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1167 &p.program->Base );
1168 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001169
Brian Paule998c342006-10-29 21:17:18 +00001170 if (DISASSEM) {
1171 _mesa_print_program(&p.program->Base);
1172 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001173 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001174}
1175
Brian Paul5d7b49f2005-11-19 23:12:20 +00001176
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001177/**
1178 * Return a fragment program which implements the current
1179 * fixed-function texture, fog and color-sum operations.
1180 */
1181struct gl_fragment_program *
1182_mesa_get_fixed_func_fragment_program(GLcontext *ctx)
Keith Whitwellce721142005-07-11 10:10:38 +00001183{
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001184 struct gl_fragment_program *prog;
1185 struct state_key key;
1186
1187 make_state_key(ctx, &key);
1188
1189 prog = (struct gl_fragment_program *)
1190 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1191 &key, sizeof(key));
Keith Whitwellce721142005-07-11 10:10:38 +00001192
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001193 if (!prog) {
1194 prog = (struct gl_fragment_program *)
1195 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1196
1197 create_new_program(ctx, &key, prog);
1198
1199 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1200 &key, sizeof(key), &prog->Base);
Keith Whitwellce721142005-07-11 10:10:38 +00001201 }
1202
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001203 return prog;
Keith Whitwellce721142005-07-11 10:10:38 +00001204}
1205
Keith Whitwellce721142005-07-11 10:10:38 +00001206
Briana90046f2006-12-15 10:07:26 -07001207
1208/**
1209 * If _MaintainTexEnvProgram is set we'll generate a fragment program that
1210 * implements the current texture env/combine mode.
1211 * This function generates that program and puts it into effect.
1212 */
1213void
1214_mesa_UpdateTexEnvProgram( GLcontext *ctx )
Keith Whitwellce721142005-07-11 10:10:38 +00001215{
Brian Paul122629f2006-07-20 16:49:57 +00001216 const struct gl_fragment_program *prev = ctx->FragmentProgram._Current;
Keith Whitwellce721142005-07-11 10:10:38 +00001217
Briana90046f2006-12-15 10:07:26 -07001218 ASSERT(ctx->FragmentProgram._MaintainTexEnvProgram);
1219
1220 /* If a conventional fragment program/shader isn't in effect... */
Brianefcfdbd2007-02-24 15:51:41 -07001221 if (!ctx->FragmentProgram._Enabled &&
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001222 (!ctx->Shader.CurrentProgram ||
1223 !ctx->Shader.CurrentProgram->FragmentProgram) )
1224 {
Brian Paul5b5c9312008-05-07 18:51:44 -06001225 struct gl_fragment_program *newProg;
1226
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001227 newProg = _mesa_get_fixed_func_fragment_program(ctx);
Brian Paul5b5c9312008-05-07 18:51:44 -06001228
1229 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current, newProg);
1230 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._TexEnvProgram, newProg);
Keith Whitwellc3626a92005-11-01 17:25:49 +00001231 }
Keith Whitwellc3626a92005-11-01 17:25:49 +00001232
1233 /* Tell the driver about the change. Could define a new target for
1234 * this?
1235 */
Brian Paul5d7b49f2005-11-19 23:12:20 +00001236 if (ctx->FragmentProgram._Current != prev && ctx->Driver.BindProgram) {
1237 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
Briana90046f2006-12-15 10:07:26 -07001238 (struct gl_program *) ctx->FragmentProgram._Current);
Brian Paul5d7b49f2005-11-19 23:12:20 +00001239 }
Keith Whitwellce721142005-07-11 10:10:38 +00001240}