blob: 1d97a077f525fc08eafbfa165f4f403f6e114667 [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"
Vinson Lee3fdd9fa2010-07-30 00:41:08 -070040#include "main/macros.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/**
Brian Paulb4ad7c22010-07-29 08:23:59 -060084 * Return TRUE for +0 and other positive values, FALSE otherwise.
85 * Used for RCC opcode.
86 */
87static INLINE GLboolean
88positive(float x)
89{
90 fi_type fi;
91 fi.f = x;
92 if (fi.i & 0x80000000)
93 return GL_FALSE;
94 return GL_TRUE;
95}
96
97
98
99/**
Brian13e3b212007-02-22 16:09:40 -0700100 * Return a pointer to the 4-element float vector specified by the given
101 * source register.
102 */
103static INLINE const GLfloat *
Brian Paulf4361542008-11-11 10:47:10 -0700104get_src_register_pointer(const struct prog_src_register *source,
105 const struct gl_program_machine *machine)
Brian13e3b212007-02-22 16:09:40 -0700106{
Brian Paulf4361542008-11-11 10:47:10 -0700107 const struct gl_program *prog = machine->CurProgram;
108 GLint reg = source->Index;
109
Brianf183a2d2007-02-23 17:14:30 -0700110 if (source->RelAddr) {
Brian Paulf4361542008-11-11 10:47:10 -0700111 /* add address register value to src index/offset */
112 reg += machine->AddressReg[0][0];
113 if (reg < 0) {
114 return ZeroVec;
Brianf183a2d2007-02-23 17:14:30 -0700115 }
116 }
117
Brian13e3b212007-02-22 16:09:40 -0700118 switch (source->File) {
119 case PROGRAM_TEMPORARY:
Brian Paulf4361542008-11-11 10:47:10 -0700120 if (reg >= MAX_PROGRAM_TEMPS)
121 return ZeroVec;
122 return machine->Temporaries[reg];
Brian13e3b212007-02-22 16:09:40 -0700123
124 case PROGRAM_INPUT:
Brian Paulf4361542008-11-11 10:47:10 -0700125 if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
126 if (reg >= VERT_ATTRIB_MAX)
127 return ZeroVec;
128 return machine->VertAttribs[reg];
Brian13e3b212007-02-22 16:09:40 -0700129 }
130 else {
Brian Paulf4361542008-11-11 10:47:10 -0700131 if (reg >= FRAG_ATTRIB_MAX)
132 return ZeroVec;
133 return machine->Attribs[reg][machine->CurElement];
Brian13e3b212007-02-22 16:09:40 -0700134 }
135
136 case PROGRAM_OUTPUT:
Brian Paulf4361542008-11-11 10:47:10 -0700137 if (reg >= MAX_PROGRAM_OUTPUTS)
138 return ZeroVec;
139 return machine->Outputs[reg];
Brian13e3b212007-02-22 16:09:40 -0700140
141 case PROGRAM_LOCAL_PARAM:
Brian Paulf4361542008-11-11 10:47:10 -0700142 if (reg >= MAX_PROGRAM_LOCAL_PARAMS)
143 return ZeroVec;
144 return machine->CurProgram->LocalParams[reg];
Brian13e3b212007-02-22 16:09:40 -0700145
146 case PROGRAM_ENV_PARAM:
Brian Paulf4361542008-11-11 10:47:10 -0700147 if (reg >= MAX_PROGRAM_ENV_PARAMS)
148 return ZeroVec;
149 return machine->EnvParams[reg];
Brian13e3b212007-02-22 16:09:40 -0700150
151 case PROGRAM_STATE_VAR:
152 /* Fallthrough */
153 case PROGRAM_CONSTANT:
154 /* Fallthrough */
155 case PROGRAM_UNIFORM:
156 /* Fallthrough */
157 case PROGRAM_NAMED_PARAM:
Brian Paulf4361542008-11-11 10:47:10 -0700158 if (reg >= (GLint) prog->Parameters->NumParameters)
159 return ZeroVec;
160 return prog->Parameters->ParameterValues[reg];
Brian13e3b212007-02-22 16:09:40 -0700161
162 default:
Brian33eac562007-02-25 18:52:41 -0700163 _mesa_problem(NULL,
Brian Paulf4361542008-11-11 10:47:10 -0700164 "Invalid src register file %d in get_src_register_pointer()",
165 source->File);
Brian13e3b212007-02-22 16:09:40 -0700166 return NULL;
167 }
168}
169
170
Brian Paulf4361542008-11-11 10:47:10 -0700171/**
172 * Return a pointer to the 4-element float vector specified by the given
173 * destination register.
174 */
175static INLINE GLfloat *
176get_dst_register_pointer(const struct prog_dst_register *dest,
177 struct gl_program_machine *machine)
178{
179 static GLfloat dummyReg[4];
180 GLint reg = dest->Index;
181
182 if (dest->RelAddr) {
183 /* add address register value to src index/offset */
184 reg += machine->AddressReg[0][0];
185 if (reg < 0) {
186 return dummyReg;
187 }
188 }
189
190 switch (dest->File) {
191 case PROGRAM_TEMPORARY:
192 if (reg >= MAX_PROGRAM_TEMPS)
193 return dummyReg;
194 return machine->Temporaries[reg];
195
196 case PROGRAM_OUTPUT:
197 if (reg >= MAX_PROGRAM_OUTPUTS)
198 return dummyReg;
199 return machine->Outputs[reg];
200
201 case PROGRAM_WRITE_ONLY:
202 return dummyReg;
203
204 default:
205 _mesa_problem(NULL,
206 "Invalid dest register file %d in get_dst_register_pointer()",
207 dest->File);
208 return NULL;
209 }
210}
211
212
213
Brian13e3b212007-02-22 16:09:40 -0700214/**
215 * Fetch a 4-element float vector from the given source register.
216 * Apply swizzling and negating as needed.
217 */
218static void
Brian33eac562007-02-25 18:52:41 -0700219fetch_vector4(const struct prog_src_register *source,
Briane80d9012007-02-23 16:53:24 -0700220 const struct gl_program_machine *machine, GLfloat result[4])
Brian13e3b212007-02-22 16:09:40 -0700221{
Brian Paulf4361542008-11-11 10:47:10 -0700222 const GLfloat *src = get_src_register_pointer(source, machine);
Brian13e3b212007-02-22 16:09:40 -0700223 ASSERT(src);
224
225 if (source->Swizzle == SWIZZLE_NOOP) {
226 /* no swizzling */
227 COPY_4V(result, src);
228 }
229 else {
230 ASSERT(GET_SWZ(source->Swizzle, 0) <= 3);
231 ASSERT(GET_SWZ(source->Swizzle, 1) <= 3);
232 ASSERT(GET_SWZ(source->Swizzle, 2) <= 3);
233 ASSERT(GET_SWZ(source->Swizzle, 3) <= 3);
234 result[0] = src[GET_SWZ(source->Swizzle, 0)];
235 result[1] = src[GET_SWZ(source->Swizzle, 1)];
236 result[2] = src[GET_SWZ(source->Swizzle, 2)];
237 result[3] = src[GET_SWZ(source->Swizzle, 3)];
238 }
239
Brian13e3b212007-02-22 16:09:40 -0700240 if (source->Abs) {
241 result[0] = FABSF(result[0]);
242 result[1] = FABSF(result[1]);
243 result[2] = FABSF(result[2]);
244 result[3] = FABSF(result[3]);
245 }
Brian Paul7db7ff82009-04-14 22:14:30 -0600246 if (source->Negate) {
247 ASSERT(source->Negate == NEGATE_XYZW);
Brian13e3b212007-02-22 16:09:40 -0700248 result[0] = -result[0];
249 result[1] = -result[1];
250 result[2] = -result[2];
251 result[3] = -result[3];
252 }
Brian Paul92009542009-06-03 15:43:53 -0600253
254#ifdef NAN_CHECK
255 assert(!IS_INF_OR_NAN(result[0]));
256 assert(!IS_INF_OR_NAN(result[0]));
257 assert(!IS_INF_OR_NAN(result[0]));
258 assert(!IS_INF_OR_NAN(result[0]));
259#endif
Brian13e3b212007-02-22 16:09:40 -0700260}
261
Brian62da6a12007-05-02 18:44:34 -0600262
Brian13e3b212007-02-22 16:09:40 -0700263/**
Brian Paul37eef7b2008-11-07 09:33:55 -0700264 * Fetch a 4-element uint vector from the given source register.
265 * Apply swizzling but not negation/abs.
266 */
267static void
268fetch_vector4ui(const struct prog_src_register *source,
269 const struct gl_program_machine *machine, GLuint result[4])
270{
Brian Paulf4361542008-11-11 10:47:10 -0700271 const GLuint *src = (GLuint *) get_src_register_pointer(source, machine);
Brian Paul37eef7b2008-11-07 09:33:55 -0700272 ASSERT(src);
273
274 if (source->Swizzle == SWIZZLE_NOOP) {
275 /* no swizzling */
276 COPY_4V(result, src);
277 }
278 else {
279 ASSERT(GET_SWZ(source->Swizzle, 0) <= 3);
280 ASSERT(GET_SWZ(source->Swizzle, 1) <= 3);
281 ASSERT(GET_SWZ(source->Swizzle, 2) <= 3);
282 ASSERT(GET_SWZ(source->Swizzle, 3) <= 3);
283 result[0] = src[GET_SWZ(source->Swizzle, 0)];
284 result[1] = src[GET_SWZ(source->Swizzle, 1)];
285 result[2] = src[GET_SWZ(source->Swizzle, 2)];
286 result[3] = src[GET_SWZ(source->Swizzle, 3)];
287 }
288
Brian Paul7db7ff82009-04-14 22:14:30 -0600289 /* Note: no Negate or Abs here */
Brian Paul37eef7b2008-11-07 09:33:55 -0700290}
291
292
293
294/**
Brian62da6a12007-05-02 18:44:34 -0600295 * Fetch the derivative with respect to X or Y for the given register.
296 * XXX this currently only works for fragment program input attribs.
Brian13e3b212007-02-22 16:09:40 -0700297 */
Brian62da6a12007-05-02 18:44:34 -0600298static void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400299fetch_vector4_deriv(struct gl_context * ctx,
Briane80d9012007-02-23 16:53:24 -0700300 const struct prog_src_register *source,
Brian62da6a12007-05-02 18:44:34 -0600301 const struct gl_program_machine *machine,
302 char xOrY, GLfloat result[4])
Brian13e3b212007-02-22 16:09:40 -0700303{
Brian Paul37eef7b2008-11-07 09:33:55 -0700304 if (source->File == PROGRAM_INPUT &&
305 source->Index < (GLint) machine->NumDeriv) {
Brian62da6a12007-05-02 18:44:34 -0600306 const GLint col = machine->CurElement;
307 const GLfloat w = machine->Attribs[FRAG_ATTRIB_WPOS][col][3];
308 const GLfloat invQ = 1.0f / w;
309 GLfloat deriv[4];
Brian13e3b212007-02-22 16:09:40 -0700310
Brian13e3b212007-02-22 16:09:40 -0700311 if (xOrY == 'X') {
Brian62da6a12007-05-02 18:44:34 -0600312 deriv[0] = machine->DerivX[source->Index][0] * invQ;
313 deriv[1] = machine->DerivX[source->Index][1] * invQ;
314 deriv[2] = machine->DerivX[source->Index][2] * invQ;
315 deriv[3] = machine->DerivX[source->Index][3] * invQ;
Brian13e3b212007-02-22 16:09:40 -0700316 }
317 else {
Brian62da6a12007-05-02 18:44:34 -0600318 deriv[0] = machine->DerivY[source->Index][0] * invQ;
319 deriv[1] = machine->DerivY[source->Index][1] * invQ;
320 deriv[2] = machine->DerivY[source->Index][2] * invQ;
321 deriv[3] = machine->DerivY[source->Index][3] * invQ;
Brian13e3b212007-02-22 16:09:40 -0700322 }
Brian13e3b212007-02-22 16:09:40 -0700323
Brian62da6a12007-05-02 18:44:34 -0600324 result[0] = deriv[GET_SWZ(source->Swizzle, 0)];
325 result[1] = deriv[GET_SWZ(source->Swizzle, 1)];
326 result[2] = deriv[GET_SWZ(source->Swizzle, 2)];
327 result[3] = deriv[GET_SWZ(source->Swizzle, 3)];
328
Brian62da6a12007-05-02 18:44:34 -0600329 if (source->Abs) {
330 result[0] = FABSF(result[0]);
331 result[1] = FABSF(result[1]);
332 result[2] = FABSF(result[2]);
333 result[3] = FABSF(result[3]);
334 }
Brian Paul7db7ff82009-04-14 22:14:30 -0600335 if (source->Negate) {
336 ASSERT(source->Negate == NEGATE_XYZW);
Brian62da6a12007-05-02 18:44:34 -0600337 result[0] = -result[0];
338 result[1] = -result[1];
339 result[2] = -result[2];
340 result[3] = -result[3];
341 }
Brian13e3b212007-02-22 16:09:40 -0700342 }
Brian62da6a12007-05-02 18:44:34 -0600343 else {
344 ASSIGN_4V(result, 0.0, 0.0, 0.0, 0.0);
Brian13e3b212007-02-22 16:09:40 -0700345 }
Brian13e3b212007-02-22 16:09:40 -0700346}
Brian13e3b212007-02-22 16:09:40 -0700347
348
349/**
350 * As above, but only return result[0] element.
351 */
352static void
Brian33eac562007-02-25 18:52:41 -0700353fetch_vector1(const struct prog_src_register *source,
Briane80d9012007-02-23 16:53:24 -0700354 const struct gl_program_machine *machine, GLfloat result[4])
Brian13e3b212007-02-22 16:09:40 -0700355{
Brian Paulf4361542008-11-11 10:47:10 -0700356 const GLfloat *src = get_src_register_pointer(source, machine);
Brian13e3b212007-02-22 16:09:40 -0700357 ASSERT(src);
358
359 result[0] = src[GET_SWZ(source->Swizzle, 0)];
360
Brian13e3b212007-02-22 16:09:40 -0700361 if (source->Abs) {
362 result[0] = FABSF(result[0]);
363 }
Brian Paul7db7ff82009-04-14 22:14:30 -0600364 if (source->Negate) {
Brian13e3b212007-02-22 16:09:40 -0700365 result[0] = -result[0];
366 }
367}
368
369
Brian Paul8d1a01d2010-01-22 15:36:28 -0700370static GLuint
371fetch_vector1ui(const struct prog_src_register *source,
372 const struct gl_program_machine *machine)
373{
374 const GLuint *src = (GLuint *) get_src_register_pointer(source, machine);
Brian Paul984b72a2010-02-19 12:41:21 -0700375 return src[GET_SWZ(source->Swizzle, 0)];
Brian Paul8d1a01d2010-01-22 15:36:28 -0700376}
377
378
Brian13e3b212007-02-22 16:09:40 -0700379/**
Brian999b5562007-11-23 12:01:57 -0700380 * Fetch texel from texture. Use partial derivatives when possible.
381 */
382static INLINE void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400383fetch_texel(struct gl_context *ctx,
Brian999b5562007-11-23 12:01:57 -0700384 const struct gl_program_machine *machine,
385 const struct prog_instruction *inst,
386 const GLfloat texcoord[4], GLfloat lodBias,
387 GLfloat color[4])
388{
Brian Paulade50832008-05-14 16:09:46 -0600389 const GLuint unit = machine->Samplers[inst->TexSrcUnit];
390
Brian999b5562007-11-23 12:01:57 -0700391 /* Note: we only have the right derivatives for fragment input attribs.
392 */
393 if (machine->NumDeriv > 0 &&
394 inst->SrcReg[0].File == PROGRAM_INPUT &&
395 inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0 + inst->TexSrcUnit) {
396 /* simple texture fetch for which we should have derivatives */
397 GLuint attr = inst->SrcReg[0].Index;
398 machine->FetchTexelDeriv(ctx, texcoord,
399 machine->DerivX[attr],
400 machine->DerivY[attr],
Brian Paulade50832008-05-14 16:09:46 -0600401 lodBias, unit, color);
Brian999b5562007-11-23 12:01:57 -0700402 }
403 else {
Brian Paulade50832008-05-14 16:09:46 -0600404 machine->FetchTexelLod(ctx, texcoord, lodBias, unit, color);
Brian999b5562007-11-23 12:01:57 -0700405 }
406}
407
408
409/**
Brian13e3b212007-02-22 16:09:40 -0700410 * Test value against zero and return GT, LT, EQ or UN if NaN.
411 */
412static INLINE GLuint
Briane80d9012007-02-23 16:53:24 -0700413generate_cc(float value)
Brian13e3b212007-02-22 16:09:40 -0700414{
415 if (value != value)
Briane80d9012007-02-23 16:53:24 -0700416 return COND_UN; /* NaN */
Brian13e3b212007-02-22 16:09:40 -0700417 if (value > 0.0F)
418 return COND_GT;
419 if (value < 0.0F)
420 return COND_LT;
421 return COND_EQ;
422}
423
424
425/**
426 * Test if the ccMaskRule is satisfied by the given condition code.
427 * Used to mask destination writes according to the current condition code.
428 */
429static INLINE GLboolean
430test_cc(GLuint condCode, GLuint ccMaskRule)
431{
432 switch (ccMaskRule) {
433 case COND_EQ: return (condCode == COND_EQ);
434 case COND_NE: return (condCode != COND_EQ);
435 case COND_LT: return (condCode == COND_LT);
436 case COND_GE: return (condCode == COND_GT || condCode == COND_EQ);
437 case COND_LE: return (condCode == COND_LT || condCode == COND_EQ);
438 case COND_GT: return (condCode == COND_GT);
439 case COND_TR: return GL_TRUE;
440 case COND_FL: return GL_FALSE;
441 default: return GL_TRUE;
442 }
443}
444
445
446/**
447 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
448 * or GL_FALSE to indicate result.
449 */
450static INLINE GLboolean
451eval_condition(const struct gl_program_machine *machine,
452 const struct prog_instruction *inst)
453{
454 const GLuint swizzle = inst->DstReg.CondSwizzle;
455 const GLuint condMask = inst->DstReg.CondMask;
456 if (test_cc(machine->CondCodes[GET_SWZ(swizzle, 0)], condMask) ||
457 test_cc(machine->CondCodes[GET_SWZ(swizzle, 1)], condMask) ||
458 test_cc(machine->CondCodes[GET_SWZ(swizzle, 2)], condMask) ||
459 test_cc(machine->CondCodes[GET_SWZ(swizzle, 3)], condMask)) {
460 return GL_TRUE;
461 }
462 else {
463 return GL_FALSE;
464 }
465}
466
467
468
469/**
470 * Store 4 floats into a register. Observe the instructions saturate and
471 * set-condition-code flags.
472 */
473static void
Briane80d9012007-02-23 16:53:24 -0700474store_vector4(const struct prog_instruction *inst,
475 struct gl_program_machine *machine, const GLfloat value[4])
Brian13e3b212007-02-22 16:09:40 -0700476{
Brian Paulf4361542008-11-11 10:47:10 -0700477 const struct prog_dst_register *dstReg = &(inst->DstReg);
Brian13e3b212007-02-22 16:09:40 -0700478 const GLboolean clamp = inst->SaturateMode == SATURATE_ZERO_ONE;
Brian Paulf4361542008-11-11 10:47:10 -0700479 GLuint writeMask = dstReg->WriteMask;
Brian13e3b212007-02-22 16:09:40 -0700480 GLfloat clampedValue[4];
Brian Paulf4361542008-11-11 10:47:10 -0700481 GLfloat *dst = get_dst_register_pointer(dstReg, machine);
Brian13e3b212007-02-22 16:09:40 -0700482
483#if 0
484 if (value[0] > 1.0e10 ||
485 IS_INF_OR_NAN(value[0]) ||
486 IS_INF_OR_NAN(value[1]) ||
Briane80d9012007-02-23 16:53:24 -0700487 IS_INF_OR_NAN(value[2]) || IS_INF_OR_NAN(value[3]))
Brian13e3b212007-02-22 16:09:40 -0700488 printf("store %g %g %g %g\n", value[0], value[1], value[2], value[3]);
489#endif
490
491 if (clamp) {
492 clampedValue[0] = CLAMP(value[0], 0.0F, 1.0F);
493 clampedValue[1] = CLAMP(value[1], 0.0F, 1.0F);
494 clampedValue[2] = CLAMP(value[2], 0.0F, 1.0F);
495 clampedValue[3] = CLAMP(value[3], 0.0F, 1.0F);
496 value = clampedValue;
497 }
498
Brian Paulf4361542008-11-11 10:47:10 -0700499 if (dstReg->CondMask != COND_TR) {
Brian13e3b212007-02-22 16:09:40 -0700500 /* condition codes may turn off some writes */
501 if (writeMask & WRITEMASK_X) {
Brian Paulf4361542008-11-11 10:47:10 -0700502 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 0)],
503 dstReg->CondMask))
Brian13e3b212007-02-22 16:09:40 -0700504 writeMask &= ~WRITEMASK_X;
505 }
506 if (writeMask & WRITEMASK_Y) {
Brian Paulf4361542008-11-11 10:47:10 -0700507 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 1)],
508 dstReg->CondMask))
Brian13e3b212007-02-22 16:09:40 -0700509 writeMask &= ~WRITEMASK_Y;
510 }
511 if (writeMask & WRITEMASK_Z) {
Brian Paulf4361542008-11-11 10:47:10 -0700512 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 2)],
513 dstReg->CondMask))
Brian13e3b212007-02-22 16:09:40 -0700514 writeMask &= ~WRITEMASK_Z;
515 }
516 if (writeMask & WRITEMASK_W) {
Brian Paulf4361542008-11-11 10:47:10 -0700517 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 3)],
518 dstReg->CondMask))
Brian13e3b212007-02-22 16:09:40 -0700519 writeMask &= ~WRITEMASK_W;
520 }
521 }
522
Brian Paul92009542009-06-03 15:43:53 -0600523#ifdef NAN_CHECK
524 assert(!IS_INF_OR_NAN(value[0]));
525 assert(!IS_INF_OR_NAN(value[0]));
526 assert(!IS_INF_OR_NAN(value[0]));
527 assert(!IS_INF_OR_NAN(value[0]));
528#endif
529
Brian13e3b212007-02-22 16:09:40 -0700530 if (writeMask & WRITEMASK_X)
Brian Paulf4361542008-11-11 10:47:10 -0700531 dst[0] = value[0];
Brian13e3b212007-02-22 16:09:40 -0700532 if (writeMask & WRITEMASK_Y)
Brian Paulf4361542008-11-11 10:47:10 -0700533 dst[1] = value[1];
Brian13e3b212007-02-22 16:09:40 -0700534 if (writeMask & WRITEMASK_Z)
Brian Paulf4361542008-11-11 10:47:10 -0700535 dst[2] = value[2];
Brian13e3b212007-02-22 16:09:40 -0700536 if (writeMask & WRITEMASK_W)
Brian Paulf4361542008-11-11 10:47:10 -0700537 dst[3] = value[3];
Brian13e3b212007-02-22 16:09:40 -0700538
539 if (inst->CondUpdate) {
540 if (writeMask & WRITEMASK_X)
541 machine->CondCodes[0] = generate_cc(value[0]);
542 if (writeMask & WRITEMASK_Y)
543 machine->CondCodes[1] = generate_cc(value[1]);
544 if (writeMask & WRITEMASK_Z)
545 machine->CondCodes[2] = generate_cc(value[2]);
546 if (writeMask & WRITEMASK_W)
547 machine->CondCodes[3] = generate_cc(value[3]);
Briana01616e2007-03-28 11:01:28 -0600548#if DEBUG_PROG
549 printf("CondCodes=(%s,%s,%s,%s) for:\n",
550 _mesa_condcode_string(machine->CondCodes[0]),
551 _mesa_condcode_string(machine->CondCodes[1]),
552 _mesa_condcode_string(machine->CondCodes[2]),
553 _mesa_condcode_string(machine->CondCodes[3]));
554#endif
Brian13e3b212007-02-22 16:09:40 -0700555 }
556}
557
558
Brian13e3b212007-02-22 16:09:40 -0700559/**
Brian Paul37eef7b2008-11-07 09:33:55 -0700560 * Store 4 uints into a register. Observe the set-condition-code flags.
561 */
562static void
563store_vector4ui(const struct prog_instruction *inst,
564 struct gl_program_machine *machine, const GLuint value[4])
565{
Brian Paulf4361542008-11-11 10:47:10 -0700566 const struct prog_dst_register *dstReg = &(inst->DstReg);
567 GLuint writeMask = dstReg->WriteMask;
568 GLuint *dst = (GLuint *) get_dst_register_pointer(dstReg, machine);
Brian Paul37eef7b2008-11-07 09:33:55 -0700569
Brian Paulf4361542008-11-11 10:47:10 -0700570 if (dstReg->CondMask != COND_TR) {
Brian Paul37eef7b2008-11-07 09:33:55 -0700571 /* condition codes may turn off some writes */
572 if (writeMask & WRITEMASK_X) {
Brian Paulf4361542008-11-11 10:47:10 -0700573 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 0)],
574 dstReg->CondMask))
Brian Paul37eef7b2008-11-07 09:33:55 -0700575 writeMask &= ~WRITEMASK_X;
576 }
577 if (writeMask & WRITEMASK_Y) {
Brian Paulf4361542008-11-11 10:47:10 -0700578 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 1)],
579 dstReg->CondMask))
Brian Paul37eef7b2008-11-07 09:33:55 -0700580 writeMask &= ~WRITEMASK_Y;
581 }
582 if (writeMask & WRITEMASK_Z) {
Brian Paulf4361542008-11-11 10:47:10 -0700583 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 2)],
584 dstReg->CondMask))
Brian Paul37eef7b2008-11-07 09:33:55 -0700585 writeMask &= ~WRITEMASK_Z;
586 }
587 if (writeMask & WRITEMASK_W) {
Brian Paulf4361542008-11-11 10:47:10 -0700588 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 3)],
589 dstReg->CondMask))
Brian Paul37eef7b2008-11-07 09:33:55 -0700590 writeMask &= ~WRITEMASK_W;
591 }
592 }
593
594 if (writeMask & WRITEMASK_X)
Brian Paulf4361542008-11-11 10:47:10 -0700595 dst[0] = value[0];
Brian Paul37eef7b2008-11-07 09:33:55 -0700596 if (writeMask & WRITEMASK_Y)
Brian Paulf4361542008-11-11 10:47:10 -0700597 dst[1] = value[1];
Brian Paul37eef7b2008-11-07 09:33:55 -0700598 if (writeMask & WRITEMASK_Z)
Brian Paulf4361542008-11-11 10:47:10 -0700599 dst[2] = value[2];
Brian Paul37eef7b2008-11-07 09:33:55 -0700600 if (writeMask & WRITEMASK_W)
Brian Paulf4361542008-11-11 10:47:10 -0700601 dst[3] = value[3];
Brian Paul37eef7b2008-11-07 09:33:55 -0700602
603 if (inst->CondUpdate) {
604 if (writeMask & WRITEMASK_X)
Karl Schultzb30898f2010-02-13 17:31:58 -0700605 machine->CondCodes[0] = generate_cc((float)value[0]);
Brian Paul37eef7b2008-11-07 09:33:55 -0700606 if (writeMask & WRITEMASK_Y)
Karl Schultzb30898f2010-02-13 17:31:58 -0700607 machine->CondCodes[1] = generate_cc((float)value[1]);
Brian Paul37eef7b2008-11-07 09:33:55 -0700608 if (writeMask & WRITEMASK_Z)
Karl Schultzb30898f2010-02-13 17:31:58 -0700609 machine->CondCodes[2] = generate_cc((float)value[2]);
Brian Paul37eef7b2008-11-07 09:33:55 -0700610 if (writeMask & WRITEMASK_W)
Karl Schultzb30898f2010-02-13 17:31:58 -0700611 machine->CondCodes[3] = generate_cc((float)value[3]);
Brian Paul37eef7b2008-11-07 09:33:55 -0700612#if DEBUG_PROG
613 printf("CondCodes=(%s,%s,%s,%s) for:\n",
614 _mesa_condcode_string(machine->CondCodes[0]),
615 _mesa_condcode_string(machine->CondCodes[1]),
616 _mesa_condcode_string(machine->CondCodes[2]),
617 _mesa_condcode_string(machine->CondCodes[3]));
618#endif
619 }
620}
621
622
623
624/**
Brian13e3b212007-02-22 16:09:40 -0700625 * Execute the given vertex/fragment program.
626 *
Brian3c1c9992007-02-25 19:11:44 -0700627 * \param ctx rendering context
628 * \param program the program to execute
629 * \param machine machine state (must be initialized)
Brian13e3b212007-02-22 16:09:40 -0700630 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
631 */
632GLboolean
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400633_mesa_execute_program(struct gl_context * ctx,
Brian8b34b7d2007-02-25 18:26:50 -0700634 const struct gl_program *program,
Brian085d7d52007-02-25 18:23:37 -0700635 struct gl_program_machine *machine)
Brian13e3b212007-02-22 16:09:40 -0700636{
Brian8b34b7d2007-02-25 18:26:50 -0700637 const GLuint numInst = program->NumInstructions;
Briancfd00112007-02-25 18:30:45 -0700638 const GLuint maxExec = 10000;
José Fonseca452a5922008-05-31 18:14:09 +0900639 GLuint pc, numExec = 0;
Brian13e3b212007-02-22 16:09:40 -0700640
641 machine->CurProgram = program;
642
643 if (DEBUG_PROG) {
644 printf("execute program %u --------------------\n", program->Id);
645 }
646
Brian33eac562007-02-25 18:52:41 -0700647 if (program->Target == GL_VERTEX_PROGRAM_ARB) {
648 machine->EnvParams = ctx->VertexProgram.Parameters;
649 }
650 else {
651 machine->EnvParams = ctx->FragmentProgram.Parameters;
652 }
653
Brian8b34b7d2007-02-25 18:26:50 -0700654 for (pc = 0; pc < numInst; pc++) {
Brian13e3b212007-02-22 16:09:40 -0700655 const struct prog_instruction *inst = program->Instructions + pc;
656
Brian13e3b212007-02-22 16:09:40 -0700657 if (DEBUG_PROG) {
658 _mesa_print_instruction(inst);
659 }
660
661 switch (inst->Opcode) {
Briane80d9012007-02-23 16:53:24 -0700662 case OPCODE_ABS:
663 {
664 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700665 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700666 result[0] = FABSF(a[0]);
667 result[1] = FABSF(a[1]);
668 result[2] = FABSF(a[2]);
669 result[3] = FABSF(a[3]);
670 store_vector4(inst, machine, result);
671 }
672 break;
673 case OPCODE_ADD:
674 {
675 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700676 fetch_vector4(&inst->SrcReg[0], machine, a);
677 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700678 result[0] = a[0] + b[0];
679 result[1] = a[1] + b[1];
680 result[2] = a[2] + b[2];
681 result[3] = a[3] + b[3];
682 store_vector4(inst, machine, result);
683 if (DEBUG_PROG) {
684 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
685 result[0], result[1], result[2], result[3],
686 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -0700687 }
Briane80d9012007-02-23 16:53:24 -0700688 }
689 break;
Brian Paul37eef7b2008-11-07 09:33:55 -0700690 case OPCODE_AND: /* bitwise AND */
691 {
692 GLuint a[4], b[4], result[4];
693 fetch_vector4ui(&inst->SrcReg[0], machine, a);
694 fetch_vector4ui(&inst->SrcReg[1], machine, b);
695 result[0] = a[0] & b[0];
696 result[1] = a[1] & b[1];
697 result[2] = a[2] & b[2];
698 result[3] = a[3] & b[3];
699 store_vector4ui(inst, machine, result);
700 }
701 break;
Brianf183a2d2007-02-23 17:14:30 -0700702 case OPCODE_ARL:
703 {
704 GLfloat t[4];
Brian33eac562007-02-25 18:52:41 -0700705 fetch_vector4(&inst->SrcReg[0], machine, t);
Brian Paula9475cc2008-12-12 18:03:48 -0700706 machine->AddressReg[0][0] = IFLOOR(t[0]);
Brian Paula636f5b2010-02-05 14:58:46 -0700707 if (DEBUG_PROG) {
708 printf("ARL %d\n", machine->AddressReg[0][0]);
709 }
Brianf183a2d2007-02-23 17:14:30 -0700710 }
711 break;
Briane80d9012007-02-23 16:53:24 -0700712 case OPCODE_BGNLOOP:
713 /* no-op */
Brian Paule8ea2d22009-12-22 12:57:31 -0700714 ASSERT(program->Instructions[inst->BranchTarget].Opcode
715 == OPCODE_ENDLOOP);
Briane80d9012007-02-23 16:53:24 -0700716 break;
717 case OPCODE_ENDLOOP:
718 /* subtract 1 here since pc is incremented by for(pc) loop */
Brian Paule8ea2d22009-12-22 12:57:31 -0700719 ASSERT(program->Instructions[inst->BranchTarget].Opcode
720 == OPCODE_BGNLOOP);
Briane80d9012007-02-23 16:53:24 -0700721 pc = inst->BranchTarget - 1; /* go to matching BNGLOOP */
722 break;
723 case OPCODE_BGNSUB: /* begin subroutine */
724 break;
725 case OPCODE_ENDSUB: /* end subroutine */
726 break;
727 case OPCODE_BRA: /* branch (conditional) */
Briane80d9012007-02-23 16:53:24 -0700728 if (eval_condition(machine, inst)) {
729 /* take branch */
Brian Pauldb721152009-12-22 14:15:30 -0700730 /* Subtract 1 here since we'll do pc++ below */
731 pc = inst->BranchTarget - 1;
732 }
733 break;
734 case OPCODE_BRK: /* break out of loop (conditional) */
735 ASSERT(program->Instructions[inst->BranchTarget].Opcode
736 == OPCODE_ENDLOOP);
737 if (eval_condition(machine, inst)) {
738 /* break out of loop */
739 /* pc++ at end of for-loop will put us after the ENDLOOP inst */
740 pc = inst->BranchTarget;
741 }
742 break;
743 case OPCODE_CONT: /* continue loop (conditional) */
744 ASSERT(program->Instructions[inst->BranchTarget].Opcode
745 == OPCODE_ENDLOOP);
746 if (eval_condition(machine, inst)) {
747 /* continue at ENDLOOP */
Briane80d9012007-02-23 16:53:24 -0700748 /* Subtract 1 here since we'll do pc++ at end of for-loop */
749 pc = inst->BranchTarget - 1;
750 }
751 break;
752 case OPCODE_CAL: /* Call subroutine (conditional) */
753 if (eval_condition(machine, inst)) {
754 /* call the subroutine */
755 if (machine->StackDepth >= MAX_PROGRAM_CALL_DEPTH) {
756 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
Brian13e3b212007-02-22 16:09:40 -0700757 }
Briana0275b02007-03-27 11:02:20 -0600758 machine->CallStack[machine->StackDepth++] = pc + 1; /* next inst */
Brian31dc7a32007-03-27 15:21:35 -0600759 /* Subtract 1 here since we'll do pc++ at end of for-loop */
760 pc = inst->BranchTarget - 1;
Briane80d9012007-02-23 16:53:24 -0700761 }
762 break;
763 case OPCODE_CMP:
764 {
765 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700766 fetch_vector4(&inst->SrcReg[0], machine, a);
767 fetch_vector4(&inst->SrcReg[1], machine, b);
768 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -0700769 result[0] = a[0] < 0.0F ? b[0] : c[0];
770 result[1] = a[1] < 0.0F ? b[1] : c[1];
771 result[2] = a[2] < 0.0F ? b[2] : c[2];
772 result[3] = a[3] < 0.0F ? b[3] : c[3];
773 store_vector4(inst, machine, result);
Brian Paulfd7f2ae2010-09-01 12:46:57 -0600774 if (DEBUG_PROG) {
775 printf("CMP (%g %g %g %g) = (%g %g %g %g) < 0 ? (%g %g %g %g) : (%g %g %g %g)\n",
776 result[0], result[1], result[2], result[3],
777 a[0], a[1], a[2], a[3],
778 b[0], b[1], b[2], b[3],
779 c[0], c[1], c[2], c[3]);
780 }
Briane80d9012007-02-23 16:53:24 -0700781 }
782 break;
783 case OPCODE_COS:
784 {
785 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700786 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700787 result[0] = result[1] = result[2] = result[3]
Eric Anholt165694a2010-05-05 08:36:20 -0700788 = (GLfloat) cos(a[0]);
Briane80d9012007-02-23 16:53:24 -0700789 store_vector4(inst, machine, result);
790 }
791 break;
792 case OPCODE_DDX: /* Partial derivative with respect to X */
793 {
Brian62da6a12007-05-02 18:44:34 -0600794 GLfloat result[4];
795 fetch_vector4_deriv(ctx, &inst->SrcReg[0], machine,
796 'X', result);
Briane80d9012007-02-23 16:53:24 -0700797 store_vector4(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -0700798 }
799 break;
800 case OPCODE_DDY: /* Partial derivative with respect to Y */
801 {
Brian62da6a12007-05-02 18:44:34 -0600802 GLfloat result[4];
803 fetch_vector4_deriv(ctx, &inst->SrcReg[0], machine,
804 'Y', result);
Briane80d9012007-02-23 16:53:24 -0700805 store_vector4(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -0700806 }
807 break;
Brian Paul65cb74e2008-11-07 09:41:00 -0700808 case OPCODE_DP2:
809 {
810 GLfloat a[4], b[4], result[4];
811 fetch_vector4(&inst->SrcReg[0], machine, a);
812 fetch_vector4(&inst->SrcReg[1], machine, b);
813 result[0] = result[1] = result[2] = result[3] = DOT2(a, b);
814 store_vector4(inst, machine, result);
815 if (DEBUG_PROG) {
816 printf("DP2 %g = (%g %g) . (%g %g)\n",
817 result[0], a[0], a[1], b[0], b[1]);
818 }
819 }
820 break;
821 case OPCODE_DP2A:
822 {
823 GLfloat a[4], b[4], c, result[4];
824 fetch_vector4(&inst->SrcReg[0], machine, a);
825 fetch_vector4(&inst->SrcReg[1], machine, b);
826 fetch_vector1(&inst->SrcReg[1], machine, &c);
827 result[0] = result[1] = result[2] = result[3] = DOT2(a, b) + c;
828 store_vector4(inst, machine, result);
829 if (DEBUG_PROG) {
830 printf("DP2A %g = (%g %g) . (%g %g) + %g\n",
831 result[0], a[0], a[1], b[0], b[1], c);
832 }
833 }
834 break;
Briane80d9012007-02-23 16:53:24 -0700835 case OPCODE_DP3:
836 {
837 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700838 fetch_vector4(&inst->SrcReg[0], machine, a);
839 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700840 result[0] = result[1] = result[2] = result[3] = DOT3(a, b);
841 store_vector4(inst, machine, result);
842 if (DEBUG_PROG) {
843 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
844 result[0], a[0], a[1], a[2], b[0], b[1], b[2]);
Brian13e3b212007-02-22 16:09:40 -0700845 }
Briane80d9012007-02-23 16:53:24 -0700846 }
847 break;
848 case OPCODE_DP4:
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] = result[1] = result[2] = result[3] = DOT4(a, b);
854 store_vector4(inst, machine, result);
855 if (DEBUG_PROG) {
856 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
857 result[0], a[0], a[1], a[2], a[3],
858 b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -0700859 }
Briane80d9012007-02-23 16:53:24 -0700860 }
861 break;
862 case OPCODE_DPH:
863 {
864 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700865 fetch_vector4(&inst->SrcReg[0], machine, a);
866 fetch_vector4(&inst->SrcReg[1], machine, b);
Brian Paul65cb74e2008-11-07 09:41:00 -0700867 result[0] = result[1] = result[2] = result[3] = DOT3(a, b) + b[3];
Briane80d9012007-02-23 16:53:24 -0700868 store_vector4(inst, machine, result);
869 }
870 break;
871 case OPCODE_DST: /* Distance vector */
872 {
873 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700874 fetch_vector4(&inst->SrcReg[0], machine, a);
875 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700876 result[0] = 1.0F;
877 result[1] = a[1] * b[1];
878 result[2] = a[2];
879 result[3] = b[3];
880 store_vector4(inst, machine, result);
881 }
882 break;
Brianf183a2d2007-02-23 17:14:30 -0700883 case OPCODE_EXP:
Brianf183a2d2007-02-23 17:14:30 -0700884 {
885 GLfloat t[4], q[4], floor_t0;
Brian33eac562007-02-25 18:52:41 -0700886 fetch_vector1(&inst->SrcReg[0], machine, t);
Brianf183a2d2007-02-23 17:14:30 -0700887 floor_t0 = FLOORF(t[0]);
888 if (floor_t0 > FLT_MAX_EXP) {
889 SET_POS_INFINITY(q[0]);
890 SET_POS_INFINITY(q[2]);
891 }
892 else if (floor_t0 < FLT_MIN_EXP) {
893 q[0] = 0.0F;
894 q[2] = 0.0F;
895 }
896 else {
Brian761728a2007-02-24 11:14:57 -0700897 q[0] = LDEXPF(1.0, (int) floor_t0);
898 /* Note: GL_NV_vertex_program expects
899 * result.z = result.x * APPX(result.y)
900 * We do what the ARB extension says.
901 */
Eric Anholtfef303b2010-05-05 08:29:13 -0700902 q[2] = (GLfloat) pow(2.0, t[0]);
Brianf183a2d2007-02-23 17:14:30 -0700903 }
904 q[1] = t[0] - floor_t0;
905 q[3] = 1.0F;
906 store_vector4( inst, machine, q );
907 }
908 break;
Briane80d9012007-02-23 16:53:24 -0700909 case OPCODE_EX2: /* Exponential base 2 */
910 {
Brian Paul035de6a2009-06-03 15:42:52 -0600911 GLfloat a[4], result[4], val;
Brian33eac562007-02-25 18:52:41 -0700912 fetch_vector1(&inst->SrcReg[0], machine, a);
Eric Anholtfef303b2010-05-05 08:29:13 -0700913 val = (GLfloat) pow(2.0, a[0]);
Brian Paul035de6a2009-06-03 15:42:52 -0600914 /*
915 if (IS_INF_OR_NAN(val))
916 val = 1.0e10;
917 */
918 result[0] = result[1] = result[2] = result[3] = val;
Briane80d9012007-02-23 16:53:24 -0700919 store_vector4(inst, machine, result);
920 }
921 break;
922 case OPCODE_FLR:
923 {
924 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700925 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700926 result[0] = FLOORF(a[0]);
927 result[1] = FLOORF(a[1]);
928 result[2] = FLOORF(a[2]);
929 result[3] = FLOORF(a[3]);
930 store_vector4(inst, machine, result);
931 }
932 break;
933 case OPCODE_FRC:
934 {
935 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700936 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700937 result[0] = a[0] - FLOORF(a[0]);
938 result[1] = a[1] - FLOORF(a[1]);
939 result[2] = a[2] - FLOORF(a[2]);
940 result[3] = a[3] - FLOORF(a[3]);
941 store_vector4(inst, machine, result);
942 }
943 break;
944 case OPCODE_IF:
Brian63556fa2007-03-23 14:47:46 -0600945 {
946 GLboolean cond;
Brian Paulddd97292009-12-22 14:21:07 -0700947 ASSERT(program->Instructions[inst->BranchTarget].Opcode
948 == OPCODE_ELSE ||
949 program->Instructions[inst->BranchTarget].Opcode
950 == OPCODE_ENDIF);
Brian63556fa2007-03-23 14:47:46 -0600951 /* eval condition */
952 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
953 GLfloat a[4];
954 fetch_vector1(&inst->SrcReg[0], machine, a);
955 cond = (a[0] != 0.0);
956 }
957 else {
958 cond = eval_condition(machine, inst);
959 }
Briana7f73662007-04-20 08:11:51 -0600960 if (DEBUG_PROG) {
961 printf("IF: %d\n", cond);
962 }
Brian63556fa2007-03-23 14:47:46 -0600963 /* do if/else */
964 if (cond) {
965 /* do if-clause (just continue execution) */
966 }
967 else {
968 /* go to the instruction after ELSE or ENDIF */
969 assert(inst->BranchTarget >= 0);
Brian Paulddd97292009-12-22 14:21:07 -0700970 pc = inst->BranchTarget;
Brian63556fa2007-03-23 14:47:46 -0600971 }
Briane80d9012007-02-23 16:53:24 -0700972 }
973 break;
974 case OPCODE_ELSE:
975 /* goto ENDIF */
Brian Paulddd97292009-12-22 14:21:07 -0700976 ASSERT(program->Instructions[inst->BranchTarget].Opcode
977 == OPCODE_ENDIF);
Briane80d9012007-02-23 16:53:24 -0700978 assert(inst->BranchTarget >= 0);
Brian Paulddd97292009-12-22 14:21:07 -0700979 pc = inst->BranchTarget;
Briane80d9012007-02-23 16:53:24 -0700980 break;
981 case OPCODE_ENDIF:
982 /* nothing */
983 break;
Briane80d9012007-02-23 16:53:24 -0700984 case OPCODE_KIL_NV: /* NV_f_p only (conditional) */
985 if (eval_condition(machine, inst)) {
986 return GL_FALSE;
987 }
988 break;
989 case OPCODE_KIL: /* ARB_f_p only */
990 {
991 GLfloat a[4];
Brian33eac562007-02-25 18:52:41 -0700992 fetch_vector4(&inst->SrcReg[0], machine, a);
Brian Paulc0633dd2009-08-31 14:57:59 -0600993 if (DEBUG_PROG) {
994 printf("KIL if (%g %g %g %g) <= 0.0\n",
995 a[0], a[1], a[2], a[3]);
996 }
997
Briane80d9012007-02-23 16:53:24 -0700998 if (a[0] < 0.0F || a[1] < 0.0F || a[2] < 0.0F || a[3] < 0.0F) {
Brian13e3b212007-02-22 16:09:40 -0700999 return GL_FALSE;
1000 }
Briane80d9012007-02-23 16:53:24 -07001001 }
1002 break;
1003 case OPCODE_LG2: /* log base 2 */
1004 {
Brian Paul035de6a2009-06-03 15:42:52 -06001005 GLfloat a[4], result[4], val;
Brian33eac562007-02-25 18:52:41 -07001006 fetch_vector1(&inst->SrcReg[0], machine, a);
Ian Romanick962fa6b2008-12-18 14:11:06 -08001007 /* The fast LOG2 macro doesn't meet the precision requirements.
1008 */
Brian Paul035de6a2009-06-03 15:42:52 -06001009 if (a[0] == 0.0F) {
Vinson Lee18883cd2009-10-01 13:33:20 -06001010 val = -FLT_MAX;
Brian Paul035de6a2009-06-03 15:42:52 -06001011 }
1012 else {
Karl Schultzb30898f2010-02-13 17:31:58 -07001013 val = (float)(log(a[0]) * 1.442695F);
Brian Paul035de6a2009-06-03 15:42:52 -06001014 }
1015 result[0] = result[1] = result[2] = result[3] = val;
Briane80d9012007-02-23 16:53:24 -07001016 store_vector4(inst, machine, result);
1017 }
1018 break;
1019 case OPCODE_LIT:
1020 {
1021 const GLfloat epsilon = 1.0F / 256.0F; /* from NV VP spec */
1022 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001023 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001024 a[0] = MAX2(a[0], 0.0F);
1025 a[1] = MAX2(a[1], 0.0F);
1026 /* XXX ARB version clamps a[3], NV version doesn't */
1027 a[3] = CLAMP(a[3], -(128.0F - epsilon), (128.0F - epsilon));
1028 result[0] = 1.0F;
1029 result[1] = a[0];
1030 /* XXX we could probably just use pow() here */
1031 if (a[0] > 0.0F) {
1032 if (a[1] == 0.0 && a[3] == 0.0)
Brian Paul20fbb242010-01-27 17:03:04 -07001033 result[2] = 1.0F;
Briane80d9012007-02-23 16:53:24 -07001034 else
Eric Anholtfef303b2010-05-05 08:29:13 -07001035 result[2] = (GLfloat) pow(a[1], a[3]);
Brian13e3b212007-02-22 16:09:40 -07001036 }
Briane80d9012007-02-23 16:53:24 -07001037 else {
Brian Paul20fbb242010-01-27 17:03:04 -07001038 result[2] = 0.0F;
Brian13e3b212007-02-22 16:09:40 -07001039 }
Briane80d9012007-02-23 16:53:24 -07001040 result[3] = 1.0F;
1041 store_vector4(inst, machine, result);
1042 if (DEBUG_PROG) {
1043 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
1044 result[0], result[1], result[2], result[3],
1045 a[0], a[1], a[2], a[3]);
Brian13e3b212007-02-22 16:09:40 -07001046 }
Briane80d9012007-02-23 16:53:24 -07001047 }
1048 break;
Brianf183a2d2007-02-23 17:14:30 -07001049 case OPCODE_LOG:
1050 {
1051 GLfloat t[4], q[4], abs_t0;
Brian33eac562007-02-25 18:52:41 -07001052 fetch_vector1(&inst->SrcReg[0], machine, t);
Brianf183a2d2007-02-23 17:14:30 -07001053 abs_t0 = FABSF(t[0]);
1054 if (abs_t0 != 0.0F) {
1055 /* Since we really can't handle infinite values on VMS
1056 * like other OSes we'll use __MAXFLOAT to represent
1057 * infinity. This may need some tweaking.
1058 */
1059#ifdef VMS
1060 if (abs_t0 == __MAXFLOAT)
1061#else
1062 if (IS_INF_OR_NAN(abs_t0))
1063#endif
1064 {
1065 SET_POS_INFINITY(q[0]);
1066 q[1] = 1.0F;
1067 SET_POS_INFINITY(q[2]);
1068 }
1069 else {
1070 int exponent;
1071 GLfloat mantissa = FREXPF(t[0], &exponent);
1072 q[0] = (GLfloat) (exponent - 1);
1073 q[1] = (GLfloat) (2.0 * mantissa); /* map [.5, 1) -> [1, 2) */
Ian Romanick962fa6b2008-12-18 14:11:06 -08001074
1075 /* The fast LOG2 macro doesn't meet the precision
1076 * requirements.
1077 */
Karl Schultzb30898f2010-02-13 17:31:58 -07001078 q[2] = (float)(log(t[0]) * 1.442695F);
Brianf183a2d2007-02-23 17:14:30 -07001079 }
1080 }
1081 else {
1082 SET_NEG_INFINITY(q[0]);
1083 q[1] = 1.0F;
1084 SET_NEG_INFINITY(q[2]);
1085 }
1086 q[3] = 1.0;
1087 store_vector4(inst, machine, q);
1088 }
1089 break;
Briane80d9012007-02-23 16:53:24 -07001090 case OPCODE_LRP:
1091 {
1092 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001093 fetch_vector4(&inst->SrcReg[0], machine, a);
1094 fetch_vector4(&inst->SrcReg[1], machine, b);
1095 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -07001096 result[0] = a[0] * b[0] + (1.0F - a[0]) * c[0];
1097 result[1] = a[1] * b[1] + (1.0F - a[1]) * c[1];
1098 result[2] = a[2] * b[2] + (1.0F - a[2]) * c[2];
1099 result[3] = a[3] * b[3] + (1.0F - a[3]) * c[3];
1100 store_vector4(inst, machine, result);
1101 if (DEBUG_PROG) {
1102 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
1103 "(%g %g %g %g), (%g %g %g %g)\n",
1104 result[0], result[1], result[2], result[3],
1105 a[0], a[1], a[2], a[3],
1106 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
Brian13e3b212007-02-22 16:09:40 -07001107 }
Briane80d9012007-02-23 16:53:24 -07001108 }
1109 break;
1110 case OPCODE_MAD:
1111 {
1112 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001113 fetch_vector4(&inst->SrcReg[0], machine, a);
1114 fetch_vector4(&inst->SrcReg[1], machine, b);
1115 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -07001116 result[0] = a[0] * b[0] + c[0];
1117 result[1] = a[1] * b[1] + c[1];
1118 result[2] = a[2] * b[2] + c[2];
1119 result[3] = a[3] * b[3] + c[3];
1120 store_vector4(inst, machine, result);
1121 if (DEBUG_PROG) {
1122 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
1123 "(%g %g %g %g) + (%g %g %g %g)\n",
1124 result[0], result[1], result[2], result[3],
1125 a[0], a[1], a[2], a[3],
1126 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
Brian13e3b212007-02-22 16:09:40 -07001127 }
Briane80d9012007-02-23 16:53:24 -07001128 }
1129 break;
1130 case OPCODE_MAX:
1131 {
1132 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001133 fetch_vector4(&inst->SrcReg[0], machine, a);
1134 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001135 result[0] = MAX2(a[0], b[0]);
1136 result[1] = MAX2(a[1], b[1]);
1137 result[2] = MAX2(a[2], b[2]);
1138 result[3] = MAX2(a[3], b[3]);
1139 store_vector4(inst, machine, result);
1140 if (DEBUG_PROG) {
1141 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
1142 result[0], result[1], result[2], result[3],
1143 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001144 }
Briane80d9012007-02-23 16:53:24 -07001145 }
1146 break;
1147 case OPCODE_MIN:
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] = MIN2(a[0], b[0]);
1153 result[1] = MIN2(a[1], b[1]);
1154 result[2] = MIN2(a[2], b[2]);
1155 result[3] = MIN2(a[3], b[3]);
1156 store_vector4(inst, machine, result);
1157 }
1158 break;
1159 case OPCODE_MOV:
1160 {
1161 GLfloat result[4];
Brian33eac562007-02-25 18:52:41 -07001162 fetch_vector4(&inst->SrcReg[0], machine, result);
Briane80d9012007-02-23 16:53:24 -07001163 store_vector4(inst, machine, result);
1164 if (DEBUG_PROG) {
1165 printf("MOV (%g %g %g %g)\n",
1166 result[0], result[1], result[2], result[3]);
Brian13e3b212007-02-22 16:09:40 -07001167 }
Briane80d9012007-02-23 16:53:24 -07001168 }
1169 break;
1170 case OPCODE_MUL:
1171 {
1172 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001173 fetch_vector4(&inst->SrcReg[0], machine, a);
1174 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001175 result[0] = a[0] * b[0];
1176 result[1] = a[1] * b[1];
1177 result[2] = a[2] * b[2];
1178 result[3] = a[3] * b[3];
1179 store_vector4(inst, machine, result);
1180 if (DEBUG_PROG) {
1181 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1182 result[0], result[1], result[2], result[3],
1183 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001184 }
Briane80d9012007-02-23 16:53:24 -07001185 }
1186 break;
1187 case OPCODE_NOISE1:
1188 {
1189 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001190 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001191 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001192 result[1] =
Brian Paul702b5b02008-12-15 18:37:39 -07001193 result[2] =
1194 result[3] = _mesa_noise1(a[0]);
Briane80d9012007-02-23 16:53:24 -07001195 store_vector4(inst, machine, result);
1196 }
1197 break;
1198 case OPCODE_NOISE2:
1199 {
1200 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001201 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001202 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001203 result[1] =
Brian Paul702b5b02008-12-15 18:37:39 -07001204 result[2] = result[3] = _mesa_noise2(a[0], a[1]);
Briane80d9012007-02-23 16:53:24 -07001205 store_vector4(inst, machine, result);
1206 }
1207 break;
1208 case OPCODE_NOISE3:
1209 {
1210 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001211 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001212 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001213 result[1] =
1214 result[2] =
Brian Paul702b5b02008-12-15 18:37:39 -07001215 result[3] = _mesa_noise3(a[0], a[1], a[2]);
Briane80d9012007-02-23 16:53:24 -07001216 store_vector4(inst, machine, result);
1217 }
1218 break;
1219 case OPCODE_NOISE4:
1220 {
1221 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001222 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001223 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001224 result[1] =
1225 result[2] =
Brian Paul702b5b02008-12-15 18:37:39 -07001226 result[3] = _mesa_noise4(a[0], a[1], a[2], a[3]);
Briane80d9012007-02-23 16:53:24 -07001227 store_vector4(inst, machine, result);
1228 }
1229 break;
1230 case OPCODE_NOP:
1231 break;
Brian Paul37eef7b2008-11-07 09:33:55 -07001232 case OPCODE_NOT: /* bitwise NOT */
1233 {
1234 GLuint a[4], result[4];
1235 fetch_vector4ui(&inst->SrcReg[0], machine, a);
1236 result[0] = ~a[0];
1237 result[1] = ~a[1];
1238 result[2] = ~a[2];
1239 result[3] = ~a[3];
1240 store_vector4ui(inst, machine, result);
1241 }
1242 break;
Brian Paulf6ead502008-11-07 08:51:31 -07001243 case OPCODE_NRM3: /* 3-component normalization */
1244 {
1245 GLfloat a[4], result[4];
1246 GLfloat tmp;
1247 fetch_vector4(&inst->SrcReg[0], machine, a);
1248 tmp = a[0] * a[0] + a[1] * a[1] + a[2] * a[2];
1249 if (tmp != 0.0F)
Brian Paul22459e72008-11-07 12:59:36 -07001250 tmp = INV_SQRTF(tmp);
Brian Paulf6ead502008-11-07 08:51:31 -07001251 result[0] = tmp * a[0];
1252 result[1] = tmp * a[1];
1253 result[2] = tmp * a[2];
1254 result[3] = 0.0; /* undefined, but prevent valgrind warnings */
1255 store_vector4(inst, machine, result);
1256 }
1257 break;
1258 case OPCODE_NRM4: /* 4-component normalization */
1259 {
1260 GLfloat a[4], result[4];
1261 GLfloat tmp;
1262 fetch_vector4(&inst->SrcReg[0], machine, a);
1263 tmp = a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3];
1264 if (tmp != 0.0F)
Brian Paul22459e72008-11-07 12:59:36 -07001265 tmp = INV_SQRTF(tmp);
Brian Paulf6ead502008-11-07 08:51:31 -07001266 result[0] = tmp * a[0];
1267 result[1] = tmp * a[1];
1268 result[2] = tmp * a[2];
1269 result[3] = tmp * a[3];
1270 store_vector4(inst, machine, result);
1271 }
1272 break;
Brian Paul37eef7b2008-11-07 09:33:55 -07001273 case OPCODE_OR: /* bitwise OR */
1274 {
1275 GLuint a[4], b[4], result[4];
1276 fetch_vector4ui(&inst->SrcReg[0], machine, a);
1277 fetch_vector4ui(&inst->SrcReg[1], machine, b);
1278 result[0] = a[0] | b[0];
1279 result[1] = a[1] | b[1];
1280 result[2] = a[2] | b[2];
1281 result[3] = a[3] | b[3];
1282 store_vector4ui(inst, machine, result);
1283 }
1284 break;
Briane80d9012007-02-23 16:53:24 -07001285 case OPCODE_PK2H: /* pack two 16-bit floats in one 32-bit float */
1286 {
Brian Paul37eef7b2008-11-07 09:33:55 -07001287 GLfloat a[4];
1288 GLuint result[4];
Briane80d9012007-02-23 16:53:24 -07001289 GLhalfNV hx, hy;
Brian33eac562007-02-25 18:52:41 -07001290 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001291 hx = _mesa_float_to_half(a[0]);
1292 hy = _mesa_float_to_half(a[1]);
Brian Paul37eef7b2008-11-07 09:33:55 -07001293 result[0] =
1294 result[1] =
1295 result[2] =
1296 result[3] = hx | (hy << 16);
1297 store_vector4ui(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -07001298 }
1299 break;
1300 case OPCODE_PK2US: /* pack two GLushorts into one 32-bit float */
1301 {
Brian Paul37eef7b2008-11-07 09:33:55 -07001302 GLfloat a[4];
1303 GLuint result[4], usx, usy;
Brian33eac562007-02-25 18:52:41 -07001304 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001305 a[0] = CLAMP(a[0], 0.0F, 1.0F);
1306 a[1] = CLAMP(a[1], 0.0F, 1.0F);
1307 usx = IROUND(a[0] * 65535.0F);
1308 usy = IROUND(a[1] * 65535.0F);
Brian Paul37eef7b2008-11-07 09:33:55 -07001309 result[0] =
1310 result[1] =
1311 result[2] =
1312 result[3] = usx | (usy << 16);
1313 store_vector4ui(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -07001314 }
1315 break;
1316 case OPCODE_PK4B: /* pack four GLbytes into one 32-bit float */
1317 {
Brian Paul37eef7b2008-11-07 09:33:55 -07001318 GLfloat a[4];
1319 GLuint result[4], ubx, uby, ubz, ubw;
Brian33eac562007-02-25 18:52:41 -07001320 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001321 a[0] = CLAMP(a[0], -128.0F / 127.0F, 1.0F);
1322 a[1] = CLAMP(a[1], -128.0F / 127.0F, 1.0F);
1323 a[2] = CLAMP(a[2], -128.0F / 127.0F, 1.0F);
1324 a[3] = CLAMP(a[3], -128.0F / 127.0F, 1.0F);
1325 ubx = IROUND(127.0F * a[0] + 128.0F);
1326 uby = IROUND(127.0F * a[1] + 128.0F);
1327 ubz = IROUND(127.0F * a[2] + 128.0F);
1328 ubw = IROUND(127.0F * a[3] + 128.0F);
Brian Paul37eef7b2008-11-07 09:33:55 -07001329 result[0] =
1330 result[1] =
1331 result[2] =
1332 result[3] = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
1333 store_vector4ui(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -07001334 }
1335 break;
1336 case OPCODE_PK4UB: /* pack four GLubytes into one 32-bit float */
1337 {
Brian Paul37eef7b2008-11-07 09:33:55 -07001338 GLfloat a[4];
1339 GLuint result[4], ubx, uby, ubz, ubw;
Brian33eac562007-02-25 18:52:41 -07001340 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001341 a[0] = CLAMP(a[0], 0.0F, 1.0F);
1342 a[1] = CLAMP(a[1], 0.0F, 1.0F);
1343 a[2] = CLAMP(a[2], 0.0F, 1.0F);
1344 a[3] = CLAMP(a[3], 0.0F, 1.0F);
1345 ubx = IROUND(255.0F * a[0]);
1346 uby = IROUND(255.0F * a[1]);
1347 ubz = IROUND(255.0F * a[2]);
1348 ubw = IROUND(255.0F * a[3]);
Brian Paul37eef7b2008-11-07 09:33:55 -07001349 result[0] =
1350 result[1] =
1351 result[2] =
1352 result[3] = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
1353 store_vector4ui(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -07001354 }
1355 break;
1356 case OPCODE_POW:
1357 {
1358 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001359 fetch_vector1(&inst->SrcReg[0], machine, a);
1360 fetch_vector1(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001361 result[0] = result[1] = result[2] = result[3]
Eric Anholtfef303b2010-05-05 08:29:13 -07001362 = (GLfloat) pow(a[0], b[0]);
Briane80d9012007-02-23 16:53:24 -07001363 store_vector4(inst, machine, result);
1364 }
1365 break;
Brian Paulb4ad7c22010-07-29 08:23:59 -06001366 case OPCODE_RCC: /* clamped riciprocal */
1367 {
1368 const float largest = 1.884467e+19, smallest = 5.42101e-20;
1369 GLfloat a[4], r, result[4];
1370 fetch_vector1(&inst->SrcReg[0], machine, a);
1371 if (DEBUG_PROG) {
1372 if (a[0] == 0)
1373 printf("RCC(0)\n");
1374 else if (IS_INF_OR_NAN(a[0]))
1375 printf("RCC(inf)\n");
1376 }
1377 if (a[0] == 1.0F) {
1378 r = 1.0F;
1379 }
1380 else {
1381 r = 1.0F / a[0];
1382 }
1383 if (positive(r)) {
1384 if (r > largest) {
1385 r = largest;
1386 }
1387 else if (r < smallest) {
1388 r = smallest;
1389 }
1390 }
1391 else {
1392 if (r < -largest) {
1393 r = -largest;
1394 }
1395 else if (r > -smallest) {
1396 r = -smallest;
1397 }
1398 }
1399 result[0] = result[1] = result[2] = result[3] = r;
1400 store_vector4(inst, machine, result);
1401 }
1402 break;
1403
Briane80d9012007-02-23 16:53:24 -07001404 case OPCODE_RCP:
1405 {
1406 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001407 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001408 if (DEBUG_PROG) {
1409 if (a[0] == 0)
1410 printf("RCP(0)\n");
1411 else if (IS_INF_OR_NAN(a[0]))
1412 printf("RCP(inf)\n");
Brian13e3b212007-02-22 16:09:40 -07001413 }
Briane80d9012007-02-23 16:53:24 -07001414 result[0] = result[1] = result[2] = result[3] = 1.0F / a[0];
1415 store_vector4(inst, machine, result);
1416 }
1417 break;
1418 case OPCODE_RET: /* return from subroutine (conditional) */
1419 if (eval_condition(machine, inst)) {
1420 if (machine->StackDepth == 0) {
1421 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
Brian13e3b212007-02-22 16:09:40 -07001422 }
Briana0275b02007-03-27 11:02:20 -06001423 /* subtract one because of pc++ in the for loop */
1424 pc = machine->CallStack[--machine->StackDepth] - 1;
Briane80d9012007-02-23 16:53:24 -07001425 }
1426 break;
1427 case OPCODE_RFL: /* reflection vector */
1428 {
1429 GLfloat axis[4], dir[4], result[4], tmpX, tmpW;
Brian33eac562007-02-25 18:52:41 -07001430 fetch_vector4(&inst->SrcReg[0], machine, axis);
1431 fetch_vector4(&inst->SrcReg[1], machine, dir);
Briane80d9012007-02-23 16:53:24 -07001432 tmpW = DOT3(axis, axis);
1433 tmpX = (2.0F * DOT3(axis, dir)) / tmpW;
1434 result[0] = tmpX * axis[0] - dir[0];
1435 result[1] = tmpX * axis[1] - dir[1];
1436 result[2] = tmpX * axis[2] - dir[2];
1437 /* result[3] is never written! XXX enforce in parser! */
1438 store_vector4(inst, machine, result);
1439 }
1440 break;
1441 case OPCODE_RSQ: /* 1 / sqrt() */
1442 {
1443 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001444 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001445 a[0] = FABSF(a[0]);
1446 result[0] = result[1] = result[2] = result[3] = INV_SQRTF(a[0]);
1447 store_vector4(inst, machine, result);
1448 if (DEBUG_PROG) {
1449 printf("RSQ %g = 1/sqrt(|%g|)\n", result[0], a[0]);
Brian13e3b212007-02-22 16:09:40 -07001450 }
Briane80d9012007-02-23 16:53:24 -07001451 }
1452 break;
1453 case OPCODE_SCS: /* sine and cos */
1454 {
1455 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001456 fetch_vector1(&inst->SrcReg[0], machine, a);
Eric Anholt165694a2010-05-05 08:36:20 -07001457 result[0] = (GLfloat) cos(a[0]);
1458 result[1] = (GLfloat) sin(a[0]);
Briane80d9012007-02-23 16:53:24 -07001459 result[2] = 0.0; /* undefined! */
1460 result[3] = 0.0; /* undefined! */
1461 store_vector4(inst, machine, result);
1462 }
1463 break;
1464 case OPCODE_SEQ: /* set on equal */
1465 {
1466 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001467 fetch_vector4(&inst->SrcReg[0], machine, a);
1468 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001469 result[0] = (a[0] == b[0]) ? 1.0F : 0.0F;
1470 result[1] = (a[1] == b[1]) ? 1.0F : 0.0F;
1471 result[2] = (a[2] == b[2]) ? 1.0F : 0.0F;
1472 result[3] = (a[3] == b[3]) ? 1.0F : 0.0F;
1473 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001474 if (DEBUG_PROG) {
1475 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1476 result[0], result[1], result[2], result[3],
1477 a[0], a[1], a[2], a[3],
1478 b[0], b[1], b[2], b[3]);
1479 }
Briane80d9012007-02-23 16:53:24 -07001480 }
1481 break;
1482 case OPCODE_SFL: /* set false, operands ignored */
1483 {
1484 static const GLfloat result[4] = { 0.0F, 0.0F, 0.0F, 0.0F };
1485 store_vector4(inst, machine, result);
1486 }
1487 break;
1488 case OPCODE_SGE: /* set on greater or equal */
1489 {
1490 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001491 fetch_vector4(&inst->SrcReg[0], machine, a);
1492 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001493 result[0] = (a[0] >= b[0]) ? 1.0F : 0.0F;
1494 result[1] = (a[1] >= b[1]) ? 1.0F : 0.0F;
1495 result[2] = (a[2] >= b[2]) ? 1.0F : 0.0F;
1496 result[3] = (a[3] >= b[3]) ? 1.0F : 0.0F;
1497 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001498 if (DEBUG_PROG) {
1499 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1500 result[0], result[1], result[2], result[3],
1501 a[0], a[1], a[2], a[3],
1502 b[0], b[1], b[2], b[3]);
1503 }
Briane80d9012007-02-23 16:53:24 -07001504 }
1505 break;
1506 case OPCODE_SGT: /* set on greater */
1507 {
1508 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001509 fetch_vector4(&inst->SrcReg[0], machine, a);
1510 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001511 result[0] = (a[0] > b[0]) ? 1.0F : 0.0F;
1512 result[1] = (a[1] > b[1]) ? 1.0F : 0.0F;
1513 result[2] = (a[2] > b[2]) ? 1.0F : 0.0F;
1514 result[3] = (a[3] > b[3]) ? 1.0F : 0.0F;
1515 store_vector4(inst, machine, result);
1516 if (DEBUG_PROG) {
Brian28ab1122007-03-06 12:15:30 -07001517 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1518 result[0], result[1], result[2], result[3],
1519 a[0], a[1], a[2], a[3],
1520 b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001521 }
Briane80d9012007-02-23 16:53:24 -07001522 }
1523 break;
1524 case OPCODE_SIN:
1525 {
1526 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001527 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001528 result[0] = result[1] = result[2] = result[3]
Eric Anholt165694a2010-05-05 08:36:20 -07001529 = (GLfloat) sin(a[0]);
Briane80d9012007-02-23 16:53:24 -07001530 store_vector4(inst, machine, result);
1531 }
1532 break;
1533 case OPCODE_SLE: /* set on less or equal */
1534 {
1535 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001536 fetch_vector4(&inst->SrcReg[0], machine, a);
1537 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001538 result[0] = (a[0] <= b[0]) ? 1.0F : 0.0F;
1539 result[1] = (a[1] <= b[1]) ? 1.0F : 0.0F;
1540 result[2] = (a[2] <= b[2]) ? 1.0F : 0.0F;
1541 result[3] = (a[3] <= b[3]) ? 1.0F : 0.0F;
1542 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001543 if (DEBUG_PROG) {
1544 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1545 result[0], result[1], result[2], result[3],
1546 a[0], a[1], a[2], a[3],
1547 b[0], b[1], b[2], b[3]);
1548 }
Briane80d9012007-02-23 16:53:24 -07001549 }
1550 break;
1551 case OPCODE_SLT: /* set on less */
1552 {
1553 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001554 fetch_vector4(&inst->SrcReg[0], machine, a);
1555 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001556 result[0] = (a[0] < b[0]) ? 1.0F : 0.0F;
1557 result[1] = (a[1] < b[1]) ? 1.0F : 0.0F;
1558 result[2] = (a[2] < b[2]) ? 1.0F : 0.0F;
1559 result[3] = (a[3] < b[3]) ? 1.0F : 0.0F;
1560 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001561 if (DEBUG_PROG) {
1562 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1563 result[0], result[1], result[2], result[3],
1564 a[0], a[1], a[2], a[3],
1565 b[0], b[1], b[2], b[3]);
1566 }
Briane80d9012007-02-23 16:53:24 -07001567 }
1568 break;
1569 case OPCODE_SNE: /* set on not equal */
1570 {
1571 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001572 fetch_vector4(&inst->SrcReg[0], machine, a);
1573 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001574 result[0] = (a[0] != b[0]) ? 1.0F : 0.0F;
1575 result[1] = (a[1] != b[1]) ? 1.0F : 0.0F;
1576 result[2] = (a[2] != b[2]) ? 1.0F : 0.0F;
1577 result[3] = (a[3] != b[3]) ? 1.0F : 0.0F;
1578 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001579 if (DEBUG_PROG) {
1580 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1581 result[0], result[1], result[2], result[3],
1582 a[0], a[1], a[2], a[3],
1583 b[0], b[1], b[2], b[3]);
1584 }
Briane80d9012007-02-23 16:53:24 -07001585 }
1586 break;
Brian Paulf6ead502008-11-07 08:51:31 -07001587 case OPCODE_SSG: /* set sign (-1, 0 or +1) */
1588 {
1589 GLfloat a[4], result[4];
1590 fetch_vector4(&inst->SrcReg[0], machine, a);
1591 result[0] = (GLfloat) ((a[0] > 0.0F) - (a[0] < 0.0F));
1592 result[1] = (GLfloat) ((a[1] > 0.0F) - (a[1] < 0.0F));
1593 result[2] = (GLfloat) ((a[2] > 0.0F) - (a[2] < 0.0F));
1594 result[3] = (GLfloat) ((a[3] > 0.0F) - (a[3] < 0.0F));
1595 store_vector4(inst, machine, result);
1596 }
1597 break;
Briane80d9012007-02-23 16:53:24 -07001598 case OPCODE_STR: /* set true, operands ignored */
1599 {
1600 static const GLfloat result[4] = { 1.0F, 1.0F, 1.0F, 1.0F };
1601 store_vector4(inst, machine, result);
1602 }
1603 break;
1604 case OPCODE_SUB:
1605 {
1606 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001607 fetch_vector4(&inst->SrcReg[0], machine, a);
1608 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001609 result[0] = a[0] - b[0];
1610 result[1] = a[1] - b[1];
1611 result[2] = a[2] - b[2];
1612 result[3] = a[3] - b[3];
1613 store_vector4(inst, machine, result);
1614 if (DEBUG_PROG) {
1615 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1616 result[0], result[1], result[2], result[3],
1617 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001618 }
Briane80d9012007-02-23 16:53:24 -07001619 }
1620 break;
1621 case OPCODE_SWZ: /* extended swizzle */
1622 {
1623 const struct prog_src_register *source = &inst->SrcReg[0];
Brian Paulf4361542008-11-11 10:47:10 -07001624 const GLfloat *src = get_src_register_pointer(source, machine);
Briane80d9012007-02-23 16:53:24 -07001625 GLfloat result[4];
1626 GLuint i;
1627 for (i = 0; i < 4; i++) {
1628 const GLuint swz = GET_SWZ(source->Swizzle, i);
1629 if (swz == SWIZZLE_ZERO)
1630 result[i] = 0.0;
1631 else if (swz == SWIZZLE_ONE)
1632 result[i] = 1.0;
Brian13e3b212007-02-22 16:09:40 -07001633 else {
Briane80d9012007-02-23 16:53:24 -07001634 ASSERT(swz >= 0);
1635 ASSERT(swz <= 3);
1636 result[i] = src[swz];
Brian13e3b212007-02-22 16:09:40 -07001637 }
Brian Paul7db7ff82009-04-14 22:14:30 -06001638 if (source->Negate & (1 << i))
Briane80d9012007-02-23 16:53:24 -07001639 result[i] = -result[i];
Brian13e3b212007-02-22 16:09:40 -07001640 }
Briane80d9012007-02-23 16:53:24 -07001641 store_vector4(inst, machine, result);
1642 }
1643 break;
1644 case OPCODE_TEX: /* Both ARB and NV frag prog */
Brian999b5562007-11-23 12:01:57 -07001645 /* Simple texel lookup */
Briane80d9012007-02-23 16:53:24 -07001646 {
Brian999b5562007-11-23 12:01:57 -07001647 GLfloat texcoord[4], color[4];
1648 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1649
1650 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1651
Briane80d9012007-02-23 16:53:24 -07001652 if (DEBUG_PROG) {
Brian999b5562007-11-23 12:01:57 -07001653 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
Briane80d9012007-02-23 16:53:24 -07001654 color[0], color[1], color[2], color[3],
1655 inst->TexSrcUnit,
Brian999b5562007-11-23 12:01:57 -07001656 texcoord[0], texcoord[1], texcoord[2], texcoord[3]);
Briane80d9012007-02-23 16:53:24 -07001657 }
1658 store_vector4(inst, machine, color);
1659 }
1660 break;
1661 case OPCODE_TXB: /* GL_ARB_fragment_program only */
1662 /* Texel lookup with LOD bias */
1663 {
Brian999b5562007-11-23 12:01:57 -07001664 GLfloat texcoord[4], color[4], lodBias;
1665
1666 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1667
1668 /* texcoord[3] is the bias to add to lambda */
Brian Paul890f37d2009-09-23 13:34:30 -06001669 lodBias = texcoord[3];
Brian999b5562007-11-23 12:01:57 -07001670
1671 fetch_texel(ctx, machine, inst, texcoord, lodBias, color);
1672
Briane80d9012007-02-23 16:53:24 -07001673 store_vector4(inst, machine, color);
1674 }
1675 break;
1676 case OPCODE_TXD: /* GL_NV_fragment_program only */
1677 /* Texture lookup w/ partial derivatives for LOD */
1678 {
1679 GLfloat texcoord[4], dtdx[4], dtdy[4], color[4];
Brian33eac562007-02-25 18:52:41 -07001680 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1681 fetch_vector4(&inst->SrcReg[1], machine, dtdx);
1682 fetch_vector4(&inst->SrcReg[2], machine, dtdy);
Briane80d9012007-02-23 16:53:24 -07001683 machine->FetchTexelDeriv(ctx, texcoord, dtdx, dtdy,
Brian999b5562007-11-23 12:01:57 -07001684 0.0, /* lodBias */
Briane80d9012007-02-23 16:53:24 -07001685 inst->TexSrcUnit, color);
1686 store_vector4(inst, machine, color);
1687 }
1688 break;
Ian Romanick0eb20262010-08-17 17:40:20 -07001689 case OPCODE_TXL:
1690 /* Texel lookup with explicit LOD */
1691 {
1692 GLfloat texcoord[4], color[4], lod;
1693
1694 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1695
1696 /* texcoord[3] is the LOD */
1697 lod = texcoord[3];
1698
1699 machine->FetchTexelLod(ctx, texcoord, lod,
1700 machine->Samplers[inst->TexSrcUnit], color);
1701
1702 store_vector4(inst, machine, color);
1703 }
1704 break;
Briane80d9012007-02-23 16:53:24 -07001705 case OPCODE_TXP: /* GL_ARB_fragment_program only */
1706 /* Texture lookup w/ projective divide */
1707 {
Brian999b5562007-11-23 12:01:57 -07001708 GLfloat texcoord[4], color[4];
1709
Brian33eac562007-02-25 18:52:41 -07001710 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
Briane80d9012007-02-23 16:53:24 -07001711 /* Not so sure about this test - if texcoord[3] is
1712 * zero, we'd probably be fine except for an ASSERT in
1713 * IROUND_POS() which gets triggered by the inf values created.
1714 */
1715 if (texcoord[3] != 0.0) {
1716 texcoord[0] /= texcoord[3];
1717 texcoord[1] /= texcoord[3];
1718 texcoord[2] /= texcoord[3];
1719 }
Brian999b5562007-11-23 12:01:57 -07001720
1721 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1722
Briane80d9012007-02-23 16:53:24 -07001723 store_vector4(inst, machine, color);
1724 }
1725 break;
1726 case OPCODE_TXP_NV: /* GL_NV_fragment_program only */
Brian999b5562007-11-23 12:01:57 -07001727 /* Texture lookup w/ projective divide, as above, but do not
1728 * do the divide by w if sampling from a cube map.
1729 */
Briane80d9012007-02-23 16:53:24 -07001730 {
Brian999b5562007-11-23 12:01:57 -07001731 GLfloat texcoord[4], color[4];
1732
Brian33eac562007-02-25 18:52:41 -07001733 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
Briane80d9012007-02-23 16:53:24 -07001734 if (inst->TexSrcTarget != TEXTURE_CUBE_INDEX &&
1735 texcoord[3] != 0.0) {
1736 texcoord[0] /= texcoord[3];
1737 texcoord[1] /= texcoord[3];
1738 texcoord[2] /= texcoord[3];
1739 }
Brian999b5562007-11-23 12:01:57 -07001740
1741 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1742
Briane80d9012007-02-23 16:53:24 -07001743 store_vector4(inst, machine, color);
1744 }
1745 break;
Brian Paul035c0cf2008-11-06 17:14:33 -07001746 case OPCODE_TRUNC: /* truncate toward zero */
1747 {
1748 GLfloat a[4], result[4];
1749 fetch_vector4(&inst->SrcReg[0], machine, a);
1750 result[0] = (GLfloat) (GLint) a[0];
1751 result[1] = (GLfloat) (GLint) a[1];
1752 result[2] = (GLfloat) (GLint) a[2];
1753 result[3] = (GLfloat) (GLint) a[3];
1754 store_vector4(inst, machine, result);
1755 }
1756 break;
Briane80d9012007-02-23 16:53:24 -07001757 case OPCODE_UP2H: /* unpack two 16-bit floats */
1758 {
Brian Paul8d1a01d2010-01-22 15:36:28 -07001759 const GLuint raw = fetch_vector1ui(&inst->SrcReg[0], machine);
1760 GLfloat result[4];
Brian Paul7b5ad232010-01-22 16:09:03 -07001761 GLushort hx, hy;
Brian Paul8d1a01d2010-01-22 15:36:28 -07001762 hx = raw & 0xffff;
1763 hy = raw >> 16;
Briane80d9012007-02-23 16:53:24 -07001764 result[0] = result[2] = _mesa_half_to_float(hx);
1765 result[1] = result[3] = _mesa_half_to_float(hy);
1766 store_vector4(inst, machine, result);
1767 }
1768 break;
1769 case OPCODE_UP2US: /* unpack two GLushorts */
1770 {
Brian Paul8d1a01d2010-01-22 15:36:28 -07001771 const GLuint raw = fetch_vector1ui(&inst->SrcReg[0], machine);
1772 GLfloat result[4];
Briane80d9012007-02-23 16:53:24 -07001773 GLushort usx, usy;
Brian Paul8d1a01d2010-01-22 15:36:28 -07001774 usx = raw & 0xffff;
1775 usy = raw >> 16;
Briane80d9012007-02-23 16:53:24 -07001776 result[0] = result[2] = usx * (1.0f / 65535.0f);
1777 result[1] = result[3] = usy * (1.0f / 65535.0f);
1778 store_vector4(inst, machine, result);
1779 }
1780 break;
1781 case OPCODE_UP4B: /* unpack four GLbytes */
1782 {
Brian Paul8d1a01d2010-01-22 15:36:28 -07001783 const GLuint raw = fetch_vector1ui(&inst->SrcReg[0], machine);
1784 GLfloat result[4];
1785 result[0] = (((raw >> 0) & 0xff) - 128) / 127.0F;
1786 result[1] = (((raw >> 8) & 0xff) - 128) / 127.0F;
1787 result[2] = (((raw >> 16) & 0xff) - 128) / 127.0F;
1788 result[3] = (((raw >> 24) & 0xff) - 128) / 127.0F;
Briane80d9012007-02-23 16:53:24 -07001789 store_vector4(inst, machine, result);
1790 }
1791 break;
1792 case OPCODE_UP4UB: /* unpack four GLubytes */
1793 {
Brian Paul8d1a01d2010-01-22 15:36:28 -07001794 const GLuint raw = fetch_vector1ui(&inst->SrcReg[0], machine);
1795 GLfloat result[4];
1796 result[0] = ((raw >> 0) & 0xff) / 255.0F;
1797 result[1] = ((raw >> 8) & 0xff) / 255.0F;
1798 result[2] = ((raw >> 16) & 0xff) / 255.0F;
1799 result[3] = ((raw >> 24) & 0xff) / 255.0F;
Briane80d9012007-02-23 16:53:24 -07001800 store_vector4(inst, machine, result);
1801 }
1802 break;
Brian Paul37eef7b2008-11-07 09:33:55 -07001803 case OPCODE_XOR: /* bitwise XOR */
1804 {
1805 GLuint a[4], b[4], result[4];
1806 fetch_vector4ui(&inst->SrcReg[0], machine, a);
1807 fetch_vector4ui(&inst->SrcReg[1], machine, b);
1808 result[0] = a[0] ^ b[0];
1809 result[1] = a[1] ^ b[1];
1810 result[2] = a[2] ^ b[2];
1811 result[3] = a[3] ^ b[3];
1812 store_vector4ui(inst, machine, result);
1813 }
1814 break;
Briane80d9012007-02-23 16:53:24 -07001815 case OPCODE_XPD: /* cross product */
1816 {
1817 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001818 fetch_vector4(&inst->SrcReg[0], machine, a);
1819 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001820 result[0] = a[1] * b[2] - a[2] * b[1];
1821 result[1] = a[2] * b[0] - a[0] * b[2];
1822 result[2] = a[0] * b[1] - a[1] * b[0];
1823 result[3] = 1.0;
1824 store_vector4(inst, machine, result);
Brian9637c962007-03-07 17:40:57 -07001825 if (DEBUG_PROG) {
1826 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1827 result[0], result[1], result[2], result[3],
1828 a[0], a[1], a[2], b[0], b[1], b[2]);
1829 }
Briane80d9012007-02-23 16:53:24 -07001830 }
1831 break;
1832 case OPCODE_X2D: /* 2-D matrix transform */
1833 {
1834 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001835 fetch_vector4(&inst->SrcReg[0], machine, a);
1836 fetch_vector4(&inst->SrcReg[1], machine, b);
1837 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -07001838 result[0] = a[0] + b[0] * c[0] + b[1] * c[1];
1839 result[1] = a[1] + b[0] * c[2] + b[1] * c[3];
1840 result[2] = a[2] + b[0] * c[0] + b[1] * c[1];
1841 result[3] = a[3] + b[0] * c[2] + b[1] * c[3];
1842 store_vector4(inst, machine, result);
1843 }
1844 break;
1845 case OPCODE_PRINT:
1846 {
Vinson Lee4e981162010-04-18 00:11:49 -07001847 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
Vinson Lee45df4ba2010-03-04 17:40:36 -08001848 GLfloat a[4];
1849 fetch_vector4(&inst->SrcReg[0], machine, a);
1850 printf("%s%g, %g, %g, %g\n", (const char *) inst->Data,
1851 a[0], a[1], a[2], a[3]);
1852 }
1853 else {
1854 printf("%s\n", (const char *) inst->Data);
1855 }
Briane80d9012007-02-23 16:53:24 -07001856 }
1857 break;
1858 case OPCODE_END:
1859 return GL_TRUE;
1860 default:
Alan Hourihane936dba12008-04-22 20:28:35 +01001861 _mesa_problem(ctx, "Bad opcode %d in _mesa_execute_program",
Briane80d9012007-02-23 16:53:24 -07001862 inst->Opcode);
1863 return GL_TRUE; /* return value doesn't matter */
Brian13e3b212007-02-22 16:09:40 -07001864 }
Briane80d9012007-02-23 16:53:24 -07001865
Briancfd00112007-02-25 18:30:45 -07001866 numExec++;
1867 if (numExec > maxExec) {
Eric Anholtcc15ef02010-07-22 12:16:11 -07001868 static GLboolean reported = GL_FALSE;
1869 if (!reported) {
1870 _mesa_problem(ctx, "Infinite loop detected in fragment program");
1871 reported = GL_TRUE;
1872 }
Brian13e3b212007-02-22 16:09:40 -07001873 return GL_TRUE;
Brian13e3b212007-02-22 16:09:40 -07001874 }
Briane80d9012007-02-23 16:53:24 -07001875
1876 } /* for pc */
Brian13e3b212007-02-22 16:09:40 -07001877
Brian13e3b212007-02-22 16:09:40 -07001878 return GL_TRUE;
1879}