blob: 066f3bd0bec1df855bbbebbd676d0e2cd0a6e977 [file] [log] [blame]
Keith Whitwell15e75e02005-04-29 15:11:39 +00001/**************************************************************************
2 *
Keith Whitwell32ef6e72008-09-20 08:26:11 -07003 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
Keith Whitwell15e75e02005-04-29 15:11:39 +00004 * All Rights Reserved.
Brian Paul6947f852009-01-23 17:32:09 -07005 * Copyright 2009 VMware, Inc. All Rights Reserved.
Keith Whitwell15e75e02005-04-29 15:11:39 +00006 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
Keith Whitwell15e75e02005-04-29 15:11:39 +000029#include "glheader.h"
30#include "macros.h"
31#include "enums.h"
Brian Paul5b5c9312008-05-07 18:51:44 -060032#include "shader/program.h"
Brianc223c6b2007-07-04 13:15:20 -060033#include "shader/prog_parameter.h"
Keith Whitwell32ef6e72008-09-20 08:26:11 -070034#include "shader/prog_cache.h"
Brianc223c6b2007-07-04 13:15:20 -060035#include "shader/prog_instruction.h"
36#include "shader/prog_print.h"
37#include "shader/prog_statevars.h"
Brianfb3c41f2007-09-25 15:20:04 -060038#include "shader/programopt.h"
Keith Whitwell15e75e02005-04-29 15:11:39 +000039#include "texenvprogram.h"
40
Brian Paul5b5c9312008-05-07 18:51:44 -060041
Brian Paul6947f852009-01-23 17:32:09 -070042#define MAX_TERMS 4
43
44
Brian Paule9b34882008-12-31 11:54:02 -070045/*
46 * Note on texture units:
47 *
48 * The number of texture units supported by fixed-function fragment
49 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
50 * That's because there's a one-to-one correspondence between texture
51 * coordinates and samplers in fixed-function processing.
52 *
53 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
54 * sets of texcoords, so is fixed-function fragment processing.
55 *
56 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
57 */
58
59
Brian Paul5b5c9312008-05-07 18:51:44 -060060struct texenvprog_cache_item
61{
62 GLuint hash;
63 void *key;
64 struct gl_fragment_program *data;
65 struct texenvprog_cache_item *next;
66};
67
68
Brian Paule998c342006-10-29 21:17:18 +000069/**
Brian2a8e9bb2007-10-23 10:24:53 -060070 * This MAX is probably a bit generous, but that's OK. There can be
71 * up to four instructions per texture unit (TEX + 3 for combine),
72 * then there's fog and specular add.
Brian Paule998c342006-10-29 21:17:18 +000073 */
Brian Paule9b34882008-12-31 11:54:02 -070074#define MAX_INSTRUCTIONS ((MAX_TEXTURE_COORD_UNITS * 4) + 12)
Keith Whitwell15e75e02005-04-29 15:11:39 +000075
Keith Whitwell269e3892005-05-12 10:28:43 +000076#define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
Keith Whitwell15e75e02005-04-29 15:11:39 +000077
Keith Whitwellce721142005-07-11 10:10:38 +000078struct mode_opt {
Brian Paul5d7b49f2005-11-19 23:12:20 +000079 GLuint Source:4;
80 GLuint Operand:3;
Keith Whitwellce721142005-07-11 10:10:38 +000081};
82
83struct state_key {
Brian Paul5d7b49f2005-11-19 23:12:20 +000084 GLbitfield enabled_units;
85 GLuint separate_specular:1;
86 GLuint fog_enabled:1;
87 GLuint fog_mode:2;
Keith Whitwellce721142005-07-11 10:10:38 +000088
89 struct {
Brian Paul5d7b49f2005-11-19 23:12:20 +000090 GLuint enabled:1;
91 GLuint source_index:3; /* one of TEXTURE_1D/2D/3D/CUBE/RECT_INDEX */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +020092 GLuint shadow:1;
Brian Paul5d7b49f2005-11-19 23:12:20 +000093 GLuint ScaleShiftRGB:2;
94 GLuint ScaleShiftA:2;
Keith Whitwellce721142005-07-11 10:10:38 +000095
Brian Paul6947f852009-01-23 17:32:09 -070096 GLuint NumArgsRGB:3;
Brian Paul5d7b49f2005-11-19 23:12:20 +000097 GLuint ModeRGB:4;
Brian Paul6947f852009-01-23 17:32:09 -070098 struct mode_opt OptRGB[MAX_TERMS];
Keith Whitwellce721142005-07-11 10:10:38 +000099
Brian Paul6947f852009-01-23 17:32:09 -0700100 GLuint NumArgsA:3;
Brian Paul5d7b49f2005-11-19 23:12:20 +0000101 GLuint ModeA:4;
Brian Paul6947f852009-01-23 17:32:09 -0700102 struct mode_opt OptA[MAX_TERMS];
Keith Whitwellce721142005-07-11 10:10:38 +0000103 } unit[8];
104};
105
106#define FOG_LINEAR 0
107#define FOG_EXP 1
108#define FOG_EXP2 2
109#define FOG_UNKNOWN 3
110
111static GLuint translate_fog_mode( GLenum mode )
112{
113 switch (mode) {
114 case GL_LINEAR: return FOG_LINEAR;
115 case GL_EXP: return FOG_EXP;
116 case GL_EXP2: return FOG_EXP2;
117 default: return FOG_UNKNOWN;
118 }
119}
120
121#define OPR_SRC_COLOR 0
122#define OPR_ONE_MINUS_SRC_COLOR 1
123#define OPR_SRC_ALPHA 2
124#define OPR_ONE_MINUS_SRC_ALPHA 3
125#define OPR_ZERO 4
126#define OPR_ONE 5
127#define OPR_UNKNOWN 7
128
129static GLuint translate_operand( GLenum operand )
130{
131 switch (operand) {
132 case GL_SRC_COLOR: return OPR_SRC_COLOR;
133 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
134 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
135 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
136 case GL_ZERO: return OPR_ZERO;
137 case GL_ONE: return OPR_ONE;
Brian Paul6947f852009-01-23 17:32:09 -0700138 default:
139 assert(0);
140 return OPR_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000141 }
142}
143
144#define SRC_TEXTURE 0
145#define SRC_TEXTURE0 1
146#define SRC_TEXTURE1 2
147#define SRC_TEXTURE2 3
148#define SRC_TEXTURE3 4
149#define SRC_TEXTURE4 5
150#define SRC_TEXTURE5 6
151#define SRC_TEXTURE6 7
152#define SRC_TEXTURE7 8
153#define SRC_CONSTANT 9
154#define SRC_PRIMARY_COLOR 10
155#define SRC_PREVIOUS 11
Brian Paul6947f852009-01-23 17:32:09 -0700156#define SRC_ZERO 12
Keith Whitwellce721142005-07-11 10:10:38 +0000157#define SRC_UNKNOWN 15
158
159static GLuint translate_source( GLenum src )
160{
161 switch (src) {
162 case GL_TEXTURE: return SRC_TEXTURE;
163 case GL_TEXTURE0:
164 case GL_TEXTURE1:
165 case GL_TEXTURE2:
166 case GL_TEXTURE3:
167 case GL_TEXTURE4:
168 case GL_TEXTURE5:
169 case GL_TEXTURE6:
170 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
171 case GL_CONSTANT: return SRC_CONSTANT;
172 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
173 case GL_PREVIOUS: return SRC_PREVIOUS;
Brian Paul6947f852009-01-23 17:32:09 -0700174 case GL_ZERO:
175 return SRC_ZERO;
176 default:
177 assert(0);
178 return SRC_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000179 }
180}
181
Brian Paul6947f852009-01-23 17:32:09 -0700182#define MODE_REPLACE 0 /* r = a0 */
183#define MODE_MODULATE 1 /* r = a0 * a1 */
184#define MODE_ADD 2 /* r = a0 + a1 */
185#define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
186#define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
187#define MODE_SUBTRACT 5 /* r = a0 - a1 */
188#define MODE_DOT3_RGB 6 /* r = a0 . a1 */
189#define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
190#define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
191#define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
192#define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
193#define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
194#define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
195#define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
196#define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
197#define MODE_UNKNOWN 15
Keith Whitwellce721142005-07-11 10:10:38 +0000198
Brian Paul6947f852009-01-23 17:32:09 -0700199/**
200 * Translate GL combiner state into a MODE_x value
201 */
202static GLuint translate_mode( GLenum envMode, GLenum mode )
Keith Whitwellce721142005-07-11 10:10:38 +0000203{
204 switch (mode) {
205 case GL_REPLACE: return MODE_REPLACE;
206 case GL_MODULATE: return MODE_MODULATE;
Brian Paul6947f852009-01-23 17:32:09 -0700207 case GL_ADD:
208 if (envMode == GL_COMBINE4_NV)
209 return MODE_ADD_PRODUCTS;
210 else
211 return MODE_ADD;
212 case GL_ADD_SIGNED:
213 if (envMode == GL_COMBINE4_NV)
214 return MODE_ADD_PRODUCTS_SIGNED;
215 else
216 return MODE_ADD_SIGNED;
Keith Whitwellce721142005-07-11 10:10:38 +0000217 case GL_INTERPOLATE: return MODE_INTERPOLATE;
218 case GL_SUBTRACT: return MODE_SUBTRACT;
219 case GL_DOT3_RGB: return MODE_DOT3_RGB;
220 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
221 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
222 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
223 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
224 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
225 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
Brian Paul6947f852009-01-23 17:32:09 -0700226 default:
227 assert(0);
228 return MODE_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000229 }
230}
231
232#define TEXTURE_UNKNOWN_INDEX 7
Brian Paule00ac112005-09-15 05:00:45 +0000233static GLuint translate_tex_src_bit( GLbitfield bit )
Keith Whitwellce721142005-07-11 10:10:38 +0000234{
235 switch (bit) {
236 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
237 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
238 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
239 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
240 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
Brian Paul6947f852009-01-23 17:32:09 -0700241 case TEXTURE_1D_ARRAY_BIT: return TEXTURE_1D_ARRAY_INDEX;
242 case TEXTURE_2D_ARRAY_BIT: return TEXTURE_2D_ARRAY_INDEX;
243 default:
244 assert(0);
245 return TEXTURE_UNKNOWN_INDEX;
Keith Whitwellce721142005-07-11 10:10:38 +0000246 }
247}
248
Brian Paul22db5352005-11-19 23:45:10 +0000249/**
250 * Examine current texture environment state and generate a unique
251 * key to identify it.
252 */
Keith Whitwell8065c122006-05-22 16:09:27 +0000253static void make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000254{
Keith Whitwellce721142005-07-11 10:10:38 +0000255 GLuint i, j;
256
Keith Whitwell8065c122006-05-22 16:09:27 +0000257 memset(key, 0, sizeof(*key));
258
Brian Paule9b34882008-12-31 11:54:02 -0700259 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000260 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800261 GLenum format;
Roland Scheideggerbf4a0fa2008-02-15 17:26:06 +0100262
263 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
Keith Whitwellce721142005-07-11 10:10:38 +0000264 continue;
265
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800266 format = texUnit->_Current->Image[0][texUnit->_Current->BaseLevel]->_BaseFormat;
267
Keith Whitwellce721142005-07-11 10:10:38 +0000268 key->unit[i].enabled = 1;
269 key->enabled_units |= (1<<i);
270
271 key->unit[i].source_index =
272 translate_tex_src_bit(texUnit->_ReallyEnabled);
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800273 key->unit[i].shadow = ((texUnit->_Current->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
274 ((format == GL_DEPTH_COMPONENT) ||
275 (format == GL_DEPTH_STENCIL_EXT)));
Keith Whitwellce721142005-07-11 10:10:38 +0000276
277 key->unit[i].NumArgsRGB = texUnit->_CurrentCombine->_NumArgsRGB;
278 key->unit[i].NumArgsA = texUnit->_CurrentCombine->_NumArgsA;
279
280 key->unit[i].ModeRGB =
Brian Paul6947f852009-01-23 17:32:09 -0700281 translate_mode(texUnit->EnvMode, texUnit->_CurrentCombine->ModeRGB);
Keith Whitwellce721142005-07-11 10:10:38 +0000282 key->unit[i].ModeA =
Brian Paul6947f852009-01-23 17:32:09 -0700283 translate_mode(texUnit->EnvMode, texUnit->_CurrentCombine->ModeA);
Keith Whitwellce721142005-07-11 10:10:38 +0000284
285 key->unit[i].ScaleShiftRGB = texUnit->_CurrentCombine->ScaleShiftRGB;
Keith Whitwell64da1612006-05-22 14:30:58 +0000286 key->unit[i].ScaleShiftA = texUnit->_CurrentCombine->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000287
Brian Paul6947f852009-01-23 17:32:09 -0700288 for (j = 0; j < MAX_TERMS; j++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000289 key->unit[i].OptRGB[j].Operand =
290 translate_operand(texUnit->_CurrentCombine->OperandRGB[j]);
291 key->unit[i].OptA[j].Operand =
292 translate_operand(texUnit->_CurrentCombine->OperandA[j]);
293 key->unit[i].OptRGB[j].Source =
294 translate_source(texUnit->_CurrentCombine->SourceRGB[j]);
295 key->unit[i].OptA[j].Source =
296 translate_source(texUnit->_CurrentCombine->SourceA[j]);
297 }
298 }
299
300 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
301 key->separate_specular = 1;
302
303 if (ctx->Fog.Enabled) {
304 key->fog_enabled = 1;
305 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
306 }
Keith Whitwellce721142005-07-11 10:10:38 +0000307}
308
Keith Whitwell15e75e02005-04-29 15:11:39 +0000309/* Use uregs to represent registers internally, translate to Mesa's
310 * expected formats on emit.
311 *
312 * NOTE: These are passed by value extensively in this file rather
313 * than as usual by pointer reference. If this disturbs you, try
314 * remembering they are just 32bits in size.
315 *
316 * GCC is smart enough to deal with these dword-sized structures in
317 * much the same way as if I had defined them as dwords and was using
318 * macros to access and set the fields. This is much nicer and easier
319 * to evolve.
320 */
321struct ureg {
322 GLuint file:4;
323 GLuint idx:8;
324 GLuint negatebase:1;
325 GLuint abs:1;
326 GLuint negateabs:1;
327 GLuint swz:12;
328 GLuint pad:5;
329};
330
Brian Paul13abf912006-04-13 19:17:13 +0000331static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000332 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000333 ~0,
334 0,
335 0,
336 0,
337 0,
338 0
339};
340
Keith Whitwell15e75e02005-04-29 15:11:39 +0000341
Keith Whitwell15e75e02005-04-29 15:11:39 +0000342/* State used to build the fragment program:
343 */
344struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000345 struct gl_fragment_program *program;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000346 GLcontext *ctx;
Keith Whitwellce721142005-07-11 10:10:38 +0000347 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000348
Brian Paulc8d17412005-12-14 03:06:16 +0000349 GLbitfield alu_temps; /* Track texture indirections, see spec. */
350 GLbitfield temps_output; /* Track texture indirections, see spec. */
351 GLbitfield temp_in_use; /* Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000352 GLboolean error;
353
Brian Paule9b34882008-12-31 11:54:02 -0700354 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000355 /* Reg containing each texture unit's sampled texture color,
356 * else undef.
357 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000358
359 struct ureg src_previous; /* Reg containing color from previous
360 * stage. May need to be decl'd.
361 */
362
363 GLuint last_tex_stage; /* Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000364
365 struct ureg half;
366 struct ureg one;
367 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000368};
369
370
371
372static struct ureg make_ureg(GLuint file, GLuint idx)
373{
374 struct ureg reg;
375 reg.file = file;
376 reg.idx = idx;
377 reg.negatebase = 0;
378 reg.abs = 0;
379 reg.negateabs = 0;
380 reg.swz = SWIZZLE_NOOP;
381 reg.pad = 0;
382 return reg;
383}
384
385static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
386{
387 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
388 GET_SWZ(reg.swz, y),
389 GET_SWZ(reg.swz, z),
390 GET_SWZ(reg.swz, w));
391
392 return reg;
393}
394
395static struct ureg swizzle1( struct ureg reg, int x )
396{
397 return swizzle(reg, x, x, x, x);
398}
399
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000400static struct ureg negate( struct ureg reg )
401{
402 reg.negatebase ^= 1;
403 return reg;
404}
405
Keith Whitwell15e75e02005-04-29 15:11:39 +0000406static GLboolean is_undef( struct ureg reg )
407{
Alan Hourihane8d972652006-08-10 13:12:00 +0000408 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000409}
410
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000411
Keith Whitwell15e75e02005-04-29 15:11:39 +0000412static struct ureg get_temp( struct texenv_fragment_program *p )
413{
Brian Paul13abf912006-04-13 19:17:13 +0000414 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000415
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000416 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000417 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000418 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000419
420 /* Then any unused temporary:
421 */
422 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000423 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000424
Keith Whitwell15e75e02005-04-29 15:11:39 +0000425 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000426 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000427 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000428 }
429
Brian Paul13abf912006-04-13 19:17:13 +0000430 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000431 p->program->Base.NumTemporaries = bit;
432
Keith Whitwell93cd9232005-05-11 08:30:23 +0000433 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000434 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
435}
436
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000437static struct ureg get_tex_temp( struct texenv_fragment_program *p )
438{
439 int bit;
440
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000441 /* First try to find availble temp not previously used (to avoid
442 * starting a new texture indirection). According to the spec, the
443 * ~p->temps_output isn't necessary, but will keep it there for
444 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000445 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000446 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000447
448 /* Then any unused temporary:
449 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000450 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000451 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000452
453 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000454 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000455 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000456 }
457
Brian Paul13abf912006-04-13 19:17:13 +0000458 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000459 p->program->Base.NumTemporaries = bit;
460
Keith Whitwell93cd9232005-05-11 08:30:23 +0000461 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000462 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
463}
464
Keith Whitwell15e75e02005-04-29 15:11:39 +0000465
Brian93fef222007-10-29 12:25:46 -0600466static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000467{
Brian93fef222007-10-29 12:25:46 -0600468 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000469
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000470 /* KW: To support tex_env_crossbar, don't release the registers in
471 * temps_output.
472 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000473 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000474 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000475 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000476 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000477}
478
479
Brian9fe3e2e2007-02-23 11:44:14 -0700480static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000481 GLint s0,
482 GLint s1,
483 GLint s2,
484 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700485 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000486{
Brian9fe3e2e2007-02-23 11:44:14 -0700487 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000488 GLuint idx;
489 tokens[0] = s0;
490 tokens[1] = s1;
491 tokens[2] = s2;
492 tokens[3] = s3;
493 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000494 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000495 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000496}
497
Keith Whitwell47b29f52005-05-04 11:44:44 +0000498
Brian9fe3e2e2007-02-23 11:44:14 -0700499#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
500#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
501#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
502#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000503
504
505static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
506{
Brian Paulde997602005-11-12 17:53:14 +0000507 p->program->Base.InputsRead |= (1 << input);
Keith Whitwell47b29f52005-05-04 11:44:44 +0000508 return make_ureg(PROGRAM_INPUT, input);
509}
510
511
Brian Paul7e807512005-11-05 17:10:45 +0000512static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000513 struct ureg ureg )
514{
515 reg->File = ureg.file;
516 reg->Index = ureg.idx;
517 reg->Swizzle = ureg.swz;
Keith Whitwellf2802c42005-10-07 09:55:26 +0000518 reg->NegateBase = ureg.negatebase ? 0xf : 0x0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000519 reg->Abs = ureg.abs;
520 reg->NegateAbs = ureg.negateabs;
521}
522
Brian Paul7e807512005-11-05 17:10:45 +0000523static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000524 struct ureg ureg, GLuint mask )
525{
526 dst->File = ureg.file;
527 dst->Index = ureg.idx;
528 dst->WriteMask = mask;
Brianc9d495c2007-10-23 10:55:24 -0600529 dst->CondMask = COND_TR; /* always pass cond test */
530 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000531}
532
Brian Paul7e807512005-11-05 17:10:45 +0000533static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000534emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000535 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000536 struct ureg dest,
537 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000538 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000539 struct ureg src0,
540 struct ureg src1,
541 struct ureg src2 )
542{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000543 GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000544 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Brian2a8e9bb2007-10-23 10:24:53 -0600545
546 assert(nr < MAX_INSTRUCTIONS);
547
Brian Pauld6272e02006-10-29 18:03:16 +0000548 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000549 inst->Opcode = op;
550
Keith Whitwell47b29f52005-05-04 11:44:44 +0000551 emit_arg( &inst->SrcReg[0], src0 );
552 emit_arg( &inst->SrcReg[1], src1 );
553 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000554
Brian Paule31ac052005-11-20 17:52:40 +0000555 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000556
557 emit_dst( &inst->DstReg, dest, mask );
558
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000559 /* Accounting for indirection tracking:
560 */
561 if (dest.file == PROGRAM_TEMPORARY)
562 p->temps_output |= 1 << dest.idx;
563
Keith Whitwell15e75e02005-04-29 15:11:39 +0000564 return inst;
565}
566
567
568static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000569 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000570 struct ureg dest,
571 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000572 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000573 struct ureg src0,
574 struct ureg src1,
575 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000576{
577 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
578
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000579 /* Accounting for indirection tracking:
580 */
581 if (src0.file == PROGRAM_TEMPORARY)
582 p->alu_temps |= 1 << src0.idx;
583
584 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
585 p->alu_temps |= 1 << src1.idx;
586
587 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
588 p->alu_temps |= 1 << src2.idx;
589
590 if (dest.file == PROGRAM_TEMPORARY)
591 p->alu_temps |= 1 << dest.idx;
592
Brian21f99792007-01-09 11:00:21 -0700593 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000594 return dest;
595}
596
597static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000598 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000599 struct ureg dest,
600 GLuint destmask,
601 GLuint tex_unit,
602 GLuint tex_idx,
603 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000604{
Brian Paul7e807512005-11-05 17:10:45 +0000605 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000606 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000607 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000608 coord, /* arg 0? */
609 undef,
610 undef);
611
Brian Paul7e807512005-11-05 17:10:45 +0000612 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000613 inst->TexSrcUnit = tex_unit;
614
Brian21f99792007-01-09 11:00:21 -0700615 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000616
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000617 /* Is this a texture indirection?
618 */
619 if ((coord.file == PROGRAM_TEMPORARY &&
620 (p->temps_output & (1<<coord.idx))) ||
621 (dest.file == PROGRAM_TEMPORARY &&
622 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700623 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000624 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000625 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000626 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000627 }
628
629 return dest;
630}
631
632
Keith Whitwell47b29f52005-05-04 11:44:44 +0000633static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000634 GLfloat s0,
635 GLfloat s1,
636 GLfloat s2,
637 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000638{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000639 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700640 GLuint idx, swizzle;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000641 values[0] = s0;
642 values[1] = s1;
643 values[2] = s2;
644 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700645 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
646 &swizzle );
647 ASSERT(swizzle == SWIZZLE_NOOP);
Briandff0b0e2008-01-18 12:45:27 -0700648 return make_ureg(PROGRAM_CONSTANT, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000649}
650
Keith Whitwelle4902422005-05-11 15:16:35 +0000651#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000652#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
653#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
654#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000655
656
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000657static struct ureg get_one( struct texenv_fragment_program *p )
658{
659 if (is_undef(p->one))
660 p->one = register_scalar_const(p, 1.0);
661 return p->one;
662}
663
664static struct ureg get_half( struct texenv_fragment_program *p )
665{
666 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000667 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000668 return p->half;
669}
670
671static struct ureg get_zero( struct texenv_fragment_program *p )
672{
673 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000674 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000675 return p->zero;
676}
677
Keith Whitwell15e75e02005-04-29 15:11:39 +0000678
Keith Whitwell15e75e02005-04-29 15:11:39 +0000679static void program_error( struct texenv_fragment_program *p, const char *msg )
680{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000681 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000682 p->error = 1;
683}
684
Keith Whitwell15e75e02005-04-29 15:11:39 +0000685static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000686 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000687{
688 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000689 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000690 assert(!is_undef(p->src_texture[unit]));
691 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000692
Keith Whitwellce721142005-07-11 10:10:38 +0000693 case SRC_TEXTURE0:
694 case SRC_TEXTURE1:
695 case SRC_TEXTURE2:
696 case SRC_TEXTURE3:
697 case SRC_TEXTURE4:
698 case SRC_TEXTURE5:
699 case SRC_TEXTURE6:
700 case SRC_TEXTURE7:
701 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
702 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000703
Keith Whitwellce721142005-07-11 10:10:38 +0000704 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000705 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000706
Keith Whitwellce721142005-07-11 10:10:38 +0000707 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000708 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000709
Brian Paul6947f852009-01-23 17:32:09 -0700710 case SRC_ZERO:
711 return get_zero(p);
712
Keith Whitwellce721142005-07-11 10:10:38 +0000713 case SRC_PREVIOUS:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000714 if (is_undef(p->src_previous))
715 return register_input(p, FRAG_ATTRIB_COL0);
716 else
717 return p->src_previous;
Brian Paul6947f852009-01-23 17:32:09 -0700718
719 default:
720 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000721 }
722}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000723
Keith Whitwell15e75e02005-04-29 15:11:39 +0000724static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000725 GLuint mask,
726 GLuint unit,
727 GLuint source,
728 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000729{
730 struct ureg arg, src, one;
731
732 src = get_source(p, source, unit);
733
734 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000735 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000736 /* Get unused tmp,
737 * Emit tmp = 1.0 - arg.xyzw
738 */
739 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000740 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000741 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000742
Keith Whitwellce721142005-07-11 10:10:38 +0000743 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000744 if (mask == WRITEMASK_W)
745 return src;
746 else
Brian Paul22db5352005-11-19 23:45:10 +0000747 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000748 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000749 /* Get unused tmp,
750 * Emit tmp = 1.0 - arg.wwww
751 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000752 arg = get_temp(p);
753 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000754 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000755 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000756 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000757 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000758 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000759 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000760 case OPR_SRC_COLOR:
Brian Paul6947f852009-01-23 17:32:09 -0700761 return src;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000762 default:
Brian Paul6947f852009-01-23 17:32:09 -0700763 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000764 return src;
765 }
766}
767
Keith Whitwellce721142005-07-11 10:10:38 +0000768static GLboolean args_match( struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000769{
Brian Paul22db5352005-11-19 23:45:10 +0000770 GLuint i, nr = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000771
772 for (i = 0 ; i < nr ; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000773 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000774 return GL_FALSE;
775
Keith Whitwellce721142005-07-11 10:10:38 +0000776 switch(key->unit[unit].OptA[i].Operand) {
777 case OPR_SRC_ALPHA:
778 switch(key->unit[unit].OptRGB[i].Operand) {
779 case OPR_SRC_COLOR:
780 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000781 break;
782 default:
783 return GL_FALSE;
784 }
785 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000786 case OPR_ONE_MINUS_SRC_ALPHA:
787 switch(key->unit[unit].OptRGB[i].Operand) {
788 case OPR_ONE_MINUS_SRC_COLOR:
789 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000790 break;
791 default:
792 return GL_FALSE;
793 }
794 break;
795 default:
796 return GL_FALSE; /* impossible */
797 }
798 }
799
800 return GL_TRUE;
801}
802
Keith Whitwell15e75e02005-04-29 15:11:39 +0000803static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000804 struct ureg dest,
805 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000806 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000807 GLuint unit,
808 GLuint nr,
809 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +0000810 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000811{
Brian Paul6947f852009-01-23 17:32:09 -0700812 struct ureg src[MAX_TERMS];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000813 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +0000814 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000815
Brian Paul6947f852009-01-23 17:32:09 -0700816 assert(nr <= MAX_TERMS);
817
Brian Paul00630842005-12-12 15:27:55 +0000818 tmp = undef; /* silence warning (bug 5318) */
819
Keith Whitwell15e75e02005-04-29 15:11:39 +0000820 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +0000821 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000822
823 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +0000824 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000825 if (mask == WRITEMASK_XYZW && !saturate)
826 return src[0];
827 else
Brian Paul7e807512005-11-05 17:10:45 +0000828 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000829 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +0000830 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000831 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000832 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +0000833 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000834 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000835 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000836 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000837 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +0000838 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000839 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +0000840 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +0000841 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
842 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000843 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +0000844 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000845 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
846 */
Brian Paul7e807512005-11-05 17:10:45 +0000847 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000848
Keith Whitwellce721142005-07-11 10:10:38 +0000849 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +0000850 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000851
Keith Whitwellce721142005-07-11 10:10:38 +0000852 case MODE_DOT3_RGBA:
853 case MODE_DOT3_RGBA_EXT:
854 case MODE_DOT3_RGB_EXT:
855 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000856 struct ureg tmp0 = get_temp( p );
857 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +0000858 struct ureg neg1 = register_scalar_const(p, -1);
859 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000860
861 /* tmp0 = 2*src0 - 1
862 * tmp1 = 2*src1 - 1
863 *
864 * dst = tmp0 dot3 tmp1
865 */
Brian Paul7e807512005-11-05 17:10:45 +0000866 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000867 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000868
Brian Paulaa206952005-09-16 18:14:24 +0000869 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000870 tmp1 = tmp0;
871 else
Brian Paul7e807512005-11-05 17:10:45 +0000872 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000873 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +0000874 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000875 return dest;
876 }
Keith Whitwellce721142005-07-11 10:10:38 +0000877 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000878 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +0000879 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000880 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +0000881 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000882 /* Arg0 * Arg2 + Arg1 - 0.5 */
883 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000884 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +0000885 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
886 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000887 return dest;
888 }
Keith Whitwellce721142005-07-11 10:10:38 +0000889 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000890 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +0000891 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000892 return dest;
Brian Paul6947f852009-01-23 17:32:09 -0700893 case MODE_ADD_PRODUCTS:
894 /* Arg0 * Arg1 + Arg2 * Arg3 */
895 {
896 struct ureg tmp0 = get_temp(p);
897 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
898 emit_arith( p, OPCODE_MAD, dest, mask, saturate, src[2], src[3], tmp0 );
899 }
900 return dest;
901 case MODE_ADD_PRODUCTS_SIGNED:
902 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
903 {
904 struct ureg tmp0 = get_temp(p);
905 half = get_half(p);
906 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
907 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[2], src[3], tmp0 );
908 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
909 }
910 return dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000911 default:
Brian Paul6947f852009-01-23 17:32:09 -0700912 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000913 return src[0];
914 }
915}
916
Keith Whitwell15e75e02005-04-29 15:11:39 +0000917
Brian Paul22db5352005-11-19 23:45:10 +0000918/**
919 * Generate instructions for one texture unit's env/combiner mode.
920 */
921static struct ureg
922emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000923{
Keith Whitwellce721142005-07-11 10:10:38 +0000924 struct state_key *key = p->state;
Brian Paul22db5352005-11-19 23:45:10 +0000925 GLboolean saturate = (unit < p->last_tex_stage);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000926 GLuint rgb_shift, alpha_shift;
927 struct ureg out, shift;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000928 struct ureg dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000929
Keith Whitwellce721142005-07-11 10:10:38 +0000930 if (!key->unit[unit].enabled) {
931 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000932 }
Keith Whitwellce721142005-07-11 10:10:38 +0000933
934 switch (key->unit[unit].ModeRGB) {
935 case MODE_DOT3_RGB_EXT:
936 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000937 rgb_shift = 0;
938 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000939 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000940 alpha_shift = 0;
941 rgb_shift = 0;
942 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000943 default:
Keith Whitwellce721142005-07-11 10:10:38 +0000944 rgb_shift = key->unit[unit].ScaleShiftRGB;
945 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000946 break;
947 }
Keith Whitwellce721142005-07-11 10:10:38 +0000948
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000949 /* If this is the very last calculation, emit direct to output reg:
950 */
Keith Whitwellce721142005-07-11 10:10:38 +0000951 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000952 unit != p->last_tex_stage ||
953 alpha_shift ||
954 rgb_shift)
955 dest = get_temp( p );
956 else
Brian Paul90ebb582005-11-02 18:06:12 +0000957 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLR);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000958
959 /* Emit the RGB and A combine ops
960 */
Keith Whitwellce721142005-07-11 10:10:38 +0000961 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
962 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000963 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
964 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000965 key->unit[unit].NumArgsRGB,
966 key->unit[unit].ModeRGB,
967 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000968 }
Keith Whitwellce721142005-07-11 10:10:38 +0000969 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +0200970 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000971
972 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
973 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000974 key->unit[unit].NumArgsRGB,
975 key->unit[unit].ModeRGB,
976 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000977 }
978 else {
979 /* Need to do something to stop from re-emitting identical
980 * argument calculations here:
981 */
982 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
983 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000984 key->unit[unit].NumArgsRGB,
985 key->unit[unit].ModeRGB,
986 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000987 out = emit_combine( p, dest, WRITEMASK_W, saturate,
988 unit,
Keith Whitwellce721142005-07-11 10:10:38 +0000989 key->unit[unit].NumArgsA,
990 key->unit[unit].ModeA,
991 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000992 }
993
994 /* Deal with the final shift:
995 */
996 if (alpha_shift || rgb_shift) {
997 if (rgb_shift == alpha_shift) {
José Fonseca452a5922008-05-31 18:14:09 +0900998 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +0000999 }
1000 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001001 shift = register_const4f(p,
José Fonseca452a5922008-05-31 18:14:09 +09001002 (GLfloat)(1<<rgb_shift),
1003 (GLfloat)(1<<rgb_shift),
1004 (GLfloat)(1<<rgb_shift),
1005 (GLfloat)(1<<alpha_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001006 }
Brian Paul7e807512005-11-05 17:10:45 +00001007 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001008 saturate, out, shift, undef );
1009 }
1010 else
1011 return out;
1012}
1013
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001014
Brian Paul22db5352005-11-19 23:45:10 +00001015/**
1016 * Generate instruction for getting a texture source term.
1017 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001018static void load_texture( struct texenv_fragment_program *p, GLuint unit )
1019{
1020 if (is_undef(p->src_texture[unit])) {
Keith Whitwellce721142005-07-11 10:10:38 +00001021 GLuint dim = p->state->unit[unit].source_index;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001022 struct ureg texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
1023 struct ureg tmp = get_tex_temp( p );
1024
Brian Paulbfb5ea32005-07-19 18:20:04 +00001025 if (dim == TEXTURE_UNKNOWN_INDEX)
1026 program_error(p, "TexSrcBit");
Keith Whitwellce721142005-07-11 10:10:38 +00001027
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001028 /* TODO: Use D0_MASK_XY where possible.
1029 */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +02001030 if (p->state->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001031 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1032 tmp, WRITEMASK_XYZW,
1033 unit, dim, texcoord );
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +02001034 if (p->state->unit[unit].shadow)
1035 p->program->Base.ShadowSamplers |= 1 << unit;
Brian9b7e5a52007-12-14 11:16:49 -07001036
1037 p->program->Base.SamplersUsed |= (1 << unit);
Brian1fe385f2007-12-14 11:42:28 -07001038 /* This identity mapping should already be in place
1039 * (see _mesa_init_program_struct()) but let's be safe.
1040 */
1041 p->program->Base.SamplerUnits[unit] = unit;
Brian9b7e5a52007-12-14 11:16:49 -07001042 }
1043 else
Aapo Tahkolab8915342006-03-28 10:26:34 +00001044 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001045 }
1046}
1047
Keith Whitwell241b6b72005-05-23 09:50:34 +00001048static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +00001049 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001050{
1051 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +00001052 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001053 load_texture(p, unit);
1054 break;
1055
Keith Whitwellce721142005-07-11 10:10:38 +00001056 case SRC_TEXTURE0:
1057 case SRC_TEXTURE1:
1058 case SRC_TEXTURE2:
1059 case SRC_TEXTURE3:
1060 case SRC_TEXTURE4:
1061 case SRC_TEXTURE5:
1062 case SRC_TEXTURE6:
1063 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +00001064 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001065 break;
1066
1067 default:
Brian Paul6947f852009-01-23 17:32:09 -07001068 /* not a texture src - do nothing */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001069 break;
1070 }
Keith Whitwell241b6b72005-05-23 09:50:34 +00001071
1072 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001073}
1074
Brian Paul22db5352005-11-19 23:45:10 +00001075
1076/**
1077 * Generate instructions for loading all texture source terms.
1078 */
1079static GLboolean
1080load_texunit_sources( struct texenv_fragment_program *p, int unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001081{
Keith Whitwellce721142005-07-11 10:10:38 +00001082 struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +00001083 GLuint i;
1084
1085 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
1086 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001087 }
Aapo Tahkolab8915342006-03-28 10:26:34 +00001088
1089 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1090 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1091 }
1092
Keith Whitwell241b6b72005-05-23 09:50:34 +00001093 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001094}
1095
Brian Paul22db5352005-11-19 23:45:10 +00001096
1097/**
1098 * Generate a new fragment program which implements the context's
1099 * current texture env/combine mode.
1100 */
1101static void
Brian Paule998c342006-10-29 21:17:18 +00001102create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001103 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001104{
Brian Paule998c342006-10-29 21:17:18 +00001105 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001106 struct texenv_fragment_program p;
1107 GLuint unit;
1108 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001109
Keith Whitwelle4902422005-05-11 15:16:35 +00001110 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +00001111 p.ctx = ctx;
Keith Whitwellce721142005-07-11 10:10:38 +00001112 p.state = key;
1113 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001114
Brian Paule998c342006-10-29 21:17:18 +00001115 /* During code generation, use locally-allocated instruction buffer,
1116 * then alloc dynamic storage below.
1117 */
1118 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001119 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Brian21f99792007-01-09 11:00:21 -07001120 p.program->Base.NumTexIndirections = 1; /* correct? */
1121 p.program->Base.NumTexInstructions = 0;
1122 p.program->Base.NumAluInstructions = 0;
Brian1240eb22007-03-22 08:50:20 -06001123 p.program->Base.String = NULL;
Keith Whitwell9ca88152005-05-10 09:56:02 +00001124 p.program->Base.NumInstructions =
Brian Paule998c342006-10-29 21:17:18 +00001125 p.program->Base.NumTemporaries =
1126 p.program->Base.NumParameters =
1127 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001128 p.program->Base.Parameters = _mesa_new_parameter_list();
Keith Whitwelldbeea252005-05-10 13:57:50 +00001129
Brian Paulde997602005-11-12 17:53:14 +00001130 p.program->Base.InputsRead = 0;
1131 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001132
Brian Paule9b34882008-12-31 11:54:02 -07001133 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001134 p.src_texture[unit] = undef;
1135
Keith Whitwell47b29f52005-05-04 11:44:44 +00001136 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001137 p.half = undef;
1138 p.zero = undef;
1139 p.one = undef;
1140
Keith Whitwell15e75e02005-04-29 15:11:39 +00001141 p.last_tex_stage = 0;
Brian93fef222007-10-29 12:25:46 -06001142 release_temps(ctx, &p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001143
Keith Whitwellce721142005-07-11 10:10:38 +00001144 if (key->enabled_units) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001145 /* First pass - to support texture_env_crossbar, first identify
1146 * all referenced texture sources and emit texld instructions
1147 * for each:
1148 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001149 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001150 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001151 load_texunit_sources( &p, unit );
1152 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001153 }
1154
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001155 /* Second pass - emit combine instructions to build final color:
1156 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001157 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001158 if (key->enabled_units & (1<<unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001159 p.src_previous = emit_texenv( &p, unit );
Brian93fef222007-10-29 12:25:46 -06001160 release_temps(ctx, &p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001161 }
1162 }
1163
Keith Whitwellce721142005-07-11 10:10:38 +00001164 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul90ebb582005-11-02 18:06:12 +00001165 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001166
Keith Whitwellce721142005-07-11 10:10:38 +00001167 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001168 /* Emit specular add.
1169 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001170 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001171 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1172 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001173 }
Brian Paulaa206952005-09-16 18:14:24 +00001174 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001175 /* Will wind up in here if no texture enabled or a couple of
1176 * other scenarios (GL_REPLACE for instance).
1177 */
Brian Paul7e807512005-11-05 17:10:45 +00001178 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001179 }
1180
Keith Whitwell47b29f52005-05-04 11:44:44 +00001181 /* Finish up:
1182 */
Brian Paul7e807512005-11-05 17:10:45 +00001183 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001184
Keith Whitwellce721142005-07-11 10:10:38 +00001185 if (key->fog_enabled) {
1186 /* Pull fog mode from GLcontext, the value in the state key is
1187 * a reduced value and not what is expected in FogOption
1188 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001189 p.program->FogOption = ctx->Fog.Mode;
Brian19d77d62007-09-18 19:29:26 -06001190 p.program->Base.InputsRead |= FRAG_BIT_FOGC; /* XXX new */
Keith Whitwellce721142005-07-11 10:10:38 +00001191 } else
Keith Whitwell276330b2005-05-09 17:58:13 +00001192 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001193
Brian21f99792007-01-09 11:00:21 -07001194 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001195 program_error(&p, "Exceeded max nr indirect texture lookups");
1196
Brian21f99792007-01-09 11:00:21 -07001197 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001198 program_error(&p, "Exceeded max TEX instructions");
1199
Brian21f99792007-01-09 11:00:21 -07001200 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001201 program_error(&p, "Exceeded max ALU instructions");
1202
Brian Paul5d7b49f2005-11-19 23:12:20 +00001203 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001204
Brian Paule998c342006-10-29 21:17:18 +00001205 /* Allocate final instruction array */
Brianc81cce72007-09-25 15:18:51 -06001206 p.program->Base.Instructions
1207 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1208 if (!p.program->Base.Instructions) {
Brian Paule998c342006-10-29 21:17:18 +00001209 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1210 "generating tex env program");
1211 return;
1212 }
Brianc81cce72007-09-25 15:18:51 -06001213 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1214 p.program->Base.NumInstructions);
1215
1216 if (p.program->FogOption) {
1217 _mesa_append_fog_code(ctx, p.program);
1218 p.program->FogOption = GL_NONE;
1219 }
1220
Brian Paule998c342006-10-29 21:17:18 +00001221
Keith Whitwelldbeea252005-05-10 13:57:50 +00001222 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001223 */
Brian Paule998c342006-10-29 21:17:18 +00001224 if (ctx->Driver.ProgramStringNotify) {
1225 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1226 &p.program->Base );
1227 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001228
Brian Paule998c342006-10-29 21:17:18 +00001229 if (DISASSEM) {
1230 _mesa_print_program(&p.program->Base);
1231 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001232 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001233}
1234
Brian Paul5d7b49f2005-11-19 23:12:20 +00001235
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001236/**
1237 * Return a fragment program which implements the current
1238 * fixed-function texture, fog and color-sum operations.
1239 */
1240struct gl_fragment_program *
1241_mesa_get_fixed_func_fragment_program(GLcontext *ctx)
Keith Whitwellce721142005-07-11 10:10:38 +00001242{
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001243 struct gl_fragment_program *prog;
1244 struct state_key key;
1245
1246 make_state_key(ctx, &key);
1247
1248 prog = (struct gl_fragment_program *)
1249 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1250 &key, sizeof(key));
Keith Whitwellce721142005-07-11 10:10:38 +00001251
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001252 if (!prog) {
1253 prog = (struct gl_fragment_program *)
1254 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1255
1256 create_new_program(ctx, &key, prog);
1257
1258 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1259 &key, sizeof(key), &prog->Base);
Keith Whitwellce721142005-07-11 10:10:38 +00001260 }
1261
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001262 return prog;
Keith Whitwellce721142005-07-11 10:10:38 +00001263}
1264
Keith Whitwellce721142005-07-11 10:10:38 +00001265
Briana90046f2006-12-15 10:07:26 -07001266
1267/**
1268 * If _MaintainTexEnvProgram is set we'll generate a fragment program that
1269 * implements the current texture env/combine mode.
1270 * This function generates that program and puts it into effect.
1271 */
1272void
1273_mesa_UpdateTexEnvProgram( GLcontext *ctx )
Keith Whitwellce721142005-07-11 10:10:38 +00001274{
Brian Paul122629f2006-07-20 16:49:57 +00001275 const struct gl_fragment_program *prev = ctx->FragmentProgram._Current;
Keith Whitwellce721142005-07-11 10:10:38 +00001276
Briana90046f2006-12-15 10:07:26 -07001277 ASSERT(ctx->FragmentProgram._MaintainTexEnvProgram);
1278
1279 /* If a conventional fragment program/shader isn't in effect... */
Brianefcfdbd2007-02-24 15:51:41 -07001280 if (!ctx->FragmentProgram._Enabled &&
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001281 (!ctx->Shader.CurrentProgram ||
1282 !ctx->Shader.CurrentProgram->FragmentProgram) )
1283 {
Brian Paul5b5c9312008-05-07 18:51:44 -06001284 struct gl_fragment_program *newProg;
1285
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001286 newProg = _mesa_get_fixed_func_fragment_program(ctx);
Brian Paul5b5c9312008-05-07 18:51:44 -06001287
1288 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current, newProg);
1289 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._TexEnvProgram, newProg);
Keith Whitwellc3626a92005-11-01 17:25:49 +00001290 }
Keith Whitwellc3626a92005-11-01 17:25:49 +00001291
1292 /* Tell the driver about the change. Could define a new target for
1293 * this?
1294 */
Brian Paul5d7b49f2005-11-19 23:12:20 +00001295 if (ctx->FragmentProgram._Current != prev && ctx->Driver.BindProgram) {
1296 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
Briana90046f2006-12-15 10:07:26 -07001297 (struct gl_program *) ctx->FragmentProgram._Current);
Brian Paul5d7b49f2005-11-19 23:12:20 +00001298 }
Keith Whitwellce721142005-07-11 10:10:38 +00001299}