blob: b16b8dd36b808ac49475598fc93e333b3a1d18bf [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"
Brian Paul25e5a6f2009-09-01 16:05:07 -060030#include "imports.h"
Brian Paul5b5c9312008-05-07 18:51:44 -060031#include "shader/program.h"
Brianc223c6b2007-07-04 13:15:20 -060032#include "shader/prog_parameter.h"
Keith Whitwell32ef6e72008-09-20 08:26:11 -070033#include "shader/prog_cache.h"
Brianc223c6b2007-07-04 13:15:20 -060034#include "shader/prog_instruction.h"
35#include "shader/prog_print.h"
36#include "shader/prog_statevars.h"
Brianfb3c41f2007-09-25 15:20:04 -060037#include "shader/programopt.h"
Keith Whitwell15e75e02005-04-29 15:11:39 +000038#include "texenvprogram.h"
39
Brian Paul5b5c9312008-05-07 18:51:44 -060040
Brian Paule9b34882008-12-31 11:54:02 -070041/*
42 * Note on texture units:
43 *
44 * The number of texture units supported by fixed-function fragment
45 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
46 * That's because there's a one-to-one correspondence between texture
47 * coordinates and samplers in fixed-function processing.
48 *
49 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
50 * sets of texcoords, so is fixed-function fragment processing.
51 *
52 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
53 */
54
55
Brian Paul5b5c9312008-05-07 18:51:44 -060056struct texenvprog_cache_item
57{
58 GLuint hash;
59 void *key;
60 struct gl_fragment_program *data;
61 struct texenvprog_cache_item *next;
62};
63
Eric Anholt9cea84b2009-07-16 18:41:03 -070064static GLboolean
65texenv_doing_secondary_color(GLcontext *ctx)
66{
67 if (ctx->Light.Enabled &&
68 (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR))
69 return GL_TRUE;
70
71 if (ctx->Fog.ColorSumEnabled)
72 return GL_TRUE;
73
74 return GL_FALSE;
75}
Brian Paul5b5c9312008-05-07 18:51:44 -060076
Brian Paule998c342006-10-29 21:17:18 +000077/**
Brian Paulb5e1a932008-09-25 18:40:16 -060078 * Up to nine instructions per tex unit, plus fog, specular color.
Brian Paule998c342006-10-29 21:17:18 +000079 */
Brian Paul0815ebc2009-01-02 16:32:26 -070080#define MAX_INSTRUCTIONS ((MAX_TEXTURE_COORD_UNITS * 9) + 12)
Keith Whitwell15e75e02005-04-29 15:11:39 +000081
Keith Whitwell269e3892005-05-12 10:28:43 +000082#define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
Keith Whitwell15e75e02005-04-29 15:11:39 +000083
Keith Whitwellce721142005-07-11 10:10:38 +000084struct mode_opt {
Chris Wilsona46e3272009-09-02 05:11:25 -070085 GLubyte Source:4; /**< SRC_x */
86 GLubyte Operand:3; /**< OPR_x */
Keith Whitwellce721142005-07-11 10:10:38 +000087};
88
89struct state_key {
Keith Whitwell6280e332008-10-03 13:51:56 +010090 GLuint nr_enabled_units:8;
91 GLuint enabled_units:8;
Brian Paul5d7b49f2005-11-19 23:12:20 +000092 GLuint separate_specular:1;
93 GLuint fog_enabled:1;
Brian Paul9ed03152009-09-01 15:57:36 -060094 GLuint fog_mode:2; /**< FOG_x */
Keith Whitwell6280e332008-10-03 13:51:56 +010095 GLuint inputs_available:12;
Keith Whitwellce721142005-07-11 10:10:38 +000096
97 struct {
Brian Paul5d7b49f2005-11-19 23:12:20 +000098 GLuint enabled:1;
Brian Paul9ed03152009-09-01 15:57:36 -060099 GLuint source_index:3; /**< TEXTURE_x_INDEX */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +0200100 GLuint shadow:1;
Brian Paul5d7b49f2005-11-19 23:12:20 +0000101 GLuint ScaleShiftRGB:2;
102 GLuint ScaleShiftA:2;
Keith Whitwellce721142005-07-11 10:10:38 +0000103
Brian Paul9ed03152009-09-01 15:57:36 -0600104 GLuint NumArgsRGB:3; /**< up to MAX_COMBINER_TERMS */
Brian Paulf337e2c2009-09-01 15:49:55 -0600105 GLuint ModeRGB:5; /**< MODE_x */
Keith Whitwellce721142005-07-11 10:10:38 +0000106
Brian Paul9ed03152009-09-01 15:57:36 -0600107 GLuint NumArgsA:3; /**< up to MAX_COMBINER_TERMS */
Brian Paulf337e2c2009-09-01 15:49:55 -0600108 GLuint ModeA:5; /**< MODE_x */
Chris Wilsona46e3272009-09-02 05:11:25 -0700109
110 struct mode_opt OptRGB[MAX_COMBINER_TERMS];
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;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000460 GLuint swz:12;
Brian Paul1480bca2009-09-01 16:01:12 -0600461 GLuint pad:7;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000462};
463
Brian Paul13abf912006-04-13 19:17:13 +0000464static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000465 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000466 ~0,
467 0,
468 0,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000469 0
470};
471
Keith Whitwell15e75e02005-04-29 15:11:39 +0000472
Brian Paul239617f2008-10-07 11:22:47 -0600473/** State used to build the fragment program:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000474 */
475struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000476 struct gl_fragment_program *program;
Keith Whitwellce721142005-07-11 10:10:38 +0000477 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000478
Brian Paul239617f2008-10-07 11:22:47 -0600479 GLbitfield alu_temps; /**< Track texture indirections, see spec. */
480 GLbitfield temps_output; /**< Track texture indirections, see spec. */
481 GLbitfield temp_in_use; /**< Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000482 GLboolean error;
483
Brian Paule9b34882008-12-31 11:54:02 -0700484 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000485 /* Reg containing each texture unit's sampled texture color,
486 * else undef.
487 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000488
Roland Scheidegger114152e2009-03-12 15:01:16 +0100489 struct ureg texcoord_tex[MAX_TEXTURE_COORD_UNITS];
490 /* Reg containing texcoord for a texture unit,
491 * needed for bump mapping, else undef.
492 */
493
Brian Paul239617f2008-10-07 11:22:47 -0600494 struct ureg src_previous; /**< Reg containing color from previous
Keith Whitwell15e75e02005-04-29 15:11:39 +0000495 * stage. May need to be decl'd.
496 */
497
Brian Paul239617f2008-10-07 11:22:47 -0600498 GLuint last_tex_stage; /**< Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000499
500 struct ureg half;
501 struct ureg one;
502 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000503};
504
505
506
507static struct ureg make_ureg(GLuint file, GLuint idx)
508{
509 struct ureg reg;
510 reg.file = file;
511 reg.idx = idx;
512 reg.negatebase = 0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000513 reg.swz = SWIZZLE_NOOP;
514 reg.pad = 0;
515 return reg;
516}
517
518static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
519{
520 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
521 GET_SWZ(reg.swz, y),
522 GET_SWZ(reg.swz, z),
523 GET_SWZ(reg.swz, w));
524
525 return reg;
526}
527
528static struct ureg swizzle1( struct ureg reg, int x )
529{
530 return swizzle(reg, x, x, x, x);
531}
532
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000533static struct ureg negate( struct ureg reg )
534{
535 reg.negatebase ^= 1;
536 return reg;
537}
538
Keith Whitwell15e75e02005-04-29 15:11:39 +0000539static GLboolean is_undef( struct ureg reg )
540{
Alan Hourihane8d972652006-08-10 13:12:00 +0000541 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000542}
543
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000544
Keith Whitwell15e75e02005-04-29 15:11:39 +0000545static struct ureg get_temp( struct texenv_fragment_program *p )
546{
Brian Paul13abf912006-04-13 19:17:13 +0000547 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000548
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000549 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000550 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000551 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000552
553 /* Then any unused temporary:
554 */
555 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000556 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000557
Keith Whitwell15e75e02005-04-29 15:11:39 +0000558 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000559 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000560 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000561 }
562
Brian Paul13abf912006-04-13 19:17:13 +0000563 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000564 p->program->Base.NumTemporaries = bit;
565
Keith Whitwell93cd9232005-05-11 08:30:23 +0000566 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000567 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
568}
569
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000570static struct ureg get_tex_temp( struct texenv_fragment_program *p )
571{
572 int bit;
573
Brian Pauld01269a2008-09-25 18:27:22 -0600574 /* First try to find available temp not previously used (to avoid
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000575 * starting a new texture indirection). According to the spec, the
576 * ~p->temps_output isn't necessary, but will keep it there for
577 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000578 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000579 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000580
581 /* Then any unused temporary:
582 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000583 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000584 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000585
586 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000587 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000588 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000589 }
590
Brian Paul13abf912006-04-13 19:17:13 +0000591 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000592 p->program->Base.NumTemporaries = bit;
593
Keith Whitwell93cd9232005-05-11 08:30:23 +0000594 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000595 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
596}
597
Keith Whitwell15e75e02005-04-29 15:11:39 +0000598
Brian Paul5620c202008-09-26 11:18:06 -0600599/** Mark a temp reg as being no longer allocatable. */
600static void reserve_temp( struct texenv_fragment_program *p, struct ureg r )
601{
602 if (r.file == PROGRAM_TEMPORARY)
603 p->temps_output |= (1 << r.idx);
604}
605
606
Brian93fef222007-10-29 12:25:46 -0600607static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000608{
Brian93fef222007-10-29 12:25:46 -0600609 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000610
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000611 /* KW: To support tex_env_crossbar, don't release the registers in
612 * temps_output.
613 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000614 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000615 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000616 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000617 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000618}
619
620
Brian9fe3e2e2007-02-23 11:44:14 -0700621static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000622 GLint s0,
623 GLint s1,
624 GLint s2,
625 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700626 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000627{
Brian9fe3e2e2007-02-23 11:44:14 -0700628 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000629 GLuint idx;
630 tokens[0] = s0;
631 tokens[1] = s1;
632 tokens[2] = s2;
633 tokens[3] = s3;
634 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000635 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000636 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000637}
638
Keith Whitwell47b29f52005-05-04 11:44:44 +0000639
Brian9fe3e2e2007-02-23 11:44:14 -0700640#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
641#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
642#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
643#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000644
Keith Whitwell1680ef82008-10-03 17:30:59 +0100645static GLuint frag_to_vert_attrib( GLuint attrib )
646{
647 switch (attrib) {
648 case FRAG_ATTRIB_COL0: return VERT_ATTRIB_COLOR0;
649 case FRAG_ATTRIB_COL1: return VERT_ATTRIB_COLOR1;
650 default:
651 assert(attrib >= FRAG_ATTRIB_TEX0);
652 assert(attrib <= FRAG_ATTRIB_TEX7);
653 return attrib - FRAG_ATTRIB_TEX0 + VERT_ATTRIB_TEX0;
654 }
655}
656
Keith Whitwell47b29f52005-05-04 11:44:44 +0000657
658static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
659{
Keith Whitwell1680ef82008-10-03 17:30:59 +0100660 if (p->state->inputs_available & (1<<input)) {
661 p->program->Base.InputsRead |= (1 << input);
662 return make_ureg(PROGRAM_INPUT, input);
663 }
664 else {
665 GLuint idx = frag_to_vert_attrib( input );
666 return register_param3( p, STATE_INTERNAL, STATE_CURRENT_ATTRIB, idx );
667 }
Keith Whitwell47b29f52005-05-04 11:44:44 +0000668}
669
670
Brian Paul7e807512005-11-05 17:10:45 +0000671static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000672 struct ureg ureg )
673{
674 reg->File = ureg.file;
675 reg->Index = ureg.idx;
676 reg->Swizzle = ureg.swz;
Brian Paul7db7ff82009-04-14 22:14:30 -0600677 reg->Negate = ureg.negatebase ? NEGATE_XYZW : NEGATE_NONE;
Brian Paul1480bca2009-09-01 16:01:12 -0600678 reg->Abs = GL_FALSE;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000679}
680
Brian Paul7e807512005-11-05 17:10:45 +0000681static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000682 struct ureg ureg, GLuint mask )
683{
684 dst->File = ureg.file;
685 dst->Index = ureg.idx;
686 dst->WriteMask = mask;
Brianc9d495c2007-10-23 10:55:24 -0600687 dst->CondMask = COND_TR; /* always pass cond test */
688 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000689}
690
Brian Paul7e807512005-11-05 17:10:45 +0000691static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000692emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000693 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000694 struct ureg dest,
695 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000696 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000697 struct ureg src0,
698 struct ureg src1,
699 struct ureg src2 )
700{
Brian Paul9ed03152009-09-01 15:57:36 -0600701 const GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000702 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Brian2a8e9bb2007-10-23 10:24:53 -0600703
704 assert(nr < MAX_INSTRUCTIONS);
705
Brian Pauld6272e02006-10-29 18:03:16 +0000706 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000707 inst->Opcode = op;
708
Keith Whitwell47b29f52005-05-04 11:44:44 +0000709 emit_arg( &inst->SrcReg[0], src0 );
710 emit_arg( &inst->SrcReg[1], src1 );
711 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000712
Brian Paule31ac052005-11-20 17:52:40 +0000713 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000714
715 emit_dst( &inst->DstReg, dest, mask );
716
Brian Paul5620c202008-09-26 11:18:06 -0600717#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000718 /* Accounting for indirection tracking:
719 */
720 if (dest.file == PROGRAM_TEMPORARY)
721 p->temps_output |= 1 << dest.idx;
Brian Paul5620c202008-09-26 11:18:06 -0600722#endif
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000723
Keith Whitwell15e75e02005-04-29 15:11:39 +0000724 return inst;
725}
726
727
728static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000729 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000730 struct ureg dest,
731 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000732 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000733 struct ureg src0,
734 struct ureg src1,
735 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000736{
737 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
738
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000739 /* Accounting for indirection tracking:
740 */
741 if (src0.file == PROGRAM_TEMPORARY)
742 p->alu_temps |= 1 << src0.idx;
743
744 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
745 p->alu_temps |= 1 << src1.idx;
746
747 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
748 p->alu_temps |= 1 << src2.idx;
749
750 if (dest.file == PROGRAM_TEMPORARY)
751 p->alu_temps |= 1 << dest.idx;
752
Brian21f99792007-01-09 11:00:21 -0700753 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000754 return dest;
755}
756
757static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000758 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000759 struct ureg dest,
760 GLuint destmask,
761 GLuint tex_unit,
762 GLuint tex_idx,
Brian Paul44e018c2009-02-20 13:42:08 -0700763 GLuint tex_shadow,
Keith Whitwellce721142005-07-11 10:10:38 +0000764 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000765{
Brian Paul7e807512005-11-05 17:10:45 +0000766 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000767 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000768 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000769 coord, /* arg 0? */
770 undef,
771 undef);
772
Brian Paul7e807512005-11-05 17:10:45 +0000773 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000774 inst->TexSrcUnit = tex_unit;
Brian Paul44e018c2009-02-20 13:42:08 -0700775 inst->TexShadow = tex_shadow;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000776
Brian21f99792007-01-09 11:00:21 -0700777 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000778
Brian Paul5620c202008-09-26 11:18:06 -0600779 /* Accounting for indirection tracking:
780 */
781 reserve_temp(p, dest);
782
Roland Scheidegger114152e2009-03-12 15:01:16 +0100783#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000784 /* Is this a texture indirection?
785 */
786 if ((coord.file == PROGRAM_TEMPORARY &&
787 (p->temps_output & (1<<coord.idx))) ||
788 (dest.file == PROGRAM_TEMPORARY &&
789 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700790 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000791 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000792 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000793 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000794 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100795#endif
Keith Whitwell15e75e02005-04-29 15:11:39 +0000796
797 return dest;
798}
799
800
Keith Whitwell47b29f52005-05-04 11:44:44 +0000801static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000802 GLfloat s0,
803 GLfloat s1,
804 GLfloat s2,
805 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000806{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000807 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700808 GLuint idx, swizzle;
Brian Pauld01269a2008-09-25 18:27:22 -0600809 struct ureg r;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000810 values[0] = s0;
811 values[1] = s1;
812 values[2] = s2;
813 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700814 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
815 &swizzle );
Brian Pauld01269a2008-09-25 18:27:22 -0600816 r = make_ureg(PROGRAM_CONSTANT, idx);
817 r.swz = swizzle;
818 return r;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000819}
820
Keith Whitwelle4902422005-05-11 15:16:35 +0000821#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000822#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
823#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
824#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000825
826
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000827static struct ureg get_one( struct texenv_fragment_program *p )
828{
829 if (is_undef(p->one))
830 p->one = register_scalar_const(p, 1.0);
831 return p->one;
832}
833
834static struct ureg get_half( struct texenv_fragment_program *p )
835{
836 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000837 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000838 return p->half;
839}
840
841static struct ureg get_zero( struct texenv_fragment_program *p )
842{
843 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000844 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000845 return p->zero;
846}
847
Keith Whitwell15e75e02005-04-29 15:11:39 +0000848
Keith Whitwell15e75e02005-04-29 15:11:39 +0000849static void program_error( struct texenv_fragment_program *p, const char *msg )
850{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000851 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000852 p->error = 1;
853}
854
Keith Whitwell15e75e02005-04-29 15:11:39 +0000855static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000856 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000857{
858 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000859 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000860 assert(!is_undef(p->src_texture[unit]));
861 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000862
Keith Whitwellce721142005-07-11 10:10:38 +0000863 case SRC_TEXTURE0:
864 case SRC_TEXTURE1:
865 case SRC_TEXTURE2:
866 case SRC_TEXTURE3:
867 case SRC_TEXTURE4:
868 case SRC_TEXTURE5:
869 case SRC_TEXTURE6:
870 case SRC_TEXTURE7:
871 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
872 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000873
Keith Whitwellce721142005-07-11 10:10:38 +0000874 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000875 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000876
Keith Whitwellce721142005-07-11 10:10:38 +0000877 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000878 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000879
Brian Paul6947f852009-01-23 17:32:09 -0700880 case SRC_ZERO:
881 return get_zero(p);
882
Keith Whitwellce721142005-07-11 10:10:38 +0000883 case SRC_PREVIOUS:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000884 if (is_undef(p->src_previous))
885 return register_input(p, FRAG_ATTRIB_COL0);
886 else
887 return p->src_previous;
Brian Paul6947f852009-01-23 17:32:09 -0700888
889 default:
890 assert(0);
José Fonsecac6af9b22009-06-15 19:20:25 +0100891 return undef;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000892 }
893}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000894
Keith Whitwell15e75e02005-04-29 15:11:39 +0000895static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000896 GLuint mask,
897 GLuint unit,
898 GLuint source,
899 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000900{
901 struct ureg arg, src, one;
902
903 src = get_source(p, source, unit);
904
905 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000906 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000907 /* Get unused tmp,
908 * Emit tmp = 1.0 - arg.xyzw
909 */
910 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000911 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000912 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000913
Keith Whitwellce721142005-07-11 10:10:38 +0000914 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000915 if (mask == WRITEMASK_W)
916 return src;
917 else
Brian Paul22db5352005-11-19 23:45:10 +0000918 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000919 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000920 /* Get unused tmp,
921 * Emit tmp = 1.0 - arg.wwww
922 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000923 arg = get_temp(p);
924 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000925 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000926 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000927 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000928 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000929 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000930 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000931 case OPR_SRC_COLOR:
Brian Paul6947f852009-01-23 17:32:09 -0700932 return src;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000933 default:
Brian Paul6947f852009-01-23 17:32:09 -0700934 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000935 return src;
936 }
937}
938
Brian Paulf337e2c2009-09-01 15:49:55 -0600939/**
940 * Check if the RGB and Alpha sources and operands match for the given
941 * texture unit's combinder state. When the RGB and A sources and
942 * operands match, we can emit fewer instructions.
943 */
Brian Paula5e70032009-08-31 11:17:59 -0600944static GLboolean args_match( const struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000945{
Brian Paul9ed03152009-09-01 15:57:36 -0600946 GLuint i, numArgs = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000947
Brian Paul457e4272009-09-01 16:22:02 -0600948 for (i = 0; i < numArgs; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000949 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000950 return GL_FALSE;
951
Brian Paul9ed03152009-09-01 15:57:36 -0600952 switch (key->unit[unit].OptA[i].Operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000953 case OPR_SRC_ALPHA:
Brian Paul9ed03152009-09-01 15:57:36 -0600954 switch (key->unit[unit].OptRGB[i].Operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000955 case OPR_SRC_COLOR:
956 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000957 break;
958 default:
959 return GL_FALSE;
960 }
961 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000962 case OPR_ONE_MINUS_SRC_ALPHA:
Brian Paul9ed03152009-09-01 15:57:36 -0600963 switch (key->unit[unit].OptRGB[i].Operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000964 case OPR_ONE_MINUS_SRC_COLOR:
965 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000966 break;
967 default:
968 return GL_FALSE;
969 }
970 break;
971 default:
972 return GL_FALSE; /* impossible */
973 }
974 }
975
976 return GL_TRUE;
977}
978
Keith Whitwell15e75e02005-04-29 15:11:39 +0000979static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000980 struct ureg dest,
981 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000982 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000983 GLuint unit,
984 GLuint nr,
985 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +0000986 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000987{
Brian Paulbd9b2be2009-03-08 17:58:54 -0600988 struct ureg src[MAX_COMBINER_TERMS];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000989 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +0000990 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000991
Brian Paulbd9b2be2009-03-08 17:58:54 -0600992 assert(nr <= MAX_COMBINER_TERMS);
Brian Paul6947f852009-01-23 17:32:09 -0700993
Brian Paul00630842005-12-12 15:27:55 +0000994 tmp = undef; /* silence warning (bug 5318) */
995
Keith Whitwell15e75e02005-04-29 15:11:39 +0000996 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +0000997 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000998
999 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +00001000 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001001 if (mask == WRITEMASK_XYZW && !saturate)
1002 return src[0];
1003 else
Brian Paul7e807512005-11-05 17:10:45 +00001004 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001005 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +00001006 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001007 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001008 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00001009 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001010 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001011 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001012 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001013 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +00001014 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001015 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +00001016 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +00001017 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
1018 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001019 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +00001020 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001021 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
1022 */
Brian Paul7e807512005-11-05 17:10:45 +00001023 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001024
Keith Whitwellce721142005-07-11 10:10:38 +00001025 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +00001026 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001027
Keith Whitwellce721142005-07-11 10:10:38 +00001028 case MODE_DOT3_RGBA:
1029 case MODE_DOT3_RGBA_EXT:
1030 case MODE_DOT3_RGB_EXT:
1031 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001032 struct ureg tmp0 = get_temp( p );
1033 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +00001034 struct ureg neg1 = register_scalar_const(p, -1);
1035 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001036
1037 /* tmp0 = 2*src0 - 1
1038 * tmp1 = 2*src1 - 1
1039 *
1040 * dst = tmp0 dot3 tmp1
1041 */
Brian Paul7e807512005-11-05 17:10:45 +00001042 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001043 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001044
Brian Paulaa206952005-09-16 18:14:24 +00001045 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001046 tmp1 = tmp0;
1047 else
Brian Paul7e807512005-11-05 17:10:45 +00001048 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001049 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +00001050 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001051 return dest;
1052 }
Keith Whitwellce721142005-07-11 10:10:38 +00001053 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001054 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001055 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001056 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +00001057 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001058 /* Arg0 * Arg2 + Arg1 - 0.5 */
1059 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001060 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +00001061 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
1062 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001063 return dest;
1064 }
Keith Whitwellce721142005-07-11 10:10:38 +00001065 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001066 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001067 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001068 return dest;
Brian Paul6947f852009-01-23 17:32:09 -07001069 case MODE_ADD_PRODUCTS:
1070 /* Arg0 * Arg1 + Arg2 * Arg3 */
1071 {
1072 struct ureg tmp0 = get_temp(p);
1073 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1074 emit_arith( p, OPCODE_MAD, dest, mask, saturate, src[2], src[3], tmp0 );
1075 }
1076 return dest;
1077 case MODE_ADD_PRODUCTS_SIGNED:
1078 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1079 {
1080 struct ureg tmp0 = get_temp(p);
1081 half = get_half(p);
1082 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1083 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[2], src[3], tmp0 );
1084 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1085 }
1086 return dest;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001087 case MODE_BUMP_ENVMAP_ATI:
1088 /* special - not handled here */
1089 assert(0);
1090 return src[0];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001091 default:
Brian Paul6947f852009-01-23 17:32:09 -07001092 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001093 return src[0];
1094 }
1095}
1096
Keith Whitwell15e75e02005-04-29 15:11:39 +00001097
Brian Paul22db5352005-11-19 23:45:10 +00001098/**
1099 * Generate instructions for one texture unit's env/combiner mode.
1100 */
1101static struct ureg
1102emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001103{
Brian Paula5e70032009-08-31 11:17:59 -06001104 const struct state_key *key = p->state;
Brian Paul956e6c32009-08-31 11:14:16 -06001105 GLboolean saturate;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001106 GLuint rgb_shift, alpha_shift;
Brian Paula5e70032009-08-31 11:17:59 -06001107 struct ureg out, dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001108
Keith Whitwellce721142005-07-11 10:10:38 +00001109 if (!key->unit[unit].enabled) {
1110 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001111 }
Roland Scheidegger114152e2009-03-12 15:01:16 +01001112 if (key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1113 /* this isn't really a env stage delivering a color and handled elsewhere */
1114 return get_source(p, SRC_PREVIOUS, 0);
1115 }
Keith Whitwellce721142005-07-11 10:10:38 +00001116
1117 switch (key->unit[unit].ModeRGB) {
1118 case MODE_DOT3_RGB_EXT:
1119 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001120 rgb_shift = 0;
1121 break;
Keith Whitwellce721142005-07-11 10:10:38 +00001122 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001123 alpha_shift = 0;
1124 rgb_shift = 0;
1125 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001126 default:
Keith Whitwellce721142005-07-11 10:10:38 +00001127 rgb_shift = key->unit[unit].ScaleShiftRGB;
1128 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001129 break;
1130 }
Keith Whitwellce721142005-07-11 10:10:38 +00001131
Brian Paul956e6c32009-08-31 11:14:16 -06001132 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
1133 * We don't want to clamp twice.
1134 */
1135 saturate = !(rgb_shift || alpha_shift);
1136
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001137 /* If this is the very last calculation, emit direct to output reg:
1138 */
Keith Whitwellce721142005-07-11 10:10:38 +00001139 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001140 unit != p->last_tex_stage ||
1141 alpha_shift ||
1142 rgb_shift)
1143 dest = get_temp( p );
1144 else
Brian Paul8d475822009-02-28 11:49:46 -07001145 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLOR);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001146
1147 /* Emit the RGB and A combine ops
1148 */
Keith Whitwellce721142005-07-11 10:10:38 +00001149 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
1150 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001151 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1152 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001153 key->unit[unit].NumArgsRGB,
1154 key->unit[unit].ModeRGB,
1155 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001156 }
Keith Whitwellce721142005-07-11 10:10:38 +00001157 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +02001158 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001159 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1160 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001161 key->unit[unit].NumArgsRGB,
1162 key->unit[unit].ModeRGB,
1163 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001164 }
1165 else {
1166 /* Need to do something to stop from re-emitting identical
1167 * argument calculations here:
1168 */
1169 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
1170 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001171 key->unit[unit].NumArgsRGB,
1172 key->unit[unit].ModeRGB,
1173 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001174 out = emit_combine( p, dest, WRITEMASK_W, saturate,
1175 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001176 key->unit[unit].NumArgsA,
1177 key->unit[unit].ModeA,
1178 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001179 }
1180
1181 /* Deal with the final shift:
1182 */
1183 if (alpha_shift || rgb_shift) {
Brian Paula5e70032009-08-31 11:17:59 -06001184 struct ureg shift;
1185
Brian Paul956e6c32009-08-31 11:14:16 -06001186 saturate = GL_TRUE; /* always saturate at this point */
Brian Paula5e70032009-08-31 11:17:59 -06001187
Keith Whitwell15e75e02005-04-29 15:11:39 +00001188 if (rgb_shift == alpha_shift) {
José Fonseca452a5922008-05-31 18:14:09 +09001189 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001190 }
1191 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001192 shift = register_const4f(p,
José Fonseca452a5922008-05-31 18:14:09 +09001193 (GLfloat)(1<<rgb_shift),
1194 (GLfloat)(1<<rgb_shift),
1195 (GLfloat)(1<<rgb_shift),
1196 (GLfloat)(1<<alpha_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001197 }
Brian Paul7e807512005-11-05 17:10:45 +00001198 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001199 saturate, out, shift, undef );
1200 }
1201 else
1202 return out;
1203}
1204
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001205
Brian Paul22db5352005-11-19 23:45:10 +00001206/**
1207 * Generate instruction for getting a texture source term.
1208 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001209static void load_texture( struct texenv_fragment_program *p, GLuint unit )
1210{
1211 if (is_undef(p->src_texture[unit])) {
Brian Paulb5ec0a62009-09-01 15:51:23 -06001212 const GLuint texTarget = p->state->unit[unit].source_index;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001213 struct ureg texcoord;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001214 struct ureg tmp = get_tex_temp( p );
1215
Roland Scheidegger114152e2009-03-12 15:01:16 +01001216 if (is_undef(p->texcoord_tex[unit])) {
1217 texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
1218 }
1219 else {
1220 /* might want to reuse this reg for tex output actually */
1221 texcoord = p->texcoord_tex[unit];
1222 }
1223
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001224 /* TODO: Use D0_MASK_XY where possible.
1225 */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +02001226 if (p->state->unit[unit].enabled) {
Brian Paul44e018c2009-02-20 13:42:08 -07001227 GLboolean shadow = GL_FALSE;
1228
1229 if (p->state->unit[unit].shadow) {
1230 p->program->Base.ShadowSamplers |= 1 << unit;
1231 shadow = GL_TRUE;
1232 }
1233
Aapo Tahkolab8915342006-03-28 10:26:34 +00001234 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1235 tmp, WRITEMASK_XYZW,
Brian Paul44e018c2009-02-20 13:42:08 -07001236 unit, texTarget, shadow,
1237 texcoord );
Brian9b7e5a52007-12-14 11:16:49 -07001238
1239 p->program->Base.SamplersUsed |= (1 << unit);
Brian1fe385f2007-12-14 11:42:28 -07001240 /* This identity mapping should already be in place
1241 * (see _mesa_init_program_struct()) but let's be safe.
1242 */
1243 p->program->Base.SamplerUnits[unit] = unit;
Brian9b7e5a52007-12-14 11:16:49 -07001244 }
1245 else
Aapo Tahkolab8915342006-03-28 10:26:34 +00001246 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001247 }
1248}
1249
Keith Whitwell241b6b72005-05-23 09:50:34 +00001250static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +00001251 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001252{
1253 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +00001254 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001255 load_texture(p, unit);
1256 break;
1257
Keith Whitwellce721142005-07-11 10:10:38 +00001258 case SRC_TEXTURE0:
1259 case SRC_TEXTURE1:
1260 case SRC_TEXTURE2:
1261 case SRC_TEXTURE3:
1262 case SRC_TEXTURE4:
1263 case SRC_TEXTURE5:
1264 case SRC_TEXTURE6:
1265 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +00001266 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001267 break;
1268
1269 default:
Brian Paul6947f852009-01-23 17:32:09 -07001270 /* not a texture src - do nothing */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001271 break;
1272 }
Keith Whitwell241b6b72005-05-23 09:50:34 +00001273
1274 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001275}
1276
Brian Paul22db5352005-11-19 23:45:10 +00001277
1278/**
1279 * Generate instructions for loading all texture source terms.
1280 */
1281static GLboolean
Brian Paul457e4272009-09-01 16:22:02 -06001282load_texunit_sources( struct texenv_fragment_program *p, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001283{
Brian Paula5e70032009-08-31 11:17:59 -06001284 const struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +00001285 GLuint i;
1286
1287 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001288 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001289 }
Aapo Tahkolab8915342006-03-28 10:26:34 +00001290
1291 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1292 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1293 }
1294
Keith Whitwell241b6b72005-05-23 09:50:34 +00001295 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001296}
1297
Roland Scheidegger114152e2009-03-12 15:01:16 +01001298/**
1299 * Generate instructions for loading bump map textures.
1300 */
1301static GLboolean
Brian Paul457e4272009-09-01 16:22:02 -06001302load_texunit_bumpmap( struct texenv_fragment_program *p, GLuint unit )
Roland Scheidegger114152e2009-03-12 15:01:16 +01001303{
Brian Paula5e70032009-08-31 11:17:59 -06001304 const struct state_key *key = p->state;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001305 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1306 struct ureg texcDst, bumpMapRes;
1307 struct ureg constdudvcolor = register_const4f(p, 0.0, 0.0, 0.0, 1.0);
1308 struct ureg texcSrc = register_input(p, FRAG_ATTRIB_TEX0 + bumpedUnitNr);
1309 struct ureg rotMat0 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_0, unit );
1310 struct ureg rotMat1 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_1, unit );
1311
1312 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1313
1314 bumpMapRes = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1315 texcDst = get_tex_temp( p );
1316 p->texcoord_tex[bumpedUnitNr] = texcDst;
1317
Brian Paul457e4272009-09-01 16:22:02 -06001318 /* Apply rot matrix and add coords to be available in next phase.
1319 * dest = (Arg0.xxxx * rotMat0 + Arg1) + (Arg0.yyyy * rotMat1)
1320 * note only 2 coords are affected the rest are left unchanged (mul by 0)
1321 */
Roland Scheidegger114152e2009-03-12 15:01:16 +01001322 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1323 swizzle1(bumpMapRes, SWIZZLE_X), rotMat0, texcSrc );
1324 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1325 swizzle1(bumpMapRes, SWIZZLE_Y), rotMat1, texcDst );
1326
Brian Paul457e4272009-09-01 16:22:02 -06001327 /* Move 0,0,0,1 into bumpmap src if someone (crossbar) is foolish
1328 * enough to access this later, should optimize away.
1329 */
1330 emit_arith( p, OPCODE_MOV, bumpMapRes, WRITEMASK_XYZW, 0,
1331 constdudvcolor, undef, undef );
Roland Scheidegger114152e2009-03-12 15:01:16 +01001332
1333 return GL_TRUE;
1334}
Brian Paul22db5352005-11-19 23:45:10 +00001335
1336/**
1337 * Generate a new fragment program which implements the context's
1338 * current texture env/combine mode.
1339 */
1340static void
Brian Paule998c342006-10-29 21:17:18 +00001341create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001342 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001343{
Brian Paule998c342006-10-29 21:17:18 +00001344 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001345 struct texenv_fragment_program p;
1346 GLuint unit;
1347 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001348
Keith Whitwelle4902422005-05-11 15:16:35 +00001349 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwellce721142005-07-11 10:10:38 +00001350 p.state = key;
1351 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001352
Brian Paule998c342006-10-29 21:17:18 +00001353 /* During code generation, use locally-allocated instruction buffer,
1354 * then alloc dynamic storage below.
1355 */
1356 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001357 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Brian Paul457e4272009-09-01 16:22:02 -06001358 p.program->Base.String = NULL;
1359 p.program->Base.NumTexIndirections = 1; /* is this right? */
Brian21f99792007-01-09 11:00:21 -07001360 p.program->Base.NumTexInstructions = 0;
1361 p.program->Base.NumAluInstructions = 0;
Brian Paul457e4272009-09-01 16:22:02 -06001362 p.program->Base.NumInstructions = 0;
1363 p.program->Base.NumTemporaries = 0;
1364 p.program->Base.NumParameters = 0;
1365 p.program->Base.NumAttributes = 0;
1366 p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001367 p.program->Base.Parameters = _mesa_new_parameter_list();
Brian Paul457e4272009-09-01 16:22:02 -06001368 p.program->Base.InputsRead = 0x0;
Brian Paul8d475822009-02-28 11:49:46 -07001369 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLOR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001370
Roland Scheidegger114152e2009-03-12 15:01:16 +01001371 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001372 p.src_texture[unit] = undef;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001373 p.texcoord_tex[unit] = undef;
1374 }
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001375
Keith Whitwell47b29f52005-05-04 11:44:44 +00001376 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001377 p.half = undef;
1378 p.zero = undef;
1379 p.one = undef;
1380
Keith Whitwell15e75e02005-04-29 15:11:39 +00001381 p.last_tex_stage = 0;
Brian93fef222007-10-29 12:25:46 -06001382 release_temps(ctx, &p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001383
Keith Whitwellce721142005-07-11 10:10:38 +00001384 if (key->enabled_units) {
Brian Paul457e4272009-09-01 16:22:02 -06001385 GLboolean needbumpstage = GL_FALSE;
1386
Roland Scheidegger114152e2009-03-12 15:01:16 +01001387 /* Zeroth pass - bump map textures first */
Brian Paul457e4272009-09-01 16:22:02 -06001388 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++)
1389 if (key->unit[unit].enabled &&
1390 key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001391 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 */
Brian Paul457e4272009-09-01 16:22:02 -06001401 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 */
Brian Paul457e4272009-09-01 16:22:02 -06001409 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++)
Brian Paul84d6bed2009-09-01 16:10:57 -06001410 if (key->unit[unit].enabled) {
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;
Brian Paul457e4272009-09-01 16:22:02 -06001443 p.program->Base.InputsRead |= FRAG_BIT_FOGC;
1444 }
1445 else {
Keith Whitwell276330b2005-05-09 17:58:13 +00001446 p.program->FogOption = GL_NONE;
Brian Paul457e4272009-09-01 16:22:02 -06001447 }
Keith Whitwell47b29f52005-05-04 11:44:44 +00001448
Brian21f99792007-01-09 11:00:21 -07001449 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001450 program_error(&p, "Exceeded max nr indirect texture lookups");
1451
Brian21f99792007-01-09 11:00:21 -07001452 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001453 program_error(&p, "Exceeded max TEX instructions");
1454
Brian21f99792007-01-09 11:00:21 -07001455 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001456 program_error(&p, "Exceeded max ALU instructions");
1457
Brian Paul5d7b49f2005-11-19 23:12:20 +00001458 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001459
Brian Paule998c342006-10-29 21:17:18 +00001460 /* Allocate final instruction array */
Brianc81cce72007-09-25 15:18:51 -06001461 p.program->Base.Instructions
1462 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1463 if (!p.program->Base.Instructions) {
Brian Paule998c342006-10-29 21:17:18 +00001464 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1465 "generating tex env program");
1466 return;
1467 }
Brianc81cce72007-09-25 15:18:51 -06001468 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1469 p.program->Base.NumInstructions);
1470
1471 if (p.program->FogOption) {
1472 _mesa_append_fog_code(ctx, p.program);
1473 p.program->FogOption = GL_NONE;
1474 }
1475
Brian Paule998c342006-10-29 21:17:18 +00001476
Keith Whitwelldbeea252005-05-10 13:57:50 +00001477 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001478 */
Brian Paule998c342006-10-29 21:17:18 +00001479 if (ctx->Driver.ProgramStringNotify) {
1480 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1481 &p.program->Base );
1482 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001483
Brian Paule998c342006-10-29 21:17:18 +00001484 if (DISASSEM) {
1485 _mesa_print_program(&p.program->Base);
1486 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001487 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001488}
1489
Brian Paul5d7b49f2005-11-19 23:12:20 +00001490
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001491/**
1492 * Return a fragment program which implements the current
1493 * fixed-function texture, fog and color-sum operations.
1494 */
1495struct gl_fragment_program *
1496_mesa_get_fixed_func_fragment_program(GLcontext *ctx)
Keith Whitwellce721142005-07-11 10:10:38 +00001497{
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001498 struct gl_fragment_program *prog;
1499 struct state_key key;
1500
Chris Wilsona46e3272009-09-02 05:11:25 -07001501 printf("SIZE OF KEY %d\n", sizeof(key));
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001502 make_state_key(ctx, &key);
1503
1504 prog = (struct gl_fragment_program *)
1505 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1506 &key, sizeof(key));
Keith Whitwellce721142005-07-11 10:10:38 +00001507
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001508 if (!prog) {
1509 prog = (struct gl_fragment_program *)
1510 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1511
1512 create_new_program(ctx, &key, prog);
1513
1514 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1515 &key, sizeof(key), &prog->Base);
Keith Whitwellce721142005-07-11 10:10:38 +00001516 }
1517
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001518 return prog;
Keith Whitwellce721142005-07-11 10:10:38 +00001519}