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 "shader/prog_parameter.h"
32 #include "shader/prog_instruction.h"
33 #include "shader/prog_print.h"
34 #include "shader/prog_statevars.h"
35 #include "texenvprogram.h"
38 * According to Glean's texCombine test, no more than 21 instructions
39 * are needed. Allow a few extra just in case.
41 #define MAX_INSTRUCTIONS 24
43 #define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
51 GLbitfield enabled_units
;
52 GLuint separate_specular
:1;
58 GLuint source_index
:3; /* one of TEXTURE_1D/2D/3D/CUBE/RECT_INDEX */
59 GLuint ScaleShiftRGB
:2;
64 struct mode_opt OptRGB
[3];
68 struct mode_opt OptA
[3];
77 static GLuint
translate_fog_mode( GLenum mode
)
80 case GL_LINEAR
: return FOG_LINEAR
;
81 case GL_EXP
: return FOG_EXP
;
82 case GL_EXP2
: return FOG_EXP2
;
83 default: return FOG_UNKNOWN
;
87 #define OPR_SRC_COLOR 0
88 #define OPR_ONE_MINUS_SRC_COLOR 1
89 #define OPR_SRC_ALPHA 2
90 #define OPR_ONE_MINUS_SRC_ALPHA 3
95 static GLuint
translate_operand( GLenum operand
)
98 case GL_SRC_COLOR
: return OPR_SRC_COLOR
;
99 case GL_ONE_MINUS_SRC_COLOR
: return OPR_ONE_MINUS_SRC_COLOR
;
100 case GL_SRC_ALPHA
: return OPR_SRC_ALPHA
;
101 case GL_ONE_MINUS_SRC_ALPHA
: return OPR_ONE_MINUS_SRC_ALPHA
;
102 case GL_ZERO
: return OPR_ZERO
;
103 case GL_ONE
: return OPR_ONE
;
104 default: return OPR_UNKNOWN
;
108 #define SRC_TEXTURE 0
109 #define SRC_TEXTURE0 1
110 #define SRC_TEXTURE1 2
111 #define SRC_TEXTURE2 3
112 #define SRC_TEXTURE3 4
113 #define SRC_TEXTURE4 5
114 #define SRC_TEXTURE5 6
115 #define SRC_TEXTURE6 7
116 #define SRC_TEXTURE7 8
117 #define SRC_CONSTANT 9
118 #define SRC_PRIMARY_COLOR 10
119 #define SRC_PREVIOUS 11
120 #define SRC_UNKNOWN 15
122 static GLuint
translate_source( GLenum src
)
125 case GL_TEXTURE
: return SRC_TEXTURE
;
133 case GL_TEXTURE7
: return SRC_TEXTURE0
+ (src
- GL_TEXTURE0
);
134 case GL_CONSTANT
: return SRC_CONSTANT
;
135 case GL_PRIMARY_COLOR
: return SRC_PRIMARY_COLOR
;
136 case GL_PREVIOUS
: return SRC_PREVIOUS
;
137 default: return SRC_UNKNOWN
;
141 #define MODE_REPLACE 0
142 #define MODE_MODULATE 1
144 #define MODE_ADD_SIGNED 3
145 #define MODE_INTERPOLATE 4
146 #define MODE_SUBTRACT 5
147 #define MODE_DOT3_RGB 6
148 #define MODE_DOT3_RGB_EXT 7
149 #define MODE_DOT3_RGBA 8
150 #define MODE_DOT3_RGBA_EXT 9
151 #define MODE_MODULATE_ADD_ATI 10
152 #define MODE_MODULATE_SIGNED_ADD_ATI 11
153 #define MODE_MODULATE_SUBTRACT_ATI 12
154 #define MODE_UNKNOWN 15
156 static GLuint
translate_mode( GLenum mode
)
159 case GL_REPLACE
: return MODE_REPLACE
;
160 case GL_MODULATE
: return MODE_MODULATE
;
161 case GL_ADD
: return MODE_ADD
;
162 case GL_ADD_SIGNED
: return MODE_ADD_SIGNED
;
163 case GL_INTERPOLATE
: return MODE_INTERPOLATE
;
164 case GL_SUBTRACT
: return MODE_SUBTRACT
;
165 case GL_DOT3_RGB
: return MODE_DOT3_RGB
;
166 case GL_DOT3_RGB_EXT
: return MODE_DOT3_RGB_EXT
;
167 case GL_DOT3_RGBA
: return MODE_DOT3_RGBA
;
168 case GL_DOT3_RGBA_EXT
: return MODE_DOT3_RGBA_EXT
;
169 case GL_MODULATE_ADD_ATI
: return MODE_MODULATE_ADD_ATI
;
170 case GL_MODULATE_SIGNED_ADD_ATI
: return MODE_MODULATE_SIGNED_ADD_ATI
;
171 case GL_MODULATE_SUBTRACT_ATI
: return MODE_MODULATE_SUBTRACT_ATI
;
172 default: return MODE_UNKNOWN
;
176 #define TEXTURE_UNKNOWN_INDEX 7
177 static GLuint
translate_tex_src_bit( GLbitfield bit
)
180 case TEXTURE_1D_BIT
: return TEXTURE_1D_INDEX
;
181 case TEXTURE_2D_BIT
: return TEXTURE_2D_INDEX
;
182 case TEXTURE_RECT_BIT
: return TEXTURE_RECT_INDEX
;
183 case TEXTURE_3D_BIT
: return TEXTURE_3D_INDEX
;
184 case TEXTURE_CUBE_BIT
: return TEXTURE_CUBE_INDEX
;
185 default: return TEXTURE_UNKNOWN_INDEX
;
190 * Examine current texture environment state and generate a unique
191 * key to identify it.
193 static void make_state_key( GLcontext
*ctx
, struct state_key
*key
)
197 memset(key
, 0, sizeof(*key
));
199 for (i
=0;i
<MAX_TEXTURE_UNITS
;i
++) {
200 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
202 if (!texUnit
->_ReallyEnabled
)
205 key
->unit
[i
].enabled
= 1;
206 key
->enabled_units
|= (1<<i
);
208 key
->unit
[i
].source_index
=
209 translate_tex_src_bit(texUnit
->_ReallyEnabled
);
211 key
->unit
[i
].NumArgsRGB
= texUnit
->_CurrentCombine
->_NumArgsRGB
;
212 key
->unit
[i
].NumArgsA
= texUnit
->_CurrentCombine
->_NumArgsA
;
214 key
->unit
[i
].ModeRGB
=
215 translate_mode(texUnit
->_CurrentCombine
->ModeRGB
);
217 translate_mode(texUnit
->_CurrentCombine
->ModeA
);
219 key
->unit
[i
].ScaleShiftRGB
= texUnit
->_CurrentCombine
->ScaleShiftRGB
;
220 key
->unit
[i
].ScaleShiftA
= texUnit
->_CurrentCombine
->ScaleShiftA
;
223 key
->unit
[i
].OptRGB
[j
].Operand
=
224 translate_operand(texUnit
->_CurrentCombine
->OperandRGB
[j
]);
225 key
->unit
[i
].OptA
[j
].Operand
=
226 translate_operand(texUnit
->_CurrentCombine
->OperandA
[j
]);
227 key
->unit
[i
].OptRGB
[j
].Source
=
228 translate_source(texUnit
->_CurrentCombine
->SourceRGB
[j
]);
229 key
->unit
[i
].OptA
[j
].Source
=
230 translate_source(texUnit
->_CurrentCombine
->SourceA
[j
]);
234 if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
)
235 key
->separate_specular
= 1;
237 if (ctx
->Fog
.Enabled
) {
238 key
->fog_enabled
= 1;
239 key
->fog_mode
= translate_fog_mode(ctx
->Fog
.Mode
);
243 /* Use uregs to represent registers internally, translate to Mesa's
244 * expected formats on emit.
246 * NOTE: These are passed by value extensively in this file rather
247 * than as usual by pointer reference. If this disturbs you, try
248 * remembering they are just 32bits in size.
250 * GCC is smart enough to deal with these dword-sized structures in
251 * much the same way as if I had defined them as dwords and was using
252 * macros to access and set the fields. This is much nicer and easier
265 static const struct ureg undef
= {
276 /* State used to build the fragment program:
278 struct texenv_fragment_program
{
279 struct gl_fragment_program
*program
;
281 struct state_key
*state
;
283 GLbitfield alu_temps
; /* Track texture indirections, see spec. */
284 GLbitfield temps_output
; /* Track texture indirections, see spec. */
285 GLbitfield temp_in_use
; /* Tracks temporary regs which are in use. */
288 struct ureg src_texture
[MAX_TEXTURE_UNITS
];
289 /* Reg containing each texture unit's sampled texture color,
293 struct ureg src_previous
; /* Reg containing color from previous
294 * stage. May need to be decl'd.
297 GLuint last_tex_stage
; /* Number of last enabled texture unit */
306 static struct ureg
make_ureg(GLuint file
, GLuint idx
)
314 reg
.swz
= SWIZZLE_NOOP
;
319 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
321 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
324 GET_SWZ(reg
.swz
, w
));
329 static struct ureg
swizzle1( struct ureg reg
, int x
)
331 return swizzle(reg
, x
, x
, x
, x
);
334 static struct ureg
negate( struct ureg reg
)
340 static GLboolean
is_undef( struct ureg reg
)
342 return reg
.file
== PROGRAM_UNDEFINED
;
346 static struct ureg
get_temp( struct texenv_fragment_program
*p
)
350 /* First try and reuse temps which have been used already:
352 bit
= _mesa_ffs( ~p
->temp_in_use
& p
->alu_temps
);
354 /* Then any unused temporary:
357 bit
= _mesa_ffs( ~p
->temp_in_use
);
360 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
364 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
365 p
->program
->Base
.NumTemporaries
= bit
;
367 p
->temp_in_use
|= 1<<(bit
-1);
368 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
371 static struct ureg
get_tex_temp( struct texenv_fragment_program
*p
)
375 /* First try to find availble temp not previously used (to avoid
376 * starting a new texture indirection). According to the spec, the
377 * ~p->temps_output isn't necessary, but will keep it there for
380 bit
= _mesa_ffs( ~p
->temp_in_use
& ~p
->alu_temps
& ~p
->temps_output
);
382 /* Then any unused temporary:
385 bit
= _mesa_ffs( ~p
->temp_in_use
);
388 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
392 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
393 p
->program
->Base
.NumTemporaries
= bit
;
395 p
->temp_in_use
|= 1<<(bit
-1);
396 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
400 static void release_temps( struct texenv_fragment_program
*p
)
402 GLuint max_temp
= p
->ctx
->Const
.FragmentProgram
.MaxTemps
;
404 /* KW: To support tex_env_crossbar, don't release the registers in
407 if (max_temp
>= sizeof(int) * 8)
408 p
->temp_in_use
= p
->temps_output
;
410 p
->temp_in_use
= ~((1<<max_temp
)-1) | p
->temps_output
;
414 static struct ureg
register_param5( struct texenv_fragment_program
*p
,
421 gl_state_index tokens
[STATE_LENGTH
];
428 idx
= _mesa_add_state_reference( p
->program
->Base
.Parameters
, tokens
);
429 return make_ureg(PROGRAM_STATE_VAR
, idx
);
433 #define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
434 #define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
435 #define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
436 #define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
439 static struct ureg
register_input( struct texenv_fragment_program
*p
, GLuint input
)
441 p
->program
->Base
.InputsRead
|= (1 << input
);
442 return make_ureg(PROGRAM_INPUT
, input
);
446 static void emit_arg( struct prog_src_register
*reg
,
449 reg
->File
= ureg
.file
;
450 reg
->Index
= ureg
.idx
;
451 reg
->Swizzle
= ureg
.swz
;
452 reg
->NegateBase
= ureg
.negatebase
? 0xf : 0x0;
454 reg
->NegateAbs
= ureg
.negateabs
;
457 static void emit_dst( struct prog_dst_register
*dst
,
458 struct ureg ureg
, GLuint mask
)
460 dst
->File
= ureg
.file
;
461 dst
->Index
= ureg
.idx
;
462 dst
->WriteMask
= mask
;
464 dst
->CondSwizzle
= 0;
467 static struct prog_instruction
*
468 emit_op(struct texenv_fragment_program
*p
,
477 GLuint nr
= p
->program
->Base
.NumInstructions
++;
478 struct prog_instruction
*inst
= &p
->program
->Base
.Instructions
[nr
];
480 _mesa_init_instructions(inst
, 1);
483 emit_arg( &inst
->SrcReg
[0], src0
);
484 emit_arg( &inst
->SrcReg
[1], src1
);
485 emit_arg( &inst
->SrcReg
[2], src2
);
487 inst
->SaturateMode
= saturate
? SATURATE_ZERO_ONE
: SATURATE_OFF
;
489 emit_dst( &inst
->DstReg
, dest
, mask
);
491 /* Accounting for indirection tracking:
493 if (dest
.file
== PROGRAM_TEMPORARY
)
494 p
->temps_output
|= 1 << dest
.idx
;
500 static struct ureg
emit_arith( struct texenv_fragment_program
*p
,
509 emit_op(p
, op
, dest
, mask
, saturate
, src0
, src1
, src2
);
511 /* Accounting for indirection tracking:
513 if (src0
.file
== PROGRAM_TEMPORARY
)
514 p
->alu_temps
|= 1 << src0
.idx
;
516 if (!is_undef(src1
) && src1
.file
== PROGRAM_TEMPORARY
)
517 p
->alu_temps
|= 1 << src1
.idx
;
519 if (!is_undef(src2
) && src2
.file
== PROGRAM_TEMPORARY
)
520 p
->alu_temps
|= 1 << src2
.idx
;
522 if (dest
.file
== PROGRAM_TEMPORARY
)
523 p
->alu_temps
|= 1 << dest
.idx
;
525 p
->program
->Base
.NumAluInstructions
++;
529 static struct ureg
emit_texld( struct texenv_fragment_program
*p
,
537 struct prog_instruction
*inst
= emit_op( p
, op
,
539 GL_FALSE
, /* don't saturate? */
544 inst
->TexSrcTarget
= tex_idx
;
545 inst
->TexSrcUnit
= tex_unit
;
547 p
->program
->Base
.NumTexInstructions
++;
549 /* Is this a texture indirection?
551 if ((coord
.file
== PROGRAM_TEMPORARY
&&
552 (p
->temps_output
& (1<<coord
.idx
))) ||
553 (dest
.file
== PROGRAM_TEMPORARY
&&
554 (p
->alu_temps
& (1<<dest
.idx
)))) {
555 p
->program
->Base
.NumTexIndirections
++;
556 p
->temps_output
= 1<<coord
.idx
;
558 assert(0); /* KW: texture env crossbar */
565 static struct ureg
register_const4f( struct texenv_fragment_program
*p
,
577 idx
= _mesa_add_unnamed_constant( p
->program
->Base
.Parameters
, values
, 4,
579 ASSERT(swizzle
== SWIZZLE_NOOP
);
580 return make_ureg(PROGRAM_STATE_VAR
, idx
);
583 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
584 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
585 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
586 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
589 static struct ureg
get_one( struct texenv_fragment_program
*p
)
591 if (is_undef(p
->one
))
592 p
->one
= register_scalar_const(p
, 1.0);
596 static struct ureg
get_half( struct texenv_fragment_program
*p
)
598 if (is_undef(p
->half
))
599 p
->half
= register_scalar_const(p
, 0.5);
603 static struct ureg
get_zero( struct texenv_fragment_program
*p
)
605 if (is_undef(p
->zero
))
606 p
->zero
= register_scalar_const(p
, 0.0);
611 static void program_error( struct texenv_fragment_program
*p
, const char *msg
)
613 _mesa_problem(NULL
, msg
);
617 static struct ureg
get_source( struct texenv_fragment_program
*p
,
618 GLuint src
, GLuint unit
)
622 assert(!is_undef(p
->src_texture
[unit
]));
623 return p
->src_texture
[unit
];
633 assert(!is_undef(p
->src_texture
[src
- SRC_TEXTURE0
]));
634 return p
->src_texture
[src
- SRC_TEXTURE0
];
637 return register_param2(p
, STATE_TEXENV_COLOR
, unit
);
639 case SRC_PRIMARY_COLOR
:
640 return register_input(p
, FRAG_ATTRIB_COL0
);
644 if (is_undef(p
->src_previous
))
645 return register_input(p
, FRAG_ATTRIB_COL0
);
647 return p
->src_previous
;
651 static struct ureg
emit_combine_source( struct texenv_fragment_program
*p
,
657 struct ureg arg
, src
, one
;
659 src
= get_source(p
, source
, unit
);
662 case OPR_ONE_MINUS_SRC_COLOR
:
664 * Emit tmp = 1.0 - arg.xyzw
668 return emit_arith( p
, OPCODE_SUB
, arg
, mask
, 0, one
, src
, undef
);
671 if (mask
== WRITEMASK_W
)
674 return swizzle1( src
, SWIZZLE_W
);
675 case OPR_ONE_MINUS_SRC_ALPHA
:
677 * Emit tmp = 1.0 - arg.wwww
681 return emit_arith(p
, OPCODE_SUB
, arg
, mask
, 0,
682 one
, swizzle1(src
, SWIZZLE_W
), undef
);
693 static GLboolean
args_match( struct state_key
*key
, GLuint unit
)
695 GLuint i
, nr
= key
->unit
[unit
].NumArgsRGB
;
697 for (i
= 0 ; i
< nr
; i
++) {
698 if (key
->unit
[unit
].OptA
[i
].Source
!= key
->unit
[unit
].OptRGB
[i
].Source
)
701 switch(key
->unit
[unit
].OptA
[i
].Operand
) {
703 switch(key
->unit
[unit
].OptRGB
[i
].Operand
) {
711 case OPR_ONE_MINUS_SRC_ALPHA
:
712 switch(key
->unit
[unit
].OptRGB
[i
].Operand
) {
713 case OPR_ONE_MINUS_SRC_COLOR
:
714 case OPR_ONE_MINUS_SRC_ALPHA
:
721 return GL_FALSE
; /* impossible */
728 static struct ureg
emit_combine( struct texenv_fragment_program
*p
,
735 const struct mode_opt
*opt
)
738 struct ureg tmp
, half
;
741 tmp
= undef
; /* silence warning (bug 5318) */
743 for (i
= 0; i
< nr
; i
++)
744 src
[i
] = emit_combine_source( p
, mask
, unit
, opt
[i
].Source
, opt
[i
].Operand
);
748 if (mask
== WRITEMASK_XYZW
&& !saturate
)
751 return emit_arith( p
, OPCODE_MOV
, dest
, mask
, saturate
, src
[0], undef
, undef
);
753 return emit_arith( p
, OPCODE_MUL
, dest
, mask
, saturate
,
754 src
[0], src
[1], undef
);
756 return emit_arith( p
, OPCODE_ADD
, dest
, mask
, saturate
,
757 src
[0], src
[1], undef
);
758 case MODE_ADD_SIGNED
:
764 emit_arith( p
, OPCODE_ADD
, tmp
, mask
, 0, src
[0], src
[1], undef
);
765 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp
, half
, undef
);
767 case MODE_INTERPOLATE
:
768 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
770 return emit_arith( p
, OPCODE_LRP
, dest
, mask
, saturate
, src
[2], src
[0], src
[1] );
773 return emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, src
[0], src
[1], undef
);
776 case MODE_DOT3_RGBA_EXT
:
777 case MODE_DOT3_RGB_EXT
:
778 case MODE_DOT3_RGB
: {
779 struct ureg tmp0
= get_temp( p
);
780 struct ureg tmp1
= get_temp( p
);
781 struct ureg neg1
= register_scalar_const(p
, -1);
782 struct ureg two
= register_scalar_const(p
, 2);
787 * dst = tmp0 dot3 tmp1
789 emit_arith( p
, OPCODE_MAD
, tmp0
, WRITEMASK_XYZW
, 0,
792 if (_mesa_memcmp(&src
[0], &src
[1], sizeof(struct ureg
)) == 0)
795 emit_arith( p
, OPCODE_MAD
, tmp1
, WRITEMASK_XYZW
, 0,
797 emit_arith( p
, OPCODE_DP3
, dest
, mask
, saturate
, tmp0
, tmp1
, undef
);
800 case MODE_MODULATE_ADD_ATI
:
801 /* Arg0 * Arg2 + Arg1 */
802 return emit_arith( p
, OPCODE_MAD
, dest
, mask
, saturate
,
803 src
[0], src
[2], src
[1] );
804 case MODE_MODULATE_SIGNED_ADD_ATI
: {
805 /* Arg0 * Arg2 + Arg1 - 0.5 */
806 struct ureg tmp0
= get_temp(p
);
808 emit_arith( p
, OPCODE_MAD
, tmp0
, mask
, 0, src
[0], src
[2], src
[1] );
809 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp0
, half
, undef
);
812 case MODE_MODULATE_SUBTRACT_ATI
:
813 /* Arg0 * Arg2 - Arg1 */
814 emit_arith( p
, OPCODE_MAD
, dest
, mask
, 0, src
[0], src
[2], negate(src
[1]) );
823 * Generate instructions for one texture unit's env/combiner mode.
826 emit_texenv(struct texenv_fragment_program
*p
, GLuint unit
)
828 struct state_key
*key
= p
->state
;
829 GLboolean saturate
= (unit
< p
->last_tex_stage
);
830 GLuint rgb_shift
, alpha_shift
;
831 struct ureg out
, shift
;
834 if (!key
->unit
[unit
].enabled
) {
835 return get_source(p
, SRC_PREVIOUS
, 0);
838 switch (key
->unit
[unit
].ModeRGB
) {
839 case MODE_DOT3_RGB_EXT
:
840 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
843 case MODE_DOT3_RGBA_EXT
:
848 rgb_shift
= key
->unit
[unit
].ScaleShiftRGB
;
849 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
853 /* If this is the very last calculation, emit direct to output reg:
855 if (key
->separate_specular
||
856 unit
!= p
->last_tex_stage
||
859 dest
= get_temp( p
);
861 dest
= make_ureg(PROGRAM_OUTPUT
, FRAG_RESULT_COLR
);
863 /* Emit the RGB and A combine ops
865 if (key
->unit
[unit
].ModeRGB
== key
->unit
[unit
].ModeA
&&
866 args_match(key
, unit
)) {
867 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, saturate
,
869 key
->unit
[unit
].NumArgsRGB
,
870 key
->unit
[unit
].ModeRGB
,
871 key
->unit
[unit
].OptRGB
);
873 else if (key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA_EXT
||
874 key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA
) {
876 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, saturate
,
878 key
->unit
[unit
].NumArgsRGB
,
879 key
->unit
[unit
].ModeRGB
,
880 key
->unit
[unit
].OptRGB
);
883 /* Need to do something to stop from re-emitting identical
884 * argument calculations here:
886 out
= emit_combine( p
, dest
, WRITEMASK_XYZ
, saturate
,
888 key
->unit
[unit
].NumArgsRGB
,
889 key
->unit
[unit
].ModeRGB
,
890 key
->unit
[unit
].OptRGB
);
891 out
= emit_combine( p
, dest
, WRITEMASK_W
, saturate
,
893 key
->unit
[unit
].NumArgsA
,
894 key
->unit
[unit
].ModeA
,
895 key
->unit
[unit
].OptA
);
898 /* Deal with the final shift:
900 if (alpha_shift
|| rgb_shift
) {
901 if (rgb_shift
== alpha_shift
) {
902 shift
= register_scalar_const(p
, 1<<rgb_shift
);
905 shift
= register_const4f(p
,
911 return emit_arith( p
, OPCODE_MUL
, dest
, WRITEMASK_XYZW
,
912 saturate
, out
, shift
, undef
);
920 * Generate instruction for getting a texture source term.
922 static void load_texture( struct texenv_fragment_program
*p
, GLuint unit
)
924 if (is_undef(p
->src_texture
[unit
])) {
925 GLuint dim
= p
->state
->unit
[unit
].source_index
;
926 struct ureg texcoord
= register_input(p
, FRAG_ATTRIB_TEX0
+unit
);
927 struct ureg tmp
= get_tex_temp( p
);
929 if (dim
== TEXTURE_UNKNOWN_INDEX
)
930 program_error(p
, "TexSrcBit");
932 /* TODO: Use D0_MASK_XY where possible.
934 if (p
->state
->unit
[unit
].enabled
)
935 p
->src_texture
[unit
] = emit_texld( p
, OPCODE_TXP
,
937 unit
, dim
, texcoord
);
939 p
->src_texture
[unit
] = get_zero(p
);
943 static GLboolean
load_texenv_source( struct texenv_fragment_program
*p
,
944 GLuint src
, GLuint unit
)
948 load_texture(p
, unit
);
959 load_texture(p
, src
- SRC_TEXTURE0
);
971 * Generate instructions for loading all texture source terms.
974 load_texunit_sources( struct texenv_fragment_program
*p
, int unit
)
976 struct state_key
*key
= p
->state
;
979 for (i
= 0; i
< key
->unit
[unit
].NumArgsRGB
; i
++) {
980 load_texenv_source( p
, key
->unit
[unit
].OptRGB
[i
].Source
, unit
);
983 for (i
= 0; i
< key
->unit
[unit
].NumArgsA
; i
++) {
984 load_texenv_source( p
, key
->unit
[unit
].OptA
[i
].Source
, unit
);
992 * Generate a new fragment program which implements the context's
993 * current texture env/combine mode.
996 create_new_program(GLcontext
*ctx
, struct state_key
*key
,
997 struct gl_fragment_program
*program
)
999 struct prog_instruction instBuffer
[MAX_INSTRUCTIONS
];
1000 struct texenv_fragment_program p
;
1002 struct ureg cf
, out
;
1004 _mesa_memset(&p
, 0, sizeof(p
));
1007 p
.program
= program
;
1009 /* During code generation, use locally-allocated instruction buffer,
1010 * then alloc dynamic storage below.
1012 p
.program
->Base
.Instructions
= instBuffer
;
1013 p
.program
->Base
.Target
= GL_FRAGMENT_PROGRAM_ARB
;
1014 p
.program
->Base
.NumTexIndirections
= 1; /* correct? */
1015 p
.program
->Base
.NumTexInstructions
= 0;
1016 p
.program
->Base
.NumAluInstructions
= 0;
1017 p
.program
->Base
.String
= NULL
;
1018 p
.program
->Base
.NumInstructions
=
1019 p
.program
->Base
.NumTemporaries
=
1020 p
.program
->Base
.NumParameters
=
1021 p
.program
->Base
.NumAttributes
= p
.program
->Base
.NumAddressRegs
= 0;
1022 p
.program
->Base
.Parameters
= _mesa_new_parameter_list();
1024 p
.program
->Base
.InputsRead
= 0;
1025 p
.program
->Base
.OutputsWritten
= 1 << FRAG_RESULT_COLR
;
1027 for (unit
= 0; unit
< MAX_TEXTURE_UNITS
; unit
++)
1028 p
.src_texture
[unit
] = undef
;
1030 p
.src_previous
= undef
;
1035 p
.last_tex_stage
= 0;
1038 if (key
->enabled_units
) {
1039 /* First pass - to support texture_env_crossbar, first identify
1040 * all referenced texture sources and emit texld instructions
1043 for (unit
= 0 ; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
1044 if (key
->unit
[unit
].enabled
) {
1045 load_texunit_sources( &p
, unit
);
1046 p
.last_tex_stage
= unit
;
1049 /* Second pass - emit combine instructions to build final color:
1051 for (unit
= 0 ; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
1052 if (key
->enabled_units
& (1<<unit
)) {
1053 p
.src_previous
= emit_texenv( &p
, unit
);
1054 release_temps(&p
); /* release all temps */
1058 cf
= get_source( &p
, SRC_PREVIOUS
, 0 );
1059 out
= make_ureg( PROGRAM_OUTPUT
, FRAG_RESULT_COLR
);
1061 if (key
->separate_specular
) {
1062 /* Emit specular add.
1064 struct ureg s
= register_input(&p
, FRAG_ATTRIB_COL1
);
1065 emit_arith( &p
, OPCODE_ADD
, out
, WRITEMASK_XYZ
, 0, cf
, s
, undef
);
1066 emit_arith( &p
, OPCODE_MOV
, out
, WRITEMASK_W
, 0, cf
, undef
, undef
);
1068 else if (_mesa_memcmp(&cf
, &out
, sizeof(cf
)) != 0) {
1069 /* Will wind up in here if no texture enabled or a couple of
1070 * other scenarios (GL_REPLACE for instance).
1072 emit_arith( &p
, OPCODE_MOV
, out
, WRITEMASK_XYZW
, 0, cf
, undef
, undef
);
1077 emit_arith( &p
, OPCODE_END
, undef
, WRITEMASK_XYZW
, 0, undef
, undef
, undef
);
1079 if (key
->fog_enabled
) {
1080 /* Pull fog mode from GLcontext, the value in the state key is
1081 * a reduced value and not what is expected in FogOption
1083 p
.program
->FogOption
= ctx
->Fog
.Mode
;
1085 p
.program
->FogOption
= GL_NONE
;
1087 if (p
.program
->Base
.NumTexIndirections
> ctx
->Const
.FragmentProgram
.MaxTexIndirections
)
1088 program_error(&p
, "Exceeded max nr indirect texture lookups");
1090 if (p
.program
->Base
.NumTexInstructions
> ctx
->Const
.FragmentProgram
.MaxTexInstructions
)
1091 program_error(&p
, "Exceeded max TEX instructions");
1093 if (p
.program
->Base
.NumAluInstructions
> ctx
->Const
.FragmentProgram
.MaxAluInstructions
)
1094 program_error(&p
, "Exceeded max ALU instructions");
1096 ASSERT(p
.program
->Base
.NumInstructions
<= MAX_INSTRUCTIONS
);
1098 /* Allocate final instruction array */
1099 program
->Base
.Instructions
1100 = _mesa_alloc_instructions(program
->Base
.NumInstructions
);
1101 if (!program
->Base
.Instructions
) {
1102 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1103 "generating tex env program");
1106 _mesa_copy_instructions(program
->Base
.Instructions
, instBuffer
,
1107 program
->Base
.NumInstructions
);
1109 /* Notify driver the fragment program has (actually) changed.
1111 if (ctx
->Driver
.ProgramStringNotify
) {
1112 ctx
->Driver
.ProgramStringNotify( ctx
, GL_FRAGMENT_PROGRAM_ARB
,
1117 _mesa_print_program(&p
.program
->Base
);
1123 static struct gl_fragment_program
*
1124 search_cache(const struct texenvprog_cache
*cache
,
1129 struct texenvprog_cache_item
*c
;
1131 for (c
= cache
->items
[hash
% cache
->size
]; c
; c
= c
->next
) {
1132 if (c
->hash
== hash
&& memcmp(c
->key
, key
, keysize
) == 0)
1133 return (struct gl_fragment_program
*) c
->data
;
1139 static void rehash( struct texenvprog_cache
*cache
)
1141 struct texenvprog_cache_item
**items
;
1142 struct texenvprog_cache_item
*c
, *next
;
1145 size
= cache
->size
* 3;
1146 items
= (struct texenvprog_cache_item
**) _mesa_malloc(size
* sizeof(*items
));
1147 _mesa_memset(items
, 0, size
* sizeof(*items
));
1149 for (i
= 0; i
< cache
->size
; i
++)
1150 for (c
= cache
->items
[i
]; c
; c
= next
) {
1152 c
->next
= items
[c
->hash
% size
];
1153 items
[c
->hash
% size
] = c
;
1156 _mesa_free(cache
->items
);
1157 cache
->items
= items
;
1161 static void clear_cache( struct texenvprog_cache
*cache
)
1163 struct texenvprog_cache_item
*c
, *next
;
1166 for (i
= 0; i
< cache
->size
; i
++) {
1167 for (c
= cache
->items
[i
]; c
; c
= next
) {
1170 cache
->ctx
->Driver
.DeleteProgram(cache
->ctx
,
1171 (struct gl_program
*) c
->data
);
1174 cache
->items
[i
] = NULL
;
1182 static void cache_item( struct texenvprog_cache
*cache
,
1184 const struct state_key
*key
,
1187 struct texenvprog_cache_item
*c
1188 = (struct texenvprog_cache_item
*) MALLOC(sizeof(*c
));
1191 c
->key
= _mesa_malloc(sizeof(*key
));
1192 memcpy(c
->key
, key
, sizeof(*key
));
1194 c
->data
= (struct gl_fragment_program
*) data
;
1196 if (cache
->n_items
> cache
->size
* 1.5) {
1197 if (cache
->size
< 1000)
1204 c
->next
= cache
->items
[hash
% cache
->size
];
1205 cache
->items
[hash
% cache
->size
] = c
;
1208 static GLuint
hash_key( const struct state_key
*key
)
1210 GLuint
*ikey
= (GLuint
*)key
;
1213 /* Make a slightly better attempt at a hash function:
1215 for (i
= 0; i
< sizeof(*key
)/sizeof(*ikey
); i
++)
1218 hash
+= (hash
<< 10);
1219 hash
^= (hash
>> 6);
1227 * If _MaintainTexEnvProgram is set we'll generate a fragment program that
1228 * implements the current texture env/combine mode.
1229 * This function generates that program and puts it into effect.
1232 _mesa_UpdateTexEnvProgram( GLcontext
*ctx
)
1234 struct state_key key
;
1236 const struct gl_fragment_program
*prev
= ctx
->FragmentProgram
._Current
;
1238 ASSERT(ctx
->FragmentProgram
._MaintainTexEnvProgram
);
1240 /* If a conventional fragment program/shader isn't in effect... */
1241 if (!ctx
->FragmentProgram
._Enabled
&&
1242 !ctx
->Shader
.CurrentProgram
) {
1243 make_state_key(ctx
, &key
);
1244 hash
= hash_key(&key
);
1246 ctx
->FragmentProgram
._Current
=
1247 ctx
->FragmentProgram
._TexEnvProgram
=
1248 search_cache(&ctx
->Texture
.env_fp_cache
, hash
, &key
, sizeof(key
));
1250 if (!ctx
->FragmentProgram
._TexEnvProgram
) {
1252 _mesa_printf("Building new texenv proggy for key %x\n", hash
);
1254 /* create new tex env program */
1255 ctx
->FragmentProgram
._Current
=
1256 ctx
->FragmentProgram
._TexEnvProgram
=
1257 (struct gl_fragment_program
*)
1258 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
1260 create_new_program(ctx
, &key
, ctx
->FragmentProgram
._TexEnvProgram
);
1262 cache_item(&ctx
->Texture
.env_fp_cache
, hash
, &key
,
1263 ctx
->FragmentProgram
._TexEnvProgram
);
1267 _mesa_printf("Found existing texenv program for key %x\n", hash
);
1271 ctx
->FragmentProgram
._Current
= ctx
->FragmentProgram
.Current
;
1274 /* Tell the driver about the change. Could define a new target for
1277 if (ctx
->FragmentProgram
._Current
!= prev
&& ctx
->Driver
.BindProgram
) {
1278 ctx
->Driver
.BindProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
1279 (struct gl_program
*) ctx
->FragmentProgram
._Current
);
1284 void _mesa_TexEnvProgramCacheInit( GLcontext
*ctx
)
1286 ctx
->Texture
.env_fp_cache
.ctx
= ctx
;
1287 ctx
->Texture
.env_fp_cache
.size
= 17;
1288 ctx
->Texture
.env_fp_cache
.n_items
= 0;
1289 ctx
->Texture
.env_fp_cache
.items
= (struct texenvprog_cache_item
**)
1290 _mesa_calloc(ctx
->Texture
.env_fp_cache
.size
*
1291 sizeof(struct texenvprog_cache_item
));
1295 void _mesa_TexEnvProgramCacheDestroy( GLcontext
*ctx
)
1297 clear_cache(&ctx
->Texture
.env_fp_cache
);
1298 _mesa_free(ctx
->Texture
.env_fp_cache
.items
);