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