blob: a3f1246c98596f7f11b3333c23b346e79a4a0d0a [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{
Eric Anholte5f63c42009-06-02 07:47:20 -0700264 /* _NEW_PROGRAM */
Brian Paulf0b07942008-12-17 14:04:03 -0700265 const GLboolean vertexShader = (ctx->Shader.CurrentProgram &&
266 ctx->Shader.CurrentProgram->VertexProgram);
267 const GLboolean vertexProgram = ctx->VertexProgram._Enabled;
Brian Paul239617f2008-10-07 11:22:47 -0600268 GLbitfield fp_inputs = 0x0;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100269
Brian Paul6d4d51d2008-10-10 13:39:14 -0600270 if (ctx->VertexProgram._Overriden) {
271 /* Somebody's messing with the vertex program and we don't have
272 * a clue what's happening. Assume that it could be producing
273 * all possible outputs.
274 */
275 fp_inputs = ~0;
276 }
277 else if (ctx->RenderMode == GL_FEEDBACK) {
Eric Anholte5f63c42009-06-02 07:47:20 -0700278 /* _NEW_RENDERMODE */
Brian Paul6d4d51d2008-10-10 13:39:14 -0600279 fp_inputs = (FRAG_BIT_COL0 | FRAG_BIT_TEX0);
280 }
Brian Paulf0b07942008-12-17 14:04:03 -0700281 else if (!(vertexProgram || vertexShader) ||
Brian Pauld7296a12008-12-17 11:29:06 -0700282 !ctx->VertexProgram._Current) {
Brian Paulf0b07942008-12-17 14:04:03 -0700283 /* Fixed function vertex logic */
Eric Anholte5f63c42009-06-02 07:47:20 -0700284 /* _NEW_ARRAY */
Brian Paul239617f2008-10-07 11:22:47 -0600285 GLbitfield varying_inputs = ctx->varying_vp_inputs;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100286
Keith Whitwell97e63432008-10-20 13:03:45 +0100287 /* These get generated in the setup routine regardless of the
288 * vertex program:
289 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700290 /* _NEW_POINT */
Keith Whitwell97e63432008-10-20 13:03:45 +0100291 if (ctx->Point.PointSprite)
292 varying_inputs |= FRAG_BITS_TEX_ANY;
293
Keith Whitwell1680ef82008-10-03 17:30:59 +0100294 /* First look at what values may be computed by the generated
295 * vertex program:
296 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700297 /* _NEW_LIGHT */
Keith Whitwell1680ef82008-10-03 17:30:59 +0100298 if (ctx->Light.Enabled) {
299 fp_inputs |= FRAG_BIT_COL0;
300
301 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
302 fp_inputs |= FRAG_BIT_COL1;
303 }
304
Eric Anholte5f63c42009-06-02 07:47:20 -0700305 /* _NEW_TEXTURE */
Keith Whitwell1680ef82008-10-03 17:30:59 +0100306 fp_inputs |= (ctx->Texture._TexGenEnabled |
307 ctx->Texture._TexMatEnabled) << FRAG_ATTRIB_TEX0;
308
309 /* Then look at what might be varying as a result of enabled
310 * arrays, etc:
311 */
312 if (varying_inputs & VERT_BIT_COLOR0) fp_inputs |= FRAG_BIT_COL0;
313 if (varying_inputs & VERT_BIT_COLOR1) fp_inputs |= FRAG_BIT_COL1;
314
315 fp_inputs |= (((varying_inputs & VERT_BIT_TEX_ANY) >> VERT_ATTRIB_TEX0)
316 << FRAG_ATTRIB_TEX0);
317
318 }
319 else {
320 /* calculate from vp->outputs */
Brian Paul2389c052008-12-17 19:01:34 -0700321 struct gl_vertex_program *vprog;
322 GLbitfield vp_outputs;
323
324 /* Choose GLSL vertex shader over ARB vertex program. Need this
325 * since vertex shader state validation comes after fragment state
326 * validation (see additional comments in state.c).
327 */
328 if (vertexShader)
329 vprog = ctx->Shader.CurrentProgram->VertexProgram;
330 else
331 vprog = ctx->VertexProgram._Current;
332
333 vp_outputs = vprog->Base.OutputsWritten;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100334
Keith Whitwell97e63432008-10-20 13:03:45 +0100335 /* These get generated in the setup routine regardless of the
336 * vertex program:
337 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700338 /* _NEW_POINT */
Keith Whitwell97e63432008-10-20 13:03:45 +0100339 if (ctx->Point.PointSprite)
340 vp_outputs |= FRAG_BITS_TEX_ANY;
341
Keith Whitwell1680ef82008-10-03 17:30:59 +0100342 if (vp_outputs & (1 << VERT_RESULT_COL0)) fp_inputs |= FRAG_BIT_COL0;
343 if (vp_outputs & (1 << VERT_RESULT_COL1)) fp_inputs |= FRAG_BIT_COL1;
344
Keith Whitwell0370d6b2008-10-04 12:41:56 +0100345 fp_inputs |= (((vp_outputs & VERT_RESULT_TEX_ANY) >> VERT_RESULT_TEX0)
346 << FRAG_ATTRIB_TEX0);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100347 }
348
349 return fp_inputs;
350}
351
352
Brian Paul22db5352005-11-19 23:45:10 +0000353/**
354 * Examine current texture environment state and generate a unique
355 * key to identify it.
356 */
Keith Whitwell8065c122006-05-22 16:09:27 +0000357static void make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000358{
Keith Whitwellce721142005-07-11 10:10:38 +0000359 GLuint i, j;
Brian Paul239617f2008-10-07 11:22:47 -0600360 GLbitfield inputs_referenced = FRAG_BIT_COL0;
361 GLbitfield inputs_available = get_fp_input_mask( ctx );
Keith Whitwell1680ef82008-10-03 17:30:59 +0100362
Keith Whitwell8065c122006-05-22 16:09:27 +0000363 memset(key, 0, sizeof(*key));
364
Eric Anholte5f63c42009-06-02 07:47:20 -0700365 /* _NEW_TEXTURE */
Brian Paule9b34882008-12-31 11:54:02 -0700366 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000367 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800368 GLenum format;
Roland Scheideggerbf4a0fa2008-02-15 17:26:06 +0100369
370 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
Keith Whitwellce721142005-07-11 10:10:38 +0000371 continue;
372
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800373 format = texUnit->_Current->Image[0][texUnit->_Current->BaseLevel]->_BaseFormat;
374
Keith Whitwellce721142005-07-11 10:10:38 +0000375 key->unit[i].enabled = 1;
376 key->enabled_units |= (1<<i);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100377 key->nr_enabled_units = i+1;
378 inputs_referenced |= FRAG_BIT_TEX(i);
Keith Whitwellce721142005-07-11 10:10:38 +0000379
380 key->unit[i].source_index =
381 translate_tex_src_bit(texUnit->_ReallyEnabled);
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800382 key->unit[i].shadow = ((texUnit->_Current->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
383 ((format == GL_DEPTH_COMPONENT) ||
384 (format == GL_DEPTH_STENCIL_EXT)));
Keith Whitwellce721142005-07-11 10:10:38 +0000385
386 key->unit[i].NumArgsRGB = texUnit->_CurrentCombine->_NumArgsRGB;
387 key->unit[i].NumArgsA = texUnit->_CurrentCombine->_NumArgsA;
388
389 key->unit[i].ModeRGB =
Brian Paul6947f852009-01-23 17:32:09 -0700390 translate_mode(texUnit->EnvMode, texUnit->_CurrentCombine->ModeRGB);
Keith Whitwellce721142005-07-11 10:10:38 +0000391 key->unit[i].ModeA =
Brian Paul6947f852009-01-23 17:32:09 -0700392 translate_mode(texUnit->EnvMode, texUnit->_CurrentCombine->ModeA);
Roland Scheidegger114152e2009-03-12 15:01:16 +0100393
Keith Whitwellce721142005-07-11 10:10:38 +0000394 key->unit[i].ScaleShiftRGB = texUnit->_CurrentCombine->ScaleShiftRGB;
Keith Whitwell64da1612006-05-22 14:30:58 +0000395 key->unit[i].ScaleShiftA = texUnit->_CurrentCombine->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000396
Brian Paulbd9b2be2009-03-08 17:58:54 -0600397 for (j = 0; j < MAX_COMBINER_TERMS; j++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000398 key->unit[i].OptRGB[j].Operand =
399 translate_operand(texUnit->_CurrentCombine->OperandRGB[j]);
400 key->unit[i].OptA[j].Operand =
401 translate_operand(texUnit->_CurrentCombine->OperandA[j]);
402 key->unit[i].OptRGB[j].Source =
403 translate_source(texUnit->_CurrentCombine->SourceRGB[j]);
404 key->unit[i].OptA[j].Source =
405 translate_source(texUnit->_CurrentCombine->SourceA[j]);
406 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100407
408 if (key->unit[i].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
409 /* requires some special translation */
410 key->unit[i].NumArgsRGB = 2;
411 key->unit[i].ScaleShiftRGB = 0;
412 key->unit[i].OptRGB[0].Operand = OPR_SRC_COLOR;
413 key->unit[i].OptRGB[0].Source = SRC_TEXTURE;
414 key->unit[i].OptRGB[1].Operand = OPR_SRC_COLOR;
415 key->unit[i].OptRGB[1].Source = texUnit->BumpTarget - GL_TEXTURE0 + SRC_TEXTURE0;
416 }
Keith Whitwellce721142005-07-11 10:10:38 +0000417 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100418
Eric Anholte5f63c42009-06-02 07:47:20 -0700419 /* _DD_NEW_SEPARATE_SPECULAR */
Keith Whitwell1680ef82008-10-03 17:30:59 +0100420 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) {
Keith Whitwellce721142005-07-11 10:10:38 +0000421 key->separate_specular = 1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100422 inputs_referenced |= FRAG_BIT_COL1;
423 }
Keith Whitwellce721142005-07-11 10:10:38 +0000424
Eric Anholte5f63c42009-06-02 07:47:20 -0700425 /* _NEW_FOG */
Keith Whitwellce721142005-07-11 10:10:38 +0000426 if (ctx->Fog.Enabled) {
427 key->fog_enabled = 1;
428 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100429 inputs_referenced |= FRAG_BIT_FOGC; /* maybe */
Keith Whitwellce721142005-07-11 10:10:38 +0000430 }
Keith Whitwell1680ef82008-10-03 17:30:59 +0100431
432 key->inputs_available = (inputs_available & inputs_referenced);
Keith Whitwellce721142005-07-11 10:10:38 +0000433}
434
Brian Paul239617f2008-10-07 11:22:47 -0600435/**
436 * Use uregs to represent registers internally, translate to Mesa's
Keith Whitwell15e75e02005-04-29 15:11:39 +0000437 * expected formats on emit.
438 *
439 * NOTE: These are passed by value extensively in this file rather
440 * than as usual by pointer reference. If this disturbs you, try
441 * remembering they are just 32bits in size.
442 *
443 * GCC is smart enough to deal with these dword-sized structures in
444 * much the same way as if I had defined them as dwords and was using
445 * macros to access and set the fields. This is much nicer and easier
446 * to evolve.
447 */
448struct ureg {
449 GLuint file:4;
450 GLuint idx:8;
451 GLuint negatebase:1;
452 GLuint abs:1;
453 GLuint negateabs:1;
454 GLuint swz:12;
455 GLuint pad:5;
456};
457
Brian Paul13abf912006-04-13 19:17:13 +0000458static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000459 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000460 ~0,
461 0,
462 0,
463 0,
464 0,
465 0
466};
467
Keith Whitwell15e75e02005-04-29 15:11:39 +0000468
Brian Paul239617f2008-10-07 11:22:47 -0600469/** State used to build the fragment program:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000470 */
471struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000472 struct gl_fragment_program *program;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000473 GLcontext *ctx;
Keith Whitwellce721142005-07-11 10:10:38 +0000474 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000475
Brian Paul239617f2008-10-07 11:22:47 -0600476 GLbitfield alu_temps; /**< Track texture indirections, see spec. */
477 GLbitfield temps_output; /**< Track texture indirections, see spec. */
478 GLbitfield temp_in_use; /**< Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000479 GLboolean error;
480
Brian Paule9b34882008-12-31 11:54:02 -0700481 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000482 /* Reg containing each texture unit's sampled texture color,
483 * else undef.
484 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000485
Roland Scheidegger114152e2009-03-12 15:01:16 +0100486 struct ureg texcoord_tex[MAX_TEXTURE_COORD_UNITS];
487 /* Reg containing texcoord for a texture unit,
488 * needed for bump mapping, else undef.
489 */
490
Brian Paul239617f2008-10-07 11:22:47 -0600491 struct ureg src_previous; /**< Reg containing color from previous
Keith Whitwell15e75e02005-04-29 15:11:39 +0000492 * stage. May need to be decl'd.
493 */
494
Brian Paul239617f2008-10-07 11:22:47 -0600495 GLuint last_tex_stage; /**< Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000496
497 struct ureg half;
498 struct ureg one;
499 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000500};
501
502
503
504static struct ureg make_ureg(GLuint file, GLuint idx)
505{
506 struct ureg reg;
507 reg.file = file;
508 reg.idx = idx;
509 reg.negatebase = 0;
510 reg.abs = 0;
511 reg.negateabs = 0;
512 reg.swz = SWIZZLE_NOOP;
513 reg.pad = 0;
514 return reg;
515}
516
517static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
518{
519 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
520 GET_SWZ(reg.swz, y),
521 GET_SWZ(reg.swz, z),
522 GET_SWZ(reg.swz, w));
523
524 return reg;
525}
526
527static struct ureg swizzle1( struct ureg reg, int x )
528{
529 return swizzle(reg, x, x, x, x);
530}
531
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000532static struct ureg negate( struct ureg reg )
533{
534 reg.negatebase ^= 1;
535 return reg;
536}
537
Keith Whitwell15e75e02005-04-29 15:11:39 +0000538static GLboolean is_undef( struct ureg reg )
539{
Alan Hourihane8d972652006-08-10 13:12:00 +0000540 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000541}
542
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000543
Keith Whitwell15e75e02005-04-29 15:11:39 +0000544static struct ureg get_temp( struct texenv_fragment_program *p )
545{
Brian Paul13abf912006-04-13 19:17:13 +0000546 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000547
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000548 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000549 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000550 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000551
552 /* Then any unused temporary:
553 */
554 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000555 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000556
Keith Whitwell15e75e02005-04-29 15:11:39 +0000557 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000558 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000559 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000560 }
561
Brian Paul13abf912006-04-13 19:17:13 +0000562 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000563 p->program->Base.NumTemporaries = bit;
564
Keith Whitwell93cd9232005-05-11 08:30:23 +0000565 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000566 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
567}
568
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000569static struct ureg get_tex_temp( struct texenv_fragment_program *p )
570{
571 int bit;
572
Brian Pauld01269a2008-09-25 18:27:22 -0600573 /* First try to find available temp not previously used (to avoid
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000574 * starting a new texture indirection). According to the spec, the
575 * ~p->temps_output isn't necessary, but will keep it there for
576 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000577 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000578 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000579
580 /* Then any unused temporary:
581 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000582 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000583 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000584
585 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000586 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000587 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000588 }
589
Brian Paul13abf912006-04-13 19:17:13 +0000590 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000591 p->program->Base.NumTemporaries = bit;
592
Keith Whitwell93cd9232005-05-11 08:30:23 +0000593 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000594 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
595}
596
Keith Whitwell15e75e02005-04-29 15:11:39 +0000597
Brian Paul5620c202008-09-26 11:18:06 -0600598/** Mark a temp reg as being no longer allocatable. */
599static void reserve_temp( struct texenv_fragment_program *p, struct ureg r )
600{
601 if (r.file == PROGRAM_TEMPORARY)
602 p->temps_output |= (1 << r.idx);
603}
604
605
Brian93fef222007-10-29 12:25:46 -0600606static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000607{
Brian93fef222007-10-29 12:25:46 -0600608 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000609
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000610 /* KW: To support tex_env_crossbar, don't release the registers in
611 * temps_output.
612 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000613 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000614 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000615 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000616 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000617}
618
619
Brian9fe3e2e2007-02-23 11:44:14 -0700620static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000621 GLint s0,
622 GLint s1,
623 GLint s2,
624 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700625 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000626{
Brian9fe3e2e2007-02-23 11:44:14 -0700627 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000628 GLuint idx;
629 tokens[0] = s0;
630 tokens[1] = s1;
631 tokens[2] = s2;
632 tokens[3] = s3;
633 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000634 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000635 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000636}
637
Keith Whitwell47b29f52005-05-04 11:44:44 +0000638
Brian9fe3e2e2007-02-23 11:44:14 -0700639#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
640#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
641#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
642#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000643
Keith Whitwell1680ef82008-10-03 17:30:59 +0100644static GLuint frag_to_vert_attrib( GLuint attrib )
645{
646 switch (attrib) {
647 case FRAG_ATTRIB_COL0: return VERT_ATTRIB_COLOR0;
648 case FRAG_ATTRIB_COL1: return VERT_ATTRIB_COLOR1;
649 default:
650 assert(attrib >= FRAG_ATTRIB_TEX0);
651 assert(attrib <= FRAG_ATTRIB_TEX7);
652 return attrib - FRAG_ATTRIB_TEX0 + VERT_ATTRIB_TEX0;
653 }
654}
655
Keith Whitwell47b29f52005-05-04 11:44:44 +0000656
657static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
658{
Keith Whitwell1680ef82008-10-03 17:30:59 +0100659 if (p->state->inputs_available & (1<<input)) {
660 p->program->Base.InputsRead |= (1 << input);
661 return make_ureg(PROGRAM_INPUT, input);
662 }
663 else {
664 GLuint idx = frag_to_vert_attrib( input );
665 return register_param3( p, STATE_INTERNAL, STATE_CURRENT_ATTRIB, idx );
666 }
Keith Whitwell47b29f52005-05-04 11:44:44 +0000667}
668
669
Brian Paul7e807512005-11-05 17:10:45 +0000670static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000671 struct ureg ureg )
672{
673 reg->File = ureg.file;
674 reg->Index = ureg.idx;
675 reg->Swizzle = ureg.swz;
Brian Paul7db7ff82009-04-14 22:14:30 -0600676 reg->Negate = ureg.negatebase ? NEGATE_XYZW : NEGATE_NONE;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000677 reg->Abs = ureg.abs;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000678}
679
Brian Paul7e807512005-11-05 17:10:45 +0000680static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000681 struct ureg ureg, GLuint mask )
682{
683 dst->File = ureg.file;
684 dst->Index = ureg.idx;
685 dst->WriteMask = mask;
Brianc9d495c2007-10-23 10:55:24 -0600686 dst->CondMask = COND_TR; /* always pass cond test */
687 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000688}
689
Brian Paul7e807512005-11-05 17:10:45 +0000690static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000691emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000692 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000693 struct ureg dest,
694 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000695 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000696 struct ureg src0,
697 struct ureg src1,
698 struct ureg src2 )
699{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000700 GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000701 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Brian2a8e9bb2007-10-23 10:24:53 -0600702
703 assert(nr < MAX_INSTRUCTIONS);
704
Brian Pauld6272e02006-10-29 18:03:16 +0000705 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000706 inst->Opcode = op;
707
Keith Whitwell47b29f52005-05-04 11:44:44 +0000708 emit_arg( &inst->SrcReg[0], src0 );
709 emit_arg( &inst->SrcReg[1], src1 );
710 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000711
Brian Paule31ac052005-11-20 17:52:40 +0000712 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000713
714 emit_dst( &inst->DstReg, dest, mask );
715
Brian Paul5620c202008-09-26 11:18:06 -0600716#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000717 /* Accounting for indirection tracking:
718 */
719 if (dest.file == PROGRAM_TEMPORARY)
720 p->temps_output |= 1 << dest.idx;
Brian Paul5620c202008-09-26 11:18:06 -0600721#endif
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000722
Keith Whitwell15e75e02005-04-29 15:11:39 +0000723 return inst;
724}
725
726
727static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000728 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000729 struct ureg dest,
730 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000731 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000732 struct ureg src0,
733 struct ureg src1,
734 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000735{
736 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
737
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000738 /* Accounting for indirection tracking:
739 */
740 if (src0.file == PROGRAM_TEMPORARY)
741 p->alu_temps |= 1 << src0.idx;
742
743 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
744 p->alu_temps |= 1 << src1.idx;
745
746 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
747 p->alu_temps |= 1 << src2.idx;
748
749 if (dest.file == PROGRAM_TEMPORARY)
750 p->alu_temps |= 1 << dest.idx;
751
Brian21f99792007-01-09 11:00:21 -0700752 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000753 return dest;
754}
755
756static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000757 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000758 struct ureg dest,
759 GLuint destmask,
760 GLuint tex_unit,
761 GLuint tex_idx,
Brian Paul44e018c2009-02-20 13:42:08 -0700762 GLuint tex_shadow,
Keith Whitwellce721142005-07-11 10:10:38 +0000763 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000764{
Brian Paul7e807512005-11-05 17:10:45 +0000765 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000766 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000767 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000768 coord, /* arg 0? */
769 undef,
770 undef);
771
Brian Paul7e807512005-11-05 17:10:45 +0000772 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000773 inst->TexSrcUnit = tex_unit;
Brian Paul44e018c2009-02-20 13:42:08 -0700774 inst->TexShadow = tex_shadow;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000775
Brian21f99792007-01-09 11:00:21 -0700776 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000777
Brian Paul5620c202008-09-26 11:18:06 -0600778 /* Accounting for indirection tracking:
779 */
780 reserve_temp(p, dest);
781
Roland Scheidegger114152e2009-03-12 15:01:16 +0100782#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000783 /* Is this a texture indirection?
784 */
785 if ((coord.file == PROGRAM_TEMPORARY &&
786 (p->temps_output & (1<<coord.idx))) ||
787 (dest.file == PROGRAM_TEMPORARY &&
788 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700789 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000790 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000791 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000792 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000793 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100794#endif
Keith Whitwell15e75e02005-04-29 15:11:39 +0000795
796 return dest;
797}
798
799
Keith Whitwell47b29f52005-05-04 11:44:44 +0000800static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000801 GLfloat s0,
802 GLfloat s1,
803 GLfloat s2,
804 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000805{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000806 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700807 GLuint idx, swizzle;
Brian Pauld01269a2008-09-25 18:27:22 -0600808 struct ureg r;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000809 values[0] = s0;
810 values[1] = s1;
811 values[2] = s2;
812 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700813 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
814 &swizzle );
Brian Pauld01269a2008-09-25 18:27:22 -0600815 r = make_ureg(PROGRAM_CONSTANT, idx);
816 r.swz = swizzle;
817 return r;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000818}
819
Keith Whitwelle4902422005-05-11 15:16:35 +0000820#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000821#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
822#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
823#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000824
825
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000826static struct ureg get_one( struct texenv_fragment_program *p )
827{
828 if (is_undef(p->one))
829 p->one = register_scalar_const(p, 1.0);
830 return p->one;
831}
832
833static struct ureg get_half( struct texenv_fragment_program *p )
834{
835 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000836 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000837 return p->half;
838}
839
840static struct ureg get_zero( struct texenv_fragment_program *p )
841{
842 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000843 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000844 return p->zero;
845}
846
Keith Whitwell15e75e02005-04-29 15:11:39 +0000847
Keith Whitwell15e75e02005-04-29 15:11:39 +0000848static void program_error( struct texenv_fragment_program *p, const char *msg )
849{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000850 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000851 p->error = 1;
852}
853
Keith Whitwell15e75e02005-04-29 15:11:39 +0000854static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000855 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000856{
857 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000858 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000859 assert(!is_undef(p->src_texture[unit]));
860 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000861
Keith Whitwellce721142005-07-11 10:10:38 +0000862 case SRC_TEXTURE0:
863 case SRC_TEXTURE1:
864 case SRC_TEXTURE2:
865 case SRC_TEXTURE3:
866 case SRC_TEXTURE4:
867 case SRC_TEXTURE5:
868 case SRC_TEXTURE6:
869 case SRC_TEXTURE7:
870 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
871 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000872
Keith Whitwellce721142005-07-11 10:10:38 +0000873 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000874 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000875
Keith Whitwellce721142005-07-11 10:10:38 +0000876 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000877 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000878
Brian Paul6947f852009-01-23 17:32:09 -0700879 case SRC_ZERO:
880 return get_zero(p);
881
Keith Whitwellce721142005-07-11 10:10:38 +0000882 case SRC_PREVIOUS:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000883 if (is_undef(p->src_previous))
884 return register_input(p, FRAG_ATTRIB_COL0);
885 else
886 return p->src_previous;
Brian Paul6947f852009-01-23 17:32:09 -0700887
888 default:
889 assert(0);
José Fonsecac6af9b22009-06-15 19:20:25 +0100890 return undef;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000891 }
892}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000893
Keith Whitwell15e75e02005-04-29 15:11:39 +0000894static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000895 GLuint mask,
896 GLuint unit,
897 GLuint source,
898 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000899{
900 struct ureg arg, src, one;
901
902 src = get_source(p, source, unit);
903
904 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000905 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000906 /* Get unused tmp,
907 * Emit tmp = 1.0 - arg.xyzw
908 */
909 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000910 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000911 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000912
Keith Whitwellce721142005-07-11 10:10:38 +0000913 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000914 if (mask == WRITEMASK_W)
915 return src;
916 else
Brian Paul22db5352005-11-19 23:45:10 +0000917 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000918 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000919 /* Get unused tmp,
920 * Emit tmp = 1.0 - arg.wwww
921 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000922 arg = get_temp(p);
923 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000924 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000925 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000926 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000927 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000928 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000929 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000930 case OPR_SRC_COLOR:
Brian Paul6947f852009-01-23 17:32:09 -0700931 return src;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000932 default:
Brian Paul6947f852009-01-23 17:32:09 -0700933 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000934 return src;
935 }
936}
937
Keith Whitwellce721142005-07-11 10:10:38 +0000938static GLboolean args_match( struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000939{
Brian Paul22db5352005-11-19 23:45:10 +0000940 GLuint i, nr = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000941
942 for (i = 0 ; i < nr ; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000943 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000944 return GL_FALSE;
945
Keith Whitwellce721142005-07-11 10:10:38 +0000946 switch(key->unit[unit].OptA[i].Operand) {
947 case OPR_SRC_ALPHA:
948 switch(key->unit[unit].OptRGB[i].Operand) {
949 case OPR_SRC_COLOR:
950 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000951 break;
952 default:
953 return GL_FALSE;
954 }
955 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000956 case OPR_ONE_MINUS_SRC_ALPHA:
957 switch(key->unit[unit].OptRGB[i].Operand) {
958 case OPR_ONE_MINUS_SRC_COLOR:
959 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000960 break;
961 default:
962 return GL_FALSE;
963 }
964 break;
965 default:
966 return GL_FALSE; /* impossible */
967 }
968 }
969
970 return GL_TRUE;
971}
972
Keith Whitwell15e75e02005-04-29 15:11:39 +0000973static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000974 struct ureg dest,
975 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000976 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000977 GLuint unit,
978 GLuint nr,
979 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +0000980 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000981{
Brian Paulbd9b2be2009-03-08 17:58:54 -0600982 struct ureg src[MAX_COMBINER_TERMS];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000983 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +0000984 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000985
Brian Paulbd9b2be2009-03-08 17:58:54 -0600986 assert(nr <= MAX_COMBINER_TERMS);
Brian Paul6947f852009-01-23 17:32:09 -0700987
Brian Paul00630842005-12-12 15:27:55 +0000988 tmp = undef; /* silence warning (bug 5318) */
989
Keith Whitwell15e75e02005-04-29 15:11:39 +0000990 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +0000991 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000992
993 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +0000994 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000995 if (mask == WRITEMASK_XYZW && !saturate)
996 return src[0];
997 else
Brian Paul7e807512005-11-05 17:10:45 +0000998 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000999 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +00001000 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001001 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001002 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00001003 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001004 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001005 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001006 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001007 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +00001008 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001009 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +00001010 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +00001011 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
1012 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001013 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +00001014 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001015 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
1016 */
Brian Paul7e807512005-11-05 17:10:45 +00001017 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001018
Keith Whitwellce721142005-07-11 10:10:38 +00001019 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +00001020 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001021
Keith Whitwellce721142005-07-11 10:10:38 +00001022 case MODE_DOT3_RGBA:
1023 case MODE_DOT3_RGBA_EXT:
1024 case MODE_DOT3_RGB_EXT:
1025 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001026 struct ureg tmp0 = get_temp( p );
1027 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +00001028 struct ureg neg1 = register_scalar_const(p, -1);
1029 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001030
1031 /* tmp0 = 2*src0 - 1
1032 * tmp1 = 2*src1 - 1
1033 *
1034 * dst = tmp0 dot3 tmp1
1035 */
Brian Paul7e807512005-11-05 17:10:45 +00001036 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001037 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001038
Brian Paulaa206952005-09-16 18:14:24 +00001039 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001040 tmp1 = tmp0;
1041 else
Brian Paul7e807512005-11-05 17:10:45 +00001042 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001043 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +00001044 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001045 return dest;
1046 }
Keith Whitwellce721142005-07-11 10:10:38 +00001047 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001048 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001049 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001050 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +00001051 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001052 /* Arg0 * Arg2 + Arg1 - 0.5 */
1053 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001054 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +00001055 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
1056 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001057 return dest;
1058 }
Keith Whitwellce721142005-07-11 10:10:38 +00001059 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001060 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001061 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001062 return dest;
Brian Paul6947f852009-01-23 17:32:09 -07001063 case MODE_ADD_PRODUCTS:
1064 /* Arg0 * Arg1 + Arg2 * Arg3 */
1065 {
1066 struct ureg tmp0 = get_temp(p);
1067 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1068 emit_arith( p, OPCODE_MAD, dest, mask, saturate, src[2], src[3], tmp0 );
1069 }
1070 return dest;
1071 case MODE_ADD_PRODUCTS_SIGNED:
1072 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1073 {
1074 struct ureg tmp0 = get_temp(p);
1075 half = get_half(p);
1076 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1077 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[2], src[3], tmp0 );
1078 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1079 }
1080 return dest;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001081 case MODE_BUMP_ENVMAP_ATI:
1082 /* special - not handled here */
1083 assert(0);
1084 return src[0];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001085 default:
Brian Paul6947f852009-01-23 17:32:09 -07001086 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001087 return src[0];
1088 }
1089}
1090
Keith Whitwell15e75e02005-04-29 15:11:39 +00001091
Brian Paul22db5352005-11-19 23:45:10 +00001092/**
1093 * Generate instructions for one texture unit's env/combiner mode.
1094 */
1095static struct ureg
1096emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001097{
Keith Whitwellce721142005-07-11 10:10:38 +00001098 struct state_key *key = p->state;
Brian Paul22db5352005-11-19 23:45:10 +00001099 GLboolean saturate = (unit < p->last_tex_stage);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001100 GLuint rgb_shift, alpha_shift;
1101 struct ureg out, shift;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001102 struct ureg dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001103
Keith Whitwellce721142005-07-11 10:10:38 +00001104 if (!key->unit[unit].enabled) {
1105 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001106 }
Roland Scheidegger114152e2009-03-12 15:01:16 +01001107 if (key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1108 /* this isn't really a env stage delivering a color and handled elsewhere */
1109 return get_source(p, SRC_PREVIOUS, 0);
1110 }
Keith Whitwellce721142005-07-11 10:10:38 +00001111
1112 switch (key->unit[unit].ModeRGB) {
1113 case MODE_DOT3_RGB_EXT:
1114 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001115 rgb_shift = 0;
1116 break;
Keith Whitwellce721142005-07-11 10:10:38 +00001117 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001118 alpha_shift = 0;
1119 rgb_shift = 0;
1120 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001121 default:
Keith Whitwellce721142005-07-11 10:10:38 +00001122 rgb_shift = key->unit[unit].ScaleShiftRGB;
1123 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001124 break;
1125 }
Keith Whitwellce721142005-07-11 10:10:38 +00001126
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001127 /* If this is the very last calculation, emit direct to output reg:
1128 */
Keith Whitwellce721142005-07-11 10:10:38 +00001129 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001130 unit != p->last_tex_stage ||
1131 alpha_shift ||
1132 rgb_shift)
1133 dest = get_temp( p );
1134 else
Brian Paul8d475822009-02-28 11:49:46 -07001135 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLOR);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001136
1137 /* Emit the RGB and A combine ops
1138 */
Keith Whitwellce721142005-07-11 10:10:38 +00001139 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
1140 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001141 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1142 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001143 key->unit[unit].NumArgsRGB,
1144 key->unit[unit].ModeRGB,
1145 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001146 }
Keith Whitwellce721142005-07-11 10:10:38 +00001147 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +02001148 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001149
1150 out = emit_combine( p, dest, WRITEMASK_XYZW, 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 }
1156 else {
1157 /* Need to do something to stop from re-emitting identical
1158 * argument calculations here:
1159 */
1160 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
1161 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001162 key->unit[unit].NumArgsRGB,
1163 key->unit[unit].ModeRGB,
1164 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001165 out = emit_combine( p, dest, WRITEMASK_W, saturate,
1166 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001167 key->unit[unit].NumArgsA,
1168 key->unit[unit].ModeA,
1169 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001170 }
1171
1172 /* Deal with the final shift:
1173 */
1174 if (alpha_shift || rgb_shift) {
1175 if (rgb_shift == alpha_shift) {
José Fonseca452a5922008-05-31 18:14:09 +09001176 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001177 }
1178 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001179 shift = register_const4f(p,
José Fonseca452a5922008-05-31 18:14:09 +09001180 (GLfloat)(1<<rgb_shift),
1181 (GLfloat)(1<<rgb_shift),
1182 (GLfloat)(1<<rgb_shift),
1183 (GLfloat)(1<<alpha_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001184 }
Brian Paul7e807512005-11-05 17:10:45 +00001185 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001186 saturate, out, shift, undef );
1187 }
1188 else
1189 return out;
1190}
1191
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001192
Brian Paul22db5352005-11-19 23:45:10 +00001193/**
1194 * Generate instruction for getting a texture source term.
1195 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001196static void load_texture( struct texenv_fragment_program *p, GLuint unit )
1197{
1198 if (is_undef(p->src_texture[unit])) {
Brian Paul44e018c2009-02-20 13:42:08 -07001199 GLuint texTarget = p->state->unit[unit].source_index;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001200 struct ureg texcoord;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001201 struct ureg tmp = get_tex_temp( p );
1202
Roland Scheidegger114152e2009-03-12 15:01:16 +01001203 if (is_undef(p->texcoord_tex[unit])) {
1204 texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
1205 }
1206 else {
1207 /* might want to reuse this reg for tex output actually */
1208 texcoord = p->texcoord_tex[unit];
1209 }
1210
Brian Paul44e018c2009-02-20 13:42:08 -07001211 if (texTarget == TEXTURE_UNKNOWN_INDEX)
Brian Paulbfb5ea32005-07-19 18:20:04 +00001212 program_error(p, "TexSrcBit");
Keith Whitwellce721142005-07-11 10:10:38 +00001213
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001214 /* TODO: Use D0_MASK_XY where possible.
1215 */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +02001216 if (p->state->unit[unit].enabled) {
Brian Paul44e018c2009-02-20 13:42:08 -07001217 GLboolean shadow = GL_FALSE;
1218
1219 if (p->state->unit[unit].shadow) {
1220 p->program->Base.ShadowSamplers |= 1 << unit;
1221 shadow = GL_TRUE;
1222 }
1223
Aapo Tahkolab8915342006-03-28 10:26:34 +00001224 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1225 tmp, WRITEMASK_XYZW,
Brian Paul44e018c2009-02-20 13:42:08 -07001226 unit, texTarget, shadow,
1227 texcoord );
Brian9b7e5a52007-12-14 11:16:49 -07001228
1229 p->program->Base.SamplersUsed |= (1 << unit);
Brian1fe385f2007-12-14 11:42:28 -07001230 /* This identity mapping should already be in place
1231 * (see _mesa_init_program_struct()) but let's be safe.
1232 */
1233 p->program->Base.SamplerUnits[unit] = unit;
Brian9b7e5a52007-12-14 11:16:49 -07001234 }
1235 else
Aapo Tahkolab8915342006-03-28 10:26:34 +00001236 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001237 }
1238}
1239
Keith Whitwell241b6b72005-05-23 09:50:34 +00001240static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +00001241 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001242{
1243 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +00001244 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001245 load_texture(p, unit);
1246 break;
1247
Keith Whitwellce721142005-07-11 10:10:38 +00001248 case SRC_TEXTURE0:
1249 case SRC_TEXTURE1:
1250 case SRC_TEXTURE2:
1251 case SRC_TEXTURE3:
1252 case SRC_TEXTURE4:
1253 case SRC_TEXTURE5:
1254 case SRC_TEXTURE6:
1255 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +00001256 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001257 break;
1258
1259 default:
Brian Paul6947f852009-01-23 17:32:09 -07001260 /* not a texture src - do nothing */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001261 break;
1262 }
Keith Whitwell241b6b72005-05-23 09:50:34 +00001263
1264 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001265}
1266
Brian Paul22db5352005-11-19 23:45:10 +00001267
1268/**
1269 * Generate instructions for loading all texture source terms.
1270 */
1271static GLboolean
1272load_texunit_sources( struct texenv_fragment_program *p, int unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001273{
Keith Whitwellce721142005-07-11 10:10:38 +00001274 struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +00001275 GLuint i;
1276
1277 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001278 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001279 }
Aapo Tahkolab8915342006-03-28 10:26:34 +00001280
1281 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1282 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1283 }
1284
Keith Whitwell241b6b72005-05-23 09:50:34 +00001285 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001286}
1287
Roland Scheidegger114152e2009-03-12 15:01:16 +01001288/**
1289 * Generate instructions for loading bump map textures.
1290 */
1291static GLboolean
1292load_texunit_bumpmap( struct texenv_fragment_program *p, int unit )
1293{
1294 struct state_key *key = p->state;
1295 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1296 struct ureg texcDst, bumpMapRes;
1297 struct ureg constdudvcolor = register_const4f(p, 0.0, 0.0, 0.0, 1.0);
1298 struct ureg texcSrc = register_input(p, FRAG_ATTRIB_TEX0 + bumpedUnitNr);
1299 struct ureg rotMat0 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_0, unit );
1300 struct ureg rotMat1 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_1, unit );
1301
1302 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1303
1304 bumpMapRes = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1305 texcDst = get_tex_temp( p );
1306 p->texcoord_tex[bumpedUnitNr] = texcDst;
1307
1308 /* apply rot matrix and add coords to be available in next phase */
1309 /* dest = (Arg0.xxxx * rotMat0 + Arg1) + (Arg0.yyyy * rotMat1) */
1310 /* note only 2 coords are affected the rest are left unchanged (mul by 0) */
1311 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1312 swizzle1(bumpMapRes, SWIZZLE_X), rotMat0, texcSrc );
1313 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1314 swizzle1(bumpMapRes, SWIZZLE_Y), rotMat1, texcDst );
1315
1316 /* move 0,0,0,1 into bumpmap src if someone (crossbar) is foolish
1317 enough to access this later, should optimize away */
1318 emit_arith( p, OPCODE_MOV, bumpMapRes, WRITEMASK_XYZW, 0, constdudvcolor, undef, undef );
1319
1320 return GL_TRUE;
1321}
Brian Paul22db5352005-11-19 23:45:10 +00001322
1323/**
1324 * Generate a new fragment program which implements the context's
1325 * current texture env/combine mode.
1326 */
1327static void
Brian Paule998c342006-10-29 21:17:18 +00001328create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001329 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001330{
Brian Paule998c342006-10-29 21:17:18 +00001331 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001332 struct texenv_fragment_program p;
1333 GLuint unit;
1334 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001335
Keith Whitwelle4902422005-05-11 15:16:35 +00001336 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +00001337 p.ctx = ctx;
Keith Whitwellce721142005-07-11 10:10:38 +00001338 p.state = key;
1339 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001340
Brian Paule998c342006-10-29 21:17:18 +00001341 /* During code generation, use locally-allocated instruction buffer,
1342 * then alloc dynamic storage below.
1343 */
1344 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001345 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001346 p.program->Base.NumTexIndirections = 1;
Brian21f99792007-01-09 11:00:21 -07001347 p.program->Base.NumTexInstructions = 0;
1348 p.program->Base.NumAluInstructions = 0;
Brian1240eb22007-03-22 08:50:20 -06001349 p.program->Base.String = NULL;
Keith Whitwell9ca88152005-05-10 09:56:02 +00001350 p.program->Base.NumInstructions =
Brian Paule998c342006-10-29 21:17:18 +00001351 p.program->Base.NumTemporaries =
1352 p.program->Base.NumParameters =
1353 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001354 p.program->Base.Parameters = _mesa_new_parameter_list();
Keith Whitwelldbeea252005-05-10 13:57:50 +00001355
Brian Paulde997602005-11-12 17:53:14 +00001356 p.program->Base.InputsRead = 0;
Brian Paul8d475822009-02-28 11:49:46 -07001357 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLOR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001358
Roland Scheidegger114152e2009-03-12 15:01:16 +01001359 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001360 p.src_texture[unit] = undef;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001361 p.texcoord_tex[unit] = undef;
1362 }
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001363
Keith Whitwell47b29f52005-05-04 11:44:44 +00001364 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001365 p.half = undef;
1366 p.zero = undef;
1367 p.one = undef;
1368
Keith Whitwell15e75e02005-04-29 15:11:39 +00001369 p.last_tex_stage = 0;
Brian93fef222007-10-29 12:25:46 -06001370 release_temps(ctx, &p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001371
Keith Whitwellce721142005-07-11 10:10:38 +00001372 if (key->enabled_units) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001373 GLboolean needbumpstage = GL_FALSE;
1374 /* Zeroth pass - bump map textures first */
1375 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
1376 if (key->unit[unit].enabled && key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1377 needbumpstage = GL_TRUE;
1378 load_texunit_bumpmap( &p, unit );
1379 }
1380 if (needbumpstage)
1381 p.program->Base.NumTexIndirections++;
1382
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001383 /* First pass - to support texture_env_crossbar, first identify
1384 * all referenced texture sources and emit texld instructions
1385 * for each:
1386 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001387 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001388 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001389 load_texunit_sources( &p, unit );
1390 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001391 }
1392
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001393 /* Second pass - emit combine instructions to build final color:
1394 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001395 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001396 if (key->enabled_units & (1<<unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001397 p.src_previous = emit_texenv( &p, unit );
Brian Paul5620c202008-09-26 11:18:06 -06001398 reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
Brian93fef222007-10-29 12:25:46 -06001399 release_temps(ctx, &p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001400 }
1401 }
1402
Keith Whitwellce721142005-07-11 10:10:38 +00001403 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul8d475822009-02-28 11:49:46 -07001404 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLOR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001405
Keith Whitwellce721142005-07-11 10:10:38 +00001406 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001407 /* Emit specular add.
1408 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001409 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001410 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1411 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001412 }
Brian Paulaa206952005-09-16 18:14:24 +00001413 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001414 /* Will wind up in here if no texture enabled or a couple of
1415 * other scenarios (GL_REPLACE for instance).
1416 */
Brian Paul7e807512005-11-05 17:10:45 +00001417 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001418 }
1419
Keith Whitwell47b29f52005-05-04 11:44:44 +00001420 /* Finish up:
1421 */
Brian Paul7e807512005-11-05 17:10:45 +00001422 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001423
Keith Whitwellce721142005-07-11 10:10:38 +00001424 if (key->fog_enabled) {
1425 /* Pull fog mode from GLcontext, the value in the state key is
1426 * a reduced value and not what is expected in FogOption
1427 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001428 p.program->FogOption = ctx->Fog.Mode;
Brian19d77d62007-09-18 19:29:26 -06001429 p.program->Base.InputsRead |= FRAG_BIT_FOGC; /* XXX new */
Keith Whitwellce721142005-07-11 10:10:38 +00001430 } else
Keith Whitwell276330b2005-05-09 17:58:13 +00001431 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001432
Brian21f99792007-01-09 11:00:21 -07001433 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001434 program_error(&p, "Exceeded max nr indirect texture lookups");
1435
Brian21f99792007-01-09 11:00:21 -07001436 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001437 program_error(&p, "Exceeded max TEX instructions");
1438
Brian21f99792007-01-09 11:00:21 -07001439 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001440 program_error(&p, "Exceeded max ALU instructions");
1441
Brian Paul5d7b49f2005-11-19 23:12:20 +00001442 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001443
Brian Paule998c342006-10-29 21:17:18 +00001444 /* Allocate final instruction array */
Brianc81cce72007-09-25 15:18:51 -06001445 p.program->Base.Instructions
1446 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1447 if (!p.program->Base.Instructions) {
Brian Paule998c342006-10-29 21:17:18 +00001448 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1449 "generating tex env program");
1450 return;
1451 }
Brianc81cce72007-09-25 15:18:51 -06001452 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1453 p.program->Base.NumInstructions);
1454
1455 if (p.program->FogOption) {
1456 _mesa_append_fog_code(ctx, p.program);
1457 p.program->FogOption = GL_NONE;
1458 }
1459
Brian Paule998c342006-10-29 21:17:18 +00001460
Keith Whitwelldbeea252005-05-10 13:57:50 +00001461 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001462 */
Brian Paule998c342006-10-29 21:17:18 +00001463 if (ctx->Driver.ProgramStringNotify) {
1464 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1465 &p.program->Base );
1466 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001467
Brian Paule998c342006-10-29 21:17:18 +00001468 if (DISASSEM) {
1469 _mesa_print_program(&p.program->Base);
1470 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001471 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001472}
1473
Brian Paul5d7b49f2005-11-19 23:12:20 +00001474
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001475/**
1476 * Return a fragment program which implements the current
1477 * fixed-function texture, fog and color-sum operations.
1478 */
1479struct gl_fragment_program *
1480_mesa_get_fixed_func_fragment_program(GLcontext *ctx)
Keith Whitwellce721142005-07-11 10:10:38 +00001481{
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001482 struct gl_fragment_program *prog;
1483 struct state_key key;
1484
1485 make_state_key(ctx, &key);
1486
1487 prog = (struct gl_fragment_program *)
1488 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1489 &key, sizeof(key));
Keith Whitwellce721142005-07-11 10:10:38 +00001490
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001491 if (!prog) {
1492 prog = (struct gl_fragment_program *)
1493 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1494
1495 create_new_program(ctx, &key, prog);
1496
1497 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1498 &key, sizeof(key), &prog->Base);
Keith Whitwellce721142005-07-11 10:10:38 +00001499 }
1500
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001501 return prog;
Keith Whitwellce721142005-07-11 10:10:38 +00001502}