1 /**************************************************************************
3 * Copyright 2007 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 "shader/prog_parameter.h"
32 #include "shader/prog_cache.h"
33 #include "shader/prog_instruction.h"
34 #include "shader/prog_print.h"
35 #include "shader/prog_statevars.h"
36 #include "shader/programopt.h"
37 #include "texenvprogram.h"
40 * Up to nine instructions per tex unit, plus fog, specular color.
42 #define MAX_INSTRUCTIONS ((MAX_TEXTURE_UNITS * 9) + 12)
44 #define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
52 GLbitfield enabled_units
;
53 GLuint separate_specular
:1;
59 GLuint source_index
:3; /* one of TEXTURE_1D/2D/3D/CUBE/RECT_INDEX */
60 GLuint ScaleShiftRGB
:2;
65 struct mode_opt OptRGB
[3];
69 struct mode_opt OptA
[3];
78 static GLuint
translate_fog_mode( GLenum mode
)
81 case GL_LINEAR
: return FOG_LINEAR
;
82 case GL_EXP
: return FOG_EXP
;
83 case GL_EXP2
: return FOG_EXP2
;
84 default: return FOG_UNKNOWN
;
88 #define OPR_SRC_COLOR 0
89 #define OPR_ONE_MINUS_SRC_COLOR 1
90 #define OPR_SRC_ALPHA 2
91 #define OPR_ONE_MINUS_SRC_ALPHA 3
96 static GLuint
translate_operand( GLenum operand
)
99 case GL_SRC_COLOR
: return OPR_SRC_COLOR
;
100 case GL_ONE_MINUS_SRC_COLOR
: return OPR_ONE_MINUS_SRC_COLOR
;
101 case GL_SRC_ALPHA
: return OPR_SRC_ALPHA
;
102 case GL_ONE_MINUS_SRC_ALPHA
: return OPR_ONE_MINUS_SRC_ALPHA
;
103 case GL_ZERO
: return OPR_ZERO
;
104 case GL_ONE
: return OPR_ONE
;
105 default: return OPR_UNKNOWN
;
109 #define SRC_TEXTURE 0
110 #define SRC_TEXTURE0 1
111 #define SRC_TEXTURE1 2
112 #define SRC_TEXTURE2 3
113 #define SRC_TEXTURE3 4
114 #define SRC_TEXTURE4 5
115 #define SRC_TEXTURE5 6
116 #define SRC_TEXTURE6 7
117 #define SRC_TEXTURE7 8
118 #define SRC_CONSTANT 9
119 #define SRC_PRIMARY_COLOR 10
120 #define SRC_PREVIOUS 11
121 #define SRC_UNKNOWN 15
123 static GLuint
translate_source( GLenum src
)
126 case GL_TEXTURE
: return SRC_TEXTURE
;
134 case GL_TEXTURE7
: return SRC_TEXTURE0
+ (src
- GL_TEXTURE0
);
135 case GL_CONSTANT
: return SRC_CONSTANT
;
136 case GL_PRIMARY_COLOR
: return SRC_PRIMARY_COLOR
;
137 case GL_PREVIOUS
: return SRC_PREVIOUS
;
138 default: return SRC_UNKNOWN
;
142 #define MODE_REPLACE 0
143 #define MODE_MODULATE 1
145 #define MODE_ADD_SIGNED 3
146 #define MODE_INTERPOLATE 4
147 #define MODE_SUBTRACT 5
148 #define MODE_DOT3_RGB 6
149 #define MODE_DOT3_RGB_EXT 7
150 #define MODE_DOT3_RGBA 8
151 #define MODE_DOT3_RGBA_EXT 9
152 #define MODE_MODULATE_ADD_ATI 10
153 #define MODE_MODULATE_SIGNED_ADD_ATI 11
154 #define MODE_MODULATE_SUBTRACT_ATI 12
155 #define MODE_UNKNOWN 15
157 static GLuint
translate_mode( GLenum mode
)
160 case GL_REPLACE
: return MODE_REPLACE
;
161 case GL_MODULATE
: return MODE_MODULATE
;
162 case GL_ADD
: return MODE_ADD
;
163 case GL_ADD_SIGNED
: return MODE_ADD_SIGNED
;
164 case GL_INTERPOLATE
: return MODE_INTERPOLATE
;
165 case GL_SUBTRACT
: return MODE_SUBTRACT
;
166 case GL_DOT3_RGB
: return MODE_DOT3_RGB
;
167 case GL_DOT3_RGB_EXT
: return MODE_DOT3_RGB_EXT
;
168 case GL_DOT3_RGBA
: return MODE_DOT3_RGBA
;
169 case GL_DOT3_RGBA_EXT
: return MODE_DOT3_RGBA_EXT
;
170 case GL_MODULATE_ADD_ATI
: return MODE_MODULATE_ADD_ATI
;
171 case GL_MODULATE_SIGNED_ADD_ATI
: return MODE_MODULATE_SIGNED_ADD_ATI
;
172 case GL_MODULATE_SUBTRACT_ATI
: return MODE_MODULATE_SUBTRACT_ATI
;
173 default: return MODE_UNKNOWN
;
177 #define TEXTURE_UNKNOWN_INDEX 7
178 static GLuint
translate_tex_src_bit( GLbitfield bit
)
181 case TEXTURE_1D_BIT
: return TEXTURE_1D_INDEX
;
182 case TEXTURE_2D_BIT
: return TEXTURE_2D_INDEX
;
183 case TEXTURE_RECT_BIT
: return TEXTURE_RECT_INDEX
;
184 case TEXTURE_3D_BIT
: return TEXTURE_3D_INDEX
;
185 case TEXTURE_CUBE_BIT
: return TEXTURE_CUBE_INDEX
;
186 default: return TEXTURE_UNKNOWN_INDEX
;
191 * Examine current texture environment state and generate a unique
192 * key to identify it.
194 static void make_state_key( GLcontext
*ctx
, struct state_key
*key
)
198 memset(key
, 0, sizeof(*key
));
200 for (i
=0;i
<MAX_TEXTURE_UNITS
;i
++) {
201 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
203 if (!texUnit
->_ReallyEnabled
)
206 key
->unit
[i
].enabled
= 1;
207 key
->enabled_units
|= (1<<i
);
209 key
->unit
[i
].source_index
=
210 translate_tex_src_bit(texUnit
->_ReallyEnabled
);
212 key
->unit
[i
].NumArgsRGB
= texUnit
->_CurrentCombine
->_NumArgsRGB
;
213 key
->unit
[i
].NumArgsA
= texUnit
->_CurrentCombine
->_NumArgsA
;
215 key
->unit
[i
].ModeRGB
=
216 translate_mode(texUnit
->_CurrentCombine
->ModeRGB
);
218 translate_mode(texUnit
->_CurrentCombine
->ModeA
);
220 key
->unit
[i
].ScaleShiftRGB
= texUnit
->_CurrentCombine
->ScaleShiftRGB
;
221 key
->unit
[i
].ScaleShiftA
= texUnit
->_CurrentCombine
->ScaleShiftA
;
224 key
->unit
[i
].OptRGB
[j
].Operand
=
225 translate_operand(texUnit
->_CurrentCombine
->OperandRGB
[j
]);
226 key
->unit
[i
].OptA
[j
].Operand
=
227 translate_operand(texUnit
->_CurrentCombine
->OperandA
[j
]);
228 key
->unit
[i
].OptRGB
[j
].Source
=
229 translate_source(texUnit
->_CurrentCombine
->SourceRGB
[j
]);
230 key
->unit
[i
].OptA
[j
].Source
=
231 translate_source(texUnit
->_CurrentCombine
->SourceA
[j
]);
235 if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
)
236 key
->separate_specular
= 1;
238 if (ctx
->Fog
.Enabled
) {
239 key
->fog_enabled
= 1;
240 key
->fog_mode
= translate_fog_mode(ctx
->Fog
.Mode
);
244 /* Use uregs to represent registers internally, translate to Mesa's
245 * expected formats on emit.
247 * NOTE: These are passed by value extensively in this file rather
248 * than as usual by pointer reference. If this disturbs you, try
249 * remembering they are just 32bits in size.
251 * GCC is smart enough to deal with these dword-sized structures in
252 * much the same way as if I had defined them as dwords and was using
253 * macros to access and set the fields. This is much nicer and easier
266 static const struct ureg undef
= {
277 /* State used to build the fragment program:
279 struct texenv_fragment_program
{
280 struct gl_fragment_program
*program
;
282 struct state_key
*state
;
284 GLbitfield alu_temps
; /* Track texture indirections, see spec. */
285 GLbitfield temps_output
; /* Track texture indirections, see spec. */
286 GLbitfield temp_in_use
; /* Tracks temporary regs which are in use. */
289 struct ureg src_texture
[MAX_TEXTURE_UNITS
];
290 /* Reg containing each texture unit's sampled texture color,
294 struct ureg src_previous
; /* Reg containing color from previous
295 * stage. May need to be decl'd.
298 GLuint last_tex_stage
; /* Number of last enabled texture unit */
307 static struct ureg
make_ureg(GLuint file
, GLuint idx
)
315 reg
.swz
= SWIZZLE_NOOP
;
320 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
322 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
325 GET_SWZ(reg
.swz
, w
));
330 static struct ureg
swizzle1( struct ureg reg
, int x
)
332 return swizzle(reg
, x
, x
, x
, x
);
335 static struct ureg
negate( struct ureg reg
)
341 static GLboolean
is_undef( struct ureg reg
)
343 return reg
.file
== PROGRAM_UNDEFINED
;
347 static struct ureg
get_temp( struct texenv_fragment_program
*p
)
351 /* First try and reuse temps which have been used already:
353 bit
= _mesa_ffs( ~p
->temp_in_use
& p
->alu_temps
);
355 /* Then any unused temporary:
358 bit
= _mesa_ffs( ~p
->temp_in_use
);
361 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
365 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
366 p
->program
->Base
.NumTemporaries
= bit
;
368 p
->temp_in_use
|= 1<<(bit
-1);
369 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
372 static struct ureg
get_tex_temp( struct texenv_fragment_program
*p
)
376 /* First try to find available temp not previously used (to avoid
377 * starting a new texture indirection). According to the spec, the
378 * ~p->temps_output isn't necessary, but will keep it there for
381 bit
= _mesa_ffs( ~p
->temp_in_use
& ~p
->alu_temps
& ~p
->temps_output
);
383 /* Then any unused temporary:
386 bit
= _mesa_ffs( ~p
->temp_in_use
);
389 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
393 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
394 p
->program
->Base
.NumTemporaries
= bit
;
396 p
->temp_in_use
|= 1<<(bit
-1);
397 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
401 /** Mark a temp reg as being no longer allocatable. */
402 static void reserve_temp( struct texenv_fragment_program
*p
, struct ureg r
)
404 if (r
.file
== PROGRAM_TEMPORARY
)
405 p
->temps_output
|= (1 << r
.idx
);
409 static void release_temps(GLcontext
*ctx
, struct texenv_fragment_program
*p
)
411 GLuint max_temp
= ctx
->Const
.FragmentProgram
.MaxTemps
;
413 /* KW: To support tex_env_crossbar, don't release the registers in
416 if (max_temp
>= sizeof(int) * 8)
417 p
->temp_in_use
= p
->temps_output
;
419 p
->temp_in_use
= ~((1<<max_temp
)-1) | p
->temps_output
;
423 static struct ureg
register_param5( struct texenv_fragment_program
*p
,
430 gl_state_index tokens
[STATE_LENGTH
];
437 idx
= _mesa_add_state_reference( p
->program
->Base
.Parameters
, tokens
);
438 return make_ureg(PROGRAM_STATE_VAR
, idx
);
442 #define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
443 #define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
444 #define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
445 #define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
448 static struct ureg
register_input( struct texenv_fragment_program
*p
, GLuint input
)
450 p
->program
->Base
.InputsRead
|= (1 << input
);
451 return make_ureg(PROGRAM_INPUT
, input
);
455 static void emit_arg( struct prog_src_register
*reg
,
458 reg
->File
= ureg
.file
;
459 reg
->Index
= ureg
.idx
;
460 reg
->Swizzle
= ureg
.swz
;
461 reg
->NegateBase
= ureg
.negatebase
? 0xf : 0x0;
463 reg
->NegateAbs
= ureg
.negateabs
;
466 static void emit_dst( struct prog_dst_register
*dst
,
467 struct ureg ureg
, GLuint mask
)
469 dst
->File
= ureg
.file
;
470 dst
->Index
= ureg
.idx
;
471 dst
->WriteMask
= mask
;
472 dst
->CondMask
= COND_TR
; /* always pass cond test */
473 dst
->CondSwizzle
= SWIZZLE_NOOP
;
476 static struct prog_instruction
*
477 emit_op(struct texenv_fragment_program
*p
,
486 GLuint nr
= p
->program
->Base
.NumInstructions
++;
487 struct prog_instruction
*inst
= &p
->program
->Base
.Instructions
[nr
];
489 assert(nr
< MAX_INSTRUCTIONS
);
491 _mesa_init_instructions(inst
, 1);
494 emit_arg( &inst
->SrcReg
[0], src0
);
495 emit_arg( &inst
->SrcReg
[1], src1
);
496 emit_arg( &inst
->SrcReg
[2], src2
);
498 inst
->SaturateMode
= saturate
? SATURATE_ZERO_ONE
: SATURATE_OFF
;
500 emit_dst( &inst
->DstReg
, dest
, mask
);
503 /* Accounting for indirection tracking:
505 if (dest
.file
== PROGRAM_TEMPORARY
)
506 p
->temps_output
|= 1 << dest
.idx
;
513 static struct ureg
emit_arith( struct texenv_fragment_program
*p
,
522 emit_op(p
, op
, dest
, mask
, saturate
, src0
, src1
, src2
);
524 /* Accounting for indirection tracking:
526 if (src0
.file
== PROGRAM_TEMPORARY
)
527 p
->alu_temps
|= 1 << src0
.idx
;
529 if (!is_undef(src1
) && src1
.file
== PROGRAM_TEMPORARY
)
530 p
->alu_temps
|= 1 << src1
.idx
;
532 if (!is_undef(src2
) && src2
.file
== PROGRAM_TEMPORARY
)
533 p
->alu_temps
|= 1 << src2
.idx
;
535 if (dest
.file
== PROGRAM_TEMPORARY
)
536 p
->alu_temps
|= 1 << dest
.idx
;
538 p
->program
->Base
.NumAluInstructions
++;
542 static struct ureg
emit_texld( struct texenv_fragment_program
*p
,
550 struct prog_instruction
*inst
= emit_op( p
, op
,
552 GL_FALSE
, /* don't saturate? */
557 inst
->TexSrcTarget
= tex_idx
;
558 inst
->TexSrcUnit
= tex_unit
;
560 p
->program
->Base
.NumTexInstructions
++;
562 /* Accounting for indirection tracking:
564 reserve_temp(p
, dest
);
566 /* Is this a texture indirection?
568 if ((coord
.file
== PROGRAM_TEMPORARY
&&
569 (p
->temps_output
& (1<<coord
.idx
))) ||
570 (dest
.file
== PROGRAM_TEMPORARY
&&
571 (p
->alu_temps
& (1<<dest
.idx
)))) {
572 p
->program
->Base
.NumTexIndirections
++;
573 p
->temps_output
= 1<<coord
.idx
;
575 assert(0); /* KW: texture env crossbar */
582 static struct ureg
register_const4f( struct texenv_fragment_program
*p
,
595 idx
= _mesa_add_unnamed_constant( p
->program
->Base
.Parameters
, values
, 4,
597 r
= make_ureg(PROGRAM_CONSTANT
, idx
);
602 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
603 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
604 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
605 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
608 static struct ureg
get_one( struct texenv_fragment_program
*p
)
610 if (is_undef(p
->one
))
611 p
->one
= register_scalar_const(p
, 1.0);
615 static struct ureg
get_half( struct texenv_fragment_program
*p
)
617 if (is_undef(p
->half
))
618 p
->half
= register_scalar_const(p
, 0.5);
622 static struct ureg
get_zero( struct texenv_fragment_program
*p
)
624 if (is_undef(p
->zero
))
625 p
->zero
= register_scalar_const(p
, 0.0);
630 static void program_error( struct texenv_fragment_program
*p
, const char *msg
)
632 _mesa_problem(NULL
, msg
);
636 static struct ureg
get_source( struct texenv_fragment_program
*p
,
637 GLuint src
, GLuint unit
)
641 assert(!is_undef(p
->src_texture
[unit
]));
642 return p
->src_texture
[unit
];
652 assert(!is_undef(p
->src_texture
[src
- SRC_TEXTURE0
]));
653 return p
->src_texture
[src
- SRC_TEXTURE0
];
656 return register_param2(p
, STATE_TEXENV_COLOR
, unit
);
658 case SRC_PRIMARY_COLOR
:
659 return register_input(p
, FRAG_ATTRIB_COL0
);
663 if (is_undef(p
->src_previous
))
664 return register_input(p
, FRAG_ATTRIB_COL0
);
666 return p
->src_previous
;
670 static struct ureg
emit_combine_source( struct texenv_fragment_program
*p
,
676 struct ureg arg
, src
, one
;
678 src
= get_source(p
, source
, unit
);
681 case OPR_ONE_MINUS_SRC_COLOR
:
683 * Emit tmp = 1.0 - arg.xyzw
687 return emit_arith( p
, OPCODE_SUB
, arg
, mask
, 0, one
, src
, undef
);
690 if (mask
== WRITEMASK_W
)
693 return swizzle1( src
, SWIZZLE_W
);
694 case OPR_ONE_MINUS_SRC_ALPHA
:
696 * Emit tmp = 1.0 - arg.wwww
700 return emit_arith(p
, OPCODE_SUB
, arg
, mask
, 0,
701 one
, swizzle1(src
, SWIZZLE_W
), undef
);
712 static GLboolean
args_match( struct state_key
*key
, GLuint unit
)
714 GLuint i
, nr
= key
->unit
[unit
].NumArgsRGB
;
716 for (i
= 0 ; i
< nr
; i
++) {
717 if (key
->unit
[unit
].OptA
[i
].Source
!= key
->unit
[unit
].OptRGB
[i
].Source
)
720 switch(key
->unit
[unit
].OptA
[i
].Operand
) {
722 switch(key
->unit
[unit
].OptRGB
[i
].Operand
) {
730 case OPR_ONE_MINUS_SRC_ALPHA
:
731 switch(key
->unit
[unit
].OptRGB
[i
].Operand
) {
732 case OPR_ONE_MINUS_SRC_COLOR
:
733 case OPR_ONE_MINUS_SRC_ALPHA
:
740 return GL_FALSE
; /* impossible */
747 static struct ureg
emit_combine( struct texenv_fragment_program
*p
,
754 const struct mode_opt
*opt
)
757 struct ureg tmp
, half
;
760 tmp
= undef
; /* silence warning (bug 5318) */
762 for (i
= 0; i
< nr
; i
++)
763 src
[i
] = emit_combine_source( p
, mask
, unit
, opt
[i
].Source
, opt
[i
].Operand
);
767 if (mask
== WRITEMASK_XYZW
&& !saturate
)
770 return emit_arith( p
, OPCODE_MOV
, dest
, mask
, saturate
, src
[0], undef
, undef
);
772 return emit_arith( p
, OPCODE_MUL
, dest
, mask
, saturate
,
773 src
[0], src
[1], undef
);
775 return emit_arith( p
, OPCODE_ADD
, dest
, mask
, saturate
,
776 src
[0], src
[1], undef
);
777 case MODE_ADD_SIGNED
:
783 emit_arith( p
, OPCODE_ADD
, tmp
, mask
, 0, src
[0], src
[1], undef
);
784 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp
, half
, undef
);
786 case MODE_INTERPOLATE
:
787 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
789 return emit_arith( p
, OPCODE_LRP
, dest
, mask
, saturate
, src
[2], src
[0], src
[1] );
792 return emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, src
[0], src
[1], undef
);
795 case MODE_DOT3_RGBA_EXT
:
796 case MODE_DOT3_RGB_EXT
:
797 case MODE_DOT3_RGB
: {
798 struct ureg tmp0
= get_temp( p
);
799 struct ureg tmp1
= get_temp( p
);
800 struct ureg neg1
= register_scalar_const(p
, -1);
801 struct ureg two
= register_scalar_const(p
, 2);
806 * dst = tmp0 dot3 tmp1
808 emit_arith( p
, OPCODE_MAD
, tmp0
, WRITEMASK_XYZW
, 0,
811 if (_mesa_memcmp(&src
[0], &src
[1], sizeof(struct ureg
)) == 0)
814 emit_arith( p
, OPCODE_MAD
, tmp1
, WRITEMASK_XYZW
, 0,
816 emit_arith( p
, OPCODE_DP3
, dest
, mask
, saturate
, tmp0
, tmp1
, undef
);
819 case MODE_MODULATE_ADD_ATI
:
820 /* Arg0 * Arg2 + Arg1 */
821 return emit_arith( p
, OPCODE_MAD
, dest
, mask
, saturate
,
822 src
[0], src
[2], src
[1] );
823 case MODE_MODULATE_SIGNED_ADD_ATI
: {
824 /* Arg0 * Arg2 + Arg1 - 0.5 */
825 struct ureg tmp0
= get_temp(p
);
827 emit_arith( p
, OPCODE_MAD
, tmp0
, mask
, 0, src
[0], src
[2], src
[1] );
828 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp0
, half
, undef
);
831 case MODE_MODULATE_SUBTRACT_ATI
:
832 /* Arg0 * Arg2 - Arg1 */
833 emit_arith( p
, OPCODE_MAD
, dest
, mask
, 0, src
[0], src
[2], negate(src
[1]) );
842 * Generate instructions for one texture unit's env/combiner mode.
845 emit_texenv(struct texenv_fragment_program
*p
, GLuint unit
)
847 struct state_key
*key
= p
->state
;
848 GLboolean saturate
= (unit
< p
->last_tex_stage
);
849 GLuint rgb_shift
, alpha_shift
;
850 struct ureg out
, shift
;
853 if (!key
->unit
[unit
].enabled
) {
854 return get_source(p
, SRC_PREVIOUS
, 0);
857 switch (key
->unit
[unit
].ModeRGB
) {
858 case MODE_DOT3_RGB_EXT
:
859 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
862 case MODE_DOT3_RGBA_EXT
:
867 rgb_shift
= key
->unit
[unit
].ScaleShiftRGB
;
868 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
872 /* If this is the very last calculation, emit direct to output reg:
874 if (key
->separate_specular
||
875 unit
!= p
->last_tex_stage
||
878 dest
= get_temp( p
);
880 dest
= make_ureg(PROGRAM_OUTPUT
, FRAG_RESULT_COLR
);
882 /* Emit the RGB and A combine ops
884 if (key
->unit
[unit
].ModeRGB
== key
->unit
[unit
].ModeA
&&
885 args_match(key
, unit
)) {
886 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, saturate
,
888 key
->unit
[unit
].NumArgsRGB
,
889 key
->unit
[unit
].ModeRGB
,
890 key
->unit
[unit
].OptRGB
);
892 else if (key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA_EXT
||
893 key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA
) {
895 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, saturate
,
897 key
->unit
[unit
].NumArgsRGB
,
898 key
->unit
[unit
].ModeRGB
,
899 key
->unit
[unit
].OptRGB
);
902 /* Need to do something to stop from re-emitting identical
903 * argument calculations here:
905 out
= emit_combine( p
, dest
, WRITEMASK_XYZ
, saturate
,
907 key
->unit
[unit
].NumArgsRGB
,
908 key
->unit
[unit
].ModeRGB
,
909 key
->unit
[unit
].OptRGB
);
910 out
= emit_combine( p
, dest
, WRITEMASK_W
, saturate
,
912 key
->unit
[unit
].NumArgsA
,
913 key
->unit
[unit
].ModeA
,
914 key
->unit
[unit
].OptA
);
917 /* Deal with the final shift:
919 if (alpha_shift
|| rgb_shift
) {
920 if (rgb_shift
== alpha_shift
) {
921 shift
= register_scalar_const(p
, (GLfloat
)(1<<rgb_shift
));
924 shift
= register_const4f(p
,
925 (GLfloat
)(1<<rgb_shift
),
926 (GLfloat
)(1<<rgb_shift
),
927 (GLfloat
)(1<<rgb_shift
),
928 (GLfloat
)(1<<alpha_shift
));
930 return emit_arith( p
, OPCODE_MUL
, dest
, WRITEMASK_XYZW
,
931 saturate
, out
, shift
, undef
);
939 * Generate instruction for getting a texture source term.
941 static void load_texture( struct texenv_fragment_program
*p
, GLuint unit
)
943 if (is_undef(p
->src_texture
[unit
])) {
944 GLuint dim
= p
->state
->unit
[unit
].source_index
;
945 struct ureg texcoord
= register_input(p
, FRAG_ATTRIB_TEX0
+unit
);
946 struct ureg tmp
= get_tex_temp( p
);
948 if (dim
== TEXTURE_UNKNOWN_INDEX
)
949 program_error(p
, "TexSrcBit");
951 /* TODO: Use D0_MASK_XY where possible.
953 if (p
->state
->unit
[unit
].enabled
) {
954 p
->src_texture
[unit
] = emit_texld( p
, OPCODE_TXP
,
956 unit
, dim
, texcoord
);
957 p
->program
->Base
.SamplersUsed
|= (1 << unit
);
958 /* This identity mapping should already be in place
959 * (see _mesa_init_program_struct()) but let's be safe.
961 p
->program
->Base
.SamplerUnits
[unit
] = unit
;
964 p
->src_texture
[unit
] = get_zero(p
);
968 static GLboolean
load_texenv_source( struct texenv_fragment_program
*p
,
969 GLuint src
, GLuint unit
)
973 load_texture(p
, unit
);
984 load_texture(p
, src
- SRC_TEXTURE0
);
996 * Generate instructions for loading all texture source terms.
999 load_texunit_sources( struct texenv_fragment_program
*p
, int unit
)
1001 struct state_key
*key
= p
->state
;
1004 for (i
= 0; i
< key
->unit
[unit
].NumArgsRGB
; i
++) {
1005 load_texenv_source( p
, key
->unit
[unit
].OptRGB
[i
].Source
, unit
);
1008 for (i
= 0; i
< key
->unit
[unit
].NumArgsA
; i
++) {
1009 load_texenv_source( p
, key
->unit
[unit
].OptA
[i
].Source
, unit
);
1017 * Generate a new fragment program which implements the context's
1018 * current texture env/combine mode.
1021 create_new_program(GLcontext
*ctx
, struct state_key
*key
,
1022 struct gl_fragment_program
*program
)
1024 struct prog_instruction instBuffer
[MAX_INSTRUCTIONS
];
1025 struct texenv_fragment_program p
;
1027 struct ureg cf
, out
;
1029 _mesa_memset(&p
, 0, sizeof(p
));
1032 p
.program
= program
;
1034 /* During code generation, use locally-allocated instruction buffer,
1035 * then alloc dynamic storage below.
1037 p
.program
->Base
.Instructions
= instBuffer
;
1038 p
.program
->Base
.Target
= GL_FRAGMENT_PROGRAM_ARB
;
1039 p
.program
->Base
.NumTexIndirections
= 1; /* correct? */
1040 p
.program
->Base
.NumTexInstructions
= 0;
1041 p
.program
->Base
.NumAluInstructions
= 0;
1042 p
.program
->Base
.String
= NULL
;
1043 p
.program
->Base
.NumInstructions
=
1044 p
.program
->Base
.NumTemporaries
=
1045 p
.program
->Base
.NumParameters
=
1046 p
.program
->Base
.NumAttributes
= p
.program
->Base
.NumAddressRegs
= 0;
1047 p
.program
->Base
.Parameters
= _mesa_new_parameter_list();
1049 p
.program
->Base
.InputsRead
= 0;
1050 p
.program
->Base
.OutputsWritten
= 1 << FRAG_RESULT_COLR
;
1052 for (unit
= 0; unit
< MAX_TEXTURE_UNITS
; unit
++)
1053 p
.src_texture
[unit
] = undef
;
1055 p
.src_previous
= undef
;
1060 p
.last_tex_stage
= 0;
1061 release_temps(ctx
, &p
);
1063 if (key
->enabled_units
) {
1064 /* First pass - to support texture_env_crossbar, first identify
1065 * all referenced texture sources and emit texld instructions
1068 for (unit
= 0 ; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
1069 if (key
->unit
[unit
].enabled
) {
1070 load_texunit_sources( &p
, unit
);
1071 p
.last_tex_stage
= unit
;
1074 /* Second pass - emit combine instructions to build final color:
1076 for (unit
= 0 ; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
1077 if (key
->enabled_units
& (1<<unit
)) {
1078 p
.src_previous
= emit_texenv( &p
, unit
);
1079 reserve_temp(&p
, p
.src_previous
); /* don't re-use this temp reg */
1080 release_temps(ctx
, &p
); /* release all temps */
1084 cf
= get_source( &p
, SRC_PREVIOUS
, 0 );
1085 out
= make_ureg( PROGRAM_OUTPUT
, FRAG_RESULT_COLR
);
1087 if (key
->separate_specular
) {
1088 /* Emit specular add.
1090 struct ureg s
= register_input(&p
, FRAG_ATTRIB_COL1
);
1091 emit_arith( &p
, OPCODE_ADD
, out
, WRITEMASK_XYZ
, 0, cf
, s
, undef
);
1092 emit_arith( &p
, OPCODE_MOV
, out
, WRITEMASK_W
, 0, cf
, undef
, undef
);
1094 else if (_mesa_memcmp(&cf
, &out
, sizeof(cf
)) != 0) {
1095 /* Will wind up in here if no texture enabled or a couple of
1096 * other scenarios (GL_REPLACE for instance).
1098 emit_arith( &p
, OPCODE_MOV
, out
, WRITEMASK_XYZW
, 0, cf
, undef
, undef
);
1103 emit_arith( &p
, OPCODE_END
, undef
, WRITEMASK_XYZW
, 0, undef
, undef
, undef
);
1105 if (key
->fog_enabled
) {
1106 /* Pull fog mode from GLcontext, the value in the state key is
1107 * a reduced value and not what is expected in FogOption
1109 p
.program
->FogOption
= ctx
->Fog
.Mode
;
1110 p
.program
->Base
.InputsRead
|= FRAG_BIT_FOGC
; /* XXX new */
1112 p
.program
->FogOption
= GL_NONE
;
1114 if (p
.program
->Base
.NumTexIndirections
> ctx
->Const
.FragmentProgram
.MaxTexIndirections
)
1115 program_error(&p
, "Exceeded max nr indirect texture lookups");
1117 if (p
.program
->Base
.NumTexInstructions
> ctx
->Const
.FragmentProgram
.MaxTexInstructions
)
1118 program_error(&p
, "Exceeded max TEX instructions");
1120 if (p
.program
->Base
.NumAluInstructions
> ctx
->Const
.FragmentProgram
.MaxAluInstructions
)
1121 program_error(&p
, "Exceeded max ALU instructions");
1123 ASSERT(p
.program
->Base
.NumInstructions
<= MAX_INSTRUCTIONS
);
1125 /* Allocate final instruction array */
1126 p
.program
->Base
.Instructions
1127 = _mesa_alloc_instructions(p
.program
->Base
.NumInstructions
);
1128 if (!p
.program
->Base
.Instructions
) {
1129 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1130 "generating tex env program");
1133 _mesa_copy_instructions(p
.program
->Base
.Instructions
, instBuffer
,
1134 p
.program
->Base
.NumInstructions
);
1136 if (p
.program
->FogOption
) {
1137 _mesa_append_fog_code(ctx
, p
.program
);
1138 p
.program
->FogOption
= GL_NONE
;
1142 /* Notify driver the fragment program has (actually) changed.
1144 if (ctx
->Driver
.ProgramStringNotify
) {
1145 ctx
->Driver
.ProgramStringNotify( ctx
, GL_FRAGMENT_PROGRAM_ARB
,
1150 _mesa_print_program(&p
.program
->Base
);
1157 * Return a fragment program which implements the current
1158 * fixed-function texture, fog and color-sum operations.
1160 struct gl_fragment_program
*
1161 _mesa_get_fixed_func_fragment_program(GLcontext
*ctx
)
1163 struct gl_fragment_program
*prog
;
1164 struct state_key key
;
1166 make_state_key(ctx
, &key
);
1168 prog
= (struct gl_fragment_program
*)
1169 _mesa_search_program_cache(ctx
->FragmentProgram
.Cache
,
1173 prog
= (struct gl_fragment_program
*)
1174 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
1176 create_new_program(ctx
, &key
, prog
);
1178 _mesa_program_cache_insert(ctx
, ctx
->FragmentProgram
.Cache
,
1179 &key
, sizeof(key
), &prog
->Base
);
1188 * If _MaintainTexEnvProgram is set we'll generate a fragment program that
1189 * implements the current texture env/combine mode.
1190 * This function generates that program and puts it into effect.
1193 _mesa_UpdateTexEnvProgram( GLcontext
*ctx
)
1195 const struct gl_fragment_program
*prev
= ctx
->FragmentProgram
._Current
;
1197 ASSERT(ctx
->FragmentProgram
._MaintainTexEnvProgram
);
1199 /* If a conventional fragment program/shader isn't in effect... */
1200 if (!ctx
->FragmentProgram
._Enabled
&&
1201 (!ctx
->Shader
.CurrentProgram
||
1202 !ctx
->Shader
.CurrentProgram
->FragmentProgram
) ) {
1204 ctx
->FragmentProgram
._Current
1205 = ctx
->FragmentProgram
._TexEnvProgram
1206 = _mesa_get_fixed_func_fragment_program(ctx
);
1209 /* Tell the driver about the change. Could define a new target for
1212 if (ctx
->FragmentProgram
._Current
!= prev
&& ctx
->Driver
.BindProgram
) {
1213 ctx
->Driver
.BindProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
1214 (struct gl_program
*) ctx
->FragmentProgram
._Current
);