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