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"
41 #include "prog_execute.h"
42 #include "prog_instruction.h"
43 #include "prog_parameter.h"
44 #include "prog_print.h"
45 #include "prog_noise.h"
53 * Set x to positive or negative infinity.
55 #if defined(USE_IEEE) || defined(_WIN32)
56 #define SET_POS_INFINITY(x) \
62 #define SET_NEG_INFINITY(x) \
69 #define SET_POS_INFINITY(x) x = __MAXFLOAT
70 #define SET_NEG_INFINITY(x) x = -__MAXFLOAT
72 #define SET_POS_INFINITY(x) x = (GLfloat) HUGE_VAL
73 #define SET_NEG_INFINITY(x) x = (GLfloat) -HUGE_VAL
76 #define SET_FLOAT_BITS(x, bits) ((fi_type *) (void *) &(x))->i = bits
79 static const GLfloat ZeroVec
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
84 * Return a pointer to the 4-element float vector specified by the given
87 static INLINE
const GLfloat
*
88 get_src_register_pointer(const struct prog_src_register
*source
,
89 const struct gl_program_machine
*machine
)
91 const struct gl_program
*prog
= machine
->CurProgram
;
92 GLint reg
= source
->Index
;
94 if (source
->RelAddr
) {
95 /* add address register value to src index/offset */
96 reg
+= machine
->AddressReg
[0][0];
102 switch (source
->File
) {
103 case PROGRAM_TEMPORARY
:
104 if (reg
>= MAX_PROGRAM_TEMPS
)
106 return machine
->Temporaries
[reg
];
109 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
110 if (reg
>= VERT_ATTRIB_MAX
)
112 return machine
->VertAttribs
[reg
];
115 if (reg
>= FRAG_ATTRIB_MAX
)
117 return machine
->Attribs
[reg
][machine
->CurElement
];
121 if (reg
>= MAX_PROGRAM_OUTPUTS
)
123 return machine
->Outputs
[reg
];
125 case PROGRAM_LOCAL_PARAM
:
126 if (reg
>= MAX_PROGRAM_LOCAL_PARAMS
)
128 return machine
->CurProgram
->LocalParams
[reg
];
130 case PROGRAM_ENV_PARAM
:
131 if (reg
>= MAX_PROGRAM_ENV_PARAMS
)
133 return machine
->EnvParams
[reg
];
135 case PROGRAM_STATE_VAR
:
137 case PROGRAM_CONSTANT
:
139 case PROGRAM_UNIFORM
:
141 case PROGRAM_NAMED_PARAM
:
142 if (reg
>= (GLint
) prog
->Parameters
->NumParameters
)
144 return prog
->Parameters
->ParameterValues
[reg
];
148 "Invalid src register file %d in get_src_register_pointer()",
156 * Return a pointer to the 4-element float vector specified by the given
157 * destination register.
159 static INLINE GLfloat
*
160 get_dst_register_pointer(const struct prog_dst_register
*dest
,
161 struct gl_program_machine
*machine
)
163 static GLfloat dummyReg
[4];
164 GLint reg
= dest
->Index
;
167 /* add address register value to src index/offset */
168 reg
+= machine
->AddressReg
[0][0];
174 switch (dest
->File
) {
175 case PROGRAM_TEMPORARY
:
176 if (reg
>= MAX_PROGRAM_TEMPS
)
178 return machine
->Temporaries
[reg
];
181 if (reg
>= MAX_PROGRAM_OUTPUTS
)
183 return machine
->Outputs
[reg
];
185 case PROGRAM_WRITE_ONLY
:
190 "Invalid dest register file %d in get_dst_register_pointer()",
199 * Fetch a 4-element float vector from the given source register.
200 * Apply swizzling and negating as needed.
203 fetch_vector4(const struct prog_src_register
*source
,
204 const struct gl_program_machine
*machine
, GLfloat result
[4])
206 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
209 if (source
->Swizzle
== SWIZZLE_NOOP
) {
211 COPY_4V(result
, src
);
214 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
215 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
216 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
217 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
218 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
219 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
220 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
221 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
225 result
[0] = FABSF(result
[0]);
226 result
[1] = FABSF(result
[1]);
227 result
[2] = FABSF(result
[2]);
228 result
[3] = FABSF(result
[3]);
230 if (source
->Negate
) {
231 ASSERT(source
->Negate
== NEGATE_XYZW
);
232 result
[0] = -result
[0];
233 result
[1] = -result
[1];
234 result
[2] = -result
[2];
235 result
[3] = -result
[3];
239 assert(!IS_INF_OR_NAN(result
[0]));
240 assert(!IS_INF_OR_NAN(result
[0]));
241 assert(!IS_INF_OR_NAN(result
[0]));
242 assert(!IS_INF_OR_NAN(result
[0]));
248 * Fetch a 4-element uint vector from the given source register.
249 * Apply swizzling but not negation/abs.
252 fetch_vector4ui(const struct prog_src_register
*source
,
253 const struct gl_program_machine
*machine
, GLuint result
[4])
255 const GLuint
*src
= (GLuint
*) get_src_register_pointer(source
, machine
);
258 if (source
->Swizzle
== SWIZZLE_NOOP
) {
260 COPY_4V(result
, src
);
263 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
264 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
265 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
266 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
267 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
268 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
269 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
270 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
273 /* Note: no Negate or Abs here */
279 * Fetch the derivative with respect to X or Y for the given register.
280 * XXX this currently only works for fragment program input attribs.
283 fetch_vector4_deriv(GLcontext
* ctx
,
284 const struct prog_src_register
*source
,
285 const struct gl_program_machine
*machine
,
286 char xOrY
, GLfloat result
[4])
288 if (source
->File
== PROGRAM_INPUT
&&
289 source
->Index
< (GLint
) machine
->NumDeriv
) {
290 const GLint col
= machine
->CurElement
;
291 const GLfloat w
= machine
->Attribs
[FRAG_ATTRIB_WPOS
][col
][3];
292 const GLfloat invQ
= 1.0f
/ w
;
296 deriv
[0] = machine
->DerivX
[source
->Index
][0] * invQ
;
297 deriv
[1] = machine
->DerivX
[source
->Index
][1] * invQ
;
298 deriv
[2] = machine
->DerivX
[source
->Index
][2] * invQ
;
299 deriv
[3] = machine
->DerivX
[source
->Index
][3] * invQ
;
302 deriv
[0] = machine
->DerivY
[source
->Index
][0] * invQ
;
303 deriv
[1] = machine
->DerivY
[source
->Index
][1] * invQ
;
304 deriv
[2] = machine
->DerivY
[source
->Index
][2] * invQ
;
305 deriv
[3] = machine
->DerivY
[source
->Index
][3] * invQ
;
308 result
[0] = deriv
[GET_SWZ(source
->Swizzle
, 0)];
309 result
[1] = deriv
[GET_SWZ(source
->Swizzle
, 1)];
310 result
[2] = deriv
[GET_SWZ(source
->Swizzle
, 2)];
311 result
[3] = deriv
[GET_SWZ(source
->Swizzle
, 3)];
314 result
[0] = FABSF(result
[0]);
315 result
[1] = FABSF(result
[1]);
316 result
[2] = FABSF(result
[2]);
317 result
[3] = FABSF(result
[3]);
319 if (source
->Negate
) {
320 ASSERT(source
->Negate
== NEGATE_XYZW
);
321 result
[0] = -result
[0];
322 result
[1] = -result
[1];
323 result
[2] = -result
[2];
324 result
[3] = -result
[3];
328 ASSIGN_4V(result
, 0.0, 0.0, 0.0, 0.0);
334 * As above, but only return result[0] element.
337 fetch_vector1(const struct prog_src_register
*source
,
338 const struct gl_program_machine
*machine
, GLfloat result
[4])
340 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
343 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
346 result
[0] = FABSF(result
[0]);
348 if (source
->Negate
) {
349 result
[0] = -result
[0];
355 fetch_vector1ui(const struct prog_src_register
*source
,
356 const struct gl_program_machine
*machine
)
358 const GLuint
*src
= (GLuint
*) get_src_register_pointer(source
, machine
);
363 result
= src
[GET_SWZ(source
->Swizzle
, 0)];
366 result
= FABSF(result
);
368 if (source
->Negate
) {
377 * Fetch texel from texture. Use partial derivatives when possible.
380 fetch_texel(GLcontext
*ctx
,
381 const struct gl_program_machine
*machine
,
382 const struct prog_instruction
*inst
,
383 const GLfloat texcoord
[4], GLfloat lodBias
,
386 const GLuint unit
= machine
->Samplers
[inst
->TexSrcUnit
];
388 /* Note: we only have the right derivatives for fragment input attribs.
390 if (machine
->NumDeriv
> 0 &&
391 inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
392 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
) {
393 /* simple texture fetch for which we should have derivatives */
394 GLuint attr
= inst
->SrcReg
[0].Index
;
395 machine
->FetchTexelDeriv(ctx
, texcoord
,
396 machine
->DerivX
[attr
],
397 machine
->DerivY
[attr
],
398 lodBias
, unit
, color
);
401 machine
->FetchTexelLod(ctx
, texcoord
, lodBias
, unit
, color
);
407 * Test value against zero and return GT, LT, EQ or UN if NaN.
410 generate_cc(float value
)
413 return COND_UN
; /* NaN */
423 * Test if the ccMaskRule is satisfied by the given condition code.
424 * Used to mask destination writes according to the current condition code.
426 static INLINE GLboolean
427 test_cc(GLuint condCode
, GLuint ccMaskRule
)
429 switch (ccMaskRule
) {
430 case COND_EQ
: return (condCode
== COND_EQ
);
431 case COND_NE
: return (condCode
!= COND_EQ
);
432 case COND_LT
: return (condCode
== COND_LT
);
433 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
434 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
435 case COND_GT
: return (condCode
== COND_GT
);
436 case COND_TR
: return GL_TRUE
;
437 case COND_FL
: return GL_FALSE
;
438 default: return GL_TRUE
;
444 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
445 * or GL_FALSE to indicate result.
447 static INLINE GLboolean
448 eval_condition(const struct gl_program_machine
*machine
,
449 const struct prog_instruction
*inst
)
451 const GLuint swizzle
= inst
->DstReg
.CondSwizzle
;
452 const GLuint condMask
= inst
->DstReg
.CondMask
;
453 if (test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 0)], condMask
) ||
454 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 1)], condMask
) ||
455 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 2)], condMask
) ||
456 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 3)], condMask
)) {
467 * Store 4 floats into a register. Observe the instructions saturate and
468 * set-condition-code flags.
471 store_vector4(const struct prog_instruction
*inst
,
472 struct gl_program_machine
*machine
, const GLfloat value
[4])
474 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
475 const GLboolean clamp
= inst
->SaturateMode
== SATURATE_ZERO_ONE
;
476 GLuint writeMask
= dstReg
->WriteMask
;
477 GLfloat clampedValue
[4];
478 GLfloat
*dst
= get_dst_register_pointer(dstReg
, machine
);
481 if (value
[0] > 1.0e10
||
482 IS_INF_OR_NAN(value
[0]) ||
483 IS_INF_OR_NAN(value
[1]) ||
484 IS_INF_OR_NAN(value
[2]) || IS_INF_OR_NAN(value
[3]))
485 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
489 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
490 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
491 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
492 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
493 value
= clampedValue
;
496 if (dstReg
->CondMask
!= COND_TR
) {
497 /* condition codes may turn off some writes */
498 if (writeMask
& WRITEMASK_X
) {
499 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
501 writeMask
&= ~WRITEMASK_X
;
503 if (writeMask
& WRITEMASK_Y
) {
504 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
506 writeMask
&= ~WRITEMASK_Y
;
508 if (writeMask
& WRITEMASK_Z
) {
509 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
511 writeMask
&= ~WRITEMASK_Z
;
513 if (writeMask
& WRITEMASK_W
) {
514 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
516 writeMask
&= ~WRITEMASK_W
;
521 assert(!IS_INF_OR_NAN(value
[0]));
522 assert(!IS_INF_OR_NAN(value
[0]));
523 assert(!IS_INF_OR_NAN(value
[0]));
524 assert(!IS_INF_OR_NAN(value
[0]));
527 if (writeMask
& WRITEMASK_X
)
529 if (writeMask
& WRITEMASK_Y
)
531 if (writeMask
& WRITEMASK_Z
)
533 if (writeMask
& WRITEMASK_W
)
536 if (inst
->CondUpdate
) {
537 if (writeMask
& WRITEMASK_X
)
538 machine
->CondCodes
[0] = generate_cc(value
[0]);
539 if (writeMask
& WRITEMASK_Y
)
540 machine
->CondCodes
[1] = generate_cc(value
[1]);
541 if (writeMask
& WRITEMASK_Z
)
542 machine
->CondCodes
[2] = generate_cc(value
[2]);
543 if (writeMask
& WRITEMASK_W
)
544 machine
->CondCodes
[3] = generate_cc(value
[3]);
546 printf("CondCodes=(%s,%s,%s,%s) for:\n",
547 _mesa_condcode_string(machine
->CondCodes
[0]),
548 _mesa_condcode_string(machine
->CondCodes
[1]),
549 _mesa_condcode_string(machine
->CondCodes
[2]),
550 _mesa_condcode_string(machine
->CondCodes
[3]));
557 * Store 4 uints into a register. Observe the set-condition-code flags.
560 store_vector4ui(const struct prog_instruction
*inst
,
561 struct gl_program_machine
*machine
, const GLuint value
[4])
563 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
564 GLuint writeMask
= dstReg
->WriteMask
;
565 GLuint
*dst
= (GLuint
*) get_dst_register_pointer(dstReg
, machine
);
567 if (dstReg
->CondMask
!= COND_TR
) {
568 /* condition codes may turn off some writes */
569 if (writeMask
& WRITEMASK_X
) {
570 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
572 writeMask
&= ~WRITEMASK_X
;
574 if (writeMask
& WRITEMASK_Y
) {
575 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
577 writeMask
&= ~WRITEMASK_Y
;
579 if (writeMask
& WRITEMASK_Z
) {
580 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
582 writeMask
&= ~WRITEMASK_Z
;
584 if (writeMask
& WRITEMASK_W
) {
585 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
587 writeMask
&= ~WRITEMASK_W
;
591 if (writeMask
& WRITEMASK_X
)
593 if (writeMask
& WRITEMASK_Y
)
595 if (writeMask
& WRITEMASK_Z
)
597 if (writeMask
& WRITEMASK_W
)
600 if (inst
->CondUpdate
) {
601 if (writeMask
& WRITEMASK_X
)
602 machine
->CondCodes
[0] = generate_cc(value
[0]);
603 if (writeMask
& WRITEMASK_Y
)
604 machine
->CondCodes
[1] = generate_cc(value
[1]);
605 if (writeMask
& WRITEMASK_Z
)
606 machine
->CondCodes
[2] = generate_cc(value
[2]);
607 if (writeMask
& WRITEMASK_W
)
608 machine
->CondCodes
[3] = generate_cc(value
[3]);
610 printf("CondCodes=(%s,%s,%s,%s) for:\n",
611 _mesa_condcode_string(machine
->CondCodes
[0]),
612 _mesa_condcode_string(machine
->CondCodes
[1]),
613 _mesa_condcode_string(machine
->CondCodes
[2]),
614 _mesa_condcode_string(machine
->CondCodes
[3]));
622 * Execute the given vertex/fragment program.
624 * \param ctx rendering context
625 * \param program the program to execute
626 * \param machine machine state (must be initialized)
627 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
630 _mesa_execute_program(GLcontext
* ctx
,
631 const struct gl_program
*program
,
632 struct gl_program_machine
*machine
)
634 const GLuint numInst
= program
->NumInstructions
;
635 const GLuint maxExec
= 10000;
636 GLuint pc
, numExec
= 0;
638 machine
->CurProgram
= program
;
641 printf("execute program %u --------------------\n", program
->Id
);
644 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
645 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
648 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
651 for (pc
= 0; pc
< numInst
; pc
++) {
652 const struct prog_instruction
*inst
= program
->Instructions
+ pc
;
655 _mesa_print_instruction(inst
);
658 switch (inst
->Opcode
) {
661 GLfloat a
[4], result
[4];
662 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
663 result
[0] = FABSF(a
[0]);
664 result
[1] = FABSF(a
[1]);
665 result
[2] = FABSF(a
[2]);
666 result
[3] = FABSF(a
[3]);
667 store_vector4(inst
, machine
, result
);
672 GLfloat a
[4], b
[4], result
[4];
673 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
674 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
675 result
[0] = a
[0] + b
[0];
676 result
[1] = a
[1] + b
[1];
677 result
[2] = a
[2] + b
[2];
678 result
[3] = a
[3] + b
[3];
679 store_vector4(inst
, machine
, result
);
681 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
682 result
[0], result
[1], result
[2], result
[3],
683 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
687 case OPCODE_AND
: /* bitwise AND */
689 GLuint a
[4], b
[4], result
[4];
690 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
691 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
692 result
[0] = a
[0] & b
[0];
693 result
[1] = a
[1] & b
[1];
694 result
[2] = a
[2] & b
[2];
695 result
[3] = a
[3] & b
[3];
696 store_vector4ui(inst
, machine
, result
);
702 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
703 machine
->AddressReg
[0][0] = IFLOOR(t
[0]);
708 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
712 /* subtract 1 here since pc is incremented by for(pc) loop */
713 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
715 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
717 case OPCODE_BGNSUB
: /* begin subroutine */
719 case OPCODE_ENDSUB
: /* end subroutine */
721 case OPCODE_BRA
: /* branch (conditional) */
722 if (eval_condition(machine
, inst
)) {
724 /* Subtract 1 here since we'll do pc++ below */
725 pc
= inst
->BranchTarget
- 1;
728 case OPCODE_BRK
: /* break out of loop (conditional) */
729 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
731 if (eval_condition(machine
, inst
)) {
732 /* break out of loop */
733 /* pc++ at end of for-loop will put us after the ENDLOOP inst */
734 pc
= inst
->BranchTarget
;
737 case OPCODE_CONT
: /* continue loop (conditional) */
738 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
740 if (eval_condition(machine
, inst
)) {
741 /* continue at ENDLOOP */
742 /* Subtract 1 here since we'll do pc++ at end of for-loop */
743 pc
= inst
->BranchTarget
- 1;
746 case OPCODE_CAL
: /* Call subroutine (conditional) */
747 if (eval_condition(machine
, inst
)) {
748 /* call the subroutine */
749 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
750 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
752 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1; /* next inst */
753 /* Subtract 1 here since we'll do pc++ at end of for-loop */
754 pc
= inst
->BranchTarget
- 1;
759 GLfloat a
[4], b
[4], c
[4], result
[4];
760 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
761 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
762 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
763 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
764 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
765 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
766 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
767 store_vector4(inst
, machine
, result
);
772 GLfloat a
[4], result
[4];
773 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
774 result
[0] = result
[1] = result
[2] = result
[3]
775 = (GLfloat
) _mesa_cos(a
[0]);
776 store_vector4(inst
, machine
, result
);
779 case OPCODE_DDX
: /* Partial derivative with respect to X */
782 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
784 store_vector4(inst
, machine
, result
);
787 case OPCODE_DDY
: /* Partial derivative with respect to Y */
790 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
792 store_vector4(inst
, machine
, result
);
797 GLfloat a
[4], b
[4], result
[4];
798 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
799 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
800 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
);
801 store_vector4(inst
, machine
, result
);
803 printf("DP2 %g = (%g %g) . (%g %g)\n",
804 result
[0], a
[0], a
[1], b
[0], b
[1]);
810 GLfloat a
[4], b
[4], c
, result
[4];
811 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
812 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
813 fetch_vector1(&inst
->SrcReg
[1], machine
, &c
);
814 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
) + c
;
815 store_vector4(inst
, machine
, result
);
817 printf("DP2A %g = (%g %g) . (%g %g) + %g\n",
818 result
[0], a
[0], a
[1], b
[0], b
[1], c
);
824 GLfloat a
[4], b
[4], result
[4];
825 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
826 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
827 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
828 store_vector4(inst
, machine
, result
);
830 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
831 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
837 GLfloat a
[4], b
[4], result
[4];
838 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
839 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
840 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
, b
);
841 store_vector4(inst
, machine
, result
);
843 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
844 result
[0], a
[0], a
[1], a
[2], a
[3],
845 b
[0], b
[1], b
[2], b
[3]);
851 GLfloat a
[4], b
[4], result
[4];
852 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
853 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
854 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
) + b
[3];
855 store_vector4(inst
, machine
, result
);
858 case OPCODE_DST
: /* Distance vector */
860 GLfloat a
[4], b
[4], result
[4];
861 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
862 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
864 result
[1] = a
[1] * b
[1];
867 store_vector4(inst
, machine
, result
);
872 GLfloat t
[4], q
[4], floor_t0
;
873 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
874 floor_t0
= FLOORF(t
[0]);
875 if (floor_t0
> FLT_MAX_EXP
) {
876 SET_POS_INFINITY(q
[0]);
877 SET_POS_INFINITY(q
[2]);
879 else if (floor_t0
< FLT_MIN_EXP
) {
884 q
[0] = LDEXPF(1.0, (int) floor_t0
);
885 /* Note: GL_NV_vertex_program expects
886 * result.z = result.x * APPX(result.y)
887 * We do what the ARB extension says.
889 q
[2] = (GLfloat
) _mesa_pow(2.0, t
[0]);
891 q
[1] = t
[0] - floor_t0
;
893 store_vector4( inst
, machine
, q
);
896 case OPCODE_EX2
: /* Exponential base 2 */
898 GLfloat a
[4], result
[4], val
;
899 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
900 val
= (GLfloat
) _mesa_pow(2.0, a
[0]);
902 if (IS_INF_OR_NAN(val))
905 result
[0] = result
[1] = result
[2] = result
[3] = val
;
906 store_vector4(inst
, machine
, result
);
911 GLfloat a
[4], result
[4];
912 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
913 result
[0] = FLOORF(a
[0]);
914 result
[1] = FLOORF(a
[1]);
915 result
[2] = FLOORF(a
[2]);
916 result
[3] = FLOORF(a
[3]);
917 store_vector4(inst
, machine
, result
);
922 GLfloat a
[4], result
[4];
923 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
924 result
[0] = a
[0] - FLOORF(a
[0]);
925 result
[1] = a
[1] - FLOORF(a
[1]);
926 result
[2] = a
[2] - FLOORF(a
[2]);
927 result
[3] = a
[3] - FLOORF(a
[3]);
928 store_vector4(inst
, machine
, result
);
934 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
936 program
->Instructions
[inst
->BranchTarget
].Opcode
939 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
941 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
942 cond
= (a
[0] != 0.0);
945 cond
= eval_condition(machine
, inst
);
948 printf("IF: %d\n", cond
);
952 /* do if-clause (just continue execution) */
955 /* go to the instruction after ELSE or ENDIF */
956 assert(inst
->BranchTarget
>= 0);
957 pc
= inst
->BranchTarget
;
963 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
965 assert(inst
->BranchTarget
>= 0);
966 pc
= inst
->BranchTarget
;
971 case OPCODE_KIL_NV
: /* NV_f_p only (conditional) */
972 if (eval_condition(machine
, inst
)) {
976 case OPCODE_KIL
: /* ARB_f_p only */
979 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
981 printf("KIL if (%g %g %g %g) <= 0.0\n",
982 a
[0], a
[1], a
[2], a
[3]);
985 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
990 case OPCODE_LG2
: /* log base 2 */
992 GLfloat a
[4], result
[4], val
;
993 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
994 /* The fast LOG2 macro doesn't meet the precision requirements.
1000 val
= log(a
[0]) * 1.442695F
;
1002 result
[0] = result
[1] = result
[2] = result
[3] = val
;
1003 store_vector4(inst
, machine
, result
);
1008 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
1009 GLfloat a
[4], result
[4];
1010 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1011 a
[0] = MAX2(a
[0], 0.0F
);
1012 a
[1] = MAX2(a
[1], 0.0F
);
1013 /* XXX ARB version clamps a[3], NV version doesn't */
1014 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
1017 /* XXX we could probably just use pow() here */
1019 if (a
[1] == 0.0 && a
[3] == 0.0)
1022 result
[2] = (GLfloat
) _mesa_pow(a
[1], a
[3]);
1028 store_vector4(inst
, machine
, result
);
1030 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
1031 result
[0], result
[1], result
[2], result
[3],
1032 a
[0], a
[1], a
[2], a
[3]);
1038 GLfloat t
[4], q
[4], abs_t0
;
1039 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
1040 abs_t0
= FABSF(t
[0]);
1041 if (abs_t0
!= 0.0F
) {
1042 /* Since we really can't handle infinite values on VMS
1043 * like other OSes we'll use __MAXFLOAT to represent
1044 * infinity. This may need some tweaking.
1047 if (abs_t0
== __MAXFLOAT
)
1049 if (IS_INF_OR_NAN(abs_t0
))
1052 SET_POS_INFINITY(q
[0]);
1054 SET_POS_INFINITY(q
[2]);
1058 GLfloat mantissa
= FREXPF(t
[0], &exponent
);
1059 q
[0] = (GLfloat
) (exponent
- 1);
1060 q
[1] = (GLfloat
) (2.0 * mantissa
); /* map [.5, 1) -> [1, 2) */
1062 /* The fast LOG2 macro doesn't meet the precision
1065 q
[2] = (log(t
[0]) * 1.442695F
);
1069 SET_NEG_INFINITY(q
[0]);
1071 SET_NEG_INFINITY(q
[2]);
1074 store_vector4(inst
, machine
, q
);
1079 GLfloat a
[4], b
[4], c
[4], result
[4];
1080 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1081 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1082 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1083 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
1084 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
1085 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
1086 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
1087 store_vector4(inst
, machine
, result
);
1089 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
1090 "(%g %g %g %g), (%g %g %g %g)\n",
1091 result
[0], result
[1], result
[2], result
[3],
1092 a
[0], a
[1], a
[2], a
[3],
1093 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1099 GLfloat a
[4], b
[4], c
[4], result
[4];
1100 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1101 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1102 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1103 result
[0] = a
[0] * b
[0] + c
[0];
1104 result
[1] = a
[1] * b
[1] + c
[1];
1105 result
[2] = a
[2] * b
[2] + c
[2];
1106 result
[3] = a
[3] * b
[3] + c
[3];
1107 store_vector4(inst
, machine
, result
);
1109 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
1110 "(%g %g %g %g) + (%g %g %g %g)\n",
1111 result
[0], result
[1], result
[2], result
[3],
1112 a
[0], a
[1], a
[2], a
[3],
1113 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1119 GLfloat a
[4], b
[4], result
[4];
1120 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1121 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1122 result
[0] = MAX2(a
[0], b
[0]);
1123 result
[1] = MAX2(a
[1], b
[1]);
1124 result
[2] = MAX2(a
[2], b
[2]);
1125 result
[3] = MAX2(a
[3], b
[3]);
1126 store_vector4(inst
, machine
, result
);
1128 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
1129 result
[0], result
[1], result
[2], result
[3],
1130 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[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] = MIN2(a
[0], b
[0]);
1140 result
[1] = MIN2(a
[1], b
[1]);
1141 result
[2] = MIN2(a
[2], b
[2]);
1142 result
[3] = MIN2(a
[3], b
[3]);
1143 store_vector4(inst
, machine
, result
);
1149 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
1150 store_vector4(inst
, machine
, result
);
1152 printf("MOV (%g %g %g %g)\n",
1153 result
[0], result
[1], result
[2], result
[3]);
1159 GLfloat a
[4], b
[4], result
[4];
1160 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1161 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1162 result
[0] = a
[0] * b
[0];
1163 result
[1] = a
[1] * b
[1];
1164 result
[2] = a
[2] * b
[2];
1165 result
[3] = a
[3] * b
[3];
1166 store_vector4(inst
, machine
, result
);
1168 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1169 result
[0], result
[1], result
[2], result
[3],
1170 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1176 GLfloat a
[4], result
[4];
1177 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1181 result
[3] = _mesa_noise1(a
[0]);
1182 store_vector4(inst
, machine
, result
);
1187 GLfloat a
[4], result
[4];
1188 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1191 result
[2] = result
[3] = _mesa_noise2(a
[0], a
[1]);
1192 store_vector4(inst
, machine
, result
);
1197 GLfloat a
[4], result
[4];
1198 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1202 result
[3] = _mesa_noise3(a
[0], a
[1], a
[2]);
1203 store_vector4(inst
, machine
, result
);
1208 GLfloat a
[4], result
[4];
1209 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1213 result
[3] = _mesa_noise4(a
[0], a
[1], a
[2], a
[3]);
1214 store_vector4(inst
, machine
, result
);
1219 case OPCODE_NOT
: /* bitwise NOT */
1221 GLuint a
[4], result
[4];
1222 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1227 store_vector4ui(inst
, machine
, result
);
1230 case OPCODE_NRM3
: /* 3-component normalization */
1232 GLfloat a
[4], result
[4];
1234 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1235 tmp
= a
[0] * a
[0] + a
[1] * a
[1] + a
[2] * a
[2];
1237 tmp
= INV_SQRTF(tmp
);
1238 result
[0] = tmp
* a
[0];
1239 result
[1] = tmp
* a
[1];
1240 result
[2] = tmp
* a
[2];
1241 result
[3] = 0.0; /* undefined, but prevent valgrind warnings */
1242 store_vector4(inst
, machine
, result
);
1245 case OPCODE_NRM4
: /* 4-component normalization */
1247 GLfloat a
[4], result
[4];
1249 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1250 tmp
= a
[0] * a
[0] + a
[1] * a
[1] + a
[2] * a
[2] + a
[3] * a
[3];
1252 tmp
= INV_SQRTF(tmp
);
1253 result
[0] = tmp
* a
[0];
1254 result
[1] = tmp
* a
[1];
1255 result
[2] = tmp
* a
[2];
1256 result
[3] = tmp
* a
[3];
1257 store_vector4(inst
, machine
, result
);
1260 case OPCODE_OR
: /* bitwise OR */
1262 GLuint a
[4], b
[4], result
[4];
1263 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1264 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
1265 result
[0] = a
[0] | b
[0];
1266 result
[1] = a
[1] | b
[1];
1267 result
[2] = a
[2] | b
[2];
1268 result
[3] = a
[3] | b
[3];
1269 store_vector4ui(inst
, machine
, result
);
1272 case OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
1277 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1278 hx
= _mesa_float_to_half(a
[0]);
1279 hy
= _mesa_float_to_half(a
[1]);
1283 result
[3] = hx
| (hy
<< 16);
1284 store_vector4ui(inst
, machine
, result
);
1287 case OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
1290 GLuint result
[4], usx
, usy
;
1291 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1292 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1293 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1294 usx
= IROUND(a
[0] * 65535.0F
);
1295 usy
= IROUND(a
[1] * 65535.0F
);
1299 result
[3] = usx
| (usy
<< 16);
1300 store_vector4ui(inst
, machine
, result
);
1303 case OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
1306 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1307 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1308 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
1309 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
1310 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
1311 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
1312 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
1313 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
1314 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
1315 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
1319 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1320 store_vector4ui(inst
, machine
, result
);
1323 case OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
1326 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1327 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1328 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1329 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1330 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
1331 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
1332 ubx
= IROUND(255.0F
* a
[0]);
1333 uby
= IROUND(255.0F
* a
[1]);
1334 ubz
= IROUND(255.0F
* a
[2]);
1335 ubw
= IROUND(255.0F
* a
[3]);
1339 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1340 store_vector4ui(inst
, machine
, result
);
1345 GLfloat a
[4], b
[4], result
[4];
1346 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1347 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
1348 result
[0] = result
[1] = result
[2] = result
[3]
1349 = (GLfloat
) _mesa_pow(a
[0], b
[0]);
1350 store_vector4(inst
, machine
, result
);
1355 GLfloat a
[4], result
[4];
1356 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1360 else if (IS_INF_OR_NAN(a
[0]))
1361 printf("RCP(inf)\n");
1363 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
1364 store_vector4(inst
, machine
, result
);
1367 case OPCODE_RET
: /* return from subroutine (conditional) */
1368 if (eval_condition(machine
, inst
)) {
1369 if (machine
->StackDepth
== 0) {
1370 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
1372 /* subtract one because of pc++ in the for loop */
1373 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
1376 case OPCODE_RFL
: /* reflection vector */
1378 GLfloat axis
[4], dir
[4], result
[4], tmpX
, tmpW
;
1379 fetch_vector4(&inst
->SrcReg
[0], machine
, axis
);
1380 fetch_vector4(&inst
->SrcReg
[1], machine
, dir
);
1381 tmpW
= DOT3(axis
, axis
);
1382 tmpX
= (2.0F
* DOT3(axis
, dir
)) / tmpW
;
1383 result
[0] = tmpX
* axis
[0] - dir
[0];
1384 result
[1] = tmpX
* axis
[1] - dir
[1];
1385 result
[2] = tmpX
* axis
[2] - dir
[2];
1386 /* result[3] is never written! XXX enforce in parser! */
1387 store_vector4(inst
, machine
, result
);
1390 case OPCODE_RSQ
: /* 1 / sqrt() */
1392 GLfloat a
[4], result
[4];
1393 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1395 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1396 store_vector4(inst
, machine
, result
);
1398 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
1402 case OPCODE_SCS
: /* sine and cos */
1404 GLfloat a
[4], result
[4];
1405 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1406 result
[0] = (GLfloat
) _mesa_cos(a
[0]);
1407 result
[1] = (GLfloat
) _mesa_sin(a
[0]);
1408 result
[2] = 0.0; /* undefined! */
1409 result
[3] = 0.0; /* undefined! */
1410 store_vector4(inst
, machine
, result
);
1413 case OPCODE_SEQ
: /* set on equal */
1415 GLfloat a
[4], b
[4], result
[4];
1416 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1417 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1418 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1419 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1420 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1421 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1422 store_vector4(inst
, machine
, result
);
1424 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1425 result
[0], result
[1], result
[2], result
[3],
1426 a
[0], a
[1], a
[2], a
[3],
1427 b
[0], b
[1], b
[2], b
[3]);
1431 case OPCODE_SFL
: /* set false, operands ignored */
1433 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1434 store_vector4(inst
, machine
, result
);
1437 case OPCODE_SGE
: /* set on greater or equal */
1439 GLfloat a
[4], b
[4], result
[4];
1440 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1441 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1442 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1443 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1444 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1445 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1446 store_vector4(inst
, machine
, result
);
1448 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1449 result
[0], result
[1], result
[2], result
[3],
1450 a
[0], a
[1], a
[2], a
[3],
1451 b
[0], b
[1], b
[2], b
[3]);
1455 case OPCODE_SGT
: /* set on greater */
1457 GLfloat a
[4], b
[4], result
[4];
1458 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1459 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1460 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1461 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1462 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1463 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1464 store_vector4(inst
, machine
, result
);
1466 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1467 result
[0], result
[1], result
[2], result
[3],
1468 a
[0], a
[1], a
[2], a
[3],
1469 b
[0], b
[1], b
[2], b
[3]);
1475 GLfloat a
[4], result
[4];
1476 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1477 result
[0] = result
[1] = result
[2] = result
[3]
1478 = (GLfloat
) _mesa_sin(a
[0]);
1479 store_vector4(inst
, machine
, result
);
1482 case OPCODE_SLE
: /* set on less or equal */
1484 GLfloat a
[4], b
[4], result
[4];
1485 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1486 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1487 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1488 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1489 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1490 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1491 store_vector4(inst
, machine
, result
);
1493 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1494 result
[0], result
[1], result
[2], result
[3],
1495 a
[0], a
[1], a
[2], a
[3],
1496 b
[0], b
[1], b
[2], b
[3]);
1500 case OPCODE_SLT
: /* set on less */
1502 GLfloat a
[4], b
[4], result
[4];
1503 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1504 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1505 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1506 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1507 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1508 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1509 store_vector4(inst
, machine
, result
);
1511 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1512 result
[0], result
[1], result
[2], result
[3],
1513 a
[0], a
[1], a
[2], a
[3],
1514 b
[0], b
[1], b
[2], b
[3]);
1518 case OPCODE_SNE
: /* set on not equal */
1520 GLfloat a
[4], b
[4], result
[4];
1521 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1522 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1523 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1524 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1525 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1526 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1527 store_vector4(inst
, machine
, result
);
1529 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1530 result
[0], result
[1], result
[2], result
[3],
1531 a
[0], a
[1], a
[2], a
[3],
1532 b
[0], b
[1], b
[2], b
[3]);
1536 case OPCODE_SSG
: /* set sign (-1, 0 or +1) */
1538 GLfloat a
[4], result
[4];
1539 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1540 result
[0] = (GLfloat
) ((a
[0] > 0.0F
) - (a
[0] < 0.0F
));
1541 result
[1] = (GLfloat
) ((a
[1] > 0.0F
) - (a
[1] < 0.0F
));
1542 result
[2] = (GLfloat
) ((a
[2] > 0.0F
) - (a
[2] < 0.0F
));
1543 result
[3] = (GLfloat
) ((a
[3] > 0.0F
) - (a
[3] < 0.0F
));
1544 store_vector4(inst
, machine
, result
);
1547 case OPCODE_STR
: /* set true, operands ignored */
1549 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1550 store_vector4(inst
, machine
, result
);
1555 GLfloat a
[4], b
[4], result
[4];
1556 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1557 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1558 result
[0] = a
[0] - b
[0];
1559 result
[1] = a
[1] - b
[1];
1560 result
[2] = a
[2] - b
[2];
1561 result
[3] = a
[3] - b
[3];
1562 store_vector4(inst
, machine
, result
);
1564 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1565 result
[0], result
[1], result
[2], result
[3],
1566 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1570 case OPCODE_SWZ
: /* extended swizzle */
1572 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1573 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
1576 for (i
= 0; i
< 4; i
++) {
1577 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1578 if (swz
== SWIZZLE_ZERO
)
1580 else if (swz
== SWIZZLE_ONE
)
1585 result
[i
] = src
[swz
];
1587 if (source
->Negate
& (1 << i
))
1588 result
[i
] = -result
[i
];
1590 store_vector4(inst
, machine
, result
);
1593 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1594 /* Simple texel lookup */
1596 GLfloat texcoord
[4], color
[4];
1597 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1599 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1602 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1603 color
[0], color
[1], color
[2], color
[3],
1605 texcoord
[0], texcoord
[1], texcoord
[2], texcoord
[3]);
1607 store_vector4(inst
, machine
, color
);
1610 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1611 /* Texel lookup with LOD bias */
1613 GLfloat texcoord
[4], color
[4], lodBias
;
1615 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1617 /* texcoord[3] is the bias to add to lambda */
1618 lodBias
= texcoord
[3];
1620 fetch_texel(ctx
, machine
, inst
, texcoord
, lodBias
, color
);
1622 store_vector4(inst
, machine
, color
);
1625 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1626 /* Texture lookup w/ partial derivatives for LOD */
1628 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1629 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1630 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1631 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1632 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1634 inst
->TexSrcUnit
, color
);
1635 store_vector4(inst
, machine
, color
);
1638 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1639 /* Texture lookup w/ projective divide */
1641 GLfloat texcoord
[4], color
[4];
1643 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1644 /* Not so sure about this test - if texcoord[3] is
1645 * zero, we'd probably be fine except for an ASSERT in
1646 * IROUND_POS() which gets triggered by the inf values created.
1648 if (texcoord
[3] != 0.0) {
1649 texcoord
[0] /= texcoord
[3];
1650 texcoord
[1] /= texcoord
[3];
1651 texcoord
[2] /= texcoord
[3];
1654 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1656 store_vector4(inst
, machine
, color
);
1659 case OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1660 /* Texture lookup w/ projective divide, as above, but do not
1661 * do the divide by w if sampling from a cube map.
1664 GLfloat texcoord
[4], color
[4];
1666 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1667 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1668 texcoord
[3] != 0.0) {
1669 texcoord
[0] /= texcoord
[3];
1670 texcoord
[1] /= texcoord
[3];
1671 texcoord
[2] /= texcoord
[3];
1674 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1676 store_vector4(inst
, machine
, color
);
1679 case OPCODE_TRUNC
: /* truncate toward zero */
1681 GLfloat a
[4], result
[4];
1682 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1683 result
[0] = (GLfloat
) (GLint
) a
[0];
1684 result
[1] = (GLfloat
) (GLint
) a
[1];
1685 result
[2] = (GLfloat
) (GLint
) a
[2];
1686 result
[3] = (GLfloat
) (GLint
) a
[3];
1687 store_vector4(inst
, machine
, result
);
1690 case OPCODE_UP2H
: /* unpack two 16-bit floats */
1692 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1697 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1698 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1699 store_vector4(inst
, machine
, result
);
1702 case OPCODE_UP2US
: /* unpack two GLushorts */
1704 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1709 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1710 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1711 store_vector4(inst
, machine
, result
);
1714 case OPCODE_UP4B
: /* unpack four GLbytes */
1716 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1718 result
[0] = (((raw
>> 0) & 0xff) - 128) / 127.0F
;
1719 result
[1] = (((raw
>> 8) & 0xff) - 128) / 127.0F
;
1720 result
[2] = (((raw
>> 16) & 0xff) - 128) / 127.0F
;
1721 result
[3] = (((raw
>> 24) & 0xff) - 128) / 127.0F
;
1722 store_vector4(inst
, machine
, result
);
1725 case OPCODE_UP4UB
: /* unpack four GLubytes */
1727 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1729 result
[0] = ((raw
>> 0) & 0xff) / 255.0F
;
1730 result
[1] = ((raw
>> 8) & 0xff) / 255.0F
;
1731 result
[2] = ((raw
>> 16) & 0xff) / 255.0F
;
1732 result
[3] = ((raw
>> 24) & 0xff) / 255.0F
;
1733 store_vector4(inst
, machine
, result
);
1736 case OPCODE_XOR
: /* bitwise XOR */
1738 GLuint a
[4], b
[4], result
[4];
1739 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1740 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
1741 result
[0] = a
[0] ^ b
[0];
1742 result
[1] = a
[1] ^ b
[1];
1743 result
[2] = a
[2] ^ b
[2];
1744 result
[3] = a
[3] ^ b
[3];
1745 store_vector4ui(inst
, machine
, result
);
1748 case OPCODE_XPD
: /* cross product */
1750 GLfloat a
[4], b
[4], result
[4];
1751 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1752 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1753 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1754 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1755 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1757 store_vector4(inst
, machine
, result
);
1759 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1760 result
[0], result
[1], result
[2], result
[3],
1761 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1765 case OPCODE_X2D
: /* 2-D matrix transform */
1767 GLfloat a
[4], b
[4], c
[4], result
[4];
1768 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1769 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1770 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1771 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1772 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1773 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1774 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1775 store_vector4(inst
, machine
, result
);
1780 if (inst
->SrcReg
[0].File
!= -1) {
1782 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1783 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst
->Data
,
1784 a
[0], a
[1], a
[2], a
[3]);
1787 _mesa_printf("%s\n", (const char *) inst
->Data
);
1794 _mesa_problem(ctx
, "Bad opcode %d in _mesa_execute_program",
1796 return GL_TRUE
; /* return value doesn't matter */
1800 if (numExec
> maxExec
) {
1801 _mesa_problem(ctx
, "Infinite loop detected in fragment program");