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 /* if 1, print some debugging info */
49 * Virtual machine state used during execution of a fragment programs.
53 GLfloat Temporaries
[MAX_NV_FRAGMENT_PROGRAM_TEMPS
][4];
54 GLfloat Inputs
[MAX_NV_FRAGMENT_PROGRAM_INPUTS
][4];
55 GLfloat Outputs
[MAX_NV_FRAGMENT_PROGRAM_OUTPUTS
][4];
56 GLuint CondCodes
[4]; /**< COND_* value for x/y/z/w */
60 #if FEATURE_MESA_program_debug
61 static struct fp_machine
*CurrentMachine
= NULL
;
64 * For GL_MESA_program_debug.
65 * Return current value (4*GLfloat) of a fragment program register.
66 * Called via ctx->Driver.GetFragmentProgramRegister().
69 _swrast_get_program_register(GLcontext
*ctx
, enum register_file file
,
70 GLuint index
, GLfloat val
[4])
75 COPY_4V(val
, CurrentMachine
->Inputs
[index
]);
78 COPY_4V(val
, CurrentMachine
->Outputs
[index
]);
80 case PROGRAM_TEMPORARY
:
81 COPY_4V(val
, CurrentMachine
->Temporaries
[index
]);
85 "bad register file in _swrast_get_program_register");
89 #endif /* FEATURE_MESA_program_debug */
96 fetch_texel( GLcontext
*ctx
, const GLfloat texcoord
[4], GLfloat lambda
,
97 GLuint unit
, GLfloat color
[4] )
100 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
102 /* XXX use a float-valued TextureSample routine here!!! */
103 swrast
->TextureSample
[unit
](ctx
, ctx
->Texture
.Unit
[unit
]._Current
,
104 1, (const GLfloat (*)[4]) texcoord
,
106 color
[0] = CHAN_TO_FLOAT(rgba
[0]);
107 color
[1] = CHAN_TO_FLOAT(rgba
[1]);
108 color
[2] = CHAN_TO_FLOAT(rgba
[2]);
109 color
[3] = CHAN_TO_FLOAT(rgba
[3]);
114 * Fetch a texel with the given partial derivatives to compute a level
115 * of detail in the mipmap.
118 fetch_texel_deriv( GLcontext
*ctx
, const GLfloat texcoord
[4],
119 const GLfloat texdx
[4], const GLfloat texdy
[4],
120 GLuint unit
, GLfloat color
[4] )
122 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
123 const struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[unit
]._Current
;
124 const struct gl_texture_image
*texImg
= texObj
->Image
[0][texObj
->BaseLevel
];
125 const GLfloat texW
= (GLfloat
) texImg
->WidthScale
;
126 const GLfloat texH
= (GLfloat
) texImg
->HeightScale
;
129 GLfloat lambda
= _swrast_compute_lambda(texdx
[0], texdy
[0], /* ds/dx, ds/dy */
130 texdx
[1], texdy
[1], /* dt/dx, dt/dy */
131 texdx
[3], texdy
[2], /* dq/dx, dq/dy */
133 texcoord
[0], texcoord
[1], texcoord
[3],
136 swrast
->TextureSample
[unit
](ctx
, ctx
->Texture
.Unit
[unit
]._Current
,
137 1, (const GLfloat (*)[4]) texcoord
,
139 color
[0] = CHAN_TO_FLOAT(rgba
[0]);
140 color
[1] = CHAN_TO_FLOAT(rgba
[1]);
141 color
[2] = CHAN_TO_FLOAT(rgba
[2]);
142 color
[3] = CHAN_TO_FLOAT(rgba
[3]);
147 * Return a pointer to the 4-element float vector specified by the given
150 static INLINE
const GLfloat
*
151 get_register_pointer( GLcontext
*ctx
,
152 const struct prog_src_register
*source
,
153 const struct fp_machine
*machine
,
154 const struct gl_fragment_program
*program
)
156 switch (source
->File
) {
157 case PROGRAM_TEMPORARY
:
158 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_TEMPS
);
159 return machine
->Temporaries
[source
->Index
];
161 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_INPUTS
);
162 return machine
->Inputs
[source
->Index
];
164 /* This is only for PRINT */
165 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_OUTPUTS
);
166 return machine
->Outputs
[source
->Index
];
167 case PROGRAM_LOCAL_PARAM
:
168 ASSERT(source
->Index
< MAX_PROGRAM_LOCAL_PARAMS
);
169 return program
->Base
.LocalParams
[source
->Index
];
170 case PROGRAM_ENV_PARAM
:
171 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_PARAMS
);
172 return ctx
->FragmentProgram
.Parameters
[source
->Index
];
173 case PROGRAM_STATE_VAR
:
175 case PROGRAM_NAMED_PARAM
:
176 ASSERT(source
->Index
< (GLint
) program
->Base
.Parameters
->NumParameters
);
177 return program
->Base
.Parameters
->ParameterValues
[source
->Index
];
179 _mesa_problem(ctx
, "Invalid input register file %d in fetch_vector4",
187 * Fetch a 4-element float vector from the given source register.
188 * Apply swizzling and negating as needed.
191 fetch_vector4( GLcontext
*ctx
,
192 const struct prog_src_register
*source
,
193 const struct fp_machine
*machine
,
194 const struct gl_fragment_program
*program
,
197 const GLfloat
*src
= get_register_pointer(ctx
, source
, machine
, program
);
200 if (source
->Swizzle
== MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
,
201 SWIZZLE_Z
, SWIZZLE_W
)) {
203 COPY_4V(result
, src
);
206 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
207 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
208 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
209 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
212 if (source
->NegateBase
) {
213 result
[0] = -result
[0];
214 result
[1] = -result
[1];
215 result
[2] = -result
[2];
216 result
[3] = -result
[3];
219 result
[0] = FABSF(result
[0]);
220 result
[1] = FABSF(result
[1]);
221 result
[2] = FABSF(result
[2]);
222 result
[3] = FABSF(result
[3]);
224 if (source
->NegateAbs
) {
225 result
[0] = -result
[0];
226 result
[1] = -result
[1];
227 result
[2] = -result
[2];
228 result
[3] = -result
[3];
234 * Fetch the derivative with respect to X for the given register.
235 * \return GL_TRUE if it was easily computed or GL_FALSE if we
236 * need to execute another instance of the program (ugh)!
239 fetch_vector4_deriv( GLcontext
*ctx
,
240 const struct prog_src_register
*source
,
242 char xOrY
, GLint column
, GLfloat result
[4] )
246 ASSERT(xOrY
== 'X' || xOrY
== 'Y');
248 switch (source
->Index
) {
249 case FRAG_ATTRIB_WPOS
:
253 src
[2] = span
->dzdx
/ ctx
->DrawBuffer
->_DepthMaxF
;
259 src
[2] = span
->dzdy
/ ctx
->DrawBuffer
->_DepthMaxF
;
263 case FRAG_ATTRIB_COL0
:
265 src
[0] = span
->drdx
* (1.0F
/ CHAN_MAXF
);
266 src
[1] = span
->dgdx
* (1.0F
/ CHAN_MAXF
);
267 src
[2] = span
->dbdx
* (1.0F
/ CHAN_MAXF
);
268 src
[3] = span
->dadx
* (1.0F
/ CHAN_MAXF
);
271 src
[0] = span
->drdy
* (1.0F
/ CHAN_MAXF
);
272 src
[1] = span
->dgdy
* (1.0F
/ CHAN_MAXF
);
273 src
[2] = span
->dbdy
* (1.0F
/ CHAN_MAXF
);
274 src
[3] = span
->dady
* (1.0F
/ CHAN_MAXF
);
277 case FRAG_ATTRIB_COL1
:
279 src
[0] = span
->dsrdx
* (1.0F
/ CHAN_MAXF
);
280 src
[1] = span
->dsgdx
* (1.0F
/ CHAN_MAXF
);
281 src
[2] = span
->dsbdx
* (1.0F
/ CHAN_MAXF
);
282 src
[3] = 0.0; /* XXX need this */
285 src
[0] = span
->dsrdy
* (1.0F
/ CHAN_MAXF
);
286 src
[1] = span
->dsgdy
* (1.0F
/ CHAN_MAXF
);
287 src
[2] = span
->dsbdy
* (1.0F
/ CHAN_MAXF
);
288 src
[3] = 0.0; /* XXX need this */
291 case FRAG_ATTRIB_FOGC
:
293 src
[0] = span
->dfogdx
;
299 src
[0] = span
->dfogdy
;
305 case FRAG_ATTRIB_TEX0
:
306 case FRAG_ATTRIB_TEX1
:
307 case FRAG_ATTRIB_TEX2
:
308 case FRAG_ATTRIB_TEX3
:
309 case FRAG_ATTRIB_TEX4
:
310 case FRAG_ATTRIB_TEX5
:
311 case FRAG_ATTRIB_TEX6
:
312 case FRAG_ATTRIB_TEX7
:
314 const GLuint u
= source
->Index
- FRAG_ATTRIB_TEX0
;
315 /* this is a little tricky - I think I've got it right */
316 const GLfloat invQ
= 1.0f
/ (span
->tex
[u
][3]
317 + span
->texStepX
[u
][3] * column
);
318 src
[0] = span
->texStepX
[u
][0] * invQ
;
319 src
[1] = span
->texStepX
[u
][1] * invQ
;
320 src
[2] = span
->texStepX
[u
][2] * invQ
;
321 src
[3] = span
->texStepX
[u
][3] * invQ
;
324 const GLuint u
= source
->Index
- FRAG_ATTRIB_TEX0
;
325 /* Tricky, as above, but in Y direction */
326 const GLfloat invQ
= 1.0f
/ (span
->tex
[u
][3] + span
->texStepY
[u
][3]);
327 src
[0] = span
->texStepY
[u
][0] * invQ
;
328 src
[1] = span
->texStepY
[u
][1] * invQ
;
329 src
[2] = span
->texStepY
[u
][2] * invQ
;
330 src
[3] = span
->texStepY
[u
][3] * invQ
;
337 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
338 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
339 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
340 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
342 if (source
->NegateBase
) {
343 result
[0] = -result
[0];
344 result
[1] = -result
[1];
345 result
[2] = -result
[2];
346 result
[3] = -result
[3];
349 result
[0] = FABSF(result
[0]);
350 result
[1] = FABSF(result
[1]);
351 result
[2] = FABSF(result
[2]);
352 result
[3] = FABSF(result
[3]);
354 if (source
->NegateAbs
) {
355 result
[0] = -result
[0];
356 result
[1] = -result
[1];
357 result
[2] = -result
[2];
358 result
[3] = -result
[3];
365 * As above, but only return result[0] element.
368 fetch_vector1( GLcontext
*ctx
,
369 const struct prog_src_register
*source
,
370 const struct fp_machine
*machine
,
371 const struct gl_fragment_program
*program
,
374 const GLfloat
*src
= get_register_pointer(ctx
, source
, machine
, program
);
377 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
379 if (source
->NegateBase
) {
380 result
[0] = -result
[0];
383 result
[0] = FABSF(result
[0]);
385 if (source
->NegateAbs
) {
386 result
[0] = -result
[0];
392 * Test value against zero and return GT, LT, EQ or UN if NaN.
395 generate_cc( float value
)
398 return COND_UN
; /* NaN */
408 * Test if the ccMaskRule is satisfied by the given condition code.
409 * Used to mask destination writes according to the current condition code.
411 static INLINE GLboolean
412 test_cc(GLuint condCode
, GLuint ccMaskRule
)
414 switch (ccMaskRule
) {
415 case COND_EQ
: return (condCode
== COND_EQ
);
416 case COND_NE
: return (condCode
!= COND_EQ
);
417 case COND_LT
: return (condCode
== COND_LT
);
418 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
419 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
420 case COND_GT
: return (condCode
== COND_GT
);
421 case COND_TR
: return GL_TRUE
;
422 case COND_FL
: return GL_FALSE
;
423 default: return GL_TRUE
;
429 * Store 4 floats into a register. Observe the instructions saturate and
430 * set-condition-code flags.
433 store_vector4( const struct prog_instruction
*inst
,
434 struct fp_machine
*machine
,
435 const GLfloat value
[4] )
437 const struct prog_dst_register
*dest
= &(inst
->DstReg
);
438 const GLboolean clamp
= inst
->SaturateMode
== SATURATE_ZERO_ONE
;
441 GLfloat clampedValue
[4];
442 GLuint writeMask
= dest
->WriteMask
;
444 switch (dest
->File
) {
446 dstReg
= machine
->Outputs
[dest
->Index
];
448 case PROGRAM_TEMPORARY
:
449 dstReg
= machine
->Temporaries
[dest
->Index
];
451 case PROGRAM_WRITE_ONLY
:
455 _mesa_problem(NULL
, "bad register file in store_vector4(fp)");
460 if (value
[0] > 1.0e10
||
461 IS_INF_OR_NAN(value
[0]) ||
462 IS_INF_OR_NAN(value
[1]) ||
463 IS_INF_OR_NAN(value
[2]) ||
464 IS_INF_OR_NAN(value
[3]) )
465 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
469 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
470 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
471 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
472 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
473 value
= clampedValue
;
476 if (dest
->CondMask
!= COND_TR
) {
477 /* condition codes may turn off some writes */
478 if (writeMask
& WRITEMASK_X
) {
479 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 0)],
481 writeMask
&= ~WRITEMASK_X
;
483 if (writeMask
& WRITEMASK_Y
) {
484 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 1)],
486 writeMask
&= ~WRITEMASK_Y
;
488 if (writeMask
& WRITEMASK_Z
) {
489 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 2)],
491 writeMask
&= ~WRITEMASK_Z
;
493 if (writeMask
& WRITEMASK_W
) {
494 if (!test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 3)],
496 writeMask
&= ~WRITEMASK_W
;
500 if (writeMask
& WRITEMASK_X
)
501 dstReg
[0] = value
[0];
502 if (writeMask
& WRITEMASK_Y
)
503 dstReg
[1] = value
[1];
504 if (writeMask
& WRITEMASK_Z
)
505 dstReg
[2] = value
[2];
506 if (writeMask
& WRITEMASK_W
)
507 dstReg
[3] = value
[3];
509 if (inst
->CondUpdate
) {
510 if (writeMask
& WRITEMASK_X
)
511 machine
->CondCodes
[0] = generate_cc(value
[0]);
512 if (writeMask
& WRITEMASK_Y
)
513 machine
->CondCodes
[1] = generate_cc(value
[1]);
514 if (writeMask
& WRITEMASK_Z
)
515 machine
->CondCodes
[2] = generate_cc(value
[2]);
516 if (writeMask
& WRITEMASK_W
)
517 machine
->CondCodes
[3] = generate_cc(value
[3]);
523 * Initialize a new machine state instance from an existing one, adding
524 * the partial derivatives onto the input registers.
525 * Used to implement DDX and DDY instructions in non-trivial cases.
528 init_machine_deriv( GLcontext
*ctx
,
529 const struct fp_machine
*machine
,
530 const struct gl_fragment_program
*program
,
531 const SWspan
*span
, char xOrY
,
532 struct fp_machine
*dMachine
)
536 ASSERT(xOrY
== 'X' || xOrY
== 'Y');
538 /* copy existing machine */
539 _mesa_memcpy(dMachine
, machine
, sizeof(struct fp_machine
));
541 if (program
->Base
.Target
== GL_FRAGMENT_PROGRAM_NV
) {
542 /* Clear temporary registers (undefined for ARB_f_p) */
543 _mesa_bzero( (void*) machine
->Temporaries
,
544 MAX_NV_FRAGMENT_PROGRAM_TEMPS
* 4 * sizeof(GLfloat
));
547 /* Add derivatives */
548 if (program
->Base
.InputsRead
& (1 << FRAG_ATTRIB_WPOS
)) {
549 GLfloat
*wpos
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_WPOS
];
553 wpos
[2] += span
->dzdx
;
554 wpos
[3] += span
->dwdx
;
559 wpos
[2] += span
->dzdy
;
560 wpos
[3] += span
->dwdy
;
563 if (program
->Base
.InputsRead
& (1 << FRAG_ATTRIB_COL0
)) {
564 GLfloat
*col0
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_COL0
];
566 col0
[0] += span
->drdx
* (1.0F
/ CHAN_MAXF
);
567 col0
[1] += span
->dgdx
* (1.0F
/ CHAN_MAXF
);
568 col0
[2] += span
->dbdx
* (1.0F
/ CHAN_MAXF
);
569 col0
[3] += span
->dadx
* (1.0F
/ CHAN_MAXF
);
572 col0
[0] += span
->drdy
* (1.0F
/ CHAN_MAXF
);
573 col0
[1] += span
->dgdy
* (1.0F
/ CHAN_MAXF
);
574 col0
[2] += span
->dbdy
* (1.0F
/ CHAN_MAXF
);
575 col0
[3] += span
->dady
* (1.0F
/ CHAN_MAXF
);
578 if (program
->Base
.InputsRead
& (1 << FRAG_ATTRIB_COL1
)) {
579 GLfloat
*col1
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_COL1
];
581 col1
[0] += span
->dsrdx
* (1.0F
/ CHAN_MAXF
);
582 col1
[1] += span
->dsgdx
* (1.0F
/ CHAN_MAXF
);
583 col1
[2] += span
->dsbdx
* (1.0F
/ CHAN_MAXF
);
584 col1
[3] += 0.0; /*XXX fix */
587 col1
[0] += span
->dsrdy
* (1.0F
/ CHAN_MAXF
);
588 col1
[1] += span
->dsgdy
* (1.0F
/ CHAN_MAXF
);
589 col1
[2] += span
->dsbdy
* (1.0F
/ CHAN_MAXF
);
590 col1
[3] += 0.0; /*XXX fix */
593 if (program
->Base
.InputsRead
& (1 << FRAG_ATTRIB_FOGC
)) {
594 GLfloat
*fogc
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_FOGC
];
596 fogc
[0] += span
->dfogdx
;
599 fogc
[0] += span
->dfogdy
;
602 for (u
= 0; u
< ctx
->Const
.MaxTextureCoordUnits
; u
++) {
603 if (program
->Base
.InputsRead
& (1 << (FRAG_ATTRIB_TEX0
+ u
))) {
604 GLfloat
*tex
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_TEX0
+ u
];
605 /* XXX perspective-correct interpolation */
607 tex
[0] += span
->texStepX
[u
][0];
608 tex
[1] += span
->texStepX
[u
][1];
609 tex
[2] += span
->texStepX
[u
][2];
610 tex
[3] += span
->texStepX
[u
][3];
613 tex
[0] += span
->texStepY
[u
][0];
614 tex
[1] += span
->texStepY
[u
][1];
615 tex
[2] += span
->texStepY
[u
][2];
616 tex
[3] += span
->texStepY
[u
][3];
621 /* init condition codes */
622 dMachine
->CondCodes
[0] = COND_EQ
;
623 dMachine
->CondCodes
[1] = COND_EQ
;
624 dMachine
->CondCodes
[2] = COND_EQ
;
625 dMachine
->CondCodes
[3] = COND_EQ
;
630 * Execute the given vertex program.
631 * NOTE: we do everything in single-precision floating point; we don't
632 * currently observe the single/half/fixed-precision qualifiers.
633 * \param ctx - rendering context
634 * \param program - the fragment program to execute
635 * \param machine - machine state (register file)
636 * \param maxInst - max number of instructions to execute
637 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
640 execute_program( GLcontext
*ctx
,
641 const struct gl_fragment_program
*program
, GLuint maxInst
,
642 struct fp_machine
*machine
, const SWspan
*span
,
648 printf("execute fragment program --------------------\n");
651 for (pc
= 0; pc
< maxInst
; pc
++) {
652 const struct prog_instruction
*inst
= program
->Base
.Instructions
+ pc
;
654 if (ctx
->FragmentProgram
.CallbackEnabled
&&
655 ctx
->FragmentProgram
.Callback
) {
656 ctx
->FragmentProgram
.CurrentPosition
= inst
->StringPos
;
657 ctx
->FragmentProgram
.Callback(program
->Base
.Target
,
658 ctx
->FragmentProgram
.CallbackData
);
661 switch (inst
->Opcode
) {
664 GLfloat a
[4], result
[4];
665 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
666 result
[0] = FABSF(a
[0]);
667 result
[1] = FABSF(a
[1]);
668 result
[2] = FABSF(a
[2]);
669 result
[3] = FABSF(a
[3]);
670 store_vector4( inst
, machine
, result
);
675 GLfloat a
[4], b
[4], result
[4];
676 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
677 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
678 result
[0] = a
[0] + b
[0];
679 result
[1] = a
[1] + b
[1];
680 result
[2] = a
[2] + b
[2];
681 result
[3] = a
[3] + b
[3];
682 store_vector4( inst
, machine
, result
);
687 GLfloat a
[4], b
[4], c
[4], result
[4];
688 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
689 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
690 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
691 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
692 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
693 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
694 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
695 store_vector4( inst
, machine
, result
);
700 GLfloat a
[4], result
[4];
701 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
702 result
[0] = result
[1] = result
[2] = result
[3]
703 = (GLfloat
) _mesa_cos(a
[0]);
704 store_vector4( inst
, machine
, result
);
707 case OPCODE_DDX
: /* Partial derivative with respect to X */
709 GLfloat a
[4], aNext
[4], result
[4];
710 struct fp_machine dMachine
;
711 if (!fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], span
, 'X',
713 /* This is tricky. Make a copy of the current machine state,
714 * increment the input registers by the dx or dy partial
715 * derivatives, then re-execute the program up to the
716 * preceeding instruction, then fetch the source register.
717 * Finally, find the difference in the register values for
718 * the original and derivative runs.
720 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
721 init_machine_deriv(ctx
, machine
, program
, span
,
723 execute_program(ctx
, program
, pc
, &dMachine
, span
, column
);
724 fetch_vector4( ctx
, &inst
->SrcReg
[0], &dMachine
, program
, aNext
);
725 result
[0] = aNext
[0] - a
[0];
726 result
[1] = aNext
[1] - a
[1];
727 result
[2] = aNext
[2] - a
[2];
728 result
[3] = aNext
[3] - a
[3];
730 store_vector4( inst
, machine
, result
);
733 case OPCODE_DDY
: /* Partial derivative with respect to Y */
735 GLfloat a
[4], aNext
[4], result
[4];
736 struct fp_machine dMachine
;
737 if (!fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], span
, 'Y',
739 init_machine_deriv(ctx
, machine
, program
, span
,
741 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
742 execute_program(ctx
, program
, pc
, &dMachine
, span
, column
);
743 fetch_vector4( ctx
, &inst
->SrcReg
[0], &dMachine
, program
, aNext
);
744 result
[0] = aNext
[0] - a
[0];
745 result
[1] = aNext
[1] - a
[1];
746 result
[2] = aNext
[2] - a
[2];
747 result
[3] = aNext
[3] - a
[3];
749 store_vector4( inst
, machine
, result
);
754 GLfloat a
[4], b
[4], result
[4];
755 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
756 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
757 result
[0] = result
[1] = result
[2] = result
[3] = DOT3(a
, b
);
758 store_vector4( inst
, machine
, result
);
760 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
761 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
767 GLfloat a
[4], b
[4], result
[4];
768 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
769 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
770 result
[0] = result
[1] = result
[2] = result
[3] = DOT4(a
,b
);
771 store_vector4( inst
, machine
, result
);
773 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
774 result
[0], a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
780 GLfloat a
[4], b
[4], result
[4];
781 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
782 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
783 result
[0] = result
[1] = result
[2] = result
[3] =
784 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + b
[3];
785 store_vector4( inst
, machine
, result
);
788 case OPCODE_DST
: /* Distance vector */
790 GLfloat a
[4], b
[4], result
[4];
791 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
792 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
794 result
[1] = a
[1] * b
[1];
797 store_vector4( inst
, machine
, result
);
800 case OPCODE_EX2
: /* Exponential base 2 */
802 GLfloat a
[4], result
[4];
803 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
804 result
[0] = result
[1] = result
[2] = result
[3] =
805 (GLfloat
) _mesa_pow(2.0, a
[0]);
806 store_vector4( inst
, machine
, result
);
811 GLfloat a
[4], result
[4];
812 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
813 result
[0] = FLOORF(a
[0]);
814 result
[1] = FLOORF(a
[1]);
815 result
[2] = FLOORF(a
[2]);
816 result
[3] = FLOORF(a
[3]);
817 store_vector4( inst
, machine
, result
);
822 GLfloat a
[4], result
[4];
823 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
824 result
[0] = a
[0] - FLOORF(a
[0]);
825 result
[1] = a
[1] - FLOORF(a
[1]);
826 result
[2] = a
[2] - FLOORF(a
[2]);
827 result
[3] = a
[3] - FLOORF(a
[3]);
828 store_vector4( inst
, machine
, result
);
831 case OPCODE_KIL_NV
: /* NV_f_p only */
833 const GLuint swizzle
= inst
->DstReg
.CondSwizzle
;
834 const GLuint condMask
= inst
->DstReg
.CondMask
;
835 if (test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 0)], condMask
) ||
836 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 1)], condMask
) ||
837 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 2)], condMask
) ||
838 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 3)], condMask
)) {
843 case OPCODE_KIL
: /* ARB_f_p only */
846 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
847 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
852 case OPCODE_LG2
: /* log base 2 */
854 GLfloat a
[4], result
[4];
855 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
856 result
[0] = result
[1] = result
[2] = result
[3] = LOG2(a
[0]);
857 store_vector4( inst
, machine
, result
);
862 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
863 GLfloat a
[4], result
[4];
864 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
865 a
[0] = MAX2(a
[0], 0.0F
);
866 a
[1] = MAX2(a
[1], 0.0F
);
867 /* XXX ARB version clamps a[3], NV version doesn't */
868 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
871 /* XXX we could probably just use pow() here */
873 if (a
[1] == 0.0 && a
[3] == 0.0)
876 result
[2] = EXPF(a
[3] * LOGF(a
[1]));
882 store_vector4( inst
, machine
, result
);
887 GLfloat a
[4], b
[4], c
[4], result
[4];
888 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
889 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
890 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
891 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
892 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
893 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
894 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
895 store_vector4( inst
, machine
, result
);
900 GLfloat a
[4], b
[4], c
[4], result
[4];
901 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
902 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
903 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
904 result
[0] = a
[0] * b
[0] + c
[0];
905 result
[1] = a
[1] * b
[1] + c
[1];
906 result
[2] = a
[2] * b
[2] + c
[2];
907 result
[3] = a
[3] * b
[3] + c
[3];
908 store_vector4( inst
, machine
, result
);
913 GLfloat a
[4], b
[4], result
[4];
914 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
915 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
916 result
[0] = MAX2(a
[0], b
[0]);
917 result
[1] = MAX2(a
[1], b
[1]);
918 result
[2] = MAX2(a
[2], b
[2]);
919 result
[3] = MAX2(a
[3], b
[3]);
920 store_vector4( inst
, machine
, result
);
922 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
923 result
[0], result
[1], result
[2], result
[3],
924 a
[0], a
[1], a
[2], a
[3],
925 b
[0], b
[1], b
[2], b
[3]);
931 GLfloat a
[4], b
[4], result
[4];
932 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
933 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
934 result
[0] = MIN2(a
[0], b
[0]);
935 result
[1] = MIN2(a
[1], b
[1]);
936 result
[2] = MIN2(a
[2], b
[2]);
937 result
[3] = MIN2(a
[3], b
[3]);
938 store_vector4( inst
, machine
, result
);
944 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, result
);
945 store_vector4( inst
, machine
, result
);
947 printf("MOV (%g %g %g %g)\n",
948 result
[0], result
[1], result
[2], result
[3]);
954 GLfloat a
[4], b
[4], result
[4];
955 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
956 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
957 result
[0] = a
[0] * b
[0];
958 result
[1] = a
[1] * b
[1];
959 result
[2] = a
[2] * b
[2];
960 result
[3] = a
[3] * b
[3];
961 store_vector4( inst
, machine
, result
);
963 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
964 result
[0], result
[1], result
[2], result
[3],
965 a
[0], a
[1], a
[2], a
[3],
966 b
[0], b
[1], b
[2], b
[3]);
970 case OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
972 GLfloat a
[4], result
[4];
974 GLuint
*rawResult
= (GLuint
*) result
;
976 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
977 hx
= _mesa_float_to_half(a
[0]);
978 hy
= _mesa_float_to_half(a
[1]);
979 twoHalves
= hx
| (hy
<< 16);
980 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
982 store_vector4( inst
, machine
, result
);
985 case OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
987 GLfloat a
[4], result
[4];
988 GLuint usx
, usy
, *rawResult
= (GLuint
*) result
;
989 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
990 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
991 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
992 usx
= IROUND(a
[0] * 65535.0F
);
993 usy
= IROUND(a
[1] * 65535.0F
);
994 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
996 store_vector4( inst
, machine
, result
);
999 case OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
1001 GLfloat a
[4], result
[4];
1002 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
1003 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1004 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
1005 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
1006 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
1007 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
1008 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
1009 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
1010 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
1011 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
1012 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1013 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1014 store_vector4( inst
, machine
, result
);
1017 case OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
1019 GLfloat a
[4], result
[4];
1020 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
1021 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1022 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
1023 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
1024 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
1025 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
1026 ubx
= IROUND(255.0F
* a
[0]);
1027 uby
= IROUND(255.0F
* a
[1]);
1028 ubz
= IROUND(255.0F
* a
[2]);
1029 ubw
= IROUND(255.0F
* a
[3]);
1030 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
1031 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
1032 store_vector4( inst
, machine
, result
);
1037 GLfloat a
[4], b
[4], result
[4];
1038 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1039 fetch_vector1( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1040 result
[0] = result
[1] = result
[2] = result
[3]
1041 = (GLfloat
)_mesa_pow(a
[0], b
[0]);
1042 store_vector4( inst
, machine
, result
);
1047 GLfloat a
[4], result
[4];
1048 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1052 else if (IS_INF_OR_NAN(a
[0]))
1053 printf("RCP(inf)\n");
1055 result
[0] = result
[1] = result
[2] = result
[3] = 1.0F
/ a
[0];
1056 store_vector4( inst
, machine
, result
);
1059 case OPCODE_RFL
: /* reflection vector */
1061 GLfloat axis
[4], dir
[4], result
[4], tmpX
, tmpW
;
1062 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, axis
);
1063 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, dir
);
1064 tmpW
= DOT3(axis
, axis
);
1065 tmpX
= (2.0F
* DOT3(axis
, dir
)) / tmpW
;
1066 result
[0] = tmpX
* axis
[0] - dir
[0];
1067 result
[1] = tmpX
* axis
[1] - dir
[1];
1068 result
[2] = tmpX
* axis
[2] - dir
[2];
1069 /* result[3] is never written! XXX enforce in parser! */
1070 store_vector4( inst
, machine
, result
);
1073 case OPCODE_RSQ
: /* 1 / sqrt() */
1075 GLfloat a
[4], result
[4];
1076 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1078 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1079 store_vector4( inst
, machine
, result
);
1081 printf("RSQ %g = 1/sqrt(|%g|)\n", result
[0], a
[0]);
1085 case OPCODE_SCS
: /* sine and cos */
1087 GLfloat a
[4], result
[4];
1088 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1089 result
[0] = (GLfloat
)_mesa_cos(a
[0]);
1090 result
[1] = (GLfloat
)_mesa_sin(a
[0]);
1091 result
[2] = 0.0; /* undefined! */
1092 result
[3] = 0.0; /* undefined! */
1093 store_vector4( inst
, machine
, result
);
1096 case OPCODE_SEQ
: /* set on equal */
1098 GLfloat a
[4], b
[4], result
[4];
1099 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1100 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1101 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1102 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1103 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1104 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1105 store_vector4( inst
, machine
, result
);
1108 case OPCODE_SFL
: /* set false, operands ignored */
1110 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1111 store_vector4( inst
, machine
, result
);
1114 case OPCODE_SGE
: /* set on greater or equal */
1116 GLfloat a
[4], b
[4], result
[4];
1117 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1118 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1119 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1120 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1121 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1122 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1123 store_vector4( inst
, machine
, result
);
1126 case OPCODE_SGT
: /* set on greater */
1128 GLfloat a
[4], b
[4], result
[4];
1129 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1130 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1131 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1132 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1133 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1134 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1135 store_vector4( inst
, machine
, result
);
1140 GLfloat a
[4], result
[4];
1141 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1142 result
[0] = result
[1] = result
[2] = result
[3]
1143 = (GLfloat
) _mesa_sin(a
[0]);
1144 store_vector4( inst
, machine
, result
);
1147 case OPCODE_SLE
: /* set on less or equal */
1149 GLfloat a
[4], b
[4], result
[4];
1150 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1151 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1152 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1153 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1154 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1155 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1156 store_vector4( inst
, machine
, result
);
1159 case OPCODE_SLT
: /* set on less */
1161 GLfloat a
[4], b
[4], result
[4];
1162 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1163 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1164 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1165 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1166 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1167 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1168 store_vector4( inst
, machine
, result
);
1171 case OPCODE_SNE
: /* set on not equal */
1173 GLfloat a
[4], b
[4], result
[4];
1174 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1175 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1176 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1177 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1178 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1179 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1180 store_vector4( inst
, machine
, result
);
1183 case OPCODE_STR
: /* set true, operands ignored */
1185 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1186 store_vector4( inst
, machine
, result
);
1191 GLfloat a
[4], b
[4], result
[4];
1192 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1193 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1194 result
[0] = a
[0] - b
[0];
1195 result
[1] = a
[1] - b
[1];
1196 result
[2] = a
[2] - b
[2];
1197 result
[3] = a
[3] - b
[3];
1198 store_vector4( inst
, machine
, result
);
1201 case OPCODE_SWZ
: /* extended swizzle */
1203 const struct prog_src_register
*source
= &inst
->SrcReg
[0];
1204 const GLfloat
*src
= get_register_pointer(ctx
, source
,
1208 for (i
= 0; i
< 4; i
++) {
1209 const GLuint swz
= GET_SWZ(source
->Swizzle
, i
);
1210 if (swz
== SWIZZLE_ZERO
)
1212 else if (swz
== SWIZZLE_ONE
)
1217 result
[i
] = src
[swz
];
1219 if (source
->NegateBase
& (1 << i
))
1220 result
[i
] = -result
[i
];
1222 store_vector4( inst
, machine
, result
);
1225 case OPCODE_TEX
: /* Both ARB and NV frag prog */
1228 GLfloat texcoord
[4], color
[4];
1229 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1230 /* Note: we pass 0 for LOD. The ARB extension requires it
1231 * while the NV extension says it's implementation dependant.
1233 /* KW: Previously lambda was passed as zero, but I
1234 * believe this is incorrect, the spec seems to
1235 * indicate rather that lambda should not be
1236 * changed/biased, unlike TXB where texcoord[3] is
1237 * added to the lambda calculations. The lambda should
1238 * still be calculated normally for TEX & TXP though,
1239 * not set to zero. Otherwise it's very difficult to
1240 * implement normal GL semantics through the fragment
1243 fetch_texel( ctx
, texcoord
,
1244 span
->array
->lambda
[inst
->TexSrcUnit
][column
],
1245 inst
->TexSrcUnit
, color
);
1248 printf("color[3] = %f\n", color
[3]);
1250 store_vector4( inst
, machine
, color
);
1253 case OPCODE_TXB
: /* GL_ARB_fragment_program only */
1254 /* Texel lookup with LOD bias */
1256 GLfloat texcoord
[4], color
[4], bias
, lambda
;
1258 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1259 /* texcoord[3] is the bias to add to lambda */
1260 bias
= ctx
->Texture
.Unit
[inst
->TexSrcUnit
].LodBias
1261 + ctx
->Texture
.Unit
[inst
->TexSrcUnit
]._Current
->LodBias
1263 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
] + bias
;
1264 fetch_texel( ctx
, texcoord
, lambda
,
1265 inst
->TexSrcUnit
, color
);
1266 store_vector4( inst
, machine
, color
);
1269 case OPCODE_TXD
: /* GL_NV_fragment_program only */
1270 /* Texture lookup w/ partial derivatives for LOD */
1272 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1273 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1274 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, dtdx
);
1275 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, dtdy
);
1276 fetch_texel_deriv( ctx
, texcoord
, dtdx
, dtdy
, inst
->TexSrcUnit
,
1278 store_vector4( inst
, machine
, color
);
1281 case OPCODE_TXP
: /* GL_ARB_fragment_program only */
1282 /* Texture lookup w/ projective divide */
1284 GLfloat texcoord
[4], color
[4];
1285 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1286 /* Not so sure about this test - if texcoord[3] is
1287 * zero, we'd probably be fine except for an ASSERT in
1288 * IROUND_POS() which gets triggered by the inf values created.
1290 if (texcoord
[3] != 0.0) {
1291 texcoord
[0] /= texcoord
[3];
1292 texcoord
[1] /= texcoord
[3];
1293 texcoord
[2] /= texcoord
[3];
1295 /* KW: Previously lambda was passed as zero, but I
1296 * believe this is incorrect, the spec seems to
1297 * indicate rather that lambda should not be
1298 * changed/biased, unlike TXB where texcoord[3] is
1299 * added to the lambda calculations. The lambda should
1300 * still be calculated normally for TEX & TXP though,
1303 fetch_texel( ctx
, texcoord
,
1304 span
->array
->lambda
[inst
->TexSrcUnit
][column
],
1305 inst
->TexSrcUnit
, color
);
1306 store_vector4( inst
, machine
, color
);
1309 case OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1310 /* Texture lookup w/ projective divide */
1312 GLfloat texcoord
[4], color
[4];
1313 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1314 if (inst
->TexSrcTarget
!= TEXTURE_CUBE_INDEX
&&
1315 texcoord
[3] != 0.0) {
1316 texcoord
[0] /= texcoord
[3];
1317 texcoord
[1] /= texcoord
[3];
1318 texcoord
[2] /= texcoord
[3];
1320 fetch_texel( ctx
, texcoord
,
1321 span
->array
->lambda
[inst
->TexSrcUnit
][column
],
1322 inst
->TexSrcUnit
, color
);
1323 store_vector4( inst
, machine
, color
);
1326 case OPCODE_UP2H
: /* unpack two 16-bit floats */
1328 GLfloat a
[4], result
[4];
1329 const GLuint
*rawBits
= (const GLuint
*) a
;
1331 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1332 hx
= rawBits
[0] & 0xffff;
1333 hy
= rawBits
[0] >> 16;
1334 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1335 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1336 store_vector4( inst
, machine
, result
);
1339 case OPCODE_UP2US
: /* unpack two GLushorts */
1341 GLfloat a
[4], result
[4];
1342 const GLuint
*rawBits
= (const GLuint
*) a
;
1344 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1345 usx
= rawBits
[0] & 0xffff;
1346 usy
= rawBits
[0] >> 16;
1347 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1348 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1349 store_vector4( inst
, machine
, result
);
1352 case OPCODE_UP4B
: /* unpack four GLbytes */
1354 GLfloat a
[4], result
[4];
1355 const GLuint
*rawBits
= (const GLuint
*) a
;
1356 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1357 result
[0] = (((rawBits
[0] >> 0) & 0xff) - 128) / 127.0F
;
1358 result
[1] = (((rawBits
[0] >> 8) & 0xff) - 128) / 127.0F
;
1359 result
[2] = (((rawBits
[0] >> 16) & 0xff) - 128) / 127.0F
;
1360 result
[3] = (((rawBits
[0] >> 24) & 0xff) - 128) / 127.0F
;
1361 store_vector4( inst
, machine
, result
);
1364 case OPCODE_UP4UB
: /* unpack four GLubytes */
1366 GLfloat a
[4], result
[4];
1367 const GLuint
*rawBits
= (const GLuint
*) a
;
1368 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1369 result
[0] = ((rawBits
[0] >> 0) & 0xff) / 255.0F
;
1370 result
[1] = ((rawBits
[0] >> 8) & 0xff) / 255.0F
;
1371 result
[2] = ((rawBits
[0] >> 16) & 0xff) / 255.0F
;
1372 result
[3] = ((rawBits
[0] >> 24) & 0xff) / 255.0F
;
1373 store_vector4( inst
, machine
, result
);
1376 case OPCODE_XPD
: /* cross product */
1378 GLfloat a
[4], b
[4], result
[4];
1379 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1380 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1381 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1382 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1383 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1385 store_vector4( inst
, machine
, result
);
1388 case OPCODE_X2D
: /* 2-D matrix transform */
1390 GLfloat a
[4], b
[4], c
[4], result
[4];
1391 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1392 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1393 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
1394 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1395 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1396 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1397 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1398 store_vector4( inst
, machine
, result
);
1403 if (inst
->SrcReg
[0].File
!= -1) {
1405 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1406 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst
->Data
,
1407 a
[0], a
[1], a
[2], a
[3]);
1410 _mesa_printf("%s\n", (const char *) inst
->Data
);
1417 _mesa_problem(ctx
, "Bad opcode %d in _mesa_exec_fragment_program",
1419 return GL_TRUE
; /* return value doesn't matter */
1427 * Initialize the virtual fragment program machine state prior to running
1428 * fragment program on a fragment. This involves initializing the input
1429 * registers, condition codes, etc.
1430 * \param machine the virtual machine state to init
1431 * \param program the fragment program we're about to run
1432 * \param span the span of pixels we'll operate on
1433 * \param col which element (column) of the span we'll operate on
1436 init_machine( GLcontext
*ctx
, struct fp_machine
*machine
,
1437 const struct gl_fragment_program
*program
,
1438 const SWspan
*span
, GLuint col
)
1440 GLuint inputsRead
= program
->Base
.InputsRead
;
1443 if (ctx
->FragmentProgram
.CallbackEnabled
)
1446 if (program
->Base
.Target
== GL_FRAGMENT_PROGRAM_NV
) {
1447 /* Clear temporary registers (undefined for ARB_f_p) */
1448 _mesa_bzero(machine
->Temporaries
,
1449 MAX_NV_FRAGMENT_PROGRAM_TEMPS
* 4 * sizeof(GLfloat
));
1452 /* Load input registers */
1453 if (inputsRead
& (1 << FRAG_ATTRIB_WPOS
)) {
1454 GLfloat
*wpos
= machine
->Inputs
[FRAG_ATTRIB_WPOS
];
1455 ASSERT(span
->arrayMask
& SPAN_Z
);
1456 if (span
->arrayMask
& SPAN_XY
) {
1457 wpos
[0] = (GLfloat
) span
->array
->x
[col
];
1458 wpos
[1] = (GLfloat
) span
->array
->y
[col
];
1461 wpos
[0] = (GLfloat
) span
->x
+ col
;
1462 wpos
[1] = (GLfloat
) span
->y
;
1464 wpos
[2] = (GLfloat
) span
->array
->z
[col
] / ctx
->DrawBuffer
->_DepthMaxF
;
1465 wpos
[3] = span
->w
+ col
* span
->dwdx
;
1467 if (inputsRead
& (1 << FRAG_ATTRIB_COL0
)) {
1468 ASSERT(span
->arrayMask
& SPAN_RGBA
);
1469 COPY_4V(machine
->Inputs
[FRAG_ATTRIB_COL0
],
1470 span
->array
->color
.sz4
.rgba
[col
]);
1472 if (inputsRead
& (1 << FRAG_ATTRIB_COL1
)) {
1473 ASSERT(span
->arrayMask
& SPAN_SPEC
);
1474 COPY_4V(machine
->Inputs
[FRAG_ATTRIB_COL1
],
1475 span
->array
->color
.sz4
.spec
[col
]);
1477 if (inputsRead
& (1 << FRAG_ATTRIB_FOGC
)) {
1478 GLfloat
*fogc
= machine
->Inputs
[FRAG_ATTRIB_FOGC
];
1479 ASSERT(span
->arrayMask
& SPAN_FOG
);
1480 fogc
[0] = span
->array
->fog
[col
];
1485 for (u
= 0; u
< ctx
->Const
.MaxTextureCoordUnits
; u
++) {
1486 if (inputsRead
& (1 << (FRAG_ATTRIB_TEX0
+ u
))) {
1487 GLfloat
*tex
= machine
->Inputs
[FRAG_ATTRIB_TEX0
+ u
];
1488 /*ASSERT(ctx->Texture._EnabledCoordUnits & (1 << u));*/
1489 COPY_4V(tex
, span
->array
->texcoords
[u
][col
]);
1490 /*ASSERT(tex[0] != 0 || tex[1] != 0 || tex[2] != 0);*/
1494 /* init condition codes */
1495 machine
->CondCodes
[0] = COND_EQ
;
1496 machine
->CondCodes
[1] = COND_EQ
;
1497 machine
->CondCodes
[2] = COND_EQ
;
1498 machine
->CondCodes
[3] = COND_EQ
;
1503 * Run fragment program on the pixels in span from 'start' to 'end' - 1.
1506 run_program(GLcontext
*ctx
, SWspan
*span
, GLuint start
, GLuint end
)
1508 const struct gl_fragment_program
*program
= ctx
->FragmentProgram
._Current
;
1509 struct fp_machine machine
;
1512 CurrentMachine
= &machine
;
1514 for (i
= start
; i
< end
; i
++) {
1515 if (span
->array
->mask
[i
]) {
1516 init_machine(ctx
, &machine
, program
, span
, i
);
1518 if (execute_program(ctx
, program
, ~0, &machine
, span
, i
)) {
1519 /* Store result color */
1520 COPY_4V(span
->array
->color
.sz4
.rgba
[i
],
1521 machine
.Outputs
[FRAG_RESULT_COLR
]);
1523 /* Store result depth/z */
1524 if (program
->Base
.OutputsWritten
& (1 << FRAG_RESULT_DEPR
)) {
1525 const GLfloat depth
= machine
.Outputs
[FRAG_RESULT_DEPR
][2];
1527 span
->array
->z
[i
] = 0;
1528 else if (depth
>= 1.0)
1529 span
->array
->z
[i
] = ctx
->DrawBuffer
->_DepthMax
;
1531 span
->array
->z
[i
] = IROUND(depth
* ctx
->DrawBuffer
->_DepthMaxF
);
1535 /* killed fragment */
1536 span
->array
->mask
[i
] = GL_FALSE
;
1537 span
->writeAll
= GL_FALSE
;
1542 CurrentMachine
= NULL
;
1547 * Execute the current fragment program for all the fragments
1548 * in the given span.
1551 _swrast_exec_fragment_program( GLcontext
*ctx
, SWspan
*span
)
1553 const struct gl_fragment_program
*program
= ctx
->FragmentProgram
._Current
;
1555 /* incoming colors should be floats */
1556 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
1558 ctx
->_CurrentProgram
= GL_FRAGMENT_PROGRAM_ARB
; /* or NV, doesn't matter */
1560 if (program
->Base
.Parameters
) {
1561 _mesa_load_state_parameters(ctx
, program
->Base
.Parameters
);
1564 run_program(ctx
, span
, 0, span
->end
);
1566 if (program
->Base
.OutputsWritten
& (1 << FRAG_RESULT_DEPR
)) {
1567 span
->interpMask
&= ~SPAN_Z
;
1568 span
->arrayMask
|= SPAN_Z
;
1571 ctx
->_CurrentProgram
= 0;