blob: 013d65ce8699a04dd16a201196d466aa26919871 [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]);
Briana01616e2007-03-28 11:01:28 -0600511#if DEBUG_PROG
512 printf("CondCodes=(%s,%s,%s,%s) for:\n",
513 _mesa_condcode_string(machine->CondCodes[0]),
514 _mesa_condcode_string(machine->CondCodes[1]),
515 _mesa_condcode_string(machine->CondCodes[2]),
516 _mesa_condcode_string(machine->CondCodes[3]));
517#endif
Brian13e3b212007-02-22 16:09:40 -0700518 }
519}
520
521
522#if 0
523/**
524 * Initialize a new machine state instance from an existing one, adding
525 * the partial derivatives onto the input registers.
526 * Used to implement DDX and DDY instructions in non-trivial cases.
527 */
528static void
Briane80d9012007-02-23 16:53:24 -0700529init_machine_deriv(GLcontext * ctx,
530 const struct gl_program_machine *machine,
531 const struct gl_fragment_program *program,
532 const SWspan * span, char xOrY,
533 struct gl_program_machine *dMachine)
Brian13e3b212007-02-22 16:09:40 -0700534{
535 GLuint attr;
536
537 ASSERT(xOrY == 'X' || xOrY == 'Y');
538
539 /* copy existing machine */
540 _mesa_memcpy(dMachine, machine, sizeof(struct gl_program_machine));
541
542 if (program->Base.Target == GL_FRAGMENT_PROGRAM_NV) {
543 /* XXX also need to do this when using valgrind */
544 /* Clear temporary registers (undefined for ARB_f_p) */
Briane80d9012007-02-23 16:53:24 -0700545 _mesa_bzero((void *) machine->Temporaries,
546 MAX_PROGRAM_TEMPS * 4 * sizeof(GLfloat));
Brian13e3b212007-02-22 16:09:40 -0700547 }
548
549 /* Add derivatives */
550 if (program->Base.InputsRead & FRAG_BIT_WPOS) {
551 GLfloat *wpos = machine->Attribs[FRAG_ATTRIB_WPOS][machine->CurElement];
552 if (xOrY == 'X') {
553 wpos[0] += 1.0F;
554 wpos[1] += 0.0F;
555 wpos[2] += span->attrStepX[FRAG_ATTRIB_WPOS][2];
556 wpos[3] += span->attrStepX[FRAG_ATTRIB_WPOS][3];
557 }
558 else {
559 wpos[0] += 0.0F;
560 wpos[1] += 1.0F;
561 wpos[2] += span->attrStepY[FRAG_ATTRIB_WPOS][2];
562 wpos[3] += span->attrStepY[FRAG_ATTRIB_WPOS][3];
563 }
564 }
565
566 /* primary, secondary colors */
567 for (attr = FRAG_ATTRIB_COL0; attr <= FRAG_ATTRIB_COL1; attr++) {
568 if (program->Base.InputsRead & (1 << attr)) {
569 GLfloat *col = machine->Attribs[attr][machine->CurElement];
570 if (xOrY == 'X') {
571 col[0] += span->attrStepX[attr][0] * (1.0F / CHAN_MAXF);
572 col[1] += span->attrStepX[attr][1] * (1.0F / CHAN_MAXF);
573 col[2] += span->attrStepX[attr][2] * (1.0F / CHAN_MAXF);
574 col[3] += span->attrStepX[attr][3] * (1.0F / CHAN_MAXF);
575 }
576 else {
577 col[0] += span->attrStepY[attr][0] * (1.0F / CHAN_MAXF);
578 col[1] += span->attrStepY[attr][1] * (1.0F / CHAN_MAXF);
579 col[2] += span->attrStepY[attr][2] * (1.0F / CHAN_MAXF);
580 col[3] += span->attrStepY[attr][3] * (1.0F / CHAN_MAXF);
581 }
582 }
583 }
584 if (program->Base.InputsRead & FRAG_BIT_FOGC) {
585 GLfloat *fogc = machine->Attribs[FRAG_ATTRIB_FOGC][machine->CurElement];
586 if (xOrY == 'X') {
587 fogc[0] += span->attrStepX[FRAG_ATTRIB_FOGC][0];
588 }
589 else {
590 fogc[0] += span->attrStepY[FRAG_ATTRIB_FOGC][0];
591 }
592 }
593 /* texcoord and varying vars */
594 for (attr = FRAG_ATTRIB_TEX0; attr < FRAG_ATTRIB_MAX; attr++) {
595 if (program->Base.InputsRead & (1 << attr)) {
596 GLfloat *val = machine->Attribs[attr][machine->CurElement];
597 /* XXX perspective-correct interpolation */
598 if (xOrY == 'X') {
599 val[0] += span->attrStepX[attr][0];
600 val[1] += span->attrStepX[attr][1];
601 val[2] += span->attrStepX[attr][2];
602 val[3] += span->attrStepX[attr][3];
603 }
604 else {
605 val[0] += span->attrStepY[attr][0];
606 val[1] += span->attrStepY[attr][1];
607 val[2] += span->attrStepY[attr][2];
608 val[3] += span->attrStepY[attr][3];
609 }
610 }
611 }
612
613 /* init condition codes */
614 dMachine->CondCodes[0] = COND_EQ;
615 dMachine->CondCodes[1] = COND_EQ;
616 dMachine->CondCodes[2] = COND_EQ;
617 dMachine->CondCodes[3] = COND_EQ;
618}
619#endif
620
621
622/**
623 * Execute the given vertex/fragment program.
624 *
Brian3c1c9992007-02-25 19:11:44 -0700625 * \param ctx rendering context
626 * \param program the program to execute
627 * \param machine machine state (must be initialized)
Brian13e3b212007-02-22 16:09:40 -0700628 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
629 */
630GLboolean
Briane80d9012007-02-23 16:53:24 -0700631_mesa_execute_program(GLcontext * ctx,
Brian8b34b7d2007-02-25 18:26:50 -0700632 const struct gl_program *program,
Brian085d7d52007-02-25 18:23:37 -0700633 struct gl_program_machine *machine)
Brian13e3b212007-02-22 16:09:40 -0700634{
Brian8b34b7d2007-02-25 18:26:50 -0700635 const GLuint numInst = program->NumInstructions;
Briancfd00112007-02-25 18:30:45 -0700636 const GLuint maxExec = 10000;
637 GLint pc, numExec = 0;
Brian13e3b212007-02-22 16:09:40 -0700638
639 machine->CurProgram = program;
640
641 if (DEBUG_PROG) {
642 printf("execute program %u --------------------\n", program->Id);
643 }
644
645#if FEATURE_MESA_program_debug
646 CurrentMachine = machine;
647#endif
648
Brian33eac562007-02-25 18:52:41 -0700649 if (program->Target == GL_VERTEX_PROGRAM_ARB) {
650 machine->EnvParams = ctx->VertexProgram.Parameters;
651 }
652 else {
653 machine->EnvParams = ctx->FragmentProgram.Parameters;
654 }
655
Brian8b34b7d2007-02-25 18:26:50 -0700656 for (pc = 0; pc < numInst; pc++) {
Brian13e3b212007-02-22 16:09:40 -0700657 const struct prog_instruction *inst = program->Instructions + pc;
658
659#if FEATURE_MESA_program_debug
660 if (ctx->FragmentProgram.CallbackEnabled &&
661 ctx->FragmentProgram.Callback) {
662 ctx->FragmentProgram.CurrentPosition = inst->StringPos;
663 ctx->FragmentProgram.Callback(program->Target,
664 ctx->FragmentProgram.CallbackData);
665 }
666#endif
667
668 if (DEBUG_PROG) {
669 _mesa_print_instruction(inst);
670 }
671
672 switch (inst->Opcode) {
Briane80d9012007-02-23 16:53:24 -0700673 case OPCODE_ABS:
674 {
675 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700676 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700677 result[0] = FABSF(a[0]);
678 result[1] = FABSF(a[1]);
679 result[2] = FABSF(a[2]);
680 result[3] = FABSF(a[3]);
681 store_vector4(inst, machine, result);
682 }
683 break;
684 case OPCODE_ADD:
685 {
686 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700687 fetch_vector4(&inst->SrcReg[0], machine, a);
688 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700689 result[0] = a[0] + b[0];
690 result[1] = a[1] + b[1];
691 result[2] = a[2] + b[2];
692 result[3] = a[3] + b[3];
693 store_vector4(inst, machine, result);
694 if (DEBUG_PROG) {
695 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
696 result[0], result[1], result[2], result[3],
697 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -0700698 }
Briane80d9012007-02-23 16:53:24 -0700699 }
700 break;
Brianf183a2d2007-02-23 17:14:30 -0700701 case OPCODE_ARL:
702 {
703 GLfloat t[4];
Brian33eac562007-02-25 18:52:41 -0700704 fetch_vector4(&inst->SrcReg[0], machine, t);
Brianf183a2d2007-02-23 17:14:30 -0700705 machine->AddressReg[0][0] = (GLint) FLOORF(t[0]);
706 }
707 break;
Briane80d9012007-02-23 16:53:24 -0700708 case OPCODE_BGNLOOP:
709 /* no-op */
710 break;
711 case OPCODE_ENDLOOP:
712 /* subtract 1 here since pc is incremented by for(pc) loop */
713 pc = inst->BranchTarget - 1; /* go to matching BNGLOOP */
714 break;
715 case OPCODE_BGNSUB: /* begin subroutine */
716 break;
717 case OPCODE_ENDSUB: /* end subroutine */
718 break;
719 case OPCODE_BRA: /* branch (conditional) */
720 /* fall-through */
721 case OPCODE_BRK: /* break out of loop (conditional) */
722 /* fall-through */
723 case OPCODE_CONT: /* continue loop (conditional) */
724 if (eval_condition(machine, inst)) {
725 /* take branch */
726 /* Subtract 1 here since we'll do pc++ at end of for-loop */
727 pc = inst->BranchTarget - 1;
728 }
729 break;
730 case OPCODE_CAL: /* Call subroutine (conditional) */
731 if (eval_condition(machine, inst)) {
732 /* call the subroutine */
733 if (machine->StackDepth >= MAX_PROGRAM_CALL_DEPTH) {
734 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
Brian13e3b212007-02-22 16:09:40 -0700735 }
Briana0275b02007-03-27 11:02:20 -0600736 machine->CallStack[machine->StackDepth++] = pc + 1; /* next inst */
Brian31dc7a32007-03-27 15:21:35 -0600737 /* Subtract 1 here since we'll do pc++ at end of for-loop */
738 pc = inst->BranchTarget - 1;
Briane80d9012007-02-23 16:53:24 -0700739 }
740 break;
741 case OPCODE_CMP:
742 {
743 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700744 fetch_vector4(&inst->SrcReg[0], machine, a);
745 fetch_vector4(&inst->SrcReg[1], machine, b);
746 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -0700747 result[0] = a[0] < 0.0F ? b[0] : c[0];
748 result[1] = a[1] < 0.0F ? b[1] : c[1];
749 result[2] = a[2] < 0.0F ? b[2] : c[2];
750 result[3] = a[3] < 0.0F ? b[3] : c[3];
751 store_vector4(inst, machine, result);
752 }
753 break;
754 case OPCODE_COS:
755 {
756 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700757 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700758 result[0] = result[1] = result[2] = result[3]
759 = (GLfloat) _mesa_cos(a[0]);
760 store_vector4(inst, machine, result);
761 }
762 break;
763 case OPCODE_DDX: /* Partial derivative with respect to X */
764 {
Brian13e3b212007-02-22 16:09:40 -0700765#if 0
Briane80d9012007-02-23 16:53:24 -0700766 GLfloat a[4], aNext[4], result[4];
767 struct gl_program_machine dMachine;
768 if (!fetch_vector4_deriv(ctx, &inst->SrcReg[0], span, 'X',
769 column, result)) {
770 /* This is tricky. Make a copy of the current machine state,
771 * increment the input registers by the dx or dy partial
772 * derivatives, then re-execute the program up to the
773 * preceeding instruction, then fetch the source register.
774 * Finally, find the difference in the register values for
775 * the original and derivative runs.
776 */
Brian33eac562007-02-25 18:52:41 -0700777 fetch_vector4(&inst->SrcReg[0], machine, program, a);
Briane80d9012007-02-23 16:53:24 -0700778 init_machine_deriv(ctx, machine, program, span,
779 'X', &dMachine);
780 execute_program(ctx, program, pc, &dMachine, span, column);
Brian33eac562007-02-25 18:52:41 -0700781 fetch_vector4(&inst->SrcReg[0], &dMachine, program,
Briane80d9012007-02-23 16:53:24 -0700782 aNext);
783 result[0] = aNext[0] - a[0];
784 result[1] = aNext[1] - a[1];
785 result[2] = aNext[2] - a[2];
786 result[3] = aNext[3] - a[3];
Brian13e3b212007-02-22 16:09:40 -0700787 }
Briane80d9012007-02-23 16:53:24 -0700788 store_vector4(inst, machine, result);
789#else
Brianf183a2d2007-02-23 17:14:30 -0700790 store_vector4(inst, machine, ZeroVec);
Briane80d9012007-02-23 16:53:24 -0700791#endif
792 }
793 break;
794 case OPCODE_DDY: /* Partial derivative with respect to Y */
795 {
Brian13e3b212007-02-22 16:09:40 -0700796#if 0
Briane80d9012007-02-23 16:53:24 -0700797 GLfloat a[4], aNext[4], result[4];
798 struct gl_program_machine dMachine;
799 if (!fetch_vector4_deriv(ctx, &inst->SrcReg[0], span, 'Y',
800 column, result)) {
801 init_machine_deriv(ctx, machine, program, span,
802 'Y', &dMachine);
Brian33eac562007-02-25 18:52:41 -0700803 fetch_vector4(&inst->SrcReg[0], machine, program, a);
Briane80d9012007-02-23 16:53:24 -0700804 execute_program(ctx, program, pc, &dMachine, span, column);
Brian33eac562007-02-25 18:52:41 -0700805 fetch_vector4(&inst->SrcReg[0], &dMachine, program,
Briane80d9012007-02-23 16:53:24 -0700806 aNext);
807 result[0] = aNext[0] - a[0];
808 result[1] = aNext[1] - a[1];
809 result[2] = aNext[2] - a[2];
810 result[3] = aNext[3] - a[3];
811 }
812 store_vector4(inst, machine, result);
Brian13e3b212007-02-22 16:09:40 -0700813#else
Brianf183a2d2007-02-23 17:14:30 -0700814 store_vector4(inst, machine, ZeroVec);
Brian13e3b212007-02-22 16:09:40 -0700815#endif
Briane80d9012007-02-23 16:53:24 -0700816 }
817 break;
818 case OPCODE_DP3:
819 {
820 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700821 fetch_vector4(&inst->SrcReg[0], machine, a);
822 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700823 result[0] = result[1] = result[2] = result[3] = DOT3(a, b);
824 store_vector4(inst, machine, result);
825 if (DEBUG_PROG) {
826 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
827 result[0], a[0], a[1], a[2], b[0], b[1], b[2]);
Brian13e3b212007-02-22 16:09:40 -0700828 }
Briane80d9012007-02-23 16:53:24 -0700829 }
830 break;
831 case OPCODE_DP4:
832 {
833 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700834 fetch_vector4(&inst->SrcReg[0], machine, a);
835 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700836 result[0] = result[1] = result[2] = result[3] = DOT4(a, b);
837 store_vector4(inst, machine, result);
838 if (DEBUG_PROG) {
839 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
840 result[0], a[0], a[1], a[2], a[3],
841 b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -0700842 }
Briane80d9012007-02-23 16:53:24 -0700843 }
844 break;
845 case OPCODE_DPH:
846 {
847 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700848 fetch_vector4(&inst->SrcReg[0], machine, a);
849 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700850 result[0] = result[1] = result[2] = result[3] =
851 a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + b[3];
852 store_vector4(inst, machine, result);
853 }
854 break;
855 case OPCODE_DST: /* Distance vector */
856 {
857 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700858 fetch_vector4(&inst->SrcReg[0], machine, a);
859 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700860 result[0] = 1.0F;
861 result[1] = a[1] * b[1];
862 result[2] = a[2];
863 result[3] = b[3];
864 store_vector4(inst, machine, result);
865 }
866 break;
Brianf183a2d2007-02-23 17:14:30 -0700867 case OPCODE_EXP:
Brianf183a2d2007-02-23 17:14:30 -0700868 {
869 GLfloat t[4], q[4], floor_t0;
Brian33eac562007-02-25 18:52:41 -0700870 fetch_vector1(&inst->SrcReg[0], machine, t);
Brianf183a2d2007-02-23 17:14:30 -0700871 floor_t0 = FLOORF(t[0]);
872 if (floor_t0 > FLT_MAX_EXP) {
873 SET_POS_INFINITY(q[0]);
874 SET_POS_INFINITY(q[2]);
875 }
876 else if (floor_t0 < FLT_MIN_EXP) {
877 q[0] = 0.0F;
878 q[2] = 0.0F;
879 }
880 else {
Brian761728a2007-02-24 11:14:57 -0700881 q[0] = LDEXPF(1.0, (int) floor_t0);
882 /* Note: GL_NV_vertex_program expects
883 * result.z = result.x * APPX(result.y)
884 * We do what the ARB extension says.
885 */
886 q[2] = pow(2.0, t[0]);
Brianf183a2d2007-02-23 17:14:30 -0700887 }
888 q[1] = t[0] - floor_t0;
889 q[3] = 1.0F;
890 store_vector4( inst, machine, q );
891 }
892 break;
Briane80d9012007-02-23 16:53:24 -0700893 case OPCODE_EX2: /* Exponential base 2 */
894 {
895 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700896 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700897 result[0] = result[1] = result[2] = result[3] =
898 (GLfloat) _mesa_pow(2.0, a[0]);
899 store_vector4(inst, machine, result);
900 }
901 break;
902 case OPCODE_FLR:
903 {
904 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700905 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700906 result[0] = FLOORF(a[0]);
907 result[1] = FLOORF(a[1]);
908 result[2] = FLOORF(a[2]);
909 result[3] = FLOORF(a[3]);
910 store_vector4(inst, machine, result);
911 }
912 break;
913 case OPCODE_FRC:
914 {
915 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700916 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700917 result[0] = a[0] - FLOORF(a[0]);
918 result[1] = a[1] - FLOORF(a[1]);
919 result[2] = a[2] - FLOORF(a[2]);
920 result[3] = a[3] - FLOORF(a[3]);
921 store_vector4(inst, machine, result);
922 }
923 break;
924 case OPCODE_IF:
Brian63556fa2007-03-23 14:47:46 -0600925 {
926 GLboolean cond;
927 /* eval condition */
928 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
929 GLfloat a[4];
930 fetch_vector1(&inst->SrcReg[0], machine, a);
931 cond = (a[0] != 0.0);
932 }
933 else {
934 cond = eval_condition(machine, inst);
935 }
936 /* do if/else */
937 if (cond) {
938 /* do if-clause (just continue execution) */
939 }
940 else {
941 /* go to the instruction after ELSE or ENDIF */
942 assert(inst->BranchTarget >= 0);
943 pc = inst->BranchTarget - 1;
944 }
Briane80d9012007-02-23 16:53:24 -0700945 }
946 break;
947 case OPCODE_ELSE:
948 /* goto ENDIF */
949 assert(inst->BranchTarget >= 0);
950 pc = inst->BranchTarget - 1;
951 break;
952 case OPCODE_ENDIF:
953 /* nothing */
954 break;
955 case OPCODE_INT: /* float to int */
956 {
957 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700958 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700959 result[0] = (GLfloat) (GLint) a[0];
960 result[1] = (GLfloat) (GLint) a[1];
961 result[2] = (GLfloat) (GLint) a[2];
962 result[3] = (GLfloat) (GLint) a[3];
963 store_vector4(inst, machine, result);
964 }
965 break;
966 case OPCODE_KIL_NV: /* NV_f_p only (conditional) */
967 if (eval_condition(machine, inst)) {
968 return GL_FALSE;
969 }
970 break;
971 case OPCODE_KIL: /* ARB_f_p only */
972 {
973 GLfloat a[4];
Brian33eac562007-02-25 18:52:41 -0700974 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700975 if (a[0] < 0.0F || a[1] < 0.0F || a[2] < 0.0F || a[3] < 0.0F) {
Brian13e3b212007-02-22 16:09:40 -0700976 return GL_FALSE;
977 }
Briane80d9012007-02-23 16:53:24 -0700978 }
979 break;
980 case OPCODE_LG2: /* log base 2 */
981 {
982 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700983 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700984 result[0] = result[1] = result[2] = result[3] = LOG2(a[0]);
985 store_vector4(inst, machine, result);
986 }
987 break;
988 case OPCODE_LIT:
989 {
990 const GLfloat epsilon = 1.0F / 256.0F; /* from NV VP spec */
991 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700992 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700993 a[0] = MAX2(a[0], 0.0F);
994 a[1] = MAX2(a[1], 0.0F);
995 /* XXX ARB version clamps a[3], NV version doesn't */
996 a[3] = CLAMP(a[3], -(128.0F - epsilon), (128.0F - epsilon));
997 result[0] = 1.0F;
998 result[1] = a[0];
999 /* XXX we could probably just use pow() here */
1000 if (a[0] > 0.0F) {
1001 if (a[1] == 0.0 && a[3] == 0.0)
1002 result[2] = 1.0;
1003 else
1004 result[2] = EXPF(a[3] * LOGF(a[1]));
Brian13e3b212007-02-22 16:09:40 -07001005 }
Briane80d9012007-02-23 16:53:24 -07001006 else {
1007 result[2] = 0.0;
Brian13e3b212007-02-22 16:09:40 -07001008 }
Briane80d9012007-02-23 16:53:24 -07001009 result[3] = 1.0F;
1010 store_vector4(inst, machine, result);
1011 if (DEBUG_PROG) {
1012 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
1013 result[0], result[1], result[2], result[3],
1014 a[0], a[1], a[2], a[3]);
Brian13e3b212007-02-22 16:09:40 -07001015 }
Briane80d9012007-02-23 16:53:24 -07001016 }
1017 break;
Brianf183a2d2007-02-23 17:14:30 -07001018 case OPCODE_LOG:
1019 {
1020 GLfloat t[4], q[4], abs_t0;
Brian33eac562007-02-25 18:52:41 -07001021 fetch_vector1(&inst->SrcReg[0], machine, t);
Brianf183a2d2007-02-23 17:14:30 -07001022 abs_t0 = FABSF(t[0]);
1023 if (abs_t0 != 0.0F) {
1024 /* Since we really can't handle infinite values on VMS
1025 * like other OSes we'll use __MAXFLOAT to represent
1026 * infinity. This may need some tweaking.
1027 */
1028#ifdef VMS
1029 if (abs_t0 == __MAXFLOAT)
1030#else
1031 if (IS_INF_OR_NAN(abs_t0))
1032#endif
1033 {
1034 SET_POS_INFINITY(q[0]);
1035 q[1] = 1.0F;
1036 SET_POS_INFINITY(q[2]);
1037 }
1038 else {
1039 int exponent;
1040 GLfloat mantissa = FREXPF(t[0], &exponent);
1041 q[0] = (GLfloat) (exponent - 1);
1042 q[1] = (GLfloat) (2.0 * mantissa); /* map [.5, 1) -> [1, 2) */
1043 q[2] = (GLfloat) (q[0] + LOG2(q[1]));
1044 }
1045 }
1046 else {
1047 SET_NEG_INFINITY(q[0]);
1048 q[1] = 1.0F;
1049 SET_NEG_INFINITY(q[2]);
1050 }
1051 q[3] = 1.0;
1052 store_vector4(inst, machine, q);
1053 }
1054 break;
Briane80d9012007-02-23 16:53:24 -07001055 case OPCODE_LRP:
1056 {
1057 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001058 fetch_vector4(&inst->SrcReg[0], machine, a);
1059 fetch_vector4(&inst->SrcReg[1], machine, b);
1060 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -07001061 result[0] = a[0] * b[0] + (1.0F - a[0]) * c[0];
1062 result[1] = a[1] * b[1] + (1.0F - a[1]) * c[1];
1063 result[2] = a[2] * b[2] + (1.0F - a[2]) * c[2];
1064 result[3] = a[3] * b[3] + (1.0F - a[3]) * c[3];
1065 store_vector4(inst, machine, result);
1066 if (DEBUG_PROG) {
1067 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
1068 "(%g %g %g %g), (%g %g %g %g)\n",
1069 result[0], result[1], result[2], result[3],
1070 a[0], a[1], a[2], a[3],
1071 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
Brian13e3b212007-02-22 16:09:40 -07001072 }
Briane80d9012007-02-23 16:53:24 -07001073 }
1074 break;
1075 case OPCODE_MAD:
1076 {
1077 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001078 fetch_vector4(&inst->SrcReg[0], machine, a);
1079 fetch_vector4(&inst->SrcReg[1], machine, b);
1080 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -07001081 result[0] = a[0] * b[0] + c[0];
1082 result[1] = a[1] * b[1] + c[1];
1083 result[2] = a[2] * b[2] + c[2];
1084 result[3] = a[3] * b[3] + c[3];
1085 store_vector4(inst, machine, result);
1086 if (DEBUG_PROG) {
1087 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
1088 "(%g %g %g %g) + (%g %g %g %g)\n",
1089 result[0], result[1], result[2], result[3],
1090 a[0], a[1], a[2], a[3],
1091 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
Brian13e3b212007-02-22 16:09:40 -07001092 }
Briane80d9012007-02-23 16:53:24 -07001093 }
1094 break;
1095 case OPCODE_MAX:
1096 {
1097 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001098 fetch_vector4(&inst->SrcReg[0], machine, a);
1099 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001100 result[0] = MAX2(a[0], b[0]);
1101 result[1] = MAX2(a[1], b[1]);
1102 result[2] = MAX2(a[2], b[2]);
1103 result[3] = MAX2(a[3], b[3]);
1104 store_vector4(inst, machine, result);
1105 if (DEBUG_PROG) {
1106 printf("MAX (%g %g %g %g) = (%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], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001109 }
Briane80d9012007-02-23 16:53:24 -07001110 }
1111 break;
1112 case OPCODE_MIN:
1113 {
1114 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001115 fetch_vector4(&inst->SrcReg[0], machine, a);
1116 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001117 result[0] = MIN2(a[0], b[0]);
1118 result[1] = MIN2(a[1], b[1]);
1119 result[2] = MIN2(a[2], b[2]);
1120 result[3] = MIN2(a[3], b[3]);
1121 store_vector4(inst, machine, result);
1122 }
1123 break;
1124 case OPCODE_MOV:
1125 {
1126 GLfloat result[4];
Brian33eac562007-02-25 18:52:41 -07001127 fetch_vector4(&inst->SrcReg[0], machine, result);
Briane80d9012007-02-23 16:53:24 -07001128 store_vector4(inst, machine, result);
1129 if (DEBUG_PROG) {
1130 printf("MOV (%g %g %g %g)\n",
1131 result[0], result[1], result[2], result[3]);
Brian13e3b212007-02-22 16:09:40 -07001132 }
Briane80d9012007-02-23 16:53:24 -07001133 }
1134 break;
1135 case OPCODE_MUL:
1136 {
1137 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001138 fetch_vector4(&inst->SrcReg[0], machine, a);
1139 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001140 result[0] = a[0] * b[0];
1141 result[1] = a[1] * b[1];
1142 result[2] = a[2] * b[2];
1143 result[3] = a[3] * b[3];
1144 store_vector4(inst, machine, result);
1145 if (DEBUG_PROG) {
1146 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1147 result[0], result[1], result[2], result[3],
1148 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001149 }
Briane80d9012007-02-23 16:53:24 -07001150 }
1151 break;
1152 case OPCODE_NOISE1:
1153 {
1154 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001155 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001156 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001157 result[1] =
Briane80d9012007-02-23 16:53:24 -07001158 result[2] = result[3] = _slang_library_noise1(a[0]);
1159 store_vector4(inst, machine, result);
1160 }
1161 break;
1162 case OPCODE_NOISE2:
1163 {
1164 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001165 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001166 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001167 result[1] =
Briane80d9012007-02-23 16:53:24 -07001168 result[2] = result[3] = _slang_library_noise2(a[0], a[1]);
1169 store_vector4(inst, machine, result);
1170 }
1171 break;
1172 case OPCODE_NOISE3:
1173 {
1174 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001175 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001176 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001177 result[1] =
1178 result[2] =
1179 result[3] = _slang_library_noise3(a[0], a[1], a[2]);
Briane80d9012007-02-23 16:53:24 -07001180 store_vector4(inst, machine, result);
1181 }
1182 break;
1183 case OPCODE_NOISE4:
1184 {
1185 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001186 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001187 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001188 result[1] =
1189 result[2] =
1190 result[3] = _slang_library_noise4(a[0], a[1], a[2], a[3]);
Briane80d9012007-02-23 16:53:24 -07001191 store_vector4(inst, machine, result);
1192 }
1193 break;
1194 case OPCODE_NOP:
1195 break;
1196 case OPCODE_PK2H: /* pack two 16-bit floats in one 32-bit float */
1197 {
1198 GLfloat a[4], result[4];
1199 GLhalfNV hx, hy;
1200 GLuint *rawResult = (GLuint *) result;
1201 GLuint twoHalves;
Brian33eac562007-02-25 18:52:41 -07001202 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001203 hx = _mesa_float_to_half(a[0]);
1204 hy = _mesa_float_to_half(a[1]);
1205 twoHalves = hx | (hy << 16);
1206 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
1207 = twoHalves;
1208 store_vector4(inst, machine, result);
1209 }
1210 break;
1211 case OPCODE_PK2US: /* pack two GLushorts into one 32-bit float */
1212 {
1213 GLfloat a[4], result[4];
1214 GLuint usx, usy, *rawResult = (GLuint *) result;
Brian33eac562007-02-25 18:52:41 -07001215 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001216 a[0] = CLAMP(a[0], 0.0F, 1.0F);
1217 a[1] = CLAMP(a[1], 0.0F, 1.0F);
1218 usx = IROUND(a[0] * 65535.0F);
1219 usy = IROUND(a[1] * 65535.0F);
1220 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
1221 = usx | (usy << 16);
1222 store_vector4(inst, machine, result);
1223 }
1224 break;
1225 case OPCODE_PK4B: /* pack four GLbytes into one 32-bit float */
1226 {
1227 GLfloat a[4], result[4];
1228 GLuint ubx, uby, ubz, ubw, *rawResult = (GLuint *) result;
Brian33eac562007-02-25 18:52:41 -07001229 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001230 a[0] = CLAMP(a[0], -128.0F / 127.0F, 1.0F);
1231 a[1] = CLAMP(a[1], -128.0F / 127.0F, 1.0F);
1232 a[2] = CLAMP(a[2], -128.0F / 127.0F, 1.0F);
1233 a[3] = CLAMP(a[3], -128.0F / 127.0F, 1.0F);
1234 ubx = IROUND(127.0F * a[0] + 128.0F);
1235 uby = IROUND(127.0F * a[1] + 128.0F);
1236 ubz = IROUND(127.0F * a[2] + 128.0F);
1237 ubw = IROUND(127.0F * a[3] + 128.0F);
1238 rawResult[0] = rawResult[1] = rawResult[2] = rawResult[3]
1239 = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
1240 store_vector4(inst, machine, result);
1241 }
1242 break;
1243 case OPCODE_PK4UB: /* pack four GLubytes 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], 0.0F, 1.0F);
1249 a[1] = CLAMP(a[1], 0.0F, 1.0F);
1250 a[2] = CLAMP(a[2], 0.0F, 1.0F);
1251 a[3] = CLAMP(a[3], 0.0F, 1.0F);
1252 ubx = IROUND(255.0F * a[0]);
1253 uby = IROUND(255.0F * a[1]);
1254 ubz = IROUND(255.0F * a[2]);
1255 ubw = IROUND(255.0F * a[3]);
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_POW:
1262 {
1263 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001264 fetch_vector1(&inst->SrcReg[0], machine, a);
1265 fetch_vector1(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001266 result[0] = result[1] = result[2] = result[3]
1267 = (GLfloat) _mesa_pow(a[0], b[0]);
1268 store_vector4(inst, machine, result);
1269 }
1270 break;
1271 case OPCODE_RCP:
1272 {
1273 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001274 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001275 if (DEBUG_PROG) {
1276 if (a[0] == 0)
1277 printf("RCP(0)\n");
1278 else if (IS_INF_OR_NAN(a[0]))
1279 printf("RCP(inf)\n");
Brian13e3b212007-02-22 16:09:40 -07001280 }
Briane80d9012007-02-23 16:53:24 -07001281 result[0] = result[1] = result[2] = result[3] = 1.0F / a[0];
1282 store_vector4(inst, machine, result);
1283 }
1284 break;
1285 case OPCODE_RET: /* return from subroutine (conditional) */
1286 if (eval_condition(machine, inst)) {
1287 if (machine->StackDepth == 0) {
1288 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
Brian13e3b212007-02-22 16:09:40 -07001289 }
Briana0275b02007-03-27 11:02:20 -06001290 /* subtract one because of pc++ in the for loop */
1291 pc = machine->CallStack[--machine->StackDepth] - 1;
Briane80d9012007-02-23 16:53:24 -07001292 }
1293 break;
1294 case OPCODE_RFL: /* reflection vector */
1295 {
1296 GLfloat axis[4], dir[4], result[4], tmpX, tmpW;
Brian33eac562007-02-25 18:52:41 -07001297 fetch_vector4(&inst->SrcReg[0], machine, axis);
1298 fetch_vector4(&inst->SrcReg[1], machine, dir);
Briane80d9012007-02-23 16:53:24 -07001299 tmpW = DOT3(axis, axis);
1300 tmpX = (2.0F * DOT3(axis, dir)) / tmpW;
1301 result[0] = tmpX * axis[0] - dir[0];
1302 result[1] = tmpX * axis[1] - dir[1];
1303 result[2] = tmpX * axis[2] - dir[2];
1304 /* result[3] is never written! XXX enforce in parser! */
1305 store_vector4(inst, machine, result);
1306 }
1307 break;
1308 case OPCODE_RSQ: /* 1 / sqrt() */
1309 {
1310 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001311 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001312 a[0] = FABSF(a[0]);
1313 result[0] = result[1] = result[2] = result[3] = INV_SQRTF(a[0]);
1314 store_vector4(inst, machine, result);
1315 if (DEBUG_PROG) {
1316 printf("RSQ %g = 1/sqrt(|%g|)\n", result[0], a[0]);
Brian13e3b212007-02-22 16:09:40 -07001317 }
Briane80d9012007-02-23 16:53:24 -07001318 }
1319 break;
1320 case OPCODE_SCS: /* sine and cos */
1321 {
1322 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001323 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001324 result[0] = (GLfloat) _mesa_cos(a[0]);
1325 result[1] = (GLfloat) _mesa_sin(a[0]);
1326 result[2] = 0.0; /* undefined! */
1327 result[3] = 0.0; /* undefined! */
1328 store_vector4(inst, machine, result);
1329 }
1330 break;
1331 case OPCODE_SEQ: /* set on equal */
1332 {
1333 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001334 fetch_vector4(&inst->SrcReg[0], machine, a);
1335 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001336 result[0] = (a[0] == b[0]) ? 1.0F : 0.0F;
1337 result[1] = (a[1] == b[1]) ? 1.0F : 0.0F;
1338 result[2] = (a[2] == b[2]) ? 1.0F : 0.0F;
1339 result[3] = (a[3] == b[3]) ? 1.0F : 0.0F;
1340 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001341 if (DEBUG_PROG) {
1342 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1343 result[0], result[1], result[2], result[3],
1344 a[0], a[1], a[2], a[3],
1345 b[0], b[1], b[2], b[3]);
1346 }
Briane80d9012007-02-23 16:53:24 -07001347 }
1348 break;
1349 case OPCODE_SFL: /* set false, operands ignored */
1350 {
1351 static const GLfloat result[4] = { 0.0F, 0.0F, 0.0F, 0.0F };
1352 store_vector4(inst, machine, result);
1353 }
1354 break;
1355 case OPCODE_SGE: /* set on greater or equal */
1356 {
1357 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001358 fetch_vector4(&inst->SrcReg[0], machine, a);
1359 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001360 result[0] = (a[0] >= b[0]) ? 1.0F : 0.0F;
1361 result[1] = (a[1] >= b[1]) ? 1.0F : 0.0F;
1362 result[2] = (a[2] >= b[2]) ? 1.0F : 0.0F;
1363 result[3] = (a[3] >= b[3]) ? 1.0F : 0.0F;
1364 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001365 if (DEBUG_PROG) {
1366 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1367 result[0], result[1], result[2], result[3],
1368 a[0], a[1], a[2], a[3],
1369 b[0], b[1], b[2], b[3]);
1370 }
Briane80d9012007-02-23 16:53:24 -07001371 }
1372 break;
1373 case OPCODE_SGT: /* set on greater */
1374 {
1375 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001376 fetch_vector4(&inst->SrcReg[0], machine, a);
1377 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001378 result[0] = (a[0] > b[0]) ? 1.0F : 0.0F;
1379 result[1] = (a[1] > b[1]) ? 1.0F : 0.0F;
1380 result[2] = (a[2] > b[2]) ? 1.0F : 0.0F;
1381 result[3] = (a[3] > b[3]) ? 1.0F : 0.0F;
1382 store_vector4(inst, machine, result);
1383 if (DEBUG_PROG) {
Brian28ab1122007-03-06 12:15:30 -07001384 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1385 result[0], result[1], result[2], result[3],
1386 a[0], a[1], a[2], a[3],
1387 b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001388 }
Briane80d9012007-02-23 16:53:24 -07001389 }
1390 break;
1391 case OPCODE_SIN:
1392 {
1393 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001394 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001395 result[0] = result[1] = result[2] = result[3]
1396 = (GLfloat) _mesa_sin(a[0]);
1397 store_vector4(inst, machine, result);
1398 }
1399 break;
1400 case OPCODE_SLE: /* set on less or equal */
1401 {
1402 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001403 fetch_vector4(&inst->SrcReg[0], machine, a);
1404 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001405 result[0] = (a[0] <= b[0]) ? 1.0F : 0.0F;
1406 result[1] = (a[1] <= b[1]) ? 1.0F : 0.0F;
1407 result[2] = (a[2] <= b[2]) ? 1.0F : 0.0F;
1408 result[3] = (a[3] <= b[3]) ? 1.0F : 0.0F;
1409 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001410 if (DEBUG_PROG) {
1411 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1412 result[0], result[1], result[2], result[3],
1413 a[0], a[1], a[2], a[3],
1414 b[0], b[1], b[2], b[3]);
1415 }
Briane80d9012007-02-23 16:53:24 -07001416 }
1417 break;
1418 case OPCODE_SLT: /* set on less */
1419 {
1420 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001421 fetch_vector4(&inst->SrcReg[0], machine, a);
1422 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001423 result[0] = (a[0] < b[0]) ? 1.0F : 0.0F;
1424 result[1] = (a[1] < b[1]) ? 1.0F : 0.0F;
1425 result[2] = (a[2] < b[2]) ? 1.0F : 0.0F;
1426 result[3] = (a[3] < b[3]) ? 1.0F : 0.0F;
1427 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001428 if (DEBUG_PROG) {
1429 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1430 result[0], result[1], result[2], result[3],
1431 a[0], a[1], a[2], a[3],
1432 b[0], b[1], b[2], b[3]);
1433 }
Briane80d9012007-02-23 16:53:24 -07001434 }
1435 break;
1436 case OPCODE_SNE: /* set on not equal */
1437 {
1438 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001439 fetch_vector4(&inst->SrcReg[0], machine, a);
1440 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001441 result[0] = (a[0] != b[0]) ? 1.0F : 0.0F;
1442 result[1] = (a[1] != b[1]) ? 1.0F : 0.0F;
1443 result[2] = (a[2] != b[2]) ? 1.0F : 0.0F;
1444 result[3] = (a[3] != b[3]) ? 1.0F : 0.0F;
1445 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001446 if (DEBUG_PROG) {
1447 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1448 result[0], result[1], result[2], result[3],
1449 a[0], a[1], a[2], a[3],
1450 b[0], b[1], b[2], b[3]);
1451 }
Briane80d9012007-02-23 16:53:24 -07001452 }
1453 break;
1454 case OPCODE_STR: /* set true, operands ignored */
1455 {
1456 static const GLfloat result[4] = { 1.0F, 1.0F, 1.0F, 1.0F };
1457 store_vector4(inst, machine, result);
1458 }
1459 break;
1460 case OPCODE_SUB:
1461 {
1462 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001463 fetch_vector4(&inst->SrcReg[0], machine, a);
1464 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001465 result[0] = a[0] - b[0];
1466 result[1] = a[1] - b[1];
1467 result[2] = a[2] - b[2];
1468 result[3] = a[3] - b[3];
1469 store_vector4(inst, machine, result);
1470 if (DEBUG_PROG) {
1471 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1472 result[0], result[1], result[2], result[3],
1473 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001474 }
Briane80d9012007-02-23 16:53:24 -07001475 }
1476 break;
1477 case OPCODE_SWZ: /* extended swizzle */
1478 {
1479 const struct prog_src_register *source = &inst->SrcReg[0];
Brian33eac562007-02-25 18:52:41 -07001480 const GLfloat *src = get_register_pointer(source, machine);
Briane80d9012007-02-23 16:53:24 -07001481 GLfloat result[4];
1482 GLuint i;
1483 for (i = 0; i < 4; i++) {
1484 const GLuint swz = GET_SWZ(source->Swizzle, i);
1485 if (swz == SWIZZLE_ZERO)
1486 result[i] = 0.0;
1487 else if (swz == SWIZZLE_ONE)
1488 result[i] = 1.0;
Brian13e3b212007-02-22 16:09:40 -07001489 else {
Briane80d9012007-02-23 16:53:24 -07001490 ASSERT(swz >= 0);
1491 ASSERT(swz <= 3);
1492 result[i] = src[swz];
Brian13e3b212007-02-22 16:09:40 -07001493 }
Briane80d9012007-02-23 16:53:24 -07001494 if (source->NegateBase & (1 << i))
1495 result[i] = -result[i];
Brian13e3b212007-02-22 16:09:40 -07001496 }
Briane80d9012007-02-23 16:53:24 -07001497 store_vector4(inst, machine, result);
1498 }
1499 break;
1500 case OPCODE_TEX: /* Both ARB and NV frag prog */
1501 /* Texel lookup */
1502 {
1503 /* Note: only use the precomputed lambda value when we're
1504 * sampling texture unit [K] with texcoord[K].
1505 * Otherwise, the lambda value may have no relation to the
1506 * instruction's texcoord or texture image. Using the wrong
1507 * lambda is usually bad news.
1508 * The rest of the time, just use zero (until we get a more
1509 * sophisticated way of computing lambda).
1510 */
1511 GLfloat coord[4], color[4], lambda;
1512#if 0
1513 if (inst->SrcReg[0].File == PROGRAM_INPUT &&
1514 inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0 + inst->TexSrcUnit)
1515 lambda = span->array->lambda[inst->TexSrcUnit][column];
1516 else
1517#endif
1518 lambda = 0.0;
Brian33eac562007-02-25 18:52:41 -07001519 fetch_vector4(&inst->SrcReg[0], machine, coord);
Briane80d9012007-02-23 16:53:24 -07001520 machine->FetchTexelLod(ctx, coord, lambda, inst->TexSrcUnit,
1521 color);
1522 if (DEBUG_PROG) {
1523 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g], "
1524 "lod %f\n",
1525 color[0], color[1], color[2], color[3],
1526 inst->TexSrcUnit,
1527 coord[0], coord[1], coord[2], coord[3], lambda);
1528 }
1529 store_vector4(inst, machine, color);
1530 }
1531 break;
1532 case OPCODE_TXB: /* GL_ARB_fragment_program only */
1533 /* Texel lookup with LOD bias */
1534 {
1535 const struct gl_texture_unit *texUnit
1536 = &ctx->Texture.Unit[inst->TexSrcUnit];
1537 GLfloat coord[4], color[4], lambda, bias;
1538#if 0
1539 if (inst->SrcReg[0].File == PROGRAM_INPUT &&
1540 inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0 + inst->TexSrcUnit)
1541 lambda = span->array->lambda[inst->TexSrcUnit][column];
1542 else
1543#endif
1544 lambda = 0.0;
Brian33eac562007-02-25 18:52:41 -07001545 fetch_vector4(&inst->SrcReg[0], machine, coord);
Briane80d9012007-02-23 16:53:24 -07001546 /* coord[3] is the bias to add to lambda */
1547 bias = texUnit->LodBias + coord[3];
1548 if (texUnit->_Current)
1549 bias += texUnit->_Current->LodBias;
1550 machine->FetchTexelLod(ctx, coord, lambda + bias,
1551 inst->TexSrcUnit, color);
1552 store_vector4(inst, machine, color);
1553 }
1554 break;
1555 case OPCODE_TXD: /* GL_NV_fragment_program only */
1556 /* Texture lookup w/ partial derivatives for LOD */
1557 {
1558 GLfloat texcoord[4], dtdx[4], dtdy[4], color[4];
Brian33eac562007-02-25 18:52:41 -07001559 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1560 fetch_vector4(&inst->SrcReg[1], machine, dtdx);
1561 fetch_vector4(&inst->SrcReg[2], machine, dtdy);
Briane80d9012007-02-23 16:53:24 -07001562 machine->FetchTexelDeriv(ctx, texcoord, dtdx, dtdy,
1563 inst->TexSrcUnit, color);
1564 store_vector4(inst, machine, color);
1565 }
1566 break;
1567 case OPCODE_TXP: /* GL_ARB_fragment_program only */
1568 /* Texture lookup w/ projective divide */
1569 {
1570 GLfloat texcoord[4], color[4], lambda;
1571#if 0
1572 if (inst->SrcReg[0].File == PROGRAM_INPUT &&
1573 inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0 + inst->TexSrcUnit)
1574 lambda = span->array->lambda[inst->TexSrcUnit][column];
1575 else
1576#endif
1577 lambda = 0.0;
Brian33eac562007-02-25 18:52:41 -07001578 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
Briane80d9012007-02-23 16:53:24 -07001579 /* Not so sure about this test - if texcoord[3] is
1580 * zero, we'd probably be fine except for an ASSERT in
1581 * IROUND_POS() which gets triggered by the inf values created.
1582 */
1583 if (texcoord[3] != 0.0) {
1584 texcoord[0] /= texcoord[3];
1585 texcoord[1] /= texcoord[3];
1586 texcoord[2] /= texcoord[3];
1587 }
1588 machine->FetchTexelLod(ctx, texcoord, lambda,
1589 inst->TexSrcUnit, color);
1590 store_vector4(inst, machine, color);
1591 }
1592 break;
1593 case OPCODE_TXP_NV: /* GL_NV_fragment_program only */
1594 /* Texture lookup w/ projective divide */
1595 {
1596 GLfloat texcoord[4], color[4], lambda;
1597#if 0
1598 if (inst->SrcReg[0].File == PROGRAM_INPUT &&
1599 inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0 + inst->TexSrcUnit)
1600 lambda = span->array->lambda[inst->TexSrcUnit][column];
1601 else
1602#endif
1603 lambda = 0.0;
Brian33eac562007-02-25 18:52:41 -07001604 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
Briane80d9012007-02-23 16:53:24 -07001605 if (inst->TexSrcTarget != TEXTURE_CUBE_INDEX &&
1606 texcoord[3] != 0.0) {
1607 texcoord[0] /= texcoord[3];
1608 texcoord[1] /= texcoord[3];
1609 texcoord[2] /= texcoord[3];
1610 }
1611 machine->FetchTexelLod(ctx, texcoord, lambda,
1612 inst->TexSrcUnit, color);
1613 store_vector4(inst, machine, color);
1614 }
1615 break;
1616 case OPCODE_UP2H: /* unpack two 16-bit floats */
1617 {
1618 GLfloat a[4], result[4];
1619 const GLuint *rawBits = (const GLuint *) a;
1620 GLhalfNV hx, hy;
Brian33eac562007-02-25 18:52:41 -07001621 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001622 hx = rawBits[0] & 0xffff;
1623 hy = rawBits[0] >> 16;
1624 result[0] = result[2] = _mesa_half_to_float(hx);
1625 result[1] = result[3] = _mesa_half_to_float(hy);
1626 store_vector4(inst, machine, result);
1627 }
1628 break;
1629 case OPCODE_UP2US: /* unpack two GLushorts */
1630 {
1631 GLfloat a[4], result[4];
1632 const GLuint *rawBits = (const GLuint *) a;
1633 GLushort usx, usy;
Brian33eac562007-02-25 18:52:41 -07001634 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001635 usx = rawBits[0] & 0xffff;
1636 usy = rawBits[0] >> 16;
1637 result[0] = result[2] = usx * (1.0f / 65535.0f);
1638 result[1] = result[3] = usy * (1.0f / 65535.0f);
1639 store_vector4(inst, machine, result);
1640 }
1641 break;
1642 case OPCODE_UP4B: /* unpack four GLbytes */
1643 {
1644 GLfloat a[4], result[4];
1645 const GLuint *rawBits = (const GLuint *) a;
Brian33eac562007-02-25 18:52:41 -07001646 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001647 result[0] = (((rawBits[0] >> 0) & 0xff) - 128) / 127.0F;
1648 result[1] = (((rawBits[0] >> 8) & 0xff) - 128) / 127.0F;
1649 result[2] = (((rawBits[0] >> 16) & 0xff) - 128) / 127.0F;
1650 result[3] = (((rawBits[0] >> 24) & 0xff) - 128) / 127.0F;
1651 store_vector4(inst, machine, result);
1652 }
1653 break;
1654 case OPCODE_UP4UB: /* unpack four GLubytes */
1655 {
1656 GLfloat a[4], result[4];
1657 const GLuint *rawBits = (const GLuint *) a;
Brian33eac562007-02-25 18:52:41 -07001658 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001659 result[0] = ((rawBits[0] >> 0) & 0xff) / 255.0F;
1660 result[1] = ((rawBits[0] >> 8) & 0xff) / 255.0F;
1661 result[2] = ((rawBits[0] >> 16) & 0xff) / 255.0F;
1662 result[3] = ((rawBits[0] >> 24) & 0xff) / 255.0F;
1663 store_vector4(inst, machine, result);
1664 }
1665 break;
1666 case OPCODE_XPD: /* cross product */
1667 {
1668 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001669 fetch_vector4(&inst->SrcReg[0], machine, a);
1670 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001671 result[0] = a[1] * b[2] - a[2] * b[1];
1672 result[1] = a[2] * b[0] - a[0] * b[2];
1673 result[2] = a[0] * b[1] - a[1] * b[0];
1674 result[3] = 1.0;
1675 store_vector4(inst, machine, result);
Brian9637c962007-03-07 17:40:57 -07001676 if (DEBUG_PROG) {
1677 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1678 result[0], result[1], result[2], result[3],
1679 a[0], a[1], a[2], b[0], b[1], b[2]);
1680 }
Briane80d9012007-02-23 16:53:24 -07001681 }
1682 break;
1683 case OPCODE_X2D: /* 2-D matrix transform */
1684 {
1685 GLfloat a[4], b[4], c[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);
1688 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -07001689 result[0] = a[0] + b[0] * c[0] + b[1] * c[1];
1690 result[1] = a[1] + b[0] * c[2] + b[1] * c[3];
1691 result[2] = a[2] + b[0] * c[0] + b[1] * c[1];
1692 result[3] = a[3] + b[0] * c[2] + b[1] * c[3];
1693 store_vector4(inst, machine, result);
1694 }
1695 break;
1696 case OPCODE_PRINT:
1697 {
1698 if (inst->SrcReg[0].File != -1) {
1699 GLfloat a[4];
Brian33eac562007-02-25 18:52:41 -07001700 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001701 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst->Data,
1702 a[0], a[1], a[2], a[3]);
1703 }
1704 else {
1705 _mesa_printf("%s\n", (const char *) inst->Data);
1706 }
1707 }
1708 break;
1709 case OPCODE_END:
1710 return GL_TRUE;
1711 default:
1712 _mesa_problem(ctx, "Bad opcode %d in _mesa_exec_fragment_program",
1713 inst->Opcode);
1714 return GL_TRUE; /* return value doesn't matter */
Brian13e3b212007-02-22 16:09:40 -07001715
1716 }
Briane80d9012007-02-23 16:53:24 -07001717
Briancfd00112007-02-25 18:30:45 -07001718 numExec++;
1719 if (numExec > maxExec) {
Brian13e3b212007-02-22 16:09:40 -07001720 _mesa_problem(ctx, "Infinite loop detected in fragment program");
1721 return GL_TRUE;
Brian13e3b212007-02-22 16:09:40 -07001722 }
Briane80d9012007-02-23 16:53:24 -07001723
1724 } /* for pc */
Brian13e3b212007-02-22 16:09:40 -07001725
1726#if FEATURE_MESA_program_debug
1727 CurrentMachine = NULL;
1728#endif
1729
1730 return GL_TRUE;
1731}