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