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