blob: 38be03908415ee24612ebe70ff611146c2b35bfb [file] [log] [blame]
Keith Whitwell15e75e02005-04-29 15:11:39 +00001/**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * 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"
32#include "shader/prog_instruction.h"
33#include "shader/prog_print.h"
34#include "shader/prog_statevars.h"
Keith Whitwell15e75e02005-04-29 15:11:39 +000035#include "texenvprogram.h"
36
Brian Paule998c342006-10-29 21:17:18 +000037/**
Brian2a8e9bb2007-10-23 10:24:53 -060038 * This MAX is probably a bit generous, but that's OK. There can be
39 * up to four instructions per texture unit (TEX + 3 for combine),
40 * then there's fog and specular add.
Brian Paule998c342006-10-29 21:17:18 +000041 */
Brian2a8e9bb2007-10-23 10:24:53 -060042#define MAX_INSTRUCTIONS ((MAX_TEXTURE_UNITS * 4) + 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
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000376 /* First try to find availble temp not previously used (to avoid
377 * 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
401static void release_temps( struct texenv_fragment_program *p )
402{
Brian Paul05051032005-11-01 04:36:33 +0000403 GLuint max_temp = p->ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000404
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000405 /* KW: To support tex_env_crossbar, don't release the registers in
406 * temps_output.
407 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000408 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000409 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000410 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000411 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000412}
413
414
Brian9fe3e2e2007-02-23 11:44:14 -0700415static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000416 GLint s0,
417 GLint s1,
418 GLint s2,
419 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700420 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000421{
Brian9fe3e2e2007-02-23 11:44:14 -0700422 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000423 GLuint idx;
424 tokens[0] = s0;
425 tokens[1] = s1;
426 tokens[2] = s2;
427 tokens[3] = s3;
428 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000429 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000430 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000431}
432
Keith Whitwell47b29f52005-05-04 11:44:44 +0000433
Brian9fe3e2e2007-02-23 11:44:14 -0700434#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
435#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
436#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
437#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000438
439
440static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
441{
Brian Paulde997602005-11-12 17:53:14 +0000442 p->program->Base.InputsRead |= (1 << input);
Keith Whitwell47b29f52005-05-04 11:44:44 +0000443 return make_ureg(PROGRAM_INPUT, input);
444}
445
446
Brian Paul7e807512005-11-05 17:10:45 +0000447static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000448 struct ureg ureg )
449{
450 reg->File = ureg.file;
451 reg->Index = ureg.idx;
452 reg->Swizzle = ureg.swz;
Keith Whitwellf2802c42005-10-07 09:55:26 +0000453 reg->NegateBase = ureg.negatebase ? 0xf : 0x0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000454 reg->Abs = ureg.abs;
455 reg->NegateAbs = ureg.negateabs;
456}
457
Brian Paul7e807512005-11-05 17:10:45 +0000458static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000459 struct ureg ureg, GLuint mask )
460{
461 dst->File = ureg.file;
462 dst->Index = ureg.idx;
463 dst->WriteMask = mask;
Brianc9d495c2007-10-23 10:55:24 -0600464 dst->CondMask = COND_TR; /* always pass cond test */
465 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000466}
467
Brian Paul7e807512005-11-05 17:10:45 +0000468static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000469emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000470 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000471 struct ureg dest,
472 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000473 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000474 struct ureg src0,
475 struct ureg src1,
476 struct ureg src2 )
477{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000478 GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000479 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Brian2a8e9bb2007-10-23 10:24:53 -0600480
481 assert(nr < MAX_INSTRUCTIONS);
482
Brian Pauld6272e02006-10-29 18:03:16 +0000483 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000484 inst->Opcode = op;
485
Keith Whitwell47b29f52005-05-04 11:44:44 +0000486 emit_arg( &inst->SrcReg[0], src0 );
487 emit_arg( &inst->SrcReg[1], src1 );
488 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000489
Brian Paule31ac052005-11-20 17:52:40 +0000490 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000491
492 emit_dst( &inst->DstReg, dest, mask );
493
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000494 /* Accounting for indirection tracking:
495 */
496 if (dest.file == PROGRAM_TEMPORARY)
497 p->temps_output |= 1 << dest.idx;
498
Keith Whitwell15e75e02005-04-29 15:11:39 +0000499 return inst;
500}
501
502
503static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000504 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000505 struct ureg dest,
506 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000507 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000508 struct ureg src0,
509 struct ureg src1,
510 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000511{
512 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
513
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000514 /* Accounting for indirection tracking:
515 */
516 if (src0.file == PROGRAM_TEMPORARY)
517 p->alu_temps |= 1 << src0.idx;
518
519 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
520 p->alu_temps |= 1 << src1.idx;
521
522 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
523 p->alu_temps |= 1 << src2.idx;
524
525 if (dest.file == PROGRAM_TEMPORARY)
526 p->alu_temps |= 1 << dest.idx;
527
Brian21f99792007-01-09 11:00:21 -0700528 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000529 return dest;
530}
531
532static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000533 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000534 struct ureg dest,
535 GLuint destmask,
536 GLuint tex_unit,
537 GLuint tex_idx,
538 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000539{
Brian Paul7e807512005-11-05 17:10:45 +0000540 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000541 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000542 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000543 coord, /* arg 0? */
544 undef,
545 undef);
546
Brian Paul7e807512005-11-05 17:10:45 +0000547 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000548 inst->TexSrcUnit = tex_unit;
549
Brian21f99792007-01-09 11:00:21 -0700550 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000551
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000552 /* Is this a texture indirection?
553 */
554 if ((coord.file == PROGRAM_TEMPORARY &&
555 (p->temps_output & (1<<coord.idx))) ||
556 (dest.file == PROGRAM_TEMPORARY &&
557 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700558 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000559 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000560 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000561 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000562 }
563
564 return dest;
565}
566
567
Keith Whitwell47b29f52005-05-04 11:44:44 +0000568static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000569 GLfloat s0,
570 GLfloat s1,
571 GLfloat s2,
572 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000573{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000574 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700575 GLuint idx, swizzle;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000576 values[0] = s0;
577 values[1] = s1;
578 values[2] = s2;
579 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700580 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
581 &swizzle );
582 ASSERT(swizzle == SWIZZLE_NOOP);
Keith Whitwell47b29f52005-05-04 11:44:44 +0000583 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000584}
585
Keith Whitwelle4902422005-05-11 15:16:35 +0000586#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000587#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
588#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
589#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000590
591
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000592static struct ureg get_one( struct texenv_fragment_program *p )
593{
594 if (is_undef(p->one))
595 p->one = register_scalar_const(p, 1.0);
596 return p->one;
597}
598
599static struct ureg get_half( struct texenv_fragment_program *p )
600{
601 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000602 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000603 return p->half;
604}
605
606static struct ureg get_zero( struct texenv_fragment_program *p )
607{
608 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000609 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000610 return p->zero;
611}
612
Keith Whitwell15e75e02005-04-29 15:11:39 +0000613
Keith Whitwell15e75e02005-04-29 15:11:39 +0000614static void program_error( struct texenv_fragment_program *p, const char *msg )
615{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000616 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000617 p->error = 1;
618}
619
Keith Whitwell15e75e02005-04-29 15:11:39 +0000620static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000621 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000622{
623 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000624 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000625 assert(!is_undef(p->src_texture[unit]));
626 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000627
Keith Whitwellce721142005-07-11 10:10:38 +0000628 case SRC_TEXTURE0:
629 case SRC_TEXTURE1:
630 case SRC_TEXTURE2:
631 case SRC_TEXTURE3:
632 case SRC_TEXTURE4:
633 case SRC_TEXTURE5:
634 case SRC_TEXTURE6:
635 case SRC_TEXTURE7:
636 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
637 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000638
Keith Whitwellce721142005-07-11 10:10:38 +0000639 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000640 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000641
Keith Whitwellce721142005-07-11 10:10:38 +0000642 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000643 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000644
Keith Whitwellce721142005-07-11 10:10:38 +0000645 case SRC_PREVIOUS:
646 default:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000647 if (is_undef(p->src_previous))
648 return register_input(p, FRAG_ATTRIB_COL0);
649 else
650 return p->src_previous;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000651 }
652}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000653
Keith Whitwell15e75e02005-04-29 15:11:39 +0000654static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000655 GLuint mask,
656 GLuint unit,
657 GLuint source,
658 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000659{
660 struct ureg arg, src, one;
661
662 src = get_source(p, source, unit);
663
664 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000665 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000666 /* Get unused tmp,
667 * Emit tmp = 1.0 - arg.xyzw
668 */
669 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000670 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000671 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000672
Keith Whitwellce721142005-07-11 10:10:38 +0000673 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000674 if (mask == WRITEMASK_W)
675 return src;
676 else
Brian Paul22db5352005-11-19 23:45:10 +0000677 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000678 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000679 /* Get unused tmp,
680 * Emit tmp = 1.0 - arg.wwww
681 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000682 arg = get_temp(p);
683 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000684 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000685 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000686 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000687 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000688 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000689 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000690 case OPR_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000691 default:
692 return src;
693 }
694}
695
Keith Whitwellce721142005-07-11 10:10:38 +0000696static GLboolean args_match( struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000697{
Brian Paul22db5352005-11-19 23:45:10 +0000698 GLuint i, nr = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000699
700 for (i = 0 ; i < nr ; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000701 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000702 return GL_FALSE;
703
Keith Whitwellce721142005-07-11 10:10:38 +0000704 switch(key->unit[unit].OptA[i].Operand) {
705 case OPR_SRC_ALPHA:
706 switch(key->unit[unit].OptRGB[i].Operand) {
707 case OPR_SRC_COLOR:
708 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000709 break;
710 default:
711 return GL_FALSE;
712 }
713 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000714 case OPR_ONE_MINUS_SRC_ALPHA:
715 switch(key->unit[unit].OptRGB[i].Operand) {
716 case OPR_ONE_MINUS_SRC_COLOR:
717 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000718 break;
719 default:
720 return GL_FALSE;
721 }
722 break;
723 default:
724 return GL_FALSE; /* impossible */
725 }
726 }
727
728 return GL_TRUE;
729}
730
Keith Whitwell15e75e02005-04-29 15:11:39 +0000731static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000732 struct ureg dest,
733 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000734 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000735 GLuint unit,
736 GLuint nr,
737 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +0000738 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000739{
Keith Whitwell15e75e02005-04-29 15:11:39 +0000740 struct ureg src[3];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000741 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +0000742 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000743
Brian Paul00630842005-12-12 15:27:55 +0000744 tmp = undef; /* silence warning (bug 5318) */
745
Keith Whitwell15e75e02005-04-29 15:11:39 +0000746 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +0000747 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000748
749 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +0000750 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000751 if (mask == WRITEMASK_XYZW && !saturate)
752 return src[0];
753 else
Brian Paul7e807512005-11-05 17:10:45 +0000754 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000755 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +0000756 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000757 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000758 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +0000759 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000760 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000761 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000762 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000763 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +0000764 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000765 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +0000766 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +0000767 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
768 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000769 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +0000770 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000771 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
772 */
Brian Paul7e807512005-11-05 17:10:45 +0000773 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000774
Keith Whitwellce721142005-07-11 10:10:38 +0000775 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +0000776 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000777
Keith Whitwellce721142005-07-11 10:10:38 +0000778 case MODE_DOT3_RGBA:
779 case MODE_DOT3_RGBA_EXT:
780 case MODE_DOT3_RGB_EXT:
781 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000782 struct ureg tmp0 = get_temp( p );
783 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +0000784 struct ureg neg1 = register_scalar_const(p, -1);
785 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000786
787 /* tmp0 = 2*src0 - 1
788 * tmp1 = 2*src1 - 1
789 *
790 * dst = tmp0 dot3 tmp1
791 */
Brian Paul7e807512005-11-05 17:10:45 +0000792 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000793 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000794
Brian Paulaa206952005-09-16 18:14:24 +0000795 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000796 tmp1 = tmp0;
797 else
Brian Paul7e807512005-11-05 17:10:45 +0000798 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000799 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +0000800 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000801 return dest;
802 }
Keith Whitwellce721142005-07-11 10:10:38 +0000803 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000804 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +0000805 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000806 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +0000807 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000808 /* Arg0 * Arg2 + Arg1 - 0.5 */
809 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000810 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +0000811 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
812 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000813 return dest;
814 }
Keith Whitwellce721142005-07-11 10:10:38 +0000815 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000816 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +0000817 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000818 return dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000819 default:
820 return src[0];
821 }
822}
823
Keith Whitwell15e75e02005-04-29 15:11:39 +0000824
Brian Paul22db5352005-11-19 23:45:10 +0000825/**
826 * Generate instructions for one texture unit's env/combiner mode.
827 */
828static struct ureg
829emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000830{
Keith Whitwellce721142005-07-11 10:10:38 +0000831 struct state_key *key = p->state;
Brian Paul22db5352005-11-19 23:45:10 +0000832 GLboolean saturate = (unit < p->last_tex_stage);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000833 GLuint rgb_shift, alpha_shift;
834 struct ureg out, shift;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000835 struct ureg dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000836
Keith Whitwellce721142005-07-11 10:10:38 +0000837 if (!key->unit[unit].enabled) {
838 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000839 }
Keith Whitwellce721142005-07-11 10:10:38 +0000840
841 switch (key->unit[unit].ModeRGB) {
842 case MODE_DOT3_RGB_EXT:
843 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000844 rgb_shift = 0;
845 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000846 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000847 alpha_shift = 0;
848 rgb_shift = 0;
849 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000850 default:
Keith Whitwellce721142005-07-11 10:10:38 +0000851 rgb_shift = key->unit[unit].ScaleShiftRGB;
852 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000853 break;
854 }
Keith Whitwellce721142005-07-11 10:10:38 +0000855
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000856 /* If this is the very last calculation, emit direct to output reg:
857 */
Keith Whitwellce721142005-07-11 10:10:38 +0000858 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000859 unit != p->last_tex_stage ||
860 alpha_shift ||
861 rgb_shift)
862 dest = get_temp( p );
863 else
Brian Paul90ebb582005-11-02 18:06:12 +0000864 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLR);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000865
866 /* Emit the RGB and A combine ops
867 */
Keith Whitwellce721142005-07-11 10:10:38 +0000868 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
869 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000870 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
871 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000872 key->unit[unit].NumArgsRGB,
873 key->unit[unit].ModeRGB,
874 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000875 }
Keith Whitwellce721142005-07-11 10:10:38 +0000876 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +0200877 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000878
879 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
880 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000881 key->unit[unit].NumArgsRGB,
882 key->unit[unit].ModeRGB,
883 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000884 }
885 else {
886 /* Need to do something to stop from re-emitting identical
887 * argument calculations here:
888 */
889 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
890 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000891 key->unit[unit].NumArgsRGB,
892 key->unit[unit].ModeRGB,
893 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000894 out = emit_combine( p, dest, WRITEMASK_W, saturate,
895 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000896 key->unit[unit].NumArgsA,
897 key->unit[unit].ModeA,
898 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000899 }
900
901 /* Deal with the final shift:
902 */
903 if (alpha_shift || rgb_shift) {
904 if (rgb_shift == alpha_shift) {
Keith Whitwelle4902422005-05-11 15:16:35 +0000905 shift = register_scalar_const(p, 1<<rgb_shift);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000906 }
907 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000908 shift = register_const4f(p,
909 1<<rgb_shift,
910 1<<rgb_shift,
911 1<<rgb_shift,
912 1<<alpha_shift);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000913 }
Brian Paul7e807512005-11-05 17:10:45 +0000914 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000915 saturate, out, shift, undef );
916 }
917 else
918 return out;
919}
920
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000921
Brian Paul22db5352005-11-19 23:45:10 +0000922/**
923 * Generate instruction for getting a texture source term.
924 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000925static void load_texture( struct texenv_fragment_program *p, GLuint unit )
926{
927 if (is_undef(p->src_texture[unit])) {
Keith Whitwellce721142005-07-11 10:10:38 +0000928 GLuint dim = p->state->unit[unit].source_index;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000929 struct ureg texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
930 struct ureg tmp = get_tex_temp( p );
931
Brian Paulbfb5ea32005-07-19 18:20:04 +0000932 if (dim == TEXTURE_UNKNOWN_INDEX)
933 program_error(p, "TexSrcBit");
Keith Whitwellce721142005-07-11 10:10:38 +0000934
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000935 /* TODO: Use D0_MASK_XY where possible.
936 */
Aapo Tahkolab8915342006-03-28 10:26:34 +0000937 if (p->state->unit[unit].enabled)
938 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
939 tmp, WRITEMASK_XYZW,
940 unit, dim, texcoord );
941 else
942 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000943 }
944}
945
Keith Whitwell241b6b72005-05-23 09:50:34 +0000946static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000947 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000948{
949 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000950 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000951 load_texture(p, unit);
952 break;
953
Keith Whitwellce721142005-07-11 10:10:38 +0000954 case SRC_TEXTURE0:
955 case SRC_TEXTURE1:
956 case SRC_TEXTURE2:
957 case SRC_TEXTURE3:
958 case SRC_TEXTURE4:
959 case SRC_TEXTURE5:
960 case SRC_TEXTURE6:
961 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +0000962 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000963 break;
964
965 default:
966 break;
967 }
Keith Whitwell241b6b72005-05-23 09:50:34 +0000968
969 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000970}
971
Brian Paul22db5352005-11-19 23:45:10 +0000972
973/**
974 * Generate instructions for loading all texture source terms.
975 */
976static GLboolean
977load_texunit_sources( struct texenv_fragment_program *p, int unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000978{
Keith Whitwellce721142005-07-11 10:10:38 +0000979 struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +0000980 GLuint i;
981
982 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
983 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000984 }
Aapo Tahkolab8915342006-03-28 10:26:34 +0000985
986 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
987 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
988 }
989
Keith Whitwell241b6b72005-05-23 09:50:34 +0000990 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000991}
992
Brian Paul22db5352005-11-19 23:45:10 +0000993
994/**
995 * Generate a new fragment program which implements the context's
996 * current texture env/combine mode.
997 */
998static void
Brian Paule998c342006-10-29 21:17:18 +0000999create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001000 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001001{
Brian Paule998c342006-10-29 21:17:18 +00001002 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001003 struct texenv_fragment_program p;
1004 GLuint unit;
1005 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001006
Keith Whitwelle4902422005-05-11 15:16:35 +00001007 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +00001008 p.ctx = ctx;
Keith Whitwellce721142005-07-11 10:10:38 +00001009 p.state = key;
1010 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001011
Brian Paule998c342006-10-29 21:17:18 +00001012 /* During code generation, use locally-allocated instruction buffer,
1013 * then alloc dynamic storage below.
1014 */
1015 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001016 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Brian21f99792007-01-09 11:00:21 -07001017 p.program->Base.NumTexIndirections = 1; /* correct? */
1018 p.program->Base.NumTexInstructions = 0;
1019 p.program->Base.NumAluInstructions = 0;
Brian1240eb22007-03-22 08:50:20 -06001020 p.program->Base.String = NULL;
Keith Whitwell9ca88152005-05-10 09:56:02 +00001021 p.program->Base.NumInstructions =
Brian Paule998c342006-10-29 21:17:18 +00001022 p.program->Base.NumTemporaries =
1023 p.program->Base.NumParameters =
1024 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001025 p.program->Base.Parameters = _mesa_new_parameter_list();
Keith Whitwelldbeea252005-05-10 13:57:50 +00001026
Brian Paulde997602005-11-12 17:53:14 +00001027 p.program->Base.InputsRead = 0;
1028 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001029
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001030 for (unit = 0; unit < MAX_TEXTURE_UNITS; unit++)
1031 p.src_texture[unit] = undef;
1032
Keith Whitwell47b29f52005-05-04 11:44:44 +00001033 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001034 p.half = undef;
1035 p.zero = undef;
1036 p.one = undef;
1037
Keith Whitwell15e75e02005-04-29 15:11:39 +00001038 p.last_tex_stage = 0;
Keith Whitwell93cd9232005-05-11 08:30:23 +00001039 release_temps(&p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001040
Keith Whitwellce721142005-07-11 10:10:38 +00001041 if (key->enabled_units) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001042 /* First pass - to support texture_env_crossbar, first identify
1043 * all referenced texture sources and emit texld instructions
1044 * for each:
1045 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001046 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001047 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001048 load_texunit_sources( &p, unit );
1049 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001050 }
1051
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001052 /* Second pass - emit combine instructions to build final color:
1053 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001054 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001055 if (key->enabled_units & (1<<unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001056 p.src_previous = emit_texenv( &p, unit );
Keith Whitwell93cd9232005-05-11 08:30:23 +00001057 release_temps(&p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001058 }
1059 }
1060
Keith Whitwellce721142005-07-11 10:10:38 +00001061 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul90ebb582005-11-02 18:06:12 +00001062 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001063
Keith Whitwellce721142005-07-11 10:10:38 +00001064 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001065 /* Emit specular add.
1066 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001067 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001068 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1069 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001070 }
Brian Paulaa206952005-09-16 18:14:24 +00001071 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001072 /* Will wind up in here if no texture enabled or a couple of
1073 * other scenarios (GL_REPLACE for instance).
1074 */
Brian Paul7e807512005-11-05 17:10:45 +00001075 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001076 }
1077
Keith Whitwell47b29f52005-05-04 11:44:44 +00001078 /* Finish up:
1079 */
Brian Paul7e807512005-11-05 17:10:45 +00001080 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001081
Keith Whitwellce721142005-07-11 10:10:38 +00001082 if (key->fog_enabled) {
1083 /* Pull fog mode from GLcontext, the value in the state key is
1084 * a reduced value and not what is expected in FogOption
1085 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001086 p.program->FogOption = ctx->Fog.Mode;
Keith Whitwellce721142005-07-11 10:10:38 +00001087 } else
Keith Whitwell276330b2005-05-09 17:58:13 +00001088 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001089
Brian21f99792007-01-09 11:00:21 -07001090 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001091 program_error(&p, "Exceeded max nr indirect texture lookups");
1092
Brian21f99792007-01-09 11:00:21 -07001093 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001094 program_error(&p, "Exceeded max TEX instructions");
1095
Brian21f99792007-01-09 11:00:21 -07001096 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001097 program_error(&p, "Exceeded max ALU instructions");
1098
Brian Paul5d7b49f2005-11-19 23:12:20 +00001099 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001100
Brian Paule998c342006-10-29 21:17:18 +00001101 /* Allocate final instruction array */
Brian Paule998c342006-10-29 21:17:18 +00001102 program->Base.Instructions
1103 = _mesa_alloc_instructions(program->Base.NumInstructions);
1104 if (!program->Base.Instructions) {
1105 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1106 "generating tex env program");
1107 return;
1108 }
Brian1240eb22007-03-22 08:50:20 -06001109 _mesa_copy_instructions(program->Base.Instructions, instBuffer,
1110 program->Base.NumInstructions);
Brian Paule998c342006-10-29 21:17:18 +00001111
Keith Whitwelldbeea252005-05-10 13:57:50 +00001112 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001113 */
Brian Paule998c342006-10-29 21:17:18 +00001114 if (ctx->Driver.ProgramStringNotify) {
1115 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1116 &p.program->Base );
1117 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001118
Brian Paule998c342006-10-29 21:17:18 +00001119 if (DISASSEM) {
1120 _mesa_print_program(&p.program->Base);
1121 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001122 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001123}
1124
Brian Paul5d7b49f2005-11-19 23:12:20 +00001125
Brian Paul122629f2006-07-20 16:49:57 +00001126static struct gl_fragment_program *
Brian Pauld9736db2006-05-23 02:44:46 +00001127search_cache(const struct texenvprog_cache *cache,
1128 GLuint hash,
Brian Paule4cb9cd2006-05-30 22:15:24 +00001129 const void *key,
Brian Pauld9736db2006-05-23 02:44:46 +00001130 GLuint keysize)
Keith Whitwellce721142005-07-11 10:10:38 +00001131{
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001132 struct texenvprog_cache_item *c;
Keith Whitwellce721142005-07-11 10:10:38 +00001133
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001134 for (c = cache->items[hash % cache->size]; c; c = c->next) {
1135 if (c->hash == hash && memcmp(c->key, key, keysize) == 0)
Brian Paul122629f2006-07-20 16:49:57 +00001136 return (struct gl_fragment_program *) c->data;
Keith Whitwellce721142005-07-11 10:10:38 +00001137 }
1138
1139 return NULL;
1140}
1141
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001142static void rehash( struct texenvprog_cache *cache )
1143{
1144 struct texenvprog_cache_item **items;
1145 struct texenvprog_cache_item *c, *next;
1146 GLuint size, i;
1147
1148 size = cache->size * 3;
1149 items = (struct texenvprog_cache_item**) _mesa_malloc(size * sizeof(*items));
1150 _mesa_memset(items, 0, size * sizeof(*items));
1151
1152 for (i = 0; i < cache->size; i++)
1153 for (c = cache->items[i]; c; c = next) {
1154 next = c->next;
1155 c->next = items[c->hash % size];
1156 items[c->hash % size] = c;
1157 }
1158
1159 _mesa_free(cache->items);
1160 cache->items = items;
1161 cache->size = size;
1162}
1163
Keith Whitwell8065c122006-05-22 16:09:27 +00001164static void clear_cache( struct texenvprog_cache *cache )
1165{
1166 struct texenvprog_cache_item *c, *next;
1167 GLuint i;
1168
1169 for (i = 0; i < cache->size; i++) {
1170 for (c = cache->items[i]; c; c = next) {
1171 next = c->next;
1172 _mesa_free(c->key);
Brian Paul122629f2006-07-20 16:49:57 +00001173 cache->ctx->Driver.DeleteProgram(cache->ctx,
1174 (struct gl_program *) c->data);
Keith Whitwell8065c122006-05-22 16:09:27 +00001175 _mesa_free(c);
1176 }
1177 cache->items[i] = NULL;
1178 }
1179
1180
1181 cache->n_items = 0;
1182}
1183
1184
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001185static void cache_item( struct texenvprog_cache *cache,
Keith Whitwellce721142005-07-11 10:10:38 +00001186 GLuint hash,
Brian Paulb8f2f6f2006-05-23 01:55:31 +00001187 const struct state_key *key,
Keith Whitwellce721142005-07-11 10:10:38 +00001188 void *data )
1189{
Brian18d1fde2007-01-23 11:46:02 -07001190 struct texenvprog_cache_item *c
1191 = (struct texenvprog_cache_item *) MALLOC(sizeof(*c));
Keith Whitwellce721142005-07-11 10:10:38 +00001192 c->hash = hash;
Keith Whitwell8065c122006-05-22 16:09:27 +00001193
1194 c->key = _mesa_malloc(sizeof(*key));
1195 memcpy(c->key, key, sizeof(*key));
1196
Brian18d1fde2007-01-23 11:46:02 -07001197 c->data = (struct gl_fragment_program *) data;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001198
Keith Whitwell8065c122006-05-22 16:09:27 +00001199 if (cache->n_items > cache->size * 1.5) {
1200 if (cache->size < 1000)
1201 rehash(cache);
1202 else
1203 clear_cache(cache);
1204 }
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001205
Keith Whitwell8065c122006-05-22 16:09:27 +00001206 cache->n_items++;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001207 c->next = cache->items[hash % cache->size];
1208 cache->items[hash % cache->size] = c;
Keith Whitwellce721142005-07-11 10:10:38 +00001209}
1210
Brian Pauld9736db2006-05-23 02:44:46 +00001211static GLuint hash_key( const struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +00001212{
1213 GLuint *ikey = (GLuint *)key;
1214 GLuint hash = 0, i;
1215
Keith Whitwell8065c122006-05-22 16:09:27 +00001216 /* Make a slightly better attempt at a hash function:
Keith Whitwellce721142005-07-11 10:10:38 +00001217 */
Keith Whitwell8065c122006-05-22 16:09:27 +00001218 for (i = 0; i < sizeof(*key)/sizeof(*ikey); i++)
1219 {
1220 hash += ikey[i];
1221 hash += (hash << 10);
1222 hash ^= (hash >> 6);
1223 }
Keith Whitwellce721142005-07-11 10:10:38 +00001224
1225 return hash;
1226}
1227
Briana90046f2006-12-15 10:07:26 -07001228
1229/**
1230 * If _MaintainTexEnvProgram is set we'll generate a fragment program that
1231 * implements the current texture env/combine mode.
1232 * This function generates that program and puts it into effect.
1233 */
1234void
1235_mesa_UpdateTexEnvProgram( GLcontext *ctx )
Keith Whitwellce721142005-07-11 10:10:38 +00001236{
Keith Whitwell8065c122006-05-22 16:09:27 +00001237 struct state_key key;
Keith Whitwellce721142005-07-11 10:10:38 +00001238 GLuint hash;
Brian Paul122629f2006-07-20 16:49:57 +00001239 const struct gl_fragment_program *prev = ctx->FragmentProgram._Current;
Keith Whitwellce721142005-07-11 10:10:38 +00001240
Briana90046f2006-12-15 10:07:26 -07001241 ASSERT(ctx->FragmentProgram._MaintainTexEnvProgram);
1242
1243 /* If a conventional fragment program/shader isn't in effect... */
Brianefcfdbd2007-02-24 15:51:41 -07001244 if (!ctx->FragmentProgram._Enabled &&
1245 !ctx->Shader.CurrentProgram) {
Keith Whitwell8065c122006-05-22 16:09:27 +00001246 make_state_key(ctx, &key);
1247 hash = hash_key(&key);
Keith Whitwellc3626a92005-11-01 17:25:49 +00001248
Brian Pauld9736db2006-05-23 02:44:46 +00001249 ctx->FragmentProgram._Current =
Briana90046f2006-12-15 10:07:26 -07001250 ctx->FragmentProgram._TexEnvProgram =
1251 search_cache(&ctx->Texture.env_fp_cache, hash, &key, sizeof(key));
Keith Whitwellce721142005-07-11 10:10:38 +00001252
Briana90046f2006-12-15 10:07:26 -07001253 if (!ctx->FragmentProgram._TexEnvProgram) {
1254 if (0)
1255 _mesa_printf("Building new texenv proggy for key %x\n", hash);
1256
1257 /* create new tex env program */
Brianefcfdbd2007-02-24 15:51:41 -07001258 ctx->FragmentProgram._Current =
1259 ctx->FragmentProgram._TexEnvProgram =
1260 (struct gl_fragment_program *)
Briana90046f2006-12-15 10:07:26 -07001261 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1262
1263 create_new_program(ctx, &key, ctx->FragmentProgram._TexEnvProgram);
1264
1265 cache_item(&ctx->Texture.env_fp_cache, hash, &key,
1266 ctx->FragmentProgram._TexEnvProgram);
Keith Whitwellc3626a92005-11-01 17:25:49 +00001267 }
Briana90046f2006-12-15 10:07:26 -07001268 else {
1269 if (0)
1270 _mesa_printf("Found existing texenv program for key %x\n", hash);
Keith Whitwellc3626a92005-11-01 17:25:49 +00001271 }
1272 }
1273 else {
1274 ctx->FragmentProgram._Current = ctx->FragmentProgram.Current;
Keith Whitwellce721142005-07-11 10:10:38 +00001275 }
Keith Whitwellc3626a92005-11-01 17:25:49 +00001276
1277 /* Tell the driver about the change. Could define a new target for
1278 * this?
1279 */
Brian Paul5d7b49f2005-11-19 23:12:20 +00001280 if (ctx->FragmentProgram._Current != prev && ctx->Driver.BindProgram) {
1281 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
Briana90046f2006-12-15 10:07:26 -07001282 (struct gl_program *) ctx->FragmentProgram._Current);
Brian Paul5d7b49f2005-11-19 23:12:20 +00001283 }
Keith Whitwellce721142005-07-11 10:10:38 +00001284}
1285
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001286
1287void _mesa_TexEnvProgramCacheInit( GLcontext *ctx )
1288{
Keith Whitwell8065c122006-05-22 16:09:27 +00001289 ctx->Texture.env_fp_cache.ctx = ctx;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001290 ctx->Texture.env_fp_cache.size = 17;
1291 ctx->Texture.env_fp_cache.n_items = 0;
1292 ctx->Texture.env_fp_cache.items = (struct texenvprog_cache_item **)
1293 _mesa_calloc(ctx->Texture.env_fp_cache.size *
1294 sizeof(struct texenvprog_cache_item));
1295}
1296
1297
Keith Whitwellce721142005-07-11 10:10:38 +00001298void _mesa_TexEnvProgramCacheDestroy( GLcontext *ctx )
1299{
Keith Whitwell8065c122006-05-22 16:09:27 +00001300 clear_cache(&ctx->Texture.env_fp_cache);
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001301 _mesa_free(ctx->Texture.env_fp_cache.items);
Keith Whitwellce721142005-07-11 10:10:38 +00001302}