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