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
];
124 /* This is only for PRINT */
125 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_OUTPUTS
);
126 src
= machine
->Outputs
[source
->Index
];
128 case PROGRAM_LOCAL_PARAM
:
129 ASSERT(source
->Index
< MAX_PROGRAM_LOCAL_PARAMS
);
130 src
= program
->Base
.LocalParams
[source
->Index
];
132 case PROGRAM_ENV_PARAM
:
133 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_PARAMS
);
134 src
= ctx
->FragmentProgram
.Parameters
[source
->Index
];
136 case PROGRAM_STATE_VAR
:
138 case PROGRAM_NAMED_PARAM
:
139 ASSERT(source
->Index
< (GLint
) program
->Parameters
->NumParameters
);
140 src
= program
->Parameters
->ParameterValues
[source
->Index
];
143 _mesa_problem(ctx
, "Invalid input register file %d in fetch_vector4", source
->File
);
151 * Fetch a 4-element float vector from the given source register.
152 * Apply swizzling and negating as needed.
155 fetch_vector4( GLcontext
*ctx
,
156 const struct fp_src_register
*source
,
157 const struct fp_machine
*machine
,
158 const struct fragment_program
*program
,
161 const GLfloat
*src
= get_register_pointer(ctx
, source
, machine
, program
);
164 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
165 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
166 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
167 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
169 if (source
->NegateBase
) {
170 result
[0] = -result
[0];
171 result
[1] = -result
[1];
172 result
[2] = -result
[2];
173 result
[3] = -result
[3];
176 result
[0] = FABSF(result
[0]);
177 result
[1] = FABSF(result
[1]);
178 result
[2] = FABSF(result
[2]);
179 result
[3] = FABSF(result
[3]);
181 if (source
->NegateAbs
) {
182 result
[0] = -result
[0];
183 result
[1] = -result
[1];
184 result
[2] = -result
[2];
185 result
[3] = -result
[3];
191 * Fetch the derivative with respect to X for the given register.
192 * \return GL_TRUE if it was easily computed or GL_FALSE if we
193 * need to execute another instance of the program (ugh)!
196 fetch_vector4_deriv( GLcontext
*ctx
,
197 const struct fp_src_register
*source
,
198 const struct sw_span
*span
,
199 char xOrY
, GLint column
, GLfloat result
[4] )
203 ASSERT(xOrY
== 'X' || xOrY
== 'Y');
205 switch (source
->Index
) {
206 case FRAG_ATTRIB_WPOS
:
210 src
[2] = span
->dzdx
/ ctx
->DrawBuffer
->_DepthMaxF
;
216 src
[2] = span
->dzdy
/ ctx
->DrawBuffer
->_DepthMaxF
;
220 case FRAG_ATTRIB_COL0
:
222 src
[0] = span
->drdx
* (1.0F
/ CHAN_MAXF
);
223 src
[1] = span
->dgdx
* (1.0F
/ CHAN_MAXF
);
224 src
[2] = span
->dbdx
* (1.0F
/ CHAN_MAXF
);
225 src
[3] = span
->dadx
* (1.0F
/ CHAN_MAXF
);
228 src
[0] = span
->drdy
* (1.0F
/ CHAN_MAXF
);
229 src
[1] = span
->dgdy
* (1.0F
/ CHAN_MAXF
);
230 src
[2] = span
->dbdy
* (1.0F
/ CHAN_MAXF
);
231 src
[3] = span
->dady
* (1.0F
/ CHAN_MAXF
);
234 case FRAG_ATTRIB_COL1
:
236 src
[0] = span
->dsrdx
* (1.0F
/ CHAN_MAXF
);
237 src
[1] = span
->dsgdx
* (1.0F
/ CHAN_MAXF
);
238 src
[2] = span
->dsbdx
* (1.0F
/ CHAN_MAXF
);
239 src
[3] = 0.0; /* XXX need this */
242 src
[0] = span
->dsrdy
* (1.0F
/ CHAN_MAXF
);
243 src
[1] = span
->dsgdy
* (1.0F
/ CHAN_MAXF
);
244 src
[2] = span
->dsbdy
* (1.0F
/ CHAN_MAXF
);
245 src
[3] = 0.0; /* XXX need this */
248 case FRAG_ATTRIB_FOGC
:
250 src
[0] = span
->dfogdx
;
256 src
[0] = span
->dfogdy
;
262 case FRAG_ATTRIB_TEX0
:
263 case FRAG_ATTRIB_TEX1
:
264 case FRAG_ATTRIB_TEX2
:
265 case FRAG_ATTRIB_TEX3
:
266 case FRAG_ATTRIB_TEX4
:
267 case FRAG_ATTRIB_TEX5
:
268 case FRAG_ATTRIB_TEX6
:
269 case FRAG_ATTRIB_TEX7
:
271 const GLuint u
= source
->Index
- FRAG_ATTRIB_TEX0
;
272 /* this is a little tricky - I think I've got it right */
273 const GLfloat invQ
= 1.0f
/ (span
->tex
[u
][3]
274 + span
->texStepX
[u
][3] * column
);
275 src
[0] = span
->texStepX
[u
][0] * invQ
;
276 src
[1] = span
->texStepX
[u
][1] * invQ
;
277 src
[2] = span
->texStepX
[u
][2] * invQ
;
278 src
[3] = span
->texStepX
[u
][3] * invQ
;
281 const GLuint u
= source
->Index
- FRAG_ATTRIB_TEX0
;
282 /* Tricky, as above, but in Y direction */
283 const GLfloat invQ
= 1.0f
/ (span
->tex
[u
][3] + span
->texStepY
[u
][3]);
284 src
[0] = span
->texStepY
[u
][0] * invQ
;
285 src
[1] = span
->texStepY
[u
][1] * invQ
;
286 src
[2] = span
->texStepY
[u
][2] * invQ
;
287 src
[3] = span
->texStepY
[u
][3] * invQ
;
294 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
295 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
296 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
297 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
299 if (source
->NegateBase
) {
300 result
[0] = -result
[0];
301 result
[1] = -result
[1];
302 result
[2] = -result
[2];
303 result
[3] = -result
[3];
306 result
[0] = FABSF(result
[0]);
307 result
[1] = FABSF(result
[1]);
308 result
[2] = FABSF(result
[2]);
309 result
[3] = FABSF(result
[3]);
311 if (source
->NegateAbs
) {
312 result
[0] = -result
[0];
313 result
[1] = -result
[1];
314 result
[2] = -result
[2];
315 result
[3] = -result
[3];
322 * As above, but only return result[0] element.
325 fetch_vector1( GLcontext
*ctx
,
326 const struct fp_src_register
*source
,
327 const struct fp_machine
*machine
,
328 const struct fragment_program
*program
,
331 const GLfloat
*src
= get_register_pointer(ctx
, source
, machine
, program
);
334 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
336 if (source
->NegateBase
) {
337 result
[0] = -result
[0];
340 result
[0] = FABSF(result
[0]);
342 if (source
->NegateAbs
) {
343 result
[0] = -result
[0];
349 * Test value against zero and return GT, LT, EQ or UN if NaN.
352 generate_cc( float value
)
355 return COND_UN
; /* NaN */
365 * Test if the ccMaskRule is satisfied by the given condition code.
366 * Used to mask destination writes according to the current condition codee.
368 static INLINE GLboolean
369 test_cc(GLuint condCode
, GLuint ccMaskRule
)
371 switch (ccMaskRule
) {
372 case COND_EQ
: return (condCode
== COND_EQ
);
373 case COND_NE
: return (condCode
!= COND_EQ
);
374 case COND_LT
: return (condCode
== COND_LT
);
375 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
376 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
377 case COND_GT
: return (condCode
== COND_GT
);
378 case COND_TR
: return GL_TRUE
;
379 case COND_FL
: return GL_FALSE
;
380 default: return GL_TRUE
;
386 * Store 4 floats into a register. Observe the instructions saturate and
387 * set-condition-code flags.
390 store_vector4( const struct fp_instruction
*inst
,
391 struct fp_machine
*machine
,
392 const GLfloat value
[4] )
394 const struct fp_dst_register
*dest
= &(inst
->DstReg
);
395 const GLboolean clamp
= inst
->Saturate
;
396 const GLboolean updateCC
= inst
->UpdateCondRegister
;
399 GLfloat clampedValue
[4];
400 GLboolean condWriteMask
[4];
401 GLuint writeMask
= dest
->WriteMask
;
403 switch (dest
->File
) {
405 dstReg
= machine
->Outputs
[dest
->Index
];
407 case PROGRAM_TEMPORARY
:
408 dstReg
= machine
->Temporaries
[dest
->Index
];
410 case PROGRAM_WRITE_ONLY
:
414 _mesa_problem(NULL
, "bad register file in store_vector4(fp)");
419 if (value
[0] > 1.0e10
||
420 IS_INF_OR_NAN(value
[0]) ||
421 IS_INF_OR_NAN(value
[1]) ||
422 IS_INF_OR_NAN(value
[2]) ||
423 IS_INF_OR_NAN(value
[3]) )
424 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
428 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
429 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
430 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
431 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
432 value
= clampedValue
;
435 if (dest
->CondMask
!= COND_TR
) {
436 condWriteMask
[0] = GET_BIT(writeMask
, 0)
437 && test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 0)], dest
->CondMask
);
438 condWriteMask
[1] = GET_BIT(writeMask
, 1)
439 && test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 1)], dest
->CondMask
);
440 condWriteMask
[2] = GET_BIT(writeMask
, 2)
441 && test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 2)], dest
->CondMask
);
442 condWriteMask
[3] = GET_BIT(writeMask
, 3)
443 && test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 3)], dest
->CondMask
);
445 writeMask
= ((condWriteMask
[0] << 0) |
446 (condWriteMask
[1] << 1) |
447 (condWriteMask
[2] << 2) |
448 (condWriteMask
[3] << 3));
451 if (GET_BIT(writeMask
, 0)) {
452 dstReg
[0] = value
[0];
454 machine
->CondCodes
[0] = generate_cc(value
[0]);
456 if (GET_BIT(writeMask
, 1)) {
457 dstReg
[1] = value
[1];
459 machine
->CondCodes
[1] = generate_cc(value
[1]);
461 if (GET_BIT(writeMask
, 2)) {
462 dstReg
[2] = value
[2];
464 machine
->CondCodes
[2] = generate_cc(value
[2]);
466 if (GET_BIT(writeMask
, 3)) {
467 dstReg
[3] = value
[3];
469 machine
->CondCodes
[3] = generate_cc(value
[3]);
475 * Initialize a new machine state instance from an existing one, adding
476 * the partial derivatives onto the input registers.
477 * Used to implement DDX and DDY instructions in non-trivial cases.
480 init_machine_deriv( GLcontext
*ctx
,
481 const struct fp_machine
*machine
,
482 const struct fragment_program
*program
,
483 const struct sw_span
*span
, char xOrY
,
484 struct fp_machine
*dMachine
)
488 ASSERT(xOrY
== 'X' || xOrY
== 'Y');
490 /* copy existing machine */
491 _mesa_memcpy(dMachine
, machine
, sizeof(struct fp_machine
));
493 if (program
->Base
.Target
== GL_FRAGMENT_PROGRAM_NV
) {
494 /* Clear temporary registers (undefined for ARB_f_p) */
495 _mesa_bzero( (void*) machine
->Temporaries
,
496 MAX_NV_FRAGMENT_PROGRAM_TEMPS
* 4 * sizeof(GLfloat
));
499 /* Add derivatives */
500 if (program
->InputsRead
& (1 << FRAG_ATTRIB_WPOS
)) {
501 GLfloat
*wpos
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_WPOS
];
505 wpos
[2] += span
->dzdx
;
506 wpos
[3] += span
->dwdx
;
511 wpos
[2] += span
->dzdy
;
512 wpos
[3] += span
->dwdy
;
515 if (program
->InputsRead
& (1 << FRAG_ATTRIB_COL0
)) {
516 GLfloat
*col0
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_COL0
];
518 col0
[0] += span
->drdx
* (1.0F
/ CHAN_MAXF
);
519 col0
[1] += span
->dgdx
* (1.0F
/ CHAN_MAXF
);
520 col0
[2] += span
->dbdx
* (1.0F
/ CHAN_MAXF
);
521 col0
[3] += span
->dadx
* (1.0F
/ CHAN_MAXF
);
524 col0
[0] += span
->drdy
* (1.0F
/ CHAN_MAXF
);
525 col0
[1] += span
->dgdy
* (1.0F
/ CHAN_MAXF
);
526 col0
[2] += span
->dbdy
* (1.0F
/ CHAN_MAXF
);
527 col0
[3] += span
->dady
* (1.0F
/ CHAN_MAXF
);
530 if (program
->InputsRead
& (1 << FRAG_ATTRIB_COL1
)) {
531 GLfloat
*col1
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_COL1
];
533 col1
[0] += span
->dsrdx
* (1.0F
/ CHAN_MAXF
);
534 col1
[1] += span
->dsgdx
* (1.0F
/ CHAN_MAXF
);
535 col1
[2] += span
->dsbdx
* (1.0F
/ CHAN_MAXF
);
536 col1
[3] += 0.0; /*XXX fix */
539 col1
[0] += span
->dsrdy
* (1.0F
/ CHAN_MAXF
);
540 col1
[1] += span
->dsgdy
* (1.0F
/ CHAN_MAXF
);
541 col1
[2] += span
->dsbdy
* (1.0F
/ CHAN_MAXF
);
542 col1
[3] += 0.0; /*XXX fix */
545 if (program
->InputsRead
& (1 << FRAG_ATTRIB_FOGC
)) {
546 GLfloat
*fogc
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_FOGC
];
548 fogc
[0] += span
->dfogdx
;
551 fogc
[0] += span
->dfogdy
;
554 for (u
= 0; u
< ctx
->Const
.MaxTextureCoordUnits
; u
++) {
555 if (program
->InputsRead
& (1 << (FRAG_ATTRIB_TEX0
+ u
))) {
556 GLfloat
*tex
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_TEX0
+ u
];
557 /* XXX perspective-correct interpolation */
559 tex
[0] += span
->texStepX
[u
][0];
560 tex
[1] += span
->texStepX
[u
][1];
561 tex
[2] += span
->texStepX
[u
][2];
562 tex
[3] += span
->texStepX
[u
][3];
565 tex
[0] += span
->texStepY
[u
][0];
566 tex
[1] += span
->texStepY
[u
][1];
567 tex
[2] += span
->texStepY
[u
][2];
568 tex
[3] += span
->texStepY
[u
][3];
573 /* init condition codes */
574 dMachine
->CondCodes
[0] = COND_EQ
;
575 dMachine
->CondCodes
[1] = COND_EQ
;
576 dMachine
->CondCodes
[2] = COND_EQ
;
577 dMachine
->CondCodes
[3] = COND_EQ
;
582 * Execute the given vertex program.
583 * NOTE: we do everything in single-precision floating point; we don't
584 * currently observe the single/half/fixed-precision qualifiers.
585 * \param ctx - rendering context
586 * \param program - the fragment program to execute
587 * \param machine - machine state (register file)
588 * \param maxInst - max number of instructions to execute
589 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
592 execute_program( GLcontext
*ctx
,
593 const struct fragment_program
*program
, GLuint maxInst
,
594 struct fp_machine
*machine
, const struct sw_span
*span
,
600 printf("execute fragment program --------------------\n");
603 for (pc
= 0; pc
< maxInst
; pc
++) {
604 const struct fp_instruction
*inst
= program
->Instructions
+ pc
;
606 if (ctx
->FragmentProgram
.CallbackEnabled
&&
607 ctx
->FragmentProgram
.Callback
) {
608 ctx
->FragmentProgram
.CurrentPosition
= inst
->StringPos
;
609 ctx
->FragmentProgram
.Callback(program
->Base
.Target
,
610 ctx
->FragmentProgram
.CallbackData
);
613 switch (inst
->Opcode
) {
616 GLfloat a
[4], result
[4];
617 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
618 result
[0] = FABSF(a
[0]);
619 result
[1] = FABSF(a
[1]);
620 result
[2] = FABSF(a
[2]);
621 result
[3] = FABSF(a
[3]);
622 store_vector4( inst
, machine
, result
);
627 GLfloat a
[4], b
[4], result
[4];
628 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
629 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
630 result
[0] = a
[0] + b
[0];
631 result
[1] = a
[1] + b
[1];
632 result
[2] = a
[2] + b
[2];
633 result
[3] = a
[3] + b
[3];
634 store_vector4( inst
, machine
, result
);
639 GLfloat a
[4], b
[4], c
[4], result
[4];
640 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
641 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
642 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
643 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
644 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
645 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
646 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
647 store_vector4( inst
, machine
, result
);
652 GLfloat a
[4], result
[4];
653 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
654 result
[0] = result
[1] = result
[2] = result
[3] = (GLfloat
)_mesa_cos(a
[0]);
655 store_vector4( inst
, machine
, result
);
658 case FP_OPCODE_DDX
: /* Partial derivative with respect to X */
660 GLfloat a
[4], aNext
[4], result
[4];
661 struct fp_machine dMachine
;
662 if (!fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], span
, 'X',
664 /* This is tricky. Make a copy of the current machine state,
665 * increment the input registers by the dx or dy partial
666 * derivatives, then re-execute the program up to the
667 * preceeding instruction, then fetch the source register.
668 * Finally, find the difference in the register values for
669 * the original and derivative runs.
671 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
672 init_machine_deriv(ctx
, machine
, program
, span
,
674 execute_program(ctx
, program
, pc
, &dMachine
, span
, column
);
675 fetch_vector4( ctx
, &inst
->SrcReg
[0], &dMachine
, program
, aNext
);
676 result
[0] = aNext
[0] - a
[0];
677 result
[1] = aNext
[1] - a
[1];
678 result
[2] = aNext
[2] - a
[2];
679 result
[3] = aNext
[3] - a
[3];
681 store_vector4( inst
, machine
, result
);
684 case FP_OPCODE_DDY
: /* Partial derivative with respect to Y */
686 GLfloat a
[4], aNext
[4], result
[4];
687 struct fp_machine dMachine
;
688 if (!fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], span
, 'Y',
690 init_machine_deriv(ctx
, machine
, program
, span
,
692 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
693 execute_program(ctx
, program
, pc
, &dMachine
, span
, column
);
694 fetch_vector4( ctx
, &inst
->SrcReg
[0], &dMachine
, program
, aNext
);
695 result
[0] = aNext
[0] - a
[0];
696 result
[1] = aNext
[1] - a
[1];
697 result
[2] = aNext
[2] - a
[2];
698 result
[3] = aNext
[3] - a
[3];
700 store_vector4( inst
, machine
, result
);
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];
710 store_vector4( inst
, machine
, result
);
712 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
713 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
719 GLfloat a
[4], b
[4], result
[4];
720 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
721 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
722 result
[0] = result
[1] = result
[2] = result
[3] =
723 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + a
[3] * b
[3];
724 store_vector4( inst
, machine
, result
);
726 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
727 result
[0], a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
733 GLfloat a
[4], b
[4], result
[4];
734 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
735 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
736 result
[0] = result
[1] = result
[2] = result
[3] =
737 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + b
[3];
738 store_vector4( inst
, machine
, result
);
741 case FP_OPCODE_DST
: /* Distance vector */
743 GLfloat a
[4], b
[4], result
[4];
744 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
745 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
747 result
[1] = a
[1] * b
[1];
750 store_vector4( inst
, machine
, result
);
753 case FP_OPCODE_EX2
: /* Exponential base 2 */
755 GLfloat a
[4], result
[4];
756 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
757 result
[0] = result
[1] = result
[2] = result
[3] =
758 (GLfloat
) _mesa_pow(2.0, a
[0]);
759 store_vector4( inst
, machine
, result
);
764 GLfloat a
[4], result
[4];
765 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
766 result
[0] = FLOORF(a
[0]);
767 result
[1] = FLOORF(a
[1]);
768 result
[2] = FLOORF(a
[2]);
769 result
[3] = FLOORF(a
[3]);
770 store_vector4( inst
, machine
, result
);
775 GLfloat a
[4], result
[4];
776 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
777 result
[0] = a
[0] - FLOORF(a
[0]);
778 result
[1] = a
[1] - FLOORF(a
[1]);
779 result
[2] = a
[2] - FLOORF(a
[2]);
780 result
[3] = a
[3] - FLOORF(a
[3]);
781 store_vector4( inst
, machine
, result
);
784 case FP_OPCODE_KIL_NV
: /* NV_f_p only */
786 const GLuint swizzle
= inst
->DstReg
.CondSwizzle
;
787 const GLuint condMask
= inst
->DstReg
.CondMask
;
788 if (test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 0)], condMask
) ||
789 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 1)], condMask
) ||
790 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 2)], condMask
) ||
791 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 3)], condMask
)) {
796 case FP_OPCODE_KIL
: /* ARB_f_p only */
799 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
800 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
805 case FP_OPCODE_LG2
: /* log base 2 */
807 GLfloat a
[4], result
[4];
808 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
809 result
[0] = result
[1] = result
[2] = result
[3]
811 store_vector4( inst
, machine
, result
);
816 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
817 GLfloat a
[4], result
[4];
818 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
819 a
[0] = MAX2(a
[0], 0.0F
);
820 a
[1] = MAX2(a
[1], 0.0F
);
821 /* XXX ARB version clamps a[3], NV version doesn't */
822 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
825 /* XXX we could probably just use pow() here */
826 result
[2] = (a
[0] > 0.0F
) ? (GLfloat
) exp(a
[3] * log(a
[1])) : 0.0F
;
828 store_vector4( inst
, machine
, result
);
833 GLfloat a
[4], b
[4], c
[4], result
[4];
834 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
835 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
836 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
837 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
838 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
839 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
840 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
841 store_vector4( inst
, machine
, result
);
846 GLfloat a
[4], b
[4], c
[4], result
[4];
847 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
848 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
849 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
850 result
[0] = a
[0] * b
[0] + c
[0];
851 result
[1] = a
[1] * b
[1] + c
[1];
852 result
[2] = a
[2] * b
[2] + c
[2];
853 result
[3] = a
[3] * b
[3] + c
[3];
854 store_vector4( inst
, machine
, result
);
859 GLfloat a
[4], b
[4], result
[4];
860 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
861 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
862 result
[0] = MAX2(a
[0], b
[0]);
863 result
[1] = MAX2(a
[1], b
[1]);
864 result
[2] = MAX2(a
[2], b
[2]);
865 result
[3] = MAX2(a
[3], b
[3]);
866 store_vector4( inst
, machine
, result
);
868 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
869 result
[0], result
[1], result
[2], result
[3],
870 a
[0], a
[1], a
[2], a
[3],
871 b
[0], b
[1], b
[2], b
[3]);
877 GLfloat a
[4], b
[4], result
[4];
878 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
879 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
880 result
[0] = MIN2(a
[0], b
[0]);
881 result
[1] = MIN2(a
[1], b
[1]);
882 result
[2] = MIN2(a
[2], b
[2]);
883 result
[3] = MIN2(a
[3], b
[3]);
884 store_vector4( inst
, machine
, result
);
890 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, result
);
891 store_vector4( inst
, machine
, result
);
893 printf("MOV (%g %g %g %g)\n",
894 result
[0], result
[1], result
[2], result
[3]);
900 GLfloat a
[4], b
[4], result
[4];
901 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
902 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
903 result
[0] = a
[0] * b
[0];
904 result
[1] = a
[1] * b
[1];
905 result
[2] = a
[2] * b
[2];
906 result
[3] = a
[3] * b
[3];
907 store_vector4( inst
, machine
, result
);
909 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
910 result
[0], result
[1], result
[2], result
[3],
911 a
[0], a
[1], a
[2], a
[3],
912 b
[0], b
[1], b
[2], b
[3]);
916 case FP_OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
918 GLfloat a
[4], result
[4];
920 GLuint
*rawResult
= (GLuint
*) result
;
922 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
923 hx
= _mesa_float_to_half(a
[0]);
924 hy
= _mesa_float_to_half(a
[1]);
925 twoHalves
= hx
| (hy
<< 16);
926 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
928 store_vector4( inst
, machine
, result
);
931 case FP_OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
933 GLfloat a
[4], result
[4];
934 GLuint usx
, usy
, *rawResult
= (GLuint
*) result
;
935 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
936 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
937 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
938 usx
= IROUND(a
[0] * 65535.0F
);
939 usy
= IROUND(a
[1] * 65535.0F
);
940 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
942 store_vector4( inst
, machine
, result
);
945 case FP_OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
947 GLfloat a
[4], result
[4];
948 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
949 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
950 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
951 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
952 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
953 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
954 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
955 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
956 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
957 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
958 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
959 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
960 store_vector4( inst
, machine
, result
);
963 case FP_OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
965 GLfloat a
[4], result
[4];
966 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
967 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
968 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
969 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
970 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
971 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
972 ubx
= IROUND(255.0F
* a
[0]);
973 uby
= IROUND(255.0F
* a
[1]);
974 ubz
= IROUND(255.0F
* a
[2]);
975 ubw
= IROUND(255.0F
* a
[3]);
976 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
977 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
978 store_vector4( inst
, machine
, result
);
983 GLfloat a
[4], b
[4], result
[4];
984 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
985 fetch_vector1( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
986 result
[0] = result
[1] = result
[2] = result
[3]
987 = (GLfloat
)_mesa_pow(a
[0], b
[0]);
988 store_vector4( inst
, machine
, result
);
993 GLfloat a
[4], result
[4];
994 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
998 else if (IS_INF_OR_NAN(a
[0]))
999 printf("RCP(inf)\n");
1001 result
[0] = result
[1] = result
[2] = result
[3]
1003 store_vector4( inst
, machine
, result
);
1008 GLfloat axis
[4], dir
[4], result
[4], tmp
[4];
1009 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, axis
);
1010 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, dir
);
1011 tmp
[3] = axis
[0] * axis
[0]
1013 + axis
[2] * axis
[2];
1014 tmp
[0] = (2.0F
* (axis
[0] * dir
[0] +
1016 axis
[2] * dir
[2])) / tmp
[3];
1017 result
[0] = tmp
[0] * axis
[0] - dir
[0];
1018 result
[1] = tmp
[0] * axis
[1] - dir
[1];
1019 result
[2] = tmp
[0] * axis
[2] - dir
[2];
1020 /* result[3] is never written! XXX enforce in parser! */
1021 store_vector4( inst
, machine
, result
);
1024 case FP_OPCODE_RSQ
: /* 1 / sqrt() */
1026 GLfloat a
[4], result
[4];
1027 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1028 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1029 store_vector4( inst
, machine
, result
);
1031 printf("RSQ %g = 1/sqrt(%g)\n", result
[0], a
[0]);
1035 case FP_OPCODE_SCS
: /* sine and cos */
1037 GLfloat a
[4], result
[4];
1038 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1039 result
[0] = (GLfloat
)cos(a
[0]);
1040 result
[1] = (GLfloat
)sin(a
[0]);
1041 result
[2] = 0.0; /* undefined! */
1042 result
[3] = 0.0; /* undefined! */
1043 store_vector4( inst
, machine
, result
);
1046 case FP_OPCODE_SEQ
: /* set on equal */
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
);
1058 case FP_OPCODE_SFL
: /* set false, operands ignored */
1060 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1061 store_vector4( inst
, machine
, result
);
1064 case FP_OPCODE_SGE
: /* set on greater or equal */
1066 GLfloat a
[4], b
[4], result
[4];
1067 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1068 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1069 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1070 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1071 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1072 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1073 store_vector4( inst
, machine
, result
);
1076 case FP_OPCODE_SGT
: /* set on greater */
1078 GLfloat a
[4], b
[4], result
[4];
1079 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1080 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1081 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1082 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1083 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1084 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1085 store_vector4( inst
, machine
, result
);
1090 GLfloat a
[4], result
[4];
1091 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1092 result
[0] = result
[1] = result
[2] =
1093 result
[3] = (GLfloat
)_mesa_sin(a
[0]);
1094 store_vector4( inst
, machine
, result
);
1097 case FP_OPCODE_SLE
: /* set on less or equal */
1099 GLfloat a
[4], b
[4], result
[4];
1100 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1101 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1102 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1103 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1104 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1105 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1106 store_vector4( inst
, machine
, result
);
1109 case FP_OPCODE_SLT
: /* set on less */
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]) ? 1.0F
: 0.0F
;
1115 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1116 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1117 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1118 store_vector4( inst
, machine
, result
);
1121 case FP_OPCODE_SNE
: /* set on not equal */
1123 GLfloat a
[4], b
[4], result
[4];
1124 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1125 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1126 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1127 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1128 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1129 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1130 store_vector4( inst
, machine
, result
);
1133 case FP_OPCODE_STR
: /* set true, operands ignored */
1135 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1136 store_vector4( inst
, machine
, result
);
1141 GLfloat a
[4], b
[4], result
[4];
1142 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1143 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1144 result
[0] = a
[0] - b
[0];
1145 result
[1] = a
[1] - b
[1];
1146 result
[2] = a
[2] - b
[2];
1147 result
[3] = a
[3] - b
[3];
1148 store_vector4( inst
, machine
, result
);
1153 const struct fp_src_register
*source
= &inst
->SrcReg
[0];
1154 const GLfloat
*src
= get_register_pointer(ctx
, source
,
1159 /* do extended swizzling here */
1160 for (i
= 0; i
< 3; i
++) {
1161 if (GET_SWZ(source
->Swizzle
, i
) == SWIZZLE_ZERO
)
1163 else if (GET_SWZ(source
->Swizzle
, i
) == SWIZZLE_ONE
)
1166 result
[i
] = -src
[GET_SWZ(source
->Swizzle
, i
)];
1168 if (source
->NegateBase
)
1169 result
[i
] = -result
[i
];
1171 store_vector4( inst
, machine
, result
);
1174 case FP_OPCODE_TEX
: /* Both ARB and NV frag prog */
1177 GLfloat texcoord
[4], color
[4];
1178 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1179 /* Note: we pass 0 for LOD. The ARB extension requires it
1180 * while the NV extension says it's implementation dependant.
1182 fetch_texel( ctx
, texcoord
, 0.0F
, inst
->TexSrcUnit
, color
);
1184 printf("color[3] = %f\n", color
[3]);
1185 store_vector4( inst
, machine
, color
);
1188 case FP_OPCODE_TXB
: /* GL_ARB_fragment_program only */
1189 /* Texel lookup with LOD bias */
1191 GLfloat texcoord
[4], color
[4], bias
, lambda
;
1193 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1194 /* texcoord[3] is the bias to add to lambda */
1195 bias
= ctx
->Texture
.Unit
[inst
->TexSrcUnit
].LodBias
1196 + ctx
->Texture
.Unit
[inst
->TexSrcUnit
]._Current
->LodBias
1198 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
] + bias
;
1199 fetch_texel( ctx
, texcoord
, lambda
,
1200 inst
->TexSrcUnit
, color
);
1201 store_vector4( inst
, machine
, color
);
1204 case FP_OPCODE_TXD
: /* GL_NV_fragment_program only */
1205 /* Texture lookup w/ partial derivatives for LOD */
1207 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1208 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1209 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, dtdx
);
1210 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, dtdy
);
1211 fetch_texel_deriv( ctx
, texcoord
, dtdx
, dtdy
, inst
->TexSrcUnit
,
1213 store_vector4( inst
, machine
, color
);
1216 case FP_OPCODE_TXP
: /* GL_ARB_fragment_program only */
1217 /* Texture lookup w/ projective divide */
1219 GLfloat texcoord
[4], color
[4];
1220 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1221 texcoord
[0] /= texcoord
[3];
1222 texcoord
[1] /= texcoord
[3];
1223 texcoord
[2] /= texcoord
[3];
1225 fetch_texel( ctx
, texcoord
, 0.0F
, inst
->TexSrcUnit
, color
);
1226 store_vector4( inst
, machine
, color
);
1229 case FP_OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1230 /* Texture lookup w/ projective divide */
1232 GLfloat texcoord
[4], color
[4];
1233 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1234 if (inst
->TexSrcIdx
!= TEXTURE_CUBE_INDEX
) {
1235 texcoord
[0] /= texcoord
[3];
1236 texcoord
[1] /= texcoord
[3];
1237 texcoord
[2] /= texcoord
[3];
1239 fetch_texel( ctx
, texcoord
,
1240 span
->array
->lambda
[inst
->TexSrcUnit
][column
],
1241 inst
->TexSrcUnit
, color
);
1242 store_vector4( inst
, machine
, color
);
1245 case FP_OPCODE_UP2H
: /* unpack two 16-bit floats */
1247 GLfloat a
[4], result
[4];
1248 const GLuint
*rawBits
= (const GLuint
*) a
;
1250 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1251 hx
= rawBits
[0] & 0xffff;
1252 hy
= rawBits
[0] >> 16;
1253 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1254 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1255 store_vector4( inst
, machine
, result
);
1258 case FP_OPCODE_UP2US
: /* unpack two GLushorts */
1260 GLfloat a
[4], result
[4];
1261 const GLuint
*rawBits
= (const GLuint
*) a
;
1263 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1264 usx
= rawBits
[0] & 0xffff;
1265 usy
= rawBits
[0] >> 16;
1266 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1267 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1268 store_vector4( inst
, machine
, result
);
1271 case FP_OPCODE_UP4B
: /* unpack four GLbytes */
1273 GLfloat a
[4], result
[4];
1274 const GLuint
*rawBits
= (const GLuint
*) a
;
1275 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1276 result
[0] = (((rawBits
[0] >> 0) & 0xff) - 128) / 127.0F
;
1277 result
[1] = (((rawBits
[0] >> 8) & 0xff) - 128) / 127.0F
;
1278 result
[2] = (((rawBits
[0] >> 16) & 0xff) - 128) / 127.0F
;
1279 result
[3] = (((rawBits
[0] >> 24) & 0xff) - 128) / 127.0F
;
1280 store_vector4( inst
, machine
, result
);
1283 case FP_OPCODE_UP4UB
: /* unpack four GLubytes */
1285 GLfloat a
[4], result
[4];
1286 const GLuint
*rawBits
= (const GLuint
*) a
;
1287 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1288 result
[0] = ((rawBits
[0] >> 0) & 0xff) / 255.0F
;
1289 result
[1] = ((rawBits
[0] >> 8) & 0xff) / 255.0F
;
1290 result
[2] = ((rawBits
[0] >> 16) & 0xff) / 255.0F
;
1291 result
[3] = ((rawBits
[0] >> 24) & 0xff) / 255.0F
;
1292 store_vector4( inst
, machine
, result
);
1295 case FP_OPCODE_XPD
: /* cross product */
1297 GLfloat a
[4], b
[4], result
[4];
1298 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1299 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1300 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1301 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1302 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1304 store_vector4( inst
, machine
, result
);
1307 case FP_OPCODE_X2D
: /* 2-D matrix transform */
1309 GLfloat a
[4], b
[4], c
[4], result
[4];
1310 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1311 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1312 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
1313 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1314 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1315 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1316 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1317 store_vector4( inst
, machine
, result
);
1320 case FP_OPCODE_PRINT
:
1322 if (inst
->SrcReg
[0].File
!= -1) {
1324 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1325 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst
->Data
,
1326 a
[0], a
[1], a
[2], a
[3]);
1329 _mesa_printf("%s\n", (const char *) inst
->Data
);
1336 _mesa_problem(ctx
, "Bad opcode %d in _mesa_exec_fragment_program",
1338 return GL_TRUE
; /* return value doesn't matter */
1346 init_machine( GLcontext
*ctx
, struct fp_machine
*machine
,
1347 const struct fragment_program
*program
,
1348 const struct sw_span
*span
, GLuint col
)
1350 GLuint inputsRead
= program
->InputsRead
;
1353 if (ctx
->FragmentProgram
.CallbackEnabled
)
1356 if (program
->Base
.Target
== GL_FRAGMENT_PROGRAM_NV
) {
1357 /* Clear temporary registers (undefined for ARB_f_p) */
1358 _mesa_bzero(machine
->Temporaries
,
1359 MAX_NV_FRAGMENT_PROGRAM_TEMPS
* 4 * sizeof(GLfloat
));
1362 /* Load input registers */
1363 if (inputsRead
& (1 << FRAG_ATTRIB_WPOS
)) {
1364 GLfloat
*wpos
= machine
->Inputs
[FRAG_ATTRIB_WPOS
];
1365 wpos
[0] = (GLfloat
) span
->x
+ col
;
1366 wpos
[1] = (GLfloat
) span
->y
;
1367 wpos
[2] = (GLfloat
) span
->array
->z
[col
] / ctx
->DrawBuffer
->_DepthMaxF
;
1368 wpos
[3] = span
->w
+ col
* span
->dwdx
;
1370 if (inputsRead
& (1 << FRAG_ATTRIB_COL0
)) {
1371 GLfloat
*col0
= machine
->Inputs
[FRAG_ATTRIB_COL0
];
1372 col0
[0] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][RCOMP
]);
1373 col0
[1] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][GCOMP
]);
1374 col0
[2] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][BCOMP
]);
1375 col0
[3] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][ACOMP
]);
1377 if (inputsRead
& (1 << FRAG_ATTRIB_COL1
)) {
1378 GLfloat
*col1
= machine
->Inputs
[FRAG_ATTRIB_COL1
];
1379 col1
[0] = CHAN_TO_FLOAT(span
->array
->spec
[col
][RCOMP
]);
1380 col1
[1] = CHAN_TO_FLOAT(span
->array
->spec
[col
][GCOMP
]);
1381 col1
[2] = CHAN_TO_FLOAT(span
->array
->spec
[col
][BCOMP
]);
1382 col1
[3] = CHAN_TO_FLOAT(span
->array
->spec
[col
][ACOMP
]);
1384 if (inputsRead
& (1 << FRAG_ATTRIB_FOGC
)) {
1385 GLfloat
*fogc
= machine
->Inputs
[FRAG_ATTRIB_FOGC
];
1386 fogc
[0] = span
->array
->fog
[col
];
1391 for (u
= 0; u
< ctx
->Const
.MaxTextureCoordUnits
; u
++) {
1392 if (inputsRead
& (1 << (FRAG_ATTRIB_TEX0
+ u
))) {
1393 GLfloat
*tex
= machine
->Inputs
[FRAG_ATTRIB_TEX0
+ u
];
1394 /*ASSERT(ctx->Texture._EnabledCoordUnits & (1 << u));*/
1395 COPY_4V(tex
, span
->array
->texcoords
[u
][col
]);
1396 /*ASSERT(tex[0] != 0 || tex[1] != 0 || tex[2] != 0);*/
1400 /* init condition codes */
1401 machine
->CondCodes
[0] = COND_EQ
;
1402 machine
->CondCodes
[1] = COND_EQ
;
1403 machine
->CondCodes
[2] = COND_EQ
;
1404 machine
->CondCodes
[3] = COND_EQ
;
1410 * Execute the current fragment program, operating on the given span.
1413 _swrast_exec_fragment_program( GLcontext
*ctx
, struct sw_span
*span
)
1415 const struct fragment_program
*program
= ctx
->FragmentProgram
._Current
;
1418 ctx
->_CurrentProgram
= GL_FRAGMENT_PROGRAM_ARB
; /* or NV, doesn't matter */
1420 for (i
= 0; i
< span
->end
; i
++) {
1421 if (span
->array
->mask
[i
]) {
1422 init_machine(ctx
, &ctx
->FragmentProgram
.Machine
,
1423 ctx
->FragmentProgram
._Current
, span
, i
);
1426 if (!_swrast_execute_codegen_program(ctx
, program
, ~0,
1427 &ctx
->FragmentProgram
.Machine
,
1429 span
->array
->mask
[i
] = GL_FALSE
; /* killed fragment */
1432 if (!execute_program(ctx
, program
, ~0,
1433 &ctx
->FragmentProgram
.Machine
, span
, i
)) {
1434 span
->array
->mask
[i
] = GL_FALSE
; /* killed fragment */
1438 /* Store output registers */
1440 const GLfloat
*colOut
1441 = ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLR
];
1442 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][RCOMP
], colOut
[0]);
1443 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][GCOMP
], colOut
[1]);
1444 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][BCOMP
], colOut
[2]);
1445 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][ACOMP
], colOut
[3]);
1448 if (program
->OutputsWritten
& (1 << FRAG_OUTPUT_DEPR
))
1449 span
->array
->z
[i
] = IROUND(ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_DEPR
][0] * ctx
->DrawBuffer
->_DepthMaxF
);
1453 ctx
->_CurrentProgram
= 0;