2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #include "nvfragprog.h"
33 #include "s_nvfragprog.h"
35 #include "s_texture.h"
38 /* if 1, print some debugging info */
46 fetch_texel( GLcontext
*ctx
, const GLfloat texcoord
[4], GLfloat lambda
,
47 GLuint unit
, GLfloat color
[4] )
50 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
52 swrast
->TextureSample
[unit
](ctx
, unit
, ctx
->Texture
.Unit
[unit
]._Current
,
53 1, (const GLfloat (*)[4]) texcoord
,
55 color
[0] = CHAN_TO_FLOAT(rgba
[0]);
56 color
[1] = CHAN_TO_FLOAT(rgba
[1]);
57 color
[2] = CHAN_TO_FLOAT(rgba
[2]);
58 color
[3] = CHAN_TO_FLOAT(rgba
[3]);
63 * Fetch a texel with the given partial derivatives to compute a level
64 * of detail in the mipmap.
67 fetch_texel_deriv( GLcontext
*ctx
, const GLfloat texcoord
[4],
68 const GLfloat texdx
[4], const GLfloat texdy
[4],
69 GLuint unit
, GLfloat color
[4] )
71 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
72 const struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[unit
]._Current
;
73 const struct gl_texture_image
*texImg
= texObj
->Image
[texObj
->BaseLevel
];
74 const GLfloat texW
= (GLfloat
) texImg
->WidthScale
;
75 const GLfloat texH
= (GLfloat
) texImg
->HeightScale
;
78 GLfloat lambda
= _swrast_compute_lambda(texdx
[0], texdy
[0], /* ds/dx, ds/dy */
79 texdx
[1], texdy
[1], /* dt/dx, dt/dy */
80 texdx
[3], texdy
[2], /* dq/dx, dq/dy */
82 texcoord
[0], texcoord
[1], texcoord
[3],
85 swrast
->TextureSample
[unit
](ctx
, unit
, ctx
->Texture
.Unit
[unit
]._Current
,
86 1, (const GLfloat (*)[4]) texcoord
,
88 color
[0] = CHAN_TO_FLOAT(rgba
[0]);
89 color
[1] = CHAN_TO_FLOAT(rgba
[1]);
90 color
[2] = CHAN_TO_FLOAT(rgba
[2]);
91 color
[3] = CHAN_TO_FLOAT(rgba
[3]);
97 * Fetch a 4-element float vector from the given source register.
98 * Apply swizzling and negating as needed.
101 fetch_vector4( GLcontext
*ctx
,
102 const struct fp_src_register
*source
,
103 struct fp_machine
*machine
,
104 const struct fragment_program
*program
,
109 switch (source
->File
) {
110 case PROGRAM_TEMPORARY
:
111 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_TEMPS
);
112 src
= machine
->Temporaries
[source
->Index
];
115 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_INPUTS
);
116 src
= machine
->Inputs
[source
->Index
];
118 case PROGRAM_LOCAL_PARAM
:
119 ASSERT(source
->Index
< MAX_PROGRAM_LOCAL_PARAMS
);
120 src
= program
->Base
.LocalParams
[source
->Index
];
122 case PROGRAM_ENV_PARAM
:
123 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_PARAMS
);
124 src
= ctx
->FragmentProgram
.Parameters
[source
->Index
];
126 case PROGRAM_NAMED_PARAM
:
127 ASSERT(source
->Index
< program
->Parameters
->NumParameters
);
128 src
= program
->Parameters
->Parameters
[source
->Index
].Values
;
130 case PROGRAM_STATE_VAR
:
133 _mesa_problem(ctx
, "Invalid input register file in fetch_vector4");
137 result
[0] = src
[source
->Swizzle
[0]];
138 result
[1] = src
[source
->Swizzle
[1]];
139 result
[2] = src
[source
->Swizzle
[2]];
140 result
[3] = src
[source
->Swizzle
[3]];
142 if (source
->NegateBase
) {
143 result
[0] = -result
[0];
144 result
[1] = -result
[1];
145 result
[2] = -result
[2];
146 result
[3] = -result
[3];
149 result
[0] = FABSF(result
[0]);
150 result
[1] = FABSF(result
[1]);
151 result
[2] = FABSF(result
[2]);
152 result
[3] = FABSF(result
[3]);
154 if (source
->NegateAbs
) {
155 result
[0] = -result
[0];
156 result
[1] = -result
[1];
157 result
[2] = -result
[2];
158 result
[3] = -result
[3];
164 * Fetch the derivative with respect to X for the given register.
165 * \return GL_TRUE if it was easily computed or GL_FALSE if we
166 * need to execute another instance of the program (ugh)!
169 fetch_vector4_deriv( const struct fp_src_register
*source
,
170 const struct sw_span
*span
,
171 char xOrY
, GLfloat result
[4] )
175 ASSERT(xOrY
== 'X' || xOrY
== 'Y');
177 assert(source
->File
== PROGRAM_INPUT
);
179 switch (source
->Index
) {
180 case FRAG_ATTRIB_WPOS
:
194 case FRAG_ATTRIB_COL0
:
196 src
[0] = span
->drdx
* (1.0F
/ CHAN_MAXF
);
197 src
[1] = span
->dgdx
* (1.0F
/ CHAN_MAXF
);
198 src
[2] = span
->dbdx
* (1.0F
/ CHAN_MAXF
);
199 src
[3] = span
->dadx
* (1.0F
/ CHAN_MAXF
);
202 src
[0] = span
->drdy
* (1.0F
/ CHAN_MAXF
);
203 src
[1] = span
->dgdy
* (1.0F
/ CHAN_MAXF
);
204 src
[2] = span
->dbdy
* (1.0F
/ CHAN_MAXF
);
205 src
[3] = span
->dady
* (1.0F
/ CHAN_MAXF
);
208 case FRAG_ATTRIB_COL1
:
210 src
[0] = span
->dsrdx
* (1.0F
/ CHAN_MAXF
);
211 src
[1] = span
->dsgdx
* (1.0F
/ CHAN_MAXF
);
212 src
[2] = span
->dsbdx
* (1.0F
/ CHAN_MAXF
);
213 src
[3] = 0.0; /* XXX need this */
216 src
[0] = span
->dsrdy
* (1.0F
/ CHAN_MAXF
);
217 src
[1] = span
->dsgdy
* (1.0F
/ CHAN_MAXF
);
218 src
[2] = span
->dsbdy
* (1.0F
/ CHAN_MAXF
);
219 src
[3] = 0.0; /* XXX need this */
222 case FRAG_ATTRIB_FOGC
:
224 src
[0] = span
->dfogdx
;
230 src
[0] = span
->dfogdy
;
236 case FRAG_ATTRIB_TEX0
:
237 case FRAG_ATTRIB_TEX1
:
238 case FRAG_ATTRIB_TEX2
:
239 case FRAG_ATTRIB_TEX3
:
240 case FRAG_ATTRIB_TEX4
:
241 case FRAG_ATTRIB_TEX5
:
242 case FRAG_ATTRIB_TEX6
:
243 case FRAG_ATTRIB_TEX7
:
245 const GLuint u
= source
->Index
- FRAG_ATTRIB_TEX0
;
246 src
[0] = span
->texStepX
[u
][0] * (1.0F
/ CHAN_MAXF
);
247 src
[1] = span
->texStepX
[u
][1] * (1.0F
/ CHAN_MAXF
);
248 src
[2] = span
->texStepX
[u
][2] * (1.0F
/ CHAN_MAXF
);
249 src
[3] = span
->texStepX
[u
][3] * (1.0F
/ CHAN_MAXF
);
252 const GLuint u
= source
->Index
- FRAG_ATTRIB_TEX0
;
253 src
[0] = span
->texStepY
[u
][0] * (1.0F
/ CHAN_MAXF
);
254 src
[1] = span
->texStepY
[u
][1] * (1.0F
/ CHAN_MAXF
);
255 src
[2] = span
->texStepY
[u
][2] * (1.0F
/ CHAN_MAXF
);
256 src
[3] = span
->texStepY
[u
][3] * (1.0F
/ CHAN_MAXF
);
263 result
[0] = src
[source
->Swizzle
[0]];
264 result
[1] = src
[source
->Swizzle
[1]];
265 result
[2] = src
[source
->Swizzle
[2]];
266 result
[3] = src
[source
->Swizzle
[3]];
268 if (source
->NegateBase
) {
269 result
[0] = -result
[0];
270 result
[1] = -result
[1];
271 result
[2] = -result
[2];
272 result
[3] = -result
[3];
275 result
[0] = FABSF(result
[0]);
276 result
[1] = FABSF(result
[1]);
277 result
[2] = FABSF(result
[2]);
278 result
[3] = FABSF(result
[3]);
280 if (source
->NegateAbs
) {
281 result
[0] = -result
[0];
282 result
[1] = -result
[1];
283 result
[2] = -result
[2];
284 result
[3] = -result
[3];
291 * As above, but only return result[0] element.
294 fetch_vector1( GLcontext
*ctx
,
295 const struct fp_src_register
*source
,
296 const struct fp_machine
*machine
,
297 const struct fragment_program
*program
,
302 switch (source
->File
) {
303 case PROGRAM_TEMPORARY
:
304 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_TEMPS
);
305 src
= machine
->Temporaries
[source
->Index
];
308 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_INPUTS
);
309 src
= machine
->Inputs
[source
->Index
];
311 case PROGRAM_LOCAL_PARAM
:
312 ASSERT(source
->Index
< MAX_PROGRAM_LOCAL_PARAMS
);
313 src
= program
->Base
.LocalParams
[source
->Index
];
315 case PROGRAM_ENV_PARAM
:
316 ASSERT(source
->Index
< MAX_NV_FRAGMENT_PROGRAM_PARAMS
);
317 src
= ctx
->FragmentProgram
.Parameters
[source
->Index
];
319 case PROGRAM_NAMED_PARAM
:
320 ASSERT(source
->Index
< program
->Parameters
->NumParameters
);
321 src
= program
->Parameters
->Parameters
[source
->Index
].Values
;
323 case PROGRAM_STATE_VAR
:
326 _mesa_problem(ctx
, "Invalid input register file in fetch_vector1");
330 result
[0] = src
[source
->Swizzle
[0]];
332 if (source
->NegateBase
) {
333 result
[0] = -result
[0];
336 result
[0] = FABSF(result
[0]);
338 if (source
->NegateAbs
) {
339 result
[0] = -result
[0];
345 * Test value against zero and return GT, LT, EQ or UN if NaN.
348 generate_cc( float value
)
351 return COND_UN
; /* NaN */
360 * Test if the ccMaskRule is satisfied by the given condition code.
361 * Used to mask destination writes according to the current condition codee.
363 static INLINE GLboolean
364 test_cc(GLuint condCode
, GLuint ccMaskRule
)
366 switch (ccMaskRule
) {
367 case COND_EQ
: return (condCode
== COND_EQ
);
368 case COND_NE
: return (condCode
!= COND_EQ
);
369 case COND_LT
: return (condCode
== COND_LT
);
370 case COND_GE
: return (condCode
== COND_GT
|| condCode
== COND_EQ
);
371 case COND_LE
: return (condCode
== COND_LT
|| condCode
== COND_EQ
);
372 case COND_GT
: return (condCode
== COND_GT
);
373 case COND_TR
: return GL_TRUE
;
374 case COND_FL
: return GL_FALSE
;
375 default: return GL_TRUE
;
381 * Store 4 floats into a register. Observe the instructions saturate and
382 * set-condition-code flags.
385 store_vector4( const struct fp_instruction
*inst
,
386 struct fp_machine
*machine
,
387 const GLfloat value
[4] )
389 const struct fp_dst_register
*dest
= &(inst
->DstReg
);
390 const GLboolean clamp
= inst
->Saturate
;
391 const GLboolean updateCC
= inst
->UpdateCondRegister
;
393 GLfloat clampedValue
[4];
394 const GLboolean
*writeMask
= dest
->WriteMask
;
395 GLboolean condWriteMask
[4];
397 switch (dest
->File
) {
399 dstReg
= machine
->Outputs
[dest
->Index
];
401 case PROGRAM_TEMPORARY
:
402 dstReg
= machine
->Temporaries
[dest
->Index
];
405 _mesa_problem(NULL
, "bad register file in store_vector4(fp)");
410 if (value
[0] > 1.0e10
||
411 IS_INF_OR_NAN(value
[0]) ||
412 IS_INF_OR_NAN(value
[1]) ||
413 IS_INF_OR_NAN(value
[2]) ||
414 IS_INF_OR_NAN(value
[3]) )
415 printf("store %g %g %g %g\n", value
[0], value
[1], value
[2], value
[3]);
419 clampedValue
[0] = CLAMP(value
[0], 0.0F
, 1.0F
);
420 clampedValue
[1] = CLAMP(value
[1], 0.0F
, 1.0F
);
421 clampedValue
[2] = CLAMP(value
[2], 0.0F
, 1.0F
);
422 clampedValue
[3] = CLAMP(value
[3], 0.0F
, 1.0F
);
423 value
= clampedValue
;
426 if (dest
->CondMask
!= COND_TR
) {
427 condWriteMask
[0] = writeMask
[0]
428 && test_cc(machine
->CondCodes
[dest
->CondSwizzle
[0]], dest
->CondMask
);
429 condWriteMask
[1] = writeMask
[1]
430 && test_cc(machine
->CondCodes
[dest
->CondSwizzle
[1]], dest
->CondMask
);
431 condWriteMask
[2] = writeMask
[2]
432 && test_cc(machine
->CondCodes
[dest
->CondSwizzle
[2]], dest
->CondMask
);
433 condWriteMask
[3] = writeMask
[3]
434 && test_cc(machine
->CondCodes
[dest
->CondSwizzle
[3]], dest
->CondMask
);
435 writeMask
= condWriteMask
;
439 dstReg
[0] = value
[0];
441 machine
->CondCodes
[0] = generate_cc(value
[0]);
444 dstReg
[1] = value
[1];
446 machine
->CondCodes
[1] = generate_cc(value
[1]);
449 dstReg
[2] = value
[2];
451 machine
->CondCodes
[2] = generate_cc(value
[2]);
454 dstReg
[3] = value
[3];
456 machine
->CondCodes
[3] = generate_cc(value
[3]);
462 * Initialize a new machine state instance from an existing one, adding
463 * the partial derivatives onto the input registers.
464 * Used to implement DDX and DDY instructions in non-trivial cases.
467 init_machine_deriv( GLcontext
*ctx
,
468 const struct fp_machine
*machine
,
469 const struct fragment_program
*program
,
470 const struct sw_span
*span
, char xOrY
,
471 struct fp_machine
*dMachine
)
475 ASSERT(xOrY
== 'X' || xOrY
== 'Y');
477 /* copy existing machine */
478 _mesa_memcpy(dMachine
, machine
, sizeof(struct fp_machine
));
480 /* Clear temporary registers */
481 _mesa_bzero( (void*) machine
->Temporaries
,
482 MAX_NV_FRAGMENT_PROGRAM_TEMPS
* 4 * sizeof(GLfloat
));
484 /* Add derivatives */
485 if (program
->InputsRead
& (1 << FRAG_ATTRIB_WPOS
)) {
486 GLfloat
*wpos
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_WPOS
];
490 wpos
[2] += span
->dzdx
;
491 wpos
[3] += span
->dwdx
;
496 wpos
[2] += span
->dzdy
;
497 wpos
[3] += span
->dwdy
;
500 if (program
->InputsRead
& (1 << FRAG_ATTRIB_COL0
)) {
501 GLfloat
*col0
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_COL0
];
503 col0
[0] += span
->drdx
* (1.0F
/ CHAN_MAXF
);
504 col0
[1] += span
->dgdx
* (1.0F
/ CHAN_MAXF
);
505 col0
[2] += span
->dbdx
* (1.0F
/ CHAN_MAXF
);
506 col0
[3] += span
->dadx
* (1.0F
/ CHAN_MAXF
);
509 col0
[0] += span
->drdy
* (1.0F
/ CHAN_MAXF
);
510 col0
[1] += span
->dgdy
* (1.0F
/ CHAN_MAXF
);
511 col0
[2] += span
->dbdy
* (1.0F
/ CHAN_MAXF
);
512 col0
[3] += span
->dady
* (1.0F
/ CHAN_MAXF
);
515 if (program
->InputsRead
& (1 << FRAG_ATTRIB_COL1
)) {
516 GLfloat
*col1
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_COL1
];
518 col1
[0] += span
->dsrdx
* (1.0F
/ CHAN_MAXF
);
519 col1
[1] += span
->dsgdx
* (1.0F
/ CHAN_MAXF
);
520 col1
[2] += span
->dsbdx
* (1.0F
/ CHAN_MAXF
);
521 col1
[3] += 0.0; /*XXX fix */
524 col1
[0] += span
->dsrdy
* (1.0F
/ CHAN_MAXF
);
525 col1
[1] += span
->dsgdy
* (1.0F
/ CHAN_MAXF
);
526 col1
[2] += span
->dsbdy
* (1.0F
/ CHAN_MAXF
);
527 col1
[3] += 0.0; /*XXX fix */
530 if (program
->InputsRead
& (1 << FRAG_ATTRIB_FOGC
)) {
531 GLfloat
*fogc
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_FOGC
];
533 fogc
[0] += span
->dfogdx
;
536 fogc
[0] += span
->dfogdy
;
539 for (u
= 0; u
< ctx
->Const
.MaxTextureCoordUnits
; u
++) {
540 if (program
->InputsRead
& (1 << (FRAG_ATTRIB_TEX0
+ u
))) {
541 GLfloat
*tex
= (GLfloat
*) machine
->Inputs
[FRAG_ATTRIB_TEX0
+ u
];
543 tex
[0] += span
->texStepX
[u
][0];
544 tex
[1] += span
->texStepX
[u
][1];
545 tex
[2] += span
->texStepX
[u
][2];
546 tex
[3] += span
->texStepX
[u
][3];
549 tex
[0] += span
->texStepY
[u
][0];
550 tex
[1] += span
->texStepY
[u
][1];
551 tex
[2] += span
->texStepY
[u
][2];
552 tex
[3] += span
->texStepY
[u
][3];
557 /* init condition codes */
558 dMachine
->CondCodes
[0] = COND_EQ
;
559 dMachine
->CondCodes
[1] = COND_EQ
;
560 dMachine
->CondCodes
[2] = COND_EQ
;
561 dMachine
->CondCodes
[3] = COND_EQ
;
566 * Execute the given vertex program.
567 * NOTE: we do everything in single-precision floating point; we don't
568 * currently observe the single/half/fixed-precision qualifiers.
569 * \param ctx - rendering context
570 * \param program - the fragment program to execute
571 * \param machine - machine state (register file)
572 * \param maxInst - max number of instructions to execute
573 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
576 execute_program( GLcontext
*ctx
,
577 const struct fragment_program
*program
, GLuint maxInst
,
578 struct fp_machine
*machine
, const struct sw_span
*span
,
584 printf("execute fragment program --------------------\n");
587 for (pc
= 0; pc
< maxInst
; pc
++) {
588 const struct fp_instruction
*inst
= program
->Instructions
+ pc
;
590 if (ctx
->FragmentProgram
.CallbackEnabled
&&
591 ctx
->FragmentProgram
.Callback
) {
592 ctx
->FragmentProgram
.CurrentPosition
= inst
->StringPos
;
593 ctx
->FragmentProgram
.Callback(program
->Base
.Target
,
594 ctx
->FragmentProgram
.CallbackData
);
597 switch (inst
->Opcode
) {
600 GLfloat a
[4], result
[4];
601 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
602 result
[0] = FABSF(a
[0]);
603 result
[1] = FABSF(a
[1]);
604 result
[2] = FABSF(a
[2]);
605 result
[3] = FABSF(a
[3]);
606 store_vector4( inst
, machine
, result
);
611 GLfloat a
[4], b
[4], result
[4];
612 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
613 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
614 result
[0] = a
[0] + b
[0];
615 result
[1] = a
[1] + b
[1];
616 result
[2] = a
[2] + b
[2];
617 result
[3] = a
[3] + b
[3];
618 store_vector4( inst
, machine
, result
);
623 GLfloat a
[4], b
[4], c
[4], result
[4];
624 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
625 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
626 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
627 result
[0] = a
[0] < 0.0F
? b
[0] : c
[0];
628 result
[1] = a
[1] < 0.0F
? b
[1] : c
[1];
629 result
[2] = a
[2] < 0.0F
? b
[2] : c
[2];
630 result
[3] = a
[3] < 0.0F
? b
[3] : c
[3];
631 store_vector4( inst
, machine
, result
);
636 GLfloat a
[4], result
[4];
637 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
638 result
[0] = result
[1] = result
[2] = result
[3] = (GLfloat
)_mesa_cos(a
[0]);
639 store_vector4( inst
, machine
, result
);
642 case FP_OPCODE_DDX
: /* Partial derivative with respect to X */
644 GLfloat a
[4], aNext
[4], result
[4];
645 struct fp_machine dMachine
;
646 if (!fetch_vector4_deriv(&inst
->SrcReg
[0], span
, 'X', result
)) {
647 /* This is tricky. Make a copy of the current machine state,
648 * increment the input registers by the dx or dy partial
649 * derivatives, then re-execute the program up to the
650 * preceeding instruction, then fetch the source register.
651 * Finally, find the difference in the register values for
652 * the original and derivative runs.
654 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
655 init_machine_deriv(ctx
, machine
, program
, span
,
657 execute_program(ctx
, program
, pc
, &dMachine
, span
, column
);
658 fetch_vector4( ctx
, &inst
->SrcReg
[0], &dMachine
, program
, aNext
);
659 result
[0] = aNext
[0] - a
[0];
660 result
[1] = aNext
[1] - a
[1];
661 result
[2] = aNext
[2] - a
[2];
662 result
[3] = aNext
[3] - a
[3];
664 store_vector4( inst
, machine
, result
);
667 case FP_OPCODE_DDY
: /* Partial derivative with respect to Y */
669 GLfloat a
[4], aNext
[4], result
[4];
670 struct fp_machine dMachine
;
671 if (!fetch_vector4_deriv(&inst
->SrcReg
[0], span
, 'Y', result
)) {
672 init_machine_deriv(ctx
, machine
, program
, span
,
674 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
675 execute_program(ctx
, program
, pc
, &dMachine
, span
, column
);
676 fetch_vector4( ctx
, &inst
->SrcReg
[0], &dMachine
, program
, aNext
);
677 result
[0] = aNext
[0] - a
[0];
678 result
[1] = aNext
[1] - a
[1];
679 result
[2] = aNext
[2] - a
[2];
680 result
[3] = aNext
[3] - a
[3];
682 store_vector4( inst
, machine
, result
);
687 GLfloat a
[4], b
[4], result
[4];
688 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
689 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
690 result
[0] = result
[1] = result
[2] = result
[3] =
691 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2];
692 store_vector4( inst
, machine
, result
);
694 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
695 result
[0], a
[0], a
[1], a
[2], b
[0], b
[1], b
[2]);
701 GLfloat a
[4], b
[4], result
[4];
702 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
703 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
704 result
[0] = result
[1] = result
[2] = result
[3] =
705 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + a
[3] * b
[3];
706 store_vector4( inst
, machine
, result
);
711 GLfloat a
[4], b
[4], result
[4];
712 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
713 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
714 result
[0] = result
[1] = result
[2] = result
[3] =
715 a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + b
[3];
716 store_vector4( inst
, machine
, result
);
719 case FP_OPCODE_DST
: /* Distance vector */
721 GLfloat a
[4], b
[4], result
[4];
722 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
723 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
725 result
[1] = a
[1] * b
[1];
728 store_vector4( inst
, machine
, result
);
731 case FP_OPCODE_EX2
: /* Exponential base 2 */
733 GLfloat a
[4], result
[4];
734 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
735 result
[0] = result
[1] = result
[2] = result
[3] =
736 (GLfloat
) _mesa_pow(2.0, a
[0]);
737 store_vector4( inst
, machine
, result
);
742 GLfloat a
[4], result
[4];
743 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
744 result
[0] = FLOORF(a
[0]);
745 result
[1] = FLOORF(a
[1]);
746 result
[2] = FLOORF(a
[2]);
747 result
[3] = FLOORF(a
[3]);
748 store_vector4( inst
, machine
, result
);
753 GLfloat a
[4], result
[4];
754 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
755 result
[0] = a
[0] - FLOORF(a
[0]);
756 result
[1] = a
[1] - FLOORF(a
[1]);
757 result
[2] = a
[2] - FLOORF(a
[2]);
758 result
[3] = a
[3] - FLOORF(a
[3]);
759 store_vector4( inst
, machine
, result
);
764 const GLuint
*swizzle
= inst
->DstReg
.CondSwizzle
;
765 const GLuint condMask
= inst
->DstReg
.CondMask
;
766 if (test_cc(machine
->CondCodes
[swizzle
[0]], condMask
) ||
767 test_cc(machine
->CondCodes
[swizzle
[1]], condMask
) ||
768 test_cc(machine
->CondCodes
[swizzle
[2]], condMask
) ||
769 test_cc(machine
->CondCodes
[swizzle
[3]], condMask
)) {
774 case FP_OPCODE_LG2
: /* log base 2 */
776 GLfloat a
[4], result
[4];
777 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
778 result
[0] = result
[1] = result
[2] = result
[3]
780 store_vector4( inst
, machine
, result
);
785 GLfloat a
[4], result
[4];
786 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
793 result
[2] = (a
[0] > 0.0F
) ? (GLfloat
)_mesa_pow(2.0, a
[3]) : 0.0F
;
795 store_vector4( inst
, machine
, result
);
800 GLfloat a
[4], b
[4], c
[4], result
[4];
801 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
802 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
803 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
804 result
[0] = a
[0] * b
[0] + (1.0F
- a
[0]) * c
[0];
805 result
[1] = a
[1] * b
[1] + (1.0F
- a
[1]) * c
[1];
806 result
[2] = a
[2] * b
[2] + (1.0F
- a
[2]) * c
[2];
807 result
[3] = a
[3] * b
[3] + (1.0F
- a
[3]) * c
[3];
808 store_vector4( inst
, machine
, result
);
813 GLfloat a
[4], b
[4], c
[4], result
[4];
814 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
815 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
816 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
817 result
[0] = a
[0] * b
[0] + c
[0];
818 result
[1] = a
[1] * b
[1] + c
[1];
819 result
[2] = a
[2] * b
[2] + c
[2];
820 result
[3] = a
[3] * b
[3] + c
[3];
821 store_vector4( inst
, machine
, result
);
826 GLfloat a
[4], b
[4], result
[4];
827 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
828 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
829 result
[0] = MAX2(a
[0], b
[0]);
830 result
[1] = MAX2(a
[1], b
[1]);
831 result
[2] = MAX2(a
[2], b
[2]);
832 result
[3] = MAX2(a
[3], b
[3]);
833 store_vector4( inst
, machine
, result
);
838 GLfloat a
[4], b
[4], result
[4];
839 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
840 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
841 result
[0] = MIN2(a
[0], b
[0]);
842 result
[1] = MIN2(a
[1], b
[1]);
843 result
[2] = MIN2(a
[2], b
[2]);
844 result
[3] = MIN2(a
[3], b
[3]);
845 store_vector4( inst
, machine
, result
);
851 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, result
);
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] = a
[0] * b
[0];
861 result
[1] = a
[1] * b
[1];
862 result
[2] = a
[2] * b
[2];
863 result
[3] = a
[3] * b
[3];
864 store_vector4( inst
, machine
, result
);
866 printf("MUL (%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]);
873 case FP_OPCODE_PK2H
: /* pack two 16-bit floats */
874 /* XXX this is probably wrong */
876 GLfloat a
[4], result
[4];
877 const GLuint
*rawBits
= (const GLuint
*) a
;
878 GLuint
*rawResult
= (GLuint
*) result
;
879 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
880 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
881 = rawBits
[0] | (rawBits
[1] << 16);
882 store_vector4( inst
, machine
, result
);
885 case FP_OPCODE_PK2US
: /* pack two GLushorts */
887 GLfloat a
[4], result
[4];
888 GLuint usx
, usy
, *rawResult
= (GLuint
*) result
;
889 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
890 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
891 a
[1] = CLAMP(a
[0], 0.0F
, 1.0F
);
892 usx
= IROUND(a
[0] * 65535.0F
);
893 usy
= IROUND(a
[1] * 65535.0F
);
894 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
896 store_vector4( inst
, machine
, result
);
899 case FP_OPCODE_PK4B
: /* pack four GLbytes */
901 GLfloat a
[4], result
[4];
902 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
903 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
904 a
[0] = CLAMP(a
[0], -128.0F
/ 127.0F
, 1.0F
);
905 a
[1] = CLAMP(a
[1], -128.0F
/ 127.0F
, 1.0F
);
906 a
[2] = CLAMP(a
[2], -128.0F
/ 127.0F
, 1.0F
);
907 a
[3] = CLAMP(a
[3], -128.0F
/ 127.0F
, 1.0F
);
908 ubx
= IROUND(127.0F
* a
[0] + 128.0F
);
909 uby
= IROUND(127.0F
* a
[1] + 128.0F
);
910 ubz
= IROUND(127.0F
* a
[2] + 128.0F
);
911 ubw
= IROUND(127.0F
* a
[3] + 128.0F
);
912 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
913 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
914 store_vector4( inst
, machine
, result
);
917 case FP_OPCODE_PK4UB
: /* pack four GLubytes */
919 GLfloat a
[4], result
[4];
920 GLuint ubx
, uby
, ubz
, ubw
, *rawResult
= (GLuint
*) result
;
921 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
922 a
[0] = CLAMP(a
[0], 0.0F
, 1.0F
);
923 a
[1] = CLAMP(a
[1], 0.0F
, 1.0F
);
924 a
[2] = CLAMP(a
[2], 0.0F
, 1.0F
);
925 a
[3] = CLAMP(a
[3], 0.0F
, 1.0F
);
926 ubx
= IROUND(255.0F
* a
[0]);
927 uby
= IROUND(255.0F
* a
[1]);
928 ubz
= IROUND(255.0F
* a
[2]);
929 ubw
= IROUND(255.0F
* a
[3]);
930 rawResult
[0] = rawResult
[1] = rawResult
[2] = rawResult
[3]
931 = ubx
| (uby
<< 8) | (ubz
<< 16) | (ubw
<< 24);
932 store_vector4( inst
, machine
, result
);
937 GLfloat a
[4], b
[4], result
[4];
938 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
939 fetch_vector1( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
940 result
[0] = result
[1] = result
[2] = result
[3]
941 = (GLfloat
)_mesa_pow(a
[0], b
[0]);
942 store_vector4( inst
, machine
, result
);
947 GLfloat a
[4], result
[4];
948 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
952 else if (IS_INF_OR_NAN(a
[0]))
953 printf("RCP(inf)\n");
955 result
[0] = result
[1] = result
[2] = result
[3]
957 store_vector4( inst
, machine
, result
);
962 GLfloat axis
[4], dir
[4], result
[4], tmp
[4];
963 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, axis
);
964 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, dir
);
965 tmp
[3] = axis
[0] * axis
[0]
968 tmp
[0] = (2.0F
* (axis
[0] * dir
[0] +
970 axis
[2] * dir
[2])) / tmp
[3];
971 result
[0] = tmp
[0] * axis
[0] - dir
[0];
972 result
[1] = tmp
[0] * axis
[1] - dir
[1];
973 result
[2] = tmp
[0] * axis
[2] - dir
[2];
974 /* result[3] is never written! XXX enforce in parser! */
975 store_vector4( inst
, machine
, result
);
978 case FP_OPCODE_RSQ
: /* 1 / sqrt() */
980 GLfloat a
[4], result
[4];
981 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
982 result
[0] = result
[1] = result
[2] = result
[3] = INV_SQRTF(a
[0]);
983 store_vector4( inst
, machine
, result
);
985 printf("RSQ %g = 1/sqrt(%g)\n", result
[0], a
[0]);
989 case FP_OPCODE_SCS
: /* sine and cos */
991 GLfloat a
[4], result
[4];
992 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
993 result
[0] = cos(a
[0]);
994 result
[1] = sin(a
[0]);
995 result
[2] = 0.0; /* undefined! */
996 result
[3] = 0.0; /* undefined! */
997 store_vector4( inst
, machine
, result
);
1000 case FP_OPCODE_SEQ
: /* set on equal */
1002 GLfloat a
[4], b
[4], result
[4];
1003 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1004 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1005 result
[0] = (a
[0] == b
[0]) ? 1.0F
: 0.0F
;
1006 result
[1] = (a
[1] == b
[1]) ? 1.0F
: 0.0F
;
1007 result
[2] = (a
[2] == b
[2]) ? 1.0F
: 0.0F
;
1008 result
[3] = (a
[3] == b
[3]) ? 1.0F
: 0.0F
;
1009 store_vector4( inst
, machine
, result
);
1012 case FP_OPCODE_SFL
: /* set false, operands ignored */
1014 static const GLfloat result
[4] = { 0.0F
, 0.0F
, 0.0F
, 0.0F
};
1015 store_vector4( inst
, machine
, result
);
1018 case FP_OPCODE_SGE
: /* set on greater or equal */
1020 GLfloat a
[4], b
[4], result
[4];
1021 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1022 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1023 result
[0] = (a
[0] >= b
[0]) ? 1.0F
: 0.0F
;
1024 result
[1] = (a
[1] >= b
[1]) ? 1.0F
: 0.0F
;
1025 result
[2] = (a
[2] >= b
[2]) ? 1.0F
: 0.0F
;
1026 result
[3] = (a
[3] >= b
[3]) ? 1.0F
: 0.0F
;
1027 store_vector4( inst
, machine
, result
);
1030 case FP_OPCODE_SGT
: /* set on greater */
1032 GLfloat a
[4], b
[4], result
[4];
1033 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1034 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1035 result
[0] = (a
[0] > b
[0]) ? 1.0F
: 0.0F
;
1036 result
[1] = (a
[1] > b
[1]) ? 1.0F
: 0.0F
;
1037 result
[2] = (a
[2] > b
[2]) ? 1.0F
: 0.0F
;
1038 result
[3] = (a
[3] > b
[3]) ? 1.0F
: 0.0F
;
1039 store_vector4( inst
, machine
, result
);
1044 GLfloat a
[4], result
[4];
1045 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1046 result
[0] = result
[1] = result
[2] =
1047 result
[3] = (GLfloat
)_mesa_sin(a
[0]);
1048 store_vector4( inst
, machine
, result
);
1051 case FP_OPCODE_SLE
: /* set on less or equal */
1053 GLfloat a
[4], b
[4], result
[4];
1054 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1055 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1056 result
[0] = (a
[0] <= b
[0]) ? 1.0F
: 0.0F
;
1057 result
[1] = (a
[1] <= b
[1]) ? 1.0F
: 0.0F
;
1058 result
[2] = (a
[2] <= b
[2]) ? 1.0F
: 0.0F
;
1059 result
[3] = (a
[3] <= b
[3]) ? 1.0F
: 0.0F
;
1060 store_vector4( inst
, machine
, result
);
1063 case FP_OPCODE_SLT
: /* set on less */
1065 GLfloat a
[4], b
[4], result
[4];
1066 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1067 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1068 result
[0] = (a
[0] < b
[0]) ? 1.0F
: 0.0F
;
1069 result
[1] = (a
[1] < b
[1]) ? 1.0F
: 0.0F
;
1070 result
[2] = (a
[2] < b
[2]) ? 1.0F
: 0.0F
;
1071 result
[3] = (a
[3] < b
[3]) ? 1.0F
: 0.0F
;
1072 store_vector4( inst
, machine
, result
);
1075 case FP_OPCODE_SNE
: /* set on not equal */
1077 GLfloat a
[4], b
[4], result
[4];
1078 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1079 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1080 result
[0] = (a
[0] != b
[0]) ? 1.0F
: 0.0F
;
1081 result
[1] = (a
[1] != b
[1]) ? 1.0F
: 0.0F
;
1082 result
[2] = (a
[2] != b
[2]) ? 1.0F
: 0.0F
;
1083 result
[3] = (a
[3] != b
[3]) ? 1.0F
: 0.0F
;
1084 store_vector4( inst
, machine
, result
);
1087 case FP_OPCODE_STR
: /* set true, operands ignored */
1089 static const GLfloat result
[4] = { 1.0F
, 1.0F
, 1.0F
, 1.0F
};
1090 store_vector4( inst
, machine
, result
);
1095 GLfloat a
[4], b
[4], result
[4];
1096 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1097 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1098 result
[0] = a
[0] - b
[0];
1099 result
[1] = a
[1] - b
[1];
1100 result
[2] = a
[2] - b
[2];
1101 result
[3] = a
[3] - b
[3];
1102 store_vector4( inst
, machine
, result
);
1107 /* XXX to do: extended swizzle */
1113 GLfloat texcoord
[4], color
[4];
1114 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1115 /* XXX: Undo perspective divide from interpolate_texcoords() */
1116 fetch_texel( ctx
, texcoord
,
1117 span
->array
->lambda
[inst
->TexSrcUnit
][column
],
1118 inst
->TexSrcUnit
, color
);
1119 store_vector4( inst
, machine
, color
);
1123 /* Texel lookup with LOD bias */
1125 GLfloat texcoord
[4], color
[4];
1126 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1127 /* XXX: apply bias from texcoord[3]!!! */
1128 fetch_texel( ctx
, texcoord
,
1129 span
->array
->lambda
[inst
->TexSrcUnit
][column
],
1130 inst
->TexSrcUnit
, color
);
1131 store_vector4( inst
, machine
, color
);
1135 /* Texture lookup w/ partial derivatives for LOD */
1137 GLfloat texcoord
[4], dtdx
[4], dtdy
[4], color
[4];
1138 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1139 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, dtdx
);
1140 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, dtdy
);
1141 fetch_texel_deriv( ctx
, texcoord
, dtdx
, dtdy
, inst
->TexSrcUnit
,
1143 store_vector4( inst
, machine
, color
);
1147 /* Texture lookup w/ perspective divide */
1149 GLfloat texcoord
[4], color
[4];
1150 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, texcoord
);
1151 /* Already did perspective divide in interpolate_texcoords() */
1152 fetch_texel( ctx
, texcoord
,
1153 span
->array
->lambda
[inst
->TexSrcUnit
][column
],
1154 inst
->TexSrcUnit
, color
);
1155 store_vector4( inst
, machine
, color
);
1158 case FP_OPCODE_UP2H
: /* unpack two 16-bit floats */
1159 /* XXX this is probably wrong */
1161 GLfloat a
[4], result
[4];
1162 const GLuint
*rawBits
= (const GLuint
*) a
;
1163 GLuint
*rawResult
= (GLuint
*) result
;
1164 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1165 rawResult
[0] = rawBits
[0] & 0xffff;
1166 rawResult
[1] = (rawBits
[0] >> 16) & 0xffff;
1167 rawResult
[2] = rawBits
[0] & 0xffff;
1168 rawResult
[3] = (rawBits
[0] >> 16) & 0xffff;
1169 store_vector4( inst
, machine
, result
);
1172 case FP_OPCODE_UP2US
: /* unpack two GLushorts */
1174 GLfloat a
[4], result
[4];
1175 const GLuint
*rawBits
= (const GLuint
*) a
;
1176 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1177 result
[0] = (GLfloat
) ((rawBits
[0] >> 0) & 0xffff) / 65535.0F
;
1178 result
[1] = (GLfloat
) ((rawBits
[0] >> 16) & 0xffff) / 65535.0F
;
1179 result
[2] = result
[0];
1180 result
[3] = result
[1];
1181 store_vector4( inst
, machine
, result
);
1184 case FP_OPCODE_UP4B
: /* unpack four GLbytes */
1186 GLfloat a
[4], result
[4];
1187 const GLuint
*rawBits
= (const GLuint
*) a
;
1188 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1189 result
[0] = (((rawBits
[0] >> 0) & 0xff) - 128) / 127.0F
;
1190 result
[0] = (((rawBits
[0] >> 8) & 0xff) - 128) / 127.0F
;
1191 result
[0] = (((rawBits
[0] >> 16) & 0xff) - 128) / 127.0F
;
1192 result
[0] = (((rawBits
[0] >> 24) & 0xff) - 128) / 127.0F
;
1193 store_vector4( inst
, machine
, result
);
1196 case FP_OPCODE_UP4UB
: /* unpack four GLubytes */
1198 GLfloat a
[4], result
[4];
1199 const GLuint
*rawBits
= (const GLuint
*) a
;
1200 fetch_vector1( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1201 result
[0] = ((rawBits
[0] >> 0) & 0xff) / 255.0F
;
1202 result
[0] = ((rawBits
[0] >> 8) & 0xff) / 255.0F
;
1203 result
[0] = ((rawBits
[0] >> 16) & 0xff) / 255.0F
;
1204 result
[0] = ((rawBits
[0] >> 24) & 0xff) / 255.0F
;
1205 store_vector4( inst
, machine
, result
);
1208 case FP_OPCODE_X2D
: /* 2-D matrix transform */
1210 GLfloat a
[4], b
[4], c
[4], result
[4];
1211 fetch_vector4( ctx
, &inst
->SrcReg
[0], machine
, program
, a
);
1212 fetch_vector4( ctx
, &inst
->SrcReg
[1], machine
, program
, b
);
1213 fetch_vector4( ctx
, &inst
->SrcReg
[2], machine
, program
, c
);
1214 result
[0] = a
[0] + b
[0] * c
[0] + b
[1] * c
[1];
1215 result
[1] = a
[1] + b
[0] * c
[2] + b
[1] * c
[3];
1216 result
[2] = a
[2] + b
[0] * c
[0] + b
[1] * c
[1];
1217 result
[3] = a
[3] + b
[0] * c
[2] + b
[1] * c
[3];
1218 store_vector4( inst
, machine
, result
);
1224 _mesa_problem(ctx
, "Bad opcode %d in _mesa_exec_fragment_program",
1226 return GL_TRUE
; /* return value doesn't matter */
1234 init_machine( GLcontext
*ctx
, struct fp_machine
*machine
,
1235 const struct fragment_program
*program
,
1236 const struct sw_span
*span
, GLuint col
)
1238 GLuint inputsRead
= program
->InputsRead
;
1241 if (ctx
->FragmentProgram
.CallbackEnabled
)
1244 /* Clear temporary registers */
1245 _mesa_bzero(machine
->Temporaries
,
1246 MAX_NV_FRAGMENT_PROGRAM_TEMPS
* 4 * sizeof(GLfloat
));
1248 /* Load input registers */
1249 if (inputsRead
& (1 << FRAG_ATTRIB_WPOS
)) {
1250 GLfloat
*wpos
= machine
->Inputs
[FRAG_ATTRIB_WPOS
];
1251 wpos
[0] = (GLfloat
) span
->x
+ col
;
1252 wpos
[1] = (GLfloat
) span
->y
;
1253 wpos
[2] = (GLfloat
) span
->array
->z
[col
] / ctx
->DepthMaxF
;
1254 wpos
[3] = span
->w
+ col
* span
->dwdx
;
1256 if (inputsRead
& (1 << FRAG_ATTRIB_COL0
)) {
1257 GLfloat
*col0
= machine
->Inputs
[FRAG_ATTRIB_COL0
];
1258 col0
[0] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][RCOMP
]);
1259 col0
[1] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][GCOMP
]);
1260 col0
[2] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][BCOMP
]);
1261 col0
[3] = CHAN_TO_FLOAT(span
->array
->rgba
[col
][ACOMP
]);
1263 if (inputsRead
& (1 << FRAG_ATTRIB_COL1
)) {
1264 GLfloat
*col1
= machine
->Inputs
[FRAG_ATTRIB_COL1
];
1265 col1
[0] = CHAN_TO_FLOAT(span
->array
->spec
[col
][RCOMP
]);
1266 col1
[1] = CHAN_TO_FLOAT(span
->array
->spec
[col
][GCOMP
]);
1267 col1
[2] = CHAN_TO_FLOAT(span
->array
->spec
[col
][BCOMP
]);
1268 col1
[3] = CHAN_TO_FLOAT(span
->array
->spec
[col
][ACOMP
]);
1270 if (inputsRead
& (1 << FRAG_ATTRIB_FOGC
)) {
1271 GLfloat
*fogc
= machine
->Inputs
[FRAG_ATTRIB_FOGC
];
1272 fogc
[0] = span
->array
->fog
[col
];
1277 for (u
= 0; u
< ctx
->Const
.MaxTextureCoordUnits
; u
++) {
1278 if (inputsRead
& (1 << (FRAG_ATTRIB_TEX0
+ u
))) {
1279 GLfloat
*tex
= machine
->Inputs
[FRAG_ATTRIB_TEX0
+ u
];
1280 /*ASSERT(ctx->Texture._EnabledCoordUnits & (1 << u));*/
1281 COPY_4V(tex
, span
->array
->texcoords
[u
][col
]);
1282 /*ASSERT(tex[0] != 0 || tex[1] != 0 || tex[2] != 0);*/
1286 /* init condition codes */
1287 machine
->CondCodes
[0] = COND_EQ
;
1288 machine
->CondCodes
[1] = COND_EQ
;
1289 machine
->CondCodes
[2] = COND_EQ
;
1290 machine
->CondCodes
[3] = COND_EQ
;
1295 _swrast_exec_nv_fragment_program( GLcontext
*ctx
, struct sw_span
*span
)
1297 const struct fragment_program
*program
= ctx
->FragmentProgram
.Current
;
1300 ctx
->_CurrentProgram
= GL_FRAGMENT_PROGRAM_ARB
; /* or NV, doesn't matter */
1302 for (i
= 0; i
< span
->end
; i
++) {
1303 if (span
->array
->mask
[i
]) {
1304 init_machine(ctx
, &ctx
->FragmentProgram
.Machine
,
1305 ctx
->FragmentProgram
.Current
, span
, i
);
1307 if (!execute_program(ctx
, program
, ~0,
1308 &ctx
->FragmentProgram
.Machine
, span
, i
)) {
1309 span
->array
->mask
[i
] = GL_FALSE
; /* killed fragment */
1312 /* Store output registers */
1314 const GLfloat
*colOut
1315 = ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_COLR
];
1316 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][RCOMP
], colOut
[0]);
1317 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][GCOMP
], colOut
[1]);
1318 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][BCOMP
], colOut
[2]);
1319 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][ACOMP
], colOut
[3]);
1322 if (program
->OutputsWritten
& (1 << FRAG_OUTPUT_DEPR
))
1323 span
->array
->z
[i
] = IROUND(ctx
->FragmentProgram
.Machine
.Outputs
[FRAG_OUTPUT_DEPR
][0] * ctx
->DepthMaxF
);
1327 ctx
->_CurrentProgram
= 0;