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