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"
42 #include "prog_execute.h"
43 #include "prog_instruction.h"
44 #include "prog_parameter.h"
45 #include "prog_print.h"
46 #include "prog_noise.h"
54 * Set x to positive or negative infinity.
56 #if defined(USE_IEEE) || defined(_WIN32)
57 #define SET_POS_INFINITY(x) ( *((GLuint *) (void *)&x) = 0x7F800000 )
58 #define SET_NEG_INFINITY(x) ( *((GLuint *) (void *)&x) = 0xFF800000 )
60 #define SET_POS_INFINITY(x) x = __MAXFLOAT
61 #define SET_NEG_INFINITY(x) x = -__MAXFLOAT
63 #define SET_POS_INFINITY(x) x = (GLfloat) HUGE_VAL
64 #define SET_NEG_INFINITY(x) x = (GLfloat) -HUGE_VAL
67 #define SET_FLOAT_BITS(x, bits) ((fi_type *) (void *) &(x))->i = bits
70 static const GLfloat ZeroVec
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
75 * Return a pointer to the 4-element float vector specified by the given
78 static INLINE
const GLfloat
*
79 get_src_register_pointer(const struct prog_src_register
*source
,
80 const struct gl_program_machine
*machine
)
82 const struct gl_program
*prog
= machine
->CurProgram
;
83 GLint reg
= source
->Index
;
85 if (source
->RelAddr
) {
86 /* add address register value to src index/offset */
87 reg
+= machine
->AddressReg
[0][0];
93 switch (source
->File
) {
94 case PROGRAM_TEMPORARY
:
95 if (reg
>= MAX_PROGRAM_TEMPS
)
97 return machine
->Temporaries
[reg
];
100 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
101 if (reg
>= VERT_ATTRIB_MAX
)
103 return machine
->VertAttribs
[reg
];
106 if (reg
>= FRAG_ATTRIB_MAX
)
108 return machine
->Attribs
[reg
][machine
->CurElement
];
112 if (reg
>= MAX_PROGRAM_OUTPUTS
)
114 return machine
->Outputs
[reg
];
116 case PROGRAM_LOCAL_PARAM
:
117 if (reg
>= MAX_PROGRAM_LOCAL_PARAMS
)
119 return machine
->CurProgram
->LocalParams
[reg
];
121 case PROGRAM_ENV_PARAM
:
122 if (reg
>= MAX_PROGRAM_ENV_PARAMS
)
124 return machine
->EnvParams
[reg
];
126 case PROGRAM_STATE_VAR
:
128 case PROGRAM_CONSTANT
:
130 case PROGRAM_UNIFORM
:
132 case PROGRAM_NAMED_PARAM
:
133 if (reg
>= (GLint
) prog
->Parameters
->NumParameters
)
135 return prog
->Parameters
->ParameterValues
[reg
];
139 "Invalid src register file %d in get_src_register_pointer()",
147 * Return a pointer to the 4-element float vector specified by the given
148 * destination register.
150 static INLINE GLfloat
*
151 get_dst_register_pointer(const struct prog_dst_register
*dest
,
152 struct gl_program_machine
*machine
)
154 static GLfloat dummyReg
[4];
155 GLint reg
= dest
->Index
;
158 /* add address register value to src index/offset */
159 reg
+= machine
->AddressReg
[0][0];
165 switch (dest
->File
) {
166 case PROGRAM_TEMPORARY
:
167 if (reg
>= MAX_PROGRAM_TEMPS
)
169 return machine
->Temporaries
[reg
];
172 if (reg
>= MAX_PROGRAM_OUTPUTS
)
174 return machine
->Outputs
[reg
];
176 case PROGRAM_WRITE_ONLY
:
181 "Invalid dest register file %d in get_dst_register_pointer()",
190 * Fetch a 4-element float vector from the given source register.
191 * Apply swizzling and negating as needed.
194 fetch_vector4(const struct prog_src_register
*source
,
195 const struct gl_program_machine
*machine
, GLfloat result
[4])
197 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
200 if (source
->Swizzle
== SWIZZLE_NOOP
) {
202 COPY_4V(result
, src
);
205 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
206 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
207 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
208 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
209 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
210 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
211 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
212 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
216 result
[0] = FABSF(result
[0]);
217 result
[1] = FABSF(result
[1]);
218 result
[2] = FABSF(result
[2]);
219 result
[3] = FABSF(result
[3]);
221 if (source
->Negate
) {
222 ASSERT(source
->Negate
== NEGATE_XYZW
);
223 result
[0] = -result
[0];
224 result
[1] = -result
[1];
225 result
[2] = -result
[2];
226 result
[3] = -result
[3];
230 assert(!IS_INF_OR_NAN(result
[0]));
231 assert(!IS_INF_OR_NAN(result
[0]));
232 assert(!IS_INF_OR_NAN(result
[0]));
233 assert(!IS_INF_OR_NAN(result
[0]));
239 * Fetch a 4-element uint vector from the given source register.
240 * Apply swizzling but not negation/abs.
243 fetch_vector4ui(const struct prog_src_register
*source
,
244 const struct gl_program_machine
*machine
, GLuint result
[4])
246 const GLuint
*src
= (GLuint
*) get_src_register_pointer(source
, machine
);
249 if (source
->Swizzle
== SWIZZLE_NOOP
) {
251 COPY_4V(result
, src
);
254 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
255 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
256 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
257 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
258 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
259 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
260 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
261 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
264 /* Note: no Negate or Abs here */
270 * Fetch the derivative with respect to X or Y for the given register.
271 * XXX this currently only works for fragment program input attribs.
274 fetch_vector4_deriv(GLcontext
* ctx
,
275 const struct prog_src_register
*source
,
276 const struct gl_program_machine
*machine
,
277 char xOrY
, GLfloat result
[4])
279 if (source
->File
== PROGRAM_INPUT
&&
280 source
->Index
< (GLint
) machine
->NumDeriv
) {
281 const GLint col
= machine
->CurElement
;
282 const GLfloat w
= machine
->Attribs
[FRAG_ATTRIB_WPOS
][col
][3];
283 const GLfloat invQ
= 1.0f
/ w
;
287 deriv
[0] = machine
->DerivX
[source
->Index
][0] * invQ
;
288 deriv
[1] = machine
->DerivX
[source
->Index
][1] * invQ
;
289 deriv
[2] = machine
->DerivX
[source
->Index
][2] * invQ
;
290 deriv
[3] = machine
->DerivX
[source
->Index
][3] * invQ
;
293 deriv
[0] = machine
->DerivY
[source
->Index
][0] * invQ
;
294 deriv
[1] = machine
->DerivY
[source
->Index
][1] * invQ
;
295 deriv
[2] = machine
->DerivY
[source
->Index
][2] * invQ
;
296 deriv
[3] = machine
->DerivY
[source
->Index
][3] * invQ
;
299 result
[0] = deriv
[GET_SWZ(source
->Swizzle
, 0)];
300 result
[1] = deriv
[GET_SWZ(source
->Swizzle
, 1)];
301 result
[2] = deriv
[GET_SWZ(source
->Swizzle
, 2)];
302 result
[3] = deriv
[GET_SWZ(source
->Swizzle
, 3)];
305 result
[0] = FABSF(result
[0]);
306 result
[1] = FABSF(result
[1]);
307 result
[2] = FABSF(result
[2]);
308 result
[3] = FABSF(result
[3]);
310 if (source
->Negate
) {
311 ASSERT(source
->Negate
== NEGATE_XYZW
);
312 result
[0] = -result
[0];
313 result
[1] = -result
[1];
314 result
[2] = -result
[2];
315 result
[3] = -result
[3];
319 ASSIGN_4V(result
, 0.0, 0.0, 0.0, 0.0);
325 * As above, but only return result[0] element.
328 fetch_vector1(const struct prog_src_register
*source
,
329 const struct gl_program_machine
*machine
, GLfloat result
[4])
331 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
334 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
337 result
[0] = FABSF(result
[0]);
339 if (source
->Negate
) {
340 result
[0] = -result
[0];
346 * Fetch texel from texture. Use partial derivatives when possible.
349 fetch_texel(GLcontext
*ctx
,
350 const struct gl_program_machine
*machine
,
351 const struct prog_instruction
*inst
,
352 const GLfloat texcoord
[4], GLfloat lodBias
,
355 const GLuint unit
= machine
->Samplers
[inst
->TexSrcUnit
];
357 /* Note: we only have the right derivatives for fragment input attribs.
359 if (machine
->NumDeriv
> 0 &&
360 inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
361 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
) {
362 /* simple texture fetch for which we should have derivatives */
363 GLuint attr
= inst
->SrcReg
[0].Index
;
364 machine
->FetchTexelDeriv(ctx
, texcoord
,
365 machine
->DerivX
[attr
],
366 machine
->DerivY
[attr
],
367 lodBias
, unit
, color
);
370 machine
->FetchTexelLod(ctx
, texcoord
, lodBias
, unit
, color
);
376 * Test value against zero and return GT, LT, EQ or UN if NaN.
379 generate_cc(float value
)
382 return COND_UN
; /* NaN */
392 * Test if the ccMaskRule is satisfied by the given condition code.
393 * Used to mask destination writes according to the current condition code.
395 static INLINE GLboolean
396 test_cc(GLuint condCode
, GLuint ccMaskRule
)
398 switch (ccMaskRule
) {
399 case COND_EQ
: return (condCode
== COND_EQ
);
400 case COND_NE
: return (condCode
!= COND_EQ
);
401 case COND_LT
: return (condCode
== COND_LT
);
402 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
403 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
404 case COND_GT
: return (condCode
== COND_GT
);
405 case COND_TR
: return GL_TRUE
;
406 case COND_FL
: return GL_FALSE
;
407 default: return GL_TRUE
;
413 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
414 * or GL_FALSE to indicate result.
416 static INLINE GLboolean
417 eval_condition(const struct gl_program_machine
*machine
,
418 const struct prog_instruction
*inst
)
420 const GLuint swizzle
= inst
->DstReg
.CondSwizzle
;
421 const GLuint condMask
= inst
->DstReg
.CondMask
;
422 if (test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 0)], condMask
) ||
423 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 1)], condMask
) ||
424 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 2)], condMask
) ||
425 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 3)], condMask
)) {
436 * Store 4 floats into a register. Observe the instructions saturate and
437 * set-condition-code flags.
440 store_vector4(const struct prog_instruction
*inst
,
441 struct gl_program_machine
*machine
, const GLfloat value
[4])
443 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
444 const GLboolean clamp
= inst
->SaturateMode
== SATURATE_ZERO_ONE
;
445 GLuint writeMask
= dstReg
->WriteMask
;
446 GLfloat clampedValue
[4];
447 GLfloat
*dst
= get_dst_register_pointer(dstReg
, machine
);
450 if (value
[0] > 1.0e10
||
451 IS_INF_OR_NAN(value
[0]) ||
452 IS_INF_OR_NAN(value
[1]) ||
453 IS_INF_OR_NAN(value
[2]) || IS_INF_OR_NAN(value
[3]))
454 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
458 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
459 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
460 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
461 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
462 value
= clampedValue
;
465 if (dstReg
->CondMask
!= COND_TR
) {
466 /* condition codes may turn off some writes */
467 if (writeMask
& WRITEMASK_X
) {
468 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
470 writeMask
&= ~WRITEMASK_X
;
472 if (writeMask
& WRITEMASK_Y
) {
473 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
475 writeMask
&= ~WRITEMASK_Y
;
477 if (writeMask
& WRITEMASK_Z
) {
478 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
480 writeMask
&= ~WRITEMASK_Z
;
482 if (writeMask
& WRITEMASK_W
) {
483 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
485 writeMask
&= ~WRITEMASK_W
;
490 assert(!IS_INF_OR_NAN(value
[0]));
491 assert(!IS_INF_OR_NAN(value
[0]));
492 assert(!IS_INF_OR_NAN(value
[0]));
493 assert(!IS_INF_OR_NAN(value
[0]));
496 if (writeMask
& WRITEMASK_X
)
498 if (writeMask
& WRITEMASK_Y
)
500 if (writeMask
& WRITEMASK_Z
)
502 if (writeMask
& WRITEMASK_W
)
505 if (inst
->CondUpdate
) {
506 if (writeMask
& WRITEMASK_X
)
507 machine
->CondCodes
[0] = generate_cc(value
[0]);
508 if (writeMask
& WRITEMASK_Y
)
509 machine
->CondCodes
[1] = generate_cc(value
[1]);
510 if (writeMask
& WRITEMASK_Z
)
511 machine
->CondCodes
[2] = generate_cc(value
[2]);
512 if (writeMask
& WRITEMASK_W
)
513 machine
->CondCodes
[3] = generate_cc(value
[3]);
515 printf("CondCodes=(%s,%s,%s,%s) for:\n",
516 _mesa_condcode_string(machine
->CondCodes
[0]),
517 _mesa_condcode_string(machine
->CondCodes
[1]),
518 _mesa_condcode_string(machine
->CondCodes
[2]),
519 _mesa_condcode_string(machine
->CondCodes
[3]));
526 * Store 4 uints into a register. Observe the set-condition-code flags.
529 store_vector4ui(const struct prog_instruction
*inst
,
530 struct gl_program_machine
*machine
, const GLuint value
[4])
532 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
533 GLuint writeMask
= dstReg
->WriteMask
;
534 GLuint
*dst
= (GLuint
*) get_dst_register_pointer(dstReg
, machine
);
536 if (dstReg
->CondMask
!= COND_TR
) {
537 /* condition codes may turn off some writes */
538 if (writeMask
& WRITEMASK_X
) {
539 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
541 writeMask
&= ~WRITEMASK_X
;
543 if (writeMask
& WRITEMASK_Y
) {
544 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
546 writeMask
&= ~WRITEMASK_Y
;
548 if (writeMask
& WRITEMASK_Z
) {
549 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
551 writeMask
&= ~WRITEMASK_Z
;
553 if (writeMask
& WRITEMASK_W
) {
554 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
556 writeMask
&= ~WRITEMASK_W
;
560 if (writeMask
& WRITEMASK_X
)
562 if (writeMask
& WRITEMASK_Y
)
564 if (writeMask
& WRITEMASK_Z
)
566 if (writeMask
& WRITEMASK_W
)
569 if (inst
->CondUpdate
) {
570 if (writeMask
& WRITEMASK_X
)
571 machine
->CondCodes
[0] = generate_cc(value
[0]);
572 if (writeMask
& WRITEMASK_Y
)
573 machine
->CondCodes
[1] = generate_cc(value
[1]);
574 if (writeMask
& WRITEMASK_Z
)
575 machine
->CondCodes
[2] = generate_cc(value
[2]);
576 if (writeMask
& WRITEMASK_W
)
577 machine
->CondCodes
[3] = generate_cc(value
[3]);
579 printf("CondCodes=(%s,%s,%s,%s) for:\n",
580 _mesa_condcode_string(machine
->CondCodes
[0]),
581 _mesa_condcode_string(machine
->CondCodes
[1]),
582 _mesa_condcode_string(machine
->CondCodes
[2]),
583 _mesa_condcode_string(machine
->CondCodes
[3]));
591 * Execute the given vertex/fragment program.
593 * \param ctx rendering context
594 * \param program the program to execute
595 * \param machine machine state (must be initialized)
596 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
599 _mesa_execute_program(GLcontext
* ctx
,
600 const struct gl_program
*program
,
601 struct gl_program_machine
*machine
)
603 const GLuint numInst
= program
->NumInstructions
;
604 const GLuint maxExec
= 10000;
605 GLuint pc
, numExec
= 0;
607 machine
->CurProgram
= program
;
610 printf("execute program %u --------------------\n", program
->Id
);
613 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
614 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
617 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
620 for (pc
= 0; pc
< numInst
; pc
++) {
621 const struct prog_instruction
*inst
= program
->Instructions
+ pc
;
624 _mesa_print_instruction(inst
);
627 switch (inst
->Opcode
) {
630 GLfloat a
[4], result
[4];
631 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
632 result
[0] = FABSF(a
[0]);
633 result
[1] = FABSF(a
[1]);
634 result
[2] = FABSF(a
[2]);
635 result
[3] = FABSF(a
[3]);
636 store_vector4(inst
, machine
, result
);
641 GLfloat a
[4], b
[4], result
[4];
642 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
643 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
644 result
[0] = a
[0] + b
[0];
645 result
[1] = a
[1] + b
[1];
646 result
[2] = a
[2] + b
[2];
647 result
[3] = a
[3] + b
[3];
648 store_vector4(inst
, machine
, result
);
650 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
651 result
[0], result
[1], result
[2], result
[3],
652 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
656 case OPCODE_AND
: /* bitwise AND */
658 GLuint a
[4], b
[4], result
[4];
659 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
660 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
661 result
[0] = a
[0] & b
[0];
662 result
[1] = a
[1] & b
[1];
663 result
[2] = a
[2] & b
[2];
664 result
[3] = a
[3] & b
[3];
665 store_vector4ui(inst
, machine
, result
);
671 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
672 machine
->AddressReg
[0][0] = IFLOOR(t
[0]);
679 /* subtract 1 here since pc is incremented by for(pc) loop */
680 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
682 case OPCODE_BGNSUB
: /* begin subroutine */
684 case OPCODE_ENDSUB
: /* end subroutine */
686 case OPCODE_BRA
: /* branch (conditional) */
688 case OPCODE_BRK
: /* break out of loop (conditional) */
690 case OPCODE_CONT
: /* continue loop (conditional) */
691 if (eval_condition(machine
, inst
)) {
693 /* Subtract 1 here since we'll do pc++ at end of for-loop */
694 pc
= inst
->BranchTarget
- 1;
697 case OPCODE_CAL
: /* Call subroutine (conditional) */
698 if (eval_condition(machine
, inst
)) {
699 /* call the subroutine */
700 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
701 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
703 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1; /* next inst */
704 /* Subtract 1 here since we'll do pc++ at end of for-loop */
705 pc
= inst
->BranchTarget
- 1;
710 GLfloat a
[4], b
[4], c
[4], result
[4];
711 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
712 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
713 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
714 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
715 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
716 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
717 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
718 store_vector4(inst
, machine
, result
);
723 GLfloat a
[4], result
[4];
724 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
725 result
[0] = result
[1] = result
[2] = result
[3]
726 = (GLfloat
) _mesa_cos(a
[0]);
727 store_vector4(inst
, machine
, result
);
730 case OPCODE_DDX
: /* Partial derivative with respect to X */
733 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
735 store_vector4(inst
, machine
, result
);
738 case OPCODE_DDY
: /* Partial derivative with respect to Y */
741 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
743 store_vector4(inst
, machine
, result
);
748 GLfloat a
[4], b
[4], result
[4];
749 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
750 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
751 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
);
752 store_vector4(inst
, machine
, result
);
754 printf("DP2 %g = (%g %g) . (%g %g)\n",
755 result
[0], a
[0], a
[1], b
[0], b
[1]);
761 GLfloat a
[4], b
[4], c
, result
[4];
762 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
763 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
764 fetch_vector1(&inst
->SrcReg
[1], machine
, &c
);
765 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
) + c
;
766 store_vector4(inst
, machine
, result
);
768 printf("DP2A %g = (%g %g) . (%g %g) + %g\n",
769 result
[0], a
[0], a
[1], b
[0], b
[1], c
);
775 GLfloat a
[4], b
[4], result
[4];
776 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
777 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
778 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
779 store_vector4(inst
, machine
, result
);
781 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
782 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
788 GLfloat a
[4], b
[4], result
[4];
789 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
790 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
791 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
, b
);
792 store_vector4(inst
, machine
, result
);
794 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
795 result
[0], a
[0], a
[1], a
[2], a
[3],
796 b
[0], b
[1], b
[2], b
[3]);
802 GLfloat a
[4], b
[4], result
[4];
803 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
804 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
805 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
) + b
[3];
806 store_vector4(inst
, machine
, result
);
809 case OPCODE_DST
: /* Distance vector */
811 GLfloat a
[4], b
[4], result
[4];
812 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
813 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
815 result
[1] = a
[1] * b
[1];
818 store_vector4(inst
, machine
, result
);
823 GLfloat t
[4], q
[4], floor_t0
;
824 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
825 floor_t0
= FLOORF(t
[0]);
826 if (floor_t0
> FLT_MAX_EXP
) {
827 SET_POS_INFINITY(q
[0]);
828 SET_POS_INFINITY(q
[2]);
830 else if (floor_t0
< FLT_MIN_EXP
) {
835 q
[0] = LDEXPF(1.0, (int) floor_t0
);
836 /* Note: GL_NV_vertex_program expects
837 * result.z = result.x * APPX(result.y)
838 * We do what the ARB extension says.
840 q
[2] = (GLfloat
) _mesa_pow(2.0, t
[0]);
842 q
[1] = t
[0] - floor_t0
;
844 store_vector4( inst
, machine
, q
);
847 case OPCODE_EX2
: /* Exponential base 2 */
849 GLfloat a
[4], result
[4], val
;
850 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
851 val
= (GLfloat
) _mesa_pow(2.0, a
[0]);
853 if (IS_INF_OR_NAN(val))
856 result
[0] = result
[1] = result
[2] = result
[3] = val
;
857 store_vector4(inst
, machine
, result
);
862 GLfloat a
[4], result
[4];
863 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
864 result
[0] = FLOORF(a
[0]);
865 result
[1] = FLOORF(a
[1]);
866 result
[2] = FLOORF(a
[2]);
867 result
[3] = FLOORF(a
[3]);
868 store_vector4(inst
, machine
, result
);
873 GLfloat a
[4], result
[4];
874 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
875 result
[0] = a
[0] - FLOORF(a
[0]);
876 result
[1] = a
[1] - FLOORF(a
[1]);
877 result
[2] = a
[2] - FLOORF(a
[2]);
878 result
[3] = a
[3] - FLOORF(a
[3]);
879 store_vector4(inst
, machine
, result
);
886 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
888 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
889 cond
= (a
[0] != 0.0);
892 cond
= eval_condition(machine
, inst
);
895 printf("IF: %d\n", cond
);
899 /* do if-clause (just continue execution) */
902 /* go to the instruction after ELSE or ENDIF */
903 assert(inst
->BranchTarget
>= 0);
904 pc
= inst
->BranchTarget
- 1;
910 assert(inst
->BranchTarget
>= 0);
911 pc
= inst
->BranchTarget
- 1;
916 case OPCODE_KIL_NV
: /* NV_f_p only (conditional) */
917 if (eval_condition(machine
, inst
)) {
921 case OPCODE_KIL
: /* ARB_f_p only */
924 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
925 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
930 case OPCODE_LG2
: /* log base 2 */
932 GLfloat a
[4], result
[4], val
;
933 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
934 /* The fast LOG2 macro doesn't meet the precision requirements.
940 val
= log(a
[0]) * 1.442695F
;
942 result
[0] = result
[1] = result
[2] = result
[3] = val
;
943 store_vector4(inst
, machine
, result
);
948 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
949 GLfloat a
[4], result
[4];
950 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
951 a
[0] = MAX2(a
[0], 0.0F
);
952 a
[1] = MAX2(a
[1], 0.0F
);
953 /* XXX ARB version clamps a[3], NV version doesn't */
954 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
957 /* XXX we could probably just use pow() here */
959 if (a
[1] == 0.0 && a
[3] == 0.0)
962 result
[2] = (GLfloat
) _mesa_pow(a
[1], a
[3]);
968 store_vector4(inst
, machine
, result
);
970 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
971 result
[0], result
[1], result
[2], result
[3],
972 a
[0], a
[1], a
[2], a
[3]);
978 GLfloat t
[4], q
[4], abs_t0
;
979 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
980 abs_t0
= FABSF(t
[0]);
981 if (abs_t0
!= 0.0F
) {
982 /* Since we really can't handle infinite values on VMS
983 * like other OSes we'll use __MAXFLOAT to represent
984 * infinity. This may need some tweaking.
987 if (abs_t0
== __MAXFLOAT
)
989 if (IS_INF_OR_NAN(abs_t0
))
992 SET_POS_INFINITY(q
[0]);
994 SET_POS_INFINITY(q
[2]);
998 GLfloat mantissa
= FREXPF(t
[0], &exponent
);
999 q
[0] = (GLfloat
) (exponent
- 1);
1000 q
[1] = (GLfloat
) (2.0 * mantissa
); /* map [.5, 1) -> [1, 2) */
1002 /* The fast LOG2 macro doesn't meet the precision
1005 q
[2] = (log(t
[0]) * 1.442695F
);
1009 SET_NEG_INFINITY(q
[0]);
1011 SET_NEG_INFINITY(q
[2]);
1014 store_vector4(inst
, machine
, q
);
1019 GLfloat a
[4], b
[4], c
[4], result
[4];
1020 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1021 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1022 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1023 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
1024 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
1025 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
1026 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
1027 store_vector4(inst
, machine
, result
);
1029 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
1030 "(%g %g %g %g), (%g %g %g %g)\n",
1031 result
[0], result
[1], result
[2], result
[3],
1032 a
[0], a
[1], a
[2], a
[3],
1033 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1039 GLfloat a
[4], b
[4], c
[4], result
[4];
1040 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1041 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1042 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1043 result
[0] = a
[0] * b
[0] + c
[0];
1044 result
[1] = a
[1] * b
[1] + c
[1];
1045 result
[2] = a
[2] * b
[2] + c
[2];
1046 result
[3] = a
[3] * b
[3] + c
[3];
1047 store_vector4(inst
, machine
, result
);
1049 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
1050 "(%g %g %g %g) + (%g %g %g %g)\n",
1051 result
[0], result
[1], result
[2], result
[3],
1052 a
[0], a
[1], a
[2], a
[3],
1053 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1059 GLfloat a
[4], b
[4], result
[4];
1060 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1061 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1062 result
[0] = MAX2(a
[0], b
[0]);
1063 result
[1] = MAX2(a
[1], b
[1]);
1064 result
[2] = MAX2(a
[2], b
[2]);
1065 result
[3] = MAX2(a
[3], b
[3]);
1066 store_vector4(inst
, machine
, result
);
1068 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
1069 result
[0], result
[1], result
[2], result
[3],
1070 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1076 GLfloat a
[4], b
[4], result
[4];
1077 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1078 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1079 result
[0] = MIN2(a
[0], b
[0]);
1080 result
[1] = MIN2(a
[1], b
[1]);
1081 result
[2] = MIN2(a
[2], b
[2]);
1082 result
[3] = MIN2(a
[3], b
[3]);
1083 store_vector4(inst
, machine
, result
);
1089 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
1090 store_vector4(inst
, machine
, result
);
1092 printf("MOV (%g %g %g %g)\n",
1093 result
[0], result
[1], result
[2], result
[3]);
1099 GLfloat a
[4], b
[4], result
[4];
1100 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1101 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1102 result
[0] = a
[0] * b
[0];
1103 result
[1] = a
[1] * b
[1];
1104 result
[2] = a
[2] * b
[2];
1105 result
[3] = a
[3] * b
[3];
1106 store_vector4(inst
, machine
, result
);
1108 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1109 result
[0], result
[1], result
[2], result
[3],
1110 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1116 GLfloat a
[4], result
[4];
1117 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1121 result
[3] = _mesa_noise1(a
[0]);
1122 store_vector4(inst
, machine
, result
);
1127 GLfloat a
[4], result
[4];
1128 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1131 result
[2] = result
[3] = _mesa_noise2(a
[0], a
[1]);
1132 store_vector4(inst
, machine
, result
);
1137 GLfloat a
[4], result
[4];
1138 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1142 result
[3] = _mesa_noise3(a
[0], a
[1], a
[2]);
1143 store_vector4(inst
, machine
, result
);
1148 GLfloat a
[4], result
[4];
1149 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1153 result
[3] = _mesa_noise4(a
[0], a
[1], a
[2], a
[3]);
1154 store_vector4(inst
, machine
, result
);
1159 case OPCODE_NOT
: /* bitwise NOT */
1161 GLuint a
[4], result
[4];
1162 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1167 store_vector4ui(inst
, machine
, result
);
1170 case OPCODE_NRM3
: /* 3-component normalization */
1172 GLfloat a
[4], result
[4];
1174 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1175 tmp
= a
[0] * a
[0] + a
[1] * a
[1] + a
[2] * a
[2];
1177 tmp
= INV_SQRTF(tmp
);
1178 result
[0] = tmp
* a
[0];
1179 result
[1] = tmp
* a
[1];
1180 result
[2] = tmp
* a
[2];
1181 result
[3] = 0.0; /* undefined, but prevent valgrind warnings */
1182 store_vector4(inst
, machine
, result
);
1185 case OPCODE_NRM4
: /* 4-component normalization */
1187 GLfloat a
[4], result
[4];
1189 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1190 tmp
= a
[0] * a
[0] + a
[1] * a
[1] + a
[2] * a
[2] + a
[3] * a
[3];
1192 tmp
= INV_SQRTF(tmp
);
1193 result
[0] = tmp
* a
[0];
1194 result
[1] = tmp
* a
[1];
1195 result
[2] = tmp
* a
[2];
1196 result
[3] = tmp
* a
[3];
1197 store_vector4(inst
, machine
, result
);
1200 case OPCODE_OR
: /* bitwise OR */
1202 GLuint a
[4], b
[4], result
[4];
1203 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1204 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
1205 result
[0] = a
[0] | b
[0];
1206 result
[1] = a
[1] | b
[1];
1207 result
[2] = a
[2] | b
[2];
1208 result
[3] = a
[3] | b
[3];
1209 store_vector4ui(inst
, machine
, result
);
1212 case OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
1217 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1218 hx
= _mesa_float_to_half(a
[0]);
1219 hy
= _mesa_float_to_half(a
[1]);
1223 result
[3] = hx
| (hy
<< 16);
1224 store_vector4ui(inst
, machine
, result
);
1227 case OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
1230 GLuint result
[4], usx
, usy
;
1231 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1232 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1233 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1234 usx
= IROUND(a
[0] * 65535.0F
);
1235 usy
= IROUND(a
[1] * 65535.0F
);
1239 result
[3] = usx
| (usy
<< 16);
1240 store_vector4ui(inst
, machine
, result
);
1243 case OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
1246 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1247 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1248 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
1249 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
1250 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
1251 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
1252 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
1253 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
1254 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
1255 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
1259 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1260 store_vector4ui(inst
, machine
, result
);
1263 case OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
1266 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1267 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1268 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1269 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1270 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
1271 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
1272 ubx
= IROUND(255.0F
* a
[0]);
1273 uby
= IROUND(255.0F
* a
[1]);
1274 ubz
= IROUND(255.0F
* a
[2]);
1275 ubw
= IROUND(255.0F
* a
[3]);
1279 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1280 store_vector4ui(inst
, machine
, result
);
1285 GLfloat a
[4], b
[4], result
[4];
1286 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1287 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
1288 result
[0] = result
[1] = result
[2] = result
[3]
1289 = (GLfloat
) _mesa_pow(a
[0], b
[0]);
1290 store_vector4(inst
, machine
, result
);
1295 GLfloat a
[4], result
[4];
1296 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1300 else if (IS_INF_OR_NAN(a
[0]))
1301 printf("RCP(inf)\n");
1303 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
1304 store_vector4(inst
, machine
, result
);
1307 case OPCODE_RET
: /* return from subroutine (conditional) */
1308 if (eval_condition(machine
, inst
)) {
1309 if (machine
->StackDepth
== 0) {
1310 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
1312 /* subtract one because of pc++ in the for loop */
1313 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
1316 case OPCODE_RFL
: /* reflection vector */
1318 GLfloat axis
[4], dir
[4], result
[4], tmpX
, tmpW
;
1319 fetch_vector4(&inst
->SrcReg
[0], machine
, axis
);
1320 fetch_vector4(&inst
->SrcReg
[1], machine
, dir
);
1321 tmpW
= DOT3(axis
, axis
);
1322 tmpX
= (2.0F
* DOT3(axis
, dir
)) / tmpW
;
1323 result
[0] = tmpX
* axis
[0] - dir
[0];
1324 result
[1] = tmpX
* axis
[1] - dir
[1];
1325 result
[2] = tmpX
* axis
[2] - dir
[2];
1326 /* result[3] is never written! XXX enforce in parser! */
1327 store_vector4(inst
, machine
, result
);
1330 case OPCODE_RSQ
: /* 1 / sqrt() */
1332 GLfloat a
[4], result
[4];
1333 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1335 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1336 store_vector4(inst
, machine
, result
);
1338 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
1342 case OPCODE_SCS
: /* sine and cos */
1344 GLfloat a
[4], result
[4];
1345 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1346 result
[0] = (GLfloat
) _mesa_cos(a
[0]);
1347 result
[1] = (GLfloat
) _mesa_sin(a
[0]);
1348 result
[2] = 0.0; /* undefined! */
1349 result
[3] = 0.0; /* undefined! */
1350 store_vector4(inst
, machine
, result
);
1353 case OPCODE_SEQ
: /* set on equal */
1355 GLfloat a
[4], b
[4], result
[4];
1356 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1357 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1358 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1359 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1360 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1361 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1362 store_vector4(inst
, machine
, result
);
1364 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1365 result
[0], result
[1], result
[2], result
[3],
1366 a
[0], a
[1], a
[2], a
[3],
1367 b
[0], b
[1], b
[2], b
[3]);
1371 case OPCODE_SFL
: /* set false, operands ignored */
1373 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1374 store_vector4(inst
, machine
, result
);
1377 case OPCODE_SGE
: /* set on greater or equal */
1379 GLfloat a
[4], b
[4], result
[4];
1380 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1381 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1382 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1383 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1384 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1385 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1386 store_vector4(inst
, machine
, result
);
1388 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1389 result
[0], result
[1], result
[2], result
[3],
1390 a
[0], a
[1], a
[2], a
[3],
1391 b
[0], b
[1], b
[2], b
[3]);
1395 case OPCODE_SGT
: /* set on greater */
1397 GLfloat a
[4], b
[4], result
[4];
1398 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1399 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1400 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1401 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1402 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1403 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1404 store_vector4(inst
, machine
, result
);
1406 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1407 result
[0], result
[1], result
[2], result
[3],
1408 a
[0], a
[1], a
[2], a
[3],
1409 b
[0], b
[1], b
[2], b
[3]);
1415 GLfloat a
[4], result
[4];
1416 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1417 result
[0] = result
[1] = result
[2] = result
[3]
1418 = (GLfloat
) _mesa_sin(a
[0]);
1419 store_vector4(inst
, machine
, result
);
1422 case OPCODE_SLE
: /* set on less or equal */
1424 GLfloat a
[4], b
[4], result
[4];
1425 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1426 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1427 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1428 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1429 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1430 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1431 store_vector4(inst
, machine
, result
);
1433 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1434 result
[0], result
[1], result
[2], result
[3],
1435 a
[0], a
[1], a
[2], a
[3],
1436 b
[0], b
[1], b
[2], b
[3]);
1440 case OPCODE_SLT
: /* set on less */
1442 GLfloat a
[4], b
[4], result
[4];
1443 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1444 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1445 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1446 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1447 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1448 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1449 store_vector4(inst
, machine
, result
);
1451 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1452 result
[0], result
[1], result
[2], result
[3],
1453 a
[0], a
[1], a
[2], a
[3],
1454 b
[0], b
[1], b
[2], b
[3]);
1458 case OPCODE_SNE
: /* set on not equal */
1460 GLfloat a
[4], b
[4], result
[4];
1461 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1462 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1463 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1464 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1465 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1466 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1467 store_vector4(inst
, machine
, result
);
1469 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1470 result
[0], result
[1], result
[2], result
[3],
1471 a
[0], a
[1], a
[2], a
[3],
1472 b
[0], b
[1], b
[2], b
[3]);
1476 case OPCODE_SSG
: /* set sign (-1, 0 or +1) */
1478 GLfloat a
[4], result
[4];
1479 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1480 result
[0] = (GLfloat
) ((a
[0] > 0.0F
) - (a
[0] < 0.0F
));
1481 result
[1] = (GLfloat
) ((a
[1] > 0.0F
) - (a
[1] < 0.0F
));
1482 result
[2] = (GLfloat
) ((a
[2] > 0.0F
) - (a
[2] < 0.0F
));
1483 result
[3] = (GLfloat
) ((a
[3] > 0.0F
) - (a
[3] < 0.0F
));
1484 store_vector4(inst
, machine
, result
);
1487 case OPCODE_STR
: /* set true, operands ignored */
1489 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1490 store_vector4(inst
, machine
, result
);
1495 GLfloat a
[4], b
[4], result
[4];
1496 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1497 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1498 result
[0] = a
[0] - b
[0];
1499 result
[1] = a
[1] - b
[1];
1500 result
[2] = a
[2] - b
[2];
1501 result
[3] = a
[3] - b
[3];
1502 store_vector4(inst
, machine
, result
);
1504 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1505 result
[0], result
[1], result
[2], result
[3],
1506 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1510 case OPCODE_SWZ
: /* extended swizzle */
1512 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1513 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
1516 for (i
= 0; i
< 4; i
++) {
1517 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1518 if (swz
== SWIZZLE_ZERO
)
1520 else if (swz
== SWIZZLE_ONE
)
1525 result
[i
] = src
[swz
];
1527 if (source
->Negate
& (1 << i
))
1528 result
[i
] = -result
[i
];
1530 store_vector4(inst
, machine
, result
);
1533 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1534 /* Simple texel lookup */
1536 GLfloat texcoord
[4], color
[4];
1537 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1539 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1542 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1543 color
[0], color
[1], color
[2], color
[3],
1545 texcoord
[0], texcoord
[1], texcoord
[2], texcoord
[3]);
1547 store_vector4(inst
, machine
, color
);
1550 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1551 /* Texel lookup with LOD bias */
1553 const GLuint unit
= machine
->Samplers
[inst
->TexSrcUnit
];
1554 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
1555 GLfloat texcoord
[4], color
[4], lodBias
;
1557 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1559 /* texcoord[3] is the bias to add to lambda */
1560 lodBias
= texUnit
->LodBias
+ texcoord
[3];
1561 if (texUnit
->_Current
) {
1562 lodBias
+= texUnit
->_Current
->LodBias
;
1565 fetch_texel(ctx
, machine
, inst
, texcoord
, lodBias
, color
);
1567 store_vector4(inst
, machine
, color
);
1570 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1571 /* Texture lookup w/ partial derivatives for LOD */
1573 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1574 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1575 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1576 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1577 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1579 inst
->TexSrcUnit
, color
);
1580 store_vector4(inst
, machine
, color
);
1583 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1584 /* Texture lookup w/ projective divide */
1586 GLfloat texcoord
[4], color
[4];
1588 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1589 /* Not so sure about this test - if texcoord[3] is
1590 * zero, we'd probably be fine except for an ASSERT in
1591 * IROUND_POS() which gets triggered by the inf values created.
1593 if (texcoord
[3] != 0.0) {
1594 texcoord
[0] /= texcoord
[3];
1595 texcoord
[1] /= texcoord
[3];
1596 texcoord
[2] /= texcoord
[3];
1599 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1601 store_vector4(inst
, machine
, color
);
1604 case OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1605 /* Texture lookup w/ projective divide, as above, but do not
1606 * do the divide by w if sampling from a cube map.
1609 GLfloat texcoord
[4], color
[4];
1611 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1612 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1613 texcoord
[3] != 0.0) {
1614 texcoord
[0] /= texcoord
[3];
1615 texcoord
[1] /= texcoord
[3];
1616 texcoord
[2] /= texcoord
[3];
1619 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1621 store_vector4(inst
, machine
, color
);
1624 case OPCODE_TRUNC
: /* truncate toward zero */
1626 GLfloat a
[4], result
[4];
1627 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1628 result
[0] = (GLfloat
) (GLint
) a
[0];
1629 result
[1] = (GLfloat
) (GLint
) a
[1];
1630 result
[2] = (GLfloat
) (GLint
) a
[2];
1631 result
[3] = (GLfloat
) (GLint
) a
[3];
1632 store_vector4(inst
, machine
, result
);
1635 case OPCODE_UP2H
: /* unpack two 16-bit floats */
1637 GLfloat a
[4], result
[4];
1638 const GLuint
*rawBits
= (const GLuint
*) a
;
1640 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1641 hx
= rawBits
[0] & 0xffff;
1642 hy
= rawBits
[0] >> 16;
1643 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1644 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1645 store_vector4(inst
, machine
, result
);
1648 case OPCODE_UP2US
: /* unpack two GLushorts */
1650 GLfloat a
[4], result
[4];
1651 const GLuint
*rawBits
= (const GLuint
*) a
;
1653 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1654 usx
= rawBits
[0] & 0xffff;
1655 usy
= rawBits
[0] >> 16;
1656 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1657 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1658 store_vector4(inst
, machine
, result
);
1661 case OPCODE_UP4B
: /* unpack four GLbytes */
1663 GLfloat a
[4], result
[4];
1664 const GLuint
*rawBits
= (const GLuint
*) a
;
1665 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1666 result
[0] = (((rawBits
[0] >> 0) & 0xff) - 128) / 127.0F
;
1667 result
[1] = (((rawBits
[0] >> 8) & 0xff) - 128) / 127.0F
;
1668 result
[2] = (((rawBits
[0] >> 16) & 0xff) - 128) / 127.0F
;
1669 result
[3] = (((rawBits
[0] >> 24) & 0xff) - 128) / 127.0F
;
1670 store_vector4(inst
, machine
, result
);
1673 case OPCODE_UP4UB
: /* unpack four GLubytes */
1675 GLfloat a
[4], result
[4];
1676 const GLuint
*rawBits
= (const GLuint
*) a
;
1677 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1678 result
[0] = ((rawBits
[0] >> 0) & 0xff) / 255.0F
;
1679 result
[1] = ((rawBits
[0] >> 8) & 0xff) / 255.0F
;
1680 result
[2] = ((rawBits
[0] >> 16) & 0xff) / 255.0F
;
1681 result
[3] = ((rawBits
[0] >> 24) & 0xff) / 255.0F
;
1682 store_vector4(inst
, machine
, result
);
1685 case OPCODE_XOR
: /* bitwise XOR */
1687 GLuint a
[4], b
[4], result
[4];
1688 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1689 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
1690 result
[0] = a
[0] ^ b
[0];
1691 result
[1] = a
[1] ^ b
[1];
1692 result
[2] = a
[2] ^ b
[2];
1693 result
[3] = a
[3] ^ b
[3];
1694 store_vector4ui(inst
, machine
, result
);
1697 case OPCODE_XPD
: /* cross product */
1699 GLfloat a
[4], b
[4], result
[4];
1700 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1701 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1702 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1703 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1704 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1706 store_vector4(inst
, machine
, result
);
1708 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1709 result
[0], result
[1], result
[2], result
[3],
1710 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1714 case OPCODE_X2D
: /* 2-D matrix transform */
1716 GLfloat a
[4], b
[4], c
[4], result
[4];
1717 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1718 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1719 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1720 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1721 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1722 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1723 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1724 store_vector4(inst
, machine
, result
);
1729 if (inst
->SrcReg
[0].File
!= -1) {
1731 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1732 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst
->Data
,
1733 a
[0], a
[1], a
[2], a
[3]);
1736 _mesa_printf("%s\n", (const char *) inst
->Data
);
1743 _mesa_problem(ctx
, "Bad opcode %d in _mesa_execute_program",
1745 return GL_TRUE
; /* return value doesn't matter */
1749 if (numExec
> maxExec
) {
1750 _mesa_problem(ctx
, "Infinite loop detected in fragment program");