blob: 51c13a563d6c6ee686ac6223c42d5e61c664ba07 [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 Paul6947f852009-01-23 17:32:09 -070042#define MAX_TERMS 4
43
44
Brian Paule9b34882008-12-31 11:54:02 -070045/*
46 * Note on texture units:
47 *
48 * The number of texture units supported by fixed-function fragment
49 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
50 * That's because there's a one-to-one correspondence between texture
51 * coordinates and samplers in fixed-function processing.
52 *
53 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
54 * sets of texcoords, so is fixed-function fragment processing.
55 *
56 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
57 */
58
59
Brian Paul5b5c9312008-05-07 18:51:44 -060060struct texenvprog_cache_item
61{
62 GLuint hash;
63 void *key;
64 struct gl_fragment_program *data;
65 struct texenvprog_cache_item *next;
66};
67
68
Brian Paule998c342006-10-29 21:17:18 +000069/**
Brian Paulb5e1a932008-09-25 18:40:16 -060070 * Up to nine instructions per tex unit, plus fog, specular color.
Brian Paule998c342006-10-29 21:17:18 +000071 */
Brian Paul0815ebc2009-01-02 16:32:26 -070072#define MAX_INSTRUCTIONS ((MAX_TEXTURE_COORD_UNITS * 9) + 12)
Keith Whitwell15e75e02005-04-29 15:11:39 +000073
Keith Whitwell269e3892005-05-12 10:28:43 +000074#define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
Keith Whitwell15e75e02005-04-29 15:11:39 +000075
Keith Whitwellce721142005-07-11 10:10:38 +000076struct mode_opt {
Brian Paul5d7b49f2005-11-19 23:12:20 +000077 GLuint Source:4;
78 GLuint Operand:3;
Keith Whitwellce721142005-07-11 10:10:38 +000079};
80
81struct state_key {
Keith Whitwell6280e332008-10-03 13:51:56 +010082 GLuint nr_enabled_units:8;
83 GLuint enabled_units:8;
Brian Paul5d7b49f2005-11-19 23:12:20 +000084 GLuint separate_specular:1;
85 GLuint fog_enabled:1;
86 GLuint fog_mode:2;
Keith Whitwell6280e332008-10-03 13:51:56 +010087 GLuint inputs_available:12;
Keith Whitwellce721142005-07-11 10:10:38 +000088
89 struct {
Brian Paul5d7b49f2005-11-19 23:12:20 +000090 GLuint enabled:1;
91 GLuint source_index:3; /* one of TEXTURE_1D/2D/3D/CUBE/RECT_INDEX */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +020092 GLuint shadow:1;
Brian Paul5d7b49f2005-11-19 23:12:20 +000093 GLuint ScaleShiftRGB:2;
94 GLuint ScaleShiftA:2;
Keith Whitwellce721142005-07-11 10:10:38 +000095
Brian Paul6947f852009-01-23 17:32:09 -070096 GLuint NumArgsRGB:3;
Brian Paul5d7b49f2005-11-19 23:12:20 +000097 GLuint ModeRGB:4;
Brian Paul6947f852009-01-23 17:32:09 -070098 struct mode_opt OptRGB[MAX_TERMS];
Keith Whitwellce721142005-07-11 10:10:38 +000099
Brian Paul6947f852009-01-23 17:32:09 -0700100 GLuint NumArgsA:3;
Brian Paul5d7b49f2005-11-19 23:12:20 +0000101 GLuint ModeA:4;
Brian Paul6947f852009-01-23 17:32:09 -0700102 struct mode_opt OptA[MAX_TERMS];
Keith Whitwellce721142005-07-11 10:10:38 +0000103 } unit[8];
104};
105
106#define FOG_LINEAR 0
107#define FOG_EXP 1
108#define FOG_EXP2 2
109#define FOG_UNKNOWN 3
110
111static GLuint translate_fog_mode( GLenum mode )
112{
113 switch (mode) {
114 case GL_LINEAR: return FOG_LINEAR;
115 case GL_EXP: return FOG_EXP;
116 case GL_EXP2: return FOG_EXP2;
117 default: return FOG_UNKNOWN;
118 }
119}
120
121#define OPR_SRC_COLOR 0
122#define OPR_ONE_MINUS_SRC_COLOR 1
123#define OPR_SRC_ALPHA 2
124#define OPR_ONE_MINUS_SRC_ALPHA 3
125#define OPR_ZERO 4
126#define OPR_ONE 5
127#define OPR_UNKNOWN 7
128
129static GLuint translate_operand( GLenum operand )
130{
131 switch (operand) {
132 case GL_SRC_COLOR: return OPR_SRC_COLOR;
133 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
134 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
135 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
136 case GL_ZERO: return OPR_ZERO;
137 case GL_ONE: return OPR_ONE;
Brian Paul6947f852009-01-23 17:32:09 -0700138 default:
139 assert(0);
140 return OPR_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000141 }
142}
143
144#define SRC_TEXTURE 0
145#define SRC_TEXTURE0 1
146#define SRC_TEXTURE1 2
147#define SRC_TEXTURE2 3
148#define SRC_TEXTURE3 4
149#define SRC_TEXTURE4 5
150#define SRC_TEXTURE5 6
151#define SRC_TEXTURE6 7
152#define SRC_TEXTURE7 8
153#define SRC_CONSTANT 9
154#define SRC_PRIMARY_COLOR 10
155#define SRC_PREVIOUS 11
Brian Paul6947f852009-01-23 17:32:09 -0700156#define SRC_ZERO 12
Keith Whitwellce721142005-07-11 10:10:38 +0000157#define SRC_UNKNOWN 15
158
159static GLuint translate_source( GLenum src )
160{
161 switch (src) {
162 case GL_TEXTURE: return SRC_TEXTURE;
163 case GL_TEXTURE0:
164 case GL_TEXTURE1:
165 case GL_TEXTURE2:
166 case GL_TEXTURE3:
167 case GL_TEXTURE4:
168 case GL_TEXTURE5:
169 case GL_TEXTURE6:
170 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
171 case GL_CONSTANT: return SRC_CONSTANT;
172 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
173 case GL_PREVIOUS: return SRC_PREVIOUS;
Brian Paul6947f852009-01-23 17:32:09 -0700174 case GL_ZERO:
175 return SRC_ZERO;
176 default:
177 assert(0);
178 return SRC_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000179 }
180}
181
Brian Paul6947f852009-01-23 17:32:09 -0700182#define MODE_REPLACE 0 /* r = a0 */
183#define MODE_MODULATE 1 /* r = a0 * a1 */
184#define MODE_ADD 2 /* r = a0 + a1 */
185#define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
186#define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
187#define MODE_SUBTRACT 5 /* r = a0 - a1 */
188#define MODE_DOT3_RGB 6 /* r = a0 . a1 */
189#define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
190#define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
191#define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
192#define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
193#define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
194#define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
195#define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
196#define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
197#define MODE_UNKNOWN 15
Keith Whitwellce721142005-07-11 10:10:38 +0000198
Brian Paul6947f852009-01-23 17:32:09 -0700199/**
200 * Translate GL combiner state into a MODE_x value
201 */
202static GLuint translate_mode( GLenum envMode, GLenum mode )
Keith Whitwellce721142005-07-11 10:10:38 +0000203{
204 switch (mode) {
205 case GL_REPLACE: return MODE_REPLACE;
206 case GL_MODULATE: return MODE_MODULATE;
Brian Paul6947f852009-01-23 17:32:09 -0700207 case GL_ADD:
208 if (envMode == GL_COMBINE4_NV)
209 return MODE_ADD_PRODUCTS;
210 else
211 return MODE_ADD;
212 case GL_ADD_SIGNED:
213 if (envMode == GL_COMBINE4_NV)
214 return MODE_ADD_PRODUCTS_SIGNED;
215 else
216 return MODE_ADD_SIGNED;
Keith Whitwellce721142005-07-11 10:10:38 +0000217 case GL_INTERPOLATE: return MODE_INTERPOLATE;
218 case GL_SUBTRACT: return MODE_SUBTRACT;
219 case GL_DOT3_RGB: return MODE_DOT3_RGB;
220 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
221 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
222 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
223 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
224 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
225 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
Brian Paul6947f852009-01-23 17:32:09 -0700226 default:
227 assert(0);
228 return MODE_UNKNOWN;
Keith Whitwellce721142005-07-11 10:10:38 +0000229 }
230}
231
232#define TEXTURE_UNKNOWN_INDEX 7
Brian Paule00ac112005-09-15 05:00:45 +0000233static GLuint translate_tex_src_bit( GLbitfield bit )
Keith Whitwellce721142005-07-11 10:10:38 +0000234{
Brian Paul1519b932008-12-17 13:17:15 -0700235 /* make sure number of switch cases is correct */
236 assert(NUM_TEXTURE_TARGETS == 7);
Keith Whitwellce721142005-07-11 10:10:38 +0000237 switch (bit) {
238 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
239 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
240 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
241 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
242 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
Brian Paul6947f852009-01-23 17:32:09 -0700243 case TEXTURE_1D_ARRAY_BIT: return TEXTURE_1D_ARRAY_INDEX;
244 case TEXTURE_2D_ARRAY_BIT: return TEXTURE_2D_ARRAY_INDEX;
245 default:
246 assert(0);
247 return TEXTURE_UNKNOWN_INDEX;
Keith Whitwellce721142005-07-11 10:10:38 +0000248 }
249}
250
Keith Whitwell1680ef82008-10-03 17:30:59 +0100251#define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
252#define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
253
Brian Paul239617f2008-10-07 11:22:47 -0600254/**
255 * Identify all possible varying inputs. The fragment program will
Keith Whitwell1680ef82008-10-03 17:30:59 +0100256 * never reference non-varying inputs, but will track them via state
257 * constants instead.
258 *
259 * This function figures out all the inputs that the fragment program
260 * has access to. The bitmask is later reduced to just those which
261 * are actually referenced.
262 */
Brian Paul239617f2008-10-07 11:22:47 -0600263static GLbitfield get_fp_input_mask( GLcontext *ctx )
Keith Whitwell1680ef82008-10-03 17:30:59 +0100264{
Brian Paulf0b07942008-12-17 14:04:03 -0700265 const GLboolean vertexShader = (ctx->Shader.CurrentProgram &&
266 ctx->Shader.CurrentProgram->VertexProgram);
267 const GLboolean vertexProgram = ctx->VertexProgram._Enabled;
Brian Paul239617f2008-10-07 11:22:47 -0600268 GLbitfield fp_inputs = 0x0;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100269
Brian Paul6d4d51d2008-10-10 13:39:14 -0600270 if (ctx->VertexProgram._Overriden) {
271 /* Somebody's messing with the vertex program and we don't have
272 * a clue what's happening. Assume that it could be producing
273 * all possible outputs.
274 */
275 fp_inputs = ~0;
276 }
277 else if (ctx->RenderMode == GL_FEEDBACK) {
278 fp_inputs = (FRAG_BIT_COL0 | FRAG_BIT_TEX0);
279 }
Brian Paulf0b07942008-12-17 14:04:03 -0700280 else if (!(vertexProgram || vertexShader) ||
Brian Pauld7296a12008-12-17 11:29:06 -0700281 !ctx->VertexProgram._Current) {
Brian Paulf0b07942008-12-17 14:04:03 -0700282 /* Fixed function vertex logic */
Brian Paul239617f2008-10-07 11:22:47 -0600283 GLbitfield varying_inputs = ctx->varying_vp_inputs;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100284
Keith Whitwell97e63432008-10-20 13:03:45 +0100285 /* These get generated in the setup routine regardless of the
286 * vertex program:
287 */
288 if (ctx->Point.PointSprite)
289 varying_inputs |= FRAG_BITS_TEX_ANY;
290
Keith Whitwell1680ef82008-10-03 17:30:59 +0100291 /* First look at what values may be computed by the generated
292 * vertex program:
293 */
294 if (ctx->Light.Enabled) {
295 fp_inputs |= FRAG_BIT_COL0;
296
297 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
298 fp_inputs |= FRAG_BIT_COL1;
299 }
300
301 fp_inputs |= (ctx->Texture._TexGenEnabled |
302 ctx->Texture._TexMatEnabled) << FRAG_ATTRIB_TEX0;
303
304 /* Then look at what might be varying as a result of enabled
305 * arrays, etc:
306 */
307 if (varying_inputs & VERT_BIT_COLOR0) fp_inputs |= FRAG_BIT_COL0;
308 if (varying_inputs & VERT_BIT_COLOR1) fp_inputs |= FRAG_BIT_COL1;
309
310 fp_inputs |= (((varying_inputs & VERT_BIT_TEX_ANY) >> VERT_ATTRIB_TEX0)
311 << FRAG_ATTRIB_TEX0);
312
313 }
314 else {
315 /* calculate from vp->outputs */
Brian Paul2389c052008-12-17 19:01:34 -0700316 struct gl_vertex_program *vprog;
317 GLbitfield vp_outputs;
318
319 /* Choose GLSL vertex shader over ARB vertex program. Need this
320 * since vertex shader state validation comes after fragment state
321 * validation (see additional comments in state.c).
322 */
323 if (vertexShader)
324 vprog = ctx->Shader.CurrentProgram->VertexProgram;
325 else
326 vprog = ctx->VertexProgram._Current;
327
328 vp_outputs = vprog->Base.OutputsWritten;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100329
Keith Whitwell97e63432008-10-20 13:03:45 +0100330 /* These get generated in the setup routine regardless of the
331 * vertex program:
332 */
333 if (ctx->Point.PointSprite)
334 vp_outputs |= FRAG_BITS_TEX_ANY;
335
Keith Whitwell1680ef82008-10-03 17:30:59 +0100336 if (vp_outputs & (1 << VERT_RESULT_COL0)) fp_inputs |= FRAG_BIT_COL0;
337 if (vp_outputs & (1 << VERT_RESULT_COL1)) fp_inputs |= FRAG_BIT_COL1;
338
Keith Whitwell0370d6b2008-10-04 12:41:56 +0100339 fp_inputs |= (((vp_outputs & VERT_RESULT_TEX_ANY) >> VERT_RESULT_TEX0)
340 << FRAG_ATTRIB_TEX0);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100341 }
342
343 return fp_inputs;
344}
345
346
Brian Paul22db5352005-11-19 23:45:10 +0000347/**
348 * Examine current texture environment state and generate a unique
349 * key to identify it.
350 */
Keith Whitwell8065c122006-05-22 16:09:27 +0000351static void make_state_key( GLcontext *ctx, struct state_key *key )
Keith Whitwellce721142005-07-11 10:10:38 +0000352{
Keith Whitwellce721142005-07-11 10:10:38 +0000353 GLuint i, j;
Brian Paul239617f2008-10-07 11:22:47 -0600354 GLbitfield inputs_referenced = FRAG_BIT_COL0;
355 GLbitfield inputs_available = get_fp_input_mask( ctx );
Keith Whitwell1680ef82008-10-03 17:30:59 +0100356
Keith Whitwell8065c122006-05-22 16:09:27 +0000357 memset(key, 0, sizeof(*key));
358
Brian Paule9b34882008-12-31 11:54:02 -0700359 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
Brian Pauld9736db2006-05-23 02:44:46 +0000360 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800361 GLenum format;
Roland Scheideggerbf4a0fa2008-02-15 17:26:06 +0100362
363 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
Keith Whitwellce721142005-07-11 10:10:38 +0000364 continue;
365
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800366 format = texUnit->_Current->Image[0][texUnit->_Current->BaseLevel]->_BaseFormat;
367
Keith Whitwellce721142005-07-11 10:10:38 +0000368 key->unit[i].enabled = 1;
369 key->enabled_units |= (1<<i);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100370 key->nr_enabled_units = i+1;
371 inputs_referenced |= FRAG_BIT_TEX(i);
Keith Whitwellce721142005-07-11 10:10:38 +0000372
373 key->unit[i].source_index =
374 translate_tex_src_bit(texUnit->_ReallyEnabled);
Xiang, Haihaob6bb5e02008-11-20 16:54:16 +0800375 key->unit[i].shadow = ((texUnit->_Current->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
376 ((format == GL_DEPTH_COMPONENT) ||
377 (format == GL_DEPTH_STENCIL_EXT)));
Keith Whitwellce721142005-07-11 10:10:38 +0000378
379 key->unit[i].NumArgsRGB = texUnit->_CurrentCombine->_NumArgsRGB;
380 key->unit[i].NumArgsA = texUnit->_CurrentCombine->_NumArgsA;
381
382 key->unit[i].ModeRGB =
Brian Paul6947f852009-01-23 17:32:09 -0700383 translate_mode(texUnit->EnvMode, texUnit->_CurrentCombine->ModeRGB);
Keith Whitwellce721142005-07-11 10:10:38 +0000384 key->unit[i].ModeA =
Brian Paul6947f852009-01-23 17:32:09 -0700385 translate_mode(texUnit->EnvMode, texUnit->_CurrentCombine->ModeA);
Keith Whitwellce721142005-07-11 10:10:38 +0000386
387 key->unit[i].ScaleShiftRGB = texUnit->_CurrentCombine->ScaleShiftRGB;
Keith Whitwell64da1612006-05-22 14:30:58 +0000388 key->unit[i].ScaleShiftA = texUnit->_CurrentCombine->ScaleShiftA;
Keith Whitwellce721142005-07-11 10:10:38 +0000389
Brian Paul6947f852009-01-23 17:32:09 -0700390 for (j = 0; j < MAX_TERMS; j++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000391 key->unit[i].OptRGB[j].Operand =
392 translate_operand(texUnit->_CurrentCombine->OperandRGB[j]);
393 key->unit[i].OptA[j].Operand =
394 translate_operand(texUnit->_CurrentCombine->OperandA[j]);
395 key->unit[i].OptRGB[j].Source =
396 translate_source(texUnit->_CurrentCombine->SourceRGB[j]);
397 key->unit[i].OptA[j].Source =
398 translate_source(texUnit->_CurrentCombine->SourceA[j]);
399 }
400 }
401
Keith Whitwell1680ef82008-10-03 17:30:59 +0100402 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) {
Keith Whitwellce721142005-07-11 10:10:38 +0000403 key->separate_specular = 1;
Keith Whitwell1680ef82008-10-03 17:30:59 +0100404 inputs_referenced |= FRAG_BIT_COL1;
405 }
Keith Whitwellce721142005-07-11 10:10:38 +0000406
407 if (ctx->Fog.Enabled) {
408 key->fog_enabled = 1;
409 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
Keith Whitwell1680ef82008-10-03 17:30:59 +0100410 inputs_referenced |= FRAG_BIT_FOGC; /* maybe */
Keith Whitwellce721142005-07-11 10:10:38 +0000411 }
Keith Whitwell1680ef82008-10-03 17:30:59 +0100412
413 key->inputs_available = (inputs_available & inputs_referenced);
Keith Whitwellce721142005-07-11 10:10:38 +0000414}
415
Brian Paul239617f2008-10-07 11:22:47 -0600416/**
417 * Use uregs to represent registers internally, translate to Mesa's
Keith Whitwell15e75e02005-04-29 15:11:39 +0000418 * expected formats on emit.
419 *
420 * NOTE: These are passed by value extensively in this file rather
421 * than as usual by pointer reference. If this disturbs you, try
422 * remembering they are just 32bits in size.
423 *
424 * GCC is smart enough to deal with these dword-sized structures in
425 * much the same way as if I had defined them as dwords and was using
426 * macros to access and set the fields. This is much nicer and easier
427 * to evolve.
428 */
429struct ureg {
430 GLuint file:4;
431 GLuint idx:8;
432 GLuint negatebase:1;
433 GLuint abs:1;
434 GLuint negateabs:1;
435 GLuint swz:12;
436 GLuint pad:5;
437};
438
Brian Paul13abf912006-04-13 19:17:13 +0000439static const struct ureg undef = {
Alan Hourihane8d972652006-08-10 13:12:00 +0000440 PROGRAM_UNDEFINED,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000441 ~0,
442 0,
443 0,
444 0,
445 0,
446 0
447};
448
Keith Whitwell15e75e02005-04-29 15:11:39 +0000449
Brian Paul239617f2008-10-07 11:22:47 -0600450/** State used to build the fragment program:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000451 */
452struct texenv_fragment_program {
Brian Paul122629f2006-07-20 16:49:57 +0000453 struct gl_fragment_program *program;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000454 GLcontext *ctx;
Keith Whitwellce721142005-07-11 10:10:38 +0000455 struct state_key *state;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000456
Brian Paul239617f2008-10-07 11:22:47 -0600457 GLbitfield alu_temps; /**< Track texture indirections, see spec. */
458 GLbitfield temps_output; /**< Track texture indirections, see spec. */
459 GLbitfield temp_in_use; /**< Tracks temporary regs which are in use. */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000460 GLboolean error;
461
Brian Paule9b34882008-12-31 11:54:02 -0700462 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
Keith Whitwellce721142005-07-11 10:10:38 +0000463 /* Reg containing each texture unit's sampled texture color,
464 * else undef.
465 */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000466
Brian Paul239617f2008-10-07 11:22:47 -0600467 struct ureg src_previous; /**< Reg containing color from previous
Keith Whitwell15e75e02005-04-29 15:11:39 +0000468 * stage. May need to be decl'd.
469 */
470
Brian Paul239617f2008-10-07 11:22:47 -0600471 GLuint last_tex_stage; /**< Number of last enabled texture unit */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000472
473 struct ureg half;
474 struct ureg one;
475 struct ureg zero;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000476};
477
478
479
480static struct ureg make_ureg(GLuint file, GLuint idx)
481{
482 struct ureg reg;
483 reg.file = file;
484 reg.idx = idx;
485 reg.negatebase = 0;
486 reg.abs = 0;
487 reg.negateabs = 0;
488 reg.swz = SWIZZLE_NOOP;
489 reg.pad = 0;
490 return reg;
491}
492
493static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
494{
495 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
496 GET_SWZ(reg.swz, y),
497 GET_SWZ(reg.swz, z),
498 GET_SWZ(reg.swz, w));
499
500 return reg;
501}
502
503static struct ureg swizzle1( struct ureg reg, int x )
504{
505 return swizzle(reg, x, x, x, x);
506}
507
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000508static struct ureg negate( struct ureg reg )
509{
510 reg.negatebase ^= 1;
511 return reg;
512}
513
Keith Whitwell15e75e02005-04-29 15:11:39 +0000514static GLboolean is_undef( struct ureg reg )
515{
Alan Hourihane8d972652006-08-10 13:12:00 +0000516 return reg.file == PROGRAM_UNDEFINED;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000517}
518
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000519
Keith Whitwell15e75e02005-04-29 15:11:39 +0000520static struct ureg get_temp( struct texenv_fragment_program *p )
521{
Brian Paul13abf912006-04-13 19:17:13 +0000522 GLint bit;
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000523
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000524 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000525 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000526 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000527
528 /* Then any unused temporary:
529 */
530 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000531 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000532
Keith Whitwell15e75e02005-04-29 15:11:39 +0000533 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000534 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000535 _mesa_exit(1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000536 }
537
Brian Paul13abf912006-04-13 19:17:13 +0000538 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000539 p->program->Base.NumTemporaries = bit;
540
Keith Whitwell93cd9232005-05-11 08:30:23 +0000541 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000542 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
543}
544
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000545static struct ureg get_tex_temp( struct texenv_fragment_program *p )
546{
547 int bit;
548
Brian Pauld01269a2008-09-25 18:27:22 -0600549 /* First try to find available temp not previously used (to avoid
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000550 * starting a new texture indirection). According to the spec, the
551 * ~p->temps_output isn't necessary, but will keep it there for
552 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000553 */
Brian Paulb3aefd12005-09-19 20:12:32 +0000554 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000555
556 /* Then any unused temporary:
557 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000558 if (!bit)
Brian Paulb3aefd12005-09-19 20:12:32 +0000559 bit = _mesa_ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000560
561 if (!bit) {
Brian Paulbfb5ea32005-07-19 18:20:04 +0000562 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
Brian Paulb3aefd12005-09-19 20:12:32 +0000563 _mesa_exit(1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000564 }
565
Brian Paul13abf912006-04-13 19:17:13 +0000566 if ((GLuint) bit > p->program->Base.NumTemporaries)
Keith Whitwellb5cbaf92005-09-08 18:45:03 +0000567 p->program->Base.NumTemporaries = bit;
568
Keith Whitwell93cd9232005-05-11 08:30:23 +0000569 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000570 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
571}
572
Keith Whitwell15e75e02005-04-29 15:11:39 +0000573
Brian Paul5620c202008-09-26 11:18:06 -0600574/** Mark a temp reg as being no longer allocatable. */
575static void reserve_temp( struct texenv_fragment_program *p, struct ureg r )
576{
577 if (r.file == PROGRAM_TEMPORARY)
578 p->temps_output |= (1 << r.idx);
579}
580
581
Brian93fef222007-10-29 12:25:46 -0600582static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000583{
Brian93fef222007-10-29 12:25:46 -0600584 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000585
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000586 /* KW: To support tex_env_crossbar, don't release the registers in
587 * temps_output.
588 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000589 if (max_temp >= sizeof(int) * 8)
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000590 p->temp_in_use = p->temps_output;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000591 else
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000592 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000593}
594
595
Brian9fe3e2e2007-02-23 11:44:14 -0700596static struct ureg register_param5( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000597 GLint s0,
598 GLint s1,
599 GLint s2,
600 GLint s3,
Brian9fe3e2e2007-02-23 11:44:14 -0700601 GLint s4)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000602{
Brian9fe3e2e2007-02-23 11:44:14 -0700603 gl_state_index tokens[STATE_LENGTH];
Keith Whitwell47b29f52005-05-04 11:44:44 +0000604 GLuint idx;
605 tokens[0] = s0;
606 tokens[1] = s1;
607 tokens[2] = s2;
608 tokens[3] = s3;
609 tokens[4] = s4;
Brian Paulde997602005-11-12 17:53:14 +0000610 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000611 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000612}
613
Keith Whitwell47b29f52005-05-04 11:44:44 +0000614
Brian9fe3e2e2007-02-23 11:44:14 -0700615#define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
616#define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
617#define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
618#define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000619
Keith Whitwell1680ef82008-10-03 17:30:59 +0100620static GLuint frag_to_vert_attrib( GLuint attrib )
621{
622 switch (attrib) {
623 case FRAG_ATTRIB_COL0: return VERT_ATTRIB_COLOR0;
624 case FRAG_ATTRIB_COL1: return VERT_ATTRIB_COLOR1;
625 default:
626 assert(attrib >= FRAG_ATTRIB_TEX0);
627 assert(attrib <= FRAG_ATTRIB_TEX7);
628 return attrib - FRAG_ATTRIB_TEX0 + VERT_ATTRIB_TEX0;
629 }
630}
631
Keith Whitwell47b29f52005-05-04 11:44:44 +0000632
633static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
634{
Keith Whitwell1680ef82008-10-03 17:30:59 +0100635 if (p->state->inputs_available & (1<<input)) {
636 p->program->Base.InputsRead |= (1 << input);
637 return make_ureg(PROGRAM_INPUT, input);
638 }
639 else {
640 GLuint idx = frag_to_vert_attrib( input );
641 return register_param3( p, STATE_INTERNAL, STATE_CURRENT_ATTRIB, idx );
642 }
Keith Whitwell47b29f52005-05-04 11:44:44 +0000643}
644
645
Brian Paul7e807512005-11-05 17:10:45 +0000646static void emit_arg( struct prog_src_register *reg,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000647 struct ureg ureg )
648{
649 reg->File = ureg.file;
650 reg->Index = ureg.idx;
651 reg->Swizzle = ureg.swz;
Keith Whitwellf2802c42005-10-07 09:55:26 +0000652 reg->NegateBase = ureg.negatebase ? 0xf : 0x0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000653 reg->Abs = ureg.abs;
654 reg->NegateAbs = ureg.negateabs;
655}
656
Brian Paul7e807512005-11-05 17:10:45 +0000657static void emit_dst( struct prog_dst_register *dst,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000658 struct ureg ureg, GLuint mask )
659{
660 dst->File = ureg.file;
661 dst->Index = ureg.idx;
662 dst->WriteMask = mask;
Brianc9d495c2007-10-23 10:55:24 -0600663 dst->CondMask = COND_TR; /* always pass cond test */
664 dst->CondSwizzle = SWIZZLE_NOOP;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000665}
666
Brian Paul7e807512005-11-05 17:10:45 +0000667static struct prog_instruction *
Keith Whitwell15e75e02005-04-29 15:11:39 +0000668emit_op(struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000669 enum prog_opcode op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000670 struct ureg dest,
671 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000672 GLboolean saturate,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000673 struct ureg src0,
674 struct ureg src1,
675 struct ureg src2 )
676{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000677 GLuint nr = p->program->Base.NumInstructions++;
Brian Paulde997602005-11-12 17:53:14 +0000678 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
Brian2a8e9bb2007-10-23 10:24:53 -0600679
680 assert(nr < MAX_INSTRUCTIONS);
681
Brian Pauld6272e02006-10-29 18:03:16 +0000682 _mesa_init_instructions(inst, 1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000683 inst->Opcode = op;
684
Keith Whitwell47b29f52005-05-04 11:44:44 +0000685 emit_arg( &inst->SrcReg[0], src0 );
686 emit_arg( &inst->SrcReg[1], src1 );
687 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000688
Brian Paule31ac052005-11-20 17:52:40 +0000689 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000690
691 emit_dst( &inst->DstReg, dest, mask );
692
Brian Paul5620c202008-09-26 11:18:06 -0600693#if 0
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000694 /* Accounting for indirection tracking:
695 */
696 if (dest.file == PROGRAM_TEMPORARY)
697 p->temps_output |= 1 << dest.idx;
Brian Paul5620c202008-09-26 11:18:06 -0600698#endif
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000699
Keith Whitwell15e75e02005-04-29 15:11:39 +0000700 return inst;
701}
702
703
704static struct ureg emit_arith( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000705 enum prog_opcode op,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000706 struct ureg dest,
707 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000708 GLboolean saturate,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000709 struct ureg src0,
710 struct ureg src1,
711 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000712{
713 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
714
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000715 /* Accounting for indirection tracking:
716 */
717 if (src0.file == PROGRAM_TEMPORARY)
718 p->alu_temps |= 1 << src0.idx;
719
720 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
721 p->alu_temps |= 1 << src1.idx;
722
723 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
724 p->alu_temps |= 1 << src2.idx;
725
726 if (dest.file == PROGRAM_TEMPORARY)
727 p->alu_temps |= 1 << dest.idx;
728
Brian21f99792007-01-09 11:00:21 -0700729 p->program->Base.NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000730 return dest;
731}
732
733static struct ureg emit_texld( struct texenv_fragment_program *p,
Brian Paul5d7b49f2005-11-19 23:12:20 +0000734 enum prog_opcode op,
Keith Whitwellce721142005-07-11 10:10:38 +0000735 struct ureg dest,
736 GLuint destmask,
737 GLuint tex_unit,
738 GLuint tex_idx,
Brian Paul44e018c2009-02-20 13:42:08 -0700739 GLuint tex_shadow,
Keith Whitwellce721142005-07-11 10:10:38 +0000740 struct ureg coord )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000741{
Brian Paul7e807512005-11-05 17:10:45 +0000742 struct prog_instruction *inst = emit_op( p, op,
Keith Whitwell15e75e02005-04-29 15:11:39 +0000743 dest, destmask,
Brian Paul22db5352005-11-19 23:45:10 +0000744 GL_FALSE, /* don't saturate? */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000745 coord, /* arg 0? */
746 undef,
747 undef);
748
Brian Paul7e807512005-11-05 17:10:45 +0000749 inst->TexSrcTarget = tex_idx;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000750 inst->TexSrcUnit = tex_unit;
Brian Paul44e018c2009-02-20 13:42:08 -0700751 inst->TexShadow = tex_shadow;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000752
Brian21f99792007-01-09 11:00:21 -0700753 p->program->Base.NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000754
Brian Paul5620c202008-09-26 11:18:06 -0600755 /* Accounting for indirection tracking:
756 */
757 reserve_temp(p, dest);
758
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000759 /* Is this a texture indirection?
760 */
761 if ((coord.file == PROGRAM_TEMPORARY &&
762 (p->temps_output & (1<<coord.idx))) ||
763 (dest.file == PROGRAM_TEMPORARY &&
764 (p->alu_temps & (1<<dest.idx)))) {
Brian21f99792007-01-09 11:00:21 -0700765 p->program->Base.NumTexIndirections++;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000766 p->temps_output = 1<<coord.idx;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000767 p->alu_temps = 0;
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000768 assert(0); /* KW: texture env crossbar */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000769 }
770
771 return dest;
772}
773
774
Keith Whitwell47b29f52005-05-04 11:44:44 +0000775static struct ureg register_const4f( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000776 GLfloat s0,
777 GLfloat s1,
778 GLfloat s2,
779 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000780{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000781 GLfloat values[4];
Briana90046f2006-12-15 10:07:26 -0700782 GLuint idx, swizzle;
Brian Pauld01269a2008-09-25 18:27:22 -0600783 struct ureg r;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000784 values[0] = s0;
785 values[1] = s1;
786 values[2] = s2;
787 values[3] = s3;
Briana90046f2006-12-15 10:07:26 -0700788 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
789 &swizzle );
Brian Pauld01269a2008-09-25 18:27:22 -0600790 r = make_ureg(PROGRAM_CONSTANT, idx);
791 r.swz = swizzle;
792 return r;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000793}
794
Keith Whitwelle4902422005-05-11 15:16:35 +0000795#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000796#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
797#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
798#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000799
800
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000801static struct ureg get_one( struct texenv_fragment_program *p )
802{
803 if (is_undef(p->one))
804 p->one = register_scalar_const(p, 1.0);
805 return p->one;
806}
807
808static struct ureg get_half( struct texenv_fragment_program *p )
809{
810 if (is_undef(p->half))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000811 p->half = register_scalar_const(p, 0.5);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000812 return p->half;
813}
814
815static struct ureg get_zero( struct texenv_fragment_program *p )
816{
817 if (is_undef(p->zero))
Aapo Tahkola4dd8a892006-01-24 20:24:06 +0000818 p->zero = register_scalar_const(p, 0.0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000819 return p->zero;
820}
821
Keith Whitwell15e75e02005-04-29 15:11:39 +0000822
Keith Whitwell15e75e02005-04-29 15:11:39 +0000823static void program_error( struct texenv_fragment_program *p, const char *msg )
824{
Brian Paulbfb5ea32005-07-19 18:20:04 +0000825 _mesa_problem(NULL, msg);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000826 p->error = 1;
827}
828
Keith Whitwell15e75e02005-04-29 15:11:39 +0000829static struct ureg get_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000830 GLuint src, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000831{
832 switch (src) {
Keith Whitwellce721142005-07-11 10:10:38 +0000833 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000834 assert(!is_undef(p->src_texture[unit]));
835 return p->src_texture[unit];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000836
Keith Whitwellce721142005-07-11 10:10:38 +0000837 case SRC_TEXTURE0:
838 case SRC_TEXTURE1:
839 case SRC_TEXTURE2:
840 case SRC_TEXTURE3:
841 case SRC_TEXTURE4:
842 case SRC_TEXTURE5:
843 case SRC_TEXTURE6:
844 case SRC_TEXTURE7:
845 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
846 return p->src_texture[src - SRC_TEXTURE0];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000847
Keith Whitwellce721142005-07-11 10:10:38 +0000848 case SRC_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000849 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000850
Keith Whitwellce721142005-07-11 10:10:38 +0000851 case SRC_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000852 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000853
Brian Paul6947f852009-01-23 17:32:09 -0700854 case SRC_ZERO:
855 return get_zero(p);
856
Keith Whitwellce721142005-07-11 10:10:38 +0000857 case SRC_PREVIOUS:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000858 if (is_undef(p->src_previous))
859 return register_input(p, FRAG_ATTRIB_COL0);
860 else
861 return p->src_previous;
Brian Paul6947f852009-01-23 17:32:09 -0700862
863 default:
864 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000865 }
866}
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000867
Keith Whitwell15e75e02005-04-29 15:11:39 +0000868static struct ureg emit_combine_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000869 GLuint mask,
870 GLuint unit,
871 GLuint source,
872 GLuint operand )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000873{
874 struct ureg arg, src, one;
875
876 src = get_source(p, source, unit);
877
878 switch (operand) {
Keith Whitwellce721142005-07-11 10:10:38 +0000879 case OPR_ONE_MINUS_SRC_COLOR:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000880 /* Get unused tmp,
881 * Emit tmp = 1.0 - arg.xyzw
882 */
883 arg = get_temp( p );
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000884 one = get_one( p );
Brian Paul7e807512005-11-05 17:10:45 +0000885 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000886
Keith Whitwellce721142005-07-11 10:10:38 +0000887 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000888 if (mask == WRITEMASK_W)
889 return src;
890 else
Brian Paul22db5352005-11-19 23:45:10 +0000891 return swizzle1( src, SWIZZLE_W );
Keith Whitwellce721142005-07-11 10:10:38 +0000892 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000893 /* Get unused tmp,
894 * Emit tmp = 1.0 - arg.wwww
895 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000896 arg = get_temp(p);
897 one = get_one(p);
Brian Paul7e807512005-11-05 17:10:45 +0000898 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
Brian Paul22db5352005-11-19 23:45:10 +0000899 one, swizzle1(src, SWIZZLE_W), undef);
Keith Whitwellce721142005-07-11 10:10:38 +0000900 case OPR_ZERO:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000901 return get_zero(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000902 case OPR_ONE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000903 return get_one(p);
Keith Whitwellce721142005-07-11 10:10:38 +0000904 case OPR_SRC_COLOR:
Brian Paul6947f852009-01-23 17:32:09 -0700905 return src;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000906 default:
Brian Paul6947f852009-01-23 17:32:09 -0700907 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000908 return src;
909 }
910}
911
Keith Whitwellce721142005-07-11 10:10:38 +0000912static GLboolean args_match( struct state_key *key, GLuint unit )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000913{
Brian Paul22db5352005-11-19 23:45:10 +0000914 GLuint i, nr = key->unit[unit].NumArgsRGB;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000915
916 for (i = 0 ; i < nr ; i++) {
Keith Whitwellce721142005-07-11 10:10:38 +0000917 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000918 return GL_FALSE;
919
Keith Whitwellce721142005-07-11 10:10:38 +0000920 switch(key->unit[unit].OptA[i].Operand) {
921 case OPR_SRC_ALPHA:
922 switch(key->unit[unit].OptRGB[i].Operand) {
923 case OPR_SRC_COLOR:
924 case OPR_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000925 break;
926 default:
927 return GL_FALSE;
928 }
929 break;
Keith Whitwellce721142005-07-11 10:10:38 +0000930 case OPR_ONE_MINUS_SRC_ALPHA:
931 switch(key->unit[unit].OptRGB[i].Operand) {
932 case OPR_ONE_MINUS_SRC_COLOR:
933 case OPR_ONE_MINUS_SRC_ALPHA:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000934 break;
935 default:
936 return GL_FALSE;
937 }
938 break;
939 default:
940 return GL_FALSE; /* impossible */
941 }
942 }
943
944 return GL_TRUE;
945}
946
Keith Whitwell15e75e02005-04-29 15:11:39 +0000947static struct ureg emit_combine( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +0000948 struct ureg dest,
949 GLuint mask,
Brian Paul22db5352005-11-19 23:45:10 +0000950 GLboolean saturate,
Keith Whitwellce721142005-07-11 10:10:38 +0000951 GLuint unit,
952 GLuint nr,
953 GLuint mode,
Brian Pauld9736db2006-05-23 02:44:46 +0000954 const struct mode_opt *opt)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000955{
Brian Paul6947f852009-01-23 17:32:09 -0700956 struct ureg src[MAX_TERMS];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000957 struct ureg tmp, half;
Brian Paul22db5352005-11-19 23:45:10 +0000958 GLuint i;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000959
Brian Paul6947f852009-01-23 17:32:09 -0700960 assert(nr <= MAX_TERMS);
961
Brian Paul00630842005-12-12 15:27:55 +0000962 tmp = undef; /* silence warning (bug 5318) */
963
Keith Whitwell15e75e02005-04-29 15:11:39 +0000964 for (i = 0; i < nr; i++)
Keith Whitwellce721142005-07-11 10:10:38 +0000965 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000966
967 switch (mode) {
Keith Whitwellce721142005-07-11 10:10:38 +0000968 case MODE_REPLACE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000969 if (mask == WRITEMASK_XYZW && !saturate)
970 return src[0];
971 else
Brian Paul7e807512005-11-05 17:10:45 +0000972 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000973 case MODE_MODULATE:
Brian Paul7e807512005-11-05 17:10:45 +0000974 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000975 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000976 case MODE_ADD:
Brian Paul7e807512005-11-05 17:10:45 +0000977 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000978 src[0], src[1], undef );
Keith Whitwellce721142005-07-11 10:10:38 +0000979 case MODE_ADD_SIGNED:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000980 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000981 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +0000982 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +0000983 half = get_half(p);
Jerome Glisse99da2d32006-01-24 23:04:51 +0000984 tmp = get_temp( p );
Brian Paul7e807512005-11-05 17:10:45 +0000985 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
986 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000987 return dest;
Keith Whitwellce721142005-07-11 10:10:38 +0000988 case MODE_INTERPOLATE:
Keith Whitwell15e75e02005-04-29 15:11:39 +0000989 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
990 */
Brian Paul7e807512005-11-05 17:10:45 +0000991 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000992
Keith Whitwellce721142005-07-11 10:10:38 +0000993 case MODE_SUBTRACT:
Brian Paul7e807512005-11-05 17:10:45 +0000994 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000995
Keith Whitwellce721142005-07-11 10:10:38 +0000996 case MODE_DOT3_RGBA:
997 case MODE_DOT3_RGBA_EXT:
998 case MODE_DOT3_RGB_EXT:
999 case MODE_DOT3_RGB: {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001000 struct ureg tmp0 = get_temp( p );
1001 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +00001002 struct ureg neg1 = register_scalar_const(p, -1);
1003 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001004
1005 /* tmp0 = 2*src0 - 1
1006 * tmp1 = 2*src1 - 1
1007 *
1008 * dst = tmp0 dot3 tmp1
1009 */
Brian Paul7e807512005-11-05 17:10:45 +00001010 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001011 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001012
Brian Paulaa206952005-09-16 18:14:24 +00001013 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001014 tmp1 = tmp0;
1015 else
Brian Paul7e807512005-11-05 17:10:45 +00001016 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001017 two, src[1], neg1);
Brian Paul7e807512005-11-05 17:10:45 +00001018 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001019 return dest;
1020 }
Keith Whitwellce721142005-07-11 10:10:38 +00001021 case MODE_MODULATE_ADD_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001022 /* Arg0 * Arg2 + Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001023 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001024 src[0], src[2], src[1] );
Keith Whitwellce721142005-07-11 10:10:38 +00001025 case MODE_MODULATE_SIGNED_ADD_ATI: {
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001026 /* Arg0 * Arg2 + Arg1 - 0.5 */
1027 struct ureg tmp0 = get_temp(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001028 half = get_half(p);
Brian Paul7e807512005-11-05 17:10:45 +00001029 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
1030 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001031 return dest;
1032 }
Keith Whitwellce721142005-07-11 10:10:38 +00001033 case MODE_MODULATE_SUBTRACT_ATI:
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001034 /* Arg0 * Arg2 - Arg1 */
Brian Paul7e807512005-11-05 17:10:45 +00001035 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
Keith Whitwell6fe176a2005-05-23 08:08:43 +00001036 return dest;
Brian Paul6947f852009-01-23 17:32:09 -07001037 case MODE_ADD_PRODUCTS:
1038 /* Arg0 * Arg1 + Arg2 * Arg3 */
1039 {
1040 struct ureg tmp0 = get_temp(p);
1041 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1042 emit_arith( p, OPCODE_MAD, dest, mask, saturate, src[2], src[3], tmp0 );
1043 }
1044 return dest;
1045 case MODE_ADD_PRODUCTS_SIGNED:
1046 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1047 {
1048 struct ureg tmp0 = get_temp(p);
1049 half = get_half(p);
1050 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1051 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[2], src[3], tmp0 );
1052 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1053 }
1054 return dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001055 default:
Brian Paul6947f852009-01-23 17:32:09 -07001056 assert(0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001057 return src[0];
1058 }
1059}
1060
Keith Whitwell15e75e02005-04-29 15:11:39 +00001061
Brian Paul22db5352005-11-19 23:45:10 +00001062/**
1063 * Generate instructions for one texture unit's env/combiner mode.
1064 */
1065static struct ureg
1066emit_texenv(struct texenv_fragment_program *p, GLuint unit)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001067{
Keith Whitwellce721142005-07-11 10:10:38 +00001068 struct state_key *key = p->state;
Brian Paul22db5352005-11-19 23:45:10 +00001069 GLboolean saturate = (unit < p->last_tex_stage);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001070 GLuint rgb_shift, alpha_shift;
1071 struct ureg out, shift;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001072 struct ureg dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001073
Keith Whitwellce721142005-07-11 10:10:38 +00001074 if (!key->unit[unit].enabled) {
1075 return get_source(p, SRC_PREVIOUS, 0);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001076 }
Keith Whitwellce721142005-07-11 10:10:38 +00001077
1078 switch (key->unit[unit].ModeRGB) {
1079 case MODE_DOT3_RGB_EXT:
1080 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001081 rgb_shift = 0;
1082 break;
Keith Whitwellce721142005-07-11 10:10:38 +00001083 case MODE_DOT3_RGBA_EXT:
Keith Whitwell15e75e02005-04-29 15:11:39 +00001084 alpha_shift = 0;
1085 rgb_shift = 0;
1086 break;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001087 default:
Keith Whitwellce721142005-07-11 10:10:38 +00001088 rgb_shift = key->unit[unit].ScaleShiftRGB;
1089 alpha_shift = key->unit[unit].ScaleShiftA;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001090 break;
1091 }
Keith Whitwellce721142005-07-11 10:10:38 +00001092
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001093 /* If this is the very last calculation, emit direct to output reg:
1094 */
Keith Whitwellce721142005-07-11 10:10:38 +00001095 if (key->separate_specular ||
Keith Whitwellcf4f3c52005-05-16 12:15:01 +00001096 unit != p->last_tex_stage ||
1097 alpha_shift ||
1098 rgb_shift)
1099 dest = get_temp( p );
1100 else
Brian Paul8d475822009-02-28 11:49:46 -07001101 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLOR);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001102
1103 /* Emit the RGB and A combine ops
1104 */
Keith Whitwellce721142005-07-11 10:10:38 +00001105 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
1106 args_match(key, unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001107 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1108 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001109 key->unit[unit].NumArgsRGB,
1110 key->unit[unit].ModeRGB,
1111 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001112 }
Keith Whitwellce721142005-07-11 10:10:38 +00001113 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
Roland Scheidegger9a451762007-07-03 14:27:41 +02001114 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001115
1116 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1117 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001118 key->unit[unit].NumArgsRGB,
1119 key->unit[unit].ModeRGB,
1120 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001121 }
1122 else {
1123 /* Need to do something to stop from re-emitting identical
1124 * argument calculations here:
1125 */
1126 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
1127 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001128 key->unit[unit].NumArgsRGB,
1129 key->unit[unit].ModeRGB,
1130 key->unit[unit].OptRGB);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001131 out = emit_combine( p, dest, WRITEMASK_W, saturate,
1132 unit,
Keith Whitwellce721142005-07-11 10:10:38 +00001133 key->unit[unit].NumArgsA,
1134 key->unit[unit].ModeA,
1135 key->unit[unit].OptA);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001136 }
1137
1138 /* Deal with the final shift:
1139 */
1140 if (alpha_shift || rgb_shift) {
1141 if (rgb_shift == alpha_shift) {
José Fonseca452a5922008-05-31 18:14:09 +09001142 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001143 }
1144 else {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001145 shift = register_const4f(p,
José Fonseca452a5922008-05-31 18:14:09 +09001146 (GLfloat)(1<<rgb_shift),
1147 (GLfloat)(1<<rgb_shift),
1148 (GLfloat)(1<<rgb_shift),
1149 (GLfloat)(1<<alpha_shift));
Keith Whitwell15e75e02005-04-29 15:11:39 +00001150 }
Brian Paul7e807512005-11-05 17:10:45 +00001151 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
Keith Whitwell15e75e02005-04-29 15:11:39 +00001152 saturate, out, shift, undef );
1153 }
1154 else
1155 return out;
1156}
1157
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001158
Brian Paul22db5352005-11-19 23:45:10 +00001159/**
1160 * Generate instruction for getting a texture source term.
1161 */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001162static void load_texture( struct texenv_fragment_program *p, GLuint unit )
1163{
1164 if (is_undef(p->src_texture[unit])) {
Brian Paul44e018c2009-02-20 13:42:08 -07001165 GLuint texTarget = p->state->unit[unit].source_index;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001166 struct ureg texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
1167 struct ureg tmp = get_tex_temp( p );
1168
Brian Paul44e018c2009-02-20 13:42:08 -07001169 if (texTarget == TEXTURE_UNKNOWN_INDEX)
Brian Paulbfb5ea32005-07-19 18:20:04 +00001170 program_error(p, "TexSrcBit");
Keith Whitwellce721142005-07-11 10:10:38 +00001171
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001172 /* TODO: Use D0_MASK_XY where possible.
1173 */
Nicolai Haehnle83ad2a72008-06-14 02:28:58 +02001174 if (p->state->unit[unit].enabled) {
Brian Paul44e018c2009-02-20 13:42:08 -07001175 GLboolean shadow = GL_FALSE;
1176
1177 if (p->state->unit[unit].shadow) {
1178 p->program->Base.ShadowSamplers |= 1 << unit;
1179 shadow = GL_TRUE;
1180 }
1181
Aapo Tahkolab8915342006-03-28 10:26:34 +00001182 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1183 tmp, WRITEMASK_XYZW,
Brian Paul44e018c2009-02-20 13:42:08 -07001184 unit, texTarget, shadow,
1185 texcoord );
Brian9b7e5a52007-12-14 11:16:49 -07001186
1187 p->program->Base.SamplersUsed |= (1 << unit);
Brian1fe385f2007-12-14 11:42:28 -07001188 /* This identity mapping should already be in place
1189 * (see _mesa_init_program_struct()) but let's be safe.
1190 */
1191 p->program->Base.SamplerUnits[unit] = unit;
Brian9b7e5a52007-12-14 11:16:49 -07001192 }
1193 else
Aapo Tahkolab8915342006-03-28 10:26:34 +00001194 p->src_texture[unit] = get_zero(p);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001195 }
1196}
1197
Keith Whitwell241b6b72005-05-23 09:50:34 +00001198static GLboolean load_texenv_source( struct texenv_fragment_program *p,
Keith Whitwellce721142005-07-11 10:10:38 +00001199 GLuint src, GLuint unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001200{
1201 switch (src) {
Aapo Tahkola4dd8a892006-01-24 20:24:06 +00001202 case SRC_TEXTURE:
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001203 load_texture(p, unit);
1204 break;
1205
Keith Whitwellce721142005-07-11 10:10:38 +00001206 case SRC_TEXTURE0:
1207 case SRC_TEXTURE1:
1208 case SRC_TEXTURE2:
1209 case SRC_TEXTURE3:
1210 case SRC_TEXTURE4:
1211 case SRC_TEXTURE5:
1212 case SRC_TEXTURE6:
1213 case SRC_TEXTURE7:
Keith Whitwellce721142005-07-11 10:10:38 +00001214 load_texture(p, src - SRC_TEXTURE0);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001215 break;
1216
1217 default:
Brian Paul6947f852009-01-23 17:32:09 -07001218 /* not a texture src - do nothing */
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001219 break;
1220 }
Keith Whitwell241b6b72005-05-23 09:50:34 +00001221
1222 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001223}
1224
Brian Paul22db5352005-11-19 23:45:10 +00001225
1226/**
1227 * Generate instructions for loading all texture source terms.
1228 */
1229static GLboolean
1230load_texunit_sources( struct texenv_fragment_program *p, int unit )
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001231{
Keith Whitwellce721142005-07-11 10:10:38 +00001232 struct state_key *key = p->state;
Aapo Tahkolab8915342006-03-28 10:26:34 +00001233 GLuint i;
1234
1235 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
1236 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit);
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001237 }
Aapo Tahkolab8915342006-03-28 10:26:34 +00001238
1239 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1240 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1241 }
1242
Keith Whitwell241b6b72005-05-23 09:50:34 +00001243 return GL_TRUE;
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001244}
1245
Brian Paul22db5352005-11-19 23:45:10 +00001246
1247/**
1248 * Generate a new fragment program which implements the context's
1249 * current texture env/combine mode.
1250 */
1251static void
Brian Paule998c342006-10-29 21:17:18 +00001252create_new_program(GLcontext *ctx, struct state_key *key,
Brian Paul122629f2006-07-20 16:49:57 +00001253 struct gl_fragment_program *program)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001254{
Brian Paule998c342006-10-29 21:17:18 +00001255 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
Keith Whitwell15e75e02005-04-29 15:11:39 +00001256 struct texenv_fragment_program p;
1257 GLuint unit;
1258 struct ureg cf, out;
Keith Whitwell276330b2005-05-09 17:58:13 +00001259
Keith Whitwelle4902422005-05-11 15:16:35 +00001260 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +00001261 p.ctx = ctx;
Keith Whitwellce721142005-07-11 10:10:38 +00001262 p.state = key;
1263 p.program = program;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001264
Brian Paule998c342006-10-29 21:17:18 +00001265 /* During code generation, use locally-allocated instruction buffer,
1266 * then alloc dynamic storage below.
1267 */
1268 p.program->Base.Instructions = instBuffer;
Keith Whitwell276330b2005-05-09 17:58:13 +00001269 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Brian21f99792007-01-09 11:00:21 -07001270 p.program->Base.NumTexIndirections = 1; /* correct? */
1271 p.program->Base.NumTexInstructions = 0;
1272 p.program->Base.NumAluInstructions = 0;
Brian1240eb22007-03-22 08:50:20 -06001273 p.program->Base.String = NULL;
Keith Whitwell9ca88152005-05-10 09:56:02 +00001274 p.program->Base.NumInstructions =
Brian Paule998c342006-10-29 21:17:18 +00001275 p.program->Base.NumTemporaries =
1276 p.program->Base.NumParameters =
1277 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00001278 p.program->Base.Parameters = _mesa_new_parameter_list();
Keith Whitwelldbeea252005-05-10 13:57:50 +00001279
Brian Paulde997602005-11-12 17:53:14 +00001280 p.program->Base.InputsRead = 0;
Brian Paul8d475822009-02-28 11:49:46 -07001281 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLOR;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001282
Brian Paule9b34882008-12-31 11:54:02 -07001283 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001284 p.src_texture[unit] = undef;
1285
Keith Whitwell47b29f52005-05-04 11:44:44 +00001286 p.src_previous = undef;
Keith Whitwell5ddc53f2006-05-22 14:17:32 +00001287 p.half = undef;
1288 p.zero = undef;
1289 p.one = undef;
1290
Keith Whitwell15e75e02005-04-29 15:11:39 +00001291 p.last_tex_stage = 0;
Brian93fef222007-10-29 12:25:46 -06001292 release_temps(ctx, &p);
Keith Whitwell15e75e02005-04-29 15:11:39 +00001293
Keith Whitwellce721142005-07-11 10:10:38 +00001294 if (key->enabled_units) {
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001295 /* First pass - to support texture_env_crossbar, first identify
1296 * all referenced texture sources and emit texld instructions
1297 * for each:
1298 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001299 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001300 if (key->unit[unit].enabled) {
Aapo Tahkolab8915342006-03-28 10:26:34 +00001301 load_texunit_sources( &p, unit );
1302 p.last_tex_stage = unit;
Keith Whitwell15e75e02005-04-29 15:11:39 +00001303 }
1304
Keith Whitwell2dea6df2005-05-23 09:37:32 +00001305 /* Second pass - emit combine instructions to build final color:
1306 */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001307 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
Keith Whitwellce721142005-07-11 10:10:38 +00001308 if (key->enabled_units & (1<<unit)) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001309 p.src_previous = emit_texenv( &p, unit );
Brian Paul5620c202008-09-26 11:18:06 -06001310 reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
Brian93fef222007-10-29 12:25:46 -06001311 release_temps(ctx, &p); /* release all temps */
Keith Whitwell15e75e02005-04-29 15:11:39 +00001312 }
1313 }
1314
Keith Whitwellce721142005-07-11 10:10:38 +00001315 cf = get_source( &p, SRC_PREVIOUS, 0 );
Brian Paul8d475822009-02-28 11:49:46 -07001316 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLOR );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001317
Keith Whitwellce721142005-07-11 10:10:38 +00001318 if (key->separate_specular) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001319 /* Emit specular add.
1320 */
Keith Whitwell47b29f52005-05-04 11:44:44 +00001321 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Brian Paul7e807512005-11-05 17:10:45 +00001322 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1323 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001324 }
Brian Paulaa206952005-09-16 18:14:24 +00001325 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
Keith Whitwell15e75e02005-04-29 15:11:39 +00001326 /* Will wind up in here if no texture enabled or a couple of
1327 * other scenarios (GL_REPLACE for instance).
1328 */
Brian Paul7e807512005-11-05 17:10:45 +00001329 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +00001330 }
1331
Keith Whitwell47b29f52005-05-04 11:44:44 +00001332 /* Finish up:
1333 */
Brian Paul7e807512005-11-05 17:10:45 +00001334 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
Keith Whitwell47b29f52005-05-04 11:44:44 +00001335
Keith Whitwellce721142005-07-11 10:10:38 +00001336 if (key->fog_enabled) {
1337 /* Pull fog mode from GLcontext, the value in the state key is
1338 * a reduced value and not what is expected in FogOption
1339 */
Keith Whitwell276330b2005-05-09 17:58:13 +00001340 p.program->FogOption = ctx->Fog.Mode;
Brian19d77d62007-09-18 19:29:26 -06001341 p.program->Base.InputsRead |= FRAG_BIT_FOGC; /* XXX new */
Keith Whitwellce721142005-07-11 10:10:38 +00001342 } else
Keith Whitwell276330b2005-05-09 17:58:13 +00001343 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +00001344
Brian21f99792007-01-09 11:00:21 -07001345 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001346 program_error(&p, "Exceeded max nr indirect texture lookups");
1347
Brian21f99792007-01-09 11:00:21 -07001348 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001349 program_error(&p, "Exceeded max TEX instructions");
1350
Brian21f99792007-01-09 11:00:21 -07001351 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +00001352 program_error(&p, "Exceeded max ALU instructions");
1353
Brian Paul5d7b49f2005-11-19 23:12:20 +00001354 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
Keith Whitwell8b88f622005-05-10 11:39:50 +00001355
Brian Paule998c342006-10-29 21:17:18 +00001356 /* Allocate final instruction array */
Brianc81cce72007-09-25 15:18:51 -06001357 p.program->Base.Instructions
1358 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1359 if (!p.program->Base.Instructions) {
Brian Paule998c342006-10-29 21:17:18 +00001360 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1361 "generating tex env program");
1362 return;
1363 }
Brianc81cce72007-09-25 15:18:51 -06001364 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1365 p.program->Base.NumInstructions);
1366
1367 if (p.program->FogOption) {
1368 _mesa_append_fog_code(ctx, p.program);
1369 p.program->FogOption = GL_NONE;
1370 }
1371
Brian Paule998c342006-10-29 21:17:18 +00001372
Keith Whitwelldbeea252005-05-10 13:57:50 +00001373 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +00001374 */
Brian Paule998c342006-10-29 21:17:18 +00001375 if (ctx->Driver.ProgramStringNotify) {
1376 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1377 &p.program->Base );
1378 }
Keith Whitwelldbeea252005-05-10 13:57:50 +00001379
Brian Paule998c342006-10-29 21:17:18 +00001380 if (DISASSEM) {
1381 _mesa_print_program(&p.program->Base);
1382 _mesa_printf("\n");
Keith Whitwelle4902422005-05-11 15:16:35 +00001383 }
Keith Whitwell15e75e02005-04-29 15:11:39 +00001384}
1385
Brian Paul5d7b49f2005-11-19 23:12:20 +00001386
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001387/**
1388 * Return a fragment program which implements the current
1389 * fixed-function texture, fog and color-sum operations.
1390 */
1391struct gl_fragment_program *
1392_mesa_get_fixed_func_fragment_program(GLcontext *ctx)
Keith Whitwellce721142005-07-11 10:10:38 +00001393{
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001394 struct gl_fragment_program *prog;
1395 struct state_key key;
1396
1397 make_state_key(ctx, &key);
1398
1399 prog = (struct gl_fragment_program *)
1400 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1401 &key, sizeof(key));
Keith Whitwellce721142005-07-11 10:10:38 +00001402
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001403 if (!prog) {
1404 prog = (struct gl_fragment_program *)
1405 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1406
1407 create_new_program(ctx, &key, prog);
1408
1409 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1410 &key, sizeof(key), &prog->Base);
Keith Whitwellce721142005-07-11 10:10:38 +00001411 }
1412
Keith Whitwell32ef6e72008-09-20 08:26:11 -07001413 return prog;
Keith Whitwellce721142005-07-11 10:10:38 +00001414}