blob: 3fbd119b34797e78a785709932c87525cbbd476f [file] [log] [blame]
Keith Whitwell15e75e02005-04-29 15:11:39 +00001/**************************************************************************
2 *
Keith Whitwell32ef6e72008-09-20 08:26:11 -07003 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
Keith Whitwell15e75e02005-04-29 15:11:39 +00004 * All Rights Reserved.
Brian Paul6947f852009-01-23 17:32:09 -07005 * Copyright 2009 VMware, Inc. All Rights Reserved.
Keith Whitwell15e75e02005-04-29 15:11:39 +00006 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
Keith Whitwell15e75e02005-04-29 15:11:39 +000029#include "glheader.h"
30#include "macros.h"
31#include "enums.h"
Brian Paul5b5c9312008-05-07 18:51:44 -060032#include "shader/program.h"
Brianc223c6b2007-07-04 13:15:20 -060033#include "shader/prog_parameter.h"
Keith Whitwell32ef6e72008-09-20 08:26:11 -070034#include "shader/prog_cache.h"
Brianc223c6b2007-07-04 13:15:20 -060035#include "shader/prog_instruction.h"
36#include "shader/prog_print.h"
37#include "shader/prog_statevars.h"
Brianfb3c41f2007-09-25 15:20:04 -060038#include "shader/programopt.h"
Keith Whitwell15e75e02005-04-29 15:11:39 +000039#include "texenvprogram.h"
40
Brian Paul5b5c9312008-05-07 18:51:44 -060041
Brian Paul6947f852009-01-23 17:32:09 -070042#define MAX_TERMS 4
43
44
Brian Paule9b34882008-12-31 11:54:02 -070045/*
46 * Note on texture units:
47 *
48 * The number of texture units supported by fixed-function fragment
49 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
50 * That's because there's a one-to-one correspondence between texture
51 * coordinates and samplers in fixed-function processing.
52 *
53 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
54 * sets of texcoords, so is fixed-function fragment processing.
55 *
56 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
57 */
58
59
Brian Paul5b5c9312008-05-07 18:51:44 -060060struct texenvprog_cache_item
61{
62 GLuint hash;
63 void *key;
64 struct gl_fragment_program *data;
65 struct texenvprog_cache_item *next;
66};
67
68
Brian Paule998c342006-10-29 21:17:18 +000069/**
Brian Paulb5e1a932008-09-25 18:40:16 -060070 * Up to nine instructions per tex unit, plus fog, specular color.
Brian Paule998c342006-10-29 21:17:18 +000071 */
Brian Paul0815ebc2009-01-02 16:32:26 -070072#define MAX_INSTRUCTIONS ((MAX_TEXTURE_COORD_UNITS * 9) + 12)
Keith Whitwell15e75e02005-04-29 15:11:39 +000073
Keith Whitwell269e3892005-05-12 10:28:43 +000074#define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
Keith Whitwell15e75e02005-04-29 15:11:39 +000075
Keith Whitwellce721142005-07-11 10:10:38 +000076struct mode_opt {
Brian Paul5d7b49f2005-11-19 23:12:20 +000077 GLuint Source:4;
78 GLuint Operand:3;
Keith Whitwellce721142005-07-11 10:10:38 +000079};
80
81struct state_key {
Keith Whitwell6280e332008-10-03 13:51:56 +010082 GLuint nr_enabled_units:8;
83 GLuint enabled_units:8;
Brian Paul5d7b49f2005-11-19 23:12:20 +000084 GLuint separate_specular:1;
85 GLuint fog_enabled:1;
86 GLuint fog_mode:2;
Keith Whitwell6280e332008-10-03 13:51:56 +010087 GLuint inputs_available:12;
Keith Whitwellce721142005-07-11 10:10:38 +000088
89 struct {
Brian Paul5d7b49f2005-11-19 23:12:20 +000090 GLuint enabled:1;
91 GLuint source_index:3; /* one of TEXTURE_1D/2D/3D/CUBE/RECT_INDEX */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +020092 GLuint shadow:1;
Brian Paul5d7b49f2005-11-19 23:12:20 +000093 GLuint ScaleShiftRGB:2;
94 GLuint ScaleShiftA:2;
Keith Whitwellce721142005-07-11 10:10:38 +000095
Brian Paul6947f852009-01-23 17:32:09 -070096 GLuint NumArgsRGB:3;
Roland Scheidegger114152e2009-03-12 15:01:16 +010097 GLuint ModeRGB:5;
Brian Paul6947f852009-01-23 17:32:09 -070098 struct mode_opt OptRGB[MAX_TERMS];
Keith Whitwellce721142005-07-11 10:10:38 +000099
Brian Paul6947f852009-01-23 17:32:09 -0700100 GLuint NumArgsA:3;
Roland Scheidegger114152e2009-03-12 15:01:16 +0100101 GLuint ModeA:5;
Brian Paul6947f852009-01-23 17:32:09 -0700102 struct mode_opt OptA[MAX_TERMS];
Keith Whitwellce721142005-07-11 10:10:38 +0000103 } unit[8];
104};
105
106#define FOG_LINEAR 0
107#define FOG_EXP 1
108#define FOG_EXP2 2
109#define FOG_UNKNOWN 3
110
111static GLuint translate_fog_mode( GLenum mode )
112{
113 switch (mode) {
114 case GL_LINEAR: return FOG_LINEAR;
115 case GL_EXP: return FOG_EXP;
116 case GL_EXP2: return FOG_EXP2;
117 default: return FOG_UNKNOWN;
118 }
119}
120
121#define OPR_SRC_COLOR 0
122#define OPR_ONE_MINUS_SRC_COLOR 1
123#define OPR_SRC_ALPHA 2
124#define OPR_ONE_MINUS_SRC_ALPHA 3
125#define OPR_ZERO 4
126#define OPR_ONE 5
127#define OPR_UNKNOWN 7
128
129static GLuint translate_operand( GLenum operand )
130{
131 switch (operand) {
132 case GL_SRC_COLOR: return OPR_SRC_COLOR;
133 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
134 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
135 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
136 case GL_ZERO: return OPR_ZERO;
137 case GL_ONE: return OPR_ONE;
Brian Paul6947f852009-01-23 17:32:09 -0700138 default:
139 assert(0);
140 return OPR_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000141 }
142}
143
144#define SRC_TEXTURE 0
145#define SRC_TEXTURE0 1
146#define SRC_TEXTURE1 2
147#define SRC_TEXTURE2 3
148#define SRC_TEXTURE3 4
149#define SRC_TEXTURE4 5
150#define SRC_TEXTURE5 6
151#define SRC_TEXTURE6 7
152#define SRC_TEXTURE7 8
153#define SRC_CONSTANT 9
154#define SRC_PRIMARY_COLOR 10
155#define SRC_PREVIOUS 11
Brian Paul6947f852009-01-23 17:32:09 -0700156#define SRC_ZERO 12
Keith Whitwellce721142005-07-11 10:10:38 +0000157#define SRC_UNKNOWN 15
158
159static GLuint translate_source( GLenum src )
160{
161 switch (src) {
162 case GL_TEXTURE: return SRC_TEXTURE;
163 case GL_TEXTURE0:
164 case GL_TEXTURE1:
165 case GL_TEXTURE2:
166 case GL_TEXTURE3:
167 case GL_TEXTURE4:
168 case GL_TEXTURE5:
169 case GL_TEXTURE6:
170 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
171 case GL_CONSTANT: return SRC_CONSTANT;
172 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
173 case GL_PREVIOUS: return SRC_PREVIOUS;
Brian Paul6947f852009-01-23 17:32:09 -0700174 case GL_ZERO:
175 return SRC_ZERO;
176 default:
177 assert(0);
178 return SRC_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000179 }
180}
181
Brian Paul6947f852009-01-23 17:32:09 -0700182#define MODE_REPLACE 0 /* r = a0 */
183#define MODE_MODULATE 1 /* r = a0 * a1 */
184#define MODE_ADD 2 /* r = a0 + a1 */
185#define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
186#define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
187#define MODE_SUBTRACT 5 /* r = a0 - a1 */
188#define MODE_DOT3_RGB 6 /* r = a0 . a1 */
189#define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
190#define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
191#define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
192#define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
193#define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
194#define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
195#define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
196#define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
Roland Scheidegger114152e2009-03-12 15:01:16 +0100197#define MODE_BUMP_ENVMAP_ATI 15 /* special */
198#define MODE_UNKNOWN 16
Keith Whitwellce721142005-07-11 10:10:38 +0000199
Brian Paul6947f852009-01-23 17:32:09 -0700200/**
201 * Translate GL combiner state into a MODE_x value
202 */
203static GLuint translate_mode( GLenum envMode, GLenum mode )
Keith Whitwellce721142005-07-11 10:10:38 +0000204{
205 switch (mode) {
206 case GL_REPLACE: return MODE_REPLACE;
207 case GL_MODULATE: return MODE_MODULATE;
Brian Paul6947f852009-01-23 17:32:09 -0700208 case GL_ADD:
209 if (envMode == GL_COMBINE4_NV)
210 return MODE_ADD_PRODUCTS;
211 else
212 return MODE_ADD;
213 case GL_ADD_SIGNED:
214 if (envMode == GL_COMBINE4_NV)
215 return MODE_ADD_PRODUCTS_SIGNED;
216 else
217 return MODE_ADD_SIGNED;
Keith Whitwellce721142005-07-11 10:10:38 +0000218 case GL_INTERPOLATE: return MODE_INTERPOLATE;
219 case GL_SUBTRACT: return MODE_SUBTRACT;
220 case GL_DOT3_RGB: return MODE_DOT3_RGB;
221 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
222 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
223 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
224 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
225 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
226 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
Roland Scheidegger114152e2009-03-12 15:01:16 +0100227 case GL_BUMP_ENVMAP_ATI: return MODE_BUMP_ENVMAP_ATI;
Brian Paul6947f852009-01-23 17:32:09 -0700228 default:
229 assert(0);
230 return MODE_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000231 }
232}
233
234#define TEXTURE_UNKNOWN_INDEX 7
Brian Paule00ac112005-09-15 05:00:45 +0000235static GLuint translate_tex_src_bit( GLbitfield bit )
Keith Whitwellce721142005-07-11 10:10:38 +0000236{
Brian Paul1519b932008-12-17 13:17:15 -0700237 /* make sure number of switch cases is correct */
238 assert(NUM_TEXTURE_TARGETS == 7);
Keith Whitwellce721142005-07-11 10:10:38 +0000239 switch (bit) {
240 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
241 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
242 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
243 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
244 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
Brian Paul6947f852009-01-23 17:32:09 -0700245 case TEXTURE_1D_ARRAY_BIT: return TEXTURE_1D_ARRAY_INDEX;
246 case TEXTURE_2D_ARRAY_BIT: return TEXTURE_2D_ARRAY_INDEX;
247 default:
248 assert(0);
249 return TEXTURE_UNKNOWN_INDEX;
Keith Whitwellce721142005-07-11 10:10:38 +0000250 }
251}
252
Keith Whitwell1680ef82008-10-03 17:30:59 +0100253#define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
254#define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
255
Brian Paul239617f2008-10-07 11:22:47 -0600256/**
257 * Identify all possible varying inputs. The fragment program will
Keith Whitwell1680ef82008-10-03 17:30:59 +0100258 * never reference non-varying inputs, but will track them via state
259 * constants instead.
260 *
261 * This function figures out all the inputs that the fragment program
262 * has access to. The bitmask is later reduced to just those which
263 * are actually referenced.
264 */
Brian Paul239617f2008-10-07 11:22:47 -0600265static GLbitfield get_fp_input_mask( GLcontext *ctx )
Keith Whitwell1680ef82008-10-03 17:30:59 +0100266{
Brian Paulf0b07942008-12-17 14:04:03 -0700267 const GLboolean vertexShader = (ctx->Shader.CurrentProgram &&
268 ctx->Shader.CurrentProgram->VertexProgram);
269 const GLboolean vertexProgram = ctx->VertexProgram._Enabled;
Brian Paul239617f2008-10-07 11:22:47 -0600270 GLbitfield fp_inputs = 0x0;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100271
Brian Paul6d4d51d2008-10-10 13:39:14 -0600272 if (ctx->VertexProgram._Overriden) {
273 /* Somebody's messing with the vertex program and we don't have
274 * a clue what's happening. Assume that it could be producing
275 * all possible outputs.
276 */
277 fp_inputs = ~0;
278 }
279 else if (ctx->RenderMode == GL_FEEDBACK) {
280 fp_inputs = (FRAG_BIT_COL0 | FRAG_BIT_TEX0);
281 }
Brian Paulf0b07942008-12-17 14:04:03 -0700282 else if (!(vertexProgram || vertexShader) ||
Brian Pauld7296a12008-12-17 11:29:06 -0700283 !ctx->VertexProgram._Current) {
Brian Paulf0b07942008-12-17 14:04:03 -0700284 /* Fixed function vertex logic */
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 */
290 if (ctx->Point.PointSprite)
291 varying_inputs |= FRAG_BITS_TEX_ANY;
292
Keith Whitwell1680ef82008-10-03 17:30:59 +0100293 /* First look at what values may be computed by the generated
294 * vertex program:
295 */
296 if (ctx->Light.Enabled) {
297 fp_inputs |= FRAG_BIT_COL0;
298
299 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
300 fp_inputs |= FRAG_BIT_COL1;
301 }
302
303 fp_inputs |= (ctx->Texture._TexGenEnabled |
304 ctx->Texture._TexMatEnabled) << FRAG_ATTRIB_TEX0;
305
306 /* Then look at what might be varying as a result of enabled
307 * arrays, etc:
308 */
309 if (varying_inputs & VERT_BIT_COLOR0) fp_inputs |= FRAG_BIT_COL0;
310 if (varying_inputs & VERT_BIT_COLOR1) fp_inputs |= FRAG_BIT_COL1;
311
312 fp_inputs |= (((varying_inputs & VERT_BIT_TEX_ANY) >> VERT_ATTRIB_TEX0)
313 << FRAG_ATTRIB_TEX0);
314
315 }
316 else {
317 /* calculate from vp->outputs */
Brian Paul2389c052008-12-17 19:01:34 -0700318 struct gl_vertex_program *vprog;
319 GLbitfield vp_outputs;
320
321 /* Choose GLSL vertex shader over ARB vertex program. Need this
322 * since vertex shader state validation comes after fragment state
323 * validation (see additional comments in state.c).
324 */
325 if (vertexShader)
326 vprog = ctx->Shader.CurrentProgram->VertexProgram;
327 else
328 vprog = ctx->VertexProgram._Current;
329
330 vp_outputs = vprog->Base.OutputsWritten;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100331
Keith Whitwell97e63432008-10-20 13:03:45 +0100332 /* These get generated in the setup routine regardless of the
333 * vertex program:
334 */
335 if (ctx->Point.PointSprite)
336 vp_outputs |= FRAG_BITS_TEX_ANY;
337
Keith Whitwell1680ef82008-10-03 17:30:59 +0100338 if (vp_outputs & (1 << VERT_RESULT_COL0)) fp_inputs |= FRAG_BIT_COL0;
339 if (vp_outputs & (1 << VERT_RESULT_COL1)) fp_inputs |= FRAG_BIT_COL1;
340
Keith Whitwell0370d6b2008-10-04 12:41:56 +0100341 fp_inputs |= (((vp_outputs & VERT_RESULT_TEX_ANY) >> VERT_RESULT_TEX0)
342 << FRAG_ATTRIB_TEX0);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100343 }
344
345 return fp_inputs;
346}
347
348
Brian Paul22db5352005-11-19 23:45:10 +0000349/**
350 * Examine current texture environment state and generate a unique
351 * key to identify it.
352 */
Keith Whitwell8065c122006-05-22 16:09:27 +0000353static void make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000354{
Keith Whitwellce721142005-07-11 10:10:38 +0000355 GLuint i, j;
Brian Paul239617f2008-10-07 11:22:47 -0600356 GLbitfield inputs_referenced = FRAG_BIT_COL0;
357 GLbitfield inputs_available = get_fp_input_mask( ctx );
Keith Whitwell1680ef82008-10-03 17:30:59 +0100358
Keith Whitwell8065c122006-05-22 16:09:27 +0000359 memset(key, 0, sizeof(*key));
360
Brian Paule9b34882008-12-31 11:54:02 -0700361 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000362 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800363 GLenum format;
Roland Scheideggerbf4a0fa2008-02-15 17:26:06 +0100364
365 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
Keith Whitwellce721142005-07-11 10:10:38 +0000366 continue;
367
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800368 format = texUnit->_Current->Image[0][texUnit->_Current->BaseLevel]->_BaseFormat;
369
Keith Whitwellce721142005-07-11 10:10:38 +0000370 key->unit[i].enabled = 1;
371 key->enabled_units |= (1<<i);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100372 key->nr_enabled_units = i+1;
373 inputs_referenced |= FRAG_BIT_TEX(i);
Keith Whitwellce721142005-07-11 10:10:38 +0000374
375 key->unit[i].source_index =
376 translate_tex_src_bit(texUnit->_ReallyEnabled);
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800377 key->unit[i].shadow = ((texUnit->_Current->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
378 ((format == GL_DEPTH_COMPONENT) ||
379 (format == GL_DEPTH_STENCIL_EXT)));
Keith Whitwellce721142005-07-11 10:10:38 +0000380
381 key->unit[i].NumArgsRGB = texUnit->_CurrentCombine->_NumArgsRGB;
382 key->unit[i].NumArgsA = texUnit->_CurrentCombine->_NumArgsA;
383
384 key->unit[i].ModeRGB =
Brian Paul6947f852009-01-23 17:32:09 -0700385 translate_mode(texUnit->EnvMode, texUnit->_CurrentCombine->ModeRGB);
Keith Whitwellce721142005-07-11 10:10:38 +0000386 key->unit[i].ModeA =
Brian Paul6947f852009-01-23 17:32:09 -0700387 translate_mode(texUnit->EnvMode, texUnit->_CurrentCombine->ModeA);
Roland Scheidegger114152e2009-03-12 15:01:16 +0100388
Keith Whitwellce721142005-07-11 10:10:38 +0000389 key->unit[i].ScaleShiftRGB = texUnit->_CurrentCombine->ScaleShiftRGB;
Keith Whitwell64da1612006-05-22 14:30:58 +0000390 key->unit[i].ScaleShiftA = texUnit->_CurrentCombine->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000391
Brian Paul6947f852009-01-23 17:32:09 -0700392 for (j = 0; j < MAX_TERMS; j++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000393 key->unit[i].OptRGB[j].Operand =
394 translate_operand(texUnit->_CurrentCombine->OperandRGB[j]);
395 key->unit[i].OptA[j].Operand =
396 translate_operand(texUnit->_CurrentCombine->OperandA[j]);
397 key->unit[i].OptRGB[j].Source =
398 translate_source(texUnit->_CurrentCombine->SourceRGB[j]);
399 key->unit[i].OptA[j].Source =
400 translate_source(texUnit->_CurrentCombine->SourceA[j]);
401 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100402
403 if (key->unit[i].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
404 /* requires some special translation */
405 key->unit[i].NumArgsRGB = 2;
406 key->unit[i].ScaleShiftRGB = 0;
407 key->unit[i].OptRGB[0].Operand = OPR_SRC_COLOR;
408 key->unit[i].OptRGB[0].Source = SRC_TEXTURE;
409 key->unit[i].OptRGB[1].Operand = OPR_SRC_COLOR;
410 key->unit[i].OptRGB[1].Source = texUnit->BumpTarget - GL_TEXTURE0 + SRC_TEXTURE0;
411 }
Keith Whitwellce721142005-07-11 10:10:38 +0000412 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100413
Keith Whitwell1680ef82008-10-03 17:30:59 +0100414 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) {
Keith Whitwellce721142005-07-11 10:10:38 +0000415 key->separate_specular = 1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100416 inputs_referenced |= FRAG_BIT_COL1;
417 }
Keith Whitwellce721142005-07-11 10:10:38 +0000418
419 if (ctx->Fog.Enabled) {
420 key->fog_enabled = 1;
421 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100422 inputs_referenced |= FRAG_BIT_FOGC; /* maybe */
Keith Whitwellce721142005-07-11 10:10:38 +0000423 }
Keith Whitwell1680ef82008-10-03 17:30:59 +0100424
425 key->inputs_available = (inputs_available & inputs_referenced);
Keith Whitwellce721142005-07-11 10:10:38 +0000426}
427
Brian Paul239617f2008-10-07 11:22:47 -0600428/**
429 * Use uregs to represent registers internally, translate to Mesa's
Keith Whitwell15e75e02005-04-29 15:11:39 +0000430 * expected formats on emit.
431 *
432 * NOTE: These are passed by value extensively in this file rather
433 * than as usual by pointer reference. If this disturbs you, try
434 * remembering they are just 32bits in size.
435 *
436 * GCC is smart enough to deal with these dword-sized structures in
437 * much the same way as if I had defined them as dwords and was using
438 * macros to access and set the fields. This is much nicer and easier
439 * to evolve.
440 */
441struct ureg {
442 GLuint file:4;
443 GLuint idx:8;
444 GLuint negatebase:1;
445 GLuint abs:1;
446 GLuint negateabs:1;
447 GLuint swz:12;
448 GLuint pad:5;
449};
450
Brian Paul13abf912006-04-13 19:17:13 +0000451static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000452 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000453 ~0,
454 0,
455 0,
456 0,
457 0,
458 0
459};
460
Keith Whitwell15e75e02005-04-29 15:11:39 +0000461
Brian Paul239617f2008-10-07 11:22:47 -0600462/** State used to build the fragment program:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000463 */
464struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000465 struct gl_fragment_program *program;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000466 GLcontext *ctx;
Keith Whitwellce721142005-07-11 10:10:38 +0000467 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000468
Brian Paul239617f2008-10-07 11:22:47 -0600469 GLbitfield alu_temps; /**< Track texture indirections, see spec. */
470 GLbitfield temps_output; /**< Track texture indirections, see spec. */
471 GLbitfield temp_in_use; /**< Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000472 GLboolean error;
473
Brian Paule9b34882008-12-31 11:54:02 -0700474 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000475 /* Reg containing each texture unit's sampled texture color,
476 * else undef.
477 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000478
Roland Scheidegger114152e2009-03-12 15:01:16 +0100479 struct ureg texcoord_tex[MAX_TEXTURE_COORD_UNITS];
480 /* Reg containing texcoord for a texture unit,
481 * needed for bump mapping, else undef.
482 */
483
Brian Paul239617f2008-10-07 11:22:47 -0600484 struct ureg src_previous; /**< Reg containing color from previous
Keith Whitwell15e75e02005-04-29 15:11:39 +0000485 * stage. May need to be decl'd.
486 */
487
Brian Paul239617f2008-10-07 11:22:47 -0600488 GLuint last_tex_stage; /**< Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000489
490 struct ureg half;
491 struct ureg one;
492 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000493};
494
495
496
497static struct ureg make_ureg(GLuint file, GLuint idx)
498{
499 struct ureg reg;
500 reg.file = file;
501 reg.idx = idx;
502 reg.negatebase = 0;
503 reg.abs = 0;
504 reg.negateabs = 0;
505 reg.swz = SWIZZLE_NOOP;
506 reg.pad = 0;
507 return reg;
508}
509
510static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
511{
512 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
513 GET_SWZ(reg.swz, y),
514 GET_SWZ(reg.swz, z),
515 GET_SWZ(reg.swz, w));
516
517 return reg;
518}
519
520static struct ureg swizzle1( struct ureg reg, int x )
521{
522 return swizzle(reg, x, x, x, x);
523}
524
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000525static struct ureg negate( struct ureg reg )
526{
527 reg.negatebase ^= 1;
528 return reg;
529}
530
Keith Whitwell15e75e02005-04-29 15:11:39 +0000531static GLboolean is_undef( struct ureg reg )
532{
Alan Hourihane8d972652006-08-10 13:12:00 +0000533 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000534}
535
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000536
Keith Whitwell15e75e02005-04-29 15:11:39 +0000537static struct ureg get_temp( struct texenv_fragment_program *p )
538{
Brian Paul13abf912006-04-13 19:17:13 +0000539 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000540
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000541 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000542 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000543 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000544
545 /* Then any unused temporary:
546 */
547 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000548 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000549
Keith Whitwell15e75e02005-04-29 15:11:39 +0000550 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000551 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000552 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000553 }
554
Brian Paul13abf912006-04-13 19:17:13 +0000555 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000556 p->program->Base.NumTemporaries = bit;
557
Keith Whitwell93cd9232005-05-11 08:30:23 +0000558 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000559 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
560}
561
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000562static struct ureg get_tex_temp( struct texenv_fragment_program *p )
563{
564 int bit;
565
Brian Pauld01269a2008-09-25 18:27:22 -0600566 /* First try to find available temp not previously used (to avoid
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000567 * starting a new texture indirection). According to the spec, the
568 * ~p->temps_output isn't necessary, but will keep it there for
569 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000570 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000571 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000572
573 /* Then any unused temporary:
574 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000575 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000576 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000577
578 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000579 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000580 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000581 }
582
Brian Paul13abf912006-04-13 19:17:13 +0000583 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000584 p->program->Base.NumTemporaries = bit;
585
Keith Whitwell93cd9232005-05-11 08:30:23 +0000586 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000587 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
588}
589
Keith Whitwell15e75e02005-04-29 15:11:39 +0000590
Brian Paul5620c202008-09-26 11:18:06 -0600591/** Mark a temp reg as being no longer allocatable. */
592static void reserve_temp( struct texenv_fragment_program *p, struct ureg r )
593{
594 if (r.file == PROGRAM_TEMPORARY)
595 p->temps_output |= (1 << r.idx);
596}
597
598
Brian93fef222007-10-29 12:25:46 -0600599static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000600{
Brian93fef222007-10-29 12:25:46 -0600601 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000602
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000603 /* KW: To support tex_env_crossbar, don't release the registers in
604 * temps_output.
605 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000606 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000607 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000608 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000609 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000610}
611
612
Brian9fe3e2e2007-02-23 11:44:14 -0700613static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000614 GLint s0,
615 GLint s1,
616 GLint s2,
617 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700618 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000619{
Brian9fe3e2e2007-02-23 11:44:14 -0700620 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000621 GLuint idx;
622 tokens[0] = s0;
623 tokens[1] = s1;
624 tokens[2] = s2;
625 tokens[3] = s3;
626 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000627 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000628 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000629}
630
Keith Whitwell47b29f52005-05-04 11:44:44 +0000631
Brian9fe3e2e2007-02-23 11:44:14 -0700632#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
633#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
634#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
635#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000636
Keith Whitwell1680ef82008-10-03 17:30:59 +0100637static GLuint frag_to_vert_attrib( GLuint attrib )
638{
639 switch (attrib) {
640 case FRAG_ATTRIB_COL0: return VERT_ATTRIB_COLOR0;
641 case FRAG_ATTRIB_COL1: return VERT_ATTRIB_COLOR1;
642 default:
643 assert(attrib >= FRAG_ATTRIB_TEX0);
644 assert(attrib <= FRAG_ATTRIB_TEX7);
645 return attrib - FRAG_ATTRIB_TEX0 + VERT_ATTRIB_TEX0;
646 }
647}
648
Keith Whitwell47b29f52005-05-04 11:44:44 +0000649
650static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
651{
Keith Whitwell1680ef82008-10-03 17:30:59 +0100652 if (p->state->inputs_available & (1<<input)) {
653 p->program->Base.InputsRead |= (1 << input);
654 return make_ureg(PROGRAM_INPUT, input);
655 }
656 else {
657 GLuint idx = frag_to_vert_attrib( input );
658 return register_param3( p, STATE_INTERNAL, STATE_CURRENT_ATTRIB, idx );
659 }
Keith Whitwell47b29f52005-05-04 11:44:44 +0000660}
661
662
Brian Paul7e807512005-11-05 17:10:45 +0000663static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000664 struct ureg ureg )
665{
666 reg->File = ureg.file;
667 reg->Index = ureg.idx;
668 reg->Swizzle = ureg.swz;
Keith Whitwellf2802c42005-10-07 09:55:26 +0000669 reg->NegateBase = ureg.negatebase ? 0xf : 0x0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000670 reg->Abs = ureg.abs;
671 reg->NegateAbs = ureg.negateabs;
672}
673
Brian Paul7e807512005-11-05 17:10:45 +0000674static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000675 struct ureg ureg, GLuint mask )
676{
677 dst->File = ureg.file;
678 dst->Index = ureg.idx;
679 dst->WriteMask = mask;
Brianc9d495c2007-10-23 10:55:24 -0600680 dst->CondMask = COND_TR; /* always pass cond test */
681 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000682}
683
Brian Paul7e807512005-11-05 17:10:45 +0000684static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000685emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000686 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000687 struct ureg dest,
688 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000689 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000690 struct ureg src0,
691 struct ureg src1,
692 struct ureg src2 )
693{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000694 GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000695 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Brian2a8e9bb2007-10-23 10:24:53 -0600696
697 assert(nr < MAX_INSTRUCTIONS);
698
Brian Pauld6272e02006-10-29 18:03:16 +0000699 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000700 inst->Opcode = op;
701
Keith Whitwell47b29f52005-05-04 11:44:44 +0000702 emit_arg( &inst->SrcReg[0], src0 );
703 emit_arg( &inst->SrcReg[1], src1 );
704 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000705
Brian Paule31ac052005-11-20 17:52:40 +0000706 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000707
708 emit_dst( &inst->DstReg, dest, mask );
709
Brian Paul5620c202008-09-26 11:18:06 -0600710#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000711 /* Accounting for indirection tracking:
712 */
713 if (dest.file == PROGRAM_TEMPORARY)
714 p->temps_output |= 1 << dest.idx;
Brian Paul5620c202008-09-26 11:18:06 -0600715#endif
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000716
Keith Whitwell15e75e02005-04-29 15:11:39 +0000717 return inst;
718}
719
720
721static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000722 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000723 struct ureg dest,
724 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000725 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000726 struct ureg src0,
727 struct ureg src1,
728 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000729{
730 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
731
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000732 /* Accounting for indirection tracking:
733 */
734 if (src0.file == PROGRAM_TEMPORARY)
735 p->alu_temps |= 1 << src0.idx;
736
737 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
738 p->alu_temps |= 1 << src1.idx;
739
740 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
741 p->alu_temps |= 1 << src2.idx;
742
743 if (dest.file == PROGRAM_TEMPORARY)
744 p->alu_temps |= 1 << dest.idx;
745
Brian21f99792007-01-09 11:00:21 -0700746 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000747 return dest;
748}
749
750static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000751 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000752 struct ureg dest,
753 GLuint destmask,
754 GLuint tex_unit,
755 GLuint tex_idx,
Brian Paul44e018c2009-02-20 13:42:08 -0700756 GLuint tex_shadow,
Keith Whitwellce721142005-07-11 10:10:38 +0000757 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000758{
Brian Paul7e807512005-11-05 17:10:45 +0000759 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000760 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000761 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000762 coord, /* arg 0? */
763 undef,
764 undef);
765
Brian Paul7e807512005-11-05 17:10:45 +0000766 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000767 inst->TexSrcUnit = tex_unit;
Brian Paul44e018c2009-02-20 13:42:08 -0700768 inst->TexShadow = tex_shadow;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000769
Brian21f99792007-01-09 11:00:21 -0700770 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000771
Brian Paul5620c202008-09-26 11:18:06 -0600772 /* Accounting for indirection tracking:
773 */
774 reserve_temp(p, dest);
775
Roland Scheidegger114152e2009-03-12 15:01:16 +0100776#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000777 /* Is this a texture indirection?
778 */
779 if ((coord.file == PROGRAM_TEMPORARY &&
780 (p->temps_output & (1<<coord.idx))) ||
781 (dest.file == PROGRAM_TEMPORARY &&
782 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700783 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000784 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000785 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000786 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000787 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100788#endif
Keith Whitwell15e75e02005-04-29 15:11:39 +0000789
790 return dest;
791}
792
793
Keith Whitwell47b29f52005-05-04 11:44:44 +0000794static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000795 GLfloat s0,
796 GLfloat s1,
797 GLfloat s2,
798 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000799{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000800 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700801 GLuint idx, swizzle;
Brian Pauld01269a2008-09-25 18:27:22 -0600802 struct ureg r;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000803 values[0] = s0;
804 values[1] = s1;
805 values[2] = s2;
806 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700807 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
808 &swizzle );
Brian Pauld01269a2008-09-25 18:27:22 -0600809 r = make_ureg(PROGRAM_CONSTANT, idx);
810 r.swz = swizzle;
811 return r;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000812}
813
Keith Whitwelle4902422005-05-11 15:16:35 +0000814#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000815#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
816#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
817#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000818
819
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000820static struct ureg get_one( struct texenv_fragment_program *p )
821{
822 if (is_undef(p->one))
823 p->one = register_scalar_const(p, 1.0);
824 return p->one;
825}
826
827static struct ureg get_half( struct texenv_fragment_program *p )
828{
829 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000830 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000831 return p->half;
832}
833
834static struct ureg get_zero( struct texenv_fragment_program *p )
835{
836 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000837 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000838 return p->zero;
839}
840
Keith Whitwell15e75e02005-04-29 15:11:39 +0000841
Keith Whitwell15e75e02005-04-29 15:11:39 +0000842static void program_error( struct texenv_fragment_program *p, const char *msg )
843{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000844 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000845 p->error = 1;
846}
847
Keith Whitwell15e75e02005-04-29 15:11:39 +0000848static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000849 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000850{
851 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000852 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000853 assert(!is_undef(p->src_texture[unit]));
854 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000855
Keith Whitwellce721142005-07-11 10:10:38 +0000856 case SRC_TEXTURE0:
857 case SRC_TEXTURE1:
858 case SRC_TEXTURE2:
859 case SRC_TEXTURE3:
860 case SRC_TEXTURE4:
861 case SRC_TEXTURE5:
862 case SRC_TEXTURE6:
863 case SRC_TEXTURE7:
864 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
865 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000866
Keith Whitwellce721142005-07-11 10:10:38 +0000867 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000868 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000869
Keith Whitwellce721142005-07-11 10:10:38 +0000870 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000871 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000872
Brian Paul6947f852009-01-23 17:32:09 -0700873 case SRC_ZERO:
874 return get_zero(p);
875
Keith Whitwellce721142005-07-11 10:10:38 +0000876 case SRC_PREVIOUS:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000877 if (is_undef(p->src_previous))
878 return register_input(p, FRAG_ATTRIB_COL0);
879 else
880 return p->src_previous;
Brian Paul6947f852009-01-23 17:32:09 -0700881
882 default:
883 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000884 }
885}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000886
Keith Whitwell15e75e02005-04-29 15:11:39 +0000887static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000888 GLuint mask,
889 GLuint unit,
890 GLuint source,
891 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000892{
893 struct ureg arg, src, one;
894
895 src = get_source(p, source, unit);
896
897 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000898 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000899 /* Get unused tmp,
900 * Emit tmp = 1.0 - arg.xyzw
901 */
902 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000903 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000904 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000905
Keith Whitwellce721142005-07-11 10:10:38 +0000906 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000907 if (mask == WRITEMASK_W)
908 return src;
909 else
Brian Paul22db5352005-11-19 23:45:10 +0000910 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000911 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000912 /* Get unused tmp,
913 * Emit tmp = 1.0 - arg.wwww
914 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000915 arg = get_temp(p);
916 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000917 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000918 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000919 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000920 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000921 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000922 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000923 case OPR_SRC_COLOR:
Brian Paul6947f852009-01-23 17:32:09 -0700924 return src;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000925 default:
Brian Paul6947f852009-01-23 17:32:09 -0700926 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000927 return src;
928 }
929}
930
Keith Whitwellce721142005-07-11 10:10:38 +0000931static GLboolean args_match( struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000932{
Brian Paul22db5352005-11-19 23:45:10 +0000933 GLuint i, nr = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000934
935 for (i = 0 ; i < nr ; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000936 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000937 return GL_FALSE;
938
Keith Whitwellce721142005-07-11 10:10:38 +0000939 switch(key->unit[unit].OptA[i].Operand) {
940 case OPR_SRC_ALPHA:
941 switch(key->unit[unit].OptRGB[i].Operand) {
942 case OPR_SRC_COLOR:
943 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000944 break;
945 default:
946 return GL_FALSE;
947 }
948 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000949 case OPR_ONE_MINUS_SRC_ALPHA:
950 switch(key->unit[unit].OptRGB[i].Operand) {
951 case OPR_ONE_MINUS_SRC_COLOR:
952 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000953 break;
954 default:
955 return GL_FALSE;
956 }
957 break;
958 default:
959 return GL_FALSE; /* impossible */
960 }
961 }
962
963 return GL_TRUE;
964}
965
Keith Whitwell15e75e02005-04-29 15:11:39 +0000966static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000967 struct ureg dest,
968 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000969 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000970 GLuint unit,
971 GLuint nr,
972 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +0000973 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000974{
Brian Paul6947f852009-01-23 17:32:09 -0700975 struct ureg src[MAX_TERMS];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000976 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +0000977 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000978
Brian Paul6947f852009-01-23 17:32:09 -0700979 assert(nr <= MAX_TERMS);
980
Brian Paul00630842005-12-12 15:27:55 +0000981 tmp = undef; /* silence warning (bug 5318) */
982
Keith Whitwell15e75e02005-04-29 15:11:39 +0000983 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +0000984 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000985
986 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +0000987 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000988 if (mask == WRITEMASK_XYZW && !saturate)
989 return src[0];
990 else
Brian Paul7e807512005-11-05 17:10:45 +0000991 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000992 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +0000993 return emit_arith( p, OPCODE_MUL, 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:
Brian Paul7e807512005-11-05 17:10:45 +0000996 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000997 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000998 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000999 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001000 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +00001001 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001002 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +00001003 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +00001004 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
1005 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001006 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +00001007 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001008 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
1009 */
Brian Paul7e807512005-11-05 17:10:45 +00001010 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001011
Keith Whitwellce721142005-07-11 10:10:38 +00001012 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +00001013 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001014
Keith Whitwellce721142005-07-11 10:10:38 +00001015 case MODE_DOT3_RGBA:
1016 case MODE_DOT3_RGBA_EXT:
1017 case MODE_DOT3_RGB_EXT:
1018 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001019 struct ureg tmp0 = get_temp( p );
1020 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +00001021 struct ureg neg1 = register_scalar_const(p, -1);
1022 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001023
1024 /* tmp0 = 2*src0 - 1
1025 * tmp1 = 2*src1 - 1
1026 *
1027 * dst = tmp0 dot3 tmp1
1028 */
Brian Paul7e807512005-11-05 17:10:45 +00001029 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001030 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001031
Brian Paulaa206952005-09-16 18:14:24 +00001032 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001033 tmp1 = tmp0;
1034 else
Brian Paul7e807512005-11-05 17:10:45 +00001035 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001036 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +00001037 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001038 return dest;
1039 }
Keith Whitwellce721142005-07-11 10:10:38 +00001040 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001041 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001042 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001043 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +00001044 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001045 /* Arg0 * Arg2 + Arg1 - 0.5 */
1046 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001047 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +00001048 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
1049 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001050 return dest;
1051 }
Keith Whitwellce721142005-07-11 10:10:38 +00001052 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001053 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001054 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001055 return dest;
Brian Paul6947f852009-01-23 17:32:09 -07001056 case MODE_ADD_PRODUCTS:
1057 /* Arg0 * Arg1 + Arg2 * Arg3 */
1058 {
1059 struct ureg tmp0 = get_temp(p);
1060 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1061 emit_arith( p, OPCODE_MAD, dest, mask, saturate, src[2], src[3], tmp0 );
1062 }
1063 return dest;
1064 case MODE_ADD_PRODUCTS_SIGNED:
1065 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1066 {
1067 struct ureg tmp0 = get_temp(p);
1068 half = get_half(p);
1069 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1070 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[2], src[3], tmp0 );
1071 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1072 }
1073 return dest;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001074 case MODE_BUMP_ENVMAP_ATI:
1075 /* special - not handled here */
1076 assert(0);
1077 return src[0];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001078 default:
Brian Paul6947f852009-01-23 17:32:09 -07001079 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001080 return src[0];
1081 }
1082}
1083
Keith Whitwell15e75e02005-04-29 15:11:39 +00001084
Brian Paul22db5352005-11-19 23:45:10 +00001085/**
1086 * Generate instructions for one texture unit's env/combiner mode.
1087 */
1088static struct ureg
1089emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001090{
Keith Whitwellce721142005-07-11 10:10:38 +00001091 struct state_key *key = p->state;
Brian Paul22db5352005-11-19 23:45:10 +00001092 GLboolean saturate = (unit < p->last_tex_stage);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001093 GLuint rgb_shift, alpha_shift;
1094 struct ureg out, shift;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001095 struct ureg dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001096
Keith Whitwellce721142005-07-11 10:10:38 +00001097 if (!key->unit[unit].enabled) {
1098 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001099 }
Roland Scheidegger114152e2009-03-12 15:01:16 +01001100 if (key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1101 /* this isn't really a env stage delivering a color and handled elsewhere */
1102 return get_source(p, SRC_PREVIOUS, 0);
1103 }
Keith Whitwellce721142005-07-11 10:10:38 +00001104
1105 switch (key->unit[unit].ModeRGB) {
1106 case MODE_DOT3_RGB_EXT:
1107 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001108 rgb_shift = 0;
1109 break;
Keith Whitwellce721142005-07-11 10:10:38 +00001110 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001111 alpha_shift = 0;
1112 rgb_shift = 0;
1113 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001114 default:
Keith Whitwellce721142005-07-11 10:10:38 +00001115 rgb_shift = key->unit[unit].ScaleShiftRGB;
1116 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001117 break;
1118 }
Keith Whitwellce721142005-07-11 10:10:38 +00001119
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001120 /* If this is the very last calculation, emit direct to output reg:
1121 */
Keith Whitwellce721142005-07-11 10:10:38 +00001122 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001123 unit != p->last_tex_stage ||
1124 alpha_shift ||
1125 rgb_shift)
1126 dest = get_temp( p );
1127 else
Brian Paul8d475822009-02-28 11:49:46 -07001128 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLOR);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001129
1130 /* Emit the RGB and A combine ops
1131 */
Keith Whitwellce721142005-07-11 10:10:38 +00001132 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
1133 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001134 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1135 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001136 key->unit[unit].NumArgsRGB,
1137 key->unit[unit].ModeRGB,
1138 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001139 }
Keith Whitwellce721142005-07-11 10:10:38 +00001140 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +02001141 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001142
1143 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1144 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001145 key->unit[unit].NumArgsRGB,
1146 key->unit[unit].ModeRGB,
1147 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001148 }
1149 else {
1150 /* Need to do something to stop from re-emitting identical
1151 * argument calculations here:
1152 */
1153 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
1154 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001155 key->unit[unit].NumArgsRGB,
1156 key->unit[unit].ModeRGB,
1157 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001158 out = emit_combine( p, dest, WRITEMASK_W, saturate,
1159 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001160 key->unit[unit].NumArgsA,
1161 key->unit[unit].ModeA,
1162 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001163 }
1164
1165 /* Deal with the final shift:
1166 */
1167 if (alpha_shift || rgb_shift) {
1168 if (rgb_shift == alpha_shift) {
José Fonseca452a5922008-05-31 18:14:09 +09001169 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001170 }
1171 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001172 shift = register_const4f(p,
José Fonseca452a5922008-05-31 18:14:09 +09001173 (GLfloat)(1<<rgb_shift),
1174 (GLfloat)(1<<rgb_shift),
1175 (GLfloat)(1<<rgb_shift),
1176 (GLfloat)(1<<alpha_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001177 }
Brian Paul7e807512005-11-05 17:10:45 +00001178 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001179 saturate, out, shift, undef );
1180 }
1181 else
1182 return out;
1183}
1184
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001185
Brian Paul22db5352005-11-19 23:45:10 +00001186/**
1187 * Generate instruction for getting a texture source term.
1188 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001189static void load_texture( struct texenv_fragment_program *p, GLuint unit )
1190{
1191 if (is_undef(p->src_texture[unit])) {
Brian Paul44e018c2009-02-20 13:42:08 -07001192 GLuint texTarget = p->state->unit[unit].source_index;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001193 struct ureg texcoord;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001194 struct ureg tmp = get_tex_temp( p );
1195
Roland Scheidegger114152e2009-03-12 15:01:16 +01001196 if (is_undef(p->texcoord_tex[unit])) {
1197 texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
1198 }
1199 else {
1200 /* might want to reuse this reg for tex output actually */
1201 texcoord = p->texcoord_tex[unit];
1202 }
1203
Brian Paul44e018c2009-02-20 13:42:08 -07001204 if (texTarget == TEXTURE_UNKNOWN_INDEX)
Brian Paulbfb5ea32005-07-19 18:20:04 +00001205 program_error(p, "TexSrcBit");
Keith Whitwellce721142005-07-11 10:10:38 +00001206
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001207 /* TODO: Use D0_MASK_XY where possible.
1208 */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +02001209 if (p->state->unit[unit].enabled) {
Brian Paul44e018c2009-02-20 13:42:08 -07001210 GLboolean shadow = GL_FALSE;
1211
1212 if (p->state->unit[unit].shadow) {
1213 p->program->Base.ShadowSamplers |= 1 << unit;
1214 shadow = GL_TRUE;
1215 }
1216
Aapo Tahkolab8915342006-03-28 10:26:34 +00001217 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1218 tmp, WRITEMASK_XYZW,
Brian Paul44e018c2009-02-20 13:42:08 -07001219 unit, texTarget, shadow,
1220 texcoord );
Brian9b7e5a52007-12-14 11:16:49 -07001221
1222 p->program->Base.SamplersUsed |= (1 << unit);
Brian1fe385f2007-12-14 11:42:28 -07001223 /* This identity mapping should already be in place
1224 * (see _mesa_init_program_struct()) but let's be safe.
1225 */
1226 p->program->Base.SamplerUnits[unit] = unit;
Brian9b7e5a52007-12-14 11:16:49 -07001227 }
1228 else
Aapo Tahkolab8915342006-03-28 10:26:34 +00001229 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001230 }
1231}
1232
Keith Whitwell241b6b72005-05-23 09:50:34 +00001233static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +00001234 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001235{
1236 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +00001237 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001238 load_texture(p, unit);
1239 break;
1240
Keith Whitwellce721142005-07-11 10:10:38 +00001241 case SRC_TEXTURE0:
1242 case SRC_TEXTURE1:
1243 case SRC_TEXTURE2:
1244 case SRC_TEXTURE3:
1245 case SRC_TEXTURE4:
1246 case SRC_TEXTURE5:
1247 case SRC_TEXTURE6:
1248 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +00001249 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001250 break;
1251
1252 default:
Brian Paul6947f852009-01-23 17:32:09 -07001253 /* not a texture src - do nothing */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001254 break;
1255 }
Keith Whitwell241b6b72005-05-23 09:50:34 +00001256
1257 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001258}
1259
Brian Paul22db5352005-11-19 23:45:10 +00001260
1261/**
1262 * Generate instructions for loading all texture source terms.
1263 */
1264static GLboolean
1265load_texunit_sources( struct texenv_fragment_program *p, int unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001266{
Keith Whitwellce721142005-07-11 10:10:38 +00001267 struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +00001268 GLuint i;
1269
1270 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001271 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001272 }
Aapo Tahkolab8915342006-03-28 10:26:34 +00001273
1274 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1275 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1276 }
1277
Keith Whitwell241b6b72005-05-23 09:50:34 +00001278 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001279}
1280
Roland Scheidegger114152e2009-03-12 15:01:16 +01001281/**
1282 * Generate instructions for loading bump map textures.
1283 */
1284static GLboolean
1285load_texunit_bumpmap( struct texenv_fragment_program *p, int unit )
1286{
1287 struct state_key *key = p->state;
1288 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1289 struct ureg texcDst, bumpMapRes;
1290 struct ureg constdudvcolor = register_const4f(p, 0.0, 0.0, 0.0, 1.0);
1291 struct ureg texcSrc = register_input(p, FRAG_ATTRIB_TEX0 + bumpedUnitNr);
1292 struct ureg rotMat0 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_0, unit );
1293 struct ureg rotMat1 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_1, unit );
1294
1295 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1296
1297 bumpMapRes = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1298 texcDst = get_tex_temp( p );
1299 p->texcoord_tex[bumpedUnitNr] = texcDst;
1300
1301 /* apply rot matrix and add coords to be available in next phase */
1302 /* dest = (Arg0.xxxx * rotMat0 + Arg1) + (Arg0.yyyy * rotMat1) */
1303 /* note only 2 coords are affected the rest are left unchanged (mul by 0) */
1304 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1305 swizzle1(bumpMapRes, SWIZZLE_X), rotMat0, texcSrc );
1306 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1307 swizzle1(bumpMapRes, SWIZZLE_Y), rotMat1, texcDst );
1308
1309 /* move 0,0,0,1 into bumpmap src if someone (crossbar) is foolish
1310 enough to access this later, should optimize away */
1311 emit_arith( p, OPCODE_MOV, bumpMapRes, WRITEMASK_XYZW, 0, constdudvcolor, undef, undef );
1312
1313 return GL_TRUE;
1314}
Brian Paul22db5352005-11-19 23:45:10 +00001315
1316/**
1317 * Generate a new fragment program which implements the context's
1318 * current texture env/combine mode.
1319 */
1320static void
Brian Paule998c342006-10-29 21:17:18 +00001321create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001322 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001323{
Brian Paule998c342006-10-29 21:17:18 +00001324 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001325 struct texenv_fragment_program p;
1326 GLuint unit;
1327 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001328
Keith Whitwelle4902422005-05-11 15:16:35 +00001329 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +00001330 p.ctx = ctx;
Keith Whitwellce721142005-07-11 10:10:38 +00001331 p.state = key;
1332 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001333
Brian Paule998c342006-10-29 21:17:18 +00001334 /* During code generation, use locally-allocated instruction buffer,
1335 * then alloc dynamic storage below.
1336 */
1337 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001338 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001339 p.program->Base.NumTexIndirections = 1;
Brian21f99792007-01-09 11:00:21 -07001340 p.program->Base.NumTexInstructions = 0;
1341 p.program->Base.NumAluInstructions = 0;
Brian1240eb22007-03-22 08:50:20 -06001342 p.program->Base.String = NULL;
Keith Whitwell9ca88152005-05-10 09:56:02 +00001343 p.program->Base.NumInstructions =
Brian Paule998c342006-10-29 21:17:18 +00001344 p.program->Base.NumTemporaries =
1345 p.program->Base.NumParameters =
1346 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001347 p.program->Base.Parameters = _mesa_new_parameter_list();
Keith Whitwelldbeea252005-05-10 13:57:50 +00001348
Brian Paulde997602005-11-12 17:53:14 +00001349 p.program->Base.InputsRead = 0;
Brian Paul8d475822009-02-28 11:49:46 -07001350 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLOR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001351
Roland Scheidegger114152e2009-03-12 15:01:16 +01001352 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001353 p.src_texture[unit] = undef;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001354 p.texcoord_tex[unit] = undef;
1355 }
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001356
Keith Whitwell47b29f52005-05-04 11:44:44 +00001357 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001358 p.half = undef;
1359 p.zero = undef;
1360 p.one = undef;
1361
Keith Whitwell15e75e02005-04-29 15:11:39 +00001362 p.last_tex_stage = 0;
Brian93fef222007-10-29 12:25:46 -06001363 release_temps(ctx, &p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001364
Keith Whitwellce721142005-07-11 10:10:38 +00001365 if (key->enabled_units) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001366 GLboolean needbumpstage = GL_FALSE;
1367 /* Zeroth pass - bump map textures first */
1368 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
1369 if (key->unit[unit].enabled && key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1370 needbumpstage = GL_TRUE;
1371 load_texunit_bumpmap( &p, unit );
1372 }
1373 if (needbumpstage)
1374 p.program->Base.NumTexIndirections++;
1375
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001376 /* First pass - to support texture_env_crossbar, first identify
1377 * all referenced texture sources and emit texld instructions
1378 * for each:
1379 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001380 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001381 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001382 load_texunit_sources( &p, unit );
1383 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001384 }
1385
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001386 /* Second pass - emit combine instructions to build final color:
1387 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001388 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001389 if (key->enabled_units & (1<<unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001390 p.src_previous = emit_texenv( &p, unit );
Brian Paul5620c202008-09-26 11:18:06 -06001391 reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
Brian93fef222007-10-29 12:25:46 -06001392 release_temps(ctx, &p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001393 }
1394 }
1395
Keith Whitwellce721142005-07-11 10:10:38 +00001396 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul8d475822009-02-28 11:49:46 -07001397 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLOR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001398
Keith Whitwellce721142005-07-11 10:10:38 +00001399 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001400 /* Emit specular add.
1401 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001402 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001403 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1404 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001405 }
Brian Paulaa206952005-09-16 18:14:24 +00001406 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001407 /* Will wind up in here if no texture enabled or a couple of
1408 * other scenarios (GL_REPLACE for instance).
1409 */
Brian Paul7e807512005-11-05 17:10:45 +00001410 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001411 }
1412
Keith Whitwell47b29f52005-05-04 11:44:44 +00001413 /* Finish up:
1414 */
Brian Paul7e807512005-11-05 17:10:45 +00001415 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001416
Keith Whitwellce721142005-07-11 10:10:38 +00001417 if (key->fog_enabled) {
1418 /* Pull fog mode from GLcontext, the value in the state key is
1419 * a reduced value and not what is expected in FogOption
1420 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001421 p.program->FogOption = ctx->Fog.Mode;
Brian19d77d62007-09-18 19:29:26 -06001422 p.program->Base.InputsRead |= FRAG_BIT_FOGC; /* XXX new */
Keith Whitwellce721142005-07-11 10:10:38 +00001423 } else
Keith Whitwell276330b2005-05-09 17:58:13 +00001424 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001425
Brian21f99792007-01-09 11:00:21 -07001426 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001427 program_error(&p, "Exceeded max nr indirect texture lookups");
1428
Brian21f99792007-01-09 11:00:21 -07001429 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001430 program_error(&p, "Exceeded max TEX instructions");
1431
Brian21f99792007-01-09 11:00:21 -07001432 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001433 program_error(&p, "Exceeded max ALU instructions");
1434
Brian Paul5d7b49f2005-11-19 23:12:20 +00001435 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001436
Brian Paule998c342006-10-29 21:17:18 +00001437 /* Allocate final instruction array */
Brianc81cce72007-09-25 15:18:51 -06001438 p.program->Base.Instructions
1439 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1440 if (!p.program->Base.Instructions) {
Brian Paule998c342006-10-29 21:17:18 +00001441 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1442 "generating tex env program");
1443 return;
1444 }
Brianc81cce72007-09-25 15:18:51 -06001445 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1446 p.program->Base.NumInstructions);
1447
1448 if (p.program->FogOption) {
1449 _mesa_append_fog_code(ctx, p.program);
1450 p.program->FogOption = GL_NONE;
1451 }
1452
Brian Paule998c342006-10-29 21:17:18 +00001453
Keith Whitwelldbeea252005-05-10 13:57:50 +00001454 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001455 */
Brian Paule998c342006-10-29 21:17:18 +00001456 if (ctx->Driver.ProgramStringNotify) {
1457 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1458 &p.program->Base );
1459 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001460
Brian Paule998c342006-10-29 21:17:18 +00001461 if (DISASSEM) {
1462 _mesa_print_program(&p.program->Base);
1463 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001464 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001465}
1466
Brian Paul5d7b49f2005-11-19 23:12:20 +00001467
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001468/**
1469 * Return a fragment program which implements the current
1470 * fixed-function texture, fog and color-sum operations.
1471 */
1472struct gl_fragment_program *
1473_mesa_get_fixed_func_fragment_program(GLcontext *ctx)
Keith Whitwellce721142005-07-11 10:10:38 +00001474{
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001475 struct gl_fragment_program *prog;
1476 struct state_key key;
1477
1478 make_state_key(ctx, &key);
1479
1480 prog = (struct gl_fragment_program *)
1481 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1482 &key, sizeof(key));
Keith Whitwellce721142005-07-11 10:10:38 +00001483
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001484 if (!prog) {
1485 prog = (struct gl_fragment_program *)
1486 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1487
1488 create_new_program(ctx, &key, prog);
1489
1490 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1491 &key, sizeof(key), &prog->Base);
Keith Whitwellce721142005-07-11 10:10:38 +00001492 }
1493
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001494 return prog;
Keith Whitwellce721142005-07-11 10:10:38 +00001495}