mesa: remove the unused _mesa_UpdateTexEnvProgram() function
[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 * This MAX is probably a bit generous, but that's OK. There can be
71 * up to four instructions per texture unit (TEX + 3 for combine),
72 * then there's fog and specular add.
73 */
74 #define MAX_INSTRUCTIONS ((MAX_TEXTURE_COORD_UNITS * 4) + 12)
75
76 #define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
77
78 struct mode_opt {
79 GLuint Source:4;
80 GLuint Operand:3;
81 };
82
83 struct state_key {
84 GLbitfield enabled_units;
85 GLuint separate_specular:1;
86 GLuint fog_enabled:1;
87 GLuint fog_mode:2;
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 switch (bit) {
236 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
237 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
238 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
239 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
240 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
241 case TEXTURE_1D_ARRAY_BIT: return TEXTURE_1D_ARRAY_INDEX;
242 case TEXTURE_2D_ARRAY_BIT: return TEXTURE_2D_ARRAY_INDEX;
243 default:
244 assert(0);
245 return TEXTURE_UNKNOWN_INDEX;
246 }
247 }
248
249 /**
250 * Examine current texture environment state and generate a unique
251 * key to identify it.
252 */
253 static void make_state_key( GLcontext *ctx, struct state_key *key )
254 {
255 GLuint i, j;
256
257 memset(key, 0, sizeof(*key));
258
259 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
260 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
261 GLenum format;
262
263 if (!texUnit->_ReallyEnabled || !texUnit->Enabled)
264 continue;
265
266 format = texUnit->_Current->Image[0][texUnit->_Current->BaseLevel]->_BaseFormat;
267
268 key->unit[i].enabled = 1;
269 key->enabled_units |= (1<<i);
270
271 key->unit[i].source_index =
272 translate_tex_src_bit(texUnit->_ReallyEnabled);
273 key->unit[i].shadow = ((texUnit->_Current->CompareMode == GL_COMPARE_R_TO_TEXTURE) &&
274 ((format == GL_DEPTH_COMPONENT) ||
275 (format == GL_DEPTH_STENCIL_EXT)));
276
277 key->unit[i].NumArgsRGB = texUnit->_CurrentCombine->_NumArgsRGB;
278 key->unit[i].NumArgsA = texUnit->_CurrentCombine->_NumArgsA;
279
280 key->unit[i].ModeRGB =
281 translate_mode(texUnit->EnvMode, texUnit->_CurrentCombine->ModeRGB);
282 key->unit[i].ModeA =
283 translate_mode(texUnit->EnvMode, texUnit->_CurrentCombine->ModeA);
284
285 key->unit[i].ScaleShiftRGB = texUnit->_CurrentCombine->ScaleShiftRGB;
286 key->unit[i].ScaleShiftA = texUnit->_CurrentCombine->ScaleShiftA;
287
288 for (j = 0; j < MAX_TERMS; j++) {
289 key->unit[i].OptRGB[j].Operand =
290 translate_operand(texUnit->_CurrentCombine->OperandRGB[j]);
291 key->unit[i].OptA[j].Operand =
292 translate_operand(texUnit->_CurrentCombine->OperandA[j]);
293 key->unit[i].OptRGB[j].Source =
294 translate_source(texUnit->_CurrentCombine->SourceRGB[j]);
295 key->unit[i].OptA[j].Source =
296 translate_source(texUnit->_CurrentCombine->SourceA[j]);
297 }
298 }
299
300 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
301 key->separate_specular = 1;
302
303 if (ctx->Fog.Enabled) {
304 key->fog_enabled = 1;
305 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
306 }
307 }
308
309 /* Use uregs to represent registers internally, translate to Mesa's
310 * expected formats on emit.
311 *
312 * NOTE: These are passed by value extensively in this file rather
313 * than as usual by pointer reference. If this disturbs you, try
314 * remembering they are just 32bits in size.
315 *
316 * GCC is smart enough to deal with these dword-sized structures in
317 * much the same way as if I had defined them as dwords and was using
318 * macros to access and set the fields. This is much nicer and easier
319 * to evolve.
320 */
321 struct ureg {
322 GLuint file:4;
323 GLuint idx:8;
324 GLuint negatebase:1;
325 GLuint abs:1;
326 GLuint negateabs:1;
327 GLuint swz:12;
328 GLuint pad:5;
329 };
330
331 static const struct ureg undef = {
332 PROGRAM_UNDEFINED,
333 ~0,
334 0,
335 0,
336 0,
337 0,
338 0
339 };
340
341
342 /* State used to build the fragment program:
343 */
344 struct texenv_fragment_program {
345 struct gl_fragment_program *program;
346 GLcontext *ctx;
347 struct state_key *state;
348
349 GLbitfield alu_temps; /* Track texture indirections, see spec. */
350 GLbitfield temps_output; /* Track texture indirections, see spec. */
351 GLbitfield temp_in_use; /* Tracks temporary regs which are in use. */
352 GLboolean error;
353
354 struct ureg src_texture[MAX_TEXTURE_COORD_UNITS];
355 /* Reg containing each texture unit's sampled texture color,
356 * else undef.
357 */
358
359 struct ureg src_previous; /* Reg containing color from previous
360 * stage. May need to be decl'd.
361 */
362
363 GLuint last_tex_stage; /* Number of last enabled texture unit */
364
365 struct ureg half;
366 struct ureg one;
367 struct ureg zero;
368 };
369
370
371
372 static struct ureg make_ureg(GLuint file, GLuint idx)
373 {
374 struct ureg reg;
375 reg.file = file;
376 reg.idx = idx;
377 reg.negatebase = 0;
378 reg.abs = 0;
379 reg.negateabs = 0;
380 reg.swz = SWIZZLE_NOOP;
381 reg.pad = 0;
382 return reg;
383 }
384
385 static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
386 {
387 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
388 GET_SWZ(reg.swz, y),
389 GET_SWZ(reg.swz, z),
390 GET_SWZ(reg.swz, w));
391
392 return reg;
393 }
394
395 static struct ureg swizzle1( struct ureg reg, int x )
396 {
397 return swizzle(reg, x, x, x, x);
398 }
399
400 static struct ureg negate( struct ureg reg )
401 {
402 reg.negatebase ^= 1;
403 return reg;
404 }
405
406 static GLboolean is_undef( struct ureg reg )
407 {
408 return reg.file == PROGRAM_UNDEFINED;
409 }
410
411
412 static struct ureg get_temp( struct texenv_fragment_program *p )
413 {
414 GLint bit;
415
416 /* First try and reuse temps which have been used already:
417 */
418 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
419
420 /* Then any unused temporary:
421 */
422 if (!bit)
423 bit = _mesa_ffs( ~p->temp_in_use );
424
425 if (!bit) {
426 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
427 _mesa_exit(1);
428 }
429
430 if ((GLuint) bit > p->program->Base.NumTemporaries)
431 p->program->Base.NumTemporaries = bit;
432
433 p->temp_in_use |= 1<<(bit-1);
434 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
435 }
436
437 static struct ureg get_tex_temp( struct texenv_fragment_program *p )
438 {
439 int bit;
440
441 /* First try to find availble temp not previously used (to avoid
442 * starting a new texture indirection). According to the spec, the
443 * ~p->temps_output isn't necessary, but will keep it there for
444 * now:
445 */
446 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
447
448 /* Then any unused temporary:
449 */
450 if (!bit)
451 bit = _mesa_ffs( ~p->temp_in_use );
452
453 if (!bit) {
454 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
455 _mesa_exit(1);
456 }
457
458 if ((GLuint) bit > p->program->Base.NumTemporaries)
459 p->program->Base.NumTemporaries = bit;
460
461 p->temp_in_use |= 1<<(bit-1);
462 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
463 }
464
465
466 static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
467 {
468 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
469
470 /* KW: To support tex_env_crossbar, don't release the registers in
471 * temps_output.
472 */
473 if (max_temp >= sizeof(int) * 8)
474 p->temp_in_use = p->temps_output;
475 else
476 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
477 }
478
479
480 static struct ureg register_param5( struct texenv_fragment_program *p,
481 GLint s0,
482 GLint s1,
483 GLint s2,
484 GLint s3,
485 GLint s4)
486 {
487 gl_state_index tokens[STATE_LENGTH];
488 GLuint idx;
489 tokens[0] = s0;
490 tokens[1] = s1;
491 tokens[2] = s2;
492 tokens[3] = s3;
493 tokens[4] = s4;
494 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
495 return make_ureg(PROGRAM_STATE_VAR, idx);
496 }
497
498
499 #define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
500 #define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
501 #define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
502 #define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
503
504
505 static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
506 {
507 p->program->Base.InputsRead |= (1 << input);
508 return make_ureg(PROGRAM_INPUT, input);
509 }
510
511
512 static void emit_arg( struct prog_src_register *reg,
513 struct ureg ureg )
514 {
515 reg->File = ureg.file;
516 reg->Index = ureg.idx;
517 reg->Swizzle = ureg.swz;
518 reg->NegateBase = ureg.negatebase ? 0xf : 0x0;
519 reg->Abs = ureg.abs;
520 reg->NegateAbs = ureg.negateabs;
521 }
522
523 static void emit_dst( struct prog_dst_register *dst,
524 struct ureg ureg, GLuint mask )
525 {
526 dst->File = ureg.file;
527 dst->Index = ureg.idx;
528 dst->WriteMask = mask;
529 dst->CondMask = COND_TR; /* always pass cond test */
530 dst->CondSwizzle = SWIZZLE_NOOP;
531 }
532
533 static struct prog_instruction *
534 emit_op(struct texenv_fragment_program *p,
535 enum prog_opcode op,
536 struct ureg dest,
537 GLuint mask,
538 GLboolean saturate,
539 struct ureg src0,
540 struct ureg src1,
541 struct ureg src2 )
542 {
543 GLuint nr = p->program->Base.NumInstructions++;
544 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
545
546 assert(nr < MAX_INSTRUCTIONS);
547
548 _mesa_init_instructions(inst, 1);
549 inst->Opcode = op;
550
551 emit_arg( &inst->SrcReg[0], src0 );
552 emit_arg( &inst->SrcReg[1], src1 );
553 emit_arg( &inst->SrcReg[2], src2 );
554
555 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
556
557 emit_dst( &inst->DstReg, dest, mask );
558
559 /* Accounting for indirection tracking:
560 */
561 if (dest.file == PROGRAM_TEMPORARY)
562 p->temps_output |= 1 << dest.idx;
563
564 return inst;
565 }
566
567
568 static struct ureg emit_arith( struct texenv_fragment_program *p,
569 enum prog_opcode op,
570 struct ureg dest,
571 GLuint mask,
572 GLboolean saturate,
573 struct ureg src0,
574 struct ureg src1,
575 struct ureg src2 )
576 {
577 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
578
579 /* Accounting for indirection tracking:
580 */
581 if (src0.file == PROGRAM_TEMPORARY)
582 p->alu_temps |= 1 << src0.idx;
583
584 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
585 p->alu_temps |= 1 << src1.idx;
586
587 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
588 p->alu_temps |= 1 << src2.idx;
589
590 if (dest.file == PROGRAM_TEMPORARY)
591 p->alu_temps |= 1 << dest.idx;
592
593 p->program->Base.NumAluInstructions++;
594 return dest;
595 }
596
597 static struct ureg emit_texld( struct texenv_fragment_program *p,
598 enum prog_opcode op,
599 struct ureg dest,
600 GLuint destmask,
601 GLuint tex_unit,
602 GLuint tex_idx,
603 struct ureg coord )
604 {
605 struct prog_instruction *inst = emit_op( p, op,
606 dest, destmask,
607 GL_FALSE, /* don't saturate? */
608 coord, /* arg 0? */
609 undef,
610 undef);
611
612 inst->TexSrcTarget = tex_idx;
613 inst->TexSrcUnit = tex_unit;
614
615 p->program->Base.NumTexInstructions++;
616
617 /* Is this a texture indirection?
618 */
619 if ((coord.file == PROGRAM_TEMPORARY &&
620 (p->temps_output & (1<<coord.idx))) ||
621 (dest.file == PROGRAM_TEMPORARY &&
622 (p->alu_temps & (1<<dest.idx)))) {
623 p->program->Base.NumTexIndirections++;
624 p->temps_output = 1<<coord.idx;
625 p->alu_temps = 0;
626 assert(0); /* KW: texture env crossbar */
627 }
628
629 return dest;
630 }
631
632
633 static struct ureg register_const4f( struct texenv_fragment_program *p,
634 GLfloat s0,
635 GLfloat s1,
636 GLfloat s2,
637 GLfloat s3)
638 {
639 GLfloat values[4];
640 GLuint idx, swizzle;
641 values[0] = s0;
642 values[1] = s1;
643 values[2] = s2;
644 values[3] = s3;
645 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
646 &swizzle );
647 ASSERT(swizzle == SWIZZLE_NOOP);
648 return make_ureg(PROGRAM_CONSTANT, idx);
649 }
650
651 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
652 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
653 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
654 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
655
656
657 static struct ureg get_one( struct texenv_fragment_program *p )
658 {
659 if (is_undef(p->one))
660 p->one = register_scalar_const(p, 1.0);
661 return p->one;
662 }
663
664 static struct ureg get_half( struct texenv_fragment_program *p )
665 {
666 if (is_undef(p->half))
667 p->half = register_scalar_const(p, 0.5);
668 return p->half;
669 }
670
671 static struct ureg get_zero( struct texenv_fragment_program *p )
672 {
673 if (is_undef(p->zero))
674 p->zero = register_scalar_const(p, 0.0);
675 return p->zero;
676 }
677
678
679 static void program_error( struct texenv_fragment_program *p, const char *msg )
680 {
681 _mesa_problem(NULL, msg);
682 p->error = 1;
683 }
684
685 static struct ureg get_source( struct texenv_fragment_program *p,
686 GLuint src, GLuint unit )
687 {
688 switch (src) {
689 case SRC_TEXTURE:
690 assert(!is_undef(p->src_texture[unit]));
691 return p->src_texture[unit];
692
693 case SRC_TEXTURE0:
694 case SRC_TEXTURE1:
695 case SRC_TEXTURE2:
696 case SRC_TEXTURE3:
697 case SRC_TEXTURE4:
698 case SRC_TEXTURE5:
699 case SRC_TEXTURE6:
700 case SRC_TEXTURE7:
701 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
702 return p->src_texture[src - SRC_TEXTURE0];
703
704 case SRC_CONSTANT:
705 return register_param2(p, STATE_TEXENV_COLOR, unit);
706
707 case SRC_PRIMARY_COLOR:
708 return register_input(p, FRAG_ATTRIB_COL0);
709
710 case SRC_ZERO:
711 return get_zero(p);
712
713 case SRC_PREVIOUS:
714 if (is_undef(p->src_previous))
715 return register_input(p, FRAG_ATTRIB_COL0);
716 else
717 return p->src_previous;
718
719 default:
720 assert(0);
721 }
722 }
723
724 static struct ureg emit_combine_source( struct texenv_fragment_program *p,
725 GLuint mask,
726 GLuint unit,
727 GLuint source,
728 GLuint operand )
729 {
730 struct ureg arg, src, one;
731
732 src = get_source(p, source, unit);
733
734 switch (operand) {
735 case OPR_ONE_MINUS_SRC_COLOR:
736 /* Get unused tmp,
737 * Emit tmp = 1.0 - arg.xyzw
738 */
739 arg = get_temp( p );
740 one = get_one( p );
741 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
742
743 case OPR_SRC_ALPHA:
744 if (mask == WRITEMASK_W)
745 return src;
746 else
747 return swizzle1( src, SWIZZLE_W );
748 case OPR_ONE_MINUS_SRC_ALPHA:
749 /* Get unused tmp,
750 * Emit tmp = 1.0 - arg.wwww
751 */
752 arg = get_temp(p);
753 one = get_one(p);
754 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
755 one, swizzle1(src, SWIZZLE_W), undef);
756 case OPR_ZERO:
757 return get_zero(p);
758 case OPR_ONE:
759 return get_one(p);
760 case OPR_SRC_COLOR:
761 return src;
762 default:
763 assert(0);
764 return src;
765 }
766 }
767
768 static GLboolean args_match( struct state_key *key, GLuint unit )
769 {
770 GLuint i, nr = key->unit[unit].NumArgsRGB;
771
772 for (i = 0 ; i < nr ; i++) {
773 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
774 return GL_FALSE;
775
776 switch(key->unit[unit].OptA[i].Operand) {
777 case OPR_SRC_ALPHA:
778 switch(key->unit[unit].OptRGB[i].Operand) {
779 case OPR_SRC_COLOR:
780 case OPR_SRC_ALPHA:
781 break;
782 default:
783 return GL_FALSE;
784 }
785 break;
786 case OPR_ONE_MINUS_SRC_ALPHA:
787 switch(key->unit[unit].OptRGB[i].Operand) {
788 case OPR_ONE_MINUS_SRC_COLOR:
789 case OPR_ONE_MINUS_SRC_ALPHA:
790 break;
791 default:
792 return GL_FALSE;
793 }
794 break;
795 default:
796 return GL_FALSE; /* impossible */
797 }
798 }
799
800 return GL_TRUE;
801 }
802
803 static struct ureg emit_combine( struct texenv_fragment_program *p,
804 struct ureg dest,
805 GLuint mask,
806 GLboolean saturate,
807 GLuint unit,
808 GLuint nr,
809 GLuint mode,
810 const struct mode_opt *opt)
811 {
812 struct ureg src[MAX_TERMS];
813 struct ureg tmp, half;
814 GLuint i;
815
816 assert(nr <= MAX_TERMS);
817
818 tmp = undef; /* silence warning (bug 5318) */
819
820 for (i = 0; i < nr; i++)
821 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
822
823 switch (mode) {
824 case MODE_REPLACE:
825 if (mask == WRITEMASK_XYZW && !saturate)
826 return src[0];
827 else
828 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
829 case MODE_MODULATE:
830 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
831 src[0], src[1], undef );
832 case MODE_ADD:
833 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
834 src[0], src[1], undef );
835 case MODE_ADD_SIGNED:
836 /* tmp = arg0 + arg1
837 * result = tmp - .5
838 */
839 half = get_half(p);
840 tmp = get_temp( p );
841 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
842 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
843 return dest;
844 case MODE_INTERPOLATE:
845 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
846 */
847 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
848
849 case MODE_SUBTRACT:
850 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
851
852 case MODE_DOT3_RGBA:
853 case MODE_DOT3_RGBA_EXT:
854 case MODE_DOT3_RGB_EXT:
855 case MODE_DOT3_RGB: {
856 struct ureg tmp0 = get_temp( p );
857 struct ureg tmp1 = get_temp( p );
858 struct ureg neg1 = register_scalar_const(p, -1);
859 struct ureg two = register_scalar_const(p, 2);
860
861 /* tmp0 = 2*src0 - 1
862 * tmp1 = 2*src1 - 1
863 *
864 * dst = tmp0 dot3 tmp1
865 */
866 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
867 two, src[0], neg1);
868
869 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
870 tmp1 = tmp0;
871 else
872 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
873 two, src[1], neg1);
874 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
875 return dest;
876 }
877 case MODE_MODULATE_ADD_ATI:
878 /* Arg0 * Arg2 + Arg1 */
879 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
880 src[0], src[2], src[1] );
881 case MODE_MODULATE_SIGNED_ADD_ATI: {
882 /* Arg0 * Arg2 + Arg1 - 0.5 */
883 struct ureg tmp0 = get_temp(p);
884 half = get_half(p);
885 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
886 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
887 return dest;
888 }
889 case MODE_MODULATE_SUBTRACT_ATI:
890 /* Arg0 * Arg2 - Arg1 */
891 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
892 return dest;
893 case MODE_ADD_PRODUCTS:
894 /* Arg0 * Arg1 + Arg2 * Arg3 */
895 {
896 struct ureg tmp0 = get_temp(p);
897 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
898 emit_arith( p, OPCODE_MAD, dest, mask, saturate, src[2], src[3], tmp0 );
899 }
900 return dest;
901 case MODE_ADD_PRODUCTS_SIGNED:
902 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
903 {
904 struct ureg tmp0 = get_temp(p);
905 half = get_half(p);
906 emit_arith( p, OPCODE_MUL, tmp0, mask, 0, src[0], src[1], undef );
907 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[2], src[3], tmp0 );
908 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
909 }
910 return dest;
911 default:
912 assert(0);
913 return src[0];
914 }
915 }
916
917
918 /**
919 * Generate instructions for one texture unit's env/combiner mode.
920 */
921 static struct ureg
922 emit_texenv(struct texenv_fragment_program *p, GLuint unit)
923 {
924 struct state_key *key = p->state;
925 GLboolean saturate = (unit < p->last_tex_stage);
926 GLuint rgb_shift, alpha_shift;
927 struct ureg out, shift;
928 struct ureg dest;
929
930 if (!key->unit[unit].enabled) {
931 return get_source(p, SRC_PREVIOUS, 0);
932 }
933
934 switch (key->unit[unit].ModeRGB) {
935 case MODE_DOT3_RGB_EXT:
936 alpha_shift = key->unit[unit].ScaleShiftA;
937 rgb_shift = 0;
938 break;
939 case MODE_DOT3_RGBA_EXT:
940 alpha_shift = 0;
941 rgb_shift = 0;
942 break;
943 default:
944 rgb_shift = key->unit[unit].ScaleShiftRGB;
945 alpha_shift = key->unit[unit].ScaleShiftA;
946 break;
947 }
948
949 /* If this is the very last calculation, emit direct to output reg:
950 */
951 if (key->separate_specular ||
952 unit != p->last_tex_stage ||
953 alpha_shift ||
954 rgb_shift)
955 dest = get_temp( p );
956 else
957 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLR);
958
959 /* Emit the RGB and A combine ops
960 */
961 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
962 args_match(key, unit)) {
963 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
964 unit,
965 key->unit[unit].NumArgsRGB,
966 key->unit[unit].ModeRGB,
967 key->unit[unit].OptRGB);
968 }
969 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
970 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
971
972 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
973 unit,
974 key->unit[unit].NumArgsRGB,
975 key->unit[unit].ModeRGB,
976 key->unit[unit].OptRGB);
977 }
978 else {
979 /* Need to do something to stop from re-emitting identical
980 * argument calculations here:
981 */
982 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
983 unit,
984 key->unit[unit].NumArgsRGB,
985 key->unit[unit].ModeRGB,
986 key->unit[unit].OptRGB);
987 out = emit_combine( p, dest, WRITEMASK_W, saturate,
988 unit,
989 key->unit[unit].NumArgsA,
990 key->unit[unit].ModeA,
991 key->unit[unit].OptA);
992 }
993
994 /* Deal with the final shift:
995 */
996 if (alpha_shift || rgb_shift) {
997 if (rgb_shift == alpha_shift) {
998 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
999 }
1000 else {
1001 shift = register_const4f(p,
1002 (GLfloat)(1<<rgb_shift),
1003 (GLfloat)(1<<rgb_shift),
1004 (GLfloat)(1<<rgb_shift),
1005 (GLfloat)(1<<alpha_shift));
1006 }
1007 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
1008 saturate, out, shift, undef );
1009 }
1010 else
1011 return out;
1012 }
1013
1014
1015 /**
1016 * Generate instruction for getting a texture source term.
1017 */
1018 static void load_texture( struct texenv_fragment_program *p, GLuint unit )
1019 {
1020 if (is_undef(p->src_texture[unit])) {
1021 GLuint dim = p->state->unit[unit].source_index;
1022 struct ureg texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
1023 struct ureg tmp = get_tex_temp( p );
1024
1025 if (dim == TEXTURE_UNKNOWN_INDEX)
1026 program_error(p, "TexSrcBit");
1027
1028 /* TODO: Use D0_MASK_XY where possible.
1029 */
1030 if (p->state->unit[unit].enabled) {
1031 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
1032 tmp, WRITEMASK_XYZW,
1033 unit, dim, texcoord );
1034 if (p->state->unit[unit].shadow)
1035 p->program->Base.ShadowSamplers |= 1 << unit;
1036
1037 p->program->Base.SamplersUsed |= (1 << unit);
1038 /* This identity mapping should already be in place
1039 * (see _mesa_init_program_struct()) but let's be safe.
1040 */
1041 p->program->Base.SamplerUnits[unit] = unit;
1042 }
1043 else
1044 p->src_texture[unit] = get_zero(p);
1045 }
1046 }
1047
1048 static GLboolean load_texenv_source( struct texenv_fragment_program *p,
1049 GLuint src, GLuint unit )
1050 {
1051 switch (src) {
1052 case SRC_TEXTURE:
1053 load_texture(p, unit);
1054 break;
1055
1056 case SRC_TEXTURE0:
1057 case SRC_TEXTURE1:
1058 case SRC_TEXTURE2:
1059 case SRC_TEXTURE3:
1060 case SRC_TEXTURE4:
1061 case SRC_TEXTURE5:
1062 case SRC_TEXTURE6:
1063 case SRC_TEXTURE7:
1064 load_texture(p, src - SRC_TEXTURE0);
1065 break;
1066
1067 default:
1068 /* not a texture src - do nothing */
1069 break;
1070 }
1071
1072 return GL_TRUE;
1073 }
1074
1075
1076 /**
1077 * Generate instructions for loading all texture source terms.
1078 */
1079 static GLboolean
1080 load_texunit_sources( struct texenv_fragment_program *p, int unit )
1081 {
1082 struct state_key *key = p->state;
1083 GLuint i;
1084
1085 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
1086 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit);
1087 }
1088
1089 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1090 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1091 }
1092
1093 return GL_TRUE;
1094 }
1095
1096
1097 /**
1098 * Generate a new fragment program which implements the context's
1099 * current texture env/combine mode.
1100 */
1101 static void
1102 create_new_program(GLcontext *ctx, struct state_key *key,
1103 struct gl_fragment_program *program)
1104 {
1105 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
1106 struct texenv_fragment_program p;
1107 GLuint unit;
1108 struct ureg cf, out;
1109
1110 _mesa_memset(&p, 0, sizeof(p));
1111 p.ctx = ctx;
1112 p.state = key;
1113 p.program = program;
1114
1115 /* During code generation, use locally-allocated instruction buffer,
1116 * then alloc dynamic storage below.
1117 */
1118 p.program->Base.Instructions = instBuffer;
1119 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
1120 p.program->Base.NumTexIndirections = 1; /* correct? */
1121 p.program->Base.NumTexInstructions = 0;
1122 p.program->Base.NumAluInstructions = 0;
1123 p.program->Base.String = NULL;
1124 p.program->Base.NumInstructions =
1125 p.program->Base.NumTemporaries =
1126 p.program->Base.NumParameters =
1127 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
1128 p.program->Base.Parameters = _mesa_new_parameter_list();
1129
1130 p.program->Base.InputsRead = 0;
1131 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLR;
1132
1133 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++)
1134 p.src_texture[unit] = undef;
1135
1136 p.src_previous = undef;
1137 p.half = undef;
1138 p.zero = undef;
1139 p.one = undef;
1140
1141 p.last_tex_stage = 0;
1142 release_temps(ctx, &p);
1143
1144 if (key->enabled_units) {
1145 /* First pass - to support texture_env_crossbar, first identify
1146 * all referenced texture sources and emit texld instructions
1147 * for each:
1148 */
1149 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
1150 if (key->unit[unit].enabled) {
1151 load_texunit_sources( &p, unit );
1152 p.last_tex_stage = unit;
1153 }
1154
1155 /* Second pass - emit combine instructions to build final color:
1156 */
1157 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
1158 if (key->enabled_units & (1<<unit)) {
1159 p.src_previous = emit_texenv( &p, unit );
1160 release_temps(ctx, &p); /* release all temps */
1161 }
1162 }
1163
1164 cf = get_source( &p, SRC_PREVIOUS, 0 );
1165 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLR );
1166
1167 if (key->separate_specular) {
1168 /* Emit specular add.
1169 */
1170 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
1171 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1172 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
1173 }
1174 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
1175 /* Will wind up in here if no texture enabled or a couple of
1176 * other scenarios (GL_REPLACE for instance).
1177 */
1178 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
1179 }
1180
1181 /* Finish up:
1182 */
1183 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
1184
1185 if (key->fog_enabled) {
1186 /* Pull fog mode from GLcontext, the value in the state key is
1187 * a reduced value and not what is expected in FogOption
1188 */
1189 p.program->FogOption = ctx->Fog.Mode;
1190 p.program->Base.InputsRead |= FRAG_BIT_FOGC; /* XXX new */
1191 } else
1192 p.program->FogOption = GL_NONE;
1193
1194 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
1195 program_error(&p, "Exceeded max nr indirect texture lookups");
1196
1197 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
1198 program_error(&p, "Exceeded max TEX instructions");
1199
1200 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
1201 program_error(&p, "Exceeded max ALU instructions");
1202
1203 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
1204
1205 /* Allocate final instruction array */
1206 p.program->Base.Instructions
1207 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1208 if (!p.program->Base.Instructions) {
1209 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1210 "generating tex env program");
1211 return;
1212 }
1213 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1214 p.program->Base.NumInstructions);
1215
1216 if (p.program->FogOption) {
1217 _mesa_append_fog_code(ctx, p.program);
1218 p.program->FogOption = GL_NONE;
1219 }
1220
1221
1222 /* Notify driver the fragment program has (actually) changed.
1223 */
1224 if (ctx->Driver.ProgramStringNotify) {
1225 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1226 &p.program->Base );
1227 }
1228
1229 if (DISASSEM) {
1230 _mesa_print_program(&p.program->Base);
1231 _mesa_printf("\n");
1232 }
1233 }
1234
1235
1236 /**
1237 * Return a fragment program which implements the current
1238 * fixed-function texture, fog and color-sum operations.
1239 */
1240 struct gl_fragment_program *
1241 _mesa_get_fixed_func_fragment_program(GLcontext *ctx)
1242 {
1243 struct gl_fragment_program *prog;
1244 struct state_key key;
1245
1246 make_state_key(ctx, &key);
1247
1248 prog = (struct gl_fragment_program *)
1249 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1250 &key, sizeof(key));
1251
1252 if (!prog) {
1253 prog = (struct gl_fragment_program *)
1254 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1255
1256 create_new_program(ctx, &key, prog);
1257
1258 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1259 &key, sizeof(key), &prog->Base);
1260 }
1261
1262 return prog;
1263 }