Initial support for floating point and signed texture formats.
[mesa.git] / src / mesa / swrast / s_nvfragprog.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.0
4 *
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25 /*
26 * Regarding GL_NV_fragment_program:
27 *
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.
32 */
33
34 #include "glheader.h"
35 #include "colormac.h"
36 #include "context.h"
37 #include "nvfragprog.h"
38 #include "macros.h"
39 #include "program.h"
40
41 #include "s_nvfragprog.h"
42 #include "s_span.h"
43 #include "s_texture.h"
44
45
46 /* if 1, print some debugging info */
47 #define DEBUG_FRAG 0
48
49 /**
50 * Fetch a texel.
51 */
52 static void
53 fetch_texel( GLcontext *ctx, const GLfloat texcoord[4], GLfloat lambda,
54 GLuint unit, GLfloat color[4] )
55 {
56 GLchan rgba[4];
57 SWcontext *swrast = SWRAST_CONTEXT(ctx);
58
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,
62 &lambda, &rgba);
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]);
67 }
68
69
70 /**
71 * Fetch a texel with the given partial derivatives to compute a level
72 * of detail in the mipmap.
73 */
74 static void
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] )
78 {
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[texObj->BaseLevel];
82 const GLfloat texW = (GLfloat) texImg->WidthScale;
83 const GLfloat texH = (GLfloat) texImg->HeightScale;
84 GLchan rgba[4];
85
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 */
89 texW, texH,
90 texcoord[0], texcoord[1], texcoord[3],
91 1.0F / texcoord[3]);
92
93 swrast->TextureSample[unit](ctx, unit, ctx->Texture.Unit[unit]._Current,
94 1, (const GLfloat (*)[4]) texcoord,
95 &lambda, &rgba);
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]);
100 }
101
102
103 /**
104 * Return a pointer to the 4-element float vector specified by the given
105 * source register.
106 */
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 )
112 {
113 const GLfloat *src;
114 switch (source->File) {
115 case PROGRAM_TEMPORARY:
116 ASSERT(source->Index < MAX_NV_FRAGMENT_PROGRAM_TEMPS);
117 src = machine->Temporaries[source->Index];
118 break;
119 case PROGRAM_INPUT:
120 ASSERT(source->Index < MAX_NV_FRAGMENT_PROGRAM_INPUTS);
121 src = machine->Inputs[source->Index];
122 break;
123 case PROGRAM_LOCAL_PARAM:
124 ASSERT(source->Index < MAX_PROGRAM_LOCAL_PARAMS);
125 src = program->Base.LocalParams[source->Index];
126 break;
127 case PROGRAM_ENV_PARAM:
128 ASSERT(source->Index < MAX_NV_FRAGMENT_PROGRAM_PARAMS);
129 src = ctx->FragmentProgram.Parameters[source->Index];
130 break;
131
132 case PROGRAM_STATE_VAR:
133 /* Fallthrough */
134
135 case PROGRAM_NAMED_PARAM:
136 ASSERT(source->Index < (GLint) program->Parameters->NumParameters);
137 src = program->Parameters->Parameters[source->Index].Values;
138 break;
139 default:
140 _mesa_problem(ctx, "Invalid input register file in fetch_vector4");
141 src = NULL;
142 }
143 return src;
144 }
145
146
147 /**
148 * Fetch a 4-element float vector from the given source register.
149 * Apply swizzling and negating as needed.
150 */
151 static void
152 fetch_vector4( GLcontext *ctx,
153 const struct fp_src_register *source,
154 const struct fp_machine *machine,
155 const struct fragment_program *program,
156 GLfloat result[4] )
157 {
158 const GLfloat *src = get_register_pointer(ctx, source, machine, program);
159 ASSERT(src);
160
161 result[0] = src[source->Swizzle[0]];
162 result[1] = src[source->Swizzle[1]];
163 result[2] = src[source->Swizzle[2]];
164 result[3] = src[source->Swizzle[3]];
165
166 if (source->NegateBase) {
167 result[0] = -result[0];
168 result[1] = -result[1];
169 result[2] = -result[2];
170 result[3] = -result[3];
171 }
172 if (source->Abs) {
173 result[0] = FABSF(result[0]);
174 result[1] = FABSF(result[1]);
175 result[2] = FABSF(result[2]);
176 result[3] = FABSF(result[3]);
177 }
178 if (source->NegateAbs) {
179 result[0] = -result[0];
180 result[1] = -result[1];
181 result[2] = -result[2];
182 result[3] = -result[3];
183 }
184 }
185
186
187 /**
188 * Fetch the derivative with respect to X for the given register.
189 * \return GL_TRUE if it was easily computed or GL_FALSE if we
190 * need to execute another instance of the program (ugh)!
191 */
192 static GLboolean
193 fetch_vector4_deriv( GLcontext *ctx,
194 const struct fp_src_register *source,
195 const struct sw_span *span,
196 char xOrY, GLint column, GLfloat result[4] )
197 {
198 GLfloat src[4];
199
200 ASSERT(xOrY == 'X' || xOrY == 'Y');
201
202 switch (source->Index) {
203 case FRAG_ATTRIB_WPOS:
204 if (xOrY == 'X') {
205 src[0] = 1.0;
206 src[1] = 0.0;
207 src[2] = span->dzdx / ctx->DepthMaxF;
208 src[3] = span->dwdx;
209 }
210 else {
211 src[0] = 0.0;
212 src[1] = 1.0;
213 src[2] = span->dzdy / ctx->DepthMaxF;
214 src[3] = span->dwdy;
215 }
216 break;
217 case FRAG_ATTRIB_COL0:
218 if (xOrY == 'X') {
219 src[0] = span->drdx * (1.0F / CHAN_MAXF);
220 src[1] = span->dgdx * (1.0F / CHAN_MAXF);
221 src[2] = span->dbdx * (1.0F / CHAN_MAXF);
222 src[3] = span->dadx * (1.0F / CHAN_MAXF);
223 }
224 else {
225 src[0] = span->drdy * (1.0F / CHAN_MAXF);
226 src[1] = span->dgdy * (1.0F / CHAN_MAXF);
227 src[2] = span->dbdy * (1.0F / CHAN_MAXF);
228 src[3] = span->dady * (1.0F / CHAN_MAXF);
229 }
230 break;
231 case FRAG_ATTRIB_COL1:
232 if (xOrY == 'X') {
233 src[0] = span->dsrdx * (1.0F / CHAN_MAXF);
234 src[1] = span->dsgdx * (1.0F / CHAN_MAXF);
235 src[2] = span->dsbdx * (1.0F / CHAN_MAXF);
236 src[3] = 0.0; /* XXX need this */
237 }
238 else {
239 src[0] = span->dsrdy * (1.0F / CHAN_MAXF);
240 src[1] = span->dsgdy * (1.0F / CHAN_MAXF);
241 src[2] = span->dsbdy * (1.0F / CHAN_MAXF);
242 src[3] = 0.0; /* XXX need this */
243 }
244 break;
245 case FRAG_ATTRIB_FOGC:
246 if (xOrY == 'X') {
247 src[0] = span->dfogdx;
248 src[1] = 0.0;
249 src[2] = 0.0;
250 src[3] = 0.0;
251 }
252 else {
253 src[0] = span->dfogdy;
254 src[1] = 0.0;
255 src[2] = 0.0;
256 src[3] = 0.0;
257 }
258 break;
259 case FRAG_ATTRIB_TEX0:
260 case FRAG_ATTRIB_TEX1:
261 case FRAG_ATTRIB_TEX2:
262 case FRAG_ATTRIB_TEX3:
263 case FRAG_ATTRIB_TEX4:
264 case FRAG_ATTRIB_TEX5:
265 case FRAG_ATTRIB_TEX6:
266 case FRAG_ATTRIB_TEX7:
267 if (xOrY == 'X') {
268 const GLuint u = source->Index - FRAG_ATTRIB_TEX0;
269 /* this is a little tricky - I think I've got it right */
270 const GLfloat invQ = 1.0f / (span->tex[u][3]
271 + span->texStepX[u][3] * column);
272 src[0] = span->texStepX[u][0] * invQ;
273 src[1] = span->texStepX[u][1] * invQ;
274 src[2] = span->texStepX[u][2] * invQ;
275 src[3] = span->texStepX[u][3] * invQ;
276 }
277 else {
278 const GLuint u = source->Index - FRAG_ATTRIB_TEX0;
279 /* Tricky, as above, but in Y direction */
280 const GLfloat invQ = 1.0f / (span->tex[u][3] + span->texStepY[u][3]);
281 src[0] = span->texStepY[u][0] * invQ;
282 src[1] = span->texStepY[u][1] * invQ;
283 src[2] = span->texStepY[u][2] * invQ;
284 src[3] = span->texStepY[u][3] * invQ;
285 }
286 break;
287 default:
288 return GL_FALSE;
289 }
290
291 result[0] = src[source->Swizzle[0]];
292 result[1] = src[source->Swizzle[1]];
293 result[2] = src[source->Swizzle[2]];
294 result[3] = src[source->Swizzle[3]];
295
296 if (source->NegateBase) {
297 result[0] = -result[0];
298 result[1] = -result[1];
299 result[2] = -result[2];
300 result[3] = -result[3];
301 }
302 if (source->Abs) {
303 result[0] = FABSF(result[0]);
304 result[1] = FABSF(result[1]);
305 result[2] = FABSF(result[2]);
306 result[3] = FABSF(result[3]);
307 }
308 if (source->NegateAbs) {
309 result[0] = -result[0];
310 result[1] = -result[1];
311 result[2] = -result[2];
312 result[3] = -result[3];
313 }
314 return GL_TRUE;
315 }
316
317
318 /**
319 * As above, but only return result[0] element.
320 */
321 static void
322 fetch_vector1( GLcontext *ctx,
323 const struct fp_src_register *source,
324 const struct fp_machine *machine,
325 const struct fragment_program *program,
326 GLfloat result[4] )
327 {
328 const GLfloat *src = get_register_pointer(ctx, source, machine, program);
329 ASSERT(src);
330
331 result[0] = src[source->Swizzle[0]];
332
333 if (source->NegateBase) {
334 result[0] = -result[0];
335 }
336 if (source->Abs) {
337 result[0] = FABSF(result[0]);
338 }
339 if (source->NegateAbs) {
340 result[0] = -result[0];
341 }
342 }
343
344
345 /*
346 * Test value against zero and return GT, LT, EQ or UN if NaN.
347 */
348 static INLINE GLuint
349 generate_cc( float value )
350 {
351 if (value != value)
352 return COND_UN; /* NaN */
353 if (value > 0.0F)
354 return COND_GT;
355 if (value < 0.0F)
356 return COND_LT;
357 return COND_EQ;
358 }
359
360 /*
361 * Test if the ccMaskRule is satisfied by the given condition code.
362 * Used to mask destination writes according to the current condition codee.
363 */
364 static INLINE GLboolean
365 test_cc(GLuint condCode, GLuint ccMaskRule)
366 {
367 switch (ccMaskRule) {
368 case COND_EQ: return (condCode == COND_EQ);
369 case COND_NE: return (condCode != COND_EQ);
370 case COND_LT: return (condCode == COND_LT);
371 case COND_GE: return (condCode == COND_GT || condCode == COND_EQ);
372 case COND_LE: return (condCode == COND_LT || condCode == COND_EQ);
373 case COND_GT: return (condCode == COND_GT);
374 case COND_TR: return GL_TRUE;
375 case COND_FL: return GL_FALSE;
376 default: return GL_TRUE;
377 }
378 }
379
380
381 /**
382 * Store 4 floats into a register. Observe the instructions saturate and
383 * set-condition-code flags.
384 */
385 static void
386 store_vector4( const struct fp_instruction *inst,
387 struct fp_machine *machine,
388 const GLfloat value[4] )
389 {
390 const struct fp_dst_register *dest = &(inst->DstReg);
391 const GLboolean clamp = inst->Saturate;
392 const GLboolean updateCC = inst->UpdateCondRegister;
393 GLfloat *dstReg;
394 GLfloat clampedValue[4];
395 const GLboolean *writeMask = dest->WriteMask;
396 GLboolean condWriteMask[4];
397
398 switch (dest->File) {
399 case PROGRAM_OUTPUT:
400 dstReg = machine->Outputs[dest->Index];
401 break;
402 case PROGRAM_TEMPORARY:
403 dstReg = machine->Temporaries[dest->Index];
404 break;
405 default:
406 _mesa_problem(NULL, "bad register file in store_vector4(fp)");
407 return;
408 }
409
410 #if DEBUG_FRAG
411 if (value[0] > 1.0e10 ||
412 IS_INF_OR_NAN(value[0]) ||
413 IS_INF_OR_NAN(value[1]) ||
414 IS_INF_OR_NAN(value[2]) ||
415 IS_INF_OR_NAN(value[3]) )
416 printf("store %g %g %g %g\n", value[0], value[1], value[2], value[3]);
417 #endif
418
419 if (clamp) {
420 clampedValue[0] = CLAMP(value[0], 0.0F, 1.0F);
421 clampedValue[1] = CLAMP(value[1], 0.0F, 1.0F);
422 clampedValue[2] = CLAMP(value[2], 0.0F, 1.0F);
423 clampedValue[3] = CLAMP(value[3], 0.0F, 1.0F);
424 value = clampedValue;
425 }
426
427 if (dest->CondMask != COND_TR) {
428 condWriteMask[0] = writeMask[0]
429 && test_cc(machine->CondCodes[dest->CondSwizzle[0]], dest->CondMask);
430 condWriteMask[1] = writeMask[1]
431 && test_cc(machine->CondCodes[dest->CondSwizzle[1]], dest->CondMask);
432 condWriteMask[2] = writeMask[2]
433 && test_cc(machine->CondCodes[dest->CondSwizzle[2]], dest->CondMask);
434 condWriteMask[3] = writeMask[3]
435 && test_cc(machine->CondCodes[dest->CondSwizzle[3]], dest->CondMask);
436 writeMask = condWriteMask;
437 }
438
439 if (writeMask[0]) {
440 dstReg[0] = value[0];
441 if (updateCC)
442 machine->CondCodes[0] = generate_cc(value[0]);
443 }
444 if (writeMask[1]) {
445 dstReg[1] = value[1];
446 if (updateCC)
447 machine->CondCodes[1] = generate_cc(value[1]);
448 }
449 if (writeMask[2]) {
450 dstReg[2] = value[2];
451 if (updateCC)
452 machine->CondCodes[2] = generate_cc(value[2]);
453 }
454 if (writeMask[3]) {
455 dstReg[3] = value[3];
456 if (updateCC)
457 machine->CondCodes[3] = generate_cc(value[3]);
458 }
459 }
460
461
462 /**
463 * Initialize a new machine state instance from an existing one, adding
464 * the partial derivatives onto the input registers.
465 * Used to implement DDX and DDY instructions in non-trivial cases.
466 */
467 static void
468 init_machine_deriv( GLcontext *ctx,
469 const struct fp_machine *machine,
470 const struct fragment_program *program,
471 const struct sw_span *span, char xOrY,
472 struct fp_machine *dMachine )
473 {
474 GLuint u;
475
476 ASSERT(xOrY == 'X' || xOrY == 'Y');
477
478 /* copy existing machine */
479 _mesa_memcpy(dMachine, machine, sizeof(struct fp_machine));
480
481 /* Clear temporary registers */
482 _mesa_bzero( (void*) machine->Temporaries,
483 MAX_NV_FRAGMENT_PROGRAM_TEMPS * 4 * sizeof(GLfloat));
484
485 /* Add derivatives */
486 if (program->InputsRead & (1 << FRAG_ATTRIB_WPOS)) {
487 GLfloat *wpos = (GLfloat*) machine->Inputs[FRAG_ATTRIB_WPOS];
488 if (xOrY == 'X') {
489 wpos[0] += 1.0F;
490 wpos[1] += 0.0F;
491 wpos[2] += span->dzdx;
492 wpos[3] += span->dwdx;
493 }
494 else {
495 wpos[0] += 0.0F;
496 wpos[1] += 1.0F;
497 wpos[2] += span->dzdy;
498 wpos[3] += span->dwdy;
499 }
500 }
501 if (program->InputsRead & (1 << FRAG_ATTRIB_COL0)) {
502 GLfloat *col0 = (GLfloat*) machine->Inputs[FRAG_ATTRIB_COL0];
503 if (xOrY == 'X') {
504 col0[0] += span->drdx * (1.0F / CHAN_MAXF);
505 col0[1] += span->dgdx * (1.0F / CHAN_MAXF);
506 col0[2] += span->dbdx * (1.0F / CHAN_MAXF);
507 col0[3] += span->dadx * (1.0F / CHAN_MAXF);
508 }
509 else {
510 col0[0] += span->drdy * (1.0F / CHAN_MAXF);
511 col0[1] += span->dgdy * (1.0F / CHAN_MAXF);
512 col0[2] += span->dbdy * (1.0F / CHAN_MAXF);
513 col0[3] += span->dady * (1.0F / CHAN_MAXF);
514 }
515 }
516 if (program->InputsRead & (1 << FRAG_ATTRIB_COL1)) {
517 GLfloat *col1 = (GLfloat*) machine->Inputs[FRAG_ATTRIB_COL1];
518 if (xOrY == 'X') {
519 col1[0] += span->dsrdx * (1.0F / CHAN_MAXF);
520 col1[1] += span->dsgdx * (1.0F / CHAN_MAXF);
521 col1[2] += span->dsbdx * (1.0F / CHAN_MAXF);
522 col1[3] += 0.0; /*XXX fix */
523 }
524 else {
525 col1[0] += span->dsrdy * (1.0F / CHAN_MAXF);
526 col1[1] += span->dsgdy * (1.0F / CHAN_MAXF);
527 col1[2] += span->dsbdy * (1.0F / CHAN_MAXF);
528 col1[3] += 0.0; /*XXX fix */
529 }
530 }
531 if (program->InputsRead & (1 << FRAG_ATTRIB_FOGC)) {
532 GLfloat *fogc = (GLfloat*) machine->Inputs[FRAG_ATTRIB_FOGC];
533 if (xOrY == 'X') {
534 fogc[0] += span->dfogdx;
535 }
536 else {
537 fogc[0] += span->dfogdy;
538 }
539 }
540 for (u = 0; u < ctx->Const.MaxTextureCoordUnits; u++) {
541 if (program->InputsRead & (1 << (FRAG_ATTRIB_TEX0 + u))) {
542 GLfloat *tex = (GLfloat*) machine->Inputs[FRAG_ATTRIB_TEX0 + u];
543 /* XXX perspective-correct interpolation */
544 if (xOrY == 'X') {
545 tex[0] += span->texStepX[u][0];
546 tex[1] += span->texStepX[u][1];
547 tex[2] += span->texStepX[u][2];
548 tex[3] += span->texStepX[u][3];
549 }
550 else {
551 tex[0] += span->texStepY[u][0];
552 tex[1] += span->texStepY[u][1];
553 tex[2] += span->texStepY[u][2];
554 tex[3] += span->texStepY[u][3];
555 }
556 }
557 }
558
559 /* init condition codes */
560 dMachine->CondCodes[0] = COND_EQ;
561 dMachine->CondCodes[1] = COND_EQ;
562 dMachine->CondCodes[2] = COND_EQ;
563 dMachine->CondCodes[3] = COND_EQ;
564 }
565
566
567 /**
568 * Execute the given vertex program.
569 * NOTE: we do everything in single-precision floating point; we don't
570 * currently observe the single/half/fixed-precision qualifiers.
571 * \param ctx - rendering context
572 * \param program - the fragment program to execute
573 * \param machine - machine state (register file)
574 * \param maxInst - max number of instructions to execute
575 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
576 */
577 static GLboolean
578 execute_program( GLcontext *ctx,
579 const struct fragment_program *program, GLuint maxInst,
580 struct fp_machine *machine, const struct sw_span *span,
581 GLuint column )
582 {
583 GLuint pc;
584
585 #if DEBUG_FRAG
586 printf("execute fragment program --------------------\n");
587 #endif
588
589 /* XXX: This should go someplace else, but it is safe here (and slow!)
590 * - karl
591 */
592 _mesa_load_state_parameters(ctx, program->Parameters);
593
594 for (pc = 0; pc < maxInst; pc++) {
595 const struct fp_instruction *inst = program->Instructions + pc;
596
597 if (ctx->FragmentProgram.CallbackEnabled &&
598 ctx->FragmentProgram.Callback) {
599 ctx->FragmentProgram.CurrentPosition = inst->StringPos;
600 ctx->FragmentProgram.Callback(program->Base.Target,
601 ctx->FragmentProgram.CallbackData);
602 }
603
604 switch (inst->Opcode) {
605 case FP_OPCODE_ABS:
606 {
607 GLfloat a[4], result[4];
608 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
609 result[0] = FABSF(a[0]);
610 result[1] = FABSF(a[1]);
611 result[2] = FABSF(a[2]);
612 result[3] = FABSF(a[3]);
613 store_vector4( inst, machine, result );
614 }
615 break;
616 case FP_OPCODE_ADD:
617 {
618 GLfloat a[4], b[4], result[4];
619 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
620 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
621 result[0] = a[0] + b[0];
622 result[1] = a[1] + b[1];
623 result[2] = a[2] + b[2];
624 result[3] = a[3] + b[3];
625 store_vector4( inst, machine, result );
626 }
627 break;
628 case FP_OPCODE_CMP:
629 {
630 GLfloat a[4], b[4], c[4], result[4];
631 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
632 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
633 fetch_vector4( ctx, &inst->SrcReg[2], machine, program, c );
634 result[0] = a[0] < 0.0F ? b[0] : c[0];
635 result[1] = a[1] < 0.0F ? b[1] : c[1];
636 result[2] = a[2] < 0.0F ? b[2] : c[2];
637 result[3] = a[3] < 0.0F ? b[3] : c[3];
638 store_vector4( inst, machine, result );
639 }
640 break;
641 case FP_OPCODE_COS:
642 {
643 GLfloat a[4], result[4];
644 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
645 result[0] = result[1] = result[2] = result[3] = (GLfloat)_mesa_cos(a[0]);
646 store_vector4( inst, machine, result );
647 }
648 break;
649 case FP_OPCODE_DDX: /* Partial derivative with respect to X */
650 {
651 GLfloat a[4], aNext[4], result[4];
652 struct fp_machine dMachine;
653 if (!fetch_vector4_deriv(ctx, &inst->SrcReg[0], span, 'X',
654 column, result)) {
655 /* This is tricky. Make a copy of the current machine state,
656 * increment the input registers by the dx or dy partial
657 * derivatives, then re-execute the program up to the
658 * preceeding instruction, then fetch the source register.
659 * Finally, find the difference in the register values for
660 * the original and derivative runs.
661 */
662 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a);
663 init_machine_deriv(ctx, machine, program, span,
664 'X', &dMachine);
665 execute_program(ctx, program, pc, &dMachine, span, column);
666 fetch_vector4( ctx, &inst->SrcReg[0], &dMachine, program, aNext );
667 result[0] = aNext[0] - a[0];
668 result[1] = aNext[1] - a[1];
669 result[2] = aNext[2] - a[2];
670 result[3] = aNext[3] - a[3];
671 }
672 store_vector4( inst, machine, result );
673 }
674 break;
675 case FP_OPCODE_DDY: /* Partial derivative with respect to Y */
676 {
677 GLfloat a[4], aNext[4], result[4];
678 struct fp_machine dMachine;
679 if (!fetch_vector4_deriv(ctx, &inst->SrcReg[0], span, 'Y',
680 column, result)) {
681 init_machine_deriv(ctx, machine, program, span,
682 'Y', &dMachine);
683 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a);
684 execute_program(ctx, program, pc, &dMachine, span, column);
685 fetch_vector4( ctx, &inst->SrcReg[0], &dMachine, program, aNext );
686 result[0] = aNext[0] - a[0];
687 result[1] = aNext[1] - a[1];
688 result[2] = aNext[2] - a[2];
689 result[3] = aNext[3] - a[3];
690 }
691 store_vector4( inst, machine, result );
692 }
693 break;
694 case FP_OPCODE_DP3:
695 {
696 GLfloat a[4], b[4], result[4];
697 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
698 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
699 result[0] = result[1] = result[2] = result[3] =
700 a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
701 store_vector4( inst, machine, result );
702 #if DEBUG_FRAG
703 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
704 result[0], a[0], a[1], a[2], b[0], b[1], b[2]);
705 #endif
706 }
707 break;
708 case FP_OPCODE_DP4:
709 {
710 GLfloat a[4], b[4], result[4];
711 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
712 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
713 result[0] = result[1] = result[2] = result[3] =
714 a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
715 store_vector4( inst, machine, result );
716 }
717 break;
718 case FP_OPCODE_DPH:
719 {
720 GLfloat a[4], b[4], result[4];
721 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
722 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
723 result[0] = result[1] = result[2] = result[3] =
724 a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + b[3];
725 store_vector4( inst, machine, result );
726 }
727 break;
728 case FP_OPCODE_DST: /* Distance vector */
729 {
730 GLfloat a[4], b[4], result[4];
731 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
732 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
733 result[0] = 1.0F;
734 result[1] = a[1] * b[1];
735 result[2] = a[2];
736 result[3] = b[3];
737 store_vector4( inst, machine, result );
738 }
739 break;
740 case FP_OPCODE_EX2: /* Exponential base 2 */
741 {
742 GLfloat a[4], result[4];
743 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
744 result[0] = result[1] = result[2] = result[3] =
745 (GLfloat) _mesa_pow(2.0, a[0]);
746 store_vector4( inst, machine, result );
747 }
748 break;
749 case FP_OPCODE_FLR:
750 {
751 GLfloat a[4], result[4];
752 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
753 result[0] = FLOORF(a[0]);
754 result[1] = FLOORF(a[1]);
755 result[2] = FLOORF(a[2]);
756 result[3] = FLOORF(a[3]);
757 store_vector4( inst, machine, result );
758 }
759 break;
760 case FP_OPCODE_FRC:
761 {
762 GLfloat a[4], result[4];
763 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
764 result[0] = a[0] - FLOORF(a[0]);
765 result[1] = a[1] - FLOORF(a[1]);
766 result[2] = a[2] - FLOORF(a[2]);
767 result[3] = a[3] - FLOORF(a[3]);
768 store_vector4( inst, machine, result );
769 }
770 break;
771 case FP_OPCODE_KIL:
772 {
773 const GLuint *swizzle = inst->DstReg.CondSwizzle;
774 const GLuint condMask = inst->DstReg.CondMask;
775 if (test_cc(machine->CondCodes[swizzle[0]], condMask) ||
776 test_cc(machine->CondCodes[swizzle[1]], condMask) ||
777 test_cc(machine->CondCodes[swizzle[2]], condMask) ||
778 test_cc(machine->CondCodes[swizzle[3]], condMask)) {
779 return GL_FALSE;
780 }
781 }
782 break;
783 case FP_OPCODE_LG2: /* log base 2 */
784 {
785 GLfloat a[4], result[4];
786 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
787 result[0] = result[1] = result[2] = result[3]
788 = LOG2(a[0]);
789 store_vector4( inst, machine, result );
790 }
791 break;
792 case FP_OPCODE_LIT:
793 {
794 GLfloat a[4], result[4];
795 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
796 if (a[0] < 0.0F)
797 a[0] = 0.0F;
798 if (a[1] < 0.0F)
799 a[1] = 0.0F;
800 result[0] = 1.0F;
801 result[1] = a[0];
802 result[2] = (a[0] > 0.0F) ? (GLfloat) exp(a[3] * log(a[1])) : 0.0F;
803 result[3] = 1.0F;
804 store_vector4( inst, machine, result );
805 }
806 break;
807 case FP_OPCODE_LRP:
808 {
809 GLfloat a[4], b[4], c[4], result[4];
810 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
811 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
812 fetch_vector4( ctx, &inst->SrcReg[2], machine, program, c );
813 result[0] = a[0] * b[0] + (1.0F - a[0]) * c[0];
814 result[1] = a[1] * b[1] + (1.0F - a[1]) * c[1];
815 result[2] = a[2] * b[2] + (1.0F - a[2]) * c[2];
816 result[3] = a[3] * b[3] + (1.0F - a[3]) * c[3];
817 store_vector4( inst, machine, result );
818 }
819 break;
820 case FP_OPCODE_MAD:
821 {
822 GLfloat a[4], b[4], c[4], result[4];
823 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
824 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
825 fetch_vector4( ctx, &inst->SrcReg[2], machine, program, c );
826 result[0] = a[0] * b[0] + c[0];
827 result[1] = a[1] * b[1] + c[1];
828 result[2] = a[2] * b[2] + c[2];
829 result[3] = a[3] * b[3] + c[3];
830 store_vector4( inst, machine, result );
831 }
832 break;
833 case FP_OPCODE_MAX:
834 {
835 GLfloat a[4], b[4], result[4];
836 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
837 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
838 result[0] = MAX2(a[0], b[0]);
839 result[1] = MAX2(a[1], b[1]);
840 result[2] = MAX2(a[2], b[2]);
841 result[3] = MAX2(a[3], b[3]);
842 store_vector4( inst, machine, result );
843 }
844 break;
845 case FP_OPCODE_MIN:
846 {
847 GLfloat a[4], b[4], result[4];
848 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
849 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
850 result[0] = MIN2(a[0], b[0]);
851 result[1] = MIN2(a[1], b[1]);
852 result[2] = MIN2(a[2], b[2]);
853 result[3] = MIN2(a[3], b[3]);
854 store_vector4( inst, machine, result );
855 }
856 break;
857 case FP_OPCODE_MOV:
858 {
859 GLfloat result[4];
860 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, result );
861 store_vector4( inst, machine, result );
862 }
863 break;
864 case FP_OPCODE_MUL:
865 {
866 GLfloat a[4], b[4], result[4];
867 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
868 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
869 result[0] = a[0] * b[0];
870 result[1] = a[1] * b[1];
871 result[2] = a[2] * b[2];
872 result[3] = a[3] * b[3];
873 store_vector4( inst, machine, result );
874 #if DEBUG_FRAG
875 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
876 result[0], result[1], result[2], result[3],
877 a[0], a[1], a[2], a[3],
878 b[0], b[1], b[2], b[3]);
879 #endif
880 }
881 break;
882 case FP_OPCODE_PK2H: /* pack two 16-bit floats */
883 /* XXX this is probably wrong */
884 {
885 GLfloat a[4], result[4];
886 const GLuint *rawBits = (const GLuint *) a;
887 GLuint *rawResult = (GLuint *) result;
888 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
889 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
890 = rawBits[0] | (rawBits[1] << 16);
891 store_vector4( inst, machine, result );
892 }
893 break;
894 case FP_OPCODE_PK2US: /* pack two GLushorts */
895 {
896 GLfloat a[4], result[4];
897 GLuint usx, usy, *rawResult = (GLuint *) result;
898 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
899 a[0] = CLAMP(a[0], 0.0F, 1.0F);
900 a[1] = CLAMP(a[0], 0.0F, 1.0F);
901 usx = IROUND(a[0] * 65535.0F);
902 usy = IROUND(a[1] * 65535.0F);
903 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
904 = usx | (usy << 16);
905 store_vector4( inst, machine, result );
906 }
907 break;
908 case FP_OPCODE_PK4B: /* pack four GLbytes */
909 {
910 GLfloat a[4], result[4];
911 GLuint ubx, uby, ubz, ubw, *rawResult = (GLuint *) result;
912 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
913 a[0] = CLAMP(a[0], -128.0F / 127.0F, 1.0F);
914 a[1] = CLAMP(a[1], -128.0F / 127.0F, 1.0F);
915 a[2] = CLAMP(a[2], -128.0F / 127.0F, 1.0F);
916 a[3] = CLAMP(a[3], -128.0F / 127.0F, 1.0F);
917 ubx = IROUND(127.0F * a[0] + 128.0F);
918 uby = IROUND(127.0F * a[1] + 128.0F);
919 ubz = IROUND(127.0F * a[2] + 128.0F);
920 ubw = IROUND(127.0F * a[3] + 128.0F);
921 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
922 = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
923 store_vector4( inst, machine, result );
924 }
925 break;
926 case FP_OPCODE_PK4UB: /* pack four GLubytes */
927 {
928 GLfloat a[4], result[4];
929 GLuint ubx, uby, ubz, ubw, *rawResult = (GLuint *) result;
930 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
931 a[0] = CLAMP(a[0], 0.0F, 1.0F);
932 a[1] = CLAMP(a[1], 0.0F, 1.0F);
933 a[2] = CLAMP(a[2], 0.0F, 1.0F);
934 a[3] = CLAMP(a[3], 0.0F, 1.0F);
935 ubx = IROUND(255.0F * a[0]);
936 uby = IROUND(255.0F * a[1]);
937 ubz = IROUND(255.0F * a[2]);
938 ubw = IROUND(255.0F * a[3]);
939 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
940 = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
941 store_vector4( inst, machine, result );
942 }
943 break;
944 case FP_OPCODE_POW:
945 {
946 GLfloat a[4], b[4], result[4];
947 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
948 fetch_vector1( ctx, &inst->SrcReg[1], machine, program, b );
949 result[0] = result[1] = result[2] = result[3]
950 = (GLfloat)_mesa_pow(a[0], b[0]);
951 store_vector4( inst, machine, result );
952 }
953 break;
954 case FP_OPCODE_RCP:
955 {
956 GLfloat a[4], result[4];
957 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
958 #if DEBUG_FRAG
959 if (a[0] == 0)
960 printf("RCP(0)\n");
961 else if (IS_INF_OR_NAN(a[0]))
962 printf("RCP(inf)\n");
963 #endif
964 result[0] = result[1] = result[2] = result[3]
965 = 1.0F / a[0];
966 store_vector4( inst, machine, result );
967 }
968 break;
969 case FP_OPCODE_RFL:
970 {
971 GLfloat axis[4], dir[4], result[4], tmp[4];
972 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, axis );
973 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, dir );
974 tmp[3] = axis[0] * axis[0]
975 + axis[1] * axis[1]
976 + axis[2] * axis[2];
977 tmp[0] = (2.0F * (axis[0] * dir[0] +
978 axis[1] * dir[1] +
979 axis[2] * dir[2])) / tmp[3];
980 result[0] = tmp[0] * axis[0] - dir[0];
981 result[1] = tmp[0] * axis[1] - dir[1];
982 result[2] = tmp[0] * axis[2] - dir[2];
983 /* result[3] is never written! XXX enforce in parser! */
984 store_vector4( inst, machine, result );
985 }
986 break;
987 case FP_OPCODE_RSQ: /* 1 / sqrt() */
988 {
989 GLfloat a[4], result[4];
990 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
991 result[0] = result[1] = result[2] = result[3] = INV_SQRTF(a[0]);
992 store_vector4( inst, machine, result );
993 #if DEBUG_FRAG
994 printf("RSQ %g = 1/sqrt(%g)\n", result[0], a[0]);
995 #endif
996 }
997 break;
998 case FP_OPCODE_SCS: /* sine and cos */
999 {
1000 GLfloat a[4], result[4];
1001 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
1002 result[0] = (GLfloat)cos(a[0]);
1003 result[1] = (GLfloat)sin(a[0]);
1004 result[2] = 0.0; /* undefined! */
1005 result[3] = 0.0; /* undefined! */
1006 store_vector4( inst, machine, result );
1007 }
1008 break;
1009 case FP_OPCODE_SEQ: /* set on equal */
1010 {
1011 GLfloat a[4], b[4], result[4];
1012 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
1013 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
1014 result[0] = (a[0] == b[0]) ? 1.0F : 0.0F;
1015 result[1] = (a[1] == b[1]) ? 1.0F : 0.0F;
1016 result[2] = (a[2] == b[2]) ? 1.0F : 0.0F;
1017 result[3] = (a[3] == b[3]) ? 1.0F : 0.0F;
1018 store_vector4( inst, machine, result );
1019 }
1020 break;
1021 case FP_OPCODE_SFL: /* set false, operands ignored */
1022 {
1023 static const GLfloat result[4] = { 0.0F, 0.0F, 0.0F, 0.0F };
1024 store_vector4( inst, machine, result );
1025 }
1026 break;
1027 case FP_OPCODE_SGE: /* set on greater or equal */
1028 {
1029 GLfloat a[4], b[4], result[4];
1030 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
1031 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
1032 result[0] = (a[0] >= b[0]) ? 1.0F : 0.0F;
1033 result[1] = (a[1] >= b[1]) ? 1.0F : 0.0F;
1034 result[2] = (a[2] >= b[2]) ? 1.0F : 0.0F;
1035 result[3] = (a[3] >= b[3]) ? 1.0F : 0.0F;
1036 store_vector4( inst, machine, result );
1037 }
1038 break;
1039 case FP_OPCODE_SGT: /* set on greater */
1040 {
1041 GLfloat a[4], b[4], result[4];
1042 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
1043 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
1044 result[0] = (a[0] > b[0]) ? 1.0F : 0.0F;
1045 result[1] = (a[1] > b[1]) ? 1.0F : 0.0F;
1046 result[2] = (a[2] > b[2]) ? 1.0F : 0.0F;
1047 result[3] = (a[3] > b[3]) ? 1.0F : 0.0F;
1048 store_vector4( inst, machine, result );
1049 }
1050 break;
1051 case FP_OPCODE_SIN:
1052 {
1053 GLfloat a[4], result[4];
1054 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
1055 result[0] = result[1] = result[2] =
1056 result[3] = (GLfloat)_mesa_sin(a[0]);
1057 store_vector4( inst, machine, result );
1058 }
1059 break;
1060 case FP_OPCODE_SLE: /* set on less or equal */
1061 {
1062 GLfloat a[4], b[4], result[4];
1063 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
1064 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
1065 result[0] = (a[0] <= b[0]) ? 1.0F : 0.0F;
1066 result[1] = (a[1] <= b[1]) ? 1.0F : 0.0F;
1067 result[2] = (a[2] <= b[2]) ? 1.0F : 0.0F;
1068 result[3] = (a[3] <= b[3]) ? 1.0F : 0.0F;
1069 store_vector4( inst, machine, result );
1070 }
1071 break;
1072 case FP_OPCODE_SLT: /* set on less */
1073 {
1074 GLfloat a[4], b[4], result[4];
1075 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
1076 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
1077 result[0] = (a[0] < b[0]) ? 1.0F : 0.0F;
1078 result[1] = (a[1] < b[1]) ? 1.0F : 0.0F;
1079 result[2] = (a[2] < b[2]) ? 1.0F : 0.0F;
1080 result[3] = (a[3] < b[3]) ? 1.0F : 0.0F;
1081 store_vector4( inst, machine, result );
1082 }
1083 break;
1084 case FP_OPCODE_SNE: /* set on not equal */
1085 {
1086 GLfloat a[4], b[4], result[4];
1087 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
1088 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
1089 result[0] = (a[0] != b[0]) ? 1.0F : 0.0F;
1090 result[1] = (a[1] != b[1]) ? 1.0F : 0.0F;
1091 result[2] = (a[2] != b[2]) ? 1.0F : 0.0F;
1092 result[3] = (a[3] != b[3]) ? 1.0F : 0.0F;
1093 store_vector4( inst, machine, result );
1094 }
1095 break;
1096 case FP_OPCODE_STR: /* set true, operands ignored */
1097 {
1098 static const GLfloat result[4] = { 1.0F, 1.0F, 1.0F, 1.0F };
1099 store_vector4( inst, machine, result );
1100 }
1101 break;
1102 case FP_OPCODE_SUB:
1103 {
1104 GLfloat a[4], b[4], result[4];
1105 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
1106 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
1107 result[0] = a[0] - b[0];
1108 result[1] = a[1] - b[1];
1109 result[2] = a[2] - b[2];
1110 result[3] = a[3] - b[3];
1111 store_vector4( inst, machine, result );
1112 }
1113 break;
1114 case FP_OPCODE_SWZ:
1115 {
1116 const struct fp_src_register *source = &inst->SrcReg[0];
1117 const GLfloat *src = get_register_pointer(ctx, source,
1118 machine, program);
1119 GLfloat result[4];
1120 GLuint i;
1121
1122 /* do extended swizzling here */
1123 for (i = 0; i < 3; i++) {
1124 if (source->Swizzle[i] == SWIZZLE_ZERO)
1125 result[i] = 0.0;
1126 else if (source->Swizzle[i] == SWIZZLE_ONE)
1127 result[i] = -1.0;
1128 else
1129 result[i] = -src[source->Swizzle[i]];
1130 if (source->NegateBase)
1131 result[i] = -result[i];
1132 }
1133 store_vector4( inst, machine, result );
1134 }
1135 break;
1136 case FP_OPCODE_TEX:
1137 /* Texel lookup */
1138 {
1139 GLfloat texcoord[4], color[4];
1140 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, texcoord );
1141 /* XXX: Undo perspective divide from interpolate_texcoords() */
1142 fetch_texel( ctx, texcoord,
1143 span->array->lambda[inst->TexSrcUnit][column],
1144 inst->TexSrcUnit, color );
1145 store_vector4( inst, machine, color );
1146 }
1147 break;
1148 case FP_OPCODE_TXB:
1149 /* Texel lookup with LOD bias */
1150 {
1151 GLfloat texcoord[4], color[4], bias, lambda;
1152
1153 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, texcoord );
1154 /* texcoord[3] is the bias to add to lambda */
1155 bias = ctx->Texture.Unit[inst->TexSrcUnit].LodBias
1156 + ctx->Texture.Unit[inst->TexSrcUnit]._Current->LodBias
1157 + texcoord[3];
1158 lambda = span->array->lambda[inst->TexSrcUnit][column] + bias;
1159 fetch_texel( ctx, texcoord, lambda,
1160 inst->TexSrcUnit, color );
1161 store_vector4( inst, machine, color );
1162 }
1163 break;
1164 case FP_OPCODE_TXD:
1165 /* Texture lookup w/ partial derivatives for LOD */
1166 {
1167 GLfloat texcoord[4], dtdx[4], dtdy[4], color[4];
1168 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, texcoord );
1169 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, dtdx );
1170 fetch_vector4( ctx, &inst->SrcReg[2], machine, program, dtdy );
1171 fetch_texel_deriv( ctx, texcoord, dtdx, dtdy, inst->TexSrcUnit,
1172 color );
1173 store_vector4( inst, machine, color );
1174 }
1175 break;
1176 case FP_OPCODE_TXP:
1177 /* Texture lookup w/ perspective divide */
1178 {
1179 GLfloat texcoord[4], color[4];
1180 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, texcoord );
1181 /* Already did perspective divide in interpolate_texcoords() */
1182 fetch_texel( ctx, texcoord,
1183 span->array->lambda[inst->TexSrcUnit][column],
1184 inst->TexSrcUnit, color );
1185 store_vector4( inst, machine, color );
1186 }
1187 break;
1188 case FP_OPCODE_UP2H: /* unpack two 16-bit floats */
1189 /* XXX this is probably wrong */
1190 {
1191 GLfloat a[4], result[4];
1192 const GLuint *rawBits = (const GLuint *) a;
1193 GLuint *rawResult = (GLuint *) result;
1194 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
1195 rawResult[0] = rawBits[0] & 0xffff;
1196 rawResult[1] = (rawBits[0] >> 16) & 0xffff;
1197 rawResult[2] = rawBits[0] & 0xffff;
1198 rawResult[3] = (rawBits[0] >> 16) & 0xffff;
1199 store_vector4( inst, machine, result );
1200 }
1201 break;
1202 case FP_OPCODE_UP2US: /* unpack two GLushorts */
1203 {
1204 GLfloat a[4], result[4];
1205 const GLuint *rawBits = (const GLuint *) a;
1206 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
1207 result[0] = (GLfloat) ((rawBits[0] >> 0) & 0xffff) / 65535.0F;
1208 result[1] = (GLfloat) ((rawBits[0] >> 16) & 0xffff) / 65535.0F;
1209 result[2] = result[0];
1210 result[3] = result[1];
1211 store_vector4( inst, machine, result );
1212 }
1213 break;
1214 case FP_OPCODE_UP4B: /* unpack four GLbytes */
1215 {
1216 GLfloat a[4], result[4];
1217 const GLuint *rawBits = (const GLuint *) a;
1218 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
1219 result[0] = (((rawBits[0] >> 0) & 0xff) - 128) / 127.0F;
1220 result[0] = (((rawBits[0] >> 8) & 0xff) - 128) / 127.0F;
1221 result[0] = (((rawBits[0] >> 16) & 0xff) - 128) / 127.0F;
1222 result[0] = (((rawBits[0] >> 24) & 0xff) - 128) / 127.0F;
1223 store_vector4( inst, machine, result );
1224 }
1225 break;
1226 case FP_OPCODE_UP4UB: /* unpack four GLubytes */
1227 {
1228 GLfloat a[4], result[4];
1229 const GLuint *rawBits = (const GLuint *) a;
1230 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
1231 result[0] = ((rawBits[0] >> 0) & 0xff) / 255.0F;
1232 result[0] = ((rawBits[0] >> 8) & 0xff) / 255.0F;
1233 result[0] = ((rawBits[0] >> 16) & 0xff) / 255.0F;
1234 result[0] = ((rawBits[0] >> 24) & 0xff) / 255.0F;
1235 store_vector4( inst, machine, result );
1236 }
1237 break;
1238 case FP_OPCODE_X2D: /* 2-D matrix transform */
1239 {
1240 GLfloat a[4], b[4], c[4], result[4];
1241 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
1242 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
1243 fetch_vector4( ctx, &inst->SrcReg[2], machine, program, c );
1244 result[0] = a[0] + b[0] * c[0] + b[1] * c[1];
1245 result[1] = a[1] + b[0] * c[2] + b[1] * c[3];
1246 result[2] = a[2] + b[0] * c[0] + b[1] * c[1];
1247 result[3] = a[3] + b[0] * c[2] + b[1] * c[3];
1248 store_vector4( inst, machine, result );
1249 }
1250 break;
1251 case FP_OPCODE_END:
1252 return GL_TRUE;
1253 default:
1254 _mesa_problem(ctx, "Bad opcode %d in _mesa_exec_fragment_program",
1255 inst->Opcode);
1256 return GL_TRUE; /* return value doesn't matter */
1257 }
1258 }
1259 return GL_TRUE;
1260 }
1261
1262
1263 static void
1264 init_machine( GLcontext *ctx, struct fp_machine *machine,
1265 const struct fragment_program *program,
1266 const struct sw_span *span, GLuint col )
1267 {
1268 GLuint inputsRead = program->InputsRead;
1269 GLuint u;
1270
1271 if (ctx->FragmentProgram.CallbackEnabled)
1272 inputsRead = ~0;
1273
1274 /* Clear temporary registers */
1275 _mesa_bzero(machine->Temporaries,
1276 MAX_NV_FRAGMENT_PROGRAM_TEMPS * 4 * sizeof(GLfloat));
1277
1278 /* Load input registers */
1279 if (inputsRead & (1 << FRAG_ATTRIB_WPOS)) {
1280 GLfloat *wpos = machine->Inputs[FRAG_ATTRIB_WPOS];
1281 wpos[0] = (GLfloat) span->x + col;
1282 wpos[1] = (GLfloat) span->y;
1283 wpos[2] = (GLfloat) span->array->z[col] / ctx->DepthMaxF;
1284 wpos[3] = span->w + col * span->dwdx;
1285 }
1286 if (inputsRead & (1 << FRAG_ATTRIB_COL0)) {
1287 GLfloat *col0 = machine->Inputs[FRAG_ATTRIB_COL0];
1288 col0[0] = CHAN_TO_FLOAT(span->array->rgba[col][RCOMP]);
1289 col0[1] = CHAN_TO_FLOAT(span->array->rgba[col][GCOMP]);
1290 col0[2] = CHAN_TO_FLOAT(span->array->rgba[col][BCOMP]);
1291 col0[3] = CHAN_TO_FLOAT(span->array->rgba[col][ACOMP]);
1292 }
1293 if (inputsRead & (1 << FRAG_ATTRIB_COL1)) {
1294 GLfloat *col1 = machine->Inputs[FRAG_ATTRIB_COL1];
1295 col1[0] = CHAN_TO_FLOAT(span->array->spec[col][RCOMP]);
1296 col1[1] = CHAN_TO_FLOAT(span->array->spec[col][GCOMP]);
1297 col1[2] = CHAN_TO_FLOAT(span->array->spec[col][BCOMP]);
1298 col1[3] = CHAN_TO_FLOAT(span->array->spec[col][ACOMP]);
1299 }
1300 if (inputsRead & (1 << FRAG_ATTRIB_FOGC)) {
1301 GLfloat *fogc = machine->Inputs[FRAG_ATTRIB_FOGC];
1302 fogc[0] = span->array->fog[col];
1303 fogc[1] = 0.0F;
1304 fogc[2] = 0.0F;
1305 fogc[3] = 0.0F;
1306 }
1307 for (u = 0; u < ctx->Const.MaxTextureCoordUnits; u++) {
1308 if (inputsRead & (1 << (FRAG_ATTRIB_TEX0 + u))) {
1309 GLfloat *tex = machine->Inputs[FRAG_ATTRIB_TEX0 + u];
1310 /*ASSERT(ctx->Texture._EnabledCoordUnits & (1 << u));*/
1311 COPY_4V(tex, span->array->texcoords[u][col]);
1312 /*ASSERT(tex[0] != 0 || tex[1] != 0 || tex[2] != 0);*/
1313 }
1314 }
1315
1316 /* init condition codes */
1317 machine->CondCodes[0] = COND_EQ;
1318 machine->CondCodes[1] = COND_EQ;
1319 machine->CondCodes[2] = COND_EQ;
1320 machine->CondCodes[3] = COND_EQ;
1321 }
1322
1323
1324 void
1325 _swrast_exec_nv_fragment_program( GLcontext *ctx, struct sw_span *span )
1326 {
1327 const struct fragment_program *program = ctx->FragmentProgram.Current;
1328 GLuint i;
1329
1330 ctx->_CurrentProgram = GL_FRAGMENT_PROGRAM_ARB; /* or NV, doesn't matter */
1331
1332 for (i = 0; i < span->end; i++) {
1333 if (span->array->mask[i]) {
1334 init_machine(ctx, &ctx->FragmentProgram.Machine,
1335 ctx->FragmentProgram.Current, span, i);
1336
1337 if (!execute_program(ctx, program, ~0,
1338 &ctx->FragmentProgram.Machine, span, i)) {
1339 span->array->mask[i] = GL_FALSE; /* killed fragment */
1340 }
1341
1342 /* Store output registers */
1343 {
1344 const GLfloat *colOut
1345 = ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_COLR];
1346 UNCLAMPED_FLOAT_TO_CHAN(span->array->rgba[i][RCOMP], colOut[0]);
1347 UNCLAMPED_FLOAT_TO_CHAN(span->array->rgba[i][GCOMP], colOut[1]);
1348 UNCLAMPED_FLOAT_TO_CHAN(span->array->rgba[i][BCOMP], colOut[2]);
1349 UNCLAMPED_FLOAT_TO_CHAN(span->array->rgba[i][ACOMP], colOut[3]);
1350 }
1351 /* depth value */
1352 if (program->OutputsWritten & (1 << FRAG_OUTPUT_DEPR))
1353 span->array->z[i] = IROUND(ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_DEPR][0] * ctx->DepthMaxF);
1354 }
1355 }
1356
1357 ctx->_CurrentProgram = 0;
1358 }
1359