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