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