2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 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 * Regarding GL_NV_fragment_program:
28 * Portions of this software may use or implement intellectual
29 * property owned and licensed by NVIDIA Corporation. NVIDIA disclaims
30 * any and all warranties with respect to such intellectual property,
31 * including any use thereof or modifications thereto.
37 #include "program_instruction.h"
40 #include "s_nvfragprog.h"
44 /* See comments below for info about this */
47 /* if 1, print some debugging info */
52 * Virtual machine state used during execution of a fragment programs.
56 GLfloat Temporaries
[MAX_NV_FRAGMENT_PROGRAM_TEMPS
][4];
57 GLfloat Inputs
[MAX_NV_FRAGMENT_PROGRAM_INPUTS
][4];
58 GLfloat Outputs
[MAX_NV_FRAGMENT_PROGRAM_OUTPUTS
][4];
59 GLuint CondCodes
[4]; /**< COND_* value for x/y/z/w */
63 #if FEATURE_MESA_program_debug
64 static struct fp_machine
*CurrentMachine
= NULL
;
67 * For GL_MESA_program_debug.
68 * Return current value (4*GLfloat) of a fragment program register.
69 * Called via ctx->Driver.GetFragmentProgramRegister().
72 _swrast_get_program_register(GLcontext
*ctx
, enum register_file file
,
73 GLuint index
, GLfloat val
[4])
78 COPY_4V(val
, CurrentMachine
->Inputs
[index
]);
81 COPY_4V(val
, CurrentMachine
->Outputs
[index
]);
83 case PROGRAM_TEMPORARY
:
84 COPY_4V(val
, CurrentMachine
->Temporaries
[index
]);
88 "bad register file in _swrast_get_program_register");
92 #endif /* FEATURE_MESA_program_debug */
99 fetch_texel( GLcontext
*ctx
, const GLfloat texcoord
[4], GLfloat lambda
,
100 GLuint unit
, GLfloat color
[4] )
103 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
105 /* XXX use a float-valued TextureSample routine here!!! */
106 swrast
->TextureSample
[unit
](ctx
, ctx
->Texture
.Unit
[unit
]._Current
,
107 1, (const GLfloat (*)[4]) texcoord
,
109 color
[0] = CHAN_TO_FLOAT(rgba
[0]);
110 color
[1] = CHAN_TO_FLOAT(rgba
[1]);
111 color
[2] = CHAN_TO_FLOAT(rgba
[2]);
112 color
[3] = CHAN_TO_FLOAT(rgba
[3]);
117 * Fetch a texel with the given partial derivatives to compute a level
118 * of detail in the mipmap.
121 fetch_texel_deriv( GLcontext
*ctx
, const GLfloat texcoord
[4],
122 const GLfloat texdx
[4], const GLfloat texdy
[4],
123 GLuint unit
, GLfloat color
[4] )
125 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
126 const struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[unit
]._Current
;
127 const struct gl_texture_image
*texImg
= texObj
->Image
[0][texObj
->BaseLevel
];
128 const GLfloat texW
= (GLfloat
) texImg
->WidthScale
;
129 const GLfloat texH
= (GLfloat
) texImg
->HeightScale
;
132 GLfloat lambda
= _swrast_compute_lambda(texdx
[0], texdy
[0], /* ds/dx, ds/dy */
133 texdx
[1], texdy
[1], /* dt/dx, dt/dy */
134 texdx
[3], texdy
[2], /* dq/dx, dq/dy */
136 texcoord
[0], texcoord
[1], texcoord
[3],
139 swrast
->TextureSample
[unit
](ctx
, ctx
->Texture
.Unit
[unit
]._Current
,
140 1, (const GLfloat (*)[4]) texcoord
,
142 color
[0] = CHAN_TO_FLOAT(rgba
[0]);
143 color
[1] = CHAN_TO_FLOAT(rgba
[1]);
144 color
[2] = CHAN_TO_FLOAT(rgba
[2]);
145 color
[3] = CHAN_TO_FLOAT(rgba
[3]);
150 * Return a pointer to the 4-element float vector specified by the given
153 static INLINE
const GLfloat
*
154 get_register_pointer( GLcontext
*ctx
,
155 const struct prog_src_register
*source
,
156 const struct fp_machine
*machine
,
157 const struct gl_fragment_program
*program
)
159 switch (source
->File
) {
160 case PROGRAM_TEMPORARY
:
161 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_TEMPS
);
162 return machine
->Temporaries
[source
->Index
];
164 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_INPUTS
);
165 return machine
->Inputs
[source
->Index
];
167 /* This is only for PRINT */
168 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_OUTPUTS
);
169 return machine
->Outputs
[source
->Index
];
170 case PROGRAM_LOCAL_PARAM
:
171 ASSERT(source
->Index
< MAX_PROGRAM_LOCAL_PARAMS
);
172 return program
->Base
.LocalParams
[source
->Index
];
173 case PROGRAM_ENV_PARAM
:
174 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_PARAMS
);
175 return ctx
->FragmentProgram
.Parameters
[source
->Index
];
176 case PROGRAM_STATE_VAR
:
178 case PROGRAM_CONSTANT
:
180 case PROGRAM_NAMED_PARAM
:
181 ASSERT(source
->Index
< (GLint
) program
->Base
.Parameters
->NumParameters
);
182 return program
->Base
.Parameters
->ParameterValues
[source
->Index
];
184 _mesa_problem(ctx
, "Invalid input register file %d in fp "
185 "get_register_pointer", source
->File
);
192 * Fetch a 4-element float vector from the given source register.
193 * Apply swizzling and negating as needed.
196 fetch_vector4( GLcontext
*ctx
,
197 const struct prog_src_register
*source
,
198 const struct fp_machine
*machine
,
199 const struct gl_fragment_program
*program
,
202 const GLfloat
*src
= get_register_pointer(ctx
, source
, machine
, program
);
205 if (source
->Swizzle
== MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
206 SWIZZLE_Z
, SWIZZLE_W
)) {
208 COPY_4V(result
, src
);
211 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
212 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
213 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
214 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
217 if (source
->NegateBase
) {
218 result
[0] = -result
[0];
219 result
[1] = -result
[1];
220 result
[2] = -result
[2];
221 result
[3] = -result
[3];
224 result
[0] = FABSF(result
[0]);
225 result
[1] = FABSF(result
[1]);
226 result
[2] = FABSF(result
[2]);
227 result
[3] = FABSF(result
[3]);
229 if (source
->NegateAbs
) {
230 result
[0] = -result
[0];
231 result
[1] = -result
[1];
232 result
[2] = -result
[2];
233 result
[3] = -result
[3];
239 * Fetch the derivative with respect to X for the given register.
240 * \return GL_TRUE if it was easily computed or GL_FALSE if we
241 * need to execute another instance of the program (ugh)!
244 fetch_vector4_deriv( GLcontext
*ctx
,
245 const struct prog_src_register
*source
,
247 char xOrY
, GLint column
, GLfloat result
[4] )
251 ASSERT(xOrY
== 'X' || xOrY
== 'Y');
253 switch (source
->Index
) {
254 case FRAG_ATTRIB_WPOS
:
258 src
[2] = span
->dzdx
/ ctx
->DrawBuffer
->_DepthMaxF
;
264 src
[2] = span
->dzdy
/ ctx
->DrawBuffer
->_DepthMaxF
;
268 case FRAG_ATTRIB_COL0
:
270 src
[0] = span
->drdx
* (1.0F
/ CHAN_MAXF
);
271 src
[1] = span
->dgdx
* (1.0F
/ CHAN_MAXF
);
272 src
[2] = span
->dbdx
* (1.0F
/ CHAN_MAXF
);
273 src
[3] = span
->dadx
* (1.0F
/ CHAN_MAXF
);
276 src
[0] = span
->drdy
* (1.0F
/ CHAN_MAXF
);
277 src
[1] = span
->dgdy
* (1.0F
/ CHAN_MAXF
);
278 src
[2] = span
->dbdy
* (1.0F
/ CHAN_MAXF
);
279 src
[3] = span
->dady
* (1.0F
/ CHAN_MAXF
);
282 case FRAG_ATTRIB_COL1
:
284 src
[0] = span
->dsrdx
* (1.0F
/ CHAN_MAXF
);
285 src
[1] = span
->dsgdx
* (1.0F
/ CHAN_MAXF
);
286 src
[2] = span
->dsbdx
* (1.0F
/ CHAN_MAXF
);
287 src
[3] = 0.0; /* XXX need this */
290 src
[0] = span
->dsrdy
* (1.0F
/ CHAN_MAXF
);
291 src
[1] = span
->dsgdy
* (1.0F
/ CHAN_MAXF
);
292 src
[2] = span
->dsbdy
* (1.0F
/ CHAN_MAXF
);
293 src
[3] = 0.0; /* XXX need this */
296 case FRAG_ATTRIB_FOGC
:
298 src
[0] = span
->dfogdx
;
304 src
[0] = span
->dfogdy
;
310 case FRAG_ATTRIB_TEX0
:
311 case FRAG_ATTRIB_TEX1
:
312 case FRAG_ATTRIB_TEX2
:
313 case FRAG_ATTRIB_TEX3
:
314 case FRAG_ATTRIB_TEX4
:
315 case FRAG_ATTRIB_TEX5
:
316 case FRAG_ATTRIB_TEX6
:
317 case FRAG_ATTRIB_TEX7
:
319 const GLuint u
= source
->Index
- FRAG_ATTRIB_TEX0
;
320 /* this is a little tricky - I think I've got it right */
321 const GLfloat invQ
= 1.0f
/ (span
->tex
[u
][3]
322 + span
->texStepX
[u
][3] * column
);
323 src
[0] = span
->texStepX
[u
][0] * invQ
;
324 src
[1] = span
->texStepX
[u
][1] * invQ
;
325 src
[2] = span
->texStepX
[u
][2] * invQ
;
326 src
[3] = span
->texStepX
[u
][3] * invQ
;
329 const GLuint u
= source
->Index
- FRAG_ATTRIB_TEX0
;
330 /* Tricky, as above, but in Y direction */
331 const GLfloat invQ
= 1.0f
/ (span
->tex
[u
][3] + span
->texStepY
[u
][3]);
332 src
[0] = span
->texStepY
[u
][0] * invQ
;
333 src
[1] = span
->texStepY
[u
][1] * invQ
;
334 src
[2] = span
->texStepY
[u
][2] * invQ
;
335 src
[3] = span
->texStepY
[u
][3] * invQ
;
342 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
343 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
344 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
345 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
347 if (source
->NegateBase
) {
348 result
[0] = -result
[0];
349 result
[1] = -result
[1];
350 result
[2] = -result
[2];
351 result
[3] = -result
[3];
354 result
[0] = FABSF(result
[0]);
355 result
[1] = FABSF(result
[1]);
356 result
[2] = FABSF(result
[2]);
357 result
[3] = FABSF(result
[3]);
359 if (source
->NegateAbs
) {
360 result
[0] = -result
[0];
361 result
[1] = -result
[1];
362 result
[2] = -result
[2];
363 result
[3] = -result
[3];
370 * As above, but only return result[0] element.
373 fetch_vector1( GLcontext
*ctx
,
374 const struct prog_src_register
*source
,
375 const struct fp_machine
*machine
,
376 const struct gl_fragment_program
*program
,
379 const GLfloat
*src
= get_register_pointer(ctx
, source
, machine
, program
);
382 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
384 if (source
->NegateBase
) {
385 result
[0] = -result
[0];
388 result
[0] = FABSF(result
[0]);
390 if (source
->NegateAbs
) {
391 result
[0] = -result
[0];
397 * Test value against zero and return GT, LT, EQ or UN if NaN.
400 generate_cc( float value
)
403 return COND_UN
; /* NaN */
413 * Test if the ccMaskRule is satisfied by the given condition code.
414 * Used to mask destination writes according to the current condition code.
416 static INLINE GLboolean
417 test_cc(GLuint condCode
, GLuint ccMaskRule
)
419 switch (ccMaskRule
) {
420 case COND_EQ
: return (condCode
== COND_EQ
);
421 case COND_NE
: return (condCode
!= COND_EQ
);
422 case COND_LT
: return (condCode
== COND_LT
);
423 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
424 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
425 case COND_GT
: return (condCode
== COND_GT
);
426 case COND_TR
: return GL_TRUE
;
427 case COND_FL
: return GL_FALSE
;
428 default: return GL_TRUE
;
434 * Store 4 floats into a register. Observe the instructions saturate and
435 * set-condition-code flags.
438 store_vector4( const struct prog_instruction
*inst
,
439 struct fp_machine
*machine
,
440 const GLfloat value
[4] )
442 const struct prog_dst_register
*dest
= &(inst
->DstReg
);
443 const GLboolean clamp
= inst
->SaturateMode
== SATURATE_ZERO_ONE
;
446 GLfloat clampedValue
[4];
447 GLuint writeMask
= dest
->WriteMask
;
449 switch (dest
->File
) {
451 dstReg
= machine
->Outputs
[dest
->Index
];
453 case PROGRAM_TEMPORARY
:
454 dstReg
= machine
->Temporaries
[dest
->Index
];
456 case PROGRAM_WRITE_ONLY
:
460 _mesa_problem(NULL
, "bad register file in store_vector4(fp)");
465 if (value
[0] > 1.0e10
||
466 IS_INF_OR_NAN(value
[0]) ||
467 IS_INF_OR_NAN(value
[1]) ||
468 IS_INF_OR_NAN(value
[2]) ||
469 IS_INF_OR_NAN(value
[3]) )
470 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
474 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
475 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
476 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
477 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
478 value
= clampedValue
;
481 if (dest
->CondMask
!= COND_TR
) {
482 /* condition codes may turn off some writes */
483 if (writeMask
& WRITEMASK_X
) {
484 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 0)],
486 writeMask
&= ~WRITEMASK_X
;
488 if (writeMask
& WRITEMASK_Y
) {
489 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 1)],
491 writeMask
&= ~WRITEMASK_Y
;
493 if (writeMask
& WRITEMASK_Z
) {
494 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 2)],
496 writeMask
&= ~WRITEMASK_Z
;
498 if (writeMask
& WRITEMASK_W
) {
499 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 3)],
501 writeMask
&= ~WRITEMASK_W
;
505 if (writeMask
& WRITEMASK_X
)
506 dstReg
[0] = value
[0];
507 if (writeMask
& WRITEMASK_Y
)
508 dstReg
[1] = value
[1];
509 if (writeMask
& WRITEMASK_Z
)
510 dstReg
[2] = value
[2];
511 if (writeMask
& WRITEMASK_W
)
512 dstReg
[3] = value
[3];
514 if (inst
->CondUpdate
) {
515 if (writeMask
& WRITEMASK_X
)
516 machine
->CondCodes
[0] = generate_cc(value
[0]);
517 if (writeMask
& WRITEMASK_Y
)
518 machine
->CondCodes
[1] = generate_cc(value
[1]);
519 if (writeMask
& WRITEMASK_Z
)
520 machine
->CondCodes
[2] = generate_cc(value
[2]);
521 if (writeMask
& WRITEMASK_W
)
522 machine
->CondCodes
[3] = generate_cc(value
[3]);
528 * Initialize a new machine state instance from an existing one, adding
529 * the partial derivatives onto the input registers.
530 * Used to implement DDX and DDY instructions in non-trivial cases.
533 init_machine_deriv( GLcontext
*ctx
,
534 const struct fp_machine
*machine
,
535 const struct gl_fragment_program
*program
,
536 const SWspan
*span
, char xOrY
,
537 struct fp_machine
*dMachine
)
541 ASSERT(xOrY
== 'X' || xOrY
== 'Y');
543 /* copy existing machine */
544 _mesa_memcpy(dMachine
, machine
, sizeof(struct fp_machine
));
546 if (program
->Base
.Target
== GL_FRAGMENT_PROGRAM_NV
) {
547 /* Clear temporary registers (undefined for ARB_f_p) */
548 _mesa_bzero( (void*) machine
->Temporaries
,
549 MAX_NV_FRAGMENT_PROGRAM_TEMPS
* 4 * sizeof(GLfloat
));
552 /* Add derivatives */
553 if (program
->Base
.InputsRead
& (1 << FRAG_ATTRIB_WPOS
)) {
554 GLfloat
*wpos
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_WPOS
];
558 wpos
[2] += span
->dzdx
;
559 wpos
[3] += span
->dwdx
;
564 wpos
[2] += span
->dzdy
;
565 wpos
[3] += span
->dwdy
;
568 if (program
->Base
.InputsRead
& (1 << FRAG_ATTRIB_COL0
)) {
569 GLfloat
*col0
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_COL0
];
571 col0
[0] += span
->drdx
* (1.0F
/ CHAN_MAXF
);
572 col0
[1] += span
->dgdx
* (1.0F
/ CHAN_MAXF
);
573 col0
[2] += span
->dbdx
* (1.0F
/ CHAN_MAXF
);
574 col0
[3] += span
->dadx
* (1.0F
/ CHAN_MAXF
);
577 col0
[0] += span
->drdy
* (1.0F
/ CHAN_MAXF
);
578 col0
[1] += span
->dgdy
* (1.0F
/ CHAN_MAXF
);
579 col0
[2] += span
->dbdy
* (1.0F
/ CHAN_MAXF
);
580 col0
[3] += span
->dady
* (1.0F
/ CHAN_MAXF
);
583 if (program
->Base
.InputsRead
& (1 << FRAG_ATTRIB_COL1
)) {
584 GLfloat
*col1
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_COL1
];
586 col1
[0] += span
->dsrdx
* (1.0F
/ CHAN_MAXF
);
587 col1
[1] += span
->dsgdx
* (1.0F
/ CHAN_MAXF
);
588 col1
[2] += span
->dsbdx
* (1.0F
/ CHAN_MAXF
);
589 col1
[3] += 0.0; /*XXX fix */
592 col1
[0] += span
->dsrdy
* (1.0F
/ CHAN_MAXF
);
593 col1
[1] += span
->dsgdy
* (1.0F
/ CHAN_MAXF
);
594 col1
[2] += span
->dsbdy
* (1.0F
/ CHAN_MAXF
);
595 col1
[3] += 0.0; /*XXX fix */
598 if (program
->Base
.InputsRead
& (1 << FRAG_ATTRIB_FOGC
)) {
599 GLfloat
*fogc
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_FOGC
];
601 fogc
[0] += span
->dfogdx
;
604 fogc
[0] += span
->dfogdy
;
607 for (u
= 0; u
< ctx
->Const
.MaxTextureCoordUnits
; u
++) {
608 if (program
->Base
.InputsRead
& (1 << (FRAG_ATTRIB_TEX0
+ u
))) {
609 GLfloat
*tex
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_TEX0
+ u
];
610 /* XXX perspective-correct interpolation */
612 tex
[0] += span
->texStepX
[u
][0];
613 tex
[1] += span
->texStepX
[u
][1];
614 tex
[2] += span
->texStepX
[u
][2];
615 tex
[3] += span
->texStepX
[u
][3];
618 tex
[0] += span
->texStepY
[u
][0];
619 tex
[1] += span
->texStepY
[u
][1];
620 tex
[2] += span
->texStepY
[u
][2];
621 tex
[3] += span
->texStepY
[u
][3];
626 /* init condition codes */
627 dMachine
->CondCodes
[0] = COND_EQ
;
628 dMachine
->CondCodes
[1] = COND_EQ
;
629 dMachine
->CondCodes
[2] = COND_EQ
;
630 dMachine
->CondCodes
[3] = COND_EQ
;
635 * Execute the given vertex program.
636 * NOTE: we do everything in single-precision floating point; we don't
637 * currently observe the single/half/fixed-precision qualifiers.
638 * \param ctx - rendering context
639 * \param program - the fragment program to execute
640 * \param machine - machine state (register file)
641 * \param maxInst - max number of instructions to execute
642 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
645 execute_program( GLcontext
*ctx
,
646 const struct gl_fragment_program
*program
, GLuint maxInst
,
647 struct fp_machine
*machine
, const SWspan
*span
,
653 printf("execute fragment program --------------------\n");
656 for (pc
= 0; pc
< maxInst
; pc
++) {
657 const struct prog_instruction
*inst
= program
->Base
.Instructions
+ pc
;
659 if (ctx
->FragmentProgram
.CallbackEnabled
&&
660 ctx
->FragmentProgram
.Callback
) {
661 ctx
->FragmentProgram
.CurrentPosition
= inst
->StringPos
;
662 ctx
->FragmentProgram
.Callback(program
->Base
.Target
,
663 ctx
->FragmentProgram
.CallbackData
);
667 _mesa_print_instruction(inst
);
669 switch (inst
->Opcode
) {
672 GLfloat a
[4], result
[4];
673 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
674 result
[0] = FABSF(a
[0]);
675 result
[1] = FABSF(a
[1]);
676 result
[2] = FABSF(a
[2]);
677 result
[3] = FABSF(a
[3]);
678 store_vector4( inst
, machine
, result
);
683 GLfloat a
[4], b
[4], result
[4];
684 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
685 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
686 result
[0] = a
[0] + b
[0];
687 result
[1] = a
[1] + b
[1];
688 result
[2] = a
[2] + b
[2];
689 result
[3] = a
[3] + b
[3];
690 store_vector4( inst
, machine
, result
);
692 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
693 result
[0], result
[1], result
[2], result
[3],
694 a
[0], a
[1], a
[2], a
[3],
695 b
[0], b
[1], b
[2], b
[3]);
701 GLfloat a
[4], b
[4], c
[4], result
[4];
702 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
703 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
704 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
705 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
706 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
707 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
708 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
709 store_vector4( inst
, machine
, result
);
714 GLfloat a
[4], result
[4];
715 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
716 result
[0] = result
[1] = result
[2] = result
[3]
717 = (GLfloat
) _mesa_cos(a
[0]);
718 store_vector4( inst
, machine
, result
);
721 case OPCODE_DDX
: /* Partial derivative with respect to X */
723 GLfloat a
[4], aNext
[4], result
[4];
724 struct fp_machine dMachine
;
725 if (!fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], span
, 'X',
727 /* This is tricky. Make a copy of the current machine state,
728 * increment the input registers by the dx or dy partial
729 * derivatives, then re-execute the program up to the
730 * preceeding instruction, then fetch the source register.
731 * Finally, find the difference in the register values for
732 * the original and derivative runs.
734 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
735 init_machine_deriv(ctx
, machine
, program
, span
,
737 execute_program(ctx
, program
, pc
, &dMachine
, span
, column
);
738 fetch_vector4( ctx
, &inst
->SrcReg
[0], &dMachine
, program
, aNext
);
739 result
[0] = aNext
[0] - a
[0];
740 result
[1] = aNext
[1] - a
[1];
741 result
[2] = aNext
[2] - a
[2];
742 result
[3] = aNext
[3] - a
[3];
744 store_vector4( inst
, machine
, result
);
747 case OPCODE_DDY
: /* Partial derivative with respect to Y */
749 GLfloat a
[4], aNext
[4], result
[4];
750 struct fp_machine dMachine
;
751 if (!fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], span
, 'Y',
753 init_machine_deriv(ctx
, machine
, program
, span
,
755 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
756 execute_program(ctx
, program
, pc
, &dMachine
, span
, column
);
757 fetch_vector4( ctx
, &inst
->SrcReg
[0], &dMachine
, program
, aNext
);
758 result
[0] = aNext
[0] - a
[0];
759 result
[1] = aNext
[1] - a
[1];
760 result
[2] = aNext
[2] - a
[2];
761 result
[3] = aNext
[3] - a
[3];
763 store_vector4( inst
, machine
, result
);
768 GLfloat a
[4], b
[4], result
[4];
769 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
770 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
771 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
772 store_vector4( inst
, machine
, result
);
774 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
775 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
781 GLfloat a
[4], b
[4], result
[4];
782 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
783 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
784 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
,b
);
785 store_vector4( inst
, machine
, result
);
787 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
788 result
[0], a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
794 GLfloat a
[4], b
[4], result
[4];
795 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
796 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
797 result
[0] = result
[1] = result
[2] = result
[3] =
798 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + b
[3];
799 store_vector4( inst
, machine
, result
);
802 case OPCODE_DST
: /* Distance vector */
804 GLfloat a
[4], b
[4], result
[4];
805 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
806 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
808 result
[1] = a
[1] * b
[1];
811 store_vector4( inst
, machine
, result
);
814 case OPCODE_EX2
: /* Exponential base 2 */
816 GLfloat a
[4], result
[4];
817 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
818 result
[0] = result
[1] = result
[2] = result
[3] =
819 (GLfloat
) _mesa_pow(2.0, a
[0]);
820 store_vector4( inst
, machine
, result
);
825 GLfloat a
[4], result
[4];
826 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
827 result
[0] = FLOORF(a
[0]);
828 result
[1] = FLOORF(a
[1]);
829 result
[2] = FLOORF(a
[2]);
830 result
[3] = FLOORF(a
[3]);
831 store_vector4( inst
, machine
, result
);
836 GLfloat a
[4], result
[4];
837 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
838 result
[0] = a
[0] - FLOORF(a
[0]);
839 result
[1] = a
[1] - FLOORF(a
[1]);
840 result
[2] = a
[2] - FLOORF(a
[2]);
841 result
[3] = a
[3] - FLOORF(a
[3]);
842 store_vector4( inst
, machine
, result
);
845 case OPCODE_KIL_NV
: /* NV_f_p only */
847 const GLuint swizzle
= inst
->DstReg
.CondSwizzle
;
848 const GLuint condMask
= inst
->DstReg
.CondMask
;
849 if (test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 0)], condMask
) ||
850 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 1)], condMask
) ||
851 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 2)], condMask
) ||
852 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 3)], condMask
)) {
857 case OPCODE_KIL
: /* ARB_f_p only */
860 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
861 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
866 case OPCODE_LG2
: /* log base 2 */
868 GLfloat a
[4], result
[4];
869 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
870 result
[0] = result
[1] = result
[2] = result
[3] = LOG2(a
[0]);
871 store_vector4( inst
, machine
, result
);
876 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
877 GLfloat a
[4], result
[4];
878 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
879 a
[0] = MAX2(a
[0], 0.0F
);
880 a
[1] = MAX2(a
[1], 0.0F
);
881 /* XXX ARB version clamps a[3], NV version doesn't */
882 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
885 /* XXX we could probably just use pow() here */
887 if (a
[1] == 0.0 && a
[3] == 0.0)
890 result
[2] = EXPF(a
[3] * LOGF(a
[1]));
896 store_vector4( inst
, machine
, result
);
898 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
899 result
[0], result
[1], result
[2], result
[3],
900 a
[0], a
[1], a
[2], a
[3]);
906 GLfloat a
[4], b
[4], c
[4], result
[4];
907 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
908 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
909 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
910 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
911 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
912 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
913 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
914 store_vector4( inst
, machine
, result
);
916 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
917 "(%g %g %g %g), (%g %g %g %g)\n",
918 result
[0], result
[1], result
[2], result
[3],
919 a
[0], a
[1], a
[2], a
[3],
920 b
[0], b
[1], b
[2], b
[3],
921 c
[0], c
[1], c
[2], c
[3]);
927 GLfloat a
[4], b
[4], c
[4], result
[4];
928 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
929 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
930 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
931 result
[0] = a
[0] * b
[0] + c
[0];
932 result
[1] = a
[1] * b
[1] + c
[1];
933 result
[2] = a
[2] * b
[2] + c
[2];
934 result
[3] = a
[3] * b
[3] + c
[3];
935 store_vector4( inst
, machine
, result
);
937 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
938 "(%g %g %g %g) + (%g %g %g %g)\n",
939 result
[0], result
[1], result
[2], result
[3],
940 a
[0], a
[1], a
[2], a
[3],
941 b
[0], b
[1], b
[2], b
[3],
942 c
[0], c
[1], c
[2], c
[3]);
948 GLfloat a
[4], b
[4], result
[4];
949 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
950 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
951 result
[0] = MAX2(a
[0], b
[0]);
952 result
[1] = MAX2(a
[1], b
[1]);
953 result
[2] = MAX2(a
[2], b
[2]);
954 result
[3] = MAX2(a
[3], b
[3]);
955 store_vector4( inst
, machine
, result
);
957 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
958 result
[0], result
[1], result
[2], result
[3],
959 a
[0], a
[1], a
[2], a
[3],
960 b
[0], b
[1], b
[2], b
[3]);
966 GLfloat a
[4], b
[4], result
[4];
967 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
968 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
969 result
[0] = MIN2(a
[0], b
[0]);
970 result
[1] = MIN2(a
[1], b
[1]);
971 result
[2] = MIN2(a
[2], b
[2]);
972 result
[3] = MIN2(a
[3], b
[3]);
973 store_vector4( inst
, machine
, result
);
979 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, result
);
980 store_vector4( inst
, machine
, result
);
982 printf("MOV (%g %g %g %g)\n",
983 result
[0], result
[1], result
[2], result
[3]);
989 GLfloat a
[4], b
[4], result
[4];
990 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
991 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
992 result
[0] = a
[0] * b
[0];
993 result
[1] = a
[1] * b
[1];
994 result
[2] = a
[2] * b
[2];
995 result
[3] = a
[3] * b
[3];
996 store_vector4( inst
, machine
, result
);
998 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
999 result
[0], result
[1], result
[2], result
[3],
1000 a
[0], a
[1], a
[2], a
[3],
1001 b
[0], b
[1], b
[2], b
[3]);
1005 case OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
1007 GLfloat a
[4], result
[4];
1009 GLuint
*rawResult
= (GLuint
*) result
;
1011 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1012 hx
= _mesa_float_to_half(a
[0]);
1013 hy
= _mesa_float_to_half(a
[1]);
1014 twoHalves
= hx
| (hy
<< 16);
1015 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1017 store_vector4( inst
, machine
, result
);
1020 case OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
1022 GLfloat a
[4], result
[4];
1023 GLuint usx
, usy
, *rawResult
= (GLuint
*) result
;
1024 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1025 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1026 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1027 usx
= IROUND(a
[0] * 65535.0F
);
1028 usy
= IROUND(a
[1] * 65535.0F
);
1029 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1030 = usx
| (usy
<< 16);
1031 store_vector4( inst
, machine
, result
);
1034 case OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
1036 GLfloat a
[4], result
[4];
1037 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
1038 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1039 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
1040 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
1041 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
1042 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
1043 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
1044 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
1045 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
1046 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
1047 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1048 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1049 store_vector4( inst
, machine
, result
);
1052 case OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
1054 GLfloat a
[4], result
[4];
1055 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
1056 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1057 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1058 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1059 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
1060 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
1061 ubx
= IROUND(255.0F
* a
[0]);
1062 uby
= IROUND(255.0F
* a
[1]);
1063 ubz
= IROUND(255.0F
* a
[2]);
1064 ubw
= IROUND(255.0F
* a
[3]);
1065 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1066 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1067 store_vector4( inst
, machine
, result
);
1072 GLfloat a
[4], b
[4], result
[4];
1073 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1074 fetch_vector1( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1075 result
[0] = result
[1] = result
[2] = result
[3]
1076 = (GLfloat
)_mesa_pow(a
[0], b
[0]);
1077 store_vector4( inst
, machine
, result
);
1082 GLfloat a
[4], result
[4];
1083 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1087 else if (IS_INF_OR_NAN(a
[0]))
1088 printf("RCP(inf)\n");
1090 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
1091 store_vector4( inst
, machine
, result
);
1094 case OPCODE_RFL
: /* reflection vector */
1096 GLfloat axis
[4], dir
[4], result
[4], tmpX
, tmpW
;
1097 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, axis
);
1098 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, dir
);
1099 tmpW
= DOT3(axis
, axis
);
1100 tmpX
= (2.0F
* DOT3(axis
, dir
)) / tmpW
;
1101 result
[0] = tmpX
* axis
[0] - dir
[0];
1102 result
[1] = tmpX
* axis
[1] - dir
[1];
1103 result
[2] = tmpX
* axis
[2] - dir
[2];
1104 /* result[3] is never written! XXX enforce in parser! */
1105 store_vector4( inst
, machine
, result
);
1108 case OPCODE_RSQ
: /* 1 / sqrt() */
1110 GLfloat a
[4], result
[4];
1111 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1113 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1114 store_vector4( inst
, machine
, result
);
1116 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
1120 case OPCODE_SCS
: /* sine and cos */
1122 GLfloat a
[4], result
[4];
1123 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1124 result
[0] = (GLfloat
)_mesa_cos(a
[0]);
1125 result
[1] = (GLfloat
)_mesa_sin(a
[0]);
1126 result
[2] = 0.0; /* undefined! */
1127 result
[3] = 0.0; /* undefined! */
1128 store_vector4( inst
, machine
, result
);
1131 case OPCODE_SEQ
: /* set on equal */
1133 GLfloat a
[4], b
[4], result
[4];
1134 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1135 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1136 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1137 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1138 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1139 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1140 store_vector4( inst
, machine
, result
);
1143 case OPCODE_SFL
: /* set false, operands ignored */
1145 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1146 store_vector4( inst
, machine
, result
);
1149 case OPCODE_SGE
: /* set on greater or equal */
1151 GLfloat a
[4], b
[4], result
[4];
1152 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1153 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1154 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1155 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1156 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1157 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1158 store_vector4( inst
, machine
, result
);
1161 case OPCODE_SGT
: /* set on greater */
1163 GLfloat a
[4], b
[4], result
[4];
1164 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1165 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1166 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1167 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1168 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1169 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1170 store_vector4( inst
, machine
, result
);
1175 GLfloat a
[4], result
[4];
1176 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1177 result
[0] = result
[1] = result
[2] = result
[3]
1178 = (GLfloat
) _mesa_sin(a
[0]);
1179 store_vector4( inst
, machine
, result
);
1182 case OPCODE_SLE
: /* set on less or equal */
1184 GLfloat a
[4], b
[4], result
[4];
1185 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1186 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1187 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1188 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1189 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1190 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1191 store_vector4( inst
, machine
, result
);
1194 case OPCODE_SLT
: /* set on less */
1196 GLfloat a
[4], b
[4], result
[4];
1197 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1198 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1199 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1200 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1201 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1202 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1203 store_vector4( inst
, machine
, result
);
1206 case OPCODE_SNE
: /* set on not equal */
1208 GLfloat a
[4], b
[4], result
[4];
1209 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1210 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1211 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1212 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1213 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1214 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1215 store_vector4( inst
, machine
, result
);
1218 case OPCODE_STR
: /* set true, operands ignored */
1220 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1221 store_vector4( inst
, machine
, result
);
1226 GLfloat a
[4], b
[4], result
[4];
1227 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1228 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1229 result
[0] = a
[0] - b
[0];
1230 result
[1] = a
[1] - b
[1];
1231 result
[2] = a
[2] - b
[2];
1232 result
[3] = a
[3] - b
[3];
1233 store_vector4( inst
, machine
, result
);
1235 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1236 result
[0], result
[1], result
[2], result
[3],
1237 a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
1241 case OPCODE_SWZ
: /* extended swizzle */
1243 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1244 const GLfloat
*src
= get_register_pointer(ctx
, source
,
1248 for (i
= 0; i
< 4; i
++) {
1249 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1250 if (swz
== SWIZZLE_ZERO
)
1252 else if (swz
== SWIZZLE_ONE
)
1257 result
[i
] = src
[swz
];
1259 if (source
->NegateBase
& (1 << i
))
1260 result
[i
] = -result
[i
];
1262 store_vector4( inst
, machine
, result
);
1265 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1268 /* Note: only use the precomputed lambda value when we're
1269 * sampling texture unit [K] with texcoord[K].
1270 * Otherwise, the lambda value may have no relation to the
1271 * instruction's texcoord or texture image. Using the wrong
1272 * lambda is usually bad news.
1273 * The rest of the time, just use zero (until we get a more
1274 * sophisticated way of computing lambda).
1276 GLfloat coord
[4], color
[4], lambda
;
1277 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
1278 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+inst
->TexSrcUnit
)
1279 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
];
1282 fetch_vector4(ctx
, &inst
->SrcReg
[0], machine
, program
, coord
);
1283 fetch_texel( ctx
, coord
, lambda
, inst
->TexSrcUnit
, color
);
1285 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g], "
1287 color
[0], color
[1], color
[2], color
[3], inst
->TexSrcUnit
,
1288 coord
[0], coord
[1], coord
[2], coord
[3], lambda
);
1290 store_vector4( inst
, machine
, color
);
1293 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1294 /* Texel lookup with LOD bias */
1296 GLfloat coord
[4], color
[4], lambda
, bias
;
1297 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
1298 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+inst
->TexSrcUnit
)
1299 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
];
1302 fetch_vector4(ctx
, &inst
->SrcReg
[0], machine
, program
, coord
);
1303 /* coord[3] is the bias to add to lambda */
1304 bias
= ctx
->Texture
.Unit
[inst
->TexSrcUnit
].LodBias
1305 + ctx
->Texture
.Unit
[inst
->TexSrcUnit
]._Current
->LodBias
1307 fetch_texel(ctx
, coord
, lambda
+ bias
, inst
->TexSrcUnit
, color
);
1308 store_vector4( inst
, machine
, color
);
1311 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1312 /* Texture lookup w/ partial derivatives for LOD */
1314 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1315 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1316 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, dtdx
);
1317 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, dtdy
);
1318 fetch_texel_deriv( ctx
, texcoord
, dtdx
, dtdy
, inst
->TexSrcUnit
,
1320 store_vector4( inst
, machine
, color
);
1323 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1324 /* Texture lookup w/ projective divide */
1326 GLfloat texcoord
[4], color
[4], lambda
;
1327 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
1328 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+inst
->TexSrcUnit
)
1329 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
];
1332 fetch_vector4(ctx
, &inst
->SrcReg
[0], machine
, program
,texcoord
);
1333 /* Not so sure about this test - if texcoord[3] is
1334 * zero, we'd probably be fine except for an ASSERT in
1335 * IROUND_POS() which gets triggered by the inf values created.
1337 if (texcoord
[3] != 0.0) {
1338 texcoord
[0] /= texcoord
[3];
1339 texcoord
[1] /= texcoord
[3];
1340 texcoord
[2] /= texcoord
[3];
1342 fetch_texel( ctx
, texcoord
, lambda
, inst
->TexSrcUnit
, color
);
1343 store_vector4( inst
, machine
, color
);
1346 case OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1347 /* Texture lookup w/ projective divide */
1349 GLfloat texcoord
[4], color
[4], lambda
;
1350 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
1351 inst
->SrcReg
[0].Index
== FRAG_ATTRIB_TEX0
+inst
->TexSrcUnit
)
1352 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
];
1355 fetch_vector4(ctx
, &inst
->SrcReg
[0], machine
, program
,texcoord
);
1356 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1357 texcoord
[3] != 0.0) {
1358 texcoord
[0] /= texcoord
[3];
1359 texcoord
[1] /= texcoord
[3];
1360 texcoord
[2] /= texcoord
[3];
1362 fetch_texel( ctx
, texcoord
, lambda
, inst
->TexSrcUnit
, color
);
1363 store_vector4( inst
, machine
, color
);
1366 case OPCODE_UP2H
: /* unpack two 16-bit floats */
1368 GLfloat a
[4], result
[4];
1369 const GLuint
*rawBits
= (const GLuint
*) a
;
1371 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1372 hx
= rawBits
[0] & 0xffff;
1373 hy
= rawBits
[0] >> 16;
1374 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1375 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1376 store_vector4( inst
, machine
, result
);
1379 case OPCODE_UP2US
: /* unpack two GLushorts */
1381 GLfloat a
[4], result
[4];
1382 const GLuint
*rawBits
= (const GLuint
*) a
;
1384 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1385 usx
= rawBits
[0] & 0xffff;
1386 usy
= rawBits
[0] >> 16;
1387 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1388 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1389 store_vector4( inst
, machine
, result
);
1392 case OPCODE_UP4B
: /* unpack four GLbytes */
1394 GLfloat a
[4], result
[4];
1395 const GLuint
*rawBits
= (const GLuint
*) a
;
1396 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1397 result
[0] = (((rawBits
[0] >> 0) & 0xff) - 128) / 127.0F
;
1398 result
[1] = (((rawBits
[0] >> 8) & 0xff) - 128) / 127.0F
;
1399 result
[2] = (((rawBits
[0] >> 16) & 0xff) - 128) / 127.0F
;
1400 result
[3] = (((rawBits
[0] >> 24) & 0xff) - 128) / 127.0F
;
1401 store_vector4( inst
, machine
, result
);
1404 case OPCODE_UP4UB
: /* unpack four GLubytes */
1406 GLfloat a
[4], result
[4];
1407 const GLuint
*rawBits
= (const GLuint
*) a
;
1408 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1409 result
[0] = ((rawBits
[0] >> 0) & 0xff) / 255.0F
;
1410 result
[1] = ((rawBits
[0] >> 8) & 0xff) / 255.0F
;
1411 result
[2] = ((rawBits
[0] >> 16) & 0xff) / 255.0F
;
1412 result
[3] = ((rawBits
[0] >> 24) & 0xff) / 255.0F
;
1413 store_vector4( inst
, machine
, result
);
1416 case OPCODE_XPD
: /* cross product */
1418 GLfloat a
[4], b
[4], result
[4];
1419 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1420 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1421 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1422 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1423 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1425 store_vector4( inst
, machine
, result
);
1428 case OPCODE_X2D
: /* 2-D matrix transform */
1430 GLfloat a
[4], b
[4], c
[4], result
[4];
1431 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1432 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1433 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
1434 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1435 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1436 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1437 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1438 store_vector4( inst
, machine
, result
);
1443 if (inst
->SrcReg
[0].File
!= -1) {
1445 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1446 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst
->Data
,
1447 a
[0], a
[1], a
[2], a
[3]);
1450 _mesa_printf("%s\n", (const char *) inst
->Data
);
1457 _mesa_problem(ctx
, "Bad opcode %d in _mesa_exec_fragment_program",
1459 return GL_TRUE
; /* return value doesn't matter */
1467 * Initialize the virtual fragment program machine state prior to running
1468 * fragment program on a fragment. This involves initializing the input
1469 * registers, condition codes, etc.
1470 * \param machine the virtual machine state to init
1471 * \param program the fragment program we're about to run
1472 * \param span the span of pixels we'll operate on
1473 * \param col which element (column) of the span we'll operate on
1476 init_machine( GLcontext
*ctx
, struct fp_machine
*machine
,
1477 const struct gl_fragment_program
*program
,
1478 const SWspan
*span
, GLuint col
)
1480 GLuint inputsRead
= program
->Base
.InputsRead
;
1483 if (ctx
->FragmentProgram
.CallbackEnabled
)
1486 if (program
->Base
.Target
== GL_FRAGMENT_PROGRAM_NV
) {
1487 /* Clear temporary registers (undefined for ARB_f_p) */
1488 _mesa_bzero(machine
->Temporaries
,
1489 MAX_NV_FRAGMENT_PROGRAM_TEMPS
* 4 * sizeof(GLfloat
));
1492 /* Load input registers */
1493 if (inputsRead
& (1 << FRAG_ATTRIB_WPOS
)) {
1494 GLfloat
*wpos
= machine
->Inputs
[FRAG_ATTRIB_WPOS
];
1495 ASSERT(span
->arrayMask
& SPAN_Z
);
1496 if (span
->arrayMask
& SPAN_XY
) {
1497 wpos
[0] = (GLfloat
) span
->array
->x
[col
];
1498 wpos
[1] = (GLfloat
) span
->array
->y
[col
];
1501 wpos
[0] = (GLfloat
) span
->x
+ col
;
1502 wpos
[1] = (GLfloat
) span
->y
;
1504 wpos
[2] = (GLfloat
) span
->array
->z
[col
] / ctx
->DrawBuffer
->_DepthMaxF
;
1505 wpos
[3] = span
->w
+ col
* span
->dwdx
;
1507 if (inputsRead
& (1 << FRAG_ATTRIB_COL0
)) {
1508 ASSERT(span
->arrayMask
& SPAN_RGBA
);
1509 COPY_4V(machine
->Inputs
[FRAG_ATTRIB_COL0
],
1510 span
->array
->color
.sz4
.rgba
[col
]);
1512 if (inputsRead
& (1 << FRAG_ATTRIB_COL1
)) {
1513 ASSERT(span
->arrayMask
& SPAN_SPEC
);
1514 COPY_4V(machine
->Inputs
[FRAG_ATTRIB_COL1
],
1515 span
->array
->color
.sz4
.spec
[col
]);
1517 if (inputsRead
& (1 << FRAG_ATTRIB_FOGC
)) {
1518 GLfloat
*fogc
= machine
->Inputs
[FRAG_ATTRIB_FOGC
];
1519 ASSERT(span
->arrayMask
& SPAN_FOG
);
1520 fogc
[0] = span
->array
->fog
[col
];
1525 for (u
= 0; u
< ctx
->Const
.MaxTextureCoordUnits
; u
++) {
1526 if (inputsRead
& (1 << (FRAG_ATTRIB_TEX0
+ u
))) {
1527 GLfloat
*tex
= machine
->Inputs
[FRAG_ATTRIB_TEX0
+ u
];
1528 /*ASSERT(ctx->Texture._EnabledCoordUnits & (1 << u));*/
1529 COPY_4V(tex
, span
->array
->texcoords
[u
][col
]);
1530 /*ASSERT(tex[0] != 0 || tex[1] != 0 || tex[2] != 0);*/
1534 /* init condition codes */
1535 machine
->CondCodes
[0] = COND_EQ
;
1536 machine
->CondCodes
[1] = COND_EQ
;
1537 machine
->CondCodes
[2] = COND_EQ
;
1538 machine
->CondCodes
[3] = COND_EQ
;
1543 * Run fragment program on the pixels in span from 'start' to 'end' - 1.
1546 run_program(GLcontext
*ctx
, SWspan
*span
, GLuint start
, GLuint end
)
1548 const struct gl_fragment_program
*program
= ctx
->FragmentProgram
._Current
;
1549 struct fp_machine machine
;
1552 CurrentMachine
= &machine
;
1554 for (i
= start
; i
< end
; i
++) {
1555 if (span
->array
->mask
[i
]) {
1556 init_machine(ctx
, &machine
, program
, span
, i
);
1558 if (execute_program(ctx
, program
, ~0, &machine
, span
, i
)) {
1559 /* Store result color */
1560 COPY_4V(span
->array
->color
.sz4
.rgba
[i
],
1561 machine
.Outputs
[FRAG_RESULT_COLR
]);
1563 /* Store result depth/z */
1564 if (program
->Base
.OutputsWritten
& (1 << FRAG_RESULT_DEPR
)) {
1565 const GLfloat depth
= machine
.Outputs
[FRAG_RESULT_DEPR
][2];
1567 span
->array
->z
[i
] = 0;
1568 else if (depth
>= 1.0)
1569 span
->array
->z
[i
] = ctx
->DrawBuffer
->_DepthMax
;
1571 span
->array
->z
[i
] = IROUND(depth
* ctx
->DrawBuffer
->_DepthMaxF
);
1575 /* killed fragment */
1576 span
->array
->mask
[i
] = GL_FALSE
;
1577 span
->writeAll
= GL_FALSE
;
1582 CurrentMachine
= NULL
;
1587 * Execute the current fragment program for all the fragments
1588 * in the given span.
1591 _swrast_exec_fragment_program( GLcontext
*ctx
, SWspan
*span
)
1593 const struct gl_fragment_program
*program
= ctx
->FragmentProgram
._Current
;
1595 /* incoming colors should be floats */
1596 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
1598 ctx
->_CurrentProgram
= GL_FRAGMENT_PROGRAM_ARB
; /* or NV, doesn't matter */
1600 run_program(ctx
, span
, 0, span
->end
);
1602 if (program
->Base
.OutputsWritten
& (1 << FRAG_RESULT_DEPR
)) {
1603 span
->interpMask
&= ~SPAN_Z
;
1604 span
->arrayMask
|= SPAN_Z
;
1607 ctx
->_CurrentProgram
= 0;