blob: 48abf51d89057f0b0eccdfb000ec2003c2c2d920 [file] [log] [blame]
Keith Whitwell15e75e02005-04-29 15:11:39 +00001/**************************************************************************
2 *
Brianf4a5ea22007-10-31 12:45:32 -06003 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
Keith Whitwell15e75e02005-04-29 15:11:39 +00004 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
Keith Whitwell15e75e02005-04-29 15:11:39 +000028#include "glheader.h"
29#include "macros.h"
30#include "enums.h"
Brian Paul5b5c9312008-05-07 18:51:44 -060031#include "shader/program.h"
Brianc223c6b2007-07-04 13:15:20 -060032#include "shader/prog_parameter.h"
Brianf4a5ea22007-10-31 12:45:32 -060033#include "shader/prog_cache.h"
Brianc223c6b2007-07-04 13:15:20 -060034#include "shader/prog_instruction.h"
35#include "shader/prog_print.h"
36#include "shader/prog_statevars.h"
Brian83fad682007-09-25 15:20:04 -060037#include "shader/programopt.h"
Keith Whitwell15e75e02005-04-29 15:11:39 +000038#include "texenvprogram.h"
39
Brian Paul5b5c9312008-05-07 18:51:44 -060040
Brian Paule9b34882008-12-31 11:54:02 -070041/*
42 * Note on texture units:
43 *
44 * The number of texture units supported by fixed-function fragment
45 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
46 * That's because there's a one-to-one correspondence between texture
47 * coordinates and samplers in fixed-function processing.
48 *
49 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
50 * sets of texcoords, so is fixed-function fragment processing.
51 *
52 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
53 */
54
55
Brian Paul5b5c9312008-05-07 18:51:44 -060056struct texenvprog_cache_item
57{
58 GLuint hash;
59 void *key;
60 struct gl_fragment_program *data;
61 struct texenvprog_cache_item *next;
62};
63
64
Brian Paule998c342006-10-29 21:17:18 +000065/**
Brian Paulb5e1a932008-09-25 18:40:16 -060066 * Up to nine instructions per tex unit, plus fog, specular color.
Brian Paule998c342006-10-29 21:17:18 +000067 */
Brian Paul0815ebc2009-01-02 16:32:26 -070068#define MAX_INSTRUCTIONS ((MAX_TEXTURE_COORD_UNITS * 9) + 12)
Keith Whitwell15e75e02005-04-29 15:11:39 +000069
Keith Whitwell269e3892005-05-12 10:28:43 +000070#define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
Keith Whitwell15e75e02005-04-29 15:11:39 +000071
Keith Whitwellce721142005-07-11 10:10:38 +000072struct mode_opt {
José Fonseca9972d712008-12-30 17:21:25 +000073 GLuint Source:4;
74 GLuint Operand:3;
Keith Whitwellce721142005-07-11 10:10:38 +000075};
76
77struct state_key {
Keith Whitwell6280e332008-10-03 13:51:56 +010078 GLuint nr_enabled_units:8;
79 GLuint enabled_units:8;
Brian Paul5d7b49f2005-11-19 23:12:20 +000080 GLuint separate_specular:1;
81 GLuint fog_enabled:1;
82 GLuint fog_mode:2;
Keith Whitwell6280e332008-10-03 13:51:56 +010083 GLuint inputs_available:12;
Keith Whitwellce721142005-07-11 10:10:38 +000084
85 struct {
Brian Paul5d7b49f2005-11-19 23:12:20 +000086 GLuint enabled:1;
87 GLuint source_index:3; /* one of TEXTURE_1D/2D/3D/CUBE/RECT_INDEX */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +020088 GLuint shadow:1;
Brian Paul5d7b49f2005-11-19 23:12:20 +000089 GLuint ScaleShiftRGB:2;
90 GLuint ScaleShiftA:2;
Keith Whitwellce721142005-07-11 10:10:38 +000091
Brian Paul5d7b49f2005-11-19 23:12:20 +000092 GLuint NumArgsRGB:2;
93 GLuint ModeRGB:4;
Brian Paul5d7b49f2005-11-19 23:12:20 +000094 GLuint NumArgsA:2;
95 GLuint ModeA:4;
Keith Whitwell6280e332008-10-03 13:51:56 +010096
97 struct mode_opt OptRGB[3];
Keith Whitwellce721142005-07-11 10:10:38 +000098 struct mode_opt OptA[3];
99 } unit[8];
100};
101
102#define FOG_LINEAR 0
103#define FOG_EXP 1
104#define FOG_EXP2 2
105#define FOG_UNKNOWN 3
106
107static GLuint translate_fog_mode( GLenum mode )
108{
109 switch (mode) {
110 case GL_LINEAR: return FOG_LINEAR;
111 case GL_EXP: return FOG_EXP;
112 case GL_EXP2: return FOG_EXP2;
113 default: return FOG_UNKNOWN;
114 }
115}
116
117#define OPR_SRC_COLOR 0
118#define OPR_ONE_MINUS_SRC_COLOR 1
119#define OPR_SRC_ALPHA 2
120#define OPR_ONE_MINUS_SRC_ALPHA 3
121#define OPR_ZERO 4
122#define OPR_ONE 5
123#define OPR_UNKNOWN 7
124
125static GLuint translate_operand( GLenum operand )
126{
127 switch (operand) {
128 case GL_SRC_COLOR: return OPR_SRC_COLOR;
129 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
130 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
131 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
132 case GL_ZERO: return OPR_ZERO;
133 case GL_ONE: return OPR_ONE;
134 default: return OPR_UNKNOWN;
135 }
136}
137
138#define SRC_TEXTURE 0
139#define SRC_TEXTURE0 1
140#define SRC_TEXTURE1 2
141#define SRC_TEXTURE2 3
142#define SRC_TEXTURE3 4
143#define SRC_TEXTURE4 5
144#define SRC_TEXTURE5 6
145#define SRC_TEXTURE6 7
146#define SRC_TEXTURE7 8
147#define SRC_CONSTANT 9
148#define SRC_PRIMARY_COLOR 10
149#define SRC_PREVIOUS 11
150#define SRC_UNKNOWN 15
151
152static GLuint translate_source( GLenum src )
153{
154 switch (src) {
155 case GL_TEXTURE: return SRC_TEXTURE;
156 case GL_TEXTURE0:
157 case GL_TEXTURE1:
158 case GL_TEXTURE2:
159 case GL_TEXTURE3:
160 case GL_TEXTURE4:
161 case GL_TEXTURE5:
162 case GL_TEXTURE6:
163 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
164 case GL_CONSTANT: return SRC_CONSTANT;
165 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
166 case GL_PREVIOUS: return SRC_PREVIOUS;
167 default: return SRC_UNKNOWN;
168 }
169}
170
171#define MODE_REPLACE 0
172#define MODE_MODULATE 1
173#define MODE_ADD 2
174#define MODE_ADD_SIGNED 3
175#define MODE_INTERPOLATE 4
176#define MODE_SUBTRACT 5
177#define MODE_DOT3_RGB 6
178#define MODE_DOT3_RGB_EXT 7
179#define MODE_DOT3_RGBA 8
180#define MODE_DOT3_RGBA_EXT 9
181#define MODE_MODULATE_ADD_ATI 10
182#define MODE_MODULATE_SIGNED_ADD_ATI 11
183#define MODE_MODULATE_SUBTRACT_ATI 12
184#define MODE_UNKNOWN 15
185
186static GLuint translate_mode( GLenum mode )
187{
188 switch (mode) {
189 case GL_REPLACE: return MODE_REPLACE;
190 case GL_MODULATE: return MODE_MODULATE;
191 case GL_ADD: return MODE_ADD;
192 case GL_ADD_SIGNED: return MODE_ADD_SIGNED;
193 case GL_INTERPOLATE: return MODE_INTERPOLATE;
194 case GL_SUBTRACT: return MODE_SUBTRACT;
195 case GL_DOT3_RGB: return MODE_DOT3_RGB;
196 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
197 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
198 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
199 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
200 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
201 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
202 default: return MODE_UNKNOWN;
203 }
204}
205
206#define TEXTURE_UNKNOWN_INDEX 7
Brian Paule00ac112005-09-15 05:00:45 +0000207static GLuint translate_tex_src_bit( GLbitfield bit )
Keith Whitwellce721142005-07-11 10:10:38 +0000208{
Brian Paul1519b932008-12-17 13:17:15 -0700209 /* make sure number of switch cases is correct */
210 assert(NUM_TEXTURE_TARGETS == 7);
Keith Whitwellce721142005-07-11 10:10:38 +0000211 switch (bit) {
Brian Paul1519b932008-12-17 13:17:15 -0700212 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
213 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
214 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
215 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
216 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
217 case TEXTURE_1D_ARRAY_BIT: return TEXTURE_1D_ARRAY_INDEX;
218 case TEXTURE_2D_ARRAY_BIT: return TEXTURE_2D_ARRAY_INDEX;
219 default: return TEXTURE_UNKNOWN_INDEX;
Keith Whitwellce721142005-07-11 10:10:38 +0000220 }
221}
222
Keith Whitwell1680ef82008-10-03 17:30:59 +0100223#define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
224#define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
225
Brian Paul239617f2008-10-07 11:22:47 -0600226/**
227 * Identify all possible varying inputs. The fragment program will
Keith Whitwell1680ef82008-10-03 17:30:59 +0100228 * never reference non-varying inputs, but will track them via state
229 * constants instead.
230 *
231 * This function figures out all the inputs that the fragment program
232 * has access to. The bitmask is later reduced to just those which
233 * are actually referenced.
234 */
Brian Paul239617f2008-10-07 11:22:47 -0600235static GLbitfield get_fp_input_mask( GLcontext *ctx )
Keith Whitwell1680ef82008-10-03 17:30:59 +0100236{
Brian Paulf0b07942008-12-17 14:04:03 -0700237 const GLboolean vertexShader = (ctx->Shader.CurrentProgram &&
238 ctx->Shader.CurrentProgram->VertexProgram);
239 const GLboolean vertexProgram = ctx->VertexProgram._Enabled;
Brian Paul239617f2008-10-07 11:22:47 -0600240 GLbitfield fp_inputs = 0x0;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100241
Brian Paul6d4d51d2008-10-10 13:39:14 -0600242 if (ctx->VertexProgram._Overriden) {
243 /* Somebody's messing with the vertex program and we don't have
244 * a clue what's happening. Assume that it could be producing
245 * all possible outputs.
246 */
247 fp_inputs = ~0;
248 }
249 else if (ctx->RenderMode == GL_FEEDBACK) {
250 fp_inputs = (FRAG_BIT_COL0 | FRAG_BIT_TEX0);
251 }
Brian Paulf0b07942008-12-17 14:04:03 -0700252 else if (!(vertexProgram || vertexShader) ||
Brian Pauld7296a12008-12-17 11:29:06 -0700253 !ctx->VertexProgram._Current) {
Brian Paulf0b07942008-12-17 14:04:03 -0700254 /* Fixed function vertex logic */
Brian Paul239617f2008-10-07 11:22:47 -0600255 GLbitfield varying_inputs = ctx->varying_vp_inputs;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100256
Keith Whitwell97e63432008-10-20 13:03:45 +0100257 /* These get generated in the setup routine regardless of the
258 * vertex program:
259 */
260 if (ctx->Point.PointSprite)
261 varying_inputs |= FRAG_BITS_TEX_ANY;
262
Keith Whitwell1680ef82008-10-03 17:30:59 +0100263 /* First look at what values may be computed by the generated
264 * vertex program:
265 */
266 if (ctx->Light.Enabled) {
267 fp_inputs |= FRAG_BIT_COL0;
268
269 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
270 fp_inputs |= FRAG_BIT_COL1;
271 }
272
273 fp_inputs |= (ctx->Texture._TexGenEnabled |
274 ctx->Texture._TexMatEnabled) << FRAG_ATTRIB_TEX0;
275
276 /* Then look at what might be varying as a result of enabled
277 * arrays, etc:
278 */
279 if (varying_inputs & VERT_BIT_COLOR0) fp_inputs |= FRAG_BIT_COL0;
280 if (varying_inputs & VERT_BIT_COLOR1) fp_inputs |= FRAG_BIT_COL1;
281
282 fp_inputs |= (((varying_inputs & VERT_BIT_TEX_ANY) >> VERT_ATTRIB_TEX0)
283 << FRAG_ATTRIB_TEX0);
284
285 }
286 else {
287 /* calculate from vp->outputs */
Brian Paul2389c052008-12-17 19:01:34 -0700288 struct gl_vertex_program *vprog;
289 GLbitfield vp_outputs;
290
291 /* Choose GLSL vertex shader over ARB vertex program. Need this
292 * since vertex shader state validation comes after fragment state
293 * validation (see additional comments in state.c).
294 */
295 if (vertexShader)
296 vprog = ctx->Shader.CurrentProgram->VertexProgram;
297 else
298 vprog = ctx->VertexProgram._Current;
299
300 vp_outputs = vprog->Base.OutputsWritten;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100301
Keith Whitwell97e63432008-10-20 13:03:45 +0100302 /* These get generated in the setup routine regardless of the
303 * vertex program:
304 */
305 if (ctx->Point.PointSprite)
306 vp_outputs |= FRAG_BITS_TEX_ANY;
307
Keith Whitwell1680ef82008-10-03 17:30:59 +0100308 if (vp_outputs & (1 << VERT_RESULT_COL0)) fp_inputs |= FRAG_BIT_COL0;
309 if (vp_outputs & (1 << VERT_RESULT_COL1)) fp_inputs |= FRAG_BIT_COL1;
310
Keith Whitwell0370d6b2008-10-04 12:41:56 +0100311 fp_inputs |= (((vp_outputs & VERT_RESULT_TEX_ANY) >> VERT_RESULT_TEX0)
312 << FRAG_ATTRIB_TEX0);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100313 }
314
315 return fp_inputs;
316}
317
318
Brian Paul22db5352005-11-19 23:45:10 +0000319/**
320 * Examine current texture environment state and generate a unique
321 * key to identify it.
322 */
Keith Whitwell8065c122006-05-22 16:09:27 +0000323static void make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000324{
Keith Whitwellce721142005-07-11 10:10:38 +0000325 GLuint i, j;
Brian Paul239617f2008-10-07 11:22:47 -0600326 GLbitfield inputs_referenced = FRAG_BIT_COL0;
327 GLbitfield inputs_available = get_fp_input_mask( ctx );
Keith Whitwell1680ef82008-10-03 17:30:59 +0100328
Keith Whitwell8065c122006-05-22 16:09:27 +0000329 memset(key, 0, sizeof(*key));
330
Brian Paule9b34882008-12-31 11:54:02 -0700331 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000332 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800333 GLenum format;
Roland Scheideggerbf4a0fa2008-02-15 17:26:06 +0100334
335 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
Keith Whitwellce721142005-07-11 10:10:38 +0000336 continue;
337
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800338 format = texUnit->_Current->Image[0][texUnit->_Current->BaseLevel]->_BaseFormat;
339
Keith Whitwellce721142005-07-11 10:10:38 +0000340 key->unit[i].enabled = 1;
341 key->enabled_units |= (1<<i);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100342 key->nr_enabled_units = i+1;
343 inputs_referenced |= FRAG_BIT_TEX(i);
Keith Whitwellce721142005-07-11 10:10:38 +0000344
345 key->unit[i].source_index =
346 translate_tex_src_bit(texUnit->_ReallyEnabled);
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800347 key->unit[i].shadow = ((texUnit->_Current->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
348 ((format == GL_DEPTH_COMPONENT) ||
349 (format == GL_DEPTH_STENCIL_EXT)));
Keith Whitwellce721142005-07-11 10:10:38 +0000350
351 key->unit[i].NumArgsRGB = texUnit->_CurrentCombine->_NumArgsRGB;
352 key->unit[i].NumArgsA = texUnit->_CurrentCombine->_NumArgsA;
353
354 key->unit[i].ModeRGB =
355 translate_mode(texUnit->_CurrentCombine->ModeRGB);
356 key->unit[i].ModeA =
357 translate_mode(texUnit->_CurrentCombine->ModeA);
358
359 key->unit[i].ScaleShiftRGB = texUnit->_CurrentCombine->ScaleShiftRGB;
Keith Whitwell64da1612006-05-22 14:30:58 +0000360 key->unit[i].ScaleShiftA = texUnit->_CurrentCombine->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000361
362 for (j=0;j<3;j++) {
363 key->unit[i].OptRGB[j].Operand =
364 translate_operand(texUnit->_CurrentCombine->OperandRGB[j]);
365 key->unit[i].OptA[j].Operand =
366 translate_operand(texUnit->_CurrentCombine->OperandA[j]);
367 key->unit[i].OptRGB[j].Source =
368 translate_source(texUnit->_CurrentCombine->SourceRGB[j]);
369 key->unit[i].OptA[j].Source =
370 translate_source(texUnit->_CurrentCombine->SourceA[j]);
371 }
372 }
373
Keith Whitwell1680ef82008-10-03 17:30:59 +0100374 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) {
Keith Whitwellce721142005-07-11 10:10:38 +0000375 key->separate_specular = 1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100376 inputs_referenced |= FRAG_BIT_COL1;
377 }
Keith Whitwellce721142005-07-11 10:10:38 +0000378
379 if (ctx->Fog.Enabled) {
380 key->fog_enabled = 1;
381 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100382 inputs_referenced |= FRAG_BIT_FOGC; /* maybe */
Keith Whitwellce721142005-07-11 10:10:38 +0000383 }
Keith Whitwell1680ef82008-10-03 17:30:59 +0100384
385 key->inputs_available = (inputs_available & inputs_referenced);
Keith Whitwellce721142005-07-11 10:10:38 +0000386}
387
Brian Paul239617f2008-10-07 11:22:47 -0600388/**
389 * Use uregs to represent registers internally, translate to Mesa's
Keith Whitwell15e75e02005-04-29 15:11:39 +0000390 * expected formats on emit.
391 *
392 * NOTE: These are passed by value extensively in this file rather
393 * than as usual by pointer reference. If this disturbs you, try
394 * remembering they are just 32bits in size.
395 *
396 * GCC is smart enough to deal with these dword-sized structures in
397 * much the same way as if I had defined them as dwords and was using
398 * macros to access and set the fields. This is much nicer and easier
399 * to evolve.
400 */
401struct ureg {
402 GLuint file:4;
403 GLuint idx:8;
404 GLuint negatebase:1;
405 GLuint abs:1;
406 GLuint negateabs:1;
407 GLuint swz:12;
408 GLuint pad:5;
409};
410
Brian Paul13abf912006-04-13 19:17:13 +0000411static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000412 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000413 ~0,
414 0,
415 0,
416 0,
417 0,
418 0
419};
420
Keith Whitwell15e75e02005-04-29 15:11:39 +0000421
Brian Paul239617f2008-10-07 11:22:47 -0600422/** State used to build the fragment program:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000423 */
424struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000425 struct gl_fragment_program *program;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000426 GLcontext *ctx;
Keith Whitwellce721142005-07-11 10:10:38 +0000427 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000428
Brian Paul239617f2008-10-07 11:22:47 -0600429 GLbitfield alu_temps; /**< Track texture indirections, see spec. */
430 GLbitfield temps_output; /**< Track texture indirections, see spec. */
431 GLbitfield temp_in_use; /**< Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000432 GLboolean error;
433
Brian Paule9b34882008-12-31 11:54:02 -0700434 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000435 /* Reg containing each texture unit's sampled texture color,
436 * else undef.
437 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000438
Brian Paul239617f2008-10-07 11:22:47 -0600439 struct ureg src_previous; /**< Reg containing color from previous
Keith Whitwell15e75e02005-04-29 15:11:39 +0000440 * stage. May need to be decl'd.
441 */
442
Brian Paul239617f2008-10-07 11:22:47 -0600443 GLuint last_tex_stage; /**< Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000444
445 struct ureg half;
446 struct ureg one;
447 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000448};
449
450
451
452static struct ureg make_ureg(GLuint file, GLuint idx)
453{
454 struct ureg reg;
455 reg.file = file;
456 reg.idx = idx;
457 reg.negatebase = 0;
458 reg.abs = 0;
459 reg.negateabs = 0;
460 reg.swz = SWIZZLE_NOOP;
461 reg.pad = 0;
462 return reg;
463}
464
465static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
466{
467 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
468 GET_SWZ(reg.swz, y),
469 GET_SWZ(reg.swz, z),
470 GET_SWZ(reg.swz, w));
471
472 return reg;
473}
474
475static struct ureg swizzle1( struct ureg reg, int x )
476{
477 return swizzle(reg, x, x, x, x);
478}
479
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000480static struct ureg negate( struct ureg reg )
481{
482 reg.negatebase ^= 1;
483 return reg;
484}
485
Keith Whitwell15e75e02005-04-29 15:11:39 +0000486static GLboolean is_undef( struct ureg reg )
487{
Alan Hourihane8d972652006-08-10 13:12:00 +0000488 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000489}
490
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000491
Keith Whitwell15e75e02005-04-29 15:11:39 +0000492static struct ureg get_temp( struct texenv_fragment_program *p )
493{
Brian Paul13abf912006-04-13 19:17:13 +0000494 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000495
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000496 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000497 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000498 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000499
500 /* Then any unused temporary:
501 */
502 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000503 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000504
Keith Whitwell15e75e02005-04-29 15:11:39 +0000505 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000506 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000507 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000508 }
509
Brian Paul13abf912006-04-13 19:17:13 +0000510 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000511 p->program->Base.NumTemporaries = bit;
512
Keith Whitwell93cd9232005-05-11 08:30:23 +0000513 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000514 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
515}
516
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000517static struct ureg get_tex_temp( struct texenv_fragment_program *p )
518{
519 int bit;
520
Brian Pauld01269a2008-09-25 18:27:22 -0600521 /* First try to find available temp not previously used (to avoid
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000522 * starting a new texture indirection). According to the spec, the
523 * ~p->temps_output isn't necessary, but will keep it there for
524 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000525 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000526 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000527
528 /* Then any unused temporary:
529 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000530 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000531 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000532
533 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000534 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000535 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000536 }
537
Brian Paul13abf912006-04-13 19:17:13 +0000538 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000539 p->program->Base.NumTemporaries = bit;
540
Keith Whitwell93cd9232005-05-11 08:30:23 +0000541 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000542 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
543}
544
Keith Whitwell15e75e02005-04-29 15:11:39 +0000545
Brian Paul5620c202008-09-26 11:18:06 -0600546/** Mark a temp reg as being no longer allocatable. */
547static void reserve_temp( struct texenv_fragment_program *p, struct ureg r )
548{
549 if (r.file == PROGRAM_TEMPORARY)
550 p->temps_output |= (1 << r.idx);
551}
552
553
Brianf18d4e02007-10-29 12:25:46 -0600554static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000555{
Brianf18d4e02007-10-29 12:25:46 -0600556 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000557
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000558 /* KW: To support tex_env_crossbar, don't release the registers in
559 * temps_output.
560 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000561 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000562 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000563 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000564 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000565}
566
567
Brian9fe3e2e2007-02-23 11:44:14 -0700568static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000569 GLint s0,
570 GLint s1,
571 GLint s2,
572 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700573 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000574{
Brian9fe3e2e2007-02-23 11:44:14 -0700575 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000576 GLuint idx;
577 tokens[0] = s0;
578 tokens[1] = s1;
579 tokens[2] = s2;
580 tokens[3] = s3;
581 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000582 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000583 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000584}
585
Keith Whitwell47b29f52005-05-04 11:44:44 +0000586
Brian9fe3e2e2007-02-23 11:44:14 -0700587#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
588#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
589#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
590#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000591
Keith Whitwell1680ef82008-10-03 17:30:59 +0100592static GLuint frag_to_vert_attrib( GLuint attrib )
593{
594 switch (attrib) {
595 case FRAG_ATTRIB_COL0: return VERT_ATTRIB_COLOR0;
596 case FRAG_ATTRIB_COL1: return VERT_ATTRIB_COLOR1;
597 default:
598 assert(attrib >= FRAG_ATTRIB_TEX0);
599 assert(attrib <= FRAG_ATTRIB_TEX7);
600 return attrib - FRAG_ATTRIB_TEX0 + VERT_ATTRIB_TEX0;
601 }
602}
603
Keith Whitwell47b29f52005-05-04 11:44:44 +0000604
605static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
606{
Keith Whitwell1680ef82008-10-03 17:30:59 +0100607 if (p->state->inputs_available & (1<<input)) {
608 p->program->Base.InputsRead |= (1 << input);
609 return make_ureg(PROGRAM_INPUT, input);
610 }
611 else {
612 GLuint idx = frag_to_vert_attrib( input );
613 return register_param3( p, STATE_INTERNAL, STATE_CURRENT_ATTRIB, idx );
614 }
Keith Whitwell47b29f52005-05-04 11:44:44 +0000615}
616
617
Brian Paul7e807512005-11-05 17:10:45 +0000618static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000619 struct ureg ureg )
620{
621 reg->File = ureg.file;
622 reg->Index = ureg.idx;
623 reg->Swizzle = ureg.swz;
Keith Whitwellf2802c42005-10-07 09:55:26 +0000624 reg->NegateBase = ureg.negatebase ? 0xf : 0x0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000625 reg->Abs = ureg.abs;
626 reg->NegateAbs = ureg.negateabs;
627}
628
Brian Paul7e807512005-11-05 17:10:45 +0000629static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000630 struct ureg ureg, GLuint mask )
631{
632 dst->File = ureg.file;
633 dst->Index = ureg.idx;
634 dst->WriteMask = mask;
Brianc9d495c2007-10-23 10:55:24 -0600635 dst->CondMask = COND_TR; /* always pass cond test */
636 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000637}
638
Brian Paul7e807512005-11-05 17:10:45 +0000639static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000640emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000641 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000642 struct ureg dest,
643 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000644 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000645 struct ureg src0,
646 struct ureg src1,
647 struct ureg src2 )
648{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000649 GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000650 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Brian2a8e9bb2007-10-23 10:24:53 -0600651
652 assert(nr < MAX_INSTRUCTIONS);
653
Brian Pauld6272e02006-10-29 18:03:16 +0000654 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000655 inst->Opcode = op;
656
Keith Whitwell47b29f52005-05-04 11:44:44 +0000657 emit_arg( &inst->SrcReg[0], src0 );
658 emit_arg( &inst->SrcReg[1], src1 );
659 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000660
Brian Paule31ac052005-11-20 17:52:40 +0000661 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000662
663 emit_dst( &inst->DstReg, dest, mask );
664
Brian Paul5620c202008-09-26 11:18:06 -0600665#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000666 /* Accounting for indirection tracking:
667 */
668 if (dest.file == PROGRAM_TEMPORARY)
669 p->temps_output |= 1 << dest.idx;
Brian Paul5620c202008-09-26 11:18:06 -0600670#endif
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000671
Keith Whitwell15e75e02005-04-29 15:11:39 +0000672 return inst;
673}
674
675
676static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000677 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000678 struct ureg dest,
679 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000680 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000681 struct ureg src0,
682 struct ureg src1,
683 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000684{
685 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
686
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000687 /* Accounting for indirection tracking:
688 */
689 if (src0.file == PROGRAM_TEMPORARY)
690 p->alu_temps |= 1 << src0.idx;
691
692 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
693 p->alu_temps |= 1 << src1.idx;
694
695 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
696 p->alu_temps |= 1 << src2.idx;
697
698 if (dest.file == PROGRAM_TEMPORARY)
699 p->alu_temps |= 1 << dest.idx;
700
Brian21f99792007-01-09 11:00:21 -0700701 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000702 return dest;
703}
704
705static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000706 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000707 struct ureg dest,
708 GLuint destmask,
709 GLuint tex_unit,
710 GLuint tex_idx,
711 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000712{
Brian Paul7e807512005-11-05 17:10:45 +0000713 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000714 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000715 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000716 coord, /* arg 0? */
717 undef,
718 undef);
719
Brian Paul7e807512005-11-05 17:10:45 +0000720 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000721 inst->TexSrcUnit = tex_unit;
722
Brian21f99792007-01-09 11:00:21 -0700723 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000724
Brian Paul5620c202008-09-26 11:18:06 -0600725 /* Accounting for indirection tracking:
726 */
727 reserve_temp(p, dest);
728
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000729 /* Is this a texture indirection?
730 */
731 if ((coord.file == PROGRAM_TEMPORARY &&
732 (p->temps_output & (1<<coord.idx))) ||
733 (dest.file == PROGRAM_TEMPORARY &&
734 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700735 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000736 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000737 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000738 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000739 }
740
741 return dest;
742}
743
744
Keith Whitwell47b29f52005-05-04 11:44:44 +0000745static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000746 GLfloat s0,
747 GLfloat s1,
748 GLfloat s2,
749 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000750{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000751 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700752 GLuint idx, swizzle;
Brian Pauld01269a2008-09-25 18:27:22 -0600753 struct ureg r;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000754 values[0] = s0;
755 values[1] = s1;
756 values[2] = s2;
757 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700758 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
759 &swizzle );
Brian Pauld01269a2008-09-25 18:27:22 -0600760 r = make_ureg(PROGRAM_CONSTANT, idx);
761 r.swz = swizzle;
762 return r;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000763}
764
Keith Whitwelle4902422005-05-11 15:16:35 +0000765#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000766#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
767#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
768#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000769
770
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000771static struct ureg get_one( struct texenv_fragment_program *p )
772{
773 if (is_undef(p->one))
774 p->one = register_scalar_const(p, 1.0);
775 return p->one;
776}
777
778static struct ureg get_half( struct texenv_fragment_program *p )
779{
780 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000781 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000782 return p->half;
783}
784
785static struct ureg get_zero( struct texenv_fragment_program *p )
786{
787 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000788 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000789 return p->zero;
790}
791
Keith Whitwell15e75e02005-04-29 15:11:39 +0000792
Keith Whitwell15e75e02005-04-29 15:11:39 +0000793static void program_error( struct texenv_fragment_program *p, const char *msg )
794{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000795 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000796 p->error = 1;
797}
798
Keith Whitwell15e75e02005-04-29 15:11:39 +0000799static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000800 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000801{
802 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000803 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000804 assert(!is_undef(p->src_texture[unit]));
805 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000806
Keith Whitwellce721142005-07-11 10:10:38 +0000807 case SRC_TEXTURE0:
808 case SRC_TEXTURE1:
809 case SRC_TEXTURE2:
810 case SRC_TEXTURE3:
811 case SRC_TEXTURE4:
812 case SRC_TEXTURE5:
813 case SRC_TEXTURE6:
814 case SRC_TEXTURE7:
815 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
816 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000817
Keith Whitwellce721142005-07-11 10:10:38 +0000818 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000819 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000820
Keith Whitwellce721142005-07-11 10:10:38 +0000821 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000822 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000823
Keith Whitwellce721142005-07-11 10:10:38 +0000824 case SRC_PREVIOUS:
825 default:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000826 if (is_undef(p->src_previous))
827 return register_input(p, FRAG_ATTRIB_COL0);
828 else
829 return p->src_previous;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000830 }
831}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000832
Keith Whitwell15e75e02005-04-29 15:11:39 +0000833static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000834 GLuint mask,
835 GLuint unit,
836 GLuint source,
837 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000838{
839 struct ureg arg, src, one;
840
841 src = get_source(p, source, unit);
842
843 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000844 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000845 /* Get unused tmp,
846 * Emit tmp = 1.0 - arg.xyzw
847 */
848 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000849 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000850 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000851
Keith Whitwellce721142005-07-11 10:10:38 +0000852 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000853 if (mask == WRITEMASK_W)
854 return src;
855 else
Brian Paul22db5352005-11-19 23:45:10 +0000856 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000857 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000858 /* Get unused tmp,
859 * Emit tmp = 1.0 - arg.wwww
860 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000861 arg = get_temp(p);
862 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000863 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000864 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000865 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000866 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000867 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000868 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000869 case OPR_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000870 default:
871 return src;
872 }
873}
874
Keith Whitwellce721142005-07-11 10:10:38 +0000875static GLboolean args_match( struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000876{
Brian Paul22db5352005-11-19 23:45:10 +0000877 GLuint i, nr = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000878
879 for (i = 0 ; i < nr ; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000880 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000881 return GL_FALSE;
882
Keith Whitwellce721142005-07-11 10:10:38 +0000883 switch(key->unit[unit].OptA[i].Operand) {
884 case OPR_SRC_ALPHA:
885 switch(key->unit[unit].OptRGB[i].Operand) {
886 case OPR_SRC_COLOR:
887 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000888 break;
889 default:
890 return GL_FALSE;
891 }
892 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000893 case OPR_ONE_MINUS_SRC_ALPHA:
894 switch(key->unit[unit].OptRGB[i].Operand) {
895 case OPR_ONE_MINUS_SRC_COLOR:
896 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000897 break;
898 default:
899 return GL_FALSE;
900 }
901 break;
902 default:
903 return GL_FALSE; /* impossible */
904 }
905 }
906
907 return GL_TRUE;
908}
909
Keith Whitwell15e75e02005-04-29 15:11:39 +0000910static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000911 struct ureg dest,
912 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000913 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000914 GLuint unit,
915 GLuint nr,
916 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +0000917 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000918{
Keith Whitwell15e75e02005-04-29 15:11:39 +0000919 struct ureg src[3];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000920 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +0000921 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000922
Brian Paul00630842005-12-12 15:27:55 +0000923 tmp = undef; /* silence warning (bug 5318) */
924
Keith Whitwell15e75e02005-04-29 15:11:39 +0000925 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +0000926 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000927
928 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +0000929 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000930 if (mask == WRITEMASK_XYZW && !saturate)
931 return src[0];
932 else
Brian Paul7e807512005-11-05 17:10:45 +0000933 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000934 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +0000935 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000936 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000937 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +0000938 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000939 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000940 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000941 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000942 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +0000943 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000944 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +0000945 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +0000946 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
947 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000948 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +0000949 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000950 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
951 */
Brian Paul7e807512005-11-05 17:10:45 +0000952 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000953
Keith Whitwellce721142005-07-11 10:10:38 +0000954 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +0000955 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000956
Keith Whitwellce721142005-07-11 10:10:38 +0000957 case MODE_DOT3_RGBA:
958 case MODE_DOT3_RGBA_EXT:
959 case MODE_DOT3_RGB_EXT:
960 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +0000961 struct ureg tmp0 = get_temp( p );
962 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +0000963 struct ureg neg1 = register_scalar_const(p, -1);
964 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000965
966 /* tmp0 = 2*src0 - 1
967 * tmp1 = 2*src1 - 1
968 *
969 * dst = tmp0 dot3 tmp1
970 */
Brian Paul7e807512005-11-05 17:10:45 +0000971 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000972 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000973
Brian Paulaa206952005-09-16 18:14:24 +0000974 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000975 tmp1 = tmp0;
976 else
Brian Paul7e807512005-11-05 17:10:45 +0000977 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000978 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +0000979 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000980 return dest;
981 }
Keith Whitwellce721142005-07-11 10:10:38 +0000982 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000983 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +0000984 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000985 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +0000986 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000987 /* Arg0 * Arg2 + Arg1 - 0.5 */
988 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000989 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +0000990 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
991 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000992 return dest;
993 }
Keith Whitwellce721142005-07-11 10:10:38 +0000994 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000995 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +0000996 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000997 return dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000998 default:
999 return src[0];
1000 }
1001}
1002
Keith Whitwell15e75e02005-04-29 15:11:39 +00001003
Brian Paul22db5352005-11-19 23:45:10 +00001004/**
1005 * Generate instructions for one texture unit's env/combiner mode.
1006 */
1007static struct ureg
1008emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001009{
Keith Whitwellce721142005-07-11 10:10:38 +00001010 struct state_key *key = p->state;
Brian Paul22db5352005-11-19 23:45:10 +00001011 GLboolean saturate = (unit < p->last_tex_stage);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001012 GLuint rgb_shift, alpha_shift;
1013 struct ureg out, shift;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001014 struct ureg dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001015
Keith Whitwellce721142005-07-11 10:10:38 +00001016 if (!key->unit[unit].enabled) {
1017 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001018 }
Keith Whitwellce721142005-07-11 10:10:38 +00001019
1020 switch (key->unit[unit].ModeRGB) {
1021 case MODE_DOT3_RGB_EXT:
1022 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001023 rgb_shift = 0;
1024 break;
Keith Whitwellce721142005-07-11 10:10:38 +00001025 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001026 alpha_shift = 0;
1027 rgb_shift = 0;
1028 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001029 default:
Keith Whitwellce721142005-07-11 10:10:38 +00001030 rgb_shift = key->unit[unit].ScaleShiftRGB;
1031 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001032 break;
1033 }
Keith Whitwellce721142005-07-11 10:10:38 +00001034
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001035 /* If this is the very last calculation, emit direct to output reg:
1036 */
Keith Whitwellce721142005-07-11 10:10:38 +00001037 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001038 unit != p->last_tex_stage ||
1039 alpha_shift ||
1040 rgb_shift)
1041 dest = get_temp( p );
1042 else
Brian Paul90ebb582005-11-02 18:06:12 +00001043 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLR);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001044
1045 /* Emit the RGB and A combine ops
1046 */
Keith Whitwellce721142005-07-11 10:10:38 +00001047 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
1048 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001049 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1050 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001051 key->unit[unit].NumArgsRGB,
1052 key->unit[unit].ModeRGB,
1053 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001054 }
Keith Whitwellce721142005-07-11 10:10:38 +00001055 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +02001056 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001057
1058 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1059 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001060 key->unit[unit].NumArgsRGB,
1061 key->unit[unit].ModeRGB,
1062 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001063 }
1064 else {
1065 /* Need to do something to stop from re-emitting identical
1066 * argument calculations here:
1067 */
1068 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
1069 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001070 key->unit[unit].NumArgsRGB,
1071 key->unit[unit].ModeRGB,
1072 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001073 out = emit_combine( p, dest, WRITEMASK_W, saturate,
1074 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001075 key->unit[unit].NumArgsA,
1076 key->unit[unit].ModeA,
1077 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001078 }
1079
1080 /* Deal with the final shift:
1081 */
1082 if (alpha_shift || rgb_shift) {
1083 if (rgb_shift == alpha_shift) {
José Fonseca53174af2008-05-31 18:14:09 +09001084 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001085 }
1086 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001087 shift = register_const4f(p,
José Fonseca53174af2008-05-31 18:14:09 +09001088 (GLfloat)(1<<rgb_shift),
1089 (GLfloat)(1<<rgb_shift),
1090 (GLfloat)(1<<rgb_shift),
1091 (GLfloat)(1<<alpha_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001092 }
Brian Paul7e807512005-11-05 17:10:45 +00001093 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001094 saturate, out, shift, undef );
1095 }
1096 else
1097 return out;
1098}
1099
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001100
Brian Paul22db5352005-11-19 23:45:10 +00001101/**
1102 * Generate instruction for getting a texture source term.
1103 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001104static void load_texture( struct texenv_fragment_program *p, GLuint unit )
1105{
1106 if (is_undef(p->src_texture[unit])) {
Keith Whitwellce721142005-07-11 10:10:38 +00001107 GLuint dim = p->state->unit[unit].source_index;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001108 struct ureg texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
1109 struct ureg tmp = get_tex_temp( p );
1110
Brian Paulbfb5ea32005-07-19 18:20:04 +00001111 if (dim == TEXTURE_UNKNOWN_INDEX)
1112 program_error(p, "TexSrcBit");
Keith Whitwellce721142005-07-11 10:10:38 +00001113
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001114 /* TODO: Use D0_MASK_XY where possible.
1115 */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +02001116 if (p->state->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001117 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1118 tmp, WRITEMASK_XYZW,
1119 unit, dim, texcoord );
Keith Whitwell0397b2b2008-09-11 16:05:15 +01001120
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +02001121 if (p->state->unit[unit].shadow)
1122 p->program->Base.ShadowSamplers |= 1 << unit;
Keith Whitwell0397b2b2008-09-11 16:05:15 +01001123
Brian1e3b07f2007-12-14 11:16:49 -07001124 p->program->Base.SamplersUsed |= (1 << unit);
Brianfce46122007-12-14 11:42:28 -07001125 /* This identity mapping should already be in place
1126 * (see _mesa_init_program_struct()) but let's be safe.
1127 */
1128 p->program->Base.SamplerUnits[unit] = unit;
Brian1e3b07f2007-12-14 11:16:49 -07001129 }
Aapo Tahkolab8915342006-03-28 10:26:34 +00001130 else
1131 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001132 }
1133}
1134
Keith Whitwell241b6b72005-05-23 09:50:34 +00001135static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +00001136 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001137{
1138 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +00001139 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001140 load_texture(p, unit);
1141 break;
1142
Keith Whitwellce721142005-07-11 10:10:38 +00001143 case SRC_TEXTURE0:
1144 case SRC_TEXTURE1:
1145 case SRC_TEXTURE2:
1146 case SRC_TEXTURE3:
1147 case SRC_TEXTURE4:
1148 case SRC_TEXTURE5:
1149 case SRC_TEXTURE6:
1150 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +00001151 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001152 break;
1153
1154 default:
1155 break;
1156 }
Keith Whitwell241b6b72005-05-23 09:50:34 +00001157
1158 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001159}
1160
Brian Paul22db5352005-11-19 23:45:10 +00001161
1162/**
1163 * Generate instructions for loading all texture source terms.
1164 */
1165static GLboolean
1166load_texunit_sources( struct texenv_fragment_program *p, int unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001167{
Keith Whitwellce721142005-07-11 10:10:38 +00001168 struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +00001169 GLuint i;
1170
1171 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
1172 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001173 }
Aapo Tahkolab8915342006-03-28 10:26:34 +00001174
1175 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1176 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1177 }
1178
Keith Whitwell241b6b72005-05-23 09:50:34 +00001179 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001180}
1181
Brian Paul22db5352005-11-19 23:45:10 +00001182
1183/**
1184 * Generate a new fragment program which implements the context's
1185 * current texture env/combine mode.
1186 */
1187static void
Brian Paule998c342006-10-29 21:17:18 +00001188create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001189 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001190{
Brian Paule998c342006-10-29 21:17:18 +00001191 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001192 struct texenv_fragment_program p;
1193 GLuint unit;
1194 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001195
Keith Whitwelle4902422005-05-11 15:16:35 +00001196 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +00001197 p.ctx = ctx;
Keith Whitwellce721142005-07-11 10:10:38 +00001198 p.state = key;
1199 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001200
Brian Paule998c342006-10-29 21:17:18 +00001201 /* During code generation, use locally-allocated instruction buffer,
1202 * then alloc dynamic storage below.
1203 */
1204 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001205 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Brian21f99792007-01-09 11:00:21 -07001206 p.program->Base.NumTexIndirections = 1; /* correct? */
1207 p.program->Base.NumTexInstructions = 0;
1208 p.program->Base.NumAluInstructions = 0;
Brian1240eb22007-03-22 08:50:20 -06001209 p.program->Base.String = NULL;
Keith Whitwell9ca88152005-05-10 09:56:02 +00001210 p.program->Base.NumInstructions =
Brian Paule998c342006-10-29 21:17:18 +00001211 p.program->Base.NumTemporaries =
1212 p.program->Base.NumParameters =
1213 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001214 p.program->Base.Parameters = _mesa_new_parameter_list();
Keith Whitwelldbeea252005-05-10 13:57:50 +00001215
Brian Paulde997602005-11-12 17:53:14 +00001216 p.program->Base.InputsRead = 0;
1217 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001218
Brian Paule9b34882008-12-31 11:54:02 -07001219 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001220 p.src_texture[unit] = undef;
1221
Keith Whitwell47b29f52005-05-04 11:44:44 +00001222 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001223 p.half = undef;
1224 p.zero = undef;
1225 p.one = undef;
1226
Keith Whitwell15e75e02005-04-29 15:11:39 +00001227 p.last_tex_stage = 0;
Brianf18d4e02007-10-29 12:25:46 -06001228 release_temps(ctx, &p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001229
Keith Whitwellce721142005-07-11 10:10:38 +00001230 if (key->enabled_units) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001231 /* First pass - to support texture_env_crossbar, first identify
1232 * all referenced texture sources and emit texld instructions
1233 * for each:
1234 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001235 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001236 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001237 load_texunit_sources( &p, unit );
1238 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001239 }
1240
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001241 /* Second pass - emit combine instructions to build final color:
1242 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001243 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001244 if (key->enabled_units & (1<<unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001245 p.src_previous = emit_texenv( &p, unit );
Brian Paul5620c202008-09-26 11:18:06 -06001246 reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
Brianf18d4e02007-10-29 12:25:46 -06001247 release_temps(ctx, &p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001248 }
1249 }
1250
Keith Whitwellce721142005-07-11 10:10:38 +00001251 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul90ebb582005-11-02 18:06:12 +00001252 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001253
Keith Whitwellce721142005-07-11 10:10:38 +00001254 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001255 /* Emit specular add.
1256 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001257 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001258 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1259 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001260 }
Brian Paulaa206952005-09-16 18:14:24 +00001261 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001262 /* Will wind up in here if no texture enabled or a couple of
1263 * other scenarios (GL_REPLACE for instance).
1264 */
Brian Paul7e807512005-11-05 17:10:45 +00001265 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001266 }
1267
Keith Whitwell47b29f52005-05-04 11:44:44 +00001268 /* Finish up:
1269 */
Brian Paul7e807512005-11-05 17:10:45 +00001270 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001271
Keith Whitwellce721142005-07-11 10:10:38 +00001272 if (key->fog_enabled) {
1273 /* Pull fog mode from GLcontext, the value in the state key is
1274 * a reduced value and not what is expected in FogOption
1275 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001276 p.program->FogOption = ctx->Fog.Mode;
Brian63be96b2007-09-18 19:29:26 -06001277 p.program->Base.InputsRead |= FRAG_BIT_FOGC; /* XXX new */
Keith Whitwellce721142005-07-11 10:10:38 +00001278 } else
Keith Whitwell276330b2005-05-09 17:58:13 +00001279 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001280
Brian21f99792007-01-09 11:00:21 -07001281 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001282 program_error(&p, "Exceeded max nr indirect texture lookups");
1283
Brian21f99792007-01-09 11:00:21 -07001284 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001285 program_error(&p, "Exceeded max TEX instructions");
1286
Brian21f99792007-01-09 11:00:21 -07001287 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001288 program_error(&p, "Exceeded max ALU instructions");
1289
Brian Paul5d7b49f2005-11-19 23:12:20 +00001290 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001291
Brian Paule998c342006-10-29 21:17:18 +00001292 /* Allocate final instruction array */
Brian3bf8d2a2007-09-25 15:18:51 -06001293 p.program->Base.Instructions
1294 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1295 if (!p.program->Base.Instructions) {
Brian Paule998c342006-10-29 21:17:18 +00001296 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1297 "generating tex env program");
1298 return;
1299 }
Brian3bf8d2a2007-09-25 15:18:51 -06001300 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1301 p.program->Base.NumInstructions);
1302
1303 if (p.program->FogOption) {
1304 _mesa_append_fog_code(ctx, p.program);
1305 p.program->FogOption = GL_NONE;
1306 }
1307
Brian Paule998c342006-10-29 21:17:18 +00001308
Keith Whitwelldbeea252005-05-10 13:57:50 +00001309 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001310 */
Brian Paule998c342006-10-29 21:17:18 +00001311 if (ctx->Driver.ProgramStringNotify) {
1312 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1313 &p.program->Base );
1314 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001315
Brian Paule998c342006-10-29 21:17:18 +00001316 if (DISASSEM) {
1317 _mesa_print_program(&p.program->Base);
1318 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001319 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001320}
1321
Brian Paul5d7b49f2005-11-19 23:12:20 +00001322
Briana90046f2006-12-15 10:07:26 -07001323/**
Brian83ce6c52007-10-29 11:23:02 -06001324 * Return a fragment program which implements the current
1325 * fixed-function texture, fog and color-sum operations.
1326 */
1327struct gl_fragment_program *
1328_mesa_get_fixed_func_fragment_program(GLcontext *ctx)
Keith Whitwellce721142005-07-11 10:10:38 +00001329{
Brian83ce6c52007-10-29 11:23:02 -06001330 struct gl_fragment_program *prog;
1331 struct state_key key;
Brian83ce6c52007-10-29 11:23:02 -06001332
1333 make_state_key(ctx, &key);
Brian83ce6c52007-10-29 11:23:02 -06001334
Brianf4a5ea22007-10-31 12:45:32 -06001335 prog = (struct gl_fragment_program *)
1336 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1337 &key, sizeof(key));
Keith Whitwellce721142005-07-11 10:10:38 +00001338
Brian83ce6c52007-10-29 11:23:02 -06001339 if (!prog) {
Brian83ce6c52007-10-29 11:23:02 -06001340 prog = (struct gl_fragment_program *)
1341 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1342
1343 create_new_program(ctx, &key, prog);
1344
Brianf4a5ea22007-10-31 12:45:32 -06001345 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1346 &key, sizeof(key), &prog->Base);
Keith Whitwellce721142005-07-11 10:10:38 +00001347 }
1348
Brian83ce6c52007-10-29 11:23:02 -06001349 return prog;
Keith Whitwellce721142005-07-11 10:10:38 +00001350}
1351
Keith Whitwellce721142005-07-11 10:10:38 +00001352
Briana90046f2006-12-15 10:07:26 -07001353
1354/**
1355 * If _MaintainTexEnvProgram is set we'll generate a fragment program that
1356 * implements the current texture env/combine mode.
1357 * This function generates that program and puts it into effect.
Keith Whitwellaf74aba2008-09-12 10:04:56 +01001358 *
1359 * XXX: remove this function. currently only called by some drivers,
1360 * not by mesa core. We now handle this properly from inside mesa.
Briana90046f2006-12-15 10:07:26 -07001361 */
1362void
1363_mesa_UpdateTexEnvProgram( GLcontext *ctx )
Keith Whitwellce721142005-07-11 10:10:38 +00001364{
Brian Paul122629f2006-07-20 16:49:57 +00001365 const struct gl_fragment_program *prev = ctx->FragmentProgram._Current;
Keith Whitwellce721142005-07-11 10:10:38 +00001366
Briana90046f2006-12-15 10:07:26 -07001367 ASSERT(ctx->FragmentProgram._MaintainTexEnvProgram);
1368
1369 /* If a conventional fragment program/shader isn't in effect... */
Brianefcfdbd2007-02-24 15:51:41 -07001370 if (!ctx->FragmentProgram._Enabled &&
Briand781cdc2007-10-02 16:55:21 -06001371 (!ctx->Shader.CurrentProgram ||
Keith Whitwell0397b2b2008-09-11 16:05:15 +01001372 !ctx->Shader.CurrentProgram->FragmentProgram) )
1373 {
Brian Paul5b5c9312008-05-07 18:51:44 -06001374 struct gl_fragment_program *newProg;
1375
Keith Whitwell0397b2b2008-09-11 16:05:15 +01001376 newProg = _mesa_get_fixed_func_fragment_program(ctx);
Brian Paul5b5c9312008-05-07 18:51:44 -06001377
1378 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current, newProg);
1379 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._TexEnvProgram, newProg);
Keith Whitwellc3626a92005-11-01 17:25:49 +00001380 }
Keith Whitwellc3626a92005-11-01 17:25:49 +00001381
1382 /* Tell the driver about the change. Could define a new target for
1383 * this?
1384 */
Brian Paul5d7b49f2005-11-19 23:12:20 +00001385 if (ctx->FragmentProgram._Current != prev && ctx->Driver.BindProgram) {
1386 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
Briana90046f2006-12-15 10:07:26 -07001387 (struct gl_program *) ctx->FragmentProgram._Current);
Brian Paul5d7b49f2005-11-19 23:12:20 +00001388 }
Keith Whitwellce721142005-07-11 10:10:38 +00001389}