Debian Codegraph @

nx-X11/extras/Mesa/src/mesa/swrast/s_nvfragprog.c

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