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