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