blob: 1ee78ffa16e2e378eeab3bd3212d12f9795a9f0d [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 Paul5d7b49f2005-11-19 23:12:20 +000086 GLuint Source:4;
87 GLuint Operand:3;
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;
Roland Scheidegger114152e2009-03-12 15:01:16 +0100106 GLuint ModeRGB:5;
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;
Roland Scheidegger114152e2009-03-12 15:01:16 +0100110 GLuint ModeA:5;
Brian Paulbd9b2be2009-03-08 17:58:54 -0600111 struct mode_opt OptA[MAX_COMBINER_TERMS];
Keith Whitwellce721142005-07-11 10:10:38 +0000112 } unit[8];
113};
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
243#define TEXTURE_UNKNOWN_INDEX 7
Brian Paule00ac112005-09-15 05:00:45 +0000244static GLuint translate_tex_src_bit( GLbitfield bit )
Keith Whitwellce721142005-07-11 10:10:38 +0000245{
Brian Paul1519b932008-12-17 13:17:15 -0700246 /* make sure number of switch cases is correct */
247 assert(NUM_TEXTURE_TARGETS == 7);
Keith Whitwellce721142005-07-11 10:10:38 +0000248 switch (bit) {
249 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
250 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
251 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
252 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
253 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
Brian Paul6947f852009-01-23 17:32:09 -0700254 case TEXTURE_1D_ARRAY_BIT: return TEXTURE_1D_ARRAY_INDEX;
255 case TEXTURE_2D_ARRAY_BIT: return TEXTURE_2D_ARRAY_INDEX;
256 default:
257 assert(0);
258 return TEXTURE_UNKNOWN_INDEX;
Keith Whitwellce721142005-07-11 10:10:38 +0000259 }
260}
261
Keith Whitwell1680ef82008-10-03 17:30:59 +0100262#define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
263#define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
264
Brian Paul239617f2008-10-07 11:22:47 -0600265/**
266 * Identify all possible varying inputs. The fragment program will
Keith Whitwell1680ef82008-10-03 17:30:59 +0100267 * never reference non-varying inputs, but will track them via state
268 * constants instead.
269 *
270 * This function figures out all the inputs that the fragment program
271 * has access to. The bitmask is later reduced to just those which
272 * are actually referenced.
273 */
Brian Paul239617f2008-10-07 11:22:47 -0600274static GLbitfield get_fp_input_mask( GLcontext *ctx )
Keith Whitwell1680ef82008-10-03 17:30:59 +0100275{
Eric Anholte5f63c42009-06-02 07:47:20 -0700276 /* _NEW_PROGRAM */
Brian Paulf0b07942008-12-17 14:04:03 -0700277 const GLboolean vertexShader = (ctx->Shader.CurrentProgram &&
Eric Anholt40990d92009-08-03 12:36:52 -0700278 ctx->Shader.CurrentProgram->LinkStatus &&
Brian Paulf0b07942008-12-17 14:04:03 -0700279 ctx->Shader.CurrentProgram->VertexProgram);
280 const GLboolean vertexProgram = ctx->VertexProgram._Enabled;
Brian Paul239617f2008-10-07 11:22:47 -0600281 GLbitfield fp_inputs = 0x0;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100282
Brian Paul6d4d51d2008-10-10 13:39:14 -0600283 if (ctx->VertexProgram._Overriden) {
284 /* Somebody's messing with the vertex program and we don't have
285 * a clue what's happening. Assume that it could be producing
286 * all possible outputs.
287 */
288 fp_inputs = ~0;
289 }
290 else if (ctx->RenderMode == GL_FEEDBACK) {
Eric Anholte5f63c42009-06-02 07:47:20 -0700291 /* _NEW_RENDERMODE */
Brian Paul6d4d51d2008-10-10 13:39:14 -0600292 fp_inputs = (FRAG_BIT_COL0 | FRAG_BIT_TEX0);
293 }
Brian Paulf0b07942008-12-17 14:04:03 -0700294 else if (!(vertexProgram || vertexShader) ||
Brian Pauld7296a12008-12-17 11:29:06 -0700295 !ctx->VertexProgram._Current) {
Brian Paulf0b07942008-12-17 14:04:03 -0700296 /* Fixed function vertex logic */
Eric Anholte5f63c42009-06-02 07:47:20 -0700297 /* _NEW_ARRAY */
Brian Paul239617f2008-10-07 11:22:47 -0600298 GLbitfield varying_inputs = ctx->varying_vp_inputs;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100299
Keith Whitwell97e63432008-10-20 13:03:45 +0100300 /* These get generated in the setup routine regardless of the
301 * vertex program:
302 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700303 /* _NEW_POINT */
Keith Whitwell97e63432008-10-20 13:03:45 +0100304 if (ctx->Point.PointSprite)
305 varying_inputs |= FRAG_BITS_TEX_ANY;
306
Keith Whitwell1680ef82008-10-03 17:30:59 +0100307 /* First look at what values may be computed by the generated
308 * vertex program:
309 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700310 /* _NEW_LIGHT */
Keith Whitwell1680ef82008-10-03 17:30:59 +0100311 if (ctx->Light.Enabled) {
312 fp_inputs |= FRAG_BIT_COL0;
313
Eric Anholt9cea84b2009-07-16 18:41:03 -0700314 if (texenv_doing_secondary_color(ctx))
Keith Whitwell1680ef82008-10-03 17:30:59 +0100315 fp_inputs |= FRAG_BIT_COL1;
316 }
317
Eric Anholte5f63c42009-06-02 07:47:20 -0700318 /* _NEW_TEXTURE */
Keith Whitwell1680ef82008-10-03 17:30:59 +0100319 fp_inputs |= (ctx->Texture._TexGenEnabled |
320 ctx->Texture._TexMatEnabled) << FRAG_ATTRIB_TEX0;
321
322 /* Then look at what might be varying as a result of enabled
323 * arrays, etc:
324 */
325 if (varying_inputs & VERT_BIT_COLOR0) fp_inputs |= FRAG_BIT_COL0;
326 if (varying_inputs & VERT_BIT_COLOR1) fp_inputs |= FRAG_BIT_COL1;
327
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
Keith Whitwell1680ef82008-10-03 17:30:59 +0100355 if (vp_outputs & (1 << VERT_RESULT_COL0)) fp_inputs |= FRAG_BIT_COL0;
356 if (vp_outputs & (1 << VERT_RESULT_COL1)) fp_inputs |= FRAG_BIT_COL1;
357
Keith Whitwell0370d6b2008-10-04 12:41:56 +0100358 fp_inputs |= (((vp_outputs & VERT_RESULT_TEX_ANY) >> VERT_RESULT_TEX0)
359 << FRAG_ATTRIB_TEX0);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100360 }
361
362 return fp_inputs;
363}
364
365
Brian Paul22db5352005-11-19 23:45:10 +0000366/**
367 * Examine current texture environment state and generate a unique
368 * key to identify it.
369 */
Keith Whitwell8065c122006-05-22 16:09:27 +0000370static void make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000371{
Keith Whitwellce721142005-07-11 10:10:38 +0000372 GLuint i, j;
Brian Paul239617f2008-10-07 11:22:47 -0600373 GLbitfield inputs_referenced = FRAG_BIT_COL0;
374 GLbitfield inputs_available = get_fp_input_mask( ctx );
Keith Whitwell1680ef82008-10-03 17:30:59 +0100375
Keith Whitwell8065c122006-05-22 16:09:27 +0000376 memset(key, 0, sizeof(*key));
377
Eric Anholte5f63c42009-06-02 07:47:20 -0700378 /* _NEW_TEXTURE */
Brian Paule9b34882008-12-31 11:54:02 -0700379 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000380 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800381 GLenum format;
Roland Scheideggerbf4a0fa2008-02-15 17:26:06 +0100382
383 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
Keith Whitwellce721142005-07-11 10:10:38 +0000384 continue;
385
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800386 format = texUnit->_Current->Image[0][texUnit->_Current->BaseLevel]->_BaseFormat;
387
Keith Whitwellce721142005-07-11 10:10:38 +0000388 key->unit[i].enabled = 1;
389 key->enabled_units |= (1<<i);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100390 key->nr_enabled_units = i+1;
391 inputs_referenced |= FRAG_BIT_TEX(i);
Keith Whitwellce721142005-07-11 10:10:38 +0000392
393 key->unit[i].source_index =
394 translate_tex_src_bit(texUnit->_ReallyEnabled);
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800395 key->unit[i].shadow = ((texUnit->_Current->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
396 ((format == GL_DEPTH_COMPONENT) ||
397 (format == GL_DEPTH_STENCIL_EXT)));
Keith Whitwellce721142005-07-11 10:10:38 +0000398
399 key->unit[i].NumArgsRGB = texUnit->_CurrentCombine->_NumArgsRGB;
400 key->unit[i].NumArgsA = texUnit->_CurrentCombine->_NumArgsA;
401
402 key->unit[i].ModeRGB =
Brian Paul6947f852009-01-23 17:32:09 -0700403 translate_mode(texUnit->EnvMode, texUnit->_CurrentCombine->ModeRGB);
Keith Whitwellce721142005-07-11 10:10:38 +0000404 key->unit[i].ModeA =
Brian Paul6947f852009-01-23 17:32:09 -0700405 translate_mode(texUnit->EnvMode, texUnit->_CurrentCombine->ModeA);
Roland Scheidegger114152e2009-03-12 15:01:16 +0100406
Keith Whitwellce721142005-07-11 10:10:38 +0000407 key->unit[i].ScaleShiftRGB = texUnit->_CurrentCombine->ScaleShiftRGB;
Keith Whitwell64da1612006-05-22 14:30:58 +0000408 key->unit[i].ScaleShiftA = texUnit->_CurrentCombine->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000409
Brian Paulbd9b2be2009-03-08 17:58:54 -0600410 for (j = 0; j < MAX_COMBINER_TERMS; j++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000411 key->unit[i].OptRGB[j].Operand =
412 translate_operand(texUnit->_CurrentCombine->OperandRGB[j]);
413 key->unit[i].OptA[j].Operand =
414 translate_operand(texUnit->_CurrentCombine->OperandA[j]);
415 key->unit[i].OptRGB[j].Source =
416 translate_source(texUnit->_CurrentCombine->SourceRGB[j]);
417 key->unit[i].OptA[j].Source =
418 translate_source(texUnit->_CurrentCombine->SourceA[j]);
419 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100420
421 if (key->unit[i].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
422 /* requires some special translation */
423 key->unit[i].NumArgsRGB = 2;
424 key->unit[i].ScaleShiftRGB = 0;
425 key->unit[i].OptRGB[0].Operand = OPR_SRC_COLOR;
426 key->unit[i].OptRGB[0].Source = SRC_TEXTURE;
427 key->unit[i].OptRGB[1].Operand = OPR_SRC_COLOR;
428 key->unit[i].OptRGB[1].Source = texUnit->BumpTarget - GL_TEXTURE0 + SRC_TEXTURE0;
429 }
Keith Whitwellce721142005-07-11 10:10:38 +0000430 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100431
Eric Anholt9cea84b2009-07-16 18:41:03 -0700432 /* _NEW_LIGHT | _NEW_FOG */
433 if (texenv_doing_secondary_color(ctx)) {
Keith Whitwellce721142005-07-11 10:10:38 +0000434 key->separate_specular = 1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100435 inputs_referenced |= FRAG_BIT_COL1;
436 }
Keith Whitwellce721142005-07-11 10:10:38 +0000437
Eric Anholte5f63c42009-06-02 07:47:20 -0700438 /* _NEW_FOG */
Keith Whitwellce721142005-07-11 10:10:38 +0000439 if (ctx->Fog.Enabled) {
440 key->fog_enabled = 1;
441 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100442 inputs_referenced |= FRAG_BIT_FOGC; /* maybe */
Keith Whitwellce721142005-07-11 10:10:38 +0000443 }
Keith Whitwell1680ef82008-10-03 17:30:59 +0100444
445 key->inputs_available = (inputs_available & inputs_referenced);
Keith Whitwellce721142005-07-11 10:10:38 +0000446}
447
Brian Paul239617f2008-10-07 11:22:47 -0600448/**
449 * Use uregs to represent registers internally, translate to Mesa's
Keith Whitwell15e75e02005-04-29 15:11:39 +0000450 * expected formats on emit.
451 *
452 * NOTE: These are passed by value extensively in this file rather
453 * than as usual by pointer reference. If this disturbs you, try
454 * remembering they are just 32bits in size.
455 *
456 * GCC is smart enough to deal with these dword-sized structures in
457 * much the same way as if I had defined them as dwords and was using
458 * macros to access and set the fields. This is much nicer and easier
459 * to evolve.
460 */
461struct ureg {
462 GLuint file:4;
463 GLuint idx:8;
464 GLuint negatebase:1;
465 GLuint abs:1;
466 GLuint negateabs:1;
467 GLuint swz:12;
468 GLuint pad:5;
469};
470
Brian Paul13abf912006-04-13 19:17:13 +0000471static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000472 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000473 ~0,
474 0,
475 0,
476 0,
477 0,
478 0
479};
480
Keith Whitwell15e75e02005-04-29 15:11:39 +0000481
Brian Paul239617f2008-10-07 11:22:47 -0600482/** State used to build the fragment program:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000483 */
484struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000485 struct gl_fragment_program *program;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000486 GLcontext *ctx;
Keith Whitwellce721142005-07-11 10:10:38 +0000487 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000488
Brian Paul239617f2008-10-07 11:22:47 -0600489 GLbitfield alu_temps; /**< Track texture indirections, see spec. */
490 GLbitfield temps_output; /**< Track texture indirections, see spec. */
491 GLbitfield temp_in_use; /**< Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000492 GLboolean error;
493
Brian Paule9b34882008-12-31 11:54:02 -0700494 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000495 /* Reg containing each texture unit's sampled texture color,
496 * else undef.
497 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000498
Roland Scheidegger114152e2009-03-12 15:01:16 +0100499 struct ureg texcoord_tex[MAX_TEXTURE_COORD_UNITS];
500 /* Reg containing texcoord for a texture unit,
501 * needed for bump mapping, else undef.
502 */
503
Brian Paul239617f2008-10-07 11:22:47 -0600504 struct ureg src_previous; /**< Reg containing color from previous
Keith Whitwell15e75e02005-04-29 15:11:39 +0000505 * stage. May need to be decl'd.
506 */
507
Brian Paul239617f2008-10-07 11:22:47 -0600508 GLuint last_tex_stage; /**< Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000509
510 struct ureg half;
511 struct ureg one;
512 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000513};
514
515
516
517static struct ureg make_ureg(GLuint file, GLuint idx)
518{
519 struct ureg reg;
520 reg.file = file;
521 reg.idx = idx;
522 reg.negatebase = 0;
523 reg.abs = 0;
524 reg.negateabs = 0;
525 reg.swz = SWIZZLE_NOOP;
526 reg.pad = 0;
527 return reg;
528}
529
530static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
531{
532 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
533 GET_SWZ(reg.swz, y),
534 GET_SWZ(reg.swz, z),
535 GET_SWZ(reg.swz, w));
536
537 return reg;
538}
539
540static struct ureg swizzle1( struct ureg reg, int x )
541{
542 return swizzle(reg, x, x, x, x);
543}
544
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000545static struct ureg negate( struct ureg reg )
546{
547 reg.negatebase ^= 1;
548 return reg;
549}
550
Keith Whitwell15e75e02005-04-29 15:11:39 +0000551static GLboolean is_undef( struct ureg reg )
552{
Alan Hourihane8d972652006-08-10 13:12:00 +0000553 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000554}
555
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000556
Keith Whitwell15e75e02005-04-29 15:11:39 +0000557static struct ureg get_temp( struct texenv_fragment_program *p )
558{
Brian Paul13abf912006-04-13 19:17:13 +0000559 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000560
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000561 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000562 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000563 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000564
565 /* Then any unused temporary:
566 */
567 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000568 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000569
Keith Whitwell15e75e02005-04-29 15:11:39 +0000570 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000571 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000572 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000573 }
574
Brian Paul13abf912006-04-13 19:17:13 +0000575 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000576 p->program->Base.NumTemporaries = bit;
577
Keith Whitwell93cd9232005-05-11 08:30:23 +0000578 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000579 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
580}
581
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000582static struct ureg get_tex_temp( struct texenv_fragment_program *p )
583{
584 int bit;
585
Brian Pauld01269a2008-09-25 18:27:22 -0600586 /* First try to find available temp not previously used (to avoid
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000587 * starting a new texture indirection). According to the spec, the
588 * ~p->temps_output isn't necessary, but will keep it there for
589 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000590 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000591 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000592
593 /* Then any unused temporary:
594 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000595 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000596 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000597
598 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000599 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000600 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000601 }
602
Brian Paul13abf912006-04-13 19:17:13 +0000603 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000604 p->program->Base.NumTemporaries = bit;
605
Keith Whitwell93cd9232005-05-11 08:30:23 +0000606 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000607 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
608}
609
Keith Whitwell15e75e02005-04-29 15:11:39 +0000610
Brian Paul5620c202008-09-26 11:18:06 -0600611/** Mark a temp reg as being no longer allocatable. */
612static void reserve_temp( struct texenv_fragment_program *p, struct ureg r )
613{
614 if (r.file == PROGRAM_TEMPORARY)
615 p->temps_output |= (1 << r.idx);
616}
617
618
Brian93fef222007-10-29 12:25:46 -0600619static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000620{
Brian93fef222007-10-29 12:25:46 -0600621 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000622
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000623 /* KW: To support tex_env_crossbar, don't release the registers in
624 * temps_output.
625 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000626 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000627 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000628 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000629 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000630}
631
632
Brian9fe3e2e2007-02-23 11:44:14 -0700633static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000634 GLint s0,
635 GLint s1,
636 GLint s2,
637 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700638 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000639{
Brian9fe3e2e2007-02-23 11:44:14 -0700640 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000641 GLuint idx;
642 tokens[0] = s0;
643 tokens[1] = s1;
644 tokens[2] = s2;
645 tokens[3] = s3;
646 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000647 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000648 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000649}
650
Keith Whitwell47b29f52005-05-04 11:44:44 +0000651
Brian9fe3e2e2007-02-23 11:44:14 -0700652#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
653#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
654#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
655#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000656
Keith Whitwell1680ef82008-10-03 17:30:59 +0100657static GLuint frag_to_vert_attrib( GLuint attrib )
658{
659 switch (attrib) {
660 case FRAG_ATTRIB_COL0: return VERT_ATTRIB_COLOR0;
661 case FRAG_ATTRIB_COL1: return VERT_ATTRIB_COLOR1;
662 default:
663 assert(attrib >= FRAG_ATTRIB_TEX0);
664 assert(attrib <= FRAG_ATTRIB_TEX7);
665 return attrib - FRAG_ATTRIB_TEX0 + VERT_ATTRIB_TEX0;
666 }
667}
668
Keith Whitwell47b29f52005-05-04 11:44:44 +0000669
670static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
671{
Keith Whitwell1680ef82008-10-03 17:30:59 +0100672 if (p->state->inputs_available & (1<<input)) {
673 p->program->Base.InputsRead |= (1 << input);
674 return make_ureg(PROGRAM_INPUT, input);
675 }
676 else {
677 GLuint idx = frag_to_vert_attrib( input );
678 return register_param3( p, STATE_INTERNAL, STATE_CURRENT_ATTRIB, idx );
679 }
Keith Whitwell47b29f52005-05-04 11:44:44 +0000680}
681
682
Brian Paul7e807512005-11-05 17:10:45 +0000683static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000684 struct ureg ureg )
685{
686 reg->File = ureg.file;
687 reg->Index = ureg.idx;
688 reg->Swizzle = ureg.swz;
Brian Paul7db7ff82009-04-14 22:14:30 -0600689 reg->Negate = ureg.negatebase ? NEGATE_XYZW : NEGATE_NONE;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000690 reg->Abs = ureg.abs;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000691}
692
Brian Paul7e807512005-11-05 17:10:45 +0000693static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000694 struct ureg ureg, GLuint mask )
695{
696 dst->File = ureg.file;
697 dst->Index = ureg.idx;
698 dst->WriteMask = mask;
Brianc9d495c2007-10-23 10:55:24 -0600699 dst->CondMask = COND_TR; /* always pass cond test */
700 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000701}
702
Brian Paul7e807512005-11-05 17:10:45 +0000703static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000704emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000705 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000706 struct ureg dest,
707 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000708 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000709 struct ureg src0,
710 struct ureg src1,
711 struct ureg src2 )
712{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000713 GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000714 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Brian2a8e9bb2007-10-23 10:24:53 -0600715
716 assert(nr < MAX_INSTRUCTIONS);
717
Brian Pauld6272e02006-10-29 18:03:16 +0000718 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000719 inst->Opcode = op;
720
Keith Whitwell47b29f52005-05-04 11:44:44 +0000721 emit_arg( &inst->SrcReg[0], src0 );
722 emit_arg( &inst->SrcReg[1], src1 );
723 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000724
Brian Paule31ac052005-11-20 17:52:40 +0000725 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000726
727 emit_dst( &inst->DstReg, dest, mask );
728
Brian Paul5620c202008-09-26 11:18:06 -0600729#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000730 /* Accounting for indirection tracking:
731 */
732 if (dest.file == PROGRAM_TEMPORARY)
733 p->temps_output |= 1 << dest.idx;
Brian Paul5620c202008-09-26 11:18:06 -0600734#endif
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000735
Keith Whitwell15e75e02005-04-29 15:11:39 +0000736 return inst;
737}
738
739
740static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000741 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000742 struct ureg dest,
743 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000744 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000745 struct ureg src0,
746 struct ureg src1,
747 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000748{
749 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
750
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000751 /* Accounting for indirection tracking:
752 */
753 if (src0.file == PROGRAM_TEMPORARY)
754 p->alu_temps |= 1 << src0.idx;
755
756 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
757 p->alu_temps |= 1 << src1.idx;
758
759 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
760 p->alu_temps |= 1 << src2.idx;
761
762 if (dest.file == PROGRAM_TEMPORARY)
763 p->alu_temps |= 1 << dest.idx;
764
Brian21f99792007-01-09 11:00:21 -0700765 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000766 return dest;
767}
768
769static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000770 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000771 struct ureg dest,
772 GLuint destmask,
773 GLuint tex_unit,
774 GLuint tex_idx,
Brian Paul44e018c2009-02-20 13:42:08 -0700775 GLuint tex_shadow,
Keith Whitwellce721142005-07-11 10:10:38 +0000776 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000777{
Brian Paul7e807512005-11-05 17:10:45 +0000778 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000779 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000780 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000781 coord, /* arg 0? */
782 undef,
783 undef);
784
Brian Paul7e807512005-11-05 17:10:45 +0000785 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000786 inst->TexSrcUnit = tex_unit;
Brian Paul44e018c2009-02-20 13:42:08 -0700787 inst->TexShadow = tex_shadow;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000788
Brian21f99792007-01-09 11:00:21 -0700789 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000790
Brian Paul5620c202008-09-26 11:18:06 -0600791 /* Accounting for indirection tracking:
792 */
793 reserve_temp(p, dest);
794
Roland Scheidegger114152e2009-03-12 15:01:16 +0100795#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000796 /* Is this a texture indirection?
797 */
798 if ((coord.file == PROGRAM_TEMPORARY &&
799 (p->temps_output & (1<<coord.idx))) ||
800 (dest.file == PROGRAM_TEMPORARY &&
801 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700802 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000803 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000804 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000805 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000806 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100807#endif
Keith Whitwell15e75e02005-04-29 15:11:39 +0000808
809 return dest;
810}
811
812
Keith Whitwell47b29f52005-05-04 11:44:44 +0000813static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000814 GLfloat s0,
815 GLfloat s1,
816 GLfloat s2,
817 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000818{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000819 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700820 GLuint idx, swizzle;
Brian Pauld01269a2008-09-25 18:27:22 -0600821 struct ureg r;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000822 values[0] = s0;
823 values[1] = s1;
824 values[2] = s2;
825 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700826 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
827 &swizzle );
Brian Pauld01269a2008-09-25 18:27:22 -0600828 r = make_ureg(PROGRAM_CONSTANT, idx);
829 r.swz = swizzle;
830 return r;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000831}
832
Keith Whitwelle4902422005-05-11 15:16:35 +0000833#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000834#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
835#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
836#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000837
838
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000839static struct ureg get_one( struct texenv_fragment_program *p )
840{
841 if (is_undef(p->one))
842 p->one = register_scalar_const(p, 1.0);
843 return p->one;
844}
845
846static struct ureg get_half( struct texenv_fragment_program *p )
847{
848 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000849 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000850 return p->half;
851}
852
853static struct ureg get_zero( struct texenv_fragment_program *p )
854{
855 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000856 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000857 return p->zero;
858}
859
Keith Whitwell15e75e02005-04-29 15:11:39 +0000860
Keith Whitwell15e75e02005-04-29 15:11:39 +0000861static void program_error( struct texenv_fragment_program *p, const char *msg )
862{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000863 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000864 p->error = 1;
865}
866
Keith Whitwell15e75e02005-04-29 15:11:39 +0000867static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000868 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000869{
870 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000871 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000872 assert(!is_undef(p->src_texture[unit]));
873 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000874
Keith Whitwellce721142005-07-11 10:10:38 +0000875 case SRC_TEXTURE0:
876 case SRC_TEXTURE1:
877 case SRC_TEXTURE2:
878 case SRC_TEXTURE3:
879 case SRC_TEXTURE4:
880 case SRC_TEXTURE5:
881 case SRC_TEXTURE6:
882 case SRC_TEXTURE7:
883 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
884 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000885
Keith Whitwellce721142005-07-11 10:10:38 +0000886 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000887 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000888
Keith Whitwellce721142005-07-11 10:10:38 +0000889 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000890 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000891
Brian Paul6947f852009-01-23 17:32:09 -0700892 case SRC_ZERO:
893 return get_zero(p);
894
Keith Whitwellce721142005-07-11 10:10:38 +0000895 case SRC_PREVIOUS:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000896 if (is_undef(p->src_previous))
897 return register_input(p, FRAG_ATTRIB_COL0);
898 else
899 return p->src_previous;
Brian Paul6947f852009-01-23 17:32:09 -0700900
901 default:
902 assert(0);
José Fonsecac6af9b22009-06-15 19:20:25 +0100903 return undef;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000904 }
905}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000906
Keith Whitwell15e75e02005-04-29 15:11:39 +0000907static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000908 GLuint mask,
909 GLuint unit,
910 GLuint source,
911 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000912{
913 struct ureg arg, src, one;
914
915 src = get_source(p, source, unit);
916
917 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000918 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000919 /* Get unused tmp,
920 * Emit tmp = 1.0 - arg.xyzw
921 */
922 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000923 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000924 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000925
Keith Whitwellce721142005-07-11 10:10:38 +0000926 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000927 if (mask == WRITEMASK_W)
928 return src;
929 else
Brian Paul22db5352005-11-19 23:45:10 +0000930 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000931 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000932 /* Get unused tmp,
933 * Emit tmp = 1.0 - arg.wwww
934 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000935 arg = get_temp(p);
936 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000937 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000938 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000939 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000940 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000941 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000942 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000943 case OPR_SRC_COLOR:
Brian Paul6947f852009-01-23 17:32:09 -0700944 return src;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000945 default:
Brian Paul6947f852009-01-23 17:32:09 -0700946 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000947 return src;
948 }
949}
950
Keith Whitwellce721142005-07-11 10:10:38 +0000951static GLboolean args_match( struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000952{
Brian Paul22db5352005-11-19 23:45:10 +0000953 GLuint i, nr = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000954
955 for (i = 0 ; i < nr ; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000956 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000957 return GL_FALSE;
958
Keith Whitwellce721142005-07-11 10:10:38 +0000959 switch(key->unit[unit].OptA[i].Operand) {
960 case OPR_SRC_ALPHA:
961 switch(key->unit[unit].OptRGB[i].Operand) {
962 case OPR_SRC_COLOR:
963 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000964 break;
965 default:
966 return GL_FALSE;
967 }
968 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000969 case OPR_ONE_MINUS_SRC_ALPHA:
970 switch(key->unit[unit].OptRGB[i].Operand) {
971 case OPR_ONE_MINUS_SRC_COLOR:
972 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000973 break;
974 default:
975 return GL_FALSE;
976 }
977 break;
978 default:
979 return GL_FALSE; /* impossible */
980 }
981 }
982
983 return GL_TRUE;
984}
985
Keith Whitwell15e75e02005-04-29 15:11:39 +0000986static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000987 struct ureg dest,
988 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000989 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000990 GLuint unit,
991 GLuint nr,
992 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +0000993 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000994{
Brian Paulbd9b2be2009-03-08 17:58:54 -0600995 struct ureg src[MAX_COMBINER_TERMS];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000996 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +0000997 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000998
Brian Paulbd9b2be2009-03-08 17:58:54 -0600999 assert(nr <= MAX_COMBINER_TERMS);
Brian Paul6947f852009-01-23 17:32:09 -07001000
Brian Paul00630842005-12-12 15:27:55 +00001001 tmp = undef; /* silence warning (bug 5318) */
1002
Keith Whitwell15e75e02005-04-29 15:11:39 +00001003 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +00001004 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001005
1006 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +00001007 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001008 if (mask == WRITEMASK_XYZW && !saturate)
1009 return src[0];
1010 else
Brian Paul7e807512005-11-05 17:10:45 +00001011 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001012 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +00001013 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001014 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001015 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00001016 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001017 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001018 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001019 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001020 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +00001021 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001022 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +00001023 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +00001024 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
1025 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001026 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +00001027 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001028 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
1029 */
Brian Paul7e807512005-11-05 17:10:45 +00001030 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001031
Keith Whitwellce721142005-07-11 10:10:38 +00001032 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +00001033 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001034
Keith Whitwellce721142005-07-11 10:10:38 +00001035 case MODE_DOT3_RGBA:
1036 case MODE_DOT3_RGBA_EXT:
1037 case MODE_DOT3_RGB_EXT:
1038 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001039 struct ureg tmp0 = get_temp( p );
1040 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +00001041 struct ureg neg1 = register_scalar_const(p, -1);
1042 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001043
1044 /* tmp0 = 2*src0 - 1
1045 * tmp1 = 2*src1 - 1
1046 *
1047 * dst = tmp0 dot3 tmp1
1048 */
Brian Paul7e807512005-11-05 17:10:45 +00001049 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001050 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001051
Brian Paulaa206952005-09-16 18:14:24 +00001052 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001053 tmp1 = tmp0;
1054 else
Brian Paul7e807512005-11-05 17:10:45 +00001055 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001056 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +00001057 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001058 return dest;
1059 }
Keith Whitwellce721142005-07-11 10:10:38 +00001060 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001061 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001062 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001063 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +00001064 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001065 /* Arg0 * Arg2 + Arg1 - 0.5 */
1066 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001067 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +00001068 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
1069 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001070 return dest;
1071 }
Keith Whitwellce721142005-07-11 10:10:38 +00001072 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001073 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001074 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001075 return dest;
Brian Paul6947f852009-01-23 17:32:09 -07001076 case MODE_ADD_PRODUCTS:
1077 /* Arg0 * Arg1 + Arg2 * Arg3 */
1078 {
1079 struct ureg tmp0 = get_temp(p);
1080 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1081 emit_arith( p, OPCODE_MAD, dest, mask, saturate, src[2], src[3], tmp0 );
1082 }
1083 return dest;
1084 case MODE_ADD_PRODUCTS_SIGNED:
1085 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1086 {
1087 struct ureg tmp0 = get_temp(p);
1088 half = get_half(p);
1089 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1090 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[2], src[3], tmp0 );
1091 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1092 }
1093 return dest;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001094 case MODE_BUMP_ENVMAP_ATI:
1095 /* special - not handled here */
1096 assert(0);
1097 return src[0];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001098 default:
Brian Paul6947f852009-01-23 17:32:09 -07001099 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001100 return src[0];
1101 }
1102}
1103
Keith Whitwell15e75e02005-04-29 15:11:39 +00001104
Brian Paul22db5352005-11-19 23:45:10 +00001105/**
1106 * Generate instructions for one texture unit's env/combiner mode.
1107 */
1108static struct ureg
1109emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001110{
Keith Whitwellce721142005-07-11 10:10:38 +00001111 struct state_key *key = p->state;
Brian Paul22db5352005-11-19 23:45:10 +00001112 GLboolean saturate = (unit < p->last_tex_stage);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001113 GLuint rgb_shift, alpha_shift;
1114 struct ureg out, shift;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001115 struct ureg dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001116
Keith Whitwellce721142005-07-11 10:10:38 +00001117 if (!key->unit[unit].enabled) {
1118 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001119 }
Roland Scheidegger114152e2009-03-12 15:01:16 +01001120 if (key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1121 /* this isn't really a env stage delivering a color and handled elsewhere */
1122 return get_source(p, SRC_PREVIOUS, 0);
1123 }
Keith Whitwellce721142005-07-11 10:10:38 +00001124
1125 switch (key->unit[unit].ModeRGB) {
1126 case MODE_DOT3_RGB_EXT:
1127 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001128 rgb_shift = 0;
1129 break;
Keith Whitwellce721142005-07-11 10:10:38 +00001130 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001131 alpha_shift = 0;
1132 rgb_shift = 0;
1133 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001134 default:
Keith Whitwellce721142005-07-11 10:10:38 +00001135 rgb_shift = key->unit[unit].ScaleShiftRGB;
1136 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001137 break;
1138 }
Keith Whitwellce721142005-07-11 10:10:38 +00001139
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001140 /* If this is the very last calculation, emit direct to output reg:
1141 */
Keith Whitwellce721142005-07-11 10:10:38 +00001142 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001143 unit != p->last_tex_stage ||
1144 alpha_shift ||
1145 rgb_shift)
1146 dest = get_temp( p );
1147 else
Brian Paul8d475822009-02-28 11:49:46 -07001148 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLOR);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001149
1150 /* Emit the RGB and A combine ops
1151 */
Keith Whitwellce721142005-07-11 10:10:38 +00001152 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
1153 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001154 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1155 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001156 key->unit[unit].NumArgsRGB,
1157 key->unit[unit].ModeRGB,
1158 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001159 }
Keith Whitwellce721142005-07-11 10:10:38 +00001160 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +02001161 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001162
1163 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1164 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001165 key->unit[unit].NumArgsRGB,
1166 key->unit[unit].ModeRGB,
1167 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001168 }
1169 else {
1170 /* Need to do something to stop from re-emitting identical
1171 * argument calculations here:
1172 */
1173 out = emit_combine( p, dest, WRITEMASK_XYZ, 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 out = emit_combine( p, dest, WRITEMASK_W, saturate,
1179 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001180 key->unit[unit].NumArgsA,
1181 key->unit[unit].ModeA,
1182 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001183 }
1184
1185 /* Deal with the final shift:
1186 */
1187 if (alpha_shift || rgb_shift) {
1188 if (rgb_shift == alpha_shift) {
José Fonseca452a5922008-05-31 18:14:09 +09001189 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001190 }
1191 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001192 shift = register_const4f(p,
José Fonseca452a5922008-05-31 18:14:09 +09001193 (GLfloat)(1<<rgb_shift),
1194 (GLfloat)(1<<rgb_shift),
1195 (GLfloat)(1<<rgb_shift),
1196 (GLfloat)(1<<alpha_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001197 }
Brian Paul7e807512005-11-05 17:10:45 +00001198 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001199 saturate, out, shift, undef );
1200 }
1201 else
1202 return out;
1203}
1204
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001205
Brian Paul22db5352005-11-19 23:45:10 +00001206/**
1207 * Generate instruction for getting a texture source term.
1208 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001209static void load_texture( struct texenv_fragment_program *p, GLuint unit )
1210{
1211 if (is_undef(p->src_texture[unit])) {
Brian Paul44e018c2009-02-20 13:42:08 -07001212 GLuint texTarget = p->state->unit[unit].source_index;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001213 struct ureg texcoord;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001214 struct ureg tmp = get_tex_temp( p );
1215
Roland Scheidegger114152e2009-03-12 15:01:16 +01001216 if (is_undef(p->texcoord_tex[unit])) {
1217 texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
1218 }
1219 else {
1220 /* might want to reuse this reg for tex output actually */
1221 texcoord = p->texcoord_tex[unit];
1222 }
1223
Brian Paul44e018c2009-02-20 13:42:08 -07001224 if (texTarget == TEXTURE_UNKNOWN_INDEX)
Brian Paulbfb5ea32005-07-19 18:20:04 +00001225 program_error(p, "TexSrcBit");
Keith Whitwellce721142005-07-11 10:10:38 +00001226
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001227 /* TODO: Use D0_MASK_XY where possible.
1228 */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +02001229 if (p->state->unit[unit].enabled) {
Brian Paul44e018c2009-02-20 13:42:08 -07001230 GLboolean shadow = GL_FALSE;
1231
1232 if (p->state->unit[unit].shadow) {
1233 p->program->Base.ShadowSamplers |= 1 << unit;
1234 shadow = GL_TRUE;
1235 }
1236
Aapo Tahkolab8915342006-03-28 10:26:34 +00001237 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1238 tmp, WRITEMASK_XYZW,
Brian Paul44e018c2009-02-20 13:42:08 -07001239 unit, texTarget, shadow,
1240 texcoord );
Brian9b7e5a52007-12-14 11:16:49 -07001241
1242 p->program->Base.SamplersUsed |= (1 << unit);
Brian1fe385f2007-12-14 11:42:28 -07001243 /* This identity mapping should already be in place
1244 * (see _mesa_init_program_struct()) but let's be safe.
1245 */
1246 p->program->Base.SamplerUnits[unit] = unit;
Brian9b7e5a52007-12-14 11:16:49 -07001247 }
1248 else
Aapo Tahkolab8915342006-03-28 10:26:34 +00001249 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001250 }
1251}
1252
Keith Whitwell241b6b72005-05-23 09:50:34 +00001253static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +00001254 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001255{
1256 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +00001257 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001258 load_texture(p, unit);
1259 break;
1260
Keith Whitwellce721142005-07-11 10:10:38 +00001261 case SRC_TEXTURE0:
1262 case SRC_TEXTURE1:
1263 case SRC_TEXTURE2:
1264 case SRC_TEXTURE3:
1265 case SRC_TEXTURE4:
1266 case SRC_TEXTURE5:
1267 case SRC_TEXTURE6:
1268 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +00001269 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001270 break;
1271
1272 default:
Brian Paul6947f852009-01-23 17:32:09 -07001273 /* not a texture src - do nothing */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001274 break;
1275 }
Keith Whitwell241b6b72005-05-23 09:50:34 +00001276
1277 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001278}
1279
Brian Paul22db5352005-11-19 23:45:10 +00001280
1281/**
1282 * Generate instructions for loading all texture source terms.
1283 */
1284static GLboolean
1285load_texunit_sources( struct texenv_fragment_program *p, int unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001286{
Keith Whitwellce721142005-07-11 10:10:38 +00001287 struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +00001288 GLuint i;
1289
1290 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001291 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001292 }
Aapo Tahkolab8915342006-03-28 10:26:34 +00001293
1294 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1295 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1296 }
1297
Keith Whitwell241b6b72005-05-23 09:50:34 +00001298 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001299}
1300
Roland Scheidegger114152e2009-03-12 15:01:16 +01001301/**
1302 * Generate instructions for loading bump map textures.
1303 */
1304static GLboolean
1305load_texunit_bumpmap( struct texenv_fragment_program *p, int unit )
1306{
1307 struct state_key *key = p->state;
1308 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1309 struct ureg texcDst, bumpMapRes;
1310 struct ureg constdudvcolor = register_const4f(p, 0.0, 0.0, 0.0, 1.0);
1311 struct ureg texcSrc = register_input(p, FRAG_ATTRIB_TEX0 + bumpedUnitNr);
1312 struct ureg rotMat0 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_0, unit );
1313 struct ureg rotMat1 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_1, unit );
1314
1315 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1316
1317 bumpMapRes = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1318 texcDst = get_tex_temp( p );
1319 p->texcoord_tex[bumpedUnitNr] = texcDst;
1320
1321 /* apply rot matrix and add coords to be available in next phase */
1322 /* dest = (Arg0.xxxx * rotMat0 + Arg1) + (Arg0.yyyy * rotMat1) */
1323 /* note only 2 coords are affected the rest are left unchanged (mul by 0) */
1324 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1325 swizzle1(bumpMapRes, SWIZZLE_X), rotMat0, texcSrc );
1326 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1327 swizzle1(bumpMapRes, SWIZZLE_Y), rotMat1, texcDst );
1328
1329 /* move 0,0,0,1 into bumpmap src if someone (crossbar) is foolish
1330 enough to access this later, should optimize away */
1331 emit_arith( p, OPCODE_MOV, bumpMapRes, WRITEMASK_XYZW, 0, constdudvcolor, undef, undef );
1332
1333 return GL_TRUE;
1334}
Brian Paul22db5352005-11-19 23:45:10 +00001335
1336/**
1337 * Generate a new fragment program which implements the context's
1338 * current texture env/combine mode.
1339 */
1340static void
Brian Paule998c342006-10-29 21:17:18 +00001341create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001342 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001343{
Brian Paule998c342006-10-29 21:17:18 +00001344 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001345 struct texenv_fragment_program p;
1346 GLuint unit;
1347 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001348
Keith Whitwelle4902422005-05-11 15:16:35 +00001349 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +00001350 p.ctx = ctx;
Keith Whitwellce721142005-07-11 10:10:38 +00001351 p.state = key;
1352 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001353
Brian Paule998c342006-10-29 21:17:18 +00001354 /* During code generation, use locally-allocated instruction buffer,
1355 * then alloc dynamic storage below.
1356 */
1357 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001358 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001359 p.program->Base.NumTexIndirections = 1;
Brian21f99792007-01-09 11:00:21 -07001360 p.program->Base.NumTexInstructions = 0;
1361 p.program->Base.NumAluInstructions = 0;
Brian1240eb22007-03-22 08:50:20 -06001362 p.program->Base.String = NULL;
Keith Whitwell9ca88152005-05-10 09:56:02 +00001363 p.program->Base.NumInstructions =
Brian Paule998c342006-10-29 21:17:18 +00001364 p.program->Base.NumTemporaries =
1365 p.program->Base.NumParameters =
1366 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001367 p.program->Base.Parameters = _mesa_new_parameter_list();
Keith Whitwelldbeea252005-05-10 13:57:50 +00001368
Brian Paulde997602005-11-12 17:53:14 +00001369 p.program->Base.InputsRead = 0;
Brian Paul8d475822009-02-28 11:49:46 -07001370 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLOR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001371
Roland Scheidegger114152e2009-03-12 15:01:16 +01001372 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001373 p.src_texture[unit] = undef;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001374 p.texcoord_tex[unit] = undef;
1375 }
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001376
Keith Whitwell47b29f52005-05-04 11:44:44 +00001377 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001378 p.half = undef;
1379 p.zero = undef;
1380 p.one = undef;
1381
Keith Whitwell15e75e02005-04-29 15:11:39 +00001382 p.last_tex_stage = 0;
Brian93fef222007-10-29 12:25:46 -06001383 release_temps(ctx, &p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001384
Keith Whitwellce721142005-07-11 10:10:38 +00001385 if (key->enabled_units) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001386 GLboolean needbumpstage = GL_FALSE;
1387 /* Zeroth pass - bump map textures first */
1388 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
1389 if (key->unit[unit].enabled && key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1390 needbumpstage = GL_TRUE;
1391 load_texunit_bumpmap( &p, unit );
1392 }
1393 if (needbumpstage)
1394 p.program->Base.NumTexIndirections++;
1395
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001396 /* First pass - to support texture_env_crossbar, first identify
1397 * all referenced texture sources and emit texld instructions
1398 * for each:
1399 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001400 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001401 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001402 load_texunit_sources( &p, unit );
1403 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001404 }
1405
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001406 /* Second pass - emit combine instructions to build final color:
1407 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001408 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001409 if (key->enabled_units & (1<<unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001410 p.src_previous = emit_texenv( &p, unit );
Brian Paul5620c202008-09-26 11:18:06 -06001411 reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
Brian93fef222007-10-29 12:25:46 -06001412 release_temps(ctx, &p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001413 }
1414 }
1415
Keith Whitwellce721142005-07-11 10:10:38 +00001416 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul8d475822009-02-28 11:49:46 -07001417 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLOR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001418
Keith Whitwellce721142005-07-11 10:10:38 +00001419 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001420 /* Emit specular add.
1421 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001422 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001423 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1424 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001425 }
Brian Paulaa206952005-09-16 18:14:24 +00001426 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001427 /* Will wind up in here if no texture enabled or a couple of
1428 * other scenarios (GL_REPLACE for instance).
1429 */
Brian Paul7e807512005-11-05 17:10:45 +00001430 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001431 }
1432
Keith Whitwell47b29f52005-05-04 11:44:44 +00001433 /* Finish up:
1434 */
Brian Paul7e807512005-11-05 17:10:45 +00001435 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001436
Keith Whitwellce721142005-07-11 10:10:38 +00001437 if (key->fog_enabled) {
1438 /* Pull fog mode from GLcontext, the value in the state key is
1439 * a reduced value and not what is expected in FogOption
1440 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001441 p.program->FogOption = ctx->Fog.Mode;
Brian19d77d62007-09-18 19:29:26 -06001442 p.program->Base.InputsRead |= FRAG_BIT_FOGC; /* XXX new */
Keith Whitwellce721142005-07-11 10:10:38 +00001443 } else
Keith Whitwell276330b2005-05-09 17:58:13 +00001444 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001445
Brian21f99792007-01-09 11:00:21 -07001446 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001447 program_error(&p, "Exceeded max nr indirect texture lookups");
1448
Brian21f99792007-01-09 11:00:21 -07001449 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001450 program_error(&p, "Exceeded max TEX instructions");
1451
Brian21f99792007-01-09 11:00:21 -07001452 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001453 program_error(&p, "Exceeded max ALU instructions");
1454
Brian Paul5d7b49f2005-11-19 23:12:20 +00001455 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001456
Brian Paule998c342006-10-29 21:17:18 +00001457 /* Allocate final instruction array */
Brianc81cce72007-09-25 15:18:51 -06001458 p.program->Base.Instructions
1459 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1460 if (!p.program->Base.Instructions) {
Brian Paule998c342006-10-29 21:17:18 +00001461 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1462 "generating tex env program");
1463 return;
1464 }
Brianc81cce72007-09-25 15:18:51 -06001465 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1466 p.program->Base.NumInstructions);
1467
1468 if (p.program->FogOption) {
1469 _mesa_append_fog_code(ctx, p.program);
1470 p.program->FogOption = GL_NONE;
1471 }
1472
Brian Paule998c342006-10-29 21:17:18 +00001473
Keith Whitwelldbeea252005-05-10 13:57:50 +00001474 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001475 */
Brian Paule998c342006-10-29 21:17:18 +00001476 if (ctx->Driver.ProgramStringNotify) {
1477 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1478 &p.program->Base );
1479 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001480
Brian Paule998c342006-10-29 21:17:18 +00001481 if (DISASSEM) {
1482 _mesa_print_program(&p.program->Base);
1483 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001484 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001485}
1486
Brian Paul5d7b49f2005-11-19 23:12:20 +00001487
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001488/**
1489 * Return a fragment program which implements the current
1490 * fixed-function texture, fog and color-sum operations.
1491 */
1492struct gl_fragment_program *
1493_mesa_get_fixed_func_fragment_program(GLcontext *ctx)
Keith Whitwellce721142005-07-11 10:10:38 +00001494{
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001495 struct gl_fragment_program *prog;
1496 struct state_key key;
1497
1498 make_state_key(ctx, &key);
1499
1500 prog = (struct gl_fragment_program *)
1501 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1502 &key, sizeof(key));
Keith Whitwellce721142005-07-11 10:10:38 +00001503
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001504 if (!prog) {
1505 prog = (struct gl_fragment_program *)
1506 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1507
1508 create_new_program(ctx, &key, prog);
1509
1510 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1511 &key, sizeof(key), &prog->Base);
Keith Whitwellce721142005-07-11 10:10:38 +00001512 }
1513
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001514 return prog;
Keith Whitwellce721142005-07-11 10:10:38 +00001515}