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/macros.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
};
83 * Return a pointer to the 4-element float vector specified by the given
86 static inline const GLfloat
*
87 get_src_register_pointer(const struct prog_src_register
*source
,
88 const struct gl_program_machine
*machine
)
90 const struct gl_program
*prog
= machine
->CurProgram
;
91 GLint reg
= source
->Index
;
93 if (source
->RelAddr
) {
94 /* add address register value to src index/offset */
95 reg
+= machine
->AddressReg
[0][0];
101 switch (source
->File
) {
102 case PROGRAM_TEMPORARY
:
103 if (reg
>= MAX_PROGRAM_TEMPS
)
105 return machine
->Temporaries
[reg
];
108 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
109 if (reg
>= VERT_ATTRIB_MAX
)
111 return machine
->VertAttribs
[reg
];
114 if (reg
>= FRAG_ATTRIB_MAX
)
116 return machine
->Attribs
[reg
][machine
->CurElement
];
120 if (reg
>= MAX_PROGRAM_OUTPUTS
)
122 return machine
->Outputs
[reg
];
124 case PROGRAM_LOCAL_PARAM
:
125 if (reg
>= MAX_PROGRAM_LOCAL_PARAMS
)
127 return machine
->CurProgram
->LocalParams
[reg
];
129 case PROGRAM_ENV_PARAM
:
130 if (reg
>= MAX_PROGRAM_ENV_PARAMS
)
132 return machine
->EnvParams
[reg
];
134 case PROGRAM_STATE_VAR
:
136 case PROGRAM_CONSTANT
:
138 case PROGRAM_UNIFORM
:
140 case PROGRAM_NAMED_PARAM
:
141 if (reg
>= (GLint
) prog
->Parameters
->NumParameters
)
143 return (GLfloat
*) prog
->Parameters
->ParameterValues
[reg
];
145 case PROGRAM_SYSTEM_VALUE
:
146 assert(reg
< Elements(machine
->SystemValues
));
147 return machine
->SystemValues
[reg
];
151 "Invalid src register file %d in get_src_register_pointer()",
159 * Return a pointer to the 4-element float vector specified by the given
160 * destination register.
162 static inline GLfloat
*
163 get_dst_register_pointer(const struct prog_dst_register
*dest
,
164 struct gl_program_machine
*machine
)
166 static GLfloat dummyReg
[4];
167 GLint reg
= dest
->Index
;
170 /* add address register value to src index/offset */
171 reg
+= machine
->AddressReg
[0][0];
177 switch (dest
->File
) {
178 case PROGRAM_TEMPORARY
:
179 if (reg
>= MAX_PROGRAM_TEMPS
)
181 return machine
->Temporaries
[reg
];
184 if (reg
>= MAX_PROGRAM_OUTPUTS
)
186 return machine
->Outputs
[reg
];
188 case PROGRAM_WRITE_ONLY
:
193 "Invalid dest register file %d in get_dst_register_pointer()",
202 * Fetch a 4-element float vector from the given source register.
203 * Apply swizzling and negating as needed.
206 fetch_vector4(const struct prog_src_register
*source
,
207 const struct gl_program_machine
*machine
, GLfloat result
[4])
209 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
212 if (source
->Swizzle
== SWIZZLE_NOOP
) {
214 COPY_4V(result
, src
);
217 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
218 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
219 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
220 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
221 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
222 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
223 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
224 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
228 result
[0] = FABSF(result
[0]);
229 result
[1] = FABSF(result
[1]);
230 result
[2] = FABSF(result
[2]);
231 result
[3] = FABSF(result
[3]);
233 if (source
->Negate
) {
234 ASSERT(source
->Negate
== NEGATE_XYZW
);
235 result
[0] = -result
[0];
236 result
[1] = -result
[1];
237 result
[2] = -result
[2];
238 result
[3] = -result
[3];
242 assert(!IS_INF_OR_NAN(result
[0]));
243 assert(!IS_INF_OR_NAN(result
[0]));
244 assert(!IS_INF_OR_NAN(result
[0]));
245 assert(!IS_INF_OR_NAN(result
[0]));
251 * Fetch a 4-element uint vector from the given source register.
252 * Apply swizzling but not negation/abs.
255 fetch_vector4ui(const struct prog_src_register
*source
,
256 const struct gl_program_machine
*machine
, GLuint result
[4])
258 const GLuint
*src
= (GLuint
*) get_src_register_pointer(source
, machine
);
261 if (source
->Swizzle
== SWIZZLE_NOOP
) {
263 COPY_4V(result
, src
);
266 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
267 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
268 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
269 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
270 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
271 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
272 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
273 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
276 /* Note: no Negate or Abs here */
282 * Fetch the derivative with respect to X or Y for the given register.
283 * XXX this currently only works for fragment program input attribs.
286 fetch_vector4_deriv(struct gl_context
* ctx
,
287 const struct prog_src_register
*source
,
288 const struct gl_program_machine
*machine
,
289 char xOrY
, GLfloat result
[4])
291 if (source
->File
== PROGRAM_INPUT
&&
292 source
->Index
< (GLint
) machine
->NumDeriv
) {
293 const GLint col
= machine
->CurElement
;
294 const GLfloat w
= machine
->Attribs
[FRAG_ATTRIB_WPOS
][col
][3];
295 const GLfloat invQ
= 1.0f
/ w
;
299 deriv
[0] = machine
->DerivX
[source
->Index
][0] * invQ
;
300 deriv
[1] = machine
->DerivX
[source
->Index
][1] * invQ
;
301 deriv
[2] = machine
->DerivX
[source
->Index
][2] * invQ
;
302 deriv
[3] = machine
->DerivX
[source
->Index
][3] * invQ
;
305 deriv
[0] = machine
->DerivY
[source
->Index
][0] * invQ
;
306 deriv
[1] = machine
->DerivY
[source
->Index
][1] * invQ
;
307 deriv
[2] = machine
->DerivY
[source
->Index
][2] * invQ
;
308 deriv
[3] = machine
->DerivY
[source
->Index
][3] * invQ
;
311 result
[0] = deriv
[GET_SWZ(source
->Swizzle
, 0)];
312 result
[1] = deriv
[GET_SWZ(source
->Swizzle
, 1)];
313 result
[2] = deriv
[GET_SWZ(source
->Swizzle
, 2)];
314 result
[3] = deriv
[GET_SWZ(source
->Swizzle
, 3)];
317 result
[0] = FABSF(result
[0]);
318 result
[1] = FABSF(result
[1]);
319 result
[2] = FABSF(result
[2]);
320 result
[3] = FABSF(result
[3]);
322 if (source
->Negate
) {
323 ASSERT(source
->Negate
== NEGATE_XYZW
);
324 result
[0] = -result
[0];
325 result
[1] = -result
[1];
326 result
[2] = -result
[2];
327 result
[3] = -result
[3];
331 ASSIGN_4V(result
, 0.0, 0.0, 0.0, 0.0);
337 * As above, but only return result[0] element.
340 fetch_vector1(const struct prog_src_register
*source
,
341 const struct gl_program_machine
*machine
, GLfloat result
[4])
343 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
346 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
349 result
[0] = FABSF(result
[0]);
351 if (source
->Negate
) {
352 result
[0] = -result
[0];
358 fetch_vector1ui(const struct prog_src_register
*source
,
359 const struct gl_program_machine
*machine
)
361 const GLuint
*src
= (GLuint
*) get_src_register_pointer(source
, machine
);
362 return src
[GET_SWZ(source
->Swizzle
, 0)];
367 * Fetch texel from texture. Use partial derivatives when possible.
370 fetch_texel(struct gl_context
*ctx
,
371 const struct gl_program_machine
*machine
,
372 const struct prog_instruction
*inst
,
373 const GLfloat texcoord
[4], GLfloat lodBias
,
376 const GLuint unit
= machine
->Samplers
[inst
->TexSrcUnit
];
378 /* Note: we only have the right derivatives for fragment input attribs.
380 if (machine
->NumDeriv
> 0 &&
381 inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
382 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
) {
383 /* simple texture fetch for which we should have derivatives */
384 GLuint attr
= inst
->SrcReg
[0].Index
;
385 machine
->FetchTexelDeriv(ctx
, texcoord
,
386 machine
->DerivX
[attr
],
387 machine
->DerivY
[attr
],
388 lodBias
, unit
, color
);
391 machine
->FetchTexelLod(ctx
, texcoord
, lodBias
, unit
, color
);
397 * Test value against zero and return GT, LT, EQ or UN if NaN.
400 generate_cc(float value
)
403 return COND_UN
; /* NaN */
413 * Test if the ccMaskRule is satisfied by the given condition code.
414 * Used to mask destination writes according to the current condition code.
416 static inline GLboolean
417 test_cc(GLuint condCode
, GLuint ccMaskRule
)
419 switch (ccMaskRule
) {
420 case COND_EQ
: return (condCode
== COND_EQ
);
421 case COND_NE
: return (condCode
!= COND_EQ
);
422 case COND_LT
: return (condCode
== COND_LT
);
423 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
424 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
425 case COND_GT
: return (condCode
== COND_GT
);
426 case COND_TR
: return GL_TRUE
;
427 case COND_FL
: return GL_FALSE
;
428 default: return GL_TRUE
;
434 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
435 * or GL_FALSE to indicate result.
437 static inline GLboolean
438 eval_condition(const struct gl_program_machine
*machine
,
439 const struct prog_instruction
*inst
)
441 const GLuint swizzle
= inst
->DstReg
.CondSwizzle
;
442 const GLuint condMask
= inst
->DstReg
.CondMask
;
443 if (test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 0)], condMask
) ||
444 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 1)], condMask
) ||
445 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 2)], condMask
) ||
446 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 3)], condMask
)) {
457 * Store 4 floats into a register. Observe the instructions saturate and
458 * set-condition-code flags.
461 store_vector4(const struct prog_instruction
*inst
,
462 struct gl_program_machine
*machine
, const GLfloat value
[4])
464 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
465 const GLboolean clamp
= inst
->SaturateMode
== SATURATE_ZERO_ONE
;
466 GLuint writeMask
= dstReg
->WriteMask
;
467 GLfloat clampedValue
[4];
468 GLfloat
*dst
= get_dst_register_pointer(dstReg
, machine
);
471 if (value
[0] > 1.0e10
||
472 IS_INF_OR_NAN(value
[0]) ||
473 IS_INF_OR_NAN(value
[1]) ||
474 IS_INF_OR_NAN(value
[2]) || IS_INF_OR_NAN(value
[3]))
475 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
479 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
480 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
481 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
482 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
483 value
= clampedValue
;
486 if (dstReg
->CondMask
!= COND_TR
) {
487 /* condition codes may turn off some writes */
488 if (writeMask
& WRITEMASK_X
) {
489 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
491 writeMask
&= ~WRITEMASK_X
;
493 if (writeMask
& WRITEMASK_Y
) {
494 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
496 writeMask
&= ~WRITEMASK_Y
;
498 if (writeMask
& WRITEMASK_Z
) {
499 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
501 writeMask
&= ~WRITEMASK_Z
;
503 if (writeMask
& WRITEMASK_W
) {
504 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
506 writeMask
&= ~WRITEMASK_W
;
511 assert(!IS_INF_OR_NAN(value
[0]));
512 assert(!IS_INF_OR_NAN(value
[0]));
513 assert(!IS_INF_OR_NAN(value
[0]));
514 assert(!IS_INF_OR_NAN(value
[0]));
517 if (writeMask
& WRITEMASK_X
)
519 if (writeMask
& WRITEMASK_Y
)
521 if (writeMask
& WRITEMASK_Z
)
523 if (writeMask
& WRITEMASK_W
)
526 if (inst
->CondUpdate
) {
527 if (writeMask
& WRITEMASK_X
)
528 machine
->CondCodes
[0] = generate_cc(value
[0]);
529 if (writeMask
& WRITEMASK_Y
)
530 machine
->CondCodes
[1] = generate_cc(value
[1]);
531 if (writeMask
& WRITEMASK_Z
)
532 machine
->CondCodes
[2] = generate_cc(value
[2]);
533 if (writeMask
& WRITEMASK_W
)
534 machine
->CondCodes
[3] = generate_cc(value
[3]);
536 printf("CondCodes=(%s,%s,%s,%s) for:\n",
537 _mesa_condcode_string(machine
->CondCodes
[0]),
538 _mesa_condcode_string(machine
->CondCodes
[1]),
539 _mesa_condcode_string(machine
->CondCodes
[2]),
540 _mesa_condcode_string(machine
->CondCodes
[3]));
547 * Store 4 uints into a register. Observe the set-condition-code flags.
550 store_vector4ui(const struct prog_instruction
*inst
,
551 struct gl_program_machine
*machine
, const GLuint value
[4])
553 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
554 GLuint writeMask
= dstReg
->WriteMask
;
555 GLuint
*dst
= (GLuint
*) get_dst_register_pointer(dstReg
, machine
);
557 if (dstReg
->CondMask
!= COND_TR
) {
558 /* condition codes may turn off some writes */
559 if (writeMask
& WRITEMASK_X
) {
560 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
562 writeMask
&= ~WRITEMASK_X
;
564 if (writeMask
& WRITEMASK_Y
) {
565 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
567 writeMask
&= ~WRITEMASK_Y
;
569 if (writeMask
& WRITEMASK_Z
) {
570 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
572 writeMask
&= ~WRITEMASK_Z
;
574 if (writeMask
& WRITEMASK_W
) {
575 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
577 writeMask
&= ~WRITEMASK_W
;
581 if (writeMask
& WRITEMASK_X
)
583 if (writeMask
& WRITEMASK_Y
)
585 if (writeMask
& WRITEMASK_Z
)
587 if (writeMask
& WRITEMASK_W
)
590 if (inst
->CondUpdate
) {
591 if (writeMask
& WRITEMASK_X
)
592 machine
->CondCodes
[0] = generate_cc((float)value
[0]);
593 if (writeMask
& WRITEMASK_Y
)
594 machine
->CondCodes
[1] = generate_cc((float)value
[1]);
595 if (writeMask
& WRITEMASK_Z
)
596 machine
->CondCodes
[2] = generate_cc((float)value
[2]);
597 if (writeMask
& WRITEMASK_W
)
598 machine
->CondCodes
[3] = generate_cc((float)value
[3]);
600 printf("CondCodes=(%s,%s,%s,%s) for:\n",
601 _mesa_condcode_string(machine
->CondCodes
[0]),
602 _mesa_condcode_string(machine
->CondCodes
[1]),
603 _mesa_condcode_string(machine
->CondCodes
[2]),
604 _mesa_condcode_string(machine
->CondCodes
[3]));
612 * Execute the given vertex/fragment program.
614 * \param ctx rendering context
615 * \param program the program to execute
616 * \param machine machine state (must be initialized)
617 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
620 _mesa_execute_program(struct gl_context
* ctx
,
621 const struct gl_program
*program
,
622 struct gl_program_machine
*machine
)
624 const GLuint numInst
= program
->NumInstructions
;
625 const GLuint maxExec
= 65536;
626 GLuint pc
, numExec
= 0;
628 machine
->CurProgram
= program
;
631 printf("execute program %u --------------------\n", program
->Id
);
634 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
635 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
638 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
641 for (pc
= 0; pc
< numInst
; pc
++) {
642 const struct prog_instruction
*inst
= program
->Instructions
+ pc
;
645 _mesa_print_instruction(inst
);
648 switch (inst
->Opcode
) {
651 GLfloat a
[4], result
[4];
652 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
653 result
[0] = FABSF(a
[0]);
654 result
[1] = FABSF(a
[1]);
655 result
[2] = FABSF(a
[2]);
656 result
[3] = FABSF(a
[3]);
657 store_vector4(inst
, machine
, result
);
662 GLfloat a
[4], b
[4], result
[4];
663 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
664 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
665 result
[0] = a
[0] + b
[0];
666 result
[1] = a
[1] + b
[1];
667 result
[2] = a
[2] + b
[2];
668 result
[3] = a
[3] + b
[3];
669 store_vector4(inst
, machine
, result
);
671 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
672 result
[0], result
[1], result
[2], result
[3],
673 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
677 case OPCODE_AND
: /* bitwise AND */
679 GLuint a
[4], b
[4], result
[4];
680 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
681 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
682 result
[0] = a
[0] & b
[0];
683 result
[1] = a
[1] & b
[1];
684 result
[2] = a
[2] & b
[2];
685 result
[3] = a
[3] & b
[3];
686 store_vector4ui(inst
, machine
, result
);
692 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
693 machine
->AddressReg
[0][0] = IFLOOR(t
[0]);
695 printf("ARL %d\n", machine
->AddressReg
[0][0]);
701 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
705 /* subtract 1 here since pc is incremented by for(pc) loop */
706 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
708 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
710 case OPCODE_BGNSUB
: /* begin subroutine */
712 case OPCODE_ENDSUB
: /* end subroutine */
714 case OPCODE_BRK
: /* break out of loop (conditional) */
715 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
717 if (eval_condition(machine
, inst
)) {
718 /* break out of loop */
719 /* pc++ at end of for-loop will put us after the ENDLOOP inst */
720 pc
= inst
->BranchTarget
;
723 case OPCODE_CONT
: /* continue loop (conditional) */
724 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
726 if (eval_condition(machine
, inst
)) {
727 /* continue at ENDLOOP */
728 /* Subtract 1 here since we'll do pc++ at end of for-loop */
729 pc
= inst
->BranchTarget
- 1;
732 case OPCODE_CAL
: /* Call subroutine (conditional) */
733 if (eval_condition(machine
, inst
)) {
734 /* call the subroutine */
735 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
736 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
738 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1; /* next inst */
739 /* Subtract 1 here since we'll do pc++ at end of for-loop */
740 pc
= inst
->BranchTarget
- 1;
745 GLfloat a
[4], b
[4], c
[4], result
[4];
746 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
747 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
748 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
749 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
750 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
751 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
752 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
753 store_vector4(inst
, machine
, result
);
755 printf("CMP (%g %g %g %g) = (%g %g %g %g) < 0 ? (%g %g %g %g) : (%g %g %g %g)\n",
756 result
[0], result
[1], result
[2], result
[3],
757 a
[0], a
[1], a
[2], a
[3],
758 b
[0], b
[1], b
[2], b
[3],
759 c
[0], c
[1], c
[2], c
[3]);
765 GLfloat a
[4], result
[4];
766 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
767 result
[0] = result
[1] = result
[2] = result
[3]
768 = (GLfloat
) cos(a
[0]);
769 store_vector4(inst
, machine
, result
);
772 case OPCODE_DDX
: /* Partial derivative with respect to X */
775 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
777 store_vector4(inst
, machine
, result
);
780 case OPCODE_DDY
: /* Partial derivative with respect to Y */
783 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
785 store_vector4(inst
, machine
, result
);
790 GLfloat a
[4], b
[4], result
[4];
791 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
792 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
793 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
);
794 store_vector4(inst
, machine
, result
);
796 printf("DP2 %g = (%g %g) . (%g %g)\n",
797 result
[0], a
[0], a
[1], b
[0], b
[1]);
803 GLfloat a
[4], b
[4], c
, result
[4];
804 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
805 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
806 fetch_vector1(&inst
->SrcReg
[1], machine
, &c
);
807 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
) + c
;
808 store_vector4(inst
, machine
, result
);
810 printf("DP2A %g = (%g %g) . (%g %g) + %g\n",
811 result
[0], a
[0], a
[1], b
[0], b
[1], c
);
817 GLfloat a
[4], b
[4], result
[4];
818 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
819 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
820 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
821 store_vector4(inst
, machine
, result
);
823 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
824 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
830 GLfloat a
[4], b
[4], result
[4];
831 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
832 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
833 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
, b
);
834 store_vector4(inst
, machine
, result
);
836 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
837 result
[0], a
[0], a
[1], a
[2], a
[3],
838 b
[0], b
[1], b
[2], b
[3]);
844 GLfloat a
[4], b
[4], result
[4];
845 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
846 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
847 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
) + b
[3];
848 store_vector4(inst
, machine
, result
);
851 case OPCODE_DST
: /* Distance vector */
853 GLfloat a
[4], b
[4], result
[4];
854 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
855 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
857 result
[1] = a
[1] * b
[1];
860 store_vector4(inst
, machine
, result
);
865 GLfloat t
[4], q
[4], floor_t0
;
866 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
867 floor_t0
= FLOORF(t
[0]);
868 if (floor_t0
> FLT_MAX_EXP
) {
869 SET_POS_INFINITY(q
[0]);
870 SET_POS_INFINITY(q
[2]);
872 else if (floor_t0
< FLT_MIN_EXP
) {
877 q
[0] = LDEXPF(1.0, (int) floor_t0
);
878 /* Note: GL_NV_vertex_program expects
879 * result.z = result.x * APPX(result.y)
880 * We do what the ARB extension says.
882 q
[2] = (GLfloat
) pow(2.0, t
[0]);
884 q
[1] = t
[0] - floor_t0
;
886 store_vector4( inst
, machine
, q
);
889 case OPCODE_EX2
: /* Exponential base 2 */
891 GLfloat a
[4], result
[4], val
;
892 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
893 val
= (GLfloat
) pow(2.0, a
[0]);
895 if (IS_INF_OR_NAN(val))
898 result
[0] = result
[1] = result
[2] = result
[3] = val
;
899 store_vector4(inst
, machine
, result
);
904 GLfloat a
[4], result
[4];
905 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
906 result
[0] = FLOORF(a
[0]);
907 result
[1] = FLOORF(a
[1]);
908 result
[2] = FLOORF(a
[2]);
909 result
[3] = FLOORF(a
[3]);
910 store_vector4(inst
, machine
, result
);
915 GLfloat a
[4], result
[4];
916 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
917 result
[0] = a
[0] - FLOORF(a
[0]);
918 result
[1] = a
[1] - FLOORF(a
[1]);
919 result
[2] = a
[2] - FLOORF(a
[2]);
920 result
[3] = a
[3] - FLOORF(a
[3]);
921 store_vector4(inst
, machine
, result
);
927 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
929 program
->Instructions
[inst
->BranchTarget
].Opcode
932 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
934 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
935 cond
= (a
[0] != 0.0);
938 cond
= eval_condition(machine
, inst
);
941 printf("IF: %d\n", cond
);
945 /* do if-clause (just continue execution) */
948 /* go to the instruction after ELSE or ENDIF */
949 assert(inst
->BranchTarget
>= 0);
950 pc
= inst
->BranchTarget
;
956 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
958 assert(inst
->BranchTarget
>= 0);
959 pc
= inst
->BranchTarget
;
964 case OPCODE_KIL_NV
: /* NV_f_p only (conditional) */
965 if (eval_condition(machine
, inst
)) {
969 case OPCODE_KIL
: /* ARB_f_p only */
972 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
974 printf("KIL if (%g %g %g %g) <= 0.0\n",
975 a
[0], a
[1], a
[2], a
[3]);
978 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
983 case OPCODE_LG2
: /* log base 2 */
985 GLfloat a
[4], result
[4], val
;
986 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
987 /* The fast LOG2 macro doesn't meet the precision requirements.
993 val
= (float)(log(a
[0]) * 1.442695F
);
995 result
[0] = result
[1] = result
[2] = result
[3] = val
;
996 store_vector4(inst
, machine
, result
);
1001 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
1002 GLfloat a
[4], result
[4];
1003 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1004 a
[0] = MAX2(a
[0], 0.0F
);
1005 a
[1] = MAX2(a
[1], 0.0F
);
1006 /* XXX ARB version clamps a[3], NV version doesn't */
1007 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
1010 /* XXX we could probably just use pow() here */
1012 if (a
[1] == 0.0 && a
[3] == 0.0)
1015 result
[2] = (GLfloat
) pow(a
[1], a
[3]);
1021 store_vector4(inst
, machine
, result
);
1023 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
1024 result
[0], result
[1], result
[2], result
[3],
1025 a
[0], a
[1], a
[2], a
[3]);
1031 GLfloat t
[4], q
[4], abs_t0
;
1032 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
1033 abs_t0
= FABSF(t
[0]);
1034 if (abs_t0
!= 0.0F
) {
1035 /* Since we really can't handle infinite values on VMS
1036 * like other OSes we'll use __MAXFLOAT to represent
1037 * infinity. This may need some tweaking.
1040 if (abs_t0
== __MAXFLOAT
)
1042 if (IS_INF_OR_NAN(abs_t0
))
1045 SET_POS_INFINITY(q
[0]);
1047 SET_POS_INFINITY(q
[2]);
1051 GLfloat mantissa
= FREXPF(t
[0], &exponent
);
1052 q
[0] = (GLfloat
) (exponent
- 1);
1053 q
[1] = (GLfloat
) (2.0 * mantissa
); /* map [.5, 1) -> [1, 2) */
1055 /* The fast LOG2 macro doesn't meet the precision
1058 q
[2] = (float)(log(t
[0]) * 1.442695F
);
1062 SET_NEG_INFINITY(q
[0]);
1064 SET_NEG_INFINITY(q
[2]);
1067 store_vector4(inst
, machine
, q
);
1072 GLfloat a
[4], b
[4], c
[4], result
[4];
1073 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1074 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1075 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1076 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
1077 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
1078 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
1079 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
1080 store_vector4(inst
, machine
, result
);
1082 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
1083 "(%g %g %g %g), (%g %g %g %g)\n",
1084 result
[0], result
[1], result
[2], result
[3],
1085 a
[0], a
[1], a
[2], a
[3],
1086 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1092 GLfloat a
[4], b
[4], c
[4], result
[4];
1093 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1094 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1095 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1096 result
[0] = a
[0] * b
[0] + c
[0];
1097 result
[1] = a
[1] * b
[1] + c
[1];
1098 result
[2] = a
[2] * b
[2] + c
[2];
1099 result
[3] = a
[3] * b
[3] + c
[3];
1100 store_vector4(inst
, machine
, result
);
1102 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
1103 "(%g %g %g %g) + (%g %g %g %g)\n",
1104 result
[0], result
[1], result
[2], result
[3],
1105 a
[0], a
[1], a
[2], a
[3],
1106 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1112 GLfloat a
[4], b
[4], result
[4];
1113 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1114 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1115 result
[0] = MAX2(a
[0], b
[0]);
1116 result
[1] = MAX2(a
[1], b
[1]);
1117 result
[2] = MAX2(a
[2], b
[2]);
1118 result
[3] = MAX2(a
[3], b
[3]);
1119 store_vector4(inst
, machine
, result
);
1121 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
1122 result
[0], result
[1], result
[2], result
[3],
1123 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1129 GLfloat a
[4], b
[4], result
[4];
1130 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1131 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1132 result
[0] = MIN2(a
[0], b
[0]);
1133 result
[1] = MIN2(a
[1], b
[1]);
1134 result
[2] = MIN2(a
[2], b
[2]);
1135 result
[3] = MIN2(a
[3], b
[3]);
1136 store_vector4(inst
, machine
, result
);
1142 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
1143 store_vector4(inst
, machine
, result
);
1145 printf("MOV (%g %g %g %g)\n",
1146 result
[0], result
[1], result
[2], result
[3]);
1152 GLfloat a
[4], b
[4], result
[4];
1153 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1154 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1155 result
[0] = a
[0] * b
[0];
1156 result
[1] = a
[1] * b
[1];
1157 result
[2] = a
[2] * b
[2];
1158 result
[3] = a
[3] * b
[3];
1159 store_vector4(inst
, machine
, result
);
1161 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1162 result
[0], result
[1], result
[2], result
[3],
1163 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1169 GLfloat a
[4], result
[4];
1170 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1174 result
[3] = _mesa_noise1(a
[0]);
1175 store_vector4(inst
, machine
, result
);
1180 GLfloat a
[4], result
[4];
1181 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1184 result
[2] = result
[3] = _mesa_noise2(a
[0], a
[1]);
1185 store_vector4(inst
, machine
, result
);
1190 GLfloat a
[4], result
[4];
1191 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1195 result
[3] = _mesa_noise3(a
[0], a
[1], a
[2]);
1196 store_vector4(inst
, machine
, result
);
1201 GLfloat a
[4], result
[4];
1202 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1206 result
[3] = _mesa_noise4(a
[0], a
[1], a
[2], a
[3]);
1207 store_vector4(inst
, machine
, result
);
1212 case OPCODE_NOT
: /* bitwise NOT */
1214 GLuint a
[4], result
[4];
1215 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1220 store_vector4ui(inst
, machine
, result
);
1223 case OPCODE_NRM3
: /* 3-component normalization */
1225 GLfloat a
[4], result
[4];
1227 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1228 tmp
= a
[0] * a
[0] + a
[1] * a
[1] + a
[2] * a
[2];
1230 tmp
= INV_SQRTF(tmp
);
1231 result
[0] = tmp
* a
[0];
1232 result
[1] = tmp
* a
[1];
1233 result
[2] = tmp
* a
[2];
1234 result
[3] = 0.0; /* undefined, but prevent valgrind warnings */
1235 store_vector4(inst
, machine
, result
);
1238 case OPCODE_NRM4
: /* 4-component normalization */
1240 GLfloat a
[4], result
[4];
1242 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1243 tmp
= a
[0] * a
[0] + a
[1] * a
[1] + a
[2] * a
[2] + a
[3] * a
[3];
1245 tmp
= INV_SQRTF(tmp
);
1246 result
[0] = tmp
* a
[0];
1247 result
[1] = tmp
* a
[1];
1248 result
[2] = tmp
* a
[2];
1249 result
[3] = tmp
* a
[3];
1250 store_vector4(inst
, machine
, result
);
1253 case OPCODE_OR
: /* bitwise OR */
1255 GLuint a
[4], b
[4], result
[4];
1256 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1257 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
1258 result
[0] = a
[0] | b
[0];
1259 result
[1] = a
[1] | b
[1];
1260 result
[2] = a
[2] | b
[2];
1261 result
[3] = a
[3] | b
[3];
1262 store_vector4ui(inst
, machine
, result
);
1265 case OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
1270 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1271 hx
= _mesa_float_to_half(a
[0]);
1272 hy
= _mesa_float_to_half(a
[1]);
1276 result
[3] = hx
| (hy
<< 16);
1277 store_vector4ui(inst
, machine
, result
);
1280 case OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
1283 GLuint result
[4], usx
, usy
;
1284 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1285 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1286 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1287 usx
= F_TO_I(a
[0] * 65535.0F
);
1288 usy
= F_TO_I(a
[1] * 65535.0F
);
1292 result
[3] = usx
| (usy
<< 16);
1293 store_vector4ui(inst
, machine
, result
);
1296 case OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
1299 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1300 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1301 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
1302 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
1303 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
1304 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
1305 ubx
= F_TO_I(127.0F
* a
[0] + 128.0F
);
1306 uby
= F_TO_I(127.0F
* a
[1] + 128.0F
);
1307 ubz
= F_TO_I(127.0F
* a
[2] + 128.0F
);
1308 ubw
= F_TO_I(127.0F
* a
[3] + 128.0F
);
1312 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1313 store_vector4ui(inst
, machine
, result
);
1316 case OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
1319 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1320 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1321 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1322 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1323 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
1324 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
1325 ubx
= F_TO_I(255.0F
* a
[0]);
1326 uby
= F_TO_I(255.0F
* a
[1]);
1327 ubz
= F_TO_I(255.0F
* a
[2]);
1328 ubw
= F_TO_I(255.0F
* a
[3]);
1332 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1333 store_vector4ui(inst
, machine
, result
);
1338 GLfloat a
[4], b
[4], result
[4];
1339 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1340 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
1341 result
[0] = result
[1] = result
[2] = result
[3]
1342 = (GLfloat
) pow(a
[0], b
[0]);
1343 store_vector4(inst
, machine
, result
);
1349 GLfloat a
[4], result
[4];
1350 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1354 else if (IS_INF_OR_NAN(a
[0]))
1355 printf("RCP(inf)\n");
1357 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
1358 store_vector4(inst
, machine
, result
);
1361 case OPCODE_RET
: /* return from subroutine (conditional) */
1362 if (eval_condition(machine
, inst
)) {
1363 if (machine
->StackDepth
== 0) {
1364 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
1366 /* subtract one because of pc++ in the for loop */
1367 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
1370 case OPCODE_RFL
: /* reflection vector */
1372 GLfloat axis
[4], dir
[4], result
[4], tmpX
, tmpW
;
1373 fetch_vector4(&inst
->SrcReg
[0], machine
, axis
);
1374 fetch_vector4(&inst
->SrcReg
[1], machine
, dir
);
1375 tmpW
= DOT3(axis
, axis
);
1376 tmpX
= (2.0F
* DOT3(axis
, dir
)) / tmpW
;
1377 result
[0] = tmpX
* axis
[0] - dir
[0];
1378 result
[1] = tmpX
* axis
[1] - dir
[1];
1379 result
[2] = tmpX
* axis
[2] - dir
[2];
1380 /* result[3] is never written! XXX enforce in parser! */
1381 store_vector4(inst
, machine
, result
);
1384 case OPCODE_RSQ
: /* 1 / sqrt() */
1386 GLfloat a
[4], result
[4];
1387 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1389 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1390 store_vector4(inst
, machine
, result
);
1392 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
1396 case OPCODE_SCS
: /* sine and cos */
1398 GLfloat a
[4], result
[4];
1399 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1400 result
[0] = (GLfloat
) cos(a
[0]);
1401 result
[1] = (GLfloat
) sin(a
[0]);
1402 result
[2] = 0.0; /* undefined! */
1403 result
[3] = 0.0; /* undefined! */
1404 store_vector4(inst
, machine
, result
);
1407 case OPCODE_SEQ
: /* set on equal */
1409 GLfloat a
[4], b
[4], result
[4];
1410 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1411 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1412 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1413 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1414 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1415 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1416 store_vector4(inst
, machine
, result
);
1418 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1419 result
[0], result
[1], result
[2], result
[3],
1420 a
[0], a
[1], a
[2], a
[3],
1421 b
[0], b
[1], b
[2], b
[3]);
1425 case OPCODE_SFL
: /* set false, operands ignored */
1427 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1428 store_vector4(inst
, machine
, result
);
1431 case OPCODE_SGE
: /* set on greater or equal */
1433 GLfloat a
[4], b
[4], result
[4];
1434 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1435 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1436 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1437 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1438 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1439 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1440 store_vector4(inst
, machine
, result
);
1442 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1443 result
[0], result
[1], result
[2], result
[3],
1444 a
[0], a
[1], a
[2], a
[3],
1445 b
[0], b
[1], b
[2], b
[3]);
1449 case OPCODE_SGT
: /* set on greater */
1451 GLfloat a
[4], b
[4], result
[4];
1452 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1453 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1454 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1455 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1456 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1457 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1458 store_vector4(inst
, machine
, result
);
1460 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1461 result
[0], result
[1], result
[2], result
[3],
1462 a
[0], a
[1], a
[2], a
[3],
1463 b
[0], b
[1], b
[2], b
[3]);
1469 GLfloat a
[4], result
[4];
1470 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1471 result
[0] = result
[1] = result
[2] = result
[3]
1472 = (GLfloat
) sin(a
[0]);
1473 store_vector4(inst
, machine
, result
);
1476 case OPCODE_SLE
: /* set on less or equal */
1478 GLfloat a
[4], b
[4], result
[4];
1479 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1480 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1481 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1482 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1483 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1484 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1485 store_vector4(inst
, machine
, result
);
1487 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1488 result
[0], result
[1], result
[2], result
[3],
1489 a
[0], a
[1], a
[2], a
[3],
1490 b
[0], b
[1], b
[2], b
[3]);
1494 case OPCODE_SLT
: /* set on less */
1496 GLfloat a
[4], b
[4], result
[4];
1497 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1498 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1499 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1500 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1501 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1502 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1503 store_vector4(inst
, machine
, result
);
1505 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1506 result
[0], result
[1], result
[2], result
[3],
1507 a
[0], a
[1], a
[2], a
[3],
1508 b
[0], b
[1], b
[2], b
[3]);
1512 case OPCODE_SNE
: /* set on not equal */
1514 GLfloat a
[4], b
[4], result
[4];
1515 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1516 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1517 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1518 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1519 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1520 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1521 store_vector4(inst
, machine
, result
);
1523 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1524 result
[0], result
[1], result
[2], result
[3],
1525 a
[0], a
[1], a
[2], a
[3],
1526 b
[0], b
[1], b
[2], b
[3]);
1530 case OPCODE_SSG
: /* set sign (-1, 0 or +1) */
1532 GLfloat a
[4], result
[4];
1533 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1534 result
[0] = (GLfloat
) ((a
[0] > 0.0F
) - (a
[0] < 0.0F
));
1535 result
[1] = (GLfloat
) ((a
[1] > 0.0F
) - (a
[1] < 0.0F
));
1536 result
[2] = (GLfloat
) ((a
[2] > 0.0F
) - (a
[2] < 0.0F
));
1537 result
[3] = (GLfloat
) ((a
[3] > 0.0F
) - (a
[3] < 0.0F
));
1538 store_vector4(inst
, machine
, result
);
1541 case OPCODE_STR
: /* set true, operands ignored */
1543 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1544 store_vector4(inst
, machine
, result
);
1549 GLfloat a
[4], b
[4], result
[4];
1550 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1551 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1552 result
[0] = a
[0] - b
[0];
1553 result
[1] = a
[1] - b
[1];
1554 result
[2] = a
[2] - b
[2];
1555 result
[3] = a
[3] - b
[3];
1556 store_vector4(inst
, machine
, result
);
1558 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1559 result
[0], result
[1], result
[2], result
[3],
1560 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1564 case OPCODE_SWZ
: /* extended swizzle */
1566 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1567 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
1570 for (i
= 0; i
< 4; i
++) {
1571 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1572 if (swz
== SWIZZLE_ZERO
)
1574 else if (swz
== SWIZZLE_ONE
)
1579 result
[i
] = src
[swz
];
1581 if (source
->Negate
& (1 << i
))
1582 result
[i
] = -result
[i
];
1584 store_vector4(inst
, machine
, result
);
1587 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1588 /* Simple texel lookup */
1590 GLfloat texcoord
[4], color
[4];
1591 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1593 /* For TEX, texcoord.Q should not be used and its value should not
1594 * matter (at most, we pass coord.xyz to texture3D() in GLSL).
1595 * Set Q=1 so that FetchTexelDeriv() doesn't get a garbage value
1596 * which is effectively what happens when the texcoord swizzle
1601 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1604 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1605 color
[0], color
[1], color
[2], color
[3],
1607 texcoord
[0], texcoord
[1], texcoord
[2], texcoord
[3]);
1609 store_vector4(inst
, machine
, color
);
1612 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1613 /* Texel lookup with LOD bias */
1615 GLfloat texcoord
[4], color
[4], lodBias
;
1617 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1619 /* texcoord[3] is the bias to add to lambda */
1620 lodBias
= texcoord
[3];
1622 fetch_texel(ctx
, machine
, inst
, texcoord
, lodBias
, color
);
1625 printf("TXB (%g, %g, %g, %g) = texture[%d][%g %g %g %g]"
1627 color
[0], color
[1], color
[2], color
[3],
1636 store_vector4(inst
, machine
, color
);
1639 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1640 /* Texture lookup w/ partial derivatives for LOD */
1642 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1643 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1644 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1645 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1646 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1648 inst
->TexSrcUnit
, color
);
1649 store_vector4(inst
, machine
, color
);
1653 /* Texel lookup with explicit LOD */
1655 GLfloat texcoord
[4], color
[4], lod
;
1657 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1659 /* texcoord[3] is the LOD */
1662 machine
->FetchTexelLod(ctx
, texcoord
, lod
,
1663 machine
->Samplers
[inst
->TexSrcUnit
], color
);
1665 store_vector4(inst
, machine
, color
);
1668 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1669 /* Texture lookup w/ projective divide */
1671 GLfloat texcoord
[4], color
[4];
1673 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1674 /* Not so sure about this test - if texcoord[3] is
1675 * zero, we'd probably be fine except for an ASSERT in
1676 * IROUND_POS() which gets triggered by the inf values created.
1678 if (texcoord
[3] != 0.0) {
1679 texcoord
[0] /= texcoord
[3];
1680 texcoord
[1] /= texcoord
[3];
1681 texcoord
[2] /= texcoord
[3];
1684 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1686 store_vector4(inst
, machine
, color
);
1689 case OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1690 /* Texture lookup w/ projective divide, as above, but do not
1691 * do the divide by w if sampling from a cube map.
1694 GLfloat texcoord
[4], color
[4];
1696 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1697 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1698 texcoord
[3] != 0.0) {
1699 texcoord
[0] /= texcoord
[3];
1700 texcoord
[1] /= texcoord
[3];
1701 texcoord
[2] /= texcoord
[3];
1704 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1706 store_vector4(inst
, machine
, color
);
1709 case OPCODE_TRUNC
: /* truncate toward zero */
1711 GLfloat a
[4], result
[4];
1712 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1713 result
[0] = (GLfloat
) (GLint
) a
[0];
1714 result
[1] = (GLfloat
) (GLint
) a
[1];
1715 result
[2] = (GLfloat
) (GLint
) a
[2];
1716 result
[3] = (GLfloat
) (GLint
) a
[3];
1717 store_vector4(inst
, machine
, result
);
1720 case OPCODE_UP2H
: /* unpack two 16-bit floats */
1722 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1727 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1728 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1729 store_vector4(inst
, machine
, result
);
1732 case OPCODE_UP2US
: /* unpack two GLushorts */
1734 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1739 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1740 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1741 store_vector4(inst
, machine
, result
);
1744 case OPCODE_UP4B
: /* unpack four GLbytes */
1746 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1748 result
[0] = (((raw
>> 0) & 0xff) - 128) / 127.0F
;
1749 result
[1] = (((raw
>> 8) & 0xff) - 128) / 127.0F
;
1750 result
[2] = (((raw
>> 16) & 0xff) - 128) / 127.0F
;
1751 result
[3] = (((raw
>> 24) & 0xff) - 128) / 127.0F
;
1752 store_vector4(inst
, machine
, result
);
1755 case OPCODE_UP4UB
: /* unpack four GLubytes */
1757 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1759 result
[0] = ((raw
>> 0) & 0xff) / 255.0F
;
1760 result
[1] = ((raw
>> 8) & 0xff) / 255.0F
;
1761 result
[2] = ((raw
>> 16) & 0xff) / 255.0F
;
1762 result
[3] = ((raw
>> 24) & 0xff) / 255.0F
;
1763 store_vector4(inst
, machine
, result
);
1766 case OPCODE_XOR
: /* bitwise XOR */
1768 GLuint a
[4], b
[4], result
[4];
1769 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1770 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
1771 result
[0] = a
[0] ^ b
[0];
1772 result
[1] = a
[1] ^ b
[1];
1773 result
[2] = a
[2] ^ b
[2];
1774 result
[3] = a
[3] ^ b
[3];
1775 store_vector4ui(inst
, machine
, result
);
1778 case OPCODE_XPD
: /* cross product */
1780 GLfloat a
[4], b
[4], result
[4];
1781 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1782 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1783 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1784 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1785 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1787 store_vector4(inst
, machine
, result
);
1789 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1790 result
[0], result
[1], result
[2], result
[3],
1791 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1795 case OPCODE_X2D
: /* 2-D matrix transform */
1797 GLfloat a
[4], b
[4], c
[4], result
[4];
1798 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1799 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1800 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1801 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1802 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1803 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1804 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1805 store_vector4(inst
, machine
, result
);
1810 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
1812 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1813 printf("%s%g, %g, %g, %g\n", (const char *) inst
->Data
,
1814 a
[0], a
[1], a
[2], a
[3]);
1817 printf("%s\n", (const char *) inst
->Data
);
1824 _mesa_problem(ctx
, "Bad opcode %d in _mesa_execute_program",
1826 return GL_TRUE
; /* return value doesn't matter */
1830 if (numExec
> maxExec
) {
1831 static GLboolean reported
= GL_FALSE
;
1833 _mesa_problem(ctx
, "Infinite loop detected in fragment program");