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/program.h"
32 #include "shader/prog_parameter.h"
33 #include "shader/prog_cache.h"
34 #include "shader/prog_instruction.h"
35 #include "shader/prog_print.h"
36 #include "shader/prog_statevars.h"
37 #include "shader/programopt.h"
38 #include "texenvprogram.h"
42 * Note on texture units:
44 * The number of texture units supported by fixed-function fragment
45 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
46 * That's because there's a one-to-one correspondence between texture
47 * coordinates and samplers in fixed-function processing.
49 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
50 * sets of texcoords, so is fixed-function fragment processing.
52 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
56 struct texenvprog_cache_item
60 struct gl_fragment_program
*data
;
61 struct texenvprog_cache_item
*next
;
66 * This MAX is probably a bit generous, but that's OK. There can be
67 * up to four instructions per texture unit (TEX + 3 for combine),
68 * then there's fog and specular add.
70 #define MAX_INSTRUCTIONS ((MAX_TEXTURE_COORD_UNITS * 4) + 12)
72 #define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
80 GLbitfield enabled_units
;
81 GLuint separate_specular
:1;
87 GLuint source_index
:3; /* one of TEXTURE_1D/2D/3D/CUBE/RECT_INDEX */
89 GLuint ScaleShiftRGB
:2;
94 struct mode_opt OptRGB
[3];
98 struct mode_opt OptA
[3];
105 #define FOG_UNKNOWN 3
107 static GLuint
translate_fog_mode( GLenum mode
)
110 case GL_LINEAR
: return FOG_LINEAR
;
111 case GL_EXP
: return FOG_EXP
;
112 case GL_EXP2
: return FOG_EXP2
;
113 default: return FOG_UNKNOWN
;
117 #define OPR_SRC_COLOR 0
118 #define OPR_ONE_MINUS_SRC_COLOR 1
119 #define OPR_SRC_ALPHA 2
120 #define OPR_ONE_MINUS_SRC_ALPHA 3
123 #define OPR_UNKNOWN 7
125 static GLuint
translate_operand( GLenum operand
)
128 case GL_SRC_COLOR
: return OPR_SRC_COLOR
;
129 case GL_ONE_MINUS_SRC_COLOR
: return OPR_ONE_MINUS_SRC_COLOR
;
130 case GL_SRC_ALPHA
: return OPR_SRC_ALPHA
;
131 case GL_ONE_MINUS_SRC_ALPHA
: return OPR_ONE_MINUS_SRC_ALPHA
;
132 case GL_ZERO
: return OPR_ZERO
;
133 case GL_ONE
: return OPR_ONE
;
134 default: return OPR_UNKNOWN
;
138 #define SRC_TEXTURE 0
139 #define SRC_TEXTURE0 1
140 #define SRC_TEXTURE1 2
141 #define SRC_TEXTURE2 3
142 #define SRC_TEXTURE3 4
143 #define SRC_TEXTURE4 5
144 #define SRC_TEXTURE5 6
145 #define SRC_TEXTURE6 7
146 #define SRC_TEXTURE7 8
147 #define SRC_CONSTANT 9
148 #define SRC_PRIMARY_COLOR 10
149 #define SRC_PREVIOUS 11
150 #define SRC_UNKNOWN 15
152 static GLuint
translate_source( GLenum src
)
155 case GL_TEXTURE
: return SRC_TEXTURE
;
163 case GL_TEXTURE7
: return SRC_TEXTURE0
+ (src
- GL_TEXTURE0
);
164 case GL_CONSTANT
: return SRC_CONSTANT
;
165 case GL_PRIMARY_COLOR
: return SRC_PRIMARY_COLOR
;
166 case GL_PREVIOUS
: return SRC_PREVIOUS
;
167 default: return SRC_UNKNOWN
;
171 #define MODE_REPLACE 0
172 #define MODE_MODULATE 1
174 #define MODE_ADD_SIGNED 3
175 #define MODE_INTERPOLATE 4
176 #define MODE_SUBTRACT 5
177 #define MODE_DOT3_RGB 6
178 #define MODE_DOT3_RGB_EXT 7
179 #define MODE_DOT3_RGBA 8
180 #define MODE_DOT3_RGBA_EXT 9
181 #define MODE_MODULATE_ADD_ATI 10
182 #define MODE_MODULATE_SIGNED_ADD_ATI 11
183 #define MODE_MODULATE_SUBTRACT_ATI 12
184 #define MODE_UNKNOWN 15
186 static GLuint
translate_mode( GLenum mode
)
189 case GL_REPLACE
: return MODE_REPLACE
;
190 case GL_MODULATE
: return MODE_MODULATE
;
191 case GL_ADD
: return MODE_ADD
;
192 case GL_ADD_SIGNED
: return MODE_ADD_SIGNED
;
193 case GL_INTERPOLATE
: return MODE_INTERPOLATE
;
194 case GL_SUBTRACT
: return MODE_SUBTRACT
;
195 case GL_DOT3_RGB
: return MODE_DOT3_RGB
;
196 case GL_DOT3_RGB_EXT
: return MODE_DOT3_RGB_EXT
;
197 case GL_DOT3_RGBA
: return MODE_DOT3_RGBA
;
198 case GL_DOT3_RGBA_EXT
: return MODE_DOT3_RGBA_EXT
;
199 case GL_MODULATE_ADD_ATI
: return MODE_MODULATE_ADD_ATI
;
200 case GL_MODULATE_SIGNED_ADD_ATI
: return MODE_MODULATE_SIGNED_ADD_ATI
;
201 case GL_MODULATE_SUBTRACT_ATI
: return MODE_MODULATE_SUBTRACT_ATI
;
202 default: return MODE_UNKNOWN
;
206 #define TEXTURE_UNKNOWN_INDEX 7
207 static GLuint
translate_tex_src_bit( GLbitfield bit
)
210 case TEXTURE_1D_BIT
: return TEXTURE_1D_INDEX
;
211 case TEXTURE_2D_BIT
: return TEXTURE_2D_INDEX
;
212 case TEXTURE_RECT_BIT
: return TEXTURE_RECT_INDEX
;
213 case TEXTURE_3D_BIT
: return TEXTURE_3D_INDEX
;
214 case TEXTURE_CUBE_BIT
: return TEXTURE_CUBE_INDEX
;
215 default: return TEXTURE_UNKNOWN_INDEX
;
220 * Examine current texture environment state and generate a unique
221 * key to identify it.
223 static void make_state_key( GLcontext
*ctx
, struct state_key
*key
)
227 memset(key
, 0, sizeof(*key
));
229 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
230 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
233 if (!texUnit
->_ReallyEnabled
|| !texUnit
->Enabled
)
236 format
= texUnit
->_Current
->Image
[0][texUnit
->_Current
->BaseLevel
]->_BaseFormat
;
238 key
->unit
[i
].enabled
= 1;
239 key
->enabled_units
|= (1<<i
);
241 key
->unit
[i
].source_index
=
242 translate_tex_src_bit(texUnit
->_ReallyEnabled
);
243 key
->unit
[i
].shadow
= ((texUnit
->_Current
->CompareMode
== GL_COMPARE_R_TO_TEXTURE
) &&
244 ((format
== GL_DEPTH_COMPONENT
) ||
245 (format
== GL_DEPTH_STENCIL_EXT
)));
247 key
->unit
[i
].NumArgsRGB
= texUnit
->_CurrentCombine
->_NumArgsRGB
;
248 key
->unit
[i
].NumArgsA
= texUnit
->_CurrentCombine
->_NumArgsA
;
250 key
->unit
[i
].ModeRGB
=
251 translate_mode(texUnit
->_CurrentCombine
->ModeRGB
);
253 translate_mode(texUnit
->_CurrentCombine
->ModeA
);
255 key
->unit
[i
].ScaleShiftRGB
= texUnit
->_CurrentCombine
->ScaleShiftRGB
;
256 key
->unit
[i
].ScaleShiftA
= texUnit
->_CurrentCombine
->ScaleShiftA
;
259 key
->unit
[i
].OptRGB
[j
].Operand
=
260 translate_operand(texUnit
->_CurrentCombine
->OperandRGB
[j
]);
261 key
->unit
[i
].OptA
[j
].Operand
=
262 translate_operand(texUnit
->_CurrentCombine
->OperandA
[j
]);
263 key
->unit
[i
].OptRGB
[j
].Source
=
264 translate_source(texUnit
->_CurrentCombine
->SourceRGB
[j
]);
265 key
->unit
[i
].OptA
[j
].Source
=
266 translate_source(texUnit
->_CurrentCombine
->SourceA
[j
]);
270 if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
)
271 key
->separate_specular
= 1;
273 if (ctx
->Fog
.Enabled
) {
274 key
->fog_enabled
= 1;
275 key
->fog_mode
= translate_fog_mode(ctx
->Fog
.Mode
);
279 /* Use uregs to represent registers internally, translate to Mesa's
280 * expected formats on emit.
282 * NOTE: These are passed by value extensively in this file rather
283 * than as usual by pointer reference. If this disturbs you, try
284 * remembering they are just 32bits in size.
286 * GCC is smart enough to deal with these dword-sized structures in
287 * much the same way as if I had defined them as dwords and was using
288 * macros to access and set the fields. This is much nicer and easier
301 static const struct ureg undef
= {
312 /* State used to build the fragment program:
314 struct texenv_fragment_program
{
315 struct gl_fragment_program
*program
;
317 struct state_key
*state
;
319 GLbitfield alu_temps
; /* Track texture indirections, see spec. */
320 GLbitfield temps_output
; /* Track texture indirections, see spec. */
321 GLbitfield temp_in_use
; /* Tracks temporary regs which are in use. */
324 struct ureg src_texture
[MAX_TEXTURE_COORD_UNITS
];
325 /* Reg containing each texture unit's sampled texture color,
329 struct ureg src_previous
; /* Reg containing color from previous
330 * stage. May need to be decl'd.
333 GLuint last_tex_stage
; /* Number of last enabled texture unit */
342 static struct ureg
make_ureg(GLuint file
, GLuint idx
)
350 reg
.swz
= SWIZZLE_NOOP
;
355 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
357 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
360 GET_SWZ(reg
.swz
, w
));
365 static struct ureg
swizzle1( struct ureg reg
, int x
)
367 return swizzle(reg
, x
, x
, x
, x
);
370 static struct ureg
negate( struct ureg reg
)
376 static GLboolean
is_undef( struct ureg reg
)
378 return reg
.file
== PROGRAM_UNDEFINED
;
382 static struct ureg
get_temp( struct texenv_fragment_program
*p
)
386 /* First try and reuse temps which have been used already:
388 bit
= _mesa_ffs( ~p
->temp_in_use
& p
->alu_temps
);
390 /* Then any unused temporary:
393 bit
= _mesa_ffs( ~p
->temp_in_use
);
396 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
400 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
401 p
->program
->Base
.NumTemporaries
= bit
;
403 p
->temp_in_use
|= 1<<(bit
-1);
404 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
407 static struct ureg
get_tex_temp( struct texenv_fragment_program
*p
)
411 /* First try to find availble temp not previously used (to avoid
412 * starting a new texture indirection). According to the spec, the
413 * ~p->temps_output isn't necessary, but will keep it there for
416 bit
= _mesa_ffs( ~p
->temp_in_use
& ~p
->alu_temps
& ~p
->temps_output
);
418 /* Then any unused temporary:
421 bit
= _mesa_ffs( ~p
->temp_in_use
);
424 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
428 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
429 p
->program
->Base
.NumTemporaries
= bit
;
431 p
->temp_in_use
|= 1<<(bit
-1);
432 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
436 static void release_temps(GLcontext
*ctx
, struct texenv_fragment_program
*p
)
438 GLuint max_temp
= ctx
->Const
.FragmentProgram
.MaxTemps
;
440 /* KW: To support tex_env_crossbar, don't release the registers in
443 if (max_temp
>= sizeof(int) * 8)
444 p
->temp_in_use
= p
->temps_output
;
446 p
->temp_in_use
= ~((1<<max_temp
)-1) | p
->temps_output
;
450 static struct ureg
register_param5( struct texenv_fragment_program
*p
,
457 gl_state_index tokens
[STATE_LENGTH
];
464 idx
= _mesa_add_state_reference( p
->program
->Base
.Parameters
, tokens
);
465 return make_ureg(PROGRAM_STATE_VAR
, idx
);
469 #define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
470 #define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
471 #define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
472 #define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
475 static struct ureg
register_input( struct texenv_fragment_program
*p
, GLuint input
)
477 p
->program
->Base
.InputsRead
|= (1 << input
);
478 return make_ureg(PROGRAM_INPUT
, input
);
482 static void emit_arg( struct prog_src_register
*reg
,
485 reg
->File
= ureg
.file
;
486 reg
->Index
= ureg
.idx
;
487 reg
->Swizzle
= ureg
.swz
;
488 reg
->NegateBase
= ureg
.negatebase
? 0xf : 0x0;
490 reg
->NegateAbs
= ureg
.negateabs
;
493 static void emit_dst( struct prog_dst_register
*dst
,
494 struct ureg ureg
, GLuint mask
)
496 dst
->File
= ureg
.file
;
497 dst
->Index
= ureg
.idx
;
498 dst
->WriteMask
= mask
;
499 dst
->CondMask
= COND_TR
; /* always pass cond test */
500 dst
->CondSwizzle
= SWIZZLE_NOOP
;
503 static struct prog_instruction
*
504 emit_op(struct texenv_fragment_program
*p
,
513 GLuint nr
= p
->program
->Base
.NumInstructions
++;
514 struct prog_instruction
*inst
= &p
->program
->Base
.Instructions
[nr
];
516 assert(nr
< MAX_INSTRUCTIONS
);
518 _mesa_init_instructions(inst
, 1);
521 emit_arg( &inst
->SrcReg
[0], src0
);
522 emit_arg( &inst
->SrcReg
[1], src1
);
523 emit_arg( &inst
->SrcReg
[2], src2
);
525 inst
->SaturateMode
= saturate
? SATURATE_ZERO_ONE
: SATURATE_OFF
;
527 emit_dst( &inst
->DstReg
, dest
, mask
);
529 /* Accounting for indirection tracking:
531 if (dest
.file
== PROGRAM_TEMPORARY
)
532 p
->temps_output
|= 1 << dest
.idx
;
538 static struct ureg
emit_arith( struct texenv_fragment_program
*p
,
547 emit_op(p
, op
, dest
, mask
, saturate
, src0
, src1
, src2
);
549 /* Accounting for indirection tracking:
551 if (src0
.file
== PROGRAM_TEMPORARY
)
552 p
->alu_temps
|= 1 << src0
.idx
;
554 if (!is_undef(src1
) && src1
.file
== PROGRAM_TEMPORARY
)
555 p
->alu_temps
|= 1 << src1
.idx
;
557 if (!is_undef(src2
) && src2
.file
== PROGRAM_TEMPORARY
)
558 p
->alu_temps
|= 1 << src2
.idx
;
560 if (dest
.file
== PROGRAM_TEMPORARY
)
561 p
->alu_temps
|= 1 << dest
.idx
;
563 p
->program
->Base
.NumAluInstructions
++;
567 static struct ureg
emit_texld( struct texenv_fragment_program
*p
,
575 struct prog_instruction
*inst
= emit_op( p
, op
,
577 GL_FALSE
, /* don't saturate? */
582 inst
->TexSrcTarget
= tex_idx
;
583 inst
->TexSrcUnit
= tex_unit
;
585 p
->program
->Base
.NumTexInstructions
++;
587 /* Is this a texture indirection?
589 if ((coord
.file
== PROGRAM_TEMPORARY
&&
590 (p
->temps_output
& (1<<coord
.idx
))) ||
591 (dest
.file
== PROGRAM_TEMPORARY
&&
592 (p
->alu_temps
& (1<<dest
.idx
)))) {
593 p
->program
->Base
.NumTexIndirections
++;
594 p
->temps_output
= 1<<coord
.idx
;
596 assert(0); /* KW: texture env crossbar */
603 static struct ureg
register_const4f( struct texenv_fragment_program
*p
,
615 idx
= _mesa_add_unnamed_constant( p
->program
->Base
.Parameters
, values
, 4,
617 ASSERT(swizzle
== SWIZZLE_NOOP
);
618 return make_ureg(PROGRAM_CONSTANT
, idx
);
621 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
622 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
623 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
624 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
627 static struct ureg
get_one( struct texenv_fragment_program
*p
)
629 if (is_undef(p
->one
))
630 p
->one
= register_scalar_const(p
, 1.0);
634 static struct ureg
get_half( struct texenv_fragment_program
*p
)
636 if (is_undef(p
->half
))
637 p
->half
= register_scalar_const(p
, 0.5);
641 static struct ureg
get_zero( struct texenv_fragment_program
*p
)
643 if (is_undef(p
->zero
))
644 p
->zero
= register_scalar_const(p
, 0.0);
649 static void program_error( struct texenv_fragment_program
*p
, const char *msg
)
651 _mesa_problem(NULL
, msg
);
655 static struct ureg
get_source( struct texenv_fragment_program
*p
,
656 GLuint src
, GLuint unit
)
660 assert(!is_undef(p
->src_texture
[unit
]));
661 return p
->src_texture
[unit
];
671 assert(!is_undef(p
->src_texture
[src
- SRC_TEXTURE0
]));
672 return p
->src_texture
[src
- SRC_TEXTURE0
];
675 return register_param2(p
, STATE_TEXENV_COLOR
, unit
);
677 case SRC_PRIMARY_COLOR
:
678 return register_input(p
, FRAG_ATTRIB_COL0
);
682 if (is_undef(p
->src_previous
))
683 return register_input(p
, FRAG_ATTRIB_COL0
);
685 return p
->src_previous
;
689 static struct ureg
emit_combine_source( struct texenv_fragment_program
*p
,
695 struct ureg arg
, src
, one
;
697 src
= get_source(p
, source
, unit
);
700 case OPR_ONE_MINUS_SRC_COLOR
:
702 * Emit tmp = 1.0 - arg.xyzw
706 return emit_arith( p
, OPCODE_SUB
, arg
, mask
, 0, one
, src
, undef
);
709 if (mask
== WRITEMASK_W
)
712 return swizzle1( src
, SWIZZLE_W
);
713 case OPR_ONE_MINUS_SRC_ALPHA
:
715 * Emit tmp = 1.0 - arg.wwww
719 return emit_arith(p
, OPCODE_SUB
, arg
, mask
, 0,
720 one
, swizzle1(src
, SWIZZLE_W
), undef
);
731 static GLboolean
args_match( struct state_key
*key
, GLuint unit
)
733 GLuint i
, nr
= key
->unit
[unit
].NumArgsRGB
;
735 for (i
= 0 ; i
< nr
; i
++) {
736 if (key
->unit
[unit
].OptA
[i
].Source
!= key
->unit
[unit
].OptRGB
[i
].Source
)
739 switch(key
->unit
[unit
].OptA
[i
].Operand
) {
741 switch(key
->unit
[unit
].OptRGB
[i
].Operand
) {
749 case OPR_ONE_MINUS_SRC_ALPHA
:
750 switch(key
->unit
[unit
].OptRGB
[i
].Operand
) {
751 case OPR_ONE_MINUS_SRC_COLOR
:
752 case OPR_ONE_MINUS_SRC_ALPHA
:
759 return GL_FALSE
; /* impossible */
766 static struct ureg
emit_combine( struct texenv_fragment_program
*p
,
773 const struct mode_opt
*opt
)
776 struct ureg tmp
, half
;
779 tmp
= undef
; /* silence warning (bug 5318) */
781 for (i
= 0; i
< nr
; i
++)
782 src
[i
] = emit_combine_source( p
, mask
, unit
, opt
[i
].Source
, opt
[i
].Operand
);
786 if (mask
== WRITEMASK_XYZW
&& !saturate
)
789 return emit_arith( p
, OPCODE_MOV
, dest
, mask
, saturate
, src
[0], undef
, undef
);
791 return emit_arith( p
, OPCODE_MUL
, dest
, mask
, saturate
,
792 src
[0], src
[1], undef
);
794 return emit_arith( p
, OPCODE_ADD
, dest
, mask
, saturate
,
795 src
[0], src
[1], undef
);
796 case MODE_ADD_SIGNED
:
802 emit_arith( p
, OPCODE_ADD
, tmp
, mask
, 0, src
[0], src
[1], undef
);
803 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp
, half
, undef
);
805 case MODE_INTERPOLATE
:
806 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
808 return emit_arith( p
, OPCODE_LRP
, dest
, mask
, saturate
, src
[2], src
[0], src
[1] );
811 return emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, src
[0], src
[1], undef
);
814 case MODE_DOT3_RGBA_EXT
:
815 case MODE_DOT3_RGB_EXT
:
816 case MODE_DOT3_RGB
: {
817 struct ureg tmp0
= get_temp( p
);
818 struct ureg tmp1
= get_temp( p
);
819 struct ureg neg1
= register_scalar_const(p
, -1);
820 struct ureg two
= register_scalar_const(p
, 2);
825 * dst = tmp0 dot3 tmp1
827 emit_arith( p
, OPCODE_MAD
, tmp0
, WRITEMASK_XYZW
, 0,
830 if (_mesa_memcmp(&src
[0], &src
[1], sizeof(struct ureg
)) == 0)
833 emit_arith( p
, OPCODE_MAD
, tmp1
, WRITEMASK_XYZW
, 0,
835 emit_arith( p
, OPCODE_DP3
, dest
, mask
, saturate
, tmp0
, tmp1
, undef
);
838 case MODE_MODULATE_ADD_ATI
:
839 /* Arg0 * Arg2 + Arg1 */
840 return emit_arith( p
, OPCODE_MAD
, dest
, mask
, saturate
,
841 src
[0], src
[2], src
[1] );
842 case MODE_MODULATE_SIGNED_ADD_ATI
: {
843 /* Arg0 * Arg2 + Arg1 - 0.5 */
844 struct ureg tmp0
= get_temp(p
);
846 emit_arith( p
, OPCODE_MAD
, tmp0
, mask
, 0, src
[0], src
[2], src
[1] );
847 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp0
, half
, undef
);
850 case MODE_MODULATE_SUBTRACT_ATI
:
851 /* Arg0 * Arg2 - Arg1 */
852 emit_arith( p
, OPCODE_MAD
, dest
, mask
, 0, src
[0], src
[2], negate(src
[1]) );
861 * Generate instructions for one texture unit's env/combiner mode.
864 emit_texenv(struct texenv_fragment_program
*p
, GLuint unit
)
866 struct state_key
*key
= p
->state
;
867 GLboolean saturate
= (unit
< p
->last_tex_stage
);
868 GLuint rgb_shift
, alpha_shift
;
869 struct ureg out
, shift
;
872 if (!key
->unit
[unit
].enabled
) {
873 return get_source(p
, SRC_PREVIOUS
, 0);
876 switch (key
->unit
[unit
].ModeRGB
) {
877 case MODE_DOT3_RGB_EXT
:
878 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
881 case MODE_DOT3_RGBA_EXT
:
886 rgb_shift
= key
->unit
[unit
].ScaleShiftRGB
;
887 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
891 /* If this is the very last calculation, emit direct to output reg:
893 if (key
->separate_specular
||
894 unit
!= p
->last_tex_stage
||
897 dest
= get_temp( p
);
899 dest
= make_ureg(PROGRAM_OUTPUT
, FRAG_RESULT_COLR
);
901 /* Emit the RGB and A combine ops
903 if (key
->unit
[unit
].ModeRGB
== key
->unit
[unit
].ModeA
&&
904 args_match(key
, unit
)) {
905 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, saturate
,
907 key
->unit
[unit
].NumArgsRGB
,
908 key
->unit
[unit
].ModeRGB
,
909 key
->unit
[unit
].OptRGB
);
911 else if (key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA_EXT
||
912 key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA
) {
914 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, saturate
,
916 key
->unit
[unit
].NumArgsRGB
,
917 key
->unit
[unit
].ModeRGB
,
918 key
->unit
[unit
].OptRGB
);
921 /* Need to do something to stop from re-emitting identical
922 * argument calculations here:
924 out
= emit_combine( p
, dest
, WRITEMASK_XYZ
, saturate
,
926 key
->unit
[unit
].NumArgsRGB
,
927 key
->unit
[unit
].ModeRGB
,
928 key
->unit
[unit
].OptRGB
);
929 out
= emit_combine( p
, dest
, WRITEMASK_W
, saturate
,
931 key
->unit
[unit
].NumArgsA
,
932 key
->unit
[unit
].ModeA
,
933 key
->unit
[unit
].OptA
);
936 /* Deal with the final shift:
938 if (alpha_shift
|| rgb_shift
) {
939 if (rgb_shift
== alpha_shift
) {
940 shift
= register_scalar_const(p
, (GLfloat
)(1<<rgb_shift
));
943 shift
= register_const4f(p
,
944 (GLfloat
)(1<<rgb_shift
),
945 (GLfloat
)(1<<rgb_shift
),
946 (GLfloat
)(1<<rgb_shift
),
947 (GLfloat
)(1<<alpha_shift
));
949 return emit_arith( p
, OPCODE_MUL
, dest
, WRITEMASK_XYZW
,
950 saturate
, out
, shift
, undef
);
958 * Generate instruction for getting a texture source term.
960 static void load_texture( struct texenv_fragment_program
*p
, GLuint unit
)
962 if (is_undef(p
->src_texture
[unit
])) {
963 GLuint dim
= p
->state
->unit
[unit
].source_index
;
964 struct ureg texcoord
= register_input(p
, FRAG_ATTRIB_TEX0
+unit
);
965 struct ureg tmp
= get_tex_temp( p
);
967 if (dim
== TEXTURE_UNKNOWN_INDEX
)
968 program_error(p
, "TexSrcBit");
970 /* TODO: Use D0_MASK_XY where possible.
972 if (p
->state
->unit
[unit
].enabled
) {
973 p
->src_texture
[unit
] = emit_texld( p
, OPCODE_TXP
,
975 unit
, dim
, texcoord
);
976 if (p
->state
->unit
[unit
].shadow
)
977 p
->program
->Base
.ShadowSamplers
|= 1 << unit
;
979 p
->program
->Base
.SamplersUsed
|= (1 << unit
);
980 /* This identity mapping should already be in place
981 * (see _mesa_init_program_struct()) but let's be safe.
983 p
->program
->Base
.SamplerUnits
[unit
] = unit
;
986 p
->src_texture
[unit
] = get_zero(p
);
990 static GLboolean
load_texenv_source( struct texenv_fragment_program
*p
,
991 GLuint src
, GLuint unit
)
995 load_texture(p
, unit
);
1006 load_texture(p
, src
- SRC_TEXTURE0
);
1018 * Generate instructions for loading all texture source terms.
1021 load_texunit_sources( struct texenv_fragment_program
*p
, int unit
)
1023 struct state_key
*key
= p
->state
;
1026 for (i
= 0; i
< key
->unit
[unit
].NumArgsRGB
; i
++) {
1027 load_texenv_source( p
, key
->unit
[unit
].OptRGB
[i
].Source
, unit
);
1030 for (i
= 0; i
< key
->unit
[unit
].NumArgsA
; i
++) {
1031 load_texenv_source( p
, key
->unit
[unit
].OptA
[i
].Source
, unit
);
1039 * Generate a new fragment program which implements the context's
1040 * current texture env/combine mode.
1043 create_new_program(GLcontext
*ctx
, struct state_key
*key
,
1044 struct gl_fragment_program
*program
)
1046 struct prog_instruction instBuffer
[MAX_INSTRUCTIONS
];
1047 struct texenv_fragment_program p
;
1049 struct ureg cf
, out
;
1051 _mesa_memset(&p
, 0, sizeof(p
));
1054 p
.program
= program
;
1056 /* During code generation, use locally-allocated instruction buffer,
1057 * then alloc dynamic storage below.
1059 p
.program
->Base
.Instructions
= instBuffer
;
1060 p
.program
->Base
.Target
= GL_FRAGMENT_PROGRAM_ARB
;
1061 p
.program
->Base
.NumTexIndirections
= 1; /* correct? */
1062 p
.program
->Base
.NumTexInstructions
= 0;
1063 p
.program
->Base
.NumAluInstructions
= 0;
1064 p
.program
->Base
.String
= NULL
;
1065 p
.program
->Base
.NumInstructions
=
1066 p
.program
->Base
.NumTemporaries
=
1067 p
.program
->Base
.NumParameters
=
1068 p
.program
->Base
.NumAttributes
= p
.program
->Base
.NumAddressRegs
= 0;
1069 p
.program
->Base
.Parameters
= _mesa_new_parameter_list();
1071 p
.program
->Base
.InputsRead
= 0;
1072 p
.program
->Base
.OutputsWritten
= 1 << FRAG_RESULT_COLR
;
1074 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
1075 p
.src_texture
[unit
] = undef
;
1077 p
.src_previous
= undef
;
1082 p
.last_tex_stage
= 0;
1083 release_temps(ctx
, &p
);
1085 if (key
->enabled_units
) {
1086 /* First pass - to support texture_env_crossbar, first identify
1087 * all referenced texture sources and emit texld instructions
1090 for (unit
= 0 ; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
1091 if (key
->unit
[unit
].enabled
) {
1092 load_texunit_sources( &p
, unit
);
1093 p
.last_tex_stage
= unit
;
1096 /* Second pass - emit combine instructions to build final color:
1098 for (unit
= 0 ; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
1099 if (key
->enabled_units
& (1<<unit
)) {
1100 p
.src_previous
= emit_texenv( &p
, unit
);
1101 release_temps(ctx
, &p
); /* release all temps */
1105 cf
= get_source( &p
, SRC_PREVIOUS
, 0 );
1106 out
= make_ureg( PROGRAM_OUTPUT
, FRAG_RESULT_COLR
);
1108 if (key
->separate_specular
) {
1109 /* Emit specular add.
1111 struct ureg s
= register_input(&p
, FRAG_ATTRIB_COL1
);
1112 emit_arith( &p
, OPCODE_ADD
, out
, WRITEMASK_XYZ
, 0, cf
, s
, undef
);
1113 emit_arith( &p
, OPCODE_MOV
, out
, WRITEMASK_W
, 0, cf
, undef
, undef
);
1115 else if (_mesa_memcmp(&cf
, &out
, sizeof(cf
)) != 0) {
1116 /* Will wind up in here if no texture enabled or a couple of
1117 * other scenarios (GL_REPLACE for instance).
1119 emit_arith( &p
, OPCODE_MOV
, out
, WRITEMASK_XYZW
, 0, cf
, undef
, undef
);
1124 emit_arith( &p
, OPCODE_END
, undef
, WRITEMASK_XYZW
, 0, undef
, undef
, undef
);
1126 if (key
->fog_enabled
) {
1127 /* Pull fog mode from GLcontext, the value in the state key is
1128 * a reduced value and not what is expected in FogOption
1130 p
.program
->FogOption
= ctx
->Fog
.Mode
;
1131 p
.program
->Base
.InputsRead
|= FRAG_BIT_FOGC
; /* XXX new */
1133 p
.program
->FogOption
= GL_NONE
;
1135 if (p
.program
->Base
.NumTexIndirections
> ctx
->Const
.FragmentProgram
.MaxTexIndirections
)
1136 program_error(&p
, "Exceeded max nr indirect texture lookups");
1138 if (p
.program
->Base
.NumTexInstructions
> ctx
->Const
.FragmentProgram
.MaxTexInstructions
)
1139 program_error(&p
, "Exceeded max TEX instructions");
1141 if (p
.program
->Base
.NumAluInstructions
> ctx
->Const
.FragmentProgram
.MaxAluInstructions
)
1142 program_error(&p
, "Exceeded max ALU instructions");
1144 ASSERT(p
.program
->Base
.NumInstructions
<= MAX_INSTRUCTIONS
);
1146 /* Allocate final instruction array */
1147 p
.program
->Base
.Instructions
1148 = _mesa_alloc_instructions(p
.program
->Base
.NumInstructions
);
1149 if (!p
.program
->Base
.Instructions
) {
1150 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1151 "generating tex env program");
1154 _mesa_copy_instructions(p
.program
->Base
.Instructions
, instBuffer
,
1155 p
.program
->Base
.NumInstructions
);
1157 if (p
.program
->FogOption
) {
1158 _mesa_append_fog_code(ctx
, p
.program
);
1159 p
.program
->FogOption
= GL_NONE
;
1163 /* Notify driver the fragment program has (actually) changed.
1165 if (ctx
->Driver
.ProgramStringNotify
) {
1166 ctx
->Driver
.ProgramStringNotify( ctx
, GL_FRAGMENT_PROGRAM_ARB
,
1171 _mesa_print_program(&p
.program
->Base
);
1178 * Return a fragment program which implements the current
1179 * fixed-function texture, fog and color-sum operations.
1181 struct gl_fragment_program
*
1182 _mesa_get_fixed_func_fragment_program(GLcontext
*ctx
)
1184 struct gl_fragment_program
*prog
;
1185 struct state_key key
;
1187 make_state_key(ctx
, &key
);
1189 prog
= (struct gl_fragment_program
*)
1190 _mesa_search_program_cache(ctx
->FragmentProgram
.Cache
,
1194 prog
= (struct gl_fragment_program
*)
1195 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
1197 create_new_program(ctx
, &key
, prog
);
1199 _mesa_program_cache_insert(ctx
, ctx
->FragmentProgram
.Cache
,
1200 &key
, sizeof(key
), &prog
->Base
);
1209 * If _MaintainTexEnvProgram is set we'll generate a fragment program that
1210 * implements the current texture env/combine mode.
1211 * This function generates that program and puts it into effect.
1214 _mesa_UpdateTexEnvProgram( GLcontext
*ctx
)
1216 const struct gl_fragment_program
*prev
= ctx
->FragmentProgram
._Current
;
1218 ASSERT(ctx
->FragmentProgram
._MaintainTexEnvProgram
);
1220 /* If a conventional fragment program/shader isn't in effect... */
1221 if (!ctx
->FragmentProgram
._Enabled
&&
1222 (!ctx
->Shader
.CurrentProgram
||
1223 !ctx
->Shader
.CurrentProgram
->FragmentProgram
) )
1225 struct gl_fragment_program
*newProg
;
1227 newProg
= _mesa_get_fixed_func_fragment_program(ctx
);
1229 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._Current
, newProg
);
1230 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._TexEnvProgram
, newProg
);
1233 /* Tell the driver about the change. Could define a new target for
1236 if (ctx
->FragmentProgram
._Current
!= prev
&& ctx
->Driver
.BindProgram
) {
1237 ctx
->Driver
.BindProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
1238 (struct gl_program
*) ctx
->FragmentProgram
._Current
);