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