blob: f4beb9a78b62d6b249621b9fabb049cde27094f9 [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
Brian13e3b212007-02-22 16:09:40 -0700189/**
190 * Fetch a 4-element float vector from the given source register.
191 * Apply swizzling and negating as needed.
192 */
193static void
Brian33eac562007-02-25 18:52:41 -0700194fetch_vector4(const struct prog_src_register *source,
Briane80d9012007-02-23 16:53:24 -0700195 const struct gl_program_machine *machine, GLfloat result[4])
Brian13e3b212007-02-22 16:09:40 -0700196{
Brian Paulf4361542008-11-11 10:47:10 -0700197 const GLfloat *src = get_src_register_pointer(source, machine);
Brian13e3b212007-02-22 16:09:40 -0700198 ASSERT(src);
199
200 if (source->Swizzle == SWIZZLE_NOOP) {
201 /* no swizzling */
202 COPY_4V(result, src);
203 }
204 else {
205 ASSERT(GET_SWZ(source->Swizzle, 0) <= 3);
206 ASSERT(GET_SWZ(source->Swizzle, 1) <= 3);
207 ASSERT(GET_SWZ(source->Swizzle, 2) <= 3);
208 ASSERT(GET_SWZ(source->Swizzle, 3) <= 3);
209 result[0] = src[GET_SWZ(source->Swizzle, 0)];
210 result[1] = src[GET_SWZ(source->Swizzle, 1)];
211 result[2] = src[GET_SWZ(source->Swizzle, 2)];
212 result[3] = src[GET_SWZ(source->Swizzle, 3)];
213 }
214
Brian13e3b212007-02-22 16:09:40 -0700215 if (source->Abs) {
216 result[0] = FABSF(result[0]);
217 result[1] = FABSF(result[1]);
218 result[2] = FABSF(result[2]);
219 result[3] = FABSF(result[3]);
220 }
Brian Paul7db7ff82009-04-14 22:14:30 -0600221 if (source->Negate) {
222 ASSERT(source->Negate == NEGATE_XYZW);
Brian13e3b212007-02-22 16:09:40 -0700223 result[0] = -result[0];
224 result[1] = -result[1];
225 result[2] = -result[2];
226 result[3] = -result[3];
227 }
Brian Paul92009542009-06-03 15:43:53 -0600228
229#ifdef NAN_CHECK
230 assert(!IS_INF_OR_NAN(result[0]));
231 assert(!IS_INF_OR_NAN(result[0]));
232 assert(!IS_INF_OR_NAN(result[0]));
233 assert(!IS_INF_OR_NAN(result[0]));
234#endif
Brian13e3b212007-02-22 16:09:40 -0700235}
236
Brian62da6a12007-05-02 18:44:34 -0600237
Brian13e3b212007-02-22 16:09:40 -0700238/**
Brian Paul37eef7b2008-11-07 09:33:55 -0700239 * Fetch a 4-element uint vector from the given source register.
240 * Apply swizzling but not negation/abs.
241 */
242static void
243fetch_vector4ui(const struct prog_src_register *source,
244 const struct gl_program_machine *machine, GLuint result[4])
245{
Brian Paulf4361542008-11-11 10:47:10 -0700246 const GLuint *src = (GLuint *) get_src_register_pointer(source, machine);
Brian Paul37eef7b2008-11-07 09:33:55 -0700247 ASSERT(src);
248
249 if (source->Swizzle == SWIZZLE_NOOP) {
250 /* no swizzling */
251 COPY_4V(result, src);
252 }
253 else {
254 ASSERT(GET_SWZ(source->Swizzle, 0) <= 3);
255 ASSERT(GET_SWZ(source->Swizzle, 1) <= 3);
256 ASSERT(GET_SWZ(source->Swizzle, 2) <= 3);
257 ASSERT(GET_SWZ(source->Swizzle, 3) <= 3);
258 result[0] = src[GET_SWZ(source->Swizzle, 0)];
259 result[1] = src[GET_SWZ(source->Swizzle, 1)];
260 result[2] = src[GET_SWZ(source->Swizzle, 2)];
261 result[3] = src[GET_SWZ(source->Swizzle, 3)];
262 }
263
Brian Paul7db7ff82009-04-14 22:14:30 -0600264 /* Note: no Negate or Abs here */
Brian Paul37eef7b2008-11-07 09:33:55 -0700265}
266
267
268
269/**
Brian62da6a12007-05-02 18:44:34 -0600270 * Fetch the derivative with respect to X or Y for the given register.
271 * XXX this currently only works for fragment program input attribs.
Brian13e3b212007-02-22 16:09:40 -0700272 */
Brian62da6a12007-05-02 18:44:34 -0600273static void
Briane80d9012007-02-23 16:53:24 -0700274fetch_vector4_deriv(GLcontext * ctx,
275 const struct prog_src_register *source,
Brian62da6a12007-05-02 18:44:34 -0600276 const struct gl_program_machine *machine,
277 char xOrY, GLfloat result[4])
Brian13e3b212007-02-22 16:09:40 -0700278{
Brian Paul37eef7b2008-11-07 09:33:55 -0700279 if (source->File == PROGRAM_INPUT &&
280 source->Index < (GLint) machine->NumDeriv) {
Brian62da6a12007-05-02 18:44:34 -0600281 const GLint col = machine->CurElement;
282 const GLfloat w = machine->Attribs[FRAG_ATTRIB_WPOS][col][3];
283 const GLfloat invQ = 1.0f / w;
284 GLfloat deriv[4];
Brian13e3b212007-02-22 16:09:40 -0700285
Brian13e3b212007-02-22 16:09:40 -0700286 if (xOrY == 'X') {
Brian62da6a12007-05-02 18:44:34 -0600287 deriv[0] = machine->DerivX[source->Index][0] * invQ;
288 deriv[1] = machine->DerivX[source->Index][1] * invQ;
289 deriv[2] = machine->DerivX[source->Index][2] * invQ;
290 deriv[3] = machine->DerivX[source->Index][3] * invQ;
Brian13e3b212007-02-22 16:09:40 -0700291 }
292 else {
Brian62da6a12007-05-02 18:44:34 -0600293 deriv[0] = machine->DerivY[source->Index][0] * invQ;
294 deriv[1] = machine->DerivY[source->Index][1] * invQ;
295 deriv[2] = machine->DerivY[source->Index][2] * invQ;
296 deriv[3] = machine->DerivY[source->Index][3] * invQ;
Brian13e3b212007-02-22 16:09:40 -0700297 }
Brian13e3b212007-02-22 16:09:40 -0700298
Brian62da6a12007-05-02 18:44:34 -0600299 result[0] = deriv[GET_SWZ(source->Swizzle, 0)];
300 result[1] = deriv[GET_SWZ(source->Swizzle, 1)];
301 result[2] = deriv[GET_SWZ(source->Swizzle, 2)];
302 result[3] = deriv[GET_SWZ(source->Swizzle, 3)];
303
Brian62da6a12007-05-02 18:44:34 -0600304 if (source->Abs) {
305 result[0] = FABSF(result[0]);
306 result[1] = FABSF(result[1]);
307 result[2] = FABSF(result[2]);
308 result[3] = FABSF(result[3]);
309 }
Brian Paul7db7ff82009-04-14 22:14:30 -0600310 if (source->Negate) {
311 ASSERT(source->Negate == NEGATE_XYZW);
Brian62da6a12007-05-02 18:44:34 -0600312 result[0] = -result[0];
313 result[1] = -result[1];
314 result[2] = -result[2];
315 result[3] = -result[3];
316 }
Brian13e3b212007-02-22 16:09:40 -0700317 }
Brian62da6a12007-05-02 18:44:34 -0600318 else {
319 ASSIGN_4V(result, 0.0, 0.0, 0.0, 0.0);
Brian13e3b212007-02-22 16:09:40 -0700320 }
Brian13e3b212007-02-22 16:09:40 -0700321}
Brian13e3b212007-02-22 16:09:40 -0700322
323
324/**
325 * As above, but only return result[0] element.
326 */
327static void
Brian33eac562007-02-25 18:52:41 -0700328fetch_vector1(const struct prog_src_register *source,
Briane80d9012007-02-23 16:53:24 -0700329 const struct gl_program_machine *machine, GLfloat result[4])
Brian13e3b212007-02-22 16:09:40 -0700330{
Brian Paulf4361542008-11-11 10:47:10 -0700331 const GLfloat *src = get_src_register_pointer(source, machine);
Brian13e3b212007-02-22 16:09:40 -0700332 ASSERT(src);
333
334 result[0] = src[GET_SWZ(source->Swizzle, 0)];
335
Brian13e3b212007-02-22 16:09:40 -0700336 if (source->Abs) {
337 result[0] = FABSF(result[0]);
338 }
Brian Paul7db7ff82009-04-14 22:14:30 -0600339 if (source->Negate) {
Brian13e3b212007-02-22 16:09:40 -0700340 result[0] = -result[0];
341 }
342}
343
344
345/**
Brian999b5562007-11-23 12:01:57 -0700346 * Fetch texel from texture. Use partial derivatives when possible.
347 */
348static INLINE void
349fetch_texel(GLcontext *ctx,
350 const struct gl_program_machine *machine,
351 const struct prog_instruction *inst,
352 const GLfloat texcoord[4], GLfloat lodBias,
353 GLfloat color[4])
354{
Brian Paulade50832008-05-14 16:09:46 -0600355 const GLuint unit = machine->Samplers[inst->TexSrcUnit];
356
Brian999b5562007-11-23 12:01:57 -0700357 /* Note: we only have the right derivatives for fragment input attribs.
358 */
359 if (machine->NumDeriv > 0 &&
360 inst->SrcReg[0].File == PROGRAM_INPUT &&
361 inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0 + inst->TexSrcUnit) {
362 /* simple texture fetch for which we should have derivatives */
363 GLuint attr = inst->SrcReg[0].Index;
364 machine->FetchTexelDeriv(ctx, texcoord,
365 machine->DerivX[attr],
366 machine->DerivY[attr],
Brian Paulade50832008-05-14 16:09:46 -0600367 lodBias, unit, color);
Brian999b5562007-11-23 12:01:57 -0700368 }
369 else {
Brian Paulade50832008-05-14 16:09:46 -0600370 machine->FetchTexelLod(ctx, texcoord, lodBias, unit, color);
Brian999b5562007-11-23 12:01:57 -0700371 }
372}
373
374
375/**
Brian13e3b212007-02-22 16:09:40 -0700376 * Test value against zero and return GT, LT, EQ or UN if NaN.
377 */
378static INLINE GLuint
Briane80d9012007-02-23 16:53:24 -0700379generate_cc(float value)
Brian13e3b212007-02-22 16:09:40 -0700380{
381 if (value != value)
Briane80d9012007-02-23 16:53:24 -0700382 return COND_UN; /* NaN */
Brian13e3b212007-02-22 16:09:40 -0700383 if (value > 0.0F)
384 return COND_GT;
385 if (value < 0.0F)
386 return COND_LT;
387 return COND_EQ;
388}
389
390
391/**
392 * Test if the ccMaskRule is satisfied by the given condition code.
393 * Used to mask destination writes according to the current condition code.
394 */
395static INLINE GLboolean
396test_cc(GLuint condCode, GLuint ccMaskRule)
397{
398 switch (ccMaskRule) {
399 case COND_EQ: return (condCode == COND_EQ);
400 case COND_NE: return (condCode != COND_EQ);
401 case COND_LT: return (condCode == COND_LT);
402 case COND_GE: return (condCode == COND_GT || condCode == COND_EQ);
403 case COND_LE: return (condCode == COND_LT || condCode == COND_EQ);
404 case COND_GT: return (condCode == COND_GT);
405 case COND_TR: return GL_TRUE;
406 case COND_FL: return GL_FALSE;
407 default: return GL_TRUE;
408 }
409}
410
411
412/**
413 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
414 * or GL_FALSE to indicate result.
415 */
416static INLINE GLboolean
417eval_condition(const struct gl_program_machine *machine,
418 const struct prog_instruction *inst)
419{
420 const GLuint swizzle = inst->DstReg.CondSwizzle;
421 const GLuint condMask = inst->DstReg.CondMask;
422 if (test_cc(machine->CondCodes[GET_SWZ(swizzle, 0)], condMask) ||
423 test_cc(machine->CondCodes[GET_SWZ(swizzle, 1)], condMask) ||
424 test_cc(machine->CondCodes[GET_SWZ(swizzle, 2)], condMask) ||
425 test_cc(machine->CondCodes[GET_SWZ(swizzle, 3)], condMask)) {
426 return GL_TRUE;
427 }
428 else {
429 return GL_FALSE;
430 }
431}
432
433
434
435/**
436 * Store 4 floats into a register. Observe the instructions saturate and
437 * set-condition-code flags.
438 */
439static void
Briane80d9012007-02-23 16:53:24 -0700440store_vector4(const struct prog_instruction *inst,
441 struct gl_program_machine *machine, const GLfloat value[4])
Brian13e3b212007-02-22 16:09:40 -0700442{
Brian Paulf4361542008-11-11 10:47:10 -0700443 const struct prog_dst_register *dstReg = &(inst->DstReg);
Brian13e3b212007-02-22 16:09:40 -0700444 const GLboolean clamp = inst->SaturateMode == SATURATE_ZERO_ONE;
Brian Paulf4361542008-11-11 10:47:10 -0700445 GLuint writeMask = dstReg->WriteMask;
Brian13e3b212007-02-22 16:09:40 -0700446 GLfloat clampedValue[4];
Brian Paulf4361542008-11-11 10:47:10 -0700447 GLfloat *dst = get_dst_register_pointer(dstReg, machine);
Brian13e3b212007-02-22 16:09:40 -0700448
449#if 0
450 if (value[0] > 1.0e10 ||
451 IS_INF_OR_NAN(value[0]) ||
452 IS_INF_OR_NAN(value[1]) ||
Briane80d9012007-02-23 16:53:24 -0700453 IS_INF_OR_NAN(value[2]) || IS_INF_OR_NAN(value[3]))
Brian13e3b212007-02-22 16:09:40 -0700454 printf("store %g %g %g %g\n", value[0], value[1], value[2], value[3]);
455#endif
456
457 if (clamp) {
458 clampedValue[0] = CLAMP(value[0], 0.0F, 1.0F);
459 clampedValue[1] = CLAMP(value[1], 0.0F, 1.0F);
460 clampedValue[2] = CLAMP(value[2], 0.0F, 1.0F);
461 clampedValue[3] = CLAMP(value[3], 0.0F, 1.0F);
462 value = clampedValue;
463 }
464
Brian Paulf4361542008-11-11 10:47:10 -0700465 if (dstReg->CondMask != COND_TR) {
Brian13e3b212007-02-22 16:09:40 -0700466 /* condition codes may turn off some writes */
467 if (writeMask & WRITEMASK_X) {
Brian Paulf4361542008-11-11 10:47:10 -0700468 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 0)],
469 dstReg->CondMask))
Brian13e3b212007-02-22 16:09:40 -0700470 writeMask &= ~WRITEMASK_X;
471 }
472 if (writeMask & WRITEMASK_Y) {
Brian Paulf4361542008-11-11 10:47:10 -0700473 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 1)],
474 dstReg->CondMask))
Brian13e3b212007-02-22 16:09:40 -0700475 writeMask &= ~WRITEMASK_Y;
476 }
477 if (writeMask & WRITEMASK_Z) {
Brian Paulf4361542008-11-11 10:47:10 -0700478 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 2)],
479 dstReg->CondMask))
Brian13e3b212007-02-22 16:09:40 -0700480 writeMask &= ~WRITEMASK_Z;
481 }
482 if (writeMask & WRITEMASK_W) {
Brian Paulf4361542008-11-11 10:47:10 -0700483 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 3)],
484 dstReg->CondMask))
Brian13e3b212007-02-22 16:09:40 -0700485 writeMask &= ~WRITEMASK_W;
486 }
487 }
488
Brian Paul92009542009-06-03 15:43:53 -0600489#ifdef NAN_CHECK
490 assert(!IS_INF_OR_NAN(value[0]));
491 assert(!IS_INF_OR_NAN(value[0]));
492 assert(!IS_INF_OR_NAN(value[0]));
493 assert(!IS_INF_OR_NAN(value[0]));
494#endif
495
Brian13e3b212007-02-22 16:09:40 -0700496 if (writeMask & WRITEMASK_X)
Brian Paulf4361542008-11-11 10:47:10 -0700497 dst[0] = value[0];
Brian13e3b212007-02-22 16:09:40 -0700498 if (writeMask & WRITEMASK_Y)
Brian Paulf4361542008-11-11 10:47:10 -0700499 dst[1] = value[1];
Brian13e3b212007-02-22 16:09:40 -0700500 if (writeMask & WRITEMASK_Z)
Brian Paulf4361542008-11-11 10:47:10 -0700501 dst[2] = value[2];
Brian13e3b212007-02-22 16:09:40 -0700502 if (writeMask & WRITEMASK_W)
Brian Paulf4361542008-11-11 10:47:10 -0700503 dst[3] = value[3];
Brian13e3b212007-02-22 16:09:40 -0700504
505 if (inst->CondUpdate) {
506 if (writeMask & WRITEMASK_X)
507 machine->CondCodes[0] = generate_cc(value[0]);
508 if (writeMask & WRITEMASK_Y)
509 machine->CondCodes[1] = generate_cc(value[1]);
510 if (writeMask & WRITEMASK_Z)
511 machine->CondCodes[2] = generate_cc(value[2]);
512 if (writeMask & WRITEMASK_W)
513 machine->CondCodes[3] = generate_cc(value[3]);
Briana01616e2007-03-28 11:01:28 -0600514#if DEBUG_PROG
515 printf("CondCodes=(%s,%s,%s,%s) for:\n",
516 _mesa_condcode_string(machine->CondCodes[0]),
517 _mesa_condcode_string(machine->CondCodes[1]),
518 _mesa_condcode_string(machine->CondCodes[2]),
519 _mesa_condcode_string(machine->CondCodes[3]));
520#endif
Brian13e3b212007-02-22 16:09:40 -0700521 }
522}
523
524
Brian13e3b212007-02-22 16:09:40 -0700525/**
Brian Paul37eef7b2008-11-07 09:33:55 -0700526 * Store 4 uints into a register. Observe the set-condition-code flags.
527 */
528static void
529store_vector4ui(const struct prog_instruction *inst,
530 struct gl_program_machine *machine, const GLuint value[4])
531{
Brian Paulf4361542008-11-11 10:47:10 -0700532 const struct prog_dst_register *dstReg = &(inst->DstReg);
533 GLuint writeMask = dstReg->WriteMask;
534 GLuint *dst = (GLuint *) get_dst_register_pointer(dstReg, machine);
Brian Paul37eef7b2008-11-07 09:33:55 -0700535
Brian Paulf4361542008-11-11 10:47:10 -0700536 if (dstReg->CondMask != COND_TR) {
Brian Paul37eef7b2008-11-07 09:33:55 -0700537 /* condition codes may turn off some writes */
538 if (writeMask & WRITEMASK_X) {
Brian Paulf4361542008-11-11 10:47:10 -0700539 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 0)],
540 dstReg->CondMask))
Brian Paul37eef7b2008-11-07 09:33:55 -0700541 writeMask &= ~WRITEMASK_X;
542 }
543 if (writeMask & WRITEMASK_Y) {
Brian Paulf4361542008-11-11 10:47:10 -0700544 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 1)],
545 dstReg->CondMask))
Brian Paul37eef7b2008-11-07 09:33:55 -0700546 writeMask &= ~WRITEMASK_Y;
547 }
548 if (writeMask & WRITEMASK_Z) {
Brian Paulf4361542008-11-11 10:47:10 -0700549 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 2)],
550 dstReg->CondMask))
Brian Paul37eef7b2008-11-07 09:33:55 -0700551 writeMask &= ~WRITEMASK_Z;
552 }
553 if (writeMask & WRITEMASK_W) {
Brian Paulf4361542008-11-11 10:47:10 -0700554 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 3)],
555 dstReg->CondMask))
Brian Paul37eef7b2008-11-07 09:33:55 -0700556 writeMask &= ~WRITEMASK_W;
557 }
558 }
559
560 if (writeMask & WRITEMASK_X)
Brian Paulf4361542008-11-11 10:47:10 -0700561 dst[0] = value[0];
Brian Paul37eef7b2008-11-07 09:33:55 -0700562 if (writeMask & WRITEMASK_Y)
Brian Paulf4361542008-11-11 10:47:10 -0700563 dst[1] = value[1];
Brian Paul37eef7b2008-11-07 09:33:55 -0700564 if (writeMask & WRITEMASK_Z)
Brian Paulf4361542008-11-11 10:47:10 -0700565 dst[2] = value[2];
Brian Paul37eef7b2008-11-07 09:33:55 -0700566 if (writeMask & WRITEMASK_W)
Brian Paulf4361542008-11-11 10:47:10 -0700567 dst[3] = value[3];
Brian Paul37eef7b2008-11-07 09:33:55 -0700568
569 if (inst->CondUpdate) {
570 if (writeMask & WRITEMASK_X)
571 machine->CondCodes[0] = generate_cc(value[0]);
572 if (writeMask & WRITEMASK_Y)
573 machine->CondCodes[1] = generate_cc(value[1]);
574 if (writeMask & WRITEMASK_Z)
575 machine->CondCodes[2] = generate_cc(value[2]);
576 if (writeMask & WRITEMASK_W)
577 machine->CondCodes[3] = generate_cc(value[3]);
578#if DEBUG_PROG
579 printf("CondCodes=(%s,%s,%s,%s) for:\n",
580 _mesa_condcode_string(machine->CondCodes[0]),
581 _mesa_condcode_string(machine->CondCodes[1]),
582 _mesa_condcode_string(machine->CondCodes[2]),
583 _mesa_condcode_string(machine->CondCodes[3]));
584#endif
585 }
586}
587
588
589
590/**
Brian13e3b212007-02-22 16:09:40 -0700591 * Execute the given vertex/fragment program.
592 *
Brian3c1c9992007-02-25 19:11:44 -0700593 * \param ctx rendering context
594 * \param program the program to execute
595 * \param machine machine state (must be initialized)
Brian13e3b212007-02-22 16:09:40 -0700596 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
597 */
598GLboolean
Briane80d9012007-02-23 16:53:24 -0700599_mesa_execute_program(GLcontext * ctx,
Brian8b34b7d2007-02-25 18:26:50 -0700600 const struct gl_program *program,
Brian085d7d52007-02-25 18:23:37 -0700601 struct gl_program_machine *machine)
Brian13e3b212007-02-22 16:09:40 -0700602{
Brian8b34b7d2007-02-25 18:26:50 -0700603 const GLuint numInst = program->NumInstructions;
Briancfd00112007-02-25 18:30:45 -0700604 const GLuint maxExec = 10000;
José Fonseca452a5922008-05-31 18:14:09 +0900605 GLuint pc, numExec = 0;
Brian13e3b212007-02-22 16:09:40 -0700606
607 machine->CurProgram = program;
608
609 if (DEBUG_PROG) {
610 printf("execute program %u --------------------\n", program->Id);
611 }
612
Brian33eac562007-02-25 18:52:41 -0700613 if (program->Target == GL_VERTEX_PROGRAM_ARB) {
614 machine->EnvParams = ctx->VertexProgram.Parameters;
615 }
616 else {
617 machine->EnvParams = ctx->FragmentProgram.Parameters;
618 }
619
Brian8b34b7d2007-02-25 18:26:50 -0700620 for (pc = 0; pc < numInst; pc++) {
Brian13e3b212007-02-22 16:09:40 -0700621 const struct prog_instruction *inst = program->Instructions + pc;
622
Brian13e3b212007-02-22 16:09:40 -0700623 if (DEBUG_PROG) {
624 _mesa_print_instruction(inst);
625 }
626
627 switch (inst->Opcode) {
Briane80d9012007-02-23 16:53:24 -0700628 case OPCODE_ABS:
629 {
630 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700631 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700632 result[0] = FABSF(a[0]);
633 result[1] = FABSF(a[1]);
634 result[2] = FABSF(a[2]);
635 result[3] = FABSF(a[3]);
636 store_vector4(inst, machine, result);
637 }
638 break;
639 case OPCODE_ADD:
640 {
641 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700642 fetch_vector4(&inst->SrcReg[0], machine, a);
643 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700644 result[0] = a[0] + b[0];
645 result[1] = a[1] + b[1];
646 result[2] = a[2] + b[2];
647 result[3] = a[3] + b[3];
648 store_vector4(inst, machine, result);
649 if (DEBUG_PROG) {
650 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
651 result[0], result[1], result[2], result[3],
652 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -0700653 }
Briane80d9012007-02-23 16:53:24 -0700654 }
655 break;
Brian Paul37eef7b2008-11-07 09:33:55 -0700656 case OPCODE_AND: /* bitwise AND */
657 {
658 GLuint a[4], b[4], result[4];
659 fetch_vector4ui(&inst->SrcReg[0], machine, a);
660 fetch_vector4ui(&inst->SrcReg[1], machine, b);
661 result[0] = a[0] & b[0];
662 result[1] = a[1] & b[1];
663 result[2] = a[2] & b[2];
664 result[3] = a[3] & b[3];
665 store_vector4ui(inst, machine, result);
666 }
667 break;
Brianf183a2d2007-02-23 17:14:30 -0700668 case OPCODE_ARL:
669 {
670 GLfloat t[4];
Brian33eac562007-02-25 18:52:41 -0700671 fetch_vector4(&inst->SrcReg[0], machine, t);
Brian Paula9475cc2008-12-12 18:03:48 -0700672 machine->AddressReg[0][0] = IFLOOR(t[0]);
Brianf183a2d2007-02-23 17:14:30 -0700673 }
674 break;
Briane80d9012007-02-23 16:53:24 -0700675 case OPCODE_BGNLOOP:
676 /* no-op */
677 break;
678 case OPCODE_ENDLOOP:
679 /* subtract 1 here since pc is incremented by for(pc) loop */
680 pc = inst->BranchTarget - 1; /* go to matching BNGLOOP */
681 break;
682 case OPCODE_BGNSUB: /* begin subroutine */
683 break;
684 case OPCODE_ENDSUB: /* end subroutine */
685 break;
686 case OPCODE_BRA: /* branch (conditional) */
687 /* fall-through */
688 case OPCODE_BRK: /* break out of loop (conditional) */
689 /* fall-through */
690 case OPCODE_CONT: /* continue loop (conditional) */
691 if (eval_condition(machine, inst)) {
692 /* take branch */
693 /* Subtract 1 here since we'll do pc++ at end of for-loop */
694 pc = inst->BranchTarget - 1;
695 }
696 break;
697 case OPCODE_CAL: /* Call subroutine (conditional) */
698 if (eval_condition(machine, inst)) {
699 /* call the subroutine */
700 if (machine->StackDepth >= MAX_PROGRAM_CALL_DEPTH) {
701 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
Brian13e3b212007-02-22 16:09:40 -0700702 }
Briana0275b02007-03-27 11:02:20 -0600703 machine->CallStack[machine->StackDepth++] = pc + 1; /* next inst */
Brian31dc7a32007-03-27 15:21:35 -0600704 /* Subtract 1 here since we'll do pc++ at end of for-loop */
705 pc = inst->BranchTarget - 1;
Briane80d9012007-02-23 16:53:24 -0700706 }
707 break;
708 case OPCODE_CMP:
709 {
710 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700711 fetch_vector4(&inst->SrcReg[0], machine, a);
712 fetch_vector4(&inst->SrcReg[1], machine, b);
713 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -0700714 result[0] = a[0] < 0.0F ? b[0] : c[0];
715 result[1] = a[1] < 0.0F ? b[1] : c[1];
716 result[2] = a[2] < 0.0F ? b[2] : c[2];
717 result[3] = a[3] < 0.0F ? b[3] : c[3];
718 store_vector4(inst, machine, result);
719 }
720 break;
721 case OPCODE_COS:
722 {
723 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700724 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700725 result[0] = result[1] = result[2] = result[3]
726 = (GLfloat) _mesa_cos(a[0]);
727 store_vector4(inst, machine, result);
728 }
729 break;
730 case OPCODE_DDX: /* Partial derivative with respect to X */
731 {
Brian62da6a12007-05-02 18:44:34 -0600732 GLfloat result[4];
733 fetch_vector4_deriv(ctx, &inst->SrcReg[0], machine,
734 'X', result);
Briane80d9012007-02-23 16:53:24 -0700735 store_vector4(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -0700736 }
737 break;
738 case OPCODE_DDY: /* Partial derivative with respect to Y */
739 {
Brian62da6a12007-05-02 18:44:34 -0600740 GLfloat result[4];
741 fetch_vector4_deriv(ctx, &inst->SrcReg[0], machine,
742 'Y', result);
Briane80d9012007-02-23 16:53:24 -0700743 store_vector4(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -0700744 }
745 break;
Brian Paul65cb74e2008-11-07 09:41:00 -0700746 case OPCODE_DP2:
747 {
748 GLfloat a[4], b[4], result[4];
749 fetch_vector4(&inst->SrcReg[0], machine, a);
750 fetch_vector4(&inst->SrcReg[1], machine, b);
751 result[0] = result[1] = result[2] = result[3] = DOT2(a, b);
752 store_vector4(inst, machine, result);
753 if (DEBUG_PROG) {
754 printf("DP2 %g = (%g %g) . (%g %g)\n",
755 result[0], a[0], a[1], b[0], b[1]);
756 }
757 }
758 break;
759 case OPCODE_DP2A:
760 {
761 GLfloat a[4], b[4], c, result[4];
762 fetch_vector4(&inst->SrcReg[0], machine, a);
763 fetch_vector4(&inst->SrcReg[1], machine, b);
764 fetch_vector1(&inst->SrcReg[1], machine, &c);
765 result[0] = result[1] = result[2] = result[3] = DOT2(a, b) + c;
766 store_vector4(inst, machine, result);
767 if (DEBUG_PROG) {
768 printf("DP2A %g = (%g %g) . (%g %g) + %g\n",
769 result[0], a[0], a[1], b[0], b[1], c);
770 }
771 }
772 break;
Briane80d9012007-02-23 16:53:24 -0700773 case OPCODE_DP3:
774 {
775 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700776 fetch_vector4(&inst->SrcReg[0], machine, a);
777 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700778 result[0] = result[1] = result[2] = result[3] = DOT3(a, b);
779 store_vector4(inst, machine, result);
780 if (DEBUG_PROG) {
781 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
782 result[0], a[0], a[1], a[2], b[0], b[1], b[2]);
Brian13e3b212007-02-22 16:09:40 -0700783 }
Briane80d9012007-02-23 16:53:24 -0700784 }
785 break;
786 case OPCODE_DP4:
787 {
788 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700789 fetch_vector4(&inst->SrcReg[0], machine, a);
790 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700791 result[0] = result[1] = result[2] = result[3] = DOT4(a, b);
792 store_vector4(inst, machine, result);
793 if (DEBUG_PROG) {
794 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
795 result[0], a[0], a[1], a[2], a[3],
796 b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -0700797 }
Briane80d9012007-02-23 16:53:24 -0700798 }
799 break;
800 case OPCODE_DPH:
801 {
802 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700803 fetch_vector4(&inst->SrcReg[0], machine, a);
804 fetch_vector4(&inst->SrcReg[1], machine, b);
Brian Paul65cb74e2008-11-07 09:41:00 -0700805 result[0] = result[1] = result[2] = result[3] = DOT3(a, b) + b[3];
Briane80d9012007-02-23 16:53:24 -0700806 store_vector4(inst, machine, result);
807 }
808 break;
809 case OPCODE_DST: /* Distance vector */
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] = 1.0F;
815 result[1] = a[1] * b[1];
816 result[2] = a[2];
817 result[3] = b[3];
818 store_vector4(inst, machine, result);
819 }
820 break;
Brianf183a2d2007-02-23 17:14:30 -0700821 case OPCODE_EXP:
Brianf183a2d2007-02-23 17:14:30 -0700822 {
823 GLfloat t[4], q[4], floor_t0;
Brian33eac562007-02-25 18:52:41 -0700824 fetch_vector1(&inst->SrcReg[0], machine, t);
Brianf183a2d2007-02-23 17:14:30 -0700825 floor_t0 = FLOORF(t[0]);
826 if (floor_t0 > FLT_MAX_EXP) {
827 SET_POS_INFINITY(q[0]);
828 SET_POS_INFINITY(q[2]);
829 }
830 else if (floor_t0 < FLT_MIN_EXP) {
831 q[0] = 0.0F;
832 q[2] = 0.0F;
833 }
834 else {
Brian761728a2007-02-24 11:14:57 -0700835 q[0] = LDEXPF(1.0, (int) floor_t0);
836 /* Note: GL_NV_vertex_program expects
837 * result.z = result.x * APPX(result.y)
838 * We do what the ARB extension says.
839 */
Brian Paulb8a200a2009-04-03 10:29:11 -0600840 q[2] = (GLfloat) _mesa_pow(2.0, t[0]);
Brianf183a2d2007-02-23 17:14:30 -0700841 }
842 q[1] = t[0] - floor_t0;
843 q[3] = 1.0F;
844 store_vector4( inst, machine, q );
845 }
846 break;
Briane80d9012007-02-23 16:53:24 -0700847 case OPCODE_EX2: /* Exponential base 2 */
848 {
Brian Paul035de6a2009-06-03 15:42:52 -0600849 GLfloat a[4], result[4], val;
Brian33eac562007-02-25 18:52:41 -0700850 fetch_vector1(&inst->SrcReg[0], machine, a);
Brian Paul035de6a2009-06-03 15:42:52 -0600851 val = (GLfloat) _mesa_pow(2.0, a[0]);
852 /*
853 if (IS_INF_OR_NAN(val))
854 val = 1.0e10;
855 */
856 result[0] = result[1] = result[2] = result[3] = val;
Briane80d9012007-02-23 16:53:24 -0700857 store_vector4(inst, machine, result);
858 }
859 break;
860 case OPCODE_FLR:
861 {
862 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700863 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700864 result[0] = FLOORF(a[0]);
865 result[1] = FLOORF(a[1]);
866 result[2] = FLOORF(a[2]);
867 result[3] = FLOORF(a[3]);
868 store_vector4(inst, machine, result);
869 }
870 break;
871 case OPCODE_FRC:
872 {
873 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700874 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700875 result[0] = a[0] - FLOORF(a[0]);
876 result[1] = a[1] - FLOORF(a[1]);
877 result[2] = a[2] - FLOORF(a[2]);
878 result[3] = a[3] - FLOORF(a[3]);
879 store_vector4(inst, machine, result);
880 }
881 break;
882 case OPCODE_IF:
Brian63556fa2007-03-23 14:47:46 -0600883 {
884 GLboolean cond;
885 /* eval condition */
886 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
887 GLfloat a[4];
888 fetch_vector1(&inst->SrcReg[0], machine, a);
889 cond = (a[0] != 0.0);
890 }
891 else {
892 cond = eval_condition(machine, inst);
893 }
Briana7f73662007-04-20 08:11:51 -0600894 if (DEBUG_PROG) {
895 printf("IF: %d\n", cond);
896 }
Brian63556fa2007-03-23 14:47:46 -0600897 /* do if/else */
898 if (cond) {
899 /* do if-clause (just continue execution) */
900 }
901 else {
902 /* go to the instruction after ELSE or ENDIF */
903 assert(inst->BranchTarget >= 0);
904 pc = inst->BranchTarget - 1;
905 }
Briane80d9012007-02-23 16:53:24 -0700906 }
907 break;
908 case OPCODE_ELSE:
909 /* goto ENDIF */
910 assert(inst->BranchTarget >= 0);
911 pc = inst->BranchTarget - 1;
912 break;
913 case OPCODE_ENDIF:
914 /* nothing */
915 break;
Briane80d9012007-02-23 16:53:24 -0700916 case OPCODE_KIL_NV: /* NV_f_p only (conditional) */
917 if (eval_condition(machine, inst)) {
918 return GL_FALSE;
919 }
920 break;
921 case OPCODE_KIL: /* ARB_f_p only */
922 {
923 GLfloat a[4];
Brian33eac562007-02-25 18:52:41 -0700924 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700925 if (a[0] < 0.0F || a[1] < 0.0F || a[2] < 0.0F || a[3] < 0.0F) {
Brian13e3b212007-02-22 16:09:40 -0700926 return GL_FALSE;
927 }
Briane80d9012007-02-23 16:53:24 -0700928 }
929 break;
930 case OPCODE_LG2: /* log base 2 */
931 {
Brian Paul035de6a2009-06-03 15:42:52 -0600932 GLfloat a[4], result[4], val;
Brian33eac562007-02-25 18:52:41 -0700933 fetch_vector1(&inst->SrcReg[0], machine, a);
Ian Romanick962fa6b2008-12-18 14:11:06 -0800934 /* The fast LOG2 macro doesn't meet the precision requirements.
935 */
Brian Paul035de6a2009-06-03 15:42:52 -0600936 if (a[0] == 0.0F) {
937 val = 0.0F;
938 }
939 else {
940 val = log(a[0]) * 1.442695F;
941 }
942 result[0] = result[1] = result[2] = result[3] = val;
Briane80d9012007-02-23 16:53:24 -0700943 store_vector4(inst, machine, result);
944 }
945 break;
946 case OPCODE_LIT:
947 {
948 const GLfloat epsilon = 1.0F / 256.0F; /* from NV VP spec */
949 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700950 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700951 a[0] = MAX2(a[0], 0.0F);
952 a[1] = MAX2(a[1], 0.0F);
953 /* XXX ARB version clamps a[3], NV version doesn't */
954 a[3] = CLAMP(a[3], -(128.0F - epsilon), (128.0F - epsilon));
955 result[0] = 1.0F;
956 result[1] = a[0];
957 /* XXX we could probably just use pow() here */
958 if (a[0] > 0.0F) {
959 if (a[1] == 0.0 && a[3] == 0.0)
960 result[2] = 1.0;
961 else
Brian Paulb8a200a2009-04-03 10:29:11 -0600962 result[2] = (GLfloat) _mesa_pow(a[1], a[3]);
Brian13e3b212007-02-22 16:09:40 -0700963 }
Briane80d9012007-02-23 16:53:24 -0700964 else {
965 result[2] = 0.0;
Brian13e3b212007-02-22 16:09:40 -0700966 }
Briane80d9012007-02-23 16:53:24 -0700967 result[3] = 1.0F;
968 store_vector4(inst, machine, result);
969 if (DEBUG_PROG) {
970 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
971 result[0], result[1], result[2], result[3],
972 a[0], a[1], a[2], a[3]);
Brian13e3b212007-02-22 16:09:40 -0700973 }
Briane80d9012007-02-23 16:53:24 -0700974 }
975 break;
Brianf183a2d2007-02-23 17:14:30 -0700976 case OPCODE_LOG:
977 {
978 GLfloat t[4], q[4], abs_t0;
Brian33eac562007-02-25 18:52:41 -0700979 fetch_vector1(&inst->SrcReg[0], machine, t);
Brianf183a2d2007-02-23 17:14:30 -0700980 abs_t0 = FABSF(t[0]);
981 if (abs_t0 != 0.0F) {
982 /* Since we really can't handle infinite values on VMS
983 * like other OSes we'll use __MAXFLOAT to represent
984 * infinity. This may need some tweaking.
985 */
986#ifdef VMS
987 if (abs_t0 == __MAXFLOAT)
988#else
989 if (IS_INF_OR_NAN(abs_t0))
990#endif
991 {
992 SET_POS_INFINITY(q[0]);
993 q[1] = 1.0F;
994 SET_POS_INFINITY(q[2]);
995 }
996 else {
997 int exponent;
998 GLfloat mantissa = FREXPF(t[0], &exponent);
999 q[0] = (GLfloat) (exponent - 1);
1000 q[1] = (GLfloat) (2.0 * mantissa); /* map [.5, 1) -> [1, 2) */
Ian Romanick962fa6b2008-12-18 14:11:06 -08001001
1002 /* The fast LOG2 macro doesn't meet the precision
1003 * requirements.
1004 */
1005 q[2] = (log(t[0]) * 1.442695F);
Brianf183a2d2007-02-23 17:14:30 -07001006 }
1007 }
1008 else {
1009 SET_NEG_INFINITY(q[0]);
1010 q[1] = 1.0F;
1011 SET_NEG_INFINITY(q[2]);
1012 }
1013 q[3] = 1.0;
1014 store_vector4(inst, machine, q);
1015 }
1016 break;
Briane80d9012007-02-23 16:53:24 -07001017 case OPCODE_LRP:
1018 {
1019 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001020 fetch_vector4(&inst->SrcReg[0], machine, a);
1021 fetch_vector4(&inst->SrcReg[1], machine, b);
1022 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -07001023 result[0] = a[0] * b[0] + (1.0F - a[0]) * c[0];
1024 result[1] = a[1] * b[1] + (1.0F - a[1]) * c[1];
1025 result[2] = a[2] * b[2] + (1.0F - a[2]) * c[2];
1026 result[3] = a[3] * b[3] + (1.0F - a[3]) * c[3];
1027 store_vector4(inst, machine, result);
1028 if (DEBUG_PROG) {
1029 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
1030 "(%g %g %g %g), (%g %g %g %g)\n",
1031 result[0], result[1], result[2], result[3],
1032 a[0], a[1], a[2], a[3],
1033 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
Brian13e3b212007-02-22 16:09:40 -07001034 }
Briane80d9012007-02-23 16:53:24 -07001035 }
1036 break;
1037 case OPCODE_MAD:
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] + c[0];
1044 result[1] = a[1] * b[1] + c[1];
1045 result[2] = a[2] * b[2] + c[2];
1046 result[3] = a[3] * b[3] + c[3];
1047 store_vector4(inst, machine, result);
1048 if (DEBUG_PROG) {
1049 printf("MAD (%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_MAX:
1058 {
1059 GLfloat a[4], b[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);
Briane80d9012007-02-23 16:53:24 -07001062 result[0] = MAX2(a[0], b[0]);
1063 result[1] = MAX2(a[1], b[1]);
1064 result[2] = MAX2(a[2], b[2]);
1065 result[3] = MAX2(a[3], b[3]);
1066 store_vector4(inst, machine, result);
1067 if (DEBUG_PROG) {
1068 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
1069 result[0], result[1], result[2], result[3],
1070 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001071 }
Briane80d9012007-02-23 16:53:24 -07001072 }
1073 break;
1074 case OPCODE_MIN:
1075 {
1076 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001077 fetch_vector4(&inst->SrcReg[0], machine, a);
1078 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001079 result[0] = MIN2(a[0], b[0]);
1080 result[1] = MIN2(a[1], b[1]);
1081 result[2] = MIN2(a[2], b[2]);
1082 result[3] = MIN2(a[3], b[3]);
1083 store_vector4(inst, machine, result);
1084 }
1085 break;
1086 case OPCODE_MOV:
1087 {
1088 GLfloat result[4];
Brian33eac562007-02-25 18:52:41 -07001089 fetch_vector4(&inst->SrcReg[0], machine, result);
Briane80d9012007-02-23 16:53:24 -07001090 store_vector4(inst, machine, result);
1091 if (DEBUG_PROG) {
1092 printf("MOV (%g %g %g %g)\n",
1093 result[0], result[1], result[2], result[3]);
Brian13e3b212007-02-22 16:09:40 -07001094 }
Briane80d9012007-02-23 16:53:24 -07001095 }
1096 break;
1097 case OPCODE_MUL:
1098 {
1099 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001100 fetch_vector4(&inst->SrcReg[0], machine, a);
1101 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001102 result[0] = a[0] * b[0];
1103 result[1] = a[1] * b[1];
1104 result[2] = a[2] * b[2];
1105 result[3] = a[3] * b[3];
1106 store_vector4(inst, machine, result);
1107 if (DEBUG_PROG) {
1108 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1109 result[0], result[1], result[2], result[3],
1110 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001111 }
Briane80d9012007-02-23 16:53:24 -07001112 }
1113 break;
1114 case OPCODE_NOISE1:
1115 {
1116 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001117 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001118 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001119 result[1] =
Brian Paul702b5b02008-12-15 18:37:39 -07001120 result[2] =
1121 result[3] = _mesa_noise1(a[0]);
Briane80d9012007-02-23 16:53:24 -07001122 store_vector4(inst, machine, result);
1123 }
1124 break;
1125 case OPCODE_NOISE2:
1126 {
1127 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001128 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001129 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001130 result[1] =
Brian Paul702b5b02008-12-15 18:37:39 -07001131 result[2] = result[3] = _mesa_noise2(a[0], a[1]);
Briane80d9012007-02-23 16:53:24 -07001132 store_vector4(inst, machine, result);
1133 }
1134 break;
1135 case OPCODE_NOISE3:
1136 {
1137 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001138 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001139 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001140 result[1] =
1141 result[2] =
Brian Paul702b5b02008-12-15 18:37:39 -07001142 result[3] = _mesa_noise3(a[0], a[1], a[2]);
Briane80d9012007-02-23 16:53:24 -07001143 store_vector4(inst, machine, result);
1144 }
1145 break;
1146 case OPCODE_NOISE4:
1147 {
1148 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001149 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001150 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001151 result[1] =
1152 result[2] =
Brian Paul702b5b02008-12-15 18:37:39 -07001153 result[3] = _mesa_noise4(a[0], a[1], a[2], a[3]);
Briane80d9012007-02-23 16:53:24 -07001154 store_vector4(inst, machine, result);
1155 }
1156 break;
1157 case OPCODE_NOP:
1158 break;
Brian Paul37eef7b2008-11-07 09:33:55 -07001159 case OPCODE_NOT: /* bitwise NOT */
1160 {
1161 GLuint a[4], result[4];
1162 fetch_vector4ui(&inst->SrcReg[0], machine, a);
1163 result[0] = ~a[0];
1164 result[1] = ~a[1];
1165 result[2] = ~a[2];
1166 result[3] = ~a[3];
1167 store_vector4ui(inst, machine, result);
1168 }
1169 break;
Brian Paulf6ead502008-11-07 08:51:31 -07001170 case OPCODE_NRM3: /* 3-component normalization */
1171 {
1172 GLfloat a[4], result[4];
1173 GLfloat tmp;
1174 fetch_vector4(&inst->SrcReg[0], machine, a);
1175 tmp = a[0] * a[0] + a[1] * a[1] + a[2] * a[2];
1176 if (tmp != 0.0F)
Brian Paul22459e72008-11-07 12:59:36 -07001177 tmp = INV_SQRTF(tmp);
Brian Paulf6ead502008-11-07 08:51:31 -07001178 result[0] = tmp * a[0];
1179 result[1] = tmp * a[1];
1180 result[2] = tmp * a[2];
1181 result[3] = 0.0; /* undefined, but prevent valgrind warnings */
1182 store_vector4(inst, machine, result);
1183 }
1184 break;
1185 case OPCODE_NRM4: /* 4-component normalization */
1186 {
1187 GLfloat a[4], result[4];
1188 GLfloat tmp;
1189 fetch_vector4(&inst->SrcReg[0], machine, a);
1190 tmp = a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3];
1191 if (tmp != 0.0F)
Brian Paul22459e72008-11-07 12:59:36 -07001192 tmp = INV_SQRTF(tmp);
Brian Paulf6ead502008-11-07 08:51:31 -07001193 result[0] = tmp * a[0];
1194 result[1] = tmp * a[1];
1195 result[2] = tmp * a[2];
1196 result[3] = tmp * a[3];
1197 store_vector4(inst, machine, result);
1198 }
1199 break;
Brian Paul37eef7b2008-11-07 09:33:55 -07001200 case OPCODE_OR: /* bitwise OR */
1201 {
1202 GLuint a[4], b[4], result[4];
1203 fetch_vector4ui(&inst->SrcReg[0], machine, a);
1204 fetch_vector4ui(&inst->SrcReg[1], machine, b);
1205 result[0] = a[0] | b[0];
1206 result[1] = a[1] | b[1];
1207 result[2] = a[2] | b[2];
1208 result[3] = a[3] | b[3];
1209 store_vector4ui(inst, machine, result);
1210 }
1211 break;
Briane80d9012007-02-23 16:53:24 -07001212 case OPCODE_PK2H: /* pack two 16-bit floats in one 32-bit float */
1213 {
Brian Paul37eef7b2008-11-07 09:33:55 -07001214 GLfloat a[4];
1215 GLuint result[4];
Briane80d9012007-02-23 16:53:24 -07001216 GLhalfNV hx, hy;
Brian33eac562007-02-25 18:52:41 -07001217 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001218 hx = _mesa_float_to_half(a[0]);
1219 hy = _mesa_float_to_half(a[1]);
Brian Paul37eef7b2008-11-07 09:33:55 -07001220 result[0] =
1221 result[1] =
1222 result[2] =
1223 result[3] = hx | (hy << 16);
1224 store_vector4ui(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -07001225 }
1226 break;
1227 case OPCODE_PK2US: /* pack two GLushorts into one 32-bit float */
1228 {
Brian Paul37eef7b2008-11-07 09:33:55 -07001229 GLfloat a[4];
1230 GLuint result[4], usx, usy;
Brian33eac562007-02-25 18:52:41 -07001231 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001232 a[0] = CLAMP(a[0], 0.0F, 1.0F);
1233 a[1] = CLAMP(a[1], 0.0F, 1.0F);
1234 usx = IROUND(a[0] * 65535.0F);
1235 usy = IROUND(a[1] * 65535.0F);
Brian Paul37eef7b2008-11-07 09:33:55 -07001236 result[0] =
1237 result[1] =
1238 result[2] =
1239 result[3] = usx | (usy << 16);
1240 store_vector4ui(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -07001241 }
1242 break;
1243 case OPCODE_PK4B: /* pack four GLbytes into one 32-bit float */
1244 {
Brian Paul37eef7b2008-11-07 09:33:55 -07001245 GLfloat a[4];
1246 GLuint result[4], ubx, uby, ubz, ubw;
Brian33eac562007-02-25 18:52:41 -07001247 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001248 a[0] = CLAMP(a[0], -128.0F / 127.0F, 1.0F);
1249 a[1] = CLAMP(a[1], -128.0F / 127.0F, 1.0F);
1250 a[2] = CLAMP(a[2], -128.0F / 127.0F, 1.0F);
1251 a[3] = CLAMP(a[3], -128.0F / 127.0F, 1.0F);
1252 ubx = IROUND(127.0F * a[0] + 128.0F);
1253 uby = IROUND(127.0F * a[1] + 128.0F);
1254 ubz = IROUND(127.0F * a[2] + 128.0F);
1255 ubw = IROUND(127.0F * a[3] + 128.0F);
Brian Paul37eef7b2008-11-07 09:33:55 -07001256 result[0] =
1257 result[1] =
1258 result[2] =
1259 result[3] = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
1260 store_vector4ui(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -07001261 }
1262 break;
1263 case OPCODE_PK4UB: /* pack four GLubytes 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], 0.0F, 1.0F);
1269 a[1] = CLAMP(a[1], 0.0F, 1.0F);
1270 a[2] = CLAMP(a[2], 0.0F, 1.0F);
1271 a[3] = CLAMP(a[3], 0.0F, 1.0F);
1272 ubx = IROUND(255.0F * a[0]);
1273 uby = IROUND(255.0F * a[1]);
1274 ubz = IROUND(255.0F * a[2]);
1275 ubw = IROUND(255.0F * a[3]);
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_POW:
1284 {
1285 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001286 fetch_vector1(&inst->SrcReg[0], machine, a);
1287 fetch_vector1(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001288 result[0] = result[1] = result[2] = result[3]
1289 = (GLfloat) _mesa_pow(a[0], b[0]);
1290 store_vector4(inst, machine, result);
1291 }
1292 break;
1293 case OPCODE_RCP:
1294 {
1295 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001296 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001297 if (DEBUG_PROG) {
1298 if (a[0] == 0)
1299 printf("RCP(0)\n");
1300 else if (IS_INF_OR_NAN(a[0]))
1301 printf("RCP(inf)\n");
Brian13e3b212007-02-22 16:09:40 -07001302 }
Briane80d9012007-02-23 16:53:24 -07001303 result[0] = result[1] = result[2] = result[3] = 1.0F / a[0];
1304 store_vector4(inst, machine, result);
1305 }
1306 break;
1307 case OPCODE_RET: /* return from subroutine (conditional) */
1308 if (eval_condition(machine, inst)) {
1309 if (machine->StackDepth == 0) {
1310 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
Brian13e3b212007-02-22 16:09:40 -07001311 }
Briana0275b02007-03-27 11:02:20 -06001312 /* subtract one because of pc++ in the for loop */
1313 pc = machine->CallStack[--machine->StackDepth] - 1;
Briane80d9012007-02-23 16:53:24 -07001314 }
1315 break;
1316 case OPCODE_RFL: /* reflection vector */
1317 {
1318 GLfloat axis[4], dir[4], result[4], tmpX, tmpW;
Brian33eac562007-02-25 18:52:41 -07001319 fetch_vector4(&inst->SrcReg[0], machine, axis);
1320 fetch_vector4(&inst->SrcReg[1], machine, dir);
Briane80d9012007-02-23 16:53:24 -07001321 tmpW = DOT3(axis, axis);
1322 tmpX = (2.0F * DOT3(axis, dir)) / tmpW;
1323 result[0] = tmpX * axis[0] - dir[0];
1324 result[1] = tmpX * axis[1] - dir[1];
1325 result[2] = tmpX * axis[2] - dir[2];
1326 /* result[3] is never written! XXX enforce in parser! */
1327 store_vector4(inst, machine, result);
1328 }
1329 break;
1330 case OPCODE_RSQ: /* 1 / sqrt() */
1331 {
1332 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001333 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001334 a[0] = FABSF(a[0]);
1335 result[0] = result[1] = result[2] = result[3] = INV_SQRTF(a[0]);
1336 store_vector4(inst, machine, result);
1337 if (DEBUG_PROG) {
1338 printf("RSQ %g = 1/sqrt(|%g|)\n", result[0], a[0]);
Brian13e3b212007-02-22 16:09:40 -07001339 }
Briane80d9012007-02-23 16:53:24 -07001340 }
1341 break;
1342 case OPCODE_SCS: /* sine and cos */
1343 {
1344 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001345 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001346 result[0] = (GLfloat) _mesa_cos(a[0]);
1347 result[1] = (GLfloat) _mesa_sin(a[0]);
1348 result[2] = 0.0; /* undefined! */
1349 result[3] = 0.0; /* undefined! */
1350 store_vector4(inst, machine, result);
1351 }
1352 break;
1353 case OPCODE_SEQ: /* set on equal */
1354 {
1355 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001356 fetch_vector4(&inst->SrcReg[0], machine, a);
1357 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001358 result[0] = (a[0] == b[0]) ? 1.0F : 0.0F;
1359 result[1] = (a[1] == b[1]) ? 1.0F : 0.0F;
1360 result[2] = (a[2] == b[2]) ? 1.0F : 0.0F;
1361 result[3] = (a[3] == b[3]) ? 1.0F : 0.0F;
1362 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001363 if (DEBUG_PROG) {
1364 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1365 result[0], result[1], result[2], result[3],
1366 a[0], a[1], a[2], a[3],
1367 b[0], b[1], b[2], b[3]);
1368 }
Briane80d9012007-02-23 16:53:24 -07001369 }
1370 break;
1371 case OPCODE_SFL: /* set false, operands ignored */
1372 {
1373 static const GLfloat result[4] = { 0.0F, 0.0F, 0.0F, 0.0F };
1374 store_vector4(inst, machine, result);
1375 }
1376 break;
1377 case OPCODE_SGE: /* set on greater or equal */
1378 {
1379 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001380 fetch_vector4(&inst->SrcReg[0], machine, a);
1381 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001382 result[0] = (a[0] >= b[0]) ? 1.0F : 0.0F;
1383 result[1] = (a[1] >= b[1]) ? 1.0F : 0.0F;
1384 result[2] = (a[2] >= b[2]) ? 1.0F : 0.0F;
1385 result[3] = (a[3] >= b[3]) ? 1.0F : 0.0F;
1386 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001387 if (DEBUG_PROG) {
1388 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1389 result[0], result[1], result[2], result[3],
1390 a[0], a[1], a[2], a[3],
1391 b[0], b[1], b[2], b[3]);
1392 }
Briane80d9012007-02-23 16:53:24 -07001393 }
1394 break;
1395 case OPCODE_SGT: /* set on greater */
1396 {
1397 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001398 fetch_vector4(&inst->SrcReg[0], machine, a);
1399 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001400 result[0] = (a[0] > b[0]) ? 1.0F : 0.0F;
1401 result[1] = (a[1] > b[1]) ? 1.0F : 0.0F;
1402 result[2] = (a[2] > b[2]) ? 1.0F : 0.0F;
1403 result[3] = (a[3] > b[3]) ? 1.0F : 0.0F;
1404 store_vector4(inst, machine, result);
1405 if (DEBUG_PROG) {
Brian28ab1122007-03-06 12:15:30 -07001406 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1407 result[0], result[1], result[2], result[3],
1408 a[0], a[1], a[2], a[3],
1409 b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001410 }
Briane80d9012007-02-23 16:53:24 -07001411 }
1412 break;
1413 case OPCODE_SIN:
1414 {
1415 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001416 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001417 result[0] = result[1] = result[2] = result[3]
1418 = (GLfloat) _mesa_sin(a[0]);
1419 store_vector4(inst, machine, result);
1420 }
1421 break;
1422 case OPCODE_SLE: /* set on less or equal */
1423 {
1424 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001425 fetch_vector4(&inst->SrcReg[0], machine, a);
1426 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001427 result[0] = (a[0] <= b[0]) ? 1.0F : 0.0F;
1428 result[1] = (a[1] <= b[1]) ? 1.0F : 0.0F;
1429 result[2] = (a[2] <= b[2]) ? 1.0F : 0.0F;
1430 result[3] = (a[3] <= b[3]) ? 1.0F : 0.0F;
1431 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001432 if (DEBUG_PROG) {
1433 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1434 result[0], result[1], result[2], result[3],
1435 a[0], a[1], a[2], a[3],
1436 b[0], b[1], b[2], b[3]);
1437 }
Briane80d9012007-02-23 16:53:24 -07001438 }
1439 break;
1440 case OPCODE_SLT: /* set on less */
1441 {
1442 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001443 fetch_vector4(&inst->SrcReg[0], machine, a);
1444 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001445 result[0] = (a[0] < b[0]) ? 1.0F : 0.0F;
1446 result[1] = (a[1] < b[1]) ? 1.0F : 0.0F;
1447 result[2] = (a[2] < b[2]) ? 1.0F : 0.0F;
1448 result[3] = (a[3] < b[3]) ? 1.0F : 0.0F;
1449 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001450 if (DEBUG_PROG) {
1451 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1452 result[0], result[1], result[2], result[3],
1453 a[0], a[1], a[2], a[3],
1454 b[0], b[1], b[2], b[3]);
1455 }
Briane80d9012007-02-23 16:53:24 -07001456 }
1457 break;
1458 case OPCODE_SNE: /* set on not equal */
1459 {
1460 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001461 fetch_vector4(&inst->SrcReg[0], machine, a);
1462 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001463 result[0] = (a[0] != b[0]) ? 1.0F : 0.0F;
1464 result[1] = (a[1] != b[1]) ? 1.0F : 0.0F;
1465 result[2] = (a[2] != b[2]) ? 1.0F : 0.0F;
1466 result[3] = (a[3] != b[3]) ? 1.0F : 0.0F;
1467 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001468 if (DEBUG_PROG) {
1469 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1470 result[0], result[1], result[2], result[3],
1471 a[0], a[1], a[2], a[3],
1472 b[0], b[1], b[2], b[3]);
1473 }
Briane80d9012007-02-23 16:53:24 -07001474 }
1475 break;
Brian Paulf6ead502008-11-07 08:51:31 -07001476 case OPCODE_SSG: /* set sign (-1, 0 or +1) */
1477 {
1478 GLfloat a[4], result[4];
1479 fetch_vector4(&inst->SrcReg[0], machine, a);
1480 result[0] = (GLfloat) ((a[0] > 0.0F) - (a[0] < 0.0F));
1481 result[1] = (GLfloat) ((a[1] > 0.0F) - (a[1] < 0.0F));
1482 result[2] = (GLfloat) ((a[2] > 0.0F) - (a[2] < 0.0F));
1483 result[3] = (GLfloat) ((a[3] > 0.0F) - (a[3] < 0.0F));
1484 store_vector4(inst, machine, result);
1485 }
1486 break;
Briane80d9012007-02-23 16:53:24 -07001487 case OPCODE_STR: /* set true, operands ignored */
1488 {
1489 static const GLfloat result[4] = { 1.0F, 1.0F, 1.0F, 1.0F };
1490 store_vector4(inst, machine, result);
1491 }
1492 break;
1493 case OPCODE_SUB:
1494 {
1495 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001496 fetch_vector4(&inst->SrcReg[0], machine, a);
1497 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001498 result[0] = a[0] - b[0];
1499 result[1] = a[1] - b[1];
1500 result[2] = a[2] - b[2];
1501 result[3] = a[3] - b[3];
1502 store_vector4(inst, machine, result);
1503 if (DEBUG_PROG) {
1504 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1505 result[0], result[1], result[2], result[3],
1506 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001507 }
Briane80d9012007-02-23 16:53:24 -07001508 }
1509 break;
1510 case OPCODE_SWZ: /* extended swizzle */
1511 {
1512 const struct prog_src_register *source = &inst->SrcReg[0];
Brian Paulf4361542008-11-11 10:47:10 -07001513 const GLfloat *src = get_src_register_pointer(source, machine);
Briane80d9012007-02-23 16:53:24 -07001514 GLfloat result[4];
1515 GLuint i;
1516 for (i = 0; i < 4; i++) {
1517 const GLuint swz = GET_SWZ(source->Swizzle, i);
1518 if (swz == SWIZZLE_ZERO)
1519 result[i] = 0.0;
1520 else if (swz == SWIZZLE_ONE)
1521 result[i] = 1.0;
Brian13e3b212007-02-22 16:09:40 -07001522 else {
Briane80d9012007-02-23 16:53:24 -07001523 ASSERT(swz >= 0);
1524 ASSERT(swz <= 3);
1525 result[i] = src[swz];
Brian13e3b212007-02-22 16:09:40 -07001526 }
Brian Paul7db7ff82009-04-14 22:14:30 -06001527 if (source->Negate & (1 << i))
Briane80d9012007-02-23 16:53:24 -07001528 result[i] = -result[i];
Brian13e3b212007-02-22 16:09:40 -07001529 }
Briane80d9012007-02-23 16:53:24 -07001530 store_vector4(inst, machine, result);
1531 }
1532 break;
1533 case OPCODE_TEX: /* Both ARB and NV frag prog */
Brian999b5562007-11-23 12:01:57 -07001534 /* Simple texel lookup */
Briane80d9012007-02-23 16:53:24 -07001535 {
Brian999b5562007-11-23 12:01:57 -07001536 GLfloat texcoord[4], color[4];
1537 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1538
1539 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1540
Briane80d9012007-02-23 16:53:24 -07001541 if (DEBUG_PROG) {
Brian999b5562007-11-23 12:01:57 -07001542 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
Briane80d9012007-02-23 16:53:24 -07001543 color[0], color[1], color[2], color[3],
1544 inst->TexSrcUnit,
Brian999b5562007-11-23 12:01:57 -07001545 texcoord[0], texcoord[1], texcoord[2], texcoord[3]);
Briane80d9012007-02-23 16:53:24 -07001546 }
1547 store_vector4(inst, machine, color);
1548 }
1549 break;
1550 case OPCODE_TXB: /* GL_ARB_fragment_program only */
1551 /* Texel lookup with LOD bias */
1552 {
Brian Paul1ab22502009-04-01 18:50:07 -06001553 const GLuint unit = machine->Samplers[inst->TexSrcUnit];
1554 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
Brian999b5562007-11-23 12:01:57 -07001555 GLfloat texcoord[4], color[4], lodBias;
1556
1557 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1558
1559 /* texcoord[3] is the bias to add to lambda */
1560 lodBias = texUnit->LodBias + texcoord[3];
1561 if (texUnit->_Current) {
1562 lodBias += texUnit->_Current->LodBias;
1563 }
1564
1565 fetch_texel(ctx, machine, inst, texcoord, lodBias, color);
1566
Briane80d9012007-02-23 16:53:24 -07001567 store_vector4(inst, machine, color);
1568 }
1569 break;
1570 case OPCODE_TXD: /* GL_NV_fragment_program only */
1571 /* Texture lookup w/ partial derivatives for LOD */
1572 {
1573 GLfloat texcoord[4], dtdx[4], dtdy[4], color[4];
Brian33eac562007-02-25 18:52:41 -07001574 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1575 fetch_vector4(&inst->SrcReg[1], machine, dtdx);
1576 fetch_vector4(&inst->SrcReg[2], machine, dtdy);
Briane80d9012007-02-23 16:53:24 -07001577 machine->FetchTexelDeriv(ctx, texcoord, dtdx, dtdy,
Brian999b5562007-11-23 12:01:57 -07001578 0.0, /* lodBias */
Briane80d9012007-02-23 16:53:24 -07001579 inst->TexSrcUnit, color);
1580 store_vector4(inst, machine, color);
1581 }
1582 break;
1583 case OPCODE_TXP: /* GL_ARB_fragment_program only */
1584 /* Texture lookup w/ projective divide */
1585 {
Brian999b5562007-11-23 12:01:57 -07001586 GLfloat texcoord[4], color[4];
1587
Brian33eac562007-02-25 18:52:41 -07001588 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
Briane80d9012007-02-23 16:53:24 -07001589 /* Not so sure about this test - if texcoord[3] is
1590 * zero, we'd probably be fine except for an ASSERT in
1591 * IROUND_POS() which gets triggered by the inf values created.
1592 */
1593 if (texcoord[3] != 0.0) {
1594 texcoord[0] /= texcoord[3];
1595 texcoord[1] /= texcoord[3];
1596 texcoord[2] /= texcoord[3];
1597 }
Brian999b5562007-11-23 12:01:57 -07001598
1599 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1600
Briane80d9012007-02-23 16:53:24 -07001601 store_vector4(inst, machine, color);
1602 }
1603 break;
1604 case OPCODE_TXP_NV: /* GL_NV_fragment_program only */
Brian999b5562007-11-23 12:01:57 -07001605 /* Texture lookup w/ projective divide, as above, but do not
1606 * do the divide by w if sampling from a cube map.
1607 */
Briane80d9012007-02-23 16:53:24 -07001608 {
Brian999b5562007-11-23 12:01:57 -07001609 GLfloat texcoord[4], color[4];
1610
Brian33eac562007-02-25 18:52:41 -07001611 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
Briane80d9012007-02-23 16:53:24 -07001612 if (inst->TexSrcTarget != TEXTURE_CUBE_INDEX &&
1613 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;
Brian Paul035c0cf2008-11-06 17:14:33 -07001624 case OPCODE_TRUNC: /* truncate toward zero */
1625 {
1626 GLfloat a[4], result[4];
1627 fetch_vector4(&inst->SrcReg[0], machine, a);
1628 result[0] = (GLfloat) (GLint) a[0];
1629 result[1] = (GLfloat) (GLint) a[1];
1630 result[2] = (GLfloat) (GLint) a[2];
1631 result[3] = (GLfloat) (GLint) a[3];
1632 store_vector4(inst, machine, result);
1633 }
1634 break;
Briane80d9012007-02-23 16:53:24 -07001635 case OPCODE_UP2H: /* unpack two 16-bit floats */
1636 {
1637 GLfloat a[4], result[4];
1638 const GLuint *rawBits = (const GLuint *) a;
1639 GLhalfNV hx, hy;
Brian33eac562007-02-25 18:52:41 -07001640 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001641 hx = rawBits[0] & 0xffff;
1642 hy = rawBits[0] >> 16;
1643 result[0] = result[2] = _mesa_half_to_float(hx);
1644 result[1] = result[3] = _mesa_half_to_float(hy);
1645 store_vector4(inst, machine, result);
1646 }
1647 break;
1648 case OPCODE_UP2US: /* unpack two GLushorts */
1649 {
1650 GLfloat a[4], result[4];
1651 const GLuint *rawBits = (const GLuint *) a;
1652 GLushort usx, usy;
Brian33eac562007-02-25 18:52:41 -07001653 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001654 usx = rawBits[0] & 0xffff;
1655 usy = rawBits[0] >> 16;
1656 result[0] = result[2] = usx * (1.0f / 65535.0f);
1657 result[1] = result[3] = usy * (1.0f / 65535.0f);
1658 store_vector4(inst, machine, result);
1659 }
1660 break;
1661 case OPCODE_UP4B: /* unpack four GLbytes */
1662 {
1663 GLfloat a[4], result[4];
1664 const GLuint *rawBits = (const GLuint *) a;
Brian33eac562007-02-25 18:52:41 -07001665 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001666 result[0] = (((rawBits[0] >> 0) & 0xff) - 128) / 127.0F;
1667 result[1] = (((rawBits[0] >> 8) & 0xff) - 128) / 127.0F;
1668 result[2] = (((rawBits[0] >> 16) & 0xff) - 128) / 127.0F;
1669 result[3] = (((rawBits[0] >> 24) & 0xff) - 128) / 127.0F;
1670 store_vector4(inst, machine, result);
1671 }
1672 break;
1673 case OPCODE_UP4UB: /* unpack four GLubytes */
1674 {
1675 GLfloat a[4], result[4];
1676 const GLuint *rawBits = (const GLuint *) a;
Brian33eac562007-02-25 18:52:41 -07001677 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001678 result[0] = ((rawBits[0] >> 0) & 0xff) / 255.0F;
1679 result[1] = ((rawBits[0] >> 8) & 0xff) / 255.0F;
1680 result[2] = ((rawBits[0] >> 16) & 0xff) / 255.0F;
1681 result[3] = ((rawBits[0] >> 24) & 0xff) / 255.0F;
1682 store_vector4(inst, machine, result);
1683 }
1684 break;
Brian Paul37eef7b2008-11-07 09:33:55 -07001685 case OPCODE_XOR: /* bitwise XOR */
1686 {
1687 GLuint a[4], b[4], result[4];
1688 fetch_vector4ui(&inst->SrcReg[0], machine, a);
1689 fetch_vector4ui(&inst->SrcReg[1], machine, b);
1690 result[0] = a[0] ^ b[0];
1691 result[1] = a[1] ^ b[1];
1692 result[2] = a[2] ^ b[2];
1693 result[3] = a[3] ^ b[3];
1694 store_vector4ui(inst, machine, result);
1695 }
1696 break;
Briane80d9012007-02-23 16:53:24 -07001697 case OPCODE_XPD: /* cross product */
1698 {
1699 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001700 fetch_vector4(&inst->SrcReg[0], machine, a);
1701 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001702 result[0] = a[1] * b[2] - a[2] * b[1];
1703 result[1] = a[2] * b[0] - a[0] * b[2];
1704 result[2] = a[0] * b[1] - a[1] * b[0];
1705 result[3] = 1.0;
1706 store_vector4(inst, machine, result);
Brian9637c962007-03-07 17:40:57 -07001707 if (DEBUG_PROG) {
1708 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1709 result[0], result[1], result[2], result[3],
1710 a[0], a[1], a[2], b[0], b[1], b[2]);
1711 }
Briane80d9012007-02-23 16:53:24 -07001712 }
1713 break;
1714 case OPCODE_X2D: /* 2-D matrix transform */
1715 {
1716 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001717 fetch_vector4(&inst->SrcReg[0], machine, a);
1718 fetch_vector4(&inst->SrcReg[1], machine, b);
1719 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -07001720 result[0] = a[0] + b[0] * c[0] + b[1] * c[1];
1721 result[1] = a[1] + b[0] * c[2] + b[1] * c[3];
1722 result[2] = a[2] + b[0] * c[0] + b[1] * c[1];
1723 result[3] = a[3] + b[0] * c[2] + b[1] * c[3];
1724 store_vector4(inst, machine, result);
1725 }
1726 break;
1727 case OPCODE_PRINT:
1728 {
1729 if (inst->SrcReg[0].File != -1) {
1730 GLfloat a[4];
Brian33eac562007-02-25 18:52:41 -07001731 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001732 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst->Data,
1733 a[0], a[1], a[2], a[3]);
1734 }
1735 else {
1736 _mesa_printf("%s\n", (const char *) inst->Data);
1737 }
1738 }
1739 break;
1740 case OPCODE_END:
1741 return GL_TRUE;
1742 default:
Alan Hourihane936dba12008-04-22 20:28:35 +01001743 _mesa_problem(ctx, "Bad opcode %d in _mesa_execute_program",
Briane80d9012007-02-23 16:53:24 -07001744 inst->Opcode);
1745 return GL_TRUE; /* return value doesn't matter */
Brian13e3b212007-02-22 16:09:40 -07001746 }
Briane80d9012007-02-23 16:53:24 -07001747
Briancfd00112007-02-25 18:30:45 -07001748 numExec++;
1749 if (numExec > maxExec) {
Brian13e3b212007-02-22 16:09:40 -07001750 _mesa_problem(ctx, "Infinite loop detected in fragment program");
1751 return GL_TRUE;
Brian13e3b212007-02-22 16:09:40 -07001752 }
Briane80d9012007-02-23 16:53:24 -07001753
1754 } /* for pc */
Brian13e3b212007-02-22 16:09:40 -07001755
Brian13e3b212007-02-22 16:09:40 -07001756 return GL_TRUE;
1757}