2 * Mesa 3-D graphics library
5 * Copyright (C) 2005 Tungsten Graphics All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * TUNGSTEN GRAPHICS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Create a vertex program to execute the current fixed function T&L pipeline.
29 * \author Keith Whitwell
38 #include "t_context.h"
39 #include "t_vp_build.h"
41 #include "shader/program.h"
42 #include "shader/nvvertprog.h"
43 #include "shader/arbvertparse.h"
46 /* Very useful debugging tool - produces annotated listing of
47 * generated program with line/function references for each
48 * instruction back into this file:
52 /* Use uregs to represent registers internally, translate to Mesa's
53 * expected formats on emit.
55 * NOTE: These are passed by value extensively in this file rather
56 * than as usual by pointer reference. If this disturbs you, try
57 * remembering they are just 32bits in size.
59 * GCC is smart enough to deal with these dword-sized structures in
60 * much the same way as if I had defined them as dwords and was using
61 * macros to access and set the fields. This is much nicer and easier
75 struct vertex_program
*program
;
80 struct ureg eye_position
;
81 struct ureg eye_position_normalized
;
82 struct ureg eye_normal
;
86 GLuint color_materials
;
90 const static struct ureg undef
= {
108 static struct ureg
make_ureg(GLuint file
, GLuint idx
)
114 reg
.swz
= SWIZZLE_NOOP
;
121 static struct ureg
negate( struct ureg reg
)
128 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
130 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
133 GET_SWZ(reg
.swz
, w
));
138 static struct ureg
swizzle1( struct ureg reg
, int x
)
140 return swizzle(reg
, x
, x
, x
, x
);
143 static struct ureg
get_temp( struct tnl_program
*p
)
145 int bit
= ffs( ~p
->temp_flag
);
147 fprintf(stderr
, "%s: out of temporaries\n", __FILE__
);
151 p
->temp_flag
|= 1<<(bit
-1);
152 return make_ureg(PROGRAM_TEMPORARY
, bit
-1);
155 static struct ureg
reserve_temp( struct tnl_program
*p
)
157 struct ureg temp
= get_temp( p
);
158 p
->temp_reserved
|= 1<<temp
.idx
;
162 static void release_temp( struct tnl_program
*p
, struct ureg reg
)
164 if (reg
.file
== PROGRAM_TEMPORARY
) {
165 p
->temp_flag
&= ~(1<<reg
.idx
);
166 p
->temp_flag
|= p
->temp_reserved
; /* can't release reserved temps */
170 static void release_temps( struct tnl_program
*p
)
172 p
->temp_flag
= p
->temp_reserved
;
177 static struct ureg
register_input( struct tnl_program
*p
, GLuint input
)
179 p
->program
->InputsRead
|= (1<<input
);
180 return make_ureg(PROGRAM_INPUT
, input
);
183 static struct ureg
register_output( struct tnl_program
*p
, GLuint output
)
185 p
->program
->OutputsWritten
|= (1<<output
);
186 return make_ureg(PROGRAM_OUTPUT
, output
);
189 static struct ureg
register_const4f( struct tnl_program
*p
,
201 idx
= _mesa_add_unnamed_constant( p
->program
->Parameters
, values
);
202 return make_ureg(PROGRAM_STATE_VAR
, idx
);
205 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
206 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
207 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
209 static GLboolean
is_undef( struct ureg reg
)
211 return reg
.file
== 0xf;
214 static struct ureg
get_identity_param( struct tnl_program
*p
)
216 if (is_undef(p
->identity
))
217 p
->identity
= register_const4f(p
, 0,0,0,1);
222 static struct ureg
register_param6( struct tnl_program
*p
,
238 idx
= _mesa_add_state_reference( p
->program
->Parameters
, tokens
);
239 return make_ureg(PROGRAM_STATE_VAR
, idx
);
243 #define register_param1(p,s0) register_param6(p,s0,0,0,0,0,0)
244 #define register_param2(p,s0,s1) register_param6(p,s0,s1,0,0,0,0)
245 #define register_param3(p,s0,s1,s2) register_param6(p,s0,s1,s2,0,0,0)
246 #define register_param4(p,s0,s1,s2,s3) register_param6(p,s0,s1,s2,s3,0,0)
249 static void register_matrix_param6( struct tnl_program
*p
,
256 struct ureg
*matrix
)
260 /* This is a bit sad as the support is there to pull the whole
261 * matrix out in one go:
263 for (i
= 0; i
<= s4
- s3
; i
++)
264 matrix
[i
] = register_param6( p
, s0
, s1
, s2
, i
, i
, s5
);
268 static void emit_arg( struct vp_src_register
*src
,
271 src
->File
= reg
.file
;
272 src
->Index
= reg
.idx
;
273 src
->Swizzle
= reg
.swz
;
274 src
->Negate
= reg
.negate
;
279 static void emit_dst( struct vp_dst_register
*dst
,
280 struct ureg reg
, GLuint mask
)
282 dst
->File
= reg
.file
;
283 dst
->Index
= reg
.idx
;
284 /* allow zero as a shorthand for xyzw */
285 dst
->WriteMask
= mask
? mask
: WRITEMASK_XYZW
;
289 static void debug_insn( struct vp_instruction
*inst
, const char *fn
,
293 static const char *last_fn
;
297 _mesa_printf("%s:\n", fn
);
300 _mesa_printf("%d:\t", line
);
301 _mesa_debug_vp_inst(1, inst
);
306 static void emit_op3fn(struct tnl_program
*p
,
316 GLuint nr
= p
->program
->Base
.NumInstructions
++;
317 struct vp_instruction
*inst
= &p
->program
->Instructions
[nr
];
323 emit_arg( &inst
->SrcReg
[0], src0
);
324 emit_arg( &inst
->SrcReg
[1], src1
);
325 emit_arg( &inst
->SrcReg
[2], src2
);
327 emit_dst( &inst
->DstReg
, dest
, mask
);
329 debug_insn(inst
, fn
, line
);
334 #define emit_op3(p, op, dst, mask, src0, src1, src2) \
335 emit_op3fn(p, op, dst, mask, src0, src1, src2, __FUNCTION__, __LINE__)
337 #define emit_op2(p, op, dst, mask, src0, src1) \
338 emit_op3fn(p, op, dst, mask, src0, src1, undef, __FUNCTION__, __LINE__)
340 #define emit_op1(p, op, dst, mask, src0) \
341 emit_op3fn(p, op, dst, mask, src0, undef, undef, __FUNCTION__, __LINE__)
344 static struct ureg
make_temp( struct tnl_program
*p
, struct ureg reg
)
346 if (reg
.file
== PROGRAM_TEMPORARY
&&
347 !(p
->temp_reserved
& (1<<reg
.idx
)))
350 struct ureg temp
= get_temp(p
);
351 emit_op1(p
, VP_OPCODE_MOV
, temp
, 0, reg
);
357 /* Currently no tracking performed of input/output/register size or
358 * active elements. Could be used to reduce these operations, as
359 * could the matrix type.
361 static void emit_matrix_transform_vec4( struct tnl_program
*p
,
363 const struct ureg
*mat
,
366 emit_op2(p
, VP_OPCODE_DP4
, dest
, WRITEMASK_X
, src
, mat
[0]);
367 emit_op2(p
, VP_OPCODE_DP4
, dest
, WRITEMASK_Y
, src
, mat
[1]);
368 emit_op2(p
, VP_OPCODE_DP4
, dest
, WRITEMASK_Z
, src
, mat
[2]);
369 emit_op2(p
, VP_OPCODE_DP4
, dest
, WRITEMASK_W
, src
, mat
[3]);
372 /* This version is much easier to implement if writemasks are not
373 * supported natively on the target or (like SSE), the target doesn't
374 * have a clean/obvious dotproduct implementation.
376 static void emit_transpose_matrix_transform_vec4( struct tnl_program
*p
,
378 const struct ureg
*mat
,
383 if (dest
.file
!= PROGRAM_TEMPORARY
)
388 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, swizzle1(src
,X
), mat
[0]);
389 emit_op3(p
, VP_OPCODE_MAD
, tmp
, 0, swizzle1(src
,Y
), mat
[1], tmp
);
390 emit_op3(p
, VP_OPCODE_MAD
, tmp
, 0, swizzle1(src
,Z
), mat
[2], tmp
);
391 emit_op3(p
, VP_OPCODE_MAD
, dest
, 0, swizzle1(src
,W
), mat
[3], tmp
);
393 if (dest
.file
!= PROGRAM_TEMPORARY
)
394 release_temp(p
, tmp
);
397 static void emit_matrix_transform_vec3( struct tnl_program
*p
,
399 const struct ureg
*mat
,
402 emit_op2(p
, VP_OPCODE_DP3
, dest
, WRITEMASK_X
, src
, mat
[0]);
403 emit_op2(p
, VP_OPCODE_DP3
, dest
, WRITEMASK_Y
, src
, mat
[1]);
404 emit_op2(p
, VP_OPCODE_DP3
, dest
, WRITEMASK_Z
, src
, mat
[2]);
408 static void emit_normalize_vec3( struct tnl_program
*p
,
412 struct ureg tmp
= get_temp(p
);
413 emit_op2(p
, VP_OPCODE_DP3
, tmp
, 0, src
, src
);
414 emit_op1(p
, VP_OPCODE_RSQ
, tmp
, 0, tmp
);
415 emit_op2(p
, VP_OPCODE_MUL
, dest
, 0, src
, tmp
);
416 release_temp(p
, tmp
);
419 static struct ureg
get_eye_position( struct tnl_program
*p
)
421 if (is_undef(p
->eye_position
)) {
422 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
423 struct ureg modelview
[4];
425 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 0, 3,
426 STATE_MATRIX_TRANSPOSE
, modelview
);
427 p
->eye_position
= reserve_temp(p
);
429 emit_transpose_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
432 return p
->eye_position
;
436 static struct ureg
get_eye_position_normalized( struct tnl_program
*p
)
438 if (is_undef(p
->eye_position_normalized
)) {
439 struct ureg eye
= get_eye_position(p
);
440 p
->eye_position_normalized
= reserve_temp(p
);
441 emit_normalize_vec3(p
, p
->eye_position_normalized
, eye
);
444 return p
->eye_position_normalized
;
448 static struct ureg
get_eye_normal( struct tnl_program
*p
)
450 if (is_undef(p
->eye_normal
)) {
451 struct ureg normal
= register_input(p
, VERT_ATTRIB_NORMAL
);
452 struct ureg mvinv
[3];
454 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 0, 2,
455 STATE_MATRIX_INVTRANS
, mvinv
);
457 p
->eye_normal
= reserve_temp(p
);
459 /* Transform to eye space:
461 emit_matrix_transform_vec3( p
, p
->eye_normal
, mvinv
, normal
);
463 /* Normalize/Rescale:
465 if (p
->ctx
->Transform
.Normalize
) {
466 emit_normalize_vec3( p
, p
->eye_normal
, p
->eye_normal
);
468 else if (p
->ctx
->Transform
.RescaleNormals
) {
469 struct ureg rescale
= register_param2(p
, STATE_INTERNAL
,
472 emit_op2( p
, VP_OPCODE_MUL
, p
->eye_normal
, 0, normal
,
473 swizzle1(rescale
, X
));
477 return p
->eye_normal
;
482 static void build_hpos( struct tnl_program
*p
)
484 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
485 struct ureg hpos
= register_output( p
, VERT_RESULT_HPOS
);
488 register_matrix_param6( p
, STATE_MATRIX
, STATE_MVP
, 0, 0, 3,
489 STATE_MATRIX_TRANSPOSE
, mvp
);
490 emit_transpose_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
494 static GLuint
material_attrib( GLuint side
, GLuint property
)
496 return (_TNL_ATTRIB_MAT_FRONT_AMBIENT
+
497 (property
- STATE_AMBIENT
) * 2 +
501 static void set_material_flags( struct tnl_program
*p
)
503 GLcontext
*ctx
= p
->ctx
;
504 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
507 p
->color_materials
= 0;
510 if (ctx
->Light
.ColorMaterialEnabled
) {
513 ctx
->Light
.ColorMaterialBitmask
<< _TNL_ATTRIB_MAT_FRONT_AMBIENT
;
516 for (i
= _TNL_ATTRIB_MAT_FRONT_AMBIENT
; i
< _TNL_ATTRIB_INDEX
; i
++)
517 if (tnl
->vb
.AttribPtr
[i
]->stride
)
518 p
->materials
|= 1<<i
;
522 static struct ureg
get_material( struct tnl_program
*p
, GLuint side
,
525 GLuint attrib
= material_attrib(side
, property
);
527 if (p
->color_materials
& (1<<attrib
))
528 return register_input(p
, VERT_ATTRIB_COLOR0
);
529 else if (p
->materials
& (1<<attrib
))
530 return register_input( p
, attrib
);
532 return register_param3( p
, STATE_MATERIAL
, side
, property
);
535 #define SCENE_COLOR_BITS(side) (( _TNL_BIT_MAT_FRONT_EMISSION | \
536 _TNL_BIT_MAT_FRONT_AMBIENT | \
537 _TNL_BIT_MAT_FRONT_DIFFUSE) << (side))
539 /* Either return a precalculated constant value or emit code to
540 * calculate these values dynamically in the case where material calls
541 * are present between begin/end pairs.
543 * Probably want to shift this to the program compilation phase - if
544 * we always emitted the calculation here, a smart compiler could
545 * detect that it was constant (given a certain set of inputs), and
546 * lift it out of the main loop. That way the programs created here
547 * would be independent of the vertex_buffer details.
549 static struct ureg
get_scenecolor( struct tnl_program
*p
, GLuint side
)
551 if (p
->materials
& SCENE_COLOR_BITS(side
)) {
552 struct ureg lm_ambient
= register_param1(p
, STATE_LIGHTMODEL_AMBIENT
);
553 struct ureg material_emission
= get_material(p
, side
, STATE_EMISSION
);
554 struct ureg material_ambient
= get_material(p
, side
, STATE_AMBIENT
);
555 struct ureg material_diffuse
= get_material(p
, side
, STATE_DIFFUSE
);
556 struct ureg tmp
= make_temp(p
, material_diffuse
);
557 emit_op3(p
, VP_OPCODE_MAD
, tmp
, WRITEMASK_XYZ
, lm_ambient
,
558 material_ambient
, material_emission
);
562 return register_param2( p
, STATE_LIGHTMODEL_SCENECOLOR
, side
);
566 static struct ureg
get_lightprod( struct tnl_program
*p
, GLuint light
,
567 GLuint side
, GLuint property
)
569 GLuint attrib
= material_attrib(side
, property
);
570 if (p
->materials
& (1<<attrib
)) {
571 struct ureg light_value
=
572 register_param3(p
, STATE_LIGHT
, light
, property
);
573 struct ureg material_value
= get_material(p
, side
, property
);
574 struct ureg tmp
= get_temp(p
);
575 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, light_value
, material_value
);
579 return register_param4(p
, STATE_LIGHTPROD
, light
, side
, property
);
582 static struct ureg
calculate_light_attenuation( struct tnl_program
*p
,
584 struct gl_light
*light
,
588 struct ureg attenuation
= register_param3(p
, STATE_LIGHT
, i
,
590 struct ureg att
= get_temp(p
);
592 /* Calculate spot attenuation:
594 if (light
->SpotCutoff
!= 180.0F
) {
595 struct ureg spot_dir
= register_param3(p
, STATE_LIGHT
, i
,
596 STATE_SPOT_DIRECTION
);
597 struct ureg spot
= get_temp(p
);
598 struct ureg slt
= get_temp(p
);
600 emit_normalize_vec3( p
, spot
, spot_dir
); /* XXX: precompute! */
601 emit_op2(p
, VP_OPCODE_DP3
, spot
, 0, negate(VPpli
), spot_dir
);
602 emit_op2(p
, VP_OPCODE_SLT
, slt
, 0, swizzle1(spot_dir
,W
), spot
);
603 emit_op2(p
, VP_OPCODE_POW
, spot
, 0, spot
, swizzle1(attenuation
, W
));
604 emit_op2(p
, VP_OPCODE_MUL
, att
, 0, slt
, spot
);
606 release_temp(p
, spot
);
607 release_temp(p
, slt
);
610 /* Calculate distance attenuation:
612 if (light
->ConstantAttenuation
!= 1.0 ||
613 light
->LinearAttenuation
!= 1.0 ||
614 light
->QuadraticAttenuation
!= 1.0) {
617 emit_op1(p
, VP_OPCODE_RCP
, dist
, WRITEMASK_YZ
, dist
);
619 emit_op2(p
, VP_OPCODE_MUL
, dist
, WRITEMASK_XZ
, dist
, swizzle1(dist
,Y
));
621 emit_op2(p
, VP_OPCODE_DP3
, dist
, 0, attenuation
, dist
);
623 if (light
->SpotCutoff
!= 180.0F
) {
625 emit_op1(p
, VP_OPCODE_RCP
, dist
, 0, dist
);
626 /* spot-atten * dist-atten */
627 emit_op2(p
, VP_OPCODE_MUL
, att
, 0, dist
, att
);
630 emit_op1(p
, VP_OPCODE_RCP
, att
, 0, dist
);
641 /* Need to add some addtional parameters to allow lighting in object
642 * space - STATE_SPOT_DIRECTION and STATE_HALF implicitly assume eye
645 static void build_lighting( struct tnl_program
*p
)
647 GLcontext
*ctx
= p
->ctx
;
648 const GLboolean twoside
= ctx
->Light
.Model
.TwoSide
;
649 const GLboolean separate
= (ctx
->Light
.Model
.ColorControl
==
650 GL_SEPARATE_SPECULAR_COLOR
);
651 GLuint nr_lights
= 0, count
= 0;
652 struct ureg normal
= get_eye_normal(p
);
653 struct ureg lit
= get_temp(p
);
654 struct ureg dots
= get_temp(p
);
655 struct ureg _col0
= undef
, _col1
= undef
;
656 struct ureg _bfc0
= undef
, _bfc1
= undef
;
659 for (i
= 0; i
< MAX_LIGHTS
; i
++)
660 if (ctx
->Light
.Light
[i
].Enabled
)
663 set_material_flags(p
);
666 struct ureg shininess
= get_material(p
, 0, STATE_SHININESS
);
667 emit_op1(p
, VP_OPCODE_MOV
, dots
, WRITEMASK_W
, swizzle1(shininess
,X
));
668 release_temp(p
, shininess
);
670 _col0
= make_temp(p
, get_scenecolor(p
, 0));
672 _col1
= make_temp(p
, get_identity_param(p
));
679 struct ureg shininess
= get_material(p
, 1, STATE_SHININESS
);
680 emit_op1(p
, VP_OPCODE_MOV
, dots
, WRITEMASK_Z
,
681 negate(swizzle1(shininess
,X
)));
682 release_temp(p
, shininess
);
684 _bfc0
= make_temp(p
, get_scenecolor(p
, 1));
686 _bfc1
= make_temp(p
, get_identity_param(p
));
691 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
692 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
694 if (light
->Enabled
) {
695 struct ureg half
= undef
;
696 struct ureg att
= undef
, VPpli
= undef
;
700 if (light
->EyePosition
[3] == 0) {
701 /* Can used precomputed constants in this case.
702 * Attenuation never applies to infinite lights.
704 VPpli
= register_param3(p
, STATE_LIGHT
, i
,
705 STATE_POSITION_NORMALIZED
);
706 half
= register_param3(p
, STATE_LIGHT
, i
, STATE_HALF
);
709 struct ureg Ppli
= register_param3(p
, STATE_LIGHT
, i
,
711 struct ureg V
= get_eye_position(p
);
712 struct ureg dist
= get_temp(p
);
717 /* Calulate VPpli vector
719 emit_op2(p
, VP_OPCODE_SUB
, VPpli
, 0, Ppli
, V
);
721 /* Normalize VPpli. The dist value also used in
724 emit_op2(p
, VP_OPCODE_DP3
, dist
, 0, VPpli
, VPpli
);
725 emit_op1(p
, VP_OPCODE_RSQ
, dist
, 0, dist
);
726 emit_op2(p
, VP_OPCODE_MUL
, VPpli
, 0, VPpli
, dist
);
729 /* Calculate attenuation:
731 if (light
->SpotCutoff
!= 180.0 ||
732 light
->ConstantAttenuation
!= 1.0 ||
733 light
->LinearAttenuation
!= 1.0 ||
734 light
->QuadraticAttenuation
!= 1.0) {
735 att
= calculate_light_attenuation(p
, i
, light
, VPpli
, dist
);
739 /* Calculate viewer direction, or use infinite viewer:
741 if (ctx
->Light
.Model
.LocalViewer
) {
742 struct ureg eye_hat
= get_eye_position_normalized(p
);
743 emit_op2(p
, VP_OPCODE_SUB
, half
, 0, VPpli
, eye_hat
);
746 struct ureg z_dir
= swizzle(get_identity_param(p
),X
,Y
,W
,Z
);
747 emit_op2(p
, VP_OPCODE_ADD
, half
, 0, VPpli
, z_dir
);
750 emit_normalize_vec3(p
, half
, half
);
752 release_temp(p
, dist
);
755 /* Calculate dot products:
757 emit_op2(p
, VP_OPCODE_DP3
, dots
, WRITEMASK_X
, normal
, VPpli
);
758 emit_op2(p
, VP_OPCODE_DP3
, dots
, WRITEMASK_Y
, normal
, half
);
761 /* Front face lighting:
764 struct ureg ambient
= get_lightprod(p
, i
, 0, STATE_AMBIENT
);
765 struct ureg diffuse
= get_lightprod(p
, i
, 0, STATE_DIFFUSE
);
766 struct ureg specular
= get_lightprod(p
, i
, 0, STATE_SPECULAR
);
767 struct ureg res0
, res1
;
769 emit_op1(p
, VP_OPCODE_LIT
, lit
, 0, dots
);
772 emit_op2(p
, VP_OPCODE_MUL
, lit
, 0, lit
, att
);
775 if (count
== nr_lights
) {
777 res0
= register_output( p
, VERT_RESULT_COL0
);
778 res1
= register_output( p
, VERT_RESULT_COL1
);
782 res1
= register_output( p
, VERT_RESULT_COL0
);
789 emit_op3(p
, VP_OPCODE_MAD
, _col0
, 0, swizzle1(lit
,X
), ambient
, _col0
);
790 emit_op3(p
, VP_OPCODE_MAD
, res0
, 0, swizzle1(lit
,Y
), diffuse
, _col0
);
791 emit_op3(p
, VP_OPCODE_MAD
, res1
, 0, swizzle1(lit
,Z
), specular
, _col1
);
793 release_temp(p
, ambient
);
794 release_temp(p
, diffuse
);
795 release_temp(p
, specular
);
798 /* Back face lighting:
801 struct ureg ambient
= get_lightprod(p
, i
, 1, STATE_AMBIENT
);
802 struct ureg diffuse
= get_lightprod(p
, i
, 1, STATE_DIFFUSE
);
803 struct ureg specular
= get_lightprod(p
, i
, 1, STATE_SPECULAR
);
804 struct ureg res0
, res1
;
806 emit_op1(p
, VP_OPCODE_LIT
, lit
, 0, negate(swizzle(dots
,X
,Y
,W
,Z
)));
809 emit_op2(p
, VP_OPCODE_MUL
, lit
, 0, lit
, att
);
811 if (count
== nr_lights
) {
813 res0
= register_output( p
, VERT_RESULT_BFC0
);
814 res1
= register_output( p
, VERT_RESULT_BFC1
);
818 res1
= register_output( p
, VERT_RESULT_BFC0
);
826 emit_op3(p
, VP_OPCODE_MAD
, _bfc0
, 0, swizzle1(lit
,X
), ambient
, _bfc0
);
827 emit_op3(p
, VP_OPCODE_MAD
, res0
, 0, swizzle1(lit
,Y
), diffuse
, _bfc0
);
828 emit_op3(p
, VP_OPCODE_MAD
, res1
, 0, swizzle1(lit
,Z
), specular
, _bfc1
);
830 release_temp(p
, ambient
);
831 release_temp(p
, diffuse
);
832 release_temp(p
, specular
);
835 release_temp(p
, half
);
836 release_temp(p
, VPpli
);
837 release_temp(p
, att
);
845 static void build_fog( struct tnl_program
*p
)
847 GLcontext
*ctx
= p
->ctx
;
848 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
849 struct ureg fog
= register_output(p
, VERT_RESULT_FOGC
);
852 if (ctx
->Fog
.FogCoordinateSource
== GL_FRAGMENT_DEPTH_EXT
) {
853 input
= swizzle1(get_eye_position(p
), Z
);
856 input
= swizzle1(register_input(p
, VERT_ATTRIB_FOG
), X
);
859 if (tnl
->_DoVertexFog
) {
860 struct ureg params
= register_param1(p
, STATE_FOG_PARAMS
);
861 struct ureg tmp
= get_temp(p
);
863 switch (ctx
->Fog
.Mode
) {
865 struct ureg id
= get_identity_param(p
);
866 emit_op2(p
, VP_OPCODE_SUB
, tmp
, 0, swizzle1(params
,Z
), input
);
867 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, tmp
, swizzle1(params
,W
));
868 emit_op2(p
, VP_OPCODE_MAX
, tmp
, 0, tmp
, swizzle1(id
,X
)); /* saturate */
869 emit_op2(p
, VP_OPCODE_MIN
, fog
, WRITEMASK_X
, tmp
, swizzle1(id
,W
));
873 emit_op1(p
, VP_OPCODE_ABS
, tmp
, 0, input
);
874 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, tmp
, swizzle1(params
,X
));
875 emit_op2(p
, VP_OPCODE_POW
, fog
, WRITEMASK_X
,
876 register_const1f(p
, M_E
), negate(tmp
));
879 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, input
, swizzle1(params
,X
));
880 emit_op2(p
, VP_OPCODE_MUL
, tmp
, 0, tmp
, tmp
);
881 emit_op2(p
, VP_OPCODE_POW
, fog
, WRITEMASK_X
,
882 register_const1f(p
, M_E
), negate(tmp
));
886 release_temp(p
, tmp
);
889 /* results = incoming fog coords (compute fog per-fragment later)
891 * KW: Is it really necessary to do anything in this case?
893 emit_op1(p
, VP_OPCODE_MOV
, fog
, WRITEMASK_X
, input
);
897 static void build_reflect_texgen( struct tnl_program
*p
,
901 struct ureg normal
= get_eye_normal(p
);
902 struct ureg eye_hat
= get_eye_position_normalized(p
);
903 struct ureg tmp
= get_temp(p
);
906 emit_op2(p
, VP_OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
908 emit_op2(p
, VP_OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
910 emit_op3(p
, VP_OPCODE_MAD
, dest
, writemask
, negate(tmp
), normal
, eye_hat
);
913 static void build_sphere_texgen( struct tnl_program
*p
,
917 struct ureg normal
= get_eye_normal(p
);
918 struct ureg eye_hat
= get_eye_position_normalized(p
);
919 struct ureg tmp
= get_temp(p
);
920 struct ureg half
= register_const1f(p
, .5);
921 struct ureg r
= get_temp(p
);
922 struct ureg inv_m
= get_temp(p
);
923 struct ureg id
= get_identity_param(p
);
925 /* Could share the above calculations, but it would be
926 * a fairly odd state for someone to set (both sphere and
927 * reflection active for different texture coordinate
928 * components. Of course - if two texture units enable
929 * reflect and/or sphere, things start to tilt in favour
930 * of seperating this out:
934 emit_op2(p
, VP_OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
936 emit_op2(p
, VP_OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
938 emit_op3(p
, VP_OPCODE_MAD
, r
, 0, negate(tmp
), normal
, eye_hat
);
940 emit_op2(p
, VP_OPCODE_ADD
, tmp
, 0, r
, swizzle(id
,X
,Y
,W
,Z
));
941 /* rx^2 + ry^2 + (rz+1)^2 */
942 emit_op2(p
, VP_OPCODE_DP3
, tmp
, 0, tmp
, tmp
);
944 emit_op1(p
, VP_OPCODE_RSQ
, tmp
, 0, tmp
);
946 emit_op2(p
, VP_OPCODE_MUL
, inv_m
, 0, tmp
, swizzle1(half
,X
));
948 emit_op3(p
, VP_OPCODE_MAD
, dest
, writemask
, r
, inv_m
, swizzle1(half
,X
));
950 release_temp(p
, tmp
);
952 release_temp(p
, inv_m
);
956 static void build_texture_transform( struct tnl_program
*p
)
958 GLcontext
*ctx
= p
->ctx
;
961 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
962 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
963 GLuint texmat_enabled
= ctx
->Texture
._TexMatEnabled
& ENABLE_TEXMAT(i
);
964 struct ureg out
= register_output(p
, VERT_RESULT_TEX0
+ i
);
966 if (texUnit
->TexGenEnabled
|| texmat_enabled
) {
967 struct ureg out_texgen
= undef
;
969 if (texUnit
->TexGenEnabled
) {
970 GLuint copy_mask
= 0;
971 GLuint sphere_mask
= 0;
972 GLuint reflect_mask
= 0;
973 GLuint normal_mask
= 0;
977 out_texgen
= get_temp(p
);
981 modes
[0] = texUnit
->GenModeS
;
982 modes
[1] = texUnit
->GenModeT
;
983 modes
[2] = texUnit
->GenModeR
;
984 modes
[3] = texUnit
->GenModeQ
;
986 for (j
= 0; j
< 4; j
++) {
987 if (texUnit
->TexGenEnabled
& (1<<j
)) {
989 case GL_OBJECT_LINEAR
: {
990 struct ureg obj
= register_input(p
, VERT_ATTRIB_POS
);
992 register_param3(p
, STATE_TEXGEN
, i
,
993 STATE_TEXGEN_OBJECT_S
+ j
);
995 emit_op2(p
, VP_OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
999 case GL_EYE_LINEAR
: {
1000 struct ureg eye
= get_eye_position(p
);
1002 register_param3(p
, STATE_TEXGEN
, i
,
1003 STATE_TEXGEN_EYE_S
+ j
);
1005 emit_op2(p
, VP_OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1010 sphere_mask
|= WRITEMASK_X
<< j
;
1012 case GL_REFLECTION_MAP_NV
:
1013 reflect_mask
|= WRITEMASK_X
<< j
;
1015 case GL_NORMAL_MAP_NV
:
1016 normal_mask
|= WRITEMASK_X
<< j
;
1021 copy_mask
|= WRITEMASK_X
<< j
;
1026 build_sphere_texgen(p
, out_texgen
, sphere_mask
);
1030 build_reflect_texgen(p
, out_texgen
, reflect_mask
);
1034 struct ureg normal
= get_eye_normal(p
);
1035 emit_op1(p
, VP_OPCODE_MOV
, out_texgen
, normal_mask
, normal
);
1039 struct ureg in
= register_input(p
, VERT_ATTRIB_TEX0
+i
);
1040 emit_op1(p
, VP_OPCODE_MOV
, out_texgen
, copy_mask
, in
);
1044 if (texmat_enabled
) {
1045 struct ureg texmat
[4];
1046 struct ureg in
= (!is_undef(out_texgen
) ?
1048 register_input(p
, VERT_ATTRIB_TEX0
+i
));
1049 register_matrix_param6( p
, STATE_MATRIX
, STATE_TEXTURE
, i
,
1051 emit_matrix_transform_vec4( p
, out
, texmat
, in
);
1060 /* Seems like it could be tighter:
1062 static void build_pointsize( struct tnl_program
*p
)
1064 struct ureg eye
= get_eye_position(p
);
1065 struct ureg state_size
= register_param1(p
, STATE_POINT_SIZE
);
1066 struct ureg state_attenuation
= register_param1(p
, STATE_POINT_ATTENUATION
);
1067 struct ureg out
= register_output(p
, VERT_RESULT_PSIZ
);
1068 struct ureg ut
= get_temp(p
);
1070 /* 1, -Z, Z * Z, 1 */
1071 emit_op1(p
, VP_OPCODE_MOV
, ut
, 0, swizzle1(get_identity_param(p
), W
));
1072 emit_op2(p
, VP_OPCODE_MUL
, ut
, WRITEMASK_YZ
, ut
, negate(swizzle1(eye
, Z
)));
1073 emit_op2(p
, VP_OPCODE_MUL
, ut
, WRITEMASK_Z
, ut
, negate(swizzle1(eye
, Z
)));
1076 /* p1 + p2 * dist + p3 * dist * dist, 0 */
1077 emit_op2(p
, VP_OPCODE_DP3
, ut
, 0, ut
, state_attenuation
);
1080 emit_op1(p
, VP_OPCODE_RCP
, ut
, 0, ut
);
1082 /* out = pointSize / factor */
1083 emit_op2(p
, VP_OPCODE_MUL
, out
, WRITEMASK_X
, ut
, state_size
);
1085 release_temp(p
, ut
);
1089 static void build_passthrough( struct tnl_program
*p
, GLuint inputs
)
1095 void _tnl_UpdateFixedFunctionProgram( GLcontext
*ctx
)
1097 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
1098 struct tnl_program p
;
1099 GLuint db_NumInstructions
;
1100 struct vp_instruction
*db_Instructions
;
1102 if (ctx
->VertexProgram
._Enabled
)
1105 if (!ctx
->_TnlProgram
)
1106 ctx
->_TnlProgram
= (struct vertex_program
*)
1107 ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
1109 memset(&p
, 0, sizeof(p
));
1111 p
.program
= ctx
->_TnlProgram
;
1113 p
.eye_position
= undef
;
1114 p
.eye_position_normalized
= undef
;
1115 p
.eye_normal
= undef
;
1119 p
.temp_reserved
= ~((1<<MAX_NV_VERTEX_PROGRAM_TEMPS
)-1);
1121 db_Instructions
= p
.program
->Instructions
;
1122 db_NumInstructions
= p
.program
->Base
.NumInstructions
;
1124 p
.program
->Instructions
= MALLOC(sizeof(struct vp_instruction
) * 100);
1126 /* Initialize the arb_program struct */
1127 p
.program
->Base
.String
= 0;
1128 p
.program
->Base
.NumInstructions
=
1129 p
.program
->Base
.NumTemporaries
=
1130 p
.program
->Base
.NumParameters
=
1131 p
.program
->Base
.NumAttributes
= p
.program
->Base
.NumAddressRegs
= 0;
1133 if (p
.program
->Parameters
)
1134 _mesa_free_parameters(p
.program
->Parameters
);
1136 p
.program
->Parameters
= _mesa_new_parameter_list();
1138 p
.program
->InputsRead
= 0;
1139 p
.program
->OutputsWritten
= 0;
1141 /* Emit the program, starting with modelviewproject:
1145 /* Lighting calculations:
1147 if (ctx
->Light
.Enabled
)
1150 if (ctx
->Fog
.Enabled
)
1153 if (ctx
->Texture
._TexGenEnabled
|| ctx
->Texture
._TexMatEnabled
)
1154 build_texture_transform(&p
);
1156 if (ctx
->Point
._Attenuated
)
1157 build_pointsize(&p
);
1159 /* Is there a need to copy inputs to outputs? The software
1160 * implementation might do this more efficiently by just assigning
1161 * the missing results to point at input arrays.
1163 if (/* tnl->vp_copy_inputs && */
1164 (tnl
->render_inputs
& ~p
.program
->OutputsWritten
)) {
1165 build_passthrough(&p
, tnl
->render_inputs
);
1171 emit_op1(&p
, VP_OPCODE_END
, undef
, 0, undef
);
1176 _mesa_printf ("\n");
1180 /* Notify driver the fragment program has (actually) changed.
1182 if (db_Instructions
== NULL
||
1183 db_NumInstructions
!= p
.program
->Base
.NumInstructions
||
1184 memcmp(db_Instructions
, p
.program
->Instructions
,
1185 db_NumInstructions
* sizeof(*db_Instructions
)) != 0) {
1186 _mesa_printf("new program string\n");
1187 ctx
->Driver
.ProgramStringNotify( ctx
, GL_VERTEX_PROGRAM_ARB
,
1191 FREE(db_Instructions
);