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