blob: 870970a35541a893ee11030786bfdc8c4d403390 [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/**
251 * Translate TEXTURE_x_BIT to TEXTURE_x_INDEX.
252 */
Brian Paule00ac112005-09-15 05:00:45 +0000253static GLuint translate_tex_src_bit( GLbitfield bit )
Keith Whitwellce721142005-07-11 10:10:38 +0000254{
Brian Paulb5ec0a62009-09-01 15:51:23 -0600255 ASSERT(bit);
256 return _mesa_ffs(bit) - 1;
Keith Whitwellce721142005-07-11 10:10:38 +0000257}
258
Brian Paulf337e2c2009-09-01 15:49:55 -0600259
Keith Whitwell1680ef82008-10-03 17:30:59 +0100260#define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
261#define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
262
Brian Paul239617f2008-10-07 11:22:47 -0600263/**
264 * Identify all possible varying inputs. The fragment program will
Keith Whitwell1680ef82008-10-03 17:30:59 +0100265 * never reference non-varying inputs, but will track them via state
266 * constants instead.
267 *
268 * This function figures out all the inputs that the fragment program
269 * has access to. The bitmask is later reduced to just those which
270 * are actually referenced.
271 */
Brian Paul239617f2008-10-07 11:22:47 -0600272static GLbitfield get_fp_input_mask( GLcontext *ctx )
Keith Whitwell1680ef82008-10-03 17:30:59 +0100273{
Eric Anholte5f63c42009-06-02 07:47:20 -0700274 /* _NEW_PROGRAM */
Brian Paulf0b07942008-12-17 14:04:03 -0700275 const GLboolean vertexShader = (ctx->Shader.CurrentProgram &&
Eric Anholt40990d92009-08-03 12:36:52 -0700276 ctx->Shader.CurrentProgram->LinkStatus &&
Brian Paulf0b07942008-12-17 14:04:03 -0700277 ctx->Shader.CurrentProgram->VertexProgram);
278 const GLboolean vertexProgram = ctx->VertexProgram._Enabled;
Brian Paul239617f2008-10-07 11:22:47 -0600279 GLbitfield fp_inputs = 0x0;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100280
Brian Paul6d4d51d2008-10-10 13:39:14 -0600281 if (ctx->VertexProgram._Overriden) {
282 /* Somebody's messing with the vertex program and we don't have
283 * a clue what's happening. Assume that it could be producing
284 * all possible outputs.
285 */
286 fp_inputs = ~0;
287 }
288 else if (ctx->RenderMode == GL_FEEDBACK) {
Eric Anholte5f63c42009-06-02 07:47:20 -0700289 /* _NEW_RENDERMODE */
Brian Paul6d4d51d2008-10-10 13:39:14 -0600290 fp_inputs = (FRAG_BIT_COL0 | FRAG_BIT_TEX0);
291 }
Brian Paulf0b07942008-12-17 14:04:03 -0700292 else if (!(vertexProgram || vertexShader) ||
Brian Pauld7296a12008-12-17 11:29:06 -0700293 !ctx->VertexProgram._Current) {
Brian Paulf0b07942008-12-17 14:04:03 -0700294 /* Fixed function vertex logic */
Eric Anholte5f63c42009-06-02 07:47:20 -0700295 /* _NEW_ARRAY */
Brian Paul239617f2008-10-07 11:22:47 -0600296 GLbitfield varying_inputs = ctx->varying_vp_inputs;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100297
Keith Whitwell97e63432008-10-20 13:03:45 +0100298 /* These get generated in the setup routine regardless of the
299 * vertex program:
300 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700301 /* _NEW_POINT */
Keith Whitwell97e63432008-10-20 13:03:45 +0100302 if (ctx->Point.PointSprite)
303 varying_inputs |= FRAG_BITS_TEX_ANY;
304
Keith Whitwell1680ef82008-10-03 17:30:59 +0100305 /* First look at what values may be computed by the generated
306 * vertex program:
307 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700308 /* _NEW_LIGHT */
Keith Whitwell1680ef82008-10-03 17:30:59 +0100309 if (ctx->Light.Enabled) {
310 fp_inputs |= FRAG_BIT_COL0;
311
Eric Anholt9cea84b2009-07-16 18:41:03 -0700312 if (texenv_doing_secondary_color(ctx))
Keith Whitwell1680ef82008-10-03 17:30:59 +0100313 fp_inputs |= FRAG_BIT_COL1;
314 }
315
Eric Anholte5f63c42009-06-02 07:47:20 -0700316 /* _NEW_TEXTURE */
Keith Whitwell1680ef82008-10-03 17:30:59 +0100317 fp_inputs |= (ctx->Texture._TexGenEnabled |
318 ctx->Texture._TexMatEnabled) << FRAG_ATTRIB_TEX0;
319
320 /* Then look at what might be varying as a result of enabled
321 * arrays, etc:
322 */
Brian Paul6807d962009-08-07 09:42:28 -0600323 if (varying_inputs & VERT_BIT_COLOR0)
324 fp_inputs |= FRAG_BIT_COL0;
325 if (varying_inputs & VERT_BIT_COLOR1)
326 fp_inputs |= FRAG_BIT_COL1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100327
328 fp_inputs |= (((varying_inputs & VERT_BIT_TEX_ANY) >> VERT_ATTRIB_TEX0)
329 << FRAG_ATTRIB_TEX0);
330
331 }
332 else {
333 /* calculate from vp->outputs */
Brian Paul2389c052008-12-17 19:01:34 -0700334 struct gl_vertex_program *vprog;
335 GLbitfield vp_outputs;
336
337 /* Choose GLSL vertex shader over ARB vertex program. Need this
338 * since vertex shader state validation comes after fragment state
339 * validation (see additional comments in state.c).
340 */
341 if (vertexShader)
342 vprog = ctx->Shader.CurrentProgram->VertexProgram;
343 else
Eric Anholta9ba1bf2009-08-03 12:38:56 -0700344 vprog = ctx->VertexProgram.Current;
Brian Paul2389c052008-12-17 19:01:34 -0700345
346 vp_outputs = vprog->Base.OutputsWritten;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100347
Keith Whitwell97e63432008-10-20 13:03:45 +0100348 /* These get generated in the setup routine regardless of the
349 * vertex program:
350 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700351 /* _NEW_POINT */
Keith Whitwell97e63432008-10-20 13:03:45 +0100352 if (ctx->Point.PointSprite)
353 vp_outputs |= FRAG_BITS_TEX_ANY;
354
Brian Paul6807d962009-08-07 09:42:28 -0600355 if (vp_outputs & (1 << VERT_RESULT_COL0))
356 fp_inputs |= FRAG_BIT_COL0;
357 if (vp_outputs & (1 << VERT_RESULT_COL1))
358 fp_inputs |= FRAG_BIT_COL1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100359
Keith Whitwell0370d6b2008-10-04 12:41:56 +0100360 fp_inputs |= (((vp_outputs & VERT_RESULT_TEX_ANY) >> VERT_RESULT_TEX0)
361 << FRAG_ATTRIB_TEX0);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100362 }
363
364 return fp_inputs;
365}
366
367
Brian Paul22db5352005-11-19 23:45:10 +0000368/**
369 * Examine current texture environment state and generate a unique
370 * key to identify it.
371 */
Brian Paul7a7d5872009-09-02 15:39:13 -0600372static GLuint make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000373{
Keith Whitwellce721142005-07-11 10:10:38 +0000374 GLuint i, j;
Brian Paul239617f2008-10-07 11:22:47 -0600375 GLbitfield inputs_referenced = FRAG_BIT_COL0;
Brian Paulf337e2c2009-09-01 15:49:55 -0600376 const GLbitfield inputs_available = get_fp_input_mask( ctx );
Brian Paul7a7d5872009-09-02 15:39:13 -0600377 GLuint keySize;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100378
Keith Whitwell8065c122006-05-22 16:09:27 +0000379 memset(key, 0, sizeof(*key));
380
Eric Anholte5f63c42009-06-02 07:47:20 -0700381 /* _NEW_TEXTURE */
Brian Paule9b34882008-12-31 11:54:02 -0700382 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000383 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Brian Paulf337e2c2009-09-01 15:49:55 -0600384 const struct gl_texture_object *texObj = texUnit->_Current;
385 const struct gl_tex_env_combine_state *comb = texUnit->_CurrentCombine;
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800386 GLenum format;
Roland Scheideggerbf4a0fa2008-02-15 17:26:06 +0100387
388 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
Keith Whitwellce721142005-07-11 10:10:38 +0000389 continue;
390
Brian Paulf337e2c2009-09-01 15:49:55 -0600391 format = texObj->Image[0][texObj->BaseLevel]->_BaseFormat;
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800392
Keith Whitwellce721142005-07-11 10:10:38 +0000393 key->unit[i].enabled = 1;
394 key->enabled_units |= (1<<i);
Brian Paul7a7d5872009-09-02 15:39:13 -0600395 key->nr_enabled_units = i + 1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100396 inputs_referenced |= FRAG_BIT_TEX(i);
Keith Whitwellce721142005-07-11 10:10:38 +0000397
Brian Paulf337e2c2009-09-01 15:49:55 -0600398 key->unit[i].source_index =
399 translate_tex_src_bit(texUnit->_ReallyEnabled);
400
401 key->unit[i].shadow = ((texObj->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800402 ((format == GL_DEPTH_COMPONENT) ||
403 (format == GL_DEPTH_STENCIL_EXT)));
Keith Whitwellce721142005-07-11 10:10:38 +0000404
Brian Paulf337e2c2009-09-01 15:49:55 -0600405 key->unit[i].NumArgsRGB = comb->_NumArgsRGB;
406 key->unit[i].NumArgsA = comb->_NumArgsA;
Keith Whitwellce721142005-07-11 10:10:38 +0000407
408 key->unit[i].ModeRGB =
Brian Paulf337e2c2009-09-01 15:49:55 -0600409 translate_mode(texUnit->EnvMode, comb->ModeRGB);
Keith Whitwellce721142005-07-11 10:10:38 +0000410 key->unit[i].ModeA =
Brian Paulf337e2c2009-09-01 15:49:55 -0600411 translate_mode(texUnit->EnvMode, comb->ModeA);
Roland Scheidegger114152e2009-03-12 15:01:16 +0100412
Brian Paulf337e2c2009-09-01 15:49:55 -0600413 key->unit[i].ScaleShiftRGB = comb->ScaleShiftRGB;
414 key->unit[i].ScaleShiftA = comb->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000415
Brian Paulbd9b2be2009-03-08 17:58:54 -0600416 for (j = 0; j < MAX_COMBINER_TERMS; j++) {
Brian Paulf337e2c2009-09-01 15:49:55 -0600417 key->unit[i].OptRGB[j].Operand = translate_operand(comb->OperandRGB[j]);
418 key->unit[i].OptA[j].Operand = translate_operand(comb->OperandA[j]);
419 key->unit[i].OptRGB[j].Source = translate_source(comb->SourceRGB[j]);
420 key->unit[i].OptA[j].Source = translate_source(comb->SourceA[j]);
Keith Whitwellce721142005-07-11 10:10:38 +0000421 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100422
423 if (key->unit[i].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
424 /* requires some special translation */
425 key->unit[i].NumArgsRGB = 2;
426 key->unit[i].ScaleShiftRGB = 0;
427 key->unit[i].OptRGB[0].Operand = OPR_SRC_COLOR;
428 key->unit[i].OptRGB[0].Source = SRC_TEXTURE;
429 key->unit[i].OptRGB[1].Operand = OPR_SRC_COLOR;
430 key->unit[i].OptRGB[1].Source = texUnit->BumpTarget - GL_TEXTURE0 + SRC_TEXTURE0;
431 }
Keith Whitwellce721142005-07-11 10:10:38 +0000432 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100433
Eric Anholt9cea84b2009-07-16 18:41:03 -0700434 /* _NEW_LIGHT | _NEW_FOG */
435 if (texenv_doing_secondary_color(ctx)) {
Keith Whitwellce721142005-07-11 10:10:38 +0000436 key->separate_specular = 1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100437 inputs_referenced |= FRAG_BIT_COL1;
438 }
Keith Whitwellce721142005-07-11 10:10:38 +0000439
Eric Anholte5f63c42009-06-02 07:47:20 -0700440 /* _NEW_FOG */
Keith Whitwellce721142005-07-11 10:10:38 +0000441 if (ctx->Fog.Enabled) {
442 key->fog_enabled = 1;
443 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100444 inputs_referenced |= FRAG_BIT_FOGC; /* maybe */
Keith Whitwellce721142005-07-11 10:10:38 +0000445 }
Keith Whitwell1680ef82008-10-03 17:30:59 +0100446
447 key->inputs_available = (inputs_available & inputs_referenced);
Brian Paul7a7d5872009-09-02 15:39:13 -0600448
449 /* compute size of state key, ignoring unused texture units */
450 keySize = sizeof(*key) - sizeof(key->unit)
451 + key->nr_enabled_units * sizeof(key->unit[0]);
452
453 return keySize;
Keith Whitwellce721142005-07-11 10:10:38 +0000454}
455
Brian Paul7a7d5872009-09-02 15:39:13 -0600456
Brian Paul239617f2008-10-07 11:22:47 -0600457/**
458 * Use uregs to represent registers internally, translate to Mesa's
Keith Whitwell15e75e02005-04-29 15:11:39 +0000459 * expected formats on emit.
460 *
461 * NOTE: These are passed by value extensively in this file rather
462 * than as usual by pointer reference. If this disturbs you, try
463 * remembering they are just 32bits in size.
464 *
465 * GCC is smart enough to deal with these dword-sized structures in
466 * much the same way as if I had defined them as dwords and was using
467 * macros to access and set the fields. This is much nicer and easier
468 * to evolve.
469 */
470struct ureg {
471 GLuint file:4;
472 GLuint idx:8;
473 GLuint negatebase:1;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000474 GLuint swz:12;
Brian Paul1480bca2009-09-01 16:01:12 -0600475 GLuint pad:7;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000476};
477
Brian Paul13abf912006-04-13 19:17:13 +0000478static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000479 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000480 ~0,
481 0,
482 0,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000483 0
484};
485
Keith Whitwell15e75e02005-04-29 15:11:39 +0000486
Brian Paul239617f2008-10-07 11:22:47 -0600487/** State used to build the fragment program:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000488 */
489struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000490 struct gl_fragment_program *program;
Keith Whitwellce721142005-07-11 10:10:38 +0000491 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000492
Brian Paul239617f2008-10-07 11:22:47 -0600493 GLbitfield alu_temps; /**< Track texture indirections, see spec. */
494 GLbitfield temps_output; /**< Track texture indirections, see spec. */
495 GLbitfield temp_in_use; /**< Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000496 GLboolean error;
497
Brian Paule9b34882008-12-31 11:54:02 -0700498 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000499 /* Reg containing each texture unit's sampled texture color,
500 * else undef.
501 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000502
Roland Scheidegger114152e2009-03-12 15:01:16 +0100503 struct ureg texcoord_tex[MAX_TEXTURE_COORD_UNITS];
504 /* Reg containing texcoord for a texture unit,
505 * needed for bump mapping, else undef.
506 */
507
Brian Paul239617f2008-10-07 11:22:47 -0600508 struct ureg src_previous; /**< Reg containing color from previous
Keith Whitwell15e75e02005-04-29 15:11:39 +0000509 * stage. May need to be decl'd.
510 */
511
Brian Paul239617f2008-10-07 11:22:47 -0600512 GLuint last_tex_stage; /**< Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000513
514 struct ureg half;
515 struct ureg one;
516 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000517};
518
519
520
521static struct ureg make_ureg(GLuint file, GLuint idx)
522{
523 struct ureg reg;
524 reg.file = file;
525 reg.idx = idx;
526 reg.negatebase = 0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000527 reg.swz = SWIZZLE_NOOP;
528 reg.pad = 0;
529 return reg;
530}
531
532static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
533{
534 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
535 GET_SWZ(reg.swz, y),
536 GET_SWZ(reg.swz, z),
537 GET_SWZ(reg.swz, w));
538
539 return reg;
540}
541
542static struct ureg swizzle1( struct ureg reg, int x )
543{
544 return swizzle(reg, x, x, x, x);
545}
546
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000547static struct ureg negate( struct ureg reg )
548{
549 reg.negatebase ^= 1;
550 return reg;
551}
552
Keith Whitwell15e75e02005-04-29 15:11:39 +0000553static GLboolean is_undef( struct ureg reg )
554{
Alan Hourihane8d972652006-08-10 13:12:00 +0000555 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000556}
557
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000558
Keith Whitwell15e75e02005-04-29 15:11:39 +0000559static struct ureg get_temp( struct texenv_fragment_program *p )
560{
Brian Paul13abf912006-04-13 19:17:13 +0000561 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000562
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000563 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000564 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000565 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000566
567 /* Then any unused temporary:
568 */
569 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000570 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000571
Keith Whitwell15e75e02005-04-29 15:11:39 +0000572 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000573 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000574 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000575 }
576
Brian Paul13abf912006-04-13 19:17:13 +0000577 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000578 p->program->Base.NumTemporaries = bit;
579
Keith Whitwell93cd9232005-05-11 08:30:23 +0000580 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000581 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
582}
583
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000584static struct ureg get_tex_temp( struct texenv_fragment_program *p )
585{
586 int bit;
587
Brian Pauld01269a2008-09-25 18:27:22 -0600588 /* First try to find available temp not previously used (to avoid
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000589 * starting a new texture indirection). According to the spec, the
590 * ~p->temps_output isn't necessary, but will keep it there for
591 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000592 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000593 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000594
595 /* Then any unused temporary:
596 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000597 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000598 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000599
600 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000601 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000602 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000603 }
604
Brian Paul13abf912006-04-13 19:17:13 +0000605 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000606 p->program->Base.NumTemporaries = bit;
607
Keith Whitwell93cd9232005-05-11 08:30:23 +0000608 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000609 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
610}
611
Keith Whitwell15e75e02005-04-29 15:11:39 +0000612
Brian Paul5620c202008-09-26 11:18:06 -0600613/** Mark a temp reg as being no longer allocatable. */
614static void reserve_temp( struct texenv_fragment_program *p, struct ureg r )
615{
616 if (r.file == PROGRAM_TEMPORARY)
617 p->temps_output |= (1 << r.idx);
618}
619
620
Brian93fef222007-10-29 12:25:46 -0600621static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000622{
Brian93fef222007-10-29 12:25:46 -0600623 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000624
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000625 /* KW: To support tex_env_crossbar, don't release the registers in
626 * temps_output.
627 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000628 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000629 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000630 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000631 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000632}
633
634
Brian9fe3e2e2007-02-23 11:44:14 -0700635static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000636 GLint s0,
637 GLint s1,
638 GLint s2,
639 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700640 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000641{
Brian9fe3e2e2007-02-23 11:44:14 -0700642 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000643 GLuint idx;
644 tokens[0] = s0;
645 tokens[1] = s1;
646 tokens[2] = s2;
647 tokens[3] = s3;
648 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000649 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000650 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000651}
652
Keith Whitwell47b29f52005-05-04 11:44:44 +0000653
Brian9fe3e2e2007-02-23 11:44:14 -0700654#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
655#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
656#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
657#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000658
Keith Whitwell1680ef82008-10-03 17:30:59 +0100659static GLuint frag_to_vert_attrib( GLuint attrib )
660{
661 switch (attrib) {
662 case FRAG_ATTRIB_COL0: return VERT_ATTRIB_COLOR0;
663 case FRAG_ATTRIB_COL1: return VERT_ATTRIB_COLOR1;
664 default:
665 assert(attrib >= FRAG_ATTRIB_TEX0);
666 assert(attrib <= FRAG_ATTRIB_TEX7);
667 return attrib - FRAG_ATTRIB_TEX0 + VERT_ATTRIB_TEX0;
668 }
669}
670
Keith Whitwell47b29f52005-05-04 11:44:44 +0000671
672static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
673{
Keith Whitwell1680ef82008-10-03 17:30:59 +0100674 if (p->state->inputs_available & (1<<input)) {
675 p->program->Base.InputsRead |= (1 << input);
676 return make_ureg(PROGRAM_INPUT, input);
677 }
678 else {
679 GLuint idx = frag_to_vert_attrib( input );
680 return register_param3( p, STATE_INTERNAL, STATE_CURRENT_ATTRIB, idx );
681 }
Keith Whitwell47b29f52005-05-04 11:44:44 +0000682}
683
684
Brian Paul7e807512005-11-05 17:10:45 +0000685static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000686 struct ureg ureg )
687{
688 reg->File = ureg.file;
689 reg->Index = ureg.idx;
690 reg->Swizzle = ureg.swz;
Brian Paul7db7ff82009-04-14 22:14:30 -0600691 reg->Negate = ureg.negatebase ? NEGATE_XYZW : NEGATE_NONE;
Brian Paul1480bca2009-09-01 16:01:12 -0600692 reg->Abs = GL_FALSE;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000693}
694
Brian Paul7e807512005-11-05 17:10:45 +0000695static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000696 struct ureg ureg, GLuint mask )
697{
698 dst->File = ureg.file;
699 dst->Index = ureg.idx;
700 dst->WriteMask = mask;
Brianc9d495c2007-10-23 10:55:24 -0600701 dst->CondMask = COND_TR; /* always pass cond test */
702 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000703}
704
Brian Paul7e807512005-11-05 17:10:45 +0000705static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000706emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000707 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000708 struct ureg dest,
709 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000710 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000711 struct ureg src0,
712 struct ureg src1,
713 struct ureg src2 )
714{
Brian Paul9ed03152009-09-01 15:57:36 -0600715 const GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000716 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Brian2a8e9bb2007-10-23 10:24:53 -0600717
718 assert(nr < MAX_INSTRUCTIONS);
719
Brian Pauld6272e02006-10-29 18:03:16 +0000720 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000721 inst->Opcode = op;
722
Keith Whitwell47b29f52005-05-04 11:44:44 +0000723 emit_arg( &inst->SrcReg[0], src0 );
724 emit_arg( &inst->SrcReg[1], src1 );
725 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000726
Brian Paule31ac052005-11-20 17:52:40 +0000727 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000728
729 emit_dst( &inst->DstReg, dest, mask );
730
Brian Paul5620c202008-09-26 11:18:06 -0600731#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000732 /* Accounting for indirection tracking:
733 */
734 if (dest.file == PROGRAM_TEMPORARY)
735 p->temps_output |= 1 << dest.idx;
Brian Paul5620c202008-09-26 11:18:06 -0600736#endif
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000737
Keith Whitwell15e75e02005-04-29 15:11:39 +0000738 return inst;
739}
740
741
742static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000743 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000744 struct ureg dest,
745 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000746 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000747 struct ureg src0,
748 struct ureg src1,
749 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000750{
751 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
752
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000753 /* Accounting for indirection tracking:
754 */
755 if (src0.file == PROGRAM_TEMPORARY)
756 p->alu_temps |= 1 << src0.idx;
757
758 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
759 p->alu_temps |= 1 << src1.idx;
760
761 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
762 p->alu_temps |= 1 << src2.idx;
763
764 if (dest.file == PROGRAM_TEMPORARY)
765 p->alu_temps |= 1 << dest.idx;
766
Brian21f99792007-01-09 11:00:21 -0700767 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000768 return dest;
769}
770
771static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000772 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000773 struct ureg dest,
774 GLuint destmask,
775 GLuint tex_unit,
776 GLuint tex_idx,
Brian Paul44e018c2009-02-20 13:42:08 -0700777 GLuint tex_shadow,
Keith Whitwellce721142005-07-11 10:10:38 +0000778 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000779{
Brian Paul7e807512005-11-05 17:10:45 +0000780 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000781 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000782 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000783 coord, /* arg 0? */
784 undef,
785 undef);
786
Brian Paul7e807512005-11-05 17:10:45 +0000787 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000788 inst->TexSrcUnit = tex_unit;
Brian Paul44e018c2009-02-20 13:42:08 -0700789 inst->TexShadow = tex_shadow;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000790
Brian21f99792007-01-09 11:00:21 -0700791 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000792
Brian Paul5620c202008-09-26 11:18:06 -0600793 /* Accounting for indirection tracking:
794 */
795 reserve_temp(p, dest);
796
Roland Scheidegger114152e2009-03-12 15:01:16 +0100797#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000798 /* Is this a texture indirection?
799 */
800 if ((coord.file == PROGRAM_TEMPORARY &&
801 (p->temps_output & (1<<coord.idx))) ||
802 (dest.file == PROGRAM_TEMPORARY &&
803 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700804 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000805 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000806 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000807 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000808 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100809#endif
Keith Whitwell15e75e02005-04-29 15:11:39 +0000810
811 return dest;
812}
813
814
Keith Whitwell47b29f52005-05-04 11:44:44 +0000815static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000816 GLfloat s0,
817 GLfloat s1,
818 GLfloat s2,
819 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000820{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000821 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700822 GLuint idx, swizzle;
Brian Pauld01269a2008-09-25 18:27:22 -0600823 struct ureg r;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000824 values[0] = s0;
825 values[1] = s1;
826 values[2] = s2;
827 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700828 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
829 &swizzle );
Brian Pauld01269a2008-09-25 18:27:22 -0600830 r = make_ureg(PROGRAM_CONSTANT, idx);
831 r.swz = swizzle;
832 return r;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000833}
834
Keith Whitwelle4902422005-05-11 15:16:35 +0000835#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000836#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
837#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
838#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000839
840
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000841static struct ureg get_one( struct texenv_fragment_program *p )
842{
843 if (is_undef(p->one))
844 p->one = register_scalar_const(p, 1.0);
845 return p->one;
846}
847
848static struct ureg get_half( struct texenv_fragment_program *p )
849{
850 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000851 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000852 return p->half;
853}
854
855static struct ureg get_zero( struct texenv_fragment_program *p )
856{
857 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000858 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000859 return p->zero;
860}
861
Keith Whitwell15e75e02005-04-29 15:11:39 +0000862
Keith Whitwell15e75e02005-04-29 15:11:39 +0000863static void program_error( struct texenv_fragment_program *p, const char *msg )
864{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000865 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000866 p->error = 1;
867}
868
Keith Whitwell15e75e02005-04-29 15:11:39 +0000869static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000870 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000871{
872 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000873 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000874 assert(!is_undef(p->src_texture[unit]));
875 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000876
Keith Whitwellce721142005-07-11 10:10:38 +0000877 case SRC_TEXTURE0:
878 case SRC_TEXTURE1:
879 case SRC_TEXTURE2:
880 case SRC_TEXTURE3:
881 case SRC_TEXTURE4:
882 case SRC_TEXTURE5:
883 case SRC_TEXTURE6:
884 case SRC_TEXTURE7:
885 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
886 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000887
Keith Whitwellce721142005-07-11 10:10:38 +0000888 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000889 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000890
Keith Whitwellce721142005-07-11 10:10:38 +0000891 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000892 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000893
Brian Paul6947f852009-01-23 17:32:09 -0700894 case SRC_ZERO:
895 return get_zero(p);
896
Keith Whitwellce721142005-07-11 10:10:38 +0000897 case SRC_PREVIOUS:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000898 if (is_undef(p->src_previous))
899 return register_input(p, FRAG_ATTRIB_COL0);
900 else
901 return p->src_previous;
Brian Paul6947f852009-01-23 17:32:09 -0700902
903 default:
904 assert(0);
José Fonsecac6af9b22009-06-15 19:20:25 +0100905 return undef;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000906 }
907}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000908
Keith Whitwell15e75e02005-04-29 15:11:39 +0000909static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000910 GLuint mask,
911 GLuint unit,
912 GLuint source,
913 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000914{
915 struct ureg arg, src, one;
916
917 src = get_source(p, source, unit);
918
919 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000920 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000921 /* Get unused tmp,
922 * Emit tmp = 1.0 - arg.xyzw
923 */
924 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000925 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000926 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000927
Keith Whitwellce721142005-07-11 10:10:38 +0000928 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000929 if (mask == WRITEMASK_W)
930 return src;
931 else
Brian Paul22db5352005-11-19 23:45:10 +0000932 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000933 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000934 /* Get unused tmp,
935 * Emit tmp = 1.0 - arg.wwww
936 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000937 arg = get_temp(p);
938 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000939 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000940 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000941 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000942 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000943 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000944 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000945 case OPR_SRC_COLOR:
Brian Paul6947f852009-01-23 17:32:09 -0700946 return src;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000947 default:
Brian Paul6947f852009-01-23 17:32:09 -0700948 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000949 return src;
950 }
951}
952
Brian Paulf337e2c2009-09-01 15:49:55 -0600953/**
954 * Check if the RGB and Alpha sources and operands match for the given
955 * texture unit's combinder state. When the RGB and A sources and
956 * operands match, we can emit fewer instructions.
957 */
Brian Paula5e70032009-08-31 11:17:59 -0600958static GLboolean args_match( const struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000959{
Brian Paul9ed03152009-09-01 15:57:36 -0600960 GLuint i, numArgs = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000961
Brian Paul457e4272009-09-01 16:22:02 -0600962 for (i = 0; i < numArgs; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000963 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000964 return GL_FALSE;
965
Brian Paul9ed03152009-09-01 15:57:36 -0600966 switch (key->unit[unit].OptA[i].Operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000967 case OPR_SRC_ALPHA:
Brian Paul9ed03152009-09-01 15:57:36 -0600968 switch (key->unit[unit].OptRGB[i].Operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000969 case OPR_SRC_COLOR:
970 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000971 break;
972 default:
973 return GL_FALSE;
974 }
975 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000976 case OPR_ONE_MINUS_SRC_ALPHA:
Brian Paul9ed03152009-09-01 15:57:36 -0600977 switch (key->unit[unit].OptRGB[i].Operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000978 case OPR_ONE_MINUS_SRC_COLOR:
979 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000980 break;
981 default:
982 return GL_FALSE;
983 }
984 break;
985 default:
986 return GL_FALSE; /* impossible */
987 }
988 }
989
990 return GL_TRUE;
991}
992
Keith Whitwell15e75e02005-04-29 15:11:39 +0000993static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000994 struct ureg dest,
995 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000996 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000997 GLuint unit,
998 GLuint nr,
999 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +00001000 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001001{
Brian Paulbd9b2be2009-03-08 17:58:54 -06001002 struct ureg src[MAX_COMBINER_TERMS];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001003 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +00001004 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001005
Brian Paulbd9b2be2009-03-08 17:58:54 -06001006 assert(nr <= MAX_COMBINER_TERMS);
Brian Paul6947f852009-01-23 17:32:09 -07001007
Brian Paul00630842005-12-12 15:27:55 +00001008 tmp = undef; /* silence warning (bug 5318) */
1009
Keith Whitwell15e75e02005-04-29 15:11:39 +00001010 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +00001011 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001012
1013 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +00001014 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001015 if (mask == WRITEMASK_XYZW && !saturate)
1016 return src[0];
1017 else
Brian Paul7e807512005-11-05 17:10:45 +00001018 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001019 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +00001020 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001021 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001022 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00001023 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001024 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001025 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001026 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001027 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +00001028 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001029 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +00001030 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +00001031 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
1032 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001033 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +00001034 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001035 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
1036 */
Brian Paul7e807512005-11-05 17:10:45 +00001037 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001038
Keith Whitwellce721142005-07-11 10:10:38 +00001039 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +00001040 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001041
Keith Whitwellce721142005-07-11 10:10:38 +00001042 case MODE_DOT3_RGBA:
1043 case MODE_DOT3_RGBA_EXT:
1044 case MODE_DOT3_RGB_EXT:
1045 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001046 struct ureg tmp0 = get_temp( p );
1047 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +00001048 struct ureg neg1 = register_scalar_const(p, -1);
1049 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001050
1051 /* tmp0 = 2*src0 - 1
1052 * tmp1 = 2*src1 - 1
1053 *
1054 * dst = tmp0 dot3 tmp1
1055 */
Brian Paul7e807512005-11-05 17:10:45 +00001056 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001057 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001058
Brian Paulaa206952005-09-16 18:14:24 +00001059 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001060 tmp1 = tmp0;
1061 else
Brian Paul7e807512005-11-05 17:10:45 +00001062 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001063 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +00001064 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001065 return dest;
1066 }
Keith Whitwellce721142005-07-11 10:10:38 +00001067 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001068 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001069 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001070 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +00001071 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001072 /* Arg0 * Arg2 + Arg1 - 0.5 */
1073 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001074 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +00001075 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
1076 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001077 return dest;
1078 }
Keith Whitwellce721142005-07-11 10:10:38 +00001079 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001080 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001081 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001082 return dest;
Brian Paul6947f852009-01-23 17:32:09 -07001083 case MODE_ADD_PRODUCTS:
1084 /* Arg0 * Arg1 + Arg2 * Arg3 */
1085 {
1086 struct ureg tmp0 = get_temp(p);
1087 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1088 emit_arith( p, OPCODE_MAD, dest, mask, saturate, src[2], src[3], tmp0 );
1089 }
1090 return dest;
1091 case MODE_ADD_PRODUCTS_SIGNED:
1092 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1093 {
1094 struct ureg tmp0 = get_temp(p);
1095 half = get_half(p);
1096 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1097 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[2], src[3], tmp0 );
1098 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1099 }
1100 return dest;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001101 case MODE_BUMP_ENVMAP_ATI:
1102 /* special - not handled here */
1103 assert(0);
1104 return src[0];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001105 default:
Brian Paul6947f852009-01-23 17:32:09 -07001106 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001107 return src[0];
1108 }
1109}
1110
Keith Whitwell15e75e02005-04-29 15:11:39 +00001111
Brian Paul22db5352005-11-19 23:45:10 +00001112/**
1113 * Generate instructions for one texture unit's env/combiner mode.
1114 */
1115static struct ureg
1116emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001117{
Brian Paula5e70032009-08-31 11:17:59 -06001118 const struct state_key *key = p->state;
Brian Paul956e6c32009-08-31 11:14:16 -06001119 GLboolean saturate;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001120 GLuint rgb_shift, alpha_shift;
Brian Paula5e70032009-08-31 11:17:59 -06001121 struct ureg out, dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001122
Keith Whitwellce721142005-07-11 10:10:38 +00001123 if (!key->unit[unit].enabled) {
1124 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001125 }
Roland Scheidegger114152e2009-03-12 15:01:16 +01001126 if (key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1127 /* this isn't really a env stage delivering a color and handled elsewhere */
1128 return get_source(p, SRC_PREVIOUS, 0);
1129 }
Keith Whitwellce721142005-07-11 10:10:38 +00001130
1131 switch (key->unit[unit].ModeRGB) {
1132 case MODE_DOT3_RGB_EXT:
1133 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001134 rgb_shift = 0;
1135 break;
Keith Whitwellce721142005-07-11 10:10:38 +00001136 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001137 alpha_shift = 0;
1138 rgb_shift = 0;
1139 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001140 default:
Keith Whitwellce721142005-07-11 10:10:38 +00001141 rgb_shift = key->unit[unit].ScaleShiftRGB;
1142 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001143 break;
1144 }
Keith Whitwellce721142005-07-11 10:10:38 +00001145
Brian Paul956e6c32009-08-31 11:14:16 -06001146 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
1147 * We don't want to clamp twice.
1148 */
1149 saturate = !(rgb_shift || alpha_shift);
1150
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001151 /* If this is the very last calculation, emit direct to output reg:
1152 */
Keith Whitwellce721142005-07-11 10:10:38 +00001153 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001154 unit != p->last_tex_stage ||
1155 alpha_shift ||
1156 rgb_shift)
1157 dest = get_temp( p );
1158 else
Brian Paul8d475822009-02-28 11:49:46 -07001159 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLOR);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001160
1161 /* Emit the RGB and A combine ops
1162 */
Keith Whitwellce721142005-07-11 10:10:38 +00001163 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
1164 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001165 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1166 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001167 key->unit[unit].NumArgsRGB,
1168 key->unit[unit].ModeRGB,
1169 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001170 }
Keith Whitwellce721142005-07-11 10:10:38 +00001171 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +02001172 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001173 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1174 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001175 key->unit[unit].NumArgsRGB,
1176 key->unit[unit].ModeRGB,
1177 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001178 }
1179 else {
1180 /* Need to do something to stop from re-emitting identical
1181 * argument calculations here:
1182 */
1183 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
1184 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001185 key->unit[unit].NumArgsRGB,
1186 key->unit[unit].ModeRGB,
1187 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001188 out = emit_combine( p, dest, WRITEMASK_W, saturate,
1189 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001190 key->unit[unit].NumArgsA,
1191 key->unit[unit].ModeA,
1192 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001193 }
1194
1195 /* Deal with the final shift:
1196 */
1197 if (alpha_shift || rgb_shift) {
Brian Paula5e70032009-08-31 11:17:59 -06001198 struct ureg shift;
1199
Brian Paul956e6c32009-08-31 11:14:16 -06001200 saturate = GL_TRUE; /* always saturate at this point */
Brian Paula5e70032009-08-31 11:17:59 -06001201
Keith Whitwell15e75e02005-04-29 15:11:39 +00001202 if (rgb_shift == alpha_shift) {
José Fonseca452a5922008-05-31 18:14:09 +09001203 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001204 }
1205 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001206 shift = register_const4f(p,
José Fonseca452a5922008-05-31 18:14:09 +09001207 (GLfloat)(1<<rgb_shift),
1208 (GLfloat)(1<<rgb_shift),
1209 (GLfloat)(1<<rgb_shift),
1210 (GLfloat)(1<<alpha_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001211 }
Brian Paul7e807512005-11-05 17:10:45 +00001212 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001213 saturate, out, shift, undef );
1214 }
1215 else
1216 return out;
1217}
1218
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001219
Brian Paul22db5352005-11-19 23:45:10 +00001220/**
1221 * Generate instruction for getting a texture source term.
1222 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001223static void load_texture( struct texenv_fragment_program *p, GLuint unit )
1224{
1225 if (is_undef(p->src_texture[unit])) {
Brian Paulb5ec0a62009-09-01 15:51:23 -06001226 const GLuint texTarget = p->state->unit[unit].source_index;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001227 struct ureg texcoord;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001228 struct ureg tmp = get_tex_temp( p );
1229
Roland Scheidegger114152e2009-03-12 15:01:16 +01001230 if (is_undef(p->texcoord_tex[unit])) {
1231 texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
1232 }
1233 else {
1234 /* might want to reuse this reg for tex output actually */
1235 texcoord = p->texcoord_tex[unit];
1236 }
1237
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001238 /* TODO: Use D0_MASK_XY where possible.
1239 */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +02001240 if (p->state->unit[unit].enabled) {
Brian Paul44e018c2009-02-20 13:42:08 -07001241 GLboolean shadow = GL_FALSE;
1242
1243 if (p->state->unit[unit].shadow) {
1244 p->program->Base.ShadowSamplers |= 1 << unit;
1245 shadow = GL_TRUE;
1246 }
1247
Aapo Tahkolab8915342006-03-28 10:26:34 +00001248 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1249 tmp, WRITEMASK_XYZW,
Brian Paul44e018c2009-02-20 13:42:08 -07001250 unit, texTarget, shadow,
1251 texcoord );
Brian9b7e5a52007-12-14 11:16:49 -07001252
1253 p->program->Base.SamplersUsed |= (1 << unit);
Brian1fe385f2007-12-14 11:42:28 -07001254 /* This identity mapping should already be in place
1255 * (see _mesa_init_program_struct()) but let's be safe.
1256 */
1257 p->program->Base.SamplerUnits[unit] = unit;
Brian9b7e5a52007-12-14 11:16:49 -07001258 }
1259 else
Aapo Tahkolab8915342006-03-28 10:26:34 +00001260 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001261 }
1262}
1263
Keith Whitwell241b6b72005-05-23 09:50:34 +00001264static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +00001265 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001266{
1267 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +00001268 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001269 load_texture(p, unit);
1270 break;
1271
Keith Whitwellce721142005-07-11 10:10:38 +00001272 case SRC_TEXTURE0:
1273 case SRC_TEXTURE1:
1274 case SRC_TEXTURE2:
1275 case SRC_TEXTURE3:
1276 case SRC_TEXTURE4:
1277 case SRC_TEXTURE5:
1278 case SRC_TEXTURE6:
1279 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +00001280 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001281 break;
1282
1283 default:
Brian Paul6947f852009-01-23 17:32:09 -07001284 /* not a texture src - do nothing */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001285 break;
1286 }
Keith Whitwell241b6b72005-05-23 09:50:34 +00001287
1288 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001289}
1290
Brian Paul22db5352005-11-19 23:45:10 +00001291
1292/**
1293 * Generate instructions for loading all texture source terms.
1294 */
1295static GLboolean
Brian Paul457e4272009-09-01 16:22:02 -06001296load_texunit_sources( struct texenv_fragment_program *p, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001297{
Brian Paula5e70032009-08-31 11:17:59 -06001298 const struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +00001299 GLuint i;
1300
1301 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001302 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001303 }
Aapo Tahkolab8915342006-03-28 10:26:34 +00001304
1305 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1306 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1307 }
1308
Keith Whitwell241b6b72005-05-23 09:50:34 +00001309 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001310}
1311
Roland Scheidegger114152e2009-03-12 15:01:16 +01001312/**
1313 * Generate instructions for loading bump map textures.
1314 */
1315static GLboolean
Brian Paul457e4272009-09-01 16:22:02 -06001316load_texunit_bumpmap( struct texenv_fragment_program *p, GLuint unit )
Roland Scheidegger114152e2009-03-12 15:01:16 +01001317{
Brian Paula5e70032009-08-31 11:17:59 -06001318 const struct state_key *key = p->state;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001319 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1320 struct ureg texcDst, bumpMapRes;
1321 struct ureg constdudvcolor = register_const4f(p, 0.0, 0.0, 0.0, 1.0);
1322 struct ureg texcSrc = register_input(p, FRAG_ATTRIB_TEX0 + bumpedUnitNr);
1323 struct ureg rotMat0 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_0, unit );
1324 struct ureg rotMat1 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_1, unit );
1325
1326 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1327
1328 bumpMapRes = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1329 texcDst = get_tex_temp( p );
1330 p->texcoord_tex[bumpedUnitNr] = texcDst;
1331
Brian Paul457e4272009-09-01 16:22:02 -06001332 /* Apply rot matrix and add coords to be available in next phase.
1333 * dest = (Arg0.xxxx * rotMat0 + Arg1) + (Arg0.yyyy * rotMat1)
1334 * note only 2 coords are affected the rest are left unchanged (mul by 0)
1335 */
Roland Scheidegger114152e2009-03-12 15:01:16 +01001336 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1337 swizzle1(bumpMapRes, SWIZZLE_X), rotMat0, texcSrc );
1338 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1339 swizzle1(bumpMapRes, SWIZZLE_Y), rotMat1, texcDst );
1340
Brian Paul457e4272009-09-01 16:22:02 -06001341 /* Move 0,0,0,1 into bumpmap src if someone (crossbar) is foolish
1342 * enough to access this later, should optimize away.
1343 */
1344 emit_arith( p, OPCODE_MOV, bumpMapRes, WRITEMASK_XYZW, 0,
1345 constdudvcolor, undef, undef );
Roland Scheidegger114152e2009-03-12 15:01:16 +01001346
1347 return GL_TRUE;
1348}
Brian Paul22db5352005-11-19 23:45:10 +00001349
1350/**
1351 * Generate a new fragment program which implements the context's
1352 * current texture env/combine mode.
1353 */
1354static void
Brian Paule998c342006-10-29 21:17:18 +00001355create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001356 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001357{
Brian Paule998c342006-10-29 21:17:18 +00001358 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001359 struct texenv_fragment_program p;
1360 GLuint unit;
1361 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001362
Keith Whitwelle4902422005-05-11 15:16:35 +00001363 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwellce721142005-07-11 10:10:38 +00001364 p.state = key;
1365 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001366
Brian Paule998c342006-10-29 21:17:18 +00001367 /* During code generation, use locally-allocated instruction buffer,
1368 * then alloc dynamic storage below.
1369 */
1370 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001371 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Brian Paul457e4272009-09-01 16:22:02 -06001372 p.program->Base.String = NULL;
1373 p.program->Base.NumTexIndirections = 1; /* is this right? */
Brian21f99792007-01-09 11:00:21 -07001374 p.program->Base.NumTexInstructions = 0;
1375 p.program->Base.NumAluInstructions = 0;
Brian Paul457e4272009-09-01 16:22:02 -06001376 p.program->Base.NumInstructions = 0;
1377 p.program->Base.NumTemporaries = 0;
1378 p.program->Base.NumParameters = 0;
1379 p.program->Base.NumAttributes = 0;
1380 p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001381 p.program->Base.Parameters = _mesa_new_parameter_list();
Brian Paul457e4272009-09-01 16:22:02 -06001382 p.program->Base.InputsRead = 0x0;
Brian Paul8d475822009-02-28 11:49:46 -07001383 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLOR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001384
Roland Scheidegger114152e2009-03-12 15:01:16 +01001385 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001386 p.src_texture[unit] = undef;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001387 p.texcoord_tex[unit] = undef;
1388 }
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001389
Keith Whitwell47b29f52005-05-04 11:44:44 +00001390 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001391 p.half = undef;
1392 p.zero = undef;
1393 p.one = undef;
1394
Keith Whitwell15e75e02005-04-29 15:11:39 +00001395 p.last_tex_stage = 0;
Brian93fef222007-10-29 12:25:46 -06001396 release_temps(ctx, &p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001397
Keith Whitwellce721142005-07-11 10:10:38 +00001398 if (key->enabled_units) {
Brian Paul457e4272009-09-01 16:22:02 -06001399 GLboolean needbumpstage = GL_FALSE;
1400
Roland Scheidegger114152e2009-03-12 15:01:16 +01001401 /* Zeroth pass - bump map textures first */
Brian Paul7a7d5872009-09-02 15:39:13 -06001402 for (unit = 0; unit < key->nr_enabled_units; unit++)
Brian Paul457e4272009-09-01 16:22:02 -06001403 if (key->unit[unit].enabled &&
1404 key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001405 needbumpstage = GL_TRUE;
1406 load_texunit_bumpmap( &p, unit );
1407 }
1408 if (needbumpstage)
1409 p.program->Base.NumTexIndirections++;
1410
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001411 /* First pass - to support texture_env_crossbar, first identify
1412 * all referenced texture sources and emit texld instructions
1413 * for each:
1414 */
Brian Paul7a7d5872009-09-02 15:39:13 -06001415 for (unit = 0; unit < key->nr_enabled_units; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001416 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001417 load_texunit_sources( &p, unit );
1418 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001419 }
1420
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001421 /* Second pass - emit combine instructions to build final color:
1422 */
Brian Paul7a7d5872009-09-02 15:39:13 -06001423 for (unit = 0; unit < key->nr_enabled_units; unit++)
Brian Paul84d6bed2009-09-01 16:10:57 -06001424 if (key->unit[unit].enabled) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001425 p.src_previous = emit_texenv( &p, unit );
Brian Paul5620c202008-09-26 11:18:06 -06001426 reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
Brian93fef222007-10-29 12:25:46 -06001427 release_temps(ctx, &p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001428 }
1429 }
1430
Keith Whitwellce721142005-07-11 10:10:38 +00001431 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul8d475822009-02-28 11:49:46 -07001432 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLOR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001433
Keith Whitwellce721142005-07-11 10:10:38 +00001434 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001435 /* Emit specular add.
1436 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001437 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001438 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1439 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001440 }
Brian Paulaa206952005-09-16 18:14:24 +00001441 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001442 /* Will wind up in here if no texture enabled or a couple of
1443 * other scenarios (GL_REPLACE for instance).
1444 */
Brian Paul7e807512005-11-05 17:10:45 +00001445 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001446 }
1447
Keith Whitwell47b29f52005-05-04 11:44:44 +00001448 /* Finish up:
1449 */
Brian Paul7e807512005-11-05 17:10:45 +00001450 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001451
Keith Whitwellce721142005-07-11 10:10:38 +00001452 if (key->fog_enabled) {
1453 /* Pull fog mode from GLcontext, the value in the state key is
1454 * a reduced value and not what is expected in FogOption
1455 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001456 p.program->FogOption = ctx->Fog.Mode;
Brian Paul457e4272009-09-01 16:22:02 -06001457 p.program->Base.InputsRead |= FRAG_BIT_FOGC;
1458 }
1459 else {
Keith Whitwell276330b2005-05-09 17:58:13 +00001460 p.program->FogOption = GL_NONE;
Brian Paul457e4272009-09-01 16:22:02 -06001461 }
Keith Whitwell47b29f52005-05-04 11:44:44 +00001462
Brian21f99792007-01-09 11:00:21 -07001463 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001464 program_error(&p, "Exceeded max nr indirect texture lookups");
1465
Brian21f99792007-01-09 11:00:21 -07001466 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001467 program_error(&p, "Exceeded max TEX instructions");
1468
Brian21f99792007-01-09 11:00:21 -07001469 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001470 program_error(&p, "Exceeded max ALU instructions");
1471
Brian Paul5d7b49f2005-11-19 23:12:20 +00001472 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001473
Brian Paule998c342006-10-29 21:17:18 +00001474 /* Allocate final instruction array */
Brianc81cce72007-09-25 15:18:51 -06001475 p.program->Base.Instructions
1476 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1477 if (!p.program->Base.Instructions) {
Brian Paule998c342006-10-29 21:17:18 +00001478 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1479 "generating tex env program");
1480 return;
1481 }
Brianc81cce72007-09-25 15:18:51 -06001482 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1483 p.program->Base.NumInstructions);
1484
1485 if (p.program->FogOption) {
1486 _mesa_append_fog_code(ctx, p.program);
1487 p.program->FogOption = GL_NONE;
1488 }
1489
Brian Paule998c342006-10-29 21:17:18 +00001490
Keith Whitwelldbeea252005-05-10 13:57:50 +00001491 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001492 */
Brian Paule998c342006-10-29 21:17:18 +00001493 if (ctx->Driver.ProgramStringNotify) {
1494 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1495 &p.program->Base );
1496 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001497
Brian Paule998c342006-10-29 21:17:18 +00001498 if (DISASSEM) {
1499 _mesa_print_program(&p.program->Base);
1500 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001501 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001502}
1503
Brian Paul5d7b49f2005-11-19 23:12:20 +00001504
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001505/**
1506 * Return a fragment program which implements the current
1507 * fixed-function texture, fog and color-sum operations.
1508 */
1509struct gl_fragment_program *
1510_mesa_get_fixed_func_fragment_program(GLcontext *ctx)
Keith Whitwellce721142005-07-11 10:10:38 +00001511{
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001512 struct gl_fragment_program *prog;
1513 struct state_key key;
Brian Paul7a7d5872009-09-02 15:39:13 -06001514 GLuint keySize;
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001515
Brian Paul7a7d5872009-09-02 15:39:13 -06001516 keySize = make_state_key(ctx, &key);
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001517
1518 prog = (struct gl_fragment_program *)
1519 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
Brian Paul7a7d5872009-09-02 15:39:13 -06001520 &key, keySize);
Keith Whitwellce721142005-07-11 10:10:38 +00001521
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001522 if (!prog) {
1523 prog = (struct gl_fragment_program *)
1524 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1525
1526 create_new_program(ctx, &key, prog);
1527
1528 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
Brian Paul7a7d5872009-09-02 15:39:13 -06001529 &key, keySize, &prog->Base);
Keith Whitwellce721142005-07-11 10:10:38 +00001530 }
1531
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001532 return prog;
Keith Whitwellce721142005-07-11 10:10:38 +00001533}