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) \
63 #define SET_NEG_INFINITY(x) \
70 #define SET_POS_INFINITY(x) x = __MAXFLOAT
71 #define SET_NEG_INFINITY(x) x = -__MAXFLOAT
73 #define SET_POS_INFINITY(x) x = (GLfloat) HUGE_VAL
74 #define SET_NEG_INFINITY(x) x = (GLfloat) -HUGE_VAL
77 #define SET_FLOAT_BITS(x, bits) ((fi_type *) (void *) &(x))->i = bits
80 static const GLfloat ZeroVec
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
85 * Return a pointer to the 4-element float vector specified by the given
88 static INLINE
const GLfloat
*
89 get_src_register_pointer(const struct prog_src_register
*source
,
90 const struct gl_program_machine
*machine
)
92 const struct gl_program
*prog
= machine
->CurProgram
;
93 GLint reg
= source
->Index
;
95 if (source
->RelAddr
) {
96 /* add address register value to src index/offset */
97 reg
+= machine
->AddressReg
[0][0];
103 switch (source
->File
) {
104 case PROGRAM_TEMPORARY
:
105 if (reg
>= MAX_PROGRAM_TEMPS
)
107 return machine
->Temporaries
[reg
];
110 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
111 if (reg
>= VERT_ATTRIB_MAX
)
113 return machine
->VertAttribs
[reg
];
116 if (reg
>= FRAG_ATTRIB_MAX
)
118 return machine
->Attribs
[reg
][machine
->CurElement
];
122 if (reg
>= MAX_PROGRAM_OUTPUTS
)
124 return machine
->Outputs
[reg
];
126 case PROGRAM_LOCAL_PARAM
:
127 if (reg
>= MAX_PROGRAM_LOCAL_PARAMS
)
129 return machine
->CurProgram
->LocalParams
[reg
];
131 case PROGRAM_ENV_PARAM
:
132 if (reg
>= MAX_PROGRAM_ENV_PARAMS
)
134 return machine
->EnvParams
[reg
];
136 case PROGRAM_STATE_VAR
:
138 case PROGRAM_CONSTANT
:
140 case PROGRAM_UNIFORM
:
142 case PROGRAM_NAMED_PARAM
:
143 if (reg
>= (GLint
) prog
->Parameters
->NumParameters
)
145 return prog
->Parameters
->ParameterValues
[reg
];
149 "Invalid src register file %d in get_src_register_pointer()",
157 * Return a pointer to the 4-element float vector specified by the given
158 * destination register.
160 static INLINE GLfloat
*
161 get_dst_register_pointer(const struct prog_dst_register
*dest
,
162 struct gl_program_machine
*machine
)
164 static GLfloat dummyReg
[4];
165 GLint reg
= dest
->Index
;
168 /* add address register value to src index/offset */
169 reg
+= machine
->AddressReg
[0][0];
175 switch (dest
->File
) {
176 case PROGRAM_TEMPORARY
:
177 if (reg
>= MAX_PROGRAM_TEMPS
)
179 return machine
->Temporaries
[reg
];
182 if (reg
>= MAX_PROGRAM_OUTPUTS
)
184 return machine
->Outputs
[reg
];
186 case PROGRAM_WRITE_ONLY
:
191 "Invalid dest register file %d in get_dst_register_pointer()",
200 * Fetch a 4-element float vector from the given source register.
201 * Apply swizzling and negating as needed.
204 fetch_vector4(const struct prog_src_register
*source
,
205 const struct gl_program_machine
*machine
, GLfloat result
[4])
207 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
210 if (source
->Swizzle
== SWIZZLE_NOOP
) {
212 COPY_4V(result
, src
);
215 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
216 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
217 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
218 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
219 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
220 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
221 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
222 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
226 result
[0] = FABSF(result
[0]);
227 result
[1] = FABSF(result
[1]);
228 result
[2] = FABSF(result
[2]);
229 result
[3] = FABSF(result
[3]);
231 if (source
->Negate
) {
232 ASSERT(source
->Negate
== NEGATE_XYZW
);
233 result
[0] = -result
[0];
234 result
[1] = -result
[1];
235 result
[2] = -result
[2];
236 result
[3] = -result
[3];
240 assert(!IS_INF_OR_NAN(result
[0]));
241 assert(!IS_INF_OR_NAN(result
[0]));
242 assert(!IS_INF_OR_NAN(result
[0]));
243 assert(!IS_INF_OR_NAN(result
[0]));
249 * Fetch a 4-element uint vector from the given source register.
250 * Apply swizzling but not negation/abs.
253 fetch_vector4ui(const struct prog_src_register
*source
,
254 const struct gl_program_machine
*machine
, GLuint result
[4])
256 const GLuint
*src
= (GLuint
*) get_src_register_pointer(source
, machine
);
259 if (source
->Swizzle
== SWIZZLE_NOOP
) {
261 COPY_4V(result
, src
);
264 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
265 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
266 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
267 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
268 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
269 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
270 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
271 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
274 /* Note: no Negate or Abs here */
280 * Fetch the derivative with respect to X or Y for the given register.
281 * XXX this currently only works for fragment program input attribs.
284 fetch_vector4_deriv(GLcontext
* ctx
,
285 const struct prog_src_register
*source
,
286 const struct gl_program_machine
*machine
,
287 char xOrY
, GLfloat result
[4])
289 if (source
->File
== PROGRAM_INPUT
&&
290 source
->Index
< (GLint
) machine
->NumDeriv
) {
291 const GLint col
= machine
->CurElement
;
292 const GLfloat w
= machine
->Attribs
[FRAG_ATTRIB_WPOS
][col
][3];
293 const GLfloat invQ
= 1.0f
/ w
;
297 deriv
[0] = machine
->DerivX
[source
->Index
][0] * invQ
;
298 deriv
[1] = machine
->DerivX
[source
->Index
][1] * invQ
;
299 deriv
[2] = machine
->DerivX
[source
->Index
][2] * invQ
;
300 deriv
[3] = machine
->DerivX
[source
->Index
][3] * invQ
;
303 deriv
[0] = machine
->DerivY
[source
->Index
][0] * invQ
;
304 deriv
[1] = machine
->DerivY
[source
->Index
][1] * invQ
;
305 deriv
[2] = machine
->DerivY
[source
->Index
][2] * invQ
;
306 deriv
[3] = machine
->DerivY
[source
->Index
][3] * invQ
;
309 result
[0] = deriv
[GET_SWZ(source
->Swizzle
, 0)];
310 result
[1] = deriv
[GET_SWZ(source
->Swizzle
, 1)];
311 result
[2] = deriv
[GET_SWZ(source
->Swizzle
, 2)];
312 result
[3] = deriv
[GET_SWZ(source
->Swizzle
, 3)];
315 result
[0] = FABSF(result
[0]);
316 result
[1] = FABSF(result
[1]);
317 result
[2] = FABSF(result
[2]);
318 result
[3] = FABSF(result
[3]);
320 if (source
->Negate
) {
321 ASSERT(source
->Negate
== NEGATE_XYZW
);
322 result
[0] = -result
[0];
323 result
[1] = -result
[1];
324 result
[2] = -result
[2];
325 result
[3] = -result
[3];
329 ASSIGN_4V(result
, 0.0, 0.0, 0.0, 0.0);
335 * As above, but only return result[0] element.
338 fetch_vector1(const struct prog_src_register
*source
,
339 const struct gl_program_machine
*machine
, GLfloat result
[4])
341 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
344 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
347 result
[0] = FABSF(result
[0]);
349 if (source
->Negate
) {
350 result
[0] = -result
[0];
356 * Fetch texel from texture. Use partial derivatives when possible.
359 fetch_texel(GLcontext
*ctx
,
360 const struct gl_program_machine
*machine
,
361 const struct prog_instruction
*inst
,
362 const GLfloat texcoord
[4], GLfloat lodBias
,
365 const GLuint unit
= machine
->Samplers
[inst
->TexSrcUnit
];
367 /* Note: we only have the right derivatives for fragment input attribs.
369 if (machine
->NumDeriv
> 0 &&
370 inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
371 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
) {
372 /* simple texture fetch for which we should have derivatives */
373 GLuint attr
= inst
->SrcReg
[0].Index
;
374 machine
->FetchTexelDeriv(ctx
, texcoord
,
375 machine
->DerivX
[attr
],
376 machine
->DerivY
[attr
],
377 lodBias
, unit
, color
);
380 machine
->FetchTexelLod(ctx
, texcoord
, lodBias
, unit
, color
);
386 * Test value against zero and return GT, LT, EQ or UN if NaN.
389 generate_cc(float value
)
392 return COND_UN
; /* NaN */
402 * Test if the ccMaskRule is satisfied by the given condition code.
403 * Used to mask destination writes according to the current condition code.
405 static INLINE GLboolean
406 test_cc(GLuint condCode
, GLuint ccMaskRule
)
408 switch (ccMaskRule
) {
409 case COND_EQ
: return (condCode
== COND_EQ
);
410 case COND_NE
: return (condCode
!= COND_EQ
);
411 case COND_LT
: return (condCode
== COND_LT
);
412 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
413 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
414 case COND_GT
: return (condCode
== COND_GT
);
415 case COND_TR
: return GL_TRUE
;
416 case COND_FL
: return GL_FALSE
;
417 default: return GL_TRUE
;
423 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
424 * or GL_FALSE to indicate result.
426 static INLINE GLboolean
427 eval_condition(const struct gl_program_machine
*machine
,
428 const struct prog_instruction
*inst
)
430 const GLuint swizzle
= inst
->DstReg
.CondSwizzle
;
431 const GLuint condMask
= inst
->DstReg
.CondMask
;
432 if (test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 0)], condMask
) ||
433 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 1)], condMask
) ||
434 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 2)], condMask
) ||
435 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 3)], condMask
)) {
446 * Store 4 floats into a register. Observe the instructions saturate and
447 * set-condition-code flags.
450 store_vector4(const struct prog_instruction
*inst
,
451 struct gl_program_machine
*machine
, const GLfloat value
[4])
453 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
454 const GLboolean clamp
= inst
->SaturateMode
== SATURATE_ZERO_ONE
;
455 GLuint writeMask
= dstReg
->WriteMask
;
456 GLfloat clampedValue
[4];
457 GLfloat
*dst
= get_dst_register_pointer(dstReg
, machine
);
460 if (value
[0] > 1.0e10
||
461 IS_INF_OR_NAN(value
[0]) ||
462 IS_INF_OR_NAN(value
[1]) ||
463 IS_INF_OR_NAN(value
[2]) || IS_INF_OR_NAN(value
[3]))
464 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
468 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
469 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
470 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
471 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
472 value
= clampedValue
;
475 if (dstReg
->CondMask
!= COND_TR
) {
476 /* condition codes may turn off some writes */
477 if (writeMask
& WRITEMASK_X
) {
478 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
480 writeMask
&= ~WRITEMASK_X
;
482 if (writeMask
& WRITEMASK_Y
) {
483 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
485 writeMask
&= ~WRITEMASK_Y
;
487 if (writeMask
& WRITEMASK_Z
) {
488 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
490 writeMask
&= ~WRITEMASK_Z
;
492 if (writeMask
& WRITEMASK_W
) {
493 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
495 writeMask
&= ~WRITEMASK_W
;
500 assert(!IS_INF_OR_NAN(value
[0]));
501 assert(!IS_INF_OR_NAN(value
[0]));
502 assert(!IS_INF_OR_NAN(value
[0]));
503 assert(!IS_INF_OR_NAN(value
[0]));
506 if (writeMask
& WRITEMASK_X
)
508 if (writeMask
& WRITEMASK_Y
)
510 if (writeMask
& WRITEMASK_Z
)
512 if (writeMask
& WRITEMASK_W
)
515 if (inst
->CondUpdate
) {
516 if (writeMask
& WRITEMASK_X
)
517 machine
->CondCodes
[0] = generate_cc(value
[0]);
518 if (writeMask
& WRITEMASK_Y
)
519 machine
->CondCodes
[1] = generate_cc(value
[1]);
520 if (writeMask
& WRITEMASK_Z
)
521 machine
->CondCodes
[2] = generate_cc(value
[2]);
522 if (writeMask
& WRITEMASK_W
)
523 machine
->CondCodes
[3] = generate_cc(value
[3]);
525 printf("CondCodes=(%s,%s,%s,%s) for:\n",
526 _mesa_condcode_string(machine
->CondCodes
[0]),
527 _mesa_condcode_string(machine
->CondCodes
[1]),
528 _mesa_condcode_string(machine
->CondCodes
[2]),
529 _mesa_condcode_string(machine
->CondCodes
[3]));
536 * Store 4 uints into a register. Observe the set-condition-code flags.
539 store_vector4ui(const struct prog_instruction
*inst
,
540 struct gl_program_machine
*machine
, const GLuint value
[4])
542 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
543 GLuint writeMask
= dstReg
->WriteMask
;
544 GLuint
*dst
= (GLuint
*) get_dst_register_pointer(dstReg
, machine
);
546 if (dstReg
->CondMask
!= COND_TR
) {
547 /* condition codes may turn off some writes */
548 if (writeMask
& WRITEMASK_X
) {
549 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
551 writeMask
&= ~WRITEMASK_X
;
553 if (writeMask
& WRITEMASK_Y
) {
554 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
556 writeMask
&= ~WRITEMASK_Y
;
558 if (writeMask
& WRITEMASK_Z
) {
559 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
561 writeMask
&= ~WRITEMASK_Z
;
563 if (writeMask
& WRITEMASK_W
) {
564 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
566 writeMask
&= ~WRITEMASK_W
;
570 if (writeMask
& WRITEMASK_X
)
572 if (writeMask
& WRITEMASK_Y
)
574 if (writeMask
& WRITEMASK_Z
)
576 if (writeMask
& WRITEMASK_W
)
579 if (inst
->CondUpdate
) {
580 if (writeMask
& WRITEMASK_X
)
581 machine
->CondCodes
[0] = generate_cc(value
[0]);
582 if (writeMask
& WRITEMASK_Y
)
583 machine
->CondCodes
[1] = generate_cc(value
[1]);
584 if (writeMask
& WRITEMASK_Z
)
585 machine
->CondCodes
[2] = generate_cc(value
[2]);
586 if (writeMask
& WRITEMASK_W
)
587 machine
->CondCodes
[3] = generate_cc(value
[3]);
589 printf("CondCodes=(%s,%s,%s,%s) for:\n",
590 _mesa_condcode_string(machine
->CondCodes
[0]),
591 _mesa_condcode_string(machine
->CondCodes
[1]),
592 _mesa_condcode_string(machine
->CondCodes
[2]),
593 _mesa_condcode_string(machine
->CondCodes
[3]));
601 * Execute the given vertex/fragment program.
603 * \param ctx rendering context
604 * \param program the program to execute
605 * \param machine machine state (must be initialized)
606 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
609 _mesa_execute_program(GLcontext
* ctx
,
610 const struct gl_program
*program
,
611 struct gl_program_machine
*machine
)
613 const GLuint numInst
= program
->NumInstructions
;
614 const GLuint maxExec
= 10000;
615 GLuint pc
, numExec
= 0;
617 machine
->CurProgram
= program
;
620 printf("execute program %u --------------------\n", program
->Id
);
623 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
624 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
627 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
630 for (pc
= 0; pc
< numInst
; pc
++) {
631 const struct prog_instruction
*inst
= program
->Instructions
+ pc
;
634 _mesa_print_instruction(inst
);
637 switch (inst
->Opcode
) {
640 GLfloat a
[4], result
[4];
641 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
642 result
[0] = FABSF(a
[0]);
643 result
[1] = FABSF(a
[1]);
644 result
[2] = FABSF(a
[2]);
645 result
[3] = FABSF(a
[3]);
646 store_vector4(inst
, machine
, result
);
651 GLfloat a
[4], b
[4], result
[4];
652 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
653 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
654 result
[0] = a
[0] + b
[0];
655 result
[1] = a
[1] + b
[1];
656 result
[2] = a
[2] + b
[2];
657 result
[3] = a
[3] + b
[3];
658 store_vector4(inst
, machine
, result
);
660 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
661 result
[0], result
[1], result
[2], result
[3],
662 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
666 case OPCODE_AND
: /* bitwise AND */
668 GLuint a
[4], b
[4], result
[4];
669 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
670 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
671 result
[0] = a
[0] & b
[0];
672 result
[1] = a
[1] & b
[1];
673 result
[2] = a
[2] & b
[2];
674 result
[3] = a
[3] & b
[3];
675 store_vector4ui(inst
, machine
, result
);
681 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
682 machine
->AddressReg
[0][0] = IFLOOR(t
[0]);
687 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
691 /* subtract 1 here since pc is incremented by for(pc) loop */
692 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
694 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
696 case OPCODE_BGNSUB
: /* begin subroutine */
698 case OPCODE_ENDSUB
: /* end subroutine */
700 case OPCODE_BRA
: /* branch (conditional) */
701 if (eval_condition(machine
, inst
)) {
703 /* Subtract 1 here since we'll do pc++ below */
704 pc
= inst
->BranchTarget
- 1;
707 case OPCODE_BRK
: /* break out of loop (conditional) */
708 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
710 if (eval_condition(machine
, inst
)) {
711 /* break out of loop */
712 /* pc++ at end of for-loop will put us after the ENDLOOP inst */
713 pc
= inst
->BranchTarget
;
716 case OPCODE_CONT
: /* continue loop (conditional) */
717 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
719 if (eval_condition(machine
, inst
)) {
720 /* continue at ENDLOOP */
721 /* Subtract 1 here since we'll do pc++ at end of for-loop */
722 pc
= inst
->BranchTarget
- 1;
725 case OPCODE_CAL
: /* Call subroutine (conditional) */
726 if (eval_condition(machine
, inst
)) {
727 /* call the subroutine */
728 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
729 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
731 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1; /* next inst */
732 /* Subtract 1 here since we'll do pc++ at end of for-loop */
733 pc
= inst
->BranchTarget
- 1;
738 GLfloat a
[4], b
[4], c
[4], result
[4];
739 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
740 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
741 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
742 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
743 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
744 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
745 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
746 store_vector4(inst
, machine
, result
);
751 GLfloat a
[4], result
[4];
752 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
753 result
[0] = result
[1] = result
[2] = result
[3]
754 = (GLfloat
) _mesa_cos(a
[0]);
755 store_vector4(inst
, machine
, result
);
758 case OPCODE_DDX
: /* Partial derivative with respect to X */
761 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
763 store_vector4(inst
, machine
, result
);
766 case OPCODE_DDY
: /* Partial derivative with respect to Y */
769 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
771 store_vector4(inst
, machine
, result
);
776 GLfloat a
[4], b
[4], result
[4];
777 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
778 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
779 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
);
780 store_vector4(inst
, machine
, result
);
782 printf("DP2 %g = (%g %g) . (%g %g)\n",
783 result
[0], a
[0], a
[1], b
[0], b
[1]);
789 GLfloat a
[4], b
[4], c
, result
[4];
790 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
791 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
792 fetch_vector1(&inst
->SrcReg
[1], machine
, &c
);
793 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
) + c
;
794 store_vector4(inst
, machine
, result
);
796 printf("DP2A %g = (%g %g) . (%g %g) + %g\n",
797 result
[0], a
[0], a
[1], b
[0], b
[1], c
);
803 GLfloat a
[4], b
[4], result
[4];
804 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
805 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
806 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
807 store_vector4(inst
, machine
, result
);
809 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
810 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
816 GLfloat a
[4], b
[4], result
[4];
817 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
818 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
819 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
, b
);
820 store_vector4(inst
, machine
, result
);
822 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
823 result
[0], a
[0], a
[1], a
[2], a
[3],
824 b
[0], b
[1], b
[2], b
[3]);
830 GLfloat a
[4], b
[4], result
[4];
831 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
832 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
833 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
) + b
[3];
834 store_vector4(inst
, machine
, result
);
837 case OPCODE_DST
: /* Distance vector */
839 GLfloat a
[4], b
[4], result
[4];
840 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
841 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
843 result
[1] = a
[1] * b
[1];
846 store_vector4(inst
, machine
, result
);
851 GLfloat t
[4], q
[4], floor_t0
;
852 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
853 floor_t0
= FLOORF(t
[0]);
854 if (floor_t0
> FLT_MAX_EXP
) {
855 SET_POS_INFINITY(q
[0]);
856 SET_POS_INFINITY(q
[2]);
858 else if (floor_t0
< FLT_MIN_EXP
) {
863 q
[0] = LDEXPF(1.0, (int) floor_t0
);
864 /* Note: GL_NV_vertex_program expects
865 * result.z = result.x * APPX(result.y)
866 * We do what the ARB extension says.
868 q
[2] = (GLfloat
) _mesa_pow(2.0, t
[0]);
870 q
[1] = t
[0] - floor_t0
;
872 store_vector4( inst
, machine
, q
);
875 case OPCODE_EX2
: /* Exponential base 2 */
877 GLfloat a
[4], result
[4], val
;
878 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
879 val
= (GLfloat
) _mesa_pow(2.0, a
[0]);
881 if (IS_INF_OR_NAN(val))
884 result
[0] = result
[1] = result
[2] = result
[3] = val
;
885 store_vector4(inst
, machine
, result
);
890 GLfloat a
[4], result
[4];
891 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
892 result
[0] = FLOORF(a
[0]);
893 result
[1] = FLOORF(a
[1]);
894 result
[2] = FLOORF(a
[2]);
895 result
[3] = FLOORF(a
[3]);
896 store_vector4(inst
, machine
, result
);
901 GLfloat a
[4], result
[4];
902 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
903 result
[0] = a
[0] - FLOORF(a
[0]);
904 result
[1] = a
[1] - FLOORF(a
[1]);
905 result
[2] = a
[2] - FLOORF(a
[2]);
906 result
[3] = a
[3] - FLOORF(a
[3]);
907 store_vector4(inst
, machine
, result
);
913 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
915 program
->Instructions
[inst
->BranchTarget
].Opcode
918 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
920 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
921 cond
= (a
[0] != 0.0);
924 cond
= eval_condition(machine
, inst
);
927 printf("IF: %d\n", cond
);
931 /* do if-clause (just continue execution) */
934 /* go to the instruction after ELSE or ENDIF */
935 assert(inst
->BranchTarget
>= 0);
936 pc
= inst
->BranchTarget
;
942 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
944 assert(inst
->BranchTarget
>= 0);
945 pc
= inst
->BranchTarget
;
950 case OPCODE_KIL_NV
: /* NV_f_p only (conditional) */
951 if (eval_condition(machine
, inst
)) {
955 case OPCODE_KIL
: /* ARB_f_p only */
958 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
960 printf("KIL if (%g %g %g %g) <= 0.0\n",
961 a
[0], a
[1], a
[2], a
[3]);
964 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
969 case OPCODE_LG2
: /* log base 2 */
971 GLfloat a
[4], result
[4], val
;
972 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
973 /* The fast LOG2 macro doesn't meet the precision requirements.
979 val
= log(a
[0]) * 1.442695F
;
981 result
[0] = result
[1] = result
[2] = result
[3] = val
;
982 store_vector4(inst
, machine
, result
);
987 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
988 GLfloat a
[4], result
[4];
989 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
990 a
[0] = MAX2(a
[0], 0.0F
);
991 a
[1] = MAX2(a
[1], 0.0F
);
992 /* XXX ARB version clamps a[3], NV version doesn't */
993 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
996 /* XXX we could probably just use pow() here */
998 if (a
[1] == 0.0 && a
[3] == 0.0)
1001 result
[2] = (GLfloat
) _mesa_pow(a
[1], a
[3]);
1007 store_vector4(inst
, machine
, result
);
1009 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
1010 result
[0], result
[1], result
[2], result
[3],
1011 a
[0], a
[1], a
[2], a
[3]);
1017 GLfloat t
[4], q
[4], abs_t0
;
1018 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
1019 abs_t0
= FABSF(t
[0]);
1020 if (abs_t0
!= 0.0F
) {
1021 /* Since we really can't handle infinite values on VMS
1022 * like other OSes we'll use __MAXFLOAT to represent
1023 * infinity. This may need some tweaking.
1026 if (abs_t0
== __MAXFLOAT
)
1028 if (IS_INF_OR_NAN(abs_t0
))
1031 SET_POS_INFINITY(q
[0]);
1033 SET_POS_INFINITY(q
[2]);
1037 GLfloat mantissa
= FREXPF(t
[0], &exponent
);
1038 q
[0] = (GLfloat
) (exponent
- 1);
1039 q
[1] = (GLfloat
) (2.0 * mantissa
); /* map [.5, 1) -> [1, 2) */
1041 /* The fast LOG2 macro doesn't meet the precision
1044 q
[2] = (log(t
[0]) * 1.442695F
);
1048 SET_NEG_INFINITY(q
[0]);
1050 SET_NEG_INFINITY(q
[2]);
1053 store_vector4(inst
, machine
, q
);
1058 GLfloat a
[4], b
[4], c
[4], result
[4];
1059 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1060 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1061 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1062 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
1063 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
1064 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
1065 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
1066 store_vector4(inst
, machine
, result
);
1068 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
1069 "(%g %g %g %g), (%g %g %g %g)\n",
1070 result
[0], result
[1], result
[2], result
[3],
1071 a
[0], a
[1], a
[2], a
[3],
1072 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1078 GLfloat a
[4], b
[4], c
[4], result
[4];
1079 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1080 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1081 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1082 result
[0] = a
[0] * b
[0] + c
[0];
1083 result
[1] = a
[1] * b
[1] + c
[1];
1084 result
[2] = a
[2] * b
[2] + c
[2];
1085 result
[3] = a
[3] * b
[3] + c
[3];
1086 store_vector4(inst
, machine
, result
);
1088 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
1089 "(%g %g %g %g) + (%g %g %g %g)\n",
1090 result
[0], result
[1], result
[2], result
[3],
1091 a
[0], a
[1], a
[2], a
[3],
1092 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1098 GLfloat a
[4], b
[4], result
[4];
1099 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1100 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1101 result
[0] = MAX2(a
[0], b
[0]);
1102 result
[1] = MAX2(a
[1], b
[1]);
1103 result
[2] = MAX2(a
[2], b
[2]);
1104 result
[3] = MAX2(a
[3], b
[3]);
1105 store_vector4(inst
, machine
, result
);
1107 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
1108 result
[0], result
[1], result
[2], result
[3],
1109 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1115 GLfloat a
[4], b
[4], result
[4];
1116 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1117 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1118 result
[0] = MIN2(a
[0], b
[0]);
1119 result
[1] = MIN2(a
[1], b
[1]);
1120 result
[2] = MIN2(a
[2], b
[2]);
1121 result
[3] = MIN2(a
[3], b
[3]);
1122 store_vector4(inst
, machine
, result
);
1128 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
1129 store_vector4(inst
, machine
, result
);
1131 printf("MOV (%g %g %g %g)\n",
1132 result
[0], result
[1], result
[2], result
[3]);
1138 GLfloat a
[4], b
[4], result
[4];
1139 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1140 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1141 result
[0] = a
[0] * b
[0];
1142 result
[1] = a
[1] * b
[1];
1143 result
[2] = a
[2] * b
[2];
1144 result
[3] = a
[3] * b
[3];
1145 store_vector4(inst
, machine
, result
);
1147 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1148 result
[0], result
[1], result
[2], result
[3],
1149 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1155 GLfloat a
[4], result
[4];
1156 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1160 result
[3] = _mesa_noise1(a
[0]);
1161 store_vector4(inst
, machine
, result
);
1166 GLfloat a
[4], result
[4];
1167 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1170 result
[2] = result
[3] = _mesa_noise2(a
[0], a
[1]);
1171 store_vector4(inst
, machine
, result
);
1176 GLfloat a
[4], result
[4];
1177 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1181 result
[3] = _mesa_noise3(a
[0], a
[1], a
[2]);
1182 store_vector4(inst
, machine
, result
);
1187 GLfloat a
[4], result
[4];
1188 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1192 result
[3] = _mesa_noise4(a
[0], a
[1], a
[2], a
[3]);
1193 store_vector4(inst
, machine
, result
);
1198 case OPCODE_NOT
: /* bitwise NOT */
1200 GLuint a
[4], result
[4];
1201 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1206 store_vector4ui(inst
, machine
, result
);
1209 case OPCODE_NRM3
: /* 3-component normalization */
1211 GLfloat a
[4], result
[4];
1213 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1214 tmp
= a
[0] * a
[0] + a
[1] * a
[1] + a
[2] * a
[2];
1216 tmp
= INV_SQRTF(tmp
);
1217 result
[0] = tmp
* a
[0];
1218 result
[1] = tmp
* a
[1];
1219 result
[2] = tmp
* a
[2];
1220 result
[3] = 0.0; /* undefined, but prevent valgrind warnings */
1221 store_vector4(inst
, machine
, result
);
1224 case OPCODE_NRM4
: /* 4-component normalization */
1226 GLfloat a
[4], result
[4];
1228 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1229 tmp
= a
[0] * a
[0] + a
[1] * a
[1] + a
[2] * a
[2] + a
[3] * a
[3];
1231 tmp
= INV_SQRTF(tmp
);
1232 result
[0] = tmp
* a
[0];
1233 result
[1] = tmp
* a
[1];
1234 result
[2] = tmp
* a
[2];
1235 result
[3] = tmp
* a
[3];
1236 store_vector4(inst
, machine
, result
);
1239 case OPCODE_OR
: /* bitwise OR */
1241 GLuint a
[4], b
[4], result
[4];
1242 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1243 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
1244 result
[0] = a
[0] | b
[0];
1245 result
[1] = a
[1] | b
[1];
1246 result
[2] = a
[2] | b
[2];
1247 result
[3] = a
[3] | b
[3];
1248 store_vector4ui(inst
, machine
, result
);
1251 case OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
1256 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1257 hx
= _mesa_float_to_half(a
[0]);
1258 hy
= _mesa_float_to_half(a
[1]);
1262 result
[3] = hx
| (hy
<< 16);
1263 store_vector4ui(inst
, machine
, result
);
1266 case OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
1269 GLuint result
[4], usx
, usy
;
1270 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1271 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1272 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1273 usx
= IROUND(a
[0] * 65535.0F
);
1274 usy
= IROUND(a
[1] * 65535.0F
);
1278 result
[3] = usx
| (usy
<< 16);
1279 store_vector4ui(inst
, machine
, result
);
1282 case OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
1285 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1286 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1287 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
1288 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
1289 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
1290 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
1291 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
1292 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
1293 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
1294 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
1298 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1299 store_vector4ui(inst
, machine
, result
);
1302 case OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
1305 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1306 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1307 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1308 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1309 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
1310 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
1311 ubx
= IROUND(255.0F
* a
[0]);
1312 uby
= IROUND(255.0F
* a
[1]);
1313 ubz
= IROUND(255.0F
* a
[2]);
1314 ubw
= IROUND(255.0F
* a
[3]);
1318 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1319 store_vector4ui(inst
, machine
, result
);
1324 GLfloat a
[4], b
[4], result
[4];
1325 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1326 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
1327 result
[0] = result
[1] = result
[2] = result
[3]
1328 = (GLfloat
) _mesa_pow(a
[0], b
[0]);
1329 store_vector4(inst
, machine
, result
);
1334 GLfloat a
[4], result
[4];
1335 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1339 else if (IS_INF_OR_NAN(a
[0]))
1340 printf("RCP(inf)\n");
1342 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
1343 store_vector4(inst
, machine
, result
);
1346 case OPCODE_RET
: /* return from subroutine (conditional) */
1347 if (eval_condition(machine
, inst
)) {
1348 if (machine
->StackDepth
== 0) {
1349 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
1351 /* subtract one because of pc++ in the for loop */
1352 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
1355 case OPCODE_RFL
: /* reflection vector */
1357 GLfloat axis
[4], dir
[4], result
[4], tmpX
, tmpW
;
1358 fetch_vector4(&inst
->SrcReg
[0], machine
, axis
);
1359 fetch_vector4(&inst
->SrcReg
[1], machine
, dir
);
1360 tmpW
= DOT3(axis
, axis
);
1361 tmpX
= (2.0F
* DOT3(axis
, dir
)) / tmpW
;
1362 result
[0] = tmpX
* axis
[0] - dir
[0];
1363 result
[1] = tmpX
* axis
[1] - dir
[1];
1364 result
[2] = tmpX
* axis
[2] - dir
[2];
1365 /* result[3] is never written! XXX enforce in parser! */
1366 store_vector4(inst
, machine
, result
);
1369 case OPCODE_RSQ
: /* 1 / sqrt() */
1371 GLfloat a
[4], result
[4];
1372 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1374 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1375 store_vector4(inst
, machine
, result
);
1377 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
1381 case OPCODE_SCS
: /* sine and cos */
1383 GLfloat a
[4], result
[4];
1384 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1385 result
[0] = (GLfloat
) _mesa_cos(a
[0]);
1386 result
[1] = (GLfloat
) _mesa_sin(a
[0]);
1387 result
[2] = 0.0; /* undefined! */
1388 result
[3] = 0.0; /* undefined! */
1389 store_vector4(inst
, machine
, result
);
1392 case OPCODE_SEQ
: /* set on equal */
1394 GLfloat a
[4], b
[4], result
[4];
1395 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1396 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1397 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1398 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1399 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1400 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1401 store_vector4(inst
, machine
, result
);
1403 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1404 result
[0], result
[1], result
[2], result
[3],
1405 a
[0], a
[1], a
[2], a
[3],
1406 b
[0], b
[1], b
[2], b
[3]);
1410 case OPCODE_SFL
: /* set false, operands ignored */
1412 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1413 store_vector4(inst
, machine
, result
);
1416 case OPCODE_SGE
: /* set on greater or equal */
1418 GLfloat a
[4], b
[4], result
[4];
1419 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1420 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1421 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1422 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1423 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1424 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1425 store_vector4(inst
, machine
, result
);
1427 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1428 result
[0], result
[1], result
[2], result
[3],
1429 a
[0], a
[1], a
[2], a
[3],
1430 b
[0], b
[1], b
[2], b
[3]);
1434 case OPCODE_SGT
: /* set on greater */
1436 GLfloat a
[4], b
[4], result
[4];
1437 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1438 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1439 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1440 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1441 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1442 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1443 store_vector4(inst
, machine
, result
);
1445 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1446 result
[0], result
[1], result
[2], result
[3],
1447 a
[0], a
[1], a
[2], a
[3],
1448 b
[0], b
[1], b
[2], b
[3]);
1454 GLfloat a
[4], result
[4];
1455 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1456 result
[0] = result
[1] = result
[2] = result
[3]
1457 = (GLfloat
) _mesa_sin(a
[0]);
1458 store_vector4(inst
, machine
, result
);
1461 case OPCODE_SLE
: /* set on less or equal */
1463 GLfloat a
[4], b
[4], result
[4];
1464 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1465 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1466 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1467 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1468 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1469 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1470 store_vector4(inst
, machine
, result
);
1472 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1473 result
[0], result
[1], result
[2], result
[3],
1474 a
[0], a
[1], a
[2], a
[3],
1475 b
[0], b
[1], b
[2], b
[3]);
1479 case OPCODE_SLT
: /* set on less */
1481 GLfloat a
[4], b
[4], result
[4];
1482 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1483 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1484 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1485 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1486 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1487 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1488 store_vector4(inst
, machine
, result
);
1490 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1491 result
[0], result
[1], result
[2], result
[3],
1492 a
[0], a
[1], a
[2], a
[3],
1493 b
[0], b
[1], b
[2], b
[3]);
1497 case OPCODE_SNE
: /* set on not equal */
1499 GLfloat a
[4], b
[4], result
[4];
1500 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1501 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1502 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1503 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1504 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1505 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1506 store_vector4(inst
, machine
, result
);
1508 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1509 result
[0], result
[1], result
[2], result
[3],
1510 a
[0], a
[1], a
[2], a
[3],
1511 b
[0], b
[1], b
[2], b
[3]);
1515 case OPCODE_SSG
: /* set sign (-1, 0 or +1) */
1517 GLfloat a
[4], result
[4];
1518 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1519 result
[0] = (GLfloat
) ((a
[0] > 0.0F
) - (a
[0] < 0.0F
));
1520 result
[1] = (GLfloat
) ((a
[1] > 0.0F
) - (a
[1] < 0.0F
));
1521 result
[2] = (GLfloat
) ((a
[2] > 0.0F
) - (a
[2] < 0.0F
));
1522 result
[3] = (GLfloat
) ((a
[3] > 0.0F
) - (a
[3] < 0.0F
));
1523 store_vector4(inst
, machine
, result
);
1526 case OPCODE_STR
: /* set true, operands ignored */
1528 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1529 store_vector4(inst
, machine
, result
);
1534 GLfloat a
[4], b
[4], result
[4];
1535 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1536 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1537 result
[0] = a
[0] - b
[0];
1538 result
[1] = a
[1] - b
[1];
1539 result
[2] = a
[2] - b
[2];
1540 result
[3] = a
[3] - b
[3];
1541 store_vector4(inst
, machine
, result
);
1543 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1544 result
[0], result
[1], result
[2], result
[3],
1545 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1549 case OPCODE_SWZ
: /* extended swizzle */
1551 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1552 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
1555 for (i
= 0; i
< 4; i
++) {
1556 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1557 if (swz
== SWIZZLE_ZERO
)
1559 else if (swz
== SWIZZLE_ONE
)
1564 result
[i
] = src
[swz
];
1566 if (source
->Negate
& (1 << i
))
1567 result
[i
] = -result
[i
];
1569 store_vector4(inst
, machine
, result
);
1572 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1573 /* Simple texel lookup */
1575 GLfloat texcoord
[4], color
[4];
1576 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1578 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1581 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1582 color
[0], color
[1], color
[2], color
[3],
1584 texcoord
[0], texcoord
[1], texcoord
[2], texcoord
[3]);
1586 store_vector4(inst
, machine
, color
);
1589 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1590 /* Texel lookup with LOD bias */
1592 GLfloat texcoord
[4], color
[4], lodBias
;
1594 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1596 /* texcoord[3] is the bias to add to lambda */
1597 lodBias
= texcoord
[3];
1599 fetch_texel(ctx
, machine
, inst
, texcoord
, lodBias
, color
);
1601 store_vector4(inst
, machine
, color
);
1604 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1605 /* Texture lookup w/ partial derivatives for LOD */
1607 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1608 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1609 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1610 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1611 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1613 inst
->TexSrcUnit
, color
);
1614 store_vector4(inst
, machine
, color
);
1617 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1618 /* Texture lookup w/ projective divide */
1620 GLfloat texcoord
[4], color
[4];
1622 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1623 /* Not so sure about this test - if texcoord[3] is
1624 * zero, we'd probably be fine except for an ASSERT in
1625 * IROUND_POS() which gets triggered by the inf values created.
1627 if (texcoord
[3] != 0.0) {
1628 texcoord
[0] /= texcoord
[3];
1629 texcoord
[1] /= texcoord
[3];
1630 texcoord
[2] /= texcoord
[3];
1633 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1635 store_vector4(inst
, machine
, color
);
1638 case OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1639 /* Texture lookup w/ projective divide, as above, but do not
1640 * do the divide by w if sampling from a cube map.
1643 GLfloat texcoord
[4], color
[4];
1645 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1646 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1647 texcoord
[3] != 0.0) {
1648 texcoord
[0] /= texcoord
[3];
1649 texcoord
[1] /= texcoord
[3];
1650 texcoord
[2] /= texcoord
[3];
1653 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1655 store_vector4(inst
, machine
, color
);
1658 case OPCODE_TRUNC
: /* truncate toward zero */
1660 GLfloat a
[4], result
[4];
1661 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1662 result
[0] = (GLfloat
) (GLint
) a
[0];
1663 result
[1] = (GLfloat
) (GLint
) a
[1];
1664 result
[2] = (GLfloat
) (GLint
) a
[2];
1665 result
[3] = (GLfloat
) (GLint
) a
[3];
1666 store_vector4(inst
, machine
, result
);
1669 case OPCODE_UP2H
: /* unpack two 16-bit floats */
1671 GLfloat a
[4], result
[4];
1674 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1678 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1679 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1680 store_vector4(inst
, machine
, result
);
1683 case OPCODE_UP2US
: /* unpack two GLushorts */
1685 GLfloat a
[4], result
[4];
1688 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1690 usx
= fi
.i
& 0xffff;
1692 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1693 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1694 store_vector4(inst
, machine
, result
);
1697 case OPCODE_UP4B
: /* unpack four GLbytes */
1699 GLfloat a
[4], result
[4];
1701 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1703 result
[0] = (((fi
.i
>> 0) & 0xff) - 128) / 127.0F
;
1704 result
[1] = (((fi
.i
>> 8) & 0xff) - 128) / 127.0F
;
1705 result
[2] = (((fi
.i
>> 16) & 0xff) - 128) / 127.0F
;
1706 result
[3] = (((fi
.i
>> 24) & 0xff) - 128) / 127.0F
;
1707 store_vector4(inst
, machine
, result
);
1710 case OPCODE_UP4UB
: /* unpack four GLubytes */
1712 GLfloat a
[4], result
[4];
1714 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1716 result
[0] = ((fi
.i
>> 0) & 0xff) / 255.0F
;
1717 result
[1] = ((fi
.i
>> 8) & 0xff) / 255.0F
;
1718 result
[2] = ((fi
.i
>> 16) & 0xff) / 255.0F
;
1719 result
[3] = ((fi
.i
>> 24) & 0xff) / 255.0F
;
1720 store_vector4(inst
, machine
, result
);
1723 case OPCODE_XOR
: /* bitwise XOR */
1725 GLuint a
[4], b
[4], result
[4];
1726 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1727 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
1728 result
[0] = a
[0] ^ b
[0];
1729 result
[1] = a
[1] ^ b
[1];
1730 result
[2] = a
[2] ^ b
[2];
1731 result
[3] = a
[3] ^ b
[3];
1732 store_vector4ui(inst
, machine
, result
);
1735 case OPCODE_XPD
: /* cross product */
1737 GLfloat a
[4], b
[4], result
[4];
1738 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1739 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1740 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1741 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1742 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1744 store_vector4(inst
, machine
, result
);
1746 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1747 result
[0], result
[1], result
[2], result
[3],
1748 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1752 case OPCODE_X2D
: /* 2-D matrix transform */
1754 GLfloat a
[4], b
[4], c
[4], result
[4];
1755 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1756 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1757 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1758 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1759 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1760 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1761 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1762 store_vector4(inst
, machine
, result
);
1767 if (inst
->SrcReg
[0].File
!= -1) {
1769 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1770 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst
->Data
,
1771 a
[0], a
[1], a
[2], a
[3]);
1774 _mesa_printf("%s\n", (const char *) inst
->Data
);
1781 _mesa_problem(ctx
, "Bad opcode %d in _mesa_execute_program",
1783 return GL_TRUE
; /* return value doesn't matter */
1787 if (numExec
> maxExec
) {
1788 _mesa_problem(ctx
, "Infinite loop detected in fragment program");