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 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_INDEX
; i
++)
150 if (brw
->vb
.info
.varying
[0] & (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.
245 #define BRW_TNL_MAX_INSN 256
247 /* Use uregs to represent registers internally, translate to Mesa's
248 * expected formats on emit.
250 * NOTE: These are passed by value extensively in this file rather
251 * than as usual by pointer reference. If this disturbs you, try
252 * remembering they are just 32bits in size.
254 * GCC is smart enough to deal with these dword-sized structures in
255 * much the same way as if I had defined them as dwords and was using
256 * macros to access and set the fields. This is much nicer and easier
261 GLint idx
:8; /* relative addressing may be negative */
269 const struct state_key
*state
;
270 struct gl_vertex_program
*program
;
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 p
->program
->Base
.InputsRead
|= (1<<input
);
378 return make_ureg(PROGRAM_INPUT
, input
);
381 static struct ureg
register_output( struct tnl_program
*p
, GLuint output
)
383 p
->program
->Base
.OutputsWritten
|= (1<<output
);
384 return make_ureg(PROGRAM_OUTPUT
, output
);
387 static struct ureg
register_const4f( struct tnl_program
*p
,
399 idx
= _mesa_add_unnamed_constant( p
->program
->Base
.Parameters
, values
);
400 return make_ureg(PROGRAM_STATE_VAR
, idx
);
403 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
404 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
405 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
406 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
408 static GLboolean
is_undef( struct ureg reg
)
410 return reg
.file
== PROGRAM_UNDEFINED
;
413 static struct ureg
get_identity_param( struct tnl_program
*p
)
415 if (is_undef(p
->identity
))
416 p
->identity
= register_const4f(p
, 0,0,0,1);
421 static struct ureg
register_param6( struct tnl_program
*p
,
437 idx
= _mesa_add_state_reference( p
->program
->Base
.Parameters
, tokens
);
438 return make_ureg(PROGRAM_STATE_VAR
, idx
);
442 #define register_param1(p,s0) register_param6(p,s0,0,0,0,0,0)
443 #define register_param2(p,s0,s1) register_param6(p,s0,s1,0,0,0,0)
444 #define register_param3(p,s0,s1,s2) register_param6(p,s0,s1,s2,0,0,0)
445 #define register_param4(p,s0,s1,s2,s3) register_param6(p,s0,s1,s2,s3,0,0)
448 static void register_matrix_param6( struct tnl_program
*p
,
455 struct ureg
*matrix
)
459 /* This is a bit sad as the support is there to pull the whole
460 * matrix out in one go:
462 for (i
= 0; i
<= s4
- s3
; i
++)
463 matrix
[i
] = register_param6( p
, s0
, s1
, s2
, i
, i
, s5
);
467 static void emit_arg( struct prog_src_register
*src
,
470 src
->File
= reg
.file
;
471 src
->Index
= reg
.idx
;
472 src
->Swizzle
= reg
.swz
;
474 src
->NegateBase
= reg
.negate
;
479 static void emit_dst( struct prog_dst_register
*dst
,
480 struct ureg reg
, GLuint mask
)
482 dst
->File
= reg
.file
;
483 dst
->Index
= reg
.idx
;
484 /* allow zero as a shorthand for xyzw */
485 dst
->WriteMask
= mask
? mask
: WRITEMASK_XYZW
;
487 dst
->CondSwizzle
= 0;
492 static void debug_insn( struct prog_instruction
*inst
, const char *fn
,
496 static const char *last_fn
;
500 _mesa_printf("%s:\n", fn
);
503 _mesa_printf("%d:\t", line
);
504 _mesa_print_instruction(inst
);
509 static void emit_op3fn(struct tnl_program
*p
,
519 GLuint nr
= p
->program
->Base
.NumInstructions
++;
520 struct prog_instruction
*inst
= &p
->program
->Base
.Instructions
[nr
];
522 if (p
->program
->Base
.NumInstructions
> BRW_TNL_MAX_INSN
) {
523 _mesa_problem(0, "Out of instructions in emit_op3fn\n");
531 emit_arg( &inst
->SrcReg
[0], src0
);
532 emit_arg( &inst
->SrcReg
[1], src1
);
533 emit_arg( &inst
->SrcReg
[2], src2
);
535 emit_dst( &inst
->DstReg
, dest
, mask
);
537 debug_insn(inst
, fn
, line
);
542 #define emit_op3(p, op, dst, mask, src0, src1, src2) \
543 emit_op3fn(p, op, dst, mask, src0, src1, src2, __FUNCTION__, __LINE__)
545 #define emit_op2(p, op, dst, mask, src0, src1) \
546 emit_op3fn(p, op, dst, mask, src0, src1, undef, __FUNCTION__, __LINE__)
548 #define emit_op1(p, op, dst, mask, src0) \
549 emit_op3fn(p, op, dst, mask, src0, undef, undef, __FUNCTION__, __LINE__)
552 static struct ureg
make_temp( struct tnl_program
*p
, struct ureg reg
)
554 if (reg
.file
== PROGRAM_TEMPORARY
&&
555 !(p
->temp_reserved
& (1<<reg
.idx
)))
558 struct ureg temp
= get_temp(p
);
559 emit_op1(p
, OPCODE_MOV
, temp
, 0, reg
);
565 /* Currently no tracking performed of input/output/register size or
566 * active elements. Could be used to reduce these operations, as
567 * could the matrix type.
569 static void emit_matrix_transform_vec4( struct tnl_program
*p
,
571 const struct ureg
*mat
,
574 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_X
, src
, mat
[0]);
575 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_Y
, src
, mat
[1]);
576 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_Z
, src
, mat
[2]);
577 emit_op2(p
, OPCODE_DP4
, dest
, WRITEMASK_W
, src
, mat
[3]);
580 /* This version is much easier to implement if writemasks are not
581 * supported natively on the target or (like SSE), the target doesn't
582 * have a clean/obvious dotproduct implementation.
584 static void emit_transpose_matrix_transform_vec4( struct tnl_program
*p
,
586 const struct ureg
*mat
,
591 if (dest
.file
!= PROGRAM_TEMPORARY
)
596 emit_op2(p
, OPCODE_MUL
, tmp
, 0, swizzle1(src
,X
), mat
[0]);
597 emit_op3(p
, OPCODE_MAD
, tmp
, 0, swizzle1(src
,Y
), mat
[1], tmp
);
598 emit_op3(p
, OPCODE_MAD
, tmp
, 0, swizzle1(src
,Z
), mat
[2], tmp
);
599 emit_op3(p
, OPCODE_MAD
, dest
, 0, swizzle1(src
,W
), mat
[3], tmp
);
601 if (dest
.file
!= PROGRAM_TEMPORARY
)
602 release_temp(p
, tmp
);
605 static void emit_matrix_transform_vec3( struct tnl_program
*p
,
607 const struct ureg
*mat
,
610 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_X
, src
, mat
[0]);
611 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_Y
, src
, mat
[1]);
612 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_Z
, src
, mat
[2]);
616 static void emit_normalize_vec3( struct tnl_program
*p
,
620 emit_op2(p
, OPCODE_DP3
, dest
, WRITEMASK_W
, src
, src
);
621 emit_op1(p
, OPCODE_RSQ
, dest
, WRITEMASK_W
, swizzle1(dest
,W
));
622 emit_op2(p
, OPCODE_MUL
, dest
, WRITEMASK_XYZ
, src
, swizzle1(dest
,W
));
625 static void emit_passthrough( struct tnl_program
*p
,
629 struct ureg out
= register_output(p
, output
);
630 emit_op1(p
, OPCODE_MOV
, out
, 0, register_input(p
, input
));
633 static struct ureg
get_eye_position( struct tnl_program
*p
)
635 if (is_undef(p
->eye_position
)) {
636 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
637 struct ureg modelview
[4];
639 p
->eye_position
= reserve_temp(p
);
642 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 0, 3,
643 STATE_MATRIX
, modelview
);
645 emit_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
648 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 0, 3,
649 STATE_MATRIX_TRANSPOSE
, modelview
);
651 emit_transpose_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
655 return p
->eye_position
;
660 static struct ureg
get_eye_z( struct tnl_program
*p
)
662 if (!is_undef(p
->eye_position
)) {
663 return swizzle1(p
->eye_position
, Z
);
665 else if (!is_undef(p
->eye_z
)) {
666 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
667 struct ureg modelview2
;
669 p
->eye_z
= reserve_temp(p
);
671 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 2, 1,
672 STATE_MATRIX
, &modelview2
);
674 emit_matrix_transform_vec4(p
, p
->eye_position
, modelview
, pos
);
675 emit_op2(p
, OPCODE_DP4
, p
->eye_z
, WRITEMASK_Z
, pos
, modelview2
);
678 return swizzle1(p
->eye_z
, Z
)
684 static struct ureg
get_eye_position_normalized( struct tnl_program
*p
)
686 if (is_undef(p
->eye_position_normalized
)) {
687 struct ureg eye
= get_eye_position(p
);
688 p
->eye_position_normalized
= reserve_temp(p
);
689 emit_normalize_vec3(p
, p
->eye_position_normalized
, eye
);
692 return p
->eye_position_normalized
;
696 static struct ureg
get_eye_normal( struct tnl_program
*p
)
698 if (is_undef(p
->eye_normal
)) {
699 struct ureg normal
= register_input(p
, VERT_ATTRIB_NORMAL
);
700 struct ureg mvinv
[3];
702 register_matrix_param6( p
, STATE_MATRIX
, STATE_MODELVIEW
, 0, 0, 2,
703 STATE_MATRIX_INVTRANS
, mvinv
);
705 p
->eye_normal
= reserve_temp(p
);
707 /* Transform to eye space:
709 emit_matrix_transform_vec3( p
, p
->eye_normal
, mvinv
, normal
);
711 /* Normalize/Rescale:
713 if (p
->state
->normalize
) {
714 emit_normalize_vec3( p
, p
->eye_normal
, p
->eye_normal
);
716 else if (p
->state
->rescale_normals
) {
717 struct ureg rescale
= register_param2(p
, STATE_INTERNAL
,
720 emit_op2( p
, OPCODE_MUL
, p
->eye_normal
, 0, p
->eye_normal
,
721 swizzle1(rescale
, X
));
725 return p
->eye_normal
;
730 static void build_hpos( struct tnl_program
*p
)
732 struct ureg pos
= register_input( p
, VERT_ATTRIB_POS
);
733 struct ureg hpos
= register_output( p
, VERT_RESULT_HPOS
);
737 register_matrix_param6( p
, STATE_MATRIX
, STATE_MVP
, 0, 0, 3,
739 emit_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
742 register_matrix_param6( p
, STATE_MATRIX
, STATE_MVP
, 0, 0, 3,
743 STATE_MATRIX_TRANSPOSE
, mvp
);
744 emit_transpose_matrix_transform_vec4( p
, hpos
, mvp
, pos
);
749 static GLuint
material_attrib( GLuint side
, GLuint property
)
751 return (property
- STATE_AMBIENT
) * 2 + side
;
754 /* Get a bitmask of which material values vary on a per-vertex basis.
756 static void set_material_flags( struct tnl_program
*p
)
758 p
->color_materials
= 0;
761 if (p
->state
->light_color_material
) {
763 p
->color_materials
= p
->state
->light_color_material_mask
;
766 p
->materials
|= p
->state
->light_material_mask
;
770 static struct ureg
get_material( struct tnl_program
*p
, GLuint side
,
773 GLuint attrib
= material_attrib(side
, property
);
775 if (p
->color_materials
& (1<<attrib
))
776 return register_input(p
, VERT_ATTRIB_COLOR0
);
777 else if (p
->materials
& (1<<attrib
))
778 return register_input( p
, attrib
+ BRW_ATTRIB_MAT_FRONT_AMBIENT
);
780 return register_param3( p
, STATE_MATERIAL
, side
, property
);
783 #define SCENE_COLOR_BITS(side) ((MAT_BIT_FRONT_EMISSION | \
784 MAT_BIT_FRONT_AMBIENT | \
785 MAT_BIT_FRONT_DIFFUSE) << (side))
787 /* Either return a precalculated constant value or emit code to
788 * calculate these values dynamically in the case where material calls
789 * are present between begin/end pairs.
791 * Probably want to shift this to the program compilation phase - if
792 * we always emitted the calculation here, a smart compiler could
793 * detect that it was constant (given a certain set of inputs), and
794 * lift it out of the main loop. That way the programs created here
795 * would be independent of the vertex_buffer details.
797 static struct ureg
get_scenecolor( struct tnl_program
*p
, GLuint side
)
799 if (p
->materials
& SCENE_COLOR_BITS(side
)) {
800 struct ureg lm_ambient
= register_param1(p
, STATE_LIGHTMODEL_AMBIENT
);
801 struct ureg material_emission
= get_material(p
, side
, STATE_EMISSION
);
802 struct ureg material_ambient
= get_material(p
, side
, STATE_AMBIENT
);
803 struct ureg material_diffuse
= get_material(p
, side
, STATE_DIFFUSE
);
804 struct ureg tmp
= make_temp(p
, material_diffuse
);
805 emit_op3(p
, OPCODE_MAD
, tmp
, WRITEMASK_XYZ
, lm_ambient
,
806 material_ambient
, material_emission
);
810 return register_param2( p
, STATE_LIGHTMODEL_SCENECOLOR
, side
);
814 static struct ureg
get_lightprod( struct tnl_program
*p
, GLuint light
,
815 GLuint side
, GLuint property
)
817 GLuint attrib
= material_attrib(side
, property
);
818 if (p
->materials
& (1<<attrib
)) {
819 struct ureg light_value
=
820 register_param3(p
, STATE_LIGHT
, light
, property
);
821 struct ureg material_value
= get_material(p
, side
, property
);
822 struct ureg tmp
= get_temp(p
);
823 emit_op2(p
, OPCODE_MUL
, tmp
, 0, light_value
, material_value
);
827 return register_param4(p
, STATE_LIGHTPROD
, light
, side
, property
);
830 static struct ureg
calculate_light_attenuation( struct tnl_program
*p
,
835 struct ureg attenuation
= register_param3(p
, STATE_LIGHT
, i
,
837 struct ureg att
= get_temp(p
);
839 /* Calculate spot attenuation:
841 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
842 struct ureg spot_dir
= register_param3(p
, STATE_LIGHT
, i
,
843 STATE_SPOT_DIRECTION
);
844 struct ureg spot
= get_temp(p
);
845 struct ureg slt
= get_temp(p
);
847 emit_normalize_vec3( p
, spot
, spot_dir
); /* XXX: precompute! */
848 emit_op2(p
, OPCODE_DP3
, spot
, 0, ureg_negate(VPpli
), spot
);
849 emit_op2(p
, OPCODE_SLT
, slt
, 0, swizzle1(spot_dir
,W
), spot
);
850 emit_op2(p
, OPCODE_POW
, spot
, 0, spot
, swizzle1(attenuation
, W
));
851 emit_op2(p
, OPCODE_MUL
, att
, 0, slt
, spot
);
853 release_temp(p
, spot
);
854 release_temp(p
, slt
);
857 /* Calculate distance attenuation:
859 if (p
->state
->unit
[i
].light_attenuated
) {
862 emit_op1(p
, OPCODE_RCP
, dist
, WRITEMASK_YZ
, dist
);
864 emit_op2(p
, OPCODE_MUL
, dist
, WRITEMASK_XZ
, dist
, swizzle1(dist
,Y
));
866 emit_op2(p
, OPCODE_DP3
, dist
, 0, attenuation
, dist
);
868 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
) {
870 emit_op1(p
, OPCODE_RCP
, dist
, 0, dist
);
871 /* spot-atten * dist-atten */
872 emit_op2(p
, OPCODE_MUL
, att
, 0, dist
, att
);
875 emit_op1(p
, OPCODE_RCP
, att
, 0, dist
);
886 /* Need to add some addtional parameters to allow lighting in object
887 * space - STATE_SPOT_DIRECTION and STATE_HALF implicitly assume eye
890 static void build_lighting( struct tnl_program
*p
)
892 const GLboolean twoside
= p
->state
->light_twoside
;
893 const GLboolean separate
= p
->state
->separate_specular
;
894 GLuint nr_lights
= 0, count
= 0;
895 struct ureg normal
= get_eye_normal(p
);
896 struct ureg lit
= get_temp(p
);
897 struct ureg dots
= get_temp(p
);
898 struct ureg _col0
= undef
, _col1
= undef
;
899 struct ureg _bfc0
= undef
, _bfc1
= undef
;
902 for (i
= 0; i
< MAX_LIGHTS
; i
++)
903 if (p
->state
->unit
[i
].light_enabled
)
906 set_material_flags(p
);
909 struct ureg shininess
= get_material(p
, 0, STATE_SHININESS
);
910 emit_op1(p
, OPCODE_MOV
, dots
, WRITEMASK_W
, swizzle1(shininess
,X
));
911 release_temp(p
, shininess
);
913 _col0
= make_temp(p
, get_scenecolor(p
, 0));
915 _col1
= make_temp(p
, get_identity_param(p
));
922 struct ureg shininess
= get_material(p
, 1, STATE_SHININESS
);
923 emit_op1(p
, OPCODE_MOV
, dots
, WRITEMASK_Z
,
924 ureg_negate(swizzle1(shininess
,X
)));
925 release_temp(p
, shininess
);
927 _bfc0
= make_temp(p
, get_scenecolor(p
, 1));
929 _bfc1
= make_temp(p
, get_identity_param(p
));
935 /* If no lights, still need to emit the scenecolor.
937 /* KW: changed to do this always - v1.17 "Fix lighting alpha result"?
939 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
941 struct ureg res0
= register_output( p
, VERT_RESULT_COL0
);
942 emit_op1(p
, OPCODE_MOV
, res0
, 0, _col0
);
945 struct ureg res0
= register_output( p
, VERT_RESULT_BFC0
);
946 emit_op1(p
, OPCODE_MOV
, res0
, 0, _bfc0
);
950 if (separate
&& (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL1
)) {
952 struct ureg res1
= register_output( p
, VERT_RESULT_COL1
);
953 emit_op1(p
, OPCODE_MOV
, res1
, 0, _col1
);
956 struct ureg res1
= register_output( p
, VERT_RESULT_BFC1
);
957 emit_op1(p
, OPCODE_MOV
, res1
, 0, _bfc1
);
961 if (nr_lights
== 0) {
967 for (i
= 0; i
< MAX_LIGHTS
; i
++) {
968 if (p
->state
->unit
[i
].light_enabled
) {
969 struct ureg half
= undef
;
970 struct ureg att
= undef
, VPpli
= undef
;
974 if (p
->state
->unit
[i
].light_eyepos3_is_zero
) {
975 /* Can used precomputed constants in this case.
976 * Attenuation never applies to infinite lights.
978 VPpli
= register_param3(p
, STATE_LIGHT
, i
,
979 STATE_POSITION_NORMALIZED
);
980 half
= register_param3(p
, STATE_LIGHT
, i
, STATE_HALF
);
983 struct ureg Ppli
= register_param3(p
, STATE_LIGHT
, i
,
985 struct ureg V
= get_eye_position(p
);
986 struct ureg dist
= get_temp(p
);
991 /* Calulate VPpli vector
993 emit_op2(p
, OPCODE_SUB
, VPpli
, 0, Ppli
, V
);
995 /* Normalize VPpli. The dist value also used in
998 emit_op2(p
, OPCODE_DP3
, dist
, 0, VPpli
, VPpli
);
999 emit_op1(p
, OPCODE_RSQ
, dist
, 0, dist
);
1000 emit_op2(p
, OPCODE_MUL
, VPpli
, 0, VPpli
, dist
);
1003 /* Calculate attenuation:
1005 if (!p
->state
->unit
[i
].light_spotcutoff_is_180
||
1006 p
->state
->unit
[i
].light_attenuated
) {
1007 att
= calculate_light_attenuation(p
, i
, VPpli
, dist
);
1011 /* Calculate viewer direction, or use infinite viewer:
1013 if (p
->state
->light_local_viewer
) {
1014 struct ureg eye_hat
= get_eye_position_normalized(p
);
1015 emit_op2(p
, OPCODE_SUB
, half
, 0, VPpli
, eye_hat
);
1018 struct ureg z_dir
= swizzle(get_identity_param(p
),X
,Y
,W
,Z
);
1019 emit_op2(p
, OPCODE_ADD
, half
, 0, VPpli
, z_dir
);
1022 emit_normalize_vec3(p
, half
, half
);
1024 release_temp(p
, dist
);
1027 /* Calculate dot products:
1029 emit_op2(p
, OPCODE_DP3
, dots
, WRITEMASK_X
, normal
, VPpli
);
1030 emit_op2(p
, OPCODE_DP3
, dots
, WRITEMASK_Y
, normal
, half
);
1033 /* Front face lighting:
1036 struct ureg ambient
= get_lightprod(p
, i
, 0, STATE_AMBIENT
);
1037 struct ureg diffuse
= get_lightprod(p
, i
, 0, STATE_DIFFUSE
);
1038 struct ureg specular
= get_lightprod(p
, i
, 0, STATE_SPECULAR
);
1039 struct ureg res0
, res1
;
1040 GLuint mask0
, mask1
;
1042 emit_op1(p
, OPCODE_LIT
, lit
, 0, dots
);
1045 emit_op2(p
, OPCODE_MUL
, lit
, 0, lit
, att
);
1053 if (count
== nr_lights
) {
1055 mask0
= WRITEMASK_XYZ
;
1056 mask1
= WRITEMASK_XYZ
;
1058 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
1059 res0
= register_output( p
, VERT_RESULT_COL0
);
1061 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL1
)
1062 res1
= register_output( p
, VERT_RESULT_COL1
);
1065 mask1
= WRITEMASK_XYZ
;
1067 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
1068 res1
= register_output( p
, VERT_RESULT_COL0
);
1072 emit_op3(p
, OPCODE_MAD
, _col0
, 0, swizzle1(lit
,X
), ambient
, _col0
);
1073 emit_op3(p
, OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _col0
);
1074 emit_op3(p
, OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _col1
);
1076 release_temp(p
, ambient
);
1077 release_temp(p
, diffuse
);
1078 release_temp(p
, specular
);
1081 /* Back face lighting:
1084 struct ureg ambient
= get_lightprod(p
, i
, 1, STATE_AMBIENT
);
1085 struct ureg diffuse
= get_lightprod(p
, i
, 1, STATE_DIFFUSE
);
1086 struct ureg specular
= get_lightprod(p
, i
, 1, STATE_SPECULAR
);
1087 struct ureg res0
, res1
;
1088 GLuint mask0
, mask1
;
1090 emit_op1(p
, OPCODE_LIT
, lit
, 0, ureg_negate(swizzle(dots
,X
,Y
,W
,Z
)));
1093 emit_op2(p
, OPCODE_MUL
, lit
, 0, lit
, att
);
1100 if (count
== nr_lights
) {
1102 mask0
= WRITEMASK_XYZ
;
1103 mask1
= WRITEMASK_XYZ
;
1104 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
1105 res0
= register_output( p
, VERT_RESULT_BFC0
);
1107 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL1
)
1108 res1
= register_output( p
, VERT_RESULT_BFC1
);
1111 mask1
= WRITEMASK_XYZ
;
1113 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
1114 res1
= register_output( p
, VERT_RESULT_BFC0
);
1118 emit_op3(p
, OPCODE_MAD
, _bfc0
, 0, swizzle1(lit
,X
), ambient
, _bfc0
);
1119 emit_op3(p
, OPCODE_MAD
, res0
, mask0
, swizzle1(lit
,Y
), diffuse
, _bfc0
);
1120 emit_op3(p
, OPCODE_MAD
, res1
, mask1
, swizzle1(lit
,Z
), specular
, _bfc1
);
1122 release_temp(p
, ambient
);
1123 release_temp(p
, diffuse
);
1124 release_temp(p
, specular
);
1127 release_temp(p
, half
);
1128 release_temp(p
, VPpli
);
1129 release_temp(p
, att
);
1137 static void build_fog( struct tnl_program
*p
)
1139 struct ureg fog
= register_output(p
, VERT_RESULT_FOGC
);
1142 if (p
->state
->fog_source_is_depth
) {
1143 input
= swizzle1(get_eye_position(p
), Z
);
1146 input
= swizzle1(register_input(p
, VERT_ATTRIB_FOG
), X
);
1149 if (p
->state
->fog_option
&&
1150 p
->state
->tnl_do_vertex_fog
) {
1151 struct ureg params
= register_param1(p
, STATE_FOG_PARAMS
);
1152 struct ureg tmp
= get_temp(p
);
1153 struct ureg id
= get_identity_param(p
);
1155 emit_op1(p
, OPCODE_MOV
, fog
, 0, id
);
1157 switch (p
->state
->fog_option
) {
1159 emit_op1(p
, OPCODE_ABS
, tmp
, 0, input
);
1160 emit_op2(p
, OPCODE_SUB
, tmp
, 0, swizzle1(params
,Z
), tmp
);
1161 emit_op2(p
, OPCODE_MUL
, tmp
, 0, tmp
, swizzle1(params
,W
));
1162 emit_op2(p
, OPCODE_MAX
, tmp
, 0, tmp
, swizzle1(id
,X
)); /* saturate */
1163 emit_op2(p
, OPCODE_MIN
, fog
, WRITEMASK_X
, tmp
, swizzle1(id
,W
));
1167 emit_op1(p
, OPCODE_ABS
, tmp
, 0, input
);
1168 emit_op2(p
, OPCODE_MUL
, tmp
, 0, tmp
, swizzle1(params
,X
));
1169 emit_op2(p
, OPCODE_POW
, fog
, WRITEMASK_X
,
1170 register_const1f(p
, M_E
), ureg_negate(tmp
));
1173 emit_op2(p
, OPCODE_MUL
, tmp
, 0, input
, swizzle1(params
,X
));
1174 emit_op2(p
, OPCODE_MUL
, tmp
, 0, tmp
, tmp
);
1175 emit_op2(p
, OPCODE_POW
, fog
, WRITEMASK_X
,
1176 register_const1f(p
, M_E
), ureg_negate(tmp
));
1180 release_temp(p
, tmp
);
1183 /* results = incoming fog coords (compute fog per-fragment later)
1185 * KW: Is it really necessary to do anything in this case?
1187 emit_op1(p
, OPCODE_MOV
, fog
, 0, input
);
1191 static void build_reflect_texgen( struct tnl_program
*p
,
1195 struct ureg normal
= get_eye_normal(p
);
1196 struct ureg eye_hat
= get_eye_position_normalized(p
);
1197 struct ureg tmp
= get_temp(p
);
1200 emit_op2(p
, OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1202 emit_op2(p
, OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1204 emit_op3(p
, OPCODE_MAD
, dest
, writemask
, ureg_negate(tmp
), normal
, eye_hat
);
1206 release_temp(p
, tmp
);
1209 static void build_sphere_texgen( struct tnl_program
*p
,
1213 struct ureg normal
= get_eye_normal(p
);
1214 struct ureg eye_hat
= get_eye_position_normalized(p
);
1215 struct ureg tmp
= get_temp(p
);
1216 struct ureg half
= register_scalar_const(p
, .5);
1217 struct ureg r
= get_temp(p
);
1218 struct ureg inv_m
= get_temp(p
);
1219 struct ureg id
= get_identity_param(p
);
1221 /* Could share the above calculations, but it would be
1222 * a fairly odd state for someone to set (both sphere and
1223 * reflection active for different texture coordinate
1224 * components. Of course - if two texture units enable
1225 * reflect and/or sphere, things start to tilt in favour
1226 * of seperating this out:
1230 emit_op2(p
, OPCODE_DP3
, tmp
, 0, normal
, eye_hat
);
1232 emit_op2(p
, OPCODE_ADD
, tmp
, 0, tmp
, tmp
);
1234 emit_op3(p
, OPCODE_MAD
, r
, 0, ureg_negate(tmp
), normal
, eye_hat
);
1236 emit_op2(p
, OPCODE_ADD
, tmp
, 0, r
, swizzle(id
,X
,Y
,W
,Z
));
1237 /* rx^2 + ry^2 + (rz+1)^2 */
1238 emit_op2(p
, OPCODE_DP3
, tmp
, 0, tmp
, tmp
);
1240 emit_op1(p
, OPCODE_RSQ
, tmp
, 0, tmp
);
1242 emit_op2(p
, OPCODE_MUL
, inv_m
, 0, tmp
, half
);
1244 emit_op3(p
, OPCODE_MAD
, dest
, writemask
, r
, inv_m
, half
);
1246 release_temp(p
, tmp
);
1248 release_temp(p
, inv_m
);
1252 static void build_texture_transform( struct tnl_program
*p
)
1256 for (i
= 0; i
< MAX_TEXTURE_UNITS
; i
++) {
1258 if (!(p
->state
->fragprog_inputs_read
& (FRAG_BIT_TEX0
<<i
)))
1261 if (p
->state
->unit
[i
].texgen_enabled
||
1262 p
->state
->unit
[i
].texmat_enabled
) {
1264 GLuint texmat_enabled
= p
->state
->unit
[i
].texmat_enabled
;
1265 struct ureg out
= register_output(p
, VERT_RESULT_TEX0
+ i
);
1266 struct ureg out_texgen
= undef
;
1268 if (p
->state
->unit
[i
].texgen_enabled
) {
1269 GLuint copy_mask
= 0;
1270 GLuint sphere_mask
= 0;
1271 GLuint reflect_mask
= 0;
1272 GLuint normal_mask
= 0;
1276 out_texgen
= get_temp(p
);
1280 modes
[0] = p
->state
->unit
[i
].texgen_mode0
;
1281 modes
[1] = p
->state
->unit
[i
].texgen_mode1
;
1282 modes
[2] = p
->state
->unit
[i
].texgen_mode2
;
1283 modes
[3] = p
->state
->unit
[i
].texgen_mode3
;
1285 for (j
= 0; j
< 4; j
++) {
1287 case TXG_OBJ_LINEAR
: {
1288 struct ureg obj
= register_input(p
, VERT_ATTRIB_POS
);
1290 register_param3(p
, STATE_TEXGEN
, i
,
1291 STATE_TEXGEN_OBJECT_S
+ j
);
1293 emit_op2(p
, OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1297 case TXG_EYE_LINEAR
: {
1298 struct ureg eye
= get_eye_position(p
);
1300 register_param3(p
, STATE_TEXGEN
, i
,
1301 STATE_TEXGEN_EYE_S
+ j
);
1303 emit_op2(p
, OPCODE_DP4
, out_texgen
, WRITEMASK_X
<< j
,
1307 case TXG_SPHERE_MAP
:
1308 sphere_mask
|= WRITEMASK_X
<< j
;
1310 case TXG_REFLECTION_MAP
:
1311 reflect_mask
|= WRITEMASK_X
<< j
;
1313 case TXG_NORMAL_MAP
:
1314 normal_mask
|= WRITEMASK_X
<< j
;
1317 copy_mask
|= WRITEMASK_X
<< j
;
1324 build_sphere_texgen(p
, out_texgen
, sphere_mask
);
1328 build_reflect_texgen(p
, out_texgen
, reflect_mask
);
1332 struct ureg normal
= get_eye_normal(p
);
1333 emit_op1(p
, OPCODE_MOV
, out_texgen
, normal_mask
, normal
);
1337 struct ureg in
= register_input(p
, VERT_ATTRIB_TEX0
+i
);
1338 emit_op1(p
, OPCODE_MOV
, out_texgen
, copy_mask
, in
);
1342 if (texmat_enabled
) {
1343 struct ureg texmat
[4];
1344 struct ureg in
= (!is_undef(out_texgen
) ?
1346 register_input(p
, VERT_ATTRIB_TEX0
+i
));
1348 register_matrix_param6( p
, STATE_MATRIX
, STATE_TEXTURE
, i
,
1349 0, 3, STATE_MATRIX
, texmat
);
1350 emit_matrix_transform_vec4( p
, out
, texmat
, in
);
1353 register_matrix_param6( p
, STATE_MATRIX
, STATE_TEXTURE
, i
,
1354 0, 3, STATE_MATRIX_TRANSPOSE
, texmat
);
1355 emit_transpose_matrix_transform_vec4( p
, out
, texmat
, in
);
1362 emit_passthrough(p
, VERT_ATTRIB_TEX0
+i
, VERT_RESULT_TEX0
+i
);
1368 /* Seems like it could be tighter:
1370 static void build_pointsize( struct tnl_program
*p
)
1372 struct ureg eye
= get_eye_position(p
);
1373 struct ureg state_size
= register_param1(p
, STATE_POINT_SIZE
);
1374 struct ureg state_attenuation
= register_param1(p
, STATE_POINT_ATTENUATION
);
1375 struct ureg out
= register_output(p
, VERT_RESULT_PSIZ
);
1376 struct ureg ut
= get_temp(p
);
1378 /* 1, -Z, Z * Z, 1 */
1379 emit_op1(p
, OPCODE_MOV
, ut
, 0, swizzle1(get_identity_param(p
), W
));
1380 emit_op2(p
, OPCODE_MUL
, ut
, WRITEMASK_YZ
, ut
, ureg_negate(swizzle1(eye
, Z
)));
1381 emit_op2(p
, OPCODE_MUL
, ut
, WRITEMASK_Z
, ut
, ureg_negate(swizzle1(eye
, Z
)));
1384 /* p1 + p2 * dist + p3 * dist * dist, 0 */
1385 emit_op2(p
, OPCODE_DP3
, ut
, 0, ut
, state_attenuation
);
1388 emit_op1(p
, OPCODE_RCP
, ut
, 0, ut
);
1390 /* out = pointSize / factor */
1391 emit_op2(p
, OPCODE_MUL
, out
, WRITEMASK_X
, ut
, state_size
);
1393 release_temp(p
, ut
);
1396 static void build_tnl_program( struct tnl_program
*p
)
1398 /* Emit the program, starting with modelviewproject:
1402 /* Lighting calculations:
1404 if (p
->state
->fragprog_inputs_read
& (FRAG_BIT_COL0
|FRAG_BIT_COL1
)) {
1405 if (p
->state
->light_global_enabled
)
1408 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL0
)
1409 emit_passthrough(p
, VERT_ATTRIB_COLOR0
, VERT_RESULT_COL0
);
1411 if (p
->state
->fragprog_inputs_read
& FRAG_BIT_COL1
)
1412 emit_passthrough(p
, VERT_ATTRIB_COLOR1
, VERT_RESULT_COL1
);
1416 if ((p
->state
->fragprog_inputs_read
& FRAG_BIT_FOGC
) ||
1417 p
->state
->fog_option
!= FOG_NONE
)
1420 if (p
->state
->fragprog_inputs_read
& FRAG_BITS_TEX_ANY
)
1421 build_texture_transform(p
);
1423 if (p
->state
->point_attenuated
)
1428 emit_op1(p
, OPCODE_END
, undef
, 0, undef
);
1433 _mesa_printf ("\n");
1438 static void build_new_tnl_program( const struct state_key
*key
,
1439 struct gl_vertex_program
*program
,
1442 struct tnl_program p
;
1444 _mesa_memset(&p
, 0, sizeof(p
));
1446 p
.program
= program
;
1447 p
.eye_position
= undef
;
1448 p
.eye_position_normalized
= undef
;
1449 p
.eye_normal
= undef
;
1453 if (max_temps
>= sizeof(int) * 8)
1454 p
.temp_reserved
= 0;
1456 p
.temp_reserved
= ~((1<<max_temps
)-1);
1458 p
.program
->Base
.Instructions
= MALLOC(sizeof(struct prog_instruction
) * BRW_TNL_MAX_INSN
);
1459 p
.program
->Base
.String
= 0;
1460 p
.program
->Base
.NumInstructions
=
1461 p
.program
->Base
.NumTemporaries
=
1462 p
.program
->Base
.NumParameters
=
1463 p
.program
->Base
.NumAttributes
= p
.program
->Base
.NumAddressRegs
= 0;
1464 p
.program
->Base
.Parameters
= _mesa_new_parameter_list();
1465 p
.program
->Base
.InputsRead
= 0;
1466 p
.program
->Base
.OutputsWritten
= 0;
1468 build_tnl_program( &p
);
1471 static void *search_cache( struct brw_tnl_cache
*cache
,
1476 struct brw_tnl_cache_item
*c
;
1478 for (c
= cache
->items
[hash
% cache
->size
]; c
; c
= c
->next
) {
1479 if (c
->hash
== hash
&& memcmp(c
->key
, key
, keysize
) == 0)
1486 static void rehash( struct brw_tnl_cache
*cache
)
1488 struct brw_tnl_cache_item
**items
;
1489 struct brw_tnl_cache_item
*c
, *next
;
1492 size
= cache
->size
* 3;
1493 items
= (struct brw_tnl_cache_item
**) _mesa_malloc(size
* sizeof(*items
));
1494 _mesa_memset(items
, 0, size
* sizeof(*items
));
1496 for (i
= 0; i
< cache
->size
; i
++)
1497 for (c
= cache
->items
[i
]; c
; c
= next
) {
1499 c
->next
= items
[c
->hash
% size
];
1500 items
[c
->hash
% size
] = c
;
1504 cache
->items
= items
;
1508 static void cache_item( struct brw_tnl_cache
*cache
,
1510 const struct state_key
*key
,
1513 struct brw_tnl_cache_item
*c
= MALLOC(sizeof(*c
));
1516 c
->key
= malloc(sizeof(*key
));
1517 memcpy(c
->key
, key
, sizeof(*key
));
1521 if (++cache
->n_items
> cache
->size
* 1.5)
1524 c
->next
= cache
->items
[hash
% cache
->size
];
1525 cache
->items
[hash
% cache
->size
] = c
;
1529 static GLuint
hash_key( struct state_key
*key
)
1531 GLuint
*ikey
= (GLuint
*)key
;
1534 /* I'm sure this can be improved on, but speed is important:
1536 for (i
= 0; i
< sizeof(*key
)/sizeof(GLuint
); i
++)
1542 static void update_tnl_program( struct brw_context
*brw
)
1544 GLcontext
*ctx
= &brw
->intel
.ctx
;
1545 struct state_key key
;
1547 struct gl_vertex_program
*old
= brw
->tnl_program
;
1550 if (brw
->attribs
.VertexProgram
->_Enabled
)
1553 /* Grab all the relevent state and put it in a single structure:
1555 make_state_key(ctx
, &key
);
1556 hash
= hash_key(&key
);
1558 /* Look for an already-prepared program for this state:
1560 brw
->tnl_program
= (struct gl_vertex_program
*)
1561 search_cache( &brw
->tnl_program_cache
, hash
, &key
, sizeof(key
) );
1563 /* OK, we'll have to build a new one:
1565 if (!brw
->tnl_program
) {
1566 brw
->tnl_program
= (struct gl_vertex_program
*)
1567 ctx
->Driver
.NewProgram(ctx
, GL_VERTEX_PROGRAM_ARB
, 0);
1569 build_new_tnl_program( &key
, brw
->tnl_program
,
1570 /* ctx->Const.MaxVertexProgramTemps */
1574 if (ctx
->Driver
.ProgramStringNotify
)
1575 ctx
->Driver
.ProgramStringNotify( ctx
, GL_VERTEX_PROGRAM_ARB
,
1576 &brw
->tnl_program
->Base
);
1578 cache_item( &brw
->tnl_program_cache
,
1579 hash
, &key
, brw
->tnl_program
);
1582 if (old
!= brw
->tnl_program
)
1583 brw
->state
.dirty
.brw
|= BRW_NEW_TNL_PROGRAM
;
1586 /* Note: See brw_draw.c - the vertex program must not rely on
1587 * brw->primitive or brw->reduced_prim.
1589 const struct brw_tracked_state brw_tnl_vertprog
= {
1591 .mesa
= (_NEW_PROGRAM
|
1598 .brw
= (BRW_NEW_FRAGMENT_PROGRAM
|
1599 BRW_NEW_INPUT_VARYING
),
1602 .update
= update_tnl_program
1608 static void update_active_vertprog( struct brw_context
*brw
)
1610 struct gl_vertex_program
*prev
= brw
->vertex_program
;
1613 if (brw
->attribs
.VertexProgram
->_Enabled
) {
1614 brw
->vertex_program
= brw
->attribs
.VertexProgram
->Current
;
1617 /* BRW_NEW_TNL_PROGRAM */
1618 brw
->vertex_program
= brw
->tnl_program
;
1621 if (brw
->vertex_program
!= prev
)
1622 brw
->state
.dirty
.brw
|= BRW_NEW_VERTEX_PROGRAM
;
1627 const struct brw_tracked_state brw_active_vertprog
= {
1629 .mesa
= _NEW_PROGRAM
,
1630 .brw
= BRW_NEW_TNL_PROGRAM
,
1633 .update
= update_active_vertprog
1637 void brw_ProgramCacheInit( GLcontext
*ctx
)
1639 struct brw_context
*brw
= brw_context(ctx
);
1641 brw
->tnl_program_cache
.size
= 17;
1642 brw
->tnl_program_cache
.n_items
= 0;
1643 brw
->tnl_program_cache
.items
= (struct brw_tnl_cache_item
**)
1644 _mesa_calloc(brw
->tnl_program_cache
.size
*
1645 sizeof(struct brw_tnl_cache_item
));
1648 void brw_ProgramCacheDestroy( GLcontext
*ctx
)
1650 struct brw_context
*brw
= brw_context(ctx
);
1651 struct brw_tnl_cache_item
*c
, *next
;
1654 for (i
= 0; i
< brw
->tnl_program_cache
.size
; i
++)
1655 for (c
= brw
->tnl_program_cache
.items
[i
]; c
; c
= next
) {
1662 FREE(brw
->tnl_program_cache
.items
);