blob: 68a59350a1388b3b735d16d7429fc853ae7fee1d [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 }
228}
229
Brian62da6a12007-05-02 18:44:34 -0600230
Brian13e3b212007-02-22 16:09:40 -0700231/**
Brian Paul37eef7b2008-11-07 09:33:55 -0700232 * Fetch a 4-element uint vector from the given source register.
233 * Apply swizzling but not negation/abs.
234 */
235static void
236fetch_vector4ui(const struct prog_src_register *source,
237 const struct gl_program_machine *machine, GLuint result[4])
238{
Brian Paulf4361542008-11-11 10:47:10 -0700239 const GLuint *src = (GLuint *) get_src_register_pointer(source, machine);
Brian Paul37eef7b2008-11-07 09:33:55 -0700240 ASSERT(src);
241
242 if (source->Swizzle == SWIZZLE_NOOP) {
243 /* no swizzling */
244 COPY_4V(result, src);
245 }
246 else {
247 ASSERT(GET_SWZ(source->Swizzle, 0) <= 3);
248 ASSERT(GET_SWZ(source->Swizzle, 1) <= 3);
249 ASSERT(GET_SWZ(source->Swizzle, 2) <= 3);
250 ASSERT(GET_SWZ(source->Swizzle, 3) <= 3);
251 result[0] = src[GET_SWZ(source->Swizzle, 0)];
252 result[1] = src[GET_SWZ(source->Swizzle, 1)];
253 result[2] = src[GET_SWZ(source->Swizzle, 2)];
254 result[3] = src[GET_SWZ(source->Swizzle, 3)];
255 }
256
Brian Paul7db7ff82009-04-14 22:14:30 -0600257 /* Note: no Negate or Abs here */
Brian Paul37eef7b2008-11-07 09:33:55 -0700258}
259
260
261
262/**
Brian62da6a12007-05-02 18:44:34 -0600263 * Fetch the derivative with respect to X or Y for the given register.
264 * XXX this currently only works for fragment program input attribs.
Brian13e3b212007-02-22 16:09:40 -0700265 */
Brian62da6a12007-05-02 18:44:34 -0600266static void
Briane80d9012007-02-23 16:53:24 -0700267fetch_vector4_deriv(GLcontext * ctx,
268 const struct prog_src_register *source,
Brian62da6a12007-05-02 18:44:34 -0600269 const struct gl_program_machine *machine,
270 char xOrY, GLfloat result[4])
Brian13e3b212007-02-22 16:09:40 -0700271{
Brian Paul37eef7b2008-11-07 09:33:55 -0700272 if (source->File == PROGRAM_INPUT &&
273 source->Index < (GLint) machine->NumDeriv) {
Brian62da6a12007-05-02 18:44:34 -0600274 const GLint col = machine->CurElement;
275 const GLfloat w = machine->Attribs[FRAG_ATTRIB_WPOS][col][3];
276 const GLfloat invQ = 1.0f / w;
277 GLfloat deriv[4];
Brian13e3b212007-02-22 16:09:40 -0700278
Brian13e3b212007-02-22 16:09:40 -0700279 if (xOrY == 'X') {
Brian62da6a12007-05-02 18:44:34 -0600280 deriv[0] = machine->DerivX[source->Index][0] * invQ;
281 deriv[1] = machine->DerivX[source->Index][1] * invQ;
282 deriv[2] = machine->DerivX[source->Index][2] * invQ;
283 deriv[3] = machine->DerivX[source->Index][3] * invQ;
Brian13e3b212007-02-22 16:09:40 -0700284 }
285 else {
Brian62da6a12007-05-02 18:44:34 -0600286 deriv[0] = machine->DerivY[source->Index][0] * invQ;
287 deriv[1] = machine->DerivY[source->Index][1] * invQ;
288 deriv[2] = machine->DerivY[source->Index][2] * invQ;
289 deriv[3] = machine->DerivY[source->Index][3] * invQ;
Brian13e3b212007-02-22 16:09:40 -0700290 }
Brian13e3b212007-02-22 16:09:40 -0700291
Brian62da6a12007-05-02 18:44:34 -0600292 result[0] = deriv[GET_SWZ(source->Swizzle, 0)];
293 result[1] = deriv[GET_SWZ(source->Swizzle, 1)];
294 result[2] = deriv[GET_SWZ(source->Swizzle, 2)];
295 result[3] = deriv[GET_SWZ(source->Swizzle, 3)];
296
Brian62da6a12007-05-02 18:44:34 -0600297 if (source->Abs) {
298 result[0] = FABSF(result[0]);
299 result[1] = FABSF(result[1]);
300 result[2] = FABSF(result[2]);
301 result[3] = FABSF(result[3]);
302 }
Brian Paul7db7ff82009-04-14 22:14:30 -0600303 if (source->Negate) {
304 ASSERT(source->Negate == NEGATE_XYZW);
Brian62da6a12007-05-02 18:44:34 -0600305 result[0] = -result[0];
306 result[1] = -result[1];
307 result[2] = -result[2];
308 result[3] = -result[3];
309 }
Brian13e3b212007-02-22 16:09:40 -0700310 }
Brian62da6a12007-05-02 18:44:34 -0600311 else {
312 ASSIGN_4V(result, 0.0, 0.0, 0.0, 0.0);
Brian13e3b212007-02-22 16:09:40 -0700313 }
Brian13e3b212007-02-22 16:09:40 -0700314}
Brian13e3b212007-02-22 16:09:40 -0700315
316
317/**
318 * As above, but only return result[0] element.
319 */
320static void
Brian33eac562007-02-25 18:52:41 -0700321fetch_vector1(const struct prog_src_register *source,
Briane80d9012007-02-23 16:53:24 -0700322 const struct gl_program_machine *machine, GLfloat result[4])
Brian13e3b212007-02-22 16:09:40 -0700323{
Brian Paulf4361542008-11-11 10:47:10 -0700324 const GLfloat *src = get_src_register_pointer(source, machine);
Brian13e3b212007-02-22 16:09:40 -0700325 ASSERT(src);
326
327 result[0] = src[GET_SWZ(source->Swizzle, 0)];
328
Brian13e3b212007-02-22 16:09:40 -0700329 if (source->Abs) {
330 result[0] = FABSF(result[0]);
331 }
Brian Paul7db7ff82009-04-14 22:14:30 -0600332 if (source->Negate) {
Brian13e3b212007-02-22 16:09:40 -0700333 result[0] = -result[0];
334 }
335}
336
337
338/**
Brian999b5562007-11-23 12:01:57 -0700339 * Fetch texel from texture. Use partial derivatives when possible.
340 */
341static INLINE void
342fetch_texel(GLcontext *ctx,
343 const struct gl_program_machine *machine,
344 const struct prog_instruction *inst,
345 const GLfloat texcoord[4], GLfloat lodBias,
346 GLfloat color[4])
347{
Brian Paulade50832008-05-14 16:09:46 -0600348 const GLuint unit = machine->Samplers[inst->TexSrcUnit];
349
Brian999b5562007-11-23 12:01:57 -0700350 /* Note: we only have the right derivatives for fragment input attribs.
351 */
352 if (machine->NumDeriv > 0 &&
353 inst->SrcReg[0].File == PROGRAM_INPUT &&
354 inst->SrcReg[0].Index == FRAG_ATTRIB_TEX0 + inst->TexSrcUnit) {
355 /* simple texture fetch for which we should have derivatives */
356 GLuint attr = inst->SrcReg[0].Index;
357 machine->FetchTexelDeriv(ctx, texcoord,
358 machine->DerivX[attr],
359 machine->DerivY[attr],
Brian Paulade50832008-05-14 16:09:46 -0600360 lodBias, unit, color);
Brian999b5562007-11-23 12:01:57 -0700361 }
362 else {
Brian Paulade50832008-05-14 16:09:46 -0600363 machine->FetchTexelLod(ctx, texcoord, lodBias, unit, color);
Brian999b5562007-11-23 12:01:57 -0700364 }
365}
366
367
368/**
Brian13e3b212007-02-22 16:09:40 -0700369 * Test value against zero and return GT, LT, EQ or UN if NaN.
370 */
371static INLINE GLuint
Briane80d9012007-02-23 16:53:24 -0700372generate_cc(float value)
Brian13e3b212007-02-22 16:09:40 -0700373{
374 if (value != value)
Briane80d9012007-02-23 16:53:24 -0700375 return COND_UN; /* NaN */
Brian13e3b212007-02-22 16:09:40 -0700376 if (value > 0.0F)
377 return COND_GT;
378 if (value < 0.0F)
379 return COND_LT;
380 return COND_EQ;
381}
382
383
384/**
385 * Test if the ccMaskRule is satisfied by the given condition code.
386 * Used to mask destination writes according to the current condition code.
387 */
388static INLINE GLboolean
389test_cc(GLuint condCode, GLuint ccMaskRule)
390{
391 switch (ccMaskRule) {
392 case COND_EQ: return (condCode == COND_EQ);
393 case COND_NE: return (condCode != COND_EQ);
394 case COND_LT: return (condCode == COND_LT);
395 case COND_GE: return (condCode == COND_GT || condCode == COND_EQ);
396 case COND_LE: return (condCode == COND_LT || condCode == COND_EQ);
397 case COND_GT: return (condCode == COND_GT);
398 case COND_TR: return GL_TRUE;
399 case COND_FL: return GL_FALSE;
400 default: return GL_TRUE;
401 }
402}
403
404
405/**
406 * Evaluate the 4 condition codes against a predicate and return GL_TRUE
407 * or GL_FALSE to indicate result.
408 */
409static INLINE GLboolean
410eval_condition(const struct gl_program_machine *machine,
411 const struct prog_instruction *inst)
412{
413 const GLuint swizzle = inst->DstReg.CondSwizzle;
414 const GLuint condMask = inst->DstReg.CondMask;
415 if (test_cc(machine->CondCodes[GET_SWZ(swizzle, 0)], condMask) ||
416 test_cc(machine->CondCodes[GET_SWZ(swizzle, 1)], condMask) ||
417 test_cc(machine->CondCodes[GET_SWZ(swizzle, 2)], condMask) ||
418 test_cc(machine->CondCodes[GET_SWZ(swizzle, 3)], condMask)) {
419 return GL_TRUE;
420 }
421 else {
422 return GL_FALSE;
423 }
424}
425
426
427
428/**
429 * Store 4 floats into a register. Observe the instructions saturate and
430 * set-condition-code flags.
431 */
432static void
Briane80d9012007-02-23 16:53:24 -0700433store_vector4(const struct prog_instruction *inst,
434 struct gl_program_machine *machine, const GLfloat value[4])
Brian13e3b212007-02-22 16:09:40 -0700435{
Brian Paulf4361542008-11-11 10:47:10 -0700436 const struct prog_dst_register *dstReg = &(inst->DstReg);
Brian13e3b212007-02-22 16:09:40 -0700437 const GLboolean clamp = inst->SaturateMode == SATURATE_ZERO_ONE;
Brian Paulf4361542008-11-11 10:47:10 -0700438 GLuint writeMask = dstReg->WriteMask;
Brian13e3b212007-02-22 16:09:40 -0700439 GLfloat clampedValue[4];
Brian Paulf4361542008-11-11 10:47:10 -0700440 GLfloat *dst = get_dst_register_pointer(dstReg, machine);
Brian13e3b212007-02-22 16:09:40 -0700441
442#if 0
443 if (value[0] > 1.0e10 ||
444 IS_INF_OR_NAN(value[0]) ||
445 IS_INF_OR_NAN(value[1]) ||
Briane80d9012007-02-23 16:53:24 -0700446 IS_INF_OR_NAN(value[2]) || IS_INF_OR_NAN(value[3]))
Brian13e3b212007-02-22 16:09:40 -0700447 printf("store %g %g %g %g\n", value[0], value[1], value[2], value[3]);
448#endif
449
450 if (clamp) {
451 clampedValue[0] = CLAMP(value[0], 0.0F, 1.0F);
452 clampedValue[1] = CLAMP(value[1], 0.0F, 1.0F);
453 clampedValue[2] = CLAMP(value[2], 0.0F, 1.0F);
454 clampedValue[3] = CLAMP(value[3], 0.0F, 1.0F);
455 value = clampedValue;
456 }
457
Brian Paulf4361542008-11-11 10:47:10 -0700458 if (dstReg->CondMask != COND_TR) {
Brian13e3b212007-02-22 16:09:40 -0700459 /* condition codes may turn off some writes */
460 if (writeMask & WRITEMASK_X) {
Brian Paulf4361542008-11-11 10:47:10 -0700461 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 0)],
462 dstReg->CondMask))
Brian13e3b212007-02-22 16:09:40 -0700463 writeMask &= ~WRITEMASK_X;
464 }
465 if (writeMask & WRITEMASK_Y) {
Brian Paulf4361542008-11-11 10:47:10 -0700466 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 1)],
467 dstReg->CondMask))
Brian13e3b212007-02-22 16:09:40 -0700468 writeMask &= ~WRITEMASK_Y;
469 }
470 if (writeMask & WRITEMASK_Z) {
Brian Paulf4361542008-11-11 10:47:10 -0700471 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 2)],
472 dstReg->CondMask))
Brian13e3b212007-02-22 16:09:40 -0700473 writeMask &= ~WRITEMASK_Z;
474 }
475 if (writeMask & WRITEMASK_W) {
Brian Paulf4361542008-11-11 10:47:10 -0700476 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 3)],
477 dstReg->CondMask))
Brian13e3b212007-02-22 16:09:40 -0700478 writeMask &= ~WRITEMASK_W;
479 }
480 }
481
482 if (writeMask & WRITEMASK_X)
Brian Paulf4361542008-11-11 10:47:10 -0700483 dst[0] = value[0];
Brian13e3b212007-02-22 16:09:40 -0700484 if (writeMask & WRITEMASK_Y)
Brian Paulf4361542008-11-11 10:47:10 -0700485 dst[1] = value[1];
Brian13e3b212007-02-22 16:09:40 -0700486 if (writeMask & WRITEMASK_Z)
Brian Paulf4361542008-11-11 10:47:10 -0700487 dst[2] = value[2];
Brian13e3b212007-02-22 16:09:40 -0700488 if (writeMask & WRITEMASK_W)
Brian Paulf4361542008-11-11 10:47:10 -0700489 dst[3] = value[3];
Brian13e3b212007-02-22 16:09:40 -0700490
491 if (inst->CondUpdate) {
492 if (writeMask & WRITEMASK_X)
493 machine->CondCodes[0] = generate_cc(value[0]);
494 if (writeMask & WRITEMASK_Y)
495 machine->CondCodes[1] = generate_cc(value[1]);
496 if (writeMask & WRITEMASK_Z)
497 machine->CondCodes[2] = generate_cc(value[2]);
498 if (writeMask & WRITEMASK_W)
499 machine->CondCodes[3] = generate_cc(value[3]);
Briana01616e2007-03-28 11:01:28 -0600500#if DEBUG_PROG
501 printf("CondCodes=(%s,%s,%s,%s) for:\n",
502 _mesa_condcode_string(machine->CondCodes[0]),
503 _mesa_condcode_string(machine->CondCodes[1]),
504 _mesa_condcode_string(machine->CondCodes[2]),
505 _mesa_condcode_string(machine->CondCodes[3]));
506#endif
Brian13e3b212007-02-22 16:09:40 -0700507 }
508}
509
510
Brian13e3b212007-02-22 16:09:40 -0700511/**
Brian Paul37eef7b2008-11-07 09:33:55 -0700512 * Store 4 uints into a register. Observe the set-condition-code flags.
513 */
514static void
515store_vector4ui(const struct prog_instruction *inst,
516 struct gl_program_machine *machine, const GLuint value[4])
517{
Brian Paulf4361542008-11-11 10:47:10 -0700518 const struct prog_dst_register *dstReg = &(inst->DstReg);
519 GLuint writeMask = dstReg->WriteMask;
520 GLuint *dst = (GLuint *) get_dst_register_pointer(dstReg, machine);
Brian Paul37eef7b2008-11-07 09:33:55 -0700521
Brian Paulf4361542008-11-11 10:47:10 -0700522 if (dstReg->CondMask != COND_TR) {
Brian Paul37eef7b2008-11-07 09:33:55 -0700523 /* condition codes may turn off some writes */
524 if (writeMask & WRITEMASK_X) {
Brian Paulf4361542008-11-11 10:47:10 -0700525 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 0)],
526 dstReg->CondMask))
Brian Paul37eef7b2008-11-07 09:33:55 -0700527 writeMask &= ~WRITEMASK_X;
528 }
529 if (writeMask & WRITEMASK_Y) {
Brian Paulf4361542008-11-11 10:47:10 -0700530 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 1)],
531 dstReg->CondMask))
Brian Paul37eef7b2008-11-07 09:33:55 -0700532 writeMask &= ~WRITEMASK_Y;
533 }
534 if (writeMask & WRITEMASK_Z) {
Brian Paulf4361542008-11-11 10:47:10 -0700535 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 2)],
536 dstReg->CondMask))
Brian Paul37eef7b2008-11-07 09:33:55 -0700537 writeMask &= ~WRITEMASK_Z;
538 }
539 if (writeMask & WRITEMASK_W) {
Brian Paulf4361542008-11-11 10:47:10 -0700540 if (!test_cc(machine->CondCodes[GET_SWZ(dstReg->CondSwizzle, 3)],
541 dstReg->CondMask))
Brian Paul37eef7b2008-11-07 09:33:55 -0700542 writeMask &= ~WRITEMASK_W;
543 }
544 }
545
546 if (writeMask & WRITEMASK_X)
Brian Paulf4361542008-11-11 10:47:10 -0700547 dst[0] = value[0];
Brian Paul37eef7b2008-11-07 09:33:55 -0700548 if (writeMask & WRITEMASK_Y)
Brian Paulf4361542008-11-11 10:47:10 -0700549 dst[1] = value[1];
Brian Paul37eef7b2008-11-07 09:33:55 -0700550 if (writeMask & WRITEMASK_Z)
Brian Paulf4361542008-11-11 10:47:10 -0700551 dst[2] = value[2];
Brian Paul37eef7b2008-11-07 09:33:55 -0700552 if (writeMask & WRITEMASK_W)
Brian Paulf4361542008-11-11 10:47:10 -0700553 dst[3] = value[3];
Brian Paul37eef7b2008-11-07 09:33:55 -0700554
555 if (inst->CondUpdate) {
556 if (writeMask & WRITEMASK_X)
557 machine->CondCodes[0] = generate_cc(value[0]);
558 if (writeMask & WRITEMASK_Y)
559 machine->CondCodes[1] = generate_cc(value[1]);
560 if (writeMask & WRITEMASK_Z)
561 machine->CondCodes[2] = generate_cc(value[2]);
562 if (writeMask & WRITEMASK_W)
563 machine->CondCodes[3] = generate_cc(value[3]);
564#if DEBUG_PROG
565 printf("CondCodes=(%s,%s,%s,%s) for:\n",
566 _mesa_condcode_string(machine->CondCodes[0]),
567 _mesa_condcode_string(machine->CondCodes[1]),
568 _mesa_condcode_string(machine->CondCodes[2]),
569 _mesa_condcode_string(machine->CondCodes[3]));
570#endif
571 }
572}
573
574
575
576/**
Brian13e3b212007-02-22 16:09:40 -0700577 * Execute the given vertex/fragment program.
578 *
Brian3c1c9992007-02-25 19:11:44 -0700579 * \param ctx rendering context
580 * \param program the program to execute
581 * \param machine machine state (must be initialized)
Brian13e3b212007-02-22 16:09:40 -0700582 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
583 */
584GLboolean
Briane80d9012007-02-23 16:53:24 -0700585_mesa_execute_program(GLcontext * ctx,
Brian8b34b7d2007-02-25 18:26:50 -0700586 const struct gl_program *program,
Brian085d7d52007-02-25 18:23:37 -0700587 struct gl_program_machine *machine)
Brian13e3b212007-02-22 16:09:40 -0700588{
Brian8b34b7d2007-02-25 18:26:50 -0700589 const GLuint numInst = program->NumInstructions;
Briancfd00112007-02-25 18:30:45 -0700590 const GLuint maxExec = 10000;
José Fonseca452a5922008-05-31 18:14:09 +0900591 GLuint pc, numExec = 0;
Brian13e3b212007-02-22 16:09:40 -0700592
593 machine->CurProgram = program;
594
595 if (DEBUG_PROG) {
596 printf("execute program %u --------------------\n", program->Id);
597 }
598
Brian33eac562007-02-25 18:52:41 -0700599 if (program->Target == GL_VERTEX_PROGRAM_ARB) {
600 machine->EnvParams = ctx->VertexProgram.Parameters;
601 }
602 else {
603 machine->EnvParams = ctx->FragmentProgram.Parameters;
604 }
605
Brian8b34b7d2007-02-25 18:26:50 -0700606 for (pc = 0; pc < numInst; pc++) {
Brian13e3b212007-02-22 16:09:40 -0700607 const struct prog_instruction *inst = program->Instructions + pc;
608
Brian13e3b212007-02-22 16:09:40 -0700609 if (DEBUG_PROG) {
610 _mesa_print_instruction(inst);
611 }
612
613 switch (inst->Opcode) {
Briane80d9012007-02-23 16:53:24 -0700614 case OPCODE_ABS:
615 {
616 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700617 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700618 result[0] = FABSF(a[0]);
619 result[1] = FABSF(a[1]);
620 result[2] = FABSF(a[2]);
621 result[3] = FABSF(a[3]);
622 store_vector4(inst, machine, result);
623 }
624 break;
625 case OPCODE_ADD:
626 {
627 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700628 fetch_vector4(&inst->SrcReg[0], machine, a);
629 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700630 result[0] = a[0] + b[0];
631 result[1] = a[1] + b[1];
632 result[2] = a[2] + b[2];
633 result[3] = a[3] + b[3];
634 store_vector4(inst, machine, result);
635 if (DEBUG_PROG) {
636 printf("ADD (%g %g %g %g) = (%g %g %g %g) + (%g %g %g %g)\n",
637 result[0], result[1], result[2], result[3],
638 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -0700639 }
Briane80d9012007-02-23 16:53:24 -0700640 }
641 break;
Brian Paul37eef7b2008-11-07 09:33:55 -0700642 case OPCODE_AND: /* bitwise AND */
643 {
644 GLuint a[4], b[4], result[4];
645 fetch_vector4ui(&inst->SrcReg[0], machine, a);
646 fetch_vector4ui(&inst->SrcReg[1], machine, b);
647 result[0] = a[0] & b[0];
648 result[1] = a[1] & b[1];
649 result[2] = a[2] & b[2];
650 result[3] = a[3] & b[3];
651 store_vector4ui(inst, machine, result);
652 }
653 break;
Brianf183a2d2007-02-23 17:14:30 -0700654 case OPCODE_ARL:
655 {
656 GLfloat t[4];
Brian33eac562007-02-25 18:52:41 -0700657 fetch_vector4(&inst->SrcReg[0], machine, t);
Brian Paula9475cc2008-12-12 18:03:48 -0700658 machine->AddressReg[0][0] = IFLOOR(t[0]);
Brianf183a2d2007-02-23 17:14:30 -0700659 }
660 break;
Briane80d9012007-02-23 16:53:24 -0700661 case OPCODE_BGNLOOP:
662 /* no-op */
663 break;
664 case OPCODE_ENDLOOP:
665 /* subtract 1 here since pc is incremented by for(pc) loop */
666 pc = inst->BranchTarget - 1; /* go to matching BNGLOOP */
667 break;
668 case OPCODE_BGNSUB: /* begin subroutine */
669 break;
670 case OPCODE_ENDSUB: /* end subroutine */
671 break;
672 case OPCODE_BRA: /* branch (conditional) */
673 /* fall-through */
674 case OPCODE_BRK: /* break out of loop (conditional) */
675 /* fall-through */
676 case OPCODE_CONT: /* continue loop (conditional) */
677 if (eval_condition(machine, inst)) {
678 /* take branch */
679 /* Subtract 1 here since we'll do pc++ at end of for-loop */
680 pc = inst->BranchTarget - 1;
681 }
682 break;
683 case OPCODE_CAL: /* Call subroutine (conditional) */
684 if (eval_condition(machine, inst)) {
685 /* call the subroutine */
686 if (machine->StackDepth >= MAX_PROGRAM_CALL_DEPTH) {
687 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
Brian13e3b212007-02-22 16:09:40 -0700688 }
Briana0275b02007-03-27 11:02:20 -0600689 machine->CallStack[machine->StackDepth++] = pc + 1; /* next inst */
Brian31dc7a32007-03-27 15:21:35 -0600690 /* Subtract 1 here since we'll do pc++ at end of for-loop */
691 pc = inst->BranchTarget - 1;
Briane80d9012007-02-23 16:53:24 -0700692 }
693 break;
694 case OPCODE_CMP:
695 {
696 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700697 fetch_vector4(&inst->SrcReg[0], machine, a);
698 fetch_vector4(&inst->SrcReg[1], machine, b);
699 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -0700700 result[0] = a[0] < 0.0F ? b[0] : c[0];
701 result[1] = a[1] < 0.0F ? b[1] : c[1];
702 result[2] = a[2] < 0.0F ? b[2] : c[2];
703 result[3] = a[3] < 0.0F ? b[3] : c[3];
704 store_vector4(inst, machine, result);
705 }
706 break;
707 case OPCODE_COS:
708 {
709 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700710 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700711 result[0] = result[1] = result[2] = result[3]
712 = (GLfloat) _mesa_cos(a[0]);
713 store_vector4(inst, machine, result);
714 }
715 break;
716 case OPCODE_DDX: /* Partial derivative with respect to X */
717 {
Brian62da6a12007-05-02 18:44:34 -0600718 GLfloat result[4];
719 fetch_vector4_deriv(ctx, &inst->SrcReg[0], machine,
720 'X', result);
Briane80d9012007-02-23 16:53:24 -0700721 store_vector4(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -0700722 }
723 break;
724 case OPCODE_DDY: /* Partial derivative with respect to Y */
725 {
Brian62da6a12007-05-02 18:44:34 -0600726 GLfloat result[4];
727 fetch_vector4_deriv(ctx, &inst->SrcReg[0], machine,
728 'Y', result);
Briane80d9012007-02-23 16:53:24 -0700729 store_vector4(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -0700730 }
731 break;
Brian Paul65cb74e2008-11-07 09:41:00 -0700732 case OPCODE_DP2:
733 {
734 GLfloat a[4], b[4], result[4];
735 fetch_vector4(&inst->SrcReg[0], machine, a);
736 fetch_vector4(&inst->SrcReg[1], machine, b);
737 result[0] = result[1] = result[2] = result[3] = DOT2(a, b);
738 store_vector4(inst, machine, result);
739 if (DEBUG_PROG) {
740 printf("DP2 %g = (%g %g) . (%g %g)\n",
741 result[0], a[0], a[1], b[0], b[1]);
742 }
743 }
744 break;
745 case OPCODE_DP2A:
746 {
747 GLfloat a[4], b[4], c, result[4];
748 fetch_vector4(&inst->SrcReg[0], machine, a);
749 fetch_vector4(&inst->SrcReg[1], machine, b);
750 fetch_vector1(&inst->SrcReg[1], machine, &c);
751 result[0] = result[1] = result[2] = result[3] = DOT2(a, b) + c;
752 store_vector4(inst, machine, result);
753 if (DEBUG_PROG) {
754 printf("DP2A %g = (%g %g) . (%g %g) + %g\n",
755 result[0], a[0], a[1], b[0], b[1], c);
756 }
757 }
758 break;
Briane80d9012007-02-23 16:53:24 -0700759 case OPCODE_DP3:
760 {
761 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700762 fetch_vector4(&inst->SrcReg[0], machine, a);
763 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700764 result[0] = result[1] = result[2] = result[3] = DOT3(a, b);
765 store_vector4(inst, machine, result);
766 if (DEBUG_PROG) {
767 printf("DP3 %g = (%g %g %g) . (%g %g %g)\n",
768 result[0], a[0], a[1], a[2], b[0], b[1], b[2]);
Brian13e3b212007-02-22 16:09:40 -0700769 }
Briane80d9012007-02-23 16:53:24 -0700770 }
771 break;
772 case OPCODE_DP4:
773 {
774 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700775 fetch_vector4(&inst->SrcReg[0], machine, a);
776 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700777 result[0] = result[1] = result[2] = result[3] = DOT4(a, b);
778 store_vector4(inst, machine, result);
779 if (DEBUG_PROG) {
780 printf("DP4 %g = (%g, %g %g %g) . (%g, %g %g %g)\n",
781 result[0], a[0], a[1], a[2], a[3],
782 b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -0700783 }
Briane80d9012007-02-23 16:53:24 -0700784 }
785 break;
786 case OPCODE_DPH:
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);
Brian Paul65cb74e2008-11-07 09:41:00 -0700791 result[0] = result[1] = result[2] = result[3] = DOT3(a, b) + b[3];
Briane80d9012007-02-23 16:53:24 -0700792 store_vector4(inst, machine, result);
793 }
794 break;
795 case OPCODE_DST: /* Distance vector */
796 {
797 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700798 fetch_vector4(&inst->SrcReg[0], machine, a);
799 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -0700800 result[0] = 1.0F;
801 result[1] = a[1] * b[1];
802 result[2] = a[2];
803 result[3] = b[3];
804 store_vector4(inst, machine, result);
805 }
806 break;
Brianf183a2d2007-02-23 17:14:30 -0700807 case OPCODE_EXP:
Brianf183a2d2007-02-23 17:14:30 -0700808 {
809 GLfloat t[4], q[4], floor_t0;
Brian33eac562007-02-25 18:52:41 -0700810 fetch_vector1(&inst->SrcReg[0], machine, t);
Brianf183a2d2007-02-23 17:14:30 -0700811 floor_t0 = FLOORF(t[0]);
812 if (floor_t0 > FLT_MAX_EXP) {
813 SET_POS_INFINITY(q[0]);
814 SET_POS_INFINITY(q[2]);
815 }
816 else if (floor_t0 < FLT_MIN_EXP) {
817 q[0] = 0.0F;
818 q[2] = 0.0F;
819 }
820 else {
Brian761728a2007-02-24 11:14:57 -0700821 q[0] = LDEXPF(1.0, (int) floor_t0);
822 /* Note: GL_NV_vertex_program expects
823 * result.z = result.x * APPX(result.y)
824 * We do what the ARB extension says.
825 */
Brian Paulb8a200a2009-04-03 10:29:11 -0600826 q[2] = (GLfloat) _mesa_pow(2.0, t[0]);
Brianf183a2d2007-02-23 17:14:30 -0700827 }
828 q[1] = t[0] - floor_t0;
829 q[3] = 1.0F;
830 store_vector4( inst, machine, q );
831 }
832 break;
Briane80d9012007-02-23 16:53:24 -0700833 case OPCODE_EX2: /* Exponential base 2 */
834 {
835 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700836 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700837 result[0] = result[1] = result[2] = result[3] =
838 (GLfloat) _mesa_pow(2.0, a[0]);
839 store_vector4(inst, machine, result);
840 }
841 break;
842 case OPCODE_FLR:
843 {
844 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700845 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700846 result[0] = FLOORF(a[0]);
847 result[1] = FLOORF(a[1]);
848 result[2] = FLOORF(a[2]);
849 result[3] = FLOORF(a[3]);
850 store_vector4(inst, machine, result);
851 }
852 break;
853 case OPCODE_FRC:
854 {
855 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700856 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700857 result[0] = a[0] - FLOORF(a[0]);
858 result[1] = a[1] - FLOORF(a[1]);
859 result[2] = a[2] - FLOORF(a[2]);
860 result[3] = a[3] - FLOORF(a[3]);
861 store_vector4(inst, machine, result);
862 }
863 break;
864 case OPCODE_IF:
Brian63556fa2007-03-23 14:47:46 -0600865 {
866 GLboolean cond;
867 /* eval condition */
868 if (inst->SrcReg[0].File != PROGRAM_UNDEFINED) {
869 GLfloat a[4];
870 fetch_vector1(&inst->SrcReg[0], machine, a);
871 cond = (a[0] != 0.0);
872 }
873 else {
874 cond = eval_condition(machine, inst);
875 }
Briana7f73662007-04-20 08:11:51 -0600876 if (DEBUG_PROG) {
877 printf("IF: %d\n", cond);
878 }
Brian63556fa2007-03-23 14:47:46 -0600879 /* do if/else */
880 if (cond) {
881 /* do if-clause (just continue execution) */
882 }
883 else {
884 /* go to the instruction after ELSE or ENDIF */
885 assert(inst->BranchTarget >= 0);
886 pc = inst->BranchTarget - 1;
887 }
Briane80d9012007-02-23 16:53:24 -0700888 }
889 break;
890 case OPCODE_ELSE:
891 /* goto ENDIF */
892 assert(inst->BranchTarget >= 0);
893 pc = inst->BranchTarget - 1;
894 break;
895 case OPCODE_ENDIF:
896 /* nothing */
897 break;
Briane80d9012007-02-23 16:53:24 -0700898 case OPCODE_KIL_NV: /* NV_f_p only (conditional) */
899 if (eval_condition(machine, inst)) {
900 return GL_FALSE;
901 }
902 break;
903 case OPCODE_KIL: /* ARB_f_p only */
904 {
905 GLfloat a[4];
Brian33eac562007-02-25 18:52:41 -0700906 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700907 if (a[0] < 0.0F || a[1] < 0.0F || a[2] < 0.0F || a[3] < 0.0F) {
Brian13e3b212007-02-22 16:09:40 -0700908 return GL_FALSE;
909 }
Briane80d9012007-02-23 16:53:24 -0700910 }
911 break;
912 case OPCODE_LG2: /* log base 2 */
913 {
914 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700915 fetch_vector1(&inst->SrcReg[0], machine, a);
Ian Romanick962fa6b2008-12-18 14:11:06 -0800916 /* The fast LOG2 macro doesn't meet the precision requirements.
917 */
918 result[0] = result[1] = result[2] = result[3] =
919 (log(a[0]) * 1.442695F);
Briane80d9012007-02-23 16:53:24 -0700920 store_vector4(inst, machine, result);
921 }
922 break;
923 case OPCODE_LIT:
924 {
925 const GLfloat epsilon = 1.0F / 256.0F; /* from NV VP spec */
926 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700927 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -0700928 a[0] = MAX2(a[0], 0.0F);
929 a[1] = MAX2(a[1], 0.0F);
930 /* XXX ARB version clamps a[3], NV version doesn't */
931 a[3] = CLAMP(a[3], -(128.0F - epsilon), (128.0F - epsilon));
932 result[0] = 1.0F;
933 result[1] = a[0];
934 /* XXX we could probably just use pow() here */
935 if (a[0] > 0.0F) {
936 if (a[1] == 0.0 && a[3] == 0.0)
937 result[2] = 1.0;
938 else
Brian Paulb8a200a2009-04-03 10:29:11 -0600939 result[2] = (GLfloat) _mesa_pow(a[1], a[3]);
Brian13e3b212007-02-22 16:09:40 -0700940 }
Briane80d9012007-02-23 16:53:24 -0700941 else {
942 result[2] = 0.0;
Brian13e3b212007-02-22 16:09:40 -0700943 }
Briane80d9012007-02-23 16:53:24 -0700944 result[3] = 1.0F;
945 store_vector4(inst, machine, result);
946 if (DEBUG_PROG) {
947 printf("LIT (%g %g %g %g) : (%g %g %g %g)\n",
948 result[0], result[1], result[2], result[3],
949 a[0], a[1], a[2], a[3]);
Brian13e3b212007-02-22 16:09:40 -0700950 }
Briane80d9012007-02-23 16:53:24 -0700951 }
952 break;
Brianf183a2d2007-02-23 17:14:30 -0700953 case OPCODE_LOG:
954 {
955 GLfloat t[4], q[4], abs_t0;
Brian33eac562007-02-25 18:52:41 -0700956 fetch_vector1(&inst->SrcReg[0], machine, t);
Brianf183a2d2007-02-23 17:14:30 -0700957 abs_t0 = FABSF(t[0]);
958 if (abs_t0 != 0.0F) {
959 /* Since we really can't handle infinite values on VMS
960 * like other OSes we'll use __MAXFLOAT to represent
961 * infinity. This may need some tweaking.
962 */
963#ifdef VMS
964 if (abs_t0 == __MAXFLOAT)
965#else
966 if (IS_INF_OR_NAN(abs_t0))
967#endif
968 {
969 SET_POS_INFINITY(q[0]);
970 q[1] = 1.0F;
971 SET_POS_INFINITY(q[2]);
972 }
973 else {
974 int exponent;
975 GLfloat mantissa = FREXPF(t[0], &exponent);
976 q[0] = (GLfloat) (exponent - 1);
977 q[1] = (GLfloat) (2.0 * mantissa); /* map [.5, 1) -> [1, 2) */
Ian Romanick962fa6b2008-12-18 14:11:06 -0800978
979 /* The fast LOG2 macro doesn't meet the precision
980 * requirements.
981 */
982 q[2] = (log(t[0]) * 1.442695F);
Brianf183a2d2007-02-23 17:14:30 -0700983 }
984 }
985 else {
986 SET_NEG_INFINITY(q[0]);
987 q[1] = 1.0F;
988 SET_NEG_INFINITY(q[2]);
989 }
990 q[3] = 1.0;
991 store_vector4(inst, machine, q);
992 }
993 break;
Briane80d9012007-02-23 16:53:24 -0700994 case OPCODE_LRP:
995 {
996 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -0700997 fetch_vector4(&inst->SrcReg[0], machine, a);
998 fetch_vector4(&inst->SrcReg[1], machine, b);
999 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -07001000 result[0] = a[0] * b[0] + (1.0F - a[0]) * c[0];
1001 result[1] = a[1] * b[1] + (1.0F - a[1]) * c[1];
1002 result[2] = a[2] * b[2] + (1.0F - a[2]) * c[2];
1003 result[3] = a[3] * b[3] + (1.0F - a[3]) * c[3];
1004 store_vector4(inst, machine, result);
1005 if (DEBUG_PROG) {
1006 printf("LRP (%g %g %g %g) = (%g %g %g %g), "
1007 "(%g %g %g %g), (%g %g %g %g)\n",
1008 result[0], result[1], result[2], result[3],
1009 a[0], a[1], a[2], a[3],
1010 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
Brian13e3b212007-02-22 16:09:40 -07001011 }
Briane80d9012007-02-23 16:53:24 -07001012 }
1013 break;
1014 case OPCODE_MAD:
1015 {
1016 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001017 fetch_vector4(&inst->SrcReg[0], machine, a);
1018 fetch_vector4(&inst->SrcReg[1], machine, b);
1019 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -07001020 result[0] = a[0] * b[0] + c[0];
1021 result[1] = a[1] * b[1] + c[1];
1022 result[2] = a[2] * b[2] + c[2];
1023 result[3] = a[3] * b[3] + c[3];
1024 store_vector4(inst, machine, result);
1025 if (DEBUG_PROG) {
1026 printf("MAD (%g %g %g %g) = (%g %g %g %g) * "
1027 "(%g %g %g %g) + (%g %g %g %g)\n",
1028 result[0], result[1], result[2], result[3],
1029 a[0], a[1], a[2], a[3],
1030 b[0], b[1], b[2], b[3], c[0], c[1], c[2], c[3]);
Brian13e3b212007-02-22 16:09:40 -07001031 }
Briane80d9012007-02-23 16:53:24 -07001032 }
1033 break;
1034 case OPCODE_MAX:
1035 {
1036 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001037 fetch_vector4(&inst->SrcReg[0], machine, a);
1038 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001039 result[0] = MAX2(a[0], b[0]);
1040 result[1] = MAX2(a[1], b[1]);
1041 result[2] = MAX2(a[2], b[2]);
1042 result[3] = MAX2(a[3], b[3]);
1043 store_vector4(inst, machine, result);
1044 if (DEBUG_PROG) {
1045 printf("MAX (%g %g %g %g) = (%g %g %g %g), (%g %g %g %g)\n",
1046 result[0], result[1], result[2], result[3],
1047 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001048 }
Briane80d9012007-02-23 16:53:24 -07001049 }
1050 break;
1051 case OPCODE_MIN:
1052 {
1053 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001054 fetch_vector4(&inst->SrcReg[0], machine, a);
1055 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001056 result[0] = MIN2(a[0], b[0]);
1057 result[1] = MIN2(a[1], b[1]);
1058 result[2] = MIN2(a[2], b[2]);
1059 result[3] = MIN2(a[3], b[3]);
1060 store_vector4(inst, machine, result);
1061 }
1062 break;
1063 case OPCODE_MOV:
1064 {
1065 GLfloat result[4];
Brian33eac562007-02-25 18:52:41 -07001066 fetch_vector4(&inst->SrcReg[0], machine, result);
Briane80d9012007-02-23 16:53:24 -07001067 store_vector4(inst, machine, result);
1068 if (DEBUG_PROG) {
1069 printf("MOV (%g %g %g %g)\n",
1070 result[0], result[1], result[2], result[3]);
Brian13e3b212007-02-22 16:09:40 -07001071 }
Briane80d9012007-02-23 16:53:24 -07001072 }
1073 break;
1074 case OPCODE_MUL:
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] = a[0] * b[0];
1080 result[1] = a[1] * b[1];
1081 result[2] = a[2] * b[2];
1082 result[3] = a[3] * b[3];
1083 store_vector4(inst, machine, result);
1084 if (DEBUG_PROG) {
1085 printf("MUL (%g %g %g %g) = (%g %g %g %g) * (%g %g %g %g)\n",
1086 result[0], result[1], result[2], result[3],
1087 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001088 }
Briane80d9012007-02-23 16:53:24 -07001089 }
1090 break;
1091 case OPCODE_NOISE1:
1092 {
1093 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001094 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001095 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001096 result[1] =
Brian Paul702b5b02008-12-15 18:37:39 -07001097 result[2] =
1098 result[3] = _mesa_noise1(a[0]);
Briane80d9012007-02-23 16:53:24 -07001099 store_vector4(inst, machine, result);
1100 }
1101 break;
1102 case OPCODE_NOISE2:
1103 {
1104 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001105 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001106 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001107 result[1] =
Brian Paul702b5b02008-12-15 18:37:39 -07001108 result[2] = result[3] = _mesa_noise2(a[0], a[1]);
Briane80d9012007-02-23 16:53:24 -07001109 store_vector4(inst, machine, result);
1110 }
1111 break;
1112 case OPCODE_NOISE3:
1113 {
1114 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001115 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001116 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001117 result[1] =
1118 result[2] =
Brian Paul702b5b02008-12-15 18:37:39 -07001119 result[3] = _mesa_noise3(a[0], a[1], a[2]);
Briane80d9012007-02-23 16:53:24 -07001120 store_vector4(inst, machine, result);
1121 }
1122 break;
1123 case OPCODE_NOISE4:
1124 {
1125 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001126 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001127 result[0] =
Brian13e3b212007-02-22 16:09:40 -07001128 result[1] =
1129 result[2] =
Brian Paul702b5b02008-12-15 18:37:39 -07001130 result[3] = _mesa_noise4(a[0], a[1], a[2], a[3]);
Briane80d9012007-02-23 16:53:24 -07001131 store_vector4(inst, machine, result);
1132 }
1133 break;
1134 case OPCODE_NOP:
1135 break;
Brian Paul37eef7b2008-11-07 09:33:55 -07001136 case OPCODE_NOT: /* bitwise NOT */
1137 {
1138 GLuint a[4], result[4];
1139 fetch_vector4ui(&inst->SrcReg[0], machine, a);
1140 result[0] = ~a[0];
1141 result[1] = ~a[1];
1142 result[2] = ~a[2];
1143 result[3] = ~a[3];
1144 store_vector4ui(inst, machine, result);
1145 }
1146 break;
Brian Paulf6ead502008-11-07 08:51:31 -07001147 case OPCODE_NRM3: /* 3-component normalization */
1148 {
1149 GLfloat a[4], result[4];
1150 GLfloat tmp;
1151 fetch_vector4(&inst->SrcReg[0], machine, a);
1152 tmp = a[0] * a[0] + a[1] * a[1] + a[2] * a[2];
1153 if (tmp != 0.0F)
Brian Paul22459e72008-11-07 12:59:36 -07001154 tmp = INV_SQRTF(tmp);
Brian Paulf6ead502008-11-07 08:51:31 -07001155 result[0] = tmp * a[0];
1156 result[1] = tmp * a[1];
1157 result[2] = tmp * a[2];
1158 result[3] = 0.0; /* undefined, but prevent valgrind warnings */
1159 store_vector4(inst, machine, result);
1160 }
1161 break;
1162 case OPCODE_NRM4: /* 4-component normalization */
1163 {
1164 GLfloat a[4], result[4];
1165 GLfloat tmp;
1166 fetch_vector4(&inst->SrcReg[0], machine, a);
1167 tmp = a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3];
1168 if (tmp != 0.0F)
Brian Paul22459e72008-11-07 12:59:36 -07001169 tmp = INV_SQRTF(tmp);
Brian Paulf6ead502008-11-07 08:51:31 -07001170 result[0] = tmp * a[0];
1171 result[1] = tmp * a[1];
1172 result[2] = tmp * a[2];
1173 result[3] = tmp * a[3];
1174 store_vector4(inst, machine, result);
1175 }
1176 break;
Brian Paul37eef7b2008-11-07 09:33:55 -07001177 case OPCODE_OR: /* bitwise OR */
1178 {
1179 GLuint a[4], b[4], result[4];
1180 fetch_vector4ui(&inst->SrcReg[0], machine, a);
1181 fetch_vector4ui(&inst->SrcReg[1], machine, b);
1182 result[0] = a[0] | b[0];
1183 result[1] = a[1] | b[1];
1184 result[2] = a[2] | b[2];
1185 result[3] = a[3] | b[3];
1186 store_vector4ui(inst, machine, result);
1187 }
1188 break;
Briane80d9012007-02-23 16:53:24 -07001189 case OPCODE_PK2H: /* pack two 16-bit floats in one 32-bit float */
1190 {
Brian Paul37eef7b2008-11-07 09:33:55 -07001191 GLfloat a[4];
1192 GLuint result[4];
Briane80d9012007-02-23 16:53:24 -07001193 GLhalfNV hx, hy;
Brian33eac562007-02-25 18:52:41 -07001194 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001195 hx = _mesa_float_to_half(a[0]);
1196 hy = _mesa_float_to_half(a[1]);
Brian Paul37eef7b2008-11-07 09:33:55 -07001197 result[0] =
1198 result[1] =
1199 result[2] =
1200 result[3] = hx | (hy << 16);
1201 store_vector4ui(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -07001202 }
1203 break;
1204 case OPCODE_PK2US: /* pack two GLushorts into one 32-bit float */
1205 {
Brian Paul37eef7b2008-11-07 09:33:55 -07001206 GLfloat a[4];
1207 GLuint result[4], usx, usy;
Brian33eac562007-02-25 18:52:41 -07001208 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001209 a[0] = CLAMP(a[0], 0.0F, 1.0F);
1210 a[1] = CLAMP(a[1], 0.0F, 1.0F);
1211 usx = IROUND(a[0] * 65535.0F);
1212 usy = IROUND(a[1] * 65535.0F);
Brian Paul37eef7b2008-11-07 09:33:55 -07001213 result[0] =
1214 result[1] =
1215 result[2] =
1216 result[3] = usx | (usy << 16);
1217 store_vector4ui(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -07001218 }
1219 break;
1220 case OPCODE_PK4B: /* pack four GLbytes into one 32-bit float */
1221 {
Brian Paul37eef7b2008-11-07 09:33:55 -07001222 GLfloat a[4];
1223 GLuint result[4], ubx, uby, ubz, ubw;
Brian33eac562007-02-25 18:52:41 -07001224 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001225 a[0] = CLAMP(a[0], -128.0F / 127.0F, 1.0F);
1226 a[1] = CLAMP(a[1], -128.0F / 127.0F, 1.0F);
1227 a[2] = CLAMP(a[2], -128.0F / 127.0F, 1.0F);
1228 a[3] = CLAMP(a[3], -128.0F / 127.0F, 1.0F);
1229 ubx = IROUND(127.0F * a[0] + 128.0F);
1230 uby = IROUND(127.0F * a[1] + 128.0F);
1231 ubz = IROUND(127.0F * a[2] + 128.0F);
1232 ubw = IROUND(127.0F * a[3] + 128.0F);
Brian Paul37eef7b2008-11-07 09:33:55 -07001233 result[0] =
1234 result[1] =
1235 result[2] =
1236 result[3] = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
1237 store_vector4ui(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -07001238 }
1239 break;
1240 case OPCODE_PK4UB: /* pack four GLubytes into one 32-bit float */
1241 {
Brian Paul37eef7b2008-11-07 09:33:55 -07001242 GLfloat a[4];
1243 GLuint result[4], ubx, uby, ubz, ubw;
Brian33eac562007-02-25 18:52:41 -07001244 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001245 a[0] = CLAMP(a[0], 0.0F, 1.0F);
1246 a[1] = CLAMP(a[1], 0.0F, 1.0F);
1247 a[2] = CLAMP(a[2], 0.0F, 1.0F);
1248 a[3] = CLAMP(a[3], 0.0F, 1.0F);
1249 ubx = IROUND(255.0F * a[0]);
1250 uby = IROUND(255.0F * a[1]);
1251 ubz = IROUND(255.0F * a[2]);
1252 ubw = IROUND(255.0F * a[3]);
Brian Paul37eef7b2008-11-07 09:33:55 -07001253 result[0] =
1254 result[1] =
1255 result[2] =
1256 result[3] = ubx | (uby << 8) | (ubz << 16) | (ubw << 24);
1257 store_vector4ui(inst, machine, result);
Briane80d9012007-02-23 16:53:24 -07001258 }
1259 break;
1260 case OPCODE_POW:
1261 {
1262 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001263 fetch_vector1(&inst->SrcReg[0], machine, a);
1264 fetch_vector1(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001265 result[0] = result[1] = result[2] = result[3]
1266 = (GLfloat) _mesa_pow(a[0], b[0]);
1267 store_vector4(inst, machine, result);
1268 }
1269 break;
1270 case OPCODE_RCP:
1271 {
1272 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001273 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001274 if (DEBUG_PROG) {
1275 if (a[0] == 0)
1276 printf("RCP(0)\n");
1277 else if (IS_INF_OR_NAN(a[0]))
1278 printf("RCP(inf)\n");
Brian13e3b212007-02-22 16:09:40 -07001279 }
Briane80d9012007-02-23 16:53:24 -07001280 result[0] = result[1] = result[2] = result[3] = 1.0F / a[0];
1281 store_vector4(inst, machine, result);
1282 }
1283 break;
1284 case OPCODE_RET: /* return from subroutine (conditional) */
1285 if (eval_condition(machine, inst)) {
1286 if (machine->StackDepth == 0) {
1287 return GL_TRUE; /* Per GL_NV_vertex_program2 spec */
Brian13e3b212007-02-22 16:09:40 -07001288 }
Briana0275b02007-03-27 11:02:20 -06001289 /* subtract one because of pc++ in the for loop */
1290 pc = machine->CallStack[--machine->StackDepth] - 1;
Briane80d9012007-02-23 16:53:24 -07001291 }
1292 break;
1293 case OPCODE_RFL: /* reflection vector */
1294 {
1295 GLfloat axis[4], dir[4], result[4], tmpX, tmpW;
Brian33eac562007-02-25 18:52:41 -07001296 fetch_vector4(&inst->SrcReg[0], machine, axis);
1297 fetch_vector4(&inst->SrcReg[1], machine, dir);
Briane80d9012007-02-23 16:53:24 -07001298 tmpW = DOT3(axis, axis);
1299 tmpX = (2.0F * DOT3(axis, dir)) / tmpW;
1300 result[0] = tmpX * axis[0] - dir[0];
1301 result[1] = tmpX * axis[1] - dir[1];
1302 result[2] = tmpX * axis[2] - dir[2];
1303 /* result[3] is never written! XXX enforce in parser! */
1304 store_vector4(inst, machine, result);
1305 }
1306 break;
1307 case OPCODE_RSQ: /* 1 / sqrt() */
1308 {
1309 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001310 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001311 a[0] = FABSF(a[0]);
1312 result[0] = result[1] = result[2] = result[3] = INV_SQRTF(a[0]);
1313 store_vector4(inst, machine, result);
1314 if (DEBUG_PROG) {
1315 printf("RSQ %g = 1/sqrt(|%g|)\n", result[0], a[0]);
Brian13e3b212007-02-22 16:09:40 -07001316 }
Briane80d9012007-02-23 16:53:24 -07001317 }
1318 break;
1319 case OPCODE_SCS: /* sine and cos */
1320 {
1321 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001322 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001323 result[0] = (GLfloat) _mesa_cos(a[0]);
1324 result[1] = (GLfloat) _mesa_sin(a[0]);
1325 result[2] = 0.0; /* undefined! */
1326 result[3] = 0.0; /* undefined! */
1327 store_vector4(inst, machine, result);
1328 }
1329 break;
1330 case OPCODE_SEQ: /* set on equal */
1331 {
1332 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001333 fetch_vector4(&inst->SrcReg[0], machine, a);
1334 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001335 result[0] = (a[0] == b[0]) ? 1.0F : 0.0F;
1336 result[1] = (a[1] == b[1]) ? 1.0F : 0.0F;
1337 result[2] = (a[2] == b[2]) ? 1.0F : 0.0F;
1338 result[3] = (a[3] == b[3]) ? 1.0F : 0.0F;
1339 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001340 if (DEBUG_PROG) {
1341 printf("SEQ (%g %g %g %g) = (%g %g %g %g) == (%g %g %g %g)\n",
1342 result[0], result[1], result[2], result[3],
1343 a[0], a[1], a[2], a[3],
1344 b[0], b[1], b[2], b[3]);
1345 }
Briane80d9012007-02-23 16:53:24 -07001346 }
1347 break;
1348 case OPCODE_SFL: /* set false, operands ignored */
1349 {
1350 static const GLfloat result[4] = { 0.0F, 0.0F, 0.0F, 0.0F };
1351 store_vector4(inst, machine, result);
1352 }
1353 break;
1354 case OPCODE_SGE: /* set on greater or equal */
1355 {
1356 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001357 fetch_vector4(&inst->SrcReg[0], machine, a);
1358 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001359 result[0] = (a[0] >= b[0]) ? 1.0F : 0.0F;
1360 result[1] = (a[1] >= b[1]) ? 1.0F : 0.0F;
1361 result[2] = (a[2] >= b[2]) ? 1.0F : 0.0F;
1362 result[3] = (a[3] >= b[3]) ? 1.0F : 0.0F;
1363 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001364 if (DEBUG_PROG) {
1365 printf("SGE (%g %g %g %g) = (%g %g %g %g) >= (%g %g %g %g)\n",
1366 result[0], result[1], result[2], result[3],
1367 a[0], a[1], a[2], a[3],
1368 b[0], b[1], b[2], b[3]);
1369 }
Briane80d9012007-02-23 16:53:24 -07001370 }
1371 break;
1372 case OPCODE_SGT: /* set on greater */
1373 {
1374 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001375 fetch_vector4(&inst->SrcReg[0], machine, a);
1376 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001377 result[0] = (a[0] > b[0]) ? 1.0F : 0.0F;
1378 result[1] = (a[1] > b[1]) ? 1.0F : 0.0F;
1379 result[2] = (a[2] > b[2]) ? 1.0F : 0.0F;
1380 result[3] = (a[3] > b[3]) ? 1.0F : 0.0F;
1381 store_vector4(inst, machine, result);
1382 if (DEBUG_PROG) {
Brian28ab1122007-03-06 12:15:30 -07001383 printf("SGT (%g %g %g %g) = (%g %g %g %g) > (%g %g %g %g)\n",
1384 result[0], result[1], result[2], result[3],
1385 a[0], a[1], a[2], a[3],
1386 b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001387 }
Briane80d9012007-02-23 16:53:24 -07001388 }
1389 break;
1390 case OPCODE_SIN:
1391 {
1392 GLfloat a[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001393 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001394 result[0] = result[1] = result[2] = result[3]
1395 = (GLfloat) _mesa_sin(a[0]);
1396 store_vector4(inst, machine, result);
1397 }
1398 break;
1399 case OPCODE_SLE: /* set on less or equal */
1400 {
1401 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001402 fetch_vector4(&inst->SrcReg[0], machine, a);
1403 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001404 result[0] = (a[0] <= b[0]) ? 1.0F : 0.0F;
1405 result[1] = (a[1] <= b[1]) ? 1.0F : 0.0F;
1406 result[2] = (a[2] <= b[2]) ? 1.0F : 0.0F;
1407 result[3] = (a[3] <= b[3]) ? 1.0F : 0.0F;
1408 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001409 if (DEBUG_PROG) {
1410 printf("SLE (%g %g %g %g) = (%g %g %g %g) <= (%g %g %g %g)\n",
1411 result[0], result[1], result[2], result[3],
1412 a[0], a[1], a[2], a[3],
1413 b[0], b[1], b[2], b[3]);
1414 }
Briane80d9012007-02-23 16:53:24 -07001415 }
1416 break;
1417 case OPCODE_SLT: /* set on less */
1418 {
1419 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001420 fetch_vector4(&inst->SrcReg[0], machine, a);
1421 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001422 result[0] = (a[0] < b[0]) ? 1.0F : 0.0F;
1423 result[1] = (a[1] < b[1]) ? 1.0F : 0.0F;
1424 result[2] = (a[2] < b[2]) ? 1.0F : 0.0F;
1425 result[3] = (a[3] < b[3]) ? 1.0F : 0.0F;
1426 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001427 if (DEBUG_PROG) {
1428 printf("SLT (%g %g %g %g) = (%g %g %g %g) < (%g %g %g %g)\n",
1429 result[0], result[1], result[2], result[3],
1430 a[0], a[1], a[2], a[3],
1431 b[0], b[1], b[2], b[3]);
1432 }
Briane80d9012007-02-23 16:53:24 -07001433 }
1434 break;
1435 case OPCODE_SNE: /* set on not equal */
1436 {
1437 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001438 fetch_vector4(&inst->SrcReg[0], machine, a);
1439 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001440 result[0] = (a[0] != b[0]) ? 1.0F : 0.0F;
1441 result[1] = (a[1] != b[1]) ? 1.0F : 0.0F;
1442 result[2] = (a[2] != b[2]) ? 1.0F : 0.0F;
1443 result[3] = (a[3] != b[3]) ? 1.0F : 0.0F;
1444 store_vector4(inst, machine, result);
Brian28ab1122007-03-06 12:15:30 -07001445 if (DEBUG_PROG) {
1446 printf("SNE (%g %g %g %g) = (%g %g %g %g) != (%g %g %g %g)\n",
1447 result[0], result[1], result[2], result[3],
1448 a[0], a[1], a[2], a[3],
1449 b[0], b[1], b[2], b[3]);
1450 }
Briane80d9012007-02-23 16:53:24 -07001451 }
1452 break;
Brian Paulf6ead502008-11-07 08:51:31 -07001453 case OPCODE_SSG: /* set sign (-1, 0 or +1) */
1454 {
1455 GLfloat a[4], result[4];
1456 fetch_vector4(&inst->SrcReg[0], machine, a);
1457 result[0] = (GLfloat) ((a[0] > 0.0F) - (a[0] < 0.0F));
1458 result[1] = (GLfloat) ((a[1] > 0.0F) - (a[1] < 0.0F));
1459 result[2] = (GLfloat) ((a[2] > 0.0F) - (a[2] < 0.0F));
1460 result[3] = (GLfloat) ((a[3] > 0.0F) - (a[3] < 0.0F));
1461 store_vector4(inst, machine, result);
1462 }
1463 break;
Briane80d9012007-02-23 16:53:24 -07001464 case OPCODE_STR: /* set true, operands ignored */
1465 {
1466 static const GLfloat result[4] = { 1.0F, 1.0F, 1.0F, 1.0F };
1467 store_vector4(inst, machine, result);
1468 }
1469 break;
1470 case OPCODE_SUB:
1471 {
1472 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001473 fetch_vector4(&inst->SrcReg[0], machine, a);
1474 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001475 result[0] = a[0] - b[0];
1476 result[1] = a[1] - b[1];
1477 result[2] = a[2] - b[2];
1478 result[3] = a[3] - b[3];
1479 store_vector4(inst, machine, result);
1480 if (DEBUG_PROG) {
1481 printf("SUB (%g %g %g %g) = (%g %g %g %g) - (%g %g %g %g)\n",
1482 result[0], result[1], result[2], result[3],
1483 a[0], a[1], a[2], a[3], b[0], b[1], b[2], b[3]);
Brian13e3b212007-02-22 16:09:40 -07001484 }
Briane80d9012007-02-23 16:53:24 -07001485 }
1486 break;
1487 case OPCODE_SWZ: /* extended swizzle */
1488 {
1489 const struct prog_src_register *source = &inst->SrcReg[0];
Brian Paulf4361542008-11-11 10:47:10 -07001490 const GLfloat *src = get_src_register_pointer(source, machine);
Briane80d9012007-02-23 16:53:24 -07001491 GLfloat result[4];
1492 GLuint i;
1493 for (i = 0; i < 4; i++) {
1494 const GLuint swz = GET_SWZ(source->Swizzle, i);
1495 if (swz == SWIZZLE_ZERO)
1496 result[i] = 0.0;
1497 else if (swz == SWIZZLE_ONE)
1498 result[i] = 1.0;
Brian13e3b212007-02-22 16:09:40 -07001499 else {
Briane80d9012007-02-23 16:53:24 -07001500 ASSERT(swz >= 0);
1501 ASSERT(swz <= 3);
1502 result[i] = src[swz];
Brian13e3b212007-02-22 16:09:40 -07001503 }
Brian Paul7db7ff82009-04-14 22:14:30 -06001504 if (source->Negate & (1 << i))
Briane80d9012007-02-23 16:53:24 -07001505 result[i] = -result[i];
Brian13e3b212007-02-22 16:09:40 -07001506 }
Briane80d9012007-02-23 16:53:24 -07001507 store_vector4(inst, machine, result);
1508 }
1509 break;
1510 case OPCODE_TEX: /* Both ARB and NV frag prog */
Brian999b5562007-11-23 12:01:57 -07001511 /* Simple texel lookup */
Briane80d9012007-02-23 16:53:24 -07001512 {
Brian999b5562007-11-23 12:01:57 -07001513 GLfloat texcoord[4], color[4];
1514 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1515
1516 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1517
Briane80d9012007-02-23 16:53:24 -07001518 if (DEBUG_PROG) {
Brian999b5562007-11-23 12:01:57 -07001519 printf("TEX (%g, %g, %g, %g) = texture[%d][%g, %g, %g, %g]\n",
Briane80d9012007-02-23 16:53:24 -07001520 color[0], color[1], color[2], color[3],
1521 inst->TexSrcUnit,
Brian999b5562007-11-23 12:01:57 -07001522 texcoord[0], texcoord[1], texcoord[2], texcoord[3]);
Briane80d9012007-02-23 16:53:24 -07001523 }
1524 store_vector4(inst, machine, color);
1525 }
1526 break;
1527 case OPCODE_TXB: /* GL_ARB_fragment_program only */
1528 /* Texel lookup with LOD bias */
1529 {
Brian Paul1ab22502009-04-01 18:50:07 -06001530 const GLuint unit = machine->Samplers[inst->TexSrcUnit];
1531 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
Brian999b5562007-11-23 12:01:57 -07001532 GLfloat texcoord[4], color[4], lodBias;
1533
1534 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1535
1536 /* texcoord[3] is the bias to add to lambda */
1537 lodBias = texUnit->LodBias + texcoord[3];
1538 if (texUnit->_Current) {
1539 lodBias += texUnit->_Current->LodBias;
1540 }
1541
1542 fetch_texel(ctx, machine, inst, texcoord, lodBias, color);
1543
Briane80d9012007-02-23 16:53:24 -07001544 store_vector4(inst, machine, color);
1545 }
1546 break;
1547 case OPCODE_TXD: /* GL_NV_fragment_program only */
1548 /* Texture lookup w/ partial derivatives for LOD */
1549 {
1550 GLfloat texcoord[4], dtdx[4], dtdy[4], color[4];
Brian33eac562007-02-25 18:52:41 -07001551 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
1552 fetch_vector4(&inst->SrcReg[1], machine, dtdx);
1553 fetch_vector4(&inst->SrcReg[2], machine, dtdy);
Briane80d9012007-02-23 16:53:24 -07001554 machine->FetchTexelDeriv(ctx, texcoord, dtdx, dtdy,
Brian999b5562007-11-23 12:01:57 -07001555 0.0, /* lodBias */
Briane80d9012007-02-23 16:53:24 -07001556 inst->TexSrcUnit, color);
1557 store_vector4(inst, machine, color);
1558 }
1559 break;
1560 case OPCODE_TXP: /* GL_ARB_fragment_program only */
1561 /* Texture lookup w/ projective divide */
1562 {
Brian999b5562007-11-23 12:01:57 -07001563 GLfloat texcoord[4], color[4];
1564
Brian33eac562007-02-25 18:52:41 -07001565 fetch_vector4(&inst->SrcReg[0], machine, texcoord);
Briane80d9012007-02-23 16:53:24 -07001566 /* Not so sure about this test - if texcoord[3] is
1567 * zero, we'd probably be fine except for an ASSERT in
1568 * IROUND_POS() which gets triggered by the inf values created.
1569 */
1570 if (texcoord[3] != 0.0) {
1571 texcoord[0] /= texcoord[3];
1572 texcoord[1] /= texcoord[3];
1573 texcoord[2] /= texcoord[3];
1574 }
Brian999b5562007-11-23 12:01:57 -07001575
1576 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1577
Briane80d9012007-02-23 16:53:24 -07001578 store_vector4(inst, machine, color);
1579 }
1580 break;
1581 case OPCODE_TXP_NV: /* GL_NV_fragment_program only */
Brian999b5562007-11-23 12:01:57 -07001582 /* Texture lookup w/ projective divide, as above, but do not
1583 * do the divide by w if sampling from a cube map.
1584 */
Briane80d9012007-02-23 16:53:24 -07001585 {
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 if (inst->TexSrcTarget != TEXTURE_CUBE_INDEX &&
1590 texcoord[3] != 0.0) {
1591 texcoord[0] /= texcoord[3];
1592 texcoord[1] /= texcoord[3];
1593 texcoord[2] /= texcoord[3];
1594 }
Brian999b5562007-11-23 12:01:57 -07001595
1596 fetch_texel(ctx, machine, inst, texcoord, 0.0, color);
1597
Briane80d9012007-02-23 16:53:24 -07001598 store_vector4(inst, machine, color);
1599 }
1600 break;
Brian Paul035c0cf2008-11-06 17:14:33 -07001601 case OPCODE_TRUNC: /* truncate toward zero */
1602 {
1603 GLfloat a[4], result[4];
1604 fetch_vector4(&inst->SrcReg[0], machine, a);
1605 result[0] = (GLfloat) (GLint) a[0];
1606 result[1] = (GLfloat) (GLint) a[1];
1607 result[2] = (GLfloat) (GLint) a[2];
1608 result[3] = (GLfloat) (GLint) a[3];
1609 store_vector4(inst, machine, result);
1610 }
1611 break;
Briane80d9012007-02-23 16:53:24 -07001612 case OPCODE_UP2H: /* unpack two 16-bit floats */
1613 {
1614 GLfloat a[4], result[4];
1615 const GLuint *rawBits = (const GLuint *) a;
1616 GLhalfNV hx, hy;
Brian33eac562007-02-25 18:52:41 -07001617 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001618 hx = rawBits[0] & 0xffff;
1619 hy = rawBits[0] >> 16;
1620 result[0] = result[2] = _mesa_half_to_float(hx);
1621 result[1] = result[3] = _mesa_half_to_float(hy);
1622 store_vector4(inst, machine, result);
1623 }
1624 break;
1625 case OPCODE_UP2US: /* unpack two GLushorts */
1626 {
1627 GLfloat a[4], result[4];
1628 const GLuint *rawBits = (const GLuint *) a;
1629 GLushort usx, usy;
Brian33eac562007-02-25 18:52:41 -07001630 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001631 usx = rawBits[0] & 0xffff;
1632 usy = rawBits[0] >> 16;
1633 result[0] = result[2] = usx * (1.0f / 65535.0f);
1634 result[1] = result[3] = usy * (1.0f / 65535.0f);
1635 store_vector4(inst, machine, result);
1636 }
1637 break;
1638 case OPCODE_UP4B: /* unpack four GLbytes */
1639 {
1640 GLfloat a[4], result[4];
1641 const GLuint *rawBits = (const GLuint *) a;
Brian33eac562007-02-25 18:52:41 -07001642 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001643 result[0] = (((rawBits[0] >> 0) & 0xff) - 128) / 127.0F;
1644 result[1] = (((rawBits[0] >> 8) & 0xff) - 128) / 127.0F;
1645 result[2] = (((rawBits[0] >> 16) & 0xff) - 128) / 127.0F;
1646 result[3] = (((rawBits[0] >> 24) & 0xff) - 128) / 127.0F;
1647 store_vector4(inst, machine, result);
1648 }
1649 break;
1650 case OPCODE_UP4UB: /* unpack four GLubytes */
1651 {
1652 GLfloat a[4], result[4];
1653 const GLuint *rawBits = (const GLuint *) a;
Brian33eac562007-02-25 18:52:41 -07001654 fetch_vector1(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001655 result[0] = ((rawBits[0] >> 0) & 0xff) / 255.0F;
1656 result[1] = ((rawBits[0] >> 8) & 0xff) / 255.0F;
1657 result[2] = ((rawBits[0] >> 16) & 0xff) / 255.0F;
1658 result[3] = ((rawBits[0] >> 24) & 0xff) / 255.0F;
1659 store_vector4(inst, machine, result);
1660 }
1661 break;
Brian Paul37eef7b2008-11-07 09:33:55 -07001662 case OPCODE_XOR: /* bitwise XOR */
1663 {
1664 GLuint a[4], b[4], result[4];
1665 fetch_vector4ui(&inst->SrcReg[0], machine, a);
1666 fetch_vector4ui(&inst->SrcReg[1], machine, b);
1667 result[0] = a[0] ^ b[0];
1668 result[1] = a[1] ^ b[1];
1669 result[2] = a[2] ^ b[2];
1670 result[3] = a[3] ^ b[3];
1671 store_vector4ui(inst, machine, result);
1672 }
1673 break;
Briane80d9012007-02-23 16:53:24 -07001674 case OPCODE_XPD: /* cross product */
1675 {
1676 GLfloat a[4], b[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001677 fetch_vector4(&inst->SrcReg[0], machine, a);
1678 fetch_vector4(&inst->SrcReg[1], machine, b);
Briane80d9012007-02-23 16:53:24 -07001679 result[0] = a[1] * b[2] - a[2] * b[1];
1680 result[1] = a[2] * b[0] - a[0] * b[2];
1681 result[2] = a[0] * b[1] - a[1] * b[0];
1682 result[3] = 1.0;
1683 store_vector4(inst, machine, result);
Brian9637c962007-03-07 17:40:57 -07001684 if (DEBUG_PROG) {
1685 printf("XPD (%g %g %g %g) = (%g %g %g) X (%g %g %g)\n",
1686 result[0], result[1], result[2], result[3],
1687 a[0], a[1], a[2], b[0], b[1], b[2]);
1688 }
Briane80d9012007-02-23 16:53:24 -07001689 }
1690 break;
1691 case OPCODE_X2D: /* 2-D matrix transform */
1692 {
1693 GLfloat a[4], b[4], c[4], result[4];
Brian33eac562007-02-25 18:52:41 -07001694 fetch_vector4(&inst->SrcReg[0], machine, a);
1695 fetch_vector4(&inst->SrcReg[1], machine, b);
1696 fetch_vector4(&inst->SrcReg[2], machine, c);
Briane80d9012007-02-23 16:53:24 -07001697 result[0] = a[0] + b[0] * c[0] + b[1] * c[1];
1698 result[1] = a[1] + b[0] * c[2] + b[1] * c[3];
1699 result[2] = a[2] + b[0] * c[0] + b[1] * c[1];
1700 result[3] = a[3] + b[0] * c[2] + b[1] * c[3];
1701 store_vector4(inst, machine, result);
1702 }
1703 break;
1704 case OPCODE_PRINT:
1705 {
1706 if (inst->SrcReg[0].File != -1) {
1707 GLfloat a[4];
Brian33eac562007-02-25 18:52:41 -07001708 fetch_vector4(&inst->SrcReg[0], machine, a);
Briane80d9012007-02-23 16:53:24 -07001709 _mesa_printf("%s%g, %g, %g, %g\n", (const char *) inst->Data,
1710 a[0], a[1], a[2], a[3]);
1711 }
1712 else {
1713 _mesa_printf("%s\n", (const char *) inst->Data);
1714 }
1715 }
1716 break;
1717 case OPCODE_END:
1718 return GL_TRUE;
1719 default:
Alan Hourihane936dba12008-04-22 20:28:35 +01001720 _mesa_problem(ctx, "Bad opcode %d in _mesa_execute_program",
Briane80d9012007-02-23 16:53:24 -07001721 inst->Opcode);
1722 return GL_TRUE; /* return value doesn't matter */
Brian13e3b212007-02-22 16:09:40 -07001723 }
Briane80d9012007-02-23 16:53:24 -07001724
Briancfd00112007-02-25 18:30:45 -07001725 numExec++;
1726 if (numExec > maxExec) {
Brian13e3b212007-02-22 16:09:40 -07001727 _mesa_problem(ctx, "Infinite loop detected in fragment program");
1728 return GL_TRUE;
Brian13e3b212007-02-22 16:09:40 -07001729 }
Briane80d9012007-02-23 16:53:24 -07001730
1731 } /* for pc */
Brian13e3b212007-02-22 16:09:40 -07001732
Brian13e3b212007-02-22 16:09:40 -07001733 return GL_TRUE;
1734}