blob: 8163ae6a6f5ec5795e7c2dd5f773e8294671b63f [file] [log] [blame]
Brian13e3b212007-02-22 16:09:40 -07001/*
2 * Mesa 3-D graphics library
Brian Paulf4361542008-11-11 10:47:10 -07003 * Version: 7.3
Brian13e3b212007-02-22 16:09:40 -07004 *
Brian Paulf4361542008-11-11 10:47:10 -07005 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
Brian13e3b212007-02-22 16:09:40 -07006 *
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
Brian Paulbbd28712008-09-18 12:26:54 -060038#include "main/glheader.h"
39#include "main/colormac.h"
40#include "main/context.h"
Brian13e3b212007-02-22 16:09:40 -070041#include "program.h"
42#include "prog_execute.h"
43#include "prog_instruction.h"
44#include "prog_parameter.h"
45#include "prog_print.h"
Brian Paul702b5b02008-12-15 18:37:39 -070046#include "prog_noise.h"
Brian13e3b212007-02-22 16:09:40 -070047
48
Brian13e3b212007-02-22 16:09:40 -070049/* debug predicate */
50#define DEBUG_PROG 0
51
52
Brianf183a2d2007-02-23 17:14:30 -070053/**
54 * Set x to positive or negative infinity.
55 */
56#if defined(USE_IEEE) || defined(_WIN32)
57#define SET_POS_INFINITY(x) ( *((GLuint *) (void *)&x) = 0x7F800000 )
58#define SET_NEG_INFINITY(x) ( *((GLuint *) (void *)&x) = 0xFF800000 )
59#elif defined(VMS)
60#define SET_POS_INFINITY(x) x = __MAXFLOAT
61#define SET_NEG_INFINITY(x) x = -__MAXFLOAT
62#else
63#define SET_POS_INFINITY(x) x = (GLfloat) HUGE_VAL
64#define SET_NEG_INFINITY(x) x = (GLfloat) -HUGE_VAL
65#endif
66
67#define SET_FLOAT_BITS(x, bits) ((fi_type *) (void *) &(x))->i = bits
68
69
70static const GLfloat ZeroVec[4] = { 0.0F, 0.0F, 0.0F, 0.0F };
71
72
73
Brian13e3b212007-02-22 16:09:40 -070074/**
75 * Return a pointer to the 4-element float vector specified by the given
76 * source register.
77 */
78static INLINE const GLfloat *
Brian Paulf4361542008-11-11 10:47:10 -070079get_src_register_pointer(const struct prog_src_register *source,
80 const struct gl_program_machine *machine)
Brian13e3b212007-02-22 16:09:40 -070081{
Brian Paulf4361542008-11-11 10:47:10 -070082 const struct gl_program *prog = machine->CurProgram;
83 GLint reg = source->Index;
84
Brianf183a2d2007-02-23 17:14:30 -070085 if (source->RelAddr) {
Brian Paulf4361542008-11-11 10:47:10 -070086 /* add address register value to src index/offset */
87 reg += machine->AddressReg[0][0];
88 if (reg < 0) {
89 return ZeroVec;
Brianf183a2d2007-02-23 17:14:30 -070090 }
91 }
92
Brian13e3b212007-02-22 16:09:40 -070093 switch (source->File) {
94 case PROGRAM_TEMPORARY:
Brian Paulf4361542008-11-11 10:47:10 -070095 if (reg >= MAX_PROGRAM_TEMPS)
96 return ZeroVec;
97 return machine->Temporaries[reg];
Brian13e3b212007-02-22 16:09:40 -070098
99 case PROGRAM_INPUT:
Brian Paulf4361542008-11-11 10:47:10 -0700100 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
101 if (reg >= VERT_ATTRIB_MAX)
102 return ZeroVec;
103 return machine->VertAttribs[reg];
Brian13e3b212007-02-22 16:09:40 -0700104 }
105 else {
Brian Paulf4361542008-11-11 10:47:10 -0700106 if (reg >= FRAG_ATTRIB_MAX)
107 return ZeroVec;
108 return machine->Attribs[reg][machine->CurElement];
Brian13e3b212007-02-22 16:09:40 -0700109 }
110
111 case PROGRAM_OUTPUT:
Brian Paulf4361542008-11-11 10:47:10 -0700112 if (reg >= MAX_PROGRAM_OUTPUTS)
113 return ZeroVec;
114 return machine->Outputs[reg];
Brian13e3b212007-02-22 16:09:40 -0700115
116 case PROGRAM_LOCAL_PARAM:
Brian Paulf4361542008-11-11 10:47:10 -0700117 if (reg >= MAX_PROGRAM_LOCAL_PARAMS)
118 return ZeroVec;
119 return machine->CurProgram->LocalParams[reg];
Brian13e3b212007-02-22 16:09:40 -0700120
121 case PROGRAM_ENV_PARAM:
Brian Paulf4361542008-11-11 10:47:10 -0700122 if (reg >= MAX_PROGRAM_ENV_PARAMS)
123 return ZeroVec;
124 return machine->EnvParams[reg];
Brian13e3b212007-02-22 16:09:40 -0700125
126 case PROGRAM_STATE_VAR:
127 /* Fallthrough */
128 case PROGRAM_CONSTANT:
129 /* Fallthrough */
130 case PROGRAM_UNIFORM:
131 /* Fallthrough */
132 case PROGRAM_NAMED_PARAM:
Brian Paulf4361542008-11-11 10:47:10 -0700133 if (reg >= (GLint) prog->Parameters->NumParameters)
134 return ZeroVec;
135 return prog->Parameters->ParameterValues[reg];
Brian13e3b212007-02-22 16:09:40 -0700136
137 default:
Brian33eac562007-02-25 18:52:41 -0700138 _mesa_problem(NULL,
Brian Paulf4361542008-11-11 10:47:10 -0700139 "Invalid src register file %d in get_src_register_pointer()",
140 source->File);
Brian13e3b212007-02-22 16:09:40 -0700141 return NULL;
142 }
143}
144
145
Brian Paulf4361542008-11-11 10:47:10 -0700146/**
147 * Return a pointer to the 4-element float vector specified by the given
148 * destination register.
149 */
150static INLINE GLfloat *
151get_dst_register_pointer(const struct prog_dst_register *dest,
152 struct gl_program_machine *machine)
153{
154 static GLfloat dummyReg[4];
155 GLint reg = dest->Index;
156
157 if (dest->RelAddr) {
158 /* add address register value to src index/offset */
159 reg += machine->AddressReg[0][0];
160 if (reg < 0) {
161 return dummyReg;
162 }
163 }
164
165 switch (dest->File) {
166 case PROGRAM_TEMPORARY:
167 if (reg >= MAX_PROGRAM_TEMPS)
168 return dummyReg;
169 return machine->Temporaries[reg];
170
171 case PROGRAM_OUTPUT:
172 if (reg >= MAX_PROGRAM_OUTPUTS)
173 return dummyReg;
174 return machine->Outputs[reg];
175
176 case PROGRAM_WRITE_ONLY:
177 return dummyReg;
178
179 default:
180 _mesa_problem(NULL,
181 "Invalid dest register file %d in get_dst_register_pointer()",
182 dest->File);
183 return NULL;
184 }
185}
186
187
188
Brian6774f322007-02-25 18:39:46 -0700189#if FEATURE_MESA_program_debug
190static struct gl_program_machine *CurrentMachine = NULL;
191
192/**
193 * For GL_MESA_program_debug.
194 * Return current value (4*GLfloat) of a program register.
195 * Called via ctx->Driver.GetProgramRegister().
196 */
197void
198_mesa_get_program_register(GLcontext *ctx, enum register_file file,
199 GLuint index, GLfloat val[4])
200{
201 if (CurrentMachine) {
Brian Paulf4361542008-11-11 10:47:10 -0700202 struct prog_src_register srcReg;
203 const GLfloat *src;
204 srcReg.File = file;
205 srcReg.Index = index;
206 src = get_src_register_pointer(&srcReg, CurrentMachine);
207 COPY_4V(val, src);
Brian6774f322007-02-25 18:39:46 -0700208 }
209}
210#endif /* FEATURE_MESA_program_debug */
211
212
Brian13e3b212007-02-22 16:09:40 -0700213/**
214 * Fetch a 4-element float vector from the given source register.
215 * Apply swizzling and negating as needed.
216 */
217static void
Brian33eac562007-02-25 18:52:41 -0700218fetch_vector4(const struct prog_src_register *source,
Briane80d9012007-02-23 16:53:24 -0700219 const struct gl_program_machine *machine, GLfloat result[4])
Brian13e3b212007-02-22 16:09:40 -0700220{
Brian Paulf4361542008-11-11 10:47:10 -0700221 const GLfloat *src = get_src_register_pointer(source, machine);
Brian13e3b212007-02-22 16:09:40 -0700222 ASSERT(src);
223
224 if (source->Swizzle == SWIZZLE_NOOP) {
225 /* no swizzling */
226 COPY_4V(result, src);
227 }
228 else {
229 ASSERT(GET_SWZ(source->Swizzle, 0) <= 3);
230 ASSERT(GET_SWZ(source->Swizzle, 1) <= 3);
231 ASSERT(GET_SWZ(source->Swizzle, 2) <= 3);
232 ASSERT(GET_SWZ(source->Swizzle, 3) <= 3);
233 result[0] = src[GET_SWZ(source->Swizzle, 0)];
234 result[1] = src[GET_SWZ(source->Swizzle, 1)];
235 result[2] = src[GET_SWZ(source->Swizzle, 2)];
236 result[3] = src[GET_SWZ(source->Swizzle, 3)];
237 }
238
239 if (source->NegateBase) {
240 result[0] = -result[0];
241 result[1] = -result[1];
242 result[2] = -result[2];
243 result[3] = -result[3];
244 }
245 if (source->Abs) {
246 result[0] = FABSF(result[0]);
247 result[1] = FABSF(result[1]);
248 result[2] = FABSF(result[2]);
249 result[3] = FABSF(result[3]);
250 }
251 if (source->NegateAbs) {
252 result[0] = -result[0];
253 result[1] = -result[1];
254 result[2] = -result[2];
255 result[3] = -result[3];
256 }
257}
258
Brian62da6a12007-05-02 18:44:34 -0600259
Brian13e3b212007-02-22 16:09:40 -0700260/**
Brian Paul37eef7b2008-11-07 09:33:55 -0700261 * Fetch a 4-element uint vector from the given source register.
262 * Apply swizzling but not negation/abs.
263 */
264static void
265fetch_vector4ui(const struct prog_src_register *source,
266 const struct gl_program_machine *machine, GLuint result[4])
267{
Brian Paulf4361542008-11-11 10:47:10 -0700268 const GLuint *src = (GLuint *) get_src_register_pointer(source, machine);
Brian Paul37eef7b2008-11-07 09:33:55 -0700269 ASSERT(src);
270
271 if (source->Swizzle == SWIZZLE_NOOP) {
272 /* no swizzling */
273 COPY_4V(result, src);
274 }
275 else {
276 ASSERT(GET_SWZ(source->Swizzle, 0) <= 3);
277 ASSERT(GET_SWZ(source->Swizzle, 1) <= 3);
278 ASSERT(GET_SWZ(source->Swizzle, 2) <= 3);
279 ASSERT(GET_SWZ(source->Swizzle, 3) <= 3);
280 result[0] = src[GET_SWZ(source->Swizzle, 0)];
281 result[1] = src[GET_SWZ(source->Swizzle, 1)];
282 result[2] = src[GET_SWZ(source->Swizzle, 2)];
283 result[3] = src[GET_SWZ(source->Swizzle, 3)];
284 }
285
286 /* Note: no NegateBase, Abs, NegateAbs here */
287}
288
289
290
291/**
Brian62da6a12007-05-02 18:44:34 -0600292 * Fetch the derivative with respect to X or Y for the given register.
293 * XXX this currently only works for fragment program input attribs.
Brian13e3b212007-02-22 16:09:40 -0700294 */
Brian62da6a12007-05-02 18:44:34 -0600295static void
Briane80d9012007-02-23 16:53:24 -0700296fetch_vector4_deriv(GLcontext * ctx,
297 const struct prog_src_register *source,
Brian62da6a12007-05-02 18:44:34 -0600298 const struct gl_program_machine *machine,
299 char xOrY, GLfloat result[4])
Brian13e3b212007-02-22 16:09:40 -0700300{
Brian Paul37eef7b2008-11-07 09:33:55 -0700301 if (source->File == PROGRAM_INPUT &&
302 source->Index < (GLint) machine->NumDeriv) {
Brian62da6a12007-05-02 18:44:34 -0600303 const GLint col = machine->CurElement;
304 const GLfloat w = machine->Attribs[FRAG_ATTRIB_WPOS][col][3];
305 const GLfloat invQ = 1.0f / w;
306 GLfloat deriv[4];
Brian13e3b212007-02-22 16:09:40 -0700307
Brian13e3b212007-02-22 16:09:40 -0700308 if (xOrY == 'X') {
Brian62da6a12007-05-02 18:44:34 -0600309 deriv[0] = machine->DerivX[source->Index][0] * invQ;
310 deriv[1] = machine->DerivX[source->Index][1] * invQ;
311 deriv[2] = machine->DerivX[source->Index][2] * invQ;
312 deriv[3] = machine->DerivX[source->Index][3] * invQ;
Brian13e3b212007-02-22 16:09:40 -0700313 }
314 else {
Brian62da6a12007-05-02 18:44:34 -0600315 deriv[0] = machine->DerivY[source->Index][0] * invQ;
316 deriv[1] = machine->DerivY[source->Index][1] * invQ;
317 deriv[2] = machine->DerivY[source->Index][2] * invQ;
318 deriv[3] = machine->DerivY[source->Index][3] * invQ;
Brian13e3b212007-02-22 16:09:40 -0700319 }
Brian13e3b212007-02-22 16:09:40 -0700320
Brian62da6a12007-05-02 18:44:34 -0600321 result[0] = deriv[GET_SWZ(source->Swizzle, 0)];
322 result[1] = deriv[GET_SWZ(source->Swizzle, 1)];
323 result[2] = deriv[GET_SWZ(source->Swizzle, 2)];
324 result[3] = deriv[GET_SWZ(source->Swizzle, 3)];
325
326 if (source->NegateBase) {
327 result[0] = -result[0];
328 result[1] = -result[1];
329 result[2] = -result[2];
330 result[3] = -result[3];
331 }
332 if (source->Abs) {
333 result[0] = FABSF(result[0]);
334 result[1] = FABSF(result[1]);
335 result[2] = FABSF(result[2]);
336 result[3] = FABSF(result[3]);
337 }
338 if (source->NegateAbs) {
339 result[0] = -result[0];
340 result[1] = -result[1];
341 result[2] = -result[2];
342 result[3] = -result[3];
343 }
Brian13e3b212007-02-22 16:09:40 -0700344 }
Brian62da6a12007-05-02 18:44:34 -0600345 else {
346 ASSIGN_4V(result, 0.0, 0.0, 0.0, 0.0);
Brian13e3b212007-02-22 16:09:40 -0700347 }
Brian13e3b212007-02-22 16:09:40 -0700348}
Brian13e3b212007-02-22 16:09:40 -0700349
350
351/**
352 * As above, but only return result[0] element.
353 */
354static void
Brian33eac562007-02-25 18:52:41 -0700355fetch_vector1(const struct prog_src_register *source,
Briane80d9012007-02-23 16:53:24 -0700356 const struct gl_program_machine *machine, GLfloat result[4])
Brian13e3b212007-02-22 16:09:40 -0700357{
Brian Paulf4361542008-11-11 10:47:10 -0700358 const GLfloat *src = get_src_register_pointer(source, machine);
Brian13e3b212007-02-22 16:09:40 -0700359 ASSERT(src);
360
361 result[0] = src[GET_SWZ(source->Swizzle, 0)];
362
363 if (source->NegateBase) {
364 result[0] = -result[0];
365 }
366 if (source->Abs) {
367 result[0] = FABSF(result[0]);
368 }
369 if (source->NegateAbs) {
370 result[0] = -result[0];
371 }
372}
373
374
375/**
Brian999b5562007-11-23 12:01:57 -0700376 * Fetch texel from texture. Use partial derivatives when possible.
377 */
378static INLINE void
379fetch_texel(GLcontext *ctx,
380 const struct gl_program_machine *machine,
381 const struct prog_instruction *inst,
382 const GLfloat texcoord[4], GLfloat lodBias,
383 GLfloat color[4])
384{
Brian Paulade50832008-05-14 16:09:46 -0600385 const GLuint unit = machine->Samplers[inst->TexSrcUnit];
386
Brian999b5562007-11-23 12:01:57 -0700387 /* Note: we only have the right derivatives for fragment input attribs.
388 */
389 if (machine->NumDeriv > 0 &&
390 inst->SrcReg[0].File == PROGRAM_INPUT &&
391 inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0 + inst->TexSrcUnit) {
392 /* simple texture fetch for which we should have derivatives */
393 GLuint attr = inst->SrcReg[0].Index;
394 machine->FetchTexelDeriv(ctx, texcoord,
395 machine->DerivX[attr],
396 machine->DerivY[attr],
Brian Paulade50832008-05-14 16:09:46 -0600397 lodBias, unit, color);
Brian999b5562007-11-23 12:01:57 -0700398 }
399 else {
Brian Paulade50832008-05-14 16:09:46 -0600400 machine->FetchTexelLod(ctx, texcoord, lodBias, unit, color);
Brian999b5562007-11-23 12:01:57 -0700401 }
402}
403
404
405/**
Brian13e3b212007-02-22 16:09:40 -0700406 * Test value against zero and return GT, LT, EQ or UN if NaN.
407 */
408static INLINE GLuint
Briane80d9012007-02-23 16:53:24 -0700409generate_cc(float value)
Brian13e3b212007-02-22 16:09:40 -0700410{
411 if (value != value)
Briane80d9012007-02-23 16:53:24 -0700412 return COND_UN; /* NaN */
Brian13e3b212007-02-22 16:09:40 -0700413 if (value > 0.0F)
414 return COND_GT;
415 if (value < 0.0F)
416 return COND_LT;
417 return COND_EQ;
418}
419
420
421/**
422 * Test if the ccMaskRule is satisfied by the given condition code.
423 * Used to mask destination writes according to the current condition code.
424 */
425static INLINE GLboolean
426test_cc(GLuint condCode, GLuint ccMaskRule)
427{
428 switch (ccMaskRule) {
429 case COND_EQ: return (condCode == COND_EQ);
430 case COND_NE: return (condCode != COND_EQ);
431 case COND_LT: return (condCode == COND_LT);
432 case COND_GE: return (condCode == COND_GT || condCode == COND_EQ);
433 case COND_LE: return (condCode == COND_LT || condCode == COND_EQ);
434 case COND_GT: return (condCode == COND_GT);
435 case COND_TR: return GL_TRUE;
436 case COND_FL: return GL_FALSE;
437 default: return GL_TRUE;
438 }
439}
440
441
442/**
443 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
444 * or GL_FALSE to indicate result.
445 */
446static INLINE GLboolean
447eval_condition(const struct gl_program_machine *machine,
448 const struct prog_instruction *inst)
449{
450 const GLuint swizzle = inst->DstReg.CondSwizzle;
451 const GLuint condMask = inst->DstReg.CondMask;
452 if (test_cc(machine->CondCodes[GET_SWZ(swizzle, 0)], condMask) ||
453 test_cc(machine->CondCodes[GET_SWZ(swizzle, 1)], condMask) ||
454 test_cc(machine->CondCodes[GET_SWZ(swizzle, 2)], condMask) ||
455 test_cc(machine->CondCodes[GET_SWZ(swizzle, 3)], condMask)) {
456 return GL_TRUE;
457 }
458 else {
459 return GL_FALSE;
460 }
461}
462
463
464
465/**
466 * Store 4 floats into a register. Observe the instructions saturate and
467 * set-condition-code flags.
468 */
469static void
Briane80d9012007-02-23 16:53:24 -0700470store_vector4(const struct prog_instruction *inst,
471 struct gl_program_machine *machine, const GLfloat value[4])
Brian13e3b212007-02-22 16:09:40 -0700472{
Brian Paulf4361542008-11-11 10:47:10 -0700473 const struct prog_dst_register *dstReg = &(inst->DstReg);
Brian13e3b212007-02-22 16:09:40 -0700474 const GLboolean clamp = inst->SaturateMode == SATURATE_ZERO_ONE;
Brian Paulf4361542008-11-11 10:47:10 -0700475 GLuint writeMask = dstReg->WriteMask;
Brian13e3b212007-02-22 16:09:40 -0700476 GLfloat clampedValue[4];
Brian Paulf4361542008-11-11 10:47:10 -0700477 GLfloat *dst = get_dst_register_pointer(dstReg, machine);
Brian13e3b212007-02-22 16:09:40 -0700478
479#if 0
480 if (value[0] > 1.0e10 ||
481 IS_INF_OR_NAN(value[0]) ||
482 IS_INF_OR_NAN(value[1]) ||
Briane80d9012007-02-23 16:53:24 -0700483 IS_INF_OR_NAN(value[2]) || IS_INF_OR_NAN(value[3]))
Brian13e3b212007-02-22 16:09:40 -0700484 printf("store %g %g %g %g\n", value[0], value[1], value[2], value[3]);
485#endif
486
487 if (clamp) {
488 clampedValue[0] = CLAMP(value[0], 0.0F, 1.0F);
489 clampedValue[1] = CLAMP(value[1], 0.0F, 1.0F);
490 clampedValue[2] = CLAMP(value[2], 0.0F, 1.0F);
491 clampedValue[3] = CLAMP(value[3], 0.0F, 1.0F);
492 value = clampedValue;
493 }
494
Brian Paulf4361542008-11-11 10:47:10 -0700495 if (dstReg->CondMask != COND_TR) {
Brian13e3b212007-02-22 16:09:40 -0700496 /* condition codes may turn off some writes */
497 if (writeMask & WRITEMASK_X) {
Brian Paulf4361542008-11-11 10:47:10 -0700498 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 0)],
499 dstReg->CondMask))
Brian13e3b212007-02-22 16:09:40 -0700500 writeMask &= ~WRITEMASK_X;
501 }
502 if (writeMask & WRITEMASK_Y) {
Brian Paulf4361542008-11-11 10:47:10 -0700503 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 1)],
504 dstReg->CondMask))
Brian13e3b212007-02-22 16:09:40 -0700505 writeMask &= ~WRITEMASK_Y;
506 }
507 if (writeMask & WRITEMASK_Z) {
Brian Paulf4361542008-11-11 10:47:10 -0700508 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 2)],
509 dstReg->CondMask))
Brian13e3b212007-02-22 16:09:40 -0700510 writeMask &= ~WRITEMASK_Z;
511 }
512 if (writeMask & WRITEMASK_W) {
Brian Paulf4361542008-11-11 10:47:10 -0700513 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 3)],
514 dstReg->CondMask))
Brian13e3b212007-02-22 16:09:40 -0700515 writeMask &= ~WRITEMASK_W;
516 }
517 }
518
519 if (writeMask & WRITEMASK_X)
Brian Paulf4361542008-11-11 10:47:10 -0700520 dst[0] = value[0];
Brian13e3b212007-02-22 16:09:40 -0700521 if (writeMask & WRITEMASK_Y)
Brian Paulf4361542008-11-11 10:47:10 -0700522 dst[1] = value[1];
Brian13e3b212007-02-22 16:09:40 -0700523 if (writeMask & WRITEMASK_Z)
Brian Paulf4361542008-11-11 10:47:10 -0700524 dst[2] = value[2];
Brian13e3b212007-02-22 16:09:40 -0700525 if (writeMask & WRITEMASK_W)
Brian Paulf4361542008-11-11 10:47:10 -0700526 dst[3] = value[3];
Brian13e3b212007-02-22 16:09:40 -0700527
528 if (inst->CondUpdate) {
529 if (writeMask & WRITEMASK_X)
530 machine->CondCodes[0] = generate_cc(value[0]);
531 if (writeMask & WRITEMASK_Y)
532 machine->CondCodes[1] = generate_cc(value[1]);
533 if (writeMask & WRITEMASK_Z)
534 machine->CondCodes[2] = generate_cc(value[2]);
535 if (writeMask & WRITEMASK_W)
536 machine->CondCodes[3] = generate_cc(value[3]);
Briana01616e2007-03-28 11:01:28 -0600537#if DEBUG_PROG
538 printf("CondCodes=(%s,%s,%s,%s) for:\n",
539 _mesa_condcode_string(machine->CondCodes[0]),
540 _mesa_condcode_string(machine->CondCodes[1]),
541 _mesa_condcode_string(machine->CondCodes[2]),
542 _mesa_condcode_string(machine->CondCodes[3]));
543#endif
Brian13e3b212007-02-22 16:09:40 -0700544 }
545}
546
547
Brian13e3b212007-02-22 16:09:40 -0700548/**
Brian Paul37eef7b2008-11-07 09:33:55 -0700549 * Store 4 uints into a register. Observe the set-condition-code flags.
550 */
551static void
552store_vector4ui(const struct prog_instruction *inst,
553 struct gl_program_machine *machine, const GLuint value[4])
554{
Brian Paulf4361542008-11-11 10:47:10 -0700555 const struct prog_dst_register *dstReg = &(inst->DstReg);
556 GLuint writeMask = dstReg->WriteMask;
557 GLuint *dst = (GLuint *) get_dst_register_pointer(dstReg, machine);
Brian Paul37eef7b2008-11-07 09:33:55 -0700558
Brian Paulf4361542008-11-11 10:47:10 -0700559 if (dstReg->CondMask != COND_TR) {
Brian Paul37eef7b2008-11-07 09:33:55 -0700560 /* condition codes may turn off some writes */
561 if (writeMask & WRITEMASK_X) {
Brian Paulf4361542008-11-11 10:47:10 -0700562 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 0)],
563 dstReg->CondMask))
Brian Paul37eef7b2008-11-07 09:33:55 -0700564 writeMask &= ~WRITEMASK_X;
565 }
566 if (writeMask & WRITEMASK_Y) {
Brian Paulf4361542008-11-11 10:47:10 -0700567 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 1)],
568 dstReg->CondMask))
Brian Paul37eef7b2008-11-07 09:33:55 -0700569 writeMask &= ~WRITEMASK_Y;
570 }
571 if (writeMask & WRITEMASK_Z) {
Brian Paulf4361542008-11-11 10:47:10 -0700572 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 2)],
573 dstReg->CondMask))
Brian Paul37eef7b2008-11-07 09:33:55 -0700574 writeMask &= ~WRITEMASK_Z;
575 }
576 if (writeMask & WRITEMASK_W) {
Brian Paulf4361542008-11-11 10:47:10 -0700577 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 3)],
578 dstReg->CondMask))
Brian Paul37eef7b2008-11-07 09:33:55 -0700579 writeMask &= ~WRITEMASK_W;
580 }
581 }
582
583 if (writeMask & WRITEMASK_X)
Brian Paulf4361542008-11-11 10:47:10 -0700584 dst[0] = value[0];
Brian Paul37eef7b2008-11-07 09:33:55 -0700585 if (writeMask & WRITEMASK_Y)
Brian Paulf4361542008-11-11 10:47:10 -0700586 dst[1] = value[1];
Brian Paul37eef7b2008-11-07 09:33:55 -0700587 if (writeMask & WRITEMASK_Z)
Brian Paulf4361542008-11-11 10:47:10 -0700588 dst[2] = value[2];
Brian Paul37eef7b2008-11-07 09:33:55 -0700589 if (writeMask & WRITEMASK_W)
Brian Paulf4361542008-11-11 10:47:10 -0700590 dst[3] = value[3];
Brian Paul37eef7b2008-11-07 09:33:55 -0700591
592 if (inst->CondUpdate) {
593 if (writeMask & WRITEMASK_X)
594 machine->CondCodes[0] = generate_cc(value[0]);
595 if (writeMask & WRITEMASK_Y)
596 machine->CondCodes[1] = generate_cc(value[1]);
597 if (writeMask & WRITEMASK_Z)
598 machine->CondCodes[2] = generate_cc(value[2]);
599 if (writeMask & WRITEMASK_W)
600 machine->CondCodes[3] = generate_cc(value[3]);
601#if DEBUG_PROG
602 printf("CondCodes=(%s,%s,%s,%s) for:\n",
603 _mesa_condcode_string(machine->CondCodes[0]),
604 _mesa_condcode_string(machine->CondCodes[1]),
605 _mesa_condcode_string(machine->CondCodes[2]),
606 _mesa_condcode_string(machine->CondCodes[3]));
607#endif
608 }
609}
610
611
612
613/**
Brian13e3b212007-02-22 16:09:40 -0700614 * Execute the given vertex/fragment program.
615 *
Brian3c1c9992007-02-25 19:11:44 -0700616 * \param ctx rendering context
617 * \param program the program to execute
618 * \param machine machine state (must be initialized)
Brian13e3b212007-02-22 16:09:40 -0700619 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
620 */
621GLboolean
Briane80d9012007-02-23 16:53:24 -0700622_mesa_execute_program(GLcontext * ctx,
Brian8b34b7d2007-02-25 18:26:50 -0700623 const struct gl_program *program,
Brian085d7d52007-02-25 18:23:37 -0700624 struct gl_program_machine *machine)
Brian13e3b212007-02-22 16:09:40 -0700625{
Brian8b34b7d2007-02-25 18:26:50 -0700626 const GLuint numInst = program->NumInstructions;
Briancfd00112007-02-25 18:30:45 -0700627 const GLuint maxExec = 10000;
José Fonseca452a5922008-05-31 18:14:09 +0900628 GLuint pc, numExec = 0;
Brian13e3b212007-02-22 16:09:40 -0700629
630 machine->CurProgram = program;
631
632 if (DEBUG_PROG) {
633 printf("execute program %u --------------------\n", program->Id);
634 }
635
636#if FEATURE_MESA_program_debug
637 CurrentMachine = machine;
638#endif
639
Brian33eac562007-02-25 18:52:41 -0700640 if (program->Target == GL_VERTEX_PROGRAM_ARB) {
641 machine->EnvParams = ctx->VertexProgram.Parameters;
642 }
643 else {
644 machine->EnvParams = ctx->FragmentProgram.Parameters;
645 }
646
Brian8b34b7d2007-02-25 18:26:50 -0700647 for (pc = 0; pc < numInst; pc++) {
Brian13e3b212007-02-22 16:09:40 -0700648 const struct prog_instruction *inst = program->Instructions + pc;
649
650#if FEATURE_MESA_program_debug
651 if (ctx->FragmentProgram.CallbackEnabled &&
652 ctx->FragmentProgram.Callback) {
653 ctx->FragmentProgram.CurrentPosition = inst->StringPos;
654 ctx->FragmentProgram.Callback(program->Target,
655 ctx->FragmentProgram.CallbackData);
656 }
657#endif
658
659 if (DEBUG_PROG) {
660 _mesa_print_instruction(inst);
661 }
662
663 switch (inst->Opcode) {
Briane80d9012007-02-23 16:53:24 -0700664 case OPCODE_ABS:
665 {
666 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700667 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700668 result[0] = FABSF(a[0]);
669 result[1] = FABSF(a[1]);
670 result[2] = FABSF(a[2]);
671 result[3] = FABSF(a[3]);
672 store_vector4(inst, machine, result);
673 }
674 break;
675 case OPCODE_ADD:
676 {
677 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700678 fetch_vector4(&inst->SrcReg[0], machine, a);
679 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700680 result[0] = a[0] + b[0];
681 result[1] = a[1] + b[1];
682 result[2] = a[2] + b[2];
683 result[3] = a[3] + b[3];
684 store_vector4(inst, machine, result);
685 if (DEBUG_PROG) {
686 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
687 result[0], result[1], result[2], result[3],
688 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -0700689 }
Briane80d9012007-02-23 16:53:24 -0700690 }
691 break;
Brian Paul37eef7b2008-11-07 09:33:55 -0700692 case OPCODE_AND: /* bitwise AND */
693 {
694 GLuint a[4], b[4], result[4];
695 fetch_vector4ui(&inst->SrcReg[0], machine, a);
696 fetch_vector4ui(&inst->SrcReg[1], machine, b);
697 result[0] = a[0] & b[0];
698 result[1] = a[1] & b[1];
699 result[2] = a[2] & b[2];
700 result[3] = a[3] & b[3];
701 store_vector4ui(inst, machine, result);
702 }
703 break;
Brianf183a2d2007-02-23 17:14:30 -0700704 case OPCODE_ARL:
705 {
706 GLfloat t[4];
Brian33eac562007-02-25 18:52:41 -0700707 fetch_vector4(&inst->SrcReg[0], machine, t);
Brian Paula9475cc2008-12-12 18:03:48 -0700708 machine->AddressReg[0][0] = IFLOOR(t[0]);
Brianf183a2d2007-02-23 17:14:30 -0700709 }
710 break;
Briane80d9012007-02-23 16:53:24 -0700711 case OPCODE_BGNLOOP:
712 /* no-op */
713 break;
714 case OPCODE_ENDLOOP:
715 /* subtract 1 here since pc is incremented by for(pc) loop */
716 pc = inst->BranchTarget - 1; /* go to matching BNGLOOP */
717 break;
718 case OPCODE_BGNSUB: /* begin subroutine */
719 break;
720 case OPCODE_ENDSUB: /* end subroutine */
721 break;
722 case OPCODE_BRA: /* branch (conditional) */
723 /* fall-through */
724 case OPCODE_BRK: /* break out of loop (conditional) */
725 /* fall-through */
726 case OPCODE_CONT: /* continue loop (conditional) */
727 if (eval_condition(machine, inst)) {
728 /* take branch */
729 /* Subtract 1 here since we'll do pc++ at end of for-loop */
730 pc = inst->BranchTarget - 1;
731 }
732 break;
733 case OPCODE_CAL: /* Call subroutine (conditional) */
734 if (eval_condition(machine, inst)) {
735 /* call the subroutine */
736 if (machine->StackDepth >= MAX_PROGRAM_CALL_DEPTH) {
737 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
Brian13e3b212007-02-22 16:09:40 -0700738 }
Briana0275b02007-03-27 11:02:20 -0600739 machine->CallStack[machine->StackDepth++] = pc + 1; /* next inst */
Brian31dc7a32007-03-27 15:21:35 -0600740 /* Subtract 1 here since we'll do pc++ at end of for-loop */
741 pc = inst->BranchTarget - 1;
Briane80d9012007-02-23 16:53:24 -0700742 }
743 break;
744 case OPCODE_CMP:
745 {
746 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700747 fetch_vector4(&inst->SrcReg[0], machine, a);
748 fetch_vector4(&inst->SrcReg[1], machine, b);
749 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -0700750 result[0] = a[0] < 0.0F ? b[0] : c[0];
751 result[1] = a[1] < 0.0F ? b[1] : c[1];
752 result[2] = a[2] < 0.0F ? b[2] : c[2];
753 result[3] = a[3] < 0.0F ? b[3] : c[3];
754 store_vector4(inst, machine, result);
755 }
756 break;
757 case OPCODE_COS:
758 {
759 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700760 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700761 result[0] = result[1] = result[2] = result[3]
762 = (GLfloat) _mesa_cos(a[0]);
763 store_vector4(inst, machine, result);
764 }
765 break;
766 case OPCODE_DDX: /* Partial derivative with respect to X */
767 {
Brian62da6a12007-05-02 18:44:34 -0600768 GLfloat result[4];
769 fetch_vector4_deriv(ctx, &inst->SrcReg[0], machine,
770 'X', result);
Briane80d9012007-02-23 16:53:24 -0700771 store_vector4(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -0700772 }
773 break;
774 case OPCODE_DDY: /* Partial derivative with respect to Y */
775 {
Brian62da6a12007-05-02 18:44:34 -0600776 GLfloat result[4];
777 fetch_vector4_deriv(ctx, &inst->SrcReg[0], machine,
778 'Y', result);
Briane80d9012007-02-23 16:53:24 -0700779 store_vector4(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -0700780 }
781 break;
Brian Paul65cb74e2008-11-07 09:41:00 -0700782 case OPCODE_DP2:
783 {
784 GLfloat a[4], b[4], result[4];
785 fetch_vector4(&inst->SrcReg[0], machine, a);
786 fetch_vector4(&inst->SrcReg[1], machine, b);
787 result[0] = result[1] = result[2] = result[3] = DOT2(a, b);
788 store_vector4(inst, machine, result);
789 if (DEBUG_PROG) {
790 printf("DP2 %g = (%g %g) . (%g %g)\n",
791 result[0], a[0], a[1], b[0], b[1]);
792 }
793 }
794 break;
795 case OPCODE_DP2A:
796 {
797 GLfloat a[4], b[4], c, result[4];
798 fetch_vector4(&inst->SrcReg[0], machine, a);
799 fetch_vector4(&inst->SrcReg[1], machine, b);
800 fetch_vector1(&inst->SrcReg[1], machine, &c);
801 result[0] = result[1] = result[2] = result[3] = DOT2(a, b) + c;
802 store_vector4(inst, machine, result);
803 if (DEBUG_PROG) {
804 printf("DP2A %g = (%g %g) . (%g %g) + %g\n",
805 result[0], a[0], a[1], b[0], b[1], c);
806 }
807 }
808 break;
Briane80d9012007-02-23 16:53:24 -0700809 case OPCODE_DP3:
810 {
811 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700812 fetch_vector4(&inst->SrcReg[0], machine, a);
813 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700814 result[0] = result[1] = result[2] = result[3] = DOT3(a, b);
815 store_vector4(inst, machine, result);
816 if (DEBUG_PROG) {
817 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
818 result[0], a[0], a[1], a[2], b[0], b[1], b[2]);
Brian13e3b212007-02-22 16:09:40 -0700819 }
Briane80d9012007-02-23 16:53:24 -0700820 }
821 break;
822 case OPCODE_DP4:
823 {
824 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700825 fetch_vector4(&inst->SrcReg[0], machine, a);
826 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700827 result[0] = result[1] = result[2] = result[3] = DOT4(a, b);
828 store_vector4(inst, machine, result);
829 if (DEBUG_PROG) {
830 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
831 result[0], a[0], a[1], a[2], a[3],
832 b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -0700833 }
Briane80d9012007-02-23 16:53:24 -0700834 }
835 break;
836 case OPCODE_DPH:
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);
Brian Paul65cb74e2008-11-07 09:41:00 -0700841 result[0] = result[1] = result[2] = result[3] = DOT3(a, b) + b[3];
Briane80d9012007-02-23 16:53:24 -0700842 store_vector4(inst, machine, result);
843 }
844 break;
845 case OPCODE_DST: /* Distance vector */
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] = 1.0F;
851 result[1] = a[1] * b[1];
852 result[2] = a[2];
853 result[3] = b[3];
854 store_vector4(inst, machine, result);
855 }
856 break;
Brianf183a2d2007-02-23 17:14:30 -0700857 case OPCODE_EXP:
Brianf183a2d2007-02-23 17:14:30 -0700858 {
859 GLfloat t[4], q[4], floor_t0;
Brian33eac562007-02-25 18:52:41 -0700860 fetch_vector1(&inst->SrcReg[0], machine, t);
Brianf183a2d2007-02-23 17:14:30 -0700861 floor_t0 = FLOORF(t[0]);
862 if (floor_t0 > FLT_MAX_EXP) {
863 SET_POS_INFINITY(q[0]);
864 SET_POS_INFINITY(q[2]);
865 }
866 else if (floor_t0 < FLT_MIN_EXP) {
867 q[0] = 0.0F;
868 q[2] = 0.0F;
869 }
870 else {
Brian761728a2007-02-24 11:14:57 -0700871 q[0] = LDEXPF(1.0, (int) floor_t0);
872 /* Note: GL_NV_vertex_program expects
873 * result.z = result.x * APPX(result.y)
874 * We do what the ARB extension says.
875 */
Brian Paul016701f2008-07-29 17:43:35 -0600876 q[2] = (GLfloat) pow(2.0, t[0]);
Brianf183a2d2007-02-23 17:14:30 -0700877 }
878 q[1] = t[0] - floor_t0;
879 q[3] = 1.0F;
880 store_vector4( inst, machine, q );
881 }
882 break;
Briane80d9012007-02-23 16:53:24 -0700883 case OPCODE_EX2: /* Exponential base 2 */
884 {
885 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700886 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700887 result[0] = result[1] = result[2] = result[3] =
888 (GLfloat) _mesa_pow(2.0, a[0]);
889 store_vector4(inst, machine, result);
890 }
891 break;
892 case OPCODE_FLR:
893 {
894 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700895 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700896 result[0] = FLOORF(a[0]);
897 result[1] = FLOORF(a[1]);
898 result[2] = FLOORF(a[2]);
899 result[3] = FLOORF(a[3]);
900 store_vector4(inst, machine, result);
901 }
902 break;
903 case OPCODE_FRC:
904 {
905 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700906 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700907 result[0] = a[0] - FLOORF(a[0]);
908 result[1] = a[1] - FLOORF(a[1]);
909 result[2] = a[2] - FLOORF(a[2]);
910 result[3] = a[3] - FLOORF(a[3]);
911 store_vector4(inst, machine, result);
912 }
913 break;
914 case OPCODE_IF:
Brian63556fa2007-03-23 14:47:46 -0600915 {
916 GLboolean cond;
917 /* eval condition */
918 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
919 GLfloat a[4];
920 fetch_vector1(&inst->SrcReg[0], machine, a);
921 cond = (a[0] != 0.0);
922 }
923 else {
924 cond = eval_condition(machine, inst);
925 }
Briana7f73662007-04-20 08:11:51 -0600926 if (DEBUG_PROG) {
927 printf("IF: %d\n", cond);
928 }
Brian63556fa2007-03-23 14:47:46 -0600929 /* do if/else */
930 if (cond) {
931 /* do if-clause (just continue execution) */
932 }
933 else {
934 /* go to the instruction after ELSE or ENDIF */
935 assert(inst->BranchTarget >= 0);
936 pc = inst->BranchTarget - 1;
937 }
Briane80d9012007-02-23 16:53:24 -0700938 }
939 break;
940 case OPCODE_ELSE:
941 /* goto ENDIF */
942 assert(inst->BranchTarget >= 0);
943 pc = inst->BranchTarget - 1;
944 break;
945 case OPCODE_ENDIF:
946 /* nothing */
947 break;
Briane80d9012007-02-23 16:53:24 -0700948 case OPCODE_KIL_NV: /* NV_f_p only (conditional) */
949 if (eval_condition(machine, inst)) {
950 return GL_FALSE;
951 }
952 break;
953 case OPCODE_KIL: /* ARB_f_p only */
954 {
955 GLfloat a[4];
Brian33eac562007-02-25 18:52:41 -0700956 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700957 if (a[0] < 0.0F || a[1] < 0.0F || a[2] < 0.0F || a[3] < 0.0F) {
Brian13e3b212007-02-22 16:09:40 -0700958 return GL_FALSE;
959 }
Briane80d9012007-02-23 16:53:24 -0700960 }
961 break;
962 case OPCODE_LG2: /* log base 2 */
963 {
964 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700965 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700966 result[0] = result[1] = result[2] = result[3] = LOG2(a[0]);
967 store_vector4(inst, machine, result);
968 }
969 break;
970 case OPCODE_LIT:
971 {
972 const GLfloat epsilon = 1.0F / 256.0F; /* from NV VP spec */
973 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700974 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700975 a[0] = MAX2(a[0], 0.0F);
976 a[1] = MAX2(a[1], 0.0F);
977 /* XXX ARB version clamps a[3], NV version doesn't */
978 a[3] = CLAMP(a[3], -(128.0F - epsilon), (128.0F - epsilon));
979 result[0] = 1.0F;
980 result[1] = a[0];
981 /* XXX we could probably just use pow() here */
982 if (a[0] > 0.0F) {
983 if (a[1] == 0.0 && a[3] == 0.0)
984 result[2] = 1.0;
985 else
986 result[2] = EXPF(a[3] * LOGF(a[1]));
Brian13e3b212007-02-22 16:09:40 -0700987 }
Briane80d9012007-02-23 16:53:24 -0700988 else {
989 result[2] = 0.0;
Brian13e3b212007-02-22 16:09:40 -0700990 }
Briane80d9012007-02-23 16:53:24 -0700991 result[3] = 1.0F;
992 store_vector4(inst, machine, result);
993 if (DEBUG_PROG) {
994 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
995 result[0], result[1], result[2], result[3],
996 a[0], a[1], a[2], a[3]);
Brian13e3b212007-02-22 16:09:40 -0700997 }
Briane80d9012007-02-23 16:53:24 -0700998 }
999 break;
Brianf183a2d2007-02-23 17:14:30 -07001000 case OPCODE_LOG:
1001 {
1002 GLfloat t[4], q[4], abs_t0;
Brian33eac562007-02-25 18:52:41 -07001003 fetch_vector1(&inst->SrcReg[0], machine, t);
Brianf183a2d2007-02-23 17:14:30 -07001004 abs_t0 = FABSF(t[0]);
1005 if (abs_t0 != 0.0F) {
1006 /* Since we really can't handle infinite values on VMS
1007 * like other OSes we'll use __MAXFLOAT to represent
1008 * infinity. This may need some tweaking.
1009 */
1010#ifdef VMS
1011 if (abs_t0 == __MAXFLOAT)
1012#else
1013 if (IS_INF_OR_NAN(abs_t0))
1014#endif
1015 {
1016 SET_POS_INFINITY(q[0]);
1017 q[1] = 1.0F;
1018 SET_POS_INFINITY(q[2]);
1019 }
1020 else {
1021 int exponent;
1022 GLfloat mantissa = FREXPF(t[0], &exponent);
1023 q[0] = (GLfloat) (exponent - 1);
1024 q[1] = (GLfloat) (2.0 * mantissa); /* map [.5, 1) -> [1, 2) */
1025 q[2] = (GLfloat) (q[0] + LOG2(q[1]));
1026 }
1027 }
1028 else {
1029 SET_NEG_INFINITY(q[0]);
1030 q[1] = 1.0F;
1031 SET_NEG_INFINITY(q[2]);
1032 }
1033 q[3] = 1.0;
1034 store_vector4(inst, machine, q);
1035 }
1036 break;
Briane80d9012007-02-23 16:53:24 -07001037 case OPCODE_LRP:
1038 {
1039 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001040 fetch_vector4(&inst->SrcReg[0], machine, a);
1041 fetch_vector4(&inst->SrcReg[1], machine, b);
1042 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -07001043 result[0] = a[0] * b[0] + (1.0F - a[0]) * c[0];
1044 result[1] = a[1] * b[1] + (1.0F - a[1]) * c[1];
1045 result[2] = a[2] * b[2] + (1.0F - a[2]) * c[2];
1046 result[3] = a[3] * b[3] + (1.0F - a[3]) * c[3];
1047 store_vector4(inst, machine, result);
1048 if (DEBUG_PROG) {
1049 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
1050 "(%g %g %g %g), (%g %g %g %g)\n",
1051 result[0], result[1], result[2], result[3],
1052 a[0], a[1], a[2], a[3],
1053 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
Brian13e3b212007-02-22 16:09:40 -07001054 }
Briane80d9012007-02-23 16:53:24 -07001055 }
1056 break;
1057 case OPCODE_MAD:
1058 {
1059 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001060 fetch_vector4(&inst->SrcReg[0], machine, a);
1061 fetch_vector4(&inst->SrcReg[1], machine, b);
1062 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -07001063 result[0] = a[0] * b[0] + c[0];
1064 result[1] = a[1] * b[1] + c[1];
1065 result[2] = a[2] * b[2] + c[2];
1066 result[3] = a[3] * b[3] + c[3];
1067 store_vector4(inst, machine, result);
1068 if (DEBUG_PROG) {
1069 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
1070 "(%g %g %g %g) + (%g %g %g %g)\n",
1071 result[0], result[1], result[2], result[3],
1072 a[0], a[1], a[2], a[3],
1073 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
Brian13e3b212007-02-22 16:09:40 -07001074 }
Briane80d9012007-02-23 16:53:24 -07001075 }
1076 break;
1077 case OPCODE_MAX:
1078 {
1079 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001080 fetch_vector4(&inst->SrcReg[0], machine, a);
1081 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001082 result[0] = MAX2(a[0], b[0]);
1083 result[1] = MAX2(a[1], b[1]);
1084 result[2] = MAX2(a[2], b[2]);
1085 result[3] = MAX2(a[3], b[3]);
1086 store_vector4(inst, machine, result);
1087 if (DEBUG_PROG) {
1088 printf("MAX (%g %g %g %g) = (%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], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001091 }
Briane80d9012007-02-23 16:53:24 -07001092 }
1093 break;
1094 case OPCODE_MIN:
1095 {
1096 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001097 fetch_vector4(&inst->SrcReg[0], machine, a);
1098 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001099 result[0] = MIN2(a[0], b[0]);
1100 result[1] = MIN2(a[1], b[1]);
1101 result[2] = MIN2(a[2], b[2]);
1102 result[3] = MIN2(a[3], b[3]);
1103 store_vector4(inst, machine, result);
1104 }
1105 break;
1106 case OPCODE_MOV:
1107 {
1108 GLfloat result[4];
Brian33eac562007-02-25 18:52:41 -07001109 fetch_vector4(&inst->SrcReg[0], machine, result);
Briane80d9012007-02-23 16:53:24 -07001110 store_vector4(inst, machine, result);
1111 if (DEBUG_PROG) {
1112 printf("MOV (%g %g %g %g)\n",
1113 result[0], result[1], result[2], result[3]);
Brian13e3b212007-02-22 16:09:40 -07001114 }
Briane80d9012007-02-23 16:53:24 -07001115 }
1116 break;
1117 case OPCODE_MUL:
1118 {
1119 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001120 fetch_vector4(&inst->SrcReg[0], machine, a);
1121 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001122 result[0] = a[0] * b[0];
1123 result[1] = a[1] * b[1];
1124 result[2] = a[2] * b[2];
1125 result[3] = a[3] * b[3];
1126 store_vector4(inst, machine, result);
1127 if (DEBUG_PROG) {
1128 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1129 result[0], result[1], result[2], result[3],
1130 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001131 }
Briane80d9012007-02-23 16:53:24 -07001132 }
1133 break;
1134 case OPCODE_NOISE1:
1135 {
1136 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001137 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001138 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001139 result[1] =
Brian Paul702b5b02008-12-15 18:37:39 -07001140 result[2] =
1141 result[3] = _mesa_noise1(a[0]);
Briane80d9012007-02-23 16:53:24 -07001142 store_vector4(inst, machine, result);
1143 }
1144 break;
1145 case OPCODE_NOISE2:
1146 {
1147 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001148 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001149 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001150 result[1] =
Brian Paul702b5b02008-12-15 18:37:39 -07001151 result[2] = result[3] = _mesa_noise2(a[0], a[1]);
Briane80d9012007-02-23 16:53:24 -07001152 store_vector4(inst, machine, result);
1153 }
1154 break;
1155 case OPCODE_NOISE3:
1156 {
1157 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001158 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001159 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001160 result[1] =
1161 result[2] =
Brian Paul702b5b02008-12-15 18:37:39 -07001162 result[3] = _mesa_noise3(a[0], a[1], a[2]);
Briane80d9012007-02-23 16:53:24 -07001163 store_vector4(inst, machine, result);
1164 }
1165 break;
1166 case OPCODE_NOISE4:
1167 {
1168 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001169 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001170 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001171 result[1] =
1172 result[2] =
Brian Paul702b5b02008-12-15 18:37:39 -07001173 result[3] = _mesa_noise4(a[0], a[1], a[2], a[3]);
Briane80d9012007-02-23 16:53:24 -07001174 store_vector4(inst, machine, result);
1175 }
1176 break;
1177 case OPCODE_NOP:
1178 break;
Brian Paul37eef7b2008-11-07 09:33:55 -07001179 case OPCODE_NOT: /* bitwise NOT */
1180 {
1181 GLuint a[4], result[4];
1182 fetch_vector4ui(&inst->SrcReg[0], machine, a);
1183 result[0] = ~a[0];
1184 result[1] = ~a[1];
1185 result[2] = ~a[2];
1186 result[3] = ~a[3];
1187 store_vector4ui(inst, machine, result);
1188 }
1189 break;
Brian Paulf6ead502008-11-07 08:51:31 -07001190 case OPCODE_NRM3: /* 3-component normalization */
1191 {
1192 GLfloat a[4], result[4];
1193 GLfloat tmp;
1194 fetch_vector4(&inst->SrcReg[0], machine, a);
1195 tmp = a[0] * a[0] + a[1] * a[1] + a[2] * a[2];
1196 if (tmp != 0.0F)
Brian Paul22459e72008-11-07 12:59:36 -07001197 tmp = INV_SQRTF(tmp);
Brian Paulf6ead502008-11-07 08:51:31 -07001198 result[0] = tmp * a[0];
1199 result[1] = tmp * a[1];
1200 result[2] = tmp * a[2];
1201 result[3] = 0.0; /* undefined, but prevent valgrind warnings */
1202 store_vector4(inst, machine, result);
1203 }
1204 break;
1205 case OPCODE_NRM4: /* 4-component normalization */
1206 {
1207 GLfloat a[4], result[4];
1208 GLfloat tmp;
1209 fetch_vector4(&inst->SrcReg[0], machine, a);
1210 tmp = a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3];
1211 if (tmp != 0.0F)
Brian Paul22459e72008-11-07 12:59:36 -07001212 tmp = INV_SQRTF(tmp);
Brian Paulf6ead502008-11-07 08:51:31 -07001213 result[0] = tmp * a[0];
1214 result[1] = tmp * a[1];
1215 result[2] = tmp * a[2];
1216 result[3] = tmp * a[3];
1217 store_vector4(inst, machine, result);
1218 }
1219 break;
Brian Paul37eef7b2008-11-07 09:33:55 -07001220 case OPCODE_OR: /* bitwise OR */
1221 {
1222 GLuint a[4], b[4], result[4];
1223 fetch_vector4ui(&inst->SrcReg[0], machine, a);
1224 fetch_vector4ui(&inst->SrcReg[1], machine, b);
1225 result[0] = a[0] | b[0];
1226 result[1] = a[1] | b[1];
1227 result[2] = a[2] | b[2];
1228 result[3] = a[3] | b[3];
1229 store_vector4ui(inst, machine, result);
1230 }
1231 break;
Briane80d9012007-02-23 16:53:24 -07001232 case OPCODE_PK2H: /* pack two 16-bit floats in one 32-bit float */
1233 {
Brian Paul37eef7b2008-11-07 09:33:55 -07001234 GLfloat a[4];
1235 GLuint result[4];
Briane80d9012007-02-23 16:53:24 -07001236 GLhalfNV hx, hy;
Brian33eac562007-02-25 18:52:41 -07001237 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001238 hx = _mesa_float_to_half(a[0]);
1239 hy = _mesa_float_to_half(a[1]);
Brian Paul37eef7b2008-11-07 09:33:55 -07001240 result[0] =
1241 result[1] =
1242 result[2] =
1243 result[3] = hx | (hy << 16);
1244 store_vector4ui(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -07001245 }
1246 break;
1247 case OPCODE_PK2US: /* pack two GLushorts into one 32-bit float */
1248 {
Brian Paul37eef7b2008-11-07 09:33:55 -07001249 GLfloat a[4];
1250 GLuint result[4], usx, usy;
Brian33eac562007-02-25 18:52:41 -07001251 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001252 a[0] = CLAMP(a[0], 0.0F, 1.0F);
1253 a[1] = CLAMP(a[1], 0.0F, 1.0F);
1254 usx = IROUND(a[0] * 65535.0F);
1255 usy = IROUND(a[1] * 65535.0F);
Brian Paul37eef7b2008-11-07 09:33:55 -07001256 result[0] =
1257 result[1] =
1258 result[2] =
1259 result[3] = usx | (usy << 16);
1260 store_vector4ui(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -07001261 }
1262 break;
1263 case OPCODE_PK4B: /* pack four GLbytes into one 32-bit float */
1264 {
Brian Paul37eef7b2008-11-07 09:33:55 -07001265 GLfloat a[4];
1266 GLuint result[4], ubx, uby, ubz, ubw;
Brian33eac562007-02-25 18:52:41 -07001267 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001268 a[0] = CLAMP(a[0], -128.0F / 127.0F, 1.0F);
1269 a[1] = CLAMP(a[1], -128.0F / 127.0F, 1.0F);
1270 a[2] = CLAMP(a[2], -128.0F / 127.0F, 1.0F);
1271 a[3] = CLAMP(a[3], -128.0F / 127.0F, 1.0F);
1272 ubx = IROUND(127.0F * a[0] + 128.0F);
1273 uby = IROUND(127.0F * a[1] + 128.0F);
1274 ubz = IROUND(127.0F * a[2] + 128.0F);
1275 ubw = IROUND(127.0F * a[3] + 128.0F);
Brian Paul37eef7b2008-11-07 09:33:55 -07001276 result[0] =
1277 result[1] =
1278 result[2] =
1279 result[3] = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
1280 store_vector4ui(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -07001281 }
1282 break;
1283 case OPCODE_PK4UB: /* pack four GLubytes into one 32-bit float */
1284 {
Brian Paul37eef7b2008-11-07 09:33:55 -07001285 GLfloat a[4];
1286 GLuint result[4], ubx, uby, ubz, ubw;
Brian33eac562007-02-25 18:52:41 -07001287 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001288 a[0] = CLAMP(a[0], 0.0F, 1.0F);
1289 a[1] = CLAMP(a[1], 0.0F, 1.0F);
1290 a[2] = CLAMP(a[2], 0.0F, 1.0F);
1291 a[3] = CLAMP(a[3], 0.0F, 1.0F);
1292 ubx = IROUND(255.0F * a[0]);
1293 uby = IROUND(255.0F * a[1]);
1294 ubz = IROUND(255.0F * a[2]);
1295 ubw = IROUND(255.0F * a[3]);
Brian Paul37eef7b2008-11-07 09:33:55 -07001296 result[0] =
1297 result[1] =
1298 result[2] =
1299 result[3] = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
1300 store_vector4ui(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -07001301 }
1302 break;
1303 case OPCODE_POW:
1304 {
1305 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001306 fetch_vector1(&inst->SrcReg[0], machine, a);
1307 fetch_vector1(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001308 result[0] = result[1] = result[2] = result[3]
1309 = (GLfloat) _mesa_pow(a[0], b[0]);
1310 store_vector4(inst, machine, result);
1311 }
1312 break;
1313 case OPCODE_RCP:
1314 {
1315 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001316 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001317 if (DEBUG_PROG) {
1318 if (a[0] == 0)
1319 printf("RCP(0)\n");
1320 else if (IS_INF_OR_NAN(a[0]))
1321 printf("RCP(inf)\n");
Brian13e3b212007-02-22 16:09:40 -07001322 }
Briane80d9012007-02-23 16:53:24 -07001323 result[0] = result[1] = result[2] = result[3] = 1.0F / a[0];
1324 store_vector4(inst, machine, result);
1325 }
1326 break;
1327 case OPCODE_RET: /* return from subroutine (conditional) */
1328 if (eval_condition(machine, inst)) {
1329 if (machine->StackDepth == 0) {
1330 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
Brian13e3b212007-02-22 16:09:40 -07001331 }
Briana0275b02007-03-27 11:02:20 -06001332 /* subtract one because of pc++ in the for loop */
1333 pc = machine->CallStack[--machine->StackDepth] - 1;
Briane80d9012007-02-23 16:53:24 -07001334 }
1335 break;
1336 case OPCODE_RFL: /* reflection vector */
1337 {
1338 GLfloat axis[4], dir[4], result[4], tmpX, tmpW;
Brian33eac562007-02-25 18:52:41 -07001339 fetch_vector4(&inst->SrcReg[0], machine, axis);
1340 fetch_vector4(&inst->SrcReg[1], machine, dir);
Briane80d9012007-02-23 16:53:24 -07001341 tmpW = DOT3(axis, axis);
1342 tmpX = (2.0F * DOT3(axis, dir)) / tmpW;
1343 result[0] = tmpX * axis[0] - dir[0];
1344 result[1] = tmpX * axis[1] - dir[1];
1345 result[2] = tmpX * axis[2] - dir[2];
1346 /* result[3] is never written! XXX enforce in parser! */
1347 store_vector4(inst, machine, result);
1348 }
1349 break;
1350 case OPCODE_RSQ: /* 1 / sqrt() */
1351 {
1352 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001353 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001354 a[0] = FABSF(a[0]);
1355 result[0] = result[1] = result[2] = result[3] = INV_SQRTF(a[0]);
1356 store_vector4(inst, machine, result);
1357 if (DEBUG_PROG) {
1358 printf("RSQ %g = 1/sqrt(|%g|)\n", result[0], a[0]);
Brian13e3b212007-02-22 16:09:40 -07001359 }
Briane80d9012007-02-23 16:53:24 -07001360 }
1361 break;
1362 case OPCODE_SCS: /* sine and cos */
1363 {
1364 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001365 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001366 result[0] = (GLfloat) _mesa_cos(a[0]);
1367 result[1] = (GLfloat) _mesa_sin(a[0]);
1368 result[2] = 0.0; /* undefined! */
1369 result[3] = 0.0; /* undefined! */
1370 store_vector4(inst, machine, result);
1371 }
1372 break;
1373 case OPCODE_SEQ: /* set on equal */
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);
Brian28ab1122007-03-06 12:15:30 -07001383 if (DEBUG_PROG) {
1384 printf("SEQ (%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]);
1388 }
Briane80d9012007-02-23 16:53:24 -07001389 }
1390 break;
1391 case OPCODE_SFL: /* set false, operands ignored */
1392 {
1393 static const GLfloat result[4] = { 0.0F, 0.0F, 0.0F, 0.0F };
1394 store_vector4(inst, machine, result);
1395 }
1396 break;
1397 case OPCODE_SGE: /* set on greater or equal */
1398 {
1399 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001400 fetch_vector4(&inst->SrcReg[0], machine, a);
1401 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001402 result[0] = (a[0] >= b[0]) ? 1.0F : 0.0F;
1403 result[1] = (a[1] >= b[1]) ? 1.0F : 0.0F;
1404 result[2] = (a[2] >= b[2]) ? 1.0F : 0.0F;
1405 result[3] = (a[3] >= b[3]) ? 1.0F : 0.0F;
1406 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001407 if (DEBUG_PROG) {
1408 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1409 result[0], result[1], result[2], result[3],
1410 a[0], a[1], a[2], a[3],
1411 b[0], b[1], b[2], b[3]);
1412 }
Briane80d9012007-02-23 16:53:24 -07001413 }
1414 break;
1415 case OPCODE_SGT: /* set on greater */
1416 {
1417 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001418 fetch_vector4(&inst->SrcReg[0], machine, a);
1419 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001420 result[0] = (a[0] > b[0]) ? 1.0F : 0.0F;
1421 result[1] = (a[1] > b[1]) ? 1.0F : 0.0F;
1422 result[2] = (a[2] > b[2]) ? 1.0F : 0.0F;
1423 result[3] = (a[3] > b[3]) ? 1.0F : 0.0F;
1424 store_vector4(inst, machine, result);
1425 if (DEBUG_PROG) {
Brian28ab1122007-03-06 12:15:30 -07001426 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1427 result[0], result[1], result[2], result[3],
1428 a[0], a[1], a[2], a[3],
1429 b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001430 }
Briane80d9012007-02-23 16:53:24 -07001431 }
1432 break;
1433 case OPCODE_SIN:
1434 {
1435 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001436 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001437 result[0] = result[1] = result[2] = result[3]
1438 = (GLfloat) _mesa_sin(a[0]);
1439 store_vector4(inst, machine, result);
1440 }
1441 break;
1442 case OPCODE_SLE: /* set on less or equal */
1443 {
1444 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001445 fetch_vector4(&inst->SrcReg[0], machine, a);
1446 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001447 result[0] = (a[0] <= b[0]) ? 1.0F : 0.0F;
1448 result[1] = (a[1] <= b[1]) ? 1.0F : 0.0F;
1449 result[2] = (a[2] <= b[2]) ? 1.0F : 0.0F;
1450 result[3] = (a[3] <= b[3]) ? 1.0F : 0.0F;
1451 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001452 if (DEBUG_PROG) {
1453 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1454 result[0], result[1], result[2], result[3],
1455 a[0], a[1], a[2], a[3],
1456 b[0], b[1], b[2], b[3]);
1457 }
Briane80d9012007-02-23 16:53:24 -07001458 }
1459 break;
1460 case OPCODE_SLT: /* set on less */
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]) ? 1.0F : 0.0F;
1466 result[1] = (a[1] < b[1]) ? 1.0F : 0.0F;
1467 result[2] = (a[2] < b[2]) ? 1.0F : 0.0F;
1468 result[3] = (a[3] < b[3]) ? 1.0F : 0.0F;
1469 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001470 if (DEBUG_PROG) {
1471 printf("SLT (%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],
1474 b[0], b[1], b[2], b[3]);
1475 }
Briane80d9012007-02-23 16:53:24 -07001476 }
1477 break;
1478 case OPCODE_SNE: /* set on not equal */
1479 {
1480 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001481 fetch_vector4(&inst->SrcReg[0], machine, a);
1482 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001483 result[0] = (a[0] != b[0]) ? 1.0F : 0.0F;
1484 result[1] = (a[1] != b[1]) ? 1.0F : 0.0F;
1485 result[2] = (a[2] != b[2]) ? 1.0F : 0.0F;
1486 result[3] = (a[3] != b[3]) ? 1.0F : 0.0F;
1487 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001488 if (DEBUG_PROG) {
1489 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1490 result[0], result[1], result[2], result[3],
1491 a[0], a[1], a[2], a[3],
1492 b[0], b[1], b[2], b[3]);
1493 }
Briane80d9012007-02-23 16:53:24 -07001494 }
1495 break;
Brian Paulf6ead502008-11-07 08:51:31 -07001496 case OPCODE_SSG: /* set sign (-1, 0 or +1) */
1497 {
1498 GLfloat a[4], result[4];
1499 fetch_vector4(&inst->SrcReg[0], machine, a);
1500 result[0] = (GLfloat) ((a[0] > 0.0F) - (a[0] < 0.0F));
1501 result[1] = (GLfloat) ((a[1] > 0.0F) - (a[1] < 0.0F));
1502 result[2] = (GLfloat) ((a[2] > 0.0F) - (a[2] < 0.0F));
1503 result[3] = (GLfloat) ((a[3] > 0.0F) - (a[3] < 0.0F));
1504 store_vector4(inst, machine, result);
1505 }
1506 break;
Briane80d9012007-02-23 16:53:24 -07001507 case OPCODE_STR: /* set true, operands ignored */
1508 {
1509 static const GLfloat result[4] = { 1.0F, 1.0F, 1.0F, 1.0F };
1510 store_vector4(inst, machine, result);
1511 }
1512 break;
1513 case OPCODE_SUB:
1514 {
1515 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001516 fetch_vector4(&inst->SrcReg[0], machine, a);
1517 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001518 result[0] = a[0] - b[0];
1519 result[1] = a[1] - b[1];
1520 result[2] = a[2] - b[2];
1521 result[3] = a[3] - b[3];
1522 store_vector4(inst, machine, result);
1523 if (DEBUG_PROG) {
1524 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1525 result[0], result[1], result[2], result[3],
1526 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001527 }
Briane80d9012007-02-23 16:53:24 -07001528 }
1529 break;
1530 case OPCODE_SWZ: /* extended swizzle */
1531 {
1532 const struct prog_src_register *source = &inst->SrcReg[0];
Brian Paulf4361542008-11-11 10:47:10 -07001533 const GLfloat *src = get_src_register_pointer(source, machine);
Briane80d9012007-02-23 16:53:24 -07001534 GLfloat result[4];
1535 GLuint i;
1536 for (i = 0; i < 4; i++) {
1537 const GLuint swz = GET_SWZ(source->Swizzle, i);
1538 if (swz == SWIZZLE_ZERO)
1539 result[i] = 0.0;
1540 else if (swz == SWIZZLE_ONE)
1541 result[i] = 1.0;
Brian13e3b212007-02-22 16:09:40 -07001542 else {
Briane80d9012007-02-23 16:53:24 -07001543 ASSERT(swz >= 0);
1544 ASSERT(swz <= 3);
1545 result[i] = src[swz];
Brian13e3b212007-02-22 16:09:40 -07001546 }
Briane80d9012007-02-23 16:53:24 -07001547 if (source->NegateBase & (1 << i))
1548 result[i] = -result[i];
Brian13e3b212007-02-22 16:09:40 -07001549 }
Briane80d9012007-02-23 16:53:24 -07001550 store_vector4(inst, machine, result);
1551 }
1552 break;
1553 case OPCODE_TEX: /* Both ARB and NV frag prog */
Brian999b5562007-11-23 12:01:57 -07001554 /* Simple texel lookup */
Briane80d9012007-02-23 16:53:24 -07001555 {
Brian999b5562007-11-23 12:01:57 -07001556 GLfloat texcoord[4], color[4];
1557 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1558
1559 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1560
Briane80d9012007-02-23 16:53:24 -07001561 if (DEBUG_PROG) {
Brian999b5562007-11-23 12:01:57 -07001562 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
Briane80d9012007-02-23 16:53:24 -07001563 color[0], color[1], color[2], color[3],
1564 inst->TexSrcUnit,
Brian999b5562007-11-23 12:01:57 -07001565 texcoord[0], texcoord[1], texcoord[2], texcoord[3]);
Briane80d9012007-02-23 16:53:24 -07001566 }
1567 store_vector4(inst, machine, color);
1568 }
1569 break;
1570 case OPCODE_TXB: /* GL_ARB_fragment_program only */
1571 /* Texel lookup with LOD bias */
1572 {
1573 const struct gl_texture_unit *texUnit
1574 = &ctx->Texture.Unit[inst->TexSrcUnit];
Brian999b5562007-11-23 12:01:57 -07001575 GLfloat texcoord[4], color[4], lodBias;
1576
1577 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1578
1579 /* texcoord[3] is the bias to add to lambda */
1580 lodBias = texUnit->LodBias + texcoord[3];
1581 if (texUnit->_Current) {
1582 lodBias += texUnit->_Current->LodBias;
1583 }
1584
1585 fetch_texel(ctx, machine, inst, texcoord, lodBias, color);
1586
Briane80d9012007-02-23 16:53:24 -07001587 store_vector4(inst, machine, color);
1588 }
1589 break;
1590 case OPCODE_TXD: /* GL_NV_fragment_program only */
1591 /* Texture lookup w/ partial derivatives for LOD */
1592 {
1593 GLfloat texcoord[4], dtdx[4], dtdy[4], color[4];
Brian33eac562007-02-25 18:52:41 -07001594 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1595 fetch_vector4(&inst->SrcReg[1], machine, dtdx);
1596 fetch_vector4(&inst->SrcReg[2], machine, dtdy);
Briane80d9012007-02-23 16:53:24 -07001597 machine->FetchTexelDeriv(ctx, texcoord, dtdx, dtdy,
Brian999b5562007-11-23 12:01:57 -07001598 0.0, /* lodBias */
Briane80d9012007-02-23 16:53:24 -07001599 inst->TexSrcUnit, color);
1600 store_vector4(inst, machine, color);
1601 }
1602 break;
1603 case OPCODE_TXP: /* GL_ARB_fragment_program only */
1604 /* Texture lookup w/ projective divide */
1605 {
Brian999b5562007-11-23 12:01:57 -07001606 GLfloat texcoord[4], color[4];
1607
Brian33eac562007-02-25 18:52:41 -07001608 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
Briane80d9012007-02-23 16:53:24 -07001609 /* Not so sure about this test - if texcoord[3] is
1610 * zero, we'd probably be fine except for an ASSERT in
1611 * IROUND_POS() which gets triggered by the inf values created.
1612 */
1613 if (texcoord[3] != 0.0) {
1614 texcoord[0] /= texcoord[3];
1615 texcoord[1] /= texcoord[3];
1616 texcoord[2] /= texcoord[3];
1617 }
Brian999b5562007-11-23 12:01:57 -07001618
1619 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1620
Briane80d9012007-02-23 16:53:24 -07001621 store_vector4(inst, machine, color);
1622 }
1623 break;
1624 case OPCODE_TXP_NV: /* GL_NV_fragment_program only */
Brian999b5562007-11-23 12:01:57 -07001625 /* Texture lookup w/ projective divide, as above, but do not
1626 * do the divide by w if sampling from a cube map.
1627 */
Briane80d9012007-02-23 16:53:24 -07001628 {
Brian999b5562007-11-23 12:01:57 -07001629 GLfloat texcoord[4], color[4];
1630
Brian33eac562007-02-25 18:52:41 -07001631 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
Briane80d9012007-02-23 16:53:24 -07001632 if (inst->TexSrcTarget != TEXTURE_CUBE_INDEX &&
1633 texcoord[3] != 0.0) {
1634 texcoord[0] /= texcoord[3];
1635 texcoord[1] /= texcoord[3];
1636 texcoord[2] /= texcoord[3];
1637 }
Brian999b5562007-11-23 12:01:57 -07001638
1639 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1640
Briane80d9012007-02-23 16:53:24 -07001641 store_vector4(inst, machine, color);
1642 }
1643 break;
Brian Paul035c0cf2008-11-06 17:14:33 -07001644 case OPCODE_TRUNC: /* truncate toward zero */
1645 {
1646 GLfloat a[4], result[4];
1647 fetch_vector4(&inst->SrcReg[0], machine, a);
1648 result[0] = (GLfloat) (GLint) a[0];
1649 result[1] = (GLfloat) (GLint) a[1];
1650 result[2] = (GLfloat) (GLint) a[2];
1651 result[3] = (GLfloat) (GLint) a[3];
1652 store_vector4(inst, machine, result);
1653 }
1654 break;
Briane80d9012007-02-23 16:53:24 -07001655 case OPCODE_UP2H: /* unpack two 16-bit floats */
1656 {
1657 GLfloat a[4], result[4];
1658 const GLuint *rawBits = (const GLuint *) a;
1659 GLhalfNV hx, hy;
Brian33eac562007-02-25 18:52:41 -07001660 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001661 hx = rawBits[0] & 0xffff;
1662 hy = rawBits[0] >> 16;
1663 result[0] = result[2] = _mesa_half_to_float(hx);
1664 result[1] = result[3] = _mesa_half_to_float(hy);
1665 store_vector4(inst, machine, result);
1666 }
1667 break;
1668 case OPCODE_UP2US: /* unpack two GLushorts */
1669 {
1670 GLfloat a[4], result[4];
1671 const GLuint *rawBits = (const GLuint *) a;
1672 GLushort usx, usy;
Brian33eac562007-02-25 18:52:41 -07001673 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001674 usx = rawBits[0] & 0xffff;
1675 usy = rawBits[0] >> 16;
1676 result[0] = result[2] = usx * (1.0f / 65535.0f);
1677 result[1] = result[3] = usy * (1.0f / 65535.0f);
1678 store_vector4(inst, machine, result);
1679 }
1680 break;
1681 case OPCODE_UP4B: /* unpack four GLbytes */
1682 {
1683 GLfloat a[4], result[4];
1684 const GLuint *rawBits = (const GLuint *) a;
Brian33eac562007-02-25 18:52:41 -07001685 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001686 result[0] = (((rawBits[0] >> 0) & 0xff) - 128) / 127.0F;
1687 result[1] = (((rawBits[0] >> 8) & 0xff) - 128) / 127.0F;
1688 result[2] = (((rawBits[0] >> 16) & 0xff) - 128) / 127.0F;
1689 result[3] = (((rawBits[0] >> 24) & 0xff) - 128) / 127.0F;
1690 store_vector4(inst, machine, result);
1691 }
1692 break;
1693 case OPCODE_UP4UB: /* unpack four GLubytes */
1694 {
1695 GLfloat a[4], result[4];
1696 const GLuint *rawBits = (const GLuint *) a;
Brian33eac562007-02-25 18:52:41 -07001697 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001698 result[0] = ((rawBits[0] >> 0) & 0xff) / 255.0F;
1699 result[1] = ((rawBits[0] >> 8) & 0xff) / 255.0F;
1700 result[2] = ((rawBits[0] >> 16) & 0xff) / 255.0F;
1701 result[3] = ((rawBits[0] >> 24) & 0xff) / 255.0F;
1702 store_vector4(inst, machine, result);
1703 }
1704 break;
Brian Paul37eef7b2008-11-07 09:33:55 -07001705 case OPCODE_XOR: /* bitwise XOR */
1706 {
1707 GLuint a[4], b[4], result[4];
1708 fetch_vector4ui(&inst->SrcReg[0], machine, a);
1709 fetch_vector4ui(&inst->SrcReg[1], machine, b);
1710 result[0] = a[0] ^ b[0];
1711 result[1] = a[1] ^ b[1];
1712 result[2] = a[2] ^ b[2];
1713 result[3] = a[3] ^ b[3];
1714 store_vector4ui(inst, machine, result);
1715 }
1716 break;
Briane80d9012007-02-23 16:53:24 -07001717 case OPCODE_XPD: /* cross product */
1718 {
1719 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001720 fetch_vector4(&inst->SrcReg[0], machine, a);
1721 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001722 result[0] = a[1] * b[2] - a[2] * b[1];
1723 result[1] = a[2] * b[0] - a[0] * b[2];
1724 result[2] = a[0] * b[1] - a[1] * b[0];
1725 result[3] = 1.0;
1726 store_vector4(inst, machine, result);
Brian9637c962007-03-07 17:40:57 -07001727 if (DEBUG_PROG) {
1728 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1729 result[0], result[1], result[2], result[3],
1730 a[0], a[1], a[2], b[0], b[1], b[2]);
1731 }
Briane80d9012007-02-23 16:53:24 -07001732 }
1733 break;
1734 case OPCODE_X2D: /* 2-D matrix transform */
1735 {
1736 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001737 fetch_vector4(&inst->SrcReg[0], machine, a);
1738 fetch_vector4(&inst->SrcReg[1], machine, b);
1739 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -07001740 result[0] = a[0] + b[0] * c[0] + b[1] * c[1];
1741 result[1] = a[1] + b[0] * c[2] + b[1] * c[3];
1742 result[2] = a[2] + b[0] * c[0] + b[1] * c[1];
1743 result[3] = a[3] + b[0] * c[2] + b[1] * c[3];
1744 store_vector4(inst, machine, result);
1745 }
1746 break;
1747 case OPCODE_PRINT:
1748 {
1749 if (inst->SrcReg[0].File != -1) {
1750 GLfloat a[4];
Brian33eac562007-02-25 18:52:41 -07001751 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001752 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst->Data,
1753 a[0], a[1], a[2], a[3]);
1754 }
1755 else {
1756 _mesa_printf("%s\n", (const char *) inst->Data);
1757 }
1758 }
1759 break;
1760 case OPCODE_END:
1761 return GL_TRUE;
1762 default:
Alan Hourihane936dba12008-04-22 20:28:35 +01001763 _mesa_problem(ctx, "Bad opcode %d in _mesa_execute_program",
Briane80d9012007-02-23 16:53:24 -07001764 inst->Opcode);
1765 return GL_TRUE; /* return value doesn't matter */
Brian13e3b212007-02-22 16:09:40 -07001766 }
Briane80d9012007-02-23 16:53:24 -07001767
Briancfd00112007-02-25 18:30:45 -07001768 numExec++;
1769 if (numExec > maxExec) {
Brian13e3b212007-02-22 16:09:40 -07001770 _mesa_problem(ctx, "Infinite loop detected in fragment program");
1771 return GL_TRUE;
Brian13e3b212007-02-22 16:09:40 -07001772 }
Briane80d9012007-02-23 16:53:24 -07001773
1774 } /* for pc */
Brian13e3b212007-02-22 16:09:40 -07001775
1776#if FEATURE_MESA_program_debug
1777 CurrentMachine = NULL;
1778#endif
1779
1780 return GL_TRUE;
1781}