more DEBUG_PROG
[mesa.git] / src / mesa / shader / prog_execute.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 1999-2007 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 prog_execute.c
27 * Software interpreter for vertex/fragment programs.
28 * \author Brian Paul
29 */
30
31 /*
32 * NOTE: we do everything in single-precision floating point; we don't
33 * currently observe the single/half/fixed-precision qualifiers.
34 *
35 */
36
37
38 #include "glheader.h"
39 #include "colormac.h"
40 #include "context.h"
41 #include "program.h"
42 #include "prog_execute.h"
43 #include "prog_instruction.h"
44 #include "prog_parameter.h"
45 #include "prog_print.h"
46 #include "slang_library_noise.h"
47
48
49 /* See comments below for info about this */
50 #define LAMBDA_ZERO 1
51
52 /* debug predicate */
53 #define DEBUG_PROG 0
54
55
56 /**
57 * Set x to positive or negative infinity.
58 */
59 #if defined(USE_IEEE) || defined(_WIN32)
60 #define SET_POS_INFINITY(x) ( *((GLuint *) (void *)&x) = 0x7F800000 )
61 #define SET_NEG_INFINITY(x) ( *((GLuint *) (void *)&x) = 0xFF800000 )
62 #elif defined(VMS)
63 #define SET_POS_INFINITY(x) x = __MAXFLOAT
64 #define SET_NEG_INFINITY(x) x = -__MAXFLOAT
65 #else
66 #define SET_POS_INFINITY(x) x = (GLfloat) HUGE_VAL
67 #define SET_NEG_INFINITY(x) x = (GLfloat) -HUGE_VAL
68 #endif
69
70 #define SET_FLOAT_BITS(x, bits) ((fi_type *) (void *) &(x))->i = bits
71
72
73 static const GLfloat ZeroVec[4] = { 0.0F, 0.0F, 0.0F, 0.0F };
74
75
76
77 /**
78 * Return a pointer to the 4-element float vector specified by the given
79 * source register.
80 */
81 static INLINE const GLfloat *
82 get_register_pointer(const struct prog_src_register *source,
83 const struct gl_program_machine *machine)
84 {
85 if (source->RelAddr) {
86 const GLint reg = source->Index + machine->AddressReg[0][0];
87 if (source->File == PROGRAM_ENV_PARAM)
88 if (reg < 0 || reg >= MAX_PROGRAM_ENV_PARAMS)
89 return ZeroVec;
90 else
91 return machine->EnvParams[reg];
92 else {
93 const struct gl_program_parameter_list *params;
94 ASSERT(source->File == PROGRAM_LOCAL_PARAM ||
95 source->File == PROGRAM_STATE_VAR);
96 params = machine->CurProgram->Parameters;
97 if (reg < 0 || reg >= params->NumParameters)
98 return ZeroVec;
99 else
100 return params->ParameterValues[reg];
101 }
102 }
103
104 switch (source->File) {
105 case PROGRAM_TEMPORARY:
106 ASSERT(source->Index < MAX_PROGRAM_TEMPS);
107 return machine->Temporaries[source->Index];
108
109 case PROGRAM_INPUT:
110 if (machine->CurProgram->Target == GL_VERTEX_PROGRAM_ARB) {
111 ASSERT(source->Index < VERT_ATTRIB_MAX);
112 return machine->VertAttribs[source->Index];
113 }
114 else {
115 ASSERT(source->Index < FRAG_ATTRIB_MAX);
116 return machine->Attribs[source->Index][machine->CurElement];
117 }
118
119 case PROGRAM_OUTPUT:
120 ASSERT(source->Index < MAX_PROGRAM_OUTPUTS);
121 return machine->Outputs[source->Index];
122
123 case PROGRAM_LOCAL_PARAM:
124 ASSERT(source->Index < MAX_PROGRAM_LOCAL_PARAMS);
125 return machine->CurProgram->LocalParams[source->Index];
126
127 case PROGRAM_ENV_PARAM:
128 ASSERT(source->Index < MAX_PROGRAM_ENV_PARAMS);
129 return machine->EnvParams[source->Index];
130
131 case PROGRAM_STATE_VAR:
132 /* Fallthrough */
133 case PROGRAM_CONSTANT:
134 /* Fallthrough */
135 case PROGRAM_UNIFORM:
136 /* Fallthrough */
137 case PROGRAM_NAMED_PARAM:
138 ASSERT(source->Index <
139 (GLint) machine->CurProgram->Parameters->NumParameters);
140 return machine->CurProgram->Parameters->ParameterValues[source->Index];
141
142 default:
143 _mesa_problem(NULL,
144 "Invalid input register file %d in get_register_pointer()",
145 source->File);
146 return NULL;
147 }
148 }
149
150
151 #if FEATURE_MESA_program_debug
152 static struct gl_program_machine *CurrentMachine = NULL;
153
154 /**
155 * For GL_MESA_program_debug.
156 * Return current value (4*GLfloat) of a program register.
157 * Called via ctx->Driver.GetProgramRegister().
158 */
159 void
160 _mesa_get_program_register(GLcontext *ctx, enum register_file file,
161 GLuint index, GLfloat val[4])
162 {
163 if (CurrentMachine) {
164 struct prog_src_register src;
165 const GLfloat *reg;
166 src.File = file;
167 src.Index = index;
168 reg = get_register_pointer(&src, CurrentMachine);
169 COPY_4V(val, reg);
170 }
171 }
172 #endif /* FEATURE_MESA_program_debug */
173
174
175 /**
176 * Fetch a 4-element float vector from the given source register.
177 * Apply swizzling and negating as needed.
178 */
179 static void
180 fetch_vector4(const struct prog_src_register *source,
181 const struct gl_program_machine *machine, GLfloat result[4])
182 {
183 const GLfloat *src = get_register_pointer(source, machine);
184 ASSERT(src);
185
186 if (source->Swizzle == SWIZZLE_NOOP) {
187 /* no swizzling */
188 COPY_4V(result, src);
189 }
190 else {
191 ASSERT(GET_SWZ(source->Swizzle, 0) <= 3);
192 ASSERT(GET_SWZ(source->Swizzle, 1) <= 3);
193 ASSERT(GET_SWZ(source->Swizzle, 2) <= 3);
194 ASSERT(GET_SWZ(source->Swizzle, 3) <= 3);
195 result[0] = src[GET_SWZ(source->Swizzle, 0)];
196 result[1] = src[GET_SWZ(source->Swizzle, 1)];
197 result[2] = src[GET_SWZ(source->Swizzle, 2)];
198 result[3] = src[GET_SWZ(source->Swizzle, 3)];
199 }
200
201 if (source->NegateBase) {
202 result[0] = -result[0];
203 result[1] = -result[1];
204 result[2] = -result[2];
205 result[3] = -result[3];
206 }
207 if (source->Abs) {
208 result[0] = FABSF(result[0]);
209 result[1] = FABSF(result[1]);
210 result[2] = FABSF(result[2]);
211 result[3] = FABSF(result[3]);
212 }
213 if (source->NegateAbs) {
214 result[0] = -result[0];
215 result[1] = -result[1];
216 result[2] = -result[2];
217 result[3] = -result[3];
218 }
219 }
220
221 #if 0
222 /**
223 * Fetch the derivative with respect to X for the given register.
224 * \return GL_TRUE if it was easily computed or GL_FALSE if we
225 * need to execute another instance of the program (ugh)!
226 */
227 static GLboolean
228 fetch_vector4_deriv(GLcontext * ctx,
229 const struct prog_src_register *source,
230 const SWspan * span,
231 char xOrY, GLint column, GLfloat result[4])
232 {
233 GLfloat src[4];
234
235 ASSERT(xOrY == 'X' || xOrY == 'Y');
236
237 switch (source->Index) {
238 case FRAG_ATTRIB_WPOS:
239 if (xOrY == 'X') {
240 src[0] = 1.0;
241 src[1] = 0.0;
242 src[2] = span->attrStepX[FRAG_ATTRIB_WPOS][2]
243 / ctx->DrawBuffer->_DepthMaxF;
244 src[3] = span->attrStepX[FRAG_ATTRIB_WPOS][3];
245 }
246 else {
247 src[0] = 0.0;
248 src[1] = 1.0;
249 src[2] = span->attrStepY[FRAG_ATTRIB_WPOS][2]
250 / ctx->DrawBuffer->_DepthMaxF;
251 src[3] = span->attrStepY[FRAG_ATTRIB_WPOS][3];
252 }
253 break;
254 case FRAG_ATTRIB_COL0:
255 case FRAG_ATTRIB_COL1:
256 if (xOrY == 'X') {
257 src[0] = span->attrStepX[source->Index][0] * (1.0F / CHAN_MAXF);
258 src[1] = span->attrStepX[source->Index][1] * (1.0F / CHAN_MAXF);
259 src[2] = span->attrStepX[source->Index][2] * (1.0F / CHAN_MAXF);
260 src[3] = span->attrStepX[source->Index][3] * (1.0F / CHAN_MAXF);
261 }
262 else {
263 src[0] = span->attrStepY[source->Index][0] * (1.0F / CHAN_MAXF);
264 src[1] = span->attrStepY[source->Index][1] * (1.0F / CHAN_MAXF);
265 src[2] = span->attrStepY[source->Index][2] * (1.0F / CHAN_MAXF);
266 src[3] = span->attrStepY[source->Index][3] * (1.0F / CHAN_MAXF);
267 }
268 break;
269 case FRAG_ATTRIB_FOGC:
270 if (xOrY == 'X') {
271 src[0] = span->attrStepX[FRAG_ATTRIB_FOGC][0] * (1.0F / CHAN_MAXF);
272 src[1] = 0.0;
273 src[2] = 0.0;
274 src[3] = 0.0;
275 }
276 else {
277 src[0] = span->attrStepY[FRAG_ATTRIB_FOGC][0] * (1.0F / CHAN_MAXF);
278 src[1] = 0.0;
279 src[2] = 0.0;
280 src[3] = 0.0;
281 }
282 break;
283 default:
284 assert(source->Index < FRAG_ATTRIB_MAX);
285 /* texcoord or varying */
286 if (xOrY == 'X') {
287 /* this is a little tricky - I think I've got it right */
288 const GLfloat invQ = 1.0f / (span->attrStart[source->Index][3]
289 +
290 span->attrStepX[source->Index][3] *
291 column);
292 src[0] = span->attrStepX[source->Index][0] * invQ;
293 src[1] = span->attrStepX[source->Index][1] * invQ;
294 src[2] = span->attrStepX[source->Index][2] * invQ;
295 src[3] = span->attrStepX[source->Index][3] * invQ;
296 }
297 else {
298 /* Tricky, as above, but in Y direction */
299 const GLfloat invQ = 1.0f / (span->attrStart[source->Index][3]
300 + span->attrStepY[source->Index][3]);
301 src[0] = span->attrStepY[source->Index][0] * invQ;
302 src[1] = span->attrStepY[source->Index][1] * invQ;
303 src[2] = span->attrStepY[source->Index][2] * invQ;
304 src[3] = span->attrStepY[source->Index][3] * invQ;
305 }
306 break;
307 }
308
309 result[0] = src[GET_SWZ(source->Swizzle, 0)];
310 result[1] = src[GET_SWZ(source->Swizzle, 1)];
311 result[2] = src[GET_SWZ(source->Swizzle, 2)];
312 result[3] = src[GET_SWZ(source->Swizzle, 3)];
313
314 if (source->NegateBase) {
315 result[0] = -result[0];
316 result[1] = -result[1];
317 result[2] = -result[2];
318 result[3] = -result[3];
319 }
320 if (source->Abs) {
321 result[0] = FABSF(result[0]);
322 result[1] = FABSF(result[1]);
323 result[2] = FABSF(result[2]);
324 result[3] = FABSF(result[3]);
325 }
326 if (source->NegateAbs) {
327 result[0] = -result[0];
328 result[1] = -result[1];
329 result[2] = -result[2];
330 result[3] = -result[3];
331 }
332 return GL_TRUE;
333 }
334 #endif
335
336
337 /**
338 * As above, but only return result[0] element.
339 */
340 static void
341 fetch_vector1(const struct prog_src_register *source,
342 const struct gl_program_machine *machine, GLfloat result[4])
343 {
344 const GLfloat *src = get_register_pointer(source, machine);
345 ASSERT(src);
346
347 result[0] = src[GET_SWZ(source->Swizzle, 0)];
348
349 if (source->NegateBase) {
350 result[0] = -result[0];
351 }
352 if (source->Abs) {
353 result[0] = FABSF(result[0]);
354 }
355 if (source->NegateAbs) {
356 result[0] = -result[0];
357 }
358 }
359
360
361 /**
362 * Test value against zero and return GT, LT, EQ or UN if NaN.
363 */
364 static INLINE GLuint
365 generate_cc(float value)
366 {
367 if (value != value)
368 return COND_UN; /* NaN */
369 if (value > 0.0F)
370 return COND_GT;
371 if (value < 0.0F)
372 return COND_LT;
373 return COND_EQ;
374 }
375
376
377 /**
378 * Test if the ccMaskRule is satisfied by the given condition code.
379 * Used to mask destination writes according to the current condition code.
380 */
381 static INLINE GLboolean
382 test_cc(GLuint condCode, GLuint ccMaskRule)
383 {
384 switch (ccMaskRule) {
385 case COND_EQ: return (condCode == COND_EQ);
386 case COND_NE: return (condCode != COND_EQ);
387 case COND_LT: return (condCode == COND_LT);
388 case COND_GE: return (condCode == COND_GT || condCode == COND_EQ);
389 case COND_LE: return (condCode == COND_LT || condCode == COND_EQ);
390 case COND_GT: return (condCode == COND_GT);
391 case COND_TR: return GL_TRUE;
392 case COND_FL: return GL_FALSE;
393 default: return GL_TRUE;
394 }
395 }
396
397
398 /**
399 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
400 * or GL_FALSE to indicate result.
401 */
402 static INLINE GLboolean
403 eval_condition(const struct gl_program_machine *machine,
404 const struct prog_instruction *inst)
405 {
406 const GLuint swizzle = inst->DstReg.CondSwizzle;
407 const GLuint condMask = inst->DstReg.CondMask;
408 if (test_cc(machine->CondCodes[GET_SWZ(swizzle, 0)], condMask) ||
409 test_cc(machine->CondCodes[GET_SWZ(swizzle, 1)], condMask) ||
410 test_cc(machine->CondCodes[GET_SWZ(swizzle, 2)], condMask) ||
411 test_cc(machine->CondCodes[GET_SWZ(swizzle, 3)], condMask)) {
412 return GL_TRUE;
413 }
414 else {
415 return GL_FALSE;
416 }
417 }
418
419
420
421 /**
422 * Store 4 floats into a register. Observe the instructions saturate and
423 * set-condition-code flags.
424 */
425 static void
426 store_vector4(const struct prog_instruction *inst,
427 struct gl_program_machine *machine, const GLfloat value[4])
428 {
429 const struct prog_dst_register *dest = &(inst->DstReg);
430 const GLboolean clamp = inst->SaturateMode == SATURATE_ZERO_ONE;
431 GLfloat *dstReg;
432 GLfloat dummyReg[4];
433 GLfloat clampedValue[4];
434 GLuint writeMask = dest->WriteMask;
435
436 switch (dest->File) {
437 case PROGRAM_OUTPUT:
438 ASSERT(dest->Index < MAX_PROGRAM_OUTPUTS);
439 dstReg = machine->Outputs[dest->Index];
440 break;
441 case PROGRAM_TEMPORARY:
442 ASSERT(dest->Index < MAX_PROGRAM_TEMPS);
443 dstReg = machine->Temporaries[dest->Index];
444 break;
445 case PROGRAM_WRITE_ONLY:
446 dstReg = dummyReg;
447 return;
448 default:
449 _mesa_problem(NULL, "bad register file in store_vector4(fp)");
450 return;
451 }
452
453 #if 0
454 if (value[0] > 1.0e10 ||
455 IS_INF_OR_NAN(value[0]) ||
456 IS_INF_OR_NAN(value[1]) ||
457 IS_INF_OR_NAN(value[2]) || IS_INF_OR_NAN(value[3]))
458 printf("store %g %g %g %g\n", value[0], value[1], value[2], value[3]);
459 #endif
460
461 if (clamp) {
462 clampedValue[0] = CLAMP(value[0], 0.0F, 1.0F);
463 clampedValue[1] = CLAMP(value[1], 0.0F, 1.0F);
464 clampedValue[2] = CLAMP(value[2], 0.0F, 1.0F);
465 clampedValue[3] = CLAMP(value[3], 0.0F, 1.0F);
466 value = clampedValue;
467 }
468
469 if (dest->CondMask != COND_TR) {
470 /* condition codes may turn off some writes */
471 if (writeMask & WRITEMASK_X) {
472 if (!test_cc(machine->CondCodes[GET_SWZ(dest->CondSwizzle, 0)],
473 dest->CondMask))
474 writeMask &= ~WRITEMASK_X;
475 }
476 if (writeMask & WRITEMASK_Y) {
477 if (!test_cc(machine->CondCodes[GET_SWZ(dest->CondSwizzle, 1)],
478 dest->CondMask))
479 writeMask &= ~WRITEMASK_Y;
480 }
481 if (writeMask & WRITEMASK_Z) {
482 if (!test_cc(machine->CondCodes[GET_SWZ(dest->CondSwizzle, 2)],
483 dest->CondMask))
484 writeMask &= ~WRITEMASK_Z;
485 }
486 if (writeMask & WRITEMASK_W) {
487 if (!test_cc(machine->CondCodes[GET_SWZ(dest->CondSwizzle, 3)],
488 dest->CondMask))
489 writeMask &= ~WRITEMASK_W;
490 }
491 }
492
493 if (writeMask & WRITEMASK_X)
494 dstReg[0] = value[0];
495 if (writeMask & WRITEMASK_Y)
496 dstReg[1] = value[1];
497 if (writeMask & WRITEMASK_Z)
498 dstReg[2] = value[2];
499 if (writeMask & WRITEMASK_W)
500 dstReg[3] = value[3];
501
502 if (inst->CondUpdate) {
503 if (writeMask & WRITEMASK_X)
504 machine->CondCodes[0] = generate_cc(value[0]);
505 if (writeMask & WRITEMASK_Y)
506 machine->CondCodes[1] = generate_cc(value[1]);
507 if (writeMask & WRITEMASK_Z)
508 machine->CondCodes[2] = generate_cc(value[2]);
509 if (writeMask & WRITEMASK_W)
510 machine->CondCodes[3] = generate_cc(value[3]);
511 }
512 }
513
514
515 #if 0
516 /**
517 * Initialize a new machine state instance from an existing one, adding
518 * the partial derivatives onto the input registers.
519 * Used to implement DDX and DDY instructions in non-trivial cases.
520 */
521 static void
522 init_machine_deriv(GLcontext * ctx,
523 const struct gl_program_machine *machine,
524 const struct gl_fragment_program *program,
525 const SWspan * span, char xOrY,
526 struct gl_program_machine *dMachine)
527 {
528 GLuint attr;
529
530 ASSERT(xOrY == 'X' || xOrY == 'Y');
531
532 /* copy existing machine */
533 _mesa_memcpy(dMachine, machine, sizeof(struct gl_program_machine));
534
535 if (program->Base.Target == GL_FRAGMENT_PROGRAM_NV) {
536 /* XXX also need to do this when using valgrind */
537 /* Clear temporary registers (undefined for ARB_f_p) */
538 _mesa_bzero((void *) machine->Temporaries,
539 MAX_PROGRAM_TEMPS * 4 * sizeof(GLfloat));
540 }
541
542 /* Add derivatives */
543 if (program->Base.InputsRead & FRAG_BIT_WPOS) {
544 GLfloat *wpos = machine->Attribs[FRAG_ATTRIB_WPOS][machine->CurElement];
545 if (xOrY == 'X') {
546 wpos[0] += 1.0F;
547 wpos[1] += 0.0F;
548 wpos[2] += span->attrStepX[FRAG_ATTRIB_WPOS][2];
549 wpos[3] += span->attrStepX[FRAG_ATTRIB_WPOS][3];
550 }
551 else {
552 wpos[0] += 0.0F;
553 wpos[1] += 1.0F;
554 wpos[2] += span->attrStepY[FRAG_ATTRIB_WPOS][2];
555 wpos[3] += span->attrStepY[FRAG_ATTRIB_WPOS][3];
556 }
557 }
558
559 /* primary, secondary colors */
560 for (attr = FRAG_ATTRIB_COL0; attr <= FRAG_ATTRIB_COL1; attr++) {
561 if (program->Base.InputsRead & (1 << attr)) {
562 GLfloat *col = machine->Attribs[attr][machine->CurElement];
563 if (xOrY == 'X') {
564 col[0] += span->attrStepX[attr][0] * (1.0F / CHAN_MAXF);
565 col[1] += span->attrStepX[attr][1] * (1.0F / CHAN_MAXF);
566 col[2] += span->attrStepX[attr][2] * (1.0F / CHAN_MAXF);
567 col[3] += span->attrStepX[attr][3] * (1.0F / CHAN_MAXF);
568 }
569 else {
570 col[0] += span->attrStepY[attr][0] * (1.0F / CHAN_MAXF);
571 col[1] += span->attrStepY[attr][1] * (1.0F / CHAN_MAXF);
572 col[2] += span->attrStepY[attr][2] * (1.0F / CHAN_MAXF);
573 col[3] += span->attrStepY[attr][3] * (1.0F / CHAN_MAXF);
574 }
575 }
576 }
577 if (program->Base.InputsRead & FRAG_BIT_FOGC) {
578 GLfloat *fogc = machine->Attribs[FRAG_ATTRIB_FOGC][machine->CurElement];
579 if (xOrY == 'X') {
580 fogc[0] += span->attrStepX[FRAG_ATTRIB_FOGC][0];
581 }
582 else {
583 fogc[0] += span->attrStepY[FRAG_ATTRIB_FOGC][0];
584 }
585 }
586 /* texcoord and varying vars */
587 for (attr = FRAG_ATTRIB_TEX0; attr < FRAG_ATTRIB_MAX; attr++) {
588 if (program->Base.InputsRead & (1 << attr)) {
589 GLfloat *val = machine->Attribs[attr][machine->CurElement];
590 /* XXX perspective-correct interpolation */
591 if (xOrY == 'X') {
592 val[0] += span->attrStepX[attr][0];
593 val[1] += span->attrStepX[attr][1];
594 val[2] += span->attrStepX[attr][2];
595 val[3] += span->attrStepX[attr][3];
596 }
597 else {
598 val[0] += span->attrStepY[attr][0];
599 val[1] += span->attrStepY[attr][1];
600 val[2] += span->attrStepY[attr][2];
601 val[3] += span->attrStepY[attr][3];
602 }
603 }
604 }
605
606 /* init condition codes */
607 dMachine->CondCodes[0] = COND_EQ;
608 dMachine->CondCodes[1] = COND_EQ;
609 dMachine->CondCodes[2] = COND_EQ;
610 dMachine->CondCodes[3] = COND_EQ;
611 }
612 #endif
613
614
615 /**
616 * Execute the given vertex/fragment program.
617 *
618 * \param ctx rendering context
619 * \param program the program to execute
620 * \param machine machine state (must be initialized)
621 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
622 */
623 GLboolean
624 _mesa_execute_program(GLcontext * ctx,
625 const struct gl_program *program,
626 struct gl_program_machine *machine)
627 {
628 const GLuint numInst = program->NumInstructions;
629 const GLuint maxExec = 10000;
630 GLint pc, numExec = 0;
631
632 machine->CurProgram = program;
633
634 if (DEBUG_PROG) {
635 printf("execute program %u --------------------\n", program->Id);
636 }
637
638 #if FEATURE_MESA_program_debug
639 CurrentMachine = machine;
640 #endif
641
642 if (program->Target == GL_VERTEX_PROGRAM_ARB) {
643 machine->EnvParams = ctx->VertexProgram.Parameters;
644 }
645 else {
646 machine->EnvParams = ctx->FragmentProgram.Parameters;
647 }
648
649 for (pc = 0; pc < numInst; pc++) {
650 const struct prog_instruction *inst = program->Instructions + pc;
651
652 #if FEATURE_MESA_program_debug
653 if (ctx->FragmentProgram.CallbackEnabled &&
654 ctx->FragmentProgram.Callback) {
655 ctx->FragmentProgram.CurrentPosition = inst->StringPos;
656 ctx->FragmentProgram.Callback(program->Target,
657 ctx->FragmentProgram.CallbackData);
658 }
659 #endif
660
661 if (DEBUG_PROG) {
662 _mesa_print_instruction(inst);
663 }
664
665 switch (inst->Opcode) {
666 case OPCODE_ABS:
667 {
668 GLfloat a[4], result[4];
669 fetch_vector4(&inst->SrcReg[0], machine, a);
670 result[0] = FABSF(a[0]);
671 result[1] = FABSF(a[1]);
672 result[2] = FABSF(a[2]);
673 result[3] = FABSF(a[3]);
674 store_vector4(inst, machine, result);
675 }
676 break;
677 case OPCODE_ADD:
678 {
679 GLfloat a[4], b[4], result[4];
680 fetch_vector4(&inst->SrcReg[0], machine, a);
681 fetch_vector4(&inst->SrcReg[1], machine, b);
682 result[0] = a[0] + b[0];
683 result[1] = a[1] + b[1];
684 result[2] = a[2] + b[2];
685 result[3] = a[3] + b[3];
686 store_vector4(inst, machine, result);
687 if (DEBUG_PROG) {
688 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
689 result[0], result[1], result[2], result[3],
690 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
691 }
692 }
693 break;
694 case OPCODE_ARL:
695 {
696 GLfloat t[4];
697 fetch_vector4(&inst->SrcReg[0], machine, t);
698 machine->AddressReg[0][0] = (GLint) FLOORF(t[0]);
699 }
700 break;
701 case OPCODE_BGNLOOP:
702 /* no-op */
703 break;
704 case OPCODE_ENDLOOP:
705 /* subtract 1 here since pc is incremented by for(pc) loop */
706 pc = inst->BranchTarget - 1; /* go to matching BNGLOOP */
707 break;
708 case OPCODE_BGNSUB: /* begin subroutine */
709 break;
710 case OPCODE_ENDSUB: /* end subroutine */
711 break;
712 case OPCODE_BRA: /* branch (conditional) */
713 /* fall-through */
714 case OPCODE_BRK: /* break out of loop (conditional) */
715 /* fall-through */
716 case OPCODE_CONT: /* continue loop (conditional) */
717 if (eval_condition(machine, inst)) {
718 /* take branch */
719 /* Subtract 1 here since we'll do pc++ at end of for-loop */
720 pc = inst->BranchTarget - 1;
721 }
722 break;
723 case OPCODE_CAL: /* Call subroutine (conditional) */
724 if (eval_condition(machine, inst)) {
725 /* call the subroutine */
726 if (machine->StackDepth >= MAX_PROGRAM_CALL_DEPTH) {
727 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
728 }
729 machine->CallStack[machine->StackDepth++] = pc + 1;
730 pc = inst->BranchTarget; /* XXX - 1 ??? */
731 }
732 break;
733 case OPCODE_CMP:
734 {
735 GLfloat a[4], b[4], c[4], result[4];
736 fetch_vector4(&inst->SrcReg[0], machine, a);
737 fetch_vector4(&inst->SrcReg[1], machine, b);
738 fetch_vector4(&inst->SrcReg[2], machine, c);
739 result[0] = a[0] < 0.0F ? b[0] : c[0];
740 result[1] = a[1] < 0.0F ? b[1] : c[1];
741 result[2] = a[2] < 0.0F ? b[2] : c[2];
742 result[3] = a[3] < 0.0F ? b[3] : c[3];
743 store_vector4(inst, machine, result);
744 }
745 break;
746 case OPCODE_COS:
747 {
748 GLfloat a[4], result[4];
749 fetch_vector1(&inst->SrcReg[0], machine, a);
750 result[0] = result[1] = result[2] = result[3]
751 = (GLfloat) _mesa_cos(a[0]);
752 store_vector4(inst, machine, result);
753 }
754 break;
755 case OPCODE_DDX: /* Partial derivative with respect to X */
756 {
757 #if 0
758 GLfloat a[4], aNext[4], result[4];
759 struct gl_program_machine dMachine;
760 if (!fetch_vector4_deriv(ctx, &inst->SrcReg[0], span, 'X',
761 column, result)) {
762 /* This is tricky. Make a copy of the current machine state,
763 * increment the input registers by the dx or dy partial
764 * derivatives, then re-execute the program up to the
765 * preceeding instruction, then fetch the source register.
766 * Finally, find the difference in the register values for
767 * the original and derivative runs.
768 */
769 fetch_vector4(&inst->SrcReg[0], machine, program, a);
770 init_machine_deriv(ctx, machine, program, span,
771 'X', &dMachine);
772 execute_program(ctx, program, pc, &dMachine, span, column);
773 fetch_vector4(&inst->SrcReg[0], &dMachine, program,
774 aNext);
775 result[0] = aNext[0] - a[0];
776 result[1] = aNext[1] - a[1];
777 result[2] = aNext[2] - a[2];
778 result[3] = aNext[3] - a[3];
779 }
780 store_vector4(inst, machine, result);
781 #else
782 store_vector4(inst, machine, ZeroVec);
783 #endif
784 }
785 break;
786 case OPCODE_DDY: /* Partial derivative with respect to Y */
787 {
788 #if 0
789 GLfloat a[4], aNext[4], result[4];
790 struct gl_program_machine dMachine;
791 if (!fetch_vector4_deriv(ctx, &inst->SrcReg[0], span, 'Y',
792 column, result)) {
793 init_machine_deriv(ctx, machine, program, span,
794 'Y', &dMachine);
795 fetch_vector4(&inst->SrcReg[0], machine, program, a);
796 execute_program(ctx, program, pc, &dMachine, span, column);
797 fetch_vector4(&inst->SrcReg[0], &dMachine, program,
798 aNext);
799 result[0] = aNext[0] - a[0];
800 result[1] = aNext[1] - a[1];
801 result[2] = aNext[2] - a[2];
802 result[3] = aNext[3] - a[3];
803 }
804 store_vector4(inst, machine, result);
805 #else
806 store_vector4(inst, machine, ZeroVec);
807 #endif
808 }
809 break;
810 case OPCODE_DP3:
811 {
812 GLfloat a[4], b[4], result[4];
813 fetch_vector4(&inst->SrcReg[0], machine, a);
814 fetch_vector4(&inst->SrcReg[1], machine, b);
815 result[0] = result[1] = result[2] = result[3] = DOT3(a, b);
816 store_vector4(inst, machine, result);
817 if (DEBUG_PROG) {
818 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
819 result[0], a[0], a[1], a[2], b[0], b[1], b[2]);
820 }
821 }
822 break;
823 case OPCODE_DP4:
824 {
825 GLfloat a[4], b[4], result[4];
826 fetch_vector4(&inst->SrcReg[0], machine, a);
827 fetch_vector4(&inst->SrcReg[1], machine, b);
828 result[0] = result[1] = result[2] = result[3] = DOT4(a, b);
829 store_vector4(inst, machine, result);
830 if (DEBUG_PROG) {
831 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
832 result[0], a[0], a[1], a[2], a[3],
833 b[0], b[1], b[2], b[3]);
834 }
835 }
836 break;
837 case OPCODE_DPH:
838 {
839 GLfloat a[4], b[4], result[4];
840 fetch_vector4(&inst->SrcReg[0], machine, a);
841 fetch_vector4(&inst->SrcReg[1], machine, b);
842 result[0] = result[1] = result[2] = result[3] =
843 a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + b[3];
844 store_vector4(inst, machine, result);
845 }
846 break;
847 case OPCODE_DST: /* Distance vector */
848 {
849 GLfloat a[4], b[4], result[4];
850 fetch_vector4(&inst->SrcReg[0], machine, a);
851 fetch_vector4(&inst->SrcReg[1], machine, b);
852 result[0] = 1.0F;
853 result[1] = a[1] * b[1];
854 result[2] = a[2];
855 result[3] = b[3];
856 store_vector4(inst, machine, result);
857 }
858 break;
859 case OPCODE_EXP:
860 {
861 GLfloat t[4], q[4], floor_t0;
862 fetch_vector1(&inst->SrcReg[0], machine, t);
863 floor_t0 = FLOORF(t[0]);
864 if (floor_t0 > FLT_MAX_EXP) {
865 SET_POS_INFINITY(q[0]);
866 SET_POS_INFINITY(q[2]);
867 }
868 else if (floor_t0 < FLT_MIN_EXP) {
869 q[0] = 0.0F;
870 q[2] = 0.0F;
871 }
872 else {
873 q[0] = LDEXPF(1.0, (int) floor_t0);
874 /* Note: GL_NV_vertex_program expects
875 * result.z = result.x * APPX(result.y)
876 * We do what the ARB extension says.
877 */
878 q[2] = pow(2.0, t[0]);
879 }
880 q[1] = t[0] - floor_t0;
881 q[3] = 1.0F;
882 store_vector4( inst, machine, q );
883 }
884 break;
885 case OPCODE_EX2: /* Exponential base 2 */
886 {
887 GLfloat a[4], result[4];
888 fetch_vector1(&inst->SrcReg[0], machine, a);
889 result[0] = result[1] = result[2] = result[3] =
890 (GLfloat) _mesa_pow(2.0, a[0]);
891 store_vector4(inst, machine, result);
892 }
893 break;
894 case OPCODE_FLR:
895 {
896 GLfloat a[4], result[4];
897 fetch_vector4(&inst->SrcReg[0], machine, a);
898 result[0] = FLOORF(a[0]);
899 result[1] = FLOORF(a[1]);
900 result[2] = FLOORF(a[2]);
901 result[3] = FLOORF(a[3]);
902 store_vector4(inst, machine, result);
903 }
904 break;
905 case OPCODE_FRC:
906 {
907 GLfloat a[4], result[4];
908 fetch_vector4(&inst->SrcReg[0], machine, a);
909 result[0] = a[0] - FLOORF(a[0]);
910 result[1] = a[1] - FLOORF(a[1]);
911 result[2] = a[2] - FLOORF(a[2]);
912 result[3] = a[3] - FLOORF(a[3]);
913 store_vector4(inst, machine, result);
914 }
915 break;
916 case OPCODE_IF:
917 if (eval_condition(machine, inst)) {
918 /* do if-clause (just continue execution) */
919 }
920 else {
921 /* go to the instruction after ELSE or ENDIF */
922 assert(inst->BranchTarget >= 0);
923 pc = inst->BranchTarget - 1;
924 }
925 break;
926 case OPCODE_ELSE:
927 /* goto ENDIF */
928 assert(inst->BranchTarget >= 0);
929 pc = inst->BranchTarget - 1;
930 break;
931 case OPCODE_ENDIF:
932 /* nothing */
933 break;
934 case OPCODE_INT: /* float to int */
935 {
936 GLfloat a[4], result[4];
937 fetch_vector4(&inst->SrcReg[0], machine, a);
938 result[0] = (GLfloat) (GLint) a[0];
939 result[1] = (GLfloat) (GLint) a[1];
940 result[2] = (GLfloat) (GLint) a[2];
941 result[3] = (GLfloat) (GLint) a[3];
942 store_vector4(inst, machine, result);
943 }
944 break;
945 case OPCODE_KIL_NV: /* NV_f_p only (conditional) */
946 if (eval_condition(machine, inst)) {
947 return GL_FALSE;
948 }
949 break;
950 case OPCODE_KIL: /* ARB_f_p only */
951 {
952 GLfloat a[4];
953 fetch_vector4(&inst->SrcReg[0], machine, a);
954 if (a[0] < 0.0F || a[1] < 0.0F || a[2] < 0.0F || a[3] < 0.0F) {
955 return GL_FALSE;
956 }
957 }
958 break;
959 case OPCODE_LG2: /* log base 2 */
960 {
961 GLfloat a[4], result[4];
962 fetch_vector1(&inst->SrcReg[0], machine, a);
963 result[0] = result[1] = result[2] = result[3] = LOG2(a[0]);
964 store_vector4(inst, machine, result);
965 }
966 break;
967 case OPCODE_LIT:
968 {
969 const GLfloat epsilon = 1.0F / 256.0F; /* from NV VP spec */
970 GLfloat a[4], result[4];
971 fetch_vector4(&inst->SrcReg[0], machine, a);
972 a[0] = MAX2(a[0], 0.0F);
973 a[1] = MAX2(a[1], 0.0F);
974 /* XXX ARB version clamps a[3], NV version doesn't */
975 a[3] = CLAMP(a[3], -(128.0F - epsilon), (128.0F - epsilon));
976 result[0] = 1.0F;
977 result[1] = a[0];
978 /* XXX we could probably just use pow() here */
979 if (a[0] > 0.0F) {
980 if (a[1] == 0.0 && a[3] == 0.0)
981 result[2] = 1.0;
982 else
983 result[2] = EXPF(a[3] * LOGF(a[1]));
984 }
985 else {
986 result[2] = 0.0;
987 }
988 result[3] = 1.0F;
989 store_vector4(inst, machine, result);
990 if (DEBUG_PROG) {
991 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
992 result[0], result[1], result[2], result[3],
993 a[0], a[1], a[2], a[3]);
994 }
995 }
996 break;
997 case OPCODE_LOG:
998 {
999 GLfloat t[4], q[4], abs_t0;
1000 fetch_vector1(&inst->SrcReg[0], machine, t);
1001 abs_t0 = FABSF(t[0]);
1002 if (abs_t0 != 0.0F) {
1003 /* Since we really can't handle infinite values on VMS
1004 * like other OSes we'll use __MAXFLOAT to represent
1005 * infinity. This may need some tweaking.
1006 */
1007 #ifdef VMS
1008 if (abs_t0 == __MAXFLOAT)
1009 #else
1010 if (IS_INF_OR_NAN(abs_t0))
1011 #endif
1012 {
1013 SET_POS_INFINITY(q[0]);
1014 q[1] = 1.0F;
1015 SET_POS_INFINITY(q[2]);
1016 }
1017 else {
1018 int exponent;
1019 GLfloat mantissa = FREXPF(t[0], &exponent);
1020 q[0] = (GLfloat) (exponent - 1);
1021 q[1] = (GLfloat) (2.0 * mantissa); /* map [.5, 1) -> [1, 2) */
1022 q[2] = (GLfloat) (q[0] + LOG2(q[1]));
1023 }
1024 }
1025 else {
1026 SET_NEG_INFINITY(q[0]);
1027 q[1] = 1.0F;
1028 SET_NEG_INFINITY(q[2]);
1029 }
1030 q[3] = 1.0;
1031 store_vector4(inst, machine, q);
1032 }
1033 break;
1034 case OPCODE_LRP:
1035 {
1036 GLfloat a[4], b[4], c[4], result[4];
1037 fetch_vector4(&inst->SrcReg[0], machine, a);
1038 fetch_vector4(&inst->SrcReg[1], machine, b);
1039 fetch_vector4(&inst->SrcReg[2], machine, c);
1040 result[0] = a[0] * b[0] + (1.0F - a[0]) * c[0];
1041 result[1] = a[1] * b[1] + (1.0F - a[1]) * c[1];
1042 result[2] = a[2] * b[2] + (1.0F - a[2]) * c[2];
1043 result[3] = a[3] * b[3] + (1.0F - a[3]) * c[3];
1044 store_vector4(inst, machine, result);
1045 if (DEBUG_PROG) {
1046 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
1047 "(%g %g %g %g), (%g %g %g %g)\n",
1048 result[0], result[1], result[2], result[3],
1049 a[0], a[1], a[2], a[3],
1050 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
1051 }
1052 }
1053 break;
1054 case OPCODE_MAD:
1055 {
1056 GLfloat a[4], b[4], c[4], result[4];
1057 fetch_vector4(&inst->SrcReg[0], machine, a);
1058 fetch_vector4(&inst->SrcReg[1], machine, b);
1059 fetch_vector4(&inst->SrcReg[2], machine, c);
1060 result[0] = a[0] * b[0] + c[0];
1061 result[1] = a[1] * b[1] + c[1];
1062 result[2] = a[2] * b[2] + c[2];
1063 result[3] = a[3] * b[3] + c[3];
1064 store_vector4(inst, machine, result);
1065 if (DEBUG_PROG) {
1066 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
1067 "(%g %g %g %g) + (%g %g %g %g)\n",
1068 result[0], result[1], result[2], result[3],
1069 a[0], a[1], a[2], a[3],
1070 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
1071 }
1072 }
1073 break;
1074 case OPCODE_MAX:
1075 {
1076 GLfloat a[4], b[4], result[4];
1077 fetch_vector4(&inst->SrcReg[0], machine, a);
1078 fetch_vector4(&inst->SrcReg[1], machine, b);
1079 result[0] = MAX2(a[0], b[0]);
1080 result[1] = MAX2(a[1], b[1]);
1081 result[2] = MAX2(a[2], b[2]);
1082 result[3] = MAX2(a[3], b[3]);
1083 store_vector4(inst, machine, result);
1084 if (DEBUG_PROG) {
1085 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
1086 result[0], result[1], result[2], result[3],
1087 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
1088 }
1089 }
1090 break;
1091 case OPCODE_MIN:
1092 {
1093 GLfloat a[4], b[4], result[4];
1094 fetch_vector4(&inst->SrcReg[0], machine, a);
1095 fetch_vector4(&inst->SrcReg[1], machine, b);
1096 result[0] = MIN2(a[0], b[0]);
1097 result[1] = MIN2(a[1], b[1]);
1098 result[2] = MIN2(a[2], b[2]);
1099 result[3] = MIN2(a[3], b[3]);
1100 store_vector4(inst, machine, result);
1101 }
1102 break;
1103 case OPCODE_MOV:
1104 {
1105 GLfloat result[4];
1106 fetch_vector4(&inst->SrcReg[0], machine, result);
1107 store_vector4(inst, machine, result);
1108 if (DEBUG_PROG) {
1109 printf("MOV (%g %g %g %g)\n",
1110 result[0], result[1], result[2], result[3]);
1111 }
1112 }
1113 break;
1114 case OPCODE_MUL:
1115 {
1116 GLfloat a[4], b[4], result[4];
1117 fetch_vector4(&inst->SrcReg[0], machine, a);
1118 fetch_vector4(&inst->SrcReg[1], machine, b);
1119 result[0] = a[0] * b[0];
1120 result[1] = a[1] * b[1];
1121 result[2] = a[2] * b[2];
1122 result[3] = a[3] * b[3];
1123 store_vector4(inst, machine, result);
1124 if (DEBUG_PROG) {
1125 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1126 result[0], result[1], result[2], result[3],
1127 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
1128 }
1129 }
1130 break;
1131 case OPCODE_NOISE1:
1132 {
1133 GLfloat a[4], result[4];
1134 fetch_vector1(&inst->SrcReg[0], machine, a);
1135 result[0] =
1136 result[1] =
1137 result[2] = result[3] = _slang_library_noise1(a[0]);
1138 store_vector4(inst, machine, result);
1139 }
1140 break;
1141 case OPCODE_NOISE2:
1142 {
1143 GLfloat a[4], result[4];
1144 fetch_vector4(&inst->SrcReg[0], machine, a);
1145 result[0] =
1146 result[1] =
1147 result[2] = result[3] = _slang_library_noise2(a[0], a[1]);
1148 store_vector4(inst, machine, result);
1149 }
1150 break;
1151 case OPCODE_NOISE3:
1152 {
1153 GLfloat a[4], result[4];
1154 fetch_vector4(&inst->SrcReg[0], machine, a);
1155 result[0] =
1156 result[1] =
1157 result[2] =
1158 result[3] = _slang_library_noise3(a[0], a[1], a[2]);
1159 store_vector4(inst, machine, result);
1160 }
1161 break;
1162 case OPCODE_NOISE4:
1163 {
1164 GLfloat a[4], result[4];
1165 fetch_vector4(&inst->SrcReg[0], machine, a);
1166 result[0] =
1167 result[1] =
1168 result[2] =
1169 result[3] = _slang_library_noise4(a[0], a[1], a[2], a[3]);
1170 store_vector4(inst, machine, result);
1171 }
1172 break;
1173 case OPCODE_NOP:
1174 break;
1175 case OPCODE_PK2H: /* pack two 16-bit floats in one 32-bit float */
1176 {
1177 GLfloat a[4], result[4];
1178 GLhalfNV hx, hy;
1179 GLuint *rawResult = (GLuint *) result;
1180 GLuint twoHalves;
1181 fetch_vector4(&inst->SrcReg[0], machine, a);
1182 hx = _mesa_float_to_half(a[0]);
1183 hy = _mesa_float_to_half(a[1]);
1184 twoHalves = hx | (hy << 16);
1185 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
1186 = twoHalves;
1187 store_vector4(inst, machine, result);
1188 }
1189 break;
1190 case OPCODE_PK2US: /* pack two GLushorts into one 32-bit float */
1191 {
1192 GLfloat a[4], result[4];
1193 GLuint usx, usy, *rawResult = (GLuint *) result;
1194 fetch_vector4(&inst->SrcReg[0], machine, a);
1195 a[0] = CLAMP(a[0], 0.0F, 1.0F);
1196 a[1] = CLAMP(a[1], 0.0F, 1.0F);
1197 usx = IROUND(a[0] * 65535.0F);
1198 usy = IROUND(a[1] * 65535.0F);
1199 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
1200 = usx | (usy << 16);
1201 store_vector4(inst, machine, result);
1202 }
1203 break;
1204 case OPCODE_PK4B: /* pack four GLbytes into one 32-bit float */
1205 {
1206 GLfloat a[4], result[4];
1207 GLuint ubx, uby, ubz, ubw, *rawResult = (GLuint *) result;
1208 fetch_vector4(&inst->SrcReg[0], machine, a);
1209 a[0] = CLAMP(a[0], -128.0F / 127.0F, 1.0F);
1210 a[1] = CLAMP(a[1], -128.0F / 127.0F, 1.0F);
1211 a[2] = CLAMP(a[2], -128.0F / 127.0F, 1.0F);
1212 a[3] = CLAMP(a[3], -128.0F / 127.0F, 1.0F);
1213 ubx = IROUND(127.0F * a[0] + 128.0F);
1214 uby = IROUND(127.0F * a[1] + 128.0F);
1215 ubz = IROUND(127.0F * a[2] + 128.0F);
1216 ubw = IROUND(127.0F * a[3] + 128.0F);
1217 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
1218 = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
1219 store_vector4(inst, machine, result);
1220 }
1221 break;
1222 case OPCODE_PK4UB: /* pack four GLubytes into one 32-bit float */
1223 {
1224 GLfloat a[4], result[4];
1225 GLuint ubx, uby, ubz, ubw, *rawResult = (GLuint *) result;
1226 fetch_vector4(&inst->SrcReg[0], machine, a);
1227 a[0] = CLAMP(a[0], 0.0F, 1.0F);
1228 a[1] = CLAMP(a[1], 0.0F, 1.0F);
1229 a[2] = CLAMP(a[2], 0.0F, 1.0F);
1230 a[3] = CLAMP(a[3], 0.0F, 1.0F);
1231 ubx = IROUND(255.0F * a[0]);
1232 uby = IROUND(255.0F * a[1]);
1233 ubz = IROUND(255.0F * a[2]);
1234 ubw = IROUND(255.0F * a[3]);
1235 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
1236 = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
1237 store_vector4(inst, machine, result);
1238 }
1239 break;
1240 case OPCODE_POW:
1241 {
1242 GLfloat a[4], b[4], result[4];
1243 fetch_vector1(&inst->SrcReg[0], machine, a);
1244 fetch_vector1(&inst->SrcReg[1], machine, b);
1245 result[0] = result[1] = result[2] = result[3]
1246 = (GLfloat) _mesa_pow(a[0], b[0]);
1247 store_vector4(inst, machine, result);
1248 }
1249 break;
1250 case OPCODE_RCP:
1251 {
1252 GLfloat a[4], result[4];
1253 fetch_vector1(&inst->SrcReg[0], machine, a);
1254 if (DEBUG_PROG) {
1255 if (a[0] == 0)
1256 printf("RCP(0)\n");
1257 else if (IS_INF_OR_NAN(a[0]))
1258 printf("RCP(inf)\n");
1259 }
1260 result[0] = result[1] = result[2] = result[3] = 1.0F / a[0];
1261 store_vector4(inst, machine, result);
1262 }
1263 break;
1264 case OPCODE_RET: /* return from subroutine (conditional) */
1265 if (eval_condition(machine, inst)) {
1266 if (machine->StackDepth == 0) {
1267 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
1268 }
1269 pc = machine->CallStack[--machine->StackDepth];
1270 }
1271 break;
1272 case OPCODE_RFL: /* reflection vector */
1273 {
1274 GLfloat axis[4], dir[4], result[4], tmpX, tmpW;
1275 fetch_vector4(&inst->SrcReg[0], machine, axis);
1276 fetch_vector4(&inst->SrcReg[1], machine, dir);
1277 tmpW = DOT3(axis, axis);
1278 tmpX = (2.0F * DOT3(axis, dir)) / tmpW;
1279 result[0] = tmpX * axis[0] - dir[0];
1280 result[1] = tmpX * axis[1] - dir[1];
1281 result[2] = tmpX * axis[2] - dir[2];
1282 /* result[3] is never written! XXX enforce in parser! */
1283 store_vector4(inst, machine, result);
1284 }
1285 break;
1286 case OPCODE_RSQ: /* 1 / sqrt() */
1287 {
1288 GLfloat a[4], result[4];
1289 fetch_vector1(&inst->SrcReg[0], machine, a);
1290 a[0] = FABSF(a[0]);
1291 result[0] = result[1] = result[2] = result[3] = INV_SQRTF(a[0]);
1292 store_vector4(inst, machine, result);
1293 if (DEBUG_PROG) {
1294 printf("RSQ %g = 1/sqrt(|%g|)\n", result[0], a[0]);
1295 }
1296 }
1297 break;
1298 case OPCODE_SCS: /* sine and cos */
1299 {
1300 GLfloat a[4], result[4];
1301 fetch_vector1(&inst->SrcReg[0], machine, a);
1302 result[0] = (GLfloat) _mesa_cos(a[0]);
1303 result[1] = (GLfloat) _mesa_sin(a[0]);
1304 result[2] = 0.0; /* undefined! */
1305 result[3] = 0.0; /* undefined! */
1306 store_vector4(inst, machine, result);
1307 }
1308 break;
1309 case OPCODE_SEQ: /* set on equal */
1310 {
1311 GLfloat a[4], b[4], result[4];
1312 fetch_vector4(&inst->SrcReg[0], machine, a);
1313 fetch_vector4(&inst->SrcReg[1], machine, b);
1314 result[0] = (a[0] == b[0]) ? 1.0F : 0.0F;
1315 result[1] = (a[1] == b[1]) ? 1.0F : 0.0F;
1316 result[2] = (a[2] == b[2]) ? 1.0F : 0.0F;
1317 result[3] = (a[3] == b[3]) ? 1.0F : 0.0F;
1318 store_vector4(inst, machine, result);
1319 if (DEBUG_PROG) {
1320 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1321 result[0], result[1], result[2], result[3],
1322 a[0], a[1], a[2], a[3],
1323 b[0], b[1], b[2], b[3]);
1324 }
1325 }
1326 break;
1327 case OPCODE_SFL: /* set false, operands ignored */
1328 {
1329 static const GLfloat result[4] = { 0.0F, 0.0F, 0.0F, 0.0F };
1330 store_vector4(inst, machine, result);
1331 }
1332 break;
1333 case OPCODE_SGE: /* set on greater or equal */
1334 {
1335 GLfloat a[4], b[4], result[4];
1336 fetch_vector4(&inst->SrcReg[0], machine, a);
1337 fetch_vector4(&inst->SrcReg[1], machine, b);
1338 result[0] = (a[0] >= b[0]) ? 1.0F : 0.0F;
1339 result[1] = (a[1] >= b[1]) ? 1.0F : 0.0F;
1340 result[2] = (a[2] >= b[2]) ? 1.0F : 0.0F;
1341 result[3] = (a[3] >= b[3]) ? 1.0F : 0.0F;
1342 store_vector4(inst, machine, result);
1343 if (DEBUG_PROG) {
1344 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1345 result[0], result[1], result[2], result[3],
1346 a[0], a[1], a[2], a[3],
1347 b[0], b[1], b[2], b[3]);
1348 }
1349 }
1350 break;
1351 case OPCODE_SGT: /* set on greater */
1352 {
1353 GLfloat a[4], b[4], result[4];
1354 fetch_vector4(&inst->SrcReg[0], machine, a);
1355 fetch_vector4(&inst->SrcReg[1], machine, b);
1356 result[0] = (a[0] > b[0]) ? 1.0F : 0.0F;
1357 result[1] = (a[1] > b[1]) ? 1.0F : 0.0F;
1358 result[2] = (a[2] > b[2]) ? 1.0F : 0.0F;
1359 result[3] = (a[3] > b[3]) ? 1.0F : 0.0F;
1360 store_vector4(inst, machine, result);
1361 if (DEBUG_PROG) {
1362 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1363 result[0], result[1], result[2], result[3],
1364 a[0], a[1], a[2], a[3],
1365 b[0], b[1], b[2], b[3]);
1366 }
1367 }
1368 break;
1369 case OPCODE_SIN:
1370 {
1371 GLfloat a[4], result[4];
1372 fetch_vector1(&inst->SrcReg[0], machine, a);
1373 result[0] = result[1] = result[2] = result[3]
1374 = (GLfloat) _mesa_sin(a[0]);
1375 store_vector4(inst, machine, result);
1376 }
1377 break;
1378 case OPCODE_SLE: /* set on less or equal */
1379 {
1380 GLfloat a[4], b[4], result[4];
1381 fetch_vector4(&inst->SrcReg[0], machine, a);
1382 fetch_vector4(&inst->SrcReg[1], machine, b);
1383 result[0] = (a[0] <= b[0]) ? 1.0F : 0.0F;
1384 result[1] = (a[1] <= b[1]) ? 1.0F : 0.0F;
1385 result[2] = (a[2] <= b[2]) ? 1.0F : 0.0F;
1386 result[3] = (a[3] <= b[3]) ? 1.0F : 0.0F;
1387 store_vector4(inst, machine, result);
1388 if (DEBUG_PROG) {
1389 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1390 result[0], result[1], result[2], result[3],
1391 a[0], a[1], a[2], a[3],
1392 b[0], b[1], b[2], b[3]);
1393 }
1394 }
1395 break;
1396 case OPCODE_SLT: /* set on less */
1397 {
1398 GLfloat a[4], b[4], result[4];
1399 fetch_vector4(&inst->SrcReg[0], machine, a);
1400 fetch_vector4(&inst->SrcReg[1], machine, b);
1401 result[0] = (a[0] < b[0]) ? 1.0F : 0.0F;
1402 result[1] = (a[1] < b[1]) ? 1.0F : 0.0F;
1403 result[2] = (a[2] < b[2]) ? 1.0F : 0.0F;
1404 result[3] = (a[3] < b[3]) ? 1.0F : 0.0F;
1405 store_vector4(inst, machine, result);
1406 if (DEBUG_PROG) {
1407 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1408 result[0], result[1], result[2], result[3],
1409 a[0], a[1], a[2], a[3],
1410 b[0], b[1], b[2], b[3]);
1411 }
1412 }
1413 break;
1414 case OPCODE_SNE: /* set on not equal */
1415 {
1416 GLfloat a[4], b[4], result[4];
1417 fetch_vector4(&inst->SrcReg[0], machine, a);
1418 fetch_vector4(&inst->SrcReg[1], machine, b);
1419 result[0] = (a[0] != b[0]) ? 1.0F : 0.0F;
1420 result[1] = (a[1] != b[1]) ? 1.0F : 0.0F;
1421 result[2] = (a[2] != b[2]) ? 1.0F : 0.0F;
1422 result[3] = (a[3] != b[3]) ? 1.0F : 0.0F;
1423 store_vector4(inst, machine, result);
1424 if (DEBUG_PROG) {
1425 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1426 result[0], result[1], result[2], result[3],
1427 a[0], a[1], a[2], a[3],
1428 b[0], b[1], b[2], b[3]);
1429 }
1430 }
1431 break;
1432 case OPCODE_STR: /* set true, operands ignored */
1433 {
1434 static const GLfloat result[4] = { 1.0F, 1.0F, 1.0F, 1.0F };
1435 store_vector4(inst, machine, result);
1436 }
1437 break;
1438 case OPCODE_SUB:
1439 {
1440 GLfloat a[4], b[4], result[4];
1441 fetch_vector4(&inst->SrcReg[0], machine, a);
1442 fetch_vector4(&inst->SrcReg[1], machine, b);
1443 result[0] = a[0] - b[0];
1444 result[1] = a[1] - b[1];
1445 result[2] = a[2] - b[2];
1446 result[3] = a[3] - b[3];
1447 store_vector4(inst, machine, result);
1448 if (DEBUG_PROG) {
1449 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1450 result[0], result[1], result[2], result[3],
1451 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
1452 }
1453 }
1454 break;
1455 case OPCODE_SWZ: /* extended swizzle */
1456 {
1457 const struct prog_src_register *source = &inst->SrcReg[0];
1458 const GLfloat *src = get_register_pointer(source, machine);
1459 GLfloat result[4];
1460 GLuint i;
1461 for (i = 0; i < 4; i++) {
1462 const GLuint swz = GET_SWZ(source->Swizzle, i);
1463 if (swz == SWIZZLE_ZERO)
1464 result[i] = 0.0;
1465 else if (swz == SWIZZLE_ONE)
1466 result[i] = 1.0;
1467 else {
1468 ASSERT(swz >= 0);
1469 ASSERT(swz <= 3);
1470 result[i] = src[swz];
1471 }
1472 if (source->NegateBase & (1 << i))
1473 result[i] = -result[i];
1474 }
1475 store_vector4(inst, machine, result);
1476 }
1477 break;
1478 case OPCODE_TEX: /* Both ARB and NV frag prog */
1479 /* Texel lookup */
1480 {
1481 /* Note: only use the precomputed lambda value when we're
1482 * sampling texture unit [K] with texcoord[K].
1483 * Otherwise, the lambda value may have no relation to the
1484 * instruction's texcoord or texture image. Using the wrong
1485 * lambda is usually bad news.
1486 * The rest of the time, just use zero (until we get a more
1487 * sophisticated way of computing lambda).
1488 */
1489 GLfloat coord[4], color[4], lambda;
1490 #if 0
1491 if (inst->SrcReg[0].File == PROGRAM_INPUT &&
1492 inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0 + inst->TexSrcUnit)
1493 lambda = span->array->lambda[inst->TexSrcUnit][column];
1494 else
1495 #endif
1496 lambda = 0.0;
1497 fetch_vector4(&inst->SrcReg[0], machine, coord);
1498 machine->FetchTexelLod(ctx, coord, lambda, inst->TexSrcUnit,
1499 color);
1500 if (DEBUG_PROG) {
1501 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g], "
1502 "lod %f\n",
1503 color[0], color[1], color[2], color[3],
1504 inst->TexSrcUnit,
1505 coord[0], coord[1], coord[2], coord[3], lambda);
1506 }
1507 store_vector4(inst, machine, color);
1508 }
1509 break;
1510 case OPCODE_TXB: /* GL_ARB_fragment_program only */
1511 /* Texel lookup with LOD bias */
1512 {
1513 const struct gl_texture_unit *texUnit
1514 = &ctx->Texture.Unit[inst->TexSrcUnit];
1515 GLfloat coord[4], color[4], lambda, bias;
1516 #if 0
1517 if (inst->SrcReg[0].File == PROGRAM_INPUT &&
1518 inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0 + inst->TexSrcUnit)
1519 lambda = span->array->lambda[inst->TexSrcUnit][column];
1520 else
1521 #endif
1522 lambda = 0.0;
1523 fetch_vector4(&inst->SrcReg[0], machine, coord);
1524 /* coord[3] is the bias to add to lambda */
1525 bias = texUnit->LodBias + coord[3];
1526 if (texUnit->_Current)
1527 bias += texUnit->_Current->LodBias;
1528 machine->FetchTexelLod(ctx, coord, lambda + bias,
1529 inst->TexSrcUnit, color);
1530 store_vector4(inst, machine, color);
1531 }
1532 break;
1533 case OPCODE_TXD: /* GL_NV_fragment_program only */
1534 /* Texture lookup w/ partial derivatives for LOD */
1535 {
1536 GLfloat texcoord[4], dtdx[4], dtdy[4], color[4];
1537 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1538 fetch_vector4(&inst->SrcReg[1], machine, dtdx);
1539 fetch_vector4(&inst->SrcReg[2], machine, dtdy);
1540 machine->FetchTexelDeriv(ctx, texcoord, dtdx, dtdy,
1541 inst->TexSrcUnit, color);
1542 store_vector4(inst, machine, color);
1543 }
1544 break;
1545 case OPCODE_TXP: /* GL_ARB_fragment_program only */
1546 /* Texture lookup w/ projective divide */
1547 {
1548 GLfloat texcoord[4], color[4], lambda;
1549 #if 0
1550 if (inst->SrcReg[0].File == PROGRAM_INPUT &&
1551 inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0 + inst->TexSrcUnit)
1552 lambda = span->array->lambda[inst->TexSrcUnit][column];
1553 else
1554 #endif
1555 lambda = 0.0;
1556 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1557 /* Not so sure about this test - if texcoord[3] is
1558 * zero, we'd probably be fine except for an ASSERT in
1559 * IROUND_POS() which gets triggered by the inf values created.
1560 */
1561 if (texcoord[3] != 0.0) {
1562 texcoord[0] /= texcoord[3];
1563 texcoord[1] /= texcoord[3];
1564 texcoord[2] /= texcoord[3];
1565 }
1566 machine->FetchTexelLod(ctx, texcoord, lambda,
1567 inst->TexSrcUnit, color);
1568 store_vector4(inst, machine, color);
1569 }
1570 break;
1571 case OPCODE_TXP_NV: /* GL_NV_fragment_program only */
1572 /* Texture lookup w/ projective divide */
1573 {
1574 GLfloat texcoord[4], color[4], lambda;
1575 #if 0
1576 if (inst->SrcReg[0].File == PROGRAM_INPUT &&
1577 inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0 + inst->TexSrcUnit)
1578 lambda = span->array->lambda[inst->TexSrcUnit][column];
1579 else
1580 #endif
1581 lambda = 0.0;
1582 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1583 if (inst->TexSrcTarget != TEXTURE_CUBE_INDEX &&
1584 texcoord[3] != 0.0) {
1585 texcoord[0] /= texcoord[3];
1586 texcoord[1] /= texcoord[3];
1587 texcoord[2] /= texcoord[3];
1588 }
1589 machine->FetchTexelLod(ctx, texcoord, lambda,
1590 inst->TexSrcUnit, color);
1591 store_vector4(inst, machine, color);
1592 }
1593 break;
1594 case OPCODE_UP2H: /* unpack two 16-bit floats */
1595 {
1596 GLfloat a[4], result[4];
1597 const GLuint *rawBits = (const GLuint *) a;
1598 GLhalfNV hx, hy;
1599 fetch_vector1(&inst->SrcReg[0], machine, a);
1600 hx = rawBits[0] & 0xffff;
1601 hy = rawBits[0] >> 16;
1602 result[0] = result[2] = _mesa_half_to_float(hx);
1603 result[1] = result[3] = _mesa_half_to_float(hy);
1604 store_vector4(inst, machine, result);
1605 }
1606 break;
1607 case OPCODE_UP2US: /* unpack two GLushorts */
1608 {
1609 GLfloat a[4], result[4];
1610 const GLuint *rawBits = (const GLuint *) a;
1611 GLushort usx, usy;
1612 fetch_vector1(&inst->SrcReg[0], machine, a);
1613 usx = rawBits[0] & 0xffff;
1614 usy = rawBits[0] >> 16;
1615 result[0] = result[2] = usx * (1.0f / 65535.0f);
1616 result[1] = result[3] = usy * (1.0f / 65535.0f);
1617 store_vector4(inst, machine, result);
1618 }
1619 break;
1620 case OPCODE_UP4B: /* unpack four GLbytes */
1621 {
1622 GLfloat a[4], result[4];
1623 const GLuint *rawBits = (const GLuint *) a;
1624 fetch_vector1(&inst->SrcReg[0], machine, a);
1625 result[0] = (((rawBits[0] >> 0) & 0xff) - 128) / 127.0F;
1626 result[1] = (((rawBits[0] >> 8) & 0xff) - 128) / 127.0F;
1627 result[2] = (((rawBits[0] >> 16) & 0xff) - 128) / 127.0F;
1628 result[3] = (((rawBits[0] >> 24) & 0xff) - 128) / 127.0F;
1629 store_vector4(inst, machine, result);
1630 }
1631 break;
1632 case OPCODE_UP4UB: /* unpack four GLubytes */
1633 {
1634 GLfloat a[4], result[4];
1635 const GLuint *rawBits = (const GLuint *) a;
1636 fetch_vector1(&inst->SrcReg[0], machine, a);
1637 result[0] = ((rawBits[0] >> 0) & 0xff) / 255.0F;
1638 result[1] = ((rawBits[0] >> 8) & 0xff) / 255.0F;
1639 result[2] = ((rawBits[0] >> 16) & 0xff) / 255.0F;
1640 result[3] = ((rawBits[0] >> 24) & 0xff) / 255.0F;
1641 store_vector4(inst, machine, result);
1642 }
1643 break;
1644 case OPCODE_XPD: /* cross product */
1645 {
1646 GLfloat a[4], b[4], result[4];
1647 fetch_vector4(&inst->SrcReg[0], machine, a);
1648 fetch_vector4(&inst->SrcReg[1], machine, b);
1649 result[0] = a[1] * b[2] - a[2] * b[1];
1650 result[1] = a[2] * b[0] - a[0] * b[2];
1651 result[2] = a[0] * b[1] - a[1] * b[0];
1652 result[3] = 1.0;
1653 store_vector4(inst, machine, result);
1654 if (DEBUG_PROG) {
1655 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1656 result[0], result[1], result[2], result[3],
1657 a[0], a[1], a[2], b[0], b[1], b[2]);
1658 }
1659 }
1660 break;
1661 case OPCODE_X2D: /* 2-D matrix transform */
1662 {
1663 GLfloat a[4], b[4], c[4], result[4];
1664 fetch_vector4(&inst->SrcReg[0], machine, a);
1665 fetch_vector4(&inst->SrcReg[1], machine, b);
1666 fetch_vector4(&inst->SrcReg[2], machine, c);
1667 result[0] = a[0] + b[0] * c[0] + b[1] * c[1];
1668 result[1] = a[1] + b[0] * c[2] + b[1] * c[3];
1669 result[2] = a[2] + b[0] * c[0] + b[1] * c[1];
1670 result[3] = a[3] + b[0] * c[2] + b[1] * c[3];
1671 store_vector4(inst, machine, result);
1672 }
1673 break;
1674 case OPCODE_PRINT:
1675 {
1676 if (inst->SrcReg[0].File != -1) {
1677 GLfloat a[4];
1678 fetch_vector4(&inst->SrcReg[0], machine, a);
1679 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst->Data,
1680 a[0], a[1], a[2], a[3]);
1681 }
1682 else {
1683 _mesa_printf("%s\n", (const char *) inst->Data);
1684 }
1685 }
1686 break;
1687 case OPCODE_END:
1688 return GL_TRUE;
1689 default:
1690 _mesa_problem(ctx, "Bad opcode %d in _mesa_exec_fragment_program",
1691 inst->Opcode);
1692 return GL_TRUE; /* return value doesn't matter */
1693
1694 }
1695
1696 numExec++;
1697 if (numExec > maxExec) {
1698 _mesa_problem(ctx, "Infinite loop detected in fragment program");
1699 return GL_TRUE;
1700 }
1701
1702 } /* for pc */
1703
1704 #if FEATURE_MESA_program_debug
1705 CurrentMachine = NULL;
1706 #endif
1707
1708 return GL_TRUE;
1709 }