2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * 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_src_register_pointer(const struct prog_src_register
*source
,
80 const struct gl_program_machine
*machine
)
82 const struct gl_program
*prog
= machine
->CurProgram
;
83 GLint reg
= source
->Index
;
85 if (source
->RelAddr
) {
86 /* add address register value to src index/offset */
87 reg
+= machine
->AddressReg
[0][0];
93 switch (source
->File
) {
94 case PROGRAM_TEMPORARY
:
95 if (reg
>= MAX_PROGRAM_TEMPS
)
97 return machine
->Temporaries
[reg
];
100 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
101 if (reg
>= VERT_ATTRIB_MAX
)
103 return machine
->VertAttribs
[reg
];
106 if (reg
>= FRAG_ATTRIB_MAX
)
108 return machine
->Attribs
[reg
][machine
->CurElement
];
112 if (reg
>= MAX_PROGRAM_OUTPUTS
)
114 return machine
->Outputs
[reg
];
116 case PROGRAM_LOCAL_PARAM
:
117 if (reg
>= MAX_PROGRAM_LOCAL_PARAMS
)
119 return machine
->CurProgram
->LocalParams
[reg
];
121 case PROGRAM_ENV_PARAM
:
122 if (reg
>= MAX_PROGRAM_ENV_PARAMS
)
124 return machine
->EnvParams
[reg
];
126 case PROGRAM_STATE_VAR
:
128 case PROGRAM_CONSTANT
:
130 case PROGRAM_UNIFORM
:
132 case PROGRAM_NAMED_PARAM
:
133 if (reg
>= (GLint
) prog
->Parameters
->NumParameters
)
135 return prog
->Parameters
->ParameterValues
[reg
];
139 "Invalid src register file %d in get_src_register_pointer()",
147 * Return a pointer to the 4-element float vector specified by the given
148 * destination register.
150 static INLINE GLfloat
*
151 get_dst_register_pointer(const struct prog_dst_register
*dest
,
152 struct gl_program_machine
*machine
)
154 static GLfloat dummyReg
[4];
155 GLint reg
= dest
->Index
;
158 /* add address register value to src index/offset */
159 reg
+= machine
->AddressReg
[0][0];
165 switch (dest
->File
) {
166 case PROGRAM_TEMPORARY
:
167 if (reg
>= MAX_PROGRAM_TEMPS
)
169 return machine
->Temporaries
[reg
];
172 if (reg
>= MAX_PROGRAM_OUTPUTS
)
174 return machine
->Outputs
[reg
];
176 case PROGRAM_WRITE_ONLY
:
181 "Invalid dest register file %d in get_dst_register_pointer()",
189 #if FEATURE_MESA_program_debug
190 static struct gl_program_machine
*CurrentMachine
= NULL
;
193 * For GL_MESA_program_debug.
194 * Return current value (4*GLfloat) of a program register.
195 * Called via ctx->Driver.GetProgramRegister().
198 _mesa_get_program_register(GLcontext
*ctx
, enum register_file file
,
199 GLuint index
, GLfloat val
[4])
201 if (CurrentMachine
) {
202 struct prog_src_register srcReg
;
205 srcReg
.Index
= index
;
206 src
= get_src_register_pointer(&srcReg
, CurrentMachine
);
210 #endif /* FEATURE_MESA_program_debug */
214 * Fetch a 4-element float vector from the given source register.
215 * Apply swizzling and negating as needed.
218 fetch_vector4(const struct prog_src_register
*source
,
219 const struct gl_program_machine
*machine
, GLfloat result
[4])
221 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
224 if (source
->Swizzle
== SWIZZLE_NOOP
) {
226 COPY_4V(result
, src
);
229 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
230 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
231 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
232 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
233 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
234 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
235 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
236 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
239 if (source
->NegateBase
) {
240 result
[0] = -result
[0];
241 result
[1] = -result
[1];
242 result
[2] = -result
[2];
243 result
[3] = -result
[3];
246 result
[0] = FABSF(result
[0]);
247 result
[1] = FABSF(result
[1]);
248 result
[2] = FABSF(result
[2]);
249 result
[3] = FABSF(result
[3]);
251 if (source
->NegateAbs
) {
252 result
[0] = -result
[0];
253 result
[1] = -result
[1];
254 result
[2] = -result
[2];
255 result
[3] = -result
[3];
261 * Fetch the derivative with respect to X or Y for the given register.
262 * XXX this currently only works for fragment program input attribs.
265 fetch_vector4_deriv(GLcontext
* ctx
,
266 const struct prog_src_register
*source
,
267 const struct gl_program_machine
*machine
,
268 char xOrY
, GLfloat result
[4])
270 if (source
->File
== PROGRAM_INPUT
&& source
->Index
< (GLint
)machine
->NumDeriv
) {
271 const GLint col
= machine
->CurElement
;
272 const GLfloat w
= machine
->Attribs
[FRAG_ATTRIB_WPOS
][col
][3];
273 const GLfloat invQ
= 1.0f
/ w
;
277 deriv
[0] = machine
->DerivX
[source
->Index
][0] * invQ
;
278 deriv
[1] = machine
->DerivX
[source
->Index
][1] * invQ
;
279 deriv
[2] = machine
->DerivX
[source
->Index
][2] * invQ
;
280 deriv
[3] = machine
->DerivX
[source
->Index
][3] * invQ
;
283 deriv
[0] = machine
->DerivY
[source
->Index
][0] * invQ
;
284 deriv
[1] = machine
->DerivY
[source
->Index
][1] * invQ
;
285 deriv
[2] = machine
->DerivY
[source
->Index
][2] * invQ
;
286 deriv
[3] = machine
->DerivY
[source
->Index
][3] * invQ
;
289 result
[0] = deriv
[GET_SWZ(source
->Swizzle
, 0)];
290 result
[1] = deriv
[GET_SWZ(source
->Swizzle
, 1)];
291 result
[2] = deriv
[GET_SWZ(source
->Swizzle
, 2)];
292 result
[3] = deriv
[GET_SWZ(source
->Swizzle
, 3)];
294 if (source
->NegateBase
) {
295 result
[0] = -result
[0];
296 result
[1] = -result
[1];
297 result
[2] = -result
[2];
298 result
[3] = -result
[3];
301 result
[0] = FABSF(result
[0]);
302 result
[1] = FABSF(result
[1]);
303 result
[2] = FABSF(result
[2]);
304 result
[3] = FABSF(result
[3]);
306 if (source
->NegateAbs
) {
307 result
[0] = -result
[0];
308 result
[1] = -result
[1];
309 result
[2] = -result
[2];
310 result
[3] = -result
[3];
314 ASSIGN_4V(result
, 0.0, 0.0, 0.0, 0.0);
320 * As above, but only return result[0] element.
323 fetch_vector1(const struct prog_src_register
*source
,
324 const struct gl_program_machine
*machine
, GLfloat result
[4])
326 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
329 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
331 if (source
->NegateBase
) {
332 result
[0] = -result
[0];
335 result
[0] = FABSF(result
[0]);
337 if (source
->NegateAbs
) {
338 result
[0] = -result
[0];
344 * Fetch texel from texture. Use partial derivatives when possible.
347 fetch_texel(GLcontext
*ctx
,
348 const struct gl_program_machine
*machine
,
349 const struct prog_instruction
*inst
,
350 const GLfloat texcoord
[4], GLfloat lodBias
,
353 const GLuint unit
= machine
->Samplers
[inst
->TexSrcUnit
];
355 /* Note: we only have the right derivatives for fragment input attribs.
357 if (machine
->NumDeriv
> 0 &&
358 inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
359 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
) {
360 /* simple texture fetch for which we should have derivatives */
361 GLuint attr
= inst
->SrcReg
[0].Index
;
362 machine
->FetchTexelDeriv(ctx
, texcoord
,
363 machine
->DerivX
[attr
],
364 machine
->DerivY
[attr
],
365 lodBias
, unit
, color
);
368 machine
->FetchTexelLod(ctx
, texcoord
, lodBias
, unit
, color
);
374 * Test value against zero and return GT, LT, EQ or UN if NaN.
377 generate_cc(float value
)
380 return COND_UN
; /* NaN */
390 * Test if the ccMaskRule is satisfied by the given condition code.
391 * Used to mask destination writes according to the current condition code.
393 static INLINE GLboolean
394 test_cc(GLuint condCode
, GLuint ccMaskRule
)
396 switch (ccMaskRule
) {
397 case COND_EQ
: return (condCode
== COND_EQ
);
398 case COND_NE
: return (condCode
!= COND_EQ
);
399 case COND_LT
: return (condCode
== COND_LT
);
400 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
401 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
402 case COND_GT
: return (condCode
== COND_GT
);
403 case COND_TR
: return GL_TRUE
;
404 case COND_FL
: return GL_FALSE
;
405 default: return GL_TRUE
;
411 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
412 * or GL_FALSE to indicate result.
414 static INLINE GLboolean
415 eval_condition(const struct gl_program_machine
*machine
,
416 const struct prog_instruction
*inst
)
418 const GLuint swizzle
= inst
->DstReg
.CondSwizzle
;
419 const GLuint condMask
= inst
->DstReg
.CondMask
;
420 if (test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 0)], condMask
) ||
421 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 1)], condMask
) ||
422 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 2)], condMask
) ||
423 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 3)], condMask
)) {
434 * Store 4 floats into a register. Observe the instructions saturate and
435 * set-condition-code flags.
438 store_vector4(const struct prog_instruction
*inst
,
439 struct gl_program_machine
*machine
, const GLfloat value
[4])
441 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
442 const GLboolean clamp
= inst
->SaturateMode
== SATURATE_ZERO_ONE
;
443 GLuint writeMask
= dstReg
->WriteMask
;
444 GLfloat clampedValue
[4];
445 GLfloat
*dst
= get_dst_register_pointer(dstReg
, machine
);
448 if (value
[0] > 1.0e10
||
449 IS_INF_OR_NAN(value
[0]) ||
450 IS_INF_OR_NAN(value
[1]) ||
451 IS_INF_OR_NAN(value
[2]) || IS_INF_OR_NAN(value
[3]))
452 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
456 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
457 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
458 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
459 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
460 value
= clampedValue
;
463 if (dstReg
->CondMask
!= COND_TR
) {
464 /* condition codes may turn off some writes */
465 if (writeMask
& WRITEMASK_X
) {
466 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
468 writeMask
&= ~WRITEMASK_X
;
470 if (writeMask
& WRITEMASK_Y
) {
471 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
473 writeMask
&= ~WRITEMASK_Y
;
475 if (writeMask
& WRITEMASK_Z
) {
476 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
478 writeMask
&= ~WRITEMASK_Z
;
480 if (writeMask
& WRITEMASK_W
) {
481 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
483 writeMask
&= ~WRITEMASK_W
;
487 if (writeMask
& WRITEMASK_X
)
489 if (writeMask
& WRITEMASK_Y
)
491 if (writeMask
& WRITEMASK_Z
)
493 if (writeMask
& WRITEMASK_W
)
496 if (inst
->CondUpdate
) {
497 if (writeMask
& WRITEMASK_X
)
498 machine
->CondCodes
[0] = generate_cc(value
[0]);
499 if (writeMask
& WRITEMASK_Y
)
500 machine
->CondCodes
[1] = generate_cc(value
[1]);
501 if (writeMask
& WRITEMASK_Z
)
502 machine
->CondCodes
[2] = generate_cc(value
[2]);
503 if (writeMask
& WRITEMASK_W
)
504 machine
->CondCodes
[3] = generate_cc(value
[3]);
506 printf("CondCodes=(%s,%s,%s,%s) for:\n",
507 _mesa_condcode_string(machine
->CondCodes
[0]),
508 _mesa_condcode_string(machine
->CondCodes
[1]),
509 _mesa_condcode_string(machine
->CondCodes
[2]),
510 _mesa_condcode_string(machine
->CondCodes
[3]));
517 * Execute the given vertex/fragment program.
519 * \param ctx rendering context
520 * \param program the program to execute
521 * \param machine machine state (must be initialized)
522 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
525 _mesa_execute_program(GLcontext
* ctx
,
526 const struct gl_program
*program
,
527 struct gl_program_machine
*machine
)
529 const GLuint numInst
= program
->NumInstructions
;
530 const GLuint maxExec
= 10000;
531 GLuint pc
, numExec
= 0;
533 machine
->CurProgram
= program
;
536 printf("execute program %u --------------------\n", program
->Id
);
539 #if FEATURE_MESA_program_debug
540 CurrentMachine
= machine
;
543 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
544 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
547 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
550 for (pc
= 0; pc
< numInst
; pc
++) {
551 const struct prog_instruction
*inst
= program
->Instructions
+ pc
;
553 #if FEATURE_MESA_program_debug
554 if (ctx
->FragmentProgram
.CallbackEnabled
&&
555 ctx
->FragmentProgram
.Callback
) {
556 ctx
->FragmentProgram
.CurrentPosition
= inst
->StringPos
;
557 ctx
->FragmentProgram
.Callback(program
->Target
,
558 ctx
->FragmentProgram
.CallbackData
);
563 _mesa_print_instruction(inst
);
566 switch (inst
->Opcode
) {
569 GLfloat a
[4], result
[4];
570 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
571 result
[0] = FABSF(a
[0]);
572 result
[1] = FABSF(a
[1]);
573 result
[2] = FABSF(a
[2]);
574 result
[3] = FABSF(a
[3]);
575 store_vector4(inst
, machine
, result
);
580 GLfloat a
[4], b
[4], result
[4];
581 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
582 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
583 result
[0] = a
[0] + b
[0];
584 result
[1] = a
[1] + b
[1];
585 result
[2] = a
[2] + b
[2];
586 result
[3] = a
[3] + b
[3];
587 store_vector4(inst
, machine
, result
);
589 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
590 result
[0], result
[1], result
[2], result
[3],
591 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
598 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
599 machine
->AddressReg
[0][0] = (GLint
) FLOORF(t
[0]);
606 /* subtract 1 here since pc is incremented by for(pc) loop */
607 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
609 case OPCODE_BGNSUB
: /* begin subroutine */
611 case OPCODE_ENDSUB
: /* end subroutine */
613 case OPCODE_BRA
: /* branch (conditional) */
615 case OPCODE_BRK
: /* break out of loop (conditional) */
617 case OPCODE_CONT
: /* continue loop (conditional) */
618 if (eval_condition(machine
, inst
)) {
620 /* Subtract 1 here since we'll do pc++ at end of for-loop */
621 pc
= inst
->BranchTarget
- 1;
624 case OPCODE_CAL
: /* Call subroutine (conditional) */
625 if (eval_condition(machine
, inst
)) {
626 /* call the subroutine */
627 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
628 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
630 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1; /* next inst */
631 /* Subtract 1 here since we'll do pc++ at end of for-loop */
632 pc
= inst
->BranchTarget
- 1;
637 GLfloat a
[4], b
[4], c
[4], result
[4];
638 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
639 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
640 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
641 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
642 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
643 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
644 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
645 store_vector4(inst
, machine
, result
);
650 GLfloat a
[4], result
[4];
651 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
652 result
[0] = result
[1] = result
[2] = result
[3]
653 = (GLfloat
) _mesa_cos(a
[0]);
654 store_vector4(inst
, machine
, result
);
657 case OPCODE_DDX
: /* Partial derivative with respect to X */
660 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
662 store_vector4(inst
, machine
, result
);
665 case OPCODE_DDY
: /* Partial derivative with respect to Y */
668 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
670 store_vector4(inst
, machine
, result
);
675 GLfloat a
[4], b
[4], result
[4];
676 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
677 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
678 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
679 store_vector4(inst
, machine
, result
);
681 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
682 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
688 GLfloat a
[4], b
[4], result
[4];
689 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
690 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
691 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
, b
);
692 store_vector4(inst
, machine
, result
);
694 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
695 result
[0], a
[0], a
[1], a
[2], a
[3],
696 b
[0], b
[1], b
[2], b
[3]);
702 GLfloat a
[4], b
[4], result
[4];
703 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
704 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
705 result
[0] = result
[1] = result
[2] = result
[3] =
706 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + b
[3];
707 store_vector4(inst
, machine
, result
);
710 case OPCODE_DST
: /* Distance vector */
712 GLfloat a
[4], b
[4], result
[4];
713 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
714 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
716 result
[1] = a
[1] * b
[1];
719 store_vector4(inst
, machine
, result
);
724 GLfloat t
[4], q
[4], floor_t0
;
725 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
726 floor_t0
= FLOORF(t
[0]);
727 if (floor_t0
> FLT_MAX_EXP
) {
728 SET_POS_INFINITY(q
[0]);
729 SET_POS_INFINITY(q
[2]);
731 else if (floor_t0
< FLT_MIN_EXP
) {
736 q
[0] = LDEXPF(1.0, (int) floor_t0
);
737 /* Note: GL_NV_vertex_program expects
738 * result.z = result.x * APPX(result.y)
739 * We do what the ARB extension says.
741 q
[2] = (GLfloat
) pow(2.0, t
[0]);
743 q
[1] = t
[0] - floor_t0
;
745 store_vector4( inst
, machine
, q
);
748 case OPCODE_EX2
: /* Exponential base 2 */
750 GLfloat a
[4], result
[4];
751 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
752 result
[0] = result
[1] = result
[2] = result
[3] =
753 (GLfloat
) _mesa_pow(2.0, a
[0]);
754 store_vector4(inst
, machine
, result
);
759 GLfloat a
[4], result
[4];
760 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
761 result
[0] = FLOORF(a
[0]);
762 result
[1] = FLOORF(a
[1]);
763 result
[2] = FLOORF(a
[2]);
764 result
[3] = FLOORF(a
[3]);
765 store_vector4(inst
, machine
, result
);
770 GLfloat a
[4], result
[4];
771 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
772 result
[0] = a
[0] - FLOORF(a
[0]);
773 result
[1] = a
[1] - FLOORF(a
[1]);
774 result
[2] = a
[2] - FLOORF(a
[2]);
775 result
[3] = a
[3] - FLOORF(a
[3]);
776 store_vector4(inst
, machine
, result
);
783 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
785 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
786 cond
= (a
[0] != 0.0);
789 cond
= eval_condition(machine
, inst
);
792 printf("IF: %d\n", cond
);
796 /* do if-clause (just continue execution) */
799 /* go to the instruction after ELSE or ENDIF */
800 assert(inst
->BranchTarget
>= 0);
801 pc
= inst
->BranchTarget
- 1;
807 assert(inst
->BranchTarget
>= 0);
808 pc
= inst
->BranchTarget
- 1;
813 case OPCODE_INT
: /* float to int */
815 GLfloat a
[4], result
[4];
816 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
817 result
[0] = (GLfloat
) (GLint
) a
[0];
818 result
[1] = (GLfloat
) (GLint
) a
[1];
819 result
[2] = (GLfloat
) (GLint
) a
[2];
820 result
[3] = (GLfloat
) (GLint
) a
[3];
821 store_vector4(inst
, machine
, result
);
824 case OPCODE_KIL_NV
: /* NV_f_p only (conditional) */
825 if (eval_condition(machine
, inst
)) {
829 case OPCODE_KIL
: /* ARB_f_p only */
832 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
833 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
838 case OPCODE_LG2
: /* log base 2 */
840 GLfloat a
[4], result
[4];
841 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
842 result
[0] = result
[1] = result
[2] = result
[3] = LOG2(a
[0]);
843 store_vector4(inst
, machine
, result
);
848 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
849 GLfloat a
[4], result
[4];
850 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
851 a
[0] = MAX2(a
[0], 0.0F
);
852 a
[1] = MAX2(a
[1], 0.0F
);
853 /* XXX ARB version clamps a[3], NV version doesn't */
854 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
857 /* XXX we could probably just use pow() here */
859 if (a
[1] == 0.0 && a
[3] == 0.0)
862 result
[2] = EXPF(a
[3] * LOGF(a
[1]));
868 store_vector4(inst
, machine
, result
);
870 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
871 result
[0], result
[1], result
[2], result
[3],
872 a
[0], a
[1], a
[2], a
[3]);
878 GLfloat t
[4], q
[4], abs_t0
;
879 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
880 abs_t0
= FABSF(t
[0]);
881 if (abs_t0
!= 0.0F
) {
882 /* Since we really can't handle infinite values on VMS
883 * like other OSes we'll use __MAXFLOAT to represent
884 * infinity. This may need some tweaking.
887 if (abs_t0
== __MAXFLOAT
)
889 if (IS_INF_OR_NAN(abs_t0
))
892 SET_POS_INFINITY(q
[0]);
894 SET_POS_INFINITY(q
[2]);
898 GLfloat mantissa
= FREXPF(t
[0], &exponent
);
899 q
[0] = (GLfloat
) (exponent
- 1);
900 q
[1] = (GLfloat
) (2.0 * mantissa
); /* map [.5, 1) -> [1, 2) */
901 q
[2] = (GLfloat
) (q
[0] + LOG2(q
[1]));
905 SET_NEG_INFINITY(q
[0]);
907 SET_NEG_INFINITY(q
[2]);
910 store_vector4(inst
, machine
, q
);
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] + (1.0F
- a
[0]) * c
[0];
920 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
921 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
922 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
923 store_vector4(inst
, machine
, result
);
925 printf("LRP (%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], c
[4], result
[4];
936 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
937 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
938 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
939 result
[0] = a
[0] * b
[0] + c
[0];
940 result
[1] = a
[1] * b
[1] + c
[1];
941 result
[2] = a
[2] * b
[2] + c
[2];
942 result
[3] = a
[3] * b
[3] + c
[3];
943 store_vector4(inst
, machine
, result
);
945 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
946 "(%g %g %g %g) + (%g %g %g %g)\n",
947 result
[0], result
[1], result
[2], result
[3],
948 a
[0], a
[1], a
[2], a
[3],
949 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
955 GLfloat a
[4], b
[4], result
[4];
956 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
957 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
958 result
[0] = MAX2(a
[0], b
[0]);
959 result
[1] = MAX2(a
[1], b
[1]);
960 result
[2] = MAX2(a
[2], b
[2]);
961 result
[3] = MAX2(a
[3], b
[3]);
962 store_vector4(inst
, machine
, result
);
964 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
965 result
[0], result
[1], result
[2], result
[3],
966 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
972 GLfloat a
[4], b
[4], result
[4];
973 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
974 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
975 result
[0] = MIN2(a
[0], b
[0]);
976 result
[1] = MIN2(a
[1], b
[1]);
977 result
[2] = MIN2(a
[2], b
[2]);
978 result
[3] = MIN2(a
[3], b
[3]);
979 store_vector4(inst
, machine
, result
);
985 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
986 store_vector4(inst
, machine
, result
);
988 printf("MOV (%g %g %g %g)\n",
989 result
[0], result
[1], result
[2], result
[3]);
995 GLfloat a
[4], b
[4], result
[4];
996 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
997 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
998 result
[0] = a
[0] * b
[0];
999 result
[1] = a
[1] * b
[1];
1000 result
[2] = a
[2] * b
[2];
1001 result
[3] = a
[3] * b
[3];
1002 store_vector4(inst
, machine
, result
);
1004 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1005 result
[0], result
[1], result
[2], result
[3],
1006 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1012 GLfloat a
[4], result
[4];
1013 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1016 result
[2] = result
[3] = _slang_library_noise1(a
[0]);
1017 store_vector4(inst
, machine
, result
);
1022 GLfloat a
[4], result
[4];
1023 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1026 result
[2] = result
[3] = _slang_library_noise2(a
[0], a
[1]);
1027 store_vector4(inst
, machine
, result
);
1032 GLfloat a
[4], result
[4];
1033 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1037 result
[3] = _slang_library_noise3(a
[0], a
[1], a
[2]);
1038 store_vector4(inst
, machine
, result
);
1043 GLfloat a
[4], result
[4];
1044 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1048 result
[3] = _slang_library_noise4(a
[0], a
[1], a
[2], a
[3]);
1049 store_vector4(inst
, machine
, result
);
1054 case OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
1056 GLfloat a
[4], result
[4];
1058 GLuint
*rawResult
= (GLuint
*) result
;
1060 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1061 hx
= _mesa_float_to_half(a
[0]);
1062 hy
= _mesa_float_to_half(a
[1]);
1063 twoHalves
= hx
| (hy
<< 16);
1064 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1066 store_vector4(inst
, machine
, result
);
1069 case OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
1071 GLfloat a
[4], result
[4];
1072 GLuint usx
, usy
, *rawResult
= (GLuint
*) result
;
1073 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1074 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1075 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1076 usx
= IROUND(a
[0] * 65535.0F
);
1077 usy
= IROUND(a
[1] * 65535.0F
);
1078 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1079 = usx
| (usy
<< 16);
1080 store_vector4(inst
, machine
, result
);
1083 case OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
1085 GLfloat a
[4], result
[4];
1086 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
1087 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1088 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
1089 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
1090 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
1091 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
1092 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
1093 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
1094 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
1095 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
1096 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1097 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1098 store_vector4(inst
, machine
, result
);
1101 case OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
1103 GLfloat a
[4], result
[4];
1104 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
1105 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1106 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1107 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1108 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
1109 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
1110 ubx
= IROUND(255.0F
* a
[0]);
1111 uby
= IROUND(255.0F
* a
[1]);
1112 ubz
= IROUND(255.0F
* a
[2]);
1113 ubw
= IROUND(255.0F
* a
[3]);
1114 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1115 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1116 store_vector4(inst
, machine
, result
);
1121 GLfloat a
[4], b
[4], result
[4];
1122 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1123 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
1124 result
[0] = result
[1] = result
[2] = result
[3]
1125 = (GLfloat
) _mesa_pow(a
[0], b
[0]);
1126 store_vector4(inst
, machine
, result
);
1131 GLfloat a
[4], result
[4];
1132 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1136 else if (IS_INF_OR_NAN(a
[0]))
1137 printf("RCP(inf)\n");
1139 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
1140 store_vector4(inst
, machine
, result
);
1143 case OPCODE_RET
: /* return from subroutine (conditional) */
1144 if (eval_condition(machine
, inst
)) {
1145 if (machine
->StackDepth
== 0) {
1146 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
1148 /* subtract one because of pc++ in the for loop */
1149 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
1152 case OPCODE_RFL
: /* reflection vector */
1154 GLfloat axis
[4], dir
[4], result
[4], tmpX
, tmpW
;
1155 fetch_vector4(&inst
->SrcReg
[0], machine
, axis
);
1156 fetch_vector4(&inst
->SrcReg
[1], machine
, dir
);
1157 tmpW
= DOT3(axis
, axis
);
1158 tmpX
= (2.0F
* DOT3(axis
, dir
)) / tmpW
;
1159 result
[0] = tmpX
* axis
[0] - dir
[0];
1160 result
[1] = tmpX
* axis
[1] - dir
[1];
1161 result
[2] = tmpX
* axis
[2] - dir
[2];
1162 /* result[3] is never written! XXX enforce in parser! */
1163 store_vector4(inst
, machine
, result
);
1166 case OPCODE_RSQ
: /* 1 / sqrt() */
1168 GLfloat a
[4], result
[4];
1169 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1171 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1172 store_vector4(inst
, machine
, result
);
1174 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
1178 case OPCODE_SCS
: /* sine and cos */
1180 GLfloat a
[4], result
[4];
1181 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1182 result
[0] = (GLfloat
) _mesa_cos(a
[0]);
1183 result
[1] = (GLfloat
) _mesa_sin(a
[0]);
1184 result
[2] = 0.0; /* undefined! */
1185 result
[3] = 0.0; /* undefined! */
1186 store_vector4(inst
, machine
, result
);
1189 case OPCODE_SEQ
: /* set on equal */
1191 GLfloat a
[4], b
[4], result
[4];
1192 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1193 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1194 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1195 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1196 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1197 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1198 store_vector4(inst
, machine
, result
);
1200 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1201 result
[0], result
[1], result
[2], result
[3],
1202 a
[0], a
[1], a
[2], a
[3],
1203 b
[0], b
[1], b
[2], b
[3]);
1207 case OPCODE_SFL
: /* set false, operands ignored */
1209 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1210 store_vector4(inst
, machine
, result
);
1213 case OPCODE_SGE
: /* set on greater or equal */
1215 GLfloat a
[4], b
[4], result
[4];
1216 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1217 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1218 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1219 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1220 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1221 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1222 store_vector4(inst
, machine
, result
);
1224 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1225 result
[0], result
[1], result
[2], result
[3],
1226 a
[0], a
[1], a
[2], a
[3],
1227 b
[0], b
[1], b
[2], b
[3]);
1231 case OPCODE_SGT
: /* set on greater */
1233 GLfloat a
[4], b
[4], result
[4];
1234 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1235 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1236 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1237 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1238 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1239 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1240 store_vector4(inst
, machine
, result
);
1242 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1243 result
[0], result
[1], result
[2], result
[3],
1244 a
[0], a
[1], a
[2], a
[3],
1245 b
[0], b
[1], b
[2], b
[3]);
1251 GLfloat a
[4], result
[4];
1252 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1253 result
[0] = result
[1] = result
[2] = result
[3]
1254 = (GLfloat
) _mesa_sin(a
[0]);
1255 store_vector4(inst
, machine
, result
);
1258 case OPCODE_SLE
: /* set on less or equal */
1260 GLfloat a
[4], b
[4], result
[4];
1261 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1262 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1263 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1264 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1265 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1266 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1267 store_vector4(inst
, machine
, result
);
1269 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1270 result
[0], result
[1], result
[2], result
[3],
1271 a
[0], a
[1], a
[2], a
[3],
1272 b
[0], b
[1], b
[2], b
[3]);
1276 case OPCODE_SLT
: /* set on less */
1278 GLfloat a
[4], b
[4], result
[4];
1279 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1280 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1281 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1282 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1283 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1284 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1285 store_vector4(inst
, machine
, result
);
1287 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1288 result
[0], result
[1], result
[2], result
[3],
1289 a
[0], a
[1], a
[2], a
[3],
1290 b
[0], b
[1], b
[2], b
[3]);
1294 case OPCODE_SNE
: /* set on not equal */
1296 GLfloat a
[4], b
[4], result
[4];
1297 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1298 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1299 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1300 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1301 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1302 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1303 store_vector4(inst
, machine
, result
);
1305 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1306 result
[0], result
[1], result
[2], result
[3],
1307 a
[0], a
[1], a
[2], a
[3],
1308 b
[0], b
[1], b
[2], b
[3]);
1312 case OPCODE_STR
: /* set true, operands ignored */
1314 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1315 store_vector4(inst
, machine
, result
);
1320 GLfloat a
[4], b
[4], result
[4];
1321 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1322 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1323 result
[0] = a
[0] - b
[0];
1324 result
[1] = a
[1] - b
[1];
1325 result
[2] = a
[2] - b
[2];
1326 result
[3] = a
[3] - b
[3];
1327 store_vector4(inst
, machine
, result
);
1329 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1330 result
[0], result
[1], result
[2], result
[3],
1331 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1335 case OPCODE_SWZ
: /* extended swizzle */
1337 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1338 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
1341 for (i
= 0; i
< 4; i
++) {
1342 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1343 if (swz
== SWIZZLE_ZERO
)
1345 else if (swz
== SWIZZLE_ONE
)
1350 result
[i
] = src
[swz
];
1352 if (source
->NegateBase
& (1 << i
))
1353 result
[i
] = -result
[i
];
1355 store_vector4(inst
, machine
, result
);
1358 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1359 /* Simple texel lookup */
1361 GLfloat texcoord
[4], color
[4];
1362 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1364 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1367 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1368 color
[0], color
[1], color
[2], color
[3],
1370 texcoord
[0], texcoord
[1], texcoord
[2], texcoord
[3]);
1372 store_vector4(inst
, machine
, color
);
1375 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1376 /* Texel lookup with LOD bias */
1378 const struct gl_texture_unit
*texUnit
1379 = &ctx
->Texture
.Unit
[inst
->TexSrcUnit
];
1380 GLfloat texcoord
[4], color
[4], lodBias
;
1382 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1384 /* texcoord[3] is the bias to add to lambda */
1385 lodBias
= texUnit
->LodBias
+ texcoord
[3];
1386 if (texUnit
->_Current
) {
1387 lodBias
+= texUnit
->_Current
->LodBias
;
1390 fetch_texel(ctx
, machine
, inst
, texcoord
, lodBias
, color
);
1392 store_vector4(inst
, machine
, color
);
1395 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1396 /* Texture lookup w/ partial derivatives for LOD */
1398 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1399 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1400 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1401 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1402 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1404 inst
->TexSrcUnit
, color
);
1405 store_vector4(inst
, machine
, color
);
1408 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1409 /* Texture lookup w/ projective divide */
1411 GLfloat texcoord
[4], color
[4];
1413 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1414 /* Not so sure about this test - if texcoord[3] is
1415 * zero, we'd probably be fine except for an ASSERT in
1416 * IROUND_POS() which gets triggered by the inf values created.
1418 if (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_TXP_NV
: /* GL_NV_fragment_program only */
1430 /* Texture lookup w/ projective divide, as above, but do not
1431 * do the divide by w if sampling from a cube map.
1434 GLfloat texcoord
[4], color
[4];
1436 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1437 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1438 texcoord
[3] != 0.0) {
1439 texcoord
[0] /= texcoord
[3];
1440 texcoord
[1] /= texcoord
[3];
1441 texcoord
[2] /= texcoord
[3];
1444 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1446 store_vector4(inst
, machine
, color
);
1449 case OPCODE_UP2H
: /* unpack two 16-bit floats */
1451 GLfloat a
[4], result
[4];
1452 const GLuint
*rawBits
= (const GLuint
*) a
;
1454 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1455 hx
= rawBits
[0] & 0xffff;
1456 hy
= rawBits
[0] >> 16;
1457 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1458 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1459 store_vector4(inst
, machine
, result
);
1462 case OPCODE_UP2US
: /* unpack two GLushorts */
1464 GLfloat a
[4], result
[4];
1465 const GLuint
*rawBits
= (const GLuint
*) a
;
1467 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1468 usx
= rawBits
[0] & 0xffff;
1469 usy
= rawBits
[0] >> 16;
1470 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1471 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1472 store_vector4(inst
, machine
, result
);
1475 case OPCODE_UP4B
: /* unpack four GLbytes */
1477 GLfloat a
[4], result
[4];
1478 const GLuint
*rawBits
= (const GLuint
*) a
;
1479 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1480 result
[0] = (((rawBits
[0] >> 0) & 0xff) - 128) / 127.0F
;
1481 result
[1] = (((rawBits
[0] >> 8) & 0xff) - 128) / 127.0F
;
1482 result
[2] = (((rawBits
[0] >> 16) & 0xff) - 128) / 127.0F
;
1483 result
[3] = (((rawBits
[0] >> 24) & 0xff) - 128) / 127.0F
;
1484 store_vector4(inst
, machine
, result
);
1487 case OPCODE_UP4UB
: /* unpack four GLubytes */
1489 GLfloat a
[4], result
[4];
1490 const GLuint
*rawBits
= (const GLuint
*) a
;
1491 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1492 result
[0] = ((rawBits
[0] >> 0) & 0xff) / 255.0F
;
1493 result
[1] = ((rawBits
[0] >> 8) & 0xff) / 255.0F
;
1494 result
[2] = ((rawBits
[0] >> 16) & 0xff) / 255.0F
;
1495 result
[3] = ((rawBits
[0] >> 24) & 0xff) / 255.0F
;
1496 store_vector4(inst
, machine
, result
);
1499 case OPCODE_XPD
: /* cross product */
1501 GLfloat a
[4], b
[4], result
[4];
1502 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1503 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1504 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1505 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1506 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1508 store_vector4(inst
, machine
, result
);
1510 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1511 result
[0], result
[1], result
[2], result
[3],
1512 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1516 case OPCODE_X2D
: /* 2-D matrix transform */
1518 GLfloat a
[4], b
[4], c
[4], result
[4];
1519 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1520 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1521 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1522 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1523 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1524 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1525 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1526 store_vector4(inst
, machine
, result
);
1531 if (inst
->SrcReg
[0].File
!= -1) {
1533 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1534 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst
->Data
,
1535 a
[0], a
[1], a
[2], a
[3]);
1538 _mesa_printf("%s\n", (const char *) inst
->Data
);
1545 _mesa_problem(ctx
, "Bad opcode %d in _mesa_execute_program",
1547 return GL_TRUE
; /* return value doesn't matter */
1551 if (numExec
> maxExec
) {
1552 _mesa_problem(ctx
, "Infinite loop detected in fragment program");
1558 #if FEATURE_MESA_program_debug
1559 CurrentMachine
= NULL
;