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