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