blob: 869d8d026be6f19eddf9cced7a1f974d328b69fd [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
Eric Anholt9cea84b2009-07-16 18:41:03 -070065static GLboolean
66texenv_doing_secondary_color(GLcontext *ctx)
67{
68 if (ctx->Light.Enabled &&
69 (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR))
70 return GL_TRUE;
71
72 if (ctx->Fog.ColorSumEnabled)
73 return GL_TRUE;
74
75 return GL_FALSE;
76}
Brian Paul5b5c9312008-05-07 18:51:44 -060077
Brian Paule998c342006-10-29 21:17:18 +000078/**
Brian Paulb5e1a932008-09-25 18:40:16 -060079 * Up to nine instructions per tex unit, plus fog, specular color.
Brian Paule998c342006-10-29 21:17:18 +000080 */
Brian Paul0815ebc2009-01-02 16:32:26 -070081#define MAX_INSTRUCTIONS ((MAX_TEXTURE_COORD_UNITS * 9) + 12)
Keith Whitwell15e75e02005-04-29 15:11:39 +000082
Keith Whitwell269e3892005-05-12 10:28:43 +000083#define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
Keith Whitwell15e75e02005-04-29 15:11:39 +000084
Keith Whitwellce721142005-07-11 10:10:38 +000085struct mode_opt {
Brian Paulf337e2c2009-09-01 15:49:55 -060086 GLuint Source:4; /**< SRC_x */
87 GLuint Operand:3; /**< OPR_x */
Keith Whitwellce721142005-07-11 10:10:38 +000088};
89
90struct state_key {
Keith Whitwell6280e332008-10-03 13:51:56 +010091 GLuint nr_enabled_units:8;
92 GLuint enabled_units:8;
Brian Paul5d7b49f2005-11-19 23:12:20 +000093 GLuint separate_specular:1;
94 GLuint fog_enabled:1;
Brian Paul9ed03152009-09-01 15:57:36 -060095 GLuint fog_mode:2; /**< FOG_x */
Keith Whitwell6280e332008-10-03 13:51:56 +010096 GLuint inputs_available:12;
Keith Whitwellce721142005-07-11 10:10:38 +000097
98 struct {
Brian Paul5d7b49f2005-11-19 23:12:20 +000099 GLuint enabled:1;
Brian Paul9ed03152009-09-01 15:57:36 -0600100 GLuint source_index:3; /**< TEXTURE_x_INDEX */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +0200101 GLuint shadow:1;
Brian Paul5d7b49f2005-11-19 23:12:20 +0000102 GLuint ScaleShiftRGB:2;
103 GLuint ScaleShiftA:2;
Keith Whitwellce721142005-07-11 10:10:38 +0000104
Brian Paul9ed03152009-09-01 15:57:36 -0600105 GLuint NumArgsRGB:3; /**< up to MAX_COMBINER_TERMS */
Brian Paulf337e2c2009-09-01 15:49:55 -0600106 GLuint ModeRGB:5; /**< MODE_x */
Brian Paulbd9b2be2009-03-08 17:58:54 -0600107 struct mode_opt OptRGB[MAX_COMBINER_TERMS];
Keith Whitwellce721142005-07-11 10:10:38 +0000108
Brian Paul9ed03152009-09-01 15:57:36 -0600109 GLuint NumArgsA:3; /**< up to MAX_COMBINER_TERMS */
Brian Paulf337e2c2009-09-01 15:49:55 -0600110 GLuint ModeA:5; /**< MODE_x */
Brian Paulbd9b2be2009-03-08 17:58:54 -0600111 struct mode_opt OptA[MAX_COMBINER_TERMS];
Brian Pauld55a28e2009-09-01 15:34:16 -0600112 } unit[MAX_TEXTURE_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000113};
114
115#define FOG_LINEAR 0
116#define FOG_EXP 1
117#define FOG_EXP2 2
118#define FOG_UNKNOWN 3
119
120static GLuint translate_fog_mode( GLenum mode )
121{
122 switch (mode) {
123 case GL_LINEAR: return FOG_LINEAR;
124 case GL_EXP: return FOG_EXP;
125 case GL_EXP2: return FOG_EXP2;
126 default: return FOG_UNKNOWN;
127 }
128}
129
130#define OPR_SRC_COLOR 0
131#define OPR_ONE_MINUS_SRC_COLOR 1
132#define OPR_SRC_ALPHA 2
133#define OPR_ONE_MINUS_SRC_ALPHA 3
134#define OPR_ZERO 4
135#define OPR_ONE 5
136#define OPR_UNKNOWN 7
137
138static GLuint translate_operand( GLenum operand )
139{
140 switch (operand) {
141 case GL_SRC_COLOR: return OPR_SRC_COLOR;
142 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
143 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
144 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
145 case GL_ZERO: return OPR_ZERO;
146 case GL_ONE: return OPR_ONE;
Brian Paul6947f852009-01-23 17:32:09 -0700147 default:
148 assert(0);
149 return OPR_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000150 }
151}
152
153#define SRC_TEXTURE 0
154#define SRC_TEXTURE0 1
155#define SRC_TEXTURE1 2
156#define SRC_TEXTURE2 3
157#define SRC_TEXTURE3 4
158#define SRC_TEXTURE4 5
159#define SRC_TEXTURE5 6
160#define SRC_TEXTURE6 7
161#define SRC_TEXTURE7 8
162#define SRC_CONSTANT 9
163#define SRC_PRIMARY_COLOR 10
164#define SRC_PREVIOUS 11
Brian Paul6947f852009-01-23 17:32:09 -0700165#define SRC_ZERO 12
Keith Whitwellce721142005-07-11 10:10:38 +0000166#define SRC_UNKNOWN 15
167
168static GLuint translate_source( GLenum src )
169{
170 switch (src) {
171 case GL_TEXTURE: return SRC_TEXTURE;
172 case GL_TEXTURE0:
173 case GL_TEXTURE1:
174 case GL_TEXTURE2:
175 case GL_TEXTURE3:
176 case GL_TEXTURE4:
177 case GL_TEXTURE5:
178 case GL_TEXTURE6:
179 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
180 case GL_CONSTANT: return SRC_CONSTANT;
181 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
182 case GL_PREVIOUS: return SRC_PREVIOUS;
Brian Paul6947f852009-01-23 17:32:09 -0700183 case GL_ZERO:
184 return SRC_ZERO;
185 default:
186 assert(0);
187 return SRC_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000188 }
189}
190
Brian Paul6947f852009-01-23 17:32:09 -0700191#define MODE_REPLACE 0 /* r = a0 */
192#define MODE_MODULATE 1 /* r = a0 * a1 */
193#define MODE_ADD 2 /* r = a0 + a1 */
194#define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
195#define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
196#define MODE_SUBTRACT 5 /* r = a0 - a1 */
197#define MODE_DOT3_RGB 6 /* r = a0 . a1 */
198#define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
199#define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
200#define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
201#define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
202#define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
203#define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
204#define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
205#define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
Roland Scheidegger114152e2009-03-12 15:01:16 +0100206#define MODE_BUMP_ENVMAP_ATI 15 /* special */
207#define MODE_UNKNOWN 16
Keith Whitwellce721142005-07-11 10:10:38 +0000208
Brian Paul6947f852009-01-23 17:32:09 -0700209/**
210 * Translate GL combiner state into a MODE_x value
211 */
212static GLuint translate_mode( GLenum envMode, GLenum mode )
Keith Whitwellce721142005-07-11 10:10:38 +0000213{
214 switch (mode) {
215 case GL_REPLACE: return MODE_REPLACE;
216 case GL_MODULATE: return MODE_MODULATE;
Brian Paul6947f852009-01-23 17:32:09 -0700217 case GL_ADD:
218 if (envMode == GL_COMBINE4_NV)
219 return MODE_ADD_PRODUCTS;
220 else
221 return MODE_ADD;
222 case GL_ADD_SIGNED:
223 if (envMode == GL_COMBINE4_NV)
224 return MODE_ADD_PRODUCTS_SIGNED;
225 else
226 return MODE_ADD_SIGNED;
Keith Whitwellce721142005-07-11 10:10:38 +0000227 case GL_INTERPOLATE: return MODE_INTERPOLATE;
228 case GL_SUBTRACT: return MODE_SUBTRACT;
229 case GL_DOT3_RGB: return MODE_DOT3_RGB;
230 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
231 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
232 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
233 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
234 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
235 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
Roland Scheidegger114152e2009-03-12 15:01:16 +0100236 case GL_BUMP_ENVMAP_ATI: return MODE_BUMP_ENVMAP_ATI;
Brian Paul6947f852009-01-23 17:32:09 -0700237 default:
238 assert(0);
239 return MODE_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000240 }
241}
242
Brian Paulf337e2c2009-09-01 15:49:55 -0600243
Brian Paulf337e2c2009-09-01 15:49:55 -0600244/**
245 * Translate TEXTURE_x_BIT to TEXTURE_x_INDEX.
246 */
Brian Paule00ac112005-09-15 05:00:45 +0000247static GLuint translate_tex_src_bit( GLbitfield bit )
Keith Whitwellce721142005-07-11 10:10:38 +0000248{
Brian Paulb5ec0a62009-09-01 15:51:23 -0600249 ASSERT(bit);
250 return _mesa_ffs(bit) - 1;
Keith Whitwellce721142005-07-11 10:10:38 +0000251}
252
Brian Paulf337e2c2009-09-01 15:49:55 -0600253
Keith Whitwell1680ef82008-10-03 17:30:59 +0100254#define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
255#define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
256
Brian Paul239617f2008-10-07 11:22:47 -0600257/**
258 * Identify all possible varying inputs. The fragment program will
Keith Whitwell1680ef82008-10-03 17:30:59 +0100259 * never reference non-varying inputs, but will track them via state
260 * constants instead.
261 *
262 * This function figures out all the inputs that the fragment program
263 * has access to. The bitmask is later reduced to just those which
264 * are actually referenced.
265 */
Brian Paul239617f2008-10-07 11:22:47 -0600266static GLbitfield get_fp_input_mask( GLcontext *ctx )
Keith Whitwell1680ef82008-10-03 17:30:59 +0100267{
Eric Anholte5f63c42009-06-02 07:47:20 -0700268 /* _NEW_PROGRAM */
Brian Paulf0b07942008-12-17 14:04:03 -0700269 const GLboolean vertexShader = (ctx->Shader.CurrentProgram &&
Eric Anholt40990d92009-08-03 12:36:52 -0700270 ctx->Shader.CurrentProgram->LinkStatus &&
Brian Paulf0b07942008-12-17 14:04:03 -0700271 ctx->Shader.CurrentProgram->VertexProgram);
272 const GLboolean vertexProgram = ctx->VertexProgram._Enabled;
Brian Paul239617f2008-10-07 11:22:47 -0600273 GLbitfield fp_inputs = 0x0;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100274
Brian Paul6d4d51d2008-10-10 13:39:14 -0600275 if (ctx->VertexProgram._Overriden) {
276 /* Somebody's messing with the vertex program and we don't have
277 * a clue what's happening. Assume that it could be producing
278 * all possible outputs.
279 */
280 fp_inputs = ~0;
281 }
282 else if (ctx->RenderMode == GL_FEEDBACK) {
Eric Anholte5f63c42009-06-02 07:47:20 -0700283 /* _NEW_RENDERMODE */
Brian Paul6d4d51d2008-10-10 13:39:14 -0600284 fp_inputs = (FRAG_BIT_COL0 | FRAG_BIT_TEX0);
285 }
Brian Paulf0b07942008-12-17 14:04:03 -0700286 else if (!(vertexProgram || vertexShader) ||
Brian Pauld7296a12008-12-17 11:29:06 -0700287 !ctx->VertexProgram._Current) {
Brian Paulf0b07942008-12-17 14:04:03 -0700288 /* Fixed function vertex logic */
Eric Anholte5f63c42009-06-02 07:47:20 -0700289 /* _NEW_ARRAY */
Brian Paul239617f2008-10-07 11:22:47 -0600290 GLbitfield varying_inputs = ctx->varying_vp_inputs;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100291
Keith Whitwell97e63432008-10-20 13:03:45 +0100292 /* These get generated in the setup routine regardless of the
293 * vertex program:
294 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700295 /* _NEW_POINT */
Keith Whitwell97e63432008-10-20 13:03:45 +0100296 if (ctx->Point.PointSprite)
297 varying_inputs |= FRAG_BITS_TEX_ANY;
298
Keith Whitwell1680ef82008-10-03 17:30:59 +0100299 /* First look at what values may be computed by the generated
300 * vertex program:
301 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700302 /* _NEW_LIGHT */
Keith Whitwell1680ef82008-10-03 17:30:59 +0100303 if (ctx->Light.Enabled) {
304 fp_inputs |= FRAG_BIT_COL0;
305
Eric Anholt9cea84b2009-07-16 18:41:03 -0700306 if (texenv_doing_secondary_color(ctx))
Keith Whitwell1680ef82008-10-03 17:30:59 +0100307 fp_inputs |= FRAG_BIT_COL1;
308 }
309
Eric Anholte5f63c42009-06-02 07:47:20 -0700310 /* _NEW_TEXTURE */
Keith Whitwell1680ef82008-10-03 17:30:59 +0100311 fp_inputs |= (ctx->Texture._TexGenEnabled |
312 ctx->Texture._TexMatEnabled) << FRAG_ATTRIB_TEX0;
313
314 /* Then look at what might be varying as a result of enabled
315 * arrays, etc:
316 */
Brian Paul6807d962009-08-07 09:42:28 -0600317 if (varying_inputs & VERT_BIT_COLOR0)
318 fp_inputs |= FRAG_BIT_COL0;
319 if (varying_inputs & VERT_BIT_COLOR1)
320 fp_inputs |= FRAG_BIT_COL1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100321
322 fp_inputs |= (((varying_inputs & VERT_BIT_TEX_ANY) >> VERT_ATTRIB_TEX0)
323 << FRAG_ATTRIB_TEX0);
324
325 }
326 else {
327 /* calculate from vp->outputs */
Brian Paul2389c052008-12-17 19:01:34 -0700328 struct gl_vertex_program *vprog;
329 GLbitfield vp_outputs;
330
331 /* Choose GLSL vertex shader over ARB vertex program. Need this
332 * since vertex shader state validation comes after fragment state
333 * validation (see additional comments in state.c).
334 */
335 if (vertexShader)
336 vprog = ctx->Shader.CurrentProgram->VertexProgram;
337 else
Eric Anholta9ba1bf2009-08-03 12:38:56 -0700338 vprog = ctx->VertexProgram.Current;
Brian Paul2389c052008-12-17 19:01:34 -0700339
340 vp_outputs = vprog->Base.OutputsWritten;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100341
Keith Whitwell97e63432008-10-20 13:03:45 +0100342 /* These get generated in the setup routine regardless of the
343 * vertex program:
344 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700345 /* _NEW_POINT */
Keith Whitwell97e63432008-10-20 13:03:45 +0100346 if (ctx->Point.PointSprite)
347 vp_outputs |= FRAG_BITS_TEX_ANY;
348
Brian Paul6807d962009-08-07 09:42:28 -0600349 if (vp_outputs & (1 << VERT_RESULT_COL0))
350 fp_inputs |= FRAG_BIT_COL0;
351 if (vp_outputs & (1 << VERT_RESULT_COL1))
352 fp_inputs |= FRAG_BIT_COL1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100353
Keith Whitwell0370d6b2008-10-04 12:41:56 +0100354 fp_inputs |= (((vp_outputs & VERT_RESULT_TEX_ANY) >> VERT_RESULT_TEX0)
355 << FRAG_ATTRIB_TEX0);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100356 }
357
358 return fp_inputs;
359}
360
361
Brian Paul22db5352005-11-19 23:45:10 +0000362/**
363 * Examine current texture environment state and generate a unique
364 * key to identify it.
365 */
Keith Whitwell8065c122006-05-22 16:09:27 +0000366static void make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000367{
Keith Whitwellce721142005-07-11 10:10:38 +0000368 GLuint i, j;
Brian Paul239617f2008-10-07 11:22:47 -0600369 GLbitfield inputs_referenced = FRAG_BIT_COL0;
Brian Paulf337e2c2009-09-01 15:49:55 -0600370 const GLbitfield inputs_available = get_fp_input_mask( ctx );
Keith Whitwell1680ef82008-10-03 17:30:59 +0100371
Keith Whitwell8065c122006-05-22 16:09:27 +0000372 memset(key, 0, sizeof(*key));
373
Eric Anholte5f63c42009-06-02 07:47:20 -0700374 /* _NEW_TEXTURE */
Brian Paule9b34882008-12-31 11:54:02 -0700375 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000376 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Brian Paulf337e2c2009-09-01 15:49:55 -0600377 const struct gl_texture_object *texObj = texUnit->_Current;
378 const struct gl_tex_env_combine_state *comb = texUnit->_CurrentCombine;
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800379 GLenum format;
Roland Scheideggerbf4a0fa2008-02-15 17:26:06 +0100380
381 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
Keith Whitwellce721142005-07-11 10:10:38 +0000382 continue;
383
Brian Paulf337e2c2009-09-01 15:49:55 -0600384 format = texObj->Image[0][texObj->BaseLevel]->_BaseFormat;
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800385
Keith Whitwellce721142005-07-11 10:10:38 +0000386 key->unit[i].enabled = 1;
387 key->enabled_units |= (1<<i);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100388 key->nr_enabled_units = i+1;
389 inputs_referenced |= FRAG_BIT_TEX(i);
Keith Whitwellce721142005-07-11 10:10:38 +0000390
Brian Paulf337e2c2009-09-01 15:49:55 -0600391 key->unit[i].source_index =
392 translate_tex_src_bit(texUnit->_ReallyEnabled);
393
394 key->unit[i].shadow = ((texObj->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800395 ((format == GL_DEPTH_COMPONENT) ||
396 (format == GL_DEPTH_STENCIL_EXT)));
Keith Whitwellce721142005-07-11 10:10:38 +0000397
Brian Paulf337e2c2009-09-01 15:49:55 -0600398 key->unit[i].NumArgsRGB = comb->_NumArgsRGB;
399 key->unit[i].NumArgsA = comb->_NumArgsA;
Keith Whitwellce721142005-07-11 10:10:38 +0000400
401 key->unit[i].ModeRGB =
Brian Paulf337e2c2009-09-01 15:49:55 -0600402 translate_mode(texUnit->EnvMode, comb->ModeRGB);
Keith Whitwellce721142005-07-11 10:10:38 +0000403 key->unit[i].ModeA =
Brian Paulf337e2c2009-09-01 15:49:55 -0600404 translate_mode(texUnit->EnvMode, comb->ModeA);
Roland Scheidegger114152e2009-03-12 15:01:16 +0100405
Brian Paulf337e2c2009-09-01 15:49:55 -0600406 key->unit[i].ScaleShiftRGB = comb->ScaleShiftRGB;
407 key->unit[i].ScaleShiftA = comb->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000408
Brian Paulbd9b2be2009-03-08 17:58:54 -0600409 for (j = 0; j < MAX_COMBINER_TERMS; j++) {
Brian Paulf337e2c2009-09-01 15:49:55 -0600410 key->unit[i].OptRGB[j].Operand = translate_operand(comb->OperandRGB[j]);
411 key->unit[i].OptA[j].Operand = translate_operand(comb->OperandA[j]);
412 key->unit[i].OptRGB[j].Source = translate_source(comb->SourceRGB[j]);
413 key->unit[i].OptA[j].Source = translate_source(comb->SourceA[j]);
Keith Whitwellce721142005-07-11 10:10:38 +0000414 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100415
416 if (key->unit[i].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
417 /* requires some special translation */
418 key->unit[i].NumArgsRGB = 2;
419 key->unit[i].ScaleShiftRGB = 0;
420 key->unit[i].OptRGB[0].Operand = OPR_SRC_COLOR;
421 key->unit[i].OptRGB[0].Source = SRC_TEXTURE;
422 key->unit[i].OptRGB[1].Operand = OPR_SRC_COLOR;
423 key->unit[i].OptRGB[1].Source = texUnit->BumpTarget - GL_TEXTURE0 + SRC_TEXTURE0;
424 }
Keith Whitwellce721142005-07-11 10:10:38 +0000425 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100426
Eric Anholt9cea84b2009-07-16 18:41:03 -0700427 /* _NEW_LIGHT | _NEW_FOG */
428 if (texenv_doing_secondary_color(ctx)) {
Keith Whitwellce721142005-07-11 10:10:38 +0000429 key->separate_specular = 1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100430 inputs_referenced |= FRAG_BIT_COL1;
431 }
Keith Whitwellce721142005-07-11 10:10:38 +0000432
Eric Anholte5f63c42009-06-02 07:47:20 -0700433 /* _NEW_FOG */
Keith Whitwellce721142005-07-11 10:10:38 +0000434 if (ctx->Fog.Enabled) {
435 key->fog_enabled = 1;
436 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100437 inputs_referenced |= FRAG_BIT_FOGC; /* maybe */
Keith Whitwellce721142005-07-11 10:10:38 +0000438 }
Keith Whitwell1680ef82008-10-03 17:30:59 +0100439
440 key->inputs_available = (inputs_available & inputs_referenced);
Keith Whitwellce721142005-07-11 10:10:38 +0000441}
442
Brian Paul239617f2008-10-07 11:22:47 -0600443/**
444 * Use uregs to represent registers internally, translate to Mesa's
Keith Whitwell15e75e02005-04-29 15:11:39 +0000445 * expected formats on emit.
446 *
447 * NOTE: These are passed by value extensively in this file rather
448 * than as usual by pointer reference. If this disturbs you, try
449 * remembering they are just 32bits in size.
450 *
451 * GCC is smart enough to deal with these dword-sized structures in
452 * much the same way as if I had defined them as dwords and was using
453 * macros to access and set the fields. This is much nicer and easier
454 * to evolve.
455 */
456struct ureg {
457 GLuint file:4;
458 GLuint idx:8;
459 GLuint negatebase:1;
460 GLuint abs:1;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000461 GLuint swz:12;
Brian Paulc7ba2b82009-09-01 15:58:49 -0600462 GLuint pad:6;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000463};
464
Brian Paul13abf912006-04-13 19:17:13 +0000465static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000466 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000467 ~0,
468 0,
469 0,
470 0,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000471 0
472};
473
Keith Whitwell15e75e02005-04-29 15:11:39 +0000474
Brian Paul239617f2008-10-07 11:22:47 -0600475/** State used to build the fragment program:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000476 */
477struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000478 struct gl_fragment_program *program;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000479 GLcontext *ctx;
Keith Whitwellce721142005-07-11 10:10:38 +0000480 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000481
Brian Paul239617f2008-10-07 11:22:47 -0600482 GLbitfield alu_temps; /**< Track texture indirections, see spec. */
483 GLbitfield temps_output; /**< Track texture indirections, see spec. */
484 GLbitfield temp_in_use; /**< Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000485 GLboolean error;
486
Brian Paule9b34882008-12-31 11:54:02 -0700487 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000488 /* Reg containing each texture unit's sampled texture color,
489 * else undef.
490 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000491
Roland Scheidegger114152e2009-03-12 15:01:16 +0100492 struct ureg texcoord_tex[MAX_TEXTURE_COORD_UNITS];
493 /* Reg containing texcoord for a texture unit,
494 * needed for bump mapping, else undef.
495 */
496
Brian Paul239617f2008-10-07 11:22:47 -0600497 struct ureg src_previous; /**< Reg containing color from previous
Keith Whitwell15e75e02005-04-29 15:11:39 +0000498 * stage. May need to be decl'd.
499 */
500
Brian Paul239617f2008-10-07 11:22:47 -0600501 GLuint last_tex_stage; /**< Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000502
503 struct ureg half;
504 struct ureg one;
505 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000506};
507
508
509
510static struct ureg make_ureg(GLuint file, GLuint idx)
511{
512 struct ureg reg;
513 reg.file = file;
514 reg.idx = idx;
515 reg.negatebase = 0;
516 reg.abs = 0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000517 reg.swz = SWIZZLE_NOOP;
518 reg.pad = 0;
519 return reg;
520}
521
522static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
523{
524 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
525 GET_SWZ(reg.swz, y),
526 GET_SWZ(reg.swz, z),
527 GET_SWZ(reg.swz, w));
528
529 return reg;
530}
531
532static struct ureg swizzle1( struct ureg reg, int x )
533{
534 return swizzle(reg, x, x, x, x);
535}
536
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000537static struct ureg negate( struct ureg reg )
538{
539 reg.negatebase ^= 1;
540 return reg;
541}
542
Keith Whitwell15e75e02005-04-29 15:11:39 +0000543static GLboolean is_undef( struct ureg reg )
544{
Alan Hourihane8d972652006-08-10 13:12:00 +0000545 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000546}
547
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000548
Keith Whitwell15e75e02005-04-29 15:11:39 +0000549static struct ureg get_temp( struct texenv_fragment_program *p )
550{
Brian Paul13abf912006-04-13 19:17:13 +0000551 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000552
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000553 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000554 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000555 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000556
557 /* Then any unused temporary:
558 */
559 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000560 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000561
Keith Whitwell15e75e02005-04-29 15:11:39 +0000562 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000563 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000564 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000565 }
566
Brian Paul13abf912006-04-13 19:17:13 +0000567 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000568 p->program->Base.NumTemporaries = bit;
569
Keith Whitwell93cd9232005-05-11 08:30:23 +0000570 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000571 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
572}
573
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000574static struct ureg get_tex_temp( struct texenv_fragment_program *p )
575{
576 int bit;
577
Brian Pauld01269a2008-09-25 18:27:22 -0600578 /* First try to find available temp not previously used (to avoid
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000579 * starting a new texture indirection). According to the spec, the
580 * ~p->temps_output isn't necessary, but will keep it there for
581 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000582 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000583 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000584
585 /* Then any unused temporary:
586 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000587 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000588 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000589
590 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000591 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000592 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000593 }
594
Brian Paul13abf912006-04-13 19:17:13 +0000595 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000596 p->program->Base.NumTemporaries = bit;
597
Keith Whitwell93cd9232005-05-11 08:30:23 +0000598 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000599 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
600}
601
Keith Whitwell15e75e02005-04-29 15:11:39 +0000602
Brian Paul5620c202008-09-26 11:18:06 -0600603/** Mark a temp reg as being no longer allocatable. */
604static void reserve_temp( struct texenv_fragment_program *p, struct ureg r )
605{
606 if (r.file == PROGRAM_TEMPORARY)
607 p->temps_output |= (1 << r.idx);
608}
609
610
Brian93fef222007-10-29 12:25:46 -0600611static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000612{
Brian93fef222007-10-29 12:25:46 -0600613 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000614
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000615 /* KW: To support tex_env_crossbar, don't release the registers in
616 * temps_output.
617 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000618 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000619 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000620 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000621 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000622}
623
624
Brian9fe3e2e2007-02-23 11:44:14 -0700625static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000626 GLint s0,
627 GLint s1,
628 GLint s2,
629 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700630 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000631{
Brian9fe3e2e2007-02-23 11:44:14 -0700632 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000633 GLuint idx;
634 tokens[0] = s0;
635 tokens[1] = s1;
636 tokens[2] = s2;
637 tokens[3] = s3;
638 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000639 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000640 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000641}
642
Keith Whitwell47b29f52005-05-04 11:44:44 +0000643
Brian9fe3e2e2007-02-23 11:44:14 -0700644#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
645#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
646#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
647#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000648
Keith Whitwell1680ef82008-10-03 17:30:59 +0100649static GLuint frag_to_vert_attrib( GLuint attrib )
650{
651 switch (attrib) {
652 case FRAG_ATTRIB_COL0: return VERT_ATTRIB_COLOR0;
653 case FRAG_ATTRIB_COL1: return VERT_ATTRIB_COLOR1;
654 default:
655 assert(attrib >= FRAG_ATTRIB_TEX0);
656 assert(attrib <= FRAG_ATTRIB_TEX7);
657 return attrib - FRAG_ATTRIB_TEX0 + VERT_ATTRIB_TEX0;
658 }
659}
660
Keith Whitwell47b29f52005-05-04 11:44:44 +0000661
662static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
663{
Keith Whitwell1680ef82008-10-03 17:30:59 +0100664 if (p->state->inputs_available & (1<<input)) {
665 p->program->Base.InputsRead |= (1 << input);
666 return make_ureg(PROGRAM_INPUT, input);
667 }
668 else {
669 GLuint idx = frag_to_vert_attrib( input );
670 return register_param3( p, STATE_INTERNAL, STATE_CURRENT_ATTRIB, idx );
671 }
Keith Whitwell47b29f52005-05-04 11:44:44 +0000672}
673
674
Brian Paul7e807512005-11-05 17:10:45 +0000675static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000676 struct ureg ureg )
677{
678 reg->File = ureg.file;
679 reg->Index = ureg.idx;
680 reg->Swizzle = ureg.swz;
Brian Paul7db7ff82009-04-14 22:14:30 -0600681 reg->Negate = ureg.negatebase ? NEGATE_XYZW : NEGATE_NONE;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000682 reg->Abs = ureg.abs;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000683}
684
Brian Paul7e807512005-11-05 17:10:45 +0000685static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000686 struct ureg ureg, GLuint mask )
687{
688 dst->File = ureg.file;
689 dst->Index = ureg.idx;
690 dst->WriteMask = mask;
Brianc9d495c2007-10-23 10:55:24 -0600691 dst->CondMask = COND_TR; /* always pass cond test */
692 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000693}
694
Brian Paul7e807512005-11-05 17:10:45 +0000695static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000696emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000697 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000698 struct ureg dest,
699 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000700 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000701 struct ureg src0,
702 struct ureg src1,
703 struct ureg src2 )
704{
Brian Paul9ed03152009-09-01 15:57:36 -0600705 const GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000706 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Brian2a8e9bb2007-10-23 10:24:53 -0600707
708 assert(nr < MAX_INSTRUCTIONS);
709
Brian Pauld6272e02006-10-29 18:03:16 +0000710 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000711 inst->Opcode = op;
712
Keith Whitwell47b29f52005-05-04 11:44:44 +0000713 emit_arg( &inst->SrcReg[0], src0 );
714 emit_arg( &inst->SrcReg[1], src1 );
715 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000716
Brian Paule31ac052005-11-20 17:52:40 +0000717 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000718
719 emit_dst( &inst->DstReg, dest, mask );
720
Brian Paul5620c202008-09-26 11:18:06 -0600721#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000722 /* Accounting for indirection tracking:
723 */
724 if (dest.file == PROGRAM_TEMPORARY)
725 p->temps_output |= 1 << dest.idx;
Brian Paul5620c202008-09-26 11:18:06 -0600726#endif
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000727
Keith Whitwell15e75e02005-04-29 15:11:39 +0000728 return inst;
729}
730
731
732static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000733 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000734 struct ureg dest,
735 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000736 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000737 struct ureg src0,
738 struct ureg src1,
739 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000740{
741 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
742
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000743 /* Accounting for indirection tracking:
744 */
745 if (src0.file == PROGRAM_TEMPORARY)
746 p->alu_temps |= 1 << src0.idx;
747
748 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
749 p->alu_temps |= 1 << src1.idx;
750
751 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
752 p->alu_temps |= 1 << src2.idx;
753
754 if (dest.file == PROGRAM_TEMPORARY)
755 p->alu_temps |= 1 << dest.idx;
756
Brian21f99792007-01-09 11:00:21 -0700757 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000758 return dest;
759}
760
761static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000762 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000763 struct ureg dest,
764 GLuint destmask,
765 GLuint tex_unit,
766 GLuint tex_idx,
Brian Paul44e018c2009-02-20 13:42:08 -0700767 GLuint tex_shadow,
Keith Whitwellce721142005-07-11 10:10:38 +0000768 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000769{
Brian Paul7e807512005-11-05 17:10:45 +0000770 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000771 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000772 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000773 coord, /* arg 0? */
774 undef,
775 undef);
776
Brian Paul7e807512005-11-05 17:10:45 +0000777 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000778 inst->TexSrcUnit = tex_unit;
Brian Paul44e018c2009-02-20 13:42:08 -0700779 inst->TexShadow = tex_shadow;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000780
Brian21f99792007-01-09 11:00:21 -0700781 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000782
Brian Paul5620c202008-09-26 11:18:06 -0600783 /* Accounting for indirection tracking:
784 */
785 reserve_temp(p, dest);
786
Roland Scheidegger114152e2009-03-12 15:01:16 +0100787#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000788 /* Is this a texture indirection?
789 */
790 if ((coord.file == PROGRAM_TEMPORARY &&
791 (p->temps_output & (1<<coord.idx))) ||
792 (dest.file == PROGRAM_TEMPORARY &&
793 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700794 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000795 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000796 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000797 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000798 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100799#endif
Keith Whitwell15e75e02005-04-29 15:11:39 +0000800
801 return dest;
802}
803
804
Keith Whitwell47b29f52005-05-04 11:44:44 +0000805static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000806 GLfloat s0,
807 GLfloat s1,
808 GLfloat s2,
809 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000810{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000811 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700812 GLuint idx, swizzle;
Brian Pauld01269a2008-09-25 18:27:22 -0600813 struct ureg r;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000814 values[0] = s0;
815 values[1] = s1;
816 values[2] = s2;
817 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700818 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
819 &swizzle );
Brian Pauld01269a2008-09-25 18:27:22 -0600820 r = make_ureg(PROGRAM_CONSTANT, idx);
821 r.swz = swizzle;
822 return r;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000823}
824
Keith Whitwelle4902422005-05-11 15:16:35 +0000825#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000826#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
827#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
828#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000829
830
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000831static struct ureg get_one( struct texenv_fragment_program *p )
832{
833 if (is_undef(p->one))
834 p->one = register_scalar_const(p, 1.0);
835 return p->one;
836}
837
838static struct ureg get_half( struct texenv_fragment_program *p )
839{
840 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000841 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000842 return p->half;
843}
844
845static struct ureg get_zero( struct texenv_fragment_program *p )
846{
847 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000848 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000849 return p->zero;
850}
851
Keith Whitwell15e75e02005-04-29 15:11:39 +0000852
Keith Whitwell15e75e02005-04-29 15:11:39 +0000853static void program_error( struct texenv_fragment_program *p, const char *msg )
854{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000855 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000856 p->error = 1;
857}
858
Keith Whitwell15e75e02005-04-29 15:11:39 +0000859static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000860 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000861{
862 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000863 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000864 assert(!is_undef(p->src_texture[unit]));
865 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000866
Keith Whitwellce721142005-07-11 10:10:38 +0000867 case SRC_TEXTURE0:
868 case SRC_TEXTURE1:
869 case SRC_TEXTURE2:
870 case SRC_TEXTURE3:
871 case SRC_TEXTURE4:
872 case SRC_TEXTURE5:
873 case SRC_TEXTURE6:
874 case SRC_TEXTURE7:
875 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
876 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000877
Keith Whitwellce721142005-07-11 10:10:38 +0000878 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000879 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000880
Keith Whitwellce721142005-07-11 10:10:38 +0000881 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000882 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000883
Brian Paul6947f852009-01-23 17:32:09 -0700884 case SRC_ZERO:
885 return get_zero(p);
886
Keith Whitwellce721142005-07-11 10:10:38 +0000887 case SRC_PREVIOUS:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000888 if (is_undef(p->src_previous))
889 return register_input(p, FRAG_ATTRIB_COL0);
890 else
891 return p->src_previous;
Brian Paul6947f852009-01-23 17:32:09 -0700892
893 default:
894 assert(0);
José Fonsecac6af9b22009-06-15 19:20:25 +0100895 return undef;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000896 }
897}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000898
Keith Whitwell15e75e02005-04-29 15:11:39 +0000899static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000900 GLuint mask,
901 GLuint unit,
902 GLuint source,
903 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000904{
905 struct ureg arg, src, one;
906
907 src = get_source(p, source, unit);
908
909 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000910 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000911 /* Get unused tmp,
912 * Emit tmp = 1.0 - arg.xyzw
913 */
914 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000915 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000916 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000917
Keith Whitwellce721142005-07-11 10:10:38 +0000918 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000919 if (mask == WRITEMASK_W)
920 return src;
921 else
Brian Paul22db5352005-11-19 23:45:10 +0000922 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000923 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000924 /* Get unused tmp,
925 * Emit tmp = 1.0 - arg.wwww
926 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000927 arg = get_temp(p);
928 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000929 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000930 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000931 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000932 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000933 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000934 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000935 case OPR_SRC_COLOR:
Brian Paul6947f852009-01-23 17:32:09 -0700936 return src;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000937 default:
Brian Paul6947f852009-01-23 17:32:09 -0700938 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000939 return src;
940 }
941}
942
Brian Paulf337e2c2009-09-01 15:49:55 -0600943/**
944 * Check if the RGB and Alpha sources and operands match for the given
945 * texture unit's combinder state. When the RGB and A sources and
946 * operands match, we can emit fewer instructions.
947 */
Brian Paula5e70032009-08-31 11:17:59 -0600948static GLboolean args_match( const struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000949{
Brian Paul9ed03152009-09-01 15:57:36 -0600950 GLuint i, numArgs = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000951
Brian Paul9ed03152009-09-01 15:57:36 -0600952 for (i = 0 ; i < numArgs ; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000953 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000954 return GL_FALSE;
955
Brian Paul9ed03152009-09-01 15:57:36 -0600956 switch (key->unit[unit].OptA[i].Operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000957 case OPR_SRC_ALPHA:
Brian Paul9ed03152009-09-01 15:57:36 -0600958 switch (key->unit[unit].OptRGB[i].Operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000959 case OPR_SRC_COLOR:
960 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000961 break;
962 default:
963 return GL_FALSE;
964 }
965 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000966 case OPR_ONE_MINUS_SRC_ALPHA:
Brian Paul9ed03152009-09-01 15:57:36 -0600967 switch (key->unit[unit].OptRGB[i].Operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000968 case OPR_ONE_MINUS_SRC_COLOR:
969 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000970 break;
971 default:
972 return GL_FALSE;
973 }
974 break;
975 default:
976 return GL_FALSE; /* impossible */
977 }
978 }
979
980 return GL_TRUE;
981}
982
Keith Whitwell15e75e02005-04-29 15:11:39 +0000983static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000984 struct ureg dest,
985 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000986 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000987 GLuint unit,
988 GLuint nr,
989 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +0000990 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000991{
Brian Paulbd9b2be2009-03-08 17:58:54 -0600992 struct ureg src[MAX_COMBINER_TERMS];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000993 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +0000994 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000995
Brian Paulbd9b2be2009-03-08 17:58:54 -0600996 assert(nr <= MAX_COMBINER_TERMS);
Brian Paul6947f852009-01-23 17:32:09 -0700997
Brian Paul00630842005-12-12 15:27:55 +0000998 tmp = undef; /* silence warning (bug 5318) */
999
Keith Whitwell15e75e02005-04-29 15:11:39 +00001000 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +00001001 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001002
1003 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +00001004 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001005 if (mask == WRITEMASK_XYZW && !saturate)
1006 return src[0];
1007 else
Brian Paul7e807512005-11-05 17:10:45 +00001008 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001009 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +00001010 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001011 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001012 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00001013 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001014 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001015 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001016 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001017 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +00001018 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001019 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +00001020 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +00001021 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
1022 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001023 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +00001024 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001025 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
1026 */
Brian Paul7e807512005-11-05 17:10:45 +00001027 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001028
Keith Whitwellce721142005-07-11 10:10:38 +00001029 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +00001030 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001031
Keith Whitwellce721142005-07-11 10:10:38 +00001032 case MODE_DOT3_RGBA:
1033 case MODE_DOT3_RGBA_EXT:
1034 case MODE_DOT3_RGB_EXT:
1035 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001036 struct ureg tmp0 = get_temp( p );
1037 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +00001038 struct ureg neg1 = register_scalar_const(p, -1);
1039 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001040
1041 /* tmp0 = 2*src0 - 1
1042 * tmp1 = 2*src1 - 1
1043 *
1044 * dst = tmp0 dot3 tmp1
1045 */
Brian Paul7e807512005-11-05 17:10:45 +00001046 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001047 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001048
Brian Paulaa206952005-09-16 18:14:24 +00001049 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001050 tmp1 = tmp0;
1051 else
Brian Paul7e807512005-11-05 17:10:45 +00001052 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001053 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +00001054 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001055 return dest;
1056 }
Keith Whitwellce721142005-07-11 10:10:38 +00001057 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001058 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001059 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001060 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +00001061 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001062 /* Arg0 * Arg2 + Arg1 - 0.5 */
1063 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001064 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +00001065 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
1066 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001067 return dest;
1068 }
Keith Whitwellce721142005-07-11 10:10:38 +00001069 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001070 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001071 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001072 return dest;
Brian Paul6947f852009-01-23 17:32:09 -07001073 case MODE_ADD_PRODUCTS:
1074 /* Arg0 * Arg1 + Arg2 * Arg3 */
1075 {
1076 struct ureg tmp0 = get_temp(p);
1077 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1078 emit_arith( p, OPCODE_MAD, dest, mask, saturate, src[2], src[3], tmp0 );
1079 }
1080 return dest;
1081 case MODE_ADD_PRODUCTS_SIGNED:
1082 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1083 {
1084 struct ureg tmp0 = get_temp(p);
1085 half = get_half(p);
1086 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1087 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[2], src[3], tmp0 );
1088 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1089 }
1090 return dest;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001091 case MODE_BUMP_ENVMAP_ATI:
1092 /* special - not handled here */
1093 assert(0);
1094 return src[0];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001095 default:
Brian Paul6947f852009-01-23 17:32:09 -07001096 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001097 return src[0];
1098 }
1099}
1100
Keith Whitwell15e75e02005-04-29 15:11:39 +00001101
Brian Paul22db5352005-11-19 23:45:10 +00001102/**
1103 * Generate instructions for one texture unit's env/combiner mode.
1104 */
1105static struct ureg
1106emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001107{
Brian Paula5e70032009-08-31 11:17:59 -06001108 const struct state_key *key = p->state;
Brian Paul956e6c32009-08-31 11:14:16 -06001109 GLboolean saturate;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001110 GLuint rgb_shift, alpha_shift;
Brian Paula5e70032009-08-31 11:17:59 -06001111 struct ureg out, dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001112
Keith Whitwellce721142005-07-11 10:10:38 +00001113 if (!key->unit[unit].enabled) {
1114 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001115 }
Roland Scheidegger114152e2009-03-12 15:01:16 +01001116 if (key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1117 /* this isn't really a env stage delivering a color and handled elsewhere */
1118 return get_source(p, SRC_PREVIOUS, 0);
1119 }
Keith Whitwellce721142005-07-11 10:10:38 +00001120
1121 switch (key->unit[unit].ModeRGB) {
1122 case MODE_DOT3_RGB_EXT:
1123 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001124 rgb_shift = 0;
1125 break;
Keith Whitwellce721142005-07-11 10:10:38 +00001126 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001127 alpha_shift = 0;
1128 rgb_shift = 0;
1129 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001130 default:
Keith Whitwellce721142005-07-11 10:10:38 +00001131 rgb_shift = key->unit[unit].ScaleShiftRGB;
1132 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001133 break;
1134 }
Keith Whitwellce721142005-07-11 10:10:38 +00001135
Brian Paul956e6c32009-08-31 11:14:16 -06001136 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
1137 * We don't want to clamp twice.
1138 */
1139 saturate = !(rgb_shift || alpha_shift);
1140
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001141 /* If this is the very last calculation, emit direct to output reg:
1142 */
Keith Whitwellce721142005-07-11 10:10:38 +00001143 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001144 unit != p->last_tex_stage ||
1145 alpha_shift ||
1146 rgb_shift)
1147 dest = get_temp( p );
1148 else
Brian Paul8d475822009-02-28 11:49:46 -07001149 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLOR);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001150
1151 /* Emit the RGB and A combine ops
1152 */
Keith Whitwellce721142005-07-11 10:10:38 +00001153 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
1154 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001155 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1156 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001157 key->unit[unit].NumArgsRGB,
1158 key->unit[unit].ModeRGB,
1159 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001160 }
Keith Whitwellce721142005-07-11 10:10:38 +00001161 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +02001162 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001163 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1164 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001165 key->unit[unit].NumArgsRGB,
1166 key->unit[unit].ModeRGB,
1167 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001168 }
1169 else {
1170 /* Need to do something to stop from re-emitting identical
1171 * argument calculations here:
1172 */
1173 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
1174 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001175 key->unit[unit].NumArgsRGB,
1176 key->unit[unit].ModeRGB,
1177 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001178 out = emit_combine( p, dest, WRITEMASK_W, saturate,
1179 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001180 key->unit[unit].NumArgsA,
1181 key->unit[unit].ModeA,
1182 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001183 }
1184
1185 /* Deal with the final shift:
1186 */
1187 if (alpha_shift || rgb_shift) {
Brian Paula5e70032009-08-31 11:17:59 -06001188 struct ureg shift;
1189
Brian Paul956e6c32009-08-31 11:14:16 -06001190 saturate = GL_TRUE; /* always saturate at this point */
Brian Paula5e70032009-08-31 11:17:59 -06001191
Keith Whitwell15e75e02005-04-29 15:11:39 +00001192 if (rgb_shift == alpha_shift) {
José Fonseca452a5922008-05-31 18:14:09 +09001193 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001194 }
1195 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001196 shift = register_const4f(p,
José Fonseca452a5922008-05-31 18:14:09 +09001197 (GLfloat)(1<<rgb_shift),
1198 (GLfloat)(1<<rgb_shift),
1199 (GLfloat)(1<<rgb_shift),
1200 (GLfloat)(1<<alpha_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001201 }
Brian Paul7e807512005-11-05 17:10:45 +00001202 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001203 saturate, out, shift, undef );
1204 }
1205 else
1206 return out;
1207}
1208
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001209
Brian Paul22db5352005-11-19 23:45:10 +00001210/**
1211 * Generate instruction for getting a texture source term.
1212 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001213static void load_texture( struct texenv_fragment_program *p, GLuint unit )
1214{
1215 if (is_undef(p->src_texture[unit])) {
Brian Paulb5ec0a62009-09-01 15:51:23 -06001216 const GLuint texTarget = p->state->unit[unit].source_index;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001217 struct ureg texcoord;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001218 struct ureg tmp = get_tex_temp( p );
1219
Roland Scheidegger114152e2009-03-12 15:01:16 +01001220 if (is_undef(p->texcoord_tex[unit])) {
1221 texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
1222 }
1223 else {
1224 /* might want to reuse this reg for tex output actually */
1225 texcoord = p->texcoord_tex[unit];
1226 }
1227
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001228 /* TODO: Use D0_MASK_XY where possible.
1229 */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +02001230 if (p->state->unit[unit].enabled) {
Brian Paul44e018c2009-02-20 13:42:08 -07001231 GLboolean shadow = GL_FALSE;
1232
1233 if (p->state->unit[unit].shadow) {
1234 p->program->Base.ShadowSamplers |= 1 << unit;
1235 shadow = GL_TRUE;
1236 }
1237
Aapo Tahkolab8915342006-03-28 10:26:34 +00001238 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1239 tmp, WRITEMASK_XYZW,
Brian Paul44e018c2009-02-20 13:42:08 -07001240 unit, texTarget, shadow,
1241 texcoord );
Brian9b7e5a52007-12-14 11:16:49 -07001242
1243 p->program->Base.SamplersUsed |= (1 << unit);
Brian1fe385f2007-12-14 11:42:28 -07001244 /* This identity mapping should already be in place
1245 * (see _mesa_init_program_struct()) but let's be safe.
1246 */
1247 p->program->Base.SamplerUnits[unit] = unit;
Brian9b7e5a52007-12-14 11:16:49 -07001248 }
1249 else
Aapo Tahkolab8915342006-03-28 10:26:34 +00001250 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001251 }
1252}
1253
Keith Whitwell241b6b72005-05-23 09:50:34 +00001254static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +00001255 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001256{
1257 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +00001258 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001259 load_texture(p, unit);
1260 break;
1261
Keith Whitwellce721142005-07-11 10:10:38 +00001262 case SRC_TEXTURE0:
1263 case SRC_TEXTURE1:
1264 case SRC_TEXTURE2:
1265 case SRC_TEXTURE3:
1266 case SRC_TEXTURE4:
1267 case SRC_TEXTURE5:
1268 case SRC_TEXTURE6:
1269 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +00001270 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001271 break;
1272
1273 default:
Brian Paul6947f852009-01-23 17:32:09 -07001274 /* not a texture src - do nothing */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001275 break;
1276 }
Keith Whitwell241b6b72005-05-23 09:50:34 +00001277
1278 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001279}
1280
Brian Paul22db5352005-11-19 23:45:10 +00001281
1282/**
1283 * Generate instructions for loading all texture source terms.
1284 */
1285static GLboolean
1286load_texunit_sources( struct texenv_fragment_program *p, int unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001287{
Brian Paula5e70032009-08-31 11:17:59 -06001288 const struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +00001289 GLuint i;
1290
1291 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001292 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001293 }
Aapo Tahkolab8915342006-03-28 10:26:34 +00001294
1295 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1296 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1297 }
1298
Keith Whitwell241b6b72005-05-23 09:50:34 +00001299 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001300}
1301
Roland Scheidegger114152e2009-03-12 15:01:16 +01001302/**
1303 * Generate instructions for loading bump map textures.
1304 */
1305static GLboolean
1306load_texunit_bumpmap( struct texenv_fragment_program *p, int unit )
1307{
Brian Paula5e70032009-08-31 11:17:59 -06001308 const struct state_key *key = p->state;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001309 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1310 struct ureg texcDst, bumpMapRes;
1311 struct ureg constdudvcolor = register_const4f(p, 0.0, 0.0, 0.0, 1.0);
1312 struct ureg texcSrc = register_input(p, FRAG_ATTRIB_TEX0 + bumpedUnitNr);
1313 struct ureg rotMat0 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_0, unit );
1314 struct ureg rotMat1 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_1, unit );
1315
1316 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1317
1318 bumpMapRes = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1319 texcDst = get_tex_temp( p );
1320 p->texcoord_tex[bumpedUnitNr] = texcDst;
1321
1322 /* apply rot matrix and add coords to be available in next phase */
1323 /* dest = (Arg0.xxxx * rotMat0 + Arg1) + (Arg0.yyyy * rotMat1) */
1324 /* note only 2 coords are affected the rest are left unchanged (mul by 0) */
1325 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1326 swizzle1(bumpMapRes, SWIZZLE_X), rotMat0, texcSrc );
1327 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1328 swizzle1(bumpMapRes, SWIZZLE_Y), rotMat1, texcDst );
1329
1330 /* move 0,0,0,1 into bumpmap src if someone (crossbar) is foolish
1331 enough to access this later, should optimize away */
1332 emit_arith( p, OPCODE_MOV, bumpMapRes, WRITEMASK_XYZW, 0, constdudvcolor, undef, undef );
1333
1334 return GL_TRUE;
1335}
Brian Paul22db5352005-11-19 23:45:10 +00001336
1337/**
1338 * Generate a new fragment program which implements the context's
1339 * current texture env/combine mode.
1340 */
1341static void
Brian Paule998c342006-10-29 21:17:18 +00001342create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001343 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001344{
Brian Paule998c342006-10-29 21:17:18 +00001345 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001346 struct texenv_fragment_program p;
1347 GLuint unit;
1348 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001349
Keith Whitwelle4902422005-05-11 15:16:35 +00001350 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +00001351 p.ctx = ctx;
Keith Whitwellce721142005-07-11 10:10:38 +00001352 p.state = key;
1353 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001354
Brian Paule998c342006-10-29 21:17:18 +00001355 /* During code generation, use locally-allocated instruction buffer,
1356 * then alloc dynamic storage below.
1357 */
1358 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001359 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001360 p.program->Base.NumTexIndirections = 1;
Brian21f99792007-01-09 11:00:21 -07001361 p.program->Base.NumTexInstructions = 0;
1362 p.program->Base.NumAluInstructions = 0;
Brian1240eb22007-03-22 08:50:20 -06001363 p.program->Base.String = NULL;
Keith Whitwell9ca88152005-05-10 09:56:02 +00001364 p.program->Base.NumInstructions =
Brian Paule998c342006-10-29 21:17:18 +00001365 p.program->Base.NumTemporaries =
1366 p.program->Base.NumParameters =
1367 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001368 p.program->Base.Parameters = _mesa_new_parameter_list();
Keith Whitwelldbeea252005-05-10 13:57:50 +00001369
Brian Paulde997602005-11-12 17:53:14 +00001370 p.program->Base.InputsRead = 0;
Brian Paul8d475822009-02-28 11:49:46 -07001371 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLOR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001372
Roland Scheidegger114152e2009-03-12 15:01:16 +01001373 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001374 p.src_texture[unit] = undef;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001375 p.texcoord_tex[unit] = undef;
1376 }
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001377
Keith Whitwell47b29f52005-05-04 11:44:44 +00001378 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001379 p.half = undef;
1380 p.zero = undef;
1381 p.one = undef;
1382
Keith Whitwell15e75e02005-04-29 15:11:39 +00001383 p.last_tex_stage = 0;
Brian93fef222007-10-29 12:25:46 -06001384 release_temps(ctx, &p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001385
Keith Whitwellce721142005-07-11 10:10:38 +00001386 if (key->enabled_units) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001387 GLboolean needbumpstage = GL_FALSE;
1388 /* Zeroth pass - bump map textures first */
1389 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
1390 if (key->unit[unit].enabled && key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1391 needbumpstage = GL_TRUE;
1392 load_texunit_bumpmap( &p, unit );
1393 }
1394 if (needbumpstage)
1395 p.program->Base.NumTexIndirections++;
1396
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001397 /* First pass - to support texture_env_crossbar, first identify
1398 * all referenced texture sources and emit texld instructions
1399 * for each:
1400 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001401 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001402 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001403 load_texunit_sources( &p, unit );
1404 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001405 }
1406
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001407 /* Second pass - emit combine instructions to build final color:
1408 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001409 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001410 if (key->enabled_units & (1<<unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001411 p.src_previous = emit_texenv( &p, unit );
Brian Paul5620c202008-09-26 11:18:06 -06001412 reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
Brian93fef222007-10-29 12:25:46 -06001413 release_temps(ctx, &p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001414 }
1415 }
1416
Keith Whitwellce721142005-07-11 10:10:38 +00001417 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul8d475822009-02-28 11:49:46 -07001418 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLOR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001419
Keith Whitwellce721142005-07-11 10:10:38 +00001420 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001421 /* Emit specular add.
1422 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001423 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001424 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1425 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001426 }
Brian Paulaa206952005-09-16 18:14:24 +00001427 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001428 /* Will wind up in here if no texture enabled or a couple of
1429 * other scenarios (GL_REPLACE for instance).
1430 */
Brian Paul7e807512005-11-05 17:10:45 +00001431 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001432 }
1433
Keith Whitwell47b29f52005-05-04 11:44:44 +00001434 /* Finish up:
1435 */
Brian Paul7e807512005-11-05 17:10:45 +00001436 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001437
Keith Whitwellce721142005-07-11 10:10:38 +00001438 if (key->fog_enabled) {
1439 /* Pull fog mode from GLcontext, the value in the state key is
1440 * a reduced value and not what is expected in FogOption
1441 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001442 p.program->FogOption = ctx->Fog.Mode;
Brian19d77d62007-09-18 19:29:26 -06001443 p.program->Base.InputsRead |= FRAG_BIT_FOGC; /* XXX new */
Keith Whitwellce721142005-07-11 10:10:38 +00001444 } else
Keith Whitwell276330b2005-05-09 17:58:13 +00001445 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001446
Brian21f99792007-01-09 11:00:21 -07001447 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001448 program_error(&p, "Exceeded max nr indirect texture lookups");
1449
Brian21f99792007-01-09 11:00:21 -07001450 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001451 program_error(&p, "Exceeded max TEX instructions");
1452
Brian21f99792007-01-09 11:00:21 -07001453 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001454 program_error(&p, "Exceeded max ALU instructions");
1455
Brian Paul5d7b49f2005-11-19 23:12:20 +00001456 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001457
Brian Paule998c342006-10-29 21:17:18 +00001458 /* Allocate final instruction array */
Brianc81cce72007-09-25 15:18:51 -06001459 p.program->Base.Instructions
1460 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1461 if (!p.program->Base.Instructions) {
Brian Paule998c342006-10-29 21:17:18 +00001462 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1463 "generating tex env program");
1464 return;
1465 }
Brianc81cce72007-09-25 15:18:51 -06001466 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1467 p.program->Base.NumInstructions);
1468
1469 if (p.program->FogOption) {
1470 _mesa_append_fog_code(ctx, p.program);
1471 p.program->FogOption = GL_NONE;
1472 }
1473
Brian Paule998c342006-10-29 21:17:18 +00001474
Keith Whitwelldbeea252005-05-10 13:57:50 +00001475 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001476 */
Brian Paule998c342006-10-29 21:17:18 +00001477 if (ctx->Driver.ProgramStringNotify) {
1478 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1479 &p.program->Base );
1480 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001481
Brian Paule998c342006-10-29 21:17:18 +00001482 if (DISASSEM) {
1483 _mesa_print_program(&p.program->Base);
1484 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001485 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001486}
1487
Brian Paul5d7b49f2005-11-19 23:12:20 +00001488
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001489/**
1490 * Return a fragment program which implements the current
1491 * fixed-function texture, fog and color-sum operations.
1492 */
1493struct gl_fragment_program *
1494_mesa_get_fixed_func_fragment_program(GLcontext *ctx)
Keith Whitwellce721142005-07-11 10:10:38 +00001495{
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001496 struct gl_fragment_program *prog;
1497 struct state_key key;
1498
1499 make_state_key(ctx, &key);
1500
1501 prog = (struct gl_fragment_program *)
1502 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1503 &key, sizeof(key));
Keith Whitwellce721142005-07-11 10:10:38 +00001504
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001505 if (!prog) {
1506 prog = (struct gl_fragment_program *)
1507 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1508
1509 create_new_program(ctx, &key, prog);
1510
1511 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1512 &key, sizeof(key), &prog->Base);
Keith Whitwellce721142005-07-11 10:10:38 +00001513 }
1514
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001515 return prog;
Keith Whitwellce721142005-07-11 10:10:38 +00001516}