blob: 08985814858b7a900af44320c49f1492e8b0fa75 [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"
30#include "macros.h"
31#include "enums.h"
Brian Paul5b5c9312008-05-07 18:51:44 -060032#include "shader/program.h"
Brianc223c6b2007-07-04 13:15:20 -060033#include "shader/prog_parameter.h"
Keith Whitwell32ef6e72008-09-20 08:26:11 -070034#include "shader/prog_cache.h"
Brianc223c6b2007-07-04 13:15:20 -060035#include "shader/prog_instruction.h"
36#include "shader/prog_print.h"
37#include "shader/prog_statevars.h"
Brianfb3c41f2007-09-25 15:20:04 -060038#include "shader/programopt.h"
Keith Whitwell15e75e02005-04-29 15:11:39 +000039#include "texenvprogram.h"
40
Brian Paul5b5c9312008-05-07 18:51:44 -060041
Brian Paule9b34882008-12-31 11:54:02 -070042/*
43 * Note on texture units:
44 *
45 * The number of texture units supported by fixed-function fragment
46 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
47 * That's because there's a one-to-one correspondence between texture
48 * coordinates and samplers in fixed-function processing.
49 *
50 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
51 * sets of texcoords, so is fixed-function fragment processing.
52 *
53 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
54 */
55
56
Brian Paul5b5c9312008-05-07 18:51:44 -060057struct texenvprog_cache_item
58{
59 GLuint hash;
60 void *key;
61 struct gl_fragment_program *data;
62 struct texenvprog_cache_item *next;
63};
64
Eric Anholt9cea84b2009-07-16 18:41:03 -070065static GLboolean
66texenv_doing_secondary_color(GLcontext *ctx)
67{
68 if (ctx->Light.Enabled &&
69 (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR))
70 return GL_TRUE;
71
72 if (ctx->Fog.ColorSumEnabled)
73 return GL_TRUE;
74
75 return GL_FALSE;
76}
Brian Paul5b5c9312008-05-07 18:51:44 -060077
Brian Paule998c342006-10-29 21:17:18 +000078/**
Brian Paulb5e1a932008-09-25 18:40:16 -060079 * Up to nine instructions per tex unit, plus fog, specular color.
Brian Paule998c342006-10-29 21:17:18 +000080 */
Brian Paul0815ebc2009-01-02 16:32:26 -070081#define MAX_INSTRUCTIONS ((MAX_TEXTURE_COORD_UNITS * 9) + 12)
Keith Whitwell15e75e02005-04-29 15:11:39 +000082
Keith Whitwell269e3892005-05-12 10:28:43 +000083#define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
Keith Whitwell15e75e02005-04-29 15:11:39 +000084
Keith Whitwellce721142005-07-11 10:10:38 +000085struct mode_opt {
Brian Paulf337e2c2009-09-01 15:49:55 -060086 GLuint Source:4; /**< SRC_x */
87 GLuint Operand:3; /**< OPR_x */
Keith Whitwellce721142005-07-11 10:10:38 +000088};
89
90struct state_key {
Keith Whitwell6280e332008-10-03 13:51:56 +010091 GLuint nr_enabled_units:8;
92 GLuint enabled_units:8;
Brian Paul5d7b49f2005-11-19 23:12:20 +000093 GLuint separate_specular:1;
94 GLuint fog_enabled:1;
95 GLuint fog_mode:2;
Keith Whitwell6280e332008-10-03 13:51:56 +010096 GLuint inputs_available:12;
Keith Whitwellce721142005-07-11 10:10:38 +000097
98 struct {
Brian Paul5d7b49f2005-11-19 23:12:20 +000099 GLuint enabled:1;
100 GLuint source_index:3; /* one of TEXTURE_1D/2D/3D/CUBE/RECT_INDEX */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +0200101 GLuint shadow:1;
Brian Paul5d7b49f2005-11-19 23:12:20 +0000102 GLuint ScaleShiftRGB:2;
103 GLuint ScaleShiftA:2;
Keith Whitwellce721142005-07-11 10:10:38 +0000104
Brian Paul6947f852009-01-23 17:32:09 -0700105 GLuint NumArgsRGB:3;
Brian Paulf337e2c2009-09-01 15:49:55 -0600106 GLuint ModeRGB:5; /**< MODE_x */
Brian Paulbd9b2be2009-03-08 17:58:54 -0600107 struct mode_opt OptRGB[MAX_COMBINER_TERMS];
Keith Whitwellce721142005-07-11 10:10:38 +0000108
Brian Paul6947f852009-01-23 17:32:09 -0700109 GLuint NumArgsA:3;
Brian Paulf337e2c2009-09-01 15:49:55 -0600110 GLuint ModeA:5; /**< MODE_x */
Brian Paulbd9b2be2009-03-08 17:58:54 -0600111 struct mode_opt OptA[MAX_COMBINER_TERMS];
Brian Pauld55a28e2009-09-01 15:34:16 -0600112 } unit[MAX_TEXTURE_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000113};
114
115#define FOG_LINEAR 0
116#define FOG_EXP 1
117#define FOG_EXP2 2
118#define FOG_UNKNOWN 3
119
120static GLuint translate_fog_mode( GLenum mode )
121{
122 switch (mode) {
123 case GL_LINEAR: return FOG_LINEAR;
124 case GL_EXP: return FOG_EXP;
125 case GL_EXP2: return FOG_EXP2;
126 default: return FOG_UNKNOWN;
127 }
128}
129
130#define OPR_SRC_COLOR 0
131#define OPR_ONE_MINUS_SRC_COLOR 1
132#define OPR_SRC_ALPHA 2
133#define OPR_ONE_MINUS_SRC_ALPHA 3
134#define OPR_ZERO 4
135#define OPR_ONE 5
136#define OPR_UNKNOWN 7
137
138static GLuint translate_operand( GLenum operand )
139{
140 switch (operand) {
141 case GL_SRC_COLOR: return OPR_SRC_COLOR;
142 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
143 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
144 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
145 case GL_ZERO: return OPR_ZERO;
146 case GL_ONE: return OPR_ONE;
Brian Paul6947f852009-01-23 17:32:09 -0700147 default:
148 assert(0);
149 return OPR_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000150 }
151}
152
153#define SRC_TEXTURE 0
154#define SRC_TEXTURE0 1
155#define SRC_TEXTURE1 2
156#define SRC_TEXTURE2 3
157#define SRC_TEXTURE3 4
158#define SRC_TEXTURE4 5
159#define SRC_TEXTURE5 6
160#define SRC_TEXTURE6 7
161#define SRC_TEXTURE7 8
162#define SRC_CONSTANT 9
163#define SRC_PRIMARY_COLOR 10
164#define SRC_PREVIOUS 11
Brian Paul6947f852009-01-23 17:32:09 -0700165#define SRC_ZERO 12
Keith Whitwellce721142005-07-11 10:10:38 +0000166#define SRC_UNKNOWN 15
167
168static GLuint translate_source( GLenum src )
169{
170 switch (src) {
171 case GL_TEXTURE: return SRC_TEXTURE;
172 case GL_TEXTURE0:
173 case GL_TEXTURE1:
174 case GL_TEXTURE2:
175 case GL_TEXTURE3:
176 case GL_TEXTURE4:
177 case GL_TEXTURE5:
178 case GL_TEXTURE6:
179 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
180 case GL_CONSTANT: return SRC_CONSTANT;
181 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
182 case GL_PREVIOUS: return SRC_PREVIOUS;
Brian Paul6947f852009-01-23 17:32:09 -0700183 case GL_ZERO:
184 return SRC_ZERO;
185 default:
186 assert(0);
187 return SRC_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000188 }
189}
190
Brian Paul6947f852009-01-23 17:32:09 -0700191#define MODE_REPLACE 0 /* r = a0 */
192#define MODE_MODULATE 1 /* r = a0 * a1 */
193#define MODE_ADD 2 /* r = a0 + a1 */
194#define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
195#define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
196#define MODE_SUBTRACT 5 /* r = a0 - a1 */
197#define MODE_DOT3_RGB 6 /* r = a0 . a1 */
198#define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
199#define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
200#define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
201#define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
202#define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
203#define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
204#define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
205#define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
Roland Scheidegger114152e2009-03-12 15:01:16 +0100206#define MODE_BUMP_ENVMAP_ATI 15 /* special */
207#define MODE_UNKNOWN 16
Keith Whitwellce721142005-07-11 10:10:38 +0000208
Brian Paul6947f852009-01-23 17:32:09 -0700209/**
210 * Translate GL combiner state into a MODE_x value
211 */
212static GLuint translate_mode( GLenum envMode, GLenum mode )
Keith Whitwellce721142005-07-11 10:10:38 +0000213{
214 switch (mode) {
215 case GL_REPLACE: return MODE_REPLACE;
216 case GL_MODULATE: return MODE_MODULATE;
Brian Paul6947f852009-01-23 17:32:09 -0700217 case GL_ADD:
218 if (envMode == GL_COMBINE4_NV)
219 return MODE_ADD_PRODUCTS;
220 else
221 return MODE_ADD;
222 case GL_ADD_SIGNED:
223 if (envMode == GL_COMBINE4_NV)
224 return MODE_ADD_PRODUCTS_SIGNED;
225 else
226 return MODE_ADD_SIGNED;
Keith Whitwellce721142005-07-11 10:10:38 +0000227 case GL_INTERPOLATE: return MODE_INTERPOLATE;
228 case GL_SUBTRACT: return MODE_SUBTRACT;
229 case GL_DOT3_RGB: return MODE_DOT3_RGB;
230 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
231 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
232 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
233 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
234 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
235 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
Roland Scheidegger114152e2009-03-12 15:01:16 +0100236 case GL_BUMP_ENVMAP_ATI: return MODE_BUMP_ENVMAP_ATI;
Brian Paul6947f852009-01-23 17:32:09 -0700237 default:
238 assert(0);
239 return MODE_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000240 }
241}
242
Brian Paulf337e2c2009-09-01 15:49:55 -0600243
Keith Whitwellce721142005-07-11 10:10:38 +0000244#define TEXTURE_UNKNOWN_INDEX 7
Brian Paulf337e2c2009-09-01 15:49:55 -0600245
246/**
247 * Translate TEXTURE_x_BIT to TEXTURE_x_INDEX.
248 */
Brian Paule00ac112005-09-15 05:00:45 +0000249static GLuint translate_tex_src_bit( GLbitfield bit )
Keith Whitwellce721142005-07-11 10:10:38 +0000250{
Brian Paul1519b932008-12-17 13:17:15 -0700251 /* make sure number of switch cases is correct */
252 assert(NUM_TEXTURE_TARGETS == 7);
Keith Whitwellce721142005-07-11 10:10:38 +0000253 switch (bit) {
254 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
255 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
256 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
257 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
258 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
Brian Paul6947f852009-01-23 17:32:09 -0700259 case TEXTURE_1D_ARRAY_BIT: return TEXTURE_1D_ARRAY_INDEX;
260 case TEXTURE_2D_ARRAY_BIT: return TEXTURE_2D_ARRAY_INDEX;
261 default:
262 assert(0);
263 return TEXTURE_UNKNOWN_INDEX;
Keith Whitwellce721142005-07-11 10:10:38 +0000264 }
265}
266
Brian Paulf337e2c2009-09-01 15:49:55 -0600267
Keith Whitwell1680ef82008-10-03 17:30:59 +0100268#define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
269#define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
270
Brian Paul239617f2008-10-07 11:22:47 -0600271/**
272 * Identify all possible varying inputs. The fragment program will
Keith Whitwell1680ef82008-10-03 17:30:59 +0100273 * never reference non-varying inputs, but will track them via state
274 * constants instead.
275 *
276 * This function figures out all the inputs that the fragment program
277 * has access to. The bitmask is later reduced to just those which
278 * are actually referenced.
279 */
Brian Paul239617f2008-10-07 11:22:47 -0600280static GLbitfield get_fp_input_mask( GLcontext *ctx )
Keith Whitwell1680ef82008-10-03 17:30:59 +0100281{
Eric Anholte5f63c42009-06-02 07:47:20 -0700282 /* _NEW_PROGRAM */
Brian Paulf0b07942008-12-17 14:04:03 -0700283 const GLboolean vertexShader = (ctx->Shader.CurrentProgram &&
Eric Anholt40990d92009-08-03 12:36:52 -0700284 ctx->Shader.CurrentProgram->LinkStatus &&
Brian Paulf0b07942008-12-17 14:04:03 -0700285 ctx->Shader.CurrentProgram->VertexProgram);
286 const GLboolean vertexProgram = ctx->VertexProgram._Enabled;
Brian Paul239617f2008-10-07 11:22:47 -0600287 GLbitfield fp_inputs = 0x0;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100288
Brian Paul6d4d51d2008-10-10 13:39:14 -0600289 if (ctx->VertexProgram._Overriden) {
290 /* Somebody's messing with the vertex program and we don't have
291 * a clue what's happening. Assume that it could be producing
292 * all possible outputs.
293 */
294 fp_inputs = ~0;
295 }
296 else if (ctx->RenderMode == GL_FEEDBACK) {
Eric Anholte5f63c42009-06-02 07:47:20 -0700297 /* _NEW_RENDERMODE */
Brian Paul6d4d51d2008-10-10 13:39:14 -0600298 fp_inputs = (FRAG_BIT_COL0 | FRAG_BIT_TEX0);
299 }
Brian Paulf0b07942008-12-17 14:04:03 -0700300 else if (!(vertexProgram || vertexShader) ||
Brian Pauld7296a12008-12-17 11:29:06 -0700301 !ctx->VertexProgram._Current) {
Brian Paulf0b07942008-12-17 14:04:03 -0700302 /* Fixed function vertex logic */
Eric Anholte5f63c42009-06-02 07:47:20 -0700303 /* _NEW_ARRAY */
Brian Paul239617f2008-10-07 11:22:47 -0600304 GLbitfield varying_inputs = ctx->varying_vp_inputs;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100305
Keith Whitwell97e63432008-10-20 13:03:45 +0100306 /* These get generated in the setup routine regardless of the
307 * vertex program:
308 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700309 /* _NEW_POINT */
Keith Whitwell97e63432008-10-20 13:03:45 +0100310 if (ctx->Point.PointSprite)
311 varying_inputs |= FRAG_BITS_TEX_ANY;
312
Keith Whitwell1680ef82008-10-03 17:30:59 +0100313 /* First look at what values may be computed by the generated
314 * vertex program:
315 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700316 /* _NEW_LIGHT */
Keith Whitwell1680ef82008-10-03 17:30:59 +0100317 if (ctx->Light.Enabled) {
318 fp_inputs |= FRAG_BIT_COL0;
319
Eric Anholt9cea84b2009-07-16 18:41:03 -0700320 if (texenv_doing_secondary_color(ctx))
Keith Whitwell1680ef82008-10-03 17:30:59 +0100321 fp_inputs |= FRAG_BIT_COL1;
322 }
323
Eric Anholte5f63c42009-06-02 07:47:20 -0700324 /* _NEW_TEXTURE */
Keith Whitwell1680ef82008-10-03 17:30:59 +0100325 fp_inputs |= (ctx->Texture._TexGenEnabled |
326 ctx->Texture._TexMatEnabled) << FRAG_ATTRIB_TEX0;
327
328 /* Then look at what might be varying as a result of enabled
329 * arrays, etc:
330 */
Brian Paul6807d962009-08-07 09:42:28 -0600331 if (varying_inputs & VERT_BIT_COLOR0)
332 fp_inputs |= FRAG_BIT_COL0;
333 if (varying_inputs & VERT_BIT_COLOR1)
334 fp_inputs |= FRAG_BIT_COL1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100335
336 fp_inputs |= (((varying_inputs & VERT_BIT_TEX_ANY) >> VERT_ATTRIB_TEX0)
337 << FRAG_ATTRIB_TEX0);
338
339 }
340 else {
341 /* calculate from vp->outputs */
Brian Paul2389c052008-12-17 19:01:34 -0700342 struct gl_vertex_program *vprog;
343 GLbitfield vp_outputs;
344
345 /* Choose GLSL vertex shader over ARB vertex program. Need this
346 * since vertex shader state validation comes after fragment state
347 * validation (see additional comments in state.c).
348 */
349 if (vertexShader)
350 vprog = ctx->Shader.CurrentProgram->VertexProgram;
351 else
Eric Anholta9ba1bf2009-08-03 12:38:56 -0700352 vprog = ctx->VertexProgram.Current;
Brian Paul2389c052008-12-17 19:01:34 -0700353
354 vp_outputs = vprog->Base.OutputsWritten;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100355
Keith Whitwell97e63432008-10-20 13:03:45 +0100356 /* These get generated in the setup routine regardless of the
357 * vertex program:
358 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700359 /* _NEW_POINT */
Keith Whitwell97e63432008-10-20 13:03:45 +0100360 if (ctx->Point.PointSprite)
361 vp_outputs |= FRAG_BITS_TEX_ANY;
362
Brian Paul6807d962009-08-07 09:42:28 -0600363 if (vp_outputs & (1 << VERT_RESULT_COL0))
364 fp_inputs |= FRAG_BIT_COL0;
365 if (vp_outputs & (1 << VERT_RESULT_COL1))
366 fp_inputs |= FRAG_BIT_COL1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100367
Keith Whitwell0370d6b2008-10-04 12:41:56 +0100368 fp_inputs |= (((vp_outputs & VERT_RESULT_TEX_ANY) >> VERT_RESULT_TEX0)
369 << FRAG_ATTRIB_TEX0);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100370 }
371
372 return fp_inputs;
373}
374
375
Brian Paul22db5352005-11-19 23:45:10 +0000376/**
377 * Examine current texture environment state and generate a unique
378 * key to identify it.
379 */
Keith Whitwell8065c122006-05-22 16:09:27 +0000380static void make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000381{
Keith Whitwellce721142005-07-11 10:10:38 +0000382 GLuint i, j;
Brian Paul239617f2008-10-07 11:22:47 -0600383 GLbitfield inputs_referenced = FRAG_BIT_COL0;
Brian Paulf337e2c2009-09-01 15:49:55 -0600384 const GLbitfield inputs_available = get_fp_input_mask( ctx );
Keith Whitwell1680ef82008-10-03 17:30:59 +0100385
Keith Whitwell8065c122006-05-22 16:09:27 +0000386 memset(key, 0, sizeof(*key));
387
Eric Anholte5f63c42009-06-02 07:47:20 -0700388 /* _NEW_TEXTURE */
Brian Paule9b34882008-12-31 11:54:02 -0700389 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000390 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Brian Paulf337e2c2009-09-01 15:49:55 -0600391 const struct gl_texture_object *texObj = texUnit->_Current;
392 const struct gl_tex_env_combine_state *comb = texUnit->_CurrentCombine;
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800393 GLenum format;
Roland Scheideggerbf4a0fa2008-02-15 17:26:06 +0100394
395 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
Keith Whitwellce721142005-07-11 10:10:38 +0000396 continue;
397
Brian Paulf337e2c2009-09-01 15:49:55 -0600398 format = texObj->Image[0][texObj->BaseLevel]->_BaseFormat;
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800399
Keith Whitwellce721142005-07-11 10:10:38 +0000400 key->unit[i].enabled = 1;
401 key->enabled_units |= (1<<i);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100402 key->nr_enabled_units = i+1;
403 inputs_referenced |= FRAG_BIT_TEX(i);
Keith Whitwellce721142005-07-11 10:10:38 +0000404
Brian Paulf337e2c2009-09-01 15:49:55 -0600405 key->unit[i].source_index =
406 translate_tex_src_bit(texUnit->_ReallyEnabled);
407
408 key->unit[i].shadow = ((texObj->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800409 ((format == GL_DEPTH_COMPONENT) ||
410 (format == GL_DEPTH_STENCIL_EXT)));
Keith Whitwellce721142005-07-11 10:10:38 +0000411
Brian Paulf337e2c2009-09-01 15:49:55 -0600412 key->unit[i].NumArgsRGB = comb->_NumArgsRGB;
413 key->unit[i].NumArgsA = comb->_NumArgsA;
Keith Whitwellce721142005-07-11 10:10:38 +0000414
415 key->unit[i].ModeRGB =
Brian Paulf337e2c2009-09-01 15:49:55 -0600416 translate_mode(texUnit->EnvMode, comb->ModeRGB);
Keith Whitwellce721142005-07-11 10:10:38 +0000417 key->unit[i].ModeA =
Brian Paulf337e2c2009-09-01 15:49:55 -0600418 translate_mode(texUnit->EnvMode, comb->ModeA);
Roland Scheidegger114152e2009-03-12 15:01:16 +0100419
Brian Paulf337e2c2009-09-01 15:49:55 -0600420 key->unit[i].ScaleShiftRGB = comb->ScaleShiftRGB;
421 key->unit[i].ScaleShiftA = comb->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000422
Brian Paulbd9b2be2009-03-08 17:58:54 -0600423 for (j = 0; j < MAX_COMBINER_TERMS; j++) {
Brian Paulf337e2c2009-09-01 15:49:55 -0600424 key->unit[i].OptRGB[j].Operand = translate_operand(comb->OperandRGB[j]);
425 key->unit[i].OptA[j].Operand = translate_operand(comb->OperandA[j]);
426 key->unit[i].OptRGB[j].Source = translate_source(comb->SourceRGB[j]);
427 key->unit[i].OptA[j].Source = translate_source(comb->SourceA[j]);
Keith Whitwellce721142005-07-11 10:10:38 +0000428 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100429
430 if (key->unit[i].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
431 /* requires some special translation */
432 key->unit[i].NumArgsRGB = 2;
433 key->unit[i].ScaleShiftRGB = 0;
434 key->unit[i].OptRGB[0].Operand = OPR_SRC_COLOR;
435 key->unit[i].OptRGB[0].Source = SRC_TEXTURE;
436 key->unit[i].OptRGB[1].Operand = OPR_SRC_COLOR;
437 key->unit[i].OptRGB[1].Source = texUnit->BumpTarget - GL_TEXTURE0 + SRC_TEXTURE0;
438 }
Keith Whitwellce721142005-07-11 10:10:38 +0000439 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100440
Eric Anholt9cea84b2009-07-16 18:41:03 -0700441 /* _NEW_LIGHT | _NEW_FOG */
442 if (texenv_doing_secondary_color(ctx)) {
Keith Whitwellce721142005-07-11 10:10:38 +0000443 key->separate_specular = 1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100444 inputs_referenced |= FRAG_BIT_COL1;
445 }
Keith Whitwellce721142005-07-11 10:10:38 +0000446
Eric Anholte5f63c42009-06-02 07:47:20 -0700447 /* _NEW_FOG */
Keith Whitwellce721142005-07-11 10:10:38 +0000448 if (ctx->Fog.Enabled) {
449 key->fog_enabled = 1;
450 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100451 inputs_referenced |= FRAG_BIT_FOGC; /* maybe */
Keith Whitwellce721142005-07-11 10:10:38 +0000452 }
Keith Whitwell1680ef82008-10-03 17:30:59 +0100453
454 key->inputs_available = (inputs_available & inputs_referenced);
Keith Whitwellce721142005-07-11 10:10:38 +0000455}
456
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;
474 GLuint abs:1;
475 GLuint negateabs:1;
476 GLuint swz:12;
477 GLuint pad:5;
478};
479
Brian Paul13abf912006-04-13 19:17:13 +0000480static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000481 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000482 ~0,
483 0,
484 0,
485 0,
486 0,
487 0
488};
489
Keith Whitwell15e75e02005-04-29 15:11:39 +0000490
Brian Paul239617f2008-10-07 11:22:47 -0600491/** State used to build the fragment program:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000492 */
493struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000494 struct gl_fragment_program *program;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000495 GLcontext *ctx;
Keith Whitwellce721142005-07-11 10:10:38 +0000496 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000497
Brian Paul239617f2008-10-07 11:22:47 -0600498 GLbitfield alu_temps; /**< Track texture indirections, see spec. */
499 GLbitfield temps_output; /**< Track texture indirections, see spec. */
500 GLbitfield temp_in_use; /**< Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000501 GLboolean error;
502
Brian Paule9b34882008-12-31 11:54:02 -0700503 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000504 /* Reg containing each texture unit's sampled texture color,
505 * else undef.
506 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000507
Roland Scheidegger114152e2009-03-12 15:01:16 +0100508 struct ureg texcoord_tex[MAX_TEXTURE_COORD_UNITS];
509 /* Reg containing texcoord for a texture unit,
510 * needed for bump mapping, else undef.
511 */
512
Brian Paul239617f2008-10-07 11:22:47 -0600513 struct ureg src_previous; /**< Reg containing color from previous
Keith Whitwell15e75e02005-04-29 15:11:39 +0000514 * stage. May need to be decl'd.
515 */
516
Brian Paul239617f2008-10-07 11:22:47 -0600517 GLuint last_tex_stage; /**< Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000518
519 struct ureg half;
520 struct ureg one;
521 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000522};
523
524
525
526static struct ureg make_ureg(GLuint file, GLuint idx)
527{
528 struct ureg reg;
529 reg.file = file;
530 reg.idx = idx;
531 reg.negatebase = 0;
532 reg.abs = 0;
533 reg.negateabs = 0;
534 reg.swz = SWIZZLE_NOOP;
535 reg.pad = 0;
536 return reg;
537}
538
539static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
540{
541 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
542 GET_SWZ(reg.swz, y),
543 GET_SWZ(reg.swz, z),
544 GET_SWZ(reg.swz, w));
545
546 return reg;
547}
548
549static struct ureg swizzle1( struct ureg reg, int x )
550{
551 return swizzle(reg, x, x, x, x);
552}
553
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000554static struct ureg negate( struct ureg reg )
555{
556 reg.negatebase ^= 1;
557 return reg;
558}
559
Keith Whitwell15e75e02005-04-29 15:11:39 +0000560static GLboolean is_undef( struct ureg reg )
561{
Alan Hourihane8d972652006-08-10 13:12:00 +0000562 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000563}
564
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000565
Keith Whitwell15e75e02005-04-29 15:11:39 +0000566static struct ureg get_temp( struct texenv_fragment_program *p )
567{
Brian Paul13abf912006-04-13 19:17:13 +0000568 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000569
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000570 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000571 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000572 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000573
574 /* Then any unused temporary:
575 */
576 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000577 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000578
Keith Whitwell15e75e02005-04-29 15:11:39 +0000579 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000580 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000581 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000582 }
583
Brian Paul13abf912006-04-13 19:17:13 +0000584 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000585 p->program->Base.NumTemporaries = bit;
586
Keith Whitwell93cd9232005-05-11 08:30:23 +0000587 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000588 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
589}
590
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000591static struct ureg get_tex_temp( struct texenv_fragment_program *p )
592{
593 int bit;
594
Brian Pauld01269a2008-09-25 18:27:22 -0600595 /* First try to find available temp not previously used (to avoid
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000596 * starting a new texture indirection). According to the spec, the
597 * ~p->temps_output isn't necessary, but will keep it there for
598 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000599 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000600 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000601
602 /* Then any unused temporary:
603 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000604 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000605 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000606
607 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000608 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000609 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000610 }
611
Brian Paul13abf912006-04-13 19:17:13 +0000612 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000613 p->program->Base.NumTemporaries = bit;
614
Keith Whitwell93cd9232005-05-11 08:30:23 +0000615 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000616 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
617}
618
Keith Whitwell15e75e02005-04-29 15:11:39 +0000619
Brian Paul5620c202008-09-26 11:18:06 -0600620/** Mark a temp reg as being no longer allocatable. */
621static void reserve_temp( struct texenv_fragment_program *p, struct ureg r )
622{
623 if (r.file == PROGRAM_TEMPORARY)
624 p->temps_output |= (1 << r.idx);
625}
626
627
Brian93fef222007-10-29 12:25:46 -0600628static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000629{
Brian93fef222007-10-29 12:25:46 -0600630 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000631
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000632 /* KW: To support tex_env_crossbar, don't release the registers in
633 * temps_output.
634 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000635 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000636 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000637 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000638 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000639}
640
641
Brian9fe3e2e2007-02-23 11:44:14 -0700642static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000643 GLint s0,
644 GLint s1,
645 GLint s2,
646 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700647 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000648{
Brian9fe3e2e2007-02-23 11:44:14 -0700649 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000650 GLuint idx;
651 tokens[0] = s0;
652 tokens[1] = s1;
653 tokens[2] = s2;
654 tokens[3] = s3;
655 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000656 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000657 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000658}
659
Keith Whitwell47b29f52005-05-04 11:44:44 +0000660
Brian9fe3e2e2007-02-23 11:44:14 -0700661#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
662#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
663#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
664#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000665
Keith Whitwell1680ef82008-10-03 17:30:59 +0100666static GLuint frag_to_vert_attrib( GLuint attrib )
667{
668 switch (attrib) {
669 case FRAG_ATTRIB_COL0: return VERT_ATTRIB_COLOR0;
670 case FRAG_ATTRIB_COL1: return VERT_ATTRIB_COLOR1;
671 default:
672 assert(attrib >= FRAG_ATTRIB_TEX0);
673 assert(attrib <= FRAG_ATTRIB_TEX7);
674 return attrib - FRAG_ATTRIB_TEX0 + VERT_ATTRIB_TEX0;
675 }
676}
677
Keith Whitwell47b29f52005-05-04 11:44:44 +0000678
679static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
680{
Keith Whitwell1680ef82008-10-03 17:30:59 +0100681 if (p->state->inputs_available & (1<<input)) {
682 p->program->Base.InputsRead |= (1 << input);
683 return make_ureg(PROGRAM_INPUT, input);
684 }
685 else {
686 GLuint idx = frag_to_vert_attrib( input );
687 return register_param3( p, STATE_INTERNAL, STATE_CURRENT_ATTRIB, idx );
688 }
Keith Whitwell47b29f52005-05-04 11:44:44 +0000689}
690
691
Brian Paul7e807512005-11-05 17:10:45 +0000692static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000693 struct ureg ureg )
694{
695 reg->File = ureg.file;
696 reg->Index = ureg.idx;
697 reg->Swizzle = ureg.swz;
Brian Paul7db7ff82009-04-14 22:14:30 -0600698 reg->Negate = ureg.negatebase ? NEGATE_XYZW : NEGATE_NONE;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000699 reg->Abs = ureg.abs;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000700}
701
Brian Paul7e807512005-11-05 17:10:45 +0000702static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000703 struct ureg ureg, GLuint mask )
704{
705 dst->File = ureg.file;
706 dst->Index = ureg.idx;
707 dst->WriteMask = mask;
Brianc9d495c2007-10-23 10:55:24 -0600708 dst->CondMask = COND_TR; /* always pass cond test */
709 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000710}
711
Brian Paul7e807512005-11-05 17:10:45 +0000712static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000713emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000714 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000715 struct ureg dest,
716 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000717 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000718 struct ureg src0,
719 struct ureg src1,
720 struct ureg src2 )
721{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000722 GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000723 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Brian2a8e9bb2007-10-23 10:24:53 -0600724
725 assert(nr < MAX_INSTRUCTIONS);
726
Brian Pauld6272e02006-10-29 18:03:16 +0000727 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000728 inst->Opcode = op;
729
Keith Whitwell47b29f52005-05-04 11:44:44 +0000730 emit_arg( &inst->SrcReg[0], src0 );
731 emit_arg( &inst->SrcReg[1], src1 );
732 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000733
Brian Paule31ac052005-11-20 17:52:40 +0000734 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000735
736 emit_dst( &inst->DstReg, dest, mask );
737
Brian Paul5620c202008-09-26 11:18:06 -0600738#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000739 /* Accounting for indirection tracking:
740 */
741 if (dest.file == PROGRAM_TEMPORARY)
742 p->temps_output |= 1 << dest.idx;
Brian Paul5620c202008-09-26 11:18:06 -0600743#endif
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000744
Keith Whitwell15e75e02005-04-29 15:11:39 +0000745 return inst;
746}
747
748
749static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000750 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000751 struct ureg dest,
752 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000753 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000754 struct ureg src0,
755 struct ureg src1,
756 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000757{
758 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
759
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000760 /* Accounting for indirection tracking:
761 */
762 if (src0.file == PROGRAM_TEMPORARY)
763 p->alu_temps |= 1 << src0.idx;
764
765 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
766 p->alu_temps |= 1 << src1.idx;
767
768 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
769 p->alu_temps |= 1 << src2.idx;
770
771 if (dest.file == PROGRAM_TEMPORARY)
772 p->alu_temps |= 1 << dest.idx;
773
Brian21f99792007-01-09 11:00:21 -0700774 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000775 return dest;
776}
777
778static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000779 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000780 struct ureg dest,
781 GLuint destmask,
782 GLuint tex_unit,
783 GLuint tex_idx,
Brian Paul44e018c2009-02-20 13:42:08 -0700784 GLuint tex_shadow,
Keith Whitwellce721142005-07-11 10:10:38 +0000785 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000786{
Brian Paul7e807512005-11-05 17:10:45 +0000787 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000788 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000789 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000790 coord, /* arg 0? */
791 undef,
792 undef);
793
Brian Paul7e807512005-11-05 17:10:45 +0000794 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000795 inst->TexSrcUnit = tex_unit;
Brian Paul44e018c2009-02-20 13:42:08 -0700796 inst->TexShadow = tex_shadow;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000797
Brian21f99792007-01-09 11:00:21 -0700798 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000799
Brian Paul5620c202008-09-26 11:18:06 -0600800 /* Accounting for indirection tracking:
801 */
802 reserve_temp(p, dest);
803
Roland Scheidegger114152e2009-03-12 15:01:16 +0100804#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000805 /* Is this a texture indirection?
806 */
807 if ((coord.file == PROGRAM_TEMPORARY &&
808 (p->temps_output & (1<<coord.idx))) ||
809 (dest.file == PROGRAM_TEMPORARY &&
810 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700811 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000812 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000813 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000814 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000815 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100816#endif
Keith Whitwell15e75e02005-04-29 15:11:39 +0000817
818 return dest;
819}
820
821
Keith Whitwell47b29f52005-05-04 11:44:44 +0000822static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000823 GLfloat s0,
824 GLfloat s1,
825 GLfloat s2,
826 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000827{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000828 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700829 GLuint idx, swizzle;
Brian Pauld01269a2008-09-25 18:27:22 -0600830 struct ureg r;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000831 values[0] = s0;
832 values[1] = s1;
833 values[2] = s2;
834 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700835 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
836 &swizzle );
Brian Pauld01269a2008-09-25 18:27:22 -0600837 r = make_ureg(PROGRAM_CONSTANT, idx);
838 r.swz = swizzle;
839 return r;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000840}
841
Keith Whitwelle4902422005-05-11 15:16:35 +0000842#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000843#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
844#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
845#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000846
847
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000848static struct ureg get_one( struct texenv_fragment_program *p )
849{
850 if (is_undef(p->one))
851 p->one = register_scalar_const(p, 1.0);
852 return p->one;
853}
854
855static struct ureg get_half( struct texenv_fragment_program *p )
856{
857 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000858 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000859 return p->half;
860}
861
862static struct ureg get_zero( struct texenv_fragment_program *p )
863{
864 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000865 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000866 return p->zero;
867}
868
Keith Whitwell15e75e02005-04-29 15:11:39 +0000869
Keith Whitwell15e75e02005-04-29 15:11:39 +0000870static void program_error( struct texenv_fragment_program *p, const char *msg )
871{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000872 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000873 p->error = 1;
874}
875
Keith Whitwell15e75e02005-04-29 15:11:39 +0000876static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000877 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000878{
879 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000880 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000881 assert(!is_undef(p->src_texture[unit]));
882 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000883
Keith Whitwellce721142005-07-11 10:10:38 +0000884 case SRC_TEXTURE0:
885 case SRC_TEXTURE1:
886 case SRC_TEXTURE2:
887 case SRC_TEXTURE3:
888 case SRC_TEXTURE4:
889 case SRC_TEXTURE5:
890 case SRC_TEXTURE6:
891 case SRC_TEXTURE7:
892 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
893 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000894
Keith Whitwellce721142005-07-11 10:10:38 +0000895 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000896 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000897
Keith Whitwellce721142005-07-11 10:10:38 +0000898 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000899 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000900
Brian Paul6947f852009-01-23 17:32:09 -0700901 case SRC_ZERO:
902 return get_zero(p);
903
Keith Whitwellce721142005-07-11 10:10:38 +0000904 case SRC_PREVIOUS:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000905 if (is_undef(p->src_previous))
906 return register_input(p, FRAG_ATTRIB_COL0);
907 else
908 return p->src_previous;
Brian Paul6947f852009-01-23 17:32:09 -0700909
910 default:
911 assert(0);
José Fonsecac6af9b22009-06-15 19:20:25 +0100912 return undef;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000913 }
914}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000915
Keith Whitwell15e75e02005-04-29 15:11:39 +0000916static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000917 GLuint mask,
918 GLuint unit,
919 GLuint source,
920 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000921{
922 struct ureg arg, src, one;
923
924 src = get_source(p, source, unit);
925
926 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000927 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000928 /* Get unused tmp,
929 * Emit tmp = 1.0 - arg.xyzw
930 */
931 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000932 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000933 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000934
Keith Whitwellce721142005-07-11 10:10:38 +0000935 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000936 if (mask == WRITEMASK_W)
937 return src;
938 else
Brian Paul22db5352005-11-19 23:45:10 +0000939 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000940 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000941 /* Get unused tmp,
942 * Emit tmp = 1.0 - arg.wwww
943 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000944 arg = get_temp(p);
945 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000946 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000947 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000948 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000949 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000950 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000951 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000952 case OPR_SRC_COLOR:
Brian Paul6947f852009-01-23 17:32:09 -0700953 return src;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000954 default:
Brian Paul6947f852009-01-23 17:32:09 -0700955 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000956 return src;
957 }
958}
959
Brian Paulf337e2c2009-09-01 15:49:55 -0600960/**
961 * Check if the RGB and Alpha sources and operands match for the given
962 * texture unit's combinder state. When the RGB and A sources and
963 * operands match, we can emit fewer instructions.
964 */
Brian Paula5e70032009-08-31 11:17:59 -0600965static GLboolean args_match( const struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000966{
Brian Paul22db5352005-11-19 23:45:10 +0000967 GLuint i, nr = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000968
969 for (i = 0 ; i < nr ; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000970 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000971 return GL_FALSE;
972
Keith Whitwellce721142005-07-11 10:10:38 +0000973 switch(key->unit[unit].OptA[i].Operand) {
974 case OPR_SRC_ALPHA:
975 switch(key->unit[unit].OptRGB[i].Operand) {
976 case OPR_SRC_COLOR:
977 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000978 break;
979 default:
980 return GL_FALSE;
981 }
982 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000983 case OPR_ONE_MINUS_SRC_ALPHA:
984 switch(key->unit[unit].OptRGB[i].Operand) {
985 case OPR_ONE_MINUS_SRC_COLOR:
986 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000987 break;
988 default:
989 return GL_FALSE;
990 }
991 break;
992 default:
993 return GL_FALSE; /* impossible */
994 }
995 }
996
997 return GL_TRUE;
998}
999
Keith Whitwell15e75e02005-04-29 15:11:39 +00001000static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +00001001 struct ureg dest,
1002 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +00001003 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +00001004 GLuint unit,
1005 GLuint nr,
1006 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +00001007 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001008{
Brian Paulbd9b2be2009-03-08 17:58:54 -06001009 struct ureg src[MAX_COMBINER_TERMS];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001010 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +00001011 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001012
Brian Paulbd9b2be2009-03-08 17:58:54 -06001013 assert(nr <= MAX_COMBINER_TERMS);
Brian Paul6947f852009-01-23 17:32:09 -07001014
Brian Paul00630842005-12-12 15:27:55 +00001015 tmp = undef; /* silence warning (bug 5318) */
1016
Keith Whitwell15e75e02005-04-29 15:11:39 +00001017 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +00001018 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001019
1020 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +00001021 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001022 if (mask == WRITEMASK_XYZW && !saturate)
1023 return src[0];
1024 else
Brian Paul7e807512005-11-05 17:10:45 +00001025 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001026 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +00001027 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001028 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001029 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00001030 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001031 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001032 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001033 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001034 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +00001035 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001036 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +00001037 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +00001038 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
1039 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001040 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +00001041 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001042 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
1043 */
Brian Paul7e807512005-11-05 17:10:45 +00001044 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001045
Keith Whitwellce721142005-07-11 10:10:38 +00001046 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +00001047 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001048
Keith Whitwellce721142005-07-11 10:10:38 +00001049 case MODE_DOT3_RGBA:
1050 case MODE_DOT3_RGBA_EXT:
1051 case MODE_DOT3_RGB_EXT:
1052 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001053 struct ureg tmp0 = get_temp( p );
1054 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +00001055 struct ureg neg1 = register_scalar_const(p, -1);
1056 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001057
1058 /* tmp0 = 2*src0 - 1
1059 * tmp1 = 2*src1 - 1
1060 *
1061 * dst = tmp0 dot3 tmp1
1062 */
Brian Paul7e807512005-11-05 17:10:45 +00001063 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001064 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001065
Brian Paulaa206952005-09-16 18:14:24 +00001066 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001067 tmp1 = tmp0;
1068 else
Brian Paul7e807512005-11-05 17:10:45 +00001069 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001070 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +00001071 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001072 return dest;
1073 }
Keith Whitwellce721142005-07-11 10:10:38 +00001074 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001075 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001076 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001077 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +00001078 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001079 /* Arg0 * Arg2 + Arg1 - 0.5 */
1080 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001081 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +00001082 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
1083 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001084 return dest;
1085 }
Keith Whitwellce721142005-07-11 10:10:38 +00001086 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001087 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001088 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001089 return dest;
Brian Paul6947f852009-01-23 17:32:09 -07001090 case MODE_ADD_PRODUCTS:
1091 /* Arg0 * Arg1 + Arg2 * Arg3 */
1092 {
1093 struct ureg tmp0 = get_temp(p);
1094 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1095 emit_arith( p, OPCODE_MAD, dest, mask, saturate, src[2], src[3], tmp0 );
1096 }
1097 return dest;
1098 case MODE_ADD_PRODUCTS_SIGNED:
1099 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1100 {
1101 struct ureg tmp0 = get_temp(p);
1102 half = get_half(p);
1103 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1104 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[2], src[3], tmp0 );
1105 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1106 }
1107 return dest;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001108 case MODE_BUMP_ENVMAP_ATI:
1109 /* special - not handled here */
1110 assert(0);
1111 return src[0];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001112 default:
Brian Paul6947f852009-01-23 17:32:09 -07001113 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001114 return src[0];
1115 }
1116}
1117
Keith Whitwell15e75e02005-04-29 15:11:39 +00001118
Brian Paul22db5352005-11-19 23:45:10 +00001119/**
1120 * Generate instructions for one texture unit's env/combiner mode.
1121 */
1122static struct ureg
1123emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001124{
Brian Paula5e70032009-08-31 11:17:59 -06001125 const struct state_key *key = p->state;
Brian Paul956e6c32009-08-31 11:14:16 -06001126 GLboolean saturate;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001127 GLuint rgb_shift, alpha_shift;
Brian Paula5e70032009-08-31 11:17:59 -06001128 struct ureg out, dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001129
Keith Whitwellce721142005-07-11 10:10:38 +00001130 if (!key->unit[unit].enabled) {
1131 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001132 }
Roland Scheidegger114152e2009-03-12 15:01:16 +01001133 if (key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1134 /* this isn't really a env stage delivering a color and handled elsewhere */
1135 return get_source(p, SRC_PREVIOUS, 0);
1136 }
Keith Whitwellce721142005-07-11 10:10:38 +00001137
1138 switch (key->unit[unit].ModeRGB) {
1139 case MODE_DOT3_RGB_EXT:
1140 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001141 rgb_shift = 0;
1142 break;
Keith Whitwellce721142005-07-11 10:10:38 +00001143 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001144 alpha_shift = 0;
1145 rgb_shift = 0;
1146 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001147 default:
Keith Whitwellce721142005-07-11 10:10:38 +00001148 rgb_shift = key->unit[unit].ScaleShiftRGB;
1149 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001150 break;
1151 }
Keith Whitwellce721142005-07-11 10:10:38 +00001152
Brian Paul956e6c32009-08-31 11:14:16 -06001153 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
1154 * We don't want to clamp twice.
1155 */
1156 saturate = !(rgb_shift || alpha_shift);
1157
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001158 /* If this is the very last calculation, emit direct to output reg:
1159 */
Keith Whitwellce721142005-07-11 10:10:38 +00001160 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001161 unit != p->last_tex_stage ||
1162 alpha_shift ||
1163 rgb_shift)
1164 dest = get_temp( p );
1165 else
Brian Paul8d475822009-02-28 11:49:46 -07001166 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLOR);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001167
1168 /* Emit the RGB and A combine ops
1169 */
Keith Whitwellce721142005-07-11 10:10:38 +00001170 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
1171 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001172 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1173 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001174 key->unit[unit].NumArgsRGB,
1175 key->unit[unit].ModeRGB,
1176 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001177 }
Keith Whitwellce721142005-07-11 10:10:38 +00001178 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +02001179 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001180 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1181 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001182 key->unit[unit].NumArgsRGB,
1183 key->unit[unit].ModeRGB,
1184 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001185 }
1186 else {
1187 /* Need to do something to stop from re-emitting identical
1188 * argument calculations here:
1189 */
1190 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
1191 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001192 key->unit[unit].NumArgsRGB,
1193 key->unit[unit].ModeRGB,
1194 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001195 out = emit_combine( p, dest, WRITEMASK_W, saturate,
1196 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001197 key->unit[unit].NumArgsA,
1198 key->unit[unit].ModeA,
1199 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001200 }
1201
1202 /* Deal with the final shift:
1203 */
1204 if (alpha_shift || rgb_shift) {
Brian Paula5e70032009-08-31 11:17:59 -06001205 struct ureg shift;
1206
Brian Paul956e6c32009-08-31 11:14:16 -06001207 saturate = GL_TRUE; /* always saturate at this point */
Brian Paula5e70032009-08-31 11:17:59 -06001208
Keith Whitwell15e75e02005-04-29 15:11:39 +00001209 if (rgb_shift == alpha_shift) {
José Fonseca452a5922008-05-31 18:14:09 +09001210 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001211 }
1212 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001213 shift = register_const4f(p,
José Fonseca452a5922008-05-31 18:14:09 +09001214 (GLfloat)(1<<rgb_shift),
1215 (GLfloat)(1<<rgb_shift),
1216 (GLfloat)(1<<rgb_shift),
1217 (GLfloat)(1<<alpha_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001218 }
Brian Paul7e807512005-11-05 17:10:45 +00001219 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001220 saturate, out, shift, undef );
1221 }
1222 else
1223 return out;
1224}
1225
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001226
Brian Paul22db5352005-11-19 23:45:10 +00001227/**
1228 * Generate instruction for getting a texture source term.
1229 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001230static void load_texture( struct texenv_fragment_program *p, GLuint unit )
1231{
1232 if (is_undef(p->src_texture[unit])) {
Brian Paul44e018c2009-02-20 13:42:08 -07001233 GLuint texTarget = p->state->unit[unit].source_index;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001234 struct ureg texcoord;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001235 struct ureg tmp = get_tex_temp( p );
1236
Roland Scheidegger114152e2009-03-12 15:01:16 +01001237 if (is_undef(p->texcoord_tex[unit])) {
1238 texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
1239 }
1240 else {
1241 /* might want to reuse this reg for tex output actually */
1242 texcoord = p->texcoord_tex[unit];
1243 }
1244
Brian Paul44e018c2009-02-20 13:42:08 -07001245 if (texTarget == TEXTURE_UNKNOWN_INDEX)
Brian Paulbfb5ea32005-07-19 18:20:04 +00001246 program_error(p, "TexSrcBit");
Brian Paulf337e2c2009-09-01 15:49:55 -06001247
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001248 /* TODO: Use D0_MASK_XY where possible.
1249 */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +02001250 if (p->state->unit[unit].enabled) {
Brian Paul44e018c2009-02-20 13:42:08 -07001251 GLboolean shadow = GL_FALSE;
1252
1253 if (p->state->unit[unit].shadow) {
1254 p->program->Base.ShadowSamplers |= 1 << unit;
1255 shadow = GL_TRUE;
1256 }
1257
Aapo Tahkolab8915342006-03-28 10:26:34 +00001258 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1259 tmp, WRITEMASK_XYZW,
Brian Paul44e018c2009-02-20 13:42:08 -07001260 unit, texTarget, shadow,
1261 texcoord );
Brian9b7e5a52007-12-14 11:16:49 -07001262
1263 p->program->Base.SamplersUsed |= (1 << unit);
Brian1fe385f2007-12-14 11:42:28 -07001264 /* This identity mapping should already be in place
1265 * (see _mesa_init_program_struct()) but let's be safe.
1266 */
1267 p->program->Base.SamplerUnits[unit] = unit;
Brian9b7e5a52007-12-14 11:16:49 -07001268 }
1269 else
Aapo Tahkolab8915342006-03-28 10:26:34 +00001270 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001271 }
1272}
1273
Keith Whitwell241b6b72005-05-23 09:50:34 +00001274static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +00001275 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001276{
1277 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +00001278 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001279 load_texture(p, unit);
1280 break;
1281
Keith Whitwellce721142005-07-11 10:10:38 +00001282 case SRC_TEXTURE0:
1283 case SRC_TEXTURE1:
1284 case SRC_TEXTURE2:
1285 case SRC_TEXTURE3:
1286 case SRC_TEXTURE4:
1287 case SRC_TEXTURE5:
1288 case SRC_TEXTURE6:
1289 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +00001290 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001291 break;
1292
1293 default:
Brian Paul6947f852009-01-23 17:32:09 -07001294 /* not a texture src - do nothing */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001295 break;
1296 }
Keith Whitwell241b6b72005-05-23 09:50:34 +00001297
1298 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001299}
1300
Brian Paul22db5352005-11-19 23:45:10 +00001301
1302/**
1303 * Generate instructions for loading all texture source terms.
1304 */
1305static GLboolean
1306load_texunit_sources( struct texenv_fragment_program *p, int unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001307{
Brian Paula5e70032009-08-31 11:17:59 -06001308 const struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +00001309 GLuint i;
1310
1311 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001312 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001313 }
Aapo Tahkolab8915342006-03-28 10:26:34 +00001314
1315 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1316 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1317 }
1318
Keith Whitwell241b6b72005-05-23 09:50:34 +00001319 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001320}
1321
Roland Scheidegger114152e2009-03-12 15:01:16 +01001322/**
1323 * Generate instructions for loading bump map textures.
1324 */
1325static GLboolean
1326load_texunit_bumpmap( struct texenv_fragment_program *p, int unit )
1327{
Brian Paula5e70032009-08-31 11:17:59 -06001328 const struct state_key *key = p->state;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001329 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1330 struct ureg texcDst, bumpMapRes;
1331 struct ureg constdudvcolor = register_const4f(p, 0.0, 0.0, 0.0, 1.0);
1332 struct ureg texcSrc = register_input(p, FRAG_ATTRIB_TEX0 + bumpedUnitNr);
1333 struct ureg rotMat0 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_0, unit );
1334 struct ureg rotMat1 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_1, unit );
1335
1336 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1337
1338 bumpMapRes = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1339 texcDst = get_tex_temp( p );
1340 p->texcoord_tex[bumpedUnitNr] = texcDst;
1341
1342 /* apply rot matrix and add coords to be available in next phase */
1343 /* dest = (Arg0.xxxx * rotMat0 + Arg1) + (Arg0.yyyy * rotMat1) */
1344 /* note only 2 coords are affected the rest are left unchanged (mul by 0) */
1345 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1346 swizzle1(bumpMapRes, SWIZZLE_X), rotMat0, texcSrc );
1347 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1348 swizzle1(bumpMapRes, SWIZZLE_Y), rotMat1, texcDst );
1349
1350 /* move 0,0,0,1 into bumpmap src if someone (crossbar) is foolish
1351 enough to access this later, should optimize away */
1352 emit_arith( p, OPCODE_MOV, bumpMapRes, WRITEMASK_XYZW, 0, constdudvcolor, undef, undef );
1353
1354 return GL_TRUE;
1355}
Brian Paul22db5352005-11-19 23:45:10 +00001356
1357/**
1358 * Generate a new fragment program which implements the context's
1359 * current texture env/combine mode.
1360 */
1361static void
Brian Paule998c342006-10-29 21:17:18 +00001362create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001363 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001364{
Brian Paule998c342006-10-29 21:17:18 +00001365 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001366 struct texenv_fragment_program p;
1367 GLuint unit;
1368 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001369
Keith Whitwelle4902422005-05-11 15:16:35 +00001370 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +00001371 p.ctx = ctx;
Keith Whitwellce721142005-07-11 10:10:38 +00001372 p.state = key;
1373 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001374
Brian Paule998c342006-10-29 21:17:18 +00001375 /* During code generation, use locally-allocated instruction buffer,
1376 * then alloc dynamic storage below.
1377 */
1378 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001379 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001380 p.program->Base.NumTexIndirections = 1;
Brian21f99792007-01-09 11:00:21 -07001381 p.program->Base.NumTexInstructions = 0;
1382 p.program->Base.NumAluInstructions = 0;
Brian1240eb22007-03-22 08:50:20 -06001383 p.program->Base.String = NULL;
Keith Whitwell9ca88152005-05-10 09:56:02 +00001384 p.program->Base.NumInstructions =
Brian Paule998c342006-10-29 21:17:18 +00001385 p.program->Base.NumTemporaries =
1386 p.program->Base.NumParameters =
1387 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001388 p.program->Base.Parameters = _mesa_new_parameter_list();
Keith Whitwelldbeea252005-05-10 13:57:50 +00001389
Brian Paulde997602005-11-12 17:53:14 +00001390 p.program->Base.InputsRead = 0;
Brian Paul8d475822009-02-28 11:49:46 -07001391 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLOR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001392
Roland Scheidegger114152e2009-03-12 15:01:16 +01001393 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001394 p.src_texture[unit] = undef;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001395 p.texcoord_tex[unit] = undef;
1396 }
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001397
Keith Whitwell47b29f52005-05-04 11:44:44 +00001398 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001399 p.half = undef;
1400 p.zero = undef;
1401 p.one = undef;
1402
Keith Whitwell15e75e02005-04-29 15:11:39 +00001403 p.last_tex_stage = 0;
Brian93fef222007-10-29 12:25:46 -06001404 release_temps(ctx, &p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001405
Keith Whitwellce721142005-07-11 10:10:38 +00001406 if (key->enabled_units) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001407 GLboolean needbumpstage = GL_FALSE;
1408 /* Zeroth pass - bump map textures first */
1409 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
1410 if (key->unit[unit].enabled && key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1411 needbumpstage = GL_TRUE;
1412 load_texunit_bumpmap( &p, unit );
1413 }
1414 if (needbumpstage)
1415 p.program->Base.NumTexIndirections++;
1416
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001417 /* First pass - to support texture_env_crossbar, first identify
1418 * all referenced texture sources and emit texld instructions
1419 * for each:
1420 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001421 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001422 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001423 load_texunit_sources( &p, unit );
1424 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001425 }
1426
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001427 /* Second pass - emit combine instructions to build final color:
1428 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001429 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001430 if (key->enabled_units & (1<<unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001431 p.src_previous = emit_texenv( &p, unit );
Brian Paul5620c202008-09-26 11:18:06 -06001432 reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
Brian93fef222007-10-29 12:25:46 -06001433 release_temps(ctx, &p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001434 }
1435 }
1436
Keith Whitwellce721142005-07-11 10:10:38 +00001437 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul8d475822009-02-28 11:49:46 -07001438 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLOR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001439
Keith Whitwellce721142005-07-11 10:10:38 +00001440 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001441 /* Emit specular add.
1442 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001443 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001444 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1445 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001446 }
Brian Paulaa206952005-09-16 18:14:24 +00001447 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001448 /* Will wind up in here if no texture enabled or a couple of
1449 * other scenarios (GL_REPLACE for instance).
1450 */
Brian Paul7e807512005-11-05 17:10:45 +00001451 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001452 }
1453
Keith Whitwell47b29f52005-05-04 11:44:44 +00001454 /* Finish up:
1455 */
Brian Paul7e807512005-11-05 17:10:45 +00001456 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001457
Keith Whitwellce721142005-07-11 10:10:38 +00001458 if (key->fog_enabled) {
1459 /* Pull fog mode from GLcontext, the value in the state key is
1460 * a reduced value and not what is expected in FogOption
1461 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001462 p.program->FogOption = ctx->Fog.Mode;
Brian19d77d62007-09-18 19:29:26 -06001463 p.program->Base.InputsRead |= FRAG_BIT_FOGC; /* XXX new */
Keith Whitwellce721142005-07-11 10:10:38 +00001464 } else
Keith Whitwell276330b2005-05-09 17:58:13 +00001465 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001466
Brian21f99792007-01-09 11:00:21 -07001467 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001468 program_error(&p, "Exceeded max nr indirect texture lookups");
1469
Brian21f99792007-01-09 11:00:21 -07001470 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001471 program_error(&p, "Exceeded max TEX instructions");
1472
Brian21f99792007-01-09 11:00:21 -07001473 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001474 program_error(&p, "Exceeded max ALU instructions");
1475
Brian Paul5d7b49f2005-11-19 23:12:20 +00001476 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001477
Brian Paule998c342006-10-29 21:17:18 +00001478 /* Allocate final instruction array */
Brianc81cce72007-09-25 15:18:51 -06001479 p.program->Base.Instructions
1480 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1481 if (!p.program->Base.Instructions) {
Brian Paule998c342006-10-29 21:17:18 +00001482 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1483 "generating tex env program");
1484 return;
1485 }
Brianc81cce72007-09-25 15:18:51 -06001486 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1487 p.program->Base.NumInstructions);
1488
1489 if (p.program->FogOption) {
1490 _mesa_append_fog_code(ctx, p.program);
1491 p.program->FogOption = GL_NONE;
1492 }
1493
Brian Paule998c342006-10-29 21:17:18 +00001494
Keith Whitwelldbeea252005-05-10 13:57:50 +00001495 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001496 */
Brian Paule998c342006-10-29 21:17:18 +00001497 if (ctx->Driver.ProgramStringNotify) {
1498 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1499 &p.program->Base );
1500 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001501
Brian Paule998c342006-10-29 21:17:18 +00001502 if (DISASSEM) {
1503 _mesa_print_program(&p.program->Base);
1504 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001505 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001506}
1507
Brian Paul5d7b49f2005-11-19 23:12:20 +00001508
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001509/**
1510 * Return a fragment program which implements the current
1511 * fixed-function texture, fog and color-sum operations.
1512 */
1513struct gl_fragment_program *
1514_mesa_get_fixed_func_fragment_program(GLcontext *ctx)
Keith Whitwellce721142005-07-11 10:10:38 +00001515{
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001516 struct gl_fragment_program *prog;
1517 struct state_key key;
1518
1519 make_state_key(ctx, &key);
1520
1521 prog = (struct gl_fragment_program *)
1522 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1523 &key, sizeof(key));
Keith Whitwellce721142005-07-11 10:10:38 +00001524
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001525 if (!prog) {
1526 prog = (struct gl_fragment_program *)
1527 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1528
1529 create_new_program(ctx, &key, prog);
1530
1531 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1532 &key, sizeof(key), &prog->Base);
Keith Whitwellce721142005-07-11 10:10:38 +00001533 }
1534
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001535 return prog;
Keith Whitwellce721142005-07-11 10:10:38 +00001536}