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"
49 /** Max of number of lights and texture coord units */
50 #define NUM_UNITS MAX2(MAX_TEXTURE_COORD_UNITS, MAX_LIGHTS)
53 unsigned light_color_material_mask
:12;
54 unsigned light_global_enabled
:1;
55 unsigned light_local_viewer
:1;
56 unsigned light_twoside
:1;
57 unsigned material_shininess_is_zero
:1;
58 unsigned need_eye_coords
:1;
60 unsigned rescale_normals
:1;
62 unsigned fog_source_is_depth
:1;
63 unsigned separate_specular
:1;
64 unsigned point_attenuated
:1;
65 unsigned point_array
:1;
66 unsigned texture_enabled_global
:1;
67 unsigned fragprog_inputs_read
:12;
69 unsigned varying_vp_inputs
;
72 unsigned light_enabled
:1;
73 unsigned light_eyepos3_is_zero
:1;
74 unsigned light_spotcutoff_is_180
:1;
75 unsigned light_attenuated
:1;
76 unsigned texunit_really_enabled
:1;
77 unsigned texmat_enabled
:1;
78 unsigned texgen_enabled
:4;
79 unsigned texgen_mode0
:4;
80 unsigned texgen_mode1
:4;
81 unsigned texgen_mode2
:4;
82 unsigned texgen_mode3
:4;
88 #define TXG_OBJ_LINEAR 1
89 #define TXG_EYE_LINEAR 2
90 #define TXG_SPHERE_MAP 3
91 #define TXG_REFLECTION_MAP 4
92 #define TXG_NORMAL_MAP 5
94 static GLuint
translate_texgen( GLboolean enabled
, GLenum mode
)
100 case GL_OBJECT_LINEAR
: return TXG_OBJ_LINEAR
;
101 case GL_EYE_LINEAR
: return TXG_EYE_LINEAR
;
102 case GL_SPHERE_MAP
: return TXG_SPHERE_MAP
;
103 case GL_REFLECTION_MAP_NV
: return TXG_REFLECTION_MAP
;
104 case GL_NORMAL_MAP_NV
: return TXG_NORMAL_MAP
;
105 default: return TXG_NONE
;
111 static GLboolean
check_active_shininess( GLcontext
*ctx
,
112 const struct state_key
*key
,
115 GLuint bit
= 1 << (MAT_ATTRIB_FRONT_SHININESS
+ side
);
117 if ((key
->varying_vp_inputs
& VERT_BIT_COLOR0
) &&
118 (key
->light_color_material_mask
& bit
))
121 if (key
->varying_vp_inputs
& (bit
<< 16))
124 if (ctx
->Light
.Material
.Attrib
[MAT_ATTRIB_FRONT_SHININESS
+ side
][0] != 0.0F
)
131 static void make_state_key( GLcontext
*ctx
, struct state_key
*key
)
133 const struct gl_fragment_program
*fp
;
136 memset(key
, 0, sizeof(struct state_key
));
137 fp
= ctx
->FragmentProgram
._Current
;
139 /* This now relies on texenvprogram.c being active:
143 key
->need_eye_coords
= ctx
->_NeedEyeCoords
;
145 key
->fragprog_inputs_read
= fp
->Base
.InputsRead
;
146 key
->varying_vp_inputs
= ctx
->varying_vp_inputs
;
148 if (ctx
->RenderMode
== GL_FEEDBACK
) {
149 /* make sure the vertprog emits color and tex0 */
150 key
->fragprog_inputs_read
|= (FRAG_BIT_COL0
| FRAG_BIT_TEX0
);
153 key
->separate_specular
= (ctx
->Light
.Model
.ColorControl
==
154 GL_SEPARATE_SPECULAR_COLOR
);
156 if (ctx
->Light
.Enabled
) {
157 key
->light_global_enabled
= 1;
159 if (ctx
->Light
.Model
.LocalViewer
)
160 key
->light_local_viewer
= 1;
162 if (ctx
->Light
.Model
.TwoSide
)
163 key
->light_twoside
= 1;
165 if (ctx
->Light
.ColorMaterialEnabled
) {
166 key
->light_color_material_mask
= ctx
->Light
.ColorMaterialBitmask
;
169 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
170 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
172 if (light
->Enabled
) {
173 key
->unit
[i
].light_enabled
= 1;
175 if (light
->EyePosition
[3] == 0.0)
176 key
->unit
[i
].light_eyepos3_is_zero
= 1;
178 if (light
->SpotCutoff
== 180.0)
179 key
->unit
[i
].light_spotcutoff_is_180
= 1;
181 if (light
->ConstantAttenuation
!= 1.0 ||
182 light
->LinearAttenuation
!= 0.0 ||
183 light
->QuadraticAttenuation
!= 0.0)
184 key
->unit
[i
].light_attenuated
= 1;
188 if (check_active_shininess(ctx
, key
, 0)) {
189 key
->material_shininess_is_zero
= 0;
191 else if (key
->light_twoside
&&
192 check_active_shininess(ctx
, key
, 1)) {
193 key
->material_shininess_is_zero
= 0;
196 key
->material_shininess_is_zero
= 1;
200 if (ctx
->Transform
.Normalize
)
203 if (ctx
->Transform
.RescaleNormals
)
204 key
->rescale_normals
= 1;
206 if (ctx
->Fog
.FogCoordinateSource
== GL_FRAGMENT_DEPTH_EXT
)
207 key
->fog_source_is_depth
= 1;
209 if (ctx
->Point
._Attenuated
)
210 key
->point_attenuated
= 1;
212 #if FEATURE_point_size_array
213 if (ctx
->Array
.ArrayObj
->PointSize
.Enabled
)
214 key
->point_array
= 1;
217 if (ctx
->Texture
._TexGenEnabled
||
218 ctx
->Texture
._TexMatEnabled
||
219 ctx
->Texture
._EnabledUnits
)
220 key
->texture_enabled_global
= 1;
222 for (i
= 0; i
< MAX_TEXTURE_COORD_UNITS
; i
++) {
223 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
225 if (texUnit
->_ReallyEnabled
)
226 key
->unit
[i
].texunit_really_enabled
= 1;
228 if (ctx
->Texture
._TexMatEnabled
& ENABLE_TEXMAT(i
))
229 key
->unit
[i
].texmat_enabled
= 1;
231 if (texUnit
->TexGenEnabled
) {
232 key
->unit
[i
].texgen_enabled
= 1;
234 key
->unit
[i
].texgen_mode0
=
235 translate_texgen( texUnit
->TexGenEnabled
& (1<<0),
236 texUnit
->GenS
.Mode
);
237 key
->unit
[i
].texgen_mode1
=
238 translate_texgen( texUnit
->TexGenEnabled
& (1<<1),
239 texUnit
->GenT
.Mode
);
240 key
->unit
[i
].texgen_mode2
=
241 translate_texgen( texUnit
->TexGenEnabled
& (1<<2),
242 texUnit
->GenR
.Mode
);
243 key
->unit
[i
].texgen_mode3
=
244 translate_texgen( texUnit
->TexGenEnabled
& (1<<3),
245 texUnit
->GenQ
.Mode
);
252 /* Very useful debugging tool - produces annotated listing of
253 * generated program with line/function references for each
254 * instruction back into this file:
259 /* Use uregs to represent registers internally, translate to Mesa's
260 * expected formats on emit.
262 * NOTE: These are passed by value extensively in this file rather
263 * than as usual by pointer reference. If this disturbs you, try
264 * remembering they are just 32bits in size.
266 * GCC is smart enough to deal with these dword-sized structures in
267 * much the same way as if I had defined them as dwords and was using
268 * macros to access and set the fields. This is much nicer and easier
273 GLint idx
:9; /* relative addressing may be negative */
274 /* sizeof(idx) should == sizeof(prog_src_reg::Index) */
282 const struct state_key
*state
;
283 struct gl_vertex_program
*program
;
284 GLint max_inst
; /** number of instructions allocated for program */
285 GLboolean mvp_with_dp4
;
288 GLuint temp_reserved
;
290 struct ureg eye_position
;
291 struct ureg eye_position_z
;
292 struct ureg eye_position_normalized
;
293 struct ureg transformed_normal
;
294 struct ureg identity
;
297 GLuint color_materials
;
301 static const struct ureg undef
= {
319 static struct ureg
make_ureg(GLuint file
, GLint idx
)
325 reg
.swz
= SWIZZLE_NOOP
;
332 static struct ureg
negate( struct ureg reg
)
339 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
341 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
344 GET_SWZ(reg
.swz
, w
));
349 static struct ureg
swizzle1( struct ureg reg
, int x
)
351 return swizzle(reg
, x
, x
, x
, x
);
355 static struct ureg
get_temp( struct tnl_program
*p
)
357 int bit
= _mesa_ffs( ~p
->temp_in_use
);
359 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
363 if ((GLuint
) bit
> p
->program
->Base
.NumTemporaries
)
364 p
->program
->Base
.NumTemporaries
= bit
;
366 p
->temp_in_use
|= 1<<(bit
-1);
367 return make_ureg(PROGRAM_TEMPORARY
, bit
-1);
371 static struct ureg
reserve_temp( struct tnl_program
*p
)
373 struct ureg temp
= get_temp( p
);
374 p
->temp_reserved
|= 1<<temp
.idx
;
379 static void release_temp( struct tnl_program
*p
, struct ureg reg
)
381 if (reg
.file
== PROGRAM_TEMPORARY
) {
382 p
->temp_in_use
&= ~(1<<reg
.idx
);
383 p
->temp_in_use
|= p
->temp_reserved
; /* can't release reserved temps */
387 static void release_temps( struct tnl_program
*p
)
389 p
->temp_in_use
= p
->temp_reserved
;
393 static struct ureg
register_param5(struct tnl_program
*p
,
400 gl_state_index tokens
[STATE_LENGTH
];
407 idx
= _mesa_add_state_reference( p
->program
->Base
.Parameters
, tokens
);
408 return make_ureg(PROGRAM_STATE_VAR
, idx
);
412 #define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
413 #define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
414 #define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
415 #define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
420 * \param input one of VERT_ATTRIB_x tokens.
422 static struct ureg
register_input( struct tnl_program
*p
, GLuint input
)
426 if (p
->state
->varying_vp_inputs
& (1<<input
)) {
427 p
->program
->Base
.InputsRead
|= (1<<input
);
428 return make_ureg(PROGRAM_INPUT
, input
);
431 return register_param3( p
, STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, input
);
437 * \param input one of VERT_RESULT_x tokens.
439 static struct ureg
register_output( struct tnl_program
*p
, GLuint output
)
441 p
->program
->Base
.OutputsWritten
|= BITFIELD64_BIT(output
);
442 return make_ureg(PROGRAM_OUTPUT
, output
);
446 static struct ureg
register_const4f( struct tnl_program
*p
,
459 idx
= _mesa_add_unnamed_constant( p
->program
->Base
.Parameters
, values
, 4,
461 ASSERT(swizzle
== SWIZZLE_NOOP
);
462 return make_ureg(PROGRAM_CONSTANT
, idx
);
465 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
466 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
467 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
468 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
470 static GLboolean
is_undef( struct ureg reg
)
472 return reg
.file
== PROGRAM_UNDEFINED
;
476 static struct ureg
get_identity_param( struct tnl_program
*p
)
478 if (is_undef(p
->identity
))
479 p
->identity
= register_const4f(p
, 0,0,0,1);
484 static void register_matrix_param5( struct tnl_program
*p
,
485 GLint s0
, /* modelview, projection, etc */
486 GLint s1
, /* texture matrix number */
487 GLint s2
, /* first row */
488 GLint s3
, /* last row */
489 GLint s4
, /* inverse, transpose, etc */
490 struct ureg
*matrix
)
494 /* This is a bit sad as the support is there to pull the whole
495 * matrix out in one go:
497 for (i
= 0; i
<= s3
- s2
; i
++)
498 matrix
[i
] = register_param5( p
, s0
, s1
, i
, i
, s4
);
502 static void emit_arg( struct prog_src_register
*src
,
505 src
->File
= reg
.file
;
506 src
->Index
= reg
.idx
;
507 src
->Swizzle
= reg
.swz
;
508 src
->Negate
= reg
.negate
? NEGATE_XYZW
: NEGATE_NONE
;
511 /* Check that bitfield sizes aren't exceeded */
512 ASSERT(src
->Index
== reg
.idx
);
516 static void emit_dst( struct prog_dst_register
*dst
,
517 struct ureg reg
, GLuint mask
)
519 dst
->File
= reg
.file
;
520 dst
->Index
= reg
.idx
;
521 /* allow zero as a shorthand for xyzw */
522 dst
->WriteMask
= mask
? mask
: WRITEMASK_XYZW
;
523 dst
->CondMask
= COND_TR
; /* always pass cond test */
524 dst
->CondSwizzle
= SWIZZLE_NOOP
;
526 /* Check that bitfield sizes aren't exceeded */
527 ASSERT(dst
->Index
== reg
.idx
);
531 static void debug_insn( struct prog_instruction
*inst
, const char *fn
,
535 static const char *last_fn
;
539 _mesa_printf("%s:\n", fn
);
542 _mesa_printf("%d:\t", line
);
543 _mesa_print_instruction(inst
);
548 static void emit_op3fn(struct tnl_program
*p
,
559 struct prog_instruction
*inst
;
561 assert((GLint
) p
->program
->Base
.NumInstructions
<= p
->max_inst
);
563 if (p
->program
->Base
.NumInstructions
== p
->max_inst
) {
564 /* need to extend the program's instruction array */
565 struct prog_instruction
*newInst
;
567 /* double the size */
570 newInst
= _mesa_alloc_instructions(p
->max_inst
);
572 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "vertex program build");
576 _mesa_copy_instructions(newInst
,
577 p
->program
->Base
.Instructions
,
578 p
->program
->Base
.NumInstructions
);
580 _mesa_free_instructions(p
->program
->Base
.Instructions
,
581 p
->program
->Base
.NumInstructions
);
583 p
->program
->Base
.Instructions
= newInst
;
586 nr
= p
->program
->Base
.NumInstructions
++;
588 inst
= &p
->program
->Base
.Instructions
[nr
];
589 inst
->Opcode
= (enum prog_opcode
) op
;
592 emit_arg( &inst
->SrcReg
[0], src0
);
593 emit_arg( &inst
->SrcReg
[1], src1
);
594 emit_arg( &inst
->SrcReg
[2], src2
);
596 emit_dst( &inst
->DstReg
, dest
, mask
);
598 debug_insn(inst
, fn
, line
);
602 #define emit_op3(p, op, dst, mask, src0, src1, src2) \
603 emit_op3fn(p, op, dst, mask, src0, src1, src2, __FUNCTION__, __LINE__)
605 #define emit_op2(p, op, dst, mask, src0, src1) \
606 emit_op3fn(p, op, dst, mask, src0, src1, undef, __FUNCTION__, __LINE__)
608 #define emit_op1(p, op, dst, mask, src0) \
609 emit_op3fn(p, op, dst, mask, src0, undef, undef, __FUNCTION__, __LINE__)
612 static struct ureg
make_temp( struct tnl_program
*p
, struct ureg reg
)
614 if (reg
.file
== PROGRAM_TEMPORARY
&&
615 !(p
->temp_reserved
& (1<<reg
.idx
)))
618 struct ureg temp
= get_temp(p
);
619 emit_op1(p
, OPCODE_MOV
, temp
, 0, reg
);
625 /* Currently no tracking performed of input/output/register size or
626 * active elements. Could be used to reduce these operations, as
627 * could the matrix type.
629 static void emit_matrix_transform_vec4( struct tnl_program
*p
,
631 const struct ureg
*mat
,
634 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_X
, src
, mat
[0]);
635 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_Y
, src
, mat
[1]);
636 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_Z
, src
, mat
[2]);
637 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_W
, src
, mat
[3]);
641 /* This version is much easier to implement if writemasks are not
642 * supported natively on the target or (like SSE), the target doesn't
643 * have a clean/obvious dotproduct implementation.
645 static void emit_transpose_matrix_transform_vec4( struct tnl_program
*p
,
647 const struct ureg
*mat
,
652 if (dest
.file
!= PROGRAM_TEMPORARY
)
657 emit_op2(p
, OPCODE_MUL
, tmp
, 0, swizzle1(src
,X
), mat
[0]);
658 emit_op3(p
, OPCODE_MAD
, tmp
, 0, swizzle1(src
,Y
), mat
[1], tmp
);
659 emit_op3(p
, OPCODE_MAD
, tmp
, 0, swizzle1(src
,Z
), mat
[2], tmp
);
660 emit_op3(p
, OPCODE_MAD
, dest
, 0, swizzle1(src
,W
), mat
[3], tmp
);
662 if (dest
.file
!= PROGRAM_TEMPORARY
)
663 release_temp(p
, tmp
);
667 static void emit_matrix_transform_vec3( struct tnl_program
*p
,
669 const struct ureg
*mat
,
672 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_X
, src
, mat
[0]);
673 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_Y
, src
, mat
[1]);
674 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_Z
, src
, mat
[2]);
678 static void emit_normalize_vec3( struct tnl_program
*p
,
683 /* XXX use this when drivers are ready for NRM3 */
684 emit_op1(p
, OPCODE_NRM3
, dest
, WRITEMASK_XYZ
, src
);
686 struct ureg tmp
= get_temp(p
);
687 emit_op2(p
, OPCODE_DP3
, tmp
, WRITEMASK_X
, src
, src
);
688 emit_op1(p
, OPCODE_RSQ
, tmp
, WRITEMASK_X
, tmp
);
689 emit_op2(p
, OPCODE_MUL
, dest
, 0, src
, swizzle1(tmp
, X
));
690 release_temp(p
, tmp
);
695 static void emit_passthrough( struct tnl_program
*p
,
699 struct ureg out
= register_output(p
, output
);
700 emit_op1(p
, OPCODE_MOV
, out
, 0, register_input(p
, input
));
704 static struct ureg
get_eye_position( struct tnl_program
*p
)
706 if (is_undef(p
->eye_position
)) {
707 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
708 struct ureg modelview
[4];
710 p
->eye_position
= reserve_temp(p
);
712 if (p
->mvp_with_dp4
) {
713 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 3,
716 emit_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
719 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 3,
720 STATE_MATRIX_TRANSPOSE
, modelview
);
722 emit_transpose_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
726 return p
->eye_position
;
730 static struct ureg
get_eye_position_z( struct tnl_program
*p
)
732 if (!is_undef(p
->eye_position
))
733 return swizzle1(p
->eye_position
, Z
);
735 if (is_undef(p
->eye_position_z
)) {
736 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
737 struct ureg modelview
[4];
739 p
->eye_position_z
= reserve_temp(p
);
741 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 3,
744 emit_op2(p
, OPCODE_DP4
, p
->eye_position_z
, 0, pos
, modelview
[2]);
747 return p
->eye_position_z
;
751 static struct ureg
get_eye_position_normalized( struct tnl_program
*p
)
753 if (is_undef(p
->eye_position_normalized
)) {
754 struct ureg eye
= get_eye_position(p
);
755 p
->eye_position_normalized
= reserve_temp(p
);
756 emit_normalize_vec3(p
, p
->eye_position_normalized
, eye
);
759 return p
->eye_position_normalized
;
763 static struct ureg
get_transformed_normal( struct tnl_program
*p
)
765 if (is_undef(p
->transformed_normal
) &&
766 !p
->state
->need_eye_coords
&&
767 !p
->state
->normalize
&&
768 !(p
->state
->need_eye_coords
== p
->state
->rescale_normals
))
770 p
->transformed_normal
= register_input(p
, VERT_ATTRIB_NORMAL
);
772 else if (is_undef(p
->transformed_normal
))
774 struct ureg normal
= register_input(p
, VERT_ATTRIB_NORMAL
);
775 struct ureg mvinv
[3];
776 struct ureg transformed_normal
= reserve_temp(p
);
778 if (p
->state
->need_eye_coords
) {
779 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 2,
780 STATE_MATRIX_INVTRANS
, mvinv
);
782 /* Transform to eye space:
784 emit_matrix_transform_vec3( p
, transformed_normal
, mvinv
, normal
);
785 normal
= transformed_normal
;
788 /* Normalize/Rescale:
790 if (p
->state
->normalize
) {
791 emit_normalize_vec3( p
, transformed_normal
, normal
);
792 normal
= transformed_normal
;
794 else if (p
->state
->need_eye_coords
== p
->state
->rescale_normals
) {
795 /* This is already adjusted for eye/non-eye rendering:
797 struct ureg rescale
= register_param2(p
, STATE_INTERNAL
,
800 emit_op2( p
, OPCODE_MUL
, transformed_normal
, 0, normal
, rescale
);
801 normal
= transformed_normal
;
804 assert(normal
.file
== PROGRAM_TEMPORARY
);
805 p
->transformed_normal
= normal
;
808 return p
->transformed_normal
;
812 static void build_hpos( struct tnl_program
*p
)
814 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
815 struct ureg hpos
= register_output( p
, VERT_RESULT_HPOS
);
818 if (p
->mvp_with_dp4
) {
819 register_matrix_param5( p
, STATE_MVP_MATRIX
, 0, 0, 3,
821 emit_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
824 register_matrix_param5( p
, STATE_MVP_MATRIX
, 0, 0, 3,
825 STATE_MATRIX_TRANSPOSE
, mvp
);
826 emit_transpose_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
831 static GLuint
material_attrib( GLuint side
, GLuint property
)
833 return (property
- STATE_AMBIENT
) * 2 + side
;
838 * Get a bitmask of which material values vary on a per-vertex basis.
840 static void set_material_flags( struct tnl_program
*p
)
842 p
->color_materials
= 0;
845 if (p
->state
->varying_vp_inputs
& VERT_BIT_COLOR0
) {
847 p
->color_materials
= p
->state
->light_color_material_mask
;
850 p
->materials
|= (p
->state
->varying_vp_inputs
>> 16);
854 static struct ureg
get_material( struct tnl_program
*p
, GLuint side
,
857 GLuint attrib
= material_attrib(side
, property
);
859 if (p
->color_materials
& (1<<attrib
))
860 return register_input(p
, VERT_ATTRIB_COLOR0
);
861 else if (p
->materials
& (1<<attrib
)) {
862 /* Put material values in the GENERIC slots -- they are not used
863 * for anything in fixed function mode.
865 return register_input( p
, attrib
+ VERT_ATTRIB_GENERIC0
);
868 return register_param3( p
, STATE_MATERIAL
, side
, property
);
871 #define SCENE_COLOR_BITS(side) (( MAT_BIT_FRONT_EMISSION | \
872 MAT_BIT_FRONT_AMBIENT | \
873 MAT_BIT_FRONT_DIFFUSE) << (side))
877 * Either return a precalculated constant value or emit code to
878 * calculate these values dynamically in the case where material calls
879 * are present between begin/end pairs.
881 * Probably want to shift this to the program compilation phase - if
882 * we always emitted the calculation here, a smart compiler could
883 * detect that it was constant (given a certain set of inputs), and
884 * lift it out of the main loop. That way the programs created here
885 * would be independent of the vertex_buffer details.
887 static struct ureg
get_scenecolor( struct tnl_program
*p
, GLuint side
)
889 if (p
->materials
& SCENE_COLOR_BITS(side
)) {
890 struct ureg lm_ambient
= register_param1(p
, STATE_LIGHTMODEL_AMBIENT
);
891 struct ureg material_emission
= get_material(p
, side
, STATE_EMISSION
);
892 struct ureg material_ambient
= get_material(p
, side
, STATE_AMBIENT
);
893 struct ureg material_diffuse
= get_material(p
, side
, STATE_DIFFUSE
);
894 struct ureg tmp
= make_temp(p
, material_diffuse
);
895 emit_op3(p
, OPCODE_MAD
, tmp
, WRITEMASK_XYZ
, lm_ambient
,
896 material_ambient
, material_emission
);
900 return register_param2( p
, STATE_LIGHTMODEL_SCENECOLOR
, side
);
904 static struct ureg
get_lightprod( struct tnl_program
*p
, GLuint light
,
905 GLuint side
, GLuint property
)
907 GLuint attrib
= material_attrib(side
, property
);
908 if (p
->materials
& (1<<attrib
)) {
909 struct ureg light_value
=
910 register_param3(p
, STATE_LIGHT
, light
, property
);
911 struct ureg material_value
= get_material(p
, side
, property
);
912 struct ureg tmp
= get_temp(p
);
913 emit_op2(p
, OPCODE_MUL
, tmp
, 0, light_value
, material_value
);
917 return register_param4(p
, STATE_LIGHTPROD
, light
, side
, property
);
921 static struct ureg
calculate_light_attenuation( struct tnl_program
*p
,
926 struct ureg attenuation
= register_param3(p
, STATE_LIGHT
, i
,
928 struct ureg att
= get_temp(p
);
930 /* Calculate spot attenuation:
932 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
933 struct ureg spot_dir_norm
= register_param3(p
, STATE_INTERNAL
,
934 STATE_LIGHT_SPOT_DIR_NORMALIZED
, i
);
935 struct ureg spot
= get_temp(p
);
936 struct ureg slt
= get_temp(p
);
938 emit_op2(p
, OPCODE_DP3
, spot
, 0, negate(VPpli
), spot_dir_norm
);
939 emit_op2(p
, OPCODE_SLT
, slt
, 0, swizzle1(spot_dir_norm
,W
), spot
);
940 emit_op2(p
, OPCODE_POW
, spot
, 0, spot
, swizzle1(attenuation
, W
));
941 emit_op2(p
, OPCODE_MUL
, att
, 0, slt
, spot
);
943 release_temp(p
, spot
);
944 release_temp(p
, slt
);
947 /* Calculate distance attenuation:
949 if (p
->state
->unit
[i
].light_attenuated
) {
951 emit_op1(p
, OPCODE_RCP
, dist
, WRITEMASK_YZ
, dist
);
953 emit_op2(p
, OPCODE_MUL
, dist
, WRITEMASK_XZ
, dist
, swizzle1(dist
,Y
));
955 emit_op2(p
, OPCODE_DP3
, dist
, 0, attenuation
, dist
);
957 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
959 emit_op1(p
, OPCODE_RCP
, dist
, 0, dist
);
960 /* spot-atten * dist-atten */
961 emit_op2(p
, OPCODE_MUL
, att
, 0, dist
, att
);
965 emit_op1(p
, OPCODE_RCP
, att
, 0, dist
);
975 * lit.y = MAX(0, dots.x)
976 * lit.z = SLT(0, dots.x)
978 static void emit_degenerate_lit( struct tnl_program
*p
,
982 struct ureg id
= get_identity_param(p
); /* id = {0,0,0,1} */
984 /* Note that lit.x & lit.w will not be examined. Note also that
985 * dots.xyzw == dots.xxxx.
988 /* MAX lit, id, dots;
990 emit_op2(p
, OPCODE_MAX
, lit
, WRITEMASK_XYZW
, id
, dots
);
992 /* result[2] = (in > 0 ? 1 : 0)
993 * SLT lit.z, id.z, dots; # lit.z = (0 < dots.z) ? 1 : 0
995 emit_op2(p
, OPCODE_SLT
, lit
, WRITEMASK_Z
, swizzle1(id
,Z
), dots
);
999 /* Need to add some addtional parameters to allow lighting in object
1000 * space - STATE_SPOT_DIRECTION and STATE_HALF_VECTOR implicitly assume eye
1003 static void build_lighting( struct tnl_program
*p
)
1005 const GLboolean twoside
= p
->state
->light_twoside
;
1006 const GLboolean separate
= p
->state
->separate_specular
;
1007 GLuint nr_lights
= 0, count
= 0;
1008 struct ureg normal
= get_transformed_normal(p
);
1009 struct ureg lit
= get_temp(p
);
1010 struct ureg dots
= get_temp(p
);
1011 struct ureg _col0
= undef
, _col1
= undef
;
1012 struct ureg _bfc0
= undef
, _bfc1
= undef
;
1017 * dots.x = dot(normal, VPpli)
1018 * dots.y = dot(normal, halfAngle)
1019 * dots.z = back.shininess
1020 * dots.w = front.shininess
1023 for (i
= 0; i
< MAX_LIGHTS
; i
++)
1024 if (p
->state
->unit
[i
].light_enabled
)
1027 set_material_flags(p
);
1030 if (!p
->state
->material_shininess_is_zero
) {
1031 struct ureg shininess
= get_material(p
, 0, STATE_SHININESS
);
1032 emit_op1(p
, OPCODE_MOV
, dots
, WRITEMASK_W
, swizzle1(shininess
,X
));
1033 release_temp(p
, shininess
);
1036 _col0
= make_temp(p
, get_scenecolor(p
, 0));
1038 _col1
= make_temp(p
, get_identity_param(p
));
1044 if (!p
->state
->material_shininess_is_zero
) {
1045 /* Note that we negate the back-face specular exponent here.
1046 * The negation will be un-done later in the back-face code below.
1048 struct ureg shininess
= get_material(p
, 1, STATE_SHININESS
);
1049 emit_op1(p
, OPCODE_MOV
, dots
, WRITEMASK_Z
,
1050 negate(swizzle1(shininess
,X
)));
1051 release_temp(p
, shininess
);
1054 _bfc0
= make_temp(p
, get_scenecolor(p
, 1));
1056 _bfc1
= make_temp(p
, get_identity_param(p
));
1061 /* If no lights, still need to emit the scenecolor.
1064 struct ureg res0
= register_output( p
, VERT_RESULT_COL0
);
1065 emit_op1(p
, OPCODE_MOV
, res0
, 0, _col0
);
1069 struct ureg res1
= register_output( p
, VERT_RESULT_COL1
);
1070 emit_op1(p
, OPCODE_MOV
, res1
, 0, _col1
);
1074 struct ureg res0
= register_output( p
, VERT_RESULT_BFC0
);
1075 emit_op1(p
, OPCODE_MOV
, res0
, 0, _bfc0
);
1078 if (twoside
&& separate
) {
1079 struct ureg res1
= register_output( p
, VERT_RESULT_BFC1
);
1080 emit_op1(p
, OPCODE_MOV
, res1
, 0, _bfc1
);
1083 if (nr_lights
== 0) {
1088 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
1089 if (p
->state
->unit
[i
].light_enabled
) {
1090 struct ureg half
= undef
;
1091 struct ureg att
= undef
, VPpli
= undef
;
1095 if (p
->state
->unit
[i
].light_eyepos3_is_zero
) {
1096 /* Can used precomputed constants in this case.
1097 * Attenuation never applies to infinite lights.
1099 VPpli
= register_param3(p
, STATE_INTERNAL
,
1100 STATE_LIGHT_POSITION_NORMALIZED
, i
);
1102 if (!p
->state
->material_shininess_is_zero
) {
1103 if (p
->state
->light_local_viewer
) {
1104 struct ureg eye_hat
= get_eye_position_normalized(p
);
1106 emit_op2(p
, OPCODE_SUB
, half
, 0, VPpli
, eye_hat
);
1107 emit_normalize_vec3(p
, half
, half
);
1110 half
= register_param3(p
, STATE_INTERNAL
,
1111 STATE_LIGHT_HALF_VECTOR
, i
);
1116 struct ureg Ppli
= register_param3(p
, STATE_INTERNAL
,
1117 STATE_LIGHT_POSITION
, i
);
1118 struct ureg V
= get_eye_position(p
);
1119 struct ureg dist
= get_temp(p
);
1121 VPpli
= get_temp(p
);
1123 /* Calculate VPpli vector
1125 emit_op2(p
, OPCODE_SUB
, VPpli
, 0, Ppli
, V
);
1127 /* Normalize VPpli. The dist value also used in
1128 * attenuation below.
1130 emit_op2(p
, OPCODE_DP3
, dist
, 0, VPpli
, VPpli
);
1131 emit_op1(p
, OPCODE_RSQ
, dist
, 0, dist
);
1132 emit_op2(p
, OPCODE_MUL
, VPpli
, 0, VPpli
, dist
);
1134 /* Calculate attenuation:
1136 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
||
1137 p
->state
->unit
[i
].light_attenuated
) {
1138 att
= calculate_light_attenuation(p
, i
, VPpli
, dist
);
1141 /* Calculate viewer direction, or use infinite viewer:
1143 if (!p
->state
->material_shininess_is_zero
) {
1146 if (p
->state
->light_local_viewer
) {
1147 struct ureg eye_hat
= get_eye_position_normalized(p
);
1148 emit_op2(p
, OPCODE_SUB
, half
, 0, VPpli
, eye_hat
);
1151 struct ureg z_dir
= swizzle(get_identity_param(p
),X
,Y
,W
,Z
);
1152 emit_op2(p
, OPCODE_ADD
, half
, 0, VPpli
, z_dir
);
1155 emit_normalize_vec3(p
, half
, half
);
1158 release_temp(p
, dist
);
1161 /* Calculate dot products:
1163 if (p
->state
->material_shininess_is_zero
) {
1164 emit_op2(p
, OPCODE_DP3
, dots
, 0, normal
, VPpli
);
1167 emit_op2(p
, OPCODE_DP3
, dots
, WRITEMASK_X
, normal
, VPpli
);
1168 emit_op2(p
, OPCODE_DP3
, dots
, WRITEMASK_Y
, normal
, half
);
1171 /* Front face lighting:
1174 struct ureg ambient
= get_lightprod(p
, i
, 0, STATE_AMBIENT
);
1175 struct ureg diffuse
= get_lightprod(p
, i
, 0, STATE_DIFFUSE
);
1176 struct ureg specular
= get_lightprod(p
, i
, 0, STATE_SPECULAR
);
1177 struct ureg res0
, res1
;
1178 GLuint mask0
, mask1
;
1180 if (count
== nr_lights
) {
1182 mask0
= WRITEMASK_XYZ
;
1183 mask1
= WRITEMASK_XYZ
;
1184 res0
= register_output( p
, VERT_RESULT_COL0
);
1185 res1
= register_output( p
, VERT_RESULT_COL1
);
1189 mask1
= WRITEMASK_XYZ
;
1191 res1
= register_output( p
, VERT_RESULT_COL0
);
1201 if (!is_undef(att
)) {
1202 /* light is attenuated by distance */
1203 emit_op1(p
, OPCODE_LIT
, lit
, 0, dots
);
1204 emit_op2(p
, OPCODE_MUL
, lit
, 0, lit
, att
);
1205 emit_op3(p
, OPCODE_MAD
, _col0
, 0, swizzle1(lit
,X
), ambient
, _col0
);
1207 else if (!p
->state
->material_shininess_is_zero
) {
1208 /* there's a non-zero specular term */
1209 emit_op1(p
, OPCODE_LIT
, lit
, 0, dots
);
1210 emit_op2(p
, OPCODE_ADD
, _col0
, 0, ambient
, _col0
);
1213 /* no attenutation, no specular */
1214 emit_degenerate_lit(p
, lit
, dots
);
1215 emit_op2(p
, OPCODE_ADD
, _col0
, 0, ambient
, _col0
);
1218 emit_op3(p
, OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _col0
);
1219 emit_op3(p
, OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _col1
);
1221 release_temp(p
, ambient
);
1222 release_temp(p
, diffuse
);
1223 release_temp(p
, specular
);
1226 /* Back face lighting:
1229 struct ureg ambient
= get_lightprod(p
, i
, 1, STATE_AMBIENT
);
1230 struct ureg diffuse
= get_lightprod(p
, i
, 1, STATE_DIFFUSE
);
1231 struct ureg specular
= get_lightprod(p
, i
, 1, STATE_SPECULAR
);
1232 struct ureg res0
, res1
;
1233 GLuint mask0
, mask1
;
1235 if (count
== nr_lights
) {
1237 mask0
= WRITEMASK_XYZ
;
1238 mask1
= WRITEMASK_XYZ
;
1239 res0
= register_output( p
, VERT_RESULT_BFC0
);
1240 res1
= register_output( p
, VERT_RESULT_BFC1
);
1244 mask1
= WRITEMASK_XYZ
;
1246 res1
= register_output( p
, VERT_RESULT_BFC0
);
1256 /* For the back face we need to negate the X and Y component
1257 * dot products. dots.Z has the negated back-face specular
1258 * exponent. We swizzle that into the W position. This
1259 * negation makes the back-face specular term positive again.
1261 dots
= negate(swizzle(dots
,X
,Y
,W
,Z
));
1263 if (!is_undef(att
)) {
1264 emit_op1(p
, OPCODE_LIT
, lit
, 0, dots
);
1265 emit_op2(p
, OPCODE_MUL
, lit
, 0, lit
, att
);
1266 emit_op3(p
, OPCODE_MAD
, _bfc0
, 0, swizzle1(lit
,X
), ambient
, _bfc0
);
1268 else if (!p
->state
->material_shininess_is_zero
) {
1269 emit_op1(p
, OPCODE_LIT
, lit
, 0, dots
);
1270 emit_op2(p
, OPCODE_ADD
, _bfc0
, 0, ambient
, _bfc0
); /**/
1273 emit_degenerate_lit(p
, lit
, dots
);
1274 emit_op2(p
, OPCODE_ADD
, _bfc0
, 0, ambient
, _bfc0
);
1277 emit_op3(p
, OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _bfc0
);
1278 emit_op3(p
, OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _bfc1
);
1279 /* restore dots to its original state for subsequent lights
1280 * by negating and swizzling again.
1282 dots
= negate(swizzle(dots
,X
,Y
,W
,Z
));
1284 release_temp(p
, ambient
);
1285 release_temp(p
, diffuse
);
1286 release_temp(p
, specular
);
1289 release_temp(p
, half
);
1290 release_temp(p
, VPpli
);
1291 release_temp(p
, att
);
1299 static void build_fog( struct tnl_program
*p
)
1301 struct ureg fog
= register_output(p
, VERT_RESULT_FOGC
);
1304 if (p
->state
->fog_source_is_depth
) {
1305 input
= get_eye_position_z(p
);
1308 input
= swizzle1(register_input(p
, VERT_ATTRIB_FOG
), X
);
1311 /* result.fog = {abs(f),0,0,1}; */
1312 emit_op1(p
, OPCODE_ABS
, fog
, WRITEMASK_X
, input
);
1313 emit_op1(p
, OPCODE_MOV
, fog
, WRITEMASK_YZW
, get_identity_param(p
));
1317 static void build_reflect_texgen( struct tnl_program
*p
,
1321 struct ureg normal
= get_transformed_normal(p
);
1322 struct ureg eye_hat
= get_eye_position_normalized(p
);
1323 struct ureg tmp
= get_temp(p
);
1326 emit_op2(p
, OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1328 emit_op2(p
, OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1330 emit_op3(p
, OPCODE_MAD
, dest
, writemask
, negate(tmp
), normal
, eye_hat
);
1332 release_temp(p
, tmp
);
1336 static void build_sphere_texgen( struct tnl_program
*p
,
1340 struct ureg normal
= get_transformed_normal(p
);
1341 struct ureg eye_hat
= get_eye_position_normalized(p
);
1342 struct ureg tmp
= get_temp(p
);
1343 struct ureg half
= register_scalar_const(p
, .5);
1344 struct ureg r
= get_temp(p
);
1345 struct ureg inv_m
= get_temp(p
);
1346 struct ureg id
= get_identity_param(p
);
1348 /* Could share the above calculations, but it would be
1349 * a fairly odd state for someone to set (both sphere and
1350 * reflection active for different texture coordinate
1351 * components. Of course - if two texture units enable
1352 * reflect and/or sphere, things start to tilt in favour
1353 * of seperating this out:
1357 emit_op2(p
, OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1359 emit_op2(p
, OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1361 emit_op3(p
, OPCODE_MAD
, r
, 0, negate(tmp
), normal
, eye_hat
);
1363 emit_op2(p
, OPCODE_ADD
, tmp
, 0, r
, swizzle(id
,X
,Y
,W
,Z
));
1364 /* rx^2 + ry^2 + (rz+1)^2 */
1365 emit_op2(p
, OPCODE_DP3
, tmp
, 0, tmp
, tmp
);
1367 emit_op1(p
, OPCODE_RSQ
, tmp
, 0, tmp
);
1369 emit_op2(p
, OPCODE_MUL
, inv_m
, 0, tmp
, half
);
1371 emit_op3(p
, OPCODE_MAD
, dest
, writemask
, r
, inv_m
, half
);
1373 release_temp(p
, tmp
);
1375 release_temp(p
, inv_m
);
1379 static void build_texture_transform( struct tnl_program
*p
)
1383 for (i
= 0; i
< MAX_TEXTURE_COORD_UNITS
; i
++) {
1385 if (!(p
->state
->fragprog_inputs_read
& FRAG_BIT_TEX(i
)))
1388 if (p
->state
->unit
[i
].texgen_enabled
||
1389 p
->state
->unit
[i
].texmat_enabled
) {
1391 GLuint texmat_enabled
= p
->state
->unit
[i
].texmat_enabled
;
1392 struct ureg out
= register_output(p
, VERT_RESULT_TEX0
+ i
);
1393 struct ureg out_texgen
= undef
;
1395 if (p
->state
->unit
[i
].texgen_enabled
) {
1396 GLuint copy_mask
= 0;
1397 GLuint sphere_mask
= 0;
1398 GLuint reflect_mask
= 0;
1399 GLuint normal_mask
= 0;
1403 out_texgen
= get_temp(p
);
1407 modes
[0] = p
->state
->unit
[i
].texgen_mode0
;
1408 modes
[1] = p
->state
->unit
[i
].texgen_mode1
;
1409 modes
[2] = p
->state
->unit
[i
].texgen_mode2
;
1410 modes
[3] = p
->state
->unit
[i
].texgen_mode3
;
1412 for (j
= 0; j
< 4; j
++) {
1414 case TXG_OBJ_LINEAR
: {
1415 struct ureg obj
= register_input(p
, VERT_ATTRIB_POS
);
1417 register_param3(p
, STATE_TEXGEN
, i
,
1418 STATE_TEXGEN_OBJECT_S
+ j
);
1420 emit_op2(p
, OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1424 case TXG_EYE_LINEAR
: {
1425 struct ureg eye
= get_eye_position(p
);
1427 register_param3(p
, STATE_TEXGEN
, i
,
1428 STATE_TEXGEN_EYE_S
+ j
);
1430 emit_op2(p
, OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1434 case TXG_SPHERE_MAP
:
1435 sphere_mask
|= WRITEMASK_X
<< j
;
1437 case TXG_REFLECTION_MAP
:
1438 reflect_mask
|= WRITEMASK_X
<< j
;
1440 case TXG_NORMAL_MAP
:
1441 normal_mask
|= WRITEMASK_X
<< j
;
1444 copy_mask
|= WRITEMASK_X
<< j
;
1449 build_sphere_texgen(p
, out_texgen
, sphere_mask
);
1453 build_reflect_texgen(p
, out_texgen
, reflect_mask
);
1457 struct ureg normal
= get_transformed_normal(p
);
1458 emit_op1(p
, OPCODE_MOV
, out_texgen
, normal_mask
, normal
);
1462 struct ureg in
= register_input(p
, VERT_ATTRIB_TEX0
+i
);
1463 emit_op1(p
, OPCODE_MOV
, out_texgen
, copy_mask
, in
);
1467 if (texmat_enabled
) {
1468 struct ureg texmat
[4];
1469 struct ureg in
= (!is_undef(out_texgen
) ?
1471 register_input(p
, VERT_ATTRIB_TEX0
+i
));
1472 if (p
->mvp_with_dp4
) {
1473 register_matrix_param5( p
, STATE_TEXTURE_MATRIX
, i
, 0, 3,
1475 emit_matrix_transform_vec4( p
, out
, texmat
, in
);
1478 register_matrix_param5( p
, STATE_TEXTURE_MATRIX
, i
, 0, 3,
1479 STATE_MATRIX_TRANSPOSE
, texmat
);
1480 emit_transpose_matrix_transform_vec4( p
, out
, texmat
, in
);
1487 emit_passthrough(p
, VERT_ATTRIB_TEX0
+i
, VERT_RESULT_TEX0
+i
);
1494 * Point size attenuation computation.
1496 static void build_atten_pointsize( struct tnl_program
*p
)
1498 struct ureg eye
= get_eye_position_z(p
);
1499 struct ureg state_size
= register_param2(p
, STATE_INTERNAL
, STATE_POINT_SIZE_CLAMPED
);
1500 struct ureg state_attenuation
= register_param1(p
, STATE_POINT_ATTENUATION
);
1501 struct ureg out
= register_output(p
, VERT_RESULT_PSIZ
);
1502 struct ureg ut
= get_temp(p
);
1505 emit_op1(p
, OPCODE_ABS
, ut
, WRITEMASK_Y
, swizzle1(eye
, Z
));
1506 /* p1 + dist * (p2 + dist * p3); */
1507 emit_op3(p
, OPCODE_MAD
, ut
, WRITEMASK_X
, swizzle1(ut
, Y
),
1508 swizzle1(state_attenuation
, Z
), swizzle1(state_attenuation
, Y
));
1509 emit_op3(p
, OPCODE_MAD
, ut
, WRITEMASK_X
, swizzle1(ut
, Y
),
1510 ut
, swizzle1(state_attenuation
, X
));
1512 /* 1 / sqrt(factor) */
1513 emit_op1(p
, OPCODE_RSQ
, ut
, WRITEMASK_X
, ut
);
1516 /* out = pointSize / sqrt(factor) */
1517 emit_op2(p
, OPCODE_MUL
, out
, WRITEMASK_X
, ut
, state_size
);
1519 /* this is a good place to clamp the point size since there's likely
1520 * no hardware registers to clamp point size at rasterization time.
1522 emit_op2(p
, OPCODE_MUL
, ut
, WRITEMASK_X
, ut
, state_size
);
1523 emit_op2(p
, OPCODE_MAX
, ut
, WRITEMASK_X
, ut
, swizzle1(state_size
, Y
));
1524 emit_op2(p
, OPCODE_MIN
, out
, WRITEMASK_X
, ut
, swizzle1(state_size
, Z
));
1527 release_temp(p
, ut
);
1532 * Pass-though per-vertex point size, from user's point size array.
1534 static void build_array_pointsize( struct tnl_program
*p
)
1536 struct ureg in
= register_input(p
, VERT_ATTRIB_POINT_SIZE
);
1537 struct ureg out
= register_output(p
, VERT_RESULT_PSIZ
);
1538 emit_op1(p
, OPCODE_MOV
, out
, WRITEMASK_X
, in
);
1542 static void build_tnl_program( struct tnl_program
*p
)
1544 /* Emit the program, starting with modelviewproject:
1548 /* Lighting calculations:
1550 if (p
->state
->fragprog_inputs_read
& (FRAG_BIT_COL0
|FRAG_BIT_COL1
)) {
1551 if (p
->state
->light_global_enabled
)
1554 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
1555 emit_passthrough(p
, VERT_ATTRIB_COLOR0
, VERT_RESULT_COL0
);
1557 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL1
)
1558 emit_passthrough(p
, VERT_ATTRIB_COLOR1
, VERT_RESULT_COL1
);
1562 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_FOGC
)
1565 if (p
->state
->fragprog_inputs_read
& FRAG_BITS_TEX_ANY
)
1566 build_texture_transform(p
);
1568 if (p
->state
->point_attenuated
)
1569 build_atten_pointsize(p
);
1570 else if (p
->state
->point_array
)
1571 build_array_pointsize(p
);
1575 emit_op1(p
, OPCODE_END
, undef
, 0, undef
);
1580 _mesa_printf ("\n");
1586 create_new_program( const struct state_key
*key
,
1587 struct gl_vertex_program
*program
,
1588 GLboolean mvp_with_dp4
,
1591 struct tnl_program p
;
1593 _mesa_memset(&p
, 0, sizeof(p
));
1595 p
.program
= program
;
1596 p
.eye_position
= undef
;
1597 p
.eye_position_z
= undef
;
1598 p
.eye_position_normalized
= undef
;
1599 p
.transformed_normal
= undef
;
1602 p
.mvp_with_dp4
= mvp_with_dp4
;
1604 if (max_temps
>= sizeof(int) * 8)
1605 p
.temp_reserved
= 0;
1607 p
.temp_reserved
= ~((1<<max_temps
)-1);
1609 /* Start by allocating 32 instructions.
1610 * If we need more, we'll grow the instruction array as needed.
1613 p
.program
->Base
.Instructions
= _mesa_alloc_instructions(p
.max_inst
);
1614 p
.program
->Base
.String
= NULL
;
1615 p
.program
->Base
.NumInstructions
=
1616 p
.program
->Base
.NumTemporaries
=
1617 p
.program
->Base
.NumParameters
=
1618 p
.program
->Base
.NumAttributes
= p
.program
->Base
.NumAddressRegs
= 0;
1619 p
.program
->Base
.Parameters
= _mesa_new_parameter_list();
1620 p
.program
->Base
.InputsRead
= 0;
1621 p
.program
->Base
.OutputsWritten
= 0;
1623 build_tnl_program( &p
);
1628 * Return a vertex program which implements the current fixed-function
1629 * transform/lighting/texgen operations.
1630 * XXX move this into core mesa (main/)
1632 struct gl_vertex_program
*
1633 _mesa_get_fixed_func_vertex_program(GLcontext
*ctx
)
1635 struct gl_vertex_program
*prog
;
1636 struct state_key key
;
1638 /* Grab all the relevent state and put it in a single structure:
1640 make_state_key(ctx
, &key
);
1642 /* Look for an already-prepared program for this state:
1644 prog
= (struct gl_vertex_program
*)
1645 _mesa_search_program_cache(ctx
->VertexProgram
.Cache
, &key
, sizeof(key
));
1648 /* OK, we'll have to build a new one */
1650 _mesa_printf("Build new TNL program\n");
1652 prog
= (struct gl_vertex_program
*)
1653 ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
1657 create_new_program( &key
, prog
,
1659 ctx
->Const
.VertexProgram
.MaxTemps
);
1662 if (ctx
->Driver
.ProgramStringNotify
)
1663 ctx
->Driver
.ProgramStringNotify( ctx
, GL_VERTEX_PROGRAM_ARB
,
1666 _mesa_program_cache_insert(ctx
, ctx
->VertexProgram
.Cache
,
1667 &key
, sizeof(key
), &prog
->Base
);