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