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