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