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 "slang_library_noise.h"
49 /* See comments below for info about this */
57 * Set x to positive or negative infinity.
59 #if defined(USE_IEEE) || defined(_WIN32)
60 #define SET_POS_INFINITY(x) ( *((GLuint *) (void *)&x) = 0x7F800000 )
61 #define SET_NEG_INFINITY(x) ( *((GLuint *) (void *)&x) = 0xFF800000 )
63 #define SET_POS_INFINITY(x) x = __MAXFLOAT
64 #define SET_NEG_INFINITY(x) x = -__MAXFLOAT
66 #define SET_POS_INFINITY(x) x = (GLfloat) HUGE_VAL
67 #define SET_NEG_INFINITY(x) x = (GLfloat) -HUGE_VAL
70 #define SET_FLOAT_BITS(x, bits) ((fi_type *) (void *) &(x))->i = bits
73 static const GLfloat ZeroVec
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
78 * Return a pointer to the 4-element float vector specified by the given
81 static INLINE
const GLfloat
*
82 get_register_pointer(const struct prog_src_register
*source
,
83 const struct gl_program_machine
*machine
)
85 if (source
->RelAddr
) {
86 const GLint reg
= source
->Index
+ machine
->AddressReg
[0][0];
87 if (source
->File
== PROGRAM_ENV_PARAM
)
88 if (reg
< 0 || reg
>= MAX_PROGRAM_ENV_PARAMS
)
91 return machine
->EnvParams
[reg
];
93 const struct gl_program_parameter_list
*params
;
94 ASSERT(source
->File
== PROGRAM_LOCAL_PARAM
||
95 source
->File
== PROGRAM_STATE_VAR
);
96 params
= machine
->CurProgram
->Parameters
;
97 if (reg
< 0 || reg
>= 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
< 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 * Test value against zero and return GT, LT, EQ or UN if NaN.
309 generate_cc(float value
)
312 return COND_UN
; /* NaN */
322 * Test if the ccMaskRule is satisfied by the given condition code.
323 * Used to mask destination writes according to the current condition code.
325 static INLINE GLboolean
326 test_cc(GLuint condCode
, GLuint ccMaskRule
)
328 switch (ccMaskRule
) {
329 case COND_EQ
: return (condCode
== COND_EQ
);
330 case COND_NE
: return (condCode
!= COND_EQ
);
331 case COND_LT
: return (condCode
== COND_LT
);
332 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
333 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
334 case COND_GT
: return (condCode
== COND_GT
);
335 case COND_TR
: return GL_TRUE
;
336 case COND_FL
: return GL_FALSE
;
337 default: return GL_TRUE
;
343 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
344 * or GL_FALSE to indicate result.
346 static INLINE GLboolean
347 eval_condition(const struct gl_program_machine
*machine
,
348 const struct prog_instruction
*inst
)
350 const GLuint swizzle
= inst
->DstReg
.CondSwizzle
;
351 const GLuint condMask
= inst
->DstReg
.CondMask
;
352 if (test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 0)], condMask
) ||
353 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 1)], condMask
) ||
354 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 2)], condMask
) ||
355 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 3)], condMask
)) {
366 * Store 4 floats into a register. Observe the instructions saturate and
367 * set-condition-code flags.
370 store_vector4(const struct prog_instruction
*inst
,
371 struct gl_program_machine
*machine
, const GLfloat value
[4])
373 const struct prog_dst_register
*dest
= &(inst
->DstReg
);
374 const GLboolean clamp
= inst
->SaturateMode
== SATURATE_ZERO_ONE
;
377 GLfloat clampedValue
[4];
378 GLuint writeMask
= dest
->WriteMask
;
380 switch (dest
->File
) {
382 ASSERT(dest
->Index
< MAX_PROGRAM_OUTPUTS
);
383 dstReg
= machine
->Outputs
[dest
->Index
];
385 case PROGRAM_TEMPORARY
:
386 ASSERT(dest
->Index
< MAX_PROGRAM_TEMPS
);
387 dstReg
= machine
->Temporaries
[dest
->Index
];
389 case PROGRAM_WRITE_ONLY
:
393 _mesa_problem(NULL
, "bad register file in store_vector4(fp)");
398 if (value
[0] > 1.0e10
||
399 IS_INF_OR_NAN(value
[0]) ||
400 IS_INF_OR_NAN(value
[1]) ||
401 IS_INF_OR_NAN(value
[2]) || IS_INF_OR_NAN(value
[3]))
402 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
406 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
407 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
408 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
409 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
410 value
= clampedValue
;
413 if (dest
->CondMask
!= COND_TR
) {
414 /* condition codes may turn off some writes */
415 if (writeMask
& WRITEMASK_X
) {
416 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 0)],
418 writeMask
&= ~WRITEMASK_X
;
420 if (writeMask
& WRITEMASK_Y
) {
421 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 1)],
423 writeMask
&= ~WRITEMASK_Y
;
425 if (writeMask
& WRITEMASK_Z
) {
426 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 2)],
428 writeMask
&= ~WRITEMASK_Z
;
430 if (writeMask
& WRITEMASK_W
) {
431 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 3)],
433 writeMask
&= ~WRITEMASK_W
;
437 if (writeMask
& WRITEMASK_X
)
438 dstReg
[0] = value
[0];
439 if (writeMask
& WRITEMASK_Y
)
440 dstReg
[1] = value
[1];
441 if (writeMask
& WRITEMASK_Z
)
442 dstReg
[2] = value
[2];
443 if (writeMask
& WRITEMASK_W
)
444 dstReg
[3] = value
[3];
446 if (inst
->CondUpdate
) {
447 if (writeMask
& WRITEMASK_X
)
448 machine
->CondCodes
[0] = generate_cc(value
[0]);
449 if (writeMask
& WRITEMASK_Y
)
450 machine
->CondCodes
[1] = generate_cc(value
[1]);
451 if (writeMask
& WRITEMASK_Z
)
452 machine
->CondCodes
[2] = generate_cc(value
[2]);
453 if (writeMask
& WRITEMASK_W
)
454 machine
->CondCodes
[3] = generate_cc(value
[3]);
456 printf("CondCodes=(%s,%s,%s,%s) for:\n",
457 _mesa_condcode_string(machine
->CondCodes
[0]),
458 _mesa_condcode_string(machine
->CondCodes
[1]),
459 _mesa_condcode_string(machine
->CondCodes
[2]),
460 _mesa_condcode_string(machine
->CondCodes
[3]));
467 * Execute the given vertex/fragment program.
469 * \param ctx rendering context
470 * \param program the program to execute
471 * \param machine machine state (must be initialized)
472 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
475 _mesa_execute_program(GLcontext
* ctx
,
476 const struct gl_program
*program
,
477 struct gl_program_machine
*machine
)
479 const GLuint numInst
= program
->NumInstructions
;
480 const GLuint maxExec
= 10000;
481 GLint pc
, numExec
= 0;
483 machine
->CurProgram
= program
;
486 printf("execute program %u --------------------\n", program
->Id
);
489 #if FEATURE_MESA_program_debug
490 CurrentMachine
= machine
;
493 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
494 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
497 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
500 for (pc
= 0; pc
< numInst
; pc
++) {
501 const struct prog_instruction
*inst
= program
->Instructions
+ pc
;
503 #if FEATURE_MESA_program_debug
504 if (ctx
->FragmentProgram
.CallbackEnabled
&&
505 ctx
->FragmentProgram
.Callback
) {
506 ctx
->FragmentProgram
.CurrentPosition
= inst
->StringPos
;
507 ctx
->FragmentProgram
.Callback(program
->Target
,
508 ctx
->FragmentProgram
.CallbackData
);
513 _mesa_print_instruction(inst
);
516 switch (inst
->Opcode
) {
519 GLfloat a
[4], result
[4];
520 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
521 result
[0] = FABSF(a
[0]);
522 result
[1] = FABSF(a
[1]);
523 result
[2] = FABSF(a
[2]);
524 result
[3] = FABSF(a
[3]);
525 store_vector4(inst
, machine
, result
);
530 GLfloat a
[4], b
[4], result
[4];
531 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
532 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
533 result
[0] = a
[0] + b
[0];
534 result
[1] = a
[1] + b
[1];
535 result
[2] = a
[2] + b
[2];
536 result
[3] = a
[3] + b
[3];
537 store_vector4(inst
, machine
, result
);
539 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
540 result
[0], result
[1], result
[2], result
[3],
541 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
548 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
549 machine
->AddressReg
[0][0] = (GLint
) FLOORF(t
[0]);
556 /* subtract 1 here since pc is incremented by for(pc) loop */
557 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
559 case OPCODE_BGNSUB
: /* begin subroutine */
561 case OPCODE_ENDSUB
: /* end subroutine */
563 case OPCODE_BRA
: /* branch (conditional) */
565 case OPCODE_BRK
: /* break out of loop (conditional) */
567 case OPCODE_CONT
: /* continue loop (conditional) */
568 if (eval_condition(machine
, inst
)) {
570 /* Subtract 1 here since we'll do pc++ at end of for-loop */
571 pc
= inst
->BranchTarget
- 1;
574 case OPCODE_CAL
: /* Call subroutine (conditional) */
575 if (eval_condition(machine
, inst
)) {
576 /* call the subroutine */
577 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
578 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
580 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1; /* next inst */
581 /* Subtract 1 here since we'll do pc++ at end of for-loop */
582 pc
= inst
->BranchTarget
- 1;
587 GLfloat a
[4], b
[4], c
[4], result
[4];
588 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
589 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
590 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
591 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
592 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
593 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
594 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
595 store_vector4(inst
, machine
, result
);
600 GLfloat a
[4], result
[4];
601 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
602 result
[0] = result
[1] = result
[2] = result
[3]
603 = (GLfloat
) _mesa_cos(a
[0]);
604 store_vector4(inst
, machine
, result
);
607 case OPCODE_DDX
: /* Partial derivative with respect to X */
610 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
612 store_vector4(inst
, machine
, result
);
615 case OPCODE_DDY
: /* Partial derivative with respect to Y */
618 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
620 store_vector4(inst
, machine
, result
);
625 GLfloat a
[4], b
[4], result
[4];
626 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
627 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
628 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
629 store_vector4(inst
, machine
, result
);
631 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
632 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
638 GLfloat a
[4], b
[4], result
[4];
639 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
640 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
641 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
, b
);
642 store_vector4(inst
, machine
, result
);
644 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
645 result
[0], a
[0], a
[1], a
[2], a
[3],
646 b
[0], b
[1], b
[2], b
[3]);
652 GLfloat a
[4], b
[4], result
[4];
653 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
654 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
655 result
[0] = result
[1] = result
[2] = result
[3] =
656 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + b
[3];
657 store_vector4(inst
, machine
, result
);
660 case OPCODE_DST
: /* Distance vector */
662 GLfloat a
[4], b
[4], result
[4];
663 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
664 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
666 result
[1] = a
[1] * b
[1];
669 store_vector4(inst
, machine
, result
);
674 GLfloat t
[4], q
[4], floor_t0
;
675 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
676 floor_t0
= FLOORF(t
[0]);
677 if (floor_t0
> FLT_MAX_EXP
) {
678 SET_POS_INFINITY(q
[0]);
679 SET_POS_INFINITY(q
[2]);
681 else if (floor_t0
< FLT_MIN_EXP
) {
686 q
[0] = LDEXPF(1.0, (int) floor_t0
);
687 /* Note: GL_NV_vertex_program expects
688 * result.z = result.x * APPX(result.y)
689 * We do what the ARB extension says.
691 q
[2] = pow(2.0, t
[0]);
693 q
[1] = t
[0] - floor_t0
;
695 store_vector4( inst
, machine
, q
);
698 case OPCODE_EX2
: /* Exponential base 2 */
700 GLfloat a
[4], result
[4];
701 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
702 result
[0] = result
[1] = result
[2] = result
[3] =
703 (GLfloat
) _mesa_pow(2.0, a
[0]);
704 store_vector4(inst
, machine
, result
);
709 GLfloat a
[4], result
[4];
710 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
711 result
[0] = FLOORF(a
[0]);
712 result
[1] = FLOORF(a
[1]);
713 result
[2] = FLOORF(a
[2]);
714 result
[3] = FLOORF(a
[3]);
715 store_vector4(inst
, machine
, result
);
720 GLfloat a
[4], result
[4];
721 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
722 result
[0] = a
[0] - FLOORF(a
[0]);
723 result
[1] = a
[1] - FLOORF(a
[1]);
724 result
[2] = a
[2] - FLOORF(a
[2]);
725 result
[3] = a
[3] - FLOORF(a
[3]);
726 store_vector4(inst
, machine
, result
);
733 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
735 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
736 cond
= (a
[0] != 0.0);
739 cond
= eval_condition(machine
, inst
);
742 printf("IF: %d\n", cond
);
746 /* do if-clause (just continue execution) */
749 /* go to the instruction after ELSE or ENDIF */
750 assert(inst
->BranchTarget
>= 0);
751 pc
= inst
->BranchTarget
- 1;
757 assert(inst
->BranchTarget
>= 0);
758 pc
= inst
->BranchTarget
- 1;
763 case OPCODE_INT
: /* float to int */
765 GLfloat a
[4], result
[4];
766 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
767 result
[0] = (GLfloat
) (GLint
) a
[0];
768 result
[1] = (GLfloat
) (GLint
) a
[1];
769 result
[2] = (GLfloat
) (GLint
) a
[2];
770 result
[3] = (GLfloat
) (GLint
) a
[3];
771 store_vector4(inst
, machine
, result
);
774 case OPCODE_KIL_NV
: /* NV_f_p only (conditional) */
775 if (eval_condition(machine
, inst
)) {
779 case OPCODE_KIL
: /* ARB_f_p only */
782 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
783 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
788 case OPCODE_LG2
: /* log base 2 */
790 GLfloat a
[4], result
[4];
791 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
792 result
[0] = result
[1] = result
[2] = result
[3] = LOG2(a
[0]);
793 store_vector4(inst
, machine
, result
);
798 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
799 GLfloat a
[4], result
[4];
800 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
801 a
[0] = MAX2(a
[0], 0.0F
);
802 a
[1] = MAX2(a
[1], 0.0F
);
803 /* XXX ARB version clamps a[3], NV version doesn't */
804 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
807 /* XXX we could probably just use pow() here */
809 if (a
[1] == 0.0 && a
[3] == 0.0)
812 result
[2] = EXPF(a
[3] * LOGF(a
[1]));
818 store_vector4(inst
, machine
, result
);
820 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
821 result
[0], result
[1], result
[2], result
[3],
822 a
[0], a
[1], a
[2], a
[3]);
828 GLfloat t
[4], q
[4], abs_t0
;
829 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
830 abs_t0
= FABSF(t
[0]);
831 if (abs_t0
!= 0.0F
) {
832 /* Since we really can't handle infinite values on VMS
833 * like other OSes we'll use __MAXFLOAT to represent
834 * infinity. This may need some tweaking.
837 if (abs_t0
== __MAXFLOAT
)
839 if (IS_INF_OR_NAN(abs_t0
))
842 SET_POS_INFINITY(q
[0]);
844 SET_POS_INFINITY(q
[2]);
848 GLfloat mantissa
= FREXPF(t
[0], &exponent
);
849 q
[0] = (GLfloat
) (exponent
- 1);
850 q
[1] = (GLfloat
) (2.0 * mantissa
); /* map [.5, 1) -> [1, 2) */
851 q
[2] = (GLfloat
) (q
[0] + LOG2(q
[1]));
855 SET_NEG_INFINITY(q
[0]);
857 SET_NEG_INFINITY(q
[2]);
860 store_vector4(inst
, machine
, q
);
865 GLfloat a
[4], b
[4], c
[4], result
[4];
866 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
867 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
868 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
869 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
870 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
871 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
872 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
873 store_vector4(inst
, machine
, result
);
875 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
876 "(%g %g %g %g), (%g %g %g %g)\n",
877 result
[0], result
[1], result
[2], result
[3],
878 a
[0], a
[1], a
[2], a
[3],
879 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
885 GLfloat a
[4], b
[4], c
[4], result
[4];
886 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
887 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
888 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
889 result
[0] = a
[0] * b
[0] + c
[0];
890 result
[1] = a
[1] * b
[1] + c
[1];
891 result
[2] = a
[2] * b
[2] + c
[2];
892 result
[3] = a
[3] * b
[3] + c
[3];
893 store_vector4(inst
, machine
, result
);
895 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
896 "(%g %g %g %g) + (%g %g %g %g)\n",
897 result
[0], result
[1], result
[2], result
[3],
898 a
[0], a
[1], a
[2], a
[3],
899 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
905 GLfloat a
[4], b
[4], result
[4];
906 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
907 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
908 result
[0] = MAX2(a
[0], b
[0]);
909 result
[1] = MAX2(a
[1], b
[1]);
910 result
[2] = MAX2(a
[2], b
[2]);
911 result
[3] = MAX2(a
[3], b
[3]);
912 store_vector4(inst
, machine
, result
);
914 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
915 result
[0], result
[1], result
[2], result
[3],
916 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
922 GLfloat a
[4], b
[4], result
[4];
923 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
924 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
925 result
[0] = MIN2(a
[0], b
[0]);
926 result
[1] = MIN2(a
[1], b
[1]);
927 result
[2] = MIN2(a
[2], b
[2]);
928 result
[3] = MIN2(a
[3], b
[3]);
929 store_vector4(inst
, machine
, result
);
935 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
936 store_vector4(inst
, machine
, result
);
938 printf("MOV (%g %g %g %g)\n",
939 result
[0], result
[1], result
[2], result
[3]);
945 GLfloat a
[4], b
[4], result
[4];
946 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
947 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
948 result
[0] = a
[0] * b
[0];
949 result
[1] = a
[1] * b
[1];
950 result
[2] = a
[2] * b
[2];
951 result
[3] = a
[3] * b
[3];
952 store_vector4(inst
, machine
, result
);
954 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
955 result
[0], result
[1], result
[2], result
[3],
956 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
962 GLfloat a
[4], result
[4];
963 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
966 result
[2] = result
[3] = _slang_library_noise1(a
[0]);
967 store_vector4(inst
, machine
, result
);
972 GLfloat a
[4], result
[4];
973 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
976 result
[2] = result
[3] = _slang_library_noise2(a
[0], a
[1]);
977 store_vector4(inst
, machine
, result
);
982 GLfloat a
[4], result
[4];
983 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
987 result
[3] = _slang_library_noise3(a
[0], a
[1], a
[2]);
988 store_vector4(inst
, machine
, result
);
993 GLfloat a
[4], result
[4];
994 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
998 result
[3] = _slang_library_noise4(a
[0], a
[1], a
[2], a
[3]);
999 store_vector4(inst
, machine
, result
);
1004 case OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
1006 GLfloat a
[4], result
[4];
1008 GLuint
*rawResult
= (GLuint
*) result
;
1010 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1011 hx
= _mesa_float_to_half(a
[0]);
1012 hy
= _mesa_float_to_half(a
[1]);
1013 twoHalves
= hx
| (hy
<< 16);
1014 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1016 store_vector4(inst
, machine
, result
);
1019 case OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
1021 GLfloat a
[4], result
[4];
1022 GLuint usx
, usy
, *rawResult
= (GLuint
*) result
;
1023 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1024 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1025 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1026 usx
= IROUND(a
[0] * 65535.0F
);
1027 usy
= IROUND(a
[1] * 65535.0F
);
1028 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1029 = usx
| (usy
<< 16);
1030 store_vector4(inst
, machine
, result
);
1033 case OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
1035 GLfloat a
[4], result
[4];
1036 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
1037 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1038 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
1039 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
1040 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
1041 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
1042 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
1043 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
1044 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
1045 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
1046 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1047 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1048 store_vector4(inst
, machine
, result
);
1051 case OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
1053 GLfloat a
[4], result
[4];
1054 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
1055 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1056 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1057 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1058 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
1059 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
1060 ubx
= IROUND(255.0F
* a
[0]);
1061 uby
= IROUND(255.0F
* a
[1]);
1062 ubz
= IROUND(255.0F
* a
[2]);
1063 ubw
= IROUND(255.0F
* a
[3]);
1064 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1065 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1066 store_vector4(inst
, machine
, result
);
1071 GLfloat a
[4], b
[4], result
[4];
1072 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1073 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
1074 result
[0] = result
[1] = result
[2] = result
[3]
1075 = (GLfloat
) _mesa_pow(a
[0], b
[0]);
1076 store_vector4(inst
, machine
, result
);
1081 GLfloat a
[4], result
[4];
1082 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1086 else if (IS_INF_OR_NAN(a
[0]))
1087 printf("RCP(inf)\n");
1089 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
1090 store_vector4(inst
, machine
, result
);
1093 case OPCODE_RET
: /* return from subroutine (conditional) */
1094 if (eval_condition(machine
, inst
)) {
1095 if (machine
->StackDepth
== 0) {
1096 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
1098 /* subtract one because of pc++ in the for loop */
1099 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
1102 case OPCODE_RFL
: /* reflection vector */
1104 GLfloat axis
[4], dir
[4], result
[4], tmpX
, tmpW
;
1105 fetch_vector4(&inst
->SrcReg
[0], machine
, axis
);
1106 fetch_vector4(&inst
->SrcReg
[1], machine
, dir
);
1107 tmpW
= DOT3(axis
, axis
);
1108 tmpX
= (2.0F
* DOT3(axis
, dir
)) / tmpW
;
1109 result
[0] = tmpX
* axis
[0] - dir
[0];
1110 result
[1] = tmpX
* axis
[1] - dir
[1];
1111 result
[2] = tmpX
* axis
[2] - dir
[2];
1112 /* result[3] is never written! XXX enforce in parser! */
1113 store_vector4(inst
, machine
, result
);
1116 case OPCODE_RSQ
: /* 1 / sqrt() */
1118 GLfloat a
[4], result
[4];
1119 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1121 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1122 store_vector4(inst
, machine
, result
);
1124 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
1128 case OPCODE_SCS
: /* sine and cos */
1130 GLfloat a
[4], result
[4];
1131 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1132 result
[0] = (GLfloat
) _mesa_cos(a
[0]);
1133 result
[1] = (GLfloat
) _mesa_sin(a
[0]);
1134 result
[2] = 0.0; /* undefined! */
1135 result
[3] = 0.0; /* undefined! */
1136 store_vector4(inst
, machine
, result
);
1139 case OPCODE_SEQ
: /* set on equal */
1141 GLfloat a
[4], b
[4], result
[4];
1142 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1143 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1144 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1145 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1146 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1147 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1148 store_vector4(inst
, machine
, result
);
1150 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1151 result
[0], result
[1], result
[2], result
[3],
1152 a
[0], a
[1], a
[2], a
[3],
1153 b
[0], b
[1], b
[2], b
[3]);
1157 case OPCODE_SFL
: /* set false, operands ignored */
1159 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1160 store_vector4(inst
, machine
, result
);
1163 case OPCODE_SGE
: /* set on greater or equal */
1165 GLfloat a
[4], b
[4], result
[4];
1166 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1167 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1168 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1169 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1170 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1171 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1172 store_vector4(inst
, machine
, result
);
1174 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1175 result
[0], result
[1], result
[2], result
[3],
1176 a
[0], a
[1], a
[2], a
[3],
1177 b
[0], b
[1], b
[2], b
[3]);
1181 case OPCODE_SGT
: /* set on greater */
1183 GLfloat a
[4], b
[4], result
[4];
1184 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1185 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1186 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1187 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1188 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1189 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1190 store_vector4(inst
, machine
, result
);
1192 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1193 result
[0], result
[1], result
[2], result
[3],
1194 a
[0], a
[1], a
[2], a
[3],
1195 b
[0], b
[1], b
[2], b
[3]);
1201 GLfloat a
[4], result
[4];
1202 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1203 result
[0] = result
[1] = result
[2] = result
[3]
1204 = (GLfloat
) _mesa_sin(a
[0]);
1205 store_vector4(inst
, machine
, result
);
1208 case OPCODE_SLE
: /* set on less or equal */
1210 GLfloat a
[4], b
[4], result
[4];
1211 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1212 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1213 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1214 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1215 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1216 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1217 store_vector4(inst
, machine
, result
);
1219 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1220 result
[0], result
[1], result
[2], result
[3],
1221 a
[0], a
[1], a
[2], a
[3],
1222 b
[0], b
[1], b
[2], b
[3]);
1226 case OPCODE_SLT
: /* set on less */
1228 GLfloat a
[4], b
[4], result
[4];
1229 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1230 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1231 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1232 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1233 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1234 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1235 store_vector4(inst
, machine
, result
);
1237 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1238 result
[0], result
[1], result
[2], result
[3],
1239 a
[0], a
[1], a
[2], a
[3],
1240 b
[0], b
[1], b
[2], b
[3]);
1244 case OPCODE_SNE
: /* set on not equal */
1246 GLfloat a
[4], b
[4], result
[4];
1247 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1248 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1249 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1250 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1251 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1252 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1253 store_vector4(inst
, machine
, result
);
1255 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1256 result
[0], result
[1], result
[2], result
[3],
1257 a
[0], a
[1], a
[2], a
[3],
1258 b
[0], b
[1], b
[2], b
[3]);
1262 case OPCODE_STR
: /* set true, operands ignored */
1264 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1265 store_vector4(inst
, machine
, result
);
1270 GLfloat a
[4], b
[4], result
[4];
1271 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1272 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1273 result
[0] = a
[0] - b
[0];
1274 result
[1] = a
[1] - b
[1];
1275 result
[2] = a
[2] - b
[2];
1276 result
[3] = a
[3] - b
[3];
1277 store_vector4(inst
, machine
, result
);
1279 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1280 result
[0], result
[1], result
[2], result
[3],
1281 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1285 case OPCODE_SWZ
: /* extended swizzle */
1287 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1288 const GLfloat
*src
= get_register_pointer(source
, machine
);
1291 for (i
= 0; i
< 4; i
++) {
1292 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1293 if (swz
== SWIZZLE_ZERO
)
1295 else if (swz
== SWIZZLE_ONE
)
1300 result
[i
] = src
[swz
];
1302 if (source
->NegateBase
& (1 << i
))
1303 result
[i
] = -result
[i
];
1305 store_vector4(inst
, machine
, result
);
1308 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1311 /* Note: only use the precomputed lambda value when we're
1312 * sampling texture unit [K] with texcoord[K].
1313 * Otherwise, the lambda value may have no relation to the
1314 * instruction's texcoord or texture image. Using the wrong
1315 * lambda is usually bad news.
1316 * The rest of the time, just use zero (until we get a more
1317 * sophisticated way of computing lambda).
1319 GLfloat coord
[4], color
[4], lambda
;
1321 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
1322 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
)
1323 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
];
1327 fetch_vector4(&inst
->SrcReg
[0], machine
, coord
);
1328 machine
->FetchTexelLod(ctx
, coord
, lambda
, inst
->TexSrcUnit
,
1331 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g], "
1333 color
[0], color
[1], color
[2], color
[3],
1335 coord
[0], coord
[1], coord
[2], coord
[3], lambda
);
1337 store_vector4(inst
, machine
, color
);
1340 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1341 /* Texel lookup with LOD bias */
1343 const struct gl_texture_unit
*texUnit
1344 = &ctx
->Texture
.Unit
[inst
->TexSrcUnit
];
1345 GLfloat coord
[4], color
[4], lambda
, bias
;
1347 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
1348 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
)
1349 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
];
1353 fetch_vector4(&inst
->SrcReg
[0], machine
, coord
);
1354 /* coord[3] is the bias to add to lambda */
1355 bias
= texUnit
->LodBias
+ coord
[3];
1356 if (texUnit
->_Current
)
1357 bias
+= texUnit
->_Current
->LodBias
;
1358 machine
->FetchTexelLod(ctx
, coord
, lambda
+ bias
,
1359 inst
->TexSrcUnit
, color
);
1360 store_vector4(inst
, machine
, color
);
1363 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1364 /* Texture lookup w/ partial derivatives for LOD */
1366 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1367 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1368 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1369 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1370 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1371 inst
->TexSrcUnit
, color
);
1372 store_vector4(inst
, machine
, color
);
1375 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1376 /* Texture lookup w/ projective divide */
1378 GLfloat texcoord
[4], color
[4], lambda
;
1380 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
1381 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
)
1382 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
];
1386 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1387 /* Not so sure about this test - if texcoord[3] is
1388 * zero, we'd probably be fine except for an ASSERT in
1389 * IROUND_POS() which gets triggered by the inf values created.
1391 if (texcoord
[3] != 0.0) {
1392 texcoord
[0] /= texcoord
[3];
1393 texcoord
[1] /= texcoord
[3];
1394 texcoord
[2] /= texcoord
[3];
1396 machine
->FetchTexelLod(ctx
, texcoord
, lambda
,
1397 inst
->TexSrcUnit
, color
);
1398 store_vector4(inst
, machine
, color
);
1401 case OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1402 /* Texture lookup w/ projective divide */
1404 GLfloat texcoord
[4], color
[4], lambda
;
1406 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
1407 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
)
1408 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
];
1412 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1413 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1414 texcoord
[3] != 0.0) {
1415 texcoord
[0] /= texcoord
[3];
1416 texcoord
[1] /= texcoord
[3];
1417 texcoord
[2] /= texcoord
[3];
1419 machine
->FetchTexelLod(ctx
, texcoord
, lambda
,
1420 inst
->TexSrcUnit
, color
);
1421 store_vector4(inst
, machine
, color
);
1424 case OPCODE_UP2H
: /* unpack two 16-bit floats */
1426 GLfloat a
[4], result
[4];
1427 const GLuint
*rawBits
= (const GLuint
*) a
;
1429 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1430 hx
= rawBits
[0] & 0xffff;
1431 hy
= rawBits
[0] >> 16;
1432 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1433 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1434 store_vector4(inst
, machine
, result
);
1437 case OPCODE_UP2US
: /* unpack two GLushorts */
1439 GLfloat a
[4], result
[4];
1440 const GLuint
*rawBits
= (const GLuint
*) a
;
1442 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1443 usx
= rawBits
[0] & 0xffff;
1444 usy
= rawBits
[0] >> 16;
1445 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1446 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1447 store_vector4(inst
, machine
, result
);
1450 case OPCODE_UP4B
: /* unpack four GLbytes */
1452 GLfloat a
[4], result
[4];
1453 const GLuint
*rawBits
= (const GLuint
*) a
;
1454 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1455 result
[0] = (((rawBits
[0] >> 0) & 0xff) - 128) / 127.0F
;
1456 result
[1] = (((rawBits
[0] >> 8) & 0xff) - 128) / 127.0F
;
1457 result
[2] = (((rawBits
[0] >> 16) & 0xff) - 128) / 127.0F
;
1458 result
[3] = (((rawBits
[0] >> 24) & 0xff) - 128) / 127.0F
;
1459 store_vector4(inst
, machine
, result
);
1462 case OPCODE_UP4UB
: /* unpack four GLubytes */
1464 GLfloat a
[4], result
[4];
1465 const GLuint
*rawBits
= (const GLuint
*) a
;
1466 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1467 result
[0] = ((rawBits
[0] >> 0) & 0xff) / 255.0F
;
1468 result
[1] = ((rawBits
[0] >> 8) & 0xff) / 255.0F
;
1469 result
[2] = ((rawBits
[0] >> 16) & 0xff) / 255.0F
;
1470 result
[3] = ((rawBits
[0] >> 24) & 0xff) / 255.0F
;
1471 store_vector4(inst
, machine
, result
);
1474 case OPCODE_XPD
: /* cross product */
1476 GLfloat a
[4], b
[4], result
[4];
1477 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1478 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1479 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1480 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1481 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1483 store_vector4(inst
, machine
, result
);
1485 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1486 result
[0], result
[1], result
[2], result
[3],
1487 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1491 case OPCODE_X2D
: /* 2-D matrix transform */
1493 GLfloat a
[4], b
[4], c
[4], result
[4];
1494 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1495 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1496 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1497 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1498 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1499 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1500 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1501 store_vector4(inst
, machine
, result
);
1506 if (inst
->SrcReg
[0].File
!= -1) {
1508 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1509 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst
->Data
,
1510 a
[0], a
[1], a
[2], a
[3]);
1513 _mesa_printf("%s\n", (const char *) inst
->Data
);
1520 _mesa_problem(ctx
, "Bad opcode %d in _mesa_exec_fragment_program",
1522 return GL_TRUE
; /* return value doesn't matter */
1527 if (numExec
> maxExec
) {
1528 _mesa_problem(ctx
, "Infinite loop detected in fragment program");
1534 #if FEATURE_MESA_program_debug
1535 CurrentMachine
= NULL
;