blob: 847db5cfb64a160653162550122c950dd9c4f4a7 [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 */
Brian Paul6807d962009-08-07 09:42:28 -0600325 if (varying_inputs & VERT_BIT_COLOR0)
326 fp_inputs |= FRAG_BIT_COL0;
327 if (varying_inputs & VERT_BIT_COLOR1)
328 fp_inputs |= FRAG_BIT_COL1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100329
330 fp_inputs |= (((varying_inputs & VERT_BIT_TEX_ANY) >> VERT_ATTRIB_TEX0)
331 << FRAG_ATTRIB_TEX0);
332
333 }
334 else {
335 /* calculate from vp->outputs */
Brian Paul2389c052008-12-17 19:01:34 -0700336 struct gl_vertex_program *vprog;
337 GLbitfield vp_outputs;
338
339 /* Choose GLSL vertex shader over ARB vertex program. Need this
340 * since vertex shader state validation comes after fragment state
341 * validation (see additional comments in state.c).
342 */
343 if (vertexShader)
344 vprog = ctx->Shader.CurrentProgram->VertexProgram;
345 else
Eric Anholta9ba1bf2009-08-03 12:38:56 -0700346 vprog = ctx->VertexProgram.Current;
Brian Paul2389c052008-12-17 19:01:34 -0700347
348 vp_outputs = vprog->Base.OutputsWritten;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100349
Keith Whitwell97e63432008-10-20 13:03:45 +0100350 /* These get generated in the setup routine regardless of the
351 * vertex program:
352 */
Eric Anholte5f63c42009-06-02 07:47:20 -0700353 /* _NEW_POINT */
Keith Whitwell97e63432008-10-20 13:03:45 +0100354 if (ctx->Point.PointSprite)
355 vp_outputs |= FRAG_BITS_TEX_ANY;
356
Brian Paul6807d962009-08-07 09:42:28 -0600357 if (vp_outputs & (1 << VERT_RESULT_COL0))
358 fp_inputs |= FRAG_BIT_COL0;
359 if (vp_outputs & (1 << VERT_RESULT_COL1))
360 fp_inputs |= FRAG_BIT_COL1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100361
Keith Whitwell0370d6b2008-10-04 12:41:56 +0100362 fp_inputs |= (((vp_outputs & VERT_RESULT_TEX_ANY) >> VERT_RESULT_TEX0)
363 << FRAG_ATTRIB_TEX0);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100364 }
365
366 return fp_inputs;
367}
368
369
Brian Paul22db5352005-11-19 23:45:10 +0000370/**
371 * Examine current texture environment state and generate a unique
372 * key to identify it.
373 */
Keith Whitwell8065c122006-05-22 16:09:27 +0000374static void make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000375{
Keith Whitwellce721142005-07-11 10:10:38 +0000376 GLuint i, j;
Brian Paul239617f2008-10-07 11:22:47 -0600377 GLbitfield inputs_referenced = FRAG_BIT_COL0;
378 GLbitfield inputs_available = get_fp_input_mask( ctx );
Keith Whitwell1680ef82008-10-03 17:30:59 +0100379
Keith Whitwell8065c122006-05-22 16:09:27 +0000380 memset(key, 0, sizeof(*key));
381
Eric Anholte5f63c42009-06-02 07:47:20 -0700382 /* _NEW_TEXTURE */
Brian Paule9b34882008-12-31 11:54:02 -0700383 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000384 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800385 GLenum format;
Roland Scheideggerbf4a0fa2008-02-15 17:26:06 +0100386
387 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
Keith Whitwellce721142005-07-11 10:10:38 +0000388 continue;
389
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800390 format = texUnit->_Current->Image[0][texUnit->_Current->BaseLevel]->_BaseFormat;
391
Keith Whitwellce721142005-07-11 10:10:38 +0000392 key->unit[i].enabled = 1;
393 key->enabled_units |= (1<<i);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100394 key->nr_enabled_units = i+1;
395 inputs_referenced |= FRAG_BIT_TEX(i);
Keith Whitwellce721142005-07-11 10:10:38 +0000396
397 key->unit[i].source_index =
398 translate_tex_src_bit(texUnit->_ReallyEnabled);
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800399 key->unit[i].shadow = ((texUnit->_Current->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
400 ((format == GL_DEPTH_COMPONENT) ||
401 (format == GL_DEPTH_STENCIL_EXT)));
Keith Whitwellce721142005-07-11 10:10:38 +0000402
403 key->unit[i].NumArgsRGB = texUnit->_CurrentCombine->_NumArgsRGB;
404 key->unit[i].NumArgsA = texUnit->_CurrentCombine->_NumArgsA;
405
406 key->unit[i].ModeRGB =
Brian Paul6947f852009-01-23 17:32:09 -0700407 translate_mode(texUnit->EnvMode, texUnit->_CurrentCombine->ModeRGB);
Keith Whitwellce721142005-07-11 10:10:38 +0000408 key->unit[i].ModeA =
Brian Paul6947f852009-01-23 17:32:09 -0700409 translate_mode(texUnit->EnvMode, texUnit->_CurrentCombine->ModeA);
Roland Scheidegger114152e2009-03-12 15:01:16 +0100410
Keith Whitwellce721142005-07-11 10:10:38 +0000411 key->unit[i].ScaleShiftRGB = texUnit->_CurrentCombine->ScaleShiftRGB;
Keith Whitwell64da1612006-05-22 14:30:58 +0000412 key->unit[i].ScaleShiftA = texUnit->_CurrentCombine->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000413
Brian Paulbd9b2be2009-03-08 17:58:54 -0600414 for (j = 0; j < MAX_COMBINER_TERMS; j++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000415 key->unit[i].OptRGB[j].Operand =
416 translate_operand(texUnit->_CurrentCombine->OperandRGB[j]);
417 key->unit[i].OptA[j].Operand =
418 translate_operand(texUnit->_CurrentCombine->OperandA[j]);
419 key->unit[i].OptRGB[j].Source =
420 translate_source(texUnit->_CurrentCombine->SourceRGB[j]);
421 key->unit[i].OptA[j].Source =
422 translate_source(texUnit->_CurrentCombine->SourceA[j]);
423 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100424
425 if (key->unit[i].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
426 /* requires some special translation */
427 key->unit[i].NumArgsRGB = 2;
428 key->unit[i].ScaleShiftRGB = 0;
429 key->unit[i].OptRGB[0].Operand = OPR_SRC_COLOR;
430 key->unit[i].OptRGB[0].Source = SRC_TEXTURE;
431 key->unit[i].OptRGB[1].Operand = OPR_SRC_COLOR;
432 key->unit[i].OptRGB[1].Source = texUnit->BumpTarget - GL_TEXTURE0 + SRC_TEXTURE0;
433 }
Keith Whitwellce721142005-07-11 10:10:38 +0000434 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100435
Eric Anholt9cea84b2009-07-16 18:41:03 -0700436 /* _NEW_LIGHT | _NEW_FOG */
437 if (texenv_doing_secondary_color(ctx)) {
Keith Whitwellce721142005-07-11 10:10:38 +0000438 key->separate_specular = 1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100439 inputs_referenced |= FRAG_BIT_COL1;
440 }
Keith Whitwellce721142005-07-11 10:10:38 +0000441
Eric Anholte5f63c42009-06-02 07:47:20 -0700442 /* _NEW_FOG */
Keith Whitwellce721142005-07-11 10:10:38 +0000443 if (ctx->Fog.Enabled) {
444 key->fog_enabled = 1;
445 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100446 inputs_referenced |= FRAG_BIT_FOGC; /* maybe */
Keith Whitwellce721142005-07-11 10:10:38 +0000447 }
Keith Whitwell1680ef82008-10-03 17:30:59 +0100448
449 key->inputs_available = (inputs_available & inputs_referenced);
Keith Whitwellce721142005-07-11 10:10:38 +0000450}
451
Brian Paul239617f2008-10-07 11:22:47 -0600452/**
453 * Use uregs to represent registers internally, translate to Mesa's
Keith Whitwell15e75e02005-04-29 15:11:39 +0000454 * expected formats on emit.
455 *
456 * NOTE: These are passed by value extensively in this file rather
457 * than as usual by pointer reference. If this disturbs you, try
458 * remembering they are just 32bits in size.
459 *
460 * GCC is smart enough to deal with these dword-sized structures in
461 * much the same way as if I had defined them as dwords and was using
462 * macros to access and set the fields. This is much nicer and easier
463 * to evolve.
464 */
465struct ureg {
466 GLuint file:4;
467 GLuint idx:8;
468 GLuint negatebase:1;
469 GLuint abs:1;
470 GLuint negateabs:1;
471 GLuint swz:12;
472 GLuint pad:5;
473};
474
Brian Paul13abf912006-04-13 19:17:13 +0000475static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000476 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000477 ~0,
478 0,
479 0,
480 0,
481 0,
482 0
483};
484
Keith Whitwell15e75e02005-04-29 15:11:39 +0000485
Brian Paul239617f2008-10-07 11:22:47 -0600486/** State used to build the fragment program:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000487 */
488struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000489 struct gl_fragment_program *program;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000490 GLcontext *ctx;
Keith Whitwellce721142005-07-11 10:10:38 +0000491 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000492
Brian Paul239617f2008-10-07 11:22:47 -0600493 GLbitfield alu_temps; /**< Track texture indirections, see spec. */
494 GLbitfield temps_output; /**< Track texture indirections, see spec. */
495 GLbitfield temp_in_use; /**< Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000496 GLboolean error;
497
Brian Paule9b34882008-12-31 11:54:02 -0700498 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000499 /* Reg containing each texture unit's sampled texture color,
500 * else undef.
501 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000502
Roland Scheidegger114152e2009-03-12 15:01:16 +0100503 struct ureg texcoord_tex[MAX_TEXTURE_COORD_UNITS];
504 /* Reg containing texcoord for a texture unit,
505 * needed for bump mapping, else undef.
506 */
507
Brian Paul239617f2008-10-07 11:22:47 -0600508 struct ureg src_previous; /**< Reg containing color from previous
Keith Whitwell15e75e02005-04-29 15:11:39 +0000509 * stage. May need to be decl'd.
510 */
511
Brian Paul239617f2008-10-07 11:22:47 -0600512 GLuint last_tex_stage; /**< Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000513
514 struct ureg half;
515 struct ureg one;
516 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000517};
518
519
520
521static struct ureg make_ureg(GLuint file, GLuint idx)
522{
523 struct ureg reg;
524 reg.file = file;
525 reg.idx = idx;
526 reg.negatebase = 0;
527 reg.abs = 0;
528 reg.negateabs = 0;
529 reg.swz = SWIZZLE_NOOP;
530 reg.pad = 0;
531 return reg;
532}
533
534static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
535{
536 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
537 GET_SWZ(reg.swz, y),
538 GET_SWZ(reg.swz, z),
539 GET_SWZ(reg.swz, w));
540
541 return reg;
542}
543
544static struct ureg swizzle1( struct ureg reg, int x )
545{
546 return swizzle(reg, x, x, x, x);
547}
548
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000549static struct ureg negate( struct ureg reg )
550{
551 reg.negatebase ^= 1;
552 return reg;
553}
554
Keith Whitwell15e75e02005-04-29 15:11:39 +0000555static GLboolean is_undef( struct ureg reg )
556{
Alan Hourihane8d972652006-08-10 13:12:00 +0000557 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000558}
559
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000560
Keith Whitwell15e75e02005-04-29 15:11:39 +0000561static struct ureg get_temp( struct texenv_fragment_program *p )
562{
Brian Paul13abf912006-04-13 19:17:13 +0000563 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000564
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000565 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000566 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000567 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000568
569 /* Then any unused temporary:
570 */
571 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000572 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000573
Keith Whitwell15e75e02005-04-29 15:11:39 +0000574 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000575 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000576 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000577 }
578
Brian Paul13abf912006-04-13 19:17:13 +0000579 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000580 p->program->Base.NumTemporaries = bit;
581
Keith Whitwell93cd9232005-05-11 08:30:23 +0000582 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000583 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
584}
585
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000586static struct ureg get_tex_temp( struct texenv_fragment_program *p )
587{
588 int bit;
589
Brian Pauld01269a2008-09-25 18:27:22 -0600590 /* First try to find available temp not previously used (to avoid
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000591 * starting a new texture indirection). According to the spec, the
592 * ~p->temps_output isn't necessary, but will keep it there for
593 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000594 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000595 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000596
597 /* Then any unused temporary:
598 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000599 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000600 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000601
602 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000603 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000604 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000605 }
606
Brian Paul13abf912006-04-13 19:17:13 +0000607 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000608 p->program->Base.NumTemporaries = bit;
609
Keith Whitwell93cd9232005-05-11 08:30:23 +0000610 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000611 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
612}
613
Keith Whitwell15e75e02005-04-29 15:11:39 +0000614
Brian Paul5620c202008-09-26 11:18:06 -0600615/** Mark a temp reg as being no longer allocatable. */
616static void reserve_temp( struct texenv_fragment_program *p, struct ureg r )
617{
618 if (r.file == PROGRAM_TEMPORARY)
619 p->temps_output |= (1 << r.idx);
620}
621
622
Brian93fef222007-10-29 12:25:46 -0600623static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000624{
Brian93fef222007-10-29 12:25:46 -0600625 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000626
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000627 /* KW: To support tex_env_crossbar, don't release the registers in
628 * temps_output.
629 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000630 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000631 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000632 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000633 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000634}
635
636
Brian9fe3e2e2007-02-23 11:44:14 -0700637static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000638 GLint s0,
639 GLint s1,
640 GLint s2,
641 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700642 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000643{
Brian9fe3e2e2007-02-23 11:44:14 -0700644 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000645 GLuint idx;
646 tokens[0] = s0;
647 tokens[1] = s1;
648 tokens[2] = s2;
649 tokens[3] = s3;
650 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000651 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000652 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000653}
654
Keith Whitwell47b29f52005-05-04 11:44:44 +0000655
Brian9fe3e2e2007-02-23 11:44:14 -0700656#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
657#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
658#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
659#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000660
Keith Whitwell1680ef82008-10-03 17:30:59 +0100661static GLuint frag_to_vert_attrib( GLuint attrib )
662{
663 switch (attrib) {
664 case FRAG_ATTRIB_COL0: return VERT_ATTRIB_COLOR0;
665 case FRAG_ATTRIB_COL1: return VERT_ATTRIB_COLOR1;
666 default:
667 assert(attrib >= FRAG_ATTRIB_TEX0);
668 assert(attrib <= FRAG_ATTRIB_TEX7);
669 return attrib - FRAG_ATTRIB_TEX0 + VERT_ATTRIB_TEX0;
670 }
671}
672
Keith Whitwell47b29f52005-05-04 11:44:44 +0000673
674static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
675{
Keith Whitwell1680ef82008-10-03 17:30:59 +0100676 if (p->state->inputs_available & (1<<input)) {
677 p->program->Base.InputsRead |= (1 << input);
678 return make_ureg(PROGRAM_INPUT, input);
679 }
680 else {
681 GLuint idx = frag_to_vert_attrib( input );
682 return register_param3( p, STATE_INTERNAL, STATE_CURRENT_ATTRIB, idx );
683 }
Keith Whitwell47b29f52005-05-04 11:44:44 +0000684}
685
686
Brian Paul7e807512005-11-05 17:10:45 +0000687static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000688 struct ureg ureg )
689{
690 reg->File = ureg.file;
691 reg->Index = ureg.idx;
692 reg->Swizzle = ureg.swz;
Brian Paul7db7ff82009-04-14 22:14:30 -0600693 reg->Negate = ureg.negatebase ? NEGATE_XYZW : NEGATE_NONE;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000694 reg->Abs = ureg.abs;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000695}
696
Brian Paul7e807512005-11-05 17:10:45 +0000697static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000698 struct ureg ureg, GLuint mask )
699{
700 dst->File = ureg.file;
701 dst->Index = ureg.idx;
702 dst->WriteMask = mask;
Brianc9d495c2007-10-23 10:55:24 -0600703 dst->CondMask = COND_TR; /* always pass cond test */
704 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000705}
706
Brian Paul7e807512005-11-05 17:10:45 +0000707static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000708emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000709 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000710 struct ureg dest,
711 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000712 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000713 struct ureg src0,
714 struct ureg src1,
715 struct ureg src2 )
716{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000717 GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000718 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Brian2a8e9bb2007-10-23 10:24:53 -0600719
720 assert(nr < MAX_INSTRUCTIONS);
721
Brian Pauld6272e02006-10-29 18:03:16 +0000722 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000723 inst->Opcode = op;
724
Keith Whitwell47b29f52005-05-04 11:44:44 +0000725 emit_arg( &inst->SrcReg[0], src0 );
726 emit_arg( &inst->SrcReg[1], src1 );
727 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000728
Brian Paule31ac052005-11-20 17:52:40 +0000729 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000730
731 emit_dst( &inst->DstReg, dest, mask );
732
Brian Paul5620c202008-09-26 11:18:06 -0600733#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000734 /* Accounting for indirection tracking:
735 */
736 if (dest.file == PROGRAM_TEMPORARY)
737 p->temps_output |= 1 << dest.idx;
Brian Paul5620c202008-09-26 11:18:06 -0600738#endif
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000739
Keith Whitwell15e75e02005-04-29 15:11:39 +0000740 return inst;
741}
742
743
744static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000745 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000746 struct ureg dest,
747 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000748 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000749 struct ureg src0,
750 struct ureg src1,
751 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000752{
753 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
754
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000755 /* Accounting for indirection tracking:
756 */
757 if (src0.file == PROGRAM_TEMPORARY)
758 p->alu_temps |= 1 << src0.idx;
759
760 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
761 p->alu_temps |= 1 << src1.idx;
762
763 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
764 p->alu_temps |= 1 << src2.idx;
765
766 if (dest.file == PROGRAM_TEMPORARY)
767 p->alu_temps |= 1 << dest.idx;
768
Brian21f99792007-01-09 11:00:21 -0700769 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000770 return dest;
771}
772
773static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000774 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000775 struct ureg dest,
776 GLuint destmask,
777 GLuint tex_unit,
778 GLuint tex_idx,
Brian Paul44e018c2009-02-20 13:42:08 -0700779 GLuint tex_shadow,
Keith Whitwellce721142005-07-11 10:10:38 +0000780 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000781{
Brian Paul7e807512005-11-05 17:10:45 +0000782 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000783 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000784 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000785 coord, /* arg 0? */
786 undef,
787 undef);
788
Brian Paul7e807512005-11-05 17:10:45 +0000789 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000790 inst->TexSrcUnit = tex_unit;
Brian Paul44e018c2009-02-20 13:42:08 -0700791 inst->TexShadow = tex_shadow;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000792
Brian21f99792007-01-09 11:00:21 -0700793 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000794
Brian Paul5620c202008-09-26 11:18:06 -0600795 /* Accounting for indirection tracking:
796 */
797 reserve_temp(p, dest);
798
Roland Scheidegger114152e2009-03-12 15:01:16 +0100799#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000800 /* Is this a texture indirection?
801 */
802 if ((coord.file == PROGRAM_TEMPORARY &&
803 (p->temps_output & (1<<coord.idx))) ||
804 (dest.file == PROGRAM_TEMPORARY &&
805 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700806 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000807 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000808 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000809 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000810 }
Roland Scheidegger114152e2009-03-12 15:01:16 +0100811#endif
Keith Whitwell15e75e02005-04-29 15:11:39 +0000812
813 return dest;
814}
815
816
Keith Whitwell47b29f52005-05-04 11:44:44 +0000817static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000818 GLfloat s0,
819 GLfloat s1,
820 GLfloat s2,
821 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000822{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000823 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700824 GLuint idx, swizzle;
Brian Pauld01269a2008-09-25 18:27:22 -0600825 struct ureg r;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000826 values[0] = s0;
827 values[1] = s1;
828 values[2] = s2;
829 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700830 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
831 &swizzle );
Brian Pauld01269a2008-09-25 18:27:22 -0600832 r = make_ureg(PROGRAM_CONSTANT, idx);
833 r.swz = swizzle;
834 return r;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000835}
836
Keith Whitwelle4902422005-05-11 15:16:35 +0000837#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000838#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
839#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
840#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000841
842
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000843static struct ureg get_one( struct texenv_fragment_program *p )
844{
845 if (is_undef(p->one))
846 p->one = register_scalar_const(p, 1.0);
847 return p->one;
848}
849
850static struct ureg get_half( struct texenv_fragment_program *p )
851{
852 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000853 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000854 return p->half;
855}
856
857static struct ureg get_zero( struct texenv_fragment_program *p )
858{
859 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000860 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000861 return p->zero;
862}
863
Keith Whitwell15e75e02005-04-29 15:11:39 +0000864
Keith Whitwell15e75e02005-04-29 15:11:39 +0000865static void program_error( struct texenv_fragment_program *p, const char *msg )
866{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000867 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000868 p->error = 1;
869}
870
Keith Whitwell15e75e02005-04-29 15:11:39 +0000871static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000872 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000873{
874 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000875 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000876 assert(!is_undef(p->src_texture[unit]));
877 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000878
Keith Whitwellce721142005-07-11 10:10:38 +0000879 case SRC_TEXTURE0:
880 case SRC_TEXTURE1:
881 case SRC_TEXTURE2:
882 case SRC_TEXTURE3:
883 case SRC_TEXTURE4:
884 case SRC_TEXTURE5:
885 case SRC_TEXTURE6:
886 case SRC_TEXTURE7:
887 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
888 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000889
Keith Whitwellce721142005-07-11 10:10:38 +0000890 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000891 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000892
Keith Whitwellce721142005-07-11 10:10:38 +0000893 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000894 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000895
Brian Paul6947f852009-01-23 17:32:09 -0700896 case SRC_ZERO:
897 return get_zero(p);
898
Keith Whitwellce721142005-07-11 10:10:38 +0000899 case SRC_PREVIOUS:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000900 if (is_undef(p->src_previous))
901 return register_input(p, FRAG_ATTRIB_COL0);
902 else
903 return p->src_previous;
Brian Paul6947f852009-01-23 17:32:09 -0700904
905 default:
906 assert(0);
José Fonsecac6af9b22009-06-15 19:20:25 +0100907 return undef;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000908 }
909}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000910
Keith Whitwell15e75e02005-04-29 15:11:39 +0000911static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000912 GLuint mask,
913 GLuint unit,
914 GLuint source,
915 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000916{
917 struct ureg arg, src, one;
918
919 src = get_source(p, source, unit);
920
921 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000922 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000923 /* Get unused tmp,
924 * Emit tmp = 1.0 - arg.xyzw
925 */
926 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000927 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000928 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000929
Keith Whitwellce721142005-07-11 10:10:38 +0000930 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000931 if (mask == WRITEMASK_W)
932 return src;
933 else
Brian Paul22db5352005-11-19 23:45:10 +0000934 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000935 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000936 /* Get unused tmp,
937 * Emit tmp = 1.0 - arg.wwww
938 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000939 arg = get_temp(p);
940 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000941 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000942 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000943 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000944 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000945 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000946 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000947 case OPR_SRC_COLOR:
Brian Paul6947f852009-01-23 17:32:09 -0700948 return src;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000949 default:
Brian Paul6947f852009-01-23 17:32:09 -0700950 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000951 return src;
952 }
953}
954
Keith Whitwellce721142005-07-11 10:10:38 +0000955static GLboolean args_match( struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000956{
Brian Paul22db5352005-11-19 23:45:10 +0000957 GLuint i, nr = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000958
959 for (i = 0 ; i < nr ; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000960 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000961 return GL_FALSE;
962
Keith Whitwellce721142005-07-11 10:10:38 +0000963 switch(key->unit[unit].OptA[i].Operand) {
964 case OPR_SRC_ALPHA:
965 switch(key->unit[unit].OptRGB[i].Operand) {
966 case OPR_SRC_COLOR:
967 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000968 break;
969 default:
970 return GL_FALSE;
971 }
972 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000973 case OPR_ONE_MINUS_SRC_ALPHA:
974 switch(key->unit[unit].OptRGB[i].Operand) {
975 case OPR_ONE_MINUS_SRC_COLOR:
976 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000977 break;
978 default:
979 return GL_FALSE;
980 }
981 break;
982 default:
983 return GL_FALSE; /* impossible */
984 }
985 }
986
987 return GL_TRUE;
988}
989
Keith Whitwell15e75e02005-04-29 15:11:39 +0000990static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000991 struct ureg dest,
992 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000993 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000994 GLuint unit,
995 GLuint nr,
996 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +0000997 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000998{
Brian Paulbd9b2be2009-03-08 17:58:54 -0600999 struct ureg src[MAX_COMBINER_TERMS];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001000 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +00001001 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001002
Brian Paulbd9b2be2009-03-08 17:58:54 -06001003 assert(nr <= MAX_COMBINER_TERMS);
Brian Paul6947f852009-01-23 17:32:09 -07001004
Brian Paul00630842005-12-12 15:27:55 +00001005 tmp = undef; /* silence warning (bug 5318) */
1006
Keith Whitwell15e75e02005-04-29 15:11:39 +00001007 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +00001008 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001009
1010 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +00001011 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001012 if (mask == WRITEMASK_XYZW && !saturate)
1013 return src[0];
1014 else
Brian Paul7e807512005-11-05 17:10:45 +00001015 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001016 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +00001017 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001018 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001019 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00001020 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001021 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +00001022 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001023 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001024 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +00001025 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001026 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +00001027 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +00001028 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
1029 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001030 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +00001031 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001032 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
1033 */
Brian Paul7e807512005-11-05 17:10:45 +00001034 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001035
Keith Whitwellce721142005-07-11 10:10:38 +00001036 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +00001037 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001038
Keith Whitwellce721142005-07-11 10:10:38 +00001039 case MODE_DOT3_RGBA:
1040 case MODE_DOT3_RGBA_EXT:
1041 case MODE_DOT3_RGB_EXT:
1042 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001043 struct ureg tmp0 = get_temp( p );
1044 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +00001045 struct ureg neg1 = register_scalar_const(p, -1);
1046 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001047
1048 /* tmp0 = 2*src0 - 1
1049 * tmp1 = 2*src1 - 1
1050 *
1051 * dst = tmp0 dot3 tmp1
1052 */
Brian Paul7e807512005-11-05 17:10:45 +00001053 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001054 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001055
Brian Paulaa206952005-09-16 18:14:24 +00001056 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001057 tmp1 = tmp0;
1058 else
Brian Paul7e807512005-11-05 17:10:45 +00001059 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001060 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +00001061 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001062 return dest;
1063 }
Keith Whitwellce721142005-07-11 10:10:38 +00001064 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001065 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001066 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001067 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +00001068 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001069 /* Arg0 * Arg2 + Arg1 - 0.5 */
1070 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001071 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +00001072 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
1073 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001074 return dest;
1075 }
Keith Whitwellce721142005-07-11 10:10:38 +00001076 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001077 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001078 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001079 return dest;
Brian Paul6947f852009-01-23 17:32:09 -07001080 case MODE_ADD_PRODUCTS:
1081 /* Arg0 * Arg1 + Arg2 * Arg3 */
1082 {
1083 struct ureg tmp0 = get_temp(p);
1084 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1085 emit_arith( p, OPCODE_MAD, dest, mask, saturate, src[2], src[3], tmp0 );
1086 }
1087 return dest;
1088 case MODE_ADD_PRODUCTS_SIGNED:
1089 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1090 {
1091 struct ureg tmp0 = get_temp(p);
1092 half = get_half(p);
1093 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1094 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[2], src[3], tmp0 );
1095 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1096 }
1097 return dest;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001098 case MODE_BUMP_ENVMAP_ATI:
1099 /* special - not handled here */
1100 assert(0);
1101 return src[0];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001102 default:
Brian Paul6947f852009-01-23 17:32:09 -07001103 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001104 return src[0];
1105 }
1106}
1107
Keith Whitwell15e75e02005-04-29 15:11:39 +00001108
Brian Paul22db5352005-11-19 23:45:10 +00001109/**
1110 * Generate instructions for one texture unit's env/combiner mode.
1111 */
1112static struct ureg
1113emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001114{
Keith Whitwellce721142005-07-11 10:10:38 +00001115 struct state_key *key = p->state;
Brian Paul956e6c32009-08-31 11:14:16 -06001116 GLboolean saturate;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001117 GLuint rgb_shift, alpha_shift;
1118 struct ureg out, shift;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001119 struct ureg dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001120
Keith Whitwellce721142005-07-11 10:10:38 +00001121 if (!key->unit[unit].enabled) {
1122 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001123 }
Roland Scheidegger114152e2009-03-12 15:01:16 +01001124 if (key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1125 /* this isn't really a env stage delivering a color and handled elsewhere */
1126 return get_source(p, SRC_PREVIOUS, 0);
1127 }
Keith Whitwellce721142005-07-11 10:10:38 +00001128
1129 switch (key->unit[unit].ModeRGB) {
1130 case MODE_DOT3_RGB_EXT:
1131 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001132 rgb_shift = 0;
1133 break;
Keith Whitwellce721142005-07-11 10:10:38 +00001134 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001135 alpha_shift = 0;
1136 rgb_shift = 0;
1137 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001138 default:
Keith Whitwellce721142005-07-11 10:10:38 +00001139 rgb_shift = key->unit[unit].ScaleShiftRGB;
1140 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001141 break;
1142 }
Keith Whitwellce721142005-07-11 10:10:38 +00001143
Brian Paul956e6c32009-08-31 11:14:16 -06001144 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
1145 * We don't want to clamp twice.
1146 */
1147 saturate = !(rgb_shift || alpha_shift);
1148
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001149 /* If this is the very last calculation, emit direct to output reg:
1150 */
Keith Whitwellce721142005-07-11 10:10:38 +00001151 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001152 unit != p->last_tex_stage ||
1153 alpha_shift ||
1154 rgb_shift)
1155 dest = get_temp( p );
1156 else
Brian Paul8d475822009-02-28 11:49:46 -07001157 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLOR);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001158
1159 /* Emit the RGB and A combine ops
1160 */
Keith Whitwellce721142005-07-11 10:10:38 +00001161 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
1162 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001163 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 }
Keith Whitwellce721142005-07-11 10:10:38 +00001169 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +02001170 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001171
1172 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 }
1178 else {
1179 /* Need to do something to stop from re-emitting identical
1180 * argument calculations here:
1181 */
1182 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
1183 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001184 key->unit[unit].NumArgsRGB,
1185 key->unit[unit].ModeRGB,
1186 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001187 out = emit_combine( p, dest, WRITEMASK_W, saturate,
1188 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001189 key->unit[unit].NumArgsA,
1190 key->unit[unit].ModeA,
1191 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001192 }
1193
1194 /* Deal with the final shift:
1195 */
1196 if (alpha_shift || rgb_shift) {
Brian Paul956e6c32009-08-31 11:14:16 -06001197 saturate = GL_TRUE; /* always saturate at this point */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001198 if (rgb_shift == alpha_shift) {
José Fonseca452a5922008-05-31 18:14:09 +09001199 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001200 }
1201 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001202 shift = register_const4f(p,
José Fonseca452a5922008-05-31 18:14:09 +09001203 (GLfloat)(1<<rgb_shift),
1204 (GLfloat)(1<<rgb_shift),
1205 (GLfloat)(1<<rgb_shift),
1206 (GLfloat)(1<<alpha_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001207 }
Brian Paul7e807512005-11-05 17:10:45 +00001208 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001209 saturate, out, shift, undef );
1210 }
1211 else
1212 return out;
1213}
1214
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001215
Brian Paul22db5352005-11-19 23:45:10 +00001216/**
1217 * Generate instruction for getting a texture source term.
1218 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001219static void load_texture( struct texenv_fragment_program *p, GLuint unit )
1220{
1221 if (is_undef(p->src_texture[unit])) {
Brian Paul44e018c2009-02-20 13:42:08 -07001222 GLuint texTarget = p->state->unit[unit].source_index;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001223 struct ureg texcoord;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001224 struct ureg tmp = get_tex_temp( p );
1225
Roland Scheidegger114152e2009-03-12 15:01:16 +01001226 if (is_undef(p->texcoord_tex[unit])) {
1227 texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
1228 }
1229 else {
1230 /* might want to reuse this reg for tex output actually */
1231 texcoord = p->texcoord_tex[unit];
1232 }
1233
Brian Paul44e018c2009-02-20 13:42:08 -07001234 if (texTarget == TEXTURE_UNKNOWN_INDEX)
Brian Paulbfb5ea32005-07-19 18:20:04 +00001235 program_error(p, "TexSrcBit");
Keith Whitwellce721142005-07-11 10:10:38 +00001236
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001237 /* TODO: Use D0_MASK_XY where possible.
1238 */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +02001239 if (p->state->unit[unit].enabled) {
Brian Paul44e018c2009-02-20 13:42:08 -07001240 GLboolean shadow = GL_FALSE;
1241
1242 if (p->state->unit[unit].shadow) {
1243 p->program->Base.ShadowSamplers |= 1 << unit;
1244 shadow = GL_TRUE;
1245 }
1246
Aapo Tahkolab8915342006-03-28 10:26:34 +00001247 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1248 tmp, WRITEMASK_XYZW,
Brian Paul44e018c2009-02-20 13:42:08 -07001249 unit, texTarget, shadow,
1250 texcoord );
Brian9b7e5a52007-12-14 11:16:49 -07001251
1252 p->program->Base.SamplersUsed |= (1 << unit);
Brian1fe385f2007-12-14 11:42:28 -07001253 /* This identity mapping should already be in place
1254 * (see _mesa_init_program_struct()) but let's be safe.
1255 */
1256 p->program->Base.SamplerUnits[unit] = unit;
Brian9b7e5a52007-12-14 11:16:49 -07001257 }
1258 else
Aapo Tahkolab8915342006-03-28 10:26:34 +00001259 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001260 }
1261}
1262
Keith Whitwell241b6b72005-05-23 09:50:34 +00001263static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +00001264 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001265{
1266 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +00001267 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001268 load_texture(p, unit);
1269 break;
1270
Keith Whitwellce721142005-07-11 10:10:38 +00001271 case SRC_TEXTURE0:
1272 case SRC_TEXTURE1:
1273 case SRC_TEXTURE2:
1274 case SRC_TEXTURE3:
1275 case SRC_TEXTURE4:
1276 case SRC_TEXTURE5:
1277 case SRC_TEXTURE6:
1278 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +00001279 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001280 break;
1281
1282 default:
Brian Paul6947f852009-01-23 17:32:09 -07001283 /* not a texture src - do nothing */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001284 break;
1285 }
Keith Whitwell241b6b72005-05-23 09:50:34 +00001286
1287 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001288}
1289
Brian Paul22db5352005-11-19 23:45:10 +00001290
1291/**
1292 * Generate instructions for loading all texture source terms.
1293 */
1294static GLboolean
1295load_texunit_sources( struct texenv_fragment_program *p, int unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001296{
Keith Whitwellce721142005-07-11 10:10:38 +00001297 struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +00001298 GLuint i;
1299
1300 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001301 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001302 }
Aapo Tahkolab8915342006-03-28 10:26:34 +00001303
1304 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1305 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1306 }
1307
Keith Whitwell241b6b72005-05-23 09:50:34 +00001308 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001309}
1310
Roland Scheidegger114152e2009-03-12 15:01:16 +01001311/**
1312 * Generate instructions for loading bump map textures.
1313 */
1314static GLboolean
1315load_texunit_bumpmap( struct texenv_fragment_program *p, int unit )
1316{
1317 struct state_key *key = p->state;
1318 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1319 struct ureg texcDst, bumpMapRes;
1320 struct ureg constdudvcolor = register_const4f(p, 0.0, 0.0, 0.0, 1.0);
1321 struct ureg texcSrc = register_input(p, FRAG_ATTRIB_TEX0 + bumpedUnitNr);
1322 struct ureg rotMat0 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_0, unit );
1323 struct ureg rotMat1 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_1, unit );
1324
1325 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1326
1327 bumpMapRes = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1328 texcDst = get_tex_temp( p );
1329 p->texcoord_tex[bumpedUnitNr] = texcDst;
1330
1331 /* apply rot matrix and add coords to be available in next phase */
1332 /* dest = (Arg0.xxxx * rotMat0 + Arg1) + (Arg0.yyyy * rotMat1) */
1333 /* note only 2 coords are affected the rest are left unchanged (mul by 0) */
1334 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1335 swizzle1(bumpMapRes, SWIZZLE_X), rotMat0, texcSrc );
1336 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1337 swizzle1(bumpMapRes, SWIZZLE_Y), rotMat1, texcDst );
1338
1339 /* move 0,0,0,1 into bumpmap src if someone (crossbar) is foolish
1340 enough to access this later, should optimize away */
1341 emit_arith( p, OPCODE_MOV, bumpMapRes, WRITEMASK_XYZW, 0, constdudvcolor, undef, undef );
1342
1343 return GL_TRUE;
1344}
Brian Paul22db5352005-11-19 23:45:10 +00001345
1346/**
1347 * Generate a new fragment program which implements the context's
1348 * current texture env/combine mode.
1349 */
1350static void
Brian Paule998c342006-10-29 21:17:18 +00001351create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001352 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001353{
Brian Paule998c342006-10-29 21:17:18 +00001354 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001355 struct texenv_fragment_program p;
1356 GLuint unit;
1357 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001358
Keith Whitwelle4902422005-05-11 15:16:35 +00001359 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +00001360 p.ctx = ctx;
Keith Whitwellce721142005-07-11 10:10:38 +00001361 p.state = key;
1362 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001363
Brian Paule998c342006-10-29 21:17:18 +00001364 /* During code generation, use locally-allocated instruction buffer,
1365 * then alloc dynamic storage below.
1366 */
1367 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001368 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001369 p.program->Base.NumTexIndirections = 1;
Brian21f99792007-01-09 11:00:21 -07001370 p.program->Base.NumTexInstructions = 0;
1371 p.program->Base.NumAluInstructions = 0;
Brian1240eb22007-03-22 08:50:20 -06001372 p.program->Base.String = NULL;
Keith Whitwell9ca88152005-05-10 09:56:02 +00001373 p.program->Base.NumInstructions =
Brian Paule998c342006-10-29 21:17:18 +00001374 p.program->Base.NumTemporaries =
1375 p.program->Base.NumParameters =
1376 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001377 p.program->Base.Parameters = _mesa_new_parameter_list();
Keith Whitwelldbeea252005-05-10 13:57:50 +00001378
Brian Paulde997602005-11-12 17:53:14 +00001379 p.program->Base.InputsRead = 0;
Brian Paul8d475822009-02-28 11:49:46 -07001380 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLOR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001381
Roland Scheidegger114152e2009-03-12 15:01:16 +01001382 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001383 p.src_texture[unit] = undef;
Roland Scheidegger114152e2009-03-12 15:01:16 +01001384 p.texcoord_tex[unit] = undef;
1385 }
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001386
Keith Whitwell47b29f52005-05-04 11:44:44 +00001387 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001388 p.half = undef;
1389 p.zero = undef;
1390 p.one = undef;
1391
Keith Whitwell15e75e02005-04-29 15:11:39 +00001392 p.last_tex_stage = 0;
Brian93fef222007-10-29 12:25:46 -06001393 release_temps(ctx, &p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001394
Keith Whitwellce721142005-07-11 10:10:38 +00001395 if (key->enabled_units) {
Roland Scheidegger114152e2009-03-12 15:01:16 +01001396 GLboolean needbumpstage = GL_FALSE;
1397 /* Zeroth pass - bump map textures first */
1398 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
1399 if (key->unit[unit].enabled && key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1400 needbumpstage = GL_TRUE;
1401 load_texunit_bumpmap( &p, unit );
1402 }
1403 if (needbumpstage)
1404 p.program->Base.NumTexIndirections++;
1405
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001406 /* First pass - to support texture_env_crossbar, first identify
1407 * all referenced texture sources and emit texld instructions
1408 * for each:
1409 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001410 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001411 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001412 load_texunit_sources( &p, unit );
1413 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001414 }
1415
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001416 /* Second pass - emit combine instructions to build final color:
1417 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001418 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001419 if (key->enabled_units & (1<<unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001420 p.src_previous = emit_texenv( &p, unit );
Brian Paul5620c202008-09-26 11:18:06 -06001421 reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
Brian93fef222007-10-29 12:25:46 -06001422 release_temps(ctx, &p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001423 }
1424 }
1425
Keith Whitwellce721142005-07-11 10:10:38 +00001426 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul8d475822009-02-28 11:49:46 -07001427 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLOR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001428
Keith Whitwellce721142005-07-11 10:10:38 +00001429 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001430 /* Emit specular add.
1431 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001432 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001433 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1434 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001435 }
Brian Paulaa206952005-09-16 18:14:24 +00001436 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001437 /* Will wind up in here if no texture enabled or a couple of
1438 * other scenarios (GL_REPLACE for instance).
1439 */
Brian Paul7e807512005-11-05 17:10:45 +00001440 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001441 }
1442
Keith Whitwell47b29f52005-05-04 11:44:44 +00001443 /* Finish up:
1444 */
Brian Paul7e807512005-11-05 17:10:45 +00001445 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001446
Keith Whitwellce721142005-07-11 10:10:38 +00001447 if (key->fog_enabled) {
1448 /* Pull fog mode from GLcontext, the value in the state key is
1449 * a reduced value and not what is expected in FogOption
1450 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001451 p.program->FogOption = ctx->Fog.Mode;
Brian19d77d62007-09-18 19:29:26 -06001452 p.program->Base.InputsRead |= FRAG_BIT_FOGC; /* XXX new */
Keith Whitwellce721142005-07-11 10:10:38 +00001453 } else
Keith Whitwell276330b2005-05-09 17:58:13 +00001454 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001455
Brian21f99792007-01-09 11:00:21 -07001456 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001457 program_error(&p, "Exceeded max nr indirect texture lookups");
1458
Brian21f99792007-01-09 11:00:21 -07001459 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001460 program_error(&p, "Exceeded max TEX instructions");
1461
Brian21f99792007-01-09 11:00:21 -07001462 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001463 program_error(&p, "Exceeded max ALU instructions");
1464
Brian Paul5d7b49f2005-11-19 23:12:20 +00001465 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001466
Brian Paule998c342006-10-29 21:17:18 +00001467 /* Allocate final instruction array */
Brianc81cce72007-09-25 15:18:51 -06001468 p.program->Base.Instructions
1469 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1470 if (!p.program->Base.Instructions) {
Brian Paule998c342006-10-29 21:17:18 +00001471 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1472 "generating tex env program");
1473 return;
1474 }
Brianc81cce72007-09-25 15:18:51 -06001475 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1476 p.program->Base.NumInstructions);
1477
1478 if (p.program->FogOption) {
1479 _mesa_append_fog_code(ctx, p.program);
1480 p.program->FogOption = GL_NONE;
1481 }
1482
Brian Paule998c342006-10-29 21:17:18 +00001483
Keith Whitwelldbeea252005-05-10 13:57:50 +00001484 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001485 */
Brian Paule998c342006-10-29 21:17:18 +00001486 if (ctx->Driver.ProgramStringNotify) {
1487 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1488 &p.program->Base );
1489 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001490
Brian Paule998c342006-10-29 21:17:18 +00001491 if (DISASSEM) {
1492 _mesa_print_program(&p.program->Base);
1493 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001494 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001495}
1496
Brian Paul5d7b49f2005-11-19 23:12:20 +00001497
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001498/**
1499 * Return a fragment program which implements the current
1500 * fixed-function texture, fog and color-sum operations.
1501 */
1502struct gl_fragment_program *
1503_mesa_get_fixed_func_fragment_program(GLcontext *ctx)
Keith Whitwellce721142005-07-11 10:10:38 +00001504{
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001505 struct gl_fragment_program *prog;
1506 struct state_key key;
1507
1508 make_state_key(ctx, &key);
1509
1510 prog = (struct gl_fragment_program *)
1511 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1512 &key, sizeof(key));
Keith Whitwellce721142005-07-11 10:10:38 +00001513
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001514 if (!prog) {
1515 prog = (struct gl_fragment_program *)
1516 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1517
1518 create_new_program(ctx, &key, prog);
1519
1520 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1521 &key, sizeof(key), &prog->Base);
Keith Whitwellce721142005-07-11 10:10:38 +00001522 }
1523
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001524 return prog;
Keith Whitwellce721142005-07-11 10:10:38 +00001525}