blob: 414607e22884703cde4a6a9f3acb3b60e2dad711 [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__
Gary Wongd98b9f42009-09-02 10:11:15 -060086 __extension__ GLubyte Source:4; /**< SRC_x */
87 __extension__ GLubyte Operand:3; /**< OPR_x */
Brian Paul83e44702009-09-02 08:45:34 -060088#else
Brian Paul5e809212009-09-02 10:38:46 -060089 GLubyte Source; /**< SRC_x */
90 GLubyte Operand; /**< OPR_x */
Brian Paul83e44702009-09-02 08:45:34 -060091#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
Brian Paul7a7d5872009-09-02 15:39:13 -0600102 /* NOTE: This array of structs must be last! (see "keySize" below) */
Keith Whitwellce721142005-07-11 10:10:38 +0000103 struct {
Brian Paul5d7b49f2005-11-19 23:12:20 +0000104 GLuint enabled:1;
Brian Paul9ed03152009-09-01 15:57:36 -0600105 GLuint source_index:3; /**< TEXTURE_x_INDEX */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +0200106 GLuint shadow:1;
Brian Paul5d7b49f2005-11-19 23:12:20 +0000107 GLuint ScaleShiftRGB:2;
108 GLuint ScaleShiftA:2;
Keith Whitwellce721142005-07-11 10:10:38 +0000109
Brian Paul9ed03152009-09-01 15:57:36 -0600110 GLuint NumArgsRGB:3; /**< up to MAX_COMBINER_TERMS */
Brian Paulf337e2c2009-09-01 15:49:55 -0600111 GLuint ModeRGB:5; /**< MODE_x */
Keith Whitwellce721142005-07-11 10:10:38 +0000112
Brian Paul9ed03152009-09-01 15:57:36 -0600113 GLuint NumArgsA:3; /**< up to MAX_COMBINER_TERMS */
Brian Paulf337e2c2009-09-01 15:49:55 -0600114 GLuint ModeA:5; /**< MODE_x */
Chris Wilsona46e3272009-09-02 05:11:25 -0700115
116 struct mode_opt OptRGB[MAX_COMBINER_TERMS];
Brian Paulbd9b2be2009-03-08 17:58:54 -0600117 struct mode_opt OptA[MAX_COMBINER_TERMS];
Brian Pauld55a28e2009-09-01 15:34:16 -0600118 } unit[MAX_TEXTURE_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000119};
120
121#define FOG_LINEAR 0
122#define FOG_EXP 1
123#define FOG_EXP2 2
124#define FOG_UNKNOWN 3
125
126static GLuint translate_fog_mode( GLenum mode )
127{
128 switch (mode) {
129 case GL_LINEAR: return FOG_LINEAR;
130 case GL_EXP: return FOG_EXP;
131 case GL_EXP2: return FOG_EXP2;
132 default: return FOG_UNKNOWN;
133 }
134}
135
136#define OPR_SRC_COLOR 0
137#define OPR_ONE_MINUS_SRC_COLOR 1
138#define OPR_SRC_ALPHA 2
139#define OPR_ONE_MINUS_SRC_ALPHA 3
140#define OPR_ZERO 4
141#define OPR_ONE 5
142#define OPR_UNKNOWN 7
143
144static GLuint translate_operand( GLenum operand )
145{
146 switch (operand) {
147 case GL_SRC_COLOR: return OPR_SRC_COLOR;
148 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
149 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
150 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
151 case GL_ZERO: return OPR_ZERO;
152 case GL_ONE: return OPR_ONE;
Brian Paul6947f852009-01-23 17:32:09 -0700153 default:
154 assert(0);
155 return OPR_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000156 }
157}
158
159#define SRC_TEXTURE 0
160#define SRC_TEXTURE0 1
161#define SRC_TEXTURE1 2
162#define SRC_TEXTURE2 3
163#define SRC_TEXTURE3 4
164#define SRC_TEXTURE4 5
165#define SRC_TEXTURE5 6
166#define SRC_TEXTURE6 7
167#define SRC_TEXTURE7 8
168#define SRC_CONSTANT 9
169#define SRC_PRIMARY_COLOR 10
170#define SRC_PREVIOUS 11
Brian Paul6947f852009-01-23 17:32:09 -0700171#define SRC_ZERO 12
Keith Whitwellce721142005-07-11 10:10:38 +0000172#define SRC_UNKNOWN 15
173
174static GLuint translate_source( GLenum src )
175{
176 switch (src) {
177 case GL_TEXTURE: return SRC_TEXTURE;
178 case GL_TEXTURE0:
179 case GL_TEXTURE1:
180 case GL_TEXTURE2:
181 case GL_TEXTURE3:
182 case GL_TEXTURE4:
183 case GL_TEXTURE5:
184 case GL_TEXTURE6:
185 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
186 case GL_CONSTANT: return SRC_CONSTANT;
187 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
188 case GL_PREVIOUS: return SRC_PREVIOUS;
Brian Paul6947f852009-01-23 17:32:09 -0700189 case GL_ZERO:
190 return SRC_ZERO;
191 default:
192 assert(0);
193 return SRC_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000194 }
195}
196
Brian Paul6947f852009-01-23 17:32:09 -0700197#define MODE_REPLACE 0 /* r = a0 */
198#define MODE_MODULATE 1 /* r = a0 * a1 */
199#define MODE_ADD 2 /* r = a0 + a1 */
200#define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
201#define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
202#define MODE_SUBTRACT 5 /* r = a0 - a1 */
203#define MODE_DOT3_RGB 6 /* r = a0 . a1 */
204#define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
205#define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
206#define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
207#define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
208#define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
209#define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
210#define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
211#define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
Roland Scheidegger114152e2009-03-12 15:01:16 +0100212#define MODE_BUMP_ENVMAP_ATI 15 /* special */
213#define MODE_UNKNOWN 16
Keith Whitwellce721142005-07-11 10:10:38 +0000214
Brian Paul6947f852009-01-23 17:32:09 -0700215/**
216 * Translate GL combiner state into a MODE_x value
217 */
218static GLuint translate_mode( GLenum envMode, GLenum mode )
Keith Whitwellce721142005-07-11 10:10:38 +0000219{
220 switch (mode) {
221 case GL_REPLACE: return MODE_REPLACE;
222 case GL_MODULATE: return MODE_MODULATE;
Brian Paul6947f852009-01-23 17:32:09 -0700223 case GL_ADD:
224 if (envMode == GL_COMBINE4_NV)
225 return MODE_ADD_PRODUCTS;
226 else
227 return MODE_ADD;
228 case GL_ADD_SIGNED:
229 if (envMode == GL_COMBINE4_NV)
230 return MODE_ADD_PRODUCTS_SIGNED;
231 else
232 return MODE_ADD_SIGNED;
Keith Whitwellce721142005-07-11 10:10:38 +0000233 case GL_INTERPOLATE: return MODE_INTERPOLATE;
234 case GL_SUBTRACT: return MODE_SUBTRACT;
235 case GL_DOT3_RGB: return MODE_DOT3_RGB;
236 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
237 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
238 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
239 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
240 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
241 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
Roland Scheidegger114152e2009-03-12 15:01:16 +0100242 case GL_BUMP_ENVMAP_ATI: return MODE_BUMP_ENVMAP_ATI;
Brian Paul6947f852009-01-23 17:32:09 -0700243 default:
244 assert(0);
245 return MODE_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000246 }
247}
248
Brian Paulf337e2c2009-09-01 15:49:55 -0600249
Brian Paulf337e2c2009-09-01 15:49:55 -0600250/**
Brian Paule9ba9ff2009-09-10 08:41:12 -0600251 * Do we need to clamp the results of the given texture env/combine mode?
252 * If the inputs to the mode are in [0,1] we don't always have to clamp
253 * the results.
254 */
255static GLboolean
256need_saturate( GLuint mode )
257{
258 switch (mode) {
259 case MODE_REPLACE:
260 case MODE_MODULATE:
261 case MODE_INTERPOLATE:
262 return GL_FALSE;
263 case MODE_ADD:
264 case MODE_ADD_SIGNED:
265 case MODE_SUBTRACT:
266 case MODE_DOT3_RGB:
267 case MODE_DOT3_RGB_EXT:
268 case MODE_DOT3_RGBA:
269 case MODE_DOT3_RGBA_EXT:
270 case MODE_MODULATE_ADD_ATI:
271 case MODE_MODULATE_SIGNED_ADD_ATI:
272 case MODE_MODULATE_SUBTRACT_ATI:
273 case MODE_ADD_PRODUCTS:
274 case MODE_ADD_PRODUCTS_SIGNED:
275 case MODE_BUMP_ENVMAP_ATI:
276 return GL_TRUE;
277 default:
278 assert(0);
Brian Paula491e252009-09-23 15:44:37 -0600279 return GL_FALSE;
Brian Paule9ba9ff2009-09-10 08:41:12 -0600280 }
281}
282
283
284
285/**
Brian Paulf337e2c2009-09-01 15:49:55 -0600286 * Translate TEXTURE_x_BIT to TEXTURE_x_INDEX.
287 */
Brian Paule00ac112005-09-15 05:00:45 +0000288static GLuint translate_tex_src_bit( GLbitfield bit )
Keith Whitwellce721142005-07-11 10:10:38 +0000289{
Brian Paulb5ec0a62009-09-01 15:51:23 -0600290 ASSERT(bit);
291 return _mesa_ffs(bit) - 1;
Keith Whitwellce721142005-07-11 10:10:38 +0000292}
293
Brian Paulf337e2c2009-09-01 15:49:55 -0600294
Keith Whitwell1680ef82008-10-03 17:30:59 +0100295#define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
296#define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
297
Brian Paul239617f2008-10-07 11:22:47 -0600298/**
299 * Identify all possible varying inputs. The fragment program will
Keith Whitwell1680ef82008-10-03 17:30:59 +0100300 * never reference non-varying inputs, but will track them via state
301 * constants instead.
302 *
303 * This function figures out all the inputs that the fragment program
304 * has access to. The bitmask is later reduced to just those which
305 * are actually referenced.
306 */
Brian Paul239617f2008-10-07 11:22:47 -0600307static GLbitfield get_fp_input_mask( GLcontext *ctx )
Keith Whitwell1680ef82008-10-03 17:30:59 +0100308{
Eric Anholte5f63c42009-06-02 07:47:20 -0700309 /* _NEW_PROGRAM */
Brian Paulf0b07942008-12-17 14:04:03 -0700310 const GLboolean vertexShader = (ctx->Shader.CurrentProgram &&
Eric Anholt40990d92009-08-03 12:36:52 -0700311 ctx->Shader.CurrentProgram->LinkStatus &&
Brian Paulf0b07942008-12-17 14:04:03 -0700312 ctx->Shader.CurrentProgram->VertexProgram);
313 const GLboolean vertexProgram = ctx->VertexProgram._Enabled;
Brian Paul239617f2008-10-07 11:22:47 -0600314 GLbitfield fp_inputs = 0x0;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100315
Brian Paul6d4d51d2008-10-10 13:39:14 -0600316 if (ctx->VertexProgram._Overriden) {
317 /* Somebody's messing with the vertex program and we don't have
318 * a clue what's happening. Assume that it could be producing
319 * all possible outputs.
320 */
321 fp_inputs = ~0;
322 }
323 else if (ctx->RenderMode == GL_FEEDBACK) {
Eric Anholte5f63c42009-06-02 07:47:20 -0700324 /* _NEW_RENDERMODE */
Brian Paul6d4d51d2008-10-10 13:39:14 -0600325 fp_inputs = (FRAG_BIT_COL0 | FRAG_BIT_TEX0);
326 }
Brian Paulf0b07942008-12-17 14:04:03 -0700327 else if (!(vertexProgram || vertexShader) ||
Brian Pauld7296a12008-12-17 11:29:06 -0700328 !ctx->VertexProgram._Current) {
Brian Paulf0b07942008-12-17 14:04:03 -0700329 /* Fixed function vertex logic */
Eric Anholte5f63c42009-06-02 07:47:20 -0700330 /* _NEW_ARRAY */
Brian Paul239617f2008-10-07 11:22:47 -0600331 GLbitfield varying_inputs = ctx->varying_vp_inputs;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100332
Keith Whitwell97e63432008-10-20 13:03:45 +0100333 /* These get generated in the setup routine regardless of the
334 * vertex program:
335 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700336 /* _NEW_POINT */
Keith Whitwell97e63432008-10-20 13:03:45 +0100337 if (ctx->Point.PointSprite)
338 varying_inputs |= FRAG_BITS_TEX_ANY;
339
Keith Whitwell1680ef82008-10-03 17:30:59 +0100340 /* First look at what values may be computed by the generated
341 * vertex program:
342 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700343 /* _NEW_LIGHT */
Keith Whitwell1680ef82008-10-03 17:30:59 +0100344 if (ctx->Light.Enabled) {
345 fp_inputs |= FRAG_BIT_COL0;
346
Eric Anholt9cea84b2009-07-16 18:41:03 -0700347 if (texenv_doing_secondary_color(ctx))
Keith Whitwell1680ef82008-10-03 17:30:59 +0100348 fp_inputs |= FRAG_BIT_COL1;
349 }
350
Eric Anholte5f63c42009-06-02 07:47:20 -0700351 /* _NEW_TEXTURE */
Keith Whitwell1680ef82008-10-03 17:30:59 +0100352 fp_inputs |= (ctx->Texture._TexGenEnabled |
353 ctx->Texture._TexMatEnabled) << FRAG_ATTRIB_TEX0;
354
355 /* Then look at what might be varying as a result of enabled
356 * arrays, etc:
357 */
Brian Paul6807d962009-08-07 09:42:28 -0600358 if (varying_inputs & VERT_BIT_COLOR0)
359 fp_inputs |= FRAG_BIT_COL0;
360 if (varying_inputs & VERT_BIT_COLOR1)
361 fp_inputs |= FRAG_BIT_COL1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100362
363 fp_inputs |= (((varying_inputs & VERT_BIT_TEX_ANY) >> VERT_ATTRIB_TEX0)
364 << FRAG_ATTRIB_TEX0);
365
366 }
367 else {
368 /* calculate from vp->outputs */
Brian Paul2389c052008-12-17 19:01:34 -0700369 struct gl_vertex_program *vprog;
Ian Romanick5606dfb2009-11-17 16:10:24 -0800370 GLbitfield64 vp_outputs;
Brian Paul2389c052008-12-17 19:01:34 -0700371
372 /* Choose GLSL vertex shader over ARB vertex program. Need this
373 * since vertex shader state validation comes after fragment state
374 * validation (see additional comments in state.c).
375 */
376 if (vertexShader)
377 vprog = ctx->Shader.CurrentProgram->VertexProgram;
378 else
Eric Anholta9ba1bf2009-08-03 12:38:56 -0700379 vprog = ctx->VertexProgram.Current;
Brian Paul2389c052008-12-17 19:01:34 -0700380
381 vp_outputs = vprog->Base.OutputsWritten;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100382
Keith Whitwell97e63432008-10-20 13:03:45 +0100383 /* These get generated in the setup routine regardless of the
384 * vertex program:
385 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700386 /* _NEW_POINT */
Keith Whitwell97e63432008-10-20 13:03:45 +0100387 if (ctx->Point.PointSprite)
388 vp_outputs |= FRAG_BITS_TEX_ANY;
389
Brian Paul6807d962009-08-07 09:42:28 -0600390 if (vp_outputs & (1 << VERT_RESULT_COL0))
391 fp_inputs |= FRAG_BIT_COL0;
392 if (vp_outputs & (1 << VERT_RESULT_COL1))
393 fp_inputs |= FRAG_BIT_COL1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100394
Keith Whitwell0370d6b2008-10-04 12:41:56 +0100395 fp_inputs |= (((vp_outputs & VERT_RESULT_TEX_ANY) >> VERT_RESULT_TEX0)
396 << FRAG_ATTRIB_TEX0);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100397 }
398
399 return fp_inputs;
400}
401
402
Brian Paul22db5352005-11-19 23:45:10 +0000403/**
404 * Examine current texture environment state and generate a unique
405 * key to identify it.
406 */
Brian Paul7a7d5872009-09-02 15:39:13 -0600407static GLuint make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000408{
Keith Whitwellce721142005-07-11 10:10:38 +0000409 GLuint i, j;
Brian Paul239617f2008-10-07 11:22:47 -0600410 GLbitfield inputs_referenced = FRAG_BIT_COL0;
Brian Paulf337e2c2009-09-01 15:49:55 -0600411 const GLbitfield inputs_available = get_fp_input_mask( ctx );
Brian Paul7a7d5872009-09-02 15:39:13 -0600412 GLuint keySize;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100413
Keith Whitwell8065c122006-05-22 16:09:27 +0000414 memset(key, 0, sizeof(*key));
415
Eric Anholte5f63c42009-06-02 07:47:20 -0700416 /* _NEW_TEXTURE */
Brian Paule9b34882008-12-31 11:54:02 -0700417 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000418 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Brian Paulf337e2c2009-09-01 15:49:55 -0600419 const struct gl_texture_object *texObj = texUnit->_Current;
420 const struct gl_tex_env_combine_state *comb = texUnit->_CurrentCombine;
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800421 GLenum format;
Roland Scheideggerbf4a0fa2008-02-15 17:26:06 +0100422
423 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
Keith Whitwellce721142005-07-11 10:10:38 +0000424 continue;
425
Brian Paulf337e2c2009-09-01 15:49:55 -0600426 format = texObj->Image[0][texObj->BaseLevel]->_BaseFormat;
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800427
Keith Whitwellce721142005-07-11 10:10:38 +0000428 key->unit[i].enabled = 1;
429 key->enabled_units |= (1<<i);
Brian Paul7a7d5872009-09-02 15:39:13 -0600430 key->nr_enabled_units = i + 1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100431 inputs_referenced |= FRAG_BIT_TEX(i);
Keith Whitwellce721142005-07-11 10:10:38 +0000432
Brian Paulf337e2c2009-09-01 15:49:55 -0600433 key->unit[i].source_index =
434 translate_tex_src_bit(texUnit->_ReallyEnabled);
435
436 key->unit[i].shadow = ((texObj->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800437 ((format == GL_DEPTH_COMPONENT) ||
438 (format == GL_DEPTH_STENCIL_EXT)));
Keith Whitwellce721142005-07-11 10:10:38 +0000439
Brian Paulf337e2c2009-09-01 15:49:55 -0600440 key->unit[i].NumArgsRGB = comb->_NumArgsRGB;
441 key->unit[i].NumArgsA = comb->_NumArgsA;
Keith Whitwellce721142005-07-11 10:10:38 +0000442
443 key->unit[i].ModeRGB =
Brian Paulf337e2c2009-09-01 15:49:55 -0600444 translate_mode(texUnit->EnvMode, comb->ModeRGB);
Keith Whitwellce721142005-07-11 10:10:38 +0000445 key->unit[i].ModeA =
Brian Paulf337e2c2009-09-01 15:49:55 -0600446 translate_mode(texUnit->EnvMode, comb->ModeA);
Roland Scheidegger114152e2009-03-12 15:01:16 +0100447
Brian Paulf337e2c2009-09-01 15:49:55 -0600448 key->unit[i].ScaleShiftRGB = comb->ScaleShiftRGB;
449 key->unit[i].ScaleShiftA = comb->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000450
Brian Paulbd9b2be2009-03-08 17:58:54 -0600451 for (j = 0; j < MAX_COMBINER_TERMS; j++) {
Brian Paulf337e2c2009-09-01 15:49:55 -0600452 key->unit[i].OptRGB[j].Operand = translate_operand(comb->OperandRGB[j]);
453 key->unit[i].OptA[j].Operand = translate_operand(comb->OperandA[j]);
454 key->unit[i].OptRGB[j].Source = translate_source(comb->SourceRGB[j]);
455 key->unit[i].OptA[j].Source = translate_source(comb->SourceA[j]);
Keith Whitwellce721142005-07-11 10:10:38 +0000456 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100457
458 if (key->unit[i].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
459 /* requires some special translation */
460 key->unit[i].NumArgsRGB = 2;
461 key->unit[i].ScaleShiftRGB = 0;
462 key->unit[i].OptRGB[0].Operand = OPR_SRC_COLOR;
463 key->unit[i].OptRGB[0].Source = SRC_TEXTURE;
464 key->unit[i].OptRGB[1].Operand = OPR_SRC_COLOR;
465 key->unit[i].OptRGB[1].Source = texUnit->BumpTarget - GL_TEXTURE0 + SRC_TEXTURE0;
466 }
Keith Whitwellce721142005-07-11 10:10:38 +0000467 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100468
Eric Anholt9cea84b2009-07-16 18:41:03 -0700469 /* _NEW_LIGHT | _NEW_FOG */
470 if (texenv_doing_secondary_color(ctx)) {
Keith Whitwellce721142005-07-11 10:10:38 +0000471 key->separate_specular = 1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100472 inputs_referenced |= FRAG_BIT_COL1;
473 }
Keith Whitwellce721142005-07-11 10:10:38 +0000474
Eric Anholte5f63c42009-06-02 07:47:20 -0700475 /* _NEW_FOG */
Keith Whitwellce721142005-07-11 10:10:38 +0000476 if (ctx->Fog.Enabled) {
477 key->fog_enabled = 1;
478 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100479 inputs_referenced |= FRAG_BIT_FOGC; /* maybe */
Keith Whitwellce721142005-07-11 10:10:38 +0000480 }
Keith Whitwell1680ef82008-10-03 17:30:59 +0100481
482 key->inputs_available = (inputs_available & inputs_referenced);
Brian Paul7a7d5872009-09-02 15:39:13 -0600483
484 /* compute size of state key, ignoring unused texture units */
485 keySize = sizeof(*key) - sizeof(key->unit)
486 + key->nr_enabled_units * sizeof(key->unit[0]);
487
488 return keySize;
Keith Whitwellce721142005-07-11 10:10:38 +0000489}
490
Brian Paul7a7d5872009-09-02 15:39:13 -0600491
Brian Paul239617f2008-10-07 11:22:47 -0600492/**
493 * Use uregs to represent registers internally, translate to Mesa's
Keith Whitwell15e75e02005-04-29 15:11:39 +0000494 * expected formats on emit.
495 *
496 * NOTE: These are passed by value extensively in this file rather
497 * than as usual by pointer reference. If this disturbs you, try
498 * remembering they are just 32bits in size.
499 *
500 * GCC is smart enough to deal with these dword-sized structures in
501 * much the same way as if I had defined them as dwords and was using
502 * macros to access and set the fields. This is much nicer and easier
503 * to evolve.
504 */
505struct ureg {
506 GLuint file:4;
507 GLuint idx:8;
508 GLuint negatebase:1;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000509 GLuint swz:12;
Brian Paul1480bca2009-09-01 16:01:12 -0600510 GLuint pad:7;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000511};
512
Brian Paul13abf912006-04-13 19:17:13 +0000513static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000514 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000515 ~0,
516 0,
517 0,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000518 0
519};
520
Keith Whitwell15e75e02005-04-29 15:11:39 +0000521
Brian Paul239617f2008-10-07 11:22:47 -0600522/** State used to build the fragment program:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000523 */
524struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000525 struct gl_fragment_program *program;
Keith Whitwellce721142005-07-11 10:10:38 +0000526 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000527
Brian Paul239617f2008-10-07 11:22:47 -0600528 GLbitfield alu_temps; /**< Track texture indirections, see spec. */
529 GLbitfield temps_output; /**< Track texture indirections, see spec. */
530 GLbitfield temp_in_use; /**< Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000531 GLboolean error;
532
Brian Paule9b34882008-12-31 11:54:02 -0700533 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000534 /* Reg containing each texture unit's sampled texture color,
535 * else undef.
536 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000537
Roland Scheidegger114152e2009-03-12 15:01:16 +0100538 struct ureg texcoord_tex[MAX_TEXTURE_COORD_UNITS];
539 /* Reg containing texcoord for a texture unit,
540 * needed for bump mapping, else undef.
541 */
542
Brian Paul239617f2008-10-07 11:22:47 -0600543 struct ureg src_previous; /**< Reg containing color from previous
Keith Whitwell15e75e02005-04-29 15:11:39 +0000544 * stage. May need to be decl'd.
545 */
546
Brian Paul239617f2008-10-07 11:22:47 -0600547 GLuint last_tex_stage; /**< Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000548
549 struct ureg half;
550 struct ureg one;
551 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000552};
553
554
555
556static struct ureg make_ureg(GLuint file, GLuint idx)
557{
558 struct ureg reg;
559 reg.file = file;
560 reg.idx = idx;
561 reg.negatebase = 0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000562 reg.swz = SWIZZLE_NOOP;
563 reg.pad = 0;
564 return reg;
565}
566
567static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
568{
569 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
570 GET_SWZ(reg.swz, y),
571 GET_SWZ(reg.swz, z),
572 GET_SWZ(reg.swz, w));
573
574 return reg;
575}
576
577static struct ureg swizzle1( struct ureg reg, int x )
578{
579 return swizzle(reg, x, x, x, x);
580}
581
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000582static struct ureg negate( struct ureg reg )
583{
584 reg.negatebase ^= 1;
585 return reg;
586}
587
Keith Whitwell15e75e02005-04-29 15:11:39 +0000588static GLboolean is_undef( struct ureg reg )
589{
Alan Hourihane8d972652006-08-10 13:12:00 +0000590 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000591}
592
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000593
Keith Whitwell15e75e02005-04-29 15:11:39 +0000594static struct ureg get_temp( struct texenv_fragment_program *p )
595{
Brian Paul13abf912006-04-13 19:17:13 +0000596 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000597
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000598 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000599 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000600 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000601
602 /* Then any unused temporary:
603 */
604 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000605 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000606
Keith Whitwell15e75e02005-04-29 15:11:39 +0000607 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000608 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Eric Anholt1a80fe42009-12-22 10:56:34 -0800609 exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000610 }
611
Brian Paul13abf912006-04-13 19:17:13 +0000612 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000613 p->program->Base.NumTemporaries = bit;
614
Keith Whitwell93cd9232005-05-11 08:30:23 +0000615 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000616 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
617}
618
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000619static struct ureg get_tex_temp( struct texenv_fragment_program *p )
620{
621 int bit;
622
Brian Pauld01269a2008-09-25 18:27:22 -0600623 /* First try to find available temp not previously used (to avoid
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000624 * starting a new texture indirection). According to the spec, the
625 * ~p->temps_output isn't necessary, but will keep it there for
626 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000627 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000628 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000629
630 /* Then any unused temporary:
631 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000632 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000633 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000634
635 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000636 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Eric Anholt1a80fe42009-12-22 10:56:34 -0800637 exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000638 }
639
Brian Paul13abf912006-04-13 19:17:13 +0000640 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000641 p->program->Base.NumTemporaries = bit;
642
Keith Whitwell93cd9232005-05-11 08:30:23 +0000643 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000644 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
645}
646
Keith Whitwell15e75e02005-04-29 15:11:39 +0000647
Brian Paul5620c202008-09-26 11:18:06 -0600648/** Mark a temp reg as being no longer allocatable. */
649static void reserve_temp( struct texenv_fragment_program *p, struct ureg r )
650{
651 if (r.file == PROGRAM_TEMPORARY)
652 p->temps_output |= (1 << r.idx);
653}
654
655
Brian93fef222007-10-29 12:25:46 -0600656static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000657{
Brian93fef222007-10-29 12:25:46 -0600658 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000659
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000660 /* KW: To support tex_env_crossbar, don't release the registers in
661 * temps_output.
662 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000663 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000664 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000665 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000666 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000667}
668
669
Brian9fe3e2e2007-02-23 11:44:14 -0700670static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000671 GLint s0,
672 GLint s1,
673 GLint s2,
674 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700675 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000676{
Brian9fe3e2e2007-02-23 11:44:14 -0700677 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000678 GLuint idx;
679 tokens[0] = s0;
680 tokens[1] = s1;
681 tokens[2] = s2;
682 tokens[3] = s3;
683 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000684 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000685 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000686}
687
Keith Whitwell47b29f52005-05-04 11:44:44 +0000688
Brian9fe3e2e2007-02-23 11:44:14 -0700689#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
690#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
691#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
692#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000693
Keith Whitwell1680ef82008-10-03 17:30:59 +0100694static GLuint frag_to_vert_attrib( GLuint attrib )
695{
696 switch (attrib) {
697 case FRAG_ATTRIB_COL0: return VERT_ATTRIB_COLOR0;
698 case FRAG_ATTRIB_COL1: return VERT_ATTRIB_COLOR1;
699 default:
700 assert(attrib >= FRAG_ATTRIB_TEX0);
701 assert(attrib <= FRAG_ATTRIB_TEX7);
702 return attrib - FRAG_ATTRIB_TEX0 + VERT_ATTRIB_TEX0;
703 }
704}
705
Keith Whitwell47b29f52005-05-04 11:44:44 +0000706
707static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
708{
Keith Whitwell1680ef82008-10-03 17:30:59 +0100709 if (p->state->inputs_available & (1<<input)) {
710 p->program->Base.InputsRead |= (1 << input);
711 return make_ureg(PROGRAM_INPUT, input);
712 }
713 else {
714 GLuint idx = frag_to_vert_attrib( input );
715 return register_param3( p, STATE_INTERNAL, STATE_CURRENT_ATTRIB, idx );
716 }
Keith Whitwell47b29f52005-05-04 11:44:44 +0000717}
718
719
Brian Paul7e807512005-11-05 17:10:45 +0000720static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000721 struct ureg ureg )
722{
723 reg->File = ureg.file;
724 reg->Index = ureg.idx;
725 reg->Swizzle = ureg.swz;
Brian Paul7db7ff82009-04-14 22:14:30 -0600726 reg->Negate = ureg.negatebase ? NEGATE_XYZW : NEGATE_NONE;
Brian Paul1480bca2009-09-01 16:01:12 -0600727 reg->Abs = GL_FALSE;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000728}
729
Brian Paul7e807512005-11-05 17:10:45 +0000730static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000731 struct ureg ureg, GLuint mask )
732{
733 dst->File = ureg.file;
734 dst->Index = ureg.idx;
735 dst->WriteMask = mask;
Brianc9d495c2007-10-23 10:55:24 -0600736 dst->CondMask = COND_TR; /* always pass cond test */
737 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000738}
739
Brian Paul7e807512005-11-05 17:10:45 +0000740static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000741emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000742 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000743 struct ureg dest,
744 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000745 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000746 struct ureg src0,
747 struct ureg src1,
748 struct ureg src2 )
749{
Brian Paul9ed03152009-09-01 15:57:36 -0600750 const GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000751 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Brian2a8e9bb2007-10-23 10:24:53 -0600752
753 assert(nr < MAX_INSTRUCTIONS);
754
Brian Pauld6272e02006-10-29 18:03:16 +0000755 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000756 inst->Opcode = op;
757
Keith Whitwell47b29f52005-05-04 11:44:44 +0000758 emit_arg( &inst->SrcReg[0], src0 );
759 emit_arg( &inst->SrcReg[1], src1 );
760 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000761
Brian Paule31ac052005-11-20 17:52:40 +0000762 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000763
764 emit_dst( &inst->DstReg, dest, mask );
765
Brian Paul5620c202008-09-26 11:18:06 -0600766#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000767 /* Accounting for indirection tracking:
768 */
769 if (dest.file == PROGRAM_TEMPORARY)
770 p->temps_output |= 1 << dest.idx;
Brian Paul5620c202008-09-26 11:18:06 -0600771#endif
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000772
Keith Whitwell15e75e02005-04-29 15:11:39 +0000773 return inst;
774}
775
776
777static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000778 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000779 struct ureg dest,
780 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000781 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000782 struct ureg src0,
783 struct ureg src1,
784 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000785{
786 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
787
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000788 /* Accounting for indirection tracking:
789 */
790 if (src0.file == PROGRAM_TEMPORARY)
791 p->alu_temps |= 1 << src0.idx;
792
793 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
794 p->alu_temps |= 1 << src1.idx;
795
796 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
797 p->alu_temps |= 1 << src2.idx;
798
799 if (dest.file == PROGRAM_TEMPORARY)
800 p->alu_temps |= 1 << dest.idx;
801
Brian21f99792007-01-09 11:00:21 -0700802 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000803 return dest;
804}
805
806static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000807 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000808 struct ureg dest,
809 GLuint destmask,
810 GLuint tex_unit,
811 GLuint tex_idx,
Brian Paul44e018c2009-02-20 13:42:08 -0700812 GLuint tex_shadow,
Keith Whitwellce721142005-07-11 10:10:38 +0000813 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000814{
Brian Paul7e807512005-11-05 17:10:45 +0000815 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000816 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000817 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000818 coord, /* arg 0? */
819 undef,
820 undef);
821
Brian Paul7e807512005-11-05 17:10:45 +0000822 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000823 inst->TexSrcUnit = tex_unit;
Brian Paul44e018c2009-02-20 13:42:08 -0700824 inst->TexShadow = tex_shadow;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000825
Brian21f99792007-01-09 11:00:21 -0700826 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000827
Brian Paul5620c202008-09-26 11:18:06 -0600828 /* Accounting for indirection tracking:
829 */
830 reserve_temp(p, dest);
831
Roland Scheidegger114152e2009-03-12 15:01:16 +0100832#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000833 /* Is this a texture indirection?
834 */
835 if ((coord.file == PROGRAM_TEMPORARY &&
836 (p->temps_output & (1<<coord.idx))) ||
837 (dest.file == PROGRAM_TEMPORARY &&
838 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700839 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000840 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000841 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000842 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000843 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100844#endif
Keith Whitwell15e75e02005-04-29 15:11:39 +0000845
846 return dest;
847}
848
849
Keith Whitwell47b29f52005-05-04 11:44:44 +0000850static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000851 GLfloat s0,
852 GLfloat s1,
853 GLfloat s2,
854 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000855{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000856 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700857 GLuint idx, swizzle;
Brian Pauld01269a2008-09-25 18:27:22 -0600858 struct ureg r;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000859 values[0] = s0;
860 values[1] = s1;
861 values[2] = s2;
862 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700863 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
864 &swizzle );
Brian Pauld01269a2008-09-25 18:27:22 -0600865 r = make_ureg(PROGRAM_CONSTANT, idx);
866 r.swz = swizzle;
867 return r;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000868}
869
Keith Whitwelle4902422005-05-11 15:16:35 +0000870#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000871#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
872#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
873#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000874
875
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000876static struct ureg get_one( struct texenv_fragment_program *p )
877{
878 if (is_undef(p->one))
879 p->one = register_scalar_const(p, 1.0);
880 return p->one;
881}
882
883static struct ureg get_half( struct texenv_fragment_program *p )
884{
885 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000886 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000887 return p->half;
888}
889
890static struct ureg get_zero( struct texenv_fragment_program *p )
891{
892 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000893 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000894 return p->zero;
895}
896
Keith Whitwell15e75e02005-04-29 15:11:39 +0000897
Keith Whitwell15e75e02005-04-29 15:11:39 +0000898static void program_error( struct texenv_fragment_program *p, const char *msg )
899{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000900 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000901 p->error = 1;
902}
903
Keith Whitwell15e75e02005-04-29 15:11:39 +0000904static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000905 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000906{
907 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000908 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000909 assert(!is_undef(p->src_texture[unit]));
910 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000911
Keith Whitwellce721142005-07-11 10:10:38 +0000912 case SRC_TEXTURE0:
913 case SRC_TEXTURE1:
914 case SRC_TEXTURE2:
915 case SRC_TEXTURE3:
916 case SRC_TEXTURE4:
917 case SRC_TEXTURE5:
918 case SRC_TEXTURE6:
919 case SRC_TEXTURE7:
920 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
921 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000922
Keith Whitwellce721142005-07-11 10:10:38 +0000923 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000924 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000925
Keith Whitwellce721142005-07-11 10:10:38 +0000926 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000927 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000928
Brian Paul6947f852009-01-23 17:32:09 -0700929 case SRC_ZERO:
930 return get_zero(p);
931
Keith Whitwellce721142005-07-11 10:10:38 +0000932 case SRC_PREVIOUS:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000933 if (is_undef(p->src_previous))
934 return register_input(p, FRAG_ATTRIB_COL0);
935 else
936 return p->src_previous;
Brian Paul6947f852009-01-23 17:32:09 -0700937
938 default:
939 assert(0);
José Fonsecac6af9b22009-06-15 19:20:25 +0100940 return undef;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000941 }
942}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000943
Keith Whitwell15e75e02005-04-29 15:11:39 +0000944static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000945 GLuint mask,
946 GLuint unit,
947 GLuint source,
948 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000949{
950 struct ureg arg, src, one;
951
952 src = get_source(p, source, unit);
953
954 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000955 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000956 /* Get unused tmp,
957 * Emit tmp = 1.0 - arg.xyzw
958 */
959 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000960 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000961 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000962
Keith Whitwellce721142005-07-11 10:10:38 +0000963 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000964 if (mask == WRITEMASK_W)
965 return src;
966 else
Brian Paul22db5352005-11-19 23:45:10 +0000967 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000968 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000969 /* Get unused tmp,
970 * Emit tmp = 1.0 - arg.wwww
971 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000972 arg = get_temp(p);
973 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000974 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000975 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000976 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000977 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000978 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000979 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000980 case OPR_SRC_COLOR:
Brian Paul6947f852009-01-23 17:32:09 -0700981 return src;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000982 default:
Brian Paul6947f852009-01-23 17:32:09 -0700983 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000984 return src;
985 }
986}
987
Brian Paulf337e2c2009-09-01 15:49:55 -0600988/**
989 * Check if the RGB and Alpha sources and operands match for the given
990 * texture unit's combinder state. When the RGB and A sources and
991 * operands match, we can emit fewer instructions.
992 */
Brian Paula5e70032009-08-31 11:17:59 -0600993static GLboolean args_match( const struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000994{
Brian Paul9ed03152009-09-01 15:57:36 -0600995 GLuint i, numArgs = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000996
Brian Paul457e4272009-09-01 16:22:02 -0600997 for (i = 0; i < numArgs; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000998 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000999 return GL_FALSE;
1000
Brian Paul9ed03152009-09-01 15:57:36 -06001001 switch (key->unit[unit].OptA[i].Operand) {
Keith Whitwellce721142005-07-11 10:10:38 +00001002 case OPR_SRC_ALPHA:
Brian Paul9ed03152009-09-01 15:57:36 -06001003 switch (key->unit[unit].OptRGB[i].Operand) {
Keith Whitwellce721142005-07-11 10:10:38 +00001004 case OPR_SRC_COLOR:
1005 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001006 break;
1007 default:
1008 return GL_FALSE;
1009 }
1010 break;
Keith Whitwellce721142005-07-11 10:10:38 +00001011 case OPR_ONE_MINUS_SRC_ALPHA:
Brian Paul9ed03152009-09-01 15:57:36 -06001012 switch (key->unit[unit].OptRGB[i].Operand) {
Keith Whitwellce721142005-07-11 10:10:38 +00001013 case OPR_ONE_MINUS_SRC_COLOR:
1014 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001015 break;
1016 default:
1017 return GL_FALSE;
1018 }
1019 break;
1020 default:
1021 return GL_FALSE; /* impossible */
1022 }
1023 }
1024
1025 return GL_TRUE;
1026}
1027
Keith Whitwell15e75e02005-04-29 15:11:39 +00001028static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +00001029 struct ureg dest,
1030 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +00001031 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +00001032 GLuint unit,
1033 GLuint nr,
1034 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +00001035 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001036{
Brian Paulbd9b2be2009-03-08 17:58:54 -06001037 struct ureg src[MAX_COMBINER_TERMS];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001038 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +00001039 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001040
Brian Paulbd9b2be2009-03-08 17:58:54 -06001041 assert(nr <= MAX_COMBINER_TERMS);
Brian Paul6947f852009-01-23 17:32:09 -07001042
Keith Whitwell15e75e02005-04-29 15:11:39 +00001043 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +00001044 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001045
1046 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +00001047 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001048 if (mask == WRITEMASK_XYZW && !saturate)
1049 return src[0];
1050 else
Brian Paul7e807512005-11-05 17:10:45 +00001051 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001052 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +00001053 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001054 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001055 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00001056 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001057 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001058 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001059 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001060 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +00001061 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001062 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +00001063 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +00001064 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
1065 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001066 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +00001067 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001068 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
1069 */
Brian Paul7e807512005-11-05 17:10:45 +00001070 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001071
Keith Whitwellce721142005-07-11 10:10:38 +00001072 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +00001073 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001074
Keith Whitwellce721142005-07-11 10:10:38 +00001075 case MODE_DOT3_RGBA:
1076 case MODE_DOT3_RGBA_EXT:
1077 case MODE_DOT3_RGB_EXT:
1078 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001079 struct ureg tmp0 = get_temp( p );
1080 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +00001081 struct ureg neg1 = register_scalar_const(p, -1);
1082 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001083
1084 /* tmp0 = 2*src0 - 1
1085 * tmp1 = 2*src1 - 1
1086 *
1087 * dst = tmp0 dot3 tmp1
1088 */
Brian Paul7e807512005-11-05 17:10:45 +00001089 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001090 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001091
Brian Paulaa206952005-09-16 18:14:24 +00001092 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001093 tmp1 = tmp0;
1094 else
Brian Paul7e807512005-11-05 17:10:45 +00001095 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001096 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +00001097 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001098 return dest;
1099 }
Keith Whitwellce721142005-07-11 10:10:38 +00001100 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001101 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001102 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001103 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +00001104 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001105 /* Arg0 * Arg2 + Arg1 - 0.5 */
1106 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001107 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +00001108 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
1109 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001110 return dest;
1111 }
Keith Whitwellce721142005-07-11 10:10:38 +00001112 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001113 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001114 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001115 return dest;
Brian Paul6947f852009-01-23 17:32:09 -07001116 case MODE_ADD_PRODUCTS:
1117 /* Arg0 * Arg1 + Arg2 * Arg3 */
1118 {
1119 struct ureg tmp0 = get_temp(p);
1120 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1121 emit_arith( p, OPCODE_MAD, dest, mask, saturate, src[2], src[3], tmp0 );
1122 }
1123 return dest;
1124 case MODE_ADD_PRODUCTS_SIGNED:
1125 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1126 {
1127 struct ureg tmp0 = get_temp(p);
1128 half = get_half(p);
1129 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1130 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[2], src[3], tmp0 );
1131 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1132 }
1133 return dest;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001134 case MODE_BUMP_ENVMAP_ATI:
1135 /* special - not handled here */
1136 assert(0);
1137 return src[0];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001138 default:
Brian Paul6947f852009-01-23 17:32:09 -07001139 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001140 return src[0];
1141 }
1142}
1143
Keith Whitwell15e75e02005-04-29 15:11:39 +00001144
Brian Paul22db5352005-11-19 23:45:10 +00001145/**
1146 * Generate instructions for one texture unit's env/combiner mode.
1147 */
1148static struct ureg
1149emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001150{
Brian Paula5e70032009-08-31 11:17:59 -06001151 const struct state_key *key = p->state;
Brian Paule9ba9ff2009-09-10 08:41:12 -06001152 GLboolean rgb_saturate, alpha_saturate;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001153 GLuint rgb_shift, alpha_shift;
Brian Paula5e70032009-08-31 11:17:59 -06001154 struct ureg out, dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001155
Keith Whitwellce721142005-07-11 10:10:38 +00001156 if (!key->unit[unit].enabled) {
1157 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001158 }
Roland Scheidegger114152e2009-03-12 15:01:16 +01001159 if (key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1160 /* this isn't really a env stage delivering a color and handled elsewhere */
1161 return get_source(p, SRC_PREVIOUS, 0);
1162 }
Keith Whitwellce721142005-07-11 10:10:38 +00001163
1164 switch (key->unit[unit].ModeRGB) {
1165 case MODE_DOT3_RGB_EXT:
1166 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001167 rgb_shift = 0;
1168 break;
Keith Whitwellce721142005-07-11 10:10:38 +00001169 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001170 alpha_shift = 0;
1171 rgb_shift = 0;
1172 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001173 default:
Keith Whitwellce721142005-07-11 10:10:38 +00001174 rgb_shift = key->unit[unit].ScaleShiftRGB;
1175 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001176 break;
1177 }
Keith Whitwellce721142005-07-11 10:10:38 +00001178
Brian Paul956e6c32009-08-31 11:14:16 -06001179 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
1180 * We don't want to clamp twice.
1181 */
Brian Paule9ba9ff2009-09-10 08:41:12 -06001182 if (rgb_shift)
1183 rgb_saturate = GL_FALSE; /* saturate after rgb shift */
1184 else if (need_saturate(key->unit[unit].ModeRGB))
1185 rgb_saturate = GL_TRUE;
1186 else
1187 rgb_saturate = GL_FALSE;
1188
1189 if (alpha_shift)
1190 alpha_saturate = GL_FALSE; /* saturate after alpha shift */
1191 else if (need_saturate(key->unit[unit].ModeA))
1192 alpha_saturate = GL_TRUE;
1193 else
1194 alpha_saturate = GL_FALSE;
Brian Paul956e6c32009-08-31 11:14:16 -06001195
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001196 /* If this is the very last calculation, emit direct to output reg:
1197 */
Keith Whitwellce721142005-07-11 10:10:38 +00001198 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001199 unit != p->last_tex_stage ||
1200 alpha_shift ||
1201 rgb_shift)
1202 dest = get_temp( p );
1203 else
Brian Paul8d475822009-02-28 11:49:46 -07001204 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLOR);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001205
1206 /* Emit the RGB and A combine ops
1207 */
Keith Whitwellce721142005-07-11 10:10:38 +00001208 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
1209 args_match(key, unit)) {
Brian Paule9ba9ff2009-09-10 08:41:12 -06001210 out = emit_combine( p, dest, WRITEMASK_XYZW, rgb_saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001211 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001212 key->unit[unit].NumArgsRGB,
1213 key->unit[unit].ModeRGB,
1214 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001215 }
Keith Whitwellce721142005-07-11 10:10:38 +00001216 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +02001217 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Brian Paule9ba9ff2009-09-10 08:41:12 -06001218 out = emit_combine( p, dest, WRITEMASK_XYZW, rgb_saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001219 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001220 key->unit[unit].NumArgsRGB,
1221 key->unit[unit].ModeRGB,
1222 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001223 }
1224 else {
1225 /* Need to do something to stop from re-emitting identical
1226 * argument calculations here:
1227 */
Brian Paule9ba9ff2009-09-10 08:41:12 -06001228 out = emit_combine( p, dest, WRITEMASK_XYZ, rgb_saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001229 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001230 key->unit[unit].NumArgsRGB,
1231 key->unit[unit].ModeRGB,
1232 key->unit[unit].OptRGB);
Brian Paule9ba9ff2009-09-10 08:41:12 -06001233 out = emit_combine( p, dest, WRITEMASK_W, alpha_saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001234 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001235 key->unit[unit].NumArgsA,
1236 key->unit[unit].ModeA,
1237 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001238 }
1239
1240 /* Deal with the final shift:
1241 */
1242 if (alpha_shift || rgb_shift) {
Brian Paula5e70032009-08-31 11:17:59 -06001243 struct ureg shift;
Brian Paule9ba9ff2009-09-10 08:41:12 -06001244 GLboolean saturate = GL_TRUE; /* always saturate at this point */
Brian Paula5e70032009-08-31 11:17:59 -06001245
Keith Whitwell15e75e02005-04-29 15:11:39 +00001246 if (rgb_shift == alpha_shift) {
José Fonseca452a5922008-05-31 18:14:09 +09001247 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001248 }
1249 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001250 shift = register_const4f(p,
José Fonseca452a5922008-05-31 18:14:09 +09001251 (GLfloat)(1<<rgb_shift),
1252 (GLfloat)(1<<rgb_shift),
1253 (GLfloat)(1<<rgb_shift),
1254 (GLfloat)(1<<alpha_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001255 }
Brian Paul7e807512005-11-05 17:10:45 +00001256 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001257 saturate, out, shift, undef );
1258 }
1259 else
1260 return out;
1261}
1262
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001263
Brian Paul22db5352005-11-19 23:45:10 +00001264/**
1265 * Generate instruction for getting a texture source term.
1266 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001267static void load_texture( struct texenv_fragment_program *p, GLuint unit )
1268{
1269 if (is_undef(p->src_texture[unit])) {
Brian Paulb5ec0a62009-09-01 15:51:23 -06001270 const GLuint texTarget = p->state->unit[unit].source_index;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001271 struct ureg texcoord;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001272 struct ureg tmp = get_tex_temp( p );
1273
Roland Scheidegger114152e2009-03-12 15:01:16 +01001274 if (is_undef(p->texcoord_tex[unit])) {
1275 texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
1276 }
1277 else {
1278 /* might want to reuse this reg for tex output actually */
1279 texcoord = p->texcoord_tex[unit];
1280 }
1281
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001282 /* TODO: Use D0_MASK_XY where possible.
1283 */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +02001284 if (p->state->unit[unit].enabled) {
Brian Paul44e018c2009-02-20 13:42:08 -07001285 GLboolean shadow = GL_FALSE;
1286
1287 if (p->state->unit[unit].shadow) {
1288 p->program->Base.ShadowSamplers |= 1 << unit;
1289 shadow = GL_TRUE;
1290 }
1291
Aapo Tahkolab8915342006-03-28 10:26:34 +00001292 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1293 tmp, WRITEMASK_XYZW,
Brian Paul44e018c2009-02-20 13:42:08 -07001294 unit, texTarget, shadow,
1295 texcoord );
Brian9b7e5a52007-12-14 11:16:49 -07001296
1297 p->program->Base.SamplersUsed |= (1 << unit);
Brian1fe385f2007-12-14 11:42:28 -07001298 /* This identity mapping should already be in place
1299 * (see _mesa_init_program_struct()) but let's be safe.
1300 */
1301 p->program->Base.SamplerUnits[unit] = unit;
Brian9b7e5a52007-12-14 11:16:49 -07001302 }
1303 else
Aapo Tahkolab8915342006-03-28 10:26:34 +00001304 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001305 }
1306}
1307
Keith Whitwell241b6b72005-05-23 09:50:34 +00001308static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +00001309 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001310{
1311 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +00001312 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001313 load_texture(p, unit);
1314 break;
1315
Keith Whitwellce721142005-07-11 10:10:38 +00001316 case SRC_TEXTURE0:
1317 case SRC_TEXTURE1:
1318 case SRC_TEXTURE2:
1319 case SRC_TEXTURE3:
1320 case SRC_TEXTURE4:
1321 case SRC_TEXTURE5:
1322 case SRC_TEXTURE6:
1323 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +00001324 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001325 break;
1326
1327 default:
Brian Paul6947f852009-01-23 17:32:09 -07001328 /* not a texture src - do nothing */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001329 break;
1330 }
Keith Whitwell241b6b72005-05-23 09:50:34 +00001331
1332 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001333}
1334
Brian Paul22db5352005-11-19 23:45:10 +00001335
1336/**
1337 * Generate instructions for loading all texture source terms.
1338 */
1339static GLboolean
Brian Paul457e4272009-09-01 16:22:02 -06001340load_texunit_sources( struct texenv_fragment_program *p, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001341{
Brian Paula5e70032009-08-31 11:17:59 -06001342 const struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +00001343 GLuint i;
1344
1345 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001346 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001347 }
Aapo Tahkolab8915342006-03-28 10:26:34 +00001348
1349 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1350 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1351 }
1352
Keith Whitwell241b6b72005-05-23 09:50:34 +00001353 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001354}
1355
Roland Scheidegger114152e2009-03-12 15:01:16 +01001356/**
1357 * Generate instructions for loading bump map textures.
1358 */
1359static GLboolean
Brian Paul457e4272009-09-01 16:22:02 -06001360load_texunit_bumpmap( struct texenv_fragment_program *p, GLuint unit )
Roland Scheidegger114152e2009-03-12 15:01:16 +01001361{
Brian Paula5e70032009-08-31 11:17:59 -06001362 const struct state_key *key = p->state;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001363 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1364 struct ureg texcDst, bumpMapRes;
1365 struct ureg constdudvcolor = register_const4f(p, 0.0, 0.0, 0.0, 1.0);
1366 struct ureg texcSrc = register_input(p, FRAG_ATTRIB_TEX0 + bumpedUnitNr);
1367 struct ureg rotMat0 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_0, unit );
1368 struct ureg rotMat1 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_1, unit );
1369
1370 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1371
1372 bumpMapRes = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1373 texcDst = get_tex_temp( p );
1374 p->texcoord_tex[bumpedUnitNr] = texcDst;
1375
Brian Paul457e4272009-09-01 16:22:02 -06001376 /* Apply rot matrix and add coords to be available in next phase.
1377 * dest = (Arg0.xxxx * rotMat0 + Arg1) + (Arg0.yyyy * rotMat1)
1378 * note only 2 coords are affected the rest are left unchanged (mul by 0)
1379 */
Roland Scheidegger114152e2009-03-12 15:01:16 +01001380 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1381 swizzle1(bumpMapRes, SWIZZLE_X), rotMat0, texcSrc );
1382 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1383 swizzle1(bumpMapRes, SWIZZLE_Y), rotMat1, texcDst );
1384
Brian Paul457e4272009-09-01 16:22:02 -06001385 /* Move 0,0,0,1 into bumpmap src if someone (crossbar) is foolish
1386 * enough to access this later, should optimize away.
1387 */
1388 emit_arith( p, OPCODE_MOV, bumpMapRes, WRITEMASK_XYZW, 0,
1389 constdudvcolor, undef, undef );
Roland Scheidegger114152e2009-03-12 15:01:16 +01001390
1391 return GL_TRUE;
1392}
Brian Paul22db5352005-11-19 23:45:10 +00001393
1394/**
1395 * Generate a new fragment program which implements the context's
1396 * current texture env/combine mode.
1397 */
1398static void
Brian Paule998c342006-10-29 21:17:18 +00001399create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001400 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001401{
Brian Paule998c342006-10-29 21:17:18 +00001402 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001403 struct texenv_fragment_program p;
1404 GLuint unit;
1405 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001406
Keith Whitwelle4902422005-05-11 15:16:35 +00001407 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwellce721142005-07-11 10:10:38 +00001408 p.state = key;
1409 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001410
Brian Paule998c342006-10-29 21:17:18 +00001411 /* During code generation, use locally-allocated instruction buffer,
1412 * then alloc dynamic storage below.
1413 */
1414 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001415 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Brian Paul457e4272009-09-01 16:22:02 -06001416 p.program->Base.String = NULL;
1417 p.program->Base.NumTexIndirections = 1; /* is this right? */
Brian21f99792007-01-09 11:00:21 -07001418 p.program->Base.NumTexInstructions = 0;
1419 p.program->Base.NumAluInstructions = 0;
Brian Paul457e4272009-09-01 16:22:02 -06001420 p.program->Base.NumInstructions = 0;
1421 p.program->Base.NumTemporaries = 0;
1422 p.program->Base.NumParameters = 0;
1423 p.program->Base.NumAttributes = 0;
1424 p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001425 p.program->Base.Parameters = _mesa_new_parameter_list();
Brian Paul457e4272009-09-01 16:22:02 -06001426 p.program->Base.InputsRead = 0x0;
Brian Paul8d475822009-02-28 11:49:46 -07001427 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLOR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001428
Roland Scheidegger114152e2009-03-12 15:01:16 +01001429 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001430 p.src_texture[unit] = undef;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001431 p.texcoord_tex[unit] = undef;
1432 }
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001433
Keith Whitwell47b29f52005-05-04 11:44:44 +00001434 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001435 p.half = undef;
1436 p.zero = undef;
1437 p.one = undef;
1438
Keith Whitwell15e75e02005-04-29 15:11:39 +00001439 p.last_tex_stage = 0;
Brian93fef222007-10-29 12:25:46 -06001440 release_temps(ctx, &p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001441
Keith Whitwellce721142005-07-11 10:10:38 +00001442 if (key->enabled_units) {
Brian Paul457e4272009-09-01 16:22:02 -06001443 GLboolean needbumpstage = GL_FALSE;
1444
Roland Scheidegger114152e2009-03-12 15:01:16 +01001445 /* Zeroth pass - bump map textures first */
Brian Paul7a7d5872009-09-02 15:39:13 -06001446 for (unit = 0; unit < key->nr_enabled_units; unit++)
Brian Paul457e4272009-09-01 16:22:02 -06001447 if (key->unit[unit].enabled &&
1448 key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001449 needbumpstage = GL_TRUE;
1450 load_texunit_bumpmap( &p, unit );
1451 }
1452 if (needbumpstage)
1453 p.program->Base.NumTexIndirections++;
1454
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001455 /* First pass - to support texture_env_crossbar, first identify
1456 * all referenced texture sources and emit texld instructions
1457 * for each:
1458 */
Brian Paul7a7d5872009-09-02 15:39:13 -06001459 for (unit = 0; unit < key->nr_enabled_units; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001460 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001461 load_texunit_sources( &p, unit );
1462 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001463 }
1464
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001465 /* Second pass - emit combine instructions to build final color:
1466 */
Brian Paul7a7d5872009-09-02 15:39:13 -06001467 for (unit = 0; unit < key->nr_enabled_units; unit++)
Brian Paul84d6bed2009-09-01 16:10:57 -06001468 if (key->unit[unit].enabled) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001469 p.src_previous = emit_texenv( &p, unit );
Brian Paul5620c202008-09-26 11:18:06 -06001470 reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
Brian93fef222007-10-29 12:25:46 -06001471 release_temps(ctx, &p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001472 }
1473 }
1474
Keith Whitwellce721142005-07-11 10:10:38 +00001475 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul8d475822009-02-28 11:49:46 -07001476 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLOR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001477
Keith Whitwellce721142005-07-11 10:10:38 +00001478 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001479 /* Emit specular add.
1480 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001481 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001482 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1483 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001484 }
Brian Paulaa206952005-09-16 18:14:24 +00001485 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001486 /* Will wind up in here if no texture enabled or a couple of
1487 * other scenarios (GL_REPLACE for instance).
1488 */
Brian Paul7e807512005-11-05 17:10:45 +00001489 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001490 }
1491
Keith Whitwell47b29f52005-05-04 11:44:44 +00001492 /* Finish up:
1493 */
Brian Paul7e807512005-11-05 17:10:45 +00001494 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001495
Keith Whitwellce721142005-07-11 10:10:38 +00001496 if (key->fog_enabled) {
1497 /* Pull fog mode from GLcontext, the value in the state key is
1498 * a reduced value and not what is expected in FogOption
1499 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001500 p.program->FogOption = ctx->Fog.Mode;
Brian Paul457e4272009-09-01 16:22:02 -06001501 p.program->Base.InputsRead |= FRAG_BIT_FOGC;
1502 }
1503 else {
Keith Whitwell276330b2005-05-09 17:58:13 +00001504 p.program->FogOption = GL_NONE;
Brian Paul457e4272009-09-01 16:22:02 -06001505 }
Keith Whitwell47b29f52005-05-04 11:44:44 +00001506
Brian21f99792007-01-09 11:00:21 -07001507 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001508 program_error(&p, "Exceeded max nr indirect texture lookups");
1509
Brian21f99792007-01-09 11:00:21 -07001510 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001511 program_error(&p, "Exceeded max TEX instructions");
1512
Brian21f99792007-01-09 11:00:21 -07001513 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001514 program_error(&p, "Exceeded max ALU instructions");
1515
Brian Paul5d7b49f2005-11-19 23:12:20 +00001516 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001517
Brian Paule998c342006-10-29 21:17:18 +00001518 /* Allocate final instruction array */
Brianc81cce72007-09-25 15:18:51 -06001519 p.program->Base.Instructions
1520 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1521 if (!p.program->Base.Instructions) {
Brian Paule998c342006-10-29 21:17:18 +00001522 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1523 "generating tex env program");
1524 return;
1525 }
Brianc81cce72007-09-25 15:18:51 -06001526 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1527 p.program->Base.NumInstructions);
1528
1529 if (p.program->FogOption) {
1530 _mesa_append_fog_code(ctx, p.program);
1531 p.program->FogOption = GL_NONE;
1532 }
1533
Brian Paule998c342006-10-29 21:17:18 +00001534
Keith Whitwelldbeea252005-05-10 13:57:50 +00001535 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001536 */
Brian Paule998c342006-10-29 21:17:18 +00001537 if (ctx->Driver.ProgramStringNotify) {
Brian Paul4ac9c802010-02-04 16:49:35 -07001538 GLboolean ok = ctx->Driver.ProgramStringNotify(ctx,
1539 GL_FRAGMENT_PROGRAM_ARB,
1540 &p.program->Base);
1541 /* Driver should be able to handle any texenv programs as long as
1542 * the driver correctly reported max number of texture units correctly,
1543 * etc.
1544 */
1545 ASSERT(ok);
1546 (void) ok; /* silence unused var warning */
Brian Paule998c342006-10-29 21:17:18 +00001547 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001548
Brian Paule998c342006-10-29 21:17:18 +00001549 if (DISASSEM) {
1550 _mesa_print_program(&p.program->Base);
1551 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001552 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001553}
1554
Brian Paul5d7b49f2005-11-19 23:12:20 +00001555
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001556/**
1557 * Return a fragment program which implements the current
1558 * fixed-function texture, fog and color-sum operations.
1559 */
1560struct gl_fragment_program *
1561_mesa_get_fixed_func_fragment_program(GLcontext *ctx)
Keith Whitwellce721142005-07-11 10:10:38 +00001562{
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001563 struct gl_fragment_program *prog;
1564 struct state_key key;
Brian Paul7a7d5872009-09-02 15:39:13 -06001565 GLuint keySize;
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001566
Brian Paul7a7d5872009-09-02 15:39:13 -06001567 keySize = make_state_key(ctx, &key);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001568
1569 prog = (struct gl_fragment_program *)
1570 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
Brian Paul7a7d5872009-09-02 15:39:13 -06001571 &key, keySize);
Keith Whitwellce721142005-07-11 10:10:38 +00001572
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001573 if (!prog) {
1574 prog = (struct gl_fragment_program *)
1575 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1576
1577 create_new_program(ctx, &key, prog);
1578
1579 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
Brian Paul7a7d5872009-09-02 15:39:13 -06001580 &key, keySize, &prog->Base);
Keith Whitwellce721142005-07-11 10:10:38 +00001581 }
1582
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001583 return prog;
Keith Whitwellce721142005-07-11 10:10:38 +00001584}