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"
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 tex_temp_flag
; /* Temps which have been the result of a texture
89 GLuint temp_flag
; /* 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 texture results:
149 bit
= ffs( ~(p
->temp_flag
& p
->tex_temp_flag
) );
151 /* Then any unused temporary:
154 bit
= ffs( ~p
->temp_flag
);
157 fprintf(stderr
, "%s: out of temporaries\n", __FILE__
);
161 p
->temp_flag
|= 1<<(bit
-1);
162 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
165 static struct ureg
get_tex_temp( struct texenv_fragment_program
*p
)
169 /* First try to find temp not previously used as a texture result:
171 bit
= ffs( ~(p
->temp_flag
& ~p
->tex_temp_flag
) );
173 /* Then any unused temporary:
176 bit
= ffs( ~p
->temp_flag
);
177 p
->program
->NumTexIndirections
++;
181 fprintf(stderr
, "%s: out of temporaries\n", __FILE__
);
185 p
->temp_flag
|= 1<<(bit
-1);
186 p
->tex_temp_flag
|= 1<<(bit
-1);
187 return make_ureg(PROGRAM_TEMPORARY
, (bit
-1));
191 static void release_temps( struct texenv_fragment_program
*p
)
197 static struct ureg
register_param6( struct texenv_fragment_program
*p
,
213 idx
= _mesa_add_state_reference( p
->program
->Parameters
, tokens
);
214 return make_ureg(PROGRAM_STATE_VAR
, idx
);
218 #define register_param1(p,s0) register_param6(p,s0,0,0,0,0,0)
219 #define register_param2(p,s0,s1) register_param6(p,s0,s1,0,0,0,0)
220 #define register_param3(p,s0,s1,s2) register_param6(p,s0,s1,s2,0,0,0)
221 #define register_param4(p,s0,s1,s2,s3) register_param6(p,s0,s1,s2,s3,0,0)
224 static struct ureg
register_input( struct texenv_fragment_program
*p
, GLuint input
)
226 p
->program
->InputsRead
|= (1<<input
);
227 return make_ureg(PROGRAM_INPUT
, input
);
231 static void emit_arg( struct fp_src_register
*reg
,
234 reg
->File
= ureg
.file
;
235 reg
->Index
= ureg
.idx
;
236 reg
->Swizzle
= ureg
.swz
;
237 reg
->NegateBase
= ureg
.negatebase
;
239 reg
->NegateAbs
= ureg
.negateabs
;
242 static void emit_dst( struct fp_dst_register
*dst
,
243 struct ureg ureg
, GLuint mask
)
245 dst
->File
= ureg
.file
;
246 dst
->Index
= ureg
.idx
;
247 dst
->WriteMask
= mask
;
249 dst
->CondSwizzle
= 0;
252 static struct fp_instruction
*
253 emit_op(struct texenv_fragment_program
*p
,
262 GLuint nr
= p
->program
->Base
.NumInstructions
++;
263 struct fp_instruction
*inst
= &p
->program
->Instructions
[nr
];
265 memset(inst
, 0, sizeof(*inst
));
268 emit_arg( &inst
->SrcReg
[0], src0
);
269 emit_arg( &inst
->SrcReg
[1], src1
);
270 emit_arg( &inst
->SrcReg
[2], src2
);
272 inst
->Saturate
= saturate
;
274 emit_dst( &inst
->DstReg
, dest
, mask
);
280 static struct ureg
emit_arith( struct texenv_fragment_program
*p
,
289 emit_op(p
, op
, dest
, mask
, saturate
, src0
, src1
, src2
);
291 p
->program
->NumAluInstructions
++;
295 static struct ureg
emit_texld( struct texenv_fragment_program
*p
,
303 struct fp_instruction
*inst
= emit_op( p
, op
,
305 0, /* don't saturate? */
310 inst
->TexSrcIdx
= tex_idx
;
311 inst
->TexSrcUnit
= tex_unit
;
313 p
->program
->NumTexInstructions
++;
315 if (coord
.file
!= PROGRAM_INPUT
&&
316 (coord
.idx
< FRAG_ATTRIB_TEX0
||
317 coord
.idx
> FRAG_ATTRIB_TEX7
)) {
318 p
->program
->NumTexIndirections
++;
325 static struct ureg
register_const4f( struct texenv_fragment_program
*p
,
337 idx
= _mesa_add_unnamed_constant( p
->program
->Parameters
, values
);
338 return make_ureg(PROGRAM_STATE_VAR
, idx
);
341 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
342 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
343 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
352 static void program_error( struct texenv_fragment_program
*p
, const char *msg
)
354 fprintf(stderr
, "%s\n", msg
);
359 static GLuint
translate_tex_src_bit( struct texenv_fragment_program
*p
,
363 case TEXTURE_1D_BIT
: return TEXTURE_1D_INDEX
;
364 case TEXTURE_2D_BIT
: return TEXTURE_2D_INDEX
;
365 case TEXTURE_RECT_BIT
: return TEXTURE_RECT_INDEX
;
366 case TEXTURE_3D_BIT
: return TEXTURE_3D_INDEX
;
367 case TEXTURE_CUBE_BIT
: return TEXTURE_CUBE_INDEX
;
368 default: program_error(p
, "TexSrcBit"); return 0;
373 static struct ureg
get_source( struct texenv_fragment_program
*p
,
374 GLenum src
, GLuint unit
)
378 if (is_undef(p
->src_texture
)) {
380 GLuint dim
= translate_tex_src_bit( p
, p
->ctx
->Texture
.Unit
[unit
]._ReallyEnabled
);
381 struct ureg texcoord
= register_input(p
, FRAG_ATTRIB_TEX0
+unit
);
382 struct ureg tmp
= get_tex_temp( p
);
384 /* TODO: Use D0_MASK_XY where possible.
386 p
->src_texture
= emit_texld( p
, FP_OPCODE_TXP
,
388 unit
, dim
, texcoord
);
391 return p
->src_texture
;
406 return register_param2(p
, STATE_TEXENV_COLOR
, unit
);
407 case GL_PRIMARY_COLOR
:
408 return register_input(p
, FRAG_ATTRIB_COL0
);
411 if (is_undef(p
->src_previous
))
412 return register_input(p
, FRAG_ATTRIB_COL0
);
414 return p
->src_previous
;
419 static struct ureg
emit_combine_source( struct texenv_fragment_program
*p
,
425 struct ureg arg
, src
, one
;
427 src
= get_source(p
, source
, unit
);
430 case GL_ONE_MINUS_SRC_COLOR
:
432 * Emit tmp = 1.0 - arg.xyzw
435 one
= register_const1f(p
, 1.0);
436 return emit_arith( p
, FP_OPCODE_SUB
, arg
, mask
, 0, one
, src
, undef
);
439 if (mask
== WRITEMASK_W
)
442 return swizzle1( src
, W
);
443 case GL_ONE_MINUS_SRC_ALPHA
:
445 * Emit tmp = 1.0 - arg.wwww
448 one
= register_const1f(p
, 1.0);
449 return emit_arith( p
, FP_OPCODE_SUB
, arg
, mask
, 0,
450 one
, swizzle1(src
, W
), undef
);
459 static int nr_args( GLenum mode
)
462 case GL_REPLACE
: return 1;
463 case GL_MODULATE
: return 2;
464 case GL_ADD
: return 2;
465 case GL_ADD_SIGNED
: return 2;
466 case GL_INTERPOLATE
: return 3;
467 case GL_SUBTRACT
: return 2;
468 case GL_DOT3_RGB_EXT
: return 2;
469 case GL_DOT3_RGBA_EXT
: return 2;
470 case GL_DOT3_RGB
: return 2;
471 case GL_DOT3_RGBA
: return 2;
477 static GLboolean
args_match( struct gl_texture_unit
*texUnit
)
479 int i
, nr
= nr_args(texUnit
->_CurrentCombine
->ModeRGB
);
481 for (i
= 0 ; i
< nr
; i
++) {
482 if (texUnit
->_CurrentCombine
->SourceA
[i
] != texUnit
->_CurrentCombine
->SourceRGB
[i
])
485 switch(texUnit
->_CurrentCombine
->OperandA
[i
]) {
487 switch(texUnit
->_CurrentCombine
->OperandRGB
[i
]) {
495 case GL_ONE_MINUS_SRC_ALPHA
:
496 switch(texUnit
->_CurrentCombine
->OperandRGB
[i
]) {
497 case GL_ONE_MINUS_SRC_COLOR
:
498 case GL_ONE_MINUS_SRC_ALPHA
:
505 return GL_FALSE
; /* impossible */
513 static struct ureg
emit_combine( struct texenv_fragment_program
*p
,
519 const GLenum
*source
,
520 const GLenum
*operand
)
522 int nr
= nr_args(mode
);
527 for (i
= 0; i
< nr
; i
++)
528 src
[i
] = emit_combine_source( p
, mask
, unit
, source
[i
], operand
[i
] );
532 if (mask
== WRITEMASK_XYZW
&& !saturate
)
535 return emit_arith( p
, FP_OPCODE_MOV
, dest
, mask
, saturate
, src
[0], undef
, undef
);
537 return emit_arith( p
, FP_OPCODE_MUL
, dest
, mask
, saturate
,
538 src
[0], src
[1], undef
);
540 return emit_arith( p
, FP_OPCODE_ADD
, dest
, mask
, saturate
,
541 src
[0], src
[1], undef
);
546 tmp
= register_const1f(p
, .5);
547 tmp
= swizzle1(tmp
,X
);
548 emit_arith( p
, FP_OPCODE_ADD
, dest
, mask
, 0, src
[0], src
[1], undef
);
549 emit_arith( p
, FP_OPCODE_SUB
, dest
, mask
, saturate
, dest
, tmp
, undef
);
552 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
554 return emit_arith( p
, FP_OPCODE_LRP
, dest
, mask
, saturate
, src
[2], src
[0], src
[1] );
557 return emit_arith( p
, FP_OPCODE_SUB
, dest
, mask
, saturate
, src
[0], src
[1], undef
);
560 case GL_DOT3_RGBA_EXT
:
561 case GL_DOT3_RGB_EXT
:
563 struct ureg tmp0
= get_temp( p
);
564 struct ureg tmp1
= get_temp( p
);
565 struct ureg neg1
= register_const1f(p
, -1);
566 struct ureg two
= register_const1f(p
, 2);
571 * dst = tmp0 dot3 tmp1
573 emit_arith( p
, FP_OPCODE_MAD
, tmp0
, WRITEMASK_XYZW
, 0,
576 if (memcmp(&src
[0], &src
[1], sizeof(struct ureg
)) == 0)
579 emit_arith( p
, FP_OPCODE_MAD
, tmp1
, WRITEMASK_XYZW
, 0,
581 emit_arith( p
, FP_OPCODE_DP3
, dest
, mask
, saturate
, tmp0
, tmp1
, undef
);
590 static struct ureg
get_dest( struct texenv_fragment_program
*p
, int unit
)
592 if (p
->ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
)
593 return get_temp( p
);
594 else if (unit
!= p
->last_tex_stage
)
595 return get_temp( p
);
597 return make_ureg(PROGRAM_OUTPUT
, FRAG_OUTPUT_COLR
);
602 static struct ureg
emit_texenv( struct texenv_fragment_program
*p
, int unit
)
604 struct gl_texture_unit
*texUnit
= &p
->ctx
->Texture
.Unit
[unit
];
605 GLuint saturate
= (unit
< p
->last_tex_stage
);
606 GLuint rgb_shift
, alpha_shift
;
607 struct ureg out
, shift
;
608 struct ureg dest
= get_dest(p
, unit
);
610 if (!texUnit
->_ReallyEnabled
) {
611 return get_source(p
, GL_PREVIOUS
, 0);
614 switch (texUnit
->_CurrentCombine
->ModeRGB
) {
615 case GL_DOT3_RGB_EXT
:
616 alpha_shift
= texUnit
->_CurrentCombine
->ScaleShiftA
;
620 case GL_DOT3_RGBA_EXT
:
626 rgb_shift
= texUnit
->_CurrentCombine
->ScaleShiftRGB
;
627 alpha_shift
= texUnit
->_CurrentCombine
->ScaleShiftA
;
632 /* Emit the RGB and A combine ops
634 if (texUnit
->_CurrentCombine
->ModeRGB
== texUnit
->_CurrentCombine
->ModeA
&&
635 args_match( texUnit
)) {
636 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, saturate
,
638 texUnit
->_CurrentCombine
->ModeRGB
,
639 texUnit
->_CurrentCombine
->SourceRGB
,
640 texUnit
->_CurrentCombine
->OperandRGB
);
642 else if (texUnit
->_CurrentCombine
->ModeRGB
== GL_DOT3_RGBA_EXT
||
643 texUnit
->_CurrentCombine
->ModeRGB
== GL_DOT3_RGBA
) {
645 out
= emit_combine( p
, dest
, WRITEMASK_XYZW
, saturate
,
647 texUnit
->_CurrentCombine
->ModeRGB
,
648 texUnit
->_CurrentCombine
->SourceRGB
,
649 texUnit
->_CurrentCombine
->OperandRGB
);
652 /* Need to do something to stop from re-emitting identical
653 * argument calculations here:
655 out
= emit_combine( p
, dest
, WRITEMASK_XYZ
, saturate
,
657 texUnit
->_CurrentCombine
->ModeRGB
,
658 texUnit
->_CurrentCombine
->SourceRGB
,
659 texUnit
->_CurrentCombine
->OperandRGB
);
660 out
= emit_combine( p
, dest
, WRITEMASK_W
, saturate
,
662 texUnit
->_CurrentCombine
->ModeA
,
663 texUnit
->_CurrentCombine
->SourceA
,
664 texUnit
->_CurrentCombine
->OperandA
);
667 /* Deal with the final shift:
669 if (alpha_shift
|| rgb_shift
) {
670 if (rgb_shift
== alpha_shift
) {
671 shift
= register_const1f(p
, 1<<rgb_shift
);
672 shift
= swizzle1(shift
,X
);
675 shift
= register_const2f(p
, 1<<rgb_shift
, 1<<alpha_shift
);
676 shift
= swizzle(shift
,X
,X
,X
,Y
);
678 return emit_arith( p
, FP_OPCODE_MUL
, dest
, WRITEMASK_XYZW
,
679 saturate
, out
, shift
, undef
);
685 void _mesa_UpdateTexEnvProgram( GLcontext
*ctx
)
687 struct texenv_fragment_program p
;
691 if (ctx
->FragmentProgram
._Enabled
)
694 if (!ctx
->_TexEnvProgram
)
695 ctx
->FragmentProgram
._Current
= ctx
->_TexEnvProgram
=
696 (struct fragment_program
*)
697 ctx
->Driver
.NewProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
, 0);
700 p
.program
= ctx
->_TexEnvProgram
;
702 if (p
.program
->Instructions
== NULL
) {
703 p
.program
->Instructions
= MALLOC(sizeof(struct fp_instruction
) * 100);
706 p
.program
->Base
.NumInstructions
= 0;
707 p
.program
->Base
.Target
= GL_FRAGMENT_PROGRAM_ARB
;
708 p
.program
->NumTexIndirections
= 1; /* correct? */
709 p
.program
->NumTexInstructions
= 0;
710 p
.program
->NumAluInstructions
= 0;
711 p
.program
->Base
.String
= 0;
712 p
.program
->Base
.NumInstructions
=
713 p
.program
->Base
.NumTemporaries
=
714 p
.program
->Base
.NumParameters
=
715 p
.program
->Base
.NumAttributes
= p
.program
->Base
.NumAddressRegs
= 0;
716 if (p
.program
->Parameters
)
717 _mesa_free_parameter_list(p
.program
->Parameters
);
718 p
.program
->Parameters
= _mesa_new_parameter_list();
719 p
.program
->InputsRead
= 0;
720 p
.program
->OutputsWritten
= 0;
722 p
.src_texture
= undef
;
723 p
.src_previous
= undef
;
724 p
.last_tex_stage
= 0;
726 if (ctx
->Texture
._EnabledUnits
) {
727 for (unit
= 0 ; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
728 if (ctx
->Texture
.Unit
[unit
]._ReallyEnabled
) {
729 p
.last_tex_stage
= unit
;
732 for (unit
= 0 ; unit
< ctx
->Const
.MaxTextureUnits
; unit
++)
733 if (ctx
->Texture
.Unit
[unit
]._ReallyEnabled
) {
734 p
.src_previous
= emit_texenv( &p
, unit
);
735 p
.src_texture
= undef
;
736 p
.temp_flag
= 0xffff000;
737 p
.temp_flag
|= 1 << p
.src_previous
.idx
;
741 cf
= get_source( &p
, GL_PREVIOUS
, 0 );
742 out
= make_ureg( PROGRAM_OUTPUT
, FRAG_OUTPUT_COLR
);
744 if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
) {
745 /* Emit specular add.
747 struct ureg s
= register_input(&p
, FRAG_ATTRIB_COL1
);
748 emit_arith( &p
, FP_OPCODE_ADD
, out
, WRITEMASK_XYZ
, 0, cf
, s
, undef
);
750 else if (memcmp(&cf
, &out
, sizeof(cf
)) != 0) {
751 /* Will wind up in here if no texture enabled or a couple of
752 * other scenarios (GL_REPLACE for instance).
754 emit_arith( &p
, FP_OPCODE_MOV
, out
, WRITEMASK_XYZW
, 0, cf
, undef
, undef
);
759 emit_arith( &p
, FP_OPCODE_END
, undef
, WRITEMASK_XYZW
, 0, undef
, undef
, undef
);
761 if (ctx
->Fog
.Enabled
)
762 p
.program
->FogOption
= ctx
->Fog
.Mode
;
764 p
.program
->FogOption
= GL_NONE
;
766 if (p
.program
->NumTexIndirections
> ctx
->Const
.MaxFragmentProgramTexIndirections
)
767 program_error(&p
, "Exceeded max nr indirect texture lookups");
769 if (p
.program
->NumTexInstructions
> ctx
->Const
.MaxFragmentProgramTexInstructions
)
770 program_error(&p
, "Exceeded max TEX instructions");
772 if (p
.program
->NumAluInstructions
> ctx
->Const
.MaxFragmentProgramAluInstructions
)
773 program_error(&p
, "Exceeded max ALU instructions");
776 _mesa_debug_fp_inst(p
.program
->NumTexInstructions
+ p
.program
->NumAluInstructions
,
777 p
.program
->Instructions
);
781 /* Notify driver the fragment program has (potentially) changed.
783 ctx
->Driver
.ProgramStringNotify( ctx
, GL_FRAGMENT_PROGRAM_ARB
,