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