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 **************************************************************************/
33 #include "texenvprogram.h"
35 #include "shader/program.h"
36 #include "shader/nvfragprog.h"
37 #include "shader/arbfragparse.h"
40 #define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
42 /* Use uregs to represent registers internally, translate to Mesa's
43 * expected formats on emit.
45 * NOTE: These are passed by value extensively in this file rather
46 * than as usual by pointer reference. If this disturbs you, try
47 * remembering they are just 32bits in size.
49 * GCC is smart enough to deal with these dword-sized structures in
50 * much the same way as if I had defined them as dwords and was using
51 * macros to access and set the fields. This is much nicer and easier
64 const static struct ureg undef
= {
79 /* State used to build the fragment program:
81 struct texenv_fragment_program
{
82 struct fragment_program
*program
;
85 GLuint temp_used_for_txp
; /* Temps which have been the result of a texture
89 GLuint temp_in_use
; /* Tracks temporary regs which are in
96 struct ureg src_texture
; /* Reg containing sampled texture color,
100 struct ureg src_previous
; /* Reg containing color from previous
101 * stage. May need to be decl'd.
104 GLuint last_tex_stage
; /* Number of last enabled texture unit */
109 static struct ureg
make_ureg(GLuint file
, GLuint idx
)
117 reg
.swz
= SWIZZLE_NOOP
;
122 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
124 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
127 GET_SWZ(reg
.swz
, w
));
132 static struct ureg
swizzle1( struct ureg reg
, int x
)
134 return swizzle(reg
, x
, x
, x
, x
);
137 static GLboolean
is_undef( struct ureg reg
)
139 return reg
.file
== 0xf;
143 static struct ureg
get_temp( struct texenv_fragment_program
*p
)
147 /* First try and reuse temps which have been used for texture
150 bit
= ffs( ~p
->temp_in_use
& p
->temp_used_for_txp
);
152 /* Then any unused temporary:
155 bit
= ffs( ~p
->temp_in_use
);
158 fprintf(stderr
, "%s: out of temporaries\n", __FILE__
);
162 p
->temp_in_use
|= 1<<(bit
-1);
163 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
166 static struct ureg
get_tex_temp( struct texenv_fragment_program
*p
)
170 /* First try to find availble temp not previously used as a texture
173 bit
= ffs( ~p
->temp_in_use
& ~p
->temp_used_for_txp
);
175 /* Then any unused temporary:
178 bit
= ffs( ~p
->temp_in_use
);
179 p
->program
->NumTexIndirections
++;
183 fprintf(stderr
, "%s: out of temporaries\n", __FILE__
);
187 p
->temp_in_use
|= 1<<(bit
-1);
188 p
->temp_used_for_txp
|= 1<<(bit
-1);
189 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
193 static void release_temps( struct texenv_fragment_program
*p
)
195 GLuint max_temp
= p
->ctx
->Const
.MaxFragmentProgramTemps
;
197 if (max_temp
>= sizeof(int) * 8)
200 p
->temp_in_use
= ~((1<<max_temp
)-1);
204 static struct ureg
register_param6( struct texenv_fragment_program
*p
,
220 idx
= _mesa_add_state_reference( p
->program
->Parameters
, tokens
);
221 return make_ureg(PROGRAM_STATE_VAR
, idx
);
225 #define register_param1(p,s0) register_param6(p,s0,0,0,0,0,0)
226 #define register_param2(p,s0,s1) register_param6(p,s0,s1,0,0,0,0)
227 #define register_param3(p,s0,s1,s2) register_param6(p,s0,s1,s2,0,0,0)
228 #define register_param4(p,s0,s1,s2,s3) register_param6(p,s0,s1,s2,s3,0,0)
231 static struct ureg
register_input( struct texenv_fragment_program
*p
, GLuint input
)
233 p
->program
->InputsRead
|= (1<<input
);
234 return make_ureg(PROGRAM_INPUT
, input
);
238 static void emit_arg( struct fp_src_register
*reg
,
241 reg
->File
= ureg
.file
;
242 reg
->Index
= ureg
.idx
;
243 reg
->Swizzle
= ureg
.swz
;
244 reg
->NegateBase
= ureg
.negatebase
;
246 reg
->NegateAbs
= ureg
.negateabs
;
249 static void emit_dst( struct fp_dst_register
*dst
,
250 struct ureg ureg
, GLuint mask
)
252 dst
->File
= ureg
.file
;
253 dst
->Index
= ureg
.idx
;
254 dst
->WriteMask
= mask
;
256 dst
->CondSwizzle
= 0;
259 static struct fp_instruction
*
260 emit_op(struct texenv_fragment_program
*p
,
269 GLuint nr
= p
->program
->Base
.NumInstructions
++;
270 struct fp_instruction
*inst
= &p
->program
->Instructions
[nr
];
272 memset(inst
, 0, sizeof(*inst
));
275 emit_arg( &inst
->SrcReg
[0], src0
);
276 emit_arg( &inst
->SrcReg
[1], src1
);
277 emit_arg( &inst
->SrcReg
[2], src2
);
279 inst
->Saturate
= saturate
;
281 emit_dst( &inst
->DstReg
, dest
, mask
);
287 static struct ureg
emit_arith( struct texenv_fragment_program
*p
,
296 emit_op(p
, op
, dest
, mask
, saturate
, src0
, src1
, src2
);
298 p
->program
->NumAluInstructions
++;
302 static struct ureg
emit_texld( struct texenv_fragment_program
*p
,
310 struct fp_instruction
*inst
= emit_op( p
, op
,
312 0, /* don't saturate? */
317 inst
->TexSrcIdx
= tex_idx
;
318 inst
->TexSrcUnit
= tex_unit
;
320 p
->program
->NumTexInstructions
++;
322 if (coord
.file
!= PROGRAM_INPUT
&&
323 (coord
.idx
< FRAG_ATTRIB_TEX0
||
324 coord
.idx
> FRAG_ATTRIB_TEX7
)) {
325 p
->program
->NumTexIndirections
++;
332 static struct ureg
register_const4f( struct texenv_fragment_program
*p
,
344 idx
= _mesa_add_unnamed_constant( p
->program
->Parameters
, values
);
345 return make_ureg(PROGRAM_STATE_VAR
, idx
);
348 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
349 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
350 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
351 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
360 static void program_error( struct texenv_fragment_program
*p
, const char *msg
)
362 fprintf(stderr
, "%s\n", msg
);
367 static GLuint
translate_tex_src_bit( struct texenv_fragment_program
*p
,
371 case TEXTURE_1D_BIT
: return TEXTURE_1D_INDEX
;
372 case TEXTURE_2D_BIT
: return TEXTURE_2D_INDEX
;
373 case TEXTURE_RECT_BIT
: return TEXTURE_RECT_INDEX
;
374 case TEXTURE_3D_BIT
: return TEXTURE_3D_INDEX
;
375 case TEXTURE_CUBE_BIT
: return TEXTURE_CUBE_INDEX
;
376 default: program_error(p
, "TexSrcBit"); return 0;
381 static struct ureg
get_source( struct texenv_fragment_program
*p
,
382 GLenum src
, GLuint unit
)
386 if (is_undef(p
->src_texture
)) {
388 GLuint dim
= translate_tex_src_bit( p
, p
->ctx
->Texture
.Unit
[unit
]._ReallyEnabled
);
389 struct ureg texcoord
= register_input(p
, FRAG_ATTRIB_TEX0
+unit
);
390 struct ureg tmp
= get_tex_temp( p
);
392 /* TODO: Use D0_MASK_XY where possible.
394 p
->src_texture
= emit_texld( p
, FP_OPCODE_TXP
,
396 unit
, dim
, texcoord
);
399 return p
->src_texture
;
414 return register_param2(p
, STATE_TEXENV_COLOR
, unit
);
415 case GL_PRIMARY_COLOR
:
416 return register_input(p
, FRAG_ATTRIB_COL0
);
419 if (is_undef(p
->src_previous
))
420 return register_input(p
, FRAG_ATTRIB_COL0
);
422 return p
->src_previous
;
427 static struct ureg
emit_combine_source( struct texenv_fragment_program
*p
,
433 struct ureg arg
, src
, one
;
435 src
= get_source(p
, source
, unit
);
438 case GL_ONE_MINUS_SRC_COLOR
:
440 * Emit tmp = 1.0 - arg.xyzw
443 one
= register_scalar_const(p
, 1.0);
444 return emit_arith( p
, FP_OPCODE_SUB
, arg
, mask
, 0, one
, src
, undef
);
447 if (mask
== WRITEMASK_W
)
450 return swizzle1( src
, W
);
451 case GL_ONE_MINUS_SRC_ALPHA
:
453 * Emit tmp = 1.0 - arg.wwww
456 one
= register_scalar_const(p
, 1.0);
457 return emit_arith( p
, FP_OPCODE_SUB
, arg
, mask
, 0,
458 one
, swizzle1(src
, W
), undef
);
467 static int nr_args( GLenum mode
)
470 case GL_REPLACE
: return 1;
471 case GL_MODULATE
: return 2;
472 case GL_ADD
: return 2;
473 case GL_ADD_SIGNED
: return 2;
474 case GL_INTERPOLATE
: return 3;
475 case GL_SUBTRACT
: return 2;
476 case GL_DOT3_RGB_EXT
: return 2;
477 case GL_DOT3_RGBA_EXT
: return 2;
478 case GL_DOT3_RGB
: return 2;
479 case GL_DOT3_RGBA
: return 2;
485 static GLboolean
args_match( struct gl_texture_unit
*texUnit
)
487 int i
, nr
= nr_args(texUnit
->_CurrentCombine
->ModeRGB
);
489 for (i
= 0 ; i
< nr
; i
++) {
490 if (texUnit
->_CurrentCombine
->SourceA
[i
] != texUnit
->_CurrentCombine
->SourceRGB
[i
])
493 switch(texUnit
->_CurrentCombine
->OperandA
[i
]) {
495 switch(texUnit
->_CurrentCombine
->OperandRGB
[i
]) {
503 case GL_ONE_MINUS_SRC_ALPHA
:
504 switch(texUnit
->_CurrentCombine
->OperandRGB
[i
]) {
505 case GL_ONE_MINUS_SRC_COLOR
:
506 case GL_ONE_MINUS_SRC_ALPHA
:
513 return GL_FALSE
; /* impossible */
521 static struct ureg
emit_combine( struct texenv_fragment_program
*p
,
527 const GLenum
*source
,
528 const GLenum
*operand
)
530 int nr
= nr_args(mode
);
535 for (i
= 0; i
< nr
; i
++)
536 src
[i
] = emit_combine_source( p
, mask
, unit
, source
[i
], operand
[i
] );
540 if (mask
== WRITEMASK_XYZW
&& !saturate
)
543 return emit_arith( p
, FP_OPCODE_MOV
, dest
, mask
, saturate
, src
[0], undef
, undef
);
545 return emit_arith( p
, FP_OPCODE_MUL
, dest
, mask
, saturate
,
546 src
[0], src
[1], undef
);
548 return emit_arith( p
, FP_OPCODE_ADD
, dest
, mask
, saturate
,
549 src
[0], src
[1], undef
);
554 tmp
= register_scalar_const(p
, .5);
555 emit_arith( p
, FP_OPCODE_ADD
, dest
, mask
, 0, src
[0], src
[1], undef
);
556 emit_arith( p
, FP_OPCODE_SUB
, dest
, mask
, saturate
, dest
, tmp
, undef
);
559 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
561 return emit_arith( p
, FP_OPCODE_LRP
, dest
, mask
, saturate
, src
[2], src
[0], src
[1] );
564 return emit_arith( p
, FP_OPCODE_SUB
, dest
, mask
, saturate
, src
[0], src
[1], undef
);
567 case GL_DOT3_RGBA_EXT
:
568 case GL_DOT3_RGB_EXT
:
570 struct ureg tmp0
= get_temp( p
);
571 struct ureg tmp1
= get_temp( p
);
572 struct ureg neg1
= register_scalar_const(p
, -1);
573 struct ureg two
= register_scalar_const(p
, 2);
578 * dst = tmp0 dot3 tmp1
580 emit_arith( p
, FP_OPCODE_MAD
, tmp0
, WRITEMASK_XYZW
, 0,
583 if (memcmp(&src
[0], &src
[1], sizeof(struct ureg
)) == 0)
586 emit_arith( p
, FP_OPCODE_MAD
, tmp1
, WRITEMASK_XYZW
, 0,
588 emit_arith( p
, FP_OPCODE_DP3
, dest
, mask
, saturate
, tmp0
, tmp1
, undef
);
597 static struct ureg
get_dest( struct texenv_fragment_program
*p
, int unit
)
599 if (p
->ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
)
600 return get_temp( p
);
601 else if (unit
!= p
->last_tex_stage
)
602 return get_temp( p
);
604 return make_ureg(PROGRAM_OUTPUT
, FRAG_OUTPUT_COLR
);
609 static struct ureg
emit_texenv( struct texenv_fragment_program
*p
, int unit
)
611 struct gl_texture_unit
*texUnit
= &p
->ctx
->Texture
.Unit
[unit
];
612 GLuint saturate
= (unit
< p
->last_tex_stage
);
613 GLuint rgb_shift
, alpha_shift
;
614 struct ureg out
, shift
;
615 struct ureg dest
= get_dest(p
, unit
);
617 if (!texUnit
->_ReallyEnabled
) {
618 return get_source(p
, GL_PREVIOUS
, 0);
621 switch (texUnit
->_CurrentCombine
->ModeRGB
) {
622 case GL_DOT3_RGB_EXT
:
623 alpha_shift
= texUnit
->_CurrentCombine
->ScaleShiftA
;
627 case GL_DOT3_RGBA_EXT
:
633 rgb_shift
= texUnit
->_CurrentCombine
->ScaleShiftRGB
;
634 alpha_shift
= texUnit
->_CurrentCombine
->ScaleShiftA
;
639 /* Emit the RGB and A combine ops
641 if (texUnit
->_CurrentCombine
->ModeRGB
== texUnit
->_CurrentCombine
->ModeA
&&
642 args_match( texUnit
)) {
643 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, saturate
,
645 texUnit
->_CurrentCombine
->ModeRGB
,
646 texUnit
->_CurrentCombine
->SourceRGB
,
647 texUnit
->_CurrentCombine
->OperandRGB
);
649 else if (texUnit
->_CurrentCombine
->ModeRGB
== GL_DOT3_RGBA_EXT
||
650 texUnit
->_CurrentCombine
->ModeRGB
== GL_DOT3_RGBA
) {
652 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, saturate
,
654 texUnit
->_CurrentCombine
->ModeRGB
,
655 texUnit
->_CurrentCombine
->SourceRGB
,
656 texUnit
->_CurrentCombine
->OperandRGB
);
659 /* Need to do something to stop from re-emitting identical
660 * argument calculations here:
662 out
= emit_combine( p
, dest
, WRITEMASK_XYZ
, saturate
,
664 texUnit
->_CurrentCombine
->ModeRGB
,
665 texUnit
->_CurrentCombine
->SourceRGB
,
666 texUnit
->_CurrentCombine
->OperandRGB
);
667 out
= emit_combine( p
, dest
, WRITEMASK_W
, saturate
,
669 texUnit
->_CurrentCombine
->ModeA
,
670 texUnit
->_CurrentCombine
->SourceA
,
671 texUnit
->_CurrentCombine
->OperandA
);
674 /* Deal with the final shift:
676 if (alpha_shift
|| rgb_shift
) {
677 if (rgb_shift
== alpha_shift
) {
678 shift
= register_scalar_const(p
, 1<<rgb_shift
);
681 shift
= register_const2f(p
, 1<<rgb_shift
, 1<<alpha_shift
);
682 shift
= swizzle(shift
,X
,X
,X
,Y
);
684 return emit_arith( p
, FP_OPCODE_MUL
, dest
, WRITEMASK_XYZW
,
685 saturate
, out
, shift
, undef
);
691 void _mesa_UpdateTexEnvProgram( GLcontext
*ctx
)
693 struct texenv_fragment_program p
;
696 GLuint db_NumInstructions
= 0;
697 struct fp_instruction
*db_Instructions
= NULL
;
699 if (ctx
->FragmentProgram
._Enabled
)
702 if (!ctx
->_TexEnvProgram
)
703 ctx
->FragmentProgram
._Current
= ctx
->_TexEnvProgram
=
704 (struct fragment_program
*)
705 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
707 _mesa_memset(&p
, 0, sizeof(p
));
709 p
.program
= ctx
->_TexEnvProgram
;
711 if (ctx
->Driver
.ProgramStringNotify
|| DISASSEM
) {
712 db_Instructions
= p
.program
->Instructions
;
713 db_NumInstructions
= p
.program
->Base
.NumInstructions
;
714 p
.program
->Instructions
= NULL
;
717 if (!p
.program
->Instructions
)
718 p
.program
->Instructions
= MALLOC(sizeof(struct fp_instruction
) * 100);
720 p
.program
->Base
.NumInstructions
= 0;
721 p
.program
->Base
.Target
= GL_FRAGMENT_PROGRAM_ARB
;
722 p
.program
->NumTexIndirections
= 1; /* correct? */
723 p
.program
->NumTexInstructions
= 0;
724 p
.program
->NumAluInstructions
= 0;
725 p
.program
->Base
.String
= 0;
726 p
.program
->Base
.NumInstructions
=
727 p
.program
->Base
.NumTemporaries
=
728 p
.program
->Base
.NumParameters
=
729 p
.program
->Base
.NumAttributes
= p
.program
->Base
.NumAddressRegs
= 0;
731 if (p
.program
->Parameters
)
732 _mesa_free_parameters(p
.program
->Parameters
);
734 p
.program
->Parameters
= _mesa_new_parameter_list();
736 p
.program
->InputsRead
= 0;
737 p
.program
->OutputsWritten
= 0;
739 p
.src_texture
= undef
;
740 p
.src_previous
= undef
;
741 p
.last_tex_stage
= 0;
744 if (ctx
->Texture
._EnabledUnits
) {
745 for (unit
= 0 ; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
746 if (ctx
->Texture
.Unit
[unit
]._ReallyEnabled
) {
747 p
.last_tex_stage
= unit
;
750 for (unit
= 0 ; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
751 if (ctx
->Texture
.Unit
[unit
]._ReallyEnabled
) {
752 p
.src_previous
= emit_texenv( &p
, unit
);
753 p
.src_texture
= undef
;
754 release_temps(&p
); /* release all temps */
755 if (p
.src_previous
.file
== PROGRAM_TEMPORARY
)
756 p
.temp_in_use
|= 1 << p
.src_previous
.idx
; /* except for this one */
760 cf
= get_source( &p
, GL_PREVIOUS
, 0 );
761 out
= make_ureg( PROGRAM_OUTPUT
, FRAG_OUTPUT_COLR
);
763 if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
) {
764 /* Emit specular add.
766 struct ureg s
= register_input(&p
, FRAG_ATTRIB_COL1
);
767 emit_arith( &p
, FP_OPCODE_ADD
, out
, WRITEMASK_XYZ
, 0, cf
, s
, undef
);
769 else if (memcmp(&cf
, &out
, sizeof(cf
)) != 0) {
770 /* Will wind up in here if no texture enabled or a couple of
771 * other scenarios (GL_REPLACE for instance).
773 emit_arith( &p
, FP_OPCODE_MOV
, out
, WRITEMASK_XYZW
, 0, cf
, undef
, undef
);
778 emit_arith( &p
, FP_OPCODE_END
, undef
, WRITEMASK_XYZW
, 0, undef
, undef
, undef
);
780 if (ctx
->Fog
.Enabled
)
781 p
.program
->FogOption
= ctx
->Fog
.Mode
;
783 p
.program
->FogOption
= GL_NONE
;
785 if (p
.program
->NumTexIndirections
> ctx
->Const
.MaxFragmentProgramTexIndirections
)
786 program_error(&p
, "Exceeded max nr indirect texture lookups");
788 if (p
.program
->NumTexInstructions
> ctx
->Const
.MaxFragmentProgramTexInstructions
)
789 program_error(&p
, "Exceeded max TEX instructions");
791 if (p
.program
->NumAluInstructions
> ctx
->Const
.MaxFragmentProgramAluInstructions
)
792 program_error(&p
, "Exceeded max ALU instructions");
795 /* Notify driver the fragment program has (actually) changed.
797 if (ctx
->Driver
.ProgramStringNotify
|| DISASSEM
) {
798 if (db_Instructions
== NULL
||
799 db_NumInstructions
!= p
.program
->Base
.NumInstructions
||
800 memcmp(db_Instructions
, p
.program
->Instructions
,
801 db_NumInstructions
* sizeof(*db_Instructions
)) != 0) {
803 if (ctx
->Driver
.ProgramStringNotify
)
804 ctx
->Driver
.ProgramStringNotify( ctx
, GL_FRAGMENT_PROGRAM_ARB
,
808 _mesa_debug_fp_inst(p
.program
->NumTexInstructions
+ p
.program
->NumAluInstructions
,
809 p
.program
->Instructions
);
814 FREE(db_Instructions
);