1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 "program/program.h"
42 #include "program/prog_cache.h"
43 #include "program/prog_instruction.h"
44 #include "program/prog_parameter.h"
45 #include "program/prog_print.h"
46 #include "program/prog_statevars.h"
47 #include "util/bitscan.h"
50 /** Max of number of lights and texture coord units */
51 #define NUM_UNITS MAX2(MAX_TEXTURE_COORD_UNITS, MAX_LIGHTS)
54 GLbitfield varying_vp_inputs
;
56 unsigned fragprog_inputs_read
:12;
58 unsigned light_color_material_mask
:12;
59 unsigned light_global_enabled
:1;
60 unsigned light_local_viewer
:1;
61 unsigned light_twoside
:1;
62 unsigned material_shininess_is_zero
:1;
63 unsigned need_eye_coords
:1;
65 unsigned rescale_normals
:1;
67 unsigned fog_distance_mode
:2;
68 unsigned separate_specular
:1;
69 unsigned point_attenuated
:1;
72 unsigned char light_enabled
:1;
73 unsigned char light_eyepos3_is_zero
:1;
74 unsigned char light_spotcutoff_is_180
:1;
75 unsigned char light_attenuated
:1;
76 unsigned char texmat_enabled
:1;
77 unsigned char coord_replace
:1;
78 unsigned char texgen_enabled
:1;
79 unsigned char texgen_mode0
:4;
80 unsigned char texgen_mode1
:4;
81 unsigned char texgen_mode2
:4;
82 unsigned char 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
;
109 #define FDM_EYE_RADIAL 0
110 #define FDM_EYE_PLANE 1
111 #define FDM_EYE_PLANE_ABS 2
112 #define FDM_FROM_ARRAY 3
114 static GLuint
translate_fog_distance_mode(GLenum source
, GLenum mode
)
116 if (source
== GL_FRAGMENT_DEPTH_EXT
) {
118 case GL_EYE_RADIAL_NV
:
119 return FDM_EYE_RADIAL
;
121 return FDM_EYE_PLANE
;
122 default: /* shouldn't happen; fall through to a sensible default */
123 case GL_EYE_PLANE_ABSOLUTE_NV
:
124 return FDM_EYE_PLANE_ABS
;
127 return FDM_FROM_ARRAY
;
131 static GLboolean
check_active_shininess( struct gl_context
*ctx
,
132 const struct state_key
*key
,
135 GLuint attr
= MAT_ATTRIB_FRONT_SHININESS
+ side
;
137 if ((key
->varying_vp_inputs
& VERT_BIT_COLOR0
) &&
138 (key
->light_color_material_mask
& (1 << attr
)))
141 if (key
->varying_vp_inputs
& VERT_BIT_MAT(attr
))
144 if (ctx
->Light
.Material
.Attrib
[attr
][0] != 0.0F
)
151 static void make_state_key( struct gl_context
*ctx
, struct state_key
*key
)
153 const struct gl_program
*fp
= ctx
->FragmentProgram
._Current
;
156 memset(key
, 0, sizeof(struct state_key
));
158 /* This now relies on texenvprogram.c being active:
162 key
->need_eye_coords
= ctx
->_NeedEyeCoords
;
164 key
->fragprog_inputs_read
= fp
->info
.inputs_read
;
165 key
->varying_vp_inputs
= ctx
->varying_vp_inputs
;
167 if (ctx
->RenderMode
== GL_FEEDBACK
) {
168 /* make sure the vertprog emits color and tex0 */
169 key
->fragprog_inputs_read
|= (VARYING_BIT_COL0
| VARYING_BIT_TEX0
);
172 if (ctx
->Light
.Enabled
) {
173 key
->light_global_enabled
= 1;
175 if (ctx
->Light
.Model
.LocalViewer
)
176 key
->light_local_viewer
= 1;
178 if (ctx
->Light
.Model
.TwoSide
)
179 key
->light_twoside
= 1;
181 if (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)
182 key
->separate_specular
= 1;
184 if (ctx
->Light
.ColorMaterialEnabled
) {
185 key
->light_color_material_mask
= ctx
->Light
._ColorMaterialBitmask
;
188 mask
= ctx
->Light
._EnabledLights
;
190 const int i
= u_bit_scan(&mask
);
191 struct gl_light
*light
= &ctx
->Light
.Light
[i
];
193 key
->unit
[i
].light_enabled
= 1;
195 if (light
->EyePosition
[3] == 0.0F
)
196 key
->unit
[i
].light_eyepos3_is_zero
= 1;
198 if (light
->SpotCutoff
== 180.0F
)
199 key
->unit
[i
].light_spotcutoff_is_180
= 1;
201 if (light
->ConstantAttenuation
!= 1.0F
||
202 light
->LinearAttenuation
!= 0.0F
||
203 light
->QuadraticAttenuation
!= 0.0F
)
204 key
->unit
[i
].light_attenuated
= 1;
207 if (check_active_shininess(ctx
, key
, 0)) {
208 key
->material_shininess_is_zero
= 0;
210 else if (key
->light_twoside
&&
211 check_active_shininess(ctx
, key
, 1)) {
212 key
->material_shininess_is_zero
= 0;
215 key
->material_shininess_is_zero
= 1;
219 if (ctx
->Transform
.Normalize
)
222 if (ctx
->Transform
.RescaleNormals
)
223 key
->rescale_normals
= 1;
225 /* Only distinguish fog parameters if we actually need */
226 if (key
->fragprog_inputs_read
& VARYING_BIT_FOGC
)
227 key
->fog_distance_mode
=
228 translate_fog_distance_mode(ctx
->Fog
.FogCoordinateSource
,
229 ctx
->Fog
.FogDistanceMode
);
231 if (ctx
->Point
._Attenuated
)
232 key
->point_attenuated
= 1;
234 mask
= ctx
->Texture
._EnabledCoordUnits
| ctx
->Texture
._TexGenEnabled
235 | ctx
->Texture
._TexMatEnabled
| ctx
->Point
.CoordReplace
;
237 const int i
= u_bit_scan(&mask
);
238 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
240 if (ctx
->Point
.PointSprite
)
241 if (ctx
->Point
.CoordReplace
& (1u << i
))
242 key
->unit
[i
].coord_replace
= 1;
244 if (ctx
->Texture
._TexMatEnabled
& ENABLE_TEXMAT(i
))
245 key
->unit
[i
].texmat_enabled
= 1;
247 if (texUnit
->TexGenEnabled
) {
248 key
->unit
[i
].texgen_enabled
= 1;
250 key
->unit
[i
].texgen_mode0
=
251 translate_texgen( texUnit
->TexGenEnabled
& (1<<0),
252 texUnit
->GenS
.Mode
);
253 key
->unit
[i
].texgen_mode1
=
254 translate_texgen( texUnit
->TexGenEnabled
& (1<<1),
255 texUnit
->GenT
.Mode
);
256 key
->unit
[i
].texgen_mode2
=
257 translate_texgen( texUnit
->TexGenEnabled
& (1<<2),
258 texUnit
->GenR
.Mode
);
259 key
->unit
[i
].texgen_mode3
=
260 translate_texgen( texUnit
->TexGenEnabled
& (1<<3),
261 texUnit
->GenQ
.Mode
);
268 /* Very useful debugging tool - produces annotated listing of
269 * generated program with line/function references for each
270 * instruction back into this file:
275 /* Use uregs to represent registers internally, translate to Mesa's
276 * expected formats on emit.
278 * NOTE: These are passed by value extensively in this file rather
279 * than as usual by pointer reference. If this disturbs you, try
280 * remembering they are just 32bits in size.
282 * GCC is smart enough to deal with these dword-sized structures in
283 * much the same way as if I had defined them as dwords and was using
284 * macros to access and set the fields. This is much nicer and easier
289 GLint idx
:9; /* relative addressing may be negative */
290 /* sizeof(idx) should == sizeof(prog_src_reg::Index) */
298 const struct state_key
*state
;
299 struct gl_program
*program
;
300 GLuint max_inst
; /** number of instructions allocated for program */
301 GLboolean mvp_with_dp4
;
304 GLuint temp_reserved
;
306 struct ureg eye_position
;
307 struct ureg eye_position_z
;
308 struct ureg eye_position_normalized
;
309 struct ureg transformed_normal
;
310 struct ureg identity
;
313 GLuint color_materials
;
317 static const struct ureg undef
= {
335 static struct ureg
make_ureg(GLuint file
, GLint idx
)
341 reg
.swz
= SWIZZLE_NOOP
;
347 static struct ureg
negate( struct ureg reg
)
354 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
356 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
359 GET_SWZ(reg
.swz
, w
));
364 static struct ureg
swizzle1( struct ureg reg
, int x
)
366 return swizzle(reg
, x
, x
, x
, x
);
370 static struct ureg
get_temp( struct tnl_program
*p
)
372 int bit
= ffs( ~p
->temp_in_use
);
374 _mesa_problem(NULL
, "%s: out of temporaries\n", __FILE__
);
378 if ((GLuint
) bit
> p
->program
->arb
.NumTemporaries
)
379 p
->program
->arb
.NumTemporaries
= bit
;
381 p
->temp_in_use
|= 1<<(bit
-1);
382 return make_ureg(PROGRAM_TEMPORARY
, bit
-1);
386 static struct ureg
reserve_temp( struct tnl_program
*p
)
388 struct ureg temp
= get_temp( p
);
389 p
->temp_reserved
|= 1<<temp
.idx
;
394 static void release_temp( struct tnl_program
*p
, struct ureg reg
)
396 if (reg
.file
== PROGRAM_TEMPORARY
) {
397 p
->temp_in_use
&= ~(1<<reg
.idx
);
398 p
->temp_in_use
|= p
->temp_reserved
; /* can't release reserved temps */
402 static void release_temps( struct tnl_program
*p
)
404 p
->temp_in_use
= p
->temp_reserved
;
408 static struct ureg
register_param5(struct tnl_program
*p
,
415 gl_state_index tokens
[STATE_LENGTH
];
422 idx
= _mesa_add_state_reference(p
->program
->Parameters
, tokens
);
423 return make_ureg(PROGRAM_STATE_VAR
, idx
);
427 #define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
428 #define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
429 #define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
430 #define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
435 * \param input one of VERT_ATTRIB_x tokens.
437 static struct ureg
register_input( struct tnl_program
*p
, GLuint input
)
439 assert(input
< VERT_ATTRIB_MAX
);
441 if (p
->state
->varying_vp_inputs
& VERT_BIT(input
)) {
442 p
->program
->info
.inputs_read
|= VERT_BIT(input
);
443 return make_ureg(PROGRAM_INPUT
, input
);
446 return register_param3( p
, STATE_INTERNAL
, STATE_CURRENT_ATTRIB
, input
);
452 * \param input one of VARYING_SLOT_x tokens.
454 static struct ureg
register_output( struct tnl_program
*p
, GLuint output
)
456 p
->program
->info
.outputs_written
|= BITFIELD64_BIT(output
);
457 return make_ureg(PROGRAM_OUTPUT
, output
);
461 static struct ureg
register_const4f( struct tnl_program
*p
,
467 gl_constant_value values
[4];
474 idx
= _mesa_add_unnamed_constant(p
->program
->Parameters
, values
, 4,
476 assert(swizzle
== SWIZZLE_NOOP
);
477 return make_ureg(PROGRAM_CONSTANT
, idx
);
480 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
481 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
482 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
483 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
485 static GLboolean
is_undef( struct ureg reg
)
487 return reg
.file
== PROGRAM_UNDEFINED
;
491 static struct ureg
get_identity_param( struct tnl_program
*p
)
493 if (is_undef(p
->identity
))
494 p
->identity
= register_const4f(p
, 0,0,0,1);
499 static void register_matrix_param5( struct tnl_program
*p
,
500 GLint s0
, /* modelview, projection, etc */
501 GLint s1
, /* texture matrix number */
502 GLint s2
, /* first row */
503 GLint s3
, /* last row */
504 GLint s4
, /* inverse, transpose, etc */
505 struct ureg
*matrix
)
509 /* This is a bit sad as the support is there to pull the whole
510 * matrix out in one go:
512 for (i
= 0; i
<= s3
- s2
; i
++)
513 matrix
[i
] = register_param5( p
, s0
, s1
, i
, i
, s4
);
517 static void emit_arg( struct prog_src_register
*src
,
520 src
->File
= reg
.file
;
521 src
->Index
= reg
.idx
;
522 src
->Swizzle
= reg
.swz
;
523 src
->Negate
= reg
.negate
? NEGATE_XYZW
: NEGATE_NONE
;
525 /* Check that bitfield sizes aren't exceeded */
526 assert(src
->Index
== reg
.idx
);
530 static void emit_dst( struct prog_dst_register
*dst
,
531 struct ureg reg
, GLuint mask
)
533 dst
->File
= reg
.file
;
534 dst
->Index
= reg
.idx
;
535 /* allow zero as a shorthand for xyzw */
536 dst
->WriteMask
= mask
? mask
: WRITEMASK_XYZW
;
537 /* Check that bitfield sizes aren't exceeded */
538 assert(dst
->Index
== reg
.idx
);
542 static void debug_insn( struct prog_instruction
*inst
, const char *fn
,
546 static const char *last_fn
;
553 printf("%d:\t", line
);
554 _mesa_print_instruction(inst
);
559 static void emit_op3fn(struct tnl_program
*p
,
570 struct prog_instruction
*inst
;
572 assert(p
->program
->arb
.NumInstructions
<= p
->max_inst
);
574 if (p
->program
->arb
.NumInstructions
== p
->max_inst
) {
575 /* need to extend the program's instruction array */
576 struct prog_instruction
*newInst
;
578 /* double the size */
582 rzalloc_array(p
->program
, struct prog_instruction
, p
->max_inst
);
584 _mesa_error(NULL
, GL_OUT_OF_MEMORY
, "vertex program build");
588 _mesa_copy_instructions(newInst
, p
->program
->arb
.Instructions
,
589 p
->program
->arb
.NumInstructions
);
591 ralloc_free(p
->program
->arb
.Instructions
);
593 p
->program
->arb
.Instructions
= newInst
;
596 nr
= p
->program
->arb
.NumInstructions
++;
598 inst
= &p
->program
->arb
.Instructions
[nr
];
599 inst
->Opcode
= (enum prog_opcode
) op
;
601 emit_arg( &inst
->SrcReg
[0], src0
);
602 emit_arg( &inst
->SrcReg
[1], src1
);
603 emit_arg( &inst
->SrcReg
[2], src2
);
605 emit_dst( &inst
->DstReg
, dest
, mask
);
607 debug_insn(inst
, fn
, line
);
611 #define emit_op3(p, op, dst, mask, src0, src1, src2) \
612 emit_op3fn(p, op, dst, mask, src0, src1, src2, __func__, __LINE__)
614 #define emit_op2(p, op, dst, mask, src0, src1) \
615 emit_op3fn(p, op, dst, mask, src0, src1, undef, __func__, __LINE__)
617 #define emit_op1(p, op, dst, mask, src0) \
618 emit_op3fn(p, op, dst, mask, src0, undef, undef, __func__, __LINE__)
621 static struct ureg
make_temp( struct tnl_program
*p
, struct ureg reg
)
623 if (reg
.file
== PROGRAM_TEMPORARY
&&
624 !(p
->temp_reserved
& (1<<reg
.idx
)))
627 struct ureg temp
= get_temp(p
);
628 emit_op1(p
, OPCODE_MOV
, temp
, 0, reg
);
634 /* Currently no tracking performed of input/output/register size or
635 * active elements. Could be used to reduce these operations, as
636 * could the matrix type.
638 static void emit_matrix_transform_vec4( struct tnl_program
*p
,
640 const struct ureg
*mat
,
643 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_X
, src
, mat
[0]);
644 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_Y
, src
, mat
[1]);
645 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_Z
, src
, mat
[2]);
646 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_W
, src
, mat
[3]);
650 /* This version is much easier to implement if writemasks are not
651 * supported natively on the target or (like SSE), the target doesn't
652 * have a clean/obvious dotproduct implementation.
654 static void emit_transpose_matrix_transform_vec4( struct tnl_program
*p
,
656 const struct ureg
*mat
,
661 if (dest
.file
!= PROGRAM_TEMPORARY
)
666 emit_op2(p
, OPCODE_MUL
, tmp
, 0, swizzle1(src
,X
), mat
[0]);
667 emit_op3(p
, OPCODE_MAD
, tmp
, 0, swizzle1(src
,Y
), mat
[1], tmp
);
668 emit_op3(p
, OPCODE_MAD
, tmp
, 0, swizzle1(src
,Z
), mat
[2], tmp
);
669 emit_op3(p
, OPCODE_MAD
, dest
, 0, swizzle1(src
,W
), mat
[3], tmp
);
671 if (dest
.file
!= PROGRAM_TEMPORARY
)
672 release_temp(p
, tmp
);
676 static void emit_matrix_transform_vec3( struct tnl_program
*p
,
678 const struct ureg
*mat
,
681 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_X
, src
, mat
[0]);
682 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_Y
, src
, mat
[1]);
683 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_Z
, src
, mat
[2]);
687 static void emit_normalize_vec3( struct tnl_program
*p
,
691 struct ureg tmp
= get_temp(p
);
692 emit_op2(p
, OPCODE_DP3
, tmp
, WRITEMASK_X
, src
, src
);
693 emit_op1(p
, OPCODE_RSQ
, tmp
, WRITEMASK_X
, tmp
);
694 emit_op2(p
, OPCODE_MUL
, dest
, 0, src
, swizzle1(tmp
, X
));
695 release_temp(p
, tmp
);
699 static void emit_passthrough( struct tnl_program
*p
,
703 struct ureg out
= register_output(p
, output
);
704 emit_op1(p
, OPCODE_MOV
, out
, 0, register_input(p
, input
));
708 static struct ureg
get_eye_position( struct tnl_program
*p
)
710 if (is_undef(p
->eye_position
)) {
711 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
712 struct ureg modelview
[4];
714 p
->eye_position
= reserve_temp(p
);
716 if (p
->mvp_with_dp4
) {
717 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 3,
720 emit_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
723 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 3,
724 STATE_MATRIX_TRANSPOSE
, modelview
);
726 emit_transpose_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
730 return p
->eye_position
;
734 static struct ureg
get_eye_position_z( struct tnl_program
*p
)
736 if (!is_undef(p
->eye_position
))
737 return swizzle1(p
->eye_position
, Z
);
739 if (is_undef(p
->eye_position_z
)) {
740 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
741 struct ureg modelview
[4];
743 p
->eye_position_z
= reserve_temp(p
);
745 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 3,
748 emit_op2(p
, OPCODE_DP4
, p
->eye_position_z
, 0, pos
, modelview
[2]);
751 return p
->eye_position_z
;
755 static struct ureg
get_eye_position_normalized( struct tnl_program
*p
)
757 if (is_undef(p
->eye_position_normalized
)) {
758 struct ureg eye
= get_eye_position(p
);
759 p
->eye_position_normalized
= reserve_temp(p
);
760 emit_normalize_vec3(p
, p
->eye_position_normalized
, eye
);
763 return p
->eye_position_normalized
;
767 static struct ureg
get_transformed_normal( struct tnl_program
*p
)
769 if (is_undef(p
->transformed_normal
) &&
770 !p
->state
->need_eye_coords
&&
771 !p
->state
->normalize
&&
772 !(p
->state
->need_eye_coords
== p
->state
->rescale_normals
))
774 p
->transformed_normal
= register_input(p
, VERT_ATTRIB_NORMAL
);
776 else if (is_undef(p
->transformed_normal
))
778 struct ureg normal
= register_input(p
, VERT_ATTRIB_NORMAL
);
779 struct ureg mvinv
[3];
780 struct ureg transformed_normal
= reserve_temp(p
);
782 if (p
->state
->need_eye_coords
) {
783 register_matrix_param5( p
, STATE_MODELVIEW_MATRIX
, 0, 0, 2,
784 STATE_MATRIX_INVTRANS
, mvinv
);
786 /* Transform to eye space:
788 emit_matrix_transform_vec3( p
, transformed_normal
, mvinv
, normal
);
789 normal
= transformed_normal
;
792 /* Normalize/Rescale:
794 if (p
->state
->normalize
) {
795 emit_normalize_vec3( p
, transformed_normal
, normal
);
796 normal
= transformed_normal
;
798 else if (p
->state
->need_eye_coords
== p
->state
->rescale_normals
) {
799 /* This is already adjusted for eye/non-eye rendering:
801 struct ureg rescale
= register_param2(p
, STATE_INTERNAL
,
804 emit_op2( p
, OPCODE_MUL
, transformed_normal
, 0, normal
, rescale
);
805 normal
= transformed_normal
;
808 assert(normal
.file
== PROGRAM_TEMPORARY
);
809 p
->transformed_normal
= normal
;
812 return p
->transformed_normal
;
816 static void build_hpos( struct tnl_program
*p
)
818 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
819 struct ureg hpos
= register_output( p
, VARYING_SLOT_POS
);
822 if (p
->mvp_with_dp4
) {
823 register_matrix_param5( p
, STATE_MVP_MATRIX
, 0, 0, 3,
825 emit_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
828 register_matrix_param5( p
, STATE_MVP_MATRIX
, 0, 0, 3,
829 STATE_MATRIX_TRANSPOSE
, mvp
);
830 emit_transpose_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
835 static GLuint
material_attrib( GLuint side
, GLuint property
)
837 return (property
- STATE_AMBIENT
) * 2 + side
;
842 * Get a bitmask of which material values vary on a per-vertex basis.
844 static void set_material_flags( struct tnl_program
*p
)
846 p
->color_materials
= 0;
849 if (p
->state
->varying_vp_inputs
& VERT_BIT_COLOR0
) {
851 p
->color_materials
= p
->state
->light_color_material_mask
;
854 p
->materials
|= ((p
->state
->varying_vp_inputs
& VERT_BIT_MAT_ALL
)
855 >> VERT_ATTRIB_MAT(0));
859 static struct ureg
get_material( struct tnl_program
*p
, GLuint side
,
862 GLuint attrib
= material_attrib(side
, property
);
864 if (p
->color_materials
& (1<<attrib
))
865 return register_input(p
, VERT_ATTRIB_COLOR0
);
866 else if (p
->materials
& (1<<attrib
)) {
867 /* Put material values in the GENERIC slots -- they are not used
868 * for anything in fixed function mode.
870 return register_input( p
, VERT_ATTRIB_MAT(attrib
) );
873 return register_param3( p
, STATE_MATERIAL
, side
, property
);
876 #define SCENE_COLOR_BITS(side) (( MAT_BIT_FRONT_EMISSION | \
877 MAT_BIT_FRONT_AMBIENT | \
878 MAT_BIT_FRONT_DIFFUSE) << (side))
882 * Either return a precalculated constant value or emit code to
883 * calculate these values dynamically in the case where material calls
884 * are present between begin/end pairs.
886 * Probably want to shift this to the program compilation phase - if
887 * we always emitted the calculation here, a smart compiler could
888 * detect that it was constant (given a certain set of inputs), and
889 * lift it out of the main loop. That way the programs created here
890 * would be independent of the vertex_buffer details.
892 static struct ureg
get_scenecolor( struct tnl_program
*p
, GLuint side
)
894 if (p
->materials
& SCENE_COLOR_BITS(side
)) {
895 struct ureg lm_ambient
= register_param1(p
, STATE_LIGHTMODEL_AMBIENT
);
896 struct ureg material_emission
= get_material(p
, side
, STATE_EMISSION
);
897 struct ureg material_ambient
= get_material(p
, side
, STATE_AMBIENT
);
898 struct ureg material_diffuse
= get_material(p
, side
, STATE_DIFFUSE
);
899 struct ureg tmp
= make_temp(p
, material_diffuse
);
900 emit_op3(p
, OPCODE_MAD
, tmp
, WRITEMASK_XYZ
, lm_ambient
,
901 material_ambient
, material_emission
);
905 return register_param2( p
, STATE_LIGHTMODEL_SCENECOLOR
, side
);
909 static struct ureg
get_lightprod( struct tnl_program
*p
, GLuint light
,
910 GLuint side
, GLuint property
)
912 GLuint attrib
= material_attrib(side
, property
);
913 if (p
->materials
& (1<<attrib
)) {
914 struct ureg light_value
=
915 register_param3(p
, STATE_LIGHT
, light
, property
);
916 struct ureg material_value
= get_material(p
, side
, property
);
917 struct ureg tmp
= get_temp(p
);
918 emit_op2(p
, OPCODE_MUL
, tmp
, 0, light_value
, material_value
);
922 return register_param4(p
, STATE_LIGHTPROD
, light
, side
, property
);
926 static struct ureg
calculate_light_attenuation( struct tnl_program
*p
,
931 struct ureg attenuation
= register_param3(p
, STATE_LIGHT
, i
,
933 struct ureg att
= undef
;
935 /* Calculate spot attenuation:
937 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
938 struct ureg spot_dir_norm
= register_param3(p
, STATE_INTERNAL
,
939 STATE_LIGHT_SPOT_DIR_NORMALIZED
, i
);
940 struct ureg spot
= get_temp(p
);
941 struct ureg slt
= get_temp(p
);
945 emit_op2(p
, OPCODE_DP3
, spot
, 0, negate(VPpli
), spot_dir_norm
);
946 emit_op2(p
, OPCODE_SLT
, slt
, 0, swizzle1(spot_dir_norm
,W
), spot
);
947 emit_op1(p
, OPCODE_ABS
, spot
, 0, spot
);
948 emit_op2(p
, OPCODE_POW
, spot
, 0, spot
, swizzle1(attenuation
, W
));
949 emit_op2(p
, OPCODE_MUL
, att
, 0, slt
, spot
);
951 release_temp(p
, spot
);
952 release_temp(p
, slt
);
955 /* Calculate distance attenuation(See formula (2.4) at glspec 2.1 page 62):
957 * Skip the calucation when _dist_ is undefined(light_eyepos3_is_zero)
959 if (p
->state
->unit
[i
].light_attenuated
&& !is_undef(dist
)) {
963 emit_op1(p
, OPCODE_RCP
, dist
, WRITEMASK_YZ
, dist
);
965 emit_op2(p
, OPCODE_MUL
, dist
, WRITEMASK_XZ
, dist
, swizzle1(dist
,Y
));
967 emit_op2(p
, OPCODE_DP3
, dist
, 0, attenuation
, dist
);
969 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
971 emit_op1(p
, OPCODE_RCP
, dist
, 0, dist
);
972 /* spot-atten * dist-atten */
973 emit_op2(p
, OPCODE_MUL
, att
, 0, dist
, att
);
977 emit_op1(p
, OPCODE_RCP
, att
, 0, dist
);
987 * lit.y = MAX(0, dots.x)
988 * lit.z = SLT(0, dots.x)
990 static void emit_degenerate_lit( struct tnl_program
*p
,
994 struct ureg id
= get_identity_param(p
); /* id = {0,0,0,1} */
996 /* Note that lit.x & lit.w will not be examined. Note also that
997 * dots.xyzw == dots.xxxx.
1000 /* MAX lit, id, dots;
1002 emit_op2(p
, OPCODE_MAX
, lit
, WRITEMASK_XYZW
, id
, dots
);
1004 /* result[2] = (in > 0 ? 1 : 0)
1005 * SLT lit.z, id.z, dots; # lit.z = (0 < dots.z) ? 1 : 0
1007 emit_op2(p
, OPCODE_SLT
, lit
, WRITEMASK_Z
, swizzle1(id
,Z
), dots
);
1011 /* Need to add some addtional parameters to allow lighting in object
1012 * space - STATE_SPOT_DIRECTION and STATE_HALF_VECTOR implicitly assume eye
1015 static void build_lighting( struct tnl_program
*p
)
1017 const GLboolean twoside
= p
->state
->light_twoside
;
1018 const GLboolean separate
= p
->state
->separate_specular
;
1019 GLuint nr_lights
= 0, count
= 0;
1020 struct ureg normal
= get_transformed_normal(p
);
1021 struct ureg lit
= get_temp(p
);
1022 struct ureg dots
= get_temp(p
);
1023 struct ureg _col0
= undef
, _col1
= undef
;
1024 struct ureg _bfc0
= undef
, _bfc1
= undef
;
1029 * dots.x = dot(normal, VPpli)
1030 * dots.y = dot(normal, halfAngle)
1031 * dots.z = back.shininess
1032 * dots.w = front.shininess
1035 for (i
= 0; i
< MAX_LIGHTS
; i
++)
1036 if (p
->state
->unit
[i
].light_enabled
)
1039 set_material_flags(p
);
1042 if (!p
->state
->material_shininess_is_zero
) {
1043 struct ureg shininess
= get_material(p
, 0, STATE_SHININESS
);
1044 emit_op1(p
, OPCODE_MOV
, dots
, WRITEMASK_W
, swizzle1(shininess
,X
));
1045 release_temp(p
, shininess
);
1048 _col0
= make_temp(p
, get_scenecolor(p
, 0));
1050 _col1
= make_temp(p
, get_identity_param(p
));
1056 if (!p
->state
->material_shininess_is_zero
) {
1057 /* Note that we negate the back-face specular exponent here.
1058 * The negation will be un-done later in the back-face code below.
1060 struct ureg shininess
= get_material(p
, 1, STATE_SHININESS
);
1061 emit_op1(p
, OPCODE_MOV
, dots
, WRITEMASK_Z
,
1062 negate(swizzle1(shininess
,X
)));
1063 release_temp(p
, shininess
);
1066 _bfc0
= make_temp(p
, get_scenecolor(p
, 1));
1068 _bfc1
= make_temp(p
, get_identity_param(p
));
1073 /* If no lights, still need to emit the scenecolor.
1076 struct ureg res0
= register_output( p
, VARYING_SLOT_COL0
);
1077 emit_op1(p
, OPCODE_MOV
, res0
, 0, _col0
);
1081 struct ureg res1
= register_output( p
, VARYING_SLOT_COL1
);
1082 emit_op1(p
, OPCODE_MOV
, res1
, 0, _col1
);
1086 struct ureg res0
= register_output( p
, VARYING_SLOT_BFC0
);
1087 emit_op1(p
, OPCODE_MOV
, res0
, 0, _bfc0
);
1090 if (twoside
&& separate
) {
1091 struct ureg res1
= register_output( p
, VARYING_SLOT_BFC1
);
1092 emit_op1(p
, OPCODE_MOV
, res1
, 0, _bfc1
);
1095 if (nr_lights
== 0) {
1100 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
1101 if (p
->state
->unit
[i
].light_enabled
) {
1102 struct ureg half
= undef
;
1103 struct ureg att
= undef
, VPpli
= undef
;
1104 struct ureg dist
= undef
;
1107 if (p
->state
->unit
[i
].light_eyepos3_is_zero
) {
1108 VPpli
= register_param3(p
, STATE_INTERNAL
,
1109 STATE_LIGHT_POSITION_NORMALIZED
, i
);
1111 struct ureg Ppli
= register_param3(p
, STATE_INTERNAL
,
1112 STATE_LIGHT_POSITION
, i
);
1113 struct ureg V
= get_eye_position(p
);
1115 VPpli
= get_temp(p
);
1118 /* Calculate VPpli vector
1120 emit_op2(p
, OPCODE_SUB
, VPpli
, 0, Ppli
, V
);
1122 /* Normalize VPpli. The dist value also used in
1123 * attenuation below.
1125 emit_op2(p
, OPCODE_DP3
, dist
, 0, VPpli
, VPpli
);
1126 emit_op1(p
, OPCODE_RSQ
, dist
, 0, dist
);
1127 emit_op2(p
, OPCODE_MUL
, VPpli
, 0, VPpli
, dist
);
1130 /* Calculate attenuation:
1132 att
= calculate_light_attenuation(p
, i
, VPpli
, dist
);
1133 release_temp(p
, dist
);
1135 /* Calculate viewer direction, or use infinite viewer:
1137 if (!p
->state
->material_shininess_is_zero
) {
1138 if (p
->state
->light_local_viewer
) {
1139 struct ureg eye_hat
= get_eye_position_normalized(p
);
1141 emit_op2(p
, OPCODE_SUB
, half
, 0, VPpli
, eye_hat
);
1142 emit_normalize_vec3(p
, half
, half
);
1143 } else if (p
->state
->unit
[i
].light_eyepos3_is_zero
) {
1144 half
= register_param3(p
, STATE_INTERNAL
,
1145 STATE_LIGHT_HALF_VECTOR
, i
);
1147 struct ureg z_dir
= swizzle(get_identity_param(p
),X
,Y
,W
,Z
);
1149 emit_op2(p
, OPCODE_ADD
, half
, 0, VPpli
, z_dir
);
1150 emit_normalize_vec3(p
, half
, half
);
1154 /* Calculate dot products:
1156 if (p
->state
->material_shininess_is_zero
) {
1157 emit_op2(p
, OPCODE_DP3
, dots
, 0, normal
, VPpli
);
1160 emit_op2(p
, OPCODE_DP3
, dots
, WRITEMASK_X
, normal
, VPpli
);
1161 emit_op2(p
, OPCODE_DP3
, dots
, WRITEMASK_Y
, normal
, half
);
1164 /* Front face lighting:
1167 struct ureg ambient
= get_lightprod(p
, i
, 0, STATE_AMBIENT
);
1168 struct ureg diffuse
= get_lightprod(p
, i
, 0, STATE_DIFFUSE
);
1169 struct ureg specular
= get_lightprod(p
, i
, 0, STATE_SPECULAR
);
1170 struct ureg res0
, res1
;
1171 GLuint mask0
, mask1
;
1173 if (count
== nr_lights
) {
1175 mask0
= WRITEMASK_XYZ
;
1176 mask1
= WRITEMASK_XYZ
;
1177 res0
= register_output( p
, VARYING_SLOT_COL0
);
1178 res1
= register_output( p
, VARYING_SLOT_COL1
);
1182 mask1
= WRITEMASK_XYZ
;
1184 res1
= register_output( p
, VARYING_SLOT_COL0
);
1194 if (!is_undef(att
)) {
1195 /* light is attenuated by distance */
1196 emit_op1(p
, OPCODE_LIT
, lit
, 0, dots
);
1197 emit_op2(p
, OPCODE_MUL
, lit
, 0, lit
, att
);
1198 emit_op3(p
, OPCODE_MAD
, _col0
, 0, swizzle1(lit
,X
), ambient
, _col0
);
1200 else if (!p
->state
->material_shininess_is_zero
) {
1201 /* there's a non-zero specular term */
1202 emit_op1(p
, OPCODE_LIT
, lit
, 0, dots
);
1203 emit_op2(p
, OPCODE_ADD
, _col0
, 0, ambient
, _col0
);
1206 /* no attenutation, no specular */
1207 emit_degenerate_lit(p
, lit
, dots
);
1208 emit_op2(p
, OPCODE_ADD
, _col0
, 0, ambient
, _col0
);
1211 emit_op3(p
, OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _col0
);
1212 emit_op3(p
, OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _col1
);
1214 release_temp(p
, ambient
);
1215 release_temp(p
, diffuse
);
1216 release_temp(p
, specular
);
1219 /* Back face lighting:
1222 struct ureg ambient
= get_lightprod(p
, i
, 1, STATE_AMBIENT
);
1223 struct ureg diffuse
= get_lightprod(p
, i
, 1, STATE_DIFFUSE
);
1224 struct ureg specular
= get_lightprod(p
, i
, 1, STATE_SPECULAR
);
1225 struct ureg res0
, res1
;
1226 GLuint mask0
, mask1
;
1228 if (count
== nr_lights
) {
1230 mask0
= WRITEMASK_XYZ
;
1231 mask1
= WRITEMASK_XYZ
;
1232 res0
= register_output( p
, VARYING_SLOT_BFC0
);
1233 res1
= register_output( p
, VARYING_SLOT_BFC1
);
1237 mask1
= WRITEMASK_XYZ
;
1239 res1
= register_output( p
, VARYING_SLOT_BFC0
);
1249 /* For the back face we need to negate the X and Y component
1250 * dot products. dots.Z has the negated back-face specular
1251 * exponent. We swizzle that into the W position. This
1252 * negation makes the back-face specular term positive again.
1254 dots
= negate(swizzle(dots
,X
,Y
,W
,Z
));
1256 if (!is_undef(att
)) {
1257 emit_op1(p
, OPCODE_LIT
, lit
, 0, dots
);
1258 emit_op2(p
, OPCODE_MUL
, lit
, 0, lit
, att
);
1259 emit_op3(p
, OPCODE_MAD
, _bfc0
, 0, swizzle1(lit
,X
), ambient
, _bfc0
);
1261 else if (!p
->state
->material_shininess_is_zero
) {
1262 emit_op1(p
, OPCODE_LIT
, lit
, 0, dots
);
1263 emit_op2(p
, OPCODE_ADD
, _bfc0
, 0, ambient
, _bfc0
); /**/
1266 emit_degenerate_lit(p
, lit
, dots
);
1267 emit_op2(p
, OPCODE_ADD
, _bfc0
, 0, ambient
, _bfc0
);
1270 emit_op3(p
, OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _bfc0
);
1271 emit_op3(p
, OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _bfc1
);
1272 /* restore dots to its original state for subsequent lights
1273 * by negating and swizzling again.
1275 dots
= negate(swizzle(dots
,X
,Y
,W
,Z
));
1277 release_temp(p
, ambient
);
1278 release_temp(p
, diffuse
);
1279 release_temp(p
, specular
);
1282 release_temp(p
, half
);
1283 release_temp(p
, VPpli
);
1284 release_temp(p
, att
);
1292 static void build_fog( struct tnl_program
*p
)
1294 struct ureg fog
= register_output(p
, VARYING_SLOT_FOGC
);
1297 switch (p
->state
->fog_distance_mode
) {
1298 case FDM_EYE_RADIAL
: /* Z = sqrt(Xe*Xe + Ye*Ye + Ze*Ze) */
1299 input
= get_eye_position(p
);
1300 emit_op2(p
, OPCODE_DP3
, fog
, WRITEMASK_X
, input
, input
);
1301 emit_op1(p
, OPCODE_RSQ
, fog
, WRITEMASK_X
, fog
);
1302 emit_op1(p
, OPCODE_RCP
, fog
, WRITEMASK_X
, fog
);
1304 case FDM_EYE_PLANE
: /* Z = Ze */
1305 input
= get_eye_position_z(p
);
1306 emit_op1(p
, OPCODE_MOV
, fog
, WRITEMASK_X
, input
);
1308 case FDM_EYE_PLANE_ABS
: /* Z = abs(Ze) */
1309 input
= get_eye_position_z(p
);
1310 emit_op1(p
, OPCODE_ABS
, fog
, WRITEMASK_X
, input
);
1312 case FDM_FROM_ARRAY
:
1313 input
= swizzle1(register_input(p
, VERT_ATTRIB_FOG
), X
);
1314 emit_op1(p
, OPCODE_ABS
, fog
, WRITEMASK_X
, input
);
1317 assert(!"Bad fog mode in build_fog()");
1321 emit_op1(p
, OPCODE_MOV
, fog
, WRITEMASK_YZW
, get_identity_param(p
));
1325 static void build_reflect_texgen( struct tnl_program
*p
,
1329 struct ureg normal
= get_transformed_normal(p
);
1330 struct ureg eye_hat
= get_eye_position_normalized(p
);
1331 struct ureg tmp
= get_temp(p
);
1334 emit_op2(p
, OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1336 emit_op2(p
, OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1338 emit_op3(p
, OPCODE_MAD
, dest
, writemask
, negate(tmp
), normal
, eye_hat
);
1340 release_temp(p
, tmp
);
1344 static void build_sphere_texgen( struct tnl_program
*p
,
1348 struct ureg normal
= get_transformed_normal(p
);
1349 struct ureg eye_hat
= get_eye_position_normalized(p
);
1350 struct ureg tmp
= get_temp(p
);
1351 struct ureg half
= register_scalar_const(p
, .5);
1352 struct ureg r
= get_temp(p
);
1353 struct ureg inv_m
= get_temp(p
);
1354 struct ureg id
= get_identity_param(p
);
1356 /* Could share the above calculations, but it would be
1357 * a fairly odd state for someone to set (both sphere and
1358 * reflection active for different texture coordinate
1359 * components. Of course - if two texture units enable
1360 * reflect and/or sphere, things start to tilt in favour
1361 * of seperating this out:
1365 emit_op2(p
, OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1367 emit_op2(p
, OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1369 emit_op3(p
, OPCODE_MAD
, r
, 0, negate(tmp
), normal
, eye_hat
);
1371 emit_op2(p
, OPCODE_ADD
, tmp
, 0, r
, swizzle(id
,X
,Y
,W
,Z
));
1372 /* rx^2 + ry^2 + (rz+1)^2 */
1373 emit_op2(p
, OPCODE_DP3
, tmp
, 0, tmp
, tmp
);
1375 emit_op1(p
, OPCODE_RSQ
, tmp
, 0, tmp
);
1377 emit_op2(p
, OPCODE_MUL
, inv_m
, 0, tmp
, half
);
1379 emit_op3(p
, OPCODE_MAD
, dest
, writemask
, r
, inv_m
, half
);
1381 release_temp(p
, tmp
);
1383 release_temp(p
, inv_m
);
1387 static void build_texture_transform( struct tnl_program
*p
)
1391 for (i
= 0; i
< MAX_TEXTURE_COORD_UNITS
; i
++) {
1393 if (!(p
->state
->fragprog_inputs_read
& VARYING_BIT_TEX(i
)))
1396 if (p
->state
->unit
[i
].coord_replace
)
1399 if (p
->state
->unit
[i
].texgen_enabled
||
1400 p
->state
->unit
[i
].texmat_enabled
) {
1402 GLuint texmat_enabled
= p
->state
->unit
[i
].texmat_enabled
;
1403 struct ureg out
= register_output(p
, VARYING_SLOT_TEX0
+ i
);
1404 struct ureg out_texgen
= undef
;
1406 if (p
->state
->unit
[i
].texgen_enabled
) {
1407 GLuint copy_mask
= 0;
1408 GLuint sphere_mask
= 0;
1409 GLuint reflect_mask
= 0;
1410 GLuint normal_mask
= 0;
1414 out_texgen
= get_temp(p
);
1418 modes
[0] = p
->state
->unit
[i
].texgen_mode0
;
1419 modes
[1] = p
->state
->unit
[i
].texgen_mode1
;
1420 modes
[2] = p
->state
->unit
[i
].texgen_mode2
;
1421 modes
[3] = p
->state
->unit
[i
].texgen_mode3
;
1423 for (j
= 0; j
< 4; j
++) {
1425 case TXG_OBJ_LINEAR
: {
1426 struct ureg obj
= register_input(p
, VERT_ATTRIB_POS
);
1428 register_param3(p
, STATE_TEXGEN
, i
,
1429 STATE_TEXGEN_OBJECT_S
+ j
);
1431 emit_op2(p
, OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1435 case TXG_EYE_LINEAR
: {
1436 struct ureg eye
= get_eye_position(p
);
1438 register_param3(p
, STATE_TEXGEN
, i
,
1439 STATE_TEXGEN_EYE_S
+ j
);
1441 emit_op2(p
, OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1445 case TXG_SPHERE_MAP
:
1446 sphere_mask
|= WRITEMASK_X
<< j
;
1448 case TXG_REFLECTION_MAP
:
1449 reflect_mask
|= WRITEMASK_X
<< j
;
1451 case TXG_NORMAL_MAP
:
1452 normal_mask
|= WRITEMASK_X
<< j
;
1455 copy_mask
|= WRITEMASK_X
<< j
;
1460 build_sphere_texgen(p
, out_texgen
, sphere_mask
);
1464 build_reflect_texgen(p
, out_texgen
, reflect_mask
);
1468 struct ureg normal
= get_transformed_normal(p
);
1469 emit_op1(p
, OPCODE_MOV
, out_texgen
, normal_mask
, normal
);
1473 struct ureg in
= register_input(p
, VERT_ATTRIB_TEX0
+i
);
1474 emit_op1(p
, OPCODE_MOV
, out_texgen
, copy_mask
, in
);
1478 if (texmat_enabled
) {
1479 struct ureg texmat
[4];
1480 struct ureg in
= (!is_undef(out_texgen
) ?
1482 register_input(p
, VERT_ATTRIB_TEX0
+i
));
1483 if (p
->mvp_with_dp4
) {
1484 register_matrix_param5( p
, STATE_TEXTURE_MATRIX
, i
, 0, 3,
1486 emit_matrix_transform_vec4( p
, out
, texmat
, in
);
1489 register_matrix_param5( p
, STATE_TEXTURE_MATRIX
, i
, 0, 3,
1490 STATE_MATRIX_TRANSPOSE
, texmat
);
1491 emit_transpose_matrix_transform_vec4( p
, out
, texmat
, in
);
1498 emit_passthrough(p
, VERT_ATTRIB_TEX0
+i
, VARYING_SLOT_TEX0
+i
);
1505 * Point size attenuation computation.
1507 static void build_atten_pointsize( struct tnl_program
*p
)
1509 struct ureg eye
= get_eye_position_z(p
);
1510 struct ureg state_size
= register_param2(p
, STATE_INTERNAL
, STATE_POINT_SIZE_CLAMPED
);
1511 struct ureg state_attenuation
= register_param1(p
, STATE_POINT_ATTENUATION
);
1512 struct ureg out
= register_output(p
, VARYING_SLOT_PSIZ
);
1513 struct ureg ut
= get_temp(p
);
1516 emit_op1(p
, OPCODE_ABS
, ut
, WRITEMASK_Y
, swizzle1(eye
, Z
));
1517 /* p1 + dist * (p2 + dist * p3); */
1518 emit_op3(p
, OPCODE_MAD
, ut
, WRITEMASK_X
, swizzle1(ut
, Y
),
1519 swizzle1(state_attenuation
, Z
), swizzle1(state_attenuation
, Y
));
1520 emit_op3(p
, OPCODE_MAD
, ut
, WRITEMASK_X
, swizzle1(ut
, Y
),
1521 ut
, swizzle1(state_attenuation
, X
));
1523 /* 1 / sqrt(factor) */
1524 emit_op1(p
, OPCODE_RSQ
, ut
, WRITEMASK_X
, ut
);
1527 /* out = pointSize / sqrt(factor) */
1528 emit_op2(p
, OPCODE_MUL
, out
, WRITEMASK_X
, ut
, state_size
);
1530 /* this is a good place to clamp the point size since there's likely
1531 * no hardware registers to clamp point size at rasterization time.
1533 emit_op2(p
, OPCODE_MUL
, ut
, WRITEMASK_X
, ut
, state_size
);
1534 emit_op2(p
, OPCODE_MAX
, ut
, WRITEMASK_X
, ut
, swizzle1(state_size
, Y
));
1535 emit_op2(p
, OPCODE_MIN
, out
, WRITEMASK_X
, ut
, swizzle1(state_size
, Z
));
1538 release_temp(p
, ut
);
1543 * Pass-though per-vertex point size, from user's point size array.
1545 static void build_array_pointsize( struct tnl_program
*p
)
1547 struct ureg in
= register_input(p
, VERT_ATTRIB_POINT_SIZE
);
1548 struct ureg out
= register_output(p
, VARYING_SLOT_PSIZ
);
1549 emit_op1(p
, OPCODE_MOV
, out
, WRITEMASK_X
, in
);
1553 static void build_tnl_program( struct tnl_program
*p
)
1555 /* Emit the program, starting with the modelview, projection transforms:
1559 /* Lighting calculations:
1561 if (p
->state
->fragprog_inputs_read
& (VARYING_BIT_COL0
|VARYING_BIT_COL1
)) {
1562 if (p
->state
->light_global_enabled
)
1565 if (p
->state
->fragprog_inputs_read
& VARYING_BIT_COL0
)
1566 emit_passthrough(p
, VERT_ATTRIB_COLOR0
, VARYING_SLOT_COL0
);
1568 if (p
->state
->fragprog_inputs_read
& VARYING_BIT_COL1
)
1569 emit_passthrough(p
, VERT_ATTRIB_COLOR1
, VARYING_SLOT_COL1
);
1573 if (p
->state
->fragprog_inputs_read
& VARYING_BIT_FOGC
)
1576 if (p
->state
->fragprog_inputs_read
& VARYING_BITS_TEX_ANY
)
1577 build_texture_transform(p
);
1579 if (p
->state
->point_attenuated
)
1580 build_atten_pointsize(p
);
1581 else if (p
->state
->varying_vp_inputs
& VERT_BIT_POINT_SIZE
)
1582 build_array_pointsize(p
);
1586 emit_op1(p
, OPCODE_END
, undef
, 0, undef
);
1597 create_new_program( const struct state_key
*key
,
1598 struct gl_program
*program
,
1599 GLboolean mvp_with_dp4
,
1602 struct tnl_program p
;
1604 memset(&p
, 0, sizeof(p
));
1606 p
.program
= program
;
1607 p
.eye_position
= undef
;
1608 p
.eye_position_z
= undef
;
1609 p
.eye_position_normalized
= undef
;
1610 p
.transformed_normal
= undef
;
1613 p
.mvp_with_dp4
= mvp_with_dp4
;
1615 if (max_temps
>= sizeof(int) * 8)
1616 p
.temp_reserved
= 0;
1618 p
.temp_reserved
= ~((1<<max_temps
)-1);
1620 /* Start by allocating 32 instructions.
1621 * If we need more, we'll grow the instruction array as needed.
1624 p
.program
->arb
.Instructions
=
1625 rzalloc_array(program
, struct prog_instruction
, p
.max_inst
);
1626 p
.program
->String
= NULL
;
1627 p
.program
->arb
.NumInstructions
=
1628 p
.program
->arb
.NumTemporaries
=
1629 p
.program
->arb
.NumParameters
=
1630 p
.program
->arb
.NumAttributes
= p
.program
->arb
.NumAddressRegs
= 0;
1631 p
.program
->Parameters
= _mesa_new_parameter_list();
1632 p
.program
->info
.inputs_read
= 0;
1633 p
.program
->info
.outputs_written
= 0;
1635 build_tnl_program( &p
);
1640 * Return a vertex program which implements the current fixed-function
1641 * transform/lighting/texgen operations.
1644 _mesa_get_fixed_func_vertex_program(struct gl_context
*ctx
)
1646 struct gl_program
*prog
;
1647 struct state_key key
;
1649 /* Grab all the relevant state and put it in a single structure:
1651 make_state_key(ctx
, &key
);
1653 /* Look for an already-prepared program for this state:
1655 prog
= _mesa_search_program_cache(ctx
->VertexProgram
.Cache
, &key
,
1659 /* OK, we'll have to build a new one */
1661 printf("Build new TNL program\n");
1663 prog
= ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0, true);
1667 create_new_program( &key
, prog
,
1668 ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_VERTEX
].OptimizeForAOS
,
1669 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTemps
);
1671 if (ctx
->Driver
.ProgramStringNotify
)
1672 ctx
->Driver
.ProgramStringNotify(ctx
, GL_VERTEX_PROGRAM_ARB
, prog
);
1674 _mesa_program_cache_insert(ctx
, ctx
->VertexProgram
.Cache
, &key
,