2 * Mesa 3-D graphics library
5 * Copyright (C) 2005 Tungsten Graphics 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 "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * TUNGSTEN GRAPHICS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Create a vertex program to execute the current fixed function T&L pipeline.
29 * \author Keith Whitwell
36 #include "t_context.h"
37 #include "t_vp_build.h"
39 #include "shader/program.h"
40 #include "shader/nvvertprog.h"
41 #include "shader/arbvertparse.h"
44 unsigned light_global_enabled
:1;
45 unsigned light_local_viewer
:1;
46 unsigned light_twoside
:1;
47 unsigned light_color_material
:1;
48 unsigned light_color_material_mask
:12;
49 unsigned light_material_mask
:12;
52 unsigned rescale_normals
:1;
53 unsigned fog_source_is_depth
:1;
54 unsigned tnl_do_vertex_fog
:1;
55 unsigned separate_specular
:1;
56 unsigned fog_enabled
:1;
58 unsigned point_attenuated
:1;
60 unsigned texgen_enabled_global
:1;
63 unsigned light_enabled
:1;
64 unsigned light_eyepos3_is_zero
:1;
65 unsigned light_spotcutoff_is_180
:1;
66 unsigned light_attenuated
:1;
67 unsigned texunit_really_enabled
:1;
68 unsigned texmat_enabled
:1;
69 unsigned texgen_enabled
:4;
70 unsigned texgen_mode0
:4;
71 unsigned texgen_mode1
:4;
72 unsigned texgen_mode2
:4;
73 unsigned texgen_mode3
:4;
84 static GLuint
translate_fog_mode( GLenum mode
)
87 case GL_LINEAR
: return FOG_LINEAR
;
88 case GL_EXP
: return FOG_EXP
;
89 case GL_EXP2
: return FOG_EXP2
;
90 default: return FOG_UNKNOWN
;
95 #define TXG_OBJ_LINEAR 1
96 #define TXG_EYE_LINEAR 2
97 #define TXG_SPHERE_MAP 3
98 #define TXG_REFLECTION_MAP 4
99 #define TXG_NORMAL_MAP 5
101 static GLuint
translate_texgen( GLboolean enabled
, GLenum mode
)
107 case GL_OBJECT_LINEAR
: return TXG_OBJ_LINEAR
;
108 case GL_EYE_LINEAR
: return TXG_EYE_LINEAR
;
109 case GL_SPHERE_MAP
: return TXG_SPHERE_MAP
;
110 case GL_REFLECTION_MAP_NV
: return TXG_REFLECTION_MAP
;
111 case GL_NORMAL_MAP_NV
: return TXG_NORMAL_MAP
;
112 default: return TXG_NONE
;
116 static struct state_key
*make_state_key( GLcontext
*ctx
)
118 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
119 struct vertex_buffer
*VB
= &tnl
->vb
;
120 struct state_key
*key
= CALLOC_STRUCT(state_key
);
123 key
->separate_specular
= (ctx
->Light
.Model
.ColorControl
==
124 GL_SEPARATE_SPECULAR_COLOR
);
126 if (ctx
->Light
.Enabled
) {
127 key
->light_global_enabled
= 1;
129 if (ctx
->Light
.Model
.LocalViewer
)
130 key
->light_local_viewer
= 1;
132 if (ctx
->Light
.Model
.TwoSide
)
133 key
->light_twoside
= 1;
135 if (ctx
->Light
.ColorMaterialEnabled
)
136 key
->light_color_material
= 1;
138 if (ctx
->Light
.ColorMaterialBitmask
)
139 key
->light_color_material_mask
= 1;
141 for (i
= _TNL_ATTRIB_MAT_FRONT_AMBIENT
; i
< _TNL_ATTRIB_INDEX
; i
++)
142 if (VB
->AttribPtr
[i
]->stride
)
143 key
->light_material_mask
|= 1<<i
;
145 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
146 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
148 if (light
->Enabled
) {
149 key
->unit
[i
].light_enabled
= 1;
151 if (light
->EyePosition
[3] == 0.0)
152 key
->unit
[i
].light_eyepos3_is_zero
= 1;
154 if (light
->SpotCutoff
== 180.0)
155 key
->unit
[i
].light_spotcutoff_is_180
= 1;
157 if (light
->ConstantAttenuation
!= 1.0 ||
158 light
->LinearAttenuation
!= 1.0 ||
159 light
->QuadraticAttenuation
!= 1.0)
160 key
->unit
[i
].light_attenuated
= 1;
165 if (ctx
->Transform
.Normalize
)
168 if (ctx
->Transform
.RescaleNormals
)
169 key
->rescale_normals
= 1;
171 if (ctx
->Fog
.Enabled
)
172 key
->fog_enabled
= 1;
174 if (key
->fog_enabled
) {
175 if (ctx
->Fog
.FogCoordinateSource
== GL_FRAGMENT_DEPTH_EXT
)
176 key
->fog_source_is_depth
= 1;
178 if (tnl
->_DoVertexFog
)
179 key
->tnl_do_vertex_fog
= 1;
181 key
->fog_mode
= translate_fog_mode(ctx
->Fog
.Mode
);
184 if (ctx
->Point
._Attenuated
)
185 key
->point_attenuated
= 1;
187 if (ctx
->Texture
._TexGenEnabled
)
188 key
->texgen_enabled_global
= 1;
190 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
191 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
193 if (texUnit
->_ReallyEnabled
)
194 key
->unit
[i
].texunit_really_enabled
= 1;
196 if (ctx
->Texture
._TexMatEnabled
& ENABLE_TEXMAT(i
))
197 key
->unit
[i
].texmat_enabled
= 1;
199 if (texUnit
->TexGenEnabled
) {
200 key
->unit
[i
].texgen_enabled
= 1;
202 key
->unit
[i
].texgen_mode0
=
203 translate_texgen( texUnit
->TexGenEnabled
& (1<<0),
205 key
->unit
[i
].texgen_mode1
=
206 translate_texgen( texUnit
->TexGenEnabled
& (1<<1),
208 key
->unit
[i
].texgen_mode2
=
209 translate_texgen( texUnit
->TexGenEnabled
& (1<<2),
211 key
->unit
[i
].texgen_mode3
=
212 translate_texgen( texUnit
->TexGenEnabled
& (1<<3),
222 /* Very useful debugging tool - produces annotated listing of
223 * generated program with line/function references for each
224 * instruction back into this file:
228 /* Should be tunable by the driver - do we want to do matrix
229 * multiplications with DP4's or with MUL/MAD's? SSE works better
230 * with the latter, drivers may differ.
236 /* Use uregs to represent registers internally, translate to Mesa's
237 * expected formats on emit.
239 * NOTE: These are passed by value extensively in this file rather
240 * than as usual by pointer reference. If this disturbs you, try
241 * remembering they are just 32bits in size.
243 * GCC is smart enough to deal with these dword-sized structures in
244 * much the same way as if I had defined them as dwords and was using
245 * macros to access and set the fields. This is much nicer and easier
258 const struct state_key
*state
;
259 struct vertex_program
*program
;
262 GLuint temp_reserved
;
264 struct ureg eye_position
;
265 struct ureg eye_position_normalized
;
266 struct ureg eye_normal
;
267 struct ureg identity
;
270 GLuint color_materials
;
274 const static struct ureg undef
= {
292 static struct ureg
make_ureg(GLuint file
, GLuint idx
)
298 reg
.swz
= SWIZZLE_NOOP
;
305 static struct ureg
negate( struct ureg reg
)
312 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
314 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
317 GET_SWZ(reg
.swz
, w
));
322 static struct ureg
swizzle1( struct ureg reg
, int x
)
324 return swizzle(reg
, x
, x
, x
, x
);
327 static struct ureg
get_temp( struct tnl_program
*p
)
329 int bit
= ffs( ~p
->temp_in_use
);
331 fprintf(stderr
, "%s: out of temporaries\n", __FILE__
);
335 p
->temp_in_use
|= 1<<(bit
-1);
336 return make_ureg(PROGRAM_TEMPORARY
, bit
-1);
339 static struct ureg
reserve_temp( struct tnl_program
*p
)
341 struct ureg temp
= get_temp( p
);
342 p
->temp_reserved
|= 1<<temp
.idx
;
346 static void release_temp( struct tnl_program
*p
, struct ureg reg
)
348 if (reg
.file
== PROGRAM_TEMPORARY
) {
349 p
->temp_in_use
&= ~(1<<reg
.idx
);
350 p
->temp_in_use
|= p
->temp_reserved
; /* can't release reserved temps */
354 static void release_temps( struct tnl_program
*p
)
356 p
->temp_in_use
= p
->temp_reserved
;
361 static struct ureg
register_input( struct tnl_program
*p
, GLuint input
)
363 p
->program
->InputsRead
|= (1<<input
);
364 return make_ureg(PROGRAM_INPUT
, input
);
367 static struct ureg
register_output( struct tnl_program
*p
, GLuint output
)
369 p
->program
->OutputsWritten
|= (1<<output
);
370 return make_ureg(PROGRAM_OUTPUT
, output
);
373 static struct ureg
register_const4f( struct tnl_program
*p
,
385 idx
= _mesa_add_unnamed_constant( p
->program
->Parameters
, values
);
386 return make_ureg(PROGRAM_STATE_VAR
, idx
);
389 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
390 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
391 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
392 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
394 static GLboolean
is_undef( struct ureg reg
)
396 return reg
.file
== 0xf;
399 static struct ureg
get_identity_param( struct tnl_program
*p
)
401 if (is_undef(p
->identity
))
402 p
->identity
= register_const4f(p
, 0,0,0,1);
407 static struct ureg
register_param6( struct tnl_program
*p
,
423 idx
= _mesa_add_state_reference( p
->program
->Parameters
, tokens
);
424 return make_ureg(PROGRAM_STATE_VAR
, idx
);
428 #define register_param1(p,s0) register_param6(p,s0,0,0,0,0,0)
429 #define register_param2(p,s0,s1) register_param6(p,s0,s1,0,0,0,0)
430 #define register_param3(p,s0,s1,s2) register_param6(p,s0,s1,s2,0,0,0)
431 #define register_param4(p,s0,s1,s2,s3) register_param6(p,s0,s1,s2,s3,0,0)
434 static void register_matrix_param6( struct tnl_program
*p
,
441 struct ureg
*matrix
)
445 /* This is a bit sad as the support is there to pull the whole
446 * matrix out in one go:
448 for (i
= 0; i
<= s4
- s3
; i
++)
449 matrix
[i
] = register_param6( p
, s0
, s1
, s2
, i
, i
, s5
);
453 static void emit_arg( struct vp_src_register
*src
,
456 src
->File
= reg
.file
;
457 src
->Index
= reg
.idx
;
458 src
->Swizzle
= reg
.swz
;
459 src
->Negate
= reg
.negate
;
464 static void emit_dst( struct vp_dst_register
*dst
,
465 struct ureg reg
, GLuint mask
)
467 dst
->File
= reg
.file
;
468 dst
->Index
= reg
.idx
;
469 /* allow zero as a shorthand for xyzw */
470 dst
->WriteMask
= mask
? mask
: WRITEMASK_XYZW
;
474 static void debug_insn( struct vp_instruction
*inst
, const char *fn
,
478 static const char *last_fn
;
482 _mesa_printf("%s:\n", fn
);
485 _mesa_printf("%d:\t", line
);
486 _mesa_debug_vp_inst(1, inst
);
491 static void emit_op3fn(struct tnl_program
*p
,
501 GLuint nr
= p
->program
->Base
.NumInstructions
++;
502 struct vp_instruction
*inst
= &p
->program
->Instructions
[nr
];
504 if (p
->program
->Base
.NumInstructions
> MAX_INSN
) {
505 _mesa_problem(0, "Out of instructions in emit_op3fn\n");
513 emit_arg( &inst
->SrcReg
[0], src0
);
514 emit_arg( &inst
->SrcReg
[1], src1
);
515 emit_arg( &inst
->SrcReg
[2], src2
);
517 emit_dst( &inst
->DstReg
, dest
, mask
);
519 debug_insn(inst
, fn
, line
);
524 #define emit_op3(p, op, dst, mask, src0, src1, src2) \
525 emit_op3fn(p, op, dst, mask, src0, src1, src2, __FUNCTION__, __LINE__)
527 #define emit_op2(p, op, dst, mask, src0, src1) \
528 emit_op3fn(p, op, dst, mask, src0, src1, undef, __FUNCTION__, __LINE__)
530 #define emit_op1(p, op, dst, mask, src0) \
531 emit_op3fn(p, op, dst, mask, src0, undef, undef, __FUNCTION__, __LINE__)
534 static struct ureg
make_temp( struct tnl_program
*p
, struct ureg reg
)
536 if (reg
.file
== PROGRAM_TEMPORARY
&&
537 !(p
->temp_reserved
& (1<<reg
.idx
)))
540 struct ureg temp
= get_temp(p
);
541 emit_op1(p
, VP_OPCODE_MOV
, temp
, 0, reg
);
547 /* Currently no tracking performed of input/output/register size or
548 * active elements. Could be used to reduce these operations, as
549 * could the matrix type.
551 static void emit_matrix_transform_vec4( struct tnl_program
*p
,
553 const struct ureg
*mat
,
556 emit_op2(p
, VP_OPCODE_DP4
, dest
, WRITEMASK_X
, src
, mat
[0]);
557 emit_op2(p
, VP_OPCODE_DP4
, dest
, WRITEMASK_Y
, src
, mat
[1]);
558 emit_op2(p
, VP_OPCODE_DP4
, dest
, WRITEMASK_Z
, src
, mat
[2]);
559 emit_op2(p
, VP_OPCODE_DP4
, dest
, WRITEMASK_W
, src
, mat
[3]);
562 /* This version is much easier to implement if writemasks are not
563 * supported natively on the target or (like SSE), the target doesn't
564 * have a clean/obvious dotproduct implementation.
566 static void emit_transpose_matrix_transform_vec4( struct tnl_program
*p
,
568 const struct ureg
*mat
,
573 if (dest
.file
!= PROGRAM_TEMPORARY
)
578 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, swizzle1(src
,X
), mat
[0]);
579 emit_op3(p
, VP_OPCODE_MAD
, tmp
, 0, swizzle1(src
,Y
), mat
[1], tmp
);
580 emit_op3(p
, VP_OPCODE_MAD
, tmp
, 0, swizzle1(src
,Z
), mat
[2], tmp
);
581 emit_op3(p
, VP_OPCODE_MAD
, dest
, 0, swizzle1(src
,W
), mat
[3], tmp
);
583 if (dest
.file
!= PROGRAM_TEMPORARY
)
584 release_temp(p
, tmp
);
587 static void emit_matrix_transform_vec3( struct tnl_program
*p
,
589 const struct ureg
*mat
,
592 emit_op2(p
, VP_OPCODE_DP3
, dest
, WRITEMASK_X
, src
, mat
[0]);
593 emit_op2(p
, VP_OPCODE_DP3
, dest
, WRITEMASK_Y
, src
, mat
[1]);
594 emit_op2(p
, VP_OPCODE_DP3
, dest
, WRITEMASK_Z
, src
, mat
[2]);
598 static void emit_normalize_vec3( struct tnl_program
*p
,
602 struct ureg tmp
= get_temp(p
);
603 emit_op2(p
, VP_OPCODE_DP3
, tmp
, 0, src
, src
);
604 emit_op1(p
, VP_OPCODE_RSQ
, tmp
, 0, tmp
);
605 emit_op2(p
, VP_OPCODE_MUL
, dest
, 0, src
, tmp
);
606 release_temp(p
, tmp
);
609 static void emit_passthrough( struct tnl_program
*p
,
613 struct ureg out
= register_output(p
, output
);
614 emit_op1(p
, VP_OPCODE_MOV
, out
, 0, register_input(p
, input
));
617 static struct ureg
get_eye_position( struct tnl_program
*p
)
619 if (is_undef(p
->eye_position
)) {
620 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
621 struct ureg modelview
[4];
623 p
->eye_position
= reserve_temp(p
);
626 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 0, 3,
627 STATE_MATRIX
, modelview
);
629 emit_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
632 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 0, 3,
633 STATE_MATRIX_TRANSPOSE
, modelview
);
635 emit_transpose_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
639 return p
->eye_position
;
643 static struct ureg
get_eye_position_normalized( struct tnl_program
*p
)
645 if (is_undef(p
->eye_position_normalized
)) {
646 struct ureg eye
= get_eye_position(p
);
647 p
->eye_position_normalized
= reserve_temp(p
);
648 emit_normalize_vec3(p
, p
->eye_position_normalized
, eye
);
651 return p
->eye_position_normalized
;
655 static struct ureg
get_eye_normal( struct tnl_program
*p
)
657 if (is_undef(p
->eye_normal
)) {
658 struct ureg normal
= register_input(p
, VERT_ATTRIB_NORMAL
);
659 struct ureg mvinv
[3];
661 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 0, 2,
662 STATE_MATRIX_INVTRANS
, mvinv
);
664 p
->eye_normal
= reserve_temp(p
);
666 /* Transform to eye space:
668 emit_matrix_transform_vec3( p
, p
->eye_normal
, mvinv
, normal
);
670 /* Normalize/Rescale:
672 if (p
->state
->normalize
) {
673 emit_normalize_vec3( p
, p
->eye_normal
, p
->eye_normal
);
675 else if (p
->state
->rescale_normals
) {
676 struct ureg rescale
= register_param2(p
, STATE_INTERNAL
,
679 emit_op2( p
, VP_OPCODE_MUL
, p
->eye_normal
, 0, normal
,
680 swizzle1(rescale
, X
));
684 return p
->eye_normal
;
689 static void build_hpos( struct tnl_program
*p
)
691 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
692 struct ureg hpos
= register_output( p
, VERT_RESULT_HPOS
);
696 register_matrix_param6( p
, STATE_MATRIX
, STATE_MVP
, 0, 0, 3,
698 emit_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
701 register_matrix_param6( p
, STATE_MATRIX
, STATE_MVP
, 0, 0, 3,
702 STATE_MATRIX_TRANSPOSE
, mvp
);
703 emit_transpose_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
708 static GLuint
material_attrib( GLuint side
, GLuint property
)
710 return (_TNL_ATTRIB_MAT_FRONT_AMBIENT
+
711 (property
- STATE_AMBIENT
) * 2 +
715 static void set_material_flags( struct tnl_program
*p
)
717 p
->color_materials
= 0;
720 if (p
->state
->light_color_material
) {
722 p
->color_materials
= p
->state
->light_color_material_mask
;
725 p
->materials
|= p
->state
->light_material_mask
;
729 static struct ureg
get_material( struct tnl_program
*p
, GLuint side
,
732 GLuint attrib
= material_attrib(side
, property
);
734 if (p
->color_materials
& (1<<attrib
))
735 return register_input(p
, VERT_ATTRIB_COLOR0
);
736 else if (p
->materials
& (1<<attrib
))
737 return register_input( p
, attrib
);
739 return register_param3( p
, STATE_MATERIAL
, side
, property
);
742 #define SCENE_COLOR_BITS(side) (( _TNL_BIT_MAT_FRONT_EMISSION | \
743 _TNL_BIT_MAT_FRONT_AMBIENT | \
744 _TNL_BIT_MAT_FRONT_DIFFUSE) << (side))
746 /* Either return a precalculated constant value or emit code to
747 * calculate these values dynamically in the case where material calls
748 * are present between begin/end pairs.
750 * Probably want to shift this to the program compilation phase - if
751 * we always emitted the calculation here, a smart compiler could
752 * detect that it was constant (given a certain set of inputs), and
753 * lift it out of the main loop. That way the programs created here
754 * would be independent of the vertex_buffer details.
756 static struct ureg
get_scenecolor( struct tnl_program
*p
, GLuint side
)
758 if (p
->materials
& SCENE_COLOR_BITS(side
)) {
759 struct ureg lm_ambient
= register_param1(p
, STATE_LIGHTMODEL_AMBIENT
);
760 struct ureg material_emission
= get_material(p
, side
, STATE_EMISSION
);
761 struct ureg material_ambient
= get_material(p
, side
, STATE_AMBIENT
);
762 struct ureg material_diffuse
= get_material(p
, side
, STATE_DIFFUSE
);
763 struct ureg tmp
= make_temp(p
, material_diffuse
);
764 emit_op3(p
, VP_OPCODE_MAD
, tmp
, WRITEMASK_XYZ
, lm_ambient
,
765 material_ambient
, material_emission
);
769 return register_param2( p
, STATE_LIGHTMODEL_SCENECOLOR
, side
);
773 static struct ureg
get_lightprod( struct tnl_program
*p
, GLuint light
,
774 GLuint side
, GLuint property
)
776 GLuint attrib
= material_attrib(side
, property
);
777 if (p
->materials
& (1<<attrib
)) {
778 struct ureg light_value
=
779 register_param3(p
, STATE_LIGHT
, light
, property
);
780 struct ureg material_value
= get_material(p
, side
, property
);
781 struct ureg tmp
= get_temp(p
);
782 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, light_value
, material_value
);
786 return register_param4(p
, STATE_LIGHTPROD
, light
, side
, property
);
789 static struct ureg
calculate_light_attenuation( struct tnl_program
*p
,
794 struct ureg attenuation
= register_param3(p
, STATE_LIGHT
, i
,
796 struct ureg att
= get_temp(p
);
798 /* Calculate spot attenuation:
800 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
801 struct ureg spot_dir
= register_param3(p
, STATE_LIGHT
, i
,
802 STATE_SPOT_DIRECTION
);
803 struct ureg spot
= get_temp(p
);
804 struct ureg slt
= get_temp(p
);
806 emit_normalize_vec3( p
, spot
, spot_dir
); /* XXX: precompute! */
807 emit_op2(p
, VP_OPCODE_DP3
, spot
, 0, negate(VPpli
), spot_dir
);
808 emit_op2(p
, VP_OPCODE_SLT
, slt
, 0, swizzle1(spot_dir
,W
), spot
);
809 emit_op2(p
, VP_OPCODE_POW
, spot
, 0, spot
, swizzle1(attenuation
, W
));
810 emit_op2(p
, VP_OPCODE_MUL
, att
, 0, slt
, spot
);
812 release_temp(p
, spot
);
813 release_temp(p
, slt
);
816 /* Calculate distance attenuation:
818 if (p
->state
->unit
[i
].light_attenuated
) {
821 emit_op1(p
, VP_OPCODE_RCP
, dist
, WRITEMASK_YZ
, dist
);
823 emit_op2(p
, VP_OPCODE_MUL
, dist
, WRITEMASK_XZ
, dist
, swizzle1(dist
,Y
));
825 emit_op2(p
, VP_OPCODE_DP3
, dist
, 0, attenuation
, dist
);
827 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
829 emit_op1(p
, VP_OPCODE_RCP
, dist
, 0, dist
);
830 /* spot-atten * dist-atten */
831 emit_op2(p
, VP_OPCODE_MUL
, att
, 0, dist
, att
);
834 emit_op1(p
, VP_OPCODE_RCP
, att
, 0, dist
);
845 /* Need to add some addtional parameters to allow lighting in object
846 * space - STATE_SPOT_DIRECTION and STATE_HALF implicitly assume eye
849 static void build_lighting( struct tnl_program
*p
)
851 const GLboolean twoside
= p
->state
->light_twoside
;
852 const GLboolean separate
= p
->state
->separate_specular
;
853 GLuint nr_lights
= 0, count
= 0;
854 struct ureg normal
= get_eye_normal(p
);
855 struct ureg lit
= get_temp(p
);
856 struct ureg dots
= get_temp(p
);
857 struct ureg _col0
= undef
, _col1
= undef
;
858 struct ureg _bfc0
= undef
, _bfc1
= undef
;
861 for (i
= 0; i
< MAX_LIGHTS
; i
++)
862 if (p
->state
->unit
[i
].light_enabled
)
865 set_material_flags(p
);
868 struct ureg shininess
= get_material(p
, 0, STATE_SHININESS
);
869 emit_op1(p
, VP_OPCODE_MOV
, dots
, WRITEMASK_W
, swizzle1(shininess
,X
));
870 release_temp(p
, shininess
);
872 _col0
= make_temp(p
, get_scenecolor(p
, 0));
874 _col1
= make_temp(p
, get_identity_param(p
));
881 struct ureg shininess
= get_material(p
, 1, STATE_SHININESS
);
882 emit_op1(p
, VP_OPCODE_MOV
, dots
, WRITEMASK_Z
,
883 negate(swizzle1(shininess
,X
)));
884 release_temp(p
, shininess
);
886 _bfc0
= make_temp(p
, get_scenecolor(p
, 1));
888 _bfc1
= make_temp(p
, get_identity_param(p
));
894 /* If no lights, still need to emit the scenecolor.
896 if (nr_lights
== 0) {
898 struct ureg res0
= register_output( p
, VERT_RESULT_COL0
);
899 emit_op1(p
, VP_OPCODE_MOV
, res0
, 0, _col0
);
903 struct ureg res1
= register_output( p
, VERT_RESULT_COL1
);
904 emit_op1(p
, VP_OPCODE_MOV
, res1
, 0, _col1
);
908 struct ureg res0
= register_output( p
, VERT_RESULT_BFC0
);
909 emit_op1(p
, VP_OPCODE_MOV
, res0
, 0, _bfc0
);
912 if (twoside
&& separate
) {
913 struct ureg res1
= register_output( p
, VERT_RESULT_BFC1
);
914 emit_op1(p
, VP_OPCODE_MOV
, res1
, 0, _bfc1
);
922 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
923 if (p
->state
->unit
[i
].light_enabled
) {
924 struct ureg half
= undef
;
925 struct ureg att
= undef
, VPpli
= undef
;
929 if (p
->state
->unit
[i
].light_eyepos3_is_zero
) {
930 /* Can used precomputed constants in this case.
931 * Attenuation never applies to infinite lights.
933 VPpli
= register_param3(p
, STATE_LIGHT
, i
,
934 STATE_POSITION_NORMALIZED
);
935 half
= register_param3(p
, STATE_LIGHT
, i
, STATE_HALF
);
938 struct ureg Ppli
= register_param3(p
, STATE_LIGHT
, i
,
940 struct ureg V
= get_eye_position(p
);
941 struct ureg dist
= get_temp(p
);
946 /* Calulate VPpli vector
948 emit_op2(p
, VP_OPCODE_SUB
, VPpli
, 0, Ppli
, V
);
950 /* Normalize VPpli. The dist value also used in
953 emit_op2(p
, VP_OPCODE_DP3
, dist
, 0, VPpli
, VPpli
);
954 emit_op1(p
, VP_OPCODE_RSQ
, dist
, 0, dist
);
955 emit_op2(p
, VP_OPCODE_MUL
, VPpli
, 0, VPpli
, dist
);
958 /* Calculate attenuation:
960 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
||
961 p
->state
->unit
[i
].light_attenuated
) {
962 att
= calculate_light_attenuation(p
, i
, VPpli
, dist
);
966 /* Calculate viewer direction, or use infinite viewer:
968 if (p
->state
->light_local_viewer
) {
969 struct ureg eye_hat
= get_eye_position_normalized(p
);
970 emit_op2(p
, VP_OPCODE_SUB
, half
, 0, VPpli
, eye_hat
);
973 struct ureg z_dir
= swizzle(get_identity_param(p
),X
,Y
,W
,Z
);
974 emit_op2(p
, VP_OPCODE_ADD
, half
, 0, VPpli
, z_dir
);
977 emit_normalize_vec3(p
, half
, half
);
979 release_temp(p
, dist
);
982 /* Calculate dot products:
984 emit_op2(p
, VP_OPCODE_DP3
, dots
, WRITEMASK_X
, normal
, VPpli
);
985 emit_op2(p
, VP_OPCODE_DP3
, dots
, WRITEMASK_Y
, normal
, half
);
988 /* Front face lighting:
991 struct ureg ambient
= get_lightprod(p
, i
, 0, STATE_AMBIENT
);
992 struct ureg diffuse
= get_lightprod(p
, i
, 0, STATE_DIFFUSE
);
993 struct ureg specular
= get_lightprod(p
, i
, 0, STATE_SPECULAR
);
994 struct ureg res0
, res1
;
996 emit_op1(p
, VP_OPCODE_LIT
, lit
, 0, dots
);
999 emit_op2(p
, VP_OPCODE_MUL
, lit
, 0, lit
, att
);
1002 if (count
== nr_lights
) {
1004 res0
= register_output( p
, VERT_RESULT_COL0
);
1005 res1
= register_output( p
, VERT_RESULT_COL1
);
1009 res1
= register_output( p
, VERT_RESULT_COL0
);
1016 emit_op3(p
, VP_OPCODE_MAD
, _col0
, 0, swizzle1(lit
,X
), ambient
, _col0
);
1017 emit_op3(p
, VP_OPCODE_MAD
, res0
, 0, swizzle1(lit
,Y
), diffuse
, _col0
);
1018 emit_op3(p
, VP_OPCODE_MAD
, res1
, 0, swizzle1(lit
,Z
), specular
, _col1
);
1020 release_temp(p
, ambient
);
1021 release_temp(p
, diffuse
);
1022 release_temp(p
, specular
);
1025 /* Back face lighting:
1028 struct ureg ambient
= get_lightprod(p
, i
, 1, STATE_AMBIENT
);
1029 struct ureg diffuse
= get_lightprod(p
, i
, 1, STATE_DIFFUSE
);
1030 struct ureg specular
= get_lightprod(p
, i
, 1, STATE_SPECULAR
);
1031 struct ureg res0
, res1
;
1033 emit_op1(p
, VP_OPCODE_LIT
, lit
, 0, negate(swizzle(dots
,X
,Y
,W
,Z
)));
1036 emit_op2(p
, VP_OPCODE_MUL
, lit
, 0, lit
, att
);
1038 if (count
== nr_lights
) {
1040 res0
= register_output( p
, VERT_RESULT_BFC0
);
1041 res1
= register_output( p
, VERT_RESULT_BFC1
);
1045 res1
= register_output( p
, VERT_RESULT_BFC0
);
1053 emit_op3(p
, VP_OPCODE_MAD
, _bfc0
, 0, swizzle1(lit
,X
), ambient
, _bfc0
);
1054 emit_op3(p
, VP_OPCODE_MAD
, res0
, 0, swizzle1(lit
,Y
), diffuse
, _bfc0
);
1055 emit_op3(p
, VP_OPCODE_MAD
, res1
, 0, swizzle1(lit
,Z
), specular
, _bfc1
);
1057 release_temp(p
, ambient
);
1058 release_temp(p
, diffuse
);
1059 release_temp(p
, specular
);
1062 release_temp(p
, half
);
1063 release_temp(p
, VPpli
);
1064 release_temp(p
, att
);
1072 static void build_fog( struct tnl_program
*p
)
1074 struct ureg fog
= register_output(p
, VERT_RESULT_FOGC
);
1077 if (p
->state
->fog_source_is_depth
) {
1078 input
= swizzle1(get_eye_position(p
), Z
);
1081 input
= swizzle1(register_input(p
, VERT_ATTRIB_FOG
), X
);
1084 if (p
->state
->tnl_do_vertex_fog
) {
1085 struct ureg params
= register_param1(p
, STATE_FOG_PARAMS
);
1086 struct ureg tmp
= get_temp(p
);
1088 switch (p
->state
->fog_mode
) {
1090 struct ureg id
= get_identity_param(p
);
1091 emit_op2(p
, VP_OPCODE_SUB
, tmp
, 0, swizzle1(params
,Z
), input
);
1092 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, tmp
, swizzle1(params
,W
));
1093 emit_op2(p
, VP_OPCODE_MAX
, tmp
, 0, tmp
, swizzle1(id
,X
)); /* saturate */
1094 emit_op2(p
, VP_OPCODE_MIN
, fog
, WRITEMASK_X
, tmp
, swizzle1(id
,W
));
1098 emit_op1(p
, VP_OPCODE_ABS
, tmp
, 0, input
);
1099 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, tmp
, swizzle1(params
,X
));
1100 emit_op2(p
, VP_OPCODE_POW
, fog
, WRITEMASK_X
,
1101 register_const1f(p
, M_E
), negate(tmp
));
1104 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, input
, swizzle1(params
,X
));
1105 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, tmp
, tmp
);
1106 emit_op2(p
, VP_OPCODE_POW
, fog
, WRITEMASK_X
,
1107 register_const1f(p
, M_E
), negate(tmp
));
1111 release_temp(p
, tmp
);
1114 /* results = incoming fog coords (compute fog per-fragment later)
1116 * KW: Is it really necessary to do anything in this case?
1118 emit_op1(p
, VP_OPCODE_MOV
, fog
, WRITEMASK_X
, input
);
1122 static void build_reflect_texgen( struct tnl_program
*p
,
1126 struct ureg normal
= get_eye_normal(p
);
1127 struct ureg eye_hat
= get_eye_position_normalized(p
);
1128 struct ureg tmp
= get_temp(p
);
1131 emit_op2(p
, VP_OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1133 emit_op2(p
, VP_OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1135 emit_op3(p
, VP_OPCODE_MAD
, dest
, writemask
, negate(tmp
), normal
, eye_hat
);
1138 static void build_sphere_texgen( struct tnl_program
*p
,
1142 struct ureg normal
= get_eye_normal(p
);
1143 struct ureg eye_hat
= get_eye_position_normalized(p
);
1144 struct ureg tmp
= get_temp(p
);
1145 struct ureg half
= register_scalar_const(p
, .5);
1146 struct ureg r
= get_temp(p
);
1147 struct ureg inv_m
= get_temp(p
);
1148 struct ureg id
= get_identity_param(p
);
1150 /* Could share the above calculations, but it would be
1151 * a fairly odd state for someone to set (both sphere and
1152 * reflection active for different texture coordinate
1153 * components. Of course - if two texture units enable
1154 * reflect and/or sphere, things start to tilt in favour
1155 * of seperating this out:
1159 emit_op2(p
, VP_OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1161 emit_op2(p
, VP_OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1163 emit_op3(p
, VP_OPCODE_MAD
, r
, 0, negate(tmp
), normal
, eye_hat
);
1165 emit_op2(p
, VP_OPCODE_ADD
, tmp
, 0, r
, swizzle(id
,X
,Y
,W
,Z
));
1166 /* rx^2 + ry^2 + (rz+1)^2 */
1167 emit_op2(p
, VP_OPCODE_DP3
, tmp
, 0, tmp
, tmp
);
1169 emit_op1(p
, VP_OPCODE_RSQ
, tmp
, 0, tmp
);
1171 emit_op2(p
, VP_OPCODE_MUL
, inv_m
, 0, tmp
, half
);
1173 emit_op3(p
, VP_OPCODE_MAD
, dest
, writemask
, r
, inv_m
, half
);
1175 release_temp(p
, tmp
);
1177 release_temp(p
, inv_m
);
1181 static void build_texture_transform( struct tnl_program
*p
)
1185 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
1186 GLuint texmat_enabled
= p
->state
->unit
[i
].texmat_enabled
;
1188 if (p
->state
->unit
[i
].texgen_enabled
|| texmat_enabled
) {
1189 struct ureg out
= register_output(p
, VERT_RESULT_TEX0
+ i
);
1190 struct ureg out_texgen
= undef
;
1192 if (p
->state
->unit
[i
].texgen_enabled
) {
1193 GLuint copy_mask
= 0;
1194 GLuint sphere_mask
= 0;
1195 GLuint reflect_mask
= 0;
1196 GLuint normal_mask
= 0;
1200 out_texgen
= get_temp(p
);
1204 modes
[0] = p
->state
->unit
[i
].texgen_mode0
;
1205 modes
[1] = p
->state
->unit
[i
].texgen_mode1
;
1206 modes
[2] = p
->state
->unit
[i
].texgen_mode2
;
1207 modes
[3] = p
->state
->unit
[i
].texgen_mode3
;
1209 for (j
= 0; j
< 4; j
++) {
1211 case TXG_OBJ_LINEAR
: {
1212 struct ureg obj
= register_input(p
, VERT_ATTRIB_POS
);
1214 register_param3(p
, STATE_TEXGEN
, i
,
1215 STATE_TEXGEN_OBJECT_S
+ j
);
1217 emit_op2(p
, VP_OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1221 case TXG_EYE_LINEAR
: {
1222 struct ureg eye
= get_eye_position(p
);
1224 register_param3(p
, STATE_TEXGEN
, i
,
1225 STATE_TEXGEN_EYE_S
+ j
);
1227 emit_op2(p
, VP_OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1231 case TXG_SPHERE_MAP
:
1232 sphere_mask
|= WRITEMASK_X
<< j
;
1234 case TXG_REFLECTION_MAP
:
1235 reflect_mask
|= WRITEMASK_X
<< j
;
1237 case TXG_NORMAL_MAP
:
1238 normal_mask
|= WRITEMASK_X
<< j
;
1241 copy_mask
|= WRITEMASK_X
<< j
;
1248 build_sphere_texgen(p
, out_texgen
, sphere_mask
);
1252 build_reflect_texgen(p
, out_texgen
, reflect_mask
);
1256 struct ureg normal
= get_eye_normal(p
);
1257 emit_op1(p
, VP_OPCODE_MOV
, out_texgen
, normal_mask
, normal
);
1261 struct ureg in
= register_input(p
, VERT_ATTRIB_TEX0
+i
);
1262 emit_op1(p
, VP_OPCODE_MOV
, out_texgen
, copy_mask
, in
);
1266 if (texmat_enabled
) {
1267 struct ureg texmat
[4];
1268 struct ureg in
= (!is_undef(out_texgen
) ?
1270 register_input(p
, VERT_ATTRIB_TEX0
+i
));
1272 register_matrix_param6( p
, STATE_MATRIX
, STATE_TEXTURE
, i
,
1273 0, 3, STATE_MATRIX
, texmat
);
1274 emit_matrix_transform_vec4( p
, out
, texmat
, in
);
1277 register_matrix_param6( p
, STATE_MATRIX
, STATE_TEXTURE
, i
,
1278 0, 3, STATE_MATRIX_TRANSPOSE
, texmat
);
1279 emit_matrix_transform_vec4( p
, out
, texmat
, in
);
1285 else if (p
->state
->unit
[i
].texunit_really_enabled
) {
1286 /* KW: _ReallyEnabled isn't sufficient? Need to know whether
1287 * this texture unit is referenced by the fragment shader.
1289 emit_passthrough(p
, VERT_ATTRIB_TEX0
+i
, VERT_RESULT_TEX0
+i
);
1295 /* Seems like it could be tighter:
1297 static void build_pointsize( struct tnl_program
*p
)
1299 struct ureg eye
= get_eye_position(p
);
1300 struct ureg state_size
= register_param1(p
, STATE_POINT_SIZE
);
1301 struct ureg state_attenuation
= register_param1(p
, STATE_POINT_ATTENUATION
);
1302 struct ureg out
= register_output(p
, VERT_RESULT_PSIZ
);
1303 struct ureg ut
= get_temp(p
);
1305 /* 1, -Z, Z * Z, 1 */
1306 emit_op1(p
, VP_OPCODE_MOV
, ut
, 0, swizzle1(get_identity_param(p
), W
));
1307 emit_op2(p
, VP_OPCODE_MUL
, ut
, WRITEMASK_YZ
, ut
, negate(swizzle1(eye
, Z
)));
1308 emit_op2(p
, VP_OPCODE_MUL
, ut
, WRITEMASK_Z
, ut
, negate(swizzle1(eye
, Z
)));
1311 /* p1 + p2 * dist + p3 * dist * dist, 0 */
1312 emit_op2(p
, VP_OPCODE_DP3
, ut
, 0, ut
, state_attenuation
);
1315 emit_op1(p
, VP_OPCODE_RCP
, ut
, 0, ut
);
1317 /* out = pointSize / factor */
1318 emit_op2(p
, VP_OPCODE_MUL
, out
, WRITEMASK_X
, ut
, state_size
);
1320 release_temp(p
, ut
);
1323 static void build_tnl_program( struct tnl_program
*p
)
1324 { /* Emit the program, starting with modelviewproject:
1328 /* Lighting calculations:
1330 if (p
->state
->light_global_enabled
)
1333 emit_passthrough(p
, VERT_ATTRIB_COLOR0
, VERT_RESULT_COL0
);
1335 if (p
->state
->fog_enabled
)
1338 if (p
->state
->nr_tex
)
1339 build_texture_transform(p
);
1341 if (p
->state
->point_attenuated
)
1346 emit_op1(p
, VP_OPCODE_END
, undef
, 0, undef
);
1351 _mesa_printf ("\n");
1356 void create_new_program( const struct state_key
*key
,
1357 struct vertex_program
*program
,
1360 struct tnl_program p
;
1362 _mesa_memset(&p
, 0, sizeof(p
));
1364 p
.program
= program
;
1365 p
.eye_position
= undef
;
1366 p
.eye_position_normalized
= undef
;
1367 p
.eye_normal
= undef
;
1371 if (max_temps
>= sizeof(int) * 8)
1372 p
.temp_reserved
= 0;
1374 p
.temp_reserved
= ~((1<<max_temps
)-1);
1376 p
.program
->Instructions
= MALLOC(sizeof(struct vp_instruction
) * MAX_INSN
);
1377 p
.program
->Base
.String
= 0;
1378 p
.program
->Base
.NumInstructions
=
1379 p
.program
->Base
.NumTemporaries
=
1380 p
.program
->Base
.NumParameters
=
1381 p
.program
->Base
.NumAttributes
= p
.program
->Base
.NumAddressRegs
= 0;
1382 p
.program
->Parameters
= _mesa_new_parameter_list();
1383 p
.program
->InputsRead
= 0;
1384 p
.program
->OutputsWritten
= 0;
1386 build_tnl_program( &p
);
1389 static void *search_cache( struct tnl_cache
*cache
,
1394 struct tnl_cache
*c
;
1396 for (c
= cache
; c
; c
= c
->next
) {
1397 if (c
->hash
== hash
&& memcmp(c
->key
, key
, keysize
) == 0)
1404 static void cache_item( struct tnl_cache
**cache
,
1409 struct tnl_cache
*c
= MALLOC(sizeof(*c
));
1417 static GLuint
hash_key( struct state_key
*key
)
1419 GLuint
*ikey
= (GLuint
*)key
;
1422 /* I'm sure this can be improved on, but speed is important:
1424 for (i
= 0; i
< sizeof(*key
)/sizeof(GLuint
); i
++)
1430 void _tnl_UpdateFixedFunctionProgram( GLcontext
*ctx
)
1432 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1433 struct state_key
*key
;
1436 if (ctx
->VertexProgram
._Enabled
)
1439 /* Grab all the relevent state and put it in a single structure:
1441 key
= make_state_key(ctx
);
1442 hash
= hash_key(key
);
1444 /* Look for an already-prepared program for this state:
1446 ctx
->_TnlProgram
= (struct vertex_program
*)
1447 search_cache( tnl
->vp_cache
, hash
, key
, sizeof(key
) );
1449 /* OK, we'll have to build a new one:
1451 if (!ctx
->_TnlProgram
) {
1453 _mesa_printf("Build new TNL program\n");
1455 ctx
->_TnlProgram
= (struct vertex_program
*)
1456 ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
1458 create_new_program( key
, ctx
->_TnlProgram
,
1459 ctx
->Const
.MaxVertexProgramTemps
);
1461 cache_item(&tnl
->vp_cache
, hash
, key
, ctx
->_TnlProgram
);
1465 _mesa_printf("Found existing TNL program for key %x\n", hash
);
1468 /* Need a BindProgram callback for the driver?
1473 void _tnl_ProgramCacheDestroy( GLcontext
*ctx
)
1475 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1476 struct tnl_cache
*a
, *tmp
;
1478 for (a
= tnl
->vp_cache
; a
; a
= tmp
) {