2c77538c2af8a7b43b569b5f5b4e0019b9c2d023
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(const struct prog_src_register
*source
,
226 const struct gl_program_machine
*machine
,
227 char xOrY
, GLfloat result
[4])
229 if (source
->File
== PROGRAM_INPUT
&&
230 source
->Index
< (GLint
) machine
->NumDeriv
) {
231 const GLint col
= machine
->CurElement
;
232 const GLfloat w
= machine
->Attribs
[VARYING_SLOT_POS
][col
][3];
233 const GLfloat invQ
= 1.0f
/ w
;
237 deriv
[0] = machine
->DerivX
[source
->Index
][0] * invQ
;
238 deriv
[1] = machine
->DerivX
[source
->Index
][1] * invQ
;
239 deriv
[2] = machine
->DerivX
[source
->Index
][2] * invQ
;
240 deriv
[3] = machine
->DerivX
[source
->Index
][3] * invQ
;
243 deriv
[0] = machine
->DerivY
[source
->Index
][0] * invQ
;
244 deriv
[1] = machine
->DerivY
[source
->Index
][1] * invQ
;
245 deriv
[2] = machine
->DerivY
[source
->Index
][2] * invQ
;
246 deriv
[3] = machine
->DerivY
[source
->Index
][3] * invQ
;
249 result
[0] = deriv
[GET_SWZ(source
->Swizzle
, 0)];
250 result
[1] = deriv
[GET_SWZ(source
->Swizzle
, 1)];
251 result
[2] = deriv
[GET_SWZ(source
->Swizzle
, 2)];
252 result
[3] = deriv
[GET_SWZ(source
->Swizzle
, 3)];
254 if (source
->Negate
) {
255 assert(source
->Negate
== NEGATE_XYZW
);
256 result
[0] = -result
[0];
257 result
[1] = -result
[1];
258 result
[2] = -result
[2];
259 result
[3] = -result
[3];
263 ASSIGN_4V(result
, 0.0, 0.0, 0.0, 0.0);
269 * As above, but only return result[0] element.
272 fetch_vector1(const struct prog_src_register
*source
,
273 const struct gl_program_machine
*machine
, GLfloat result
[4])
275 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
277 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
279 if (source
->Negate
) {
280 result
[0] = -result
[0];
286 * Fetch texel from texture. Use partial derivatives when possible.
289 fetch_texel(struct gl_context
*ctx
,
290 const struct gl_program_machine
*machine
,
291 const struct prog_instruction
*inst
,
292 const GLfloat texcoord
[4], GLfloat lodBias
,
295 const GLuint unit
= machine
->Samplers
[inst
->TexSrcUnit
];
297 /* Note: we only have the right derivatives for fragment input attribs.
299 if (machine
->NumDeriv
> 0 &&
300 inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
301 inst
->SrcReg
[0].Index
== VARYING_SLOT_TEX0
+ inst
->TexSrcUnit
) {
302 /* simple texture fetch for which we should have derivatives */
303 GLuint attr
= inst
->SrcReg
[0].Index
;
304 machine
->FetchTexelDeriv(ctx
, texcoord
,
305 machine
->DerivX
[attr
],
306 machine
->DerivY
[attr
],
307 lodBias
, unit
, color
);
310 machine
->FetchTexelLod(ctx
, texcoord
, lodBias
, unit
, color
);
316 * Store 4 floats into a register. Observe the instructions saturate and
317 * set-condition-code flags.
320 store_vector4(const struct prog_instruction
*inst
,
321 struct gl_program_machine
*machine
, const GLfloat value
[4])
323 const struct prog_dst_register
*dstReg
= &(inst
->DstReg
);
324 const GLboolean clamp
= inst
->Saturate
;
325 GLuint writeMask
= dstReg
->WriteMask
;
326 GLfloat clampedValue
[4];
327 GLfloat
*dst
= get_dst_register_pointer(dstReg
, machine
);
330 if (value
[0] > 1.0e10
||
331 IS_INF_OR_NAN(value
[0]) ||
332 IS_INF_OR_NAN(value
[1]) ||
333 IS_INF_OR_NAN(value
[2]) || IS_INF_OR_NAN(value
[3]))
334 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
338 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
339 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
340 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
341 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
342 value
= clampedValue
;
346 assert(!IS_INF_OR_NAN(value
[0]));
347 assert(!IS_INF_OR_NAN(value
[0]));
348 assert(!IS_INF_OR_NAN(value
[0]));
349 assert(!IS_INF_OR_NAN(value
[0]));
352 if (writeMask
& WRITEMASK_X
)
354 if (writeMask
& WRITEMASK_Y
)
356 if (writeMask
& WRITEMASK_Z
)
358 if (writeMask
& WRITEMASK_W
)
364 * Execute the given vertex/fragment program.
366 * \param ctx rendering context
367 * \param program the program to execute
368 * \param machine machine state (must be initialized)
369 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
372 _mesa_execute_program(struct gl_context
* ctx
,
373 const struct gl_program
*program
,
374 struct gl_program_machine
*machine
)
376 const GLuint numInst
= program
->arb
.NumInstructions
;
377 const GLuint maxExec
= 65536;
378 GLuint pc
, numExec
= 0;
380 machine
->CurProgram
= program
;
383 printf("execute program %u --------------------\n", program
->Id
);
386 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
387 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
390 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
393 for (pc
= 0; pc
< numInst
; pc
++) {
394 const struct prog_instruction
*inst
= program
->arb
.Instructions
+ pc
;
397 _mesa_print_instruction(inst
);
400 switch (inst
->Opcode
) {
403 GLfloat a
[4], result
[4];
404 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
405 result
[0] = fabsf(a
[0]);
406 result
[1] = fabsf(a
[1]);
407 result
[2] = fabsf(a
[2]);
408 result
[3] = fabsf(a
[3]);
409 store_vector4(inst
, machine
, result
);
414 GLfloat a
[4], b
[4], result
[4];
415 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
416 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
417 result
[0] = a
[0] + b
[0];
418 result
[1] = a
[1] + b
[1];
419 result
[2] = a
[2] + b
[2];
420 result
[3] = a
[3] + b
[3];
421 store_vector4(inst
, machine
, result
);
423 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
424 result
[0], result
[1], result
[2], result
[3],
425 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
432 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
433 machine
->AddressReg
[0][0] = IFLOOR(t
[0]);
435 printf("ARL %d\n", machine
->AddressReg
[0][0]);
441 assert(program
->arb
.Instructions
[inst
->BranchTarget
].Opcode
445 /* subtract 1 here since pc is incremented by for(pc) loop */
446 assert(program
->arb
.Instructions
[inst
->BranchTarget
].Opcode
448 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
450 case OPCODE_BGNSUB
: /* begin subroutine */
452 case OPCODE_ENDSUB
: /* end subroutine */
454 case OPCODE_BRK
: /* break out of loop (conditional) */
455 assert(program
->arb
.Instructions
[inst
->BranchTarget
].Opcode
457 /* break out of loop */
458 /* pc++ at end of for-loop will put us after the ENDLOOP inst */
459 pc
= inst
->BranchTarget
;
461 case OPCODE_CONT
: /* continue loop (conditional) */
462 assert(program
->arb
.Instructions
[inst
->BranchTarget
].Opcode
464 /* continue at ENDLOOP */
465 /* Subtract 1 here since we'll do pc++ at end of for-loop */
466 pc
= inst
->BranchTarget
- 1;
468 case OPCODE_CAL
: /* Call subroutine (conditional) */
469 /* call the subroutine */
470 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
471 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
473 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1; /* next inst */
474 /* Subtract 1 here since we'll do pc++ at end of for-loop */
475 pc
= inst
->BranchTarget
- 1;
479 GLfloat a
[4], b
[4], c
[4], result
[4];
480 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
481 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
482 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
483 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
484 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
485 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
486 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
487 store_vector4(inst
, machine
, result
);
489 printf("CMP (%g %g %g %g) = (%g %g %g %g) < 0 ? (%g %g %g %g) : (%g %g %g %g)\n",
490 result
[0], result
[1], result
[2], result
[3],
491 a
[0], a
[1], a
[2], a
[3],
492 b
[0], b
[1], b
[2], b
[3],
493 c
[0], c
[1], c
[2], c
[3]);
499 GLfloat a
[4], result
[4];
500 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
501 result
[0] = result
[1] = result
[2] = result
[3]
503 store_vector4(inst
, machine
, result
);
506 case OPCODE_DDX
: /* Partial derivative with respect to X */
509 fetch_vector4_deriv(&inst
->SrcReg
[0], machine
, 'X', result
);
510 store_vector4(inst
, machine
, result
);
513 case OPCODE_DDY
: /* Partial derivative with respect to Y */
516 fetch_vector4_deriv(&inst
->SrcReg
[0], machine
, 'Y', result
);
517 store_vector4(inst
, machine
, result
);
522 GLfloat a
[4], b
[4], result
[4];
523 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
524 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
525 result
[0] = result
[1] = result
[2] = result
[3] = DOT2(a
, b
);
526 store_vector4(inst
, machine
, result
);
528 printf("DP2 %g = (%g %g) . (%g %g)\n",
529 result
[0], a
[0], a
[1], b
[0], b
[1]);
535 GLfloat a
[4], b
[4], result
[4];
536 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
537 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
538 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
539 store_vector4(inst
, machine
, result
);
541 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
542 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
548 GLfloat a
[4], b
[4], result
[4];
549 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
550 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
551 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
, b
);
552 store_vector4(inst
, machine
, result
);
554 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
555 result
[0], a
[0], a
[1], a
[2], a
[3],
556 b
[0], b
[1], b
[2], b
[3]);
562 GLfloat a
[4], b
[4], result
[4];
563 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
564 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
565 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
) + b
[3];
566 store_vector4(inst
, machine
, result
);
569 case OPCODE_DST
: /* Distance vector */
571 GLfloat a
[4], b
[4], result
[4];
572 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
573 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
575 result
[1] = a
[1] * b
[1];
578 store_vector4(inst
, machine
, result
);
583 GLfloat t
[4], q
[4], floor_t0
;
584 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
585 floor_t0
= floorf(t
[0]);
586 if (floor_t0
> FLT_MAX_EXP
) {
587 SET_POS_INFINITY(q
[0]);
588 SET_POS_INFINITY(q
[2]);
590 else if (floor_t0
< FLT_MIN_EXP
) {
595 q
[0] = ldexpf(1.0, (int) floor_t0
);
596 /* Note: GL_NV_vertex_program expects
597 * result.z = result.x * APPX(result.y)
598 * We do what the ARB extension says.
602 q
[1] = t
[0] - floor_t0
;
604 store_vector4( inst
, machine
, q
);
607 case OPCODE_EX2
: /* Exponential base 2 */
609 GLfloat a
[4], result
[4], val
;
610 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
613 if (IS_INF_OR_NAN(val))
616 result
[0] = result
[1] = result
[2] = result
[3] = val
;
617 store_vector4(inst
, machine
, result
);
622 GLfloat a
[4], result
[4];
623 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
624 result
[0] = floorf(a
[0]);
625 result
[1] = floorf(a
[1]);
626 result
[2] = floorf(a
[2]);
627 result
[3] = floorf(a
[3]);
628 store_vector4(inst
, machine
, result
);
633 GLfloat a
[4], result
[4];
634 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
635 result
[0] = a
[0] - floorf(a
[0]);
636 result
[1] = a
[1] - floorf(a
[1]);
637 result
[2] = a
[2] - floorf(a
[2]);
638 result
[3] = a
[3] - floorf(a
[3]);
639 store_vector4(inst
, machine
, result
);
645 assert(program
->arb
.Instructions
[inst
->BranchTarget
].Opcode
647 program
->arb
.Instructions
[inst
->BranchTarget
].Opcode
651 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
652 cond
= (a
[0] != 0.0F
);
654 printf("IF: %d\n", cond
);
658 /* do if-clause (just continue execution) */
661 /* go to the instruction after ELSE or ENDIF */
662 assert(inst
->BranchTarget
>= 0);
663 pc
= inst
->BranchTarget
;
669 assert(program
->arb
.Instructions
[inst
->BranchTarget
].Opcode
671 assert(inst
->BranchTarget
>= 0);
672 pc
= inst
->BranchTarget
;
677 case OPCODE_KIL
: /* ARB_f_p only */
680 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
682 printf("KIL if (%g %g %g %g) <= 0.0\n",
683 a
[0], a
[1], a
[2], a
[3]);
686 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
691 case OPCODE_LG2
: /* log base 2 */
693 GLfloat a
[4], result
[4], val
;
694 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
695 /* The fast LOG2 macro doesn't meet the precision requirements.
701 val
= logf(a
[0]) * 1.442695F
;
703 result
[0] = result
[1] = result
[2] = result
[3] = val
;
704 store_vector4(inst
, machine
, result
);
709 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
710 GLfloat a
[4], result
[4];
711 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
712 a
[0] = MAX2(a
[0], 0.0F
);
713 a
[1] = MAX2(a
[1], 0.0F
);
714 /* XXX ARB version clamps a[3], NV version doesn't */
715 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
718 /* XXX we could probably just use pow() here */
720 if (a
[1] == 0.0F
&& a
[3] == 0.0F
)
723 result
[2] = powf(a
[1], a
[3]);
729 store_vector4(inst
, machine
, result
);
731 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
732 result
[0], result
[1], result
[2], result
[3],
733 a
[0], a
[1], a
[2], a
[3]);
739 GLfloat t
[4], q
[4], abs_t0
;
740 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
741 abs_t0
= fabsf(t
[0]);
742 if (abs_t0
!= 0.0F
) {
743 if (IS_INF_OR_NAN(abs_t0
))
745 SET_POS_INFINITY(q
[0]);
747 SET_POS_INFINITY(q
[2]);
751 GLfloat mantissa
= frexpf(t
[0], &exponent
);
752 q
[0] = (GLfloat
) (exponent
- 1);
753 q
[1] = 2.0F
* mantissa
; /* map [.5, 1) -> [1, 2) */
755 /* The fast LOG2 macro doesn't meet the precision
758 q
[2] = logf(t
[0]) * 1.442695F
;
762 SET_NEG_INFINITY(q
[0]);
764 SET_NEG_INFINITY(q
[2]);
767 store_vector4(inst
, machine
, q
);
772 GLfloat a
[4], b
[4], c
[4], result
[4];
773 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
774 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
775 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
776 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
777 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
778 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
779 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
780 store_vector4(inst
, machine
, result
);
782 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
783 "(%g %g %g %g), (%g %g %g %g)\n",
784 result
[0], result
[1], result
[2], result
[3],
785 a
[0], a
[1], a
[2], a
[3],
786 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
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] + c
[0];
797 result
[1] = a
[1] * b
[1] + c
[1];
798 result
[2] = a
[2] * b
[2] + c
[2];
799 result
[3] = a
[3] * b
[3] + c
[3];
800 store_vector4(inst
, machine
, result
);
802 printf("MAD (%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], result
[4];
813 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
814 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
815 result
[0] = MAX2(a
[0], b
[0]);
816 result
[1] = MAX2(a
[1], b
[1]);
817 result
[2] = MAX2(a
[2], b
[2]);
818 result
[3] = MAX2(a
[3], b
[3]);
819 store_vector4(inst
, machine
, result
);
821 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
822 result
[0], result
[1], result
[2], result
[3],
823 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
829 GLfloat a
[4], b
[4], result
[4];
830 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
831 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
832 result
[0] = MIN2(a
[0], b
[0]);
833 result
[1] = MIN2(a
[1], b
[1]);
834 result
[2] = MIN2(a
[2], b
[2]);
835 result
[3] = MIN2(a
[3], b
[3]);
836 store_vector4(inst
, machine
, result
);
842 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
843 store_vector4(inst
, machine
, result
);
845 printf("MOV (%g %g %g %g)\n",
846 result
[0], result
[1], result
[2], result
[3]);
852 GLfloat a
[4], b
[4], result
[4];
853 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
854 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
855 result
[0] = a
[0] * b
[0];
856 result
[1] = a
[1] * b
[1];
857 result
[2] = a
[2] * b
[2];
858 result
[3] = a
[3] * b
[3];
859 store_vector4(inst
, machine
, result
);
861 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
862 result
[0], result
[1], result
[2], result
[3],
863 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
869 GLfloat a
[4], result
[4];
870 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
874 result
[3] = _mesa_noise1(a
[0]);
875 store_vector4(inst
, machine
, result
);
880 GLfloat a
[4], result
[4];
881 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
884 result
[2] = result
[3] = _mesa_noise2(a
[0], a
[1]);
885 store_vector4(inst
, machine
, result
);
890 GLfloat a
[4], result
[4];
891 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
895 result
[3] = _mesa_noise3(a
[0], a
[1], a
[2]);
896 store_vector4(inst
, machine
, result
);
901 GLfloat a
[4], result
[4];
902 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
906 result
[3] = _mesa_noise4(a
[0], a
[1], a
[2], a
[3]);
907 store_vector4(inst
, machine
, result
);
914 GLfloat a
[4], b
[4], result
[4];
915 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
916 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
917 result
[0] = result
[1] = result
[2] = result
[3]
919 store_vector4(inst
, machine
, result
);
925 GLfloat a
[4], result
[4];
926 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
930 else if (IS_INF_OR_NAN(a
[0]))
931 printf("RCP(inf)\n");
933 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
934 store_vector4(inst
, machine
, result
);
937 case OPCODE_RET
: /* return from subroutine (conditional) */
938 if (machine
->StackDepth
== 0) {
939 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
941 /* subtract one because of pc++ in the for loop */
942 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
944 case OPCODE_RSQ
: /* 1 / sqrt() */
946 GLfloat a
[4], result
[4];
947 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
949 result
[0] = result
[1] = result
[2] = result
[3] = 1.0f
/ sqrtf(a
[0]);
950 store_vector4(inst
, machine
, result
);
952 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
956 case OPCODE_SCS
: /* sine and cos */
958 GLfloat a
[4], result
[4];
959 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
960 result
[0] = cosf(a
[0]);
961 result
[1] = sinf(a
[0]);
962 result
[2] = 0.0F
; /* undefined! */
963 result
[3] = 0.0F
; /* undefined! */
964 store_vector4(inst
, machine
, result
);
967 case OPCODE_SGE
: /* set on greater or equal */
969 GLfloat a
[4], b
[4], result
[4];
970 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
971 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
972 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
973 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
974 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
975 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
976 store_vector4(inst
, machine
, result
);
978 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
979 result
[0], result
[1], result
[2], result
[3],
980 a
[0], a
[1], a
[2], a
[3],
981 b
[0], b
[1], b
[2], b
[3]);
987 GLfloat a
[4], result
[4];
988 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
989 result
[0] = result
[1] = result
[2] = result
[3]
991 store_vector4(inst
, machine
, result
);
994 case OPCODE_SLT
: /* set on less */
996 GLfloat a
[4], b
[4], result
[4];
997 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
998 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
999 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1000 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1001 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1002 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1003 store_vector4(inst
, machine
, result
);
1005 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1006 result
[0], result
[1], result
[2], result
[3],
1007 a
[0], a
[1], a
[2], a
[3],
1008 b
[0], b
[1], b
[2], b
[3]);
1012 case OPCODE_SSG
: /* set sign (-1, 0 or +1) */
1014 GLfloat a
[4], result
[4];
1015 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1016 result
[0] = (GLfloat
) ((a
[0] > 0.0F
) - (a
[0] < 0.0F
));
1017 result
[1] = (GLfloat
) ((a
[1] > 0.0F
) - (a
[1] < 0.0F
));
1018 result
[2] = (GLfloat
) ((a
[2] > 0.0F
) - (a
[2] < 0.0F
));
1019 result
[3] = (GLfloat
) ((a
[3] > 0.0F
) - (a
[3] < 0.0F
));
1020 store_vector4(inst
, machine
, result
);
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];
1029 result
[1] = a
[1] - b
[1];
1030 result
[2] = a
[2] - b
[2];
1031 result
[3] = a
[3] - b
[3];
1032 store_vector4(inst
, machine
, result
);
1034 printf("SUB (%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], b
[0], b
[1], b
[2], b
[3]);
1040 case OPCODE_SWZ
: /* extended swizzle */
1042 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1043 const GLfloat
*src
= get_src_register_pointer(source
, machine
);
1046 for (i
= 0; i
< 4; i
++) {
1047 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1048 if (swz
== SWIZZLE_ZERO
)
1050 else if (swz
== SWIZZLE_ONE
)
1054 result
[i
] = src
[swz
];
1056 if (source
->Negate
& (1 << i
))
1057 result
[i
] = -result
[i
];
1059 store_vector4(inst
, machine
, result
);
1062 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1063 /* Simple texel lookup */
1065 GLfloat texcoord
[4], color
[4];
1066 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1068 /* For TEX, texcoord.Q should not be used and its value should not
1069 * matter (at most, we pass coord.xyz to texture3D() in GLSL).
1070 * Set Q=1 so that FetchTexelDeriv() doesn't get a garbage value
1071 * which is effectively what happens when the texcoord swizzle
1076 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1079 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1080 color
[0], color
[1], color
[2], color
[3],
1082 texcoord
[0], texcoord
[1], texcoord
[2], texcoord
[3]);
1084 store_vector4(inst
, machine
, color
);
1087 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1088 /* Texel lookup with LOD bias */
1090 GLfloat texcoord
[4], color
[4], lodBias
;
1092 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1094 /* texcoord[3] is the bias to add to lambda */
1095 lodBias
= texcoord
[3];
1097 fetch_texel(ctx
, machine
, inst
, texcoord
, lodBias
, color
);
1100 printf("TXB (%g, %g, %g, %g) = texture[%d][%g %g %g %g]"
1102 color
[0], color
[1], color
[2], color
[3],
1111 store_vector4(inst
, machine
, color
);
1115 /* Texture lookup w/ partial derivatives for LOD */
1117 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1118 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1119 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1120 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1121 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1123 inst
->TexSrcUnit
, color
);
1124 store_vector4(inst
, machine
, color
);
1128 /* Texel lookup with explicit LOD */
1130 GLfloat texcoord
[4], color
[4], lod
;
1132 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1134 /* texcoord[3] is the LOD */
1137 machine
->FetchTexelLod(ctx
, texcoord
, lod
,
1138 machine
->Samplers
[inst
->TexSrcUnit
], color
);
1140 store_vector4(inst
, machine
, color
);
1143 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1144 /* Texture lookup w/ projective divide */
1146 GLfloat texcoord
[4], color
[4];
1148 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1149 /* Not so sure about this test - if texcoord[3] is
1150 * zero, we'd probably be fine except for an assert in
1151 * IROUND_POS() which gets triggered by the inf values created.
1153 if (texcoord
[3] != 0.0F
) {
1154 texcoord
[0] /= texcoord
[3];
1155 texcoord
[1] /= texcoord
[3];
1156 texcoord
[2] /= texcoord
[3];
1159 fetch_texel(ctx
, machine
, inst
, texcoord
, 0.0, color
);
1161 store_vector4(inst
, machine
, color
);
1164 case OPCODE_TRUNC
: /* truncate toward zero */
1166 GLfloat a
[4], result
[4];
1167 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1168 result
[0] = (GLfloat
) (GLint
) a
[0];
1169 result
[1] = (GLfloat
) (GLint
) a
[1];
1170 result
[2] = (GLfloat
) (GLint
) a
[2];
1171 result
[3] = (GLfloat
) (GLint
) a
[3];
1172 store_vector4(inst
, machine
, result
);
1175 case OPCODE_XPD
: /* cross product */
1177 GLfloat a
[4], b
[4], result
[4];
1178 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1179 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1180 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1181 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1182 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1184 store_vector4(inst
, machine
, result
);
1186 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1187 result
[0], result
[1], result
[2], result
[3],
1188 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1195 _mesa_problem(ctx
, "Bad opcode %d in _mesa_execute_program",
1197 return GL_TRUE
; /* return value doesn't matter */
1201 if (numExec
> maxExec
) {
1202 static GLboolean reported
= GL_FALSE
;
1204 _mesa_problem(ctx
, "Infinite loop detected in fragment program");