Merge branch 'master' into gallium-texture-transfer
[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 struct ureg coord )
740 {
741 struct prog_instruction *inst = emit_op( p, op,
742 dest, destmask,
743 GL_FALSE, /* don't saturate? */
744 coord, /* arg 0? */
745 undef,
746 undef);
747
748 inst->TexSrcTarget = tex_idx;
749 inst->TexSrcUnit = tex_unit;
750
751 p->program->Base.NumTexInstructions++;
752
753 /* Accounting for indirection tracking:
754 */
755 reserve_temp(p, dest);
756
757 /* Is this a texture indirection?
758 */
759 if ((coord.file == PROGRAM_TEMPORARY &&
760 (p->temps_output & (1<<coord.idx))) ||
761 (dest.file == PROGRAM_TEMPORARY &&
762 (p->alu_temps & (1<<dest.idx)))) {
763 p->program->Base.NumTexIndirections++;
764 p->temps_output = 1<<coord.idx;
765 p->alu_temps = 0;
766 assert(0); /* KW: texture env crossbar */
767 }
768
769 return dest;
770 }
771
772
773 static struct ureg register_const4f( struct texenv_fragment_program *p,
774 GLfloat s0,
775 GLfloat s1,
776 GLfloat s2,
777 GLfloat s3)
778 {
779 GLfloat values[4];
780 GLuint idx, swizzle;
781 struct ureg r;
782 values[0] = s0;
783 values[1] = s1;
784 values[2] = s2;
785 values[3] = s3;
786 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
787 &swizzle );
788 r = make_ureg(PROGRAM_CONSTANT, idx);
789 r.swz = swizzle;
790 return r;
791 }
792
793 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
794 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
795 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
796 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
797
798
799 static struct ureg get_one( struct texenv_fragment_program *p )
800 {
801 if (is_undef(p->one))
802 p->one = register_scalar_const(p, 1.0);
803 return p->one;
804 }
805
806 static struct ureg get_half( struct texenv_fragment_program *p )
807 {
808 if (is_undef(p->half))
809 p->half = register_scalar_const(p, 0.5);
810 return p->half;
811 }
812
813 static struct ureg get_zero( struct texenv_fragment_program *p )
814 {
815 if (is_undef(p->zero))
816 p->zero = register_scalar_const(p, 0.0);
817 return p->zero;
818 }
819
820
821 static void program_error( struct texenv_fragment_program *p, const char *msg )
822 {
823 _mesa_problem(NULL, msg);
824 p->error = 1;
825 }
826
827 static struct ureg get_source( struct texenv_fragment_program *p,
828 GLuint src, GLuint unit )
829 {
830 switch (src) {
831 case SRC_TEXTURE:
832 assert(!is_undef(p->src_texture[unit]));
833 return p->src_texture[unit];
834
835 case SRC_TEXTURE0:
836 case SRC_TEXTURE1:
837 case SRC_TEXTURE2:
838 case SRC_TEXTURE3:
839 case SRC_TEXTURE4:
840 case SRC_TEXTURE5:
841 case SRC_TEXTURE6:
842 case SRC_TEXTURE7:
843 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
844 return p->src_texture[src - SRC_TEXTURE0];
845
846 case SRC_CONSTANT:
847 return register_param2(p, STATE_TEXENV_COLOR, unit);
848
849 case SRC_PRIMARY_COLOR:
850 return register_input(p, FRAG_ATTRIB_COL0);
851
852 case SRC_ZERO:
853 return get_zero(p);
854
855 case SRC_PREVIOUS:
856 if (is_undef(p->src_previous))
857 return register_input(p, FRAG_ATTRIB_COL0);
858 else
859 return p->src_previous;
860
861 default:
862 assert(0);
863 }
864 }
865
866 static struct ureg emit_combine_source( struct texenv_fragment_program *p,
867 GLuint mask,
868 GLuint unit,
869 GLuint source,
870 GLuint operand )
871 {
872 struct ureg arg, src, one;
873
874 src = get_source(p, source, unit);
875
876 switch (operand) {
877 case OPR_ONE_MINUS_SRC_COLOR:
878 /* Get unused tmp,
879 * Emit tmp = 1.0 - arg.xyzw
880 */
881 arg = get_temp( p );
882 one = get_one( p );
883 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
884
885 case OPR_SRC_ALPHA:
886 if (mask == WRITEMASK_W)
887 return src;
888 else
889 return swizzle1( src, SWIZZLE_W );
890 case OPR_ONE_MINUS_SRC_ALPHA:
891 /* Get unused tmp,
892 * Emit tmp = 1.0 - arg.wwww
893 */
894 arg = get_temp(p);
895 one = get_one(p);
896 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
897 one, swizzle1(src, SWIZZLE_W), undef);
898 case OPR_ZERO:
899 return get_zero(p);
900 case OPR_ONE:
901 return get_one(p);
902 case OPR_SRC_COLOR:
903 return src;
904 default:
905 assert(0);
906 return src;
907 }
908 }
909
910 static GLboolean args_match( struct state_key *key, GLuint unit )
911 {
912 GLuint i, nr = key->unit[unit].NumArgsRGB;
913
914 for (i = 0 ; i < nr ; i++) {
915 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
916 return GL_FALSE;
917
918 switch(key->unit[unit].OptA[i].Operand) {
919 case OPR_SRC_ALPHA:
920 switch(key->unit[unit].OptRGB[i].Operand) {
921 case OPR_SRC_COLOR:
922 case OPR_SRC_ALPHA:
923 break;
924 default:
925 return GL_FALSE;
926 }
927 break;
928 case OPR_ONE_MINUS_SRC_ALPHA:
929 switch(key->unit[unit].OptRGB[i].Operand) {
930 case OPR_ONE_MINUS_SRC_COLOR:
931 case OPR_ONE_MINUS_SRC_ALPHA:
932 break;
933 default:
934 return GL_FALSE;
935 }
936 break;
937 default:
938 return GL_FALSE; /* impossible */
939 }
940 }
941
942 return GL_TRUE;
943 }
944
945 static struct ureg emit_combine( struct texenv_fragment_program *p,
946 struct ureg dest,
947 GLuint mask,
948 GLboolean saturate,
949 GLuint unit,
950 GLuint nr,
951 GLuint mode,
952 const struct mode_opt *opt)
953 {
954 struct ureg src[MAX_TERMS];
955 struct ureg tmp, half;
956 GLuint i;
957
958 assert(nr <= MAX_TERMS);
959
960 tmp = undef; /* silence warning (bug 5318) */
961
962 for (i = 0; i < nr; i++)
963 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
964
965 switch (mode) {
966 case MODE_REPLACE:
967 if (mask == WRITEMASK_XYZW && !saturate)
968 return src[0];
969 else
970 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
971 case MODE_MODULATE:
972 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
973 src[0], src[1], undef );
974 case MODE_ADD:
975 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
976 src[0], src[1], undef );
977 case MODE_ADD_SIGNED:
978 /* tmp = arg0 + arg1
979 * result = tmp - .5
980 */
981 half = get_half(p);
982 tmp = get_temp( p );
983 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
984 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
985 return dest;
986 case MODE_INTERPOLATE:
987 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
988 */
989 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
990
991 case MODE_SUBTRACT:
992 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
993
994 case MODE_DOT3_RGBA:
995 case MODE_DOT3_RGBA_EXT:
996 case MODE_DOT3_RGB_EXT:
997 case MODE_DOT3_RGB: {
998 struct ureg tmp0 = get_temp( p );
999 struct ureg tmp1 = get_temp( p );
1000 struct ureg neg1 = register_scalar_const(p, -1);
1001 struct ureg two = register_scalar_const(p, 2);
1002
1003 /* tmp0 = 2*src0 - 1
1004 * tmp1 = 2*src1 - 1
1005 *
1006 * dst = tmp0 dot3 tmp1
1007 */
1008 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
1009 two, src[0], neg1);
1010
1011 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
1012 tmp1 = tmp0;
1013 else
1014 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
1015 two, src[1], neg1);
1016 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
1017 return dest;
1018 }
1019 case MODE_MODULATE_ADD_ATI:
1020 /* Arg0 * Arg2 + Arg1 */
1021 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
1022 src[0], src[2], src[1] );
1023 case MODE_MODULATE_SIGNED_ADD_ATI: {
1024 /* Arg0 * Arg2 + Arg1 - 0.5 */
1025 struct ureg tmp0 = get_temp(p);
1026 half = get_half(p);
1027 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
1028 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1029 return dest;
1030 }
1031 case MODE_MODULATE_SUBTRACT_ATI:
1032 /* Arg0 * Arg2 - Arg1 */
1033 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
1034 return dest;
1035 case MODE_ADD_PRODUCTS:
1036 /* Arg0 * Arg1 + Arg2 * Arg3 */
1037 {
1038 struct ureg tmp0 = get_temp(p);
1039 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1040 emit_arith( p, OPCODE_MAD, dest, mask, saturate, src[2], src[3], tmp0 );
1041 }
1042 return dest;
1043 case MODE_ADD_PRODUCTS_SIGNED:
1044 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
1045 {
1046 struct ureg tmp0 = get_temp(p);
1047 half = get_half(p);
1048 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
1049 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[2], src[3], tmp0 );
1050 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
1051 }
1052 return dest;
1053 default:
1054 assert(0);
1055 return src[0];
1056 }
1057 }
1058
1059
1060 /**
1061 * Generate instructions for one texture unit's env/combiner mode.
1062 */
1063 static struct ureg
1064 emit_texenv(struct texenv_fragment_program *p, GLuint unit)
1065 {
1066 struct state_key *key = p->state;
1067 GLboolean saturate = (unit < p->last_tex_stage);
1068 GLuint rgb_shift, alpha_shift;
1069 struct ureg out, shift;
1070 struct ureg dest;
1071
1072 if (!key->unit[unit].enabled) {
1073 return get_source(p, SRC_PREVIOUS, 0);
1074 }
1075
1076 switch (key->unit[unit].ModeRGB) {
1077 case MODE_DOT3_RGB_EXT:
1078 alpha_shift = key->unit[unit].ScaleShiftA;
1079 rgb_shift = 0;
1080 break;
1081 case MODE_DOT3_RGBA_EXT:
1082 alpha_shift = 0;
1083 rgb_shift = 0;
1084 break;
1085 default:
1086 rgb_shift = key->unit[unit].ScaleShiftRGB;
1087 alpha_shift = key->unit[unit].ScaleShiftA;
1088 break;
1089 }
1090
1091 /* If this is the very last calculation, emit direct to output reg:
1092 */
1093 if (key->separate_specular ||
1094 unit != p->last_tex_stage ||
1095 alpha_shift ||
1096 rgb_shift)
1097 dest = get_temp( p );
1098 else
1099 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLR);
1100
1101 /* Emit the RGB and A combine ops
1102 */
1103 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
1104 args_match(key, unit)) {
1105 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1106 unit,
1107 key->unit[unit].NumArgsRGB,
1108 key->unit[unit].ModeRGB,
1109 key->unit[unit].OptRGB);
1110 }
1111 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
1112 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
1113
1114 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
1115 unit,
1116 key->unit[unit].NumArgsRGB,
1117 key->unit[unit].ModeRGB,
1118 key->unit[unit].OptRGB);
1119 }
1120 else {
1121 /* Need to do something to stop from re-emitting identical
1122 * argument calculations here:
1123 */
1124 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
1125 unit,
1126 key->unit[unit].NumArgsRGB,
1127 key->unit[unit].ModeRGB,
1128 key->unit[unit].OptRGB);
1129 out = emit_combine( p, dest, WRITEMASK_W, saturate,
1130 unit,
1131 key->unit[unit].NumArgsA,
1132 key->unit[unit].ModeA,
1133 key->unit[unit].OptA);
1134 }
1135
1136 /* Deal with the final shift:
1137 */
1138 if (alpha_shift || rgb_shift) {
1139 if (rgb_shift == alpha_shift) {
1140 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
1141 }
1142 else {
1143 shift = register_const4f(p,
1144 (GLfloat)(1<<rgb_shift),
1145 (GLfloat)(1<<rgb_shift),
1146 (GLfloat)(1<<rgb_shift),
1147 (GLfloat)(1<<alpha_shift));
1148 }
1149 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
1150 saturate, out, shift, undef );
1151 }
1152 else
1153 return out;
1154 }
1155
1156
1157 /**
1158 * Generate instruction for getting a texture source term.
1159 */
1160 static void load_texture( struct texenv_fragment_program *p, GLuint unit )
1161 {
1162 if (is_undef(p->src_texture[unit])) {
1163 GLuint dim = p->state->unit[unit].source_index;
1164 struct ureg texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
1165 struct ureg tmp = get_tex_temp( p );
1166
1167 if (dim == TEXTURE_UNKNOWN_INDEX)
1168 program_error(p, "TexSrcBit");
1169
1170 /* TODO: Use D0_MASK_XY where possible.
1171 */
1172 if (p->state->unit[unit].enabled) {
1173 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1174 tmp, WRITEMASK_XYZW,
1175 unit, dim, texcoord );
1176
1177 if (p->state->unit[unit].shadow)
1178 p->program->Base.ShadowSamplers |= 1 << unit;
1179
1180 p->program->Base.SamplersUsed |= (1 << unit);
1181 /* This identity mapping should already be in place
1182 * (see _mesa_init_program_struct()) but let's be safe.
1183 */
1184 p->program->Base.SamplerUnits[unit] = unit;
1185 }
1186 else
1187 p->src_texture[unit] = get_zero(p);
1188 }
1189 }
1190
1191 static GLboolean load_texenv_source( struct texenv_fragment_program *p,
1192 GLuint src, GLuint unit )
1193 {
1194 switch (src) {
1195 case SRC_TEXTURE:
1196 load_texture(p, unit);
1197 break;
1198
1199 case SRC_TEXTURE0:
1200 case SRC_TEXTURE1:
1201 case SRC_TEXTURE2:
1202 case SRC_TEXTURE3:
1203 case SRC_TEXTURE4:
1204 case SRC_TEXTURE5:
1205 case SRC_TEXTURE6:
1206 case SRC_TEXTURE7:
1207 load_texture(p, src - SRC_TEXTURE0);
1208 break;
1209
1210 default:
1211 /* not a texture src - do nothing */
1212 break;
1213 }
1214
1215 return GL_TRUE;
1216 }
1217
1218
1219 /**
1220 * Generate instructions for loading all texture source terms.
1221 */
1222 static GLboolean
1223 load_texunit_sources( struct texenv_fragment_program *p, int unit )
1224 {
1225 struct state_key *key = p->state;
1226 GLuint i;
1227
1228 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
1229 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit);
1230 }
1231
1232 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1233 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1234 }
1235
1236 return GL_TRUE;
1237 }
1238
1239
1240 /**
1241 * Generate a new fragment program which implements the context's
1242 * current texture env/combine mode.
1243 */
1244 static void
1245 create_new_program(GLcontext *ctx, struct state_key *key,
1246 struct gl_fragment_program *program)
1247 {
1248 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
1249 struct texenv_fragment_program p;
1250 GLuint unit;
1251 struct ureg cf, out;
1252
1253 _mesa_memset(&p, 0, sizeof(p));
1254 p.ctx = ctx;
1255 p.state = key;
1256 p.program = program;
1257
1258 /* During code generation, use locally-allocated instruction buffer,
1259 * then alloc dynamic storage below.
1260 */
1261 p.program->Base.Instructions = instBuffer;
1262 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
1263 p.program->Base.NumTexIndirections = 1; /* correct? */
1264 p.program->Base.NumTexInstructions = 0;
1265 p.program->Base.NumAluInstructions = 0;
1266 p.program->Base.String = NULL;
1267 p.program->Base.NumInstructions =
1268 p.program->Base.NumTemporaries =
1269 p.program->Base.NumParameters =
1270 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
1271 p.program->Base.Parameters = _mesa_new_parameter_list();
1272
1273 p.program->Base.InputsRead = 0;
1274 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLR;
1275
1276 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++)
1277 p.src_texture[unit] = undef;
1278
1279 p.src_previous = undef;
1280 p.half = undef;
1281 p.zero = undef;
1282 p.one = undef;
1283
1284 p.last_tex_stage = 0;
1285 release_temps(ctx, &p);
1286
1287 if (key->enabled_units) {
1288 /* First pass - to support texture_env_crossbar, first identify
1289 * all referenced texture sources and emit texld instructions
1290 * for each:
1291 */
1292 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
1293 if (key->unit[unit].enabled) {
1294 load_texunit_sources( &p, unit );
1295 p.last_tex_stage = unit;
1296 }
1297
1298 /* Second pass - emit combine instructions to build final color:
1299 */
1300 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
1301 if (key->enabled_units & (1<<unit)) {
1302 p.src_previous = emit_texenv( &p, unit );
1303 reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
1304 release_temps(ctx, &p); /* release all temps */
1305 }
1306 }
1307
1308 cf = get_source( &p, SRC_PREVIOUS, 0 );
1309 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLR );
1310
1311 if (key->separate_specular) {
1312 /* Emit specular add.
1313 */
1314 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
1315 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1316 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
1317 }
1318 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
1319 /* Will wind up in here if no texture enabled or a couple of
1320 * other scenarios (GL_REPLACE for instance).
1321 */
1322 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
1323 }
1324
1325 /* Finish up:
1326 */
1327 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
1328
1329 if (key->fog_enabled) {
1330 /* Pull fog mode from GLcontext, the value in the state key is
1331 * a reduced value and not what is expected in FogOption
1332 */
1333 p.program->FogOption = ctx->Fog.Mode;
1334 p.program->Base.InputsRead |= FRAG_BIT_FOGC; /* XXX new */
1335 } else
1336 p.program->FogOption = GL_NONE;
1337
1338 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
1339 program_error(&p, "Exceeded max nr indirect texture lookups");
1340
1341 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
1342 program_error(&p, "Exceeded max TEX instructions");
1343
1344 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
1345 program_error(&p, "Exceeded max ALU instructions");
1346
1347 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
1348
1349 /* Allocate final instruction array */
1350 p.program->Base.Instructions
1351 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1352 if (!p.program->Base.Instructions) {
1353 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1354 "generating tex env program");
1355 return;
1356 }
1357 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1358 p.program->Base.NumInstructions);
1359
1360 if (p.program->FogOption) {
1361 _mesa_append_fog_code(ctx, p.program);
1362 p.program->FogOption = GL_NONE;
1363 }
1364
1365
1366 /* Notify driver the fragment program has (actually) changed.
1367 */
1368 if (ctx->Driver.ProgramStringNotify) {
1369 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1370 &p.program->Base );
1371 }
1372
1373 if (DISASSEM) {
1374 _mesa_print_program(&p.program->Base);
1375 _mesa_printf("\n");
1376 }
1377 }
1378
1379
1380 /**
1381 * Return a fragment program which implements the current
1382 * fixed-function texture, fog and color-sum operations.
1383 */
1384 struct gl_fragment_program *
1385 _mesa_get_fixed_func_fragment_program(GLcontext *ctx)
1386 {
1387 struct gl_fragment_program *prog;
1388 struct state_key key;
1389
1390 make_state_key(ctx, &key);
1391
1392 prog = (struct gl_fragment_program *)
1393 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1394 &key, sizeof(key));
1395
1396 if (!prog) {
1397 prog = (struct gl_fragment_program *)
1398 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1399
1400 create_new_program(ctx, &key, prog);
1401
1402 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1403 &key, sizeof(key), &prog->Base);
1404 }
1405
1406 return prog;
1407 }