blob: 72b54b27d9a6a61d2c42adc1b74a1ce3031717a4 [file] [log] [blame]
Keith Whitwell15e75e02005-04-29 15:11:39 +00001/**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
Keith Whitwell15e75e02005-04-29 15:11:39 +000028#include "glheader.h"
29#include "macros.h"
30#include "enums.h"
Brianc223c6b2007-07-04 13:15:20 -060031#include "shader/prog_parameter.h"
32#include "shader/prog_instruction.h"
33#include "shader/prog_print.h"
34#include "shader/prog_statevars.h"
Keith Whitwell15e75e02005-04-29 15:11:39 +000035#include "texenvprogram.h"
36
Brian Paule998c342006-10-29 21:17:18 +000037/**
38 * According to Glean's texCombine test, no more than 21 instructions
39 * are needed. Allow a few extra just in case.
40 */
41#define MAX_INSTRUCTIONS 24
Keith Whitwell15e75e02005-04-29 15:11:39 +000042
Keith Whitwell269e3892005-05-12 10:28:43 +000043#define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
Keith Whitwell15e75e02005-04-29 15:11:39 +000044
Keith Whitwellce721142005-07-11 10:10:38 +000045struct mode_opt {
Brian Paul5d7b49f2005-11-19 23:12:20 +000046 GLuint Source:4;
47 GLuint Operand:3;
Keith Whitwellce721142005-07-11 10:10:38 +000048};
49
50struct state_key {
Brian Paul5d7b49f2005-11-19 23:12:20 +000051 GLbitfield enabled_units;
52 GLuint separate_specular:1;
53 GLuint fog_enabled:1;
54 GLuint fog_mode:2;
Keith Whitwellce721142005-07-11 10:10:38 +000055
56 struct {
Brian Paul5d7b49f2005-11-19 23:12:20 +000057 GLuint enabled:1;
58 GLuint source_index:3; /* one of TEXTURE_1D/2D/3D/CUBE/RECT_INDEX */
59 GLuint ScaleShiftRGB:2;
60 GLuint ScaleShiftA:2;
Keith Whitwellce721142005-07-11 10:10:38 +000061
Brian Paul5d7b49f2005-11-19 23:12:20 +000062 GLuint NumArgsRGB:2;
63 GLuint ModeRGB:4;
Keith Whitwellce721142005-07-11 10:10:38 +000064 struct mode_opt OptRGB[3];
65
Brian Paul5d7b49f2005-11-19 23:12:20 +000066 GLuint NumArgsA:2;
67 GLuint ModeA:4;
Keith Whitwellce721142005-07-11 10:10:38 +000068 struct mode_opt OptA[3];
69 } unit[8];
70};
71
72#define FOG_LINEAR 0
73#define FOG_EXP 1
74#define FOG_EXP2 2
75#define FOG_UNKNOWN 3
76
77static GLuint translate_fog_mode( GLenum mode )
78{
79 switch (mode) {
80 case GL_LINEAR: return FOG_LINEAR;
81 case GL_EXP: return FOG_EXP;
82 case GL_EXP2: return FOG_EXP2;
83 default: return FOG_UNKNOWN;
84 }
85}
86
87#define OPR_SRC_COLOR 0
88#define OPR_ONE_MINUS_SRC_COLOR 1
89#define OPR_SRC_ALPHA 2
90#define OPR_ONE_MINUS_SRC_ALPHA 3
91#define OPR_ZERO 4
92#define OPR_ONE 5
93#define OPR_UNKNOWN 7
94
95static GLuint translate_operand( GLenum operand )
96{
97 switch (operand) {
98 case GL_SRC_COLOR: return OPR_SRC_COLOR;
99 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
100 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
101 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
102 case GL_ZERO: return OPR_ZERO;
103 case GL_ONE: return OPR_ONE;
104 default: return OPR_UNKNOWN;
105 }
106}
107
108#define SRC_TEXTURE 0
109#define SRC_TEXTURE0 1
110#define SRC_TEXTURE1 2
111#define SRC_TEXTURE2 3
112#define SRC_TEXTURE3 4
113#define SRC_TEXTURE4 5
114#define SRC_TEXTURE5 6
115#define SRC_TEXTURE6 7
116#define SRC_TEXTURE7 8
117#define SRC_CONSTANT 9
118#define SRC_PRIMARY_COLOR 10
119#define SRC_PREVIOUS 11
120#define SRC_UNKNOWN 15
121
122static GLuint translate_source( GLenum src )
123{
124 switch (src) {
125 case GL_TEXTURE: return SRC_TEXTURE;
126 case GL_TEXTURE0:
127 case GL_TEXTURE1:
128 case GL_TEXTURE2:
129 case GL_TEXTURE3:
130 case GL_TEXTURE4:
131 case GL_TEXTURE5:
132 case GL_TEXTURE6:
133 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
134 case GL_CONSTANT: return SRC_CONSTANT;
135 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
136 case GL_PREVIOUS: return SRC_PREVIOUS;
137 default: return SRC_UNKNOWN;
138 }
139}
140
141#define MODE_REPLACE 0
142#define MODE_MODULATE 1
143#define MODE_ADD 2
144#define MODE_ADD_SIGNED 3
145#define MODE_INTERPOLATE 4
146#define MODE_SUBTRACT 5
147#define MODE_DOT3_RGB 6
148#define MODE_DOT3_RGB_EXT 7
149#define MODE_DOT3_RGBA 8
150#define MODE_DOT3_RGBA_EXT 9
151#define MODE_MODULATE_ADD_ATI 10
152#define MODE_MODULATE_SIGNED_ADD_ATI 11
153#define MODE_MODULATE_SUBTRACT_ATI 12
154#define MODE_UNKNOWN 15
155
156static GLuint translate_mode( GLenum mode )
157{
158 switch (mode) {
159 case GL_REPLACE: return MODE_REPLACE;
160 case GL_MODULATE: return MODE_MODULATE;
161 case GL_ADD: return MODE_ADD;
162 case GL_ADD_SIGNED: return MODE_ADD_SIGNED;
163 case GL_INTERPOLATE: return MODE_INTERPOLATE;
164 case GL_SUBTRACT: return MODE_SUBTRACT;
165 case GL_DOT3_RGB: return MODE_DOT3_RGB;
166 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
167 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
168 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
169 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
170 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
171 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
172 default: return MODE_UNKNOWN;
173 }
174}
175
176#define TEXTURE_UNKNOWN_INDEX 7
Brian Paule00ac112005-09-15 05:00:45 +0000177static GLuint translate_tex_src_bit( GLbitfield bit )
Keith Whitwellce721142005-07-11 10:10:38 +0000178{
179 switch (bit) {
180 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
181 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
182 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
183 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
184 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
185 default: return TEXTURE_UNKNOWN_INDEX;
186 }
187}
188
Brian Paul22db5352005-11-19 23:45:10 +0000189/**
190 * Examine current texture environment state and generate a unique
191 * key to identify it.
192 */
Keith Whitwell8065c122006-05-22 16:09:27 +0000193static void make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000194{
Keith Whitwellce721142005-07-11 10:10:38 +0000195 GLuint i, j;
196
Keith Whitwell8065c122006-05-22 16:09:27 +0000197 memset(key, 0, sizeof(*key));
198
Keith Whitwellce721142005-07-11 10:10:38 +0000199 for (i=0;i<MAX_TEXTURE_UNITS;i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000200 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Keith Whitwellce721142005-07-11 10:10:38 +0000201
202 if (!texUnit->_ReallyEnabled)
203 continue;
204
205 key->unit[i].enabled = 1;
206 key->enabled_units |= (1<<i);
207
208 key->unit[i].source_index =
209 translate_tex_src_bit(texUnit->_ReallyEnabled);
210
211 key->unit[i].NumArgsRGB = texUnit->_CurrentCombine->_NumArgsRGB;
212 key->unit[i].NumArgsA = texUnit->_CurrentCombine->_NumArgsA;
213
214 key->unit[i].ModeRGB =
215 translate_mode(texUnit->_CurrentCombine->ModeRGB);
216 key->unit[i].ModeA =
217 translate_mode(texUnit->_CurrentCombine->ModeA);
218
219 key->unit[i].ScaleShiftRGB = texUnit->_CurrentCombine->ScaleShiftRGB;
Keith Whitwell64da1612006-05-22 14:30:58 +0000220 key->unit[i].ScaleShiftA = texUnit->_CurrentCombine->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000221
222 for (j=0;j<3;j++) {
223 key->unit[i].OptRGB[j].Operand =
224 translate_operand(texUnit->_CurrentCombine->OperandRGB[j]);
225 key->unit[i].OptA[j].Operand =
226 translate_operand(texUnit->_CurrentCombine->OperandA[j]);
227 key->unit[i].OptRGB[j].Source =
228 translate_source(texUnit->_CurrentCombine->SourceRGB[j]);
229 key->unit[i].OptA[j].Source =
230 translate_source(texUnit->_CurrentCombine->SourceA[j]);
231 }
232 }
233
234 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
235 key->separate_specular = 1;
236
237 if (ctx->Fog.Enabled) {
238 key->fog_enabled = 1;
239 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
240 }
Keith Whitwellce721142005-07-11 10:10:38 +0000241}
242
Keith Whitwell15e75e02005-04-29 15:11:39 +0000243/* Use uregs to represent registers internally, translate to Mesa's
244 * expected formats on emit.
245 *
246 * NOTE: These are passed by value extensively in this file rather
247 * than as usual by pointer reference. If this disturbs you, try
248 * remembering they are just 32bits in size.
249 *
250 * GCC is smart enough to deal with these dword-sized structures in
251 * much the same way as if I had defined them as dwords and was using
252 * macros to access and set the fields. This is much nicer and easier
253 * to evolve.
254 */
255struct ureg {
256 GLuint file:4;
257 GLuint idx:8;
258 GLuint negatebase:1;
259 GLuint abs:1;
260 GLuint negateabs:1;
261 GLuint swz:12;
262 GLuint pad:5;
263};
264
Brian Paul13abf912006-04-13 19:17:13 +0000265static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000266 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000267 ~0,
268 0,
269 0,
270 0,
271 0,
272 0
273};
274
Keith Whitwell15e75e02005-04-29 15:11:39 +0000275
Keith Whitwell15e75e02005-04-29 15:11:39 +0000276/* State used to build the fragment program:
277 */
278struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000279 struct gl_fragment_program *program;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000280 GLcontext *ctx;
Keith Whitwellce721142005-07-11 10:10:38 +0000281 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000282
Brian Paulc8d17412005-12-14 03:06:16 +0000283 GLbitfield alu_temps; /* Track texture indirections, see spec. */
284 GLbitfield temps_output; /* Track texture indirections, see spec. */
285 GLbitfield temp_in_use; /* Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000286 GLboolean error;
287
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000288 struct ureg src_texture[MAX_TEXTURE_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000289 /* Reg containing each texture unit's sampled texture color,
290 * else undef.
291 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000292
293 struct ureg src_previous; /* Reg containing color from previous
294 * stage. May need to be decl'd.
295 */
296
297 GLuint last_tex_stage; /* Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000298
299 struct ureg half;
300 struct ureg one;
301 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000302};
303
304
305
306static struct ureg make_ureg(GLuint file, GLuint idx)
307{
308 struct ureg reg;
309 reg.file = file;
310 reg.idx = idx;
311 reg.negatebase = 0;
312 reg.abs = 0;
313 reg.negateabs = 0;
314 reg.swz = SWIZZLE_NOOP;
315 reg.pad = 0;
316 return reg;
317}
318
319static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
320{
321 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
322 GET_SWZ(reg.swz, y),
323 GET_SWZ(reg.swz, z),
324 GET_SWZ(reg.swz, w));
325
326 return reg;
327}
328
329static struct ureg swizzle1( struct ureg reg, int x )
330{
331 return swizzle(reg, x, x, x, x);
332}
333
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000334static struct ureg negate( struct ureg reg )
335{
336 reg.negatebase ^= 1;
337 return reg;
338}
339
Keith Whitwell15e75e02005-04-29 15:11:39 +0000340static GLboolean is_undef( struct ureg reg )
341{
Alan Hourihane8d972652006-08-10 13:12:00 +0000342 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000343}
344
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000345
Keith Whitwell15e75e02005-04-29 15:11:39 +0000346static struct ureg get_temp( struct texenv_fragment_program *p )
347{
Brian Paul13abf912006-04-13 19:17:13 +0000348 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000349
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000350 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000351 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000352 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000353
354 /* Then any unused temporary:
355 */
356 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000357 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000358
Keith Whitwell15e75e02005-04-29 15:11:39 +0000359 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000360 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000361 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000362 }
363
Brian Paul13abf912006-04-13 19:17:13 +0000364 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000365 p->program->Base.NumTemporaries = bit;
366
Keith Whitwell93cd9232005-05-11 08:30:23 +0000367 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000368 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
369}
370
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000371static struct ureg get_tex_temp( struct texenv_fragment_program *p )
372{
373 int bit;
374
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000375 /* First try to find availble temp not previously used (to avoid
376 * starting a new texture indirection). According to the spec, the
377 * ~p->temps_output isn't necessary, but will keep it there for
378 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000379 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000380 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000381
382 /* Then any unused temporary:
383 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000384 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000385 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000386
387 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000388 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000389 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000390 }
391
Brian Paul13abf912006-04-13 19:17:13 +0000392 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000393 p->program->Base.NumTemporaries = bit;
394
Keith Whitwell93cd9232005-05-11 08:30:23 +0000395 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000396 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
397}
398
Keith Whitwell15e75e02005-04-29 15:11:39 +0000399
400static void release_temps( struct texenv_fragment_program *p )
401{
Brian Paul05051032005-11-01 04:36:33 +0000402 GLuint max_temp = p->ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000403
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000404 /* KW: To support tex_env_crossbar, don't release the registers in
405 * temps_output.
406 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000407 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000408 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000409 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000410 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000411}
412
413
Brian9fe3e2e2007-02-23 11:44:14 -0700414static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000415 GLint s0,
416 GLint s1,
417 GLint s2,
418 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700419 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000420{
Brian9fe3e2e2007-02-23 11:44:14 -0700421 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000422 GLuint idx;
423 tokens[0] = s0;
424 tokens[1] = s1;
425 tokens[2] = s2;
426 tokens[3] = s3;
427 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000428 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000429 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000430}
431
Keith Whitwell47b29f52005-05-04 11:44:44 +0000432
Brian9fe3e2e2007-02-23 11:44:14 -0700433#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
434#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
435#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
436#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000437
438
439static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
440{
Brian Paulde997602005-11-12 17:53:14 +0000441 p->program->Base.InputsRead |= (1 << input);
Keith Whitwell47b29f52005-05-04 11:44:44 +0000442 return make_ureg(PROGRAM_INPUT, input);
443}
444
445
Brian Paul7e807512005-11-05 17:10:45 +0000446static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000447 struct ureg ureg )
448{
449 reg->File = ureg.file;
450 reg->Index = ureg.idx;
451 reg->Swizzle = ureg.swz;
Keith Whitwellf2802c42005-10-07 09:55:26 +0000452 reg->NegateBase = ureg.negatebase ? 0xf : 0x0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000453 reg->Abs = ureg.abs;
454 reg->NegateAbs = ureg.negateabs;
455}
456
Brian Paul7e807512005-11-05 17:10:45 +0000457static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000458 struct ureg ureg, GLuint mask )
459{
460 dst->File = ureg.file;
461 dst->Index = ureg.idx;
462 dst->WriteMask = mask;
463 dst->CondMask = 0;
464 dst->CondSwizzle = 0;
465}
466
Brian Paul7e807512005-11-05 17:10:45 +0000467static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000468emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000469 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000470 struct ureg dest,
471 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000472 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000473 struct ureg src0,
474 struct ureg src1,
475 struct ureg src2 )
476{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000477 GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000478 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000479
Brian Pauld6272e02006-10-29 18:03:16 +0000480 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000481 inst->Opcode = op;
482
Keith Whitwell47b29f52005-05-04 11:44:44 +0000483 emit_arg( &inst->SrcReg[0], src0 );
484 emit_arg( &inst->SrcReg[1], src1 );
485 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000486
Brian Paule31ac052005-11-20 17:52:40 +0000487 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000488
489 emit_dst( &inst->DstReg, dest, mask );
490
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000491 /* Accounting for indirection tracking:
492 */
493 if (dest.file == PROGRAM_TEMPORARY)
494 p->temps_output |= 1 << dest.idx;
495
Keith Whitwell15e75e02005-04-29 15:11:39 +0000496 return inst;
497}
498
499
500static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000501 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000502 struct ureg dest,
503 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000504 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000505 struct ureg src0,
506 struct ureg src1,
507 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000508{
509 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
510
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000511 /* Accounting for indirection tracking:
512 */
513 if (src0.file == PROGRAM_TEMPORARY)
514 p->alu_temps |= 1 << src0.idx;
515
516 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
517 p->alu_temps |= 1 << src1.idx;
518
519 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
520 p->alu_temps |= 1 << src2.idx;
521
522 if (dest.file == PROGRAM_TEMPORARY)
523 p->alu_temps |= 1 << dest.idx;
524
Brian21f99792007-01-09 11:00:21 -0700525 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000526 return dest;
527}
528
529static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000530 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000531 struct ureg dest,
532 GLuint destmask,
533 GLuint tex_unit,
534 GLuint tex_idx,
535 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000536{
Brian Paul7e807512005-11-05 17:10:45 +0000537 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000538 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000539 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000540 coord, /* arg 0? */
541 undef,
542 undef);
543
Brian Paul7e807512005-11-05 17:10:45 +0000544 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000545 inst->TexSrcUnit = tex_unit;
546
Brian21f99792007-01-09 11:00:21 -0700547 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000548
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000549 /* Is this a texture indirection?
550 */
551 if ((coord.file == PROGRAM_TEMPORARY &&
552 (p->temps_output & (1<<coord.idx))) ||
553 (dest.file == PROGRAM_TEMPORARY &&
554 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700555 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000556 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000557 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000558 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000559 }
560
561 return dest;
562}
563
564
Keith Whitwell47b29f52005-05-04 11:44:44 +0000565static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000566 GLfloat s0,
567 GLfloat s1,
568 GLfloat s2,
569 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000570{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000571 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700572 GLuint idx, swizzle;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000573 values[0] = s0;
574 values[1] = s1;
575 values[2] = s2;
576 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700577 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
578 &swizzle );
579 ASSERT(swizzle == SWIZZLE_NOOP);
Keith Whitwell47b29f52005-05-04 11:44:44 +0000580 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000581}
582
Keith Whitwelle4902422005-05-11 15:16:35 +0000583#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000584#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
585#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
586#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000587
588
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000589static struct ureg get_one( struct texenv_fragment_program *p )
590{
591 if (is_undef(p->one))
592 p->one = register_scalar_const(p, 1.0);
593 return p->one;
594}
595
596static struct ureg get_half( struct texenv_fragment_program *p )
597{
598 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000599 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000600 return p->half;
601}
602
603static struct ureg get_zero( struct texenv_fragment_program *p )
604{
605 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000606 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000607 return p->zero;
608}
609
Keith Whitwell15e75e02005-04-29 15:11:39 +0000610
Keith Whitwell15e75e02005-04-29 15:11:39 +0000611static void program_error( struct texenv_fragment_program *p, const char *msg )
612{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000613 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000614 p->error = 1;
615}
616
Keith Whitwell15e75e02005-04-29 15:11:39 +0000617static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000618 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000619{
620 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000621 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000622 assert(!is_undef(p->src_texture[unit]));
623 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000624
Keith Whitwellce721142005-07-11 10:10:38 +0000625 case SRC_TEXTURE0:
626 case SRC_TEXTURE1:
627 case SRC_TEXTURE2:
628 case SRC_TEXTURE3:
629 case SRC_TEXTURE4:
630 case SRC_TEXTURE5:
631 case SRC_TEXTURE6:
632 case SRC_TEXTURE7:
633 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
634 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000635
Keith Whitwellce721142005-07-11 10:10:38 +0000636 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000637 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000638
Keith Whitwellce721142005-07-11 10:10:38 +0000639 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000640 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000641
Keith Whitwellce721142005-07-11 10:10:38 +0000642 case SRC_PREVIOUS:
643 default:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000644 if (is_undef(p->src_previous))
645 return register_input(p, FRAG_ATTRIB_COL0);
646 else
647 return p->src_previous;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000648 }
649}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000650
Keith Whitwell15e75e02005-04-29 15:11:39 +0000651static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000652 GLuint mask,
653 GLuint unit,
654 GLuint source,
655 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000656{
657 struct ureg arg, src, one;
658
659 src = get_source(p, source, unit);
660
661 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000662 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000663 /* Get unused tmp,
664 * Emit tmp = 1.0 - arg.xyzw
665 */
666 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000667 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000668 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000669
Keith Whitwellce721142005-07-11 10:10:38 +0000670 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000671 if (mask == WRITEMASK_W)
672 return src;
673 else
Brian Paul22db5352005-11-19 23:45:10 +0000674 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000675 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000676 /* Get unused tmp,
677 * Emit tmp = 1.0 - arg.wwww
678 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000679 arg = get_temp(p);
680 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000681 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000682 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000683 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000684 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000685 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000686 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000687 case OPR_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000688 default:
689 return src;
690 }
691}
692
Keith Whitwellce721142005-07-11 10:10:38 +0000693static GLboolean args_match( struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000694{
Brian Paul22db5352005-11-19 23:45:10 +0000695 GLuint i, nr = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000696
697 for (i = 0 ; i < nr ; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000698 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000699 return GL_FALSE;
700
Keith Whitwellce721142005-07-11 10:10:38 +0000701 switch(key->unit[unit].OptA[i].Operand) {
702 case OPR_SRC_ALPHA:
703 switch(key->unit[unit].OptRGB[i].Operand) {
704 case OPR_SRC_COLOR:
705 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000706 break;
707 default:
708 return GL_FALSE;
709 }
710 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000711 case OPR_ONE_MINUS_SRC_ALPHA:
712 switch(key->unit[unit].OptRGB[i].Operand) {
713 case OPR_ONE_MINUS_SRC_COLOR:
714 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000715 break;
716 default:
717 return GL_FALSE;
718 }
719 break;
720 default:
721 return GL_FALSE; /* impossible */
722 }
723 }
724
725 return GL_TRUE;
726}
727
Keith Whitwell15e75e02005-04-29 15:11:39 +0000728static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000729 struct ureg dest,
730 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000731 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000732 GLuint unit,
733 GLuint nr,
734 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +0000735 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000736{
Keith Whitwell15e75e02005-04-29 15:11:39 +0000737 struct ureg src[3];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000738 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +0000739 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000740
Brian Paul00630842005-12-12 15:27:55 +0000741 tmp = undef; /* silence warning (bug 5318) */
742
Keith Whitwell15e75e02005-04-29 15:11:39 +0000743 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +0000744 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000745
746 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +0000747 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000748 if (mask == WRITEMASK_XYZW && !saturate)
749 return src[0];
750 else
Brian Paul7e807512005-11-05 17:10:45 +0000751 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000752 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +0000753 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000754 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000755 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +0000756 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000757 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000758 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000759 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000760 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +0000761 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000762 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +0000763 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +0000764 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
765 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000766 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +0000767 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000768 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
769 */
Brian Paul7e807512005-11-05 17:10:45 +0000770 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000771
Keith Whitwellce721142005-07-11 10:10:38 +0000772 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +0000773 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000774
Keith Whitwellce721142005-07-11 10:10:38 +0000775 case MODE_DOT3_RGBA:
776 case MODE_DOT3_RGBA_EXT:
777 case MODE_DOT3_RGB_EXT:
778 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000779 struct ureg tmp0 = get_temp( p );
780 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +0000781 struct ureg neg1 = register_scalar_const(p, -1);
782 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000783
784 /* tmp0 = 2*src0 - 1
785 * tmp1 = 2*src1 - 1
786 *
787 * dst = tmp0 dot3 tmp1
788 */
Brian Paul7e807512005-11-05 17:10:45 +0000789 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000790 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000791
Brian Paulaa206952005-09-16 18:14:24 +0000792 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000793 tmp1 = tmp0;
794 else
Brian Paul7e807512005-11-05 17:10:45 +0000795 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000796 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +0000797 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000798 return dest;
799 }
Keith Whitwellce721142005-07-11 10:10:38 +0000800 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000801 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +0000802 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000803 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +0000804 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000805 /* Arg0 * Arg2 + Arg1 - 0.5 */
806 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000807 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +0000808 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
809 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000810 return dest;
811 }
Keith Whitwellce721142005-07-11 10:10:38 +0000812 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000813 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +0000814 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000815 return dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000816 default:
817 return src[0];
818 }
819}
820
Keith Whitwell15e75e02005-04-29 15:11:39 +0000821
Brian Paul22db5352005-11-19 23:45:10 +0000822/**
823 * Generate instructions for one texture unit's env/combiner mode.
824 */
825static struct ureg
826emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000827{
Keith Whitwellce721142005-07-11 10:10:38 +0000828 struct state_key *key = p->state;
Brian Paul22db5352005-11-19 23:45:10 +0000829 GLboolean saturate = (unit < p->last_tex_stage);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000830 GLuint rgb_shift, alpha_shift;
831 struct ureg out, shift;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000832 struct ureg dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000833
Keith Whitwellce721142005-07-11 10:10:38 +0000834 if (!key->unit[unit].enabled) {
835 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000836 }
Keith Whitwellce721142005-07-11 10:10:38 +0000837
838 switch (key->unit[unit].ModeRGB) {
839 case MODE_DOT3_RGB_EXT:
840 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000841 rgb_shift = 0;
842 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000843 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000844 alpha_shift = 0;
845 rgb_shift = 0;
846 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000847 default:
Keith Whitwellce721142005-07-11 10:10:38 +0000848 rgb_shift = key->unit[unit].ScaleShiftRGB;
849 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000850 break;
851 }
Keith Whitwellce721142005-07-11 10:10:38 +0000852
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000853 /* If this is the very last calculation, emit direct to output reg:
854 */
Keith Whitwellce721142005-07-11 10:10:38 +0000855 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000856 unit != p->last_tex_stage ||
857 alpha_shift ||
858 rgb_shift)
859 dest = get_temp( p );
860 else
Brian Paul90ebb582005-11-02 18:06:12 +0000861 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLR);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000862
863 /* Emit the RGB and A combine ops
864 */
Keith Whitwellce721142005-07-11 10:10:38 +0000865 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
866 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000867 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
868 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000869 key->unit[unit].NumArgsRGB,
870 key->unit[unit].ModeRGB,
871 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000872 }
Keith Whitwellce721142005-07-11 10:10:38 +0000873 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +0200874 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000875
876 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
877 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000878 key->unit[unit].NumArgsRGB,
879 key->unit[unit].ModeRGB,
880 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000881 }
882 else {
883 /* Need to do something to stop from re-emitting identical
884 * argument calculations here:
885 */
886 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
887 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000888 key->unit[unit].NumArgsRGB,
889 key->unit[unit].ModeRGB,
890 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000891 out = emit_combine( p, dest, WRITEMASK_W, saturate,
892 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000893 key->unit[unit].NumArgsA,
894 key->unit[unit].ModeA,
895 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000896 }
897
898 /* Deal with the final shift:
899 */
900 if (alpha_shift || rgb_shift) {
901 if (rgb_shift == alpha_shift) {
Keith Whitwelle4902422005-05-11 15:16:35 +0000902 shift = register_scalar_const(p, 1<<rgb_shift);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000903 }
904 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000905 shift = register_const4f(p,
906 1<<rgb_shift,
907 1<<rgb_shift,
908 1<<rgb_shift,
909 1<<alpha_shift);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000910 }
Brian Paul7e807512005-11-05 17:10:45 +0000911 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000912 saturate, out, shift, undef );
913 }
914 else
915 return out;
916}
917
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000918
Brian Paul22db5352005-11-19 23:45:10 +0000919/**
920 * Generate instruction for getting a texture source term.
921 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000922static void load_texture( struct texenv_fragment_program *p, GLuint unit )
923{
924 if (is_undef(p->src_texture[unit])) {
Keith Whitwellce721142005-07-11 10:10:38 +0000925 GLuint dim = p->state->unit[unit].source_index;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000926 struct ureg texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
927 struct ureg tmp = get_tex_temp( p );
928
Brian Paulbfb5ea32005-07-19 18:20:04 +0000929 if (dim == TEXTURE_UNKNOWN_INDEX)
930 program_error(p, "TexSrcBit");
Keith Whitwellce721142005-07-11 10:10:38 +0000931
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000932 /* TODO: Use D0_MASK_XY where possible.
933 */
Aapo Tahkolab8915342006-03-28 10:26:34 +0000934 if (p->state->unit[unit].enabled)
935 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
936 tmp, WRITEMASK_XYZW,
937 unit, dim, texcoord );
938 else
939 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000940 }
941}
942
Keith Whitwell241b6b72005-05-23 09:50:34 +0000943static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000944 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000945{
946 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000947 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000948 load_texture(p, unit);
949 break;
950
Keith Whitwellce721142005-07-11 10:10:38 +0000951 case SRC_TEXTURE0:
952 case SRC_TEXTURE1:
953 case SRC_TEXTURE2:
954 case SRC_TEXTURE3:
955 case SRC_TEXTURE4:
956 case SRC_TEXTURE5:
957 case SRC_TEXTURE6:
958 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +0000959 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000960 break;
961
962 default:
963 break;
964 }
Keith Whitwell241b6b72005-05-23 09:50:34 +0000965
966 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000967}
968
Brian Paul22db5352005-11-19 23:45:10 +0000969
970/**
971 * Generate instructions for loading all texture source terms.
972 */
973static GLboolean
974load_texunit_sources( struct texenv_fragment_program *p, int unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000975{
Keith Whitwellce721142005-07-11 10:10:38 +0000976 struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +0000977 GLuint i;
978
979 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
980 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000981 }
Aapo Tahkolab8915342006-03-28 10:26:34 +0000982
983 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
984 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
985 }
986
Keith Whitwell241b6b72005-05-23 09:50:34 +0000987 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000988}
989
Brian Paul22db5352005-11-19 23:45:10 +0000990
991/**
992 * Generate a new fragment program which implements the context's
993 * current texture env/combine mode.
994 */
995static void
Brian Paule998c342006-10-29 21:17:18 +0000996create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +0000997 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000998{
Brian Paule998c342006-10-29 21:17:18 +0000999 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001000 struct texenv_fragment_program p;
1001 GLuint unit;
1002 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001003
Keith Whitwelle4902422005-05-11 15:16:35 +00001004 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +00001005 p.ctx = ctx;
Keith Whitwellce721142005-07-11 10:10:38 +00001006 p.state = key;
1007 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001008
Brian Paule998c342006-10-29 21:17:18 +00001009 /* During code generation, use locally-allocated instruction buffer,
1010 * then alloc dynamic storage below.
1011 */
1012 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001013 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Brian21f99792007-01-09 11:00:21 -07001014 p.program->Base.NumTexIndirections = 1; /* correct? */
1015 p.program->Base.NumTexInstructions = 0;
1016 p.program->Base.NumAluInstructions = 0;
Brian1240eb22007-03-22 08:50:20 -06001017 p.program->Base.String = NULL;
Keith Whitwell9ca88152005-05-10 09:56:02 +00001018 p.program->Base.NumInstructions =
Brian Paule998c342006-10-29 21:17:18 +00001019 p.program->Base.NumTemporaries =
1020 p.program->Base.NumParameters =
1021 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001022 p.program->Base.Parameters = _mesa_new_parameter_list();
Keith Whitwelldbeea252005-05-10 13:57:50 +00001023
Brian Paulde997602005-11-12 17:53:14 +00001024 p.program->Base.InputsRead = 0;
1025 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001026
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001027 for (unit = 0; unit < MAX_TEXTURE_UNITS; unit++)
1028 p.src_texture[unit] = undef;
1029
Keith Whitwell47b29f52005-05-04 11:44:44 +00001030 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001031 p.half = undef;
1032 p.zero = undef;
1033 p.one = undef;
1034
Keith Whitwell15e75e02005-04-29 15:11:39 +00001035 p.last_tex_stage = 0;
Keith Whitwell93cd9232005-05-11 08:30:23 +00001036 release_temps(&p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001037
Keith Whitwellce721142005-07-11 10:10:38 +00001038 if (key->enabled_units) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001039 /* First pass - to support texture_env_crossbar, first identify
1040 * all referenced texture sources and emit texld instructions
1041 * for each:
1042 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001043 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001044 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001045 load_texunit_sources( &p, unit );
1046 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001047 }
1048
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001049 /* Second pass - emit combine instructions to build final color:
1050 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001051 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001052 if (key->enabled_units & (1<<unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001053 p.src_previous = emit_texenv( &p, unit );
Keith Whitwell93cd9232005-05-11 08:30:23 +00001054 release_temps(&p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001055 }
1056 }
1057
Keith Whitwellce721142005-07-11 10:10:38 +00001058 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul90ebb582005-11-02 18:06:12 +00001059 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001060
Keith Whitwellce721142005-07-11 10:10:38 +00001061 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001062 /* Emit specular add.
1063 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001064 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001065 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1066 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001067 }
Brian Paulaa206952005-09-16 18:14:24 +00001068 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001069 /* Will wind up in here if no texture enabled or a couple of
1070 * other scenarios (GL_REPLACE for instance).
1071 */
Brian Paul7e807512005-11-05 17:10:45 +00001072 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001073 }
1074
Keith Whitwell47b29f52005-05-04 11:44:44 +00001075 /* Finish up:
1076 */
Brian Paul7e807512005-11-05 17:10:45 +00001077 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001078
Keith Whitwellce721142005-07-11 10:10:38 +00001079 if (key->fog_enabled) {
1080 /* Pull fog mode from GLcontext, the value in the state key is
1081 * a reduced value and not what is expected in FogOption
1082 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001083 p.program->FogOption = ctx->Fog.Mode;
Keith Whitwellce721142005-07-11 10:10:38 +00001084 } else
Keith Whitwell276330b2005-05-09 17:58:13 +00001085 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001086
Brian21f99792007-01-09 11:00:21 -07001087 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001088 program_error(&p, "Exceeded max nr indirect texture lookups");
1089
Brian21f99792007-01-09 11:00:21 -07001090 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001091 program_error(&p, "Exceeded max TEX instructions");
1092
Brian21f99792007-01-09 11:00:21 -07001093 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001094 program_error(&p, "Exceeded max ALU instructions");
1095
Brian Paul5d7b49f2005-11-19 23:12:20 +00001096 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001097
Brian Paule998c342006-10-29 21:17:18 +00001098 /* Allocate final instruction array */
Brian Paule998c342006-10-29 21:17:18 +00001099 program->Base.Instructions
1100 = _mesa_alloc_instructions(program->Base.NumInstructions);
1101 if (!program->Base.Instructions) {
1102 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1103 "generating tex env program");
1104 return;
1105 }
Brian1240eb22007-03-22 08:50:20 -06001106 _mesa_copy_instructions(program->Base.Instructions, instBuffer,
1107 program->Base.NumInstructions);
Brian Paule998c342006-10-29 21:17:18 +00001108
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{
Brian18d1fde2007-01-23 11:46:02 -07001187 struct texenvprog_cache_item *c
1188 = (struct texenvprog_cache_item *) MALLOC(sizeof(*c));
Keith Whitwellce721142005-07-11 10:10:38 +00001189 c->hash = hash;
Keith Whitwell8065c122006-05-22 16:09:27 +00001190
1191 c->key = _mesa_malloc(sizeof(*key));
1192 memcpy(c->key, key, sizeof(*key));
1193
Brian18d1fde2007-01-23 11:46:02 -07001194 c->data = (struct gl_fragment_program *) data;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001195
Keith Whitwell8065c122006-05-22 16:09:27 +00001196 if (cache->n_items > cache->size * 1.5) {
1197 if (cache->size < 1000)
1198 rehash(cache);
1199 else
1200 clear_cache(cache);
1201 }
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001202
Keith Whitwell8065c122006-05-22 16:09:27 +00001203 cache->n_items++;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001204 c->next = cache->items[hash % cache->size];
1205 cache->items[hash % cache->size] = c;
Keith Whitwellce721142005-07-11 10:10:38 +00001206}
1207
Brian Pauld9736db2006-05-23 02:44:46 +00001208static GLuint hash_key( const struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +00001209{
1210 GLuint *ikey = (GLuint *)key;
1211 GLuint hash = 0, i;
1212
Keith Whitwell8065c122006-05-22 16:09:27 +00001213 /* Make a slightly better attempt at a hash function:
Keith Whitwellce721142005-07-11 10:10:38 +00001214 */
Keith Whitwell8065c122006-05-22 16:09:27 +00001215 for (i = 0; i < sizeof(*key)/sizeof(*ikey); i++)
1216 {
1217 hash += ikey[i];
1218 hash += (hash << 10);
1219 hash ^= (hash >> 6);
1220 }
Keith Whitwellce721142005-07-11 10:10:38 +00001221
1222 return hash;
1223}
1224
Briana90046f2006-12-15 10:07:26 -07001225
1226/**
1227 * If _MaintainTexEnvProgram is set we'll generate a fragment program that
1228 * implements the current texture env/combine mode.
1229 * This function generates that program and puts it into effect.
1230 */
1231void
1232_mesa_UpdateTexEnvProgram( GLcontext *ctx )
Keith Whitwellce721142005-07-11 10:10:38 +00001233{
Keith Whitwell8065c122006-05-22 16:09:27 +00001234 struct state_key key;
Keith Whitwellce721142005-07-11 10:10:38 +00001235 GLuint hash;
Brian Paul122629f2006-07-20 16:49:57 +00001236 const struct gl_fragment_program *prev = ctx->FragmentProgram._Current;
Keith Whitwellce721142005-07-11 10:10:38 +00001237
Briana90046f2006-12-15 10:07:26 -07001238 ASSERT(ctx->FragmentProgram._MaintainTexEnvProgram);
1239
1240 /* If a conventional fragment program/shader isn't in effect... */
Brianefcfdbd2007-02-24 15:51:41 -07001241 if (!ctx->FragmentProgram._Enabled &&
1242 !ctx->Shader.CurrentProgram) {
Keith Whitwell8065c122006-05-22 16:09:27 +00001243 make_state_key(ctx, &key);
1244 hash = hash_key(&key);
Keith Whitwellc3626a92005-11-01 17:25:49 +00001245
Brian Pauld9736db2006-05-23 02:44:46 +00001246 ctx->FragmentProgram._Current =
Briana90046f2006-12-15 10:07:26 -07001247 ctx->FragmentProgram._TexEnvProgram =
1248 search_cache(&ctx->Texture.env_fp_cache, hash, &key, sizeof(key));
Keith Whitwellce721142005-07-11 10:10:38 +00001249
Briana90046f2006-12-15 10:07:26 -07001250 if (!ctx->FragmentProgram._TexEnvProgram) {
1251 if (0)
1252 _mesa_printf("Building new texenv proggy for key %x\n", hash);
1253
1254 /* create new tex env program */
Brianefcfdbd2007-02-24 15:51:41 -07001255 ctx->FragmentProgram._Current =
1256 ctx->FragmentProgram._TexEnvProgram =
1257 (struct gl_fragment_program *)
Briana90046f2006-12-15 10:07:26 -07001258 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1259
1260 create_new_program(ctx, &key, ctx->FragmentProgram._TexEnvProgram);
1261
1262 cache_item(&ctx->Texture.env_fp_cache, hash, &key,
1263 ctx->FragmentProgram._TexEnvProgram);
Keith Whitwellc3626a92005-11-01 17:25:49 +00001264 }
Briana90046f2006-12-15 10:07:26 -07001265 else {
1266 if (0)
1267 _mesa_printf("Found existing texenv program for key %x\n", hash);
Keith Whitwellc3626a92005-11-01 17:25:49 +00001268 }
1269 }
1270 else {
1271 ctx->FragmentProgram._Current = ctx->FragmentProgram.Current;
Keith Whitwellce721142005-07-11 10:10:38 +00001272 }
Keith Whitwellc3626a92005-11-01 17:25:49 +00001273
1274 /* Tell the driver about the change. Could define a new target for
1275 * this?
1276 */
Brian Paul5d7b49f2005-11-19 23:12:20 +00001277 if (ctx->FragmentProgram._Current != prev && ctx->Driver.BindProgram) {
1278 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
Briana90046f2006-12-15 10:07:26 -07001279 (struct gl_program *) ctx->FragmentProgram._Current);
Brian Paul5d7b49f2005-11-19 23:12:20 +00001280 }
Keith Whitwellce721142005-07-11 10:10:38 +00001281}
1282
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001283
1284void _mesa_TexEnvProgramCacheInit( GLcontext *ctx )
1285{
Keith Whitwell8065c122006-05-22 16:09:27 +00001286 ctx->Texture.env_fp_cache.ctx = ctx;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001287 ctx->Texture.env_fp_cache.size = 17;
1288 ctx->Texture.env_fp_cache.n_items = 0;
1289 ctx->Texture.env_fp_cache.items = (struct texenvprog_cache_item **)
1290 _mesa_calloc(ctx->Texture.env_fp_cache.size *
1291 sizeof(struct texenvprog_cache_item));
1292}
1293
1294
Keith Whitwellce721142005-07-11 10:10:38 +00001295void _mesa_TexEnvProgramCacheDestroy( GLcontext *ctx )
1296{
Keith Whitwell8065c122006-05-22 16:09:27 +00001297 clear_cache(&ctx->Texture.env_fp_cache);
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001298 _mesa_free(ctx->Texture.env_fp_cache.items);
Keith Whitwellce721142005-07-11 10:10:38 +00001299}