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