1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * Copyright 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
32 #include "shader/program.h"
33 #include "shader/prog_parameter.h"
34 #include "shader/prog_cache.h"
35 #include "shader/prog_instruction.h"
36 #include "shader/prog_print.h"
37 #include "shader/prog_statevars.h"
38 #include "shader/programopt.h"
39 #include "texenvprogram.h"
46 * Note on texture units:
48 * The number of texture units supported by fixed-function fragment
49 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
50 * That's because there's a one-to-one correspondence between texture
51 * coordinates and samplers in fixed-function processing.
53 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
54 * sets of texcoords, so is fixed-function fragment processing.
56 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
60 struct texenvprog_cache_item
64 struct gl_fragment_program
*data
;
65 struct texenvprog_cache_item
*next
;
70 * This MAX is probably a bit generous, but that's OK. There can be
71 * up to four instructions per texture unit (TEX + 3 for combine),
72 * then there's fog and specular add.
74 #define MAX_INSTRUCTIONS ((MAX_TEXTURE_COORD_UNITS * 4) + 12)
76 #define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
84 GLbitfield enabled_units
;
85 GLuint separate_specular
:1;
91 GLuint source_index
:3; /* one of TEXTURE_1D/2D/3D/CUBE/RECT_INDEX */
93 GLuint ScaleShiftRGB
:2;
98 struct mode_opt OptRGB
[MAX_TERMS
];
102 struct mode_opt OptA
[MAX_TERMS
];
109 #define FOG_UNKNOWN 3
111 static GLuint
translate_fog_mode( GLenum mode
)
114 case GL_LINEAR
: return FOG_LINEAR
;
115 case GL_EXP
: return FOG_EXP
;
116 case GL_EXP2
: return FOG_EXP2
;
117 default: return FOG_UNKNOWN
;
121 #define OPR_SRC_COLOR 0
122 #define OPR_ONE_MINUS_SRC_COLOR 1
123 #define OPR_SRC_ALPHA 2
124 #define OPR_ONE_MINUS_SRC_ALPHA 3
127 #define OPR_UNKNOWN 7
129 static GLuint
translate_operand( GLenum operand
)
132 case GL_SRC_COLOR
: return OPR_SRC_COLOR
;
133 case GL_ONE_MINUS_SRC_COLOR
: return OPR_ONE_MINUS_SRC_COLOR
;
134 case GL_SRC_ALPHA
: return OPR_SRC_ALPHA
;
135 case GL_ONE_MINUS_SRC_ALPHA
: return OPR_ONE_MINUS_SRC_ALPHA
;
136 case GL_ZERO
: return OPR_ZERO
;
137 case GL_ONE
: return OPR_ONE
;
144 #define SRC_TEXTURE 0
145 #define SRC_TEXTURE0 1
146 #define SRC_TEXTURE1 2
147 #define SRC_TEXTURE2 3
148 #define SRC_TEXTURE3 4
149 #define SRC_TEXTURE4 5
150 #define SRC_TEXTURE5 6
151 #define SRC_TEXTURE6 7
152 #define SRC_TEXTURE7 8
153 #define SRC_CONSTANT 9
154 #define SRC_PRIMARY_COLOR 10
155 #define SRC_PREVIOUS 11
157 #define SRC_UNKNOWN 15
159 static GLuint
translate_source( GLenum src
)
162 case GL_TEXTURE
: return SRC_TEXTURE
;
170 case GL_TEXTURE7
: return SRC_TEXTURE0
+ (src
- GL_TEXTURE0
);
171 case GL_CONSTANT
: return SRC_CONSTANT
;
172 case GL_PRIMARY_COLOR
: return SRC_PRIMARY_COLOR
;
173 case GL_PREVIOUS
: return SRC_PREVIOUS
;
182 #define MODE_REPLACE 0 /* r = a0 */
183 #define MODE_MODULATE 1 /* r = a0 * a1 */
184 #define MODE_ADD 2 /* r = a0 + a1 */
185 #define MODE_ADD_SIGNED 3 /* r = a0 + a1 - 0.5 */
186 #define MODE_INTERPOLATE 4 /* r = a0 * a2 + a1 * (1 - a2) */
187 #define MODE_SUBTRACT 5 /* r = a0 - a1 */
188 #define MODE_DOT3_RGB 6 /* r = a0 . a1 */
189 #define MODE_DOT3_RGB_EXT 7 /* r = a0 . a1 */
190 #define MODE_DOT3_RGBA 8 /* r = a0 . a1 */
191 #define MODE_DOT3_RGBA_EXT 9 /* r = a0 . a1 */
192 #define MODE_MODULATE_ADD_ATI 10 /* r = a0 * a2 + a1 */
193 #define MODE_MODULATE_SIGNED_ADD_ATI 11 /* r = a0 * a2 + a1 - 0.5 */
194 #define MODE_MODULATE_SUBTRACT_ATI 12 /* r = a0 * a2 - a1 */
195 #define MODE_ADD_PRODUCTS 13 /* r = a0 * a1 + a2 * a3 */
196 #define MODE_ADD_PRODUCTS_SIGNED 14 /* r = a0 * a1 + a2 * a3 - 0.5 */
197 #define MODE_UNKNOWN 15
200 * Translate GL combiner state into a MODE_x value
202 static GLuint
translate_mode( GLenum envMode
, GLenum mode
)
205 case GL_REPLACE
: return MODE_REPLACE
;
206 case GL_MODULATE
: return MODE_MODULATE
;
208 if (envMode
== GL_COMBINE4_NV
)
209 return MODE_ADD_PRODUCTS
;
213 if (envMode
== GL_COMBINE4_NV
)
214 return MODE_ADD_PRODUCTS_SIGNED
;
216 return MODE_ADD_SIGNED
;
217 case GL_INTERPOLATE
: return MODE_INTERPOLATE
;
218 case GL_SUBTRACT
: return MODE_SUBTRACT
;
219 case GL_DOT3_RGB
: return MODE_DOT3_RGB
;
220 case GL_DOT3_RGB_EXT
: return MODE_DOT3_RGB_EXT
;
221 case GL_DOT3_RGBA
: return MODE_DOT3_RGBA
;
222 case GL_DOT3_RGBA_EXT
: return MODE_DOT3_RGBA_EXT
;
223 case GL_MODULATE_ADD_ATI
: return MODE_MODULATE_ADD_ATI
;
224 case GL_MODULATE_SIGNED_ADD_ATI
: return MODE_MODULATE_SIGNED_ADD_ATI
;
225 case GL_MODULATE_SUBTRACT_ATI
: return MODE_MODULATE_SUBTRACT_ATI
;
232 #define TEXTURE_UNKNOWN_INDEX 7
233 static GLuint
translate_tex_src_bit( GLbitfield bit
)
236 case TEXTURE_1D_BIT
: return TEXTURE_1D_INDEX
;
237 case TEXTURE_2D_BIT
: return TEXTURE_2D_INDEX
;
238 case TEXTURE_RECT_BIT
: return TEXTURE_RECT_INDEX
;
239 case TEXTURE_3D_BIT
: return TEXTURE_3D_INDEX
;
240 case TEXTURE_CUBE_BIT
: return TEXTURE_CUBE_INDEX
;
241 case TEXTURE_1D_ARRAY_BIT
: return TEXTURE_1D_ARRAY_INDEX
;
242 case TEXTURE_2D_ARRAY_BIT
: return TEXTURE_2D_ARRAY_INDEX
;
245 return TEXTURE_UNKNOWN_INDEX
;
250 * Examine current texture environment state and generate a unique
251 * key to identify it.
253 static void make_state_key( GLcontext
*ctx
, struct state_key
*key
)
257 memset(key
, 0, sizeof(*key
));
259 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
260 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
263 if (!texUnit
->_ReallyEnabled
|| !texUnit
->Enabled
)
266 format
= texUnit
->_Current
->Image
[0][texUnit
->_Current
->BaseLevel
]->_BaseFormat
;
268 key
->unit
[i
].enabled
= 1;
269 key
->enabled_units
|= (1<<i
);
271 key
->unit
[i
].source_index
=
272 translate_tex_src_bit(texUnit
->_ReallyEnabled
);
273 key
->unit
[i
].shadow
= ((texUnit
->_Current
->CompareMode
== GL_COMPARE_R_TO_TEXTURE
) &&
274 ((format
== GL_DEPTH_COMPONENT
) ||
275 (format
== GL_DEPTH_STENCIL_EXT
)));
277 key
->unit
[i
].NumArgsRGB
= texUnit
->_CurrentCombine
->_NumArgsRGB
;
278 key
->unit
[i
].NumArgsA
= texUnit
->_CurrentCombine
->_NumArgsA
;
280 key
->unit
[i
].ModeRGB
=
281 translate_mode(texUnit
->EnvMode
, texUnit
->_CurrentCombine
->ModeRGB
);
283 translate_mode(texUnit
->EnvMode
, texUnit
->_CurrentCombine
->ModeA
);
285 key
->unit
[i
].ScaleShiftRGB
= texUnit
->_CurrentCombine
->ScaleShiftRGB
;
286 key
->unit
[i
].ScaleShiftA
= texUnit
->_CurrentCombine
->ScaleShiftA
;
288 for (j
= 0; j
< MAX_TERMS
; j
++) {
289 key
->unit
[i
].OptRGB
[j
].Operand
=
290 translate_operand(texUnit
->_CurrentCombine
->OperandRGB
[j
]);
291 key
->unit
[i
].OptA
[j
].Operand
=
292 translate_operand(texUnit
->_CurrentCombine
->OperandA
[j
]);
293 key
->unit
[i
].OptRGB
[j
].Source
=
294 translate_source(texUnit
->_CurrentCombine
->SourceRGB
[j
]);
295 key
->unit
[i
].OptA
[j
].Source
=
296 translate_source(texUnit
->_CurrentCombine
->SourceA
[j
]);
300 if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
)
301 key
->separate_specular
= 1;
303 if (ctx
->Fog
.Enabled
) {
304 key
->fog_enabled
= 1;
305 key
->fog_mode
= translate_fog_mode(ctx
->Fog
.Mode
);
309 /* Use uregs to represent registers internally, translate to Mesa's
310 * expected formats on emit.
312 * NOTE: These are passed by value extensively in this file rather
313 * than as usual by pointer reference. If this disturbs you, try
314 * remembering they are just 32bits in size.
316 * GCC is smart enough to deal with these dword-sized structures in
317 * much the same way as if I had defined them as dwords and was using
318 * macros to access and set the fields. This is much nicer and easier
331 static const struct ureg undef
= {
342 /* State used to build the fragment program:
344 struct texenv_fragment_program
{
345 struct gl_fragment_program
*program
;
347 struct state_key
*state
;
349 GLbitfield alu_temps
; /* Track texture indirections, see spec. */
350 GLbitfield temps_output
; /* Track texture indirections, see spec. */
351 GLbitfield temp_in_use
; /* Tracks temporary regs which are in use. */
354 struct ureg src_texture
[MAX_TEXTURE_COORD_UNITS
];
355 /* Reg containing each texture unit's sampled texture color,
359 struct ureg src_previous
; /* Reg containing color from previous
360 * stage. May need to be decl'd.
363 GLuint last_tex_stage
; /* Number of last enabled texture unit */
372 static struct ureg
make_ureg(GLuint file
, GLuint idx
)
380 reg
.swz
= SWIZZLE_NOOP
;
385 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
387 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
390 GET_SWZ(reg
.swz
, w
));
395 static struct ureg
swizzle1( struct ureg reg
, int x
)
397 return swizzle(reg
, x
, x
, x
, x
);
400 static struct ureg
negate( struct ureg reg
)
406 static GLboolean
is_undef( struct ureg reg
)
408 return reg
.file
== PROGRAM_UNDEFINED
;
412 static struct ureg
get_temp( struct texenv_fragment_program
*p
)
416 /* First try and reuse temps which have been used already:
418 bit
= _mesa_ffs( ~p
->temp_in_use
& p
->alu_temps
);
420 /* Then any unused temporary:
423 bit
= _mesa_ffs( ~p
->temp_in_use
);
426 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
430 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
431 p
->program
->Base
.NumTemporaries
= bit
;
433 p
->temp_in_use
|= 1<<(bit
-1);
434 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
437 static struct ureg
get_tex_temp( struct texenv_fragment_program
*p
)
441 /* First try to find availble temp not previously used (to avoid
442 * starting a new texture indirection). According to the spec, the
443 * ~p->temps_output isn't necessary, but will keep it there for
446 bit
= _mesa_ffs( ~p
->temp_in_use
& ~p
->alu_temps
& ~p
->temps_output
);
448 /* Then any unused temporary:
451 bit
= _mesa_ffs( ~p
->temp_in_use
);
454 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
458 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
459 p
->program
->Base
.NumTemporaries
= bit
;
461 p
->temp_in_use
|= 1<<(bit
-1);
462 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
466 static void release_temps(GLcontext
*ctx
, struct texenv_fragment_program
*p
)
468 GLuint max_temp
= ctx
->Const
.FragmentProgram
.MaxTemps
;
470 /* KW: To support tex_env_crossbar, don't release the registers in
473 if (max_temp
>= sizeof(int) * 8)
474 p
->temp_in_use
= p
->temps_output
;
476 p
->temp_in_use
= ~((1<<max_temp
)-1) | p
->temps_output
;
480 static struct ureg
register_param5( struct texenv_fragment_program
*p
,
487 gl_state_index tokens
[STATE_LENGTH
];
494 idx
= _mesa_add_state_reference( p
->program
->Base
.Parameters
, tokens
);
495 return make_ureg(PROGRAM_STATE_VAR
, idx
);
499 #define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
500 #define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
501 #define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
502 #define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
505 static struct ureg
register_input( struct texenv_fragment_program
*p
, GLuint input
)
507 p
->program
->Base
.InputsRead
|= (1 << input
);
508 return make_ureg(PROGRAM_INPUT
, input
);
512 static void emit_arg( struct prog_src_register
*reg
,
515 reg
->File
= ureg
.file
;
516 reg
->Index
= ureg
.idx
;
517 reg
->Swizzle
= ureg
.swz
;
518 reg
->NegateBase
= ureg
.negatebase
? 0xf : 0x0;
520 reg
->NegateAbs
= ureg
.negateabs
;
523 static void emit_dst( struct prog_dst_register
*dst
,
524 struct ureg ureg
, GLuint mask
)
526 dst
->File
= ureg
.file
;
527 dst
->Index
= ureg
.idx
;
528 dst
->WriteMask
= mask
;
529 dst
->CondMask
= COND_TR
; /* always pass cond test */
530 dst
->CondSwizzle
= SWIZZLE_NOOP
;
533 static struct prog_instruction
*
534 emit_op(struct texenv_fragment_program
*p
,
543 GLuint nr
= p
->program
->Base
.NumInstructions
++;
544 struct prog_instruction
*inst
= &p
->program
->Base
.Instructions
[nr
];
546 assert(nr
< MAX_INSTRUCTIONS
);
548 _mesa_init_instructions(inst
, 1);
551 emit_arg( &inst
->SrcReg
[0], src0
);
552 emit_arg( &inst
->SrcReg
[1], src1
);
553 emit_arg( &inst
->SrcReg
[2], src2
);
555 inst
->SaturateMode
= saturate
? SATURATE_ZERO_ONE
: SATURATE_OFF
;
557 emit_dst( &inst
->DstReg
, dest
, mask
);
559 /* Accounting for indirection tracking:
561 if (dest
.file
== PROGRAM_TEMPORARY
)
562 p
->temps_output
|= 1 << dest
.idx
;
568 static struct ureg
emit_arith( struct texenv_fragment_program
*p
,
577 emit_op(p
, op
, dest
, mask
, saturate
, src0
, src1
, src2
);
579 /* Accounting for indirection tracking:
581 if (src0
.file
== PROGRAM_TEMPORARY
)
582 p
->alu_temps
|= 1 << src0
.idx
;
584 if (!is_undef(src1
) && src1
.file
== PROGRAM_TEMPORARY
)
585 p
->alu_temps
|= 1 << src1
.idx
;
587 if (!is_undef(src2
) && src2
.file
== PROGRAM_TEMPORARY
)
588 p
->alu_temps
|= 1 << src2
.idx
;
590 if (dest
.file
== PROGRAM_TEMPORARY
)
591 p
->alu_temps
|= 1 << dest
.idx
;
593 p
->program
->Base
.NumAluInstructions
++;
597 static struct ureg
emit_texld( struct texenv_fragment_program
*p
,
605 struct prog_instruction
*inst
= emit_op( p
, op
,
607 GL_FALSE
, /* don't saturate? */
612 inst
->TexSrcTarget
= tex_idx
;
613 inst
->TexSrcUnit
= tex_unit
;
615 p
->program
->Base
.NumTexInstructions
++;
617 /* Is this a texture indirection?
619 if ((coord
.file
== PROGRAM_TEMPORARY
&&
620 (p
->temps_output
& (1<<coord
.idx
))) ||
621 (dest
.file
== PROGRAM_TEMPORARY
&&
622 (p
->alu_temps
& (1<<dest
.idx
)))) {
623 p
->program
->Base
.NumTexIndirections
++;
624 p
->temps_output
= 1<<coord
.idx
;
626 assert(0); /* KW: texture env crossbar */
633 static struct ureg
register_const4f( struct texenv_fragment_program
*p
,
645 idx
= _mesa_add_unnamed_constant( p
->program
->Base
.Parameters
, values
, 4,
647 ASSERT(swizzle
== SWIZZLE_NOOP
);
648 return make_ureg(PROGRAM_CONSTANT
, idx
);
651 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
652 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
653 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
654 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
657 static struct ureg
get_one( struct texenv_fragment_program
*p
)
659 if (is_undef(p
->one
))
660 p
->one
= register_scalar_const(p
, 1.0);
664 static struct ureg
get_half( struct texenv_fragment_program
*p
)
666 if (is_undef(p
->half
))
667 p
->half
= register_scalar_const(p
, 0.5);
671 static struct ureg
get_zero( struct texenv_fragment_program
*p
)
673 if (is_undef(p
->zero
))
674 p
->zero
= register_scalar_const(p
, 0.0);
679 static void program_error( struct texenv_fragment_program
*p
, const char *msg
)
681 _mesa_problem(NULL
, msg
);
685 static struct ureg
get_source( struct texenv_fragment_program
*p
,
686 GLuint src
, GLuint unit
)
690 assert(!is_undef(p
->src_texture
[unit
]));
691 return p
->src_texture
[unit
];
701 assert(!is_undef(p
->src_texture
[src
- SRC_TEXTURE0
]));
702 return p
->src_texture
[src
- SRC_TEXTURE0
];
705 return register_param2(p
, STATE_TEXENV_COLOR
, unit
);
707 case SRC_PRIMARY_COLOR
:
708 return register_input(p
, FRAG_ATTRIB_COL0
);
714 if (is_undef(p
->src_previous
))
715 return register_input(p
, FRAG_ATTRIB_COL0
);
717 return p
->src_previous
;
724 static struct ureg
emit_combine_source( struct texenv_fragment_program
*p
,
730 struct ureg arg
, src
, one
;
732 src
= get_source(p
, source
, unit
);
735 case OPR_ONE_MINUS_SRC_COLOR
:
737 * Emit tmp = 1.0 - arg.xyzw
741 return emit_arith( p
, OPCODE_SUB
, arg
, mask
, 0, one
, src
, undef
);
744 if (mask
== WRITEMASK_W
)
747 return swizzle1( src
, SWIZZLE_W
);
748 case OPR_ONE_MINUS_SRC_ALPHA
:
750 * Emit tmp = 1.0 - arg.wwww
754 return emit_arith(p
, OPCODE_SUB
, arg
, mask
, 0,
755 one
, swizzle1(src
, SWIZZLE_W
), undef
);
768 static GLboolean
args_match( struct state_key
*key
, GLuint unit
)
770 GLuint i
, nr
= key
->unit
[unit
].NumArgsRGB
;
772 for (i
= 0 ; i
< nr
; i
++) {
773 if (key
->unit
[unit
].OptA
[i
].Source
!= key
->unit
[unit
].OptRGB
[i
].Source
)
776 switch(key
->unit
[unit
].OptA
[i
].Operand
) {
778 switch(key
->unit
[unit
].OptRGB
[i
].Operand
) {
786 case OPR_ONE_MINUS_SRC_ALPHA
:
787 switch(key
->unit
[unit
].OptRGB
[i
].Operand
) {
788 case OPR_ONE_MINUS_SRC_COLOR
:
789 case OPR_ONE_MINUS_SRC_ALPHA
:
796 return GL_FALSE
; /* impossible */
803 static struct ureg
emit_combine( struct texenv_fragment_program
*p
,
810 const struct mode_opt
*opt
)
812 struct ureg src
[MAX_TERMS
];
813 struct ureg tmp
, half
;
816 assert(nr
<= MAX_TERMS
);
818 tmp
= undef
; /* silence warning (bug 5318) */
820 for (i
= 0; i
< nr
; i
++)
821 src
[i
] = emit_combine_source( p
, mask
, unit
, opt
[i
].Source
, opt
[i
].Operand
);
825 if (mask
== WRITEMASK_XYZW
&& !saturate
)
828 return emit_arith( p
, OPCODE_MOV
, dest
, mask
, saturate
, src
[0], undef
, undef
);
830 return emit_arith( p
, OPCODE_MUL
, dest
, mask
, saturate
,
831 src
[0], src
[1], undef
);
833 return emit_arith( p
, OPCODE_ADD
, dest
, mask
, saturate
,
834 src
[0], src
[1], undef
);
835 case MODE_ADD_SIGNED
:
841 emit_arith( p
, OPCODE_ADD
, tmp
, mask
, 0, src
[0], src
[1], undef
);
842 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp
, half
, undef
);
844 case MODE_INTERPOLATE
:
845 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
847 return emit_arith( p
, OPCODE_LRP
, dest
, mask
, saturate
, src
[2], src
[0], src
[1] );
850 return emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, src
[0], src
[1], undef
);
853 case MODE_DOT3_RGBA_EXT
:
854 case MODE_DOT3_RGB_EXT
:
855 case MODE_DOT3_RGB
: {
856 struct ureg tmp0
= get_temp( p
);
857 struct ureg tmp1
= get_temp( p
);
858 struct ureg neg1
= register_scalar_const(p
, -1);
859 struct ureg two
= register_scalar_const(p
, 2);
864 * dst = tmp0 dot3 tmp1
866 emit_arith( p
, OPCODE_MAD
, tmp0
, WRITEMASK_XYZW
, 0,
869 if (_mesa_memcmp(&src
[0], &src
[1], sizeof(struct ureg
)) == 0)
872 emit_arith( p
, OPCODE_MAD
, tmp1
, WRITEMASK_XYZW
, 0,
874 emit_arith( p
, OPCODE_DP3
, dest
, mask
, saturate
, tmp0
, tmp1
, undef
);
877 case MODE_MODULATE_ADD_ATI
:
878 /* Arg0 * Arg2 + Arg1 */
879 return emit_arith( p
, OPCODE_MAD
, dest
, mask
, saturate
,
880 src
[0], src
[2], src
[1] );
881 case MODE_MODULATE_SIGNED_ADD_ATI
: {
882 /* Arg0 * Arg2 + Arg1 - 0.5 */
883 struct ureg tmp0
= get_temp(p
);
885 emit_arith( p
, OPCODE_MAD
, tmp0
, mask
, 0, src
[0], src
[2], src
[1] );
886 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp0
, half
, undef
);
889 case MODE_MODULATE_SUBTRACT_ATI
:
890 /* Arg0 * Arg2 - Arg1 */
891 emit_arith( p
, OPCODE_MAD
, dest
, mask
, 0, src
[0], src
[2], negate(src
[1]) );
893 case MODE_ADD_PRODUCTS
:
894 /* Arg0 * Arg1 + Arg2 * Arg3 */
896 struct ureg tmp0
= get_temp(p
);
897 emit_arith( p
, OPCODE_MUL
, tmp0
, mask
, 0, src
[0], src
[1], undef
);
898 emit_arith( p
, OPCODE_MAD
, dest
, mask
, saturate
, src
[2], src
[3], tmp0
);
901 case MODE_ADD_PRODUCTS_SIGNED
:
902 /* Arg0 * Arg1 + Arg2 * Arg3 - 0.5 */
904 struct ureg tmp0
= get_temp(p
);
906 emit_arith( p
, OPCODE_MUL
, tmp0
, mask
, 0, src
[0], src
[1], undef
);
907 emit_arith( p
, OPCODE_MAD
, tmp0
, mask
, 0, src
[2], src
[3], tmp0
);
908 emit_arith( p
, OPCODE_SUB
, dest
, mask
, saturate
, tmp0
, half
, undef
);
919 * Generate instructions for one texture unit's env/combiner mode.
922 emit_texenv(struct texenv_fragment_program
*p
, GLuint unit
)
924 struct state_key
*key
= p
->state
;
925 GLboolean saturate
= (unit
< p
->last_tex_stage
);
926 GLuint rgb_shift
, alpha_shift
;
927 struct ureg out
, shift
;
930 if (!key
->unit
[unit
].enabled
) {
931 return get_source(p
, SRC_PREVIOUS
, 0);
934 switch (key
->unit
[unit
].ModeRGB
) {
935 case MODE_DOT3_RGB_EXT
:
936 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
939 case MODE_DOT3_RGBA_EXT
:
944 rgb_shift
= key
->unit
[unit
].ScaleShiftRGB
;
945 alpha_shift
= key
->unit
[unit
].ScaleShiftA
;
949 /* If this is the very last calculation, emit direct to output reg:
951 if (key
->separate_specular
||
952 unit
!= p
->last_tex_stage
||
955 dest
= get_temp( p
);
957 dest
= make_ureg(PROGRAM_OUTPUT
, FRAG_RESULT_COLR
);
959 /* Emit the RGB and A combine ops
961 if (key
->unit
[unit
].ModeRGB
== key
->unit
[unit
].ModeA
&&
962 args_match(key
, unit
)) {
963 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, saturate
,
965 key
->unit
[unit
].NumArgsRGB
,
966 key
->unit
[unit
].ModeRGB
,
967 key
->unit
[unit
].OptRGB
);
969 else if (key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA_EXT
||
970 key
->unit
[unit
].ModeRGB
== MODE_DOT3_RGBA
) {
972 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, saturate
,
974 key
->unit
[unit
].NumArgsRGB
,
975 key
->unit
[unit
].ModeRGB
,
976 key
->unit
[unit
].OptRGB
);
979 /* Need to do something to stop from re-emitting identical
980 * argument calculations here:
982 out
= emit_combine( p
, dest
, WRITEMASK_XYZ
, saturate
,
984 key
->unit
[unit
].NumArgsRGB
,
985 key
->unit
[unit
].ModeRGB
,
986 key
->unit
[unit
].OptRGB
);
987 out
= emit_combine( p
, dest
, WRITEMASK_W
, saturate
,
989 key
->unit
[unit
].NumArgsA
,
990 key
->unit
[unit
].ModeA
,
991 key
->unit
[unit
].OptA
);
994 /* Deal with the final shift:
996 if (alpha_shift
|| rgb_shift
) {
997 if (rgb_shift
== alpha_shift
) {
998 shift
= register_scalar_const(p
, (GLfloat
)(1<<rgb_shift
));
1001 shift
= register_const4f(p
,
1002 (GLfloat
)(1<<rgb_shift
),
1003 (GLfloat
)(1<<rgb_shift
),
1004 (GLfloat
)(1<<rgb_shift
),
1005 (GLfloat
)(1<<alpha_shift
));
1007 return emit_arith( p
, OPCODE_MUL
, dest
, WRITEMASK_XYZW
,
1008 saturate
, out
, shift
, undef
);
1016 * Generate instruction for getting a texture source term.
1018 static void load_texture( struct texenv_fragment_program
*p
, GLuint unit
)
1020 if (is_undef(p
->src_texture
[unit
])) {
1021 GLuint dim
= p
->state
->unit
[unit
].source_index
;
1022 struct ureg texcoord
= register_input(p
, FRAG_ATTRIB_TEX0
+unit
);
1023 struct ureg tmp
= get_tex_temp( p
);
1025 if (dim
== TEXTURE_UNKNOWN_INDEX
)
1026 program_error(p
, "TexSrcBit");
1028 /* TODO: Use D0_MASK_XY where possible.
1030 if (p
->state
->unit
[unit
].enabled
) {
1031 p
->src_texture
[unit
] = emit_texld( p
, OPCODE_TXP
,
1032 tmp
, WRITEMASK_XYZW
,
1033 unit
, dim
, texcoord
);
1034 if (p
->state
->unit
[unit
].shadow
)
1035 p
->program
->Base
.ShadowSamplers
|= 1 << unit
;
1037 p
->program
->Base
.SamplersUsed
|= (1 << unit
);
1038 /* This identity mapping should already be in place
1039 * (see _mesa_init_program_struct()) but let's be safe.
1041 p
->program
->Base
.SamplerUnits
[unit
] = unit
;
1044 p
->src_texture
[unit
] = get_zero(p
);
1048 static GLboolean
load_texenv_source( struct texenv_fragment_program
*p
,
1049 GLuint src
, GLuint unit
)
1053 load_texture(p
, unit
);
1064 load_texture(p
, src
- SRC_TEXTURE0
);
1068 /* not a texture src - do nothing */
1077 * Generate instructions for loading all texture source terms.
1080 load_texunit_sources( struct texenv_fragment_program
*p
, int unit
)
1082 struct state_key
*key
= p
->state
;
1085 for (i
= 0; i
< key
->unit
[unit
].NumArgsRGB
; i
++) {
1086 load_texenv_source( p
, key
->unit
[unit
].OptRGB
[i
].Source
, unit
);
1089 for (i
= 0; i
< key
->unit
[unit
].NumArgsA
; i
++) {
1090 load_texenv_source( p
, key
->unit
[unit
].OptA
[i
].Source
, unit
);
1098 * Generate a new fragment program which implements the context's
1099 * current texture env/combine mode.
1102 create_new_program(GLcontext
*ctx
, struct state_key
*key
,
1103 struct gl_fragment_program
*program
)
1105 struct prog_instruction instBuffer
[MAX_INSTRUCTIONS
];
1106 struct texenv_fragment_program p
;
1108 struct ureg cf
, out
;
1110 _mesa_memset(&p
, 0, sizeof(p
));
1113 p
.program
= program
;
1115 /* During code generation, use locally-allocated instruction buffer,
1116 * then alloc dynamic storage below.
1118 p
.program
->Base
.Instructions
= instBuffer
;
1119 p
.program
->Base
.Target
= GL_FRAGMENT_PROGRAM_ARB
;
1120 p
.program
->Base
.NumTexIndirections
= 1; /* correct? */
1121 p
.program
->Base
.NumTexInstructions
= 0;
1122 p
.program
->Base
.NumAluInstructions
= 0;
1123 p
.program
->Base
.String
= NULL
;
1124 p
.program
->Base
.NumInstructions
=
1125 p
.program
->Base
.NumTemporaries
=
1126 p
.program
->Base
.NumParameters
=
1127 p
.program
->Base
.NumAttributes
= p
.program
->Base
.NumAddressRegs
= 0;
1128 p
.program
->Base
.Parameters
= _mesa_new_parameter_list();
1130 p
.program
->Base
.InputsRead
= 0;
1131 p
.program
->Base
.OutputsWritten
= 1 << FRAG_RESULT_COLR
;
1133 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
1134 p
.src_texture
[unit
] = undef
;
1136 p
.src_previous
= undef
;
1141 p
.last_tex_stage
= 0;
1142 release_temps(ctx
, &p
);
1144 if (key
->enabled_units
) {
1145 /* First pass - to support texture_env_crossbar, first identify
1146 * all referenced texture sources and emit texld instructions
1149 for (unit
= 0 ; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
1150 if (key
->unit
[unit
].enabled
) {
1151 load_texunit_sources( &p
, unit
);
1152 p
.last_tex_stage
= unit
;
1155 /* Second pass - emit combine instructions to build final color:
1157 for (unit
= 0 ; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
1158 if (key
->enabled_units
& (1<<unit
)) {
1159 p
.src_previous
= emit_texenv( &p
, unit
);
1160 release_temps(ctx
, &p
); /* release all temps */
1164 cf
= get_source( &p
, SRC_PREVIOUS
, 0 );
1165 out
= make_ureg( PROGRAM_OUTPUT
, FRAG_RESULT_COLR
);
1167 if (key
->separate_specular
) {
1168 /* Emit specular add.
1170 struct ureg s
= register_input(&p
, FRAG_ATTRIB_COL1
);
1171 emit_arith( &p
, OPCODE_ADD
, out
, WRITEMASK_XYZ
, 0, cf
, s
, undef
);
1172 emit_arith( &p
, OPCODE_MOV
, out
, WRITEMASK_W
, 0, cf
, undef
, undef
);
1174 else if (_mesa_memcmp(&cf
, &out
, sizeof(cf
)) != 0) {
1175 /* Will wind up in here if no texture enabled or a couple of
1176 * other scenarios (GL_REPLACE for instance).
1178 emit_arith( &p
, OPCODE_MOV
, out
, WRITEMASK_XYZW
, 0, cf
, undef
, undef
);
1183 emit_arith( &p
, OPCODE_END
, undef
, WRITEMASK_XYZW
, 0, undef
, undef
, undef
);
1185 if (key
->fog_enabled
) {
1186 /* Pull fog mode from GLcontext, the value in the state key is
1187 * a reduced value and not what is expected in FogOption
1189 p
.program
->FogOption
= ctx
->Fog
.Mode
;
1190 p
.program
->Base
.InputsRead
|= FRAG_BIT_FOGC
; /* XXX new */
1192 p
.program
->FogOption
= GL_NONE
;
1194 if (p
.program
->Base
.NumTexIndirections
> ctx
->Const
.FragmentProgram
.MaxTexIndirections
)
1195 program_error(&p
, "Exceeded max nr indirect texture lookups");
1197 if (p
.program
->Base
.NumTexInstructions
> ctx
->Const
.FragmentProgram
.MaxTexInstructions
)
1198 program_error(&p
, "Exceeded max TEX instructions");
1200 if (p
.program
->Base
.NumAluInstructions
> ctx
->Const
.FragmentProgram
.MaxAluInstructions
)
1201 program_error(&p
, "Exceeded max ALU instructions");
1203 ASSERT(p
.program
->Base
.NumInstructions
<= MAX_INSTRUCTIONS
);
1205 /* Allocate final instruction array */
1206 p
.program
->Base
.Instructions
1207 = _mesa_alloc_instructions(p
.program
->Base
.NumInstructions
);
1208 if (!p
.program
->Base
.Instructions
) {
1209 _mesa_error(ctx
, GL_OUT_OF_MEMORY
,
1210 "generating tex env program");
1213 _mesa_copy_instructions(p
.program
->Base
.Instructions
, instBuffer
,
1214 p
.program
->Base
.NumInstructions
);
1216 if (p
.program
->FogOption
) {
1217 _mesa_append_fog_code(ctx
, p
.program
);
1218 p
.program
->FogOption
= GL_NONE
;
1222 /* Notify driver the fragment program has (actually) changed.
1224 if (ctx
->Driver
.ProgramStringNotify
) {
1225 ctx
->Driver
.ProgramStringNotify( ctx
, GL_FRAGMENT_PROGRAM_ARB
,
1230 _mesa_print_program(&p
.program
->Base
);
1237 * Return a fragment program which implements the current
1238 * fixed-function texture, fog and color-sum operations.
1240 struct gl_fragment_program
*
1241 _mesa_get_fixed_func_fragment_program(GLcontext
*ctx
)
1243 struct gl_fragment_program
*prog
;
1244 struct state_key key
;
1246 make_state_key(ctx
, &key
);
1248 prog
= (struct gl_fragment_program
*)
1249 _mesa_search_program_cache(ctx
->FragmentProgram
.Cache
,
1253 prog
= (struct gl_fragment_program
*)
1254 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
1256 create_new_program(ctx
, &key
, prog
);
1258 _mesa_program_cache_insert(ctx
, ctx
->FragmentProgram
.Cache
,
1259 &key
, sizeof(key
), &prog
->Base
);