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_CONSTANT
||
93 source
->File
== PROGRAM_STATE_VAR
);
94 params
= machine
->CurProgram
->Parameters
;
95 if (reg
< 0 || reg
>= params
->NumParameters
)
98 return params
->ParameterValues
[reg
];
102 switch (source
->File
) {
103 case PROGRAM_TEMPORARY
:
104 ASSERT(source
->Index
< MAX_PROGRAM_TEMPS
);
105 return machine
->Temporaries
[source
->Index
];
108 if (machine
->CurProgram
->Target
== GL_VERTEX_PROGRAM_ARB
) {
109 ASSERT(source
->Index
< VERT_ATTRIB_MAX
);
110 return machine
->VertAttribs
[source
->Index
];
113 ASSERT(source
->Index
< FRAG_ATTRIB_MAX
);
114 return machine
->Attribs
[source
->Index
][machine
->CurElement
];
118 ASSERT(source
->Index
< MAX_PROGRAM_OUTPUTS
);
119 return machine
->Outputs
[source
->Index
];
121 case PROGRAM_LOCAL_PARAM
:
122 ASSERT(source
->Index
< MAX_PROGRAM_LOCAL_PARAMS
);
123 return machine
->CurProgram
->LocalParams
[source
->Index
];
125 case PROGRAM_ENV_PARAM
:
126 ASSERT(source
->Index
< MAX_PROGRAM_ENV_PARAMS
);
127 return machine
->EnvParams
[source
->Index
];
129 case PROGRAM_STATE_VAR
:
131 case PROGRAM_CONSTANT
:
133 case PROGRAM_UNIFORM
:
135 case PROGRAM_NAMED_PARAM
:
136 ASSERT(source
->Index
<
137 (GLint
) machine
->CurProgram
->Parameters
->NumParameters
);
138 return machine
->CurProgram
->Parameters
->ParameterValues
[source
->Index
];
142 "Invalid input register file %d in get_register_pointer()",
149 #if FEATURE_MESA_program_debug
150 static struct gl_program_machine
*CurrentMachine
= NULL
;
153 * For GL_MESA_program_debug.
154 * Return current value (4*GLfloat) of a program register.
155 * Called via ctx->Driver.GetProgramRegister().
158 _mesa_get_program_register(GLcontext
*ctx
, enum register_file file
,
159 GLuint index
, GLfloat val
[4])
161 if (CurrentMachine
) {
162 struct prog_src_register src
;
166 reg
= get_register_pointer(&src
, CurrentMachine
);
170 #endif /* FEATURE_MESA_program_debug */
174 * Fetch a 4-element float vector from the given source register.
175 * Apply swizzling and negating as needed.
178 fetch_vector4(const struct prog_src_register
*source
,
179 const struct gl_program_machine
*machine
, GLfloat result
[4])
181 const GLfloat
*src
= get_register_pointer(source
, machine
);
184 if (source
->Swizzle
== SWIZZLE_NOOP
) {
186 COPY_4V(result
, src
);
189 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
190 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
191 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
192 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
193 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
194 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
195 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
196 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
199 if (source
->NegateBase
) {
200 result
[0] = -result
[0];
201 result
[1] = -result
[1];
202 result
[2] = -result
[2];
203 result
[3] = -result
[3];
206 result
[0] = FABSF(result
[0]);
207 result
[1] = FABSF(result
[1]);
208 result
[2] = FABSF(result
[2]);
209 result
[3] = FABSF(result
[3]);
211 if (source
->NegateAbs
) {
212 result
[0] = -result
[0];
213 result
[1] = -result
[1];
214 result
[2] = -result
[2];
215 result
[3] = -result
[3];
221 * Fetch the derivative with respect to X or Y for the given register.
222 * XXX this currently only works for fragment program input attribs.
225 fetch_vector4_deriv(GLcontext
* ctx
,
226 const struct prog_src_register
*source
,
227 const struct gl_program_machine
*machine
,
228 char xOrY
, GLfloat result
[4])
230 if (source
->File
== PROGRAM_INPUT
&& source
->Index
< machine
->NumDeriv
) {
231 const GLint col
= machine
->CurElement
;
232 const GLfloat w
= machine
->Attribs
[FRAG_ATTRIB_WPOS
][col
][3];
233 const GLfloat invQ
= 1.0f
/ w
;
237 deriv
[0] = machine
->DerivX
[source
->Index
][0] * invQ
;
238 deriv
[1] = machine
->DerivX
[source
->Index
][1] * invQ
;
239 deriv
[2] = machine
->DerivX
[source
->Index
][2] * invQ
;
240 deriv
[3] = machine
->DerivX
[source
->Index
][3] * invQ
;
243 deriv
[0] = machine
->DerivY
[source
->Index
][0] * invQ
;
244 deriv
[1] = machine
->DerivY
[source
->Index
][1] * invQ
;
245 deriv
[2] = machine
->DerivY
[source
->Index
][2] * invQ
;
246 deriv
[3] = machine
->DerivY
[source
->Index
][3] * invQ
;
249 result
[0] = deriv
[GET_SWZ(source
->Swizzle
, 0)];
250 result
[1] = deriv
[GET_SWZ(source
->Swizzle
, 1)];
251 result
[2] = deriv
[GET_SWZ(source
->Swizzle
, 2)];
252 result
[3] = deriv
[GET_SWZ(source
->Swizzle
, 3)];
254 if (source
->NegateBase
) {
255 result
[0] = -result
[0];
256 result
[1] = -result
[1];
257 result
[2] = -result
[2];
258 result
[3] = -result
[3];
261 result
[0] = FABSF(result
[0]);
262 result
[1] = FABSF(result
[1]);
263 result
[2] = FABSF(result
[2]);
264 result
[3] = FABSF(result
[3]);
266 if (source
->NegateAbs
) {
267 result
[0] = -result
[0];
268 result
[1] = -result
[1];
269 result
[2] = -result
[2];
270 result
[3] = -result
[3];
274 ASSIGN_4V(result
, 0.0, 0.0, 0.0, 0.0);
280 * As above, but only return result[0] element.
283 fetch_vector1(const struct prog_src_register
*source
,
284 const struct gl_program_machine
*machine
, GLfloat result
[4])
286 const GLfloat
*src
= get_register_pointer(source
, machine
);
289 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
291 if (source
->NegateBase
) {
292 result
[0] = -result
[0];
295 result
[0] = FABSF(result
[0]);
297 if (source
->NegateAbs
) {
298 result
[0] = -result
[0];
304 * Fetch texel from texture. Use partial derivatives when possible.
307 fetch_texel(GLcontext
*ctx
,
308 const struct gl_program_machine
*machine
,
309 const struct prog_instruction
*inst
,
310 const GLfloat texcoord
[4], GLfloat lodBias
,
313 /* Note: we only have the right derivatives for fragment input attribs.
315 if (machine
->NumDeriv
> 0 &&
316 inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
317 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
) {
318 /* simple texture fetch for which we should have derivatives */
319 GLuint attr
= inst
->SrcReg
[0].Index
;
320 machine
->FetchTexelDeriv(ctx
, texcoord
,
321 machine
->DerivX
[attr
],
322 machine
->DerivY
[attr
],
324 inst
->TexSrcUnit
, color
);
327 machine
->FetchTexelLod(ctx
, texcoord
, lodBias
,
328 inst
->TexSrcUnit
, color
);
334 * Test value against zero and return GT, LT, EQ or UN if NaN.
337 generate_cc(float value
)
340 return COND_UN
; /* NaN */
350 * Test if the ccMaskRule is satisfied by the given condition code.
351 * Used to mask destination writes according to the current condition code.
353 static INLINE GLboolean
354 test_cc(GLuint condCode
, GLuint ccMaskRule
)
356 switch (ccMaskRule
) {
357 case COND_EQ
: return (condCode
== COND_EQ
);
358 case COND_NE
: return (condCode
!= COND_EQ
);
359 case COND_LT
: return (condCode
== COND_LT
);
360 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
361 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
362 case COND_GT
: return (condCode
== COND_GT
);
363 case COND_TR
: return GL_TRUE
;
364 case COND_FL
: return GL_FALSE
;
365 default: return GL_TRUE
;
371 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
372 * or GL_FALSE to indicate result.
374 static INLINE GLboolean
375 eval_condition(const struct gl_program_machine
*machine
,
376 const struct prog_instruction
*inst
)
378 const GLuint swizzle
= inst
->DstReg
.CondSwizzle
;
379 const GLuint condMask
= inst
->DstReg
.CondMask
;
380 if (test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 0)], condMask
) ||
381 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 1)], condMask
) ||
382 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 2)], condMask
) ||
383 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 3)], condMask
)) {
394 * Store 4 floats into a register. Observe the instructions saturate and
395 * set-condition-code flags.
398 store_vector4(const struct prog_instruction
*inst
,
399 struct gl_program_machine
*machine
, const GLfloat value
[4])
401 const struct prog_dst_register
*dest
= &(inst
->DstReg
);
402 const GLboolean clamp
= inst
->SaturateMode
== SATURATE_ZERO_ONE
;
405 GLfloat clampedValue
[4];
406 GLuint writeMask
= dest
->WriteMask
;
408 switch (dest
->File
) {
410 ASSERT(dest
->Index
< MAX_PROGRAM_OUTPUTS
);
411 dstReg
= machine
->Outputs
[dest
->Index
];
413 case PROGRAM_TEMPORARY
:
414 ASSERT(dest
->Index
< MAX_PROGRAM_TEMPS
);
415 dstReg
= machine
->Temporaries
[dest
->Index
];
417 case PROGRAM_WRITE_ONLY
:
421 _mesa_problem(NULL
, "bad register file in store_vector4(fp)");
426 if (value
[0] > 1.0e10
||
427 IS_INF_OR_NAN(value
[0]) ||
428 IS_INF_OR_NAN(value
[1]) ||
429 IS_INF_OR_NAN(value
[2]) || IS_INF_OR_NAN(value
[3]))
430 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
434 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
435 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
436 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
437 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
438 value
= clampedValue
;
441 if (dest
->CondMask
!= COND_TR
) {
442 /* condition codes may turn off some writes */
443 if (writeMask
& WRITEMASK_X
) {
444 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 0)],
446 writeMask
&= ~WRITEMASK_X
;
448 if (writeMask
& WRITEMASK_Y
) {
449 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 1)],
451 writeMask
&= ~WRITEMASK_Y
;
453 if (writeMask
& WRITEMASK_Z
) {
454 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 2)],
456 writeMask
&= ~WRITEMASK_Z
;
458 if (writeMask
& WRITEMASK_W
) {
459 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 3)],
461 writeMask
&= ~WRITEMASK_W
;
465 if (writeMask
& WRITEMASK_X
)
466 dstReg
[0] = value
[0];
467 if (writeMask
& WRITEMASK_Y
)
468 dstReg
[1] = value
[1];
469 if (writeMask
& WRITEMASK_Z
)
470 dstReg
[2] = value
[2];
471 if (writeMask
& WRITEMASK_W
)
472 dstReg
[3] = value
[3];
474 if (inst
->CondUpdate
) {
475 if (writeMask
& WRITEMASK_X
)
476 machine
->CondCodes
[0] = generate_cc(value
[0]);
477 if (writeMask
& WRITEMASK_Y
)
478 machine
->CondCodes
[1] = generate_cc(value
[1]);
479 if (writeMask
& WRITEMASK_Z
)
480 machine
->CondCodes
[2] = generate_cc(value
[2]);
481 if (writeMask
& WRITEMASK_W
)
482 machine
->CondCodes
[3] = generate_cc(value
[3]);
484 printf("CondCodes=(%s,%s,%s,%s) for:\n",
485 _mesa_condcode_string(machine
->CondCodes
[0]),
486 _mesa_condcode_string(machine
->CondCodes
[1]),
487 _mesa_condcode_string(machine
->CondCodes
[2]),
488 _mesa_condcode_string(machine
->CondCodes
[3]));
495 * Execute the given vertex/fragment program.
497 * \param ctx rendering context
498 * \param program the program to execute
499 * \param machine machine state (must be initialized)
500 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
503 _mesa_execute_program(GLcontext
* ctx
,
504 const struct gl_program
*program
,
505 struct gl_program_machine
*machine
)
507 const GLuint numInst
= program
->NumInstructions
;
508 const GLuint maxExec
= 10000;
509 GLint pc
, numExec
= 0;
511 machine
->CurProgram
= program
;
514 printf("execute program %u --------------------\n", program
->Id
);
517 #if FEATURE_MESA_program_debug
518 CurrentMachine
= machine
;
521 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
522 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
525 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
528 for (pc
= 0; pc
< numInst
; pc
++) {
529 const struct prog_instruction
*inst
= program
->Instructions
+ pc
;
531 #if FEATURE_MESA_program_debug
532 if (ctx
->FragmentProgram
.CallbackEnabled
&&
533 ctx
->FragmentProgram
.Callback
) {
534 ctx
->FragmentProgram
.CurrentPosition
= inst
->StringPos
;
535 ctx
->FragmentProgram
.Callback(program
->Target
,
536 ctx
->FragmentProgram
.CallbackData
);
541 _mesa_print_instruction(inst
);
544 switch (inst
->Opcode
) {
547 GLfloat a
[4], result
[4];
548 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
549 result
[0] = FABSF(a
[0]);
550 result
[1] = FABSF(a
[1]);
551 result
[2] = FABSF(a
[2]);
552 result
[3] = FABSF(a
[3]);
553 store_vector4(inst
, machine
, result
);
558 GLfloat a
[4], b
[4], result
[4];
559 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
560 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
561 result
[0] = a
[0] + b
[0];
562 result
[1] = a
[1] + b
[1];
563 result
[2] = a
[2] + b
[2];
564 result
[3] = a
[3] + b
[3];
565 store_vector4(inst
, machine
, result
);
567 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
568 result
[0], result
[1], result
[2], result
[3],
569 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
576 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
577 machine
->AddressReg
[0][0] = (GLint
) FLOORF(t
[0]);
584 /* subtract 1 here since pc is incremented by for(pc) loop */
585 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
587 case OPCODE_BGNSUB
: /* begin subroutine */
589 case OPCODE_ENDSUB
: /* end subroutine */
591 case OPCODE_BRA
: /* branch (conditional) */
593 case OPCODE_BRK
: /* break out of loop (conditional) */
595 case OPCODE_CONT
: /* continue loop (conditional) */
596 if (eval_condition(machine
, inst
)) {
598 /* Subtract 1 here since we'll do pc++ at end of for-loop */
599 pc
= inst
->BranchTarget
- 1;
602 case OPCODE_CAL
: /* Call subroutine (conditional) */
603 if (eval_condition(machine
, inst
)) {
604 /* call the subroutine */
605 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
606 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
608 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1; /* next inst */
609 /* Subtract 1 here since we'll do pc++ at end of for-loop */
610 pc
= inst
->BranchTarget
- 1;
615 GLfloat a
[4], b
[4], c
[4], result
[4];
616 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
617 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
618 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
619 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
620 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
621 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
622 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
623 store_vector4(inst
, machine
, result
);
628 GLfloat a
[4], result
[4];
629 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
630 result
[0] = result
[1] = result
[2] = result
[3]
631 = (GLfloat
) _mesa_cos(a
[0]);
632 store_vector4(inst
, machine
, result
);
635 case OPCODE_DDX
: /* Partial derivative with respect to X */
638 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
640 store_vector4(inst
, machine
, result
);
643 case OPCODE_DDY
: /* Partial derivative with respect to Y */
646 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
648 store_vector4(inst
, machine
, result
);
653 GLfloat a
[4], b
[4], result
[4];
654 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
655 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
656 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
657 store_vector4(inst
, machine
, result
);
659 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
660 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
666 GLfloat a
[4], b
[4], result
[4];
667 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
668 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
669 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
, b
);
670 store_vector4(inst
, machine
, result
);
672 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
673 result
[0], a
[0], a
[1], a
[2], a
[3],
674 b
[0], b
[1], b
[2], b
[3]);
680 GLfloat a
[4], b
[4], result
[4];
681 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
682 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
683 result
[0] = result
[1] = result
[2] = result
[3] =
684 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + b
[3];
685 store_vector4(inst
, machine
, result
);
688 case OPCODE_DST
: /* Distance vector */
690 GLfloat a
[4], b
[4], result
[4];
691 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
692 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
694 result
[1] = a
[1] * b
[1];
697 store_vector4(inst
, machine
, result
);
702 GLfloat t
[4], q
[4], floor_t0
;
703 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
704 floor_t0
= FLOORF(t
[0]);
705 if (floor_t0
> FLT_MAX_EXP
) {
706 SET_POS_INFINITY(q
[0]);
707 SET_POS_INFINITY(q
[2]);
709 else if (floor_t0
< FLT_MIN_EXP
) {
714 q
[0] = LDEXPF(1.0, (int) floor_t0
);
715 /* Note: GL_NV_vertex_program expects
716 * result.z = result.x * APPX(result.y)
717 * We do what the ARB extension says.
719 q
[2] = pow(2.0, t
[0]);
721 q
[1] = t
[0] - floor_t0
;
723 store_vector4( inst
, machine
, q
);
726 case OPCODE_EX2
: /* Exponential base 2 */
728 GLfloat a
[4], result
[4];
729 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
730 result
[0] = result
[1] = result
[2] = result
[3] =
731 (GLfloat
) _mesa_pow(2.0, a
[0]);
732 store_vector4(inst
, machine
, result
);
737 GLfloat a
[4], result
[4];
738 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
739 result
[0] = FLOORF(a
[0]);
740 result
[1] = FLOORF(a
[1]);
741 result
[2] = FLOORF(a
[2]);
742 result
[3] = FLOORF(a
[3]);
743 store_vector4(inst
, machine
, result
);
748 GLfloat a
[4], result
[4];
749 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
750 result
[0] = a
[0] - FLOORF(a
[0]);
751 result
[1] = a
[1] - FLOORF(a
[1]);
752 result
[2] = a
[2] - FLOORF(a
[2]);
753 result
[3] = a
[3] - FLOORF(a
[3]);
754 store_vector4(inst
, machine
, result
);
761 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
763 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
764 cond
= (a
[0] != 0.0);
767 cond
= eval_condition(machine
, inst
);
770 printf("IF: %d\n", cond
);
774 /* do if-clause (just continue execution) */
777 /* go to the instruction after ELSE or ENDIF */
778 assert(inst
->BranchTarget
>= 0);
779 pc
= inst
->BranchTarget
- 1;
785 assert(inst
->BranchTarget
>= 0);
786 pc
= inst
->BranchTarget
- 1;
791 case OPCODE_INT
: /* float to int */
793 GLfloat a
[4], result
[4];
794 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
795 result
[0] = (GLfloat
) (GLint
) a
[0];
796 result
[1] = (GLfloat
) (GLint
) a
[1];
797 result
[2] = (GLfloat
) (GLint
) a
[2];
798 result
[3] = (GLfloat
) (GLint
) a
[3];
799 store_vector4(inst
, machine
, result
);
802 case OPCODE_KIL_NV
: /* NV_f_p only (conditional) */
803 if (eval_condition(machine
, inst
)) {
807 case OPCODE_KIL
: /* ARB_f_p only */
810 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
811 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
816 case OPCODE_LG2
: /* log base 2 */
818 GLfloat a
[4], result
[4];
819 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
820 result
[0] = result
[1] = result
[2] = result
[3] = LOG2(a
[0]);
821 store_vector4(inst
, machine
, result
);
826 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
827 GLfloat a
[4], result
[4];
828 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
829 a
[0] = MAX2(a
[0], 0.0F
);
830 a
[1] = MAX2(a
[1], 0.0F
);
831 /* XXX ARB version clamps a[3], NV version doesn't */
832 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
835 /* XXX we could probably just use pow() here */
837 if (a
[1] == 0.0 && a
[3] == 0.0)
840 result
[2] = EXPF(a
[3] * LOGF(a
[1]));
846 store_vector4(inst
, machine
, result
);
848 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
849 result
[0], result
[1], result
[2], result
[3],
850 a
[0], a
[1], a
[2], a
[3]);
856 GLfloat t
[4], q
[4], abs_t0
;
857 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
858 abs_t0
= FABSF(t
[0]);
859 if (abs_t0
!= 0.0F
) {
860 /* Since we really can't handle infinite values on VMS
861 * like other OSes we'll use __MAXFLOAT to represent
862 * infinity. This may need some tweaking.
865 if (abs_t0
== __MAXFLOAT
)
867 if (IS_INF_OR_NAN(abs_t0
))
870 SET_POS_INFINITY(q
[0]);
872 SET_POS_INFINITY(q
[2]);
876 GLfloat mantissa
= FREXPF(t
[0], &exponent
);
877 q
[0] = (GLfloat
) (exponent
- 1);
878 q
[1] = (GLfloat
) (2.0 * mantissa
); /* map [.5, 1) -> [1, 2) */
879 q
[2] = (GLfloat
) (q
[0] + LOG2(q
[1]));
883 SET_NEG_INFINITY(q
[0]);
885 SET_NEG_INFINITY(q
[2]);
888 store_vector4(inst
, machine
, q
);
893 GLfloat a
[4], b
[4], c
[4], result
[4];
894 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
895 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
896 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
897 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
898 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
899 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
900 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
901 store_vector4(inst
, machine
, result
);
903 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
904 "(%g %g %g %g), (%g %g %g %g)\n",
905 result
[0], result
[1], result
[2], result
[3],
906 a
[0], a
[1], a
[2], a
[3],
907 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
913 GLfloat a
[4], b
[4], c
[4], result
[4];
914 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
915 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
916 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
917 result
[0] = a
[0] * b
[0] + c
[0];
918 result
[1] = a
[1] * b
[1] + c
[1];
919 result
[2] = a
[2] * b
[2] + c
[2];
920 result
[3] = a
[3] * b
[3] + c
[3];
921 store_vector4(inst
, machine
, result
);
923 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
924 "(%g %g %g %g) + (%g %g %g %g)\n",
925 result
[0], result
[1], result
[2], result
[3],
926 a
[0], a
[1], a
[2], a
[3],
927 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
933 GLfloat a
[4], b
[4], result
[4];
934 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
935 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
936 result
[0] = MAX2(a
[0], b
[0]);
937 result
[1] = MAX2(a
[1], b
[1]);
938 result
[2] = MAX2(a
[2], b
[2]);
939 result
[3] = MAX2(a
[3], b
[3]);
940 store_vector4(inst
, machine
, result
);
942 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
943 result
[0], result
[1], result
[2], result
[3],
944 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
950 GLfloat a
[4], b
[4], result
[4];
951 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
952 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
953 result
[0] = MIN2(a
[0], b
[0]);
954 result
[1] = MIN2(a
[1], b
[1]);
955 result
[2] = MIN2(a
[2], b
[2]);
956 result
[3] = MIN2(a
[3], b
[3]);
957 store_vector4(inst
, machine
, result
);
963 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
964 store_vector4(inst
, machine
, result
);
966 printf("MOV (%g %g %g %g)\n",
967 result
[0], result
[1], result
[2], result
[3]);
973 GLfloat a
[4], b
[4], result
[4];
974 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
975 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
976 result
[0] = a
[0] * b
[0];
977 result
[1] = a
[1] * b
[1];
978 result
[2] = a
[2] * b
[2];
979 result
[3] = a
[3] * b
[3];
980 store_vector4(inst
, machine
, result
);
982 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
983 result
[0], result
[1], result
[2], result
[3],
984 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
990 GLfloat a
[4], result
[4];
991 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
994 result
[2] = result
[3] = _slang_library_noise1(a
[0]);
995 store_vector4(inst
, machine
, result
);
1000 GLfloat a
[4], result
[4];
1001 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1004 result
[2] = result
[3] = _slang_library_noise2(a
[0], a
[1]);
1005 store_vector4(inst
, machine
, result
);
1010 GLfloat a
[4], result
[4];
1011 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1015 result
[3] = _slang_library_noise3(a
[0], a
[1], a
[2]);
1016 store_vector4(inst
, machine
, result
);
1021 GLfloat a
[4], result
[4];
1022 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1026 result
[3] = _slang_library_noise4(a
[0], a
[1], a
[2], a
[3]);
1027 store_vector4(inst
, machine
, result
);
1032 case OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
1034 GLfloat a
[4], result
[4];
1036 GLuint
*rawResult
= (GLuint
*) result
;
1038 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1039 hx
= _mesa_float_to_half(a
[0]);
1040 hy
= _mesa_float_to_half(a
[1]);
1041 twoHalves
= hx
| (hy
<< 16);
1042 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1044 store_vector4(inst
, machine
, result
);
1047 case OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
1049 GLfloat a
[4], result
[4];
1050 GLuint usx
, usy
, *rawResult
= (GLuint
*) result
;
1051 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1052 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1053 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1054 usx
= IROUND(a
[0] * 65535.0F
);
1055 usy
= IROUND(a
[1] * 65535.0F
);
1056 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1057 = usx
| (usy
<< 16);
1058 store_vector4(inst
, machine
, result
);
1061 case OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
1063 GLfloat a
[4], result
[4];
1064 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
1065 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1066 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
1067 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
1068 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
1069 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
1070 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
1071 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
1072 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
1073 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
1074 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1075 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1076 store_vector4(inst
, machine
, result
);
1079 case OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
1081 GLfloat a
[4], result
[4];
1082 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
1083 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1084 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1085 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1086 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
1087 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
1088 ubx
= IROUND(255.0F
* a
[0]);
1089 uby
= IROUND(255.0F
* a
[1]);
1090 ubz
= IROUND(255.0F
* a
[2]);
1091 ubw
= IROUND(255.0F
* a
[3]);
1092 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1093 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1094 store_vector4(inst
, machine
, result
);
1099 GLfloat a
[4], b
[4], result
[4];
1100 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1101 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
1102 result
[0] = result
[1] = result
[2] = result
[3]
1103 = (GLfloat
) _mesa_pow(a
[0], b
[0]);
1104 store_vector4(inst
, machine
, result
);
1109 GLfloat a
[4], result
[4];
1110 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1114 else if (IS_INF_OR_NAN(a
[0]))
1115 printf("RCP(inf)\n");
1117 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
1118 store_vector4(inst
, machine
, result
);
1121 case OPCODE_RET
: /* return from subroutine (conditional) */
1122 if (eval_condition(machine
, inst
)) {
1123 if (machine
->StackDepth
== 0) {
1124 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
1126 /* subtract one because of pc++ in the for loop */
1127 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
1130 case OPCODE_RFL
: /* reflection vector */
1132 GLfloat axis
[4], dir
[4], result
[4], tmpX
, tmpW
;
1133 fetch_vector4(&inst
->SrcReg
[0], machine
, axis
);
1134 fetch_vector4(&inst
->SrcReg
[1], machine
, dir
);
1135 tmpW
= DOT3(axis
, axis
);
1136 tmpX
= (2.0F
* DOT3(axis
, dir
)) / tmpW
;
1137 result
[0] = tmpX
* axis
[0] - dir
[0];
1138 result
[1] = tmpX
* axis
[1] - dir
[1];
1139 result
[2] = tmpX
* axis
[2] - dir
[2];
1140 /* result[3] is never written! XXX enforce in parser! */
1141 store_vector4(inst
, machine
, result
);
1144 case OPCODE_RSQ
: /* 1 / sqrt() */
1146 GLfloat a
[4], result
[4];
1147 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1149 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1150 store_vector4(inst
, machine
, result
);
1152 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
1156 case OPCODE_SCS
: /* sine and cos */
1158 GLfloat a
[4], result
[4];
1159 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1160 result
[0] = (GLfloat
) _mesa_cos(a
[0]);
1161 result
[1] = (GLfloat
) _mesa_sin(a
[0]);
1162 result
[2] = 0.0; /* undefined! */
1163 result
[3] = 0.0; /* undefined! */
1164 store_vector4(inst
, machine
, result
);
1167 case OPCODE_SEQ
: /* set on equal */
1169 GLfloat a
[4], b
[4], result
[4];
1170 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1171 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1172 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1173 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1174 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1175 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1176 store_vector4(inst
, machine
, result
);
1178 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1179 result
[0], result
[1], result
[2], result
[3],
1180 a
[0], a
[1], a
[2], a
[3],
1181 b
[0], b
[1], b
[2], b
[3]);
1185 case OPCODE_SFL
: /* set false, operands ignored */
1187 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1188 store_vector4(inst
, machine
, result
);
1191 case OPCODE_SGE
: /* set on greater or equal */
1193 GLfloat a
[4], b
[4], result
[4];
1194 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1195 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1196 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1197 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1198 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1199 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1200 store_vector4(inst
, machine
, result
);
1202 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1203 result
[0], result
[1], result
[2], result
[3],
1204 a
[0], a
[1], a
[2], a
[3],
1205 b
[0], b
[1], b
[2], b
[3]);
1209 case OPCODE_SGT
: /* set on greater */
1211 GLfloat a
[4], b
[4], result
[4];
1212 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1213 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1214 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1215 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1216 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1217 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1218 store_vector4(inst
, machine
, result
);
1220 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1221 result
[0], result
[1], result
[2], result
[3],
1222 a
[0], a
[1], a
[2], a
[3],
1223 b
[0], b
[1], b
[2], b
[3]);
1229 GLfloat a
[4], result
[4];
1230 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1231 result
[0] = result
[1] = result
[2] = result
[3]
1232 = (GLfloat
) _mesa_sin(a
[0]);
1233 store_vector4(inst
, machine
, result
);
1236 case OPCODE_SLE
: /* set on less or equal */
1238 GLfloat a
[4], b
[4], result
[4];
1239 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1240 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1241 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1242 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1243 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1244 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1245 store_vector4(inst
, machine
, result
);
1247 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1248 result
[0], result
[1], result
[2], result
[3],
1249 a
[0], a
[1], a
[2], a
[3],
1250 b
[0], b
[1], b
[2], b
[3]);
1254 case OPCODE_SLT
: /* set on less */
1256 GLfloat a
[4], b
[4], result
[4];
1257 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1258 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1259 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1260 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1261 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1262 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1263 store_vector4(inst
, machine
, result
);
1265 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1266 result
[0], result
[1], result
[2], result
[3],
1267 a
[0], a
[1], a
[2], a
[3],
1268 b
[0], b
[1], b
[2], b
[3]);
1272 case OPCODE_SNE
: /* set on not equal */
1274 GLfloat a
[4], b
[4], result
[4];
1275 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1276 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1277 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1278 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1279 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1280 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1281 store_vector4(inst
, machine
, result
);
1283 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1284 result
[0], result
[1], result
[2], result
[3],
1285 a
[0], a
[1], a
[2], a
[3],
1286 b
[0], b
[1], b
[2], b
[3]);
1290 case OPCODE_STR
: /* set true, operands ignored */
1292 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1293 store_vector4(inst
, machine
, result
);
1298 GLfloat a
[4], b
[4], result
[4];
1299 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1300 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1301 result
[0] = a
[0] - b
[0];
1302 result
[1] = a
[1] - b
[1];
1303 result
[2] = a
[2] - b
[2];
1304 result
[3] = a
[3] - b
[3];
1305 store_vector4(inst
, machine
, result
);
1307 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1308 result
[0], result
[1], result
[2], result
[3],
1309 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1313 case OPCODE_SWZ
: /* extended swizzle */
1315 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1316 const GLfloat
*src
= get_register_pointer(source
, machine
);
1319 for (i
= 0; i
< 4; i
++) {
1320 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1321 if (swz
== SWIZZLE_ZERO
)
1323 else if (swz
== SWIZZLE_ONE
)
1328 result
[i
] = src
[swz
];
1330 if (source
->NegateBase
& (1 << i
))
1331 result
[i
] = -result
[i
];
1333 store_vector4(inst
, machine
, result
);
1336 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1337 /* Simple texel lookup */
1339 GLfloat texcoord
[4], color
[4];
1340 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1342 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1345 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1346 color
[0], color
[1], color
[2], color
[3],
1348 texcoord
[0], texcoord
[1], texcoord
[2], texcoord
[3]);
1350 store_vector4(inst
, machine
, color
);
1353 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1354 /* Texel lookup with LOD bias */
1356 const struct gl_texture_unit
*texUnit
1357 = &ctx
->Texture
.Unit
[inst
->TexSrcUnit
];
1358 GLfloat texcoord
[4], color
[4], lodBias
;
1360 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1362 /* texcoord[3] is the bias to add to lambda */
1363 lodBias
= texUnit
->LodBias
+ texcoord
[3];
1364 if (texUnit
->_Current
) {
1365 lodBias
+= texUnit
->_Current
->LodBias
;
1368 fetch_texel(ctx
, machine
, inst
, texcoord
, lodBias
, color
);
1370 store_vector4(inst
, machine
, color
);
1373 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1374 /* Texture lookup w/ partial derivatives for LOD */
1376 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1377 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1378 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1379 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1380 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1382 inst
->TexSrcUnit
, color
);
1383 store_vector4(inst
, machine
, color
);
1386 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1387 /* Texture lookup w/ projective divide */
1389 GLfloat texcoord
[4], color
[4];
1391 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1392 /* Not so sure about this test - if texcoord[3] is
1393 * zero, we'd probably be fine except for an ASSERT in
1394 * IROUND_POS() which gets triggered by the inf values created.
1396 if (texcoord
[3] != 0.0) {
1397 texcoord
[0] /= texcoord
[3];
1398 texcoord
[1] /= texcoord
[3];
1399 texcoord
[2] /= texcoord
[3];
1402 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1404 store_vector4(inst
, machine
, color
);
1407 case OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1408 /* Texture lookup w/ projective divide, as above, but do not
1409 * do the divide by w if sampling from a cube map.
1412 GLfloat texcoord
[4], color
[4];
1414 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1415 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1416 texcoord
[3] != 0.0) {
1417 texcoord
[0] /= texcoord
[3];
1418 texcoord
[1] /= texcoord
[3];
1419 texcoord
[2] /= texcoord
[3];
1422 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1424 store_vector4(inst
, machine
, color
);
1427 case OPCODE_UP2H
: /* unpack two 16-bit floats */
1429 GLfloat a
[4], result
[4];
1430 const GLuint
*rawBits
= (const GLuint
*) a
;
1432 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1433 hx
= rawBits
[0] & 0xffff;
1434 hy
= rawBits
[0] >> 16;
1435 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1436 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1437 store_vector4(inst
, machine
, result
);
1440 case OPCODE_UP2US
: /* unpack two GLushorts */
1442 GLfloat a
[4], result
[4];
1443 const GLuint
*rawBits
= (const GLuint
*) a
;
1445 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1446 usx
= rawBits
[0] & 0xffff;
1447 usy
= rawBits
[0] >> 16;
1448 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1449 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1450 store_vector4(inst
, machine
, result
);
1453 case OPCODE_UP4B
: /* unpack four GLbytes */
1455 GLfloat a
[4], result
[4];
1456 const GLuint
*rawBits
= (const GLuint
*) a
;
1457 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1458 result
[0] = (((rawBits
[0] >> 0) & 0xff) - 128) / 127.0F
;
1459 result
[1] = (((rawBits
[0] >> 8) & 0xff) - 128) / 127.0F
;
1460 result
[2] = (((rawBits
[0] >> 16) & 0xff) - 128) / 127.0F
;
1461 result
[3] = (((rawBits
[0] >> 24) & 0xff) - 128) / 127.0F
;
1462 store_vector4(inst
, machine
, result
);
1465 case OPCODE_UP4UB
: /* unpack four GLubytes */
1467 GLfloat a
[4], result
[4];
1468 const GLuint
*rawBits
= (const GLuint
*) a
;
1469 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1470 result
[0] = ((rawBits
[0] >> 0) & 0xff) / 255.0F
;
1471 result
[1] = ((rawBits
[0] >> 8) & 0xff) / 255.0F
;
1472 result
[2] = ((rawBits
[0] >> 16) & 0xff) / 255.0F
;
1473 result
[3] = ((rawBits
[0] >> 24) & 0xff) / 255.0F
;
1474 store_vector4(inst
, machine
, result
);
1477 case OPCODE_XPD
: /* cross product */
1479 GLfloat a
[4], b
[4], result
[4];
1480 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1481 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1482 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1483 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1484 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1486 store_vector4(inst
, machine
, result
);
1488 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1489 result
[0], result
[1], result
[2], result
[3],
1490 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1494 case OPCODE_X2D
: /* 2-D matrix transform */
1496 GLfloat a
[4], b
[4], c
[4], result
[4];
1497 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1498 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1499 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1500 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1501 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1502 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1503 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1504 store_vector4(inst
, machine
, result
);
1509 if (inst
->SrcReg
[0].File
!= -1) {
1511 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1512 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst
->Data
,
1513 a
[0], a
[1], a
[2], a
[3]);
1516 _mesa_printf("%s\n", (const char *) inst
->Data
);
1523 _mesa_problem(ctx
, "Bad opcode %d in _mesa_execute_program",
1526 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
;