Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[mesa.git] / src / mesa / main / texenvprogram.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 * Copyright 2009 VMware, Inc. All Rights Reserved.
6 *
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
29 #include "glheader.h"
30 #include "imports.h"
31 #include "shader/program.h"
32 #include "shader/prog_parameter.h"
33 #include "shader/prog_cache.h"
34 #include "shader/prog_instruction.h"
35 #include "shader/prog_print.h"
36 #include "shader/prog_statevars.h"
37 #include "shader/programopt.h"
38 #include "texenvprogram.h"
39
40
41 /*
42 * Note on texture units:
43 *
44 * The number of texture units supported by fixed-function fragment
45 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
46 * That's because there's a one-to-one correspondence between texture
47 * coordinates and samplers in fixed-function processing.
48 *
49 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
50 * sets of texcoords, so is fixed-function fragment processing.
51 *
52 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
53 */
54
55
56 struct texenvprog_cache_item
57 {
58 GLuint hash;
59 void *key;
60 struct gl_fragment_program *data;
61 struct texenvprog_cache_item *next;
62 };
63
64 static GLboolean
65 texenv_doing_secondary_color(GLcontext *ctx)
66 {
67 if (ctx->Light.Enabled &&
68 (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR))
69 return GL_TRUE;
70
71 if (ctx->Fog.ColorSumEnabled)
72 return GL_TRUE;
73
74 return GL_FALSE;
75 }
76
77 /**
78 * Up to nine instructions per tex unit, plus fog, specular color.
79 */
80 #define MAX_INSTRUCTIONS ((MAX_TEXTURE_COORD_UNITS * 9) + 12)
81
82 #define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
83
84 struct mode_opt {
85 #ifdef __GNUC__
86 __extension__ GLubyte Source:4; /**< SRC_x */
87 __extension__ GLubyte Operand:3; /**< OPR_x */
88 #else
89 GLubyte Source; /**< SRC_x */
90 GLubyte Operand; /**< OPR_x */
91 #endif
92 };
93
94 struct state_key {
95 GLuint nr_enabled_units:8;
96 GLuint enabled_units:8;
97 GLuint separate_specular:1;
98 GLuint fog_enabled:1;
99 GLuint fog_mode:2; /**< FOG_x */
100 GLuint inputs_available:12;
101
102 /* NOTE: This array of structs must be last! (see "keySize" below) */
103 struct {
104 GLuint enabled:1;
105 GLuint source_index:3; /**< TEXTURE_x_INDEX */
106 GLuint shadow:1;
107 GLuint ScaleShiftRGB:2;
108 GLuint ScaleShiftA:2;
109
110 GLuint NumArgsRGB:3; /**< up to MAX_COMBINER_TERMS */
111 GLuint ModeRGB:5; /**< MODE_x */
112
113 GLuint NumArgsA:3; /**< up to MAX_COMBINER_TERMS */
114 GLuint ModeA:5; /**< MODE_x */
115
116 struct mode_opt OptRGB[MAX_COMBINER_TERMS];
117 struct mode_opt OptA[MAX_COMBINER_TERMS];
118 } unit[MAX_TEXTURE_UNITS];
119 };
120
121 #define FOG_LINEAR 0
122 #define FOG_EXP 1
123 #define FOG_EXP2 2
124 #define FOG_UNKNOWN 3
125
126 static GLuint translate_fog_mode( GLenum mode )
127 {
128 switch (mode) {
129 case GL_LINEAR: return FOG_LINEAR;
130 case GL_EXP: return FOG_EXP;
131 case GL_EXP2: return FOG_EXP2;
132 default: return FOG_UNKNOWN;
133 }
134 }
135
136 #define OPR_SRC_COLOR 0
137 #define OPR_ONE_MINUS_SRC_COLOR 1
138 #define OPR_SRC_ALPHA 2
139 #define OPR_ONE_MINUS_SRC_ALPHA 3
140 #define OPR_ZERO 4
141 #define OPR_ONE 5
142 #define OPR_UNKNOWN 7
143
144 static GLuint translate_operand( GLenum operand )
145 {
146 switch (operand) {
147 case GL_SRC_COLOR: return OPR_SRC_COLOR;
148 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
149 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
150 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
151 case GL_ZERO: return OPR_ZERO;
152 case GL_ONE: return OPR_ONE;
153 default:
154 assert(0);
155 return OPR_UNKNOWN;
156 }
157 }
158
159 #define SRC_TEXTURE 0
160 #define SRC_TEXTURE0 1
161 #define SRC_TEXTURE1 2
162 #define SRC_TEXTURE2 3
163 #define SRC_TEXTURE3 4
164 #define SRC_TEXTURE4 5
165 #define SRC_TEXTURE5 6
166 #define SRC_TEXTURE6 7
167 #define SRC_TEXTURE7 8
168 #define SRC_CONSTANT 9
169 #define SRC_PRIMARY_COLOR 10
170 #define SRC_PREVIOUS 11
171 #define SRC_ZERO 12
172 #define SRC_UNKNOWN 15
173
174 static GLuint translate_source( GLenum src )
175 {
176 switch (src) {
177 case GL_TEXTURE: return SRC_TEXTURE;
178 case GL_TEXTURE0:
179 case GL_TEXTURE1:
180 case GL_TEXTURE2:
181 case GL_TEXTURE3:
182 case GL_TEXTURE4:
183 case GL_TEXTURE5:
184 case GL_TEXTURE6:
185 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
186 case GL_CONSTANT: return SRC_CONSTANT;
187 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
188 case GL_PREVIOUS: return SRC_PREVIOUS;
189 case GL_ZERO:
190 return SRC_ZERO;
191 default:
192 assert(0);
193 return SRC_UNKNOWN;
194 }
195 }
196
197 #define MODE_REPLACE 0 /* r = a0 */
198 #define MODE_MODULATE 1 /* r = a0 * a1 */
199 #define MODE_ADD 2 /* r = a0 + a1 */
200 #define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
201 #define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
202 #define MODE_SUBTRACT 5 /* r = a0 - a1 */
203 #define MODE_DOT3_RGB 6 /* r = a0 . a1 */
204 #define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
205 #define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
206 #define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
207 #define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
208 #define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
209 #define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
210 #define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
211 #define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
212 #define MODE_BUMP_ENVMAP_ATI 15 /* special */
213 #define MODE_UNKNOWN 16
214
215 /**
216 * Translate GL combiner state into a MODE_x value
217 */
218 static GLuint translate_mode( GLenum envMode, GLenum mode )
219 {
220 switch (mode) {
221 case GL_REPLACE: return MODE_REPLACE;
222 case GL_MODULATE: return MODE_MODULATE;
223 case GL_ADD:
224 if (envMode == GL_COMBINE4_NV)
225 return MODE_ADD_PRODUCTS;
226 else
227 return MODE_ADD;
228 case GL_ADD_SIGNED:
229 if (envMode == GL_COMBINE4_NV)
230 return MODE_ADD_PRODUCTS_SIGNED;
231 else
232 return MODE_ADD_SIGNED;
233 case GL_INTERPOLATE: return MODE_INTERPOLATE;
234 case GL_SUBTRACT: return MODE_SUBTRACT;
235 case GL_DOT3_RGB: return MODE_DOT3_RGB;
236 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
237 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
238 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
239 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
240 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
241 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
242 case GL_BUMP_ENVMAP_ATI: return MODE_BUMP_ENVMAP_ATI;
243 default:
244 assert(0);
245 return MODE_UNKNOWN;
246 }
247 }
248
249
250 /**
251 * Do we need to clamp the results of the given texture env/combine mode?
252 * If the inputs to the mode are in [0,1] we don't always have to clamp
253 * the results.
254 */
255 static GLboolean
256 need_saturate( GLuint mode )
257 {
258 switch (mode) {
259 case MODE_REPLACE:
260 case MODE_MODULATE:
261 case MODE_INTERPOLATE:
262 return GL_FALSE;
263 case MODE_ADD:
264 case MODE_ADD_SIGNED:
265 case MODE_SUBTRACT:
266 case MODE_DOT3_RGB:
267 case MODE_DOT3_RGB_EXT:
268 case MODE_DOT3_RGBA:
269 case MODE_DOT3_RGBA_EXT:
270 case MODE_MODULATE_ADD_ATI:
271 case MODE_MODULATE_SIGNED_ADD_ATI:
272 case MODE_MODULATE_SUBTRACT_ATI:
273 case MODE_ADD_PRODUCTS:
274 case MODE_ADD_PRODUCTS_SIGNED:
275 case MODE_BUMP_ENVMAP_ATI:
276 return GL_TRUE;
277 default:
278 assert(0);
279 return GL_FALSE;
280 }
281 }
282
283
284
285 /**
286 * Translate TEXTURE_x_BIT to TEXTURE_x_INDEX.
287 */
288 static GLuint translate_tex_src_bit( GLbitfield bit )
289 {
290 ASSERT(bit);
291 return _mesa_ffs(bit) - 1;
292 }
293
294
295 #define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
296 #define VERT_RESULT_TEX_ANY (0xff << VERT_RESULT_TEX0)
297
298 /**
299 * Identify all possible varying inputs. The fragment program will
300 * never reference non-varying inputs, but will track them via state
301 * constants instead.
302 *
303 * This function figures out all the inputs that the fragment program
304 * has access to. The bitmask is later reduced to just those which
305 * are actually referenced.
306 */
307 static GLbitfield get_fp_input_mask( GLcontext *ctx )
308 {
309 /* _NEW_PROGRAM */
310 const GLboolean vertexShader = (ctx->Shader.CurrentProgram &&
311 ctx->Shader.CurrentProgram->LinkStatus &&
312 ctx->Shader.CurrentProgram->VertexProgram);
313 const GLboolean vertexProgram = ctx->VertexProgram._Enabled;
314 GLbitfield fp_inputs = 0x0;
315
316 if (ctx->VertexProgram._Overriden) {
317 /* Somebody's messing with the vertex program and we don't have
318 * a clue what's happening. Assume that it could be producing
319 * all possible outputs.
320 */
321 fp_inputs = ~0;
322 }
323 else if (ctx->RenderMode == GL_FEEDBACK) {
324 /* _NEW_RENDERMODE */
325 fp_inputs = (FRAG_BIT_COL0 | FRAG_BIT_TEX0);
326 }
327 else if (!(vertexProgram || vertexShader) ||
328 !ctx->VertexProgram._Current) {
329 /* Fixed function vertex logic */
330 /* _NEW_ARRAY */
331 GLbitfield varying_inputs = ctx->varying_vp_inputs;
332
333 /* These get generated in the setup routine regardless of the
334 * vertex program:
335 */
336 /* _NEW_POINT */
337 if (ctx->Point.PointSprite)
338 varying_inputs |= FRAG_BITS_TEX_ANY;
339
340 /* First look at what values may be computed by the generated
341 * vertex program:
342 */
343 /* _NEW_LIGHT */
344 if (ctx->Light.Enabled) {
345 fp_inputs |= FRAG_BIT_COL0;
346
347 if (texenv_doing_secondary_color(ctx))
348 fp_inputs |= FRAG_BIT_COL1;
349 }
350
351 /* _NEW_TEXTURE */
352 fp_inputs |= (ctx->Texture._TexGenEnabled |
353 ctx->Texture._TexMatEnabled) << FRAG_ATTRIB_TEX0;
354
355 /* Then look at what might be varying as a result of enabled
356 * arrays, etc:
357 */
358 if (varying_inputs & VERT_BIT_COLOR0)
359 fp_inputs |= FRAG_BIT_COL0;
360 if (varying_inputs & VERT_BIT_COLOR1)
361 fp_inputs |= FRAG_BIT_COL1;
362
363 fp_inputs |= (((varying_inputs & VERT_BIT_TEX_ANY) >> VERT_ATTRIB_TEX0)
364 << FRAG_ATTRIB_TEX0);
365
366 }
367 else {
368 /* calculate from vp->outputs */
369 struct gl_vertex_program *vprog;
370 GLbitfield64 vp_outputs;
371
372 /* Choose GLSL vertex shader over ARB vertex program. Need this
373 * since vertex shader state validation comes after fragment state
374 * validation (see additional comments in state.c).
375 */
376 if (vertexShader)
377 vprog = ctx->Shader.CurrentProgram->VertexProgram;
378 else
379 vprog = ctx->VertexProgram.Current;
380
381 vp_outputs = vprog->Base.OutputsWritten;
382
383 /* These get generated in the setup routine regardless of the
384 * vertex program:
385 */
386 /* _NEW_POINT */
387 if (ctx->Point.PointSprite)
388 vp_outputs |= FRAG_BITS_TEX_ANY;
389
390 if (vp_outputs & (1 << VERT_RESULT_COL0))
391 fp_inputs |= FRAG_BIT_COL0;
392 if (vp_outputs & (1 << VERT_RESULT_COL1))
393 fp_inputs |= FRAG_BIT_COL1;
394
395 fp_inputs |= (((vp_outputs & VERT_RESULT_TEX_ANY) >> VERT_RESULT_TEX0)
396 << FRAG_ATTRIB_TEX0);
397 }
398
399 return fp_inputs;
400 }
401
402
403 /**
404 * Examine current texture environment state and generate a unique
405 * key to identify it.
406 */
407 static GLuint make_state_key( GLcontext *ctx, struct state_key *key )
408 {
409 GLuint i, j;
410 GLbitfield inputs_referenced = FRAG_BIT_COL0;
411 const GLbitfield inputs_available = get_fp_input_mask( ctx );
412 GLuint keySize;
413
414 memset(key, 0, sizeof(*key));
415
416 /* _NEW_TEXTURE */
417 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
418 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
419 const struct gl_texture_object *texObj = texUnit->_Current;
420 const struct gl_tex_env_combine_state *comb = texUnit->_CurrentCombine;
421 GLenum format;
422
423 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
424 continue;
425
426 format = texObj->Image[0][texObj->BaseLevel]->_BaseFormat;
427
428 key->unit[i].enabled = 1;
429 key->enabled_units |= (1<<i);
430 key->nr_enabled_units = i + 1;
431 inputs_referenced |= FRAG_BIT_TEX(i);
432
433 key->unit[i].source_index =
434 translate_tex_src_bit(texUnit->_ReallyEnabled);
435
436 key->unit[i].shadow = ((texObj->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
437 ((format == GL_DEPTH_COMPONENT) ||
438 (format == GL_DEPTH_STENCIL_EXT)));
439
440 key->unit[i].NumArgsRGB = comb->_NumArgsRGB;
441 key->unit[i].NumArgsA = comb->_NumArgsA;
442
443 key->unit[i].ModeRGB =
444 translate_mode(texUnit->EnvMode, comb->ModeRGB);
445 key->unit[i].ModeA =
446 translate_mode(texUnit->EnvMode, comb->ModeA);
447
448 key->unit[i].ScaleShiftRGB = comb->ScaleShiftRGB;
449 key->unit[i].ScaleShiftA = comb->ScaleShiftA;
450
451 for (j = 0; j < MAX_COMBINER_TERMS; j++) {
452 key->unit[i].OptRGB[j].Operand = translate_operand(comb->OperandRGB[j]);
453 key->unit[i].OptA[j].Operand = translate_operand(comb->OperandA[j]);
454 key->unit[i].OptRGB[j].Source = translate_source(comb->SourceRGB[j]);
455 key->unit[i].OptA[j].Source = translate_source(comb->SourceA[j]);
456 }
457
458 if (key->unit[i].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
459 /* requires some special translation */
460 key->unit[i].NumArgsRGB = 2;
461 key->unit[i].ScaleShiftRGB = 0;
462 key->unit[i].OptRGB[0].Operand = OPR_SRC_COLOR;
463 key->unit[i].OptRGB[0].Source = SRC_TEXTURE;
464 key->unit[i].OptRGB[1].Operand = OPR_SRC_COLOR;
465 key->unit[i].OptRGB[1].Source = texUnit->BumpTarget - GL_TEXTURE0 + SRC_TEXTURE0;
466 }
467 }
468
469 /* _NEW_LIGHT | _NEW_FOG */
470 if (texenv_doing_secondary_color(ctx)) {
471 key->separate_specular = 1;
472 inputs_referenced |= FRAG_BIT_COL1;
473 }
474
475 /* _NEW_FOG */
476 if (ctx->Fog.Enabled) {
477 key->fog_enabled = 1;
478 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
479 inputs_referenced |= FRAG_BIT_FOGC; /* maybe */
480 }
481
482 key->inputs_available = (inputs_available & inputs_referenced);
483
484 /* compute size of state key, ignoring unused texture units */
485 keySize = sizeof(*key) - sizeof(key->unit)
486 + key->nr_enabled_units * sizeof(key->unit[0]);
487
488 return keySize;
489 }
490
491
492 /**
493 * Use uregs to represent registers internally, translate to Mesa's
494 * expected formats on emit.
495 *
496 * NOTE: These are passed by value extensively in this file rather
497 * than as usual by pointer reference. If this disturbs you, try
498 * remembering they are just 32bits in size.
499 *
500 * GCC is smart enough to deal with these dword-sized structures in
501 * much the same way as if I had defined them as dwords and was using
502 * macros to access and set the fields. This is much nicer and easier
503 * to evolve.
504 */
505 struct ureg {
506 GLuint file:4;
507 GLuint idx:8;
508 GLuint negatebase:1;
509 GLuint swz:12;
510 GLuint pad:7;
511 };
512
513 static const struct ureg undef = {
514 PROGRAM_UNDEFINED,
515 ~0,
516 0,
517 0,
518 0
519 };
520
521
522 /** State used to build the fragment program:
523 */
524 struct texenv_fragment_program {
525 struct gl_fragment_program *program;
526 struct state_key *state;
527
528 GLbitfield alu_temps; /**< Track texture indirections, see spec. */
529 GLbitfield temps_output; /**< Track texture indirections, see spec. */
530 GLbitfield temp_in_use; /**< Tracks temporary regs which are in use. */
531 GLboolean error;
532
533 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
534 /* Reg containing each texture unit's sampled texture color,
535 * else undef.
536 */
537
538 struct ureg texcoord_tex[MAX_TEXTURE_COORD_UNITS];
539 /* Reg containing texcoord for a texture unit,
540 * needed for bump mapping, else undef.
541 */
542
543 struct ureg src_previous; /**< Reg containing color from previous
544 * stage. May need to be decl'd.
545 */
546
547 GLuint last_tex_stage; /**< Number of last enabled texture unit */
548
549 struct ureg half;
550 struct ureg one;
551 struct ureg zero;
552 };
553
554
555
556 static struct ureg make_ureg(GLuint file, GLuint idx)
557 {
558 struct ureg reg;
559 reg.file = file;
560 reg.idx = idx;
561 reg.negatebase = 0;
562 reg.swz = SWIZZLE_NOOP;
563 reg.pad = 0;
564 return reg;
565 }
566
567 static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
568 {
569 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
570 GET_SWZ(reg.swz, y),
571 GET_SWZ(reg.swz, z),
572 GET_SWZ(reg.swz, w));
573
574 return reg;
575 }
576
577 static struct ureg swizzle1( struct ureg reg, int x )
578 {
579 return swizzle(reg, x, x, x, x);
580 }
581
582 static struct ureg negate( struct ureg reg )
583 {
584 reg.negatebase ^= 1;
585 return reg;
586 }
587
588 static GLboolean is_undef( struct ureg reg )
589 {
590 return reg.file == PROGRAM_UNDEFINED;
591 }
592
593
594 static struct ureg get_temp( struct texenv_fragment_program *p )
595 {
596 GLint bit;
597
598 /* First try and reuse temps which have been used already:
599 */
600 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
601
602 /* Then any unused temporary:
603 */
604 if (!bit)
605 bit = _mesa_ffs( ~p->temp_in_use );
606
607 if (!bit) {
608 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
609 _mesa_exit(1);
610 }
611
612 if ((GLuint) bit > p->program->Base.NumTemporaries)
613 p->program->Base.NumTemporaries = bit;
614
615 p->temp_in_use |= 1<<(bit-1);
616 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
617 }
618
619 static struct ureg get_tex_temp( struct texenv_fragment_program *p )
620 {
621 int bit;
622
623 /* First try to find available temp not previously used (to avoid
624 * starting a new texture indirection). According to the spec, the
625 * ~p->temps_output isn't necessary, but will keep it there for
626 * now:
627 */
628 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
629
630 /* Then any unused temporary:
631 */
632 if (!bit)
633 bit = _mesa_ffs( ~p->temp_in_use );
634
635 if (!bit) {
636 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
637 _mesa_exit(1);
638 }
639
640 if ((GLuint) bit > p->program->Base.NumTemporaries)
641 p->program->Base.NumTemporaries = bit;
642
643 p->temp_in_use |= 1<<(bit-1);
644 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
645 }
646
647
648 /** Mark a temp reg as being no longer allocatable. */
649 static void reserve_temp( struct texenv_fragment_program *p, struct ureg r )
650 {
651 if (r.file == PROGRAM_TEMPORARY)
652 p->temps_output |= (1 << r.idx);
653 }
654
655
656 static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
657 {
658 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
659
660 /* KW: To support tex_env_crossbar, don't release the registers in
661 * temps_output.
662 */
663 if (max_temp >= sizeof(int) * 8)
664 p->temp_in_use = p->temps_output;
665 else
666 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
667 }
668
669
670 static struct ureg register_param5( struct texenv_fragment_program *p,
671 GLint s0,
672 GLint s1,
673 GLint s2,
674 GLint s3,
675 GLint s4)
676 {
677 gl_state_index tokens[STATE_LENGTH];
678 GLuint idx;
679 tokens[0] = s0;
680 tokens[1] = s1;
681 tokens[2] = s2;
682 tokens[3] = s3;
683 tokens[4] = s4;
684 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
685 return make_ureg(PROGRAM_STATE_VAR, idx);
686 }
687
688
689 #define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
690 #define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
691 #define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
692 #define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
693
694 static GLuint frag_to_vert_attrib( GLuint attrib )
695 {
696 switch (attrib) {
697 case FRAG_ATTRIB_COL0: return VERT_ATTRIB_COLOR0;
698 case FRAG_ATTRIB_COL1: return VERT_ATTRIB_COLOR1;
699 default:
700 assert(attrib >= FRAG_ATTRIB_TEX0);
701 assert(attrib <= FRAG_ATTRIB_TEX7);
702 return attrib - FRAG_ATTRIB_TEX0 + VERT_ATTRIB_TEX0;
703 }
704 }
705
706
707 static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
708 {
709 if (p->state->inputs_available & (1<<input)) {
710 p->program->Base.InputsRead |= (1 << input);
711 return make_ureg(PROGRAM_INPUT, input);
712 }
713 else {
714 GLuint idx = frag_to_vert_attrib( input );
715 return register_param3( p, STATE_INTERNAL, STATE_CURRENT_ATTRIB, idx );
716 }
717 }
718
719
720 static void emit_arg( struct prog_src_register *reg,
721 struct ureg ureg )
722 {
723 reg->File = ureg.file;
724 reg->Index = ureg.idx;
725 reg->Swizzle = ureg.swz;
726 reg->Negate = ureg.negatebase ? NEGATE_XYZW : NEGATE_NONE;
727 reg->Abs = GL_FALSE;
728 }
729
730 static void emit_dst( struct prog_dst_register *dst,
731 struct ureg ureg, GLuint mask )
732 {
733 dst->File = ureg.file;
734 dst->Index = ureg.idx;
735 dst->WriteMask = mask;
736 dst->CondMask = COND_TR; /* always pass cond test */
737 dst->CondSwizzle = SWIZZLE_NOOP;
738 }
739
740 static struct prog_instruction *
741 emit_op(struct texenv_fragment_program *p,
742 enum prog_opcode op,
743 struct ureg dest,
744 GLuint mask,
745 GLboolean saturate,
746 struct ureg src0,
747 struct ureg src1,
748 struct ureg src2 )
749 {
750 const GLuint nr = p->program->Base.NumInstructions++;
751 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
752
753 assert(nr < MAX_INSTRUCTIONS);
754
755 _mesa_init_instructions(inst, 1);
756 inst->Opcode = op;
757
758 emit_arg( &inst->SrcReg[0], src0 );
759 emit_arg( &inst->SrcReg[1], src1 );
760 emit_arg( &inst->SrcReg[2], src2 );
761
762 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
763
764 emit_dst( &inst->DstReg, dest, mask );
765
766 #if 0
767 /* Accounting for indirection tracking:
768 */
769 if (dest.file == PROGRAM_TEMPORARY)
770 p->temps_output |= 1 << dest.idx;
771 #endif
772
773 return inst;
774 }
775
776
777 static struct ureg emit_arith( struct texenv_fragment_program *p,
778 enum prog_opcode op,
779 struct ureg dest,
780 GLuint mask,
781 GLboolean saturate,
782 struct ureg src0,
783 struct ureg src1,
784 struct ureg src2 )
785 {
786 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
787
788 /* Accounting for indirection tracking:
789 */
790 if (src0.file == PROGRAM_TEMPORARY)
791 p->alu_temps |= 1 << src0.idx;
792
793 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
794 p->alu_temps |= 1 << src1.idx;
795
796 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
797 p->alu_temps |= 1 << src2.idx;
798
799 if (dest.file == PROGRAM_TEMPORARY)
800 p->alu_temps |= 1 << dest.idx;
801
802 p->program->Base.NumAluInstructions++;
803 return dest;
804 }
805
806 static struct ureg emit_texld( struct texenv_fragment_program *p,
807 enum prog_opcode op,
808 struct ureg dest,
809 GLuint destmask,
810 GLuint tex_unit,
811 GLuint tex_idx,
812 GLuint tex_shadow,
813 struct ureg coord )
814 {
815 struct prog_instruction *inst = emit_op( p, op,
816 dest, destmask,
817 GL_FALSE, /* don't saturate? */
818 coord, /* arg 0? */
819 undef,
820 undef);
821
822 inst->TexSrcTarget = tex_idx;
823 inst->TexSrcUnit = tex_unit;
824 inst->TexShadow = tex_shadow;
825
826 p->program->Base.NumTexInstructions++;
827
828 /* Accounting for indirection tracking:
829 */
830 reserve_temp(p, dest);
831
832 #if 0
833 /* Is this a texture indirection?
834 */
835 if ((coord.file == PROGRAM_TEMPORARY &&
836 (p->temps_output & (1<<coord.idx))) ||
837 (dest.file == PROGRAM_TEMPORARY &&
838 (p->alu_temps & (1<<dest.idx)))) {
839 p->program->Base.NumTexIndirections++;
840 p->temps_output = 1<<coord.idx;
841 p->alu_temps = 0;
842 assert(0); /* KW: texture env crossbar */
843 }
844 #endif
845
846 return dest;
847 }
848
849
850 static struct ureg register_const4f( struct texenv_fragment_program *p,
851 GLfloat s0,
852 GLfloat s1,
853 GLfloat s2,
854 GLfloat s3)
855 {
856 GLfloat values[4];
857 GLuint idx, swizzle;
858 struct ureg r;
859 values[0] = s0;
860 values[1] = s1;
861 values[2] = s2;
862 values[3] = s3;
863 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
864 &swizzle );
865 r = make_ureg(PROGRAM_CONSTANT, idx);
866 r.swz = swizzle;
867 return r;
868 }
869
870 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
871 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
872 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
873 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
874
875
876 static struct ureg get_one( struct texenv_fragment_program *p )
877 {
878 if (is_undef(p->one))
879 p->one = register_scalar_const(p, 1.0);
880 return p->one;
881 }
882
883 static struct ureg get_half( struct texenv_fragment_program *p )
884 {
885 if (is_undef(p->half))
886 p->half = register_scalar_const(p, 0.5);
887 return p->half;
888 }
889
890 static struct ureg get_zero( struct texenv_fragment_program *p )
891 {
892 if (is_undef(p->zero))
893 p->zero = register_scalar_const(p, 0.0);
894 return p->zero;
895 }
896
897
898 static void program_error( struct texenv_fragment_program *p, const char *msg )
899 {
900 _mesa_problem(NULL, msg);
901 p->error = 1;
902 }
903
904 static struct ureg get_source( struct texenv_fragment_program *p,
905 GLuint src, GLuint unit )
906 {
907 switch (src) {
908 case SRC_TEXTURE:
909 assert(!is_undef(p->src_texture[unit]));
910 return p->src_texture[unit];
911
912 case SRC_TEXTURE0:
913 case SRC_TEXTURE1:
914 case SRC_TEXTURE2:
915 case SRC_TEXTURE3:
916 case SRC_TEXTURE4:
917 case SRC_TEXTURE5:
918 case SRC_TEXTURE6:
919 case SRC_TEXTURE7:
920 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
921 return p->src_texture[src - SRC_TEXTURE0];
922
923 case SRC_CONSTANT:
924 return register_param2(p, STATE_TEXENV_COLOR, unit);
925
926 case SRC_PRIMARY_COLOR:
927 return register_input(p, FRAG_ATTRIB_COL0);
928
929 case SRC_ZERO:
930 return get_zero(p);
931
932 case SRC_PREVIOUS:
933 if (is_undef(p->src_previous))
934 return register_input(p, FRAG_ATTRIB_COL0);
935 else
936 return p->src_previous;
937
938 default:
939 assert(0);
940 return undef;
941 }
942 }
943
944 static struct ureg emit_combine_source( struct texenv_fragment_program *p,
945 GLuint mask,
946 GLuint unit,
947 GLuint source,
948 GLuint operand )
949 {
950 struct ureg arg, src, one;
951
952 src = get_source(p, source, unit);
953
954 switch (operand) {
955 case OPR_ONE_MINUS_SRC_COLOR:
956 /* Get unused tmp,
957 * Emit tmp = 1.0 - arg.xyzw
958 */
959 arg = get_temp( p );
960 one = get_one( p );
961 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
962
963 case OPR_SRC_ALPHA:
964 if (mask == WRITEMASK_W)
965 return src;
966 else
967 return swizzle1( src, SWIZZLE_W );
968 case OPR_ONE_MINUS_SRC_ALPHA:
969 /* Get unused tmp,
970 * Emit tmp = 1.0 - arg.wwww
971 */
972 arg = get_temp(p);
973 one = get_one(p);
974 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
975 one, swizzle1(src, SWIZZLE_W), undef);
976 case OPR_ZERO:
977 return get_zero(p);
978 case OPR_ONE:
979 return get_one(p);
980 case OPR_SRC_COLOR:
981 return src;
982 default:
983 assert(0);
984 return src;
985 }
986 }
987
988 /**
989 * Check if the RGB and Alpha sources and operands match for the given
990 * texture unit's combinder state. When the RGB and A sources and
991 * operands match, we can emit fewer instructions.
992 */
993 static GLboolean args_match( const struct state_key *key, GLuint unit )
994 {
995 GLuint i, numArgs = key->unit[unit].NumArgsRGB;
996
997 for (i = 0; i < numArgs; i++) {
998 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
999 return GL_FALSE;
1000
1001 switch (key->unit[unit].OptA[i].Operand) {
1002 case OPR_SRC_ALPHA:
1003 switch (key->unit[unit].OptRGB[i].Operand) {
1004 case OPR_SRC_COLOR:
1005 case OPR_SRC_ALPHA:
1006 break;
1007 default:
1008 return GL_FALSE;
1009 }
1010 break;
1011 case OPR_ONE_MINUS_SRC_ALPHA:
1012 switch (key->unit[unit].OptRGB[i].Operand) {
1013 case OPR_ONE_MINUS_SRC_COLOR:
1014 case OPR_ONE_MINUS_SRC_ALPHA:
1015 break;
1016 default:
1017 return GL_FALSE;
1018 }
1019 break;
1020 default:
1021 return GL_FALSE; /* impossible */
1022 }
1023 }
1024
1025 return GL_TRUE;
1026 }
1027
1028 static struct ureg emit_combine( struct texenv_fragment_program *p,
1029 struct ureg dest,
1030 GLuint mask,
1031 GLboolean saturate,
1032 GLuint unit,
1033 GLuint nr,
1034 GLuint mode,
1035 const struct mode_opt *opt)
1036 {
1037 struct ureg src[MAX_COMBINER_TERMS];
1038 struct ureg tmp, half;
1039 GLuint i;
1040
1041 assert(nr <= MAX_COMBINER_TERMS);
1042
1043 tmp = undef; /* silence warning (bug 5318) */
1044
1045 for (i = 0; i < nr; i++)
1046 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
1047
1048 switch (mode) {
1049 case MODE_REPLACE:
1050 if (mask == WRITEMASK_XYZW && !saturate)
1051 return src[0];
1052 else
1053 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
1054 case MODE_MODULATE:
1055 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
1056 src[0], src[1], undef );
1057 case MODE_ADD:
1058 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
1059 src[0], src[1], undef );
1060 case MODE_ADD_SIGNED:
1061 /* tmp = arg0 + arg1
1062 * result = tmp - .5
1063 */
1064 half = get_half(p);
1065 tmp = get_temp( p );
1066 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
1067 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
1068 return dest;
1069 case MODE_INTERPOLATE:
1070 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
1071 */
1072 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
1073
1074 case MODE_SUBTRACT:
1075 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
1076
1077 case MODE_DOT3_RGBA:
1078 case MODE_DOT3_RGBA_EXT:
1079 case MODE_DOT3_RGB_EXT:
1080 case MODE_DOT3_RGB: {
1081 struct ureg tmp0 = get_temp( p );
1082 struct ureg tmp1 = get_temp( p );
1083 struct ureg neg1 = register_scalar_const(p, -1);
1084 struct ureg two = register_scalar_const(p, 2);
1085
1086 /* tmp0 = 2*src0 - 1
1087 * tmp1 = 2*src1 - 1
1088 *
1089 * dst = tmp0 dot3 tmp1
1090 */
1091 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
1092 two, src[0], neg1);
1093
1094 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
1095 tmp1 = tmp0;
1096 else
1097 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
1098 two, src[1], neg1);
1099 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
1100 return dest;
1101 }
1102 case MODE_MODULATE_ADD_ATI:
1103 /* Arg0 * Arg2 + Arg1 */
1104 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
1105 src[0], src[2], src[1] );
1106 case MODE_MODULATE_SIGNED_ADD_ATI: {
1107 /* Arg0 * Arg2 + Arg1 - 0.5 */
1108 struct ureg tmp0 = get_temp(p);
1109 half = get_half(p);
1110 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
1111 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1112 return dest;
1113 }
1114 case MODE_MODULATE_SUBTRACT_ATI:
1115 /* Arg0 * Arg2 - Arg1 */
1116 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
1117 return dest;
1118 case MODE_ADD_PRODUCTS:
1119 /* Arg0 * Arg1 + Arg2 * Arg3 */
1120 {
1121 struct ureg tmp0 = get_temp(p);
1122 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1123 emit_arith( p, OPCODE_MAD, dest, mask, saturate, src[2], src[3], tmp0 );
1124 }
1125 return dest;
1126 case MODE_ADD_PRODUCTS_SIGNED:
1127 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1128 {
1129 struct ureg tmp0 = get_temp(p);
1130 half = get_half(p);
1131 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1132 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[2], src[3], tmp0 );
1133 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1134 }
1135 return dest;
1136 case MODE_BUMP_ENVMAP_ATI:
1137 /* special - not handled here */
1138 assert(0);
1139 return src[0];
1140 default:
1141 assert(0);
1142 return src[0];
1143 }
1144 }
1145
1146
1147 /**
1148 * Generate instructions for one texture unit's env/combiner mode.
1149 */
1150 static struct ureg
1151 emit_texenv(struct texenv_fragment_program *p, GLuint unit)
1152 {
1153 const struct state_key *key = p->state;
1154 GLboolean rgb_saturate, alpha_saturate;
1155 GLuint rgb_shift, alpha_shift;
1156 struct ureg out, dest;
1157
1158 if (!key->unit[unit].enabled) {
1159 return get_source(p, SRC_PREVIOUS, 0);
1160 }
1161 if (key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1162 /* this isn't really a env stage delivering a color and handled elsewhere */
1163 return get_source(p, SRC_PREVIOUS, 0);
1164 }
1165
1166 switch (key->unit[unit].ModeRGB) {
1167 case MODE_DOT3_RGB_EXT:
1168 alpha_shift = key->unit[unit].ScaleShiftA;
1169 rgb_shift = 0;
1170 break;
1171 case MODE_DOT3_RGBA_EXT:
1172 alpha_shift = 0;
1173 rgb_shift = 0;
1174 break;
1175 default:
1176 rgb_shift = key->unit[unit].ScaleShiftRGB;
1177 alpha_shift = key->unit[unit].ScaleShiftA;
1178 break;
1179 }
1180
1181 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
1182 * We don't want to clamp twice.
1183 */
1184 if (rgb_shift)
1185 rgb_saturate = GL_FALSE; /* saturate after rgb shift */
1186 else if (need_saturate(key->unit[unit].ModeRGB))
1187 rgb_saturate = GL_TRUE;
1188 else
1189 rgb_saturate = GL_FALSE;
1190
1191 if (alpha_shift)
1192 alpha_saturate = GL_FALSE; /* saturate after alpha shift */
1193 else if (need_saturate(key->unit[unit].ModeA))
1194 alpha_saturate = GL_TRUE;
1195 else
1196 alpha_saturate = GL_FALSE;
1197
1198 /* If this is the very last calculation, emit direct to output reg:
1199 */
1200 if (key->separate_specular ||
1201 unit != p->last_tex_stage ||
1202 alpha_shift ||
1203 rgb_shift)
1204 dest = get_temp( p );
1205 else
1206 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLOR);
1207
1208 /* Emit the RGB and A combine ops
1209 */
1210 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
1211 args_match(key, unit)) {
1212 out = emit_combine( p, dest, WRITEMASK_XYZW, rgb_saturate,
1213 unit,
1214 key->unit[unit].NumArgsRGB,
1215 key->unit[unit].ModeRGB,
1216 key->unit[unit].OptRGB);
1217 }
1218 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
1219 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
1220 out = emit_combine( p, dest, WRITEMASK_XYZW, rgb_saturate,
1221 unit,
1222 key->unit[unit].NumArgsRGB,
1223 key->unit[unit].ModeRGB,
1224 key->unit[unit].OptRGB);
1225 }
1226 else {
1227 /* Need to do something to stop from re-emitting identical
1228 * argument calculations here:
1229 */
1230 out = emit_combine( p, dest, WRITEMASK_XYZ, rgb_saturate,
1231 unit,
1232 key->unit[unit].NumArgsRGB,
1233 key->unit[unit].ModeRGB,
1234 key->unit[unit].OptRGB);
1235 out = emit_combine( p, dest, WRITEMASK_W, alpha_saturate,
1236 unit,
1237 key->unit[unit].NumArgsA,
1238 key->unit[unit].ModeA,
1239 key->unit[unit].OptA);
1240 }
1241
1242 /* Deal with the final shift:
1243 */
1244 if (alpha_shift || rgb_shift) {
1245 struct ureg shift;
1246 GLboolean saturate = GL_TRUE; /* always saturate at this point */
1247
1248 if (rgb_shift == alpha_shift) {
1249 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
1250 }
1251 else {
1252 shift = register_const4f(p,
1253 (GLfloat)(1<<rgb_shift),
1254 (GLfloat)(1<<rgb_shift),
1255 (GLfloat)(1<<rgb_shift),
1256 (GLfloat)(1<<alpha_shift));
1257 }
1258 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
1259 saturate, out, shift, undef );
1260 }
1261 else
1262 return out;
1263 }
1264
1265
1266 /**
1267 * Generate instruction for getting a texture source term.
1268 */
1269 static void load_texture( struct texenv_fragment_program *p, GLuint unit )
1270 {
1271 if (is_undef(p->src_texture[unit])) {
1272 const GLuint texTarget = p->state->unit[unit].source_index;
1273 struct ureg texcoord;
1274 struct ureg tmp = get_tex_temp( p );
1275
1276 if (is_undef(p->texcoord_tex[unit])) {
1277 texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
1278 }
1279 else {
1280 /* might want to reuse this reg for tex output actually */
1281 texcoord = p->texcoord_tex[unit];
1282 }
1283
1284 /* TODO: Use D0_MASK_XY where possible.
1285 */
1286 if (p->state->unit[unit].enabled) {
1287 GLboolean shadow = GL_FALSE;
1288
1289 if (p->state->unit[unit].shadow) {
1290 p->program->Base.ShadowSamplers |= 1 << unit;
1291 shadow = GL_TRUE;
1292 }
1293
1294 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1295 tmp, WRITEMASK_XYZW,
1296 unit, texTarget, shadow,
1297 texcoord );
1298
1299 p->program->Base.SamplersUsed |= (1 << unit);
1300 /* This identity mapping should already be in place
1301 * (see _mesa_init_program_struct()) but let's be safe.
1302 */
1303 p->program->Base.SamplerUnits[unit] = unit;
1304 }
1305 else
1306 p->src_texture[unit] = get_zero(p);
1307 }
1308 }
1309
1310 static GLboolean load_texenv_source( struct texenv_fragment_program *p,
1311 GLuint src, GLuint unit )
1312 {
1313 switch (src) {
1314 case SRC_TEXTURE:
1315 load_texture(p, unit);
1316 break;
1317
1318 case SRC_TEXTURE0:
1319 case SRC_TEXTURE1:
1320 case SRC_TEXTURE2:
1321 case SRC_TEXTURE3:
1322 case SRC_TEXTURE4:
1323 case SRC_TEXTURE5:
1324 case SRC_TEXTURE6:
1325 case SRC_TEXTURE7:
1326 load_texture(p, src - SRC_TEXTURE0);
1327 break;
1328
1329 default:
1330 /* not a texture src - do nothing */
1331 break;
1332 }
1333
1334 return GL_TRUE;
1335 }
1336
1337
1338 /**
1339 * Generate instructions for loading all texture source terms.
1340 */
1341 static GLboolean
1342 load_texunit_sources( struct texenv_fragment_program *p, GLuint unit )
1343 {
1344 const struct state_key *key = p->state;
1345 GLuint i;
1346
1347 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
1348 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit );
1349 }
1350
1351 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1352 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1353 }
1354
1355 return GL_TRUE;
1356 }
1357
1358 /**
1359 * Generate instructions for loading bump map textures.
1360 */
1361 static GLboolean
1362 load_texunit_bumpmap( struct texenv_fragment_program *p, GLuint unit )
1363 {
1364 const struct state_key *key = p->state;
1365 GLuint bumpedUnitNr = key->unit[unit].OptRGB[1].Source - SRC_TEXTURE0;
1366 struct ureg texcDst, bumpMapRes;
1367 struct ureg constdudvcolor = register_const4f(p, 0.0, 0.0, 0.0, 1.0);
1368 struct ureg texcSrc = register_input(p, FRAG_ATTRIB_TEX0 + bumpedUnitNr);
1369 struct ureg rotMat0 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_0, unit );
1370 struct ureg rotMat1 = register_param3( p, STATE_INTERNAL, STATE_ROT_MATRIX_1, unit );
1371
1372 load_texenv_source( p, unit + SRC_TEXTURE0, unit );
1373
1374 bumpMapRes = get_source(p, key->unit[unit].OptRGB[0].Source, unit);
1375 texcDst = get_tex_temp( p );
1376 p->texcoord_tex[bumpedUnitNr] = texcDst;
1377
1378 /* Apply rot matrix and add coords to be available in next phase.
1379 * dest = (Arg0.xxxx * rotMat0 + Arg1) + (Arg0.yyyy * rotMat1)
1380 * note only 2 coords are affected the rest are left unchanged (mul by 0)
1381 */
1382 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1383 swizzle1(bumpMapRes, SWIZZLE_X), rotMat0, texcSrc );
1384 emit_arith( p, OPCODE_MAD, texcDst, WRITEMASK_XYZW, 0,
1385 swizzle1(bumpMapRes, SWIZZLE_Y), rotMat1, texcDst );
1386
1387 /* Move 0,0,0,1 into bumpmap src if someone (crossbar) is foolish
1388 * enough to access this later, should optimize away.
1389 */
1390 emit_arith( p, OPCODE_MOV, bumpMapRes, WRITEMASK_XYZW, 0,
1391 constdudvcolor, undef, undef );
1392
1393 return GL_TRUE;
1394 }
1395
1396 /**
1397 * Generate a new fragment program which implements the context's
1398 * current texture env/combine mode.
1399 */
1400 static void
1401 create_new_program(GLcontext *ctx, struct state_key *key,
1402 struct gl_fragment_program *program)
1403 {
1404 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
1405 struct texenv_fragment_program p;
1406 GLuint unit;
1407 struct ureg cf, out;
1408
1409 _mesa_memset(&p, 0, sizeof(p));
1410 p.state = key;
1411 p.program = program;
1412
1413 /* During code generation, use locally-allocated instruction buffer,
1414 * then alloc dynamic storage below.
1415 */
1416 p.program->Base.Instructions = instBuffer;
1417 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
1418 p.program->Base.String = NULL;
1419 p.program->Base.NumTexIndirections = 1; /* is this right? */
1420 p.program->Base.NumTexInstructions = 0;
1421 p.program->Base.NumAluInstructions = 0;
1422 p.program->Base.NumInstructions = 0;
1423 p.program->Base.NumTemporaries = 0;
1424 p.program->Base.NumParameters = 0;
1425 p.program->Base.NumAttributes = 0;
1426 p.program->Base.NumAddressRegs = 0;
1427 p.program->Base.Parameters = _mesa_new_parameter_list();
1428 p.program->Base.InputsRead = 0x0;
1429 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLOR;
1430
1431 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
1432 p.src_texture[unit] = undef;
1433 p.texcoord_tex[unit] = undef;
1434 }
1435
1436 p.src_previous = undef;
1437 p.half = undef;
1438 p.zero = undef;
1439 p.one = undef;
1440
1441 p.last_tex_stage = 0;
1442 release_temps(ctx, &p);
1443
1444 if (key->enabled_units) {
1445 GLboolean needbumpstage = GL_FALSE;
1446
1447 /* Zeroth pass - bump map textures first */
1448 for (unit = 0; unit < key->nr_enabled_units; unit++)
1449 if (key->unit[unit].enabled &&
1450 key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
1451 needbumpstage = GL_TRUE;
1452 load_texunit_bumpmap( &p, unit );
1453 }
1454 if (needbumpstage)
1455 p.program->Base.NumTexIndirections++;
1456
1457 /* First pass - to support texture_env_crossbar, first identify
1458 * all referenced texture sources and emit texld instructions
1459 * for each:
1460 */
1461 for (unit = 0; unit < key->nr_enabled_units; unit++)
1462 if (key->unit[unit].enabled) {
1463 load_texunit_sources( &p, unit );
1464 p.last_tex_stage = unit;
1465 }
1466
1467 /* Second pass - emit combine instructions to build final color:
1468 */
1469 for (unit = 0; unit < key->nr_enabled_units; unit++)
1470 if (key->unit[unit].enabled) {
1471 p.src_previous = emit_texenv( &p, unit );
1472 reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
1473 release_temps(ctx, &p); /* release all temps */
1474 }
1475 }
1476
1477 cf = get_source( &p, SRC_PREVIOUS, 0 );
1478 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLOR );
1479
1480 if (key->separate_specular) {
1481 /* Emit specular add.
1482 */
1483 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
1484 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1485 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
1486 }
1487 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
1488 /* Will wind up in here if no texture enabled or a couple of
1489 * other scenarios (GL_REPLACE for instance).
1490 */
1491 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
1492 }
1493
1494 /* Finish up:
1495 */
1496 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
1497
1498 if (key->fog_enabled) {
1499 /* Pull fog mode from GLcontext, the value in the state key is
1500 * a reduced value and not what is expected in FogOption
1501 */
1502 p.program->FogOption = ctx->Fog.Mode;
1503 p.program->Base.InputsRead |= FRAG_BIT_FOGC;
1504 }
1505 else {
1506 p.program->FogOption = GL_NONE;
1507 }
1508
1509 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
1510 program_error(&p, "Exceeded max nr indirect texture lookups");
1511
1512 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
1513 program_error(&p, "Exceeded max TEX instructions");
1514
1515 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
1516 program_error(&p, "Exceeded max ALU instructions");
1517
1518 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
1519
1520 /* Allocate final instruction array */
1521 p.program->Base.Instructions
1522 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1523 if (!p.program->Base.Instructions) {
1524 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1525 "generating tex env program");
1526 return;
1527 }
1528 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1529 p.program->Base.NumInstructions);
1530
1531 if (p.program->FogOption) {
1532 _mesa_append_fog_code(ctx, p.program);
1533 p.program->FogOption = GL_NONE;
1534 }
1535
1536
1537 /* Notify driver the fragment program has (actually) changed.
1538 */
1539 if (ctx->Driver.ProgramStringNotify) {
1540 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1541 &p.program->Base );
1542 }
1543
1544 if (DISASSEM) {
1545 _mesa_print_program(&p.program->Base);
1546 _mesa_printf("\n");
1547 }
1548 }
1549
1550
1551 /**
1552 * Return a fragment program which implements the current
1553 * fixed-function texture, fog and color-sum operations.
1554 */
1555 struct gl_fragment_program *
1556 _mesa_get_fixed_func_fragment_program(GLcontext *ctx)
1557 {
1558 struct gl_fragment_program *prog;
1559 struct state_key key;
1560 GLuint keySize;
1561
1562 keySize = make_state_key(ctx, &key);
1563
1564 prog = (struct gl_fragment_program *)
1565 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1566 &key, keySize);
1567
1568 if (!prog) {
1569 prog = (struct gl_fragment_program *)
1570 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1571
1572 create_new_program(ctx, &key, prog);
1573
1574 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1575 &key, keySize, &prog->Base);
1576 }
1577
1578 return prog;
1579 }