1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 * \file ffvertex_prog.
31 * Create a vertex program to execute the current fixed function T&L pipeline.
32 * \author Keith Whitwell
36 #include "main/glheader.h"
37 #include "main/mtypes.h"
38 #include "main/macros.h"
39 #include "main/enums.h"
40 #include "main/ffvertex_prog.h"
41 #include "shader/program.h"
42 #include "shader/prog_cache.h"
43 #include "shader/prog_instruction.h"
44 #include "shader/prog_parameter.h"
45 #include "shader/prog_print.h"
46 #include "shader/prog_statevars.h"
50 unsigned light_global_enabled
:1;
51 unsigned light_local_viewer
:1;
52 unsigned light_twoside
:1;
53 unsigned light_color_material
:1;
54 unsigned light_color_material_mask
:12;
55 unsigned light_material_mask
:12;
56 unsigned material_shininess_is_zero
:1;
58 unsigned need_eye_coords
:1;
60 unsigned rescale_normals
:1;
61 unsigned fog_source_is_depth
:1;
62 unsigned tnl_do_vertex_fog
:1;
63 unsigned separate_specular
:1;
65 unsigned point_attenuated
:1;
66 unsigned point_array
:1;
67 unsigned texture_enabled_global
:1;
68 unsigned fragprog_inputs_read
:12;
71 unsigned light_enabled
:1;
72 unsigned light_eyepos3_is_zero
:1;
73 unsigned light_spotcutoff_is_180
:1;
74 unsigned light_attenuated
:1;
75 unsigned texunit_really_enabled
:1;
76 unsigned texmat_enabled
:1;
77 unsigned texgen_enabled
:4;
78 unsigned texgen_mode0
:4;
79 unsigned texgen_mode1
:4;
80 unsigned texgen_mode2
:4;
81 unsigned texgen_mode3
:4;
92 static GLuint
translate_fog_mode( GLenum mode
)
95 case GL_LINEAR
: return FOG_LINEAR
;
96 case GL_EXP
: return FOG_EXP
;
97 case GL_EXP2
: return FOG_EXP2
;
98 default: return FOG_NONE
;
103 #define TXG_OBJ_LINEAR 1
104 #define TXG_EYE_LINEAR 2
105 #define TXG_SPHERE_MAP 3
106 #define TXG_REFLECTION_MAP 4
107 #define TXG_NORMAL_MAP 5
109 static GLuint
translate_texgen( GLboolean enabled
, GLenum mode
)
115 case GL_OBJECT_LINEAR
: return TXG_OBJ_LINEAR
;
116 case GL_EYE_LINEAR
: return TXG_EYE_LINEAR
;
117 case GL_SPHERE_MAP
: return TXG_SPHERE_MAP
;
118 case GL_REFLECTION_MAP_NV
: return TXG_REFLECTION_MAP
;
119 case GL_NORMAL_MAP_NV
: return TXG_NORMAL_MAP
;
120 default: return TXG_NONE
;
126 * Returns bitmask of flags indicating which materials are set per-vertex
128 * XXX get these from the VBO...
131 tnl_get_per_vertex_materials(GLcontext
*ctx
)
133 GLbitfield mask
= 0x0;
135 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
136 struct vertex_buffer
*VB
= &tnl
->vb
;
139 for (i
= _TNL_FIRST_MAT
; i
<= _TNL_LAST_MAT
; i
++)
140 if (VB
->AttribPtr
[i
] && VB
->AttribPtr
[i
]->stride
)
141 mask
|= 1 << (i
- _TNL_FIRST_MAT
);
147 * Should fog be computed per-vertex?
150 tnl_get_per_vertex_fog(GLcontext
*ctx
)
153 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
154 return tnl
->_DoVertexFog
;
160 static GLboolean
check_active_shininess( GLcontext
*ctx
,
161 const struct state_key
*key
,
164 GLuint bit
= 1 << (MAT_ATTRIB_FRONT_SHININESS
+ side
);
166 if (key
->light_color_material_mask
& bit
)
169 if (key
->light_material_mask
& bit
)
172 if (ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SHININESS
+ side
][0] != 0.0F
)
181 static void make_state_key( GLcontext
*ctx
, struct state_key
*key
)
183 const struct gl_fragment_program
*fp
;
186 memset(key
, 0, sizeof(struct state_key
));
187 fp
= ctx
->FragmentProgram
._Current
;
189 /* This now relies on texenvprogram.c being active:
193 key
->need_eye_coords
= ctx
->_NeedEyeCoords
;
195 key
->fragprog_inputs_read
= fp
->Base
.InputsRead
;
197 if (ctx
->RenderMode
== GL_FEEDBACK
) {
198 /* make sure the vertprog emits color and tex0 */
199 key
->fragprog_inputs_read
|= (FRAG_BIT_COL0
| FRAG_BIT_TEX0
);
202 key
->separate_specular
= (ctx
->Light
.Model
.ColorControl
==
203 GL_SEPARATE_SPECULAR_COLOR
);
205 if (ctx
->Light
.Enabled
) {
206 key
->light_global_enabled
= 1;
208 if (ctx
->Light
.Model
.LocalViewer
)
209 key
->light_local_viewer
= 1;
211 if (ctx
->Light
.Model
.TwoSide
)
212 key
->light_twoside
= 1;
214 if (ctx
->Light
.ColorMaterialEnabled
) {
215 key
->light_color_material
= 1;
216 key
->light_color_material_mask
= ctx
->Light
.ColorMaterialBitmask
;
219 key
->light_material_mask
= tnl_get_per_vertex_materials(ctx
);
221 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
222 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
224 if (light
->Enabled
) {
225 key
->unit
[i
].light_enabled
= 1;
227 if (light
->EyePosition
[3] == 0.0)
228 key
->unit
[i
].light_eyepos3_is_zero
= 1;
230 if (light
->SpotCutoff
== 180.0)
231 key
->unit
[i
].light_spotcutoff_is_180
= 1;
233 if (light
->ConstantAttenuation
!= 1.0 ||
234 light
->LinearAttenuation
!= 0.0 ||
235 light
->QuadraticAttenuation
!= 0.0)
236 key
->unit
[i
].light_attenuated
= 1;
240 if (check_active_shininess(ctx
, key
, 0)) {
241 key
->material_shininess_is_zero
= 0;
243 else if (key
->light_twoside
&&
244 check_active_shininess(ctx
, key
, 1)) {
245 key
->material_shininess_is_zero
= 0;
248 key
->material_shininess_is_zero
= 1;
252 if (ctx
->Transform
.Normalize
)
255 if (ctx
->Transform
.RescaleNormals
)
256 key
->rescale_normals
= 1;
258 key
->fog_mode
= translate_fog_mode(fp
->FogOption
);
260 if (ctx
->Fog
.FogCoordinateSource
== GL_FRAGMENT_DEPTH_EXT
)
261 key
->fog_source_is_depth
= 1;
263 key
->tnl_do_vertex_fog
= tnl_get_per_vertex_fog(ctx
);
265 if (ctx
->Point
._Attenuated
)
266 key
->point_attenuated
= 1;
268 #if FEATURE_point_size_array
269 if (ctx
->Array
.ArrayObj
->PointSize
.Enabled
)
270 key
->point_array
= 1;
273 if (ctx
->Texture
._TexGenEnabled
||
274 ctx
->Texture
._TexMatEnabled
||
275 ctx
->Texture
._EnabledUnits
)
276 key
->texture_enabled_global
= 1;
278 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
279 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
281 if (texUnit
->_ReallyEnabled
)
282 key
->unit
[i
].texunit_really_enabled
= 1;
284 if (ctx
->Texture
._TexMatEnabled
& ENABLE_TEXMAT(i
))
285 key
->unit
[i
].texmat_enabled
= 1;
287 if (texUnit
->TexGenEnabled
) {
288 key
->unit
[i
].texgen_enabled
= 1;
290 key
->unit
[i
].texgen_mode0
=
291 translate_texgen( texUnit
->TexGenEnabled
& (1<<0),
293 key
->unit
[i
].texgen_mode1
=
294 translate_texgen( texUnit
->TexGenEnabled
& (1<<1),
296 key
->unit
[i
].texgen_mode2
=
297 translate_texgen( texUnit
->TexGenEnabled
& (1<<2),
299 key
->unit
[i
].texgen_mode3
=
300 translate_texgen( texUnit
->TexGenEnabled
& (1<<3),
308 /* Very useful debugging tool - produces annotated listing of
309 * generated program with line/function references for each
310 * instruction back into this file:
314 /* Should be tunable by the driver - do we want to do matrix
315 * multiplications with DP4's or with MUL/MAD's? SSE works better
316 * with the latter, drivers may differ.
321 /* Use uregs to represent registers internally, translate to Mesa's
322 * expected formats on emit.
324 * NOTE: These are passed by value extensively in this file rather
325 * than as usual by pointer reference. If this disturbs you, try
326 * remembering they are just 32bits in size.
328 * GCC is smart enough to deal with these dword-sized structures in
329 * much the same way as if I had defined them as dwords and was using
330 * macros to access and set the fields. This is much nicer and easier
335 GLint idx
:9; /* relative addressing may be negative */
336 /* sizeof(idx) should == sizeof(prog_src_reg::Index) */
344 const struct state_key
*state
;
345 struct gl_vertex_program
*program
;
346 GLint max_inst
; /** number of instructions allocated for program */
349 GLuint temp_reserved
;
351 struct ureg eye_position
;
352 struct ureg eye_position_z
;
353 struct ureg eye_position_normalized
;
354 struct ureg transformed_normal
;
355 struct ureg identity
;
358 GLuint color_materials
;
362 static const struct ureg undef
= {
380 static struct ureg
make_ureg(GLuint file
, GLint idx
)
386 reg
.swz
= SWIZZLE_NOOP
;
393 static struct ureg
negate( struct ureg reg
)
400 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
402 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
405 GET_SWZ(reg
.swz
, w
));
410 static struct ureg
swizzle1( struct ureg reg
, int x
)
412 return swizzle(reg
, x
, x
, x
, x
);
415 static struct ureg
get_temp( struct tnl_program
*p
)
417 int bit
= _mesa_ffs( ~p
->temp_in_use
);
419 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
423 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
424 p
->program
->Base
.NumTemporaries
= bit
;
426 p
->temp_in_use
|= 1<<(bit
-1);
427 return make_ureg(PROGRAM_TEMPORARY
, bit
-1);
430 static struct ureg
reserve_temp( struct tnl_program
*p
)
432 struct ureg temp
= get_temp( p
);
433 p
->temp_reserved
|= 1<<temp
.idx
;
437 static void release_temp( struct tnl_program
*p
, struct ureg reg
)
439 if (reg
.file
== PROGRAM_TEMPORARY
) {
440 p
->temp_in_use
&= ~(1<<reg
.idx
);
441 p
->temp_in_use
|= p
->temp_reserved
; /* can't release reserved temps */
445 static void release_temps( struct tnl_program
*p
)
447 p
->temp_in_use
= p
->temp_reserved
;
453 * \param input one of VERT_ATTRIB_x tokens.
455 static struct ureg
register_input( struct tnl_program
*p
, GLuint input
)
457 p
->program
->Base
.InputsRead
|= (1<<input
);
458 return make_ureg(PROGRAM_INPUT
, input
);
462 * \param input one of VERT_RESULT_x tokens.
464 static struct ureg
register_output( struct tnl_program
*p
, GLuint output
)
466 p
->program
->Base
.OutputsWritten
|= (1<<output
);
467 return make_ureg(PROGRAM_OUTPUT
, output
);
470 static struct ureg
register_const4f( struct tnl_program
*p
,
483 idx
= _mesa_add_unnamed_constant( p
->program
->Base
.Parameters
, values
, 4,
485 ASSERT(swizzle
== SWIZZLE_NOOP
);
486 return make_ureg(PROGRAM_CONSTANT
, idx
);
489 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
490 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
491 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
492 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
494 static GLboolean
is_undef( struct ureg reg
)
496 return reg
.file
== PROGRAM_UNDEFINED
;
499 static struct ureg
get_identity_param( struct tnl_program
*p
)
501 if (is_undef(p
->identity
))
502 p
->identity
= register_const4f(p
, 0,0,0,1);
507 static struct ureg
register_param5(struct tnl_program
*p
,
514 gl_state_index tokens
[STATE_LENGTH
];
521 idx
= _mesa_add_state_reference( p
->program
->Base
.Parameters
, tokens
);
522 return make_ureg(PROGRAM_STATE_VAR
, idx
);
526 #define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
527 #define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
528 #define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
529 #define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
532 static void register_matrix_param5( struct tnl_program
*p
,
533 GLint s0
, /* modelview, projection, etc */
534 GLint s1
, /* texture matrix number */
535 GLint s2
, /* first row */
536 GLint s3
, /* last row */
537 GLint s4
, /* inverse, transpose, etc */
538 struct ureg
*matrix
)
542 /* This is a bit sad as the support is there to pull the whole
543 * matrix out in one go:
545 for (i
= 0; i
<= s3
- s2
; i
++)
546 matrix
[i
] = register_param5( p
, s0
, s1
, i
, i
, s4
);
550 static void emit_arg( struct prog_src_register
*src
,
553 src
->File
= reg
.file
;
554 src
->Index
= reg
.idx
;
555 src
->Swizzle
= reg
.swz
;
556 src
->NegateBase
= reg
.negate
? NEGATE_XYZW
: 0;
560 /* Check that bitfield sizes aren't exceeded */
561 ASSERT(src
->Index
== reg
.idx
);
564 static void emit_dst( struct prog_dst_register
*dst
,
565 struct ureg reg
, GLuint mask
)
567 dst
->File
= reg
.file
;
568 dst
->Index
= reg
.idx
;
569 /* allow zero as a shorthand for xyzw */
570 dst
->WriteMask
= mask
? mask
: WRITEMASK_XYZW
;
571 dst
->CondMask
= COND_TR
; /* always pass cond test */
572 dst
->CondSwizzle
= SWIZZLE_NOOP
;
575 /* Check that bitfield sizes aren't exceeded */
576 ASSERT(dst
->Index
== reg
.idx
);
579 static void debug_insn( struct prog_instruction
*inst
, const char *fn
,
583 static const char *last_fn
;
587 _mesa_printf("%s:\n", fn
);
590 _mesa_printf("%d:\t", line
);
591 _mesa_print_instruction(inst
);
596 static void emit_op3fn(struct tnl_program
*p
,
607 struct prog_instruction
*inst
;
609 assert((GLint
) p
->program
->Base
.NumInstructions
<= p
->max_inst
);
611 if (p
->program
->Base
.NumInstructions
== p
->max_inst
) {
612 /* need to extend the program's instruction array */
613 struct prog_instruction
*newInst
;
615 /* double the size */
618 newInst
= _mesa_alloc_instructions(p
->max_inst
);
620 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "vertex program build");
624 _mesa_copy_instructions(newInst
,
625 p
->program
->Base
.Instructions
,
626 p
->program
->Base
.NumInstructions
);
628 _mesa_free_instructions(p
->program
->Base
.Instructions
,
629 p
->program
->Base
.NumInstructions
);
631 p
->program
->Base
.Instructions
= newInst
;
634 nr
= p
->program
->Base
.NumInstructions
++;
636 inst
= &p
->program
->Base
.Instructions
[nr
];
637 inst
->Opcode
= (enum prog_opcode
) op
;
641 emit_arg( &inst
->SrcReg
[0], src0
);
642 emit_arg( &inst
->SrcReg
[1], src1
);
643 emit_arg( &inst
->SrcReg
[2], src2
);
645 emit_dst( &inst
->DstReg
, dest
, mask
);
647 debug_insn(inst
, fn
, line
);
651 #define emit_op3(p, op, dst, mask, src0, src1, src2) \
652 emit_op3fn(p, op, dst, mask, src0, src1, src2, __FUNCTION__, __LINE__)
654 #define emit_op2(p, op, dst, mask, src0, src1) \
655 emit_op3fn(p, op, dst, mask, src0, src1, undef, __FUNCTION__, __LINE__)
657 #define emit_op1(p, op, dst, mask, src0) \
658 emit_op3fn(p, op, dst, mask, src0, undef, undef, __FUNCTION__, __LINE__)
661 static struct ureg
make_temp( struct tnl_program
*p
, struct ureg reg
)
663 if (reg
.file
== PROGRAM_TEMPORARY
&&
664 !(p
->temp_reserved
& (1<<reg
.idx
)))
667 struct ureg temp
= get_temp(p
);
668 emit_op1(p
, OPCODE_MOV
, temp
, 0, reg
);
674 /* Currently no tracking performed of input/output/register size or
675 * active elements. Could be used to reduce these operations, as
676 * could the matrix type.
678 static void emit_matrix_transform_vec4( struct tnl_program
*p
,
680 const struct ureg
*mat
,
683 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_X
, src
, mat
[0]);
684 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_Y
, src
, mat
[1]);
685 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_Z
, src
, mat
[2]);
686 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_W
, src
, mat
[3]);
689 /* This version is much easier to implement if writemasks are not
690 * supported natively on the target or (like SSE), the target doesn't
691 * have a clean/obvious dotproduct implementation.
693 static void emit_transpose_matrix_transform_vec4( struct tnl_program
*p
,
695 const struct ureg
*mat
,
700 if (dest
.file
!= PROGRAM_TEMPORARY
)
705 emit_op2(p
, OPCODE_MUL
, tmp
, 0, swizzle1(src
,X
), mat
[0]);
706 emit_op3(p
, OPCODE_MAD
, tmp
, 0, swizzle1(src
,Y
), mat
[1], tmp
);
707 emit_op3(p
, OPCODE_MAD
, tmp
, 0, swizzle1(src
,Z
), mat
[2], tmp
);
708 emit_op3(p
, OPCODE_MAD
, dest
, 0, swizzle1(src
,W
), mat
[3], tmp
);
710 if (dest
.file
!= PROGRAM_TEMPORARY
)
711 release_temp(p
, tmp
);
714 static void emit_matrix_transform_vec3( struct tnl_program
*p
,
716 const struct ureg
*mat
,
719 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_X
, src
, mat
[0]);
720 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_Y
, src
, mat
[1]);
721 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_Z
, src
, mat
[2]);
725 static void emit_normalize_vec3( struct tnl_program
*p
,
730 /* XXX use this when drivers are ready for NRM3 */
731 emit_op1(p
, OPCODE_NRM3
, dest
, WRITEMASK_XYZ
, src
);
733 struct ureg tmp
= get_temp(p
);
734 emit_op2(p
, OPCODE_DP3
, tmp
, WRITEMASK_X
, src
, src
);
735 emit_op1(p
, OPCODE_RSQ
, tmp
, WRITEMASK_X
, tmp
);
736 emit_op2(p
, OPCODE_MUL
, dest
, 0, src
, swizzle1(tmp
, X
));
737 release_temp(p
, tmp
);
741 static void emit_passthrough( struct tnl_program
*p
,
745 struct ureg out
= register_output(p
, output
);
746 emit_op1(p
, OPCODE_MOV
, out
, 0, register_input(p
, input
));
749 static struct ureg
get_eye_position( struct tnl_program
*p
)
751 if (is_undef(p
->eye_position
)) {
752 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
753 struct ureg modelview
[4];
755 p
->eye_position
= reserve_temp(p
);
758 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 3,
761 emit_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
764 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 3,
765 STATE_MATRIX_TRANSPOSE
, modelview
);
767 emit_transpose_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
771 return p
->eye_position
;
775 static struct ureg
get_eye_position_z( struct tnl_program
*p
)
777 if (!is_undef(p
->eye_position
))
778 return swizzle1(p
->eye_position
, Z
);
780 if (is_undef(p
->eye_position_z
)) {
781 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
782 struct ureg modelview
[4];
784 p
->eye_position_z
= reserve_temp(p
);
786 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 3,
789 emit_op2(p
, OPCODE_DP4
, p
->eye_position_z
, 0, pos
, modelview
[2]);
792 return p
->eye_position_z
;
797 static struct ureg
get_eye_position_normalized( struct tnl_program
*p
)
799 if (is_undef(p
->eye_position_normalized
)) {
800 struct ureg eye
= get_eye_position(p
);
801 p
->eye_position_normalized
= reserve_temp(p
);
802 emit_normalize_vec3(p
, p
->eye_position_normalized
, eye
);
805 return p
->eye_position_normalized
;
809 static struct ureg
get_transformed_normal( struct tnl_program
*p
)
811 if (is_undef(p
->transformed_normal
) &&
812 !p
->state
->need_eye_coords
&&
813 !p
->state
->normalize
&&
814 !(p
->state
->need_eye_coords
== p
->state
->rescale_normals
))
816 p
->transformed_normal
= register_input(p
, VERT_ATTRIB_NORMAL
);
818 else if (is_undef(p
->transformed_normal
))
820 struct ureg normal
= register_input(p
, VERT_ATTRIB_NORMAL
);
821 struct ureg mvinv
[3];
822 struct ureg transformed_normal
= reserve_temp(p
);
824 if (p
->state
->need_eye_coords
) {
825 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 2,
826 STATE_MATRIX_INVTRANS
, mvinv
);
828 /* Transform to eye space:
830 emit_matrix_transform_vec3( p
, transformed_normal
, mvinv
, normal
);
831 normal
= transformed_normal
;
834 /* Normalize/Rescale:
836 if (p
->state
->normalize
) {
837 emit_normalize_vec3( p
, transformed_normal
, normal
);
838 normal
= transformed_normal
;
840 else if (p
->state
->need_eye_coords
== p
->state
->rescale_normals
) {
841 /* This is already adjusted for eye/non-eye rendering:
843 struct ureg rescale
= register_param2(p
, STATE_INTERNAL
,
846 emit_op2( p
, OPCODE_MUL
, transformed_normal
, 0, normal
, rescale
);
847 normal
= transformed_normal
;
850 assert(normal
.file
== PROGRAM_TEMPORARY
);
851 p
->transformed_normal
= normal
;
854 return p
->transformed_normal
;
859 static void build_hpos( struct tnl_program
*p
)
861 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
862 struct ureg hpos
= register_output( p
, VERT_RESULT_HPOS
);
866 register_matrix_param5( p
, STATE_MVP_MATRIX
, 0, 0, 3,
868 emit_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
871 register_matrix_param5( p
, STATE_MVP_MATRIX
, 0, 0, 3,
872 STATE_MATRIX_TRANSPOSE
, mvp
);
873 emit_transpose_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
878 static GLuint
material_attrib( GLuint side
, GLuint property
)
880 return ((property
- STATE_AMBIENT
) * 2 +
884 /* Get a bitmask of which material values vary on a per-vertex basis.
886 static void set_material_flags( struct tnl_program
*p
)
888 p
->color_materials
= 0;
891 if (p
->state
->light_color_material
) {
893 p
->color_materials
= p
->state
->light_color_material_mask
;
896 p
->materials
|= p
->state
->light_material_mask
;
900 /* XXX temporary!!! */
901 #define _TNL_ATTRIB_MAT_FRONT_AMBIENT 32
903 static struct ureg
get_material( struct tnl_program
*p
, GLuint side
,
906 GLuint attrib
= material_attrib(side
, property
);
908 if (p
->color_materials
& (1<<attrib
))
909 return register_input(p
, VERT_ATTRIB_COLOR0
);
910 else if (p
->materials
& (1<<attrib
))
911 return register_input( p
, attrib
+ _TNL_ATTRIB_MAT_FRONT_AMBIENT
);
913 return register_param3( p
, STATE_MATERIAL
, side
, property
);
916 #define SCENE_COLOR_BITS(side) (( MAT_BIT_FRONT_EMISSION | \
917 MAT_BIT_FRONT_AMBIENT | \
918 MAT_BIT_FRONT_DIFFUSE) << (side))
920 /* Either return a precalculated constant value or emit code to
921 * calculate these values dynamically in the case where material calls
922 * are present between begin/end pairs.
924 * Probably want to shift this to the program compilation phase - if
925 * we always emitted the calculation here, a smart compiler could
926 * detect that it was constant (given a certain set of inputs), and
927 * lift it out of the main loop. That way the programs created here
928 * would be independent of the vertex_buffer details.
930 static struct ureg
get_scenecolor( struct tnl_program
*p
, GLuint side
)
932 if (p
->materials
& SCENE_COLOR_BITS(side
)) {
933 struct ureg lm_ambient
= register_param1(p
, STATE_LIGHTMODEL_AMBIENT
);
934 struct ureg material_emission
= get_material(p
, side
, STATE_EMISSION
);
935 struct ureg material_ambient
= get_material(p
, side
, STATE_AMBIENT
);
936 struct ureg material_diffuse
= get_material(p
, side
, STATE_DIFFUSE
);
937 struct ureg tmp
= make_temp(p
, material_diffuse
);
938 emit_op3(p
, OPCODE_MAD
, tmp
, WRITEMASK_XYZ
, lm_ambient
,
939 material_ambient
, material_emission
);
943 return register_param2( p
, STATE_LIGHTMODEL_SCENECOLOR
, side
);
947 static struct ureg
get_lightprod( struct tnl_program
*p
, GLuint light
,
948 GLuint side
, GLuint property
)
950 GLuint attrib
= material_attrib(side
, property
);
951 if (p
->materials
& (1<<attrib
)) {
952 struct ureg light_value
=
953 register_param3(p
, STATE_LIGHT
, light
, property
);
954 struct ureg material_value
= get_material(p
, side
, property
);
955 struct ureg tmp
= get_temp(p
);
956 emit_op2(p
, OPCODE_MUL
, tmp
, 0, light_value
, material_value
);
960 return register_param4(p
, STATE_LIGHTPROD
, light
, side
, property
);
963 static struct ureg
calculate_light_attenuation( struct tnl_program
*p
,
968 struct ureg attenuation
= register_param3(p
, STATE_LIGHT
, i
,
970 struct ureg att
= get_temp(p
);
972 /* Calculate spot attenuation:
974 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
975 struct ureg spot_dir_norm
= register_param3(p
, STATE_INTERNAL
,
976 STATE_LIGHT_SPOT_DIR_NORMALIZED
, i
);
977 struct ureg spot
= get_temp(p
);
978 struct ureg slt
= get_temp(p
);
980 emit_op2(p
, OPCODE_DP3
, spot
, 0, negate(VPpli
), spot_dir_norm
);
981 emit_op2(p
, OPCODE_SLT
, slt
, 0, swizzle1(spot_dir_norm
,W
), spot
);
982 emit_op2(p
, OPCODE_POW
, spot
, 0, spot
, swizzle1(attenuation
, W
));
983 emit_op2(p
, OPCODE_MUL
, att
, 0, slt
, spot
);
985 release_temp(p
, spot
);
986 release_temp(p
, slt
);
989 /* Calculate distance attenuation:
991 if (p
->state
->unit
[i
].light_attenuated
) {
994 emit_op1(p
, OPCODE_RCP
, dist
, WRITEMASK_YZ
, dist
);
996 emit_op2(p
, OPCODE_MUL
, dist
, WRITEMASK_XZ
, dist
, swizzle1(dist
,Y
));
998 emit_op2(p
, OPCODE_DP3
, dist
, 0, attenuation
, dist
);
1000 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
1002 emit_op1(p
, OPCODE_RCP
, dist
, 0, dist
);
1003 /* spot-atten * dist-atten */
1004 emit_op2(p
, OPCODE_MUL
, att
, 0, dist
, att
);
1007 emit_op1(p
, OPCODE_RCP
, att
, 0, dist
);
1017 * lit.y = MAX(0, dots.x)
1018 * lit.z = SLT(0, dots.x)
1020 static void emit_degenerate_lit( struct tnl_program
*p
,
1024 struct ureg id
= get_identity_param(p
); /* id = {0,0,0,1} */
1026 /* Note that lit.x & lit.w will not be examined. Note also that
1027 * dots.xyzw == dots.xxxx.
1030 /* MAX lit, id, dots;
1032 emit_op2(p
, OPCODE_MAX
, lit
, WRITEMASK_XYZW
, id
, dots
);
1034 /* result[2] = (in > 0 ? 1 : 0)
1035 * SLT lit.z, id.z, dots; # lit.z = (0 < dots.z) ? 1 : 0
1037 emit_op2(p
, OPCODE_SLT
, lit
, WRITEMASK_Z
, swizzle1(id
,Z
), dots
);
1041 /* Need to add some addtional parameters to allow lighting in object
1042 * space - STATE_SPOT_DIRECTION and STATE_HALF_VECTOR implicitly assume eye
1045 static void build_lighting( struct tnl_program
*p
)
1047 const GLboolean twoside
= p
->state
->light_twoside
;
1048 const GLboolean separate
= p
->state
->separate_specular
;
1049 GLuint nr_lights
= 0, count
= 0;
1050 struct ureg normal
= get_transformed_normal(p
);
1051 struct ureg lit
= get_temp(p
);
1052 struct ureg dots
= get_temp(p
);
1053 struct ureg _col0
= undef
, _col1
= undef
;
1054 struct ureg _bfc0
= undef
, _bfc1
= undef
;
1059 * dot.x = dot(normal, VPpli)
1060 * dot.y = dot(normal, halfAngle)
1061 * dot.z = back.shininess
1062 * dot.w = front.shininess
1065 for (i
= 0; i
< MAX_LIGHTS
; i
++)
1066 if (p
->state
->unit
[i
].light_enabled
)
1069 set_material_flags(p
);
1072 if (!p
->state
->material_shininess_is_zero
) {
1073 struct ureg shininess
= get_material(p
, 0, STATE_SHININESS
);
1074 emit_op1(p
, OPCODE_MOV
, dots
, WRITEMASK_W
, swizzle1(shininess
,X
));
1075 release_temp(p
, shininess
);
1078 _col0
= make_temp(p
, get_scenecolor(p
, 0));
1080 _col1
= make_temp(p
, get_identity_param(p
));
1087 if (!p
->state
->material_shininess_is_zero
) {
1088 struct ureg shininess
= get_material(p
, 1, STATE_SHININESS
);
1089 emit_op1(p
, OPCODE_MOV
, dots
, WRITEMASK_Z
,
1090 negate(swizzle1(shininess
,X
)));
1091 release_temp(p
, shininess
);
1094 _bfc0
= make_temp(p
, get_scenecolor(p
, 1));
1096 _bfc1
= make_temp(p
, get_identity_param(p
));
1101 /* If no lights, still need to emit the scenecolor.
1104 struct ureg res0
= register_output( p
, VERT_RESULT_COL0
);
1105 emit_op1(p
, OPCODE_MOV
, res0
, 0, _col0
);
1109 struct ureg res1
= register_output( p
, VERT_RESULT_COL1
);
1110 emit_op1(p
, OPCODE_MOV
, res1
, 0, _col1
);
1114 struct ureg res0
= register_output( p
, VERT_RESULT_BFC0
);
1115 emit_op1(p
, OPCODE_MOV
, res0
, 0, _bfc0
);
1118 if (twoside
&& separate
) {
1119 struct ureg res1
= register_output( p
, VERT_RESULT_BFC1
);
1120 emit_op1(p
, OPCODE_MOV
, res1
, 0, _bfc1
);
1123 if (nr_lights
== 0) {
1128 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
1129 if (p
->state
->unit
[i
].light_enabled
) {
1130 struct ureg half
= undef
;
1131 struct ureg att
= undef
, VPpli
= undef
;
1135 if (p
->state
->unit
[i
].light_eyepos3_is_zero
) {
1136 /* Can used precomputed constants in this case.
1137 * Attenuation never applies to infinite lights.
1139 VPpli
= register_param3(p
, STATE_INTERNAL
,
1140 STATE_LIGHT_POSITION_NORMALIZED
, i
);
1142 if (!p
->state
->material_shininess_is_zero
) {
1143 if (p
->state
->light_local_viewer
) {
1144 struct ureg eye_hat
= get_eye_position_normalized(p
);
1146 emit_op2(p
, OPCODE_SUB
, half
, 0, VPpli
, eye_hat
);
1147 emit_normalize_vec3(p
, half
, half
);
1149 half
= register_param3(p
, STATE_INTERNAL
,
1150 STATE_LIGHT_HALF_VECTOR
, i
);
1155 struct ureg Ppli
= register_param3(p
, STATE_INTERNAL
,
1156 STATE_LIGHT_POSITION
, i
);
1157 struct ureg V
= get_eye_position(p
);
1158 struct ureg dist
= get_temp(p
);
1160 VPpli
= get_temp(p
);
1162 /* Calculate VPpli vector
1164 emit_op2(p
, OPCODE_SUB
, VPpli
, 0, Ppli
, V
);
1166 /* Normalize VPpli. The dist value also used in
1167 * attenuation below.
1169 emit_op2(p
, OPCODE_DP3
, dist
, 0, VPpli
, VPpli
);
1170 emit_op1(p
, OPCODE_RSQ
, dist
, 0, dist
);
1171 emit_op2(p
, OPCODE_MUL
, VPpli
, 0, VPpli
, dist
);
1173 /* Calculate attenuation:
1175 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
||
1176 p
->state
->unit
[i
].light_attenuated
) {
1177 att
= calculate_light_attenuation(p
, i
, VPpli
, dist
);
1180 /* Calculate viewer direction, or use infinite viewer:
1182 if (!p
->state
->material_shininess_is_zero
) {
1185 if (p
->state
->light_local_viewer
) {
1186 struct ureg eye_hat
= get_eye_position_normalized(p
);
1187 emit_op2(p
, OPCODE_SUB
, half
, 0, VPpli
, eye_hat
);
1190 struct ureg z_dir
= swizzle(get_identity_param(p
),X
,Y
,W
,Z
);
1191 emit_op2(p
, OPCODE_ADD
, half
, 0, VPpli
, z_dir
);
1194 emit_normalize_vec3(p
, half
, half
);
1197 release_temp(p
, dist
);
1200 /* Calculate dot products:
1202 if (p
->state
->material_shininess_is_zero
) {
1203 emit_op2(p
, OPCODE_DP3
, dots
, 0, normal
, VPpli
);
1206 emit_op2(p
, OPCODE_DP3
, dots
, WRITEMASK_X
, normal
, VPpli
);
1207 emit_op2(p
, OPCODE_DP3
, dots
, WRITEMASK_Y
, normal
, half
);
1210 /* Front face lighting:
1213 struct ureg ambient
= get_lightprod(p
, i
, 0, STATE_AMBIENT
);
1214 struct ureg diffuse
= get_lightprod(p
, i
, 0, STATE_DIFFUSE
);
1215 struct ureg specular
= get_lightprod(p
, i
, 0, STATE_SPECULAR
);
1216 struct ureg res0
, res1
;
1217 GLuint mask0
, mask1
;
1220 if (count
== nr_lights
) {
1222 mask0
= WRITEMASK_XYZ
;
1223 mask1
= WRITEMASK_XYZ
;
1224 res0
= register_output( p
, VERT_RESULT_COL0
);
1225 res1
= register_output( p
, VERT_RESULT_COL1
);
1229 mask1
= WRITEMASK_XYZ
;
1231 res1
= register_output( p
, VERT_RESULT_COL0
);
1241 if (!is_undef(att
)) {
1242 /* light is attenuated by distance */
1243 emit_op1(p
, OPCODE_LIT
, lit
, 0, dots
);
1244 emit_op2(p
, OPCODE_MUL
, lit
, 0, lit
, att
);
1245 emit_op3(p
, OPCODE_MAD
, _col0
, 0, swizzle1(lit
,X
), ambient
, _col0
);
1247 else if (!p
->state
->material_shininess_is_zero
) {
1248 /* there's a non-zero specular term */
1249 emit_op1(p
, OPCODE_LIT
, lit
, 0, dots
);
1250 emit_op2(p
, OPCODE_ADD
, _col0
, 0, ambient
, _col0
);
1253 /* no attenutation, no specular */
1254 emit_degenerate_lit(p
, lit
, dots
);
1255 emit_op2(p
, OPCODE_ADD
, _col0
, 0, ambient
, _col0
);
1258 emit_op3(p
, OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _col0
);
1259 emit_op3(p
, OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _col1
);
1261 release_temp(p
, ambient
);
1262 release_temp(p
, diffuse
);
1263 release_temp(p
, specular
);
1266 /* Back face lighting:
1269 struct ureg ambient
= get_lightprod(p
, i
, 1, STATE_AMBIENT
);
1270 struct ureg diffuse
= get_lightprod(p
, i
, 1, STATE_DIFFUSE
);
1271 struct ureg specular
= get_lightprod(p
, i
, 1, STATE_SPECULAR
);
1272 struct ureg res0
, res1
;
1273 GLuint mask0
, mask1
;
1275 if (count
== nr_lights
) {
1277 mask0
= WRITEMASK_XYZ
;
1278 mask1
= WRITEMASK_XYZ
;
1279 res0
= register_output( p
, VERT_RESULT_BFC0
);
1280 res1
= register_output( p
, VERT_RESULT_BFC1
);
1284 mask1
= WRITEMASK_XYZ
;
1286 res1
= register_output( p
, VERT_RESULT_BFC0
);
1295 dots
= negate(swizzle(dots
,X
,Y
,W
,Z
));
1297 if (!is_undef(att
)) {
1298 emit_op1(p
, OPCODE_LIT
, lit
, 0, dots
);
1299 emit_op2(p
, OPCODE_MUL
, lit
, 0, lit
, att
);
1300 emit_op3(p
, OPCODE_MAD
, _bfc0
, 0, swizzle1(lit
,X
), ambient
, _bfc0
);
1302 else if (!p
->state
->material_shininess_is_zero
) {
1303 emit_op1(p
, OPCODE_LIT
, lit
, 0, dots
);
1304 emit_op2(p
, OPCODE_ADD
, _bfc0
, 0, ambient
, _bfc0
);
1307 emit_degenerate_lit(p
, lit
, dots
);
1308 emit_op2(p
, OPCODE_ADD
, _bfc0
, 0, ambient
, _bfc0
);
1311 emit_op3(p
, OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _bfc0
);
1312 emit_op3(p
, OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _bfc1
);
1314 /* restore negate flag for next lighting */
1315 dots
= negate(dots
);
1317 release_temp(p
, ambient
);
1318 release_temp(p
, diffuse
);
1319 release_temp(p
, specular
);
1322 release_temp(p
, half
);
1323 release_temp(p
, VPpli
);
1324 release_temp(p
, att
);
1332 static void build_fog( struct tnl_program
*p
)
1334 struct ureg fog
= register_output(p
, VERT_RESULT_FOGC
);
1337 if (p
->state
->fog_source_is_depth
) {
1338 input
= get_eye_position_z(p
);
1341 input
= swizzle1(register_input(p
, VERT_ATTRIB_FOG
), X
);
1344 if (p
->state
->fog_mode
&& p
->state
->tnl_do_vertex_fog
) {
1345 struct ureg params
= register_param2(p
, STATE_INTERNAL
,
1346 STATE_FOG_PARAMS_OPTIMIZED
);
1347 struct ureg tmp
= get_temp(p
);
1348 GLboolean useabs
= (p
->state
->fog_mode
!= FOG_EXP2
);
1351 emit_op1(p
, OPCODE_ABS
, tmp
, 0, input
);
1354 switch (p
->state
->fog_mode
) {
1356 struct ureg id
= get_identity_param(p
);
1357 emit_op3(p
, OPCODE_MAD
, tmp
, 0, useabs
? tmp
: input
,
1358 swizzle1(params
,X
), swizzle1(params
,Y
));
1359 emit_op2(p
, OPCODE_MAX
, tmp
, 0, tmp
, swizzle1(id
,X
)); /* saturate */
1360 emit_op2(p
, OPCODE_MIN
, fog
, WRITEMASK_X
, tmp
, swizzle1(id
,W
));
1364 emit_op2(p
, OPCODE_MUL
, tmp
, 0, useabs
? tmp
: input
,
1365 swizzle1(params
,Z
));
1366 emit_op1(p
, OPCODE_EX2
, fog
, WRITEMASK_X
, negate(tmp
));
1369 emit_op2(p
, OPCODE_MUL
, tmp
, 0, input
, swizzle1(params
,W
));
1370 emit_op2(p
, OPCODE_MUL
, tmp
, 0, tmp
, tmp
);
1371 emit_op1(p
, OPCODE_EX2
, fog
, WRITEMASK_X
, negate(tmp
));
1375 release_temp(p
, tmp
);
1378 /* results = incoming fog coords (compute fog per-fragment later)
1380 * KW: Is it really necessary to do anything in this case?
1381 * BP: Yes, we always need to compute the absolute value, unless
1382 * we want to push that down into the fragment program...
1384 GLboolean useabs
= GL_TRUE
;
1385 emit_op1(p
, useabs
? OPCODE_ABS
: OPCODE_MOV
, fog
, WRITEMASK_X
, input
);
1389 static void build_reflect_texgen( struct tnl_program
*p
,
1393 struct ureg normal
= get_transformed_normal(p
);
1394 struct ureg eye_hat
= get_eye_position_normalized(p
);
1395 struct ureg tmp
= get_temp(p
);
1398 emit_op2(p
, OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1400 emit_op2(p
, OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1402 emit_op3(p
, OPCODE_MAD
, dest
, writemask
, negate(tmp
), normal
, eye_hat
);
1404 release_temp(p
, tmp
);
1407 static void build_sphere_texgen( struct tnl_program
*p
,
1411 struct ureg normal
= get_transformed_normal(p
);
1412 struct ureg eye_hat
= get_eye_position_normalized(p
);
1413 struct ureg tmp
= get_temp(p
);
1414 struct ureg half
= register_scalar_const(p
, .5);
1415 struct ureg r
= get_temp(p
);
1416 struct ureg inv_m
= get_temp(p
);
1417 struct ureg id
= get_identity_param(p
);
1419 /* Could share the above calculations, but it would be
1420 * a fairly odd state for someone to set (both sphere and
1421 * reflection active for different texture coordinate
1422 * components. Of course - if two texture units enable
1423 * reflect and/or sphere, things start to tilt in favour
1424 * of seperating this out:
1428 emit_op2(p
, OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1430 emit_op2(p
, OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1432 emit_op3(p
, OPCODE_MAD
, r
, 0, negate(tmp
), normal
, eye_hat
);
1434 emit_op2(p
, OPCODE_ADD
, tmp
, 0, r
, swizzle(id
,X
,Y
,W
,Z
));
1435 /* rx^2 + ry^2 + (rz+1)^2 */
1436 emit_op2(p
, OPCODE_DP3
, tmp
, 0, tmp
, tmp
);
1438 emit_op1(p
, OPCODE_RSQ
, tmp
, 0, tmp
);
1440 emit_op2(p
, OPCODE_MUL
, inv_m
, 0, tmp
, half
);
1442 emit_op3(p
, OPCODE_MAD
, dest
, writemask
, r
, inv_m
, half
);
1444 release_temp(p
, tmp
);
1446 release_temp(p
, inv_m
);
1450 static void build_texture_transform( struct tnl_program
*p
)
1454 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
1456 if (!(p
->state
->fragprog_inputs_read
& FRAG_BIT_TEX(i
)))
1459 if (p
->state
->unit
[i
].texgen_enabled
||
1460 p
->state
->unit
[i
].texmat_enabled
) {
1462 GLuint texmat_enabled
= p
->state
->unit
[i
].texmat_enabled
;
1463 struct ureg out
= register_output(p
, VERT_RESULT_TEX0
+ i
);
1464 struct ureg out_texgen
= undef
;
1466 if (p
->state
->unit
[i
].texgen_enabled
) {
1467 GLuint copy_mask
= 0;
1468 GLuint sphere_mask
= 0;
1469 GLuint reflect_mask
= 0;
1470 GLuint normal_mask
= 0;
1474 out_texgen
= get_temp(p
);
1478 modes
[0] = p
->state
->unit
[i
].texgen_mode0
;
1479 modes
[1] = p
->state
->unit
[i
].texgen_mode1
;
1480 modes
[2] = p
->state
->unit
[i
].texgen_mode2
;
1481 modes
[3] = p
->state
->unit
[i
].texgen_mode3
;
1483 for (j
= 0; j
< 4; j
++) {
1485 case TXG_OBJ_LINEAR
: {
1486 struct ureg obj
= register_input(p
, VERT_ATTRIB_POS
);
1488 register_param3(p
, STATE_TEXGEN
, i
,
1489 STATE_TEXGEN_OBJECT_S
+ j
);
1491 emit_op2(p
, OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1495 case TXG_EYE_LINEAR
: {
1496 struct ureg eye
= get_eye_position(p
);
1498 register_param3(p
, STATE_TEXGEN
, i
,
1499 STATE_TEXGEN_EYE_S
+ j
);
1501 emit_op2(p
, OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1505 case TXG_SPHERE_MAP
:
1506 sphere_mask
|= WRITEMASK_X
<< j
;
1508 case TXG_REFLECTION_MAP
:
1509 reflect_mask
|= WRITEMASK_X
<< j
;
1511 case TXG_NORMAL_MAP
:
1512 normal_mask
|= WRITEMASK_X
<< j
;
1515 copy_mask
|= WRITEMASK_X
<< j
;
1522 build_sphere_texgen(p
, out_texgen
, sphere_mask
);
1526 build_reflect_texgen(p
, out_texgen
, reflect_mask
);
1530 struct ureg normal
= get_transformed_normal(p
);
1531 emit_op1(p
, OPCODE_MOV
, out_texgen
, normal_mask
, normal
);
1535 struct ureg in
= register_input(p
, VERT_ATTRIB_TEX0
+i
);
1536 emit_op1(p
, OPCODE_MOV
, out_texgen
, copy_mask
, in
);
1540 if (texmat_enabled
) {
1541 struct ureg texmat
[4];
1542 struct ureg in
= (!is_undef(out_texgen
) ?
1544 register_input(p
, VERT_ATTRIB_TEX0
+i
));
1546 register_matrix_param5( p
, STATE_TEXTURE_MATRIX
, i
, 0, 3,
1548 emit_matrix_transform_vec4( p
, out
, texmat
, in
);
1551 register_matrix_param5( p
, STATE_TEXTURE_MATRIX
, i
, 0, 3,
1552 STATE_MATRIX_TRANSPOSE
, texmat
);
1553 emit_transpose_matrix_transform_vec4( p
, out
, texmat
, in
);
1560 emit_passthrough(p
, VERT_ATTRIB_TEX0
+i
, VERT_RESULT_TEX0
+i
);
1567 * Point size attenuation computation.
1569 static void build_atten_pointsize( struct tnl_program
*p
)
1571 struct ureg eye
= get_eye_position_z(p
);
1572 struct ureg state_size
= register_param1(p
, STATE_POINT_SIZE
);
1573 struct ureg state_attenuation
= register_param1(p
, STATE_POINT_ATTENUATION
);
1574 struct ureg out
= register_output(p
, VERT_RESULT_PSIZ
);
1575 struct ureg ut
= get_temp(p
);
1578 emit_op1(p
, OPCODE_ABS
, ut
, WRITEMASK_Y
, swizzle1(eye
, Z
));
1579 /* p1 + dist * (p2 + dist * p3); */
1580 emit_op3(p
, OPCODE_MAD
, ut
, WRITEMASK_X
, swizzle1(ut
, Y
),
1581 swizzle1(state_attenuation
, Z
), swizzle1(state_attenuation
, Y
));
1582 emit_op3(p
, OPCODE_MAD
, ut
, WRITEMASK_X
, swizzle1(ut
, Y
),
1583 ut
, swizzle1(state_attenuation
, X
));
1585 /* 1 / sqrt(factor) */
1586 emit_op1(p
, OPCODE_RSQ
, ut
, WRITEMASK_X
, ut
);
1589 /* out = pointSize / sqrt(factor) */
1590 emit_op2(p
, OPCODE_MUL
, out
, WRITEMASK_X
, ut
, state_size
);
1592 /* this is a good place to clamp the point size since there's likely
1593 * no hardware registers to clamp point size at rasterization time.
1595 emit_op2(p
, OPCODE_MUL
, ut
, WRITEMASK_X
, ut
, state_size
);
1596 emit_op2(p
, OPCODE_MAX
, ut
, WRITEMASK_X
, ut
, swizzle1(state_size
, Y
));
1597 emit_op2(p
, OPCODE_MIN
, out
, WRITEMASK_X
, ut
, swizzle1(state_size
, Z
));
1600 release_temp(p
, ut
);
1604 * Emit constant point size.
1606 static void build_constant_pointsize( struct tnl_program
*p
)
1608 struct ureg state_size
= register_param1(p
, STATE_POINT_SIZE
);
1609 struct ureg out
= register_output(p
, VERT_RESULT_PSIZ
);
1610 emit_op1(p
, OPCODE_MOV
, out
, WRITEMASK_X
, state_size
);
1614 * Pass-though per-vertex point size, from user's point size array.
1616 static void build_array_pointsize( struct tnl_program
*p
)
1618 struct ureg in
= register_input(p
, VERT_ATTRIB_POINT_SIZE
);
1619 struct ureg out
= register_output(p
, VERT_RESULT_PSIZ
);
1620 emit_op1(p
, OPCODE_MOV
, out
, WRITEMASK_X
, in
);
1624 static void build_tnl_program( struct tnl_program
*p
)
1625 { /* Emit the program, starting with modelviewproject:
1629 /* Lighting calculations:
1631 if (p
->state
->fragprog_inputs_read
& (FRAG_BIT_COL0
|FRAG_BIT_COL1
)) {
1632 if (p
->state
->light_global_enabled
)
1635 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
1636 emit_passthrough(p
, VERT_ATTRIB_COLOR0
, VERT_RESULT_COL0
);
1638 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL1
)
1639 emit_passthrough(p
, VERT_ATTRIB_COLOR1
, VERT_RESULT_COL1
);
1643 if ((p
->state
->fragprog_inputs_read
& FRAG_BIT_FOGC
) ||
1644 p
->state
->fog_mode
!= FOG_NONE
)
1647 if (p
->state
->fragprog_inputs_read
& FRAG_BITS_TEX_ANY
)
1648 build_texture_transform(p
);
1650 if (p
->state
->point_attenuated
)
1651 build_atten_pointsize(p
);
1652 else if (p
->state
->point_array
)
1653 build_array_pointsize(p
);
1656 build_constant_pointsize(p
);
1658 (void) build_constant_pointsize
;
1663 emit_op1(p
, OPCODE_END
, undef
, 0, undef
);
1668 _mesa_printf ("\n");
1674 create_new_program( const struct state_key
*key
,
1675 struct gl_vertex_program
*program
,
1678 struct tnl_program p
;
1680 _mesa_memset(&p
, 0, sizeof(p
));
1682 p
.program
= program
;
1683 p
.eye_position
= undef
;
1684 p
.eye_position_z
= undef
;
1685 p
.eye_position_normalized
= undef
;
1686 p
.transformed_normal
= undef
;
1690 if (max_temps
>= sizeof(int) * 8)
1691 p
.temp_reserved
= 0;
1693 p
.temp_reserved
= ~((1<<max_temps
)-1);
1695 /* Start by allocating 32 instructions.
1696 * If we need more, we'll grow the instruction array as needed.
1699 p
.program
->Base
.Instructions
= _mesa_alloc_instructions(p
.max_inst
);
1700 p
.program
->Base
.String
= NULL
;
1701 p
.program
->Base
.NumInstructions
=
1702 p
.program
->Base
.NumTemporaries
=
1703 p
.program
->Base
.NumParameters
=
1704 p
.program
->Base
.NumAttributes
= p
.program
->Base
.NumAddressRegs
= 0;
1705 p
.program
->Base
.Parameters
= _mesa_new_parameter_list();
1706 p
.program
->Base
.InputsRead
= 0;
1707 p
.program
->Base
.OutputsWritten
= 0;
1709 build_tnl_program( &p
);
1714 * Return a vertex program which implements the current fixed-function
1715 * transform/lighting/texgen operations.
1716 * XXX move this into core mesa (main/)
1718 struct gl_vertex_program
*
1719 _mesa_get_fixed_func_vertex_program(GLcontext
*ctx
)
1721 struct gl_vertex_program
*prog
;
1722 struct state_key key
;
1724 /* Grab all the relevent state and put it in a single structure:
1726 make_state_key(ctx
, &key
);
1728 /* Look for an already-prepared program for this state:
1730 prog
= (struct gl_vertex_program
*)
1731 _mesa_search_program_cache(ctx
->VertexProgram
.Cache
, &key
, sizeof(key
));
1734 /* OK, we'll have to build a new one */
1736 _mesa_printf("Build new TNL program\n");
1738 prog
= (struct gl_vertex_program
*)
1739 ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
1743 create_new_program( &key
, prog
,
1744 ctx
->Const
.VertexProgram
.MaxTemps
);
1747 if (ctx
->Driver
.ProgramStringNotify
)
1748 ctx
->Driver
.ProgramStringNotify( ctx
, GL_VERTEX_PROGRAM_ARB
,
1751 _mesa_program_cache_insert(ctx
, ctx
->VertexProgram
.Cache
,
1752 &key
, sizeof(key
), &prog
->Base
);