Build vertex programs using state from a 'state key' struct which is
[mesa.git] / src / mesa / tnl / t_vp_build.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.3
4 *
5 * Copyright (C) 2005 Tungsten Graphics All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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,
21 * WHETHER IN
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.
24 */
25
26 /**
27 * \file t_vp_build.c
28 * Create a vertex program to execute the current fixed function T&L pipeline.
29 * \author Keith Whitwell
30 */
31
32
33 #include "glheader.h"
34 #include "macros.h"
35 #include "enums.h"
36 #include "t_context.h"
37 #include "t_vp_build.h"
38
39 #include "shader/program.h"
40 #include "shader/nvvertprog.h"
41 #include "shader/arbvertparse.h"
42
43 struct state_key {
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;
50
51 unsigned normalize:1;
52 unsigned rescale_normals:1;
53 unsigned fog_source_is_depth:1;
54 unsigned tnl_do_vertex_fog:1;
55 unsigned separate_specular:1;
56 unsigned fog_enabled:1;
57 unsigned fog_mode:2;
58 unsigned point_attenuated:1;
59 unsigned nr_tex:4;
60 unsigned texgen_enabled_global:1;
61
62 struct {
63 unsigned light_enabled:1;
64 unsigned light_eyepos3_is_zero:1;
65 unsigned light_spotcutoff_is_180:1;
66 unsigned light_attenuated:1;
67 unsigned texunit_really_enabled:1;
68 unsigned texmat_enabled:1;
69 unsigned texgen_enabled:4;
70 unsigned texgen_mode0:4;
71 unsigned texgen_mode1:4;
72 unsigned texgen_mode2:4;
73 unsigned texgen_mode3:4;
74 } unit[8];
75 };
76
77
78
79 #define FOG_LINEAR 0
80 #define FOG_EXP 1
81 #define FOG_EXP2 2
82 #define FOG_UNKNOWN 3
83
84 static GLuint translate_fog_mode( GLenum mode )
85 {
86 switch (mode) {
87 case GL_LINEAR: return FOG_LINEAR;
88 case GL_EXP: return FOG_EXP;
89 case GL_EXP2: return FOG_EXP2;
90 default: return FOG_UNKNOWN;
91 }
92 }
93
94 #define TXG_NONE 0
95 #define TXG_OBJ_LINEAR 1
96 #define TXG_EYE_LINEAR 2
97 #define TXG_SPHERE_MAP 3
98 #define TXG_REFLECTION_MAP 4
99 #define TXG_NORMAL_MAP 5
100
101 static GLuint translate_texgen( GLboolean enabled, GLenum mode )
102 {
103 if (!enabled)
104 return TXG_NONE;
105
106 switch (mode) {
107 case GL_OBJECT_LINEAR: return TXG_OBJ_LINEAR;
108 case GL_EYE_LINEAR: return TXG_EYE_LINEAR;
109 case GL_SPHERE_MAP: return TXG_SPHERE_MAP;
110 case GL_REFLECTION_MAP_NV: return TXG_REFLECTION_MAP;
111 case GL_NORMAL_MAP_NV: return TXG_NORMAL_MAP;
112 default: return TXG_NONE;
113 }
114 }
115
116 static struct state_key *make_state_key( GLcontext *ctx )
117 {
118 TNLcontext *tnl = TNL_CONTEXT(ctx);
119 struct vertex_buffer *VB = &tnl->vb;
120 struct state_key *key = CALLOC_STRUCT(state_key);
121 GLuint i;
122
123 key->separate_specular = (ctx->Light.Model.ColorControl ==
124 GL_SEPARATE_SPECULAR_COLOR);
125
126 if (ctx->Light.Enabled) {
127 key->light_global_enabled = 1;
128
129 if (ctx->Light.Model.LocalViewer)
130 key->light_local_viewer = 1;
131
132 if (ctx->Light.Model.TwoSide)
133 key->light_twoside = 1;
134
135 if (ctx->Light.ColorMaterialEnabled)
136 key->light_color_material = 1;
137
138 if (ctx->Light.ColorMaterialBitmask)
139 key->light_color_material_mask = 1;
140
141 for (i = _TNL_ATTRIB_MAT_FRONT_AMBIENT ; i < _TNL_ATTRIB_INDEX ; i++)
142 if (VB->AttribPtr[i]->stride)
143 key->light_material_mask |= 1<<i;
144
145 for (i = 0; i < MAX_LIGHTS; i++) {
146 struct gl_light *light = &ctx->Light.Light[i];
147
148 if (light->Enabled) {
149 key->unit[i].light_enabled = 1;
150
151 if (light->EyePosition[3] == 0.0)
152 key->unit[i].light_eyepos3_is_zero = 1;
153
154 if (light->SpotCutoff == 180.0)
155 key->unit[i].light_spotcutoff_is_180 = 1;
156
157 if (light->ConstantAttenuation != 1.0 ||
158 light->LinearAttenuation != 1.0 ||
159 light->QuadraticAttenuation != 1.0)
160 key->unit[i].light_attenuated = 1;
161 }
162 }
163 }
164
165 if (ctx->Transform.Normalize)
166 key->normalize = 1;
167
168 if (ctx->Transform.RescaleNormals)
169 key->rescale_normals = 1;
170
171 if (ctx->Fog.Enabled)
172 key->fog_enabled = 1;
173
174 if (key->fog_enabled) {
175 if (ctx->Fog.FogCoordinateSource == GL_FRAGMENT_DEPTH_EXT)
176 key->fog_source_is_depth = 1;
177
178 if (tnl->_DoVertexFog)
179 key->tnl_do_vertex_fog = 1;
180
181 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
182 }
183
184 if (ctx->Point._Attenuated)
185 key->point_attenuated = 1;
186
187 if (ctx->Texture._TexGenEnabled)
188 key->texgen_enabled_global = 1;
189
190 for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
191 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
192
193 if (texUnit->_ReallyEnabled)
194 key->unit[i].texunit_really_enabled = 1;
195
196 if (ctx->Texture._TexMatEnabled & ENABLE_TEXMAT(i))
197 key->unit[i].texmat_enabled = 1;
198
199 if (texUnit->TexGenEnabled) {
200 key->unit[i].texgen_enabled = 1;
201
202 key->unit[i].texgen_mode0 =
203 translate_texgen( texUnit->TexGenEnabled & (1<<0),
204 texUnit->GenModeS );
205 key->unit[i].texgen_mode1 =
206 translate_texgen( texUnit->TexGenEnabled & (1<<1),
207 texUnit->GenModeT );
208 key->unit[i].texgen_mode2 =
209 translate_texgen( texUnit->TexGenEnabled & (1<<2),
210 texUnit->GenModeR );
211 key->unit[i].texgen_mode3 =
212 translate_texgen( texUnit->TexGenEnabled & (1<<3),
213 texUnit->GenModeQ );
214 }
215 }
216
217 return key;
218 }
219
220
221
222 /* Very useful debugging tool - produces annotated listing of
223 * generated program with line/function references for each
224 * instruction back into this file:
225 */
226 #define DISASSEM 0
227
228 /* Should be tunable by the driver - do we want to do matrix
229 * multiplications with DP4's or with MUL/MAD's? SSE works better
230 * with the latter, drivers may differ.
231 */
232 #define PREFER_DP4 0
233
234 #define MAX_INSN 200
235
236 /* Use uregs to represent registers internally, translate to Mesa's
237 * expected formats on emit.
238 *
239 * NOTE: These are passed by value extensively in this file rather
240 * than as usual by pointer reference. If this disturbs you, try
241 * remembering they are just 32bits in size.
242 *
243 * GCC is smart enough to deal with these dword-sized structures in
244 * much the same way as if I had defined them as dwords and was using
245 * macros to access and set the fields. This is much nicer and easier
246 * to evolve.
247 */
248 struct ureg {
249 GLuint file:4;
250 GLuint idx:8;
251 GLuint negate:1;
252 GLuint swz:12;
253 GLuint pad:7;
254 };
255
256
257 struct tnl_program {
258 const struct state_key *state;
259 struct vertex_program *program;
260
261 GLuint temp_in_use;
262 GLuint temp_reserved;
263
264 struct ureg eye_position;
265 struct ureg eye_position_normalized;
266 struct ureg eye_normal;
267 struct ureg identity;
268
269 GLuint materials;
270 GLuint color_materials;
271 };
272
273
274 const static struct ureg undef = {
275 ~0,
276 ~0,
277 0,
278 0,
279 0
280 };
281
282 /* Local shorthand:
283 */
284 #define X SWIZZLE_X
285 #define Y SWIZZLE_Y
286 #define Z SWIZZLE_Z
287 #define W SWIZZLE_W
288
289
290 /* Construct a ureg:
291 */
292 static struct ureg make_ureg(GLuint file, GLuint idx)
293 {
294 struct ureg reg;
295 reg.file = file;
296 reg.idx = idx;
297 reg.negate = 0;
298 reg.swz = SWIZZLE_NOOP;
299 reg.pad = 0;
300 return reg;
301 }
302
303
304
305 static struct ureg negate( struct ureg reg )
306 {
307 reg.negate ^= 1;
308 return reg;
309 }
310
311
312 static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
313 {
314 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
315 GET_SWZ(reg.swz, y),
316 GET_SWZ(reg.swz, z),
317 GET_SWZ(reg.swz, w));
318
319 return reg;
320 }
321
322 static struct ureg swizzle1( struct ureg reg, int x )
323 {
324 return swizzle(reg, x, x, x, x);
325 }
326
327 static struct ureg get_temp( struct tnl_program *p )
328 {
329 int bit = ffs( ~p->temp_in_use );
330 if (!bit) {
331 fprintf(stderr, "%s: out of temporaries\n", __FILE__);
332 exit(1);
333 }
334
335 p->temp_in_use |= 1<<(bit-1);
336 return make_ureg(PROGRAM_TEMPORARY, bit-1);
337 }
338
339 static struct ureg reserve_temp( struct tnl_program *p )
340 {
341 struct ureg temp = get_temp( p );
342 p->temp_reserved |= 1<<temp.idx;
343 return temp;
344 }
345
346 static void release_temp( struct tnl_program *p, struct ureg reg )
347 {
348 if (reg.file == PROGRAM_TEMPORARY) {
349 p->temp_in_use &= ~(1<<reg.idx);
350 p->temp_in_use |= p->temp_reserved; /* can't release reserved temps */
351 }
352 }
353
354 static void release_temps( struct tnl_program *p )
355 {
356 p->temp_in_use = p->temp_reserved;
357 }
358
359
360
361 static struct ureg register_input( struct tnl_program *p, GLuint input )
362 {
363 p->program->InputsRead |= (1<<input);
364 return make_ureg(PROGRAM_INPUT, input);
365 }
366
367 static struct ureg register_output( struct tnl_program *p, GLuint output )
368 {
369 p->program->OutputsWritten |= (1<<output);
370 return make_ureg(PROGRAM_OUTPUT, output);
371 }
372
373 static struct ureg register_const4f( struct tnl_program *p,
374 GLfloat s0,
375 GLfloat s1,
376 GLfloat s2,
377 GLfloat s3)
378 {
379 GLfloat values[4];
380 GLuint idx;
381 values[0] = s0;
382 values[1] = s1;
383 values[2] = s2;
384 values[3] = s3;
385 idx = _mesa_add_unnamed_constant( p->program->Parameters, values );
386 return make_ureg(PROGRAM_STATE_VAR, idx);
387 }
388
389 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
390 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
391 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
392 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
393
394 static GLboolean is_undef( struct ureg reg )
395 {
396 return reg.file == 0xf;
397 }
398
399 static struct ureg get_identity_param( struct tnl_program *p )
400 {
401 if (is_undef(p->identity))
402 p->identity = register_const4f(p, 0,0,0,1);
403
404 return p->identity;
405 }
406
407 static struct ureg register_param6( struct tnl_program *p,
408 GLint s0,
409 GLint s1,
410 GLint s2,
411 GLint s3,
412 GLint s4,
413 GLint s5)
414 {
415 GLint tokens[6];
416 GLuint idx;
417 tokens[0] = s0;
418 tokens[1] = s1;
419 tokens[2] = s2;
420 tokens[3] = s3;
421 tokens[4] = s4;
422 tokens[5] = s5;
423 idx = _mesa_add_state_reference( p->program->Parameters, tokens );
424 return make_ureg(PROGRAM_STATE_VAR, idx);
425 }
426
427
428 #define register_param1(p,s0) register_param6(p,s0,0,0,0,0,0)
429 #define register_param2(p,s0,s1) register_param6(p,s0,s1,0,0,0,0)
430 #define register_param3(p,s0,s1,s2) register_param6(p,s0,s1,s2,0,0,0)
431 #define register_param4(p,s0,s1,s2,s3) register_param6(p,s0,s1,s2,s3,0,0)
432
433
434 static void register_matrix_param6( struct tnl_program *p,
435 GLint s0,
436 GLint s1,
437 GLint s2,
438 GLint s3,
439 GLint s4,
440 GLint s5,
441 struct ureg *matrix )
442 {
443 GLuint i;
444
445 /* This is a bit sad as the support is there to pull the whole
446 * matrix out in one go:
447 */
448 for (i = 0; i <= s4 - s3; i++)
449 matrix[i] = register_param6( p, s0, s1, s2, i, i, s5 );
450 }
451
452
453 static void emit_arg( struct vp_src_register *src,
454 struct ureg reg )
455 {
456 src->File = reg.file;
457 src->Index = reg.idx;
458 src->Swizzle = reg.swz;
459 src->Negate = reg.negate;
460 src->RelAddr = 0;
461 src->pad = 0;
462 }
463
464 static void emit_dst( struct vp_dst_register *dst,
465 struct ureg reg, GLuint mask )
466 {
467 dst->File = reg.file;
468 dst->Index = reg.idx;
469 /* allow zero as a shorthand for xyzw */
470 dst->WriteMask = mask ? mask : WRITEMASK_XYZW;
471 dst->pad = 0;
472 }
473
474 static void debug_insn( struct vp_instruction *inst, const char *fn,
475 GLuint line )
476 {
477 #if DISASSEM
478 static const char *last_fn;
479
480 if (fn != last_fn) {
481 last_fn = fn;
482 _mesa_printf("%s:\n", fn);
483 }
484
485 _mesa_printf("%d:\t", line);
486 _mesa_debug_vp_inst(1, inst);
487 #endif
488 }
489
490
491 static void emit_op3fn(struct tnl_program *p,
492 GLuint op,
493 struct ureg dest,
494 GLuint mask,
495 struct ureg src0,
496 struct ureg src1,
497 struct ureg src2,
498 const char *fn,
499 GLuint line)
500 {
501 GLuint nr = p->program->Base.NumInstructions++;
502 struct vp_instruction *inst = &p->program->Instructions[nr];
503
504 if (p->program->Base.NumInstructions > MAX_INSN) {
505 _mesa_problem(0, "Out of instructions in emit_op3fn\n");
506 return;
507 }
508
509 inst->Opcode = op;
510 inst->StringPos = 0;
511 inst->Data = 0;
512
513 emit_arg( &inst->SrcReg[0], src0 );
514 emit_arg( &inst->SrcReg[1], src1 );
515 emit_arg( &inst->SrcReg[2], src2 );
516
517 emit_dst( &inst->DstReg, dest, mask );
518
519 debug_insn(inst, fn, line);
520 }
521
522
523
524 #define emit_op3(p, op, dst, mask, src0, src1, src2) \
525 emit_op3fn(p, op, dst, mask, src0, src1, src2, __FUNCTION__, __LINE__)
526
527 #define emit_op2(p, op, dst, mask, src0, src1) \
528 emit_op3fn(p, op, dst, mask, src0, src1, undef, __FUNCTION__, __LINE__)
529
530 #define emit_op1(p, op, dst, mask, src0) \
531 emit_op3fn(p, op, dst, mask, src0, undef, undef, __FUNCTION__, __LINE__)
532
533
534 static struct ureg make_temp( struct tnl_program *p, struct ureg reg )
535 {
536 if (reg.file == PROGRAM_TEMPORARY &&
537 !(p->temp_reserved & (1<<reg.idx)))
538 return reg;
539 else {
540 struct ureg temp = get_temp(p);
541 emit_op1(p, VP_OPCODE_MOV, temp, 0, reg);
542 return temp;
543 }
544 }
545
546
547 /* Currently no tracking performed of input/output/register size or
548 * active elements. Could be used to reduce these operations, as
549 * could the matrix type.
550 */
551 static void emit_matrix_transform_vec4( struct tnl_program *p,
552 struct ureg dest,
553 const struct ureg *mat,
554 struct ureg src)
555 {
556 emit_op2(p, VP_OPCODE_DP4, dest, WRITEMASK_X, src, mat[0]);
557 emit_op2(p, VP_OPCODE_DP4, dest, WRITEMASK_Y, src, mat[1]);
558 emit_op2(p, VP_OPCODE_DP4, dest, WRITEMASK_Z, src, mat[2]);
559 emit_op2(p, VP_OPCODE_DP4, dest, WRITEMASK_W, src, mat[3]);
560 }
561
562 /* This version is much easier to implement if writemasks are not
563 * supported natively on the target or (like SSE), the target doesn't
564 * have a clean/obvious dotproduct implementation.
565 */
566 static void emit_transpose_matrix_transform_vec4( struct tnl_program *p,
567 struct ureg dest,
568 const struct ureg *mat,
569 struct ureg src)
570 {
571 struct ureg tmp;
572
573 if (dest.file != PROGRAM_TEMPORARY)
574 tmp = get_temp(p);
575 else
576 tmp = dest;
577
578 emit_op2(p, VP_OPCODE_MUL, tmp, 0, swizzle1(src,X), mat[0]);
579 emit_op3(p, VP_OPCODE_MAD, tmp, 0, swizzle1(src,Y), mat[1], tmp);
580 emit_op3(p, VP_OPCODE_MAD, tmp, 0, swizzle1(src,Z), mat[2], tmp);
581 emit_op3(p, VP_OPCODE_MAD, dest, 0, swizzle1(src,W), mat[3], tmp);
582
583 if (dest.file != PROGRAM_TEMPORARY)
584 release_temp(p, tmp);
585 }
586
587 static void emit_matrix_transform_vec3( struct tnl_program *p,
588 struct ureg dest,
589 const struct ureg *mat,
590 struct ureg src)
591 {
592 emit_op2(p, VP_OPCODE_DP3, dest, WRITEMASK_X, src, mat[0]);
593 emit_op2(p, VP_OPCODE_DP3, dest, WRITEMASK_Y, src, mat[1]);
594 emit_op2(p, VP_OPCODE_DP3, dest, WRITEMASK_Z, src, mat[2]);
595 }
596
597
598 static void emit_normalize_vec3( struct tnl_program *p,
599 struct ureg dest,
600 struct ureg src )
601 {
602 struct ureg tmp = get_temp(p);
603 emit_op2(p, VP_OPCODE_DP3, tmp, 0, src, src);
604 emit_op1(p, VP_OPCODE_RSQ, tmp, 0, tmp);
605 emit_op2(p, VP_OPCODE_MUL, dest, 0, src, tmp);
606 release_temp(p, tmp);
607 }
608
609 static void emit_passthrough( struct tnl_program *p,
610 GLuint input,
611 GLuint output )
612 {
613 struct ureg out = register_output(p, output);
614 emit_op1(p, VP_OPCODE_MOV, out, 0, register_input(p, input));
615 }
616
617 static struct ureg get_eye_position( struct tnl_program *p )
618 {
619 if (is_undef(p->eye_position)) {
620 struct ureg pos = register_input( p, VERT_ATTRIB_POS );
621 struct ureg modelview[4];
622
623 p->eye_position = reserve_temp(p);
624
625 if (PREFER_DP4) {
626 register_matrix_param6( p, STATE_MATRIX, STATE_MODELVIEW, 0, 0, 3,
627 STATE_MATRIX, modelview );
628
629 emit_matrix_transform_vec4(p, p->eye_position, modelview, pos);
630 }
631 else {
632 register_matrix_param6( p, STATE_MATRIX, STATE_MODELVIEW, 0, 0, 3,
633 STATE_MATRIX_TRANSPOSE, modelview );
634
635 emit_transpose_matrix_transform_vec4(p, p->eye_position, modelview, pos);
636 }
637 }
638
639 return p->eye_position;
640 }
641
642
643 static struct ureg get_eye_position_normalized( struct tnl_program *p )
644 {
645 if (is_undef(p->eye_position_normalized)) {
646 struct ureg eye = get_eye_position(p);
647 p->eye_position_normalized = reserve_temp(p);
648 emit_normalize_vec3(p, p->eye_position_normalized, eye);
649 }
650
651 return p->eye_position_normalized;
652 }
653
654
655 static struct ureg get_eye_normal( struct tnl_program *p )
656 {
657 if (is_undef(p->eye_normal)) {
658 struct ureg normal = register_input(p, VERT_ATTRIB_NORMAL );
659 struct ureg mvinv[3];
660
661 register_matrix_param6( p, STATE_MATRIX, STATE_MODELVIEW, 0, 0, 2,
662 STATE_MATRIX_INVTRANS, mvinv );
663
664 p->eye_normal = reserve_temp(p);
665
666 /* Transform to eye space:
667 */
668 emit_matrix_transform_vec3( p, p->eye_normal, mvinv, normal );
669
670 /* Normalize/Rescale:
671 */
672 if (p->state->normalize) {
673 emit_normalize_vec3( p, p->eye_normal, p->eye_normal );
674 }
675 else if (p->state->rescale_normals) {
676 struct ureg rescale = register_param2(p, STATE_INTERNAL,
677 STATE_NORMAL_SCALE);
678
679 emit_op2( p, VP_OPCODE_MUL, p->eye_normal, 0, normal,
680 swizzle1(rescale, X));
681 }
682 }
683
684 return p->eye_normal;
685 }
686
687
688
689 static void build_hpos( struct tnl_program *p )
690 {
691 struct ureg pos = register_input( p, VERT_ATTRIB_POS );
692 struct ureg hpos = register_output( p, VERT_RESULT_HPOS );
693 struct ureg mvp[4];
694
695 if (PREFER_DP4) {
696 register_matrix_param6( p, STATE_MATRIX, STATE_MVP, 0, 0, 3,
697 STATE_MATRIX, mvp );
698 emit_matrix_transform_vec4( p, hpos, mvp, pos );
699 }
700 else {
701 register_matrix_param6( p, STATE_MATRIX, STATE_MVP, 0, 0, 3,
702 STATE_MATRIX_TRANSPOSE, mvp );
703 emit_transpose_matrix_transform_vec4( p, hpos, mvp, pos );
704 }
705 }
706
707
708 static GLuint material_attrib( GLuint side, GLuint property )
709 {
710 return (_TNL_ATTRIB_MAT_FRONT_AMBIENT +
711 (property - STATE_AMBIENT) * 2 +
712 side);
713 }
714
715 static void set_material_flags( struct tnl_program *p )
716 {
717 p->color_materials = 0;
718 p->materials = 0;
719
720 if (p->state->light_color_material) {
721 p->materials =
722 p->color_materials = p->state->light_color_material_mask;
723 }
724
725 p->materials |= p->state->light_material_mask;
726 }
727
728
729 static struct ureg get_material( struct tnl_program *p, GLuint side,
730 GLuint property )
731 {
732 GLuint attrib = material_attrib(side, property);
733
734 if (p->color_materials & (1<<attrib))
735 return register_input(p, VERT_ATTRIB_COLOR0);
736 else if (p->materials & (1<<attrib))
737 return register_input( p, attrib );
738 else
739 return register_param3( p, STATE_MATERIAL, side, property );
740 }
741
742 #define SCENE_COLOR_BITS(side) (( _TNL_BIT_MAT_FRONT_EMISSION | \
743 _TNL_BIT_MAT_FRONT_AMBIENT | \
744 _TNL_BIT_MAT_FRONT_DIFFUSE) << (side))
745
746 /* Either return a precalculated constant value or emit code to
747 * calculate these values dynamically in the case where material calls
748 * are present between begin/end pairs.
749 *
750 * Probably want to shift this to the program compilation phase - if
751 * we always emitted the calculation here, a smart compiler could
752 * detect that it was constant (given a certain set of inputs), and
753 * lift it out of the main loop. That way the programs created here
754 * would be independent of the vertex_buffer details.
755 */
756 static struct ureg get_scenecolor( struct tnl_program *p, GLuint side )
757 {
758 if (p->materials & SCENE_COLOR_BITS(side)) {
759 struct ureg lm_ambient = register_param1(p, STATE_LIGHTMODEL_AMBIENT);
760 struct ureg material_emission = get_material(p, side, STATE_EMISSION);
761 struct ureg material_ambient = get_material(p, side, STATE_AMBIENT);
762 struct ureg material_diffuse = get_material(p, side, STATE_DIFFUSE);
763 struct ureg tmp = make_temp(p, material_diffuse);
764 emit_op3(p, VP_OPCODE_MAD, tmp, WRITEMASK_XYZ, lm_ambient,
765 material_ambient, material_emission);
766 return tmp;
767 }
768 else
769 return register_param2( p, STATE_LIGHTMODEL_SCENECOLOR, side );
770 }
771
772
773 static struct ureg get_lightprod( struct tnl_program *p, GLuint light,
774 GLuint side, GLuint property )
775 {
776 GLuint attrib = material_attrib(side, property);
777 if (p->materials & (1<<attrib)) {
778 struct ureg light_value =
779 register_param3(p, STATE_LIGHT, light, property);
780 struct ureg material_value = get_material(p, side, property);
781 struct ureg tmp = get_temp(p);
782 emit_op2(p, VP_OPCODE_MUL, tmp, 0, light_value, material_value);
783 return tmp;
784 }
785 else
786 return register_param4(p, STATE_LIGHTPROD, light, side, property);
787 }
788
789 static struct ureg calculate_light_attenuation( struct tnl_program *p,
790 GLuint i,
791 struct ureg VPpli,
792 struct ureg dist )
793 {
794 struct ureg attenuation = register_param3(p, STATE_LIGHT, i,
795 STATE_ATTENUATION);
796 struct ureg att = get_temp(p);
797
798 /* Calculate spot attenuation:
799 */
800 if (!p->state->unit[i].light_spotcutoff_is_180) {
801 struct ureg spot_dir = register_param3(p, STATE_LIGHT, i,
802 STATE_SPOT_DIRECTION);
803 struct ureg spot = get_temp(p);
804 struct ureg slt = get_temp(p);
805
806 emit_normalize_vec3( p, spot, spot_dir ); /* XXX: precompute! */
807 emit_op2(p, VP_OPCODE_DP3, spot, 0, negate(VPpli), spot_dir);
808 emit_op2(p, VP_OPCODE_SLT, slt, 0, swizzle1(spot_dir,W), spot);
809 emit_op2(p, VP_OPCODE_POW, spot, 0, spot, swizzle1(attenuation, W));
810 emit_op2(p, VP_OPCODE_MUL, att, 0, slt, spot);
811
812 release_temp(p, spot);
813 release_temp(p, slt);
814 }
815
816 /* Calculate distance attenuation:
817 */
818 if (p->state->unit[i].light_attenuated) {
819
820 /* 1/d,d,d,1/d */
821 emit_op1(p, VP_OPCODE_RCP, dist, WRITEMASK_YZ, dist);
822 /* 1,d,d*d,1/d */
823 emit_op2(p, VP_OPCODE_MUL, dist, WRITEMASK_XZ, dist, swizzle1(dist,Y));
824 /* 1/dist-atten */
825 emit_op2(p, VP_OPCODE_DP3, dist, 0, attenuation, dist);
826
827 if (!p->state->unit[i].light_spotcutoff_is_180) {
828 /* dist-atten */
829 emit_op1(p, VP_OPCODE_RCP, dist, 0, dist);
830 /* spot-atten * dist-atten */
831 emit_op2(p, VP_OPCODE_MUL, att, 0, dist, att);
832 } else {
833 /* dist-atten */
834 emit_op1(p, VP_OPCODE_RCP, att, 0, dist);
835 }
836 }
837
838 return att;
839 }
840
841
842
843
844
845 /* Need to add some addtional parameters to allow lighting in object
846 * space - STATE_SPOT_DIRECTION and STATE_HALF implicitly assume eye
847 * space lighting.
848 */
849 static void build_lighting( struct tnl_program *p )
850 {
851 const GLboolean twoside = p->state->light_twoside;
852 const GLboolean separate = p->state->separate_specular;
853 GLuint nr_lights = 0, count = 0;
854 struct ureg normal = get_eye_normal(p);
855 struct ureg lit = get_temp(p);
856 struct ureg dots = get_temp(p);
857 struct ureg _col0 = undef, _col1 = undef;
858 struct ureg _bfc0 = undef, _bfc1 = undef;
859 GLuint i;
860
861 for (i = 0; i < MAX_LIGHTS; i++)
862 if (p->state->unit[i].light_enabled)
863 nr_lights++;
864
865 set_material_flags(p);
866
867 {
868 struct ureg shininess = get_material(p, 0, STATE_SHININESS);
869 emit_op1(p, VP_OPCODE_MOV, dots, WRITEMASK_W, swizzle1(shininess,X));
870 release_temp(p, shininess);
871
872 _col0 = make_temp(p, get_scenecolor(p, 0));
873 if (separate)
874 _col1 = make_temp(p, get_identity_param(p));
875 else
876 _col1 = _col0;
877
878 }
879
880 if (twoside) {
881 struct ureg shininess = get_material(p, 1, STATE_SHININESS);
882 emit_op1(p, VP_OPCODE_MOV, dots, WRITEMASK_Z,
883 negate(swizzle1(shininess,X)));
884 release_temp(p, shininess);
885
886 _bfc0 = make_temp(p, get_scenecolor(p, 1));
887 if (separate)
888 _bfc1 = make_temp(p, get_identity_param(p));
889 else
890 _bfc1 = _bfc0;
891 }
892
893
894 /* If no lights, still need to emit the scenecolor.
895 */
896 if (nr_lights == 0) {
897 {
898 struct ureg res0 = register_output( p, VERT_RESULT_COL0 );
899 emit_op1(p, VP_OPCODE_MOV, res0, 0, _col0);
900 }
901
902 if (separate) {
903 struct ureg res1 = register_output( p, VERT_RESULT_COL1 );
904 emit_op1(p, VP_OPCODE_MOV, res1, 0, _col1);
905 }
906
907 if (twoside) {
908 struct ureg res0 = register_output( p, VERT_RESULT_BFC0 );
909 emit_op1(p, VP_OPCODE_MOV, res0, 0, _bfc0);
910 }
911
912 if (twoside && separate) {
913 struct ureg res1 = register_output( p, VERT_RESULT_BFC1 );
914 emit_op1(p, VP_OPCODE_MOV, res1, 0, _bfc1);
915 }
916
917 release_temps(p);
918 return;
919 }
920
921
922 for (i = 0; i < MAX_LIGHTS; i++) {
923 if (p->state->unit[i].light_enabled) {
924 struct ureg half = undef;
925 struct ureg att = undef, VPpli = undef;
926
927 count++;
928
929 if (p->state->unit[i].light_eyepos3_is_zero) {
930 /* Can used precomputed constants in this case.
931 * Attenuation never applies to infinite lights.
932 */
933 VPpli = register_param3(p, STATE_LIGHT, i,
934 STATE_POSITION_NORMALIZED);
935 half = register_param3(p, STATE_LIGHT, i, STATE_HALF);
936 }
937 else {
938 struct ureg Ppli = register_param3(p, STATE_LIGHT, i,
939 STATE_POSITION);
940 struct ureg V = get_eye_position(p);
941 struct ureg dist = get_temp(p);
942
943 VPpli = get_temp(p);
944 half = get_temp(p);
945
946 /* Calulate VPpli vector
947 */
948 emit_op2(p, VP_OPCODE_SUB, VPpli, 0, Ppli, V);
949
950 /* Normalize VPpli. The dist value also used in
951 * attenuation below.
952 */
953 emit_op2(p, VP_OPCODE_DP3, dist, 0, VPpli, VPpli);
954 emit_op1(p, VP_OPCODE_RSQ, dist, 0, dist);
955 emit_op2(p, VP_OPCODE_MUL, VPpli, 0, VPpli, dist);
956
957
958 /* Calculate attenuation:
959 */
960 if (!p->state->unit[i].light_spotcutoff_is_180 ||
961 p->state->unit[i].light_attenuated) {
962 att = calculate_light_attenuation(p, i, VPpli, dist);
963 }
964
965
966 /* Calculate viewer direction, or use infinite viewer:
967 */
968 if (p->state->light_local_viewer) {
969 struct ureg eye_hat = get_eye_position_normalized(p);
970 emit_op2(p, VP_OPCODE_SUB, half, 0, VPpli, eye_hat);
971 }
972 else {
973 struct ureg z_dir = swizzle(get_identity_param(p),X,Y,W,Z);
974 emit_op2(p, VP_OPCODE_ADD, half, 0, VPpli, z_dir);
975 }
976
977 emit_normalize_vec3(p, half, half);
978
979 release_temp(p, dist);
980 }
981
982 /* Calculate dot products:
983 */
984 emit_op2(p, VP_OPCODE_DP3, dots, WRITEMASK_X, normal, VPpli);
985 emit_op2(p, VP_OPCODE_DP3, dots, WRITEMASK_Y, normal, half);
986
987
988 /* Front face lighting:
989 */
990 {
991 struct ureg ambient = get_lightprod(p, i, 0, STATE_AMBIENT);
992 struct ureg diffuse = get_lightprod(p, i, 0, STATE_DIFFUSE);
993 struct ureg specular = get_lightprod(p, i, 0, STATE_SPECULAR);
994 struct ureg res0, res1;
995
996 emit_op1(p, VP_OPCODE_LIT, lit, 0, dots);
997
998 if (!is_undef(att))
999 emit_op2(p, VP_OPCODE_MUL, lit, 0, lit, att);
1000
1001
1002 if (count == nr_lights) {
1003 if (separate) {
1004 res0 = register_output( p, VERT_RESULT_COL0 );
1005 res1 = register_output( p, VERT_RESULT_COL1 );
1006 }
1007 else {
1008 res0 = _col0;
1009 res1 = register_output( p, VERT_RESULT_COL0 );
1010 }
1011 } else {
1012 res0 = _col0;
1013 res1 = _col1;
1014 }
1015
1016 emit_op3(p, VP_OPCODE_MAD, _col0, 0, swizzle1(lit,X), ambient, _col0);
1017 emit_op3(p, VP_OPCODE_MAD, res0, 0, swizzle1(lit,Y), diffuse, _col0);
1018 emit_op3(p, VP_OPCODE_MAD, res1, 0, swizzle1(lit,Z), specular, _col1);
1019
1020 release_temp(p, ambient);
1021 release_temp(p, diffuse);
1022 release_temp(p, specular);
1023 }
1024
1025 /* Back face lighting:
1026 */
1027 if (twoside) {
1028 struct ureg ambient = get_lightprod(p, i, 1, STATE_AMBIENT);
1029 struct ureg diffuse = get_lightprod(p, i, 1, STATE_DIFFUSE);
1030 struct ureg specular = get_lightprod(p, i, 1, STATE_SPECULAR);
1031 struct ureg res0, res1;
1032
1033 emit_op1(p, VP_OPCODE_LIT, lit, 0, negate(swizzle(dots,X,Y,W,Z)));
1034
1035 if (!is_undef(att))
1036 emit_op2(p, VP_OPCODE_MUL, lit, 0, lit, att);
1037
1038 if (count == nr_lights) {
1039 if (separate) {
1040 res0 = register_output( p, VERT_RESULT_BFC0 );
1041 res1 = register_output( p, VERT_RESULT_BFC1 );
1042 }
1043 else {
1044 res0 = _bfc0;
1045 res1 = register_output( p, VERT_RESULT_BFC0 );
1046 }
1047 } else {
1048 res0 = _bfc0;
1049 res1 = _bfc1;
1050 }
1051
1052
1053 emit_op3(p, VP_OPCODE_MAD, _bfc0, 0, swizzle1(lit,X), ambient, _bfc0);
1054 emit_op3(p, VP_OPCODE_MAD, res0, 0, swizzle1(lit,Y), diffuse, _bfc0);
1055 emit_op3(p, VP_OPCODE_MAD, res1, 0, swizzle1(lit,Z), specular, _bfc1);
1056
1057 release_temp(p, ambient);
1058 release_temp(p, diffuse);
1059 release_temp(p, specular);
1060 }
1061
1062 release_temp(p, half);
1063 release_temp(p, VPpli);
1064 release_temp(p, att);
1065 }
1066 }
1067
1068 release_temps( p );
1069 }
1070
1071
1072 static void build_fog( struct tnl_program *p )
1073 {
1074 struct ureg fog = register_output(p, VERT_RESULT_FOGC);
1075 struct ureg input;
1076
1077 if (p->state->fog_source_is_depth) {
1078 input = swizzle1(get_eye_position(p), Z);
1079 }
1080 else {
1081 input = swizzle1(register_input(p, VERT_ATTRIB_FOG), X);
1082 }
1083
1084 if (p->state->tnl_do_vertex_fog) {
1085 struct ureg params = register_param1(p, STATE_FOG_PARAMS);
1086 struct ureg tmp = get_temp(p);
1087
1088 switch (p->state->fog_mode) {
1089 case FOG_LINEAR: {
1090 struct ureg id = get_identity_param(p);
1091 emit_op2(p, VP_OPCODE_SUB, tmp, 0, swizzle1(params,Z), input);
1092 emit_op2(p, VP_OPCODE_MUL, tmp, 0, tmp, swizzle1(params,W));
1093 emit_op2(p, VP_OPCODE_MAX, tmp, 0, tmp, swizzle1(id,X)); /* saturate */
1094 emit_op2(p, VP_OPCODE_MIN, fog, WRITEMASK_X, tmp, swizzle1(id,W));
1095 break;
1096 }
1097 case FOG_EXP:
1098 emit_op1(p, VP_OPCODE_ABS, tmp, 0, input);
1099 emit_op2(p, VP_OPCODE_MUL, tmp, 0, tmp, swizzle1(params,X));
1100 emit_op2(p, VP_OPCODE_POW, fog, WRITEMASK_X,
1101 register_const1f(p, M_E), negate(tmp));
1102 break;
1103 case FOG_EXP2:
1104 emit_op2(p, VP_OPCODE_MUL, tmp, 0, input, swizzle1(params,X));
1105 emit_op2(p, VP_OPCODE_MUL, tmp, 0, tmp, tmp);
1106 emit_op2(p, VP_OPCODE_POW, fog, WRITEMASK_X,
1107 register_const1f(p, M_E), negate(tmp));
1108 break;
1109 }
1110
1111 release_temp(p, tmp);
1112 }
1113 else {
1114 /* results = incoming fog coords (compute fog per-fragment later)
1115 *
1116 * KW: Is it really necessary to do anything in this case?
1117 */
1118 emit_op1(p, VP_OPCODE_MOV, fog, WRITEMASK_X, input);
1119 }
1120 }
1121
1122 static void build_reflect_texgen( struct tnl_program *p,
1123 struct ureg dest,
1124 GLuint writemask )
1125 {
1126 struct ureg normal = get_eye_normal(p);
1127 struct ureg eye_hat = get_eye_position_normalized(p);
1128 struct ureg tmp = get_temp(p);
1129
1130 /* n.u */
1131 emit_op2(p, VP_OPCODE_DP3, tmp, 0, normal, eye_hat);
1132 /* 2n.u */
1133 emit_op2(p, VP_OPCODE_ADD, tmp, 0, tmp, tmp);
1134 /* (-2n.u)n + u */
1135 emit_op3(p, VP_OPCODE_MAD, dest, writemask, negate(tmp), normal, eye_hat);
1136 }
1137
1138 static void build_sphere_texgen( struct tnl_program *p,
1139 struct ureg dest,
1140 GLuint writemask )
1141 {
1142 struct ureg normal = get_eye_normal(p);
1143 struct ureg eye_hat = get_eye_position_normalized(p);
1144 struct ureg tmp = get_temp(p);
1145 struct ureg half = register_scalar_const(p, .5);
1146 struct ureg r = get_temp(p);
1147 struct ureg inv_m = get_temp(p);
1148 struct ureg id = get_identity_param(p);
1149
1150 /* Could share the above calculations, but it would be
1151 * a fairly odd state for someone to set (both sphere and
1152 * reflection active for different texture coordinate
1153 * components. Of course - if two texture units enable
1154 * reflect and/or sphere, things start to tilt in favour
1155 * of seperating this out:
1156 */
1157
1158 /* n.u */
1159 emit_op2(p, VP_OPCODE_DP3, tmp, 0, normal, eye_hat);
1160 /* 2n.u */
1161 emit_op2(p, VP_OPCODE_ADD, tmp, 0, tmp, tmp);
1162 /* (-2n.u)n + u */
1163 emit_op3(p, VP_OPCODE_MAD, r, 0, negate(tmp), normal, eye_hat);
1164 /* r + 0,0,1 */
1165 emit_op2(p, VP_OPCODE_ADD, tmp, 0, r, swizzle(id,X,Y,W,Z));
1166 /* rx^2 + ry^2 + (rz+1)^2 */
1167 emit_op2(p, VP_OPCODE_DP3, tmp, 0, tmp, tmp);
1168 /* 2/m */
1169 emit_op1(p, VP_OPCODE_RSQ, tmp, 0, tmp);
1170 /* 1/m */
1171 emit_op2(p, VP_OPCODE_MUL, inv_m, 0, tmp, half);
1172 /* r/m + 1/2 */
1173 emit_op3(p, VP_OPCODE_MAD, dest, writemask, r, inv_m, half);
1174
1175 release_temp(p, tmp);
1176 release_temp(p, r);
1177 release_temp(p, inv_m);
1178 }
1179
1180
1181 static void build_texture_transform( struct tnl_program *p )
1182 {
1183 GLuint i, j;
1184
1185 for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
1186 GLuint texmat_enabled = p->state->unit[i].texmat_enabled;
1187
1188 if (p->state->unit[i].texgen_enabled || texmat_enabled) {
1189 struct ureg out = register_output(p, VERT_RESULT_TEX0 + i);
1190 struct ureg out_texgen = undef;
1191
1192 if (p->state->unit[i].texgen_enabled) {
1193 GLuint copy_mask = 0;
1194 GLuint sphere_mask = 0;
1195 GLuint reflect_mask = 0;
1196 GLuint normal_mask = 0;
1197 GLuint modes[4];
1198
1199 if (texmat_enabled)
1200 out_texgen = get_temp(p);
1201 else
1202 out_texgen = out;
1203
1204 modes[0] = p->state->unit[i].texgen_mode0;
1205 modes[1] = p->state->unit[i].texgen_mode1;
1206 modes[2] = p->state->unit[i].texgen_mode2;
1207 modes[3] = p->state->unit[i].texgen_mode3;
1208
1209 for (j = 0; j < 4; j++) {
1210 switch (modes[j]) {
1211 case TXG_OBJ_LINEAR: {
1212 struct ureg obj = register_input(p, VERT_ATTRIB_POS);
1213 struct ureg plane =
1214 register_param3(p, STATE_TEXGEN, i,
1215 STATE_TEXGEN_OBJECT_S + j);
1216
1217 emit_op2(p, VP_OPCODE_DP4, out_texgen, WRITEMASK_X << j,
1218 obj, plane );
1219 break;
1220 }
1221 case TXG_EYE_LINEAR: {
1222 struct ureg eye = get_eye_position(p);
1223 struct ureg plane =
1224 register_param3(p, STATE_TEXGEN, i,
1225 STATE_TEXGEN_EYE_S + j);
1226
1227 emit_op2(p, VP_OPCODE_DP4, out_texgen, WRITEMASK_X << j,
1228 eye, plane );
1229 break;
1230 }
1231 case TXG_SPHERE_MAP:
1232 sphere_mask |= WRITEMASK_X << j;
1233 break;
1234 case TXG_REFLECTION_MAP:
1235 reflect_mask |= WRITEMASK_X << j;
1236 break;
1237 case TXG_NORMAL_MAP:
1238 normal_mask |= WRITEMASK_X << j;
1239 break;
1240 case TXG_NONE:
1241 copy_mask |= WRITEMASK_X << j;
1242 }
1243
1244 }
1245
1246
1247 if (sphere_mask) {
1248 build_sphere_texgen(p, out_texgen, sphere_mask);
1249 }
1250
1251 if (reflect_mask) {
1252 build_reflect_texgen(p, out_texgen, reflect_mask);
1253 }
1254
1255 if (normal_mask) {
1256 struct ureg normal = get_eye_normal(p);
1257 emit_op1(p, VP_OPCODE_MOV, out_texgen, normal_mask, normal );
1258 }
1259
1260 if (copy_mask) {
1261 struct ureg in = register_input(p, VERT_ATTRIB_TEX0+i);
1262 emit_op1(p, VP_OPCODE_MOV, out_texgen, copy_mask, in );
1263 }
1264 }
1265
1266 if (texmat_enabled) {
1267 struct ureg texmat[4];
1268 struct ureg in = (!is_undef(out_texgen) ?
1269 out_texgen :
1270 register_input(p, VERT_ATTRIB_TEX0+i));
1271 if (PREFER_DP4) {
1272 register_matrix_param6( p, STATE_MATRIX, STATE_TEXTURE, i,
1273 0, 3, STATE_MATRIX, texmat );
1274 emit_matrix_transform_vec4( p, out, texmat, in );
1275 }
1276 else {
1277 register_matrix_param6( p, STATE_MATRIX, STATE_TEXTURE, i,
1278 0, 3, STATE_MATRIX_TRANSPOSE, texmat );
1279 emit_matrix_transform_vec4( p, out, texmat, in );
1280 }
1281 }
1282
1283 release_temps(p);
1284 }
1285 else if (p->state->unit[i].texunit_really_enabled) {
1286 /* KW: _ReallyEnabled isn't sufficient? Need to know whether
1287 * this texture unit is referenced by the fragment shader.
1288 */
1289 emit_passthrough(p, VERT_ATTRIB_TEX0+i, VERT_RESULT_TEX0+i);
1290 }
1291 }
1292 }
1293
1294
1295 /* Seems like it could be tighter:
1296 */
1297 static void build_pointsize( struct tnl_program *p )
1298 {
1299 struct ureg eye = get_eye_position(p);
1300 struct ureg state_size = register_param1(p, STATE_POINT_SIZE);
1301 struct ureg state_attenuation = register_param1(p, STATE_POINT_ATTENUATION);
1302 struct ureg out = register_output(p, VERT_RESULT_PSIZ);
1303 struct ureg ut = get_temp(p);
1304
1305 /* 1, -Z, Z * Z, 1 */
1306 emit_op1(p, VP_OPCODE_MOV, ut, 0, swizzle1(get_identity_param(p), W));
1307 emit_op2(p, VP_OPCODE_MUL, ut, WRITEMASK_YZ, ut, negate(swizzle1(eye, Z)));
1308 emit_op2(p, VP_OPCODE_MUL, ut, WRITEMASK_Z, ut, negate(swizzle1(eye, Z)));
1309
1310
1311 /* p1 + p2 * dist + p3 * dist * dist, 0 */
1312 emit_op2(p, VP_OPCODE_DP3, ut, 0, ut, state_attenuation);
1313
1314 /* 1 / factor */
1315 emit_op1(p, VP_OPCODE_RCP, ut, 0, ut );
1316
1317 /* out = pointSize / factor */
1318 emit_op2(p, VP_OPCODE_MUL, out, WRITEMASK_X, ut, state_size);
1319
1320 release_temp(p, ut);
1321 }
1322
1323 static void build_tnl_program( struct tnl_program *p )
1324 { /* Emit the program, starting with modelviewproject:
1325 */
1326 build_hpos(p);
1327
1328 /* Lighting calculations:
1329 */
1330 if (p->state->light_global_enabled)
1331 build_lighting(p);
1332 else
1333 emit_passthrough(p, VERT_ATTRIB_COLOR0, VERT_RESULT_COL0);
1334
1335 if (p->state->fog_enabled)
1336 build_fog(p);
1337
1338 if (p->state->nr_tex)
1339 build_texture_transform(p);
1340
1341 if (p->state->point_attenuated)
1342 build_pointsize(p);
1343
1344 /* Finish up:
1345 */
1346 emit_op1(p, VP_OPCODE_END, undef, 0, undef);
1347
1348 /* Disassemble:
1349 */
1350 if (DISASSEM) {
1351 _mesa_printf ("\n");
1352 }
1353 }
1354
1355
1356 void create_new_program( const struct state_key *key,
1357 struct vertex_program *program,
1358 GLuint max_temps)
1359 {
1360 struct tnl_program p;
1361
1362 _mesa_memset(&p, 0, sizeof(p));
1363 p.state = key;
1364 p.program = program;
1365 p.eye_position = undef;
1366 p.eye_position_normalized = undef;
1367 p.eye_normal = undef;
1368 p.identity = undef;
1369 p.temp_in_use = 0;
1370
1371 if (max_temps >= sizeof(int) * 8)
1372 p.temp_reserved = 0;
1373 else
1374 p.temp_reserved = ~((1<<max_temps)-1);
1375
1376 p.program->Instructions = MALLOC(sizeof(struct vp_instruction) * MAX_INSN);
1377 p.program->Base.String = 0;
1378 p.program->Base.NumInstructions =
1379 p.program->Base.NumTemporaries =
1380 p.program->Base.NumParameters =
1381 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
1382 p.program->Parameters = _mesa_new_parameter_list();
1383 p.program->InputsRead = 0;
1384 p.program->OutputsWritten = 0;
1385
1386 build_tnl_program( &p );
1387 }
1388
1389 static void *search_cache( struct tnl_cache *cache,
1390 GLuint hash,
1391 const void *key,
1392 GLuint keysize)
1393 {
1394 struct tnl_cache *c;
1395
1396 for (c = cache; c; c = c->next) {
1397 if (c->hash == hash && memcmp(c->key, key, keysize) == 0)
1398 return c->data;
1399 }
1400
1401 return NULL;
1402 }
1403
1404 static void cache_item( struct tnl_cache **cache,
1405 GLuint hash,
1406 void *key,
1407 void *data )
1408 {
1409 struct tnl_cache *c = MALLOC(sizeof(*c));
1410 c->hash = hash;
1411 c->key = key;
1412 c->data = data;
1413 c->next = *cache;
1414 *cache = c;
1415 }
1416
1417 static GLuint hash_key( struct state_key *key )
1418 {
1419 GLuint *ikey = (GLuint *)key;
1420 GLuint hash = 0, i;
1421
1422 /* I'm sure this can be improved on, but speed is important:
1423 */
1424 for (i = 0; i < sizeof(*key)/sizeof(GLuint); i++)
1425 hash ^= ikey[i];
1426
1427 return hash;
1428 }
1429
1430 void _tnl_UpdateFixedFunctionProgram( GLcontext *ctx )
1431 {
1432 TNLcontext *tnl = TNL_CONTEXT(ctx);
1433 struct state_key *key;
1434 GLuint hash;
1435
1436 if (ctx->VertexProgram._Enabled)
1437 return;
1438
1439 /* Grab all the relevent state and put it in a single structure:
1440 */
1441 key = make_state_key(ctx);
1442 hash = hash_key(key);
1443
1444 /* Look for an already-prepared program for this state:
1445 */
1446 ctx->_TnlProgram = (struct vertex_program *)
1447 search_cache( tnl->vp_cache, hash, key, sizeof(key) );
1448
1449 /* OK, we'll have to build a new one:
1450 */
1451 if (!ctx->_TnlProgram) {
1452 if (0)
1453 _mesa_printf("Build new TNL program\n");
1454
1455 ctx->_TnlProgram = (struct vertex_program *)
1456 ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0);
1457
1458 create_new_program( key, ctx->_TnlProgram,
1459 ctx->Const.MaxVertexProgramTemps );
1460
1461 cache_item(&tnl->vp_cache, hash, key, ctx->_TnlProgram );
1462 }
1463 else {
1464 if (0)
1465 _mesa_printf("Found existing TNL program for key %x\n", hash);
1466 }
1467
1468 /* Need a BindProgram callback for the driver?
1469 */
1470 }
1471
1472
1473 void _tnl_ProgramCacheDestroy( GLcontext *ctx )
1474 {
1475 TNLcontext *tnl = TNL_CONTEXT(ctx);
1476 struct tnl_cache *a, *tmp;
1477
1478 for (a = tnl->vp_cache ; a; a = tmp) {
1479 tmp = a->next;
1480 FREE(a->key);
1481 FREE(a->data);
1482 FREE(a);
1483 }
1484 }