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