Debian Codegraph @

nx-X11/extras/Mesa/src/mesa/tnl/t_vb_arbprogram.c

raw
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.4.2
4 *
5 * Copyright (C) 1999-2006 Brian Paul 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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file t_arb_program.c
27 * Compile vertex programs to an intermediate representation.
28 * Execute vertex programs over a buffer of vertices.
29 * \author Keith Whitwell, Brian Paul
30 */
31
32 #include "glheader.h"
33 #include "context.h"
34 #include "imports.h"
35 #include "macros.h"
36 #include "mtypes.h"
37 #include "arbprogparse.h"
38 #include "light.h"
39 #include "program.h"
40 #include "math/m_matrix.h"
41 #include "math/m_translate.h"
42 #include "t_context.h"
43 #include "t_pipeline.h"
44 #include "t_vb_arbprogram.h"
45 #include "tnl.h"
46
47
48 #define DISASSEM 0
49
50 /*--------------------------------------------------------------------------- */
51
52 struct opcode_info {
53 GLuint nr_args;
54 const char *string;
55 void (*print)( union instruction , const struct opcode_info * );
56 };
57
58 struct compilation {
59 GLuint reg_active;
60 union instruction *csr;
61 };
62
63
64 #define ARB_VP_MACHINE(stage) ((struct arb_vp_machine *)(stage->privatePtr))
65
66 #define PUFF(x) ((x)[1] = (x)[2] = (x)[3] = (x)[0])
67
68
69
70 /* Lower precision functions for the EXP, LOG and LIT opcodes. The
71 * LOG2() implementation is probably not accurate enough, and the
72 * attempted optimization for Exp2 is definitely not accurate
73 * enough - it discards all of t's fractional bits!
74 */
75 static GLfloat RoughApproxLog2(GLfloat t)
76 {
77 return LOG2(t);
78 }
79
80 static GLfloat RoughApproxExp2(GLfloat t)
81 {
82 #if 0
83 fi_type fi;
84 fi.i = (GLint) t;
85 fi.i = (fi.i << 23) + 0x3f800000;
86 return fi.f;
87 #else
88 return (GLfloat) _mesa_pow(2.0, t);
89 #endif
90 }
91
92 static GLfloat RoughApproxPower(GLfloat x, GLfloat y)
93 {
94 if (x == 0.0 && y == 0.0)
95 return 1.0; /* spec requires this */
96 else
97 return RoughApproxExp2(y * RoughApproxLog2(x));
98 }
99
100
101 /* Higher precision functions for the EX2, LG2 and POW opcodes:
102 */
103 static GLfloat ApproxLog2(GLfloat t)
104 {
105 return (GLfloat) (log(t) * 1.442695F);
106 }
107
108 static GLfloat ApproxExp2(GLfloat t)
109 {
110 return (GLfloat) _mesa_pow(2.0, t);
111 }
112
113 static GLfloat ApproxPower(GLfloat x, GLfloat y)
114 {
115 return (GLfloat) _mesa_pow(x, y);
116 }
117
118 static GLfloat rough_approx_log2_0_1(GLfloat x)
119 {
120 return LOG2(x);
121 }
122
123
124
125
126 /**
127 * Perform a reduced swizzle:
128 */
129 static void do_RSW( struct arb_vp_machine *m, union instruction op )
130 {
131 GLfloat *result = m->File[0][op.rsw.dst];
132 const GLfloat *arg0 = m->File[op.rsw.file0][op.rsw.idx0];
133 GLuint swz = op.rsw.swz;
134 GLuint neg = op.rsw.neg;
135 GLfloat tmp[4];
136
137 /* Need a temporary to be correct in the case where result == arg0.
138 */
139 COPY_4V(tmp, arg0);
140
141 result[0] = tmp[GET_RSW(swz, 0)];
142 result[1] = tmp[GET_RSW(swz, 1)];
143 result[2] = tmp[GET_RSW(swz, 2)];
144 result[3] = tmp[GET_RSW(swz, 3)];
145
146 if (neg) {
147 if (neg & 0x1) result[0] = -result[0];
148 if (neg & 0x2) result[1] = -result[1];
149 if (neg & 0x4) result[2] = -result[2];
150 if (neg & 0x8) result[3] = -result[3];
151 }
152 }
153
154 /* Used to implement write masking. To make things easier for the sse
155 * generator I've gone back to a 1 argument version of this function
156 * (dst.msk = arg), rather than the semantically cleaner (dst = SEL
157 * arg0, arg1, msk)
158 *
159 * That means this is the only instruction which doesn't write a full
160 * 4 dwords out. This would make such a program harder to analyse,
161 * but it looks like analysis is going to take place on a higher level
162 * anyway.
163 */
164 static void do_MSK( struct arb_vp_machine *m, union instruction op )
165 {
166 GLfloat *dst = m->File[0][op.msk.dst];
167 const GLfloat *arg = m->File[op.msk.file][op.msk.idx];
168
169 if (op.msk.mask & 0x1) dst[0] = arg[0];
170 if (op.msk.mask & 0x2) dst[1] = arg[1];
171 if (op.msk.mask & 0x4) dst[2] = arg[2];
172 if (op.msk.mask & 0x8) dst[3] = arg[3];
173 }
174
175
176 static void do_PRT( struct arb_vp_machine *m, union instruction op )
177 {
178 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
179
180 _mesa_printf("%d: %f %f %f %f\n", m->vtx_nr,
181 arg0[0], arg0[1], arg0[2], arg0[3]);
182 }
183
184
185 /**
186 * The traditional ALU and texturing instructions. All operate on
187 * internal registers and ignore write masks and swizzling issues.
188 */
189
190 static void do_ABS( struct arb_vp_machine *m, union instruction op )
191 {
192 GLfloat *result = m->File[0][op.alu.dst];
193 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
194
195 result[0] = (arg0[0] < 0.0) ? -arg0[0] : arg0[0];
196 result[1] = (arg0[1] < 0.0) ? -arg0[1] : arg0[1];
197 result[2] = (arg0[2] < 0.0) ? -arg0[2] : arg0[2];
198 result[3] = (arg0[3] < 0.0) ? -arg0[3] : arg0[3];
199 }
200
201 static void do_ADD( struct arb_vp_machine *m, union instruction op )
202 {
203 GLfloat *result = m->File[0][op.alu.dst];
204 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
205 const GLfloat *arg1 = m->File[op.alu.file1][op.alu.idx1];
206
207 result[0] = arg0[0] + arg1[0];
208 result[1] = arg0[1] + arg1[1];
209 result[2] = arg0[2] + arg1[2];
210 result[3] = arg0[3] + arg1[3];
211 }
212
213
214 static void do_DP3( struct arb_vp_machine *m, union instruction op )
215 {
216 GLfloat *result = m->File[0][op.alu.dst];
217 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
218 const GLfloat *arg1 = m->File[op.alu.file1][op.alu.idx1];
219
220 result[0] = (arg0[0] * arg1[0] +
221 arg0[1] * arg1[1] +
222 arg0[2] * arg1[2]);
223
224 PUFF(result);
225 }
226
227
228
229 static void do_DP4( struct arb_vp_machine *m, union instruction op )
230 {
231 GLfloat *result = m->File[0][op.alu.dst];
232 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
233 const GLfloat *arg1 = m->File[op.alu.file1][op.alu.idx1];
234
235 result[0] = (arg0[0] * arg1[0] +
236 arg0[1] * arg1[1] +
237 arg0[2] * arg1[2] +
238 arg0[3] * arg1[3]);
239
240 PUFF(result);
241 }
242
243 static void do_DPH( struct arb_vp_machine *m, union instruction op )
244 {
245 GLfloat *result = m->File[0][op.alu.dst];
246 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
247 const GLfloat *arg1 = m->File[op.alu.file1][op.alu.idx1];
248
249 result[0] = (arg0[0] * arg1[0] +
250 arg0[1] * arg1[1] +
251 arg0[2] * arg1[2] +
252 1.0 * arg1[3]);
253
254 PUFF(result);
255 }
256
257 static void do_DST( struct arb_vp_machine *m, union instruction op )
258 {
259 GLfloat *result = m->File[0][op.alu.dst];
260 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
261 const GLfloat *arg1 = m->File[op.alu.file1][op.alu.idx1];
262
263 /* This should be ok even if result == arg0 or result == arg1.
264 */
265 result[0] = 1.0F;
266 result[1] = arg0[1] * arg1[1];
267 result[2] = arg0[2];
268 result[3] = arg1[3];
269 }
270
271
272 /* Intended to be high precision:
273 */
274 static void do_EX2( struct arb_vp_machine *m, union instruction op )
275 {
276 GLfloat *result = m->File[0][op.alu.dst];
277 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
278
279 result[0] = (GLfloat)ApproxExp2(arg0[0]);
280 PUFF(result);
281 }
282
283
284 /* Allowed to be lower precision:
285 */
286 static void do_EXP( struct arb_vp_machine *m, union instruction op )
287 {
288 GLfloat *result = m->File[0][op.alu.dst];
289 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
290 GLfloat tmp = arg0[0];
291 GLfloat flr_tmp = FLOORF(tmp);
292 GLfloat frac_tmp = tmp - flr_tmp;
293
294 result[0] = LDEXPF(1.0, (int)flr_tmp);
295 result[1] = frac_tmp;
296 result[2] = LDEXPF(rough_approx_log2_0_1(frac_tmp), (int)flr_tmp);
297 result[3] = 1.0F;
298 }
299
300 static void do_FLR( struct arb_vp_machine *m, union instruction op )
301 {
302 GLfloat *result = m->File[0][op.alu.dst];
303 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
304
305 result[0] = FLOORF(arg0[0]);
306 result[1] = FLOORF(arg0[1]);
307 result[2] = FLOORF(arg0[2]);
308 result[3] = FLOORF(arg0[3]);
309 }
310
311 static void do_FRC( struct arb_vp_machine *m, union instruction op )
312 {
313 GLfloat *result = m->File[0][op.alu.dst];
314 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
315
316 result[0] = arg0[0] - FLOORF(arg0[0]);
317 result[1] = arg0[1] - FLOORF(arg0[1]);
318 result[2] = arg0[2] - FLOORF(arg0[2]);
319 result[3] = arg0[3] - FLOORF(arg0[3]);
320 }
321
322 /* High precision log base 2:
323 */
324 static void do_LG2( struct arb_vp_machine *m, union instruction op )
325 {
326 GLfloat *result = m->File[0][op.alu.dst];
327 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
328
329 result[0] = ApproxLog2(arg0[0]);
330 PUFF(result);
331 }
332
333
334
335 static void do_LIT( struct arb_vp_machine *m, union instruction op )
336 {
337 GLfloat *result = m->File[0][op.alu.dst];
338 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
339 GLfloat tmp[4];
340
341 tmp[0] = 1.0;
342 tmp[1] = arg0[0];
343 if (arg0[0] > 0.0) {
344 tmp[2] = RoughApproxPower(arg0[1], arg0[3]);
345 }
346 else {
347 tmp[2] = 0.0;
348 }
349 tmp[3] = 1.0;
350
351
352 COPY_4V(result, tmp);
353 }
354
355
356 /* Intended to allow a lower precision than required for LG2 above.
357 */
358 static void do_LOG( struct arb_vp_machine *m, union instruction op )
359 {
360 GLfloat *result = m->File[0][op.alu.dst];
361 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
362 GLfloat tmp = FABSF(arg0[0]);
363 int exponent;
364 GLfloat mantissa = FREXPF(tmp, &exponent);
365
366 result[0] = (GLfloat) (exponent - 1);
367 result[1] = 2.0 * mantissa; /* map [.5, 1) -> [1, 2) */
368 result[2] = exponent + LOG2(mantissa);
369 result[3] = 1.0;
370 }
371
372 static void do_MAX( struct arb_vp_machine *m, union instruction op )
373 {
374 GLfloat *result = m->File[0][op.alu.dst];
375 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
376 const GLfloat *arg1 = m->File[op.alu.file1][op.alu.idx1];
377
378 result[0] = (arg0[0] > arg1[0]) ? arg0[0] : arg1[0];
379 result[1] = (arg0[1] > arg1[1]) ? arg0[1] : arg1[1];
380 result[2] = (arg0[2] > arg1[2]) ? arg0[2] : arg1[2];
381 result[3] = (arg0[3] > arg1[3]) ? arg0[3] : arg1[3];
382 }
383
384
385 static void do_MIN( struct arb_vp_machine *m, union instruction op )
386 {
387 GLfloat *result = m->File[0][op.alu.dst];
388 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
389 const GLfloat *arg1 = m->File[op.alu.file1][op.alu.idx1];
390
391 result[0] = (arg0[0] < arg1[0]) ? arg0[0] : arg1[0];
392 result[1] = (arg0[1] < arg1[1]) ? arg0[1] : arg1[1];
393 result[2] = (arg0[2] < arg1[2]) ? arg0[2] : arg1[2];
394 result[3] = (arg0[3] < arg1[3]) ? arg0[3] : arg1[3];
395 }
396
397 static void do_MOV( struct arb_vp_machine *m, union instruction op )
398 {
399 GLfloat *result = m->File[0][op.alu.dst];
400 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
401
402 result[0] = arg0[0];
403 result[1] = arg0[1];
404 result[2] = arg0[2];
405 result[3] = arg0[3];
406 }
407
408 static void do_MUL( struct arb_vp_machine *m, union instruction op )
409 {
410 GLfloat *result = m->File[0][op.alu.dst];
411 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
412 const GLfloat *arg1 = m->File[op.alu.file1][op.alu.idx1];
413
414 result[0] = arg0[0] * arg1[0];
415 result[1] = arg0[1] * arg1[1];
416 result[2] = arg0[2] * arg1[2];
417 result[3] = arg0[3] * arg1[3];
418 }
419
420
421 /* Intended to be "high" precision
422 */
423 static void do_POW( struct arb_vp_machine *m, union instruction op )
424 {
425 GLfloat *result = m->File[0][op.alu.dst];
426 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
427 const GLfloat *arg1 = m->File[op.alu.file1][op.alu.idx1];
428
429 result[0] = (GLfloat)ApproxPower(arg0[0], arg1[0]);
430 PUFF(result);
431 }
432
433 static void do_REL( struct arb_vp_machine *m, union instruction op )
434 {
435 GLfloat *result = m->File[0][op.alu.dst];
436 GLuint idx = (op.alu.idx0 + (GLint)m->File[0][REG_ADDR][0]) & (MAX_NV_VERTEX_PROGRAM_PARAMS-1);
437 const GLfloat *arg0 = m->File[op.alu.file0][idx];
438
439 result[0] = arg0[0];
440 result[1] = arg0[1];
441 result[2] = arg0[2];
442 result[3] = arg0[3];
443 }
444
445 static void do_RCP( struct arb_vp_machine *m, union instruction op )
446 {
447 GLfloat *result = m->File[0][op.alu.dst];
448 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
449
450 result[0] = 1.0F / arg0[0];
451 PUFF(result);
452 }
453
454 static void do_RSQ( struct arb_vp_machine *m, union instruction op )
455 {
456 GLfloat *result = m->File[0][op.alu.dst];
457 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
458
459 result[0] = INV_SQRTF(FABSF(arg0[0]));
460 PUFF(result);
461 }
462
463
464 static void do_SGE( struct arb_vp_machine *m, union instruction op )
465 {
466 GLfloat *result = m->File[0][op.alu.dst];
467 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
468 const GLfloat *arg1 = m->File[op.alu.file1][op.alu.idx1];
469
470 result[0] = (arg0[0] >= arg1[0]) ? 1.0F : 0.0F;
471 result[1] = (arg0[1] >= arg1[1]) ? 1.0F : 0.0F;
472 result[2] = (arg0[2] >= arg1[2]) ? 1.0F : 0.0F;
473 result[3] = (arg0[3] >= arg1[3]) ? 1.0F : 0.0F;
474 }
475
476
477 static void do_SLT( struct arb_vp_machine *m, union instruction op )
478 {
479 GLfloat *result = m->File[0][op.alu.dst];
480 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
481 const GLfloat *arg1 = m->File[op.alu.file1][op.alu.idx1];
482
483 result[0] = (arg0[0] < arg1[0]) ? 1.0F : 0.0F;
484 result[1] = (arg0[1] < arg1[1]) ? 1.0F : 0.0F;
485 result[2] = (arg0[2] < arg1[2]) ? 1.0F : 0.0F;
486 result[3] = (arg0[3] < arg1[3]) ? 1.0F : 0.0F;
487 }
488
489 static void do_SUB( struct arb_vp_machine *m, union instruction op )
490 {
491 GLfloat *result = m->File[0][op.alu.dst];
492 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
493 const GLfloat *arg1 = m->File[op.alu.file1][op.alu.idx1];
494
495 result[0] = arg0[0] - arg1[0];
496 result[1] = arg0[1] - arg1[1];
497 result[2] = arg0[2] - arg1[2];
498 result[3] = arg0[3] - arg1[3];
499 }
500
501
502 static void do_XPD( struct arb_vp_machine *m, union instruction op )
503 {
504 GLfloat *result = m->File[0][op.alu.dst];
505 const GLfloat *arg0 = m->File[op.alu.file0][op.alu.idx0];
506 const GLfloat *arg1 = m->File[op.alu.file1][op.alu.idx1];
507 GLfloat tmp[3];
508
509 tmp[0] = arg0[1] * arg1[2] - arg0[2] * arg1[1];
510 tmp[1] = arg0[2] * arg1[0] - arg0[0] * arg1[2];
511 tmp[2] = arg0[0] * arg1[1] - arg0[1] * arg1[0];
512
513 /* Need a temporary to be correct in the case where result == arg0
514 * or result == arg1.
515 */
516 result[0] = tmp[0];
517 result[1] = tmp[1];
518 result[2] = tmp[2];
519 }
520
521 static void do_NOP( struct arb_vp_machine *m, union instruction op )
522 {
523 }
524
525 /* Some useful debugging functions:
526 */
527 static void print_mask( GLuint mask )
528 {
529 _mesa_printf(".");
530 if (mask&0x1) _mesa_printf("x");
531 if (mask&0x2) _mesa_printf("y");
532 if (mask&0x4) _mesa_printf("z");
533 if (mask&0x8) _mesa_printf("w");
534 }
535
536 static void print_reg( GLuint file, GLuint reg )
537 {
538 static const char *reg_file[] = {
539 "REG",
540 "LOCAL_PARAM",
541 "ENV_PARAM",
542 "STATE_VAR",
543 };
544
545 if (file == 0) {
546 if (reg == REG_RES)
547 _mesa_printf("RES");
548 else if (reg >= REG_ARG0 && reg <= REG_ARG1)
549 _mesa_printf("ARG%d", reg - REG_ARG0);
550 else if (reg >= REG_TMP0 && reg <= REG_TMP11)
551 _mesa_printf("TMP%d", reg - REG_TMP0);
552 else if (reg >= REG_IN0 && reg <= REG_IN31)
553 _mesa_printf("IN%d", reg - REG_IN0);
554 else if (reg >= REG_OUT0 && reg <= REG_OUT14)
555 _mesa_printf("OUT%d", reg - REG_OUT0);
556 else if (reg == REG_ADDR)
557 _mesa_printf("ADDR");
558 else if (reg == REG_ID)
559 _mesa_printf("ID");
560 else
561 _mesa_printf("REG%d", reg);
562 }
563 else
564 _mesa_printf("%s:%d", reg_file[file], reg);
565 }
566
567
568 static void print_RSW( union instruction op, const struct opcode_info *info )
569 {
570 GLuint swz = op.rsw.swz;
571 GLuint neg = op.rsw.neg;
572 GLuint i;
573
574 _mesa_printf("%s ", info->string);
575 print_reg(0, op.rsw.dst);
576 _mesa_printf(", ");
577 print_reg(op.rsw.file0, op.rsw.idx0);
578 _mesa_printf(".");
579 for (i = 0; i < 4; i++, swz >>= 2) {
580 const char *cswz = "xyzw";
581 if (neg & (1<<i))
582 _mesa_printf("-");
583 _mesa_printf("%c", cswz[swz&0x3]);
584 }
585 _mesa_printf("\n");
586 }
587
588
589 static void print_ALU( union instruction op, const struct opcode_info *info )
590 {
591 _mesa_printf("%s ", info->string);
592 print_reg(0, op.alu.dst);
593 _mesa_printf(", ");
594 print_reg(op.alu.file0, op.alu.idx0);
595 if (info->nr_args > 1) {
596 _mesa_printf(", ");
597 print_reg(op.alu.file1, op.alu.idx1);
598 }
599 _mesa_printf("\n");
600 }
601
602 static void print_MSK( union instruction op, const struct opcode_info *info )
603 {
604 _mesa_printf("%s ", info->string);
605 print_reg(0, op.msk.dst);
606 print_mask(op.msk.mask);
607 _mesa_printf(", ");
608 print_reg(op.msk.file, op.msk.idx);
609 _mesa_printf("\n");
610 }
611
612
613 static void print_NOP( union instruction op, const struct opcode_info *info )
614 {
615 }
616
617 #define NOP 0
618 #define ALU 1
619 #define SWZ 2
620
621 static const struct opcode_info opcode_info[] =
622 {
623 { 1, "ABS", print_ALU },
624 { 2, "ADD", print_ALU },
625 { 1, "ARL", print_NOP },
626 { 2, "DP3", print_ALU },
627 { 2, "DP4", print_ALU },
628 { 2, "DPH", print_ALU },
629 { 2, "DST", print_ALU },
630 { 0, "END", print_NOP },
631 { 1, "EX2", print_ALU },
632 { 1, "EXP", print_ALU },
633 { 1, "FLR", print_ALU },
634 { 1, "FRC", print_ALU },
635 { 1, "LG2", print_ALU },
636 { 1, "LIT", print_ALU },
637 { 1, "LOG", print_ALU },
638 { 3, "MAD", print_NOP },
639 { 2, "MAX", print_ALU },
640 { 2, "MIN", print_ALU },
641 { 1, "MOV", print_ALU },
642 { 2, "MUL", print_ALU },
643 { 2, "POW", print_ALU },
644 { 1, "PRT", print_ALU }, /* PRINT */
645 { 1, "RCC", print_NOP },
646 { 1, "RCP", print_ALU },
647 { 1, "RSQ", print_ALU },
648 { 2, "SGE", print_ALU },
649 { 2, "SLT", print_ALU },
650 { 2, "SUB", print_ALU },
651 { 1, "SWZ", print_NOP },
652 { 2, "XPD", print_ALU },
653 { 1, "RSW", print_RSW },
654 { 2, "MSK", print_MSK },
655 { 1, "REL", print_ALU },
656 };
657
658 void _tnl_disassem_vba_insn( union instruction op )
659 {
660 const struct opcode_info *info = &opcode_info[op.alu.opcode];
661 info->print( op, info );
662 }
663
664
665 static void (* const opcode_func[])(struct arb_vp_machine *, union instruction) =
666 {
667 do_ABS,
668 do_ADD,
669 do_NOP,
670 do_DP3,
671 do_DP4,
672 do_DPH,
673 do_DST,
674 do_NOP,
675 do_EX2,
676 do_EXP,
677 do_FLR,
678 do_FRC,
679 do_LG2,
680 do_LIT,
681 do_LOG,
682 do_NOP,
683 do_MAX,
684 do_MIN,
685 do_MOV,
686 do_MUL,
687 do_POW,
688 do_PRT,
689 do_NOP,
690 do_RCP,
691 do_RSQ,
692 do_SGE,
693 do_SLT,
694 do_SUB,
695 do_RSW,
696 do_XPD,
697 do_RSW,
698 do_MSK,
699 do_REL,
700 };
701
702 static union instruction *cvp_next_instruction( struct compilation *cp )
703 {
704 union instruction *op = cp->csr++;
705 op->dword = 0;
706 return op;
707 }
708
709 static struct reg cvp_make_reg( GLuint file, GLuint idx )
710 {
711 struct reg reg;
712 reg.file = file;
713 reg.idx = idx;
714 return reg;
715 }
716
717 static struct reg cvp_emit_rel( struct compilation *cp,
718 struct reg reg,
719 struct reg tmpreg )
720 {
721 union instruction *op = cvp_next_instruction(cp);
722 op->alu.opcode = REL;
723 op->alu.file0 = reg.file;
724 op->alu.idx0 = reg.idx;
725 op->alu.dst = tmpreg.idx;
726 return tmpreg;
727 }
728
729
730 static struct reg cvp_load_reg( struct compilation *cp,
731 GLuint file,
732 GLuint index,
733 GLuint rel,
734 GLuint tmpidx )
735 {
736 struct reg tmpreg = cvp_make_reg(FILE_REG, tmpidx);
737 struct reg reg;
738
739 switch (file) {
740 case PROGRAM_TEMPORARY:
741 return cvp_make_reg(FILE_REG, REG_TMP0 + index);
742
743 case PROGRAM_INPUT:
744 return cvp_make_reg(FILE_REG, REG_IN0 + index);
745
746 case PROGRAM_OUTPUT:
747 return cvp_make_reg(FILE_REG, REG_OUT0 + index);
748
749 /* These two aren't populated by the parser?
750 */
751 case PROGRAM_LOCAL_PARAM:
752 reg = cvp_make_reg(FILE_LOCAL_PARAM, index);
753 if (rel)
754 return cvp_emit_rel(cp, reg, tmpreg);
755 else
756 return reg;
757
758 case PROGRAM_ENV_PARAM:
759 reg = cvp_make_reg(FILE_ENV_PARAM, index);
760 if (rel)
761 return cvp_emit_rel(cp, reg, tmpreg);
762 else
763 return reg;
764
765 case PROGRAM_STATE_VAR:
766 reg = cvp_make_reg(FILE_STATE_PARAM, index);
767 if (rel)
768 return cvp_emit_rel(cp, reg, tmpreg);
769 else
770 return reg;
771
772 /* Invalid values:
773 */
774 case PROGRAM_WRITE_ONLY:
775 case PROGRAM_ADDRESS:
776 default:
777 assert(0);
778 return tmpreg; /* can't happen */
779 }
780 }
781
782 static struct reg cvp_emit_arg( struct compilation *cp,
783 const struct vp_src_register *src,
784 GLuint arg )
785 {
786 struct reg reg = cvp_load_reg( cp, src->File, src->Index, src->RelAddr, arg );
787 union instruction rsw, noop;
788
789 /* Emit any necessary swizzling.
790 */
791 rsw.dword = 0;
792 rsw.rsw.neg = src->Negate ? WRITEMASK_XYZW : 0;
793 rsw.rsw.swz = ((GET_SWZ(src->Swizzle, 0) << 0) |
794 (GET_SWZ(src->Swizzle, 1) << 2) |
795 (GET_SWZ(src->Swizzle, 2) << 4) |
796 (GET_SWZ(src->Swizzle, 3) << 6));
797
798 noop.dword = 0;
799 noop.rsw.neg = 0;
800 noop.rsw.swz = RSW_NOOP;
801
802 if (rsw.dword != noop.dword) {
803 union instruction *op = cvp_next_instruction(cp);
804 struct reg rsw_reg = cvp_make_reg(FILE_REG, REG_ARG0 + arg);
805 op->dword = rsw.dword;
806 op->rsw.opcode = RSW;
807 op->rsw.file0 = reg.file;
808 op->rsw.idx0 = reg.idx;
809 op->rsw.dst = rsw_reg.idx;
810 return rsw_reg;
811 }
812 else
813 return reg;
814 }
815
816 static GLuint cvp_choose_result( struct compilation *cp,
817 const struct vp_dst_register *dst,
818 union instruction *fixup )
819 {
820 GLuint mask = dst->WriteMask;
821 GLuint idx;
822
823 switch (dst->File) {
824 case PROGRAM_TEMPORARY:
825 idx = REG_TMP0 + dst->Index;
826 break;
827 case PROGRAM_OUTPUT:
828 idx = REG_OUT0 + dst->Index;
829 break;
830 default:
831 assert(0);
832 return REG_RES; /* can't happen */
833 }
834
835 /* Optimization: When writing (with a writemask) to an undefined
836 * value for the first time, the writemask may be ignored.
837 */
838 if (mask != WRITEMASK_XYZW && (cp->reg_active & (1 << idx))) {
839 fixup->msk.opcode = MSK;
840 fixup->msk.dst = idx;
841 fixup->msk.file = FILE_REG;
842 fixup->msk.idx = REG_RES;
843 fixup->msk.mask = mask;
844 cp->reg_active |= 1 << idx;
845 return REG_RES;
846 }
847 else {
848 fixup->dword = 0;
849 cp->reg_active |= 1 << idx;
850 return idx;
851 }
852 }
853
854 static struct reg cvp_emit_rsw( struct compilation *cp,
855 GLuint dst,
856 struct reg src,
857 GLuint neg,
858 GLuint swz,
859 GLboolean force)
860 {
861 struct reg retval;
862
863 if (swz != RSW_NOOP || neg != 0) {
864 union instruction *op = cvp_next_instruction(cp);
865 op->rsw.opcode = RSW;
866 op->rsw.dst = dst;
867 op->rsw.file0 = src.file;
868 op->rsw.idx0 = src.idx;
869 op->rsw.neg = neg;
870 op->rsw.swz = swz;
871
872 retval.file = FILE_REG;
873 retval.idx = dst;
874 return retval;
875 }
876 else if (force) {
877 /* Oops. Degenerate case:
878 */
879 union instruction *op = cvp_next_instruction(cp);
880 op->alu.opcode = VP_OPCODE_MOV;
881 op->alu.dst = dst;
882 op->alu.file0 = src.file;
883 op->alu.idx0 = src.idx;
884
885 retval.file = FILE_REG;
886 retval.idx = dst;
887 return retval;
888 }
889 else {
890 return src;
891 }
892 }
893
894
895 static void cvp_emit_inst( struct compilation *cp,
896 const struct vp_instruction *inst )
897 {
898 const struct opcode_info *info = &opcode_info[inst->Opcode];
899 union instruction *op;
900 union instruction fixup;
901 struct reg reg[3];
902 GLuint result, i;
903
904 assert(sizeof(*op) == sizeof(GLuint));
905
906 /* Need to handle SWZ, ARL specially.
907 */
908 switch (inst->Opcode) {
909 /* Split into mul and add:
910 */
911 case VP_OPCODE_MAD:
912 result = cvp_choose_result( cp, &inst->DstReg, &fixup );
913 for (i = 0; i < 3; i++)
914 reg[i] = cvp_emit_arg( cp, &inst->SrcReg[i], REG_ARG0+i );
915
916 op = cvp_next_instruction(cp);
917 op->alu.opcode = VP_OPCODE_MUL;
918 op->alu.file0 = reg[0].file;
919 op->alu.idx0 = reg[0].idx;
920 op->alu.file1 = reg[1].file;
921 op->alu.idx1 = reg[1].idx;
922 op->alu.dst = REG_ARG0;
923
924 op = cvp_next_instruction(cp);
925 op->alu.opcode = VP_OPCODE_ADD;
926 op->alu.file0 = FILE_REG;
927 op->alu.idx0 = REG_ARG0;
928 op->alu.file1 = reg[2].file;
929 op->alu.idx1 = reg[2].idx;
930 op->alu.dst = result;
931
932 if (result == REG_RES) {
933 op = cvp_next_instruction(cp);
934 op->dword = fixup.dword;
935 }
936 break;
937
938 case VP_OPCODE_ARL:
939 reg[0] = cvp_emit_arg( cp, &inst->SrcReg[0], REG_ARG0 );
940
941 op = cvp_next_instruction(cp);
942 op->alu.opcode = VP_OPCODE_FLR;
943 op->alu.dst = REG_ADDR;
944 op->alu.file0 = reg[0].file;
945 op->alu.idx0 = reg[0].idx;
946 break;
947
948 case VP_OPCODE_SWZ: {
949 GLuint swz0 = 0, swz1 = 0;
950 GLuint neg0 = 0, neg1 = 0;
951 GLuint mask = 0;
952
953 /* Translate 3-bit-per-element swizzle into two 2-bit swizzles,
954 * one from the source register the other from a constant
955 * {0,0,0,1}.
956 */
957 for (i = 0; i < 4; i++) {
958 GLuint swzelt = GET_SWZ(inst->SrcReg[0].Swizzle, i);
959 if (swzelt >= SWIZZLE_ZERO) {
960 neg0 |= inst->SrcReg[0].Negate & (1<<i);
961 if (swzelt == SWIZZLE_ONE)
962 swz0 |= SWIZZLE_W << (i*2);
963 else if (i < SWIZZLE_W)
964 swz0 |= i << (i*2);
965 }
966 else {
967 mask |= 1<<i;
968 neg1 |= inst->SrcReg[0].Negate & (1<<i);
969 swz1 |= swzelt << (i*2);
970 }
971 }
972
973 result = cvp_choose_result( cp, &inst->DstReg, &fixup );
974 reg[0].file = FILE_REG;
975 reg[0].idx = REG_ID;
976 reg[1] = cvp_emit_arg( cp, &inst->SrcReg[0], REG_ARG0 );
977
978 if (mask == WRITEMASK_XYZW) {
979 cvp_emit_rsw(cp, result, reg[0], neg0, swz0, GL_TRUE);
980
981 }
982 else if (mask == 0) {
983 cvp_emit_rsw(cp, result, reg[1], neg1, swz1, GL_TRUE);
984 }
985 else {
986 cvp_emit_rsw(cp, result, reg[0], neg0, swz0, GL_TRUE);
987 reg[1] = cvp_emit_rsw(cp, REG_ARG0, reg[1], neg1, swz1, GL_FALSE);
988
989 op = cvp_next_instruction(cp);
990 op->msk.opcode = MSK;
991 op->msk.dst = result;
992 op->msk.file = reg[1].file;
993 op->msk.idx = reg[1].idx;
994 op->msk.mask = mask;
995 }
996
997 if (result == REG_RES) {
998 op = cvp_next_instruction(cp);
999 op->dword = fixup.dword;
1000 }
1001 break;
1002 }
1003
1004 case VP_OPCODE_END:
1005 break;
1006
1007 default:
1008 result = cvp_choose_result( cp, &inst->DstReg, &fixup );
1009 for (i = 0; i < info->nr_args; i++)
1010 reg[i] = cvp_emit_arg( cp, &inst->SrcReg[i], REG_ARG0 + i );
1011
1012 op = cvp_next_instruction(cp);
1013 op->alu.opcode = inst->Opcode;
1014 op->alu.file0 = reg[0].file;
1015 op->alu.idx0 = reg[0].idx;
1016 op->alu.file1 = reg[1].file;
1017 op->alu.idx1 = reg[1].idx;
1018 op->alu.dst = result;
1019
1020 if (result == REG_RES) {
1021 op = cvp_next_instruction(cp);
1022 op->dword = fixup.dword;
1023 }
1024 break;
1025 }
1026 }
1027
1028 static void free_tnl_data( struct vertex_program *program )
1029 {
1030 struct tnl_compiled_program *p = program->TnlData;
1031 if (p->compiled_func)
1032 _mesa_free((void *)p->compiled_func);
1033 _mesa_free(p);
1034 program->TnlData = NULL;
1035 }
1036
1037 static void compile_vertex_program( struct vertex_program *program,
1038 GLboolean try_codegen )
1039 {
1040 struct compilation cp;
1041 struct tnl_compiled_program *p = CALLOC_STRUCT(tnl_compiled_program);
1042 GLuint i;
1043
1044 if (program->TnlData)
1045 free_tnl_data( program );
1046
1047 program->TnlData = p;
1048
1049 /* Initialize cp. Note that ctx and VB aren't used in compilation
1050 * so we don't have to worry about statechanges:
1051 */
1052 _mesa_memset(&cp, 0, sizeof(cp));
1053 cp.csr = p->instructions;
1054
1055 /* Compile instructions:
1056 */
1057 for (i = 0; i < program->Base.NumInstructions; i++) {
1058 cvp_emit_inst(&cp, &program->Instructions[i]);
1059 }
1060
1061 /* Finish up:
1062 */
1063 p->nr_instructions = cp.csr - p->instructions;
1064
1065 /* Print/disassemble:
1066 */
1067 if (DISASSEM) {
1068 for (i = 0; i < p->nr_instructions; i++) {
1069 _tnl_disassem_vba_insn(p->instructions[i]);
1070 }
1071 _mesa_printf("\n\n");
1072 }
1073
1074 #ifdef USE_SSE_ASM
1075 if (try_codegen)
1076 _tnl_sse_codegen_vertex_program(p);
1077 #endif
1078
1079 }
1080
1081
1082
1083
1084 /* ----------------------------------------------------------------------
1085 * Execution
1086 */
1087 static void userclip( GLcontext *ctx,
1088 GLvector4f *clip,
1089 GLubyte *clipmask,
1090 GLubyte *clipormask,
1091 GLubyte *clipandmask )
1092 {
1093 GLuint p;
1094
1095 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
1096 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
1097 GLuint nr, i;
1098 const GLfloat a = ctx->Transform._ClipUserPlane[p][0];
1099 const GLfloat b = ctx->Transform._ClipUserPlane[p][1];
1100 const GLfloat c = ctx->Transform._ClipUserPlane[p][2];
1101 const GLfloat d = ctx->Transform._ClipUserPlane[p][3];
1102 GLfloat *coord = (GLfloat *)clip->data;
1103 GLuint stride = clip->stride;
1104 GLuint count = clip->count;
1105
1106 for (nr = 0, i = 0 ; i < count ; i++) {
1107 GLfloat dp = (coord[0] * a +
1108 coord[1] * b +
1109 coord[2] * c +
1110 coord[3] * d);
1111
1112 if (dp < 0) {
1113 nr++;
1114 clipmask[i] |= CLIP_USER_BIT;
1115 }
1116
1117 STRIDE_F(coord, stride);
1118 }
1119
1120 if (nr > 0) {
1121 *clipormask |= CLIP_USER_BIT;
1122 if (nr == count) {
1123 *clipandmask |= CLIP_USER_BIT;
1124 return;
1125 }
1126 }
1127 }
1128 }
1129 }
1130
1131
1132 static GLboolean do_ndc_cliptest( struct arb_vp_machine *m )
1133 {
1134 GLcontext *ctx = m->ctx;
1135 TNLcontext *tnl = TNL_CONTEXT(ctx);
1136 struct vertex_buffer *VB = m->VB;
1137
1138 /* Cliptest and perspective divide. Clip functions must clear
1139 * the clipmask.
1140 */
1141 m->ormask = 0;
1142 m->andmask = CLIP_ALL_BITS;
1143
1144 if (tnl->NeedNdcCoords) {
1145 VB->NdcPtr =
1146 _mesa_clip_tab[VB->ClipPtr->size]( VB->ClipPtr,
1147 &m->ndcCoords,
1148 m->clipmask,
1149 &m->ormask,
1150 &m->andmask );
1151 }
1152 else {
1153 VB->NdcPtr = NULL;
1154 _mesa_clip_np_tab[VB->ClipPtr->size]( VB->ClipPtr,
1155 NULL,
1156 m->clipmask,
1157 &m->ormask,
1158 &m->andmask );
1159 }
1160
1161 if (m->andmask) {
1162 /* All vertices are outside the frustum */
1163 return GL_FALSE;
1164 }
1165
1166 /* Test userclip planes. This contributes to VB->ClipMask.
1167 */
1168 if (ctx->Transform.ClipPlanesEnabled && !ctx->VertexProgram._Enabled) {
1169 userclip( ctx,
1170 VB->ClipPtr,
1171 m->clipmask,
1172 &m->ormask,
1173 &m->andmask );
1174
1175 if (m->andmask) {
1176 return GL_FALSE;
1177 }
1178 }
1179
1180 VB->ClipAndMask = m->andmask;
1181 VB->ClipOrMask = m->ormask;
1182 VB->ClipMask = m->clipmask;
1183
1184 return GL_TRUE;
1185 }
1186
1187
1188 static INLINE void call_func( struct tnl_compiled_program *p,
1189 struct arb_vp_machine *m )
1190 {
1191 p->compiled_func(m);
1192 }
1193
1194 /**
1195 * Execute the given vertex program.
1196 *
1197 * TODO: Integrate the t_vertex.c code here, to build machine vertices
1198 * directly at this point.
1199 *
1200 * TODO: Eliminate the VB struct entirely and just use
1201 * struct arb_vertex_machine.
1202 */
1203 static GLboolean
1204 run_arb_vertex_program(GLcontext *ctx, struct tnl_pipeline_stage *stage)
1205 {
1206 struct vertex_program *program = (ctx->VertexProgram._Enabled ?
1207 ctx->VertexProgram.Current :
1208 ctx->_TnlProgram);
1209 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
1210 struct arb_vp_machine *m = ARB_VP_MACHINE(stage);
1211 struct tnl_compiled_program *p;
1212 GLuint i, j, outputs;
1213
1214 if (!program || program->IsNVProgram)
1215 return GL_TRUE;
1216
1217 if (program->Parameters) {
1218 _mesa_load_state_parameters(ctx, program->Parameters);
1219 }
1220
1221 p = (struct tnl_compiled_program *)program->TnlData;
1222 assert(p);
1223
1224
1225 m->nr_inputs = m->nr_outputs = 0;
1226
1227 for (i = 0; i < _TNL_ATTRIB_MAX; i++) {
1228 if (program->InputsRead & (1<<i)) {
1229 GLuint j = m->nr_inputs++;
1230 m->input[j].idx = i;
1231 m->input[j].data = (GLfloat *)m->VB->AttribPtr[i]->data;
1232 m->input[j].stride = m->VB->AttribPtr[i]->stride;
1233 m->input[j].size = m->VB->AttribPtr[i]->size;
1234 ASSIGN_4V(m->File[0][REG_IN0 + i], 0, 0, 0, 1);
1235 }
1236 }
1237
1238 for (i = 0; i < 15; i++) {
1239 if (program->OutputsWritten & (1<<i)) {
1240 GLuint j = m->nr_outputs++;
1241 m->output[j].idx = i;
1242 m->output[j].data = (GLfloat *)m->attribs[i].data;
1243 }
1244 }
1245
1246
1247 /* Run the actual program:
1248 */
1249 for (m->vtx_nr = 0; m->vtx_nr < VB->Count; m->vtx_nr++) {
1250 for (j = 0; j < m->nr_inputs; j++) {
1251 GLuint idx = REG_IN0 + m->input[j].idx;
1252 switch (m->input[j].size) {
1253 case 4: m->File[0][idx][3] = m->input[j].data[3];
1254 case 3: m->File[0][idx][2] = m->input[j].data[2];
1255 case 2: m->File[0][idx][1] = m->input[j].data[1];
1256 case 1: m->File[0][idx][0] = m->input[j].data[0];
1257 }
1258
1259 STRIDE_F(m->input[j].data, m->input[j].stride);
1260 }
1261
1262 if (p->compiled_func) {
1263 call_func( p, m );
1264 }
1265 else {
1266 for (j = 0; j < p->nr_instructions; j++) {
1267 union instruction inst = p->instructions[j];
1268 opcode_func[inst.alu.opcode]( m, inst );
1269 }
1270 }
1271
1272 for (j = 0; j < m->nr_outputs; j++) {
1273 GLuint idx = REG_OUT0 + m->output[j].idx;
1274 m->output[j].data[0] = m->File[0][idx][0];
1275 m->output[j].data[1] = m->File[0][idx][1];
1276 m->output[j].data[2] = m->File[0][idx][2];
1277 m->output[j].data[3] = m->File[0][idx][3];
1278 m->output[j].data += 4;
1279 }
1280 }
1281
1282 /* Setup the VB pointers so that the next pipeline stages get
1283 * their data from the right place (the program output arrays).
1284 *
1285 * TODO: 1) Have tnl use these RESULT values for outputs rather
1286 * than trying to shoe-horn inputs and outputs into one set of
1287 * values.
1288 *
1289 * TODO: 2) Integrate t_vertex.c so that we just go straight ahead
1290 * and build machine vertices here.
1291 */
1292 VB->ClipPtr = &m->attribs[VERT_RESULT_HPOS];
1293 VB->ClipPtr->count = VB->Count;
1294
1295 outputs = program->OutputsWritten;
1296
1297 if (outputs & (1<<VERT_RESULT_COL0)) {
1298 VB->ColorPtr[0] = &m->attribs[VERT_RESULT_COL0];
1299 VB->AttribPtr[VERT_ATTRIB_COLOR0] = VB->ColorPtr[0];
1300 }
1301
1302 if (outputs & (1<<VERT_RESULT_BFC0)) {
1303 VB->ColorPtr[1] = &m->attribs[VERT_RESULT_BFC0];
1304 }
1305
1306 if (outputs & (1<<VERT_RESULT_COL1)) {
1307 VB->SecondaryColorPtr[0] = &m->attribs[VERT_RESULT_COL1];
1308 VB->AttribPtr[VERT_ATTRIB_COLOR1] = VB->SecondaryColorPtr[0];
1309 }
1310
1311 if (outputs & (1<<VERT_RESULT_BFC1)) {
1312 VB->SecondaryColorPtr[1] = &m->attribs[VERT_RESULT_BFC1];
1313 }
1314
1315 if (outputs & (1<<VERT_RESULT_FOGC)) {
1316 VB->FogCoordPtr = &m->attribs[VERT_RESULT_FOGC];
1317 VB->AttribPtr[VERT_ATTRIB_FOG] = VB->FogCoordPtr;
1318 }
1319
1320 if (outputs & (1<<VERT_RESULT_PSIZ)) {
1321 VB->PointSizePtr = &m->attribs[VERT_RESULT_PSIZ];
1322 VB->AttribPtr[_TNL_ATTRIB_POINTSIZE] = &m->attribs[VERT_RESULT_PSIZ];
1323 }
1324
1325 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
1326 if (outputs & (1<<(VERT_RESULT_TEX0+i))) {
1327 VB->TexCoordPtr[i] = &m->attribs[VERT_RESULT_TEX0 + i];
1328 VB->AttribPtr[VERT_ATTRIB_TEX0+i] = VB->TexCoordPtr[i];
1329 }
1330 }
1331
1332 #if 0
1333 for (i = 0; i < VB->Count; i++) {
1334 printf("Out %d: %f %f %f %f %f %f %f %f\n", i,
1335 VEC_ELT(VB->ClipPtr, GLfloat, i)[0],
1336 VEC_ELT(VB->ClipPtr, GLfloat, i)[1],
1337 VEC_ELT(VB->ClipPtr, GLfloat, i)[2],
1338 VEC_ELT(VB->ClipPtr, GLfloat, i)[3],
1339 VEC_ELT(VB->TexCoordPtr[0], GLfloat, i)[0],
1340 VEC_ELT(VB->TexCoordPtr[0], GLfloat, i)[1],
1341 VEC_ELT(VB->TexCoordPtr[0], GLfloat, i)[2],
1342 VEC_ELT(VB->TexCoordPtr[0], GLfloat, i)[3]);
1343 }
1344 #endif
1345
1346 /* Perform NDC and cliptest operations:
1347 */
1348 return do_ndc_cliptest(m);
1349 }
1350
1351
1352 static void
1353 validate_vertex_program( GLcontext *ctx, struct tnl_pipeline_stage *stage )
1354 {
1355 struct arb_vp_machine *m = ARB_VP_MACHINE(stage);
1356 struct vertex_program *program =
1357 (ctx->VertexProgram._Enabled ? ctx->VertexProgram.Current : 0);
1358
1359 if (!program && ctx->_MaintainTnlProgram) {
1360 program = ctx->_TnlProgram;
1361 }
1362
1363 if (program) {
1364 if (!program->TnlData)
1365 compile_vertex_program( program, m->try_codegen );
1366
1367 /* Grab the state GL state and put into registers:
1368 */
1369 m->File[FILE_LOCAL_PARAM] = program->Base.LocalParams;
1370 m->File[FILE_ENV_PARAM] = ctx->VertexProgram.Parameters;
1371 /* GL_NV_vertex_programs can't reference GL state */
1372 if (program->Parameters)
1373 m->File[FILE_STATE_PARAM] = program->Parameters->ParameterValues;
1374 else
1375 m->File[FILE_STATE_PARAM] = NULL;
1376 }
1377 }
1378
1379
1380
1381
1382
1383
1384
1385 /**
1386 * Called the first time stage->run is called. In effect, don't
1387 * allocate data until the first time the stage is run.
1388 */
1389 static GLboolean init_vertex_program( GLcontext *ctx,
1390 struct tnl_pipeline_stage *stage )
1391 {
1392 TNLcontext *tnl = TNL_CONTEXT(ctx);
1393 struct vertex_buffer *VB = &(tnl->vb);
1394 struct arb_vp_machine *m;
1395 const GLuint size = VB->Size;
1396 GLuint i;
1397
1398 stage->privatePtr = _mesa_calloc(sizeof(*m));
1399 m = ARB_VP_MACHINE(stage);
1400 if (!m)
1401 return GL_FALSE;
1402
1403 /* arb_vertex_machine struct should subsume the VB:
1404 */
1405 m->VB = VB;
1406 m->ctx = ctx;
1407
1408 m->File[0] = ALIGN_MALLOC(REG_MAX * sizeof(GLfloat) * 4, 16);
1409
1410 /* Initialize regs where necessary:
1411 */
1412 ASSIGN_4V(m->File[0][REG_ID], 0, 0, 0, 1);
1413 ASSIGN_4V(m->File[0][REG_ONES], 1, 1, 1, 1);
1414 ASSIGN_4V(m->File[0][REG_SWZ], -1, 1, 0, 0);
1415 ASSIGN_4V(m->File[0][REG_NEG], -1, -1, -1, -1);
1416 ASSIGN_4V(m->File[0][REG_LIT], 1, 0, 0, 1);
1417 ASSIGN_4V(m->File[0][REG_LIT2], 1, .5, .2, 1); /* debug value */
1418
1419 if (_mesa_getenv("MESA_EXPERIMENTAL"))
1420 m->try_codegen = GL_TRUE;
1421
1422 /* Allocate arrays of vertex output values */
1423 for (i = 0; i < VERT_RESULT_MAX; i++) {
1424 _mesa_vector4f_alloc( &m->attribs[i], 0, size, 32 );
1425 m->attribs[i].size = 4;
1426 }
1427
1428 /* a few other misc allocations */
1429 _mesa_vector4f_alloc( &m->ndcCoords, 0, size, 32 );
1430 m->clipmask = (GLubyte *) ALIGN_MALLOC(sizeof(GLubyte)*size, 32 );
1431
1432 if (ctx->_MaintainTnlProgram)
1433 _mesa_allow_light_in_model( ctx, GL_FALSE );
1434
1435 m->fpucntl_rnd_neg = RND_NEG_FPU; /* const value */
1436 m->fpucntl_restore = RESTORE_FPU; /* const value */
1437
1438 return GL_TRUE;
1439 }
1440
1441
1442
1443
1444 /**
1445 * Destructor for this pipeline stage.
1446 */
1447 static void dtr( struct tnl_pipeline_stage *stage )
1448 {
1449 struct arb_vp_machine *m = ARB_VP_MACHINE(stage);
1450
1451 if (m) {
1452 GLuint i;
1453
1454 /* free the vertex program result arrays */
1455 for (i = 0; i < VERT_RESULT_MAX; i++)
1456 _mesa_vector4f_free( &m->attribs[i] );
1457
1458 /* free misc arrays */
1459 _mesa_vector4f_free( &m->ndcCoords );
1460 ALIGN_FREE( m->clipmask );
1461 ALIGN_FREE( m->File[0] );
1462
1463 _mesa_free( m );
1464 stage->privatePtr = NULL;
1465 }
1466 }
1467
1468 /**
1469 * Public description of this pipeline stage.
1470 */
1471 const struct tnl_pipeline_stage _tnl_arb_vertex_program_stage =
1472 {
1473 "vertex-program",
1474 NULL, /* private_data */
1475 init_vertex_program, /* create */
1476 dtr, /* destroy */
1477 validate_vertex_program, /* validate */
1478 run_arb_vertex_program /* run */
1479 };
1480
1481
1482 /**
1483 * Called via ctx->Driver.ProgramStringNotify() after a new vertex program
1484 * string has been parsed.
1485 */
1486 void
1487 _tnl_program_string(GLcontext *ctx, GLenum target, struct program *program)
1488 {
1489 if (program->Target == GL_VERTEX_PROGRAM_ARB) {
1490 /* free any existing tnl data hanging off the program */
1491 struct vertex_program *vprog = (struct vertex_program *) program;
1492 if (vprog->TnlData) {
1493 free_tnl_data(vprog);
1494 }
1495 }
1496 }