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