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