2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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/macros.h"
41 #include "prog_execute.h"
42 #include "prog_instruction.h"
43 #include "prog_parameter.h"
44 #include "prog_print.h"
45 #include "prog_noise.h"
53 * Set x to positive or negative infinity.
55 #define SET_POS_INFINITY(x) \
61 #define SET_NEG_INFINITY(x) \
68 #define SET_FLOAT_BITS(x, bits) ((fi_type *) (void *) &(x))->i = bits
71 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
>= VARYING_SLOT_MAX
)
108 return machine
->Attribs
[reg
][machine
->CurElement
];
112 if (reg
>= MAX_PROGRAM_OUTPUTS
)
114 return machine
->Outputs
[reg
];
116 case PROGRAM_STATE_VAR
:
118 case PROGRAM_CONSTANT
:
120 case PROGRAM_UNIFORM
:
121 if (reg
>= (GLint
) prog
->Parameters
->NumParameters
)
123 return (GLfloat
*) prog
->Parameters
->ParameterValues
[reg
];
125 case PROGRAM_SYSTEM_VALUE
:
126 assert(reg
< Elements(machine
->SystemValues
));
127 return machine
->SystemValues
[reg
];
131 "Invalid src register file %d in get_src_register_pointer()",
139 * Return a pointer to the 4-element float vector specified by the given
140 * destination register.
142 static inline GLfloat
*
143 get_dst_register_pointer(const struct prog_dst_register
*dest
,
144 struct gl_program_machine
*machine
)
146 static GLfloat dummyReg
[4];
147 GLint reg
= dest
->Index
;
150 /* add address register value to src index/offset */
151 reg
+= machine
->AddressReg
[0][0];
157 switch (dest
->File
) {
158 case PROGRAM_TEMPORARY
:
159 if (reg
>= MAX_PROGRAM_TEMPS
)
161 return machine
->Temporaries
[reg
];
164 if (reg
>= MAX_PROGRAM_OUTPUTS
)
166 return machine
->Outputs
[reg
];
170 "Invalid dest register file %d in get_dst_register_pointer()",
179 * Fetch a 4-element float vector from the given source register.
180 * Apply swizzling and negating as needed.
183 fetch_vector4(const struct prog_src_register
*source
,
184 const struct gl_program_machine
*machine
, GLfloat result
[4])
186 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
188 if (source
->Swizzle
== SWIZZLE_NOOP
) {
190 COPY_4V(result
, src
);
193 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
194 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
195 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
196 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
197 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
198 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
199 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
200 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
204 result
[0] = FABSF(result
[0]);
205 result
[1] = FABSF(result
[1]);
206 result
[2] = FABSF(result
[2]);
207 result
[3] = FABSF(result
[3]);
209 if (source
->Negate
) {
210 ASSERT(source
->Negate
== NEGATE_XYZW
);
211 result
[0] = -result
[0];
212 result
[1] = -result
[1];
213 result
[2] = -result
[2];
214 result
[3] = -result
[3];
218 assert(!IS_INF_OR_NAN(result
[0]));
219 assert(!IS_INF_OR_NAN(result
[0]));
220 assert(!IS_INF_OR_NAN(result
[0]));
221 assert(!IS_INF_OR_NAN(result
[0]));
227 * Fetch the derivative with respect to X or Y for the given register.
228 * XXX this currently only works for fragment program input attribs.
231 fetch_vector4_deriv(struct gl_context
* ctx
,
232 const struct prog_src_register
*source
,
233 const struct gl_program_machine
*machine
,
234 char xOrY
, GLfloat result
[4])
236 if (source
->File
== PROGRAM_INPUT
&&
237 source
->Index
< (GLint
) machine
->NumDeriv
) {
238 const GLint col
= machine
->CurElement
;
239 const GLfloat w
= machine
->Attribs
[VARYING_SLOT_POS
][col
][3];
240 const GLfloat invQ
= 1.0f
/ w
;
244 deriv
[0] = machine
->DerivX
[source
->Index
][0] * invQ
;
245 deriv
[1] = machine
->DerivX
[source
->Index
][1] * invQ
;
246 deriv
[2] = machine
->DerivX
[source
->Index
][2] * invQ
;
247 deriv
[3] = machine
->DerivX
[source
->Index
][3] * invQ
;
250 deriv
[0] = machine
->DerivY
[source
->Index
][0] * invQ
;
251 deriv
[1] = machine
->DerivY
[source
->Index
][1] * invQ
;
252 deriv
[2] = machine
->DerivY
[source
->Index
][2] * invQ
;
253 deriv
[3] = machine
->DerivY
[source
->Index
][3] * invQ
;
256 result
[0] = deriv
[GET_SWZ(source
->Swizzle
, 0)];
257 result
[1] = deriv
[GET_SWZ(source
->Swizzle
, 1)];
258 result
[2] = deriv
[GET_SWZ(source
->Swizzle
, 2)];
259 result
[3] = deriv
[GET_SWZ(source
->Swizzle
, 3)];
262 result
[0] = FABSF(result
[0]);
263 result
[1] = FABSF(result
[1]);
264 result
[2] = FABSF(result
[2]);
265 result
[3] = FABSF(result
[3]);
267 if (source
->Negate
) {
268 ASSERT(source
->Negate
== NEGATE_XYZW
);
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_src_register_pointer(source
, machine
);
290 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
293 result
[0] = FABSF(result
[0]);
295 if (source
->Negate
) {
296 result
[0] = -result
[0];
302 fetch_vector1ui(const struct prog_src_register
*source
,
303 const struct gl_program_machine
*machine
)
305 const GLuint
*src
= (GLuint
*) get_src_register_pointer(source
, machine
);
306 return src
[GET_SWZ(source
->Swizzle
, 0)];
311 * Fetch texel from texture. Use partial derivatives when possible.
314 fetch_texel(struct gl_context
*ctx
,
315 const struct gl_program_machine
*machine
,
316 const struct prog_instruction
*inst
,
317 const GLfloat texcoord
[4], GLfloat lodBias
,
320 const GLuint unit
= machine
->Samplers
[inst
->TexSrcUnit
];
322 /* Note: we only have the right derivatives for fragment input attribs.
324 if (machine
->NumDeriv
> 0 &&
325 inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
326 inst
->SrcReg
[0].Index
== VARYING_SLOT_TEX0
+ inst
->TexSrcUnit
) {
327 /* simple texture fetch for which we should have derivatives */
328 GLuint attr
= inst
->SrcReg
[0].Index
;
329 machine
->FetchTexelDeriv(ctx
, texcoord
,
330 machine
->DerivX
[attr
],
331 machine
->DerivY
[attr
],
332 lodBias
, unit
, color
);
335 machine
->FetchTexelLod(ctx
, texcoord
, lodBias
, unit
, color
);
341 * Test value against zero and return GT, LT, EQ or UN if NaN.
344 generate_cc(float value
)
347 return COND_UN
; /* NaN */
357 * Test if the ccMaskRule is satisfied by the given condition code.
358 * Used to mask destination writes according to the current condition code.
360 static inline GLboolean
361 test_cc(GLuint condCode
, GLuint ccMaskRule
)
363 switch (ccMaskRule
) {
364 case COND_EQ
: return (condCode
== COND_EQ
);
365 case COND_NE
: return (condCode
!= COND_EQ
);
366 case COND_LT
: return (condCode
== COND_LT
);
367 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
368 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
369 case COND_GT
: return (condCode
== COND_GT
);
370 case COND_TR
: return GL_TRUE
;
371 case COND_FL
: return GL_FALSE
;
372 default: return GL_TRUE
;
378 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
379 * or GL_FALSE to indicate result.
381 static inline GLboolean
382 eval_condition(const struct gl_program_machine
*machine
,
383 const struct prog_instruction
*inst
)
385 const GLuint swizzle
= inst
->DstReg
.CondSwizzle
;
386 const GLuint condMask
= inst
->DstReg
.CondMask
;
387 if (test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 0)], condMask
) ||
388 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 1)], condMask
) ||
389 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 2)], condMask
) ||
390 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 3)], condMask
)) {
401 * Store 4 floats into a register. Observe the instructions saturate and
402 * set-condition-code flags.
405 store_vector4(const struct prog_instruction
*inst
,
406 struct gl_program_machine
*machine
, const GLfloat value
[4])
408 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
409 const GLboolean clamp
= inst
->SaturateMode
== SATURATE_ZERO_ONE
;
410 GLuint writeMask
= dstReg
->WriteMask
;
411 GLfloat clampedValue
[4];
412 GLfloat
*dst
= get_dst_register_pointer(dstReg
, machine
);
415 if (value
[0] > 1.0e10
||
416 IS_INF_OR_NAN(value
[0]) ||
417 IS_INF_OR_NAN(value
[1]) ||
418 IS_INF_OR_NAN(value
[2]) || IS_INF_OR_NAN(value
[3]))
419 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
423 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
424 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
425 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
426 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
427 value
= clampedValue
;
430 if (dstReg
->CondMask
!= COND_TR
) {
431 /* condition codes may turn off some writes */
432 if (writeMask
& WRITEMASK_X
) {
433 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
435 writeMask
&= ~WRITEMASK_X
;
437 if (writeMask
& WRITEMASK_Y
) {
438 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
440 writeMask
&= ~WRITEMASK_Y
;
442 if (writeMask
& WRITEMASK_Z
) {
443 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
445 writeMask
&= ~WRITEMASK_Z
;
447 if (writeMask
& WRITEMASK_W
) {
448 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
450 writeMask
&= ~WRITEMASK_W
;
455 assert(!IS_INF_OR_NAN(value
[0]));
456 assert(!IS_INF_OR_NAN(value
[0]));
457 assert(!IS_INF_OR_NAN(value
[0]));
458 assert(!IS_INF_OR_NAN(value
[0]));
461 if (writeMask
& WRITEMASK_X
)
463 if (writeMask
& WRITEMASK_Y
)
465 if (writeMask
& WRITEMASK_Z
)
467 if (writeMask
& WRITEMASK_W
)
470 if (inst
->CondUpdate
) {
471 if (writeMask
& WRITEMASK_X
)
472 machine
->CondCodes
[0] = generate_cc(value
[0]);
473 if (writeMask
& WRITEMASK_Y
)
474 machine
->CondCodes
[1] = generate_cc(value
[1]);
475 if (writeMask
& WRITEMASK_Z
)
476 machine
->CondCodes
[2] = generate_cc(value
[2]);
477 if (writeMask
& WRITEMASK_W
)
478 machine
->CondCodes
[3] = generate_cc(value
[3]);
480 printf("CondCodes=(%s,%s,%s,%s) for:\n",
481 _mesa_condcode_string(machine
->CondCodes
[0]),
482 _mesa_condcode_string(machine
->CondCodes
[1]),
483 _mesa_condcode_string(machine
->CondCodes
[2]),
484 _mesa_condcode_string(machine
->CondCodes
[3]));
491 * Store 4 uints into a register. Observe the set-condition-code flags.
494 store_vector4ui(const struct prog_instruction
*inst
,
495 struct gl_program_machine
*machine
, const GLuint value
[4])
497 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
498 GLuint writeMask
= dstReg
->WriteMask
;
499 GLuint
*dst
= (GLuint
*) get_dst_register_pointer(dstReg
, machine
);
501 if (dstReg
->CondMask
!= COND_TR
) {
502 /* condition codes may turn off some writes */
503 if (writeMask
& WRITEMASK_X
) {
504 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 0)],
506 writeMask
&= ~WRITEMASK_X
;
508 if (writeMask
& WRITEMASK_Y
) {
509 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 1)],
511 writeMask
&= ~WRITEMASK_Y
;
513 if (writeMask
& WRITEMASK_Z
) {
514 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 2)],
516 writeMask
&= ~WRITEMASK_Z
;
518 if (writeMask
& WRITEMASK_W
) {
519 if (!test_cc(machine
->CondCodes
[GET_SWZ(dstReg
->CondSwizzle
, 3)],
521 writeMask
&= ~WRITEMASK_W
;
525 if (writeMask
& WRITEMASK_X
)
527 if (writeMask
& WRITEMASK_Y
)
529 if (writeMask
& WRITEMASK_Z
)
531 if (writeMask
& WRITEMASK_W
)
534 if (inst
->CondUpdate
) {
535 if (writeMask
& WRITEMASK_X
)
536 machine
->CondCodes
[0] = generate_cc((float)value
[0]);
537 if (writeMask
& WRITEMASK_Y
)
538 machine
->CondCodes
[1] = generate_cc((float)value
[1]);
539 if (writeMask
& WRITEMASK_Z
)
540 machine
->CondCodes
[2] = generate_cc((float)value
[2]);
541 if (writeMask
& WRITEMASK_W
)
542 machine
->CondCodes
[3] = generate_cc((float)value
[3]);
544 printf("CondCodes=(%s,%s,%s,%s) for:\n",
545 _mesa_condcode_string(machine
->CondCodes
[0]),
546 _mesa_condcode_string(machine
->CondCodes
[1]),
547 _mesa_condcode_string(machine
->CondCodes
[2]),
548 _mesa_condcode_string(machine
->CondCodes
[3]));
556 * Execute the given vertex/fragment program.
558 * \param ctx rendering context
559 * \param program the program to execute
560 * \param machine machine state (must be initialized)
561 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
564 _mesa_execute_program(struct gl_context
* ctx
,
565 const struct gl_program
*program
,
566 struct gl_program_machine
*machine
)
568 const GLuint numInst
= program
->NumInstructions
;
569 const GLuint maxExec
= 65536;
570 GLuint pc
, numExec
= 0;
572 machine
->CurProgram
= program
;
575 printf("execute program %u --------------------\n", program
->Id
);
578 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
579 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
582 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
585 for (pc
= 0; pc
< numInst
; pc
++) {
586 const struct prog_instruction
*inst
= program
->Instructions
+ pc
;
589 _mesa_print_instruction(inst
);
592 switch (inst
->Opcode
) {
595 GLfloat a
[4], result
[4];
596 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
597 result
[0] = FABSF(a
[0]);
598 result
[1] = FABSF(a
[1]);
599 result
[2] = FABSF(a
[2]);
600 result
[3] = FABSF(a
[3]);
601 store_vector4(inst
, machine
, result
);
606 GLfloat a
[4], b
[4], result
[4];
607 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
608 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
609 result
[0] = a
[0] + b
[0];
610 result
[1] = a
[1] + b
[1];
611 result
[2] = a
[2] + b
[2];
612 result
[3] = a
[3] + b
[3];
613 store_vector4(inst
, machine
, result
);
615 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
616 result
[0], result
[1], result
[2], result
[3],
617 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
624 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
625 machine
->AddressReg
[0][0] = IFLOOR(t
[0]);
627 printf("ARL %d\n", machine
->AddressReg
[0][0]);
633 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
637 /* subtract 1 here since pc is incremented by for(pc) loop */
638 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
640 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
642 case OPCODE_BGNSUB
: /* begin subroutine */
644 case OPCODE_ENDSUB
: /* end subroutine */
646 case OPCODE_BRK
: /* break out of loop (conditional) */
647 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
649 if (eval_condition(machine
, inst
)) {
650 /* break out of loop */
651 /* pc++ at end of for-loop will put us after the ENDLOOP inst */
652 pc
= inst
->BranchTarget
;
655 case OPCODE_CONT
: /* continue loop (conditional) */
656 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
658 if (eval_condition(machine
, inst
)) {
659 /* continue at ENDLOOP */
660 /* Subtract 1 here since we'll do pc++ at end of for-loop */
661 pc
= inst
->BranchTarget
- 1;
664 case OPCODE_CAL
: /* Call subroutine (conditional) */
665 if (eval_condition(machine
, inst
)) {
666 /* call the subroutine */
667 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
668 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
670 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1; /* next inst */
671 /* Subtract 1 here since we'll do pc++ at end of for-loop */
672 pc
= inst
->BranchTarget
- 1;
677 GLfloat a
[4], b
[4], c
[4], result
[4];
678 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
679 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
680 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
681 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
682 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
683 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
684 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
685 store_vector4(inst
, machine
, result
);
687 printf("CMP (%g %g %g %g) = (%g %g %g %g) < 0 ? (%g %g %g %g) : (%g %g %g %g)\n",
688 result
[0], result
[1], result
[2], result
[3],
689 a
[0], a
[1], a
[2], a
[3],
690 b
[0], b
[1], b
[2], b
[3],
691 c
[0], c
[1], c
[2], c
[3]);
697 GLfloat a
[4], result
[4];
698 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
699 result
[0] = result
[1] = result
[2] = result
[3]
700 = (GLfloat
) cos(a
[0]);
701 store_vector4(inst
, machine
, result
);
704 case OPCODE_DDX
: /* Partial derivative with respect to X */
707 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
709 store_vector4(inst
, machine
, result
);
712 case OPCODE_DDY
: /* Partial derivative with respect to Y */
715 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
717 store_vector4(inst
, machine
, result
);
722 GLfloat a
[4], b
[4], result
[4];
723 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
724 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
725 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
);
726 store_vector4(inst
, machine
, result
);
728 printf("DP2 %g = (%g %g) . (%g %g)\n",
729 result
[0], a
[0], a
[1], b
[0], b
[1]);
735 GLfloat a
[4], b
[4], result
[4];
736 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
737 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
738 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
739 store_vector4(inst
, machine
, result
);
741 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
742 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
748 GLfloat a
[4], b
[4], result
[4];
749 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
750 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
751 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
, b
);
752 store_vector4(inst
, machine
, result
);
754 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
755 result
[0], a
[0], a
[1], a
[2], a
[3],
756 b
[0], b
[1], b
[2], b
[3]);
762 GLfloat a
[4], b
[4], result
[4];
763 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
764 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
765 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
) + b
[3];
766 store_vector4(inst
, machine
, result
);
769 case OPCODE_DST
: /* Distance vector */
771 GLfloat a
[4], b
[4], result
[4];
772 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
773 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
775 result
[1] = a
[1] * b
[1];
778 store_vector4(inst
, machine
, result
);
783 GLfloat t
[4], q
[4], floor_t0
;
784 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
785 floor_t0
= FLOORF(t
[0]);
786 if (floor_t0
> FLT_MAX_EXP
) {
787 SET_POS_INFINITY(q
[0]);
788 SET_POS_INFINITY(q
[2]);
790 else if (floor_t0
< FLT_MIN_EXP
) {
795 q
[0] = LDEXPF(1.0, (int) floor_t0
);
796 /* Note: GL_NV_vertex_program expects
797 * result.z = result.x * APPX(result.y)
798 * We do what the ARB extension says.
800 q
[2] = (GLfloat
) pow(2.0, t
[0]);
802 q
[1] = t
[0] - floor_t0
;
804 store_vector4( inst
, machine
, q
);
807 case OPCODE_EX2
: /* Exponential base 2 */
809 GLfloat a
[4], result
[4], val
;
810 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
811 val
= (GLfloat
) pow(2.0, a
[0]);
813 if (IS_INF_OR_NAN(val))
816 result
[0] = result
[1] = result
[2] = result
[3] = val
;
817 store_vector4(inst
, machine
, result
);
822 GLfloat a
[4], result
[4];
823 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
824 result
[0] = FLOORF(a
[0]);
825 result
[1] = FLOORF(a
[1]);
826 result
[2] = FLOORF(a
[2]);
827 result
[3] = FLOORF(a
[3]);
828 store_vector4(inst
, machine
, result
);
833 GLfloat a
[4], result
[4];
834 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
835 result
[0] = a
[0] - FLOORF(a
[0]);
836 result
[1] = a
[1] - FLOORF(a
[1]);
837 result
[2] = a
[2] - FLOORF(a
[2]);
838 result
[3] = a
[3] - FLOORF(a
[3]);
839 store_vector4(inst
, machine
, result
);
845 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
847 program
->Instructions
[inst
->BranchTarget
].Opcode
850 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
852 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
853 cond
= (a
[0] != 0.0);
856 cond
= eval_condition(machine
, inst
);
859 printf("IF: %d\n", cond
);
863 /* do if-clause (just continue execution) */
866 /* go to the instruction after ELSE or ENDIF */
867 assert(inst
->BranchTarget
>= 0);
868 pc
= inst
->BranchTarget
;
874 ASSERT(program
->Instructions
[inst
->BranchTarget
].Opcode
876 assert(inst
->BranchTarget
>= 0);
877 pc
= inst
->BranchTarget
;
882 case OPCODE_KIL_NV
: /* NV_f_p only (conditional) */
883 if (eval_condition(machine
, inst
)) {
887 case OPCODE_KIL
: /* ARB_f_p only */
890 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
892 printf("KIL if (%g %g %g %g) <= 0.0\n",
893 a
[0], a
[1], a
[2], a
[3]);
896 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
901 case OPCODE_LG2
: /* log base 2 */
903 GLfloat a
[4], result
[4], val
;
904 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
905 /* The fast LOG2 macro doesn't meet the precision requirements.
911 val
= (float)(log(a
[0]) * 1.442695F
);
913 result
[0] = result
[1] = result
[2] = result
[3] = val
;
914 store_vector4(inst
, machine
, result
);
919 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
920 GLfloat a
[4], result
[4];
921 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
922 a
[0] = MAX2(a
[0], 0.0F
);
923 a
[1] = MAX2(a
[1], 0.0F
);
924 /* XXX ARB version clamps a[3], NV version doesn't */
925 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
928 /* XXX we could probably just use pow() here */
930 if (a
[1] == 0.0 && a
[3] == 0.0)
933 result
[2] = (GLfloat
) pow(a
[1], a
[3]);
939 store_vector4(inst
, machine
, result
);
941 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
942 result
[0], result
[1], result
[2], result
[3],
943 a
[0], a
[1], a
[2], a
[3]);
949 GLfloat t
[4], q
[4], abs_t0
;
950 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
951 abs_t0
= FABSF(t
[0]);
952 if (abs_t0
!= 0.0F
) {
953 if (IS_INF_OR_NAN(abs_t0
))
955 SET_POS_INFINITY(q
[0]);
957 SET_POS_INFINITY(q
[2]);
961 GLfloat mantissa
= FREXPF(t
[0], &exponent
);
962 q
[0] = (GLfloat
) (exponent
- 1);
963 q
[1] = (GLfloat
) (2.0 * mantissa
); /* map [.5, 1) -> [1, 2) */
965 /* The fast LOG2 macro doesn't meet the precision
968 q
[2] = (float)(log(t
[0]) * 1.442695F
);
972 SET_NEG_INFINITY(q
[0]);
974 SET_NEG_INFINITY(q
[2]);
977 store_vector4(inst
, machine
, q
);
982 GLfloat a
[4], b
[4], c
[4], result
[4];
983 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
984 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
985 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
986 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
987 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
988 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
989 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
990 store_vector4(inst
, machine
, result
);
992 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
993 "(%g %g %g %g), (%g %g %g %g)\n",
994 result
[0], result
[1], result
[2], result
[3],
995 a
[0], a
[1], a
[2], a
[3],
996 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1002 GLfloat a
[4], b
[4], c
[4], result
[4];
1003 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1004 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1005 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1006 result
[0] = a
[0] * b
[0] + c
[0];
1007 result
[1] = a
[1] * b
[1] + c
[1];
1008 result
[2] = a
[2] * b
[2] + c
[2];
1009 result
[3] = a
[3] * b
[3] + c
[3];
1010 store_vector4(inst
, machine
, result
);
1012 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
1013 "(%g %g %g %g) + (%g %g %g %g)\n",
1014 result
[0], result
[1], result
[2], result
[3],
1015 a
[0], a
[1], a
[2], a
[3],
1016 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1022 GLfloat a
[4], b
[4], result
[4];
1023 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1024 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1025 result
[0] = MAX2(a
[0], b
[0]);
1026 result
[1] = MAX2(a
[1], b
[1]);
1027 result
[2] = MAX2(a
[2], b
[2]);
1028 result
[3] = MAX2(a
[3], b
[3]);
1029 store_vector4(inst
, machine
, result
);
1031 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
1032 result
[0], result
[1], result
[2], result
[3],
1033 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1039 GLfloat a
[4], b
[4], result
[4];
1040 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1041 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1042 result
[0] = MIN2(a
[0], b
[0]);
1043 result
[1] = MIN2(a
[1], b
[1]);
1044 result
[2] = MIN2(a
[2], b
[2]);
1045 result
[3] = MIN2(a
[3], b
[3]);
1046 store_vector4(inst
, machine
, result
);
1052 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
1053 store_vector4(inst
, machine
, result
);
1055 printf("MOV (%g %g %g %g)\n",
1056 result
[0], result
[1], result
[2], result
[3]);
1062 GLfloat a
[4], b
[4], result
[4];
1063 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1064 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1065 result
[0] = a
[0] * b
[0];
1066 result
[1] = a
[1] * b
[1];
1067 result
[2] = a
[2] * b
[2];
1068 result
[3] = a
[3] * b
[3];
1069 store_vector4(inst
, machine
, result
);
1071 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1072 result
[0], result
[1], result
[2], result
[3],
1073 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1079 GLfloat a
[4], result
[4];
1080 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1084 result
[3] = _mesa_noise1(a
[0]);
1085 store_vector4(inst
, machine
, result
);
1090 GLfloat a
[4], result
[4];
1091 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1094 result
[2] = result
[3] = _mesa_noise2(a
[0], a
[1]);
1095 store_vector4(inst
, machine
, result
);
1100 GLfloat a
[4], result
[4];
1101 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1105 result
[3] = _mesa_noise3(a
[0], a
[1], a
[2]);
1106 store_vector4(inst
, machine
, result
);
1111 GLfloat a
[4], result
[4];
1112 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1116 result
[3] = _mesa_noise4(a
[0], a
[1], a
[2], a
[3]);
1117 store_vector4(inst
, machine
, result
);
1122 case OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
1127 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1128 hx
= _mesa_float_to_half(a
[0]);
1129 hy
= _mesa_float_to_half(a
[1]);
1133 result
[3] = hx
| (hy
<< 16);
1134 store_vector4ui(inst
, machine
, result
);
1137 case OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
1140 GLuint result
[4], usx
, usy
;
1141 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1142 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1143 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1144 usx
= F_TO_I(a
[0] * 65535.0F
);
1145 usy
= F_TO_I(a
[1] * 65535.0F
);
1149 result
[3] = usx
| (usy
<< 16);
1150 store_vector4ui(inst
, machine
, result
);
1153 case OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
1156 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1157 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1158 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
1159 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
1160 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
1161 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
1162 ubx
= F_TO_I(127.0F
* a
[0] + 128.0F
);
1163 uby
= F_TO_I(127.0F
* a
[1] + 128.0F
);
1164 ubz
= F_TO_I(127.0F
* a
[2] + 128.0F
);
1165 ubw
= F_TO_I(127.0F
* a
[3] + 128.0F
);
1169 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1170 store_vector4ui(inst
, machine
, result
);
1173 case OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
1176 GLuint result
[4], ubx
, uby
, ubz
, ubw
;
1177 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1178 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1179 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1180 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
1181 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
1182 ubx
= F_TO_I(255.0F
* a
[0]);
1183 uby
= F_TO_I(255.0F
* a
[1]);
1184 ubz
= F_TO_I(255.0F
* a
[2]);
1185 ubw
= F_TO_I(255.0F
* a
[3]);
1189 result
[3] = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1190 store_vector4ui(inst
, machine
, result
);
1195 GLfloat a
[4], b
[4], result
[4];
1196 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1197 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
1198 result
[0] = result
[1] = result
[2] = result
[3]
1199 = (GLfloat
) pow(a
[0], b
[0]);
1200 store_vector4(inst
, machine
, result
);
1206 GLfloat a
[4], result
[4];
1207 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1211 else if (IS_INF_OR_NAN(a
[0]))
1212 printf("RCP(inf)\n");
1214 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
1215 store_vector4(inst
, machine
, result
);
1218 case OPCODE_RET
: /* return from subroutine (conditional) */
1219 if (eval_condition(machine
, inst
)) {
1220 if (machine
->StackDepth
== 0) {
1221 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
1223 /* subtract one because of pc++ in the for loop */
1224 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
1227 case OPCODE_RFL
: /* reflection vector */
1229 GLfloat axis
[4], dir
[4], result
[4], tmpX
, tmpW
;
1230 fetch_vector4(&inst
->SrcReg
[0], machine
, axis
);
1231 fetch_vector4(&inst
->SrcReg
[1], machine
, dir
);
1232 tmpW
= DOT3(axis
, axis
);
1233 tmpX
= (2.0F
* DOT3(axis
, dir
)) / tmpW
;
1234 result
[0] = tmpX
* axis
[0] - dir
[0];
1235 result
[1] = tmpX
* axis
[1] - dir
[1];
1236 result
[2] = tmpX
* axis
[2] - dir
[2];
1237 /* result[3] is never written! XXX enforce in parser! */
1238 store_vector4(inst
, machine
, result
);
1241 case OPCODE_RSQ
: /* 1 / sqrt() */
1243 GLfloat a
[4], result
[4];
1244 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1246 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1247 store_vector4(inst
, machine
, result
);
1249 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
1253 case OPCODE_SCS
: /* sine and cos */
1255 GLfloat a
[4], result
[4];
1256 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1257 result
[0] = (GLfloat
) cos(a
[0]);
1258 result
[1] = (GLfloat
) sin(a
[0]);
1259 result
[2] = 0.0; /* undefined! */
1260 result
[3] = 0.0; /* undefined! */
1261 store_vector4(inst
, machine
, result
);
1264 case OPCODE_SEQ
: /* set on equal */
1266 GLfloat a
[4], b
[4], result
[4];
1267 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1268 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1269 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1270 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1271 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1272 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1273 store_vector4(inst
, machine
, result
);
1275 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1276 result
[0], result
[1], result
[2], result
[3],
1277 a
[0], a
[1], a
[2], a
[3],
1278 b
[0], b
[1], b
[2], b
[3]);
1282 case OPCODE_SGE
: /* set on greater or equal */
1284 GLfloat a
[4], b
[4], result
[4];
1285 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1286 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1287 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1288 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1289 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1290 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1291 store_vector4(inst
, machine
, result
);
1293 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1294 result
[0], result
[1], result
[2], result
[3],
1295 a
[0], a
[1], a
[2], a
[3],
1296 b
[0], b
[1], b
[2], b
[3]);
1300 case OPCODE_SGT
: /* set on greater */
1302 GLfloat a
[4], b
[4], result
[4];
1303 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1304 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1305 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1306 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1307 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1308 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1309 store_vector4(inst
, machine
, result
);
1311 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1312 result
[0], result
[1], result
[2], result
[3],
1313 a
[0], a
[1], a
[2], a
[3],
1314 b
[0], b
[1], b
[2], b
[3]);
1320 GLfloat a
[4], result
[4];
1321 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1322 result
[0] = result
[1] = result
[2] = result
[3]
1323 = (GLfloat
) sin(a
[0]);
1324 store_vector4(inst
, machine
, result
);
1327 case OPCODE_SLE
: /* set on less or equal */
1329 GLfloat a
[4], b
[4], result
[4];
1330 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1331 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1332 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1333 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1334 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1335 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1336 store_vector4(inst
, machine
, result
);
1338 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1339 result
[0], result
[1], result
[2], result
[3],
1340 a
[0], a
[1], a
[2], a
[3],
1341 b
[0], b
[1], b
[2], b
[3]);
1345 case OPCODE_SLT
: /* set on less */
1347 GLfloat a
[4], b
[4], result
[4];
1348 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1349 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1350 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1351 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1352 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1353 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1354 store_vector4(inst
, machine
, result
);
1356 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1357 result
[0], result
[1], result
[2], result
[3],
1358 a
[0], a
[1], a
[2], a
[3],
1359 b
[0], b
[1], b
[2], b
[3]);
1363 case OPCODE_SNE
: /* set on not equal */
1365 GLfloat a
[4], b
[4], result
[4];
1366 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1367 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1368 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1369 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1370 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1371 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1372 store_vector4(inst
, machine
, result
);
1374 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1375 result
[0], result
[1], result
[2], result
[3],
1376 a
[0], a
[1], a
[2], a
[3],
1377 b
[0], b
[1], b
[2], b
[3]);
1381 case OPCODE_SSG
: /* set sign (-1, 0 or +1) */
1383 GLfloat a
[4], result
[4];
1384 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1385 result
[0] = (GLfloat
) ((a
[0] > 0.0F
) - (a
[0] < 0.0F
));
1386 result
[1] = (GLfloat
) ((a
[1] > 0.0F
) - (a
[1] < 0.0F
));
1387 result
[2] = (GLfloat
) ((a
[2] > 0.0F
) - (a
[2] < 0.0F
));
1388 result
[3] = (GLfloat
) ((a
[3] > 0.0F
) - (a
[3] < 0.0F
));
1389 store_vector4(inst
, machine
, result
);
1394 GLfloat a
[4], b
[4], result
[4];
1395 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1396 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1397 result
[0] = a
[0] - b
[0];
1398 result
[1] = a
[1] - b
[1];
1399 result
[2] = a
[2] - b
[2];
1400 result
[3] = a
[3] - b
[3];
1401 store_vector4(inst
, machine
, result
);
1403 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1404 result
[0], result
[1], result
[2], result
[3],
1405 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1409 case OPCODE_SWZ
: /* extended swizzle */
1411 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1412 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
1415 for (i
= 0; i
< 4; i
++) {
1416 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1417 if (swz
== SWIZZLE_ZERO
)
1419 else if (swz
== SWIZZLE_ONE
)
1424 result
[i
] = src
[swz
];
1426 if (source
->Negate
& (1 << i
))
1427 result
[i
] = -result
[i
];
1429 store_vector4(inst
, machine
, result
);
1432 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1433 /* Simple texel lookup */
1435 GLfloat texcoord
[4], color
[4];
1436 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1438 /* For TEX, texcoord.Q should not be used and its value should not
1439 * matter (at most, we pass coord.xyz to texture3D() in GLSL).
1440 * Set Q=1 so that FetchTexelDeriv() doesn't get a garbage value
1441 * which is effectively what happens when the texcoord swizzle
1446 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1449 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1450 color
[0], color
[1], color
[2], color
[3],
1452 texcoord
[0], texcoord
[1], texcoord
[2], texcoord
[3]);
1454 store_vector4(inst
, machine
, color
);
1457 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1458 /* Texel lookup with LOD bias */
1460 GLfloat texcoord
[4], color
[4], lodBias
;
1462 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1464 /* texcoord[3] is the bias to add to lambda */
1465 lodBias
= texcoord
[3];
1467 fetch_texel(ctx
, machine
, inst
, texcoord
, lodBias
, color
);
1470 printf("TXB (%g, %g, %g, %g) = texture[%d][%g %g %g %g]"
1472 color
[0], color
[1], color
[2], color
[3],
1481 store_vector4(inst
, machine
, color
);
1484 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1485 /* Texture lookup w/ partial derivatives for LOD */
1487 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1488 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1489 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1490 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1491 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1493 inst
->TexSrcUnit
, color
);
1494 store_vector4(inst
, machine
, color
);
1498 /* Texel lookup with explicit LOD */
1500 GLfloat texcoord
[4], color
[4], lod
;
1502 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1504 /* texcoord[3] is the LOD */
1507 machine
->FetchTexelLod(ctx
, texcoord
, lod
,
1508 machine
->Samplers
[inst
->TexSrcUnit
], color
);
1510 store_vector4(inst
, machine
, color
);
1513 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1514 /* Texture lookup w/ projective divide */
1516 GLfloat texcoord
[4], color
[4];
1518 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1519 /* Not so sure about this test - if texcoord[3] is
1520 * zero, we'd probably be fine except for an ASSERT in
1521 * IROUND_POS() which gets triggered by the inf values created.
1523 if (texcoord
[3] != 0.0) {
1524 texcoord
[0] /= texcoord
[3];
1525 texcoord
[1] /= texcoord
[3];
1526 texcoord
[2] /= texcoord
[3];
1529 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1531 store_vector4(inst
, machine
, color
);
1534 case OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1535 /* Texture lookup w/ projective divide, as above, but do not
1536 * do the divide by w if sampling from a cube map.
1539 GLfloat texcoord
[4], color
[4];
1541 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1542 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1543 texcoord
[3] != 0.0) {
1544 texcoord
[0] /= texcoord
[3];
1545 texcoord
[1] /= texcoord
[3];
1546 texcoord
[2] /= texcoord
[3];
1549 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1551 store_vector4(inst
, machine
, color
);
1554 case OPCODE_TRUNC
: /* truncate toward zero */
1556 GLfloat a
[4], result
[4];
1557 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1558 result
[0] = (GLfloat
) (GLint
) a
[0];
1559 result
[1] = (GLfloat
) (GLint
) a
[1];
1560 result
[2] = (GLfloat
) (GLint
) a
[2];
1561 result
[3] = (GLfloat
) (GLint
) a
[3];
1562 store_vector4(inst
, machine
, result
);
1565 case OPCODE_UP2H
: /* unpack two 16-bit floats */
1567 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1572 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1573 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1574 store_vector4(inst
, machine
, result
);
1577 case OPCODE_UP2US
: /* unpack two GLushorts */
1579 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1584 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1585 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1586 store_vector4(inst
, machine
, result
);
1589 case OPCODE_UP4B
: /* unpack four GLbytes */
1591 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1593 result
[0] = (((raw
>> 0) & 0xff) - 128) / 127.0F
;
1594 result
[1] = (((raw
>> 8) & 0xff) - 128) / 127.0F
;
1595 result
[2] = (((raw
>> 16) & 0xff) - 128) / 127.0F
;
1596 result
[3] = (((raw
>> 24) & 0xff) - 128) / 127.0F
;
1597 store_vector4(inst
, machine
, result
);
1600 case OPCODE_UP4UB
: /* unpack four GLubytes */
1602 const GLuint raw
= fetch_vector1ui(&inst
->SrcReg
[0], machine
);
1604 result
[0] = ((raw
>> 0) & 0xff) / 255.0F
;
1605 result
[1] = ((raw
>> 8) & 0xff) / 255.0F
;
1606 result
[2] = ((raw
>> 16) & 0xff) / 255.0F
;
1607 result
[3] = ((raw
>> 24) & 0xff) / 255.0F
;
1608 store_vector4(inst
, machine
, result
);
1611 case OPCODE_XPD
: /* cross product */
1613 GLfloat a
[4], b
[4], result
[4];
1614 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1615 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1616 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1617 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1618 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1620 store_vector4(inst
, machine
, result
);
1622 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1623 result
[0], result
[1], result
[2], result
[3],
1624 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1628 case OPCODE_X2D
: /* 2-D matrix transform */
1630 GLfloat a
[4], b
[4], c
[4], result
[4];
1631 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1632 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1633 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1634 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1635 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1636 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1637 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1638 store_vector4(inst
, machine
, result
);
1644 _mesa_problem(ctx
, "Bad opcode %d in _mesa_execute_program",
1646 return GL_TRUE
; /* return value doesn't matter */
1650 if (numExec
> maxExec
) {
1651 static GLboolean reported
= GL_FALSE
;
1653 _mesa_problem(ctx
, "Infinite loop detected in fragment program");