1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file prog_execute.c
27 * Software interpreter for vertex/fragment programs.
28 * \author Brian Paul
29 */
30
31 /*
32 * NOTE: we do everything in single-precision floating point; we don't
33 * currently observe the single/half/fixed-precision qualifiers.
34 *
35 */
36
37
38 #include "c99_math.h"
39 #include "main/glheader.h"
40 #include "main/macros.h"
41 #include "prog_execute.h"
42 #include "prog_instruction.h"
43 #include "prog_parameter.h"
44 #include "prog_print.h"
45 #include "prog_noise.h"
46
47
48 /* debug predicate */
49 #define DEBUG_PROG 0
50
51
52 /**
53 * Set x to positive or negative infinity.
54 */
55 #define SET_POS_INFINITY(x) \
56 do { \
57 fi_type fi; \
58 fi.i = 0x7F800000; \
59 x = fi.f; \
60 } while (0)
61 #define SET_NEG_INFINITY(x) \
62 do { \
63 fi_type fi; \
64 fi.i = 0xFF800000; \
65 x = fi.f; \
66 } while (0)
67
68 #define SET_FLOAT_BITS(x, bits) ((fi_type *) (void *) &(x))->i = bits
69
70
71 static const GLfloat ZeroVec[4] = { 0.0F, 0.0F, 0.0F, 0.0F };
72
73
74 /**
75 * Return a pointer to the 4-element float vector specified by the given
76 * source register.
77 */
78 static inline const GLfloat *
get_src_register_pointer(const struct prog_src_register * source,const struct gl_program_machine * machine)79 get_src_register_pointer(const struct prog_src_register *source,
80 const struct gl_program_machine *machine)
81 {
82 const struct gl_program *prog = machine->CurProgram;
83 GLint reg = source->Index;
84
85 if (source->RelAddr) {
86 /* add address register value to src index/offset */
87 reg += machine->AddressReg[0][0];
88 if (reg < 0) {
89 return ZeroVec;
90 }
91 }
92
93 switch (source->File) {
94 case PROGRAM_TEMPORARY:
95 if (reg >= MAX_PROGRAM_TEMPS)
96 return ZeroVec;
97 return machine->Temporaries[reg];
98
99 case PROGRAM_INPUT:
100 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
101 if (reg >= VERT_ATTRIB_MAX)
102 return ZeroVec;
103 return machine->VertAttribs[reg];
104 }
105 else {
106 if (reg >= VARYING_SLOT_MAX)
107 return ZeroVec;
108 return machine->Attribs[reg][machine->CurElement];
109 }
110
111 case PROGRAM_OUTPUT:
112 if (reg >= MAX_PROGRAM_OUTPUTS)
113 return ZeroVec;
114 return machine->Outputs[reg];
115
116 case PROGRAM_STATE_VAR:
117 /* Fallthrough */
118 case PROGRAM_CONSTANT:
119 /* Fallthrough */
120 case PROGRAM_UNIFORM:
121 if (reg >= (GLint) prog->Parameters->NumParameters)
122 return ZeroVec;
123 return (GLfloat *) prog->Parameters->ParameterValues[reg];
124
125 case PROGRAM_SYSTEM_VALUE:
126 assert(reg < (GLint) ARRAY_SIZE(machine->SystemValues));
127 return machine->SystemValues[reg];
128
129 default:
130 _mesa_problem(NULL,
131 "Invalid src register file %d in get_src_register_pointer()",
132 source->File);
133 return ZeroVec;
134 }
135 }
136
137
138 /**
139 * Return a pointer to the 4-element float vector specified by the given
140 * destination register.
141 */
142 static inline GLfloat *
get_dst_register_pointer(const struct prog_dst_register * dest,struct gl_program_machine * machine)143 get_dst_register_pointer(const struct prog_dst_register *dest,
144 struct gl_program_machine *machine)
145 {
146 static GLfloat dummyReg[4];
147 GLint reg = dest->Index;
148
149 if (dest->RelAddr) {
150 /* add address register value to src index/offset */
151 reg += machine->AddressReg[0][0];
152 if (reg < 0) {
153 return dummyReg;
154 }
155 }
156
157 switch (dest->File) {
158 case PROGRAM_TEMPORARY:
159 if (reg >= MAX_PROGRAM_TEMPS)
160 return dummyReg;
161 return machine->Temporaries[reg];
162
163 case PROGRAM_OUTPUT:
164 if (reg >= MAX_PROGRAM_OUTPUTS)
165 return dummyReg;
166 return machine->Outputs[reg];
167
168 default:
169 _mesa_problem(NULL,
170 "Invalid dest register file %d in get_dst_register_pointer()",
171 dest->File);
172 return dummyReg;
173 }
174 }
175
176
177
178 /**
179 * Fetch a 4-element float vector from the given source register.
180 * Apply swizzling and negating as needed.
181 */
182 static void
fetch_vector4(const struct prog_src_register * source,const struct gl_program_machine * machine,GLfloat result[4])183 fetch_vector4(const struct prog_src_register *source,
184 const struct gl_program_machine *machine, GLfloat result[4])
185 {
186 const GLfloat *src = get_src_register_pointer(source, machine);
187
188 if (source->Swizzle == SWIZZLE_NOOP) {
189 /* no swizzling */
190 COPY_4V(result, src);
191 }
192 else {
193 assert(GET_SWZ(source->Swizzle, 0) <= 3);
194 assert(GET_SWZ(source->Swizzle, 1) <= 3);
195 assert(GET_SWZ(source->Swizzle, 2) <= 3);
196 assert(GET_SWZ(source->Swizzle, 3) <= 3);
197 result[0] = src[GET_SWZ(source->Swizzle, 0)];
198 result[1] = src[GET_SWZ(source->Swizzle, 1)];
199 result[2] = src[GET_SWZ(source->Swizzle, 2)];
200 result[3] = src[GET_SWZ(source->Swizzle, 3)];
201 }
202
203 if (source->Negate) {
204 assert(source->Negate == NEGATE_XYZW);
205 result[0] = -result[0];
206 result[1] = -result[1];
207 result[2] = -result[2];
208 result[3] = -result[3];
209 }
210
211 #ifdef NAN_CHECK
212 assert(!IS_INF_OR_NAN(result[0]));
213 assert(!IS_INF_OR_NAN(result[0]));
214 assert(!IS_INF_OR_NAN(result[0]));
215 assert(!IS_INF_OR_NAN(result[0]));
216 #endif
217 }
218
219
220 /**
221 * Fetch the derivative with respect to X or Y for the given register.
222 * XXX this currently only works for fragment program input attribs.
223 */
224 static void
fetch_vector4_deriv(const struct prog_src_register * source,const struct gl_program_machine * machine,char xOrY,GLfloat result[4])225 fetch_vector4_deriv(const struct prog_src_register *source,
226 const struct gl_program_machine *machine,
227 char xOrY, GLfloat result[4])
228 {
229 if (source->File == PROGRAM_INPUT &&
230 source->Index < (GLint) machine->NumDeriv) {
231 const GLint col = machine->CurElement;
232 const GLfloat w = machine->Attribs[VARYING_SLOT_POS][col][3];
233 const GLfloat invQ = 1.0f / w;
234 GLfloat deriv[4];
235
236 if (xOrY == 'X') {
237 deriv[0] = machine->DerivX[source->Index][0] * invQ;
238 deriv[1] = machine->DerivX[source->Index][1] * invQ;
239 deriv[2] = machine->DerivX[source->Index][2] * invQ;
240 deriv[3] = machine->DerivX[source->Index][3] * invQ;
241 }
242 else {
243 deriv[0] = machine->DerivY[source->Index][0] * invQ;
244 deriv[1] = machine->DerivY[source->Index][1] * invQ;
245 deriv[2] = machine->DerivY[source->Index][2] * invQ;
246 deriv[3] = machine->DerivY[source->Index][3] * invQ;
247 }
248
249 result[0] = deriv[GET_SWZ(source->Swizzle, 0)];
250 result[1] = deriv[GET_SWZ(source->Swizzle, 1)];
251 result[2] = deriv[GET_SWZ(source->Swizzle, 2)];
252 result[3] = deriv[GET_SWZ(source->Swizzle, 3)];
253
254 if (source->Negate) {
255 assert(source->Negate == NEGATE_XYZW);
256 result[0] = -result[0];
257 result[1] = -result[1];
258 result[2] = -result[2];
259 result[3] = -result[3];
260 }
261 }
262 else {
263 ASSIGN_4V(result, 0.0, 0.0, 0.0, 0.0);
264 }
265 }
266
267
268 /**
269 * As above, but only return result[0] element.
270 */
271 static void
fetch_vector1(const struct prog_src_register * source,const struct gl_program_machine * machine,GLfloat result[4])272 fetch_vector1(const struct prog_src_register *source,
273 const struct gl_program_machine *machine, GLfloat result[4])
274 {
275 const GLfloat *src = get_src_register_pointer(source, machine);
276
277 result[0] = src[GET_SWZ(source->Swizzle, 0)];
278
279 if (source->Negate) {
280 result[0] = -result[0];
281 }
282 }
283
284
285 /**
286 * Fetch texel from texture. Use partial derivatives when possible.
287 */
288 static inline void
fetch_texel(struct gl_context * ctx,const struct gl_program_machine * machine,const struct prog_instruction * inst,const GLfloat texcoord[4],GLfloat lodBias,GLfloat color[4])289 fetch_texel(struct gl_context *ctx,
290 const struct gl_program_machine *machine,
291 const struct prog_instruction *inst,
292 const GLfloat texcoord[4], GLfloat lodBias,
293 GLfloat color[4])
294 {
295 const GLuint unit = machine->Samplers[inst->TexSrcUnit];
296
297 /* Note: we only have the right derivatives for fragment input attribs.
298 */
299 if (machine->NumDeriv > 0 &&
300 inst->SrcReg[0].File == PROGRAM_INPUT &&
301 inst->SrcReg[0].Index == VARYING_SLOT_TEX0 + inst->TexSrcUnit) {
302 /* simple texture fetch for which we should have derivatives */
303 GLuint attr = inst->SrcReg[0].Index;
304 machine->FetchTexelDeriv(ctx, texcoord,
305 machine->DerivX[attr],
306 machine->DerivY[attr],
307 lodBias, unit, color);
308 }
309 else {
310 machine->FetchTexelLod(ctx, texcoord, lodBias, unit, color);
311 }
312 }
313
314
315 /**
316 * Store 4 floats into a register. Observe the instructions saturate and
317 * set-condition-code flags.
318 */
319 static void
store_vector4(const struct prog_instruction * inst,struct gl_program_machine * machine,const GLfloat value[4])320 store_vector4(const struct prog_instruction *inst,
321 struct gl_program_machine *machine, const GLfloat value[4])
322 {
323 const struct prog_dst_register *dstReg = &(inst->DstReg);
324 const GLboolean clamp = inst->Saturate;
325 GLuint writeMask = dstReg->WriteMask;
326 GLfloat clampedValue[4];
327 GLfloat *dst = get_dst_register_pointer(dstReg, machine);
328
329 #if 0
330 if (value[0] > 1.0e10 ||
331 IS_INF_OR_NAN(value[0]) ||
332 IS_INF_OR_NAN(value[1]) ||
333 IS_INF_OR_NAN(value[2]) || IS_INF_OR_NAN(value[3]))
334 printf("store %g %g %g %g\n", value[0], value[1], value[2], value[3]);
335 #endif
336
337 if (clamp) {
338 clampedValue[0] = CLAMP(value[0], 0.0F, 1.0F);
339 clampedValue[1] = CLAMP(value[1], 0.0F, 1.0F);
340 clampedValue[2] = CLAMP(value[2], 0.0F, 1.0F);
341 clampedValue[3] = CLAMP(value[3], 0.0F, 1.0F);
342 value = clampedValue;
343 }
344
345 #ifdef NAN_CHECK
346 assert(!IS_INF_OR_NAN(value[0]));
347 assert(!IS_INF_OR_NAN(value[0]));
348 assert(!IS_INF_OR_NAN(value[0]));
349 assert(!IS_INF_OR_NAN(value[0]));
350 #endif
351
352 if (writeMask & WRITEMASK_X)
353 dst[0] = value[0];
354 if (writeMask & WRITEMASK_Y)
355 dst[1] = value[1];
356 if (writeMask & WRITEMASK_Z)
357 dst[2] = value[2];
358 if (writeMask & WRITEMASK_W)
359 dst[3] = value[3];
360 }
361
362
363 /**
364 * Execute the given vertex/fragment program.
365 *
366 * \param ctx rendering context
367 * \param program the program to execute
368 * \param machine machine state (must be initialized)
369 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
370 */
371 GLboolean
_mesa_execute_program(struct gl_context * ctx,const struct gl_program * program,struct gl_program_machine * machine)372 _mesa_execute_program(struct gl_context * ctx,
373 const struct gl_program *program,
374 struct gl_program_machine *machine)
375 {
376 const GLuint numInst = program->arb.NumInstructions;
377 const GLuint maxExec = 65536;
378 GLuint pc, numExec = 0;
379
380 machine->CurProgram = program;
381
382 if (DEBUG_PROG) {
383 printf("execute program %u --------------------\n", program->Id);
384 }
385
386 if (program->Target == GL_VERTEX_PROGRAM_ARB) {
387 machine->EnvParams = ctx->VertexProgram.Parameters;
388 }
389 else {
390 machine->EnvParams = ctx->FragmentProgram.Parameters;
391 }
392
393 for (pc = 0; pc < numInst; pc++) {
394 const struct prog_instruction *inst = program->arb.Instructions + pc;
395
396 if (DEBUG_PROG) {
397 _mesa_print_instruction(inst);
398 }
399
400 switch (inst->Opcode) {
401 case OPCODE_ABS:
402 {
403 GLfloat a[4], result[4];
404 fetch_vector4(&inst->SrcReg[0], machine, a);
405 result[0] = fabsf(a[0]);
406 result[1] = fabsf(a[1]);
407 result[2] = fabsf(a[2]);
408 result[3] = fabsf(a[3]);
409 store_vector4(inst, machine, result);
410 }
411 break;
412 case OPCODE_ADD:
413 {
414 GLfloat a[4], b[4], result[4];
415 fetch_vector4(&inst->SrcReg[0], machine, a);
416 fetch_vector4(&inst->SrcReg[1], machine, b);
417 result[0] = a[0] + b[0];
418 result[1] = a[1] + b[1];
419 result[2] = a[2] + b[2];
420 result[3] = a[3] + b[3];
421 store_vector4(inst, machine, result);
422 if (DEBUG_PROG) {
423 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
424 result[0], result[1], result[2], result[3],
425 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
426 }
427 }
428 break;
429 case OPCODE_ARL:
430 {
431 GLfloat t[4];
432 fetch_vector4(&inst->SrcReg[0], machine, t);
433 machine->AddressReg[0][0] = IFLOOR(t[0]);
434 if (DEBUG_PROG) {
435 printf("ARL %d\n", machine->AddressReg[0][0]);
436 }
437 }
438 break;
439 case OPCODE_BGNLOOP:
440 /* no-op */
441 assert(program->arb.Instructions[inst->BranchTarget].Opcode
442 == OPCODE_ENDLOOP);
443 break;
444 case OPCODE_ENDLOOP:
445 /* subtract 1 here since pc is incremented by for(pc) loop */
446 assert(program->arb.Instructions[inst->BranchTarget].Opcode
447 == OPCODE_BGNLOOP);
448 pc = inst->BranchTarget - 1; /* go to matching BNGLOOP */
449 break;
450 case OPCODE_BGNSUB: /* begin subroutine */
451 break;
452 case OPCODE_ENDSUB: /* end subroutine */
453 break;
454 case OPCODE_BRK: /* break out of loop (conditional) */
455 assert(program->arb.Instructions[inst->BranchTarget].Opcode
456 == OPCODE_ENDLOOP);
457 /* break out of loop */
458 /* pc++ at end of for-loop will put us after the ENDLOOP inst */
459 pc = inst->BranchTarget;
460 break;
461 case OPCODE_CONT: /* continue loop (conditional) */
462 assert(program->arb.Instructions[inst->BranchTarget].Opcode
463 == OPCODE_ENDLOOP);
464 /* continue at ENDLOOP */
465 /* Subtract 1 here since we'll do pc++ at end of for-loop */
466 pc = inst->BranchTarget - 1;
467 break;
468 case OPCODE_CAL: /* Call subroutine (conditional) */
469 /* call the subroutine */
470 if (machine->StackDepth >= MAX_PROGRAM_CALL_DEPTH) {
471 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
472 }
473 machine->CallStack[machine->StackDepth++] = pc + 1; /* next inst */
474 /* Subtract 1 here since we'll do pc++ at end of for-loop */
475 pc = inst->BranchTarget - 1;
476 break;
477 case OPCODE_CMP:
478 {
479 GLfloat a[4], b[4], c[4], result[4];
480 fetch_vector4(&inst->SrcReg[0], machine, a);
481 fetch_vector4(&inst->SrcReg[1], machine, b);
482 fetch_vector4(&inst->SrcReg[2], machine, c);
483 result[0] = a[0] < 0.0F ? b[0] : c[0];
484 result[1] = a[1] < 0.0F ? b[1] : c[1];
485 result[2] = a[2] < 0.0F ? b[2] : c[2];
486 result[3] = a[3] < 0.0F ? b[3] : c[3];
487 store_vector4(inst, machine, result);
488 if (DEBUG_PROG) {
489 printf("CMP (%g %g %g %g) = (%g %g %g %g) < 0 ? (%g %g %g %g) : (%g %g %g %g)\n",
490 result[0], result[1], result[2], result[3],
491 a[0], a[1], a[2], a[3],
492 b[0], b[1], b[2], b[3],
493 c[0], c[1], c[2], c[3]);
494 }
495 }
496 break;
497 case OPCODE_COS:
498 {
499 GLfloat a[4], result[4];
500 fetch_vector1(&inst->SrcReg[0], machine, a);
501 result[0] = result[1] = result[2] = result[3]
502 = cosf(a[0]);
503 store_vector4(inst, machine, result);
504 }
505 break;
506 case OPCODE_DDX: /* Partial derivative with respect to X */
507 {
508 GLfloat result[4];
509 fetch_vector4_deriv(&inst->SrcReg[0], machine, 'X', result);
510 store_vector4(inst, machine, result);
511 }
512 break;
513 case OPCODE_DDY: /* Partial derivative with respect to Y */
514 {
515 GLfloat result[4];
516 fetch_vector4_deriv(&inst->SrcReg[0], machine, 'Y', result);
517 store_vector4(inst, machine, result);
518 }
519 break;
520 case OPCODE_DP2:
521 {
522 GLfloat a[4], b[4], result[4];
523 fetch_vector4(&inst->SrcReg[0], machine, a);
524 fetch_vector4(&inst->SrcReg[1], machine, b);
525 result[0] = result[1] = result[2] = result[3] = DOT2(a, b);
526 store_vector4(inst, machine, result);
527 if (DEBUG_PROG) {
528 printf("DP2 %g = (%g %g) . (%g %g)\n",
529 result[0], a[0], a[1], b[0], b[1]);
530 }
531 }
532 break;
533 case OPCODE_DP3:
534 {
535 GLfloat a[4], b[4], result[4];
536 fetch_vector4(&inst->SrcReg[0], machine, a);
537 fetch_vector4(&inst->SrcReg[1], machine, b);
538 result[0] = result[1] = result[2] = result[3] = DOT3(a, b);
539 store_vector4(inst, machine, result);
540 if (DEBUG_PROG) {
541 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
542 result[0], a[0], a[1], a[2], b[0], b[1], b[2]);
543 }
544 }
545 break;
546 case OPCODE_DP4:
547 {
548 GLfloat a[4], b[4], result[4];
549 fetch_vector4(&inst->SrcReg[0], machine, a);
550 fetch_vector4(&inst->SrcReg[1], machine, b);
551 result[0] = result[1] = result[2] = result[3] = DOT4(a, b);
552 store_vector4(inst, machine, result);
553 if (DEBUG_PROG) {
554 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
555 result[0], a[0], a[1], a[2], a[3],
556 b[0], b[1], b[2], b[3]);
557 }
558 }
559 break;
560 case OPCODE_DPH:
561 {
562 GLfloat a[4], b[4], result[4];
563 fetch_vector4(&inst->SrcReg[0], machine, a);
564 fetch_vector4(&inst->SrcReg[1], machine, b);
565 result[0] = result[1] = result[2] = result[3] = DOT3(a, b) + b[3];
566 store_vector4(inst, machine, result);
567 }
568 break;
569 case OPCODE_DST: /* Distance vector */
570 {
571 GLfloat a[4], b[4], result[4];
572 fetch_vector4(&inst->SrcReg[0], machine, a);
573 fetch_vector4(&inst->SrcReg[1], machine, b);
574 result[0] = 1.0F;
575 result[1] = a[1] * b[1];
576 result[2] = a[2];
577 result[3] = b[3];
578 store_vector4(inst, machine, result);
579 }
580 break;
581 case OPCODE_EXP:
582 {
583 GLfloat t[4], q[4], floor_t0;
584 fetch_vector1(&inst->SrcReg[0], machine, t);
585 floor_t0 = floorf(t[0]);
586 if (floor_t0 > FLT_MAX_EXP) {
587 SET_POS_INFINITY(q[0]);
588 SET_POS_INFINITY(q[2]);
589 }
590 else if (floor_t0 < FLT_MIN_EXP) {
591 q[0] = 0.0F;
592 q[2] = 0.0F;
593 }
594 else {
595 q[0] = ldexpf(1.0, (int) floor_t0);
596 /* Note: GL_NV_vertex_program expects
597 * result.z = result.x * APPX(result.y)
598 * We do what the ARB extension says.
599 */
600 q[2] = exp2f(t[0]);
601 }
602 q[1] = t[0] - floor_t0;
603 q[3] = 1.0F;
604 store_vector4( inst, machine, q );
605 }
606 break;
607 case OPCODE_EX2: /* Exponential base 2 */
608 {
609 GLfloat a[4], result[4], val;
610 fetch_vector1(&inst->SrcReg[0], machine, a);
611 val = exp2f(a[0]);
612 /*
613 if (IS_INF_OR_NAN(val))
614 val = 1.0e10;
615 */
616 result[0] = result[1] = result[2] = result[3] = val;
617 store_vector4(inst, machine, result);
618 }
619 break;
620 case OPCODE_FLR:
621 {
622 GLfloat a[4], result[4];
623 fetch_vector4(&inst->SrcReg[0], machine, a);
624 result[0] = floorf(a[0]);
625 result[1] = floorf(a[1]);
626 result[2] = floorf(a[2]);
627 result[3] = floorf(a[3]);
628 store_vector4(inst, machine, result);
629 }
630 break;
631 case OPCODE_FRC:
632 {
633 GLfloat a[4], result[4];
634 fetch_vector4(&inst->SrcReg[0], machine, a);
635 result[0] = a[0] - floorf(a[0]);
636 result[1] = a[1] - floorf(a[1]);
637 result[2] = a[2] - floorf(a[2]);
638 result[3] = a[3] - floorf(a[3]);
639 store_vector4(inst, machine, result);
640 }
641 break;
642 case OPCODE_IF:
643 {
644 GLboolean cond;
645 assert(program->arb.Instructions[inst->BranchTarget].Opcode
646 == OPCODE_ELSE ||
647 program->arb.Instructions[inst->BranchTarget].Opcode
648 == OPCODE_ENDIF);
649 /* eval condition */
650 GLfloat a[4];
651 fetch_vector1(&inst->SrcReg[0], machine, a);
652 cond = (a[0] != 0.0F);
653 if (DEBUG_PROG) {
654 printf("IF: %d\n", cond);
655 }
656 /* do if/else */
657 if (cond) {
658 /* do if-clause (just continue execution) */
659 }
660 else {
661 /* go to the instruction after ELSE or ENDIF */
662 assert(inst->BranchTarget >= 0);
663 pc = inst->BranchTarget;
664 }
665 }
666 break;
667 case OPCODE_ELSE:
668 /* goto ENDIF */
669 assert(program->arb.Instructions[inst->BranchTarget].Opcode
670 == OPCODE_ENDIF);
671 assert(inst->BranchTarget >= 0);
672 pc = inst->BranchTarget;
673 break;
674 case OPCODE_ENDIF:
675 /* nothing */
676 break;
677 case OPCODE_KIL: /* ARB_f_p only */
678 {
679 GLfloat a[4];
680 fetch_vector4(&inst->SrcReg[0], machine, a);
681 if (DEBUG_PROG) {
682 printf("KIL if (%g %g %g %g) <= 0.0\n",
683 a[0], a[1], a[2], a[3]);
684 }
685
686 if (a[0] < 0.0F || a[1] < 0.0F || a[2] < 0.0F || a[3] < 0.0F) {
687 return GL_FALSE;
688 }
689 }
690 break;
691 case OPCODE_LG2: /* log base 2 */
692 {
693 GLfloat a[4], result[4], val;
694 fetch_vector1(&inst->SrcReg[0], machine, a);
695 /* The fast LOG2 macro doesn't meet the precision requirements.
696 */
697 if (a[0] == 0.0F) {
698 val = -FLT_MAX;
699 }
700 else {
701 val = logf(a[0]) * 1.442695F;
702 }
703 result[0] = result[1] = result[2] = result[3] = val;
704 store_vector4(inst, machine, result);
705 }
706 break;
707 case OPCODE_LIT:
708 {
709 const GLfloat epsilon = 1.0F / 256.0F; /* from NV VP spec */
710 GLfloat a[4], result[4];
711 fetch_vector4(&inst->SrcReg[0], machine, a);
712 a[0] = MAX2(a[0], 0.0F);
713 a[1] = MAX2(a[1], 0.0F);
714 /* XXX ARB version clamps a[3], NV version doesn't */
715 a[3] = CLAMP(a[3], -(128.0F - epsilon), (128.0F - epsilon));
716 result[0] = 1.0F;
717 result[1] = a[0];
718 /* XXX we could probably just use pow() here */
719 if (a[0] > 0.0F) {
720 if (a[1] == 0.0F && a[3] == 0.0F)
721 result[2] = 1.0F;
722 else
723 result[2] = powf(a[1], a[3]);
724 }
725 else {
726 result[2] = 0.0F;
727 }
728 result[3] = 1.0F;
729 store_vector4(inst, machine, result);
730 if (DEBUG_PROG) {
731 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
732 result[0], result[1], result[2], result[3],
733 a[0], a[1], a[2], a[3]);
734 }
735 }
736 break;
737 case OPCODE_LOG:
738 {
739 GLfloat t[4], q[4], abs_t0;
740 fetch_vector1(&inst->SrcReg[0], machine, t);
741 abs_t0 = fabsf(t[0]);
742 if (abs_t0 != 0.0F) {
743 if (IS_INF_OR_NAN(abs_t0))
744 {
745 SET_POS_INFINITY(q[0]);
746 q[1] = 1.0F;
747 SET_POS_INFINITY(q[2]);
748 }
749 else {
750 int exponent;
751 GLfloat mantissa = frexpf(t[0], &exponent);
752 q[0] = (GLfloat) (exponent - 1);
753 q[1] = 2.0F * mantissa; /* map [.5, 1) -> [1, 2) */
754
755 /* The fast LOG2 macro doesn't meet the precision
756 * requirements.
757 */
758 q[2] = logf(t[0]) * 1.442695F;
759 }
760 }
761 else {
762 SET_NEG_INFINITY(q[0]);
763 q[1] = 1.0F;
764 SET_NEG_INFINITY(q[2]);
765 }
766 q[3] = 1.0;
767 store_vector4(inst, machine, q);
768 }
769 break;
770 case OPCODE_LRP:
771 {
772 GLfloat a[4], b[4], c[4], result[4];
773 fetch_vector4(&inst->SrcReg[0], machine, a);
774 fetch_vector4(&inst->SrcReg[1], machine, b);
775 fetch_vector4(&inst->SrcReg[2], machine, c);
776 result[0] = a[0] * b[0] + (1.0F - a[0]) * c[0];
777 result[1] = a[1] * b[1] + (1.0F - a[1]) * c[1];
778 result[2] = a[2] * b[2] + (1.0F - a[2]) * c[2];
779 result[3] = a[3] * b[3] + (1.0F - a[3]) * c[3];
780 store_vector4(inst, machine, result);
781 if (DEBUG_PROG) {
782 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
783 "(%g %g %g %g), (%g %g %g %g)\n",
784 result[0], result[1], result[2], result[3],
785 a[0], a[1], a[2], a[3],
786 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
787 }
788 }
789 break;
790 case OPCODE_MAD:
791 {
792 GLfloat a[4], b[4], c[4], result[4];
793 fetch_vector4(&inst->SrcReg[0], machine, a);
794 fetch_vector4(&inst->SrcReg[1], machine, b);
795 fetch_vector4(&inst->SrcReg[2], machine, c);
796 result[0] = a[0] * b[0] + c[0];
797 result[1] = a[1] * b[1] + c[1];
798 result[2] = a[2] * b[2] + c[2];
799 result[3] = a[3] * b[3] + c[3];
800 store_vector4(inst, machine, result);
801 if (DEBUG_PROG) {
802 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
803 "(%g %g %g %g) + (%g %g %g %g)\n",
804 result[0], result[1], result[2], result[3],
805 a[0], a[1], a[2], a[3],
806 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
807 }
808 }
809 break;
810 case OPCODE_MAX:
811 {
812 GLfloat a[4], b[4], result[4];
813 fetch_vector4(&inst->SrcReg[0], machine, a);
814 fetch_vector4(&inst->SrcReg[1], machine, b);
815 result[0] = MAX2(a[0], b[0]);
816 result[1] = MAX2(a[1], b[1]);
817 result[2] = MAX2(a[2], b[2]);
818 result[3] = MAX2(a[3], b[3]);
819 store_vector4(inst, machine, result);
820 if (DEBUG_PROG) {
821 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
822 result[0], result[1], result[2], result[3],
823 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
824 }
825 }
826 break;
827 case OPCODE_MIN:
828 {
829 GLfloat a[4], b[4], result[4];
830 fetch_vector4(&inst->SrcReg[0], machine, a);
831 fetch_vector4(&inst->SrcReg[1], machine, b);
832 result[0] = MIN2(a[0], b[0]);
833 result[1] = MIN2(a[1], b[1]);
834 result[2] = MIN2(a[2], b[2]);
835 result[3] = MIN2(a[3], b[3]);
836 store_vector4(inst, machine, result);
837 }
838 break;
839 case OPCODE_MOV:
840 {
841 GLfloat result[4];
842 fetch_vector4(&inst->SrcReg[0], machine, result);
843 store_vector4(inst, machine, result);
844 if (DEBUG_PROG) {
845 printf("MOV (%g %g %g %g)\n",
846 result[0], result[1], result[2], result[3]);
847 }
848 }
849 break;
850 case OPCODE_MUL:
851 {
852 GLfloat a[4], b[4], result[4];
853 fetch_vector4(&inst->SrcReg[0], machine, a);
854 fetch_vector4(&inst->SrcReg[1], machine, b);
855 result[0] = a[0] * b[0];
856 result[1] = a[1] * b[1];
857 result[2] = a[2] * b[2];
858 result[3] = a[3] * b[3];
859 store_vector4(inst, machine, result);
860 if (DEBUG_PROG) {
861 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
862 result[0], result[1], result[2], result[3],
863 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
864 }
865 }
866 break;
867 case OPCODE_NOISE1:
868 {
869 GLfloat a[4], result[4];
870 fetch_vector1(&inst->SrcReg[0], machine, a);
871 result[0] =
872 result[1] =
873 result[2] =
874 result[3] = _mesa_noise1(a[0]);
875 store_vector4(inst, machine, result);
876 }
877 break;
878 case OPCODE_NOISE2:
879 {
880 GLfloat a[4], result[4];
881 fetch_vector4(&inst->SrcReg[0], machine, a);
882 result[0] =
883 result[1] =
884 result[2] = result[3] = _mesa_noise2(a[0], a[1]);
885 store_vector4(inst, machine, result);
886 }
887 break;
888 case OPCODE_NOISE3:
889 {
890 GLfloat a[4], result[4];
891 fetch_vector4(&inst->SrcReg[0], machine, a);
892 result[0] =
893 result[1] =
894 result[2] =
895 result[3] = _mesa_noise3(a[0], a[1], a[2]);
896 store_vector4(inst, machine, result);
897 }
898 break;
899 case OPCODE_NOISE4:
900 {
901 GLfloat a[4], result[4];
902 fetch_vector4(&inst->SrcReg[0], machine, a);
903 result[0] =
904 result[1] =
905 result[2] =
906 result[3] = _mesa_noise4(a[0], a[1], a[2], a[3]);
907 store_vector4(inst, machine, result);
908 }
909 break;
910 case OPCODE_NOP:
911 break;
912 case OPCODE_POW:
913 {
914 GLfloat a[4], b[4], result[4];
915 fetch_vector1(&inst->SrcReg[0], machine, a);
916 fetch_vector1(&inst->SrcReg[1], machine, b);
917 result[0] = result[1] = result[2] = result[3]
918 = powf(a[0], b[0]);
919 store_vector4(inst, machine, result);
920 }
921 break;
922
923 case OPCODE_RCP:
924 {
925 GLfloat a[4], result[4];
926 fetch_vector1(&inst->SrcReg[0], machine, a);
927 if (DEBUG_PROG) {
928 if (a[0] == 0)
929 printf("RCP(0)\n");
930 else if (IS_INF_OR_NAN(a[0]))
931 printf("RCP(inf)\n");
932 }
933 result[0] = result[1] = result[2] = result[3] = 1.0F / a[0];
934 store_vector4(inst, machine, result);
935 }
936 break;
937 case OPCODE_RET: /* return from subroutine (conditional) */
938 if (machine->StackDepth == 0) {
939 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
940 }
941 /* subtract one because of pc++ in the for loop */
942 pc = machine->CallStack[--machine->StackDepth] - 1;
943 break;
944 case OPCODE_RSQ: /* 1 / sqrt() */
945 {
946 GLfloat a[4], result[4];
947 fetch_vector1(&inst->SrcReg[0], machine, a);
948 a[0] = fabsf(a[0]);
949 result[0] = result[1] = result[2] = result[3] = 1.0f / sqrtf(a[0]);
950 store_vector4(inst, machine, result);
951 if (DEBUG_PROG) {
952 printf("RSQ %g = 1/sqrt(|%g|)\n", result[0], a[0]);
953 }
954 }
955 break;
956 case OPCODE_SCS: /* sine and cos */
957 {
958 GLfloat a[4], result[4];
959 fetch_vector1(&inst->SrcReg[0], machine, a);
960 result[0] = cosf(a[0]);
961 result[1] = sinf(a[0]);
962 result[2] = 0.0F; /* undefined! */
963 result[3] = 0.0F; /* undefined! */
964 store_vector4(inst, machine, result);
965 }
966 break;
967 case OPCODE_SGE: /* set on greater or equal */
968 {
969 GLfloat a[4], b[4], result[4];
970 fetch_vector4(&inst->SrcReg[0], machine, a);
971 fetch_vector4(&inst->SrcReg[1], machine, b);
972 result[0] = (a[0] >= b[0]) ? 1.0F : 0.0F;
973 result[1] = (a[1] >= b[1]) ? 1.0F : 0.0F;
974 result[2] = (a[2] >= b[2]) ? 1.0F : 0.0F;
975 result[3] = (a[3] >= b[3]) ? 1.0F : 0.0F;
976 store_vector4(inst, machine, result);
977 if (DEBUG_PROG) {
978 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
979 result[0], result[1], result[2], result[3],
980 a[0], a[1], a[2], a[3],
981 b[0], b[1], b[2], b[3]);
982 }
983 }
984 break;
985 case OPCODE_SIN:
986 {
987 GLfloat a[4], result[4];
988 fetch_vector1(&inst->SrcReg[0], machine, a);
989 result[0] = result[1] = result[2] = result[3]
990 = sinf(a[0]);
991 store_vector4(inst, machine, result);
992 }
993 break;
994 case OPCODE_SLT: /* set on less */
995 {
996 GLfloat a[4], b[4], result[4];
997 fetch_vector4(&inst->SrcReg[0], machine, a);
998 fetch_vector4(&inst->SrcReg[1], machine, b);
999 result[0] = (a[0] < b[0]) ? 1.0F : 0.0F;
1000 result[1] = (a[1] < b[1]) ? 1.0F : 0.0F;
1001 result[2] = (a[2] < b[2]) ? 1.0F : 0.0F;
1002 result[3] = (a[3] < b[3]) ? 1.0F : 0.0F;
1003 store_vector4(inst, machine, result);
1004 if (DEBUG_PROG) {
1005 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1006 result[0], result[1], result[2], result[3],
1007 a[0], a[1], a[2], a[3],
1008 b[0], b[1], b[2], b[3]);
1009 }
1010 }
1011 break;
1012 case OPCODE_SSG: /* set sign (-1, 0 or +1) */
1013 {
1014 GLfloat a[4], result[4];
1015 fetch_vector4(&inst->SrcReg[0], machine, a);
1016 result[0] = (GLfloat) ((a[0] > 0.0F) - (a[0] < 0.0F));
1017 result[1] = (GLfloat) ((a[1] > 0.0F) - (a[1] < 0.0F));
1018 result[2] = (GLfloat) ((a[2] > 0.0F) - (a[2] < 0.0F));
1019 result[3] = (GLfloat) ((a[3] > 0.0F) - (a[3] < 0.0F));
1020 store_vector4(inst, machine, result);
1021 }
1022 break;
1023 case OPCODE_SUB:
1024 {
1025 GLfloat a[4], b[4], result[4];
1026 fetch_vector4(&inst->SrcReg[0], machine, a);
1027 fetch_vector4(&inst->SrcReg[1], machine, b);
1028 result[0] = a[0] - b[0];
1029 result[1] = a[1] - b[1];
1030 result[2] = a[2] - b[2];
1031 result[3] = a[3] - b[3];
1032 store_vector4(inst, machine, result);
1033 if (DEBUG_PROG) {
1034 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1035 result[0], result[1], result[2], result[3],
1036 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
1037 }
1038 }
1039 break;
1040 case OPCODE_SWZ: /* extended swizzle */
1041 {
1042 const struct prog_src_register *source = &inst->SrcReg[0];
1043 const GLfloat *src = get_src_register_pointer(source, machine);
1044 GLfloat result[4];
1045 GLuint i;
1046 for (i = 0; i < 4; i++) {
1047 const GLuint swz = GET_SWZ(source->Swizzle, i);
1048 if (swz == SWIZZLE_ZERO)
1049 result[i] = 0.0;
1050 else if (swz == SWIZZLE_ONE)
1051 result[i] = 1.0;
1052 else {
1053 assert(swz <= 3);
1054 result[i] = src[swz];
1055 }
1056 if (source->Negate & (1 << i))
1057 result[i] = -result[i];
1058 }
1059 store_vector4(inst, machine, result);
1060 }
1061 break;
1062 case OPCODE_TEX: /* Both ARB and NV frag prog */
1063 /* Simple texel lookup */
1064 {
1065 GLfloat texcoord[4], color[4];
1066 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1067
1068 /* For TEX, texcoord.Q should not be used and its value should not
1069 * matter (at most, we pass coord.xyz to texture3D() in GLSL).
1070 * Set Q=1 so that FetchTexelDeriv() doesn't get a garbage value
1071 * which is effectively what happens when the texcoord swizzle
1072 * is .xyzz
1073 */
1074 texcoord[3] = 1.0f;
1075
1076 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1077
1078 if (DEBUG_PROG) {
1079 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
1080 color[0], color[1], color[2], color[3],
1081 inst->TexSrcUnit,
1082 texcoord[0], texcoord[1], texcoord[2], texcoord[3]);
1083 }
1084 store_vector4(inst, machine, color);
1085 }
1086 break;
1087 case OPCODE_TXB: /* GL_ARB_fragment_program only */
1088 /* Texel lookup with LOD bias */
1089 {
1090 GLfloat texcoord[4], color[4], lodBias;
1091
1092 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1093
1094 /* texcoord[3] is the bias to add to lambda */
1095 lodBias = texcoord[3];
1096
1097 fetch_texel(ctx, machine, inst, texcoord, lodBias, color);
1098
1099 if (DEBUG_PROG) {
1100 printf("TXB (%g, %g, %g, %g) = texture[%d][%g %g %g %g]"
1101 " bias %g\n",
1102 color[0], color[1], color[2], color[3],
1103 inst->TexSrcUnit,
1104 texcoord[0],
1105 texcoord[1],
1106 texcoord[2],
1107 texcoord[3],
1108 lodBias);
1109 }
1110
1111 store_vector4(inst, machine, color);
1112 }
1113 break;
1114 case OPCODE_TXD:
1115 /* Texture lookup w/ partial derivatives for LOD */
1116 {
1117 GLfloat texcoord[4], dtdx[4], dtdy[4], color[4];
1118 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1119 fetch_vector4(&inst->SrcReg[1], machine, dtdx);
1120 fetch_vector4(&inst->SrcReg[2], machine, dtdy);
1121 machine->FetchTexelDeriv(ctx, texcoord, dtdx, dtdy,
1122 0.0, /* lodBias */
1123 inst->TexSrcUnit, color);
1124 store_vector4(inst, machine, color);
1125 }
1126 break;
1127 case OPCODE_TXL:
1128 /* Texel lookup with explicit LOD */
1129 {
1130 GLfloat texcoord[4], color[4], lod;
1131
1132 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1133
1134 /* texcoord[3] is the LOD */
1135 lod = texcoord[3];
1136
1137 machine->FetchTexelLod(ctx, texcoord, lod,
1138 machine->Samplers[inst->TexSrcUnit], color);
1139
1140 store_vector4(inst, machine, color);
1141 }
1142 break;
1143 case OPCODE_TXP: /* GL_ARB_fragment_program only */
1144 /* Texture lookup w/ projective divide */
1145 {
1146 GLfloat texcoord[4], color[4];
1147
1148 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1149 /* Not so sure about this test - if texcoord[3] is
1150 * zero, we'd probably be fine except for an assert in
1151 * IROUND_POS() which gets triggered by the inf values created.
1152 */
1153 if (texcoord[3] != 0.0F) {
1154 texcoord[0] /= texcoord[3];
1155 texcoord[1] /= texcoord[3];
1156 texcoord[2] /= texcoord[3];
1157 }
1158
1159 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1160
1161 store_vector4(inst, machine, color);
1162 }
1163 break;
1164 case OPCODE_TRUNC: /* truncate toward zero */
1165 {
1166 GLfloat a[4], result[4];
1167 fetch_vector4(&inst->SrcReg[0], machine, a);
1168 result[0] = (GLfloat) (GLint) a[0];
1169 result[1] = (GLfloat) (GLint) a[1];
1170 result[2] = (GLfloat) (GLint) a[2];
1171 result[3] = (GLfloat) (GLint) a[3];
1172 store_vector4(inst, machine, result);
1173 }
1174 break;
1175 case OPCODE_XPD: /* cross product */
1176 {
1177 GLfloat a[4], b[4], result[4];
1178 fetch_vector4(&inst->SrcReg[0], machine, a);
1179 fetch_vector4(&inst->SrcReg[1], machine, b);
1180 result[0] = a[1] * b[2] - a[2] * b[1];
1181 result[1] = a[2] * b[0] - a[0] * b[2];
1182 result[2] = a[0] * b[1] - a[1] * b[0];
1183 result[3] = 1.0;
1184 store_vector4(inst, machine, result);
1185 if (DEBUG_PROG) {
1186 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1187 result[0], result[1], result[2], result[3],
1188 a[0], a[1], a[2], b[0], b[1], b[2]);
1189 }
1190 }
1191 break;
1192 case OPCODE_END:
1193 return GL_TRUE;
1194 default:
1195 _mesa_problem(ctx, "Bad opcode %d in _mesa_execute_program",
1196 inst->Opcode);
1197 return GL_TRUE; /* return value doesn't matter */
1198 }
1199
1200 numExec++;
1201 if (numExec > maxExec) {
1202 static GLboolean reported = GL_FALSE;
1203 if (!reported) {
1204 _mesa_problem(ctx, "Infinite loop detected in fragment program");
1205 reported = GL_TRUE;
1206 }
1207 return GL_TRUE;
1208 }
1209
1210 } /* for pc */
1211
1212 return GL_TRUE;
1213 }
1214