2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR 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.
42 #include "prog_execute.h"
43 #include "prog_instruction.h"
44 #include "prog_parameter.h"
45 #include "prog_print.h"
46 #include "slang_library_noise.h"
49 /* See comments below for info about this */
57 * Set x to positive or negative infinity.
59 #if defined(USE_IEEE) || defined(_WIN32)
60 #define SET_POS_INFINITY(x) ( *((GLuint *) (void *)&x) = 0x7F800000 )
61 #define SET_NEG_INFINITY(x) ( *((GLuint *) (void *)&x) = 0xFF800000 )
63 #define SET_POS_INFINITY(x) x = __MAXFLOAT
64 #define SET_NEG_INFINITY(x) x = -__MAXFLOAT
66 #define SET_POS_INFINITY(x) x = (GLfloat) HUGE_VAL
67 #define SET_NEG_INFINITY(x) x = (GLfloat) -HUGE_VAL
70 #define SET_FLOAT_BITS(x, bits) ((fi_type *) (void *) &(x))->i = bits
73 static const GLfloat ZeroVec
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
78 * Return a pointer to the 4-element float vector specified by the given
81 static INLINE
const GLfloat
*
82 get_register_pointer(const struct prog_src_register
*source
,
83 const struct gl_program_machine
*machine
)
85 if (source
->RelAddr
) {
86 const GLint reg
= source
->Index
+ machine
->AddressReg
[0][0];
87 if (source
->File
== PROGRAM_ENV_PARAM
)
88 if (reg
< 0 || reg
>= MAX_PROGRAM_ENV_PARAMS
)
91 return machine
->EnvParams
[reg
];
93 const struct gl_program_parameter_list
*params
;
94 ASSERT(source
->File
== PROGRAM_LOCAL_PARAM
||
95 source
->File
== PROGRAM_STATE_VAR
);
96 params
= machine
->CurProgram
->Parameters
;
97 if (reg
< 0 || reg
>= params
->NumParameters
)
100 return params
->ParameterValues
[reg
];
104 switch (source
->File
) {
105 case PROGRAM_TEMPORARY
:
106 ASSERT(source
->Index
< MAX_PROGRAM_TEMPS
);
107 return machine
->Temporaries
[source
->Index
];
110 if (machine
->CurProgram
->Target
== GL_VERTEX_PROGRAM_ARB
) {
111 ASSERT(source
->Index
< VERT_ATTRIB_MAX
);
112 return machine
->VertAttribs
[source
->Index
];
115 ASSERT(source
->Index
< FRAG_ATTRIB_MAX
);
116 return machine
->Attribs
[source
->Index
][machine
->CurElement
];
120 ASSERT(source
->Index
< MAX_PROGRAM_OUTPUTS
);
121 return machine
->Outputs
[source
->Index
];
123 case PROGRAM_LOCAL_PARAM
:
124 ASSERT(source
->Index
< MAX_PROGRAM_LOCAL_PARAMS
);
125 return machine
->CurProgram
->LocalParams
[source
->Index
];
127 case PROGRAM_ENV_PARAM
:
128 ASSERT(source
->Index
< MAX_PROGRAM_ENV_PARAMS
);
129 return machine
->EnvParams
[source
->Index
];
131 case PROGRAM_STATE_VAR
:
133 case PROGRAM_CONSTANT
:
135 case PROGRAM_UNIFORM
:
137 case PROGRAM_NAMED_PARAM
:
138 ASSERT(source
->Index
<
139 (GLint
) machine
->CurProgram
->Parameters
->NumParameters
);
140 return machine
->CurProgram
->Parameters
->ParameterValues
[source
->Index
];
144 "Invalid input register file %d in get_register_pointer()",
151 #if FEATURE_MESA_program_debug
152 static struct gl_program_machine
*CurrentMachine
= NULL
;
155 * For GL_MESA_program_debug.
156 * Return current value (4*GLfloat) of a program register.
157 * Called via ctx->Driver.GetProgramRegister().
160 _mesa_get_program_register(GLcontext
*ctx
, enum register_file file
,
161 GLuint index
, GLfloat val
[4])
163 if (CurrentMachine
) {
164 struct prog_src_register src
;
168 reg
= get_register_pointer(&src
, CurrentMachine
);
172 #endif /* FEATURE_MESA_program_debug */
176 * Fetch a 4-element float vector from the given source register.
177 * Apply swizzling and negating as needed.
180 fetch_vector4(const struct prog_src_register
*source
,
181 const struct gl_program_machine
*machine
, GLfloat result
[4])
183 const GLfloat
*src
= get_register_pointer(source
, machine
);
186 if (source
->Swizzle
== SWIZZLE_NOOP
) {
188 COPY_4V(result
, src
);
191 ASSERT(GET_SWZ(source
->Swizzle
, 0) <= 3);
192 ASSERT(GET_SWZ(source
->Swizzle
, 1) <= 3);
193 ASSERT(GET_SWZ(source
->Swizzle
, 2) <= 3);
194 ASSERT(GET_SWZ(source
->Swizzle
, 3) <= 3);
195 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
196 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
197 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
198 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
201 if (source
->NegateBase
) {
202 result
[0] = -result
[0];
203 result
[1] = -result
[1];
204 result
[2] = -result
[2];
205 result
[3] = -result
[3];
208 result
[0] = FABSF(result
[0]);
209 result
[1] = FABSF(result
[1]);
210 result
[2] = FABSF(result
[2]);
211 result
[3] = FABSF(result
[3]);
213 if (source
->NegateAbs
) {
214 result
[0] = -result
[0];
215 result
[1] = -result
[1];
216 result
[2] = -result
[2];
217 result
[3] = -result
[3];
223 * Fetch the derivative with respect to X for the given register.
224 * \return GL_TRUE if it was easily computed or GL_FALSE if we
225 * need to execute another instance of the program (ugh)!
228 fetch_vector4_deriv(GLcontext
* ctx
,
229 const struct prog_src_register
*source
,
231 char xOrY
, GLint column
, GLfloat result
[4])
235 ASSERT(xOrY
== 'X' || xOrY
== 'Y');
237 switch (source
->Index
) {
238 case FRAG_ATTRIB_WPOS
:
242 src
[2] = span
->attrStepX
[FRAG_ATTRIB_WPOS
][2]
243 / ctx
->DrawBuffer
->_DepthMaxF
;
244 src
[3] = span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
249 src
[2] = span
->attrStepY
[FRAG_ATTRIB_WPOS
][2]
250 / ctx
->DrawBuffer
->_DepthMaxF
;
251 src
[3] = span
->attrStepY
[FRAG_ATTRIB_WPOS
][3];
254 case FRAG_ATTRIB_COL0
:
255 case FRAG_ATTRIB_COL1
:
257 src
[0] = span
->attrStepX
[source
->Index
][0] * (1.0F
/ CHAN_MAXF
);
258 src
[1] = span
->attrStepX
[source
->Index
][1] * (1.0F
/ CHAN_MAXF
);
259 src
[2] = span
->attrStepX
[source
->Index
][2] * (1.0F
/ CHAN_MAXF
);
260 src
[3] = span
->attrStepX
[source
->Index
][3] * (1.0F
/ CHAN_MAXF
);
263 src
[0] = span
->attrStepY
[source
->Index
][0] * (1.0F
/ CHAN_MAXF
);
264 src
[1] = span
->attrStepY
[source
->Index
][1] * (1.0F
/ CHAN_MAXF
);
265 src
[2] = span
->attrStepY
[source
->Index
][2] * (1.0F
/ CHAN_MAXF
);
266 src
[3] = span
->attrStepY
[source
->Index
][3] * (1.0F
/ CHAN_MAXF
);
269 case FRAG_ATTRIB_FOGC
:
271 src
[0] = span
->attrStepX
[FRAG_ATTRIB_FOGC
][0] * (1.0F
/ CHAN_MAXF
);
277 src
[0] = span
->attrStepY
[FRAG_ATTRIB_FOGC
][0] * (1.0F
/ CHAN_MAXF
);
284 assert(source
->Index
< FRAG_ATTRIB_MAX
);
285 /* texcoord or varying */
287 /* this is a little tricky - I think I've got it right */
288 const GLfloat invQ
= 1.0f
/ (span
->attrStart
[source
->Index
][3]
290 span
->attrStepX
[source
->Index
][3] *
292 src
[0] = span
->attrStepX
[source
->Index
][0] * invQ
;
293 src
[1] = span
->attrStepX
[source
->Index
][1] * invQ
;
294 src
[2] = span
->attrStepX
[source
->Index
][2] * invQ
;
295 src
[3] = span
->attrStepX
[source
->Index
][3] * invQ
;
298 /* Tricky, as above, but in Y direction */
299 const GLfloat invQ
= 1.0f
/ (span
->attrStart
[source
->Index
][3]
300 + span
->attrStepY
[source
->Index
][3]);
301 src
[0] = span
->attrStepY
[source
->Index
][0] * invQ
;
302 src
[1] = span
->attrStepY
[source
->Index
][1] * invQ
;
303 src
[2] = span
->attrStepY
[source
->Index
][2] * invQ
;
304 src
[3] = span
->attrStepY
[source
->Index
][3] * invQ
;
309 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
310 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
311 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
312 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
314 if (source
->NegateBase
) {
315 result
[0] = -result
[0];
316 result
[1] = -result
[1];
317 result
[2] = -result
[2];
318 result
[3] = -result
[3];
321 result
[0] = FABSF(result
[0]);
322 result
[1] = FABSF(result
[1]);
323 result
[2] = FABSF(result
[2]);
324 result
[3] = FABSF(result
[3]);
326 if (source
->NegateAbs
) {
327 result
[0] = -result
[0];
328 result
[1] = -result
[1];
329 result
[2] = -result
[2];
330 result
[3] = -result
[3];
338 * As above, but only return result[0] element.
341 fetch_vector1(const struct prog_src_register
*source
,
342 const struct gl_program_machine
*machine
, GLfloat result
[4])
344 const GLfloat
*src
= get_register_pointer(source
, machine
);
347 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
349 if (source
->NegateBase
) {
350 result
[0] = -result
[0];
353 result
[0] = FABSF(result
[0]);
355 if (source
->NegateAbs
) {
356 result
[0] = -result
[0];
362 * Test value against zero and return GT, LT, EQ or UN if NaN.
365 generate_cc(float value
)
368 return COND_UN
; /* NaN */
378 * Test if the ccMaskRule is satisfied by the given condition code.
379 * Used to mask destination writes according to the current condition code.
381 static INLINE GLboolean
382 test_cc(GLuint condCode
, GLuint ccMaskRule
)
384 switch (ccMaskRule
) {
385 case COND_EQ
: return (condCode
== COND_EQ
);
386 case COND_NE
: return (condCode
!= COND_EQ
);
387 case COND_LT
: return (condCode
== COND_LT
);
388 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
389 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
390 case COND_GT
: return (condCode
== COND_GT
);
391 case COND_TR
: return GL_TRUE
;
392 case COND_FL
: return GL_FALSE
;
393 default: return GL_TRUE
;
399 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
400 * or GL_FALSE to indicate result.
402 static INLINE GLboolean
403 eval_condition(const struct gl_program_machine
*machine
,
404 const struct prog_instruction
*inst
)
406 const GLuint swizzle
= inst
->DstReg
.CondSwizzle
;
407 const GLuint condMask
= inst
->DstReg
.CondMask
;
408 if (test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 0)], condMask
) ||
409 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 1)], condMask
) ||
410 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 2)], condMask
) ||
411 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 3)], condMask
)) {
422 * Store 4 floats into a register. Observe the instructions saturate and
423 * set-condition-code flags.
426 store_vector4(const struct prog_instruction
*inst
,
427 struct gl_program_machine
*machine
, const GLfloat value
[4])
429 const struct prog_dst_register
*dest
= &(inst
->DstReg
);
430 const GLboolean clamp
= inst
->SaturateMode
== SATURATE_ZERO_ONE
;
433 GLfloat clampedValue
[4];
434 GLuint writeMask
= dest
->WriteMask
;
436 switch (dest
->File
) {
438 ASSERT(dest
->Index
< MAX_PROGRAM_OUTPUTS
);
439 dstReg
= machine
->Outputs
[dest
->Index
];
441 case PROGRAM_TEMPORARY
:
442 ASSERT(dest
->Index
< MAX_PROGRAM_TEMPS
);
443 dstReg
= machine
->Temporaries
[dest
->Index
];
445 case PROGRAM_WRITE_ONLY
:
449 _mesa_problem(NULL
, "bad register file in store_vector4(fp)");
454 if (value
[0] > 1.0e10
||
455 IS_INF_OR_NAN(value
[0]) ||
456 IS_INF_OR_NAN(value
[1]) ||
457 IS_INF_OR_NAN(value
[2]) || IS_INF_OR_NAN(value
[3]))
458 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
462 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
463 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
464 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
465 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
466 value
= clampedValue
;
469 if (dest
->CondMask
!= COND_TR
) {
470 /* condition codes may turn off some writes */
471 if (writeMask
& WRITEMASK_X
) {
472 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 0)],
474 writeMask
&= ~WRITEMASK_X
;
476 if (writeMask
& WRITEMASK_Y
) {
477 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 1)],
479 writeMask
&= ~WRITEMASK_Y
;
481 if (writeMask
& WRITEMASK_Z
) {
482 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 2)],
484 writeMask
&= ~WRITEMASK_Z
;
486 if (writeMask
& WRITEMASK_W
) {
487 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 3)],
489 writeMask
&= ~WRITEMASK_W
;
493 if (writeMask
& WRITEMASK_X
)
494 dstReg
[0] = value
[0];
495 if (writeMask
& WRITEMASK_Y
)
496 dstReg
[1] = value
[1];
497 if (writeMask
& WRITEMASK_Z
)
498 dstReg
[2] = value
[2];
499 if (writeMask
& WRITEMASK_W
)
500 dstReg
[3] = value
[3];
502 if (inst
->CondUpdate
) {
503 if (writeMask
& WRITEMASK_X
)
504 machine
->CondCodes
[0] = generate_cc(value
[0]);
505 if (writeMask
& WRITEMASK_Y
)
506 machine
->CondCodes
[1] = generate_cc(value
[1]);
507 if (writeMask
& WRITEMASK_Z
)
508 machine
->CondCodes
[2] = generate_cc(value
[2]);
509 if (writeMask
& WRITEMASK_W
)
510 machine
->CondCodes
[3] = generate_cc(value
[3]);
512 printf("CondCodes=(%s,%s,%s,%s) for:\n",
513 _mesa_condcode_string(machine
->CondCodes
[0]),
514 _mesa_condcode_string(machine
->CondCodes
[1]),
515 _mesa_condcode_string(machine
->CondCodes
[2]),
516 _mesa_condcode_string(machine
->CondCodes
[3]));
524 * Initialize a new machine state instance from an existing one, adding
525 * the partial derivatives onto the input registers.
526 * Used to implement DDX and DDY instructions in non-trivial cases.
529 init_machine_deriv(GLcontext
* ctx
,
530 const struct gl_program_machine
*machine
,
531 const struct gl_fragment_program
*program
,
532 const SWspan
* span
, char xOrY
,
533 struct gl_program_machine
*dMachine
)
537 ASSERT(xOrY
== 'X' || xOrY
== 'Y');
539 /* copy existing machine */
540 _mesa_memcpy(dMachine
, machine
, sizeof(struct gl_program_machine
));
542 if (program
->Base
.Target
== GL_FRAGMENT_PROGRAM_NV
) {
543 /* XXX also need to do this when using valgrind */
544 /* Clear temporary registers (undefined for ARB_f_p) */
545 _mesa_bzero((void *) machine
->Temporaries
,
546 MAX_PROGRAM_TEMPS
* 4 * sizeof(GLfloat
));
549 /* Add derivatives */
550 if (program
->Base
.InputsRead
& FRAG_BIT_WPOS
) {
551 GLfloat
*wpos
= machine
->Attribs
[FRAG_ATTRIB_WPOS
][machine
->CurElement
];
555 wpos
[2] += span
->attrStepX
[FRAG_ATTRIB_WPOS
][2];
556 wpos
[3] += span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
561 wpos
[2] += span
->attrStepY
[FRAG_ATTRIB_WPOS
][2];
562 wpos
[3] += span
->attrStepY
[FRAG_ATTRIB_WPOS
][3];
566 /* primary, secondary colors */
567 for (attr
= FRAG_ATTRIB_COL0
; attr
<= FRAG_ATTRIB_COL1
; attr
++) {
568 if (program
->Base
.InputsRead
& (1 << attr
)) {
569 GLfloat
*col
= machine
->Attribs
[attr
][machine
->CurElement
];
571 col
[0] += span
->attrStepX
[attr
][0] * (1.0F
/ CHAN_MAXF
);
572 col
[1] += span
->attrStepX
[attr
][1] * (1.0F
/ CHAN_MAXF
);
573 col
[2] += span
->attrStepX
[attr
][2] * (1.0F
/ CHAN_MAXF
);
574 col
[3] += span
->attrStepX
[attr
][3] * (1.0F
/ CHAN_MAXF
);
577 col
[0] += span
->attrStepY
[attr
][0] * (1.0F
/ CHAN_MAXF
);
578 col
[1] += span
->attrStepY
[attr
][1] * (1.0F
/ CHAN_MAXF
);
579 col
[2] += span
->attrStepY
[attr
][2] * (1.0F
/ CHAN_MAXF
);
580 col
[3] += span
->attrStepY
[attr
][3] * (1.0F
/ CHAN_MAXF
);
584 if (program
->Base
.InputsRead
& FRAG_BIT_FOGC
) {
585 GLfloat
*fogc
= machine
->Attribs
[FRAG_ATTRIB_FOGC
][machine
->CurElement
];
587 fogc
[0] += span
->attrStepX
[FRAG_ATTRIB_FOGC
][0];
590 fogc
[0] += span
->attrStepY
[FRAG_ATTRIB_FOGC
][0];
593 /* texcoord and varying vars */
594 for (attr
= FRAG_ATTRIB_TEX0
; attr
< FRAG_ATTRIB_MAX
; attr
++) {
595 if (program
->Base
.InputsRead
& (1 << attr
)) {
596 GLfloat
*val
= machine
->Attribs
[attr
][machine
->CurElement
];
597 /* XXX perspective-correct interpolation */
599 val
[0] += span
->attrStepX
[attr
][0];
600 val
[1] += span
->attrStepX
[attr
][1];
601 val
[2] += span
->attrStepX
[attr
][2];
602 val
[3] += span
->attrStepX
[attr
][3];
605 val
[0] += span
->attrStepY
[attr
][0];
606 val
[1] += span
->attrStepY
[attr
][1];
607 val
[2] += span
->attrStepY
[attr
][2];
608 val
[3] += span
->attrStepY
[attr
][3];
613 /* init condition codes */
614 dMachine
->CondCodes
[0] = COND_EQ
;
615 dMachine
->CondCodes
[1] = COND_EQ
;
616 dMachine
->CondCodes
[2] = COND_EQ
;
617 dMachine
->CondCodes
[3] = COND_EQ
;
623 * Execute the given vertex/fragment program.
625 * \param ctx rendering context
626 * \param program the program to execute
627 * \param machine machine state (must be initialized)
628 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
631 _mesa_execute_program(GLcontext
* ctx
,
632 const struct gl_program
*program
,
633 struct gl_program_machine
*machine
)
635 const GLuint numInst
= program
->NumInstructions
;
636 const GLuint maxExec
= 10000;
637 GLint pc
, numExec
= 0;
639 machine
->CurProgram
= program
;
642 printf("execute program %u --------------------\n", program
->Id
);
645 #if FEATURE_MESA_program_debug
646 CurrentMachine
= machine
;
649 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
650 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
653 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
656 for (pc
= 0; pc
< numInst
; pc
++) {
657 const struct prog_instruction
*inst
= program
->Instructions
+ pc
;
659 #if FEATURE_MESA_program_debug
660 if (ctx
->FragmentProgram
.CallbackEnabled
&&
661 ctx
->FragmentProgram
.Callback
) {
662 ctx
->FragmentProgram
.CurrentPosition
= inst
->StringPos
;
663 ctx
->FragmentProgram
.Callback(program
->Target
,
664 ctx
->FragmentProgram
.CallbackData
);
669 _mesa_print_instruction(inst
);
672 switch (inst
->Opcode
) {
675 GLfloat a
[4], result
[4];
676 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
677 result
[0] = FABSF(a
[0]);
678 result
[1] = FABSF(a
[1]);
679 result
[2] = FABSF(a
[2]);
680 result
[3] = FABSF(a
[3]);
681 store_vector4(inst
, machine
, result
);
686 GLfloat a
[4], b
[4], result
[4];
687 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
688 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
689 result
[0] = a
[0] + b
[0];
690 result
[1] = a
[1] + b
[1];
691 result
[2] = a
[2] + b
[2];
692 result
[3] = a
[3] + b
[3];
693 store_vector4(inst
, machine
, result
);
695 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
696 result
[0], result
[1], result
[2], result
[3],
697 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
704 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
705 machine
->AddressReg
[0][0] = (GLint
) FLOORF(t
[0]);
712 /* subtract 1 here since pc is incremented by for(pc) loop */
713 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
715 case OPCODE_BGNSUB
: /* begin subroutine */
717 case OPCODE_ENDSUB
: /* end subroutine */
719 case OPCODE_BRA
: /* branch (conditional) */
721 case OPCODE_BRK
: /* break out of loop (conditional) */
723 case OPCODE_CONT
: /* continue loop (conditional) */
724 if (eval_condition(machine
, inst
)) {
726 /* Subtract 1 here since we'll do pc++ at end of for-loop */
727 pc
= inst
->BranchTarget
- 1;
730 case OPCODE_BRK0
: /* Break if zero */
732 case OPCODE_CONT0
: /* Continue if zero */
735 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
738 /* Subtract 1 here since we'll do pc++ at end of for-loop */
739 pc
= inst
->BranchTarget
- 1;
743 case OPCODE_BRK1
: /* Break if non-zero */
745 case OPCODE_CONT1
: /* Continue if non-zero */
748 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
751 /* Subtract 1 here since we'll do pc++ at end of for-loop */
752 pc
= inst
->BranchTarget
- 1;
756 case OPCODE_CAL
: /* Call subroutine (conditional) */
757 if (eval_condition(machine
, inst
)) {
758 /* call the subroutine */
759 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
760 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
762 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1; /* next inst */
763 /* Subtract 1 here since we'll do pc++ at end of for-loop */
764 pc
= inst
->BranchTarget
- 1;
769 GLfloat a
[4], b
[4], c
[4], result
[4];
770 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
771 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
772 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
773 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
774 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
775 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
776 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
777 store_vector4(inst
, machine
, result
);
782 GLfloat a
[4], result
[4];
783 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
784 result
[0] = result
[1] = result
[2] = result
[3]
785 = (GLfloat
) _mesa_cos(a
[0]);
786 store_vector4(inst
, machine
, result
);
789 case OPCODE_DDX
: /* Partial derivative with respect to X */
792 GLfloat a
[4], aNext
[4], result
[4];
793 struct gl_program_machine dMachine
;
794 if (!fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], span
, 'X',
796 /* This is tricky. Make a copy of the current machine state,
797 * increment the input registers by the dx or dy partial
798 * derivatives, then re-execute the program up to the
799 * preceeding instruction, then fetch the source register.
800 * Finally, find the difference in the register values for
801 * the original and derivative runs.
803 fetch_vector4(&inst
->SrcReg
[0], machine
, program
, a
);
804 init_machine_deriv(ctx
, machine
, program
, span
,
806 execute_program(ctx
, program
, pc
, &dMachine
, span
, column
);
807 fetch_vector4(&inst
->SrcReg
[0], &dMachine
, program
,
809 result
[0] = aNext
[0] - a
[0];
810 result
[1] = aNext
[1] - a
[1];
811 result
[2] = aNext
[2] - a
[2];
812 result
[3] = aNext
[3] - a
[3];
814 store_vector4(inst
, machine
, result
);
816 store_vector4(inst
, machine
, ZeroVec
);
820 case OPCODE_DDY
: /* Partial derivative with respect to Y */
823 GLfloat a
[4], aNext
[4], result
[4];
824 struct gl_program_machine dMachine
;
825 if (!fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], span
, 'Y',
827 init_machine_deriv(ctx
, machine
, program
, span
,
829 fetch_vector4(&inst
->SrcReg
[0], machine
, program
, a
);
830 execute_program(ctx
, program
, pc
, &dMachine
, span
, column
);
831 fetch_vector4(&inst
->SrcReg
[0], &dMachine
, program
,
833 result
[0] = aNext
[0] - a
[0];
834 result
[1] = aNext
[1] - a
[1];
835 result
[2] = aNext
[2] - a
[2];
836 result
[3] = aNext
[3] - a
[3];
838 store_vector4(inst
, machine
, result
);
840 store_vector4(inst
, machine
, ZeroVec
);
846 GLfloat a
[4], b
[4], result
[4];
847 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
848 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
849 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
850 store_vector4(inst
, machine
, result
);
852 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
853 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
859 GLfloat a
[4], b
[4], result
[4];
860 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
861 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
862 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
, b
);
863 store_vector4(inst
, machine
, result
);
865 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
866 result
[0], a
[0], a
[1], a
[2], a
[3],
867 b
[0], b
[1], b
[2], b
[3]);
873 GLfloat a
[4], b
[4], result
[4];
874 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
875 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
876 result
[0] = result
[1] = result
[2] = result
[3] =
877 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + b
[3];
878 store_vector4(inst
, machine
, result
);
881 case OPCODE_DST
: /* Distance vector */
883 GLfloat a
[4], b
[4], result
[4];
884 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
885 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
887 result
[1] = a
[1] * b
[1];
890 store_vector4(inst
, machine
, result
);
895 GLfloat t
[4], q
[4], floor_t0
;
896 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
897 floor_t0
= FLOORF(t
[0]);
898 if (floor_t0
> FLT_MAX_EXP
) {
899 SET_POS_INFINITY(q
[0]);
900 SET_POS_INFINITY(q
[2]);
902 else if (floor_t0
< FLT_MIN_EXP
) {
907 q
[0] = LDEXPF(1.0, (int) floor_t0
);
908 /* Note: GL_NV_vertex_program expects
909 * result.z = result.x * APPX(result.y)
910 * We do what the ARB extension says.
912 q
[2] = pow(2.0, t
[0]);
914 q
[1] = t
[0] - floor_t0
;
916 store_vector4( inst
, machine
, q
);
919 case OPCODE_EX2
: /* Exponential base 2 */
921 GLfloat a
[4], result
[4];
922 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
923 result
[0] = result
[1] = result
[2] = result
[3] =
924 (GLfloat
) _mesa_pow(2.0, a
[0]);
925 store_vector4(inst
, machine
, result
);
930 GLfloat a
[4], result
[4];
931 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
932 result
[0] = FLOORF(a
[0]);
933 result
[1] = FLOORF(a
[1]);
934 result
[2] = FLOORF(a
[2]);
935 result
[3] = FLOORF(a
[3]);
936 store_vector4(inst
, machine
, result
);
941 GLfloat a
[4], result
[4];
942 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
943 result
[0] = a
[0] - FLOORF(a
[0]);
944 result
[1] = a
[1] - FLOORF(a
[1]);
945 result
[2] = a
[2] - FLOORF(a
[2]);
946 result
[3] = a
[3] - FLOORF(a
[3]);
947 store_vector4(inst
, machine
, result
);
954 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
956 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
957 cond
= (a
[0] != 0.0);
960 cond
= eval_condition(machine
, inst
);
964 /* do if-clause (just continue execution) */
967 /* go to the instruction after ELSE or ENDIF */
968 assert(inst
->BranchTarget
>= 0);
969 pc
= inst
->BranchTarget
- 1;
975 assert(inst
->BranchTarget
>= 0);
976 pc
= inst
->BranchTarget
- 1;
981 case OPCODE_INT
: /* float to int */
983 GLfloat a
[4], result
[4];
984 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
985 result
[0] = (GLfloat
) (GLint
) a
[0];
986 result
[1] = (GLfloat
) (GLint
) a
[1];
987 result
[2] = (GLfloat
) (GLint
) a
[2];
988 result
[3] = (GLfloat
) (GLint
) a
[3];
989 store_vector4(inst
, machine
, result
);
992 case OPCODE_KIL_NV
: /* NV_f_p only (conditional) */
993 if (eval_condition(machine
, inst
)) {
997 case OPCODE_KIL
: /* ARB_f_p only */
1000 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1001 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
1006 case OPCODE_LG2
: /* log base 2 */
1008 GLfloat a
[4], result
[4];
1009 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1010 result
[0] = result
[1] = result
[2] = result
[3] = LOG2(a
[0]);
1011 store_vector4(inst
, machine
, result
);
1016 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
1017 GLfloat a
[4], result
[4];
1018 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1019 a
[0] = MAX2(a
[0], 0.0F
);
1020 a
[1] = MAX2(a
[1], 0.0F
);
1021 /* XXX ARB version clamps a[3], NV version doesn't */
1022 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
1025 /* XXX we could probably just use pow() here */
1027 if (a
[1] == 0.0 && a
[3] == 0.0)
1030 result
[2] = EXPF(a
[3] * LOGF(a
[1]));
1036 store_vector4(inst
, machine
, result
);
1038 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
1039 result
[0], result
[1], result
[2], result
[3],
1040 a
[0], a
[1], a
[2], a
[3]);
1046 GLfloat t
[4], q
[4], abs_t0
;
1047 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
1048 abs_t0
= FABSF(t
[0]);
1049 if (abs_t0
!= 0.0F
) {
1050 /* Since we really can't handle infinite values on VMS
1051 * like other OSes we'll use __MAXFLOAT to represent
1052 * infinity. This may need some tweaking.
1055 if (abs_t0
== __MAXFLOAT
)
1057 if (IS_INF_OR_NAN(abs_t0
))
1060 SET_POS_INFINITY(q
[0]);
1062 SET_POS_INFINITY(q
[2]);
1066 GLfloat mantissa
= FREXPF(t
[0], &exponent
);
1067 q
[0] = (GLfloat
) (exponent
- 1);
1068 q
[1] = (GLfloat
) (2.0 * mantissa
); /* map [.5, 1) -> [1, 2) */
1069 q
[2] = (GLfloat
) (q
[0] + LOG2(q
[1]));
1073 SET_NEG_INFINITY(q
[0]);
1075 SET_NEG_INFINITY(q
[2]);
1078 store_vector4(inst
, machine
, q
);
1083 GLfloat a
[4], b
[4], c
[4], result
[4];
1084 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1085 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1086 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1087 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
1088 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
1089 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
1090 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
1091 store_vector4(inst
, machine
, result
);
1093 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
1094 "(%g %g %g %g), (%g %g %g %g)\n",
1095 result
[0], result
[1], result
[2], result
[3],
1096 a
[0], a
[1], a
[2], a
[3],
1097 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1103 GLfloat a
[4], b
[4], c
[4], result
[4];
1104 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1105 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1106 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1107 result
[0] = a
[0] * b
[0] + c
[0];
1108 result
[1] = a
[1] * b
[1] + c
[1];
1109 result
[2] = a
[2] * b
[2] + c
[2];
1110 result
[3] = a
[3] * b
[3] + c
[3];
1111 store_vector4(inst
, machine
, result
);
1113 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
1114 "(%g %g %g %g) + (%g %g %g %g)\n",
1115 result
[0], result
[1], result
[2], result
[3],
1116 a
[0], a
[1], a
[2], a
[3],
1117 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1123 GLfloat a
[4], b
[4], result
[4];
1124 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1125 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1126 result
[0] = MAX2(a
[0], b
[0]);
1127 result
[1] = MAX2(a
[1], b
[1]);
1128 result
[2] = MAX2(a
[2], b
[2]);
1129 result
[3] = MAX2(a
[3], b
[3]);
1130 store_vector4(inst
, machine
, result
);
1132 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
1133 result
[0], result
[1], result
[2], result
[3],
1134 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1140 GLfloat a
[4], b
[4], result
[4];
1141 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1142 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1143 result
[0] = MIN2(a
[0], b
[0]);
1144 result
[1] = MIN2(a
[1], b
[1]);
1145 result
[2] = MIN2(a
[2], b
[2]);
1146 result
[3] = MIN2(a
[3], b
[3]);
1147 store_vector4(inst
, machine
, result
);
1153 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
1154 store_vector4(inst
, machine
, result
);
1156 printf("MOV (%g %g %g %g)\n",
1157 result
[0], result
[1], result
[2], result
[3]);
1163 GLfloat a
[4], b
[4], result
[4];
1164 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1165 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1166 result
[0] = a
[0] * b
[0];
1167 result
[1] = a
[1] * b
[1];
1168 result
[2] = a
[2] * b
[2];
1169 result
[3] = a
[3] * b
[3];
1170 store_vector4(inst
, machine
, result
);
1172 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1173 result
[0], result
[1], result
[2], result
[3],
1174 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1180 GLfloat a
[4], result
[4];
1181 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1184 result
[2] = result
[3] = _slang_library_noise1(a
[0]);
1185 store_vector4(inst
, machine
, result
);
1190 GLfloat a
[4], result
[4];
1191 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1194 result
[2] = result
[3] = _slang_library_noise2(a
[0], a
[1]);
1195 store_vector4(inst
, machine
, result
);
1200 GLfloat a
[4], result
[4];
1201 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1205 result
[3] = _slang_library_noise3(a
[0], a
[1], a
[2]);
1206 store_vector4(inst
, machine
, result
);
1211 GLfloat a
[4], result
[4];
1212 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1216 result
[3] = _slang_library_noise4(a
[0], a
[1], a
[2], a
[3]);
1217 store_vector4(inst
, machine
, result
);
1222 case OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
1224 GLfloat a
[4], result
[4];
1226 GLuint
*rawResult
= (GLuint
*) result
;
1228 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1229 hx
= _mesa_float_to_half(a
[0]);
1230 hy
= _mesa_float_to_half(a
[1]);
1231 twoHalves
= hx
| (hy
<< 16);
1232 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1234 store_vector4(inst
, machine
, result
);
1237 case OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
1239 GLfloat a
[4], result
[4];
1240 GLuint usx
, usy
, *rawResult
= (GLuint
*) result
;
1241 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1242 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1243 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1244 usx
= IROUND(a
[0] * 65535.0F
);
1245 usy
= IROUND(a
[1] * 65535.0F
);
1246 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1247 = usx
| (usy
<< 16);
1248 store_vector4(inst
, machine
, result
);
1251 case OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
1253 GLfloat a
[4], result
[4];
1254 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
1255 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1256 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
1257 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
1258 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
1259 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
1260 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
1261 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
1262 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
1263 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
1264 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1265 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1266 store_vector4(inst
, machine
, result
);
1269 case OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
1271 GLfloat a
[4], result
[4];
1272 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
1273 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1274 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1275 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1276 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
1277 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
1278 ubx
= IROUND(255.0F
* a
[0]);
1279 uby
= IROUND(255.0F
* a
[1]);
1280 ubz
= IROUND(255.0F
* a
[2]);
1281 ubw
= IROUND(255.0F
* a
[3]);
1282 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1283 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1284 store_vector4(inst
, machine
, result
);
1289 GLfloat a
[4], b
[4], result
[4];
1290 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1291 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
1292 result
[0] = result
[1] = result
[2] = result
[3]
1293 = (GLfloat
) _mesa_pow(a
[0], b
[0]);
1294 store_vector4(inst
, machine
, result
);
1299 GLfloat a
[4], result
[4];
1300 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1304 else if (IS_INF_OR_NAN(a
[0]))
1305 printf("RCP(inf)\n");
1307 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
1308 store_vector4(inst
, machine
, result
);
1311 case OPCODE_RET
: /* return from subroutine (conditional) */
1312 if (eval_condition(machine
, inst
)) {
1313 if (machine
->StackDepth
== 0) {
1314 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
1316 /* subtract one because of pc++ in the for loop */
1317 pc
= machine
->CallStack
[--machine
->StackDepth
] - 1;
1320 case OPCODE_RFL
: /* reflection vector */
1322 GLfloat axis
[4], dir
[4], result
[4], tmpX
, tmpW
;
1323 fetch_vector4(&inst
->SrcReg
[0], machine
, axis
);
1324 fetch_vector4(&inst
->SrcReg
[1], machine
, dir
);
1325 tmpW
= DOT3(axis
, axis
);
1326 tmpX
= (2.0F
* DOT3(axis
, dir
)) / tmpW
;
1327 result
[0] = tmpX
* axis
[0] - dir
[0];
1328 result
[1] = tmpX
* axis
[1] - dir
[1];
1329 result
[2] = tmpX
* axis
[2] - dir
[2];
1330 /* result[3] is never written! XXX enforce in parser! */
1331 store_vector4(inst
, machine
, result
);
1334 case OPCODE_RSQ
: /* 1 / sqrt() */
1336 GLfloat a
[4], result
[4];
1337 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1339 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1340 store_vector4(inst
, machine
, result
);
1342 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
1346 case OPCODE_SCS
: /* sine and cos */
1348 GLfloat a
[4], result
[4];
1349 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1350 result
[0] = (GLfloat
) _mesa_cos(a
[0]);
1351 result
[1] = (GLfloat
) _mesa_sin(a
[0]);
1352 result
[2] = 0.0; /* undefined! */
1353 result
[3] = 0.0; /* undefined! */
1354 store_vector4(inst
, machine
, result
);
1357 case OPCODE_SEQ
: /* set on equal */
1359 GLfloat a
[4], b
[4], result
[4];
1360 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1361 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1362 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1363 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1364 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1365 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1366 store_vector4(inst
, machine
, result
);
1368 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1369 result
[0], result
[1], result
[2], result
[3],
1370 a
[0], a
[1], a
[2], a
[3],
1371 b
[0], b
[1], b
[2], b
[3]);
1375 case OPCODE_SFL
: /* set false, operands ignored */
1377 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1378 store_vector4(inst
, machine
, result
);
1381 case OPCODE_SGE
: /* set on greater or equal */
1383 GLfloat a
[4], b
[4], result
[4];
1384 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1385 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1386 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1387 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1388 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1389 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1390 store_vector4(inst
, machine
, result
);
1392 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1393 result
[0], result
[1], result
[2], result
[3],
1394 a
[0], a
[1], a
[2], a
[3],
1395 b
[0], b
[1], b
[2], b
[3]);
1399 case OPCODE_SGT
: /* set on greater */
1401 GLfloat a
[4], b
[4], result
[4];
1402 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1403 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1404 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1405 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1406 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1407 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1408 store_vector4(inst
, machine
, result
);
1410 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1411 result
[0], result
[1], result
[2], result
[3],
1412 a
[0], a
[1], a
[2], a
[3],
1413 b
[0], b
[1], b
[2], b
[3]);
1419 GLfloat a
[4], result
[4];
1420 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1421 result
[0] = result
[1] = result
[2] = result
[3]
1422 = (GLfloat
) _mesa_sin(a
[0]);
1423 store_vector4(inst
, machine
, result
);
1426 case OPCODE_SLE
: /* set on less or equal */
1428 GLfloat a
[4], b
[4], result
[4];
1429 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1430 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1431 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1432 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1433 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1434 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1435 store_vector4(inst
, machine
, result
);
1437 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1438 result
[0], result
[1], result
[2], result
[3],
1439 a
[0], a
[1], a
[2], a
[3],
1440 b
[0], b
[1], b
[2], b
[3]);
1444 case OPCODE_SLT
: /* set on less */
1446 GLfloat a
[4], b
[4], result
[4];
1447 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1448 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1449 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1450 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1451 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1452 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1453 store_vector4(inst
, machine
, result
);
1455 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1456 result
[0], result
[1], result
[2], result
[3],
1457 a
[0], a
[1], a
[2], a
[3],
1458 b
[0], b
[1], b
[2], b
[3]);
1462 case OPCODE_SNE
: /* set on not equal */
1464 GLfloat a
[4], b
[4], result
[4];
1465 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1466 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1467 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1468 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1469 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1470 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1471 store_vector4(inst
, machine
, result
);
1473 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1474 result
[0], result
[1], result
[2], result
[3],
1475 a
[0], a
[1], a
[2], a
[3],
1476 b
[0], b
[1], b
[2], b
[3]);
1480 case OPCODE_STR
: /* set true, operands ignored */
1482 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1483 store_vector4(inst
, machine
, result
);
1488 GLfloat a
[4], b
[4], result
[4];
1489 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1490 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1491 result
[0] = a
[0] - b
[0];
1492 result
[1] = a
[1] - b
[1];
1493 result
[2] = a
[2] - b
[2];
1494 result
[3] = a
[3] - b
[3];
1495 store_vector4(inst
, machine
, result
);
1497 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1498 result
[0], result
[1], result
[2], result
[3],
1499 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1503 case OPCODE_SWZ
: /* extended swizzle */
1505 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1506 const GLfloat
*src
= get_register_pointer(source
, machine
);
1509 for (i
= 0; i
< 4; i
++) {
1510 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1511 if (swz
== SWIZZLE_ZERO
)
1513 else if (swz
== SWIZZLE_ONE
)
1518 result
[i
] = src
[swz
];
1520 if (source
->NegateBase
& (1 << i
))
1521 result
[i
] = -result
[i
];
1523 store_vector4(inst
, machine
, result
);
1526 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1529 /* Note: only use the precomputed lambda value when we're
1530 * sampling texture unit [K] with texcoord[K].
1531 * Otherwise, the lambda value may have no relation to the
1532 * instruction's texcoord or texture image. Using the wrong
1533 * lambda is usually bad news.
1534 * The rest of the time, just use zero (until we get a more
1535 * sophisticated way of computing lambda).
1537 GLfloat coord
[4], color
[4], lambda
;
1539 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
1540 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
)
1541 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
];
1545 fetch_vector4(&inst
->SrcReg
[0], machine
, coord
);
1546 machine
->FetchTexelLod(ctx
, coord
, lambda
, inst
->TexSrcUnit
,
1549 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g], "
1551 color
[0], color
[1], color
[2], color
[3],
1553 coord
[0], coord
[1], coord
[2], coord
[3], lambda
);
1555 store_vector4(inst
, machine
, color
);
1558 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1559 /* Texel lookup with LOD bias */
1561 const struct gl_texture_unit
*texUnit
1562 = &ctx
->Texture
.Unit
[inst
->TexSrcUnit
];
1563 GLfloat coord
[4], color
[4], lambda
, bias
;
1565 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
1566 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
)
1567 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
];
1571 fetch_vector4(&inst
->SrcReg
[0], machine
, coord
);
1572 /* coord[3] is the bias to add to lambda */
1573 bias
= texUnit
->LodBias
+ coord
[3];
1574 if (texUnit
->_Current
)
1575 bias
+= texUnit
->_Current
->LodBias
;
1576 machine
->FetchTexelLod(ctx
, coord
, lambda
+ bias
,
1577 inst
->TexSrcUnit
, color
);
1578 store_vector4(inst
, machine
, color
);
1581 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1582 /* Texture lookup w/ partial derivatives for LOD */
1584 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1585 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1586 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1587 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1588 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1589 inst
->TexSrcUnit
, color
);
1590 store_vector4(inst
, machine
, color
);
1593 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1594 /* Texture lookup w/ projective divide */
1596 GLfloat texcoord
[4], color
[4], lambda
;
1598 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
1599 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
)
1600 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
];
1604 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1605 /* Not so sure about this test - if texcoord[3] is
1606 * zero, we'd probably be fine except for an ASSERT in
1607 * IROUND_POS() which gets triggered by the inf values created.
1609 if (texcoord
[3] != 0.0) {
1610 texcoord
[0] /= texcoord
[3];
1611 texcoord
[1] /= texcoord
[3];
1612 texcoord
[2] /= texcoord
[3];
1614 machine
->FetchTexelLod(ctx
, texcoord
, lambda
,
1615 inst
->TexSrcUnit
, color
);
1616 store_vector4(inst
, machine
, color
);
1619 case OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1620 /* Texture lookup w/ projective divide */
1622 GLfloat texcoord
[4], color
[4], lambda
;
1624 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
1625 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
)
1626 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
];
1630 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1631 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1632 texcoord
[3] != 0.0) {
1633 texcoord
[0] /= texcoord
[3];
1634 texcoord
[1] /= texcoord
[3];
1635 texcoord
[2] /= texcoord
[3];
1637 machine
->FetchTexelLod(ctx
, texcoord
, lambda
,
1638 inst
->TexSrcUnit
, color
);
1639 store_vector4(inst
, machine
, color
);
1642 case OPCODE_UP2H
: /* unpack two 16-bit floats */
1644 GLfloat a
[4], result
[4];
1645 const GLuint
*rawBits
= (const GLuint
*) a
;
1647 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1648 hx
= rawBits
[0] & 0xffff;
1649 hy
= rawBits
[0] >> 16;
1650 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1651 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1652 store_vector4(inst
, machine
, result
);
1655 case OPCODE_UP2US
: /* unpack two GLushorts */
1657 GLfloat a
[4], result
[4];
1658 const GLuint
*rawBits
= (const GLuint
*) a
;
1660 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1661 usx
= rawBits
[0] & 0xffff;
1662 usy
= rawBits
[0] >> 16;
1663 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1664 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1665 store_vector4(inst
, machine
, result
);
1668 case OPCODE_UP4B
: /* unpack four GLbytes */
1670 GLfloat a
[4], result
[4];
1671 const GLuint
*rawBits
= (const GLuint
*) a
;
1672 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1673 result
[0] = (((rawBits
[0] >> 0) & 0xff) - 128) / 127.0F
;
1674 result
[1] = (((rawBits
[0] >> 8) & 0xff) - 128) / 127.0F
;
1675 result
[2] = (((rawBits
[0] >> 16) & 0xff) - 128) / 127.0F
;
1676 result
[3] = (((rawBits
[0] >> 24) & 0xff) - 128) / 127.0F
;
1677 store_vector4(inst
, machine
, result
);
1680 case OPCODE_UP4UB
: /* unpack four GLubytes */
1682 GLfloat a
[4], result
[4];
1683 const GLuint
*rawBits
= (const GLuint
*) a
;
1684 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1685 result
[0] = ((rawBits
[0] >> 0) & 0xff) / 255.0F
;
1686 result
[1] = ((rawBits
[0] >> 8) & 0xff) / 255.0F
;
1687 result
[2] = ((rawBits
[0] >> 16) & 0xff) / 255.0F
;
1688 result
[3] = ((rawBits
[0] >> 24) & 0xff) / 255.0F
;
1689 store_vector4(inst
, machine
, result
);
1692 case OPCODE_XPD
: /* cross product */
1694 GLfloat a
[4], b
[4], result
[4];
1695 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1696 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1697 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1698 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1699 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1701 store_vector4(inst
, machine
, result
);
1703 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1704 result
[0], result
[1], result
[2], result
[3],
1705 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1709 case OPCODE_X2D
: /* 2-D matrix transform */
1711 GLfloat a
[4], b
[4], c
[4], result
[4];
1712 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1713 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1714 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1715 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1716 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1717 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1718 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1719 store_vector4(inst
, machine
, result
);
1724 if (inst
->SrcReg
[0].File
!= -1) {
1726 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1727 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst
->Data
,
1728 a
[0], a
[1], a
[2], a
[3]);
1731 _mesa_printf("%s\n", (const char *) inst
->Data
);
1738 _mesa_problem(ctx
, "Bad opcode %d in _mesa_exec_fragment_program",
1740 return GL_TRUE
; /* return value doesn't matter */
1745 if (numExec
> maxExec
) {
1746 _mesa_problem(ctx
, "Infinite loop detected in fragment program");
1752 #if FEATURE_MESA_program_debug
1753 CurrentMachine
= NULL
;