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/glheader.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
< (GLint
) ARRAY_SIZE(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)];
203 if (source
->Negate
) {
204 assert(source
->Negate
== NEGATE_XYZW
);
205 result
[0] = -result
[0];
206 result
[1] = -result
[1];
207 result
[2] = -result
[2];
208 result
[3] = -result
[3];
212 assert(!IS_INF_OR_NAN(result
[0]));
213 assert(!IS_INF_OR_NAN(result
[0]));
214 assert(!IS_INF_OR_NAN(result
[0]));
215 assert(!IS_INF_OR_NAN(result
[0]));
221 * Fetch the derivative with respect to X or Y for the given register.
222 * XXX this currently only works for fragment program input attribs.
225 fetch_vector4_deriv(struct gl_context
* ctx
,
226 const struct prog_src_register
*source
,
227 const struct gl_program_machine
*machine
,
228 char xOrY
, GLfloat result
[4])
230 if (source
->File
== PROGRAM_INPUT
&&
231 source
->Index
< (GLint
) machine
->NumDeriv
) {
232 const GLint col
= machine
->CurElement
;
233 const GLfloat w
= machine
->Attribs
[VARYING_SLOT_POS
][col
][3];
234 const GLfloat invQ
= 1.0f
/ w
;
238 deriv
[0] = machine
->DerivX
[source
->Index
][0] * invQ
;
239 deriv
[1] = machine
->DerivX
[source
->Index
][1] * invQ
;
240 deriv
[2] = machine
->DerivX
[source
->Index
][2] * invQ
;
241 deriv
[3] = machine
->DerivX
[source
->Index
][3] * invQ
;
244 deriv
[0] = machine
->DerivY
[source
->Index
][0] * invQ
;
245 deriv
[1] = machine
->DerivY
[source
->Index
][1] * invQ
;
246 deriv
[2] = machine
->DerivY
[source
->Index
][2] * invQ
;
247 deriv
[3] = machine
->DerivY
[source
->Index
][3] * invQ
;
250 result
[0] = deriv
[GET_SWZ(source
->Swizzle
, 0)];
251 result
[1] = deriv
[GET_SWZ(source
->Swizzle
, 1)];
252 result
[2] = deriv
[GET_SWZ(source
->Swizzle
, 2)];
253 result
[3] = deriv
[GET_SWZ(source
->Swizzle
, 3)];
255 if (source
->Negate
) {
256 assert(source
->Negate
== NEGATE_XYZW
);
257 result
[0] = -result
[0];
258 result
[1] = -result
[1];
259 result
[2] = -result
[2];
260 result
[3] = -result
[3];
264 ASSIGN_4V(result
, 0.0, 0.0, 0.0, 0.0);
270 * As above, but only return result[0] element.
273 fetch_vector1(const struct prog_src_register
*source
,
274 const struct gl_program_machine
*machine
, GLfloat result
[4])
276 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
278 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
280 if (source
->Negate
) {
281 result
[0] = -result
[0];
287 * Fetch texel from texture. Use partial derivatives when possible.
290 fetch_texel(struct gl_context
*ctx
,
291 const struct gl_program_machine
*machine
,
292 const struct prog_instruction
*inst
,
293 const GLfloat texcoord
[4], GLfloat lodBias
,
296 const GLuint unit
= machine
->Samplers
[inst
->TexSrcUnit
];
298 /* Note: we only have the right derivatives for fragment input attribs.
300 if (machine
->NumDeriv
> 0 &&
301 inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
302 inst
->SrcReg
[0].Index
== VARYING_SLOT_TEX0
+ inst
->TexSrcUnit
) {
303 /* simple texture fetch for which we should have derivatives */
304 GLuint attr
= inst
->SrcReg
[0].Index
;
305 machine
->FetchTexelDeriv(ctx
, texcoord
,
306 machine
->DerivX
[attr
],
307 machine
->DerivY
[attr
],
308 lodBias
, unit
, color
);
311 machine
->FetchTexelLod(ctx
, texcoord
, lodBias
, unit
, color
);
317 * Store 4 floats into a register. Observe the instructions saturate and
318 * set-condition-code flags.
321 store_vector4(const struct prog_instruction
*inst
,
322 struct gl_program_machine
*machine
, const GLfloat value
[4])
324 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
325 const GLboolean clamp
= inst
->Saturate
;
326 GLuint writeMask
= dstReg
->WriteMask
;
327 GLfloat clampedValue
[4];
328 GLfloat
*dst
= get_dst_register_pointer(dstReg
, machine
);
331 if (value
[0] > 1.0e10
||
332 IS_INF_OR_NAN(value
[0]) ||
333 IS_INF_OR_NAN(value
[1]) ||
334 IS_INF_OR_NAN(value
[2]) || IS_INF_OR_NAN(value
[3]))
335 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
339 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
340 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
341 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
342 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
343 value
= clampedValue
;
347 assert(!IS_INF_OR_NAN(value
[0]));
348 assert(!IS_INF_OR_NAN(value
[0]));
349 assert(!IS_INF_OR_NAN(value
[0]));
350 assert(!IS_INF_OR_NAN(value
[0]));
353 if (writeMask
& WRITEMASK_X
)
355 if (writeMask
& WRITEMASK_Y
)
357 if (writeMask
& WRITEMASK_Z
)
359 if (writeMask
& WRITEMASK_W
)
365 * Execute the given vertex/fragment program.
367 * \param ctx rendering context
368 * \param program the program to execute
369 * \param machine machine state (must be initialized)
370 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
373 _mesa_execute_program(struct gl_context
* ctx
,
374 const struct gl_program
*program
,
375 struct gl_program_machine
*machine
)
377 const GLuint numInst
= program
->NumInstructions
;
378 const GLuint maxExec
= 65536;
379 GLuint pc
, numExec
= 0;
381 machine
->CurProgram
= program
;
384 printf("execute program %u --------------------\n", program
->Id
);
387 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
388 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
391 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
394 for (pc
= 0; pc
< numInst
; pc
++) {
395 const struct prog_instruction
*inst
= program
->Instructions
+ pc
;
398 _mesa_print_instruction(inst
);
401 switch (inst
->Opcode
) {
404 GLfloat a
[4], result
[4];
405 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
406 result
[0] = fabsf(a
[0]);
407 result
[1] = fabsf(a
[1]);
408 result
[2] = fabsf(a
[2]);
409 result
[3] = fabsf(a
[3]);
410 store_vector4(inst
, machine
, result
);
415 GLfloat a
[4], b
[4], result
[4];
416 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
417 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
418 result
[0] = a
[0] + b
[0];
419 result
[1] = a
[1] + b
[1];
420 result
[2] = a
[2] + b
[2];
421 result
[3] = a
[3] + b
[3];
422 store_vector4(inst
, machine
, result
);
424 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
425 result
[0], result
[1], result
[2], result
[3],
426 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
433 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
434 machine
->AddressReg
[0][0] = IFLOOR(t
[0]);
436 printf("ARL %d\n", machine
->AddressReg
[0][0]);
442 assert(program
->Instructions
[inst
->BranchTarget
].Opcode
446 /* subtract 1 here since pc is incremented by for(pc) loop */
447 assert(program
->Instructions
[inst
->BranchTarget
].Opcode
449 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
451 case OPCODE_BGNSUB
: /* begin subroutine */
453 case OPCODE_ENDSUB
: /* end subroutine */
455 case OPCODE_BRK
: /* break out of loop (conditional) */
456 assert(program
->Instructions
[inst
->BranchTarget
].Opcode
458 /* break out of loop */
459 /* pc++ at end of for-loop will put us after the ENDLOOP inst */
460 pc
= inst
->BranchTarget
;
462 case OPCODE_CONT
: /* continue loop (conditional) */
463 assert(program
->Instructions
[inst
->BranchTarget
].Opcode
465 /* continue at ENDLOOP */
466 /* Subtract 1 here since we'll do pc++ at end of for-loop */
467 pc
= inst
->BranchTarget
- 1;
469 case OPCODE_CAL
: /* Call subroutine (conditional) */
470 /* call the subroutine */
471 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
472 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
474 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1; /* next inst */
475 /* Subtract 1 here since we'll do pc++ at end of for-loop */
476 pc
= inst
->BranchTarget
- 1;
480 GLfloat a
[4], b
[4], c
[4], result
[4];
481 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
482 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
483 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
484 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
485 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
486 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
487 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
488 store_vector4(inst
, machine
, result
);
490 printf("CMP (%g %g %g %g) = (%g %g %g %g) < 0 ? (%g %g %g %g) : (%g %g %g %g)\n",
491 result
[0], result
[1], result
[2], result
[3],
492 a
[0], a
[1], a
[2], a
[3],
493 b
[0], b
[1], b
[2], b
[3],
494 c
[0], c
[1], c
[2], c
[3]);
500 GLfloat a
[4], result
[4];
501 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
502 result
[0] = result
[1] = result
[2] = result
[3]
504 store_vector4(inst
, machine
, result
);
507 case OPCODE_DDX
: /* Partial derivative with respect to X */
510 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
512 store_vector4(inst
, machine
, result
);
515 case OPCODE_DDY
: /* Partial derivative with respect to Y */
518 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
520 store_vector4(inst
, machine
, result
);
525 GLfloat a
[4], b
[4], result
[4];
526 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
527 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
528 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
);
529 store_vector4(inst
, machine
, result
);
531 printf("DP2 %g = (%g %g) . (%g %g)\n",
532 result
[0], a
[0], a
[1], b
[0], b
[1]);
538 GLfloat a
[4], b
[4], result
[4];
539 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
540 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
541 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
542 store_vector4(inst
, machine
, result
);
544 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
545 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
551 GLfloat a
[4], b
[4], result
[4];
552 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
553 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
554 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
, b
);
555 store_vector4(inst
, machine
, result
);
557 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
558 result
[0], a
[0], a
[1], a
[2], a
[3],
559 b
[0], b
[1], b
[2], b
[3]);
565 GLfloat a
[4], b
[4], result
[4];
566 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
567 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
568 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
) + b
[3];
569 store_vector4(inst
, machine
, result
);
572 case OPCODE_DST
: /* Distance vector */
574 GLfloat a
[4], b
[4], result
[4];
575 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
576 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
578 result
[1] = a
[1] * b
[1];
581 store_vector4(inst
, machine
, result
);
586 GLfloat t
[4], q
[4], floor_t0
;
587 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
588 floor_t0
= floorf(t
[0]);
589 if (floor_t0
> FLT_MAX_EXP
) {
590 SET_POS_INFINITY(q
[0]);
591 SET_POS_INFINITY(q
[2]);
593 else if (floor_t0
< FLT_MIN_EXP
) {
598 q
[0] = ldexpf(1.0, (int) floor_t0
);
599 /* Note: GL_NV_vertex_program expects
600 * result.z = result.x * APPX(result.y)
601 * We do what the ARB extension says.
605 q
[1] = t
[0] - floor_t0
;
607 store_vector4( inst
, machine
, q
);
610 case OPCODE_EX2
: /* Exponential base 2 */
612 GLfloat a
[4], result
[4], val
;
613 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
616 if (IS_INF_OR_NAN(val))
619 result
[0] = result
[1] = result
[2] = result
[3] = val
;
620 store_vector4(inst
, machine
, result
);
625 GLfloat a
[4], result
[4];
626 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
627 result
[0] = floorf(a
[0]);
628 result
[1] = floorf(a
[1]);
629 result
[2] = floorf(a
[2]);
630 result
[3] = floorf(a
[3]);
631 store_vector4(inst
, machine
, result
);
636 GLfloat a
[4], result
[4];
637 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
638 result
[0] = a
[0] - floorf(a
[0]);
639 result
[1] = a
[1] - floorf(a
[1]);
640 result
[2] = a
[2] - floorf(a
[2]);
641 result
[3] = a
[3] - floorf(a
[3]);
642 store_vector4(inst
, machine
, result
);
648 assert(program
->Instructions
[inst
->BranchTarget
].Opcode
650 program
->Instructions
[inst
->BranchTarget
].Opcode
654 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
655 cond
= (a
[0] != 0.0F
);
657 printf("IF: %d\n", cond
);
661 /* do if-clause (just continue execution) */
664 /* go to the instruction after ELSE or ENDIF */
665 assert(inst
->BranchTarget
>= 0);
666 pc
= inst
->BranchTarget
;
672 assert(program
->Instructions
[inst
->BranchTarget
].Opcode
674 assert(inst
->BranchTarget
>= 0);
675 pc
= inst
->BranchTarget
;
680 case OPCODE_KIL
: /* ARB_f_p only */
683 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
685 printf("KIL if (%g %g %g %g) <= 0.0\n",
686 a
[0], a
[1], a
[2], a
[3]);
689 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
694 case OPCODE_LG2
: /* log base 2 */
696 GLfloat a
[4], result
[4], val
;
697 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
698 /* The fast LOG2 macro doesn't meet the precision requirements.
704 val
= logf(a
[0]) * 1.442695F
;
706 result
[0] = result
[1] = result
[2] = result
[3] = val
;
707 store_vector4(inst
, machine
, result
);
712 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
713 GLfloat a
[4], result
[4];
714 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
715 a
[0] = MAX2(a
[0], 0.0F
);
716 a
[1] = MAX2(a
[1], 0.0F
);
717 /* XXX ARB version clamps a[3], NV version doesn't */
718 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
721 /* XXX we could probably just use pow() here */
723 if (a
[1] == 0.0F
&& a
[3] == 0.0F
)
726 result
[2] = powf(a
[1], a
[3]);
732 store_vector4(inst
, machine
, result
);
734 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
735 result
[0], result
[1], result
[2], result
[3],
736 a
[0], a
[1], a
[2], a
[3]);
742 GLfloat t
[4], q
[4], abs_t0
;
743 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
744 abs_t0
= fabsf(t
[0]);
745 if (abs_t0
!= 0.0F
) {
746 if (IS_INF_OR_NAN(abs_t0
))
748 SET_POS_INFINITY(q
[0]);
750 SET_POS_INFINITY(q
[2]);
754 GLfloat mantissa
= frexpf(t
[0], &exponent
);
755 q
[0] = (GLfloat
) (exponent
- 1);
756 q
[1] = 2.0F
* mantissa
; /* map [.5, 1) -> [1, 2) */
758 /* The fast LOG2 macro doesn't meet the precision
761 q
[2] = logf(t
[0]) * 1.442695F
;
765 SET_NEG_INFINITY(q
[0]);
767 SET_NEG_INFINITY(q
[2]);
770 store_vector4(inst
, machine
, q
);
775 GLfloat a
[4], b
[4], c
[4], result
[4];
776 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
777 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
778 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
779 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
780 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
781 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
782 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
783 store_vector4(inst
, machine
, result
);
785 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
786 "(%g %g %g %g), (%g %g %g %g)\n",
787 result
[0], result
[1], result
[2], result
[3],
788 a
[0], a
[1], a
[2], a
[3],
789 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
795 GLfloat a
[4], b
[4], c
[4], result
[4];
796 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
797 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
798 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
799 result
[0] = a
[0] * b
[0] + c
[0];
800 result
[1] = a
[1] * b
[1] + c
[1];
801 result
[2] = a
[2] * b
[2] + c
[2];
802 result
[3] = a
[3] * b
[3] + c
[3];
803 store_vector4(inst
, machine
, result
);
805 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
806 "(%g %g %g %g) + (%g %g %g %g)\n",
807 result
[0], result
[1], result
[2], result
[3],
808 a
[0], a
[1], a
[2], a
[3],
809 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
815 GLfloat a
[4], b
[4], result
[4];
816 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
817 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
818 result
[0] = MAX2(a
[0], b
[0]);
819 result
[1] = MAX2(a
[1], b
[1]);
820 result
[2] = MAX2(a
[2], b
[2]);
821 result
[3] = MAX2(a
[3], b
[3]);
822 store_vector4(inst
, machine
, result
);
824 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
825 result
[0], result
[1], result
[2], result
[3],
826 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
832 GLfloat a
[4], b
[4], result
[4];
833 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
834 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
835 result
[0] = MIN2(a
[0], b
[0]);
836 result
[1] = MIN2(a
[1], b
[1]);
837 result
[2] = MIN2(a
[2], b
[2]);
838 result
[3] = MIN2(a
[3], b
[3]);
839 store_vector4(inst
, machine
, result
);
845 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
846 store_vector4(inst
, machine
, result
);
848 printf("MOV (%g %g %g %g)\n",
849 result
[0], result
[1], result
[2], result
[3]);
855 GLfloat a
[4], b
[4], result
[4];
856 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
857 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
858 result
[0] = a
[0] * b
[0];
859 result
[1] = a
[1] * b
[1];
860 result
[2] = a
[2] * b
[2];
861 result
[3] = a
[3] * b
[3];
862 store_vector4(inst
, machine
, result
);
864 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
865 result
[0], result
[1], result
[2], result
[3],
866 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
872 GLfloat a
[4], result
[4];
873 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
877 result
[3] = _mesa_noise1(a
[0]);
878 store_vector4(inst
, machine
, result
);
883 GLfloat a
[4], result
[4];
884 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
887 result
[2] = result
[3] = _mesa_noise2(a
[0], a
[1]);
888 store_vector4(inst
, machine
, result
);
893 GLfloat a
[4], result
[4];
894 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
898 result
[3] = _mesa_noise3(a
[0], a
[1], a
[2]);
899 store_vector4(inst
, machine
, result
);
904 GLfloat a
[4], result
[4];
905 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
909 result
[3] = _mesa_noise4(a
[0], a
[1], a
[2], a
[3]);
910 store_vector4(inst
, machine
, result
);
917 GLfloat a
[4], b
[4], result
[4];
918 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
919 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
920 result
[0] = result
[1] = result
[2] = result
[3]
922 store_vector4(inst
, machine
, result
);
928 GLfloat a
[4], result
[4];
929 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
933 else if (IS_INF_OR_NAN(a
[0]))
934 printf("RCP(inf)\n");
936 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
937 store_vector4(inst
, machine
, result
);
940 case OPCODE_RET
: /* return from subroutine (conditional) */
941 if (machine
->StackDepth
== 0) {
942 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
944 /* subtract one because of pc++ in the for loop */
945 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
947 case OPCODE_RSQ
: /* 1 / sqrt() */
949 GLfloat a
[4], result
[4];
950 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
952 result
[0] = result
[1] = result
[2] = result
[3] = 1.0f
/ sqrtf(a
[0]);
953 store_vector4(inst
, machine
, result
);
955 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
959 case OPCODE_SCS
: /* sine and cos */
961 GLfloat a
[4], result
[4];
962 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
963 result
[0] = cosf(a
[0]);
964 result
[1] = sinf(a
[0]);
965 result
[2] = 0.0F
; /* undefined! */
966 result
[3] = 0.0F
; /* undefined! */
967 store_vector4(inst
, machine
, result
);
970 case OPCODE_SEQ
: /* set on equal */
972 GLfloat a
[4], b
[4], result
[4];
973 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
974 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
975 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
976 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
977 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
978 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
979 store_vector4(inst
, machine
, result
);
981 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
982 result
[0], result
[1], result
[2], result
[3],
983 a
[0], a
[1], a
[2], a
[3],
984 b
[0], b
[1], b
[2], b
[3]);
988 case OPCODE_SGE
: /* set on greater or equal */
990 GLfloat a
[4], b
[4], result
[4];
991 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
992 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
993 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
994 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
995 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
996 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
997 store_vector4(inst
, machine
, result
);
999 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1000 result
[0], result
[1], result
[2], result
[3],
1001 a
[0], a
[1], a
[2], a
[3],
1002 b
[0], b
[1], b
[2], b
[3]);
1006 case OPCODE_SGT
: /* set on greater */
1008 GLfloat a
[4], b
[4], result
[4];
1009 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1010 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1011 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1012 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1013 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1014 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1015 store_vector4(inst
, machine
, result
);
1017 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1018 result
[0], result
[1], result
[2], result
[3],
1019 a
[0], a
[1], a
[2], a
[3],
1020 b
[0], b
[1], b
[2], b
[3]);
1026 GLfloat a
[4], result
[4];
1027 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1028 result
[0] = result
[1] = result
[2] = result
[3]
1030 store_vector4(inst
, machine
, result
);
1033 case OPCODE_SLE
: /* set on less or equal */
1035 GLfloat a
[4], b
[4], result
[4];
1036 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1037 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1038 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1039 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1040 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1041 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1042 store_vector4(inst
, machine
, result
);
1044 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1045 result
[0], result
[1], result
[2], result
[3],
1046 a
[0], a
[1], a
[2], a
[3],
1047 b
[0], b
[1], b
[2], b
[3]);
1051 case OPCODE_SLT
: /* set on less */
1053 GLfloat a
[4], b
[4], result
[4];
1054 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1055 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1056 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1057 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1058 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1059 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1060 store_vector4(inst
, machine
, result
);
1062 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1063 result
[0], result
[1], result
[2], result
[3],
1064 a
[0], a
[1], a
[2], a
[3],
1065 b
[0], b
[1], b
[2], b
[3]);
1069 case OPCODE_SNE
: /* set on not equal */
1071 GLfloat a
[4], b
[4], result
[4];
1072 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1073 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1074 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1075 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1076 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1077 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1078 store_vector4(inst
, machine
, result
);
1080 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1081 result
[0], result
[1], result
[2], result
[3],
1082 a
[0], a
[1], a
[2], a
[3],
1083 b
[0], b
[1], b
[2], b
[3]);
1087 case OPCODE_SSG
: /* set sign (-1, 0 or +1) */
1089 GLfloat a
[4], result
[4];
1090 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1091 result
[0] = (GLfloat
) ((a
[0] > 0.0F
) - (a
[0] < 0.0F
));
1092 result
[1] = (GLfloat
) ((a
[1] > 0.0F
) - (a
[1] < 0.0F
));
1093 result
[2] = (GLfloat
) ((a
[2] > 0.0F
) - (a
[2] < 0.0F
));
1094 result
[3] = (GLfloat
) ((a
[3] > 0.0F
) - (a
[3] < 0.0F
));
1095 store_vector4(inst
, machine
, result
);
1100 GLfloat a
[4], b
[4], result
[4];
1101 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1102 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1103 result
[0] = a
[0] - b
[0];
1104 result
[1] = a
[1] - b
[1];
1105 result
[2] = a
[2] - b
[2];
1106 result
[3] = a
[3] - b
[3];
1107 store_vector4(inst
, machine
, result
);
1109 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1110 result
[0], result
[1], result
[2], result
[3],
1111 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1115 case OPCODE_SWZ
: /* extended swizzle */
1117 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1118 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
1121 for (i
= 0; i
< 4; i
++) {
1122 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1123 if (swz
== SWIZZLE_ZERO
)
1125 else if (swz
== SWIZZLE_ONE
)
1129 result
[i
] = src
[swz
];
1131 if (source
->Negate
& (1 << i
))
1132 result
[i
] = -result
[i
];
1134 store_vector4(inst
, machine
, result
);
1137 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1138 /* Simple texel lookup */
1140 GLfloat texcoord
[4], color
[4];
1141 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1143 /* For TEX, texcoord.Q should not be used and its value should not
1144 * matter (at most, we pass coord.xyz to texture3D() in GLSL).
1145 * Set Q=1 so that FetchTexelDeriv() doesn't get a garbage value
1146 * which is effectively what happens when the texcoord swizzle
1151 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1154 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1155 color
[0], color
[1], color
[2], color
[3],
1157 texcoord
[0], texcoord
[1], texcoord
[2], texcoord
[3]);
1159 store_vector4(inst
, machine
, color
);
1162 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1163 /* Texel lookup with LOD bias */
1165 GLfloat texcoord
[4], color
[4], lodBias
;
1167 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1169 /* texcoord[3] is the bias to add to lambda */
1170 lodBias
= texcoord
[3];
1172 fetch_texel(ctx
, machine
, inst
, texcoord
, lodBias
, color
);
1175 printf("TXB (%g, %g, %g, %g) = texture[%d][%g %g %g %g]"
1177 color
[0], color
[1], color
[2], color
[3],
1186 store_vector4(inst
, machine
, color
);
1190 /* Texture lookup w/ partial derivatives for LOD */
1192 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1193 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1194 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1195 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1196 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1198 inst
->TexSrcUnit
, color
);
1199 store_vector4(inst
, machine
, color
);
1203 /* Texel lookup with explicit LOD */
1205 GLfloat texcoord
[4], color
[4], lod
;
1207 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1209 /* texcoord[3] is the LOD */
1212 machine
->FetchTexelLod(ctx
, texcoord
, lod
,
1213 machine
->Samplers
[inst
->TexSrcUnit
], color
);
1215 store_vector4(inst
, machine
, color
);
1218 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1219 /* Texture lookup w/ projective divide */
1221 GLfloat texcoord
[4], color
[4];
1223 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1224 /* Not so sure about this test - if texcoord[3] is
1225 * zero, we'd probably be fine except for an assert in
1226 * IROUND_POS() which gets triggered by the inf values created.
1228 if (texcoord
[3] != 0.0F
) {
1229 texcoord
[0] /= texcoord
[3];
1230 texcoord
[1] /= texcoord
[3];
1231 texcoord
[2] /= texcoord
[3];
1234 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1236 store_vector4(inst
, machine
, color
);
1239 case OPCODE_TRUNC
: /* truncate toward zero */
1241 GLfloat a
[4], result
[4];
1242 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1243 result
[0] = (GLfloat
) (GLint
) a
[0];
1244 result
[1] = (GLfloat
) (GLint
) a
[1];
1245 result
[2] = (GLfloat
) (GLint
) a
[2];
1246 result
[3] = (GLfloat
) (GLint
) a
[3];
1247 store_vector4(inst
, machine
, result
);
1250 case OPCODE_XPD
: /* cross product */
1252 GLfloat a
[4], b
[4], result
[4];
1253 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1254 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1255 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1256 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1257 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1259 store_vector4(inst
, machine
, result
);
1261 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1262 result
[0], result
[1], result
[2], result
[3],
1263 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1270 _mesa_problem(ctx
, "Bad opcode %d in _mesa_execute_program",
1272 return GL_TRUE
; /* return value doesn't matter */
1276 if (numExec
> maxExec
) {
1277 static GLboolean reported
= GL_FALSE
;
1279 _mesa_problem(ctx
, "Infinite loop detected in fragment program");