2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 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 "nvfragprog.h"
41 #include "s_nvfragprog.h"
43 #include "s_texture.h"
46 /* if 1, print some debugging info */
53 fetch_texel( GLcontext
*ctx
, const GLfloat texcoord
[4], GLfloat lambda
,
54 GLuint unit
, GLfloat color
[4] )
57 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
59 /* XXX use a float-valued TextureSample routine here!!! */
60 swrast
->TextureSample
[unit
](ctx
, unit
, ctx
->Texture
.Unit
[unit
]._Current
,
61 1, (const GLfloat (*)[4]) texcoord
,
63 color
[0] = CHAN_TO_FLOAT(rgba
[0]);
64 color
[1] = CHAN_TO_FLOAT(rgba
[1]);
65 color
[2] = CHAN_TO_FLOAT(rgba
[2]);
66 color
[3] = CHAN_TO_FLOAT(rgba
[3]);
71 * Fetch a texel with the given partial derivatives to compute a level
72 * of detail in the mipmap.
75 fetch_texel_deriv( GLcontext
*ctx
, const GLfloat texcoord
[4],
76 const GLfloat texdx
[4], const GLfloat texdy
[4],
77 GLuint unit
, GLfloat color
[4] )
79 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
80 const struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[unit
]._Current
;
81 const struct gl_texture_image
*texImg
= texObj
->Image
[0][texObj
->BaseLevel
];
82 const GLfloat texW
= (GLfloat
) texImg
->WidthScale
;
83 const GLfloat texH
= (GLfloat
) texImg
->HeightScale
;
86 GLfloat lambda
= _swrast_compute_lambda(texdx
[0], texdy
[0], /* ds/dx, ds/dy */
87 texdx
[1], texdy
[1], /* dt/dx, dt/dy */
88 texdx
[3], texdy
[2], /* dq/dx, dq/dy */
90 texcoord
[0], texcoord
[1], texcoord
[3],
93 swrast
->TextureSample
[unit
](ctx
, unit
, ctx
->Texture
.Unit
[unit
]._Current
,
94 1, (const GLfloat (*)[4]) texcoord
,
96 color
[0] = CHAN_TO_FLOAT(rgba
[0]);
97 color
[1] = CHAN_TO_FLOAT(rgba
[1]);
98 color
[2] = CHAN_TO_FLOAT(rgba
[2]);
99 color
[3] = CHAN_TO_FLOAT(rgba
[3]);
104 * Return a pointer to the 4-element float vector specified by the given
107 static INLINE
const GLfloat
*
108 get_register_pointer( GLcontext
*ctx
,
109 const struct fp_src_register
*source
,
110 const struct fp_machine
*machine
,
111 const struct fragment_program
*program
)
114 switch (source
->File
) {
115 case PROGRAM_TEMPORARY
:
116 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_TEMPS
);
117 src
= machine
->Temporaries
[source
->Index
];
120 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_INPUTS
);
121 src
= machine
->Inputs
[source
->Index
];
123 case PROGRAM_LOCAL_PARAM
:
124 ASSERT(source
->Index
< MAX_PROGRAM_LOCAL_PARAMS
);
125 src
= program
->Base
.LocalParams
[source
->Index
];
127 case PROGRAM_ENV_PARAM
:
128 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_PARAMS
);
129 src
= ctx
->FragmentProgram
.Parameters
[source
->Index
];
132 case PROGRAM_STATE_VAR
:
135 case PROGRAM_NAMED_PARAM
:
136 ASSERT(source
->Index
< (GLint
) program
->Parameters
->NumParameters
);
137 src
= program
->Parameters
->Parameters
[source
->Index
].Values
;
140 _mesa_problem(ctx
, "Invalid input register file in fetch_vector4");
148 * Fetch a 4-element float vector from the given source register.
149 * Apply swizzling and negating as needed.
152 fetch_vector4( GLcontext
*ctx
,
153 const struct fp_src_register
*source
,
154 const struct fp_machine
*machine
,
155 const struct fragment_program
*program
,
158 const GLfloat
*src
= get_register_pointer(ctx
, source
, machine
, program
);
161 result
[0] = src
[source
->Swizzle
[0]];
162 result
[1] = src
[source
->Swizzle
[1]];
163 result
[2] = src
[source
->Swizzle
[2]];
164 result
[3] = src
[source
->Swizzle
[3]];
166 if (source
->NegateBase
) {
167 result
[0] = -result
[0];
168 result
[1] = -result
[1];
169 result
[2] = -result
[2];
170 result
[3] = -result
[3];
173 result
[0] = FABSF(result
[0]);
174 result
[1] = FABSF(result
[1]);
175 result
[2] = FABSF(result
[2]);
176 result
[3] = FABSF(result
[3]);
178 if (source
->NegateAbs
) {
179 result
[0] = -result
[0];
180 result
[1] = -result
[1];
181 result
[2] = -result
[2];
182 result
[3] = -result
[3];
188 * Fetch the derivative with respect to X for the given register.
189 * \return GL_TRUE if it was easily computed or GL_FALSE if we
190 * need to execute another instance of the program (ugh)!
193 fetch_vector4_deriv( GLcontext
*ctx
,
194 const struct fp_src_register
*source
,
195 const struct sw_span
*span
,
196 char xOrY
, GLint column
, GLfloat result
[4] )
200 ASSERT(xOrY
== 'X' || xOrY
== 'Y');
202 switch (source
->Index
) {
203 case FRAG_ATTRIB_WPOS
:
207 src
[2] = span
->dzdx
/ ctx
->DepthMaxF
;
213 src
[2] = span
->dzdy
/ ctx
->DepthMaxF
;
217 case FRAG_ATTRIB_COL0
:
219 src
[0] = span
->drdx
* (1.0F
/ CHAN_MAXF
);
220 src
[1] = span
->dgdx
* (1.0F
/ CHAN_MAXF
);
221 src
[2] = span
->dbdx
* (1.0F
/ CHAN_MAXF
);
222 src
[3] = span
->dadx
* (1.0F
/ CHAN_MAXF
);
225 src
[0] = span
->drdy
* (1.0F
/ CHAN_MAXF
);
226 src
[1] = span
->dgdy
* (1.0F
/ CHAN_MAXF
);
227 src
[2] = span
->dbdy
* (1.0F
/ CHAN_MAXF
);
228 src
[3] = span
->dady
* (1.0F
/ CHAN_MAXF
);
231 case FRAG_ATTRIB_COL1
:
233 src
[0] = span
->dsrdx
* (1.0F
/ CHAN_MAXF
);
234 src
[1] = span
->dsgdx
* (1.0F
/ CHAN_MAXF
);
235 src
[2] = span
->dsbdx
* (1.0F
/ CHAN_MAXF
);
236 src
[3] = 0.0; /* XXX need this */
239 src
[0] = span
->dsrdy
* (1.0F
/ CHAN_MAXF
);
240 src
[1] = span
->dsgdy
* (1.0F
/ CHAN_MAXF
);
241 src
[2] = span
->dsbdy
* (1.0F
/ CHAN_MAXF
);
242 src
[3] = 0.0; /* XXX need this */
245 case FRAG_ATTRIB_FOGC
:
247 src
[0] = span
->dfogdx
;
253 src
[0] = span
->dfogdy
;
259 case FRAG_ATTRIB_TEX0
:
260 case FRAG_ATTRIB_TEX1
:
261 case FRAG_ATTRIB_TEX2
:
262 case FRAG_ATTRIB_TEX3
:
263 case FRAG_ATTRIB_TEX4
:
264 case FRAG_ATTRIB_TEX5
:
265 case FRAG_ATTRIB_TEX6
:
266 case FRAG_ATTRIB_TEX7
:
268 const GLuint u
= source
->Index
- FRAG_ATTRIB_TEX0
;
269 /* this is a little tricky - I think I've got it right */
270 const GLfloat invQ
= 1.0f
/ (span
->tex
[u
][3]
271 + span
->texStepX
[u
][3] * column
);
272 src
[0] = span
->texStepX
[u
][0] * invQ
;
273 src
[1] = span
->texStepX
[u
][1] * invQ
;
274 src
[2] = span
->texStepX
[u
][2] * invQ
;
275 src
[3] = span
->texStepX
[u
][3] * invQ
;
278 const GLuint u
= source
->Index
- FRAG_ATTRIB_TEX0
;
279 /* Tricky, as above, but in Y direction */
280 const GLfloat invQ
= 1.0f
/ (span
->tex
[u
][3] + span
->texStepY
[u
][3]);
281 src
[0] = span
->texStepY
[u
][0] * invQ
;
282 src
[1] = span
->texStepY
[u
][1] * invQ
;
283 src
[2] = span
->texStepY
[u
][2] * invQ
;
284 src
[3] = span
->texStepY
[u
][3] * invQ
;
291 result
[0] = src
[source
->Swizzle
[0]];
292 result
[1] = src
[source
->Swizzle
[1]];
293 result
[2] = src
[source
->Swizzle
[2]];
294 result
[3] = src
[source
->Swizzle
[3]];
296 if (source
->NegateBase
) {
297 result
[0] = -result
[0];
298 result
[1] = -result
[1];
299 result
[2] = -result
[2];
300 result
[3] = -result
[3];
303 result
[0] = FABSF(result
[0]);
304 result
[1] = FABSF(result
[1]);
305 result
[2] = FABSF(result
[2]);
306 result
[3] = FABSF(result
[3]);
308 if (source
->NegateAbs
) {
309 result
[0] = -result
[0];
310 result
[1] = -result
[1];
311 result
[2] = -result
[2];
312 result
[3] = -result
[3];
319 * As above, but only return result[0] element.
322 fetch_vector1( GLcontext
*ctx
,
323 const struct fp_src_register
*source
,
324 const struct fp_machine
*machine
,
325 const struct fragment_program
*program
,
328 const GLfloat
*src
= get_register_pointer(ctx
, source
, machine
, program
);
331 result
[0] = src
[source
->Swizzle
[0]];
333 if (source
->NegateBase
) {
334 result
[0] = -result
[0];
337 result
[0] = FABSF(result
[0]);
339 if (source
->NegateAbs
) {
340 result
[0] = -result
[0];
346 * Test value against zero and return GT, LT, EQ or UN if NaN.
349 generate_cc( float value
)
352 return COND_UN
; /* NaN */
361 * Test if the ccMaskRule is satisfied by the given condition code.
362 * Used to mask destination writes according to the current condition codee.
364 static INLINE GLboolean
365 test_cc(GLuint condCode
, GLuint ccMaskRule
)
367 switch (ccMaskRule
) {
368 case COND_EQ
: return (condCode
== COND_EQ
);
369 case COND_NE
: return (condCode
!= COND_EQ
);
370 case COND_LT
: return (condCode
== COND_LT
);
371 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
372 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
373 case COND_GT
: return (condCode
== COND_GT
);
374 case COND_TR
: return GL_TRUE
;
375 case COND_FL
: return GL_FALSE
;
376 default: return GL_TRUE
;
382 * Store 4 floats into a register. Observe the instructions saturate and
383 * set-condition-code flags.
386 store_vector4( const struct fp_instruction
*inst
,
387 struct fp_machine
*machine
,
388 const GLfloat value
[4] )
390 const struct fp_dst_register
*dest
= &(inst
->DstReg
);
391 const GLboolean clamp
= inst
->Saturate
;
392 const GLboolean updateCC
= inst
->UpdateCondRegister
;
394 GLfloat clampedValue
[4];
395 const GLboolean
*writeMask
= dest
->WriteMask
;
396 GLboolean condWriteMask
[4];
398 switch (dest
->File
) {
400 dstReg
= machine
->Outputs
[dest
->Index
];
402 case PROGRAM_TEMPORARY
:
403 dstReg
= machine
->Temporaries
[dest
->Index
];
406 _mesa_problem(NULL
, "bad register file in store_vector4(fp)");
411 if (value
[0] > 1.0e10
||
412 IS_INF_OR_NAN(value
[0]) ||
413 IS_INF_OR_NAN(value
[1]) ||
414 IS_INF_OR_NAN(value
[2]) ||
415 IS_INF_OR_NAN(value
[3]) )
416 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
420 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
421 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
422 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
423 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
424 value
= clampedValue
;
427 if (dest
->CondMask
!= COND_TR
) {
428 condWriteMask
[0] = writeMask
[0]
429 && test_cc(machine
->CondCodes
[dest
->CondSwizzle
[0]], dest
->CondMask
);
430 condWriteMask
[1] = writeMask
[1]
431 && test_cc(machine
->CondCodes
[dest
->CondSwizzle
[1]], dest
->CondMask
);
432 condWriteMask
[2] = writeMask
[2]
433 && test_cc(machine
->CondCodes
[dest
->CondSwizzle
[2]], dest
->CondMask
);
434 condWriteMask
[3] = writeMask
[3]
435 && test_cc(machine
->CondCodes
[dest
->CondSwizzle
[3]], dest
->CondMask
);
436 writeMask
= condWriteMask
;
440 dstReg
[0] = value
[0];
442 machine
->CondCodes
[0] = generate_cc(value
[0]);
445 dstReg
[1] = value
[1];
447 machine
->CondCodes
[1] = generate_cc(value
[1]);
450 dstReg
[2] = value
[2];
452 machine
->CondCodes
[2] = generate_cc(value
[2]);
455 dstReg
[3] = value
[3];
457 machine
->CondCodes
[3] = generate_cc(value
[3]);
463 * Initialize a new machine state instance from an existing one, adding
464 * the partial derivatives onto the input registers.
465 * Used to implement DDX and DDY instructions in non-trivial cases.
468 init_machine_deriv( GLcontext
*ctx
,
469 const struct fp_machine
*machine
,
470 const struct fragment_program
*program
,
471 const struct sw_span
*span
, char xOrY
,
472 struct fp_machine
*dMachine
)
476 ASSERT(xOrY
== 'X' || xOrY
== 'Y');
478 /* copy existing machine */
479 _mesa_memcpy(dMachine
, machine
, sizeof(struct fp_machine
));
481 if (program
->Base
.Target
== GL_FRAGMENT_PROGRAM_NV
) {
482 /* Clear temporary registers (undefined for ARB_f_p) */
483 _mesa_bzero( (void*) machine
->Temporaries
,
484 MAX_NV_FRAGMENT_PROGRAM_TEMPS
* 4 * sizeof(GLfloat
));
487 /* Add derivatives */
488 if (program
->InputsRead
& (1 << FRAG_ATTRIB_WPOS
)) {
489 GLfloat
*wpos
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_WPOS
];
493 wpos
[2] += span
->dzdx
;
494 wpos
[3] += span
->dwdx
;
499 wpos
[2] += span
->dzdy
;
500 wpos
[3] += span
->dwdy
;
503 if (program
->InputsRead
& (1 << FRAG_ATTRIB_COL0
)) {
504 GLfloat
*col0
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_COL0
];
506 col0
[0] += span
->drdx
* (1.0F
/ CHAN_MAXF
);
507 col0
[1] += span
->dgdx
* (1.0F
/ CHAN_MAXF
);
508 col0
[2] += span
->dbdx
* (1.0F
/ CHAN_MAXF
);
509 col0
[3] += span
->dadx
* (1.0F
/ CHAN_MAXF
);
512 col0
[0] += span
->drdy
* (1.0F
/ CHAN_MAXF
);
513 col0
[1] += span
->dgdy
* (1.0F
/ CHAN_MAXF
);
514 col0
[2] += span
->dbdy
* (1.0F
/ CHAN_MAXF
);
515 col0
[3] += span
->dady
* (1.0F
/ CHAN_MAXF
);
518 if (program
->InputsRead
& (1 << FRAG_ATTRIB_COL1
)) {
519 GLfloat
*col1
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_COL1
];
521 col1
[0] += span
->dsrdx
* (1.0F
/ CHAN_MAXF
);
522 col1
[1] += span
->dsgdx
* (1.0F
/ CHAN_MAXF
);
523 col1
[2] += span
->dsbdx
* (1.0F
/ CHAN_MAXF
);
524 col1
[3] += 0.0; /*XXX fix */
527 col1
[0] += span
->dsrdy
* (1.0F
/ CHAN_MAXF
);
528 col1
[1] += span
->dsgdy
* (1.0F
/ CHAN_MAXF
);
529 col1
[2] += span
->dsbdy
* (1.0F
/ CHAN_MAXF
);
530 col1
[3] += 0.0; /*XXX fix */
533 if (program
->InputsRead
& (1 << FRAG_ATTRIB_FOGC
)) {
534 GLfloat
*fogc
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_FOGC
];
536 fogc
[0] += span
->dfogdx
;
539 fogc
[0] += span
->dfogdy
;
542 for (u
= 0; u
< ctx
->Const
.MaxTextureCoordUnits
; u
++) {
543 if (program
->InputsRead
& (1 << (FRAG_ATTRIB_TEX0
+ u
))) {
544 GLfloat
*tex
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_TEX0
+ u
];
545 /* XXX perspective-correct interpolation */
547 tex
[0] += span
->texStepX
[u
][0];
548 tex
[1] += span
->texStepX
[u
][1];
549 tex
[2] += span
->texStepX
[u
][2];
550 tex
[3] += span
->texStepX
[u
][3];
553 tex
[0] += span
->texStepY
[u
][0];
554 tex
[1] += span
->texStepY
[u
][1];
555 tex
[2] += span
->texStepY
[u
][2];
556 tex
[3] += span
->texStepY
[u
][3];
561 /* init condition codes */
562 dMachine
->CondCodes
[0] = COND_EQ
;
563 dMachine
->CondCodes
[1] = COND_EQ
;
564 dMachine
->CondCodes
[2] = COND_EQ
;
565 dMachine
->CondCodes
[3] = COND_EQ
;
570 * Execute the given vertex program.
571 * NOTE: we do everything in single-precision floating point; we don't
572 * currently observe the single/half/fixed-precision qualifiers.
573 * \param ctx - rendering context
574 * \param program - the fragment program to execute
575 * \param machine - machine state (register file)
576 * \param maxInst - max number of instructions to execute
577 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
580 execute_program( GLcontext
*ctx
,
581 const struct fragment_program
*program
, GLuint maxInst
,
582 struct fp_machine
*machine
, const struct sw_span
*span
,
588 printf("execute fragment program --------------------\n");
591 for (pc
= 0; pc
< maxInst
; pc
++) {
592 const struct fp_instruction
*inst
= program
->Instructions
+ pc
;
594 if (ctx
->FragmentProgram
.CallbackEnabled
&&
595 ctx
->FragmentProgram
.Callback
) {
596 ctx
->FragmentProgram
.CurrentPosition
= inst
->StringPos
;
597 ctx
->FragmentProgram
.Callback(program
->Base
.Target
,
598 ctx
->FragmentProgram
.CallbackData
);
601 switch (inst
->Opcode
) {
604 GLfloat a
[4], result
[4];
605 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
606 result
[0] = FABSF(a
[0]);
607 result
[1] = FABSF(a
[1]);
608 result
[2] = FABSF(a
[2]);
609 result
[3] = FABSF(a
[3]);
610 store_vector4( inst
, machine
, result
);
615 GLfloat a
[4], b
[4], result
[4];
616 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
617 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
618 result
[0] = a
[0] + b
[0];
619 result
[1] = a
[1] + b
[1];
620 result
[2] = a
[2] + b
[2];
621 result
[3] = a
[3] + b
[3];
622 store_vector4( inst
, machine
, result
);
627 GLfloat a
[4], b
[4], c
[4], result
[4];
628 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
629 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
630 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
631 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
632 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
633 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
634 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
635 store_vector4( inst
, machine
, result
);
640 GLfloat a
[4], result
[4];
641 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
642 result
[0] = result
[1] = result
[2] = result
[3] = (GLfloat
)_mesa_cos(a
[0]);
643 store_vector4( inst
, machine
, result
);
646 case FP_OPCODE_DDX
: /* Partial derivative with respect to X */
648 GLfloat a
[4], aNext
[4], result
[4];
649 struct fp_machine dMachine
;
650 if (!fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], span
, 'X',
652 /* This is tricky. Make a copy of the current machine state,
653 * increment the input registers by the dx or dy partial
654 * derivatives, then re-execute the program up to the
655 * preceeding instruction, then fetch the source register.
656 * Finally, find the difference in the register values for
657 * the original and derivative runs.
659 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
660 init_machine_deriv(ctx
, machine
, program
, span
,
662 execute_program(ctx
, program
, pc
, &dMachine
, span
, column
);
663 fetch_vector4( ctx
, &inst
->SrcReg
[0], &dMachine
, program
, aNext
);
664 result
[0] = aNext
[0] - a
[0];
665 result
[1] = aNext
[1] - a
[1];
666 result
[2] = aNext
[2] - a
[2];
667 result
[3] = aNext
[3] - a
[3];
669 store_vector4( inst
, machine
, result
);
672 case FP_OPCODE_DDY
: /* Partial derivative with respect to Y */
674 GLfloat a
[4], aNext
[4], result
[4];
675 struct fp_machine dMachine
;
676 if (!fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], span
, 'Y',
678 init_machine_deriv(ctx
, machine
, program
, span
,
680 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
681 execute_program(ctx
, program
, pc
, &dMachine
, span
, column
);
682 fetch_vector4( ctx
, &inst
->SrcReg
[0], &dMachine
, program
, aNext
);
683 result
[0] = aNext
[0] - a
[0];
684 result
[1] = aNext
[1] - a
[1];
685 result
[2] = aNext
[2] - a
[2];
686 result
[3] = aNext
[3] - a
[3];
688 store_vector4( inst
, machine
, result
);
693 GLfloat a
[4], b
[4], result
[4];
694 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
695 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
696 result
[0] = result
[1] = result
[2] = result
[3] =
697 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2];
698 store_vector4( inst
, machine
, result
);
700 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
701 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
707 GLfloat a
[4], b
[4], result
[4];
708 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
709 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
710 result
[0] = result
[1] = result
[2] = result
[3] =
711 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + a
[3] * b
[3];
712 store_vector4( inst
, machine
, result
);
717 GLfloat a
[4], b
[4], result
[4];
718 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
719 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
720 result
[0] = result
[1] = result
[2] = result
[3] =
721 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + b
[3];
722 store_vector4( inst
, machine
, result
);
725 case FP_OPCODE_DST
: /* Distance vector */
727 GLfloat a
[4], b
[4], result
[4];
728 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
729 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
731 result
[1] = a
[1] * b
[1];
734 store_vector4( inst
, machine
, result
);
737 case FP_OPCODE_EX2
: /* Exponential base 2 */
739 GLfloat a
[4], result
[4];
740 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
741 result
[0] = result
[1] = result
[2] = result
[3] =
742 (GLfloat
) _mesa_pow(2.0, a
[0]);
743 store_vector4( inst
, machine
, result
);
748 GLfloat a
[4], result
[4];
749 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
750 result
[0] = FLOORF(a
[0]);
751 result
[1] = FLOORF(a
[1]);
752 result
[2] = FLOORF(a
[2]);
753 result
[3] = FLOORF(a
[3]);
754 store_vector4( inst
, machine
, result
);
759 GLfloat a
[4], result
[4];
760 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
761 result
[0] = a
[0] - FLOORF(a
[0]);
762 result
[1] = a
[1] - FLOORF(a
[1]);
763 result
[2] = a
[2] - FLOORF(a
[2]);
764 result
[3] = a
[3] - FLOORF(a
[3]);
765 store_vector4( inst
, machine
, result
);
768 case FP_OPCODE_KIL_NV
: /* NV_f_p only */
770 const GLuint
*swizzle
= inst
->DstReg
.CondSwizzle
;
771 const GLuint condMask
= inst
->DstReg
.CondMask
;
772 if (test_cc(machine
->CondCodes
[swizzle
[0]], condMask
) ||
773 test_cc(machine
->CondCodes
[swizzle
[1]], condMask
) ||
774 test_cc(machine
->CondCodes
[swizzle
[2]], condMask
) ||
775 test_cc(machine
->CondCodes
[swizzle
[3]], condMask
)) {
780 case FP_OPCODE_KIL
: /* ARB_f_p only */
783 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
784 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
789 case FP_OPCODE_LG2
: /* log base 2 */
791 GLfloat a
[4], result
[4];
792 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
793 result
[0] = result
[1] = result
[2] = result
[3]
795 store_vector4( inst
, machine
, result
);
800 GLfloat a
[4], result
[4];
801 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
808 result
[2] = (a
[0] > 0.0F
) ? (GLfloat
) exp(a
[3] * log(a
[1])) : 0.0F
;
810 store_vector4( inst
, machine
, result
);
815 GLfloat a
[4], b
[4], c
[4], result
[4];
816 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
817 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
818 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
819 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
820 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
821 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
822 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
823 store_vector4( inst
, machine
, result
);
828 GLfloat a
[4], b
[4], c
[4], result
[4];
829 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
830 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
831 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
832 result
[0] = a
[0] * b
[0] + c
[0];
833 result
[1] = a
[1] * b
[1] + c
[1];
834 result
[2] = a
[2] * b
[2] + c
[2];
835 result
[3] = a
[3] * b
[3] + c
[3];
836 store_vector4( inst
, machine
, result
);
841 GLfloat a
[4], b
[4], result
[4];
842 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
843 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
844 result
[0] = MAX2(a
[0], b
[0]);
845 result
[1] = MAX2(a
[1], b
[1]);
846 result
[2] = MAX2(a
[2], b
[2]);
847 result
[3] = MAX2(a
[3], b
[3]);
848 store_vector4( inst
, machine
, result
);
853 GLfloat a
[4], b
[4], result
[4];
854 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
855 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
856 result
[0] = MIN2(a
[0], b
[0]);
857 result
[1] = MIN2(a
[1], b
[1]);
858 result
[2] = MIN2(a
[2], b
[2]);
859 result
[3] = MIN2(a
[3], b
[3]);
860 store_vector4( inst
, machine
, result
);
866 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, result
);
867 store_vector4( inst
, machine
, result
);
872 GLfloat a
[4], b
[4], result
[4];
873 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
874 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
875 result
[0] = a
[0] * b
[0];
876 result
[1] = a
[1] * b
[1];
877 result
[2] = a
[2] * b
[2];
878 result
[3] = a
[3] * b
[3];
879 store_vector4( inst
, machine
, result
);
881 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
882 result
[0], result
[1], result
[2], result
[3],
883 a
[0], a
[1], a
[2], a
[3],
884 b
[0], b
[1], b
[2], b
[3]);
888 case FP_OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
890 GLfloat a
[4], result
[4];
892 GLuint
*rawResult
= (GLuint
*) result
;
894 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
895 hx
= _mesa_float_to_half(a
[0]);
896 hy
= _mesa_float_to_half(a
[1]);
897 twoHalves
= hx
| (hy
<< 16);
898 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
900 store_vector4( inst
, machine
, result
);
903 case FP_OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
905 GLfloat a
[4], result
[4];
906 GLuint usx
, usy
, *rawResult
= (GLuint
*) result
;
907 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
908 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
909 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
910 usx
= IROUND(a
[0] * 65535.0F
);
911 usy
= IROUND(a
[1] * 65535.0F
);
912 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
914 store_vector4( inst
, machine
, result
);
917 case FP_OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
919 GLfloat a
[4], result
[4];
920 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
921 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
922 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
923 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
924 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
925 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
926 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
927 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
928 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
929 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
930 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
931 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
932 store_vector4( inst
, machine
, result
);
935 case FP_OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
937 GLfloat a
[4], result
[4];
938 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
939 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
940 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
941 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
942 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
943 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
944 ubx
= IROUND(255.0F
* a
[0]);
945 uby
= IROUND(255.0F
* a
[1]);
946 ubz
= IROUND(255.0F
* a
[2]);
947 ubw
= IROUND(255.0F
* a
[3]);
948 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
949 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
950 store_vector4( inst
, machine
, result
);
955 GLfloat a
[4], b
[4], result
[4];
956 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
957 fetch_vector1( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
958 result
[0] = result
[1] = result
[2] = result
[3]
959 = (GLfloat
)_mesa_pow(a
[0], b
[0]);
960 store_vector4( inst
, machine
, result
);
965 GLfloat a
[4], result
[4];
966 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
970 else if (IS_INF_OR_NAN(a
[0]))
971 printf("RCP(inf)\n");
973 result
[0] = result
[1] = result
[2] = result
[3]
975 store_vector4( inst
, machine
, result
);
980 GLfloat axis
[4], dir
[4], result
[4], tmp
[4];
981 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, axis
);
982 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, dir
);
983 tmp
[3] = axis
[0] * axis
[0]
986 tmp
[0] = (2.0F
* (axis
[0] * dir
[0] +
988 axis
[2] * dir
[2])) / tmp
[3];
989 result
[0] = tmp
[0] * axis
[0] - dir
[0];
990 result
[1] = tmp
[0] * axis
[1] - dir
[1];
991 result
[2] = tmp
[0] * axis
[2] - dir
[2];
992 /* result[3] is never written! XXX enforce in parser! */
993 store_vector4( inst
, machine
, result
);
996 case FP_OPCODE_RSQ
: /* 1 / sqrt() */
998 GLfloat a
[4], result
[4];
999 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1000 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1001 store_vector4( inst
, machine
, result
);
1003 printf("RSQ %g = 1/sqrt(%g)\n", result
[0], a
[0]);
1007 case FP_OPCODE_SCS
: /* sine and cos */
1009 GLfloat a
[4], result
[4];
1010 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1011 result
[0] = (GLfloat
)cos(a
[0]);
1012 result
[1] = (GLfloat
)sin(a
[0]);
1013 result
[2] = 0.0; /* undefined! */
1014 result
[3] = 0.0; /* undefined! */
1015 store_vector4( inst
, machine
, result
);
1018 case FP_OPCODE_SEQ
: /* set on equal */
1020 GLfloat a
[4], b
[4], result
[4];
1021 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1022 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1023 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1024 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1025 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1026 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1027 store_vector4( inst
, machine
, result
);
1030 case FP_OPCODE_SFL
: /* set false, operands ignored */
1032 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1033 store_vector4( inst
, machine
, result
);
1036 case FP_OPCODE_SGE
: /* set on greater or equal */
1038 GLfloat a
[4], b
[4], result
[4];
1039 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1040 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1041 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1042 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1043 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1044 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1045 store_vector4( inst
, machine
, result
);
1048 case FP_OPCODE_SGT
: /* set on greater */
1050 GLfloat a
[4], b
[4], result
[4];
1051 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1052 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1053 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1054 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1055 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1056 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1057 store_vector4( inst
, machine
, result
);
1062 GLfloat a
[4], result
[4];
1063 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1064 result
[0] = result
[1] = result
[2] =
1065 result
[3] = (GLfloat
)_mesa_sin(a
[0]);
1066 store_vector4( inst
, machine
, result
);
1069 case FP_OPCODE_SLE
: /* set on less or equal */
1071 GLfloat a
[4], b
[4], result
[4];
1072 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1073 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1074 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1075 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1076 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1077 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1078 store_vector4( inst
, machine
, result
);
1081 case FP_OPCODE_SLT
: /* set on less */
1083 GLfloat a
[4], b
[4], result
[4];
1084 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1085 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1086 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1087 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1088 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1089 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1090 store_vector4( inst
, machine
, result
);
1093 case FP_OPCODE_SNE
: /* set on not equal */
1095 GLfloat a
[4], b
[4], result
[4];
1096 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1097 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1098 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1099 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1100 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1101 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1102 store_vector4( inst
, machine
, result
);
1105 case FP_OPCODE_STR
: /* set true, operands ignored */
1107 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1108 store_vector4( inst
, machine
, result
);
1113 GLfloat a
[4], b
[4], result
[4];
1114 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1115 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1116 result
[0] = a
[0] - b
[0];
1117 result
[1] = a
[1] - b
[1];
1118 result
[2] = a
[2] - b
[2];
1119 result
[3] = a
[3] - b
[3];
1120 store_vector4( inst
, machine
, result
);
1125 const struct fp_src_register
*source
= &inst
->SrcReg
[0];
1126 const GLfloat
*src
= get_register_pointer(ctx
, source
,
1131 /* do extended swizzling here */
1132 for (i
= 0; i
< 3; i
++) {
1133 if (source
->Swizzle
[i
] == SWIZZLE_ZERO
)
1135 else if (source
->Swizzle
[i
] == SWIZZLE_ONE
)
1138 result
[i
] = -src
[source
->Swizzle
[i
]];
1139 if (source
->NegateBase
)
1140 result
[i
] = -result
[i
];
1142 store_vector4( inst
, machine
, result
);
1145 case FP_OPCODE_TEX
: /* Both ARB and NV frag prog */
1148 GLfloat texcoord
[4], color
[4];
1149 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1150 /* Note: we pass 0 for LOD. The ARB extension requires it
1151 * while the NV extension says it's implementation dependant.
1153 fetch_texel( ctx
, texcoord
, 0.0F
, inst
->TexSrcUnit
, color
);
1154 store_vector4( inst
, machine
, color
);
1157 case FP_OPCODE_TXB
: /* GL_ARB_fragment_program only */
1158 /* Texel lookup with LOD bias */
1160 GLfloat texcoord
[4], color
[4], bias
, lambda
;
1162 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1163 /* texcoord[3] is the bias to add to lambda */
1164 bias
= ctx
->Texture
.Unit
[inst
->TexSrcUnit
].LodBias
1165 + ctx
->Texture
.Unit
[inst
->TexSrcUnit
]._Current
->LodBias
1167 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
] + bias
;
1168 fetch_texel( ctx
, texcoord
, lambda
,
1169 inst
->TexSrcUnit
, color
);
1170 store_vector4( inst
, machine
, color
);
1173 case FP_OPCODE_TXD
: /* GL_NV_fragment_program only */
1174 /* Texture lookup w/ partial derivatives for LOD */
1176 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1177 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1178 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, dtdx
);
1179 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, dtdy
);
1180 fetch_texel_deriv( ctx
, texcoord
, dtdx
, dtdy
, inst
->TexSrcUnit
,
1182 store_vector4( inst
, machine
, color
);
1185 case FP_OPCODE_TXP
: /* GL_ARB_fragment_program only */
1186 /* Texture lookup w/ projective divide */
1188 GLfloat texcoord
[4], color
[4];
1189 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1190 texcoord
[0] /= texcoord
[3];
1191 texcoord
[1] /= texcoord
[3];
1192 texcoord
[2] /= texcoord
[3];
1194 fetch_texel( ctx
, texcoord
, 0.0F
, inst
->TexSrcUnit
, color
);
1195 store_vector4( inst
, machine
, color
);
1198 case FP_OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1199 /* Texture lookup w/ projective divide */
1201 GLfloat texcoord
[4], color
[4];
1202 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1203 if (inst
->TexSrcBit
!= TEXTURE_CUBE_BIT
) {
1204 texcoord
[0] /= texcoord
[3];
1205 texcoord
[1] /= texcoord
[3];
1206 texcoord
[2] /= texcoord
[3];
1208 fetch_texel( ctx
, texcoord
,
1209 span
->array
->lambda
[inst
->TexSrcUnit
][column
],
1210 inst
->TexSrcUnit
, color
);
1211 store_vector4( inst
, machine
, color
);
1214 case FP_OPCODE_UP2H
: /* unpack two 16-bit floats */
1216 GLfloat a
[4], result
[4];
1217 const GLuint
*rawBits
= (const GLuint
*) a
;
1219 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1220 hx
= rawBits
[0] & 0xffff;
1221 hy
= rawBits
[0] >> 16;
1222 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1223 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1224 store_vector4( inst
, machine
, result
);
1227 case FP_OPCODE_UP2US
: /* unpack two GLushorts */
1229 GLfloat a
[4], result
[4];
1230 const GLuint
*rawBits
= (const GLuint
*) a
;
1232 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1233 usx
= rawBits
[0] & 0xffff;
1234 usy
= rawBits
[0] >> 16;
1235 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1236 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1237 store_vector4( inst
, machine
, result
);
1240 case FP_OPCODE_UP4B
: /* unpack four GLbytes */
1242 GLfloat a
[4], result
[4];
1243 const GLuint
*rawBits
= (const GLuint
*) a
;
1244 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1245 result
[0] = (((rawBits
[0] >> 0) & 0xff) - 128) / 127.0F
;
1246 result
[1] = (((rawBits
[0] >> 8) & 0xff) - 128) / 127.0F
;
1247 result
[2] = (((rawBits
[0] >> 16) & 0xff) - 128) / 127.0F
;
1248 result
[3] = (((rawBits
[0] >> 24) & 0xff) - 128) / 127.0F
;
1249 store_vector4( inst
, machine
, result
);
1252 case FP_OPCODE_UP4UB
: /* unpack four GLubytes */
1254 GLfloat a
[4], result
[4];
1255 const GLuint
*rawBits
= (const GLuint
*) a
;
1256 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1257 result
[0] = ((rawBits
[0] >> 0) & 0xff) / 255.0F
;
1258 result
[1] = ((rawBits
[0] >> 8) & 0xff) / 255.0F
;
1259 result
[2] = ((rawBits
[0] >> 16) & 0xff) / 255.0F
;
1260 result
[3] = ((rawBits
[0] >> 24) & 0xff) / 255.0F
;
1261 store_vector4( inst
, machine
, result
);
1264 case FP_OPCODE_XPD
: /* cross product */
1266 GLfloat a
[4], b
[4], result
[4];
1267 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1268 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1269 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1270 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1271 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1273 store_vector4( inst
, machine
, result
);
1276 case FP_OPCODE_X2D
: /* 2-D matrix transform */
1278 GLfloat a
[4], b
[4], c
[4], result
[4];
1279 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1280 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1281 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
1282 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1283 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1284 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1285 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1286 store_vector4( inst
, machine
, result
);
1292 _mesa_problem(ctx
, "Bad opcode %d in _mesa_exec_fragment_program",
1294 return GL_TRUE
; /* return value doesn't matter */
1302 init_machine( GLcontext
*ctx
, struct fp_machine
*machine
,
1303 const struct fragment_program
*program
,
1304 const struct sw_span
*span
, GLuint col
)
1306 GLuint inputsRead
= program
->InputsRead
;
1309 if (ctx
->FragmentProgram
.CallbackEnabled
)
1312 if (program
->Base
.Target
== GL_FRAGMENT_PROGRAM_NV
) {
1313 /* Clear temporary registers (undefined for ARB_f_p) */
1314 _mesa_bzero(machine
->Temporaries
,
1315 MAX_NV_FRAGMENT_PROGRAM_TEMPS
* 4 * sizeof(GLfloat
));
1318 /* Load input registers */
1319 if (inputsRead
& (1 << FRAG_ATTRIB_WPOS
)) {
1320 GLfloat
*wpos
= machine
->Inputs
[FRAG_ATTRIB_WPOS
];
1321 wpos
[0] = (GLfloat
) span
->x
+ col
;
1322 wpos
[1] = (GLfloat
) span
->y
;
1323 wpos
[2] = (GLfloat
) span
->array
->z
[col
] / ctx
->DepthMaxF
;
1324 wpos
[3] = span
->w
+ col
* span
->dwdx
;
1326 if (inputsRead
& (1 << FRAG_ATTRIB_COL0
)) {
1327 GLfloat
*col0
= machine
->Inputs
[FRAG_ATTRIB_COL0
];
1328 col0
[0] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][RCOMP
]);
1329 col0
[1] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][GCOMP
]);
1330 col0
[2] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][BCOMP
]);
1331 col0
[3] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][ACOMP
]);
1333 if (inputsRead
& (1 << FRAG_ATTRIB_COL1
)) {
1334 GLfloat
*col1
= machine
->Inputs
[FRAG_ATTRIB_COL1
];
1335 col1
[0] = CHAN_TO_FLOAT(span
->array
->spec
[col
][RCOMP
]);
1336 col1
[1] = CHAN_TO_FLOAT(span
->array
->spec
[col
][GCOMP
]);
1337 col1
[2] = CHAN_TO_FLOAT(span
->array
->spec
[col
][BCOMP
]);
1338 col1
[3] = CHAN_TO_FLOAT(span
->array
->spec
[col
][ACOMP
]);
1340 if (inputsRead
& (1 << FRAG_ATTRIB_FOGC
)) {
1341 GLfloat
*fogc
= machine
->Inputs
[FRAG_ATTRIB_FOGC
];
1342 fogc
[0] = span
->array
->fog
[col
];
1347 for (u
= 0; u
< ctx
->Const
.MaxTextureCoordUnits
; u
++) {
1348 if (inputsRead
& (1 << (FRAG_ATTRIB_TEX0
+ u
))) {
1349 GLfloat
*tex
= machine
->Inputs
[FRAG_ATTRIB_TEX0
+ u
];
1350 /*ASSERT(ctx->Texture._EnabledCoordUnits & (1 << u));*/
1351 COPY_4V(tex
, span
->array
->texcoords
[u
][col
]);
1352 /*ASSERT(tex[0] != 0 || tex[1] != 0 || tex[2] != 0);*/
1356 /* init condition codes */
1357 machine
->CondCodes
[0] = COND_EQ
;
1358 machine
->CondCodes
[1] = COND_EQ
;
1359 machine
->CondCodes
[2] = COND_EQ
;
1360 machine
->CondCodes
[3] = COND_EQ
;
1366 * Execute the current fragment program, operating on the given span.
1369 _swrast_exec_fragment_program( GLcontext
*ctx
, struct sw_span
*span
)
1371 const struct fragment_program
*program
= ctx
->FragmentProgram
.Current
;
1374 ctx
->_CurrentProgram
= GL_FRAGMENT_PROGRAM_ARB
; /* or NV, doesn't matter */
1376 for (i
= 0; i
< span
->end
; i
++) {
1377 if (span
->array
->mask
[i
]) {
1378 init_machine(ctx
, &ctx
->FragmentProgram
.Machine
,
1379 ctx
->FragmentProgram
.Current
, span
, i
);
1382 if (!_swrast_execute_codegen_program(ctx
, program
, ~0,
1383 &ctx
->FragmentProgram
.Machine
,
1385 span
->array
->mask
[i
] = GL_FALSE
; /* killed fragment */
1388 if (!execute_program(ctx
, program
, ~0,
1389 &ctx
->FragmentProgram
.Machine
, span
, i
)) {
1390 span
->array
->mask
[i
] = GL_FALSE
; /* killed fragment */
1394 /* Store output registers */
1396 const GLfloat
*colOut
1397 = ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLR
];
1398 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][RCOMP
], colOut
[0]);
1399 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][GCOMP
], colOut
[1]);
1400 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][BCOMP
], colOut
[2]);
1401 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][ACOMP
], colOut
[3]);
1404 if (program
->OutputsWritten
& (1 << FRAG_OUTPUT_DEPR
))
1405 span
->array
->z
[i
] = IROUND(ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_DEPR
][0] * ctx
->DepthMaxF
);
1409 ctx
->_CurrentProgram
= 0;