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