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