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 /* Clear temporary registers */
482 _mesa_bzero( (void*) machine
->Temporaries
,
483 MAX_NV_FRAGMENT_PROGRAM_TEMPS
* 4 * sizeof(GLfloat
));
485 /* Add derivatives */
486 if (program
->InputsRead
& (1 << FRAG_ATTRIB_WPOS
)) {
487 GLfloat
*wpos
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_WPOS
];
491 wpos
[2] += span
->dzdx
;
492 wpos
[3] += span
->dwdx
;
497 wpos
[2] += span
->dzdy
;
498 wpos
[3] += span
->dwdy
;
501 if (program
->InputsRead
& (1 << FRAG_ATTRIB_COL0
)) {
502 GLfloat
*col0
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_COL0
];
504 col0
[0] += span
->drdx
* (1.0F
/ CHAN_MAXF
);
505 col0
[1] += span
->dgdx
* (1.0F
/ CHAN_MAXF
);
506 col0
[2] += span
->dbdx
* (1.0F
/ CHAN_MAXF
);
507 col0
[3] += span
->dadx
* (1.0F
/ CHAN_MAXF
);
510 col0
[0] += span
->drdy
* (1.0F
/ CHAN_MAXF
);
511 col0
[1] += span
->dgdy
* (1.0F
/ CHAN_MAXF
);
512 col0
[2] += span
->dbdy
* (1.0F
/ CHAN_MAXF
);
513 col0
[3] += span
->dady
* (1.0F
/ CHAN_MAXF
);
516 if (program
->InputsRead
& (1 << FRAG_ATTRIB_COL1
)) {
517 GLfloat
*col1
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_COL1
];
519 col1
[0] += span
->dsrdx
* (1.0F
/ CHAN_MAXF
);
520 col1
[1] += span
->dsgdx
* (1.0F
/ CHAN_MAXF
);
521 col1
[2] += span
->dsbdx
* (1.0F
/ CHAN_MAXF
);
522 col1
[3] += 0.0; /*XXX fix */
525 col1
[0] += span
->dsrdy
* (1.0F
/ CHAN_MAXF
);
526 col1
[1] += span
->dsgdy
* (1.0F
/ CHAN_MAXF
);
527 col1
[2] += span
->dsbdy
* (1.0F
/ CHAN_MAXF
);
528 col1
[3] += 0.0; /*XXX fix */
531 if (program
->InputsRead
& (1 << FRAG_ATTRIB_FOGC
)) {
532 GLfloat
*fogc
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_FOGC
];
534 fogc
[0] += span
->dfogdx
;
537 fogc
[0] += span
->dfogdy
;
540 for (u
= 0; u
< ctx
->Const
.MaxTextureCoordUnits
; u
++) {
541 if (program
->InputsRead
& (1 << (FRAG_ATTRIB_TEX0
+ u
))) {
542 GLfloat
*tex
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_TEX0
+ u
];
543 /* XXX perspective-correct interpolation */
545 tex
[0] += span
->texStepX
[u
][0];
546 tex
[1] += span
->texStepX
[u
][1];
547 tex
[2] += span
->texStepX
[u
][2];
548 tex
[3] += span
->texStepX
[u
][3];
551 tex
[0] += span
->texStepY
[u
][0];
552 tex
[1] += span
->texStepY
[u
][1];
553 tex
[2] += span
->texStepY
[u
][2];
554 tex
[3] += span
->texStepY
[u
][3];
559 /* init condition codes */
560 dMachine
->CondCodes
[0] = COND_EQ
;
561 dMachine
->CondCodes
[1] = COND_EQ
;
562 dMachine
->CondCodes
[2] = COND_EQ
;
563 dMachine
->CondCodes
[3] = COND_EQ
;
568 * Execute the given vertex program.
569 * NOTE: we do everything in single-precision floating point; we don't
570 * currently observe the single/half/fixed-precision qualifiers.
571 * \param ctx - rendering context
572 * \param program - the fragment program to execute
573 * \param machine - machine state (register file)
574 * \param maxInst - max number of instructions to execute
575 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
578 execute_program( GLcontext
*ctx
,
579 const struct fragment_program
*program
, GLuint maxInst
,
580 struct fp_machine
*machine
, const struct sw_span
*span
,
586 printf("execute fragment program --------------------\n");
589 for (pc
= 0; pc
< maxInst
; pc
++) {
590 const struct fp_instruction
*inst
= program
->Instructions
+ pc
;
592 if (ctx
->FragmentProgram
.CallbackEnabled
&&
593 ctx
->FragmentProgram
.Callback
) {
594 ctx
->FragmentProgram
.CurrentPosition
= inst
->StringPos
;
595 ctx
->FragmentProgram
.Callback(program
->Base
.Target
,
596 ctx
->FragmentProgram
.CallbackData
);
599 switch (inst
->Opcode
) {
602 GLfloat a
[4], result
[4];
603 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
604 result
[0] = FABSF(a
[0]);
605 result
[1] = FABSF(a
[1]);
606 result
[2] = FABSF(a
[2]);
607 result
[3] = FABSF(a
[3]);
608 store_vector4( inst
, machine
, result
);
613 GLfloat a
[4], b
[4], result
[4];
614 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
615 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
616 result
[0] = a
[0] + b
[0];
617 result
[1] = a
[1] + b
[1];
618 result
[2] = a
[2] + b
[2];
619 result
[3] = a
[3] + b
[3];
620 store_vector4( inst
, machine
, result
);
625 GLfloat a
[4], b
[4], c
[4], result
[4];
626 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
627 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
628 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
629 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
630 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
631 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
632 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
633 store_vector4( inst
, machine
, result
);
638 GLfloat a
[4], result
[4];
639 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
640 result
[0] = result
[1] = result
[2] = result
[3] = (GLfloat
)_mesa_cos(a
[0]);
641 store_vector4( inst
, machine
, result
);
644 case FP_OPCODE_DDX
: /* Partial derivative with respect to X */
646 GLfloat a
[4], aNext
[4], result
[4];
647 struct fp_machine dMachine
;
648 if (!fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], span
, 'X',
650 /* This is tricky. Make a copy of the current machine state,
651 * increment the input registers by the dx or dy partial
652 * derivatives, then re-execute the program up to the
653 * preceeding instruction, then fetch the source register.
654 * Finally, find the difference in the register values for
655 * the original and derivative runs.
657 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
658 init_machine_deriv(ctx
, machine
, program
, span
,
660 execute_program(ctx
, program
, pc
, &dMachine
, span
, column
);
661 fetch_vector4( ctx
, &inst
->SrcReg
[0], &dMachine
, program
, aNext
);
662 result
[0] = aNext
[0] - a
[0];
663 result
[1] = aNext
[1] - a
[1];
664 result
[2] = aNext
[2] - a
[2];
665 result
[3] = aNext
[3] - a
[3];
667 store_vector4( inst
, machine
, result
);
670 case FP_OPCODE_DDY
: /* Partial derivative with respect to Y */
672 GLfloat a
[4], aNext
[4], result
[4];
673 struct fp_machine dMachine
;
674 if (!fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], span
, 'Y',
676 init_machine_deriv(ctx
, machine
, program
, span
,
678 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
679 execute_program(ctx
, program
, pc
, &dMachine
, span
, column
);
680 fetch_vector4( ctx
, &inst
->SrcReg
[0], &dMachine
, program
, aNext
);
681 result
[0] = aNext
[0] - a
[0];
682 result
[1] = aNext
[1] - a
[1];
683 result
[2] = aNext
[2] - a
[2];
684 result
[3] = aNext
[3] - a
[3];
686 store_vector4( inst
, machine
, result
);
691 GLfloat a
[4], b
[4], result
[4];
692 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
693 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
694 result
[0] = result
[1] = result
[2] = result
[3] =
695 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2];
696 store_vector4( inst
, machine
, result
);
698 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
699 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
705 GLfloat a
[4], b
[4], result
[4];
706 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
707 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
708 result
[0] = result
[1] = result
[2] = result
[3] =
709 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + a
[3] * b
[3];
710 store_vector4( inst
, machine
, result
);
715 GLfloat a
[4], b
[4], result
[4];
716 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
717 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
718 result
[0] = result
[1] = result
[2] = result
[3] =
719 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + b
[3];
720 store_vector4( inst
, machine
, result
);
723 case FP_OPCODE_DST
: /* Distance vector */
725 GLfloat a
[4], b
[4], result
[4];
726 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
727 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
729 result
[1] = a
[1] * b
[1];
732 store_vector4( inst
, machine
, result
);
735 case FP_OPCODE_EX2
: /* Exponential base 2 */
737 GLfloat a
[4], result
[4];
738 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
739 result
[0] = result
[1] = result
[2] = result
[3] =
740 (GLfloat
) _mesa_pow(2.0, a
[0]);
741 store_vector4( inst
, machine
, result
);
746 GLfloat a
[4], result
[4];
747 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
748 result
[0] = FLOORF(a
[0]);
749 result
[1] = FLOORF(a
[1]);
750 result
[2] = FLOORF(a
[2]);
751 result
[3] = FLOORF(a
[3]);
752 store_vector4( inst
, machine
, result
);
757 GLfloat a
[4], result
[4];
758 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
759 result
[0] = a
[0] - FLOORF(a
[0]);
760 result
[1] = a
[1] - FLOORF(a
[1]);
761 result
[2] = a
[2] - FLOORF(a
[2]);
762 result
[3] = a
[3] - FLOORF(a
[3]);
763 store_vector4( inst
, machine
, result
);
766 case FP_OPCODE_KIL_NV
: /* NV_f_p only */
768 const GLuint
*swizzle
= inst
->DstReg
.CondSwizzle
;
769 const GLuint condMask
= inst
->DstReg
.CondMask
;
770 if (test_cc(machine
->CondCodes
[swizzle
[0]], condMask
) ||
771 test_cc(machine
->CondCodes
[swizzle
[1]], condMask
) ||
772 test_cc(machine
->CondCodes
[swizzle
[2]], condMask
) ||
773 test_cc(machine
->CondCodes
[swizzle
[3]], condMask
)) {
778 case FP_OPCODE_KIL
: /* ARB_f_p only */
781 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
782 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
787 case FP_OPCODE_LG2
: /* log base 2 */
789 GLfloat a
[4], result
[4];
790 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
791 result
[0] = result
[1] = result
[2] = result
[3]
793 store_vector4( inst
, machine
, result
);
798 GLfloat a
[4], result
[4];
799 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
806 result
[2] = (a
[0] > 0.0F
) ? (GLfloat
) exp(a
[3] * log(a
[1])) : 0.0F
;
808 store_vector4( inst
, machine
, result
);
813 GLfloat a
[4], b
[4], c
[4], result
[4];
814 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
815 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
816 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
817 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
818 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
819 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
820 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
821 store_vector4( inst
, machine
, result
);
826 GLfloat a
[4], b
[4], c
[4], result
[4];
827 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
828 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
829 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
830 result
[0] = a
[0] * b
[0] + c
[0];
831 result
[1] = a
[1] * b
[1] + c
[1];
832 result
[2] = a
[2] * b
[2] + c
[2];
833 result
[3] = a
[3] * b
[3] + c
[3];
834 store_vector4( inst
, machine
, result
);
839 GLfloat a
[4], b
[4], result
[4];
840 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
841 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
842 result
[0] = MAX2(a
[0], b
[0]);
843 result
[1] = MAX2(a
[1], b
[1]);
844 result
[2] = MAX2(a
[2], b
[2]);
845 result
[3] = MAX2(a
[3], b
[3]);
846 store_vector4( inst
, machine
, result
);
851 GLfloat a
[4], b
[4], result
[4];
852 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
853 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
854 result
[0] = MIN2(a
[0], b
[0]);
855 result
[1] = MIN2(a
[1], b
[1]);
856 result
[2] = MIN2(a
[2], b
[2]);
857 result
[3] = MIN2(a
[3], b
[3]);
858 store_vector4( inst
, machine
, result
);
864 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, result
);
865 store_vector4( inst
, machine
, result
);
870 GLfloat a
[4], b
[4], result
[4];
871 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
872 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
873 result
[0] = a
[0] * b
[0];
874 result
[1] = a
[1] * b
[1];
875 result
[2] = a
[2] * b
[2];
876 result
[3] = a
[3] * b
[3];
877 store_vector4( inst
, machine
, result
);
879 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
880 result
[0], result
[1], result
[2], result
[3],
881 a
[0], a
[1], a
[2], a
[3],
882 b
[0], b
[1], b
[2], b
[3]);
886 case FP_OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
888 GLfloat a
[4], result
[4];
890 GLuint
*rawResult
= (GLuint
*) result
;
892 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
893 hx
= _mesa_float_to_half(a
[0]);
894 hy
= _mesa_float_to_half(a
[1]);
895 twoHalves
= hx
| (hy
<< 16);
896 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
898 store_vector4( inst
, machine
, result
);
901 case FP_OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
903 GLfloat a
[4], result
[4];
904 GLuint usx
, usy
, *rawResult
= (GLuint
*) result
;
905 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
906 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
907 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
908 usx
= IROUND(a
[0] * 65535.0F
);
909 usy
= IROUND(a
[1] * 65535.0F
);
910 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
912 store_vector4( inst
, machine
, result
);
915 case FP_OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
917 GLfloat a
[4], result
[4];
918 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
919 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
920 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
921 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
922 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
923 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
924 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
925 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
926 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
927 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
928 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
929 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
930 store_vector4( inst
, machine
, result
);
933 case FP_OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
935 GLfloat a
[4], result
[4];
936 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
937 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
938 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
939 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
940 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
941 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
942 ubx
= IROUND(255.0F
* a
[0]);
943 uby
= IROUND(255.0F
* a
[1]);
944 ubz
= IROUND(255.0F
* a
[2]);
945 ubw
= IROUND(255.0F
* a
[3]);
946 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
947 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
948 store_vector4( inst
, machine
, result
);
953 GLfloat a
[4], b
[4], result
[4];
954 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
955 fetch_vector1( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
956 result
[0] = result
[1] = result
[2] = result
[3]
957 = (GLfloat
)_mesa_pow(a
[0], b
[0]);
958 store_vector4( inst
, machine
, result
);
963 GLfloat a
[4], result
[4];
964 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
968 else if (IS_INF_OR_NAN(a
[0]))
969 printf("RCP(inf)\n");
971 result
[0] = result
[1] = result
[2] = result
[3]
973 store_vector4( inst
, machine
, result
);
978 GLfloat axis
[4], dir
[4], result
[4], tmp
[4];
979 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, axis
);
980 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, dir
);
981 tmp
[3] = axis
[0] * axis
[0]
984 tmp
[0] = (2.0F
* (axis
[0] * dir
[0] +
986 axis
[2] * dir
[2])) / tmp
[3];
987 result
[0] = tmp
[0] * axis
[0] - dir
[0];
988 result
[1] = tmp
[0] * axis
[1] - dir
[1];
989 result
[2] = tmp
[0] * axis
[2] - dir
[2];
990 /* result[3] is never written! XXX enforce in parser! */
991 store_vector4( inst
, machine
, result
);
994 case FP_OPCODE_RSQ
: /* 1 / sqrt() */
996 GLfloat a
[4], result
[4];
997 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
998 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
999 store_vector4( inst
, machine
, result
);
1001 printf("RSQ %g = 1/sqrt(%g)\n", result
[0], a
[0]);
1005 case FP_OPCODE_SCS
: /* sine and cos */
1007 GLfloat a
[4], result
[4];
1008 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1009 result
[0] = (GLfloat
)cos(a
[0]);
1010 result
[1] = (GLfloat
)sin(a
[0]);
1011 result
[2] = 0.0; /* undefined! */
1012 result
[3] = 0.0; /* undefined! */
1013 store_vector4( inst
, machine
, result
);
1016 case FP_OPCODE_SEQ
: /* set on equal */
1018 GLfloat a
[4], b
[4], result
[4];
1019 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1020 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1021 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1022 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1023 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1024 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1025 store_vector4( inst
, machine
, result
);
1028 case FP_OPCODE_SFL
: /* set false, operands ignored */
1030 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1031 store_vector4( inst
, machine
, result
);
1034 case FP_OPCODE_SGE
: /* set on greater or equal */
1036 GLfloat a
[4], b
[4], result
[4];
1037 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1038 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1039 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1040 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1041 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1042 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1043 store_vector4( inst
, machine
, result
);
1046 case FP_OPCODE_SGT
: /* set on greater */
1048 GLfloat a
[4], b
[4], result
[4];
1049 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1050 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1051 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1052 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1053 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1054 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1055 store_vector4( inst
, machine
, result
);
1060 GLfloat a
[4], result
[4];
1061 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1062 result
[0] = result
[1] = result
[2] =
1063 result
[3] = (GLfloat
)_mesa_sin(a
[0]);
1064 store_vector4( inst
, machine
, result
);
1067 case FP_OPCODE_SLE
: /* set on less or equal */
1069 GLfloat a
[4], b
[4], result
[4];
1070 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1071 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1072 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1073 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1074 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1075 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1076 store_vector4( inst
, machine
, result
);
1079 case FP_OPCODE_SLT
: /* set on less */
1081 GLfloat a
[4], b
[4], result
[4];
1082 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1083 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1084 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1085 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1086 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1087 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1088 store_vector4( inst
, machine
, result
);
1091 case FP_OPCODE_SNE
: /* set on not equal */
1093 GLfloat a
[4], b
[4], result
[4];
1094 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1095 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1096 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1097 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1098 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1099 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1100 store_vector4( inst
, machine
, result
);
1103 case FP_OPCODE_STR
: /* set true, operands ignored */
1105 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1106 store_vector4( inst
, machine
, result
);
1111 GLfloat a
[4], b
[4], result
[4];
1112 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1113 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1114 result
[0] = a
[0] - b
[0];
1115 result
[1] = a
[1] - b
[1];
1116 result
[2] = a
[2] - b
[2];
1117 result
[3] = a
[3] - b
[3];
1118 store_vector4( inst
, machine
, result
);
1123 const struct fp_src_register
*source
= &inst
->SrcReg
[0];
1124 const GLfloat
*src
= get_register_pointer(ctx
, source
,
1129 /* do extended swizzling here */
1130 for (i
= 0; i
< 3; i
++) {
1131 if (source
->Swizzle
[i
] == SWIZZLE_ZERO
)
1133 else if (source
->Swizzle
[i
] == SWIZZLE_ONE
)
1136 result
[i
] = -src
[source
->Swizzle
[i
]];
1137 if (source
->NegateBase
)
1138 result
[i
] = -result
[i
];
1140 store_vector4( inst
, machine
, result
);
1146 GLfloat texcoord
[4], color
[4];
1147 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1148 /* XXX: Undo perspective divide from interpolate_texcoords() */
1149 fetch_texel( ctx
, texcoord
,
1150 span
->array
->lambda
[inst
->TexSrcUnit
][column
],
1151 inst
->TexSrcUnit
, color
);
1152 store_vector4( inst
, machine
, color
);
1156 /* Texel lookup with LOD bias */
1158 GLfloat texcoord
[4], color
[4], bias
, lambda
;
1160 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1161 /* texcoord[3] is the bias to add to lambda */
1162 bias
= ctx
->Texture
.Unit
[inst
->TexSrcUnit
].LodBias
1163 + ctx
->Texture
.Unit
[inst
->TexSrcUnit
]._Current
->LodBias
1165 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
] + bias
;
1166 fetch_texel( ctx
, texcoord
, lambda
,
1167 inst
->TexSrcUnit
, color
);
1168 store_vector4( inst
, machine
, color
);
1172 /* Texture lookup w/ partial derivatives for LOD */
1174 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1175 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1176 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, dtdx
);
1177 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, dtdy
);
1178 fetch_texel_deriv( ctx
, texcoord
, dtdx
, dtdy
, inst
->TexSrcUnit
,
1180 store_vector4( inst
, machine
, color
);
1184 /* Texture lookup w/ perspective divide */
1186 GLfloat texcoord
[4], color
[4];
1187 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1188 /* Already did perspective divide in interpolate_texcoords() */
1189 fetch_texel( ctx
, texcoord
,
1190 span
->array
->lambda
[inst
->TexSrcUnit
][column
],
1191 inst
->TexSrcUnit
, color
);
1192 store_vector4( inst
, machine
, color
);
1195 case FP_OPCODE_UP2H
: /* unpack two 16-bit floats */
1197 GLfloat a
[4], result
[4];
1198 const GLuint
*rawBits
= (const GLuint
*) a
;
1200 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1201 hx
= rawBits
[0] & 0xffff;
1202 hy
= rawBits
[0] >> 16;
1203 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1204 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1205 store_vector4( inst
, machine
, result
);
1208 case FP_OPCODE_UP2US
: /* unpack two GLushorts */
1210 GLfloat a
[4], result
[4];
1211 const GLuint
*rawBits
= (const GLuint
*) a
;
1213 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1214 usx
= rawBits
[0] & 0xffff;
1215 usy
= rawBits
[0] >> 16;
1216 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1217 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1218 store_vector4( inst
, machine
, result
);
1221 case FP_OPCODE_UP4B
: /* unpack four GLbytes */
1223 GLfloat a
[4], result
[4];
1224 const GLuint
*rawBits
= (const GLuint
*) a
;
1225 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1226 result
[0] = (((rawBits
[0] >> 0) & 0xff) - 128) / 127.0F
;
1227 result
[1] = (((rawBits
[0] >> 8) & 0xff) - 128) / 127.0F
;
1228 result
[2] = (((rawBits
[0] >> 16) & 0xff) - 128) / 127.0F
;
1229 result
[3] = (((rawBits
[0] >> 24) & 0xff) - 128) / 127.0F
;
1230 store_vector4( inst
, machine
, result
);
1233 case FP_OPCODE_UP4UB
: /* unpack four GLubytes */
1235 GLfloat a
[4], result
[4];
1236 const GLuint
*rawBits
= (const GLuint
*) a
;
1237 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1238 result
[0] = ((rawBits
[0] >> 0) & 0xff) / 255.0F
;
1239 result
[1] = ((rawBits
[0] >> 8) & 0xff) / 255.0F
;
1240 result
[2] = ((rawBits
[0] >> 16) & 0xff) / 255.0F
;
1241 result
[3] = ((rawBits
[0] >> 24) & 0xff) / 255.0F
;
1242 store_vector4( inst
, machine
, result
);
1245 case FP_OPCODE_XPD
: /* cross product */
1247 GLfloat a
[4], b
[4], result
[4];
1248 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1249 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1250 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1251 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1252 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1254 store_vector4( inst
, machine
, result
);
1257 case FP_OPCODE_X2D
: /* 2-D matrix transform */
1259 GLfloat a
[4], b
[4], c
[4], result
[4];
1260 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1261 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1262 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
1263 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1264 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1265 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1266 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1267 store_vector4( inst
, machine
, result
);
1273 _mesa_problem(ctx
, "Bad opcode %d in _mesa_exec_fragment_program",
1275 return GL_TRUE
; /* return value doesn't matter */
1283 init_machine( GLcontext
*ctx
, struct fp_machine
*machine
,
1284 const struct fragment_program
*program
,
1285 const struct sw_span
*span
, GLuint col
)
1287 GLuint inputsRead
= program
->InputsRead
;
1290 if (ctx
->FragmentProgram
.CallbackEnabled
)
1293 /* Clear temporary registers */
1294 _mesa_bzero(machine
->Temporaries
,
1295 MAX_NV_FRAGMENT_PROGRAM_TEMPS
* 4 * sizeof(GLfloat
));
1297 /* Load input registers */
1298 if (inputsRead
& (1 << FRAG_ATTRIB_WPOS
)) {
1299 GLfloat
*wpos
= machine
->Inputs
[FRAG_ATTRIB_WPOS
];
1300 wpos
[0] = (GLfloat
) span
->x
+ col
;
1301 wpos
[1] = (GLfloat
) span
->y
;
1302 wpos
[2] = (GLfloat
) span
->array
->z
[col
] / ctx
->DepthMaxF
;
1303 wpos
[3] = span
->w
+ col
* span
->dwdx
;
1305 if (inputsRead
& (1 << FRAG_ATTRIB_COL0
)) {
1306 GLfloat
*col0
= machine
->Inputs
[FRAG_ATTRIB_COL0
];
1307 col0
[0] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][RCOMP
]);
1308 col0
[1] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][GCOMP
]);
1309 col0
[2] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][BCOMP
]);
1310 col0
[3] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][ACOMP
]);
1312 if (inputsRead
& (1 << FRAG_ATTRIB_COL1
)) {
1313 GLfloat
*col1
= machine
->Inputs
[FRAG_ATTRIB_COL1
];
1314 col1
[0] = CHAN_TO_FLOAT(span
->array
->spec
[col
][RCOMP
]);
1315 col1
[1] = CHAN_TO_FLOAT(span
->array
->spec
[col
][GCOMP
]);
1316 col1
[2] = CHAN_TO_FLOAT(span
->array
->spec
[col
][BCOMP
]);
1317 col1
[3] = CHAN_TO_FLOAT(span
->array
->spec
[col
][ACOMP
]);
1319 if (inputsRead
& (1 << FRAG_ATTRIB_FOGC
)) {
1320 GLfloat
*fogc
= machine
->Inputs
[FRAG_ATTRIB_FOGC
];
1321 fogc
[0] = span
->array
->fog
[col
];
1326 for (u
= 0; u
< ctx
->Const
.MaxTextureCoordUnits
; u
++) {
1327 if (inputsRead
& (1 << (FRAG_ATTRIB_TEX0
+ u
))) {
1328 GLfloat
*tex
= machine
->Inputs
[FRAG_ATTRIB_TEX0
+ u
];
1329 /*ASSERT(ctx->Texture._EnabledCoordUnits & (1 << u));*/
1330 COPY_4V(tex
, span
->array
->texcoords
[u
][col
]);
1331 /*ASSERT(tex[0] != 0 || tex[1] != 0 || tex[2] != 0);*/
1335 /* init condition codes */
1336 machine
->CondCodes
[0] = COND_EQ
;
1337 machine
->CondCodes
[1] = COND_EQ
;
1338 machine
->CondCodes
[2] = COND_EQ
;
1339 machine
->CondCodes
[3] = COND_EQ
;
1345 * Execute the current fragment program, operating on the given span.
1348 _swrast_exec_fragment_program( GLcontext
*ctx
, struct sw_span
*span
)
1350 const struct fragment_program
*program
= ctx
->FragmentProgram
.Current
;
1353 ctx
->_CurrentProgram
= GL_FRAGMENT_PROGRAM_ARB
; /* or NV, doesn't matter */
1355 for (i
= 0; i
< span
->end
; i
++) {
1356 if (span
->array
->mask
[i
]) {
1357 init_machine(ctx
, &ctx
->FragmentProgram
.Machine
,
1358 ctx
->FragmentProgram
.Current
, span
, i
);
1361 if (!_swrast_execute_codegen_program(ctx
, program
, ~0,
1362 &ctx
->FragmentProgram
.Machine
,
1364 span
->array
->mask
[i
] = GL_FALSE
; /* killed fragment */
1367 if (!execute_program(ctx
, program
, ~0,
1368 &ctx
->FragmentProgram
.Machine
, span
, i
)) {
1369 span
->array
->mask
[i
] = GL_FALSE
; /* killed fragment */
1373 /* Store output registers */
1375 const GLfloat
*colOut
1376 = ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLR
];
1377 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][RCOMP
], colOut
[0]);
1378 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][GCOMP
], colOut
[1]);
1379 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][BCOMP
], colOut
[2]);
1380 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][ACOMP
], colOut
[3]);
1383 if (program
->OutputsWritten
& (1 << FRAG_OUTPUT_DEPR
))
1384 span
->array
->z
[i
] = IROUND(ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_DEPR
][0] * ctx
->DepthMaxF
);
1388 ctx
->_CurrentProgram
= 0;