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