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