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
37 #include "brw_state.h"
39 #include "shader/program.h"
40 #include "shader/program_instruction.h"
41 #include "shader/arbprogparse.h"
44 unsigned light_global_enabled
:1;
45 unsigned light_local_viewer
:1;
46 unsigned light_twoside
:1;
47 unsigned light_color_material
:1;
48 unsigned light_color_material_mask
:12;
49 unsigned light_material_mask
:12;
51 unsigned rescale_normals
:1;
52 unsigned fog_source_is_depth
:1;
53 unsigned tnl_do_vertex_fog
:1;
54 unsigned separate_specular
:1;
55 unsigned fog_option
:2;
56 unsigned point_attenuated
:1;
57 unsigned texture_enabled_global
:1;
58 unsigned fragprog_inputs_read
:12;
61 unsigned light_enabled
:1;
62 unsigned light_eyepos3_is_zero
:1;
63 unsigned light_spotcutoff_is_180
:1;
64 unsigned light_attenuated
:1;
65 unsigned texunit_really_enabled
:1;
66 unsigned texmat_enabled
:1;
67 unsigned texgen_enabled
:4;
68 unsigned texgen_mode0
:4;
69 unsigned texgen_mode1
:4;
70 unsigned texgen_mode2
:4;
71 unsigned texgen_mode3
:4;
82 static GLuint
translate_fog_mode( GLenum mode
)
85 case GL_LINEAR
: return FOG_LINEAR
;
86 case GL_EXP
: return FOG_EXP
;
87 case GL_EXP2
: return FOG_EXP2
;
88 default: return FOG_NONE
;
93 #define TXG_OBJ_LINEAR 1
94 #define TXG_EYE_LINEAR 2
95 #define TXG_SPHERE_MAP 3
96 #define TXG_REFLECTION_MAP 4
97 #define TXG_NORMAL_MAP 5
99 static GLuint
translate_texgen( GLboolean enabled
, GLenum mode
)
105 case GL_OBJECT_LINEAR
: return TXG_OBJ_LINEAR
;
106 case GL_EYE_LINEAR
: return TXG_EYE_LINEAR
;
107 case GL_SPHERE_MAP
: return TXG_SPHERE_MAP
;
108 case GL_REFLECTION_MAP_NV
: return TXG_REFLECTION_MAP
;
109 case GL_NORMAL_MAP_NV
: return TXG_NORMAL_MAP
;
110 default: return TXG_NONE
;
114 static void make_state_key( GLcontext
*ctx
, struct state_key
*key
)
116 struct brw_context
*brw
= brw_context(ctx
);
117 const struct gl_fragment_program
*fp
= brw
->fragment_program
;
120 /* This now relies on texenvprogram.c being active:
124 memset(key
, 0, sizeof(*key
));
126 /* BRW_NEW_FRAGMENT_PROGRAM */
127 key
->fragprog_inputs_read
= fp
->Base
.InputsRead
;
130 key
->separate_specular
= (brw
->attribs
.Light
->Model
.ColorControl
==
131 GL_SEPARATE_SPECULAR_COLOR
);
134 if (brw
->attribs
.Light
->Enabled
) {
135 key
->light_global_enabled
= 1;
137 if (brw
->attribs
.Light
->Model
.LocalViewer
)
138 key
->light_local_viewer
= 1;
140 if (brw
->attribs
.Light
->Model
.TwoSide
)
141 key
->light_twoside
= 1;
143 if (brw
->attribs
.Light
->ColorMaterialEnabled
) {
144 key
->light_color_material
= 1;
145 key
->light_color_material_mask
= brw
->attribs
.Light
->ColorMaterialBitmask
;
148 /* BRW_NEW_INPUT_VARYING */
149 for (i
= BRW_ATTRIB_MAT_FRONT_AMBIENT
; i
< BRW_ATTRIB_MAX
; i
++)
150 if (brw
->vb
.info
.varying
& ((GLuint64EXT
)1<<i
))
151 key
->light_material_mask
|= 1<<(i
-BRW_ATTRIB_MAT_FRONT_AMBIENT
);
153 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
154 struct gl_light
*light
= &brw
->attribs
.Light
->Light
[i
];
156 if (light
->Enabled
) {
157 key
->unit
[i
].light_enabled
= 1;
159 if (light
->EyePosition
[3] == 0.0)
160 key
->unit
[i
].light_eyepos3_is_zero
= 1;
162 if (light
->SpotCutoff
== 180.0)
163 key
->unit
[i
].light_spotcutoff_is_180
= 1;
165 if (light
->ConstantAttenuation
!= 1.0 ||
166 light
->LinearAttenuation
!= 0.0 ||
167 light
->QuadraticAttenuation
!= 0.0)
168 key
->unit
[i
].light_attenuated
= 1;
174 if (brw
->attribs
.Transform
->Normalize
)
177 if (brw
->attribs
.Transform
->RescaleNormals
)
178 key
->rescale_normals
= 1;
180 /* BRW_NEW_FRAGMENT_PROGRAM */
181 key
->fog_option
= translate_fog_mode(fp
->FogOption
);
183 key
->fragprog_inputs_read
|= FRAG_BIT_FOGC
;
186 if (brw
->attribs
.Fog
->FogCoordinateSource
== GL_FRAGMENT_DEPTH_EXT
)
187 key
->fog_source_is_depth
= 1;
191 key
->tnl_do_vertex_fog
= 1;
194 if (brw
->attribs
.Point
->_Attenuated
)
195 key
->point_attenuated
= 1;
198 if (brw
->attribs
.Texture
->_TexGenEnabled
||
199 brw
->attribs
.Texture
->_TexMatEnabled
||
200 brw
->attribs
.Texture
->_EnabledUnits
)
201 key
->texture_enabled_global
= 1;
203 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
204 struct gl_texture_unit
*texUnit
= &brw
->attribs
.Texture
->Unit
[i
];
206 if (texUnit
->_ReallyEnabled
)
207 key
->unit
[i
].texunit_really_enabled
= 1;
209 if (brw
->attribs
.Texture
->_TexMatEnabled
& ENABLE_TEXMAT(i
))
210 key
->unit
[i
].texmat_enabled
= 1;
212 if (texUnit
->TexGenEnabled
) {
213 key
->unit
[i
].texgen_enabled
= 1;
215 key
->unit
[i
].texgen_mode0
=
216 translate_texgen( texUnit
->TexGenEnabled
& (1<<0),
218 key
->unit
[i
].texgen_mode1
=
219 translate_texgen( texUnit
->TexGenEnabled
& (1<<1),
221 key
->unit
[i
].texgen_mode2
=
222 translate_texgen( texUnit
->TexGenEnabled
& (1<<2),
224 key
->unit
[i
].texgen_mode3
=
225 translate_texgen( texUnit
->TexGenEnabled
& (1<<3),
233 /* Very useful debugging tool - produces annotated listing of
234 * generated program with line/function references for each
235 * instruction back into this file:
239 /* Should be tunable by the driver - do we want to do matrix
240 * multiplications with DP4's or with MUL/MAD's? SSE works better
241 * with the latter, drivers may differ.
246 /* Use uregs to represent registers internally, translate to Mesa's
247 * expected formats on emit.
249 * NOTE: These are passed by value extensively in this file rather
250 * than as usual by pointer reference. If this disturbs you, try
251 * remembering they are just 32bits in size.
253 * GCC is smart enough to deal with these dword-sized structures in
254 * much the same way as if I had defined them as dwords and was using
255 * macros to access and set the fields. This is much nicer and easier
260 GLint idx
:8; /* relative addressing may be negative */
268 const struct state_key
*state
;
269 struct gl_vertex_program
*program
;
271 GLuint nr_instructions
;
273 GLuint temp_reserved
;
275 struct ureg eye_position
;
276 struct ureg eye_position_normalized
;
277 struct ureg eye_normal
;
278 struct ureg identity
;
281 GLuint color_materials
;
285 const static struct ureg undef
= {
303 static struct ureg
make_ureg(GLuint file
, GLint idx
)
309 reg
.swz
= SWIZZLE_NOOP
;
316 static struct ureg
ureg_negate( struct ureg reg
)
323 static struct ureg
swizzle( struct ureg reg
, int x
, int y
, int z
, int w
)
325 reg
.swz
= MAKE_SWIZZLE4(GET_SWZ(reg
.swz
, x
),
328 GET_SWZ(reg
.swz
, w
));
333 static struct ureg
swizzle1( struct ureg reg
, int x
)
335 return swizzle(reg
, x
, x
, x
, x
);
338 static struct ureg
get_temp( struct tnl_program
*p
)
340 int bit
= ffs( ~p
->temp_in_use
);
342 fprintf(stderr
, "%s: out of temporaries\n", __FILE__
);
346 if (bit
> p
->program
->Base
.NumTemporaries
)
347 p
->program
->Base
.NumTemporaries
= bit
;
349 p
->temp_in_use
|= 1<<(bit
-1);
350 return make_ureg(PROGRAM_TEMPORARY
, bit
-1);
353 static struct ureg
reserve_temp( struct tnl_program
*p
)
355 struct ureg temp
= get_temp( p
);
356 p
->temp_reserved
|= 1<<temp
.idx
;
360 static void release_temp( struct tnl_program
*p
, struct ureg reg
)
362 if (reg
.file
== PROGRAM_TEMPORARY
) {
363 p
->temp_in_use
&= ~(1<<reg
.idx
);
364 p
->temp_in_use
|= p
->temp_reserved
; /* can't release reserved temps */
368 static void release_temps( struct tnl_program
*p
)
370 p
->temp_in_use
= p
->temp_reserved
;
375 static struct ureg
register_input( struct tnl_program
*p
, GLuint input
)
377 GLuint orig_input
= input
;
378 /* Cram the material flags into the generic range. We'll translate
381 if (input
>= BRW_ATTRIB_MAT_FRONT_AMBIENT
)
382 input
-= BRW_ATTRIB_MAT_FRONT_AMBIENT
- BRW_ATTRIB_GENERIC0
;
386 p
->program
->Base
.InputsRead
|= (1<<input
);
387 return make_ureg(PROGRAM_INPUT
, orig_input
);
390 static struct ureg
register_output( struct tnl_program
*p
, GLuint output
)
392 p
->program
->Base
.OutputsWritten
|= (1<<output
);
393 return make_ureg(PROGRAM_OUTPUT
, output
);
396 static struct ureg
register_const4f( struct tnl_program
*p
,
408 idx
= _mesa_add_unnamed_constant( p
->program
->Base
.Parameters
, values
, 4 );
409 return make_ureg(PROGRAM_STATE_VAR
, idx
);
412 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
413 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
414 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
415 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
417 static GLboolean
is_undef( struct ureg reg
)
419 return reg
.file
== PROGRAM_UNDEFINED
;
422 static struct ureg
get_identity_param( struct tnl_program
*p
)
424 if (is_undef(p
->identity
))
425 p
->identity
= register_const4f(p
, 0,0,0,1);
430 static struct ureg
register_param6( struct tnl_program
*p
,
446 idx
= _mesa_add_state_reference( p
->program
->Base
.Parameters
, tokens
);
447 return make_ureg(PROGRAM_STATE_VAR
, idx
);
451 #define register_param1(p,s0) register_param6(p,s0,0,0,0,0,0)
452 #define register_param2(p,s0,s1) register_param6(p,s0,s1,0,0,0,0)
453 #define register_param3(p,s0,s1,s2) register_param6(p,s0,s1,s2,0,0,0)
454 #define register_param4(p,s0,s1,s2,s3) register_param6(p,s0,s1,s2,s3,0,0)
457 static void register_matrix_param6( struct tnl_program
*p
,
464 struct ureg
*matrix
)
468 /* This is a bit sad as the support is there to pull the whole
469 * matrix out in one go:
471 for (i
= 0; i
<= s4
- s3
; i
++)
472 matrix
[i
] = register_param6( p
, s0
, s1
, s2
, i
, i
, s5
);
476 static void emit_arg( struct prog_src_register
*src
,
479 src
->File
= reg
.file
;
480 src
->Index
= reg
.idx
;
481 src
->Swizzle
= reg
.swz
;
483 src
->NegateBase
= reg
.negate
;
488 static void emit_dst( struct prog_dst_register
*dst
,
489 struct ureg reg
, GLuint mask
)
491 dst
->File
= reg
.file
;
492 dst
->Index
= reg
.idx
;
493 /* allow zero as a shorthand for xyzw */
494 dst
->WriteMask
= mask
? mask
: WRITEMASK_XYZW
;
496 dst
->CondSwizzle
= 0;
501 static void debug_insn( struct prog_instruction
*inst
, const char *fn
,
505 static const char *last_fn
;
509 _mesa_printf("%s:\n", fn
);
512 _mesa_printf("%d:\t", line
);
513 _mesa_print_instruction(inst
);
518 static void emit_op3fn(struct tnl_program
*p
,
528 GLuint nr
= p
->program
->Base
.NumInstructions
++;
530 if (nr
>= p
->nr_instructions
) {
531 p
->program
->Base
.Instructions
=
532 _mesa_realloc(p
->program
->Base
.Instructions
,
533 sizeof(struct prog_instruction
) * p
->nr_instructions
,
534 sizeof(struct prog_instruction
) * (p
->nr_instructions
*= 2));
538 struct prog_instruction
*inst
= &p
->program
->Base
.Instructions
[nr
];
539 memset(inst
, 0, sizeof(*inst
));
544 emit_arg( &inst
->SrcReg
[0], src0
);
545 emit_arg( &inst
->SrcReg
[1], src1
);
546 emit_arg( &inst
->SrcReg
[2], src2
);
548 emit_dst( &inst
->DstReg
, dest
, mask
);
550 debug_insn(inst
, fn
, line
);
556 #define emit_op3(p, op, dst, mask, src0, src1, src2) \
557 emit_op3fn(p, op, dst, mask, src0, src1, src2, __FUNCTION__, __LINE__)
559 #define emit_op2(p, op, dst, mask, src0, src1) \
560 emit_op3fn(p, op, dst, mask, src0, src1, undef, __FUNCTION__, __LINE__)
562 #define emit_op1(p, op, dst, mask, src0) \
563 emit_op3fn(p, op, dst, mask, src0, undef, undef, __FUNCTION__, __LINE__)
566 static struct ureg
make_temp( struct tnl_program
*p
, struct ureg reg
)
568 if (reg
.file
== PROGRAM_TEMPORARY
&&
569 !(p
->temp_reserved
& (1<<reg
.idx
)))
572 struct ureg temp
= get_temp(p
);
573 emit_op1(p
, OPCODE_MOV
, temp
, 0, reg
);
579 /* Currently no tracking performed of input/output/register size or
580 * active elements. Could be used to reduce these operations, as
581 * could the matrix type.
583 static void emit_matrix_transform_vec4( struct tnl_program
*p
,
585 const struct ureg
*mat
,
588 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_X
, src
, mat
[0]);
589 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_Y
, src
, mat
[1]);
590 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_Z
, src
, mat
[2]);
591 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_W
, src
, mat
[3]);
594 /* This version is much easier to implement if writemasks are not
595 * supported natively on the target or (like SSE), the target doesn't
596 * have a clean/obvious dotproduct implementation.
598 static void emit_transpose_matrix_transform_vec4( struct tnl_program
*p
,
600 const struct ureg
*mat
,
605 if (dest
.file
!= PROGRAM_TEMPORARY
)
610 emit_op2(p
, OPCODE_MUL
, tmp
, 0, swizzle1(src
,X
), mat
[0]);
611 emit_op3(p
, OPCODE_MAD
, tmp
, 0, swizzle1(src
,Y
), mat
[1], tmp
);
612 emit_op3(p
, OPCODE_MAD
, tmp
, 0, swizzle1(src
,Z
), mat
[2], tmp
);
613 emit_op3(p
, OPCODE_MAD
, dest
, 0, swizzle1(src
,W
), mat
[3], tmp
);
615 if (dest
.file
!= PROGRAM_TEMPORARY
)
616 release_temp(p
, tmp
);
619 static void emit_matrix_transform_vec3( struct tnl_program
*p
,
621 const struct ureg
*mat
,
624 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_X
, src
, mat
[0]);
625 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_Y
, src
, mat
[1]);
626 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_Z
, src
, mat
[2]);
630 static void emit_normalize_vec3( struct tnl_program
*p
,
634 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_W
, src
, src
);
635 emit_op1(p
, OPCODE_RSQ
, dest
, WRITEMASK_W
, swizzle1(dest
,W
));
636 emit_op2(p
, OPCODE_MUL
, dest
, WRITEMASK_XYZ
, src
, swizzle1(dest
,W
));
639 static void emit_passthrough( struct tnl_program
*p
,
643 struct ureg out
= register_output(p
, output
);
644 emit_op1(p
, OPCODE_MOV
, out
, 0, register_input(p
, input
));
647 static struct ureg
get_eye_position( struct tnl_program
*p
)
649 if (is_undef(p
->eye_position
)) {
650 struct ureg pos
= register_input( p
, BRW_ATTRIB_POS
);
651 struct ureg modelview
[4];
653 p
->eye_position
= reserve_temp(p
);
656 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 0, 3,
657 STATE_MATRIX
, modelview
);
659 emit_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
662 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 0, 3,
663 STATE_MATRIX_TRANSPOSE
, modelview
);
665 emit_transpose_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
669 return p
->eye_position
;
674 static struct ureg
get_eye_z( struct tnl_program
*p
)
676 if (!is_undef(p
->eye_position
)) {
677 return swizzle1(p
->eye_position
, Z
);
679 else if (!is_undef(p
->eye_z
)) {
680 struct ureg pos
= register_input( p
, BRW_ATTRIB_POS
);
681 struct ureg modelview2
;
683 p
->eye_z
= reserve_temp(p
);
685 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 2, 1,
686 STATE_MATRIX
, &modelview2
);
688 emit_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
689 emit_op2(p
, OPCODE_DP4
, p
->eye_z
, WRITEMASK_Z
, pos
, modelview2
);
692 return swizzle1(p
->eye_z
, Z
)
698 static struct ureg
get_eye_position_normalized( struct tnl_program
*p
)
700 if (is_undef(p
->eye_position_normalized
)) {
701 struct ureg eye
= get_eye_position(p
);
702 p
->eye_position_normalized
= reserve_temp(p
);
703 emit_normalize_vec3(p
, p
->eye_position_normalized
, eye
);
706 return p
->eye_position_normalized
;
710 static struct ureg
get_eye_normal( struct tnl_program
*p
)
712 if (is_undef(p
->eye_normal
)) {
713 struct ureg normal
= register_input(p
, BRW_ATTRIB_NORMAL
);
714 struct ureg mvinv
[3];
716 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 0, 2,
717 STATE_MATRIX_INVTRANS
, mvinv
);
719 p
->eye_normal
= reserve_temp(p
);
721 /* Transform to eye space:
723 emit_matrix_transform_vec3( p
, p
->eye_normal
, mvinv
, normal
);
725 /* Normalize/Rescale:
727 if (p
->state
->normalize
) {
728 emit_normalize_vec3( p
, p
->eye_normal
, p
->eye_normal
);
730 else if (p
->state
->rescale_normals
) {
731 struct ureg rescale
= register_param2(p
, STATE_INTERNAL
,
734 emit_op2( p
, OPCODE_MUL
, p
->eye_normal
, 0, p
->eye_normal
,
735 swizzle1(rescale
, X
));
739 return p
->eye_normal
;
744 static void build_hpos( struct tnl_program
*p
)
746 struct ureg pos
= register_input( p
, BRW_ATTRIB_POS
);
747 struct ureg hpos
= register_output( p
, VERT_RESULT_HPOS
);
751 register_matrix_param6( p
, STATE_MATRIX
, STATE_MVP
, 0, 0, 3,
753 emit_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
756 register_matrix_param6( p
, STATE_MATRIX
, STATE_MVP
, 0, 0, 3,
757 STATE_MATRIX_TRANSPOSE
, mvp
);
758 emit_transpose_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
763 static GLuint
material_attrib( GLuint side
, GLuint property
)
765 return (property
- STATE_AMBIENT
) * 2 + side
;
768 /* Get a bitmask of which material values vary on a per-vertex basis.
770 static void set_material_flags( struct tnl_program
*p
)
772 p
->color_materials
= 0;
775 if (p
->state
->light_color_material
) {
777 p
->color_materials
= p
->state
->light_color_material_mask
;
780 p
->materials
|= p
->state
->light_material_mask
;
784 static struct ureg
get_material( struct tnl_program
*p
, GLuint side
,
787 GLuint attrib
= material_attrib(side
, property
);
789 if (p
->color_materials
& (1<<attrib
))
790 return register_input(p
, BRW_ATTRIB_COLOR0
);
791 else if (p
->materials
& (1<<attrib
))
792 return register_input( p
, attrib
+ BRW_ATTRIB_MAT_FRONT_AMBIENT
);
794 return register_param3( p
, STATE_MATERIAL
, side
, property
);
797 #define SCENE_COLOR_BITS(side) ((MAT_BIT_FRONT_EMISSION | \
798 MAT_BIT_FRONT_AMBIENT | \
799 MAT_BIT_FRONT_DIFFUSE) << (side))
801 /* Either return a precalculated constant value or emit code to
802 * calculate these values dynamically in the case where material calls
803 * are present between begin/end pairs.
805 * Probably want to shift this to the program compilation phase - if
806 * we always emitted the calculation here, a smart compiler could
807 * detect that it was constant (given a certain set of inputs), and
808 * lift it out of the main loop. That way the programs created here
809 * would be independent of the vertex_buffer details.
811 static struct ureg
get_scenecolor( struct tnl_program
*p
, GLuint side
)
813 if (p
->materials
& SCENE_COLOR_BITS(side
)) {
814 struct ureg lm_ambient
= register_param1(p
, STATE_LIGHTMODEL_AMBIENT
);
815 struct ureg material_emission
= get_material(p
, side
, STATE_EMISSION
);
816 struct ureg material_ambient
= get_material(p
, side
, STATE_AMBIENT
);
817 struct ureg material_diffuse
= get_material(p
, side
, STATE_DIFFUSE
);
818 struct ureg tmp
= make_temp(p
, material_diffuse
);
819 emit_op3(p
, OPCODE_MAD
, tmp
, WRITEMASK_XYZ
, lm_ambient
,
820 material_ambient
, material_emission
);
824 return register_param2( p
, STATE_LIGHTMODEL_SCENECOLOR
, side
);
828 static struct ureg
get_lightprod( struct tnl_program
*p
, GLuint light
,
829 GLuint side
, GLuint property
)
831 GLuint attrib
= material_attrib(side
, property
);
832 if (p
->materials
& (1<<attrib
)) {
833 struct ureg light_value
=
834 register_param3(p
, STATE_LIGHT
, light
, property
);
835 struct ureg material_value
= get_material(p
, side
, property
);
836 struct ureg tmp
= get_temp(p
);
837 emit_op2(p
, OPCODE_MUL
, tmp
, 0, light_value
, material_value
);
841 return register_param4(p
, STATE_LIGHTPROD
, light
, side
, property
);
844 static struct ureg
calculate_light_attenuation( struct tnl_program
*p
,
849 struct ureg attenuation
= register_param3(p
, STATE_LIGHT
, i
,
851 struct ureg att
= get_temp(p
);
853 /* Calculate spot attenuation:
855 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
856 struct ureg spot_dir
= register_param3(p
, STATE_LIGHT
, i
,
857 STATE_SPOT_DIRECTION
);
858 struct ureg spot
= get_temp(p
);
859 struct ureg slt
= get_temp(p
);
861 emit_normalize_vec3( p
, spot
, spot_dir
); /* XXX: precompute! */
862 emit_op2(p
, OPCODE_DP3
, spot
, 0, ureg_negate(VPpli
), spot
);
863 emit_op2(p
, OPCODE_SLT
, slt
, 0, swizzle1(spot_dir
,W
), spot
);
864 emit_op2(p
, OPCODE_POW
, spot
, 0, spot
, swizzle1(attenuation
, W
));
865 emit_op2(p
, OPCODE_MUL
, att
, 0, slt
, spot
);
867 release_temp(p
, spot
);
868 release_temp(p
, slt
);
871 /* Calculate distance attenuation:
873 if (p
->state
->unit
[i
].light_attenuated
) {
876 emit_op1(p
, OPCODE_RCP
, dist
, WRITEMASK_YZ
, dist
);
878 emit_op2(p
, OPCODE_MUL
, dist
, WRITEMASK_XZ
, dist
, swizzle1(dist
,Y
));
880 emit_op2(p
, OPCODE_DP3
, dist
, 0, attenuation
, dist
);
882 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
884 emit_op1(p
, OPCODE_RCP
, dist
, 0, dist
);
885 /* spot-atten * dist-atten */
886 emit_op2(p
, OPCODE_MUL
, att
, 0, dist
, att
);
889 emit_op1(p
, OPCODE_RCP
, att
, 0, dist
);
900 /* Need to add some addtional parameters to allow lighting in object
901 * space - STATE_SPOT_DIRECTION and STATE_HALF implicitly assume eye
904 static void build_lighting( struct tnl_program
*p
)
906 const GLboolean twoside
= p
->state
->light_twoside
;
907 const GLboolean separate
= p
->state
->separate_specular
;
908 GLuint nr_lights
= 0, count
= 0;
909 struct ureg normal
= get_eye_normal(p
);
910 struct ureg lit
= get_temp(p
);
911 struct ureg dots
= get_temp(p
);
912 struct ureg _col0
= undef
, _col1
= undef
;
913 struct ureg _bfc0
= undef
, _bfc1
= undef
;
916 for (i
= 0; i
< MAX_LIGHTS
; i
++)
917 if (p
->state
->unit
[i
].light_enabled
)
920 set_material_flags(p
);
923 struct ureg shininess
= get_material(p
, 0, STATE_SHININESS
);
924 emit_op1(p
, OPCODE_MOV
, dots
, WRITEMASK_W
, swizzle1(shininess
,X
));
925 release_temp(p
, shininess
);
927 _col0
= make_temp(p
, get_scenecolor(p
, 0));
929 _col1
= make_temp(p
, get_identity_param(p
));
936 struct ureg shininess
= get_material(p
, 1, STATE_SHININESS
);
937 emit_op1(p
, OPCODE_MOV
, dots
, WRITEMASK_Z
,
938 ureg_negate(swizzle1(shininess
,X
)));
939 release_temp(p
, shininess
);
941 _bfc0
= make_temp(p
, get_scenecolor(p
, 1));
943 _bfc1
= make_temp(p
, get_identity_param(p
));
949 /* If no lights, still need to emit the scenecolor.
951 /* KW: changed to do this always - v1.17 "Fix lighting alpha result"?
953 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
955 struct ureg res0
= register_output( p
, VERT_RESULT_COL0
);
956 emit_op1(p
, OPCODE_MOV
, res0
, 0, _col0
);
959 struct ureg res0
= register_output( p
, VERT_RESULT_BFC0
);
960 emit_op1(p
, OPCODE_MOV
, res0
, 0, _bfc0
);
964 if (separate
&& (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL1
)) {
966 struct ureg res1
= register_output( p
, VERT_RESULT_COL1
);
967 emit_op1(p
, OPCODE_MOV
, res1
, 0, _col1
);
970 struct ureg res1
= register_output( p
, VERT_RESULT_BFC1
);
971 emit_op1(p
, OPCODE_MOV
, res1
, 0, _bfc1
);
975 if (nr_lights
== 0) {
981 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
982 if (p
->state
->unit
[i
].light_enabled
) {
983 struct ureg half
= undef
;
984 struct ureg att
= undef
, VPpli
= undef
;
988 if (p
->state
->unit
[i
].light_eyepos3_is_zero
) {
989 /* Can used precomputed constants in this case.
990 * Attenuation never applies to infinite lights.
992 VPpli
= register_param3(p
, STATE_LIGHT
, i
,
993 STATE_POSITION_NORMALIZED
);
994 half
= register_param3(p
, STATE_LIGHT
, i
, STATE_HALF
);
997 struct ureg Ppli
= register_param3(p
, STATE_LIGHT
, i
,
999 struct ureg V
= get_eye_position(p
);
1000 struct ureg dist
= get_temp(p
);
1002 VPpli
= get_temp(p
);
1005 /* Calulate VPpli vector
1007 emit_op2(p
, OPCODE_SUB
, VPpli
, 0, Ppli
, V
);
1009 /* Normalize VPpli. The dist value also used in
1010 * attenuation below.
1012 emit_op2(p
, OPCODE_DP3
, dist
, 0, VPpli
, VPpli
);
1013 emit_op1(p
, OPCODE_RSQ
, dist
, 0, dist
);
1014 emit_op2(p
, OPCODE_MUL
, VPpli
, 0, VPpli
, dist
);
1017 /* Calculate attenuation:
1019 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
||
1020 p
->state
->unit
[i
].light_attenuated
) {
1021 att
= calculate_light_attenuation(p
, i
, VPpli
, dist
);
1025 /* Calculate viewer direction, or use infinite viewer:
1027 if (p
->state
->light_local_viewer
) {
1028 struct ureg eye_hat
= get_eye_position_normalized(p
);
1029 emit_op2(p
, OPCODE_SUB
, half
, 0, VPpli
, eye_hat
);
1032 struct ureg z_dir
= swizzle(get_identity_param(p
),X
,Y
,W
,Z
);
1033 emit_op2(p
, OPCODE_ADD
, half
, 0, VPpli
, z_dir
);
1036 emit_normalize_vec3(p
, half
, half
);
1038 release_temp(p
, dist
);
1041 /* Calculate dot products:
1043 emit_op2(p
, OPCODE_DP3
, dots
, WRITEMASK_X
, normal
, VPpli
);
1044 emit_op2(p
, OPCODE_DP3
, dots
, WRITEMASK_Y
, normal
, half
);
1047 /* Front face lighting:
1050 struct ureg ambient
= get_lightprod(p
, i
, 0, STATE_AMBIENT
);
1051 struct ureg diffuse
= get_lightprod(p
, i
, 0, STATE_DIFFUSE
);
1052 struct ureg specular
= get_lightprod(p
, i
, 0, STATE_SPECULAR
);
1053 struct ureg res0
, res1
;
1054 GLuint mask0
, mask1
;
1056 emit_op1(p
, OPCODE_LIT
, lit
, 0, dots
);
1059 emit_op2(p
, OPCODE_MUL
, lit
, 0, lit
, att
);
1067 if (count
== nr_lights
) {
1069 mask0
= WRITEMASK_XYZ
;
1070 mask1
= WRITEMASK_XYZ
;
1072 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
1073 res0
= register_output( p
, VERT_RESULT_COL0
);
1075 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL1
)
1076 res1
= register_output( p
, VERT_RESULT_COL1
);
1079 mask1
= WRITEMASK_XYZ
;
1081 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
1082 res1
= register_output( p
, VERT_RESULT_COL0
);
1086 emit_op3(p
, OPCODE_MAD
, _col0
, 0, swizzle1(lit
,X
), ambient
, _col0
);
1087 emit_op3(p
, OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _col0
);
1088 emit_op3(p
, OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _col1
);
1090 release_temp(p
, ambient
);
1091 release_temp(p
, diffuse
);
1092 release_temp(p
, specular
);
1095 /* Back face lighting:
1098 struct ureg ambient
= get_lightprod(p
, i
, 1, STATE_AMBIENT
);
1099 struct ureg diffuse
= get_lightprod(p
, i
, 1, STATE_DIFFUSE
);
1100 struct ureg specular
= get_lightprod(p
, i
, 1, STATE_SPECULAR
);
1101 struct ureg res0
, res1
;
1102 GLuint mask0
, mask1
;
1104 emit_op1(p
, OPCODE_LIT
, lit
, 0, ureg_negate(swizzle(dots
,X
,Y
,W
,Z
)));
1107 emit_op2(p
, OPCODE_MUL
, lit
, 0, lit
, att
);
1114 if (count
== nr_lights
) {
1116 mask0
= WRITEMASK_XYZ
;
1117 mask1
= WRITEMASK_XYZ
;
1118 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
1119 res0
= register_output( p
, VERT_RESULT_BFC0
);
1121 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL1
)
1122 res1
= register_output( p
, VERT_RESULT_BFC1
);
1125 mask1
= WRITEMASK_XYZ
;
1127 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
1128 res1
= register_output( p
, VERT_RESULT_BFC0
);
1132 emit_op3(p
, OPCODE_MAD
, _bfc0
, 0, swizzle1(lit
,X
), ambient
, _bfc0
);
1133 emit_op3(p
, OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _bfc0
);
1134 emit_op3(p
, OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _bfc1
);
1136 release_temp(p
, ambient
);
1137 release_temp(p
, diffuse
);
1138 release_temp(p
, specular
);
1141 release_temp(p
, half
);
1142 release_temp(p
, VPpli
);
1143 release_temp(p
, att
);
1151 static void build_fog( struct tnl_program
*p
)
1153 struct ureg fog
= register_output(p
, VERT_RESULT_FOGC
);
1156 if (p
->state
->fog_source_is_depth
) {
1157 input
= swizzle1(get_eye_position(p
), Z
);
1160 input
= swizzle1(register_input(p
, BRW_ATTRIB_FOG
), X
);
1163 if (p
->state
->fog_option
&&
1164 p
->state
->tnl_do_vertex_fog
) {
1165 struct ureg params
= register_param1(p
, STATE_FOG_PARAMS
);
1166 struct ureg tmp
= get_temp(p
);
1167 struct ureg id
= get_identity_param(p
);
1169 emit_op1(p
, OPCODE_MOV
, fog
, 0, id
);
1171 switch (p
->state
->fog_option
) {
1173 emit_op1(p
, OPCODE_ABS
, tmp
, 0, input
);
1174 emit_op2(p
, OPCODE_SUB
, tmp
, 0, swizzle1(params
,Z
), tmp
);
1175 emit_op2(p
, OPCODE_MUL
, tmp
, 0, tmp
, swizzle1(params
,W
));
1176 emit_op2(p
, OPCODE_MAX
, tmp
, 0, tmp
, swizzle1(id
,X
)); /* saturate */
1177 emit_op2(p
, OPCODE_MIN
, fog
, WRITEMASK_X
, tmp
, swizzle1(id
,W
));
1181 emit_op1(p
, OPCODE_ABS
, tmp
, 0, input
);
1182 emit_op2(p
, OPCODE_MUL
, tmp
, 0, tmp
, swizzle1(params
,X
));
1183 emit_op2(p
, OPCODE_POW
, fog
, WRITEMASK_X
,
1184 register_const1f(p
, M_E
), ureg_negate(tmp
));
1187 emit_op2(p
, OPCODE_MUL
, tmp
, 0, input
, swizzle1(params
,X
));
1188 emit_op2(p
, OPCODE_MUL
, tmp
, 0, tmp
, tmp
);
1189 emit_op2(p
, OPCODE_POW
, fog
, WRITEMASK_X
,
1190 register_const1f(p
, M_E
), ureg_negate(tmp
));
1194 release_temp(p
, tmp
);
1197 /* results = incoming fog coords (compute fog per-fragment later)
1199 * KW: Is it really necessary to do anything in this case?
1201 emit_op1(p
, OPCODE_MOV
, fog
, 0, input
);
1205 static void build_reflect_texgen( struct tnl_program
*p
,
1209 struct ureg normal
= get_eye_normal(p
);
1210 struct ureg eye_hat
= get_eye_position_normalized(p
);
1211 struct ureg tmp
= get_temp(p
);
1214 emit_op2(p
, OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1216 emit_op2(p
, OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1218 emit_op3(p
, OPCODE_MAD
, dest
, writemask
, ureg_negate(tmp
), normal
, eye_hat
);
1220 release_temp(p
, tmp
);
1223 static void build_sphere_texgen( struct tnl_program
*p
,
1227 struct ureg normal
= get_eye_normal(p
);
1228 struct ureg eye_hat
= get_eye_position_normalized(p
);
1229 struct ureg tmp
= get_temp(p
);
1230 struct ureg half
= register_scalar_const(p
, .5);
1231 struct ureg r
= get_temp(p
);
1232 struct ureg inv_m
= get_temp(p
);
1233 struct ureg id
= get_identity_param(p
);
1235 /* Could share the above calculations, but it would be
1236 * a fairly odd state for someone to set (both sphere and
1237 * reflection active for different texture coordinate
1238 * components. Of course - if two texture units enable
1239 * reflect and/or sphere, things start to tilt in favour
1240 * of seperating this out:
1244 emit_op2(p
, OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1246 emit_op2(p
, OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1248 emit_op3(p
, OPCODE_MAD
, r
, 0, ureg_negate(tmp
), normal
, eye_hat
);
1250 emit_op2(p
, OPCODE_ADD
, tmp
, 0, r
, swizzle(id
,X
,Y
,W
,Z
));
1251 /* rx^2 + ry^2 + (rz+1)^2 */
1252 emit_op2(p
, OPCODE_DP3
, tmp
, 0, tmp
, tmp
);
1254 emit_op1(p
, OPCODE_RSQ
, tmp
, 0, tmp
);
1256 emit_op2(p
, OPCODE_MUL
, inv_m
, 0, tmp
, half
);
1258 emit_op3(p
, OPCODE_MAD
, dest
, writemask
, r
, inv_m
, half
);
1260 release_temp(p
, tmp
);
1262 release_temp(p
, inv_m
);
1266 static void build_texture_transform( struct tnl_program
*p
)
1270 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
1272 if (!(p
->state
->fragprog_inputs_read
& (FRAG_BIT_TEX0
<<i
)))
1275 if (p
->state
->unit
[i
].texgen_enabled
||
1276 p
->state
->unit
[i
].texmat_enabled
) {
1278 GLuint texmat_enabled
= p
->state
->unit
[i
].texmat_enabled
;
1279 struct ureg out
= register_output(p
, VERT_RESULT_TEX0
+ i
);
1280 struct ureg out_texgen
= undef
;
1282 if (p
->state
->unit
[i
].texgen_enabled
) {
1283 GLuint copy_mask
= 0;
1284 GLuint sphere_mask
= 0;
1285 GLuint reflect_mask
= 0;
1286 GLuint normal_mask
= 0;
1290 out_texgen
= get_temp(p
);
1294 modes
[0] = p
->state
->unit
[i
].texgen_mode0
;
1295 modes
[1] = p
->state
->unit
[i
].texgen_mode1
;
1296 modes
[2] = p
->state
->unit
[i
].texgen_mode2
;
1297 modes
[3] = p
->state
->unit
[i
].texgen_mode3
;
1299 for (j
= 0; j
< 4; j
++) {
1301 case TXG_OBJ_LINEAR
: {
1302 struct ureg obj
= register_input(p
, BRW_ATTRIB_POS
);
1304 register_param3(p
, STATE_TEXGEN
, i
,
1305 STATE_TEXGEN_OBJECT_S
+ j
);
1307 emit_op2(p
, OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1311 case TXG_EYE_LINEAR
: {
1312 struct ureg eye
= get_eye_position(p
);
1314 register_param3(p
, STATE_TEXGEN
, i
,
1315 STATE_TEXGEN_EYE_S
+ j
);
1317 emit_op2(p
, OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1321 case TXG_SPHERE_MAP
:
1322 sphere_mask
|= WRITEMASK_X
<< j
;
1324 case TXG_REFLECTION_MAP
:
1325 reflect_mask
|= WRITEMASK_X
<< j
;
1327 case TXG_NORMAL_MAP
:
1328 normal_mask
|= WRITEMASK_X
<< j
;
1331 copy_mask
|= WRITEMASK_X
<< j
;
1338 build_sphere_texgen(p
, out_texgen
, sphere_mask
);
1342 build_reflect_texgen(p
, out_texgen
, reflect_mask
);
1346 struct ureg normal
= get_eye_normal(p
);
1347 emit_op1(p
, OPCODE_MOV
, out_texgen
, normal_mask
, normal
);
1351 struct ureg in
= register_input(p
, BRW_ATTRIB_TEX0
+i
);
1352 emit_op1(p
, OPCODE_MOV
, out_texgen
, copy_mask
, in
);
1356 if (texmat_enabled
) {
1357 struct ureg texmat
[4];
1358 struct ureg in
= (!is_undef(out_texgen
) ?
1360 register_input(p
, BRW_ATTRIB_TEX0
+i
));
1362 register_matrix_param6( p
, STATE_MATRIX
, STATE_TEXTURE
, i
,
1363 0, 3, STATE_MATRIX
, texmat
);
1364 emit_matrix_transform_vec4( p
, out
, texmat
, in
);
1367 register_matrix_param6( p
, STATE_MATRIX
, STATE_TEXTURE
, i
,
1368 0, 3, STATE_MATRIX_TRANSPOSE
, texmat
);
1369 emit_transpose_matrix_transform_vec4( p
, out
, texmat
, in
);
1376 emit_passthrough(p
, BRW_ATTRIB_TEX0
+i
, VERT_RESULT_TEX0
+i
);
1382 /* Seems like it could be tighter:
1384 static void build_pointsize( struct tnl_program
*p
)
1386 struct ureg eye
= get_eye_position(p
);
1387 struct ureg state_size
= register_param1(p
, STATE_POINT_SIZE
);
1388 struct ureg state_attenuation
= register_param1(p
, STATE_POINT_ATTENUATION
);
1389 struct ureg out
= register_output(p
, VERT_RESULT_PSIZ
);
1390 struct ureg ut
= get_temp(p
);
1392 /* 1, Z, Z * Z, 1 */
1393 emit_op1(p
, OPCODE_MOV
, ut
, WRITEMASK_XW
, swizzle1(get_identity_param(p
), W
));
1394 emit_op1(p
, OPCODE_ABS
, ut
, WRITEMASK_YZ
, swizzle1(eye
, Z
));
1395 emit_op2(p
, OPCODE_MUL
, ut
, WRITEMASK_Z
, ut
, ut
);
1398 /* p1 + p2 * dist + p3 * dist * dist, 0 */
1399 emit_op2(p
, OPCODE_DP3
, ut
, WRITEMASK_X
, ut
, state_attenuation
);
1401 /* 1 / sqrt(factor) */
1402 emit_op1(p
, OPCODE_RSQ
, ut
, WRITEMASK_X
, ut
);
1404 /* ut = pointSize / factor */
1405 emit_op2(p
, OPCODE_MUL
, ut
, WRITEMASK_X
, ut
, state_size
);
1407 /* Clamp to min/max - state_size.[yz]
1409 emit_op2(p
, OPCODE_MAX
, ut
, WRITEMASK_X
, ut
, swizzle1(state_size
, Y
));
1410 emit_op2(p
, OPCODE_MIN
, out
, 0, swizzle1(ut
, X
), swizzle1(state_size
, Z
));
1412 release_temp(p
, ut
);
1415 static void build_tnl_program( struct tnl_program
*p
)
1417 /* Emit the program, starting with modelviewproject:
1421 /* Lighting calculations:
1423 if (p
->state
->fragprog_inputs_read
& (FRAG_BIT_COL0
|FRAG_BIT_COL1
)) {
1424 if (p
->state
->light_global_enabled
)
1427 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
1428 emit_passthrough(p
, BRW_ATTRIB_COLOR0
, VERT_RESULT_COL0
);
1430 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL1
)
1431 emit_passthrough(p
, BRW_ATTRIB_COLOR1
, VERT_RESULT_COL1
);
1435 if ((p
->state
->fragprog_inputs_read
& FRAG_BIT_FOGC
) ||
1436 p
->state
->fog_option
!= FOG_NONE
)
1439 if (p
->state
->fragprog_inputs_read
& FRAG_BITS_TEX_ANY
)
1440 build_texture_transform(p
);
1442 if (p
->state
->point_attenuated
)
1447 emit_op1(p
, OPCODE_END
, undef
, 0, undef
);
1452 _mesa_printf ("\n");
1457 static void build_new_tnl_program( const struct state_key
*key
,
1458 struct gl_vertex_program
*program
,
1461 struct tnl_program p
;
1463 _mesa_memset(&p
, 0, sizeof(p
));
1465 p
.program
= program
;
1466 p
.eye_position
= undef
;
1467 p
.eye_position_normalized
= undef
;
1468 p
.eye_normal
= undef
;
1471 p
.nr_instructions
= 16;
1473 if (max_temps
>= sizeof(int) * 8)
1474 p
.temp_reserved
= 0;
1476 p
.temp_reserved
= ~((1<<max_temps
)-1);
1478 p
.program
->Base
.Instructions
=
1479 _mesa_malloc(sizeof(struct prog_instruction
) * p
.nr_instructions
);
1480 p
.program
->Base
.String
= 0;
1481 p
.program
->Base
.NumInstructions
=
1482 p
.program
->Base
.NumTemporaries
=
1483 p
.program
->Base
.NumParameters
=
1484 p
.program
->Base
.NumAttributes
= p
.program
->Base
.NumAddressRegs
= 0;
1485 p
.program
->Base
.Parameters
= _mesa_new_parameter_list();
1486 p
.program
->Base
.InputsRead
= 0;
1487 p
.program
->Base
.OutputsWritten
= 0;
1489 build_tnl_program( &p
);
1492 static void *search_cache( struct brw_tnl_cache
*cache
,
1497 struct brw_tnl_cache_item
*c
;
1499 for (c
= cache
->items
[hash
% cache
->size
]; c
; c
= c
->next
) {
1500 if (c
->hash
== hash
&& memcmp(c
->key
, key
, keysize
) == 0)
1507 static void rehash( struct brw_tnl_cache
*cache
)
1509 struct brw_tnl_cache_item
**items
;
1510 struct brw_tnl_cache_item
*c
, *next
;
1513 size
= cache
->size
* 3;
1514 items
= (struct brw_tnl_cache_item
**) _mesa_malloc(size
* sizeof(*items
));
1515 _mesa_memset(items
, 0, size
* sizeof(*items
));
1517 for (i
= 0; i
< cache
->size
; i
++)
1518 for (c
= cache
->items
[i
]; c
; c
= next
) {
1520 c
->next
= items
[c
->hash
% size
];
1521 items
[c
->hash
% size
] = c
;
1525 cache
->items
= items
;
1529 static void cache_item( struct brw_tnl_cache
*cache
,
1531 const struct state_key
*key
,
1534 struct brw_tnl_cache_item
*c
= MALLOC(sizeof(*c
));
1537 c
->key
= malloc(sizeof(*key
));
1538 memcpy(c
->key
, key
, sizeof(*key
));
1542 if (++cache
->n_items
> cache
->size
* 1.5)
1545 c
->next
= cache
->items
[hash
% cache
->size
];
1546 cache
->items
[hash
% cache
->size
] = c
;
1550 static GLuint
hash_key( struct state_key
*key
)
1552 GLuint
*ikey
= (GLuint
*)key
;
1555 /* I'm sure this can be improved on, but speed is important:
1557 for (i
= 0; i
< sizeof(*key
)/sizeof(GLuint
); i
++)
1563 static void update_tnl_program( struct brw_context
*brw
)
1565 GLcontext
*ctx
= &brw
->intel
.ctx
;
1566 struct state_key key
;
1568 struct gl_vertex_program
*old
= brw
->tnl_program
;
1571 if (brw
->attribs
.VertexProgram
->_Enabled
)
1574 /* Grab all the relevent state and put it in a single structure:
1576 make_state_key(ctx
, &key
);
1577 hash
= hash_key(&key
);
1579 /* Look for an already-prepared program for this state:
1581 brw
->tnl_program
= (struct gl_vertex_program
*)
1582 search_cache( &brw
->tnl_program_cache
, hash
, &key
, sizeof(key
) );
1584 /* OK, we'll have to build a new one:
1586 if (!brw
->tnl_program
) {
1587 brw
->tnl_program
= (struct gl_vertex_program
*)
1588 ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
1590 build_new_tnl_program( &key
, brw
->tnl_program
,
1591 /* ctx->Const.MaxVertexProgramTemps */
1595 if (ctx
->Driver
.ProgramStringNotify
)
1596 ctx
->Driver
.ProgramStringNotify( ctx
, GL_VERTEX_PROGRAM_ARB
,
1597 &brw
->tnl_program
->Base
);
1599 cache_item( &brw
->tnl_program_cache
,
1600 hash
, &key
, brw
->tnl_program
);
1603 if (old
!= brw
->tnl_program
)
1604 brw
->state
.dirty
.brw
|= BRW_NEW_TNL_PROGRAM
;
1607 /* Note: See brw_draw.c - the vertex program must not rely on
1608 * brw->primitive or brw->reduced_prim.
1610 const struct brw_tracked_state brw_tnl_vertprog
= {
1612 .mesa
= (_NEW_PROGRAM
|
1619 .brw
= (BRW_NEW_FRAGMENT_PROGRAM
|
1620 BRW_NEW_INPUT_VARYING
),
1623 .update
= update_tnl_program
1629 static void update_active_vertprog( struct brw_context
*brw
)
1631 const struct gl_vertex_program
*prev
= brw
->vertex_program
;
1634 if (brw
->attribs
.VertexProgram
->_Enabled
) {
1635 brw
->vertex_program
= brw
->attribs
.VertexProgram
->Current
;
1638 /* BRW_NEW_TNL_PROGRAM */
1639 brw
->vertex_program
= brw
->tnl_program
;
1642 if (brw
->vertex_program
!= prev
)
1643 brw
->state
.dirty
.brw
|= BRW_NEW_VERTEX_PROGRAM
;
1648 const struct brw_tracked_state brw_active_vertprog
= {
1650 .mesa
= _NEW_PROGRAM
,
1651 .brw
= BRW_NEW_TNL_PROGRAM
,
1654 .update
= update_active_vertprog
1658 void brw_ProgramCacheInit( GLcontext
*ctx
)
1660 struct brw_context
*brw
= brw_context(ctx
);
1662 brw
->tnl_program_cache
.size
= 17;
1663 brw
->tnl_program_cache
.n_items
= 0;
1664 brw
->tnl_program_cache
.items
= (struct brw_tnl_cache_item
**)
1665 _mesa_calloc(brw
->tnl_program_cache
.size
*
1666 sizeof(struct brw_tnl_cache_item
));
1669 void brw_ProgramCacheDestroy( GLcontext
*ctx
)
1671 struct brw_context
*brw
= brw_context(ctx
);
1672 struct brw_tnl_cache_item
*c
, *next
;
1675 for (i
= 0; i
< brw
->tnl_program_cache
.size
; i
++)
1676 for (c
= brw
->tnl_program_cache
.items
[i
]; c
; c
= next
) {
1683 FREE(brw
->tnl_program_cache
.items
);