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.
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 "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
];
91 const struct gl_program_parameter_list
*params
;
92 ASSERT(source
->File
== PROGRAM_LOCAL_PARAM
||
93 source
->File
== PROGRAM_CONSTANT
||
94 source
->File
== PROGRAM_STATE_VAR
||
95 source
->File
== PROGRAM_UNIFORM
);
96 params
= machine
->CurProgram
->Parameters
;
97 if (reg
< 0 || reg
>= (GLint
)params
->NumParameters
)
100 return params
->ParameterValues
[reg
];
104 switch (source
->File
) {
105 case PROGRAM_TEMPORARY
:
106 ASSERT(source
->Index
< MAX_PROGRAM_TEMPS
);
107 return machine
->Temporaries
[source
->Index
];
110 if (machine
->CurProgram
->Target
== GL_VERTEX_PROGRAM_ARB
) {
111 ASSERT(source
->Index
< VERT_ATTRIB_MAX
);
112 return machine
->VertAttribs
[source
->Index
];
115 ASSERT(source
->Index
< FRAG_ATTRIB_MAX
);
116 return machine
->Attribs
[source
->Index
][machine
->CurElement
];
120 ASSERT(source
->Index
< MAX_PROGRAM_OUTPUTS
);
121 return machine
->Outputs
[source
->Index
];
123 case PROGRAM_LOCAL_PARAM
:
124 ASSERT(source
->Index
< MAX_PROGRAM_LOCAL_PARAMS
);
125 return machine
->CurProgram
->LocalParams
[source
->Index
];
127 case PROGRAM_ENV_PARAM
:
128 ASSERT(source
->Index
< MAX_PROGRAM_ENV_PARAMS
);
129 return machine
->EnvParams
[source
->Index
];
131 case PROGRAM_STATE_VAR
:
133 case PROGRAM_CONSTANT
:
135 case PROGRAM_UNIFORM
:
137 case PROGRAM_NAMED_PARAM
:
138 ASSERT(source
->Index
<
139 (GLint
) machine
->CurProgram
->Parameters
->NumParameters
);
140 return machine
->CurProgram
->Parameters
->ParameterValues
[source
->Index
];
144 "Invalid input register file %d in get_register_pointer()",
151 #if FEATURE_MESA_program_debug
152 static struct gl_program_machine
*CurrentMachine
= NULL
;
155 * For GL_MESA_program_debug.
156 * Return current value (4*GLfloat) of a program register.
157 * Called via ctx->Driver.GetProgramRegister().
160 _mesa_get_program_register(GLcontext
*ctx
, enum register_file file
,
161 GLuint index
, GLfloat val
[4])
163 if (CurrentMachine
) {
164 struct prog_src_register src
;
168 reg
= get_register_pointer(&src
, CurrentMachine
);
172 #endif /* FEATURE_MESA_program_debug */
176 * Fetch a 4-element float vector from the given source register.
177 * Apply swizzling and negating as needed.
180 fetch_vector4(const struct prog_src_register
*source
,
181 const struct gl_program_machine
*machine
, GLfloat result
[4])
183 const GLfloat
*src
= get_register_pointer(source
, machine
);
186 if (source
->Swizzle
== SWIZZLE_NOOP
) {
188 COPY_4V(result
, src
);
191 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
192 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
193 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
194 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
195 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
196 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
197 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
198 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
201 if (source
->NegateBase
) {
202 result
[0] = -result
[0];
203 result
[1] = -result
[1];
204 result
[2] = -result
[2];
205 result
[3] = -result
[3];
208 result
[0] = FABSF(result
[0]);
209 result
[1] = FABSF(result
[1]);
210 result
[2] = FABSF(result
[2]);
211 result
[3] = FABSF(result
[3]);
213 if (source
->NegateAbs
) {
214 result
[0] = -result
[0];
215 result
[1] = -result
[1];
216 result
[2] = -result
[2];
217 result
[3] = -result
[3];
223 * Fetch the derivative with respect to X or Y for the given register.
224 * XXX this currently only works for fragment program input attribs.
227 fetch_vector4_deriv(GLcontext
* ctx
,
228 const struct prog_src_register
*source
,
229 const struct gl_program_machine
*machine
,
230 char xOrY
, GLfloat result
[4])
232 if (source
->File
== PROGRAM_INPUT
&& source
->Index
< (GLint
)machine
->NumDeriv
) {
233 const GLint col
= machine
->CurElement
;
234 const GLfloat w
= machine
->Attribs
[FRAG_ATTRIB_WPOS
][col
][3];
235 const GLfloat invQ
= 1.0f
/ w
;
239 deriv
[0] = machine
->DerivX
[source
->Index
][0] * invQ
;
240 deriv
[1] = machine
->DerivX
[source
->Index
][1] * invQ
;
241 deriv
[2] = machine
->DerivX
[source
->Index
][2] * invQ
;
242 deriv
[3] = machine
->DerivX
[source
->Index
][3] * invQ
;
245 deriv
[0] = machine
->DerivY
[source
->Index
][0] * invQ
;
246 deriv
[1] = machine
->DerivY
[source
->Index
][1] * invQ
;
247 deriv
[2] = machine
->DerivY
[source
->Index
][2] * invQ
;
248 deriv
[3] = machine
->DerivY
[source
->Index
][3] * invQ
;
251 result
[0] = deriv
[GET_SWZ(source
->Swizzle
, 0)];
252 result
[1] = deriv
[GET_SWZ(source
->Swizzle
, 1)];
253 result
[2] = deriv
[GET_SWZ(source
->Swizzle
, 2)];
254 result
[3] = deriv
[GET_SWZ(source
->Swizzle
, 3)];
256 if (source
->NegateBase
) {
257 result
[0] = -result
[0];
258 result
[1] = -result
[1];
259 result
[2] = -result
[2];
260 result
[3] = -result
[3];
263 result
[0] = FABSF(result
[0]);
264 result
[1] = FABSF(result
[1]);
265 result
[2] = FABSF(result
[2]);
266 result
[3] = FABSF(result
[3]);
268 if (source
->NegateAbs
) {
269 result
[0] = -result
[0];
270 result
[1] = -result
[1];
271 result
[2] = -result
[2];
272 result
[3] = -result
[3];
276 ASSIGN_4V(result
, 0.0, 0.0, 0.0, 0.0);
282 * As above, but only return result[0] element.
285 fetch_vector1(const struct prog_src_register
*source
,
286 const struct gl_program_machine
*machine
, GLfloat result
[4])
288 const GLfloat
*src
= get_register_pointer(source
, machine
);
291 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
293 if (source
->NegateBase
) {
294 result
[0] = -result
[0];
297 result
[0] = FABSF(result
[0]);
299 if (source
->NegateAbs
) {
300 result
[0] = -result
[0];
306 * Fetch texel from texture. Use partial derivatives when possible.
309 fetch_texel(GLcontext
*ctx
,
310 const struct gl_program_machine
*machine
,
311 const struct prog_instruction
*inst
,
312 const GLfloat texcoord
[4], GLfloat lodBias
,
315 const GLuint unit
= machine
->Samplers
[inst
->TexSrcUnit
];
317 /* Note: we only have the right derivatives for fragment input attribs.
319 if (machine
->NumDeriv
> 0 &&
320 inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
321 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
) {
322 /* simple texture fetch for which we should have derivatives */
323 GLuint attr
= inst
->SrcReg
[0].Index
;
324 machine
->FetchTexelDeriv(ctx
, texcoord
,
325 machine
->DerivX
[attr
],
326 machine
->DerivY
[attr
],
327 lodBias
, unit
, color
);
330 machine
->FetchTexelLod(ctx
, texcoord
, lodBias
, unit
, color
);
336 * Test value against zero and return GT, LT, EQ or UN if NaN.
339 generate_cc(float value
)
342 return COND_UN
; /* NaN */
352 * Test if the ccMaskRule is satisfied by the given condition code.
353 * Used to mask destination writes according to the current condition code.
355 static INLINE GLboolean
356 test_cc(GLuint condCode
, GLuint ccMaskRule
)
358 switch (ccMaskRule
) {
359 case COND_EQ
: return (condCode
== COND_EQ
);
360 case COND_NE
: return (condCode
!= COND_EQ
);
361 case COND_LT
: return (condCode
== COND_LT
);
362 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
363 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
364 case COND_GT
: return (condCode
== COND_GT
);
365 case COND_TR
: return GL_TRUE
;
366 case COND_FL
: return GL_FALSE
;
367 default: return GL_TRUE
;
373 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
374 * or GL_FALSE to indicate result.
376 static INLINE GLboolean
377 eval_condition(const struct gl_program_machine
*machine
,
378 const struct prog_instruction
*inst
)
380 const GLuint swizzle
= inst
->DstReg
.CondSwizzle
;
381 const GLuint condMask
= inst
->DstReg
.CondMask
;
382 if (test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 0)], condMask
) ||
383 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 1)], condMask
) ||
384 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 2)], condMask
) ||
385 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 3)], condMask
)) {
396 * Store 4 floats into a register. Observe the instructions saturate and
397 * set-condition-code flags.
400 store_vector4(const struct prog_instruction
*inst
,
401 struct gl_program_machine
*machine
, const GLfloat value
[4])
403 const struct prog_dst_register
*dest
= &(inst
->DstReg
);
404 const GLboolean clamp
= inst
->SaturateMode
== SATURATE_ZERO_ONE
;
407 GLfloat clampedValue
[4];
408 GLuint writeMask
= dest
->WriteMask
;
410 switch (dest
->File
) {
412 ASSERT(dest
->Index
< MAX_PROGRAM_OUTPUTS
);
413 dstReg
= machine
->Outputs
[dest
->Index
];
415 case PROGRAM_TEMPORARY
:
416 ASSERT(dest
->Index
< MAX_PROGRAM_TEMPS
);
417 dstReg
= machine
->Temporaries
[dest
->Index
];
419 case PROGRAM_WRITE_ONLY
:
423 _mesa_problem(NULL
, "bad register file in store_vector4(fp)");
428 if (value
[0] > 1.0e10
||
429 IS_INF_OR_NAN(value
[0]) ||
430 IS_INF_OR_NAN(value
[1]) ||
431 IS_INF_OR_NAN(value
[2]) || IS_INF_OR_NAN(value
[3]))
432 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
436 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
437 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
438 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
439 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
440 value
= clampedValue
;
443 if (dest
->CondMask
!= COND_TR
) {
444 /* condition codes may turn off some writes */
445 if (writeMask
& WRITEMASK_X
) {
446 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 0)],
448 writeMask
&= ~WRITEMASK_X
;
450 if (writeMask
& WRITEMASK_Y
) {
451 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 1)],
453 writeMask
&= ~WRITEMASK_Y
;
455 if (writeMask
& WRITEMASK_Z
) {
456 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 2)],
458 writeMask
&= ~WRITEMASK_Z
;
460 if (writeMask
& WRITEMASK_W
) {
461 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 3)],
463 writeMask
&= ~WRITEMASK_W
;
467 if (writeMask
& WRITEMASK_X
)
468 dstReg
[0] = value
[0];
469 if (writeMask
& WRITEMASK_Y
)
470 dstReg
[1] = value
[1];
471 if (writeMask
& WRITEMASK_Z
)
472 dstReg
[2] = value
[2];
473 if (writeMask
& WRITEMASK_W
)
474 dstReg
[3] = value
[3];
476 if (inst
->CondUpdate
) {
477 if (writeMask
& WRITEMASK_X
)
478 machine
->CondCodes
[0] = generate_cc(value
[0]);
479 if (writeMask
& WRITEMASK_Y
)
480 machine
->CondCodes
[1] = generate_cc(value
[1]);
481 if (writeMask
& WRITEMASK_Z
)
482 machine
->CondCodes
[2] = generate_cc(value
[2]);
483 if (writeMask
& WRITEMASK_W
)
484 machine
->CondCodes
[3] = generate_cc(value
[3]);
486 printf("CondCodes=(%s,%s,%s,%s) for:\n",
487 _mesa_condcode_string(machine
->CondCodes
[0]),
488 _mesa_condcode_string(machine
->CondCodes
[1]),
489 _mesa_condcode_string(machine
->CondCodes
[2]),
490 _mesa_condcode_string(machine
->CondCodes
[3]));
497 * Execute the given vertex/fragment program.
499 * \param ctx rendering context
500 * \param program the program to execute
501 * \param machine machine state (must be initialized)
502 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
505 _mesa_execute_program(GLcontext
* ctx
,
506 const struct gl_program
*program
,
507 struct gl_program_machine
*machine
)
509 const GLuint numInst
= program
->NumInstructions
;
510 const GLuint maxExec
= 10000;
511 GLuint pc
, numExec
= 0;
513 machine
->CurProgram
= program
;
516 printf("execute program %u --------------------\n", program
->Id
);
519 #if FEATURE_MESA_program_debug
520 CurrentMachine
= machine
;
523 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
524 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
527 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
530 for (pc
= 0; pc
< numInst
; pc
++) {
531 const struct prog_instruction
*inst
= program
->Instructions
+ pc
;
533 #if FEATURE_MESA_program_debug
534 if (ctx
->FragmentProgram
.CallbackEnabled
&&
535 ctx
->FragmentProgram
.Callback
) {
536 ctx
->FragmentProgram
.CurrentPosition
= inst
->StringPos
;
537 ctx
->FragmentProgram
.Callback(program
->Target
,
538 ctx
->FragmentProgram
.CallbackData
);
543 _mesa_print_instruction(inst
);
546 switch (inst
->Opcode
) {
549 GLfloat a
[4], result
[4];
550 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
551 result
[0] = FABSF(a
[0]);
552 result
[1] = FABSF(a
[1]);
553 result
[2] = FABSF(a
[2]);
554 result
[3] = FABSF(a
[3]);
555 store_vector4(inst
, machine
, result
);
560 GLfloat a
[4], b
[4], result
[4];
561 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
562 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
563 result
[0] = a
[0] + b
[0];
564 result
[1] = a
[1] + b
[1];
565 result
[2] = a
[2] + b
[2];
566 result
[3] = a
[3] + b
[3];
567 store_vector4(inst
, machine
, result
);
569 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
570 result
[0], result
[1], result
[2], result
[3],
571 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
578 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
579 machine
->AddressReg
[0][0] = (GLint
) FLOORF(t
[0]);
586 /* subtract 1 here since pc is incremented by for(pc) loop */
587 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
589 case OPCODE_BGNSUB
: /* begin subroutine */
591 case OPCODE_ENDSUB
: /* end subroutine */
593 case OPCODE_BRA
: /* branch (conditional) */
595 case OPCODE_BRK
: /* break out of loop (conditional) */
597 case OPCODE_CONT
: /* continue loop (conditional) */
598 if (eval_condition(machine
, inst
)) {
600 /* Subtract 1 here since we'll do pc++ at end of for-loop */
601 pc
= inst
->BranchTarget
- 1;
604 case OPCODE_CAL
: /* Call subroutine (conditional) */
605 if (eval_condition(machine
, inst
)) {
606 /* call the subroutine */
607 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
608 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
610 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1; /* next inst */
611 /* Subtract 1 here since we'll do pc++ at end of for-loop */
612 pc
= inst
->BranchTarget
- 1;
617 GLfloat a
[4], b
[4], c
[4], result
[4];
618 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
619 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
620 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
621 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
622 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
623 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
624 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
625 store_vector4(inst
, machine
, result
);
630 GLfloat a
[4], result
[4];
631 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
632 result
[0] = result
[1] = result
[2] = result
[3]
633 = (GLfloat
) _mesa_cos(a
[0]);
634 store_vector4(inst
, machine
, result
);
637 case OPCODE_DDX
: /* Partial derivative with respect to X */
640 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
642 store_vector4(inst
, machine
, result
);
645 case OPCODE_DDY
: /* Partial derivative with respect to Y */
648 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
650 store_vector4(inst
, machine
, result
);
655 GLfloat a
[4], b
[4], result
[4];
656 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
657 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
658 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
659 store_vector4(inst
, machine
, result
);
661 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
662 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
668 GLfloat a
[4], b
[4], result
[4];
669 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
670 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
671 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
, b
);
672 store_vector4(inst
, machine
, result
);
674 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
675 result
[0], a
[0], a
[1], a
[2], a
[3],
676 b
[0], b
[1], b
[2], b
[3]);
682 GLfloat a
[4], b
[4], result
[4];
683 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
684 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
685 result
[0] = result
[1] = result
[2] = result
[3] =
686 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + b
[3];
687 store_vector4(inst
, machine
, result
);
690 case OPCODE_DST
: /* Distance vector */
692 GLfloat a
[4], b
[4], result
[4];
693 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
694 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
696 result
[1] = a
[1] * b
[1];
699 store_vector4(inst
, machine
, result
);
704 GLfloat t
[4], q
[4], floor_t0
;
705 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
706 floor_t0
= FLOORF(t
[0]);
707 if (floor_t0
> FLT_MAX_EXP
) {
708 SET_POS_INFINITY(q
[0]);
709 SET_POS_INFINITY(q
[2]);
711 else if (floor_t0
< FLT_MIN_EXP
) {
716 q
[0] = LDEXPF(1.0, (int) floor_t0
);
717 /* Note: GL_NV_vertex_program expects
718 * result.z = result.x * APPX(result.y)
719 * We do what the ARB extension says.
721 q
[2] = (GLfloat
) pow(2.0, t
[0]);
723 q
[1] = t
[0] - floor_t0
;
725 store_vector4( inst
, machine
, q
);
728 case OPCODE_EX2
: /* Exponential base 2 */
730 GLfloat a
[4], result
[4];
731 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
732 result
[0] = result
[1] = result
[2] = result
[3] =
733 (GLfloat
) _mesa_pow(2.0, a
[0]);
734 store_vector4(inst
, machine
, result
);
739 GLfloat a
[4], result
[4];
740 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
741 result
[0] = FLOORF(a
[0]);
742 result
[1] = FLOORF(a
[1]);
743 result
[2] = FLOORF(a
[2]);
744 result
[3] = FLOORF(a
[3]);
745 store_vector4(inst
, machine
, result
);
750 GLfloat a
[4], result
[4];
751 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
752 result
[0] = a
[0] - FLOORF(a
[0]);
753 result
[1] = a
[1] - FLOORF(a
[1]);
754 result
[2] = a
[2] - FLOORF(a
[2]);
755 result
[3] = a
[3] - FLOORF(a
[3]);
756 store_vector4(inst
, machine
, result
);
763 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
765 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
766 cond
= (a
[0] != 0.0);
769 cond
= eval_condition(machine
, inst
);
772 printf("IF: %d\n", cond
);
776 /* do if-clause (just continue execution) */
779 /* go to the instruction after ELSE or ENDIF */
780 assert(inst
->BranchTarget
>= 0);
781 pc
= inst
->BranchTarget
- 1;
787 assert(inst
->BranchTarget
>= 0);
788 pc
= inst
->BranchTarget
- 1;
793 case OPCODE_KIL_NV
: /* NV_f_p only (conditional) */
794 if (eval_condition(machine
, inst
)) {
798 case OPCODE_KIL
: /* ARB_f_p only */
801 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
802 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
807 case OPCODE_LG2
: /* log base 2 */
809 GLfloat a
[4], result
[4];
810 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
811 result
[0] = result
[1] = result
[2] = result
[3] = LOG2(a
[0]);
812 store_vector4(inst
, machine
, result
);
817 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
818 GLfloat a
[4], result
[4];
819 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
820 a
[0] = MAX2(a
[0], 0.0F
);
821 a
[1] = MAX2(a
[1], 0.0F
);
822 /* XXX ARB version clamps a[3], NV version doesn't */
823 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
826 /* XXX we could probably just use pow() here */
828 if (a
[1] == 0.0 && a
[3] == 0.0)
831 result
[2] = EXPF(a
[3] * LOGF(a
[1]));
837 store_vector4(inst
, machine
, result
);
839 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
840 result
[0], result
[1], result
[2], result
[3],
841 a
[0], a
[1], a
[2], a
[3]);
847 GLfloat t
[4], q
[4], abs_t0
;
848 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
849 abs_t0
= FABSF(t
[0]);
850 if (abs_t0
!= 0.0F
) {
851 /* Since we really can't handle infinite values on VMS
852 * like other OSes we'll use __MAXFLOAT to represent
853 * infinity. This may need some tweaking.
856 if (abs_t0
== __MAXFLOAT
)
858 if (IS_INF_OR_NAN(abs_t0
))
861 SET_POS_INFINITY(q
[0]);
863 SET_POS_INFINITY(q
[2]);
867 GLfloat mantissa
= FREXPF(t
[0], &exponent
);
868 q
[0] = (GLfloat
) (exponent
- 1);
869 q
[1] = (GLfloat
) (2.0 * mantissa
); /* map [.5, 1) -> [1, 2) */
870 q
[2] = (GLfloat
) (q
[0] + LOG2(q
[1]));
874 SET_NEG_INFINITY(q
[0]);
876 SET_NEG_INFINITY(q
[2]);
879 store_vector4(inst
, machine
, q
);
884 GLfloat a
[4], b
[4], c
[4], result
[4];
885 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
886 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
887 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
888 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
889 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
890 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
891 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
892 store_vector4(inst
, machine
, result
);
894 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
895 "(%g %g %g %g), (%g %g %g %g)\n",
896 result
[0], result
[1], result
[2], result
[3],
897 a
[0], a
[1], a
[2], a
[3],
898 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
904 GLfloat a
[4], b
[4], c
[4], result
[4];
905 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
906 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
907 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
908 result
[0] = a
[0] * b
[0] + c
[0];
909 result
[1] = a
[1] * b
[1] + c
[1];
910 result
[2] = a
[2] * b
[2] + c
[2];
911 result
[3] = a
[3] * b
[3] + c
[3];
912 store_vector4(inst
, machine
, result
);
914 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
915 "(%g %g %g %g) + (%g %g %g %g)\n",
916 result
[0], result
[1], result
[2], result
[3],
917 a
[0], a
[1], a
[2], a
[3],
918 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
924 GLfloat a
[4], b
[4], result
[4];
925 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
926 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
927 result
[0] = MAX2(a
[0], b
[0]);
928 result
[1] = MAX2(a
[1], b
[1]);
929 result
[2] = MAX2(a
[2], b
[2]);
930 result
[3] = MAX2(a
[3], b
[3]);
931 store_vector4(inst
, machine
, result
);
933 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
934 result
[0], result
[1], result
[2], result
[3],
935 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
941 GLfloat a
[4], b
[4], result
[4];
942 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
943 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
944 result
[0] = MIN2(a
[0], b
[0]);
945 result
[1] = MIN2(a
[1], b
[1]);
946 result
[2] = MIN2(a
[2], b
[2]);
947 result
[3] = MIN2(a
[3], b
[3]);
948 store_vector4(inst
, machine
, result
);
954 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
955 store_vector4(inst
, machine
, result
);
957 printf("MOV (%g %g %g %g)\n",
958 result
[0], result
[1], result
[2], result
[3]);
964 GLfloat a
[4], b
[4], result
[4];
965 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
966 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
967 result
[0] = a
[0] * b
[0];
968 result
[1] = a
[1] * b
[1];
969 result
[2] = a
[2] * b
[2];
970 result
[3] = a
[3] * b
[3];
971 store_vector4(inst
, machine
, result
);
973 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
974 result
[0], result
[1], result
[2], result
[3],
975 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
981 GLfloat a
[4], result
[4];
982 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
985 result
[2] = result
[3] = _slang_library_noise1(a
[0]);
986 store_vector4(inst
, machine
, result
);
991 GLfloat a
[4], result
[4];
992 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
995 result
[2] = result
[3] = _slang_library_noise2(a
[0], a
[1]);
996 store_vector4(inst
, machine
, result
);
1001 GLfloat a
[4], result
[4];
1002 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1006 result
[3] = _slang_library_noise3(a
[0], a
[1], a
[2]);
1007 store_vector4(inst
, machine
, result
);
1012 GLfloat a
[4], result
[4];
1013 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1017 result
[3] = _slang_library_noise4(a
[0], a
[1], a
[2], a
[3]);
1018 store_vector4(inst
, machine
, result
);
1023 case OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
1025 GLfloat a
[4], result
[4];
1027 GLuint
*rawResult
= (GLuint
*) result
;
1029 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1030 hx
= _mesa_float_to_half(a
[0]);
1031 hy
= _mesa_float_to_half(a
[1]);
1032 twoHalves
= hx
| (hy
<< 16);
1033 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1035 store_vector4(inst
, machine
, result
);
1038 case OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
1040 GLfloat a
[4], result
[4];
1041 GLuint usx
, usy
, *rawResult
= (GLuint
*) result
;
1042 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1043 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1044 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1045 usx
= IROUND(a
[0] * 65535.0F
);
1046 usy
= IROUND(a
[1] * 65535.0F
);
1047 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1048 = usx
| (usy
<< 16);
1049 store_vector4(inst
, machine
, result
);
1052 case OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
1054 GLfloat a
[4], result
[4];
1055 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
1056 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1057 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
1058 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
1059 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
1060 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
1061 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
1062 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
1063 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
1064 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
1065 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1066 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1067 store_vector4(inst
, machine
, result
);
1070 case OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
1072 GLfloat a
[4], result
[4];
1073 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
1074 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1075 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1076 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1077 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
1078 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
1079 ubx
= IROUND(255.0F
* a
[0]);
1080 uby
= IROUND(255.0F
* a
[1]);
1081 ubz
= IROUND(255.0F
* a
[2]);
1082 ubw
= IROUND(255.0F
* a
[3]);
1083 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1084 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1085 store_vector4(inst
, machine
, result
);
1090 GLfloat a
[4], b
[4], result
[4];
1091 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1092 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
1093 result
[0] = result
[1] = result
[2] = result
[3]
1094 = (GLfloat
) _mesa_pow(a
[0], b
[0]);
1095 store_vector4(inst
, machine
, result
);
1100 GLfloat a
[4], result
[4];
1101 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1105 else if (IS_INF_OR_NAN(a
[0]))
1106 printf("RCP(inf)\n");
1108 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
1109 store_vector4(inst
, machine
, result
);
1112 case OPCODE_RET
: /* return from subroutine (conditional) */
1113 if (eval_condition(machine
, inst
)) {
1114 if (machine
->StackDepth
== 0) {
1115 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
1117 /* subtract one because of pc++ in the for loop */
1118 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
1121 case OPCODE_RFL
: /* reflection vector */
1123 GLfloat axis
[4], dir
[4], result
[4], tmpX
, tmpW
;
1124 fetch_vector4(&inst
->SrcReg
[0], machine
, axis
);
1125 fetch_vector4(&inst
->SrcReg
[1], machine
, dir
);
1126 tmpW
= DOT3(axis
, axis
);
1127 tmpX
= (2.0F
* DOT3(axis
, dir
)) / tmpW
;
1128 result
[0] = tmpX
* axis
[0] - dir
[0];
1129 result
[1] = tmpX
* axis
[1] - dir
[1];
1130 result
[2] = tmpX
* axis
[2] - dir
[2];
1131 /* result[3] is never written! XXX enforce in parser! */
1132 store_vector4(inst
, machine
, result
);
1135 case OPCODE_RSQ
: /* 1 / sqrt() */
1137 GLfloat a
[4], result
[4];
1138 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1140 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1141 store_vector4(inst
, machine
, result
);
1143 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
1147 case OPCODE_SCS
: /* sine and cos */
1149 GLfloat a
[4], result
[4];
1150 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1151 result
[0] = (GLfloat
) _mesa_cos(a
[0]);
1152 result
[1] = (GLfloat
) _mesa_sin(a
[0]);
1153 result
[2] = 0.0; /* undefined! */
1154 result
[3] = 0.0; /* undefined! */
1155 store_vector4(inst
, machine
, result
);
1158 case OPCODE_SEQ
: /* set on equal */
1160 GLfloat a
[4], b
[4], result
[4];
1161 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1162 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1163 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1164 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1165 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1166 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1167 store_vector4(inst
, machine
, result
);
1169 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1170 result
[0], result
[1], result
[2], result
[3],
1171 a
[0], a
[1], a
[2], a
[3],
1172 b
[0], b
[1], b
[2], b
[3]);
1176 case OPCODE_SFL
: /* set false, operands ignored */
1178 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1179 store_vector4(inst
, machine
, result
);
1182 case OPCODE_SGE
: /* set on greater or equal */
1184 GLfloat a
[4], b
[4], result
[4];
1185 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1186 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1187 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1188 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1189 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1190 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1191 store_vector4(inst
, machine
, result
);
1193 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1194 result
[0], result
[1], result
[2], result
[3],
1195 a
[0], a
[1], a
[2], a
[3],
1196 b
[0], b
[1], b
[2], b
[3]);
1200 case OPCODE_SGT
: /* set on greater */
1202 GLfloat a
[4], b
[4], result
[4];
1203 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1204 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1205 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1206 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1207 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1208 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1209 store_vector4(inst
, machine
, result
);
1211 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1212 result
[0], result
[1], result
[2], result
[3],
1213 a
[0], a
[1], a
[2], a
[3],
1214 b
[0], b
[1], b
[2], b
[3]);
1220 GLfloat a
[4], result
[4];
1221 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1222 result
[0] = result
[1] = result
[2] = result
[3]
1223 = (GLfloat
) _mesa_sin(a
[0]);
1224 store_vector4(inst
, machine
, result
);
1227 case OPCODE_SLE
: /* set on less or equal */
1229 GLfloat a
[4], b
[4], result
[4];
1230 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1231 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1232 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1233 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1234 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1235 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1236 store_vector4(inst
, machine
, result
);
1238 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1239 result
[0], result
[1], result
[2], result
[3],
1240 a
[0], a
[1], a
[2], a
[3],
1241 b
[0], b
[1], b
[2], b
[3]);
1245 case OPCODE_SLT
: /* set on less */
1247 GLfloat a
[4], b
[4], result
[4];
1248 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1249 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1250 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1251 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1252 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1253 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1254 store_vector4(inst
, machine
, result
);
1256 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1257 result
[0], result
[1], result
[2], result
[3],
1258 a
[0], a
[1], a
[2], a
[3],
1259 b
[0], b
[1], b
[2], b
[3]);
1263 case OPCODE_SNE
: /* set on not equal */
1265 GLfloat a
[4], b
[4], result
[4];
1266 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1267 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1268 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1269 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1270 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1271 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1272 store_vector4(inst
, machine
, result
);
1274 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1275 result
[0], result
[1], result
[2], result
[3],
1276 a
[0], a
[1], a
[2], a
[3],
1277 b
[0], b
[1], b
[2], b
[3]);
1281 case OPCODE_STR
: /* set true, operands ignored */
1283 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1284 store_vector4(inst
, machine
, result
);
1289 GLfloat a
[4], b
[4], result
[4];
1290 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1291 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1292 result
[0] = a
[0] - b
[0];
1293 result
[1] = a
[1] - b
[1];
1294 result
[2] = a
[2] - b
[2];
1295 result
[3] = a
[3] - b
[3];
1296 store_vector4(inst
, machine
, result
);
1298 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1299 result
[0], result
[1], result
[2], result
[3],
1300 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1304 case OPCODE_SWZ
: /* extended swizzle */
1306 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1307 const GLfloat
*src
= get_register_pointer(source
, machine
);
1310 for (i
= 0; i
< 4; i
++) {
1311 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1312 if (swz
== SWIZZLE_ZERO
)
1314 else if (swz
== SWIZZLE_ONE
)
1319 result
[i
] = src
[swz
];
1321 if (source
->NegateBase
& (1 << i
))
1322 result
[i
] = -result
[i
];
1324 store_vector4(inst
, machine
, result
);
1327 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1328 /* Simple texel lookup */
1330 GLfloat texcoord
[4], color
[4];
1331 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1333 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1336 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1337 color
[0], color
[1], color
[2], color
[3],
1339 texcoord
[0], texcoord
[1], texcoord
[2], texcoord
[3]);
1341 store_vector4(inst
, machine
, color
);
1344 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1345 /* Texel lookup with LOD bias */
1347 const struct gl_texture_unit
*texUnit
1348 = &ctx
->Texture
.Unit
[inst
->TexSrcUnit
];
1349 GLfloat texcoord
[4], color
[4], lodBias
;
1351 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1353 /* texcoord[3] is the bias to add to lambda */
1354 lodBias
= texUnit
->LodBias
+ texcoord
[3];
1355 if (texUnit
->_Current
) {
1356 lodBias
+= texUnit
->_Current
->LodBias
;
1359 fetch_texel(ctx
, machine
, inst
, texcoord
, lodBias
, color
);
1361 store_vector4(inst
, machine
, color
);
1364 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1365 /* Texture lookup w/ partial derivatives for LOD */
1367 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1368 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1369 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1370 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1371 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1373 inst
->TexSrcUnit
, color
);
1374 store_vector4(inst
, machine
, color
);
1377 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1378 /* Texture lookup w/ projective divide */
1380 GLfloat texcoord
[4], color
[4];
1382 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1383 /* Not so sure about this test - if texcoord[3] is
1384 * zero, we'd probably be fine except for an ASSERT in
1385 * IROUND_POS() which gets triggered by the inf values created.
1387 if (texcoord
[3] != 0.0) {
1388 texcoord
[0] /= texcoord
[3];
1389 texcoord
[1] /= texcoord
[3];
1390 texcoord
[2] /= texcoord
[3];
1393 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1395 store_vector4(inst
, machine
, color
);
1398 case OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1399 /* Texture lookup w/ projective divide, as above, but do not
1400 * do the divide by w if sampling from a cube map.
1403 GLfloat texcoord
[4], color
[4];
1405 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1406 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1407 texcoord
[3] != 0.0) {
1408 texcoord
[0] /= texcoord
[3];
1409 texcoord
[1] /= texcoord
[3];
1410 texcoord
[2] /= texcoord
[3];
1413 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1415 store_vector4(inst
, machine
, color
);
1418 case OPCODE_TRUNC
: /* truncate toward zero */
1420 GLfloat a
[4], result
[4];
1421 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1422 result
[0] = (GLfloat
) (GLint
) a
[0];
1423 result
[1] = (GLfloat
) (GLint
) a
[1];
1424 result
[2] = (GLfloat
) (GLint
) a
[2];
1425 result
[3] = (GLfloat
) (GLint
) a
[3];
1426 store_vector4(inst
, machine
, result
);
1429 case OPCODE_UP2H
: /* unpack two 16-bit floats */
1431 GLfloat a
[4], result
[4];
1432 const GLuint
*rawBits
= (const GLuint
*) a
;
1434 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1435 hx
= rawBits
[0] & 0xffff;
1436 hy
= rawBits
[0] >> 16;
1437 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1438 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1439 store_vector4(inst
, machine
, result
);
1442 case OPCODE_UP2US
: /* unpack two GLushorts */
1444 GLfloat a
[4], result
[4];
1445 const GLuint
*rawBits
= (const GLuint
*) a
;
1447 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1448 usx
= rawBits
[0] & 0xffff;
1449 usy
= rawBits
[0] >> 16;
1450 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1451 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1452 store_vector4(inst
, machine
, result
);
1455 case OPCODE_UP4B
: /* unpack four GLbytes */
1457 GLfloat a
[4], result
[4];
1458 const GLuint
*rawBits
= (const GLuint
*) a
;
1459 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1460 result
[0] = (((rawBits
[0] >> 0) & 0xff) - 128) / 127.0F
;
1461 result
[1] = (((rawBits
[0] >> 8) & 0xff) - 128) / 127.0F
;
1462 result
[2] = (((rawBits
[0] >> 16) & 0xff) - 128) / 127.0F
;
1463 result
[3] = (((rawBits
[0] >> 24) & 0xff) - 128) / 127.0F
;
1464 store_vector4(inst
, machine
, result
);
1467 case OPCODE_UP4UB
: /* unpack four GLubytes */
1469 GLfloat a
[4], result
[4];
1470 const GLuint
*rawBits
= (const GLuint
*) a
;
1471 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1472 result
[0] = ((rawBits
[0] >> 0) & 0xff) / 255.0F
;
1473 result
[1] = ((rawBits
[0] >> 8) & 0xff) / 255.0F
;
1474 result
[2] = ((rawBits
[0] >> 16) & 0xff) / 255.0F
;
1475 result
[3] = ((rawBits
[0] >> 24) & 0xff) / 255.0F
;
1476 store_vector4(inst
, machine
, result
);
1479 case OPCODE_XPD
: /* cross product */
1481 GLfloat a
[4], b
[4], result
[4];
1482 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1483 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1484 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1485 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1486 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1488 store_vector4(inst
, machine
, result
);
1490 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1491 result
[0], result
[1], result
[2], result
[3],
1492 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1496 case OPCODE_X2D
: /* 2-D matrix transform */
1498 GLfloat a
[4], b
[4], c
[4], result
[4];
1499 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1500 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1501 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1502 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1503 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1504 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1505 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1506 store_vector4(inst
, machine
, result
);
1511 if (inst
->SrcReg
[0].File
!= -1) {
1513 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1514 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst
->Data
,
1515 a
[0], a
[1], a
[2], a
[3]);
1518 _mesa_printf("%s\n", (const char *) inst
->Data
);
1525 _mesa_problem(ctx
, "Bad opcode %d in _mesa_execute_program",
1527 return GL_TRUE
; /* return value doesn't matter */
1531 if (numExec
> maxExec
) {
1532 _mesa_problem(ctx
, "Infinite loop detected in fragment program");
1538 #if FEATURE_MESA_program_debug
1539 CurrentMachine
= NULL
;