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.
39 #include "main/errors.h"
40 #include "main/glheader.h"
41 #include "main/macros.h"
42 #include "main/mtypes.h"
43 #include "prog_execute.h"
44 #include "prog_instruction.h"
45 #include "prog_parameter.h"
46 #include "prog_print.h"
47 #include "prog_noise.h"
55 * Set x to positive or negative infinity.
57 #define SET_POS_INFINITY(x) \
63 #define SET_NEG_INFINITY(x) \
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
};
77 * Return a pointer to the 4-element float vector specified by the given
80 static inline const GLfloat
*
81 get_src_register_pointer(const struct prog_src_register
*source
,
82 const struct gl_program_machine
*machine
)
84 const struct gl_program
*prog
= machine
->CurProgram
;
85 GLint reg
= source
->Index
;
87 if (source
->RelAddr
) {
88 /* add address register value to src index/offset */
89 reg
+= machine
->AddressReg
[0][0];
95 switch (source
->File
) {
96 case PROGRAM_TEMPORARY
:
97 if (reg
>= MAX_PROGRAM_TEMPS
)
99 return machine
->Temporaries
[reg
];
102 if (prog
->Target
== GL_VERTEX_PROGRAM_ARB
) {
103 if (reg
>= VERT_ATTRIB_MAX
)
105 return machine
->VertAttribs
[reg
];
108 if (reg
>= VARYING_SLOT_MAX
)
110 return machine
->Attribs
[reg
][machine
->CurElement
];
114 if (reg
>= MAX_PROGRAM_OUTPUTS
)
116 return machine
->Outputs
[reg
];
118 case PROGRAM_STATE_VAR
:
120 case PROGRAM_CONSTANT
:
122 case PROGRAM_UNIFORM
: {
123 if (reg
>= (GLint
) prog
->Parameters
->NumParameters
)
126 unsigned pvo
= prog
->Parameters
->ParameterValueOffset
[reg
];
127 return (GLfloat
*) prog
->Parameters
->ParameterValues
+ pvo
;
129 case PROGRAM_SYSTEM_VALUE
:
130 assert(reg
< (GLint
) ARRAY_SIZE(machine
->SystemValues
));
131 return machine
->SystemValues
[reg
];
135 "Invalid src register file %d in get_src_register_pointer()",
143 * Return a pointer to the 4-element float vector specified by the given
144 * destination register.
146 static inline GLfloat
*
147 get_dst_register_pointer(const struct prog_dst_register
*dest
,
148 struct gl_program_machine
*machine
)
150 static GLfloat dummyReg
[4];
151 GLint reg
= dest
->Index
;
154 /* add address register value to src index/offset */
155 reg
+= machine
->AddressReg
[0][0];
161 switch (dest
->File
) {
162 case PROGRAM_TEMPORARY
:
163 if (reg
>= MAX_PROGRAM_TEMPS
)
165 return machine
->Temporaries
[reg
];
168 if (reg
>= MAX_PROGRAM_OUTPUTS
)
170 return machine
->Outputs
[reg
];
174 "Invalid dest register file %d in get_dst_register_pointer()",
183 * Fetch a 4-element float vector from the given source register.
184 * Apply swizzling and negating as needed.
187 fetch_vector4(const struct prog_src_register
*source
,
188 const struct gl_program_machine
*machine
, GLfloat result
[4])
190 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
192 if (source
->Swizzle
== SWIZZLE_NOOP
) {
194 COPY_4V(result
, src
);
197 assert(GET_SWZ(source
->Swizzle
, 0) <= 3);
198 assert(GET_SWZ(source
->Swizzle
, 1) <= 3);
199 assert(GET_SWZ(source
->Swizzle
, 2) <= 3);
200 assert(GET_SWZ(source
->Swizzle
, 3) <= 3);
201 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
202 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
203 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
204 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
207 if (source
->Negate
) {
208 assert(source
->Negate
== NEGATE_XYZW
);
209 result
[0] = -result
[0];
210 result
[1] = -result
[1];
211 result
[2] = -result
[2];
212 result
[3] = -result
[3];
216 assert(!IS_INF_OR_NAN(result
[0]));
217 assert(!IS_INF_OR_NAN(result
[0]));
218 assert(!IS_INF_OR_NAN(result
[0]));
219 assert(!IS_INF_OR_NAN(result
[0]));
225 * Fetch the derivative with respect to X or Y for the given register.
226 * XXX this currently only works for fragment program input attribs.
229 fetch_vector4_deriv(const struct prog_src_register
*source
,
230 const struct gl_program_machine
*machine
,
231 char xOrY
, GLfloat result
[4])
233 if (source
->File
== PROGRAM_INPUT
&&
234 source
->Index
< (GLint
) machine
->NumDeriv
) {
235 const GLint col
= machine
->CurElement
;
236 const GLfloat w
= machine
->Attribs
[VARYING_SLOT_POS
][col
][3];
237 const GLfloat invQ
= 1.0f
/ w
;
241 deriv
[0] = machine
->DerivX
[source
->Index
][0] * invQ
;
242 deriv
[1] = machine
->DerivX
[source
->Index
][1] * invQ
;
243 deriv
[2] = machine
->DerivX
[source
->Index
][2] * invQ
;
244 deriv
[3] = machine
->DerivX
[source
->Index
][3] * invQ
;
247 deriv
[0] = machine
->DerivY
[source
->Index
][0] * invQ
;
248 deriv
[1] = machine
->DerivY
[source
->Index
][1] * invQ
;
249 deriv
[2] = machine
->DerivY
[source
->Index
][2] * invQ
;
250 deriv
[3] = machine
->DerivY
[source
->Index
][3] * invQ
;
253 result
[0] = deriv
[GET_SWZ(source
->Swizzle
, 0)];
254 result
[1] = deriv
[GET_SWZ(source
->Swizzle
, 1)];
255 result
[2] = deriv
[GET_SWZ(source
->Swizzle
, 2)];
256 result
[3] = deriv
[GET_SWZ(source
->Swizzle
, 3)];
258 if (source
->Negate
) {
259 assert(source
->Negate
== NEGATE_XYZW
);
260 result
[0] = -result
[0];
261 result
[1] = -result
[1];
262 result
[2] = -result
[2];
263 result
[3] = -result
[3];
267 ASSIGN_4V(result
, 0.0, 0.0, 0.0, 0.0);
273 * As above, but only return result[0] element.
276 fetch_vector1(const struct prog_src_register
*source
,
277 const struct gl_program_machine
*machine
, GLfloat result
[4])
279 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
281 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
283 if (source
->Negate
) {
284 result
[0] = -result
[0];
290 * Fetch texel from texture. Use partial derivatives when possible.
293 fetch_texel(struct gl_context
*ctx
,
294 const struct gl_program_machine
*machine
,
295 const struct prog_instruction
*inst
,
296 const GLfloat texcoord
[4], GLfloat lodBias
,
299 const GLuint unit
= machine
->Samplers
[inst
->TexSrcUnit
];
301 /* Note: we only have the right derivatives for fragment input attribs.
303 if (machine
->NumDeriv
> 0 &&
304 inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
305 inst
->SrcReg
[0].Index
== VARYING_SLOT_TEX0
+ inst
->TexSrcUnit
) {
306 /* simple texture fetch for which we should have derivatives */
307 GLuint attr
= inst
->SrcReg
[0].Index
;
308 machine
->FetchTexelDeriv(ctx
, texcoord
,
309 machine
->DerivX
[attr
],
310 machine
->DerivY
[attr
],
311 lodBias
, unit
, color
);
314 machine
->FetchTexelLod(ctx
, texcoord
, lodBias
, unit
, color
);
320 * Store 4 floats into a register. Observe the instructions saturate and
321 * set-condition-code flags.
324 store_vector4(const struct prog_instruction
*inst
,
325 struct gl_program_machine
*machine
, const GLfloat value
[4])
327 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
328 const GLboolean clamp
= inst
->Saturate
;
329 GLuint writeMask
= dstReg
->WriteMask
;
330 GLfloat clampedValue
[4];
331 GLfloat
*dst
= get_dst_register_pointer(dstReg
, machine
);
334 if (value
[0] > 1.0e10
||
335 IS_INF_OR_NAN(value
[0]) ||
336 IS_INF_OR_NAN(value
[1]) ||
337 IS_INF_OR_NAN(value
[2]) || IS_INF_OR_NAN(value
[3]))
338 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
342 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
343 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
344 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
345 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
346 value
= clampedValue
;
350 assert(!IS_INF_OR_NAN(value
[0]));
351 assert(!IS_INF_OR_NAN(value
[0]));
352 assert(!IS_INF_OR_NAN(value
[0]));
353 assert(!IS_INF_OR_NAN(value
[0]));
356 if (writeMask
& WRITEMASK_X
)
358 if (writeMask
& WRITEMASK_Y
)
360 if (writeMask
& WRITEMASK_Z
)
362 if (writeMask
& WRITEMASK_W
)
368 * Execute the given vertex/fragment program.
370 * \param ctx rendering context
371 * \param program the program to execute
372 * \param machine machine state (must be initialized)
373 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
376 _mesa_execute_program(struct gl_context
* ctx
,
377 const struct gl_program
*program
,
378 struct gl_program_machine
*machine
)
380 const GLuint numInst
= program
->arb
.NumInstructions
;
381 const GLuint maxExec
= 65536;
382 GLuint pc
, numExec
= 0;
384 machine
->CurProgram
= program
;
387 printf("execute program %u --------------------\n", program
->Id
);
390 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
391 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
394 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
397 for (pc
= 0; pc
< numInst
; pc
++) {
398 const struct prog_instruction
*inst
= program
->arb
.Instructions
+ pc
;
401 _mesa_print_instruction(inst
);
404 switch (inst
->Opcode
) {
407 GLfloat a
[4], result
[4];
408 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
409 result
[0] = fabsf(a
[0]);
410 result
[1] = fabsf(a
[1]);
411 result
[2] = fabsf(a
[2]);
412 result
[3] = fabsf(a
[3]);
413 store_vector4(inst
, machine
, result
);
418 GLfloat a
[4], b
[4], result
[4];
419 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
420 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
421 result
[0] = a
[0] + b
[0];
422 result
[1] = a
[1] + b
[1];
423 result
[2] = a
[2] + b
[2];
424 result
[3] = a
[3] + b
[3];
425 store_vector4(inst
, machine
, result
);
427 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
428 result
[0], result
[1], result
[2], result
[3],
429 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
436 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
437 machine
->AddressReg
[0][0] = IFLOOR(t
[0]);
439 printf("ARL %d\n", machine
->AddressReg
[0][0]);
445 assert(program
->arb
.Instructions
[inst
->BranchTarget
].Opcode
449 /* subtract 1 here since pc is incremented by for(pc) loop */
450 assert(program
->arb
.Instructions
[inst
->BranchTarget
].Opcode
452 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
454 case OPCODE_BGNSUB
: /* begin subroutine */
456 case OPCODE_ENDSUB
: /* end subroutine */
458 case OPCODE_BRK
: /* break out of loop (conditional) */
459 assert(program
->arb
.Instructions
[inst
->BranchTarget
].Opcode
461 /* break out of loop */
462 /* pc++ at end of for-loop will put us after the ENDLOOP inst */
463 pc
= inst
->BranchTarget
;
465 case OPCODE_CONT
: /* continue loop (conditional) */
466 assert(program
->arb
.Instructions
[inst
->BranchTarget
].Opcode
468 /* continue at ENDLOOP */
469 /* Subtract 1 here since we'll do pc++ at end of for-loop */
470 pc
= inst
->BranchTarget
- 1;
472 case OPCODE_CAL
: /* Call subroutine (conditional) */
473 /* call the subroutine */
474 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
475 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
477 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1; /* next inst */
478 /* Subtract 1 here since we'll do pc++ at end of for-loop */
479 pc
= inst
->BranchTarget
- 1;
483 GLfloat a
[4], b
[4], c
[4], result
[4];
484 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
485 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
486 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
487 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
488 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
489 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
490 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
491 store_vector4(inst
, machine
, result
);
493 printf("CMP (%g %g %g %g) = (%g %g %g %g) < 0 ? (%g %g %g %g) : (%g %g %g %g)\n",
494 result
[0], result
[1], result
[2], result
[3],
495 a
[0], a
[1], a
[2], a
[3],
496 b
[0], b
[1], b
[2], b
[3],
497 c
[0], c
[1], c
[2], c
[3]);
503 GLfloat a
[4], result
[4];
504 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
505 result
[0] = result
[1] = result
[2] = result
[3]
507 store_vector4(inst
, machine
, result
);
510 case OPCODE_DDX
: /* Partial derivative with respect to X */
513 fetch_vector4_deriv(&inst
->SrcReg
[0], machine
, 'X', result
);
514 store_vector4(inst
, machine
, result
);
517 case OPCODE_DDY
: /* Partial derivative with respect to Y */
520 fetch_vector4_deriv(&inst
->SrcReg
[0], machine
, 'Y', result
);
521 store_vector4(inst
, machine
, result
);
526 GLfloat a
[4], b
[4], result
[4];
527 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
528 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
529 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
);
530 store_vector4(inst
, machine
, result
);
532 printf("DP2 %g = (%g %g) . (%g %g)\n",
533 result
[0], a
[0], a
[1], b
[0], b
[1]);
539 GLfloat a
[4], b
[4], result
[4];
540 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
541 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
542 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
543 store_vector4(inst
, machine
, result
);
545 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
546 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
552 GLfloat a
[4], b
[4], result
[4];
553 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
554 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
555 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
, b
);
556 store_vector4(inst
, machine
, result
);
558 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
559 result
[0], a
[0], a
[1], a
[2], a
[3],
560 b
[0], b
[1], b
[2], b
[3]);
566 GLfloat a
[4], b
[4], result
[4];
567 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
568 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
569 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
) + b
[3];
570 store_vector4(inst
, machine
, result
);
573 case OPCODE_DST
: /* Distance vector */
575 GLfloat a
[4], b
[4], result
[4];
576 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
577 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
579 result
[1] = a
[1] * b
[1];
582 store_vector4(inst
, machine
, result
);
587 GLfloat t
[4], q
[4], floor_t0
;
588 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
589 floor_t0
= floorf(t
[0]);
590 if (floor_t0
> FLT_MAX_EXP
) {
591 SET_POS_INFINITY(q
[0]);
592 SET_POS_INFINITY(q
[2]);
594 else if (floor_t0
< FLT_MIN_EXP
) {
599 q
[0] = ldexpf(1.0, (int) floor_t0
);
600 /* Note: GL_NV_vertex_program expects
601 * result.z = result.x * APPX(result.y)
602 * We do what the ARB extension says.
606 q
[1] = t
[0] - floor_t0
;
608 store_vector4( inst
, machine
, q
);
611 case OPCODE_EX2
: /* Exponential base 2 */
613 GLfloat a
[4], result
[4], val
;
614 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
617 if (IS_INF_OR_NAN(val))
620 result
[0] = result
[1] = result
[2] = result
[3] = val
;
621 store_vector4(inst
, machine
, result
);
626 GLfloat a
[4], result
[4];
627 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
628 result
[0] = floorf(a
[0]);
629 result
[1] = floorf(a
[1]);
630 result
[2] = floorf(a
[2]);
631 result
[3] = floorf(a
[3]);
632 store_vector4(inst
, machine
, result
);
637 GLfloat a
[4], result
[4];
638 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
639 result
[0] = a
[0] - floorf(a
[0]);
640 result
[1] = a
[1] - floorf(a
[1]);
641 result
[2] = a
[2] - floorf(a
[2]);
642 result
[3] = a
[3] - floorf(a
[3]);
643 store_vector4(inst
, machine
, result
);
649 assert(program
->arb
.Instructions
[inst
->BranchTarget
].Opcode
651 program
->arb
.Instructions
[inst
->BranchTarget
].Opcode
655 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
656 cond
= (a
[0] != 0.0F
);
658 printf("IF: %d\n", cond
);
662 /* do if-clause (just continue execution) */
665 /* go to the instruction after ELSE or ENDIF */
666 assert(inst
->BranchTarget
>= 0);
667 pc
= inst
->BranchTarget
;
673 assert(program
->arb
.Instructions
[inst
->BranchTarget
].Opcode
675 assert(inst
->BranchTarget
>= 0);
676 pc
= inst
->BranchTarget
;
681 case OPCODE_KIL
: /* ARB_f_p only */
684 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
686 printf("KIL if (%g %g %g %g) <= 0.0\n",
687 a
[0], a
[1], a
[2], a
[3]);
690 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
695 case OPCODE_LG2
: /* log base 2 */
697 GLfloat a
[4], result
[4], val
;
698 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
699 /* The fast LOG2 macro doesn't meet the precision requirements.
705 val
= logf(a
[0]) * 1.442695F
;
707 result
[0] = result
[1] = result
[2] = result
[3] = val
;
708 store_vector4(inst
, machine
, result
);
713 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
714 GLfloat a
[4], result
[4];
715 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
716 a
[0] = MAX2(a
[0], 0.0F
);
717 a
[1] = MAX2(a
[1], 0.0F
);
718 /* XXX ARB version clamps a[3], NV version doesn't */
719 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
722 /* XXX we could probably just use pow() here */
724 if (a
[1] == 0.0F
&& a
[3] == 0.0F
)
727 result
[2] = powf(a
[1], a
[3]);
733 store_vector4(inst
, machine
, result
);
735 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
736 result
[0], result
[1], result
[2], result
[3],
737 a
[0], a
[1], a
[2], a
[3]);
743 GLfloat t
[4], q
[4], abs_t0
;
744 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
745 abs_t0
= fabsf(t
[0]);
746 if (abs_t0
!= 0.0F
) {
747 if (IS_INF_OR_NAN(abs_t0
))
749 SET_POS_INFINITY(q
[0]);
751 SET_POS_INFINITY(q
[2]);
755 GLfloat mantissa
= frexpf(t
[0], &exponent
);
756 q
[0] = (GLfloat
) (exponent
- 1);
757 q
[1] = 2.0F
* mantissa
; /* map [.5, 1) -> [1, 2) */
759 /* The fast LOG2 macro doesn't meet the precision
762 q
[2] = logf(t
[0]) * 1.442695F
;
766 SET_NEG_INFINITY(q
[0]);
768 SET_NEG_INFINITY(q
[2]);
771 store_vector4(inst
, machine
, q
);
776 GLfloat a
[4], b
[4], c
[4], result
[4];
777 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
778 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
779 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
780 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
781 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
782 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
783 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
784 store_vector4(inst
, machine
, result
);
786 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
787 "(%g %g %g %g), (%g %g %g %g)\n",
788 result
[0], result
[1], result
[2], result
[3],
789 a
[0], a
[1], a
[2], a
[3],
790 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
796 GLfloat a
[4], b
[4], c
[4], result
[4];
797 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
798 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
799 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
800 result
[0] = a
[0] * b
[0] + c
[0];
801 result
[1] = a
[1] * b
[1] + c
[1];
802 result
[2] = a
[2] * b
[2] + c
[2];
803 result
[3] = a
[3] * b
[3] + c
[3];
804 store_vector4(inst
, machine
, result
);
806 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
807 "(%g %g %g %g) + (%g %g %g %g)\n",
808 result
[0], result
[1], result
[2], result
[3],
809 a
[0], a
[1], a
[2], a
[3],
810 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
816 GLfloat a
[4], b
[4], result
[4];
817 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
818 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
819 result
[0] = MAX2(a
[0], b
[0]);
820 result
[1] = MAX2(a
[1], b
[1]);
821 result
[2] = MAX2(a
[2], b
[2]);
822 result
[3] = MAX2(a
[3], b
[3]);
823 store_vector4(inst
, machine
, result
);
825 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
826 result
[0], result
[1], result
[2], result
[3],
827 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
833 GLfloat a
[4], b
[4], result
[4];
834 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
835 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
836 result
[0] = MIN2(a
[0], b
[0]);
837 result
[1] = MIN2(a
[1], b
[1]);
838 result
[2] = MIN2(a
[2], b
[2]);
839 result
[3] = MIN2(a
[3], b
[3]);
840 store_vector4(inst
, machine
, result
);
846 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
847 store_vector4(inst
, machine
, result
);
849 printf("MOV (%g %g %g %g)\n",
850 result
[0], result
[1], result
[2], result
[3]);
856 GLfloat a
[4], b
[4], result
[4];
857 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
858 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
859 result
[0] = a
[0] * b
[0];
860 result
[1] = a
[1] * b
[1];
861 result
[2] = a
[2] * b
[2];
862 result
[3] = a
[3] * b
[3];
863 store_vector4(inst
, machine
, result
);
865 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
866 result
[0], result
[1], result
[2], result
[3],
867 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
873 GLfloat a
[4], result
[4];
874 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
878 result
[3] = _mesa_noise1(a
[0]);
879 store_vector4(inst
, machine
, result
);
884 GLfloat a
[4], result
[4];
885 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
888 result
[2] = result
[3] = _mesa_noise2(a
[0], a
[1]);
889 store_vector4(inst
, machine
, result
);
894 GLfloat a
[4], result
[4];
895 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
899 result
[3] = _mesa_noise3(a
[0], a
[1], a
[2]);
900 store_vector4(inst
, machine
, result
);
905 GLfloat a
[4], result
[4];
906 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
910 result
[3] = _mesa_noise4(a
[0], a
[1], a
[2], a
[3]);
911 store_vector4(inst
, machine
, result
);
918 GLfloat a
[4], b
[4], result
[4];
919 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
920 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
921 result
[0] = result
[1] = result
[2] = result
[3]
923 store_vector4(inst
, machine
, result
);
929 GLfloat a
[4], result
[4];
930 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
934 else if (IS_INF_OR_NAN(a
[0]))
935 printf("RCP(inf)\n");
937 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
938 store_vector4(inst
, machine
, result
);
941 case OPCODE_RET
: /* return from subroutine (conditional) */
942 if (machine
->StackDepth
== 0) {
943 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
945 /* subtract one because of pc++ in the for loop */
946 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
948 case OPCODE_RSQ
: /* 1 / sqrt() */
950 GLfloat a
[4], result
[4];
951 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
953 result
[0] = result
[1] = result
[2] = result
[3] = 1.0f
/ sqrtf(a
[0]);
954 store_vector4(inst
, machine
, result
);
956 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
960 case OPCODE_SCS
: /* sine and cos */
962 GLfloat a
[4], result
[4];
963 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
964 result
[0] = cosf(a
[0]);
965 result
[1] = sinf(a
[0]);
966 result
[2] = 0.0F
; /* undefined! */
967 result
[3] = 0.0F
; /* undefined! */
968 store_vector4(inst
, machine
, result
);
971 case OPCODE_SGE
: /* set on greater or equal */
973 GLfloat a
[4], b
[4], result
[4];
974 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
975 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
976 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
977 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
978 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
979 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
980 store_vector4(inst
, machine
, result
);
982 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
983 result
[0], result
[1], result
[2], result
[3],
984 a
[0], a
[1], a
[2], a
[3],
985 b
[0], b
[1], b
[2], b
[3]);
991 GLfloat a
[4], result
[4];
992 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
993 result
[0] = result
[1] = result
[2] = result
[3]
995 store_vector4(inst
, machine
, result
);
998 case OPCODE_SLT
: /* set on less */
1000 GLfloat a
[4], b
[4], result
[4];
1001 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1002 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1003 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1004 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1005 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1006 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1007 store_vector4(inst
, machine
, result
);
1009 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1010 result
[0], result
[1], result
[2], result
[3],
1011 a
[0], a
[1], a
[2], a
[3],
1012 b
[0], b
[1], b
[2], b
[3]);
1016 case OPCODE_SSG
: /* set sign (-1, 0 or +1) */
1018 GLfloat a
[4], result
[4];
1019 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1020 result
[0] = (GLfloat
) ((a
[0] > 0.0F
) - (a
[0] < 0.0F
));
1021 result
[1] = (GLfloat
) ((a
[1] > 0.0F
) - (a
[1] < 0.0F
));
1022 result
[2] = (GLfloat
) ((a
[2] > 0.0F
) - (a
[2] < 0.0F
));
1023 result
[3] = (GLfloat
) ((a
[3] > 0.0F
) - (a
[3] < 0.0F
));
1024 store_vector4(inst
, machine
, result
);
1029 GLfloat a
[4], b
[4], result
[4];
1030 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1031 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1032 result
[0] = a
[0] - b
[0];
1033 result
[1] = a
[1] - b
[1];
1034 result
[2] = a
[2] - b
[2];
1035 result
[3] = a
[3] - b
[3];
1036 store_vector4(inst
, machine
, result
);
1038 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1039 result
[0], result
[1], result
[2], result
[3],
1040 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1044 case OPCODE_SWZ
: /* extended swizzle */
1046 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1047 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
1050 for (i
= 0; i
< 4; i
++) {
1051 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1052 if (swz
== SWIZZLE_ZERO
)
1054 else if (swz
== SWIZZLE_ONE
)
1058 result
[i
] = src
[swz
];
1060 if (source
->Negate
& (1 << i
))
1061 result
[i
] = -result
[i
];
1063 store_vector4(inst
, machine
, result
);
1066 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1067 /* Simple texel lookup */
1069 GLfloat texcoord
[4], color
[4];
1070 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1072 /* For TEX, texcoord.Q should not be used and its value should not
1073 * matter (at most, we pass coord.xyz to texture3D() in GLSL).
1074 * Set Q=1 so that FetchTexelDeriv() doesn't get a garbage value
1075 * which is effectively what happens when the texcoord swizzle
1080 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1083 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1084 color
[0], color
[1], color
[2], color
[3],
1086 texcoord
[0], texcoord
[1], texcoord
[2], texcoord
[3]);
1088 store_vector4(inst
, machine
, color
);
1091 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1092 /* Texel lookup with LOD bias */
1094 GLfloat texcoord
[4], color
[4], lodBias
;
1096 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1098 /* texcoord[3] is the bias to add to lambda */
1099 lodBias
= texcoord
[3];
1101 fetch_texel(ctx
, machine
, inst
, texcoord
, lodBias
, color
);
1104 printf("TXB (%g, %g, %g, %g) = texture[%d][%g %g %g %g]"
1106 color
[0], color
[1], color
[2], color
[3],
1115 store_vector4(inst
, machine
, color
);
1119 /* Texture lookup w/ partial derivatives for LOD */
1121 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1122 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1123 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1124 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1125 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1127 inst
->TexSrcUnit
, color
);
1128 store_vector4(inst
, machine
, color
);
1132 /* Texel lookup with explicit LOD */
1134 GLfloat texcoord
[4], color
[4], lod
;
1136 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1138 /* texcoord[3] is the LOD */
1141 machine
->FetchTexelLod(ctx
, texcoord
, lod
,
1142 machine
->Samplers
[inst
->TexSrcUnit
], color
);
1144 store_vector4(inst
, machine
, color
);
1147 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1148 /* Texture lookup w/ projective divide */
1150 GLfloat texcoord
[4], color
[4];
1152 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1153 /* Not so sure about this test - if texcoord[3] is
1154 * zero, we'd probably be fine except for an assert in
1155 * IROUND_POS() which gets triggered by the inf values created.
1157 if (texcoord
[3] != 0.0F
) {
1158 texcoord
[0] /= texcoord
[3];
1159 texcoord
[1] /= texcoord
[3];
1160 texcoord
[2] /= texcoord
[3];
1163 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1165 store_vector4(inst
, machine
, color
);
1168 case OPCODE_TRUNC
: /* truncate toward zero */
1170 GLfloat a
[4], result
[4];
1171 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1172 result
[0] = (GLfloat
) (GLint
) a
[0];
1173 result
[1] = (GLfloat
) (GLint
) a
[1];
1174 result
[2] = (GLfloat
) (GLint
) a
[2];
1175 result
[3] = (GLfloat
) (GLint
) a
[3];
1176 store_vector4(inst
, machine
, result
);
1179 case OPCODE_XPD
: /* cross product */
1181 GLfloat a
[4], b
[4], result
[4];
1182 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1183 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1184 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1185 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1186 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1188 store_vector4(inst
, machine
, result
);
1190 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1191 result
[0], result
[1], result
[2], result
[3],
1192 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1199 _mesa_problem(ctx
, "Bad opcode %d in _mesa_execute_program",
1201 return GL_TRUE
; /* return value doesn't matter */
1205 if (numExec
> maxExec
) {
1206 static GLboolean reported
= GL_FALSE
;
1208 _mesa_problem(ctx
, "Infinite loop detected in fragment program");