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"
40 #include "s_nvfragprog.h"
44 /* if 1, print some debugging info */
51 fetch_texel( GLcontext
*ctx
, const GLfloat texcoord
[4], GLfloat lambda
,
52 GLuint unit
, GLfloat color
[4] )
55 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
57 /* XXX use a float-valued TextureSample routine here!!! */
58 swrast
->TextureSample
[unit
](ctx
, ctx
->Texture
.Unit
[unit
]._Current
,
59 1, (const GLfloat (*)[4]) texcoord
,
61 color
[0] = CHAN_TO_FLOAT(rgba
[0]);
62 color
[1] = CHAN_TO_FLOAT(rgba
[1]);
63 color
[2] = CHAN_TO_FLOAT(rgba
[2]);
64 color
[3] = CHAN_TO_FLOAT(rgba
[3]);
69 * Fetch a texel with the given partial derivatives to compute a level
70 * of detail in the mipmap.
73 fetch_texel_deriv( GLcontext
*ctx
, const GLfloat texcoord
[4],
74 const GLfloat texdx
[4], const GLfloat texdy
[4],
75 GLuint unit
, GLfloat color
[4] )
77 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
78 const struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[unit
]._Current
;
79 const struct gl_texture_image
*texImg
= texObj
->Image
[0][texObj
->BaseLevel
];
80 const GLfloat texW
= (GLfloat
) texImg
->WidthScale
;
81 const GLfloat texH
= (GLfloat
) texImg
->HeightScale
;
84 GLfloat lambda
= _swrast_compute_lambda(texdx
[0], texdy
[0], /* ds/dx, ds/dy */
85 texdx
[1], texdy
[1], /* dt/dx, dt/dy */
86 texdx
[3], texdy
[2], /* dq/dx, dq/dy */
88 texcoord
[0], texcoord
[1], texcoord
[3],
91 swrast
->TextureSample
[unit
](ctx
, ctx
->Texture
.Unit
[unit
]._Current
,
92 1, (const GLfloat (*)[4]) texcoord
,
94 color
[0] = CHAN_TO_FLOAT(rgba
[0]);
95 color
[1] = CHAN_TO_FLOAT(rgba
[1]);
96 color
[2] = CHAN_TO_FLOAT(rgba
[2]);
97 color
[3] = CHAN_TO_FLOAT(rgba
[3]);
102 * Return a pointer to the 4-element float vector specified by the given
105 static INLINE
const GLfloat
*
106 get_register_pointer( GLcontext
*ctx
,
107 const struct fp_src_register
*source
,
108 const struct fp_machine
*machine
,
109 const struct fragment_program
*program
)
112 switch (source
->File
) {
113 case PROGRAM_TEMPORARY
:
114 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_TEMPS
);
115 src
= machine
->Temporaries
[source
->Index
];
118 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_INPUTS
);
119 src
= machine
->Inputs
[source
->Index
];
122 /* This is only for PRINT */
123 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_OUTPUTS
);
124 src
= machine
->Outputs
[source
->Index
];
126 case PROGRAM_LOCAL_PARAM
:
127 ASSERT(source
->Index
< MAX_PROGRAM_LOCAL_PARAMS
);
128 src
= program
->Base
.LocalParams
[source
->Index
];
130 case PROGRAM_ENV_PARAM
:
131 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_PARAMS
);
132 src
= ctx
->FragmentProgram
.Parameters
[source
->Index
];
134 case PROGRAM_STATE_VAR
:
136 case PROGRAM_NAMED_PARAM
:
137 ASSERT(source
->Index
< (GLint
) program
->Parameters
->NumParameters
);
138 src
= program
->Parameters
->ParameterValues
[source
->Index
];
141 _mesa_problem(ctx
, "Invalid input register file %d in fetch_vector4", source
->File
);
149 * Fetch a 4-element float vector from the given source register.
150 * Apply swizzling and negating as needed.
153 fetch_vector4( GLcontext
*ctx
,
154 const struct fp_src_register
*source
,
155 const struct fp_machine
*machine
,
156 const struct fragment_program
*program
,
159 const GLfloat
*src
= get_register_pointer(ctx
, source
, machine
, program
);
162 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
163 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
164 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
165 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
167 if (source
->NegateBase
) {
168 result
[0] = -result
[0];
169 result
[1] = -result
[1];
170 result
[2] = -result
[2];
171 result
[3] = -result
[3];
174 result
[0] = FABSF(result
[0]);
175 result
[1] = FABSF(result
[1]);
176 result
[2] = FABSF(result
[2]);
177 result
[3] = FABSF(result
[3]);
179 if (source
->NegateAbs
) {
180 result
[0] = -result
[0];
181 result
[1] = -result
[1];
182 result
[2] = -result
[2];
183 result
[3] = -result
[3];
189 * Fetch the derivative with respect to X for the given register.
190 * \return GL_TRUE if it was easily computed or GL_FALSE if we
191 * need to execute another instance of the program (ugh)!
194 fetch_vector4_deriv( GLcontext
*ctx
,
195 const struct fp_src_register
*source
,
196 const struct sw_span
*span
,
197 char xOrY
, GLint column
, GLfloat result
[4] )
201 ASSERT(xOrY
== 'X' || xOrY
== 'Y');
203 switch (source
->Index
) {
204 case FRAG_ATTRIB_WPOS
:
208 src
[2] = span
->dzdx
/ ctx
->DrawBuffer
->_DepthMaxF
;
214 src
[2] = span
->dzdy
/ ctx
->DrawBuffer
->_DepthMaxF
;
218 case FRAG_ATTRIB_COL0
:
220 src
[0] = span
->drdx
* (1.0F
/ CHAN_MAXF
);
221 src
[1] = span
->dgdx
* (1.0F
/ CHAN_MAXF
);
222 src
[2] = span
->dbdx
* (1.0F
/ CHAN_MAXF
);
223 src
[3] = span
->dadx
* (1.0F
/ CHAN_MAXF
);
226 src
[0] = span
->drdy
* (1.0F
/ CHAN_MAXF
);
227 src
[1] = span
->dgdy
* (1.0F
/ CHAN_MAXF
);
228 src
[2] = span
->dbdy
* (1.0F
/ CHAN_MAXF
);
229 src
[3] = span
->dady
* (1.0F
/ CHAN_MAXF
);
232 case FRAG_ATTRIB_COL1
:
234 src
[0] = span
->dsrdx
* (1.0F
/ CHAN_MAXF
);
235 src
[1] = span
->dsgdx
* (1.0F
/ CHAN_MAXF
);
236 src
[2] = span
->dsbdx
* (1.0F
/ CHAN_MAXF
);
237 src
[3] = 0.0; /* XXX need this */
240 src
[0] = span
->dsrdy
* (1.0F
/ CHAN_MAXF
);
241 src
[1] = span
->dsgdy
* (1.0F
/ CHAN_MAXF
);
242 src
[2] = span
->dsbdy
* (1.0F
/ CHAN_MAXF
);
243 src
[3] = 0.0; /* XXX need this */
246 case FRAG_ATTRIB_FOGC
:
248 src
[0] = span
->dfogdx
;
254 src
[0] = span
->dfogdy
;
260 case FRAG_ATTRIB_TEX0
:
261 case FRAG_ATTRIB_TEX1
:
262 case FRAG_ATTRIB_TEX2
:
263 case FRAG_ATTRIB_TEX3
:
264 case FRAG_ATTRIB_TEX4
:
265 case FRAG_ATTRIB_TEX5
:
266 case FRAG_ATTRIB_TEX6
:
267 case FRAG_ATTRIB_TEX7
:
269 const GLuint u
= source
->Index
- FRAG_ATTRIB_TEX0
;
270 /* this is a little tricky - I think I've got it right */
271 const GLfloat invQ
= 1.0f
/ (span
->tex
[u
][3]
272 + span
->texStepX
[u
][3] * column
);
273 src
[0] = span
->texStepX
[u
][0] * invQ
;
274 src
[1] = span
->texStepX
[u
][1] * invQ
;
275 src
[2] = span
->texStepX
[u
][2] * invQ
;
276 src
[3] = span
->texStepX
[u
][3] * invQ
;
279 const GLuint u
= source
->Index
- FRAG_ATTRIB_TEX0
;
280 /* Tricky, as above, but in Y direction */
281 const GLfloat invQ
= 1.0f
/ (span
->tex
[u
][3] + span
->texStepY
[u
][3]);
282 src
[0] = span
->texStepY
[u
][0] * invQ
;
283 src
[1] = span
->texStepY
[u
][1] * invQ
;
284 src
[2] = span
->texStepY
[u
][2] * invQ
;
285 src
[3] = span
->texStepY
[u
][3] * invQ
;
292 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
293 result
[1] = src
[GET_SWZ(source
->Swizzle
, 1)];
294 result
[2] = src
[GET_SWZ(source
->Swizzle
, 2)];
295 result
[3] = src
[GET_SWZ(source
->Swizzle
, 3)];
297 if (source
->NegateBase
) {
298 result
[0] = -result
[0];
299 result
[1] = -result
[1];
300 result
[2] = -result
[2];
301 result
[3] = -result
[3];
304 result
[0] = FABSF(result
[0]);
305 result
[1] = FABSF(result
[1]);
306 result
[2] = FABSF(result
[2]);
307 result
[3] = FABSF(result
[3]);
309 if (source
->NegateAbs
) {
310 result
[0] = -result
[0];
311 result
[1] = -result
[1];
312 result
[2] = -result
[2];
313 result
[3] = -result
[3];
320 * As above, but only return result[0] element.
323 fetch_vector1( GLcontext
*ctx
,
324 const struct fp_src_register
*source
,
325 const struct fp_machine
*machine
,
326 const struct fragment_program
*program
,
329 const GLfloat
*src
= get_register_pointer(ctx
, source
, machine
, program
);
332 result
[0] = src
[GET_SWZ(source
->Swizzle
, 0)];
334 if (source
->NegateBase
) {
335 result
[0] = -result
[0];
338 result
[0] = FABSF(result
[0]);
340 if (source
->NegateAbs
) {
341 result
[0] = -result
[0];
347 * Test value against zero and return GT, LT, EQ or UN if NaN.
350 generate_cc( float value
)
353 return COND_UN
; /* NaN */
363 * Test if the ccMaskRule is satisfied by the given condition code.
364 * Used to mask destination writes according to the current condition codee.
366 static INLINE GLboolean
367 test_cc(GLuint condCode
, GLuint ccMaskRule
)
369 switch (ccMaskRule
) {
370 case COND_EQ
: return (condCode
== COND_EQ
);
371 case COND_NE
: return (condCode
!= COND_EQ
);
372 case COND_LT
: return (condCode
== COND_LT
);
373 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
374 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
375 case COND_GT
: return (condCode
== COND_GT
);
376 case COND_TR
: return GL_TRUE
;
377 case COND_FL
: return GL_FALSE
;
378 default: return GL_TRUE
;
384 * Store 4 floats into a register. Observe the instructions saturate and
385 * set-condition-code flags.
388 store_vector4( const struct fp_instruction
*inst
,
389 struct fp_machine
*machine
,
390 const GLfloat value
[4] )
392 const struct fp_dst_register
*dest
= &(inst
->DstReg
);
393 const GLboolean clamp
= inst
->Saturate
;
394 const GLboolean updateCC
= inst
->UpdateCondRegister
;
397 GLfloat clampedValue
[4];
398 GLboolean condWriteMask
[4];
399 GLuint writeMask
= dest
->WriteMask
;
401 switch (dest
->File
) {
403 dstReg
= machine
->Outputs
[dest
->Index
];
405 case PROGRAM_TEMPORARY
:
406 dstReg
= machine
->Temporaries
[dest
->Index
];
408 case PROGRAM_WRITE_ONLY
:
412 _mesa_problem(NULL
, "bad register file in store_vector4(fp)");
417 if (value
[0] > 1.0e10
||
418 IS_INF_OR_NAN(value
[0]) ||
419 IS_INF_OR_NAN(value
[1]) ||
420 IS_INF_OR_NAN(value
[2]) ||
421 IS_INF_OR_NAN(value
[3]) )
422 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
426 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
427 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
428 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
429 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
430 value
= clampedValue
;
433 if (dest
->CondMask
!= COND_TR
) {
434 condWriteMask
[0] = GET_BIT(writeMask
, 0)
435 && test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 0)], dest
->CondMask
);
436 condWriteMask
[1] = GET_BIT(writeMask
, 1)
437 && test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 1)], dest
->CondMask
);
438 condWriteMask
[2] = GET_BIT(writeMask
, 2)
439 && test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 2)], dest
->CondMask
);
440 condWriteMask
[3] = GET_BIT(writeMask
, 3)
441 && test_cc(machine
->CondCodes
[GET_SWZ(dest
->CondSwizzle
, 3)], dest
->CondMask
);
443 writeMask
= ((condWriteMask
[0] << 0) |
444 (condWriteMask
[1] << 1) |
445 (condWriteMask
[2] << 2) |
446 (condWriteMask
[3] << 3));
449 if (GET_BIT(writeMask
, 0)) {
450 dstReg
[0] = value
[0];
452 machine
->CondCodes
[0] = generate_cc(value
[0]);
454 if (GET_BIT(writeMask
, 1)) {
455 dstReg
[1] = value
[1];
457 machine
->CondCodes
[1] = generate_cc(value
[1]);
459 if (GET_BIT(writeMask
, 2)) {
460 dstReg
[2] = value
[2];
462 machine
->CondCodes
[2] = generate_cc(value
[2]);
464 if (GET_BIT(writeMask
, 3)) {
465 dstReg
[3] = value
[3];
467 machine
->CondCodes
[3] = generate_cc(value
[3]);
473 * Initialize a new machine state instance from an existing one, adding
474 * the partial derivatives onto the input registers.
475 * Used to implement DDX and DDY instructions in non-trivial cases.
478 init_machine_deriv( GLcontext
*ctx
,
479 const struct fp_machine
*machine
,
480 const struct fragment_program
*program
,
481 const struct sw_span
*span
, char xOrY
,
482 struct fp_machine
*dMachine
)
486 ASSERT(xOrY
== 'X' || xOrY
== 'Y');
488 /* copy existing machine */
489 _mesa_memcpy(dMachine
, machine
, sizeof(struct fp_machine
));
491 if (program
->Base
.Target
== GL_FRAGMENT_PROGRAM_NV
) {
492 /* Clear temporary registers (undefined for ARB_f_p) */
493 _mesa_bzero( (void*) machine
->Temporaries
,
494 MAX_NV_FRAGMENT_PROGRAM_TEMPS
* 4 * sizeof(GLfloat
));
497 /* Add derivatives */
498 if (program
->InputsRead
& (1 << FRAG_ATTRIB_WPOS
)) {
499 GLfloat
*wpos
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_WPOS
];
503 wpos
[2] += span
->dzdx
;
504 wpos
[3] += span
->dwdx
;
509 wpos
[2] += span
->dzdy
;
510 wpos
[3] += span
->dwdy
;
513 if (program
->InputsRead
& (1 << FRAG_ATTRIB_COL0
)) {
514 GLfloat
*col0
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_COL0
];
516 col0
[0] += span
->drdx
* (1.0F
/ CHAN_MAXF
);
517 col0
[1] += span
->dgdx
* (1.0F
/ CHAN_MAXF
);
518 col0
[2] += span
->dbdx
* (1.0F
/ CHAN_MAXF
);
519 col0
[3] += span
->dadx
* (1.0F
/ CHAN_MAXF
);
522 col0
[0] += span
->drdy
* (1.0F
/ CHAN_MAXF
);
523 col0
[1] += span
->dgdy
* (1.0F
/ CHAN_MAXF
);
524 col0
[2] += span
->dbdy
* (1.0F
/ CHAN_MAXF
);
525 col0
[3] += span
->dady
* (1.0F
/ CHAN_MAXF
);
528 if (program
->InputsRead
& (1 << FRAG_ATTRIB_COL1
)) {
529 GLfloat
*col1
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_COL1
];
531 col1
[0] += span
->dsrdx
* (1.0F
/ CHAN_MAXF
);
532 col1
[1] += span
->dsgdx
* (1.0F
/ CHAN_MAXF
);
533 col1
[2] += span
->dsbdx
* (1.0F
/ CHAN_MAXF
);
534 col1
[3] += 0.0; /*XXX fix */
537 col1
[0] += span
->dsrdy
* (1.0F
/ CHAN_MAXF
);
538 col1
[1] += span
->dsgdy
* (1.0F
/ CHAN_MAXF
);
539 col1
[2] += span
->dsbdy
* (1.0F
/ CHAN_MAXF
);
540 col1
[3] += 0.0; /*XXX fix */
543 if (program
->InputsRead
& (1 << FRAG_ATTRIB_FOGC
)) {
544 GLfloat
*fogc
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_FOGC
];
546 fogc
[0] += span
->dfogdx
;
549 fogc
[0] += span
->dfogdy
;
552 for (u
= 0; u
< ctx
->Const
.MaxTextureCoordUnits
; u
++) {
553 if (program
->InputsRead
& (1 << (FRAG_ATTRIB_TEX0
+ u
))) {
554 GLfloat
*tex
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_TEX0
+ u
];
555 /* XXX perspective-correct interpolation */
557 tex
[0] += span
->texStepX
[u
][0];
558 tex
[1] += span
->texStepX
[u
][1];
559 tex
[2] += span
->texStepX
[u
][2];
560 tex
[3] += span
->texStepX
[u
][3];
563 tex
[0] += span
->texStepY
[u
][0];
564 tex
[1] += span
->texStepY
[u
][1];
565 tex
[2] += span
->texStepY
[u
][2];
566 tex
[3] += span
->texStepY
[u
][3];
571 /* init condition codes */
572 dMachine
->CondCodes
[0] = COND_EQ
;
573 dMachine
->CondCodes
[1] = COND_EQ
;
574 dMachine
->CondCodes
[2] = COND_EQ
;
575 dMachine
->CondCodes
[3] = COND_EQ
;
580 * Execute the given vertex program.
581 * NOTE: we do everything in single-precision floating point; we don't
582 * currently observe the single/half/fixed-precision qualifiers.
583 * \param ctx - rendering context
584 * \param program - the fragment program to execute
585 * \param machine - machine state (register file)
586 * \param maxInst - max number of instructions to execute
587 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
590 execute_program( GLcontext
*ctx
,
591 const struct fragment_program
*program
, GLuint maxInst
,
592 struct fp_machine
*machine
, const struct sw_span
*span
,
598 printf("execute fragment program --------------------\n");
601 for (pc
= 0; pc
< maxInst
; pc
++) {
602 const struct fp_instruction
*inst
= program
->Instructions
+ pc
;
604 if (ctx
->FragmentProgram
.CallbackEnabled
&&
605 ctx
->FragmentProgram
.Callback
) {
606 ctx
->FragmentProgram
.CurrentPosition
= inst
->StringPos
;
607 ctx
->FragmentProgram
.Callback(program
->Base
.Target
,
608 ctx
->FragmentProgram
.CallbackData
);
611 switch (inst
->Opcode
) {
614 GLfloat a
[4], result
[4];
615 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
616 result
[0] = FABSF(a
[0]);
617 result
[1] = FABSF(a
[1]);
618 result
[2] = FABSF(a
[2]);
619 result
[3] = FABSF(a
[3]);
620 store_vector4( inst
, machine
, result
);
625 GLfloat a
[4], b
[4], result
[4];
626 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
627 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
628 result
[0] = a
[0] + b
[0];
629 result
[1] = a
[1] + b
[1];
630 result
[2] = a
[2] + b
[2];
631 result
[3] = a
[3] + b
[3];
632 store_vector4( inst
, machine
, result
);
637 GLfloat a
[4], b
[4], c
[4], result
[4];
638 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
639 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
640 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
641 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
642 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
643 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
644 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
645 store_vector4( inst
, machine
, result
);
650 GLfloat a
[4], result
[4];
651 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
652 result
[0] = result
[1] = result
[2] = result
[3] = (GLfloat
)_mesa_cos(a
[0]);
653 store_vector4( inst
, machine
, result
);
656 case FP_OPCODE_DDX
: /* Partial derivative with respect to X */
658 GLfloat a
[4], aNext
[4], result
[4];
659 struct fp_machine dMachine
;
660 if (!fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], span
, 'X',
662 /* This is tricky. Make a copy of the current machine state,
663 * increment the input registers by the dx or dy partial
664 * derivatives, then re-execute the program up to the
665 * preceeding instruction, then fetch the source register.
666 * Finally, find the difference in the register values for
667 * the original and derivative runs.
669 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
670 init_machine_deriv(ctx
, machine
, program
, span
,
672 execute_program(ctx
, program
, pc
, &dMachine
, span
, column
);
673 fetch_vector4( ctx
, &inst
->SrcReg
[0], &dMachine
, program
, aNext
);
674 result
[0] = aNext
[0] - a
[0];
675 result
[1] = aNext
[1] - a
[1];
676 result
[2] = aNext
[2] - a
[2];
677 result
[3] = aNext
[3] - a
[3];
679 store_vector4( inst
, machine
, result
);
682 case FP_OPCODE_DDY
: /* Partial derivative with respect to Y */
684 GLfloat a
[4], aNext
[4], result
[4];
685 struct fp_machine dMachine
;
686 if (!fetch_vector4_deriv(ctx
, &inst
->SrcReg
[0], span
, 'Y',
688 init_machine_deriv(ctx
, machine
, program
, span
,
690 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
691 execute_program(ctx
, program
, pc
, &dMachine
, span
, column
);
692 fetch_vector4( ctx
, &inst
->SrcReg
[0], &dMachine
, program
, aNext
);
693 result
[0] = aNext
[0] - a
[0];
694 result
[1] = aNext
[1] - a
[1];
695 result
[2] = aNext
[2] - a
[2];
696 result
[3] = aNext
[3] - a
[3];
698 store_vector4( inst
, machine
, result
);
703 GLfloat a
[4], b
[4], result
[4];
704 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
705 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
706 result
[0] = result
[1] = result
[2] = result
[3] =
707 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2];
708 store_vector4( inst
, machine
, result
);
710 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
711 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
717 GLfloat a
[4], b
[4], result
[4];
718 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
719 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
720 result
[0] = result
[1] = result
[2] = result
[3] =
721 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + a
[3] * b
[3];
722 store_vector4( inst
, machine
, result
);
724 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
725 result
[0], a
[0], a
[1], a
[2], a
[3], b
[0], b
[1], b
[2], b
[3]);
731 GLfloat a
[4], b
[4], result
[4];
732 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
733 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
734 result
[0] = result
[1] = result
[2] = result
[3] =
735 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + b
[3];
736 store_vector4( inst
, machine
, result
);
739 case FP_OPCODE_DST
: /* Distance vector */
741 GLfloat a
[4], b
[4], result
[4];
742 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
743 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
745 result
[1] = a
[1] * b
[1];
748 store_vector4( inst
, machine
, result
);
751 case FP_OPCODE_EX2
: /* Exponential base 2 */
753 GLfloat a
[4], result
[4];
754 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
755 result
[0] = result
[1] = result
[2] = result
[3] =
756 (GLfloat
) _mesa_pow(2.0, a
[0]);
757 store_vector4( inst
, machine
, result
);
762 GLfloat a
[4], result
[4];
763 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
764 result
[0] = FLOORF(a
[0]);
765 result
[1] = FLOORF(a
[1]);
766 result
[2] = FLOORF(a
[2]);
767 result
[3] = FLOORF(a
[3]);
768 store_vector4( inst
, machine
, result
);
773 GLfloat a
[4], result
[4];
774 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
775 result
[0] = a
[0] - FLOORF(a
[0]);
776 result
[1] = a
[1] - FLOORF(a
[1]);
777 result
[2] = a
[2] - FLOORF(a
[2]);
778 result
[3] = a
[3] - FLOORF(a
[3]);
779 store_vector4( inst
, machine
, result
);
782 case FP_OPCODE_KIL_NV
: /* NV_f_p only */
784 const GLuint swizzle
= inst
->DstReg
.CondSwizzle
;
785 const GLuint condMask
= inst
->DstReg
.CondMask
;
786 if (test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 0)], condMask
) ||
787 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 1)], condMask
) ||
788 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 2)], condMask
) ||
789 test_cc(machine
->CondCodes
[GET_SWZ(swizzle
, 3)], condMask
)) {
794 case FP_OPCODE_KIL
: /* ARB_f_p only */
797 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
798 if (a
[0] < 0.0F
|| a
[1] < 0.0F
|| a
[2] < 0.0F
|| a
[3] < 0.0F
) {
803 case FP_OPCODE_LG2
: /* log base 2 */
805 GLfloat a
[4], result
[4];
806 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
807 result
[0] = result
[1] = result
[2] = result
[3]
809 store_vector4( inst
, machine
, result
);
814 const GLfloat epsilon
= 1.0F
/ 256.0F
; /* from NV VP spec */
815 GLfloat a
[4], result
[4];
816 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
817 a
[0] = MAX2(a
[0], 0.0F
);
818 a
[1] = MAX2(a
[1], 0.0F
);
819 /* XXX ARB version clamps a[3], NV version doesn't */
820 a
[3] = CLAMP(a
[3], -(128.0F
- epsilon
), (128.0F
- epsilon
));
823 /* XXX we could probably just use pow() here */
824 result
[2] = (a
[0] > 0.0F
) ? (GLfloat
) exp(a
[3] * log(a
[1])) : 0.0F
;
826 store_vector4( inst
, machine
, result
);
831 GLfloat a
[4], b
[4], c
[4], result
[4];
832 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
833 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
834 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
835 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
836 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
837 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
838 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
839 store_vector4( inst
, machine
, result
);
844 GLfloat a
[4], b
[4], c
[4], result
[4];
845 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
846 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
847 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
848 result
[0] = a
[0] * b
[0] + c
[0];
849 result
[1] = a
[1] * b
[1] + c
[1];
850 result
[2] = a
[2] * b
[2] + c
[2];
851 result
[3] = a
[3] * b
[3] + c
[3];
852 store_vector4( inst
, machine
, result
);
857 GLfloat a
[4], b
[4], result
[4];
858 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
859 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
860 result
[0] = MAX2(a
[0], b
[0]);
861 result
[1] = MAX2(a
[1], b
[1]);
862 result
[2] = MAX2(a
[2], b
[2]);
863 result
[3] = MAX2(a
[3], b
[3]);
864 store_vector4( inst
, machine
, result
);
866 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
867 result
[0], result
[1], result
[2], result
[3],
868 a
[0], a
[1], a
[2], a
[3],
869 b
[0], b
[1], b
[2], b
[3]);
875 GLfloat a
[4], b
[4], result
[4];
876 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
877 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
878 result
[0] = MIN2(a
[0], b
[0]);
879 result
[1] = MIN2(a
[1], b
[1]);
880 result
[2] = MIN2(a
[2], b
[2]);
881 result
[3] = MIN2(a
[3], b
[3]);
882 store_vector4( inst
, machine
, result
);
888 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, result
);
889 store_vector4( inst
, machine
, result
);
891 printf("MOV (%g %g %g %g)\n",
892 result
[0], result
[1], result
[2], result
[3]);
898 GLfloat a
[4], b
[4], result
[4];
899 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
900 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
901 result
[0] = a
[0] * b
[0];
902 result
[1] = a
[1] * b
[1];
903 result
[2] = a
[2] * b
[2];
904 result
[3] = a
[3] * b
[3];
905 store_vector4( inst
, machine
, result
);
907 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
908 result
[0], result
[1], result
[2], result
[3],
909 a
[0], a
[1], a
[2], a
[3],
910 b
[0], b
[1], b
[2], b
[3]);
914 case FP_OPCODE_PK2H
: /* pack two 16-bit floats in one 32-bit float */
916 GLfloat a
[4], result
[4];
918 GLuint
*rawResult
= (GLuint
*) result
;
920 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
921 hx
= _mesa_float_to_half(a
[0]);
922 hy
= _mesa_float_to_half(a
[1]);
923 twoHalves
= hx
| (hy
<< 16);
924 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
926 store_vector4( inst
, machine
, result
);
929 case FP_OPCODE_PK2US
: /* pack two GLushorts into one 32-bit float */
931 GLfloat a
[4], result
[4];
932 GLuint usx
, usy
, *rawResult
= (GLuint
*) result
;
933 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
934 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
935 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
936 usx
= IROUND(a
[0] * 65535.0F
);
937 usy
= IROUND(a
[1] * 65535.0F
);
938 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
940 store_vector4( inst
, machine
, result
);
943 case FP_OPCODE_PK4B
: /* pack four GLbytes into one 32-bit float */
945 GLfloat a
[4], result
[4];
946 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
947 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
948 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
949 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
950 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
951 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
952 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
953 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
954 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
955 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
956 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
957 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
958 store_vector4( inst
, machine
, result
);
961 case FP_OPCODE_PK4UB
: /* pack four GLubytes into one 32-bit float */
963 GLfloat a
[4], result
[4];
964 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
965 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
966 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
967 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
968 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
969 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
970 ubx
= IROUND(255.0F
* a
[0]);
971 uby
= IROUND(255.0F
* a
[1]);
972 ubz
= IROUND(255.0F
* a
[2]);
973 ubw
= IROUND(255.0F
* a
[3]);
974 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
975 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
976 store_vector4( inst
, machine
, result
);
981 GLfloat a
[4], b
[4], result
[4];
982 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
983 fetch_vector1( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
984 result
[0] = result
[1] = result
[2] = result
[3]
985 = (GLfloat
)_mesa_pow(a
[0], b
[0]);
986 store_vector4( inst
, machine
, result
);
991 GLfloat a
[4], result
[4];
992 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
996 else if (IS_INF_OR_NAN(a
[0]))
997 printf("RCP(inf)\n");
999 result
[0] = result
[1] = result
[2] = result
[3]
1001 store_vector4( inst
, machine
, result
);
1006 GLfloat axis
[4], dir
[4], result
[4], tmp
[4];
1007 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, axis
);
1008 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, dir
);
1009 tmp
[3] = axis
[0] * axis
[0]
1011 + axis
[2] * axis
[2];
1012 tmp
[0] = (2.0F
* (axis
[0] * dir
[0] +
1014 axis
[2] * dir
[2])) / tmp
[3];
1015 result
[0] = tmp
[0] * axis
[0] - dir
[0];
1016 result
[1] = tmp
[0] * axis
[1] - dir
[1];
1017 result
[2] = tmp
[0] * axis
[2] - dir
[2];
1018 /* result[3] is never written! XXX enforce in parser! */
1019 store_vector4( inst
, machine
, result
);
1022 case FP_OPCODE_RSQ
: /* 1 / sqrt() */
1024 GLfloat a
[4], result
[4];
1025 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1026 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
1027 store_vector4( inst
, machine
, result
);
1029 printf("RSQ %g = 1/sqrt(%g)\n", result
[0], a
[0]);
1033 case FP_OPCODE_SCS
: /* sine and cos */
1035 GLfloat a
[4], result
[4];
1036 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1037 result
[0] = (GLfloat
)cos(a
[0]);
1038 result
[1] = (GLfloat
)sin(a
[0]);
1039 result
[2] = 0.0; /* undefined! */
1040 result
[3] = 0.0; /* undefined! */
1041 store_vector4( inst
, machine
, result
);
1044 case FP_OPCODE_SEQ
: /* set on equal */
1046 GLfloat a
[4], b
[4], result
[4];
1047 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1048 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1049 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1050 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1051 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1052 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1053 store_vector4( inst
, machine
, result
);
1056 case FP_OPCODE_SFL
: /* set false, operands ignored */
1058 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1059 store_vector4( inst
, machine
, result
);
1062 case FP_OPCODE_SGE
: /* set on greater or equal */
1064 GLfloat a
[4], b
[4], result
[4];
1065 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1066 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1067 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1068 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1069 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1070 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1071 store_vector4( inst
, machine
, result
);
1074 case FP_OPCODE_SGT
: /* set on greater */
1076 GLfloat a
[4], b
[4], result
[4];
1077 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1078 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1079 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1080 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1081 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1082 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1083 store_vector4( inst
, machine
, result
);
1088 GLfloat a
[4], result
[4];
1089 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1090 result
[0] = result
[1] = result
[2] =
1091 result
[3] = (GLfloat
)_mesa_sin(a
[0]);
1092 store_vector4( inst
, machine
, result
);
1095 case FP_OPCODE_SLE
: /* set on less or equal */
1097 GLfloat a
[4], b
[4], result
[4];
1098 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1099 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1100 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1101 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1102 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1103 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1104 store_vector4( inst
, machine
, result
);
1107 case FP_OPCODE_SLT
: /* set on less */
1109 GLfloat a
[4], b
[4], result
[4];
1110 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1111 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1112 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1113 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1114 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1115 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1116 store_vector4( inst
, machine
, result
);
1119 case FP_OPCODE_SNE
: /* set on not equal */
1121 GLfloat a
[4], b
[4], result
[4];
1122 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1123 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1124 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1125 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1126 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1127 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1128 store_vector4( inst
, machine
, result
);
1131 case FP_OPCODE_STR
: /* set true, operands ignored */
1133 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1134 store_vector4( inst
, machine
, result
);
1139 GLfloat a
[4], b
[4], result
[4];
1140 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1141 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1142 result
[0] = a
[0] - b
[0];
1143 result
[1] = a
[1] - b
[1];
1144 result
[2] = a
[2] - b
[2];
1145 result
[3] = a
[3] - b
[3];
1146 store_vector4( inst
, machine
, result
);
1151 const struct fp_src_register
*source
= &inst
->SrcReg
[0];
1152 const GLfloat
*src
= get_register_pointer(ctx
, source
,
1157 /* do extended swizzling here */
1158 for (i
= 0; i
< 3; i
++) {
1159 if (GET_SWZ(source
->Swizzle
, i
) == SWIZZLE_ZERO
)
1161 else if (GET_SWZ(source
->Swizzle
, i
) == SWIZZLE_ONE
)
1164 result
[i
] = -src
[GET_SWZ(source
->Swizzle
, i
)];
1166 if (source
->NegateBase
)
1167 result
[i
] = -result
[i
];
1169 store_vector4( inst
, machine
, result
);
1172 case FP_OPCODE_TEX
: /* Both ARB and NV frag prog */
1175 GLfloat texcoord
[4], color
[4];
1176 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1177 /* Note: we pass 0 for LOD. The ARB extension requires it
1178 * while the NV extension says it's implementation dependant.
1180 /* KW: Previously lambda was passed as zero, but I
1181 * believe this is incorrect, the spec seems to
1182 * indicate rather that lambda should not be
1183 * changed/biased, unlike TXB where texcoord[3] is
1184 * added to the lambda calculations. The lambda should
1185 * still be calculated normally for TEX & TXP though,
1186 * not set to zero. Otherwise it's very difficult to
1187 * implement normal GL semantics through the fragment
1190 fetch_texel( ctx
, texcoord
,
1191 span
->array
->lambda
[inst
->TexSrcUnit
][column
],
1192 inst
->TexSrcUnit
, color
);
1195 printf("color[3] = %f\n", color
[3]);
1197 store_vector4( inst
, machine
, color
);
1200 case FP_OPCODE_TXB
: /* GL_ARB_fragment_program only */
1201 /* Texel lookup with LOD bias */
1203 GLfloat texcoord
[4], color
[4], bias
, lambda
;
1205 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1206 /* texcoord[3] is the bias to add to lambda */
1207 bias
= ctx
->Texture
.Unit
[inst
->TexSrcUnit
].LodBias
1208 + ctx
->Texture
.Unit
[inst
->TexSrcUnit
]._Current
->LodBias
1210 lambda
= span
->array
->lambda
[inst
->TexSrcUnit
][column
] + bias
;
1211 fetch_texel( ctx
, texcoord
, lambda
,
1212 inst
->TexSrcUnit
, color
);
1213 store_vector4( inst
, machine
, color
);
1216 case FP_OPCODE_TXD
: /* GL_NV_fragment_program only */
1217 /* Texture lookup w/ partial derivatives for LOD */
1219 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1220 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1221 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, dtdx
);
1222 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, dtdy
);
1223 fetch_texel_deriv( ctx
, texcoord
, dtdx
, dtdy
, inst
->TexSrcUnit
,
1225 store_vector4( inst
, machine
, color
);
1228 case FP_OPCODE_TXP
: /* GL_ARB_fragment_program only */
1229 /* Texture lookup w/ projective divide */
1231 GLfloat texcoord
[4], color
[4];
1232 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1233 /* Not so sure about this test - if texcoord[3] is
1234 * zero, we'd probably be fine except for an ASSERT in
1235 * IROUND_POS() which gets triggered by the inf values created.
1237 if (texcoord
[3] != 0.0) {
1238 texcoord
[0] /= texcoord
[3];
1239 texcoord
[1] /= texcoord
[3];
1240 texcoord
[2] /= texcoord
[3];
1242 /* KW: Previously lambda was passed as zero, but I
1243 * believe this is incorrect, the spec seems to
1244 * indicate rather that lambda should not be
1245 * changed/biased, unlike TXB where texcoord[3] is
1246 * added to the lambda calculations. The lambda should
1247 * still be calculated normally for TEX & TXP though,
1250 fetch_texel( ctx
, texcoord
,
1251 span
->array
->lambda
[inst
->TexSrcUnit
][column
],
1252 inst
->TexSrcUnit
, color
);
1253 store_vector4( inst
, machine
, color
);
1256 case FP_OPCODE_TXP_NV
: /* GL_NV_fragment_program only */
1257 /* Texture lookup w/ projective divide */
1259 GLfloat texcoord
[4], color
[4];
1260 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1261 if (inst
->TexSrcIdx
!= TEXTURE_CUBE_INDEX
&&
1262 texcoord
[3] != 0.0) {
1263 texcoord
[0] /= texcoord
[3];
1264 texcoord
[1] /= texcoord
[3];
1265 texcoord
[2] /= texcoord
[3];
1267 fetch_texel( ctx
, texcoord
,
1268 span
->array
->lambda
[inst
->TexSrcUnit
][column
],
1269 inst
->TexSrcUnit
, color
);
1270 store_vector4( inst
, machine
, color
);
1273 case FP_OPCODE_UP2H
: /* unpack two 16-bit floats */
1275 GLfloat a
[4], result
[4];
1276 const GLuint
*rawBits
= (const GLuint
*) a
;
1278 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1279 hx
= rawBits
[0] & 0xffff;
1280 hy
= rawBits
[0] >> 16;
1281 result
[0] = result
[2] = _mesa_half_to_float(hx
);
1282 result
[1] = result
[3] = _mesa_half_to_float(hy
);
1283 store_vector4( inst
, machine
, result
);
1286 case FP_OPCODE_UP2US
: /* unpack two GLushorts */
1288 GLfloat a
[4], result
[4];
1289 const GLuint
*rawBits
= (const GLuint
*) a
;
1291 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1292 usx
= rawBits
[0] & 0xffff;
1293 usy
= rawBits
[0] >> 16;
1294 result
[0] = result
[2] = usx
* (1.0f
/ 65535.0f
);
1295 result
[1] = result
[3] = usy
* (1.0f
/ 65535.0f
);
1296 store_vector4( inst
, machine
, result
);
1299 case FP_OPCODE_UP4B
: /* unpack four GLbytes */
1301 GLfloat a
[4], result
[4];
1302 const GLuint
*rawBits
= (const GLuint
*) a
;
1303 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1304 result
[0] = (((rawBits
[0] >> 0) & 0xff) - 128) / 127.0F
;
1305 result
[1] = (((rawBits
[0] >> 8) & 0xff) - 128) / 127.0F
;
1306 result
[2] = (((rawBits
[0] >> 16) & 0xff) - 128) / 127.0F
;
1307 result
[3] = (((rawBits
[0] >> 24) & 0xff) - 128) / 127.0F
;
1308 store_vector4( inst
, machine
, result
);
1311 case FP_OPCODE_UP4UB
: /* unpack four GLubytes */
1313 GLfloat a
[4], result
[4];
1314 const GLuint
*rawBits
= (const GLuint
*) a
;
1315 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1316 result
[0] = ((rawBits
[0] >> 0) & 0xff) / 255.0F
;
1317 result
[1] = ((rawBits
[0] >> 8) & 0xff) / 255.0F
;
1318 result
[2] = ((rawBits
[0] >> 16) & 0xff) / 255.0F
;
1319 result
[3] = ((rawBits
[0] >> 24) & 0xff) / 255.0F
;
1320 store_vector4( inst
, machine
, result
);
1323 case FP_OPCODE_XPD
: /* cross product */
1325 GLfloat a
[4], b
[4], result
[4];
1326 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1327 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1328 result
[0] = a
[1] * b
[2] - a
[2] * b
[1];
1329 result
[1] = a
[2] * b
[0] - a
[0] * b
[2];
1330 result
[2] = a
[0] * b
[1] - a
[1] * b
[0];
1332 store_vector4( inst
, machine
, result
);
1335 case FP_OPCODE_X2D
: /* 2-D matrix transform */
1337 GLfloat a
[4], b
[4], c
[4], result
[4];
1338 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1339 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1340 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
1341 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1342 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1343 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1344 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1345 store_vector4( inst
, machine
, result
);
1348 case FP_OPCODE_PRINT
:
1350 if (inst
->SrcReg
[0].File
!= -1) {
1352 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1353 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst
->Data
,
1354 a
[0], a
[1], a
[2], a
[3]);
1357 _mesa_printf("%s\n", (const char *) inst
->Data
);
1364 _mesa_problem(ctx
, "Bad opcode %d in _mesa_exec_fragment_program",
1366 return GL_TRUE
; /* return value doesn't matter */
1374 init_machine( GLcontext
*ctx
, struct fp_machine
*machine
,
1375 const struct fragment_program
*program
,
1376 const struct sw_span
*span
, GLuint col
)
1378 GLuint inputsRead
= program
->InputsRead
;
1381 if (ctx
->FragmentProgram
.CallbackEnabled
)
1384 if (program
->Base
.Target
== GL_FRAGMENT_PROGRAM_NV
) {
1385 /* Clear temporary registers (undefined for ARB_f_p) */
1386 _mesa_bzero(machine
->Temporaries
,
1387 MAX_NV_FRAGMENT_PROGRAM_TEMPS
* 4 * sizeof(GLfloat
));
1390 /* Load input registers */
1391 if (inputsRead
& (1 << FRAG_ATTRIB_WPOS
)) {
1392 GLfloat
*wpos
= machine
->Inputs
[FRAG_ATTRIB_WPOS
];
1393 wpos
[0] = (GLfloat
) span
->x
+ col
;
1394 wpos
[1] = (GLfloat
) span
->y
;
1395 wpos
[2] = (GLfloat
) span
->array
->z
[col
] / ctx
->DrawBuffer
->_DepthMaxF
;
1396 wpos
[3] = span
->w
+ col
* span
->dwdx
;
1398 if (inputsRead
& (1 << FRAG_ATTRIB_COL0
)) {
1399 GLfloat
*col0
= machine
->Inputs
[FRAG_ATTRIB_COL0
];
1400 col0
[0] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][RCOMP
]);
1401 col0
[1] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][GCOMP
]);
1402 col0
[2] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][BCOMP
]);
1403 col0
[3] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][ACOMP
]);
1405 if (inputsRead
& (1 << FRAG_ATTRIB_COL1
)) {
1406 GLfloat
*col1
= machine
->Inputs
[FRAG_ATTRIB_COL1
];
1407 col1
[0] = CHAN_TO_FLOAT(span
->array
->spec
[col
][RCOMP
]);
1408 col1
[1] = CHAN_TO_FLOAT(span
->array
->spec
[col
][GCOMP
]);
1409 col1
[2] = CHAN_TO_FLOAT(span
->array
->spec
[col
][BCOMP
]);
1410 col1
[3] = CHAN_TO_FLOAT(span
->array
->spec
[col
][ACOMP
]);
1412 if (inputsRead
& (1 << FRAG_ATTRIB_FOGC
)) {
1413 GLfloat
*fogc
= machine
->Inputs
[FRAG_ATTRIB_FOGC
];
1414 fogc
[0] = span
->array
->fog
[col
];
1419 for (u
= 0; u
< ctx
->Const
.MaxTextureCoordUnits
; u
++) {
1420 if (inputsRead
& (1 << (FRAG_ATTRIB_TEX0
+ u
))) {
1421 GLfloat
*tex
= machine
->Inputs
[FRAG_ATTRIB_TEX0
+ u
];
1422 /*ASSERT(ctx->Texture._EnabledCoordUnits & (1 << u));*/
1423 COPY_4V(tex
, span
->array
->texcoords
[u
][col
]);
1424 /*ASSERT(tex[0] != 0 || tex[1] != 0 || tex[2] != 0);*/
1428 /* init condition codes */
1429 machine
->CondCodes
[0] = COND_EQ
;
1430 machine
->CondCodes
[1] = COND_EQ
;
1431 machine
->CondCodes
[2] = COND_EQ
;
1432 machine
->CondCodes
[3] = COND_EQ
;
1438 * Execute the current fragment program, operating on the given span.
1441 _swrast_exec_fragment_program( GLcontext
*ctx
, struct sw_span
*span
)
1443 const struct fragment_program
*program
= ctx
->FragmentProgram
._Current
;
1446 ctx
->_CurrentProgram
= GL_FRAGMENT_PROGRAM_ARB
; /* or NV, doesn't matter */
1448 if (program
->Parameters
) {
1449 _mesa_load_state_parameters(ctx
, program
->Parameters
);
1452 for (i
= 0; i
< span
->end
; i
++) {
1453 if (span
->array
->mask
[i
]) {
1454 init_machine(ctx
, &ctx
->FragmentProgram
.Machine
,
1455 ctx
->FragmentProgram
._Current
, span
, i
);
1458 if (!_swrast_execute_codegen_program(ctx
, program
, ~0,
1459 &ctx
->FragmentProgram
.Machine
,
1461 span
->array
->mask
[i
] = GL_FALSE
; /* killed fragment */
1464 if (!execute_program(ctx
, program
, ~0,
1465 &ctx
->FragmentProgram
.Machine
, span
, i
)) {
1466 span
->array
->mask
[i
] = GL_FALSE
; /* killed fragment */
1470 /* Store output registers */
1472 const GLfloat
*colOut
1473 = ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLR
];
1474 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][RCOMP
], colOut
[0]);
1475 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][GCOMP
], colOut
[1]);
1476 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][BCOMP
], colOut
[2]);
1477 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][ACOMP
], colOut
[3]);
1480 if (program
->OutputsWritten
& (1 << FRAG_OUTPUT_DEPR
))
1481 span
->array
->z
[i
] = IROUND(ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_DEPR
][0] * ctx
->DrawBuffer
->_DepthMaxF
);
1485 ctx
->_CurrentProgram
= 0;