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