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