2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \file prog_execute.c
27 * Software interpreter for vertex/fragment programs.
32 * NOTE: we do everything in single-precision floating point; we don't
33 * currently observe the single/half/fixed-precision qualifiers.
38 #include "main/glheader.h"
39 #include "main/colormac.h"
40 #include "main/context.h"
42 #include "prog_execute.h"
43 #include "prog_instruction.h"
44 #include "prog_parameter.h"
45 #include "prog_print.h"
46 #include "prog_noise.h"
54 * Set x to positive or negative infinity.
56 #if defined(USE_IEEE) || defined(_WIN32)
57 #define SET_POS_INFINITY(x) ( *((GLuint *) (void *)&x) = 0x7F800000 )
58 #define SET_NEG_INFINITY(x) ( *((GLuint *) (void *)&x) = 0xFF800000 )
60 #define SET_POS_INFINITY(x) x = __MAXFLOAT
61 #define SET_NEG_INFINITY(x) x = -__MAXFLOAT
63 #define SET_POS_INFINITY(x) x = (GLfloat) HUGE_VAL
64 #define SET_NEG_INFINITY(x) x = (GLfloat) -HUGE_VAL
67 #define SET_FLOAT_BITS(x, bits) ((fi_type *) (void *) &(x))->i = bits
70 static const GLfloat ZeroVec
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
75 * Return a pointer to the 4-element float vector specified by the given
78 static INLINE
const GLfloat
*
79 get_src_register_pointer(const struct prog_src_register
*source
,
80 const struct gl_program_machine
*machine
)
82 const struct gl_program
*prog
= machine
->CurProgram
;
83 GLint reg
= source
->Index
;
85 if (source
->RelAddr
) {
86 /* add address register value to src index/offset */
87 reg
+= machine
->AddressReg
[0][0];
93 switch (source
->File
) {
94 case PROGRAM_TEMPORARY
:
95 if (reg
>= MAX_PROGRAM_TEMPS
)
97 return machine
->Temporaries
[reg
];
100 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
101 if (reg
>= VERT_ATTRIB_MAX
)
103 return machine
->VertAttribs
[reg
];
106 if (reg
>= FRAG_ATTRIB_MAX
)
108 return machine
->Attribs
[reg
][machine
->CurElement
];
112 if (reg
>= MAX_PROGRAM_OUTPUTS
)
114 return machine
->Outputs
[reg
];
116 case PROGRAM_LOCAL_PARAM
:
117 if (reg
>= MAX_PROGRAM_LOCAL_PARAMS
)
119 return machine
->CurProgram
->LocalParams
[reg
];
121 case PROGRAM_ENV_PARAM
:
122 if (reg
>= MAX_PROGRAM_ENV_PARAMS
)
124 return machine
->EnvParams
[reg
];
126 case PROGRAM_STATE_VAR
:
128 case PROGRAM_CONSTANT
:
130 case PROGRAM_UNIFORM
:
132 case PROGRAM_NAMED_PARAM
:
133 if (reg
>= (GLint
) prog
->Parameters
->NumParameters
)
135 return prog
->Parameters
->ParameterValues
[reg
];
139 "Invalid src register file %d in get_src_register_pointer()",
147 * Return a pointer to the 4-element float vector specified by the given
148 * destination register.
150 static INLINE GLfloat
*
151 get_dst_register_pointer(const struct prog_dst_register
*dest
,
152 struct gl_program_machine
*machine
)
154 static GLfloat dummyReg
[4];
155 GLint reg
= dest
->Index
;
158 /* add address register value to src index/offset */
159 reg
+= machine
->AddressReg
[0][0];
165 switch (dest
->File
) {
166 case PROGRAM_TEMPORARY
:
167 if (reg
>= MAX_PROGRAM_TEMPS
)
169 return machine
->Temporaries
[reg
];
172 if (reg
>= MAX_PROGRAM_OUTPUTS
)
174 return machine
->Outputs
[reg
];
176 case PROGRAM_WRITE_ONLY
:
181 "Invalid dest register file %d in get_dst_register_pointer()",
190 * Fetch a 4-element float vector from the given source register.
191 * Apply swizzling and negating as needed.
194 fetch_vector4(const struct prog_src_register
*source
,
195 const struct gl_program_machine
*machine
, GLfloat result
[4])
197 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
200 if (source
->Swizzle
== SWIZZLE_NOOP
) {
202 COPY_4V(result
, src
);
205 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
206 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
207 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
208 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
209 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
210 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
211 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
212 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
216 result
[0] = FABSF(result
[0]);
217 result
[1] = FABSF(result
[1]);
218 result
[2] = FABSF(result
[2]);
219 result
[3] = FABSF(result
[3]);
221 if (source
->Negate
) {
222 ASSERT(source
->Negate
== NEGATE_XYZW
);
223 result
[0] = -result
[0];
224 result
[1] = -result
[1];
225 result
[2] = -result
[2];
226 result
[3] = -result
[3];
232 * Fetch a 4-element uint vector from the given source register.
233 * Apply swizzling but not negation/abs.
236 fetch_vector4ui(const struct prog_src_register
*source
,
237 const struct gl_program_machine
*machine
, GLuint result
[4])
239 const GLuint
*src
= (GLuint
*) get_src_register_pointer(source
, machine
);
242 if (source
->Swizzle
== SWIZZLE_NOOP
) {
244 COPY_4V(result
, src
);
247 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
248 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
249 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
250 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
251 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
252 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
253 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
254 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
257 /* Note: no Negate or Abs here */
263 * Fetch the derivative with respect to X or Y for the given register.
264 * XXX this currently only works for fragment program input attribs.
267 fetch_vector4_deriv(GLcontext
* ctx
,
268 const struct prog_src_register
*source
,
269 const struct gl_program_machine
*machine
,
270 char xOrY
, GLfloat result
[4])
272 if (source
->File
== PROGRAM_INPUT
&&
273 source
->Index
< (GLint
) machine
->NumDeriv
) {
274 const GLint col
= machine
->CurElement
;
275 const GLfloat w
= machine
->Attribs
[FRAG_ATTRIB_WPOS
][col
][3];
276 const GLfloat invQ
= 1.0f
/ w
;
280 deriv
[0] = machine
->DerivX
[source
->Index
][0] * invQ
;
281 deriv
[1] = machine
->DerivX
[source
->Index
][1] * invQ
;
282 deriv
[2] = machine
->DerivX
[source
->Index
][2] * invQ
;
283 deriv
[3] = machine
->DerivX
[source
->Index
][3] * invQ
;
286 deriv
[0] = machine
->DerivY
[source
->Index
][0] * invQ
;
287 deriv
[1] = machine
->DerivY
[source
->Index
][1] * invQ
;
288 deriv
[2] = machine
->DerivY
[source
->Index
][2] * invQ
;
289 deriv
[3] = machine
->DerivY
[source
->Index
][3] * invQ
;
292 result
[0] = deriv
[GET_SWZ(source
->Swizzle
, 0)];
293 result
[1] = deriv
[GET_SWZ(source
->Swizzle
, 1)];
294 result
[2] = deriv
[GET_SWZ(source
->Swizzle
, 2)];
295 result
[3] = deriv
[GET_SWZ(source
->Swizzle
, 3)];
298 result
[0] = FABSF(result
[0]);
299 result
[1] = FABSF(result
[1]);
300 result
[2] = FABSF(result
[2]);
301 result
[3] = FABSF(result
[3]);
303 if (source
->Negate
) {
304 ASSERT(source
->Negate
== NEGATE_XYZW
);
305 result
[0] = -result
[0];
306 result
[1] = -result
[1];
307 result
[2] = -result
[2];
308 result
[3] = -result
[3];
312 ASSIGN_4V(result
, 0.0, 0.0, 0.0, 0.0);
318 * As above, but only return result[0] element.
321 fetch_vector1(const struct prog_src_register
*source
,
322 const struct gl_program_machine
*machine
, GLfloat result
[4])
324 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
327 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
330 result
[0] = FABSF(result
[0]);
332 if (source
->Negate
) {
333 result
[0] = -result
[0];
339 * Fetch texel from texture. Use partial derivatives when possible.
342 fetch_texel(GLcontext
*ctx
,
343 const struct gl_program_machine
*machine
,
344 const struct prog_instruction
*inst
,
345 const GLfloat texcoord
[4], GLfloat lodBias
,
348 const GLuint unit
= machine
->Samplers
[inst
->TexSrcUnit
];
350 /* Note: we only have the right derivatives for fragment input attribs.
352 if (machine
->NumDeriv
> 0 &&
353 inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
354 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
) {
355 /* simple texture fetch for which we should have derivatives */
356 GLuint attr
= inst
->SrcReg
[0].Index
;
357 machine
->FetchTexelDeriv(ctx
, texcoord
,
358 machine
->DerivX
[attr
],
359 machine
->DerivY
[attr
],
360 lodBias
, unit
, color
);
363 machine
->FetchTexelLod(ctx
, texcoord
, lodBias
, unit
, color
);
369 * Test value against zero and return GT, LT, EQ or UN if NaN.
372 generate_cc(float value
)
375 return COND_UN
; /* NaN */
385 * Test if the ccMaskRule is satisfied by the given condition code.
386 * Used to mask destination writes according to the current condition code.
388 static INLINE GLboolean
389 test_cc(GLuint condCode
, GLuint ccMaskRule
)
391 switch (ccMaskRule
) {
392 case COND_EQ
: return (condCode
== COND_EQ
);
393 case COND_NE
: return (condCode
!= COND_EQ
);
394 case COND_LT
: return (condCode
== COND_LT
);
395 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
396 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
397 case COND_GT
: return (condCode
== COND_GT
);
398 case COND_TR
: return GL_TRUE
;
399 case COND_FL
: return GL_FALSE
;
400 default: return GL_TRUE
;
406 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
407 * or GL_FALSE to indicate result.
409 static INLINE GLboolean
410 eval_condition(const struct gl_program_machine
*machine
,
411 const struct prog_instruction
*inst
)
413 const GLuint swizzle
= inst
->DstReg
.CondSwizzle
;
414 const GLuint condMask
= inst
->DstReg
.CondMask
;
415 if (test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 0)], condMask
) ||
416 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 1)], condMask
) ||
417 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 2)], condMask
) ||
418 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 3)], condMask
)) {
429 * Store 4 floats into a register. Observe the instructions saturate and
430 * set-condition-code flags.
433 store_vector4(const struct prog_instruction
*inst
,
434 struct gl_program_machine
*machine
, const GLfloat value
[4])
436 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
437 const GLboolean clamp
= inst
->SaturateMode
== SATURATE_ZERO_ONE
;
438 GLuint writeMask
= dstReg
->WriteMask
;
439 GLfloat clampedValue
[4];
440 GLfloat
*dst
= get_dst_register_pointer(dstReg
, machine
);
443 if (value
[0] > 1.0e10
||
444 IS_INF_OR_NAN(value
[0]) ||
445 IS_INF_OR_NAN(value
[1]) ||
446 IS_INF_OR_NAN(value
[2]) || IS_INF_OR_NAN(value
[3]))
447 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
451 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
452 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
453 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
454 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
455 value
= clampedValue
;
458 if (dstReg
->CondMask
!= COND_TR
) {
459 /* condition codes may turn off some writes */
460 if (writeMask
& WRITEMASK_X
) {
461 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
463 writeMask
&= ~WRITEMASK_X
;
465 if (writeMask
& WRITEMASK_Y
) {
466 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
468 writeMask
&= ~WRITEMASK_Y
;
470 if (writeMask
& WRITEMASK_Z
) {
471 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
473 writeMask
&= ~WRITEMASK_Z
;
475 if (writeMask
& WRITEMASK_W
) {
476 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
478 writeMask
&= ~WRITEMASK_W
;
482 if (writeMask
& WRITEMASK_X
)
484 if (writeMask
& WRITEMASK_Y
)
486 if (writeMask
& WRITEMASK_Z
)
488 if (writeMask
& WRITEMASK_W
)
491 if (inst
->CondUpdate
) {
492 if (writeMask
& WRITEMASK_X
)
493 machine
->CondCodes
[0] = generate_cc(value
[0]);
494 if (writeMask
& WRITEMASK_Y
)
495 machine
->CondCodes
[1] = generate_cc(value
[1]);
496 if (writeMask
& WRITEMASK_Z
)
497 machine
->CondCodes
[2] = generate_cc(value
[2]);
498 if (writeMask
& WRITEMASK_W
)
499 machine
->CondCodes
[3] = generate_cc(value
[3]);
501 printf("CondCodes=(%s,%s,%s,%s) for:\n",
502 _mesa_condcode_string(machine
->CondCodes
[0]),
503 _mesa_condcode_string(machine
->CondCodes
[1]),
504 _mesa_condcode_string(machine
->CondCodes
[2]),
505 _mesa_condcode_string(machine
->CondCodes
[3]));
512 * Store 4 uints into a register. Observe the set-condition-code flags.
515 store_vector4ui(const struct prog_instruction
*inst
,
516 struct gl_program_machine
*machine
, const GLuint value
[4])
518 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
519 GLuint writeMask
= dstReg
->WriteMask
;
520 GLuint
*dst
= (GLuint
*) get_dst_register_pointer(dstReg
, machine
);
522 if (dstReg
->CondMask
!= COND_TR
) {
523 /* condition codes may turn off some writes */
524 if (writeMask
& WRITEMASK_X
) {
525 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
527 writeMask
&= ~WRITEMASK_X
;
529 if (writeMask
& WRITEMASK_Y
) {
530 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
532 writeMask
&= ~WRITEMASK_Y
;
534 if (writeMask
& WRITEMASK_Z
) {
535 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
537 writeMask
&= ~WRITEMASK_Z
;
539 if (writeMask
& WRITEMASK_W
) {
540 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
542 writeMask
&= ~WRITEMASK_W
;
546 if (writeMask
& WRITEMASK_X
)
548 if (writeMask
& WRITEMASK_Y
)
550 if (writeMask
& WRITEMASK_Z
)
552 if (writeMask
& WRITEMASK_W
)
555 if (inst
->CondUpdate
) {
556 if (writeMask
& WRITEMASK_X
)
557 machine
->CondCodes
[0] = generate_cc(value
[0]);
558 if (writeMask
& WRITEMASK_Y
)
559 machine
->CondCodes
[1] = generate_cc(value
[1]);
560 if (writeMask
& WRITEMASK_Z
)
561 machine
->CondCodes
[2] = generate_cc(value
[2]);
562 if (writeMask
& WRITEMASK_W
)
563 machine
->CondCodes
[3] = generate_cc(value
[3]);
565 printf("CondCodes=(%s,%s,%s,%s) for:\n",
566 _mesa_condcode_string(machine
->CondCodes
[0]),
567 _mesa_condcode_string(machine
->CondCodes
[1]),
568 _mesa_condcode_string(machine
->CondCodes
[2]),
569 _mesa_condcode_string(machine
->CondCodes
[3]));
577 * Execute the given vertex/fragment program.
579 * \param ctx rendering context
580 * \param program the program to execute
581 * \param machine machine state (must be initialized)
582 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
585 _mesa_execute_program(GLcontext
* ctx
,
586 const struct gl_program
*program
,
587 struct gl_program_machine
*machine
)
589 const GLuint numInst
= program
->NumInstructions
;
590 const GLuint maxExec
= 10000;
591 GLuint pc
, numExec
= 0;
593 machine
->CurProgram
= program
;
596 printf("execute program %u --------------------\n", program
->Id
);
599 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
600 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
603 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
606 for (pc
= 0; pc
< numInst
; pc
++) {
607 const struct prog_instruction
*inst
= program
->Instructions
+ pc
;
610 _mesa_print_instruction(inst
);
613 switch (inst
->Opcode
) {
616 GLfloat a
[4], result
[4];
617 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
618 result
[0] = FABSF(a
[0]);
619 result
[1] = FABSF(a
[1]);
620 result
[2] = FABSF(a
[2]);
621 result
[3] = FABSF(a
[3]);
622 store_vector4(inst
, machine
, result
);
627 GLfloat a
[4], b
[4], result
[4];
628 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
629 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
630 result
[0] = a
[0] + b
[0];
631 result
[1] = a
[1] + b
[1];
632 result
[2] = a
[2] + b
[2];
633 result
[3] = a
[3] + b
[3];
634 store_vector4(inst
, machine
, result
);
636 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
637 result
[0], result
[1], result
[2], result
[3],
638 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
642 case OPCODE_AND
: /* bitwise AND */
644 GLuint a
[4], b
[4], result
[4];
645 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
646 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
647 result
[0] = a
[0] & b
[0];
648 result
[1] = a
[1] & b
[1];
649 result
[2] = a
[2] & b
[2];
650 result
[3] = a
[3] & b
[3];
651 store_vector4ui(inst
, machine
, result
);
657 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
658 machine
->AddressReg
[0][0] = IFLOOR(t
[0]);
665 /* subtract 1 here since pc is incremented by for(pc) loop */
666 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
668 case OPCODE_BGNSUB
: /* begin subroutine */
670 case OPCODE_ENDSUB
: /* end subroutine */
672 case OPCODE_BRA
: /* branch (conditional) */
674 case OPCODE_BRK
: /* break out of loop (conditional) */
676 case OPCODE_CONT
: /* continue loop (conditional) */
677 if (eval_condition(machine
, inst
)) {
679 /* Subtract 1 here since we'll do pc++ at end of for-loop */
680 pc
= inst
->BranchTarget
- 1;
683 case OPCODE_CAL
: /* Call subroutine (conditional) */
684 if (eval_condition(machine
, inst
)) {
685 /* call the subroutine */
686 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
687 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
689 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1; /* next inst */
690 /* Subtract 1 here since we'll do pc++ at end of for-loop */
691 pc
= inst
->BranchTarget
- 1;
696 GLfloat a
[4], b
[4], c
[4], result
[4];
697 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
698 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
699 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
700 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
701 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
702 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
703 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
704 store_vector4(inst
, machine
, result
);
709 GLfloat a
[4], result
[4];
710 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
711 result
[0] = result
[1] = result
[2] = result
[3]
712 = (GLfloat
) _mesa_cos(a
[0]);
713 store_vector4(inst
, machine
, result
);
716 case OPCODE_DDX
: /* Partial derivative with respect to X */
719 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
721 store_vector4(inst
, machine
, result
);
724 case OPCODE_DDY
: /* Partial derivative with respect to Y */
727 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
729 store_vector4(inst
, machine
, result
);
734 GLfloat a
[4], b
[4], result
[4];
735 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
736 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
737 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
);
738 store_vector4(inst
, machine
, result
);
740 printf("DP2 %g = (%g %g) . (%g %g)\n",
741 result
[0], a
[0], a
[1], b
[0], b
[1]);
747 GLfloat a
[4], b
[4], c
, result
[4];
748 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
749 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
750 fetch_vector1(&inst
->SrcReg
[1], machine
, &c
);
751 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
) + c
;
752 store_vector4(inst
, machine
, result
);
754 printf("DP2A %g = (%g %g) . (%g %g) + %g\n",
755 result
[0], a
[0], a
[1], b
[0], b
[1], c
);
761 GLfloat a
[4], b
[4], result
[4];
762 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
763 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
764 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
765 store_vector4(inst
, machine
, result
);
767 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
768 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
774 GLfloat a
[4], b
[4], result
[4];
775 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
776 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
777 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
, b
);
778 store_vector4(inst
, machine
, result
);
780 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
781 result
[0], a
[0], a
[1], a
[2], a
[3],
782 b
[0], b
[1], b
[2], b
[3]);
788 GLfloat a
[4], b
[4], result
[4];
789 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
790 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
791 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
) + b
[3];
792 store_vector4(inst
, machine
, result
);
795 case OPCODE_DST
: /* Distance vector */
797 GLfloat a
[4], b
[4], result
[4];
798 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
799 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
801 result
[1] = a
[1] * b
[1];
804 store_vector4(inst
, machine
, result
);
809 GLfloat t
[4], q
[4], floor_t0
;
810 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
811 floor_t0
= FLOORF(t
[0]);
812 if (floor_t0
> FLT_MAX_EXP
) {
813 SET_POS_INFINITY(q
[0]);
814 SET_POS_INFINITY(q
[2]);
816 else if (floor_t0
< FLT_MIN_EXP
) {
821 q
[0] = LDEXPF(1.0, (int) floor_t0
);
822 /* Note: GL_NV_vertex_program expects
823 * result.z = result.x * APPX(result.y)
824 * We do what the ARB extension says.
826 q
[2] = (GLfloat
) _mesa_pow(2.0, t
[0]);
828 q
[1] = t
[0] - floor_t0
;
830 store_vector4( inst
, machine
, q
);
833 case OPCODE_EX2
: /* Exponential base 2 */
835 GLfloat a
[4], result
[4];
836 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
837 result
[0] = result
[1] = result
[2] = result
[3] =
838 (GLfloat
) _mesa_pow(2.0, a
[0]);
839 store_vector4(inst
, machine
, result
);
844 GLfloat a
[4], result
[4];
845 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
846 result
[0] = FLOORF(a
[0]);
847 result
[1] = FLOORF(a
[1]);
848 result
[2] = FLOORF(a
[2]);
849 result
[3] = FLOORF(a
[3]);
850 store_vector4(inst
, machine
, result
);
855 GLfloat a
[4], result
[4];
856 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
857 result
[0] = a
[0] - FLOORF(a
[0]);
858 result
[1] = a
[1] - FLOORF(a
[1]);
859 result
[2] = a
[2] - FLOORF(a
[2]);
860 result
[3] = a
[3] - FLOORF(a
[3]);
861 store_vector4(inst
, machine
, result
);
868 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
870 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
871 cond
= (a
[0] != 0.0);
874 cond
= eval_condition(machine
, inst
);
877 printf("IF: %d\n", cond
);
881 /* do if-clause (just continue execution) */
884 /* go to the instruction after ELSE or ENDIF */
885 assert(inst
->BranchTarget
>= 0);
886 pc
= inst
->BranchTarget
- 1;
892 assert(inst
->BranchTarget
>= 0);
893 pc
= inst
->BranchTarget
- 1;
898 case OPCODE_KIL_NV
: /* NV_f_p only (conditional) */
899 if (eval_condition(machine
, inst
)) {
903 case OPCODE_KIL
: /* ARB_f_p only */
906 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
907 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
912 case OPCODE_LG2
: /* log base 2 */
914 GLfloat a
[4], result
[4];
915 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
916 /* The fast LOG2 macro doesn't meet the precision requirements.
918 result
[0] = result
[1] = result
[2] = result
[3] =
919 (log(a
[0]) * 1.442695F
);
920 store_vector4(inst
, machine
, result
);
925 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
926 GLfloat a
[4], result
[4];
927 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
928 a
[0] = MAX2(a
[0], 0.0F
);
929 a
[1] = MAX2(a
[1], 0.0F
);
930 /* XXX ARB version clamps a[3], NV version doesn't */
931 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
934 /* XXX we could probably just use pow() here */
936 if (a
[1] == 0.0 && a
[3] == 0.0)
939 result
[2] = (GLfloat
) _mesa_pow(a
[1], a
[3]);
945 store_vector4(inst
, machine
, result
);
947 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
948 result
[0], result
[1], result
[2], result
[3],
949 a
[0], a
[1], a
[2], a
[3]);
955 GLfloat t
[4], q
[4], abs_t0
;
956 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
957 abs_t0
= FABSF(t
[0]);
958 if (abs_t0
!= 0.0F
) {
959 /* Since we really can't handle infinite values on VMS
960 * like other OSes we'll use __MAXFLOAT to represent
961 * infinity. This may need some tweaking.
964 if (abs_t0
== __MAXFLOAT
)
966 if (IS_INF_OR_NAN(abs_t0
))
969 SET_POS_INFINITY(q
[0]);
971 SET_POS_INFINITY(q
[2]);
975 GLfloat mantissa
= FREXPF(t
[0], &exponent
);
976 q
[0] = (GLfloat
) (exponent
- 1);
977 q
[1] = (GLfloat
) (2.0 * mantissa
); /* map [.5, 1) -> [1, 2) */
979 /* The fast LOG2 macro doesn't meet the precision
982 q
[2] = (log(t
[0]) * 1.442695F
);
986 SET_NEG_INFINITY(q
[0]);
988 SET_NEG_INFINITY(q
[2]);
991 store_vector4(inst
, machine
, q
);
996 GLfloat a
[4], b
[4], c
[4], result
[4];
997 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
998 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
999 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1000 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
1001 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
1002 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
1003 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
1004 store_vector4(inst
, machine
, result
);
1006 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
1007 "(%g %g %g %g), (%g %g %g %g)\n",
1008 result
[0], result
[1], result
[2], result
[3],
1009 a
[0], a
[1], a
[2], a
[3],
1010 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1016 GLfloat a
[4], b
[4], c
[4], result
[4];
1017 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1018 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1019 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1020 result
[0] = a
[0] * b
[0] + c
[0];
1021 result
[1] = a
[1] * b
[1] + c
[1];
1022 result
[2] = a
[2] * b
[2] + c
[2];
1023 result
[3] = a
[3] * b
[3] + c
[3];
1024 store_vector4(inst
, machine
, result
);
1026 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
1027 "(%g %g %g %g) + (%g %g %g %g)\n",
1028 result
[0], result
[1], result
[2], result
[3],
1029 a
[0], a
[1], a
[2], a
[3],
1030 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1036 GLfloat a
[4], b
[4], result
[4];
1037 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1038 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1039 result
[0] = MAX2(a
[0], b
[0]);
1040 result
[1] = MAX2(a
[1], b
[1]);
1041 result
[2] = MAX2(a
[2], b
[2]);
1042 result
[3] = MAX2(a
[3], b
[3]);
1043 store_vector4(inst
, machine
, result
);
1045 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
1046 result
[0], result
[1], result
[2], result
[3],
1047 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1053 GLfloat a
[4], b
[4], result
[4];
1054 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1055 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1056 result
[0] = MIN2(a
[0], b
[0]);
1057 result
[1] = MIN2(a
[1], b
[1]);
1058 result
[2] = MIN2(a
[2], b
[2]);
1059 result
[3] = MIN2(a
[3], b
[3]);
1060 store_vector4(inst
, machine
, result
);
1066 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
1067 store_vector4(inst
, machine
, result
);
1069 printf("MOV (%g %g %g %g)\n",
1070 result
[0], result
[1], result
[2], result
[3]);
1076 GLfloat a
[4], b
[4], result
[4];
1077 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1078 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1079 result
[0] = a
[0] * b
[0];
1080 result
[1] = a
[1] * b
[1];
1081 result
[2] = a
[2] * b
[2];
1082 result
[3] = a
[3] * b
[3];
1083 store_vector4(inst
, machine
, result
);
1085 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1086 result
[0], result
[1], result
[2], result
[3],
1087 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1093 GLfloat a
[4], result
[4];
1094 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1098 result
[3] = _mesa_noise1(a
[0]);
1099 store_vector4(inst
, machine
, result
);
1104 GLfloat a
[4], result
[4];
1105 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1108 result
[2] = result
[3] = _mesa_noise2(a
[0], a
[1]);
1109 store_vector4(inst
, machine
, result
);
1114 GLfloat a
[4], result
[4];
1115 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1119 result
[3] = _mesa_noise3(a
[0], a
[1], a
[2]);
1120 store_vector4(inst
, machine
, result
);
1125 GLfloat a
[4], result
[4];
1126 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1130 result
[3] = _mesa_noise4(a
[0], a
[1], a
[2], a
[3]);
1131 store_vector4(inst
, machine
, result
);
1136 case OPCODE_NOT
: /* bitwise NOT */
1138 GLuint a
[4], result
[4];
1139 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1144 store_vector4ui(inst
, machine
, result
);
1147 case OPCODE_NRM3
: /* 3-component normalization */
1149 GLfloat a
[4], result
[4];
1151 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1152 tmp
= a
[0] * a
[0] + a
[1] * a
[1] + a
[2] * a
[2];
1154 tmp
= INV_SQRTF(tmp
);
1155 result
[0] = tmp
* a
[0];
1156 result
[1] = tmp
* a
[1];
1157 result
[2] = tmp
* a
[2];
1158 result
[3] = 0.0; /* undefined, but prevent valgrind warnings */
1159 store_vector4(inst
, machine
, result
);
1162 case OPCODE_NRM4
: /* 4-component normalization */
1164 GLfloat a
[4], result
[4];
1166 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1167 tmp
= a
[0] * a
[0] + a
[1] * a
[1] + a
[2] * a
[2] + a
[3] * a
[3];
1169 tmp
= INV_SQRTF(tmp
);
1170 result
[0] = tmp
* a
[0];
1171 result
[1] = tmp
* a
[1];
1172 result
[2] = tmp
* a
[2];
1173 result
[3] = tmp
* a
[3];
1174 store_vector4(inst
, machine
, result
);
1177 case OPCODE_OR
: /* bitwise OR */
1179 GLuint a
[4], b
[4], result
[4];
1180 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1181 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
1182 result
[0] = a
[0] | b
[0];
1183 result
[1] = a
[1] | b
[1];
1184 result
[2] = a
[2] | b
[2];
1185 result
[3] = a
[3] | b
[3];
1186 store_vector4ui(inst
, machine
, result
);
1189 case OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
1194 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1195 hx
= _mesa_float_to_half(a
[0]);
1196 hy
= _mesa_float_to_half(a
[1]);
1200 result
[3] = hx
| (hy
<< 16);
1201 store_vector4ui(inst
, machine
, result
);
1204 case OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
1207 GLuint result
[4], usx
, usy
;
1208 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1209 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1210 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1211 usx
= IROUND(a
[0] * 65535.0F
);
1212 usy
= IROUND(a
[1] * 65535.0F
);
1216 result
[3] = usx
| (usy
<< 16);
1217 store_vector4ui(inst
, machine
, result
);
1220 case OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
1223 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1224 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1225 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
1226 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
1227 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
1228 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
1229 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
1230 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
1231 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
1232 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
1236 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1237 store_vector4ui(inst
, machine
, result
);
1240 case OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
1243 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1244 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1245 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1246 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1247 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
1248 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
1249 ubx
= IROUND(255.0F
* a
[0]);
1250 uby
= IROUND(255.0F
* a
[1]);
1251 ubz
= IROUND(255.0F
* a
[2]);
1252 ubw
= IROUND(255.0F
* a
[3]);
1256 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1257 store_vector4ui(inst
, machine
, result
);
1262 GLfloat a
[4], b
[4], result
[4];
1263 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1264 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
1265 result
[0] = result
[1] = result
[2] = result
[3]
1266 = (GLfloat
) _mesa_pow(a
[0], b
[0]);
1267 store_vector4(inst
, machine
, result
);
1272 GLfloat a
[4], result
[4];
1273 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1277 else if (IS_INF_OR_NAN(a
[0]))
1278 printf("RCP(inf)\n");
1280 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
1281 store_vector4(inst
, machine
, result
);
1284 case OPCODE_RET
: /* return from subroutine (conditional) */
1285 if (eval_condition(machine
, inst
)) {
1286 if (machine
->StackDepth
== 0) {
1287 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
1289 /* subtract one because of pc++ in the for loop */
1290 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
1293 case OPCODE_RFL
: /* reflection vector */
1295 GLfloat axis
[4], dir
[4], result
[4], tmpX
, tmpW
;
1296 fetch_vector4(&inst
->SrcReg
[0], machine
, axis
);
1297 fetch_vector4(&inst
->SrcReg
[1], machine
, dir
);
1298 tmpW
= DOT3(axis
, axis
);
1299 tmpX
= (2.0F
* DOT3(axis
, dir
)) / tmpW
;
1300 result
[0] = tmpX
* axis
[0] - dir
[0];
1301 result
[1] = tmpX
* axis
[1] - dir
[1];
1302 result
[2] = tmpX
* axis
[2] - dir
[2];
1303 /* result[3] is never written! XXX enforce in parser! */
1304 store_vector4(inst
, machine
, result
);
1307 case OPCODE_RSQ
: /* 1 / sqrt() */
1309 GLfloat a
[4], result
[4];
1310 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1312 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1313 store_vector4(inst
, machine
, result
);
1315 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
1319 case OPCODE_SCS
: /* sine and cos */
1321 GLfloat a
[4], result
[4];
1322 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1323 result
[0] = (GLfloat
) _mesa_cos(a
[0]);
1324 result
[1] = (GLfloat
) _mesa_sin(a
[0]);
1325 result
[2] = 0.0; /* undefined! */
1326 result
[3] = 0.0; /* undefined! */
1327 store_vector4(inst
, machine
, result
);
1330 case OPCODE_SEQ
: /* set on equal */
1332 GLfloat a
[4], b
[4], result
[4];
1333 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1334 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1335 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1336 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1337 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1338 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1339 store_vector4(inst
, machine
, result
);
1341 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1342 result
[0], result
[1], result
[2], result
[3],
1343 a
[0], a
[1], a
[2], a
[3],
1344 b
[0], b
[1], b
[2], b
[3]);
1348 case OPCODE_SFL
: /* set false, operands ignored */
1350 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1351 store_vector4(inst
, machine
, result
);
1354 case OPCODE_SGE
: /* set on greater or equal */
1356 GLfloat a
[4], b
[4], result
[4];
1357 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1358 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1359 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1360 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1361 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1362 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1363 store_vector4(inst
, machine
, result
);
1365 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1366 result
[0], result
[1], result
[2], result
[3],
1367 a
[0], a
[1], a
[2], a
[3],
1368 b
[0], b
[1], b
[2], b
[3]);
1372 case OPCODE_SGT
: /* set on greater */
1374 GLfloat a
[4], b
[4], result
[4];
1375 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1376 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1377 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1378 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1379 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1380 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1381 store_vector4(inst
, machine
, result
);
1383 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1384 result
[0], result
[1], result
[2], result
[3],
1385 a
[0], a
[1], a
[2], a
[3],
1386 b
[0], b
[1], b
[2], b
[3]);
1392 GLfloat a
[4], result
[4];
1393 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1394 result
[0] = result
[1] = result
[2] = result
[3]
1395 = (GLfloat
) _mesa_sin(a
[0]);
1396 store_vector4(inst
, machine
, result
);
1399 case OPCODE_SLE
: /* set on less or equal */
1401 GLfloat a
[4], b
[4], result
[4];
1402 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1403 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1404 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1405 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1406 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1407 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1408 store_vector4(inst
, machine
, result
);
1410 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1411 result
[0], result
[1], result
[2], result
[3],
1412 a
[0], a
[1], a
[2], a
[3],
1413 b
[0], b
[1], b
[2], b
[3]);
1417 case OPCODE_SLT
: /* set on less */
1419 GLfloat a
[4], b
[4], result
[4];
1420 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1421 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1422 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1423 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1424 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1425 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1426 store_vector4(inst
, machine
, result
);
1428 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1429 result
[0], result
[1], result
[2], result
[3],
1430 a
[0], a
[1], a
[2], a
[3],
1431 b
[0], b
[1], b
[2], b
[3]);
1435 case OPCODE_SNE
: /* set on not equal */
1437 GLfloat a
[4], b
[4], result
[4];
1438 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1439 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1440 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1441 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1442 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1443 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1444 store_vector4(inst
, machine
, result
);
1446 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1447 result
[0], result
[1], result
[2], result
[3],
1448 a
[0], a
[1], a
[2], a
[3],
1449 b
[0], b
[1], b
[2], b
[3]);
1453 case OPCODE_SSG
: /* set sign (-1, 0 or +1) */
1455 GLfloat a
[4], result
[4];
1456 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1457 result
[0] = (GLfloat
) ((a
[0] > 0.0F
) - (a
[0] < 0.0F
));
1458 result
[1] = (GLfloat
) ((a
[1] > 0.0F
) - (a
[1] < 0.0F
));
1459 result
[2] = (GLfloat
) ((a
[2] > 0.0F
) - (a
[2] < 0.0F
));
1460 result
[3] = (GLfloat
) ((a
[3] > 0.0F
) - (a
[3] < 0.0F
));
1461 store_vector4(inst
, machine
, result
);
1464 case OPCODE_STR
: /* set true, operands ignored */
1466 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1467 store_vector4(inst
, machine
, result
);
1472 GLfloat a
[4], b
[4], result
[4];
1473 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1474 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1475 result
[0] = a
[0] - b
[0];
1476 result
[1] = a
[1] - b
[1];
1477 result
[2] = a
[2] - b
[2];
1478 result
[3] = a
[3] - b
[3];
1479 store_vector4(inst
, machine
, result
);
1481 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1482 result
[0], result
[1], result
[2], result
[3],
1483 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1487 case OPCODE_SWZ
: /* extended swizzle */
1489 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1490 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
1493 for (i
= 0; i
< 4; i
++) {
1494 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1495 if (swz
== SWIZZLE_ZERO
)
1497 else if (swz
== SWIZZLE_ONE
)
1502 result
[i
] = src
[swz
];
1504 if (source
->Negate
& (1 << i
))
1505 result
[i
] = -result
[i
];
1507 store_vector4(inst
, machine
, result
);
1510 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1511 /* Simple texel lookup */
1513 GLfloat texcoord
[4], color
[4];
1514 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1516 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1519 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1520 color
[0], color
[1], color
[2], color
[3],
1522 texcoord
[0], texcoord
[1], texcoord
[2], texcoord
[3]);
1524 store_vector4(inst
, machine
, color
);
1527 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1528 /* Texel lookup with LOD bias */
1530 const GLuint unit
= machine
->Samplers
[inst
->TexSrcUnit
];
1531 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
1532 GLfloat texcoord
[4], color
[4], lodBias
;
1534 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1536 /* texcoord[3] is the bias to add to lambda */
1537 lodBias
= texUnit
->LodBias
+ texcoord
[3];
1538 if (texUnit
->_Current
) {
1539 lodBias
+= texUnit
->_Current
->LodBias
;
1542 fetch_texel(ctx
, machine
, inst
, texcoord
, lodBias
, color
);
1544 store_vector4(inst
, machine
, color
);
1547 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1548 /* Texture lookup w/ partial derivatives for LOD */
1550 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1551 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1552 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1553 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1554 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1556 inst
->TexSrcUnit
, color
);
1557 store_vector4(inst
, machine
, color
);
1560 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1561 /* Texture lookup w/ projective divide */
1563 GLfloat texcoord
[4], color
[4];
1565 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1566 /* Not so sure about this test - if texcoord[3] is
1567 * zero, we'd probably be fine except for an ASSERT in
1568 * IROUND_POS() which gets triggered by the inf values created.
1570 if (texcoord
[3] != 0.0) {
1571 texcoord
[0] /= texcoord
[3];
1572 texcoord
[1] /= texcoord
[3];
1573 texcoord
[2] /= texcoord
[3];
1576 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1578 store_vector4(inst
, machine
, color
);
1581 case OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1582 /* Texture lookup w/ projective divide, as above, but do not
1583 * do the divide by w if sampling from a cube map.
1586 GLfloat texcoord
[4], color
[4];
1588 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1589 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1590 texcoord
[3] != 0.0) {
1591 texcoord
[0] /= texcoord
[3];
1592 texcoord
[1] /= texcoord
[3];
1593 texcoord
[2] /= texcoord
[3];
1596 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1598 store_vector4(inst
, machine
, color
);
1601 case OPCODE_TRUNC
: /* truncate toward zero */
1603 GLfloat a
[4], result
[4];
1604 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1605 result
[0] = (GLfloat
) (GLint
) a
[0];
1606 result
[1] = (GLfloat
) (GLint
) a
[1];
1607 result
[2] = (GLfloat
) (GLint
) a
[2];
1608 result
[3] = (GLfloat
) (GLint
) a
[3];
1609 store_vector4(inst
, machine
, result
);
1612 case OPCODE_UP2H
: /* unpack two 16-bit floats */
1614 GLfloat a
[4], result
[4];
1615 const GLuint
*rawBits
= (const GLuint
*) a
;
1617 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1618 hx
= rawBits
[0] & 0xffff;
1619 hy
= rawBits
[0] >> 16;
1620 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1621 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1622 store_vector4(inst
, machine
, result
);
1625 case OPCODE_UP2US
: /* unpack two GLushorts */
1627 GLfloat a
[4], result
[4];
1628 const GLuint
*rawBits
= (const GLuint
*) a
;
1630 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1631 usx
= rawBits
[0] & 0xffff;
1632 usy
= rawBits
[0] >> 16;
1633 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1634 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1635 store_vector4(inst
, machine
, result
);
1638 case OPCODE_UP4B
: /* unpack four GLbytes */
1640 GLfloat a
[4], result
[4];
1641 const GLuint
*rawBits
= (const GLuint
*) a
;
1642 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1643 result
[0] = (((rawBits
[0] >> 0) & 0xff) - 128) / 127.0F
;
1644 result
[1] = (((rawBits
[0] >> 8) & 0xff) - 128) / 127.0F
;
1645 result
[2] = (((rawBits
[0] >> 16) & 0xff) - 128) / 127.0F
;
1646 result
[3] = (((rawBits
[0] >> 24) & 0xff) - 128) / 127.0F
;
1647 store_vector4(inst
, machine
, result
);
1650 case OPCODE_UP4UB
: /* unpack four GLubytes */
1652 GLfloat a
[4], result
[4];
1653 const GLuint
*rawBits
= (const GLuint
*) a
;
1654 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1655 result
[0] = ((rawBits
[0] >> 0) & 0xff) / 255.0F
;
1656 result
[1] = ((rawBits
[0] >> 8) & 0xff) / 255.0F
;
1657 result
[2] = ((rawBits
[0] >> 16) & 0xff) / 255.0F
;
1658 result
[3] = ((rawBits
[0] >> 24) & 0xff) / 255.0F
;
1659 store_vector4(inst
, machine
, result
);
1662 case OPCODE_XOR
: /* bitwise XOR */
1664 GLuint a
[4], b
[4], result
[4];
1665 fetch_vector4ui(&inst
->SrcReg
[0], machine
, a
);
1666 fetch_vector4ui(&inst
->SrcReg
[1], machine
, b
);
1667 result
[0] = a
[0] ^ b
[0];
1668 result
[1] = a
[1] ^ b
[1];
1669 result
[2] = a
[2] ^ b
[2];
1670 result
[3] = a
[3] ^ b
[3];
1671 store_vector4ui(inst
, machine
, result
);
1674 case OPCODE_XPD
: /* cross product */
1676 GLfloat a
[4], b
[4], result
[4];
1677 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1678 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1679 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1680 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1681 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1683 store_vector4(inst
, machine
, result
);
1685 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1686 result
[0], result
[1], result
[2], result
[3],
1687 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1691 case OPCODE_X2D
: /* 2-D matrix transform */
1693 GLfloat a
[4], b
[4], c
[4], result
[4];
1694 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1695 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1696 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1697 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1698 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1699 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1700 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1701 store_vector4(inst
, machine
, result
);
1706 if (inst
->SrcReg
[0].File
!= -1) {
1708 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1709 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst
->Data
,
1710 a
[0], a
[1], a
[2], a
[3]);
1713 _mesa_printf("%s\n", (const char *) inst
->Data
);
1720 _mesa_problem(ctx
, "Bad opcode %d in _mesa_execute_program",
1722 return GL_TRUE
; /* return value doesn't matter */
1726 if (numExec
> maxExec
) {
1727 _mesa_problem(ctx
, "Infinite loop detected in fragment program");