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.c
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_color_material_mask
:12;
51 unsigned light_global_enabled
:1;
52 unsigned light_local_viewer
:1;
53 unsigned light_twoside
:1;
54 unsigned material_shininess_is_zero
:1;
55 unsigned need_eye_coords
:1;
57 unsigned rescale_normals
:1;
59 unsigned fog_source_is_depth
:1;
60 unsigned separate_specular
:1;
61 unsigned point_attenuated
:1;
62 unsigned point_array
:1;
63 unsigned texture_enabled_global
:1;
64 unsigned fragprog_inputs_read
:12;
66 unsigned varying_vp_inputs
;
69 unsigned light_enabled
:1;
70 unsigned light_eyepos3_is_zero
:1;
71 unsigned light_spotcutoff_is_180
:1;
72 unsigned light_attenuated
:1;
73 unsigned texunit_really_enabled
:1;
74 unsigned texmat_enabled
:1;
75 unsigned texgen_enabled
:4;
76 unsigned texgen_mode0
:4;
77 unsigned texgen_mode1
:4;
78 unsigned texgen_mode2
:4;
79 unsigned texgen_mode3
:4;
85 #define TXG_OBJ_LINEAR 1
86 #define TXG_EYE_LINEAR 2
87 #define TXG_SPHERE_MAP 3
88 #define TXG_REFLECTION_MAP 4
89 #define TXG_NORMAL_MAP 5
91 static GLuint
translate_texgen( GLboolean enabled
, GLenum mode
)
97 case GL_OBJECT_LINEAR
: return TXG_OBJ_LINEAR
;
98 case GL_EYE_LINEAR
: return TXG_EYE_LINEAR
;
99 case GL_SPHERE_MAP
: return TXG_SPHERE_MAP
;
100 case GL_REFLECTION_MAP_NV
: return TXG_REFLECTION_MAP
;
101 case GL_NORMAL_MAP_NV
: return TXG_NORMAL_MAP
;
102 default: return TXG_NONE
;
108 static GLboolean
check_active_shininess( GLcontext
*ctx
,
109 const struct state_key
*key
,
112 GLuint bit
= 1 << (MAT_ATTRIB_FRONT_SHININESS
+ side
);
114 if ((key
->varying_vp_inputs
& VERT_BIT_COLOR0
) &&
115 (key
->light_color_material_mask
& bit
))
118 if (key
->varying_vp_inputs
& (bit
<< 16))
121 if (ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SHININESS
+ side
][0] != 0.0F
)
128 static void make_state_key( GLcontext
*ctx
, struct state_key
*key
)
130 const struct gl_fragment_program
*fp
;
133 memset(key
, 0, sizeof(struct state_key
));
134 fp
= ctx
->FragmentProgram
._Current
;
136 /* This now relies on texenvprogram.c being active:
140 key
->need_eye_coords
= ctx
->_NeedEyeCoords
;
142 key
->fragprog_inputs_read
= fp
->Base
.InputsRead
;
143 key
->varying_vp_inputs
= ctx
->varying_vp_inputs
;
145 if (ctx
->RenderMode
== GL_FEEDBACK
) {
146 /* make sure the vertprog emits color and tex0 */
147 key
->fragprog_inputs_read
|= (FRAG_BIT_COL0
| FRAG_BIT_TEX0
);
150 key
->separate_specular
= (ctx
->Light
.Model
.ColorControl
==
151 GL_SEPARATE_SPECULAR_COLOR
);
153 if (ctx
->Light
.Enabled
) {
154 key
->light_global_enabled
= 1;
156 if (ctx
->Light
.Model
.LocalViewer
)
157 key
->light_local_viewer
= 1;
159 if (ctx
->Light
.Model
.TwoSide
)
160 key
->light_twoside
= 1;
162 if (ctx
->Light
.ColorMaterialEnabled
) {
163 key
->light_color_material_mask
= ctx
->Light
.ColorMaterialBitmask
;
166 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
167 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
169 if (light
->Enabled
) {
170 key
->unit
[i
].light_enabled
= 1;
172 if (light
->EyePosition
[3] == 0.0)
173 key
->unit
[i
].light_eyepos3_is_zero
= 1;
175 if (light
->SpotCutoff
== 180.0)
176 key
->unit
[i
].light_spotcutoff_is_180
= 1;
178 if (light
->ConstantAttenuation
!= 1.0 ||
179 light
->LinearAttenuation
!= 0.0 ||
180 light
->QuadraticAttenuation
!= 0.0)
181 key
->unit
[i
].light_attenuated
= 1;
185 if (check_active_shininess(ctx
, key
, 0)) {
186 key
->material_shininess_is_zero
= 0;
188 else if (key
->light_twoside
&&
189 check_active_shininess(ctx
, key
, 1)) {
190 key
->material_shininess_is_zero
= 0;
193 key
->material_shininess_is_zero
= 1;
197 if (ctx
->Transform
.Normalize
)
200 if (ctx
->Transform
.RescaleNormals
)
201 key
->rescale_normals
= 1;
203 if (ctx
->Fog
.FogCoordinateSource
== GL_FRAGMENT_DEPTH_EXT
)
204 key
->fog_source_is_depth
= 1;
206 if (ctx
->Point
._Attenuated
)
207 key
->point_attenuated
= 1;
209 #if FEATURE_point_size_array
210 if (ctx
->Array
.ArrayObj
->PointSize
.Enabled
)
211 key
->point_array
= 1;
214 if (ctx
->Texture
._TexGenEnabled
||
215 ctx
->Texture
._TexMatEnabled
||
216 ctx
->Texture
._EnabledUnits
)
217 key
->texture_enabled_global
= 1;
219 for (i
= 0; i
< MAX_TEXTURE_COORD_UNITS
; i
++) {
220 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
222 if (texUnit
->_ReallyEnabled
)
223 key
->unit
[i
].texunit_really_enabled
= 1;
225 if (ctx
->Texture
._TexMatEnabled
& ENABLE_TEXMAT(i
))
226 key
->unit
[i
].texmat_enabled
= 1;
228 if (texUnit
->TexGenEnabled
) {
229 key
->unit
[i
].texgen_enabled
= 1;
231 key
->unit
[i
].texgen_mode0
=
232 translate_texgen( texUnit
->TexGenEnabled
& (1<<0),
233 texUnit
->GenS
.Mode
);
234 key
->unit
[i
].texgen_mode1
=
235 translate_texgen( texUnit
->TexGenEnabled
& (1<<1),
236 texUnit
->GenT
.Mode
);
237 key
->unit
[i
].texgen_mode2
=
238 translate_texgen( texUnit
->TexGenEnabled
& (1<<2),
239 texUnit
->GenR
.Mode
);
240 key
->unit
[i
].texgen_mode3
=
241 translate_texgen( texUnit
->TexGenEnabled
& (1<<3),
242 texUnit
->GenQ
.Mode
);
249 /* Very useful debugging tool - produces annotated listing of
250 * generated program with line/function references for each
251 * instruction back into this file:
256 /* Use uregs to represent registers internally, translate to Mesa's
257 * expected formats on emit.
259 * NOTE: These are passed by value extensively in this file rather
260 * than as usual by pointer reference. If this disturbs you, try
261 * remembering they are just 32bits in size.
263 * GCC is smart enough to deal with these dword-sized structures in
264 * much the same way as if I had defined them as dwords and was using
265 * macros to access and set the fields. This is much nicer and easier
270 GLint idx
:9; /* relative addressing may be negative */
271 /* sizeof(idx) should == sizeof(prog_src_reg::Index) */
279 const struct state_key
*state
;
280 struct gl_vertex_program
*program
;
281 GLint max_inst
; /** number of instructions allocated for program */
282 GLboolean mvp_with_dp4
;
285 GLuint temp_reserved
;
287 struct ureg eye_position
;
288 struct ureg eye_position_z
;
289 struct ureg eye_position_normalized
;
290 struct ureg transformed_normal
;
291 struct ureg identity
;
294 GLuint color_materials
;
298 static const struct ureg undef
= {
316 static struct ureg
make_ureg(GLuint file
, GLint idx
)
322 reg
.swz
= SWIZZLE_NOOP
;
329 static struct ureg
negate( struct ureg reg
)
336 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
338 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
341 GET_SWZ(reg
.swz
, w
));
346 static struct ureg
swizzle1( struct ureg reg
, int x
)
348 return swizzle(reg
, x
, x
, x
, x
);
352 static struct ureg
get_temp( struct tnl_program
*p
)
354 int bit
= _mesa_ffs( ~p
->temp_in_use
);
356 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
360 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
361 p
->program
->Base
.NumTemporaries
= bit
;
363 p
->temp_in_use
|= 1<<(bit
-1);
364 return make_ureg(PROGRAM_TEMPORARY
, bit
-1);
368 static struct ureg
reserve_temp( struct tnl_program
*p
)
370 struct ureg temp
= get_temp( p
);
371 p
->temp_reserved
|= 1<<temp
.idx
;
376 static void release_temp( struct tnl_program
*p
, struct ureg reg
)
378 if (reg
.file
== PROGRAM_TEMPORARY
) {
379 p
->temp_in_use
&= ~(1<<reg
.idx
);
380 p
->temp_in_use
|= p
->temp_reserved
; /* can't release reserved temps */
384 static void release_temps( struct tnl_program
*p
)
386 p
->temp_in_use
= p
->temp_reserved
;
390 static struct ureg
register_param5(struct tnl_program
*p
,
397 gl_state_index tokens
[STATE_LENGTH
];
404 idx
= _mesa_add_state_reference( p
->program
->Base
.Parameters
, tokens
);
405 return make_ureg(PROGRAM_STATE_VAR
, idx
);
409 #define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
410 #define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
411 #define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
412 #define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
417 * \param input one of VERT_ATTRIB_x tokens.
419 static struct ureg
register_input( struct tnl_program
*p
, GLuint input
)
423 if (p
->state
->varying_vp_inputs
& (1<<input
)) {
424 p
->program
->Base
.InputsRead
|= (1<<input
);
425 return make_ureg(PROGRAM_INPUT
, input
);
428 return register_param3( p
, STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, input
);
434 * \param input one of VERT_RESULT_x tokens.
436 static struct ureg
register_output( struct tnl_program
*p
, GLuint output
)
438 p
->program
->Base
.OutputsWritten
|= (1<<output
);
439 return make_ureg(PROGRAM_OUTPUT
, output
);
443 static struct ureg
register_const4f( struct tnl_program
*p
,
456 idx
= _mesa_add_unnamed_constant( p
->program
->Base
.Parameters
, values
, 4,
458 ASSERT(swizzle
== SWIZZLE_NOOP
);
459 return make_ureg(PROGRAM_CONSTANT
, idx
);
462 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
463 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
464 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
465 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
467 static GLboolean
is_undef( struct ureg reg
)
469 return reg
.file
== PROGRAM_UNDEFINED
;
473 static struct ureg
get_identity_param( struct tnl_program
*p
)
475 if (is_undef(p
->identity
))
476 p
->identity
= register_const4f(p
, 0,0,0,1);
481 static void register_matrix_param5( struct tnl_program
*p
,
482 GLint s0
, /* modelview, projection, etc */
483 GLint s1
, /* texture matrix number */
484 GLint s2
, /* first row */
485 GLint s3
, /* last row */
486 GLint s4
, /* inverse, transpose, etc */
487 struct ureg
*matrix
)
491 /* This is a bit sad as the support is there to pull the whole
492 * matrix out in one go:
494 for (i
= 0; i
<= s3
- s2
; i
++)
495 matrix
[i
] = register_param5( p
, s0
, s1
, i
, i
, s4
);
499 static void emit_arg( struct prog_src_register
*src
,
502 src
->File
= reg
.file
;
503 src
->Index
= reg
.idx
;
504 src
->Swizzle
= reg
.swz
;
505 src
->Negate
= reg
.negate
? NEGATE_XYZW
: NEGATE_NONE
;
508 /* Check that bitfield sizes aren't exceeded */
509 ASSERT(src
->Index
== reg
.idx
);
513 static void emit_dst( struct prog_dst_register
*dst
,
514 struct ureg reg
, GLuint mask
)
516 dst
->File
= reg
.file
;
517 dst
->Index
= reg
.idx
;
518 /* allow zero as a shorthand for xyzw */
519 dst
->WriteMask
= mask
? mask
: WRITEMASK_XYZW
;
520 dst
->CondMask
= COND_TR
; /* always pass cond test */
521 dst
->CondSwizzle
= SWIZZLE_NOOP
;
524 /* Check that bitfield sizes aren't exceeded */
525 ASSERT(dst
->Index
== reg
.idx
);
529 static void debug_insn( struct prog_instruction
*inst
, const char *fn
,
533 static const char *last_fn
;
537 _mesa_printf("%s:\n", fn
);
540 _mesa_printf("%d:\t", line
);
541 _mesa_print_instruction(inst
);
546 static void emit_op3fn(struct tnl_program
*p
,
557 struct prog_instruction
*inst
;
559 assert((GLint
) p
->program
->Base
.NumInstructions
<= p
->max_inst
);
561 if (p
->program
->Base
.NumInstructions
== p
->max_inst
) {
562 /* need to extend the program's instruction array */
563 struct prog_instruction
*newInst
;
565 /* double the size */
568 newInst
= _mesa_alloc_instructions(p
->max_inst
);
570 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "vertex program build");
574 _mesa_copy_instructions(newInst
,
575 p
->program
->Base
.Instructions
,
576 p
->program
->Base
.NumInstructions
);
578 _mesa_free_instructions(p
->program
->Base
.Instructions
,
579 p
->program
->Base
.NumInstructions
);
581 p
->program
->Base
.Instructions
= newInst
;
584 nr
= p
->program
->Base
.NumInstructions
++;
586 inst
= &p
->program
->Base
.Instructions
[nr
];
587 inst
->Opcode
= (enum prog_opcode
) op
;
590 emit_arg( &inst
->SrcReg
[0], src0
);
591 emit_arg( &inst
->SrcReg
[1], src1
);
592 emit_arg( &inst
->SrcReg
[2], src2
);
594 emit_dst( &inst
->DstReg
, dest
, mask
);
596 debug_insn(inst
, fn
, line
);
600 #define emit_op3(p, op, dst, mask, src0, src1, src2) \
601 emit_op3fn(p, op, dst, mask, src0, src1, src2, __FUNCTION__, __LINE__)
603 #define emit_op2(p, op, dst, mask, src0, src1) \
604 emit_op3fn(p, op, dst, mask, src0, src1, undef, __FUNCTION__, __LINE__)
606 #define emit_op1(p, op, dst, mask, src0) \
607 emit_op3fn(p, op, dst, mask, src0, undef, undef, __FUNCTION__, __LINE__)
610 static struct ureg
make_temp( struct tnl_program
*p
, struct ureg reg
)
612 if (reg
.file
== PROGRAM_TEMPORARY
&&
613 !(p
->temp_reserved
& (1<<reg
.idx
)))
616 struct ureg temp
= get_temp(p
);
617 emit_op1(p
, OPCODE_MOV
, temp
, 0, reg
);
623 /* Currently no tracking performed of input/output/register size or
624 * active elements. Could be used to reduce these operations, as
625 * could the matrix type.
627 static void emit_matrix_transform_vec4( struct tnl_program
*p
,
629 const struct ureg
*mat
,
632 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_X
, src
, mat
[0]);
633 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_Y
, src
, mat
[1]);
634 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_Z
, src
, mat
[2]);
635 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_W
, src
, mat
[3]);
639 /* This version is much easier to implement if writemasks are not
640 * supported natively on the target or (like SSE), the target doesn't
641 * have a clean/obvious dotproduct implementation.
643 static void emit_transpose_matrix_transform_vec4( struct tnl_program
*p
,
645 const struct ureg
*mat
,
650 if (dest
.file
!= PROGRAM_TEMPORARY
)
655 emit_op2(p
, OPCODE_MUL
, tmp
, 0, swizzle1(src
,X
), mat
[0]);
656 emit_op3(p
, OPCODE_MAD
, tmp
, 0, swizzle1(src
,Y
), mat
[1], tmp
);
657 emit_op3(p
, OPCODE_MAD
, tmp
, 0, swizzle1(src
,Z
), mat
[2], tmp
);
658 emit_op3(p
, OPCODE_MAD
, dest
, 0, swizzle1(src
,W
), mat
[3], tmp
);
660 if (dest
.file
!= PROGRAM_TEMPORARY
)
661 release_temp(p
, tmp
);
665 static void emit_matrix_transform_vec3( struct tnl_program
*p
,
667 const struct ureg
*mat
,
670 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_X
, src
, mat
[0]);
671 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_Y
, src
, mat
[1]);
672 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_Z
, src
, mat
[2]);
676 static void emit_normalize_vec3( struct tnl_program
*p
,
681 /* XXX use this when drivers are ready for NRM3 */
682 emit_op1(p
, OPCODE_NRM3
, dest
, WRITEMASK_XYZ
, src
);
684 struct ureg tmp
= get_temp(p
);
685 emit_op2(p
, OPCODE_DP3
, tmp
, WRITEMASK_X
, src
, src
);
686 emit_op1(p
, OPCODE_RSQ
, tmp
, WRITEMASK_X
, tmp
);
687 emit_op2(p
, OPCODE_MUL
, dest
, 0, src
, swizzle1(tmp
, X
));
688 release_temp(p
, tmp
);
693 static void emit_passthrough( struct tnl_program
*p
,
697 struct ureg out
= register_output(p
, output
);
698 emit_op1(p
, OPCODE_MOV
, out
, 0, register_input(p
, input
));
702 static struct ureg
get_eye_position( struct tnl_program
*p
)
704 if (is_undef(p
->eye_position
)) {
705 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
706 struct ureg modelview
[4];
708 p
->eye_position
= reserve_temp(p
);
710 if (p
->mvp_with_dp4
) {
711 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 3,
714 emit_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
717 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 3,
718 STATE_MATRIX_TRANSPOSE
, modelview
);
720 emit_transpose_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
724 return p
->eye_position
;
728 static struct ureg
get_eye_position_z( struct tnl_program
*p
)
730 if (!is_undef(p
->eye_position
))
731 return swizzle1(p
->eye_position
, Z
);
733 if (is_undef(p
->eye_position_z
)) {
734 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
735 struct ureg modelview
[4];
737 p
->eye_position_z
= reserve_temp(p
);
739 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 3,
742 emit_op2(p
, OPCODE_DP4
, p
->eye_position_z
, 0, pos
, modelview
[2]);
745 return p
->eye_position_z
;
749 static struct ureg
get_eye_position_normalized( struct tnl_program
*p
)
751 if (is_undef(p
->eye_position_normalized
)) {
752 struct ureg eye
= get_eye_position(p
);
753 p
->eye_position_normalized
= reserve_temp(p
);
754 emit_normalize_vec3(p
, p
->eye_position_normalized
, eye
);
757 return p
->eye_position_normalized
;
761 static struct ureg
get_transformed_normal( struct tnl_program
*p
)
763 if (is_undef(p
->transformed_normal
) &&
764 !p
->state
->need_eye_coords
&&
765 !p
->state
->normalize
&&
766 !(p
->state
->need_eye_coords
== p
->state
->rescale_normals
))
768 p
->transformed_normal
= register_input(p
, VERT_ATTRIB_NORMAL
);
770 else if (is_undef(p
->transformed_normal
))
772 struct ureg normal
= register_input(p
, VERT_ATTRIB_NORMAL
);
773 struct ureg mvinv
[3];
774 struct ureg transformed_normal
= reserve_temp(p
);
776 if (p
->state
->need_eye_coords
) {
777 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 2,
778 STATE_MATRIX_INVTRANS
, mvinv
);
780 /* Transform to eye space:
782 emit_matrix_transform_vec3( p
, transformed_normal
, mvinv
, normal
);
783 normal
= transformed_normal
;
786 /* Normalize/Rescale:
788 if (p
->state
->normalize
) {
789 emit_normalize_vec3( p
, transformed_normal
, normal
);
790 normal
= transformed_normal
;
792 else if (p
->state
->need_eye_coords
== p
->state
->rescale_normals
) {
793 /* This is already adjusted for eye/non-eye rendering:
795 struct ureg rescale
= register_param2(p
, STATE_INTERNAL
,
798 emit_op2( p
, OPCODE_MUL
, transformed_normal
, 0, normal
, rescale
);
799 normal
= transformed_normal
;
802 assert(normal
.file
== PROGRAM_TEMPORARY
);
803 p
->transformed_normal
= normal
;
806 return p
->transformed_normal
;
810 static void build_hpos( struct tnl_program
*p
)
812 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
813 struct ureg hpos
= register_output( p
, VERT_RESULT_HPOS
);
816 if (p
->mvp_with_dp4
) {
817 register_matrix_param5( p
, STATE_MVP_MATRIX
, 0, 0, 3,
819 emit_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
822 register_matrix_param5( p
, STATE_MVP_MATRIX
, 0, 0, 3,
823 STATE_MATRIX_TRANSPOSE
, mvp
);
824 emit_transpose_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
829 static GLuint
material_attrib( GLuint side
, GLuint property
)
831 return (property
- STATE_AMBIENT
) * 2 + side
;
836 * Get a bitmask of which material values vary on a per-vertex basis.
838 static void set_material_flags( struct tnl_program
*p
)
840 p
->color_materials
= 0;
843 if (p
->state
->varying_vp_inputs
& VERT_BIT_COLOR0
) {
845 p
->color_materials
= p
->state
->light_color_material_mask
;
848 p
->materials
|= (p
->state
->varying_vp_inputs
>> 16);
852 static struct ureg
get_material( struct tnl_program
*p
, GLuint side
,
855 GLuint attrib
= material_attrib(side
, property
);
857 if (p
->color_materials
& (1<<attrib
))
858 return register_input(p
, VERT_ATTRIB_COLOR0
);
859 else if (p
->materials
& (1<<attrib
)) {
860 /* Put material values in the GENERIC slots -- they are not used
861 * for anything in fixed function mode.
863 return register_input( p
, attrib
+ VERT_ATTRIB_GENERIC0
);
866 return register_param3( p
, STATE_MATERIAL
, side
, property
);
869 #define SCENE_COLOR_BITS(side) (( MAT_BIT_FRONT_EMISSION | \
870 MAT_BIT_FRONT_AMBIENT | \
871 MAT_BIT_FRONT_DIFFUSE) << (side))
875 * Either return a precalculated constant value or emit code to
876 * calculate these values dynamically in the case where material calls
877 * are present between begin/end pairs.
879 * Probably want to shift this to the program compilation phase - if
880 * we always emitted the calculation here, a smart compiler could
881 * detect that it was constant (given a certain set of inputs), and
882 * lift it out of the main loop. That way the programs created here
883 * would be independent of the vertex_buffer details.
885 static struct ureg
get_scenecolor( struct tnl_program
*p
, GLuint side
)
887 if (p
->materials
& SCENE_COLOR_BITS(side
)) {
888 struct ureg lm_ambient
= register_param1(p
, STATE_LIGHTMODEL_AMBIENT
);
889 struct ureg material_emission
= get_material(p
, side
, STATE_EMISSION
);
890 struct ureg material_ambient
= get_material(p
, side
, STATE_AMBIENT
);
891 struct ureg material_diffuse
= get_material(p
, side
, STATE_DIFFUSE
);
892 struct ureg tmp
= make_temp(p
, material_diffuse
);
893 emit_op3(p
, OPCODE_MAD
, tmp
, WRITEMASK_XYZ
, lm_ambient
,
894 material_ambient
, material_emission
);
898 return register_param2( p
, STATE_LIGHTMODEL_SCENECOLOR
, side
);
902 static struct ureg
get_lightprod( struct tnl_program
*p
, GLuint light
,
903 GLuint side
, GLuint property
)
905 GLuint attrib
= material_attrib(side
, property
);
906 if (p
->materials
& (1<<attrib
)) {
907 struct ureg light_value
=
908 register_param3(p
, STATE_LIGHT
, light
, property
);
909 struct ureg material_value
= get_material(p
, side
, property
);
910 struct ureg tmp
= get_temp(p
);
911 emit_op2(p
, OPCODE_MUL
, tmp
, 0, light_value
, material_value
);
915 return register_param4(p
, STATE_LIGHTPROD
, light
, side
, property
);
919 static struct ureg
calculate_light_attenuation( struct tnl_program
*p
,
924 struct ureg attenuation
= register_param3(p
, STATE_LIGHT
, i
,
926 struct ureg att
= get_temp(p
);
928 /* Calculate spot attenuation:
930 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
931 struct ureg spot_dir_norm
= register_param3(p
, STATE_INTERNAL
,
932 STATE_LIGHT_SPOT_DIR_NORMALIZED
, i
);
933 struct ureg spot
= get_temp(p
);
934 struct ureg slt
= get_temp(p
);
936 emit_op2(p
, OPCODE_DP3
, spot
, 0, negate(VPpli
), spot_dir_norm
);
937 emit_op2(p
, OPCODE_SLT
, slt
, 0, swizzle1(spot_dir_norm
,W
), spot
);
938 emit_op2(p
, OPCODE_POW
, spot
, 0, spot
, swizzle1(attenuation
, W
));
939 emit_op2(p
, OPCODE_MUL
, att
, 0, slt
, spot
);
941 release_temp(p
, spot
);
942 release_temp(p
, slt
);
945 /* Calculate distance attenuation:
947 if (p
->state
->unit
[i
].light_attenuated
) {
949 emit_op1(p
, OPCODE_RCP
, dist
, WRITEMASK_YZ
, dist
);
951 emit_op2(p
, OPCODE_MUL
, dist
, WRITEMASK_XZ
, dist
, swizzle1(dist
,Y
));
953 emit_op2(p
, OPCODE_DP3
, dist
, 0, attenuation
, dist
);
955 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
957 emit_op1(p
, OPCODE_RCP
, dist
, 0, dist
);
958 /* spot-atten * dist-atten */
959 emit_op2(p
, OPCODE_MUL
, att
, 0, dist
, att
);
963 emit_op1(p
, OPCODE_RCP
, att
, 0, dist
);
973 * lit.y = MAX(0, dots.x)
974 * lit.z = SLT(0, dots.x)
976 static void emit_degenerate_lit( struct tnl_program
*p
,
980 struct ureg id
= get_identity_param(p
); /* id = {0,0,0,1} */
982 /* Note that lit.x & lit.w will not be examined. Note also that
983 * dots.xyzw == dots.xxxx.
986 /* MAX lit, id, dots;
988 emit_op2(p
, OPCODE_MAX
, lit
, WRITEMASK_XYZW
, id
, dots
);
990 /* result[2] = (in > 0 ? 1 : 0)
991 * SLT lit.z, id.z, dots; # lit.z = (0 < dots.z) ? 1 : 0
993 emit_op2(p
, OPCODE_SLT
, lit
, WRITEMASK_Z
, swizzle1(id
,Z
), dots
);
997 /* Need to add some addtional parameters to allow lighting in object
998 * space - STATE_SPOT_DIRECTION and STATE_HALF_VECTOR implicitly assume eye
1001 static void build_lighting( struct tnl_program
*p
)
1003 const GLboolean twoside
= p
->state
->light_twoside
;
1004 const GLboolean separate
= p
->state
->separate_specular
;
1005 GLuint nr_lights
= 0, count
= 0;
1006 struct ureg normal
= get_transformed_normal(p
);
1007 struct ureg lit
= get_temp(p
);
1008 struct ureg dots
= get_temp(p
);
1009 struct ureg _col0
= undef
, _col1
= undef
;
1010 struct ureg _bfc0
= undef
, _bfc1
= undef
;
1015 * dots.x = dot(normal, VPpli)
1016 * dots.y = dot(normal, halfAngle)
1017 * dots.z = back.shininess
1018 * dots.w = front.shininess
1021 for (i
= 0; i
< MAX_LIGHTS
; i
++)
1022 if (p
->state
->unit
[i
].light_enabled
)
1025 set_material_flags(p
);
1028 if (!p
->state
->material_shininess_is_zero
) {
1029 struct ureg shininess
= get_material(p
, 0, STATE_SHININESS
);
1030 emit_op1(p
, OPCODE_MOV
, dots
, WRITEMASK_W
, swizzle1(shininess
,X
));
1031 release_temp(p
, shininess
);
1034 _col0
= make_temp(p
, get_scenecolor(p
, 0));
1036 _col1
= make_temp(p
, get_identity_param(p
));
1042 if (!p
->state
->material_shininess_is_zero
) {
1043 /* Note that we negate the back-face specular exponent here.
1044 * The negation will be un-done later in the back-face code below.
1046 struct ureg shininess
= get_material(p
, 1, STATE_SHININESS
);
1047 emit_op1(p
, OPCODE_MOV
, dots
, WRITEMASK_Z
,
1048 negate(swizzle1(shininess
,X
)));
1049 release_temp(p
, shininess
);
1052 _bfc0
= make_temp(p
, get_scenecolor(p
, 1));
1054 _bfc1
= make_temp(p
, get_identity_param(p
));
1059 /* If no lights, still need to emit the scenecolor.
1062 struct ureg res0
= register_output( p
, VERT_RESULT_COL0
);
1063 emit_op1(p
, OPCODE_MOV
, res0
, 0, _col0
);
1067 struct ureg res1
= register_output( p
, VERT_RESULT_COL1
);
1068 emit_op1(p
, OPCODE_MOV
, res1
, 0, _col1
);
1072 struct ureg res0
= register_output( p
, VERT_RESULT_BFC0
);
1073 emit_op1(p
, OPCODE_MOV
, res0
, 0, _bfc0
);
1076 if (twoside
&& separate
) {
1077 struct ureg res1
= register_output( p
, VERT_RESULT_BFC1
);
1078 emit_op1(p
, OPCODE_MOV
, res1
, 0, _bfc1
);
1081 if (nr_lights
== 0) {
1086 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
1087 if (p
->state
->unit
[i
].light_enabled
) {
1088 struct ureg half
= undef
;
1089 struct ureg att
= undef
, VPpli
= undef
;
1093 if (p
->state
->unit
[i
].light_eyepos3_is_zero
) {
1094 /* Can used precomputed constants in this case.
1095 * Attenuation never applies to infinite lights.
1097 VPpli
= register_param3(p
, STATE_INTERNAL
,
1098 STATE_LIGHT_POSITION_NORMALIZED
, i
);
1100 if (!p
->state
->material_shininess_is_zero
) {
1101 if (p
->state
->light_local_viewer
) {
1102 struct ureg eye_hat
= get_eye_position_normalized(p
);
1104 emit_op2(p
, OPCODE_SUB
, half
, 0, VPpli
, eye_hat
);
1105 emit_normalize_vec3(p
, half
, half
);
1108 half
= register_param3(p
, STATE_INTERNAL
,
1109 STATE_LIGHT_HALF_VECTOR
, i
);
1114 struct ureg Ppli
= register_param3(p
, STATE_INTERNAL
,
1115 STATE_LIGHT_POSITION
, i
);
1116 struct ureg V
= get_eye_position(p
);
1117 struct ureg dist
= get_temp(p
);
1119 VPpli
= get_temp(p
);
1121 /* Calculate VPpli vector
1123 emit_op2(p
, OPCODE_SUB
, VPpli
, 0, Ppli
, V
);
1125 /* Normalize VPpli. The dist value also used in
1126 * attenuation below.
1128 emit_op2(p
, OPCODE_DP3
, dist
, 0, VPpli
, VPpli
);
1129 emit_op1(p
, OPCODE_RSQ
, dist
, 0, dist
);
1130 emit_op2(p
, OPCODE_MUL
, VPpli
, 0, VPpli
, dist
);
1132 /* Calculate attenuation:
1134 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
||
1135 p
->state
->unit
[i
].light_attenuated
) {
1136 att
= calculate_light_attenuation(p
, i
, VPpli
, dist
);
1139 /* Calculate viewer direction, or use infinite viewer:
1141 if (!p
->state
->material_shininess_is_zero
) {
1144 if (p
->state
->light_local_viewer
) {
1145 struct ureg eye_hat
= get_eye_position_normalized(p
);
1146 emit_op2(p
, OPCODE_SUB
, half
, 0, VPpli
, eye_hat
);
1149 struct ureg z_dir
= swizzle(get_identity_param(p
),X
,Y
,W
,Z
);
1150 emit_op2(p
, OPCODE_ADD
, half
, 0, VPpli
, z_dir
);
1153 emit_normalize_vec3(p
, half
, half
);
1156 release_temp(p
, dist
);
1159 /* Calculate dot products:
1161 if (p
->state
->material_shininess_is_zero
) {
1162 emit_op2(p
, OPCODE_DP3
, dots
, 0, normal
, VPpli
);
1165 emit_op2(p
, OPCODE_DP3
, dots
, WRITEMASK_X
, normal
, VPpli
);
1166 emit_op2(p
, OPCODE_DP3
, dots
, WRITEMASK_Y
, normal
, half
);
1169 /* Front face lighting:
1172 struct ureg ambient
= get_lightprod(p
, i
, 0, STATE_AMBIENT
);
1173 struct ureg diffuse
= get_lightprod(p
, i
, 0, STATE_DIFFUSE
);
1174 struct ureg specular
= get_lightprod(p
, i
, 0, STATE_SPECULAR
);
1175 struct ureg res0
, res1
;
1176 GLuint mask0
, mask1
;
1178 if (count
== nr_lights
) {
1180 mask0
= WRITEMASK_XYZ
;
1181 mask1
= WRITEMASK_XYZ
;
1182 res0
= register_output( p
, VERT_RESULT_COL0
);
1183 res1
= register_output( p
, VERT_RESULT_COL1
);
1187 mask1
= WRITEMASK_XYZ
;
1189 res1
= register_output( p
, VERT_RESULT_COL0
);
1199 if (!is_undef(att
)) {
1200 /* light is attenuated by distance */
1201 emit_op1(p
, OPCODE_LIT
, lit
, 0, dots
);
1202 emit_op2(p
, OPCODE_MUL
, lit
, 0, lit
, att
);
1203 emit_op3(p
, OPCODE_MAD
, _col0
, 0, swizzle1(lit
,X
), ambient
, _col0
);
1205 else if (!p
->state
->material_shininess_is_zero
) {
1206 /* there's a non-zero specular term */
1207 emit_op1(p
, OPCODE_LIT
, lit
, 0, dots
);
1208 emit_op2(p
, OPCODE_ADD
, _col0
, 0, ambient
, _col0
);
1211 /* no attenutation, no specular */
1212 emit_degenerate_lit(p
, lit
, dots
);
1213 emit_op2(p
, OPCODE_ADD
, _col0
, 0, ambient
, _col0
);
1216 emit_op3(p
, OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _col0
);
1217 emit_op3(p
, OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _col1
);
1219 release_temp(p
, ambient
);
1220 release_temp(p
, diffuse
);
1221 release_temp(p
, specular
);
1224 /* Back face lighting:
1227 struct ureg ambient
= get_lightprod(p
, i
, 1, STATE_AMBIENT
);
1228 struct ureg diffuse
= get_lightprod(p
, i
, 1, STATE_DIFFUSE
);
1229 struct ureg specular
= get_lightprod(p
, i
, 1, STATE_SPECULAR
);
1230 struct ureg res0
, res1
;
1231 GLuint mask0
, mask1
;
1233 if (count
== nr_lights
) {
1235 mask0
= WRITEMASK_XYZ
;
1236 mask1
= WRITEMASK_XYZ
;
1237 res0
= register_output( p
, VERT_RESULT_BFC0
);
1238 res1
= register_output( p
, VERT_RESULT_BFC1
);
1242 mask1
= WRITEMASK_XYZ
;
1244 res1
= register_output( p
, VERT_RESULT_BFC0
);
1254 /* For the back face we need to negate the X and Y component
1255 * dot products. dots.Z has the negated back-face specular
1256 * exponent. We swizzle that into the W position. This
1257 * negation makes the back-face specular term positive again.
1259 dots
= negate(swizzle(dots
,X
,Y
,W
,Z
));
1261 if (!is_undef(att
)) {
1262 emit_op1(p
, OPCODE_LIT
, lit
, 0, dots
);
1263 emit_op2(p
, OPCODE_MUL
, lit
, 0, lit
, att
);
1264 emit_op3(p
, OPCODE_MAD
, _bfc0
, 0, swizzle1(lit
,X
), ambient
, _bfc0
);
1266 else if (!p
->state
->material_shininess_is_zero
) {
1267 emit_op1(p
, OPCODE_LIT
, lit
, 0, dots
);
1268 emit_op2(p
, OPCODE_ADD
, _bfc0
, 0, ambient
, _bfc0
); /**/
1271 emit_degenerate_lit(p
, lit
, dots
);
1272 emit_op2(p
, OPCODE_ADD
, _bfc0
, 0, ambient
, _bfc0
);
1275 emit_op3(p
, OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _bfc0
);
1276 emit_op3(p
, OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _bfc1
);
1277 /* restore dots to its original state for subsequent lights
1278 * by negating and swizzling again.
1280 dots
= negate(swizzle(dots
,X
,Y
,W
,Z
));
1282 release_temp(p
, ambient
);
1283 release_temp(p
, diffuse
);
1284 release_temp(p
, specular
);
1287 release_temp(p
, half
);
1288 release_temp(p
, VPpli
);
1289 release_temp(p
, att
);
1297 static void build_fog( struct tnl_program
*p
)
1299 struct ureg fog
= register_output(p
, VERT_RESULT_FOGC
);
1302 if (p
->state
->fog_source_is_depth
) {
1303 input
= get_eye_position_z(p
);
1306 input
= swizzle1(register_input(p
, VERT_ATTRIB_FOG
), X
);
1309 emit_op1(p
, OPCODE_ABS
, fog
, WRITEMASK_X
, input
);
1313 static void build_reflect_texgen( struct tnl_program
*p
,
1317 struct ureg normal
= get_transformed_normal(p
);
1318 struct ureg eye_hat
= get_eye_position_normalized(p
);
1319 struct ureg tmp
= get_temp(p
);
1322 emit_op2(p
, OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1324 emit_op2(p
, OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1326 emit_op3(p
, OPCODE_MAD
, dest
, writemask
, negate(tmp
), normal
, eye_hat
);
1328 release_temp(p
, tmp
);
1332 static void build_sphere_texgen( struct tnl_program
*p
,
1336 struct ureg normal
= get_transformed_normal(p
);
1337 struct ureg eye_hat
= get_eye_position_normalized(p
);
1338 struct ureg tmp
= get_temp(p
);
1339 struct ureg half
= register_scalar_const(p
, .5);
1340 struct ureg r
= get_temp(p
);
1341 struct ureg inv_m
= get_temp(p
);
1342 struct ureg id
= get_identity_param(p
);
1344 /* Could share the above calculations, but it would be
1345 * a fairly odd state for someone to set (both sphere and
1346 * reflection active for different texture coordinate
1347 * components. Of course - if two texture units enable
1348 * reflect and/or sphere, things start to tilt in favour
1349 * of seperating this out:
1353 emit_op2(p
, OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1355 emit_op2(p
, OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1357 emit_op3(p
, OPCODE_MAD
, r
, 0, negate(tmp
), normal
, eye_hat
);
1359 emit_op2(p
, OPCODE_ADD
, tmp
, 0, r
, swizzle(id
,X
,Y
,W
,Z
));
1360 /* rx^2 + ry^2 + (rz+1)^2 */
1361 emit_op2(p
, OPCODE_DP3
, tmp
, 0, tmp
, tmp
);
1363 emit_op1(p
, OPCODE_RSQ
, tmp
, 0, tmp
);
1365 emit_op2(p
, OPCODE_MUL
, inv_m
, 0, tmp
, half
);
1367 emit_op3(p
, OPCODE_MAD
, dest
, writemask
, r
, inv_m
, half
);
1369 release_temp(p
, tmp
);
1371 release_temp(p
, inv_m
);
1375 static void build_texture_transform( struct tnl_program
*p
)
1379 for (i
= 0; i
< MAX_TEXTURE_COORD_UNITS
; i
++) {
1381 if (!(p
->state
->fragprog_inputs_read
& FRAG_BIT_TEX(i
)))
1384 if (p
->state
->unit
[i
].texgen_enabled
||
1385 p
->state
->unit
[i
].texmat_enabled
) {
1387 GLuint texmat_enabled
= p
->state
->unit
[i
].texmat_enabled
;
1388 struct ureg out
= register_output(p
, VERT_RESULT_TEX0
+ i
);
1389 struct ureg out_texgen
= undef
;
1391 if (p
->state
->unit
[i
].texgen_enabled
) {
1392 GLuint copy_mask
= 0;
1393 GLuint sphere_mask
= 0;
1394 GLuint reflect_mask
= 0;
1395 GLuint normal_mask
= 0;
1399 out_texgen
= get_temp(p
);
1403 modes
[0] = p
->state
->unit
[i
].texgen_mode0
;
1404 modes
[1] = p
->state
->unit
[i
].texgen_mode1
;
1405 modes
[2] = p
->state
->unit
[i
].texgen_mode2
;
1406 modes
[3] = p
->state
->unit
[i
].texgen_mode3
;
1408 for (j
= 0; j
< 4; j
++) {
1410 case TXG_OBJ_LINEAR
: {
1411 struct ureg obj
= register_input(p
, VERT_ATTRIB_POS
);
1413 register_param3(p
, STATE_TEXGEN
, i
,
1414 STATE_TEXGEN_OBJECT_S
+ j
);
1416 emit_op2(p
, OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1420 case TXG_EYE_LINEAR
: {
1421 struct ureg eye
= get_eye_position(p
);
1423 register_param3(p
, STATE_TEXGEN
, i
,
1424 STATE_TEXGEN_EYE_S
+ j
);
1426 emit_op2(p
, OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1430 case TXG_SPHERE_MAP
:
1431 sphere_mask
|= WRITEMASK_X
<< j
;
1433 case TXG_REFLECTION_MAP
:
1434 reflect_mask
|= WRITEMASK_X
<< j
;
1436 case TXG_NORMAL_MAP
:
1437 normal_mask
|= WRITEMASK_X
<< j
;
1440 copy_mask
|= WRITEMASK_X
<< j
;
1445 build_sphere_texgen(p
, out_texgen
, sphere_mask
);
1449 build_reflect_texgen(p
, out_texgen
, reflect_mask
);
1453 struct ureg normal
= get_transformed_normal(p
);
1454 emit_op1(p
, OPCODE_MOV
, out_texgen
, normal_mask
, normal
);
1458 struct ureg in
= register_input(p
, VERT_ATTRIB_TEX0
+i
);
1459 emit_op1(p
, OPCODE_MOV
, out_texgen
, copy_mask
, in
);
1463 if (texmat_enabled
) {
1464 struct ureg texmat
[4];
1465 struct ureg in
= (!is_undef(out_texgen
) ?
1467 register_input(p
, VERT_ATTRIB_TEX0
+i
));
1468 if (p
->mvp_with_dp4
) {
1469 register_matrix_param5( p
, STATE_TEXTURE_MATRIX
, i
, 0, 3,
1471 emit_matrix_transform_vec4( p
, out
, texmat
, in
);
1474 register_matrix_param5( p
, STATE_TEXTURE_MATRIX
, i
, 0, 3,
1475 STATE_MATRIX_TRANSPOSE
, texmat
);
1476 emit_transpose_matrix_transform_vec4( p
, out
, texmat
, in
);
1483 emit_passthrough(p
, VERT_ATTRIB_TEX0
+i
, VERT_RESULT_TEX0
+i
);
1490 * Point size attenuation computation.
1492 static void build_atten_pointsize( struct tnl_program
*p
)
1494 struct ureg eye
= get_eye_position_z(p
);
1495 struct ureg state_size
= register_param1(p
, STATE_POINT_SIZE
);
1496 struct ureg state_attenuation
= register_param1(p
, STATE_POINT_ATTENUATION
);
1497 struct ureg out
= register_output(p
, VERT_RESULT_PSIZ
);
1498 struct ureg ut
= get_temp(p
);
1501 emit_op1(p
, OPCODE_ABS
, ut
, WRITEMASK_Y
, swizzle1(eye
, Z
));
1502 /* p1 + dist * (p2 + dist * p3); */
1503 emit_op3(p
, OPCODE_MAD
, ut
, WRITEMASK_X
, swizzle1(ut
, Y
),
1504 swizzle1(state_attenuation
, Z
), swizzle1(state_attenuation
, Y
));
1505 emit_op3(p
, OPCODE_MAD
, ut
, WRITEMASK_X
, swizzle1(ut
, Y
),
1506 ut
, swizzle1(state_attenuation
, X
));
1508 /* 1 / sqrt(factor) */
1509 emit_op1(p
, OPCODE_RSQ
, ut
, WRITEMASK_X
, ut
);
1512 /* out = pointSize / sqrt(factor) */
1513 emit_op2(p
, OPCODE_MUL
, out
, WRITEMASK_X
, ut
, state_size
);
1515 /* this is a good place to clamp the point size since there's likely
1516 * no hardware registers to clamp point size at rasterization time.
1518 emit_op2(p
, OPCODE_MUL
, ut
, WRITEMASK_X
, ut
, state_size
);
1519 emit_op2(p
, OPCODE_MAX
, ut
, WRITEMASK_X
, ut
, swizzle1(state_size
, Y
));
1520 emit_op2(p
, OPCODE_MIN
, out
, WRITEMASK_X
, ut
, swizzle1(state_size
, Z
));
1523 release_temp(p
, ut
);
1528 * Pass-though per-vertex point size, from user's point size array.
1530 static void build_array_pointsize( struct tnl_program
*p
)
1532 struct ureg in
= register_input(p
, VERT_ATTRIB_POINT_SIZE
);
1533 struct ureg out
= register_output(p
, VERT_RESULT_PSIZ
);
1534 emit_op1(p
, OPCODE_MOV
, out
, WRITEMASK_X
, in
);
1538 static void build_tnl_program( struct tnl_program
*p
)
1540 /* Emit the program, starting with modelviewproject:
1544 /* Lighting calculations:
1546 if (p
->state
->fragprog_inputs_read
& (FRAG_BIT_COL0
|FRAG_BIT_COL1
)) {
1547 if (p
->state
->light_global_enabled
)
1550 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
1551 emit_passthrough(p
, VERT_ATTRIB_COLOR0
, VERT_RESULT_COL0
);
1553 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL1
)
1554 emit_passthrough(p
, VERT_ATTRIB_COLOR1
, VERT_RESULT_COL1
);
1558 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_FOGC
)
1561 if (p
->state
->fragprog_inputs_read
& FRAG_BITS_TEX_ANY
)
1562 build_texture_transform(p
);
1564 if (p
->state
->point_attenuated
)
1565 build_atten_pointsize(p
);
1566 else if (p
->state
->point_array
)
1567 build_array_pointsize(p
);
1571 emit_op1(p
, OPCODE_END
, undef
, 0, undef
);
1576 _mesa_printf ("\n");
1582 create_new_program( const struct state_key
*key
,
1583 struct gl_vertex_program
*program
,
1584 GLboolean mvp_with_dp4
,
1587 struct tnl_program p
;
1589 _mesa_memset(&p
, 0, sizeof(p
));
1591 p
.program
= program
;
1592 p
.eye_position
= undef
;
1593 p
.eye_position_z
= undef
;
1594 p
.eye_position_normalized
= undef
;
1595 p
.transformed_normal
= undef
;
1598 p
.mvp_with_dp4
= mvp_with_dp4
;
1600 if (max_temps
>= sizeof(int) * 8)
1601 p
.temp_reserved
= 0;
1603 p
.temp_reserved
= ~((1<<max_temps
)-1);
1605 /* Start by allocating 32 instructions.
1606 * If we need more, we'll grow the instruction array as needed.
1609 p
.program
->Base
.Instructions
= _mesa_alloc_instructions(p
.max_inst
);
1610 p
.program
->Base
.String
= NULL
;
1611 p
.program
->Base
.NumInstructions
=
1612 p
.program
->Base
.NumTemporaries
=
1613 p
.program
->Base
.NumParameters
=
1614 p
.program
->Base
.NumAttributes
= p
.program
->Base
.NumAddressRegs
= 0;
1615 p
.program
->Base
.Parameters
= _mesa_new_parameter_list();
1616 p
.program
->Base
.InputsRead
= 0;
1617 p
.program
->Base
.OutputsWritten
= 0;
1619 build_tnl_program( &p
);
1624 * Return a vertex program which implements the current fixed-function
1625 * transform/lighting/texgen operations.
1626 * XXX move this into core mesa (main/)
1628 struct gl_vertex_program
*
1629 _mesa_get_fixed_func_vertex_program(GLcontext
*ctx
)
1631 struct gl_vertex_program
*prog
;
1632 struct state_key key
;
1634 /* Grab all the relevent state and put it in a single structure:
1636 make_state_key(ctx
, &key
);
1638 /* Look for an already-prepared program for this state:
1640 prog
= (struct gl_vertex_program
*)
1641 _mesa_search_program_cache(ctx
->VertexProgram
.Cache
, &key
, sizeof(key
));
1644 /* OK, we'll have to build a new one */
1646 _mesa_printf("Build new TNL program\n");
1648 prog
= (struct gl_vertex_program
*)
1649 ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
1653 create_new_program( &key
, prog
,
1655 ctx
->Const
.VertexProgram
.MaxTemps
);
1658 if (ctx
->Driver
.ProgramStringNotify
)
1659 ctx
->Driver
.ProgramStringNotify( ctx
, GL_VERTEX_PROGRAM_ARB
,
1662 _mesa_program_cache_insert(ctx
, ctx
->VertexProgram
.Cache
,
1663 &key
, sizeof(key
), &prog
->Base
);