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