blob: 2614440a747a6edc398c108fe45218c2136fb3f5 [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"
Brian83fad682007-09-25 15:20:04 -060035#include "shader/programopt.h"
Keith Whitwell15e75e02005-04-29 15:11:39 +000036#include "texenvprogram.h"
37
Brian Paule998c342006-10-29 21:17:18 +000038/**
Briane69943e2007-10-23 10:23:01 -060039 * This MAX is probably a bit generous, but that's OK. There can be
40 * up to four instructions per texture unit (TEX + 3 for combine),
41 * then there's fog and specular add.
Brian Paule998c342006-10-29 21:17:18 +000042 */
Briane69943e2007-10-23 10:23:01 -060043#define MAX_INSTRUCTIONS ((MAX_TEXTURE_UNITS * 4) + 12)
Keith Whitwell15e75e02005-04-29 15:11:39 +000044
Keith Whitwell269e3892005-05-12 10:28:43 +000045#define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
Keith Whitwell15e75e02005-04-29 15:11:39 +000046
Keith Whitwellce721142005-07-11 10:10:38 +000047struct mode_opt {
Brian Paul5d7b49f2005-11-19 23:12:20 +000048 GLuint Source:4;
49 GLuint Operand:3;
Keith Whitwellce721142005-07-11 10:10:38 +000050};
51
52struct state_key {
Brian Paul5d7b49f2005-11-19 23:12:20 +000053 GLbitfield enabled_units;
54 GLuint separate_specular:1;
55 GLuint fog_enabled:1;
56 GLuint fog_mode:2;
Keith Whitwellce721142005-07-11 10:10:38 +000057
58 struct {
Brian Paul5d7b49f2005-11-19 23:12:20 +000059 GLuint enabled:1;
60 GLuint source_index:3; /* one of TEXTURE_1D/2D/3D/CUBE/RECT_INDEX */
61 GLuint ScaleShiftRGB:2;
62 GLuint ScaleShiftA:2;
Keith Whitwellce721142005-07-11 10:10:38 +000063
Brian Paul5d7b49f2005-11-19 23:12:20 +000064 GLuint NumArgsRGB:2;
65 GLuint ModeRGB:4;
Keith Whitwellce721142005-07-11 10:10:38 +000066 struct mode_opt OptRGB[3];
67
Brian Paul5d7b49f2005-11-19 23:12:20 +000068 GLuint NumArgsA:2;
69 GLuint ModeA:4;
Keith Whitwellce721142005-07-11 10:10:38 +000070 struct mode_opt OptA[3];
71 } unit[8];
72};
73
74#define FOG_LINEAR 0
75#define FOG_EXP 1
76#define FOG_EXP2 2
77#define FOG_UNKNOWN 3
78
79static GLuint translate_fog_mode( GLenum mode )
80{
81 switch (mode) {
82 case GL_LINEAR: return FOG_LINEAR;
83 case GL_EXP: return FOG_EXP;
84 case GL_EXP2: return FOG_EXP2;
85 default: return FOG_UNKNOWN;
86 }
87}
88
89#define OPR_SRC_COLOR 0
90#define OPR_ONE_MINUS_SRC_COLOR 1
91#define OPR_SRC_ALPHA 2
92#define OPR_ONE_MINUS_SRC_ALPHA 3
93#define OPR_ZERO 4
94#define OPR_ONE 5
95#define OPR_UNKNOWN 7
96
97static GLuint translate_operand( GLenum operand )
98{
99 switch (operand) {
100 case GL_SRC_COLOR: return OPR_SRC_COLOR;
101 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
102 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
103 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
104 case GL_ZERO: return OPR_ZERO;
105 case GL_ONE: return OPR_ONE;
106 default: return OPR_UNKNOWN;
107 }
108}
109
110#define SRC_TEXTURE 0
111#define SRC_TEXTURE0 1
112#define SRC_TEXTURE1 2
113#define SRC_TEXTURE2 3
114#define SRC_TEXTURE3 4
115#define SRC_TEXTURE4 5
116#define SRC_TEXTURE5 6
117#define SRC_TEXTURE6 7
118#define SRC_TEXTURE7 8
119#define SRC_CONSTANT 9
120#define SRC_PRIMARY_COLOR 10
121#define SRC_PREVIOUS 11
122#define SRC_UNKNOWN 15
123
124static GLuint translate_source( GLenum src )
125{
126 switch (src) {
127 case GL_TEXTURE: return SRC_TEXTURE;
128 case GL_TEXTURE0:
129 case GL_TEXTURE1:
130 case GL_TEXTURE2:
131 case GL_TEXTURE3:
132 case GL_TEXTURE4:
133 case GL_TEXTURE5:
134 case GL_TEXTURE6:
135 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
136 case GL_CONSTANT: return SRC_CONSTANT;
137 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
138 case GL_PREVIOUS: return SRC_PREVIOUS;
139 default: return SRC_UNKNOWN;
140 }
141}
142
143#define MODE_REPLACE 0
144#define MODE_MODULATE 1
145#define MODE_ADD 2
146#define MODE_ADD_SIGNED 3
147#define MODE_INTERPOLATE 4
148#define MODE_SUBTRACT 5
149#define MODE_DOT3_RGB 6
150#define MODE_DOT3_RGB_EXT 7
151#define MODE_DOT3_RGBA 8
152#define MODE_DOT3_RGBA_EXT 9
153#define MODE_MODULATE_ADD_ATI 10
154#define MODE_MODULATE_SIGNED_ADD_ATI 11
155#define MODE_MODULATE_SUBTRACT_ATI 12
156#define MODE_UNKNOWN 15
157
158static GLuint translate_mode( GLenum mode )
159{
160 switch (mode) {
161 case GL_REPLACE: return MODE_REPLACE;
162 case GL_MODULATE: return MODE_MODULATE;
163 case GL_ADD: return MODE_ADD;
164 case GL_ADD_SIGNED: return MODE_ADD_SIGNED;
165 case GL_INTERPOLATE: return MODE_INTERPOLATE;
166 case GL_SUBTRACT: return MODE_SUBTRACT;
167 case GL_DOT3_RGB: return MODE_DOT3_RGB;
168 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
169 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
170 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
171 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
172 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
173 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
174 default: return MODE_UNKNOWN;
175 }
176}
177
178#define TEXTURE_UNKNOWN_INDEX 7
Brian Paule00ac112005-09-15 05:00:45 +0000179static GLuint translate_tex_src_bit( GLbitfield bit )
Keith Whitwellce721142005-07-11 10:10:38 +0000180{
181 switch (bit) {
182 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
183 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
184 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
185 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
186 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
187 default: return TEXTURE_UNKNOWN_INDEX;
188 }
189}
190
Brian Paul22db5352005-11-19 23:45:10 +0000191/**
192 * Examine current texture environment state and generate a unique
193 * key to identify it.
194 */
Keith Whitwell8065c122006-05-22 16:09:27 +0000195static void make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000196{
Keith Whitwellce721142005-07-11 10:10:38 +0000197 GLuint i, j;
198
Keith Whitwell8065c122006-05-22 16:09:27 +0000199 memset(key, 0, sizeof(*key));
200
Keith Whitwellce721142005-07-11 10:10:38 +0000201 for (i=0;i<MAX_TEXTURE_UNITS;i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000202 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Keith Whitwellce721142005-07-11 10:10:38 +0000203
204 if (!texUnit->_ReallyEnabled)
205 continue;
206
207 key->unit[i].enabled = 1;
208 key->enabled_units |= (1<<i);
209
210 key->unit[i].source_index =
211 translate_tex_src_bit(texUnit->_ReallyEnabled);
212
213 key->unit[i].NumArgsRGB = texUnit->_CurrentCombine->_NumArgsRGB;
214 key->unit[i].NumArgsA = texUnit->_CurrentCombine->_NumArgsA;
215
216 key->unit[i].ModeRGB =
217 translate_mode(texUnit->_CurrentCombine->ModeRGB);
218 key->unit[i].ModeA =
219 translate_mode(texUnit->_CurrentCombine->ModeA);
220
221 key->unit[i].ScaleShiftRGB = texUnit->_CurrentCombine->ScaleShiftRGB;
Keith Whitwell64da1612006-05-22 14:30:58 +0000222 key->unit[i].ScaleShiftA = texUnit->_CurrentCombine->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000223
224 for (j=0;j<3;j++) {
225 key->unit[i].OptRGB[j].Operand =
226 translate_operand(texUnit->_CurrentCombine->OperandRGB[j]);
227 key->unit[i].OptA[j].Operand =
228 translate_operand(texUnit->_CurrentCombine->OperandA[j]);
229 key->unit[i].OptRGB[j].Source =
230 translate_source(texUnit->_CurrentCombine->SourceRGB[j]);
231 key->unit[i].OptA[j].Source =
232 translate_source(texUnit->_CurrentCombine->SourceA[j]);
233 }
234 }
235
236 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
237 key->separate_specular = 1;
238
239 if (ctx->Fog.Enabled) {
240 key->fog_enabled = 1;
241 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
242 }
Keith Whitwellce721142005-07-11 10:10:38 +0000243}
244
Keith Whitwell15e75e02005-04-29 15:11:39 +0000245/* Use uregs to represent registers internally, translate to Mesa's
246 * expected formats on emit.
247 *
248 * NOTE: These are passed by value extensively in this file rather
249 * than as usual by pointer reference. If this disturbs you, try
250 * remembering they are just 32bits in size.
251 *
252 * GCC is smart enough to deal with these dword-sized structures in
253 * much the same way as if I had defined them as dwords and was using
254 * macros to access and set the fields. This is much nicer and easier
255 * to evolve.
256 */
257struct ureg {
258 GLuint file:4;
259 GLuint idx:8;
260 GLuint negatebase:1;
261 GLuint abs:1;
262 GLuint negateabs:1;
263 GLuint swz:12;
264 GLuint pad:5;
265};
266
Brian Paul13abf912006-04-13 19:17:13 +0000267static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000268 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000269 ~0,
270 0,
271 0,
272 0,
273 0,
274 0
275};
276
Keith Whitwell15e75e02005-04-29 15:11:39 +0000277
Keith Whitwell15e75e02005-04-29 15:11:39 +0000278/* State used to build the fragment program:
279 */
280struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000281 struct gl_fragment_program *program;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000282 GLcontext *ctx;
Keith Whitwellce721142005-07-11 10:10:38 +0000283 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000284
Brian Paulc8d17412005-12-14 03:06:16 +0000285 GLbitfield alu_temps; /* Track texture indirections, see spec. */
286 GLbitfield temps_output; /* Track texture indirections, see spec. */
287 GLbitfield temp_in_use; /* Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000288 GLboolean error;
289
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000290 struct ureg src_texture[MAX_TEXTURE_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000291 /* Reg containing each texture unit's sampled texture color,
292 * else undef.
293 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000294
295 struct ureg src_previous; /* Reg containing color from previous
296 * stage. May need to be decl'd.
297 */
298
299 GLuint last_tex_stage; /* Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000300
301 struct ureg half;
302 struct ureg one;
303 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000304};
305
306
307
308static struct ureg make_ureg(GLuint file, GLuint idx)
309{
310 struct ureg reg;
311 reg.file = file;
312 reg.idx = idx;
313 reg.negatebase = 0;
314 reg.abs = 0;
315 reg.negateabs = 0;
316 reg.swz = SWIZZLE_NOOP;
317 reg.pad = 0;
318 return reg;
319}
320
321static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
322{
323 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
324 GET_SWZ(reg.swz, y),
325 GET_SWZ(reg.swz, z),
326 GET_SWZ(reg.swz, w));
327
328 return reg;
329}
330
331static struct ureg swizzle1( struct ureg reg, int x )
332{
333 return swizzle(reg, x, x, x, x);
334}
335
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000336static struct ureg negate( struct ureg reg )
337{
338 reg.negatebase ^= 1;
339 return reg;
340}
341
Keith Whitwell15e75e02005-04-29 15:11:39 +0000342static GLboolean is_undef( struct ureg reg )
343{
Alan Hourihane8d972652006-08-10 13:12:00 +0000344 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000345}
346
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000347
Keith Whitwell15e75e02005-04-29 15:11:39 +0000348static struct ureg get_temp( struct texenv_fragment_program *p )
349{
Brian Paul13abf912006-04-13 19:17:13 +0000350 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000351
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000352 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000353 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000354 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000355
356 /* Then any unused temporary:
357 */
358 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000359 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000360
Keith Whitwell15e75e02005-04-29 15:11:39 +0000361 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000362 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000363 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000364 }
365
Brian Paul13abf912006-04-13 19:17:13 +0000366 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000367 p->program->Base.NumTemporaries = bit;
368
Keith Whitwell93cd9232005-05-11 08:30:23 +0000369 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000370 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
371}
372
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000373static struct ureg get_tex_temp( struct texenv_fragment_program *p )
374{
375 int bit;
376
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000377 /* First try to find availble temp not previously used (to avoid
378 * starting a new texture indirection). According to the spec, the
379 * ~p->temps_output isn't necessary, but will keep it there for
380 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000381 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000382 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000383
384 /* Then any unused temporary:
385 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000386 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000387 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000388
389 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000390 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000391 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000392 }
393
Brian Paul13abf912006-04-13 19:17:13 +0000394 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000395 p->program->Base.NumTemporaries = bit;
396
Keith Whitwell93cd9232005-05-11 08:30:23 +0000397 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000398 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
399}
400
Keith Whitwell15e75e02005-04-29 15:11:39 +0000401
402static void release_temps( struct texenv_fragment_program *p )
403{
Brian Paul05051032005-11-01 04:36:33 +0000404 GLuint max_temp = p->ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000405
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000406 /* KW: To support tex_env_crossbar, don't release the registers in
407 * temps_output.
408 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000409 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000410 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000411 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000412 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000413}
414
415
Brian9fe3e2e2007-02-23 11:44:14 -0700416static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000417 GLint s0,
418 GLint s1,
419 GLint s2,
420 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700421 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000422{
Brian9fe3e2e2007-02-23 11:44:14 -0700423 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000424 GLuint idx;
425 tokens[0] = s0;
426 tokens[1] = s1;
427 tokens[2] = s2;
428 tokens[3] = s3;
429 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000430 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000431 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000432}
433
Keith Whitwell47b29f52005-05-04 11:44:44 +0000434
Brian9fe3e2e2007-02-23 11:44:14 -0700435#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
436#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
437#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
438#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000439
440
441static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
442{
Brian Paulde997602005-11-12 17:53:14 +0000443 p->program->Base.InputsRead |= (1 << input);
Keith Whitwell47b29f52005-05-04 11:44:44 +0000444 return make_ureg(PROGRAM_INPUT, input);
445}
446
447
Brian Paul7e807512005-11-05 17:10:45 +0000448static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000449 struct ureg ureg )
450{
451 reg->File = ureg.file;
452 reg->Index = ureg.idx;
453 reg->Swizzle = ureg.swz;
Keith Whitwellf2802c42005-10-07 09:55:26 +0000454 reg->NegateBase = ureg.negatebase ? 0xf : 0x0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000455 reg->Abs = ureg.abs;
456 reg->NegateAbs = ureg.negateabs;
457}
458
Brian Paul7e807512005-11-05 17:10:45 +0000459static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000460 struct ureg ureg, GLuint mask )
461{
462 dst->File = ureg.file;
463 dst->Index = ureg.idx;
464 dst->WriteMask = mask;
465 dst->CondMask = 0;
466 dst->CondSwizzle = 0;
467}
468
Brian Paul7e807512005-11-05 17:10:45 +0000469static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000470emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000471 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000472 struct ureg dest,
473 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000474 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000475 struct ureg src0,
476 struct ureg src1,
477 struct ureg src2 )
478{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000479 GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000480 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000481
Briane69943e2007-10-23 10:23:01 -0600482 assert(nr < MAX_INSTRUCTIONS);
483
Brian Pauld6272e02006-10-29 18:03:16 +0000484 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000485 inst->Opcode = op;
486
Keith Whitwell47b29f52005-05-04 11:44:44 +0000487 emit_arg( &inst->SrcReg[0], src0 );
488 emit_arg( &inst->SrcReg[1], src1 );
489 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000490
Brian Paule31ac052005-11-20 17:52:40 +0000491 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000492
493 emit_dst( &inst->DstReg, dest, mask );
494
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000495 /* Accounting for indirection tracking:
496 */
497 if (dest.file == PROGRAM_TEMPORARY)
498 p->temps_output |= 1 << dest.idx;
499
Keith Whitwell15e75e02005-04-29 15:11:39 +0000500 return inst;
501}
502
503
504static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000505 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000506 struct ureg dest,
507 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000508 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000509 struct ureg src0,
510 struct ureg src1,
511 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000512{
513 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
514
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000515 /* Accounting for indirection tracking:
516 */
517 if (src0.file == PROGRAM_TEMPORARY)
518 p->alu_temps |= 1 << src0.idx;
519
520 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
521 p->alu_temps |= 1 << src1.idx;
522
523 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
524 p->alu_temps |= 1 << src2.idx;
525
526 if (dest.file == PROGRAM_TEMPORARY)
527 p->alu_temps |= 1 << dest.idx;
528
Brian21f99792007-01-09 11:00:21 -0700529 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000530 return dest;
531}
532
533static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000534 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000535 struct ureg dest,
536 GLuint destmask,
537 GLuint tex_unit,
538 GLuint tex_idx,
539 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000540{
Brian Paul7e807512005-11-05 17:10:45 +0000541 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000542 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000543 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000544 coord, /* arg 0? */
545 undef,
546 undef);
547
Brian Paul7e807512005-11-05 17:10:45 +0000548 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000549 inst->TexSrcUnit = tex_unit;
550
Brian21f99792007-01-09 11:00:21 -0700551 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000552
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000553 /* Is this a texture indirection?
554 */
555 if ((coord.file == PROGRAM_TEMPORARY &&
556 (p->temps_output & (1<<coord.idx))) ||
557 (dest.file == PROGRAM_TEMPORARY &&
558 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700559 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000560 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000561 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000562 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000563 }
564
565 return dest;
566}
567
568
Keith Whitwell47b29f52005-05-04 11:44:44 +0000569static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000570 GLfloat s0,
571 GLfloat s1,
572 GLfloat s2,
573 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000574{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000575 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700576 GLuint idx, swizzle;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000577 values[0] = s0;
578 values[1] = s1;
579 values[2] = s2;
580 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700581 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
582 &swizzle );
583 ASSERT(swizzle == SWIZZLE_NOOP);
Keith Whitwell47b29f52005-05-04 11:44:44 +0000584 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000585}
586
Keith Whitwelle4902422005-05-11 15:16:35 +0000587#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000588#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
589#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
590#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000591
592
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000593static struct ureg get_one( struct texenv_fragment_program *p )
594{
595 if (is_undef(p->one))
596 p->one = register_scalar_const(p, 1.0);
597 return p->one;
598}
599
600static struct ureg get_half( struct texenv_fragment_program *p )
601{
602 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000603 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000604 return p->half;
605}
606
607static struct ureg get_zero( struct texenv_fragment_program *p )
608{
609 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000610 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000611 return p->zero;
612}
613
Keith Whitwell15e75e02005-04-29 15:11:39 +0000614
Keith Whitwell15e75e02005-04-29 15:11:39 +0000615static void program_error( struct texenv_fragment_program *p, const char *msg )
616{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000617 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000618 p->error = 1;
619}
620
Keith Whitwell15e75e02005-04-29 15:11:39 +0000621static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000622 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000623{
624 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000625 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000626 assert(!is_undef(p->src_texture[unit]));
627 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000628
Keith Whitwellce721142005-07-11 10:10:38 +0000629 case SRC_TEXTURE0:
630 case SRC_TEXTURE1:
631 case SRC_TEXTURE2:
632 case SRC_TEXTURE3:
633 case SRC_TEXTURE4:
634 case SRC_TEXTURE5:
635 case SRC_TEXTURE6:
636 case SRC_TEXTURE7:
637 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
638 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000639
Keith Whitwellce721142005-07-11 10:10:38 +0000640 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000641 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000642
Keith Whitwellce721142005-07-11 10:10:38 +0000643 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000644 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000645
Keith Whitwellce721142005-07-11 10:10:38 +0000646 case SRC_PREVIOUS:
647 default:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000648 if (is_undef(p->src_previous))
649 return register_input(p, FRAG_ATTRIB_COL0);
650 else
651 return p->src_previous;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000652 }
653}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000654
Keith Whitwell15e75e02005-04-29 15:11:39 +0000655static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000656 GLuint mask,
657 GLuint unit,
658 GLuint source,
659 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000660{
661 struct ureg arg, src, one;
662
663 src = get_source(p, source, unit);
664
665 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000666 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000667 /* Get unused tmp,
668 * Emit tmp = 1.0 - arg.xyzw
669 */
670 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000671 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000672 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000673
Keith Whitwellce721142005-07-11 10:10:38 +0000674 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000675 if (mask == WRITEMASK_W)
676 return src;
677 else
Brian Paul22db5352005-11-19 23:45:10 +0000678 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000679 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000680 /* Get unused tmp,
681 * Emit tmp = 1.0 - arg.wwww
682 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000683 arg = get_temp(p);
684 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000685 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000686 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000687 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000688 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000689 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000690 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000691 case OPR_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000692 default:
693 return src;
694 }
695}
696
Keith Whitwellce721142005-07-11 10:10:38 +0000697static GLboolean args_match( struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000698{
Brian Paul22db5352005-11-19 23:45:10 +0000699 GLuint i, nr = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000700
701 for (i = 0 ; i < nr ; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000702 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000703 return GL_FALSE;
704
Keith Whitwellce721142005-07-11 10:10:38 +0000705 switch(key->unit[unit].OptA[i].Operand) {
706 case OPR_SRC_ALPHA:
707 switch(key->unit[unit].OptRGB[i].Operand) {
708 case OPR_SRC_COLOR:
709 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000710 break;
711 default:
712 return GL_FALSE;
713 }
714 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000715 case OPR_ONE_MINUS_SRC_ALPHA:
716 switch(key->unit[unit].OptRGB[i].Operand) {
717 case OPR_ONE_MINUS_SRC_COLOR:
718 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000719 break;
720 default:
721 return GL_FALSE;
722 }
723 break;
724 default:
725 return GL_FALSE; /* impossible */
726 }
727 }
728
729 return GL_TRUE;
730}
731
Keith Whitwell15e75e02005-04-29 15:11:39 +0000732static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000733 struct ureg dest,
734 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000735 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000736 GLuint unit,
737 GLuint nr,
738 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +0000739 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000740{
Keith Whitwell15e75e02005-04-29 15:11:39 +0000741 struct ureg src[3];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000742 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +0000743 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000744
Brian Paul00630842005-12-12 15:27:55 +0000745 tmp = undef; /* silence warning (bug 5318) */
746
Keith Whitwell15e75e02005-04-29 15:11:39 +0000747 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +0000748 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000749
750 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +0000751 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000752 if (mask == WRITEMASK_XYZW && !saturate)
753 return src[0];
754 else
Brian Paul7e807512005-11-05 17:10:45 +0000755 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000756 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +0000757 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000758 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000759 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +0000760 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000761 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000762 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000763 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000764 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +0000765 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000766 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +0000767 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +0000768 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
769 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000770 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +0000771 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000772 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
773 */
Brian Paul7e807512005-11-05 17:10:45 +0000774 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000775
Keith Whitwellce721142005-07-11 10:10:38 +0000776 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +0000777 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000778
Keith Whitwellce721142005-07-11 10:10:38 +0000779 case MODE_DOT3_RGBA:
780 case MODE_DOT3_RGBA_EXT:
781 case MODE_DOT3_RGB_EXT:
782 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000783 struct ureg tmp0 = get_temp( p );
784 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +0000785 struct ureg neg1 = register_scalar_const(p, -1);
786 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000787
788 /* tmp0 = 2*src0 - 1
789 * tmp1 = 2*src1 - 1
790 *
791 * dst = tmp0 dot3 tmp1
792 */
Brian Paul7e807512005-11-05 17:10:45 +0000793 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000794 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000795
Brian Paulaa206952005-09-16 18:14:24 +0000796 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000797 tmp1 = tmp0;
798 else
Brian Paul7e807512005-11-05 17:10:45 +0000799 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000800 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +0000801 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000802 return dest;
803 }
Keith Whitwellce721142005-07-11 10:10:38 +0000804 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000805 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +0000806 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000807 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +0000808 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000809 /* Arg0 * Arg2 + Arg1 - 0.5 */
810 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000811 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +0000812 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
813 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000814 return dest;
815 }
Keith Whitwellce721142005-07-11 10:10:38 +0000816 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000817 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +0000818 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000819 return dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000820 default:
821 return src[0];
822 }
823}
824
Keith Whitwell15e75e02005-04-29 15:11:39 +0000825
Brian Paul22db5352005-11-19 23:45:10 +0000826/**
827 * Generate instructions for one texture unit's env/combiner mode.
828 */
829static struct ureg
830emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000831{
Keith Whitwellce721142005-07-11 10:10:38 +0000832 struct state_key *key = p->state;
Brian Paul22db5352005-11-19 23:45:10 +0000833 GLboolean saturate = (unit < p->last_tex_stage);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000834 GLuint rgb_shift, alpha_shift;
835 struct ureg out, shift;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000836 struct ureg dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000837
Keith Whitwellce721142005-07-11 10:10:38 +0000838 if (!key->unit[unit].enabled) {
839 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000840 }
Keith Whitwellce721142005-07-11 10:10:38 +0000841
842 switch (key->unit[unit].ModeRGB) {
843 case MODE_DOT3_RGB_EXT:
844 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000845 rgb_shift = 0;
846 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000847 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000848 alpha_shift = 0;
849 rgb_shift = 0;
850 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000851 default:
Keith Whitwellce721142005-07-11 10:10:38 +0000852 rgb_shift = key->unit[unit].ScaleShiftRGB;
853 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000854 break;
855 }
Keith Whitwellce721142005-07-11 10:10:38 +0000856
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000857 /* If this is the very last calculation, emit direct to output reg:
858 */
Keith Whitwellce721142005-07-11 10:10:38 +0000859 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000860 unit != p->last_tex_stage ||
861 alpha_shift ||
862 rgb_shift)
863 dest = get_temp( p );
864 else
Brian Paul90ebb582005-11-02 18:06:12 +0000865 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLR);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000866
867 /* Emit the RGB and A combine ops
868 */
Keith Whitwellce721142005-07-11 10:10:38 +0000869 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
870 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000871 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
872 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000873 key->unit[unit].NumArgsRGB,
874 key->unit[unit].ModeRGB,
875 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000876 }
Keith Whitwellce721142005-07-11 10:10:38 +0000877 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +0200878 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000879
880 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
881 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000882 key->unit[unit].NumArgsRGB,
883 key->unit[unit].ModeRGB,
884 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000885 }
886 else {
887 /* Need to do something to stop from re-emitting identical
888 * argument calculations here:
889 */
890 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
891 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000892 key->unit[unit].NumArgsRGB,
893 key->unit[unit].ModeRGB,
894 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000895 out = emit_combine( p, dest, WRITEMASK_W, saturate,
896 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000897 key->unit[unit].NumArgsA,
898 key->unit[unit].ModeA,
899 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000900 }
901
902 /* Deal with the final shift:
903 */
904 if (alpha_shift || rgb_shift) {
905 if (rgb_shift == alpha_shift) {
Keith Whitwelle4902422005-05-11 15:16:35 +0000906 shift = register_scalar_const(p, 1<<rgb_shift);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000907 }
908 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000909 shift = register_const4f(p,
910 1<<rgb_shift,
911 1<<rgb_shift,
912 1<<rgb_shift,
913 1<<alpha_shift);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000914 }
Brian Paul7e807512005-11-05 17:10:45 +0000915 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000916 saturate, out, shift, undef );
917 }
918 else
919 return out;
920}
921
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000922
Brian Paul22db5352005-11-19 23:45:10 +0000923/**
924 * Generate instruction for getting a texture source term.
925 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000926static void load_texture( struct texenv_fragment_program *p, GLuint unit )
927{
928 if (is_undef(p->src_texture[unit])) {
Keith Whitwellce721142005-07-11 10:10:38 +0000929 GLuint dim = p->state->unit[unit].source_index;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000930 struct ureg texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
931 struct ureg tmp = get_tex_temp( p );
932
Brian Paulbfb5ea32005-07-19 18:20:04 +0000933 if (dim == TEXTURE_UNKNOWN_INDEX)
934 program_error(p, "TexSrcBit");
Keith Whitwellce721142005-07-11 10:10:38 +0000935
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000936 /* TODO: Use D0_MASK_XY where possible.
937 */
Aapo Tahkolab8915342006-03-28 10:26:34 +0000938 if (p->state->unit[unit].enabled)
939 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
940 tmp, WRITEMASK_XYZW,
941 unit, dim, texcoord );
942 else
943 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000944 }
945}
946
Keith Whitwell241b6b72005-05-23 09:50:34 +0000947static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000948 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000949{
950 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000951 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000952 load_texture(p, unit);
953 break;
954
Keith Whitwellce721142005-07-11 10:10:38 +0000955 case SRC_TEXTURE0:
956 case SRC_TEXTURE1:
957 case SRC_TEXTURE2:
958 case SRC_TEXTURE3:
959 case SRC_TEXTURE4:
960 case SRC_TEXTURE5:
961 case SRC_TEXTURE6:
962 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +0000963 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000964 break;
965
966 default:
967 break;
968 }
Keith Whitwell241b6b72005-05-23 09:50:34 +0000969
970 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000971}
972
Brian Paul22db5352005-11-19 23:45:10 +0000973
974/**
975 * Generate instructions for loading all texture source terms.
976 */
977static GLboolean
978load_texunit_sources( struct texenv_fragment_program *p, int unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000979{
Keith Whitwellce721142005-07-11 10:10:38 +0000980 struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +0000981 GLuint i;
982
983 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
984 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000985 }
Aapo Tahkolab8915342006-03-28 10:26:34 +0000986
987 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
988 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
989 }
990
Keith Whitwell241b6b72005-05-23 09:50:34 +0000991 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000992}
993
Brian Paul22db5352005-11-19 23:45:10 +0000994
995/**
996 * Generate a new fragment program which implements the context's
997 * current texture env/combine mode.
998 */
999static void
Brian Paule998c342006-10-29 21:17:18 +00001000create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001001 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001002{
Brian Paule998c342006-10-29 21:17:18 +00001003 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001004 struct texenv_fragment_program p;
1005 GLuint unit;
1006 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001007
Keith Whitwelle4902422005-05-11 15:16:35 +00001008 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +00001009 p.ctx = ctx;
Keith Whitwellce721142005-07-11 10:10:38 +00001010 p.state = key;
1011 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001012
Brian Paule998c342006-10-29 21:17:18 +00001013 /* During code generation, use locally-allocated instruction buffer,
1014 * then alloc dynamic storage below.
1015 */
1016 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001017 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Brian21f99792007-01-09 11:00:21 -07001018 p.program->Base.NumTexIndirections = 1; /* correct? */
1019 p.program->Base.NumTexInstructions = 0;
1020 p.program->Base.NumAluInstructions = 0;
Brian1240eb22007-03-22 08:50:20 -06001021 p.program->Base.String = NULL;
Keith Whitwell9ca88152005-05-10 09:56:02 +00001022 p.program->Base.NumInstructions =
Brian Paule998c342006-10-29 21:17:18 +00001023 p.program->Base.NumTemporaries =
1024 p.program->Base.NumParameters =
1025 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001026 p.program->Base.Parameters = _mesa_new_parameter_list();
Keith Whitwelldbeea252005-05-10 13:57:50 +00001027
Brian Paulde997602005-11-12 17:53:14 +00001028 p.program->Base.InputsRead = 0;
1029 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001030
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001031 for (unit = 0; unit < MAX_TEXTURE_UNITS; unit++)
1032 p.src_texture[unit] = undef;
1033
Keith Whitwell47b29f52005-05-04 11:44:44 +00001034 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001035 p.half = undef;
1036 p.zero = undef;
1037 p.one = undef;
1038
Keith Whitwell15e75e02005-04-29 15:11:39 +00001039 p.last_tex_stage = 0;
Keith Whitwell93cd9232005-05-11 08:30:23 +00001040 release_temps(&p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001041
Keith Whitwellce721142005-07-11 10:10:38 +00001042 if (key->enabled_units) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001043 /* First pass - to support texture_env_crossbar, first identify
1044 * all referenced texture sources and emit texld instructions
1045 * for each:
1046 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001047 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001048 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001049 load_texunit_sources( &p, unit );
1050 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001051 }
1052
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001053 /* Second pass - emit combine instructions to build final color:
1054 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001055 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001056 if (key->enabled_units & (1<<unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001057 p.src_previous = emit_texenv( &p, unit );
Keith Whitwell93cd9232005-05-11 08:30:23 +00001058 release_temps(&p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001059 }
1060 }
1061
Keith Whitwellce721142005-07-11 10:10:38 +00001062 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul90ebb582005-11-02 18:06:12 +00001063 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001064
Keith Whitwellce721142005-07-11 10:10:38 +00001065 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001066 /* Emit specular add.
1067 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001068 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001069 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1070 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001071 }
Brian Paulaa206952005-09-16 18:14:24 +00001072 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001073 /* Will wind up in here if no texture enabled or a couple of
1074 * other scenarios (GL_REPLACE for instance).
1075 */
Brian Paul7e807512005-11-05 17:10:45 +00001076 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001077 }
1078
Keith Whitwell47b29f52005-05-04 11:44:44 +00001079 /* Finish up:
1080 */
Brian Paul7e807512005-11-05 17:10:45 +00001081 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001082
Keith Whitwellce721142005-07-11 10:10:38 +00001083 if (key->fog_enabled) {
1084 /* Pull fog mode from GLcontext, the value in the state key is
1085 * a reduced value and not what is expected in FogOption
1086 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001087 p.program->FogOption = ctx->Fog.Mode;
Brian63be96b2007-09-18 19:29:26 -06001088 p.program->Base.InputsRead |= FRAG_BIT_FOGC; /* XXX new */
Keith Whitwellce721142005-07-11 10:10:38 +00001089 } else
Keith Whitwell276330b2005-05-09 17:58:13 +00001090 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001091
Brian21f99792007-01-09 11:00:21 -07001092 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001093 program_error(&p, "Exceeded max nr indirect texture lookups");
1094
Brian21f99792007-01-09 11:00:21 -07001095 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001096 program_error(&p, "Exceeded max TEX instructions");
1097
Brian21f99792007-01-09 11:00:21 -07001098 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001099 program_error(&p, "Exceeded max ALU instructions");
1100
Brian Paul5d7b49f2005-11-19 23:12:20 +00001101 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001102
Brian Paule998c342006-10-29 21:17:18 +00001103 /* Allocate final instruction array */
Brian3bf8d2a2007-09-25 15:18:51 -06001104 p.program->Base.Instructions
1105 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1106 if (!p.program->Base.Instructions) {
Brian Paule998c342006-10-29 21:17:18 +00001107 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1108 "generating tex env program");
1109 return;
1110 }
Brian3bf8d2a2007-09-25 15:18:51 -06001111 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1112 p.program->Base.NumInstructions);
1113
1114 if (p.program->FogOption) {
1115 _mesa_append_fog_code(ctx, p.program);
1116 p.program->FogOption = GL_NONE;
1117 }
1118
Brian Paule998c342006-10-29 21:17:18 +00001119
Keith Whitwelldbeea252005-05-10 13:57:50 +00001120 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001121 */
Brian Paule998c342006-10-29 21:17:18 +00001122 if (ctx->Driver.ProgramStringNotify) {
1123 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1124 &p.program->Base );
1125 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001126
Brian Paule998c342006-10-29 21:17:18 +00001127 if (DISASSEM) {
1128 _mesa_print_program(&p.program->Base);
1129 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001130 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001131}
1132
Brian Paul5d7b49f2005-11-19 23:12:20 +00001133
Brian Paul122629f2006-07-20 16:49:57 +00001134static struct gl_fragment_program *
Brian Pauld9736db2006-05-23 02:44:46 +00001135search_cache(const struct texenvprog_cache *cache,
1136 GLuint hash,
Brian Paule4cb9cd2006-05-30 22:15:24 +00001137 const void *key,
Brian Pauld9736db2006-05-23 02:44:46 +00001138 GLuint keysize)
Keith Whitwellce721142005-07-11 10:10:38 +00001139{
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001140 struct texenvprog_cache_item *c;
Keith Whitwellce721142005-07-11 10:10:38 +00001141
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001142 for (c = cache->items[hash % cache->size]; c; c = c->next) {
1143 if (c->hash == hash && memcmp(c->key, key, keysize) == 0)
Brian Paul122629f2006-07-20 16:49:57 +00001144 return (struct gl_fragment_program *) c->data;
Keith Whitwellce721142005-07-11 10:10:38 +00001145 }
1146
1147 return NULL;
1148}
1149
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001150static void rehash( struct texenvprog_cache *cache )
1151{
1152 struct texenvprog_cache_item **items;
1153 struct texenvprog_cache_item *c, *next;
1154 GLuint size, i;
1155
1156 size = cache->size * 3;
1157 items = (struct texenvprog_cache_item**) _mesa_malloc(size * sizeof(*items));
1158 _mesa_memset(items, 0, size * sizeof(*items));
1159
1160 for (i = 0; i < cache->size; i++)
1161 for (c = cache->items[i]; c; c = next) {
1162 next = c->next;
1163 c->next = items[c->hash % size];
1164 items[c->hash % size] = c;
1165 }
1166
1167 _mesa_free(cache->items);
1168 cache->items = items;
1169 cache->size = size;
1170}
1171
Keith Whitwell8065c122006-05-22 16:09:27 +00001172static void clear_cache( struct texenvprog_cache *cache )
1173{
1174 struct texenvprog_cache_item *c, *next;
1175 GLuint i;
1176
1177 for (i = 0; i < cache->size; i++) {
1178 for (c = cache->items[i]; c; c = next) {
1179 next = c->next;
1180 _mesa_free(c->key);
Brian Paul122629f2006-07-20 16:49:57 +00001181 cache->ctx->Driver.DeleteProgram(cache->ctx,
1182 (struct gl_program *) c->data);
Keith Whitwell8065c122006-05-22 16:09:27 +00001183 _mesa_free(c);
1184 }
1185 cache->items[i] = NULL;
1186 }
1187
1188
1189 cache->n_items = 0;
1190}
1191
1192
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001193static void cache_item( struct texenvprog_cache *cache,
Keith Whitwellce721142005-07-11 10:10:38 +00001194 GLuint hash,
Brian Paulb8f2f6f2006-05-23 01:55:31 +00001195 const struct state_key *key,
Keith Whitwellce721142005-07-11 10:10:38 +00001196 void *data )
1197{
Brian18d1fde2007-01-23 11:46:02 -07001198 struct texenvprog_cache_item *c
1199 = (struct texenvprog_cache_item *) MALLOC(sizeof(*c));
Keith Whitwellce721142005-07-11 10:10:38 +00001200 c->hash = hash;
Keith Whitwell8065c122006-05-22 16:09:27 +00001201
1202 c->key = _mesa_malloc(sizeof(*key));
1203 memcpy(c->key, key, sizeof(*key));
1204
Brian18d1fde2007-01-23 11:46:02 -07001205 c->data = (struct gl_fragment_program *) data;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001206
Keith Whitwell8065c122006-05-22 16:09:27 +00001207 if (cache->n_items > cache->size * 1.5) {
1208 if (cache->size < 1000)
1209 rehash(cache);
1210 else
1211 clear_cache(cache);
1212 }
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001213
Keith Whitwell8065c122006-05-22 16:09:27 +00001214 cache->n_items++;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001215 c->next = cache->items[hash % cache->size];
1216 cache->items[hash % cache->size] = c;
Keith Whitwellce721142005-07-11 10:10:38 +00001217}
1218
Brian Pauld9736db2006-05-23 02:44:46 +00001219static GLuint hash_key( const struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +00001220{
1221 GLuint *ikey = (GLuint *)key;
1222 GLuint hash = 0, i;
1223
Keith Whitwell8065c122006-05-22 16:09:27 +00001224 /* Make a slightly better attempt at a hash function:
Keith Whitwellce721142005-07-11 10:10:38 +00001225 */
Keith Whitwell8065c122006-05-22 16:09:27 +00001226 for (i = 0; i < sizeof(*key)/sizeof(*ikey); i++)
1227 {
1228 hash += ikey[i];
1229 hash += (hash << 10);
1230 hash ^= (hash >> 6);
1231 }
Keith Whitwellce721142005-07-11 10:10:38 +00001232
1233 return hash;
1234}
1235
Briana90046f2006-12-15 10:07:26 -07001236
1237/**
1238 * If _MaintainTexEnvProgram is set we'll generate a fragment program that
1239 * implements the current texture env/combine mode.
1240 * This function generates that program and puts it into effect.
1241 */
1242void
1243_mesa_UpdateTexEnvProgram( GLcontext *ctx )
Keith Whitwellce721142005-07-11 10:10:38 +00001244{
Keith Whitwell8065c122006-05-22 16:09:27 +00001245 struct state_key key;
Keith Whitwellce721142005-07-11 10:10:38 +00001246 GLuint hash;
Brian Paul122629f2006-07-20 16:49:57 +00001247 const struct gl_fragment_program *prev = ctx->FragmentProgram._Current;
Keith Whitwellce721142005-07-11 10:10:38 +00001248
Briana90046f2006-12-15 10:07:26 -07001249 ASSERT(ctx->FragmentProgram._MaintainTexEnvProgram);
1250
1251 /* If a conventional fragment program/shader isn't in effect... */
Brianefcfdbd2007-02-24 15:51:41 -07001252 if (!ctx->FragmentProgram._Enabled &&
Briand781cdc2007-10-02 16:55:21 -06001253 (!ctx->Shader.CurrentProgram ||
1254 !ctx->Shader.CurrentProgram->FragmentProgram) ) {
Keith Whitwell8065c122006-05-22 16:09:27 +00001255 make_state_key(ctx, &key);
1256 hash = hash_key(&key);
Keith Whitwellc3626a92005-11-01 17:25:49 +00001257
Brian Pauld9736db2006-05-23 02:44:46 +00001258 ctx->FragmentProgram._Current =
Briana90046f2006-12-15 10:07:26 -07001259 ctx->FragmentProgram._TexEnvProgram =
1260 search_cache(&ctx->Texture.env_fp_cache, hash, &key, sizeof(key));
Keith Whitwellce721142005-07-11 10:10:38 +00001261
Briana90046f2006-12-15 10:07:26 -07001262 if (!ctx->FragmentProgram._TexEnvProgram) {
1263 if (0)
1264 _mesa_printf("Building new texenv proggy for key %x\n", hash);
1265
1266 /* create new tex env program */
Brianefcfdbd2007-02-24 15:51:41 -07001267 ctx->FragmentProgram._Current =
1268 ctx->FragmentProgram._TexEnvProgram =
1269 (struct gl_fragment_program *)
Briana90046f2006-12-15 10:07:26 -07001270 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1271
1272 create_new_program(ctx, &key, ctx->FragmentProgram._TexEnvProgram);
1273
1274 cache_item(&ctx->Texture.env_fp_cache, hash, &key,
1275 ctx->FragmentProgram._TexEnvProgram);
Keith Whitwellc3626a92005-11-01 17:25:49 +00001276 }
Briana90046f2006-12-15 10:07:26 -07001277 else {
1278 if (0)
1279 _mesa_printf("Found existing texenv program for key %x\n", hash);
Keith Whitwellc3626a92005-11-01 17:25:49 +00001280 }
1281 }
1282 else {
1283 ctx->FragmentProgram._Current = ctx->FragmentProgram.Current;
Keith Whitwellce721142005-07-11 10:10:38 +00001284 }
Keith Whitwellc3626a92005-11-01 17:25:49 +00001285
1286 /* Tell the driver about the change. Could define a new target for
1287 * this?
1288 */
Brian Paul5d7b49f2005-11-19 23:12:20 +00001289 if (ctx->FragmentProgram._Current != prev && ctx->Driver.BindProgram) {
1290 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
Briana90046f2006-12-15 10:07:26 -07001291 (struct gl_program *) ctx->FragmentProgram._Current);
Brian Paul5d7b49f2005-11-19 23:12:20 +00001292 }
Keith Whitwellce721142005-07-11 10:10:38 +00001293}
1294
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001295
1296void _mesa_TexEnvProgramCacheInit( GLcontext *ctx )
1297{
Keith Whitwell8065c122006-05-22 16:09:27 +00001298 ctx->Texture.env_fp_cache.ctx = ctx;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001299 ctx->Texture.env_fp_cache.size = 17;
1300 ctx->Texture.env_fp_cache.n_items = 0;
1301 ctx->Texture.env_fp_cache.items = (struct texenvprog_cache_item **)
1302 _mesa_calloc(ctx->Texture.env_fp_cache.size *
1303 sizeof(struct texenvprog_cache_item));
1304}
1305
1306
Keith Whitwellce721142005-07-11 10:10:38 +00001307void _mesa_TexEnvProgramCacheDestroy( GLcontext *ctx )
1308{
Keith Whitwell8065c122006-05-22 16:09:27 +00001309 clear_cache(&ctx->Texture.env_fp_cache);
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001310 _mesa_free(ctx->Texture.env_fp_cache.items);
Keith Whitwellce721142005-07-11 10:10:38 +00001311}