blob: f85c6513f3194c92d7c183674863a7841cce55d2 [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 "prog_execute.h"
42#include "prog_instruction.h"
43#include "prog_parameter.h"
44#include "prog_print.h"
Brian Paul702b5b02008-12-15 18:37:39 -070045#include "prog_noise.h"
Brian13e3b212007-02-22 16:09:40 -070046
47
Brian13e3b212007-02-22 16:09:40 -070048/* debug predicate */
49#define DEBUG_PROG 0
50
51
Brianf183a2d2007-02-23 17:14:30 -070052/**
53 * Set x to positive or negative infinity.
54 */
55#if defined(USE_IEEE) || defined(_WIN32)
Roland Scheidegger72362a52009-12-07 21:47:49 +010056#define SET_POS_INFINITY(x) \
57 do { \
58 fi_type fi; \
59 fi.i = 0x7F800000; \
60 x = fi.f; \
61 } while (0)
62#define SET_NEG_INFINITY(x) \
63 do { \
64 fi_type fi; \
65 fi.i = 0xFF800000; \
66 x = fi.f; \
67 } while (0)
Brianf183a2d2007-02-23 17:14:30 -070068#elif defined(VMS)
69#define SET_POS_INFINITY(x) x = __MAXFLOAT
70#define SET_NEG_INFINITY(x) x = -__MAXFLOAT
71#else
72#define SET_POS_INFINITY(x) x = (GLfloat) HUGE_VAL
73#define SET_NEG_INFINITY(x) x = (GLfloat) -HUGE_VAL
74#endif
75
76#define SET_FLOAT_BITS(x, bits) ((fi_type *) (void *) &(x))->i = bits
77
78
79static const GLfloat ZeroVec[4] = { 0.0F, 0.0F, 0.0F, 0.0F };
80
81
82
Brian13e3b212007-02-22 16:09:40 -070083/**
84 * Return a pointer to the 4-element float vector specified by the given
85 * source register.
86 */
87static INLINE const GLfloat *
Brian Paulf4361542008-11-11 10:47:10 -070088get_src_register_pointer(const struct prog_src_register *source,
89 const struct gl_program_machine *machine)
Brian13e3b212007-02-22 16:09:40 -070090{
Brian Paulf4361542008-11-11 10:47:10 -070091 const struct gl_program *prog = machine->CurProgram;
92 GLint reg = source->Index;
93
Brianf183a2d2007-02-23 17:14:30 -070094 if (source->RelAddr) {
Brian Paulf4361542008-11-11 10:47:10 -070095 /* add address register value to src index/offset */
96 reg += machine->AddressReg[0][0];
97 if (reg < 0) {
98 return ZeroVec;
Brianf183a2d2007-02-23 17:14:30 -070099 }
100 }
101
Brian13e3b212007-02-22 16:09:40 -0700102 switch (source->File) {
103 case PROGRAM_TEMPORARY:
Brian Paulf4361542008-11-11 10:47:10 -0700104 if (reg >= MAX_PROGRAM_TEMPS)
105 return ZeroVec;
106 return machine->Temporaries[reg];
Brian13e3b212007-02-22 16:09:40 -0700107
108 case PROGRAM_INPUT:
Brian Paulf4361542008-11-11 10:47:10 -0700109 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
110 if (reg >= VERT_ATTRIB_MAX)
111 return ZeroVec;
112 return machine->VertAttribs[reg];
Brian13e3b212007-02-22 16:09:40 -0700113 }
114 else {
Brian Paulf4361542008-11-11 10:47:10 -0700115 if (reg >= FRAG_ATTRIB_MAX)
116 return ZeroVec;
117 return machine->Attribs[reg][machine->CurElement];
Brian13e3b212007-02-22 16:09:40 -0700118 }
119
120 case PROGRAM_OUTPUT:
Brian Paulf4361542008-11-11 10:47:10 -0700121 if (reg >= MAX_PROGRAM_OUTPUTS)
122 return ZeroVec;
123 return machine->Outputs[reg];
Brian13e3b212007-02-22 16:09:40 -0700124
125 case PROGRAM_LOCAL_PARAM:
Brian Paulf4361542008-11-11 10:47:10 -0700126 if (reg >= MAX_PROGRAM_LOCAL_PARAMS)
127 return ZeroVec;
128 return machine->CurProgram->LocalParams[reg];
Brian13e3b212007-02-22 16:09:40 -0700129
130 case PROGRAM_ENV_PARAM:
Brian Paulf4361542008-11-11 10:47:10 -0700131 if (reg >= MAX_PROGRAM_ENV_PARAMS)
132 return ZeroVec;
133 return machine->EnvParams[reg];
Brian13e3b212007-02-22 16:09:40 -0700134
135 case PROGRAM_STATE_VAR:
136 /* Fallthrough */
137 case PROGRAM_CONSTANT:
138 /* Fallthrough */
139 case PROGRAM_UNIFORM:
140 /* Fallthrough */
141 case PROGRAM_NAMED_PARAM:
Brian Paulf4361542008-11-11 10:47:10 -0700142 if (reg >= (GLint) prog->Parameters->NumParameters)
143 return ZeroVec;
144 return prog->Parameters->ParameterValues[reg];
Brian13e3b212007-02-22 16:09:40 -0700145
146 default:
Brian33eac562007-02-25 18:52:41 -0700147 _mesa_problem(NULL,
Brian Paulf4361542008-11-11 10:47:10 -0700148 "Invalid src register file %d in get_src_register_pointer()",
149 source->File);
Brian13e3b212007-02-22 16:09:40 -0700150 return NULL;
151 }
152}
153
154
Brian Paulf4361542008-11-11 10:47:10 -0700155/**
156 * Return a pointer to the 4-element float vector specified by the given
157 * destination register.
158 */
159static INLINE GLfloat *
160get_dst_register_pointer(const struct prog_dst_register *dest,
161 struct gl_program_machine *machine)
162{
163 static GLfloat dummyReg[4];
164 GLint reg = dest->Index;
165
166 if (dest->RelAddr) {
167 /* add address register value to src index/offset */
168 reg += machine->AddressReg[0][0];
169 if (reg < 0) {
170 return dummyReg;
171 }
172 }
173
174 switch (dest->File) {
175 case PROGRAM_TEMPORARY:
176 if (reg >= MAX_PROGRAM_TEMPS)
177 return dummyReg;
178 return machine->Temporaries[reg];
179
180 case PROGRAM_OUTPUT:
181 if (reg >= MAX_PROGRAM_OUTPUTS)
182 return dummyReg;
183 return machine->Outputs[reg];
184
185 case PROGRAM_WRITE_ONLY:
186 return dummyReg;
187
188 default:
189 _mesa_problem(NULL,
190 "Invalid dest register file %d in get_dst_register_pointer()",
191 dest->File);
192 return NULL;
193 }
194}
195
196
197
Brian13e3b212007-02-22 16:09:40 -0700198/**
199 * Fetch a 4-element float vector from the given source register.
200 * Apply swizzling and negating as needed.
201 */
202static void
Brian33eac562007-02-25 18:52:41 -0700203fetch_vector4(const struct prog_src_register *source,
Briane80d9012007-02-23 16:53:24 -0700204 const struct gl_program_machine *machine, GLfloat result[4])
Brian13e3b212007-02-22 16:09:40 -0700205{
Brian Paulf4361542008-11-11 10:47:10 -0700206 const GLfloat *src = get_src_register_pointer(source, machine);
Brian13e3b212007-02-22 16:09:40 -0700207 ASSERT(src);
208
209 if (source->Swizzle == SWIZZLE_NOOP) {
210 /* no swizzling */
211 COPY_4V(result, src);
212 }
213 else {
214 ASSERT(GET_SWZ(source->Swizzle, 0) <= 3);
215 ASSERT(GET_SWZ(source->Swizzle, 1) <= 3);
216 ASSERT(GET_SWZ(source->Swizzle, 2) <= 3);
217 ASSERT(GET_SWZ(source->Swizzle, 3) <= 3);
218 result[0] = src[GET_SWZ(source->Swizzle, 0)];
219 result[1] = src[GET_SWZ(source->Swizzle, 1)];
220 result[2] = src[GET_SWZ(source->Swizzle, 2)];
221 result[3] = src[GET_SWZ(source->Swizzle, 3)];
222 }
223
Brian13e3b212007-02-22 16:09:40 -0700224 if (source->Abs) {
225 result[0] = FABSF(result[0]);
226 result[1] = FABSF(result[1]);
227 result[2] = FABSF(result[2]);
228 result[3] = FABSF(result[3]);
229 }
Brian Paul7db7ff82009-04-14 22:14:30 -0600230 if (source->Negate) {
231 ASSERT(source->Negate == NEGATE_XYZW);
Brian13e3b212007-02-22 16:09:40 -0700232 result[0] = -result[0];
233 result[1] = -result[1];
234 result[2] = -result[2];
235 result[3] = -result[3];
236 }
Brian Paul92009542009-06-03 15:43:53 -0600237
238#ifdef NAN_CHECK
239 assert(!IS_INF_OR_NAN(result[0]));
240 assert(!IS_INF_OR_NAN(result[0]));
241 assert(!IS_INF_OR_NAN(result[0]));
242 assert(!IS_INF_OR_NAN(result[0]));
243#endif
Brian13e3b212007-02-22 16:09:40 -0700244}
245
Brian62da6a12007-05-02 18:44:34 -0600246
Brian13e3b212007-02-22 16:09:40 -0700247/**
Brian Paul37eef7b2008-11-07 09:33:55 -0700248 * Fetch a 4-element uint vector from the given source register.
249 * Apply swizzling but not negation/abs.
250 */
251static void
252fetch_vector4ui(const struct prog_src_register *source,
253 const struct gl_program_machine *machine, GLuint result[4])
254{
Brian Paulf4361542008-11-11 10:47:10 -0700255 const GLuint *src = (GLuint *) get_src_register_pointer(source, machine);
Brian Paul37eef7b2008-11-07 09:33:55 -0700256 ASSERT(src);
257
258 if (source->Swizzle == SWIZZLE_NOOP) {
259 /* no swizzling */
260 COPY_4V(result, src);
261 }
262 else {
263 ASSERT(GET_SWZ(source->Swizzle, 0) <= 3);
264 ASSERT(GET_SWZ(source->Swizzle, 1) <= 3);
265 ASSERT(GET_SWZ(source->Swizzle, 2) <= 3);
266 ASSERT(GET_SWZ(source->Swizzle, 3) <= 3);
267 result[0] = src[GET_SWZ(source->Swizzle, 0)];
268 result[1] = src[GET_SWZ(source->Swizzle, 1)];
269 result[2] = src[GET_SWZ(source->Swizzle, 2)];
270 result[3] = src[GET_SWZ(source->Swizzle, 3)];
271 }
272
Brian Paul7db7ff82009-04-14 22:14:30 -0600273 /* Note: no Negate or Abs here */
Brian Paul37eef7b2008-11-07 09:33:55 -0700274}
275
276
277
278/**
Brian62da6a12007-05-02 18:44:34 -0600279 * Fetch the derivative with respect to X or Y for the given register.
280 * XXX this currently only works for fragment program input attribs.
Brian13e3b212007-02-22 16:09:40 -0700281 */
Brian62da6a12007-05-02 18:44:34 -0600282static void
Briane80d9012007-02-23 16:53:24 -0700283fetch_vector4_deriv(GLcontext * ctx,
284 const struct prog_src_register *source,
Brian62da6a12007-05-02 18:44:34 -0600285 const struct gl_program_machine *machine,
286 char xOrY, GLfloat result[4])
Brian13e3b212007-02-22 16:09:40 -0700287{
Brian Paul37eef7b2008-11-07 09:33:55 -0700288 if (source->File == PROGRAM_INPUT &&
289 source->Index < (GLint) machine->NumDeriv) {
Brian62da6a12007-05-02 18:44:34 -0600290 const GLint col = machine->CurElement;
291 const GLfloat w = machine->Attribs[FRAG_ATTRIB_WPOS][col][3];
292 const GLfloat invQ = 1.0f / w;
293 GLfloat deriv[4];
Brian13e3b212007-02-22 16:09:40 -0700294
Brian13e3b212007-02-22 16:09:40 -0700295 if (xOrY == 'X') {
Brian62da6a12007-05-02 18:44:34 -0600296 deriv[0] = machine->DerivX[source->Index][0] * invQ;
297 deriv[1] = machine->DerivX[source->Index][1] * invQ;
298 deriv[2] = machine->DerivX[source->Index][2] * invQ;
299 deriv[3] = machine->DerivX[source->Index][3] * invQ;
Brian13e3b212007-02-22 16:09:40 -0700300 }
301 else {
Brian62da6a12007-05-02 18:44:34 -0600302 deriv[0] = machine->DerivY[source->Index][0] * invQ;
303 deriv[1] = machine->DerivY[source->Index][1] * invQ;
304 deriv[2] = machine->DerivY[source->Index][2] * invQ;
305 deriv[3] = machine->DerivY[source->Index][3] * invQ;
Brian13e3b212007-02-22 16:09:40 -0700306 }
Brian13e3b212007-02-22 16:09:40 -0700307
Brian62da6a12007-05-02 18:44:34 -0600308 result[0] = deriv[GET_SWZ(source->Swizzle, 0)];
309 result[1] = deriv[GET_SWZ(source->Swizzle, 1)];
310 result[2] = deriv[GET_SWZ(source->Swizzle, 2)];
311 result[3] = deriv[GET_SWZ(source->Swizzle, 3)];
312
Brian62da6a12007-05-02 18:44:34 -0600313 if (source->Abs) {
314 result[0] = FABSF(result[0]);
315 result[1] = FABSF(result[1]);
316 result[2] = FABSF(result[2]);
317 result[3] = FABSF(result[3]);
318 }
Brian Paul7db7ff82009-04-14 22:14:30 -0600319 if (source->Negate) {
320 ASSERT(source->Negate == NEGATE_XYZW);
Brian62da6a12007-05-02 18:44:34 -0600321 result[0] = -result[0];
322 result[1] = -result[1];
323 result[2] = -result[2];
324 result[3] = -result[3];
325 }
Brian13e3b212007-02-22 16:09:40 -0700326 }
Brian62da6a12007-05-02 18:44:34 -0600327 else {
328 ASSIGN_4V(result, 0.0, 0.0, 0.0, 0.0);
Brian13e3b212007-02-22 16:09:40 -0700329 }
Brian13e3b212007-02-22 16:09:40 -0700330}
Brian13e3b212007-02-22 16:09:40 -0700331
332
333/**
334 * As above, but only return result[0] element.
335 */
336static void
Brian33eac562007-02-25 18:52:41 -0700337fetch_vector1(const struct prog_src_register *source,
Briane80d9012007-02-23 16:53:24 -0700338 const struct gl_program_machine *machine, GLfloat result[4])
Brian13e3b212007-02-22 16:09:40 -0700339{
Brian Paulf4361542008-11-11 10:47:10 -0700340 const GLfloat *src = get_src_register_pointer(source, machine);
Brian13e3b212007-02-22 16:09:40 -0700341 ASSERT(src);
342
343 result[0] = src[GET_SWZ(source->Swizzle, 0)];
344
Brian13e3b212007-02-22 16:09:40 -0700345 if (source->Abs) {
346 result[0] = FABSF(result[0]);
347 }
Brian Paul7db7ff82009-04-14 22:14:30 -0600348 if (source->Negate) {
Brian13e3b212007-02-22 16:09:40 -0700349 result[0] = -result[0];
350 }
351}
352
353
Brian Paul8d1a01d2010-01-22 15:36:28 -0700354static GLuint
355fetch_vector1ui(const struct prog_src_register *source,
356 const struct gl_program_machine *machine)
357{
358 const GLuint *src = (GLuint *) get_src_register_pointer(source, machine);
Brian Paul984b72a2010-02-19 12:41:21 -0700359 return src[GET_SWZ(source->Swizzle, 0)];
Brian Paul8d1a01d2010-01-22 15:36:28 -0700360}
361
362
Brian13e3b212007-02-22 16:09:40 -0700363/**
Brian999b5562007-11-23 12:01:57 -0700364 * Fetch texel from texture. Use partial derivatives when possible.
365 */
366static INLINE void
367fetch_texel(GLcontext *ctx,
368 const struct gl_program_machine *machine,
369 const struct prog_instruction *inst,
370 const GLfloat texcoord[4], GLfloat lodBias,
371 GLfloat color[4])
372{
Brian Paulade50832008-05-14 16:09:46 -0600373 const GLuint unit = machine->Samplers[inst->TexSrcUnit];
374
Brian999b5562007-11-23 12:01:57 -0700375 /* Note: we only have the right derivatives for fragment input attribs.
376 */
377 if (machine->NumDeriv > 0 &&
378 inst->SrcReg[0].File == PROGRAM_INPUT &&
379 inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0 + inst->TexSrcUnit) {
380 /* simple texture fetch for which we should have derivatives */
381 GLuint attr = inst->SrcReg[0].Index;
382 machine->FetchTexelDeriv(ctx, texcoord,
383 machine->DerivX[attr],
384 machine->DerivY[attr],
Brian Paulade50832008-05-14 16:09:46 -0600385 lodBias, unit, color);
Brian999b5562007-11-23 12:01:57 -0700386 }
387 else {
Brian Paulade50832008-05-14 16:09:46 -0600388 machine->FetchTexelLod(ctx, texcoord, lodBias, unit, color);
Brian999b5562007-11-23 12:01:57 -0700389 }
390}
391
392
393/**
Brian13e3b212007-02-22 16:09:40 -0700394 * Test value against zero and return GT, LT, EQ or UN if NaN.
395 */
396static INLINE GLuint
Briane80d9012007-02-23 16:53:24 -0700397generate_cc(float value)
Brian13e3b212007-02-22 16:09:40 -0700398{
399 if (value != value)
Briane80d9012007-02-23 16:53:24 -0700400 return COND_UN; /* NaN */
Brian13e3b212007-02-22 16:09:40 -0700401 if (value > 0.0F)
402 return COND_GT;
403 if (value < 0.0F)
404 return COND_LT;
405 return COND_EQ;
406}
407
408
409/**
410 * Test if the ccMaskRule is satisfied by the given condition code.
411 * Used to mask destination writes according to the current condition code.
412 */
413static INLINE GLboolean
414test_cc(GLuint condCode, GLuint ccMaskRule)
415{
416 switch (ccMaskRule) {
417 case COND_EQ: return (condCode == COND_EQ);
418 case COND_NE: return (condCode != COND_EQ);
419 case COND_LT: return (condCode == COND_LT);
420 case COND_GE: return (condCode == COND_GT || condCode == COND_EQ);
421 case COND_LE: return (condCode == COND_LT || condCode == COND_EQ);
422 case COND_GT: return (condCode == COND_GT);
423 case COND_TR: return GL_TRUE;
424 case COND_FL: return GL_FALSE;
425 default: return GL_TRUE;
426 }
427}
428
429
430/**
431 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
432 * or GL_FALSE to indicate result.
433 */
434static INLINE GLboolean
435eval_condition(const struct gl_program_machine *machine,
436 const struct prog_instruction *inst)
437{
438 const GLuint swizzle = inst->DstReg.CondSwizzle;
439 const GLuint condMask = inst->DstReg.CondMask;
440 if (test_cc(machine->CondCodes[GET_SWZ(swizzle, 0)], condMask) ||
441 test_cc(machine->CondCodes[GET_SWZ(swizzle, 1)], condMask) ||
442 test_cc(machine->CondCodes[GET_SWZ(swizzle, 2)], condMask) ||
443 test_cc(machine->CondCodes[GET_SWZ(swizzle, 3)], condMask)) {
444 return GL_TRUE;
445 }
446 else {
447 return GL_FALSE;
448 }
449}
450
451
452
453/**
454 * Store 4 floats into a register. Observe the instructions saturate and
455 * set-condition-code flags.
456 */
457static void
Briane80d9012007-02-23 16:53:24 -0700458store_vector4(const struct prog_instruction *inst,
459 struct gl_program_machine *machine, const GLfloat value[4])
Brian13e3b212007-02-22 16:09:40 -0700460{
Brian Paulf4361542008-11-11 10:47:10 -0700461 const struct prog_dst_register *dstReg = &(inst->DstReg);
Brian13e3b212007-02-22 16:09:40 -0700462 const GLboolean clamp = inst->SaturateMode == SATURATE_ZERO_ONE;
Brian Paulf4361542008-11-11 10:47:10 -0700463 GLuint writeMask = dstReg->WriteMask;
Brian13e3b212007-02-22 16:09:40 -0700464 GLfloat clampedValue[4];
Brian Paulf4361542008-11-11 10:47:10 -0700465 GLfloat *dst = get_dst_register_pointer(dstReg, machine);
Brian13e3b212007-02-22 16:09:40 -0700466
467#if 0
468 if (value[0] > 1.0e10 ||
469 IS_INF_OR_NAN(value[0]) ||
470 IS_INF_OR_NAN(value[1]) ||
Briane80d9012007-02-23 16:53:24 -0700471 IS_INF_OR_NAN(value[2]) || IS_INF_OR_NAN(value[3]))
Brian13e3b212007-02-22 16:09:40 -0700472 printf("store %g %g %g %g\n", value[0], value[1], value[2], value[3]);
473#endif
474
475 if (clamp) {
476 clampedValue[0] = CLAMP(value[0], 0.0F, 1.0F);
477 clampedValue[1] = CLAMP(value[1], 0.0F, 1.0F);
478 clampedValue[2] = CLAMP(value[2], 0.0F, 1.0F);
479 clampedValue[3] = CLAMP(value[3], 0.0F, 1.0F);
480 value = clampedValue;
481 }
482
Brian Paulf4361542008-11-11 10:47:10 -0700483 if (dstReg->CondMask != COND_TR) {
Brian13e3b212007-02-22 16:09:40 -0700484 /* condition codes may turn off some writes */
485 if (writeMask & WRITEMASK_X) {
Brian Paulf4361542008-11-11 10:47:10 -0700486 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 0)],
487 dstReg->CondMask))
Brian13e3b212007-02-22 16:09:40 -0700488 writeMask &= ~WRITEMASK_X;
489 }
490 if (writeMask & WRITEMASK_Y) {
Brian Paulf4361542008-11-11 10:47:10 -0700491 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 1)],
492 dstReg->CondMask))
Brian13e3b212007-02-22 16:09:40 -0700493 writeMask &= ~WRITEMASK_Y;
494 }
495 if (writeMask & WRITEMASK_Z) {
Brian Paulf4361542008-11-11 10:47:10 -0700496 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 2)],
497 dstReg->CondMask))
Brian13e3b212007-02-22 16:09:40 -0700498 writeMask &= ~WRITEMASK_Z;
499 }
500 if (writeMask & WRITEMASK_W) {
Brian Paulf4361542008-11-11 10:47:10 -0700501 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 3)],
502 dstReg->CondMask))
Brian13e3b212007-02-22 16:09:40 -0700503 writeMask &= ~WRITEMASK_W;
504 }
505 }
506
Brian Paul92009542009-06-03 15:43:53 -0600507#ifdef NAN_CHECK
508 assert(!IS_INF_OR_NAN(value[0]));
509 assert(!IS_INF_OR_NAN(value[0]));
510 assert(!IS_INF_OR_NAN(value[0]));
511 assert(!IS_INF_OR_NAN(value[0]));
512#endif
513
Brian13e3b212007-02-22 16:09:40 -0700514 if (writeMask & WRITEMASK_X)
Brian Paulf4361542008-11-11 10:47:10 -0700515 dst[0] = value[0];
Brian13e3b212007-02-22 16:09:40 -0700516 if (writeMask & WRITEMASK_Y)
Brian Paulf4361542008-11-11 10:47:10 -0700517 dst[1] = value[1];
Brian13e3b212007-02-22 16:09:40 -0700518 if (writeMask & WRITEMASK_Z)
Brian Paulf4361542008-11-11 10:47:10 -0700519 dst[2] = value[2];
Brian13e3b212007-02-22 16:09:40 -0700520 if (writeMask & WRITEMASK_W)
Brian Paulf4361542008-11-11 10:47:10 -0700521 dst[3] = value[3];
Brian13e3b212007-02-22 16:09:40 -0700522
523 if (inst->CondUpdate) {
524 if (writeMask & WRITEMASK_X)
525 machine->CondCodes[0] = generate_cc(value[0]);
526 if (writeMask & WRITEMASK_Y)
527 machine->CondCodes[1] = generate_cc(value[1]);
528 if (writeMask & WRITEMASK_Z)
529 machine->CondCodes[2] = generate_cc(value[2]);
530 if (writeMask & WRITEMASK_W)
531 machine->CondCodes[3] = generate_cc(value[3]);
Briana01616e2007-03-28 11:01:28 -0600532#if DEBUG_PROG
533 printf("CondCodes=(%s,%s,%s,%s) for:\n",
534 _mesa_condcode_string(machine->CondCodes[0]),
535 _mesa_condcode_string(machine->CondCodes[1]),
536 _mesa_condcode_string(machine->CondCodes[2]),
537 _mesa_condcode_string(machine->CondCodes[3]));
538#endif
Brian13e3b212007-02-22 16:09:40 -0700539 }
540}
541
542
Brian13e3b212007-02-22 16:09:40 -0700543/**
Brian Paul37eef7b2008-11-07 09:33:55 -0700544 * Store 4 uints into a register. Observe the set-condition-code flags.
545 */
546static void
547store_vector4ui(const struct prog_instruction *inst,
548 struct gl_program_machine *machine, const GLuint value[4])
549{
Brian Paulf4361542008-11-11 10:47:10 -0700550 const struct prog_dst_register *dstReg = &(inst->DstReg);
551 GLuint writeMask = dstReg->WriteMask;
552 GLuint *dst = (GLuint *) get_dst_register_pointer(dstReg, machine);
Brian Paul37eef7b2008-11-07 09:33:55 -0700553
Brian Paulf4361542008-11-11 10:47:10 -0700554 if (dstReg->CondMask != COND_TR) {
Brian Paul37eef7b2008-11-07 09:33:55 -0700555 /* condition codes may turn off some writes */
556 if (writeMask & WRITEMASK_X) {
Brian Paulf4361542008-11-11 10:47:10 -0700557 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 0)],
558 dstReg->CondMask))
Brian Paul37eef7b2008-11-07 09:33:55 -0700559 writeMask &= ~WRITEMASK_X;
560 }
561 if (writeMask & WRITEMASK_Y) {
Brian Paulf4361542008-11-11 10:47:10 -0700562 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 1)],
563 dstReg->CondMask))
Brian Paul37eef7b2008-11-07 09:33:55 -0700564 writeMask &= ~WRITEMASK_Y;
565 }
566 if (writeMask & WRITEMASK_Z) {
Brian Paulf4361542008-11-11 10:47:10 -0700567 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 2)],
568 dstReg->CondMask))
Brian Paul37eef7b2008-11-07 09:33:55 -0700569 writeMask &= ~WRITEMASK_Z;
570 }
571 if (writeMask & WRITEMASK_W) {
Brian Paulf4361542008-11-11 10:47:10 -0700572 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 3)],
573 dstReg->CondMask))
Brian Paul37eef7b2008-11-07 09:33:55 -0700574 writeMask &= ~WRITEMASK_W;
575 }
576 }
577
578 if (writeMask & WRITEMASK_X)
Brian Paulf4361542008-11-11 10:47:10 -0700579 dst[0] = value[0];
Brian Paul37eef7b2008-11-07 09:33:55 -0700580 if (writeMask & WRITEMASK_Y)
Brian Paulf4361542008-11-11 10:47:10 -0700581 dst[1] = value[1];
Brian Paul37eef7b2008-11-07 09:33:55 -0700582 if (writeMask & WRITEMASK_Z)
Brian Paulf4361542008-11-11 10:47:10 -0700583 dst[2] = value[2];
Brian Paul37eef7b2008-11-07 09:33:55 -0700584 if (writeMask & WRITEMASK_W)
Brian Paulf4361542008-11-11 10:47:10 -0700585 dst[3] = value[3];
Brian Paul37eef7b2008-11-07 09:33:55 -0700586
587 if (inst->CondUpdate) {
588 if (writeMask & WRITEMASK_X)
Karl Schultzb30898f2010-02-13 17:31:58 -0700589 machine->CondCodes[0] = generate_cc((float)value[0]);
Brian Paul37eef7b2008-11-07 09:33:55 -0700590 if (writeMask & WRITEMASK_Y)
Karl Schultzb30898f2010-02-13 17:31:58 -0700591 machine->CondCodes[1] = generate_cc((float)value[1]);
Brian Paul37eef7b2008-11-07 09:33:55 -0700592 if (writeMask & WRITEMASK_Z)
Karl Schultzb30898f2010-02-13 17:31:58 -0700593 machine->CondCodes[2] = generate_cc((float)value[2]);
Brian Paul37eef7b2008-11-07 09:33:55 -0700594 if (writeMask & WRITEMASK_W)
Karl Schultzb30898f2010-02-13 17:31:58 -0700595 machine->CondCodes[3] = generate_cc((float)value[3]);
Brian Paul37eef7b2008-11-07 09:33:55 -0700596#if DEBUG_PROG
597 printf("CondCodes=(%s,%s,%s,%s) for:\n",
598 _mesa_condcode_string(machine->CondCodes[0]),
599 _mesa_condcode_string(machine->CondCodes[1]),
600 _mesa_condcode_string(machine->CondCodes[2]),
601 _mesa_condcode_string(machine->CondCodes[3]));
602#endif
603 }
604}
605
606
607
608/**
Brian13e3b212007-02-22 16:09:40 -0700609 * Execute the given vertex/fragment program.
610 *
Brian3c1c9992007-02-25 19:11:44 -0700611 * \param ctx rendering context
612 * \param program the program to execute
613 * \param machine machine state (must be initialized)
Brian13e3b212007-02-22 16:09:40 -0700614 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
615 */
616GLboolean
Briane80d9012007-02-23 16:53:24 -0700617_mesa_execute_program(GLcontext * ctx,
Brian8b34b7d2007-02-25 18:26:50 -0700618 const struct gl_program *program,
Brian085d7d52007-02-25 18:23:37 -0700619 struct gl_program_machine *machine)
Brian13e3b212007-02-22 16:09:40 -0700620{
Brian8b34b7d2007-02-25 18:26:50 -0700621 const GLuint numInst = program->NumInstructions;
Briancfd00112007-02-25 18:30:45 -0700622 const GLuint maxExec = 10000;
José Fonseca452a5922008-05-31 18:14:09 +0900623 GLuint pc, numExec = 0;
Brian13e3b212007-02-22 16:09:40 -0700624
625 machine->CurProgram = program;
626
627 if (DEBUG_PROG) {
628 printf("execute program %u --------------------\n", program->Id);
629 }
630
Brian33eac562007-02-25 18:52:41 -0700631 if (program->Target == GL_VERTEX_PROGRAM_ARB) {
632 machine->EnvParams = ctx->VertexProgram.Parameters;
633 }
634 else {
635 machine->EnvParams = ctx->FragmentProgram.Parameters;
636 }
637
Brian8b34b7d2007-02-25 18:26:50 -0700638 for (pc = 0; pc < numInst; pc++) {
Brian13e3b212007-02-22 16:09:40 -0700639 const struct prog_instruction *inst = program->Instructions + pc;
640
Brian13e3b212007-02-22 16:09:40 -0700641 if (DEBUG_PROG) {
642 _mesa_print_instruction(inst);
643 }
644
645 switch (inst->Opcode) {
Briane80d9012007-02-23 16:53:24 -0700646 case OPCODE_ABS:
647 {
648 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700649 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700650 result[0] = FABSF(a[0]);
651 result[1] = FABSF(a[1]);
652 result[2] = FABSF(a[2]);
653 result[3] = FABSF(a[3]);
654 store_vector4(inst, machine, result);
655 }
656 break;
657 case OPCODE_ADD:
658 {
659 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700660 fetch_vector4(&inst->SrcReg[0], machine, a);
661 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700662 result[0] = a[0] + b[0];
663 result[1] = a[1] + b[1];
664 result[2] = a[2] + b[2];
665 result[3] = a[3] + b[3];
666 store_vector4(inst, machine, result);
667 if (DEBUG_PROG) {
668 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
669 result[0], result[1], result[2], result[3],
670 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -0700671 }
Briane80d9012007-02-23 16:53:24 -0700672 }
673 break;
Brian Paul37eef7b2008-11-07 09:33:55 -0700674 case OPCODE_AND: /* bitwise AND */
675 {
676 GLuint a[4], b[4], result[4];
677 fetch_vector4ui(&inst->SrcReg[0], machine, a);
678 fetch_vector4ui(&inst->SrcReg[1], machine, b);
679 result[0] = a[0] & b[0];
680 result[1] = a[1] & b[1];
681 result[2] = a[2] & b[2];
682 result[3] = a[3] & b[3];
683 store_vector4ui(inst, machine, result);
684 }
685 break;
Brianf183a2d2007-02-23 17:14:30 -0700686 case OPCODE_ARL:
687 {
688 GLfloat t[4];
Brian33eac562007-02-25 18:52:41 -0700689 fetch_vector4(&inst->SrcReg[0], machine, t);
Brian Paula9475cc2008-12-12 18:03:48 -0700690 machine->AddressReg[0][0] = IFLOOR(t[0]);
Brian Paula636f5b2010-02-05 14:58:46 -0700691 if (DEBUG_PROG) {
692 printf("ARL %d\n", machine->AddressReg[0][0]);
693 }
Brianf183a2d2007-02-23 17:14:30 -0700694 }
695 break;
Briane80d9012007-02-23 16:53:24 -0700696 case OPCODE_BGNLOOP:
697 /* no-op */
Brian Paule8ea2d22009-12-22 12:57:31 -0700698 ASSERT(program->Instructions[inst->BranchTarget].Opcode
699 == OPCODE_ENDLOOP);
Briane80d9012007-02-23 16:53:24 -0700700 break;
701 case OPCODE_ENDLOOP:
702 /* subtract 1 here since pc is incremented by for(pc) loop */
Brian Paule8ea2d22009-12-22 12:57:31 -0700703 ASSERT(program->Instructions[inst->BranchTarget].Opcode
704 == OPCODE_BGNLOOP);
Briane80d9012007-02-23 16:53:24 -0700705 pc = inst->BranchTarget - 1; /* go to matching BNGLOOP */
706 break;
707 case OPCODE_BGNSUB: /* begin subroutine */
708 break;
709 case OPCODE_ENDSUB: /* end subroutine */
710 break;
711 case OPCODE_BRA: /* branch (conditional) */
Briane80d9012007-02-23 16:53:24 -0700712 if (eval_condition(machine, inst)) {
713 /* take branch */
Brian Pauldb721152009-12-22 14:15:30 -0700714 /* Subtract 1 here since we'll do pc++ below */
715 pc = inst->BranchTarget - 1;
716 }
717 break;
718 case OPCODE_BRK: /* break out of loop (conditional) */
719 ASSERT(program->Instructions[inst->BranchTarget].Opcode
720 == OPCODE_ENDLOOP);
721 if (eval_condition(machine, inst)) {
722 /* break out of loop */
723 /* pc++ at end of for-loop will put us after the ENDLOOP inst */
724 pc = inst->BranchTarget;
725 }
726 break;
727 case OPCODE_CONT: /* continue loop (conditional) */
728 ASSERT(program->Instructions[inst->BranchTarget].Opcode
729 == OPCODE_ENDLOOP);
730 if (eval_condition(machine, inst)) {
731 /* continue at ENDLOOP */
Briane80d9012007-02-23 16:53:24 -0700732 /* Subtract 1 here since we'll do pc++ at end of for-loop */
733 pc = inst->BranchTarget - 1;
734 }
735 break;
736 case OPCODE_CAL: /* Call subroutine (conditional) */
737 if (eval_condition(machine, inst)) {
738 /* call the subroutine */
739 if (machine->StackDepth >= MAX_PROGRAM_CALL_DEPTH) {
740 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
Brian13e3b212007-02-22 16:09:40 -0700741 }
Briana0275b02007-03-27 11:02:20 -0600742 machine->CallStack[machine->StackDepth++] = pc + 1; /* next inst */
Brian31dc7a32007-03-27 15:21:35 -0600743 /* Subtract 1 here since we'll do pc++ at end of for-loop */
744 pc = inst->BranchTarget - 1;
Briane80d9012007-02-23 16:53:24 -0700745 }
746 break;
747 case OPCODE_CMP:
748 {
749 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700750 fetch_vector4(&inst->SrcReg[0], machine, a);
751 fetch_vector4(&inst->SrcReg[1], machine, b);
752 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -0700753 result[0] = a[0] < 0.0F ? b[0] : c[0];
754 result[1] = a[1] < 0.0F ? b[1] : c[1];
755 result[2] = a[2] < 0.0F ? b[2] : c[2];
756 result[3] = a[3] < 0.0F ? b[3] : c[3];
757 store_vector4(inst, machine, result);
758 }
759 break;
760 case OPCODE_COS:
761 {
762 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700763 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700764 result[0] = result[1] = result[2] = result[3]
Eric Anholt165694a2010-05-05 08:36:20 -0700765 = (GLfloat) cos(a[0]);
Briane80d9012007-02-23 16:53:24 -0700766 store_vector4(inst, machine, result);
767 }
768 break;
769 case OPCODE_DDX: /* Partial derivative with respect to X */
770 {
Brian62da6a12007-05-02 18:44:34 -0600771 GLfloat result[4];
772 fetch_vector4_deriv(ctx, &inst->SrcReg[0], machine,
773 'X', result);
Briane80d9012007-02-23 16:53:24 -0700774 store_vector4(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -0700775 }
776 break;
777 case OPCODE_DDY: /* Partial derivative with respect to Y */
778 {
Brian62da6a12007-05-02 18:44:34 -0600779 GLfloat result[4];
780 fetch_vector4_deriv(ctx, &inst->SrcReg[0], machine,
781 'Y', result);
Briane80d9012007-02-23 16:53:24 -0700782 store_vector4(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -0700783 }
784 break;
Brian Paul65cb74e2008-11-07 09:41:00 -0700785 case OPCODE_DP2:
786 {
787 GLfloat a[4], b[4], result[4];
788 fetch_vector4(&inst->SrcReg[0], machine, a);
789 fetch_vector4(&inst->SrcReg[1], machine, b);
790 result[0] = result[1] = result[2] = result[3] = DOT2(a, b);
791 store_vector4(inst, machine, result);
792 if (DEBUG_PROG) {
793 printf("DP2 %g = (%g %g) . (%g %g)\n",
794 result[0], a[0], a[1], b[0], b[1]);
795 }
796 }
797 break;
798 case OPCODE_DP2A:
799 {
800 GLfloat a[4], b[4], c, result[4];
801 fetch_vector4(&inst->SrcReg[0], machine, a);
802 fetch_vector4(&inst->SrcReg[1], machine, b);
803 fetch_vector1(&inst->SrcReg[1], machine, &c);
804 result[0] = result[1] = result[2] = result[3] = DOT2(a, b) + c;
805 store_vector4(inst, machine, result);
806 if (DEBUG_PROG) {
807 printf("DP2A %g = (%g %g) . (%g %g) + %g\n",
808 result[0], a[0], a[1], b[0], b[1], c);
809 }
810 }
811 break;
Briane80d9012007-02-23 16:53:24 -0700812 case OPCODE_DP3:
813 {
814 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700815 fetch_vector4(&inst->SrcReg[0], machine, a);
816 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700817 result[0] = result[1] = result[2] = result[3] = DOT3(a, b);
818 store_vector4(inst, machine, result);
819 if (DEBUG_PROG) {
820 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
821 result[0], a[0], a[1], a[2], b[0], b[1], b[2]);
Brian13e3b212007-02-22 16:09:40 -0700822 }
Briane80d9012007-02-23 16:53:24 -0700823 }
824 break;
825 case OPCODE_DP4:
826 {
827 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700828 fetch_vector4(&inst->SrcReg[0], machine, a);
829 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700830 result[0] = result[1] = result[2] = result[3] = DOT4(a, b);
831 store_vector4(inst, machine, result);
832 if (DEBUG_PROG) {
833 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
834 result[0], a[0], a[1], a[2], a[3],
835 b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -0700836 }
Briane80d9012007-02-23 16:53:24 -0700837 }
838 break;
839 case OPCODE_DPH:
840 {
841 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700842 fetch_vector4(&inst->SrcReg[0], machine, a);
843 fetch_vector4(&inst->SrcReg[1], machine, b);
Brian Paul65cb74e2008-11-07 09:41:00 -0700844 result[0] = result[1] = result[2] = result[3] = DOT3(a, b) + b[3];
Briane80d9012007-02-23 16:53:24 -0700845 store_vector4(inst, machine, result);
846 }
847 break;
848 case OPCODE_DST: /* Distance vector */
849 {
850 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700851 fetch_vector4(&inst->SrcReg[0], machine, a);
852 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700853 result[0] = 1.0F;
854 result[1] = a[1] * b[1];
855 result[2] = a[2];
856 result[3] = b[3];
857 store_vector4(inst, machine, result);
858 }
859 break;
Brianf183a2d2007-02-23 17:14:30 -0700860 case OPCODE_EXP:
Brianf183a2d2007-02-23 17:14:30 -0700861 {
862 GLfloat t[4], q[4], floor_t0;
Brian33eac562007-02-25 18:52:41 -0700863 fetch_vector1(&inst->SrcReg[0], machine, t);
Brianf183a2d2007-02-23 17:14:30 -0700864 floor_t0 = FLOORF(t[0]);
865 if (floor_t0 > FLT_MAX_EXP) {
866 SET_POS_INFINITY(q[0]);
867 SET_POS_INFINITY(q[2]);
868 }
869 else if (floor_t0 < FLT_MIN_EXP) {
870 q[0] = 0.0F;
871 q[2] = 0.0F;
872 }
873 else {
Brian761728a2007-02-24 11:14:57 -0700874 q[0] = LDEXPF(1.0, (int) floor_t0);
875 /* Note: GL_NV_vertex_program expects
876 * result.z = result.x * APPX(result.y)
877 * We do what the ARB extension says.
878 */
Eric Anholtfef303b2010-05-05 08:29:13 -0700879 q[2] = (GLfloat) pow(2.0, t[0]);
Brianf183a2d2007-02-23 17:14:30 -0700880 }
881 q[1] = t[0] - floor_t0;
882 q[3] = 1.0F;
883 store_vector4( inst, machine, q );
884 }
885 break;
Briane80d9012007-02-23 16:53:24 -0700886 case OPCODE_EX2: /* Exponential base 2 */
887 {
Brian Paul035de6a2009-06-03 15:42:52 -0600888 GLfloat a[4], result[4], val;
Brian33eac562007-02-25 18:52:41 -0700889 fetch_vector1(&inst->SrcReg[0], machine, a);
Eric Anholtfef303b2010-05-05 08:29:13 -0700890 val = (GLfloat) pow(2.0, a[0]);
Brian Paul035de6a2009-06-03 15:42:52 -0600891 /*
892 if (IS_INF_OR_NAN(val))
893 val = 1.0e10;
894 */
895 result[0] = result[1] = result[2] = result[3] = val;
Briane80d9012007-02-23 16:53:24 -0700896 store_vector4(inst, machine, result);
897 }
898 break;
899 case OPCODE_FLR:
900 {
901 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700902 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700903 result[0] = FLOORF(a[0]);
904 result[1] = FLOORF(a[1]);
905 result[2] = FLOORF(a[2]);
906 result[3] = FLOORF(a[3]);
907 store_vector4(inst, machine, result);
908 }
909 break;
910 case OPCODE_FRC:
911 {
912 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700913 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700914 result[0] = a[0] - FLOORF(a[0]);
915 result[1] = a[1] - FLOORF(a[1]);
916 result[2] = a[2] - FLOORF(a[2]);
917 result[3] = a[3] - FLOORF(a[3]);
918 store_vector4(inst, machine, result);
919 }
920 break;
921 case OPCODE_IF:
Brian63556fa2007-03-23 14:47:46 -0600922 {
923 GLboolean cond;
Brian Paulddd97292009-12-22 14:21:07 -0700924 ASSERT(program->Instructions[inst->BranchTarget].Opcode
925 == OPCODE_ELSE ||
926 program->Instructions[inst->BranchTarget].Opcode
927 == OPCODE_ENDIF);
Brian63556fa2007-03-23 14:47:46 -0600928 /* eval condition */
929 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
930 GLfloat a[4];
931 fetch_vector1(&inst->SrcReg[0], machine, a);
932 cond = (a[0] != 0.0);
933 }
934 else {
935 cond = eval_condition(machine, inst);
936 }
Briana7f73662007-04-20 08:11:51 -0600937 if (DEBUG_PROG) {
938 printf("IF: %d\n", cond);
939 }
Brian63556fa2007-03-23 14:47:46 -0600940 /* do if/else */
941 if (cond) {
942 /* do if-clause (just continue execution) */
943 }
944 else {
945 /* go to the instruction after ELSE or ENDIF */
946 assert(inst->BranchTarget >= 0);
Brian Paulddd97292009-12-22 14:21:07 -0700947 pc = inst->BranchTarget;
Brian63556fa2007-03-23 14:47:46 -0600948 }
Briane80d9012007-02-23 16:53:24 -0700949 }
950 break;
951 case OPCODE_ELSE:
952 /* goto ENDIF */
Brian Paulddd97292009-12-22 14:21:07 -0700953 ASSERT(program->Instructions[inst->BranchTarget].Opcode
954 == OPCODE_ENDIF);
Briane80d9012007-02-23 16:53:24 -0700955 assert(inst->BranchTarget >= 0);
Brian Paulddd97292009-12-22 14:21:07 -0700956 pc = inst->BranchTarget;
Briane80d9012007-02-23 16:53:24 -0700957 break;
958 case OPCODE_ENDIF:
959 /* nothing */
960 break;
Briane80d9012007-02-23 16:53:24 -0700961 case OPCODE_KIL_NV: /* NV_f_p only (conditional) */
962 if (eval_condition(machine, inst)) {
963 return GL_FALSE;
964 }
965 break;
966 case OPCODE_KIL: /* ARB_f_p only */
967 {
968 GLfloat a[4];
Brian33eac562007-02-25 18:52:41 -0700969 fetch_vector4(&inst->SrcReg[0], machine, a);
Brian Paulc0633dd2009-08-31 14:57:59 -0600970 if (DEBUG_PROG) {
971 printf("KIL if (%g %g %g %g) <= 0.0\n",
972 a[0], a[1], a[2], a[3]);
973 }
974
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 {
Brian Paul035de6a2009-06-03 15:42:52 -0600982 GLfloat a[4], result[4], val;
Brian33eac562007-02-25 18:52:41 -0700983 fetch_vector1(&inst->SrcReg[0], machine, a);
Ian Romanick962fa6b2008-12-18 14:11:06 -0800984 /* The fast LOG2 macro doesn't meet the precision requirements.
985 */
Brian Paul035de6a2009-06-03 15:42:52 -0600986 if (a[0] == 0.0F) {
Vinson Lee18883cd2009-10-01 13:33:20 -0600987 val = -FLT_MAX;
Brian Paul035de6a2009-06-03 15:42:52 -0600988 }
989 else {
Karl Schultzb30898f2010-02-13 17:31:58 -0700990 val = (float)(log(a[0]) * 1.442695F);
Brian Paul035de6a2009-06-03 15:42:52 -0600991 }
992 result[0] = result[1] = result[2] = result[3] = val;
Briane80d9012007-02-23 16:53:24 -0700993 store_vector4(inst, machine, result);
994 }
995 break;
996 case OPCODE_LIT:
997 {
998 const GLfloat epsilon = 1.0F / 256.0F; /* from NV VP spec */
999 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001000 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001001 a[0] = MAX2(a[0], 0.0F);
1002 a[1] = MAX2(a[1], 0.0F);
1003 /* XXX ARB version clamps a[3], NV version doesn't */
1004 a[3] = CLAMP(a[3], -(128.0F - epsilon), (128.0F - epsilon));
1005 result[0] = 1.0F;
1006 result[1] = a[0];
1007 /* XXX we could probably just use pow() here */
1008 if (a[0] > 0.0F) {
1009 if (a[1] == 0.0 && a[3] == 0.0)
Brian Paul20fbb242010-01-27 17:03:04 -07001010 result[2] = 1.0F;
Briane80d9012007-02-23 16:53:24 -07001011 else
Eric Anholtfef303b2010-05-05 08:29:13 -07001012 result[2] = (GLfloat) pow(a[1], a[3]);
Brian13e3b212007-02-22 16:09:40 -07001013 }
Briane80d9012007-02-23 16:53:24 -07001014 else {
Brian Paul20fbb242010-01-27 17:03:04 -07001015 result[2] = 0.0F;
Brian13e3b212007-02-22 16:09:40 -07001016 }
Briane80d9012007-02-23 16:53:24 -07001017 result[3] = 1.0F;
1018 store_vector4(inst, machine, result);
1019 if (DEBUG_PROG) {
1020 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
1021 result[0], result[1], result[2], result[3],
1022 a[0], a[1], a[2], a[3]);
Brian13e3b212007-02-22 16:09:40 -07001023 }
Briane80d9012007-02-23 16:53:24 -07001024 }
1025 break;
Brianf183a2d2007-02-23 17:14:30 -07001026 case OPCODE_LOG:
1027 {
1028 GLfloat t[4], q[4], abs_t0;
Brian33eac562007-02-25 18:52:41 -07001029 fetch_vector1(&inst->SrcReg[0], machine, t);
Brianf183a2d2007-02-23 17:14:30 -07001030 abs_t0 = FABSF(t[0]);
1031 if (abs_t0 != 0.0F) {
1032 /* Since we really can't handle infinite values on VMS
1033 * like other OSes we'll use __MAXFLOAT to represent
1034 * infinity. This may need some tweaking.
1035 */
1036#ifdef VMS
1037 if (abs_t0 == __MAXFLOAT)
1038#else
1039 if (IS_INF_OR_NAN(abs_t0))
1040#endif
1041 {
1042 SET_POS_INFINITY(q[0]);
1043 q[1] = 1.0F;
1044 SET_POS_INFINITY(q[2]);
1045 }
1046 else {
1047 int exponent;
1048 GLfloat mantissa = FREXPF(t[0], &exponent);
1049 q[0] = (GLfloat) (exponent - 1);
1050 q[1] = (GLfloat) (2.0 * mantissa); /* map [.5, 1) -> [1, 2) */
Ian Romanick962fa6b2008-12-18 14:11:06 -08001051
1052 /* The fast LOG2 macro doesn't meet the precision
1053 * requirements.
1054 */
Karl Schultzb30898f2010-02-13 17:31:58 -07001055 q[2] = (float)(log(t[0]) * 1.442695F);
Brianf183a2d2007-02-23 17:14:30 -07001056 }
1057 }
1058 else {
1059 SET_NEG_INFINITY(q[0]);
1060 q[1] = 1.0F;
1061 SET_NEG_INFINITY(q[2]);
1062 }
1063 q[3] = 1.0;
1064 store_vector4(inst, machine, q);
1065 }
1066 break;
Briane80d9012007-02-23 16:53:24 -07001067 case OPCODE_LRP:
1068 {
1069 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001070 fetch_vector4(&inst->SrcReg[0], machine, a);
1071 fetch_vector4(&inst->SrcReg[1], machine, b);
1072 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -07001073 result[0] = a[0] * b[0] + (1.0F - a[0]) * c[0];
1074 result[1] = a[1] * b[1] + (1.0F - a[1]) * c[1];
1075 result[2] = a[2] * b[2] + (1.0F - a[2]) * c[2];
1076 result[3] = a[3] * b[3] + (1.0F - a[3]) * c[3];
1077 store_vector4(inst, machine, result);
1078 if (DEBUG_PROG) {
1079 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
1080 "(%g %g %g %g), (%g %g %g %g)\n",
1081 result[0], result[1], result[2], result[3],
1082 a[0], a[1], a[2], a[3],
1083 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
Brian13e3b212007-02-22 16:09:40 -07001084 }
Briane80d9012007-02-23 16:53:24 -07001085 }
1086 break;
1087 case OPCODE_MAD:
1088 {
1089 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001090 fetch_vector4(&inst->SrcReg[0], machine, a);
1091 fetch_vector4(&inst->SrcReg[1], machine, b);
1092 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -07001093 result[0] = a[0] * b[0] + c[0];
1094 result[1] = a[1] * b[1] + c[1];
1095 result[2] = a[2] * b[2] + c[2];
1096 result[3] = a[3] * b[3] + c[3];
1097 store_vector4(inst, machine, result);
1098 if (DEBUG_PROG) {
1099 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
1100 "(%g %g %g %g) + (%g %g %g %g)\n",
1101 result[0], result[1], result[2], result[3],
1102 a[0], a[1], a[2], a[3],
1103 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
Brian13e3b212007-02-22 16:09:40 -07001104 }
Briane80d9012007-02-23 16:53:24 -07001105 }
1106 break;
1107 case OPCODE_MAX:
1108 {
1109 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001110 fetch_vector4(&inst->SrcReg[0], machine, a);
1111 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001112 result[0] = MAX2(a[0], b[0]);
1113 result[1] = MAX2(a[1], b[1]);
1114 result[2] = MAX2(a[2], b[2]);
1115 result[3] = MAX2(a[3], b[3]);
1116 store_vector4(inst, machine, result);
1117 if (DEBUG_PROG) {
1118 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
1119 result[0], result[1], result[2], result[3],
1120 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001121 }
Briane80d9012007-02-23 16:53:24 -07001122 }
1123 break;
1124 case OPCODE_MIN:
1125 {
1126 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001127 fetch_vector4(&inst->SrcReg[0], machine, a);
1128 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001129 result[0] = MIN2(a[0], b[0]);
1130 result[1] = MIN2(a[1], b[1]);
1131 result[2] = MIN2(a[2], b[2]);
1132 result[3] = MIN2(a[3], b[3]);
1133 store_vector4(inst, machine, result);
1134 }
1135 break;
1136 case OPCODE_MOV:
1137 {
1138 GLfloat result[4];
Brian33eac562007-02-25 18:52:41 -07001139 fetch_vector4(&inst->SrcReg[0], machine, result);
Briane80d9012007-02-23 16:53:24 -07001140 store_vector4(inst, machine, result);
1141 if (DEBUG_PROG) {
1142 printf("MOV (%g %g %g %g)\n",
1143 result[0], result[1], result[2], result[3]);
Brian13e3b212007-02-22 16:09:40 -07001144 }
Briane80d9012007-02-23 16:53:24 -07001145 }
1146 break;
1147 case OPCODE_MUL:
1148 {
1149 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001150 fetch_vector4(&inst->SrcReg[0], machine, a);
1151 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001152 result[0] = a[0] * b[0];
1153 result[1] = a[1] * b[1];
1154 result[2] = a[2] * b[2];
1155 result[3] = a[3] * b[3];
1156 store_vector4(inst, machine, result);
1157 if (DEBUG_PROG) {
1158 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1159 result[0], result[1], result[2], result[3],
1160 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001161 }
Briane80d9012007-02-23 16:53:24 -07001162 }
1163 break;
1164 case OPCODE_NOISE1:
1165 {
1166 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001167 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001168 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001169 result[1] =
Brian Paul702b5b02008-12-15 18:37:39 -07001170 result[2] =
1171 result[3] = _mesa_noise1(a[0]);
Briane80d9012007-02-23 16:53:24 -07001172 store_vector4(inst, machine, result);
1173 }
1174 break;
1175 case OPCODE_NOISE2:
1176 {
1177 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001178 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001179 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001180 result[1] =
Brian Paul702b5b02008-12-15 18:37:39 -07001181 result[2] = result[3] = _mesa_noise2(a[0], a[1]);
Briane80d9012007-02-23 16:53:24 -07001182 store_vector4(inst, machine, result);
1183 }
1184 break;
1185 case OPCODE_NOISE3:
1186 {
1187 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001188 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001189 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001190 result[1] =
1191 result[2] =
Brian Paul702b5b02008-12-15 18:37:39 -07001192 result[3] = _mesa_noise3(a[0], a[1], a[2]);
Briane80d9012007-02-23 16:53:24 -07001193 store_vector4(inst, machine, result);
1194 }
1195 break;
1196 case OPCODE_NOISE4:
1197 {
1198 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001199 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001200 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001201 result[1] =
1202 result[2] =
Brian Paul702b5b02008-12-15 18:37:39 -07001203 result[3] = _mesa_noise4(a[0], a[1], a[2], a[3]);
Briane80d9012007-02-23 16:53:24 -07001204 store_vector4(inst, machine, result);
1205 }
1206 break;
1207 case OPCODE_NOP:
1208 break;
Brian Paul37eef7b2008-11-07 09:33:55 -07001209 case OPCODE_NOT: /* bitwise NOT */
1210 {
1211 GLuint a[4], result[4];
1212 fetch_vector4ui(&inst->SrcReg[0], machine, a);
1213 result[0] = ~a[0];
1214 result[1] = ~a[1];
1215 result[2] = ~a[2];
1216 result[3] = ~a[3];
1217 store_vector4ui(inst, machine, result);
1218 }
1219 break;
Brian Paulf6ead502008-11-07 08:51:31 -07001220 case OPCODE_NRM3: /* 3-component normalization */
1221 {
1222 GLfloat a[4], result[4];
1223 GLfloat tmp;
1224 fetch_vector4(&inst->SrcReg[0], machine, a);
1225 tmp = a[0] * a[0] + a[1] * a[1] + a[2] * a[2];
1226 if (tmp != 0.0F)
Brian Paul22459e72008-11-07 12:59:36 -07001227 tmp = INV_SQRTF(tmp);
Brian Paulf6ead502008-11-07 08:51:31 -07001228 result[0] = tmp * a[0];
1229 result[1] = tmp * a[1];
1230 result[2] = tmp * a[2];
1231 result[3] = 0.0; /* undefined, but prevent valgrind warnings */
1232 store_vector4(inst, machine, result);
1233 }
1234 break;
1235 case OPCODE_NRM4: /* 4-component normalization */
1236 {
1237 GLfloat a[4], result[4];
1238 GLfloat tmp;
1239 fetch_vector4(&inst->SrcReg[0], machine, a);
1240 tmp = a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3];
1241 if (tmp != 0.0F)
Brian Paul22459e72008-11-07 12:59:36 -07001242 tmp = INV_SQRTF(tmp);
Brian Paulf6ead502008-11-07 08:51:31 -07001243 result[0] = tmp * a[0];
1244 result[1] = tmp * a[1];
1245 result[2] = tmp * a[2];
1246 result[3] = tmp * a[3];
1247 store_vector4(inst, machine, result);
1248 }
1249 break;
Brian Paul37eef7b2008-11-07 09:33:55 -07001250 case OPCODE_OR: /* bitwise OR */
1251 {
1252 GLuint a[4], b[4], result[4];
1253 fetch_vector4ui(&inst->SrcReg[0], machine, a);
1254 fetch_vector4ui(&inst->SrcReg[1], machine, b);
1255 result[0] = a[0] | b[0];
1256 result[1] = a[1] | b[1];
1257 result[2] = a[2] | b[2];
1258 result[3] = a[3] | b[3];
1259 store_vector4ui(inst, machine, result);
1260 }
1261 break;
Briane80d9012007-02-23 16:53:24 -07001262 case OPCODE_PK2H: /* pack two 16-bit floats in one 32-bit float */
1263 {
Brian Paul37eef7b2008-11-07 09:33:55 -07001264 GLfloat a[4];
1265 GLuint result[4];
Briane80d9012007-02-23 16:53:24 -07001266 GLhalfNV hx, hy;
Brian33eac562007-02-25 18:52:41 -07001267 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001268 hx = _mesa_float_to_half(a[0]);
1269 hy = _mesa_float_to_half(a[1]);
Brian Paul37eef7b2008-11-07 09:33:55 -07001270 result[0] =
1271 result[1] =
1272 result[2] =
1273 result[3] = hx | (hy << 16);
1274 store_vector4ui(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -07001275 }
1276 break;
1277 case OPCODE_PK2US: /* pack two GLushorts into one 32-bit float */
1278 {
Brian Paul37eef7b2008-11-07 09:33:55 -07001279 GLfloat a[4];
1280 GLuint result[4], usx, usy;
Brian33eac562007-02-25 18:52:41 -07001281 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001282 a[0] = CLAMP(a[0], 0.0F, 1.0F);
1283 a[1] = CLAMP(a[1], 0.0F, 1.0F);
1284 usx = IROUND(a[0] * 65535.0F);
1285 usy = IROUND(a[1] * 65535.0F);
Brian Paul37eef7b2008-11-07 09:33:55 -07001286 result[0] =
1287 result[1] =
1288 result[2] =
1289 result[3] = usx | (usy << 16);
1290 store_vector4ui(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -07001291 }
1292 break;
1293 case OPCODE_PK4B: /* pack four GLbytes into one 32-bit float */
1294 {
Brian Paul37eef7b2008-11-07 09:33:55 -07001295 GLfloat a[4];
1296 GLuint result[4], ubx, uby, ubz, ubw;
Brian33eac562007-02-25 18:52:41 -07001297 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001298 a[0] = CLAMP(a[0], -128.0F / 127.0F, 1.0F);
1299 a[1] = CLAMP(a[1], -128.0F / 127.0F, 1.0F);
1300 a[2] = CLAMP(a[2], -128.0F / 127.0F, 1.0F);
1301 a[3] = CLAMP(a[3], -128.0F / 127.0F, 1.0F);
1302 ubx = IROUND(127.0F * a[0] + 128.0F);
1303 uby = IROUND(127.0F * a[1] + 128.0F);
1304 ubz = IROUND(127.0F * a[2] + 128.0F);
1305 ubw = IROUND(127.0F * a[3] + 128.0F);
Brian Paul37eef7b2008-11-07 09:33:55 -07001306 result[0] =
1307 result[1] =
1308 result[2] =
1309 result[3] = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
1310 store_vector4ui(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -07001311 }
1312 break;
1313 case OPCODE_PK4UB: /* pack four GLubytes into one 32-bit float */
1314 {
Brian Paul37eef7b2008-11-07 09:33:55 -07001315 GLfloat a[4];
1316 GLuint result[4], ubx, uby, ubz, ubw;
Brian33eac562007-02-25 18:52:41 -07001317 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001318 a[0] = CLAMP(a[0], 0.0F, 1.0F);
1319 a[1] = CLAMP(a[1], 0.0F, 1.0F);
1320 a[2] = CLAMP(a[2], 0.0F, 1.0F);
1321 a[3] = CLAMP(a[3], 0.0F, 1.0F);
1322 ubx = IROUND(255.0F * a[0]);
1323 uby = IROUND(255.0F * a[1]);
1324 ubz = IROUND(255.0F * a[2]);
1325 ubw = IROUND(255.0F * a[3]);
Brian Paul37eef7b2008-11-07 09:33:55 -07001326 result[0] =
1327 result[1] =
1328 result[2] =
1329 result[3] = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
1330 store_vector4ui(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -07001331 }
1332 break;
1333 case OPCODE_POW:
1334 {
1335 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001336 fetch_vector1(&inst->SrcReg[0], machine, a);
1337 fetch_vector1(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001338 result[0] = result[1] = result[2] = result[3]
Eric Anholtfef303b2010-05-05 08:29:13 -07001339 = (GLfloat) pow(a[0], b[0]);
Briane80d9012007-02-23 16:53:24 -07001340 store_vector4(inst, machine, result);
1341 }
1342 break;
1343 case OPCODE_RCP:
1344 {
1345 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001346 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001347 if (DEBUG_PROG) {
1348 if (a[0] == 0)
1349 printf("RCP(0)\n");
1350 else if (IS_INF_OR_NAN(a[0]))
1351 printf("RCP(inf)\n");
Brian13e3b212007-02-22 16:09:40 -07001352 }
Briane80d9012007-02-23 16:53:24 -07001353 result[0] = result[1] = result[2] = result[3] = 1.0F / a[0];
1354 store_vector4(inst, machine, result);
1355 }
1356 break;
1357 case OPCODE_RET: /* return from subroutine (conditional) */
1358 if (eval_condition(machine, inst)) {
1359 if (machine->StackDepth == 0) {
1360 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
Brian13e3b212007-02-22 16:09:40 -07001361 }
Briana0275b02007-03-27 11:02:20 -06001362 /* subtract one because of pc++ in the for loop */
1363 pc = machine->CallStack[--machine->StackDepth] - 1;
Briane80d9012007-02-23 16:53:24 -07001364 }
1365 break;
1366 case OPCODE_RFL: /* reflection vector */
1367 {
1368 GLfloat axis[4], dir[4], result[4], tmpX, tmpW;
Brian33eac562007-02-25 18:52:41 -07001369 fetch_vector4(&inst->SrcReg[0], machine, axis);
1370 fetch_vector4(&inst->SrcReg[1], machine, dir);
Briane80d9012007-02-23 16:53:24 -07001371 tmpW = DOT3(axis, axis);
1372 tmpX = (2.0F * DOT3(axis, dir)) / tmpW;
1373 result[0] = tmpX * axis[0] - dir[0];
1374 result[1] = tmpX * axis[1] - dir[1];
1375 result[2] = tmpX * axis[2] - dir[2];
1376 /* result[3] is never written! XXX enforce in parser! */
1377 store_vector4(inst, machine, result);
1378 }
1379 break;
1380 case OPCODE_RSQ: /* 1 / sqrt() */
1381 {
1382 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001383 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001384 a[0] = FABSF(a[0]);
1385 result[0] = result[1] = result[2] = result[3] = INV_SQRTF(a[0]);
1386 store_vector4(inst, machine, result);
1387 if (DEBUG_PROG) {
1388 printf("RSQ %g = 1/sqrt(|%g|)\n", result[0], a[0]);
Brian13e3b212007-02-22 16:09:40 -07001389 }
Briane80d9012007-02-23 16:53:24 -07001390 }
1391 break;
1392 case OPCODE_SCS: /* sine and cos */
1393 {
1394 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001395 fetch_vector1(&inst->SrcReg[0], machine, a);
Eric Anholt165694a2010-05-05 08:36:20 -07001396 result[0] = (GLfloat) cos(a[0]);
1397 result[1] = (GLfloat) sin(a[0]);
Briane80d9012007-02-23 16:53:24 -07001398 result[2] = 0.0; /* undefined! */
1399 result[3] = 0.0; /* undefined! */
1400 store_vector4(inst, machine, result);
1401 }
1402 break;
1403 case OPCODE_SEQ: /* set on equal */
1404 {
1405 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001406 fetch_vector4(&inst->SrcReg[0], machine, a);
1407 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001408 result[0] = (a[0] == b[0]) ? 1.0F : 0.0F;
1409 result[1] = (a[1] == b[1]) ? 1.0F : 0.0F;
1410 result[2] = (a[2] == b[2]) ? 1.0F : 0.0F;
1411 result[3] = (a[3] == b[3]) ? 1.0F : 0.0F;
1412 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001413 if (DEBUG_PROG) {
1414 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1415 result[0], result[1], result[2], result[3],
1416 a[0], a[1], a[2], a[3],
1417 b[0], b[1], b[2], b[3]);
1418 }
Briane80d9012007-02-23 16:53:24 -07001419 }
1420 break;
1421 case OPCODE_SFL: /* set false, operands ignored */
1422 {
1423 static const GLfloat result[4] = { 0.0F, 0.0F, 0.0F, 0.0F };
1424 store_vector4(inst, machine, result);
1425 }
1426 break;
1427 case OPCODE_SGE: /* set on greater or equal */
1428 {
1429 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001430 fetch_vector4(&inst->SrcReg[0], machine, a);
1431 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001432 result[0] = (a[0] >= b[0]) ? 1.0F : 0.0F;
1433 result[1] = (a[1] >= b[1]) ? 1.0F : 0.0F;
1434 result[2] = (a[2] >= b[2]) ? 1.0F : 0.0F;
1435 result[3] = (a[3] >= b[3]) ? 1.0F : 0.0F;
1436 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001437 if (DEBUG_PROG) {
1438 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1439 result[0], result[1], result[2], result[3],
1440 a[0], a[1], a[2], a[3],
1441 b[0], b[1], b[2], b[3]);
1442 }
Briane80d9012007-02-23 16:53:24 -07001443 }
1444 break;
1445 case OPCODE_SGT: /* set on greater */
1446 {
1447 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001448 fetch_vector4(&inst->SrcReg[0], machine, a);
1449 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001450 result[0] = (a[0] > b[0]) ? 1.0F : 0.0F;
1451 result[1] = (a[1] > b[1]) ? 1.0F : 0.0F;
1452 result[2] = (a[2] > b[2]) ? 1.0F : 0.0F;
1453 result[3] = (a[3] > b[3]) ? 1.0F : 0.0F;
1454 store_vector4(inst, machine, result);
1455 if (DEBUG_PROG) {
Brian28ab1122007-03-06 12:15:30 -07001456 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1457 result[0], result[1], result[2], result[3],
1458 a[0], a[1], a[2], a[3],
1459 b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001460 }
Briane80d9012007-02-23 16:53:24 -07001461 }
1462 break;
1463 case OPCODE_SIN:
1464 {
1465 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001466 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001467 result[0] = result[1] = result[2] = result[3]
Eric Anholt165694a2010-05-05 08:36:20 -07001468 = (GLfloat) sin(a[0]);
Briane80d9012007-02-23 16:53:24 -07001469 store_vector4(inst, machine, result);
1470 }
1471 break;
1472 case OPCODE_SLE: /* set on less or equal */
1473 {
1474 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001475 fetch_vector4(&inst->SrcReg[0], machine, a);
1476 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001477 result[0] = (a[0] <= b[0]) ? 1.0F : 0.0F;
1478 result[1] = (a[1] <= b[1]) ? 1.0F : 0.0F;
1479 result[2] = (a[2] <= b[2]) ? 1.0F : 0.0F;
1480 result[3] = (a[3] <= b[3]) ? 1.0F : 0.0F;
1481 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001482 if (DEBUG_PROG) {
1483 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1484 result[0], result[1], result[2], result[3],
1485 a[0], a[1], a[2], a[3],
1486 b[0], b[1], b[2], b[3]);
1487 }
Briane80d9012007-02-23 16:53:24 -07001488 }
1489 break;
1490 case OPCODE_SLT: /* set on less */
1491 {
1492 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001493 fetch_vector4(&inst->SrcReg[0], machine, a);
1494 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001495 result[0] = (a[0] < b[0]) ? 1.0F : 0.0F;
1496 result[1] = (a[1] < b[1]) ? 1.0F : 0.0F;
1497 result[2] = (a[2] < b[2]) ? 1.0F : 0.0F;
1498 result[3] = (a[3] < b[3]) ? 1.0F : 0.0F;
1499 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001500 if (DEBUG_PROG) {
1501 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1502 result[0], result[1], result[2], result[3],
1503 a[0], a[1], a[2], a[3],
1504 b[0], b[1], b[2], b[3]);
1505 }
Briane80d9012007-02-23 16:53:24 -07001506 }
1507 break;
1508 case OPCODE_SNE: /* set on not equal */
1509 {
1510 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001511 fetch_vector4(&inst->SrcReg[0], machine, a);
1512 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001513 result[0] = (a[0] != b[0]) ? 1.0F : 0.0F;
1514 result[1] = (a[1] != b[1]) ? 1.0F : 0.0F;
1515 result[2] = (a[2] != b[2]) ? 1.0F : 0.0F;
1516 result[3] = (a[3] != b[3]) ? 1.0F : 0.0F;
1517 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001518 if (DEBUG_PROG) {
1519 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1520 result[0], result[1], result[2], result[3],
1521 a[0], a[1], a[2], a[3],
1522 b[0], b[1], b[2], b[3]);
1523 }
Briane80d9012007-02-23 16:53:24 -07001524 }
1525 break;
Brian Paulf6ead502008-11-07 08:51:31 -07001526 case OPCODE_SSG: /* set sign (-1, 0 or +1) */
1527 {
1528 GLfloat a[4], result[4];
1529 fetch_vector4(&inst->SrcReg[0], machine, a);
1530 result[0] = (GLfloat) ((a[0] > 0.0F) - (a[0] < 0.0F));
1531 result[1] = (GLfloat) ((a[1] > 0.0F) - (a[1] < 0.0F));
1532 result[2] = (GLfloat) ((a[2] > 0.0F) - (a[2] < 0.0F));
1533 result[3] = (GLfloat) ((a[3] > 0.0F) - (a[3] < 0.0F));
1534 store_vector4(inst, machine, result);
1535 }
1536 break;
Briane80d9012007-02-23 16:53:24 -07001537 case OPCODE_STR: /* set true, operands ignored */
1538 {
1539 static const GLfloat result[4] = { 1.0F, 1.0F, 1.0F, 1.0F };
1540 store_vector4(inst, machine, result);
1541 }
1542 break;
1543 case OPCODE_SUB:
1544 {
1545 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001546 fetch_vector4(&inst->SrcReg[0], machine, a);
1547 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001548 result[0] = a[0] - b[0];
1549 result[1] = a[1] - b[1];
1550 result[2] = a[2] - b[2];
1551 result[3] = a[3] - b[3];
1552 store_vector4(inst, machine, result);
1553 if (DEBUG_PROG) {
1554 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1555 result[0], result[1], result[2], result[3],
1556 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001557 }
Briane80d9012007-02-23 16:53:24 -07001558 }
1559 break;
1560 case OPCODE_SWZ: /* extended swizzle */
1561 {
1562 const struct prog_src_register *source = &inst->SrcReg[0];
Brian Paulf4361542008-11-11 10:47:10 -07001563 const GLfloat *src = get_src_register_pointer(source, machine);
Briane80d9012007-02-23 16:53:24 -07001564 GLfloat result[4];
1565 GLuint i;
1566 for (i = 0; i < 4; i++) {
1567 const GLuint swz = GET_SWZ(source->Swizzle, i);
1568 if (swz == SWIZZLE_ZERO)
1569 result[i] = 0.0;
1570 else if (swz == SWIZZLE_ONE)
1571 result[i] = 1.0;
Brian13e3b212007-02-22 16:09:40 -07001572 else {
Briane80d9012007-02-23 16:53:24 -07001573 ASSERT(swz >= 0);
1574 ASSERT(swz <= 3);
1575 result[i] = src[swz];
Brian13e3b212007-02-22 16:09:40 -07001576 }
Brian Paul7db7ff82009-04-14 22:14:30 -06001577 if (source->Negate & (1 << i))
Briane80d9012007-02-23 16:53:24 -07001578 result[i] = -result[i];
Brian13e3b212007-02-22 16:09:40 -07001579 }
Briane80d9012007-02-23 16:53:24 -07001580 store_vector4(inst, machine, result);
1581 }
1582 break;
1583 case OPCODE_TEX: /* Both ARB and NV frag prog */
Brian999b5562007-11-23 12:01:57 -07001584 /* Simple texel lookup */
Briane80d9012007-02-23 16:53:24 -07001585 {
Brian999b5562007-11-23 12:01:57 -07001586 GLfloat texcoord[4], color[4];
1587 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1588
1589 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1590
Briane80d9012007-02-23 16:53:24 -07001591 if (DEBUG_PROG) {
Brian999b5562007-11-23 12:01:57 -07001592 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
Briane80d9012007-02-23 16:53:24 -07001593 color[0], color[1], color[2], color[3],
1594 inst->TexSrcUnit,
Brian999b5562007-11-23 12:01:57 -07001595 texcoord[0], texcoord[1], texcoord[2], texcoord[3]);
Briane80d9012007-02-23 16:53:24 -07001596 }
1597 store_vector4(inst, machine, color);
1598 }
1599 break;
1600 case OPCODE_TXB: /* GL_ARB_fragment_program only */
1601 /* Texel lookup with LOD bias */
1602 {
Brian999b5562007-11-23 12:01:57 -07001603 GLfloat texcoord[4], color[4], lodBias;
1604
1605 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1606
1607 /* texcoord[3] is the bias to add to lambda */
Brian Paul890f37d2009-09-23 13:34:30 -06001608 lodBias = texcoord[3];
Brian999b5562007-11-23 12:01:57 -07001609
1610 fetch_texel(ctx, machine, inst, texcoord, lodBias, color);
1611
Briane80d9012007-02-23 16:53:24 -07001612 store_vector4(inst, machine, color);
1613 }
1614 break;
1615 case OPCODE_TXD: /* GL_NV_fragment_program only */
1616 /* Texture lookup w/ partial derivatives for LOD */
1617 {
1618 GLfloat texcoord[4], dtdx[4], dtdy[4], color[4];
Brian33eac562007-02-25 18:52:41 -07001619 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1620 fetch_vector4(&inst->SrcReg[1], machine, dtdx);
1621 fetch_vector4(&inst->SrcReg[2], machine, dtdy);
Briane80d9012007-02-23 16:53:24 -07001622 machine->FetchTexelDeriv(ctx, texcoord, dtdx, dtdy,
Brian999b5562007-11-23 12:01:57 -07001623 0.0, /* lodBias */
Briane80d9012007-02-23 16:53:24 -07001624 inst->TexSrcUnit, color);
1625 store_vector4(inst, machine, color);
1626 }
1627 break;
1628 case OPCODE_TXP: /* GL_ARB_fragment_program only */
1629 /* Texture lookup w/ projective divide */
1630 {
Brian999b5562007-11-23 12:01:57 -07001631 GLfloat texcoord[4], color[4];
1632
Brian33eac562007-02-25 18:52:41 -07001633 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
Briane80d9012007-02-23 16:53:24 -07001634 /* Not so sure about this test - if texcoord[3] is
1635 * zero, we'd probably be fine except for an ASSERT in
1636 * IROUND_POS() which gets triggered by the inf values created.
1637 */
1638 if (texcoord[3] != 0.0) {
1639 texcoord[0] /= texcoord[3];
1640 texcoord[1] /= texcoord[3];
1641 texcoord[2] /= texcoord[3];
1642 }
Brian999b5562007-11-23 12:01:57 -07001643
1644 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1645
Briane80d9012007-02-23 16:53:24 -07001646 store_vector4(inst, machine, color);
1647 }
1648 break;
1649 case OPCODE_TXP_NV: /* GL_NV_fragment_program only */
Brian999b5562007-11-23 12:01:57 -07001650 /* Texture lookup w/ projective divide, as above, but do not
1651 * do the divide by w if sampling from a cube map.
1652 */
Briane80d9012007-02-23 16:53:24 -07001653 {
Brian999b5562007-11-23 12:01:57 -07001654 GLfloat texcoord[4], color[4];
1655
Brian33eac562007-02-25 18:52:41 -07001656 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
Briane80d9012007-02-23 16:53:24 -07001657 if (inst->TexSrcTarget != TEXTURE_CUBE_INDEX &&
1658 texcoord[3] != 0.0) {
1659 texcoord[0] /= texcoord[3];
1660 texcoord[1] /= texcoord[3];
1661 texcoord[2] /= texcoord[3];
1662 }
Brian999b5562007-11-23 12:01:57 -07001663
1664 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1665
Briane80d9012007-02-23 16:53:24 -07001666 store_vector4(inst, machine, color);
1667 }
1668 break;
Brian Paul035c0cf2008-11-06 17:14:33 -07001669 case OPCODE_TRUNC: /* truncate toward zero */
1670 {
1671 GLfloat a[4], result[4];
1672 fetch_vector4(&inst->SrcReg[0], machine, a);
1673 result[0] = (GLfloat) (GLint) a[0];
1674 result[1] = (GLfloat) (GLint) a[1];
1675 result[2] = (GLfloat) (GLint) a[2];
1676 result[3] = (GLfloat) (GLint) a[3];
1677 store_vector4(inst, machine, result);
1678 }
1679 break;
Briane80d9012007-02-23 16:53:24 -07001680 case OPCODE_UP2H: /* unpack two 16-bit floats */
1681 {
Brian Paul8d1a01d2010-01-22 15:36:28 -07001682 const GLuint raw = fetch_vector1ui(&inst->SrcReg[0], machine);
1683 GLfloat result[4];
Brian Paul7b5ad232010-01-22 16:09:03 -07001684 GLushort hx, hy;
Brian Paul8d1a01d2010-01-22 15:36:28 -07001685 hx = raw & 0xffff;
1686 hy = raw >> 16;
Briane80d9012007-02-23 16:53:24 -07001687 result[0] = result[2] = _mesa_half_to_float(hx);
1688 result[1] = result[3] = _mesa_half_to_float(hy);
1689 store_vector4(inst, machine, result);
1690 }
1691 break;
1692 case OPCODE_UP2US: /* unpack two GLushorts */
1693 {
Brian Paul8d1a01d2010-01-22 15:36:28 -07001694 const GLuint raw = fetch_vector1ui(&inst->SrcReg[0], machine);
1695 GLfloat result[4];
Briane80d9012007-02-23 16:53:24 -07001696 GLushort usx, usy;
Brian Paul8d1a01d2010-01-22 15:36:28 -07001697 usx = raw & 0xffff;
1698 usy = raw >> 16;
Briane80d9012007-02-23 16:53:24 -07001699 result[0] = result[2] = usx * (1.0f / 65535.0f);
1700 result[1] = result[3] = usy * (1.0f / 65535.0f);
1701 store_vector4(inst, machine, result);
1702 }
1703 break;
1704 case OPCODE_UP4B: /* unpack four GLbytes */
1705 {
Brian Paul8d1a01d2010-01-22 15:36:28 -07001706 const GLuint raw = fetch_vector1ui(&inst->SrcReg[0], machine);
1707 GLfloat result[4];
1708 result[0] = (((raw >> 0) & 0xff) - 128) / 127.0F;
1709 result[1] = (((raw >> 8) & 0xff) - 128) / 127.0F;
1710 result[2] = (((raw >> 16) & 0xff) - 128) / 127.0F;
1711 result[3] = (((raw >> 24) & 0xff) - 128) / 127.0F;
Briane80d9012007-02-23 16:53:24 -07001712 store_vector4(inst, machine, result);
1713 }
1714 break;
1715 case OPCODE_UP4UB: /* unpack four GLubytes */
1716 {
Brian Paul8d1a01d2010-01-22 15:36:28 -07001717 const GLuint raw = fetch_vector1ui(&inst->SrcReg[0], machine);
1718 GLfloat result[4];
1719 result[0] = ((raw >> 0) & 0xff) / 255.0F;
1720 result[1] = ((raw >> 8) & 0xff) / 255.0F;
1721 result[2] = ((raw >> 16) & 0xff) / 255.0F;
1722 result[3] = ((raw >> 24) & 0xff) / 255.0F;
Briane80d9012007-02-23 16:53:24 -07001723 store_vector4(inst, machine, result);
1724 }
1725 break;
Brian Paul37eef7b2008-11-07 09:33:55 -07001726 case OPCODE_XOR: /* bitwise XOR */
1727 {
1728 GLuint a[4], b[4], result[4];
1729 fetch_vector4ui(&inst->SrcReg[0], machine, a);
1730 fetch_vector4ui(&inst->SrcReg[1], machine, b);
1731 result[0] = a[0] ^ b[0];
1732 result[1] = a[1] ^ b[1];
1733 result[2] = a[2] ^ b[2];
1734 result[3] = a[3] ^ b[3];
1735 store_vector4ui(inst, machine, result);
1736 }
1737 break;
Briane80d9012007-02-23 16:53:24 -07001738 case OPCODE_XPD: /* cross product */
1739 {
1740 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001741 fetch_vector4(&inst->SrcReg[0], machine, a);
1742 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001743 result[0] = a[1] * b[2] - a[2] * b[1];
1744 result[1] = a[2] * b[0] - a[0] * b[2];
1745 result[2] = a[0] * b[1] - a[1] * b[0];
1746 result[3] = 1.0;
1747 store_vector4(inst, machine, result);
Brian9637c962007-03-07 17:40:57 -07001748 if (DEBUG_PROG) {
1749 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1750 result[0], result[1], result[2], result[3],
1751 a[0], a[1], a[2], b[0], b[1], b[2]);
1752 }
Briane80d9012007-02-23 16:53:24 -07001753 }
1754 break;
1755 case OPCODE_X2D: /* 2-D matrix transform */
1756 {
1757 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001758 fetch_vector4(&inst->SrcReg[0], machine, a);
1759 fetch_vector4(&inst->SrcReg[1], machine, b);
1760 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -07001761 result[0] = a[0] + b[0] * c[0] + b[1] * c[1];
1762 result[1] = a[1] + b[0] * c[2] + b[1] * c[3];
1763 result[2] = a[2] + b[0] * c[0] + b[1] * c[1];
1764 result[3] = a[3] + b[0] * c[2] + b[1] * c[3];
1765 store_vector4(inst, machine, result);
1766 }
1767 break;
1768 case OPCODE_PRINT:
1769 {
Vinson Lee4e981162010-04-18 00:11:49 -07001770 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
Vinson Lee45df4ba2010-03-04 17:40:36 -08001771 GLfloat a[4];
1772 fetch_vector4(&inst->SrcReg[0], machine, a);
1773 printf("%s%g, %g, %g, %g\n", (const char *) inst->Data,
1774 a[0], a[1], a[2], a[3]);
1775 }
1776 else {
1777 printf("%s\n", (const char *) inst->Data);
1778 }
Briane80d9012007-02-23 16:53:24 -07001779 }
1780 break;
1781 case OPCODE_END:
1782 return GL_TRUE;
1783 default:
Alan Hourihane936dba12008-04-22 20:28:35 +01001784 _mesa_problem(ctx, "Bad opcode %d in _mesa_execute_program",
Briane80d9012007-02-23 16:53:24 -07001785 inst->Opcode);
1786 return GL_TRUE; /* return value doesn't matter */
Brian13e3b212007-02-22 16:09:40 -07001787 }
Briane80d9012007-02-23 16:53:24 -07001788
Briancfd00112007-02-25 18:30:45 -07001789 numExec++;
1790 if (numExec > maxExec) {
Brian13e3b212007-02-22 16:09:40 -07001791 _mesa_problem(ctx, "Infinite loop detected in fragment program");
1792 return GL_TRUE;
Brian13e3b212007-02-22 16:09:40 -07001793 }
Briane80d9012007-02-23 16:53:24 -07001794
1795 } /* for pc */
Brian13e3b212007-02-22 16:09:40 -07001796
Brian13e3b212007-02-22 16:09:40 -07001797 return GL_TRUE;
1798}