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