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