2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \file prog_execute.c
27 * Software interpreter for vertex/fragment programs.
32 * NOTE: we do everything in single-precision floating point; we don't
33 * currently observe the single/half/fixed-precision qualifiers.
38 #include "main/glheader.h"
39 #include "main/colormac.h"
40 #include "main/macros.h"
41 #include "prog_execute.h"
42 #include "prog_instruction.h"
43 #include "prog_parameter.h"
44 #include "prog_print.h"
45 #include "prog_noise.h"
53 * Set x to positive or negative infinity.
55 #if defined(USE_IEEE) || defined(_WIN32)
56 #define SET_POS_INFINITY(x) \
62 #define SET_NEG_INFINITY(x) \
69 #define SET_POS_INFINITY(x) x = (GLfloat) HUGE_VAL
70 #define SET_NEG_INFINITY(x) x = (GLfloat) -HUGE_VAL
73 #define SET_FLOAT_BITS(x, bits) ((fi_type *) (void *) &(x))->i = bits
76 static const GLfloat ZeroVec
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
80 * Return a pointer to the 4-element float vector specified by the given
83 static inline const GLfloat
*
84 get_src_register_pointer(const struct prog_src_register
*source
,
85 const struct gl_program_machine
*machine
)
87 const struct gl_program
*prog
= machine
->CurProgram
;
88 GLint reg
= source
->Index
;
90 if (source
->RelAddr
) {
91 /* add address register value to src index/offset */
92 reg
+= machine
->AddressReg
[0][0];
98 switch (source
->File
) {
99 case PROGRAM_TEMPORARY
:
100 if (reg
>= MAX_PROGRAM_TEMPS
)
102 return machine
->Temporaries
[reg
];
105 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
106 if (reg
>= VERT_ATTRIB_MAX
)
108 return machine
->VertAttribs
[reg
];
111 if (reg
>= VARYING_SLOT_MAX
)
113 return machine
->Attribs
[reg
][machine
->CurElement
];
117 if (reg
>= MAX_PROGRAM_OUTPUTS
)
119 return machine
->Outputs
[reg
];
121 case PROGRAM_LOCAL_PARAM
:
122 if (reg
>= MAX_PROGRAM_LOCAL_PARAMS
)
124 return machine
->CurProgram
->LocalParams
[reg
];
126 case PROGRAM_ENV_PARAM
:
127 if (reg
>= MAX_PROGRAM_ENV_PARAMS
)
129 return machine
->EnvParams
[reg
];
131 case PROGRAM_STATE_VAR
:
133 case PROGRAM_CONSTANT
:
135 case PROGRAM_UNIFORM
:
136 if (reg
>= (GLint
) prog
->Parameters
->NumParameters
)
138 return (GLfloat
*) prog
->Parameters
->ParameterValues
[reg
];
140 case PROGRAM_SYSTEM_VALUE
:
141 assert(reg
< Elements(machine
->SystemValues
));
142 return machine
->SystemValues
[reg
];
146 "Invalid src register file %d in get_src_register_pointer()",
154 * Return a pointer to the 4-element float vector specified by the given
155 * destination register.
157 static inline GLfloat
*
158 get_dst_register_pointer(const struct prog_dst_register
*dest
,
159 struct gl_program_machine
*machine
)
161 static GLfloat dummyReg
[4];
162 GLint reg
= dest
->Index
;
165 /* add address register value to src index/offset */
166 reg
+= machine
->AddressReg
[0][0];
172 switch (dest
->File
) {
173 case PROGRAM_TEMPORARY
:
174 if (reg
>= MAX_PROGRAM_TEMPS
)
176 return machine
->Temporaries
[reg
];
179 if (reg
>= MAX_PROGRAM_OUTPUTS
)
181 return machine
->Outputs
[reg
];
185 "Invalid dest register file %d in get_dst_register_pointer()",
194 * Fetch a 4-element float vector from the given source register.
195 * Apply swizzling and negating as needed.
198 fetch_vector4(const struct prog_src_register
*source
,
199 const struct gl_program_machine
*machine
, GLfloat result
[4])
201 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
204 if (source
->Swizzle
== SWIZZLE_NOOP
) {
206 COPY_4V(result
, src
);
209 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
210 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
211 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
212 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
213 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
214 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
215 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
216 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
220 result
[0] = FABSF(result
[0]);
221 result
[1] = FABSF(result
[1]);
222 result
[2] = FABSF(result
[2]);
223 result
[3] = FABSF(result
[3]);
225 if (source
->Negate
) {
226 ASSERT(source
->Negate
== NEGATE_XYZW
);
227 result
[0] = -result
[0];
228 result
[1] = -result
[1];
229 result
[2] = -result
[2];
230 result
[3] = -result
[3];
234 assert(!IS_INF_OR_NAN(result
[0]));
235 assert(!IS_INF_OR_NAN(result
[0]));
236 assert(!IS_INF_OR_NAN(result
[0]));
237 assert(!IS_INF_OR_NAN(result
[0]));
243 * Fetch a 4-element uint vector from the given source register.
244 * Apply swizzling but not negation/abs.
247 fetch_vector4ui(const struct prog_src_register
*source
,
248 const struct gl_program_machine
*machine
, GLuint result
[4])
250 const GLuint
*src
= (GLuint
*) get_src_register_pointer(source
, machine
);
253 if (source
->Swizzle
== SWIZZLE_NOOP
) {
255 COPY_4V(result
, src
);
258 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
259 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
260 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
261 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
262 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
263 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
264 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
265 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
268 /* Note: no Negate or Abs here */
274 * Fetch the derivative with respect to X or Y for the given register.
275 * XXX this currently only works for fragment program input attribs.
278 fetch_vector4_deriv(struct gl_context
* ctx
,
279 const struct prog_src_register
*source
,
280 const struct gl_program_machine
*machine
,
281 char xOrY
, GLfloat result
[4])
283 if (source
->File
== PROGRAM_INPUT
&&
284 source
->Index
< (GLint
) machine
->NumDeriv
) {
285 const GLint col
= machine
->CurElement
;
286 const GLfloat w
= machine
->Attribs
[VARYING_SLOT_POS
][col
][3];
287 const GLfloat invQ
= 1.0f
/ w
;
291 deriv
[0] = machine
->DerivX
[source
->Index
][0] * invQ
;
292 deriv
[1] = machine
->DerivX
[source
->Index
][1] * invQ
;
293 deriv
[2] = machine
->DerivX
[source
->Index
][2] * invQ
;
294 deriv
[3] = machine
->DerivX
[source
->Index
][3] * invQ
;
297 deriv
[0] = machine
->DerivY
[source
->Index
][0] * invQ
;
298 deriv
[1] = machine
->DerivY
[source
->Index
][1] * invQ
;
299 deriv
[2] = machine
->DerivY
[source
->Index
][2] * invQ
;
300 deriv
[3] = machine
->DerivY
[source
->Index
][3] * invQ
;
303 result
[0] = deriv
[GET_SWZ(source
->Swizzle
, 0)];
304 result
[1] = deriv
[GET_SWZ(source
->Swizzle
, 1)];
305 result
[2] = deriv
[GET_SWZ(source
->Swizzle
, 2)];
306 result
[3] = deriv
[GET_SWZ(source
->Swizzle
, 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
->Negate
) {
315 ASSERT(source
->Negate
== NEGATE_XYZW
);
316 result
[0] = -result
[0];
317 result
[1] = -result
[1];
318 result
[2] = -result
[2];
319 result
[3] = -result
[3];
323 ASSIGN_4V(result
, 0.0, 0.0, 0.0, 0.0);
329 * As above, but only return result[0] element.
332 fetch_vector1(const struct prog_src_register
*source
,
333 const struct gl_program_machine
*machine
, GLfloat result
[4])
335 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
338 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
341 result
[0] = FABSF(result
[0]);
343 if (source
->Negate
) {
344 result
[0] = -result
[0];
350 fetch_vector1ui(const struct prog_src_register
*source
,
351 const struct gl_program_machine
*machine
)
353 const GLuint
*src
= (GLuint
*) get_src_register_pointer(source
, machine
);
354 return src
[GET_SWZ(source
->Swizzle
, 0)];
359 * Fetch texel from texture. Use partial derivatives when possible.
362 fetch_texel(struct gl_context
*ctx
,
363 const struct gl_program_machine
*machine
,
364 const struct prog_instruction
*inst
,
365 const GLfloat texcoord
[4], GLfloat lodBias
,
368 const GLuint unit
= machine
->Samplers
[inst
->TexSrcUnit
];
370 /* Note: we only have the right derivatives for fragment input attribs.
372 if (machine
->NumDeriv
> 0 &&
373 inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
374 inst
->SrcReg
[0].Index
== VARYING_SLOT_TEX0
+ inst
->TexSrcUnit
) {
375 /* simple texture fetch for which we should have derivatives */
376 GLuint attr
= inst
->SrcReg
[0].Index
;
377 machine
->FetchTexelDeriv(ctx
, texcoord
,
378 machine
->DerivX
[attr
],
379 machine
->DerivY
[attr
],
380 lodBias
, unit
, color
);
383 machine
->FetchTexelLod(ctx
, texcoord
, lodBias
, unit
, color
);
389 * Test value against zero and return GT, LT, EQ or UN if NaN.
392 generate_cc(float value
)
395 return COND_UN
; /* NaN */
405 * Test if the ccMaskRule is satisfied by the given condition code.
406 * Used to mask destination writes according to the current condition code.
408 static inline GLboolean
409 test_cc(GLuint condCode
, GLuint ccMaskRule
)
411 switch (ccMaskRule
) {
412 case COND_EQ
: return (condCode
== COND_EQ
);
413 case COND_NE
: return (condCode
!= COND_EQ
);
414 case COND_LT
: return (condCode
== COND_LT
);
415 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
416 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
417 case COND_GT
: return (condCode
== COND_GT
);
418 case COND_TR
: return GL_TRUE
;
419 case COND_FL
: return GL_FALSE
;
420 default: return GL_TRUE
;
426 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
427 * or GL_FALSE to indicate result.
429 static inline GLboolean
430 eval_condition(const struct gl_program_machine
*machine
,
431 const struct prog_instruction
*inst
)
433 const GLuint swizzle
= inst
->DstReg
.CondSwizzle
;
434 const GLuint condMask
= inst
->DstReg
.CondMask
;
435 if (test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 0)], condMask
) ||
436 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 1)], condMask
) ||
437 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 2)], condMask
) ||
438 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 3)], condMask
)) {
449 * Store 4 floats into a register. Observe the instructions saturate and
450 * set-condition-code flags.
453 store_vector4(const struct prog_instruction
*inst
,
454 struct gl_program_machine
*machine
, const GLfloat value
[4])
456 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
457 const GLboolean clamp
= inst
->SaturateMode
== SATURATE_ZERO_ONE
;
458 GLuint writeMask
= dstReg
->WriteMask
;
459 GLfloat clampedValue
[4];
460 GLfloat
*dst
= get_dst_register_pointer(dstReg
, machine
);
463 if (value
[0] > 1.0e10
||
464 IS_INF_OR_NAN(value
[0]) ||
465 IS_INF_OR_NAN(value
[1]) ||
466 IS_INF_OR_NAN(value
[2]) || IS_INF_OR_NAN(value
[3]))
467 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
471 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
472 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
473 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
474 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
475 value
= clampedValue
;
478 if (dstReg
->CondMask
!= COND_TR
) {
479 /* condition codes may turn off some writes */
480 if (writeMask
& WRITEMASK_X
) {
481 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
483 writeMask
&= ~WRITEMASK_X
;
485 if (writeMask
& WRITEMASK_Y
) {
486 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
488 writeMask
&= ~WRITEMASK_Y
;
490 if (writeMask
& WRITEMASK_Z
) {
491 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
493 writeMask
&= ~WRITEMASK_Z
;
495 if (writeMask
& WRITEMASK_W
) {
496 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
498 writeMask
&= ~WRITEMASK_W
;
503 assert(!IS_INF_OR_NAN(value
[0]));
504 assert(!IS_INF_OR_NAN(value
[0]));
505 assert(!IS_INF_OR_NAN(value
[0]));
506 assert(!IS_INF_OR_NAN(value
[0]));
509 if (writeMask
& WRITEMASK_X
)
511 if (writeMask
& WRITEMASK_Y
)
513 if (writeMask
& WRITEMASK_Z
)
515 if (writeMask
& WRITEMASK_W
)
518 if (inst
->CondUpdate
) {
519 if (writeMask
& WRITEMASK_X
)
520 machine
->CondCodes
[0] = generate_cc(value
[0]);
521 if (writeMask
& WRITEMASK_Y
)
522 machine
->CondCodes
[1] = generate_cc(value
[1]);
523 if (writeMask
& WRITEMASK_Z
)
524 machine
->CondCodes
[2] = generate_cc(value
[2]);
525 if (writeMask
& WRITEMASK_W
)
526 machine
->CondCodes
[3] = generate_cc(value
[3]);
528 printf("CondCodes=(%s,%s,%s,%s) for:\n",
529 _mesa_condcode_string(machine
->CondCodes
[0]),
530 _mesa_condcode_string(machine
->CondCodes
[1]),
531 _mesa_condcode_string(machine
->CondCodes
[2]),
532 _mesa_condcode_string(machine
->CondCodes
[3]));
539 * Store 4 uints into a register. Observe the set-condition-code flags.
542 store_vector4ui(const struct prog_instruction
*inst
,
543 struct gl_program_machine
*machine
, const GLuint value
[4])
545 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
546 GLuint writeMask
= dstReg
->WriteMask
;
547 GLuint
*dst
= (GLuint
*) get_dst_register_pointer(dstReg
, machine
);
549 if (dstReg
->CondMask
!= COND_TR
) {
550 /* condition codes may turn off some writes */
551 if (writeMask
& WRITEMASK_X
) {
552 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
554 writeMask
&= ~WRITEMASK_X
;
556 if (writeMask
& WRITEMASK_Y
) {
557 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
559 writeMask
&= ~WRITEMASK_Y
;
561 if (writeMask
& WRITEMASK_Z
) {
562 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
564 writeMask
&= ~WRITEMASK_Z
;
566 if (writeMask
& WRITEMASK_W
) {
567 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
569 writeMask
&= ~WRITEMASK_W
;
573 if (writeMask
& WRITEMASK_X
)
575 if (writeMask
& WRITEMASK_Y
)
577 if (writeMask
& WRITEMASK_Z
)
579 if (writeMask
& WRITEMASK_W
)
582 if (inst
->CondUpdate
) {
583 if (writeMask
& WRITEMASK_X
)
584 machine
->CondCodes
[0] = generate_cc((float)value
[0]);
585 if (writeMask
& WRITEMASK_Y
)
586 machine
->CondCodes
[1] = generate_cc((float)value
[1]);
587 if (writeMask
& WRITEMASK_Z
)
588 machine
->CondCodes
[2] = generate_cc((float)value
[2]);
589 if (writeMask
& WRITEMASK_W
)
590 machine
->CondCodes
[3] = generate_cc((float)value
[3]);
592 printf("CondCodes=(%s,%s,%s,%s) for:\n",
593 _mesa_condcode_string(machine
->CondCodes
[0]),
594 _mesa_condcode_string(machine
->CondCodes
[1]),
595 _mesa_condcode_string(machine
->CondCodes
[2]),
596 _mesa_condcode_string(machine
->CondCodes
[3]));
604 * Execute the given vertex/fragment program.
606 * \param ctx rendering context
607 * \param program the program to execute
608 * \param machine machine state (must be initialized)
609 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
612 _mesa_execute_program(struct gl_context
* ctx
,
613 const struct gl_program
*program
,
614 struct gl_program_machine
*machine
)
616 const GLuint numInst
= program
->NumInstructions
;
617 const GLuint maxExec
= 65536;
618 GLuint pc
, numExec
= 0;
620 machine
->CurProgram
= program
;
623 printf("execute program %u --------------------\n", program
->Id
);
626 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
627 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
630 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
633 for (pc
= 0; pc
< numInst
; pc
++) {
634 const struct prog_instruction
*inst
= program
->Instructions
+ pc
;
637 _mesa_print_instruction(inst
);
640 switch (inst
->Opcode
) {
643 GLfloat a
[4], result
[4];
644 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
645 result
[0] = FABSF(a
[0]);
646 result
[1] = FABSF(a
[1]);
647 result
[2] = FABSF(a
[2]);
648 result
[3] = FABSF(a
[3]);
649 store_vector4(inst
, machine
, result
);
654 GLfloat a
[4], b
[4], result
[4];
655 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
656 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
657 result
[0] = a
[0] + b
[0];
658 result
[1] = a
[1] + b
[1];
659 result
[2] = a
[2] + b
[2];
660 result
[3] = a
[3] + b
[3];
661 store_vector4(inst
, machine
, result
);
663 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
664 result
[0], result
[1], result
[2], result
[3],
665 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
669 case OPCODE_AND
: /* bitwise AND */
671 GLuint a
[4], b
[4], result
[4];
672 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
673 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
674 result
[0] = a
[0] & b
[0];
675 result
[1] = a
[1] & b
[1];
676 result
[2] = a
[2] & b
[2];
677 result
[3] = a
[3] & b
[3];
678 store_vector4ui(inst
, machine
, result
);
684 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
685 machine
->AddressReg
[0][0] = IFLOOR(t
[0]);
687 printf("ARL %d\n", machine
->AddressReg
[0][0]);
693 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
697 /* subtract 1 here since pc is incremented by for(pc) loop */
698 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
700 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
702 case OPCODE_BGNSUB
: /* begin subroutine */
704 case OPCODE_ENDSUB
: /* end subroutine */
706 case OPCODE_BRK
: /* break out of loop (conditional) */
707 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
709 if (eval_condition(machine
, inst
)) {
710 /* break out of loop */
711 /* pc++ at end of for-loop will put us after the ENDLOOP inst */
712 pc
= inst
->BranchTarget
;
715 case OPCODE_CONT
: /* continue loop (conditional) */
716 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
718 if (eval_condition(machine
, inst
)) {
719 /* continue at ENDLOOP */
720 /* Subtract 1 here since we'll do pc++ at end of for-loop */
721 pc
= inst
->BranchTarget
- 1;
724 case OPCODE_CAL
: /* Call subroutine (conditional) */
725 if (eval_condition(machine
, inst
)) {
726 /* call the subroutine */
727 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
728 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
730 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1; /* next inst */
731 /* Subtract 1 here since we'll do pc++ at end of for-loop */
732 pc
= inst
->BranchTarget
- 1;
737 GLfloat a
[4], b
[4], c
[4], result
[4];
738 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
739 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
740 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
741 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
742 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
743 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
744 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
745 store_vector4(inst
, machine
, result
);
747 printf("CMP (%g %g %g %g) = (%g %g %g %g) < 0 ? (%g %g %g %g) : (%g %g %g %g)\n",
748 result
[0], result
[1], result
[2], result
[3],
749 a
[0], a
[1], a
[2], a
[3],
750 b
[0], b
[1], b
[2], b
[3],
751 c
[0], c
[1], c
[2], c
[3]);
757 GLfloat a
[4], result
[4];
758 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
759 result
[0] = result
[1] = result
[2] = result
[3]
760 = (GLfloat
) cos(a
[0]);
761 store_vector4(inst
, machine
, result
);
764 case OPCODE_DDX
: /* Partial derivative with respect to X */
767 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
769 store_vector4(inst
, machine
, result
);
772 case OPCODE_DDY
: /* Partial derivative with respect to Y */
775 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
777 store_vector4(inst
, machine
, result
);
782 GLfloat a
[4], b
[4], result
[4];
783 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
784 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
785 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
);
786 store_vector4(inst
, machine
, result
);
788 printf("DP2 %g = (%g %g) . (%g %g)\n",
789 result
[0], a
[0], a
[1], b
[0], b
[1]);
795 GLfloat a
[4], b
[4], c
, result
[4];
796 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
797 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
798 fetch_vector1(&inst
->SrcReg
[1], machine
, &c
);
799 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
) + c
;
800 store_vector4(inst
, machine
, result
);
802 printf("DP2A %g = (%g %g) . (%g %g) + %g\n",
803 result
[0], a
[0], a
[1], b
[0], b
[1], c
);
809 GLfloat a
[4], b
[4], result
[4];
810 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
811 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
812 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
813 store_vector4(inst
, machine
, result
);
815 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
816 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
822 GLfloat a
[4], b
[4], result
[4];
823 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
824 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
825 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
, b
);
826 store_vector4(inst
, machine
, result
);
828 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
829 result
[0], a
[0], a
[1], a
[2], a
[3],
830 b
[0], b
[1], b
[2], b
[3]);
836 GLfloat a
[4], b
[4], result
[4];
837 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
838 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
839 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
) + b
[3];
840 store_vector4(inst
, machine
, result
);
843 case OPCODE_DST
: /* Distance vector */
845 GLfloat a
[4], b
[4], result
[4];
846 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
847 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
849 result
[1] = a
[1] * b
[1];
852 store_vector4(inst
, machine
, result
);
857 GLfloat t
[4], q
[4], floor_t0
;
858 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
859 floor_t0
= FLOORF(t
[0]);
860 if (floor_t0
> FLT_MAX_EXP
) {
861 SET_POS_INFINITY(q
[0]);
862 SET_POS_INFINITY(q
[2]);
864 else if (floor_t0
< FLT_MIN_EXP
) {
869 q
[0] = LDEXPF(1.0, (int) floor_t0
);
870 /* Note: GL_NV_vertex_program expects
871 * result.z = result.x * APPX(result.y)
872 * We do what the ARB extension says.
874 q
[2] = (GLfloat
) pow(2.0, t
[0]);
876 q
[1] = t
[0] - floor_t0
;
878 store_vector4( inst
, machine
, q
);
881 case OPCODE_EX2
: /* Exponential base 2 */
883 GLfloat a
[4], result
[4], val
;
884 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
885 val
= (GLfloat
) pow(2.0, a
[0]);
887 if (IS_INF_OR_NAN(val))
890 result
[0] = result
[1] = result
[2] = result
[3] = val
;
891 store_vector4(inst
, machine
, result
);
896 GLfloat a
[4], result
[4];
897 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
898 result
[0] = FLOORF(a
[0]);
899 result
[1] = FLOORF(a
[1]);
900 result
[2] = FLOORF(a
[2]);
901 result
[3] = FLOORF(a
[3]);
902 store_vector4(inst
, machine
, result
);
907 GLfloat a
[4], result
[4];
908 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
909 result
[0] = a
[0] - FLOORF(a
[0]);
910 result
[1] = a
[1] - FLOORF(a
[1]);
911 result
[2] = a
[2] - FLOORF(a
[2]);
912 result
[3] = a
[3] - FLOORF(a
[3]);
913 store_vector4(inst
, machine
, result
);
919 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
921 program
->Instructions
[inst
->BranchTarget
].Opcode
924 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
926 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
927 cond
= (a
[0] != 0.0);
930 cond
= eval_condition(machine
, inst
);
933 printf("IF: %d\n", cond
);
937 /* do if-clause (just continue execution) */
940 /* go to the instruction after ELSE or ENDIF */
941 assert(inst
->BranchTarget
>= 0);
942 pc
= inst
->BranchTarget
;
948 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
950 assert(inst
->BranchTarget
>= 0);
951 pc
= inst
->BranchTarget
;
956 case OPCODE_KIL_NV
: /* NV_f_p only (conditional) */
957 if (eval_condition(machine
, inst
)) {
961 case OPCODE_KIL
: /* ARB_f_p only */
964 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
966 printf("KIL if (%g %g %g %g) <= 0.0\n",
967 a
[0], a
[1], a
[2], a
[3]);
970 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
975 case OPCODE_LG2
: /* log base 2 */
977 GLfloat a
[4], result
[4], val
;
978 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
979 /* The fast LOG2 macro doesn't meet the precision requirements.
985 val
= (float)(log(a
[0]) * 1.442695F
);
987 result
[0] = result
[1] = result
[2] = result
[3] = val
;
988 store_vector4(inst
, machine
, result
);
993 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
994 GLfloat a
[4], result
[4];
995 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
996 a
[0] = MAX2(a
[0], 0.0F
);
997 a
[1] = MAX2(a
[1], 0.0F
);
998 /* XXX ARB version clamps a[3], NV version doesn't */
999 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
1002 /* XXX we could probably just use pow() here */
1004 if (a
[1] == 0.0 && a
[3] == 0.0)
1007 result
[2] = (GLfloat
) pow(a
[1], a
[3]);
1013 store_vector4(inst
, machine
, result
);
1015 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
1016 result
[0], result
[1], result
[2], result
[3],
1017 a
[0], a
[1], a
[2], a
[3]);
1023 GLfloat t
[4], q
[4], abs_t0
;
1024 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
1025 abs_t0
= FABSF(t
[0]);
1026 if (abs_t0
!= 0.0F
) {
1027 if (IS_INF_OR_NAN(abs_t0
))
1029 SET_POS_INFINITY(q
[0]);
1031 SET_POS_INFINITY(q
[2]);
1035 GLfloat mantissa
= FREXPF(t
[0], &exponent
);
1036 q
[0] = (GLfloat
) (exponent
- 1);
1037 q
[1] = (GLfloat
) (2.0 * mantissa
); /* map [.5, 1) -> [1, 2) */
1039 /* The fast LOG2 macro doesn't meet the precision
1042 q
[2] = (float)(log(t
[0]) * 1.442695F
);
1046 SET_NEG_INFINITY(q
[0]);
1048 SET_NEG_INFINITY(q
[2]);
1051 store_vector4(inst
, machine
, q
);
1056 GLfloat a
[4], b
[4], c
[4], result
[4];
1057 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1058 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1059 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1060 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
1061 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
1062 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
1063 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
1064 store_vector4(inst
, machine
, result
);
1066 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
1067 "(%g %g %g %g), (%g %g %g %g)\n",
1068 result
[0], result
[1], result
[2], result
[3],
1069 a
[0], a
[1], a
[2], a
[3],
1070 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1076 GLfloat a
[4], b
[4], c
[4], result
[4];
1077 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1078 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1079 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1080 result
[0] = a
[0] * b
[0] + c
[0];
1081 result
[1] = a
[1] * b
[1] + c
[1];
1082 result
[2] = a
[2] * b
[2] + c
[2];
1083 result
[3] = a
[3] * b
[3] + c
[3];
1084 store_vector4(inst
, machine
, result
);
1086 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
1087 "(%g %g %g %g) + (%g %g %g %g)\n",
1088 result
[0], result
[1], result
[2], result
[3],
1089 a
[0], a
[1], a
[2], a
[3],
1090 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1096 GLfloat a
[4], b
[4], result
[4];
1097 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1098 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1099 result
[0] = MAX2(a
[0], b
[0]);
1100 result
[1] = MAX2(a
[1], b
[1]);
1101 result
[2] = MAX2(a
[2], b
[2]);
1102 result
[3] = MAX2(a
[3], b
[3]);
1103 store_vector4(inst
, machine
, result
);
1105 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
1106 result
[0], result
[1], result
[2], result
[3],
1107 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1113 GLfloat a
[4], b
[4], result
[4];
1114 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1115 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1116 result
[0] = MIN2(a
[0], b
[0]);
1117 result
[1] = MIN2(a
[1], b
[1]);
1118 result
[2] = MIN2(a
[2], b
[2]);
1119 result
[3] = MIN2(a
[3], b
[3]);
1120 store_vector4(inst
, machine
, result
);
1126 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
1127 store_vector4(inst
, machine
, result
);
1129 printf("MOV (%g %g %g %g)\n",
1130 result
[0], result
[1], result
[2], result
[3]);
1136 GLfloat a
[4], b
[4], result
[4];
1137 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1138 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1139 result
[0] = a
[0] * b
[0];
1140 result
[1] = a
[1] * b
[1];
1141 result
[2] = a
[2] * b
[2];
1142 result
[3] = a
[3] * b
[3];
1143 store_vector4(inst
, machine
, result
);
1145 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1146 result
[0], result
[1], result
[2], result
[3],
1147 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1153 GLfloat a
[4], result
[4];
1154 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1158 result
[3] = _mesa_noise1(a
[0]);
1159 store_vector4(inst
, machine
, result
);
1164 GLfloat a
[4], result
[4];
1165 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1168 result
[2] = result
[3] = _mesa_noise2(a
[0], a
[1]);
1169 store_vector4(inst
, machine
, result
);
1174 GLfloat a
[4], result
[4];
1175 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1179 result
[3] = _mesa_noise3(a
[0], a
[1], a
[2]);
1180 store_vector4(inst
, machine
, result
);
1185 GLfloat a
[4], result
[4];
1186 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1190 result
[3] = _mesa_noise4(a
[0], a
[1], a
[2], a
[3]);
1191 store_vector4(inst
, machine
, result
);
1196 case OPCODE_NOT
: /* bitwise NOT */
1198 GLuint a
[4], result
[4];
1199 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1204 store_vector4ui(inst
, machine
, result
);
1207 case OPCODE_NRM3
: /* 3-component normalization */
1209 GLfloat a
[4], result
[4];
1211 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1212 tmp
= a
[0] * a
[0] + a
[1] * a
[1] + a
[2] * a
[2];
1214 tmp
= INV_SQRTF(tmp
);
1215 result
[0] = tmp
* a
[0];
1216 result
[1] = tmp
* a
[1];
1217 result
[2] = tmp
* a
[2];
1218 result
[3] = 0.0; /* undefined, but prevent valgrind warnings */
1219 store_vector4(inst
, machine
, result
);
1222 case OPCODE_NRM4
: /* 4-component normalization */
1224 GLfloat a
[4], result
[4];
1226 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1227 tmp
= a
[0] * a
[0] + a
[1] * a
[1] + a
[2] * a
[2] + a
[3] * a
[3];
1229 tmp
= INV_SQRTF(tmp
);
1230 result
[0] = tmp
* a
[0];
1231 result
[1] = tmp
* a
[1];
1232 result
[2] = tmp
* a
[2];
1233 result
[3] = tmp
* a
[3];
1234 store_vector4(inst
, machine
, result
);
1237 case OPCODE_OR
: /* bitwise OR */
1239 GLuint a
[4], b
[4], result
[4];
1240 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1241 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
1242 result
[0] = a
[0] | b
[0];
1243 result
[1] = a
[1] | b
[1];
1244 result
[2] = a
[2] | b
[2];
1245 result
[3] = a
[3] | b
[3];
1246 store_vector4ui(inst
, machine
, result
);
1249 case OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
1254 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1255 hx
= _mesa_float_to_half(a
[0]);
1256 hy
= _mesa_float_to_half(a
[1]);
1260 result
[3] = hx
| (hy
<< 16);
1261 store_vector4ui(inst
, machine
, result
);
1264 case OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
1267 GLuint result
[4], usx
, usy
;
1268 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1269 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1270 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1271 usx
= F_TO_I(a
[0] * 65535.0F
);
1272 usy
= F_TO_I(a
[1] * 65535.0F
);
1276 result
[3] = usx
| (usy
<< 16);
1277 store_vector4ui(inst
, machine
, result
);
1280 case OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
1283 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1284 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1285 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
1286 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
1287 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
1288 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
1289 ubx
= F_TO_I(127.0F
* a
[0] + 128.0F
);
1290 uby
= F_TO_I(127.0F
* a
[1] + 128.0F
);
1291 ubz
= F_TO_I(127.0F
* a
[2] + 128.0F
);
1292 ubw
= F_TO_I(127.0F
* a
[3] + 128.0F
);
1296 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1297 store_vector4ui(inst
, machine
, result
);
1300 case OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
1303 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1304 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1305 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1306 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1307 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
1308 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
1309 ubx
= F_TO_I(255.0F
* a
[0]);
1310 uby
= F_TO_I(255.0F
* a
[1]);
1311 ubz
= F_TO_I(255.0F
* a
[2]);
1312 ubw
= F_TO_I(255.0F
* a
[3]);
1316 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1317 store_vector4ui(inst
, machine
, result
);
1322 GLfloat a
[4], b
[4], result
[4];
1323 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1324 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
1325 result
[0] = result
[1] = result
[2] = result
[3]
1326 = (GLfloat
) pow(a
[0], b
[0]);
1327 store_vector4(inst
, machine
, result
);
1333 GLfloat a
[4], result
[4];
1334 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1338 else if (IS_INF_OR_NAN(a
[0]))
1339 printf("RCP(inf)\n");
1341 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
1342 store_vector4(inst
, machine
, result
);
1345 case OPCODE_RET
: /* return from subroutine (conditional) */
1346 if (eval_condition(machine
, inst
)) {
1347 if (machine
->StackDepth
== 0) {
1348 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
1350 /* subtract one because of pc++ in the for loop */
1351 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
1354 case OPCODE_RFL
: /* reflection vector */
1356 GLfloat axis
[4], dir
[4], result
[4], tmpX
, tmpW
;
1357 fetch_vector4(&inst
->SrcReg
[0], machine
, axis
);
1358 fetch_vector4(&inst
->SrcReg
[1], machine
, dir
);
1359 tmpW
= DOT3(axis
, axis
);
1360 tmpX
= (2.0F
* DOT3(axis
, dir
)) / tmpW
;
1361 result
[0] = tmpX
* axis
[0] - dir
[0];
1362 result
[1] = tmpX
* axis
[1] - dir
[1];
1363 result
[2] = tmpX
* axis
[2] - dir
[2];
1364 /* result[3] is never written! XXX enforce in parser! */
1365 store_vector4(inst
, machine
, result
);
1368 case OPCODE_RSQ
: /* 1 / sqrt() */
1370 GLfloat a
[4], result
[4];
1371 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1373 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1374 store_vector4(inst
, machine
, result
);
1376 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
1380 case OPCODE_SCS
: /* sine and cos */
1382 GLfloat a
[4], result
[4];
1383 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1384 result
[0] = (GLfloat
) cos(a
[0]);
1385 result
[1] = (GLfloat
) sin(a
[0]);
1386 result
[2] = 0.0; /* undefined! */
1387 result
[3] = 0.0; /* undefined! */
1388 store_vector4(inst
, machine
, result
);
1391 case OPCODE_SEQ
: /* set on equal */
1393 GLfloat a
[4], b
[4], result
[4];
1394 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1395 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1396 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1397 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1398 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1399 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1400 store_vector4(inst
, machine
, result
);
1402 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1403 result
[0], result
[1], result
[2], result
[3],
1404 a
[0], a
[1], a
[2], a
[3],
1405 b
[0], b
[1], b
[2], b
[3]);
1409 case OPCODE_SFL
: /* set false, operands ignored */
1411 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1412 store_vector4(inst
, machine
, result
);
1415 case OPCODE_SGE
: /* set on greater or equal */
1417 GLfloat a
[4], b
[4], result
[4];
1418 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1419 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1420 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1421 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1422 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1423 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1424 store_vector4(inst
, machine
, result
);
1426 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1427 result
[0], result
[1], result
[2], result
[3],
1428 a
[0], a
[1], a
[2], a
[3],
1429 b
[0], b
[1], b
[2], b
[3]);
1433 case OPCODE_SGT
: /* set on greater */
1435 GLfloat a
[4], b
[4], result
[4];
1436 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1437 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1438 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1439 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1440 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1441 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1442 store_vector4(inst
, machine
, result
);
1444 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1445 result
[0], result
[1], result
[2], result
[3],
1446 a
[0], a
[1], a
[2], a
[3],
1447 b
[0], b
[1], b
[2], b
[3]);
1453 GLfloat a
[4], result
[4];
1454 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1455 result
[0] = result
[1] = result
[2] = result
[3]
1456 = (GLfloat
) sin(a
[0]);
1457 store_vector4(inst
, machine
, result
);
1460 case OPCODE_SLE
: /* set on less or equal */
1462 GLfloat a
[4], b
[4], result
[4];
1463 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1464 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1465 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1466 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1467 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1468 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1469 store_vector4(inst
, machine
, result
);
1471 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1472 result
[0], result
[1], result
[2], result
[3],
1473 a
[0], a
[1], a
[2], a
[3],
1474 b
[0], b
[1], b
[2], b
[3]);
1478 case OPCODE_SLT
: /* set on less */
1480 GLfloat a
[4], b
[4], result
[4];
1481 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1482 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1483 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1484 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1485 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1486 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1487 store_vector4(inst
, machine
, result
);
1489 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1490 result
[0], result
[1], result
[2], result
[3],
1491 a
[0], a
[1], a
[2], a
[3],
1492 b
[0], b
[1], b
[2], b
[3]);
1496 case OPCODE_SNE
: /* set on not equal */
1498 GLfloat a
[4], b
[4], result
[4];
1499 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1500 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1501 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1502 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1503 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1504 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1505 store_vector4(inst
, machine
, result
);
1507 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1508 result
[0], result
[1], result
[2], result
[3],
1509 a
[0], a
[1], a
[2], a
[3],
1510 b
[0], b
[1], b
[2], b
[3]);
1514 case OPCODE_SSG
: /* set sign (-1, 0 or +1) */
1516 GLfloat a
[4], result
[4];
1517 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1518 result
[0] = (GLfloat
) ((a
[0] > 0.0F
) - (a
[0] < 0.0F
));
1519 result
[1] = (GLfloat
) ((a
[1] > 0.0F
) - (a
[1] < 0.0F
));
1520 result
[2] = (GLfloat
) ((a
[2] > 0.0F
) - (a
[2] < 0.0F
));
1521 result
[3] = (GLfloat
) ((a
[3] > 0.0F
) - (a
[3] < 0.0F
));
1522 store_vector4(inst
, machine
, result
);
1525 case OPCODE_STR
: /* set true, operands ignored */
1527 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1528 store_vector4(inst
, machine
, result
);
1533 GLfloat a
[4], b
[4], result
[4];
1534 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1535 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1536 result
[0] = a
[0] - b
[0];
1537 result
[1] = a
[1] - b
[1];
1538 result
[2] = a
[2] - b
[2];
1539 result
[3] = a
[3] - b
[3];
1540 store_vector4(inst
, machine
, result
);
1542 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1543 result
[0], result
[1], result
[2], result
[3],
1544 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1548 case OPCODE_SWZ
: /* extended swizzle */
1550 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1551 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
1554 for (i
= 0; i
< 4; i
++) {
1555 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1556 if (swz
== SWIZZLE_ZERO
)
1558 else if (swz
== SWIZZLE_ONE
)
1563 result
[i
] = src
[swz
];
1565 if (source
->Negate
& (1 << i
))
1566 result
[i
] = -result
[i
];
1568 store_vector4(inst
, machine
, result
);
1571 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1572 /* Simple texel lookup */
1574 GLfloat texcoord
[4], color
[4];
1575 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1577 /* For TEX, texcoord.Q should not be used and its value should not
1578 * matter (at most, we pass coord.xyz to texture3D() in GLSL).
1579 * Set Q=1 so that FetchTexelDeriv() doesn't get a garbage value
1580 * which is effectively what happens when the texcoord swizzle
1585 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1588 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1589 color
[0], color
[1], color
[2], color
[3],
1591 texcoord
[0], texcoord
[1], texcoord
[2], texcoord
[3]);
1593 store_vector4(inst
, machine
, color
);
1596 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1597 /* Texel lookup with LOD bias */
1599 GLfloat texcoord
[4], color
[4], lodBias
;
1601 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1603 /* texcoord[3] is the bias to add to lambda */
1604 lodBias
= texcoord
[3];
1606 fetch_texel(ctx
, machine
, inst
, texcoord
, lodBias
, color
);
1609 printf("TXB (%g, %g, %g, %g) = texture[%d][%g %g %g %g]"
1611 color
[0], color
[1], color
[2], color
[3],
1620 store_vector4(inst
, machine
, color
);
1623 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1624 /* Texture lookup w/ partial derivatives for LOD */
1626 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1627 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1628 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1629 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1630 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1632 inst
->TexSrcUnit
, color
);
1633 store_vector4(inst
, machine
, color
);
1637 /* Texel lookup with explicit LOD */
1639 GLfloat texcoord
[4], color
[4], lod
;
1641 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1643 /* texcoord[3] is the LOD */
1646 machine
->FetchTexelLod(ctx
, texcoord
, lod
,
1647 machine
->Samplers
[inst
->TexSrcUnit
], color
);
1649 store_vector4(inst
, machine
, color
);
1652 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1653 /* Texture lookup w/ projective divide */
1655 GLfloat texcoord
[4], color
[4];
1657 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1658 /* Not so sure about this test - if texcoord[3] is
1659 * zero, we'd probably be fine except for an ASSERT in
1660 * IROUND_POS() which gets triggered by the inf values created.
1662 if (texcoord
[3] != 0.0) {
1663 texcoord
[0] /= texcoord
[3];
1664 texcoord
[1] /= texcoord
[3];
1665 texcoord
[2] /= texcoord
[3];
1668 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1670 store_vector4(inst
, machine
, color
);
1673 case OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1674 /* Texture lookup w/ projective divide, as above, but do not
1675 * do the divide by w if sampling from a cube map.
1678 GLfloat texcoord
[4], color
[4];
1680 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1681 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1682 texcoord
[3] != 0.0) {
1683 texcoord
[0] /= texcoord
[3];
1684 texcoord
[1] /= texcoord
[3];
1685 texcoord
[2] /= texcoord
[3];
1688 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1690 store_vector4(inst
, machine
, color
);
1693 case OPCODE_TRUNC
: /* truncate toward zero */
1695 GLfloat a
[4], result
[4];
1696 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1697 result
[0] = (GLfloat
) (GLint
) a
[0];
1698 result
[1] = (GLfloat
) (GLint
) a
[1];
1699 result
[2] = (GLfloat
) (GLint
) a
[2];
1700 result
[3] = (GLfloat
) (GLint
) a
[3];
1701 store_vector4(inst
, machine
, result
);
1704 case OPCODE_UP2H
: /* unpack two 16-bit floats */
1706 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1711 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1712 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1713 store_vector4(inst
, machine
, result
);
1716 case OPCODE_UP2US
: /* unpack two GLushorts */
1718 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1723 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1724 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1725 store_vector4(inst
, machine
, result
);
1728 case OPCODE_UP4B
: /* unpack four GLbytes */
1730 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1732 result
[0] = (((raw
>> 0) & 0xff) - 128) / 127.0F
;
1733 result
[1] = (((raw
>> 8) & 0xff) - 128) / 127.0F
;
1734 result
[2] = (((raw
>> 16) & 0xff) - 128) / 127.0F
;
1735 result
[3] = (((raw
>> 24) & 0xff) - 128) / 127.0F
;
1736 store_vector4(inst
, machine
, result
);
1739 case OPCODE_UP4UB
: /* unpack four GLubytes */
1741 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1743 result
[0] = ((raw
>> 0) & 0xff) / 255.0F
;
1744 result
[1] = ((raw
>> 8) & 0xff) / 255.0F
;
1745 result
[2] = ((raw
>> 16) & 0xff) / 255.0F
;
1746 result
[3] = ((raw
>> 24) & 0xff) / 255.0F
;
1747 store_vector4(inst
, machine
, result
);
1750 case OPCODE_XOR
: /* bitwise XOR */
1752 GLuint a
[4], b
[4], result
[4];
1753 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1754 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
1755 result
[0] = a
[0] ^ b
[0];
1756 result
[1] = a
[1] ^ b
[1];
1757 result
[2] = a
[2] ^ b
[2];
1758 result
[3] = a
[3] ^ b
[3];
1759 store_vector4ui(inst
, machine
, result
);
1762 case OPCODE_XPD
: /* cross product */
1764 GLfloat a
[4], b
[4], result
[4];
1765 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1766 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1767 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1768 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1769 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1771 store_vector4(inst
, machine
, result
);
1773 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1774 result
[0], result
[1], result
[2], result
[3],
1775 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1779 case OPCODE_X2D
: /* 2-D matrix transform */
1781 GLfloat a
[4], b
[4], c
[4], result
[4];
1782 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1783 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1784 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1785 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1786 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1787 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1788 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1789 store_vector4(inst
, machine
, result
);
1794 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
1796 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1797 printf("%s%g, %g, %g, %g\n", (const char *) inst
->Data
,
1798 a
[0], a
[1], a
[2], a
[3]);
1801 printf("%s\n", (const char *) inst
->Data
);
1808 _mesa_problem(ctx
, "Bad opcode %d in _mesa_execute_program",
1810 return GL_TRUE
; /* return value doesn't matter */
1814 if (numExec
> maxExec
) {
1815 static GLboolean reported
= GL_FALSE
;
1817 _mesa_problem(ctx
, "Infinite loop detected in fragment program");