1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
31 #include "texenvprogram.h"
33 #include "shader/program.h"
34 #include "shader/nvfragprog.h"
35 #include "shader/arbfragparse.h"
38 #define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
40 /* Use uregs to represent registers internally, translate to Mesa's
41 * expected formats on emit.
43 * NOTE: These are passed by value extensively in this file rather
44 * than as usual by pointer reference. If this disturbs you, try
45 * remembering they are just 32bits in size.
47 * GCC is smart enough to deal with these dword-sized structures in
48 * much the same way as if I had defined them as dwords and was using
49 * macros to access and set the fields. This is much nicer and easier
62 const static struct ureg undef
= {
77 /* State used to build the fragment program:
79 struct texenv_fragment_program
{
80 struct fragment_program
*program
;
83 GLuint alu_temps
; /* Track texture indirections, see spec. */
84 GLuint temps_output
; /* Track texture indirections, see spec. */
86 GLuint temp_in_use
; /* Tracks temporary regs which are in
93 struct ureg src_texture
[MAX_TEXTURE_UNITS
];
94 /* Reg containing each texture unit's sampled texture color,
98 struct ureg src_previous
; /* Reg containing color from previous
99 * stage. May need to be decl'd.
102 GLuint last_tex_stage
; /* Number of last enabled texture unit */
111 static struct ureg
make_ureg(GLuint file
, GLuint idx
)
119 reg
.swz
= SWIZZLE_NOOP
;
124 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
126 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
129 GET_SWZ(reg
.swz
, w
));
134 static struct ureg
swizzle1( struct ureg reg
, int x
)
136 return swizzle(reg
, x
, x
, x
, x
);
139 static struct ureg
negate( struct ureg reg
)
145 static GLboolean
is_undef( struct ureg reg
)
147 return reg
.file
== 0xf;
151 static struct ureg
get_temp( struct texenv_fragment_program
*p
)
155 /* First try and reuse temps which have been used already:
157 bit
= ffs( ~p
->temp_in_use
& p
->alu_temps
);
159 /* Then any unused temporary:
162 bit
= ffs( ~p
->temp_in_use
);
165 fprintf(stderr
, "%s: out of temporaries\n", __FILE__
);
169 p
->temp_in_use
|= 1<<(bit
-1);
170 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
173 static struct ureg
get_tex_temp( struct texenv_fragment_program
*p
)
177 /* First try to find availble temp not previously used (to avoid
178 * starting a new texture indirection). According to the spec, the
179 * ~p->temps_output isn't necessary, but will keep it there for
182 bit
= ffs( ~p
->temp_in_use
& ~p
->alu_temps
& ~p
->temps_output
);
184 /* Then any unused temporary:
187 bit
= ffs( ~p
->temp_in_use
);
190 fprintf(stderr
, "%s: out of temporaries\n", __FILE__
);
194 p
->temp_in_use
|= 1<<(bit
-1);
195 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
199 static void release_temps( struct texenv_fragment_program
*p
)
201 GLuint max_temp
= p
->ctx
->Const
.MaxFragmentProgramTemps
;
203 /* KW: To support tex_env_crossbar, don't release the registers in
206 if (max_temp
>= sizeof(int) * 8)
207 p
->temp_in_use
= p
->temps_output
;
209 p
->temp_in_use
= ~((1<<max_temp
)-1) | p
->temps_output
;
213 static struct ureg
register_param6( struct texenv_fragment_program
*p
,
229 idx
= _mesa_add_state_reference( p
->program
->Parameters
, tokens
);
230 return make_ureg(PROGRAM_STATE_VAR
, idx
);
234 #define register_param1(p,s0) register_param6(p,s0,0,0,0,0,0)
235 #define register_param2(p,s0,s1) register_param6(p,s0,s1,0,0,0,0)
236 #define register_param3(p,s0,s1,s2) register_param6(p,s0,s1,s2,0,0,0)
237 #define register_param4(p,s0,s1,s2,s3) register_param6(p,s0,s1,s2,s3,0,0)
240 static struct ureg
register_input( struct texenv_fragment_program
*p
, GLuint input
)
242 p
->program
->InputsRead
|= (1<<input
);
243 return make_ureg(PROGRAM_INPUT
, input
);
247 static void emit_arg( struct fp_src_register
*reg
,
250 reg
->File
= ureg
.file
;
251 reg
->Index
= ureg
.idx
;
252 reg
->Swizzle
= ureg
.swz
;
253 reg
->NegateBase
= ureg
.negatebase
;
255 reg
->NegateAbs
= ureg
.negateabs
;
258 static void emit_dst( struct fp_dst_register
*dst
,
259 struct ureg ureg
, GLuint mask
)
261 dst
->File
= ureg
.file
;
262 dst
->Index
= ureg
.idx
;
263 dst
->WriteMask
= mask
;
265 dst
->CondSwizzle
= 0;
268 static struct fp_instruction
*
269 emit_op(struct texenv_fragment_program
*p
,
278 GLuint nr
= p
->program
->Base
.NumInstructions
++;
279 struct fp_instruction
*inst
= &p
->program
->Instructions
[nr
];
281 memset(inst
, 0, sizeof(*inst
));
284 emit_arg( &inst
->SrcReg
[0], src0
);
285 emit_arg( &inst
->SrcReg
[1], src1
);
286 emit_arg( &inst
->SrcReg
[2], src2
);
288 inst
->Saturate
= saturate
;
290 emit_dst( &inst
->DstReg
, dest
, mask
);
292 /* Accounting for indirection tracking:
294 if (dest
.file
== PROGRAM_TEMPORARY
)
295 p
->temps_output
|= 1 << dest
.idx
;
301 static struct ureg
emit_arith( struct texenv_fragment_program
*p
,
310 emit_op(p
, op
, dest
, mask
, saturate
, src0
, src1
, src2
);
312 /* Accounting for indirection tracking:
314 if (src0
.file
== PROGRAM_TEMPORARY
)
315 p
->alu_temps
|= 1 << src0
.idx
;
317 if (!is_undef(src1
) && src1
.file
== PROGRAM_TEMPORARY
)
318 p
->alu_temps
|= 1 << src1
.idx
;
320 if (!is_undef(src2
) && src2
.file
== PROGRAM_TEMPORARY
)
321 p
->alu_temps
|= 1 << src2
.idx
;
323 if (dest
.file
== PROGRAM_TEMPORARY
)
324 p
->alu_temps
|= 1 << dest
.idx
;
326 p
->program
->NumAluInstructions
++;
330 static struct ureg
emit_texld( struct texenv_fragment_program
*p
,
338 struct fp_instruction
*inst
= emit_op( p
, op
,
340 0, /* don't saturate? */
345 inst
->TexSrcIdx
= tex_idx
;
346 inst
->TexSrcUnit
= tex_unit
;
348 p
->program
->NumTexInstructions
++;
350 /* Is this a texture indirection?
352 if ((coord
.file
== PROGRAM_TEMPORARY
&&
353 (p
->temps_output
& (1<<coord
.idx
))) ||
354 (dest
.file
== PROGRAM_TEMPORARY
&&
355 (p
->alu_temps
& (1<<dest
.idx
)))) {
356 p
->program
->NumTexIndirections
++;
357 p
->temps_output
= 1<<coord
.idx
;
359 assert(0); /* KW: texture env crossbar */
366 static struct ureg
register_const4f( struct texenv_fragment_program
*p
,
378 idx
= _mesa_add_unnamed_constant( p
->program
->Parameters
, values
);
379 return make_ureg(PROGRAM_STATE_VAR
, idx
);
382 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
383 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
384 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
385 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
390 static struct ureg
get_one( struct texenv_fragment_program
*p
)
392 if (is_undef(p
->one
))
393 p
->one
= register_scalar_const(p
, 1.0);
397 static struct ureg
get_half( struct texenv_fragment_program
*p
)
399 if (is_undef(p
->half
))
400 p
->one
= register_scalar_const(p
, 0.5);
404 static struct ureg
get_zero( struct texenv_fragment_program
*p
)
406 if (is_undef(p
->zero
))
407 p
->one
= register_scalar_const(p
, 0.0);
415 static void program_error( struct texenv_fragment_program
*p
, const char *msg
)
417 fprintf(stderr
, "%s\n", msg
);
422 static GLuint
translate_tex_src_bit( struct texenv_fragment_program
*p
,
426 case TEXTURE_1D_BIT
: return TEXTURE_1D_INDEX
;
427 case TEXTURE_2D_BIT
: return TEXTURE_2D_INDEX
;
428 case TEXTURE_RECT_BIT
: return TEXTURE_RECT_INDEX
;
429 case TEXTURE_3D_BIT
: return TEXTURE_3D_INDEX
;
430 case TEXTURE_CUBE_BIT
: return TEXTURE_CUBE_INDEX
;
431 default: program_error(p
, "TexSrcBit"); return 0;
436 static struct ureg
get_source( struct texenv_fragment_program
*p
,
437 GLenum src
, GLuint unit
)
441 assert(!is_undef(p
->src_texture
[unit
]));
442 return p
->src_texture
[unit
];
452 assert(!is_undef(p
->src_texture
[src
- GL_TEXTURE0
]));
453 return p
->src_texture
[src
- GL_TEXTURE0
];
456 return register_param2(p
, STATE_TEXENV_COLOR
, unit
);
458 case GL_PRIMARY_COLOR
:
459 return register_input(p
, FRAG_ATTRIB_COL0
);
463 if (is_undef(p
->src_previous
))
464 return register_input(p
, FRAG_ATTRIB_COL0
);
466 return p
->src_previous
;
471 static struct ureg
emit_combine_source( struct texenv_fragment_program
*p
,
477 struct ureg arg
, src
, one
;
479 src
= get_source(p
, source
, unit
);
482 case GL_ONE_MINUS_SRC_COLOR
:
484 * Emit tmp = 1.0 - arg.xyzw
488 return emit_arith( p
, FP_OPCODE_SUB
, arg
, mask
, 0, one
, src
, undef
);
491 if (mask
== WRITEMASK_W
)
494 return swizzle1( src
, W
);
495 case GL_ONE_MINUS_SRC_ALPHA
:
497 * Emit tmp = 1.0 - arg.wwww
501 return emit_arith(p
, FP_OPCODE_SUB
, arg
, mask
, 0,
502 one
, swizzle1(src
, W
), undef
);
515 static int nr_args( GLenum mode
)
518 case GL_REPLACE
: return 1;
519 case GL_MODULATE
: return 2;
520 case GL_ADD
: return 2;
521 case GL_ADD_SIGNED
: return 2;
522 case GL_INTERPOLATE
: return 3;
523 case GL_SUBTRACT
: return 2;
524 case GL_DOT3_RGB_EXT
: return 2;
525 case GL_DOT3_RGBA_EXT
: return 2;
526 case GL_DOT3_RGB
: return 2;
527 case GL_DOT3_RGBA
: return 2;
528 case GL_MODULATE_ADD_ATI
: return 3;
529 case GL_MODULATE_SUBTRACT_ATI
: return 3;
530 case GL_MODULATE_SIGNED_ADD_ATI
: return 3;
536 static GLboolean
args_match( struct gl_texture_unit
*texUnit
)
538 int i
, nr
= nr_args(texUnit
->_CurrentCombine
->ModeRGB
);
540 for (i
= 0 ; i
< nr
; i
++) {
541 if (texUnit
->_CurrentCombine
->SourceA
[i
] != texUnit
->_CurrentCombine
->SourceRGB
[i
])
544 switch(texUnit
->_CurrentCombine
->OperandA
[i
]) {
546 switch(texUnit
->_CurrentCombine
->OperandRGB
[i
]) {
554 case GL_ONE_MINUS_SRC_ALPHA
:
555 switch(texUnit
->_CurrentCombine
->OperandRGB
[i
]) {
556 case GL_ONE_MINUS_SRC_COLOR
:
557 case GL_ONE_MINUS_SRC_ALPHA
:
564 return GL_FALSE
; /* impossible */
572 static struct ureg
emit_combine( struct texenv_fragment_program
*p
,
578 const GLenum
*source
,
579 const GLenum
*operand
)
581 int nr
= nr_args(mode
);
583 struct ureg tmp
, half
;
586 for (i
= 0; i
< nr
; i
++)
587 src
[i
] = emit_combine_source( p
, mask
, unit
, source
[i
], operand
[i
] );
591 if (mask
== WRITEMASK_XYZW
&& !saturate
)
594 return emit_arith( p
, FP_OPCODE_MOV
, dest
, mask
, saturate
, src
[0], undef
, undef
);
596 return emit_arith( p
, FP_OPCODE_MUL
, dest
, mask
, saturate
,
597 src
[0], src
[1], undef
);
599 return emit_arith( p
, FP_OPCODE_ADD
, dest
, mask
, saturate
,
600 src
[0], src
[1], undef
);
606 emit_arith( p
, FP_OPCODE_ADD
, tmp
, mask
, 0, src
[0], src
[1], undef
);
607 emit_arith( p
, FP_OPCODE_SUB
, dest
, mask
, saturate
, tmp
, half
, undef
);
610 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
612 return emit_arith( p
, FP_OPCODE_LRP
, dest
, mask
, saturate
, src
[2], src
[0], src
[1] );
615 return emit_arith( p
, FP_OPCODE_SUB
, dest
, mask
, saturate
, src
[0], src
[1], undef
);
618 case GL_DOT3_RGBA_EXT
:
619 case GL_DOT3_RGB_EXT
:
621 struct ureg tmp0
= get_temp( p
);
622 struct ureg tmp1
= get_temp( p
);
623 struct ureg neg1
= register_scalar_const(p
, -1);
624 struct ureg two
= register_scalar_const(p
, 2);
629 * dst = tmp0 dot3 tmp1
631 emit_arith( p
, FP_OPCODE_MAD
, tmp0
, WRITEMASK_XYZW
, 0,
634 if (memcmp(&src
[0], &src
[1], sizeof(struct ureg
)) == 0)
637 emit_arith( p
, FP_OPCODE_MAD
, tmp1
, WRITEMASK_XYZW
, 0,
639 emit_arith( p
, FP_OPCODE_DP3
, dest
, mask
, saturate
, tmp0
, tmp1
, undef
);
642 case GL_MODULATE_ADD_ATI
:
643 /* Arg0 * Arg2 + Arg1 */
644 return emit_arith( p
, FP_OPCODE_MAD
, dest
, mask
, saturate
,
645 src
[0], src
[2], src
[1] );
646 case GL_MODULATE_SIGNED_ADD_ATI
: {
647 /* Arg0 * Arg2 + Arg1 - 0.5 */
648 struct ureg tmp0
= get_temp(p
);
650 emit_arith( p
, FP_OPCODE_MAD
, tmp0
, mask
, 0, src
[0], src
[2], src
[1] );
651 emit_arith( p
, FP_OPCODE_SUB
, dest
, mask
, saturate
, tmp0
, half
, undef
);
654 case GL_MODULATE_SUBTRACT_ATI
:
655 /* Arg0 * Arg2 - Arg1 */
656 emit_arith( p
, FP_OPCODE_MAD
, dest
, mask
, 0, src
[0], src
[2], negate(src
[1]) );
664 static struct ureg
emit_texenv( struct texenv_fragment_program
*p
, int unit
)
666 struct gl_texture_unit
*texUnit
= &p
->ctx
->Texture
.Unit
[unit
];
667 GLuint saturate
= (unit
< p
->last_tex_stage
);
668 GLuint rgb_shift
, alpha_shift
;
669 struct ureg out
, shift
;
672 if (!texUnit
->_ReallyEnabled
) {
673 return get_source(p
, GL_PREVIOUS
, 0);
676 switch (texUnit
->_CurrentCombine
->ModeRGB
) {
677 case GL_DOT3_RGB_EXT
:
678 alpha_shift
= texUnit
->_CurrentCombine
->ScaleShiftA
;
682 case GL_DOT3_RGBA_EXT
:
688 rgb_shift
= texUnit
->_CurrentCombine
->ScaleShiftRGB
;
689 alpha_shift
= texUnit
->_CurrentCombine
->ScaleShiftA
;
693 /* If this is the very last calculation, emit direct to output reg:
695 if ((p
->ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
) ||
696 unit
!= p
->last_tex_stage
||
699 dest
= get_temp( p
);
701 dest
= make_ureg(PROGRAM_OUTPUT
, FRAG_OUTPUT_COLR
);
703 /* Emit the RGB and A combine ops
705 if (texUnit
->_CurrentCombine
->ModeRGB
== texUnit
->_CurrentCombine
->ModeA
&&
706 args_match( texUnit
)) {
707 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, saturate
,
709 texUnit
->_CurrentCombine
->ModeRGB
,
710 texUnit
->_CurrentCombine
->SourceRGB
,
711 texUnit
->_CurrentCombine
->OperandRGB
);
713 else if (texUnit
->_CurrentCombine
->ModeRGB
== GL_DOT3_RGBA_EXT
||
714 texUnit
->_CurrentCombine
->ModeRGB
== GL_DOT3_RGBA
) {
716 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, saturate
,
718 texUnit
->_CurrentCombine
->ModeRGB
,
719 texUnit
->_CurrentCombine
->SourceRGB
,
720 texUnit
->_CurrentCombine
->OperandRGB
);
723 /* Need to do something to stop from re-emitting identical
724 * argument calculations here:
726 out
= emit_combine( p
, dest
, WRITEMASK_XYZ
, saturate
,
728 texUnit
->_CurrentCombine
->ModeRGB
,
729 texUnit
->_CurrentCombine
->SourceRGB
,
730 texUnit
->_CurrentCombine
->OperandRGB
);
731 out
= emit_combine( p
, dest
, WRITEMASK_W
, saturate
,
733 texUnit
->_CurrentCombine
->ModeA
,
734 texUnit
->_CurrentCombine
->SourceA
,
735 texUnit
->_CurrentCombine
->OperandA
);
738 /* Deal with the final shift:
740 if (alpha_shift
|| rgb_shift
) {
741 if (rgb_shift
== alpha_shift
) {
742 shift
= register_scalar_const(p
, 1<<rgb_shift
);
745 shift
= register_const4f(p
,
751 return emit_arith( p
, FP_OPCODE_MUL
, dest
, WRITEMASK_XYZW
,
752 saturate
, out
, shift
, undef
);
760 static void load_texture( struct texenv_fragment_program
*p
, GLuint unit
)
762 if (is_undef(p
->src_texture
[unit
])) {
763 GLuint dim
= translate_tex_src_bit( p
, p
->ctx
->Texture
.Unit
[unit
]._ReallyEnabled
);
764 struct ureg texcoord
= register_input(p
, FRAG_ATTRIB_TEX0
+unit
);
765 struct ureg tmp
= get_tex_temp( p
);
767 /* TODO: Use D0_MASK_XY where possible.
769 p
->src_texture
[unit
] = emit_texld( p
, FP_OPCODE_TXP
,
771 unit
, dim
, texcoord
);
775 static GLboolean
load_texenv_source( struct texenv_fragment_program
*p
,
776 GLenum src
, GLuint unit
)
780 load_texture(p
, unit
);
791 if (!p
->ctx
->Texture
.Unit
[src
- GL_TEXTURE0
]._ReallyEnabled
)
793 load_texture(p
, src
- GL_TEXTURE0
);
803 static GLboolean
load_texunit_sources( struct texenv_fragment_program
*p
, int unit
)
805 struct gl_texture_unit
*texUnit
= &p
->ctx
->Texture
.Unit
[unit
];
806 int i
, nr
= nr_args(texUnit
->_CurrentCombine
->ModeRGB
);
807 for (i
= 0; i
< nr
; i
++) {
808 if (!load_texenv_source( p
, texUnit
->_CurrentCombine
->SourceRGB
[i
], unit
) ||
809 !load_texenv_source( p
, texUnit
->_CurrentCombine
->SourceA
[i
], unit
))
815 void _mesa_UpdateTexEnvProgram( GLcontext
*ctx
)
817 struct texenv_fragment_program p
;
820 GLuint db_NumInstructions
= 0;
821 struct fp_instruction
*db_Instructions
= NULL
;
823 if (ctx
->FragmentProgram
._Enabled
)
826 if (!ctx
->_TexEnvProgram
)
827 ctx
->FragmentProgram
._Current
= ctx
->_TexEnvProgram
=
828 (struct fragment_program
*)
829 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
831 _mesa_memset(&p
, 0, sizeof(p
));
833 p
.program
= ctx
->_TexEnvProgram
;
835 if (ctx
->Driver
.ProgramStringNotify
|| DISASSEM
) {
836 db_Instructions
= p
.program
->Instructions
;
837 db_NumInstructions
= p
.program
->Base
.NumInstructions
;
838 p
.program
->Instructions
= NULL
;
841 if (!p
.program
->Instructions
)
842 p
.program
->Instructions
= MALLOC(sizeof(struct fp_instruction
) * 100);
844 p
.program
->Base
.NumInstructions
= 0;
845 p
.program
->Base
.Target
= GL_FRAGMENT_PROGRAM_ARB
;
846 p
.program
->NumTexIndirections
= 1; /* correct? */
847 p
.program
->NumTexInstructions
= 0;
848 p
.program
->NumAluInstructions
= 0;
849 p
.program
->Base
.String
= 0;
850 p
.program
->Base
.NumInstructions
=
851 p
.program
->Base
.NumTemporaries
=
852 p
.program
->Base
.NumParameters
=
853 p
.program
->Base
.NumAttributes
= p
.program
->Base
.NumAddressRegs
= 0;
855 if (p
.program
->Parameters
)
856 _mesa_free_parameters(p
.program
->Parameters
);
858 p
.program
->Parameters
= _mesa_new_parameter_list();
860 p
.program
->InputsRead
= 0;
861 p
.program
->OutputsWritten
= 1 << FRAG_OUTPUT_COLR
;
863 for (unit
= 0; unit
< MAX_TEXTURE_UNITS
; unit
++)
864 p
.src_texture
[unit
] = undef
;
866 p
.src_previous
= undef
;
867 p
.last_tex_stage
= 0;
870 if (ctx
->Texture
._EnabledUnits
) {
871 GLuint tex_env_enabled
= 0;
873 /* First pass - to support texture_env_crossbar, first identify
874 * all referenced texture sources and emit texld instructions
877 for (unit
= 0 ; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
878 if (ctx
->Texture
.Unit
[unit
]._ReallyEnabled
) {
879 if (load_texunit_sources( &p
, unit
)) {
880 tex_env_enabled
|= 1<<unit
;
881 p
.last_tex_stage
= unit
;
885 /* Second pass - emit combine instructions to build final color:
887 for (unit
= 0 ; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
888 if (tex_env_enabled
& (1<<unit
)) {
889 p
.src_previous
= emit_texenv( &p
, unit
);
890 release_temps(&p
); /* release all temps */
894 cf
= get_source( &p
, GL_PREVIOUS
, 0 );
895 out
= make_ureg( PROGRAM_OUTPUT
, FRAG_OUTPUT_COLR
);
897 if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
) {
898 /* Emit specular add.
900 struct ureg s
= register_input(&p
, FRAG_ATTRIB_COL1
);
901 emit_arith( &p
, FP_OPCODE_ADD
, out
, WRITEMASK_XYZ
, 0, cf
, s
, undef
);
903 else if (memcmp(&cf
, &out
, sizeof(cf
)) != 0) {
904 /* Will wind up in here if no texture enabled or a couple of
905 * other scenarios (GL_REPLACE for instance).
907 emit_arith( &p
, FP_OPCODE_MOV
, out
, WRITEMASK_XYZW
, 0, cf
, undef
, undef
);
912 emit_arith( &p
, FP_OPCODE_END
, undef
, WRITEMASK_XYZW
, 0, undef
, undef
, undef
);
914 if (ctx
->Fog
.Enabled
)
915 p
.program
->FogOption
= ctx
->Fog
.Mode
;
917 p
.program
->FogOption
= GL_NONE
;
919 if (p
.program
->NumTexIndirections
> ctx
->Const
.MaxFragmentProgramTexIndirections
)
920 program_error(&p
, "Exceeded max nr indirect texture lookups");
922 if (p
.program
->NumTexInstructions
> ctx
->Const
.MaxFragmentProgramTexInstructions
)
923 program_error(&p
, "Exceeded max TEX instructions");
925 if (p
.program
->NumAluInstructions
> ctx
->Const
.MaxFragmentProgramAluInstructions
)
926 program_error(&p
, "Exceeded max ALU instructions");
929 /* Notify driver the fragment program has (actually) changed.
931 if (ctx
->Driver
.ProgramStringNotify
|| DISASSEM
) {
932 if (db_Instructions
== NULL
||
933 db_NumInstructions
!= p
.program
->Base
.NumInstructions
||
934 memcmp(db_Instructions
, p
.program
->Instructions
,
935 db_NumInstructions
* sizeof(*db_Instructions
)) != 0) {
937 if (ctx
->Driver
.ProgramStringNotify
)
938 ctx
->Driver
.ProgramStringNotify( ctx
, GL_FRAGMENT_PROGRAM_ARB
,
942 _mesa_debug_fp_inst(p
.program
->NumTexInstructions
+ p
.program
->NumAluInstructions
,
943 p
.program
->Instructions
);
948 FREE(db_Instructions
);