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 "shader/prog_parameter.h"
37 #include "shader/prog_print.h"
39 #include "brw_state.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;
50 unsigned rescale_normals
:1;
51 unsigned fog_source_is_depth
:1;
52 unsigned tnl_do_vertex_fog
:1;
53 unsigned separate_specular
:1;
54 unsigned fog_option
:2;
55 unsigned point_attenuated
:1;
56 unsigned texture_enabled_global
:1;
57 unsigned fragprog_inputs_read
:12;
60 unsigned light_enabled
:1;
61 unsigned light_eyepos3_is_zero
:1;
62 unsigned light_spotcutoff_is_180
:1;
63 unsigned light_attenuated
:1;
64 unsigned texunit_really_enabled
:1;
65 unsigned texmat_enabled
:1;
66 unsigned texgen_enabled
:4;
67 unsigned texgen_mode0
:4;
68 unsigned texgen_mode1
:4;
69 unsigned texgen_mode2
:4;
70 unsigned texgen_mode3
:4;
81 static GLuint
translate_fog_mode( GLenum mode
)
84 case GL_LINEAR
: return FOG_LINEAR
;
85 case GL_EXP
: return FOG_EXP
;
86 case GL_EXP2
: return FOG_EXP2
;
87 default: return FOG_NONE
;
92 #define TXG_OBJ_LINEAR 1
93 #define TXG_EYE_LINEAR 2
94 #define TXG_SPHERE_MAP 3
95 #define TXG_REFLECTION_MAP 4
96 #define TXG_NORMAL_MAP 5
98 static GLuint
translate_texgen( GLboolean enabled
, GLenum mode
)
104 case GL_OBJECT_LINEAR
: return TXG_OBJ_LINEAR
;
105 case GL_EYE_LINEAR
: return TXG_EYE_LINEAR
;
106 case GL_SPHERE_MAP
: return TXG_SPHERE_MAP
;
107 case GL_REFLECTION_MAP_NV
: return TXG_REFLECTION_MAP
;
108 case GL_NORMAL_MAP_NV
: return TXG_NORMAL_MAP
;
109 default: return TXG_NONE
;
113 static void make_state_key( GLcontext
*ctx
, struct state_key
*key
)
115 struct brw_context
*brw
= brw_context(ctx
);
116 const struct gl_fragment_program
*fp
= brw
->fragment_program
;
119 /* This now relies on texenvprogram.c being active:
123 memset(key
, 0, sizeof(*key
));
125 /* BRW_NEW_FRAGMENT_PROGRAM */
126 key
->fragprog_inputs_read
= fp
->Base
.InputsRead
;
129 key
->separate_specular
= (brw
->attribs
.Light
->Model
.ColorControl
==
130 GL_SEPARATE_SPECULAR_COLOR
);
133 if (brw
->attribs
.Light
->Enabled
) {
134 key
->light_global_enabled
= 1;
136 if (brw
->attribs
.Light
->Model
.LocalViewer
)
137 key
->light_local_viewer
= 1;
139 if (brw
->attribs
.Light
->Model
.TwoSide
)
140 key
->light_twoside
= 1;
142 if (brw
->attribs
.Light
->ColorMaterialEnabled
) {
143 key
->light_color_material
= 1;
144 key
->light_color_material_mask
= brw
->attribs
.Light
->ColorMaterialBitmask
;
147 /* BRW_NEW_INPUT_VARYING */
149 /* For these programs, material values are stuffed into the
152 for (i
= 0 ; i
< MAT_ATTRIB_MAX
; i
++)
153 if (brw
->vb
.info
.varying
& (1<<(VERT_ATTRIB_GENERIC0
+ i
)))
154 key
->light_material_mask
|= 1<<i
;
156 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
157 struct gl_light
*light
= &brw
->attribs
.Light
->Light
[i
];
159 if (light
->Enabled
) {
160 key
->unit
[i
].light_enabled
= 1;
162 if (light
->EyePosition
[3] == 0.0)
163 key
->unit
[i
].light_eyepos3_is_zero
= 1;
165 if (light
->SpotCutoff
== 180.0)
166 key
->unit
[i
].light_spotcutoff_is_180
= 1;
168 if (light
->ConstantAttenuation
!= 1.0 ||
169 light
->LinearAttenuation
!= 0.0 ||
170 light
->QuadraticAttenuation
!= 0.0)
171 key
->unit
[i
].light_attenuated
= 1;
177 if (brw
->attribs
.Transform
->Normalize
)
180 if (brw
->attribs
.Transform
->RescaleNormals
)
181 key
->rescale_normals
= 1;
183 /* BRW_NEW_FRAGMENT_PROGRAM */
184 key
->fog_option
= translate_fog_mode(fp
->FogOption
);
186 key
->fragprog_inputs_read
|= FRAG_BIT_FOGC
;
189 if (brw
->attribs
.Fog
->FogCoordinateSource
== GL_FRAGMENT_DEPTH_EXT
)
190 key
->fog_source_is_depth
= 1;
194 key
->tnl_do_vertex_fog
= 1;
197 if (brw
->attribs
.Point
->_Attenuated
)
198 key
->point_attenuated
= 1;
201 if (brw
->attribs
.Texture
->_TexGenEnabled
||
202 brw
->attribs
.Texture
->_TexMatEnabled
||
203 brw
->attribs
.Texture
->_EnabledUnits
)
204 key
->texture_enabled_global
= 1;
206 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
207 struct gl_texture_unit
*texUnit
= &brw
->attribs
.Texture
->Unit
[i
];
209 if (texUnit
->_ReallyEnabled
)
210 key
->unit
[i
].texunit_really_enabled
= 1;
212 if (brw
->attribs
.Texture
->_TexMatEnabled
& ENABLE_TEXMAT(i
))
213 key
->unit
[i
].texmat_enabled
= 1;
215 if (texUnit
->TexGenEnabled
) {
216 key
->unit
[i
].texgen_enabled
= 1;
218 key
->unit
[i
].texgen_mode0
=
219 translate_texgen( texUnit
->TexGenEnabled
& (1<<0),
221 key
->unit
[i
].texgen_mode1
=
222 translate_texgen( texUnit
->TexGenEnabled
& (1<<1),
224 key
->unit
[i
].texgen_mode2
=
225 translate_texgen( texUnit
->TexGenEnabled
& (1<<2),
227 key
->unit
[i
].texgen_mode3
=
228 translate_texgen( texUnit
->TexGenEnabled
& (1<<3),
236 /* Very useful debugging tool - produces annotated listing of
237 * generated program with line/function references for each
238 * instruction back into this file:
242 /* Should be tunable by the driver - do we want to do matrix
243 * multiplications with DP4's or with MUL/MAD's? SSE works better
244 * with the latter, drivers may differ.
249 /* Use uregs to represent registers internally, translate to Mesa's
250 * expected formats on emit.
252 * NOTE: These are passed by value extensively in this file rather
253 * than as usual by pointer reference. If this disturbs you, try
254 * remembering they are just 32bits in size.
256 * GCC is smart enough to deal with these dword-sized structures in
257 * much the same way as if I had defined them as dwords and was using
258 * macros to access and set the fields. This is much nicer and easier
263 GLint idx
:8; /* relative addressing may be negative */
271 const struct state_key
*state
;
272 struct gl_vertex_program
*program
;
274 GLuint nr_instructions
;
276 GLuint temp_reserved
;
278 struct ureg eye_position
;
279 struct ureg eye_position_normalized
;
280 struct ureg eye_normal
;
281 struct ureg identity
;
284 GLuint color_materials
;
288 const static struct ureg undef
= {
306 static struct ureg
make_ureg(GLuint file
, GLint idx
)
312 reg
.swz
= SWIZZLE_NOOP
;
319 static struct ureg
ureg_negate( struct ureg reg
)
326 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
328 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
331 GET_SWZ(reg
.swz
, w
));
336 static struct ureg
swizzle1( struct ureg reg
, int x
)
338 return swizzle(reg
, x
, x
, x
, x
);
341 static struct ureg
get_temp( struct tnl_program
*p
)
343 int bit
= ffs( ~p
->temp_in_use
);
345 fprintf(stderr
, "%s: out of temporaries\n", __FILE__
);
349 if (bit
> p
->program
->Base
.NumTemporaries
)
350 p
->program
->Base
.NumTemporaries
= bit
;
352 p
->temp_in_use
|= 1<<(bit
-1);
353 return make_ureg(PROGRAM_TEMPORARY
, bit
-1);
356 static struct ureg
reserve_temp( struct tnl_program
*p
)
358 struct ureg temp
= get_temp( p
);
359 p
->temp_reserved
|= 1<<temp
.idx
;
363 static void release_temp( struct tnl_program
*p
, struct ureg reg
)
365 if (reg
.file
== PROGRAM_TEMPORARY
) {
366 p
->temp_in_use
&= ~(1<<reg
.idx
);
367 p
->temp_in_use
|= p
->temp_reserved
; /* can't release reserved temps */
371 static void release_temps( struct tnl_program
*p
)
373 p
->temp_in_use
= p
->temp_reserved
;
378 static struct ureg
register_input( struct tnl_program
*p
, GLuint input
)
382 p
->program
->Base
.InputsRead
|= (1<<input
);
383 return make_ureg(PROGRAM_INPUT
, input
);
386 static struct ureg
register_output( struct tnl_program
*p
, GLuint output
)
388 p
->program
->Base
.OutputsWritten
|= (1<<output
);
389 return make_ureg(PROGRAM_OUTPUT
, output
);
392 static struct ureg
register_const4f( struct tnl_program
*p
,
405 idx
= _mesa_add_unnamed_constant( p
->program
->Base
.Parameters
, values
, 4,
407 assert(swizzle
== SWIZZLE_NOOP
); /* Need to handle swizzle in reg setup */
408 return make_ureg(PROGRAM_STATE_VAR
, idx
);
411 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
412 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
413 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
414 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
416 static GLboolean
is_undef( struct ureg reg
)
418 return reg
.file
== PROGRAM_UNDEFINED
;
421 static struct ureg
get_identity_param( struct tnl_program
*p
)
423 if (is_undef(p
->identity
))
424 p
->identity
= register_const4f(p
, 0,0,0,1);
429 static struct ureg
register_param5( struct tnl_program
*p
,
436 gl_state_index tokens
[STATE_LENGTH
];
443 idx
= _mesa_add_state_reference( p
->program
->Base
.Parameters
, tokens
);
444 return make_ureg(PROGRAM_STATE_VAR
, idx
);
448 #define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
449 #define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
450 #define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
451 #define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
454 static void register_matrix_param5( struct tnl_program
*p
,
455 GLint s0
, /* matrix name */
456 GLint s1
, /* texture matrix number */
457 GLint s2
, /* first row */
458 GLint s3
, /* last row */
459 GLint s4
, /* modifier */
460 struct ureg
*matrix
)
464 /* This is a bit sad as the support is there to pull the whole
465 * matrix out in one go:
467 for (i
= 0; i
<= s3
- s2
; i
++)
468 matrix
[i
] = register_param5( p
, s0
, s1
, i
, i
, s4
);
472 static void emit_arg( struct prog_src_register
*src
,
475 src
->File
= reg
.file
;
476 src
->Index
= reg
.idx
;
477 src
->Swizzle
= reg
.swz
;
479 src
->NegateBase
= reg
.negate
;
484 static void emit_dst( struct prog_dst_register
*dst
,
485 struct ureg reg
, GLuint mask
)
487 dst
->File
= reg
.file
;
488 dst
->Index
= reg
.idx
;
489 /* allow zero as a shorthand for xyzw */
490 dst
->WriteMask
= mask
? mask
: WRITEMASK_XYZW
;
492 dst
->CondSwizzle
= 0;
497 static void debug_insn( struct prog_instruction
*inst
, const char *fn
,
501 static const char *last_fn
;
505 _mesa_printf("%s:\n", fn
);
508 _mesa_printf("%d:\t", line
);
509 _mesa_print_instruction(inst
);
514 static void emit_op3fn(struct tnl_program
*p
,
524 GLuint nr
= p
->program
->Base
.NumInstructions
++;
526 if (nr
>= p
->nr_instructions
) {
527 int new_nr_instructions
= p
->nr_instructions
* 2;
529 p
->program
->Base
.Instructions
=
530 _mesa_realloc(p
->program
->Base
.Instructions
,
531 sizeof(struct prog_instruction
) * p
->nr_instructions
,
532 sizeof(struct prog_instruction
) * new_nr_instructions
);
533 p
->nr_instructions
= new_nr_instructions
;
537 struct prog_instruction
*inst
= &p
->program
->Base
.Instructions
[nr
];
538 memset(inst
, 0, sizeof(*inst
));
543 emit_arg( &inst
->SrcReg
[0], src0
);
544 emit_arg( &inst
->SrcReg
[1], src1
);
545 emit_arg( &inst
->SrcReg
[2], src2
);
547 emit_dst( &inst
->DstReg
, dest
, mask
);
549 debug_insn(inst
, fn
, line
);
555 #define emit_op3(p, op, dst, mask, src0, src1, src2) \
556 emit_op3fn(p, op, dst, mask, src0, src1, src2, __FUNCTION__, __LINE__)
558 #define emit_op2(p, op, dst, mask, src0, src1) \
559 emit_op3fn(p, op, dst, mask, src0, src1, undef, __FUNCTION__, __LINE__)
561 #define emit_op1(p, op, dst, mask, src0) \
562 emit_op3fn(p, op, dst, mask, src0, undef, undef, __FUNCTION__, __LINE__)
565 static struct ureg
make_temp( struct tnl_program
*p
, struct ureg reg
)
567 if (reg
.file
== PROGRAM_TEMPORARY
&&
568 !(p
->temp_reserved
& (1<<reg
.idx
)))
571 struct ureg temp
= get_temp(p
);
572 emit_op1(p
, OPCODE_MOV
, temp
, 0, reg
);
578 /* Currently no tracking performed of input/output/register size or
579 * active elements. Could be used to reduce these operations, as
580 * could the matrix type.
582 static void emit_matrix_transform_vec4( struct tnl_program
*p
,
584 const struct ureg
*mat
,
587 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_X
, src
, mat
[0]);
588 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_Y
, src
, mat
[1]);
589 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_Z
, src
, mat
[2]);
590 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_W
, src
, mat
[3]);
593 /* This version is much easier to implement if writemasks are not
594 * supported natively on the target or (like SSE), the target doesn't
595 * have a clean/obvious dotproduct implementation.
597 static void emit_transpose_matrix_transform_vec4( struct tnl_program
*p
,
599 const struct ureg
*mat
,
604 if (dest
.file
!= PROGRAM_TEMPORARY
)
609 emit_op2(p
, OPCODE_MUL
, tmp
, 0, swizzle1(src
,X
), mat
[0]);
610 emit_op3(p
, OPCODE_MAD
, tmp
, 0, swizzle1(src
,Y
), mat
[1], tmp
);
611 emit_op3(p
, OPCODE_MAD
, tmp
, 0, swizzle1(src
,Z
), mat
[2], tmp
);
612 emit_op3(p
, OPCODE_MAD
, dest
, 0, swizzle1(src
,W
), mat
[3], tmp
);
614 if (dest
.file
!= PROGRAM_TEMPORARY
)
615 release_temp(p
, tmp
);
618 static void emit_matrix_transform_vec3( struct tnl_program
*p
,
620 const struct ureg
*mat
,
623 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_X
, src
, mat
[0]);
624 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_Y
, src
, mat
[1]);
625 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_Z
, src
, mat
[2]);
629 static void emit_normalize_vec3( struct tnl_program
*p
,
633 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_W
, src
, src
);
634 emit_op1(p
, OPCODE_RSQ
, dest
, WRITEMASK_W
, swizzle1(dest
,W
));
635 emit_op2(p
, OPCODE_MUL
, dest
, WRITEMASK_XYZ
, src
, swizzle1(dest
,W
));
638 static void emit_passthrough( struct tnl_program
*p
,
642 struct ureg out
= register_output(p
, output
);
643 emit_op1(p
, OPCODE_MOV
, out
, 0, register_input(p
, input
));
646 static struct ureg
get_eye_position( struct tnl_program
*p
)
648 if (is_undef(p
->eye_position
)) {
649 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
650 struct ureg modelview
[4];
652 p
->eye_position
= reserve_temp(p
);
655 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 3,
658 emit_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
661 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 3,
662 STATE_MATRIX_TRANSPOSE
, modelview
);
664 emit_transpose_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
668 return p
->eye_position
;
673 static struct ureg
get_eye_z( struct tnl_program
*p
)
675 if (!is_undef(p
->eye_position
)) {
676 return swizzle1(p
->eye_position
, Z
);
678 else if (!is_undef(p
->eye_z
)) {
679 struct ureg pos
= register_input( p
, BRW_ATTRIB_POS
);
680 struct ureg modelview2
;
682 p
->eye_z
= reserve_temp(p
);
684 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 2, 1,
685 STATE_MATRIX
, &modelview2
);
687 emit_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
688 emit_op2(p
, OPCODE_DP4
, p
->eye_z
, WRITEMASK_Z
, pos
, modelview2
);
691 return swizzle1(p
->eye_z
, Z
)
697 static struct ureg
get_eye_position_normalized( struct tnl_program
*p
)
699 if (is_undef(p
->eye_position_normalized
)) {
700 struct ureg eye
= get_eye_position(p
);
701 p
->eye_position_normalized
= reserve_temp(p
);
702 emit_normalize_vec3(p
, p
->eye_position_normalized
, eye
);
705 return p
->eye_position_normalized
;
709 static struct ureg
get_eye_normal( struct tnl_program
*p
)
711 if (is_undef(p
->eye_normal
)) {
712 struct ureg normal
= register_input(p
, VERT_ATTRIB_NORMAL
);
713 struct ureg mvinv
[3];
715 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 2,
716 STATE_MATRIX_INVTRANS
, mvinv
);
718 p
->eye_normal
= reserve_temp(p
);
720 /* Transform to eye space:
722 emit_matrix_transform_vec3( p
, p
->eye_normal
, mvinv
, normal
);
724 /* Normalize/Rescale:
726 if (p
->state
->normalize
) {
727 emit_normalize_vec3( p
, p
->eye_normal
, p
->eye_normal
);
729 else if (p
->state
->rescale_normals
) {
730 struct ureg rescale
= register_param2(p
, STATE_INTERNAL
,
733 emit_op2( p
, OPCODE_MUL
, p
->eye_normal
, 0, p
->eye_normal
,
734 swizzle1(rescale
, X
));
738 return p
->eye_normal
;
743 static void build_hpos( struct tnl_program
*p
)
745 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
746 struct ureg hpos
= register_output( p
, VERT_RESULT_HPOS
);
750 register_matrix_param5( p
, STATE_MVP_MATRIX
, 0, 0, 3,
752 emit_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
755 register_matrix_param5( p
, STATE_MVP_MATRIX
, 0, 0, 3,
756 STATE_MATRIX_TRANSPOSE
, mvp
);
757 emit_transpose_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
762 static GLuint
material_attrib( GLuint side
, GLuint property
)
764 return (property
- STATE_AMBIENT
) * 2 + side
;
767 /* Get a bitmask of which material values vary on a per-vertex basis.
769 static void set_material_flags( struct tnl_program
*p
)
771 p
->color_materials
= 0;
774 if (p
->state
->light_color_material
) {
776 p
->color_materials
= p
->state
->light_color_material_mask
;
779 p
->materials
|= p
->state
->light_material_mask
;
783 static struct ureg
get_material( struct tnl_program
*p
, GLuint side
,
786 GLuint attrib
= material_attrib(side
, property
);
788 if (p
->color_materials
& (1<<attrib
))
789 return register_input(p
, VERT_ATTRIB_COLOR0
);
790 else if (p
->materials
& (1<<attrib
))
791 return register_input( p
, attrib
+ _TNL_ATTRIB_MAT_FRONT_AMBIENT
);
793 return register_param3( p
, STATE_MATERIAL
, side
, property
);
796 #define SCENE_COLOR_BITS(side) ((MAT_BIT_FRONT_EMISSION | \
797 MAT_BIT_FRONT_AMBIENT | \
798 MAT_BIT_FRONT_DIFFUSE) << (side))
800 /* Either return a precalculated constant value or emit code to
801 * calculate these values dynamically in the case where material calls
802 * are present between begin/end pairs.
804 * Probably want to shift this to the program compilation phase - if
805 * we always emitted the calculation here, a smart compiler could
806 * detect that it was constant (given a certain set of inputs), and
807 * lift it out of the main loop. That way the programs created here
808 * would be independent of the vertex_buffer details.
810 static struct ureg
get_scenecolor( struct tnl_program
*p
, GLuint side
)
812 if (p
->materials
& SCENE_COLOR_BITS(side
)) {
813 struct ureg lm_ambient
= register_param1(p
, STATE_LIGHTMODEL_AMBIENT
);
814 struct ureg material_emission
= get_material(p
, side
, STATE_EMISSION
);
815 struct ureg material_ambient
= get_material(p
, side
, STATE_AMBIENT
);
816 struct ureg material_diffuse
= get_material(p
, side
, STATE_DIFFUSE
);
817 struct ureg tmp
= make_temp(p
, material_diffuse
);
818 emit_op3(p
, OPCODE_MAD
, tmp
, WRITEMASK_XYZ
, lm_ambient
,
819 material_ambient
, material_emission
);
823 return register_param2( p
, STATE_LIGHTMODEL_SCENECOLOR
, side
);
827 static struct ureg
get_lightprod( struct tnl_program
*p
, GLuint light
,
828 GLuint side
, GLuint property
)
830 GLuint attrib
= material_attrib(side
, property
);
831 if (p
->materials
& (1<<attrib
)) {
832 struct ureg light_value
=
833 register_param3(p
, STATE_LIGHT
, light
, property
);
834 struct ureg material_value
= get_material(p
, side
, property
);
835 struct ureg tmp
= get_temp(p
);
836 emit_op2(p
, OPCODE_MUL
, tmp
, 0, light_value
, material_value
);
840 return register_param4(p
, STATE_LIGHTPROD
, light
, side
, property
);
843 static struct ureg
calculate_light_attenuation( struct tnl_program
*p
,
848 struct ureg attenuation
= register_param3(p
, STATE_LIGHT
, i
,
850 struct ureg att
= get_temp(p
);
852 /* Calculate spot attenuation:
854 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
855 struct ureg spot_dir_norm
= register_param3(p
, STATE_INTERNAL
,
856 STATE_SPOT_DIR_NORMALIZED
, i
);
857 struct ureg spot
= get_temp(p
);
858 struct ureg slt
= get_temp(p
);
860 emit_op2(p
, OPCODE_DP3
, spot
, 0, ureg_negate(VPpli
), spot_dir_norm
);
861 emit_op2(p
, OPCODE_SLT
, slt
, 0, swizzle1(spot_dir_norm
,W
), spot
);
862 emit_op2(p
, OPCODE_POW
, spot
, 0, spot
, swizzle1(attenuation
, W
));
863 emit_op2(p
, OPCODE_MUL
, att
, 0, slt
, spot
);
865 release_temp(p
, spot
);
866 release_temp(p
, slt
);
869 /* Calculate distance attenuation:
871 if (p
->state
->unit
[i
].light_attenuated
) {
874 emit_op1(p
, OPCODE_RCP
, dist
, WRITEMASK_YZ
, dist
);
876 emit_op2(p
, OPCODE_MUL
, dist
, WRITEMASK_XZ
, dist
, swizzle1(dist
,Y
));
878 emit_op2(p
, OPCODE_DP3
, dist
, 0, attenuation
, dist
);
880 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
882 emit_op1(p
, OPCODE_RCP
, dist
, 0, dist
);
883 /* spot-atten * dist-atten */
884 emit_op2(p
, OPCODE_MUL
, att
, 0, dist
, att
);
887 emit_op1(p
, OPCODE_RCP
, att
, 0, dist
);
898 /* Need to add some addtional parameters to allow lighting in object
899 * space - STATE_SPOT_DIRECTION and STATE_HALF_VECTOR implicitly assume eye
902 static void build_lighting( struct tnl_program
*p
)
904 const GLboolean twoside
= p
->state
->light_twoside
;
905 const GLboolean separate
= p
->state
->separate_specular
;
906 GLuint nr_lights
= 0, count
= 0;
907 struct ureg normal
= get_eye_normal(p
);
908 struct ureg lit
= get_temp(p
);
909 struct ureg dots
= get_temp(p
);
910 struct ureg _col0
= undef
, _col1
= undef
;
911 struct ureg _bfc0
= undef
, _bfc1
= undef
;
914 for (i
= 0; i
< MAX_LIGHTS
; i
++)
915 if (p
->state
->unit
[i
].light_enabled
)
918 set_material_flags(p
);
921 struct ureg shininess
= get_material(p
, 0, STATE_SHININESS
);
922 emit_op1(p
, OPCODE_MOV
, dots
, WRITEMASK_W
, swizzle1(shininess
,X
));
923 release_temp(p
, shininess
);
925 _col0
= make_temp(p
, get_scenecolor(p
, 0));
927 _col1
= make_temp(p
, get_identity_param(p
));
934 struct ureg shininess
= get_material(p
, 1, STATE_SHININESS
);
935 emit_op1(p
, OPCODE_MOV
, dots
, WRITEMASK_Z
,
936 ureg_negate(swizzle1(shininess
,X
)));
937 release_temp(p
, shininess
);
939 _bfc0
= make_temp(p
, get_scenecolor(p
, 1));
941 _bfc1
= make_temp(p
, get_identity_param(p
));
947 /* If no lights, still need to emit the scenecolor.
949 /* KW: changed to do this always - v1.17 "Fix lighting alpha result"?
951 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
953 struct ureg res0
= register_output( p
, VERT_RESULT_COL0
);
954 emit_op1(p
, OPCODE_MOV
, res0
, 0, _col0
);
957 struct ureg res0
= register_output( p
, VERT_RESULT_BFC0
);
958 emit_op1(p
, OPCODE_MOV
, res0
, 0, _bfc0
);
962 if (separate
&& (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL1
)) {
964 struct ureg res1
= register_output( p
, VERT_RESULT_COL1
);
965 emit_op1(p
, OPCODE_MOV
, res1
, 0, _col1
);
968 struct ureg res1
= register_output( p
, VERT_RESULT_BFC1
);
969 emit_op1(p
, OPCODE_MOV
, res1
, 0, _bfc1
);
973 if (nr_lights
== 0) {
979 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
980 if (p
->state
->unit
[i
].light_enabled
) {
981 struct ureg half
= undef
;
982 struct ureg att
= undef
, VPpli
= undef
;
986 if (p
->state
->unit
[i
].light_eyepos3_is_zero
) {
987 /* Can used precomputed constants in this case.
988 * Attenuation never applies to infinite lights.
990 VPpli
= register_param3(p
, STATE_LIGHT
, i
,
991 STATE_POSITION_NORMALIZED
);
992 if (p
->state
->light_local_viewer
) {
993 struct ureg eye_hat
= get_eye_position_normalized(p
);
995 emit_op2(p
, OPCODE_SUB
, half
, 0, VPpli
, eye_hat
);
996 emit_normalize_vec3(p
, half
, half
);
998 half
= register_param3(p
, STATE_LIGHT
, i
, STATE_HALF_VECTOR
);
1002 struct ureg Ppli
= register_param3(p
, STATE_LIGHT
, i
,
1004 struct ureg V
= get_eye_position(p
);
1005 struct ureg dist
= get_temp(p
);
1006 struct ureg tmpPpli
= get_temp(p
);
1008 VPpli
= get_temp(p
);
1011 /* In homogeneous object coordinates
1013 emit_op1(p
, OPCODE_RCP
, dist
, 0, swizzle1(Ppli
, W
));
1014 emit_op2(p
, OPCODE_MUL
, tmpPpli
, 0, Ppli
, dist
);
1016 /* Calulate VPpli vector
1018 emit_op2(p
, OPCODE_SUB
, VPpli
, 0, tmpPpli
, V
);
1020 /* Normalize VPpli. The dist value also used in
1021 * attenuation below.
1023 emit_op2(p
, OPCODE_DP3
, dist
, 0, VPpli
, VPpli
);
1024 emit_op1(p
, OPCODE_RSQ
, dist
, 0, dist
);
1025 emit_op2(p
, OPCODE_MUL
, VPpli
, 0, VPpli
, dist
);
1028 /* Calculate attenuation:
1030 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
||
1031 p
->state
->unit
[i
].light_attenuated
) {
1032 att
= calculate_light_attenuation(p
, i
, VPpli
, dist
);
1036 /* Calculate viewer direction, or use infinite viewer:
1038 if (p
->state
->light_local_viewer
) {
1039 struct ureg eye_hat
= get_eye_position_normalized(p
);
1040 emit_op2(p
, OPCODE_SUB
, half
, 0, VPpli
, eye_hat
);
1043 struct ureg z_dir
= swizzle(get_identity_param(p
),X
,Y
,W
,Z
);
1044 emit_op2(p
, OPCODE_ADD
, half
, 0, VPpli
, z_dir
);
1047 emit_normalize_vec3(p
, half
, half
);
1049 release_temp(p
, dist
);
1050 release_temp(p
, tmpPpli
);
1053 /* Calculate dot products:
1055 emit_op2(p
, OPCODE_DP3
, dots
, WRITEMASK_X
, normal
, VPpli
);
1056 emit_op2(p
, OPCODE_DP3
, dots
, WRITEMASK_Y
, normal
, half
);
1059 /* Front face lighting:
1062 struct ureg ambient
= get_lightprod(p
, i
, 0, STATE_AMBIENT
);
1063 struct ureg diffuse
= get_lightprod(p
, i
, 0, STATE_DIFFUSE
);
1064 struct ureg specular
= get_lightprod(p
, i
, 0, STATE_SPECULAR
);
1065 struct ureg res0
, res1
;
1066 GLuint mask0
, mask1
;
1068 emit_op1(p
, OPCODE_LIT
, lit
, 0, dots
);
1071 emit_op2(p
, OPCODE_MUL
, lit
, 0, lit
, att
);
1079 if (count
== nr_lights
) {
1081 mask0
= WRITEMASK_XYZ
;
1082 mask1
= WRITEMASK_XYZ
;
1084 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
1085 res0
= register_output( p
, VERT_RESULT_COL0
);
1087 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL1
)
1088 res1
= register_output( p
, VERT_RESULT_COL1
);
1091 mask1
= WRITEMASK_XYZ
;
1093 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
1094 res1
= register_output( p
, VERT_RESULT_COL0
);
1098 emit_op3(p
, OPCODE_MAD
, _col0
, 0, swizzle1(lit
,X
), ambient
, _col0
);
1099 emit_op3(p
, OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _col0
);
1100 emit_op3(p
, OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _col1
);
1102 release_temp(p
, ambient
);
1103 release_temp(p
, diffuse
);
1104 release_temp(p
, specular
);
1107 /* Back face lighting:
1110 struct ureg ambient
= get_lightprod(p
, i
, 1, STATE_AMBIENT
);
1111 struct ureg diffuse
= get_lightprod(p
, i
, 1, STATE_DIFFUSE
);
1112 struct ureg specular
= get_lightprod(p
, i
, 1, STATE_SPECULAR
);
1113 struct ureg res0
, res1
;
1114 GLuint mask0
, mask1
;
1116 emit_op1(p
, OPCODE_LIT
, lit
, 0, ureg_negate(swizzle(dots
,X
,Y
,W
,Z
)));
1119 emit_op2(p
, OPCODE_MUL
, lit
, 0, lit
, att
);
1126 if (count
== nr_lights
) {
1128 mask0
= WRITEMASK_XYZ
;
1129 mask1
= WRITEMASK_XYZ
;
1130 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
1131 res0
= register_output( p
, VERT_RESULT_BFC0
);
1133 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL1
)
1134 res1
= register_output( p
, VERT_RESULT_BFC1
);
1137 mask1
= WRITEMASK_XYZ
;
1139 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
1140 res1
= register_output( p
, VERT_RESULT_BFC0
);
1144 emit_op3(p
, OPCODE_MAD
, _bfc0
, 0, swizzle1(lit
,X
), ambient
, _bfc0
);
1145 emit_op3(p
, OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _bfc0
);
1146 emit_op3(p
, OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _bfc1
);
1148 release_temp(p
, ambient
);
1149 release_temp(p
, diffuse
);
1150 release_temp(p
, specular
);
1153 release_temp(p
, half
);
1154 release_temp(p
, VPpli
);
1155 release_temp(p
, att
);
1163 static void build_fog( struct tnl_program
*p
)
1165 struct ureg fog
= register_output(p
, VERT_RESULT_FOGC
);
1167 GLuint useabs
= p
->state
->fog_source_is_depth
&& p
->state
->fog_option
&&
1168 (p
->state
->fog_option
!= FOG_EXP2
);
1170 if (p
->state
->fog_source_is_depth
) {
1171 input
= swizzle1(get_eye_position(p
), Z
);
1174 input
= swizzle1(register_input(p
, VERT_ATTRIB_FOG
), X
);
1175 if (p
->state
->fog_option
&&
1176 p
->state
->tnl_do_vertex_fog
)
1177 input
= swizzle1(register_input(p
, VERT_ATTRIB_FOG
), X
);
1179 input
= register_input(p
, VERT_ATTRIB_FOG
);
1182 if (p
->state
->fog_option
&&
1183 p
->state
->tnl_do_vertex_fog
) {
1184 struct ureg params
= register_param2(p
, STATE_INTERNAL
,
1185 STATE_FOG_PARAMS_OPTIMIZED
);
1186 struct ureg tmp
= get_temp(p
);
1187 struct ureg id
= get_identity_param(p
);
1189 emit_op1(p
, OPCODE_MOV
, fog
, 0, id
);
1192 emit_op1(p
, OPCODE_ABS
, tmp
, 0, input
);
1195 switch (p
->state
->fog_option
) {
1197 emit_op3(p
, OPCODE_MAD
, tmp
, 0, useabs
? tmp
: input
,
1198 swizzle1(params
,X
), swizzle1(params
,Y
));
1199 emit_op2(p
, OPCODE_MAX
, tmp
, 0, tmp
, swizzle1(id
,X
)); /* saturate */
1200 emit_op2(p
, OPCODE_MIN
, fog
, WRITEMASK_X
, tmp
, swizzle1(id
,W
));
1204 emit_op2(p
, OPCODE_MUL
, tmp
, 0, useabs
? tmp
: input
,
1205 swizzle1(params
,Z
));
1206 emit_op1(p
, OPCODE_EX2
, fog
, WRITEMASK_X
, ureg_negate(tmp
));
1209 emit_op2(p
, OPCODE_MUL
, tmp
, 0, input
, swizzle1(params
,W
));
1210 emit_op2(p
, OPCODE_MUL
, tmp
, 0, tmp
, tmp
);
1211 emit_op1(p
, OPCODE_EX2
, fog
, WRITEMASK_X
, ureg_negate(tmp
));
1215 release_temp(p
, tmp
);
1218 /* results = incoming fog coords (compute fog per-fragment later)
1220 * KW: Is it really necessary to do anything in this case?
1222 emit_op1(p
, useabs
? OPCODE_ABS
: OPCODE_MOV
, fog
, 0, input
);
1226 static void build_reflect_texgen( struct tnl_program
*p
,
1230 struct ureg normal
= get_eye_normal(p
);
1231 struct ureg eye_hat
= get_eye_position_normalized(p
);
1232 struct ureg tmp
= get_temp(p
);
1235 emit_op2(p
, OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1237 emit_op2(p
, OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1239 emit_op3(p
, OPCODE_MAD
, dest
, writemask
, ureg_negate(tmp
), normal
, eye_hat
);
1241 release_temp(p
, tmp
);
1244 static void build_sphere_texgen( struct tnl_program
*p
,
1248 struct ureg normal
= get_eye_normal(p
);
1249 struct ureg eye_hat
= get_eye_position_normalized(p
);
1250 struct ureg tmp
= get_temp(p
);
1251 struct ureg half
= register_scalar_const(p
, .5);
1252 struct ureg r
= get_temp(p
);
1253 struct ureg inv_m
= get_temp(p
);
1254 struct ureg id
= get_identity_param(p
);
1256 /* Could share the above calculations, but it would be
1257 * a fairly odd state for someone to set (both sphere and
1258 * reflection active for different texture coordinate
1259 * components. Of course - if two texture units enable
1260 * reflect and/or sphere, things start to tilt in favour
1261 * of seperating this out:
1265 emit_op2(p
, OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1267 emit_op2(p
, OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1269 emit_op3(p
, OPCODE_MAD
, r
, 0, ureg_negate(tmp
), normal
, eye_hat
);
1271 emit_op2(p
, OPCODE_ADD
, tmp
, 0, r
, swizzle(id
,X
,Y
,W
,Z
));
1272 /* rx^2 + ry^2 + (rz+1)^2 */
1273 emit_op2(p
, OPCODE_DP3
, tmp
, 0, tmp
, tmp
);
1275 emit_op1(p
, OPCODE_RSQ
, tmp
, 0, tmp
);
1277 emit_op2(p
, OPCODE_MUL
, inv_m
, 0, tmp
, half
);
1279 emit_op3(p
, OPCODE_MAD
, dest
, writemask
, r
, inv_m
, half
);
1281 release_temp(p
, tmp
);
1283 release_temp(p
, inv_m
);
1287 static void build_texture_transform( struct tnl_program
*p
)
1291 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
1293 if (!(p
->state
->fragprog_inputs_read
& (FRAG_BIT_TEX0
<<i
)))
1296 if (p
->state
->unit
[i
].texgen_enabled
||
1297 p
->state
->unit
[i
].texmat_enabled
) {
1299 GLuint texmat_enabled
= p
->state
->unit
[i
].texmat_enabled
;
1300 struct ureg out
= register_output(p
, VERT_RESULT_TEX0
+ i
);
1301 struct ureg out_texgen
= undef
;
1303 if (p
->state
->unit
[i
].texgen_enabled
) {
1304 GLuint copy_mask
= 0;
1305 GLuint sphere_mask
= 0;
1306 GLuint reflect_mask
= 0;
1307 GLuint normal_mask
= 0;
1311 out_texgen
= get_temp(p
);
1315 modes
[0] = p
->state
->unit
[i
].texgen_mode0
;
1316 modes
[1] = p
->state
->unit
[i
].texgen_mode1
;
1317 modes
[2] = p
->state
->unit
[i
].texgen_mode2
;
1318 modes
[3] = p
->state
->unit
[i
].texgen_mode3
;
1320 for (j
= 0; j
< 4; j
++) {
1322 case TXG_OBJ_LINEAR
: {
1323 struct ureg obj
= register_input(p
, VERT_ATTRIB_POS
);
1325 register_param3(p
, STATE_TEXGEN
, i
,
1326 STATE_TEXGEN_OBJECT_S
+ j
);
1328 emit_op2(p
, OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1332 case TXG_EYE_LINEAR
: {
1333 struct ureg eye
= get_eye_position(p
);
1335 register_param3(p
, STATE_TEXGEN
, i
,
1336 STATE_TEXGEN_EYE_S
+ j
);
1338 emit_op2(p
, OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1342 case TXG_SPHERE_MAP
:
1343 sphere_mask
|= WRITEMASK_X
<< j
;
1345 case TXG_REFLECTION_MAP
:
1346 reflect_mask
|= WRITEMASK_X
<< j
;
1348 case TXG_NORMAL_MAP
:
1349 normal_mask
|= WRITEMASK_X
<< j
;
1352 copy_mask
|= WRITEMASK_X
<< j
;
1359 build_sphere_texgen(p
, out_texgen
, sphere_mask
);
1363 build_reflect_texgen(p
, out_texgen
, reflect_mask
);
1367 struct ureg normal
= get_eye_normal(p
);
1368 emit_op1(p
, OPCODE_MOV
, out_texgen
, normal_mask
, normal
);
1372 struct ureg in
= register_input(p
, VERT_ATTRIB_TEX0
+i
);
1373 emit_op1(p
, OPCODE_MOV
, out_texgen
, copy_mask
, in
);
1377 if (texmat_enabled
) {
1378 struct ureg texmat
[4];
1379 struct ureg in
= (!is_undef(out_texgen
) ?
1381 register_input(p
, VERT_ATTRIB_TEX0
+i
));
1383 register_matrix_param5( p
, STATE_TEXTURE_MATRIX
, i
, 0, 3,
1385 emit_matrix_transform_vec4( p
, out
, texmat
, in
);
1388 register_matrix_param5( p
, STATE_TEXTURE_MATRIX
, i
, 0, 3,
1389 STATE_MATRIX_TRANSPOSE
, texmat
);
1390 emit_transpose_matrix_transform_vec4( p
, out
, texmat
, in
);
1397 emit_passthrough(p
, VERT_ATTRIB_TEX0
+i
, VERT_RESULT_TEX0
+i
);
1403 /* Seems like it could be tighter:
1405 static void build_pointsize( struct tnl_program
*p
)
1407 struct ureg eye
= get_eye_position(p
);
1408 struct ureg state_size
= register_param1(p
, STATE_POINT_SIZE
);
1409 struct ureg state_attenuation
= register_param1(p
, STATE_POINT_ATTENUATION
);
1410 struct ureg out
= register_output(p
, VERT_RESULT_PSIZ
);
1411 struct ureg ut
= get_temp(p
);
1413 /* 1, Z, Z * Z, 1 */
1414 emit_op1(p
, OPCODE_MOV
, ut
, WRITEMASK_XW
, swizzle1(get_identity_param(p
), W
));
1415 emit_op1(p
, OPCODE_ABS
, ut
, WRITEMASK_YZ
, swizzle1(eye
, Z
));
1416 emit_op2(p
, OPCODE_MUL
, ut
, WRITEMASK_Z
, ut
, ut
);
1419 /* p1 + p2 * dist + p3 * dist * dist, 0 */
1420 emit_op2(p
, OPCODE_DP3
, ut
, WRITEMASK_X
, ut
, state_attenuation
);
1422 /* 1 / sqrt(factor) */
1423 emit_op1(p
, OPCODE_RSQ
, ut
, WRITEMASK_X
, ut
);
1425 /* ut = pointSize / factor */
1426 emit_op2(p
, OPCODE_MUL
, ut
, WRITEMASK_X
, ut
, state_size
);
1428 /* Clamp to min/max - state_size.[yz]
1430 emit_op2(p
, OPCODE_MAX
, ut
, WRITEMASK_X
, ut
, swizzle1(state_size
, Y
));
1431 emit_op2(p
, OPCODE_MIN
, out
, 0, swizzle1(ut
, X
), swizzle1(state_size
, Z
));
1433 release_temp(p
, ut
);
1436 static void build_tnl_program( struct tnl_program
*p
)
1438 /* Emit the program, starting with modelviewproject:
1442 /* Lighting calculations:
1444 if (p
->state
->fragprog_inputs_read
& (FRAG_BIT_COL0
|FRAG_BIT_COL1
)) {
1445 if (p
->state
->light_global_enabled
)
1448 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
1449 emit_passthrough(p
, VERT_ATTRIB_COLOR0
, VERT_RESULT_COL0
);
1451 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL1
)
1452 emit_passthrough(p
, VERT_ATTRIB_COLOR1
, VERT_RESULT_COL1
);
1456 if ((p
->state
->fragprog_inputs_read
& FRAG_BIT_FOGC
) ||
1457 p
->state
->fog_option
!= FOG_NONE
)
1460 if (p
->state
->fragprog_inputs_read
& FRAG_BITS_TEX_ANY
)
1461 build_texture_transform(p
);
1463 if (p
->state
->point_attenuated
)
1468 emit_op1(p
, OPCODE_END
, undef
, 0, undef
);
1473 _mesa_printf ("\n");
1478 static void build_new_tnl_program( const struct state_key
*key
,
1479 struct gl_vertex_program
*program
,
1482 struct tnl_program p
;
1484 _mesa_memset(&p
, 0, sizeof(p
));
1486 p
.program
= program
;
1487 p
.eye_position
= undef
;
1488 p
.eye_position_normalized
= undef
;
1489 p
.eye_normal
= undef
;
1492 p
.nr_instructions
= 16;
1494 if (max_temps
>= sizeof(int) * 8)
1495 p
.temp_reserved
= 0;
1497 p
.temp_reserved
= ~((1<<max_temps
)-1);
1499 p
.program
->Base
.Instructions
=
1500 _mesa_malloc(sizeof(struct prog_instruction
) * p
.nr_instructions
);
1501 p
.program
->Base
.String
= 0;
1502 p
.program
->Base
.NumInstructions
=
1503 p
.program
->Base
.NumTemporaries
=
1504 p
.program
->Base
.NumParameters
=
1505 p
.program
->Base
.NumAttributes
= p
.program
->Base
.NumAddressRegs
= 0;
1506 p
.program
->Base
.Parameters
= _mesa_new_parameter_list();
1507 p
.program
->Base
.InputsRead
= 0;
1508 p
.program
->Base
.OutputsWritten
= 0;
1510 build_tnl_program( &p
);
1513 static void *search_cache( struct brw_tnl_cache
*cache
,
1518 struct brw_tnl_cache_item
*c
;
1520 for (c
= cache
->items
[hash
% cache
->size
]; c
; c
= c
->next
) {
1521 if (c
->hash
== hash
&& memcmp(c
->key
, key
, keysize
) == 0)
1528 static void rehash( struct brw_tnl_cache
*cache
)
1530 struct brw_tnl_cache_item
**items
;
1531 struct brw_tnl_cache_item
*c
, *next
;
1534 size
= cache
->size
* 3;
1535 items
= (struct brw_tnl_cache_item
**) _mesa_malloc(size
* sizeof(*items
));
1536 _mesa_memset(items
, 0, size
* sizeof(*items
));
1538 for (i
= 0; i
< cache
->size
; i
++)
1539 for (c
= cache
->items
[i
]; c
; c
= next
) {
1541 c
->next
= items
[c
->hash
% size
];
1542 items
[c
->hash
% size
] = c
;
1546 cache
->items
= items
;
1550 static void cache_item( struct brw_tnl_cache
*cache
,
1552 const struct state_key
*key
,
1555 struct brw_tnl_cache_item
*c
= MALLOC(sizeof(*c
));
1558 c
->key
= malloc(sizeof(*key
));
1559 memcpy(c
->key
, key
, sizeof(*key
));
1563 if (++cache
->n_items
> cache
->size
* 1.5)
1566 c
->next
= cache
->items
[hash
% cache
->size
];
1567 cache
->items
[hash
% cache
->size
] = c
;
1571 static GLuint
hash_key( struct state_key
*key
)
1573 GLuint
*ikey
= (GLuint
*)key
;
1576 /* I'm sure this can be improved on, but speed is important:
1578 for (i
= 0; i
< sizeof(*key
)/sizeof(GLuint
); i
++)
1584 static int prepare_tnl_program( struct brw_context
*brw
)
1586 GLcontext
*ctx
= &brw
->intel
.ctx
;
1587 struct state_key key
;
1589 struct gl_vertex_program
*old
= brw
->tnl_program
;
1592 if (brw
->attribs
.VertexProgram
->_Current
)
1595 /* Grab all the relevent state and put it in a single structure:
1597 make_state_key(ctx
, &key
);
1598 hash
= hash_key(&key
);
1600 /* Look for an already-prepared program for this state:
1602 brw
->tnl_program
= (struct gl_vertex_program
*)
1603 search_cache( &brw
->tnl_program_cache
, hash
, &key
, sizeof(key
) );
1605 /* OK, we'll have to build a new one:
1607 if (!brw
->tnl_program
) {
1608 brw
->tnl_program
= (struct gl_vertex_program
*)
1609 ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
1611 build_new_tnl_program( &key
, brw
->tnl_program
,
1612 /* ctx->Const.MaxVertexProgramTemps */
1616 if (ctx
->Driver
.ProgramStringNotify
)
1617 ctx
->Driver
.ProgramStringNotify( ctx
, GL_VERTEX_PROGRAM_ARB
,
1618 &brw
->tnl_program
->Base
);
1620 cache_item( &brw
->tnl_program_cache
,
1621 hash
, &key
, brw
->tnl_program
);
1624 if (old
!= brw
->tnl_program
)
1625 brw
->state
.dirty
.brw
|= BRW_NEW_TNL_PROGRAM
;
1629 /* Note: See brw_draw.c - the vertex program must not rely on
1630 * brw->primitive or brw->reduced_prim.
1632 const struct brw_tracked_state brw_tnl_vertprog
= {
1634 .mesa
= (_NEW_PROGRAM
|
1641 _NEW_TEXTURE_MATRIX
),
1642 .brw
= (BRW_NEW_FRAGMENT_PROGRAM
|
1643 BRW_NEW_INPUT_VARYING
),
1646 .prepare
= prepare_tnl_program
1652 static int prepare_active_vertprog( struct brw_context
*brw
)
1654 const struct gl_vertex_program
*prev
= brw
->vertex_program
;
1657 if (brw
->attribs
.VertexProgram
->_Current
) {
1658 brw
->vertex_program
= brw
->attribs
.VertexProgram
->_Current
;
1661 /* BRW_NEW_TNL_PROGRAM */
1662 brw
->vertex_program
= brw
->tnl_program
;
1665 if (brw
->vertex_program
!= prev
)
1666 brw
->state
.dirty
.brw
|= BRW_NEW_VERTEX_PROGRAM
;
1673 const struct brw_tracked_state brw_active_vertprog
= {
1675 .mesa
= _NEW_PROGRAM
,
1676 .brw
= BRW_NEW_TNL_PROGRAM
,
1679 .prepare
= prepare_active_vertprog
1683 void brw_ProgramCacheInit( GLcontext
*ctx
)
1685 struct brw_context
*brw
= brw_context(ctx
);
1687 brw
->tnl_program_cache
.size
= 17;
1688 brw
->tnl_program_cache
.n_items
= 0;
1689 brw
->tnl_program_cache
.items
= (struct brw_tnl_cache_item
**)
1690 _mesa_calloc(brw
->tnl_program_cache
.size
*
1691 sizeof(struct brw_tnl_cache_item
));
1694 void brw_ProgramCacheDestroy( GLcontext
*ctx
)
1696 struct brw_context
*brw
= brw_context(ctx
);
1697 struct brw_tnl_cache_item
*c
, *next
;
1700 for (i
= 0; i
< brw
->tnl_program_cache
.size
; i
++)
1701 for (c
= brw
->tnl_program_cache
.items
[i
]; c
; c
= next
) {
1708 FREE(brw
->tnl_program_cache
.items
);