2 * Mesa 3-D graphics library
5 * Copyright (C) 2006 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" /* NOTE: very light dependency on this */
37 #include "t_vp_build.h"
39 #include "shader/program.h"
40 #include "shader/program_instruction.h"
43 unsigned light_global_enabled
:1;
44 unsigned light_local_viewer
:1;
45 unsigned light_twoside
:1;
46 unsigned light_color_material
:1;
47 unsigned light_color_material_mask
:12;
48 unsigned light_material_mask
:12;
51 unsigned rescale_normals
:1;
52 unsigned fog_source_is_depth
:1;
53 unsigned tnl_do_vertex_fog
:1;
54 unsigned separate_specular
:1;
56 unsigned point_attenuated
:1;
57 unsigned texture_enabled_global
:1;
58 unsigned fragprog_inputs_read
:12;
61 unsigned light_enabled
:1;
62 unsigned light_eyepos3_is_zero
:1;
63 unsigned light_spotcutoff_is_180
:1;
64 unsigned light_attenuated
:1;
65 unsigned texunit_really_enabled
:1;
66 unsigned texmat_enabled
:1;
67 unsigned texgen_enabled
:4;
68 unsigned texgen_mode0
:4;
69 unsigned texgen_mode1
:4;
70 unsigned texgen_mode2
:4;
71 unsigned texgen_mode3
:4;
82 static GLuint
translate_fog_mode( GLenum mode
)
85 case GL_LINEAR
: return FOG_LINEAR
;
86 case GL_EXP
: return FOG_EXP
;
87 case GL_EXP2
: return FOG_EXP2
;
88 default: return FOG_NONE
;
93 #define TXG_OBJ_LINEAR 1
94 #define TXG_EYE_LINEAR 2
95 #define TXG_SPHERE_MAP 3
96 #define TXG_REFLECTION_MAP 4
97 #define TXG_NORMAL_MAP 5
99 static GLuint
translate_texgen( GLboolean enabled
, GLenum mode
)
105 case GL_OBJECT_LINEAR
: return TXG_OBJ_LINEAR
;
106 case GL_EYE_LINEAR
: return TXG_EYE_LINEAR
;
107 case GL_SPHERE_MAP
: return TXG_SPHERE_MAP
;
108 case GL_REFLECTION_MAP_NV
: return TXG_REFLECTION_MAP
;
109 case GL_NORMAL_MAP_NV
: return TXG_NORMAL_MAP
;
110 default: return TXG_NONE
;
114 static struct state_key
*make_state_key( GLcontext
*ctx
)
116 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
117 struct vertex_buffer
*VB
= &tnl
->vb
;
118 const struct gl_fragment_program
*fp
= ctx
->FragmentProgram
._Current
;
119 struct state_key
*key
= CALLOC_STRUCT(state_key
);
122 /* This now relies on texenvprogram.c being active:
126 key
->fragprog_inputs_read
= fp
->Base
.InputsRead
;
128 key
->separate_specular
= (ctx
->Light
.Model
.ColorControl
==
129 GL_SEPARATE_SPECULAR_COLOR
);
131 if (ctx
->Light
.Enabled
) {
132 key
->light_global_enabled
= 1;
134 if (ctx
->Light
.Model
.LocalViewer
)
135 key
->light_local_viewer
= 1;
137 if (ctx
->Light
.Model
.TwoSide
)
138 key
->light_twoside
= 1;
140 if (ctx
->Light
.ColorMaterialEnabled
) {
141 key
->light_color_material
= 1;
142 key
->light_color_material_mask
= ctx
->Light
.ColorMaterialBitmask
;
145 for (i
= _TNL_FIRST_MAT
; i
<= _TNL_LAST_MAT
; i
++)
146 if (VB
->AttribPtr
[i
]->stride
)
147 key
->light_material_mask
|= 1<<(i
-_TNL_ATTRIB_MAT_FRONT_AMBIENT
);
149 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
150 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
152 if (light
->Enabled
) {
153 key
->unit
[i
].light_enabled
= 1;
155 if (light
->EyePosition
[3] == 0.0)
156 key
->unit
[i
].light_eyepos3_is_zero
= 1;
158 if (light
->SpotCutoff
== 180.0)
159 key
->unit
[i
].light_spotcutoff_is_180
= 1;
161 if (light
->ConstantAttenuation
!= 1.0 ||
162 light
->LinearAttenuation
!= 0.0 ||
163 light
->QuadraticAttenuation
!= 0.0)
164 key
->unit
[i
].light_attenuated
= 1;
169 if (ctx
->Transform
.Normalize
)
172 if (ctx
->Transform
.RescaleNormals
)
173 key
->rescale_normals
= 1;
175 key
->fog_mode
= translate_fog_mode(fp
->FogOption
);
177 if (ctx
->Fog
.FogCoordinateSource
== GL_FRAGMENT_DEPTH_EXT
)
178 key
->fog_source_is_depth
= 1;
180 if (tnl
->_DoVertexFog
)
181 key
->tnl_do_vertex_fog
= 1;
183 if (ctx
->Point
._Attenuated
)
184 key
->point_attenuated
= 1;
186 if (ctx
->Texture
._TexGenEnabled
||
187 ctx
->Texture
._TexMatEnabled
||
188 ctx
->Texture
._EnabledUnits
)
189 key
->texture_enabled_global
= 1;
191 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
192 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
194 if (texUnit
->_ReallyEnabled
)
195 key
->unit
[i
].texunit_really_enabled
= 1;
197 if (ctx
->Texture
._TexMatEnabled
& ENABLE_TEXMAT(i
))
198 key
->unit
[i
].texmat_enabled
= 1;
200 if (texUnit
->TexGenEnabled
) {
201 key
->unit
[i
].texgen_enabled
= 1;
203 key
->unit
[i
].texgen_mode0
=
204 translate_texgen( texUnit
->TexGenEnabled
& (1<<0),
206 key
->unit
[i
].texgen_mode1
=
207 translate_texgen( texUnit
->TexGenEnabled
& (1<<1),
209 key
->unit
[i
].texgen_mode2
=
210 translate_texgen( texUnit
->TexGenEnabled
& (1<<2),
212 key
->unit
[i
].texgen_mode3
=
213 translate_texgen( texUnit
->TexGenEnabled
& (1<<3),
223 /* Very useful debugging tool - produces annotated listing of
224 * generated program with line/function references for each
225 * instruction back into this file:
227 #define DISASSEM (MESA_VERBOSE&VERBOSE_DISASSEM)
229 /* Should be tunable by the driver - do we want to do matrix
230 * multiplications with DP4's or with MUL/MAD's? SSE works better
231 * with the latter, drivers may differ.
237 /* Use uregs to represent registers internally, translate to Mesa's
238 * expected formats on emit.
240 * NOTE: These are passed by value extensively in this file rather
241 * than as usual by pointer reference. If this disturbs you, try
242 * remembering they are just 32bits in size.
244 * GCC is smart enough to deal with these dword-sized structures in
245 * much the same way as if I had defined them as dwords and was using
246 * macros to access and set the fields. This is much nicer and easier
251 GLint idx
:8; /* relative addressing may be negative */
259 const struct state_key
*state
;
260 struct gl_vertex_program
*program
;
263 GLuint temp_reserved
;
265 struct ureg eye_position
;
266 struct ureg eye_position_normalized
;
267 struct ureg eye_normal
;
268 struct ureg identity
;
271 GLuint color_materials
;
275 static const struct ureg undef
= {
293 static struct ureg
make_ureg(GLuint file
, GLint idx
)
299 reg
.swz
= SWIZZLE_NOOP
;
306 static struct ureg
negate( struct ureg reg
)
313 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
315 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
318 GET_SWZ(reg
.swz
, w
));
323 static struct ureg
swizzle1( struct ureg reg
, int x
)
325 return swizzle(reg
, x
, x
, x
, x
);
328 static struct ureg
get_temp( struct tnl_program
*p
)
330 int bit
= _mesa_ffs( ~p
->temp_in_use
);
332 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
336 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
337 p
->program
->Base
.NumTemporaries
= bit
;
339 p
->temp_in_use
|= 1<<(bit
-1);
340 return make_ureg(PROGRAM_TEMPORARY
, bit
-1);
343 static struct ureg
reserve_temp( struct tnl_program
*p
)
345 struct ureg temp
= get_temp( p
);
346 p
->temp_reserved
|= 1<<temp
.idx
;
350 static void release_temp( struct tnl_program
*p
, struct ureg reg
)
352 if (reg
.file
== PROGRAM_TEMPORARY
) {
353 p
->temp_in_use
&= ~(1<<reg
.idx
);
354 p
->temp_in_use
|= p
->temp_reserved
; /* can't release reserved temps */
358 static void release_temps( struct tnl_program
*p
)
360 p
->temp_in_use
= p
->temp_reserved
;
365 static struct ureg
register_input( struct tnl_program
*p
, GLuint input
)
367 p
->program
->Base
.InputsRead
|= (1<<input
);
368 return make_ureg(PROGRAM_INPUT
, input
);
371 static struct ureg
register_output( struct tnl_program
*p
, GLuint output
)
373 p
->program
->Base
.OutputsWritten
|= (1<<output
);
374 return make_ureg(PROGRAM_OUTPUT
, output
);
377 static struct ureg
register_const4f( struct tnl_program
*p
,
389 idx
= _mesa_add_unnamed_constant( p
->program
->Base
.Parameters
, values
, 4 );
390 return make_ureg(PROGRAM_STATE_VAR
, idx
);
393 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
394 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
395 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
396 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
398 static GLboolean
is_undef( struct ureg reg
)
400 return reg
.file
== PROGRAM_UNDEFINED
;
403 static struct ureg
get_identity_param( struct tnl_program
*p
)
405 if (is_undef(p
->identity
))
406 p
->identity
= register_const4f(p
, 0,0,0,1);
411 static struct ureg
register_param6( struct tnl_program
*p
,
427 idx
= _mesa_add_state_reference( p
->program
->Base
.Parameters
, tokens
);
428 return make_ureg(PROGRAM_STATE_VAR
, idx
);
432 #define register_param1(p,s0) register_param6(p,s0,0,0,0,0,0)
433 #define register_param2(p,s0,s1) register_param6(p,s0,s1,0,0,0,0)
434 #define register_param3(p,s0,s1,s2) register_param6(p,s0,s1,s2,0,0,0)
435 #define register_param4(p,s0,s1,s2,s3) register_param6(p,s0,s1,s2,s3,0,0)
438 static void register_matrix_param6( struct tnl_program
*p
,
445 struct ureg
*matrix
)
449 /* This is a bit sad as the support is there to pull the whole
450 * matrix out in one go:
452 for (i
= 0; i
<= s4
- s3
; i
++)
453 matrix
[i
] = register_param6( p
, s0
, s1
, s2
, i
, i
, s5
);
457 static void emit_arg( struct prog_src_register
*src
,
460 src
->File
= reg
.file
;
461 src
->Index
= reg
.idx
;
462 src
->Swizzle
= reg
.swz
;
463 src
->NegateBase
= reg
.negate
? NEGATE_XYZW
: 0;
469 static void emit_dst( struct prog_dst_register
*dst
,
470 struct ureg reg
, GLuint mask
)
472 dst
->File
= reg
.file
;
473 dst
->Index
= reg
.idx
;
474 /* allow zero as a shorthand for xyzw */
475 dst
->WriteMask
= mask
? mask
: WRITEMASK_XYZW
;
476 dst
->CondMask
= COND_TR
;
477 dst
->CondSwizzle
= 0;
482 static void debug_insn( struct prog_instruction
*inst
, const char *fn
,
486 static const char *last_fn
;
490 _mesa_printf("%s:\n", fn
);
493 _mesa_printf("%d:\t", line
);
494 _mesa_print_instruction(inst
);
499 static void emit_op3fn(struct tnl_program
*p
,
509 GLuint nr
= p
->program
->Base
.NumInstructions
++;
510 struct prog_instruction
*inst
= &p
->program
->Base
.Instructions
[nr
];
512 if (p
->program
->Base
.NumInstructions
> MAX_INSN
) {
513 _mesa_problem(0, "Out of instructions in emit_op3fn\n");
517 inst
->Opcode
= (enum prog_opcode
) op
;
521 emit_arg( &inst
->SrcReg
[0], src0
);
522 emit_arg( &inst
->SrcReg
[1], src1
);
523 emit_arg( &inst
->SrcReg
[2], src2
);
525 emit_dst( &inst
->DstReg
, dest
, mask
);
527 debug_insn(inst
, fn
, line
);
531 #define emit_op3(p, op, dst, mask, src0, src1, src2) \
532 emit_op3fn(p, op, dst, mask, src0, src1, src2, __FUNCTION__, __LINE__)
534 #define emit_op2(p, op, dst, mask, src0, src1) \
535 emit_op3fn(p, op, dst, mask, src0, src1, undef, __FUNCTION__, __LINE__)
537 #define emit_op1(p, op, dst, mask, src0) \
538 emit_op3fn(p, op, dst, mask, src0, undef, undef, __FUNCTION__, __LINE__)
541 static struct ureg
make_temp( struct tnl_program
*p
, struct ureg reg
)
543 if (reg
.file
== PROGRAM_TEMPORARY
&&
544 !(p
->temp_reserved
& (1<<reg
.idx
)))
547 struct ureg temp
= get_temp(p
);
548 emit_op1(p
, OPCODE_MOV
, temp
, 0, reg
);
554 /* Currently no tracking performed of input/output/register size or
555 * active elements. Could be used to reduce these operations, as
556 * could the matrix type.
558 static void emit_matrix_transform_vec4( struct tnl_program
*p
,
560 const struct ureg
*mat
,
563 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_X
, src
, mat
[0]);
564 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_Y
, src
, mat
[1]);
565 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_Z
, src
, mat
[2]);
566 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_W
, src
, mat
[3]);
569 /* This version is much easier to implement if writemasks are not
570 * supported natively on the target or (like SSE), the target doesn't
571 * have a clean/obvious dotproduct implementation.
573 static void emit_transpose_matrix_transform_vec4( struct tnl_program
*p
,
575 const struct ureg
*mat
,
580 if (dest
.file
!= PROGRAM_TEMPORARY
)
585 emit_op2(p
, OPCODE_MUL
, tmp
, 0, swizzle1(src
,X
), mat
[0]);
586 emit_op3(p
, OPCODE_MAD
, tmp
, 0, swizzle1(src
,Y
), mat
[1], tmp
);
587 emit_op3(p
, OPCODE_MAD
, tmp
, 0, swizzle1(src
,Z
), mat
[2], tmp
);
588 emit_op3(p
, OPCODE_MAD
, dest
, 0, swizzle1(src
,W
), mat
[3], tmp
);
590 if (dest
.file
!= PROGRAM_TEMPORARY
)
591 release_temp(p
, tmp
);
594 static void emit_matrix_transform_vec3( struct tnl_program
*p
,
596 const struct ureg
*mat
,
599 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_X
, src
, mat
[0]);
600 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_Y
, src
, mat
[1]);
601 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_Z
, src
, mat
[2]);
605 static void emit_normalize_vec3( struct tnl_program
*p
,
609 struct ureg tmp
= get_temp(p
);
610 emit_op2(p
, OPCODE_DP3
, tmp
, 0, src
, src
);
611 emit_op1(p
, OPCODE_RSQ
, tmp
, 0, tmp
);
612 emit_op2(p
, OPCODE_MUL
, dest
, 0, src
, tmp
);
613 release_temp(p
, tmp
);
616 static void emit_passthrough( struct tnl_program
*p
,
620 struct ureg out
= register_output(p
, output
);
621 emit_op1(p
, OPCODE_MOV
, out
, 0, register_input(p
, input
));
624 static struct ureg
get_eye_position( struct tnl_program
*p
)
626 if (is_undef(p
->eye_position
)) {
627 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
628 struct ureg modelview
[4];
630 p
->eye_position
= reserve_temp(p
);
633 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 0, 3,
634 STATE_MATRIX
, modelview
);
636 emit_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
639 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 0, 3,
640 STATE_MATRIX_TRANSPOSE
, modelview
);
642 emit_transpose_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
646 return p
->eye_position
;
650 static struct ureg
get_eye_position_normalized( struct tnl_program
*p
)
652 if (is_undef(p
->eye_position_normalized
)) {
653 struct ureg eye
= get_eye_position(p
);
654 p
->eye_position_normalized
= reserve_temp(p
);
655 emit_normalize_vec3(p
, p
->eye_position_normalized
, eye
);
658 return p
->eye_position_normalized
;
662 static struct ureg
get_eye_normal( struct tnl_program
*p
)
664 if (is_undef(p
->eye_normal
)) {
665 struct ureg normal
= register_input(p
, VERT_ATTRIB_NORMAL
);
666 struct ureg mvinv
[3];
668 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 0, 2,
669 STATE_MATRIX_INVTRANS
, mvinv
);
671 p
->eye_normal
= reserve_temp(p
);
673 /* Transform to eye space:
675 emit_matrix_transform_vec3( p
, p
->eye_normal
, mvinv
, normal
);
677 /* Normalize/Rescale:
679 if (p
->state
->normalize
) {
680 emit_normalize_vec3( p
, p
->eye_normal
, p
->eye_normal
);
682 else if (p
->state
->rescale_normals
) {
683 struct ureg rescale
= register_param2(p
, STATE_INTERNAL
,
686 emit_op2( p
, OPCODE_MUL
, p
->eye_normal
, 0, normal
,
687 swizzle1(rescale
, X
));
691 return p
->eye_normal
;
696 static void build_hpos( struct tnl_program
*p
)
698 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
699 struct ureg hpos
= register_output( p
, VERT_RESULT_HPOS
);
703 register_matrix_param6( p
, STATE_MATRIX
, STATE_MVP
, 0, 0, 3,
705 emit_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
708 register_matrix_param6( p
, STATE_MATRIX
, STATE_MVP
, 0, 0, 3,
709 STATE_MATRIX_TRANSPOSE
, mvp
);
710 emit_transpose_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
715 static GLuint
material_attrib( GLuint side
, GLuint property
)
717 return ((property
- STATE_AMBIENT
) * 2 +
721 /* Get a bitmask of which material values vary on a per-vertex basis.
723 static void set_material_flags( struct tnl_program
*p
)
725 p
->color_materials
= 0;
728 if (p
->state
->light_color_material
) {
730 p
->color_materials
= p
->state
->light_color_material_mask
;
733 p
->materials
|= p
->state
->light_material_mask
;
737 static struct ureg
get_material( struct tnl_program
*p
, GLuint side
,
740 GLuint attrib
= material_attrib(side
, property
);
742 if (p
->color_materials
& (1<<attrib
))
743 return register_input(p
, VERT_ATTRIB_COLOR0
);
744 else if (p
->materials
& (1<<attrib
))
745 return register_input( p
, attrib
+ _TNL_ATTRIB_MAT_FRONT_AMBIENT
);
747 return register_param3( p
, STATE_MATERIAL
, side
, property
);
750 #define SCENE_COLOR_BITS(side) (( MAT_BIT_FRONT_EMISSION | \
751 MAT_BIT_FRONT_AMBIENT | \
752 MAT_BIT_FRONT_DIFFUSE) << (side))
754 /* Either return a precalculated constant value or emit code to
755 * calculate these values dynamically in the case where material calls
756 * are present between begin/end pairs.
758 * Probably want to shift this to the program compilation phase - if
759 * we always emitted the calculation here, a smart compiler could
760 * detect that it was constant (given a certain set of inputs), and
761 * lift it out of the main loop. That way the programs created here
762 * would be independent of the vertex_buffer details.
764 static struct ureg
get_scenecolor( struct tnl_program
*p
, GLuint side
)
766 if (p
->materials
& SCENE_COLOR_BITS(side
)) {
767 struct ureg lm_ambient
= register_param1(p
, STATE_LIGHTMODEL_AMBIENT
);
768 struct ureg material_emission
= get_material(p
, side
, STATE_EMISSION
);
769 struct ureg material_ambient
= get_material(p
, side
, STATE_AMBIENT
);
770 struct ureg material_diffuse
= get_material(p
, side
, STATE_DIFFUSE
);
771 struct ureg tmp
= make_temp(p
, material_diffuse
);
772 emit_op3(p
, OPCODE_MAD
, tmp
, WRITEMASK_XYZ
, lm_ambient
,
773 material_ambient
, material_emission
);
777 return register_param2( p
, STATE_LIGHTMODEL_SCENECOLOR
, side
);
781 static struct ureg
get_lightprod( struct tnl_program
*p
, GLuint light
,
782 GLuint side
, GLuint property
)
784 GLuint attrib
= material_attrib(side
, property
);
785 if (p
->materials
& (1<<attrib
)) {
786 struct ureg light_value
=
787 register_param3(p
, STATE_LIGHT
, light
, property
);
788 struct ureg material_value
= get_material(p
, side
, property
);
789 struct ureg tmp
= get_temp(p
);
790 emit_op2(p
, OPCODE_MUL
, tmp
, 0, light_value
, material_value
);
794 return register_param4(p
, STATE_LIGHTPROD
, light
, side
, property
);
797 static struct ureg
calculate_light_attenuation( struct tnl_program
*p
,
802 struct ureg attenuation
= register_param3(p
, STATE_LIGHT
, i
,
804 struct ureg att
= get_temp(p
);
806 /* Calculate spot attenuation:
808 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
809 struct ureg spot_dir
= register_param3(p
, STATE_LIGHT
, i
,
810 STATE_SPOT_DIRECTION
);
811 struct ureg spot
= get_temp(p
);
812 struct ureg slt
= get_temp(p
);
814 emit_normalize_vec3( p
, spot
, spot_dir
); /* XXX: precompute! */
815 emit_op2(p
, OPCODE_DP3
, spot
, 0, negate(VPpli
), spot
);
816 emit_op2(p
, OPCODE_SLT
, slt
, 0, swizzle1(spot_dir
,W
), spot
);
817 emit_op2(p
, OPCODE_POW
, spot
, 0, spot
, swizzle1(attenuation
, W
));
818 emit_op2(p
, OPCODE_MUL
, att
, 0, slt
, spot
);
820 release_temp(p
, spot
);
821 release_temp(p
, slt
);
824 /* Calculate distance attenuation:
826 if (p
->state
->unit
[i
].light_attenuated
) {
829 emit_op1(p
, OPCODE_RCP
, dist
, WRITEMASK_YZ
, dist
);
831 emit_op2(p
, OPCODE_MUL
, dist
, WRITEMASK_XZ
, dist
, swizzle1(dist
,Y
));
833 emit_op2(p
, OPCODE_DP3
, dist
, 0, attenuation
, dist
);
835 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
837 emit_op1(p
, OPCODE_RCP
, dist
, 0, dist
);
838 /* spot-atten * dist-atten */
839 emit_op2(p
, OPCODE_MUL
, att
, 0, dist
, att
);
842 emit_op1(p
, OPCODE_RCP
, att
, 0, dist
);
853 /* Need to add some addtional parameters to allow lighting in object
854 * space - STATE_SPOT_DIRECTION and STATE_HALF implicitly assume eye
857 static void build_lighting( struct tnl_program
*p
)
859 const GLboolean twoside
= p
->state
->light_twoside
;
860 const GLboolean separate
= p
->state
->separate_specular
;
861 GLuint nr_lights
= 0, count
= 0;
862 struct ureg normal
= get_eye_normal(p
);
863 struct ureg lit
= get_temp(p
);
864 struct ureg dots
= get_temp(p
);
865 struct ureg _col0
= undef
, _col1
= undef
;
866 struct ureg _bfc0
= undef
, _bfc1
= undef
;
869 for (i
= 0; i
< MAX_LIGHTS
; i
++)
870 if (p
->state
->unit
[i
].light_enabled
)
873 set_material_flags(p
);
876 struct ureg shininess
= get_material(p
, 0, STATE_SHININESS
);
877 emit_op1(p
, OPCODE_MOV
, dots
, WRITEMASK_W
, swizzle1(shininess
,X
));
878 release_temp(p
, shininess
);
880 _col0
= make_temp(p
, get_scenecolor(p
, 0));
882 _col1
= make_temp(p
, get_identity_param(p
));
889 struct ureg shininess
= get_material(p
, 1, STATE_SHININESS
);
890 emit_op1(p
, OPCODE_MOV
, dots
, WRITEMASK_Z
,
891 negate(swizzle1(shininess
,X
)));
892 release_temp(p
, shininess
);
894 _bfc0
= make_temp(p
, get_scenecolor(p
, 1));
896 _bfc1
= make_temp(p
, get_identity_param(p
));
902 /* If no lights, still need to emit the scenecolor.
905 struct ureg res0
= register_output( p
, VERT_RESULT_COL0
);
906 emit_op1(p
, OPCODE_MOV
, res0
, 0, _col0
);
910 struct ureg res1
= register_output( p
, VERT_RESULT_COL1
);
911 emit_op1(p
, OPCODE_MOV
, res1
, 0, _col1
);
915 struct ureg res0
= register_output( p
, VERT_RESULT_BFC0
);
916 emit_op1(p
, OPCODE_MOV
, res0
, 0, _bfc0
);
919 if (twoside
&& separate
) {
920 struct ureg res1
= register_output( p
, VERT_RESULT_BFC1
);
921 emit_op1(p
, OPCODE_MOV
, res1
, 0, _bfc1
);
924 if (nr_lights
== 0) {
930 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
931 if (p
->state
->unit
[i
].light_enabled
) {
932 struct ureg half
= undef
;
933 struct ureg att
= undef
, VPpli
= undef
;
937 if (p
->state
->unit
[i
].light_eyepos3_is_zero
) {
938 /* Can used precomputed constants in this case.
939 * Attenuation never applies to infinite lights.
941 VPpli
= register_param3(p
, STATE_LIGHT
, i
,
942 STATE_POSITION_NORMALIZED
);
943 half
= register_param3(p
, STATE_LIGHT
, i
, STATE_HALF
);
946 struct ureg Ppli
= register_param3(p
, STATE_LIGHT
, i
,
948 struct ureg V
= get_eye_position(p
);
949 struct ureg dist
= get_temp(p
);
954 /* Calulate VPpli vector
956 emit_op2(p
, OPCODE_SUB
, VPpli
, 0, Ppli
, V
);
958 /* Normalize VPpli. The dist value also used in
961 emit_op2(p
, OPCODE_DP3
, dist
, 0, VPpli
, VPpli
);
962 emit_op1(p
, OPCODE_RSQ
, dist
, 0, dist
);
963 emit_op2(p
, OPCODE_MUL
, VPpli
, 0, VPpli
, dist
);
966 /* Calculate attenuation:
968 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
||
969 p
->state
->unit
[i
].light_attenuated
) {
970 att
= calculate_light_attenuation(p
, i
, VPpli
, dist
);
974 /* Calculate viewer direction, or use infinite viewer:
976 if (p
->state
->light_local_viewer
) {
977 struct ureg eye_hat
= get_eye_position_normalized(p
);
978 emit_op2(p
, OPCODE_SUB
, half
, 0, VPpli
, eye_hat
);
981 struct ureg z_dir
= swizzle(get_identity_param(p
),X
,Y
,W
,Z
);
982 emit_op2(p
, OPCODE_ADD
, half
, 0, VPpli
, z_dir
);
985 emit_normalize_vec3(p
, half
, half
);
987 release_temp(p
, dist
);
990 /* Calculate dot products:
992 emit_op2(p
, OPCODE_DP3
, dots
, WRITEMASK_X
, normal
, VPpli
);
993 emit_op2(p
, OPCODE_DP3
, dots
, WRITEMASK_Y
, normal
, half
);
996 /* Front face lighting:
999 struct ureg ambient
= get_lightprod(p
, i
, 0, STATE_AMBIENT
);
1000 struct ureg diffuse
= get_lightprod(p
, i
, 0, STATE_DIFFUSE
);
1001 struct ureg specular
= get_lightprod(p
, i
, 0, STATE_SPECULAR
);
1002 struct ureg res0
, res1
;
1003 GLuint mask0
, mask1
;
1005 emit_op1(p
, OPCODE_LIT
, lit
, 0, dots
);
1008 emit_op2(p
, OPCODE_MUL
, lit
, 0, lit
, att
);
1011 if (count
== nr_lights
) {
1013 mask0
= WRITEMASK_XYZ
;
1014 mask1
= WRITEMASK_XYZ
;
1015 res0
= register_output( p
, VERT_RESULT_COL0
);
1016 res1
= register_output( p
, VERT_RESULT_COL1
);
1020 mask1
= WRITEMASK_XYZ
;
1022 res1
= register_output( p
, VERT_RESULT_COL0
);
1031 emit_op3(p
, OPCODE_MAD
, _col0
, 0, swizzle1(lit
,X
), ambient
, _col0
);
1032 emit_op3(p
, OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _col0
);
1033 emit_op3(p
, OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _col1
);
1035 release_temp(p
, ambient
);
1036 release_temp(p
, diffuse
);
1037 release_temp(p
, specular
);
1040 /* Back face lighting:
1043 struct ureg ambient
= get_lightprod(p
, i
, 1, STATE_AMBIENT
);
1044 struct ureg diffuse
= get_lightprod(p
, i
, 1, STATE_DIFFUSE
);
1045 struct ureg specular
= get_lightprod(p
, i
, 1, STATE_SPECULAR
);
1046 struct ureg res0
, res1
;
1047 GLuint mask0
, mask1
;
1049 emit_op1(p
, OPCODE_LIT
, lit
, 0, negate(swizzle(dots
,X
,Y
,W
,Z
)));
1052 emit_op2(p
, OPCODE_MUL
, lit
, 0, lit
, att
);
1054 if (count
== nr_lights
) {
1056 mask0
= WRITEMASK_XYZ
;
1057 mask1
= WRITEMASK_XYZ
;
1058 res0
= register_output( p
, VERT_RESULT_BFC0
);
1059 res1
= register_output( p
, VERT_RESULT_BFC1
);
1063 mask1
= WRITEMASK_XYZ
;
1065 res1
= register_output( p
, VERT_RESULT_BFC0
);
1074 emit_op3(p
, OPCODE_MAD
, _bfc0
, 0, swizzle1(lit
,X
), ambient
, _bfc0
);
1075 emit_op3(p
, OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _bfc0
);
1076 emit_op3(p
, OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _bfc1
);
1078 release_temp(p
, ambient
);
1079 release_temp(p
, diffuse
);
1080 release_temp(p
, specular
);
1083 release_temp(p
, half
);
1084 release_temp(p
, VPpli
);
1085 release_temp(p
, att
);
1093 static void build_fog( struct tnl_program
*p
)
1095 struct ureg fog
= register_output(p
, VERT_RESULT_FOGC
);
1098 if (p
->state
->fog_source_is_depth
) {
1099 input
= swizzle1(get_eye_position(p
), Z
);
1102 input
= swizzle1(register_input(p
, VERT_ATTRIB_FOG
), X
);
1105 if (p
->state
->tnl_do_vertex_fog
) {
1106 struct ureg params
= register_param1(p
, STATE_FOG_PARAMS
);
1107 struct ureg tmp
= get_temp(p
);
1109 switch (p
->state
->fog_mode
) {
1111 struct ureg id
= get_identity_param(p
);
1112 emit_op2(p
, OPCODE_SUB
, tmp
, 0, swizzle1(params
,Z
), input
);
1113 emit_op2(p
, OPCODE_MUL
, tmp
, 0, tmp
, swizzle1(params
,W
));
1114 emit_op2(p
, OPCODE_MAX
, tmp
, 0, tmp
, swizzle1(id
,X
)); /* saturate */
1115 emit_op2(p
, OPCODE_MIN
, fog
, WRITEMASK_X
, tmp
, swizzle1(id
,W
));
1119 emit_op1(p
, OPCODE_ABS
, tmp
, 0, input
);
1120 emit_op2(p
, OPCODE_MUL
, tmp
, 0, tmp
, swizzle1(params
,X
));
1121 emit_op2(p
, OPCODE_POW
, fog
, WRITEMASK_X
,
1122 register_const1f(p
, M_E
), negate(tmp
));
1125 emit_op2(p
, OPCODE_MUL
, tmp
, 0, input
, swizzle1(params
,X
));
1126 emit_op2(p
, OPCODE_MUL
, tmp
, 0, tmp
, tmp
);
1127 emit_op2(p
, OPCODE_POW
, fog
, WRITEMASK_X
,
1128 register_const1f(p
, M_E
), negate(tmp
));
1132 release_temp(p
, tmp
);
1135 /* results = incoming fog coords (compute fog per-fragment later)
1137 * KW: Is it really necessary to do anything in this case?
1139 emit_op1(p
, OPCODE_MOV
, fog
, WRITEMASK_X
, input
);
1143 static void build_reflect_texgen( struct tnl_program
*p
,
1147 struct ureg normal
= get_eye_normal(p
);
1148 struct ureg eye_hat
= get_eye_position_normalized(p
);
1149 struct ureg tmp
= get_temp(p
);
1152 emit_op2(p
, OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1154 emit_op2(p
, OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1156 emit_op3(p
, OPCODE_MAD
, dest
, writemask
, negate(tmp
), normal
, eye_hat
);
1158 release_temp(p
, tmp
);
1161 static void build_sphere_texgen( struct tnl_program
*p
,
1165 struct ureg normal
= get_eye_normal(p
);
1166 struct ureg eye_hat
= get_eye_position_normalized(p
);
1167 struct ureg tmp
= get_temp(p
);
1168 struct ureg half
= register_scalar_const(p
, .5);
1169 struct ureg r
= get_temp(p
);
1170 struct ureg inv_m
= get_temp(p
);
1171 struct ureg id
= get_identity_param(p
);
1173 /* Could share the above calculations, but it would be
1174 * a fairly odd state for someone to set (both sphere and
1175 * reflection active for different texture coordinate
1176 * components. Of course - if two texture units enable
1177 * reflect and/or sphere, things start to tilt in favour
1178 * of seperating this out:
1182 emit_op2(p
, OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1184 emit_op2(p
, OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1186 emit_op3(p
, OPCODE_MAD
, r
, 0, negate(tmp
), normal
, eye_hat
);
1188 emit_op2(p
, OPCODE_ADD
, tmp
, 0, r
, swizzle(id
,X
,Y
,W
,Z
));
1189 /* rx^2 + ry^2 + (rz+1)^2 */
1190 emit_op2(p
, OPCODE_DP3
, tmp
, 0, tmp
, tmp
);
1192 emit_op1(p
, OPCODE_RSQ
, tmp
, 0, tmp
);
1194 emit_op2(p
, OPCODE_MUL
, inv_m
, 0, tmp
, half
);
1196 emit_op3(p
, OPCODE_MAD
, dest
, writemask
, r
, inv_m
, half
);
1198 release_temp(p
, tmp
);
1200 release_temp(p
, inv_m
);
1204 static void build_texture_transform( struct tnl_program
*p
)
1208 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
1210 if (!(p
->state
->fragprog_inputs_read
& (FRAG_BIT_TEX0
<<i
)))
1213 if (p
->state
->unit
[i
].texgen_enabled
||
1214 p
->state
->unit
[i
].texmat_enabled
) {
1216 GLuint texmat_enabled
= p
->state
->unit
[i
].texmat_enabled
;
1217 struct ureg out
= register_output(p
, VERT_RESULT_TEX0
+ i
);
1218 struct ureg out_texgen
= undef
;
1220 if (p
->state
->unit
[i
].texgen_enabled
) {
1221 GLuint copy_mask
= 0;
1222 GLuint sphere_mask
= 0;
1223 GLuint reflect_mask
= 0;
1224 GLuint normal_mask
= 0;
1228 out_texgen
= get_temp(p
);
1232 modes
[0] = p
->state
->unit
[i
].texgen_mode0
;
1233 modes
[1] = p
->state
->unit
[i
].texgen_mode1
;
1234 modes
[2] = p
->state
->unit
[i
].texgen_mode2
;
1235 modes
[3] = p
->state
->unit
[i
].texgen_mode3
;
1237 for (j
= 0; j
< 4; j
++) {
1239 case TXG_OBJ_LINEAR
: {
1240 struct ureg obj
= register_input(p
, VERT_ATTRIB_POS
);
1242 register_param3(p
, STATE_TEXGEN
, i
,
1243 STATE_TEXGEN_OBJECT_S
+ j
);
1245 emit_op2(p
, OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1249 case TXG_EYE_LINEAR
: {
1250 struct ureg eye
= get_eye_position(p
);
1252 register_param3(p
, STATE_TEXGEN
, i
,
1253 STATE_TEXGEN_EYE_S
+ j
);
1255 emit_op2(p
, OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1259 case TXG_SPHERE_MAP
:
1260 sphere_mask
|= WRITEMASK_X
<< j
;
1262 case TXG_REFLECTION_MAP
:
1263 reflect_mask
|= WRITEMASK_X
<< j
;
1265 case TXG_NORMAL_MAP
:
1266 normal_mask
|= WRITEMASK_X
<< j
;
1269 copy_mask
|= WRITEMASK_X
<< j
;
1276 build_sphere_texgen(p
, out_texgen
, sphere_mask
);
1280 build_reflect_texgen(p
, out_texgen
, reflect_mask
);
1284 struct ureg normal
= get_eye_normal(p
);
1285 emit_op1(p
, OPCODE_MOV
, out_texgen
, normal_mask
, normal
);
1289 struct ureg in
= register_input(p
, VERT_ATTRIB_TEX0
+i
);
1290 emit_op1(p
, OPCODE_MOV
, out_texgen
, copy_mask
, in
);
1294 if (texmat_enabled
) {
1295 struct ureg texmat
[4];
1296 struct ureg in
= (!is_undef(out_texgen
) ?
1298 register_input(p
, VERT_ATTRIB_TEX0
+i
));
1300 register_matrix_param6( p
, STATE_MATRIX
, STATE_TEXTURE
, i
,
1301 0, 3, STATE_MATRIX
, texmat
);
1302 emit_matrix_transform_vec4( p
, out
, texmat
, in
);
1305 register_matrix_param6( p
, STATE_MATRIX
, STATE_TEXTURE
, i
,
1306 0, 3, STATE_MATRIX_TRANSPOSE
, texmat
);
1307 emit_transpose_matrix_transform_vec4( p
, out
, texmat
, in
);
1314 emit_passthrough(p
, VERT_ATTRIB_TEX0
+i
, VERT_RESULT_TEX0
+i
);
1320 /* Seems like it could be tighter:
1322 static void build_pointsize( struct tnl_program
*p
)
1324 struct ureg eye
= get_eye_position(p
);
1325 struct ureg state_size
= register_param1(p
, STATE_POINT_SIZE
);
1326 struct ureg state_attenuation
= register_param1(p
, STATE_POINT_ATTENUATION
);
1327 struct ureg out
= register_output(p
, VERT_RESULT_PSIZ
);
1328 struct ureg ut
= get_temp(p
);
1330 /* 1, -Z, Z * Z, 1 */
1331 emit_op1(p
, OPCODE_MOV
, ut
, 0, swizzle1(get_identity_param(p
), W
));
1332 emit_op2(p
, OPCODE_MUL
, ut
, WRITEMASK_YZ
, ut
, negate(swizzle1(eye
, Z
)));
1333 emit_op2(p
, OPCODE_MUL
, ut
, WRITEMASK_Z
, ut
, negate(swizzle1(eye
, Z
)));
1336 /* p1 + p2 * dist + p3 * dist * dist, 0 */
1337 emit_op2(p
, OPCODE_DP3
, ut
, 0, ut
, state_attenuation
);
1340 emit_op1(p
, OPCODE_RCP
, ut
, 0, ut
);
1342 /* out = pointSize / factor */
1343 emit_op2(p
, OPCODE_MUL
, out
, WRITEMASK_X
, ut
, state_size
);
1345 release_temp(p
, ut
);
1348 static void build_tnl_program( struct tnl_program
*p
)
1349 { /* Emit the program, starting with modelviewproject:
1353 /* Lighting calculations:
1355 if (p
->state
->fragprog_inputs_read
& (FRAG_BIT_COL0
|FRAG_BIT_COL1
)) {
1356 if (p
->state
->light_global_enabled
)
1359 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
1360 emit_passthrough(p
, VERT_ATTRIB_COLOR0
, VERT_RESULT_COL0
);
1362 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL1
)
1363 emit_passthrough(p
, VERT_ATTRIB_COLOR1
, VERT_RESULT_COL1
);
1367 if ((p
->state
->fragprog_inputs_read
& FRAG_BIT_FOGC
) ||
1368 p
->state
->fog_mode
!= FOG_NONE
)
1371 if (p
->state
->fragprog_inputs_read
& FRAG_BITS_TEX_ANY
)
1372 build_texture_transform(p
);
1374 if (p
->state
->point_attenuated
)
1379 emit_op1(p
, OPCODE_END
, undef
, 0, undef
);
1384 _mesa_printf ("\n");
1390 create_new_program( const struct state_key
*key
,
1391 struct gl_vertex_program
*program
,
1394 struct tnl_program p
;
1396 _mesa_memset(&p
, 0, sizeof(p
));
1398 p
.program
= program
;
1399 p
.eye_position
= undef
;
1400 p
.eye_position_normalized
= undef
;
1401 p
.eye_normal
= undef
;
1405 if (max_temps
>= sizeof(int) * 8)
1406 p
.temp_reserved
= 0;
1408 p
.temp_reserved
= ~((1<<max_temps
)-1);
1410 p
.program
->Base
.Instructions
1411 = (struct prog_instruction
*) MALLOC(sizeof(struct prog_instruction
) * MAX_INSN
);
1412 p
.program
->Base
.String
= 0;
1413 p
.program
->Base
.NumInstructions
=
1414 p
.program
->Base
.NumTemporaries
=
1415 p
.program
->Base
.NumParameters
=
1416 p
.program
->Base
.NumAttributes
= p
.program
->Base
.NumAddressRegs
= 0;
1417 p
.program
->Base
.Parameters
= _mesa_new_parameter_list();
1418 p
.program
->Base
.InputsRead
= 0;
1419 p
.program
->Base
.OutputsWritten
= 0;
1421 build_tnl_program( &p
);
1424 static void *search_cache( struct tnl_cache
*cache
,
1429 struct tnl_cache_item
*c
;
1431 for (c
= cache
->items
[hash
% cache
->size
]; c
; c
= c
->next
) {
1432 if (c
->hash
== hash
&& _mesa_memcmp(c
->key
, key
, keysize
) == 0)
1439 static void rehash( struct tnl_cache
*cache
)
1441 struct tnl_cache_item
**items
;
1442 struct tnl_cache_item
*c
, *next
;
1445 size
= cache
->size
* 3;
1446 items
= (struct tnl_cache_item
**) _mesa_malloc(size
* sizeof(*items
));
1447 _mesa_memset(items
, 0, size
* sizeof(*items
));
1449 for (i
= 0; i
< cache
->size
; i
++)
1450 for (c
= cache
->items
[i
]; c
; c
= next
) {
1452 c
->next
= items
[c
->hash
% size
];
1453 items
[c
->hash
% size
] = c
;
1457 cache
->items
= items
;
1461 static void cache_item( struct tnl_cache
*cache
,
1466 struct tnl_cache_item
*c
= (struct tnl_cache_item
*) _mesa_malloc(sizeof(*c
));
1471 if (++cache
->n_items
> cache
->size
* 1.5)
1474 c
->next
= cache
->items
[hash
% cache
->size
];
1475 cache
->items
[hash
% cache
->size
] = c
;
1478 static GLuint
hash_key( struct state_key
*key
)
1480 GLuint
*ikey
= (GLuint
*)key
;
1483 /* I'm sure this can be improved on, but speed is important:
1485 for (i
= 0; i
< sizeof(*key
)/sizeof(GLuint
); i
++)
1491 void _tnl_UpdateFixedFunctionProgram( GLcontext
*ctx
)
1493 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1494 struct state_key
*key
;
1496 const struct gl_vertex_program
*prev
= ctx
->VertexProgram
._Current
;
1498 if (ctx
->VertexProgram
._Enabled
== GL_FALSE
) {
1499 /* Grab all the relevent state and put it in a single structure:
1501 key
= make_state_key(ctx
);
1502 hash
= hash_key(key
);
1504 /* Look for an already-prepared program for this state:
1506 ctx
->_TnlProgram
= (struct gl_vertex_program
*)
1507 search_cache( tnl
->vp_cache
, hash
, key
, sizeof(*key
) );
1509 /* OK, we'll have to build a new one:
1511 if (!ctx
->_TnlProgram
) {
1513 _mesa_printf("Build new TNL program\n");
1515 ctx
->_TnlProgram
= (struct gl_vertex_program
*)
1516 ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
1518 create_new_program( key
, ctx
->_TnlProgram
,
1519 ctx
->Const
.VertexProgram
.MaxTemps
);
1521 if (ctx
->Driver
.ProgramStringNotify
)
1522 ctx
->Driver
.ProgramStringNotify( ctx
, GL_VERTEX_PROGRAM_ARB
,
1523 &ctx
->_TnlProgram
->Base
);
1525 cache_item(tnl
->vp_cache
, hash
, key
, ctx
->_TnlProgram
);
1530 _mesa_printf("Found existing TNL program for key %x\n", hash
);
1532 ctx
->VertexProgram
._Current
= ctx
->_TnlProgram
;
1535 ctx
->VertexProgram
._Current
= ctx
->VertexProgram
.Current
;
1538 /* Tell the driver about the change. Could define a new target for
1541 if (ctx
->VertexProgram
._Current
!= prev
&&
1542 ctx
->Driver
.BindProgram
)
1543 ctx
->Driver
.BindProgram(ctx
, GL_VERTEX_PROGRAM_ARB
,
1544 (struct gl_program
*) ctx
->VertexProgram
._Current
);
1547 void _tnl_ProgramCacheInit( GLcontext
*ctx
)
1549 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1551 tnl
->vp_cache
= (struct tnl_cache
*) MALLOC(sizeof(*tnl
->vp_cache
));
1552 tnl
->vp_cache
->size
= 17;
1553 tnl
->vp_cache
->n_items
= 0;
1554 tnl
->vp_cache
->items
= (struct tnl_cache_item
**)
1555 _mesa_calloc(tnl
->vp_cache
->size
* sizeof(*tnl
->vp_cache
->items
));
1558 void _tnl_ProgramCacheDestroy( GLcontext
*ctx
)
1560 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1561 struct tnl_cache_item
*c
, *next
;
1564 for (i
= 0; i
< tnl
->vp_cache
->size
; i
++)
1565 for (c
= tnl
->vp_cache
->items
[i
]; c
; c
= next
) {
1572 FREE(tnl
->vp_cache
->items
);
1573 FREE(tnl
->vp_cache
);