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