blob: 68a4db919749a8082e8945d55febb629f0786cd6 [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"
Brian Paul5b5c9312008-05-07 18:51:44 -060031#include "shader/program.h"
Brianc223c6b2007-07-04 13:15:20 -060032#include "shader/prog_parameter.h"
33#include "shader/prog_instruction.h"
34#include "shader/prog_print.h"
35#include "shader/prog_statevars.h"
Keith Whitwell15e75e02005-04-29 15:11:39 +000036#include "texenvprogram.h"
37
Brian Paul5b5c9312008-05-07 18:51:44 -060038
39struct texenvprog_cache_item
40{
41 GLuint hash;
42 void *key;
43 struct gl_fragment_program *data;
44 struct texenvprog_cache_item *next;
45};
46
47
Brian Paule998c342006-10-29 21:17:18 +000048/**
Brian2a8e9bb2007-10-23 10:24:53 -060049 * This MAX is probably a bit generous, but that's OK. There can be
50 * up to four instructions per texture unit (TEX + 3 for combine),
51 * then there's fog and specular add.
Brian Paule998c342006-10-29 21:17:18 +000052 */
Brian2a8e9bb2007-10-23 10:24:53 -060053#define MAX_INSTRUCTIONS ((MAX_TEXTURE_UNITS * 4) + 12)
Keith Whitwell15e75e02005-04-29 15:11:39 +000054
Keith Whitwell269e3892005-05-12 10:28:43 +000055#define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
Keith Whitwell15e75e02005-04-29 15:11:39 +000056
Keith Whitwellce721142005-07-11 10:10:38 +000057struct mode_opt {
Brian Paul5d7b49f2005-11-19 23:12:20 +000058 GLuint Source:4;
59 GLuint Operand:3;
Keith Whitwellce721142005-07-11 10:10:38 +000060};
61
62struct state_key {
Brian Paul5d7b49f2005-11-19 23:12:20 +000063 GLbitfield enabled_units;
64 GLuint separate_specular:1;
65 GLuint fog_enabled:1;
66 GLuint fog_mode:2;
Keith Whitwellce721142005-07-11 10:10:38 +000067
68 struct {
Brian Paul5d7b49f2005-11-19 23:12:20 +000069 GLuint enabled:1;
70 GLuint source_index:3; /* one of TEXTURE_1D/2D/3D/CUBE/RECT_INDEX */
71 GLuint ScaleShiftRGB:2;
72 GLuint ScaleShiftA:2;
Keith Whitwellce721142005-07-11 10:10:38 +000073
Brian Paul5d7b49f2005-11-19 23:12:20 +000074 GLuint NumArgsRGB:2;
75 GLuint ModeRGB:4;
Keith Whitwellce721142005-07-11 10:10:38 +000076 struct mode_opt OptRGB[3];
77
Brian Paul5d7b49f2005-11-19 23:12:20 +000078 GLuint NumArgsA:2;
79 GLuint ModeA:4;
Keith Whitwellce721142005-07-11 10:10:38 +000080 struct mode_opt OptA[3];
81 } unit[8];
82};
83
84#define FOG_LINEAR 0
85#define FOG_EXP 1
86#define FOG_EXP2 2
87#define FOG_UNKNOWN 3
88
89static GLuint translate_fog_mode( GLenum mode )
90{
91 switch (mode) {
92 case GL_LINEAR: return FOG_LINEAR;
93 case GL_EXP: return FOG_EXP;
94 case GL_EXP2: return FOG_EXP2;
95 default: return FOG_UNKNOWN;
96 }
97}
98
99#define OPR_SRC_COLOR 0
100#define OPR_ONE_MINUS_SRC_COLOR 1
101#define OPR_SRC_ALPHA 2
102#define OPR_ONE_MINUS_SRC_ALPHA 3
103#define OPR_ZERO 4
104#define OPR_ONE 5
105#define OPR_UNKNOWN 7
106
107static GLuint translate_operand( GLenum operand )
108{
109 switch (operand) {
110 case GL_SRC_COLOR: return OPR_SRC_COLOR;
111 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
112 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
113 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
114 case GL_ZERO: return OPR_ZERO;
115 case GL_ONE: return OPR_ONE;
116 default: return OPR_UNKNOWN;
117 }
118}
119
120#define SRC_TEXTURE 0
121#define SRC_TEXTURE0 1
122#define SRC_TEXTURE1 2
123#define SRC_TEXTURE2 3
124#define SRC_TEXTURE3 4
125#define SRC_TEXTURE4 5
126#define SRC_TEXTURE5 6
127#define SRC_TEXTURE6 7
128#define SRC_TEXTURE7 8
129#define SRC_CONSTANT 9
130#define SRC_PRIMARY_COLOR 10
131#define SRC_PREVIOUS 11
132#define SRC_UNKNOWN 15
133
134static GLuint translate_source( GLenum src )
135{
136 switch (src) {
137 case GL_TEXTURE: return SRC_TEXTURE;
138 case GL_TEXTURE0:
139 case GL_TEXTURE1:
140 case GL_TEXTURE2:
141 case GL_TEXTURE3:
142 case GL_TEXTURE4:
143 case GL_TEXTURE5:
144 case GL_TEXTURE6:
145 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
146 case GL_CONSTANT: return SRC_CONSTANT;
147 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
148 case GL_PREVIOUS: return SRC_PREVIOUS;
149 default: return SRC_UNKNOWN;
150 }
151}
152
153#define MODE_REPLACE 0
154#define MODE_MODULATE 1
155#define MODE_ADD 2
156#define MODE_ADD_SIGNED 3
157#define MODE_INTERPOLATE 4
158#define MODE_SUBTRACT 5
159#define MODE_DOT3_RGB 6
160#define MODE_DOT3_RGB_EXT 7
161#define MODE_DOT3_RGBA 8
162#define MODE_DOT3_RGBA_EXT 9
163#define MODE_MODULATE_ADD_ATI 10
164#define MODE_MODULATE_SIGNED_ADD_ATI 11
165#define MODE_MODULATE_SUBTRACT_ATI 12
166#define MODE_UNKNOWN 15
167
168static GLuint translate_mode( GLenum mode )
169{
170 switch (mode) {
171 case GL_REPLACE: return MODE_REPLACE;
172 case GL_MODULATE: return MODE_MODULATE;
173 case GL_ADD: return MODE_ADD;
174 case GL_ADD_SIGNED: return MODE_ADD_SIGNED;
175 case GL_INTERPOLATE: return MODE_INTERPOLATE;
176 case GL_SUBTRACT: return MODE_SUBTRACT;
177 case GL_DOT3_RGB: return MODE_DOT3_RGB;
178 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
179 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
180 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
181 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
182 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
183 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
184 default: return MODE_UNKNOWN;
185 }
186}
187
188#define TEXTURE_UNKNOWN_INDEX 7
Brian Paule00ac112005-09-15 05:00:45 +0000189static GLuint translate_tex_src_bit( GLbitfield bit )
Keith Whitwellce721142005-07-11 10:10:38 +0000190{
191 switch (bit) {
192 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
193 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
194 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
195 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
196 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
197 default: return TEXTURE_UNKNOWN_INDEX;
198 }
199}
200
Brian Paul22db5352005-11-19 23:45:10 +0000201/**
202 * Examine current texture environment state and generate a unique
203 * key to identify it.
204 */
Keith Whitwell8065c122006-05-22 16:09:27 +0000205static void make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000206{
Keith Whitwellce721142005-07-11 10:10:38 +0000207 GLuint i, j;
208
Keith Whitwell8065c122006-05-22 16:09:27 +0000209 memset(key, 0, sizeof(*key));
210
Keith Whitwellce721142005-07-11 10:10:38 +0000211 for (i=0;i<MAX_TEXTURE_UNITS;i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000212 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Roland Scheideggerbf4a0fa2008-02-15 17:26:06 +0100213
214 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
Keith Whitwellce721142005-07-11 10:10:38 +0000215 continue;
216
217 key->unit[i].enabled = 1;
218 key->enabled_units |= (1<<i);
219
220 key->unit[i].source_index =
221 translate_tex_src_bit(texUnit->_ReallyEnabled);
222
223 key->unit[i].NumArgsRGB = texUnit->_CurrentCombine->_NumArgsRGB;
224 key->unit[i].NumArgsA = texUnit->_CurrentCombine->_NumArgsA;
225
226 key->unit[i].ModeRGB =
227 translate_mode(texUnit->_CurrentCombine->ModeRGB);
228 key->unit[i].ModeA =
229 translate_mode(texUnit->_CurrentCombine->ModeA);
230
231 key->unit[i].ScaleShiftRGB = texUnit->_CurrentCombine->ScaleShiftRGB;
Keith Whitwell64da1612006-05-22 14:30:58 +0000232 key->unit[i].ScaleShiftA = texUnit->_CurrentCombine->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000233
234 for (j=0;j<3;j++) {
235 key->unit[i].OptRGB[j].Operand =
236 translate_operand(texUnit->_CurrentCombine->OperandRGB[j]);
237 key->unit[i].OptA[j].Operand =
238 translate_operand(texUnit->_CurrentCombine->OperandA[j]);
239 key->unit[i].OptRGB[j].Source =
240 translate_source(texUnit->_CurrentCombine->SourceRGB[j]);
241 key->unit[i].OptA[j].Source =
242 translate_source(texUnit->_CurrentCombine->SourceA[j]);
243 }
244 }
245
246 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
247 key->separate_specular = 1;
248
249 if (ctx->Fog.Enabled) {
250 key->fog_enabled = 1;
251 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
252 }
Keith Whitwellce721142005-07-11 10:10:38 +0000253}
254
Keith Whitwell15e75e02005-04-29 15:11:39 +0000255/* Use uregs to represent registers internally, translate to Mesa's
256 * expected formats on emit.
257 *
258 * NOTE: These are passed by value extensively in this file rather
259 * than as usual by pointer reference. If this disturbs you, try
260 * remembering they are just 32bits in size.
261 *
262 * GCC is smart enough to deal with these dword-sized structures in
263 * much the same way as if I had defined them as dwords and was using
264 * macros to access and set the fields. This is much nicer and easier
265 * to evolve.
266 */
267struct ureg {
268 GLuint file:4;
269 GLuint idx:8;
270 GLuint negatebase:1;
271 GLuint abs:1;
272 GLuint negateabs:1;
273 GLuint swz:12;
274 GLuint pad:5;
275};
276
Brian Paul13abf912006-04-13 19:17:13 +0000277static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000278 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000279 ~0,
280 0,
281 0,
282 0,
283 0,
284 0
285};
286
Keith Whitwell15e75e02005-04-29 15:11:39 +0000287
Keith Whitwell15e75e02005-04-29 15:11:39 +0000288/* State used to build the fragment program:
289 */
290struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000291 struct gl_fragment_program *program;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000292 GLcontext *ctx;
Keith Whitwellce721142005-07-11 10:10:38 +0000293 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000294
Brian Paulc8d17412005-12-14 03:06:16 +0000295 GLbitfield alu_temps; /* Track texture indirections, see spec. */
296 GLbitfield temps_output; /* Track texture indirections, see spec. */
297 GLbitfield temp_in_use; /* Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000298 GLboolean error;
299
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000300 struct ureg src_texture[MAX_TEXTURE_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000301 /* Reg containing each texture unit's sampled texture color,
302 * else undef.
303 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000304
305 struct ureg src_previous; /* Reg containing color from previous
306 * stage. May need to be decl'd.
307 */
308
309 GLuint last_tex_stage; /* Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000310
311 struct ureg half;
312 struct ureg one;
313 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000314};
315
316
317
318static struct ureg make_ureg(GLuint file, GLuint idx)
319{
320 struct ureg reg;
321 reg.file = file;
322 reg.idx = idx;
323 reg.negatebase = 0;
324 reg.abs = 0;
325 reg.negateabs = 0;
326 reg.swz = SWIZZLE_NOOP;
327 reg.pad = 0;
328 return reg;
329}
330
331static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
332{
333 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
334 GET_SWZ(reg.swz, y),
335 GET_SWZ(reg.swz, z),
336 GET_SWZ(reg.swz, w));
337
338 return reg;
339}
340
341static struct ureg swizzle1( struct ureg reg, int x )
342{
343 return swizzle(reg, x, x, x, x);
344}
345
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000346static struct ureg negate( struct ureg reg )
347{
348 reg.negatebase ^= 1;
349 return reg;
350}
351
Keith Whitwell15e75e02005-04-29 15:11:39 +0000352static GLboolean is_undef( struct ureg reg )
353{
Alan Hourihane8d972652006-08-10 13:12:00 +0000354 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000355}
356
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000357
Keith Whitwell15e75e02005-04-29 15:11:39 +0000358static struct ureg get_temp( struct texenv_fragment_program *p )
359{
Brian Paul13abf912006-04-13 19:17:13 +0000360 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000361
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000362 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000363 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000364 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000365
366 /* Then any unused temporary:
367 */
368 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000369 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000370
Keith Whitwell15e75e02005-04-29 15:11:39 +0000371 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000372 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000373 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000374 }
375
Brian Paul13abf912006-04-13 19:17:13 +0000376 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000377 p->program->Base.NumTemporaries = bit;
378
Keith Whitwell93cd9232005-05-11 08:30:23 +0000379 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000380 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
381}
382
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000383static struct ureg get_tex_temp( struct texenv_fragment_program *p )
384{
385 int bit;
386
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000387 /* First try to find availble temp not previously used (to avoid
388 * starting a new texture indirection). According to the spec, the
389 * ~p->temps_output isn't necessary, but will keep it there for
390 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000391 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000392 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000393
394 /* Then any unused temporary:
395 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000396 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000397 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000398
399 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000400 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000401 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000402 }
403
Brian Paul13abf912006-04-13 19:17:13 +0000404 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000405 p->program->Base.NumTemporaries = bit;
406
Keith Whitwell93cd9232005-05-11 08:30:23 +0000407 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000408 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
409}
410
Keith Whitwell15e75e02005-04-29 15:11:39 +0000411
412static void release_temps( struct texenv_fragment_program *p )
413{
Brian Paul05051032005-11-01 04:36:33 +0000414 GLuint max_temp = p->ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000415
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000416 /* KW: To support tex_env_crossbar, don't release the registers in
417 * temps_output.
418 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000419 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000420 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000421 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000422 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000423}
424
425
Brian9fe3e2e2007-02-23 11:44:14 -0700426static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000427 GLint s0,
428 GLint s1,
429 GLint s2,
430 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700431 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000432{
Brian9fe3e2e2007-02-23 11:44:14 -0700433 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000434 GLuint idx;
435 tokens[0] = s0;
436 tokens[1] = s1;
437 tokens[2] = s2;
438 tokens[3] = s3;
439 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000440 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000441 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000442}
443
Keith Whitwell47b29f52005-05-04 11:44:44 +0000444
Brian9fe3e2e2007-02-23 11:44:14 -0700445#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
446#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
447#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
448#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000449
450
451static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
452{
Brian Paulde997602005-11-12 17:53:14 +0000453 p->program->Base.InputsRead |= (1 << input);
Keith Whitwell47b29f52005-05-04 11:44:44 +0000454 return make_ureg(PROGRAM_INPUT, input);
455}
456
457
Brian Paul7e807512005-11-05 17:10:45 +0000458static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000459 struct ureg ureg )
460{
461 reg->File = ureg.file;
462 reg->Index = ureg.idx;
463 reg->Swizzle = ureg.swz;
Keith Whitwellf2802c42005-10-07 09:55:26 +0000464 reg->NegateBase = ureg.negatebase ? 0xf : 0x0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000465 reg->Abs = ureg.abs;
466 reg->NegateAbs = ureg.negateabs;
467}
468
Brian Paul7e807512005-11-05 17:10:45 +0000469static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000470 struct ureg ureg, GLuint mask )
471{
472 dst->File = ureg.file;
473 dst->Index = ureg.idx;
474 dst->WriteMask = mask;
Brianc9d495c2007-10-23 10:55:24 -0600475 dst->CondMask = COND_TR; /* always pass cond test */
476 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000477}
478
Brian Paul7e807512005-11-05 17:10:45 +0000479static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000480emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000481 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000482 struct ureg dest,
483 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000484 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000485 struct ureg src0,
486 struct ureg src1,
487 struct ureg src2 )
488{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000489 GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000490 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Brian2a8e9bb2007-10-23 10:24:53 -0600491
492 assert(nr < MAX_INSTRUCTIONS);
493
Brian Pauld6272e02006-10-29 18:03:16 +0000494 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000495 inst->Opcode = op;
496
Keith Whitwell47b29f52005-05-04 11:44:44 +0000497 emit_arg( &inst->SrcReg[0], src0 );
498 emit_arg( &inst->SrcReg[1], src1 );
499 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000500
Brian Paule31ac052005-11-20 17:52:40 +0000501 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000502
503 emit_dst( &inst->DstReg, dest, mask );
504
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000505 /* Accounting for indirection tracking:
506 */
507 if (dest.file == PROGRAM_TEMPORARY)
508 p->temps_output |= 1 << dest.idx;
509
Keith Whitwell15e75e02005-04-29 15:11:39 +0000510 return inst;
511}
512
513
514static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000515 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000516 struct ureg dest,
517 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000518 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000519 struct ureg src0,
520 struct ureg src1,
521 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000522{
523 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
524
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000525 /* Accounting for indirection tracking:
526 */
527 if (src0.file == PROGRAM_TEMPORARY)
528 p->alu_temps |= 1 << src0.idx;
529
530 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
531 p->alu_temps |= 1 << src1.idx;
532
533 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
534 p->alu_temps |= 1 << src2.idx;
535
536 if (dest.file == PROGRAM_TEMPORARY)
537 p->alu_temps |= 1 << dest.idx;
538
Brian21f99792007-01-09 11:00:21 -0700539 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000540 return dest;
541}
542
543static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000544 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000545 struct ureg dest,
546 GLuint destmask,
547 GLuint tex_unit,
548 GLuint tex_idx,
549 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000550{
Brian Paul7e807512005-11-05 17:10:45 +0000551 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000552 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000553 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000554 coord, /* arg 0? */
555 undef,
556 undef);
557
Brian Paul7e807512005-11-05 17:10:45 +0000558 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000559 inst->TexSrcUnit = tex_unit;
560
Brian21f99792007-01-09 11:00:21 -0700561 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000562
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000563 /* Is this a texture indirection?
564 */
565 if ((coord.file == PROGRAM_TEMPORARY &&
566 (p->temps_output & (1<<coord.idx))) ||
567 (dest.file == PROGRAM_TEMPORARY &&
568 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700569 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000570 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000571 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000572 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000573 }
574
575 return dest;
576}
577
578
Keith Whitwell47b29f52005-05-04 11:44:44 +0000579static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000580 GLfloat s0,
581 GLfloat s1,
582 GLfloat s2,
583 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000584{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000585 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700586 GLuint idx, swizzle;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000587 values[0] = s0;
588 values[1] = s1;
589 values[2] = s2;
590 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700591 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
592 &swizzle );
593 ASSERT(swizzle == SWIZZLE_NOOP);
Keith Whitwell47b29f52005-05-04 11:44:44 +0000594 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000595}
596
Keith Whitwelle4902422005-05-11 15:16:35 +0000597#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000598#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
599#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
600#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000601
602
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000603static struct ureg get_one( struct texenv_fragment_program *p )
604{
605 if (is_undef(p->one))
606 p->one = register_scalar_const(p, 1.0);
607 return p->one;
608}
609
610static struct ureg get_half( struct texenv_fragment_program *p )
611{
612 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000613 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000614 return p->half;
615}
616
617static struct ureg get_zero( struct texenv_fragment_program *p )
618{
619 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000620 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000621 return p->zero;
622}
623
Keith Whitwell15e75e02005-04-29 15:11:39 +0000624
Keith Whitwell15e75e02005-04-29 15:11:39 +0000625static void program_error( struct texenv_fragment_program *p, const char *msg )
626{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000627 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000628 p->error = 1;
629}
630
Keith Whitwell15e75e02005-04-29 15:11:39 +0000631static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000632 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000633{
634 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000635 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000636 assert(!is_undef(p->src_texture[unit]));
637 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000638
Keith Whitwellce721142005-07-11 10:10:38 +0000639 case SRC_TEXTURE0:
640 case SRC_TEXTURE1:
641 case SRC_TEXTURE2:
642 case SRC_TEXTURE3:
643 case SRC_TEXTURE4:
644 case SRC_TEXTURE5:
645 case SRC_TEXTURE6:
646 case SRC_TEXTURE7:
647 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
648 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000649
Keith Whitwellce721142005-07-11 10:10:38 +0000650 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000651 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000652
Keith Whitwellce721142005-07-11 10:10:38 +0000653 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000654 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000655
Keith Whitwellce721142005-07-11 10:10:38 +0000656 case SRC_PREVIOUS:
657 default:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000658 if (is_undef(p->src_previous))
659 return register_input(p, FRAG_ATTRIB_COL0);
660 else
661 return p->src_previous;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000662 }
663}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000664
Keith Whitwell15e75e02005-04-29 15:11:39 +0000665static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000666 GLuint mask,
667 GLuint unit,
668 GLuint source,
669 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000670{
671 struct ureg arg, src, one;
672
673 src = get_source(p, source, unit);
674
675 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000676 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000677 /* Get unused tmp,
678 * Emit tmp = 1.0 - arg.xyzw
679 */
680 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000681 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000682 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000683
Keith Whitwellce721142005-07-11 10:10:38 +0000684 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000685 if (mask == WRITEMASK_W)
686 return src;
687 else
Brian Paul22db5352005-11-19 23:45:10 +0000688 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000689 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000690 /* Get unused tmp,
691 * Emit tmp = 1.0 - arg.wwww
692 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000693 arg = get_temp(p);
694 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000695 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000696 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000697 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000698 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000699 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000700 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000701 case OPR_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000702 default:
703 return src;
704 }
705}
706
Keith Whitwellce721142005-07-11 10:10:38 +0000707static GLboolean args_match( struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000708{
Brian Paul22db5352005-11-19 23:45:10 +0000709 GLuint i, nr = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000710
711 for (i = 0 ; i < nr ; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000712 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000713 return GL_FALSE;
714
Keith Whitwellce721142005-07-11 10:10:38 +0000715 switch(key->unit[unit].OptA[i].Operand) {
716 case OPR_SRC_ALPHA:
717 switch(key->unit[unit].OptRGB[i].Operand) {
718 case OPR_SRC_COLOR:
719 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000720 break;
721 default:
722 return GL_FALSE;
723 }
724 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000725 case OPR_ONE_MINUS_SRC_ALPHA:
726 switch(key->unit[unit].OptRGB[i].Operand) {
727 case OPR_ONE_MINUS_SRC_COLOR:
728 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000729 break;
730 default:
731 return GL_FALSE;
732 }
733 break;
734 default:
735 return GL_FALSE; /* impossible */
736 }
737 }
738
739 return GL_TRUE;
740}
741
Keith Whitwell15e75e02005-04-29 15:11:39 +0000742static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000743 struct ureg dest,
744 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000745 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000746 GLuint unit,
747 GLuint nr,
748 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +0000749 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000750{
Keith Whitwell15e75e02005-04-29 15:11:39 +0000751 struct ureg src[3];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000752 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +0000753 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000754
Brian Paul00630842005-12-12 15:27:55 +0000755 tmp = undef; /* silence warning (bug 5318) */
756
Keith Whitwell15e75e02005-04-29 15:11:39 +0000757 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +0000758 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000759
760 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +0000761 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000762 if (mask == WRITEMASK_XYZW && !saturate)
763 return src[0];
764 else
Brian Paul7e807512005-11-05 17:10:45 +0000765 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000766 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +0000767 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000768 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000769 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +0000770 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000771 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000772 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000773 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000774 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +0000775 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000776 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +0000777 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +0000778 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
779 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000780 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +0000781 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000782 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
783 */
Brian Paul7e807512005-11-05 17:10:45 +0000784 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000785
Keith Whitwellce721142005-07-11 10:10:38 +0000786 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +0000787 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000788
Keith Whitwellce721142005-07-11 10:10:38 +0000789 case MODE_DOT3_RGBA:
790 case MODE_DOT3_RGBA_EXT:
791 case MODE_DOT3_RGB_EXT:
792 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000793 struct ureg tmp0 = get_temp( p );
794 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +0000795 struct ureg neg1 = register_scalar_const(p, -1);
796 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000797
798 /* tmp0 = 2*src0 - 1
799 * tmp1 = 2*src1 - 1
800 *
801 * dst = tmp0 dot3 tmp1
802 */
Brian Paul7e807512005-11-05 17:10:45 +0000803 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000804 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000805
Brian Paulaa206952005-09-16 18:14:24 +0000806 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000807 tmp1 = tmp0;
808 else
Brian Paul7e807512005-11-05 17:10:45 +0000809 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000810 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +0000811 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000812 return dest;
813 }
Keith Whitwellce721142005-07-11 10:10:38 +0000814 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000815 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +0000816 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000817 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +0000818 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000819 /* Arg0 * Arg2 + Arg1 - 0.5 */
820 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000821 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +0000822 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
823 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000824 return dest;
825 }
Keith Whitwellce721142005-07-11 10:10:38 +0000826 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000827 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +0000828 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000829 return dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000830 default:
831 return src[0];
832 }
833}
834
Keith Whitwell15e75e02005-04-29 15:11:39 +0000835
Brian Paul22db5352005-11-19 23:45:10 +0000836/**
837 * Generate instructions for one texture unit's env/combiner mode.
838 */
839static struct ureg
840emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000841{
Keith Whitwellce721142005-07-11 10:10:38 +0000842 struct state_key *key = p->state;
Brian Paul22db5352005-11-19 23:45:10 +0000843 GLboolean saturate = (unit < p->last_tex_stage);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000844 GLuint rgb_shift, alpha_shift;
845 struct ureg out, shift;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000846 struct ureg dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000847
Keith Whitwellce721142005-07-11 10:10:38 +0000848 if (!key->unit[unit].enabled) {
849 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000850 }
Keith Whitwellce721142005-07-11 10:10:38 +0000851
852 switch (key->unit[unit].ModeRGB) {
853 case MODE_DOT3_RGB_EXT:
854 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000855 rgb_shift = 0;
856 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000857 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000858 alpha_shift = 0;
859 rgb_shift = 0;
860 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000861 default:
Keith Whitwellce721142005-07-11 10:10:38 +0000862 rgb_shift = key->unit[unit].ScaleShiftRGB;
863 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000864 break;
865 }
Keith Whitwellce721142005-07-11 10:10:38 +0000866
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000867 /* If this is the very last calculation, emit direct to output reg:
868 */
Keith Whitwellce721142005-07-11 10:10:38 +0000869 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000870 unit != p->last_tex_stage ||
871 alpha_shift ||
872 rgb_shift)
873 dest = get_temp( p );
874 else
Brian Paul90ebb582005-11-02 18:06:12 +0000875 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLR);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000876
877 /* Emit the RGB and A combine ops
878 */
Keith Whitwellce721142005-07-11 10:10:38 +0000879 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
880 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000881 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
882 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000883 key->unit[unit].NumArgsRGB,
884 key->unit[unit].ModeRGB,
885 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000886 }
Keith Whitwellce721142005-07-11 10:10:38 +0000887 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +0200888 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000889
890 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
891 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000892 key->unit[unit].NumArgsRGB,
893 key->unit[unit].ModeRGB,
894 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000895 }
896 else {
897 /* Need to do something to stop from re-emitting identical
898 * argument calculations here:
899 */
900 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
901 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000902 key->unit[unit].NumArgsRGB,
903 key->unit[unit].ModeRGB,
904 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000905 out = emit_combine( p, dest, WRITEMASK_W, saturate,
906 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000907 key->unit[unit].NumArgsA,
908 key->unit[unit].ModeA,
909 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000910 }
911
912 /* Deal with the final shift:
913 */
914 if (alpha_shift || rgb_shift) {
915 if (rgb_shift == alpha_shift) {
Keith Whitwelle4902422005-05-11 15:16:35 +0000916 shift = register_scalar_const(p, 1<<rgb_shift);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000917 }
918 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000919 shift = register_const4f(p,
920 1<<rgb_shift,
921 1<<rgb_shift,
922 1<<rgb_shift,
923 1<<alpha_shift);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000924 }
Brian Paul7e807512005-11-05 17:10:45 +0000925 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000926 saturate, out, shift, undef );
927 }
928 else
929 return out;
930}
931
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000932
Brian Paul22db5352005-11-19 23:45:10 +0000933/**
934 * Generate instruction for getting a texture source term.
935 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000936static void load_texture( struct texenv_fragment_program *p, GLuint unit )
937{
938 if (is_undef(p->src_texture[unit])) {
Keith Whitwellce721142005-07-11 10:10:38 +0000939 GLuint dim = p->state->unit[unit].source_index;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000940 struct ureg texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
941 struct ureg tmp = get_tex_temp( p );
942
Brian Paulbfb5ea32005-07-19 18:20:04 +0000943 if (dim == TEXTURE_UNKNOWN_INDEX)
944 program_error(p, "TexSrcBit");
Keith Whitwellce721142005-07-11 10:10:38 +0000945
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000946 /* TODO: Use D0_MASK_XY where possible.
947 */
Aapo Tahkolab8915342006-03-28 10:26:34 +0000948 if (p->state->unit[unit].enabled)
949 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
950 tmp, WRITEMASK_XYZW,
951 unit, dim, texcoord );
952 else
953 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000954 }
955}
956
Keith Whitwell241b6b72005-05-23 09:50:34 +0000957static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000958 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000959{
960 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000961 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000962 load_texture(p, unit);
963 break;
964
Keith Whitwellce721142005-07-11 10:10:38 +0000965 case SRC_TEXTURE0:
966 case SRC_TEXTURE1:
967 case SRC_TEXTURE2:
968 case SRC_TEXTURE3:
969 case SRC_TEXTURE4:
970 case SRC_TEXTURE5:
971 case SRC_TEXTURE6:
972 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +0000973 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000974 break;
975
976 default:
977 break;
978 }
Keith Whitwell241b6b72005-05-23 09:50:34 +0000979
980 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000981}
982
Brian Paul22db5352005-11-19 23:45:10 +0000983
984/**
985 * Generate instructions for loading all texture source terms.
986 */
987static GLboolean
988load_texunit_sources( struct texenv_fragment_program *p, int unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000989{
Keith Whitwellce721142005-07-11 10:10:38 +0000990 struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +0000991 GLuint i;
992
993 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
994 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000995 }
Aapo Tahkolab8915342006-03-28 10:26:34 +0000996
997 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
998 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
999 }
1000
Keith Whitwell241b6b72005-05-23 09:50:34 +00001001 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001002}
1003
Brian Paul22db5352005-11-19 23:45:10 +00001004
1005/**
1006 * Generate a new fragment program which implements the context's
1007 * current texture env/combine mode.
1008 */
1009static void
Brian Paule998c342006-10-29 21:17:18 +00001010create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001011 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001012{
Brian Paule998c342006-10-29 21:17:18 +00001013 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001014 struct texenv_fragment_program p;
1015 GLuint unit;
1016 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001017
Keith Whitwelle4902422005-05-11 15:16:35 +00001018 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +00001019 p.ctx = ctx;
Keith Whitwellce721142005-07-11 10:10:38 +00001020 p.state = key;
1021 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001022
Brian Paule998c342006-10-29 21:17:18 +00001023 /* During code generation, use locally-allocated instruction buffer,
1024 * then alloc dynamic storage below.
1025 */
1026 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001027 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Brian21f99792007-01-09 11:00:21 -07001028 p.program->Base.NumTexIndirections = 1; /* correct? */
1029 p.program->Base.NumTexInstructions = 0;
1030 p.program->Base.NumAluInstructions = 0;
Brian1240eb22007-03-22 08:50:20 -06001031 p.program->Base.String = NULL;
Keith Whitwell9ca88152005-05-10 09:56:02 +00001032 p.program->Base.NumInstructions =
Brian Paule998c342006-10-29 21:17:18 +00001033 p.program->Base.NumTemporaries =
1034 p.program->Base.NumParameters =
1035 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001036 p.program->Base.Parameters = _mesa_new_parameter_list();
Keith Whitwelldbeea252005-05-10 13:57:50 +00001037
Brian Paulde997602005-11-12 17:53:14 +00001038 p.program->Base.InputsRead = 0;
1039 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001040
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001041 for (unit = 0; unit < MAX_TEXTURE_UNITS; unit++)
1042 p.src_texture[unit] = undef;
1043
Keith Whitwell47b29f52005-05-04 11:44:44 +00001044 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001045 p.half = undef;
1046 p.zero = undef;
1047 p.one = undef;
1048
Keith Whitwell15e75e02005-04-29 15:11:39 +00001049 p.last_tex_stage = 0;
Keith Whitwell93cd9232005-05-11 08:30:23 +00001050 release_temps(&p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001051
Keith Whitwellce721142005-07-11 10:10:38 +00001052 if (key->enabled_units) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001053 /* First pass - to support texture_env_crossbar, first identify
1054 * all referenced texture sources and emit texld instructions
1055 * for each:
1056 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001057 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001058 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001059 load_texunit_sources( &p, unit );
1060 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001061 }
1062
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001063 /* Second pass - emit combine instructions to build final color:
1064 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001065 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001066 if (key->enabled_units & (1<<unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001067 p.src_previous = emit_texenv( &p, unit );
Keith Whitwell93cd9232005-05-11 08:30:23 +00001068 release_temps(&p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001069 }
1070 }
1071
Keith Whitwellce721142005-07-11 10:10:38 +00001072 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul90ebb582005-11-02 18:06:12 +00001073 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001074
Keith Whitwellce721142005-07-11 10:10:38 +00001075 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001076 /* Emit specular add.
1077 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001078 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001079 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1080 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001081 }
Brian Paulaa206952005-09-16 18:14:24 +00001082 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001083 /* Will wind up in here if no texture enabled or a couple of
1084 * other scenarios (GL_REPLACE for instance).
1085 */
Brian Paul7e807512005-11-05 17:10:45 +00001086 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001087 }
1088
Keith Whitwell47b29f52005-05-04 11:44:44 +00001089 /* Finish up:
1090 */
Brian Paul7e807512005-11-05 17:10:45 +00001091 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001092
Keith Whitwellce721142005-07-11 10:10:38 +00001093 if (key->fog_enabled) {
1094 /* Pull fog mode from GLcontext, the value in the state key is
1095 * a reduced value and not what is expected in FogOption
1096 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001097 p.program->FogOption = ctx->Fog.Mode;
Keith Whitwellce721142005-07-11 10:10:38 +00001098 } else
Keith Whitwell276330b2005-05-09 17:58:13 +00001099 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001100
Brian21f99792007-01-09 11:00:21 -07001101 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001102 program_error(&p, "Exceeded max nr indirect texture lookups");
1103
Brian21f99792007-01-09 11:00:21 -07001104 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001105 program_error(&p, "Exceeded max TEX instructions");
1106
Brian21f99792007-01-09 11:00:21 -07001107 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001108 program_error(&p, "Exceeded max ALU instructions");
1109
Brian Paul5d7b49f2005-11-19 23:12:20 +00001110 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001111
Brian Paule998c342006-10-29 21:17:18 +00001112 /* Allocate final instruction array */
Brian Paule998c342006-10-29 21:17:18 +00001113 program->Base.Instructions
1114 = _mesa_alloc_instructions(program->Base.NumInstructions);
1115 if (!program->Base.Instructions) {
1116 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1117 "generating tex env program");
1118 return;
1119 }
Brian1240eb22007-03-22 08:50:20 -06001120 _mesa_copy_instructions(program->Base.Instructions, instBuffer,
1121 program->Base.NumInstructions);
Brian Paule998c342006-10-29 21:17:18 +00001122
Keith Whitwelldbeea252005-05-10 13:57:50 +00001123 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001124 */
Brian Paule998c342006-10-29 21:17:18 +00001125 if (ctx->Driver.ProgramStringNotify) {
1126 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1127 &p.program->Base );
1128 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001129
Brian Paule998c342006-10-29 21:17:18 +00001130 if (DISASSEM) {
1131 _mesa_print_program(&p.program->Base);
1132 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001133 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001134}
1135
Brian Paul5d7b49f2005-11-19 23:12:20 +00001136
Brian Paul122629f2006-07-20 16:49:57 +00001137static struct gl_fragment_program *
Brian Pauld9736db2006-05-23 02:44:46 +00001138search_cache(const struct texenvprog_cache *cache,
1139 GLuint hash,
Brian Paule4cb9cd2006-05-30 22:15:24 +00001140 const void *key,
Brian Pauld9736db2006-05-23 02:44:46 +00001141 GLuint keysize)
Keith Whitwellce721142005-07-11 10:10:38 +00001142{
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001143 struct texenvprog_cache_item *c;
Keith Whitwellce721142005-07-11 10:10:38 +00001144
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001145 for (c = cache->items[hash % cache->size]; c; c = c->next) {
1146 if (c->hash == hash && memcmp(c->key, key, keysize) == 0)
Brian Paul5b5c9312008-05-07 18:51:44 -06001147 return c->data;
Keith Whitwellce721142005-07-11 10:10:38 +00001148 }
1149
1150 return NULL;
1151}
1152
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001153static void rehash( struct texenvprog_cache *cache )
1154{
1155 struct texenvprog_cache_item **items;
1156 struct texenvprog_cache_item *c, *next;
1157 GLuint size, i;
1158
1159 size = cache->size * 3;
1160 items = (struct texenvprog_cache_item**) _mesa_malloc(size * sizeof(*items));
1161 _mesa_memset(items, 0, size * sizeof(*items));
1162
1163 for (i = 0; i < cache->size; i++)
1164 for (c = cache->items[i]; c; c = next) {
1165 next = c->next;
1166 c->next = items[c->hash % size];
1167 items[c->hash % size] = c;
1168 }
1169
1170 _mesa_free(cache->items);
1171 cache->items = items;
1172 cache->size = size;
1173}
1174
Brian Paul5b5c9312008-05-07 18:51:44 -06001175static void clear_cache(GLcontext *ctx, struct texenvprog_cache *cache)
Keith Whitwell8065c122006-05-22 16:09:27 +00001176{
1177 struct texenvprog_cache_item *c, *next;
1178 GLuint i;
1179
1180 for (i = 0; i < cache->size; i++) {
1181 for (c = cache->items[i]; c; c = next) {
1182 next = c->next;
1183 _mesa_free(c->key);
Brian Paul5b5c9312008-05-07 18:51:44 -06001184 _mesa_reference_fragprog(ctx, &c->data, NULL);
Keith Whitwell8065c122006-05-22 16:09:27 +00001185 _mesa_free(c);
1186 }
1187 cache->items[i] = NULL;
1188 }
1189
1190
1191 cache->n_items = 0;
1192}
1193
1194
Brian Paul5b5c9312008-05-07 18:51:44 -06001195static void cache_item( GLcontext *ctx,
1196 struct texenvprog_cache *cache,
Keith Whitwellce721142005-07-11 10:10:38 +00001197 GLuint hash,
Brian Paulb8f2f6f2006-05-23 01:55:31 +00001198 const struct state_key *key,
Brian Paul5b5c9312008-05-07 18:51:44 -06001199 struct gl_fragment_program *prog)
Keith Whitwellce721142005-07-11 10:10:38 +00001200{
Brian Paul5b5c9312008-05-07 18:51:44 -06001201 struct texenvprog_cache_item *c = CALLOC_STRUCT(texenvprog_cache_item);
Keith Whitwellce721142005-07-11 10:10:38 +00001202 c->hash = hash;
Keith Whitwell8065c122006-05-22 16:09:27 +00001203
1204 c->key = _mesa_malloc(sizeof(*key));
1205 memcpy(c->key, key, sizeof(*key));
1206
Brian Paul5b5c9312008-05-07 18:51:44 -06001207 _mesa_reference_fragprog(ctx, &c->data, prog);
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001208
Keith Whitwell8065c122006-05-22 16:09:27 +00001209 if (cache->n_items > cache->size * 1.5) {
1210 if (cache->size < 1000)
1211 rehash(cache);
1212 else
Brian Paul5b5c9312008-05-07 18:51:44 -06001213 clear_cache(ctx, cache);
Keith Whitwell8065c122006-05-22 16:09:27 +00001214 }
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001215
Keith Whitwell8065c122006-05-22 16:09:27 +00001216 cache->n_items++;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001217 c->next = cache->items[hash % cache->size];
1218 cache->items[hash % cache->size] = c;
Keith Whitwellce721142005-07-11 10:10:38 +00001219}
1220
Brian Pauld9736db2006-05-23 02:44:46 +00001221static GLuint hash_key( const struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +00001222{
1223 GLuint *ikey = (GLuint *)key;
1224 GLuint hash = 0, i;
1225
Keith Whitwell8065c122006-05-22 16:09:27 +00001226 /* Make a slightly better attempt at a hash function:
Keith Whitwellce721142005-07-11 10:10:38 +00001227 */
Keith Whitwell8065c122006-05-22 16:09:27 +00001228 for (i = 0; i < sizeof(*key)/sizeof(*ikey); i++)
1229 {
1230 hash += ikey[i];
1231 hash += (hash << 10);
1232 hash ^= (hash >> 6);
1233 }
Keith Whitwellce721142005-07-11 10:10:38 +00001234
1235 return hash;
1236}
1237
Briana90046f2006-12-15 10:07:26 -07001238
1239/**
1240 * If _MaintainTexEnvProgram is set we'll generate a fragment program that
1241 * implements the current texture env/combine mode.
1242 * This function generates that program and puts it into effect.
1243 */
1244void
1245_mesa_UpdateTexEnvProgram( GLcontext *ctx )
Keith Whitwellce721142005-07-11 10:10:38 +00001246{
Keith Whitwell8065c122006-05-22 16:09:27 +00001247 struct state_key key;
Keith Whitwellce721142005-07-11 10:10:38 +00001248 GLuint hash;
Brian Paul122629f2006-07-20 16:49:57 +00001249 const struct gl_fragment_program *prev = ctx->FragmentProgram._Current;
Keith Whitwellce721142005-07-11 10:10:38 +00001250
Briana90046f2006-12-15 10:07:26 -07001251 ASSERT(ctx->FragmentProgram._MaintainTexEnvProgram);
1252
1253 /* If a conventional fragment program/shader isn't in effect... */
Brianefcfdbd2007-02-24 15:51:41 -07001254 if (!ctx->FragmentProgram._Enabled &&
Zou Nan haiac985702007-10-08 15:34:03 +08001255 (!ctx->Shader.CurrentProgram || !ctx->Shader.CurrentProgram->FragmentProgram)) {
Brian Paul5b5c9312008-05-07 18:51:44 -06001256 struct gl_fragment_program *newProg;
1257
Keith Whitwell8065c122006-05-22 16:09:27 +00001258 make_state_key(ctx, &key);
1259 hash = hash_key(&key);
Keith Whitwellc3626a92005-11-01 17:25:49 +00001260
Brian Paul5b5c9312008-05-07 18:51:44 -06001261 newProg = search_cache(&ctx->Texture.env_fp_cache, hash, &key, sizeof(key));
Keith Whitwellce721142005-07-11 10:10:38 +00001262
Brian Paul5b5c9312008-05-07 18:51:44 -06001263 if (!newProg) {
1264 /* create new tex env program */
1265
Briana90046f2006-12-15 10:07:26 -07001266 if (0)
1267 _mesa_printf("Building new texenv proggy for key %x\n", hash);
1268
Brian Paul5b5c9312008-05-07 18:51:44 -06001269 newProg = (struct gl_fragment_program *)
Briana90046f2006-12-15 10:07:26 -07001270 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1271
Brian Paul5b5c9312008-05-07 18:51:44 -06001272 create_new_program(ctx, &key, newProg);
Briana90046f2006-12-15 10:07:26 -07001273
Brian Paul5b5c9312008-05-07 18:51:44 -06001274 cache_item(ctx, &ctx->Texture.env_fp_cache, hash, &key, newProg);
Keith Whitwellc3626a92005-11-01 17:25:49 +00001275 }
Brian Paul5b5c9312008-05-07 18:51:44 -06001276
1277 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current, newProg);
1278 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._TexEnvProgram, newProg);
Keith Whitwellc3626a92005-11-01 17:25:49 +00001279 }
1280 else {
Xiang, Haihaode1e9882008-02-29 11:15:02 +08001281 /* _Current pointer has been updated in update_program */
Brianeecb3ab2008-03-09 10:41:50 -06001282 /* ctx->FragmentProgram._Current = ctx->FragmentProgram.Current; */
Keith Whitwellce721142005-07-11 10:10:38 +00001283 }
Keith Whitwellc3626a92005-11-01 17:25:49 +00001284
1285 /* Tell the driver about the change. Could define a new target for
1286 * this?
1287 */
Brian Paul5d7b49f2005-11-19 23:12:20 +00001288 if (ctx->FragmentProgram._Current != prev && ctx->Driver.BindProgram) {
1289 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
Briana90046f2006-12-15 10:07:26 -07001290 (struct gl_program *) ctx->FragmentProgram._Current);
Brian Paul5d7b49f2005-11-19 23:12:20 +00001291 }
Keith Whitwellce721142005-07-11 10:10:38 +00001292}
1293
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001294
1295void _mesa_TexEnvProgramCacheInit( GLcontext *ctx )
1296{
Keith Whitwell8065c122006-05-22 16:09:27 +00001297 ctx->Texture.env_fp_cache.ctx = ctx;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001298 ctx->Texture.env_fp_cache.size = 17;
1299 ctx->Texture.env_fp_cache.n_items = 0;
1300 ctx->Texture.env_fp_cache.items = (struct texenvprog_cache_item **)
1301 _mesa_calloc(ctx->Texture.env_fp_cache.size *
1302 sizeof(struct texenvprog_cache_item));
1303}
1304
1305
Keith Whitwellce721142005-07-11 10:10:38 +00001306void _mesa_TexEnvProgramCacheDestroy( GLcontext *ctx )
1307{
Brian Paul5b5c9312008-05-07 18:51:44 -06001308 clear_cache(ctx, &ctx->Texture.env_fp_cache);
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001309 _mesa_free(ctx->Texture.env_fp_cache.items);
Keith Whitwellce721142005-07-11 10:10:38 +00001310}