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