blob: 770a2c26f7e6e0798b677f5bc70dc986e1ca078b [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 {
Brian Paul83e44702009-09-02 08:45:34 -060085#ifdef __GNUC__
Chris Wilsona46e3272009-09-02 05:11:25 -070086 GLubyte Source:4; /**< SRC_x */
87 GLubyte Operand:3; /**< OPR_x */
Brian Paul83e44702009-09-02 08:45:34 -060088#else
89 GLubyte Source; /**< SRC_x */
90 GLubyte Operand; /**< OPR_x */
91#endif
Keith Whitwellce721142005-07-11 10:10:38 +000092};
93
94struct state_key {
Keith Whitwell6280e332008-10-03 13:51:56 +010095 GLuint nr_enabled_units:8;
96 GLuint enabled_units:8;
Brian Paul5d7b49f2005-11-19 23:12:20 +000097 GLuint separate_specular:1;
98 GLuint fog_enabled:1;
Brian Paul9ed03152009-09-01 15:57:36 -060099 GLuint fog_mode:2; /**< FOG_x */
Keith Whitwell6280e332008-10-03 13:51:56 +0100100 GLuint inputs_available:12;
Keith Whitwellce721142005-07-11 10:10:38 +0000101
102 struct {
Brian Paul5d7b49f2005-11-19 23:12:20 +0000103 GLuint enabled:1;
Brian Paul9ed03152009-09-01 15:57:36 -0600104 GLuint source_index:3; /**< TEXTURE_x_INDEX */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +0200105 GLuint shadow:1;
Brian Paul5d7b49f2005-11-19 23:12:20 +0000106 GLuint ScaleShiftRGB:2;
107 GLuint ScaleShiftA:2;
Keith Whitwellce721142005-07-11 10:10:38 +0000108
Brian Paul9ed03152009-09-01 15:57:36 -0600109 GLuint NumArgsRGB:3; /**< up to MAX_COMBINER_TERMS */
Brian Paulf337e2c2009-09-01 15:49:55 -0600110 GLuint ModeRGB:5; /**< MODE_x */
Keith Whitwellce721142005-07-11 10:10:38 +0000111
Brian Paul9ed03152009-09-01 15:57:36 -0600112 GLuint NumArgsA:3; /**< up to MAX_COMBINER_TERMS */
Brian Paulf337e2c2009-09-01 15:49:55 -0600113 GLuint ModeA:5; /**< MODE_x */
Chris Wilsona46e3272009-09-02 05:11:25 -0700114
115 struct mode_opt OptRGB[MAX_COMBINER_TERMS];
Brian Paulbd9b2be2009-03-08 17:58:54 -0600116 struct mode_opt OptA[MAX_COMBINER_TERMS];
Brian Pauld55a28e2009-09-01 15:34:16 -0600117 } unit[MAX_TEXTURE_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000118};
119
120#define FOG_LINEAR 0
121#define FOG_EXP 1
122#define FOG_EXP2 2
123#define FOG_UNKNOWN 3
124
125static GLuint translate_fog_mode( GLenum mode )
126{
127 switch (mode) {
128 case GL_LINEAR: return FOG_LINEAR;
129 case GL_EXP: return FOG_EXP;
130 case GL_EXP2: return FOG_EXP2;
131 default: return FOG_UNKNOWN;
132 }
133}
134
135#define OPR_SRC_COLOR 0
136#define OPR_ONE_MINUS_SRC_COLOR 1
137#define OPR_SRC_ALPHA 2
138#define OPR_ONE_MINUS_SRC_ALPHA 3
139#define OPR_ZERO 4
140#define OPR_ONE 5
141#define OPR_UNKNOWN 7
142
143static GLuint translate_operand( GLenum operand )
144{
145 switch (operand) {
146 case GL_SRC_COLOR: return OPR_SRC_COLOR;
147 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
148 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
149 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
150 case GL_ZERO: return OPR_ZERO;
151 case GL_ONE: return OPR_ONE;
Brian Paul6947f852009-01-23 17:32:09 -0700152 default:
153 assert(0);
154 return OPR_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000155 }
156}
157
158#define SRC_TEXTURE 0
159#define SRC_TEXTURE0 1
160#define SRC_TEXTURE1 2
161#define SRC_TEXTURE2 3
162#define SRC_TEXTURE3 4
163#define SRC_TEXTURE4 5
164#define SRC_TEXTURE5 6
165#define SRC_TEXTURE6 7
166#define SRC_TEXTURE7 8
167#define SRC_CONSTANT 9
168#define SRC_PRIMARY_COLOR 10
169#define SRC_PREVIOUS 11
Brian Paul6947f852009-01-23 17:32:09 -0700170#define SRC_ZERO 12
Keith Whitwellce721142005-07-11 10:10:38 +0000171#define SRC_UNKNOWN 15
172
173static GLuint translate_source( GLenum src )
174{
175 switch (src) {
176 case GL_TEXTURE: return SRC_TEXTURE;
177 case GL_TEXTURE0:
178 case GL_TEXTURE1:
179 case GL_TEXTURE2:
180 case GL_TEXTURE3:
181 case GL_TEXTURE4:
182 case GL_TEXTURE5:
183 case GL_TEXTURE6:
184 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
185 case GL_CONSTANT: return SRC_CONSTANT;
186 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
187 case GL_PREVIOUS: return SRC_PREVIOUS;
Brian Paul6947f852009-01-23 17:32:09 -0700188 case GL_ZERO:
189 return SRC_ZERO;
190 default:
191 assert(0);
192 return SRC_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000193 }
194}
195
Brian Paul6947f852009-01-23 17:32:09 -0700196#define MODE_REPLACE 0 /* r = a0 */
197#define MODE_MODULATE 1 /* r = a0 * a1 */
198#define MODE_ADD 2 /* r = a0 + a1 */
199#define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
200#define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
201#define MODE_SUBTRACT 5 /* r = a0 - a1 */
202#define MODE_DOT3_RGB 6 /* r = a0 . a1 */
203#define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
204#define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
205#define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
206#define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
207#define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
208#define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
209#define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
210#define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
Roland Scheidegger114152e2009-03-12 15:01:16 +0100211#define MODE_BUMP_ENVMAP_ATI 15 /* special */
212#define MODE_UNKNOWN 16
Keith Whitwellce721142005-07-11 10:10:38 +0000213
Brian Paul6947f852009-01-23 17:32:09 -0700214/**
215 * Translate GL combiner state into a MODE_x value
216 */
217static GLuint translate_mode( GLenum envMode, GLenum mode )
Keith Whitwellce721142005-07-11 10:10:38 +0000218{
219 switch (mode) {
220 case GL_REPLACE: return MODE_REPLACE;
221 case GL_MODULATE: return MODE_MODULATE;
Brian Paul6947f852009-01-23 17:32:09 -0700222 case GL_ADD:
223 if (envMode == GL_COMBINE4_NV)
224 return MODE_ADD_PRODUCTS;
225 else
226 return MODE_ADD;
227 case GL_ADD_SIGNED:
228 if (envMode == GL_COMBINE4_NV)
229 return MODE_ADD_PRODUCTS_SIGNED;
230 else
231 return MODE_ADD_SIGNED;
Keith Whitwellce721142005-07-11 10:10:38 +0000232 case GL_INTERPOLATE: return MODE_INTERPOLATE;
233 case GL_SUBTRACT: return MODE_SUBTRACT;
234 case GL_DOT3_RGB: return MODE_DOT3_RGB;
235 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
236 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
237 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
238 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
239 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
240 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
Roland Scheidegger114152e2009-03-12 15:01:16 +0100241 case GL_BUMP_ENVMAP_ATI: return MODE_BUMP_ENVMAP_ATI;
Brian Paul6947f852009-01-23 17:32:09 -0700242 default:
243 assert(0);
244 return MODE_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000245 }
246}
247
Brian Paulf337e2c2009-09-01 15:49:55 -0600248
Brian Paulf337e2c2009-09-01 15:49:55 -0600249/**
250 * Translate TEXTURE_x_BIT to TEXTURE_x_INDEX.
251 */
Brian Paule00ac112005-09-15 05:00:45 +0000252static GLuint translate_tex_src_bit( GLbitfield bit )
Keith Whitwellce721142005-07-11 10:10:38 +0000253{
Brian Paulb5ec0a62009-09-01 15:51:23 -0600254 ASSERT(bit);
255 return _mesa_ffs(bit) - 1;
Keith Whitwellce721142005-07-11 10:10:38 +0000256}
257
Brian Paulf337e2c2009-09-01 15:49:55 -0600258
Keith Whitwell1680ef82008-10-03 17:30:59 +0100259#define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
260#define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
261
Brian Paul239617f2008-10-07 11:22:47 -0600262/**
263 * Identify all possible varying inputs. The fragment program will
Keith Whitwell1680ef82008-10-03 17:30:59 +0100264 * never reference non-varying inputs, but will track them via state
265 * constants instead.
266 *
267 * This function figures out all the inputs that the fragment program
268 * has access to. The bitmask is later reduced to just those which
269 * are actually referenced.
270 */
Brian Paul239617f2008-10-07 11:22:47 -0600271static GLbitfield get_fp_input_mask( GLcontext *ctx )
Keith Whitwell1680ef82008-10-03 17:30:59 +0100272{
Eric Anholte5f63c42009-06-02 07:47:20 -0700273 /* _NEW_PROGRAM */
Brian Paulf0b07942008-12-17 14:04:03 -0700274 const GLboolean vertexShader = (ctx->Shader.CurrentProgram &&
Eric Anholt40990d92009-08-03 12:36:52 -0700275 ctx->Shader.CurrentProgram->LinkStatus &&
Brian Paulf0b07942008-12-17 14:04:03 -0700276 ctx->Shader.CurrentProgram->VertexProgram);
277 const GLboolean vertexProgram = ctx->VertexProgram._Enabled;
Brian Paul239617f2008-10-07 11:22:47 -0600278 GLbitfield fp_inputs = 0x0;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100279
Brian Paul6d4d51d2008-10-10 13:39:14 -0600280 if (ctx->VertexProgram._Overriden) {
281 /* Somebody's messing with the vertex program and we don't have
282 * a clue what's happening. Assume that it could be producing
283 * all possible outputs.
284 */
285 fp_inputs = ~0;
286 }
287 else if (ctx->RenderMode == GL_FEEDBACK) {
Eric Anholte5f63c42009-06-02 07:47:20 -0700288 /* _NEW_RENDERMODE */
Brian Paul6d4d51d2008-10-10 13:39:14 -0600289 fp_inputs = (FRAG_BIT_COL0 | FRAG_BIT_TEX0);
290 }
Brian Paulf0b07942008-12-17 14:04:03 -0700291 else if (!(vertexProgram || vertexShader) ||
Brian Pauld7296a12008-12-17 11:29:06 -0700292 !ctx->VertexProgram._Current) {
Brian Paulf0b07942008-12-17 14:04:03 -0700293 /* Fixed function vertex logic */
Eric Anholte5f63c42009-06-02 07:47:20 -0700294 /* _NEW_ARRAY */
Brian Paul239617f2008-10-07 11:22:47 -0600295 GLbitfield varying_inputs = ctx->varying_vp_inputs;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100296
Keith Whitwell97e63432008-10-20 13:03:45 +0100297 /* These get generated in the setup routine regardless of the
298 * vertex program:
299 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700300 /* _NEW_POINT */
Keith Whitwell97e63432008-10-20 13:03:45 +0100301 if (ctx->Point.PointSprite)
302 varying_inputs |= FRAG_BITS_TEX_ANY;
303
Keith Whitwell1680ef82008-10-03 17:30:59 +0100304 /* First look at what values may be computed by the generated
305 * vertex program:
306 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700307 /* _NEW_LIGHT */
Keith Whitwell1680ef82008-10-03 17:30:59 +0100308 if (ctx->Light.Enabled) {
309 fp_inputs |= FRAG_BIT_COL0;
310
Eric Anholt9cea84b2009-07-16 18:41:03 -0700311 if (texenv_doing_secondary_color(ctx))
Keith Whitwell1680ef82008-10-03 17:30:59 +0100312 fp_inputs |= FRAG_BIT_COL1;
313 }
314
Eric Anholte5f63c42009-06-02 07:47:20 -0700315 /* _NEW_TEXTURE */
Keith Whitwell1680ef82008-10-03 17:30:59 +0100316 fp_inputs |= (ctx->Texture._TexGenEnabled |
317 ctx->Texture._TexMatEnabled) << FRAG_ATTRIB_TEX0;
318
319 /* Then look at what might be varying as a result of enabled
320 * arrays, etc:
321 */
Brian Paul6807d962009-08-07 09:42:28 -0600322 if (varying_inputs & VERT_BIT_COLOR0)
323 fp_inputs |= FRAG_BIT_COL0;
324 if (varying_inputs & VERT_BIT_COLOR1)
325 fp_inputs |= FRAG_BIT_COL1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100326
327 fp_inputs |= (((varying_inputs & VERT_BIT_TEX_ANY) >> VERT_ATTRIB_TEX0)
328 << FRAG_ATTRIB_TEX0);
329
330 }
331 else {
332 /* calculate from vp->outputs */
Brian Paul2389c052008-12-17 19:01:34 -0700333 struct gl_vertex_program *vprog;
334 GLbitfield vp_outputs;
335
336 /* Choose GLSL vertex shader over ARB vertex program. Need this
337 * since vertex shader state validation comes after fragment state
338 * validation (see additional comments in state.c).
339 */
340 if (vertexShader)
341 vprog = ctx->Shader.CurrentProgram->VertexProgram;
342 else
Eric Anholta9ba1bf2009-08-03 12:38:56 -0700343 vprog = ctx->VertexProgram.Current;
Brian Paul2389c052008-12-17 19:01:34 -0700344
345 vp_outputs = vprog->Base.OutputsWritten;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100346
Keith Whitwell97e63432008-10-20 13:03:45 +0100347 /* These get generated in the setup routine regardless of the
348 * vertex program:
349 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700350 /* _NEW_POINT */
Keith Whitwell97e63432008-10-20 13:03:45 +0100351 if (ctx->Point.PointSprite)
352 vp_outputs |= FRAG_BITS_TEX_ANY;
353
Brian Paul6807d962009-08-07 09:42:28 -0600354 if (vp_outputs & (1 << VERT_RESULT_COL0))
355 fp_inputs |= FRAG_BIT_COL0;
356 if (vp_outputs & (1 << VERT_RESULT_COL1))
357 fp_inputs |= FRAG_BIT_COL1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100358
Keith Whitwell0370d6b2008-10-04 12:41:56 +0100359 fp_inputs |= (((vp_outputs & VERT_RESULT_TEX_ANY) >> VERT_RESULT_TEX0)
360 << FRAG_ATTRIB_TEX0);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100361 }
362
363 return fp_inputs;
364}
365
366
Brian Paul22db5352005-11-19 23:45:10 +0000367/**
368 * Examine current texture environment state and generate a unique
369 * key to identify it.
370 */
Keith Whitwell8065c122006-05-22 16:09:27 +0000371static void make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000372{
Keith Whitwellce721142005-07-11 10:10:38 +0000373 GLuint i, j;
Brian Paul239617f2008-10-07 11:22:47 -0600374 GLbitfield inputs_referenced = FRAG_BIT_COL0;
Brian Paulf337e2c2009-09-01 15:49:55 -0600375 const GLbitfield inputs_available = get_fp_input_mask( ctx );
Keith Whitwell1680ef82008-10-03 17:30:59 +0100376
Keith Whitwell8065c122006-05-22 16:09:27 +0000377 memset(key, 0, sizeof(*key));
378
Eric Anholte5f63c42009-06-02 07:47:20 -0700379 /* _NEW_TEXTURE */
Brian Paule9b34882008-12-31 11:54:02 -0700380 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000381 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Brian Paulf337e2c2009-09-01 15:49:55 -0600382 const struct gl_texture_object *texObj = texUnit->_Current;
383 const struct gl_tex_env_combine_state *comb = texUnit->_CurrentCombine;
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800384 GLenum format;
Roland Scheideggerbf4a0fa2008-02-15 17:26:06 +0100385
386 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
Keith Whitwellce721142005-07-11 10:10:38 +0000387 continue;
388
Brian Paulf337e2c2009-09-01 15:49:55 -0600389 format = texObj->Image[0][texObj->BaseLevel]->_BaseFormat;
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800390
Keith Whitwellce721142005-07-11 10:10:38 +0000391 key->unit[i].enabled = 1;
392 key->enabled_units |= (1<<i);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100393 key->nr_enabled_units = i+1;
394 inputs_referenced |= FRAG_BIT_TEX(i);
Keith Whitwellce721142005-07-11 10:10:38 +0000395
Brian Paulf337e2c2009-09-01 15:49:55 -0600396 key->unit[i].source_index =
397 translate_tex_src_bit(texUnit->_ReallyEnabled);
398
399 key->unit[i].shadow = ((texObj->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800400 ((format == GL_DEPTH_COMPONENT) ||
401 (format == GL_DEPTH_STENCIL_EXT)));
Keith Whitwellce721142005-07-11 10:10:38 +0000402
Brian Paulf337e2c2009-09-01 15:49:55 -0600403 key->unit[i].NumArgsRGB = comb->_NumArgsRGB;
404 key->unit[i].NumArgsA = comb->_NumArgsA;
Keith Whitwellce721142005-07-11 10:10:38 +0000405
406 key->unit[i].ModeRGB =
Brian Paulf337e2c2009-09-01 15:49:55 -0600407 translate_mode(texUnit->EnvMode, comb->ModeRGB);
Keith Whitwellce721142005-07-11 10:10:38 +0000408 key->unit[i].ModeA =
Brian Paulf337e2c2009-09-01 15:49:55 -0600409 translate_mode(texUnit->EnvMode, comb->ModeA);
Roland Scheidegger114152e2009-03-12 15:01:16 +0100410
Brian Paulf337e2c2009-09-01 15:49:55 -0600411 key->unit[i].ScaleShiftRGB = comb->ScaleShiftRGB;
412 key->unit[i].ScaleShiftA = comb->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000413
Brian Paulbd9b2be2009-03-08 17:58:54 -0600414 for (j = 0; j < MAX_COMBINER_TERMS; j++) {
Brian Paulf337e2c2009-09-01 15:49:55 -0600415 key->unit[i].OptRGB[j].Operand = translate_operand(comb->OperandRGB[j]);
416 key->unit[i].OptA[j].Operand = translate_operand(comb->OperandA[j]);
417 key->unit[i].OptRGB[j].Source = translate_source(comb->SourceRGB[j]);
418 key->unit[i].OptA[j].Source = translate_source(comb->SourceA[j]);
Keith Whitwellce721142005-07-11 10:10:38 +0000419 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100420
421 if (key->unit[i].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
422 /* requires some special translation */
423 key->unit[i].NumArgsRGB = 2;
424 key->unit[i].ScaleShiftRGB = 0;
425 key->unit[i].OptRGB[0].Operand = OPR_SRC_COLOR;
426 key->unit[i].OptRGB[0].Source = SRC_TEXTURE;
427 key->unit[i].OptRGB[1].Operand = OPR_SRC_COLOR;
428 key->unit[i].OptRGB[1].Source = texUnit->BumpTarget - GL_TEXTURE0 + SRC_TEXTURE0;
429 }
Keith Whitwellce721142005-07-11 10:10:38 +0000430 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100431
Eric Anholt9cea84b2009-07-16 18:41:03 -0700432 /* _NEW_LIGHT | _NEW_FOG */
433 if (texenv_doing_secondary_color(ctx)) {
Keith Whitwellce721142005-07-11 10:10:38 +0000434 key->separate_specular = 1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100435 inputs_referenced |= FRAG_BIT_COL1;
436 }
Keith Whitwellce721142005-07-11 10:10:38 +0000437
Eric Anholte5f63c42009-06-02 07:47:20 -0700438 /* _NEW_FOG */
Keith Whitwellce721142005-07-11 10:10:38 +0000439 if (ctx->Fog.Enabled) {
440 key->fog_enabled = 1;
441 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100442 inputs_referenced |= FRAG_BIT_FOGC; /* maybe */
Keith Whitwellce721142005-07-11 10:10:38 +0000443 }
Keith Whitwell1680ef82008-10-03 17:30:59 +0100444
445 key->inputs_available = (inputs_available & inputs_referenced);
Keith Whitwellce721142005-07-11 10:10:38 +0000446}
447
Brian Paul239617f2008-10-07 11:22:47 -0600448/**
449 * Use uregs to represent registers internally, translate to Mesa's
Keith Whitwell15e75e02005-04-29 15:11:39 +0000450 * expected formats on emit.
451 *
452 * NOTE: These are passed by value extensively in this file rather
453 * than as usual by pointer reference. If this disturbs you, try
454 * remembering they are just 32bits in size.
455 *
456 * GCC is smart enough to deal with these dword-sized structures in
457 * much the same way as if I had defined them as dwords and was using
458 * macros to access and set the fields. This is much nicer and easier
459 * to evolve.
460 */
461struct ureg {
462 GLuint file:4;
463 GLuint idx:8;
464 GLuint negatebase:1;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000465 GLuint swz:12;
Brian Paul1480bca2009-09-01 16:01:12 -0600466 GLuint pad:7;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000467};
468
Brian Paul13abf912006-04-13 19:17:13 +0000469static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000470 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000471 ~0,
472 0,
473 0,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000474 0
475};
476
Keith Whitwell15e75e02005-04-29 15:11:39 +0000477
Brian Paul239617f2008-10-07 11:22:47 -0600478/** State used to build the fragment program:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000479 */
480struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000481 struct gl_fragment_program *program;
Keith Whitwellce721142005-07-11 10:10:38 +0000482 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000483
Brian Paul239617f2008-10-07 11:22:47 -0600484 GLbitfield alu_temps; /**< Track texture indirections, see spec. */
485 GLbitfield temps_output; /**< Track texture indirections, see spec. */
486 GLbitfield temp_in_use; /**< Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000487 GLboolean error;
488
Brian Paule9b34882008-12-31 11:54:02 -0700489 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000490 /* Reg containing each texture unit's sampled texture color,
491 * else undef.
492 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000493
Roland Scheidegger114152e2009-03-12 15:01:16 +0100494 struct ureg texcoord_tex[MAX_TEXTURE_COORD_UNITS];
495 /* Reg containing texcoord for a texture unit,
496 * needed for bump mapping, else undef.
497 */
498
Brian Paul239617f2008-10-07 11:22:47 -0600499 struct ureg src_previous; /**< Reg containing color from previous
Keith Whitwell15e75e02005-04-29 15:11:39 +0000500 * stage. May need to be decl'd.
501 */
502
Brian Paul239617f2008-10-07 11:22:47 -0600503 GLuint last_tex_stage; /**< Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000504
505 struct ureg half;
506 struct ureg one;
507 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000508};
509
510
511
512static struct ureg make_ureg(GLuint file, GLuint idx)
513{
514 struct ureg reg;
515 reg.file = file;
516 reg.idx = idx;
517 reg.negatebase = 0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000518 reg.swz = SWIZZLE_NOOP;
519 reg.pad = 0;
520 return reg;
521}
522
523static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
524{
525 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
526 GET_SWZ(reg.swz, y),
527 GET_SWZ(reg.swz, z),
528 GET_SWZ(reg.swz, w));
529
530 return reg;
531}
532
533static struct ureg swizzle1( struct ureg reg, int x )
534{
535 return swizzle(reg, x, x, x, x);
536}
537
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000538static struct ureg negate( struct ureg reg )
539{
540 reg.negatebase ^= 1;
541 return reg;
542}
543
Keith Whitwell15e75e02005-04-29 15:11:39 +0000544static GLboolean is_undef( struct ureg reg )
545{
Alan Hourihane8d972652006-08-10 13:12:00 +0000546 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000547}
548
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000549
Keith Whitwell15e75e02005-04-29 15:11:39 +0000550static struct ureg get_temp( struct texenv_fragment_program *p )
551{
Brian Paul13abf912006-04-13 19:17:13 +0000552 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000553
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000554 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000555 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000556 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000557
558 /* Then any unused temporary:
559 */
560 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000561 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000562
Keith Whitwell15e75e02005-04-29 15:11:39 +0000563 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000564 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000565 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000566 }
567
Brian Paul13abf912006-04-13 19:17:13 +0000568 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000569 p->program->Base.NumTemporaries = bit;
570
Keith Whitwell93cd9232005-05-11 08:30:23 +0000571 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000572 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
573}
574
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000575static struct ureg get_tex_temp( struct texenv_fragment_program *p )
576{
577 int bit;
578
Brian Pauld01269a2008-09-25 18:27:22 -0600579 /* First try to find available temp not previously used (to avoid
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000580 * starting a new texture indirection). According to the spec, the
581 * ~p->temps_output isn't necessary, but will keep it there for
582 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000583 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000584 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000585
586 /* Then any unused temporary:
587 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000588 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000589 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000590
591 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000592 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000593 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000594 }
595
Brian Paul13abf912006-04-13 19:17:13 +0000596 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000597 p->program->Base.NumTemporaries = bit;
598
Keith Whitwell93cd9232005-05-11 08:30:23 +0000599 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000600 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
601}
602
Keith Whitwell15e75e02005-04-29 15:11:39 +0000603
Brian Paul5620c202008-09-26 11:18:06 -0600604/** Mark a temp reg as being no longer allocatable. */
605static void reserve_temp( struct texenv_fragment_program *p, struct ureg r )
606{
607 if (r.file == PROGRAM_TEMPORARY)
608 p->temps_output |= (1 << r.idx);
609}
610
611
Brian93fef222007-10-29 12:25:46 -0600612static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000613{
Brian93fef222007-10-29 12:25:46 -0600614 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000615
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000616 /* KW: To support tex_env_crossbar, don't release the registers in
617 * temps_output.
618 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000619 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000620 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000621 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000622 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000623}
624
625
Brian9fe3e2e2007-02-23 11:44:14 -0700626static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000627 GLint s0,
628 GLint s1,
629 GLint s2,
630 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700631 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000632{
Brian9fe3e2e2007-02-23 11:44:14 -0700633 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000634 GLuint idx;
635 tokens[0] = s0;
636 tokens[1] = s1;
637 tokens[2] = s2;
638 tokens[3] = s3;
639 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000640 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000641 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000642}
643
Keith Whitwell47b29f52005-05-04 11:44:44 +0000644
Brian9fe3e2e2007-02-23 11:44:14 -0700645#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
646#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
647#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
648#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000649
Keith Whitwell1680ef82008-10-03 17:30:59 +0100650static GLuint frag_to_vert_attrib( GLuint attrib )
651{
652 switch (attrib) {
653 case FRAG_ATTRIB_COL0: return VERT_ATTRIB_COLOR0;
654 case FRAG_ATTRIB_COL1: return VERT_ATTRIB_COLOR1;
655 default:
656 assert(attrib >= FRAG_ATTRIB_TEX0);
657 assert(attrib <= FRAG_ATTRIB_TEX7);
658 return attrib - FRAG_ATTRIB_TEX0 + VERT_ATTRIB_TEX0;
659 }
660}
661
Keith Whitwell47b29f52005-05-04 11:44:44 +0000662
663static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
664{
Keith Whitwell1680ef82008-10-03 17:30:59 +0100665 if (p->state->inputs_available & (1<<input)) {
666 p->program->Base.InputsRead |= (1 << input);
667 return make_ureg(PROGRAM_INPUT, input);
668 }
669 else {
670 GLuint idx = frag_to_vert_attrib( input );
671 return register_param3( p, STATE_INTERNAL, STATE_CURRENT_ATTRIB, idx );
672 }
Keith Whitwell47b29f52005-05-04 11:44:44 +0000673}
674
675
Brian Paul7e807512005-11-05 17:10:45 +0000676static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000677 struct ureg ureg )
678{
679 reg->File = ureg.file;
680 reg->Index = ureg.idx;
681 reg->Swizzle = ureg.swz;
Brian Paul7db7ff82009-04-14 22:14:30 -0600682 reg->Negate = ureg.negatebase ? NEGATE_XYZW : NEGATE_NONE;
Brian Paul1480bca2009-09-01 16:01:12 -0600683 reg->Abs = GL_FALSE;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000684}
685
Brian Paul7e807512005-11-05 17:10:45 +0000686static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000687 struct ureg ureg, GLuint mask )
688{
689 dst->File = ureg.file;
690 dst->Index = ureg.idx;
691 dst->WriteMask = mask;
Brianc9d495c2007-10-23 10:55:24 -0600692 dst->CondMask = COND_TR; /* always pass cond test */
693 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000694}
695
Brian Paul7e807512005-11-05 17:10:45 +0000696static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000697emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000698 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000699 struct ureg dest,
700 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000701 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000702 struct ureg src0,
703 struct ureg src1,
704 struct ureg src2 )
705{
Brian Paul9ed03152009-09-01 15:57:36 -0600706 const GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000707 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Brian2a8e9bb2007-10-23 10:24:53 -0600708
709 assert(nr < MAX_INSTRUCTIONS);
710
Brian Pauld6272e02006-10-29 18:03:16 +0000711 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000712 inst->Opcode = op;
713
Keith Whitwell47b29f52005-05-04 11:44:44 +0000714 emit_arg( &inst->SrcReg[0], src0 );
715 emit_arg( &inst->SrcReg[1], src1 );
716 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000717
Brian Paule31ac052005-11-20 17:52:40 +0000718 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000719
720 emit_dst( &inst->DstReg, dest, mask );
721
Brian Paul5620c202008-09-26 11:18:06 -0600722#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000723 /* Accounting for indirection tracking:
724 */
725 if (dest.file == PROGRAM_TEMPORARY)
726 p->temps_output |= 1 << dest.idx;
Brian Paul5620c202008-09-26 11:18:06 -0600727#endif
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000728
Keith Whitwell15e75e02005-04-29 15:11:39 +0000729 return inst;
730}
731
732
733static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000734 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000735 struct ureg dest,
736 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000737 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000738 struct ureg src0,
739 struct ureg src1,
740 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000741{
742 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
743
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000744 /* Accounting for indirection tracking:
745 */
746 if (src0.file == PROGRAM_TEMPORARY)
747 p->alu_temps |= 1 << src0.idx;
748
749 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
750 p->alu_temps |= 1 << src1.idx;
751
752 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
753 p->alu_temps |= 1 << src2.idx;
754
755 if (dest.file == PROGRAM_TEMPORARY)
756 p->alu_temps |= 1 << dest.idx;
757
Brian21f99792007-01-09 11:00:21 -0700758 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000759 return dest;
760}
761
762static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000763 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000764 struct ureg dest,
765 GLuint destmask,
766 GLuint tex_unit,
767 GLuint tex_idx,
Brian Paul44e018c2009-02-20 13:42:08 -0700768 GLuint tex_shadow,
Keith Whitwellce721142005-07-11 10:10:38 +0000769 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000770{
Brian Paul7e807512005-11-05 17:10:45 +0000771 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000772 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000773 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000774 coord, /* arg 0? */
775 undef,
776 undef);
777
Brian Paul7e807512005-11-05 17:10:45 +0000778 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000779 inst->TexSrcUnit = tex_unit;
Brian Paul44e018c2009-02-20 13:42:08 -0700780 inst->TexShadow = tex_shadow;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000781
Brian21f99792007-01-09 11:00:21 -0700782 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000783
Brian Paul5620c202008-09-26 11:18:06 -0600784 /* Accounting for indirection tracking:
785 */
786 reserve_temp(p, dest);
787
Roland Scheidegger114152e2009-03-12 15:01:16 +0100788#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000789 /* Is this a texture indirection?
790 */
791 if ((coord.file == PROGRAM_TEMPORARY &&
792 (p->temps_output & (1<<coord.idx))) ||
793 (dest.file == PROGRAM_TEMPORARY &&
794 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700795 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000796 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000797 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000798 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000799 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100800#endif
Keith Whitwell15e75e02005-04-29 15:11:39 +0000801
802 return dest;
803}
804
805
Keith Whitwell47b29f52005-05-04 11:44:44 +0000806static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000807 GLfloat s0,
808 GLfloat s1,
809 GLfloat s2,
810 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000811{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000812 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700813 GLuint idx, swizzle;
Brian Pauld01269a2008-09-25 18:27:22 -0600814 struct ureg r;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000815 values[0] = s0;
816 values[1] = s1;
817 values[2] = s2;
818 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700819 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
820 &swizzle );
Brian Pauld01269a2008-09-25 18:27:22 -0600821 r = make_ureg(PROGRAM_CONSTANT, idx);
822 r.swz = swizzle;
823 return r;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000824}
825
Keith Whitwelle4902422005-05-11 15:16:35 +0000826#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000827#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
828#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
829#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000830
831
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000832static struct ureg get_one( struct texenv_fragment_program *p )
833{
834 if (is_undef(p->one))
835 p->one = register_scalar_const(p, 1.0);
836 return p->one;
837}
838
839static struct ureg get_half( struct texenv_fragment_program *p )
840{
841 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000842 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000843 return p->half;
844}
845
846static struct ureg get_zero( struct texenv_fragment_program *p )
847{
848 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000849 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000850 return p->zero;
851}
852
Keith Whitwell15e75e02005-04-29 15:11:39 +0000853
Keith Whitwell15e75e02005-04-29 15:11:39 +0000854static void program_error( struct texenv_fragment_program *p, const char *msg )
855{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000856 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000857 p->error = 1;
858}
859
Keith Whitwell15e75e02005-04-29 15:11:39 +0000860static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000861 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000862{
863 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000864 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000865 assert(!is_undef(p->src_texture[unit]));
866 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000867
Keith Whitwellce721142005-07-11 10:10:38 +0000868 case SRC_TEXTURE0:
869 case SRC_TEXTURE1:
870 case SRC_TEXTURE2:
871 case SRC_TEXTURE3:
872 case SRC_TEXTURE4:
873 case SRC_TEXTURE5:
874 case SRC_TEXTURE6:
875 case SRC_TEXTURE7:
876 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
877 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000878
Keith Whitwellce721142005-07-11 10:10:38 +0000879 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000880 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000881
Keith Whitwellce721142005-07-11 10:10:38 +0000882 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000883 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000884
Brian Paul6947f852009-01-23 17:32:09 -0700885 case SRC_ZERO:
886 return get_zero(p);
887
Keith Whitwellce721142005-07-11 10:10:38 +0000888 case SRC_PREVIOUS:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000889 if (is_undef(p->src_previous))
890 return register_input(p, FRAG_ATTRIB_COL0);
891 else
892 return p->src_previous;
Brian Paul6947f852009-01-23 17:32:09 -0700893
894 default:
895 assert(0);
José Fonsecac6af9b22009-06-15 19:20:25 +0100896 return undef;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000897 }
898}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000899
Keith Whitwell15e75e02005-04-29 15:11:39 +0000900static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000901 GLuint mask,
902 GLuint unit,
903 GLuint source,
904 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000905{
906 struct ureg arg, src, one;
907
908 src = get_source(p, source, unit);
909
910 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000911 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000912 /* Get unused tmp,
913 * Emit tmp = 1.0 - arg.xyzw
914 */
915 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000916 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000917 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000918
Keith Whitwellce721142005-07-11 10:10:38 +0000919 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000920 if (mask == WRITEMASK_W)
921 return src;
922 else
Brian Paul22db5352005-11-19 23:45:10 +0000923 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000924 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000925 /* Get unused tmp,
926 * Emit tmp = 1.0 - arg.wwww
927 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000928 arg = get_temp(p);
929 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000930 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000931 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000932 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000933 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000934 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000935 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000936 case OPR_SRC_COLOR:
Brian Paul6947f852009-01-23 17:32:09 -0700937 return src;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000938 default:
Brian Paul6947f852009-01-23 17:32:09 -0700939 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000940 return src;
941 }
942}
943
Brian Paulf337e2c2009-09-01 15:49:55 -0600944/**
945 * Check if the RGB and Alpha sources and operands match for the given
946 * texture unit's combinder state. When the RGB and A sources and
947 * operands match, we can emit fewer instructions.
948 */
Brian Paula5e70032009-08-31 11:17:59 -0600949static GLboolean args_match( const struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000950{
Brian Paul9ed03152009-09-01 15:57:36 -0600951 GLuint i, numArgs = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000952
Brian Paul457e4272009-09-01 16:22:02 -0600953 for (i = 0; i < numArgs; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000954 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000955 return GL_FALSE;
956
Brian Paul9ed03152009-09-01 15:57:36 -0600957 switch (key->unit[unit].OptA[i].Operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000958 case OPR_SRC_ALPHA:
Brian Paul9ed03152009-09-01 15:57:36 -0600959 switch (key->unit[unit].OptRGB[i].Operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000960 case OPR_SRC_COLOR:
961 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000962 break;
963 default:
964 return GL_FALSE;
965 }
966 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000967 case OPR_ONE_MINUS_SRC_ALPHA:
Brian Paul9ed03152009-09-01 15:57:36 -0600968 switch (key->unit[unit].OptRGB[i].Operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000969 case OPR_ONE_MINUS_SRC_COLOR:
970 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000971 break;
972 default:
973 return GL_FALSE;
974 }
975 break;
976 default:
977 return GL_FALSE; /* impossible */
978 }
979 }
980
981 return GL_TRUE;
982}
983
Keith Whitwell15e75e02005-04-29 15:11:39 +0000984static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000985 struct ureg dest,
986 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000987 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000988 GLuint unit,
989 GLuint nr,
990 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +0000991 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000992{
Brian Paulbd9b2be2009-03-08 17:58:54 -0600993 struct ureg src[MAX_COMBINER_TERMS];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000994 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +0000995 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000996
Brian Paulbd9b2be2009-03-08 17:58:54 -0600997 assert(nr <= MAX_COMBINER_TERMS);
Brian Paul6947f852009-01-23 17:32:09 -0700998
Brian Paul00630842005-12-12 15:27:55 +0000999 tmp = undef; /* silence warning (bug 5318) */
1000
Keith Whitwell15e75e02005-04-29 15:11:39 +00001001 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +00001002 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001003
1004 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +00001005 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001006 if (mask == WRITEMASK_XYZW && !saturate)
1007 return src[0];
1008 else
Brian Paul7e807512005-11-05 17:10:45 +00001009 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001010 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +00001011 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001012 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001013 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00001014 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001015 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001016 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001017 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001018 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +00001019 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001020 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +00001021 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +00001022 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
1023 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001024 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +00001025 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001026 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
1027 */
Brian Paul7e807512005-11-05 17:10:45 +00001028 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001029
Keith Whitwellce721142005-07-11 10:10:38 +00001030 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +00001031 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001032
Keith Whitwellce721142005-07-11 10:10:38 +00001033 case MODE_DOT3_RGBA:
1034 case MODE_DOT3_RGBA_EXT:
1035 case MODE_DOT3_RGB_EXT:
1036 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001037 struct ureg tmp0 = get_temp( p );
1038 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +00001039 struct ureg neg1 = register_scalar_const(p, -1);
1040 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001041
1042 /* tmp0 = 2*src0 - 1
1043 * tmp1 = 2*src1 - 1
1044 *
1045 * dst = tmp0 dot3 tmp1
1046 */
Brian Paul7e807512005-11-05 17:10:45 +00001047 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001048 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001049
Brian Paulaa206952005-09-16 18:14:24 +00001050 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001051 tmp1 = tmp0;
1052 else
Brian Paul7e807512005-11-05 17:10:45 +00001053 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001054 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +00001055 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001056 return dest;
1057 }
Keith Whitwellce721142005-07-11 10:10:38 +00001058 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001059 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001060 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001061 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +00001062 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001063 /* Arg0 * Arg2 + Arg1 - 0.5 */
1064 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001065 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +00001066 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
1067 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001068 return dest;
1069 }
Keith Whitwellce721142005-07-11 10:10:38 +00001070 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001071 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001072 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001073 return dest;
Brian Paul6947f852009-01-23 17:32:09 -07001074 case MODE_ADD_PRODUCTS:
1075 /* Arg0 * Arg1 + Arg2 * Arg3 */
1076 {
1077 struct ureg tmp0 = get_temp(p);
1078 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1079 emit_arith( p, OPCODE_MAD, dest, mask, saturate, src[2], src[3], tmp0 );
1080 }
1081 return dest;
1082 case MODE_ADD_PRODUCTS_SIGNED:
1083 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1084 {
1085 struct ureg tmp0 = get_temp(p);
1086 half = get_half(p);
1087 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1088 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[2], src[3], tmp0 );
1089 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1090 }
1091 return dest;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001092 case MODE_BUMP_ENVMAP_ATI:
1093 /* special - not handled here */
1094 assert(0);
1095 return src[0];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001096 default:
Brian Paul6947f852009-01-23 17:32:09 -07001097 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001098 return src[0];
1099 }
1100}
1101
Keith Whitwell15e75e02005-04-29 15:11:39 +00001102
Brian Paul22db5352005-11-19 23:45:10 +00001103/**
1104 * Generate instructions for one texture unit's env/combiner mode.
1105 */
1106static struct ureg
1107emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001108{
Brian Paula5e70032009-08-31 11:17:59 -06001109 const struct state_key *key = p->state;
Brian Paul956e6c32009-08-31 11:14:16 -06001110 GLboolean saturate;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001111 GLuint rgb_shift, alpha_shift;
Brian Paula5e70032009-08-31 11:17:59 -06001112 struct ureg out, dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001113
Keith Whitwellce721142005-07-11 10:10:38 +00001114 if (!key->unit[unit].enabled) {
1115 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001116 }
Roland Scheidegger114152e2009-03-12 15:01:16 +01001117 if (key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1118 /* this isn't really a env stage delivering a color and handled elsewhere */
1119 return get_source(p, SRC_PREVIOUS, 0);
1120 }
Keith Whitwellce721142005-07-11 10:10:38 +00001121
1122 switch (key->unit[unit].ModeRGB) {
1123 case MODE_DOT3_RGB_EXT:
1124 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001125 rgb_shift = 0;
1126 break;
Keith Whitwellce721142005-07-11 10:10:38 +00001127 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001128 alpha_shift = 0;
1129 rgb_shift = 0;
1130 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001131 default:
Keith Whitwellce721142005-07-11 10:10:38 +00001132 rgb_shift = key->unit[unit].ScaleShiftRGB;
1133 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001134 break;
1135 }
Keith Whitwellce721142005-07-11 10:10:38 +00001136
Brian Paul956e6c32009-08-31 11:14:16 -06001137 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
1138 * We don't want to clamp twice.
1139 */
1140 saturate = !(rgb_shift || alpha_shift);
1141
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001142 /* If this is the very last calculation, emit direct to output reg:
1143 */
Keith Whitwellce721142005-07-11 10:10:38 +00001144 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001145 unit != p->last_tex_stage ||
1146 alpha_shift ||
1147 rgb_shift)
1148 dest = get_temp( p );
1149 else
Brian Paul8d475822009-02-28 11:49:46 -07001150 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLOR);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001151
1152 /* Emit the RGB and A combine ops
1153 */
Keith Whitwellce721142005-07-11 10:10:38 +00001154 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
1155 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001156 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1157 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001158 key->unit[unit].NumArgsRGB,
1159 key->unit[unit].ModeRGB,
1160 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001161 }
Keith Whitwellce721142005-07-11 10:10:38 +00001162 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +02001163 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001164 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1165 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001166 key->unit[unit].NumArgsRGB,
1167 key->unit[unit].ModeRGB,
1168 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001169 }
1170 else {
1171 /* Need to do something to stop from re-emitting identical
1172 * argument calculations here:
1173 */
1174 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
1175 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001176 key->unit[unit].NumArgsRGB,
1177 key->unit[unit].ModeRGB,
1178 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001179 out = emit_combine( p, dest, WRITEMASK_W, saturate,
1180 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001181 key->unit[unit].NumArgsA,
1182 key->unit[unit].ModeA,
1183 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001184 }
1185
1186 /* Deal with the final shift:
1187 */
1188 if (alpha_shift || rgb_shift) {
Brian Paula5e70032009-08-31 11:17:59 -06001189 struct ureg shift;
1190
Brian Paul956e6c32009-08-31 11:14:16 -06001191 saturate = GL_TRUE; /* always saturate at this point */
Brian Paula5e70032009-08-31 11:17:59 -06001192
Keith Whitwell15e75e02005-04-29 15:11:39 +00001193 if (rgb_shift == alpha_shift) {
José Fonseca452a5922008-05-31 18:14:09 +09001194 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001195 }
1196 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001197 shift = register_const4f(p,
José Fonseca452a5922008-05-31 18:14:09 +09001198 (GLfloat)(1<<rgb_shift),
1199 (GLfloat)(1<<rgb_shift),
1200 (GLfloat)(1<<rgb_shift),
1201 (GLfloat)(1<<alpha_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001202 }
Brian Paul7e807512005-11-05 17:10:45 +00001203 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001204 saturate, out, shift, undef );
1205 }
1206 else
1207 return out;
1208}
1209
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001210
Brian Paul22db5352005-11-19 23:45:10 +00001211/**
1212 * Generate instruction for getting a texture source term.
1213 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001214static void load_texture( struct texenv_fragment_program *p, GLuint unit )
1215{
1216 if (is_undef(p->src_texture[unit])) {
Brian Paulb5ec0a62009-09-01 15:51:23 -06001217 const GLuint texTarget = p->state->unit[unit].source_index;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001218 struct ureg texcoord;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001219 struct ureg tmp = get_tex_temp( p );
1220
Roland Scheidegger114152e2009-03-12 15:01:16 +01001221 if (is_undef(p->texcoord_tex[unit])) {
1222 texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
1223 }
1224 else {
1225 /* might want to reuse this reg for tex output actually */
1226 texcoord = p->texcoord_tex[unit];
1227 }
1228
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001229 /* TODO: Use D0_MASK_XY where possible.
1230 */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +02001231 if (p->state->unit[unit].enabled) {
Brian Paul44e018c2009-02-20 13:42:08 -07001232 GLboolean shadow = GL_FALSE;
1233
1234 if (p->state->unit[unit].shadow) {
1235 p->program->Base.ShadowSamplers |= 1 << unit;
1236 shadow = GL_TRUE;
1237 }
1238
Aapo Tahkolab8915342006-03-28 10:26:34 +00001239 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1240 tmp, WRITEMASK_XYZW,
Brian Paul44e018c2009-02-20 13:42:08 -07001241 unit, texTarget, shadow,
1242 texcoord );
Brian9b7e5a52007-12-14 11:16:49 -07001243
1244 p->program->Base.SamplersUsed |= (1 << unit);
Brian1fe385f2007-12-14 11:42:28 -07001245 /* This identity mapping should already be in place
1246 * (see _mesa_init_program_struct()) but let's be safe.
1247 */
1248 p->program->Base.SamplerUnits[unit] = unit;
Brian9b7e5a52007-12-14 11:16:49 -07001249 }
1250 else
Aapo Tahkolab8915342006-03-28 10:26:34 +00001251 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001252 }
1253}
1254
Keith Whitwell241b6b72005-05-23 09:50:34 +00001255static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +00001256 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001257{
1258 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +00001259 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001260 load_texture(p, unit);
1261 break;
1262
Keith Whitwellce721142005-07-11 10:10:38 +00001263 case SRC_TEXTURE0:
1264 case SRC_TEXTURE1:
1265 case SRC_TEXTURE2:
1266 case SRC_TEXTURE3:
1267 case SRC_TEXTURE4:
1268 case SRC_TEXTURE5:
1269 case SRC_TEXTURE6:
1270 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +00001271 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001272 break;
1273
1274 default:
Brian Paul6947f852009-01-23 17:32:09 -07001275 /* not a texture src - do nothing */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001276 break;
1277 }
Keith Whitwell241b6b72005-05-23 09:50:34 +00001278
1279 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001280}
1281
Brian Paul22db5352005-11-19 23:45:10 +00001282
1283/**
1284 * Generate instructions for loading all texture source terms.
1285 */
1286static GLboolean
Brian Paul457e4272009-09-01 16:22:02 -06001287load_texunit_sources( struct texenv_fragment_program *p, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001288{
Brian Paula5e70032009-08-31 11:17:59 -06001289 const struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +00001290 GLuint i;
1291
1292 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001293 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001294 }
Aapo Tahkolab8915342006-03-28 10:26:34 +00001295
1296 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1297 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1298 }
1299
Keith Whitwell241b6b72005-05-23 09:50:34 +00001300 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001301}
1302
Roland Scheidegger114152e2009-03-12 15:01:16 +01001303/**
1304 * Generate instructions for loading bump map textures.
1305 */
1306static GLboolean
Brian Paul457e4272009-09-01 16:22:02 -06001307load_texunit_bumpmap( struct texenv_fragment_program *p, GLuint unit )
Roland Scheidegger114152e2009-03-12 15:01:16 +01001308{
Brian Paula5e70032009-08-31 11:17:59 -06001309 const struct state_key *key = p->state;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001310 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1311 struct ureg texcDst, bumpMapRes;
1312 struct ureg constdudvcolor = register_const4f(p, 0.0, 0.0, 0.0, 1.0);
1313 struct ureg texcSrc = register_input(p, FRAG_ATTRIB_TEX0 + bumpedUnitNr);
1314 struct ureg rotMat0 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_0, unit );
1315 struct ureg rotMat1 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_1, unit );
1316
1317 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1318
1319 bumpMapRes = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1320 texcDst = get_tex_temp( p );
1321 p->texcoord_tex[bumpedUnitNr] = texcDst;
1322
Brian Paul457e4272009-09-01 16:22:02 -06001323 /* Apply rot matrix and add coords to be available in next phase.
1324 * dest = (Arg0.xxxx * rotMat0 + Arg1) + (Arg0.yyyy * rotMat1)
1325 * note only 2 coords are affected the rest are left unchanged (mul by 0)
1326 */
Roland Scheidegger114152e2009-03-12 15:01:16 +01001327 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1328 swizzle1(bumpMapRes, SWIZZLE_X), rotMat0, texcSrc );
1329 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1330 swizzle1(bumpMapRes, SWIZZLE_Y), rotMat1, texcDst );
1331
Brian Paul457e4272009-09-01 16:22:02 -06001332 /* Move 0,0,0,1 into bumpmap src if someone (crossbar) is foolish
1333 * enough to access this later, should optimize away.
1334 */
1335 emit_arith( p, OPCODE_MOV, bumpMapRes, WRITEMASK_XYZW, 0,
1336 constdudvcolor, undef, undef );
Roland Scheidegger114152e2009-03-12 15:01:16 +01001337
1338 return GL_TRUE;
1339}
Brian Paul22db5352005-11-19 23:45:10 +00001340
1341/**
1342 * Generate a new fragment program which implements the context's
1343 * current texture env/combine mode.
1344 */
1345static void
Brian Paule998c342006-10-29 21:17:18 +00001346create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001347 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001348{
Brian Paule998c342006-10-29 21:17:18 +00001349 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001350 struct texenv_fragment_program p;
1351 GLuint unit;
1352 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001353
Keith Whitwelle4902422005-05-11 15:16:35 +00001354 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwellce721142005-07-11 10:10:38 +00001355 p.state = key;
1356 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001357
Brian Paule998c342006-10-29 21:17:18 +00001358 /* During code generation, use locally-allocated instruction buffer,
1359 * then alloc dynamic storage below.
1360 */
1361 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001362 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Brian Paul457e4272009-09-01 16:22:02 -06001363 p.program->Base.String = NULL;
1364 p.program->Base.NumTexIndirections = 1; /* is this right? */
Brian21f99792007-01-09 11:00:21 -07001365 p.program->Base.NumTexInstructions = 0;
1366 p.program->Base.NumAluInstructions = 0;
Brian Paul457e4272009-09-01 16:22:02 -06001367 p.program->Base.NumInstructions = 0;
1368 p.program->Base.NumTemporaries = 0;
1369 p.program->Base.NumParameters = 0;
1370 p.program->Base.NumAttributes = 0;
1371 p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001372 p.program->Base.Parameters = _mesa_new_parameter_list();
Brian Paul457e4272009-09-01 16:22:02 -06001373 p.program->Base.InputsRead = 0x0;
Brian Paul8d475822009-02-28 11:49:46 -07001374 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLOR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001375
Roland Scheidegger114152e2009-03-12 15:01:16 +01001376 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001377 p.src_texture[unit] = undef;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001378 p.texcoord_tex[unit] = undef;
1379 }
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001380
Keith Whitwell47b29f52005-05-04 11:44:44 +00001381 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001382 p.half = undef;
1383 p.zero = undef;
1384 p.one = undef;
1385
Keith Whitwell15e75e02005-04-29 15:11:39 +00001386 p.last_tex_stage = 0;
Brian93fef222007-10-29 12:25:46 -06001387 release_temps(ctx, &p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001388
Keith Whitwellce721142005-07-11 10:10:38 +00001389 if (key->enabled_units) {
Brian Paul457e4272009-09-01 16:22:02 -06001390 GLboolean needbumpstage = GL_FALSE;
1391
Roland Scheidegger114152e2009-03-12 15:01:16 +01001392 /* Zeroth pass - bump map textures first */
Brian Paul457e4272009-09-01 16:22:02 -06001393 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++)
1394 if (key->unit[unit].enabled &&
1395 key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001396 needbumpstage = GL_TRUE;
1397 load_texunit_bumpmap( &p, unit );
1398 }
1399 if (needbumpstage)
1400 p.program->Base.NumTexIndirections++;
1401
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001402 /* First pass - to support texture_env_crossbar, first identify
1403 * all referenced texture sources and emit texld instructions
1404 * for each:
1405 */
Brian Paul457e4272009-09-01 16:22:02 -06001406 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001407 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001408 load_texunit_sources( &p, unit );
1409 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001410 }
1411
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001412 /* Second pass - emit combine instructions to build final color:
1413 */
Brian Paul457e4272009-09-01 16:22:02 -06001414 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++)
Brian Paul84d6bed2009-09-01 16:10:57 -06001415 if (key->unit[unit].enabled) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001416 p.src_previous = emit_texenv( &p, unit );
Brian Paul5620c202008-09-26 11:18:06 -06001417 reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
Brian93fef222007-10-29 12:25:46 -06001418 release_temps(ctx, &p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001419 }
1420 }
1421
Keith Whitwellce721142005-07-11 10:10:38 +00001422 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul8d475822009-02-28 11:49:46 -07001423 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLOR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001424
Keith Whitwellce721142005-07-11 10:10:38 +00001425 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001426 /* Emit specular add.
1427 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001428 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001429 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1430 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001431 }
Brian Paulaa206952005-09-16 18:14:24 +00001432 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001433 /* Will wind up in here if no texture enabled or a couple of
1434 * other scenarios (GL_REPLACE for instance).
1435 */
Brian Paul7e807512005-11-05 17:10:45 +00001436 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001437 }
1438
Keith Whitwell47b29f52005-05-04 11:44:44 +00001439 /* Finish up:
1440 */
Brian Paul7e807512005-11-05 17:10:45 +00001441 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001442
Keith Whitwellce721142005-07-11 10:10:38 +00001443 if (key->fog_enabled) {
1444 /* Pull fog mode from GLcontext, the value in the state key is
1445 * a reduced value and not what is expected in FogOption
1446 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001447 p.program->FogOption = ctx->Fog.Mode;
Brian Paul457e4272009-09-01 16:22:02 -06001448 p.program->Base.InputsRead |= FRAG_BIT_FOGC;
1449 }
1450 else {
Keith Whitwell276330b2005-05-09 17:58:13 +00001451 p.program->FogOption = GL_NONE;
Brian Paul457e4272009-09-01 16:22:02 -06001452 }
Keith Whitwell47b29f52005-05-04 11:44:44 +00001453
Brian21f99792007-01-09 11:00:21 -07001454 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001455 program_error(&p, "Exceeded max nr indirect texture lookups");
1456
Brian21f99792007-01-09 11:00:21 -07001457 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001458 program_error(&p, "Exceeded max TEX instructions");
1459
Brian21f99792007-01-09 11:00:21 -07001460 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001461 program_error(&p, "Exceeded max ALU instructions");
1462
Brian Paul5d7b49f2005-11-19 23:12:20 +00001463 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001464
Brian Paule998c342006-10-29 21:17:18 +00001465 /* Allocate final instruction array */
Brianc81cce72007-09-25 15:18:51 -06001466 p.program->Base.Instructions
1467 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1468 if (!p.program->Base.Instructions) {
Brian Paule998c342006-10-29 21:17:18 +00001469 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1470 "generating tex env program");
1471 return;
1472 }
Brianc81cce72007-09-25 15:18:51 -06001473 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1474 p.program->Base.NumInstructions);
1475
1476 if (p.program->FogOption) {
1477 _mesa_append_fog_code(ctx, p.program);
1478 p.program->FogOption = GL_NONE;
1479 }
1480
Brian Paule998c342006-10-29 21:17:18 +00001481
Keith Whitwelldbeea252005-05-10 13:57:50 +00001482 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001483 */
Brian Paule998c342006-10-29 21:17:18 +00001484 if (ctx->Driver.ProgramStringNotify) {
1485 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1486 &p.program->Base );
1487 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001488
Brian Paule998c342006-10-29 21:17:18 +00001489 if (DISASSEM) {
1490 _mesa_print_program(&p.program->Base);
1491 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001492 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001493}
1494
Brian Paul5d7b49f2005-11-19 23:12:20 +00001495
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001496/**
1497 * Return a fragment program which implements the current
1498 * fixed-function texture, fog and color-sum operations.
1499 */
1500struct gl_fragment_program *
1501_mesa_get_fixed_func_fragment_program(GLcontext *ctx)
Keith Whitwellce721142005-07-11 10:10:38 +00001502{
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001503 struct gl_fragment_program *prog;
1504 struct state_key key;
1505
Chris Wilsona46e3272009-09-02 05:11:25 -07001506 printf("SIZE OF KEY %d\n", sizeof(key));
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001507 make_state_key(ctx, &key);
1508
1509 prog = (struct gl_fragment_program *)
1510 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1511 &key, sizeof(key));
Keith Whitwellce721142005-07-11 10:10:38 +00001512
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001513 if (!prog) {
1514 prog = (struct gl_fragment_program *)
1515 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1516
1517 create_new_program(ctx, &key, prog);
1518
1519 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1520 &key, sizeof(key), &prog->Base);
Keith Whitwellce721142005-07-11 10:10:38 +00001521 }
1522
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001523 return prog;
Keith Whitwellce721142005-07-11 10:10:38 +00001524}