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