1 /* $Id: s_nvfragprog.c,v 1.12 2003/03/25 02:23:47 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 #include "nvfragprog.h"
35 #include "s_nvfragprog.h"
37 #include "s_texture.h"
44 fetch_texel( GLcontext
*ctx
, const GLfloat texcoord
[4], GLuint unit
,
47 const GLfloat
*lambda
= NULL
;
49 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
51 swrast
->TextureSample
[unit
](ctx
, unit
, ctx
->Texture
.Unit
[unit
]._Current
,
52 1, (const GLfloat (*)[4]) texcoord
,
54 color
[0] = CHAN_TO_FLOAT(rgba
[0]);
55 color
[1] = CHAN_TO_FLOAT(rgba
[1]);
56 color
[2] = CHAN_TO_FLOAT(rgba
[2]);
57 color
[3] = CHAN_TO_FLOAT(rgba
[3]);
62 * Fetch a texel with the given partial derivatives to compute a level
63 * of detail in the mipmap.
66 fetch_texel_deriv( GLcontext
*ctx
, const GLfloat texcoord
[4],
67 const GLfloat texdx
[4], const GLfloat texdy
[4],
68 GLuint unit
, GLfloat color
[4] )
70 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
71 const struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[unit
]._Current
;
72 const struct gl_texture_image
*texImg
= texObj
->Image
[texObj
->BaseLevel
];
73 const GLfloat texW
= (GLfloat
) texImg
->WidthScale
;
74 const GLfloat texH
= (GLfloat
) texImg
->HeightScale
;
77 GLfloat lambda
= _swrast_compute_lambda(texdx
[0], texdy
[0], /* ds/dx, ds/dy */
78 texdx
[1], texdy
[1], /* dt/dx, dt/dy */
79 texdx
[3], texdy
[2], /* dq/dx, dq/dy */
81 texcoord
[0], texcoord
[1], texcoord
[3],
84 swrast
->TextureSample
[unit
](ctx
, unit
, ctx
->Texture
.Unit
[unit
]._Current
,
85 1, (const GLfloat (*)[4]) texcoord
,
87 color
[0] = CHAN_TO_FLOAT(rgba
[0]);
88 color
[1] = CHAN_TO_FLOAT(rgba
[1]);
89 color
[2] = CHAN_TO_FLOAT(rgba
[2]);
90 color
[3] = CHAN_TO_FLOAT(rgba
[3]);
96 * Fetch a 4-element float vector from the given source register.
97 * Apply swizzling and negating as needed.
100 fetch_vector4( const struct fp_src_register
*source
,
101 const struct fp_machine
*machine
,
104 const GLfloat
*src
= machine
->Registers
[source
->Register
];
106 result
[0] = src
[source
->Swizzle
[0]];
107 result
[1] = src
[source
->Swizzle
[1]];
108 result
[2] = src
[source
->Swizzle
[2]];
109 result
[3] = src
[source
->Swizzle
[3]];
111 if (source
->NegateBase
) {
112 result
[0] = -result
[0];
113 result
[1] = -result
[1];
114 result
[2] = -result
[2];
115 result
[3] = -result
[3];
118 result
[0] = FABSF(result
[0]);
119 result
[1] = FABSF(result
[1]);
120 result
[2] = FABSF(result
[2]);
121 result
[3] = FABSF(result
[3]);
123 if (source
->NegateAbs
) {
124 result
[0] = -result
[0];
125 result
[1] = -result
[1];
126 result
[2] = -result
[2];
127 result
[3] = -result
[3];
133 * Fetch the derivative with respect to X for the given register.
134 * \return GL_TRUE if it was easily computed or GL_FALSE if we
135 * need to execute another instance of the program (ugh)!
138 fetch_vector4_deriv( const struct fp_src_register
*source
,
139 const struct sw_span
*span
,
140 char xOrY
, GLfloat result
[4] )
144 ASSERT(xOrY
== 'X' || xOrY
== 'Y');
146 switch (source
->Register
) {
147 case FRAG_ATTRIB_WPOS
:
161 case FRAG_ATTRIB_COL0
:
163 src
[0] = span
->drdx
* (1.0F
/ CHAN_MAXF
);
164 src
[1] = span
->dgdx
* (1.0F
/ CHAN_MAXF
);
165 src
[2] = span
->dbdx
* (1.0F
/ CHAN_MAXF
);
166 src
[3] = span
->dadx
* (1.0F
/ CHAN_MAXF
);
169 src
[0] = span
->drdy
* (1.0F
/ CHAN_MAXF
);
170 src
[1] = span
->dgdy
* (1.0F
/ CHAN_MAXF
);
171 src
[2] = span
->dbdy
* (1.0F
/ CHAN_MAXF
);
172 src
[3] = span
->dady
* (1.0F
/ CHAN_MAXF
);
175 case FRAG_ATTRIB_COL1
:
177 src
[0] = span
->dsrdx
* (1.0F
/ CHAN_MAXF
);
178 src
[1] = span
->dsgdx
* (1.0F
/ CHAN_MAXF
);
179 src
[2] = span
->dsbdx
* (1.0F
/ CHAN_MAXF
);
180 src
[3] = 0.0; /* XXX need this */
183 src
[0] = span
->dsrdy
* (1.0F
/ CHAN_MAXF
);
184 src
[1] = span
->dsgdy
* (1.0F
/ CHAN_MAXF
);
185 src
[2] = span
->dsbdy
* (1.0F
/ CHAN_MAXF
);
186 src
[3] = 0.0; /* XXX need this */
189 case FRAG_ATTRIB_FOGC
:
191 src
[0] = span
->dfogdx
;
197 src
[0] = span
->dfogdy
;
203 case FRAG_ATTRIB_TEX0
:
204 case FRAG_ATTRIB_TEX1
:
205 case FRAG_ATTRIB_TEX2
:
206 case FRAG_ATTRIB_TEX3
:
207 case FRAG_ATTRIB_TEX4
:
208 case FRAG_ATTRIB_TEX5
:
209 case FRAG_ATTRIB_TEX6
:
210 case FRAG_ATTRIB_TEX7
:
212 const GLuint u
= source
->Register
- FRAG_ATTRIB_TEX0
;
213 src
[0] = span
->texStepX
[u
][0] * (1.0F
/ CHAN_MAXF
);
214 src
[1] = span
->texStepX
[u
][1] * (1.0F
/ CHAN_MAXF
);
215 src
[2] = span
->texStepX
[u
][2] * (1.0F
/ CHAN_MAXF
);
216 src
[3] = span
->texStepX
[u
][3] * (1.0F
/ CHAN_MAXF
);
219 const GLuint u
= source
->Register
- FRAG_ATTRIB_TEX0
;
220 src
[0] = span
->texStepY
[u
][0] * (1.0F
/ CHAN_MAXF
);
221 src
[1] = span
->texStepY
[u
][1] * (1.0F
/ CHAN_MAXF
);
222 src
[2] = span
->texStepY
[u
][2] * (1.0F
/ CHAN_MAXF
);
223 src
[3] = span
->texStepY
[u
][3] * (1.0F
/ CHAN_MAXF
);
230 result
[0] = src
[source
->Swizzle
[0]];
231 result
[1] = src
[source
->Swizzle
[1]];
232 result
[2] = src
[source
->Swizzle
[2]];
233 result
[3] = src
[source
->Swizzle
[3]];
235 if (source
->NegateBase
) {
236 result
[0] = -result
[0];
237 result
[1] = -result
[1];
238 result
[2] = -result
[2];
239 result
[3] = -result
[3];
242 result
[0] = FABSF(result
[0]);
243 result
[1] = FABSF(result
[1]);
244 result
[2] = FABSF(result
[2]);
245 result
[3] = FABSF(result
[3]);
247 if (source
->NegateAbs
) {
248 result
[0] = -result
[0];
249 result
[1] = -result
[1];
250 result
[2] = -result
[2];
251 result
[3] = -result
[3];
258 * As above, but only return result[0] element.
261 fetch_vector1( const struct fp_src_register
*source
,
262 const struct fp_machine
*machine
,
265 const GLfloat
*src
= machine
->Registers
[source
->Register
];
267 result
[0] = src
[source
->Swizzle
[0]];
269 if (source
->NegateBase
) {
270 result
[0] = -result
[0];
273 result
[0] = FABSF(result
[0]);
275 if (source
->NegateAbs
) {
276 result
[0] = -result
[0];
282 * Test value against zero and return GT, LT, EQ or UN if NaN.
285 generate_cc( float value
)
288 return COND_UN
; /* NaN */
297 * Test if the ccMaskRule is satisfied by the given condition code.
298 * Used to mask destination writes according to the current condition codee.
300 static INLINE GLboolean
301 test_cc(GLuint condCode
, GLuint ccMaskRule
)
303 switch (ccMaskRule
) {
304 case COND_EQ
: return (condCode
== COND_EQ
);
305 case COND_NE
: return (condCode
!= COND_EQ
);
306 case COND_LT
: return (condCode
== COND_LT
);
307 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
308 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
309 case COND_GT
: return (condCode
== COND_GT
);
310 case COND_TR
: return GL_TRUE
;
311 case COND_FL
: return GL_FALSE
;
312 default: return GL_TRUE
;
318 * Store 4 floats into a register. Observe the instructions saturate and
319 * set-condition-code flags.
322 store_vector4( const struct fp_instruction
*inst
,
323 struct fp_machine
*machine
,
324 const GLfloat value
[4] )
326 const struct fp_dst_register
*dest
= &(inst
->DstReg
);
327 const GLboolean clamp
= inst
->Saturate
;
328 const GLboolean updateCC
= inst
->UpdateCondRegister
;
329 GLfloat
*dstReg
= machine
->Registers
[dest
->Register
];
330 GLfloat clampedValue
[4];
331 const GLboolean
*writeMask
= dest
->WriteMask
;
332 GLboolean condWriteMask
[4];
335 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
336 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
337 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
338 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
339 value
= clampedValue
;
342 if (dest
->CondMask
!= COND_TR
) {
343 condWriteMask
[0] = writeMask
[0]
344 && test_cc(machine
->CondCodes
[dest
->CondSwizzle
[0]], dest
->CondMask
);
345 condWriteMask
[1] = writeMask
[1]
346 && test_cc(machine
->CondCodes
[dest
->CondSwizzle
[1]], dest
->CondMask
);
347 condWriteMask
[2] = writeMask
[2]
348 && test_cc(machine
->CondCodes
[dest
->CondSwizzle
[2]], dest
->CondMask
);
349 condWriteMask
[3] = writeMask
[3]
350 && test_cc(machine
->CondCodes
[dest
->CondSwizzle
[3]], dest
->CondMask
);
351 writeMask
= condWriteMask
;
355 dstReg
[0] = value
[0];
357 machine
->CondCodes
[0] = generate_cc(value
[0]);
360 dstReg
[1] = value
[1];
362 machine
->CondCodes
[1] = generate_cc(value
[1]);
365 dstReg
[2] = value
[2];
367 machine
->CondCodes
[2] = generate_cc(value
[2]);
370 dstReg
[3] = value
[3];
372 machine
->CondCodes
[3] = generate_cc(value
[3]);
378 * Initialize a new machine state instance from an existing one, adding
379 * the partial derivatives onto the input registers.
380 * Used to implement DDX and DDY instructions in non-trivial cases.
383 init_machine_deriv( GLcontext
*ctx
,
384 const struct fp_machine
*machine
,
385 const struct fragment_program
*program
,
386 const struct sw_span
*span
, char xOrY
,
387 struct fp_machine
*dMachine
)
391 ASSERT(xOrY
== 'X' || xOrY
== 'Y');
393 /* copy existing machine */
394 _mesa_memcpy(dMachine
, machine
, sizeof(struct fp_machine
));
396 /* Clear temporary registers */
397 _mesa_bzero((GLfloat
*) (machine
->Registers
+ FP_TEMP_REG_START
) ,
398 MAX_NV_FRAGMENT_PROGRAM_TEMPS
* 4 * sizeof(GLfloat
));
400 /* Add derivatives */
401 if (program
->InputsRead
& (1 << FRAG_ATTRIB_WPOS
)) {
402 GLfloat
*wpos
= (GLfloat
*) machine
->Registers
[FP_INPUT_REG_START
+FRAG_ATTRIB_WPOS
];
406 wpos
[2] += span
->dzdx
;
407 wpos
[3] += span
->dwdx
;
412 wpos
[2] += span
->dzdy
;
413 wpos
[3] += span
->dwdy
;
416 if (program
->InputsRead
& (1 << FRAG_ATTRIB_COL0
)) {
417 GLfloat
*col0
= (GLfloat
*) machine
->Registers
[FP_INPUT_REG_START
+FRAG_ATTRIB_COL0
];
419 col0
[0] += span
->drdx
* (1.0F
/ CHAN_MAXF
);
420 col0
[1] += span
->dgdx
* (1.0F
/ CHAN_MAXF
);
421 col0
[2] += span
->dbdx
* (1.0F
/ CHAN_MAXF
);
422 col0
[3] += span
->dadx
* (1.0F
/ CHAN_MAXF
);
425 col0
[0] += span
->drdy
* (1.0F
/ CHAN_MAXF
);
426 col0
[1] += span
->dgdy
* (1.0F
/ CHAN_MAXF
);
427 col0
[2] += span
->dbdy
* (1.0F
/ CHAN_MAXF
);
428 col0
[3] += span
->dady
* (1.0F
/ CHAN_MAXF
);
431 if (program
->InputsRead
& (1 << FRAG_ATTRIB_COL1
)) {
432 GLfloat
*col1
= (GLfloat
*) machine
->Registers
[FP_INPUT_REG_START
+FRAG_ATTRIB_COL1
];
434 col1
[0] += span
->dsrdx
* (1.0F
/ CHAN_MAXF
);
435 col1
[1] += span
->dsgdx
* (1.0F
/ CHAN_MAXF
);
436 col1
[2] += span
->dsbdx
* (1.0F
/ CHAN_MAXF
);
437 col1
[3] += 0.0; /*XXX fix */
440 col1
[0] += span
->dsrdy
* (1.0F
/ CHAN_MAXF
);
441 col1
[1] += span
->dsgdy
* (1.0F
/ CHAN_MAXF
);
442 col1
[2] += span
->dsbdy
* (1.0F
/ CHAN_MAXF
);
443 col1
[3] += 0.0; /*XXX fix */
446 if (program
->InputsRead
& (1 << FRAG_ATTRIB_FOGC
)) {
447 GLfloat
*fogc
= (GLfloat
*) machine
->Registers
[FP_INPUT_REG_START
+FRAG_ATTRIB_FOGC
];
449 fogc
[0] += span
->dfogdx
;
452 fogc
[0] += span
->dfogdy
;
455 for (u
= 0; u
< ctx
->Const
.MaxTextureCoordUnits
; u
++) {
456 if (program
->InputsRead
& (1 << (FRAG_ATTRIB_TEX0
+ u
))) {
457 GLfloat
*tex
= (GLfloat
*) machine
->Registers
[FP_INPUT_REG_START
+FRAG_ATTRIB_TEX0
+u
];
459 tex
[0] += span
->texStepX
[u
][0];
460 tex
[1] += span
->texStepX
[u
][1];
461 tex
[2] += span
->texStepX
[u
][2];
462 tex
[3] += span
->texStepX
[u
][3];
465 tex
[0] += span
->texStepY
[u
][0];
466 tex
[1] += span
->texStepY
[u
][1];
467 tex
[2] += span
->texStepY
[u
][2];
468 tex
[3] += span
->texStepY
[u
][3];
476 * Execute the given vertex program.
477 * NOTE: we do everything in single-precision floating point; we don't
478 * currently observe the single/half/fixed-precision qualifiers.
479 * \param ctx - rendering context
480 * \param program - the fragment program to execute
481 * \param machine - machine state (register file)
482 * \param maxInst - max number of instructions to execute
483 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
486 execute_program( GLcontext
*ctx
,
487 const struct fragment_program
*program
, GLuint maxInst
,
488 struct fp_machine
*machine
, const struct sw_span
*span
)
492 for (pc
= 0; pc
< maxInst
; pc
++) {
493 const struct fp_instruction
*inst
= program
->Instructions
+ pc
;
494 switch (inst
->Opcode
) {
497 GLfloat a
[4], b
[4], result
[4];
498 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
499 fetch_vector4( &inst
->SrcReg
[1], machine
, b
);
500 result
[0] = a
[0] + b
[0];
501 result
[1] = a
[1] + b
[1];
502 result
[2] = a
[2] + b
[2];
503 result
[3] = a
[3] + b
[3];
504 store_vector4( inst
, machine
, result
);
509 GLfloat a
[4], result
[4];
510 fetch_vector1( &inst
->SrcReg
[0], machine
, a
);
511 result
[0] = result
[1] = result
[2] = result
[3] = _mesa_cos(a
[0]);
512 store_vector4( inst
, machine
, result
);
515 case FP_OPCODE_DDX
: /* Partial derivative with respect to X */
517 GLfloat a
[4], aNext
[4], result
[4];
518 struct fp_machine dMachine
;
519 if (!fetch_vector4_deriv(&inst
->SrcReg
[0], span
, 'X', result
)) {
520 /* This is tricky. Make a copy of the current machine state,
521 * increment the input registers by the dx or dy partial
522 * derivatives, then re-execute the program up to the
523 * preceeding instruction, then fetch the source register.
524 * Finally, find the difference in the register values for
525 * the original and derivative runs.
527 init_machine_deriv(ctx
, machine
, program
, span
,
529 execute_program(ctx
, program
, pc
, &dMachine
, span
);
530 fetch_vector4( &inst
->SrcReg
[0], &dMachine
, aNext
);
531 result
[0] = aNext
[0] - a
[0];
532 result
[1] = aNext
[1] - a
[1];
533 result
[2] = aNext
[2] - a
[2];
534 result
[3] = aNext
[3] - a
[3];
536 store_vector4( inst
, machine
, result
);
539 case FP_OPCODE_DDY
: /* Partial derivative with respect to Y */
541 GLfloat a
[4], aNext
[4], result
[4];
542 struct fp_machine dMachine
;
543 if (!fetch_vector4_deriv(&inst
->SrcReg
[0], span
, 'Y', result
)) {
544 init_machine_deriv(ctx
, machine
, program
, span
,
546 execute_program(ctx
, program
, pc
, &dMachine
, span
);
547 fetch_vector4( &inst
->SrcReg
[0], &dMachine
, aNext
);
548 result
[0] = aNext
[0] - a
[0];
549 result
[1] = aNext
[1] - a
[1];
550 result
[2] = aNext
[2] - a
[2];
551 result
[3] = aNext
[3] - a
[3];
553 store_vector4( inst
, machine
, result
);
558 GLfloat a
[4], b
[4], result
[4];
559 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
560 fetch_vector4( &inst
->SrcReg
[1], machine
, b
);
561 result
[0] = result
[1] = result
[2] = result
[3] =
562 a
[0] + b
[0] + a
[1] * b
[1] + a
[2] * b
[2];
563 store_vector4( inst
, machine
, result
);
568 GLfloat a
[4], b
[4], result
[4];
569 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
570 fetch_vector4( &inst
->SrcReg
[1], machine
, b
);
571 result
[0] = result
[1] = result
[2] = result
[3] =
572 a
[0] + b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + a
[3] * b
[3];
573 store_vector4( inst
, machine
, result
);
576 case FP_OPCODE_DST
: /* Distance vector */
578 GLfloat a
[4], b
[4], result
[4];
579 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
580 fetch_vector4( &inst
->SrcReg
[1], machine
, b
);
582 result
[1] = a
[1] * b
[1];
585 store_vector4( inst
, machine
, result
);
588 case FP_OPCODE_EX2
: /* Exponential base 2 */
590 GLfloat a
[4], result
[4];
591 fetch_vector1( &inst
->SrcReg
[0], machine
, a
);
592 result
[0] = result
[1] = result
[2] = result
[3] =
593 (GLfloat
) _mesa_pow(2.0, a
[0]);
594 store_vector4( inst
, machine
, result
);
599 GLfloat a
[4], result
[4];
600 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
601 result
[0] = FLOORF(a
[0]);
602 result
[1] = FLOORF(a
[1]);
603 result
[2] = FLOORF(a
[2]);
604 result
[3] = FLOORF(a
[3]);
605 store_vector4( inst
, machine
, result
);
610 GLfloat a
[4], result
[4];
611 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
612 result
[0] = a
[0] - FLOORF(a
[0]);
613 result
[1] = a
[1] - FLOORF(a
[1]);
614 result
[2] = a
[2] - FLOORF(a
[2]);
615 result
[3] = a
[3] - FLOORF(a
[3]);
616 store_vector4( inst
, machine
, result
);
621 const GLuint
*swizzle
= inst
->DstReg
.CondSwizzle
;
622 const GLuint condMask
= inst
->DstReg
.CondMask
;
623 if (test_cc(machine
->CondCodes
[swizzle
[0]], condMask
) ||
624 test_cc(machine
->CondCodes
[swizzle
[1]], condMask
) ||
625 test_cc(machine
->CondCodes
[swizzle
[2]], condMask
) ||
626 test_cc(machine
->CondCodes
[swizzle
[3]], condMask
))
630 case FP_OPCODE_LG2
: /* log base 2 */
632 GLfloat a
[4], result
[4];
633 fetch_vector1( &inst
->SrcReg
[0], machine
, a
);
634 result
[0] = result
[1] = result
[2] = result
[3]
636 store_vector4( inst
, machine
, result
);
641 GLfloat a
[4], result
[4];
642 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
649 result
[2] = (a
[0] > 0.0) ? _mesa_pow(2.0, a
[3]) : 0.0F
;
651 store_vector4( inst
, machine
, result
);
656 GLfloat a
[4], b
[4], c
[4], result
[4];
657 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
658 fetch_vector4( &inst
->SrcReg
[1], machine
, b
);
659 fetch_vector4( &inst
->SrcReg
[2], machine
, c
);
660 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
661 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
662 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
663 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
664 store_vector4( inst
, machine
, result
);
669 GLfloat a
[4], b
[4], c
[4], result
[4];
670 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
671 fetch_vector4( &inst
->SrcReg
[1], machine
, b
);
672 fetch_vector4( &inst
->SrcReg
[2], machine
, c
);
673 result
[0] = a
[0] * b
[0] + c
[0];
674 result
[1] = a
[1] * b
[1] + c
[1];
675 result
[2] = a
[2] * b
[2] + c
[2];
676 result
[3] = a
[3] * b
[3] + c
[3];
677 store_vector4( inst
, machine
, result
);
682 GLfloat a
[4], b
[4], result
[4];
683 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
684 fetch_vector4( &inst
->SrcReg
[1], machine
, b
);
685 result
[0] = MAX2(a
[0], b
[0]);
686 result
[1] = MAX2(a
[1], b
[1]);
687 result
[2] = MAX2(a
[2], b
[2]);
688 result
[3] = MAX2(a
[3], b
[3]);
689 store_vector4( inst
, machine
, result
);
694 GLfloat a
[4], b
[4], result
[4];
695 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
696 fetch_vector4( &inst
->SrcReg
[1], machine
, b
);
697 result
[0] = MIN2(a
[0], b
[0]);
698 result
[1] = MIN2(a
[1], b
[1]);
699 result
[2] = MIN2(a
[2], b
[2]);
700 result
[3] = MIN2(a
[3], b
[3]);
701 store_vector4( inst
, machine
, result
);
707 fetch_vector4( &inst
->SrcReg
[0], machine
, result
);
708 store_vector4( inst
, machine
, result
);
713 GLfloat a
[4], b
[4], result
[4];
714 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
715 fetch_vector4( &inst
->SrcReg
[1], machine
, b
);
716 result
[0] = a
[0] * b
[0];
717 result
[1] = a
[1] * b
[1];
718 result
[2] = a
[2] * b
[2];
719 result
[3] = a
[3] * b
[3];
720 store_vector4( inst
, machine
, result
);
723 case FP_OPCODE_PK2H
: /* pack two 16-bit floats */
724 /* XXX this is probably wrong */
726 GLfloat a
[4], result
[4];
727 const GLuint
*rawBits
= (const GLuint
*) a
;
728 GLuint
*rawResult
= (GLuint
*) result
;
729 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
730 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
731 = rawBits
[0] | (rawBits
[1] << 16);
732 store_vector4( inst
, machine
, result
);
735 case FP_OPCODE_PK2US
: /* pack two GLushorts */
737 GLfloat a
[4], result
[4];
738 GLuint usx
, usy
, *rawResult
= (GLuint
*) result
;
739 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
740 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
741 a
[1] = CLAMP(a
[0], 0.0F
, 1.0F
);
742 usx
= IROUND(a
[0] * 65535.0F
);
743 usy
= IROUND(a
[1] * 65535.0F
);
744 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
746 store_vector4( inst
, machine
, result
);
749 case FP_OPCODE_PK4B
: /* pack four GLbytes */
751 GLfloat a
[4], result
[4];
752 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
753 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
754 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
755 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
756 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
757 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
758 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
759 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
760 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
761 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
762 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
763 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
764 store_vector4( inst
, machine
, result
);
767 case FP_OPCODE_PK4UB
: /* pack four GLubytes */
769 GLfloat a
[4], result
[4];
770 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
771 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
772 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
773 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
774 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
775 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
776 ubx
= IROUND(255.0F
* a
[0]);
777 uby
= IROUND(255.0F
* a
[1]);
778 ubz
= IROUND(255.0F
* a
[2]);
779 ubw
= IROUND(255.0F
* a
[3]);
780 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
781 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
782 store_vector4( inst
, machine
, result
);
787 GLfloat a
[4], b
[4], result
[4];
788 fetch_vector1( &inst
->SrcReg
[0], machine
, a
);
789 fetch_vector1( &inst
->SrcReg
[1], machine
, b
);
790 result
[0] = result
[1] = result
[2] = result
[3]
791 = _mesa_pow(a
[0], b
[0]);
792 store_vector4( inst
, machine
, result
);
797 GLfloat a
[4], result
[4];
798 fetch_vector1( &inst
->SrcReg
[0], machine
, a
);
799 result
[0] = result
[1] = result
[2] = result
[3]
801 store_vector4( inst
, machine
, result
);
806 GLfloat axis
[4], dir
[4], result
[4], tmp
[4];
807 fetch_vector4( &inst
->SrcReg
[0], machine
, axis
);
808 fetch_vector4( &inst
->SrcReg
[1], machine
, dir
);
809 tmp
[3] = axis
[0] * axis
[0]
812 tmp
[0] = (2.0F
* (axis
[0] * dir
[0] +
814 axis
[2] * dir
[2])) / tmp
[3];
815 result
[0] = tmp
[0] * axis
[0] - dir
[0];
816 result
[1] = tmp
[0] * axis
[1] - dir
[1];
817 result
[2] = tmp
[0] * axis
[2] - dir
[2];
818 /* result[3] is never written! XXX enforce in parser! */
819 store_vector4( inst
, machine
, result
);
822 case FP_OPCODE_RSQ
: /* 1 / sqrt() */
824 GLfloat a
[4], result
[4];
825 fetch_vector1( &inst
->SrcReg
[0], machine
, a
);
826 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
827 store_vector4( inst
, machine
, result
);
830 case FP_OPCODE_SEQ
: /* set on equal */
832 GLfloat a
[4], b
[4], result
[4];
833 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
834 fetch_vector4( &inst
->SrcReg
[1], machine
, b
);
835 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
836 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
837 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
838 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
839 store_vector4( inst
, machine
, result
);
842 case FP_OPCODE_SFL
: /* set false, operands ignored */
844 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
845 store_vector4( inst
, machine
, result
);
848 case FP_OPCODE_SGE
: /* set on greater or equal */
850 GLfloat a
[4], b
[4], result
[4];
851 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
852 fetch_vector4( &inst
->SrcReg
[1], machine
, b
);
853 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
854 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
855 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
856 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
857 store_vector4( inst
, machine
, result
);
860 case FP_OPCODE_SGT
: /* set on greater */
862 GLfloat a
[4], b
[4], result
[4];
863 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
864 fetch_vector4( &inst
->SrcReg
[1], machine
, b
);
865 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
866 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
867 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
868 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
869 store_vector4( inst
, machine
, result
);
874 GLfloat a
[4], result
[4];
875 fetch_vector1( &inst
->SrcReg
[0], machine
, a
);
876 result
[0] = result
[1] = result
[2] = result
[3] = _mesa_sin(a
[0]);
877 store_vector4( inst
, machine
, result
);
880 case FP_OPCODE_SLE
: /* set on less or equal */
882 GLfloat a
[4], b
[4], result
[4];
883 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
884 fetch_vector4( &inst
->SrcReg
[1], machine
, b
);
885 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
886 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
887 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
888 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
889 store_vector4( inst
, machine
, result
);
892 case FP_OPCODE_SLT
: /* set on less */
894 GLfloat a
[4], b
[4], result
[4];
895 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
896 fetch_vector4( &inst
->SrcReg
[1], machine
, b
);
897 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
898 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
899 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
900 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
901 store_vector4( inst
, machine
, result
);
904 case FP_OPCODE_SNE
: /* set on not equal */
906 GLfloat a
[4], b
[4], result
[4];
907 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
908 fetch_vector4( &inst
->SrcReg
[1], machine
, b
);
909 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
910 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
911 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
912 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
913 store_vector4( inst
, machine
, result
);
916 case FP_OPCODE_STR
: /* set true, operands ignored */
918 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
919 store_vector4( inst
, machine
, result
);
924 GLfloat a
[4], b
[4], result
[4];
925 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
926 fetch_vector4( &inst
->SrcReg
[1], machine
, b
);
927 result
[0] = a
[0] - b
[0];
928 result
[1] = a
[1] - b
[1];
929 result
[2] = a
[2] - b
[2];
930 result
[3] = a
[3] - b
[3];
931 store_vector4( inst
, machine
, result
);
937 GLfloat texcoord
[4], color
[4];
938 fetch_vector4( &inst
->SrcReg
[0], machine
, texcoord
);
939 /* XXX: Undo perspective divide from interpolate_texcoords() */
940 fetch_texel( ctx
, texcoord
, inst
->TexSrcUnit
, color
);
941 store_vector4( inst
, machine
, color
);
945 /* Texture lookup w/ partial derivatives for LOD */
947 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
948 fetch_vector4( &inst
->SrcReg
[0], machine
, texcoord
);
949 fetch_vector4( &inst
->SrcReg
[1], machine
, dtdx
);
950 fetch_vector4( &inst
->SrcReg
[2], machine
, dtdy
);
951 fetch_texel_deriv( ctx
, texcoord
, dtdx
, dtdy
, inst
->TexSrcUnit
,
953 store_vector4( inst
, machine
, color
);
957 /* Texture lookup w/ perspective divide */
959 GLfloat texcoord
[4], color
[4];
960 fetch_vector4( &inst
->SrcReg
[0], machine
, texcoord
);
961 /* Already did perspective divide in interpolate_texcoords() */
962 fetch_texel( ctx
, texcoord
, inst
->TexSrcUnit
, color
);
963 store_vector4( inst
, machine
, color
);
966 case FP_OPCODE_UP2H
: /* unpack two 16-bit floats */
967 /* XXX this is probably wrong */
969 GLfloat a
[4], result
[4];
970 const GLuint
*rawBits
= (const GLuint
*) a
;
971 GLuint
*rawResult
= (GLuint
*) result
;
972 fetch_vector1( &inst
->SrcReg
[0], machine
, a
);
973 rawResult
[0] = rawBits
[0] & 0xffff;
974 rawResult
[1] = (rawBits
[0] >> 16) & 0xffff;
975 rawResult
[2] = rawBits
[0] & 0xffff;
976 rawResult
[3] = (rawBits
[0] >> 16) & 0xffff;
977 store_vector4( inst
, machine
, result
);
980 case FP_OPCODE_UP2US
: /* unpack two GLushorts */
982 GLfloat a
[4], result
[4];
983 const GLuint
*rawBits
= (const GLuint
*) a
;
984 fetch_vector1( &inst
->SrcReg
[0], machine
, a
);
985 result
[0] = (GLfloat
) ((rawBits
[0] >> 0) & 0xffff) / 65535.0F
;
986 result
[1] = (GLfloat
) ((rawBits
[0] >> 16) & 0xffff) / 65535.0F
;
987 result
[2] = result
[0];
988 result
[3] = result
[1];
989 store_vector4( inst
, machine
, result
);
992 case FP_OPCODE_UP4B
: /* unpack four GLbytes */
994 GLfloat a
[4], result
[4];
995 const GLuint
*rawBits
= (const GLuint
*) a
;
996 fetch_vector1( &inst
->SrcReg
[0], machine
, a
);
997 result
[0] = (((rawBits
[0] >> 0) & 0xff) - 128) / 127.0F
;
998 result
[0] = (((rawBits
[0] >> 8) & 0xff) - 128) / 127.0F
;
999 result
[0] = (((rawBits
[0] >> 16) & 0xff) - 128) / 127.0F
;
1000 result
[0] = (((rawBits
[0] >> 24) & 0xff) - 128) / 127.0F
;
1001 store_vector4( inst
, machine
, result
);
1004 case FP_OPCODE_UP4UB
: /* unpack four GLubytes */
1006 GLfloat a
[4], result
[4];
1007 const GLuint
*rawBits
= (const GLuint
*) a
;
1008 fetch_vector1( &inst
->SrcReg
[0], machine
, a
);
1009 result
[0] = ((rawBits
[0] >> 0) & 0xff) / 255.0F
;
1010 result
[0] = ((rawBits
[0] >> 8) & 0xff) / 255.0F
;
1011 result
[0] = ((rawBits
[0] >> 16) & 0xff) / 255.0F
;
1012 result
[0] = ((rawBits
[0] >> 24) & 0xff) / 255.0F
;
1013 store_vector4( inst
, machine
, result
);
1016 case FP_OPCODE_X2D
: /* 2-D matrix transform */
1018 GLfloat a
[4], b
[4], c
[4], result
[4];
1019 fetch_vector4( &inst
->SrcReg
[0], machine
, a
);
1020 fetch_vector4( &inst
->SrcReg
[1], machine
, b
);
1021 fetch_vector4( &inst
->SrcReg
[2], machine
, c
);
1022 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1023 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1024 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1025 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1026 store_vector4( inst
, machine
, result
);
1032 _mesa_problem(ctx
, "Bad opcode %d in _mesa_exec_fragment_program",
1034 return GL_TRUE
; /* return value doesn't matter */
1042 init_machine( GLcontext
*ctx
, struct fp_machine
*machine
,
1043 const struct fragment_program
*program
,
1044 const struct sw_span
*span
, GLuint col
)
1048 /* Clear temporary registers */
1049 _mesa_bzero(machine
->Registers
+ FP_TEMP_REG_START
,
1050 MAX_NV_FRAGMENT_PROGRAM_TEMPS
* 4 * sizeof(GLfloat
));
1052 /* Load program local parameters */
1053 for (j
= 0; j
< MAX_NV_FRAGMENT_PROGRAM_PARAMS
; j
++) {
1054 COPY_4V(machine
->Registers
[FP_PROG_REG_START
+ j
],
1055 program
->LocalParams
[j
]);
1058 /* Load input registers */
1059 if (program
->InputsRead
& (1 << FRAG_ATTRIB_WPOS
)) {
1060 GLfloat
*wpos
= machine
->Registers
[FP_INPUT_REG_START
+FRAG_ATTRIB_WPOS
];
1061 wpos
[0] = span
->x
+ col
;
1063 wpos
[2] = (GLfloat
) span
->array
->z
[col
] / ctx
->DepthMaxF
;
1064 wpos
[3] = span
->w
+ col
* span
->dwdx
;
1066 if (program
->InputsRead
& (1 << FRAG_ATTRIB_COL0
)) {
1067 GLfloat
*col0
= machine
->Registers
[FP_INPUT_REG_START
+FRAG_ATTRIB_COL0
];
1068 col0
[0] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][RCOMP
]);
1069 col0
[1] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][GCOMP
]);
1070 col0
[2] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][BCOMP
]);
1071 col0
[3] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][ACOMP
]);
1073 if (program
->InputsRead
& (1 << FRAG_ATTRIB_COL1
)) {
1074 GLfloat
*col1
= machine
->Registers
[FP_INPUT_REG_START
+FRAG_ATTRIB_COL1
];
1075 col1
[0] = CHAN_TO_FLOAT(span
->array
->spec
[col
][RCOMP
]);
1076 col1
[1] = CHAN_TO_FLOAT(span
->array
->spec
[col
][GCOMP
]);
1077 col1
[2] = CHAN_TO_FLOAT(span
->array
->spec
[col
][BCOMP
]);
1078 col1
[3] = CHAN_TO_FLOAT(span
->array
->spec
[col
][ACOMP
]);
1080 if (program
->InputsRead
& (1 << FRAG_ATTRIB_FOGC
)) {
1081 GLfloat
*fogc
= machine
->Registers
[FP_INPUT_REG_START
+FRAG_ATTRIB_FOGC
];
1082 fogc
[0] = span
->array
->fog
[col
];
1087 for (u
= 0; u
< ctx
->Const
.MaxTextureCoordUnits
; u
++) {
1088 if (program
->InputsRead
& (1 << (FRAG_ATTRIB_TEX0
+ u
))) {
1089 GLfloat
*tex
= machine
->Registers
[FP_INPUT_REG_START
+FRAG_ATTRIB_TEX0
+u
];
1090 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
1091 COPY_4V(tex
, span
->array
->texcoords
[u
][col
]);
1094 COPY_4V(tex
, ctx
->Current
.Attrib
[VERT_ATTRIB_TEX0
+ u
]);
1102 _swrast_exec_nv_fragment_program( GLcontext
*ctx
, struct sw_span
*span
)
1104 const struct fragment_program
*program
= ctx
->FragmentProgram
.Current
;
1107 for (i
= 0; i
< span
->end
; i
++) {
1108 if (span
->array
->mask
[i
]) {
1109 init_machine(ctx
, &ctx
->FragmentProgram
.Machine
,
1110 ctx
->FragmentProgram
.Current
, span
, i
);
1112 if (!execute_program(ctx
, program
, ~0,
1113 &ctx
->FragmentProgram
.Machine
, span
))
1114 span
->array
->mask
[i
] = GL_FALSE
; /* killed fragment */
1116 /* Store output registers */
1118 const GLfloat
*colOut
1119 = ctx
->FragmentProgram
.Machine
.Registers
[FP_OUTPUT_REG_START
];
1120 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][RCOMP
], colOut
[0]);
1121 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][GCOMP
], colOut
[1]);
1122 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][BCOMP
], colOut
[2]);
1123 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][ACOMP
], colOut
[3]);
1126 if (ctx
->FragmentProgram
.Current
->OutputsWritten
& 2)
1127 span
->array
->z
[i
] = IROUND(ctx
->FragmentProgram
.Machine
.Registers
[FP_OUTPUT_REG_START
+ 2][0] * ctx
->DepthMaxF
);