blob: b92ba2542d221001c78f0b516ae25631d362b7d3 [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 Paule9b34882008-12-31 11:54:02 -070042/*
43 * Note on texture units:
44 *
45 * The number of texture units supported by fixed-function fragment
46 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
47 * That's because there's a one-to-one correspondence between texture
48 * coordinates and samplers in fixed-function processing.
49 *
50 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
51 * sets of texcoords, so is fixed-function fragment processing.
52 *
53 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
54 */
55
56
Brian Paul5b5c9312008-05-07 18:51:44 -060057struct texenvprog_cache_item
58{
59 GLuint hash;
60 void *key;
61 struct gl_fragment_program *data;
62 struct texenvprog_cache_item *next;
63};
64
65
Brian Paule998c342006-10-29 21:17:18 +000066/**
Brian Paulb5e1a932008-09-25 18:40:16 -060067 * Up to nine instructions per tex unit, plus fog, specular color.
Brian Paule998c342006-10-29 21:17:18 +000068 */
Brian Paul0815ebc2009-01-02 16:32:26 -070069#define MAX_INSTRUCTIONS ((MAX_TEXTURE_COORD_UNITS * 9) + 12)
Keith Whitwell15e75e02005-04-29 15:11:39 +000070
Keith Whitwell269e3892005-05-12 10:28:43 +000071#define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
Keith Whitwell15e75e02005-04-29 15:11:39 +000072
Keith Whitwellce721142005-07-11 10:10:38 +000073struct mode_opt {
Brian Paul5d7b49f2005-11-19 23:12:20 +000074 GLuint Source:4;
75 GLuint Operand:3;
Keith Whitwellce721142005-07-11 10:10:38 +000076};
77
78struct state_key {
Keith Whitwell6280e332008-10-03 13:51:56 +010079 GLuint nr_enabled_units:8;
80 GLuint enabled_units:8;
Brian Paul5d7b49f2005-11-19 23:12:20 +000081 GLuint separate_specular:1;
82 GLuint fog_enabled:1;
83 GLuint fog_mode:2;
Keith Whitwell6280e332008-10-03 13:51:56 +010084 GLuint inputs_available:12;
Keith Whitwellce721142005-07-11 10:10:38 +000085
86 struct {
Brian Paul5d7b49f2005-11-19 23:12:20 +000087 GLuint enabled:1;
88 GLuint source_index:3; /* one of TEXTURE_1D/2D/3D/CUBE/RECT_INDEX */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +020089 GLuint shadow:1;
Brian Paul5d7b49f2005-11-19 23:12:20 +000090 GLuint ScaleShiftRGB:2;
91 GLuint ScaleShiftA:2;
Keith Whitwellce721142005-07-11 10:10:38 +000092
Brian Paul6947f852009-01-23 17:32:09 -070093 GLuint NumArgsRGB:3;
Roland Scheidegger114152e2009-03-12 15:01:16 +010094 GLuint ModeRGB:5;
Brian Paulbd9b2be2009-03-08 17:58:54 -060095 struct mode_opt OptRGB[MAX_COMBINER_TERMS];
Keith Whitwellce721142005-07-11 10:10:38 +000096
Brian Paul6947f852009-01-23 17:32:09 -070097 GLuint NumArgsA:3;
Roland Scheidegger114152e2009-03-12 15:01:16 +010098 GLuint ModeA:5;
Brian Paulbd9b2be2009-03-08 17:58:54 -060099 struct mode_opt OptA[MAX_COMBINER_TERMS];
Keith Whitwellce721142005-07-11 10:10:38 +0000100 } unit[8];
101};
102
103#define FOG_LINEAR 0
104#define FOG_EXP 1
105#define FOG_EXP2 2
106#define FOG_UNKNOWN 3
107
108static GLuint translate_fog_mode( GLenum mode )
109{
110 switch (mode) {
111 case GL_LINEAR: return FOG_LINEAR;
112 case GL_EXP: return FOG_EXP;
113 case GL_EXP2: return FOG_EXP2;
114 default: return FOG_UNKNOWN;
115 }
116}
117
118#define OPR_SRC_COLOR 0
119#define OPR_ONE_MINUS_SRC_COLOR 1
120#define OPR_SRC_ALPHA 2
121#define OPR_ONE_MINUS_SRC_ALPHA 3
122#define OPR_ZERO 4
123#define OPR_ONE 5
124#define OPR_UNKNOWN 7
125
126static GLuint translate_operand( GLenum operand )
127{
128 switch (operand) {
129 case GL_SRC_COLOR: return OPR_SRC_COLOR;
130 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
131 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
132 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
133 case GL_ZERO: return OPR_ZERO;
134 case GL_ONE: return OPR_ONE;
Brian Paul6947f852009-01-23 17:32:09 -0700135 default:
136 assert(0);
137 return OPR_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000138 }
139}
140
141#define SRC_TEXTURE 0
142#define SRC_TEXTURE0 1
143#define SRC_TEXTURE1 2
144#define SRC_TEXTURE2 3
145#define SRC_TEXTURE3 4
146#define SRC_TEXTURE4 5
147#define SRC_TEXTURE5 6
148#define SRC_TEXTURE6 7
149#define SRC_TEXTURE7 8
150#define SRC_CONSTANT 9
151#define SRC_PRIMARY_COLOR 10
152#define SRC_PREVIOUS 11
Brian Paul6947f852009-01-23 17:32:09 -0700153#define SRC_ZERO 12
Keith Whitwellce721142005-07-11 10:10:38 +0000154#define SRC_UNKNOWN 15
155
156static GLuint translate_source( GLenum src )
157{
158 switch (src) {
159 case GL_TEXTURE: return SRC_TEXTURE;
160 case GL_TEXTURE0:
161 case GL_TEXTURE1:
162 case GL_TEXTURE2:
163 case GL_TEXTURE3:
164 case GL_TEXTURE4:
165 case GL_TEXTURE5:
166 case GL_TEXTURE6:
167 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
168 case GL_CONSTANT: return SRC_CONSTANT;
169 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
170 case GL_PREVIOUS: return SRC_PREVIOUS;
Brian Paul6947f852009-01-23 17:32:09 -0700171 case GL_ZERO:
172 return SRC_ZERO;
173 default:
174 assert(0);
175 return SRC_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000176 }
177}
178
Brian Paul6947f852009-01-23 17:32:09 -0700179#define MODE_REPLACE 0 /* r = a0 */
180#define MODE_MODULATE 1 /* r = a0 * a1 */
181#define MODE_ADD 2 /* r = a0 + a1 */
182#define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
183#define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
184#define MODE_SUBTRACT 5 /* r = a0 - a1 */
185#define MODE_DOT3_RGB 6 /* r = a0 . a1 */
186#define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
187#define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
188#define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
189#define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
190#define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
191#define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
192#define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
193#define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
Roland Scheidegger114152e2009-03-12 15:01:16 +0100194#define MODE_BUMP_ENVMAP_ATI 15 /* special */
195#define MODE_UNKNOWN 16
Keith Whitwellce721142005-07-11 10:10:38 +0000196
Brian Paul6947f852009-01-23 17:32:09 -0700197/**
198 * Translate GL combiner state into a MODE_x value
199 */
200static GLuint translate_mode( GLenum envMode, GLenum mode )
Keith Whitwellce721142005-07-11 10:10:38 +0000201{
202 switch (mode) {
203 case GL_REPLACE: return MODE_REPLACE;
204 case GL_MODULATE: return MODE_MODULATE;
Brian Paul6947f852009-01-23 17:32:09 -0700205 case GL_ADD:
206 if (envMode == GL_COMBINE4_NV)
207 return MODE_ADD_PRODUCTS;
208 else
209 return MODE_ADD;
210 case GL_ADD_SIGNED:
211 if (envMode == GL_COMBINE4_NV)
212 return MODE_ADD_PRODUCTS_SIGNED;
213 else
214 return MODE_ADD_SIGNED;
Keith Whitwellce721142005-07-11 10:10:38 +0000215 case GL_INTERPOLATE: return MODE_INTERPOLATE;
216 case GL_SUBTRACT: return MODE_SUBTRACT;
217 case GL_DOT3_RGB: return MODE_DOT3_RGB;
218 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
219 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
220 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
221 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
222 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
223 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
Roland Scheidegger114152e2009-03-12 15:01:16 +0100224 case GL_BUMP_ENVMAP_ATI: return MODE_BUMP_ENVMAP_ATI;
Brian Paul6947f852009-01-23 17:32:09 -0700225 default:
226 assert(0);
227 return MODE_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000228 }
229}
230
231#define TEXTURE_UNKNOWN_INDEX 7
Brian Paule00ac112005-09-15 05:00:45 +0000232static GLuint translate_tex_src_bit( GLbitfield bit )
Keith Whitwellce721142005-07-11 10:10:38 +0000233{
Brian Paul1519b932008-12-17 13:17:15 -0700234 /* make sure number of switch cases is correct */
235 assert(NUM_TEXTURE_TARGETS == 7);
Keith Whitwellce721142005-07-11 10:10:38 +0000236 switch (bit) {
237 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
238 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
239 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
240 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
241 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
Brian Paul6947f852009-01-23 17:32:09 -0700242 case TEXTURE_1D_ARRAY_BIT: return TEXTURE_1D_ARRAY_INDEX;
243 case TEXTURE_2D_ARRAY_BIT: return TEXTURE_2D_ARRAY_INDEX;
244 default:
245 assert(0);
246 return TEXTURE_UNKNOWN_INDEX;
Keith Whitwellce721142005-07-11 10:10:38 +0000247 }
248}
249
Keith Whitwell1680ef82008-10-03 17:30:59 +0100250#define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
251#define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
252
Brian Paul239617f2008-10-07 11:22:47 -0600253/**
254 * Identify all possible varying inputs. The fragment program will
Keith Whitwell1680ef82008-10-03 17:30:59 +0100255 * never reference non-varying inputs, but will track them via state
256 * constants instead.
257 *
258 * This function figures out all the inputs that the fragment program
259 * has access to. The bitmask is later reduced to just those which
260 * are actually referenced.
261 */
Brian Paul239617f2008-10-07 11:22:47 -0600262static GLbitfield get_fp_input_mask( GLcontext *ctx )
Keith Whitwell1680ef82008-10-03 17:30:59 +0100263{
Brian Paulf0b07942008-12-17 14:04:03 -0700264 const GLboolean vertexShader = (ctx->Shader.CurrentProgram &&
265 ctx->Shader.CurrentProgram->VertexProgram);
266 const GLboolean vertexProgram = ctx->VertexProgram._Enabled;
Brian Paul239617f2008-10-07 11:22:47 -0600267 GLbitfield fp_inputs = 0x0;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100268
Brian Paul6d4d51d2008-10-10 13:39:14 -0600269 if (ctx->VertexProgram._Overriden) {
270 /* Somebody's messing with the vertex program and we don't have
271 * a clue what's happening. Assume that it could be producing
272 * all possible outputs.
273 */
274 fp_inputs = ~0;
275 }
276 else if (ctx->RenderMode == GL_FEEDBACK) {
277 fp_inputs = (FRAG_BIT_COL0 | FRAG_BIT_TEX0);
278 }
Brian Paulf0b07942008-12-17 14:04:03 -0700279 else if (!(vertexProgram || vertexShader) ||
Brian Pauld7296a12008-12-17 11:29:06 -0700280 !ctx->VertexProgram._Current) {
Brian Paulf0b07942008-12-17 14:04:03 -0700281 /* Fixed function vertex logic */
Brian Paul239617f2008-10-07 11:22:47 -0600282 GLbitfield varying_inputs = ctx->varying_vp_inputs;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100283
Keith Whitwell97e63432008-10-20 13:03:45 +0100284 /* These get generated in the setup routine regardless of the
285 * vertex program:
286 */
287 if (ctx->Point.PointSprite)
288 varying_inputs |= FRAG_BITS_TEX_ANY;
289
Keith Whitwell1680ef82008-10-03 17:30:59 +0100290 /* First look at what values may be computed by the generated
291 * vertex program:
292 */
293 if (ctx->Light.Enabled) {
294 fp_inputs |= FRAG_BIT_COL0;
295
296 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
297 fp_inputs |= FRAG_BIT_COL1;
298 }
299
300 fp_inputs |= (ctx->Texture._TexGenEnabled |
301 ctx->Texture._TexMatEnabled) << FRAG_ATTRIB_TEX0;
302
303 /* Then look at what might be varying as a result of enabled
304 * arrays, etc:
305 */
306 if (varying_inputs & VERT_BIT_COLOR0) fp_inputs |= FRAG_BIT_COL0;
307 if (varying_inputs & VERT_BIT_COLOR1) fp_inputs |= FRAG_BIT_COL1;
308
309 fp_inputs |= (((varying_inputs & VERT_BIT_TEX_ANY) >> VERT_ATTRIB_TEX0)
310 << FRAG_ATTRIB_TEX0);
311
312 }
313 else {
314 /* calculate from vp->outputs */
Brian Paul2389c052008-12-17 19:01:34 -0700315 struct gl_vertex_program *vprog;
316 GLbitfield vp_outputs;
317
318 /* Choose GLSL vertex shader over ARB vertex program. Need this
319 * since vertex shader state validation comes after fragment state
320 * validation (see additional comments in state.c).
321 */
322 if (vertexShader)
323 vprog = ctx->Shader.CurrentProgram->VertexProgram;
324 else
325 vprog = ctx->VertexProgram._Current;
326
327 vp_outputs = vprog->Base.OutputsWritten;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100328
Keith Whitwell97e63432008-10-20 13:03:45 +0100329 /* These get generated in the setup routine regardless of the
330 * vertex program:
331 */
332 if (ctx->Point.PointSprite)
333 vp_outputs |= FRAG_BITS_TEX_ANY;
334
Keith Whitwell1680ef82008-10-03 17:30:59 +0100335 if (vp_outputs & (1 << VERT_RESULT_COL0)) fp_inputs |= FRAG_BIT_COL0;
336 if (vp_outputs & (1 << VERT_RESULT_COL1)) fp_inputs |= FRAG_BIT_COL1;
337
Keith Whitwell0370d6b2008-10-04 12:41:56 +0100338 fp_inputs |= (((vp_outputs & VERT_RESULT_TEX_ANY) >> VERT_RESULT_TEX0)
339 << FRAG_ATTRIB_TEX0);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100340 }
341
342 return fp_inputs;
343}
344
345
Brian Paul22db5352005-11-19 23:45:10 +0000346/**
347 * Examine current texture environment state and generate a unique
348 * key to identify it.
349 */
Keith Whitwell8065c122006-05-22 16:09:27 +0000350static void make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000351{
Keith Whitwellce721142005-07-11 10:10:38 +0000352 GLuint i, j;
Brian Paul239617f2008-10-07 11:22:47 -0600353 GLbitfield inputs_referenced = FRAG_BIT_COL0;
354 GLbitfield inputs_available = get_fp_input_mask( ctx );
Keith Whitwell1680ef82008-10-03 17:30:59 +0100355
Keith Whitwell8065c122006-05-22 16:09:27 +0000356 memset(key, 0, sizeof(*key));
357
Brian Paule9b34882008-12-31 11:54:02 -0700358 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000359 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800360 GLenum format;
Roland Scheideggerbf4a0fa2008-02-15 17:26:06 +0100361
362 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
Keith Whitwellce721142005-07-11 10:10:38 +0000363 continue;
364
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800365 format = texUnit->_Current->Image[0][texUnit->_Current->BaseLevel]->_BaseFormat;
366
Keith Whitwellce721142005-07-11 10:10:38 +0000367 key->unit[i].enabled = 1;
368 key->enabled_units |= (1<<i);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100369 key->nr_enabled_units = i+1;
370 inputs_referenced |= FRAG_BIT_TEX(i);
Keith Whitwellce721142005-07-11 10:10:38 +0000371
372 key->unit[i].source_index =
373 translate_tex_src_bit(texUnit->_ReallyEnabled);
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800374 key->unit[i].shadow = ((texUnit->_Current->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
375 ((format == GL_DEPTH_COMPONENT) ||
376 (format == GL_DEPTH_STENCIL_EXT)));
Keith Whitwellce721142005-07-11 10:10:38 +0000377
378 key->unit[i].NumArgsRGB = texUnit->_CurrentCombine->_NumArgsRGB;
379 key->unit[i].NumArgsA = texUnit->_CurrentCombine->_NumArgsA;
380
381 key->unit[i].ModeRGB =
Brian Paul6947f852009-01-23 17:32:09 -0700382 translate_mode(texUnit->EnvMode, texUnit->_CurrentCombine->ModeRGB);
Keith Whitwellce721142005-07-11 10:10:38 +0000383 key->unit[i].ModeA =
Brian Paul6947f852009-01-23 17:32:09 -0700384 translate_mode(texUnit->EnvMode, texUnit->_CurrentCombine->ModeA);
Roland Scheidegger114152e2009-03-12 15:01:16 +0100385
Keith Whitwellce721142005-07-11 10:10:38 +0000386 key->unit[i].ScaleShiftRGB = texUnit->_CurrentCombine->ScaleShiftRGB;
Keith Whitwell64da1612006-05-22 14:30:58 +0000387 key->unit[i].ScaleShiftA = texUnit->_CurrentCombine->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000388
Brian Paulbd9b2be2009-03-08 17:58:54 -0600389 for (j = 0; j < MAX_COMBINER_TERMS; j++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000390 key->unit[i].OptRGB[j].Operand =
391 translate_operand(texUnit->_CurrentCombine->OperandRGB[j]);
392 key->unit[i].OptA[j].Operand =
393 translate_operand(texUnit->_CurrentCombine->OperandA[j]);
394 key->unit[i].OptRGB[j].Source =
395 translate_source(texUnit->_CurrentCombine->SourceRGB[j]);
396 key->unit[i].OptA[j].Source =
397 translate_source(texUnit->_CurrentCombine->SourceA[j]);
398 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100399
400 if (key->unit[i].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
401 /* requires some special translation */
402 key->unit[i].NumArgsRGB = 2;
403 key->unit[i].ScaleShiftRGB = 0;
404 key->unit[i].OptRGB[0].Operand = OPR_SRC_COLOR;
405 key->unit[i].OptRGB[0].Source = SRC_TEXTURE;
406 key->unit[i].OptRGB[1].Operand = OPR_SRC_COLOR;
407 key->unit[i].OptRGB[1].Source = texUnit->BumpTarget - GL_TEXTURE0 + SRC_TEXTURE0;
408 }
Keith Whitwellce721142005-07-11 10:10:38 +0000409 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100410
Keith Whitwell1680ef82008-10-03 17:30:59 +0100411 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) {
Keith Whitwellce721142005-07-11 10:10:38 +0000412 key->separate_specular = 1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100413 inputs_referenced |= FRAG_BIT_COL1;
414 }
Keith Whitwellce721142005-07-11 10:10:38 +0000415
416 if (ctx->Fog.Enabled) {
417 key->fog_enabled = 1;
418 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100419 inputs_referenced |= FRAG_BIT_FOGC; /* maybe */
Keith Whitwellce721142005-07-11 10:10:38 +0000420 }
Keith Whitwell1680ef82008-10-03 17:30:59 +0100421
422 key->inputs_available = (inputs_available & inputs_referenced);
Keith Whitwellce721142005-07-11 10:10:38 +0000423}
424
Brian Paul239617f2008-10-07 11:22:47 -0600425/**
426 * Use uregs to represent registers internally, translate to Mesa's
Keith Whitwell15e75e02005-04-29 15:11:39 +0000427 * expected formats on emit.
428 *
429 * NOTE: These are passed by value extensively in this file rather
430 * than as usual by pointer reference. If this disturbs you, try
431 * remembering they are just 32bits in size.
432 *
433 * GCC is smart enough to deal with these dword-sized structures in
434 * much the same way as if I had defined them as dwords and was using
435 * macros to access and set the fields. This is much nicer and easier
436 * to evolve.
437 */
438struct ureg {
439 GLuint file:4;
440 GLuint idx:8;
441 GLuint negatebase:1;
442 GLuint abs:1;
443 GLuint negateabs:1;
444 GLuint swz:12;
445 GLuint pad:5;
446};
447
Brian Paul13abf912006-04-13 19:17:13 +0000448static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000449 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000450 ~0,
451 0,
452 0,
453 0,
454 0,
455 0
456};
457
Keith Whitwell15e75e02005-04-29 15:11:39 +0000458
Brian Paul239617f2008-10-07 11:22:47 -0600459/** State used to build the fragment program:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000460 */
461struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000462 struct gl_fragment_program *program;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000463 GLcontext *ctx;
Keith Whitwellce721142005-07-11 10:10:38 +0000464 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000465
Brian Paul239617f2008-10-07 11:22:47 -0600466 GLbitfield alu_temps; /**< Track texture indirections, see spec. */
467 GLbitfield temps_output; /**< Track texture indirections, see spec. */
468 GLbitfield temp_in_use; /**< Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000469 GLboolean error;
470
Brian Paule9b34882008-12-31 11:54:02 -0700471 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000472 /* Reg containing each texture unit's sampled texture color,
473 * else undef.
474 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000475
Roland Scheidegger114152e2009-03-12 15:01:16 +0100476 struct ureg texcoord_tex[MAX_TEXTURE_COORD_UNITS];
477 /* Reg containing texcoord for a texture unit,
478 * needed for bump mapping, else undef.
479 */
480
Brian Paul239617f2008-10-07 11:22:47 -0600481 struct ureg src_previous; /**< Reg containing color from previous
Keith Whitwell15e75e02005-04-29 15:11:39 +0000482 * stage. May need to be decl'd.
483 */
484
Brian Paul239617f2008-10-07 11:22:47 -0600485 GLuint last_tex_stage; /**< Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000486
487 struct ureg half;
488 struct ureg one;
489 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000490};
491
492
493
494static struct ureg make_ureg(GLuint file, GLuint idx)
495{
496 struct ureg reg;
497 reg.file = file;
498 reg.idx = idx;
499 reg.negatebase = 0;
500 reg.abs = 0;
501 reg.negateabs = 0;
502 reg.swz = SWIZZLE_NOOP;
503 reg.pad = 0;
504 return reg;
505}
506
507static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
508{
509 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
510 GET_SWZ(reg.swz, y),
511 GET_SWZ(reg.swz, z),
512 GET_SWZ(reg.swz, w));
513
514 return reg;
515}
516
517static struct ureg swizzle1( struct ureg reg, int x )
518{
519 return swizzle(reg, x, x, x, x);
520}
521
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000522static struct ureg negate( struct ureg reg )
523{
524 reg.negatebase ^= 1;
525 return reg;
526}
527
Keith Whitwell15e75e02005-04-29 15:11:39 +0000528static GLboolean is_undef( struct ureg reg )
529{
Alan Hourihane8d972652006-08-10 13:12:00 +0000530 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000531}
532
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000533
Keith Whitwell15e75e02005-04-29 15:11:39 +0000534static struct ureg get_temp( struct texenv_fragment_program *p )
535{
Brian Paul13abf912006-04-13 19:17:13 +0000536 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000537
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000538 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000539 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000540 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000541
542 /* Then any unused temporary:
543 */
544 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000545 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000546
Keith Whitwell15e75e02005-04-29 15:11:39 +0000547 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000548 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000549 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000550 }
551
Brian Paul13abf912006-04-13 19:17:13 +0000552 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000553 p->program->Base.NumTemporaries = bit;
554
Keith Whitwell93cd9232005-05-11 08:30:23 +0000555 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000556 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
557}
558
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000559static struct ureg get_tex_temp( struct texenv_fragment_program *p )
560{
561 int bit;
562
Brian Pauld01269a2008-09-25 18:27:22 -0600563 /* First try to find available temp not previously used (to avoid
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000564 * starting a new texture indirection). According to the spec, the
565 * ~p->temps_output isn't necessary, but will keep it there for
566 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000567 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000568 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000569
570 /* Then any unused temporary:
571 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000572 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000573 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000574
575 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000576 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000577 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000578 }
579
Brian Paul13abf912006-04-13 19:17:13 +0000580 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000581 p->program->Base.NumTemporaries = bit;
582
Keith Whitwell93cd9232005-05-11 08:30:23 +0000583 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000584 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
585}
586
Keith Whitwell15e75e02005-04-29 15:11:39 +0000587
Brian Paul5620c202008-09-26 11:18:06 -0600588/** Mark a temp reg as being no longer allocatable. */
589static void reserve_temp( struct texenv_fragment_program *p, struct ureg r )
590{
591 if (r.file == PROGRAM_TEMPORARY)
592 p->temps_output |= (1 << r.idx);
593}
594
595
Brian93fef222007-10-29 12:25:46 -0600596static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000597{
Brian93fef222007-10-29 12:25:46 -0600598 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000599
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000600 /* KW: To support tex_env_crossbar, don't release the registers in
601 * temps_output.
602 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000603 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000604 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000605 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000606 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000607}
608
609
Brian9fe3e2e2007-02-23 11:44:14 -0700610static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000611 GLint s0,
612 GLint s1,
613 GLint s2,
614 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700615 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000616{
Brian9fe3e2e2007-02-23 11:44:14 -0700617 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000618 GLuint idx;
619 tokens[0] = s0;
620 tokens[1] = s1;
621 tokens[2] = s2;
622 tokens[3] = s3;
623 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000624 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000625 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000626}
627
Keith Whitwell47b29f52005-05-04 11:44:44 +0000628
Brian9fe3e2e2007-02-23 11:44:14 -0700629#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
630#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
631#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
632#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000633
Keith Whitwell1680ef82008-10-03 17:30:59 +0100634static GLuint frag_to_vert_attrib( GLuint attrib )
635{
636 switch (attrib) {
637 case FRAG_ATTRIB_COL0: return VERT_ATTRIB_COLOR0;
638 case FRAG_ATTRIB_COL1: return VERT_ATTRIB_COLOR1;
639 default:
640 assert(attrib >= FRAG_ATTRIB_TEX0);
641 assert(attrib <= FRAG_ATTRIB_TEX7);
642 return attrib - FRAG_ATTRIB_TEX0 + VERT_ATTRIB_TEX0;
643 }
644}
645
Keith Whitwell47b29f52005-05-04 11:44:44 +0000646
647static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
648{
Keith Whitwell1680ef82008-10-03 17:30:59 +0100649 if (p->state->inputs_available & (1<<input)) {
650 p->program->Base.InputsRead |= (1 << input);
651 return make_ureg(PROGRAM_INPUT, input);
652 }
653 else {
654 GLuint idx = frag_to_vert_attrib( input );
655 return register_param3( p, STATE_INTERNAL, STATE_CURRENT_ATTRIB, idx );
656 }
Keith Whitwell47b29f52005-05-04 11:44:44 +0000657}
658
659
Brian Paul7e807512005-11-05 17:10:45 +0000660static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000661 struct ureg ureg )
662{
663 reg->File = ureg.file;
664 reg->Index = ureg.idx;
665 reg->Swizzle = ureg.swz;
Brian Paul7db7ff82009-04-14 22:14:30 -0600666 reg->Negate = ureg.negatebase ? NEGATE_XYZW : NEGATE_NONE;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000667 reg->Abs = ureg.abs;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000668}
669
Brian Paul7e807512005-11-05 17:10:45 +0000670static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000671 struct ureg ureg, GLuint mask )
672{
673 dst->File = ureg.file;
674 dst->Index = ureg.idx;
675 dst->WriteMask = mask;
Brianc9d495c2007-10-23 10:55:24 -0600676 dst->CondMask = COND_TR; /* always pass cond test */
677 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000678}
679
Brian Paul7e807512005-11-05 17:10:45 +0000680static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000681emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000682 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000683 struct ureg dest,
684 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000685 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000686 struct ureg src0,
687 struct ureg src1,
688 struct ureg src2 )
689{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000690 GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000691 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Brian2a8e9bb2007-10-23 10:24:53 -0600692
693 assert(nr < MAX_INSTRUCTIONS);
694
Brian Pauld6272e02006-10-29 18:03:16 +0000695 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000696 inst->Opcode = op;
697
Keith Whitwell47b29f52005-05-04 11:44:44 +0000698 emit_arg( &inst->SrcReg[0], src0 );
699 emit_arg( &inst->SrcReg[1], src1 );
700 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000701
Brian Paule31ac052005-11-20 17:52:40 +0000702 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000703
704 emit_dst( &inst->DstReg, dest, mask );
705
Brian Paul5620c202008-09-26 11:18:06 -0600706#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000707 /* Accounting for indirection tracking:
708 */
709 if (dest.file == PROGRAM_TEMPORARY)
710 p->temps_output |= 1 << dest.idx;
Brian Paul5620c202008-09-26 11:18:06 -0600711#endif
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000712
Keith Whitwell15e75e02005-04-29 15:11:39 +0000713 return inst;
714}
715
716
717static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000718 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000719 struct ureg dest,
720 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000721 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000722 struct ureg src0,
723 struct ureg src1,
724 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000725{
726 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
727
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000728 /* Accounting for indirection tracking:
729 */
730 if (src0.file == PROGRAM_TEMPORARY)
731 p->alu_temps |= 1 << src0.idx;
732
733 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
734 p->alu_temps |= 1 << src1.idx;
735
736 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
737 p->alu_temps |= 1 << src2.idx;
738
739 if (dest.file == PROGRAM_TEMPORARY)
740 p->alu_temps |= 1 << dest.idx;
741
Brian21f99792007-01-09 11:00:21 -0700742 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000743 return dest;
744}
745
746static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000747 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000748 struct ureg dest,
749 GLuint destmask,
750 GLuint tex_unit,
751 GLuint tex_idx,
Brian Paul44e018c2009-02-20 13:42:08 -0700752 GLuint tex_shadow,
Keith Whitwellce721142005-07-11 10:10:38 +0000753 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000754{
Brian Paul7e807512005-11-05 17:10:45 +0000755 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000756 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000757 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000758 coord, /* arg 0? */
759 undef,
760 undef);
761
Brian Paul7e807512005-11-05 17:10:45 +0000762 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000763 inst->TexSrcUnit = tex_unit;
Brian Paul44e018c2009-02-20 13:42:08 -0700764 inst->TexShadow = tex_shadow;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000765
Brian21f99792007-01-09 11:00:21 -0700766 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000767
Brian Paul5620c202008-09-26 11:18:06 -0600768 /* Accounting for indirection tracking:
769 */
770 reserve_temp(p, dest);
771
Roland Scheidegger114152e2009-03-12 15:01:16 +0100772#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000773 /* Is this a texture indirection?
774 */
775 if ((coord.file == PROGRAM_TEMPORARY &&
776 (p->temps_output & (1<<coord.idx))) ||
777 (dest.file == PROGRAM_TEMPORARY &&
778 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700779 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000780 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000781 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000782 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000783 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100784#endif
Keith Whitwell15e75e02005-04-29 15:11:39 +0000785
786 return dest;
787}
788
789
Keith Whitwell47b29f52005-05-04 11:44:44 +0000790static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000791 GLfloat s0,
792 GLfloat s1,
793 GLfloat s2,
794 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000795{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000796 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700797 GLuint idx, swizzle;
Brian Pauld01269a2008-09-25 18:27:22 -0600798 struct ureg r;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000799 values[0] = s0;
800 values[1] = s1;
801 values[2] = s2;
802 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700803 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
804 &swizzle );
Brian Pauld01269a2008-09-25 18:27:22 -0600805 r = make_ureg(PROGRAM_CONSTANT, idx);
806 r.swz = swizzle;
807 return r;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000808}
809
Keith Whitwelle4902422005-05-11 15:16:35 +0000810#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000811#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
812#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
813#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000814
815
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000816static struct ureg get_one( struct texenv_fragment_program *p )
817{
818 if (is_undef(p->one))
819 p->one = register_scalar_const(p, 1.0);
820 return p->one;
821}
822
823static struct ureg get_half( struct texenv_fragment_program *p )
824{
825 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000826 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000827 return p->half;
828}
829
830static struct ureg get_zero( struct texenv_fragment_program *p )
831{
832 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000833 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000834 return p->zero;
835}
836
Keith Whitwell15e75e02005-04-29 15:11:39 +0000837
Keith Whitwell15e75e02005-04-29 15:11:39 +0000838static void program_error( struct texenv_fragment_program *p, const char *msg )
839{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000840 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000841 p->error = 1;
842}
843
Keith Whitwell15e75e02005-04-29 15:11:39 +0000844static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000845 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000846{
847 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000848 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000849 assert(!is_undef(p->src_texture[unit]));
850 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000851
Keith Whitwellce721142005-07-11 10:10:38 +0000852 case SRC_TEXTURE0:
853 case SRC_TEXTURE1:
854 case SRC_TEXTURE2:
855 case SRC_TEXTURE3:
856 case SRC_TEXTURE4:
857 case SRC_TEXTURE5:
858 case SRC_TEXTURE6:
859 case SRC_TEXTURE7:
860 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
861 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000862
Keith Whitwellce721142005-07-11 10:10:38 +0000863 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000864 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000865
Keith Whitwellce721142005-07-11 10:10:38 +0000866 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000867 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000868
Brian Paul6947f852009-01-23 17:32:09 -0700869 case SRC_ZERO:
870 return get_zero(p);
871
Keith Whitwellce721142005-07-11 10:10:38 +0000872 case SRC_PREVIOUS:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000873 if (is_undef(p->src_previous))
874 return register_input(p, FRAG_ATTRIB_COL0);
875 else
876 return p->src_previous;
Brian Paul6947f852009-01-23 17:32:09 -0700877
878 default:
879 assert(0);
José Fonsecac6af9b22009-06-15 19:20:25 +0100880 return undef;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000881 }
882}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000883
Keith Whitwell15e75e02005-04-29 15:11:39 +0000884static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000885 GLuint mask,
886 GLuint unit,
887 GLuint source,
888 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000889{
890 struct ureg arg, src, one;
891
892 src = get_source(p, source, unit);
893
894 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000895 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000896 /* Get unused tmp,
897 * Emit tmp = 1.0 - arg.xyzw
898 */
899 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000900 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000901 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000902
Keith Whitwellce721142005-07-11 10:10:38 +0000903 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000904 if (mask == WRITEMASK_W)
905 return src;
906 else
Brian Paul22db5352005-11-19 23:45:10 +0000907 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000908 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000909 /* Get unused tmp,
910 * Emit tmp = 1.0 - arg.wwww
911 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000912 arg = get_temp(p);
913 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000914 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000915 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000916 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000917 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000918 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000919 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000920 case OPR_SRC_COLOR:
Brian Paul6947f852009-01-23 17:32:09 -0700921 return src;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000922 default:
Brian Paul6947f852009-01-23 17:32:09 -0700923 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000924 return src;
925 }
926}
927
Keith Whitwellce721142005-07-11 10:10:38 +0000928static GLboolean args_match( struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000929{
Brian Paul22db5352005-11-19 23:45:10 +0000930 GLuint i, nr = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000931
932 for (i = 0 ; i < nr ; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000933 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000934 return GL_FALSE;
935
Keith Whitwellce721142005-07-11 10:10:38 +0000936 switch(key->unit[unit].OptA[i].Operand) {
937 case OPR_SRC_ALPHA:
938 switch(key->unit[unit].OptRGB[i].Operand) {
939 case OPR_SRC_COLOR:
940 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000941 break;
942 default:
943 return GL_FALSE;
944 }
945 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000946 case OPR_ONE_MINUS_SRC_ALPHA:
947 switch(key->unit[unit].OptRGB[i].Operand) {
948 case OPR_ONE_MINUS_SRC_COLOR:
949 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000950 break;
951 default:
952 return GL_FALSE;
953 }
954 break;
955 default:
956 return GL_FALSE; /* impossible */
957 }
958 }
959
960 return GL_TRUE;
961}
962
Keith Whitwell15e75e02005-04-29 15:11:39 +0000963static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000964 struct ureg dest,
965 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000966 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000967 GLuint unit,
968 GLuint nr,
969 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +0000970 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000971{
Brian Paulbd9b2be2009-03-08 17:58:54 -0600972 struct ureg src[MAX_COMBINER_TERMS];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000973 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +0000974 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000975
Brian Paulbd9b2be2009-03-08 17:58:54 -0600976 assert(nr <= MAX_COMBINER_TERMS);
Brian Paul6947f852009-01-23 17:32:09 -0700977
Brian Paul00630842005-12-12 15:27:55 +0000978 tmp = undef; /* silence warning (bug 5318) */
979
Keith Whitwell15e75e02005-04-29 15:11:39 +0000980 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +0000981 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000982
983 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +0000984 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000985 if (mask == WRITEMASK_XYZW && !saturate)
986 return src[0];
987 else
Brian Paul7e807512005-11-05 17:10:45 +0000988 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000989 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +0000990 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000991 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000992 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +0000993 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000994 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000995 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000996 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000997 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +0000998 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000999 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +00001000 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +00001001 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
1002 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001003 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +00001004 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001005 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
1006 */
Brian Paul7e807512005-11-05 17:10:45 +00001007 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001008
Keith Whitwellce721142005-07-11 10:10:38 +00001009 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +00001010 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001011
Keith Whitwellce721142005-07-11 10:10:38 +00001012 case MODE_DOT3_RGBA:
1013 case MODE_DOT3_RGBA_EXT:
1014 case MODE_DOT3_RGB_EXT:
1015 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001016 struct ureg tmp0 = get_temp( p );
1017 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +00001018 struct ureg neg1 = register_scalar_const(p, -1);
1019 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001020
1021 /* tmp0 = 2*src0 - 1
1022 * tmp1 = 2*src1 - 1
1023 *
1024 * dst = tmp0 dot3 tmp1
1025 */
Brian Paul7e807512005-11-05 17:10:45 +00001026 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001027 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001028
Brian Paulaa206952005-09-16 18:14:24 +00001029 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001030 tmp1 = tmp0;
1031 else
Brian Paul7e807512005-11-05 17:10:45 +00001032 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001033 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +00001034 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001035 return dest;
1036 }
Keith Whitwellce721142005-07-11 10:10:38 +00001037 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001038 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001039 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001040 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +00001041 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001042 /* Arg0 * Arg2 + Arg1 - 0.5 */
1043 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001044 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +00001045 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
1046 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001047 return dest;
1048 }
Keith Whitwellce721142005-07-11 10:10:38 +00001049 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001050 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001051 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001052 return dest;
Brian Paul6947f852009-01-23 17:32:09 -07001053 case MODE_ADD_PRODUCTS:
1054 /* Arg0 * Arg1 + Arg2 * Arg3 */
1055 {
1056 struct ureg tmp0 = get_temp(p);
1057 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1058 emit_arith( p, OPCODE_MAD, dest, mask, saturate, src[2], src[3], tmp0 );
1059 }
1060 return dest;
1061 case MODE_ADD_PRODUCTS_SIGNED:
1062 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1063 {
1064 struct ureg tmp0 = get_temp(p);
1065 half = get_half(p);
1066 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1067 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[2], src[3], tmp0 );
1068 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1069 }
1070 return dest;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001071 case MODE_BUMP_ENVMAP_ATI:
1072 /* special - not handled here */
1073 assert(0);
1074 return src[0];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001075 default:
Brian Paul6947f852009-01-23 17:32:09 -07001076 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001077 return src[0];
1078 }
1079}
1080
Keith Whitwell15e75e02005-04-29 15:11:39 +00001081
Brian Paul22db5352005-11-19 23:45:10 +00001082/**
1083 * Generate instructions for one texture unit's env/combiner mode.
1084 */
1085static struct ureg
1086emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001087{
Keith Whitwellce721142005-07-11 10:10:38 +00001088 struct state_key *key = p->state;
Brian Paul22db5352005-11-19 23:45:10 +00001089 GLboolean saturate = (unit < p->last_tex_stage);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001090 GLuint rgb_shift, alpha_shift;
1091 struct ureg out, shift;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001092 struct ureg dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001093
Keith Whitwellce721142005-07-11 10:10:38 +00001094 if (!key->unit[unit].enabled) {
1095 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001096 }
Roland Scheidegger114152e2009-03-12 15:01:16 +01001097 if (key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1098 /* this isn't really a env stage delivering a color and handled elsewhere */
1099 return get_source(p, SRC_PREVIOUS, 0);
1100 }
Keith Whitwellce721142005-07-11 10:10:38 +00001101
1102 switch (key->unit[unit].ModeRGB) {
1103 case MODE_DOT3_RGB_EXT:
1104 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001105 rgb_shift = 0;
1106 break;
Keith Whitwellce721142005-07-11 10:10:38 +00001107 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001108 alpha_shift = 0;
1109 rgb_shift = 0;
1110 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001111 default:
Keith Whitwellce721142005-07-11 10:10:38 +00001112 rgb_shift = key->unit[unit].ScaleShiftRGB;
1113 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001114 break;
1115 }
Keith Whitwellce721142005-07-11 10:10:38 +00001116
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001117 /* If this is the very last calculation, emit direct to output reg:
1118 */
Keith Whitwellce721142005-07-11 10:10:38 +00001119 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001120 unit != p->last_tex_stage ||
1121 alpha_shift ||
1122 rgb_shift)
1123 dest = get_temp( p );
1124 else
Brian Paul8d475822009-02-28 11:49:46 -07001125 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLOR);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001126
1127 /* Emit the RGB and A combine ops
1128 */
Keith Whitwellce721142005-07-11 10:10:38 +00001129 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
1130 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001131 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1132 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001133 key->unit[unit].NumArgsRGB,
1134 key->unit[unit].ModeRGB,
1135 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001136 }
Keith Whitwellce721142005-07-11 10:10:38 +00001137 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +02001138 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001139
1140 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1141 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001142 key->unit[unit].NumArgsRGB,
1143 key->unit[unit].ModeRGB,
1144 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001145 }
1146 else {
1147 /* Need to do something to stop from re-emitting identical
1148 * argument calculations here:
1149 */
1150 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
1151 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001152 key->unit[unit].NumArgsRGB,
1153 key->unit[unit].ModeRGB,
1154 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001155 out = emit_combine( p, dest, WRITEMASK_W, saturate,
1156 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001157 key->unit[unit].NumArgsA,
1158 key->unit[unit].ModeA,
1159 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001160 }
1161
1162 /* Deal with the final shift:
1163 */
1164 if (alpha_shift || rgb_shift) {
1165 if (rgb_shift == alpha_shift) {
José Fonseca452a5922008-05-31 18:14:09 +09001166 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001167 }
1168 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001169 shift = register_const4f(p,
José Fonseca452a5922008-05-31 18:14:09 +09001170 (GLfloat)(1<<rgb_shift),
1171 (GLfloat)(1<<rgb_shift),
1172 (GLfloat)(1<<rgb_shift),
1173 (GLfloat)(1<<alpha_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001174 }
Brian Paul7e807512005-11-05 17:10:45 +00001175 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001176 saturate, out, shift, undef );
1177 }
1178 else
1179 return out;
1180}
1181
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001182
Brian Paul22db5352005-11-19 23:45:10 +00001183/**
1184 * Generate instruction for getting a texture source term.
1185 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001186static void load_texture( struct texenv_fragment_program *p, GLuint unit )
1187{
1188 if (is_undef(p->src_texture[unit])) {
Brian Paul44e018c2009-02-20 13:42:08 -07001189 GLuint texTarget = p->state->unit[unit].source_index;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001190 struct ureg texcoord;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001191 struct ureg tmp = get_tex_temp( p );
1192
Roland Scheidegger114152e2009-03-12 15:01:16 +01001193 if (is_undef(p->texcoord_tex[unit])) {
1194 texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
1195 }
1196 else {
1197 /* might want to reuse this reg for tex output actually */
1198 texcoord = p->texcoord_tex[unit];
1199 }
1200
Brian Paul44e018c2009-02-20 13:42:08 -07001201 if (texTarget == TEXTURE_UNKNOWN_INDEX)
Brian Paulbfb5ea32005-07-19 18:20:04 +00001202 program_error(p, "TexSrcBit");
Keith Whitwellce721142005-07-11 10:10:38 +00001203
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001204 /* TODO: Use D0_MASK_XY where possible.
1205 */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +02001206 if (p->state->unit[unit].enabled) {
Brian Paul44e018c2009-02-20 13:42:08 -07001207 GLboolean shadow = GL_FALSE;
1208
1209 if (p->state->unit[unit].shadow) {
1210 p->program->Base.ShadowSamplers |= 1 << unit;
1211 shadow = GL_TRUE;
1212 }
1213
Aapo Tahkolab8915342006-03-28 10:26:34 +00001214 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1215 tmp, WRITEMASK_XYZW,
Brian Paul44e018c2009-02-20 13:42:08 -07001216 unit, texTarget, shadow,
1217 texcoord );
Brian9b7e5a52007-12-14 11:16:49 -07001218
1219 p->program->Base.SamplersUsed |= (1 << unit);
Brian1fe385f2007-12-14 11:42:28 -07001220 /* This identity mapping should already be in place
1221 * (see _mesa_init_program_struct()) but let's be safe.
1222 */
1223 p->program->Base.SamplerUnits[unit] = unit;
Brian9b7e5a52007-12-14 11:16:49 -07001224 }
1225 else
Aapo Tahkolab8915342006-03-28 10:26:34 +00001226 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001227 }
1228}
1229
Keith Whitwell241b6b72005-05-23 09:50:34 +00001230static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +00001231 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001232{
1233 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +00001234 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001235 load_texture(p, unit);
1236 break;
1237
Keith Whitwellce721142005-07-11 10:10:38 +00001238 case SRC_TEXTURE0:
1239 case SRC_TEXTURE1:
1240 case SRC_TEXTURE2:
1241 case SRC_TEXTURE3:
1242 case SRC_TEXTURE4:
1243 case SRC_TEXTURE5:
1244 case SRC_TEXTURE6:
1245 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +00001246 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001247 break;
1248
1249 default:
Brian Paul6947f852009-01-23 17:32:09 -07001250 /* not a texture src - do nothing */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001251 break;
1252 }
Keith Whitwell241b6b72005-05-23 09:50:34 +00001253
1254 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001255}
1256
Brian Paul22db5352005-11-19 23:45:10 +00001257
1258/**
1259 * Generate instructions for loading all texture source terms.
1260 */
1261static GLboolean
1262load_texunit_sources( struct texenv_fragment_program *p, int unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001263{
Keith Whitwellce721142005-07-11 10:10:38 +00001264 struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +00001265 GLuint i;
1266
1267 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001268 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001269 }
Aapo Tahkolab8915342006-03-28 10:26:34 +00001270
1271 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1272 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1273 }
1274
Keith Whitwell241b6b72005-05-23 09:50:34 +00001275 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001276}
1277
Roland Scheidegger114152e2009-03-12 15:01:16 +01001278/**
1279 * Generate instructions for loading bump map textures.
1280 */
1281static GLboolean
1282load_texunit_bumpmap( struct texenv_fragment_program *p, int unit )
1283{
1284 struct state_key *key = p->state;
1285 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1286 struct ureg texcDst, bumpMapRes;
1287 struct ureg constdudvcolor = register_const4f(p, 0.0, 0.0, 0.0, 1.0);
1288 struct ureg texcSrc = register_input(p, FRAG_ATTRIB_TEX0 + bumpedUnitNr);
1289 struct ureg rotMat0 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_0, unit );
1290 struct ureg rotMat1 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_1, unit );
1291
1292 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1293
1294 bumpMapRes = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1295 texcDst = get_tex_temp( p );
1296 p->texcoord_tex[bumpedUnitNr] = texcDst;
1297
1298 /* apply rot matrix and add coords to be available in next phase */
1299 /* dest = (Arg0.xxxx * rotMat0 + Arg1) + (Arg0.yyyy * rotMat1) */
1300 /* note only 2 coords are affected the rest are left unchanged (mul by 0) */
1301 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1302 swizzle1(bumpMapRes, SWIZZLE_X), rotMat0, texcSrc );
1303 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1304 swizzle1(bumpMapRes, SWIZZLE_Y), rotMat1, texcDst );
1305
1306 /* move 0,0,0,1 into bumpmap src if someone (crossbar) is foolish
1307 enough to access this later, should optimize away */
1308 emit_arith( p, OPCODE_MOV, bumpMapRes, WRITEMASK_XYZW, 0, constdudvcolor, undef, undef );
1309
1310 return GL_TRUE;
1311}
Brian Paul22db5352005-11-19 23:45:10 +00001312
1313/**
1314 * Generate a new fragment program which implements the context's
1315 * current texture env/combine mode.
1316 */
1317static void
Brian Paule998c342006-10-29 21:17:18 +00001318create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001319 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001320{
Brian Paule998c342006-10-29 21:17:18 +00001321 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001322 struct texenv_fragment_program p;
1323 GLuint unit;
1324 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001325
Keith Whitwelle4902422005-05-11 15:16:35 +00001326 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +00001327 p.ctx = ctx;
Keith Whitwellce721142005-07-11 10:10:38 +00001328 p.state = key;
1329 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001330
Brian Paule998c342006-10-29 21:17:18 +00001331 /* During code generation, use locally-allocated instruction buffer,
1332 * then alloc dynamic storage below.
1333 */
1334 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001335 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001336 p.program->Base.NumTexIndirections = 1;
Brian21f99792007-01-09 11:00:21 -07001337 p.program->Base.NumTexInstructions = 0;
1338 p.program->Base.NumAluInstructions = 0;
Brian1240eb22007-03-22 08:50:20 -06001339 p.program->Base.String = NULL;
Keith Whitwell9ca88152005-05-10 09:56:02 +00001340 p.program->Base.NumInstructions =
Brian Paule998c342006-10-29 21:17:18 +00001341 p.program->Base.NumTemporaries =
1342 p.program->Base.NumParameters =
1343 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001344 p.program->Base.Parameters = _mesa_new_parameter_list();
Keith Whitwelldbeea252005-05-10 13:57:50 +00001345
Brian Paulde997602005-11-12 17:53:14 +00001346 p.program->Base.InputsRead = 0;
Brian Paul8d475822009-02-28 11:49:46 -07001347 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLOR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001348
Roland Scheidegger114152e2009-03-12 15:01:16 +01001349 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001350 p.src_texture[unit] = undef;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001351 p.texcoord_tex[unit] = undef;
1352 }
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001353
Keith Whitwell47b29f52005-05-04 11:44:44 +00001354 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001355 p.half = undef;
1356 p.zero = undef;
1357 p.one = undef;
1358
Keith Whitwell15e75e02005-04-29 15:11:39 +00001359 p.last_tex_stage = 0;
Brian93fef222007-10-29 12:25:46 -06001360 release_temps(ctx, &p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001361
Keith Whitwellce721142005-07-11 10:10:38 +00001362 if (key->enabled_units) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001363 GLboolean needbumpstage = GL_FALSE;
1364 /* Zeroth pass - bump map textures first */
1365 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
1366 if (key->unit[unit].enabled && key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1367 needbumpstage = GL_TRUE;
1368 load_texunit_bumpmap( &p, unit );
1369 }
1370 if (needbumpstage)
1371 p.program->Base.NumTexIndirections++;
1372
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001373 /* First pass - to support texture_env_crossbar, first identify
1374 * all referenced texture sources and emit texld instructions
1375 * for each:
1376 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001377 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001378 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001379 load_texunit_sources( &p, unit );
1380 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001381 }
1382
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001383 /* Second pass - emit combine instructions to build final color:
1384 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001385 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001386 if (key->enabled_units & (1<<unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001387 p.src_previous = emit_texenv( &p, unit );
Brian Paul5620c202008-09-26 11:18:06 -06001388 reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
Brian93fef222007-10-29 12:25:46 -06001389 release_temps(ctx, &p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001390 }
1391 }
1392
Keith Whitwellce721142005-07-11 10:10:38 +00001393 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul8d475822009-02-28 11:49:46 -07001394 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLOR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001395
Keith Whitwellce721142005-07-11 10:10:38 +00001396 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001397 /* Emit specular add.
1398 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001399 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001400 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1401 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001402 }
Brian Paulaa206952005-09-16 18:14:24 +00001403 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001404 /* Will wind up in here if no texture enabled or a couple of
1405 * other scenarios (GL_REPLACE for instance).
1406 */
Brian Paul7e807512005-11-05 17:10:45 +00001407 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001408 }
1409
Keith Whitwell47b29f52005-05-04 11:44:44 +00001410 /* Finish up:
1411 */
Brian Paul7e807512005-11-05 17:10:45 +00001412 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001413
Keith Whitwellce721142005-07-11 10:10:38 +00001414 if (key->fog_enabled) {
1415 /* Pull fog mode from GLcontext, the value in the state key is
1416 * a reduced value and not what is expected in FogOption
1417 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001418 p.program->FogOption = ctx->Fog.Mode;
Brian19d77d62007-09-18 19:29:26 -06001419 p.program->Base.InputsRead |= FRAG_BIT_FOGC; /* XXX new */
Keith Whitwellce721142005-07-11 10:10:38 +00001420 } else
Keith Whitwell276330b2005-05-09 17:58:13 +00001421 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001422
Brian21f99792007-01-09 11:00:21 -07001423 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001424 program_error(&p, "Exceeded max nr indirect texture lookups");
1425
Brian21f99792007-01-09 11:00:21 -07001426 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001427 program_error(&p, "Exceeded max TEX instructions");
1428
Brian21f99792007-01-09 11:00:21 -07001429 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001430 program_error(&p, "Exceeded max ALU instructions");
1431
Brian Paul5d7b49f2005-11-19 23:12:20 +00001432 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001433
Brian Paule998c342006-10-29 21:17:18 +00001434 /* Allocate final instruction array */
Brianc81cce72007-09-25 15:18:51 -06001435 p.program->Base.Instructions
1436 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1437 if (!p.program->Base.Instructions) {
Brian Paule998c342006-10-29 21:17:18 +00001438 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1439 "generating tex env program");
1440 return;
1441 }
Brianc81cce72007-09-25 15:18:51 -06001442 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1443 p.program->Base.NumInstructions);
1444
1445 if (p.program->FogOption) {
1446 _mesa_append_fog_code(ctx, p.program);
1447 p.program->FogOption = GL_NONE;
1448 }
1449
Brian Paule998c342006-10-29 21:17:18 +00001450
Keith Whitwelldbeea252005-05-10 13:57:50 +00001451 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001452 */
Brian Paule998c342006-10-29 21:17:18 +00001453 if (ctx->Driver.ProgramStringNotify) {
1454 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1455 &p.program->Base );
1456 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001457
Brian Paule998c342006-10-29 21:17:18 +00001458 if (DISASSEM) {
1459 _mesa_print_program(&p.program->Base);
1460 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001461 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001462}
1463
Brian Paul5d7b49f2005-11-19 23:12:20 +00001464
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001465/**
1466 * Return a fragment program which implements the current
1467 * fixed-function texture, fog and color-sum operations.
1468 */
1469struct gl_fragment_program *
1470_mesa_get_fixed_func_fragment_program(GLcontext *ctx)
Keith Whitwellce721142005-07-11 10:10:38 +00001471{
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001472 struct gl_fragment_program *prog;
1473 struct state_key key;
1474
1475 make_state_key(ctx, &key);
1476
1477 prog = (struct gl_fragment_program *)
1478 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1479 &key, sizeof(key));
Keith Whitwellce721142005-07-11 10:10:38 +00001480
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001481 if (!prog) {
1482 prog = (struct gl_fragment_program *)
1483 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1484
1485 create_new_program(ctx, &key, prog);
1486
1487 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1488 &key, sizeof(key), &prog->Base);
Keith Whitwellce721142005-07-11 10:10:38 +00001489 }
1490
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001491 return prog;
Keith Whitwellce721142005-07-11 10:10:38 +00001492}