blob: 2f3e47e69ecf6eddb04c1fb63e335c4170d0233f [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);
279 }
280}
281
282
283
284/**
Brian Paulf337e2c2009-09-01 15:49:55 -0600285 * Translate TEXTURE_x_BIT to TEXTURE_x_INDEX.
286 */
Brian Paule00ac112005-09-15 05:00:45 +0000287static GLuint translate_tex_src_bit( GLbitfield bit )
Keith Whitwellce721142005-07-11 10:10:38 +0000288{
Brian Paulb5ec0a62009-09-01 15:51:23 -0600289 ASSERT(bit);
290 return _mesa_ffs(bit) - 1;
Keith Whitwellce721142005-07-11 10:10:38 +0000291}
292
Brian Paulf337e2c2009-09-01 15:49:55 -0600293
Keith Whitwell1680ef82008-10-03 17:30:59 +0100294#define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
295#define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
296
Brian Paul239617f2008-10-07 11:22:47 -0600297/**
298 * Identify all possible varying inputs. The fragment program will
Keith Whitwell1680ef82008-10-03 17:30:59 +0100299 * never reference non-varying inputs, but will track them via state
300 * constants instead.
301 *
302 * This function figures out all the inputs that the fragment program
303 * has access to. The bitmask is later reduced to just those which
304 * are actually referenced.
305 */
Brian Paul239617f2008-10-07 11:22:47 -0600306static GLbitfield get_fp_input_mask( GLcontext *ctx )
Keith Whitwell1680ef82008-10-03 17:30:59 +0100307{
Eric Anholte5f63c42009-06-02 07:47:20 -0700308 /* _NEW_PROGRAM */
Brian Paulf0b07942008-12-17 14:04:03 -0700309 const GLboolean vertexShader = (ctx->Shader.CurrentProgram &&
Eric Anholt40990d92009-08-03 12:36:52 -0700310 ctx->Shader.CurrentProgram->LinkStatus &&
Brian Paulf0b07942008-12-17 14:04:03 -0700311 ctx->Shader.CurrentProgram->VertexProgram);
312 const GLboolean vertexProgram = ctx->VertexProgram._Enabled;
Brian Paul239617f2008-10-07 11:22:47 -0600313 GLbitfield fp_inputs = 0x0;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100314
Brian Paul6d4d51d2008-10-10 13:39:14 -0600315 if (ctx->VertexProgram._Overriden) {
316 /* Somebody's messing with the vertex program and we don't have
317 * a clue what's happening. Assume that it could be producing
318 * all possible outputs.
319 */
320 fp_inputs = ~0;
321 }
322 else if (ctx->RenderMode == GL_FEEDBACK) {
Eric Anholte5f63c42009-06-02 07:47:20 -0700323 /* _NEW_RENDERMODE */
Brian Paul6d4d51d2008-10-10 13:39:14 -0600324 fp_inputs = (FRAG_BIT_COL0 | FRAG_BIT_TEX0);
325 }
Brian Paulf0b07942008-12-17 14:04:03 -0700326 else if (!(vertexProgram || vertexShader) ||
Brian Pauld7296a12008-12-17 11:29:06 -0700327 !ctx->VertexProgram._Current) {
Brian Paulf0b07942008-12-17 14:04:03 -0700328 /* Fixed function vertex logic */
Eric Anholte5f63c42009-06-02 07:47:20 -0700329 /* _NEW_ARRAY */
Brian Paul239617f2008-10-07 11:22:47 -0600330 GLbitfield varying_inputs = ctx->varying_vp_inputs;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100331
Keith Whitwell97e63432008-10-20 13:03:45 +0100332 /* These get generated in the setup routine regardless of the
333 * vertex program:
334 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700335 /* _NEW_POINT */
Keith Whitwell97e63432008-10-20 13:03:45 +0100336 if (ctx->Point.PointSprite)
337 varying_inputs |= FRAG_BITS_TEX_ANY;
338
Keith Whitwell1680ef82008-10-03 17:30:59 +0100339 /* First look at what values may be computed by the generated
340 * vertex program:
341 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700342 /* _NEW_LIGHT */
Keith Whitwell1680ef82008-10-03 17:30:59 +0100343 if (ctx->Light.Enabled) {
344 fp_inputs |= FRAG_BIT_COL0;
345
Eric Anholt9cea84b2009-07-16 18:41:03 -0700346 if (texenv_doing_secondary_color(ctx))
Keith Whitwell1680ef82008-10-03 17:30:59 +0100347 fp_inputs |= FRAG_BIT_COL1;
348 }
349
Eric Anholte5f63c42009-06-02 07:47:20 -0700350 /* _NEW_TEXTURE */
Keith Whitwell1680ef82008-10-03 17:30:59 +0100351 fp_inputs |= (ctx->Texture._TexGenEnabled |
352 ctx->Texture._TexMatEnabled) << FRAG_ATTRIB_TEX0;
353
354 /* Then look at what might be varying as a result of enabled
355 * arrays, etc:
356 */
Brian Paul6807d962009-08-07 09:42:28 -0600357 if (varying_inputs & VERT_BIT_COLOR0)
358 fp_inputs |= FRAG_BIT_COL0;
359 if (varying_inputs & VERT_BIT_COLOR1)
360 fp_inputs |= FRAG_BIT_COL1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100361
362 fp_inputs |= (((varying_inputs & VERT_BIT_TEX_ANY) >> VERT_ATTRIB_TEX0)
363 << FRAG_ATTRIB_TEX0);
364
365 }
366 else {
367 /* calculate from vp->outputs */
Brian Paul2389c052008-12-17 19:01:34 -0700368 struct gl_vertex_program *vprog;
369 GLbitfield vp_outputs;
370
371 /* Choose GLSL vertex shader over ARB vertex program. Need this
372 * since vertex shader state validation comes after fragment state
373 * validation (see additional comments in state.c).
374 */
375 if (vertexShader)
376 vprog = ctx->Shader.CurrentProgram->VertexProgram;
377 else
Eric Anholta9ba1bf2009-08-03 12:38:56 -0700378 vprog = ctx->VertexProgram.Current;
Brian Paul2389c052008-12-17 19:01:34 -0700379
380 vp_outputs = vprog->Base.OutputsWritten;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100381
Keith Whitwell97e63432008-10-20 13:03:45 +0100382 /* These get generated in the setup routine regardless of the
383 * vertex program:
384 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700385 /* _NEW_POINT */
Keith Whitwell97e63432008-10-20 13:03:45 +0100386 if (ctx->Point.PointSprite)
387 vp_outputs |= FRAG_BITS_TEX_ANY;
388
Brian Paul6807d962009-08-07 09:42:28 -0600389 if (vp_outputs & (1 << VERT_RESULT_COL0))
390 fp_inputs |= FRAG_BIT_COL0;
391 if (vp_outputs & (1 << VERT_RESULT_COL1))
392 fp_inputs |= FRAG_BIT_COL1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100393
Keith Whitwell0370d6b2008-10-04 12:41:56 +0100394 fp_inputs |= (((vp_outputs & VERT_RESULT_TEX_ANY) >> VERT_RESULT_TEX0)
395 << FRAG_ATTRIB_TEX0);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100396 }
397
398 return fp_inputs;
399}
400
401
Brian Paul22db5352005-11-19 23:45:10 +0000402/**
403 * Examine current texture environment state and generate a unique
404 * key to identify it.
405 */
Brian Paul7a7d5872009-09-02 15:39:13 -0600406static GLuint make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000407{
Keith Whitwellce721142005-07-11 10:10:38 +0000408 GLuint i, j;
Brian Paul239617f2008-10-07 11:22:47 -0600409 GLbitfield inputs_referenced = FRAG_BIT_COL0;
Brian Paulf337e2c2009-09-01 15:49:55 -0600410 const GLbitfield inputs_available = get_fp_input_mask( ctx );
Brian Paul7a7d5872009-09-02 15:39:13 -0600411 GLuint keySize;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100412
Keith Whitwell8065c122006-05-22 16:09:27 +0000413 memset(key, 0, sizeof(*key));
414
Eric Anholte5f63c42009-06-02 07:47:20 -0700415 /* _NEW_TEXTURE */
Brian Paule9b34882008-12-31 11:54:02 -0700416 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000417 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Brian Paulf337e2c2009-09-01 15:49:55 -0600418 const struct gl_texture_object *texObj = texUnit->_Current;
419 const struct gl_tex_env_combine_state *comb = texUnit->_CurrentCombine;
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800420 GLenum format;
Roland Scheideggerbf4a0fa2008-02-15 17:26:06 +0100421
422 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
Keith Whitwellce721142005-07-11 10:10:38 +0000423 continue;
424
Brian Paulf337e2c2009-09-01 15:49:55 -0600425 format = texObj->Image[0][texObj->BaseLevel]->_BaseFormat;
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800426
Keith Whitwellce721142005-07-11 10:10:38 +0000427 key->unit[i].enabled = 1;
428 key->enabled_units |= (1<<i);
Brian Paul7a7d5872009-09-02 15:39:13 -0600429 key->nr_enabled_units = i + 1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100430 inputs_referenced |= FRAG_BIT_TEX(i);
Keith Whitwellce721142005-07-11 10:10:38 +0000431
Brian Paulf337e2c2009-09-01 15:49:55 -0600432 key->unit[i].source_index =
433 translate_tex_src_bit(texUnit->_ReallyEnabled);
434
435 key->unit[i].shadow = ((texObj->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800436 ((format == GL_DEPTH_COMPONENT) ||
437 (format == GL_DEPTH_STENCIL_EXT)));
Keith Whitwellce721142005-07-11 10:10:38 +0000438
Brian Paulf337e2c2009-09-01 15:49:55 -0600439 key->unit[i].NumArgsRGB = comb->_NumArgsRGB;
440 key->unit[i].NumArgsA = comb->_NumArgsA;
Keith Whitwellce721142005-07-11 10:10:38 +0000441
442 key->unit[i].ModeRGB =
Brian Paulf337e2c2009-09-01 15:49:55 -0600443 translate_mode(texUnit->EnvMode, comb->ModeRGB);
Keith Whitwellce721142005-07-11 10:10:38 +0000444 key->unit[i].ModeA =
Brian Paulf337e2c2009-09-01 15:49:55 -0600445 translate_mode(texUnit->EnvMode, comb->ModeA);
Roland Scheidegger114152e2009-03-12 15:01:16 +0100446
Brian Paulf337e2c2009-09-01 15:49:55 -0600447 key->unit[i].ScaleShiftRGB = comb->ScaleShiftRGB;
448 key->unit[i].ScaleShiftA = comb->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000449
Brian Paulbd9b2be2009-03-08 17:58:54 -0600450 for (j = 0; j < MAX_COMBINER_TERMS; j++) {
Brian Paulf337e2c2009-09-01 15:49:55 -0600451 key->unit[i].OptRGB[j].Operand = translate_operand(comb->OperandRGB[j]);
452 key->unit[i].OptA[j].Operand = translate_operand(comb->OperandA[j]);
453 key->unit[i].OptRGB[j].Source = translate_source(comb->SourceRGB[j]);
454 key->unit[i].OptA[j].Source = translate_source(comb->SourceA[j]);
Keith Whitwellce721142005-07-11 10:10:38 +0000455 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100456
457 if (key->unit[i].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
458 /* requires some special translation */
459 key->unit[i].NumArgsRGB = 2;
460 key->unit[i].ScaleShiftRGB = 0;
461 key->unit[i].OptRGB[0].Operand = OPR_SRC_COLOR;
462 key->unit[i].OptRGB[0].Source = SRC_TEXTURE;
463 key->unit[i].OptRGB[1].Operand = OPR_SRC_COLOR;
464 key->unit[i].OptRGB[1].Source = texUnit->BumpTarget - GL_TEXTURE0 + SRC_TEXTURE0;
465 }
Keith Whitwellce721142005-07-11 10:10:38 +0000466 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100467
Eric Anholt9cea84b2009-07-16 18:41:03 -0700468 /* _NEW_LIGHT | _NEW_FOG */
469 if (texenv_doing_secondary_color(ctx)) {
Keith Whitwellce721142005-07-11 10:10:38 +0000470 key->separate_specular = 1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100471 inputs_referenced |= FRAG_BIT_COL1;
472 }
Keith Whitwellce721142005-07-11 10:10:38 +0000473
Eric Anholte5f63c42009-06-02 07:47:20 -0700474 /* _NEW_FOG */
Keith Whitwellce721142005-07-11 10:10:38 +0000475 if (ctx->Fog.Enabled) {
476 key->fog_enabled = 1;
477 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100478 inputs_referenced |= FRAG_BIT_FOGC; /* maybe */
Keith Whitwellce721142005-07-11 10:10:38 +0000479 }
Keith Whitwell1680ef82008-10-03 17:30:59 +0100480
481 key->inputs_available = (inputs_available & inputs_referenced);
Brian Paul7a7d5872009-09-02 15:39:13 -0600482
483 /* compute size of state key, ignoring unused texture units */
484 keySize = sizeof(*key) - sizeof(key->unit)
485 + key->nr_enabled_units * sizeof(key->unit[0]);
486
487 return keySize;
Keith Whitwellce721142005-07-11 10:10:38 +0000488}
489
Brian Paul7a7d5872009-09-02 15:39:13 -0600490
Brian Paul239617f2008-10-07 11:22:47 -0600491/**
492 * Use uregs to represent registers internally, translate to Mesa's
Keith Whitwell15e75e02005-04-29 15:11:39 +0000493 * expected formats on emit.
494 *
495 * NOTE: These are passed by value extensively in this file rather
496 * than as usual by pointer reference. If this disturbs you, try
497 * remembering they are just 32bits in size.
498 *
499 * GCC is smart enough to deal with these dword-sized structures in
500 * much the same way as if I had defined them as dwords and was using
501 * macros to access and set the fields. This is much nicer and easier
502 * to evolve.
503 */
504struct ureg {
505 GLuint file:4;
506 GLuint idx:8;
507 GLuint negatebase:1;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000508 GLuint swz:12;
Brian Paul1480bca2009-09-01 16:01:12 -0600509 GLuint pad:7;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000510};
511
Brian Paul13abf912006-04-13 19:17:13 +0000512static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000513 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000514 ~0,
515 0,
516 0,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000517 0
518};
519
Keith Whitwell15e75e02005-04-29 15:11:39 +0000520
Brian Paul239617f2008-10-07 11:22:47 -0600521/** State used to build the fragment program:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000522 */
523struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000524 struct gl_fragment_program *program;
Keith Whitwellce721142005-07-11 10:10:38 +0000525 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000526
Brian Paul239617f2008-10-07 11:22:47 -0600527 GLbitfield alu_temps; /**< Track texture indirections, see spec. */
528 GLbitfield temps_output; /**< Track texture indirections, see spec. */
529 GLbitfield temp_in_use; /**< Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000530 GLboolean error;
531
Brian Paule9b34882008-12-31 11:54:02 -0700532 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000533 /* Reg containing each texture unit's sampled texture color,
534 * else undef.
535 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000536
Roland Scheidegger114152e2009-03-12 15:01:16 +0100537 struct ureg texcoord_tex[MAX_TEXTURE_COORD_UNITS];
538 /* Reg containing texcoord for a texture unit,
539 * needed for bump mapping, else undef.
540 */
541
Brian Paul239617f2008-10-07 11:22:47 -0600542 struct ureg src_previous; /**< Reg containing color from previous
Keith Whitwell15e75e02005-04-29 15:11:39 +0000543 * stage. May need to be decl'd.
544 */
545
Brian Paul239617f2008-10-07 11:22:47 -0600546 GLuint last_tex_stage; /**< Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000547
548 struct ureg half;
549 struct ureg one;
550 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000551};
552
553
554
555static struct ureg make_ureg(GLuint file, GLuint idx)
556{
557 struct ureg reg;
558 reg.file = file;
559 reg.idx = idx;
560 reg.negatebase = 0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000561 reg.swz = SWIZZLE_NOOP;
562 reg.pad = 0;
563 return reg;
564}
565
566static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
567{
568 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
569 GET_SWZ(reg.swz, y),
570 GET_SWZ(reg.swz, z),
571 GET_SWZ(reg.swz, w));
572
573 return reg;
574}
575
576static struct ureg swizzle1( struct ureg reg, int x )
577{
578 return swizzle(reg, x, x, x, x);
579}
580
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000581static struct ureg negate( struct ureg reg )
582{
583 reg.negatebase ^= 1;
584 return reg;
585}
586
Keith Whitwell15e75e02005-04-29 15:11:39 +0000587static GLboolean is_undef( struct ureg reg )
588{
Alan Hourihane8d972652006-08-10 13:12:00 +0000589 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000590}
591
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000592
Keith Whitwell15e75e02005-04-29 15:11:39 +0000593static struct ureg get_temp( struct texenv_fragment_program *p )
594{
Brian Paul13abf912006-04-13 19:17:13 +0000595 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000596
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000597 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000598 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000599 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000600
601 /* Then any unused temporary:
602 */
603 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000604 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000605
Keith Whitwell15e75e02005-04-29 15:11:39 +0000606 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000607 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000608 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000609 }
610
Brian Paul13abf912006-04-13 19:17:13 +0000611 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000612 p->program->Base.NumTemporaries = bit;
613
Keith Whitwell93cd9232005-05-11 08:30:23 +0000614 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000615 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
616}
617
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000618static struct ureg get_tex_temp( struct texenv_fragment_program *p )
619{
620 int bit;
621
Brian Pauld01269a2008-09-25 18:27:22 -0600622 /* First try to find available temp not previously used (to avoid
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000623 * starting a new texture indirection). According to the spec, the
624 * ~p->temps_output isn't necessary, but will keep it there for
625 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000626 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000627 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000628
629 /* Then any unused temporary:
630 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000631 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000632 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000633
634 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000635 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000636 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000637 }
638
Brian Paul13abf912006-04-13 19:17:13 +0000639 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000640 p->program->Base.NumTemporaries = bit;
641
Keith Whitwell93cd9232005-05-11 08:30:23 +0000642 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000643 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
644}
645
Keith Whitwell15e75e02005-04-29 15:11:39 +0000646
Brian Paul5620c202008-09-26 11:18:06 -0600647/** Mark a temp reg as being no longer allocatable. */
648static void reserve_temp( struct texenv_fragment_program *p, struct ureg r )
649{
650 if (r.file == PROGRAM_TEMPORARY)
651 p->temps_output |= (1 << r.idx);
652}
653
654
Brian93fef222007-10-29 12:25:46 -0600655static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000656{
Brian93fef222007-10-29 12:25:46 -0600657 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000658
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000659 /* KW: To support tex_env_crossbar, don't release the registers in
660 * temps_output.
661 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000662 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000663 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000664 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000665 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000666}
667
668
Brian9fe3e2e2007-02-23 11:44:14 -0700669static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000670 GLint s0,
671 GLint s1,
672 GLint s2,
673 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700674 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000675{
Brian9fe3e2e2007-02-23 11:44:14 -0700676 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000677 GLuint idx;
678 tokens[0] = s0;
679 tokens[1] = s1;
680 tokens[2] = s2;
681 tokens[3] = s3;
682 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000683 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000684 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000685}
686
Keith Whitwell47b29f52005-05-04 11:44:44 +0000687
Brian9fe3e2e2007-02-23 11:44:14 -0700688#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
689#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
690#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
691#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000692
Keith Whitwell1680ef82008-10-03 17:30:59 +0100693static GLuint frag_to_vert_attrib( GLuint attrib )
694{
695 switch (attrib) {
696 case FRAG_ATTRIB_COL0: return VERT_ATTRIB_COLOR0;
697 case FRAG_ATTRIB_COL1: return VERT_ATTRIB_COLOR1;
698 default:
699 assert(attrib >= FRAG_ATTRIB_TEX0);
700 assert(attrib <= FRAG_ATTRIB_TEX7);
701 return attrib - FRAG_ATTRIB_TEX0 + VERT_ATTRIB_TEX0;
702 }
703}
704
Keith Whitwell47b29f52005-05-04 11:44:44 +0000705
706static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
707{
Keith Whitwell1680ef82008-10-03 17:30:59 +0100708 if (p->state->inputs_available & (1<<input)) {
709 p->program->Base.InputsRead |= (1 << input);
710 return make_ureg(PROGRAM_INPUT, input);
711 }
712 else {
713 GLuint idx = frag_to_vert_attrib( input );
714 return register_param3( p, STATE_INTERNAL, STATE_CURRENT_ATTRIB, idx );
715 }
Keith Whitwell47b29f52005-05-04 11:44:44 +0000716}
717
718
Brian Paul7e807512005-11-05 17:10:45 +0000719static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000720 struct ureg ureg )
721{
722 reg->File = ureg.file;
723 reg->Index = ureg.idx;
724 reg->Swizzle = ureg.swz;
Brian Paul7db7ff82009-04-14 22:14:30 -0600725 reg->Negate = ureg.negatebase ? NEGATE_XYZW : NEGATE_NONE;
Brian Paul1480bca2009-09-01 16:01:12 -0600726 reg->Abs = GL_FALSE;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000727}
728
Brian Paul7e807512005-11-05 17:10:45 +0000729static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000730 struct ureg ureg, GLuint mask )
731{
732 dst->File = ureg.file;
733 dst->Index = ureg.idx;
734 dst->WriteMask = mask;
Brianc9d495c2007-10-23 10:55:24 -0600735 dst->CondMask = COND_TR; /* always pass cond test */
736 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000737}
738
Brian Paul7e807512005-11-05 17:10:45 +0000739static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000740emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000741 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000742 struct ureg dest,
743 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000744 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000745 struct ureg src0,
746 struct ureg src1,
747 struct ureg src2 )
748{
Brian Paul9ed03152009-09-01 15:57:36 -0600749 const GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000750 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Brian2a8e9bb2007-10-23 10:24:53 -0600751
752 assert(nr < MAX_INSTRUCTIONS);
753
Brian Pauld6272e02006-10-29 18:03:16 +0000754 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000755 inst->Opcode = op;
756
Keith Whitwell47b29f52005-05-04 11:44:44 +0000757 emit_arg( &inst->SrcReg[0], src0 );
758 emit_arg( &inst->SrcReg[1], src1 );
759 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000760
Brian Paule31ac052005-11-20 17:52:40 +0000761 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000762
763 emit_dst( &inst->DstReg, dest, mask );
764
Brian Paul5620c202008-09-26 11:18:06 -0600765#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000766 /* Accounting for indirection tracking:
767 */
768 if (dest.file == PROGRAM_TEMPORARY)
769 p->temps_output |= 1 << dest.idx;
Brian Paul5620c202008-09-26 11:18:06 -0600770#endif
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000771
Keith Whitwell15e75e02005-04-29 15:11:39 +0000772 return inst;
773}
774
775
776static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000777 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000778 struct ureg dest,
779 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000780 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000781 struct ureg src0,
782 struct ureg src1,
783 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000784{
785 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
786
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000787 /* Accounting for indirection tracking:
788 */
789 if (src0.file == PROGRAM_TEMPORARY)
790 p->alu_temps |= 1 << src0.idx;
791
792 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
793 p->alu_temps |= 1 << src1.idx;
794
795 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
796 p->alu_temps |= 1 << src2.idx;
797
798 if (dest.file == PROGRAM_TEMPORARY)
799 p->alu_temps |= 1 << dest.idx;
800
Brian21f99792007-01-09 11:00:21 -0700801 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000802 return dest;
803}
804
805static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000806 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000807 struct ureg dest,
808 GLuint destmask,
809 GLuint tex_unit,
810 GLuint tex_idx,
Brian Paul44e018c2009-02-20 13:42:08 -0700811 GLuint tex_shadow,
Keith Whitwellce721142005-07-11 10:10:38 +0000812 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000813{
Brian Paul7e807512005-11-05 17:10:45 +0000814 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000815 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000816 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000817 coord, /* arg 0? */
818 undef,
819 undef);
820
Brian Paul7e807512005-11-05 17:10:45 +0000821 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000822 inst->TexSrcUnit = tex_unit;
Brian Paul44e018c2009-02-20 13:42:08 -0700823 inst->TexShadow = tex_shadow;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000824
Brian21f99792007-01-09 11:00:21 -0700825 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000826
Brian Paul5620c202008-09-26 11:18:06 -0600827 /* Accounting for indirection tracking:
828 */
829 reserve_temp(p, dest);
830
Roland Scheidegger114152e2009-03-12 15:01:16 +0100831#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000832 /* Is this a texture indirection?
833 */
834 if ((coord.file == PROGRAM_TEMPORARY &&
835 (p->temps_output & (1<<coord.idx))) ||
836 (dest.file == PROGRAM_TEMPORARY &&
837 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700838 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000839 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000840 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000841 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000842 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100843#endif
Keith Whitwell15e75e02005-04-29 15:11:39 +0000844
845 return dest;
846}
847
848
Keith Whitwell47b29f52005-05-04 11:44:44 +0000849static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000850 GLfloat s0,
851 GLfloat s1,
852 GLfloat s2,
853 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000854{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000855 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700856 GLuint idx, swizzle;
Brian Pauld01269a2008-09-25 18:27:22 -0600857 struct ureg r;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000858 values[0] = s0;
859 values[1] = s1;
860 values[2] = s2;
861 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700862 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
863 &swizzle );
Brian Pauld01269a2008-09-25 18:27:22 -0600864 r = make_ureg(PROGRAM_CONSTANT, idx);
865 r.swz = swizzle;
866 return r;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000867}
868
Keith Whitwelle4902422005-05-11 15:16:35 +0000869#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000870#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
871#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
872#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000873
874
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000875static struct ureg get_one( struct texenv_fragment_program *p )
876{
877 if (is_undef(p->one))
878 p->one = register_scalar_const(p, 1.0);
879 return p->one;
880}
881
882static struct ureg get_half( struct texenv_fragment_program *p )
883{
884 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000885 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000886 return p->half;
887}
888
889static struct ureg get_zero( struct texenv_fragment_program *p )
890{
891 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000892 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000893 return p->zero;
894}
895
Keith Whitwell15e75e02005-04-29 15:11:39 +0000896
Keith Whitwell15e75e02005-04-29 15:11:39 +0000897static void program_error( struct texenv_fragment_program *p, const char *msg )
898{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000899 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000900 p->error = 1;
901}
902
Keith Whitwell15e75e02005-04-29 15:11:39 +0000903static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000904 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000905{
906 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000907 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000908 assert(!is_undef(p->src_texture[unit]));
909 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000910
Keith Whitwellce721142005-07-11 10:10:38 +0000911 case SRC_TEXTURE0:
912 case SRC_TEXTURE1:
913 case SRC_TEXTURE2:
914 case SRC_TEXTURE3:
915 case SRC_TEXTURE4:
916 case SRC_TEXTURE5:
917 case SRC_TEXTURE6:
918 case SRC_TEXTURE7:
919 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
920 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000921
Keith Whitwellce721142005-07-11 10:10:38 +0000922 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000923 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000924
Keith Whitwellce721142005-07-11 10:10:38 +0000925 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000926 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000927
Brian Paul6947f852009-01-23 17:32:09 -0700928 case SRC_ZERO:
929 return get_zero(p);
930
Keith Whitwellce721142005-07-11 10:10:38 +0000931 case SRC_PREVIOUS:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000932 if (is_undef(p->src_previous))
933 return register_input(p, FRAG_ATTRIB_COL0);
934 else
935 return p->src_previous;
Brian Paul6947f852009-01-23 17:32:09 -0700936
937 default:
938 assert(0);
José Fonsecac6af9b22009-06-15 19:20:25 +0100939 return undef;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000940 }
941}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000942
Keith Whitwell15e75e02005-04-29 15:11:39 +0000943static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000944 GLuint mask,
945 GLuint unit,
946 GLuint source,
947 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000948{
949 struct ureg arg, src, one;
950
951 src = get_source(p, source, unit);
952
953 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000954 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000955 /* Get unused tmp,
956 * Emit tmp = 1.0 - arg.xyzw
957 */
958 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000959 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000960 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000961
Keith Whitwellce721142005-07-11 10:10:38 +0000962 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000963 if (mask == WRITEMASK_W)
964 return src;
965 else
Brian Paul22db5352005-11-19 23:45:10 +0000966 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000967 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000968 /* Get unused tmp,
969 * Emit tmp = 1.0 - arg.wwww
970 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000971 arg = get_temp(p);
972 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000973 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000974 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000975 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000976 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000977 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000978 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000979 case OPR_SRC_COLOR:
Brian Paul6947f852009-01-23 17:32:09 -0700980 return src;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000981 default:
Brian Paul6947f852009-01-23 17:32:09 -0700982 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000983 return src;
984 }
985}
986
Brian Paulf337e2c2009-09-01 15:49:55 -0600987/**
988 * Check if the RGB and Alpha sources and operands match for the given
989 * texture unit's combinder state. When the RGB and A sources and
990 * operands match, we can emit fewer instructions.
991 */
Brian Paula5e70032009-08-31 11:17:59 -0600992static GLboolean args_match( const struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000993{
Brian Paul9ed03152009-09-01 15:57:36 -0600994 GLuint i, numArgs = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000995
Brian Paul457e4272009-09-01 16:22:02 -0600996 for (i = 0; i < numArgs; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000997 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000998 return GL_FALSE;
999
Brian Paul9ed03152009-09-01 15:57:36 -06001000 switch (key->unit[unit].OptA[i].Operand) {
Keith Whitwellce721142005-07-11 10:10:38 +00001001 case OPR_SRC_ALPHA:
Brian Paul9ed03152009-09-01 15:57:36 -06001002 switch (key->unit[unit].OptRGB[i].Operand) {
Keith Whitwellce721142005-07-11 10:10:38 +00001003 case OPR_SRC_COLOR:
1004 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001005 break;
1006 default:
1007 return GL_FALSE;
1008 }
1009 break;
Keith Whitwellce721142005-07-11 10:10:38 +00001010 case OPR_ONE_MINUS_SRC_ALPHA:
Brian Paul9ed03152009-09-01 15:57:36 -06001011 switch (key->unit[unit].OptRGB[i].Operand) {
Keith Whitwellce721142005-07-11 10:10:38 +00001012 case OPR_ONE_MINUS_SRC_COLOR:
1013 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001014 break;
1015 default:
1016 return GL_FALSE;
1017 }
1018 break;
1019 default:
1020 return GL_FALSE; /* impossible */
1021 }
1022 }
1023
1024 return GL_TRUE;
1025}
1026
Keith Whitwell15e75e02005-04-29 15:11:39 +00001027static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +00001028 struct ureg dest,
1029 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +00001030 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +00001031 GLuint unit,
1032 GLuint nr,
1033 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +00001034 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001035{
Brian Paulbd9b2be2009-03-08 17:58:54 -06001036 struct ureg src[MAX_COMBINER_TERMS];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001037 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +00001038 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001039
Brian Paulbd9b2be2009-03-08 17:58:54 -06001040 assert(nr <= MAX_COMBINER_TERMS);
Brian Paul6947f852009-01-23 17:32:09 -07001041
Brian Paul00630842005-12-12 15:27:55 +00001042 tmp = undef; /* silence warning (bug 5318) */
1043
Keith Whitwell15e75e02005-04-29 15:11:39 +00001044 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +00001045 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001046
1047 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +00001048 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001049 if (mask == WRITEMASK_XYZW && !saturate)
1050 return src[0];
1051 else
Brian Paul7e807512005-11-05 17:10:45 +00001052 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001053 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +00001054 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001055 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001056 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00001057 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001058 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001059 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001060 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001061 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +00001062 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001063 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +00001064 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +00001065 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
1066 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001067 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +00001068 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001069 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
1070 */
Brian Paul7e807512005-11-05 17:10:45 +00001071 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001072
Keith Whitwellce721142005-07-11 10:10:38 +00001073 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +00001074 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001075
Keith Whitwellce721142005-07-11 10:10:38 +00001076 case MODE_DOT3_RGBA:
1077 case MODE_DOT3_RGBA_EXT:
1078 case MODE_DOT3_RGB_EXT:
1079 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001080 struct ureg tmp0 = get_temp( p );
1081 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +00001082 struct ureg neg1 = register_scalar_const(p, -1);
1083 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001084
1085 /* tmp0 = 2*src0 - 1
1086 * tmp1 = 2*src1 - 1
1087 *
1088 * dst = tmp0 dot3 tmp1
1089 */
Brian Paul7e807512005-11-05 17:10:45 +00001090 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001091 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001092
Brian Paulaa206952005-09-16 18:14:24 +00001093 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001094 tmp1 = tmp0;
1095 else
Brian Paul7e807512005-11-05 17:10:45 +00001096 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001097 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +00001098 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001099 return dest;
1100 }
Keith Whitwellce721142005-07-11 10:10:38 +00001101 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001102 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001103 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001104 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +00001105 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001106 /* Arg0 * Arg2 + Arg1 - 0.5 */
1107 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001108 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +00001109 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
1110 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001111 return dest;
1112 }
Keith Whitwellce721142005-07-11 10:10:38 +00001113 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001114 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001115 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001116 return dest;
Brian Paul6947f852009-01-23 17:32:09 -07001117 case MODE_ADD_PRODUCTS:
1118 /* Arg0 * Arg1 + Arg2 * Arg3 */
1119 {
1120 struct ureg tmp0 = get_temp(p);
1121 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1122 emit_arith( p, OPCODE_MAD, dest, mask, saturate, src[2], src[3], tmp0 );
1123 }
1124 return dest;
1125 case MODE_ADD_PRODUCTS_SIGNED:
1126 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1127 {
1128 struct ureg tmp0 = get_temp(p);
1129 half = get_half(p);
1130 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1131 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[2], src[3], tmp0 );
1132 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1133 }
1134 return dest;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001135 case MODE_BUMP_ENVMAP_ATI:
1136 /* special - not handled here */
1137 assert(0);
1138 return src[0];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001139 default:
Brian Paul6947f852009-01-23 17:32:09 -07001140 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001141 return src[0];
1142 }
1143}
1144
Keith Whitwell15e75e02005-04-29 15:11:39 +00001145
Brian Paul22db5352005-11-19 23:45:10 +00001146/**
1147 * Generate instructions for one texture unit's env/combiner mode.
1148 */
1149static struct ureg
1150emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001151{
Brian Paula5e70032009-08-31 11:17:59 -06001152 const struct state_key *key = p->state;
Brian Paule9ba9ff2009-09-10 08:41:12 -06001153 GLboolean rgb_saturate, alpha_saturate;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001154 GLuint rgb_shift, alpha_shift;
Brian Paula5e70032009-08-31 11:17:59 -06001155 struct ureg out, dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001156
Keith Whitwellce721142005-07-11 10:10:38 +00001157 if (!key->unit[unit].enabled) {
1158 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001159 }
Roland Scheidegger114152e2009-03-12 15:01:16 +01001160 if (key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1161 /* this isn't really a env stage delivering a color and handled elsewhere */
1162 return get_source(p, SRC_PREVIOUS, 0);
1163 }
Keith Whitwellce721142005-07-11 10:10:38 +00001164
1165 switch (key->unit[unit].ModeRGB) {
1166 case MODE_DOT3_RGB_EXT:
1167 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001168 rgb_shift = 0;
1169 break;
Keith Whitwellce721142005-07-11 10:10:38 +00001170 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001171 alpha_shift = 0;
1172 rgb_shift = 0;
1173 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001174 default:
Keith Whitwellce721142005-07-11 10:10:38 +00001175 rgb_shift = key->unit[unit].ScaleShiftRGB;
1176 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001177 break;
1178 }
Keith Whitwellce721142005-07-11 10:10:38 +00001179
Brian Paul956e6c32009-08-31 11:14:16 -06001180 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
1181 * We don't want to clamp twice.
1182 */
Brian Paule9ba9ff2009-09-10 08:41:12 -06001183 if (rgb_shift)
1184 rgb_saturate = GL_FALSE; /* saturate after rgb shift */
1185 else if (need_saturate(key->unit[unit].ModeRGB))
1186 rgb_saturate = GL_TRUE;
1187 else
1188 rgb_saturate = GL_FALSE;
1189
1190 if (alpha_shift)
1191 alpha_saturate = GL_FALSE; /* saturate after alpha shift */
1192 else if (need_saturate(key->unit[unit].ModeA))
1193 alpha_saturate = GL_TRUE;
1194 else
1195 alpha_saturate = GL_FALSE;
Brian Paul956e6c32009-08-31 11:14:16 -06001196
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001197 /* If this is the very last calculation, emit direct to output reg:
1198 */
Keith Whitwellce721142005-07-11 10:10:38 +00001199 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001200 unit != p->last_tex_stage ||
1201 alpha_shift ||
1202 rgb_shift)
1203 dest = get_temp( p );
1204 else
Brian Paul8d475822009-02-28 11:49:46 -07001205 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLOR);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001206
1207 /* Emit the RGB and A combine ops
1208 */
Keith Whitwellce721142005-07-11 10:10:38 +00001209 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
1210 args_match(key, unit)) {
Brian Paule9ba9ff2009-09-10 08:41:12 -06001211 out = emit_combine( p, dest, WRITEMASK_XYZW, rgb_saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001212 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001213 key->unit[unit].NumArgsRGB,
1214 key->unit[unit].ModeRGB,
1215 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001216 }
Keith Whitwellce721142005-07-11 10:10:38 +00001217 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +02001218 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Brian Paule9ba9ff2009-09-10 08:41:12 -06001219 out = emit_combine( p, dest, WRITEMASK_XYZW, rgb_saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001220 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001221 key->unit[unit].NumArgsRGB,
1222 key->unit[unit].ModeRGB,
1223 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001224 }
1225 else {
1226 /* Need to do something to stop from re-emitting identical
1227 * argument calculations here:
1228 */
Brian Paule9ba9ff2009-09-10 08:41:12 -06001229 out = emit_combine( p, dest, WRITEMASK_XYZ, rgb_saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001230 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001231 key->unit[unit].NumArgsRGB,
1232 key->unit[unit].ModeRGB,
1233 key->unit[unit].OptRGB);
Brian Paule9ba9ff2009-09-10 08:41:12 -06001234 out = emit_combine( p, dest, WRITEMASK_W, alpha_saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001235 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001236 key->unit[unit].NumArgsA,
1237 key->unit[unit].ModeA,
1238 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001239 }
1240
1241 /* Deal with the final shift:
1242 */
1243 if (alpha_shift || rgb_shift) {
Brian Paula5e70032009-08-31 11:17:59 -06001244 struct ureg shift;
Brian Paule9ba9ff2009-09-10 08:41:12 -06001245 GLboolean saturate = GL_TRUE; /* always saturate at this point */
Brian Paula5e70032009-08-31 11:17:59 -06001246
Keith Whitwell15e75e02005-04-29 15:11:39 +00001247 if (rgb_shift == alpha_shift) {
José Fonseca452a5922008-05-31 18:14:09 +09001248 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001249 }
1250 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001251 shift = register_const4f(p,
José Fonseca452a5922008-05-31 18:14:09 +09001252 (GLfloat)(1<<rgb_shift),
1253 (GLfloat)(1<<rgb_shift),
1254 (GLfloat)(1<<rgb_shift),
1255 (GLfloat)(1<<alpha_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001256 }
Brian Paul7e807512005-11-05 17:10:45 +00001257 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001258 saturate, out, shift, undef );
1259 }
1260 else
1261 return out;
1262}
1263
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001264
Brian Paul22db5352005-11-19 23:45:10 +00001265/**
1266 * Generate instruction for getting a texture source term.
1267 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001268static void load_texture( struct texenv_fragment_program *p, GLuint unit )
1269{
1270 if (is_undef(p->src_texture[unit])) {
Brian Paulb5ec0a62009-09-01 15:51:23 -06001271 const GLuint texTarget = p->state->unit[unit].source_index;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001272 struct ureg texcoord;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001273 struct ureg tmp = get_tex_temp( p );
1274
Roland Scheidegger114152e2009-03-12 15:01:16 +01001275 if (is_undef(p->texcoord_tex[unit])) {
1276 texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
1277 }
1278 else {
1279 /* might want to reuse this reg for tex output actually */
1280 texcoord = p->texcoord_tex[unit];
1281 }
1282
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001283 /* TODO: Use D0_MASK_XY where possible.
1284 */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +02001285 if (p->state->unit[unit].enabled) {
Brian Paul44e018c2009-02-20 13:42:08 -07001286 GLboolean shadow = GL_FALSE;
1287
1288 if (p->state->unit[unit].shadow) {
1289 p->program->Base.ShadowSamplers |= 1 << unit;
1290 shadow = GL_TRUE;
1291 }
1292
Aapo Tahkolab8915342006-03-28 10:26:34 +00001293 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1294 tmp, WRITEMASK_XYZW,
Brian Paul44e018c2009-02-20 13:42:08 -07001295 unit, texTarget, shadow,
1296 texcoord );
Brian9b7e5a52007-12-14 11:16:49 -07001297
1298 p->program->Base.SamplersUsed |= (1 << unit);
Brian1fe385f2007-12-14 11:42:28 -07001299 /* This identity mapping should already be in place
1300 * (see _mesa_init_program_struct()) but let's be safe.
1301 */
1302 p->program->Base.SamplerUnits[unit] = unit;
Brian9b7e5a52007-12-14 11:16:49 -07001303 }
1304 else
Aapo Tahkolab8915342006-03-28 10:26:34 +00001305 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001306 }
1307}
1308
Keith Whitwell241b6b72005-05-23 09:50:34 +00001309static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +00001310 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001311{
1312 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +00001313 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001314 load_texture(p, unit);
1315 break;
1316
Keith Whitwellce721142005-07-11 10:10:38 +00001317 case SRC_TEXTURE0:
1318 case SRC_TEXTURE1:
1319 case SRC_TEXTURE2:
1320 case SRC_TEXTURE3:
1321 case SRC_TEXTURE4:
1322 case SRC_TEXTURE5:
1323 case SRC_TEXTURE6:
1324 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +00001325 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001326 break;
1327
1328 default:
Brian Paul6947f852009-01-23 17:32:09 -07001329 /* not a texture src - do nothing */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001330 break;
1331 }
Keith Whitwell241b6b72005-05-23 09:50:34 +00001332
1333 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001334}
1335
Brian Paul22db5352005-11-19 23:45:10 +00001336
1337/**
1338 * Generate instructions for loading all texture source terms.
1339 */
1340static GLboolean
Brian Paul457e4272009-09-01 16:22:02 -06001341load_texunit_sources( struct texenv_fragment_program *p, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001342{
Brian Paula5e70032009-08-31 11:17:59 -06001343 const struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +00001344 GLuint i;
1345
1346 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001347 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001348 }
Aapo Tahkolab8915342006-03-28 10:26:34 +00001349
1350 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1351 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1352 }
1353
Keith Whitwell241b6b72005-05-23 09:50:34 +00001354 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001355}
1356
Roland Scheidegger114152e2009-03-12 15:01:16 +01001357/**
1358 * Generate instructions for loading bump map textures.
1359 */
1360static GLboolean
Brian Paul457e4272009-09-01 16:22:02 -06001361load_texunit_bumpmap( struct texenv_fragment_program *p, GLuint unit )
Roland Scheidegger114152e2009-03-12 15:01:16 +01001362{
Brian Paula5e70032009-08-31 11:17:59 -06001363 const struct state_key *key = p->state;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001364 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1365 struct ureg texcDst, bumpMapRes;
1366 struct ureg constdudvcolor = register_const4f(p, 0.0, 0.0, 0.0, 1.0);
1367 struct ureg texcSrc = register_input(p, FRAG_ATTRIB_TEX0 + bumpedUnitNr);
1368 struct ureg rotMat0 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_0, unit );
1369 struct ureg rotMat1 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_1, unit );
1370
1371 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1372
1373 bumpMapRes = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1374 texcDst = get_tex_temp( p );
1375 p->texcoord_tex[bumpedUnitNr] = texcDst;
1376
Brian Paul457e4272009-09-01 16:22:02 -06001377 /* Apply rot matrix and add coords to be available in next phase.
1378 * dest = (Arg0.xxxx * rotMat0 + Arg1) + (Arg0.yyyy * rotMat1)
1379 * note only 2 coords are affected the rest are left unchanged (mul by 0)
1380 */
Roland Scheidegger114152e2009-03-12 15:01:16 +01001381 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1382 swizzle1(bumpMapRes, SWIZZLE_X), rotMat0, texcSrc );
1383 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1384 swizzle1(bumpMapRes, SWIZZLE_Y), rotMat1, texcDst );
1385
Brian Paul457e4272009-09-01 16:22:02 -06001386 /* Move 0,0,0,1 into bumpmap src if someone (crossbar) is foolish
1387 * enough to access this later, should optimize away.
1388 */
1389 emit_arith( p, OPCODE_MOV, bumpMapRes, WRITEMASK_XYZW, 0,
1390 constdudvcolor, undef, undef );
Roland Scheidegger114152e2009-03-12 15:01:16 +01001391
1392 return GL_TRUE;
1393}
Brian Paul22db5352005-11-19 23:45:10 +00001394
1395/**
1396 * Generate a new fragment program which implements the context's
1397 * current texture env/combine mode.
1398 */
1399static void
Brian Paule998c342006-10-29 21:17:18 +00001400create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001401 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001402{
Brian Paule998c342006-10-29 21:17:18 +00001403 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001404 struct texenv_fragment_program p;
1405 GLuint unit;
1406 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001407
Keith Whitwelle4902422005-05-11 15:16:35 +00001408 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwellce721142005-07-11 10:10:38 +00001409 p.state = key;
1410 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001411
Brian Paule998c342006-10-29 21:17:18 +00001412 /* During code generation, use locally-allocated instruction buffer,
1413 * then alloc dynamic storage below.
1414 */
1415 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001416 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Brian Paul457e4272009-09-01 16:22:02 -06001417 p.program->Base.String = NULL;
1418 p.program->Base.NumTexIndirections = 1; /* is this right? */
Brian21f99792007-01-09 11:00:21 -07001419 p.program->Base.NumTexInstructions = 0;
1420 p.program->Base.NumAluInstructions = 0;
Brian Paul457e4272009-09-01 16:22:02 -06001421 p.program->Base.NumInstructions = 0;
1422 p.program->Base.NumTemporaries = 0;
1423 p.program->Base.NumParameters = 0;
1424 p.program->Base.NumAttributes = 0;
1425 p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001426 p.program->Base.Parameters = _mesa_new_parameter_list();
Brian Paul457e4272009-09-01 16:22:02 -06001427 p.program->Base.InputsRead = 0x0;
Brian Paul8d475822009-02-28 11:49:46 -07001428 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLOR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001429
Roland Scheidegger114152e2009-03-12 15:01:16 +01001430 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001431 p.src_texture[unit] = undef;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001432 p.texcoord_tex[unit] = undef;
1433 }
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001434
Keith Whitwell47b29f52005-05-04 11:44:44 +00001435 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001436 p.half = undef;
1437 p.zero = undef;
1438 p.one = undef;
1439
Keith Whitwell15e75e02005-04-29 15:11:39 +00001440 p.last_tex_stage = 0;
Brian93fef222007-10-29 12:25:46 -06001441 release_temps(ctx, &p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001442
Keith Whitwellce721142005-07-11 10:10:38 +00001443 if (key->enabled_units) {
Brian Paul457e4272009-09-01 16:22:02 -06001444 GLboolean needbumpstage = GL_FALSE;
1445
Roland Scheidegger114152e2009-03-12 15:01:16 +01001446 /* Zeroth pass - bump map textures first */
Brian Paul7a7d5872009-09-02 15:39:13 -06001447 for (unit = 0; unit < key->nr_enabled_units; unit++)
Brian Paul457e4272009-09-01 16:22:02 -06001448 if (key->unit[unit].enabled &&
1449 key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001450 needbumpstage = GL_TRUE;
1451 load_texunit_bumpmap( &p, unit );
1452 }
1453 if (needbumpstage)
1454 p.program->Base.NumTexIndirections++;
1455
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001456 /* First pass - to support texture_env_crossbar, first identify
1457 * all referenced texture sources and emit texld instructions
1458 * for each:
1459 */
Brian Paul7a7d5872009-09-02 15:39:13 -06001460 for (unit = 0; unit < key->nr_enabled_units; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001461 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001462 load_texunit_sources( &p, unit );
1463 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001464 }
1465
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001466 /* Second pass - emit combine instructions to build final color:
1467 */
Brian Paul7a7d5872009-09-02 15:39:13 -06001468 for (unit = 0; unit < key->nr_enabled_units; unit++)
Brian Paul84d6bed2009-09-01 16:10:57 -06001469 if (key->unit[unit].enabled) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001470 p.src_previous = emit_texenv( &p, unit );
Brian Paul5620c202008-09-26 11:18:06 -06001471 reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
Brian93fef222007-10-29 12:25:46 -06001472 release_temps(ctx, &p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001473 }
1474 }
1475
Keith Whitwellce721142005-07-11 10:10:38 +00001476 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul8d475822009-02-28 11:49:46 -07001477 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLOR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001478
Keith Whitwellce721142005-07-11 10:10:38 +00001479 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001480 /* Emit specular add.
1481 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001482 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001483 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1484 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001485 }
Brian Paulaa206952005-09-16 18:14:24 +00001486 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001487 /* Will wind up in here if no texture enabled or a couple of
1488 * other scenarios (GL_REPLACE for instance).
1489 */
Brian Paul7e807512005-11-05 17:10:45 +00001490 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001491 }
1492
Keith Whitwell47b29f52005-05-04 11:44:44 +00001493 /* Finish up:
1494 */
Brian Paul7e807512005-11-05 17:10:45 +00001495 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001496
Keith Whitwellce721142005-07-11 10:10:38 +00001497 if (key->fog_enabled) {
1498 /* Pull fog mode from GLcontext, the value in the state key is
1499 * a reduced value and not what is expected in FogOption
1500 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001501 p.program->FogOption = ctx->Fog.Mode;
Brian Paul457e4272009-09-01 16:22:02 -06001502 p.program->Base.InputsRead |= FRAG_BIT_FOGC;
1503 }
1504 else {
Keith Whitwell276330b2005-05-09 17:58:13 +00001505 p.program->FogOption = GL_NONE;
Brian Paul457e4272009-09-01 16:22:02 -06001506 }
Keith Whitwell47b29f52005-05-04 11:44:44 +00001507
Brian21f99792007-01-09 11:00:21 -07001508 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001509 program_error(&p, "Exceeded max nr indirect texture lookups");
1510
Brian21f99792007-01-09 11:00:21 -07001511 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001512 program_error(&p, "Exceeded max TEX instructions");
1513
Brian21f99792007-01-09 11:00:21 -07001514 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001515 program_error(&p, "Exceeded max ALU instructions");
1516
Brian Paul5d7b49f2005-11-19 23:12:20 +00001517 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001518
Brian Paule998c342006-10-29 21:17:18 +00001519 /* Allocate final instruction array */
Brianc81cce72007-09-25 15:18:51 -06001520 p.program->Base.Instructions
1521 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1522 if (!p.program->Base.Instructions) {
Brian Paule998c342006-10-29 21:17:18 +00001523 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1524 "generating tex env program");
1525 return;
1526 }
Brianc81cce72007-09-25 15:18:51 -06001527 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1528 p.program->Base.NumInstructions);
1529
1530 if (p.program->FogOption) {
1531 _mesa_append_fog_code(ctx, p.program);
1532 p.program->FogOption = GL_NONE;
1533 }
1534
Brian Paule998c342006-10-29 21:17:18 +00001535
Keith Whitwelldbeea252005-05-10 13:57:50 +00001536 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001537 */
Brian Paule998c342006-10-29 21:17:18 +00001538 if (ctx->Driver.ProgramStringNotify) {
1539 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1540 &p.program->Base );
1541 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001542
Brian Paule998c342006-10-29 21:17:18 +00001543 if (DISASSEM) {
1544 _mesa_print_program(&p.program->Base);
1545 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001546 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001547}
1548
Brian Paul5d7b49f2005-11-19 23:12:20 +00001549
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001550/**
1551 * Return a fragment program which implements the current
1552 * fixed-function texture, fog and color-sum operations.
1553 */
1554struct gl_fragment_program *
1555_mesa_get_fixed_func_fragment_program(GLcontext *ctx)
Keith Whitwellce721142005-07-11 10:10:38 +00001556{
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001557 struct gl_fragment_program *prog;
1558 struct state_key key;
Brian Paul7a7d5872009-09-02 15:39:13 -06001559 GLuint keySize;
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001560
Brian Paul7a7d5872009-09-02 15:39:13 -06001561 keySize = make_state_key(ctx, &key);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001562
1563 prog = (struct gl_fragment_program *)
1564 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
Brian Paul7a7d5872009-09-02 15:39:13 -06001565 &key, keySize);
Keith Whitwellce721142005-07-11 10:10:38 +00001566
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001567 if (!prog) {
1568 prog = (struct gl_fragment_program *)
1569 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1570
1571 create_new_program(ctx, &key, prog);
1572
1573 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
Brian Paul7a7d5872009-09-02 15:39:13 -06001574 &key, keySize, &prog->Base);
Keith Whitwellce721142005-07-11 10:10:38 +00001575 }
1576
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001577 return prog;
Keith Whitwellce721142005-07-11 10:10:38 +00001578}