2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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_STATE_VAR
:
123 case PROGRAM_CONSTANT
:
125 case PROGRAM_UNIFORM
:
126 if (reg
>= (GLint
) prog
->Parameters
->NumParameters
)
128 return (GLfloat
*) prog
->Parameters
->ParameterValues
[reg
];
130 case PROGRAM_SYSTEM_VALUE
:
131 assert(reg
< Elements(machine
->SystemValues
));
132 return machine
->SystemValues
[reg
];
136 "Invalid src register file %d in get_src_register_pointer()",
144 * Return a pointer to the 4-element float vector specified by the given
145 * destination register.
147 static inline GLfloat
*
148 get_dst_register_pointer(const struct prog_dst_register
*dest
,
149 struct gl_program_machine
*machine
)
151 static GLfloat dummyReg
[4];
152 GLint reg
= dest
->Index
;
155 /* add address register value to src index/offset */
156 reg
+= machine
->AddressReg
[0][0];
162 switch (dest
->File
) {
163 case PROGRAM_TEMPORARY
:
164 if (reg
>= MAX_PROGRAM_TEMPS
)
166 return machine
->Temporaries
[reg
];
169 if (reg
>= MAX_PROGRAM_OUTPUTS
)
171 return machine
->Outputs
[reg
];
175 "Invalid dest register file %d in get_dst_register_pointer()",
184 * Fetch a 4-element float vector from the given source register.
185 * Apply swizzling and negating as needed.
188 fetch_vector4(const struct prog_src_register
*source
,
189 const struct gl_program_machine
*machine
, GLfloat result
[4])
191 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
193 if (source
->Swizzle
== SWIZZLE_NOOP
) {
195 COPY_4V(result
, src
);
198 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
199 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
200 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
201 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
202 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
203 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
204 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
205 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
209 result
[0] = FABSF(result
[0]);
210 result
[1] = FABSF(result
[1]);
211 result
[2] = FABSF(result
[2]);
212 result
[3] = FABSF(result
[3]);
214 if (source
->Negate
) {
215 ASSERT(source
->Negate
== NEGATE_XYZW
);
216 result
[0] = -result
[0];
217 result
[1] = -result
[1];
218 result
[2] = -result
[2];
219 result
[3] = -result
[3];
223 assert(!IS_INF_OR_NAN(result
[0]));
224 assert(!IS_INF_OR_NAN(result
[0]));
225 assert(!IS_INF_OR_NAN(result
[0]));
226 assert(!IS_INF_OR_NAN(result
[0]));
232 * Fetch the derivative with respect to X or Y for the given register.
233 * XXX this currently only works for fragment program input attribs.
236 fetch_vector4_deriv(struct gl_context
* ctx
,
237 const struct prog_src_register
*source
,
238 const struct gl_program_machine
*machine
,
239 char xOrY
, GLfloat result
[4])
241 if (source
->File
== PROGRAM_INPUT
&&
242 source
->Index
< (GLint
) machine
->NumDeriv
) {
243 const GLint col
= machine
->CurElement
;
244 const GLfloat w
= machine
->Attribs
[VARYING_SLOT_POS
][col
][3];
245 const GLfloat invQ
= 1.0f
/ w
;
249 deriv
[0] = machine
->DerivX
[source
->Index
][0] * invQ
;
250 deriv
[1] = machine
->DerivX
[source
->Index
][1] * invQ
;
251 deriv
[2] = machine
->DerivX
[source
->Index
][2] * invQ
;
252 deriv
[3] = machine
->DerivX
[source
->Index
][3] * invQ
;
255 deriv
[0] = machine
->DerivY
[source
->Index
][0] * invQ
;
256 deriv
[1] = machine
->DerivY
[source
->Index
][1] * invQ
;
257 deriv
[2] = machine
->DerivY
[source
->Index
][2] * invQ
;
258 deriv
[3] = machine
->DerivY
[source
->Index
][3] * invQ
;
261 result
[0] = deriv
[GET_SWZ(source
->Swizzle
, 0)];
262 result
[1] = deriv
[GET_SWZ(source
->Swizzle
, 1)];
263 result
[2] = deriv
[GET_SWZ(source
->Swizzle
, 2)];
264 result
[3] = deriv
[GET_SWZ(source
->Swizzle
, 3)];
267 result
[0] = FABSF(result
[0]);
268 result
[1] = FABSF(result
[1]);
269 result
[2] = FABSF(result
[2]);
270 result
[3] = FABSF(result
[3]);
272 if (source
->Negate
) {
273 ASSERT(source
->Negate
== NEGATE_XYZW
);
274 result
[0] = -result
[0];
275 result
[1] = -result
[1];
276 result
[2] = -result
[2];
277 result
[3] = -result
[3];
281 ASSIGN_4V(result
, 0.0, 0.0, 0.0, 0.0);
287 * As above, but only return result[0] element.
290 fetch_vector1(const struct prog_src_register
*source
,
291 const struct gl_program_machine
*machine
, GLfloat result
[4])
293 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
295 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
298 result
[0] = FABSF(result
[0]);
300 if (source
->Negate
) {
301 result
[0] = -result
[0];
307 fetch_vector1ui(const struct prog_src_register
*source
,
308 const struct gl_program_machine
*machine
)
310 const GLuint
*src
= (GLuint
*) get_src_register_pointer(source
, machine
);
311 return src
[GET_SWZ(source
->Swizzle
, 0)];
316 * Fetch texel from texture. Use partial derivatives when possible.
319 fetch_texel(struct gl_context
*ctx
,
320 const struct gl_program_machine
*machine
,
321 const struct prog_instruction
*inst
,
322 const GLfloat texcoord
[4], GLfloat lodBias
,
325 const GLuint unit
= machine
->Samplers
[inst
->TexSrcUnit
];
327 /* Note: we only have the right derivatives for fragment input attribs.
329 if (machine
->NumDeriv
> 0 &&
330 inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
331 inst
->SrcReg
[0].Index
== VARYING_SLOT_TEX0
+ inst
->TexSrcUnit
) {
332 /* simple texture fetch for which we should have derivatives */
333 GLuint attr
= inst
->SrcReg
[0].Index
;
334 machine
->FetchTexelDeriv(ctx
, texcoord
,
335 machine
->DerivX
[attr
],
336 machine
->DerivY
[attr
],
337 lodBias
, unit
, color
);
340 machine
->FetchTexelLod(ctx
, texcoord
, lodBias
, unit
, color
);
346 * Test value against zero and return GT, LT, EQ or UN if NaN.
349 generate_cc(float value
)
352 return COND_UN
; /* NaN */
362 * Test if the ccMaskRule is satisfied by the given condition code.
363 * Used to mask destination writes according to the current condition code.
365 static inline GLboolean
366 test_cc(GLuint condCode
, GLuint ccMaskRule
)
368 switch (ccMaskRule
) {
369 case COND_EQ
: return (condCode
== COND_EQ
);
370 case COND_NE
: return (condCode
!= COND_EQ
);
371 case COND_LT
: return (condCode
== COND_LT
);
372 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
373 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
374 case COND_GT
: return (condCode
== COND_GT
);
375 case COND_TR
: return GL_TRUE
;
376 case COND_FL
: return GL_FALSE
;
377 default: return GL_TRUE
;
383 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
384 * or GL_FALSE to indicate result.
386 static inline GLboolean
387 eval_condition(const struct gl_program_machine
*machine
,
388 const struct prog_instruction
*inst
)
390 const GLuint swizzle
= inst
->DstReg
.CondSwizzle
;
391 const GLuint condMask
= inst
->DstReg
.CondMask
;
392 if (test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 0)], condMask
) ||
393 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 1)], condMask
) ||
394 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 2)], condMask
) ||
395 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 3)], condMask
)) {
406 * Store 4 floats into a register. Observe the instructions saturate and
407 * set-condition-code flags.
410 store_vector4(const struct prog_instruction
*inst
,
411 struct gl_program_machine
*machine
, const GLfloat value
[4])
413 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
414 const GLboolean clamp
= inst
->SaturateMode
== SATURATE_ZERO_ONE
;
415 GLuint writeMask
= dstReg
->WriteMask
;
416 GLfloat clampedValue
[4];
417 GLfloat
*dst
= get_dst_register_pointer(dstReg
, machine
);
420 if (value
[0] > 1.0e10
||
421 IS_INF_OR_NAN(value
[0]) ||
422 IS_INF_OR_NAN(value
[1]) ||
423 IS_INF_OR_NAN(value
[2]) || IS_INF_OR_NAN(value
[3]))
424 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
428 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
429 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
430 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
431 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
432 value
= clampedValue
;
435 if (dstReg
->CondMask
!= COND_TR
) {
436 /* condition codes may turn off some writes */
437 if (writeMask
& WRITEMASK_X
) {
438 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
440 writeMask
&= ~WRITEMASK_X
;
442 if (writeMask
& WRITEMASK_Y
) {
443 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
445 writeMask
&= ~WRITEMASK_Y
;
447 if (writeMask
& WRITEMASK_Z
) {
448 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
450 writeMask
&= ~WRITEMASK_Z
;
452 if (writeMask
& WRITEMASK_W
) {
453 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
455 writeMask
&= ~WRITEMASK_W
;
460 assert(!IS_INF_OR_NAN(value
[0]));
461 assert(!IS_INF_OR_NAN(value
[0]));
462 assert(!IS_INF_OR_NAN(value
[0]));
463 assert(!IS_INF_OR_NAN(value
[0]));
466 if (writeMask
& WRITEMASK_X
)
468 if (writeMask
& WRITEMASK_Y
)
470 if (writeMask
& WRITEMASK_Z
)
472 if (writeMask
& WRITEMASK_W
)
475 if (inst
->CondUpdate
) {
476 if (writeMask
& WRITEMASK_X
)
477 machine
->CondCodes
[0] = generate_cc(value
[0]);
478 if (writeMask
& WRITEMASK_Y
)
479 machine
->CondCodes
[1] = generate_cc(value
[1]);
480 if (writeMask
& WRITEMASK_Z
)
481 machine
->CondCodes
[2] = generate_cc(value
[2]);
482 if (writeMask
& WRITEMASK_W
)
483 machine
->CondCodes
[3] = generate_cc(value
[3]);
485 printf("CondCodes=(%s,%s,%s,%s) for:\n",
486 _mesa_condcode_string(machine
->CondCodes
[0]),
487 _mesa_condcode_string(machine
->CondCodes
[1]),
488 _mesa_condcode_string(machine
->CondCodes
[2]),
489 _mesa_condcode_string(machine
->CondCodes
[3]));
496 * Store 4 uints into a register. Observe the set-condition-code flags.
499 store_vector4ui(const struct prog_instruction
*inst
,
500 struct gl_program_machine
*machine
, const GLuint value
[4])
502 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
503 GLuint writeMask
= dstReg
->WriteMask
;
504 GLuint
*dst
= (GLuint
*) get_dst_register_pointer(dstReg
, machine
);
506 if (dstReg
->CondMask
!= COND_TR
) {
507 /* condition codes may turn off some writes */
508 if (writeMask
& WRITEMASK_X
) {
509 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
511 writeMask
&= ~WRITEMASK_X
;
513 if (writeMask
& WRITEMASK_Y
) {
514 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
516 writeMask
&= ~WRITEMASK_Y
;
518 if (writeMask
& WRITEMASK_Z
) {
519 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
521 writeMask
&= ~WRITEMASK_Z
;
523 if (writeMask
& WRITEMASK_W
) {
524 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
526 writeMask
&= ~WRITEMASK_W
;
530 if (writeMask
& WRITEMASK_X
)
532 if (writeMask
& WRITEMASK_Y
)
534 if (writeMask
& WRITEMASK_Z
)
536 if (writeMask
& WRITEMASK_W
)
539 if (inst
->CondUpdate
) {
540 if (writeMask
& WRITEMASK_X
)
541 machine
->CondCodes
[0] = generate_cc((float)value
[0]);
542 if (writeMask
& WRITEMASK_Y
)
543 machine
->CondCodes
[1] = generate_cc((float)value
[1]);
544 if (writeMask
& WRITEMASK_Z
)
545 machine
->CondCodes
[2] = generate_cc((float)value
[2]);
546 if (writeMask
& WRITEMASK_W
)
547 machine
->CondCodes
[3] = generate_cc((float)value
[3]);
549 printf("CondCodes=(%s,%s,%s,%s) for:\n",
550 _mesa_condcode_string(machine
->CondCodes
[0]),
551 _mesa_condcode_string(machine
->CondCodes
[1]),
552 _mesa_condcode_string(machine
->CondCodes
[2]),
553 _mesa_condcode_string(machine
->CondCodes
[3]));
561 * Execute the given vertex/fragment program.
563 * \param ctx rendering context
564 * \param program the program to execute
565 * \param machine machine state (must be initialized)
566 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
569 _mesa_execute_program(struct gl_context
* ctx
,
570 const struct gl_program
*program
,
571 struct gl_program_machine
*machine
)
573 const GLuint numInst
= program
->NumInstructions
;
574 const GLuint maxExec
= 65536;
575 GLuint pc
, numExec
= 0;
577 machine
->CurProgram
= program
;
580 printf("execute program %u --------------------\n", program
->Id
);
583 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
584 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
587 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
590 for (pc
= 0; pc
< numInst
; pc
++) {
591 const struct prog_instruction
*inst
= program
->Instructions
+ pc
;
594 _mesa_print_instruction(inst
);
597 switch (inst
->Opcode
) {
600 GLfloat a
[4], result
[4];
601 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
602 result
[0] = FABSF(a
[0]);
603 result
[1] = FABSF(a
[1]);
604 result
[2] = FABSF(a
[2]);
605 result
[3] = FABSF(a
[3]);
606 store_vector4(inst
, machine
, result
);
611 GLfloat a
[4], b
[4], result
[4];
612 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
613 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
614 result
[0] = a
[0] + b
[0];
615 result
[1] = a
[1] + b
[1];
616 result
[2] = a
[2] + b
[2];
617 result
[3] = a
[3] + b
[3];
618 store_vector4(inst
, machine
, result
);
620 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
621 result
[0], result
[1], result
[2], result
[3],
622 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
629 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
630 machine
->AddressReg
[0][0] = IFLOOR(t
[0]);
632 printf("ARL %d\n", machine
->AddressReg
[0][0]);
638 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
642 /* subtract 1 here since pc is incremented by for(pc) loop */
643 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
645 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
647 case OPCODE_BGNSUB
: /* begin subroutine */
649 case OPCODE_ENDSUB
: /* end subroutine */
651 case OPCODE_BRK
: /* break out of loop (conditional) */
652 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
654 if (eval_condition(machine
, inst
)) {
655 /* break out of loop */
656 /* pc++ at end of for-loop will put us after the ENDLOOP inst */
657 pc
= inst
->BranchTarget
;
660 case OPCODE_CONT
: /* continue loop (conditional) */
661 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
663 if (eval_condition(machine
, inst
)) {
664 /* continue at ENDLOOP */
665 /* Subtract 1 here since we'll do pc++ at end of for-loop */
666 pc
= inst
->BranchTarget
- 1;
669 case OPCODE_CAL
: /* Call subroutine (conditional) */
670 if (eval_condition(machine
, inst
)) {
671 /* call the subroutine */
672 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
673 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
675 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1; /* next inst */
676 /* Subtract 1 here since we'll do pc++ at end of for-loop */
677 pc
= inst
->BranchTarget
- 1;
682 GLfloat a
[4], b
[4], c
[4], result
[4];
683 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
684 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
685 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
686 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
687 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
688 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
689 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
690 store_vector4(inst
, machine
, result
);
692 printf("CMP (%g %g %g %g) = (%g %g %g %g) < 0 ? (%g %g %g %g) : (%g %g %g %g)\n",
693 result
[0], result
[1], result
[2], result
[3],
694 a
[0], a
[1], a
[2], a
[3],
695 b
[0], b
[1], b
[2], b
[3],
696 c
[0], c
[1], c
[2], c
[3]);
702 GLfloat a
[4], result
[4];
703 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
704 result
[0] = result
[1] = result
[2] = result
[3]
705 = (GLfloat
) cos(a
[0]);
706 store_vector4(inst
, machine
, result
);
709 case OPCODE_DDX
: /* Partial derivative with respect to X */
712 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
714 store_vector4(inst
, machine
, result
);
717 case OPCODE_DDY
: /* Partial derivative with respect to Y */
720 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
722 store_vector4(inst
, machine
, result
);
727 GLfloat a
[4], b
[4], result
[4];
728 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
729 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
730 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
);
731 store_vector4(inst
, machine
, result
);
733 printf("DP2 %g = (%g %g) . (%g %g)\n",
734 result
[0], a
[0], a
[1], b
[0], b
[1]);
740 GLfloat a
[4], b
[4], result
[4];
741 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
742 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
743 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
744 store_vector4(inst
, machine
, result
);
746 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
747 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
753 GLfloat a
[4], b
[4], result
[4];
754 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
755 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
756 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
, b
);
757 store_vector4(inst
, machine
, result
);
759 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
760 result
[0], a
[0], a
[1], a
[2], a
[3],
761 b
[0], b
[1], b
[2], b
[3]);
767 GLfloat a
[4], b
[4], result
[4];
768 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
769 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
770 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
) + b
[3];
771 store_vector4(inst
, machine
, result
);
774 case OPCODE_DST
: /* Distance vector */
776 GLfloat a
[4], b
[4], result
[4];
777 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
778 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
780 result
[1] = a
[1] * b
[1];
783 store_vector4(inst
, machine
, result
);
788 GLfloat t
[4], q
[4], floor_t0
;
789 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
790 floor_t0
= FLOORF(t
[0]);
791 if (floor_t0
> FLT_MAX_EXP
) {
792 SET_POS_INFINITY(q
[0]);
793 SET_POS_INFINITY(q
[2]);
795 else if (floor_t0
< FLT_MIN_EXP
) {
800 q
[0] = LDEXPF(1.0, (int) floor_t0
);
801 /* Note: GL_NV_vertex_program expects
802 * result.z = result.x * APPX(result.y)
803 * We do what the ARB extension says.
805 q
[2] = (GLfloat
) pow(2.0, t
[0]);
807 q
[1] = t
[0] - floor_t0
;
809 store_vector4( inst
, machine
, q
);
812 case OPCODE_EX2
: /* Exponential base 2 */
814 GLfloat a
[4], result
[4], val
;
815 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
816 val
= (GLfloat
) pow(2.0, a
[0]);
818 if (IS_INF_OR_NAN(val))
821 result
[0] = result
[1] = result
[2] = result
[3] = val
;
822 store_vector4(inst
, machine
, result
);
827 GLfloat a
[4], result
[4];
828 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
829 result
[0] = FLOORF(a
[0]);
830 result
[1] = FLOORF(a
[1]);
831 result
[2] = FLOORF(a
[2]);
832 result
[3] = FLOORF(a
[3]);
833 store_vector4(inst
, machine
, result
);
838 GLfloat a
[4], result
[4];
839 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
840 result
[0] = a
[0] - FLOORF(a
[0]);
841 result
[1] = a
[1] - FLOORF(a
[1]);
842 result
[2] = a
[2] - FLOORF(a
[2]);
843 result
[3] = a
[3] - FLOORF(a
[3]);
844 store_vector4(inst
, machine
, result
);
850 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
852 program
->Instructions
[inst
->BranchTarget
].Opcode
855 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
857 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
858 cond
= (a
[0] != 0.0);
861 cond
= eval_condition(machine
, inst
);
864 printf("IF: %d\n", cond
);
868 /* do if-clause (just continue execution) */
871 /* go to the instruction after ELSE or ENDIF */
872 assert(inst
->BranchTarget
>= 0);
873 pc
= inst
->BranchTarget
;
879 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
881 assert(inst
->BranchTarget
>= 0);
882 pc
= inst
->BranchTarget
;
887 case OPCODE_KIL_NV
: /* NV_f_p only (conditional) */
888 if (eval_condition(machine
, inst
)) {
892 case OPCODE_KIL
: /* ARB_f_p only */
895 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
897 printf("KIL if (%g %g %g %g) <= 0.0\n",
898 a
[0], a
[1], a
[2], a
[3]);
901 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
906 case OPCODE_LG2
: /* log base 2 */
908 GLfloat a
[4], result
[4], val
;
909 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
910 /* The fast LOG2 macro doesn't meet the precision requirements.
916 val
= (float)(log(a
[0]) * 1.442695F
);
918 result
[0] = result
[1] = result
[2] = result
[3] = val
;
919 store_vector4(inst
, machine
, result
);
924 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
925 GLfloat a
[4], result
[4];
926 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
927 a
[0] = MAX2(a
[0], 0.0F
);
928 a
[1] = MAX2(a
[1], 0.0F
);
929 /* XXX ARB version clamps a[3], NV version doesn't */
930 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
933 /* XXX we could probably just use pow() here */
935 if (a
[1] == 0.0 && a
[3] == 0.0)
938 result
[2] = (GLfloat
) pow(a
[1], a
[3]);
944 store_vector4(inst
, machine
, result
);
946 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
947 result
[0], result
[1], result
[2], result
[3],
948 a
[0], a
[1], a
[2], a
[3]);
954 GLfloat t
[4], q
[4], abs_t0
;
955 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
956 abs_t0
= FABSF(t
[0]);
957 if (abs_t0
!= 0.0F
) {
958 if (IS_INF_OR_NAN(abs_t0
))
960 SET_POS_INFINITY(q
[0]);
962 SET_POS_INFINITY(q
[2]);
966 GLfloat mantissa
= FREXPF(t
[0], &exponent
);
967 q
[0] = (GLfloat
) (exponent
- 1);
968 q
[1] = (GLfloat
) (2.0 * mantissa
); /* map [.5, 1) -> [1, 2) */
970 /* The fast LOG2 macro doesn't meet the precision
973 q
[2] = (float)(log(t
[0]) * 1.442695F
);
977 SET_NEG_INFINITY(q
[0]);
979 SET_NEG_INFINITY(q
[2]);
982 store_vector4(inst
, machine
, q
);
987 GLfloat a
[4], b
[4], c
[4], result
[4];
988 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
989 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
990 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
991 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
992 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
993 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
994 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
995 store_vector4(inst
, machine
, result
);
997 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
998 "(%g %g %g %g), (%g %g %g %g)\n",
999 result
[0], result
[1], result
[2], result
[3],
1000 a
[0], a
[1], a
[2], a
[3],
1001 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1007 GLfloat a
[4], b
[4], c
[4], result
[4];
1008 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1009 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1010 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1011 result
[0] = a
[0] * b
[0] + c
[0];
1012 result
[1] = a
[1] * b
[1] + c
[1];
1013 result
[2] = a
[2] * b
[2] + c
[2];
1014 result
[3] = a
[3] * b
[3] + c
[3];
1015 store_vector4(inst
, machine
, result
);
1017 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
1018 "(%g %g %g %g) + (%g %g %g %g)\n",
1019 result
[0], result
[1], result
[2], result
[3],
1020 a
[0], a
[1], a
[2], a
[3],
1021 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1027 GLfloat a
[4], b
[4], result
[4];
1028 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1029 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1030 result
[0] = MAX2(a
[0], b
[0]);
1031 result
[1] = MAX2(a
[1], b
[1]);
1032 result
[2] = MAX2(a
[2], b
[2]);
1033 result
[3] = MAX2(a
[3], b
[3]);
1034 store_vector4(inst
, machine
, result
);
1036 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
1037 result
[0], result
[1], result
[2], result
[3],
1038 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1044 GLfloat a
[4], b
[4], result
[4];
1045 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1046 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1047 result
[0] = MIN2(a
[0], b
[0]);
1048 result
[1] = MIN2(a
[1], b
[1]);
1049 result
[2] = MIN2(a
[2], b
[2]);
1050 result
[3] = MIN2(a
[3], b
[3]);
1051 store_vector4(inst
, machine
, result
);
1057 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
1058 store_vector4(inst
, machine
, result
);
1060 printf("MOV (%g %g %g %g)\n",
1061 result
[0], result
[1], result
[2], result
[3]);
1067 GLfloat a
[4], b
[4], result
[4];
1068 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1069 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1070 result
[0] = a
[0] * b
[0];
1071 result
[1] = a
[1] * b
[1];
1072 result
[2] = a
[2] * b
[2];
1073 result
[3] = a
[3] * b
[3];
1074 store_vector4(inst
, machine
, result
);
1076 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1077 result
[0], result
[1], result
[2], result
[3],
1078 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1084 GLfloat a
[4], result
[4];
1085 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1089 result
[3] = _mesa_noise1(a
[0]);
1090 store_vector4(inst
, machine
, result
);
1095 GLfloat a
[4], result
[4];
1096 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1099 result
[2] = result
[3] = _mesa_noise2(a
[0], a
[1]);
1100 store_vector4(inst
, machine
, result
);
1105 GLfloat a
[4], result
[4];
1106 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1110 result
[3] = _mesa_noise3(a
[0], a
[1], a
[2]);
1111 store_vector4(inst
, machine
, result
);
1116 GLfloat a
[4], result
[4];
1117 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1121 result
[3] = _mesa_noise4(a
[0], a
[1], a
[2], a
[3]);
1122 store_vector4(inst
, machine
, result
);
1127 case OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
1132 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1133 hx
= _mesa_float_to_half(a
[0]);
1134 hy
= _mesa_float_to_half(a
[1]);
1138 result
[3] = hx
| (hy
<< 16);
1139 store_vector4ui(inst
, machine
, result
);
1142 case OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
1145 GLuint result
[4], usx
, usy
;
1146 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1147 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1148 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1149 usx
= F_TO_I(a
[0] * 65535.0F
);
1150 usy
= F_TO_I(a
[1] * 65535.0F
);
1154 result
[3] = usx
| (usy
<< 16);
1155 store_vector4ui(inst
, machine
, result
);
1158 case OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
1161 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1162 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1163 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
1164 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
1165 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
1166 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
1167 ubx
= F_TO_I(127.0F
* a
[0] + 128.0F
);
1168 uby
= F_TO_I(127.0F
* a
[1] + 128.0F
);
1169 ubz
= F_TO_I(127.0F
* a
[2] + 128.0F
);
1170 ubw
= F_TO_I(127.0F
* a
[3] + 128.0F
);
1174 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1175 store_vector4ui(inst
, machine
, result
);
1178 case OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
1181 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1182 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1183 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1184 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1185 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
1186 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
1187 ubx
= F_TO_I(255.0F
* a
[0]);
1188 uby
= F_TO_I(255.0F
* a
[1]);
1189 ubz
= F_TO_I(255.0F
* a
[2]);
1190 ubw
= F_TO_I(255.0F
* a
[3]);
1194 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1195 store_vector4ui(inst
, machine
, result
);
1200 GLfloat a
[4], b
[4], result
[4];
1201 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1202 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
1203 result
[0] = result
[1] = result
[2] = result
[3]
1204 = (GLfloat
) pow(a
[0], b
[0]);
1205 store_vector4(inst
, machine
, result
);
1211 GLfloat a
[4], result
[4];
1212 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1216 else if (IS_INF_OR_NAN(a
[0]))
1217 printf("RCP(inf)\n");
1219 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
1220 store_vector4(inst
, machine
, result
);
1223 case OPCODE_RET
: /* return from subroutine (conditional) */
1224 if (eval_condition(machine
, inst
)) {
1225 if (machine
->StackDepth
== 0) {
1226 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
1228 /* subtract one because of pc++ in the for loop */
1229 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
1232 case OPCODE_RFL
: /* reflection vector */
1234 GLfloat axis
[4], dir
[4], result
[4], tmpX
, tmpW
;
1235 fetch_vector4(&inst
->SrcReg
[0], machine
, axis
);
1236 fetch_vector4(&inst
->SrcReg
[1], machine
, dir
);
1237 tmpW
= DOT3(axis
, axis
);
1238 tmpX
= (2.0F
* DOT3(axis
, dir
)) / tmpW
;
1239 result
[0] = tmpX
* axis
[0] - dir
[0];
1240 result
[1] = tmpX
* axis
[1] - dir
[1];
1241 result
[2] = tmpX
* axis
[2] - dir
[2];
1242 /* result[3] is never written! XXX enforce in parser! */
1243 store_vector4(inst
, machine
, result
);
1246 case OPCODE_RSQ
: /* 1 / sqrt() */
1248 GLfloat a
[4], result
[4];
1249 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1251 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1252 store_vector4(inst
, machine
, result
);
1254 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
1258 case OPCODE_SCS
: /* sine and cos */
1260 GLfloat a
[4], result
[4];
1261 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1262 result
[0] = (GLfloat
) cos(a
[0]);
1263 result
[1] = (GLfloat
) sin(a
[0]);
1264 result
[2] = 0.0; /* undefined! */
1265 result
[3] = 0.0; /* undefined! */
1266 store_vector4(inst
, machine
, result
);
1269 case OPCODE_SEQ
: /* set on equal */
1271 GLfloat a
[4], b
[4], result
[4];
1272 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1273 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1274 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1275 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1276 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1277 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1278 store_vector4(inst
, machine
, result
);
1280 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1281 result
[0], result
[1], result
[2], result
[3],
1282 a
[0], a
[1], a
[2], a
[3],
1283 b
[0], b
[1], b
[2], b
[3]);
1287 case OPCODE_SFL
: /* set false, operands ignored */
1289 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1290 store_vector4(inst
, machine
, result
);
1293 case OPCODE_SGE
: /* set on greater or equal */
1295 GLfloat a
[4], b
[4], result
[4];
1296 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1297 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1298 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1299 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1300 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1301 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1302 store_vector4(inst
, machine
, result
);
1304 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1305 result
[0], result
[1], result
[2], result
[3],
1306 a
[0], a
[1], a
[2], a
[3],
1307 b
[0], b
[1], b
[2], b
[3]);
1311 case OPCODE_SGT
: /* set on greater */
1313 GLfloat a
[4], b
[4], result
[4];
1314 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1315 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1316 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1317 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1318 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1319 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1320 store_vector4(inst
, machine
, result
);
1322 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1323 result
[0], result
[1], result
[2], result
[3],
1324 a
[0], a
[1], a
[2], a
[3],
1325 b
[0], b
[1], b
[2], b
[3]);
1331 GLfloat a
[4], result
[4];
1332 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1333 result
[0] = result
[1] = result
[2] = result
[3]
1334 = (GLfloat
) sin(a
[0]);
1335 store_vector4(inst
, machine
, result
);
1338 case OPCODE_SLE
: /* set on less or equal */
1340 GLfloat a
[4], b
[4], result
[4];
1341 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1342 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1343 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1344 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1345 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1346 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1347 store_vector4(inst
, machine
, result
);
1349 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1350 result
[0], result
[1], result
[2], result
[3],
1351 a
[0], a
[1], a
[2], a
[3],
1352 b
[0], b
[1], b
[2], b
[3]);
1356 case OPCODE_SLT
: /* set on less */
1358 GLfloat a
[4], b
[4], result
[4];
1359 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1360 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1361 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1362 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1363 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1364 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1365 store_vector4(inst
, machine
, result
);
1367 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1368 result
[0], result
[1], result
[2], result
[3],
1369 a
[0], a
[1], a
[2], a
[3],
1370 b
[0], b
[1], b
[2], b
[3]);
1374 case OPCODE_SNE
: /* set on not equal */
1376 GLfloat a
[4], b
[4], result
[4];
1377 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1378 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1379 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1380 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1381 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1382 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1383 store_vector4(inst
, machine
, result
);
1385 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1386 result
[0], result
[1], result
[2], result
[3],
1387 a
[0], a
[1], a
[2], a
[3],
1388 b
[0], b
[1], b
[2], b
[3]);
1392 case OPCODE_SSG
: /* set sign (-1, 0 or +1) */
1394 GLfloat a
[4], result
[4];
1395 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1396 result
[0] = (GLfloat
) ((a
[0] > 0.0F
) - (a
[0] < 0.0F
));
1397 result
[1] = (GLfloat
) ((a
[1] > 0.0F
) - (a
[1] < 0.0F
));
1398 result
[2] = (GLfloat
) ((a
[2] > 0.0F
) - (a
[2] < 0.0F
));
1399 result
[3] = (GLfloat
) ((a
[3] > 0.0F
) - (a
[3] < 0.0F
));
1400 store_vector4(inst
, machine
, result
);
1403 case OPCODE_STR
: /* set true, operands ignored */
1405 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1406 store_vector4(inst
, machine
, result
);
1411 GLfloat a
[4], b
[4], result
[4];
1412 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1413 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1414 result
[0] = a
[0] - b
[0];
1415 result
[1] = a
[1] - b
[1];
1416 result
[2] = a
[2] - b
[2];
1417 result
[3] = a
[3] - b
[3];
1418 store_vector4(inst
, machine
, result
);
1420 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1421 result
[0], result
[1], result
[2], result
[3],
1422 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1426 case OPCODE_SWZ
: /* extended swizzle */
1428 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1429 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
1432 for (i
= 0; i
< 4; i
++) {
1433 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1434 if (swz
== SWIZZLE_ZERO
)
1436 else if (swz
== SWIZZLE_ONE
)
1441 result
[i
] = src
[swz
];
1443 if (source
->Negate
& (1 << i
))
1444 result
[i
] = -result
[i
];
1446 store_vector4(inst
, machine
, result
);
1449 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1450 /* Simple texel lookup */
1452 GLfloat texcoord
[4], color
[4];
1453 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1455 /* For TEX, texcoord.Q should not be used and its value should not
1456 * matter (at most, we pass coord.xyz to texture3D() in GLSL).
1457 * Set Q=1 so that FetchTexelDeriv() doesn't get a garbage value
1458 * which is effectively what happens when the texcoord swizzle
1463 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1466 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1467 color
[0], color
[1], color
[2], color
[3],
1469 texcoord
[0], texcoord
[1], texcoord
[2], texcoord
[3]);
1471 store_vector4(inst
, machine
, color
);
1474 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1475 /* Texel lookup with LOD bias */
1477 GLfloat texcoord
[4], color
[4], lodBias
;
1479 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1481 /* texcoord[3] is the bias to add to lambda */
1482 lodBias
= texcoord
[3];
1484 fetch_texel(ctx
, machine
, inst
, texcoord
, lodBias
, color
);
1487 printf("TXB (%g, %g, %g, %g) = texture[%d][%g %g %g %g]"
1489 color
[0], color
[1], color
[2], color
[3],
1498 store_vector4(inst
, machine
, color
);
1501 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1502 /* Texture lookup w/ partial derivatives for LOD */
1504 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1505 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1506 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1507 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1508 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1510 inst
->TexSrcUnit
, color
);
1511 store_vector4(inst
, machine
, color
);
1515 /* Texel lookup with explicit LOD */
1517 GLfloat texcoord
[4], color
[4], lod
;
1519 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1521 /* texcoord[3] is the LOD */
1524 machine
->FetchTexelLod(ctx
, texcoord
, lod
,
1525 machine
->Samplers
[inst
->TexSrcUnit
], color
);
1527 store_vector4(inst
, machine
, color
);
1530 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1531 /* Texture lookup w/ projective divide */
1533 GLfloat texcoord
[4], color
[4];
1535 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1536 /* Not so sure about this test - if texcoord[3] is
1537 * zero, we'd probably be fine except for an ASSERT in
1538 * IROUND_POS() which gets triggered by the inf values created.
1540 if (texcoord
[3] != 0.0) {
1541 texcoord
[0] /= texcoord
[3];
1542 texcoord
[1] /= texcoord
[3];
1543 texcoord
[2] /= texcoord
[3];
1546 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1548 store_vector4(inst
, machine
, color
);
1551 case OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1552 /* Texture lookup w/ projective divide, as above, but do not
1553 * do the divide by w if sampling from a cube map.
1556 GLfloat texcoord
[4], color
[4];
1558 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1559 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1560 texcoord
[3] != 0.0) {
1561 texcoord
[0] /= texcoord
[3];
1562 texcoord
[1] /= texcoord
[3];
1563 texcoord
[2] /= texcoord
[3];
1566 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1568 store_vector4(inst
, machine
, color
);
1571 case OPCODE_TRUNC
: /* truncate toward zero */
1573 GLfloat a
[4], result
[4];
1574 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1575 result
[0] = (GLfloat
) (GLint
) a
[0];
1576 result
[1] = (GLfloat
) (GLint
) a
[1];
1577 result
[2] = (GLfloat
) (GLint
) a
[2];
1578 result
[3] = (GLfloat
) (GLint
) a
[3];
1579 store_vector4(inst
, machine
, result
);
1582 case OPCODE_UP2H
: /* unpack two 16-bit floats */
1584 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1589 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1590 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1591 store_vector4(inst
, machine
, result
);
1594 case OPCODE_UP2US
: /* unpack two GLushorts */
1596 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1601 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1602 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1603 store_vector4(inst
, machine
, result
);
1606 case OPCODE_UP4B
: /* unpack four GLbytes */
1608 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1610 result
[0] = (((raw
>> 0) & 0xff) - 128) / 127.0F
;
1611 result
[1] = (((raw
>> 8) & 0xff) - 128) / 127.0F
;
1612 result
[2] = (((raw
>> 16) & 0xff) - 128) / 127.0F
;
1613 result
[3] = (((raw
>> 24) & 0xff) - 128) / 127.0F
;
1614 store_vector4(inst
, machine
, result
);
1617 case OPCODE_UP4UB
: /* unpack four GLubytes */
1619 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1621 result
[0] = ((raw
>> 0) & 0xff) / 255.0F
;
1622 result
[1] = ((raw
>> 8) & 0xff) / 255.0F
;
1623 result
[2] = ((raw
>> 16) & 0xff) / 255.0F
;
1624 result
[3] = ((raw
>> 24) & 0xff) / 255.0F
;
1625 store_vector4(inst
, machine
, result
);
1628 case OPCODE_XPD
: /* cross product */
1630 GLfloat a
[4], b
[4], result
[4];
1631 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1632 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1633 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1634 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1635 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1637 store_vector4(inst
, machine
, result
);
1639 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1640 result
[0], result
[1], result
[2], result
[3],
1641 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1645 case OPCODE_X2D
: /* 2-D matrix transform */
1647 GLfloat a
[4], b
[4], c
[4], result
[4];
1648 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1649 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1650 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1651 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1652 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1653 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1654 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1655 store_vector4(inst
, machine
, result
);
1661 _mesa_problem(ctx
, "Bad opcode %d in _mesa_execute_program",
1663 return GL_TRUE
; /* return value doesn't matter */
1667 if (numExec
> maxExec
) {
1668 static GLboolean reported
= GL_FALSE
;
1670 _mesa_problem(ctx
, "Infinite loop detected in fragment program");