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_INT
: /* float to int */
795 GLfloat a
[4], result
[4];
796 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
797 result
[0] = (GLfloat
) (GLint
) a
[0];
798 result
[1] = (GLfloat
) (GLint
) a
[1];
799 result
[2] = (GLfloat
) (GLint
) a
[2];
800 result
[3] = (GLfloat
) (GLint
) a
[3];
801 store_vector4(inst
, machine
, result
);
804 case OPCODE_KIL_NV
: /* NV_f_p only (conditional) */
805 if (eval_condition(machine
, inst
)) {
809 case OPCODE_KIL
: /* ARB_f_p only */
812 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
813 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
818 case OPCODE_LG2
: /* log base 2 */
820 GLfloat a
[4], result
[4];
821 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
822 result
[0] = result
[1] = result
[2] = result
[3] = LOG2(a
[0]);
823 store_vector4(inst
, machine
, result
);
828 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
829 GLfloat a
[4], result
[4];
830 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
831 a
[0] = MAX2(a
[0], 0.0F
);
832 a
[1] = MAX2(a
[1], 0.0F
);
833 /* XXX ARB version clamps a[3], NV version doesn't */
834 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
837 /* XXX we could probably just use pow() here */
839 if (a
[1] == 0.0 && a
[3] == 0.0)
842 result
[2] = EXPF(a
[3] * LOGF(a
[1]));
848 store_vector4(inst
, machine
, result
);
850 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
851 result
[0], result
[1], result
[2], result
[3],
852 a
[0], a
[1], a
[2], a
[3]);
858 GLfloat t
[4], q
[4], abs_t0
;
859 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
860 abs_t0
= FABSF(t
[0]);
861 if (abs_t0
!= 0.0F
) {
862 /* Since we really can't handle infinite values on VMS
863 * like other OSes we'll use __MAXFLOAT to represent
864 * infinity. This may need some tweaking.
867 if (abs_t0
== __MAXFLOAT
)
869 if (IS_INF_OR_NAN(abs_t0
))
872 SET_POS_INFINITY(q
[0]);
874 SET_POS_INFINITY(q
[2]);
878 GLfloat mantissa
= FREXPF(t
[0], &exponent
);
879 q
[0] = (GLfloat
) (exponent
- 1);
880 q
[1] = (GLfloat
) (2.0 * mantissa
); /* map [.5, 1) -> [1, 2) */
881 q
[2] = (GLfloat
) (q
[0] + LOG2(q
[1]));
885 SET_NEG_INFINITY(q
[0]);
887 SET_NEG_INFINITY(q
[2]);
890 store_vector4(inst
, machine
, q
);
895 GLfloat a
[4], b
[4], c
[4], result
[4];
896 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
897 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
898 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
899 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
900 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
901 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
902 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
903 store_vector4(inst
, machine
, result
);
905 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
906 "(%g %g %g %g), (%g %g %g %g)\n",
907 result
[0], result
[1], result
[2], result
[3],
908 a
[0], a
[1], a
[2], a
[3],
909 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
915 GLfloat a
[4], b
[4], c
[4], result
[4];
916 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
917 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
918 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
919 result
[0] = a
[0] * b
[0] + c
[0];
920 result
[1] = a
[1] * b
[1] + c
[1];
921 result
[2] = a
[2] * b
[2] + c
[2];
922 result
[3] = a
[3] * b
[3] + c
[3];
923 store_vector4(inst
, machine
, result
);
925 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
926 "(%g %g %g %g) + (%g %g %g %g)\n",
927 result
[0], result
[1], result
[2], result
[3],
928 a
[0], a
[1], a
[2], a
[3],
929 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
935 GLfloat a
[4], b
[4], result
[4];
936 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
937 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
938 result
[0] = MAX2(a
[0], b
[0]);
939 result
[1] = MAX2(a
[1], b
[1]);
940 result
[2] = MAX2(a
[2], b
[2]);
941 result
[3] = MAX2(a
[3], b
[3]);
942 store_vector4(inst
, machine
, result
);
944 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
945 result
[0], result
[1], result
[2], result
[3],
946 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
952 GLfloat a
[4], b
[4], result
[4];
953 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
954 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
955 result
[0] = MIN2(a
[0], b
[0]);
956 result
[1] = MIN2(a
[1], b
[1]);
957 result
[2] = MIN2(a
[2], b
[2]);
958 result
[3] = MIN2(a
[3], b
[3]);
959 store_vector4(inst
, machine
, result
);
965 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
966 store_vector4(inst
, machine
, result
);
968 printf("MOV (%g %g %g %g)\n",
969 result
[0], result
[1], result
[2], result
[3]);
975 GLfloat a
[4], b
[4], result
[4];
976 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
977 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
978 result
[0] = a
[0] * b
[0];
979 result
[1] = a
[1] * b
[1];
980 result
[2] = a
[2] * b
[2];
981 result
[3] = a
[3] * b
[3];
982 store_vector4(inst
, machine
, result
);
984 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
985 result
[0], result
[1], result
[2], result
[3],
986 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
992 GLfloat a
[4], result
[4];
993 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
996 result
[2] = result
[3] = _slang_library_noise1(a
[0]);
997 store_vector4(inst
, machine
, result
);
1002 GLfloat a
[4], result
[4];
1003 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1006 result
[2] = result
[3] = _slang_library_noise2(a
[0], a
[1]);
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_noise3(a
[0], a
[1], a
[2]);
1018 store_vector4(inst
, machine
, result
);
1023 GLfloat a
[4], result
[4];
1024 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1028 result
[3] = _slang_library_noise4(a
[0], a
[1], a
[2], a
[3]);
1029 store_vector4(inst
, machine
, result
);
1034 case OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
1036 GLfloat a
[4], result
[4];
1038 GLuint
*rawResult
= (GLuint
*) result
;
1040 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1041 hx
= _mesa_float_to_half(a
[0]);
1042 hy
= _mesa_float_to_half(a
[1]);
1043 twoHalves
= hx
| (hy
<< 16);
1044 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1046 store_vector4(inst
, machine
, result
);
1049 case OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
1051 GLfloat a
[4], result
[4];
1052 GLuint usx
, usy
, *rawResult
= (GLuint
*) result
;
1053 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1054 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1055 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1056 usx
= IROUND(a
[0] * 65535.0F
);
1057 usy
= IROUND(a
[1] * 65535.0F
);
1058 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1059 = usx
| (usy
<< 16);
1060 store_vector4(inst
, machine
, result
);
1063 case OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
1065 GLfloat a
[4], result
[4];
1066 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
1067 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1068 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
1069 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
1070 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
1071 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
1072 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
1073 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
1074 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
1075 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
1076 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1077 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1078 store_vector4(inst
, machine
, result
);
1081 case OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
1083 GLfloat a
[4], result
[4];
1084 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
1085 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1086 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1087 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1088 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
1089 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
1090 ubx
= IROUND(255.0F
* a
[0]);
1091 uby
= IROUND(255.0F
* a
[1]);
1092 ubz
= IROUND(255.0F
* a
[2]);
1093 ubw
= IROUND(255.0F
* a
[3]);
1094 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1095 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1096 store_vector4(inst
, machine
, result
);
1101 GLfloat a
[4], b
[4], result
[4];
1102 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1103 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
1104 result
[0] = result
[1] = result
[2] = result
[3]
1105 = (GLfloat
) _mesa_pow(a
[0], b
[0]);
1106 store_vector4(inst
, machine
, result
);
1111 GLfloat a
[4], result
[4];
1112 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1116 else if (IS_INF_OR_NAN(a
[0]))
1117 printf("RCP(inf)\n");
1119 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
1120 store_vector4(inst
, machine
, result
);
1123 case OPCODE_RET
: /* return from subroutine (conditional) */
1124 if (eval_condition(machine
, inst
)) {
1125 if (machine
->StackDepth
== 0) {
1126 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
1128 /* subtract one because of pc++ in the for loop */
1129 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
1132 case OPCODE_RFL
: /* reflection vector */
1134 GLfloat axis
[4], dir
[4], result
[4], tmpX
, tmpW
;
1135 fetch_vector4(&inst
->SrcReg
[0], machine
, axis
);
1136 fetch_vector4(&inst
->SrcReg
[1], machine
, dir
);
1137 tmpW
= DOT3(axis
, axis
);
1138 tmpX
= (2.0F
* DOT3(axis
, dir
)) / tmpW
;
1139 result
[0] = tmpX
* axis
[0] - dir
[0];
1140 result
[1] = tmpX
* axis
[1] - dir
[1];
1141 result
[2] = tmpX
* axis
[2] - dir
[2];
1142 /* result[3] is never written! XXX enforce in parser! */
1143 store_vector4(inst
, machine
, result
);
1146 case OPCODE_RSQ
: /* 1 / sqrt() */
1148 GLfloat a
[4], result
[4];
1149 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1151 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1152 store_vector4(inst
, machine
, result
);
1154 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
1158 case OPCODE_SCS
: /* sine and cos */
1160 GLfloat a
[4], result
[4];
1161 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1162 result
[0] = (GLfloat
) _mesa_cos(a
[0]);
1163 result
[1] = (GLfloat
) _mesa_sin(a
[0]);
1164 result
[2] = 0.0; /* undefined! */
1165 result
[3] = 0.0; /* undefined! */
1166 store_vector4(inst
, machine
, result
);
1169 case OPCODE_SEQ
: /* set on equal */
1171 GLfloat a
[4], b
[4], result
[4];
1172 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1173 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1174 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1175 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1176 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1177 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1178 store_vector4(inst
, machine
, result
);
1180 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1181 result
[0], result
[1], result
[2], result
[3],
1182 a
[0], a
[1], a
[2], a
[3],
1183 b
[0], b
[1], b
[2], b
[3]);
1187 case OPCODE_SFL
: /* set false, operands ignored */
1189 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1190 store_vector4(inst
, machine
, result
);
1193 case OPCODE_SGE
: /* set on greater or equal */
1195 GLfloat a
[4], b
[4], result
[4];
1196 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1197 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1198 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1199 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1200 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1201 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1202 store_vector4(inst
, machine
, result
);
1204 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1205 result
[0], result
[1], result
[2], result
[3],
1206 a
[0], a
[1], a
[2], a
[3],
1207 b
[0], b
[1], b
[2], b
[3]);
1211 case OPCODE_SGT
: /* set on greater */
1213 GLfloat a
[4], b
[4], result
[4];
1214 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1215 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1216 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1217 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1218 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1219 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1220 store_vector4(inst
, machine
, result
);
1222 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1223 result
[0], result
[1], result
[2], result
[3],
1224 a
[0], a
[1], a
[2], a
[3],
1225 b
[0], b
[1], b
[2], b
[3]);
1231 GLfloat a
[4], result
[4];
1232 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1233 result
[0] = result
[1] = result
[2] = result
[3]
1234 = (GLfloat
) _mesa_sin(a
[0]);
1235 store_vector4(inst
, machine
, result
);
1238 case OPCODE_SLE
: /* set on less or equal */
1240 GLfloat a
[4], b
[4], result
[4];
1241 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1242 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1243 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1244 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1245 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1246 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1247 store_vector4(inst
, machine
, result
);
1249 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1250 result
[0], result
[1], result
[2], result
[3],
1251 a
[0], a
[1], a
[2], a
[3],
1252 b
[0], b
[1], b
[2], b
[3]);
1256 case OPCODE_SLT
: /* set on less */
1258 GLfloat a
[4], b
[4], result
[4];
1259 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1260 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1261 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1262 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1263 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1264 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1265 store_vector4(inst
, machine
, result
);
1267 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1268 result
[0], result
[1], result
[2], result
[3],
1269 a
[0], a
[1], a
[2], a
[3],
1270 b
[0], b
[1], b
[2], b
[3]);
1274 case OPCODE_SNE
: /* set on not equal */
1276 GLfloat a
[4], b
[4], result
[4];
1277 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1278 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1279 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1280 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1281 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1282 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1283 store_vector4(inst
, machine
, result
);
1285 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1286 result
[0], result
[1], result
[2], result
[3],
1287 a
[0], a
[1], a
[2], a
[3],
1288 b
[0], b
[1], b
[2], b
[3]);
1292 case OPCODE_STR
: /* set true, operands ignored */
1294 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1295 store_vector4(inst
, machine
, result
);
1300 GLfloat a
[4], b
[4], result
[4];
1301 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1302 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1303 result
[0] = a
[0] - b
[0];
1304 result
[1] = a
[1] - b
[1];
1305 result
[2] = a
[2] - b
[2];
1306 result
[3] = a
[3] - b
[3];
1307 store_vector4(inst
, machine
, result
);
1309 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1310 result
[0], result
[1], result
[2], result
[3],
1311 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1315 case OPCODE_SWZ
: /* extended swizzle */
1317 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1318 const GLfloat
*src
= get_register_pointer(source
, machine
);
1321 for (i
= 0; i
< 4; i
++) {
1322 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1323 if (swz
== SWIZZLE_ZERO
)
1325 else if (swz
== SWIZZLE_ONE
)
1330 result
[i
] = src
[swz
];
1332 if (source
->NegateBase
& (1 << i
))
1333 result
[i
] = -result
[i
];
1335 store_vector4(inst
, machine
, result
);
1338 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1339 /* Simple texel lookup */
1341 GLfloat texcoord
[4], color
[4];
1342 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1344 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1347 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1348 color
[0], color
[1], color
[2], color
[3],
1350 texcoord
[0], texcoord
[1], texcoord
[2], texcoord
[3]);
1352 store_vector4(inst
, machine
, color
);
1355 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1356 /* Texel lookup with LOD bias */
1358 const struct gl_texture_unit
*texUnit
1359 = &ctx
->Texture
.Unit
[inst
->TexSrcUnit
];
1360 GLfloat texcoord
[4], color
[4], lodBias
;
1362 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1364 /* texcoord[3] is the bias to add to lambda */
1365 lodBias
= texUnit
->LodBias
+ texcoord
[3];
1366 if (texUnit
->_Current
) {
1367 lodBias
+= texUnit
->_Current
->LodBias
;
1370 fetch_texel(ctx
, machine
, inst
, texcoord
, lodBias
, color
);
1372 store_vector4(inst
, machine
, color
);
1375 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1376 /* Texture lookup w/ partial derivatives for LOD */
1378 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1379 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1380 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1381 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1382 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1384 inst
->TexSrcUnit
, color
);
1385 store_vector4(inst
, machine
, color
);
1388 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1389 /* Texture lookup w/ projective divide */
1391 GLfloat texcoord
[4], color
[4];
1393 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1394 /* Not so sure about this test - if texcoord[3] is
1395 * zero, we'd probably be fine except for an ASSERT in
1396 * IROUND_POS() which gets triggered by the inf values created.
1398 if (texcoord
[3] != 0.0) {
1399 texcoord
[0] /= texcoord
[3];
1400 texcoord
[1] /= texcoord
[3];
1401 texcoord
[2] /= texcoord
[3];
1404 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1406 store_vector4(inst
, machine
, color
);
1409 case OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1410 /* Texture lookup w/ projective divide, as above, but do not
1411 * do the divide by w if sampling from a cube map.
1414 GLfloat texcoord
[4], color
[4];
1416 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1417 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1418 texcoord
[3] != 0.0) {
1419 texcoord
[0] /= texcoord
[3];
1420 texcoord
[1] /= texcoord
[3];
1421 texcoord
[2] /= texcoord
[3];
1424 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1426 store_vector4(inst
, machine
, color
);
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
;