blob: f881d477caa58e08e554cfb4c5a4540d98abbe14 [file] [log] [blame]
Brian13e3b212007-02-22 16:09:40 -07001/*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 1999-2007 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 * \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 "glheader.h"
39#include "colormac.h"
40#include "context.h"
41#include "program.h"
42#include "prog_execute.h"
43#include "prog_instruction.h"
44#include "prog_parameter.h"
45#include "prog_print.h"
46#include "slang_library_noise.h"
47
48
49/* See comments below for info about this */
50#define LAMBDA_ZERO 1
51
52/* debug predicate */
53#define DEBUG_PROG 0
54
55
Brianf183a2d2007-02-23 17:14:30 -070056/**
57 * Set x to positive or negative infinity.
58 */
59#if defined(USE_IEEE) || defined(_WIN32)
60#define SET_POS_INFINITY(x) ( *((GLuint *) (void *)&x) = 0x7F800000 )
61#define SET_NEG_INFINITY(x) ( *((GLuint *) (void *)&x) = 0xFF800000 )
62#elif defined(VMS)
63#define SET_POS_INFINITY(x) x = __MAXFLOAT
64#define SET_NEG_INFINITY(x) x = -__MAXFLOAT
65#else
66#define SET_POS_INFINITY(x) x = (GLfloat) HUGE_VAL
67#define SET_NEG_INFINITY(x) x = (GLfloat) -HUGE_VAL
68#endif
69
70#define SET_FLOAT_BITS(x, bits) ((fi_type *) (void *) &(x))->i = bits
71
72
73static const GLfloat ZeroVec[4] = { 0.0F, 0.0F, 0.0F, 0.0F };
74
75
76
Brian13e3b212007-02-22 16:09:40 -070077/**
78 * Return a pointer to the 4-element float vector specified by the given
79 * source register.
80 */
81static INLINE const GLfloat *
Brian33eac562007-02-25 18:52:41 -070082get_register_pointer(const struct prog_src_register *source,
Briane80d9012007-02-23 16:53:24 -070083 const struct gl_program_machine *machine)
Brian13e3b212007-02-22 16:09:40 -070084{
Brianf183a2d2007-02-23 17:14:30 -070085 if (source->RelAddr) {
86 const GLint reg = source->Index + machine->AddressReg[0][0];
Brian313d50e2007-02-25 19:01:16 -070087 if (source->File == PROGRAM_ENV_PARAM)
88 if (reg < 0 || reg >= MAX_PROGRAM_ENV_PARAMS)
89 return ZeroVec;
90 else
91 return machine->EnvParams[reg];
Brianf183a2d2007-02-23 17:14:30 -070092 else {
Brian313d50e2007-02-25 19:01:16 -070093 const struct gl_program_parameter_list *params;
Brian761728a2007-02-24 11:14:57 -070094 ASSERT(source->File == PROGRAM_LOCAL_PARAM ||
95 source->File == PROGRAM_STATE_VAR);
Brian313d50e2007-02-25 19:01:16 -070096 params = machine->CurProgram->Parameters;
97 if (reg < 0 || reg >= params->NumParameters)
98 return ZeroVec;
99 else
100 return params->ParameterValues[reg];
Brianf183a2d2007-02-23 17:14:30 -0700101 }
102 }
103
Brian13e3b212007-02-22 16:09:40 -0700104 switch (source->File) {
105 case PROGRAM_TEMPORARY:
106 ASSERT(source->Index < MAX_PROGRAM_TEMPS);
107 return machine->Temporaries[source->Index];
108
109 case PROGRAM_INPUT:
110 if (machine->CurProgram->Target == GL_VERTEX_PROGRAM_ARB) {
111 ASSERT(source->Index < VERT_ATTRIB_MAX);
112 return machine->VertAttribs[source->Index];
113 }
114 else {
115 ASSERT(source->Index < FRAG_ATTRIB_MAX);
116 return machine->Attribs[source->Index][machine->CurElement];
117 }
118
119 case PROGRAM_OUTPUT:
Brian292a8042007-02-24 15:49:54 -0700120 ASSERT(source->Index < MAX_PROGRAM_OUTPUTS);
Brian13e3b212007-02-22 16:09:40 -0700121 return machine->Outputs[source->Index];
122
123 case PROGRAM_LOCAL_PARAM:
124 ASSERT(source->Index < MAX_PROGRAM_LOCAL_PARAMS);
125 return machine->CurProgram->LocalParams[source->Index];
126
127 case PROGRAM_ENV_PARAM:
128 ASSERT(source->Index < MAX_PROGRAM_ENV_PARAMS);
Brian33eac562007-02-25 18:52:41 -0700129 return machine->EnvParams[source->Index];
Brian13e3b212007-02-22 16:09:40 -0700130
131 case PROGRAM_STATE_VAR:
132 /* Fallthrough */
133 case PROGRAM_CONSTANT:
134 /* Fallthrough */
135 case PROGRAM_UNIFORM:
136 /* Fallthrough */
137 case PROGRAM_NAMED_PARAM:
138 ASSERT(source->Index <
139 (GLint) machine->CurProgram->Parameters->NumParameters);
140 return machine->CurProgram->Parameters->ParameterValues[source->Index];
141
142 default:
Brian33eac562007-02-25 18:52:41 -0700143 _mesa_problem(NULL,
Brian13e3b212007-02-22 16:09:40 -0700144 "Invalid input register file %d in get_register_pointer()",
145 source->File);
146 return NULL;
147 }
148}
149
150
Brian6774f322007-02-25 18:39:46 -0700151#if FEATURE_MESA_program_debug
152static struct gl_program_machine *CurrentMachine = NULL;
153
154/**
155 * For GL_MESA_program_debug.
156 * Return current value (4*GLfloat) of a program register.
157 * Called via ctx->Driver.GetProgramRegister().
158 */
159void
160_mesa_get_program_register(GLcontext *ctx, enum register_file file,
161 GLuint index, GLfloat val[4])
162{
163 if (CurrentMachine) {
164 struct prog_src_register src;
165 const GLfloat *reg;
166 src.File = file;
167 src.Index = index;
Brian33eac562007-02-25 18:52:41 -0700168 reg = get_register_pointer(&src, CurrentMachine);
Brian6774f322007-02-25 18:39:46 -0700169 COPY_4V(val, reg);
170 }
171}
172#endif /* FEATURE_MESA_program_debug */
173
174
Brian13e3b212007-02-22 16:09:40 -0700175/**
176 * Fetch a 4-element float vector from the given source register.
177 * Apply swizzling and negating as needed.
178 */
179static void
Brian33eac562007-02-25 18:52:41 -0700180fetch_vector4(const struct prog_src_register *source,
Briane80d9012007-02-23 16:53:24 -0700181 const struct gl_program_machine *machine, GLfloat result[4])
Brian13e3b212007-02-22 16:09:40 -0700182{
Brian33eac562007-02-25 18:52:41 -0700183 const GLfloat *src = get_register_pointer(source, machine);
Brian13e3b212007-02-22 16:09:40 -0700184 ASSERT(src);
185
186 if (source->Swizzle == SWIZZLE_NOOP) {
187 /* no swizzling */
188 COPY_4V(result, src);
189 }
190 else {
191 ASSERT(GET_SWZ(source->Swizzle, 0) <= 3);
192 ASSERT(GET_SWZ(source->Swizzle, 1) <= 3);
193 ASSERT(GET_SWZ(source->Swizzle, 2) <= 3);
194 ASSERT(GET_SWZ(source->Swizzle, 3) <= 3);
195 result[0] = src[GET_SWZ(source->Swizzle, 0)];
196 result[1] = src[GET_SWZ(source->Swizzle, 1)];
197 result[2] = src[GET_SWZ(source->Swizzle, 2)];
198 result[3] = src[GET_SWZ(source->Swizzle, 3)];
199 }
200
201 if (source->NegateBase) {
202 result[0] = -result[0];
203 result[1] = -result[1];
204 result[2] = -result[2];
205 result[3] = -result[3];
206 }
207 if (source->Abs) {
208 result[0] = FABSF(result[0]);
209 result[1] = FABSF(result[1]);
210 result[2] = FABSF(result[2]);
211 result[3] = FABSF(result[3]);
212 }
213 if (source->NegateAbs) {
214 result[0] = -result[0];
215 result[1] = -result[1];
216 result[2] = -result[2];
217 result[3] = -result[3];
218 }
219}
220
221#if 0
222/**
223 * Fetch the derivative with respect to X for the given register.
224 * \return GL_TRUE if it was easily computed or GL_FALSE if we
225 * need to execute another instance of the program (ugh)!
226 */
227static GLboolean
Briane80d9012007-02-23 16:53:24 -0700228fetch_vector4_deriv(GLcontext * ctx,
229 const struct prog_src_register *source,
230 const SWspan * span,
231 char xOrY, GLint column, GLfloat result[4])
Brian13e3b212007-02-22 16:09:40 -0700232{
233 GLfloat src[4];
234
235 ASSERT(xOrY == 'X' || xOrY == 'Y');
236
237 switch (source->Index) {
238 case FRAG_ATTRIB_WPOS:
239 if (xOrY == 'X') {
240 src[0] = 1.0;
241 src[1] = 0.0;
242 src[2] = span->attrStepX[FRAG_ATTRIB_WPOS][2]
Briane80d9012007-02-23 16:53:24 -0700243 / ctx->DrawBuffer->_DepthMaxF;
Brian13e3b212007-02-22 16:09:40 -0700244 src[3] = span->attrStepX[FRAG_ATTRIB_WPOS][3];
245 }
246 else {
247 src[0] = 0.0;
248 src[1] = 1.0;
249 src[2] = span->attrStepY[FRAG_ATTRIB_WPOS][2]
Briane80d9012007-02-23 16:53:24 -0700250 / ctx->DrawBuffer->_DepthMaxF;
Brian13e3b212007-02-22 16:09:40 -0700251 src[3] = span->attrStepY[FRAG_ATTRIB_WPOS][3];
252 }
253 break;
254 case FRAG_ATTRIB_COL0:
255 case FRAG_ATTRIB_COL1:
256 if (xOrY == 'X') {
257 src[0] = span->attrStepX[source->Index][0] * (1.0F / CHAN_MAXF);
258 src[1] = span->attrStepX[source->Index][1] * (1.0F / CHAN_MAXF);
259 src[2] = span->attrStepX[source->Index][2] * (1.0F / CHAN_MAXF);
260 src[3] = span->attrStepX[source->Index][3] * (1.0F / CHAN_MAXF);
261 }
262 else {
263 src[0] = span->attrStepY[source->Index][0] * (1.0F / CHAN_MAXF);
264 src[1] = span->attrStepY[source->Index][1] * (1.0F / CHAN_MAXF);
265 src[2] = span->attrStepY[source->Index][2] * (1.0F / CHAN_MAXF);
266 src[3] = span->attrStepY[source->Index][3] * (1.0F / CHAN_MAXF);
267 }
268 break;
269 case FRAG_ATTRIB_FOGC:
270 if (xOrY == 'X') {
271 src[0] = span->attrStepX[FRAG_ATTRIB_FOGC][0] * (1.0F / CHAN_MAXF);
272 src[1] = 0.0;
273 src[2] = 0.0;
274 src[3] = 0.0;
275 }
276 else {
277 src[0] = span->attrStepY[FRAG_ATTRIB_FOGC][0] * (1.0F / CHAN_MAXF);
278 src[1] = 0.0;
279 src[2] = 0.0;
280 src[3] = 0.0;
281 }
282 break;
283 default:
284 assert(source->Index < FRAG_ATTRIB_MAX);
285 /* texcoord or varying */
286 if (xOrY == 'X') {
287 /* this is a little tricky - I think I've got it right */
288 const GLfloat invQ = 1.0f / (span->attrStart[source->Index][3]
Briane80d9012007-02-23 16:53:24 -0700289 +
290 span->attrStepX[source->Index][3] *
291 column);
Brian13e3b212007-02-22 16:09:40 -0700292 src[0] = span->attrStepX[source->Index][0] * invQ;
293 src[1] = span->attrStepX[source->Index][1] * invQ;
294 src[2] = span->attrStepX[source->Index][2] * invQ;
295 src[3] = span->attrStepX[source->Index][3] * invQ;
296 }
297 else {
298 /* Tricky, as above, but in Y direction */
299 const GLfloat invQ = 1.0f / (span->attrStart[source->Index][3]
300 + span->attrStepY[source->Index][3]);
301 src[0] = span->attrStepY[source->Index][0] * invQ;
302 src[1] = span->attrStepY[source->Index][1] * invQ;
303 src[2] = span->attrStepY[source->Index][2] * invQ;
304 src[3] = span->attrStepY[source->Index][3] * invQ;
305 }
306 break;
307 }
308
309 result[0] = src[GET_SWZ(source->Swizzle, 0)];
310 result[1] = src[GET_SWZ(source->Swizzle, 1)];
311 result[2] = src[GET_SWZ(source->Swizzle, 2)];
312 result[3] = src[GET_SWZ(source->Swizzle, 3)];
313
314 if (source->NegateBase) {
315 result[0] = -result[0];
316 result[1] = -result[1];
317 result[2] = -result[2];
318 result[3] = -result[3];
319 }
320 if (source->Abs) {
321 result[0] = FABSF(result[0]);
322 result[1] = FABSF(result[1]);
323 result[2] = FABSF(result[2]);
324 result[3] = FABSF(result[3]);
325 }
326 if (source->NegateAbs) {
327 result[0] = -result[0];
328 result[1] = -result[1];
329 result[2] = -result[2];
330 result[3] = -result[3];
331 }
332 return GL_TRUE;
333}
334#endif
335
336
337/**
338 * As above, but only return result[0] element.
339 */
340static void
Brian33eac562007-02-25 18:52:41 -0700341fetch_vector1(const struct prog_src_register *source,
Briane80d9012007-02-23 16:53:24 -0700342 const struct gl_program_machine *machine, GLfloat result[4])
Brian13e3b212007-02-22 16:09:40 -0700343{
Brian33eac562007-02-25 18:52:41 -0700344 const GLfloat *src = get_register_pointer(source, machine);
Brian13e3b212007-02-22 16:09:40 -0700345 ASSERT(src);
346
347 result[0] = src[GET_SWZ(source->Swizzle, 0)];
348
349 if (source->NegateBase) {
350 result[0] = -result[0];
351 }
352 if (source->Abs) {
353 result[0] = FABSF(result[0]);
354 }
355 if (source->NegateAbs) {
356 result[0] = -result[0];
357 }
358}
359
360
361/**
362 * Test value against zero and return GT, LT, EQ or UN if NaN.
363 */
364static INLINE GLuint
Briane80d9012007-02-23 16:53:24 -0700365generate_cc(float value)
Brian13e3b212007-02-22 16:09:40 -0700366{
367 if (value != value)
Briane80d9012007-02-23 16:53:24 -0700368 return COND_UN; /* NaN */
Brian13e3b212007-02-22 16:09:40 -0700369 if (value > 0.0F)
370 return COND_GT;
371 if (value < 0.0F)
372 return COND_LT;
373 return COND_EQ;
374}
375
376
377/**
378 * Test if the ccMaskRule is satisfied by the given condition code.
379 * Used to mask destination writes according to the current condition code.
380 */
381static INLINE GLboolean
382test_cc(GLuint condCode, GLuint ccMaskRule)
383{
384 switch (ccMaskRule) {
385 case COND_EQ: return (condCode == COND_EQ);
386 case COND_NE: return (condCode != COND_EQ);
387 case COND_LT: return (condCode == COND_LT);
388 case COND_GE: return (condCode == COND_GT || condCode == COND_EQ);
389 case COND_LE: return (condCode == COND_LT || condCode == COND_EQ);
390 case COND_GT: return (condCode == COND_GT);
391 case COND_TR: return GL_TRUE;
392 case COND_FL: return GL_FALSE;
393 default: return GL_TRUE;
394 }
395}
396
397
398/**
399 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
400 * or GL_FALSE to indicate result.
401 */
402static INLINE GLboolean
403eval_condition(const struct gl_program_machine *machine,
404 const struct prog_instruction *inst)
405{
406 const GLuint swizzle = inst->DstReg.CondSwizzle;
407 const GLuint condMask = inst->DstReg.CondMask;
408 if (test_cc(machine->CondCodes[GET_SWZ(swizzle, 0)], condMask) ||
409 test_cc(machine->CondCodes[GET_SWZ(swizzle, 1)], condMask) ||
410 test_cc(machine->CondCodes[GET_SWZ(swizzle, 2)], condMask) ||
411 test_cc(machine->CondCodes[GET_SWZ(swizzle, 3)], condMask)) {
412 return GL_TRUE;
413 }
414 else {
415 return GL_FALSE;
416 }
417}
418
419
420
421/**
422 * Store 4 floats into a register. Observe the instructions saturate and
423 * set-condition-code flags.
424 */
425static void
Briane80d9012007-02-23 16:53:24 -0700426store_vector4(const struct prog_instruction *inst,
427 struct gl_program_machine *machine, const GLfloat value[4])
Brian13e3b212007-02-22 16:09:40 -0700428{
429 const struct prog_dst_register *dest = &(inst->DstReg);
430 const GLboolean clamp = inst->SaturateMode == SATURATE_ZERO_ONE;
431 GLfloat *dstReg;
432 GLfloat dummyReg[4];
433 GLfloat clampedValue[4];
434 GLuint writeMask = dest->WriteMask;
435
436 switch (dest->File) {
Briane80d9012007-02-23 16:53:24 -0700437 case PROGRAM_OUTPUT:
Brian292a8042007-02-24 15:49:54 -0700438 ASSERT(dest->Index < MAX_PROGRAM_OUTPUTS);
Briane80d9012007-02-23 16:53:24 -0700439 dstReg = machine->Outputs[dest->Index];
440 break;
441 case PROGRAM_TEMPORARY:
Brian292a8042007-02-24 15:49:54 -0700442 ASSERT(dest->Index < MAX_PROGRAM_TEMPS);
Briane80d9012007-02-23 16:53:24 -0700443 dstReg = machine->Temporaries[dest->Index];
444 break;
445 case PROGRAM_WRITE_ONLY:
446 dstReg = dummyReg;
447 return;
448 default:
449 _mesa_problem(NULL, "bad register file in store_vector4(fp)");
450 return;
Brian13e3b212007-02-22 16:09:40 -0700451 }
452
453#if 0
454 if (value[0] > 1.0e10 ||
455 IS_INF_OR_NAN(value[0]) ||
456 IS_INF_OR_NAN(value[1]) ||
Briane80d9012007-02-23 16:53:24 -0700457 IS_INF_OR_NAN(value[2]) || IS_INF_OR_NAN(value[3]))
Brian13e3b212007-02-22 16:09:40 -0700458 printf("store %g %g %g %g\n", value[0], value[1], value[2], value[3]);
459#endif
460
461 if (clamp) {
462 clampedValue[0] = CLAMP(value[0], 0.0F, 1.0F);
463 clampedValue[1] = CLAMP(value[1], 0.0F, 1.0F);
464 clampedValue[2] = CLAMP(value[2], 0.0F, 1.0F);
465 clampedValue[3] = CLAMP(value[3], 0.0F, 1.0F);
466 value = clampedValue;
467 }
468
469 if (dest->CondMask != COND_TR) {
470 /* condition codes may turn off some writes */
471 if (writeMask & WRITEMASK_X) {
472 if (!test_cc(machine->CondCodes[GET_SWZ(dest->CondSwizzle, 0)],
473 dest->CondMask))
474 writeMask &= ~WRITEMASK_X;
475 }
476 if (writeMask & WRITEMASK_Y) {
477 if (!test_cc(machine->CondCodes[GET_SWZ(dest->CondSwizzle, 1)],
478 dest->CondMask))
479 writeMask &= ~WRITEMASK_Y;
480 }
481 if (writeMask & WRITEMASK_Z) {
482 if (!test_cc(machine->CondCodes[GET_SWZ(dest->CondSwizzle, 2)],
483 dest->CondMask))
484 writeMask &= ~WRITEMASK_Z;
485 }
486 if (writeMask & WRITEMASK_W) {
487 if (!test_cc(machine->CondCodes[GET_SWZ(dest->CondSwizzle, 3)],
488 dest->CondMask))
489 writeMask &= ~WRITEMASK_W;
490 }
491 }
492
493 if (writeMask & WRITEMASK_X)
494 dstReg[0] = value[0];
495 if (writeMask & WRITEMASK_Y)
496 dstReg[1] = value[1];
497 if (writeMask & WRITEMASK_Z)
498 dstReg[2] = value[2];
499 if (writeMask & WRITEMASK_W)
500 dstReg[3] = value[3];
501
502 if (inst->CondUpdate) {
503 if (writeMask & WRITEMASK_X)
504 machine->CondCodes[0] = generate_cc(value[0]);
505 if (writeMask & WRITEMASK_Y)
506 machine->CondCodes[1] = generate_cc(value[1]);
507 if (writeMask & WRITEMASK_Z)
508 machine->CondCodes[2] = generate_cc(value[2]);
509 if (writeMask & WRITEMASK_W)
510 machine->CondCodes[3] = generate_cc(value[3]);
511 }
512}
513
514
515#if 0
516/**
517 * Initialize a new machine state instance from an existing one, adding
518 * the partial derivatives onto the input registers.
519 * Used to implement DDX and DDY instructions in non-trivial cases.
520 */
521static void
Briane80d9012007-02-23 16:53:24 -0700522init_machine_deriv(GLcontext * ctx,
523 const struct gl_program_machine *machine,
524 const struct gl_fragment_program *program,
525 const SWspan * span, char xOrY,
526 struct gl_program_machine *dMachine)
Brian13e3b212007-02-22 16:09:40 -0700527{
528 GLuint attr;
529
530 ASSERT(xOrY == 'X' || xOrY == 'Y');
531
532 /* copy existing machine */
533 _mesa_memcpy(dMachine, machine, sizeof(struct gl_program_machine));
534
535 if (program->Base.Target == GL_FRAGMENT_PROGRAM_NV) {
536 /* XXX also need to do this when using valgrind */
537 /* Clear temporary registers (undefined for ARB_f_p) */
Briane80d9012007-02-23 16:53:24 -0700538 _mesa_bzero((void *) machine->Temporaries,
539 MAX_PROGRAM_TEMPS * 4 * sizeof(GLfloat));
Brian13e3b212007-02-22 16:09:40 -0700540 }
541
542 /* Add derivatives */
543 if (program->Base.InputsRead & FRAG_BIT_WPOS) {
544 GLfloat *wpos = machine->Attribs[FRAG_ATTRIB_WPOS][machine->CurElement];
545 if (xOrY == 'X') {
546 wpos[0] += 1.0F;
547 wpos[1] += 0.0F;
548 wpos[2] += span->attrStepX[FRAG_ATTRIB_WPOS][2];
549 wpos[3] += span->attrStepX[FRAG_ATTRIB_WPOS][3];
550 }
551 else {
552 wpos[0] += 0.0F;
553 wpos[1] += 1.0F;
554 wpos[2] += span->attrStepY[FRAG_ATTRIB_WPOS][2];
555 wpos[3] += span->attrStepY[FRAG_ATTRIB_WPOS][3];
556 }
557 }
558
559 /* primary, secondary colors */
560 for (attr = FRAG_ATTRIB_COL0; attr <= FRAG_ATTRIB_COL1; attr++) {
561 if (program->Base.InputsRead & (1 << attr)) {
562 GLfloat *col = machine->Attribs[attr][machine->CurElement];
563 if (xOrY == 'X') {
564 col[0] += span->attrStepX[attr][0] * (1.0F / CHAN_MAXF);
565 col[1] += span->attrStepX[attr][1] * (1.0F / CHAN_MAXF);
566 col[2] += span->attrStepX[attr][2] * (1.0F / CHAN_MAXF);
567 col[3] += span->attrStepX[attr][3] * (1.0F / CHAN_MAXF);
568 }
569 else {
570 col[0] += span->attrStepY[attr][0] * (1.0F / CHAN_MAXF);
571 col[1] += span->attrStepY[attr][1] * (1.0F / CHAN_MAXF);
572 col[2] += span->attrStepY[attr][2] * (1.0F / CHAN_MAXF);
573 col[3] += span->attrStepY[attr][3] * (1.0F / CHAN_MAXF);
574 }
575 }
576 }
577 if (program->Base.InputsRead & FRAG_BIT_FOGC) {
578 GLfloat *fogc = machine->Attribs[FRAG_ATTRIB_FOGC][machine->CurElement];
579 if (xOrY == 'X') {
580 fogc[0] += span->attrStepX[FRAG_ATTRIB_FOGC][0];
581 }
582 else {
583 fogc[0] += span->attrStepY[FRAG_ATTRIB_FOGC][0];
584 }
585 }
586 /* texcoord and varying vars */
587 for (attr = FRAG_ATTRIB_TEX0; attr < FRAG_ATTRIB_MAX; attr++) {
588 if (program->Base.InputsRead & (1 << attr)) {
589 GLfloat *val = machine->Attribs[attr][machine->CurElement];
590 /* XXX perspective-correct interpolation */
591 if (xOrY == 'X') {
592 val[0] += span->attrStepX[attr][0];
593 val[1] += span->attrStepX[attr][1];
594 val[2] += span->attrStepX[attr][2];
595 val[3] += span->attrStepX[attr][3];
596 }
597 else {
598 val[0] += span->attrStepY[attr][0];
599 val[1] += span->attrStepY[attr][1];
600 val[2] += span->attrStepY[attr][2];
601 val[3] += span->attrStepY[attr][3];
602 }
603 }
604 }
605
606 /* init condition codes */
607 dMachine->CondCodes[0] = COND_EQ;
608 dMachine->CondCodes[1] = COND_EQ;
609 dMachine->CondCodes[2] = COND_EQ;
610 dMachine->CondCodes[3] = COND_EQ;
611}
612#endif
613
614
615/**
616 * Execute the given vertex/fragment program.
617 *
Brian3c1c9992007-02-25 19:11:44 -0700618 * \param ctx rendering context
619 * \param program the program to execute
620 * \param machine machine state (must be initialized)
Brian13e3b212007-02-22 16:09:40 -0700621 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
622 */
623GLboolean
Briane80d9012007-02-23 16:53:24 -0700624_mesa_execute_program(GLcontext * ctx,
Brian8b34b7d2007-02-25 18:26:50 -0700625 const struct gl_program *program,
Brian085d7d52007-02-25 18:23:37 -0700626 struct gl_program_machine *machine)
Brian13e3b212007-02-22 16:09:40 -0700627{
Brian8b34b7d2007-02-25 18:26:50 -0700628 const GLuint numInst = program->NumInstructions;
Briancfd00112007-02-25 18:30:45 -0700629 const GLuint maxExec = 10000;
630 GLint pc, numExec = 0;
Brian13e3b212007-02-22 16:09:40 -0700631
632 machine->CurProgram = program;
633
634 if (DEBUG_PROG) {
635 printf("execute program %u --------------------\n", program->Id);
636 }
637
638#if FEATURE_MESA_program_debug
639 CurrentMachine = machine;
640#endif
641
Brian33eac562007-02-25 18:52:41 -0700642 if (program->Target == GL_VERTEX_PROGRAM_ARB) {
643 machine->EnvParams = ctx->VertexProgram.Parameters;
644 }
645 else {
646 machine->EnvParams = ctx->FragmentProgram.Parameters;
647 }
648
Brian8b34b7d2007-02-25 18:26:50 -0700649 for (pc = 0; pc < numInst; pc++) {
Brian13e3b212007-02-22 16:09:40 -0700650 const struct prog_instruction *inst = program->Instructions + pc;
651
652#if FEATURE_MESA_program_debug
653 if (ctx->FragmentProgram.CallbackEnabled &&
654 ctx->FragmentProgram.Callback) {
655 ctx->FragmentProgram.CurrentPosition = inst->StringPos;
656 ctx->FragmentProgram.Callback(program->Target,
657 ctx->FragmentProgram.CallbackData);
658 }
659#endif
660
661 if (DEBUG_PROG) {
662 _mesa_print_instruction(inst);
663 }
664
665 switch (inst->Opcode) {
Briane80d9012007-02-23 16:53:24 -0700666 case OPCODE_ABS:
667 {
668 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700669 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700670 result[0] = FABSF(a[0]);
671 result[1] = FABSF(a[1]);
672 result[2] = FABSF(a[2]);
673 result[3] = FABSF(a[3]);
674 store_vector4(inst, machine, result);
675 }
676 break;
677 case OPCODE_ADD:
678 {
679 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700680 fetch_vector4(&inst->SrcReg[0], machine, a);
681 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700682 result[0] = a[0] + b[0];
683 result[1] = a[1] + b[1];
684 result[2] = a[2] + b[2];
685 result[3] = a[3] + b[3];
686 store_vector4(inst, machine, result);
687 if (DEBUG_PROG) {
688 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
689 result[0], result[1], result[2], result[3],
690 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -0700691 }
Briane80d9012007-02-23 16:53:24 -0700692 }
693 break;
Brianf183a2d2007-02-23 17:14:30 -0700694 case OPCODE_ARL:
695 {
696 GLfloat t[4];
Brian33eac562007-02-25 18:52:41 -0700697 fetch_vector4(&inst->SrcReg[0], machine, t);
Brianf183a2d2007-02-23 17:14:30 -0700698 machine->AddressReg[0][0] = (GLint) FLOORF(t[0]);
699 }
700 break;
Briane80d9012007-02-23 16:53:24 -0700701 case OPCODE_BGNLOOP:
702 /* no-op */
703 break;
704 case OPCODE_ENDLOOP:
705 /* subtract 1 here since pc is incremented by for(pc) loop */
706 pc = inst->BranchTarget - 1; /* go to matching BNGLOOP */
707 break;
708 case OPCODE_BGNSUB: /* begin subroutine */
709 break;
710 case OPCODE_ENDSUB: /* end subroutine */
711 break;
712 case OPCODE_BRA: /* branch (conditional) */
713 /* fall-through */
714 case OPCODE_BRK: /* break out of loop (conditional) */
715 /* fall-through */
716 case OPCODE_CONT: /* continue loop (conditional) */
717 if (eval_condition(machine, inst)) {
718 /* take branch */
719 /* Subtract 1 here since we'll do pc++ at end of for-loop */
720 pc = inst->BranchTarget - 1;
721 }
722 break;
Brian63556fa2007-03-23 14:47:46 -0600723 case OPCODE_BRK0: /* Break if zero */
724 /* fall-through */
725 case OPCODE_CONT0: /* Continue if zero */
726 {
727 GLfloat a[4];
728 fetch_vector1(&inst->SrcReg[0], machine, a);
729 if (a[0] == 0.0) {
730 /* take branch */
731 /* Subtract 1 here since we'll do pc++ at end of for-loop */
732 pc = inst->BranchTarget - 1;
733 }
734 }
735 break;
736 case OPCODE_BRK1: /* Break if non-zero */
737 /* fall-through */
738 case OPCODE_CONT1: /* Continue if non-zero */
739 {
740 GLfloat a[4];
741 fetch_vector1(&inst->SrcReg[0], machine, a);
742 if (a[0] != 0.0) {
743 /* take branch */
744 /* Subtract 1 here since we'll do pc++ at end of for-loop */
745 pc = inst->BranchTarget - 1;
746 }
747 }
748 break;
Briane80d9012007-02-23 16:53:24 -0700749 case OPCODE_CAL: /* Call subroutine (conditional) */
750 if (eval_condition(machine, inst)) {
751 /* call the subroutine */
752 if (machine->StackDepth >= MAX_PROGRAM_CALL_DEPTH) {
753 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
Brian13e3b212007-02-22 16:09:40 -0700754 }
Briane80d9012007-02-23 16:53:24 -0700755 machine->CallStack[machine->StackDepth++] = pc + 1;
756 pc = inst->BranchTarget; /* XXX - 1 ??? */
757 }
758 break;
759 case OPCODE_CMP:
760 {
761 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700762 fetch_vector4(&inst->SrcReg[0], machine, a);
763 fetch_vector4(&inst->SrcReg[1], machine, b);
764 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -0700765 result[0] = a[0] < 0.0F ? b[0] : c[0];
766 result[1] = a[1] < 0.0F ? b[1] : c[1];
767 result[2] = a[2] < 0.0F ? b[2] : c[2];
768 result[3] = a[3] < 0.0F ? b[3] : c[3];
769 store_vector4(inst, machine, result);
770 }
771 break;
772 case OPCODE_COS:
773 {
774 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700775 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700776 result[0] = result[1] = result[2] = result[3]
777 = (GLfloat) _mesa_cos(a[0]);
778 store_vector4(inst, machine, result);
779 }
780 break;
781 case OPCODE_DDX: /* Partial derivative with respect to X */
782 {
Brian13e3b212007-02-22 16:09:40 -0700783#if 0
Briane80d9012007-02-23 16:53:24 -0700784 GLfloat a[4], aNext[4], result[4];
785 struct gl_program_machine dMachine;
786 if (!fetch_vector4_deriv(ctx, &inst->SrcReg[0], span, 'X',
787 column, result)) {
788 /* This is tricky. Make a copy of the current machine state,
789 * increment the input registers by the dx or dy partial
790 * derivatives, then re-execute the program up to the
791 * preceeding instruction, then fetch the source register.
792 * Finally, find the difference in the register values for
793 * the original and derivative runs.
794 */
Brian33eac562007-02-25 18:52:41 -0700795 fetch_vector4(&inst->SrcReg[0], machine, program, a);
Briane80d9012007-02-23 16:53:24 -0700796 init_machine_deriv(ctx, machine, program, span,
797 'X', &dMachine);
798 execute_program(ctx, program, pc, &dMachine, span, column);
Brian33eac562007-02-25 18:52:41 -0700799 fetch_vector4(&inst->SrcReg[0], &dMachine, program,
Briane80d9012007-02-23 16:53:24 -0700800 aNext);
801 result[0] = aNext[0] - a[0];
802 result[1] = aNext[1] - a[1];
803 result[2] = aNext[2] - a[2];
804 result[3] = aNext[3] - a[3];
Brian13e3b212007-02-22 16:09:40 -0700805 }
Briane80d9012007-02-23 16:53:24 -0700806 store_vector4(inst, machine, result);
807#else
Brianf183a2d2007-02-23 17:14:30 -0700808 store_vector4(inst, machine, ZeroVec);
Briane80d9012007-02-23 16:53:24 -0700809#endif
810 }
811 break;
812 case OPCODE_DDY: /* Partial derivative with respect to Y */
813 {
Brian13e3b212007-02-22 16:09:40 -0700814#if 0
Briane80d9012007-02-23 16:53:24 -0700815 GLfloat a[4], aNext[4], result[4];
816 struct gl_program_machine dMachine;
817 if (!fetch_vector4_deriv(ctx, &inst->SrcReg[0], span, 'Y',
818 column, result)) {
819 init_machine_deriv(ctx, machine, program, span,
820 'Y', &dMachine);
Brian33eac562007-02-25 18:52:41 -0700821 fetch_vector4(&inst->SrcReg[0], machine, program, a);
Briane80d9012007-02-23 16:53:24 -0700822 execute_program(ctx, program, pc, &dMachine, span, column);
Brian33eac562007-02-25 18:52:41 -0700823 fetch_vector4(&inst->SrcReg[0], &dMachine, program,
Briane80d9012007-02-23 16:53:24 -0700824 aNext);
825 result[0] = aNext[0] - a[0];
826 result[1] = aNext[1] - a[1];
827 result[2] = aNext[2] - a[2];
828 result[3] = aNext[3] - a[3];
829 }
830 store_vector4(inst, machine, result);
Brian13e3b212007-02-22 16:09:40 -0700831#else
Brianf183a2d2007-02-23 17:14:30 -0700832 store_vector4(inst, machine, ZeroVec);
Brian13e3b212007-02-22 16:09:40 -0700833#endif
Briane80d9012007-02-23 16:53:24 -0700834 }
835 break;
836 case OPCODE_DP3:
837 {
838 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700839 fetch_vector4(&inst->SrcReg[0], machine, a);
840 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700841 result[0] = result[1] = result[2] = result[3] = DOT3(a, b);
842 store_vector4(inst, machine, result);
843 if (DEBUG_PROG) {
844 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
845 result[0], a[0], a[1], a[2], b[0], b[1], b[2]);
Brian13e3b212007-02-22 16:09:40 -0700846 }
Briane80d9012007-02-23 16:53:24 -0700847 }
848 break;
849 case OPCODE_DP4:
850 {
851 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700852 fetch_vector4(&inst->SrcReg[0], machine, a);
853 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700854 result[0] = result[1] = result[2] = result[3] = DOT4(a, b);
855 store_vector4(inst, machine, result);
856 if (DEBUG_PROG) {
857 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
858 result[0], a[0], a[1], a[2], a[3],
859 b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -0700860 }
Briane80d9012007-02-23 16:53:24 -0700861 }
862 break;
863 case OPCODE_DPH:
864 {
865 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700866 fetch_vector4(&inst->SrcReg[0], machine, a);
867 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700868 result[0] = result[1] = result[2] = result[3] =
869 a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + b[3];
870 store_vector4(inst, machine, result);
871 }
872 break;
873 case OPCODE_DST: /* Distance vector */
874 {
875 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700876 fetch_vector4(&inst->SrcReg[0], machine, a);
877 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700878 result[0] = 1.0F;
879 result[1] = a[1] * b[1];
880 result[2] = a[2];
881 result[3] = b[3];
882 store_vector4(inst, machine, result);
883 }
884 break;
Brianf183a2d2007-02-23 17:14:30 -0700885 case OPCODE_EXP:
Brianf183a2d2007-02-23 17:14:30 -0700886 {
887 GLfloat t[4], q[4], floor_t0;
Brian33eac562007-02-25 18:52:41 -0700888 fetch_vector1(&inst->SrcReg[0], machine, t);
Brianf183a2d2007-02-23 17:14:30 -0700889 floor_t0 = FLOORF(t[0]);
890 if (floor_t0 > FLT_MAX_EXP) {
891 SET_POS_INFINITY(q[0]);
892 SET_POS_INFINITY(q[2]);
893 }
894 else if (floor_t0 < FLT_MIN_EXP) {
895 q[0] = 0.0F;
896 q[2] = 0.0F;
897 }
898 else {
Brian761728a2007-02-24 11:14:57 -0700899 q[0] = LDEXPF(1.0, (int) floor_t0);
900 /* Note: GL_NV_vertex_program expects
901 * result.z = result.x * APPX(result.y)
902 * We do what the ARB extension says.
903 */
904 q[2] = pow(2.0, t[0]);
Brianf183a2d2007-02-23 17:14:30 -0700905 }
906 q[1] = t[0] - floor_t0;
907 q[3] = 1.0F;
908 store_vector4( inst, machine, q );
909 }
910 break;
Briane80d9012007-02-23 16:53:24 -0700911 case OPCODE_EX2: /* Exponential base 2 */
912 {
913 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700914 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700915 result[0] = result[1] = result[2] = result[3] =
916 (GLfloat) _mesa_pow(2.0, a[0]);
917 store_vector4(inst, machine, result);
918 }
919 break;
920 case OPCODE_FLR:
921 {
922 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700923 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700924 result[0] = FLOORF(a[0]);
925 result[1] = FLOORF(a[1]);
926 result[2] = FLOORF(a[2]);
927 result[3] = FLOORF(a[3]);
928 store_vector4(inst, machine, result);
929 }
930 break;
931 case OPCODE_FRC:
932 {
933 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700934 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700935 result[0] = a[0] - FLOORF(a[0]);
936 result[1] = a[1] - FLOORF(a[1]);
937 result[2] = a[2] - FLOORF(a[2]);
938 result[3] = a[3] - FLOORF(a[3]);
939 store_vector4(inst, machine, result);
940 }
941 break;
942 case OPCODE_IF:
Brian63556fa2007-03-23 14:47:46 -0600943 {
944 GLboolean cond;
945 /* eval condition */
946 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
947 GLfloat a[4];
948 fetch_vector1(&inst->SrcReg[0], machine, a);
949 cond = (a[0] != 0.0);
950 }
951 else {
952 cond = eval_condition(machine, inst);
953 }
954 /* do if/else */
955 if (cond) {
956 /* do if-clause (just continue execution) */
957 }
958 else {
959 /* go to the instruction after ELSE or ENDIF */
960 assert(inst->BranchTarget >= 0);
961 pc = inst->BranchTarget - 1;
962 }
Briane80d9012007-02-23 16:53:24 -0700963 }
964 break;
965 case OPCODE_ELSE:
966 /* goto ENDIF */
967 assert(inst->BranchTarget >= 0);
968 pc = inst->BranchTarget - 1;
969 break;
970 case OPCODE_ENDIF:
971 /* nothing */
972 break;
973 case OPCODE_INT: /* float to int */
974 {
975 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700976 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700977 result[0] = (GLfloat) (GLint) a[0];
978 result[1] = (GLfloat) (GLint) a[1];
979 result[2] = (GLfloat) (GLint) a[2];
980 result[3] = (GLfloat) (GLint) a[3];
981 store_vector4(inst, machine, result);
982 }
983 break;
984 case OPCODE_KIL_NV: /* NV_f_p only (conditional) */
985 if (eval_condition(machine, inst)) {
986 return GL_FALSE;
987 }
988 break;
989 case OPCODE_KIL: /* ARB_f_p only */
990 {
991 GLfloat a[4];
Brian33eac562007-02-25 18:52:41 -0700992 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700993 if (a[0] < 0.0F || a[1] < 0.0F || a[2] < 0.0F || a[3] < 0.0F) {
Brian13e3b212007-02-22 16:09:40 -0700994 return GL_FALSE;
995 }
Briane80d9012007-02-23 16:53:24 -0700996 }
997 break;
998 case OPCODE_LG2: /* log base 2 */
999 {
1000 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001001 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001002 result[0] = result[1] = result[2] = result[3] = LOG2(a[0]);
1003 store_vector4(inst, machine, result);
1004 }
1005 break;
1006 case OPCODE_LIT:
1007 {
1008 const GLfloat epsilon = 1.0F / 256.0F; /* from NV VP spec */
1009 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001010 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001011 a[0] = MAX2(a[0], 0.0F);
1012 a[1] = MAX2(a[1], 0.0F);
1013 /* XXX ARB version clamps a[3], NV version doesn't */
1014 a[3] = CLAMP(a[3], -(128.0F - epsilon), (128.0F - epsilon));
1015 result[0] = 1.0F;
1016 result[1] = a[0];
1017 /* XXX we could probably just use pow() here */
1018 if (a[0] > 0.0F) {
1019 if (a[1] == 0.0 && a[3] == 0.0)
1020 result[2] = 1.0;
1021 else
1022 result[2] = EXPF(a[3] * LOGF(a[1]));
Brian13e3b212007-02-22 16:09:40 -07001023 }
Briane80d9012007-02-23 16:53:24 -07001024 else {
1025 result[2] = 0.0;
Brian13e3b212007-02-22 16:09:40 -07001026 }
Briane80d9012007-02-23 16:53:24 -07001027 result[3] = 1.0F;
1028 store_vector4(inst, machine, result);
1029 if (DEBUG_PROG) {
1030 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
1031 result[0], result[1], result[2], result[3],
1032 a[0], a[1], a[2], a[3]);
Brian13e3b212007-02-22 16:09:40 -07001033 }
Briane80d9012007-02-23 16:53:24 -07001034 }
1035 break;
Brianf183a2d2007-02-23 17:14:30 -07001036 case OPCODE_LOG:
1037 {
1038 GLfloat t[4], q[4], abs_t0;
Brian33eac562007-02-25 18:52:41 -07001039 fetch_vector1(&inst->SrcReg[0], machine, t);
Brianf183a2d2007-02-23 17:14:30 -07001040 abs_t0 = FABSF(t[0]);
1041 if (abs_t0 != 0.0F) {
1042 /* Since we really can't handle infinite values on VMS
1043 * like other OSes we'll use __MAXFLOAT to represent
1044 * infinity. This may need some tweaking.
1045 */
1046#ifdef VMS
1047 if (abs_t0 == __MAXFLOAT)
1048#else
1049 if (IS_INF_OR_NAN(abs_t0))
1050#endif
1051 {
1052 SET_POS_INFINITY(q[0]);
1053 q[1] = 1.0F;
1054 SET_POS_INFINITY(q[2]);
1055 }
1056 else {
1057 int exponent;
1058 GLfloat mantissa = FREXPF(t[0], &exponent);
1059 q[0] = (GLfloat) (exponent - 1);
1060 q[1] = (GLfloat) (2.0 * mantissa); /* map [.5, 1) -> [1, 2) */
1061 q[2] = (GLfloat) (q[0] + LOG2(q[1]));
1062 }
1063 }
1064 else {
1065 SET_NEG_INFINITY(q[0]);
1066 q[1] = 1.0F;
1067 SET_NEG_INFINITY(q[2]);
1068 }
1069 q[3] = 1.0;
1070 store_vector4(inst, machine, q);
1071 }
1072 break;
Briane80d9012007-02-23 16:53:24 -07001073 case OPCODE_LRP:
1074 {
1075 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001076 fetch_vector4(&inst->SrcReg[0], machine, a);
1077 fetch_vector4(&inst->SrcReg[1], machine, b);
1078 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -07001079 result[0] = a[0] * b[0] + (1.0F - a[0]) * c[0];
1080 result[1] = a[1] * b[1] + (1.0F - a[1]) * c[1];
1081 result[2] = a[2] * b[2] + (1.0F - a[2]) * c[2];
1082 result[3] = a[3] * b[3] + (1.0F - a[3]) * c[3];
1083 store_vector4(inst, machine, result);
1084 if (DEBUG_PROG) {
1085 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
1086 "(%g %g %g %g), (%g %g %g %g)\n",
1087 result[0], result[1], result[2], result[3],
1088 a[0], a[1], a[2], a[3],
1089 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
Brian13e3b212007-02-22 16:09:40 -07001090 }
Briane80d9012007-02-23 16:53:24 -07001091 }
1092 break;
1093 case OPCODE_MAD:
1094 {
1095 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001096 fetch_vector4(&inst->SrcReg[0], machine, a);
1097 fetch_vector4(&inst->SrcReg[1], machine, b);
1098 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -07001099 result[0] = a[0] * b[0] + c[0];
1100 result[1] = a[1] * b[1] + c[1];
1101 result[2] = a[2] * b[2] + c[2];
1102 result[3] = a[3] * b[3] + c[3];
1103 store_vector4(inst, machine, result);
1104 if (DEBUG_PROG) {
1105 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
1106 "(%g %g %g %g) + (%g %g %g %g)\n",
1107 result[0], result[1], result[2], result[3],
1108 a[0], a[1], a[2], a[3],
1109 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
Brian13e3b212007-02-22 16:09:40 -07001110 }
Briane80d9012007-02-23 16:53:24 -07001111 }
1112 break;
1113 case OPCODE_MAX:
1114 {
1115 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001116 fetch_vector4(&inst->SrcReg[0], machine, a);
1117 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001118 result[0] = MAX2(a[0], b[0]);
1119 result[1] = MAX2(a[1], b[1]);
1120 result[2] = MAX2(a[2], b[2]);
1121 result[3] = MAX2(a[3], b[3]);
1122 store_vector4(inst, machine, result);
1123 if (DEBUG_PROG) {
1124 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
1125 result[0], result[1], result[2], result[3],
1126 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001127 }
Briane80d9012007-02-23 16:53:24 -07001128 }
1129 break;
1130 case OPCODE_MIN:
1131 {
1132 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001133 fetch_vector4(&inst->SrcReg[0], machine, a);
1134 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001135 result[0] = MIN2(a[0], b[0]);
1136 result[1] = MIN2(a[1], b[1]);
1137 result[2] = MIN2(a[2], b[2]);
1138 result[3] = MIN2(a[3], b[3]);
1139 store_vector4(inst, machine, result);
1140 }
1141 break;
1142 case OPCODE_MOV:
1143 {
1144 GLfloat result[4];
Brian33eac562007-02-25 18:52:41 -07001145 fetch_vector4(&inst->SrcReg[0], machine, result);
Briane80d9012007-02-23 16:53:24 -07001146 store_vector4(inst, machine, result);
1147 if (DEBUG_PROG) {
1148 printf("MOV (%g %g %g %g)\n",
1149 result[0], result[1], result[2], result[3]);
Brian13e3b212007-02-22 16:09:40 -07001150 }
Briane80d9012007-02-23 16:53:24 -07001151 }
1152 break;
1153 case OPCODE_MUL:
1154 {
1155 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001156 fetch_vector4(&inst->SrcReg[0], machine, a);
1157 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001158 result[0] = a[0] * b[0];
1159 result[1] = a[1] * b[1];
1160 result[2] = a[2] * b[2];
1161 result[3] = a[3] * b[3];
1162 store_vector4(inst, machine, result);
1163 if (DEBUG_PROG) {
1164 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1165 result[0], result[1], result[2], result[3],
1166 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001167 }
Briane80d9012007-02-23 16:53:24 -07001168 }
1169 break;
1170 case OPCODE_NOISE1:
1171 {
1172 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001173 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001174 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001175 result[1] =
Briane80d9012007-02-23 16:53:24 -07001176 result[2] = result[3] = _slang_library_noise1(a[0]);
1177 store_vector4(inst, machine, result);
1178 }
1179 break;
1180 case OPCODE_NOISE2:
1181 {
1182 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001183 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001184 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001185 result[1] =
Briane80d9012007-02-23 16:53:24 -07001186 result[2] = result[3] = _slang_library_noise2(a[0], a[1]);
1187 store_vector4(inst, machine, result);
1188 }
1189 break;
1190 case OPCODE_NOISE3:
1191 {
1192 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001193 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001194 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001195 result[1] =
1196 result[2] =
1197 result[3] = _slang_library_noise3(a[0], a[1], a[2]);
Briane80d9012007-02-23 16:53:24 -07001198 store_vector4(inst, machine, result);
1199 }
1200 break;
1201 case OPCODE_NOISE4:
1202 {
1203 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001204 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001205 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001206 result[1] =
1207 result[2] =
1208 result[3] = _slang_library_noise4(a[0], a[1], a[2], a[3]);
Briane80d9012007-02-23 16:53:24 -07001209 store_vector4(inst, machine, result);
1210 }
1211 break;
1212 case OPCODE_NOP:
1213 break;
1214 case OPCODE_PK2H: /* pack two 16-bit floats in one 32-bit float */
1215 {
1216 GLfloat a[4], result[4];
1217 GLhalfNV hx, hy;
1218 GLuint *rawResult = (GLuint *) result;
1219 GLuint twoHalves;
Brian33eac562007-02-25 18:52:41 -07001220 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001221 hx = _mesa_float_to_half(a[0]);
1222 hy = _mesa_float_to_half(a[1]);
1223 twoHalves = hx | (hy << 16);
1224 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
1225 = twoHalves;
1226 store_vector4(inst, machine, result);
1227 }
1228 break;
1229 case OPCODE_PK2US: /* pack two GLushorts into one 32-bit float */
1230 {
1231 GLfloat a[4], result[4];
1232 GLuint usx, usy, *rawResult = (GLuint *) result;
Brian33eac562007-02-25 18:52:41 -07001233 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001234 a[0] = CLAMP(a[0], 0.0F, 1.0F);
1235 a[1] = CLAMP(a[1], 0.0F, 1.0F);
1236 usx = IROUND(a[0] * 65535.0F);
1237 usy = IROUND(a[1] * 65535.0F);
1238 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
1239 = usx | (usy << 16);
1240 store_vector4(inst, machine, result);
1241 }
1242 break;
1243 case OPCODE_PK4B: /* pack four GLbytes into one 32-bit float */
1244 {
1245 GLfloat a[4], result[4];
1246 GLuint ubx, uby, ubz, ubw, *rawResult = (GLuint *) result;
Brian33eac562007-02-25 18:52:41 -07001247 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001248 a[0] = CLAMP(a[0], -128.0F / 127.0F, 1.0F);
1249 a[1] = CLAMP(a[1], -128.0F / 127.0F, 1.0F);
1250 a[2] = CLAMP(a[2], -128.0F / 127.0F, 1.0F);
1251 a[3] = CLAMP(a[3], -128.0F / 127.0F, 1.0F);
1252 ubx = IROUND(127.0F * a[0] + 128.0F);
1253 uby = IROUND(127.0F * a[1] + 128.0F);
1254 ubz = IROUND(127.0F * a[2] + 128.0F);
1255 ubw = IROUND(127.0F * a[3] + 128.0F);
1256 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
1257 = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
1258 store_vector4(inst, machine, result);
1259 }
1260 break;
1261 case OPCODE_PK4UB: /* pack four GLubytes into one 32-bit float */
1262 {
1263 GLfloat a[4], result[4];
1264 GLuint ubx, uby, ubz, ubw, *rawResult = (GLuint *) result;
Brian33eac562007-02-25 18:52:41 -07001265 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001266 a[0] = CLAMP(a[0], 0.0F, 1.0F);
1267 a[1] = CLAMP(a[1], 0.0F, 1.0F);
1268 a[2] = CLAMP(a[2], 0.0F, 1.0F);
1269 a[3] = CLAMP(a[3], 0.0F, 1.0F);
1270 ubx = IROUND(255.0F * a[0]);
1271 uby = IROUND(255.0F * a[1]);
1272 ubz = IROUND(255.0F * a[2]);
1273 ubw = IROUND(255.0F * a[3]);
1274 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
1275 = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
1276 store_vector4(inst, machine, result);
1277 }
1278 break;
1279 case OPCODE_POW:
1280 {
1281 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001282 fetch_vector1(&inst->SrcReg[0], machine, a);
1283 fetch_vector1(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001284 result[0] = result[1] = result[2] = result[3]
1285 = (GLfloat) _mesa_pow(a[0], b[0]);
1286 store_vector4(inst, machine, result);
1287 }
1288 break;
1289 case OPCODE_RCP:
1290 {
1291 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001292 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001293 if (DEBUG_PROG) {
1294 if (a[0] == 0)
1295 printf("RCP(0)\n");
1296 else if (IS_INF_OR_NAN(a[0]))
1297 printf("RCP(inf)\n");
Brian13e3b212007-02-22 16:09:40 -07001298 }
Briane80d9012007-02-23 16:53:24 -07001299 result[0] = result[1] = result[2] = result[3] = 1.0F / a[0];
1300 store_vector4(inst, machine, result);
1301 }
1302 break;
1303 case OPCODE_RET: /* return from subroutine (conditional) */
1304 if (eval_condition(machine, inst)) {
1305 if (machine->StackDepth == 0) {
1306 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
Brian13e3b212007-02-22 16:09:40 -07001307 }
Briane80d9012007-02-23 16:53:24 -07001308 pc = machine->CallStack[--machine->StackDepth];
1309 }
1310 break;
1311 case OPCODE_RFL: /* reflection vector */
1312 {
1313 GLfloat axis[4], dir[4], result[4], tmpX, tmpW;
Brian33eac562007-02-25 18:52:41 -07001314 fetch_vector4(&inst->SrcReg[0], machine, axis);
1315 fetch_vector4(&inst->SrcReg[1], machine, dir);
Briane80d9012007-02-23 16:53:24 -07001316 tmpW = DOT3(axis, axis);
1317 tmpX = (2.0F * DOT3(axis, dir)) / tmpW;
1318 result[0] = tmpX * axis[0] - dir[0];
1319 result[1] = tmpX * axis[1] - dir[1];
1320 result[2] = tmpX * axis[2] - dir[2];
1321 /* result[3] is never written! XXX enforce in parser! */
1322 store_vector4(inst, machine, result);
1323 }
1324 break;
1325 case OPCODE_RSQ: /* 1 / sqrt() */
1326 {
1327 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001328 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001329 a[0] = FABSF(a[0]);
1330 result[0] = result[1] = result[2] = result[3] = INV_SQRTF(a[0]);
1331 store_vector4(inst, machine, result);
1332 if (DEBUG_PROG) {
1333 printf("RSQ %g = 1/sqrt(|%g|)\n", result[0], a[0]);
Brian13e3b212007-02-22 16:09:40 -07001334 }
Briane80d9012007-02-23 16:53:24 -07001335 }
1336 break;
1337 case OPCODE_SCS: /* sine and cos */
1338 {
1339 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001340 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001341 result[0] = (GLfloat) _mesa_cos(a[0]);
1342 result[1] = (GLfloat) _mesa_sin(a[0]);
1343 result[2] = 0.0; /* undefined! */
1344 result[3] = 0.0; /* undefined! */
1345 store_vector4(inst, machine, result);
1346 }
1347 break;
1348 case OPCODE_SEQ: /* set on equal */
1349 {
1350 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001351 fetch_vector4(&inst->SrcReg[0], machine, a);
1352 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001353 result[0] = (a[0] == b[0]) ? 1.0F : 0.0F;
1354 result[1] = (a[1] == b[1]) ? 1.0F : 0.0F;
1355 result[2] = (a[2] == b[2]) ? 1.0F : 0.0F;
1356 result[3] = (a[3] == b[3]) ? 1.0F : 0.0F;
1357 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001358 if (DEBUG_PROG) {
1359 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1360 result[0], result[1], result[2], result[3],
1361 a[0], a[1], a[2], a[3],
1362 b[0], b[1], b[2], b[3]);
1363 }
Briane80d9012007-02-23 16:53:24 -07001364 }
1365 break;
1366 case OPCODE_SFL: /* set false, operands ignored */
1367 {
1368 static const GLfloat result[4] = { 0.0F, 0.0F, 0.0F, 0.0F };
1369 store_vector4(inst, machine, result);
1370 }
1371 break;
1372 case OPCODE_SGE: /* set on greater or equal */
1373 {
1374 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001375 fetch_vector4(&inst->SrcReg[0], machine, a);
1376 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001377 result[0] = (a[0] >= b[0]) ? 1.0F : 0.0F;
1378 result[1] = (a[1] >= b[1]) ? 1.0F : 0.0F;
1379 result[2] = (a[2] >= b[2]) ? 1.0F : 0.0F;
1380 result[3] = (a[3] >= b[3]) ? 1.0F : 0.0F;
1381 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001382 if (DEBUG_PROG) {
1383 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1384 result[0], result[1], result[2], result[3],
1385 a[0], a[1], a[2], a[3],
1386 b[0], b[1], b[2], b[3]);
1387 }
Briane80d9012007-02-23 16:53:24 -07001388 }
1389 break;
1390 case OPCODE_SGT: /* set on greater */
1391 {
1392 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001393 fetch_vector4(&inst->SrcReg[0], machine, a);
1394 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001395 result[0] = (a[0] > b[0]) ? 1.0F : 0.0F;
1396 result[1] = (a[1] > b[1]) ? 1.0F : 0.0F;
1397 result[2] = (a[2] > b[2]) ? 1.0F : 0.0F;
1398 result[3] = (a[3] > b[3]) ? 1.0F : 0.0F;
1399 store_vector4(inst, machine, result);
1400 if (DEBUG_PROG) {
Brian28ab1122007-03-06 12:15:30 -07001401 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1402 result[0], result[1], result[2], result[3],
1403 a[0], a[1], a[2], a[3],
1404 b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001405 }
Briane80d9012007-02-23 16:53:24 -07001406 }
1407 break;
1408 case OPCODE_SIN:
1409 {
1410 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001411 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001412 result[0] = result[1] = result[2] = result[3]
1413 = (GLfloat) _mesa_sin(a[0]);
1414 store_vector4(inst, machine, result);
1415 }
1416 break;
1417 case OPCODE_SLE: /* set on less or equal */
1418 {
1419 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001420 fetch_vector4(&inst->SrcReg[0], machine, a);
1421 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001422 result[0] = (a[0] <= b[0]) ? 1.0F : 0.0F;
1423 result[1] = (a[1] <= b[1]) ? 1.0F : 0.0F;
1424 result[2] = (a[2] <= b[2]) ? 1.0F : 0.0F;
1425 result[3] = (a[3] <= b[3]) ? 1.0F : 0.0F;
1426 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001427 if (DEBUG_PROG) {
1428 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1429 result[0], result[1], result[2], result[3],
1430 a[0], a[1], a[2], a[3],
1431 b[0], b[1], b[2], b[3]);
1432 }
Briane80d9012007-02-23 16:53:24 -07001433 }
1434 break;
1435 case OPCODE_SLT: /* set on less */
1436 {
1437 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001438 fetch_vector4(&inst->SrcReg[0], machine, a);
1439 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001440 result[0] = (a[0] < b[0]) ? 1.0F : 0.0F;
1441 result[1] = (a[1] < b[1]) ? 1.0F : 0.0F;
1442 result[2] = (a[2] < b[2]) ? 1.0F : 0.0F;
1443 result[3] = (a[3] < b[3]) ? 1.0F : 0.0F;
1444 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001445 if (DEBUG_PROG) {
1446 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1447 result[0], result[1], result[2], result[3],
1448 a[0], a[1], a[2], a[3],
1449 b[0], b[1], b[2], b[3]);
1450 }
Briane80d9012007-02-23 16:53:24 -07001451 }
1452 break;
1453 case OPCODE_SNE: /* set on not equal */
1454 {
1455 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001456 fetch_vector4(&inst->SrcReg[0], machine, a);
1457 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001458 result[0] = (a[0] != b[0]) ? 1.0F : 0.0F;
1459 result[1] = (a[1] != b[1]) ? 1.0F : 0.0F;
1460 result[2] = (a[2] != b[2]) ? 1.0F : 0.0F;
1461 result[3] = (a[3] != b[3]) ? 1.0F : 0.0F;
1462 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001463 if (DEBUG_PROG) {
1464 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1465 result[0], result[1], result[2], result[3],
1466 a[0], a[1], a[2], a[3],
1467 b[0], b[1], b[2], b[3]);
1468 }
Briane80d9012007-02-23 16:53:24 -07001469 }
1470 break;
1471 case OPCODE_STR: /* set true, operands ignored */
1472 {
1473 static const GLfloat result[4] = { 1.0F, 1.0F, 1.0F, 1.0F };
1474 store_vector4(inst, machine, result);
1475 }
1476 break;
1477 case OPCODE_SUB:
1478 {
1479 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001480 fetch_vector4(&inst->SrcReg[0], machine, a);
1481 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001482 result[0] = a[0] - b[0];
1483 result[1] = a[1] - b[1];
1484 result[2] = a[2] - b[2];
1485 result[3] = a[3] - b[3];
1486 store_vector4(inst, machine, result);
1487 if (DEBUG_PROG) {
1488 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1489 result[0], result[1], result[2], result[3],
1490 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001491 }
Briane80d9012007-02-23 16:53:24 -07001492 }
1493 break;
1494 case OPCODE_SWZ: /* extended swizzle */
1495 {
1496 const struct prog_src_register *source = &inst->SrcReg[0];
Brian33eac562007-02-25 18:52:41 -07001497 const GLfloat *src = get_register_pointer(source, machine);
Briane80d9012007-02-23 16:53:24 -07001498 GLfloat result[4];
1499 GLuint i;
1500 for (i = 0; i < 4; i++) {
1501 const GLuint swz = GET_SWZ(source->Swizzle, i);
1502 if (swz == SWIZZLE_ZERO)
1503 result[i] = 0.0;
1504 else if (swz == SWIZZLE_ONE)
1505 result[i] = 1.0;
Brian13e3b212007-02-22 16:09:40 -07001506 else {
Briane80d9012007-02-23 16:53:24 -07001507 ASSERT(swz >= 0);
1508 ASSERT(swz <= 3);
1509 result[i] = src[swz];
Brian13e3b212007-02-22 16:09:40 -07001510 }
Briane80d9012007-02-23 16:53:24 -07001511 if (source->NegateBase & (1 << i))
1512 result[i] = -result[i];
Brian13e3b212007-02-22 16:09:40 -07001513 }
Briane80d9012007-02-23 16:53:24 -07001514 store_vector4(inst, machine, result);
1515 }
1516 break;
1517 case OPCODE_TEX: /* Both ARB and NV frag prog */
1518 /* Texel lookup */
1519 {
1520 /* Note: only use the precomputed lambda value when we're
1521 * sampling texture unit [K] with texcoord[K].
1522 * Otherwise, the lambda value may have no relation to the
1523 * instruction's texcoord or texture image. Using the wrong
1524 * lambda is usually bad news.
1525 * The rest of the time, just use zero (until we get a more
1526 * sophisticated way of computing lambda).
1527 */
1528 GLfloat coord[4], color[4], lambda;
1529#if 0
1530 if (inst->SrcReg[0].File == PROGRAM_INPUT &&
1531 inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0 + inst->TexSrcUnit)
1532 lambda = span->array->lambda[inst->TexSrcUnit][column];
1533 else
1534#endif
1535 lambda = 0.0;
Brian33eac562007-02-25 18:52:41 -07001536 fetch_vector4(&inst->SrcReg[0], machine, coord);
Briane80d9012007-02-23 16:53:24 -07001537 machine->FetchTexelLod(ctx, coord, lambda, inst->TexSrcUnit,
1538 color);
1539 if (DEBUG_PROG) {
1540 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g], "
1541 "lod %f\n",
1542 color[0], color[1], color[2], color[3],
1543 inst->TexSrcUnit,
1544 coord[0], coord[1], coord[2], coord[3], lambda);
1545 }
1546 store_vector4(inst, machine, color);
1547 }
1548 break;
1549 case OPCODE_TXB: /* GL_ARB_fragment_program only */
1550 /* Texel lookup with LOD bias */
1551 {
1552 const struct gl_texture_unit *texUnit
1553 = &ctx->Texture.Unit[inst->TexSrcUnit];
1554 GLfloat coord[4], color[4], lambda, bias;
1555#if 0
1556 if (inst->SrcReg[0].File == PROGRAM_INPUT &&
1557 inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0 + inst->TexSrcUnit)
1558 lambda = span->array->lambda[inst->TexSrcUnit][column];
1559 else
1560#endif
1561 lambda = 0.0;
Brian33eac562007-02-25 18:52:41 -07001562 fetch_vector4(&inst->SrcReg[0], machine, coord);
Briane80d9012007-02-23 16:53:24 -07001563 /* coord[3] is the bias to add to lambda */
1564 bias = texUnit->LodBias + coord[3];
1565 if (texUnit->_Current)
1566 bias += texUnit->_Current->LodBias;
1567 machine->FetchTexelLod(ctx, coord, lambda + bias,
1568 inst->TexSrcUnit, color);
1569 store_vector4(inst, machine, color);
1570 }
1571 break;
1572 case OPCODE_TXD: /* GL_NV_fragment_program only */
1573 /* Texture lookup w/ partial derivatives for LOD */
1574 {
1575 GLfloat texcoord[4], dtdx[4], dtdy[4], color[4];
Brian33eac562007-02-25 18:52:41 -07001576 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1577 fetch_vector4(&inst->SrcReg[1], machine, dtdx);
1578 fetch_vector4(&inst->SrcReg[2], machine, dtdy);
Briane80d9012007-02-23 16:53:24 -07001579 machine->FetchTexelDeriv(ctx, texcoord, dtdx, dtdy,
1580 inst->TexSrcUnit, color);
1581 store_vector4(inst, machine, color);
1582 }
1583 break;
1584 case OPCODE_TXP: /* GL_ARB_fragment_program only */
1585 /* Texture lookup w/ projective divide */
1586 {
1587 GLfloat texcoord[4], color[4], lambda;
1588#if 0
1589 if (inst->SrcReg[0].File == PROGRAM_INPUT &&
1590 inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0 + inst->TexSrcUnit)
1591 lambda = span->array->lambda[inst->TexSrcUnit][column];
1592 else
1593#endif
1594 lambda = 0.0;
Brian33eac562007-02-25 18:52:41 -07001595 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
Briane80d9012007-02-23 16:53:24 -07001596 /* Not so sure about this test - if texcoord[3] is
1597 * zero, we'd probably be fine except for an ASSERT in
1598 * IROUND_POS() which gets triggered by the inf values created.
1599 */
1600 if (texcoord[3] != 0.0) {
1601 texcoord[0] /= texcoord[3];
1602 texcoord[1] /= texcoord[3];
1603 texcoord[2] /= texcoord[3];
1604 }
1605 machine->FetchTexelLod(ctx, texcoord, lambda,
1606 inst->TexSrcUnit, color);
1607 store_vector4(inst, machine, color);
1608 }
1609 break;
1610 case OPCODE_TXP_NV: /* GL_NV_fragment_program only */
1611 /* Texture lookup w/ projective divide */
1612 {
1613 GLfloat texcoord[4], color[4], lambda;
1614#if 0
1615 if (inst->SrcReg[0].File == PROGRAM_INPUT &&
1616 inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0 + inst->TexSrcUnit)
1617 lambda = span->array->lambda[inst->TexSrcUnit][column];
1618 else
1619#endif
1620 lambda = 0.0;
Brian33eac562007-02-25 18:52:41 -07001621 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
Briane80d9012007-02-23 16:53:24 -07001622 if (inst->TexSrcTarget != TEXTURE_CUBE_INDEX &&
1623 texcoord[3] != 0.0) {
1624 texcoord[0] /= texcoord[3];
1625 texcoord[1] /= texcoord[3];
1626 texcoord[2] /= texcoord[3];
1627 }
1628 machine->FetchTexelLod(ctx, texcoord, lambda,
1629 inst->TexSrcUnit, color);
1630 store_vector4(inst, machine, color);
1631 }
1632 break;
1633 case OPCODE_UP2H: /* unpack two 16-bit floats */
1634 {
1635 GLfloat a[4], result[4];
1636 const GLuint *rawBits = (const GLuint *) a;
1637 GLhalfNV hx, hy;
Brian33eac562007-02-25 18:52:41 -07001638 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001639 hx = rawBits[0] & 0xffff;
1640 hy = rawBits[0] >> 16;
1641 result[0] = result[2] = _mesa_half_to_float(hx);
1642 result[1] = result[3] = _mesa_half_to_float(hy);
1643 store_vector4(inst, machine, result);
1644 }
1645 break;
1646 case OPCODE_UP2US: /* unpack two GLushorts */
1647 {
1648 GLfloat a[4], result[4];
1649 const GLuint *rawBits = (const GLuint *) a;
1650 GLushort usx, usy;
Brian33eac562007-02-25 18:52:41 -07001651 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001652 usx = rawBits[0] & 0xffff;
1653 usy = rawBits[0] >> 16;
1654 result[0] = result[2] = usx * (1.0f / 65535.0f);
1655 result[1] = result[3] = usy * (1.0f / 65535.0f);
1656 store_vector4(inst, machine, result);
1657 }
1658 break;
1659 case OPCODE_UP4B: /* unpack four GLbytes */
1660 {
1661 GLfloat a[4], result[4];
1662 const GLuint *rawBits = (const GLuint *) a;
Brian33eac562007-02-25 18:52:41 -07001663 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001664 result[0] = (((rawBits[0] >> 0) & 0xff) - 128) / 127.0F;
1665 result[1] = (((rawBits[0] >> 8) & 0xff) - 128) / 127.0F;
1666 result[2] = (((rawBits[0] >> 16) & 0xff) - 128) / 127.0F;
1667 result[3] = (((rawBits[0] >> 24) & 0xff) - 128) / 127.0F;
1668 store_vector4(inst, machine, result);
1669 }
1670 break;
1671 case OPCODE_UP4UB: /* unpack four GLubytes */
1672 {
1673 GLfloat a[4], result[4];
1674 const GLuint *rawBits = (const GLuint *) a;
Brian33eac562007-02-25 18:52:41 -07001675 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001676 result[0] = ((rawBits[0] >> 0) & 0xff) / 255.0F;
1677 result[1] = ((rawBits[0] >> 8) & 0xff) / 255.0F;
1678 result[2] = ((rawBits[0] >> 16) & 0xff) / 255.0F;
1679 result[3] = ((rawBits[0] >> 24) & 0xff) / 255.0F;
1680 store_vector4(inst, machine, result);
1681 }
1682 break;
1683 case OPCODE_XPD: /* cross product */
1684 {
1685 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001686 fetch_vector4(&inst->SrcReg[0], machine, a);
1687 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001688 result[0] = a[1] * b[2] - a[2] * b[1];
1689 result[1] = a[2] * b[0] - a[0] * b[2];
1690 result[2] = a[0] * b[1] - a[1] * b[0];
1691 result[3] = 1.0;
1692 store_vector4(inst, machine, result);
Brian9637c962007-03-07 17:40:57 -07001693 if (DEBUG_PROG) {
1694 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1695 result[0], result[1], result[2], result[3],
1696 a[0], a[1], a[2], b[0], b[1], b[2]);
1697 }
Briane80d9012007-02-23 16:53:24 -07001698 }
1699 break;
1700 case OPCODE_X2D: /* 2-D matrix transform */
1701 {
1702 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001703 fetch_vector4(&inst->SrcReg[0], machine, a);
1704 fetch_vector4(&inst->SrcReg[1], machine, b);
1705 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -07001706 result[0] = a[0] + b[0] * c[0] + b[1] * c[1];
1707 result[1] = a[1] + b[0] * c[2] + b[1] * c[3];
1708 result[2] = a[2] + b[0] * c[0] + b[1] * c[1];
1709 result[3] = a[3] + b[0] * c[2] + b[1] * c[3];
1710 store_vector4(inst, machine, result);
1711 }
1712 break;
1713 case OPCODE_PRINT:
1714 {
1715 if (inst->SrcReg[0].File != -1) {
1716 GLfloat a[4];
Brian33eac562007-02-25 18:52:41 -07001717 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001718 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst->Data,
1719 a[0], a[1], a[2], a[3]);
1720 }
1721 else {
1722 _mesa_printf("%s\n", (const char *) inst->Data);
1723 }
1724 }
1725 break;
1726 case OPCODE_END:
1727 return GL_TRUE;
1728 default:
1729 _mesa_problem(ctx, "Bad opcode %d in _mesa_exec_fragment_program",
1730 inst->Opcode);
1731 return GL_TRUE; /* return value doesn't matter */
Brian13e3b212007-02-22 16:09:40 -07001732
1733 }
Briane80d9012007-02-23 16:53:24 -07001734
Briancfd00112007-02-25 18:30:45 -07001735 numExec++;
1736 if (numExec > maxExec) {
Brian13e3b212007-02-22 16:09:40 -07001737 _mesa_problem(ctx, "Infinite loop detected in fragment program");
1738 return GL_TRUE;
Brian13e3b212007-02-22 16:09:40 -07001739 }
Briane80d9012007-02-23 16:53:24 -07001740
1741 } /* for pc */
Brian13e3b212007-02-22 16:09:40 -07001742
1743#if FEATURE_MESA_program_debug
1744 CurrentMachine = NULL;
1745#endif
1746
1747 return GL_TRUE;
1748}