switch to using driFillInModes fix depthbuffer = 0
[mesa.git] / src / mesa / swrast / s_nvfragprog.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.1
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[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_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 if (program->Base.Target == GL_FRAGMENT_PROGRAM_NV) {
482 /* Clear temporary registers (undefined for ARB_f_p) */
483 _mesa_bzero( (void*) machine->Temporaries,
484 MAX_NV_FRAGMENT_PROGRAM_TEMPS * 4 * sizeof(GLfloat));
485 }
486
487 /* Add derivatives */
488 if (program->InputsRead & (1 << FRAG_ATTRIB_WPOS)) {
489 GLfloat *wpos = (GLfloat*) machine->Inputs[FRAG_ATTRIB_WPOS];
490 if (xOrY == 'X') {
491 wpos[0] += 1.0F;
492 wpos[1] += 0.0F;
493 wpos[2] += span->dzdx;
494 wpos[3] += span->dwdx;
495 }
496 else {
497 wpos[0] += 0.0F;
498 wpos[1] += 1.0F;
499 wpos[2] += span->dzdy;
500 wpos[3] += span->dwdy;
501 }
502 }
503 if (program->InputsRead & (1 << FRAG_ATTRIB_COL0)) {
504 GLfloat *col0 = (GLfloat*) machine->Inputs[FRAG_ATTRIB_COL0];
505 if (xOrY == 'X') {
506 col0[0] += span->drdx * (1.0F / CHAN_MAXF);
507 col0[1] += span->dgdx * (1.0F / CHAN_MAXF);
508 col0[2] += span->dbdx * (1.0F / CHAN_MAXF);
509 col0[3] += span->dadx * (1.0F / CHAN_MAXF);
510 }
511 else {
512 col0[0] += span->drdy * (1.0F / CHAN_MAXF);
513 col0[1] += span->dgdy * (1.0F / CHAN_MAXF);
514 col0[2] += span->dbdy * (1.0F / CHAN_MAXF);
515 col0[3] += span->dady * (1.0F / CHAN_MAXF);
516 }
517 }
518 if (program->InputsRead & (1 << FRAG_ATTRIB_COL1)) {
519 GLfloat *col1 = (GLfloat*) machine->Inputs[FRAG_ATTRIB_COL1];
520 if (xOrY == 'X') {
521 col1[0] += span->dsrdx * (1.0F / CHAN_MAXF);
522 col1[1] += span->dsgdx * (1.0F / CHAN_MAXF);
523 col1[2] += span->dsbdx * (1.0F / CHAN_MAXF);
524 col1[3] += 0.0; /*XXX fix */
525 }
526 else {
527 col1[0] += span->dsrdy * (1.0F / CHAN_MAXF);
528 col1[1] += span->dsgdy * (1.0F / CHAN_MAXF);
529 col1[2] += span->dsbdy * (1.0F / CHAN_MAXF);
530 col1[3] += 0.0; /*XXX fix */
531 }
532 }
533 if (program->InputsRead & (1 << FRAG_ATTRIB_FOGC)) {
534 GLfloat *fogc = (GLfloat*) machine->Inputs[FRAG_ATTRIB_FOGC];
535 if (xOrY == 'X') {
536 fogc[0] += span->dfogdx;
537 }
538 else {
539 fogc[0] += span->dfogdy;
540 }
541 }
542 for (u = 0; u < ctx->Const.MaxTextureCoordUnits; u++) {
543 if (program->InputsRead & (1 << (FRAG_ATTRIB_TEX0 + u))) {
544 GLfloat *tex = (GLfloat*) machine->Inputs[FRAG_ATTRIB_TEX0 + u];
545 /* XXX perspective-correct interpolation */
546 if (xOrY == 'X') {
547 tex[0] += span->texStepX[u][0];
548 tex[1] += span->texStepX[u][1];
549 tex[2] += span->texStepX[u][2];
550 tex[3] += span->texStepX[u][3];
551 }
552 else {
553 tex[0] += span->texStepY[u][0];
554 tex[1] += span->texStepY[u][1];
555 tex[2] += span->texStepY[u][2];
556 tex[3] += span->texStepY[u][3];
557 }
558 }
559 }
560
561 /* init condition codes */
562 dMachine->CondCodes[0] = COND_EQ;
563 dMachine->CondCodes[1] = COND_EQ;
564 dMachine->CondCodes[2] = COND_EQ;
565 dMachine->CondCodes[3] = COND_EQ;
566 }
567
568
569 /**
570 * Execute the given vertex program.
571 * NOTE: we do everything in single-precision floating point; we don't
572 * currently observe the single/half/fixed-precision qualifiers.
573 * \param ctx - rendering context
574 * \param program - the fragment program to execute
575 * \param machine - machine state (register file)
576 * \param maxInst - max number of instructions to execute
577 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
578 */
579 static GLboolean
580 execute_program( GLcontext *ctx,
581 const struct fragment_program *program, GLuint maxInst,
582 struct fp_machine *machine, const struct sw_span *span,
583 GLuint column )
584 {
585 GLuint pc;
586
587 #if DEBUG_FRAG
588 printf("execute fragment program --------------------\n");
589 #endif
590
591 for (pc = 0; pc < maxInst; pc++) {
592 const struct fp_instruction *inst = program->Instructions + pc;
593
594 if (ctx->FragmentProgram.CallbackEnabled &&
595 ctx->FragmentProgram.Callback) {
596 ctx->FragmentProgram.CurrentPosition = inst->StringPos;
597 ctx->FragmentProgram.Callback(program->Base.Target,
598 ctx->FragmentProgram.CallbackData);
599 }
600
601 switch (inst->Opcode) {
602 case FP_OPCODE_ABS:
603 {
604 GLfloat a[4], result[4];
605 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
606 result[0] = FABSF(a[0]);
607 result[1] = FABSF(a[1]);
608 result[2] = FABSF(a[2]);
609 result[3] = FABSF(a[3]);
610 store_vector4( inst, machine, result );
611 }
612 break;
613 case FP_OPCODE_ADD:
614 {
615 GLfloat a[4], b[4], result[4];
616 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
617 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
618 result[0] = a[0] + b[0];
619 result[1] = a[1] + b[1];
620 result[2] = a[2] + b[2];
621 result[3] = a[3] + b[3];
622 store_vector4( inst, machine, result );
623 }
624 break;
625 case FP_OPCODE_CMP:
626 {
627 GLfloat a[4], b[4], c[4], result[4];
628 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
629 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
630 fetch_vector4( ctx, &inst->SrcReg[2], machine, program, c );
631 result[0] = a[0] < 0.0F ? b[0] : c[0];
632 result[1] = a[1] < 0.0F ? b[1] : c[1];
633 result[2] = a[2] < 0.0F ? b[2] : c[2];
634 result[3] = a[3] < 0.0F ? b[3] : c[3];
635 store_vector4( inst, machine, result );
636 }
637 break;
638 case FP_OPCODE_COS:
639 {
640 GLfloat a[4], result[4];
641 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
642 result[0] = result[1] = result[2] = result[3] = (GLfloat)_mesa_cos(a[0]);
643 store_vector4( inst, machine, result );
644 }
645 break;
646 case FP_OPCODE_DDX: /* Partial derivative with respect to X */
647 {
648 GLfloat a[4], aNext[4], result[4];
649 struct fp_machine dMachine;
650 if (!fetch_vector4_deriv(ctx, &inst->SrcReg[0], span, 'X',
651 column, result)) {
652 /* This is tricky. Make a copy of the current machine state,
653 * increment the input registers by the dx or dy partial
654 * derivatives, then re-execute the program up to the
655 * preceeding instruction, then fetch the source register.
656 * Finally, find the difference in the register values for
657 * the original and derivative runs.
658 */
659 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a);
660 init_machine_deriv(ctx, machine, program, span,
661 'X', &dMachine);
662 execute_program(ctx, program, pc, &dMachine, span, column);
663 fetch_vector4( ctx, &inst->SrcReg[0], &dMachine, program, aNext );
664 result[0] = aNext[0] - a[0];
665 result[1] = aNext[1] - a[1];
666 result[2] = aNext[2] - a[2];
667 result[3] = aNext[3] - a[3];
668 }
669 store_vector4( inst, machine, result );
670 }
671 break;
672 case FP_OPCODE_DDY: /* Partial derivative with respect to Y */
673 {
674 GLfloat a[4], aNext[4], result[4];
675 struct fp_machine dMachine;
676 if (!fetch_vector4_deriv(ctx, &inst->SrcReg[0], span, 'Y',
677 column, result)) {
678 init_machine_deriv(ctx, machine, program, span,
679 'Y', &dMachine);
680 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a);
681 execute_program(ctx, program, pc, &dMachine, span, column);
682 fetch_vector4( ctx, &inst->SrcReg[0], &dMachine, program, aNext );
683 result[0] = aNext[0] - a[0];
684 result[1] = aNext[1] - a[1];
685 result[2] = aNext[2] - a[2];
686 result[3] = aNext[3] - a[3];
687 }
688 store_vector4( inst, machine, result );
689 }
690 break;
691 case FP_OPCODE_DP3:
692 {
693 GLfloat a[4], b[4], result[4];
694 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
695 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
696 result[0] = result[1] = result[2] = result[3] =
697 a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
698 store_vector4( inst, machine, result );
699 #if DEBUG_FRAG
700 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
701 result[0], a[0], a[1], a[2], b[0], b[1], b[2]);
702 #endif
703 }
704 break;
705 case FP_OPCODE_DP4:
706 {
707 GLfloat a[4], b[4], result[4];
708 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
709 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
710 result[0] = result[1] = result[2] = result[3] =
711 a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
712 store_vector4( inst, machine, result );
713 }
714 break;
715 case FP_OPCODE_DPH:
716 {
717 GLfloat a[4], b[4], result[4];
718 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
719 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
720 result[0] = result[1] = result[2] = result[3] =
721 a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + b[3];
722 store_vector4( inst, machine, result );
723 }
724 break;
725 case FP_OPCODE_DST: /* Distance vector */
726 {
727 GLfloat a[4], b[4], result[4];
728 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
729 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
730 result[0] = 1.0F;
731 result[1] = a[1] * b[1];
732 result[2] = a[2];
733 result[3] = b[3];
734 store_vector4( inst, machine, result );
735 }
736 break;
737 case FP_OPCODE_EX2: /* Exponential base 2 */
738 {
739 GLfloat a[4], result[4];
740 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
741 result[0] = result[1] = result[2] = result[3] =
742 (GLfloat) _mesa_pow(2.0, a[0]);
743 store_vector4( inst, machine, result );
744 }
745 break;
746 case FP_OPCODE_FLR:
747 {
748 GLfloat a[4], result[4];
749 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
750 result[0] = FLOORF(a[0]);
751 result[1] = FLOORF(a[1]);
752 result[2] = FLOORF(a[2]);
753 result[3] = FLOORF(a[3]);
754 store_vector4( inst, machine, result );
755 }
756 break;
757 case FP_OPCODE_FRC:
758 {
759 GLfloat a[4], result[4];
760 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
761 result[0] = a[0] - FLOORF(a[0]);
762 result[1] = a[1] - FLOORF(a[1]);
763 result[2] = a[2] - FLOORF(a[2]);
764 result[3] = a[3] - FLOORF(a[3]);
765 store_vector4( inst, machine, result );
766 }
767 break;
768 case FP_OPCODE_KIL_NV: /* NV_f_p only */
769 {
770 const GLuint *swizzle = inst->DstReg.CondSwizzle;
771 const GLuint condMask = inst->DstReg.CondMask;
772 if (test_cc(machine->CondCodes[swizzle[0]], condMask) ||
773 test_cc(machine->CondCodes[swizzle[1]], condMask) ||
774 test_cc(machine->CondCodes[swizzle[2]], condMask) ||
775 test_cc(machine->CondCodes[swizzle[3]], condMask)) {
776 return GL_FALSE;
777 }
778 }
779 break;
780 case FP_OPCODE_KIL: /* ARB_f_p only */
781 {
782 GLfloat a[4];
783 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
784 if (a[0] < 0.0F || a[1] < 0.0F || a[2] < 0.0F || a[3] < 0.0F) {
785 return GL_FALSE;
786 }
787 }
788 break;
789 case FP_OPCODE_LG2: /* log base 2 */
790 {
791 GLfloat a[4], result[4];
792 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
793 result[0] = result[1] = result[2] = result[3]
794 = LOG2(a[0]);
795 store_vector4( inst, machine, result );
796 }
797 break;
798 case FP_OPCODE_LIT:
799 {
800 GLfloat a[4], result[4];
801 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
802 if (a[0] < 0.0F)
803 a[0] = 0.0F;
804 if (a[1] < 0.0F)
805 a[1] = 0.0F;
806 result[0] = 1.0F;
807 result[1] = a[0];
808 result[2] = (a[0] > 0.0F) ? (GLfloat) exp(a[3] * log(a[1])) : 0.0F;
809 result[3] = 1.0F;
810 store_vector4( inst, machine, result );
811 }
812 break;
813 case FP_OPCODE_LRP:
814 {
815 GLfloat a[4], b[4], c[4], result[4];
816 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
817 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
818 fetch_vector4( ctx, &inst->SrcReg[2], machine, program, c );
819 result[0] = a[0] * b[0] + (1.0F - a[0]) * c[0];
820 result[1] = a[1] * b[1] + (1.0F - a[1]) * c[1];
821 result[2] = a[2] * b[2] + (1.0F - a[2]) * c[2];
822 result[3] = a[3] * b[3] + (1.0F - a[3]) * c[3];
823 store_vector4( inst, machine, result );
824 }
825 break;
826 case FP_OPCODE_MAD:
827 {
828 GLfloat a[4], b[4], c[4], result[4];
829 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
830 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
831 fetch_vector4( ctx, &inst->SrcReg[2], machine, program, c );
832 result[0] = a[0] * b[0] + c[0];
833 result[1] = a[1] * b[1] + c[1];
834 result[2] = a[2] * b[2] + c[2];
835 result[3] = a[3] * b[3] + c[3];
836 store_vector4( inst, machine, result );
837 }
838 break;
839 case FP_OPCODE_MAX:
840 {
841 GLfloat a[4], b[4], result[4];
842 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
843 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
844 result[0] = MAX2(a[0], b[0]);
845 result[1] = MAX2(a[1], b[1]);
846 result[2] = MAX2(a[2], b[2]);
847 result[3] = MAX2(a[3], b[3]);
848 store_vector4( inst, machine, result );
849 }
850 break;
851 case FP_OPCODE_MIN:
852 {
853 GLfloat a[4], b[4], result[4];
854 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
855 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
856 result[0] = MIN2(a[0], b[0]);
857 result[1] = MIN2(a[1], b[1]);
858 result[2] = MIN2(a[2], b[2]);
859 result[3] = MIN2(a[3], b[3]);
860 store_vector4( inst, machine, result );
861 }
862 break;
863 case FP_OPCODE_MOV:
864 {
865 GLfloat result[4];
866 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, result );
867 store_vector4( inst, machine, result );
868 }
869 break;
870 case FP_OPCODE_MUL:
871 {
872 GLfloat a[4], b[4], result[4];
873 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
874 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
875 result[0] = a[0] * b[0];
876 result[1] = a[1] * b[1];
877 result[2] = a[2] * b[2];
878 result[3] = a[3] * b[3];
879 store_vector4( inst, machine, result );
880 #if DEBUG_FRAG
881 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
882 result[0], result[1], result[2], result[3],
883 a[0], a[1], a[2], a[3],
884 b[0], b[1], b[2], b[3]);
885 #endif
886 }
887 break;
888 case FP_OPCODE_PK2H: /* pack two 16-bit floats in one 32-bit float */
889 {
890 GLfloat a[4], result[4];
891 GLhalfNV hx, hy;
892 GLuint *rawResult = (GLuint *) result;
893 GLuint twoHalves;
894 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
895 hx = _mesa_float_to_half(a[0]);
896 hy = _mesa_float_to_half(a[1]);
897 twoHalves = hx | (hy << 16);
898 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
899 = twoHalves;
900 store_vector4( inst, machine, result );
901 }
902 break;
903 case FP_OPCODE_PK2US: /* pack two GLushorts into one 32-bit float */
904 {
905 GLfloat a[4], result[4];
906 GLuint usx, usy, *rawResult = (GLuint *) result;
907 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
908 a[0] = CLAMP(a[0], 0.0F, 1.0F);
909 a[1] = CLAMP(a[1], 0.0F, 1.0F);
910 usx = IROUND(a[0] * 65535.0F);
911 usy = IROUND(a[1] * 65535.0F);
912 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
913 = usx | (usy << 16);
914 store_vector4( inst, machine, result );
915 }
916 break;
917 case FP_OPCODE_PK4B: /* pack four GLbytes into one 32-bit float */
918 {
919 GLfloat a[4], result[4];
920 GLuint ubx, uby, ubz, ubw, *rawResult = (GLuint *) result;
921 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
922 a[0] = CLAMP(a[0], -128.0F / 127.0F, 1.0F);
923 a[1] = CLAMP(a[1], -128.0F / 127.0F, 1.0F);
924 a[2] = CLAMP(a[2], -128.0F / 127.0F, 1.0F);
925 a[3] = CLAMP(a[3], -128.0F / 127.0F, 1.0F);
926 ubx = IROUND(127.0F * a[0] + 128.0F);
927 uby = IROUND(127.0F * a[1] + 128.0F);
928 ubz = IROUND(127.0F * a[2] + 128.0F);
929 ubw = IROUND(127.0F * a[3] + 128.0F);
930 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
931 = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
932 store_vector4( inst, machine, result );
933 }
934 break;
935 case FP_OPCODE_PK4UB: /* pack four GLubytes into one 32-bit float */
936 {
937 GLfloat a[4], result[4];
938 GLuint ubx, uby, ubz, ubw, *rawResult = (GLuint *) result;
939 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
940 a[0] = CLAMP(a[0], 0.0F, 1.0F);
941 a[1] = CLAMP(a[1], 0.0F, 1.0F);
942 a[2] = CLAMP(a[2], 0.0F, 1.0F);
943 a[3] = CLAMP(a[3], 0.0F, 1.0F);
944 ubx = IROUND(255.0F * a[0]);
945 uby = IROUND(255.0F * a[1]);
946 ubz = IROUND(255.0F * a[2]);
947 ubw = IROUND(255.0F * a[3]);
948 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
949 = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
950 store_vector4( inst, machine, result );
951 }
952 break;
953 case FP_OPCODE_POW:
954 {
955 GLfloat a[4], b[4], result[4];
956 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
957 fetch_vector1( ctx, &inst->SrcReg[1], machine, program, b );
958 result[0] = result[1] = result[2] = result[3]
959 = (GLfloat)_mesa_pow(a[0], b[0]);
960 store_vector4( inst, machine, result );
961 }
962 break;
963 case FP_OPCODE_RCP:
964 {
965 GLfloat a[4], result[4];
966 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
967 #if DEBUG_FRAG
968 if (a[0] == 0)
969 printf("RCP(0)\n");
970 else if (IS_INF_OR_NAN(a[0]))
971 printf("RCP(inf)\n");
972 #endif
973 result[0] = result[1] = result[2] = result[3]
974 = 1.0F / a[0];
975 store_vector4( inst, machine, result );
976 }
977 break;
978 case FP_OPCODE_RFL:
979 {
980 GLfloat axis[4], dir[4], result[4], tmp[4];
981 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, axis );
982 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, dir );
983 tmp[3] = axis[0] * axis[0]
984 + axis[1] * axis[1]
985 + axis[2] * axis[2];
986 tmp[0] = (2.0F * (axis[0] * dir[0] +
987 axis[1] * dir[1] +
988 axis[2] * dir[2])) / tmp[3];
989 result[0] = tmp[0] * axis[0] - dir[0];
990 result[1] = tmp[0] * axis[1] - dir[1];
991 result[2] = tmp[0] * axis[2] - dir[2];
992 /* result[3] is never written! XXX enforce in parser! */
993 store_vector4( inst, machine, result );
994 }
995 break;
996 case FP_OPCODE_RSQ: /* 1 / sqrt() */
997 {
998 GLfloat a[4], result[4];
999 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
1000 result[0] = result[1] = result[2] = result[3] = INV_SQRTF(a[0]);
1001 store_vector4( inst, machine, result );
1002 #if DEBUG_FRAG
1003 printf("RSQ %g = 1/sqrt(%g)\n", result[0], a[0]);
1004 #endif
1005 }
1006 break;
1007 case FP_OPCODE_SCS: /* sine and cos */
1008 {
1009 GLfloat a[4], result[4];
1010 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
1011 result[0] = (GLfloat)cos(a[0]);
1012 result[1] = (GLfloat)sin(a[0]);
1013 result[2] = 0.0; /* undefined! */
1014 result[3] = 0.0; /* undefined! */
1015 store_vector4( inst, machine, result );
1016 }
1017 break;
1018 case FP_OPCODE_SEQ: /* set on equal */
1019 {
1020 GLfloat a[4], b[4], result[4];
1021 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
1022 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
1023 result[0] = (a[0] == b[0]) ? 1.0F : 0.0F;
1024 result[1] = (a[1] == b[1]) ? 1.0F : 0.0F;
1025 result[2] = (a[2] == b[2]) ? 1.0F : 0.0F;
1026 result[3] = (a[3] == b[3]) ? 1.0F : 0.0F;
1027 store_vector4( inst, machine, result );
1028 }
1029 break;
1030 case FP_OPCODE_SFL: /* set false, operands ignored */
1031 {
1032 static const GLfloat result[4] = { 0.0F, 0.0F, 0.0F, 0.0F };
1033 store_vector4( inst, machine, result );
1034 }
1035 break;
1036 case FP_OPCODE_SGE: /* set on greater or equal */
1037 {
1038 GLfloat a[4], b[4], result[4];
1039 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
1040 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
1041 result[0] = (a[0] >= b[0]) ? 1.0F : 0.0F;
1042 result[1] = (a[1] >= b[1]) ? 1.0F : 0.0F;
1043 result[2] = (a[2] >= b[2]) ? 1.0F : 0.0F;
1044 result[3] = (a[3] >= b[3]) ? 1.0F : 0.0F;
1045 store_vector4( inst, machine, result );
1046 }
1047 break;
1048 case FP_OPCODE_SGT: /* set on greater */
1049 {
1050 GLfloat a[4], b[4], result[4];
1051 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
1052 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
1053 result[0] = (a[0] > b[0]) ? 1.0F : 0.0F;
1054 result[1] = (a[1] > b[1]) ? 1.0F : 0.0F;
1055 result[2] = (a[2] > b[2]) ? 1.0F : 0.0F;
1056 result[3] = (a[3] > b[3]) ? 1.0F : 0.0F;
1057 store_vector4( inst, machine, result );
1058 }
1059 break;
1060 case FP_OPCODE_SIN:
1061 {
1062 GLfloat a[4], result[4];
1063 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
1064 result[0] = result[1] = result[2] =
1065 result[3] = (GLfloat)_mesa_sin(a[0]);
1066 store_vector4( inst, machine, result );
1067 }
1068 break;
1069 case FP_OPCODE_SLE: /* set on less or equal */
1070 {
1071 GLfloat a[4], b[4], result[4];
1072 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
1073 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
1074 result[0] = (a[0] <= b[0]) ? 1.0F : 0.0F;
1075 result[1] = (a[1] <= b[1]) ? 1.0F : 0.0F;
1076 result[2] = (a[2] <= b[2]) ? 1.0F : 0.0F;
1077 result[3] = (a[3] <= b[3]) ? 1.0F : 0.0F;
1078 store_vector4( inst, machine, result );
1079 }
1080 break;
1081 case FP_OPCODE_SLT: /* set on less */
1082 {
1083 GLfloat a[4], b[4], result[4];
1084 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
1085 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
1086 result[0] = (a[0] < b[0]) ? 1.0F : 0.0F;
1087 result[1] = (a[1] < b[1]) ? 1.0F : 0.0F;
1088 result[2] = (a[2] < b[2]) ? 1.0F : 0.0F;
1089 result[3] = (a[3] < b[3]) ? 1.0F : 0.0F;
1090 store_vector4( inst, machine, result );
1091 }
1092 break;
1093 case FP_OPCODE_SNE: /* set on not equal */
1094 {
1095 GLfloat a[4], b[4], result[4];
1096 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
1097 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
1098 result[0] = (a[0] != b[0]) ? 1.0F : 0.0F;
1099 result[1] = (a[1] != b[1]) ? 1.0F : 0.0F;
1100 result[2] = (a[2] != b[2]) ? 1.0F : 0.0F;
1101 result[3] = (a[3] != b[3]) ? 1.0F : 0.0F;
1102 store_vector4( inst, machine, result );
1103 }
1104 break;
1105 case FP_OPCODE_STR: /* set true, operands ignored */
1106 {
1107 static const GLfloat result[4] = { 1.0F, 1.0F, 1.0F, 1.0F };
1108 store_vector4( inst, machine, result );
1109 }
1110 break;
1111 case FP_OPCODE_SUB:
1112 {
1113 GLfloat a[4], b[4], result[4];
1114 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
1115 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
1116 result[0] = a[0] - b[0];
1117 result[1] = a[1] - b[1];
1118 result[2] = a[2] - b[2];
1119 result[3] = a[3] - b[3];
1120 store_vector4( inst, machine, result );
1121 }
1122 break;
1123 case FP_OPCODE_SWZ:
1124 {
1125 const struct fp_src_register *source = &inst->SrcReg[0];
1126 const GLfloat *src = get_register_pointer(ctx, source,
1127 machine, program);
1128 GLfloat result[4];
1129 GLuint i;
1130
1131 /* do extended swizzling here */
1132 for (i = 0; i < 3; i++) {
1133 if (source->Swizzle[i] == SWIZZLE_ZERO)
1134 result[i] = 0.0;
1135 else if (source->Swizzle[i] == SWIZZLE_ONE)
1136 result[i] = -1.0;
1137 else
1138 result[i] = -src[source->Swizzle[i]];
1139 if (source->NegateBase)
1140 result[i] = -result[i];
1141 }
1142 store_vector4( inst, machine, result );
1143 }
1144 break;
1145 case FP_OPCODE_TEX: /* Both ARB and NV frag prog */
1146 /* Texel lookup */
1147 {
1148 GLfloat texcoord[4], color[4];
1149 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, texcoord );
1150 /* Note: we pass 0 for LOD. The ARB extension requires it
1151 * while the NV extension says it's implementation dependant.
1152 */
1153 fetch_texel( ctx, texcoord, 0.0F, inst->TexSrcUnit, color );
1154 store_vector4( inst, machine, color );
1155 }
1156 break;
1157 case FP_OPCODE_TXB: /* GL_ARB_fragment_program only */
1158 /* Texel lookup with LOD bias */
1159 {
1160 GLfloat texcoord[4], color[4], bias, lambda;
1161
1162 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, texcoord );
1163 /* texcoord[3] is the bias to add to lambda */
1164 bias = ctx->Texture.Unit[inst->TexSrcUnit].LodBias
1165 + ctx->Texture.Unit[inst->TexSrcUnit]._Current->LodBias
1166 + texcoord[3];
1167 lambda = span->array->lambda[inst->TexSrcUnit][column] + bias;
1168 fetch_texel( ctx, texcoord, lambda,
1169 inst->TexSrcUnit, color );
1170 store_vector4( inst, machine, color );
1171 }
1172 break;
1173 case FP_OPCODE_TXD: /* GL_NV_fragment_program only */
1174 /* Texture lookup w/ partial derivatives for LOD */
1175 {
1176 GLfloat texcoord[4], dtdx[4], dtdy[4], color[4];
1177 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, texcoord );
1178 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, dtdx );
1179 fetch_vector4( ctx, &inst->SrcReg[2], machine, program, dtdy );
1180 fetch_texel_deriv( ctx, texcoord, dtdx, dtdy, inst->TexSrcUnit,
1181 color );
1182 store_vector4( inst, machine, color );
1183 }
1184 break;
1185 case FP_OPCODE_TXP: /* GL_ARB_fragment_program only */
1186 /* Texture lookup w/ projective divide */
1187 {
1188 GLfloat texcoord[4], color[4];
1189 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, texcoord );
1190 texcoord[0] /= texcoord[3];
1191 texcoord[1] /= texcoord[3];
1192 texcoord[2] /= texcoord[3];
1193 /* Note: LOD=0 */
1194 fetch_texel( ctx, texcoord, 0.0F, inst->TexSrcUnit, color );
1195 store_vector4( inst, machine, color );
1196 }
1197 break;
1198 case FP_OPCODE_TXP_NV: /* GL_NV_fragment_program only */
1199 /* Texture lookup w/ projective divide */
1200 {
1201 GLfloat texcoord[4], color[4];
1202 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, texcoord );
1203 if (inst->TexSrcBit != TEXTURE_CUBE_BIT) {
1204 texcoord[0] /= texcoord[3];
1205 texcoord[1] /= texcoord[3];
1206 texcoord[2] /= texcoord[3];
1207 }
1208 fetch_texel( ctx, texcoord,
1209 span->array->lambda[inst->TexSrcUnit][column],
1210 inst->TexSrcUnit, color );
1211 store_vector4( inst, machine, color );
1212 }
1213 break;
1214 case FP_OPCODE_UP2H: /* unpack two 16-bit floats */
1215 {
1216 GLfloat a[4], result[4];
1217 const GLuint *rawBits = (const GLuint *) a;
1218 GLhalfNV hx, hy;
1219 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
1220 hx = rawBits[0] & 0xffff;
1221 hy = rawBits[0] >> 16;
1222 result[0] = result[2] = _mesa_half_to_float(hx);
1223 result[1] = result[3] = _mesa_half_to_float(hy);
1224 store_vector4( inst, machine, result );
1225 }
1226 break;
1227 case FP_OPCODE_UP2US: /* unpack two GLushorts */
1228 {
1229 GLfloat a[4], result[4];
1230 const GLuint *rawBits = (const GLuint *) a;
1231 GLushort usx, usy;
1232 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
1233 usx = rawBits[0] & 0xffff;
1234 usy = rawBits[0] >> 16;
1235 result[0] = result[2] = usx * (1.0f / 65535.0f);
1236 result[1] = result[3] = usy * (1.0f / 65535.0f);
1237 store_vector4( inst, machine, result );
1238 }
1239 break;
1240 case FP_OPCODE_UP4B: /* unpack four GLbytes */
1241 {
1242 GLfloat a[4], result[4];
1243 const GLuint *rawBits = (const GLuint *) a;
1244 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
1245 result[0] = (((rawBits[0] >> 0) & 0xff) - 128) / 127.0F;
1246 result[1] = (((rawBits[0] >> 8) & 0xff) - 128) / 127.0F;
1247 result[2] = (((rawBits[0] >> 16) & 0xff) - 128) / 127.0F;
1248 result[3] = (((rawBits[0] >> 24) & 0xff) - 128) / 127.0F;
1249 store_vector4( inst, machine, result );
1250 }
1251 break;
1252 case FP_OPCODE_UP4UB: /* unpack four GLubytes */
1253 {
1254 GLfloat a[4], result[4];
1255 const GLuint *rawBits = (const GLuint *) a;
1256 fetch_vector1( ctx, &inst->SrcReg[0], machine, program, a );
1257 result[0] = ((rawBits[0] >> 0) & 0xff) / 255.0F;
1258 result[1] = ((rawBits[0] >> 8) & 0xff) / 255.0F;
1259 result[2] = ((rawBits[0] >> 16) & 0xff) / 255.0F;
1260 result[3] = ((rawBits[0] >> 24) & 0xff) / 255.0F;
1261 store_vector4( inst, machine, result );
1262 }
1263 break;
1264 case FP_OPCODE_XPD: /* cross product */
1265 {
1266 GLfloat a[4], b[4], result[4];
1267 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
1268 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
1269 result[0] = a[1] * b[2] - a[2] * b[1];
1270 result[1] = a[2] * b[0] - a[0] * b[2];
1271 result[2] = a[0] * b[1] - a[1] * b[0];
1272 result[3] = 1.0;
1273 store_vector4( inst, machine, result );
1274 }
1275 break;
1276 case FP_OPCODE_X2D: /* 2-D matrix transform */
1277 {
1278 GLfloat a[4], b[4], c[4], result[4];
1279 fetch_vector4( ctx, &inst->SrcReg[0], machine, program, a );
1280 fetch_vector4( ctx, &inst->SrcReg[1], machine, program, b );
1281 fetch_vector4( ctx, &inst->SrcReg[2], machine, program, c );
1282 result[0] = a[0] + b[0] * c[0] + b[1] * c[1];
1283 result[1] = a[1] + b[0] * c[2] + b[1] * c[3];
1284 result[2] = a[2] + b[0] * c[0] + b[1] * c[1];
1285 result[3] = a[3] + b[0] * c[2] + b[1] * c[3];
1286 store_vector4( inst, machine, result );
1287 }
1288 break;
1289 case FP_OPCODE_END:
1290 return GL_TRUE;
1291 default:
1292 _mesa_problem(ctx, "Bad opcode %d in _mesa_exec_fragment_program",
1293 inst->Opcode);
1294 return GL_TRUE; /* return value doesn't matter */
1295 }
1296 }
1297 return GL_TRUE;
1298 }
1299
1300
1301 static void
1302 init_machine( GLcontext *ctx, struct fp_machine *machine,
1303 const struct fragment_program *program,
1304 const struct sw_span *span, GLuint col )
1305 {
1306 GLuint inputsRead = program->InputsRead;
1307 GLuint u;
1308
1309 if (ctx->FragmentProgram.CallbackEnabled)
1310 inputsRead = ~0;
1311
1312 if (program->Base.Target == GL_FRAGMENT_PROGRAM_NV) {
1313 /* Clear temporary registers (undefined for ARB_f_p) */
1314 _mesa_bzero(machine->Temporaries,
1315 MAX_NV_FRAGMENT_PROGRAM_TEMPS * 4 * sizeof(GLfloat));
1316 }
1317
1318 /* Load input registers */
1319 if (inputsRead & (1 << FRAG_ATTRIB_WPOS)) {
1320 GLfloat *wpos = machine->Inputs[FRAG_ATTRIB_WPOS];
1321 wpos[0] = (GLfloat) span->x + col;
1322 wpos[1] = (GLfloat) span->y;
1323 wpos[2] = (GLfloat) span->array->z[col] / ctx->DepthMaxF;
1324 wpos[3] = span->w + col * span->dwdx;
1325 }
1326 if (inputsRead & (1 << FRAG_ATTRIB_COL0)) {
1327 GLfloat *col0 = machine->Inputs[FRAG_ATTRIB_COL0];
1328 col0[0] = CHAN_TO_FLOAT(span->array->rgba[col][RCOMP]);
1329 col0[1] = CHAN_TO_FLOAT(span->array->rgba[col][GCOMP]);
1330 col0[2] = CHAN_TO_FLOAT(span->array->rgba[col][BCOMP]);
1331 col0[3] = CHAN_TO_FLOAT(span->array->rgba[col][ACOMP]);
1332 }
1333 if (inputsRead & (1 << FRAG_ATTRIB_COL1)) {
1334 GLfloat *col1 = machine->Inputs[FRAG_ATTRIB_COL1];
1335 col1[0] = CHAN_TO_FLOAT(span->array->spec[col][RCOMP]);
1336 col1[1] = CHAN_TO_FLOAT(span->array->spec[col][GCOMP]);
1337 col1[2] = CHAN_TO_FLOAT(span->array->spec[col][BCOMP]);
1338 col1[3] = CHAN_TO_FLOAT(span->array->spec[col][ACOMP]);
1339 }
1340 if (inputsRead & (1 << FRAG_ATTRIB_FOGC)) {
1341 GLfloat *fogc = machine->Inputs[FRAG_ATTRIB_FOGC];
1342 fogc[0] = span->array->fog[col];
1343 fogc[1] = 0.0F;
1344 fogc[2] = 0.0F;
1345 fogc[3] = 0.0F;
1346 }
1347 for (u = 0; u < ctx->Const.MaxTextureCoordUnits; u++) {
1348 if (inputsRead & (1 << (FRAG_ATTRIB_TEX0 + u))) {
1349 GLfloat *tex = machine->Inputs[FRAG_ATTRIB_TEX0 + u];
1350 /*ASSERT(ctx->Texture._EnabledCoordUnits & (1 << u));*/
1351 COPY_4V(tex, span->array->texcoords[u][col]);
1352 /*ASSERT(tex[0] != 0 || tex[1] != 0 || tex[2] != 0);*/
1353 }
1354 }
1355
1356 /* init condition codes */
1357 machine->CondCodes[0] = COND_EQ;
1358 machine->CondCodes[1] = COND_EQ;
1359 machine->CondCodes[2] = COND_EQ;
1360 machine->CondCodes[3] = COND_EQ;
1361 }
1362
1363
1364
1365 /**
1366 * Execute the current fragment program, operating on the given span.
1367 */
1368 void
1369 _swrast_exec_fragment_program( GLcontext *ctx, struct sw_span *span )
1370 {
1371 const struct fragment_program *program = ctx->FragmentProgram.Current;
1372 GLuint i;
1373
1374 ctx->_CurrentProgram = GL_FRAGMENT_PROGRAM_ARB; /* or NV, doesn't matter */
1375
1376 for (i = 0; i < span->end; i++) {
1377 if (span->array->mask[i]) {
1378 init_machine(ctx, &ctx->FragmentProgram.Machine,
1379 ctx->FragmentProgram.Current, span, i);
1380
1381 #ifdef USE_TCC
1382 if (!_swrast_execute_codegen_program(ctx, program, ~0,
1383 &ctx->FragmentProgram.Machine,
1384 span, i)) {
1385 span->array->mask[i] = GL_FALSE; /* killed fragment */
1386 }
1387 #else
1388 if (!execute_program(ctx, program, ~0,
1389 &ctx->FragmentProgram.Machine, span, i)) {
1390 span->array->mask[i] = GL_FALSE; /* killed fragment */
1391 }
1392 #endif
1393
1394 /* Store output registers */
1395 {
1396 const GLfloat *colOut
1397 = ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_COLR];
1398 UNCLAMPED_FLOAT_TO_CHAN(span->array->rgba[i][RCOMP], colOut[0]);
1399 UNCLAMPED_FLOAT_TO_CHAN(span->array->rgba[i][GCOMP], colOut[1]);
1400 UNCLAMPED_FLOAT_TO_CHAN(span->array->rgba[i][BCOMP], colOut[2]);
1401 UNCLAMPED_FLOAT_TO_CHAN(span->array->rgba[i][ACOMP], colOut[3]);
1402 }
1403 /* depth value */
1404 if (program->OutputsWritten & (1 << FRAG_OUTPUT_DEPR))
1405 span->array->z[i] = IROUND(ctx->FragmentProgram.Machine.Outputs[FRAG_OUTPUT_DEPR][0] * ctx->DepthMaxF);
1406 }
1407 }
1408
1409 ctx->_CurrentProgram = 0;
1410 }
1411