2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2003 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.
29 #include "nvfragprog.h"
33 #include "s_nvfragprog.h"
35 #include "s_texture.h"
38 /* if 1, print some debugging info */
46 fetch_texel( GLcontext
*ctx
, const GLfloat texcoord
[4], GLfloat lambda
,
47 GLuint unit
, GLfloat color
[4] )
50 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
52 swrast
->TextureSample
[unit
](ctx
, unit
, ctx
->Texture
.Unit
[unit
]._Current
,
53 1, (const GLfloat (*)[4]) texcoord
,
55 color
[0] = CHAN_TO_FLOAT(rgba
[0]);
56 color
[1] = CHAN_TO_FLOAT(rgba
[1]);
57 color
[2] = CHAN_TO_FLOAT(rgba
[2]);
58 color
[3] = CHAN_TO_FLOAT(rgba
[3]);
63 * Fetch a texel with the given partial derivatives to compute a level
64 * of detail in the mipmap.
67 fetch_texel_deriv( GLcontext
*ctx
, const GLfloat texcoord
[4],
68 const GLfloat texdx
[4], const GLfloat texdy
[4],
69 GLuint unit
, GLfloat color
[4] )
71 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
72 const struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[unit
]._Current
;
73 const struct gl_texture_image
*texImg
= texObj
->Image
[texObj
->BaseLevel
];
74 const GLfloat texW
= (GLfloat
) texImg
->WidthScale
;
75 const GLfloat texH
= (GLfloat
) texImg
->HeightScale
;
78 GLfloat lambda
= _swrast_compute_lambda(texdx
[0], texdy
[0], /* ds/dx, ds/dy */
79 texdx
[1], texdy
[1], /* dt/dx, dt/dy */
80 texdx
[3], texdy
[2], /* dq/dx, dq/dy */
82 texcoord
[0], texcoord
[1], texcoord
[3],
85 swrast
->TextureSample
[unit
](ctx
, unit
, ctx
->Texture
.Unit
[unit
]._Current
,
86 1, (const GLfloat (*)[4]) texcoord
,
88 color
[0] = CHAN_TO_FLOAT(rgba
[0]);
89 color
[1] = CHAN_TO_FLOAT(rgba
[1]);
90 color
[2] = CHAN_TO_FLOAT(rgba
[2]);
91 color
[3] = CHAN_TO_FLOAT(rgba
[3]);
96 * Return a pointer to the 4-element float vector specified by the given
99 static INLINE
const GLfloat
*
100 get_register_pointer( GLcontext
*ctx
,
101 const struct fp_src_register
*source
,
102 const struct fp_machine
*machine
,
103 const struct fragment_program
*program
)
106 switch (source
->File
) {
107 case PROGRAM_TEMPORARY
:
108 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_TEMPS
);
109 src
= machine
->Temporaries
[source
->Index
];
112 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_INPUTS
);
113 src
= machine
->Inputs
[source
->Index
];
115 case PROGRAM_LOCAL_PARAM
:
116 ASSERT(source
->Index
< MAX_PROGRAM_LOCAL_PARAMS
);
117 src
= program
->Base
.LocalParams
[source
->Index
];
119 case PROGRAM_ENV_PARAM
:
120 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_PARAMS
);
121 src
= ctx
->FragmentProgram
.Parameters
[source
->Index
];
123 case PROGRAM_NAMED_PARAM
:
124 ASSERT(source
->Index
< program
->Parameters
->NumParameters
);
125 src
= program
->Parameters
->Parameters
[source
->Index
].Values
;
127 case PROGRAM_STATE_VAR
:
131 _mesa_problem(ctx
, "Invalid input register file in fetch_vector4");
139 * Fetch a 4-element float vector from the given source register.
140 * Apply swizzling and negating as needed.
143 fetch_vector4( GLcontext
*ctx
,
144 const struct fp_src_register
*source
,
145 const struct fp_machine
*machine
,
146 const struct fragment_program
*program
,
149 const GLfloat
*src
= get_register_pointer(ctx
, source
, machine
, program
);
152 result
[0] = src
[source
->Swizzle
[0]];
153 result
[1] = src
[source
->Swizzle
[1]];
154 result
[2] = src
[source
->Swizzle
[2]];
155 result
[3] = src
[source
->Swizzle
[3]];
157 if (source
->NegateBase
) {
158 result
[0] = -result
[0];
159 result
[1] = -result
[1];
160 result
[2] = -result
[2];
161 result
[3] = -result
[3];
164 result
[0] = FABSF(result
[0]);
165 result
[1] = FABSF(result
[1]);
166 result
[2] = FABSF(result
[2]);
167 result
[3] = FABSF(result
[3]);
169 if (source
->NegateAbs
) {
170 result
[0] = -result
[0];
171 result
[1] = -result
[1];
172 result
[2] = -result
[2];
173 result
[3] = -result
[3];
179 * Fetch the derivative with respect to X for the given register.
180 * \return GL_TRUE if it was easily computed or GL_FALSE if we
181 * need to execute another instance of the program (ugh)!
184 fetch_vector4_deriv( const struct fp_src_register
*source
,
185 const struct sw_span
*span
,
186 char xOrY
, GLfloat result
[4] )
190 ASSERT(xOrY
== 'X' || xOrY
== 'Y');
192 assert(source
->File
== PROGRAM_INPUT
);
194 switch (source
->Index
) {
195 case FRAG_ATTRIB_WPOS
:
209 case FRAG_ATTRIB_COL0
:
211 src
[0] = span
->drdx
* (1.0F
/ CHAN_MAXF
);
212 src
[1] = span
->dgdx
* (1.0F
/ CHAN_MAXF
);
213 src
[2] = span
->dbdx
* (1.0F
/ CHAN_MAXF
);
214 src
[3] = span
->dadx
* (1.0F
/ CHAN_MAXF
);
217 src
[0] = span
->drdy
* (1.0F
/ CHAN_MAXF
);
218 src
[1] = span
->dgdy
* (1.0F
/ CHAN_MAXF
);
219 src
[2] = span
->dbdy
* (1.0F
/ CHAN_MAXF
);
220 src
[3] = span
->dady
* (1.0F
/ CHAN_MAXF
);
223 case FRAG_ATTRIB_COL1
:
225 src
[0] = span
->dsrdx
* (1.0F
/ CHAN_MAXF
);
226 src
[1] = span
->dsgdx
* (1.0F
/ CHAN_MAXF
);
227 src
[2] = span
->dsbdx
* (1.0F
/ CHAN_MAXF
);
228 src
[3] = 0.0; /* XXX need this */
231 src
[0] = span
->dsrdy
* (1.0F
/ CHAN_MAXF
);
232 src
[1] = span
->dsgdy
* (1.0F
/ CHAN_MAXF
);
233 src
[2] = span
->dsbdy
* (1.0F
/ CHAN_MAXF
);
234 src
[3] = 0.0; /* XXX need this */
237 case FRAG_ATTRIB_FOGC
:
239 src
[0] = span
->dfogdx
;
245 src
[0] = span
->dfogdy
;
251 case FRAG_ATTRIB_TEX0
:
252 case FRAG_ATTRIB_TEX1
:
253 case FRAG_ATTRIB_TEX2
:
254 case FRAG_ATTRIB_TEX3
:
255 case FRAG_ATTRIB_TEX4
:
256 case FRAG_ATTRIB_TEX5
:
257 case FRAG_ATTRIB_TEX6
:
258 case FRAG_ATTRIB_TEX7
:
260 const GLuint u
= source
->Index
- FRAG_ATTRIB_TEX0
;
261 src
[0] = span
->texStepX
[u
][0] * (1.0F
/ CHAN_MAXF
);
262 src
[1] = span
->texStepX
[u
][1] * (1.0F
/ CHAN_MAXF
);
263 src
[2] = span
->texStepX
[u
][2] * (1.0F
/ CHAN_MAXF
);
264 src
[3] = span
->texStepX
[u
][3] * (1.0F
/ CHAN_MAXF
);
267 const GLuint u
= source
->Index
- FRAG_ATTRIB_TEX0
;
268 src
[0] = span
->texStepY
[u
][0] * (1.0F
/ CHAN_MAXF
);
269 src
[1] = span
->texStepY
[u
][1] * (1.0F
/ CHAN_MAXF
);
270 src
[2] = span
->texStepY
[u
][2] * (1.0F
/ CHAN_MAXF
);
271 src
[3] = span
->texStepY
[u
][3] * (1.0F
/ CHAN_MAXF
);
278 result
[0] = src
[source
->Swizzle
[0]];
279 result
[1] = src
[source
->Swizzle
[1]];
280 result
[2] = src
[source
->Swizzle
[2]];
281 result
[3] = src
[source
->Swizzle
[3]];
283 if (source
->NegateBase
) {
284 result
[0] = -result
[0];
285 result
[1] = -result
[1];
286 result
[2] = -result
[2];
287 result
[3] = -result
[3];
290 result
[0] = FABSF(result
[0]);
291 result
[1] = FABSF(result
[1]);
292 result
[2] = FABSF(result
[2]);
293 result
[3] = FABSF(result
[3]);
295 if (source
->NegateAbs
) {
296 result
[0] = -result
[0];
297 result
[1] = -result
[1];
298 result
[2] = -result
[2];
299 result
[3] = -result
[3];
306 * As above, but only return result[0] element.
309 fetch_vector1( GLcontext
*ctx
,
310 const struct fp_src_register
*source
,
311 const struct fp_machine
*machine
,
312 const struct fragment_program
*program
,
315 const GLfloat
*src
= get_register_pointer(ctx
, source
, machine
, program
);
318 result
[0] = src
[source
->Swizzle
[0]];
320 if (source
->NegateBase
) {
321 result
[0] = -result
[0];
324 result
[0] = FABSF(result
[0]);
326 if (source
->NegateAbs
) {
327 result
[0] = -result
[0];
333 * Test value against zero and return GT, LT, EQ or UN if NaN.
336 generate_cc( float value
)
339 return COND_UN
; /* NaN */
348 * Test if the ccMaskRule is satisfied by the given condition code.
349 * Used to mask destination writes according to the current condition codee.
351 static INLINE GLboolean
352 test_cc(GLuint condCode
, GLuint ccMaskRule
)
354 switch (ccMaskRule
) {
355 case COND_EQ
: return (condCode
== COND_EQ
);
356 case COND_NE
: return (condCode
!= COND_EQ
);
357 case COND_LT
: return (condCode
== COND_LT
);
358 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
359 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
360 case COND_GT
: return (condCode
== COND_GT
);
361 case COND_TR
: return GL_TRUE
;
362 case COND_FL
: return GL_FALSE
;
363 default: return GL_TRUE
;
369 * Store 4 floats into a register. Observe the instructions saturate and
370 * set-condition-code flags.
373 store_vector4( const struct fp_instruction
*inst
,
374 struct fp_machine
*machine
,
375 const GLfloat value
[4] )
377 const struct fp_dst_register
*dest
= &(inst
->DstReg
);
378 const GLboolean clamp
= inst
->Saturate
;
379 const GLboolean updateCC
= inst
->UpdateCondRegister
;
381 GLfloat clampedValue
[4];
382 const GLboolean
*writeMask
= dest
->WriteMask
;
383 GLboolean condWriteMask
[4];
385 switch (dest
->File
) {
387 dstReg
= machine
->Outputs
[dest
->Index
];
389 case PROGRAM_TEMPORARY
:
390 dstReg
= machine
->Temporaries
[dest
->Index
];
393 _mesa_problem(NULL
, "bad register file in store_vector4(fp)");
398 if (value
[0] > 1.0e10
||
399 IS_INF_OR_NAN(value
[0]) ||
400 IS_INF_OR_NAN(value
[1]) ||
401 IS_INF_OR_NAN(value
[2]) ||
402 IS_INF_OR_NAN(value
[3]) )
403 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
407 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
408 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
409 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
410 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
411 value
= clampedValue
;
414 if (dest
->CondMask
!= COND_TR
) {
415 condWriteMask
[0] = writeMask
[0]
416 && test_cc(machine
->CondCodes
[dest
->CondSwizzle
[0]], dest
->CondMask
);
417 condWriteMask
[1] = writeMask
[1]
418 && test_cc(machine
->CondCodes
[dest
->CondSwizzle
[1]], dest
->CondMask
);
419 condWriteMask
[2] = writeMask
[2]
420 && test_cc(machine
->CondCodes
[dest
->CondSwizzle
[2]], dest
->CondMask
);
421 condWriteMask
[3] = writeMask
[3]
422 && test_cc(machine
->CondCodes
[dest
->CondSwizzle
[3]], dest
->CondMask
);
423 writeMask
= condWriteMask
;
427 dstReg
[0] = value
[0];
429 machine
->CondCodes
[0] = generate_cc(value
[0]);
432 dstReg
[1] = value
[1];
434 machine
->CondCodes
[1] = generate_cc(value
[1]);
437 dstReg
[2] = value
[2];
439 machine
->CondCodes
[2] = generate_cc(value
[2]);
442 dstReg
[3] = value
[3];
444 machine
->CondCodes
[3] = generate_cc(value
[3]);
450 * Initialize a new machine state instance from an existing one, adding
451 * the partial derivatives onto the input registers.
452 * Used to implement DDX and DDY instructions in non-trivial cases.
455 init_machine_deriv( GLcontext
*ctx
,
456 const struct fp_machine
*machine
,
457 const struct fragment_program
*program
,
458 const struct sw_span
*span
, char xOrY
,
459 struct fp_machine
*dMachine
)
463 ASSERT(xOrY
== 'X' || xOrY
== 'Y');
465 /* copy existing machine */
466 _mesa_memcpy(dMachine
, machine
, sizeof(struct fp_machine
));
468 /* Clear temporary registers */
469 _mesa_bzero( (void*) machine
->Temporaries
,
470 MAX_NV_FRAGMENT_PROGRAM_TEMPS
* 4 * sizeof(GLfloat
));
472 /* Add derivatives */
473 if (program
->InputsRead
& (1 << FRAG_ATTRIB_WPOS
)) {
474 GLfloat
*wpos
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_WPOS
];
478 wpos
[2] += span
->dzdx
;
479 wpos
[3] += span
->dwdx
;
484 wpos
[2] += span
->dzdy
;
485 wpos
[3] += span
->dwdy
;
488 if (program
->InputsRead
& (1 << FRAG_ATTRIB_COL0
)) {
489 GLfloat
*col0
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_COL0
];
491 col0
[0] += span
->drdx
* (1.0F
/ CHAN_MAXF
);
492 col0
[1] += span
->dgdx
* (1.0F
/ CHAN_MAXF
);
493 col0
[2] += span
->dbdx
* (1.0F
/ CHAN_MAXF
);
494 col0
[3] += span
->dadx
* (1.0F
/ CHAN_MAXF
);
497 col0
[0] += span
->drdy
* (1.0F
/ CHAN_MAXF
);
498 col0
[1] += span
->dgdy
* (1.0F
/ CHAN_MAXF
);
499 col0
[2] += span
->dbdy
* (1.0F
/ CHAN_MAXF
);
500 col0
[3] += span
->dady
* (1.0F
/ CHAN_MAXF
);
503 if (program
->InputsRead
& (1 << FRAG_ATTRIB_COL1
)) {
504 GLfloat
*col1
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_COL1
];
506 col1
[0] += span
->dsrdx
* (1.0F
/ CHAN_MAXF
);
507 col1
[1] += span
->dsgdx
* (1.0F
/ CHAN_MAXF
);
508 col1
[2] += span
->dsbdx
* (1.0F
/ CHAN_MAXF
);
509 col1
[3] += 0.0; /*XXX fix */
512 col1
[0] += span
->dsrdy
* (1.0F
/ CHAN_MAXF
);
513 col1
[1] += span
->dsgdy
* (1.0F
/ CHAN_MAXF
);
514 col1
[2] += span
->dsbdy
* (1.0F
/ CHAN_MAXF
);
515 col1
[3] += 0.0; /*XXX fix */
518 if (program
->InputsRead
& (1 << FRAG_ATTRIB_FOGC
)) {
519 GLfloat
*fogc
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_FOGC
];
521 fogc
[0] += span
->dfogdx
;
524 fogc
[0] += span
->dfogdy
;
527 for (u
= 0; u
< ctx
->Const
.MaxTextureCoordUnits
; u
++) {
528 if (program
->InputsRead
& (1 << (FRAG_ATTRIB_TEX0
+ u
))) {
529 GLfloat
*tex
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_TEX0
+ u
];
531 tex
[0] += span
->texStepX
[u
][0];
532 tex
[1] += span
->texStepX
[u
][1];
533 tex
[2] += span
->texStepX
[u
][2];
534 tex
[3] += span
->texStepX
[u
][3];
537 tex
[0] += span
->texStepY
[u
][0];
538 tex
[1] += span
->texStepY
[u
][1];
539 tex
[2] += span
->texStepY
[u
][2];
540 tex
[3] += span
->texStepY
[u
][3];
545 /* init condition codes */
546 dMachine
->CondCodes
[0] = COND_EQ
;
547 dMachine
->CondCodes
[1] = COND_EQ
;
548 dMachine
->CondCodes
[2] = COND_EQ
;
549 dMachine
->CondCodes
[3] = COND_EQ
;
554 * Execute the given vertex program.
555 * NOTE: we do everything in single-precision floating point; we don't
556 * currently observe the single/half/fixed-precision qualifiers.
557 * \param ctx - rendering context
558 * \param program - the fragment program to execute
559 * \param machine - machine state (register file)
560 * \param maxInst - max number of instructions to execute
561 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
564 execute_program( GLcontext
*ctx
,
565 const struct fragment_program
*program
, GLuint maxInst
,
566 struct fp_machine
*machine
, const struct sw_span
*span
,
572 printf("execute fragment program --------------------\n");
575 for (pc
= 0; pc
< maxInst
; pc
++) {
576 const struct fp_instruction
*inst
= program
->Instructions
+ pc
;
578 if (ctx
->FragmentProgram
.CallbackEnabled
&&
579 ctx
->FragmentProgram
.Callback
) {
580 ctx
->FragmentProgram
.CurrentPosition
= inst
->StringPos
;
581 ctx
->FragmentProgram
.Callback(program
->Base
.Target
,
582 ctx
->FragmentProgram
.CallbackData
);
585 switch (inst
->Opcode
) {
588 GLfloat a
[4], result
[4];
589 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
590 result
[0] = FABSF(a
[0]);
591 result
[1] = FABSF(a
[1]);
592 result
[2] = FABSF(a
[2]);
593 result
[3] = FABSF(a
[3]);
594 store_vector4( inst
, machine
, result
);
599 GLfloat a
[4], b
[4], result
[4];
600 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
601 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
602 result
[0] = a
[0] + b
[0];
603 result
[1] = a
[1] + b
[1];
604 result
[2] = a
[2] + b
[2];
605 result
[3] = a
[3] + b
[3];
606 store_vector4( inst
, machine
, result
);
611 GLfloat a
[4], b
[4], c
[4], result
[4];
612 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
613 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
614 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
615 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
616 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
617 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
618 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
619 store_vector4( inst
, machine
, result
);
624 GLfloat a
[4], result
[4];
625 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
626 result
[0] = result
[1] = result
[2] = result
[3] = (GLfloat
)_mesa_cos(a
[0]);
627 store_vector4( inst
, machine
, result
);
630 case FP_OPCODE_DDX
: /* Partial derivative with respect to X */
632 GLfloat a
[4], aNext
[4], result
[4];
633 struct fp_machine dMachine
;
634 if (!fetch_vector4_deriv(&inst
->SrcReg
[0], span
, 'X', result
)) {
635 /* This is tricky. Make a copy of the current machine state,
636 * increment the input registers by the dx or dy partial
637 * derivatives, then re-execute the program up to the
638 * preceeding instruction, then fetch the source register.
639 * Finally, find the difference in the register values for
640 * the original and derivative runs.
642 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
643 init_machine_deriv(ctx
, machine
, program
, span
,
645 execute_program(ctx
, program
, pc
, &dMachine
, span
, column
);
646 fetch_vector4( ctx
, &inst
->SrcReg
[0], &dMachine
, program
, aNext
);
647 result
[0] = aNext
[0] - a
[0];
648 result
[1] = aNext
[1] - a
[1];
649 result
[2] = aNext
[2] - a
[2];
650 result
[3] = aNext
[3] - a
[3];
652 store_vector4( inst
, machine
, result
);
655 case FP_OPCODE_DDY
: /* Partial derivative with respect to Y */
657 GLfloat a
[4], aNext
[4], result
[4];
658 struct fp_machine dMachine
;
659 if (!fetch_vector4_deriv(&inst
->SrcReg
[0], span
, 'Y', result
)) {
660 init_machine_deriv(ctx
, machine
, program
, span
,
662 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
663 execute_program(ctx
, program
, pc
, &dMachine
, span
, column
);
664 fetch_vector4( ctx
, &inst
->SrcReg
[0], &dMachine
, program
, aNext
);
665 result
[0] = aNext
[0] - a
[0];
666 result
[1] = aNext
[1] - a
[1];
667 result
[2] = aNext
[2] - a
[2];
668 result
[3] = aNext
[3] - 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] = result
[1] = result
[2] = result
[3] =
679 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2];
680 store_vector4( inst
, machine
, result
);
682 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
683 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
689 GLfloat a
[4], b
[4], result
[4];
690 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
691 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
692 result
[0] = result
[1] = result
[2] = result
[3] =
693 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + a
[3] * b
[3];
694 store_vector4( inst
, machine
, result
);
699 GLfloat a
[4], b
[4], result
[4];
700 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
701 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
702 result
[0] = result
[1] = result
[2] = result
[3] =
703 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + b
[3];
704 store_vector4( inst
, machine
, result
);
707 case FP_OPCODE_DST
: /* Distance vector */
709 GLfloat a
[4], b
[4], result
[4];
710 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
711 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
713 result
[1] = a
[1] * b
[1];
716 store_vector4( inst
, machine
, result
);
719 case FP_OPCODE_EX2
: /* Exponential base 2 */
721 GLfloat a
[4], result
[4];
722 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
723 result
[0] = result
[1] = result
[2] = result
[3] =
724 (GLfloat
) _mesa_pow(2.0, a
[0]);
725 store_vector4( inst
, machine
, result
);
730 GLfloat a
[4], result
[4];
731 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
732 result
[0] = FLOORF(a
[0]);
733 result
[1] = FLOORF(a
[1]);
734 result
[2] = FLOORF(a
[2]);
735 result
[3] = FLOORF(a
[3]);
736 store_vector4( inst
, machine
, result
);
741 GLfloat a
[4], result
[4];
742 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
743 result
[0] = a
[0] - FLOORF(a
[0]);
744 result
[1] = a
[1] - FLOORF(a
[1]);
745 result
[2] = a
[2] - FLOORF(a
[2]);
746 result
[3] = a
[3] - FLOORF(a
[3]);
747 store_vector4( inst
, machine
, result
);
752 const GLuint
*swizzle
= inst
->DstReg
.CondSwizzle
;
753 const GLuint condMask
= inst
->DstReg
.CondMask
;
754 if (test_cc(machine
->CondCodes
[swizzle
[0]], condMask
) ||
755 test_cc(machine
->CondCodes
[swizzle
[1]], condMask
) ||
756 test_cc(machine
->CondCodes
[swizzle
[2]], condMask
) ||
757 test_cc(machine
->CondCodes
[swizzle
[3]], condMask
)) {
762 case FP_OPCODE_LG2
: /* log base 2 */
764 GLfloat a
[4], result
[4];
765 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
766 result
[0] = result
[1] = result
[2] = result
[3]
768 store_vector4( inst
, machine
, result
);
773 GLfloat a
[4], result
[4];
774 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
781 result
[2] = (a
[0] > 0.0F
) ? (GLfloat
)_mesa_pow(2.0, a
[3]) : 0.0F
;
783 store_vector4( inst
, machine
, result
);
788 GLfloat a
[4], b
[4], c
[4], result
[4];
789 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
790 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
791 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
792 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
793 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
794 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
795 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
796 store_vector4( inst
, machine
, result
);
801 GLfloat a
[4], b
[4], c
[4], result
[4];
802 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
803 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
804 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
805 result
[0] = a
[0] * b
[0] + c
[0];
806 result
[1] = a
[1] * b
[1] + c
[1];
807 result
[2] = a
[2] * b
[2] + c
[2];
808 result
[3] = a
[3] * b
[3] + c
[3];
809 store_vector4( inst
, machine
, result
);
814 GLfloat a
[4], b
[4], result
[4];
815 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
816 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
817 result
[0] = MAX2(a
[0], b
[0]);
818 result
[1] = MAX2(a
[1], b
[1]);
819 result
[2] = MAX2(a
[2], b
[2]);
820 result
[3] = MAX2(a
[3], b
[3]);
821 store_vector4( inst
, machine
, result
);
826 GLfloat a
[4], b
[4], result
[4];
827 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
828 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
829 result
[0] = MIN2(a
[0], b
[0]);
830 result
[1] = MIN2(a
[1], b
[1]);
831 result
[2] = MIN2(a
[2], b
[2]);
832 result
[3] = MIN2(a
[3], b
[3]);
833 store_vector4( inst
, machine
, result
);
839 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, result
);
840 store_vector4( inst
, machine
, result
);
845 GLfloat a
[4], b
[4], result
[4];
846 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
847 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
848 result
[0] = a
[0] * b
[0];
849 result
[1] = a
[1] * b
[1];
850 result
[2] = a
[2] * b
[2];
851 result
[3] = a
[3] * b
[3];
852 store_vector4( inst
, machine
, result
);
854 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
855 result
[0], result
[1], result
[2], result
[3],
856 a
[0], a
[1], a
[2], a
[3],
857 b
[0], b
[1], b
[2], b
[3]);
861 case FP_OPCODE_PK2H
: /* pack two 16-bit floats */
862 /* XXX this is probably wrong */
864 GLfloat a
[4], result
[4];
865 const GLuint
*rawBits
= (const GLuint
*) a
;
866 GLuint
*rawResult
= (GLuint
*) result
;
867 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
868 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
869 = rawBits
[0] | (rawBits
[1] << 16);
870 store_vector4( inst
, machine
, result
);
873 case FP_OPCODE_PK2US
: /* pack two GLushorts */
875 GLfloat a
[4], result
[4];
876 GLuint usx
, usy
, *rawResult
= (GLuint
*) result
;
877 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
878 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
879 a
[1] = CLAMP(a
[0], 0.0F
, 1.0F
);
880 usx
= IROUND(a
[0] * 65535.0F
);
881 usy
= IROUND(a
[1] * 65535.0F
);
882 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
884 store_vector4( inst
, machine
, result
);
887 case FP_OPCODE_PK4B
: /* pack four GLbytes */
889 GLfloat a
[4], result
[4];
890 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
891 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
892 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
893 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
894 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
895 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
896 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
897 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
898 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
899 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
900 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
901 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
902 store_vector4( inst
, machine
, result
);
905 case FP_OPCODE_PK4UB
: /* pack four GLubytes */
907 GLfloat a
[4], result
[4];
908 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
909 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
910 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
911 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
912 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
913 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
914 ubx
= IROUND(255.0F
* a
[0]);
915 uby
= IROUND(255.0F
* a
[1]);
916 ubz
= IROUND(255.0F
* a
[2]);
917 ubw
= IROUND(255.0F
* a
[3]);
918 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
919 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
920 store_vector4( inst
, machine
, result
);
925 GLfloat a
[4], b
[4], result
[4];
926 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
927 fetch_vector1( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
928 result
[0] = result
[1] = result
[2] = result
[3]
929 = (GLfloat
)_mesa_pow(a
[0], b
[0]);
930 store_vector4( inst
, machine
, result
);
935 GLfloat a
[4], result
[4];
936 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
940 else if (IS_INF_OR_NAN(a
[0]))
941 printf("RCP(inf)\n");
943 result
[0] = result
[1] = result
[2] = result
[3]
945 store_vector4( inst
, machine
, result
);
950 GLfloat axis
[4], dir
[4], result
[4], tmp
[4];
951 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, axis
);
952 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, dir
);
953 tmp
[3] = axis
[0] * axis
[0]
956 tmp
[0] = (2.0F
* (axis
[0] * dir
[0] +
958 axis
[2] * dir
[2])) / tmp
[3];
959 result
[0] = tmp
[0] * axis
[0] - dir
[0];
960 result
[1] = tmp
[0] * axis
[1] - dir
[1];
961 result
[2] = tmp
[0] * axis
[2] - dir
[2];
962 /* result[3] is never written! XXX enforce in parser! */
963 store_vector4( inst
, machine
, result
);
966 case FP_OPCODE_RSQ
: /* 1 / sqrt() */
968 GLfloat a
[4], result
[4];
969 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
970 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
971 store_vector4( inst
, machine
, result
);
973 printf("RSQ %g = 1/sqrt(%g)\n", result
[0], a
[0]);
977 case FP_OPCODE_SCS
: /* sine and cos */
979 GLfloat a
[4], result
[4];
980 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
981 result
[0] = cos(a
[0]);
982 result
[1] = sin(a
[0]);
983 result
[2] = 0.0; /* undefined! */
984 result
[3] = 0.0; /* undefined! */
985 store_vector4( inst
, machine
, result
);
988 case FP_OPCODE_SEQ
: /* set on equal */
990 GLfloat a
[4], b
[4], result
[4];
991 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
992 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
993 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
994 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
995 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
996 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
997 store_vector4( inst
, machine
, result
);
1000 case FP_OPCODE_SFL
: /* set false, operands ignored */
1002 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1003 store_vector4( inst
, machine
, result
);
1006 case FP_OPCODE_SGE
: /* set on greater or equal */
1008 GLfloat a
[4], b
[4], result
[4];
1009 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1010 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1011 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1012 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1013 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1014 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1015 store_vector4( inst
, machine
, result
);
1018 case FP_OPCODE_SGT
: /* set on greater */
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
);
1032 GLfloat a
[4], result
[4];
1033 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1034 result
[0] = result
[1] = result
[2] =
1035 result
[3] = (GLfloat
)_mesa_sin(a
[0]);
1036 store_vector4( inst
, machine
, result
);
1039 case FP_OPCODE_SLE
: /* set on less or equal */
1041 GLfloat a
[4], b
[4], result
[4];
1042 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1043 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1044 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1045 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1046 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1047 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1048 store_vector4( inst
, machine
, result
);
1051 case FP_OPCODE_SLT
: /* set on less */
1053 GLfloat a
[4], b
[4], result
[4];
1054 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1055 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1056 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1057 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1058 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1059 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1060 store_vector4( inst
, machine
, result
);
1063 case FP_OPCODE_SNE
: /* set on not equal */
1065 GLfloat a
[4], b
[4], result
[4];
1066 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1067 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1068 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1069 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1070 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1071 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1072 store_vector4( inst
, machine
, result
);
1075 case FP_OPCODE_STR
: /* set true, operands ignored */
1077 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1078 store_vector4( inst
, machine
, result
);
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];
1087 result
[1] = a
[1] - b
[1];
1088 result
[2] = a
[2] - b
[2];
1089 result
[3] = a
[3] - b
[3];
1090 store_vector4( inst
, machine
, result
);
1095 const struct fp_src_register
*source
= &inst
->SrcReg
[0];
1096 const GLfloat
*src
= get_register_pointer(ctx
, source
,
1101 /* do extended swizzling here */
1102 for (i
= 0; i
< 3; i
++) {
1103 if (source
->Swizzle
[i
] == SWIZZLE_ZERO
)
1105 else if (source
->Swizzle
[i
] == SWIZZLE_ONE
)
1108 result
[i
] = -src
[source
->Swizzle
[i
]];
1109 if (source
->NegateBase
)
1110 result
[i
] = -result
[i
];
1112 store_vector4( inst
, machine
, result
);
1118 GLfloat texcoord
[4], color
[4];
1119 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1120 /* XXX: Undo perspective divide from interpolate_texcoords() */
1121 fetch_texel( ctx
, texcoord
,
1122 span
->array
->lambda
[inst
->TexSrcUnit
][column
],
1123 inst
->TexSrcUnit
, color
);
1124 store_vector4( inst
, machine
, color
);
1128 /* Texel lookup with LOD bias */
1130 GLfloat texcoord
[4], color
[4], bias
, lambda
;
1132 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1133 /* texcoord[3] is the bias to add to lambda */
1134 bias
= ctx
->Texture
.Unit
[inst
->TexSrcUnit
].LodBias
1135 + ctx
->Texture
.Unit
[inst
->TexSrcUnit
]._Current
->LodBias
1137 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
] + bias
;
1138 fetch_texel( ctx
, texcoord
, lambda
,
1139 inst
->TexSrcUnit
, color
);
1140 store_vector4( inst
, machine
, color
);
1144 /* Texture lookup w/ partial derivatives for LOD */
1146 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1147 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1148 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, dtdx
);
1149 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, dtdy
);
1150 fetch_texel_deriv( ctx
, texcoord
, dtdx
, dtdy
, inst
->TexSrcUnit
,
1152 store_vector4( inst
, machine
, color
);
1156 /* Texture lookup w/ perspective divide */
1158 GLfloat texcoord
[4], color
[4];
1159 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1160 /* Already did perspective divide in interpolate_texcoords() */
1161 fetch_texel( ctx
, texcoord
,
1162 span
->array
->lambda
[inst
->TexSrcUnit
][column
],
1163 inst
->TexSrcUnit
, color
);
1164 store_vector4( inst
, machine
, color
);
1167 case FP_OPCODE_UP2H
: /* unpack two 16-bit floats */
1168 /* XXX this is probably wrong */
1170 GLfloat a
[4], result
[4];
1171 const GLuint
*rawBits
= (const GLuint
*) a
;
1172 GLuint
*rawResult
= (GLuint
*) result
;
1173 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1174 rawResult
[0] = rawBits
[0] & 0xffff;
1175 rawResult
[1] = (rawBits
[0] >> 16) & 0xffff;
1176 rawResult
[2] = rawBits
[0] & 0xffff;
1177 rawResult
[3] = (rawBits
[0] >> 16) & 0xffff;
1178 store_vector4( inst
, machine
, result
);
1181 case FP_OPCODE_UP2US
: /* unpack two GLushorts */
1183 GLfloat a
[4], result
[4];
1184 const GLuint
*rawBits
= (const GLuint
*) a
;
1185 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1186 result
[0] = (GLfloat
) ((rawBits
[0] >> 0) & 0xffff) / 65535.0F
;
1187 result
[1] = (GLfloat
) ((rawBits
[0] >> 16) & 0xffff) / 65535.0F
;
1188 result
[2] = result
[0];
1189 result
[3] = result
[1];
1190 store_vector4( inst
, machine
, result
);
1193 case FP_OPCODE_UP4B
: /* unpack four GLbytes */
1195 GLfloat a
[4], result
[4];
1196 const GLuint
*rawBits
= (const GLuint
*) a
;
1197 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1198 result
[0] = (((rawBits
[0] >> 0) & 0xff) - 128) / 127.0F
;
1199 result
[0] = (((rawBits
[0] >> 8) & 0xff) - 128) / 127.0F
;
1200 result
[0] = (((rawBits
[0] >> 16) & 0xff) - 128) / 127.0F
;
1201 result
[0] = (((rawBits
[0] >> 24) & 0xff) - 128) / 127.0F
;
1202 store_vector4( inst
, machine
, result
);
1205 case FP_OPCODE_UP4UB
: /* unpack four GLubytes */
1207 GLfloat a
[4], result
[4];
1208 const GLuint
*rawBits
= (const GLuint
*) a
;
1209 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1210 result
[0] = ((rawBits
[0] >> 0) & 0xff) / 255.0F
;
1211 result
[0] = ((rawBits
[0] >> 8) & 0xff) / 255.0F
;
1212 result
[0] = ((rawBits
[0] >> 16) & 0xff) / 255.0F
;
1213 result
[0] = ((rawBits
[0] >> 24) & 0xff) / 255.0F
;
1214 store_vector4( inst
, machine
, result
);
1217 case FP_OPCODE_X2D
: /* 2-D matrix transform */
1219 GLfloat a
[4], b
[4], c
[4], result
[4];
1220 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1221 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1222 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
1223 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1224 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1225 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1226 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1227 store_vector4( inst
, machine
, result
);
1233 _mesa_problem(ctx
, "Bad opcode %d in _mesa_exec_fragment_program",
1235 return GL_TRUE
; /* return value doesn't matter */
1243 init_machine( GLcontext
*ctx
, struct fp_machine
*machine
,
1244 const struct fragment_program
*program
,
1245 const struct sw_span
*span
, GLuint col
)
1247 GLuint inputsRead
= program
->InputsRead
;
1250 if (ctx
->FragmentProgram
.CallbackEnabled
)
1253 /* Clear temporary registers */
1254 _mesa_bzero(machine
->Temporaries
,
1255 MAX_NV_FRAGMENT_PROGRAM_TEMPS
* 4 * sizeof(GLfloat
));
1257 /* Load input registers */
1258 if (inputsRead
& (1 << FRAG_ATTRIB_WPOS
)) {
1259 GLfloat
*wpos
= machine
->Inputs
[FRAG_ATTRIB_WPOS
];
1260 wpos
[0] = (GLfloat
) span
->x
+ col
;
1261 wpos
[1] = (GLfloat
) span
->y
;
1262 wpos
[2] = (GLfloat
) span
->array
->z
[col
] / ctx
->DepthMaxF
;
1263 wpos
[3] = span
->w
+ col
* span
->dwdx
;
1265 if (inputsRead
& (1 << FRAG_ATTRIB_COL0
)) {
1266 GLfloat
*col0
= machine
->Inputs
[FRAG_ATTRIB_COL0
];
1267 col0
[0] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][RCOMP
]);
1268 col0
[1] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][GCOMP
]);
1269 col0
[2] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][BCOMP
]);
1270 col0
[3] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][ACOMP
]);
1272 if (inputsRead
& (1 << FRAG_ATTRIB_COL1
)) {
1273 GLfloat
*col1
= machine
->Inputs
[FRAG_ATTRIB_COL1
];
1274 col1
[0] = CHAN_TO_FLOAT(span
->array
->spec
[col
][RCOMP
]);
1275 col1
[1] = CHAN_TO_FLOAT(span
->array
->spec
[col
][GCOMP
]);
1276 col1
[2] = CHAN_TO_FLOAT(span
->array
->spec
[col
][BCOMP
]);
1277 col1
[3] = CHAN_TO_FLOAT(span
->array
->spec
[col
][ACOMP
]);
1279 if (inputsRead
& (1 << FRAG_ATTRIB_FOGC
)) {
1280 GLfloat
*fogc
= machine
->Inputs
[FRAG_ATTRIB_FOGC
];
1281 fogc
[0] = span
->array
->fog
[col
];
1286 for (u
= 0; u
< ctx
->Const
.MaxTextureCoordUnits
; u
++) {
1287 if (inputsRead
& (1 << (FRAG_ATTRIB_TEX0
+ u
))) {
1288 GLfloat
*tex
= machine
->Inputs
[FRAG_ATTRIB_TEX0
+ u
];
1289 /*ASSERT(ctx->Texture._EnabledCoordUnits & (1 << u));*/
1290 COPY_4V(tex
, span
->array
->texcoords
[u
][col
]);
1291 /*ASSERT(tex[0] != 0 || tex[1] != 0 || tex[2] != 0);*/
1295 /* init condition codes */
1296 machine
->CondCodes
[0] = COND_EQ
;
1297 machine
->CondCodes
[1] = COND_EQ
;
1298 machine
->CondCodes
[2] = COND_EQ
;
1299 machine
->CondCodes
[3] = COND_EQ
;
1304 _swrast_exec_nv_fragment_program( GLcontext
*ctx
, struct sw_span
*span
)
1306 const struct fragment_program
*program
= ctx
->FragmentProgram
.Current
;
1309 ctx
->_CurrentProgram
= GL_FRAGMENT_PROGRAM_ARB
; /* or NV, doesn't matter */
1311 for (i
= 0; i
< span
->end
; i
++) {
1312 if (span
->array
->mask
[i
]) {
1313 init_machine(ctx
, &ctx
->FragmentProgram
.Machine
,
1314 ctx
->FragmentProgram
.Current
, span
, i
);
1316 if (!execute_program(ctx
, program
, ~0,
1317 &ctx
->FragmentProgram
.Machine
, span
, i
)) {
1318 span
->array
->mask
[i
] = GL_FALSE
; /* killed fragment */
1321 /* Store output registers */
1323 const GLfloat
*colOut
1324 = ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLR
];
1325 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][RCOMP
], colOut
[0]);
1326 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][GCOMP
], colOut
[1]);
1327 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][BCOMP
], colOut
[2]);
1328 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][ACOMP
], colOut
[3]);
1331 if (program
->OutputsWritten
& (1 << FRAG_OUTPUT_DEPR
))
1332 span
->array
->z
[i
] = IROUND(ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_DEPR
][0] * ctx
->DepthMaxF
);
1336 ctx
->_CurrentProgram
= 0;