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