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;
59 unsigned texture_enabled_global
:1;
62 unsigned light_enabled
:1;
63 unsigned light_eyepos3_is_zero
:1;
64 unsigned light_spotcutoff_is_180
:1;
65 unsigned light_attenuated
:1;
66 unsigned texunit_really_enabled
:1;
67 unsigned texmat_enabled
:1;
68 unsigned texgen_enabled
:4;
69 unsigned texgen_mode0
:4;
70 unsigned texgen_mode1
:4;
71 unsigned texgen_mode2
:4;
72 unsigned texgen_mode3
:4;
83 static GLuint
translate_fog_mode( GLenum mode
)
86 case GL_LINEAR
: return FOG_LINEAR
;
87 case GL_EXP
: return FOG_EXP
;
88 case GL_EXP2
: return FOG_EXP2
;
89 default: return FOG_UNKNOWN
;
94 #define TXG_OBJ_LINEAR 1
95 #define TXG_EYE_LINEAR 2
96 #define TXG_SPHERE_MAP 3
97 #define TXG_REFLECTION_MAP 4
98 #define TXG_NORMAL_MAP 5
100 static GLuint
translate_texgen( GLboolean enabled
, GLenum mode
)
106 case GL_OBJECT_LINEAR
: return TXG_OBJ_LINEAR
;
107 case GL_EYE_LINEAR
: return TXG_EYE_LINEAR
;
108 case GL_SPHERE_MAP
: return TXG_SPHERE_MAP
;
109 case GL_REFLECTION_MAP_NV
: return TXG_REFLECTION_MAP
;
110 case GL_NORMAL_MAP_NV
: return TXG_NORMAL_MAP
;
111 default: return TXG_NONE
;
115 static struct state_key
*make_state_key( GLcontext
*ctx
)
117 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
118 struct vertex_buffer
*VB
= &tnl
->vb
;
119 struct state_key
*key
= CALLOC_STRUCT(state_key
);
122 key
->separate_specular
= (ctx
->Light
.Model
.ColorControl
==
123 GL_SEPARATE_SPECULAR_COLOR
);
125 if (ctx
->Light
.Enabled
) {
126 key
->light_global_enabled
= 1;
128 if (ctx
->Light
.Model
.LocalViewer
)
129 key
->light_local_viewer
= 1;
131 if (ctx
->Light
.Model
.TwoSide
)
132 key
->light_twoside
= 1;
134 if (ctx
->Light
.ColorMaterialEnabled
) {
135 key
->light_color_material
= 1;
136 key
->light_color_material_mask
= ctx
->Light
.ColorMaterialBitmask
;
137 _mesa_printf("ColorMaterialBitmask %x / %x\n", ctx
->Light
.ColorMaterialBitmask
,
138 key
->light_color_material_mask
);
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
-_TNL_ATTRIB_MAT_FRONT_AMBIENT
);
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
!= 0.0 ||
159 light
->QuadraticAttenuation
!= 0.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 ctx
->Texture
._TexMatEnabled
||
189 ctx
->Texture
._EnabledUnits
)
190 key
->texture_enabled_global
= 1;
192 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
193 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
195 if (texUnit
->_ReallyEnabled
)
196 key
->unit
[i
].texunit_really_enabled
= 1;
198 if (ctx
->Texture
._TexMatEnabled
& ENABLE_TEXMAT(i
))
199 key
->unit
[i
].texmat_enabled
= 1;
201 if (texUnit
->TexGenEnabled
) {
202 key
->unit
[i
].texgen_enabled
= 1;
204 key
->unit
[i
].texgen_mode0
=
205 translate_texgen( texUnit
->TexGenEnabled
& (1<<0),
207 key
->unit
[i
].texgen_mode1
=
208 translate_texgen( texUnit
->TexGenEnabled
& (1<<1),
210 key
->unit
[i
].texgen_mode2
=
211 translate_texgen( texUnit
->TexGenEnabled
& (1<<2),
213 key
->unit
[i
].texgen_mode3
=
214 translate_texgen( texUnit
->TexGenEnabled
& (1<<3),
224 /* Very useful debugging tool - produces annotated listing of
225 * generated program with line/function references for each
226 * instruction back into this file:
228 #define DISASSEM (MESA_VERBOSE&VERBOSE_DISASSEM)
230 /* Should be tunable by the driver - do we want to do matrix
231 * multiplications with DP4's or with MUL/MAD's? SSE works better
232 * with the latter, drivers may differ.
238 /* Use uregs to represent registers internally, translate to Mesa's
239 * expected formats on emit.
241 * NOTE: These are passed by value extensively in this file rather
242 * than as usual by pointer reference. If this disturbs you, try
243 * remembering they are just 32bits in size.
245 * GCC is smart enough to deal with these dword-sized structures in
246 * much the same way as if I had defined them as dwords and was using
247 * macros to access and set the fields. This is much nicer and easier
260 const struct state_key
*state
;
261 struct vertex_program
*program
;
264 GLuint temp_reserved
;
266 struct ureg eye_position
;
267 struct ureg eye_position_normalized
;
268 struct ureg eye_normal
;
269 struct ureg identity
;
272 GLuint color_materials
;
276 const static struct ureg undef
= {
294 static struct ureg
make_ureg(GLuint file
, GLuint idx
)
300 reg
.swz
= SWIZZLE_NOOP
;
307 static struct ureg
negate( struct ureg reg
)
314 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
316 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
319 GET_SWZ(reg
.swz
, w
));
324 static struct ureg
swizzle1( struct ureg reg
, int x
)
326 return swizzle(reg
, x
, x
, x
, x
);
329 static struct ureg
get_temp( struct tnl_program
*p
)
331 int bit
= ffs( ~p
->temp_in_use
);
333 fprintf(stderr
, "%s: out of temporaries\n", __FILE__
);
337 p
->temp_in_use
|= 1<<(bit
-1);
338 return make_ureg(PROGRAM_TEMPORARY
, bit
-1);
341 static struct ureg
reserve_temp( struct tnl_program
*p
)
343 struct ureg temp
= get_temp( p
);
344 p
->temp_reserved
|= 1<<temp
.idx
;
348 static void release_temp( struct tnl_program
*p
, struct ureg reg
)
350 if (reg
.file
== PROGRAM_TEMPORARY
) {
351 p
->temp_in_use
&= ~(1<<reg
.idx
);
352 p
->temp_in_use
|= p
->temp_reserved
; /* can't release reserved temps */
356 static void release_temps( struct tnl_program
*p
)
358 p
->temp_in_use
= p
->temp_reserved
;
363 static struct ureg
register_input( struct tnl_program
*p
, GLuint input
)
365 p
->program
->InputsRead
|= (1<<input
);
366 return make_ureg(PROGRAM_INPUT
, input
);
369 static struct ureg
register_output( struct tnl_program
*p
, GLuint output
)
371 p
->program
->OutputsWritten
|= (1<<output
);
372 return make_ureg(PROGRAM_OUTPUT
, output
);
375 static struct ureg
register_const4f( struct tnl_program
*p
,
387 idx
= _mesa_add_unnamed_constant( p
->program
->Parameters
, values
);
388 return make_ureg(PROGRAM_STATE_VAR
, idx
);
391 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
392 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
393 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
394 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
396 static GLboolean
is_undef( struct ureg reg
)
398 return reg
.file
== 0xf;
401 static struct ureg
get_identity_param( struct tnl_program
*p
)
403 if (is_undef(p
->identity
))
404 p
->identity
= register_const4f(p
, 0,0,0,1);
409 static struct ureg
register_param6( struct tnl_program
*p
,
425 idx
= _mesa_add_state_reference( p
->program
->Parameters
, tokens
);
426 return make_ureg(PROGRAM_STATE_VAR
, idx
);
430 #define register_param1(p,s0) register_param6(p,s0,0,0,0,0,0)
431 #define register_param2(p,s0,s1) register_param6(p,s0,s1,0,0,0,0)
432 #define register_param3(p,s0,s1,s2) register_param6(p,s0,s1,s2,0,0,0)
433 #define register_param4(p,s0,s1,s2,s3) register_param6(p,s0,s1,s2,s3,0,0)
436 static void register_matrix_param6( struct tnl_program
*p
,
443 struct ureg
*matrix
)
447 /* This is a bit sad as the support is there to pull the whole
448 * matrix out in one go:
450 for (i
= 0; i
<= s4
- s3
; i
++)
451 matrix
[i
] = register_param6( p
, s0
, s1
, s2
, i
, i
, s5
);
455 static void emit_arg( struct vp_src_register
*src
,
458 src
->File
= reg
.file
;
459 src
->Index
= reg
.idx
;
460 src
->Swizzle
= reg
.swz
;
461 src
->Negate
= reg
.negate
;
466 static void emit_dst( struct vp_dst_register
*dst
,
467 struct ureg reg
, GLuint mask
)
469 dst
->File
= reg
.file
;
470 dst
->Index
= reg
.idx
;
471 /* allow zero as a shorthand for xyzw */
472 dst
->WriteMask
= mask
? mask
: WRITEMASK_XYZW
;
476 static void debug_insn( struct vp_instruction
*inst
, const char *fn
,
480 static const char *last_fn
;
484 _mesa_printf("%s:\n", fn
);
487 _mesa_printf("%d:\t", line
);
488 _mesa_debug_vp_inst(1, inst
);
493 static void emit_op3fn(struct tnl_program
*p
,
503 GLuint nr
= p
->program
->Base
.NumInstructions
++;
504 struct vp_instruction
*inst
= &p
->program
->Instructions
[nr
];
506 if (p
->program
->Base
.NumInstructions
> MAX_INSN
) {
507 _mesa_problem(0, "Out of instructions in emit_op3fn\n");
515 emit_arg( &inst
->SrcReg
[0], src0
);
516 emit_arg( &inst
->SrcReg
[1], src1
);
517 emit_arg( &inst
->SrcReg
[2], src2
);
519 emit_dst( &inst
->DstReg
, dest
, mask
);
521 debug_insn(inst
, fn
, line
);
526 #define emit_op3(p, op, dst, mask, src0, src1, src2) \
527 emit_op3fn(p, op, dst, mask, src0, src1, src2, __FUNCTION__, __LINE__)
529 #define emit_op2(p, op, dst, mask, src0, src1) \
530 emit_op3fn(p, op, dst, mask, src0, src1, undef, __FUNCTION__, __LINE__)
532 #define emit_op1(p, op, dst, mask, src0) \
533 emit_op3fn(p, op, dst, mask, src0, undef, undef, __FUNCTION__, __LINE__)
536 static struct ureg
make_temp( struct tnl_program
*p
, struct ureg reg
)
538 if (reg
.file
== PROGRAM_TEMPORARY
&&
539 !(p
->temp_reserved
& (1<<reg
.idx
)))
542 struct ureg temp
= get_temp(p
);
543 emit_op1(p
, VP_OPCODE_MOV
, temp
, 0, reg
);
549 /* Currently no tracking performed of input/output/register size or
550 * active elements. Could be used to reduce these operations, as
551 * could the matrix type.
553 static void emit_matrix_transform_vec4( struct tnl_program
*p
,
555 const struct ureg
*mat
,
558 emit_op2(p
, VP_OPCODE_DP4
, dest
, WRITEMASK_X
, src
, mat
[0]);
559 emit_op2(p
, VP_OPCODE_DP4
, dest
, WRITEMASK_Y
, src
, mat
[1]);
560 emit_op2(p
, VP_OPCODE_DP4
, dest
, WRITEMASK_Z
, src
, mat
[2]);
561 emit_op2(p
, VP_OPCODE_DP4
, dest
, WRITEMASK_W
, src
, mat
[3]);
564 /* This version is much easier to implement if writemasks are not
565 * supported natively on the target or (like SSE), the target doesn't
566 * have a clean/obvious dotproduct implementation.
568 static void emit_transpose_matrix_transform_vec4( struct tnl_program
*p
,
570 const struct ureg
*mat
,
575 if (dest
.file
!= PROGRAM_TEMPORARY
)
580 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, swizzle1(src
,X
), mat
[0]);
581 emit_op3(p
, VP_OPCODE_MAD
, tmp
, 0, swizzle1(src
,Y
), mat
[1], tmp
);
582 emit_op3(p
, VP_OPCODE_MAD
, tmp
, 0, swizzle1(src
,Z
), mat
[2], tmp
);
583 emit_op3(p
, VP_OPCODE_MAD
, dest
, 0, swizzle1(src
,W
), mat
[3], tmp
);
585 if (dest
.file
!= PROGRAM_TEMPORARY
)
586 release_temp(p
, tmp
);
589 static void emit_matrix_transform_vec3( struct tnl_program
*p
,
591 const struct ureg
*mat
,
594 emit_op2(p
, VP_OPCODE_DP3
, dest
, WRITEMASK_X
, src
, mat
[0]);
595 emit_op2(p
, VP_OPCODE_DP3
, dest
, WRITEMASK_Y
, src
, mat
[1]);
596 emit_op2(p
, VP_OPCODE_DP3
, dest
, WRITEMASK_Z
, src
, mat
[2]);
600 static void emit_normalize_vec3( struct tnl_program
*p
,
604 struct ureg tmp
= get_temp(p
);
605 emit_op2(p
, VP_OPCODE_DP3
, tmp
, 0, src
, src
);
606 emit_op1(p
, VP_OPCODE_RSQ
, tmp
, 0, tmp
);
607 emit_op2(p
, VP_OPCODE_MUL
, dest
, 0, src
, tmp
);
608 release_temp(p
, tmp
);
611 static void emit_passthrough( struct tnl_program
*p
,
615 struct ureg out
= register_output(p
, output
);
616 emit_op1(p
, VP_OPCODE_MOV
, out
, 0, register_input(p
, input
));
619 static struct ureg
get_eye_position( struct tnl_program
*p
)
621 if (is_undef(p
->eye_position
)) {
622 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
623 struct ureg modelview
[4];
625 p
->eye_position
= reserve_temp(p
);
628 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 0, 3,
629 STATE_MATRIX
, modelview
);
631 emit_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
634 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 0, 3,
635 STATE_MATRIX_TRANSPOSE
, modelview
);
637 emit_transpose_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
641 return p
->eye_position
;
645 static struct ureg
get_eye_position_normalized( struct tnl_program
*p
)
647 if (is_undef(p
->eye_position_normalized
)) {
648 struct ureg eye
= get_eye_position(p
);
649 p
->eye_position_normalized
= reserve_temp(p
);
650 emit_normalize_vec3(p
, p
->eye_position_normalized
, eye
);
653 return p
->eye_position_normalized
;
657 static struct ureg
get_eye_normal( struct tnl_program
*p
)
659 if (is_undef(p
->eye_normal
)) {
660 struct ureg normal
= register_input(p
, VERT_ATTRIB_NORMAL
);
661 struct ureg mvinv
[3];
663 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 0, 2,
664 STATE_MATRIX_INVTRANS
, mvinv
);
666 p
->eye_normal
= reserve_temp(p
);
668 /* Transform to eye space:
670 emit_matrix_transform_vec3( p
, p
->eye_normal
, mvinv
, normal
);
672 /* Normalize/Rescale:
674 if (p
->state
->normalize
) {
675 emit_normalize_vec3( p
, p
->eye_normal
, p
->eye_normal
);
677 else if (p
->state
->rescale_normals
) {
678 struct ureg rescale
= register_param2(p
, STATE_INTERNAL
,
681 emit_op2( p
, VP_OPCODE_MUL
, p
->eye_normal
, 0, normal
,
682 swizzle1(rescale
, X
));
686 return p
->eye_normal
;
691 static void build_hpos( struct tnl_program
*p
)
693 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
694 struct ureg hpos
= register_output( p
, VERT_RESULT_HPOS
);
698 register_matrix_param6( p
, STATE_MATRIX
, STATE_MVP
, 0, 0, 3,
700 emit_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
703 register_matrix_param6( p
, STATE_MATRIX
, STATE_MVP
, 0, 0, 3,
704 STATE_MATRIX_TRANSPOSE
, mvp
);
705 emit_transpose_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
710 static GLuint
material_attrib( GLuint side
, GLuint property
)
712 return ((property
- STATE_AMBIENT
) * 2 +
716 static void set_material_flags( struct tnl_program
*p
)
718 p
->color_materials
= 0;
721 if (p
->state
->light_color_material
) {
723 p
->color_materials
= p
->state
->light_color_material_mask
;
726 p
->materials
|= p
->state
->light_material_mask
;
730 static struct ureg
get_material( struct tnl_program
*p
, GLuint side
,
733 GLuint attrib
= material_attrib(side
, property
);
735 if (p
->color_materials
& (1<<attrib
))
736 return register_input(p
, VERT_ATTRIB_COLOR0
);
737 else if (p
->materials
& (1<<attrib
))
738 return register_input( p
, attrib
+ _TNL_ATTRIB_MAT_FRONT_AMBIENT
);
740 return register_param3( p
, STATE_MATERIAL
, side
, property
);
743 #define SCENE_COLOR_BITS(side) (( MAT_BIT_FRONT_EMISSION | \
744 MAT_BIT_FRONT_AMBIENT | \
745 MAT_BIT_FRONT_DIFFUSE) << (side))
747 /* Either return a precalculated constant value or emit code to
748 * calculate these values dynamically in the case where material calls
749 * are present between begin/end pairs.
751 * Probably want to shift this to the program compilation phase - if
752 * we always emitted the calculation here, a smart compiler could
753 * detect that it was constant (given a certain set of inputs), and
754 * lift it out of the main loop. That way the programs created here
755 * would be independent of the vertex_buffer details.
757 static struct ureg
get_scenecolor( struct tnl_program
*p
, GLuint side
)
759 if (p
->materials
& SCENE_COLOR_BITS(side
)) {
760 struct ureg lm_ambient
= register_param1(p
, STATE_LIGHTMODEL_AMBIENT
);
761 struct ureg material_emission
= get_material(p
, side
, STATE_EMISSION
);
762 struct ureg material_ambient
= get_material(p
, side
, STATE_AMBIENT
);
763 struct ureg material_diffuse
= get_material(p
, side
, STATE_DIFFUSE
);
764 struct ureg tmp
= make_temp(p
, material_diffuse
);
765 emit_op3(p
, VP_OPCODE_MAD
, tmp
, WRITEMASK_XYZ
, lm_ambient
,
766 material_ambient
, material_emission
);
770 return register_param2( p
, STATE_LIGHTMODEL_SCENECOLOR
, side
);
774 static struct ureg
get_lightprod( struct tnl_program
*p
, GLuint light
,
775 GLuint side
, GLuint property
)
777 GLuint attrib
= material_attrib(side
, property
);
778 if (p
->materials
& (1<<attrib
)) {
779 struct ureg light_value
=
780 register_param3(p
, STATE_LIGHT
, light
, property
);
781 struct ureg material_value
= get_material(p
, side
, property
);
782 struct ureg tmp
= get_temp(p
);
783 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, light_value
, material_value
);
787 return register_param4(p
, STATE_LIGHTPROD
, light
, side
, property
);
790 static struct ureg
calculate_light_attenuation( struct tnl_program
*p
,
795 struct ureg attenuation
= register_param3(p
, STATE_LIGHT
, i
,
797 struct ureg att
= get_temp(p
);
799 /* Calculate spot attenuation:
801 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
802 struct ureg spot_dir
= register_param3(p
, STATE_LIGHT
, i
,
803 STATE_SPOT_DIRECTION
);
804 struct ureg spot
= get_temp(p
);
805 struct ureg slt
= get_temp(p
);
807 emit_normalize_vec3( p
, spot
, spot_dir
); /* XXX: precompute! */
808 emit_op2(p
, VP_OPCODE_DP3
, spot
, 0, negate(VPpli
), spot_dir
);
809 emit_op2(p
, VP_OPCODE_SLT
, slt
, 0, swizzle1(spot_dir
,W
), spot
);
810 emit_op2(p
, VP_OPCODE_POW
, spot
, 0, spot
, swizzle1(attenuation
, W
));
811 emit_op2(p
, VP_OPCODE_MUL
, att
, 0, slt
, spot
);
813 release_temp(p
, spot
);
814 release_temp(p
, slt
);
817 /* Calculate distance attenuation:
819 if (p
->state
->unit
[i
].light_attenuated
) {
822 emit_op1(p
, VP_OPCODE_RCP
, dist
, WRITEMASK_YZ
, dist
);
824 emit_op2(p
, VP_OPCODE_MUL
, dist
, WRITEMASK_XZ
, dist
, swizzle1(dist
,Y
));
826 emit_op2(p
, VP_OPCODE_DP3
, dist
, 0, attenuation
, dist
);
828 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
830 emit_op1(p
, VP_OPCODE_RCP
, dist
, 0, dist
);
831 /* spot-atten * dist-atten */
832 emit_op2(p
, VP_OPCODE_MUL
, att
, 0, dist
, att
);
835 emit_op1(p
, VP_OPCODE_RCP
, att
, 0, dist
);
846 /* Need to add some addtional parameters to allow lighting in object
847 * space - STATE_SPOT_DIRECTION and STATE_HALF implicitly assume eye
850 static void build_lighting( struct tnl_program
*p
)
852 const GLboolean twoside
= p
->state
->light_twoside
;
853 const GLboolean separate
= p
->state
->separate_specular
;
854 GLuint nr_lights
= 0, count
= 0;
855 struct ureg normal
= get_eye_normal(p
);
856 struct ureg lit
= get_temp(p
);
857 struct ureg dots
= get_temp(p
);
858 struct ureg _col0
= undef
, _col1
= undef
;
859 struct ureg _bfc0
= undef
, _bfc1
= undef
;
862 for (i
= 0; i
< MAX_LIGHTS
; i
++)
863 if (p
->state
->unit
[i
].light_enabled
)
866 set_material_flags(p
);
869 struct ureg shininess
= get_material(p
, 0, STATE_SHININESS
);
870 emit_op1(p
, VP_OPCODE_MOV
, dots
, WRITEMASK_W
, swizzle1(shininess
,X
));
871 release_temp(p
, shininess
);
873 _col0
= make_temp(p
, get_scenecolor(p
, 0));
875 _col1
= make_temp(p
, get_identity_param(p
));
882 struct ureg shininess
= get_material(p
, 1, STATE_SHININESS
);
883 emit_op1(p
, VP_OPCODE_MOV
, dots
, WRITEMASK_Z
,
884 negate(swizzle1(shininess
,X
)));
885 release_temp(p
, shininess
);
887 _bfc0
= make_temp(p
, get_scenecolor(p
, 1));
889 _bfc1
= make_temp(p
, get_identity_param(p
));
895 /* If no lights, still need to emit the scenecolor.
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
);
917 if (nr_lights
== 0) {
923 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
924 if (p
->state
->unit
[i
].light_enabled
) {
925 struct ureg half
= undef
;
926 struct ureg att
= undef
, VPpli
= undef
;
930 if (p
->state
->unit
[i
].light_eyepos3_is_zero
) {
931 /* Can used precomputed constants in this case.
932 * Attenuation never applies to infinite lights.
934 VPpli
= register_param3(p
, STATE_LIGHT
, i
,
935 STATE_POSITION_NORMALIZED
);
936 half
= register_param3(p
, STATE_LIGHT
, i
, STATE_HALF
);
939 struct ureg Ppli
= register_param3(p
, STATE_LIGHT
, i
,
941 struct ureg V
= get_eye_position(p
);
942 struct ureg dist
= get_temp(p
);
947 /* Calulate VPpli vector
949 emit_op2(p
, VP_OPCODE_SUB
, VPpli
, 0, Ppli
, V
);
951 /* Normalize VPpli. The dist value also used in
954 emit_op2(p
, VP_OPCODE_DP3
, dist
, 0, VPpli
, VPpli
);
955 emit_op1(p
, VP_OPCODE_RSQ
, dist
, 0, dist
);
956 emit_op2(p
, VP_OPCODE_MUL
, VPpli
, 0, VPpli
, dist
);
959 /* Calculate attenuation:
961 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
||
962 p
->state
->unit
[i
].light_attenuated
) {
963 att
= calculate_light_attenuation(p
, i
, VPpli
, dist
);
967 /* Calculate viewer direction, or use infinite viewer:
969 if (p
->state
->light_local_viewer
) {
970 struct ureg eye_hat
= get_eye_position_normalized(p
);
971 emit_op2(p
, VP_OPCODE_SUB
, half
, 0, VPpli
, eye_hat
);
974 struct ureg z_dir
= swizzle(get_identity_param(p
),X
,Y
,W
,Z
);
975 emit_op2(p
, VP_OPCODE_ADD
, half
, 0, VPpli
, z_dir
);
978 emit_normalize_vec3(p
, half
, half
);
980 release_temp(p
, dist
);
983 /* Calculate dot products:
985 emit_op2(p
, VP_OPCODE_DP3
, dots
, WRITEMASK_X
, normal
, VPpli
);
986 emit_op2(p
, VP_OPCODE_DP3
, dots
, WRITEMASK_Y
, normal
, half
);
989 /* Front face lighting:
992 struct ureg ambient
= get_lightprod(p
, i
, 0, STATE_AMBIENT
);
993 struct ureg diffuse
= get_lightprod(p
, i
, 0, STATE_DIFFUSE
);
994 struct ureg specular
= get_lightprod(p
, i
, 0, STATE_SPECULAR
);
995 struct ureg res0
, res1
;
998 emit_op1(p
, VP_OPCODE_LIT
, lit
, 0, dots
);
1001 emit_op2(p
, VP_OPCODE_MUL
, lit
, 0, lit
, att
);
1004 if (count
== nr_lights
) {
1006 mask0
= WRITEMASK_XYZ
;
1007 mask1
= WRITEMASK_XYZ
;
1008 res0
= register_output( p
, VERT_RESULT_COL0
);
1009 res1
= register_output( p
, VERT_RESULT_COL1
);
1013 mask1
= WRITEMASK_XYZ
;
1015 res1
= register_output( p
, VERT_RESULT_COL0
);
1024 emit_op3(p
, VP_OPCODE_MAD
, _col0
, 0, swizzle1(lit
,X
), ambient
, _col0
);
1025 emit_op3(p
, VP_OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _col0
);
1026 emit_op3(p
, VP_OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _col1
);
1028 release_temp(p
, ambient
);
1029 release_temp(p
, diffuse
);
1030 release_temp(p
, specular
);
1033 /* Back face lighting:
1036 struct ureg ambient
= get_lightprod(p
, i
, 1, STATE_AMBIENT
);
1037 struct ureg diffuse
= get_lightprod(p
, i
, 1, STATE_DIFFUSE
);
1038 struct ureg specular
= get_lightprod(p
, i
, 1, STATE_SPECULAR
);
1039 struct ureg res0
, res1
;
1040 GLuint mask0
, mask1
;
1042 emit_op1(p
, VP_OPCODE_LIT
, lit
, 0, negate(swizzle(dots
,X
,Y
,W
,Z
)));
1045 emit_op2(p
, VP_OPCODE_MUL
, lit
, 0, lit
, att
);
1047 if (count
== nr_lights
) {
1049 mask0
= WRITEMASK_XYZ
;
1050 mask1
= WRITEMASK_XYZ
;
1051 res0
= register_output( p
, VERT_RESULT_BFC0
);
1052 res1
= register_output( p
, VERT_RESULT_BFC1
);
1056 mask1
= WRITEMASK_XYZ
;
1058 res1
= register_output( p
, VERT_RESULT_BFC0
);
1067 emit_op3(p
, VP_OPCODE_MAD
, _bfc0
, 0, swizzle1(lit
,X
), ambient
, _bfc0
);
1068 emit_op3(p
, VP_OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _bfc0
);
1069 emit_op3(p
, VP_OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _bfc1
);
1071 release_temp(p
, ambient
);
1072 release_temp(p
, diffuse
);
1073 release_temp(p
, specular
);
1076 release_temp(p
, half
);
1077 release_temp(p
, VPpli
);
1078 release_temp(p
, att
);
1086 static void build_fog( struct tnl_program
*p
)
1088 struct ureg fog
= register_output(p
, VERT_RESULT_FOGC
);
1091 if (p
->state
->fog_source_is_depth
) {
1092 input
= swizzle1(get_eye_position(p
), Z
);
1095 input
= swizzle1(register_input(p
, VERT_ATTRIB_FOG
), X
);
1098 if (p
->state
->tnl_do_vertex_fog
) {
1099 struct ureg params
= register_param1(p
, STATE_FOG_PARAMS
);
1100 struct ureg tmp
= get_temp(p
);
1102 switch (p
->state
->fog_mode
) {
1104 struct ureg id
= get_identity_param(p
);
1105 emit_op2(p
, VP_OPCODE_SUB
, tmp
, 0, swizzle1(params
,Z
), input
);
1106 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, tmp
, swizzle1(params
,W
));
1107 emit_op2(p
, VP_OPCODE_MAX
, tmp
, 0, tmp
, swizzle1(id
,X
)); /* saturate */
1108 emit_op2(p
, VP_OPCODE_MIN
, fog
, WRITEMASK_X
, tmp
, swizzle1(id
,W
));
1112 emit_op1(p
, VP_OPCODE_ABS
, tmp
, 0, input
);
1113 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, tmp
, swizzle1(params
,X
));
1114 emit_op2(p
, VP_OPCODE_POW
, fog
, WRITEMASK_X
,
1115 register_const1f(p
, M_E
), negate(tmp
));
1118 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, input
, swizzle1(params
,X
));
1119 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, tmp
, tmp
);
1120 emit_op2(p
, VP_OPCODE_POW
, fog
, WRITEMASK_X
,
1121 register_const1f(p
, M_E
), negate(tmp
));
1125 release_temp(p
, tmp
);
1128 /* results = incoming fog coords (compute fog per-fragment later)
1130 * KW: Is it really necessary to do anything in this case?
1132 emit_op1(p
, VP_OPCODE_MOV
, fog
, WRITEMASK_X
, input
);
1136 static void build_reflect_texgen( struct tnl_program
*p
,
1140 struct ureg normal
= get_eye_normal(p
);
1141 struct ureg eye_hat
= get_eye_position_normalized(p
);
1142 struct ureg tmp
= get_temp(p
);
1145 emit_op2(p
, VP_OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1147 emit_op2(p
, VP_OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1149 emit_op3(p
, VP_OPCODE_MAD
, dest
, writemask
, negate(tmp
), normal
, eye_hat
);
1152 static void build_sphere_texgen( struct tnl_program
*p
,
1156 struct ureg normal
= get_eye_normal(p
);
1157 struct ureg eye_hat
= get_eye_position_normalized(p
);
1158 struct ureg tmp
= get_temp(p
);
1159 struct ureg half
= register_scalar_const(p
, .5);
1160 struct ureg r
= get_temp(p
);
1161 struct ureg inv_m
= get_temp(p
);
1162 struct ureg id
= get_identity_param(p
);
1164 /* Could share the above calculations, but it would be
1165 * a fairly odd state for someone to set (both sphere and
1166 * reflection active for different texture coordinate
1167 * components. Of course - if two texture units enable
1168 * reflect and/or sphere, things start to tilt in favour
1169 * of seperating this out:
1173 emit_op2(p
, VP_OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1175 emit_op2(p
, VP_OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1177 emit_op3(p
, VP_OPCODE_MAD
, r
, 0, negate(tmp
), normal
, eye_hat
);
1179 emit_op2(p
, VP_OPCODE_ADD
, tmp
, 0, r
, swizzle(id
,X
,Y
,W
,Z
));
1180 /* rx^2 + ry^2 + (rz+1)^2 */
1181 emit_op2(p
, VP_OPCODE_DP3
, tmp
, 0, tmp
, tmp
);
1183 emit_op1(p
, VP_OPCODE_RSQ
, tmp
, 0, tmp
);
1185 emit_op2(p
, VP_OPCODE_MUL
, inv_m
, 0, tmp
, half
);
1187 emit_op3(p
, VP_OPCODE_MAD
, dest
, writemask
, r
, inv_m
, half
);
1189 release_temp(p
, tmp
);
1191 release_temp(p
, inv_m
);
1195 static void build_texture_transform( struct tnl_program
*p
)
1199 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
1200 GLuint texmat_enabled
= p
->state
->unit
[i
].texmat_enabled
;
1202 if (p
->state
->unit
[i
].texgen_enabled
|| texmat_enabled
) {
1203 struct ureg out
= register_output(p
, VERT_RESULT_TEX0
+ i
);
1204 struct ureg out_texgen
= undef
;
1206 if (p
->state
->unit
[i
].texgen_enabled
) {
1207 GLuint copy_mask
= 0;
1208 GLuint sphere_mask
= 0;
1209 GLuint reflect_mask
= 0;
1210 GLuint normal_mask
= 0;
1214 out_texgen
= get_temp(p
);
1218 modes
[0] = p
->state
->unit
[i
].texgen_mode0
;
1219 modes
[1] = p
->state
->unit
[i
].texgen_mode1
;
1220 modes
[2] = p
->state
->unit
[i
].texgen_mode2
;
1221 modes
[3] = p
->state
->unit
[i
].texgen_mode3
;
1223 for (j
= 0; j
< 4; j
++) {
1225 case TXG_OBJ_LINEAR
: {
1226 struct ureg obj
= register_input(p
, VERT_ATTRIB_POS
);
1228 register_param3(p
, STATE_TEXGEN
, i
,
1229 STATE_TEXGEN_OBJECT_S
+ j
);
1231 emit_op2(p
, VP_OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1235 case TXG_EYE_LINEAR
: {
1236 struct ureg eye
= get_eye_position(p
);
1238 register_param3(p
, STATE_TEXGEN
, i
,
1239 STATE_TEXGEN_EYE_S
+ j
);
1241 emit_op2(p
, VP_OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1245 case TXG_SPHERE_MAP
:
1246 sphere_mask
|= WRITEMASK_X
<< j
;
1248 case TXG_REFLECTION_MAP
:
1249 reflect_mask
|= WRITEMASK_X
<< j
;
1251 case TXG_NORMAL_MAP
:
1252 normal_mask
|= WRITEMASK_X
<< j
;
1255 copy_mask
|= WRITEMASK_X
<< j
;
1262 build_sphere_texgen(p
, out_texgen
, sphere_mask
);
1266 build_reflect_texgen(p
, out_texgen
, reflect_mask
);
1270 struct ureg normal
= get_eye_normal(p
);
1271 emit_op1(p
, VP_OPCODE_MOV
, out_texgen
, normal_mask
, normal
);
1275 struct ureg in
= register_input(p
, VERT_ATTRIB_TEX0
+i
);
1276 emit_op1(p
, VP_OPCODE_MOV
, out_texgen
, copy_mask
, in
);
1280 if (texmat_enabled
) {
1281 struct ureg texmat
[4];
1282 struct ureg in
= (!is_undef(out_texgen
) ?
1284 register_input(p
, VERT_ATTRIB_TEX0
+i
));
1286 register_matrix_param6( p
, STATE_MATRIX
, STATE_TEXTURE
, i
,
1287 0, 3, STATE_MATRIX
, texmat
);
1288 emit_matrix_transform_vec4( p
, out
, texmat
, in
);
1291 register_matrix_param6( p
, STATE_MATRIX
, STATE_TEXTURE
, i
,
1292 0, 3, STATE_MATRIX_TRANSPOSE
, texmat
);
1293 emit_transpose_matrix_transform_vec4( p
, out
, texmat
, in
);
1299 else if (p
->state
->unit
[i
].texunit_really_enabled
) {
1300 /* KW: _ReallyEnabled isn't sufficient? Need to know whether
1301 * this texture unit is referenced by the fragment shader.
1303 emit_passthrough(p
, VERT_ATTRIB_TEX0
+i
, VERT_RESULT_TEX0
+i
);
1309 /* Seems like it could be tighter:
1311 static void build_pointsize( struct tnl_program
*p
)
1313 struct ureg eye
= get_eye_position(p
);
1314 struct ureg state_size
= register_param1(p
, STATE_POINT_SIZE
);
1315 struct ureg state_attenuation
= register_param1(p
, STATE_POINT_ATTENUATION
);
1316 struct ureg out
= register_output(p
, VERT_RESULT_PSIZ
);
1317 struct ureg ut
= get_temp(p
);
1319 /* 1, -Z, Z * Z, 1 */
1320 emit_op1(p
, VP_OPCODE_MOV
, ut
, 0, swizzle1(get_identity_param(p
), W
));
1321 emit_op2(p
, VP_OPCODE_MUL
, ut
, WRITEMASK_YZ
, ut
, negate(swizzle1(eye
, Z
)));
1322 emit_op2(p
, VP_OPCODE_MUL
, ut
, WRITEMASK_Z
, ut
, negate(swizzle1(eye
, Z
)));
1325 /* p1 + p2 * dist + p3 * dist * dist, 0 */
1326 emit_op2(p
, VP_OPCODE_DP3
, ut
, 0, ut
, state_attenuation
);
1329 emit_op1(p
, VP_OPCODE_RCP
, ut
, 0, ut
);
1331 /* out = pointSize / factor */
1332 emit_op2(p
, VP_OPCODE_MUL
, out
, WRITEMASK_X
, ut
, state_size
);
1334 release_temp(p
, ut
);
1337 static void build_tnl_program( struct tnl_program
*p
)
1338 { /* Emit the program, starting with modelviewproject:
1342 /* Lighting calculations:
1344 if (p
->state
->light_global_enabled
)
1347 emit_passthrough(p
, VERT_ATTRIB_COLOR0
, VERT_RESULT_COL0
);
1349 if (p
->state
->fog_enabled
)
1352 if (p
->state
->texture_enabled_global
)
1353 build_texture_transform(p
);
1355 if (p
->state
->point_attenuated
)
1360 emit_op1(p
, VP_OPCODE_END
, undef
, 0, undef
);
1365 _mesa_printf ("\n");
1370 void create_new_program( const struct state_key
*key
,
1371 struct vertex_program
*program
,
1374 struct tnl_program p
;
1376 _mesa_memset(&p
, 0, sizeof(p
));
1378 p
.program
= program
;
1379 p
.eye_position
= undef
;
1380 p
.eye_position_normalized
= undef
;
1381 p
.eye_normal
= undef
;
1385 if (max_temps
>= sizeof(int) * 8)
1386 p
.temp_reserved
= 0;
1388 p
.temp_reserved
= ~((1<<max_temps
)-1);
1390 p
.program
->Instructions
= MALLOC(sizeof(struct vp_instruction
) * MAX_INSN
);
1391 p
.program
->Base
.String
= 0;
1392 p
.program
->Base
.NumInstructions
=
1393 p
.program
->Base
.NumTemporaries
=
1394 p
.program
->Base
.NumParameters
=
1395 p
.program
->Base
.NumAttributes
= p
.program
->Base
.NumAddressRegs
= 0;
1396 p
.program
->Parameters
= _mesa_new_parameter_list();
1397 p
.program
->InputsRead
= 0;
1398 p
.program
->OutputsWritten
= 0;
1400 build_tnl_program( &p
);
1403 static void *search_cache( struct tnl_cache
*cache
,
1408 struct tnl_cache
*c
;
1410 for (c
= cache
; c
; c
= c
->next
) {
1411 if (c
->hash
== hash
&& memcmp(c
->key
, key
, keysize
) == 0)
1418 static void cache_item( struct tnl_cache
**cache
,
1423 struct tnl_cache
*c
= MALLOC(sizeof(*c
));
1431 static GLuint
hash_key( struct state_key
*key
)
1433 GLuint
*ikey
= (GLuint
*)key
;
1436 /* I'm sure this can be improved on, but speed is important:
1438 for (i
= 0; i
< sizeof(*key
)/sizeof(GLuint
); i
++)
1444 void _tnl_UpdateFixedFunctionProgram( GLcontext
*ctx
)
1446 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1447 struct state_key
*key
;
1450 if (ctx
->VertexProgram
._Enabled
)
1453 /* Grab all the relevent state and put it in a single structure:
1455 key
= make_state_key(ctx
);
1456 hash
= hash_key(key
);
1458 /* Look for an already-prepared program for this state:
1460 ctx
->_TnlProgram
= (struct vertex_program
*)
1461 search_cache( tnl
->vp_cache
, hash
, key
, sizeof(*key
) );
1463 /* OK, we'll have to build a new one:
1465 if (!ctx
->_TnlProgram
) {
1467 _mesa_printf("Build new TNL program\n");
1469 ctx
->_TnlProgram
= (struct vertex_program
*)
1470 ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
1472 create_new_program( key
, ctx
->_TnlProgram
,
1473 ctx
->Const
.MaxVertexProgramTemps
);
1475 cache_item(&tnl
->vp_cache
, hash
, key
, ctx
->_TnlProgram
);
1479 _mesa_printf("Found existing TNL program for key %x\n", hash
);
1482 /* Need a BindProgram callback for the driver?
1487 void _tnl_ProgramCacheDestroy( GLcontext
*ctx
)
1489 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1490 struct tnl_cache
*a
, *tmp
;
1492 for (a
= tnl
->vp_cache
; a
; a
= tmp
) {