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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \file prog_execute.c
27 * Software interpreter for vertex/fragment programs.
32 * NOTE: we do everything in single-precision floating point; we don't
33 * currently observe the single/half/fixed-precision qualifiers.
38 #include "main/glheader.h"
39 #include "main/colormac.h"
40 #include "main/macros.h"
41 #include "prog_execute.h"
42 #include "prog_instruction.h"
43 #include "prog_parameter.h"
44 #include "prog_print.h"
45 #include "prog_noise.h"
53 * Set x to positive or negative infinity.
55 #if defined(USE_IEEE) || defined(_WIN32)
56 #define SET_POS_INFINITY(x) \
62 #define SET_NEG_INFINITY(x) \
69 #define SET_POS_INFINITY(x) x = (GLfloat) HUGE_VAL
70 #define SET_NEG_INFINITY(x) x = (GLfloat) -HUGE_VAL
73 #define SET_FLOAT_BITS(x, bits) ((fi_type *) (void *) &(x))->i = bits
76 static const GLfloat ZeroVec
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
80 * Return a pointer to the 4-element float vector specified by the given
83 static inline const GLfloat
*
84 get_src_register_pointer(const struct prog_src_register
*source
,
85 const struct gl_program_machine
*machine
)
87 const struct gl_program
*prog
= machine
->CurProgram
;
88 GLint reg
= source
->Index
;
90 if (source
->RelAddr
) {
91 /* add address register value to src index/offset */
92 reg
+= machine
->AddressReg
[0][0];
98 switch (source
->File
) {
99 case PROGRAM_TEMPORARY
:
100 if (reg
>= MAX_PROGRAM_TEMPS
)
102 return machine
->Temporaries
[reg
];
105 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
106 if (reg
>= VERT_ATTRIB_MAX
)
108 return machine
->VertAttribs
[reg
];
111 if (reg
>= VARYING_SLOT_MAX
)
113 return machine
->Attribs
[reg
][machine
->CurElement
];
117 if (reg
>= MAX_PROGRAM_OUTPUTS
)
119 return machine
->Outputs
[reg
];
121 case PROGRAM_LOCAL_PARAM
:
122 if (reg
>= MAX_PROGRAM_LOCAL_PARAMS
)
124 return machine
->CurProgram
->LocalParams
[reg
];
126 case PROGRAM_ENV_PARAM
:
127 if (reg
>= MAX_PROGRAM_ENV_PARAMS
)
129 return machine
->EnvParams
[reg
];
131 case PROGRAM_STATE_VAR
:
133 case PROGRAM_CONSTANT
:
135 case PROGRAM_UNIFORM
:
136 if (reg
>= (GLint
) prog
->Parameters
->NumParameters
)
138 return (GLfloat
*) prog
->Parameters
->ParameterValues
[reg
];
140 case PROGRAM_SYSTEM_VALUE
:
141 assert(reg
< Elements(machine
->SystemValues
));
142 return machine
->SystemValues
[reg
];
146 "Invalid src register file %d in get_src_register_pointer()",
154 * Return a pointer to the 4-element float vector specified by the given
155 * destination register.
157 static inline GLfloat
*
158 get_dst_register_pointer(const struct prog_dst_register
*dest
,
159 struct gl_program_machine
*machine
)
161 static GLfloat dummyReg
[4];
162 GLint reg
= dest
->Index
;
165 /* add address register value to src index/offset */
166 reg
+= machine
->AddressReg
[0][0];
172 switch (dest
->File
) {
173 case PROGRAM_TEMPORARY
:
174 if (reg
>= MAX_PROGRAM_TEMPS
)
176 return machine
->Temporaries
[reg
];
179 if (reg
>= MAX_PROGRAM_OUTPUTS
)
181 return machine
->Outputs
[reg
];
185 "Invalid dest register file %d in get_dst_register_pointer()",
194 * Fetch a 4-element float vector from the given source register.
195 * Apply swizzling and negating as needed.
198 fetch_vector4(const struct prog_src_register
*source
,
199 const struct gl_program_machine
*machine
, GLfloat result
[4])
201 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
204 if (source
->Swizzle
== SWIZZLE_NOOP
) {
206 COPY_4V(result
, src
);
209 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
210 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
211 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
212 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
213 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
214 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
215 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
216 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
220 result
[0] = FABSF(result
[0]);
221 result
[1] = FABSF(result
[1]);
222 result
[2] = FABSF(result
[2]);
223 result
[3] = FABSF(result
[3]);
225 if (source
->Negate
) {
226 ASSERT(source
->Negate
== NEGATE_XYZW
);
227 result
[0] = -result
[0];
228 result
[1] = -result
[1];
229 result
[2] = -result
[2];
230 result
[3] = -result
[3];
234 assert(!IS_INF_OR_NAN(result
[0]));
235 assert(!IS_INF_OR_NAN(result
[0]));
236 assert(!IS_INF_OR_NAN(result
[0]));
237 assert(!IS_INF_OR_NAN(result
[0]));
243 * Fetch the derivative with respect to X or Y for the given register.
244 * XXX this currently only works for fragment program input attribs.
247 fetch_vector4_deriv(struct gl_context
* ctx
,
248 const struct prog_src_register
*source
,
249 const struct gl_program_machine
*machine
,
250 char xOrY
, GLfloat result
[4])
252 if (source
->File
== PROGRAM_INPUT
&&
253 source
->Index
< (GLint
) machine
->NumDeriv
) {
254 const GLint col
= machine
->CurElement
;
255 const GLfloat w
= machine
->Attribs
[VARYING_SLOT_POS
][col
][3];
256 const GLfloat invQ
= 1.0f
/ w
;
260 deriv
[0] = machine
->DerivX
[source
->Index
][0] * invQ
;
261 deriv
[1] = machine
->DerivX
[source
->Index
][1] * invQ
;
262 deriv
[2] = machine
->DerivX
[source
->Index
][2] * invQ
;
263 deriv
[3] = machine
->DerivX
[source
->Index
][3] * invQ
;
266 deriv
[0] = machine
->DerivY
[source
->Index
][0] * invQ
;
267 deriv
[1] = machine
->DerivY
[source
->Index
][1] * invQ
;
268 deriv
[2] = machine
->DerivY
[source
->Index
][2] * invQ
;
269 deriv
[3] = machine
->DerivY
[source
->Index
][3] * invQ
;
272 result
[0] = deriv
[GET_SWZ(source
->Swizzle
, 0)];
273 result
[1] = deriv
[GET_SWZ(source
->Swizzle
, 1)];
274 result
[2] = deriv
[GET_SWZ(source
->Swizzle
, 2)];
275 result
[3] = deriv
[GET_SWZ(source
->Swizzle
, 3)];
278 result
[0] = FABSF(result
[0]);
279 result
[1] = FABSF(result
[1]);
280 result
[2] = FABSF(result
[2]);
281 result
[3] = FABSF(result
[3]);
283 if (source
->Negate
) {
284 ASSERT(source
->Negate
== NEGATE_XYZW
);
285 result
[0] = -result
[0];
286 result
[1] = -result
[1];
287 result
[2] = -result
[2];
288 result
[3] = -result
[3];
292 ASSIGN_4V(result
, 0.0, 0.0, 0.0, 0.0);
298 * As above, but only return result[0] element.
301 fetch_vector1(const struct prog_src_register
*source
,
302 const struct gl_program_machine
*machine
, GLfloat result
[4])
304 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
307 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
310 result
[0] = FABSF(result
[0]);
312 if (source
->Negate
) {
313 result
[0] = -result
[0];
319 fetch_vector1ui(const struct prog_src_register
*source
,
320 const struct gl_program_machine
*machine
)
322 const GLuint
*src
= (GLuint
*) get_src_register_pointer(source
, machine
);
323 return src
[GET_SWZ(source
->Swizzle
, 0)];
328 * Fetch texel from texture. Use partial derivatives when possible.
331 fetch_texel(struct gl_context
*ctx
,
332 const struct gl_program_machine
*machine
,
333 const struct prog_instruction
*inst
,
334 const GLfloat texcoord
[4], GLfloat lodBias
,
337 const GLuint unit
= machine
->Samplers
[inst
->TexSrcUnit
];
339 /* Note: we only have the right derivatives for fragment input attribs.
341 if (machine
->NumDeriv
> 0 &&
342 inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
343 inst
->SrcReg
[0].Index
== VARYING_SLOT_TEX0
+ inst
->TexSrcUnit
) {
344 /* simple texture fetch for which we should have derivatives */
345 GLuint attr
= inst
->SrcReg
[0].Index
;
346 machine
->FetchTexelDeriv(ctx
, texcoord
,
347 machine
->DerivX
[attr
],
348 machine
->DerivY
[attr
],
349 lodBias
, unit
, color
);
352 machine
->FetchTexelLod(ctx
, texcoord
, lodBias
, unit
, color
);
358 * Test value against zero and return GT, LT, EQ or UN if NaN.
361 generate_cc(float value
)
364 return COND_UN
; /* NaN */
374 * Test if the ccMaskRule is satisfied by the given condition code.
375 * Used to mask destination writes according to the current condition code.
377 static inline GLboolean
378 test_cc(GLuint condCode
, GLuint ccMaskRule
)
380 switch (ccMaskRule
) {
381 case COND_EQ
: return (condCode
== COND_EQ
);
382 case COND_NE
: return (condCode
!= COND_EQ
);
383 case COND_LT
: return (condCode
== COND_LT
);
384 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
385 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
386 case COND_GT
: return (condCode
== COND_GT
);
387 case COND_TR
: return GL_TRUE
;
388 case COND_FL
: return GL_FALSE
;
389 default: return GL_TRUE
;
395 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
396 * or GL_FALSE to indicate result.
398 static inline GLboolean
399 eval_condition(const struct gl_program_machine
*machine
,
400 const struct prog_instruction
*inst
)
402 const GLuint swizzle
= inst
->DstReg
.CondSwizzle
;
403 const GLuint condMask
= inst
->DstReg
.CondMask
;
404 if (test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 0)], condMask
) ||
405 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 1)], condMask
) ||
406 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 2)], condMask
) ||
407 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 3)], condMask
)) {
418 * Store 4 floats into a register. Observe the instructions saturate and
419 * set-condition-code flags.
422 store_vector4(const struct prog_instruction
*inst
,
423 struct gl_program_machine
*machine
, const GLfloat value
[4])
425 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
426 const GLboolean clamp
= inst
->SaturateMode
== SATURATE_ZERO_ONE
;
427 GLuint writeMask
= dstReg
->WriteMask
;
428 GLfloat clampedValue
[4];
429 GLfloat
*dst
= get_dst_register_pointer(dstReg
, machine
);
432 if (value
[0] > 1.0e10
||
433 IS_INF_OR_NAN(value
[0]) ||
434 IS_INF_OR_NAN(value
[1]) ||
435 IS_INF_OR_NAN(value
[2]) || IS_INF_OR_NAN(value
[3]))
436 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
440 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
441 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
442 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
443 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
444 value
= clampedValue
;
447 if (dstReg
->CondMask
!= COND_TR
) {
448 /* condition codes may turn off some writes */
449 if (writeMask
& WRITEMASK_X
) {
450 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
452 writeMask
&= ~WRITEMASK_X
;
454 if (writeMask
& WRITEMASK_Y
) {
455 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
457 writeMask
&= ~WRITEMASK_Y
;
459 if (writeMask
& WRITEMASK_Z
) {
460 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
462 writeMask
&= ~WRITEMASK_Z
;
464 if (writeMask
& WRITEMASK_W
) {
465 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
467 writeMask
&= ~WRITEMASK_W
;
472 assert(!IS_INF_OR_NAN(value
[0]));
473 assert(!IS_INF_OR_NAN(value
[0]));
474 assert(!IS_INF_OR_NAN(value
[0]));
475 assert(!IS_INF_OR_NAN(value
[0]));
478 if (writeMask
& WRITEMASK_X
)
480 if (writeMask
& WRITEMASK_Y
)
482 if (writeMask
& WRITEMASK_Z
)
484 if (writeMask
& WRITEMASK_W
)
487 if (inst
->CondUpdate
) {
488 if (writeMask
& WRITEMASK_X
)
489 machine
->CondCodes
[0] = generate_cc(value
[0]);
490 if (writeMask
& WRITEMASK_Y
)
491 machine
->CondCodes
[1] = generate_cc(value
[1]);
492 if (writeMask
& WRITEMASK_Z
)
493 machine
->CondCodes
[2] = generate_cc(value
[2]);
494 if (writeMask
& WRITEMASK_W
)
495 machine
->CondCodes
[3] = generate_cc(value
[3]);
497 printf("CondCodes=(%s,%s,%s,%s) for:\n",
498 _mesa_condcode_string(machine
->CondCodes
[0]),
499 _mesa_condcode_string(machine
->CondCodes
[1]),
500 _mesa_condcode_string(machine
->CondCodes
[2]),
501 _mesa_condcode_string(machine
->CondCodes
[3]));
508 * Store 4 uints into a register. Observe the set-condition-code flags.
511 store_vector4ui(const struct prog_instruction
*inst
,
512 struct gl_program_machine
*machine
, const GLuint value
[4])
514 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
515 GLuint writeMask
= dstReg
->WriteMask
;
516 GLuint
*dst
= (GLuint
*) get_dst_register_pointer(dstReg
, machine
);
518 if (dstReg
->CondMask
!= COND_TR
) {
519 /* condition codes may turn off some writes */
520 if (writeMask
& WRITEMASK_X
) {
521 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
523 writeMask
&= ~WRITEMASK_X
;
525 if (writeMask
& WRITEMASK_Y
) {
526 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
528 writeMask
&= ~WRITEMASK_Y
;
530 if (writeMask
& WRITEMASK_Z
) {
531 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
533 writeMask
&= ~WRITEMASK_Z
;
535 if (writeMask
& WRITEMASK_W
) {
536 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
538 writeMask
&= ~WRITEMASK_W
;
542 if (writeMask
& WRITEMASK_X
)
544 if (writeMask
& WRITEMASK_Y
)
546 if (writeMask
& WRITEMASK_Z
)
548 if (writeMask
& WRITEMASK_W
)
551 if (inst
->CondUpdate
) {
552 if (writeMask
& WRITEMASK_X
)
553 machine
->CondCodes
[0] = generate_cc((float)value
[0]);
554 if (writeMask
& WRITEMASK_Y
)
555 machine
->CondCodes
[1] = generate_cc((float)value
[1]);
556 if (writeMask
& WRITEMASK_Z
)
557 machine
->CondCodes
[2] = generate_cc((float)value
[2]);
558 if (writeMask
& WRITEMASK_W
)
559 machine
->CondCodes
[3] = generate_cc((float)value
[3]);
561 printf("CondCodes=(%s,%s,%s,%s) for:\n",
562 _mesa_condcode_string(machine
->CondCodes
[0]),
563 _mesa_condcode_string(machine
->CondCodes
[1]),
564 _mesa_condcode_string(machine
->CondCodes
[2]),
565 _mesa_condcode_string(machine
->CondCodes
[3]));
573 * Execute the given vertex/fragment program.
575 * \param ctx rendering context
576 * \param program the program to execute
577 * \param machine machine state (must be initialized)
578 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
581 _mesa_execute_program(struct gl_context
* ctx
,
582 const struct gl_program
*program
,
583 struct gl_program_machine
*machine
)
585 const GLuint numInst
= program
->NumInstructions
;
586 const GLuint maxExec
= 65536;
587 GLuint pc
, numExec
= 0;
589 machine
->CurProgram
= program
;
592 printf("execute program %u --------------------\n", program
->Id
);
595 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
596 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
599 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
602 for (pc
= 0; pc
< numInst
; pc
++) {
603 const struct prog_instruction
*inst
= program
->Instructions
+ pc
;
606 _mesa_print_instruction(inst
);
609 switch (inst
->Opcode
) {
612 GLfloat a
[4], result
[4];
613 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
614 result
[0] = FABSF(a
[0]);
615 result
[1] = FABSF(a
[1]);
616 result
[2] = FABSF(a
[2]);
617 result
[3] = FABSF(a
[3]);
618 store_vector4(inst
, machine
, result
);
623 GLfloat a
[4], b
[4], result
[4];
624 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
625 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
626 result
[0] = a
[0] + b
[0];
627 result
[1] = a
[1] + b
[1];
628 result
[2] = a
[2] + b
[2];
629 result
[3] = a
[3] + b
[3];
630 store_vector4(inst
, machine
, result
);
632 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
633 result
[0], result
[1], result
[2], result
[3],
634 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
641 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
642 machine
->AddressReg
[0][0] = IFLOOR(t
[0]);
644 printf("ARL %d\n", machine
->AddressReg
[0][0]);
650 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
654 /* subtract 1 here since pc is incremented by for(pc) loop */
655 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
657 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
659 case OPCODE_BGNSUB
: /* begin subroutine */
661 case OPCODE_ENDSUB
: /* end subroutine */
663 case OPCODE_BRK
: /* break out of loop (conditional) */
664 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
666 if (eval_condition(machine
, inst
)) {
667 /* break out of loop */
668 /* pc++ at end of for-loop will put us after the ENDLOOP inst */
669 pc
= inst
->BranchTarget
;
672 case OPCODE_CONT
: /* continue loop (conditional) */
673 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
675 if (eval_condition(machine
, inst
)) {
676 /* continue at ENDLOOP */
677 /* Subtract 1 here since we'll do pc++ at end of for-loop */
678 pc
= inst
->BranchTarget
- 1;
681 case OPCODE_CAL
: /* Call subroutine (conditional) */
682 if (eval_condition(machine
, inst
)) {
683 /* call the subroutine */
684 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
685 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
687 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1; /* next inst */
688 /* Subtract 1 here since we'll do pc++ at end of for-loop */
689 pc
= inst
->BranchTarget
- 1;
694 GLfloat a
[4], b
[4], c
[4], result
[4];
695 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
696 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
697 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
698 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
699 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
700 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
701 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
702 store_vector4(inst
, machine
, result
);
704 printf("CMP (%g %g %g %g) = (%g %g %g %g) < 0 ? (%g %g %g %g) : (%g %g %g %g)\n",
705 result
[0], result
[1], result
[2], result
[3],
706 a
[0], a
[1], a
[2], a
[3],
707 b
[0], b
[1], b
[2], b
[3],
708 c
[0], c
[1], c
[2], c
[3]);
714 GLfloat a
[4], result
[4];
715 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
716 result
[0] = result
[1] = result
[2] = result
[3]
717 = (GLfloat
) cos(a
[0]);
718 store_vector4(inst
, machine
, result
);
721 case OPCODE_DDX
: /* Partial derivative with respect to X */
724 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
726 store_vector4(inst
, machine
, result
);
729 case OPCODE_DDY
: /* Partial derivative with respect to Y */
732 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
734 store_vector4(inst
, machine
, result
);
739 GLfloat a
[4], b
[4], result
[4];
740 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
741 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
742 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
);
743 store_vector4(inst
, machine
, result
);
745 printf("DP2 %g = (%g %g) . (%g %g)\n",
746 result
[0], a
[0], a
[1], b
[0], b
[1]);
752 GLfloat a
[4], b
[4], result
[4];
753 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
754 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
755 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
756 store_vector4(inst
, machine
, result
);
758 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
759 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
765 GLfloat a
[4], b
[4], result
[4];
766 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
767 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
768 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
, b
);
769 store_vector4(inst
, machine
, result
);
771 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
772 result
[0], a
[0], a
[1], a
[2], a
[3],
773 b
[0], b
[1], b
[2], b
[3]);
779 GLfloat a
[4], b
[4], result
[4];
780 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
781 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
782 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
) + b
[3];
783 store_vector4(inst
, machine
, result
);
786 case OPCODE_DST
: /* Distance vector */
788 GLfloat a
[4], b
[4], result
[4];
789 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
790 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
792 result
[1] = a
[1] * b
[1];
795 store_vector4(inst
, machine
, result
);
800 GLfloat t
[4], q
[4], floor_t0
;
801 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
802 floor_t0
= FLOORF(t
[0]);
803 if (floor_t0
> FLT_MAX_EXP
) {
804 SET_POS_INFINITY(q
[0]);
805 SET_POS_INFINITY(q
[2]);
807 else if (floor_t0
< FLT_MIN_EXP
) {
812 q
[0] = LDEXPF(1.0, (int) floor_t0
);
813 /* Note: GL_NV_vertex_program expects
814 * result.z = result.x * APPX(result.y)
815 * We do what the ARB extension says.
817 q
[2] = (GLfloat
) pow(2.0, t
[0]);
819 q
[1] = t
[0] - floor_t0
;
821 store_vector4( inst
, machine
, q
);
824 case OPCODE_EX2
: /* Exponential base 2 */
826 GLfloat a
[4], result
[4], val
;
827 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
828 val
= (GLfloat
) pow(2.0, a
[0]);
830 if (IS_INF_OR_NAN(val))
833 result
[0] = result
[1] = result
[2] = result
[3] = val
;
834 store_vector4(inst
, machine
, result
);
839 GLfloat a
[4], result
[4];
840 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
841 result
[0] = FLOORF(a
[0]);
842 result
[1] = FLOORF(a
[1]);
843 result
[2] = FLOORF(a
[2]);
844 result
[3] = FLOORF(a
[3]);
845 store_vector4(inst
, machine
, result
);
850 GLfloat a
[4], result
[4];
851 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
852 result
[0] = a
[0] - FLOORF(a
[0]);
853 result
[1] = a
[1] - FLOORF(a
[1]);
854 result
[2] = a
[2] - FLOORF(a
[2]);
855 result
[3] = a
[3] - FLOORF(a
[3]);
856 store_vector4(inst
, machine
, result
);
862 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
864 program
->Instructions
[inst
->BranchTarget
].Opcode
867 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
869 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
870 cond
= (a
[0] != 0.0);
873 cond
= eval_condition(machine
, inst
);
876 printf("IF: %d\n", cond
);
880 /* do if-clause (just continue execution) */
883 /* go to the instruction after ELSE or ENDIF */
884 assert(inst
->BranchTarget
>= 0);
885 pc
= inst
->BranchTarget
;
891 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
893 assert(inst
->BranchTarget
>= 0);
894 pc
= inst
->BranchTarget
;
899 case OPCODE_KIL_NV
: /* NV_f_p only (conditional) */
900 if (eval_condition(machine
, inst
)) {
904 case OPCODE_KIL
: /* ARB_f_p only */
907 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
909 printf("KIL if (%g %g %g %g) <= 0.0\n",
910 a
[0], a
[1], a
[2], a
[3]);
913 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
918 case OPCODE_LG2
: /* log base 2 */
920 GLfloat a
[4], result
[4], val
;
921 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
922 /* The fast LOG2 macro doesn't meet the precision requirements.
928 val
= (float)(log(a
[0]) * 1.442695F
);
930 result
[0] = result
[1] = result
[2] = result
[3] = val
;
931 store_vector4(inst
, machine
, result
);
936 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
937 GLfloat a
[4], result
[4];
938 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
939 a
[0] = MAX2(a
[0], 0.0F
);
940 a
[1] = MAX2(a
[1], 0.0F
);
941 /* XXX ARB version clamps a[3], NV version doesn't */
942 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
945 /* XXX we could probably just use pow() here */
947 if (a
[1] == 0.0 && a
[3] == 0.0)
950 result
[2] = (GLfloat
) pow(a
[1], a
[3]);
956 store_vector4(inst
, machine
, result
);
958 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
959 result
[0], result
[1], result
[2], result
[3],
960 a
[0], a
[1], a
[2], a
[3]);
966 GLfloat t
[4], q
[4], abs_t0
;
967 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
968 abs_t0
= FABSF(t
[0]);
969 if (abs_t0
!= 0.0F
) {
970 if (IS_INF_OR_NAN(abs_t0
))
972 SET_POS_INFINITY(q
[0]);
974 SET_POS_INFINITY(q
[2]);
978 GLfloat mantissa
= FREXPF(t
[0], &exponent
);
979 q
[0] = (GLfloat
) (exponent
- 1);
980 q
[1] = (GLfloat
) (2.0 * mantissa
); /* map [.5, 1) -> [1, 2) */
982 /* The fast LOG2 macro doesn't meet the precision
985 q
[2] = (float)(log(t
[0]) * 1.442695F
);
989 SET_NEG_INFINITY(q
[0]);
991 SET_NEG_INFINITY(q
[2]);
994 store_vector4(inst
, machine
, q
);
999 GLfloat a
[4], b
[4], c
[4], result
[4];
1000 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1001 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1002 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1003 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
1004 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
1005 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
1006 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
1007 store_vector4(inst
, machine
, result
);
1009 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
1010 "(%g %g %g %g), (%g %g %g %g)\n",
1011 result
[0], result
[1], result
[2], result
[3],
1012 a
[0], a
[1], a
[2], a
[3],
1013 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1019 GLfloat a
[4], b
[4], c
[4], result
[4];
1020 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1021 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1022 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1023 result
[0] = a
[0] * b
[0] + c
[0];
1024 result
[1] = a
[1] * b
[1] + c
[1];
1025 result
[2] = a
[2] * b
[2] + c
[2];
1026 result
[3] = a
[3] * b
[3] + c
[3];
1027 store_vector4(inst
, machine
, result
);
1029 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
1030 "(%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],
1033 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1039 GLfloat a
[4], b
[4], result
[4];
1040 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1041 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1042 result
[0] = MAX2(a
[0], b
[0]);
1043 result
[1] = MAX2(a
[1], b
[1]);
1044 result
[2] = MAX2(a
[2], b
[2]);
1045 result
[3] = MAX2(a
[3], b
[3]);
1046 store_vector4(inst
, machine
, result
);
1048 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
1049 result
[0], result
[1], result
[2], result
[3],
1050 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1056 GLfloat a
[4], b
[4], result
[4];
1057 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1058 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1059 result
[0] = MIN2(a
[0], b
[0]);
1060 result
[1] = MIN2(a
[1], b
[1]);
1061 result
[2] = MIN2(a
[2], b
[2]);
1062 result
[3] = MIN2(a
[3], b
[3]);
1063 store_vector4(inst
, machine
, result
);
1069 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
1070 store_vector4(inst
, machine
, result
);
1072 printf("MOV (%g %g %g %g)\n",
1073 result
[0], result
[1], result
[2], result
[3]);
1079 GLfloat a
[4], b
[4], result
[4];
1080 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1081 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1082 result
[0] = a
[0] * b
[0];
1083 result
[1] = a
[1] * b
[1];
1084 result
[2] = a
[2] * b
[2];
1085 result
[3] = a
[3] * b
[3];
1086 store_vector4(inst
, machine
, result
);
1088 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1089 result
[0], result
[1], result
[2], result
[3],
1090 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1096 GLfloat a
[4], result
[4];
1097 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1101 result
[3] = _mesa_noise1(a
[0]);
1102 store_vector4(inst
, machine
, result
);
1107 GLfloat a
[4], result
[4];
1108 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1111 result
[2] = result
[3] = _mesa_noise2(a
[0], a
[1]);
1112 store_vector4(inst
, machine
, result
);
1117 GLfloat a
[4], result
[4];
1118 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1122 result
[3] = _mesa_noise3(a
[0], a
[1], a
[2]);
1123 store_vector4(inst
, machine
, result
);
1128 GLfloat a
[4], result
[4];
1129 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1133 result
[3] = _mesa_noise4(a
[0], a
[1], a
[2], a
[3]);
1134 store_vector4(inst
, machine
, result
);
1139 case OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
1144 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1145 hx
= _mesa_float_to_half(a
[0]);
1146 hy
= _mesa_float_to_half(a
[1]);
1150 result
[3] = hx
| (hy
<< 16);
1151 store_vector4ui(inst
, machine
, result
);
1154 case OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
1157 GLuint result
[4], usx
, usy
;
1158 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1159 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1160 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1161 usx
= F_TO_I(a
[0] * 65535.0F
);
1162 usy
= F_TO_I(a
[1] * 65535.0F
);
1166 result
[3] = usx
| (usy
<< 16);
1167 store_vector4ui(inst
, machine
, result
);
1170 case OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
1173 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1174 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1175 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
1176 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
1177 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
1178 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
1179 ubx
= F_TO_I(127.0F
* a
[0] + 128.0F
);
1180 uby
= F_TO_I(127.0F
* a
[1] + 128.0F
);
1181 ubz
= F_TO_I(127.0F
* a
[2] + 128.0F
);
1182 ubw
= F_TO_I(127.0F
* a
[3] + 128.0F
);
1186 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1187 store_vector4ui(inst
, machine
, result
);
1190 case OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
1193 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1194 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1195 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1196 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1197 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
1198 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
1199 ubx
= F_TO_I(255.0F
* a
[0]);
1200 uby
= F_TO_I(255.0F
* a
[1]);
1201 ubz
= F_TO_I(255.0F
* a
[2]);
1202 ubw
= F_TO_I(255.0F
* a
[3]);
1206 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1207 store_vector4ui(inst
, machine
, result
);
1212 GLfloat a
[4], b
[4], result
[4];
1213 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1214 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
1215 result
[0] = result
[1] = result
[2] = result
[3]
1216 = (GLfloat
) pow(a
[0], b
[0]);
1217 store_vector4(inst
, machine
, result
);
1223 GLfloat a
[4], result
[4];
1224 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1228 else if (IS_INF_OR_NAN(a
[0]))
1229 printf("RCP(inf)\n");
1231 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
1232 store_vector4(inst
, machine
, result
);
1235 case OPCODE_RET
: /* return from subroutine (conditional) */
1236 if (eval_condition(machine
, inst
)) {
1237 if (machine
->StackDepth
== 0) {
1238 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
1240 /* subtract one because of pc++ in the for loop */
1241 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
1244 case OPCODE_RFL
: /* reflection vector */
1246 GLfloat axis
[4], dir
[4], result
[4], tmpX
, tmpW
;
1247 fetch_vector4(&inst
->SrcReg
[0], machine
, axis
);
1248 fetch_vector4(&inst
->SrcReg
[1], machine
, dir
);
1249 tmpW
= DOT3(axis
, axis
);
1250 tmpX
= (2.0F
* DOT3(axis
, dir
)) / tmpW
;
1251 result
[0] = tmpX
* axis
[0] - dir
[0];
1252 result
[1] = tmpX
* axis
[1] - dir
[1];
1253 result
[2] = tmpX
* axis
[2] - dir
[2];
1254 /* result[3] is never written! XXX enforce in parser! */
1255 store_vector4(inst
, machine
, result
);
1258 case OPCODE_RSQ
: /* 1 / sqrt() */
1260 GLfloat a
[4], result
[4];
1261 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1263 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1264 store_vector4(inst
, machine
, result
);
1266 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
1270 case OPCODE_SCS
: /* sine and cos */
1272 GLfloat a
[4], result
[4];
1273 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1274 result
[0] = (GLfloat
) cos(a
[0]);
1275 result
[1] = (GLfloat
) sin(a
[0]);
1276 result
[2] = 0.0; /* undefined! */
1277 result
[3] = 0.0; /* undefined! */
1278 store_vector4(inst
, machine
, result
);
1281 case OPCODE_SEQ
: /* set on equal */
1283 GLfloat a
[4], b
[4], result
[4];
1284 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1285 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1286 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1287 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1288 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1289 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1290 store_vector4(inst
, machine
, result
);
1292 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1293 result
[0], result
[1], result
[2], result
[3],
1294 a
[0], a
[1], a
[2], a
[3],
1295 b
[0], b
[1], b
[2], b
[3]);
1299 case OPCODE_SFL
: /* set false, operands ignored */
1301 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1302 store_vector4(inst
, machine
, result
);
1305 case OPCODE_SGE
: /* set on greater or equal */
1307 GLfloat a
[4], b
[4], result
[4];
1308 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1309 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1310 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1311 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1312 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1313 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1314 store_vector4(inst
, machine
, result
);
1316 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1317 result
[0], result
[1], result
[2], result
[3],
1318 a
[0], a
[1], a
[2], a
[3],
1319 b
[0], b
[1], b
[2], b
[3]);
1323 case OPCODE_SGT
: /* set on greater */
1325 GLfloat a
[4], b
[4], result
[4];
1326 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1327 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1328 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1329 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1330 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1331 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1332 store_vector4(inst
, machine
, result
);
1334 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1335 result
[0], result
[1], result
[2], result
[3],
1336 a
[0], a
[1], a
[2], a
[3],
1337 b
[0], b
[1], b
[2], b
[3]);
1343 GLfloat a
[4], result
[4];
1344 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1345 result
[0] = result
[1] = result
[2] = result
[3]
1346 = (GLfloat
) sin(a
[0]);
1347 store_vector4(inst
, machine
, result
);
1350 case OPCODE_SLE
: /* set on less or equal */
1352 GLfloat a
[4], b
[4], result
[4];
1353 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1354 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1355 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1356 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1357 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1358 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1359 store_vector4(inst
, machine
, result
);
1361 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1362 result
[0], result
[1], result
[2], result
[3],
1363 a
[0], a
[1], a
[2], a
[3],
1364 b
[0], b
[1], b
[2], b
[3]);
1368 case OPCODE_SLT
: /* set on less */
1370 GLfloat a
[4], b
[4], result
[4];
1371 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1372 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1373 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1374 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1375 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1376 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1377 store_vector4(inst
, machine
, result
);
1379 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1380 result
[0], result
[1], result
[2], result
[3],
1381 a
[0], a
[1], a
[2], a
[3],
1382 b
[0], b
[1], b
[2], b
[3]);
1386 case OPCODE_SNE
: /* set on not equal */
1388 GLfloat a
[4], b
[4], result
[4];
1389 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1390 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1391 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1392 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1393 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1394 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1395 store_vector4(inst
, machine
, result
);
1397 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1398 result
[0], result
[1], result
[2], result
[3],
1399 a
[0], a
[1], a
[2], a
[3],
1400 b
[0], b
[1], b
[2], b
[3]);
1404 case OPCODE_SSG
: /* set sign (-1, 0 or +1) */
1406 GLfloat a
[4], result
[4];
1407 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1408 result
[0] = (GLfloat
) ((a
[0] > 0.0F
) - (a
[0] < 0.0F
));
1409 result
[1] = (GLfloat
) ((a
[1] > 0.0F
) - (a
[1] < 0.0F
));
1410 result
[2] = (GLfloat
) ((a
[2] > 0.0F
) - (a
[2] < 0.0F
));
1411 result
[3] = (GLfloat
) ((a
[3] > 0.0F
) - (a
[3] < 0.0F
));
1412 store_vector4(inst
, machine
, result
);
1415 case OPCODE_STR
: /* set true, operands ignored */
1417 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1418 store_vector4(inst
, machine
, result
);
1423 GLfloat a
[4], b
[4], result
[4];
1424 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1425 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1426 result
[0] = a
[0] - b
[0];
1427 result
[1] = a
[1] - b
[1];
1428 result
[2] = a
[2] - b
[2];
1429 result
[3] = a
[3] - b
[3];
1430 store_vector4(inst
, machine
, result
);
1432 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1433 result
[0], result
[1], result
[2], result
[3],
1434 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1438 case OPCODE_SWZ
: /* extended swizzle */
1440 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1441 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
1444 for (i
= 0; i
< 4; i
++) {
1445 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1446 if (swz
== SWIZZLE_ZERO
)
1448 else if (swz
== SWIZZLE_ONE
)
1453 result
[i
] = src
[swz
];
1455 if (source
->Negate
& (1 << i
))
1456 result
[i
] = -result
[i
];
1458 store_vector4(inst
, machine
, result
);
1461 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1462 /* Simple texel lookup */
1464 GLfloat texcoord
[4], color
[4];
1465 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1467 /* For TEX, texcoord.Q should not be used and its value should not
1468 * matter (at most, we pass coord.xyz to texture3D() in GLSL).
1469 * Set Q=1 so that FetchTexelDeriv() doesn't get a garbage value
1470 * which is effectively what happens when the texcoord swizzle
1475 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1478 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1479 color
[0], color
[1], color
[2], color
[3],
1481 texcoord
[0], texcoord
[1], texcoord
[2], texcoord
[3]);
1483 store_vector4(inst
, machine
, color
);
1486 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1487 /* Texel lookup with LOD bias */
1489 GLfloat texcoord
[4], color
[4], lodBias
;
1491 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1493 /* texcoord[3] is the bias to add to lambda */
1494 lodBias
= texcoord
[3];
1496 fetch_texel(ctx
, machine
, inst
, texcoord
, lodBias
, color
);
1499 printf("TXB (%g, %g, %g, %g) = texture[%d][%g %g %g %g]"
1501 color
[0], color
[1], color
[2], color
[3],
1510 store_vector4(inst
, machine
, color
);
1513 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1514 /* Texture lookup w/ partial derivatives for LOD */
1516 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1517 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1518 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1519 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1520 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1522 inst
->TexSrcUnit
, color
);
1523 store_vector4(inst
, machine
, color
);
1527 /* Texel lookup with explicit LOD */
1529 GLfloat texcoord
[4], color
[4], lod
;
1531 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1533 /* texcoord[3] is the LOD */
1536 machine
->FetchTexelLod(ctx
, texcoord
, lod
,
1537 machine
->Samplers
[inst
->TexSrcUnit
], color
);
1539 store_vector4(inst
, machine
, color
);
1542 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1543 /* Texture lookup w/ projective divide */
1545 GLfloat texcoord
[4], color
[4];
1547 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1548 /* Not so sure about this test - if texcoord[3] is
1549 * zero, we'd probably be fine except for an ASSERT in
1550 * IROUND_POS() which gets triggered by the inf values created.
1552 if (texcoord
[3] != 0.0) {
1553 texcoord
[0] /= texcoord
[3];
1554 texcoord
[1] /= texcoord
[3];
1555 texcoord
[2] /= texcoord
[3];
1558 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1560 store_vector4(inst
, machine
, color
);
1563 case OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1564 /* Texture lookup w/ projective divide, as above, but do not
1565 * do the divide by w if sampling from a cube map.
1568 GLfloat texcoord
[4], color
[4];
1570 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1571 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1572 texcoord
[3] != 0.0) {
1573 texcoord
[0] /= texcoord
[3];
1574 texcoord
[1] /= texcoord
[3];
1575 texcoord
[2] /= texcoord
[3];
1578 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1580 store_vector4(inst
, machine
, color
);
1583 case OPCODE_TRUNC
: /* truncate toward zero */
1585 GLfloat a
[4], result
[4];
1586 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1587 result
[0] = (GLfloat
) (GLint
) a
[0];
1588 result
[1] = (GLfloat
) (GLint
) a
[1];
1589 result
[2] = (GLfloat
) (GLint
) a
[2];
1590 result
[3] = (GLfloat
) (GLint
) a
[3];
1591 store_vector4(inst
, machine
, result
);
1594 case OPCODE_UP2H
: /* unpack two 16-bit floats */
1596 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1601 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1602 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1603 store_vector4(inst
, machine
, result
);
1606 case OPCODE_UP2US
: /* unpack two GLushorts */
1608 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1613 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1614 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1615 store_vector4(inst
, machine
, result
);
1618 case OPCODE_UP4B
: /* unpack four GLbytes */
1620 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1622 result
[0] = (((raw
>> 0) & 0xff) - 128) / 127.0F
;
1623 result
[1] = (((raw
>> 8) & 0xff) - 128) / 127.0F
;
1624 result
[2] = (((raw
>> 16) & 0xff) - 128) / 127.0F
;
1625 result
[3] = (((raw
>> 24) & 0xff) - 128) / 127.0F
;
1626 store_vector4(inst
, machine
, result
);
1629 case OPCODE_UP4UB
: /* unpack four GLubytes */
1631 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1633 result
[0] = ((raw
>> 0) & 0xff) / 255.0F
;
1634 result
[1] = ((raw
>> 8) & 0xff) / 255.0F
;
1635 result
[2] = ((raw
>> 16) & 0xff) / 255.0F
;
1636 result
[3] = ((raw
>> 24) & 0xff) / 255.0F
;
1637 store_vector4(inst
, machine
, result
);
1640 case OPCODE_XPD
: /* cross product */
1642 GLfloat a
[4], b
[4], result
[4];
1643 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1644 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1645 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1646 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1647 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1649 store_vector4(inst
, machine
, result
);
1651 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1652 result
[0], result
[1], result
[2], result
[3],
1653 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1657 case OPCODE_X2D
: /* 2-D matrix transform */
1659 GLfloat a
[4], b
[4], c
[4], result
[4];
1660 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1661 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1662 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1663 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1664 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1665 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1666 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1667 store_vector4(inst
, machine
, result
);
1673 _mesa_problem(ctx
, "Bad opcode %d in _mesa_execute_program",
1675 return GL_TRUE
; /* return value doesn't matter */
1679 if (numExec
> maxExec
) {
1680 static GLboolean reported
= GL_FALSE
;
1682 _mesa_problem(ctx
, "Infinite loop detected in fragment program");