blob: 5329719cbbb57a02432bb0988c2dfdaf08eff679 [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"
31#include "texenvprogram.h"
32
33#include "shader/program.h"
Brian Paul7e807512005-11-05 17:10:45 +000034#include "shader/program_instruction.h"
Keith Whitwell15e75e02005-04-29 15:11:39 +000035
Brian Paule998c342006-10-29 21:17:18 +000036/**
37 * According to Glean's texCombine test, no more than 21 instructions
38 * are needed. Allow a few extra just in case.
39 */
40#define MAX_INSTRUCTIONS 24
Keith Whitwell15e75e02005-04-29 15:11:39 +000041
Keith Whitwell269e3892005-05-12 10:28:43 +000042#define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
Keith Whitwell15e75e02005-04-29 15:11:39 +000043
Keith Whitwellce721142005-07-11 10:10:38 +000044struct mode_opt {
Brian Paul5d7b49f2005-11-19 23:12:20 +000045 GLuint Source:4;
46 GLuint Operand:3;
Keith Whitwellce721142005-07-11 10:10:38 +000047};
48
49struct state_key {
Brian Paul5d7b49f2005-11-19 23:12:20 +000050 GLbitfield enabled_units;
51 GLuint separate_specular:1;
52 GLuint fog_enabled:1;
53 GLuint fog_mode:2;
Keith Whitwellce721142005-07-11 10:10:38 +000054
55 struct {
Brian Paul5d7b49f2005-11-19 23:12:20 +000056 GLuint enabled:1;
57 GLuint source_index:3; /* one of TEXTURE_1D/2D/3D/CUBE/RECT_INDEX */
58 GLuint ScaleShiftRGB:2;
59 GLuint ScaleShiftA:2;
Keith Whitwellce721142005-07-11 10:10:38 +000060
Brian Paul5d7b49f2005-11-19 23:12:20 +000061 GLuint NumArgsRGB:2;
62 GLuint ModeRGB:4;
Keith Whitwellce721142005-07-11 10:10:38 +000063 struct mode_opt OptRGB[3];
64
Brian Paul5d7b49f2005-11-19 23:12:20 +000065 GLuint NumArgsA:2;
66 GLuint ModeA:4;
Keith Whitwellce721142005-07-11 10:10:38 +000067 struct mode_opt OptA[3];
68 } unit[8];
69};
70
71#define FOG_LINEAR 0
72#define FOG_EXP 1
73#define FOG_EXP2 2
74#define FOG_UNKNOWN 3
75
76static GLuint translate_fog_mode( GLenum mode )
77{
78 switch (mode) {
79 case GL_LINEAR: return FOG_LINEAR;
80 case GL_EXP: return FOG_EXP;
81 case GL_EXP2: return FOG_EXP2;
82 default: return FOG_UNKNOWN;
83 }
84}
85
86#define OPR_SRC_COLOR 0
87#define OPR_ONE_MINUS_SRC_COLOR 1
88#define OPR_SRC_ALPHA 2
89#define OPR_ONE_MINUS_SRC_ALPHA 3
90#define OPR_ZERO 4
91#define OPR_ONE 5
92#define OPR_UNKNOWN 7
93
94static GLuint translate_operand( GLenum operand )
95{
96 switch (operand) {
97 case GL_SRC_COLOR: return OPR_SRC_COLOR;
98 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
99 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
100 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
101 case GL_ZERO: return OPR_ZERO;
102 case GL_ONE: return OPR_ONE;
103 default: return OPR_UNKNOWN;
104 }
105}
106
107#define SRC_TEXTURE 0
108#define SRC_TEXTURE0 1
109#define SRC_TEXTURE1 2
110#define SRC_TEXTURE2 3
111#define SRC_TEXTURE3 4
112#define SRC_TEXTURE4 5
113#define SRC_TEXTURE5 6
114#define SRC_TEXTURE6 7
115#define SRC_TEXTURE7 8
116#define SRC_CONSTANT 9
117#define SRC_PRIMARY_COLOR 10
118#define SRC_PREVIOUS 11
119#define SRC_UNKNOWN 15
120
121static GLuint translate_source( GLenum src )
122{
123 switch (src) {
124 case GL_TEXTURE: return SRC_TEXTURE;
125 case GL_TEXTURE0:
126 case GL_TEXTURE1:
127 case GL_TEXTURE2:
128 case GL_TEXTURE3:
129 case GL_TEXTURE4:
130 case GL_TEXTURE5:
131 case GL_TEXTURE6:
132 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
133 case GL_CONSTANT: return SRC_CONSTANT;
134 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
135 case GL_PREVIOUS: return SRC_PREVIOUS;
136 default: return SRC_UNKNOWN;
137 }
138}
139
140#define MODE_REPLACE 0
141#define MODE_MODULATE 1
142#define MODE_ADD 2
143#define MODE_ADD_SIGNED 3
144#define MODE_INTERPOLATE 4
145#define MODE_SUBTRACT 5
146#define MODE_DOT3_RGB 6
147#define MODE_DOT3_RGB_EXT 7
148#define MODE_DOT3_RGBA 8
149#define MODE_DOT3_RGBA_EXT 9
150#define MODE_MODULATE_ADD_ATI 10
151#define MODE_MODULATE_SIGNED_ADD_ATI 11
152#define MODE_MODULATE_SUBTRACT_ATI 12
153#define MODE_UNKNOWN 15
154
155static GLuint translate_mode( GLenum mode )
156{
157 switch (mode) {
158 case GL_REPLACE: return MODE_REPLACE;
159 case GL_MODULATE: return MODE_MODULATE;
160 case GL_ADD: return MODE_ADD;
161 case GL_ADD_SIGNED: return MODE_ADD_SIGNED;
162 case GL_INTERPOLATE: return MODE_INTERPOLATE;
163 case GL_SUBTRACT: return MODE_SUBTRACT;
164 case GL_DOT3_RGB: return MODE_DOT3_RGB;
165 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
166 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
167 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
168 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
169 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
170 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
171 default: return MODE_UNKNOWN;
172 }
173}
174
175#define TEXTURE_UNKNOWN_INDEX 7
Brian Paule00ac112005-09-15 05:00:45 +0000176static GLuint translate_tex_src_bit( GLbitfield bit )
Keith Whitwellce721142005-07-11 10:10:38 +0000177{
178 switch (bit) {
179 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
180 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
181 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
182 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
183 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
184 default: return TEXTURE_UNKNOWN_INDEX;
185 }
186}
187
Brian Paul22db5352005-11-19 23:45:10 +0000188/**
189 * Examine current texture environment state and generate a unique
190 * key to identify it.
191 */
Keith Whitwell8065c122006-05-22 16:09:27 +0000192static void make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000193{
Keith Whitwellce721142005-07-11 10:10:38 +0000194 GLuint i, j;
195
Keith Whitwell8065c122006-05-22 16:09:27 +0000196 memset(key, 0, sizeof(*key));
197
Keith Whitwellce721142005-07-11 10:10:38 +0000198 for (i=0;i<MAX_TEXTURE_UNITS;i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000199 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Keith Whitwellce721142005-07-11 10:10:38 +0000200
201 if (!texUnit->_ReallyEnabled)
202 continue;
203
204 key->unit[i].enabled = 1;
205 key->enabled_units |= (1<<i);
206
207 key->unit[i].source_index =
208 translate_tex_src_bit(texUnit->_ReallyEnabled);
209
210 key->unit[i].NumArgsRGB = texUnit->_CurrentCombine->_NumArgsRGB;
211 key->unit[i].NumArgsA = texUnit->_CurrentCombine->_NumArgsA;
212
213 key->unit[i].ModeRGB =
214 translate_mode(texUnit->_CurrentCombine->ModeRGB);
215 key->unit[i].ModeA =
216 translate_mode(texUnit->_CurrentCombine->ModeA);
217
218 key->unit[i].ScaleShiftRGB = texUnit->_CurrentCombine->ScaleShiftRGB;
Keith Whitwell64da1612006-05-22 14:30:58 +0000219 key->unit[i].ScaleShiftA = texUnit->_CurrentCombine->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000220
221 for (j=0;j<3;j++) {
222 key->unit[i].OptRGB[j].Operand =
223 translate_operand(texUnit->_CurrentCombine->OperandRGB[j]);
224 key->unit[i].OptA[j].Operand =
225 translate_operand(texUnit->_CurrentCombine->OperandA[j]);
226 key->unit[i].OptRGB[j].Source =
227 translate_source(texUnit->_CurrentCombine->SourceRGB[j]);
228 key->unit[i].OptA[j].Source =
229 translate_source(texUnit->_CurrentCombine->SourceA[j]);
230 }
231 }
232
233 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
234 key->separate_specular = 1;
235
236 if (ctx->Fog.Enabled) {
237 key->fog_enabled = 1;
238 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
239 }
Keith Whitwellce721142005-07-11 10:10:38 +0000240}
241
Keith Whitwell15e75e02005-04-29 15:11:39 +0000242/* Use uregs to represent registers internally, translate to Mesa's
243 * expected formats on emit.
244 *
245 * NOTE: These are passed by value extensively in this file rather
246 * than as usual by pointer reference. If this disturbs you, try
247 * remembering they are just 32bits in size.
248 *
249 * GCC is smart enough to deal with these dword-sized structures in
250 * much the same way as if I had defined them as dwords and was using
251 * macros to access and set the fields. This is much nicer and easier
252 * to evolve.
253 */
254struct ureg {
255 GLuint file:4;
256 GLuint idx:8;
257 GLuint negatebase:1;
258 GLuint abs:1;
259 GLuint negateabs:1;
260 GLuint swz:12;
261 GLuint pad:5;
262};
263
Brian Paul13abf912006-04-13 19:17:13 +0000264static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000265 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000266 ~0,
267 0,
268 0,
269 0,
270 0,
271 0
272};
273
Keith Whitwell15e75e02005-04-29 15:11:39 +0000274
Keith Whitwell15e75e02005-04-29 15:11:39 +0000275/* State used to build the fragment program:
276 */
277struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000278 struct gl_fragment_program *program;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000279 GLcontext *ctx;
Keith Whitwellce721142005-07-11 10:10:38 +0000280 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000281
Brian Paulc8d17412005-12-14 03:06:16 +0000282 GLbitfield alu_temps; /* Track texture indirections, see spec. */
283 GLbitfield temps_output; /* Track texture indirections, see spec. */
284 GLbitfield temp_in_use; /* Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000285 GLboolean error;
286
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000287 struct ureg src_texture[MAX_TEXTURE_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000288 /* Reg containing each texture unit's sampled texture color,
289 * else undef.
290 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000291
292 struct ureg src_previous; /* Reg containing color from previous
293 * stage. May need to be decl'd.
294 */
295
296 GLuint last_tex_stage; /* Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000297
298 struct ureg half;
299 struct ureg one;
300 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000301};
302
303
304
305static struct ureg make_ureg(GLuint file, GLuint idx)
306{
307 struct ureg reg;
308 reg.file = file;
309 reg.idx = idx;
310 reg.negatebase = 0;
311 reg.abs = 0;
312 reg.negateabs = 0;
313 reg.swz = SWIZZLE_NOOP;
314 reg.pad = 0;
315 return reg;
316}
317
318static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
319{
320 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
321 GET_SWZ(reg.swz, y),
322 GET_SWZ(reg.swz, z),
323 GET_SWZ(reg.swz, w));
324
325 return reg;
326}
327
328static struct ureg swizzle1( struct ureg reg, int x )
329{
330 return swizzle(reg, x, x, x, x);
331}
332
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000333static struct ureg negate( struct ureg reg )
334{
335 reg.negatebase ^= 1;
336 return reg;
337}
338
Keith Whitwell15e75e02005-04-29 15:11:39 +0000339static GLboolean is_undef( struct ureg reg )
340{
Alan Hourihane8d972652006-08-10 13:12:00 +0000341 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000342}
343
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000344
Keith Whitwell15e75e02005-04-29 15:11:39 +0000345static struct ureg get_temp( struct texenv_fragment_program *p )
346{
Brian Paul13abf912006-04-13 19:17:13 +0000347 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000348
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000349 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000350 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000351 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000352
353 /* Then any unused temporary:
354 */
355 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000356 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000357
Keith Whitwell15e75e02005-04-29 15:11:39 +0000358 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000359 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000360 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000361 }
362
Brian Paul13abf912006-04-13 19:17:13 +0000363 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000364 p->program->Base.NumTemporaries = bit;
365
Keith Whitwell93cd9232005-05-11 08:30:23 +0000366 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000367 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
368}
369
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000370static struct ureg get_tex_temp( struct texenv_fragment_program *p )
371{
372 int bit;
373
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000374 /* First try to find availble temp not previously used (to avoid
375 * starting a new texture indirection). According to the spec, the
376 * ~p->temps_output isn't necessary, but will keep it there for
377 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000378 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000379 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000380
381 /* Then any unused temporary:
382 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000383 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000384 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000385
386 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000387 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000388 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000389 }
390
Brian Paul13abf912006-04-13 19:17:13 +0000391 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000392 p->program->Base.NumTemporaries = bit;
393
Keith Whitwell93cd9232005-05-11 08:30:23 +0000394 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000395 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
396}
397
Keith Whitwell15e75e02005-04-29 15:11:39 +0000398
399static void release_temps( struct texenv_fragment_program *p )
400{
Brian Paul05051032005-11-01 04:36:33 +0000401 GLuint max_temp = p->ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000402
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000403 /* KW: To support tex_env_crossbar, don't release the registers in
404 * temps_output.
405 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000406 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000407 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000408 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000409 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000410}
411
412
Keith Whitwell47b29f52005-05-04 11:44:44 +0000413static struct ureg register_param6( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000414 GLint s0,
415 GLint s1,
416 GLint s2,
417 GLint s3,
418 GLint s4,
419 GLint s5)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000420{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000421 GLint tokens[6];
422 GLuint idx;
423 tokens[0] = s0;
424 tokens[1] = s1;
425 tokens[2] = s2;
426 tokens[3] = s3;
427 tokens[4] = s4;
428 tokens[5] = s5;
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
434#define register_param1(p,s0) register_param6(p,s0,0,0,0,0,0)
435#define register_param2(p,s0,s1) register_param6(p,s0,s1,0,0,0,0)
436#define register_param3(p,s0,s1,s2) register_param6(p,s0,s1,s2,0,0,0)
437#define register_param4(p,s0,s1,s2,s3) register_param6(p,s0,s1,s2,s3,0,0)
438
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;
464 dst->CondMask = 0;
465 dst->CondSwizzle = 0;
466}
467
Brian Paul7e807512005-11-05 17:10:45 +0000468static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000469emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000470 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000471 struct ureg dest,
472 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000473 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000474 struct ureg src0,
475 struct ureg src1,
476 struct ureg src2 )
477{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000478 GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000479 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000480
Brian Pauld6272e02006-10-29 18:03:16 +0000481 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000482 inst->Opcode = op;
483
Keith Whitwell47b29f52005-05-04 11:44:44 +0000484 emit_arg( &inst->SrcReg[0], src0 );
485 emit_arg( &inst->SrcReg[1], src1 );
486 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000487
Brian Paule31ac052005-11-20 17:52:40 +0000488 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000489
490 emit_dst( &inst->DstReg, dest, mask );
491
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000492 /* Accounting for indirection tracking:
493 */
494 if (dest.file == PROGRAM_TEMPORARY)
495 p->temps_output |= 1 << dest.idx;
496
Keith Whitwell15e75e02005-04-29 15:11:39 +0000497 return inst;
498}
499
500
501static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000502 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000503 struct ureg dest,
504 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000505 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000506 struct ureg src0,
507 struct ureg src1,
508 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000509{
510 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
511
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000512 /* Accounting for indirection tracking:
513 */
514 if (src0.file == PROGRAM_TEMPORARY)
515 p->alu_temps |= 1 << src0.idx;
516
517 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
518 p->alu_temps |= 1 << src1.idx;
519
520 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
521 p->alu_temps |= 1 << src2.idx;
522
523 if (dest.file == PROGRAM_TEMPORARY)
524 p->alu_temps |= 1 << dest.idx;
525
Keith Whitwell47b29f52005-05-04 11:44:44 +0000526 p->program->NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000527 return dest;
528}
529
530static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000531 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000532 struct ureg dest,
533 GLuint destmask,
534 GLuint tex_unit,
535 GLuint tex_idx,
536 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000537{
Brian Paul7e807512005-11-05 17:10:45 +0000538 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000539 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000540 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000541 coord, /* arg 0? */
542 undef,
543 undef);
544
Brian Paul7e807512005-11-05 17:10:45 +0000545 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000546 inst->TexSrcUnit = tex_unit;
547
Keith Whitwell47b29f52005-05-04 11:44:44 +0000548 p->program->NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000549
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000550 /* Is this a texture indirection?
551 */
552 if ((coord.file == PROGRAM_TEMPORARY &&
553 (p->temps_output & (1<<coord.idx))) ||
554 (dest.file == PROGRAM_TEMPORARY &&
555 (p->alu_temps & (1<<dest.idx)))) {
Keith Whitwell47b29f52005-05-04 11:44:44 +0000556 p->program->NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000557 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000558 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000559 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000560 }
561
562 return dest;
563}
564
565
Keith Whitwell47b29f52005-05-04 11:44:44 +0000566static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000567 GLfloat s0,
568 GLfloat s1,
569 GLfloat s2,
570 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000571{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000572 GLfloat values[4];
573 GLuint idx;
574 values[0] = s0;
575 values[1] = s1;
576 values[2] = s2;
577 values[3] = s3;
Brian Paul0c6723a2006-11-15 23:38:02 +0000578 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4 );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000579 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000580}
581
Keith Whitwelle4902422005-05-11 15:16:35 +0000582#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000583#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
584#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
585#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000586
587
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000588static struct ureg get_one( struct texenv_fragment_program *p )
589{
590 if (is_undef(p->one))
591 p->one = register_scalar_const(p, 1.0);
592 return p->one;
593}
594
595static struct ureg get_half( struct texenv_fragment_program *p )
596{
597 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000598 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000599 return p->half;
600}
601
602static struct ureg get_zero( struct texenv_fragment_program *p )
603{
604 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000605 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000606 return p->zero;
607}
608
Keith Whitwell15e75e02005-04-29 15:11:39 +0000609
Keith Whitwell15e75e02005-04-29 15:11:39 +0000610static void program_error( struct texenv_fragment_program *p, const char *msg )
611{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000612 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000613 p->error = 1;
614}
615
Keith Whitwell15e75e02005-04-29 15:11:39 +0000616static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000617 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000618{
619 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000620 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000621 assert(!is_undef(p->src_texture[unit]));
622 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000623
Keith Whitwellce721142005-07-11 10:10:38 +0000624 case SRC_TEXTURE0:
625 case SRC_TEXTURE1:
626 case SRC_TEXTURE2:
627 case SRC_TEXTURE3:
628 case SRC_TEXTURE4:
629 case SRC_TEXTURE5:
630 case SRC_TEXTURE6:
631 case SRC_TEXTURE7:
632 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
633 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000634
Keith Whitwellce721142005-07-11 10:10:38 +0000635 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000636 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000637
Keith Whitwellce721142005-07-11 10:10:38 +0000638 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000639 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000640
Keith Whitwellce721142005-07-11 10:10:38 +0000641 case SRC_PREVIOUS:
642 default:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000643 if (is_undef(p->src_previous))
644 return register_input(p, FRAG_ATTRIB_COL0);
645 else
646 return p->src_previous;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000647 }
648}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000649
Keith Whitwell15e75e02005-04-29 15:11:39 +0000650static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000651 GLuint mask,
652 GLuint unit,
653 GLuint source,
654 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000655{
656 struct ureg arg, src, one;
657
658 src = get_source(p, source, unit);
659
660 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000661 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000662 /* Get unused tmp,
663 * Emit tmp = 1.0 - arg.xyzw
664 */
665 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000666 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000667 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000668
Keith Whitwellce721142005-07-11 10:10:38 +0000669 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000670 if (mask == WRITEMASK_W)
671 return src;
672 else
Brian Paul22db5352005-11-19 23:45:10 +0000673 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000674 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000675 /* Get unused tmp,
676 * Emit tmp = 1.0 - arg.wwww
677 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000678 arg = get_temp(p);
679 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000680 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000681 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000682 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000683 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000684 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000685 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000686 case OPR_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000687 default:
688 return src;
689 }
690}
691
Keith Whitwellce721142005-07-11 10:10:38 +0000692static GLboolean args_match( struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000693{
Brian Paul22db5352005-11-19 23:45:10 +0000694 GLuint i, nr = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000695
696 for (i = 0 ; i < nr ; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000697 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000698 return GL_FALSE;
699
Keith Whitwellce721142005-07-11 10:10:38 +0000700 switch(key->unit[unit].OptA[i].Operand) {
701 case OPR_SRC_ALPHA:
702 switch(key->unit[unit].OptRGB[i].Operand) {
703 case OPR_SRC_COLOR:
704 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000705 break;
706 default:
707 return GL_FALSE;
708 }
709 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000710 case OPR_ONE_MINUS_SRC_ALPHA:
711 switch(key->unit[unit].OptRGB[i].Operand) {
712 case OPR_ONE_MINUS_SRC_COLOR:
713 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000714 break;
715 default:
716 return GL_FALSE;
717 }
718 break;
719 default:
720 return GL_FALSE; /* impossible */
721 }
722 }
723
724 return GL_TRUE;
725}
726
Keith Whitwell15e75e02005-04-29 15:11:39 +0000727static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000728 struct ureg dest,
729 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000730 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000731 GLuint unit,
732 GLuint nr,
733 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +0000734 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000735{
Keith Whitwell15e75e02005-04-29 15:11:39 +0000736 struct ureg src[3];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000737 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +0000738 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000739
Brian Paul00630842005-12-12 15:27:55 +0000740 tmp = undef; /* silence warning (bug 5318) */
741
Keith Whitwell15e75e02005-04-29 15:11:39 +0000742 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +0000743 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000744
745 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +0000746 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000747 if (mask == WRITEMASK_XYZW && !saturate)
748 return src[0];
749 else
Brian Paul7e807512005-11-05 17:10:45 +0000750 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000751 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +0000752 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000753 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000754 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +0000755 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000756 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000757 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000758 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000759 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +0000760 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000761 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +0000762 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +0000763 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
764 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000765 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +0000766 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000767 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
768 */
Brian Paul7e807512005-11-05 17:10:45 +0000769 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000770
Keith Whitwellce721142005-07-11 10:10:38 +0000771 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +0000772 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000773
Keith Whitwellce721142005-07-11 10:10:38 +0000774 case MODE_DOT3_RGBA:
775 case MODE_DOT3_RGBA_EXT:
776 case MODE_DOT3_RGB_EXT:
777 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000778 struct ureg tmp0 = get_temp( p );
779 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +0000780 struct ureg neg1 = register_scalar_const(p, -1);
781 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000782
783 /* tmp0 = 2*src0 - 1
784 * tmp1 = 2*src1 - 1
785 *
786 * dst = tmp0 dot3 tmp1
787 */
Brian Paul7e807512005-11-05 17:10:45 +0000788 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000789 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000790
Brian Paulaa206952005-09-16 18:14:24 +0000791 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000792 tmp1 = tmp0;
793 else
Brian Paul7e807512005-11-05 17:10:45 +0000794 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000795 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +0000796 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000797 return dest;
798 }
Keith Whitwellce721142005-07-11 10:10:38 +0000799 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000800 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +0000801 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000802 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +0000803 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000804 /* Arg0 * Arg2 + Arg1 - 0.5 */
805 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000806 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +0000807 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
808 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000809 return dest;
810 }
Keith Whitwellce721142005-07-11 10:10:38 +0000811 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000812 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +0000813 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000814 return dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000815 default:
816 return src[0];
817 }
818}
819
Keith Whitwell15e75e02005-04-29 15:11:39 +0000820
Brian Paul22db5352005-11-19 23:45:10 +0000821/**
822 * Generate instructions for one texture unit's env/combiner mode.
823 */
824static struct ureg
825emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000826{
Keith Whitwellce721142005-07-11 10:10:38 +0000827 struct state_key *key = p->state;
Brian Paul22db5352005-11-19 23:45:10 +0000828 GLboolean saturate = (unit < p->last_tex_stage);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000829 GLuint rgb_shift, alpha_shift;
830 struct ureg out, shift;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000831 struct ureg dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000832
Keith Whitwellce721142005-07-11 10:10:38 +0000833 if (!key->unit[unit].enabled) {
834 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000835 }
Keith Whitwellce721142005-07-11 10:10:38 +0000836
837 switch (key->unit[unit].ModeRGB) {
838 case MODE_DOT3_RGB_EXT:
839 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000840 rgb_shift = 0;
841 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000842 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000843 alpha_shift = 0;
844 rgb_shift = 0;
845 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000846 default:
Keith Whitwellce721142005-07-11 10:10:38 +0000847 rgb_shift = key->unit[unit].ScaleShiftRGB;
848 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000849 break;
850 }
Keith Whitwellce721142005-07-11 10:10:38 +0000851
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000852 /* If this is the very last calculation, emit direct to output reg:
853 */
Keith Whitwellce721142005-07-11 10:10:38 +0000854 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000855 unit != p->last_tex_stage ||
856 alpha_shift ||
857 rgb_shift)
858 dest = get_temp( p );
859 else
Brian Paul90ebb582005-11-02 18:06:12 +0000860 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLR);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000861
862 /* Emit the RGB and A combine ops
863 */
Keith Whitwellce721142005-07-11 10:10:38 +0000864 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
865 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000866 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
867 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000868 key->unit[unit].NumArgsRGB,
869 key->unit[unit].ModeRGB,
870 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000871 }
Keith Whitwellce721142005-07-11 10:10:38 +0000872 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
873 key->unit[unit].ModeA == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000874
875 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
876 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000877 key->unit[unit].NumArgsRGB,
878 key->unit[unit].ModeRGB,
879 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000880 }
881 else {
882 /* Need to do something to stop from re-emitting identical
883 * argument calculations here:
884 */
885 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
886 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000887 key->unit[unit].NumArgsRGB,
888 key->unit[unit].ModeRGB,
889 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000890 out = emit_combine( p, dest, WRITEMASK_W, saturate,
891 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000892 key->unit[unit].NumArgsA,
893 key->unit[unit].ModeA,
894 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000895 }
896
897 /* Deal with the final shift:
898 */
899 if (alpha_shift || rgb_shift) {
900 if (rgb_shift == alpha_shift) {
Keith Whitwelle4902422005-05-11 15:16:35 +0000901 shift = register_scalar_const(p, 1<<rgb_shift);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000902 }
903 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000904 shift = register_const4f(p,
905 1<<rgb_shift,
906 1<<rgb_shift,
907 1<<rgb_shift,
908 1<<alpha_shift);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000909 }
Brian Paul7e807512005-11-05 17:10:45 +0000910 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000911 saturate, out, shift, undef );
912 }
913 else
914 return out;
915}
916
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000917
Brian Paul22db5352005-11-19 23:45:10 +0000918/**
919 * Generate instruction for getting a texture source term.
920 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000921static void load_texture( struct texenv_fragment_program *p, GLuint unit )
922{
923 if (is_undef(p->src_texture[unit])) {
Keith Whitwellce721142005-07-11 10:10:38 +0000924 GLuint dim = p->state->unit[unit].source_index;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000925 struct ureg texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
926 struct ureg tmp = get_tex_temp( p );
927
Brian Paulbfb5ea32005-07-19 18:20:04 +0000928 if (dim == TEXTURE_UNKNOWN_INDEX)
929 program_error(p, "TexSrcBit");
Keith Whitwellce721142005-07-11 10:10:38 +0000930
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000931 /* TODO: Use D0_MASK_XY where possible.
932 */
Aapo Tahkolab8915342006-03-28 10:26:34 +0000933 if (p->state->unit[unit].enabled)
934 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
935 tmp, WRITEMASK_XYZW,
936 unit, dim, texcoord );
937 else
938 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000939 }
940}
941
Keith Whitwell241b6b72005-05-23 09:50:34 +0000942static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000943 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000944{
945 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000946 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000947 load_texture(p, unit);
948 break;
949
Keith Whitwellce721142005-07-11 10:10:38 +0000950 case SRC_TEXTURE0:
951 case SRC_TEXTURE1:
952 case SRC_TEXTURE2:
953 case SRC_TEXTURE3:
954 case SRC_TEXTURE4:
955 case SRC_TEXTURE5:
956 case SRC_TEXTURE6:
957 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +0000958 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000959 break;
960
961 default:
962 break;
963 }
Keith Whitwell241b6b72005-05-23 09:50:34 +0000964
965 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000966}
967
Brian Paul22db5352005-11-19 23:45:10 +0000968
969/**
970 * Generate instructions for loading all texture source terms.
971 */
972static GLboolean
973load_texunit_sources( struct texenv_fragment_program *p, int unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000974{
Keith Whitwellce721142005-07-11 10:10:38 +0000975 struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +0000976 GLuint i;
977
978 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
979 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000980 }
Aapo Tahkolab8915342006-03-28 10:26:34 +0000981
982 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
983 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
984 }
985
Keith Whitwell241b6b72005-05-23 09:50:34 +0000986 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000987}
988
Brian Paul22db5352005-11-19 23:45:10 +0000989
990/**
991 * Generate a new fragment program which implements the context's
992 * current texture env/combine mode.
993 */
994static void
Brian Paule998c342006-10-29 21:17:18 +0000995create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +0000996 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000997{
Brian Paule998c342006-10-29 21:17:18 +0000998 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000999 struct texenv_fragment_program p;
1000 GLuint unit;
1001 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001002
Keith Whitwelle4902422005-05-11 15:16:35 +00001003 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +00001004 p.ctx = ctx;
Keith Whitwellce721142005-07-11 10:10:38 +00001005 p.state = key;
1006 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001007
Brian Paule998c342006-10-29 21:17:18 +00001008 /* During code generation, use locally-allocated instruction buffer,
1009 * then alloc dynamic storage below.
1010 */
1011 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001012 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001013 p.program->NumTexIndirections = 1; /* correct? */
1014 p.program->NumTexInstructions = 0;
1015 p.program->NumAluInstructions = 0;
Keith Whitwell9ca88152005-05-10 09:56:02 +00001016 p.program->Base.String = 0;
1017 p.program->Base.NumInstructions =
Brian Paule998c342006-10-29 21:17:18 +00001018 p.program->Base.NumTemporaries =
1019 p.program->Base.NumParameters =
1020 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001021 p.program->Base.Parameters = _mesa_new_parameter_list();
Keith Whitwelldbeea252005-05-10 13:57:50 +00001022
Brian Paulde997602005-11-12 17:53:14 +00001023 p.program->Base.InputsRead = 0;
1024 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001025
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001026 for (unit = 0; unit < MAX_TEXTURE_UNITS; unit++)
1027 p.src_texture[unit] = undef;
1028
Keith Whitwell47b29f52005-05-04 11:44:44 +00001029 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001030 p.half = undef;
1031 p.zero = undef;
1032 p.one = undef;
1033
Keith Whitwell15e75e02005-04-29 15:11:39 +00001034 p.last_tex_stage = 0;
Keith Whitwell93cd9232005-05-11 08:30:23 +00001035 release_temps(&p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001036
Keith Whitwellce721142005-07-11 10:10:38 +00001037 if (key->enabled_units) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001038 /* First pass - to support texture_env_crossbar, first identify
1039 * all referenced texture sources and emit texld instructions
1040 * for each:
1041 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001042 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001043 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001044 load_texunit_sources( &p, unit );
1045 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001046 }
1047
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001048 /* Second pass - emit combine instructions to build final color:
1049 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001050 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001051 if (key->enabled_units & (1<<unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001052 p.src_previous = emit_texenv( &p, unit );
Keith Whitwell93cd9232005-05-11 08:30:23 +00001053 release_temps(&p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001054 }
1055 }
1056
Keith Whitwellce721142005-07-11 10:10:38 +00001057 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul90ebb582005-11-02 18:06:12 +00001058 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001059
Keith Whitwellce721142005-07-11 10:10:38 +00001060 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001061 /* Emit specular add.
1062 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001063 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001064 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1065 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001066 }
Brian Paulaa206952005-09-16 18:14:24 +00001067 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001068 /* Will wind up in here if no texture enabled or a couple of
1069 * other scenarios (GL_REPLACE for instance).
1070 */
Brian Paul7e807512005-11-05 17:10:45 +00001071 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001072 }
1073
Keith Whitwell47b29f52005-05-04 11:44:44 +00001074 /* Finish up:
1075 */
Brian Paul7e807512005-11-05 17:10:45 +00001076 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001077
Keith Whitwellce721142005-07-11 10:10:38 +00001078 if (key->fog_enabled) {
1079 /* Pull fog mode from GLcontext, the value in the state key is
1080 * a reduced value and not what is expected in FogOption
1081 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001082 p.program->FogOption = ctx->Fog.Mode;
Keith Whitwellce721142005-07-11 10:10:38 +00001083 } else
Keith Whitwell276330b2005-05-09 17:58:13 +00001084 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001085
Brian Paul05051032005-11-01 04:36:33 +00001086 if (p.program->NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001087 program_error(&p, "Exceeded max nr indirect texture lookups");
1088
Brian Paul05051032005-11-01 04:36:33 +00001089 if (p.program->NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001090 program_error(&p, "Exceeded max TEX instructions");
1091
Brian Paul05051032005-11-01 04:36:33 +00001092 if (p.program->NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001093 program_error(&p, "Exceeded max ALU instructions");
1094
Brian Paul5d7b49f2005-11-19 23:12:20 +00001095 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001096
Brian Paule998c342006-10-29 21:17:18 +00001097 /* Allocate final instruction array */
Brian Paule998c342006-10-29 21:17:18 +00001098 program->Base.Instructions
1099 = _mesa_alloc_instructions(program->Base.NumInstructions);
1100 if (!program->Base.Instructions) {
1101 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1102 "generating tex env program");
1103 return;
1104 }
1105 _mesa_memcpy(program->Base.Instructions, instBuffer,
1106 sizeof(struct prog_instruction)
1107 * program->Base.NumInstructions);
1108
Keith Whitwelldbeea252005-05-10 13:57:50 +00001109 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001110 */
Brian Paule998c342006-10-29 21:17:18 +00001111 if (ctx->Driver.ProgramStringNotify) {
1112 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1113 &p.program->Base );
1114 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001115
Brian Paule998c342006-10-29 21:17:18 +00001116 if (DISASSEM) {
1117 _mesa_print_program(&p.program->Base);
1118 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001119 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001120}
1121
Brian Paul5d7b49f2005-11-19 23:12:20 +00001122
Brian Paul122629f2006-07-20 16:49:57 +00001123static struct gl_fragment_program *
Brian Pauld9736db2006-05-23 02:44:46 +00001124search_cache(const struct texenvprog_cache *cache,
1125 GLuint hash,
Brian Paule4cb9cd2006-05-30 22:15:24 +00001126 const void *key,
Brian Pauld9736db2006-05-23 02:44:46 +00001127 GLuint keysize)
Keith Whitwellce721142005-07-11 10:10:38 +00001128{
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001129 struct texenvprog_cache_item *c;
Keith Whitwellce721142005-07-11 10:10:38 +00001130
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001131 for (c = cache->items[hash % cache->size]; c; c = c->next) {
1132 if (c->hash == hash && memcmp(c->key, key, keysize) == 0)
Brian Paul122629f2006-07-20 16:49:57 +00001133 return (struct gl_fragment_program *) c->data;
Keith Whitwellce721142005-07-11 10:10:38 +00001134 }
1135
1136 return NULL;
1137}
1138
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001139static void rehash( struct texenvprog_cache *cache )
1140{
1141 struct texenvprog_cache_item **items;
1142 struct texenvprog_cache_item *c, *next;
1143 GLuint size, i;
1144
1145 size = cache->size * 3;
1146 items = (struct texenvprog_cache_item**) _mesa_malloc(size * sizeof(*items));
1147 _mesa_memset(items, 0, size * sizeof(*items));
1148
1149 for (i = 0; i < cache->size; i++)
1150 for (c = cache->items[i]; c; c = next) {
1151 next = c->next;
1152 c->next = items[c->hash % size];
1153 items[c->hash % size] = c;
1154 }
1155
1156 _mesa_free(cache->items);
1157 cache->items = items;
1158 cache->size = size;
1159}
1160
Keith Whitwell8065c122006-05-22 16:09:27 +00001161static void clear_cache( struct texenvprog_cache *cache )
1162{
1163 struct texenvprog_cache_item *c, *next;
1164 GLuint i;
1165
1166 for (i = 0; i < cache->size; i++) {
1167 for (c = cache->items[i]; c; c = next) {
1168 next = c->next;
1169 _mesa_free(c->key);
Brian Paul122629f2006-07-20 16:49:57 +00001170 cache->ctx->Driver.DeleteProgram(cache->ctx,
1171 (struct gl_program *) c->data);
Keith Whitwell8065c122006-05-22 16:09:27 +00001172 _mesa_free(c);
1173 }
1174 cache->items[i] = NULL;
1175 }
1176
1177
1178 cache->n_items = 0;
1179}
1180
1181
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001182static void cache_item( struct texenvprog_cache *cache,
Keith Whitwellce721142005-07-11 10:10:38 +00001183 GLuint hash,
Brian Paulb8f2f6f2006-05-23 01:55:31 +00001184 const struct state_key *key,
Keith Whitwellce721142005-07-11 10:10:38 +00001185 void *data )
1186{
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001187 struct texenvprog_cache_item *c = MALLOC(sizeof(*c));
Keith Whitwellce721142005-07-11 10:10:38 +00001188 c->hash = hash;
Keith Whitwell8065c122006-05-22 16:09:27 +00001189
1190 c->key = _mesa_malloc(sizeof(*key));
1191 memcpy(c->key, key, sizeof(*key));
1192
Keith Whitwellce721142005-07-11 10:10:38 +00001193 c->data = data;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001194
Keith Whitwell8065c122006-05-22 16:09:27 +00001195 if (cache->n_items > cache->size * 1.5) {
1196 if (cache->size < 1000)
1197 rehash(cache);
1198 else
1199 clear_cache(cache);
1200 }
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001201
Keith Whitwell8065c122006-05-22 16:09:27 +00001202 cache->n_items++;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001203 c->next = cache->items[hash % cache->size];
1204 cache->items[hash % cache->size] = c;
Keith Whitwellce721142005-07-11 10:10:38 +00001205}
1206
Brian Pauld9736db2006-05-23 02:44:46 +00001207static GLuint hash_key( const struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +00001208{
1209 GLuint *ikey = (GLuint *)key;
1210 GLuint hash = 0, i;
1211
Keith Whitwell8065c122006-05-22 16:09:27 +00001212 /* Make a slightly better attempt at a hash function:
Keith Whitwellce721142005-07-11 10:10:38 +00001213 */
Keith Whitwell8065c122006-05-22 16:09:27 +00001214 for (i = 0; i < sizeof(*key)/sizeof(*ikey); i++)
1215 {
1216 hash += ikey[i];
1217 hash += (hash << 10);
1218 hash ^= (hash >> 6);
1219 }
Keith Whitwellce721142005-07-11 10:10:38 +00001220
1221 return hash;
1222}
1223
1224void _mesa_UpdateTexEnvProgram( GLcontext *ctx )
1225{
Keith Whitwell8065c122006-05-22 16:09:27 +00001226 struct state_key key;
Keith Whitwellce721142005-07-11 10:10:38 +00001227 GLuint hash;
Brian Paul122629f2006-07-20 16:49:57 +00001228 const struct gl_fragment_program *prev = ctx->FragmentProgram._Current;
Keith Whitwellce721142005-07-11 10:10:38 +00001229
Keith Whitwellc3626a92005-11-01 17:25:49 +00001230 if (!ctx->FragmentProgram._Enabled) {
Keith Whitwell8065c122006-05-22 16:09:27 +00001231 make_state_key(ctx, &key);
1232 hash = hash_key(&key);
Keith Whitwellc3626a92005-11-01 17:25:49 +00001233
Brian Pauld9736db2006-05-23 02:44:46 +00001234 ctx->FragmentProgram._Current =
1235 ctx->_TexEnvProgram =
Keith Whitwell8065c122006-05-22 16:09:27 +00001236 search_cache(&ctx->Texture.env_fp_cache, hash, &key, sizeof(key));
Keith Whitwellce721142005-07-11 10:10:38 +00001237
Keith Whitwellc3626a92005-11-01 17:25:49 +00001238 if (!ctx->_TexEnvProgram) {
Aapo Tahkola40ca5b42005-11-18 17:57:27 +00001239 if (0) _mesa_printf("Building new texenv proggy for key %x\n", hash);
Keith Whitwellce721142005-07-11 10:10:38 +00001240
Keith Whitwellc3626a92005-11-01 17:25:49 +00001241 ctx->FragmentProgram._Current = ctx->_TexEnvProgram =
Brian Paul122629f2006-07-20 16:49:57 +00001242 (struct gl_fragment_program *)
Keith Whitwellc3626a92005-11-01 17:25:49 +00001243 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
Keith Whitwellce721142005-07-11 10:10:38 +00001244
Brian Paule998c342006-10-29 21:17:18 +00001245 create_new_program(ctx, &key, ctx->_TexEnvProgram);
Keith Whitwellce721142005-07-11 10:10:38 +00001246
Keith Whitwell8065c122006-05-22 16:09:27 +00001247 cache_item(&ctx->Texture.env_fp_cache, hash, &key, ctx->_TexEnvProgram);
Keith Whitwellc3626a92005-11-01 17:25:49 +00001248 } else {
Aapo Tahkola40ca5b42005-11-18 17:57:27 +00001249 if (0) _mesa_printf("Found existing texenv program for key %x\n", hash);
Keith Whitwellc3626a92005-11-01 17:25:49 +00001250 }
1251 }
1252 else {
1253 ctx->FragmentProgram._Current = ctx->FragmentProgram.Current;
Keith Whitwellce721142005-07-11 10:10:38 +00001254 }
Keith Whitwellc3626a92005-11-01 17:25:49 +00001255
1256 /* Tell the driver about the change. Could define a new target for
1257 * this?
1258 */
Brian Paul5d7b49f2005-11-19 23:12:20 +00001259 if (ctx->FragmentProgram._Current != prev && ctx->Driver.BindProgram) {
1260 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
Brian Paul122629f2006-07-20 16:49:57 +00001261 (struct gl_program *) ctx->FragmentProgram._Current);
Brian Paul5d7b49f2005-11-19 23:12:20 +00001262 }
Keith Whitwellce721142005-07-11 10:10:38 +00001263}
1264
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001265
1266void _mesa_TexEnvProgramCacheInit( GLcontext *ctx )
1267{
Keith Whitwell8065c122006-05-22 16:09:27 +00001268 ctx->Texture.env_fp_cache.ctx = ctx;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001269 ctx->Texture.env_fp_cache.size = 17;
1270 ctx->Texture.env_fp_cache.n_items = 0;
1271 ctx->Texture.env_fp_cache.items = (struct texenvprog_cache_item **)
1272 _mesa_calloc(ctx->Texture.env_fp_cache.size *
1273 sizeof(struct texenvprog_cache_item));
1274}
1275
1276
Keith Whitwellce721142005-07-11 10:10:38 +00001277void _mesa_TexEnvProgramCacheDestroy( GLcontext *ctx )
1278{
Keith Whitwell8065c122006-05-22 16:09:27 +00001279 clear_cache(&ctx->Texture.env_fp_cache);
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001280 _mesa_free(ctx->Texture.env_fp_cache.items);
Keith Whitwellce721142005-07-11 10:10:38 +00001281}