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
;
139 for (i
= _TNL_ATTRIB_MAT_FRONT_AMBIENT
; i
< _TNL_ATTRIB_INDEX
; i
++)
140 if (VB
->AttribPtr
[i
]->stride
)
141 key
->light_material_mask
|= 1<<(i
-_TNL_ATTRIB_MAT_FRONT_AMBIENT
);
143 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
144 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
146 if (light
->Enabled
) {
147 key
->unit
[i
].light_enabled
= 1;
149 if (light
->EyePosition
[3] == 0.0)
150 key
->unit
[i
].light_eyepos3_is_zero
= 1;
152 if (light
->SpotCutoff
== 180.0)
153 key
->unit
[i
].light_spotcutoff_is_180
= 1;
155 if (light
->ConstantAttenuation
!= 1.0 ||
156 light
->LinearAttenuation
!= 0.0 ||
157 light
->QuadraticAttenuation
!= 0.0)
158 key
->unit
[i
].light_attenuated
= 1;
163 if (ctx
->Transform
.Normalize
)
166 if (ctx
->Transform
.RescaleNormals
)
167 key
->rescale_normals
= 1;
169 if (ctx
->Fog
.Enabled
)
170 key
->fog_enabled
= 1;
172 if (key
->fog_enabled
) {
173 if (ctx
->Fog
.FogCoordinateSource
== GL_FRAGMENT_DEPTH_EXT
)
174 key
->fog_source_is_depth
= 1;
176 if (tnl
->_DoVertexFog
)
177 key
->tnl_do_vertex_fog
= 1;
179 key
->fog_mode
= translate_fog_mode(ctx
->Fog
.Mode
);
182 if (ctx
->Point
._Attenuated
)
183 key
->point_attenuated
= 1;
185 if (ctx
->Texture
._TexGenEnabled
||
186 ctx
->Texture
._TexMatEnabled
||
187 ctx
->Texture
._EnabledUnits
)
188 key
->texture_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:
226 #define DISASSEM (MESA_VERBOSE&VERBOSE_DISASSEM)
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
250 GLint idx
:8; /* relative addressing may be negative */
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
, GLint 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
= _mesa_ffs( ~p
->temp_in_use
);
331 _mesa_problem(NULL
, "%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
);
523 #define emit_op3(p, op, dst, mask, src0, src1, src2) \
524 emit_op3fn(p, op, dst, mask, src0, src1, src2, __FUNCTION__, __LINE__)
526 #define emit_op2(p, op, dst, mask, src0, src1) \
527 emit_op3fn(p, op, dst, mask, src0, src1, undef, __FUNCTION__, __LINE__)
529 #define emit_op1(p, op, dst, mask, src0) \
530 emit_op3fn(p, op, dst, mask, src0, undef, undef, __FUNCTION__, __LINE__)
533 static struct ureg
make_temp( struct tnl_program
*p
, struct ureg reg
)
535 if (reg
.file
== PROGRAM_TEMPORARY
&&
536 !(p
->temp_reserved
& (1<<reg
.idx
)))
539 struct ureg temp
= get_temp(p
);
540 emit_op1(p
, VP_OPCODE_MOV
, temp
, 0, reg
);
546 /* Currently no tracking performed of input/output/register size or
547 * active elements. Could be used to reduce these operations, as
548 * could the matrix type.
550 static void emit_matrix_transform_vec4( struct tnl_program
*p
,
552 const struct ureg
*mat
,
555 emit_op2(p
, VP_OPCODE_DP4
, dest
, WRITEMASK_X
, src
, mat
[0]);
556 emit_op2(p
, VP_OPCODE_DP4
, dest
, WRITEMASK_Y
, src
, mat
[1]);
557 emit_op2(p
, VP_OPCODE_DP4
, dest
, WRITEMASK_Z
, src
, mat
[2]);
558 emit_op2(p
, VP_OPCODE_DP4
, dest
, WRITEMASK_W
, src
, mat
[3]);
561 /* This version is much easier to implement if writemasks are not
562 * supported natively on the target or (like SSE), the target doesn't
563 * have a clean/obvious dotproduct implementation.
565 static void emit_transpose_matrix_transform_vec4( struct tnl_program
*p
,
567 const struct ureg
*mat
,
572 if (dest
.file
!= PROGRAM_TEMPORARY
)
577 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, swizzle1(src
,X
), mat
[0]);
578 emit_op3(p
, VP_OPCODE_MAD
, tmp
, 0, swizzle1(src
,Y
), mat
[1], tmp
);
579 emit_op3(p
, VP_OPCODE_MAD
, tmp
, 0, swizzle1(src
,Z
), mat
[2], tmp
);
580 emit_op3(p
, VP_OPCODE_MAD
, dest
, 0, swizzle1(src
,W
), mat
[3], tmp
);
582 if (dest
.file
!= PROGRAM_TEMPORARY
)
583 release_temp(p
, tmp
);
586 static void emit_matrix_transform_vec3( struct tnl_program
*p
,
588 const struct ureg
*mat
,
591 emit_op2(p
, VP_OPCODE_DP3
, dest
, WRITEMASK_X
, src
, mat
[0]);
592 emit_op2(p
, VP_OPCODE_DP3
, dest
, WRITEMASK_Y
, src
, mat
[1]);
593 emit_op2(p
, VP_OPCODE_DP3
, dest
, WRITEMASK_Z
, src
, mat
[2]);
597 static void emit_normalize_vec3( struct tnl_program
*p
,
601 struct ureg tmp
= get_temp(p
);
602 emit_op2(p
, VP_OPCODE_DP3
, tmp
, 0, src
, src
);
603 emit_op1(p
, VP_OPCODE_RSQ
, tmp
, 0, tmp
);
604 emit_op2(p
, VP_OPCODE_MUL
, dest
, 0, src
, tmp
);
605 release_temp(p
, tmp
);
608 static void emit_passthrough( struct tnl_program
*p
,
612 struct ureg out
= register_output(p
, output
);
613 emit_op1(p
, VP_OPCODE_MOV
, out
, 0, register_input(p
, input
));
616 static struct ureg
get_eye_position( struct tnl_program
*p
)
618 if (is_undef(p
->eye_position
)) {
619 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
620 struct ureg modelview
[4];
622 p
->eye_position
= reserve_temp(p
);
625 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 0, 3,
626 STATE_MATRIX
, modelview
);
628 emit_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
631 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 0, 3,
632 STATE_MATRIX_TRANSPOSE
, modelview
);
634 emit_transpose_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
638 return p
->eye_position
;
642 static struct ureg
get_eye_position_normalized( struct tnl_program
*p
)
644 if (is_undef(p
->eye_position_normalized
)) {
645 struct ureg eye
= get_eye_position(p
);
646 p
->eye_position_normalized
= reserve_temp(p
);
647 emit_normalize_vec3(p
, p
->eye_position_normalized
, eye
);
650 return p
->eye_position_normalized
;
654 static struct ureg
get_eye_normal( struct tnl_program
*p
)
656 if (is_undef(p
->eye_normal
)) {
657 struct ureg normal
= register_input(p
, VERT_ATTRIB_NORMAL
);
658 struct ureg mvinv
[3];
660 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 0, 2,
661 STATE_MATRIX_INVTRANS
, mvinv
);
663 p
->eye_normal
= reserve_temp(p
);
665 /* Transform to eye space:
667 emit_matrix_transform_vec3( p
, p
->eye_normal
, mvinv
, normal
);
669 /* Normalize/Rescale:
671 if (p
->state
->normalize
) {
672 emit_normalize_vec3( p
, p
->eye_normal
, p
->eye_normal
);
674 else if (p
->state
->rescale_normals
) {
675 struct ureg rescale
= register_param2(p
, STATE_INTERNAL
,
678 emit_op2( p
, VP_OPCODE_MUL
, p
->eye_normal
, 0, normal
,
679 swizzle1(rescale
, X
));
683 return p
->eye_normal
;
688 static void build_hpos( struct tnl_program
*p
)
690 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
691 struct ureg hpos
= register_output( p
, VERT_RESULT_HPOS
);
695 register_matrix_param6( p
, STATE_MATRIX
, STATE_MVP
, 0, 0, 3,
697 emit_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
700 register_matrix_param6( p
, STATE_MATRIX
, STATE_MVP
, 0, 0, 3,
701 STATE_MATRIX_TRANSPOSE
, mvp
);
702 emit_transpose_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
707 static GLuint
material_attrib( GLuint side
, GLuint property
)
709 return ((property
- STATE_AMBIENT
) * 2 +
713 static void set_material_flags( struct tnl_program
*p
)
715 p
->color_materials
= 0;
718 if (p
->state
->light_color_material
) {
720 p
->color_materials
= p
->state
->light_color_material_mask
;
723 p
->materials
|= p
->state
->light_material_mask
;
727 static struct ureg
get_material( struct tnl_program
*p
, GLuint side
,
730 GLuint attrib
= material_attrib(side
, property
);
732 if (p
->color_materials
& (1<<attrib
))
733 return register_input(p
, VERT_ATTRIB_COLOR0
);
734 else if (p
->materials
& (1<<attrib
))
735 return register_input( p
, attrib
+ _TNL_ATTRIB_MAT_FRONT_AMBIENT
);
737 return register_param3( p
, STATE_MATERIAL
, side
, property
);
740 #define SCENE_COLOR_BITS(side) (( MAT_BIT_FRONT_EMISSION | \
741 MAT_BIT_FRONT_AMBIENT | \
742 MAT_BIT_FRONT_DIFFUSE) << (side))
744 /* Either return a precalculated constant value or emit code to
745 * calculate these values dynamically in the case where material calls
746 * are present between begin/end pairs.
748 * Probably want to shift this to the program compilation phase - if
749 * we always emitted the calculation here, a smart compiler could
750 * detect that it was constant (given a certain set of inputs), and
751 * lift it out of the main loop. That way the programs created here
752 * would be independent of the vertex_buffer details.
754 static struct ureg
get_scenecolor( struct tnl_program
*p
, GLuint side
)
756 if (p
->materials
& SCENE_COLOR_BITS(side
)) {
757 struct ureg lm_ambient
= register_param1(p
, STATE_LIGHTMODEL_AMBIENT
);
758 struct ureg material_emission
= get_material(p
, side
, STATE_EMISSION
);
759 struct ureg material_ambient
= get_material(p
, side
, STATE_AMBIENT
);
760 struct ureg material_diffuse
= get_material(p
, side
, STATE_DIFFUSE
);
761 struct ureg tmp
= make_temp(p
, material_diffuse
);
762 emit_op3(p
, VP_OPCODE_MAD
, tmp
, WRITEMASK_XYZ
, lm_ambient
,
763 material_ambient
, material_emission
);
767 return register_param2( p
, STATE_LIGHTMODEL_SCENECOLOR
, side
);
771 static struct ureg
get_lightprod( struct tnl_program
*p
, GLuint light
,
772 GLuint side
, GLuint property
)
774 GLuint attrib
= material_attrib(side
, property
);
775 if (p
->materials
& (1<<attrib
)) {
776 struct ureg light_value
=
777 register_param3(p
, STATE_LIGHT
, light
, property
);
778 struct ureg material_value
= get_material(p
, side
, property
);
779 struct ureg tmp
= get_temp(p
);
780 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, light_value
, material_value
);
784 return register_param4(p
, STATE_LIGHTPROD
, light
, side
, property
);
787 static struct ureg
calculate_light_attenuation( struct tnl_program
*p
,
792 struct ureg attenuation
= register_param3(p
, STATE_LIGHT
, i
,
794 struct ureg att
= get_temp(p
);
796 /* Calculate spot attenuation:
798 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
799 struct ureg spot_dir
= register_param3(p
, STATE_LIGHT
, i
,
800 STATE_SPOT_DIRECTION
);
801 struct ureg spot
= get_temp(p
);
802 struct ureg slt
= get_temp(p
);
804 emit_normalize_vec3( p
, spot
, spot_dir
); /* XXX: precompute! */
805 emit_op2(p
, VP_OPCODE_DP3
, spot
, 0, negate(VPpli
), spot
);
806 emit_op2(p
, VP_OPCODE_SLT
, slt
, 0, swizzle1(spot_dir
,W
), spot
);
807 emit_op2(p
, VP_OPCODE_POW
, spot
, 0, spot
, swizzle1(attenuation
, W
));
808 emit_op2(p
, VP_OPCODE_MUL
, att
, 0, slt
, spot
);
810 release_temp(p
, spot
);
811 release_temp(p
, slt
);
814 /* Calculate distance attenuation:
816 if (p
->state
->unit
[i
].light_attenuated
) {
819 emit_op1(p
, VP_OPCODE_RCP
, dist
, WRITEMASK_YZ
, dist
);
821 emit_op2(p
, VP_OPCODE_MUL
, dist
, WRITEMASK_XZ
, dist
, swizzle1(dist
,Y
));
823 emit_op2(p
, VP_OPCODE_DP3
, dist
, 0, attenuation
, dist
);
825 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
827 emit_op1(p
, VP_OPCODE_RCP
, dist
, 0, dist
);
828 /* spot-atten * dist-atten */
829 emit_op2(p
, VP_OPCODE_MUL
, att
, 0, dist
, att
);
832 emit_op1(p
, VP_OPCODE_RCP
, att
, 0, dist
);
843 /* Need to add some addtional parameters to allow lighting in object
844 * space - STATE_SPOT_DIRECTION and STATE_HALF implicitly assume eye
847 static void build_lighting( struct tnl_program
*p
)
849 const GLboolean twoside
= p
->state
->light_twoside
;
850 const GLboolean separate
= p
->state
->separate_specular
;
851 GLuint nr_lights
= 0, count
= 0;
852 struct ureg normal
= get_eye_normal(p
);
853 struct ureg lit
= get_temp(p
);
854 struct ureg dots
= get_temp(p
);
855 struct ureg _col0
= undef
, _col1
= undef
;
856 struct ureg _bfc0
= undef
, _bfc1
= undef
;
859 for (i
= 0; i
< MAX_LIGHTS
; i
++)
860 if (p
->state
->unit
[i
].light_enabled
)
863 set_material_flags(p
);
866 struct ureg shininess
= get_material(p
, 0, STATE_SHININESS
);
867 emit_op1(p
, VP_OPCODE_MOV
, dots
, WRITEMASK_W
, swizzle1(shininess
,X
));
868 release_temp(p
, shininess
);
870 _col0
= make_temp(p
, get_scenecolor(p
, 0));
872 _col1
= make_temp(p
, get_identity_param(p
));
879 struct ureg shininess
= get_material(p
, 1, STATE_SHININESS
);
880 emit_op1(p
, VP_OPCODE_MOV
, dots
, WRITEMASK_Z
,
881 negate(swizzle1(shininess
,X
)));
882 release_temp(p
, shininess
);
884 _bfc0
= make_temp(p
, get_scenecolor(p
, 1));
886 _bfc1
= make_temp(p
, get_identity_param(p
));
892 /* If no lights, still need to emit the scenecolor.
895 struct ureg res0
= register_output( p
, VERT_RESULT_COL0
);
896 emit_op1(p
, VP_OPCODE_MOV
, res0
, 0, _col0
);
900 struct ureg res1
= register_output( p
, VERT_RESULT_COL1
);
901 emit_op1(p
, VP_OPCODE_MOV
, res1
, 0, _col1
);
905 struct ureg res0
= register_output( p
, VERT_RESULT_BFC0
);
906 emit_op1(p
, VP_OPCODE_MOV
, res0
, 0, _bfc0
);
909 if (twoside
&& separate
) {
910 struct ureg res1
= register_output( p
, VERT_RESULT_BFC1
);
911 emit_op1(p
, VP_OPCODE_MOV
, res1
, 0, _bfc1
);
914 if (nr_lights
== 0) {
920 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
921 if (p
->state
->unit
[i
].light_enabled
) {
922 struct ureg half
= undef
;
923 struct ureg att
= undef
, VPpli
= undef
;
927 if (p
->state
->unit
[i
].light_eyepos3_is_zero
) {
928 /* Can used precomputed constants in this case.
929 * Attenuation never applies to infinite lights.
931 VPpli
= register_param3(p
, STATE_LIGHT
, i
,
932 STATE_POSITION_NORMALIZED
);
933 half
= register_param3(p
, STATE_LIGHT
, i
, STATE_HALF
);
936 struct ureg Ppli
= register_param3(p
, STATE_LIGHT
, i
,
938 struct ureg V
= get_eye_position(p
);
939 struct ureg dist
= get_temp(p
);
944 /* Calulate VPpli vector
946 emit_op2(p
, VP_OPCODE_SUB
, VPpli
, 0, Ppli
, V
);
948 /* Normalize VPpli. The dist value also used in
951 emit_op2(p
, VP_OPCODE_DP3
, dist
, 0, VPpli
, VPpli
);
952 emit_op1(p
, VP_OPCODE_RSQ
, dist
, 0, dist
);
953 emit_op2(p
, VP_OPCODE_MUL
, VPpli
, 0, VPpli
, dist
);
956 /* Calculate attenuation:
958 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
||
959 p
->state
->unit
[i
].light_attenuated
) {
960 att
= calculate_light_attenuation(p
, i
, VPpli
, dist
);
964 /* Calculate viewer direction, or use infinite viewer:
966 if (p
->state
->light_local_viewer
) {
967 struct ureg eye_hat
= get_eye_position_normalized(p
);
968 emit_op2(p
, VP_OPCODE_SUB
, half
, 0, VPpli
, eye_hat
);
971 struct ureg z_dir
= swizzle(get_identity_param(p
),X
,Y
,W
,Z
);
972 emit_op2(p
, VP_OPCODE_ADD
, half
, 0, VPpli
, z_dir
);
975 emit_normalize_vec3(p
, half
, half
);
977 release_temp(p
, dist
);
980 /* Calculate dot products:
982 emit_op2(p
, VP_OPCODE_DP3
, dots
, WRITEMASK_X
, normal
, VPpli
);
983 emit_op2(p
, VP_OPCODE_DP3
, dots
, WRITEMASK_Y
, normal
, half
);
986 /* Front face lighting:
989 struct ureg ambient
= get_lightprod(p
, i
, 0, STATE_AMBIENT
);
990 struct ureg diffuse
= get_lightprod(p
, i
, 0, STATE_DIFFUSE
);
991 struct ureg specular
= get_lightprod(p
, i
, 0, STATE_SPECULAR
);
992 struct ureg res0
, res1
;
995 emit_op1(p
, VP_OPCODE_LIT
, lit
, 0, dots
);
998 emit_op2(p
, VP_OPCODE_MUL
, lit
, 0, lit
, att
);
1001 if (count
== nr_lights
) {
1003 mask0
= WRITEMASK_XYZ
;
1004 mask1
= WRITEMASK_XYZ
;
1005 res0
= register_output( p
, VERT_RESULT_COL0
);
1006 res1
= register_output( p
, VERT_RESULT_COL1
);
1010 mask1
= WRITEMASK_XYZ
;
1012 res1
= register_output( p
, VERT_RESULT_COL0
);
1021 emit_op3(p
, VP_OPCODE_MAD
, _col0
, 0, swizzle1(lit
,X
), ambient
, _col0
);
1022 emit_op3(p
, VP_OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _col0
);
1023 emit_op3(p
, VP_OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _col1
);
1025 release_temp(p
, ambient
);
1026 release_temp(p
, diffuse
);
1027 release_temp(p
, specular
);
1030 /* Back face lighting:
1033 struct ureg ambient
= get_lightprod(p
, i
, 1, STATE_AMBIENT
);
1034 struct ureg diffuse
= get_lightprod(p
, i
, 1, STATE_DIFFUSE
);
1035 struct ureg specular
= get_lightprod(p
, i
, 1, STATE_SPECULAR
);
1036 struct ureg res0
, res1
;
1037 GLuint mask0
, mask1
;
1039 emit_op1(p
, VP_OPCODE_LIT
, lit
, 0, negate(swizzle(dots
,X
,Y
,W
,Z
)));
1042 emit_op2(p
, VP_OPCODE_MUL
, lit
, 0, lit
, att
);
1044 if (count
== nr_lights
) {
1046 mask0
= WRITEMASK_XYZ
;
1047 mask1
= WRITEMASK_XYZ
;
1048 res0
= register_output( p
, VERT_RESULT_BFC0
);
1049 res1
= register_output( p
, VERT_RESULT_BFC1
);
1053 mask1
= WRITEMASK_XYZ
;
1055 res1
= register_output( p
, VERT_RESULT_BFC0
);
1064 emit_op3(p
, VP_OPCODE_MAD
, _bfc0
, 0, swizzle1(lit
,X
), ambient
, _bfc0
);
1065 emit_op3(p
, VP_OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _bfc0
);
1066 emit_op3(p
, VP_OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _bfc1
);
1068 release_temp(p
, ambient
);
1069 release_temp(p
, diffuse
);
1070 release_temp(p
, specular
);
1073 release_temp(p
, half
);
1074 release_temp(p
, VPpli
);
1075 release_temp(p
, att
);
1083 static void build_fog( struct tnl_program
*p
)
1085 struct ureg fog
= register_output(p
, VERT_RESULT_FOGC
);
1088 if (p
->state
->fog_source_is_depth
) {
1089 input
= swizzle1(get_eye_position(p
), Z
);
1092 input
= swizzle1(register_input(p
, VERT_ATTRIB_FOG
), X
);
1095 if (p
->state
->tnl_do_vertex_fog
) {
1096 struct ureg params
= register_param1(p
, STATE_FOG_PARAMS
);
1097 struct ureg tmp
= get_temp(p
);
1099 switch (p
->state
->fog_mode
) {
1101 struct ureg id
= get_identity_param(p
);
1102 emit_op2(p
, VP_OPCODE_SUB
, tmp
, 0, swizzle1(params
,Z
), input
);
1103 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, tmp
, swizzle1(params
,W
));
1104 emit_op2(p
, VP_OPCODE_MAX
, tmp
, 0, tmp
, swizzle1(id
,X
)); /* saturate */
1105 emit_op2(p
, VP_OPCODE_MIN
, fog
, WRITEMASK_X
, tmp
, swizzle1(id
,W
));
1109 emit_op1(p
, VP_OPCODE_ABS
, tmp
, 0, input
);
1110 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, tmp
, swizzle1(params
,X
));
1111 emit_op2(p
, VP_OPCODE_POW
, fog
, WRITEMASK_X
,
1112 register_const1f(p
, M_E
), negate(tmp
));
1115 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, input
, swizzle1(params
,X
));
1116 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, tmp
, tmp
);
1117 emit_op2(p
, VP_OPCODE_POW
, fog
, WRITEMASK_X
,
1118 register_const1f(p
, M_E
), negate(tmp
));
1122 release_temp(p
, tmp
);
1125 /* results = incoming fog coords (compute fog per-fragment later)
1127 * KW: Is it really necessary to do anything in this case?
1129 emit_op1(p
, VP_OPCODE_MOV
, fog
, WRITEMASK_X
, input
);
1133 static void build_reflect_texgen( struct tnl_program
*p
,
1137 struct ureg normal
= get_eye_normal(p
);
1138 struct ureg eye_hat
= get_eye_position_normalized(p
);
1139 struct ureg tmp
= get_temp(p
);
1142 emit_op2(p
, VP_OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1144 emit_op2(p
, VP_OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1146 emit_op3(p
, VP_OPCODE_MAD
, dest
, writemask
, negate(tmp
), normal
, eye_hat
);
1149 static void build_sphere_texgen( struct tnl_program
*p
,
1153 struct ureg normal
= get_eye_normal(p
);
1154 struct ureg eye_hat
= get_eye_position_normalized(p
);
1155 struct ureg tmp
= get_temp(p
);
1156 struct ureg half
= register_scalar_const(p
, .5);
1157 struct ureg r
= get_temp(p
);
1158 struct ureg inv_m
= get_temp(p
);
1159 struct ureg id
= get_identity_param(p
);
1161 /* Could share the above calculations, but it would be
1162 * a fairly odd state for someone to set (both sphere and
1163 * reflection active for different texture coordinate
1164 * components. Of course - if two texture units enable
1165 * reflect and/or sphere, things start to tilt in favour
1166 * of seperating this out:
1170 emit_op2(p
, VP_OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1172 emit_op2(p
, VP_OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1174 emit_op3(p
, VP_OPCODE_MAD
, r
, 0, negate(tmp
), normal
, eye_hat
);
1176 emit_op2(p
, VP_OPCODE_ADD
, tmp
, 0, r
, swizzle(id
,X
,Y
,W
,Z
));
1177 /* rx^2 + ry^2 + (rz+1)^2 */
1178 emit_op2(p
, VP_OPCODE_DP3
, tmp
, 0, tmp
, tmp
);
1180 emit_op1(p
, VP_OPCODE_RSQ
, tmp
, 0, tmp
);
1182 emit_op2(p
, VP_OPCODE_MUL
, inv_m
, 0, tmp
, half
);
1184 emit_op3(p
, VP_OPCODE_MAD
, dest
, writemask
, r
, inv_m
, half
);
1186 release_temp(p
, tmp
);
1188 release_temp(p
, inv_m
);
1192 static void build_texture_transform( struct tnl_program
*p
)
1196 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
1197 GLuint texmat_enabled
= p
->state
->unit
[i
].texmat_enabled
;
1199 if (p
->state
->unit
[i
].texgen_enabled
|| texmat_enabled
) {
1200 struct ureg out
= register_output(p
, VERT_RESULT_TEX0
+ i
);
1201 struct ureg out_texgen
= undef
;
1203 if (p
->state
->unit
[i
].texgen_enabled
) {
1204 GLuint copy_mask
= 0;
1205 GLuint sphere_mask
= 0;
1206 GLuint reflect_mask
= 0;
1207 GLuint normal_mask
= 0;
1211 out_texgen
= get_temp(p
);
1215 modes
[0] = p
->state
->unit
[i
].texgen_mode0
;
1216 modes
[1] = p
->state
->unit
[i
].texgen_mode1
;
1217 modes
[2] = p
->state
->unit
[i
].texgen_mode2
;
1218 modes
[3] = p
->state
->unit
[i
].texgen_mode3
;
1220 for (j
= 0; j
< 4; j
++) {
1222 case TXG_OBJ_LINEAR
: {
1223 struct ureg obj
= register_input(p
, VERT_ATTRIB_POS
);
1225 register_param3(p
, STATE_TEXGEN
, i
,
1226 STATE_TEXGEN_OBJECT_S
+ j
);
1228 emit_op2(p
, VP_OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1232 case TXG_EYE_LINEAR
: {
1233 struct ureg eye
= get_eye_position(p
);
1235 register_param3(p
, STATE_TEXGEN
, i
,
1236 STATE_TEXGEN_EYE_S
+ j
);
1238 emit_op2(p
, VP_OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1242 case TXG_SPHERE_MAP
:
1243 sphere_mask
|= WRITEMASK_X
<< j
;
1245 case TXG_REFLECTION_MAP
:
1246 reflect_mask
|= WRITEMASK_X
<< j
;
1248 case TXG_NORMAL_MAP
:
1249 normal_mask
|= WRITEMASK_X
<< j
;
1252 copy_mask
|= WRITEMASK_X
<< j
;
1259 build_sphere_texgen(p
, out_texgen
, sphere_mask
);
1263 build_reflect_texgen(p
, out_texgen
, reflect_mask
);
1267 struct ureg normal
= get_eye_normal(p
);
1268 emit_op1(p
, VP_OPCODE_MOV
, out_texgen
, normal_mask
, normal
);
1272 struct ureg in
= register_input(p
, VERT_ATTRIB_TEX0
+i
);
1273 emit_op1(p
, VP_OPCODE_MOV
, out_texgen
, copy_mask
, in
);
1277 if (texmat_enabled
) {
1278 struct ureg texmat
[4];
1279 struct ureg in
= (!is_undef(out_texgen
) ?
1281 register_input(p
, VERT_ATTRIB_TEX0
+i
));
1283 register_matrix_param6( p
, STATE_MATRIX
, STATE_TEXTURE
, i
,
1284 0, 3, STATE_MATRIX
, texmat
);
1285 emit_matrix_transform_vec4( p
, out
, texmat
, in
);
1288 register_matrix_param6( p
, STATE_MATRIX
, STATE_TEXTURE
, i
,
1289 0, 3, STATE_MATRIX_TRANSPOSE
, texmat
);
1290 emit_transpose_matrix_transform_vec4( p
, out
, texmat
, in
);
1296 else if (p
->state
->unit
[i
].texunit_really_enabled
) {
1297 /* KW: _ReallyEnabled isn't sufficient? Need to know whether
1298 * this texture unit is referenced by the fragment shader.
1300 emit_passthrough(p
, VERT_ATTRIB_TEX0
+i
, VERT_RESULT_TEX0
+i
);
1306 /* Seems like it could be tighter:
1308 static void build_pointsize( struct tnl_program
*p
)
1310 struct ureg eye
= get_eye_position(p
);
1311 struct ureg state_size
= register_param1(p
, STATE_POINT_SIZE
);
1312 struct ureg state_attenuation
= register_param1(p
, STATE_POINT_ATTENUATION
);
1313 struct ureg out
= register_output(p
, VERT_RESULT_PSIZ
);
1314 struct ureg ut
= get_temp(p
);
1316 /* 1, -Z, Z * Z, 1 */
1317 emit_op1(p
, VP_OPCODE_MOV
, ut
, 0, swizzle1(get_identity_param(p
), W
));
1318 emit_op2(p
, VP_OPCODE_MUL
, ut
, WRITEMASK_YZ
, ut
, negate(swizzle1(eye
, Z
)));
1319 emit_op2(p
, VP_OPCODE_MUL
, ut
, WRITEMASK_Z
, ut
, negate(swizzle1(eye
, Z
)));
1322 /* p1 + p2 * dist + p3 * dist * dist, 0 */
1323 emit_op2(p
, VP_OPCODE_DP3
, ut
, 0, ut
, state_attenuation
);
1326 emit_op1(p
, VP_OPCODE_RCP
, ut
, 0, ut
);
1328 /* out = pointSize / factor */
1329 emit_op2(p
, VP_OPCODE_MUL
, out
, WRITEMASK_X
, ut
, state_size
);
1331 release_temp(p
, ut
);
1334 static void build_tnl_program( struct tnl_program
*p
)
1335 { /* Emit the program, starting with modelviewproject:
1339 /* Lighting calculations:
1341 if (p
->state
->light_global_enabled
)
1344 emit_passthrough(p
, VERT_ATTRIB_COLOR0
, VERT_RESULT_COL0
);
1346 if (p
->state
->fog_enabled
)
1349 if (p
->state
->texture_enabled_global
)
1350 build_texture_transform(p
);
1352 if (p
->state
->point_attenuated
)
1357 emit_op1(p
, VP_OPCODE_END
, undef
, 0, undef
);
1362 _mesa_printf ("\n");
1368 create_new_program( const struct state_key
*key
,
1369 struct vertex_program
*program
,
1372 struct tnl_program p
;
1374 _mesa_memset(&p
, 0, sizeof(p
));
1376 p
.program
= program
;
1377 p
.eye_position
= undef
;
1378 p
.eye_position_normalized
= undef
;
1379 p
.eye_normal
= undef
;
1383 if (max_temps
>= sizeof(int) * 8)
1384 p
.temp_reserved
= 0;
1386 p
.temp_reserved
= ~((1<<max_temps
)-1);
1388 p
.program
->Instructions
= MALLOC(sizeof(struct vp_instruction
) * MAX_INSN
);
1389 p
.program
->Base
.String
= 0;
1390 p
.program
->Base
.NumInstructions
=
1391 p
.program
->Base
.NumTemporaries
=
1392 p
.program
->Base
.NumParameters
=
1393 p
.program
->Base
.NumAttributes
= p
.program
->Base
.NumAddressRegs
= 0;
1394 p
.program
->Parameters
= _mesa_new_parameter_list();
1395 p
.program
->InputsRead
= 0;
1396 p
.program
->OutputsWritten
= 0;
1398 build_tnl_program( &p
);
1401 static void *search_cache( struct tnl_cache
*cache
,
1406 struct tnl_cache_item
*c
;
1408 for (c
= cache
->items
[hash
% cache
->size
]; c
; c
= c
->next
) {
1409 if (c
->hash
== hash
&& _mesa_memcmp(c
->key
, key
, keysize
) == 0)
1416 static void rehash( struct tnl_cache
*cache
)
1418 struct tnl_cache_item
**items
;
1419 struct tnl_cache_item
*c
, *next
;
1422 size
= cache
->size
* 3;
1423 items
= MALLOC(size
* sizeof(*items
));
1424 _mesa_memset(items
, 0, size
* sizeof(*items
));
1426 for (i
= 0; i
< cache
->size
; i
++)
1427 for (c
= cache
->items
[i
]; c
; c
= next
) {
1429 c
->next
= items
[c
->hash
% size
];
1430 items
[c
->hash
% size
] = c
;
1434 cache
->items
= items
;
1438 static void cache_item( struct tnl_cache
*cache
,
1443 struct tnl_cache_item
*c
= MALLOC(sizeof(*c
));
1448 if (++cache
->n_items
> cache
->size
* 1.5)
1451 c
->next
= cache
->items
[hash
% cache
->size
];
1452 cache
->items
[hash
% cache
->size
] = c
;
1455 static GLuint
hash_key( struct state_key
*key
)
1457 GLuint
*ikey
= (GLuint
*)key
;
1460 /* I'm sure this can be improved on, but speed is important:
1462 for (i
= 0; i
< sizeof(*key
)/sizeof(GLuint
); i
++)
1468 void _tnl_UpdateFixedFunctionProgram( GLcontext
*ctx
)
1470 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1471 struct state_key
*key
;
1474 if (ctx
->VertexProgram
._Enabled
)
1477 /* Grab all the relevent state and put it in a single structure:
1479 key
= make_state_key(ctx
);
1480 hash
= hash_key(key
);
1482 /* Look for an already-prepared program for this state:
1484 ctx
->_TnlProgram
= (struct vertex_program
*)
1485 search_cache( tnl
->vp_cache
, hash
, key
, sizeof(*key
) );
1487 /* OK, we'll have to build a new one:
1489 if (!ctx
->_TnlProgram
) {
1491 _mesa_printf("Build new TNL program\n");
1493 ctx
->_TnlProgram
= (struct vertex_program
*)
1494 ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
1496 create_new_program( key
, ctx
->_TnlProgram
,
1497 ctx
->Const
.VertexProgram
.MaxTemps
);
1499 cache_item(tnl
->vp_cache
, hash
, key
, ctx
->_TnlProgram
);
1504 _mesa_printf("Found existing TNL program for key %x\n", hash
);
1507 /* Need a BindProgram callback for the driver?
1512 void _tnl_ProgramCacheDestroy( GLcontext
*ctx
)
1514 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1515 struct tnl_cache_item
*c
, *next
;
1518 for (i
= 0; i
< tnl
->vp_cache
->size
; i
++)
1519 for (c
= tnl
->vp_cache
->items
[i
]; c
; c
= next
) {
1526 FREE(tnl
->vp_cache
->items
);
1527 FREE(tnl
->vp_cache
);