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]);
517 * Initialize a new machine state instance from an existing one, adding
518 * the partial derivatives onto the input registers.
519 * Used to implement DDX and DDY instructions in non-trivial cases.
522 init_machine_deriv(GLcontext
* ctx
,
523 const struct gl_program_machine
*machine
,
524 const struct gl_fragment_program
*program
,
525 const SWspan
* span
, char xOrY
,
526 struct gl_program_machine
*dMachine
)
530 ASSERT(xOrY
== 'X' || xOrY
== 'Y');
532 /* copy existing machine */
533 _mesa_memcpy(dMachine
, machine
, sizeof(struct gl_program_machine
));
535 if (program
->Base
.Target
== GL_FRAGMENT_PROGRAM_NV
) {
536 /* XXX also need to do this when using valgrind */
537 /* Clear temporary registers (undefined for ARB_f_p) */
538 _mesa_bzero((void *) machine
->Temporaries
,
539 MAX_PROGRAM_TEMPS
* 4 * sizeof(GLfloat
));
542 /* Add derivatives */
543 if (program
->Base
.InputsRead
& FRAG_BIT_WPOS
) {
544 GLfloat
*wpos
= machine
->Attribs
[FRAG_ATTRIB_WPOS
][machine
->CurElement
];
548 wpos
[2] += span
->attrStepX
[FRAG_ATTRIB_WPOS
][2];
549 wpos
[3] += span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
554 wpos
[2] += span
->attrStepY
[FRAG_ATTRIB_WPOS
][2];
555 wpos
[3] += span
->attrStepY
[FRAG_ATTRIB_WPOS
][3];
559 /* primary, secondary colors */
560 for (attr
= FRAG_ATTRIB_COL0
; attr
<= FRAG_ATTRIB_COL1
; attr
++) {
561 if (program
->Base
.InputsRead
& (1 << attr
)) {
562 GLfloat
*col
= machine
->Attribs
[attr
][machine
->CurElement
];
564 col
[0] += span
->attrStepX
[attr
][0] * (1.0F
/ CHAN_MAXF
);
565 col
[1] += span
->attrStepX
[attr
][1] * (1.0F
/ CHAN_MAXF
);
566 col
[2] += span
->attrStepX
[attr
][2] * (1.0F
/ CHAN_MAXF
);
567 col
[3] += span
->attrStepX
[attr
][3] * (1.0F
/ CHAN_MAXF
);
570 col
[0] += span
->attrStepY
[attr
][0] * (1.0F
/ CHAN_MAXF
);
571 col
[1] += span
->attrStepY
[attr
][1] * (1.0F
/ CHAN_MAXF
);
572 col
[2] += span
->attrStepY
[attr
][2] * (1.0F
/ CHAN_MAXF
);
573 col
[3] += span
->attrStepY
[attr
][3] * (1.0F
/ CHAN_MAXF
);
577 if (program
->Base
.InputsRead
& FRAG_BIT_FOGC
) {
578 GLfloat
*fogc
= machine
->Attribs
[FRAG_ATTRIB_FOGC
][machine
->CurElement
];
580 fogc
[0] += span
->attrStepX
[FRAG_ATTRIB_FOGC
][0];
583 fogc
[0] += span
->attrStepY
[FRAG_ATTRIB_FOGC
][0];
586 /* texcoord and varying vars */
587 for (attr
= FRAG_ATTRIB_TEX0
; attr
< FRAG_ATTRIB_MAX
; attr
++) {
588 if (program
->Base
.InputsRead
& (1 << attr
)) {
589 GLfloat
*val
= machine
->Attribs
[attr
][machine
->CurElement
];
590 /* XXX perspective-correct interpolation */
592 val
[0] += span
->attrStepX
[attr
][0];
593 val
[1] += span
->attrStepX
[attr
][1];
594 val
[2] += span
->attrStepX
[attr
][2];
595 val
[3] += span
->attrStepX
[attr
][3];
598 val
[0] += span
->attrStepY
[attr
][0];
599 val
[1] += span
->attrStepY
[attr
][1];
600 val
[2] += span
->attrStepY
[attr
][2];
601 val
[3] += span
->attrStepY
[attr
][3];
606 /* init condition codes */
607 dMachine
->CondCodes
[0] = COND_EQ
;
608 dMachine
->CondCodes
[1] = COND_EQ
;
609 dMachine
->CondCodes
[2] = COND_EQ
;
610 dMachine
->CondCodes
[3] = COND_EQ
;
616 * Execute the given vertex/fragment program.
618 * \param ctx rendering context
619 * \param program the program to execute
620 * \param machine machine state (must be initialized)
621 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
624 _mesa_execute_program(GLcontext
* ctx
,
625 const struct gl_program
*program
,
626 struct gl_program_machine
*machine
)
628 const GLuint numInst
= program
->NumInstructions
;
629 const GLuint maxExec
= 10000;
630 GLint pc
, numExec
= 0;
632 machine
->CurProgram
= program
;
635 printf("execute program %u --------------------\n", program
->Id
);
638 #if FEATURE_MESA_program_debug
639 CurrentMachine
= machine
;
642 if (program
->Target
== GL_VERTEX_PROGRAM_ARB
) {
643 machine
->EnvParams
= ctx
->VertexProgram
.Parameters
;
646 machine
->EnvParams
= ctx
->FragmentProgram
.Parameters
;
649 for (pc
= 0; pc
< numInst
; pc
++) {
650 const struct prog_instruction
*inst
= program
->Instructions
+ pc
;
652 #if FEATURE_MESA_program_debug
653 if (ctx
->FragmentProgram
.CallbackEnabled
&&
654 ctx
->FragmentProgram
.Callback
) {
655 ctx
->FragmentProgram
.CurrentPosition
= inst
->StringPos
;
656 ctx
->FragmentProgram
.Callback(program
->Target
,
657 ctx
->FragmentProgram
.CallbackData
);
662 _mesa_print_instruction(inst
);
665 switch (inst
->Opcode
) {
668 GLfloat a
[4], result
[4];
669 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
670 result
[0] = FABSF(a
[0]);
671 result
[1] = FABSF(a
[1]);
672 result
[2] = FABSF(a
[2]);
673 result
[3] = FABSF(a
[3]);
674 store_vector4(inst
, machine
, result
);
679 GLfloat a
[4], b
[4], result
[4];
680 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
681 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
682 result
[0] = a
[0] + b
[0];
683 result
[1] = a
[1] + b
[1];
684 result
[2] = a
[2] + b
[2];
685 result
[3] = a
[3] + b
[3];
686 store_vector4(inst
, machine
, result
);
688 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
689 result
[0], result
[1], result
[2], result
[3],
690 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
697 fetch_vector4(&inst
->SrcReg
[0], machine
, t
);
698 machine
->AddressReg
[0][0] = (GLint
) FLOORF(t
[0]);
705 /* subtract 1 here since pc is incremented by for(pc) loop */
706 pc
= inst
->BranchTarget
- 1; /* go to matching BNGLOOP */
708 case OPCODE_BGNSUB
: /* begin subroutine */
710 case OPCODE_ENDSUB
: /* end subroutine */
712 case OPCODE_BRA
: /* branch (conditional) */
714 case OPCODE_BRK
: /* break out of loop (conditional) */
716 case OPCODE_CONT
: /* continue loop (conditional) */
717 if (eval_condition(machine
, inst
)) {
719 /* Subtract 1 here since we'll do pc++ at end of for-loop */
720 pc
= inst
->BranchTarget
- 1;
723 case OPCODE_CAL
: /* Call subroutine (conditional) */
724 if (eval_condition(machine
, inst
)) {
725 /* call the subroutine */
726 if (machine
->StackDepth
>= MAX_PROGRAM_CALL_DEPTH
) {
727 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
729 machine
->CallStack
[machine
->StackDepth
++] = pc
+ 1;
730 pc
= inst
->BranchTarget
; /* XXX - 1 ??? */
735 GLfloat a
[4], b
[4], c
[4], result
[4];
736 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
737 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
738 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
739 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
740 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
741 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
742 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
743 store_vector4(inst
, machine
, result
);
748 GLfloat a
[4], result
[4];
749 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
750 result
[0] = result
[1] = result
[2] = result
[3]
751 = (GLfloat
) _mesa_cos(a
[0]);
752 store_vector4(inst
, machine
, result
);
755 case OPCODE_DDX
: /* Partial derivative with respect to X */
758 GLfloat a
[4], aNext
[4], result
[4];
759 struct gl_program_machine dMachine
;
760 if (!fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], span
, 'X',
762 /* This is tricky. Make a copy of the current machine state,
763 * increment the input registers by the dx or dy partial
764 * derivatives, then re-execute the program up to the
765 * preceeding instruction, then fetch the source register.
766 * Finally, find the difference in the register values for
767 * the original and derivative runs.
769 fetch_vector4(&inst
->SrcReg
[0], machine
, program
, a
);
770 init_machine_deriv(ctx
, machine
, program
, span
,
772 execute_program(ctx
, program
, pc
, &dMachine
, span
, column
);
773 fetch_vector4(&inst
->SrcReg
[0], &dMachine
, program
,
775 result
[0] = aNext
[0] - a
[0];
776 result
[1] = aNext
[1] - a
[1];
777 result
[2] = aNext
[2] - a
[2];
778 result
[3] = aNext
[3] - a
[3];
780 store_vector4(inst
, machine
, result
);
782 store_vector4(inst
, machine
, ZeroVec
);
786 case OPCODE_DDY
: /* Partial derivative with respect to Y */
789 GLfloat a
[4], aNext
[4], result
[4];
790 struct gl_program_machine dMachine
;
791 if (!fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], span
, 'Y',
793 init_machine_deriv(ctx
, machine
, program
, span
,
795 fetch_vector4(&inst
->SrcReg
[0], machine
, program
, a
);
796 execute_program(ctx
, program
, pc
, &dMachine
, span
, column
);
797 fetch_vector4(&inst
->SrcReg
[0], &dMachine
, program
,
799 result
[0] = aNext
[0] - a
[0];
800 result
[1] = aNext
[1] - a
[1];
801 result
[2] = aNext
[2] - a
[2];
802 result
[3] = aNext
[3] - a
[3];
804 store_vector4(inst
, machine
, result
);
806 store_vector4(inst
, machine
, ZeroVec
);
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] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
816 store_vector4(inst
, machine
, result
);
818 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
819 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
825 GLfloat a
[4], b
[4], result
[4];
826 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
827 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
828 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
, b
);
829 store_vector4(inst
, machine
, result
);
831 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
832 result
[0], a
[0], a
[1], a
[2], a
[3],
833 b
[0], b
[1], b
[2], b
[3]);
839 GLfloat a
[4], b
[4], result
[4];
840 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
841 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
842 result
[0] = result
[1] = result
[2] = result
[3] =
843 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + b
[3];
844 store_vector4(inst
, machine
, result
);
847 case OPCODE_DST
: /* Distance vector */
849 GLfloat a
[4], b
[4], result
[4];
850 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
851 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
853 result
[1] = a
[1] * b
[1];
856 store_vector4(inst
, machine
, result
);
861 GLfloat t
[4], q
[4], floor_t0
;
862 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
863 floor_t0
= FLOORF(t
[0]);
864 if (floor_t0
> FLT_MAX_EXP
) {
865 SET_POS_INFINITY(q
[0]);
866 SET_POS_INFINITY(q
[2]);
868 else if (floor_t0
< FLT_MIN_EXP
) {
873 q
[0] = LDEXPF(1.0, (int) floor_t0
);
874 /* Note: GL_NV_vertex_program expects
875 * result.z = result.x * APPX(result.y)
876 * We do what the ARB extension says.
878 q
[2] = pow(2.0, t
[0]);
880 q
[1] = t
[0] - floor_t0
;
882 store_vector4( inst
, machine
, q
);
885 case OPCODE_EX2
: /* Exponential base 2 */
887 GLfloat a
[4], result
[4];
888 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
889 result
[0] = result
[1] = result
[2] = result
[3] =
890 (GLfloat
) _mesa_pow(2.0, a
[0]);
891 store_vector4(inst
, machine
, result
);
896 GLfloat a
[4], result
[4];
897 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
898 result
[0] = FLOORF(a
[0]);
899 result
[1] = FLOORF(a
[1]);
900 result
[2] = FLOORF(a
[2]);
901 result
[3] = FLOORF(a
[3]);
902 store_vector4(inst
, machine
, result
);
907 GLfloat a
[4], result
[4];
908 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
909 result
[0] = a
[0] - FLOORF(a
[0]);
910 result
[1] = a
[1] - FLOORF(a
[1]);
911 result
[2] = a
[2] - FLOORF(a
[2]);
912 result
[3] = a
[3] - FLOORF(a
[3]);
913 store_vector4(inst
, machine
, result
);
917 if (eval_condition(machine
, inst
)) {
918 /* do if-clause (just continue execution) */
921 /* go to the instruction after ELSE or ENDIF */
922 assert(inst
->BranchTarget
>= 0);
923 pc
= inst
->BranchTarget
- 1;
928 assert(inst
->BranchTarget
>= 0);
929 pc
= inst
->BranchTarget
- 1;
934 case OPCODE_INT
: /* float to int */
936 GLfloat a
[4], result
[4];
937 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
938 result
[0] = (GLfloat
) (GLint
) a
[0];
939 result
[1] = (GLfloat
) (GLint
) a
[1];
940 result
[2] = (GLfloat
) (GLint
) a
[2];
941 result
[3] = (GLfloat
) (GLint
) a
[3];
942 store_vector4(inst
, machine
, result
);
945 case OPCODE_KIL_NV
: /* NV_f_p only (conditional) */
946 if (eval_condition(machine
, inst
)) {
950 case OPCODE_KIL
: /* ARB_f_p only */
953 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
954 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
959 case OPCODE_LG2
: /* log base 2 */
961 GLfloat a
[4], result
[4];
962 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
963 result
[0] = result
[1] = result
[2] = result
[3] = LOG2(a
[0]);
964 store_vector4(inst
, machine
, result
);
969 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
970 GLfloat a
[4], result
[4];
971 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
972 a
[0] = MAX2(a
[0], 0.0F
);
973 a
[1] = MAX2(a
[1], 0.0F
);
974 /* XXX ARB version clamps a[3], NV version doesn't */
975 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
978 /* XXX we could probably just use pow() here */
980 if (a
[1] == 0.0 && a
[3] == 0.0)
983 result
[2] = EXPF(a
[3] * LOGF(a
[1]));
989 store_vector4(inst
, machine
, result
);
991 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
992 result
[0], result
[1], result
[2], result
[3],
993 a
[0], a
[1], a
[2], a
[3]);
999 GLfloat t
[4], q
[4], abs_t0
;
1000 fetch_vector1(&inst
->SrcReg
[0], machine
, t
);
1001 abs_t0
= FABSF(t
[0]);
1002 if (abs_t0
!= 0.0F
) {
1003 /* Since we really can't handle infinite values on VMS
1004 * like other OSes we'll use __MAXFLOAT to represent
1005 * infinity. This may need some tweaking.
1008 if (abs_t0
== __MAXFLOAT
)
1010 if (IS_INF_OR_NAN(abs_t0
))
1013 SET_POS_INFINITY(q
[0]);
1015 SET_POS_INFINITY(q
[2]);
1019 GLfloat mantissa
= FREXPF(t
[0], &exponent
);
1020 q
[0] = (GLfloat
) (exponent
- 1);
1021 q
[1] = (GLfloat
) (2.0 * mantissa
); /* map [.5, 1) -> [1, 2) */
1022 q
[2] = (GLfloat
) (q
[0] + LOG2(q
[1]));
1026 SET_NEG_INFINITY(q
[0]);
1028 SET_NEG_INFINITY(q
[2]);
1031 store_vector4(inst
, machine
, q
);
1036 GLfloat a
[4], b
[4], c
[4], result
[4];
1037 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1038 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1039 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1040 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
1041 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
1042 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
1043 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
1044 store_vector4(inst
, machine
, result
);
1046 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
1047 "(%g %g %g %g), (%g %g %g %g)\n",
1048 result
[0], result
[1], result
[2], result
[3],
1049 a
[0], a
[1], a
[2], a
[3],
1050 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1056 GLfloat a
[4], b
[4], c
[4], result
[4];
1057 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1058 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1059 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1060 result
[0] = a
[0] * b
[0] + c
[0];
1061 result
[1] = a
[1] * b
[1] + c
[1];
1062 result
[2] = a
[2] * b
[2] + c
[2];
1063 result
[3] = a
[3] * b
[3] + c
[3];
1064 store_vector4(inst
, machine
, result
);
1066 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
1067 "(%g %g %g %g) + (%g %g %g %g)\n",
1068 result
[0], result
[1], result
[2], result
[3],
1069 a
[0], a
[1], a
[2], a
[3],
1070 b
[0], b
[1], b
[2], b
[3], c
[0], c
[1], c
[2], c
[3]);
1076 GLfloat a
[4], b
[4], result
[4];
1077 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1078 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1079 result
[0] = MAX2(a
[0], b
[0]);
1080 result
[1] = MAX2(a
[1], b
[1]);
1081 result
[2] = MAX2(a
[2], b
[2]);
1082 result
[3] = MAX2(a
[3], b
[3]);
1083 store_vector4(inst
, machine
, result
);
1085 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
1086 result
[0], result
[1], result
[2], result
[3],
1087 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1093 GLfloat a
[4], b
[4], result
[4];
1094 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1095 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1096 result
[0] = MIN2(a
[0], b
[0]);
1097 result
[1] = MIN2(a
[1], b
[1]);
1098 result
[2] = MIN2(a
[2], b
[2]);
1099 result
[3] = MIN2(a
[3], b
[3]);
1100 store_vector4(inst
, machine
, result
);
1106 fetch_vector4(&inst
->SrcReg
[0], machine
, result
);
1107 store_vector4(inst
, machine
, result
);
1109 printf("MOV (%g %g %g %g)\n",
1110 result
[0], result
[1], result
[2], result
[3]);
1116 GLfloat a
[4], b
[4], result
[4];
1117 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1118 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1119 result
[0] = a
[0] * b
[0];
1120 result
[1] = a
[1] * b
[1];
1121 result
[2] = a
[2] * b
[2];
1122 result
[3] = a
[3] * b
[3];
1123 store_vector4(inst
, machine
, result
);
1125 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1126 result
[0], result
[1], result
[2], result
[3],
1127 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1133 GLfloat a
[4], result
[4];
1134 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1137 result
[2] = result
[3] = _slang_library_noise1(a
[0]);
1138 store_vector4(inst
, machine
, result
);
1143 GLfloat a
[4], result
[4];
1144 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1147 result
[2] = result
[3] = _slang_library_noise2(a
[0], a
[1]);
1148 store_vector4(inst
, machine
, result
);
1153 GLfloat a
[4], result
[4];
1154 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1158 result
[3] = _slang_library_noise3(a
[0], a
[1], a
[2]);
1159 store_vector4(inst
, machine
, result
);
1164 GLfloat a
[4], result
[4];
1165 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1169 result
[3] = _slang_library_noise4(a
[0], a
[1], a
[2], a
[3]);
1170 store_vector4(inst
, machine
, result
);
1175 case OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
1177 GLfloat a
[4], result
[4];
1179 GLuint
*rawResult
= (GLuint
*) result
;
1181 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1182 hx
= _mesa_float_to_half(a
[0]);
1183 hy
= _mesa_float_to_half(a
[1]);
1184 twoHalves
= hx
| (hy
<< 16);
1185 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1187 store_vector4(inst
, machine
, result
);
1190 case OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
1192 GLfloat a
[4], result
[4];
1193 GLuint usx
, usy
, *rawResult
= (GLuint
*) result
;
1194 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1195 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1196 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1197 usx
= IROUND(a
[0] * 65535.0F
);
1198 usy
= IROUND(a
[1] * 65535.0F
);
1199 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1200 = usx
| (usy
<< 16);
1201 store_vector4(inst
, machine
, result
);
1204 case OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
1206 GLfloat a
[4], result
[4];
1207 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
1208 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1209 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
1210 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
1211 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
1212 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
1213 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
1214 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
1215 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
1216 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
1217 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1218 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1219 store_vector4(inst
, machine
, result
);
1222 case OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
1224 GLfloat a
[4], result
[4];
1225 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
1226 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1227 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1228 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1229 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
1230 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
1231 ubx
= IROUND(255.0F
* a
[0]);
1232 uby
= IROUND(255.0F
* a
[1]);
1233 ubz
= IROUND(255.0F
* a
[2]);
1234 ubw
= IROUND(255.0F
* a
[3]);
1235 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1236 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1237 store_vector4(inst
, machine
, result
);
1242 GLfloat a
[4], b
[4], result
[4];
1243 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1244 fetch_vector1(&inst
->SrcReg
[1], machine
, b
);
1245 result
[0] = result
[1] = result
[2] = result
[3]
1246 = (GLfloat
) _mesa_pow(a
[0], b
[0]);
1247 store_vector4(inst
, machine
, result
);
1252 GLfloat a
[4], result
[4];
1253 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1257 else if (IS_INF_OR_NAN(a
[0]))
1258 printf("RCP(inf)\n");
1260 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
1261 store_vector4(inst
, machine
, result
);
1264 case OPCODE_RET
: /* return from subroutine (conditional) */
1265 if (eval_condition(machine
, inst
)) {
1266 if (machine
->StackDepth
== 0) {
1267 return GL_TRUE
; /* Per GL_NV_vertex_program2 spec */
1269 pc
= machine
->CallStack
[--machine
->StackDepth
];
1272 case OPCODE_RFL
: /* reflection vector */
1274 GLfloat axis
[4], dir
[4], result
[4], tmpX
, tmpW
;
1275 fetch_vector4(&inst
->SrcReg
[0], machine
, axis
);
1276 fetch_vector4(&inst
->SrcReg
[1], machine
, dir
);
1277 tmpW
= DOT3(axis
, axis
);
1278 tmpX
= (2.0F
* DOT3(axis
, dir
)) / tmpW
;
1279 result
[0] = tmpX
* axis
[0] - dir
[0];
1280 result
[1] = tmpX
* axis
[1] - dir
[1];
1281 result
[2] = tmpX
* axis
[2] - dir
[2];
1282 /* result[3] is never written! XXX enforce in parser! */
1283 store_vector4(inst
, machine
, result
);
1286 case OPCODE_RSQ
: /* 1 / sqrt() */
1288 GLfloat a
[4], result
[4];
1289 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1291 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1292 store_vector4(inst
, machine
, result
);
1294 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
1298 case OPCODE_SCS
: /* sine and cos */
1300 GLfloat a
[4], result
[4];
1301 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1302 result
[0] = (GLfloat
) _mesa_cos(a
[0]);
1303 result
[1] = (GLfloat
) _mesa_sin(a
[0]);
1304 result
[2] = 0.0; /* undefined! */
1305 result
[3] = 0.0; /* undefined! */
1306 store_vector4(inst
, machine
, result
);
1309 case OPCODE_SEQ
: /* set on equal */
1311 GLfloat a
[4], b
[4], result
[4];
1312 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1313 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1314 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1315 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1316 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1317 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1318 store_vector4(inst
, machine
, result
);
1320 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1321 result
[0], result
[1], result
[2], result
[3],
1322 a
[0], a
[1], a
[2], a
[3],
1323 b
[0], b
[1], b
[2], b
[3]);
1327 case OPCODE_SFL
: /* set false, operands ignored */
1329 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1330 store_vector4(inst
, machine
, result
);
1333 case OPCODE_SGE
: /* set on greater or equal */
1335 GLfloat a
[4], b
[4], result
[4];
1336 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1337 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1338 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1339 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1340 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1341 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1342 store_vector4(inst
, machine
, result
);
1344 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1345 result
[0], result
[1], result
[2], result
[3],
1346 a
[0], a
[1], a
[2], a
[3],
1347 b
[0], b
[1], b
[2], b
[3]);
1351 case OPCODE_SGT
: /* set on greater */
1353 GLfloat a
[4], b
[4], result
[4];
1354 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1355 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1356 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1357 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1358 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1359 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1360 store_vector4(inst
, machine
, result
);
1362 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1363 result
[0], result
[1], result
[2], result
[3],
1364 a
[0], a
[1], a
[2], a
[3],
1365 b
[0], b
[1], b
[2], b
[3]);
1371 GLfloat a
[4], result
[4];
1372 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1373 result
[0] = result
[1] = result
[2] = result
[3]
1374 = (GLfloat
) _mesa_sin(a
[0]);
1375 store_vector4(inst
, machine
, result
);
1378 case OPCODE_SLE
: /* set on less or equal */
1380 GLfloat a
[4], b
[4], result
[4];
1381 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1382 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1383 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1384 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1385 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1386 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1387 store_vector4(inst
, machine
, result
);
1389 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1390 result
[0], result
[1], result
[2], result
[3],
1391 a
[0], a
[1], a
[2], a
[3],
1392 b
[0], b
[1], b
[2], b
[3]);
1396 case OPCODE_SLT
: /* set on less */
1398 GLfloat a
[4], b
[4], result
[4];
1399 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1400 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1401 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1402 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1403 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1404 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1405 store_vector4(inst
, machine
, result
);
1407 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1408 result
[0], result
[1], result
[2], result
[3],
1409 a
[0], a
[1], a
[2], a
[3],
1410 b
[0], b
[1], b
[2], b
[3]);
1414 case OPCODE_SNE
: /* set on not equal */
1416 GLfloat a
[4], b
[4], result
[4];
1417 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1418 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1419 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1420 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1421 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1422 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1423 store_vector4(inst
, machine
, result
);
1425 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1426 result
[0], result
[1], result
[2], result
[3],
1427 a
[0], a
[1], a
[2], a
[3],
1428 b
[0], b
[1], b
[2], b
[3]);
1432 case OPCODE_STR
: /* set true, operands ignored */
1434 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1435 store_vector4(inst
, machine
, result
);
1440 GLfloat a
[4], b
[4], result
[4];
1441 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1442 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1443 result
[0] = a
[0] - b
[0];
1444 result
[1] = a
[1] - b
[1];
1445 result
[2] = a
[2] - b
[2];
1446 result
[3] = a
[3] - b
[3];
1447 store_vector4(inst
, machine
, result
);
1449 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1450 result
[0], result
[1], result
[2], result
[3],
1451 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1455 case OPCODE_SWZ
: /* extended swizzle */
1457 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1458 const GLfloat
*src
= get_register_pointer(source
, machine
);
1461 for (i
= 0; i
< 4; i
++) {
1462 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1463 if (swz
== SWIZZLE_ZERO
)
1465 else if (swz
== SWIZZLE_ONE
)
1470 result
[i
] = src
[swz
];
1472 if (source
->NegateBase
& (1 << i
))
1473 result
[i
] = -result
[i
];
1475 store_vector4(inst
, machine
, result
);
1478 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1481 /* Note: only use the precomputed lambda value when we're
1482 * sampling texture unit [K] with texcoord[K].
1483 * Otherwise, the lambda value may have no relation to the
1484 * instruction's texcoord or texture image. Using the wrong
1485 * lambda is usually bad news.
1486 * The rest of the time, just use zero (until we get a more
1487 * sophisticated way of computing lambda).
1489 GLfloat coord
[4], color
[4], lambda
;
1491 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
1492 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
)
1493 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
];
1497 fetch_vector4(&inst
->SrcReg
[0], machine
, coord
);
1498 machine
->FetchTexelLod(ctx
, coord
, lambda
, inst
->TexSrcUnit
,
1501 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g], "
1503 color
[0], color
[1], color
[2], color
[3],
1505 coord
[0], coord
[1], coord
[2], coord
[3], lambda
);
1507 store_vector4(inst
, machine
, color
);
1510 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1511 /* Texel lookup with LOD bias */
1513 const struct gl_texture_unit
*texUnit
1514 = &ctx
->Texture
.Unit
[inst
->TexSrcUnit
];
1515 GLfloat coord
[4], color
[4], lambda
, bias
;
1517 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
1518 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
)
1519 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
];
1523 fetch_vector4(&inst
->SrcReg
[0], machine
, coord
);
1524 /* coord[3] is the bias to add to lambda */
1525 bias
= texUnit
->LodBias
+ coord
[3];
1526 if (texUnit
->_Current
)
1527 bias
+= texUnit
->_Current
->LodBias
;
1528 machine
->FetchTexelLod(ctx
, coord
, lambda
+ bias
,
1529 inst
->TexSrcUnit
, color
);
1530 store_vector4(inst
, machine
, color
);
1533 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1534 /* Texture lookup w/ partial derivatives for LOD */
1536 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1537 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1538 fetch_vector4(&inst
->SrcReg
[1], machine
, dtdx
);
1539 fetch_vector4(&inst
->SrcReg
[2], machine
, dtdy
);
1540 machine
->FetchTexelDeriv(ctx
, texcoord
, dtdx
, dtdy
,
1541 inst
->TexSrcUnit
, color
);
1542 store_vector4(inst
, machine
, color
);
1545 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1546 /* Texture lookup w/ projective divide */
1548 GLfloat texcoord
[4], color
[4], lambda
;
1550 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
1551 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
)
1552 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
];
1556 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1557 /* Not so sure about this test - if texcoord[3] is
1558 * zero, we'd probably be fine except for an ASSERT in
1559 * IROUND_POS() which gets triggered by the inf values created.
1561 if (texcoord
[3] != 0.0) {
1562 texcoord
[0] /= texcoord
[3];
1563 texcoord
[1] /= texcoord
[3];
1564 texcoord
[2] /= texcoord
[3];
1566 machine
->FetchTexelLod(ctx
, texcoord
, lambda
,
1567 inst
->TexSrcUnit
, color
);
1568 store_vector4(inst
, machine
, color
);
1571 case OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1572 /* Texture lookup w/ projective divide */
1574 GLfloat texcoord
[4], color
[4], lambda
;
1576 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
1577 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+ inst
->TexSrcUnit
)
1578 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
];
1582 fetch_vector4(&inst
->SrcReg
[0], machine
, texcoord
);
1583 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1584 texcoord
[3] != 0.0) {
1585 texcoord
[0] /= texcoord
[3];
1586 texcoord
[1] /= texcoord
[3];
1587 texcoord
[2] /= texcoord
[3];
1589 machine
->FetchTexelLod(ctx
, texcoord
, lambda
,
1590 inst
->TexSrcUnit
, color
);
1591 store_vector4(inst
, machine
, color
);
1594 case OPCODE_UP2H
: /* unpack two 16-bit floats */
1596 GLfloat a
[4], result
[4];
1597 const GLuint
*rawBits
= (const GLuint
*) a
;
1599 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1600 hx
= rawBits
[0] & 0xffff;
1601 hy
= rawBits
[0] >> 16;
1602 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1603 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1604 store_vector4(inst
, machine
, result
);
1607 case OPCODE_UP2US
: /* unpack two GLushorts */
1609 GLfloat a
[4], result
[4];
1610 const GLuint
*rawBits
= (const GLuint
*) a
;
1612 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1613 usx
= rawBits
[0] & 0xffff;
1614 usy
= rawBits
[0] >> 16;
1615 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1616 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1617 store_vector4(inst
, machine
, result
);
1620 case OPCODE_UP4B
: /* unpack four GLbytes */
1622 GLfloat a
[4], result
[4];
1623 const GLuint
*rawBits
= (const GLuint
*) a
;
1624 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1625 result
[0] = (((rawBits
[0] >> 0) & 0xff) - 128) / 127.0F
;
1626 result
[1] = (((rawBits
[0] >> 8) & 0xff) - 128) / 127.0F
;
1627 result
[2] = (((rawBits
[0] >> 16) & 0xff) - 128) / 127.0F
;
1628 result
[3] = (((rawBits
[0] >> 24) & 0xff) - 128) / 127.0F
;
1629 store_vector4(inst
, machine
, result
);
1632 case OPCODE_UP4UB
: /* unpack four GLubytes */
1634 GLfloat a
[4], result
[4];
1635 const GLuint
*rawBits
= (const GLuint
*) a
;
1636 fetch_vector1(&inst
->SrcReg
[0], machine
, a
);
1637 result
[0] = ((rawBits
[0] >> 0) & 0xff) / 255.0F
;
1638 result
[1] = ((rawBits
[0] >> 8) & 0xff) / 255.0F
;
1639 result
[2] = ((rawBits
[0] >> 16) & 0xff) / 255.0F
;
1640 result
[3] = ((rawBits
[0] >> 24) & 0xff) / 255.0F
;
1641 store_vector4(inst
, machine
, result
);
1644 case OPCODE_XPD
: /* cross product */
1646 GLfloat a
[4], b
[4], result
[4];
1647 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1648 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1649 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1650 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1651 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1653 store_vector4(inst
, machine
, result
);
1655 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1656 result
[0], result
[1], result
[2], result
[3],
1657 a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
1661 case OPCODE_X2D
: /* 2-D matrix transform */
1663 GLfloat a
[4], b
[4], c
[4], result
[4];
1664 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1665 fetch_vector4(&inst
->SrcReg
[1], machine
, b
);
1666 fetch_vector4(&inst
->SrcReg
[2], machine
, c
);
1667 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1668 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1669 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1670 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1671 store_vector4(inst
, machine
, result
);
1676 if (inst
->SrcReg
[0].File
!= -1) {
1678 fetch_vector4(&inst
->SrcReg
[0], machine
, a
);
1679 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst
->Data
,
1680 a
[0], a
[1], a
[2], a
[3]);
1683 _mesa_printf("%s\n", (const char *) inst
->Data
);
1690 _mesa_problem(ctx
, "Bad opcode %d in _mesa_exec_fragment_program",
1692 return GL_TRUE
; /* return value doesn't matter */
1697 if (numExec
> maxExec
) {
1698 _mesa_problem(ctx
, "Infinite loop detected in fragment program");
1704 #if FEATURE_MESA_program_debug
1705 CurrentMachine
= NULL
;