2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 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.
42 #include "prog_execute.h"
43 #include "prog_instruction.h"
44 #include "prog_parameter.h"
45 #include "prog_print.h"
46 #include "shader/slang/slang_library_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_register_pointer(const struct prog_src_register
*source
,
80 const struct gl_program_machine
*machine
)
82 if (source
->RelAddr
) {
83 const GLint reg
= source
->Index
+ machine
->AddressReg
[0][0];
84 if (source
->File
== PROGRAM_ENV_PARAM
)
85 if (reg
< 0 || reg
>= MAX_PROGRAM_ENV_PARAMS
)
88 return machine
->EnvParams
[reg
];
90 const struct gl_program_parameter_list
*params
;
91 ASSERT(source
->File
== PROGRAM_LOCAL_PARAM
||
92 source
->File
== PROGRAM_STATE_VAR
);
93 params
= machine
->CurProgram
->Parameters
;
94 if (reg
< 0 || reg
>= params
->NumParameters
)
97 return params
->ParameterValues
[reg
];
101 switch (source
->File
) {
102 case PROGRAM_TEMPORARY
:
103 ASSERT(source
->Index
< MAX_PROGRAM_TEMPS
);
104 return machine
->Temporaries
[source
->Index
];
107 if (machine
->CurProgram
->Target
== GL_VERTEX_PROGRAM_ARB
) {
108 ASSERT(source
->Index
< VERT_ATTRIB_MAX
);
109 return machine
->VertAttribs
[source
->Index
];
112 ASSERT(source
->Index
< FRAG_ATTRIB_MAX
);
113 return machine
->Attribs
[source
->Index
][machine
->CurElement
];
117 ASSERT(source
->Index
< MAX_PROGRAM_OUTPUTS
);
118 return machine
->Outputs
[source
->Index
];
120 case PROGRAM_LOCAL_PARAM
:
121 ASSERT(source
->Index
< MAX_PROGRAM_LOCAL_PARAMS
);
122 return machine
->CurProgram
->LocalParams
[source
->Index
];
124 case PROGRAM_ENV_PARAM
:
125 ASSERT(source
->Index
< MAX_PROGRAM_ENV_PARAMS
);
126 return machine
->EnvParams
[source
->Index
];
128 case PROGRAM_STATE_VAR
:
130 case PROGRAM_CONSTANT
:
132 case PROGRAM_UNIFORM
:
134 case PROGRAM_NAMED_PARAM
:
135 ASSERT(source
->Index
<
136 (GLint
) machine
->CurProgram
->Parameters
->NumParameters
);
137 return machine
->CurProgram
->Parameters
->ParameterValues
[source
->Index
];
141 "Invalid input register file %d in get_register_pointer()",
148 #if FEATURE_MESA_program_debug
149 static struct gl_program_machine
*CurrentMachine
= NULL
;
152 * For GL_MESA_program_debug.
153 * Return current value (4*GLfloat) of a program register.
154 * Called via ctx->Driver.GetProgramRegister().
157 _mesa_get_program_register(GLcontext
*ctx
, enum register_file file
,
158 GLuint index
, GLfloat val
[4])
160 if (CurrentMachine
) {
161 struct prog_src_register src
;
165 reg
= get_register_pointer(&src
, CurrentMachine
);
169 #endif /* FEATURE_MESA_program_debug */
173 * Fetch a 4-element float vector from the given source register.
174 * Apply swizzling and negating as needed.
177 fetch_vector4(const struct prog_src_register
*source
,
178 const struct gl_program_machine
*machine
, GLfloat result
[4])
180 const GLfloat
*src
= get_register_pointer(source
, machine
);
183 if (source
->Swizzle
== SWIZZLE_NOOP
) {
185 COPY_4V(result
, src
);
188 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
189 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
190 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
191 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
192 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
193 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
194 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
195 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
198 if (source
->NegateBase
) {
199 result
[0] = -result
[0];
200 result
[1] = -result
[1];
201 result
[2] = -result
[2];
202 result
[3] = -result
[3];
205 result
[0] = FABSF(result
[0]);
206 result
[1] = FABSF(result
[1]);
207 result
[2] = FABSF(result
[2]);
208 result
[3] = FABSF(result
[3]);
210 if (source
->NegateAbs
) {
211 result
[0] = -result
[0];
212 result
[1] = -result
[1];
213 result
[2] = -result
[2];
214 result
[3] = -result
[3];
220 * Fetch the derivative with respect to X or Y for the given register.
221 * XXX this currently only works for fragment program input attribs.
224 fetch_vector4_deriv(GLcontext
* ctx
,
225 const struct prog_src_register
*source
,
226 const struct gl_program_machine
*machine
,
227 char xOrY
, GLfloat result
[4])
229 if (source
->File
== PROGRAM_INPUT
&& source
->Index
< machine
->NumDeriv
) {
230 const GLint col
= machine
->CurElement
;
231 const GLfloat w
= machine
->Attribs
[FRAG_ATTRIB_WPOS
][col
][3];
232 const GLfloat invQ
= 1.0f
/ w
;
236 deriv
[0] = machine
->DerivX
[source
->Index
][0] * invQ
;
237 deriv
[1] = machine
->DerivX
[source
->Index
][1] * invQ
;
238 deriv
[2] = machine
->DerivX
[source
->Index
][2] * invQ
;
239 deriv
[3] = machine
->DerivX
[source
->Index
][3] * invQ
;
242 deriv
[0] = machine
->DerivY
[source
->Index
][0] * invQ
;
243 deriv
[1] = machine
->DerivY
[source
->Index
][1] * invQ
;
244 deriv
[2] = machine
->DerivY
[source
->Index
][2] * invQ
;
245 deriv
[3] = machine
->DerivY
[source
->Index
][3] * invQ
;
248 result
[0] = deriv
[GET_SWZ(source
->Swizzle
, 0)];
249 result
[1] = deriv
[GET_SWZ(source
->Swizzle
, 1)];
250 result
[2] = deriv
[GET_SWZ(source
->Swizzle
, 2)];
251 result
[3] = deriv
[GET_SWZ(source
->Swizzle
, 3)];
253 if (source
->NegateBase
) {
254 result
[0] = -result
[0];
255 result
[1] = -result
[1];
256 result
[2] = -result
[2];
257 result
[3] = -result
[3];
260 result
[0] = FABSF(result
[0]);
261 result
[1] = FABSF(result
[1]);
262 result
[2] = FABSF(result
[2]);
263 result
[3] = FABSF(result
[3]);
265 if (source
->NegateAbs
) {
266 result
[0] = -result
[0];
267 result
[1] = -result
[1];
268 result
[2] = -result
[2];
269 result
[3] = -result
[3];
273 ASSIGN_4V(result
, 0.0, 0.0, 0.0, 0.0);
279 * As above, but only return result[0] element.
282 fetch_vector1(const struct prog_src_register
*source
,
283 const struct gl_program_machine
*machine
, GLfloat result
[4])
285 const GLfloat
*src
= get_register_pointer(source
, machine
);
288 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
290 if (source
->NegateBase
) {
291 result
[0] = -result
[0];
294 result
[0] = FABSF(result
[0]);
296 if (source
->NegateAbs
) {
297 result
[0] = -result
[0];
303 * Fetch texel from texture. Use partial derivatives when possible.
306 fetch_texel(GLcontext
*ctx
,
307 const struct gl_program_machine
*machine
,
308 const struct prog_instruction
*inst
,
309 const GLfloat texcoord
[4], GLfloat lodBias
,
312 /* Note: we only have the right derivatives for fragment input attribs.
314 if (machine
->NumDeriv
> 0 &&
315 inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
316 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
) {
317 /* simple texture fetch for which we should have derivatives */
318 GLuint attr
= inst
->SrcReg
[0].Index
;
319 machine
->FetchTexelDeriv(ctx
, texcoord
,
320 machine
->DerivX
[attr
],
321 machine
->DerivY
[attr
],
323 inst
->TexSrcUnit
, color
);
326 machine
->FetchTexelLod(ctx
, texcoord
, lodBias
,
327 inst
->TexSrcUnit
, color
);
333 * Test value against zero and return GT, LT, EQ or UN if NaN.
336 generate_cc(float value
)
339 return COND_UN
; /* NaN */
349 * Test if the ccMaskRule is satisfied by the given condition code.
350 * Used to mask destination writes according to the current condition code.
352 static INLINE GLboolean
353 test_cc(GLuint condCode
, GLuint ccMaskRule
)
355 switch (ccMaskRule
) {
356 case COND_EQ
: return (condCode
== COND_EQ
);
357 case COND_NE
: return (condCode
!= COND_EQ
);
358 case COND_LT
: return (condCode
== COND_LT
);
359 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
360 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
361 case COND_GT
: return (condCode
== COND_GT
);
362 case COND_TR
: return GL_TRUE
;
363 case COND_FL
: return GL_FALSE
;
364 default: return GL_TRUE
;
370 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
371 * or GL_FALSE to indicate result.
373 static INLINE GLboolean
374 eval_condition(const struct gl_program_machine
*machine
,
375 const struct prog_instruction
*inst
)
377 const GLuint swizzle
= inst
->DstReg
.CondSwizzle
;
378 const GLuint condMask
= inst
->DstReg
.CondMask
;
379 if (test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 0)], condMask
) ||
380 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 1)], condMask
) ||
381 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 2)], condMask
) ||
382 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 3)], condMask
)) {
393 * Store 4 floats into a register. Observe the instructions saturate and
394 * set-condition-code flags.
397 store_vector4(const struct prog_instruction
*inst
,
398 struct gl_program_machine
*machine
, const GLfloat value
[4])
400 const struct prog_dst_register
*dest
= &(inst
->DstReg
);
401 const GLboolean clamp
= inst
->SaturateMode
== SATURATE_ZERO_ONE
;
404 GLfloat clampedValue
[4];
405 GLuint writeMask
= dest
->WriteMask
;
407 switch (dest
->File
) {
409 ASSERT(dest
->Index
< MAX_PROGRAM_OUTPUTS
);
410 dstReg
= machine
->Outputs
[dest
->Index
];
412 case PROGRAM_TEMPORARY
:
413 ASSERT(dest
->Index
< MAX_PROGRAM_TEMPS
);
414 dstReg
= machine
->Temporaries
[dest
->Index
];
416 case PROGRAM_WRITE_ONLY
:
420 _mesa_problem(NULL
, "bad register file in store_vector4(fp)");
425 if (value
[0] > 1.0e10
||
426 IS_INF_OR_NAN(value
[0]) ||
427 IS_INF_OR_NAN(value
[1]) ||
428 IS_INF_OR_NAN(value
[2]) || IS_INF_OR_NAN(value
[3]))
429 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
433 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
434 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
435 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
436 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
437 value
= clampedValue
;
440 if (dest
->CondMask
!= COND_TR
) {
441 /* condition codes may turn off some writes */
442 if (writeMask
& WRITEMASK_X
) {
443 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 0)],
445 writeMask
&= ~WRITEMASK_X
;
447 if (writeMask
& WRITEMASK_Y
) {
448 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 1)],
450 writeMask
&= ~WRITEMASK_Y
;
452 if (writeMask
& WRITEMASK_Z
) {
453 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 2)],
455 writeMask
&= ~WRITEMASK_Z
;
457 if (writeMask
& WRITEMASK_W
) {
458 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 3)],
460 writeMask
&= ~WRITEMASK_W
;
464 if (writeMask
& WRITEMASK_X
)
465 dstReg
[0] = value
[0];
466 if (writeMask
& WRITEMASK_Y
)
467 dstReg
[1] = value
[1];
468 if (writeMask
& WRITEMASK_Z
)
469 dstReg
[2] = value
[2];
470 if (writeMask
& WRITEMASK_W
)
471 dstReg
[3] = value
[3];
473 if (inst
->CondUpdate
) {
474 if (writeMask
& WRITEMASK_X
)
475 machine
->CondCodes
[0] = generate_cc(value
[0]);
476 if (writeMask
& WRITEMASK_Y
)
477 machine
->CondCodes
[1] = generate_cc(value
[1]);
478 if (writeMask
& WRITEMASK_Z
)
479 machine
->CondCodes
[2] = generate_cc(value
[2]);
480 if (writeMask
& WRITEMASK_W
)
481 machine
->CondCodes
[3] = generate_cc(value
[3]);
483 printf("CondCodes=(%s,%s,%s,%s) for:\n",
484 _mesa_condcode_string(machine
->CondCodes
[0]),
485 _mesa_condcode_string(machine
->CondCodes
[1]),
486 _mesa_condcode_string(machine
->CondCodes
[2]),
487 _mesa_condcode_string(machine
->CondCodes
[3]));
494 * Execute the given vertex/fragment program.
496 * \param ctx rendering context
497 * \param program the program to execute
498 * \param machine machine state (must be initialized)
499 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
502 _mesa_execute_program(GLcontext
* ctx
,
503 const struct gl_program
*program
,
504 struct gl_program_machine
*machine
)
506 const GLuint numInst
= program
->NumInstructions
;
507 const GLuint maxExec
= 10000;
508 GLint pc
, numExec
= 0;
510 machine
->CurProgram
= program
;
513 printf("execute program %u --------------------\n", program
->Id
);
516 #if FEATURE_MESA_program_debug
517 CurrentMachine
= machine
;
520 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
521 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
524 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
527 for (pc
= 0; pc
< numInst
; pc
++) {
528 const struct prog_instruction
*inst
= program
->Instructions
+ pc
;
530 #if FEATURE_MESA_program_debug
531 if (ctx
->FragmentProgram
.CallbackEnabled
&&
532 ctx
->FragmentProgram
.Callback
) {
533 ctx
->FragmentProgram
.CurrentPosition
= inst
->StringPos
;
534 ctx
->FragmentProgram
.Callback(program
->Target
,
535 ctx
->FragmentProgram
.CallbackData
);
540 _mesa_print_instruction(inst
);
543 switch (inst
->Opcode
) {
546 GLfloat a
[4], result
[4];
547 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
548 result
[0] = FABSF(a
[0]);
549 result
[1] = FABSF(a
[1]);
550 result
[2] = FABSF(a
[2]);
551 result
[3] = FABSF(a
[3]);
552 store_vector4(inst
, machine
, result
);
557 GLfloat a
[4], b
[4], result
[4];
558 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
559 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
560 result
[0] = a
[0] + b
[0];
561 result
[1] = a
[1] + b
[1];
562 result
[2] = a
[2] + b
[2];
563 result
[3] = a
[3] + b
[3];
564 store_vector4(inst
, machine
, result
);
566 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
567 result
[0], result
[1], result
[2], result
[3],
568 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
575 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
576 machine
->AddressReg
[0][0] = (GLint
) FLOORF(t
[0]);
583 /* subtract 1 here since pc is incremented by for(pc) loop */
584 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
586 case OPCODE_BGNSUB
: /* begin subroutine */
588 case OPCODE_ENDSUB
: /* end subroutine */
590 case OPCODE_BRA
: /* branch (conditional) */
592 case OPCODE_BRK
: /* break out of loop (conditional) */
594 case OPCODE_CONT
: /* continue loop (conditional) */
595 if (eval_condition(machine
, inst
)) {
597 /* Subtract 1 here since we'll do pc++ at end of for-loop */
598 pc
= inst
->BranchTarget
- 1;
601 case OPCODE_CAL
: /* Call subroutine (conditional) */
602 if (eval_condition(machine
, inst
)) {
603 /* call the subroutine */
604 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
605 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
607 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1; /* next inst */
608 /* Subtract 1 here since we'll do pc++ at end of for-loop */
609 pc
= inst
->BranchTarget
- 1;
614 GLfloat a
[4], b
[4], c
[4], result
[4];
615 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
616 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
617 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
618 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
619 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
620 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
621 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
622 store_vector4(inst
, machine
, result
);
627 GLfloat a
[4], result
[4];
628 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
629 result
[0] = result
[1] = result
[2] = result
[3]
630 = (GLfloat
) _mesa_cos(a
[0]);
631 store_vector4(inst
, machine
, result
);
634 case OPCODE_DDX
: /* Partial derivative with respect to X */
637 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
639 store_vector4(inst
, machine
, result
);
642 case OPCODE_DDY
: /* Partial derivative with respect to Y */
645 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
647 store_vector4(inst
, machine
, result
);
652 GLfloat a
[4], b
[4], result
[4];
653 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
654 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
655 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
656 store_vector4(inst
, machine
, result
);
658 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
659 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
665 GLfloat a
[4], b
[4], result
[4];
666 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
667 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
668 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
, b
);
669 store_vector4(inst
, machine
, result
);
671 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
672 result
[0], a
[0], a
[1], a
[2], a
[3],
673 b
[0], b
[1], b
[2], b
[3]);
679 GLfloat a
[4], b
[4], result
[4];
680 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
681 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
682 result
[0] = result
[1] = result
[2] = result
[3] =
683 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + b
[3];
684 store_vector4(inst
, machine
, result
);
687 case OPCODE_DST
: /* Distance vector */
689 GLfloat a
[4], b
[4], result
[4];
690 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
691 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
693 result
[1] = a
[1] * b
[1];
696 store_vector4(inst
, machine
, result
);
701 GLfloat t
[4], q
[4], floor_t0
;
702 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
703 floor_t0
= FLOORF(t
[0]);
704 if (floor_t0
> FLT_MAX_EXP
) {
705 SET_POS_INFINITY(q
[0]);
706 SET_POS_INFINITY(q
[2]);
708 else if (floor_t0
< FLT_MIN_EXP
) {
713 q
[0] = LDEXPF(1.0, (int) floor_t0
);
714 /* Note: GL_NV_vertex_program expects
715 * result.z = result.x * APPX(result.y)
716 * We do what the ARB extension says.
718 q
[2] = pow(2.0, t
[0]);
720 q
[1] = t
[0] - floor_t0
;
722 store_vector4( inst
, machine
, q
);
725 case OPCODE_EX2
: /* Exponential base 2 */
727 GLfloat a
[4], result
[4];
728 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
729 result
[0] = result
[1] = result
[2] = result
[3] =
730 (GLfloat
) _mesa_pow(2.0, a
[0]);
731 store_vector4(inst
, machine
, result
);
736 GLfloat a
[4], result
[4];
737 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
738 result
[0] = FLOORF(a
[0]);
739 result
[1] = FLOORF(a
[1]);
740 result
[2] = FLOORF(a
[2]);
741 result
[3] = FLOORF(a
[3]);
742 store_vector4(inst
, machine
, result
);
747 GLfloat a
[4], result
[4];
748 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
749 result
[0] = a
[0] - FLOORF(a
[0]);
750 result
[1] = a
[1] - FLOORF(a
[1]);
751 result
[2] = a
[2] - FLOORF(a
[2]);
752 result
[3] = a
[3] - FLOORF(a
[3]);
753 store_vector4(inst
, machine
, result
);
760 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
762 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
763 cond
= (a
[0] != 0.0);
766 cond
= eval_condition(machine
, inst
);
769 printf("IF: %d\n", cond
);
773 /* do if-clause (just continue execution) */
776 /* go to the instruction after ELSE or ENDIF */
777 assert(inst
->BranchTarget
>= 0);
778 pc
= inst
->BranchTarget
- 1;
784 assert(inst
->BranchTarget
>= 0);
785 pc
= inst
->BranchTarget
- 1;
790 case OPCODE_INT
: /* float to int */
792 GLfloat a
[4], result
[4];
793 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
794 result
[0] = (GLfloat
) (GLint
) a
[0];
795 result
[1] = (GLfloat
) (GLint
) a
[1];
796 result
[2] = (GLfloat
) (GLint
) a
[2];
797 result
[3] = (GLfloat
) (GLint
) a
[3];
798 store_vector4(inst
, machine
, result
);
801 case OPCODE_KIL_NV
: /* NV_f_p only (conditional) */
802 if (eval_condition(machine
, inst
)) {
806 case OPCODE_KIL
: /* ARB_f_p only */
809 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
810 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
815 case OPCODE_LG2
: /* log base 2 */
817 GLfloat a
[4], result
[4];
818 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
819 result
[0] = result
[1] = result
[2] = result
[3] = LOG2(a
[0]);
820 store_vector4(inst
, machine
, result
);
825 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
826 GLfloat a
[4], result
[4];
827 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
828 a
[0] = MAX2(a
[0], 0.0F
);
829 a
[1] = MAX2(a
[1], 0.0F
);
830 /* XXX ARB version clamps a[3], NV version doesn't */
831 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
834 /* XXX we could probably just use pow() here */
836 if (a
[1] == 0.0 && a
[3] == 0.0)
839 result
[2] = EXPF(a
[3] * LOGF(a
[1]));
845 store_vector4(inst
, machine
, result
);
847 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
848 result
[0], result
[1], result
[2], result
[3],
849 a
[0], a
[1], a
[2], a
[3]);
855 GLfloat t
[4], q
[4], abs_t0
;
856 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
857 abs_t0
= FABSF(t
[0]);
858 if (abs_t0
!= 0.0F
) {
859 /* Since we really can't handle infinite values on VMS
860 * like other OSes we'll use __MAXFLOAT to represent
861 * infinity. This may need some tweaking.
864 if (abs_t0
== __MAXFLOAT
)
866 if (IS_INF_OR_NAN(abs_t0
))
869 SET_POS_INFINITY(q
[0]);
871 SET_POS_INFINITY(q
[2]);
875 GLfloat mantissa
= FREXPF(t
[0], &exponent
);
876 q
[0] = (GLfloat
) (exponent
- 1);
877 q
[1] = (GLfloat
) (2.0 * mantissa
); /* map [.5, 1) -> [1, 2) */
878 q
[2] = (GLfloat
) (q
[0] + LOG2(q
[1]));
882 SET_NEG_INFINITY(q
[0]);
884 SET_NEG_INFINITY(q
[2]);
887 store_vector4(inst
, machine
, q
);
892 GLfloat a
[4], b
[4], c
[4], result
[4];
893 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
894 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
895 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
896 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
897 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
898 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
899 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
900 store_vector4(inst
, machine
, result
);
902 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
903 "(%g %g %g %g), (%g %g %g %g)\n",
904 result
[0], result
[1], result
[2], result
[3],
905 a
[0], a
[1], a
[2], a
[3],
906 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
912 GLfloat a
[4], b
[4], c
[4], result
[4];
913 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
914 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
915 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
916 result
[0] = a
[0] * b
[0] + c
[0];
917 result
[1] = a
[1] * b
[1] + c
[1];
918 result
[2] = a
[2] * b
[2] + c
[2];
919 result
[3] = a
[3] * b
[3] + c
[3];
920 store_vector4(inst
, machine
, result
);
922 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
923 "(%g %g %g %g) + (%g %g %g %g)\n",
924 result
[0], result
[1], result
[2], result
[3],
925 a
[0], a
[1], a
[2], a
[3],
926 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
932 GLfloat a
[4], b
[4], result
[4];
933 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
934 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
935 result
[0] = MAX2(a
[0], b
[0]);
936 result
[1] = MAX2(a
[1], b
[1]);
937 result
[2] = MAX2(a
[2], b
[2]);
938 result
[3] = MAX2(a
[3], b
[3]);
939 store_vector4(inst
, machine
, result
);
941 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
942 result
[0], result
[1], result
[2], result
[3],
943 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
949 GLfloat a
[4], b
[4], result
[4];
950 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
951 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
952 result
[0] = MIN2(a
[0], b
[0]);
953 result
[1] = MIN2(a
[1], b
[1]);
954 result
[2] = MIN2(a
[2], b
[2]);
955 result
[3] = MIN2(a
[3], b
[3]);
956 store_vector4(inst
, machine
, result
);
962 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
963 store_vector4(inst
, machine
, result
);
965 printf("MOV (%g %g %g %g)\n",
966 result
[0], result
[1], result
[2], result
[3]);
972 GLfloat a
[4], b
[4], result
[4];
973 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
974 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
975 result
[0] = a
[0] * b
[0];
976 result
[1] = a
[1] * b
[1];
977 result
[2] = a
[2] * b
[2];
978 result
[3] = a
[3] * b
[3];
979 store_vector4(inst
, machine
, result
);
981 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
982 result
[0], result
[1], result
[2], result
[3],
983 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
989 GLfloat a
[4], result
[4];
990 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
993 result
[2] = result
[3] = _slang_library_noise1(a
[0]);
994 store_vector4(inst
, machine
, result
);
999 GLfloat a
[4], result
[4];
1000 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1003 result
[2] = result
[3] = _slang_library_noise2(a
[0], a
[1]);
1004 store_vector4(inst
, machine
, result
);
1009 GLfloat a
[4], result
[4];
1010 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1014 result
[3] = _slang_library_noise3(a
[0], a
[1], a
[2]);
1015 store_vector4(inst
, machine
, result
);
1020 GLfloat a
[4], result
[4];
1021 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1025 result
[3] = _slang_library_noise4(a
[0], a
[1], a
[2], a
[3]);
1026 store_vector4(inst
, machine
, result
);
1031 case OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
1033 GLfloat a
[4], result
[4];
1035 GLuint
*rawResult
= (GLuint
*) result
;
1037 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1038 hx
= _mesa_float_to_half(a
[0]);
1039 hy
= _mesa_float_to_half(a
[1]);
1040 twoHalves
= hx
| (hy
<< 16);
1041 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1043 store_vector4(inst
, machine
, result
);
1046 case OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
1048 GLfloat a
[4], result
[4];
1049 GLuint usx
, usy
, *rawResult
= (GLuint
*) result
;
1050 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1051 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1052 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1053 usx
= IROUND(a
[0] * 65535.0F
);
1054 usy
= IROUND(a
[1] * 65535.0F
);
1055 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1056 = usx
| (usy
<< 16);
1057 store_vector4(inst
, machine
, result
);
1060 case OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
1062 GLfloat a
[4], result
[4];
1063 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
1064 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1065 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
1066 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
1067 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
1068 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
1069 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
1070 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
1071 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
1072 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
1073 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1074 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1075 store_vector4(inst
, machine
, result
);
1078 case OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
1080 GLfloat a
[4], result
[4];
1081 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
1082 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1083 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1084 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1085 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
1086 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
1087 ubx
= IROUND(255.0F
* a
[0]);
1088 uby
= IROUND(255.0F
* a
[1]);
1089 ubz
= IROUND(255.0F
* a
[2]);
1090 ubw
= IROUND(255.0F
* a
[3]);
1091 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1092 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1093 store_vector4(inst
, machine
, result
);
1098 GLfloat a
[4], b
[4], result
[4];
1099 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1100 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
1101 result
[0] = result
[1] = result
[2] = result
[3]
1102 = (GLfloat
) _mesa_pow(a
[0], b
[0]);
1103 store_vector4(inst
, machine
, result
);
1108 GLfloat a
[4], result
[4];
1109 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1113 else if (IS_INF_OR_NAN(a
[0]))
1114 printf("RCP(inf)\n");
1116 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
1117 store_vector4(inst
, machine
, result
);
1120 case OPCODE_RET
: /* return from subroutine (conditional) */
1121 if (eval_condition(machine
, inst
)) {
1122 if (machine
->StackDepth
== 0) {
1123 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
1125 /* subtract one because of pc++ in the for loop */
1126 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
1129 case OPCODE_RFL
: /* reflection vector */
1131 GLfloat axis
[4], dir
[4], result
[4], tmpX
, tmpW
;
1132 fetch_vector4(&inst
->SrcReg
[0], machine
, axis
);
1133 fetch_vector4(&inst
->SrcReg
[1], machine
, dir
);
1134 tmpW
= DOT3(axis
, axis
);
1135 tmpX
= (2.0F
* DOT3(axis
, dir
)) / tmpW
;
1136 result
[0] = tmpX
* axis
[0] - dir
[0];
1137 result
[1] = tmpX
* axis
[1] - dir
[1];
1138 result
[2] = tmpX
* axis
[2] - dir
[2];
1139 /* result[3] is never written! XXX enforce in parser! */
1140 store_vector4(inst
, machine
, result
);
1143 case OPCODE_RSQ
: /* 1 / sqrt() */
1145 GLfloat a
[4], result
[4];
1146 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1148 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1149 store_vector4(inst
, machine
, result
);
1151 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
1155 case OPCODE_SCS
: /* sine and cos */
1157 GLfloat a
[4], result
[4];
1158 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1159 result
[0] = (GLfloat
) _mesa_cos(a
[0]);
1160 result
[1] = (GLfloat
) _mesa_sin(a
[0]);
1161 result
[2] = 0.0; /* undefined! */
1162 result
[3] = 0.0; /* undefined! */
1163 store_vector4(inst
, machine
, result
);
1166 case OPCODE_SEQ
: /* set on equal */
1168 GLfloat a
[4], b
[4], result
[4];
1169 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1170 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1171 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1172 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1173 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1174 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1175 store_vector4(inst
, machine
, result
);
1177 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1178 result
[0], result
[1], result
[2], result
[3],
1179 a
[0], a
[1], a
[2], a
[3],
1180 b
[0], b
[1], b
[2], b
[3]);
1184 case OPCODE_SFL
: /* set false, operands ignored */
1186 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1187 store_vector4(inst
, machine
, result
);
1190 case OPCODE_SGE
: /* set on greater or equal */
1192 GLfloat a
[4], b
[4], result
[4];
1193 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1194 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1195 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1196 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1197 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1198 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1199 store_vector4(inst
, machine
, result
);
1201 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1202 result
[0], result
[1], result
[2], result
[3],
1203 a
[0], a
[1], a
[2], a
[3],
1204 b
[0], b
[1], b
[2], b
[3]);
1208 case OPCODE_SGT
: /* set on greater */
1210 GLfloat a
[4], b
[4], result
[4];
1211 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1212 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1213 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1214 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1215 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1216 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1217 store_vector4(inst
, machine
, result
);
1219 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1220 result
[0], result
[1], result
[2], result
[3],
1221 a
[0], a
[1], a
[2], a
[3],
1222 b
[0], b
[1], b
[2], b
[3]);
1228 GLfloat a
[4], result
[4];
1229 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1230 result
[0] = result
[1] = result
[2] = result
[3]
1231 = (GLfloat
) _mesa_sin(a
[0]);
1232 store_vector4(inst
, machine
, result
);
1235 case OPCODE_SLE
: /* set on less or equal */
1237 GLfloat a
[4], b
[4], result
[4];
1238 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1239 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1240 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1241 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1242 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1243 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1244 store_vector4(inst
, machine
, result
);
1246 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1247 result
[0], result
[1], result
[2], result
[3],
1248 a
[0], a
[1], a
[2], a
[3],
1249 b
[0], b
[1], b
[2], b
[3]);
1253 case OPCODE_SLT
: /* set on less */
1255 GLfloat a
[4], b
[4], result
[4];
1256 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1257 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1258 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1259 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1260 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1261 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1262 store_vector4(inst
, machine
, result
);
1264 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1265 result
[0], result
[1], result
[2], result
[3],
1266 a
[0], a
[1], a
[2], a
[3],
1267 b
[0], b
[1], b
[2], b
[3]);
1271 case OPCODE_SNE
: /* set on not equal */
1273 GLfloat a
[4], b
[4], result
[4];
1274 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1275 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1276 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1277 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1278 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1279 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1280 store_vector4(inst
, machine
, result
);
1282 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1283 result
[0], result
[1], result
[2], result
[3],
1284 a
[0], a
[1], a
[2], a
[3],
1285 b
[0], b
[1], b
[2], b
[3]);
1289 case OPCODE_STR
: /* set true, operands ignored */
1291 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1292 store_vector4(inst
, machine
, result
);
1297 GLfloat a
[4], b
[4], result
[4];
1298 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1299 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1300 result
[0] = a
[0] - b
[0];
1301 result
[1] = a
[1] - b
[1];
1302 result
[2] = a
[2] - b
[2];
1303 result
[3] = a
[3] - b
[3];
1304 store_vector4(inst
, machine
, result
);
1306 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1307 result
[0], result
[1], result
[2], result
[3],
1308 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1312 case OPCODE_SWZ
: /* extended swizzle */
1314 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1315 const GLfloat
*src
= get_register_pointer(source
, machine
);
1318 for (i
= 0; i
< 4; i
++) {
1319 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1320 if (swz
== SWIZZLE_ZERO
)
1322 else if (swz
== SWIZZLE_ONE
)
1327 result
[i
] = src
[swz
];
1329 if (source
->NegateBase
& (1 << i
))
1330 result
[i
] = -result
[i
];
1332 store_vector4(inst
, machine
, result
);
1335 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1336 /* Simple texel lookup */
1338 GLfloat texcoord
[4], color
[4];
1339 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1341 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1344 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1345 color
[0], color
[1], color
[2], color
[3],
1347 texcoord
[0], texcoord
[1], texcoord
[2], texcoord
[3]);
1349 store_vector4(inst
, machine
, color
);
1352 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1353 /* Texel lookup with LOD bias */
1355 const struct gl_texture_unit
*texUnit
1356 = &ctx
->Texture
.Unit
[inst
->TexSrcUnit
];
1357 GLfloat texcoord
[4], color
[4], lodBias
;
1359 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1361 /* texcoord[3] is the bias to add to lambda */
1362 lodBias
= texUnit
->LodBias
+ texcoord
[3];
1363 if (texUnit
->_Current
) {
1364 lodBias
+= texUnit
->_Current
->LodBias
;
1367 fetch_texel(ctx
, machine
, inst
, texcoord
, lodBias
, color
);
1369 store_vector4(inst
, machine
, color
);
1372 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1373 /* Texture lookup w/ partial derivatives for LOD */
1375 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1376 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1377 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1378 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1379 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1381 inst
->TexSrcUnit
, color
);
1382 store_vector4(inst
, machine
, color
);
1385 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1386 /* Texture lookup w/ projective divide */
1388 GLfloat texcoord
[4], color
[4];
1390 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1391 /* Not so sure about this test - if texcoord[3] is
1392 * zero, we'd probably be fine except for an ASSERT in
1393 * IROUND_POS() which gets triggered by the inf values created.
1395 if (texcoord
[3] != 0.0) {
1396 texcoord
[0] /= texcoord
[3];
1397 texcoord
[1] /= texcoord
[3];
1398 texcoord
[2] /= texcoord
[3];
1401 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1403 store_vector4(inst
, machine
, color
);
1406 case OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1407 /* Texture lookup w/ projective divide, as above, but do not
1408 * do the divide by w if sampling from a cube map.
1411 GLfloat texcoord
[4], color
[4];
1413 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1414 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1415 texcoord
[3] != 0.0) {
1416 texcoord
[0] /= texcoord
[3];
1417 texcoord
[1] /= texcoord
[3];
1418 texcoord
[2] /= texcoord
[3];
1421 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1423 store_vector4(inst
, machine
, color
);
1426 case OPCODE_UP2H
: /* unpack two 16-bit floats */
1428 GLfloat a
[4], result
[4];
1429 const GLuint
*rawBits
= (const GLuint
*) a
;
1431 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1432 hx
= rawBits
[0] & 0xffff;
1433 hy
= rawBits
[0] >> 16;
1434 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1435 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1436 store_vector4(inst
, machine
, result
);
1439 case OPCODE_UP2US
: /* unpack two GLushorts */
1441 GLfloat a
[4], result
[4];
1442 const GLuint
*rawBits
= (const GLuint
*) a
;
1444 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1445 usx
= rawBits
[0] & 0xffff;
1446 usy
= rawBits
[0] >> 16;
1447 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1448 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1449 store_vector4(inst
, machine
, result
);
1452 case OPCODE_UP4B
: /* unpack four GLbytes */
1454 GLfloat a
[4], result
[4];
1455 const GLuint
*rawBits
= (const GLuint
*) a
;
1456 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1457 result
[0] = (((rawBits
[0] >> 0) & 0xff) - 128) / 127.0F
;
1458 result
[1] = (((rawBits
[0] >> 8) & 0xff) - 128) / 127.0F
;
1459 result
[2] = (((rawBits
[0] >> 16) & 0xff) - 128) / 127.0F
;
1460 result
[3] = (((rawBits
[0] >> 24) & 0xff) - 128) / 127.0F
;
1461 store_vector4(inst
, machine
, result
);
1464 case OPCODE_UP4UB
: /* unpack four GLubytes */
1466 GLfloat a
[4], result
[4];
1467 const GLuint
*rawBits
= (const GLuint
*) a
;
1468 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1469 result
[0] = ((rawBits
[0] >> 0) & 0xff) / 255.0F
;
1470 result
[1] = ((rawBits
[0] >> 8) & 0xff) / 255.0F
;
1471 result
[2] = ((rawBits
[0] >> 16) & 0xff) / 255.0F
;
1472 result
[3] = ((rawBits
[0] >> 24) & 0xff) / 255.0F
;
1473 store_vector4(inst
, machine
, result
);
1476 case OPCODE_XPD
: /* cross product */
1478 GLfloat a
[4], b
[4], result
[4];
1479 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1480 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1481 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1482 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1483 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1485 store_vector4(inst
, machine
, result
);
1487 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1488 result
[0], result
[1], result
[2], result
[3],
1489 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1493 case OPCODE_X2D
: /* 2-D matrix transform */
1495 GLfloat a
[4], b
[4], c
[4], result
[4];
1496 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1497 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1498 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1499 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1500 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1501 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1502 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1503 store_vector4(inst
, machine
, result
);
1508 if (inst
->SrcReg
[0].File
!= -1) {
1510 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1511 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst
->Data
,
1512 a
[0], a
[1], a
[2], a
[3]);
1515 _mesa_printf("%s\n", (const char *) inst
->Data
);
1522 _mesa_problem(ctx
, "Bad opcode %d in _mesa_exec_fragment_program",
1524 return GL_TRUE
; /* return value doesn't matter */
1529 if (numExec
> maxExec
) {
1530 _mesa_problem(ctx
, "Infinite loop detected in fragment program");
1536 #if FEATURE_MESA_program_debug
1537 CurrentMachine
= NULL
;