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)];
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 texel from texture. Use partial derivatives when possible.
305 fetch_texel(struct gl_context
*ctx
,
306 const struct gl_program_machine
*machine
,
307 const struct prog_instruction
*inst
,
308 const GLfloat texcoord
[4], GLfloat lodBias
,
311 const GLuint unit
= machine
->Samplers
[inst
->TexSrcUnit
];
313 /* Note: we only have the right derivatives for fragment input attribs.
315 if (machine
->NumDeriv
> 0 &&
316 inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
317 inst
->SrcReg
[0].Index
== VARYING_SLOT_TEX0
+ inst
->TexSrcUnit
) {
318 /* simple texture fetch for which we should have derivatives */
319 GLuint attr
= inst
->SrcReg
[0].Index
;
320 machine
->FetchTexelDeriv(ctx
, texcoord
,
321 machine
->DerivX
[attr
],
322 machine
->DerivY
[attr
],
323 lodBias
, unit
, color
);
326 machine
->FetchTexelLod(ctx
, texcoord
, lodBias
, unit
, color
);
332 * Store 4 floats into a register. Observe the instructions saturate and
333 * set-condition-code flags.
336 store_vector4(const struct prog_instruction
*inst
,
337 struct gl_program_machine
*machine
, const GLfloat value
[4])
339 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
340 const GLboolean clamp
= inst
->Saturate
;
341 GLuint writeMask
= dstReg
->WriteMask
;
342 GLfloat clampedValue
[4];
343 GLfloat
*dst
= get_dst_register_pointer(dstReg
, machine
);
346 if (value
[0] > 1.0e10
||
347 IS_INF_OR_NAN(value
[0]) ||
348 IS_INF_OR_NAN(value
[1]) ||
349 IS_INF_OR_NAN(value
[2]) || IS_INF_OR_NAN(value
[3]))
350 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
354 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
355 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
356 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
357 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
358 value
= clampedValue
;
362 assert(!IS_INF_OR_NAN(value
[0]));
363 assert(!IS_INF_OR_NAN(value
[0]));
364 assert(!IS_INF_OR_NAN(value
[0]));
365 assert(!IS_INF_OR_NAN(value
[0]));
368 if (writeMask
& WRITEMASK_X
)
370 if (writeMask
& WRITEMASK_Y
)
372 if (writeMask
& WRITEMASK_Z
)
374 if (writeMask
& WRITEMASK_W
)
380 * Execute the given vertex/fragment program.
382 * \param ctx rendering context
383 * \param program the program to execute
384 * \param machine machine state (must be initialized)
385 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
388 _mesa_execute_program(struct gl_context
* ctx
,
389 const struct gl_program
*program
,
390 struct gl_program_machine
*machine
)
392 const GLuint numInst
= program
->NumInstructions
;
393 const GLuint maxExec
= 65536;
394 GLuint pc
, numExec
= 0;
396 machine
->CurProgram
= program
;
399 printf("execute program %u --------------------\n", program
->Id
);
402 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
403 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
406 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
409 for (pc
= 0; pc
< numInst
; pc
++) {
410 const struct prog_instruction
*inst
= program
->Instructions
+ pc
;
413 _mesa_print_instruction(inst
);
416 switch (inst
->Opcode
) {
419 GLfloat a
[4], result
[4];
420 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
421 result
[0] = fabsf(a
[0]);
422 result
[1] = fabsf(a
[1]);
423 result
[2] = fabsf(a
[2]);
424 result
[3] = fabsf(a
[3]);
425 store_vector4(inst
, machine
, result
);
430 GLfloat a
[4], b
[4], result
[4];
431 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
432 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
433 result
[0] = a
[0] + b
[0];
434 result
[1] = a
[1] + b
[1];
435 result
[2] = a
[2] + b
[2];
436 result
[3] = a
[3] + b
[3];
437 store_vector4(inst
, machine
, result
);
439 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
440 result
[0], result
[1], result
[2], result
[3],
441 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
448 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
449 machine
->AddressReg
[0][0] = IFLOOR(t
[0]);
451 printf("ARL %d\n", machine
->AddressReg
[0][0]);
457 assert(program
->Instructions
[inst
->BranchTarget
].Opcode
461 /* subtract 1 here since pc is incremented by for(pc) loop */
462 assert(program
->Instructions
[inst
->BranchTarget
].Opcode
464 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
466 case OPCODE_BGNSUB
: /* begin subroutine */
468 case OPCODE_ENDSUB
: /* end subroutine */
470 case OPCODE_BRK
: /* break out of loop (conditional) */
471 assert(program
->Instructions
[inst
->BranchTarget
].Opcode
473 /* break out of loop */
474 /* pc++ at end of for-loop will put us after the ENDLOOP inst */
475 pc
= inst
->BranchTarget
;
477 case OPCODE_CONT
: /* continue loop (conditional) */
478 assert(program
->Instructions
[inst
->BranchTarget
].Opcode
480 /* continue at ENDLOOP */
481 /* Subtract 1 here since we'll do pc++ at end of for-loop */
482 pc
= inst
->BranchTarget
- 1;
484 case OPCODE_CAL
: /* Call subroutine (conditional) */
485 /* call the subroutine */
486 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
487 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
489 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1; /* next inst */
490 /* Subtract 1 here since we'll do pc++ at end of for-loop */
491 pc
= inst
->BranchTarget
- 1;
495 GLfloat a
[4], b
[4], c
[4], result
[4];
496 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
497 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
498 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
499 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
500 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
501 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
502 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
503 store_vector4(inst
, machine
, result
);
505 printf("CMP (%g %g %g %g) = (%g %g %g %g) < 0 ? (%g %g %g %g) : (%g %g %g %g)\n",
506 result
[0], result
[1], result
[2], result
[3],
507 a
[0], a
[1], a
[2], a
[3],
508 b
[0], b
[1], b
[2], b
[3],
509 c
[0], c
[1], c
[2], c
[3]);
515 GLfloat a
[4], result
[4];
516 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
517 result
[0] = result
[1] = result
[2] = result
[3]
519 store_vector4(inst
, machine
, result
);
522 case OPCODE_DDX
: /* Partial derivative with respect to X */
525 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
527 store_vector4(inst
, machine
, result
);
530 case OPCODE_DDY
: /* Partial derivative with respect to Y */
533 fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], machine
,
535 store_vector4(inst
, machine
, result
);
540 GLfloat a
[4], b
[4], result
[4];
541 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
542 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
543 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
);
544 store_vector4(inst
, machine
, result
);
546 printf("DP2 %g = (%g %g) . (%g %g)\n",
547 result
[0], a
[0], a
[1], b
[0], b
[1]);
553 GLfloat a
[4], b
[4], result
[4];
554 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
555 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
556 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
557 store_vector4(inst
, machine
, result
);
559 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
560 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
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] = DOT4(a
, b
);
570 store_vector4(inst
, machine
, result
);
572 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
573 result
[0], a
[0], a
[1], a
[2], a
[3],
574 b
[0], b
[1], b
[2], b
[3]);
580 GLfloat a
[4], b
[4], result
[4];
581 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
582 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
583 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
) + b
[3];
584 store_vector4(inst
, machine
, result
);
587 case OPCODE_DST
: /* Distance vector */
589 GLfloat a
[4], b
[4], result
[4];
590 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
591 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
593 result
[1] = a
[1] * b
[1];
596 store_vector4(inst
, machine
, result
);
601 GLfloat t
[4], q
[4], floor_t0
;
602 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
603 floor_t0
= floorf(t
[0]);
604 if (floor_t0
> FLT_MAX_EXP
) {
605 SET_POS_INFINITY(q
[0]);
606 SET_POS_INFINITY(q
[2]);
608 else if (floor_t0
< FLT_MIN_EXP
) {
613 q
[0] = ldexpf(1.0, (int) floor_t0
);
614 /* Note: GL_NV_vertex_program expects
615 * result.z = result.x * APPX(result.y)
616 * We do what the ARB extension says.
620 q
[1] = t
[0] - floor_t0
;
622 store_vector4( inst
, machine
, q
);
625 case OPCODE_EX2
: /* Exponential base 2 */
627 GLfloat a
[4], result
[4], val
;
628 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
631 if (IS_INF_OR_NAN(val))
634 result
[0] = result
[1] = result
[2] = result
[3] = val
;
635 store_vector4(inst
, machine
, result
);
640 GLfloat a
[4], result
[4];
641 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
642 result
[0] = floorf(a
[0]);
643 result
[1] = floorf(a
[1]);
644 result
[2] = floorf(a
[2]);
645 result
[3] = floorf(a
[3]);
646 store_vector4(inst
, machine
, result
);
651 GLfloat a
[4], result
[4];
652 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
653 result
[0] = a
[0] - floorf(a
[0]);
654 result
[1] = a
[1] - floorf(a
[1]);
655 result
[2] = a
[2] - floorf(a
[2]);
656 result
[3] = a
[3] - floorf(a
[3]);
657 store_vector4(inst
, machine
, result
);
663 assert(program
->Instructions
[inst
->BranchTarget
].Opcode
665 program
->Instructions
[inst
->BranchTarget
].Opcode
668 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
670 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
671 cond
= (a
[0] != 0.0F
);
674 printf("IF: %d\n", cond
);
678 /* do if-clause (just continue execution) */
681 /* go to the instruction after ELSE or ENDIF */
682 assert(inst
->BranchTarget
>= 0);
683 pc
= inst
->BranchTarget
;
689 assert(program
->Instructions
[inst
->BranchTarget
].Opcode
691 assert(inst
->BranchTarget
>= 0);
692 pc
= inst
->BranchTarget
;
697 case OPCODE_KIL
: /* ARB_f_p only */
700 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
702 printf("KIL if (%g %g %g %g) <= 0.0\n",
703 a
[0], a
[1], a
[2], a
[3]);
706 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
711 case OPCODE_LG2
: /* log base 2 */
713 GLfloat a
[4], result
[4], val
;
714 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
715 /* The fast LOG2 macro doesn't meet the precision requirements.
721 val
= logf(a
[0]) * 1.442695F
;
723 result
[0] = result
[1] = result
[2] = result
[3] = val
;
724 store_vector4(inst
, machine
, result
);
729 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
730 GLfloat a
[4], result
[4];
731 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
732 a
[0] = MAX2(a
[0], 0.0F
);
733 a
[1] = MAX2(a
[1], 0.0F
);
734 /* XXX ARB version clamps a[3], NV version doesn't */
735 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
738 /* XXX we could probably just use pow() here */
740 if (a
[1] == 0.0F
&& a
[3] == 0.0F
)
743 result
[2] = powf(a
[1], a
[3]);
749 store_vector4(inst
, machine
, result
);
751 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
752 result
[0], result
[1], result
[2], result
[3],
753 a
[0], a
[1], a
[2], a
[3]);
759 GLfloat t
[4], q
[4], abs_t0
;
760 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
761 abs_t0
= fabsf(t
[0]);
762 if (abs_t0
!= 0.0F
) {
763 if (IS_INF_OR_NAN(abs_t0
))
765 SET_POS_INFINITY(q
[0]);
767 SET_POS_INFINITY(q
[2]);
771 GLfloat mantissa
= frexpf(t
[0], &exponent
);
772 q
[0] = (GLfloat
) (exponent
- 1);
773 q
[1] = 2.0F
* mantissa
; /* map [.5, 1) -> [1, 2) */
775 /* The fast LOG2 macro doesn't meet the precision
778 q
[2] = logf(t
[0]) * 1.442695F
;
782 SET_NEG_INFINITY(q
[0]);
784 SET_NEG_INFINITY(q
[2]);
787 store_vector4(inst
, machine
, q
);
792 GLfloat a
[4], b
[4], c
[4], result
[4];
793 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
794 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
795 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
796 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
797 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
798 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
799 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
800 store_vector4(inst
, machine
, result
);
802 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
803 "(%g %g %g %g), (%g %g %g %g)\n",
804 result
[0], result
[1], result
[2], result
[3],
805 a
[0], a
[1], a
[2], a
[3],
806 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
812 GLfloat a
[4], b
[4], c
[4], result
[4];
813 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
814 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
815 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
816 result
[0] = a
[0] * b
[0] + c
[0];
817 result
[1] = a
[1] * b
[1] + c
[1];
818 result
[2] = a
[2] * b
[2] + c
[2];
819 result
[3] = a
[3] * b
[3] + c
[3];
820 store_vector4(inst
, machine
, result
);
822 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
823 "(%g %g %g %g) + (%g %g %g %g)\n",
824 result
[0], result
[1], result
[2], result
[3],
825 a
[0], a
[1], a
[2], a
[3],
826 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[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] = MAX2(a
[0], b
[0]);
836 result
[1] = MAX2(a
[1], b
[1]);
837 result
[2] = MAX2(a
[2], b
[2]);
838 result
[3] = MAX2(a
[3], b
[3]);
839 store_vector4(inst
, machine
, result
);
841 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
842 result
[0], result
[1], result
[2], result
[3],
843 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
849 GLfloat a
[4], b
[4], result
[4];
850 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
851 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
852 result
[0] = MIN2(a
[0], b
[0]);
853 result
[1] = MIN2(a
[1], b
[1]);
854 result
[2] = MIN2(a
[2], b
[2]);
855 result
[3] = MIN2(a
[3], b
[3]);
856 store_vector4(inst
, machine
, result
);
862 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
863 store_vector4(inst
, machine
, result
);
865 printf("MOV (%g %g %g %g)\n",
866 result
[0], result
[1], result
[2], result
[3]);
872 GLfloat a
[4], b
[4], result
[4];
873 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
874 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
875 result
[0] = a
[0] * b
[0];
876 result
[1] = a
[1] * b
[1];
877 result
[2] = a
[2] * b
[2];
878 result
[3] = a
[3] * b
[3];
879 store_vector4(inst
, machine
, result
);
881 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
882 result
[0], result
[1], result
[2], result
[3],
883 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
889 GLfloat a
[4], result
[4];
890 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
894 result
[3] = _mesa_noise1(a
[0]);
895 store_vector4(inst
, machine
, result
);
900 GLfloat a
[4], result
[4];
901 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
904 result
[2] = result
[3] = _mesa_noise2(a
[0], a
[1]);
905 store_vector4(inst
, machine
, result
);
910 GLfloat a
[4], result
[4];
911 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
915 result
[3] = _mesa_noise3(a
[0], a
[1], a
[2]);
916 store_vector4(inst
, machine
, result
);
921 GLfloat a
[4], result
[4];
922 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
926 result
[3] = _mesa_noise4(a
[0], a
[1], a
[2], a
[3]);
927 store_vector4(inst
, machine
, result
);
934 GLfloat a
[4], b
[4], result
[4];
935 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
936 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
937 result
[0] = result
[1] = result
[2] = result
[3]
939 store_vector4(inst
, machine
, result
);
945 GLfloat a
[4], result
[4];
946 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
950 else if (IS_INF_OR_NAN(a
[0]))
951 printf("RCP(inf)\n");
953 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
954 store_vector4(inst
, machine
, result
);
957 case OPCODE_RET
: /* return from subroutine (conditional) */
958 if (machine
->StackDepth
== 0) {
959 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
961 /* subtract one because of pc++ in the for loop */
962 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
964 case OPCODE_RSQ
: /* 1 / sqrt() */
966 GLfloat a
[4], result
[4];
967 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
969 result
[0] = result
[1] = result
[2] = result
[3] = 1.0f
/ sqrtf(a
[0]);
970 store_vector4(inst
, machine
, result
);
972 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
976 case OPCODE_SCS
: /* sine and cos */
978 GLfloat a
[4], result
[4];
979 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
980 result
[0] = cosf(a
[0]);
981 result
[1] = sinf(a
[0]);
982 result
[2] = 0.0F
; /* undefined! */
983 result
[3] = 0.0F
; /* undefined! */
984 store_vector4(inst
, machine
, result
);
987 case OPCODE_SEQ
: /* set on equal */
989 GLfloat a
[4], b
[4], result
[4];
990 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
991 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
992 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
993 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
994 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
995 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
996 store_vector4(inst
, machine
, result
);
998 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
999 result
[0], result
[1], result
[2], result
[3],
1000 a
[0], a
[1], a
[2], a
[3],
1001 b
[0], b
[1], b
[2], b
[3]);
1005 case OPCODE_SGE
: /* set on greater or equal */
1007 GLfloat a
[4], b
[4], result
[4];
1008 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1009 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1010 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1011 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1012 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1013 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1014 store_vector4(inst
, machine
, result
);
1016 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1017 result
[0], result
[1], result
[2], result
[3],
1018 a
[0], a
[1], a
[2], a
[3],
1019 b
[0], b
[1], b
[2], b
[3]);
1023 case OPCODE_SGT
: /* set on greater */
1025 GLfloat a
[4], b
[4], result
[4];
1026 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1027 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1028 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1029 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1030 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1031 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1032 store_vector4(inst
, machine
, result
);
1034 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1035 result
[0], result
[1], result
[2], result
[3],
1036 a
[0], a
[1], a
[2], a
[3],
1037 b
[0], b
[1], b
[2], b
[3]);
1043 GLfloat a
[4], result
[4];
1044 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1045 result
[0] = result
[1] = result
[2] = result
[3]
1047 store_vector4(inst
, machine
, result
);
1050 case OPCODE_SLE
: /* set on less or equal */
1052 GLfloat a
[4], b
[4], result
[4];
1053 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1054 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1055 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1056 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1057 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1058 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1059 store_vector4(inst
, machine
, result
);
1061 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1062 result
[0], result
[1], result
[2], result
[3],
1063 a
[0], a
[1], a
[2], a
[3],
1064 b
[0], b
[1], b
[2], b
[3]);
1068 case OPCODE_SLT
: /* set on less */
1070 GLfloat a
[4], b
[4], result
[4];
1071 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1072 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1073 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1074 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1075 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1076 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1077 store_vector4(inst
, machine
, result
);
1079 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1080 result
[0], result
[1], result
[2], result
[3],
1081 a
[0], a
[1], a
[2], a
[3],
1082 b
[0], b
[1], b
[2], b
[3]);
1086 case OPCODE_SNE
: /* set on not equal */
1088 GLfloat a
[4], b
[4], result
[4];
1089 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1090 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1091 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1092 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1093 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1094 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1095 store_vector4(inst
, machine
, result
);
1097 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1098 result
[0], result
[1], result
[2], result
[3],
1099 a
[0], a
[1], a
[2], a
[3],
1100 b
[0], b
[1], b
[2], b
[3]);
1104 case OPCODE_SSG
: /* set sign (-1, 0 or +1) */
1106 GLfloat a
[4], result
[4];
1107 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1108 result
[0] = (GLfloat
) ((a
[0] > 0.0F
) - (a
[0] < 0.0F
));
1109 result
[1] = (GLfloat
) ((a
[1] > 0.0F
) - (a
[1] < 0.0F
));
1110 result
[2] = (GLfloat
) ((a
[2] > 0.0F
) - (a
[2] < 0.0F
));
1111 result
[3] = (GLfloat
) ((a
[3] > 0.0F
) - (a
[3] < 0.0F
));
1112 store_vector4(inst
, machine
, result
);
1117 GLfloat a
[4], b
[4], result
[4];
1118 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1119 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1120 result
[0] = a
[0] - b
[0];
1121 result
[1] = a
[1] - b
[1];
1122 result
[2] = a
[2] - b
[2];
1123 result
[3] = a
[3] - b
[3];
1124 store_vector4(inst
, machine
, result
);
1126 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1127 result
[0], result
[1], result
[2], result
[3],
1128 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1132 case OPCODE_SWZ
: /* extended swizzle */
1134 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1135 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
1138 for (i
= 0; i
< 4; i
++) {
1139 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1140 if (swz
== SWIZZLE_ZERO
)
1142 else if (swz
== SWIZZLE_ONE
)
1146 result
[i
] = src
[swz
];
1148 if (source
->Negate
& (1 << i
))
1149 result
[i
] = -result
[i
];
1151 store_vector4(inst
, machine
, result
);
1154 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1155 /* Simple texel lookup */
1157 GLfloat texcoord
[4], color
[4];
1158 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1160 /* For TEX, texcoord.Q should not be used and its value should not
1161 * matter (at most, we pass coord.xyz to texture3D() in GLSL).
1162 * Set Q=1 so that FetchTexelDeriv() doesn't get a garbage value
1163 * which is effectively what happens when the texcoord swizzle
1168 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1171 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1172 color
[0], color
[1], color
[2], color
[3],
1174 texcoord
[0], texcoord
[1], texcoord
[2], texcoord
[3]);
1176 store_vector4(inst
, machine
, color
);
1179 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1180 /* Texel lookup with LOD bias */
1182 GLfloat texcoord
[4], color
[4], lodBias
;
1184 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1186 /* texcoord[3] is the bias to add to lambda */
1187 lodBias
= texcoord
[3];
1189 fetch_texel(ctx
, machine
, inst
, texcoord
, lodBias
, color
);
1192 printf("TXB (%g, %g, %g, %g) = texture[%d][%g %g %g %g]"
1194 color
[0], color
[1], color
[2], color
[3],
1203 store_vector4(inst
, machine
, color
);
1206 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1207 /* Texture lookup w/ partial derivatives for LOD */
1209 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1210 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1211 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1212 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1213 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1215 inst
->TexSrcUnit
, color
);
1216 store_vector4(inst
, machine
, color
);
1220 /* Texel lookup with explicit LOD */
1222 GLfloat texcoord
[4], color
[4], lod
;
1224 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1226 /* texcoord[3] is the LOD */
1229 machine
->FetchTexelLod(ctx
, texcoord
, lod
,
1230 machine
->Samplers
[inst
->TexSrcUnit
], color
);
1232 store_vector4(inst
, machine
, color
);
1235 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1236 /* Texture lookup w/ projective divide */
1238 GLfloat texcoord
[4], color
[4];
1240 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1241 /* Not so sure about this test - if texcoord[3] is
1242 * zero, we'd probably be fine except for an assert in
1243 * IROUND_POS() which gets triggered by the inf values created.
1245 if (texcoord
[3] != 0.0F
) {
1246 texcoord
[0] /= texcoord
[3];
1247 texcoord
[1] /= texcoord
[3];
1248 texcoord
[2] /= texcoord
[3];
1251 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1253 store_vector4(inst
, machine
, color
);
1256 case OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1257 /* Texture lookup w/ projective divide, as above, but do not
1258 * do the divide by w if sampling from a cube map.
1261 GLfloat texcoord
[4], color
[4];
1263 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1264 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1265 texcoord
[3] != 0.0F
) {
1266 texcoord
[0] /= texcoord
[3];
1267 texcoord
[1] /= texcoord
[3];
1268 texcoord
[2] /= texcoord
[3];
1271 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1273 store_vector4(inst
, machine
, color
);
1276 case OPCODE_TRUNC
: /* truncate toward zero */
1278 GLfloat a
[4], result
[4];
1279 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1280 result
[0] = (GLfloat
) (GLint
) a
[0];
1281 result
[1] = (GLfloat
) (GLint
) a
[1];
1282 result
[2] = (GLfloat
) (GLint
) a
[2];
1283 result
[3] = (GLfloat
) (GLint
) a
[3];
1284 store_vector4(inst
, machine
, result
);
1287 case OPCODE_XPD
: /* cross product */
1289 GLfloat a
[4], b
[4], result
[4];
1290 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1291 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1292 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1293 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1294 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1296 store_vector4(inst
, machine
, result
);
1298 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1299 result
[0], result
[1], result
[2], result
[3],
1300 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1307 _mesa_problem(ctx
, "Bad opcode %d in _mesa_execute_program",
1309 return GL_TRUE
; /* return value doesn't matter */
1313 if (numExec
> maxExec
) {
1314 static GLboolean reported
= GL_FALSE
;
1316 _mesa_problem(ctx
, "Infinite loop detected in fragment program");