Merge commit 'origin/master' 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_KIL_NV: /* NV_f_p only (conditional) */
794 if (eval_condition(machine, inst)) {
795 return GL_FALSE;
796 }
797 break;
798 case OPCODE_KIL: /* ARB_f_p only */
799 {
800 GLfloat a[4];
801 fetch_vector4(&inst->SrcReg[0], machine, a);
802 if (a[0] < 0.0F || a[1] < 0.0F || a[2] < 0.0F || a[3] < 0.0F) {
803 return GL_FALSE;
804 }
805 }
806 break;
807 case OPCODE_LG2: /* log base 2 */
808 {
809 GLfloat a[4], result[4];
810 fetch_vector1(&inst->SrcReg[0], machine, a);
811 result[0] = result[1] = result[2] = result[3] = LOG2(a[0]);
812 store_vector4(inst, machine, result);
813 }
814 break;
815 case OPCODE_LIT:
816 {
817 const GLfloat epsilon = 1.0F / 256.0F; /* from NV VP spec */
818 GLfloat a[4], result[4];
819 fetch_vector4(&inst->SrcReg[0], machine, a);
820 a[0] = MAX2(a[0], 0.0F);
821 a[1] = MAX2(a[1], 0.0F);
822 /* XXX ARB version clamps a[3], NV version doesn't */
823 a[3] = CLAMP(a[3], -(128.0F - epsilon), (128.0F - epsilon));
824 result[0] = 1.0F;
825 result[1] = a[0];
826 /* XXX we could probably just use pow() here */
827 if (a[0] > 0.0F) {
828 if (a[1] == 0.0 && a[3] == 0.0)
829 result[2] = 1.0;
830 else
831 result[2] = EXPF(a[3] * LOGF(a[1]));
832 }
833 else {
834 result[2] = 0.0;
835 }
836 result[3] = 1.0F;
837 store_vector4(inst, machine, result);
838 if (DEBUG_PROG) {
839 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
840 result[0], result[1], result[2], result[3],
841 a[0], a[1], a[2], a[3]);
842 }
843 }
844 break;
845 case OPCODE_LOG:
846 {
847 GLfloat t[4], q[4], abs_t0;
848 fetch_vector1(&inst->SrcReg[0], machine, t);
849 abs_t0 = FABSF(t[0]);
850 if (abs_t0 != 0.0F) {
851 /* Since we really can't handle infinite values on VMS
852 * like other OSes we'll use __MAXFLOAT to represent
853 * infinity. This may need some tweaking.
854 */
855 #ifdef VMS
856 if (abs_t0 == __MAXFLOAT)
857 #else
858 if (IS_INF_OR_NAN(abs_t0))
859 #endif
860 {
861 SET_POS_INFINITY(q[0]);
862 q[1] = 1.0F;
863 SET_POS_INFINITY(q[2]);
864 }
865 else {
866 int exponent;
867 GLfloat mantissa = FREXPF(t[0], &exponent);
868 q[0] = (GLfloat) (exponent - 1);
869 q[1] = (GLfloat) (2.0 * mantissa); /* map [.5, 1) -> [1, 2) */
870 q[2] = (GLfloat) (q[0] + LOG2(q[1]));
871 }
872 }
873 else {
874 SET_NEG_INFINITY(q[0]);
875 q[1] = 1.0F;
876 SET_NEG_INFINITY(q[2]);
877 }
878 q[3] = 1.0;
879 store_vector4(inst, machine, q);
880 }
881 break;
882 case OPCODE_LRP:
883 {
884 GLfloat a[4], b[4], c[4], result[4];
885 fetch_vector4(&inst->SrcReg[0], machine, a);
886 fetch_vector4(&inst->SrcReg[1], machine, b);
887 fetch_vector4(&inst->SrcReg[2], machine, c);
888 result[0] = a[0] * b[0] + (1.0F - a[0]) * c[0];
889 result[1] = a[1] * b[1] + (1.0F - a[1]) * c[1];
890 result[2] = a[2] * b[2] + (1.0F - a[2]) * c[2];
891 result[3] = a[3] * b[3] + (1.0F - a[3]) * c[3];
892 store_vector4(inst, machine, result);
893 if (DEBUG_PROG) {
894 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
895 "(%g %g %g %g), (%g %g %g %g)\n",
896 result[0], result[1], result[2], result[3],
897 a[0], a[1], a[2], a[3],
898 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
899 }
900 }
901 break;
902 case OPCODE_MAD:
903 {
904 GLfloat a[4], b[4], c[4], result[4];
905 fetch_vector4(&inst->SrcReg[0], machine, a);
906 fetch_vector4(&inst->SrcReg[1], machine, b);
907 fetch_vector4(&inst->SrcReg[2], machine, c);
908 result[0] = a[0] * b[0] + c[0];
909 result[1] = a[1] * b[1] + c[1];
910 result[2] = a[2] * b[2] + c[2];
911 result[3] = a[3] * b[3] + c[3];
912 store_vector4(inst, machine, result);
913 if (DEBUG_PROG) {
914 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
915 "(%g %g %g %g) + (%g %g %g %g)\n",
916 result[0], result[1], result[2], result[3],
917 a[0], a[1], a[2], a[3],
918 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
919 }
920 }
921 break;
922 case OPCODE_MAX:
923 {
924 GLfloat a[4], b[4], result[4];
925 fetch_vector4(&inst->SrcReg[0], machine, a);
926 fetch_vector4(&inst->SrcReg[1], machine, b);
927 result[0] = MAX2(a[0], b[0]);
928 result[1] = MAX2(a[1], b[1]);
929 result[2] = MAX2(a[2], b[2]);
930 result[3] = MAX2(a[3], b[3]);
931 store_vector4(inst, machine, result);
932 if (DEBUG_PROG) {
933 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
934 result[0], result[1], result[2], result[3],
935 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
936 }
937 }
938 break;
939 case OPCODE_MIN:
940 {
941 GLfloat a[4], b[4], result[4];
942 fetch_vector4(&inst->SrcReg[0], machine, a);
943 fetch_vector4(&inst->SrcReg[1], machine, b);
944 result[0] = MIN2(a[0], b[0]);
945 result[1] = MIN2(a[1], b[1]);
946 result[2] = MIN2(a[2], b[2]);
947 result[3] = MIN2(a[3], b[3]);
948 store_vector4(inst, machine, result);
949 }
950 break;
951 case OPCODE_MOV:
952 {
953 GLfloat result[4];
954 fetch_vector4(&inst->SrcReg[0], machine, result);
955 store_vector4(inst, machine, result);
956 if (DEBUG_PROG) {
957 printf("MOV (%g %g %g %g)\n",
958 result[0], result[1], result[2], result[3]);
959 }
960 }
961 break;
962 case OPCODE_MUL:
963 {
964 GLfloat a[4], b[4], result[4];
965 fetch_vector4(&inst->SrcReg[0], machine, a);
966 fetch_vector4(&inst->SrcReg[1], machine, b);
967 result[0] = a[0] * b[0];
968 result[1] = a[1] * b[1];
969 result[2] = a[2] * b[2];
970 result[3] = a[3] * b[3];
971 store_vector4(inst, machine, result);
972 if (DEBUG_PROG) {
973 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
974 result[0], result[1], result[2], result[3],
975 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
976 }
977 }
978 break;
979 case OPCODE_NOISE1:
980 {
981 GLfloat a[4], result[4];
982 fetch_vector1(&inst->SrcReg[0], machine, a);
983 result[0] =
984 result[1] =
985 result[2] = result[3] = _slang_library_noise1(a[0]);
986 store_vector4(inst, machine, result);
987 }
988 break;
989 case OPCODE_NOISE2:
990 {
991 GLfloat a[4], result[4];
992 fetch_vector4(&inst->SrcReg[0], machine, a);
993 result[0] =
994 result[1] =
995 result[2] = result[3] = _slang_library_noise2(a[0], a[1]);
996 store_vector4(inst, machine, result);
997 }
998 break;
999 case OPCODE_NOISE3:
1000 {
1001 GLfloat a[4], result[4];
1002 fetch_vector4(&inst->SrcReg[0], machine, a);
1003 result[0] =
1004 result[1] =
1005 result[2] =
1006 result[3] = _slang_library_noise3(a[0], a[1], a[2]);
1007 store_vector4(inst, machine, result);
1008 }
1009 break;
1010 case OPCODE_NOISE4:
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_noise4(a[0], a[1], a[2], a[3]);
1018 store_vector4(inst, machine, result);
1019 }
1020 break;
1021 case OPCODE_NOP:
1022 break;
1023 case OPCODE_PK2H: /* pack two 16-bit floats in one 32-bit float */
1024 {
1025 GLfloat a[4], result[4];
1026 GLhalfNV hx, hy;
1027 GLuint *rawResult = (GLuint *) result;
1028 GLuint twoHalves;
1029 fetch_vector4(&inst->SrcReg[0], machine, a);
1030 hx = _mesa_float_to_half(a[0]);
1031 hy = _mesa_float_to_half(a[1]);
1032 twoHalves = hx | (hy << 16);
1033 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
1034 = twoHalves;
1035 store_vector4(inst, machine, result);
1036 }
1037 break;
1038 case OPCODE_PK2US: /* pack two GLushorts into one 32-bit float */
1039 {
1040 GLfloat a[4], result[4];
1041 GLuint usx, usy, *rawResult = (GLuint *) result;
1042 fetch_vector4(&inst->SrcReg[0], machine, a);
1043 a[0] = CLAMP(a[0], 0.0F, 1.0F);
1044 a[1] = CLAMP(a[1], 0.0F, 1.0F);
1045 usx = IROUND(a[0] * 65535.0F);
1046 usy = IROUND(a[1] * 65535.0F);
1047 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
1048 = usx | (usy << 16);
1049 store_vector4(inst, machine, result);
1050 }
1051 break;
1052 case OPCODE_PK4B: /* pack four GLbytes into one 32-bit float */
1053 {
1054 GLfloat a[4], result[4];
1055 GLuint ubx, uby, ubz, ubw, *rawResult = (GLuint *) result;
1056 fetch_vector4(&inst->SrcReg[0], machine, a);
1057 a[0] = CLAMP(a[0], -128.0F / 127.0F, 1.0F);
1058 a[1] = CLAMP(a[1], -128.0F / 127.0F, 1.0F);
1059 a[2] = CLAMP(a[2], -128.0F / 127.0F, 1.0F);
1060 a[3] = CLAMP(a[3], -128.0F / 127.0F, 1.0F);
1061 ubx = IROUND(127.0F * a[0] + 128.0F);
1062 uby = IROUND(127.0F * a[1] + 128.0F);
1063 ubz = IROUND(127.0F * a[2] + 128.0F);
1064 ubw = IROUND(127.0F * a[3] + 128.0F);
1065 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
1066 = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
1067 store_vector4(inst, machine, result);
1068 }
1069 break;
1070 case OPCODE_PK4UB: /* pack four GLubytes into one 32-bit float */
1071 {
1072 GLfloat a[4], result[4];
1073 GLuint ubx, uby, ubz, ubw, *rawResult = (GLuint *) result;
1074 fetch_vector4(&inst->SrcReg[0], machine, a);
1075 a[0] = CLAMP(a[0], 0.0F, 1.0F);
1076 a[1] = CLAMP(a[1], 0.0F, 1.0F);
1077 a[2] = CLAMP(a[2], 0.0F, 1.0F);
1078 a[3] = CLAMP(a[3], 0.0F, 1.0F);
1079 ubx = IROUND(255.0F * a[0]);
1080 uby = IROUND(255.0F * a[1]);
1081 ubz = IROUND(255.0F * a[2]);
1082 ubw = IROUND(255.0F * a[3]);
1083 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
1084 = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
1085 store_vector4(inst, machine, result);
1086 }
1087 break;
1088 case OPCODE_POW:
1089 {
1090 GLfloat a[4], b[4], result[4];
1091 fetch_vector1(&inst->SrcReg[0], machine, a);
1092 fetch_vector1(&inst->SrcReg[1], machine, b);
1093 result[0] = result[1] = result[2] = result[3]
1094 = (GLfloat) _mesa_pow(a[0], b[0]);
1095 store_vector4(inst, machine, result);
1096 }
1097 break;
1098 case OPCODE_RCP:
1099 {
1100 GLfloat a[4], result[4];
1101 fetch_vector1(&inst->SrcReg[0], machine, a);
1102 if (DEBUG_PROG) {
1103 if (a[0] == 0)
1104 printf("RCP(0)\n");
1105 else if (IS_INF_OR_NAN(a[0]))
1106 printf("RCP(inf)\n");
1107 }
1108 result[0] = result[1] = result[2] = result[3] = 1.0F / a[0];
1109 store_vector4(inst, machine, result);
1110 }
1111 break;
1112 case OPCODE_RET: /* return from subroutine (conditional) */
1113 if (eval_condition(machine, inst)) {
1114 if (machine->StackDepth == 0) {
1115 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
1116 }
1117 /* subtract one because of pc++ in the for loop */
1118 pc = machine->CallStack[--machine->StackDepth] - 1;
1119 }
1120 break;
1121 case OPCODE_RFL: /* reflection vector */
1122 {
1123 GLfloat axis[4], dir[4], result[4], tmpX, tmpW;
1124 fetch_vector4(&inst->SrcReg[0], machine, axis);
1125 fetch_vector4(&inst->SrcReg[1], machine, dir);
1126 tmpW = DOT3(axis, axis);
1127 tmpX = (2.0F * DOT3(axis, dir)) / tmpW;
1128 result[0] = tmpX * axis[0] - dir[0];
1129 result[1] = tmpX * axis[1] - dir[1];
1130 result[2] = tmpX * axis[2] - dir[2];
1131 /* result[3] is never written! XXX enforce in parser! */
1132 store_vector4(inst, machine, result);
1133 }
1134 break;
1135 case OPCODE_RSQ: /* 1 / sqrt() */
1136 {
1137 GLfloat a[4], result[4];
1138 fetch_vector1(&inst->SrcReg[0], machine, a);
1139 a[0] = FABSF(a[0]);
1140 result[0] = result[1] = result[2] = result[3] = INV_SQRTF(a[0]);
1141 store_vector4(inst, machine, result);
1142 if (DEBUG_PROG) {
1143 printf("RSQ %g = 1/sqrt(|%g|)\n", result[0], a[0]);
1144 }
1145 }
1146 break;
1147 case OPCODE_SCS: /* sine and cos */
1148 {
1149 GLfloat a[4], result[4];
1150 fetch_vector1(&inst->SrcReg[0], machine, a);
1151 result[0] = (GLfloat) _mesa_cos(a[0]);
1152 result[1] = (GLfloat) _mesa_sin(a[0]);
1153 result[2] = 0.0; /* undefined! */
1154 result[3] = 0.0; /* undefined! */
1155 store_vector4(inst, machine, result);
1156 }
1157 break;
1158 case OPCODE_SEQ: /* set on equal */
1159 {
1160 GLfloat a[4], b[4], result[4];
1161 fetch_vector4(&inst->SrcReg[0], machine, a);
1162 fetch_vector4(&inst->SrcReg[1], machine, b);
1163 result[0] = (a[0] == b[0]) ? 1.0F : 0.0F;
1164 result[1] = (a[1] == b[1]) ? 1.0F : 0.0F;
1165 result[2] = (a[2] == b[2]) ? 1.0F : 0.0F;
1166 result[3] = (a[3] == b[3]) ? 1.0F : 0.0F;
1167 store_vector4(inst, machine, result);
1168 if (DEBUG_PROG) {
1169 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1170 result[0], result[1], result[2], result[3],
1171 a[0], a[1], a[2], a[3],
1172 b[0], b[1], b[2], b[3]);
1173 }
1174 }
1175 break;
1176 case OPCODE_SFL: /* set false, operands ignored */
1177 {
1178 static const GLfloat result[4] = { 0.0F, 0.0F, 0.0F, 0.0F };
1179 store_vector4(inst, machine, result);
1180 }
1181 break;
1182 case OPCODE_SGE: /* set on greater or equal */
1183 {
1184 GLfloat a[4], b[4], result[4];
1185 fetch_vector4(&inst->SrcReg[0], machine, a);
1186 fetch_vector4(&inst->SrcReg[1], machine, b);
1187 result[0] = (a[0] >= b[0]) ? 1.0F : 0.0F;
1188 result[1] = (a[1] >= b[1]) ? 1.0F : 0.0F;
1189 result[2] = (a[2] >= b[2]) ? 1.0F : 0.0F;
1190 result[3] = (a[3] >= b[3]) ? 1.0F : 0.0F;
1191 store_vector4(inst, machine, result);
1192 if (DEBUG_PROG) {
1193 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1194 result[0], result[1], result[2], result[3],
1195 a[0], a[1], a[2], a[3],
1196 b[0], b[1], b[2], b[3]);
1197 }
1198 }
1199 break;
1200 case OPCODE_SGT: /* set on greater */
1201 {
1202 GLfloat a[4], b[4], result[4];
1203 fetch_vector4(&inst->SrcReg[0], machine, a);
1204 fetch_vector4(&inst->SrcReg[1], machine, b);
1205 result[0] = (a[0] > b[0]) ? 1.0F : 0.0F;
1206 result[1] = (a[1] > b[1]) ? 1.0F : 0.0F;
1207 result[2] = (a[2] > b[2]) ? 1.0F : 0.0F;
1208 result[3] = (a[3] > b[3]) ? 1.0F : 0.0F;
1209 store_vector4(inst, machine, result);
1210 if (DEBUG_PROG) {
1211 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1212 result[0], result[1], result[2], result[3],
1213 a[0], a[1], a[2], a[3],
1214 b[0], b[1], b[2], b[3]);
1215 }
1216 }
1217 break;
1218 case OPCODE_SIN:
1219 {
1220 GLfloat a[4], result[4];
1221 fetch_vector1(&inst->SrcReg[0], machine, a);
1222 result[0] = result[1] = result[2] = result[3]
1223 = (GLfloat) _mesa_sin(a[0]);
1224 store_vector4(inst, machine, result);
1225 }
1226 break;
1227 case OPCODE_SLE: /* set on less or equal */
1228 {
1229 GLfloat a[4], b[4], result[4];
1230 fetch_vector4(&inst->SrcReg[0], machine, a);
1231 fetch_vector4(&inst->SrcReg[1], machine, b);
1232 result[0] = (a[0] <= b[0]) ? 1.0F : 0.0F;
1233 result[1] = (a[1] <= b[1]) ? 1.0F : 0.0F;
1234 result[2] = (a[2] <= b[2]) ? 1.0F : 0.0F;
1235 result[3] = (a[3] <= b[3]) ? 1.0F : 0.0F;
1236 store_vector4(inst, machine, result);
1237 if (DEBUG_PROG) {
1238 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1239 result[0], result[1], result[2], result[3],
1240 a[0], a[1], a[2], a[3],
1241 b[0], b[1], b[2], b[3]);
1242 }
1243 }
1244 break;
1245 case OPCODE_SLT: /* set on less */
1246 {
1247 GLfloat a[4], b[4], result[4];
1248 fetch_vector4(&inst->SrcReg[0], machine, a);
1249 fetch_vector4(&inst->SrcReg[1], machine, b);
1250 result[0] = (a[0] < b[0]) ? 1.0F : 0.0F;
1251 result[1] = (a[1] < b[1]) ? 1.0F : 0.0F;
1252 result[2] = (a[2] < b[2]) ? 1.0F : 0.0F;
1253 result[3] = (a[3] < b[3]) ? 1.0F : 0.0F;
1254 store_vector4(inst, machine, result);
1255 if (DEBUG_PROG) {
1256 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1257 result[0], result[1], result[2], result[3],
1258 a[0], a[1], a[2], a[3],
1259 b[0], b[1], b[2], b[3]);
1260 }
1261 }
1262 break;
1263 case OPCODE_SNE: /* set on not equal */
1264 {
1265 GLfloat a[4], b[4], result[4];
1266 fetch_vector4(&inst->SrcReg[0], machine, a);
1267 fetch_vector4(&inst->SrcReg[1], machine, b);
1268 result[0] = (a[0] != b[0]) ? 1.0F : 0.0F;
1269 result[1] = (a[1] != b[1]) ? 1.0F : 0.0F;
1270 result[2] = (a[2] != b[2]) ? 1.0F : 0.0F;
1271 result[3] = (a[3] != b[3]) ? 1.0F : 0.0F;
1272 store_vector4(inst, machine, result);
1273 if (DEBUG_PROG) {
1274 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1275 result[0], result[1], result[2], result[3],
1276 a[0], a[1], a[2], a[3],
1277 b[0], b[1], b[2], b[3]);
1278 }
1279 }
1280 break;
1281 case OPCODE_STR: /* set true, operands ignored */
1282 {
1283 static const GLfloat result[4] = { 1.0F, 1.0F, 1.0F, 1.0F };
1284 store_vector4(inst, machine, result);
1285 }
1286 break;
1287 case OPCODE_SUB:
1288 {
1289 GLfloat a[4], b[4], result[4];
1290 fetch_vector4(&inst->SrcReg[0], machine, a);
1291 fetch_vector4(&inst->SrcReg[1], machine, b);
1292 result[0] = a[0] - b[0];
1293 result[1] = a[1] - b[1];
1294 result[2] = a[2] - b[2];
1295 result[3] = a[3] - b[3];
1296 store_vector4(inst, machine, result);
1297 if (DEBUG_PROG) {
1298 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1299 result[0], result[1], result[2], result[3],
1300 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
1301 }
1302 }
1303 break;
1304 case OPCODE_SWZ: /* extended swizzle */
1305 {
1306 const struct prog_src_register *source = &inst->SrcReg[0];
1307 const GLfloat *src = get_register_pointer(source, machine);
1308 GLfloat result[4];
1309 GLuint i;
1310 for (i = 0; i < 4; i++) {
1311 const GLuint swz = GET_SWZ(source->Swizzle, i);
1312 if (swz == SWIZZLE_ZERO)
1313 result[i] = 0.0;
1314 else if (swz == SWIZZLE_ONE)
1315 result[i] = 1.0;
1316 else {
1317 ASSERT(swz >= 0);
1318 ASSERT(swz <= 3);
1319 result[i] = src[swz];
1320 }
1321 if (source->NegateBase & (1 << i))
1322 result[i] = -result[i];
1323 }
1324 store_vector4(inst, machine, result);
1325 }
1326 break;
1327 case OPCODE_TEX: /* Both ARB and NV frag prog */
1328 /* Simple texel lookup */
1329 {
1330 GLfloat texcoord[4], color[4];
1331 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1332
1333 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1334
1335 if (DEBUG_PROG) {
1336 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1337 color[0], color[1], color[2], color[3],
1338 inst->TexSrcUnit,
1339 texcoord[0], texcoord[1], texcoord[2], texcoord[3]);
1340 }
1341 store_vector4(inst, machine, color);
1342 }
1343 break;
1344 case OPCODE_TXB: /* GL_ARB_fragment_program only */
1345 /* Texel lookup with LOD bias */
1346 {
1347 const struct gl_texture_unit *texUnit
1348 = &ctx->Texture.Unit[inst->TexSrcUnit];
1349 GLfloat texcoord[4], color[4], lodBias;
1350
1351 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1352
1353 /* texcoord[3] is the bias to add to lambda */
1354 lodBias = texUnit->LodBias + texcoord[3];
1355 if (texUnit->_Current) {
1356 lodBias += texUnit->_Current->LodBias;
1357 }
1358
1359 fetch_texel(ctx, machine, inst, texcoord, lodBias, color);
1360
1361 store_vector4(inst, machine, color);
1362 }
1363 break;
1364 case OPCODE_TXD: /* GL_NV_fragment_program only */
1365 /* Texture lookup w/ partial derivatives for LOD */
1366 {
1367 GLfloat texcoord[4], dtdx[4], dtdy[4], color[4];
1368 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1369 fetch_vector4(&inst->SrcReg[1], machine, dtdx);
1370 fetch_vector4(&inst->SrcReg[2], machine, dtdy);
1371 machine->FetchTexelDeriv(ctx, texcoord, dtdx, dtdy,
1372 0.0, /* lodBias */
1373 inst->TexSrcUnit, color);
1374 store_vector4(inst, machine, color);
1375 }
1376 break;
1377 case OPCODE_TXP: /* GL_ARB_fragment_program only */
1378 /* Texture lookup w/ projective divide */
1379 {
1380 GLfloat texcoord[4], color[4];
1381
1382 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1383 /* Not so sure about this test - if texcoord[3] is
1384 * zero, we'd probably be fine except for an ASSERT in
1385 * IROUND_POS() which gets triggered by the inf values created.
1386 */
1387 if (texcoord[3] != 0.0) {
1388 texcoord[0] /= texcoord[3];
1389 texcoord[1] /= texcoord[3];
1390 texcoord[2] /= texcoord[3];
1391 }
1392
1393 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1394
1395 store_vector4(inst, machine, color);
1396 }
1397 break;
1398 case OPCODE_TXP_NV: /* GL_NV_fragment_program only */
1399 /* Texture lookup w/ projective divide, as above, but do not
1400 * do the divide by w if sampling from a cube map.
1401 */
1402 {
1403 GLfloat texcoord[4], color[4];
1404
1405 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1406 if (inst->TexSrcTarget != TEXTURE_CUBE_INDEX &&
1407 texcoord[3] != 0.0) {
1408 texcoord[0] /= texcoord[3];
1409 texcoord[1] /= texcoord[3];
1410 texcoord[2] /= texcoord[3];
1411 }
1412
1413 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1414
1415 store_vector4(inst, machine, color);
1416 }
1417 break;
1418 case OPCODE_TRUNC: /* truncate toward zero */
1419 {
1420 GLfloat a[4], result[4];
1421 fetch_vector4(&inst->SrcReg[0], machine, a);
1422 result[0] = (GLfloat) (GLint) a[0];
1423 result[1] = (GLfloat) (GLint) a[1];
1424 result[2] = (GLfloat) (GLint) a[2];
1425 result[3] = (GLfloat) (GLint) a[3];
1426 store_vector4(inst, machine, result);
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 }