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 "shader/program.h"
37 #include "shader/prog_instruction.h"
38 #include "shader/prog_parameter.h"
39 #include "shader/prog_print.h"
40 #include "shader/prog_statevars.h"
41 #include "t_context.h" /* NOTE: very light dependency on this */
42 #include "t_vp_build.h"
46 unsigned light_global_enabled
:1;
47 unsigned light_local_viewer
:1;
48 unsigned light_twoside
:1;
49 unsigned light_color_material
:1;
50 unsigned light_color_material_mask
:12;
51 unsigned light_material_mask
:12;
54 unsigned rescale_normals
:1;
55 unsigned fog_source_is_depth
:1;
56 unsigned tnl_do_vertex_fog
:1;
57 unsigned separate_specular
:1;
59 unsigned point_attenuated
:1;
60 unsigned texture_enabled_global
:1;
61 unsigned fragprog_inputs_read
:12;
64 unsigned light_enabled
:1;
65 unsigned light_eyepos3_is_zero
:1;
66 unsigned light_spotcutoff_is_180
:1;
67 unsigned light_attenuated
:1;
68 unsigned texunit_really_enabled
:1;
69 unsigned texmat_enabled
:1;
70 unsigned texgen_enabled
:4;
71 unsigned texgen_mode0
:4;
72 unsigned texgen_mode1
:4;
73 unsigned texgen_mode2
:4;
74 unsigned texgen_mode3
:4;
85 static GLuint
translate_fog_mode( GLenum mode
)
88 case GL_LINEAR
: return FOG_LINEAR
;
89 case GL_EXP
: return FOG_EXP
;
90 case GL_EXP2
: return FOG_EXP2
;
91 default: return FOG_NONE
;
96 #define TXG_OBJ_LINEAR 1
97 #define TXG_EYE_LINEAR 2
98 #define TXG_SPHERE_MAP 3
99 #define TXG_REFLECTION_MAP 4
100 #define TXG_NORMAL_MAP 5
102 static GLuint
translate_texgen( GLboolean enabled
, GLenum mode
)
108 case GL_OBJECT_LINEAR
: return TXG_OBJ_LINEAR
;
109 case GL_EYE_LINEAR
: return TXG_EYE_LINEAR
;
110 case GL_SPHERE_MAP
: return TXG_SPHERE_MAP
;
111 case GL_REFLECTION_MAP_NV
: return TXG_REFLECTION_MAP
;
112 case GL_NORMAL_MAP_NV
: return TXG_NORMAL_MAP
;
113 default: return TXG_NONE
;
117 static struct state_key
*make_state_key( GLcontext
*ctx
)
119 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
120 struct vertex_buffer
*VB
= &tnl
->vb
;
121 const struct gl_fragment_program
*fp
;
122 struct state_key
*key
= CALLOC_STRUCT(state_key
);
125 if (ctx
->Shader
.CurrentProgram
&&
126 ctx
->Shader
.CurrentProgram
->LinkStatus
&&
127 ctx
->Shader
.CurrentProgram
->FragmentProgram
)
128 fp
= ctx
->Shader
.CurrentProgram
->FragmentProgram
;
130 fp
= ctx
->FragmentProgram
._Current
;
132 /* This now relies on texenvprogram.c being active:
136 key
->fragprog_inputs_read
= fp
->Base
.InputsRead
;
138 key
->separate_specular
= (ctx
->Light
.Model
.ColorControl
==
139 GL_SEPARATE_SPECULAR_COLOR
);
141 if (ctx
->Light
.Enabled
) {
142 key
->light_global_enabled
= 1;
144 if (ctx
->Light
.Model
.LocalViewer
)
145 key
->light_local_viewer
= 1;
147 if (ctx
->Light
.Model
.TwoSide
)
148 key
->light_twoside
= 1;
150 if (ctx
->Light
.ColorMaterialEnabled
) {
151 key
->light_color_material
= 1;
152 key
->light_color_material_mask
= ctx
->Light
.ColorMaterialBitmask
;
155 for (i
= _TNL_FIRST_MAT
; i
<= _TNL_LAST_MAT
; i
++)
156 if (VB
->AttribPtr
[i
] && VB
->AttribPtr
[i
]->stride
)
157 key
->light_material_mask
|= 1<<(i
-_TNL_ATTRIB_MAT_FRONT_AMBIENT
);
159 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
160 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
162 if (light
->Enabled
) {
163 key
->unit
[i
].light_enabled
= 1;
165 if (light
->EyePosition
[3] == 0.0)
166 key
->unit
[i
].light_eyepos3_is_zero
= 1;
168 if (light
->SpotCutoff
== 180.0)
169 key
->unit
[i
].light_spotcutoff_is_180
= 1;
171 if (light
->ConstantAttenuation
!= 1.0 ||
172 light
->LinearAttenuation
!= 0.0 ||
173 light
->QuadraticAttenuation
!= 0.0)
174 key
->unit
[i
].light_attenuated
= 1;
179 if (ctx
->Transform
.Normalize
)
182 if (ctx
->Transform
.RescaleNormals
)
183 key
->rescale_normals
= 1;
185 key
->fog_mode
= translate_fog_mode(fp
->FogOption
);
187 if (ctx
->Fog
.FogCoordinateSource
== GL_FRAGMENT_DEPTH_EXT
)
188 key
->fog_source_is_depth
= 1;
190 if (tnl
->_DoVertexFog
)
191 key
->tnl_do_vertex_fog
= 1;
193 if (ctx
->Point
._Attenuated
)
194 key
->point_attenuated
= 1;
196 if (ctx
->Texture
._TexGenEnabled
||
197 ctx
->Texture
._TexMatEnabled
||
198 ctx
->Texture
._EnabledUnits
)
199 key
->texture_enabled_global
= 1;
201 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
202 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
204 if (texUnit
->_ReallyEnabled
)
205 key
->unit
[i
].texunit_really_enabled
= 1;
207 if (ctx
->Texture
._TexMatEnabled
& ENABLE_TEXMAT(i
))
208 key
->unit
[i
].texmat_enabled
= 1;
210 if (texUnit
->TexGenEnabled
) {
211 key
->unit
[i
].texgen_enabled
= 1;
213 key
->unit
[i
].texgen_mode0
=
214 translate_texgen( texUnit
->TexGenEnabled
& (1<<0),
216 key
->unit
[i
].texgen_mode1
=
217 translate_texgen( texUnit
->TexGenEnabled
& (1<<1),
219 key
->unit
[i
].texgen_mode2
=
220 translate_texgen( texUnit
->TexGenEnabled
& (1<<2),
222 key
->unit
[i
].texgen_mode3
=
223 translate_texgen( texUnit
->TexGenEnabled
& (1<<3),
233 /* Very useful debugging tool - produces annotated listing of
234 * generated program with line/function references for each
235 * instruction back into this file:
237 #define DISASSEM (MESA_VERBOSE&VERBOSE_DISASSEM)
239 /* Should be tunable by the driver - do we want to do matrix
240 * multiplications with DP4's or with MUL/MAD's? SSE works better
241 * with the latter, drivers may differ.
247 /* Use uregs to represent registers internally, translate to Mesa's
248 * expected formats on emit.
250 * NOTE: These are passed by value extensively in this file rather
251 * than as usual by pointer reference. If this disturbs you, try
252 * remembering they are just 32bits in size.
254 * GCC is smart enough to deal with these dword-sized structures in
255 * much the same way as if I had defined them as dwords and was using
256 * macros to access and set the fields. This is much nicer and easier
261 GLint idx
:8; /* relative addressing may be negative */
269 const struct state_key
*state
;
270 struct gl_vertex_program
*program
;
273 GLuint temp_reserved
;
275 struct ureg eye_position
;
276 struct ureg eye_position_normalized
;
277 struct ureg eye_normal
;
278 struct ureg identity
;
281 GLuint color_materials
;
285 static const struct ureg undef
= {
303 static struct ureg
make_ureg(GLuint file
, GLint idx
)
309 reg
.swz
= SWIZZLE_NOOP
;
316 static struct ureg
negate( struct ureg reg
)
323 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
325 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
328 GET_SWZ(reg
.swz
, w
));
333 static struct ureg
swizzle1( struct ureg reg
, int x
)
335 return swizzle(reg
, x
, x
, x
, x
);
338 static struct ureg
get_temp( struct tnl_program
*p
)
340 int bit
= _mesa_ffs( ~p
->temp_in_use
);
342 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
346 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
347 p
->program
->Base
.NumTemporaries
= bit
;
349 p
->temp_in_use
|= 1<<(bit
-1);
350 return make_ureg(PROGRAM_TEMPORARY
, bit
-1);
353 static struct ureg
reserve_temp( struct tnl_program
*p
)
355 struct ureg temp
= get_temp( p
);
356 p
->temp_reserved
|= 1<<temp
.idx
;
360 static void release_temp( struct tnl_program
*p
, struct ureg reg
)
362 if (reg
.file
== PROGRAM_TEMPORARY
) {
363 p
->temp_in_use
&= ~(1<<reg
.idx
);
364 p
->temp_in_use
|= p
->temp_reserved
; /* can't release reserved temps */
368 static void release_temps( struct tnl_program
*p
)
370 p
->temp_in_use
= p
->temp_reserved
;
375 static struct ureg
register_input( struct tnl_program
*p
, GLuint input
)
377 p
->program
->Base
.InputsRead
|= (1<<input
);
378 return make_ureg(PROGRAM_INPUT
, input
);
381 static struct ureg
register_output( struct tnl_program
*p
, GLuint output
)
383 p
->program
->Base
.OutputsWritten
|= (1<<output
);
384 return make_ureg(PROGRAM_OUTPUT
, output
);
387 static struct ureg
register_const4f( struct tnl_program
*p
,
400 idx
= _mesa_add_unnamed_constant( p
->program
->Base
.Parameters
, values
, 4,
402 ASSERT(swizzle
== SWIZZLE_NOOP
);
403 return make_ureg(PROGRAM_STATE_VAR
, idx
);
406 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
407 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
408 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
409 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
411 static GLboolean
is_undef( struct ureg reg
)
413 return reg
.file
== PROGRAM_UNDEFINED
;
416 static struct ureg
get_identity_param( struct tnl_program
*p
)
418 if (is_undef(p
->identity
))
419 p
->identity
= register_const4f(p
, 0,0,0,1);
424 static struct ureg
register_param5(struct tnl_program
*p
,
431 gl_state_index tokens
[STATE_LENGTH
];
438 idx
= _mesa_add_state_reference( p
->program
->Base
.Parameters
, tokens
);
439 return make_ureg(PROGRAM_STATE_VAR
, idx
);
443 #define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
444 #define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
445 #define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
446 #define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
449 static void register_matrix_param5( struct tnl_program
*p
,
450 GLint s0
, /* modelview, projection, etc */
451 GLint s1
, /* texture matrix number */
452 GLint s2
, /* first row */
453 GLint s3
, /* last row */
454 GLint s4
, /* inverse, transpose, etc */
455 struct ureg
*matrix
)
459 /* This is a bit sad as the support is there to pull the whole
460 * matrix out in one go:
462 for (i
= 0; i
<= s3
- s2
; i
++)
463 matrix
[i
] = register_param5( p
, s0
, s1
, i
, i
, s4
);
467 static void emit_arg( struct prog_src_register
*src
,
470 src
->File
= reg
.file
;
471 src
->Index
= reg
.idx
;
472 src
->Swizzle
= reg
.swz
;
473 src
->NegateBase
= reg
.negate
? NEGATE_XYZW
: 0;
479 static void emit_dst( struct prog_dst_register
*dst
,
480 struct ureg reg
, GLuint mask
)
482 dst
->File
= reg
.file
;
483 dst
->Index
= reg
.idx
;
484 /* allow zero as a shorthand for xyzw */
485 dst
->WriteMask
= mask
? mask
: WRITEMASK_XYZW
;
486 dst
->CondMask
= COND_TR
;
487 dst
->CondSwizzle
= 0;
492 static void debug_insn( struct prog_instruction
*inst
, const char *fn
,
496 static const char *last_fn
;
500 _mesa_printf("%s:\n", fn
);
503 _mesa_printf("%d:\t", line
);
504 _mesa_print_instruction(inst
);
509 static void emit_op3fn(struct tnl_program
*p
,
519 GLuint nr
= p
->program
->Base
.NumInstructions
++;
520 struct prog_instruction
*inst
= &p
->program
->Base
.Instructions
[nr
];
522 if (p
->program
->Base
.NumInstructions
> MAX_INSN
) {
523 _mesa_problem(0, "Out of instructions in emit_op3fn\n");
527 inst
->Opcode
= (enum prog_opcode
) op
;
531 emit_arg( &inst
->SrcReg
[0], src0
);
532 emit_arg( &inst
->SrcReg
[1], src1
);
533 emit_arg( &inst
->SrcReg
[2], src2
);
535 emit_dst( &inst
->DstReg
, dest
, mask
);
537 debug_insn(inst
, fn
, line
);
541 #define emit_op3(p, op, dst, mask, src0, src1, src2) \
542 emit_op3fn(p, op, dst, mask, src0, src1, src2, __FUNCTION__, __LINE__)
544 #define emit_op2(p, op, dst, mask, src0, src1) \
545 emit_op3fn(p, op, dst, mask, src0, src1, undef, __FUNCTION__, __LINE__)
547 #define emit_op1(p, op, dst, mask, src0) \
548 emit_op3fn(p, op, dst, mask, src0, undef, undef, __FUNCTION__, __LINE__)
551 static struct ureg
make_temp( struct tnl_program
*p
, struct ureg reg
)
553 if (reg
.file
== PROGRAM_TEMPORARY
&&
554 !(p
->temp_reserved
& (1<<reg
.idx
)))
557 struct ureg temp
= get_temp(p
);
558 emit_op1(p
, OPCODE_MOV
, temp
, 0, reg
);
564 /* Currently no tracking performed of input/output/register size or
565 * active elements. Could be used to reduce these operations, as
566 * could the matrix type.
568 static void emit_matrix_transform_vec4( struct tnl_program
*p
,
570 const struct ureg
*mat
,
573 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_X
, src
, mat
[0]);
574 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_Y
, src
, mat
[1]);
575 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_Z
, src
, mat
[2]);
576 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_W
, src
, mat
[3]);
579 /* This version is much easier to implement if writemasks are not
580 * supported natively on the target or (like SSE), the target doesn't
581 * have a clean/obvious dotproduct implementation.
583 static void emit_transpose_matrix_transform_vec4( struct tnl_program
*p
,
585 const struct ureg
*mat
,
590 if (dest
.file
!= PROGRAM_TEMPORARY
)
595 emit_op2(p
, OPCODE_MUL
, tmp
, 0, swizzle1(src
,X
), mat
[0]);
596 emit_op3(p
, OPCODE_MAD
, tmp
, 0, swizzle1(src
,Y
), mat
[1], tmp
);
597 emit_op3(p
, OPCODE_MAD
, tmp
, 0, swizzle1(src
,Z
), mat
[2], tmp
);
598 emit_op3(p
, OPCODE_MAD
, dest
, 0, swizzle1(src
,W
), mat
[3], tmp
);
600 if (dest
.file
!= PROGRAM_TEMPORARY
)
601 release_temp(p
, tmp
);
604 static void emit_matrix_transform_vec3( struct tnl_program
*p
,
606 const struct ureg
*mat
,
609 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_X
, src
, mat
[0]);
610 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_Y
, src
, mat
[1]);
611 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_Z
, src
, mat
[2]);
615 static void emit_normalize_vec3( struct tnl_program
*p
,
619 struct ureg tmp
= get_temp(p
);
620 emit_op2(p
, OPCODE_DP3
, tmp
, 0, src
, src
);
621 emit_op1(p
, OPCODE_RSQ
, tmp
, 0, tmp
);
622 emit_op2(p
, OPCODE_MUL
, dest
, 0, src
, tmp
);
623 release_temp(p
, tmp
);
626 static void emit_passthrough( struct tnl_program
*p
,
630 struct ureg out
= register_output(p
, output
);
631 emit_op1(p
, OPCODE_MOV
, out
, 0, register_input(p
, input
));
634 static struct ureg
get_eye_position( struct tnl_program
*p
)
636 if (is_undef(p
->eye_position
)) {
637 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
638 struct ureg modelview
[4];
640 p
->eye_position
= reserve_temp(p
);
643 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 3,
646 emit_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
649 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 3,
650 STATE_MATRIX_TRANSPOSE
, modelview
);
652 emit_transpose_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
656 return p
->eye_position
;
660 static struct ureg
get_eye_position_normalized( struct tnl_program
*p
)
662 if (is_undef(p
->eye_position_normalized
)) {
663 struct ureg eye
= get_eye_position(p
);
664 p
->eye_position_normalized
= reserve_temp(p
);
665 emit_normalize_vec3(p
, p
->eye_position_normalized
, eye
);
668 return p
->eye_position_normalized
;
672 static struct ureg
get_eye_normal( struct tnl_program
*p
)
674 if (is_undef(p
->eye_normal
)) {
675 struct ureg normal
= register_input(p
, VERT_ATTRIB_NORMAL
);
676 struct ureg mvinv
[3];
678 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 2,
679 STATE_MATRIX_INVTRANS
, mvinv
);
681 p
->eye_normal
= reserve_temp(p
);
683 /* Transform to eye space:
685 emit_matrix_transform_vec3( p
, p
->eye_normal
, mvinv
, normal
);
687 /* Normalize/Rescale:
689 if (p
->state
->normalize
) {
690 emit_normalize_vec3( p
, p
->eye_normal
, p
->eye_normal
);
692 else if (p
->state
->rescale_normals
) {
693 struct ureg rescale
= register_param2(p
, STATE_INTERNAL
,
696 emit_op2( p
, OPCODE_MUL
, p
->eye_normal
, 0, normal
,
697 swizzle1(rescale
, X
));
701 return p
->eye_normal
;
706 static void build_hpos( struct tnl_program
*p
)
708 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
709 struct ureg hpos
= register_output( p
, VERT_RESULT_HPOS
);
713 register_matrix_param5( p
, STATE_MVP_MATRIX
, 0, 0, 3,
715 emit_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
718 register_matrix_param5( p
, STATE_MVP_MATRIX
, 0, 0, 3,
719 STATE_MATRIX_TRANSPOSE
, mvp
);
720 emit_transpose_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
725 static GLuint
material_attrib( GLuint side
, GLuint property
)
727 return ((property
- STATE_AMBIENT
) * 2 +
731 /* Get a bitmask of which material values vary on a per-vertex basis.
733 static void set_material_flags( struct tnl_program
*p
)
735 p
->color_materials
= 0;
738 if (p
->state
->light_color_material
) {
740 p
->color_materials
= p
->state
->light_color_material_mask
;
743 p
->materials
|= p
->state
->light_material_mask
;
747 static struct ureg
get_material( struct tnl_program
*p
, GLuint side
,
750 GLuint attrib
= material_attrib(side
, property
);
752 if (p
->color_materials
& (1<<attrib
))
753 return register_input(p
, VERT_ATTRIB_COLOR0
);
754 else if (p
->materials
& (1<<attrib
))
755 return register_input( p
, attrib
+ _TNL_ATTRIB_MAT_FRONT_AMBIENT
);
757 return register_param3( p
, STATE_MATERIAL
, side
, property
);
760 #define SCENE_COLOR_BITS(side) (( MAT_BIT_FRONT_EMISSION | \
761 MAT_BIT_FRONT_AMBIENT | \
762 MAT_BIT_FRONT_DIFFUSE) << (side))
764 /* Either return a precalculated constant value or emit code to
765 * calculate these values dynamically in the case where material calls
766 * are present between begin/end pairs.
768 * Probably want to shift this to the program compilation phase - if
769 * we always emitted the calculation here, a smart compiler could
770 * detect that it was constant (given a certain set of inputs), and
771 * lift it out of the main loop. That way the programs created here
772 * would be independent of the vertex_buffer details.
774 static struct ureg
get_scenecolor( struct tnl_program
*p
, GLuint side
)
776 if (p
->materials
& SCENE_COLOR_BITS(side
)) {
777 struct ureg lm_ambient
= register_param1(p
, STATE_LIGHTMODEL_AMBIENT
);
778 struct ureg material_emission
= get_material(p
, side
, STATE_EMISSION
);
779 struct ureg material_ambient
= get_material(p
, side
, STATE_AMBIENT
);
780 struct ureg material_diffuse
= get_material(p
, side
, STATE_DIFFUSE
);
781 struct ureg tmp
= make_temp(p
, material_diffuse
);
782 emit_op3(p
, OPCODE_MAD
, tmp
, WRITEMASK_XYZ
, lm_ambient
,
783 material_ambient
, material_emission
);
787 return register_param2( p
, STATE_LIGHTMODEL_SCENECOLOR
, side
);
791 static struct ureg
get_lightprod( struct tnl_program
*p
, GLuint light
,
792 GLuint side
, GLuint property
)
794 GLuint attrib
= material_attrib(side
, property
);
795 if (p
->materials
& (1<<attrib
)) {
796 struct ureg light_value
=
797 register_param3(p
, STATE_LIGHT
, light
, property
);
798 struct ureg material_value
= get_material(p
, side
, property
);
799 struct ureg tmp
= get_temp(p
);
800 emit_op2(p
, OPCODE_MUL
, tmp
, 0, light_value
, material_value
);
804 return register_param4(p
, STATE_LIGHTPROD
, light
, side
, property
);
807 static struct ureg
calculate_light_attenuation( struct tnl_program
*p
,
812 struct ureg attenuation
= register_param3(p
, STATE_LIGHT
, i
,
814 struct ureg att
= get_temp(p
);
816 /* Calculate spot attenuation:
818 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
819 struct ureg spot_dir_norm
= register_param3(p
, STATE_INTERNAL
,
820 STATE_SPOT_DIR_NORMALIZED
, i
);
821 struct ureg spot
= get_temp(p
);
822 struct ureg slt
= get_temp(p
);
824 emit_op2(p
, OPCODE_DP3
, spot
, 0, negate(VPpli
), spot_dir_norm
);
825 emit_op2(p
, OPCODE_SLT
, slt
, 0, swizzle1(spot_dir_norm
,W
), spot
);
826 emit_op2(p
, OPCODE_POW
, spot
, 0, spot
, swizzle1(attenuation
, W
));
827 emit_op2(p
, OPCODE_MUL
, att
, 0, slt
, spot
);
829 release_temp(p
, spot
);
830 release_temp(p
, slt
);
833 /* Calculate distance attenuation:
835 if (p
->state
->unit
[i
].light_attenuated
) {
838 emit_op1(p
, OPCODE_RCP
, dist
, WRITEMASK_YZ
, dist
);
840 emit_op2(p
, OPCODE_MUL
, dist
, WRITEMASK_XZ
, dist
, swizzle1(dist
,Y
));
842 emit_op2(p
, OPCODE_DP3
, dist
, 0, attenuation
, dist
);
844 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
846 emit_op1(p
, OPCODE_RCP
, dist
, 0, dist
);
847 /* spot-atten * dist-atten */
848 emit_op2(p
, OPCODE_MUL
, att
, 0, dist
, att
);
851 emit_op1(p
, OPCODE_RCP
, att
, 0, dist
);
862 /* Need to add some addtional parameters to allow lighting in object
863 * space - STATE_SPOT_DIRECTION and STATE_HALF_VECTOR implicitly assume eye
866 static void build_lighting( struct tnl_program
*p
)
868 const GLboolean twoside
= p
->state
->light_twoside
;
869 const GLboolean separate
= p
->state
->separate_specular
;
870 GLuint nr_lights
= 0, count
= 0;
871 struct ureg normal
= get_eye_normal(p
);
872 struct ureg lit
= get_temp(p
);
873 struct ureg dots
= get_temp(p
);
874 struct ureg _col0
= undef
, _col1
= undef
;
875 struct ureg _bfc0
= undef
, _bfc1
= undef
;
878 for (i
= 0; i
< MAX_LIGHTS
; i
++)
879 if (p
->state
->unit
[i
].light_enabled
)
882 set_material_flags(p
);
885 struct ureg shininess
= get_material(p
, 0, STATE_SHININESS
);
886 emit_op1(p
, OPCODE_MOV
, dots
, WRITEMASK_W
, swizzle1(shininess
,X
));
887 release_temp(p
, shininess
);
889 _col0
= make_temp(p
, get_scenecolor(p
, 0));
891 _col1
= make_temp(p
, get_identity_param(p
));
898 struct ureg shininess
= get_material(p
, 1, STATE_SHININESS
);
899 emit_op1(p
, OPCODE_MOV
, dots
, WRITEMASK_Z
,
900 negate(swizzle1(shininess
,X
)));
901 release_temp(p
, shininess
);
903 _bfc0
= make_temp(p
, get_scenecolor(p
, 1));
905 _bfc1
= make_temp(p
, get_identity_param(p
));
911 /* If no lights, still need to emit the scenecolor.
914 struct ureg res0
= register_output( p
, VERT_RESULT_COL0
);
915 emit_op1(p
, OPCODE_MOV
, res0
, 0, _col0
);
919 struct ureg res1
= register_output( p
, VERT_RESULT_COL1
);
920 emit_op1(p
, OPCODE_MOV
, res1
, 0, _col1
);
924 struct ureg res0
= register_output( p
, VERT_RESULT_BFC0
);
925 emit_op1(p
, OPCODE_MOV
, res0
, 0, _bfc0
);
928 if (twoside
&& separate
) {
929 struct ureg res1
= register_output( p
, VERT_RESULT_BFC1
);
930 emit_op1(p
, OPCODE_MOV
, res1
, 0, _bfc1
);
933 if (nr_lights
== 0) {
939 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
940 if (p
->state
->unit
[i
].light_enabled
) {
941 struct ureg half
= undef
;
942 struct ureg att
= undef
, VPpli
= undef
;
946 if (p
->state
->unit
[i
].light_eyepos3_is_zero
) {
947 /* Can used precomputed constants in this case.
948 * Attenuation never applies to infinite lights.
950 VPpli
= register_param3(p
, STATE_LIGHT
, i
,
951 STATE_POSITION_NORMALIZED
);
952 if (p
->state
->light_local_viewer
) {
953 struct ureg eye_hat
= get_eye_position_normalized(p
);
955 emit_op2(p
, OPCODE_SUB
, half
, 0, VPpli
, eye_hat
);
956 emit_normalize_vec3(p
, half
, half
);
958 half
= register_param3(p
, STATE_LIGHT
, i
, STATE_HALF_VECTOR
);
962 struct ureg Ppli
= register_param3(p
, STATE_LIGHT
, i
,
964 struct ureg V
= get_eye_position(p
);
965 struct ureg dist
= get_temp(p
);
970 /* Calulate VPpli vector
972 emit_op2(p
, OPCODE_SUB
, VPpli
, 0, Ppli
, V
);
974 /* Normalize VPpli. The dist value also used in
977 emit_op2(p
, OPCODE_DP3
, dist
, 0, VPpli
, VPpli
);
978 emit_op1(p
, OPCODE_RSQ
, dist
, 0, dist
);
979 emit_op2(p
, OPCODE_MUL
, VPpli
, 0, VPpli
, dist
);
982 /* Calculate attenuation:
984 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
||
985 p
->state
->unit
[i
].light_attenuated
) {
986 att
= calculate_light_attenuation(p
, i
, VPpli
, dist
);
990 /* Calculate viewer direction, or use infinite viewer:
992 if (p
->state
->light_local_viewer
) {
993 struct ureg eye_hat
= get_eye_position_normalized(p
);
994 emit_op2(p
, OPCODE_SUB
, half
, 0, VPpli
, eye_hat
);
997 struct ureg z_dir
= swizzle(get_identity_param(p
),X
,Y
,W
,Z
);
998 emit_op2(p
, OPCODE_ADD
, half
, 0, VPpli
, z_dir
);
1001 emit_normalize_vec3(p
, half
, half
);
1003 release_temp(p
, dist
);
1006 /* Calculate dot products:
1008 emit_op2(p
, OPCODE_DP3
, dots
, WRITEMASK_X
, normal
, VPpli
);
1009 emit_op2(p
, OPCODE_DP3
, dots
, WRITEMASK_Y
, normal
, half
);
1012 /* Front face lighting:
1015 struct ureg ambient
= get_lightprod(p
, i
, 0, STATE_AMBIENT
);
1016 struct ureg diffuse
= get_lightprod(p
, i
, 0, STATE_DIFFUSE
);
1017 struct ureg specular
= get_lightprod(p
, i
, 0, STATE_SPECULAR
);
1018 struct ureg res0
, res1
;
1019 GLuint mask0
, mask1
;
1021 emit_op1(p
, OPCODE_LIT
, lit
, 0, dots
);
1024 emit_op2(p
, OPCODE_MUL
, lit
, 0, lit
, att
);
1027 if (count
== nr_lights
) {
1029 mask0
= WRITEMASK_XYZ
;
1030 mask1
= WRITEMASK_XYZ
;
1031 res0
= register_output( p
, VERT_RESULT_COL0
);
1032 res1
= register_output( p
, VERT_RESULT_COL1
);
1036 mask1
= WRITEMASK_XYZ
;
1038 res1
= register_output( p
, VERT_RESULT_COL0
);
1047 emit_op3(p
, OPCODE_MAD
, _col0
, 0, swizzle1(lit
,X
), ambient
, _col0
);
1048 emit_op3(p
, OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _col0
);
1049 emit_op3(p
, OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _col1
);
1051 release_temp(p
, ambient
);
1052 release_temp(p
, diffuse
);
1053 release_temp(p
, specular
);
1056 /* Back face lighting:
1059 struct ureg ambient
= get_lightprod(p
, i
, 1, STATE_AMBIENT
);
1060 struct ureg diffuse
= get_lightprod(p
, i
, 1, STATE_DIFFUSE
);
1061 struct ureg specular
= get_lightprod(p
, i
, 1, STATE_SPECULAR
);
1062 struct ureg res0
, res1
;
1063 GLuint mask0
, mask1
;
1065 emit_op1(p
, OPCODE_LIT
, lit
, 0, negate(swizzle(dots
,X
,Y
,W
,Z
)));
1068 emit_op2(p
, OPCODE_MUL
, lit
, 0, lit
, att
);
1070 if (count
== nr_lights
) {
1072 mask0
= WRITEMASK_XYZ
;
1073 mask1
= WRITEMASK_XYZ
;
1074 res0
= register_output( p
, VERT_RESULT_BFC0
);
1075 res1
= register_output( p
, VERT_RESULT_BFC1
);
1079 mask1
= WRITEMASK_XYZ
;
1081 res1
= register_output( p
, VERT_RESULT_BFC0
);
1090 emit_op3(p
, OPCODE_MAD
, _bfc0
, 0, swizzle1(lit
,X
), ambient
, _bfc0
);
1091 emit_op3(p
, OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _bfc0
);
1092 emit_op3(p
, OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _bfc1
);
1094 release_temp(p
, ambient
);
1095 release_temp(p
, diffuse
);
1096 release_temp(p
, specular
);
1099 release_temp(p
, half
);
1100 release_temp(p
, VPpli
);
1101 release_temp(p
, att
);
1109 static void build_fog( struct tnl_program
*p
)
1111 struct ureg fog
= register_output(p
, VERT_RESULT_FOGC
);
1113 GLuint useabs
= p
->state
->fog_source_is_depth
&& p
->state
->fog_mode
&&
1114 (p
->state
->fog_mode
!= FOG_EXP2
);
1116 if (p
->state
->fog_source_is_depth
) {
1117 input
= swizzle1(get_eye_position(p
), Z
);
1120 input
= swizzle1(register_input(p
, VERT_ATTRIB_FOG
), X
);
1123 if (p
->state
->fog_mode
&& p
->state
->tnl_do_vertex_fog
) {
1124 struct ureg params
= register_param2(p
, STATE_INTERNAL
,
1125 STATE_FOG_PARAMS_OPTIMIZED
);
1126 struct ureg tmp
= get_temp(p
);
1129 emit_op1(p
, OPCODE_ABS
, tmp
, 0, input
);
1132 switch (p
->state
->fog_mode
) {
1134 struct ureg id
= get_identity_param(p
);
1135 emit_op3(p
, OPCODE_MAD
, tmp
, 0, useabs
? tmp
: input
,
1136 swizzle1(params
,X
), swizzle1(params
,Y
));
1137 emit_op2(p
, OPCODE_MAX
, tmp
, 0, tmp
, swizzle1(id
,X
)); /* saturate */
1138 emit_op2(p
, OPCODE_MIN
, fog
, WRITEMASK_X
, tmp
, swizzle1(id
,W
));
1142 emit_op2(p
, OPCODE_MUL
, tmp
, 0, useabs
? tmp
: input
,
1143 swizzle1(params
,Z
));
1144 emit_op1(p
, OPCODE_EX2
, fog
, WRITEMASK_X
, negate(tmp
));
1147 emit_op2(p
, OPCODE_MUL
, tmp
, 0, input
, swizzle1(params
,W
));
1148 emit_op2(p
, OPCODE_MUL
, tmp
, 0, tmp
, tmp
);
1149 emit_op1(p
, OPCODE_EX2
, fog
, WRITEMASK_X
, negate(tmp
));
1153 release_temp(p
, tmp
);
1156 /* results = incoming fog coords (compute fog per-fragment later)
1158 * KW: Is it really necessary to do anything in this case?
1160 emit_op1(p
, useabs
? OPCODE_ABS
: OPCODE_MOV
, fog
, WRITEMASK_X
, input
);
1164 static void build_reflect_texgen( struct tnl_program
*p
,
1168 struct ureg normal
= get_eye_normal(p
);
1169 struct ureg eye_hat
= get_eye_position_normalized(p
);
1170 struct ureg tmp
= get_temp(p
);
1173 emit_op2(p
, OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1175 emit_op2(p
, OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1177 emit_op3(p
, OPCODE_MAD
, dest
, writemask
, negate(tmp
), normal
, eye_hat
);
1179 release_temp(p
, tmp
);
1182 static void build_sphere_texgen( struct tnl_program
*p
,
1186 struct ureg normal
= get_eye_normal(p
);
1187 struct ureg eye_hat
= get_eye_position_normalized(p
);
1188 struct ureg tmp
= get_temp(p
);
1189 struct ureg half
= register_scalar_const(p
, .5);
1190 struct ureg r
= get_temp(p
);
1191 struct ureg inv_m
= get_temp(p
);
1192 struct ureg id
= get_identity_param(p
);
1194 /* Could share the above calculations, but it would be
1195 * a fairly odd state for someone to set (both sphere and
1196 * reflection active for different texture coordinate
1197 * components. Of course - if two texture units enable
1198 * reflect and/or sphere, things start to tilt in favour
1199 * of seperating this out:
1203 emit_op2(p
, OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1205 emit_op2(p
, OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1207 emit_op3(p
, OPCODE_MAD
, r
, 0, negate(tmp
), normal
, eye_hat
);
1209 emit_op2(p
, OPCODE_ADD
, tmp
, 0, r
, swizzle(id
,X
,Y
,W
,Z
));
1210 /* rx^2 + ry^2 + (rz+1)^2 */
1211 emit_op2(p
, OPCODE_DP3
, tmp
, 0, tmp
, tmp
);
1213 emit_op1(p
, OPCODE_RSQ
, tmp
, 0, tmp
);
1215 emit_op2(p
, OPCODE_MUL
, inv_m
, 0, tmp
, half
);
1217 emit_op3(p
, OPCODE_MAD
, dest
, writemask
, r
, inv_m
, half
);
1219 release_temp(p
, tmp
);
1221 release_temp(p
, inv_m
);
1225 static void build_texture_transform( struct tnl_program
*p
)
1229 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
1231 if (!(p
->state
->fragprog_inputs_read
& FRAG_BIT_TEX(i
)))
1234 if (p
->state
->unit
[i
].texgen_enabled
||
1235 p
->state
->unit
[i
].texmat_enabled
) {
1237 GLuint texmat_enabled
= p
->state
->unit
[i
].texmat_enabled
;
1238 struct ureg out
= register_output(p
, VERT_RESULT_TEX0
+ i
);
1239 struct ureg out_texgen
= undef
;
1241 if (p
->state
->unit
[i
].texgen_enabled
) {
1242 GLuint copy_mask
= 0;
1243 GLuint sphere_mask
= 0;
1244 GLuint reflect_mask
= 0;
1245 GLuint normal_mask
= 0;
1249 out_texgen
= get_temp(p
);
1253 modes
[0] = p
->state
->unit
[i
].texgen_mode0
;
1254 modes
[1] = p
->state
->unit
[i
].texgen_mode1
;
1255 modes
[2] = p
->state
->unit
[i
].texgen_mode2
;
1256 modes
[3] = p
->state
->unit
[i
].texgen_mode3
;
1258 for (j
= 0; j
< 4; j
++) {
1260 case TXG_OBJ_LINEAR
: {
1261 struct ureg obj
= register_input(p
, VERT_ATTRIB_POS
);
1263 register_param3(p
, STATE_TEXGEN
, i
,
1264 STATE_TEXGEN_OBJECT_S
+ j
);
1266 emit_op2(p
, OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1270 case TXG_EYE_LINEAR
: {
1271 struct ureg eye
= get_eye_position(p
);
1273 register_param3(p
, STATE_TEXGEN
, i
,
1274 STATE_TEXGEN_EYE_S
+ j
);
1276 emit_op2(p
, OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1280 case TXG_SPHERE_MAP
:
1281 sphere_mask
|= WRITEMASK_X
<< j
;
1283 case TXG_REFLECTION_MAP
:
1284 reflect_mask
|= WRITEMASK_X
<< j
;
1286 case TXG_NORMAL_MAP
:
1287 normal_mask
|= WRITEMASK_X
<< j
;
1290 copy_mask
|= WRITEMASK_X
<< j
;
1297 build_sphere_texgen(p
, out_texgen
, sphere_mask
);
1301 build_reflect_texgen(p
, out_texgen
, reflect_mask
);
1305 struct ureg normal
= get_eye_normal(p
);
1306 emit_op1(p
, OPCODE_MOV
, out_texgen
, normal_mask
, normal
);
1310 struct ureg in
= register_input(p
, VERT_ATTRIB_TEX0
+i
);
1311 emit_op1(p
, OPCODE_MOV
, out_texgen
, copy_mask
, in
);
1315 if (texmat_enabled
) {
1316 struct ureg texmat
[4];
1317 struct ureg in
= (!is_undef(out_texgen
) ?
1319 register_input(p
, VERT_ATTRIB_TEX0
+i
));
1321 register_matrix_param5( p
, STATE_TEXTURE_MATRIX
, i
, 0, 3,
1323 emit_matrix_transform_vec4( p
, out
, texmat
, in
);
1326 register_matrix_param5( p
, STATE_TEXTURE_MATRIX
, i
, 0, 3,
1327 STATE_MATRIX_TRANSPOSE
, texmat
);
1328 emit_transpose_matrix_transform_vec4( p
, out
, texmat
, in
);
1335 emit_passthrough(p
, VERT_ATTRIB_TEX0
+i
, VERT_RESULT_TEX0
+i
);
1341 static void build_pointsize( struct tnl_program
*p
)
1343 struct ureg eye
= get_eye_position(p
);
1344 struct ureg state_size
= register_param1(p
, STATE_POINT_SIZE
);
1345 struct ureg state_attenuation
= register_param1(p
, STATE_POINT_ATTENUATION
);
1346 struct ureg out
= register_output(p
, VERT_RESULT_PSIZ
);
1347 struct ureg ut
= get_temp(p
);
1350 emit_op1(p
, OPCODE_ABS
, ut
, WRITEMASK_Y
, swizzle1(eye
, Z
));
1351 /* p1 + dist * (p2 + dist * p3); */
1352 emit_op3(p
, OPCODE_MAD
, ut
, WRITEMASK_X
, swizzle1(ut
, Y
),
1353 swizzle1(state_attenuation
, Z
), swizzle1(state_attenuation
, Y
));
1354 emit_op3(p
, OPCODE_MAD
, ut
, WRITEMASK_X
, swizzle1(ut
, Y
),
1355 ut
, swizzle1(state_attenuation
, X
));
1357 /* 1 / sqrt(factor) */
1358 emit_op1(p
, OPCODE_RSQ
, ut
, WRITEMASK_X
, ut
);
1361 /* out = pointSize / sqrt(factor) */
1362 emit_op2(p
, OPCODE_MUL
, out
, WRITEMASK_X
, ut
, state_size
);
1364 /* not sure, might make sense to do clamping here,
1365 but it's not done in t_vb_points neither */
1366 emit_op2(p
, OPCODE_MUL
, ut
, WRITEMASK_X
, ut
, state_size
);
1367 emit_op2(p
, OPCODE_MAX
, ut
, WRITEMASK_X
, ut
, swizzle1(state_size
, Y
));
1368 emit_op2(p
, OPCODE_MIN
, out
, WRITEMASK_X
, ut
, swizzle1(state_size
, Z
));
1371 release_temp(p
, ut
);
1375 * Emit constant point size.
1377 static void constant_pointsize( struct tnl_program
*p
)
1379 struct ureg state_size
= register_param1(p
, STATE_POINT_SIZE
);
1380 struct ureg out
= register_output(p
, VERT_RESULT_PSIZ
);
1381 emit_op1(p
, OPCODE_MOV
, out
, WRITEMASK_X
, state_size
);
1384 static void build_tnl_program( struct tnl_program
*p
)
1385 { /* Emit the program, starting with modelviewproject:
1389 /* Lighting calculations:
1391 if (p
->state
->fragprog_inputs_read
& (FRAG_BIT_COL0
|FRAG_BIT_COL1
)) {
1392 if (p
->state
->light_global_enabled
)
1395 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
1396 emit_passthrough(p
, VERT_ATTRIB_COLOR0
, VERT_RESULT_COL0
);
1398 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL1
)
1399 emit_passthrough(p
, VERT_ATTRIB_COLOR1
, VERT_RESULT_COL1
);
1403 if ((p
->state
->fragprog_inputs_read
& FRAG_BIT_FOGC
) ||
1404 p
->state
->fog_mode
!= FOG_NONE
)
1407 if (p
->state
->fragprog_inputs_read
& FRAG_BITS_TEX_ANY
)
1408 build_texture_transform(p
);
1410 if (p
->state
->point_attenuated
)
1414 constant_pointsize(p
);
1419 emit_op1(p
, OPCODE_END
, undef
, 0, undef
);
1424 _mesa_printf ("\n");
1430 create_new_program( const struct state_key
*key
,
1431 struct gl_vertex_program
*program
,
1434 struct tnl_program p
;
1436 _mesa_memset(&p
, 0, sizeof(p
));
1438 p
.program
= program
;
1439 p
.eye_position
= undef
;
1440 p
.eye_position_normalized
= undef
;
1441 p
.eye_normal
= undef
;
1445 if (max_temps
>= sizeof(int) * 8)
1446 p
.temp_reserved
= 0;
1448 p
.temp_reserved
= ~((1<<max_temps
)-1);
1450 p
.program
->Base
.Instructions
= _mesa_alloc_instructions(MAX_INSN
);
1451 p
.program
->Base
.String
= NULL
;
1452 p
.program
->Base
.NumInstructions
=
1453 p
.program
->Base
.NumTemporaries
=
1454 p
.program
->Base
.NumParameters
=
1455 p
.program
->Base
.NumAttributes
= p
.program
->Base
.NumAddressRegs
= 0;
1456 p
.program
->Base
.Parameters
= _mesa_new_parameter_list();
1457 p
.program
->Base
.InputsRead
= 0;
1458 p
.program
->Base
.OutputsWritten
= 0;
1460 build_tnl_program( &p
);
1463 static void *search_cache( struct tnl_cache
*cache
,
1468 struct tnl_cache_item
*c
;
1470 for (c
= cache
->items
[hash
% cache
->size
]; c
; c
= c
->next
) {
1471 if (c
->hash
== hash
&& _mesa_memcmp(c
->key
, key
, keysize
) == 0)
1478 static void rehash( struct tnl_cache
*cache
)
1480 struct tnl_cache_item
**items
;
1481 struct tnl_cache_item
*c
, *next
;
1484 size
= cache
->size
* 3;
1485 items
= (struct tnl_cache_item
**) _mesa_malloc(size
* sizeof(*items
));
1486 _mesa_memset(items
, 0, size
* sizeof(*items
));
1488 for (i
= 0; i
< cache
->size
; i
++)
1489 for (c
= cache
->items
[i
]; c
; c
= next
) {
1491 c
->next
= items
[c
->hash
% size
];
1492 items
[c
->hash
% size
] = c
;
1496 cache
->items
= items
;
1500 static void cache_item( struct tnl_cache
*cache
,
1505 struct tnl_cache_item
*c
= (struct tnl_cache_item
*) _mesa_malloc(sizeof(*c
));
1510 if (++cache
->n_items
> cache
->size
* 1.5)
1513 c
->next
= cache
->items
[hash
% cache
->size
];
1514 cache
->items
[hash
% cache
->size
] = c
;
1517 static GLuint
hash_key( struct state_key
*key
)
1519 GLuint
*ikey
= (GLuint
*)key
;
1522 /* I'm sure this can be improved on, but speed is important:
1524 for (i
= 0; i
< sizeof(*key
)/sizeof(GLuint
); i
++)
1530 void _tnl_UpdateFixedFunctionProgram( GLcontext
*ctx
)
1532 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1533 struct state_key
*key
;
1535 const struct gl_vertex_program
*prev
= ctx
->VertexProgram
._Current
;
1537 if (!ctx
->VertexProgram
._Current
||
1538 ctx
->VertexProgram
._Current
== ctx
->VertexProgram
._TnlProgram
) {
1539 /* Grab all the relevent state and put it in a single structure:
1541 key
= make_state_key(ctx
);
1542 hash
= hash_key(key
);
1544 /* Look for an already-prepared program for this state:
1546 ctx
->VertexProgram
._TnlProgram
= (struct gl_vertex_program
*)
1547 search_cache( tnl
->vp_cache
, hash
, key
, sizeof(*key
) );
1549 /* OK, we'll have to build a new one:
1551 if (!ctx
->VertexProgram
._TnlProgram
) {
1553 _mesa_printf("Build new TNL program\n");
1555 ctx
->VertexProgram
._TnlProgram
= (struct gl_vertex_program
*)
1556 ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
1558 create_new_program( key
, ctx
->VertexProgram
._TnlProgram
,
1559 ctx
->Const
.VertexProgram
.MaxTemps
);
1561 if (ctx
->Driver
.ProgramStringNotify
)
1562 ctx
->Driver
.ProgramStringNotify( ctx
, GL_VERTEX_PROGRAM_ARB
,
1563 &ctx
->VertexProgram
._TnlProgram
->Base
);
1565 cache_item(tnl
->vp_cache
, hash
, key
, ctx
->VertexProgram
._TnlProgram
);
1570 _mesa_printf("Found existing TNL program for key %x\n", hash
);
1572 ctx
->VertexProgram
._Current
= ctx
->VertexProgram
._TnlProgram
;
1575 /* Tell the driver about the change. Could define a new target for
1578 if (ctx
->VertexProgram
._Current
!= prev
&& ctx
->Driver
.BindProgram
) {
1579 ctx
->Driver
.BindProgram(ctx
, GL_VERTEX_PROGRAM_ARB
,
1580 (struct gl_program
*) ctx
->VertexProgram
._Current
);
1584 void _tnl_ProgramCacheInit( GLcontext
*ctx
)
1586 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1588 tnl
->vp_cache
= (struct tnl_cache
*) MALLOC(sizeof(*tnl
->vp_cache
));
1589 tnl
->vp_cache
->size
= 17;
1590 tnl
->vp_cache
->n_items
= 0;
1591 tnl
->vp_cache
->items
= (struct tnl_cache_item
**)
1592 _mesa_calloc(tnl
->vp_cache
->size
* sizeof(*tnl
->vp_cache
->items
));
1595 void _tnl_ProgramCacheDestroy( GLcontext
*ctx
)
1597 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1598 struct tnl_cache_item
*c
, *next
;
1601 for (i
= 0; i
< tnl
->vp_cache
->size
; i
++)
1602 for (c
= tnl
->vp_cache
->items
[i
]; c
; c
= next
) {
1609 FREE(tnl
->vp_cache
->items
);
1610 FREE(tnl
->vp_cache
);