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)];
215 if (source
->NegateBase
) {
216 result
[0] = -result
[0];
217 result
[1] = -result
[1];
218 result
[2] = -result
[2];
219 result
[3] = -result
[3];
222 result
[0] = FABSF(result
[0]);
223 result
[1] = FABSF(result
[1]);
224 result
[2] = FABSF(result
[2]);
225 result
[3] = FABSF(result
[3]);
227 if (source
->NegateAbs
) {
228 result
[0] = -result
[0];
229 result
[1] = -result
[1];
230 result
[2] = -result
[2];
231 result
[3] = -result
[3];
237 * Fetch a 4-element uint vector from the given source register.
238 * Apply swizzling but not negation/abs.
241 fetch_vector4ui(const struct prog_src_register
*source
,
242 const struct gl_program_machine
*machine
, GLuint result
[4])
244 const GLuint
*src
= (GLuint
*) get_src_register_pointer(source
, machine
);
247 if (source
->Swizzle
== SWIZZLE_NOOP
) {
249 COPY_4V(result
, src
);
252 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
253 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
254 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
255 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
256 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
257 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
258 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
259 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
262 /* Note: no NegateBase, Abs, NegateAbs here */
268 * Fetch the derivative with respect to X or Y for the given register.
269 * XXX this currently only works for fragment program input attribs.
272 fetch_vector4_deriv(GLcontext
* ctx
,
273 const struct prog_src_register
*source
,
274 const struct gl_program_machine
*machine
,
275 char xOrY
, GLfloat result
[4])
277 if (source
->File
== PROGRAM_INPUT
&&
278 source
->Index
< (GLint
) machine
->NumDeriv
) {
279 const GLint col
= machine
->CurElement
;
280 const GLfloat w
= machine
->Attribs
[FRAG_ATTRIB_WPOS
][col
][3];
281 const GLfloat invQ
= 1.0f
/ w
;
285 deriv
[0] = machine
->DerivX
[source
->Index
][0] * invQ
;
286 deriv
[1] = machine
->DerivX
[source
->Index
][1] * invQ
;
287 deriv
[2] = machine
->DerivX
[source
->Index
][2] * invQ
;
288 deriv
[3] = machine
->DerivX
[source
->Index
][3] * invQ
;
291 deriv
[0] = machine
->DerivY
[source
->Index
][0] * invQ
;
292 deriv
[1] = machine
->DerivY
[source
->Index
][1] * invQ
;
293 deriv
[2] = machine
->DerivY
[source
->Index
][2] * invQ
;
294 deriv
[3] = machine
->DerivY
[source
->Index
][3] * invQ
;
297 result
[0] = deriv
[GET_SWZ(source
->Swizzle
, 0)];
298 result
[1] = deriv
[GET_SWZ(source
->Swizzle
, 1)];
299 result
[2] = deriv
[GET_SWZ(source
->Swizzle
, 2)];
300 result
[3] = deriv
[GET_SWZ(source
->Swizzle
, 3)];
302 if (source
->NegateBase
) {
303 result
[0] = -result
[0];
304 result
[1] = -result
[1];
305 result
[2] = -result
[2];
306 result
[3] = -result
[3];
309 result
[0] = FABSF(result
[0]);
310 result
[1] = FABSF(result
[1]);
311 result
[2] = FABSF(result
[2]);
312 result
[3] = FABSF(result
[3]);
314 if (source
->NegateAbs
) {
315 result
[0] = -result
[0];
316 result
[1] = -result
[1];
317 result
[2] = -result
[2];
318 result
[3] = -result
[3];
322 ASSIGN_4V(result
, 0.0, 0.0, 0.0, 0.0);
328 * As above, but only return result[0] element.
331 fetch_vector1(const struct prog_src_register
*source
,
332 const struct gl_program_machine
*machine
, GLfloat result
[4])
334 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
337 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
339 if (source
->NegateBase
) {
340 result
[0] = -result
[0];
343 result
[0] = FABSF(result
[0]);
345 if (source
->NegateAbs
) {
346 result
[0] = -result
[0];
352 * Fetch texel from texture. Use partial derivatives when possible.
355 fetch_texel(GLcontext
*ctx
,
356 const struct gl_program_machine
*machine
,
357 const struct prog_instruction
*inst
,
358 const GLfloat texcoord
[4], GLfloat lodBias
,
361 const GLuint unit
= machine
->Samplers
[inst
->TexSrcUnit
];
363 /* Note: we only have the right derivatives for fragment input attribs.
365 if (machine
->NumDeriv
> 0 &&
366 inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
367 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
) {
368 /* simple texture fetch for which we should have derivatives */
369 GLuint attr
= inst
->SrcReg
[0].Index
;
370 machine
->FetchTexelDeriv(ctx
, texcoord
,
371 machine
->DerivX
[attr
],
372 machine
->DerivY
[attr
],
373 lodBias
, unit
, color
);
376 machine
->FetchTexelLod(ctx
, texcoord
, lodBias
, unit
, color
);
382 * Test value against zero and return GT, LT, EQ or UN if NaN.
385 generate_cc(float value
)
388 return COND_UN
; /* NaN */
398 * Test if the ccMaskRule is satisfied by the given condition code.
399 * Used to mask destination writes according to the current condition code.
401 static INLINE GLboolean
402 test_cc(GLuint condCode
, GLuint ccMaskRule
)
404 switch (ccMaskRule
) {
405 case COND_EQ
: return (condCode
== COND_EQ
);
406 case COND_NE
: return (condCode
!= COND_EQ
);
407 case COND_LT
: return (condCode
== COND_LT
);
408 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
409 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
410 case COND_GT
: return (condCode
== COND_GT
);
411 case COND_TR
: return GL_TRUE
;
412 case COND_FL
: return GL_FALSE
;
413 default: return GL_TRUE
;
419 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
420 * or GL_FALSE to indicate result.
422 static INLINE GLboolean
423 eval_condition(const struct gl_program_machine
*machine
,
424 const struct prog_instruction
*inst
)
426 const GLuint swizzle
= inst
->DstReg
.CondSwizzle
;
427 const GLuint condMask
= inst
->DstReg
.CondMask
;
428 if (test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 0)], condMask
) ||
429 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 1)], condMask
) ||
430 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 2)], condMask
) ||
431 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 3)], condMask
)) {
442 * Store 4 floats into a register. Observe the instructions saturate and
443 * set-condition-code flags.
446 store_vector4(const struct prog_instruction
*inst
,
447 struct gl_program_machine
*machine
, const GLfloat value
[4])
449 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
450 const GLboolean clamp
= inst
->SaturateMode
== SATURATE_ZERO_ONE
;
451 GLuint writeMask
= dstReg
->WriteMask
;
452 GLfloat clampedValue
[4];
453 GLfloat
*dst
= get_dst_register_pointer(dstReg
, machine
);
456 if (value
[0] > 1.0e10
||
457 IS_INF_OR_NAN(value
[0]) ||
458 IS_INF_OR_NAN(value
[1]) ||
459 IS_INF_OR_NAN(value
[2]) || IS_INF_OR_NAN(value
[3]))
460 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
464 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
465 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
466 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
467 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
468 value
= clampedValue
;
471 if (dstReg
->CondMask
!= COND_TR
) {
472 /* condition codes may turn off some writes */
473 if (writeMask
& WRITEMASK_X
) {
474 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
476 writeMask
&= ~WRITEMASK_X
;
478 if (writeMask
& WRITEMASK_Y
) {
479 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
481 writeMask
&= ~WRITEMASK_Y
;
483 if (writeMask
& WRITEMASK_Z
) {
484 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
486 writeMask
&= ~WRITEMASK_Z
;
488 if (writeMask
& WRITEMASK_W
) {
489 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
491 writeMask
&= ~WRITEMASK_W
;
495 if (writeMask
& WRITEMASK_X
)
497 if (writeMask
& WRITEMASK_Y
)
499 if (writeMask
& WRITEMASK_Z
)
501 if (writeMask
& WRITEMASK_W
)
504 if (inst
->CondUpdate
) {
505 if (writeMask
& WRITEMASK_X
)
506 machine
->CondCodes
[0] = generate_cc(value
[0]);
507 if (writeMask
& WRITEMASK_Y
)
508 machine
->CondCodes
[1] = generate_cc(value
[1]);
509 if (writeMask
& WRITEMASK_Z
)
510 machine
->CondCodes
[2] = generate_cc(value
[2]);
511 if (writeMask
& WRITEMASK_W
)
512 machine
->CondCodes
[3] = generate_cc(value
[3]);
514 printf("CondCodes=(%s,%s,%s,%s) for:\n",
515 _mesa_condcode_string(machine
->CondCodes
[0]),
516 _mesa_condcode_string(machine
->CondCodes
[1]),
517 _mesa_condcode_string(machine
->CondCodes
[2]),
518 _mesa_condcode_string(machine
->CondCodes
[3]));
525 * Store 4 uints into a register. Observe the set-condition-code flags.
528 store_vector4ui(const struct prog_instruction
*inst
,
529 struct gl_program_machine
*machine
, const GLuint value
[4])
531 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
532 GLuint writeMask
= dstReg
->WriteMask
;
533 GLuint
*dst
= (GLuint
*) get_dst_register_pointer(dstReg
, machine
);
535 if (dstReg
->CondMask
!= COND_TR
) {
536 /* condition codes may turn off some writes */
537 if (writeMask
& WRITEMASK_X
) {
538 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
540 writeMask
&= ~WRITEMASK_X
;
542 if (writeMask
& WRITEMASK_Y
) {
543 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
545 writeMask
&= ~WRITEMASK_Y
;
547 if (writeMask
& WRITEMASK_Z
) {
548 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
550 writeMask
&= ~WRITEMASK_Z
;
552 if (writeMask
& WRITEMASK_W
) {
553 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
555 writeMask
&= ~WRITEMASK_W
;
559 if (writeMask
& WRITEMASK_X
)
561 if (writeMask
& WRITEMASK_Y
)
563 if (writeMask
& WRITEMASK_Z
)
565 if (writeMask
& WRITEMASK_W
)
568 if (inst
->CondUpdate
) {
569 if (writeMask
& WRITEMASK_X
)
570 machine
->CondCodes
[0] = generate_cc(value
[0]);
571 if (writeMask
& WRITEMASK_Y
)
572 machine
->CondCodes
[1] = generate_cc(value
[1]);
573 if (writeMask
& WRITEMASK_Z
)
574 machine
->CondCodes
[2] = generate_cc(value
[2]);
575 if (writeMask
& WRITEMASK_W
)
576 machine
->CondCodes
[3] = generate_cc(value
[3]);
578 printf("CondCodes=(%s,%s,%s,%s) for:\n",
579 _mesa_condcode_string(machine
->CondCodes
[0]),
580 _mesa_condcode_string(machine
->CondCodes
[1]),
581 _mesa_condcode_string(machine
->CondCodes
[2]),
582 _mesa_condcode_string(machine
->CondCodes
[3]));
590 * Execute the given vertex/fragment program.
592 * \param ctx rendering context
593 * \param program the program to execute
594 * \param machine machine state (must be initialized)
595 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
598 _mesa_execute_program(GLcontext
* ctx
,
599 const struct gl_program
*program
,
600 struct gl_program_machine
*machine
)
602 const GLuint numInst
= program
->NumInstructions
;
603 const GLuint maxExec
= 10000;
604 GLuint pc
, numExec
= 0;
606 machine
->CurProgram
= program
;
609 printf("execute program %u --------------------\n", program
->Id
);
612 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
613 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
616 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
619 for (pc
= 0; pc
< numInst
; pc
++) {
620 const struct prog_instruction
*inst
= program
->Instructions
+ pc
;
623 _mesa_print_instruction(inst
);
626 switch (inst
->Opcode
) {
629 GLfloat a
[4], result
[4];
630 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
631 result
[0] = FABSF(a
[0]);
632 result
[1] = FABSF(a
[1]);
633 result
[2] = FABSF(a
[2]);
634 result
[3] = FABSF(a
[3]);
635 store_vector4(inst
, machine
, result
);
640 GLfloat a
[4], b
[4], result
[4];
641 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
642 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
643 result
[0] = a
[0] + b
[0];
644 result
[1] = a
[1] + b
[1];
645 result
[2] = a
[2] + b
[2];
646 result
[3] = a
[3] + b
[3];
647 store_vector4(inst
, machine
, result
);
649 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
650 result
[0], result
[1], result
[2], result
[3],
651 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
655 case OPCODE_AND
: /* bitwise AND */
657 GLuint a
[4], b
[4], result
[4];
658 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
659 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
660 result
[0] = a
[0] & b
[0];
661 result
[1] = a
[1] & b
[1];
662 result
[2] = a
[2] & b
[2];
663 result
[3] = a
[3] & b
[3];
664 store_vector4ui(inst
, machine
, result
);
670 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
671 machine
->AddressReg
[0][0] = IFLOOR(t
[0]);
678 /* subtract 1 here since pc is incremented by for(pc) loop */
679 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
681 case OPCODE_BGNSUB
: /* begin subroutine */
683 case OPCODE_ENDSUB
: /* end subroutine */
685 case OPCODE_BRA
: /* branch (conditional) */
687 case OPCODE_BRK
: /* break out of loop (conditional) */
689 case OPCODE_CONT
: /* continue loop (conditional) */
690 if (eval_condition(machine
, inst
)) {
692 /* Subtract 1 here since we'll do pc++ at end of for-loop */
693 pc
= inst
->BranchTarget
- 1;
696 case OPCODE_CAL
: /* Call subroutine (conditional) */
697 if (eval_condition(machine
, inst
)) {
698 /* call the subroutine */
699 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
700 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
702 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1; /* next inst */
703 /* Subtract 1 here since we'll do pc++ at end of for-loop */
704 pc
= inst
->BranchTarget
- 1;
709 GLfloat a
[4], b
[4], c
[4], result
[4];
710 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
711 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
712 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
713 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
714 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
715 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
716 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
717 store_vector4(inst
, machine
, result
);
722 GLfloat a
[4], result
[4];
723 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
724 result
[0] = result
[1] = result
[2] = result
[3]
725 = (GLfloat
) _mesa_cos(a
[0]);
726 store_vector4(inst
, machine
, result
);
729 case OPCODE_DDX
: /* Partial derivative with respect to X */
732 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
734 store_vector4(inst
, machine
, result
);
737 case OPCODE_DDY
: /* Partial derivative with respect to Y */
740 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
742 store_vector4(inst
, machine
, result
);
747 GLfloat a
[4], b
[4], result
[4];
748 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
749 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
750 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
);
751 store_vector4(inst
, machine
, result
);
753 printf("DP2 %g = (%g %g) . (%g %g)\n",
754 result
[0], a
[0], a
[1], b
[0], b
[1]);
760 GLfloat a
[4], b
[4], c
, result
[4];
761 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
762 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
763 fetch_vector1(&inst
->SrcReg
[1], machine
, &c
);
764 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
) + c
;
765 store_vector4(inst
, machine
, result
);
767 printf("DP2A %g = (%g %g) . (%g %g) + %g\n",
768 result
[0], a
[0], a
[1], b
[0], b
[1], c
);
774 GLfloat a
[4], b
[4], result
[4];
775 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
776 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
777 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
778 store_vector4(inst
, machine
, result
);
780 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
781 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
787 GLfloat a
[4], b
[4], result
[4];
788 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
789 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
790 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
, b
);
791 store_vector4(inst
, machine
, result
);
793 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
794 result
[0], a
[0], a
[1], a
[2], a
[3],
795 b
[0], b
[1], b
[2], b
[3]);
801 GLfloat a
[4], b
[4], result
[4];
802 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
803 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
804 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
) + b
[3];
805 store_vector4(inst
, machine
, result
);
808 case OPCODE_DST
: /* Distance vector */
810 GLfloat a
[4], b
[4], result
[4];
811 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
812 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
814 result
[1] = a
[1] * b
[1];
817 store_vector4(inst
, machine
, result
);
822 GLfloat t
[4], q
[4], floor_t0
;
823 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
824 floor_t0
= FLOORF(t
[0]);
825 if (floor_t0
> FLT_MAX_EXP
) {
826 SET_POS_INFINITY(q
[0]);
827 SET_POS_INFINITY(q
[2]);
829 else if (floor_t0
< FLT_MIN_EXP
) {
834 q
[0] = LDEXPF(1.0, (int) floor_t0
);
835 /* Note: GL_NV_vertex_program expects
836 * result.z = result.x * APPX(result.y)
837 * We do what the ARB extension says.
839 q
[2] = (GLfloat
) _mesa_pow(2.0, t
[0]);
841 q
[1] = t
[0] - floor_t0
;
843 store_vector4( inst
, machine
, q
);
846 case OPCODE_EX2
: /* Exponential base 2 */
848 GLfloat a
[4], result
[4];
849 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
850 result
[0] = result
[1] = result
[2] = result
[3] =
851 (GLfloat
) _mesa_pow(2.0, a
[0]);
852 store_vector4(inst
, machine
, result
);
857 GLfloat a
[4], result
[4];
858 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
859 result
[0] = FLOORF(a
[0]);
860 result
[1] = FLOORF(a
[1]);
861 result
[2] = FLOORF(a
[2]);
862 result
[3] = FLOORF(a
[3]);
863 store_vector4(inst
, machine
, result
);
868 GLfloat a
[4], result
[4];
869 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
870 result
[0] = a
[0] - FLOORF(a
[0]);
871 result
[1] = a
[1] - FLOORF(a
[1]);
872 result
[2] = a
[2] - FLOORF(a
[2]);
873 result
[3] = a
[3] - FLOORF(a
[3]);
874 store_vector4(inst
, machine
, result
);
881 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
883 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
884 cond
= (a
[0] != 0.0);
887 cond
= eval_condition(machine
, inst
);
890 printf("IF: %d\n", cond
);
894 /* do if-clause (just continue execution) */
897 /* go to the instruction after ELSE or ENDIF */
898 assert(inst
->BranchTarget
>= 0);
899 pc
= inst
->BranchTarget
- 1;
905 assert(inst
->BranchTarget
>= 0);
906 pc
= inst
->BranchTarget
- 1;
911 case OPCODE_KIL_NV
: /* NV_f_p only (conditional) */
912 if (eval_condition(machine
, inst
)) {
916 case OPCODE_KIL
: /* ARB_f_p only */
919 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
920 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
925 case OPCODE_LG2
: /* log base 2 */
927 GLfloat a
[4], result
[4];
928 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
929 /* The fast LOG2 macro doesn't meet the precision requirements.
931 result
[0] = result
[1] = result
[2] = result
[3] =
932 (log(a
[0]) * 1.442695F
);
933 store_vector4(inst
, machine
, result
);
938 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
939 GLfloat a
[4], result
[4];
940 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
941 a
[0] = MAX2(a
[0], 0.0F
);
942 a
[1] = MAX2(a
[1], 0.0F
);
943 /* XXX ARB version clamps a[3], NV version doesn't */
944 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
947 /* XXX we could probably just use pow() here */
949 if (a
[1] == 0.0 && a
[3] == 0.0)
952 result
[2] = (GLfloat
) _mesa_pow(a
[1], a
[3]);
958 store_vector4(inst
, machine
, result
);
960 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
961 result
[0], result
[1], result
[2], result
[3],
962 a
[0], a
[1], a
[2], a
[3]);
968 GLfloat t
[4], q
[4], abs_t0
;
969 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
970 abs_t0
= FABSF(t
[0]);
971 if (abs_t0
!= 0.0F
) {
972 /* Since we really can't handle infinite values on VMS
973 * like other OSes we'll use __MAXFLOAT to represent
974 * infinity. This may need some tweaking.
977 if (abs_t0
== __MAXFLOAT
)
979 if (IS_INF_OR_NAN(abs_t0
))
982 SET_POS_INFINITY(q
[0]);
984 SET_POS_INFINITY(q
[2]);
988 GLfloat mantissa
= FREXPF(t
[0], &exponent
);
989 q
[0] = (GLfloat
) (exponent
- 1);
990 q
[1] = (GLfloat
) (2.0 * mantissa
); /* map [.5, 1) -> [1, 2) */
992 /* The fast LOG2 macro doesn't meet the precision
995 q
[2] = (log(t
[0]) * 1.442695F
);
999 SET_NEG_INFINITY(q
[0]);
1001 SET_NEG_INFINITY(q
[2]);
1004 store_vector4(inst
, machine
, q
);
1009 GLfloat a
[4], b
[4], c
[4], result
[4];
1010 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1011 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1012 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1013 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
1014 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
1015 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
1016 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
1017 store_vector4(inst
, machine
, result
);
1019 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
1020 "(%g %g %g %g), (%g %g %g %g)\n",
1021 result
[0], result
[1], result
[2], result
[3],
1022 a
[0], a
[1], a
[2], a
[3],
1023 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1029 GLfloat a
[4], b
[4], c
[4], result
[4];
1030 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1031 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1032 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1033 result
[0] = a
[0] * b
[0] + c
[0];
1034 result
[1] = a
[1] * b
[1] + c
[1];
1035 result
[2] = a
[2] * b
[2] + c
[2];
1036 result
[3] = a
[3] * b
[3] + c
[3];
1037 store_vector4(inst
, machine
, result
);
1039 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
1040 "(%g %g %g %g) + (%g %g %g %g)\n",
1041 result
[0], result
[1], result
[2], result
[3],
1042 a
[0], a
[1], a
[2], a
[3],
1043 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1049 GLfloat a
[4], b
[4], result
[4];
1050 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1051 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1052 result
[0] = MAX2(a
[0], b
[0]);
1053 result
[1] = MAX2(a
[1], b
[1]);
1054 result
[2] = MAX2(a
[2], b
[2]);
1055 result
[3] = MAX2(a
[3], b
[3]);
1056 store_vector4(inst
, machine
, result
);
1058 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
1059 result
[0], result
[1], result
[2], result
[3],
1060 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1066 GLfloat a
[4], b
[4], result
[4];
1067 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1068 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1069 result
[0] = MIN2(a
[0], b
[0]);
1070 result
[1] = MIN2(a
[1], b
[1]);
1071 result
[2] = MIN2(a
[2], b
[2]);
1072 result
[3] = MIN2(a
[3], b
[3]);
1073 store_vector4(inst
, machine
, result
);
1079 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
1080 store_vector4(inst
, machine
, result
);
1082 printf("MOV (%g %g %g %g)\n",
1083 result
[0], result
[1], result
[2], result
[3]);
1089 GLfloat a
[4], b
[4], result
[4];
1090 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1091 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1092 result
[0] = a
[0] * b
[0];
1093 result
[1] = a
[1] * b
[1];
1094 result
[2] = a
[2] * b
[2];
1095 result
[3] = a
[3] * b
[3];
1096 store_vector4(inst
, machine
, result
);
1098 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1099 result
[0], result
[1], result
[2], result
[3],
1100 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1106 GLfloat a
[4], result
[4];
1107 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1111 result
[3] = _mesa_noise1(a
[0]);
1112 store_vector4(inst
, machine
, result
);
1117 GLfloat a
[4], result
[4];
1118 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1121 result
[2] = result
[3] = _mesa_noise2(a
[0], a
[1]);
1122 store_vector4(inst
, machine
, result
);
1127 GLfloat a
[4], result
[4];
1128 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1132 result
[3] = _mesa_noise3(a
[0], a
[1], a
[2]);
1133 store_vector4(inst
, machine
, result
);
1138 GLfloat a
[4], result
[4];
1139 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1143 result
[3] = _mesa_noise4(a
[0], a
[1], a
[2], a
[3]);
1144 store_vector4(inst
, machine
, result
);
1149 case OPCODE_NOT
: /* bitwise NOT */
1151 GLuint a
[4], result
[4];
1152 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1157 store_vector4ui(inst
, machine
, result
);
1160 case OPCODE_NRM3
: /* 3-component normalization */
1162 GLfloat a
[4], result
[4];
1164 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1165 tmp
= a
[0] * a
[0] + a
[1] * a
[1] + a
[2] * a
[2];
1167 tmp
= INV_SQRTF(tmp
);
1168 result
[0] = tmp
* a
[0];
1169 result
[1] = tmp
* a
[1];
1170 result
[2] = tmp
* a
[2];
1171 result
[3] = 0.0; /* undefined, but prevent valgrind warnings */
1172 store_vector4(inst
, machine
, result
);
1175 case OPCODE_NRM4
: /* 4-component normalization */
1177 GLfloat a
[4], result
[4];
1179 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1180 tmp
= a
[0] * a
[0] + a
[1] * a
[1] + a
[2] * a
[2] + a
[3] * a
[3];
1182 tmp
= INV_SQRTF(tmp
);
1183 result
[0] = tmp
* a
[0];
1184 result
[1] = tmp
* a
[1];
1185 result
[2] = tmp
* a
[2];
1186 result
[3] = tmp
* a
[3];
1187 store_vector4(inst
, machine
, result
);
1190 case OPCODE_OR
: /* bitwise OR */
1192 GLuint a
[4], b
[4], result
[4];
1193 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1194 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
1195 result
[0] = a
[0] | b
[0];
1196 result
[1] = a
[1] | b
[1];
1197 result
[2] = a
[2] | b
[2];
1198 result
[3] = a
[3] | b
[3];
1199 store_vector4ui(inst
, machine
, result
);
1202 case OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
1207 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1208 hx
= _mesa_float_to_half(a
[0]);
1209 hy
= _mesa_float_to_half(a
[1]);
1213 result
[3] = hx
| (hy
<< 16);
1214 store_vector4ui(inst
, machine
, result
);
1217 case OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
1220 GLuint result
[4], usx
, usy
;
1221 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1222 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1223 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1224 usx
= IROUND(a
[0] * 65535.0F
);
1225 usy
= IROUND(a
[1] * 65535.0F
);
1229 result
[3] = usx
| (usy
<< 16);
1230 store_vector4ui(inst
, machine
, result
);
1233 case OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
1236 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1237 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1238 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
1239 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
1240 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
1241 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
1242 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
1243 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
1244 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
1245 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
1249 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1250 store_vector4ui(inst
, machine
, result
);
1253 case OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
1256 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1257 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1258 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1259 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1260 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
1261 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
1262 ubx
= IROUND(255.0F
* a
[0]);
1263 uby
= IROUND(255.0F
* a
[1]);
1264 ubz
= IROUND(255.0F
* a
[2]);
1265 ubw
= IROUND(255.0F
* a
[3]);
1269 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1270 store_vector4ui(inst
, machine
, result
);
1275 GLfloat a
[4], b
[4], result
[4];
1276 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1277 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
1278 result
[0] = result
[1] = result
[2] = result
[3]
1279 = (GLfloat
) _mesa_pow(a
[0], b
[0]);
1280 store_vector4(inst
, machine
, result
);
1285 GLfloat a
[4], result
[4];
1286 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1290 else if (IS_INF_OR_NAN(a
[0]))
1291 printf("RCP(inf)\n");
1293 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
1294 store_vector4(inst
, machine
, result
);
1297 case OPCODE_RET
: /* return from subroutine (conditional) */
1298 if (eval_condition(machine
, inst
)) {
1299 if (machine
->StackDepth
== 0) {
1300 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
1302 /* subtract one because of pc++ in the for loop */
1303 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
1306 case OPCODE_RFL
: /* reflection vector */
1308 GLfloat axis
[4], dir
[4], result
[4], tmpX
, tmpW
;
1309 fetch_vector4(&inst
->SrcReg
[0], machine
, axis
);
1310 fetch_vector4(&inst
->SrcReg
[1], machine
, dir
);
1311 tmpW
= DOT3(axis
, axis
);
1312 tmpX
= (2.0F
* DOT3(axis
, dir
)) / tmpW
;
1313 result
[0] = tmpX
* axis
[0] - dir
[0];
1314 result
[1] = tmpX
* axis
[1] - dir
[1];
1315 result
[2] = tmpX
* axis
[2] - dir
[2];
1316 /* result[3] is never written! XXX enforce in parser! */
1317 store_vector4(inst
, machine
, result
);
1320 case OPCODE_RSQ
: /* 1 / sqrt() */
1322 GLfloat a
[4], result
[4];
1323 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1325 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1326 store_vector4(inst
, machine
, result
);
1328 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
1332 case OPCODE_SCS
: /* sine and cos */
1334 GLfloat a
[4], result
[4];
1335 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1336 result
[0] = (GLfloat
) _mesa_cos(a
[0]);
1337 result
[1] = (GLfloat
) _mesa_sin(a
[0]);
1338 result
[2] = 0.0; /* undefined! */
1339 result
[3] = 0.0; /* undefined! */
1340 store_vector4(inst
, machine
, result
);
1343 case OPCODE_SEQ
: /* set on equal */
1345 GLfloat a
[4], b
[4], result
[4];
1346 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1347 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1348 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1349 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1350 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1351 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1352 store_vector4(inst
, machine
, result
);
1354 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1355 result
[0], result
[1], result
[2], result
[3],
1356 a
[0], a
[1], a
[2], a
[3],
1357 b
[0], b
[1], b
[2], b
[3]);
1361 case OPCODE_SFL
: /* set false, operands ignored */
1363 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1364 store_vector4(inst
, machine
, result
);
1367 case OPCODE_SGE
: /* set on greater or equal */
1369 GLfloat a
[4], b
[4], result
[4];
1370 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1371 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1372 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1373 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1374 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1375 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1376 store_vector4(inst
, machine
, result
);
1378 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1379 result
[0], result
[1], result
[2], result
[3],
1380 a
[0], a
[1], a
[2], a
[3],
1381 b
[0], b
[1], b
[2], b
[3]);
1385 case OPCODE_SGT
: /* set on greater */
1387 GLfloat a
[4], b
[4], result
[4];
1388 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1389 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1390 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1391 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1392 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1393 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1394 store_vector4(inst
, machine
, result
);
1396 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1397 result
[0], result
[1], result
[2], result
[3],
1398 a
[0], a
[1], a
[2], a
[3],
1399 b
[0], b
[1], b
[2], b
[3]);
1405 GLfloat a
[4], result
[4];
1406 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1407 result
[0] = result
[1] = result
[2] = result
[3]
1408 = (GLfloat
) _mesa_sin(a
[0]);
1409 store_vector4(inst
, machine
, result
);
1412 case OPCODE_SLE
: /* set on less or equal */
1414 GLfloat a
[4], b
[4], result
[4];
1415 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1416 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1417 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1418 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1419 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1420 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1421 store_vector4(inst
, machine
, result
);
1423 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1424 result
[0], result
[1], result
[2], result
[3],
1425 a
[0], a
[1], a
[2], a
[3],
1426 b
[0], b
[1], b
[2], b
[3]);
1430 case OPCODE_SLT
: /* set on less */
1432 GLfloat a
[4], b
[4], result
[4];
1433 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1434 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1435 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1436 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1437 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1438 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1439 store_vector4(inst
, machine
, result
);
1441 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1442 result
[0], result
[1], result
[2], result
[3],
1443 a
[0], a
[1], a
[2], a
[3],
1444 b
[0], b
[1], b
[2], b
[3]);
1448 case OPCODE_SNE
: /* set on not equal */
1450 GLfloat a
[4], b
[4], result
[4];
1451 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1452 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1453 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1454 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1455 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1456 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1457 store_vector4(inst
, machine
, result
);
1459 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1460 result
[0], result
[1], result
[2], result
[3],
1461 a
[0], a
[1], a
[2], a
[3],
1462 b
[0], b
[1], b
[2], b
[3]);
1466 case OPCODE_SSG
: /* set sign (-1, 0 or +1) */
1468 GLfloat a
[4], result
[4];
1469 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1470 result
[0] = (GLfloat
) ((a
[0] > 0.0F
) - (a
[0] < 0.0F
));
1471 result
[1] = (GLfloat
) ((a
[1] > 0.0F
) - (a
[1] < 0.0F
));
1472 result
[2] = (GLfloat
) ((a
[2] > 0.0F
) - (a
[2] < 0.0F
));
1473 result
[3] = (GLfloat
) ((a
[3] > 0.0F
) - (a
[3] < 0.0F
));
1474 store_vector4(inst
, machine
, result
);
1477 case OPCODE_STR
: /* set true, operands ignored */
1479 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1480 store_vector4(inst
, machine
, result
);
1485 GLfloat a
[4], b
[4], result
[4];
1486 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1487 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1488 result
[0] = a
[0] - b
[0];
1489 result
[1] = a
[1] - b
[1];
1490 result
[2] = a
[2] - b
[2];
1491 result
[3] = a
[3] - b
[3];
1492 store_vector4(inst
, machine
, result
);
1494 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1495 result
[0], result
[1], result
[2], result
[3],
1496 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1500 case OPCODE_SWZ
: /* extended swizzle */
1502 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1503 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
1506 for (i
= 0; i
< 4; i
++) {
1507 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1508 if (swz
== SWIZZLE_ZERO
)
1510 else if (swz
== SWIZZLE_ONE
)
1515 result
[i
] = src
[swz
];
1517 if (source
->NegateBase
& (1 << i
))
1518 result
[i
] = -result
[i
];
1520 store_vector4(inst
, machine
, result
);
1523 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1524 /* Simple texel lookup */
1526 GLfloat texcoord
[4], color
[4];
1527 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1529 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1532 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1533 color
[0], color
[1], color
[2], color
[3],
1535 texcoord
[0], texcoord
[1], texcoord
[2], texcoord
[3]);
1537 store_vector4(inst
, machine
, color
);
1540 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1541 /* Texel lookup with LOD bias */
1543 const GLuint unit
= machine
->Samplers
[inst
->TexSrcUnit
];
1544 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
1545 GLfloat texcoord
[4], color
[4], lodBias
;
1547 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1549 /* texcoord[3] is the bias to add to lambda */
1550 lodBias
= texUnit
->LodBias
+ texcoord
[3];
1551 if (texUnit
->_Current
) {
1552 lodBias
+= texUnit
->_Current
->LodBias
;
1555 fetch_texel(ctx
, machine
, inst
, texcoord
, lodBias
, color
);
1557 store_vector4(inst
, machine
, color
);
1560 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1561 /* Texture lookup w/ partial derivatives for LOD */
1563 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1564 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1565 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1566 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1567 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1569 inst
->TexSrcUnit
, color
);
1570 store_vector4(inst
, machine
, color
);
1573 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1574 /* Texture lookup w/ projective divide */
1576 GLfloat texcoord
[4], color
[4];
1578 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1579 /* Not so sure about this test - if texcoord[3] is
1580 * zero, we'd probably be fine except for an ASSERT in
1581 * IROUND_POS() which gets triggered by the inf values created.
1583 if (texcoord
[3] != 0.0) {
1584 texcoord
[0] /= texcoord
[3];
1585 texcoord
[1] /= texcoord
[3];
1586 texcoord
[2] /= texcoord
[3];
1589 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1591 store_vector4(inst
, machine
, color
);
1594 case OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1595 /* Texture lookup w/ projective divide, as above, but do not
1596 * do the divide by w if sampling from a cube map.
1599 GLfloat texcoord
[4], color
[4];
1601 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1602 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1603 texcoord
[3] != 0.0) {
1604 texcoord
[0] /= texcoord
[3];
1605 texcoord
[1] /= texcoord
[3];
1606 texcoord
[2] /= texcoord
[3];
1609 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1611 store_vector4(inst
, machine
, color
);
1614 case OPCODE_TRUNC
: /* truncate toward zero */
1616 GLfloat a
[4], result
[4];
1617 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1618 result
[0] = (GLfloat
) (GLint
) a
[0];
1619 result
[1] = (GLfloat
) (GLint
) a
[1];
1620 result
[2] = (GLfloat
) (GLint
) a
[2];
1621 result
[3] = (GLfloat
) (GLint
) a
[3];
1622 store_vector4(inst
, machine
, result
);
1625 case OPCODE_UP2H
: /* unpack two 16-bit floats */
1627 GLfloat a
[4], result
[4];
1628 const GLuint
*rawBits
= (const GLuint
*) a
;
1630 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1631 hx
= rawBits
[0] & 0xffff;
1632 hy
= rawBits
[0] >> 16;
1633 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1634 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1635 store_vector4(inst
, machine
, result
);
1638 case OPCODE_UP2US
: /* unpack two GLushorts */
1640 GLfloat a
[4], result
[4];
1641 const GLuint
*rawBits
= (const GLuint
*) a
;
1643 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1644 usx
= rawBits
[0] & 0xffff;
1645 usy
= rawBits
[0] >> 16;
1646 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1647 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1648 store_vector4(inst
, machine
, result
);
1651 case OPCODE_UP4B
: /* unpack four GLbytes */
1653 GLfloat a
[4], result
[4];
1654 const GLuint
*rawBits
= (const GLuint
*) a
;
1655 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1656 result
[0] = (((rawBits
[0] >> 0) & 0xff) - 128) / 127.0F
;
1657 result
[1] = (((rawBits
[0] >> 8) & 0xff) - 128) / 127.0F
;
1658 result
[2] = (((rawBits
[0] >> 16) & 0xff) - 128) / 127.0F
;
1659 result
[3] = (((rawBits
[0] >> 24) & 0xff) - 128) / 127.0F
;
1660 store_vector4(inst
, machine
, result
);
1663 case OPCODE_UP4UB
: /* unpack four GLubytes */
1665 GLfloat a
[4], result
[4];
1666 const GLuint
*rawBits
= (const GLuint
*) a
;
1667 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1668 result
[0] = ((rawBits
[0] >> 0) & 0xff) / 255.0F
;
1669 result
[1] = ((rawBits
[0] >> 8) & 0xff) / 255.0F
;
1670 result
[2] = ((rawBits
[0] >> 16) & 0xff) / 255.0F
;
1671 result
[3] = ((rawBits
[0] >> 24) & 0xff) / 255.0F
;
1672 store_vector4(inst
, machine
, result
);
1675 case OPCODE_XOR
: /* bitwise XOR */
1677 GLuint a
[4], b
[4], result
[4];
1678 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1679 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
1680 result
[0] = a
[0] ^ b
[0];
1681 result
[1] = a
[1] ^ b
[1];
1682 result
[2] = a
[2] ^ b
[2];
1683 result
[3] = a
[3] ^ b
[3];
1684 store_vector4ui(inst
, machine
, result
);
1687 case OPCODE_XPD
: /* cross product */
1689 GLfloat a
[4], b
[4], result
[4];
1690 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1691 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1692 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1693 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1694 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1696 store_vector4(inst
, machine
, result
);
1698 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1699 result
[0], result
[1], result
[2], result
[3],
1700 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1704 case OPCODE_X2D
: /* 2-D matrix transform */
1706 GLfloat a
[4], b
[4], c
[4], result
[4];
1707 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1708 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1709 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1710 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1711 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1712 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1713 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1714 store_vector4(inst
, machine
, result
);
1719 if (inst
->SrcReg
[0].File
!= -1) {
1721 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1722 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst
->Data
,
1723 a
[0], a
[1], a
[2], a
[3]);
1726 _mesa_printf("%s\n", (const char *) inst
->Data
);
1733 _mesa_problem(ctx
, "Bad opcode %d in _mesa_execute_program",
1735 return GL_TRUE
; /* return value doesn't matter */
1739 if (numExec
> maxExec
) {
1740 _mesa_problem(ctx
, "Infinite loop detected in fragment program");