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