2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
26 * \file prog_execute.c
27 * Software interpreter for vertex/fragment programs.
32 * NOTE: we do everything in single-precision floating point; we don't
33 * currently observe the single/half/fixed-precision qualifiers.
38 #include "main/glheader.h"
39 #include "main/colormac.h"
40 #include "main/context.h"
41 #include "prog_execute.h"
42 #include "prog_instruction.h"
43 #include "prog_parameter.h"
44 #include "prog_print.h"
45 #include "prog_noise.h"
53 * Set x to positive or negative infinity.
55 #if defined(USE_IEEE) || defined(_WIN32)
56 #define SET_POS_INFINITY(x) \
62 #define SET_NEG_INFINITY(x) \
69 #define SET_POS_INFINITY(x) x = __MAXFLOAT
70 #define SET_NEG_INFINITY(x) x = -__MAXFLOAT
72 #define SET_POS_INFINITY(x) x = (GLfloat) HUGE_VAL
73 #define SET_NEG_INFINITY(x) x = (GLfloat) -HUGE_VAL
76 #define SET_FLOAT_BITS(x, bits) ((fi_type *) (void *) &(x))->i = bits
79 static const GLfloat ZeroVec
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
84 * Return a pointer to the 4-element float vector specified by the given
87 static INLINE
const GLfloat
*
88 get_src_register_pointer(const struct prog_src_register
*source
,
89 const struct gl_program_machine
*machine
)
91 const struct gl_program
*prog
= machine
->CurProgram
;
92 GLint reg
= source
->Index
;
94 if (source
->RelAddr
) {
95 /* add address register value to src index/offset */
96 reg
+= machine
->AddressReg
[0][0];
102 switch (source
->File
) {
103 case PROGRAM_TEMPORARY
:
104 if (reg
>= MAX_PROGRAM_TEMPS
)
106 return machine
->Temporaries
[reg
];
109 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
110 if (reg
>= VERT_ATTRIB_MAX
)
112 return machine
->VertAttribs
[reg
];
115 if (reg
>= FRAG_ATTRIB_MAX
)
117 return machine
->Attribs
[reg
][machine
->CurElement
];
121 if (reg
>= MAX_PROGRAM_OUTPUTS
)
123 return machine
->Outputs
[reg
];
125 case PROGRAM_LOCAL_PARAM
:
126 if (reg
>= MAX_PROGRAM_LOCAL_PARAMS
)
128 return machine
->CurProgram
->LocalParams
[reg
];
130 case PROGRAM_ENV_PARAM
:
131 if (reg
>= MAX_PROGRAM_ENV_PARAMS
)
133 return machine
->EnvParams
[reg
];
135 case PROGRAM_STATE_VAR
:
137 case PROGRAM_CONSTANT
:
139 case PROGRAM_UNIFORM
:
141 case PROGRAM_NAMED_PARAM
:
142 if (reg
>= (GLint
) prog
->Parameters
->NumParameters
)
144 return prog
->Parameters
->ParameterValues
[reg
];
148 "Invalid src register file %d in get_src_register_pointer()",
156 * Return a pointer to the 4-element float vector specified by the given
157 * destination register.
159 static INLINE GLfloat
*
160 get_dst_register_pointer(const struct prog_dst_register
*dest
,
161 struct gl_program_machine
*machine
)
163 static GLfloat dummyReg
[4];
164 GLint reg
= dest
->Index
;
167 /* add address register value to src index/offset */
168 reg
+= machine
->AddressReg
[0][0];
174 switch (dest
->File
) {
175 case PROGRAM_TEMPORARY
:
176 if (reg
>= MAX_PROGRAM_TEMPS
)
178 return machine
->Temporaries
[reg
];
181 if (reg
>= MAX_PROGRAM_OUTPUTS
)
183 return machine
->Outputs
[reg
];
185 case PROGRAM_WRITE_ONLY
:
190 "Invalid dest register file %d in get_dst_register_pointer()",
199 * Fetch a 4-element float vector from the given source register.
200 * Apply swizzling and negating as needed.
203 fetch_vector4(const struct prog_src_register
*source
,
204 const struct gl_program_machine
*machine
, GLfloat result
[4])
206 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
209 if (source
->Swizzle
== SWIZZLE_NOOP
) {
211 COPY_4V(result
, src
);
214 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
215 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
216 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
217 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
218 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
219 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
220 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
221 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
225 result
[0] = FABSF(result
[0]);
226 result
[1] = FABSF(result
[1]);
227 result
[2] = FABSF(result
[2]);
228 result
[3] = FABSF(result
[3]);
230 if (source
->Negate
) {
231 ASSERT(source
->Negate
== NEGATE_XYZW
);
232 result
[0] = -result
[0];
233 result
[1] = -result
[1];
234 result
[2] = -result
[2];
235 result
[3] = -result
[3];
239 assert(!IS_INF_OR_NAN(result
[0]));
240 assert(!IS_INF_OR_NAN(result
[0]));
241 assert(!IS_INF_OR_NAN(result
[0]));
242 assert(!IS_INF_OR_NAN(result
[0]));
248 * Fetch a 4-element uint vector from the given source register.
249 * Apply swizzling but not negation/abs.
252 fetch_vector4ui(const struct prog_src_register
*source
,
253 const struct gl_program_machine
*machine
, GLuint result
[4])
255 const GLuint
*src
= (GLuint
*) get_src_register_pointer(source
, machine
);
258 if (source
->Swizzle
== SWIZZLE_NOOP
) {
260 COPY_4V(result
, src
);
263 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
264 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
265 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
266 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
267 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
268 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
269 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
270 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
273 /* Note: no Negate or Abs here */
279 * Fetch the derivative with respect to X or Y for the given register.
280 * XXX this currently only works for fragment program input attribs.
283 fetch_vector4_deriv(GLcontext
* ctx
,
284 const struct prog_src_register
*source
,
285 const struct gl_program_machine
*machine
,
286 char xOrY
, GLfloat result
[4])
288 if (source
->File
== PROGRAM_INPUT
&&
289 source
->Index
< (GLint
) machine
->NumDeriv
) {
290 const GLint col
= machine
->CurElement
;
291 const GLfloat w
= machine
->Attribs
[FRAG_ATTRIB_WPOS
][col
][3];
292 const GLfloat invQ
= 1.0f
/ w
;
296 deriv
[0] = machine
->DerivX
[source
->Index
][0] * invQ
;
297 deriv
[1] = machine
->DerivX
[source
->Index
][1] * invQ
;
298 deriv
[2] = machine
->DerivX
[source
->Index
][2] * invQ
;
299 deriv
[3] = machine
->DerivX
[source
->Index
][3] * invQ
;
302 deriv
[0] = machine
->DerivY
[source
->Index
][0] * invQ
;
303 deriv
[1] = machine
->DerivY
[source
->Index
][1] * invQ
;
304 deriv
[2] = machine
->DerivY
[source
->Index
][2] * invQ
;
305 deriv
[3] = machine
->DerivY
[source
->Index
][3] * invQ
;
308 result
[0] = deriv
[GET_SWZ(source
->Swizzle
, 0)];
309 result
[1] = deriv
[GET_SWZ(source
->Swizzle
, 1)];
310 result
[2] = deriv
[GET_SWZ(source
->Swizzle
, 2)];
311 result
[3] = deriv
[GET_SWZ(source
->Swizzle
, 3)];
314 result
[0] = FABSF(result
[0]);
315 result
[1] = FABSF(result
[1]);
316 result
[2] = FABSF(result
[2]);
317 result
[3] = FABSF(result
[3]);
319 if (source
->Negate
) {
320 ASSERT(source
->Negate
== NEGATE_XYZW
);
321 result
[0] = -result
[0];
322 result
[1] = -result
[1];
323 result
[2] = -result
[2];
324 result
[3] = -result
[3];
328 ASSIGN_4V(result
, 0.0, 0.0, 0.0, 0.0);
334 * As above, but only return result[0] element.
337 fetch_vector1(const struct prog_src_register
*source
,
338 const struct gl_program_machine
*machine
, GLfloat result
[4])
340 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
343 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
346 result
[0] = FABSF(result
[0]);
348 if (source
->Negate
) {
349 result
[0] = -result
[0];
355 fetch_vector1ui(const struct prog_src_register
*source
,
356 const struct gl_program_machine
*machine
)
358 const GLuint
*src
= (GLuint
*) get_src_register_pointer(source
, machine
);
359 return src
[GET_SWZ(source
->Swizzle
, 0)];
364 * Fetch texel from texture. Use partial derivatives when possible.
367 fetch_texel(GLcontext
*ctx
,
368 const struct gl_program_machine
*machine
,
369 const struct prog_instruction
*inst
,
370 const GLfloat texcoord
[4], GLfloat lodBias
,
373 const GLuint unit
= machine
->Samplers
[inst
->TexSrcUnit
];
375 /* Note: we only have the right derivatives for fragment input attribs.
377 if (machine
->NumDeriv
> 0 &&
378 inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
379 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
) {
380 /* simple texture fetch for which we should have derivatives */
381 GLuint attr
= inst
->SrcReg
[0].Index
;
382 machine
->FetchTexelDeriv(ctx
, texcoord
,
383 machine
->DerivX
[attr
],
384 machine
->DerivY
[attr
],
385 lodBias
, unit
, color
);
388 machine
->FetchTexelLod(ctx
, texcoord
, lodBias
, unit
, color
);
394 * Test value against zero and return GT, LT, EQ or UN if NaN.
397 generate_cc(float value
)
400 return COND_UN
; /* NaN */
410 * Test if the ccMaskRule is satisfied by the given condition code.
411 * Used to mask destination writes according to the current condition code.
413 static INLINE GLboolean
414 test_cc(GLuint condCode
, GLuint ccMaskRule
)
416 switch (ccMaskRule
) {
417 case COND_EQ
: return (condCode
== COND_EQ
);
418 case COND_NE
: return (condCode
!= COND_EQ
);
419 case COND_LT
: return (condCode
== COND_LT
);
420 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
421 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
422 case COND_GT
: return (condCode
== COND_GT
);
423 case COND_TR
: return GL_TRUE
;
424 case COND_FL
: return GL_FALSE
;
425 default: return GL_TRUE
;
431 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
432 * or GL_FALSE to indicate result.
434 static INLINE GLboolean
435 eval_condition(const struct gl_program_machine
*machine
,
436 const struct prog_instruction
*inst
)
438 const GLuint swizzle
= inst
->DstReg
.CondSwizzle
;
439 const GLuint condMask
= inst
->DstReg
.CondMask
;
440 if (test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 0)], condMask
) ||
441 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 1)], condMask
) ||
442 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 2)], condMask
) ||
443 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 3)], condMask
)) {
454 * Store 4 floats into a register. Observe the instructions saturate and
455 * set-condition-code flags.
458 store_vector4(const struct prog_instruction
*inst
,
459 struct gl_program_machine
*machine
, const GLfloat value
[4])
461 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
462 const GLboolean clamp
= inst
->SaturateMode
== SATURATE_ZERO_ONE
;
463 GLuint writeMask
= dstReg
->WriteMask
;
464 GLfloat clampedValue
[4];
465 GLfloat
*dst
= get_dst_register_pointer(dstReg
, machine
);
468 if (value
[0] > 1.0e10
||
469 IS_INF_OR_NAN(value
[0]) ||
470 IS_INF_OR_NAN(value
[1]) ||
471 IS_INF_OR_NAN(value
[2]) || IS_INF_OR_NAN(value
[3]))
472 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
476 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
477 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
478 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
479 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
480 value
= clampedValue
;
483 if (dstReg
->CondMask
!= COND_TR
) {
484 /* condition codes may turn off some writes */
485 if (writeMask
& WRITEMASK_X
) {
486 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
488 writeMask
&= ~WRITEMASK_X
;
490 if (writeMask
& WRITEMASK_Y
) {
491 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
493 writeMask
&= ~WRITEMASK_Y
;
495 if (writeMask
& WRITEMASK_Z
) {
496 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
498 writeMask
&= ~WRITEMASK_Z
;
500 if (writeMask
& WRITEMASK_W
) {
501 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
503 writeMask
&= ~WRITEMASK_W
;
508 assert(!IS_INF_OR_NAN(value
[0]));
509 assert(!IS_INF_OR_NAN(value
[0]));
510 assert(!IS_INF_OR_NAN(value
[0]));
511 assert(!IS_INF_OR_NAN(value
[0]));
514 if (writeMask
& WRITEMASK_X
)
516 if (writeMask
& WRITEMASK_Y
)
518 if (writeMask
& WRITEMASK_Z
)
520 if (writeMask
& WRITEMASK_W
)
523 if (inst
->CondUpdate
) {
524 if (writeMask
& WRITEMASK_X
)
525 machine
->CondCodes
[0] = generate_cc(value
[0]);
526 if (writeMask
& WRITEMASK_Y
)
527 machine
->CondCodes
[1] = generate_cc(value
[1]);
528 if (writeMask
& WRITEMASK_Z
)
529 machine
->CondCodes
[2] = generate_cc(value
[2]);
530 if (writeMask
& WRITEMASK_W
)
531 machine
->CondCodes
[3] = generate_cc(value
[3]);
533 printf("CondCodes=(%s,%s,%s,%s) for:\n",
534 _mesa_condcode_string(machine
->CondCodes
[0]),
535 _mesa_condcode_string(machine
->CondCodes
[1]),
536 _mesa_condcode_string(machine
->CondCodes
[2]),
537 _mesa_condcode_string(machine
->CondCodes
[3]));
544 * Store 4 uints into a register. Observe the set-condition-code flags.
547 store_vector4ui(const struct prog_instruction
*inst
,
548 struct gl_program_machine
*machine
, const GLuint value
[4])
550 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
551 GLuint writeMask
= dstReg
->WriteMask
;
552 GLuint
*dst
= (GLuint
*) get_dst_register_pointer(dstReg
, machine
);
554 if (dstReg
->CondMask
!= COND_TR
) {
555 /* condition codes may turn off some writes */
556 if (writeMask
& WRITEMASK_X
) {
557 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
559 writeMask
&= ~WRITEMASK_X
;
561 if (writeMask
& WRITEMASK_Y
) {
562 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
564 writeMask
&= ~WRITEMASK_Y
;
566 if (writeMask
& WRITEMASK_Z
) {
567 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
569 writeMask
&= ~WRITEMASK_Z
;
571 if (writeMask
& WRITEMASK_W
) {
572 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
574 writeMask
&= ~WRITEMASK_W
;
578 if (writeMask
& WRITEMASK_X
)
580 if (writeMask
& WRITEMASK_Y
)
582 if (writeMask
& WRITEMASK_Z
)
584 if (writeMask
& WRITEMASK_W
)
587 if (inst
->CondUpdate
) {
588 if (writeMask
& WRITEMASK_X
)
589 machine
->CondCodes
[0] = generate_cc((float)value
[0]);
590 if (writeMask
& WRITEMASK_Y
)
591 machine
->CondCodes
[1] = generate_cc((float)value
[1]);
592 if (writeMask
& WRITEMASK_Z
)
593 machine
->CondCodes
[2] = generate_cc((float)value
[2]);
594 if (writeMask
& WRITEMASK_W
)
595 machine
->CondCodes
[3] = generate_cc((float)value
[3]);
597 printf("CondCodes=(%s,%s,%s,%s) for:\n",
598 _mesa_condcode_string(machine
->CondCodes
[0]),
599 _mesa_condcode_string(machine
->CondCodes
[1]),
600 _mesa_condcode_string(machine
->CondCodes
[2]),
601 _mesa_condcode_string(machine
->CondCodes
[3]));
609 * Execute the given vertex/fragment program.
611 * \param ctx rendering context
612 * \param program the program to execute
613 * \param machine machine state (must be initialized)
614 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
617 _mesa_execute_program(GLcontext
* ctx
,
618 const struct gl_program
*program
,
619 struct gl_program_machine
*machine
)
621 const GLuint numInst
= program
->NumInstructions
;
622 const GLuint maxExec
= 10000;
623 GLuint pc
, numExec
= 0;
625 machine
->CurProgram
= program
;
628 printf("execute program %u --------------------\n", program
->Id
);
631 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
632 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
635 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
638 for (pc
= 0; pc
< numInst
; pc
++) {
639 const struct prog_instruction
*inst
= program
->Instructions
+ pc
;
642 _mesa_print_instruction(inst
);
645 switch (inst
->Opcode
) {
648 GLfloat a
[4], result
[4];
649 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
650 result
[0] = FABSF(a
[0]);
651 result
[1] = FABSF(a
[1]);
652 result
[2] = FABSF(a
[2]);
653 result
[3] = FABSF(a
[3]);
654 store_vector4(inst
, machine
, result
);
659 GLfloat a
[4], b
[4], result
[4];
660 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
661 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
662 result
[0] = a
[0] + b
[0];
663 result
[1] = a
[1] + b
[1];
664 result
[2] = a
[2] + b
[2];
665 result
[3] = a
[3] + b
[3];
666 store_vector4(inst
, machine
, result
);
668 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
669 result
[0], result
[1], result
[2], result
[3],
670 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
674 case OPCODE_AND
: /* bitwise AND */
676 GLuint a
[4], b
[4], result
[4];
677 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
678 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
679 result
[0] = a
[0] & b
[0];
680 result
[1] = a
[1] & b
[1];
681 result
[2] = a
[2] & b
[2];
682 result
[3] = a
[3] & b
[3];
683 store_vector4ui(inst
, machine
, result
);
689 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
690 machine
->AddressReg
[0][0] = IFLOOR(t
[0]);
692 printf("ARL %d\n", machine
->AddressReg
[0][0]);
698 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
702 /* subtract 1 here since pc is incremented by for(pc) loop */
703 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
705 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
707 case OPCODE_BGNSUB
: /* begin subroutine */
709 case OPCODE_ENDSUB
: /* end subroutine */
711 case OPCODE_BRA
: /* branch (conditional) */
712 if (eval_condition(machine
, inst
)) {
714 /* Subtract 1 here since we'll do pc++ below */
715 pc
= inst
->BranchTarget
- 1;
718 case OPCODE_BRK
: /* break out of loop (conditional) */
719 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
721 if (eval_condition(machine
, inst
)) {
722 /* break out of loop */
723 /* pc++ at end of for-loop will put us after the ENDLOOP inst */
724 pc
= inst
->BranchTarget
;
727 case OPCODE_CONT
: /* continue loop (conditional) */
728 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
730 if (eval_condition(machine
, inst
)) {
731 /* continue at ENDLOOP */
732 /* Subtract 1 here since we'll do pc++ at end of for-loop */
733 pc
= inst
->BranchTarget
- 1;
736 case OPCODE_CAL
: /* Call subroutine (conditional) */
737 if (eval_condition(machine
, inst
)) {
738 /* call the subroutine */
739 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
740 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
742 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1; /* next inst */
743 /* Subtract 1 here since we'll do pc++ at end of for-loop */
744 pc
= inst
->BranchTarget
- 1;
749 GLfloat a
[4], b
[4], c
[4], result
[4];
750 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
751 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
752 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
753 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
754 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
755 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
756 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
757 store_vector4(inst
, machine
, result
);
762 GLfloat a
[4], result
[4];
763 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
764 result
[0] = result
[1] = result
[2] = result
[3]
765 = (GLfloat
) _mesa_cos(a
[0]);
766 store_vector4(inst
, machine
, result
);
769 case OPCODE_DDX
: /* Partial derivative with respect to X */
772 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
774 store_vector4(inst
, machine
, result
);
777 case OPCODE_DDY
: /* Partial derivative with respect to Y */
780 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
782 store_vector4(inst
, machine
, result
);
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] = DOT2(a
, b
);
791 store_vector4(inst
, machine
, result
);
793 printf("DP2 %g = (%g %g) . (%g %g)\n",
794 result
[0], a
[0], a
[1], b
[0], b
[1]);
800 GLfloat a
[4], b
[4], c
, result
[4];
801 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
802 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
803 fetch_vector1(&inst
->SrcReg
[1], machine
, &c
);
804 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
) + c
;
805 store_vector4(inst
, machine
, result
);
807 printf("DP2A %g = (%g %g) . (%g %g) + %g\n",
808 result
[0], a
[0], a
[1], b
[0], b
[1], c
);
814 GLfloat a
[4], b
[4], result
[4];
815 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
816 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
817 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
818 store_vector4(inst
, machine
, result
);
820 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
821 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
827 GLfloat a
[4], b
[4], result
[4];
828 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
829 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
830 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
, b
);
831 store_vector4(inst
, machine
, result
);
833 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
834 result
[0], a
[0], a
[1], a
[2], a
[3],
835 b
[0], b
[1], b
[2], b
[3]);
841 GLfloat a
[4], b
[4], result
[4];
842 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
843 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
844 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
) + b
[3];
845 store_vector4(inst
, machine
, result
);
848 case OPCODE_DST
: /* Distance vector */
850 GLfloat a
[4], b
[4], result
[4];
851 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
852 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
854 result
[1] = a
[1] * b
[1];
857 store_vector4(inst
, machine
, result
);
862 GLfloat t
[4], q
[4], floor_t0
;
863 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
864 floor_t0
= FLOORF(t
[0]);
865 if (floor_t0
> FLT_MAX_EXP
) {
866 SET_POS_INFINITY(q
[0]);
867 SET_POS_INFINITY(q
[2]);
869 else if (floor_t0
< FLT_MIN_EXP
) {
874 q
[0] = LDEXPF(1.0, (int) floor_t0
);
875 /* Note: GL_NV_vertex_program expects
876 * result.z = result.x * APPX(result.y)
877 * We do what the ARB extension says.
879 q
[2] = (GLfloat
) _mesa_pow(2.0, t
[0]);
881 q
[1] = t
[0] - floor_t0
;
883 store_vector4( inst
, machine
, q
);
886 case OPCODE_EX2
: /* Exponential base 2 */
888 GLfloat a
[4], result
[4], val
;
889 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
890 val
= (GLfloat
) _mesa_pow(2.0, a
[0]);
892 if (IS_INF_OR_NAN(val))
895 result
[0] = result
[1] = result
[2] = result
[3] = val
;
896 store_vector4(inst
, machine
, result
);
901 GLfloat a
[4], result
[4];
902 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
903 result
[0] = FLOORF(a
[0]);
904 result
[1] = FLOORF(a
[1]);
905 result
[2] = FLOORF(a
[2]);
906 result
[3] = FLOORF(a
[3]);
907 store_vector4(inst
, machine
, result
);
912 GLfloat a
[4], result
[4];
913 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
914 result
[0] = a
[0] - FLOORF(a
[0]);
915 result
[1] = a
[1] - FLOORF(a
[1]);
916 result
[2] = a
[2] - FLOORF(a
[2]);
917 result
[3] = a
[3] - FLOORF(a
[3]);
918 store_vector4(inst
, machine
, result
);
924 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
926 program
->Instructions
[inst
->BranchTarget
].Opcode
929 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
931 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
932 cond
= (a
[0] != 0.0);
935 cond
= eval_condition(machine
, inst
);
938 printf("IF: %d\n", cond
);
942 /* do if-clause (just continue execution) */
945 /* go to the instruction after ELSE or ENDIF */
946 assert(inst
->BranchTarget
>= 0);
947 pc
= inst
->BranchTarget
;
953 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
955 assert(inst
->BranchTarget
>= 0);
956 pc
= inst
->BranchTarget
;
961 case OPCODE_KIL_NV
: /* NV_f_p only (conditional) */
962 if (eval_condition(machine
, inst
)) {
966 case OPCODE_KIL
: /* ARB_f_p only */
969 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
971 printf("KIL if (%g %g %g %g) <= 0.0\n",
972 a
[0], a
[1], a
[2], a
[3]);
975 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
980 case OPCODE_LG2
: /* log base 2 */
982 GLfloat a
[4], result
[4], val
;
983 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
984 /* The fast LOG2 macro doesn't meet the precision requirements.
990 val
= (float)(log(a
[0]) * 1.442695F
);
992 result
[0] = result
[1] = result
[2] = result
[3] = val
;
993 store_vector4(inst
, machine
, result
);
998 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
999 GLfloat a
[4], result
[4];
1000 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1001 a
[0] = MAX2(a
[0], 0.0F
);
1002 a
[1] = MAX2(a
[1], 0.0F
);
1003 /* XXX ARB version clamps a[3], NV version doesn't */
1004 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
1007 /* XXX we could probably just use pow() here */
1009 if (a
[1] == 0.0 && a
[3] == 0.0)
1012 result
[2] = (GLfloat
) _mesa_pow(a
[1], a
[3]);
1018 store_vector4(inst
, machine
, result
);
1020 printf("LIT (%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]);
1028 GLfloat t
[4], q
[4], abs_t0
;
1029 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
1030 abs_t0
= FABSF(t
[0]);
1031 if (abs_t0
!= 0.0F
) {
1032 /* Since we really can't handle infinite values on VMS
1033 * like other OSes we'll use __MAXFLOAT to represent
1034 * infinity. This may need some tweaking.
1037 if (abs_t0
== __MAXFLOAT
)
1039 if (IS_INF_OR_NAN(abs_t0
))
1042 SET_POS_INFINITY(q
[0]);
1044 SET_POS_INFINITY(q
[2]);
1048 GLfloat mantissa
= FREXPF(t
[0], &exponent
);
1049 q
[0] = (GLfloat
) (exponent
- 1);
1050 q
[1] = (GLfloat
) (2.0 * mantissa
); /* map [.5, 1) -> [1, 2) */
1052 /* The fast LOG2 macro doesn't meet the precision
1055 q
[2] = (float)(log(t
[0]) * 1.442695F
);
1059 SET_NEG_INFINITY(q
[0]);
1061 SET_NEG_INFINITY(q
[2]);
1064 store_vector4(inst
, machine
, q
);
1069 GLfloat a
[4], b
[4], c
[4], result
[4];
1070 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1071 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1072 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1073 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
1074 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
1075 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
1076 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
1077 store_vector4(inst
, machine
, result
);
1079 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
1080 "(%g %g %g %g), (%g %g %g %g)\n",
1081 result
[0], result
[1], result
[2], result
[3],
1082 a
[0], a
[1], a
[2], a
[3],
1083 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1089 GLfloat a
[4], b
[4], c
[4], result
[4];
1090 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1091 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1092 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1093 result
[0] = a
[0] * b
[0] + c
[0];
1094 result
[1] = a
[1] * b
[1] + c
[1];
1095 result
[2] = a
[2] * b
[2] + c
[2];
1096 result
[3] = a
[3] * b
[3] + c
[3];
1097 store_vector4(inst
, machine
, result
);
1099 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
1100 "(%g %g %g %g) + (%g %g %g %g)\n",
1101 result
[0], result
[1], result
[2], result
[3],
1102 a
[0], a
[1], a
[2], a
[3],
1103 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1109 GLfloat a
[4], b
[4], result
[4];
1110 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1111 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1112 result
[0] = MAX2(a
[0], b
[0]);
1113 result
[1] = MAX2(a
[1], b
[1]);
1114 result
[2] = MAX2(a
[2], b
[2]);
1115 result
[3] = MAX2(a
[3], b
[3]);
1116 store_vector4(inst
, machine
, result
);
1118 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
1119 result
[0], result
[1], result
[2], result
[3],
1120 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1126 GLfloat a
[4], b
[4], result
[4];
1127 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1128 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1129 result
[0] = MIN2(a
[0], b
[0]);
1130 result
[1] = MIN2(a
[1], b
[1]);
1131 result
[2] = MIN2(a
[2], b
[2]);
1132 result
[3] = MIN2(a
[3], b
[3]);
1133 store_vector4(inst
, machine
, result
);
1139 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
1140 store_vector4(inst
, machine
, result
);
1142 printf("MOV (%g %g %g %g)\n",
1143 result
[0], result
[1], result
[2], result
[3]);
1149 GLfloat a
[4], b
[4], result
[4];
1150 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1151 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1152 result
[0] = a
[0] * b
[0];
1153 result
[1] = a
[1] * b
[1];
1154 result
[2] = a
[2] * b
[2];
1155 result
[3] = a
[3] * b
[3];
1156 store_vector4(inst
, machine
, result
);
1158 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1159 result
[0], result
[1], result
[2], result
[3],
1160 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1166 GLfloat a
[4], result
[4];
1167 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1171 result
[3] = _mesa_noise1(a
[0]);
1172 store_vector4(inst
, machine
, result
);
1177 GLfloat a
[4], result
[4];
1178 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1181 result
[2] = result
[3] = _mesa_noise2(a
[0], a
[1]);
1182 store_vector4(inst
, machine
, result
);
1187 GLfloat a
[4], result
[4];
1188 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1192 result
[3] = _mesa_noise3(a
[0], a
[1], a
[2]);
1193 store_vector4(inst
, machine
, result
);
1198 GLfloat a
[4], result
[4];
1199 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1203 result
[3] = _mesa_noise4(a
[0], a
[1], a
[2], a
[3]);
1204 store_vector4(inst
, machine
, result
);
1209 case OPCODE_NOT
: /* bitwise NOT */
1211 GLuint a
[4], result
[4];
1212 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1217 store_vector4ui(inst
, machine
, result
);
1220 case OPCODE_NRM3
: /* 3-component normalization */
1222 GLfloat a
[4], result
[4];
1224 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1225 tmp
= a
[0] * a
[0] + a
[1] * a
[1] + a
[2] * a
[2];
1227 tmp
= INV_SQRTF(tmp
);
1228 result
[0] = tmp
* a
[0];
1229 result
[1] = tmp
* a
[1];
1230 result
[2] = tmp
* a
[2];
1231 result
[3] = 0.0; /* undefined, but prevent valgrind warnings */
1232 store_vector4(inst
, machine
, result
);
1235 case OPCODE_NRM4
: /* 4-component normalization */
1237 GLfloat a
[4], result
[4];
1239 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1240 tmp
= a
[0] * a
[0] + a
[1] * a
[1] + a
[2] * a
[2] + a
[3] * a
[3];
1242 tmp
= INV_SQRTF(tmp
);
1243 result
[0] = tmp
* a
[0];
1244 result
[1] = tmp
* a
[1];
1245 result
[2] = tmp
* a
[2];
1246 result
[3] = tmp
* a
[3];
1247 store_vector4(inst
, machine
, result
);
1250 case OPCODE_OR
: /* bitwise OR */
1252 GLuint a
[4], b
[4], result
[4];
1253 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1254 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
1255 result
[0] = a
[0] | b
[0];
1256 result
[1] = a
[1] | b
[1];
1257 result
[2] = a
[2] | b
[2];
1258 result
[3] = a
[3] | b
[3];
1259 store_vector4ui(inst
, machine
, result
);
1262 case OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
1267 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1268 hx
= _mesa_float_to_half(a
[0]);
1269 hy
= _mesa_float_to_half(a
[1]);
1273 result
[3] = hx
| (hy
<< 16);
1274 store_vector4ui(inst
, machine
, result
);
1277 case OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
1280 GLuint result
[4], usx
, usy
;
1281 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1282 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1283 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1284 usx
= IROUND(a
[0] * 65535.0F
);
1285 usy
= IROUND(a
[1] * 65535.0F
);
1289 result
[3] = usx
| (usy
<< 16);
1290 store_vector4ui(inst
, machine
, result
);
1293 case OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
1296 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1297 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1298 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
1299 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
1300 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
1301 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
1302 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
1303 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
1304 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
1305 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
1309 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1310 store_vector4ui(inst
, machine
, result
);
1313 case OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
1316 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1317 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1318 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1319 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1320 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
1321 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
1322 ubx
= IROUND(255.0F
* a
[0]);
1323 uby
= IROUND(255.0F
* a
[1]);
1324 ubz
= IROUND(255.0F
* a
[2]);
1325 ubw
= IROUND(255.0F
* a
[3]);
1329 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1330 store_vector4ui(inst
, machine
, result
);
1335 GLfloat a
[4], b
[4], result
[4];
1336 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1337 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
1338 result
[0] = result
[1] = result
[2] = result
[3]
1339 = (GLfloat
) _mesa_pow(a
[0], b
[0]);
1340 store_vector4(inst
, machine
, result
);
1345 GLfloat a
[4], result
[4];
1346 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1350 else if (IS_INF_OR_NAN(a
[0]))
1351 printf("RCP(inf)\n");
1353 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
1354 store_vector4(inst
, machine
, result
);
1357 case OPCODE_RET
: /* return from subroutine (conditional) */
1358 if (eval_condition(machine
, inst
)) {
1359 if (machine
->StackDepth
== 0) {
1360 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
1362 /* subtract one because of pc++ in the for loop */
1363 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
1366 case OPCODE_RFL
: /* reflection vector */
1368 GLfloat axis
[4], dir
[4], result
[4], tmpX
, tmpW
;
1369 fetch_vector4(&inst
->SrcReg
[0], machine
, axis
);
1370 fetch_vector4(&inst
->SrcReg
[1], machine
, dir
);
1371 tmpW
= DOT3(axis
, axis
);
1372 tmpX
= (2.0F
* DOT3(axis
, dir
)) / tmpW
;
1373 result
[0] = tmpX
* axis
[0] - dir
[0];
1374 result
[1] = tmpX
* axis
[1] - dir
[1];
1375 result
[2] = tmpX
* axis
[2] - dir
[2];
1376 /* result[3] is never written! XXX enforce in parser! */
1377 store_vector4(inst
, machine
, result
);
1380 case OPCODE_RSQ
: /* 1 / sqrt() */
1382 GLfloat a
[4], result
[4];
1383 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1385 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1386 store_vector4(inst
, machine
, result
);
1388 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
1392 case OPCODE_SCS
: /* sine and cos */
1394 GLfloat a
[4], result
[4];
1395 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1396 result
[0] = (GLfloat
) _mesa_cos(a
[0]);
1397 result
[1] = (GLfloat
) _mesa_sin(a
[0]);
1398 result
[2] = 0.0; /* undefined! */
1399 result
[3] = 0.0; /* undefined! */
1400 store_vector4(inst
, machine
, result
);
1403 case OPCODE_SEQ
: /* set on equal */
1405 GLfloat a
[4], b
[4], result
[4];
1406 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1407 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1408 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1409 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1410 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1411 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1412 store_vector4(inst
, machine
, result
);
1414 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1415 result
[0], result
[1], result
[2], result
[3],
1416 a
[0], a
[1], a
[2], a
[3],
1417 b
[0], b
[1], b
[2], b
[3]);
1421 case OPCODE_SFL
: /* set false, operands ignored */
1423 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1424 store_vector4(inst
, machine
, result
);
1427 case OPCODE_SGE
: /* set on greater or equal */
1429 GLfloat a
[4], b
[4], result
[4];
1430 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1431 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1432 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1433 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1434 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1435 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1436 store_vector4(inst
, machine
, result
);
1438 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1439 result
[0], result
[1], result
[2], result
[3],
1440 a
[0], a
[1], a
[2], a
[3],
1441 b
[0], b
[1], b
[2], b
[3]);
1445 case OPCODE_SGT
: /* set on greater */
1447 GLfloat a
[4], b
[4], result
[4];
1448 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1449 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1450 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1451 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1452 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1453 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1454 store_vector4(inst
, machine
, result
);
1456 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1457 result
[0], result
[1], result
[2], result
[3],
1458 a
[0], a
[1], a
[2], a
[3],
1459 b
[0], b
[1], b
[2], b
[3]);
1465 GLfloat a
[4], result
[4];
1466 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1467 result
[0] = result
[1] = result
[2] = result
[3]
1468 = (GLfloat
) _mesa_sin(a
[0]);
1469 store_vector4(inst
, machine
, result
);
1472 case OPCODE_SLE
: /* set on less or equal */
1474 GLfloat a
[4], b
[4], result
[4];
1475 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1476 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1477 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1478 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1479 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1480 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1481 store_vector4(inst
, machine
, result
);
1483 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1484 result
[0], result
[1], result
[2], result
[3],
1485 a
[0], a
[1], a
[2], a
[3],
1486 b
[0], b
[1], b
[2], b
[3]);
1490 case OPCODE_SLT
: /* set on less */
1492 GLfloat a
[4], b
[4], result
[4];
1493 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1494 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1495 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1496 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1497 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1498 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1499 store_vector4(inst
, machine
, result
);
1501 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1502 result
[0], result
[1], result
[2], result
[3],
1503 a
[0], a
[1], a
[2], a
[3],
1504 b
[0], b
[1], b
[2], b
[3]);
1508 case OPCODE_SNE
: /* set on not equal */
1510 GLfloat a
[4], b
[4], result
[4];
1511 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1512 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1513 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1514 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1515 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1516 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1517 store_vector4(inst
, machine
, result
);
1519 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1520 result
[0], result
[1], result
[2], result
[3],
1521 a
[0], a
[1], a
[2], a
[3],
1522 b
[0], b
[1], b
[2], b
[3]);
1526 case OPCODE_SSG
: /* set sign (-1, 0 or +1) */
1528 GLfloat a
[4], result
[4];
1529 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1530 result
[0] = (GLfloat
) ((a
[0] > 0.0F
) - (a
[0] < 0.0F
));
1531 result
[1] = (GLfloat
) ((a
[1] > 0.0F
) - (a
[1] < 0.0F
));
1532 result
[2] = (GLfloat
) ((a
[2] > 0.0F
) - (a
[2] < 0.0F
));
1533 result
[3] = (GLfloat
) ((a
[3] > 0.0F
) - (a
[3] < 0.0F
));
1534 store_vector4(inst
, machine
, result
);
1537 case OPCODE_STR
: /* set true, operands ignored */
1539 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1540 store_vector4(inst
, machine
, result
);
1545 GLfloat a
[4], b
[4], result
[4];
1546 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1547 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1548 result
[0] = a
[0] - b
[0];
1549 result
[1] = a
[1] - b
[1];
1550 result
[2] = a
[2] - b
[2];
1551 result
[3] = a
[3] - b
[3];
1552 store_vector4(inst
, machine
, result
);
1554 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1555 result
[0], result
[1], result
[2], result
[3],
1556 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1560 case OPCODE_SWZ
: /* extended swizzle */
1562 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1563 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
1566 for (i
= 0; i
< 4; i
++) {
1567 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1568 if (swz
== SWIZZLE_ZERO
)
1570 else if (swz
== SWIZZLE_ONE
)
1575 result
[i
] = src
[swz
];
1577 if (source
->Negate
& (1 << i
))
1578 result
[i
] = -result
[i
];
1580 store_vector4(inst
, machine
, result
);
1583 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1584 /* Simple texel lookup */
1586 GLfloat texcoord
[4], color
[4];
1587 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1589 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1592 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1593 color
[0], color
[1], color
[2], color
[3],
1595 texcoord
[0], texcoord
[1], texcoord
[2], texcoord
[3]);
1597 store_vector4(inst
, machine
, color
);
1600 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1601 /* Texel lookup with LOD bias */
1603 GLfloat texcoord
[4], color
[4], lodBias
;
1605 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1607 /* texcoord[3] is the bias to add to lambda */
1608 lodBias
= texcoord
[3];
1610 fetch_texel(ctx
, machine
, inst
, texcoord
, lodBias
, color
);
1612 store_vector4(inst
, machine
, color
);
1615 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1616 /* Texture lookup w/ partial derivatives for LOD */
1618 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1619 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1620 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1621 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1622 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1624 inst
->TexSrcUnit
, color
);
1625 store_vector4(inst
, machine
, color
);
1628 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1629 /* Texture lookup w/ projective divide */
1631 GLfloat texcoord
[4], color
[4];
1633 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1634 /* Not so sure about this test - if texcoord[3] is
1635 * zero, we'd probably be fine except for an ASSERT in
1636 * IROUND_POS() which gets triggered by the inf values created.
1638 if (texcoord
[3] != 0.0) {
1639 texcoord
[0] /= texcoord
[3];
1640 texcoord
[1] /= texcoord
[3];
1641 texcoord
[2] /= texcoord
[3];
1644 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1646 store_vector4(inst
, machine
, color
);
1649 case OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1650 /* Texture lookup w/ projective divide, as above, but do not
1651 * do the divide by w if sampling from a cube map.
1654 GLfloat texcoord
[4], color
[4];
1656 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1657 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1658 texcoord
[3] != 0.0) {
1659 texcoord
[0] /= texcoord
[3];
1660 texcoord
[1] /= texcoord
[3];
1661 texcoord
[2] /= texcoord
[3];
1664 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1666 store_vector4(inst
, machine
, color
);
1669 case OPCODE_TRUNC
: /* truncate toward zero */
1671 GLfloat a
[4], result
[4];
1672 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1673 result
[0] = (GLfloat
) (GLint
) a
[0];
1674 result
[1] = (GLfloat
) (GLint
) a
[1];
1675 result
[2] = (GLfloat
) (GLint
) a
[2];
1676 result
[3] = (GLfloat
) (GLint
) a
[3];
1677 store_vector4(inst
, machine
, result
);
1680 case OPCODE_UP2H
: /* unpack two 16-bit floats */
1682 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1687 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1688 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1689 store_vector4(inst
, machine
, result
);
1692 case OPCODE_UP2US
: /* unpack two GLushorts */
1694 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1699 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1700 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1701 store_vector4(inst
, machine
, result
);
1704 case OPCODE_UP4B
: /* unpack four GLbytes */
1706 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1708 result
[0] = (((raw
>> 0) & 0xff) - 128) / 127.0F
;
1709 result
[1] = (((raw
>> 8) & 0xff) - 128) / 127.0F
;
1710 result
[2] = (((raw
>> 16) & 0xff) - 128) / 127.0F
;
1711 result
[3] = (((raw
>> 24) & 0xff) - 128) / 127.0F
;
1712 store_vector4(inst
, machine
, result
);
1715 case OPCODE_UP4UB
: /* unpack four GLubytes */
1717 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1719 result
[0] = ((raw
>> 0) & 0xff) / 255.0F
;
1720 result
[1] = ((raw
>> 8) & 0xff) / 255.0F
;
1721 result
[2] = ((raw
>> 16) & 0xff) / 255.0F
;
1722 result
[3] = ((raw
>> 24) & 0xff) / 255.0F
;
1723 store_vector4(inst
, machine
, result
);
1726 case OPCODE_XOR
: /* bitwise XOR */
1728 GLuint a
[4], b
[4], result
[4];
1729 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1730 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
1731 result
[0] = a
[0] ^ b
[0];
1732 result
[1] = a
[1] ^ b
[1];
1733 result
[2] = a
[2] ^ b
[2];
1734 result
[3] = a
[3] ^ b
[3];
1735 store_vector4ui(inst
, machine
, result
);
1738 case OPCODE_XPD
: /* cross product */
1740 GLfloat a
[4], b
[4], result
[4];
1741 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1742 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1743 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1744 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1745 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1747 store_vector4(inst
, machine
, result
);
1749 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1750 result
[0], result
[1], result
[2], result
[3],
1751 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1755 case OPCODE_X2D
: /* 2-D matrix transform */
1757 GLfloat a
[4], b
[4], c
[4], result
[4];
1758 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1759 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1760 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1761 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1762 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1763 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1764 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1765 store_vector4(inst
, machine
, result
);
1770 if (inst
->SrcReg
[0].File
!= -1) {
1772 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1773 printf("%s%g, %g, %g, %g\n", (const char *) inst
->Data
,
1774 a
[0], a
[1], a
[2], a
[3]);
1777 printf("%s\n", (const char *) inst
->Data
);
1784 _mesa_problem(ctx
, "Bad opcode %d in _mesa_execute_program",
1786 return GL_TRUE
; /* return value doesn't matter */
1790 if (numExec
> maxExec
) {
1791 _mesa_problem(ctx
, "Infinite loop detected in fragment program");