blob: b9ff08de5fcc4e9ae7ab5d55dcf178c74f935e26 [file] [log] [blame]
Jouk Jansen40322e12004-04-05 08:50:36 +00001/*
2 * Mesa 3-D graphics library
Brian Paul11a54c32006-11-15 19:54:25 +00003 * Version: 6.5.1
Jouk Jansen40322e12004-04-05 08:50:36 +00004 *
Michal Krolbb38cad2006-04-11 11:41:11 +00005 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
Jouk Jansen40322e12004-04-05 08:50:36 +00006 *
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#define DEBUG_PARSING 0
26
27/**
28 * \file arbprogparse.c
29 * ARB_*_program parser core
30 * \author Karl Rasche
31 */
32
Jouk Jansen40322e12004-04-05 08:50:36 +000033#include "glheader.h"
Jouk Jansen40322e12004-04-05 08:50:36 +000034#include "imports.h"
Jouk Jansen40322e12004-04-05 08:50:36 +000035#include "arbprogparse.h"
36#include "grammar_mesa.h"
Brian Paul32df89e2005-10-29 18:26:43 +000037#include "program.h"
Brian Paul8c41a142005-11-19 15:36:28 +000038#include "context.h"
Brian Paul6211a142006-08-24 23:37:13 +000039#include "macros.h"
Brian Paul8c41a142005-11-19 15:36:28 +000040#include "mtypes.h"
41#include "program_instruction.h"
42
43
Brian Paul6211a142006-08-24 23:37:13 +000044/* For ARB programs, use the NV instruction limits */
45#define MAX_INSTRUCTIONS MAX2(MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS, \
46 MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS)
Brian Paul8c41a142005-11-19 15:36:28 +000047
48
49/**
50 * This is basically a union of the vertex_program and fragment_program
51 * structs that we can use to parse the program into
52 *
53 * XXX we can probably get rid of this entirely someday.
54 */
55struct arb_program
56{
Brian Paul122629f2006-07-20 16:49:57 +000057 struct gl_program Base;
Brian Paul8c41a142005-11-19 15:36:28 +000058
59 GLuint Position; /* Just used for error reporting while parsing */
60 GLuint MajorVersion;
61 GLuint MinorVersion;
62
63 /* ARB_vertex_progmra options */
64 GLboolean HintPositionInvariant;
65
66 /* ARB_fragment_progmra options */
67 GLenum PrecisionOption; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
68 GLenum FogOption; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
69
70 /* ARB_fragment_program specifics */
71 GLbitfield TexturesUsed[MAX_TEXTURE_IMAGE_UNITS];
72 GLuint NumAluInstructions;
73 GLuint NumTexInstructions;
74 GLuint NumTexIndirections;
75
76 GLboolean UsesKill;
77};
78
Ian Romanick9bdfee32005-07-18 12:31:24 +000079
Brian Paula6c423d2004-08-25 15:59:48 +000080
Jouk Jansen40322e12004-04-05 08:50:36 +000081/* TODO:
82 * Fragment Program Stuff:
83 * -----------------------------------------------------
84 *
85 * - things from Michal's email
86 * + overflow on atoi
87 * + not-overflowing floats (don't use parse_integer..)
88 * + can remove range checking in arbparse.c
89 *
90 * - check all limits of number of various variables
91 * + parameters
92 *
93 * - test! test! test!
94 *
95 * Vertex Program Stuff:
96 * -----------------------------------------------------
97 * - Optimize param array usage and count limits correctly, see spec,
98 * section 2.14.3.7
99 * + Record if an array is reference absolutly or relatively (or both)
100 * + For absolute arrays, store a bitmap of accesses
101 * + For single parameters, store an access flag
102 * + After parsing, make a parameter cleanup and merging pass, where
103 * relative arrays are layed out first, followed by abs arrays, and
104 * finally single state.
105 * + Remap offsets for param src and dst registers
106 * + Now we can properly count parameter usage
107 *
108 * - Multiple state binding errors in param arrays (see spec, just before
109 * section 2.14.3.3)
110 * - grep for XXX
111 *
112 * Mesa Stuff
113 * -----------------------------------------------------
114 * - User clipping planes vs. PositionInvariant
115 * - Is it sufficient to just multiply by the mvp to transform in the
116 * PositionInvariant case? Or do we need something more involved?
117 *
118 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
119 * - fetch state listed in program_parameters list
120 * + WTF should this go???
121 * + currently in nvvertexec.c and s_nvfragprog.c
122 *
123 * - allow for multiple address registers (and fetch address regs properly)
124 *
125 * Cosmetic Stuff
126 * -----------------------------------------------------
127 * - remove any leftover unused grammer.c stuff (dict_ ?)
128 * - fix grammer.c error handling so its not static
129 * - #ifdef around stuff pertaining to extentions
130 *
131 * Outstanding Questions:
132 * -----------------------------------------------------
133 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
134 * what gets hacked off because of this:
135 * + VERTEX_ATTRIB_MATRIXINDEX
136 * + VERTEX_ATTRIB_WEIGHT
137 * + MATRIX_MODELVIEW
138 * + MATRIX_PALETTE
139 *
140 * - When can we fetch env/local params from their own register files, and
141 * when to we have to fetch them into the main state register file?
142 * (think arrays)
143 *
144 * Grammar Changes:
145 * -----------------------------------------------------
146 */
147
148/* Changes since moving the file to shader directory
149
1502004-III-4 ------------------------------------------------------------
151- added #include "grammar_mesa.h"
152- removed grammar specific code part (it resides now in grammar.c)
153- added GL_ARB_fragment_program_shadow tokens
154- modified #include "arbparse_syn.h"
155- major changes inside _mesa_parse_arb_program()
156- check the program string for '\0' characters
157- copy the program string to a one-byte-longer location to have
158 it null-terminated
159- position invariance test (not writing to result.position) moved
160 to syntax part
161*/
162
163typedef GLubyte *production;
164
Brian Paul11a54c32006-11-15 19:54:25 +0000165
Jouk Jansen40322e12004-04-05 08:50:36 +0000166/**
167 * This is the text describing the rules to parse the grammar
168 */
Brian Paul11a54c32006-11-15 19:54:25 +0000169LONGSTRING static char arb_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +0000170#include "arbprogram_syn.h"
171;
172
173/**
174 * These should match up with the values defined in arbprogram.syn
175 */
176
177/*
178 Changes:
179 - changed and merged V_* and F_* opcode values to OP_*.
180 - added GL_ARB_fragment_program_shadow specific tokens (michal)
181*/
Michal Krolb80bc052004-10-21 14:09:54 +0000182#define REVISION 0x09
Jouk Jansen40322e12004-04-05 08:50:36 +0000183
184/* program type */
185#define FRAGMENT_PROGRAM 0x01
186#define VERTEX_PROGRAM 0x02
187
188/* program section */
189#define OPTION 0x01
190#define INSTRUCTION 0x02
191#define DECLARATION 0x03
192#define END 0x04
193
Michal Krolb80bc052004-10-21 14:09:54 +0000194/* GL_ARB_fragment_program option */
195#define ARB_PRECISION_HINT_FASTEST 0x00
196#define ARB_PRECISION_HINT_NICEST 0x01
197#define ARB_FOG_EXP 0x02
198#define ARB_FOG_EXP2 0x03
199#define ARB_FOG_LINEAR 0x04
Jouk Jansen40322e12004-04-05 08:50:36 +0000200
Michal Krolb80bc052004-10-21 14:09:54 +0000201/* GL_ARB_vertex_program option */
202#define ARB_POSITION_INVARIANT 0x05
Jouk Jansen40322e12004-04-05 08:50:36 +0000203
Michal Krolb80bc052004-10-21 14:09:54 +0000204/* GL_ARB_fragment_program_shadow option */
205#define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
Jouk Jansen40322e12004-04-05 08:50:36 +0000206
Michal Krolb80bc052004-10-21 14:09:54 +0000207/* GL_ARB_draw_buffers option */
208#define ARB_DRAW_BUFFERS 0x07
Michal Krolad22ce82004-10-11 08:13:25 +0000209
Jouk Jansen40322e12004-04-05 08:50:36 +0000210/* GL_ARB_fragment_program instruction class */
211#define OP_ALU_INST 0x00
212#define OP_TEX_INST 0x01
213
214/* GL_ARB_vertex_program instruction class */
215/* OP_ALU_INST */
216
217/* GL_ARB_fragment_program instruction type */
218#define OP_ALU_VECTOR 0x00
219#define OP_ALU_SCALAR 0x01
220#define OP_ALU_BINSC 0x02
221#define OP_ALU_BIN 0x03
222#define OP_ALU_TRI 0x04
223#define OP_ALU_SWZ 0x05
224#define OP_TEX_SAMPLE 0x06
225#define OP_TEX_KIL 0x07
226
227/* GL_ARB_vertex_program instruction type */
228#define OP_ALU_ARL 0x08
229/* OP_ALU_VECTOR */
230/* OP_ALU_SCALAR */
231/* OP_ALU_BINSC */
232/* OP_ALU_BIN */
233/* OP_ALU_TRI */
234/* OP_ALU_SWZ */
235
236/* GL_ARB_fragment_program instruction code */
237#define OP_ABS 0x00
238#define OP_ABS_SAT 0x1B
239#define OP_FLR 0x09
240#define OP_FLR_SAT 0x26
241#define OP_FRC 0x0A
242#define OP_FRC_SAT 0x27
243#define OP_LIT 0x0C
244#define OP_LIT_SAT 0x2A
245#define OP_MOV 0x11
246#define OP_MOV_SAT 0x30
247#define OP_COS 0x1F
248#define OP_COS_SAT 0x20
249#define OP_EX2 0x07
250#define OP_EX2_SAT 0x25
251#define OP_LG2 0x0B
252#define OP_LG2_SAT 0x29
253#define OP_RCP 0x14
254#define OP_RCP_SAT 0x33
255#define OP_RSQ 0x15
256#define OP_RSQ_SAT 0x34
257#define OP_SIN 0x38
258#define OP_SIN_SAT 0x39
259#define OP_SCS 0x35
260#define OP_SCS_SAT 0x36
261#define OP_POW 0x13
262#define OP_POW_SAT 0x32
263#define OP_ADD 0x01
264#define OP_ADD_SAT 0x1C
265#define OP_DP3 0x03
266#define OP_DP3_SAT 0x21
267#define OP_DP4 0x04
268#define OP_DP4_SAT 0x22
269#define OP_DPH 0x05
270#define OP_DPH_SAT 0x23
271#define OP_DST 0x06
272#define OP_DST_SAT 0x24
273#define OP_MAX 0x0F
274#define OP_MAX_SAT 0x2E
275#define OP_MIN 0x10
276#define OP_MIN_SAT 0x2F
277#define OP_MUL 0x12
278#define OP_MUL_SAT 0x31
279#define OP_SGE 0x16
280#define OP_SGE_SAT 0x37
281#define OP_SLT 0x17
282#define OP_SLT_SAT 0x3A
283#define OP_SUB 0x18
284#define OP_SUB_SAT 0x3B
285#define OP_XPD 0x1A
286#define OP_XPD_SAT 0x43
287#define OP_CMP 0x1D
288#define OP_CMP_SAT 0x1E
289#define OP_LRP 0x2B
290#define OP_LRP_SAT 0x2C
291#define OP_MAD 0x0E
292#define OP_MAD_SAT 0x2D
293#define OP_SWZ 0x19
294#define OP_SWZ_SAT 0x3C
295#define OP_TEX 0x3D
296#define OP_TEX_SAT 0x3E
297#define OP_TXB 0x3F
298#define OP_TXB_SAT 0x40
299#define OP_TXP 0x41
300#define OP_TXP_SAT 0x42
301#define OP_KIL 0x28
302
303/* GL_ARB_vertex_program instruction code */
304#define OP_ARL 0x02
305/* OP_ABS */
306/* OP_FLR */
307/* OP_FRC */
308/* OP_LIT */
309/* OP_MOV */
310/* OP_EX2 */
311#define OP_EXP 0x08
312/* OP_LG2 */
313#define OP_LOG 0x0D
314/* OP_RCP */
315/* OP_RSQ */
316/* OP_POW */
317/* OP_ADD */
318/* OP_DP3 */
319/* OP_DP4 */
320/* OP_DPH */
321/* OP_DST */
322/* OP_MAX */
323/* OP_MIN */
324/* OP_MUL */
325/* OP_SGE */
326/* OP_SLT */
327/* OP_SUB */
328/* OP_XPD */
329/* OP_MAD */
330/* OP_SWZ */
331
332/* fragment attribute binding */
333#define FRAGMENT_ATTRIB_COLOR 0x01
334#define FRAGMENT_ATTRIB_TEXCOORD 0x02
335#define FRAGMENT_ATTRIB_FOGCOORD 0x03
336#define FRAGMENT_ATTRIB_POSITION 0x04
337
338/* vertex attribute binding */
339#define VERTEX_ATTRIB_POSITION 0x01
340#define VERTEX_ATTRIB_WEIGHT 0x02
341#define VERTEX_ATTRIB_NORMAL 0x03
342#define VERTEX_ATTRIB_COLOR 0x04
343#define VERTEX_ATTRIB_FOGCOORD 0x05
344#define VERTEX_ATTRIB_TEXCOORD 0x06
345#define VERTEX_ATTRIB_MATRIXINDEX 0x07
346#define VERTEX_ATTRIB_GENERIC 0x08
347
348/* fragment result binding */
349#define FRAGMENT_RESULT_COLOR 0x01
350#define FRAGMENT_RESULT_DEPTH 0x02
351
352/* vertex result binding */
353#define VERTEX_RESULT_POSITION 0x01
354#define VERTEX_RESULT_COLOR 0x02
355#define VERTEX_RESULT_FOGCOORD 0x03
356#define VERTEX_RESULT_POINTSIZE 0x04
357#define VERTEX_RESULT_TEXCOORD 0x05
358
359/* texture target */
360#define TEXTARGET_1D 0x01
361#define TEXTARGET_2D 0x02
362#define TEXTARGET_3D 0x03
363#define TEXTARGET_RECT 0x04
364#define TEXTARGET_CUBE 0x05
365/* GL_ARB_fragment_program_shadow */
366#define TEXTARGET_SHADOW1D 0x06
367#define TEXTARGET_SHADOW2D 0x07
368#define TEXTARGET_SHADOWRECT 0x08
369
370/* face type */
371#define FACE_FRONT 0x00
372#define FACE_BACK 0x01
373
374/* color type */
375#define COLOR_PRIMARY 0x00
376#define COLOR_SECONDARY 0x01
377
378/* component */
379#define COMPONENT_X 0x00
380#define COMPONENT_Y 0x01
381#define COMPONENT_Z 0x02
382#define COMPONENT_W 0x03
383#define COMPONENT_0 0x04
384#define COMPONENT_1 0x05
385
386/* array index type */
387#define ARRAY_INDEX_ABSOLUTE 0x00
388#define ARRAY_INDEX_RELATIVE 0x01
389
390/* matrix name */
391#define MATRIX_MODELVIEW 0x01
392#define MATRIX_PROJECTION 0x02
393#define MATRIX_MVP 0x03
394#define MATRIX_TEXTURE 0x04
395#define MATRIX_PALETTE 0x05
396#define MATRIX_PROGRAM 0x06
397
398/* matrix modifier */
399#define MATRIX_MODIFIER_IDENTITY 0x00
400#define MATRIX_MODIFIER_INVERSE 0x01
401#define MATRIX_MODIFIER_TRANSPOSE 0x02
402#define MATRIX_MODIFIER_INVTRANS 0x03
403
404/* constant type */
405#define CONSTANT_SCALAR 0x01
406#define CONSTANT_VECTOR 0x02
407
408/* program param type */
409#define PROGRAM_PARAM_ENV 0x01
410#define PROGRAM_PARAM_LOCAL 0x02
411
412/* register type */
413#define REGISTER_ATTRIB 0x01
414#define REGISTER_PARAM 0x02
415#define REGISTER_RESULT 0x03
416#define REGISTER_ESTABLISHED_NAME 0x04
417
418/* param binding */
419#define PARAM_NULL 0x00
420#define PARAM_ARRAY_ELEMENT 0x01
421#define PARAM_STATE_ELEMENT 0x02
422#define PARAM_PROGRAM_ELEMENT 0x03
423#define PARAM_PROGRAM_ELEMENTS 0x04
424#define PARAM_CONSTANT 0x05
425
426/* param state property */
427#define STATE_MATERIAL_PARSER 0x01
428#define STATE_LIGHT_PARSER 0x02
429#define STATE_LIGHT_MODEL 0x03
430#define STATE_LIGHT_PROD 0x04
431#define STATE_FOG 0x05
432#define STATE_MATRIX_ROWS 0x06
433/* GL_ARB_fragment_program */
434#define STATE_TEX_ENV 0x07
435#define STATE_DEPTH 0x08
436/* GL_ARB_vertex_program */
437#define STATE_TEX_GEN 0x09
438#define STATE_CLIP_PLANE 0x0A
439#define STATE_POINT 0x0B
440
441/* state material property */
442#define MATERIAL_AMBIENT 0x01
443#define MATERIAL_DIFFUSE 0x02
444#define MATERIAL_SPECULAR 0x03
445#define MATERIAL_EMISSION 0x04
446#define MATERIAL_SHININESS 0x05
447
448/* state light property */
449#define LIGHT_AMBIENT 0x01
450#define LIGHT_DIFFUSE 0x02
451#define LIGHT_SPECULAR 0x03
452#define LIGHT_POSITION 0x04
453#define LIGHT_ATTENUATION 0x05
454#define LIGHT_HALF 0x06
455#define LIGHT_SPOT_DIRECTION 0x07
456
457/* state light model property */
458#define LIGHT_MODEL_AMBIENT 0x01
459#define LIGHT_MODEL_SCENECOLOR 0x02
460
461/* state light product property */
462#define LIGHT_PROD_AMBIENT 0x01
463#define LIGHT_PROD_DIFFUSE 0x02
464#define LIGHT_PROD_SPECULAR 0x03
465
466/* state texture environment property */
467#define TEX_ENV_COLOR 0x01
468
469/* state texture generation coord property */
470#define TEX_GEN_EYE 0x01
471#define TEX_GEN_OBJECT 0x02
472
473/* state fog property */
474#define FOG_COLOR 0x01
475#define FOG_PARAMS 0x02
476
477/* state depth property */
478#define DEPTH_RANGE 0x01
479
480/* state point parameters property */
481#define POINT_SIZE 0x01
482#define POINT_ATTENUATION 0x02
483
484/* declaration */
485#define ATTRIB 0x01
486#define PARAM 0x02
487#define TEMP 0x03
488#define OUTPUT 0x04
489#define ALIAS 0x05
490/* GL_ARB_vertex_program */
491#define ADDRESS 0x06
492
493/*-----------------------------------------------------------------------
494 * From here on down is the semantic checking portion
495 *
496 */
497
498/**
499 * Variable Table Handling functions
500 */
501typedef enum
502{
503 vt_none,
504 vt_address,
505 vt_attrib,
506 vt_param,
507 vt_temp,
508 vt_output,
509 vt_alias
510} var_type;
511
512
Brian Paul7aebaf32005-10-30 21:23:23 +0000513/**
514 * Setting an explicit field for each of the binding properties is a bit
515 * wasteful of space, but it should be much more clear when reading later on..
Jouk Jansen40322e12004-04-05 08:50:36 +0000516 */
517struct var_cache
518{
Brian Paul6a769d92006-04-28 15:42:15 +0000519 const GLubyte *name; /* don't free() - no need */
Jouk Jansen40322e12004-04-05 08:50:36 +0000520 var_type type;
521 GLuint address_binding; /* The index of the address register we should
522 * be using */
523 GLuint attrib_binding; /* For type vt_attrib, see nvfragprog.h for values */
Jouk Jansen40322e12004-04-05 08:50:36 +0000524 GLuint attrib_is_generic; /* If the attrib was specified through a generic
525 * vertex attrib */
526 GLuint temp_binding; /* The index of the temp register we are to use */
Brian Paul7aebaf32005-10-30 21:23:23 +0000527 GLuint output_binding; /* Output/result register number */
Jouk Jansen40322e12004-04-05 08:50:36 +0000528 struct var_cache *alias_binding; /* For type vt_alias, points to the var_cache entry
529 * that this is aliased to */
530 GLuint param_binding_type; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
531 * PROGRAM_ENV_PARAM} */
532 GLuint param_binding_begin; /* This is the offset into the program_parameter_list where
533 * the tokens representing our bound state (or constants)
534 * start */
535 GLuint param_binding_length; /* This is how many entries in the the program_parameter_list
536 * we take up with our state tokens or constants. Note that
537 * this is _not_ the same as the number of param registers
538 * we eventually use */
539 struct var_cache *next;
540};
541
542static GLvoid
543var_cache_create (struct var_cache **va)
544{
545 *va = (struct var_cache *) _mesa_malloc (sizeof (struct var_cache));
546 if (*va) {
547 (**va).name = NULL;
548 (**va).type = vt_none;
549 (**va).attrib_binding = ~0;
550 (**va).attrib_is_generic = 0;
551 (**va).temp_binding = ~0;
552 (**va).output_binding = ~0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000553 (**va).param_binding_type = ~0;
554 (**va).param_binding_begin = ~0;
555 (**va).param_binding_length = ~0;
556 (**va).alias_binding = NULL;
557 (**va).next = NULL;
558 }
559}
560
561static GLvoid
562var_cache_destroy (struct var_cache **va)
563{
564 if (*va) {
565 var_cache_destroy (&(**va).next);
566 _mesa_free (*va);
567 *va = NULL;
568 }
569}
570
571static GLvoid
572var_cache_append (struct var_cache **va, struct var_cache *nv)
573{
574 if (*va)
575 var_cache_append (&(**va).next, nv);
576 else
577 *va = nv;
578}
579
580static struct var_cache *
Brian Paula088f162006-09-05 23:08:51 +0000581var_cache_find (struct var_cache *va, const GLubyte * name)
Jouk Jansen40322e12004-04-05 08:50:36 +0000582{
Brian Paul0a360cf2005-01-17 01:21:03 +0000583 /*struct var_cache *first = va;*/
Jouk Jansen40322e12004-04-05 08:50:36 +0000584
585 while (va) {
Brian Paulaa206952005-09-16 18:14:24 +0000586 if (!_mesa_strcmp ( (const char*) name, (const char*) va->name)) {
Jouk Jansen40322e12004-04-05 08:50:36 +0000587 if (va->type == vt_alias)
Michal Krol43343912005-01-11 15:47:16 +0000588 return va->alias_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +0000589 return va;
590 }
591
592 va = va->next;
593 }
594
595 return NULL;
596}
597
Brian Paula088f162006-09-05 23:08:51 +0000598
599
600/**
601 * Called when an error is detected while parsing/compiling a program.
602 * Sets the ctx->Program.ErrorString field to descript and records a
603 * GL_INVALID_OPERATION error.
604 * \param position position of error in program string
605 * \param descrip verbose error description
606 */
607static void
608program_error(GLcontext *ctx, GLint position, const char *descrip)
609{
610 if (descrip) {
611 const char *prefix = "glProgramString(", *suffix = ")";
612 char *str = (char *) _mesa_malloc(_mesa_strlen(descrip) +
613 _mesa_strlen(prefix) +
614 _mesa_strlen(suffix) + 1);
615 if (str) {
616 _mesa_sprintf(str, "%s%s%s", prefix, descrip, suffix);
617 _mesa_error(ctx, GL_INVALID_OPERATION, str);
618 _mesa_free(str);
619 }
620 }
621 _mesa_set_program_error(ctx, position, descrip);
622}
623
624
625
Jouk Jansen40322e12004-04-05 08:50:36 +0000626/**
627 * constructs an integer from 4 GLubytes in LE format
628 */
629static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000630parse_position (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000631{
632 GLuint value;
633
634 value = (GLuint) (*(*inst)++);
635 value += (GLuint) (*(*inst)++) * 0x100;
636 value += (GLuint) (*(*inst)++) * 0x10000;
637 value += (GLuint) (*(*inst)++) * 0x1000000;
638
639 return value;
640}
641
642/**
643 * This will, given a string, lookup the string as a variable name in the
644 * var cache. If the name is found, the var cache node corresponding to the
645 * var name is returned. If it is not found, a new entry is allocated
646 *
647 * \param I Points into the binary array where the string identifier begins
648 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
649 * \return The location on the var_cache corresponding the the string starting at I
650 */
651static struct var_cache *
Brian Paula088f162006-09-05 23:08:51 +0000652parse_string (const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +0000653 struct arb_program *Program, GLuint * found)
654{
Brian Paula088f162006-09-05 23:08:51 +0000655 const GLubyte *i = *inst;
Jouk Jansen40322e12004-04-05 08:50:36 +0000656 struct var_cache *va = NULL;
Brian Paula6c423d2004-08-25 15:59:48 +0000657 (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000658
659 *inst += _mesa_strlen ((char *) i) + 1;
660
661 va = var_cache_find (*vc_head, i);
662
663 if (va) {
664 *found = 1;
665 return va;
666 }
667
668 *found = 0;
669 var_cache_create (&va);
Brian Paul6a769d92006-04-28 15:42:15 +0000670 va->name = (const GLubyte *) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000671
672 var_cache_append (vc_head, va);
673
674 return va;
675}
676
677static char *
Brian Paula088f162006-09-05 23:08:51 +0000678parse_string_without_adding (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000679{
Brian Paula088f162006-09-05 23:08:51 +0000680 const GLubyte *i = *inst;
Brian Paula6c423d2004-08-25 15:59:48 +0000681 (void) Program;
682
Jouk Jansen40322e12004-04-05 08:50:36 +0000683 *inst += _mesa_strlen ((char *) i) + 1;
684
685 return (char *) i;
686}
687
688/**
Brian Paul05908952004-06-08 15:20:23 +0000689 * \return -1 if we parse '-', return 1 otherwise
Jouk Jansen40322e12004-04-05 08:50:36 +0000690 */
Brian Paul05908952004-06-08 15:20:23 +0000691static GLint
Brian Paula088f162006-09-05 23:08:51 +0000692parse_sign (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000693{
694 /*return *(*inst)++ != '+'; */
695
696 if (**inst == '-') {
697 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000698 return -1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000699 }
700 else if (**inst == '+') {
701 (*inst)++;
Brian Paul05908952004-06-08 15:20:23 +0000702 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000703 }
704
Brian Paul05908952004-06-08 15:20:23 +0000705 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +0000706}
707
708/**
709 * parses and returns signed integer
710 */
711static GLint
Brian Paula088f162006-09-05 23:08:51 +0000712parse_integer (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000713{
714 GLint sign;
715 GLint value;
716
717 /* check if *inst points to '+' or '-'
718 * if yes, grab the sign and increment *inst
719 */
720 sign = parse_sign (inst);
721
722 /* now check if *inst points to 0
723 * if yes, increment the *inst and return the default value
724 */
725 if (**inst == 0) {
726 (*inst)++;
727 return 0;
728 }
729
730 /* parse the integer as you normally would do it */
731 value = _mesa_atoi (parse_string_without_adding (inst, Program));
732
733 /* now, after terminating 0 there is a position
734 * to parse it - parse_position()
735 */
736 Program->Position = parse_position (inst);
737
Brian Paul05908952004-06-08 15:20:23 +0000738 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000739}
740
741/**
Brian Paul1ff8f502005-02-16 15:08:29 +0000742 Accumulate this string of digits, and return them as
743 a large integer represented in floating point (for range).
744 If scale is not NULL, also accumulates a power-of-ten
745 integer scale factor that represents the number of digits
746 in the string.
747*/
748static GLdouble
Brian Paula088f162006-09-05 23:08:51 +0000749parse_float_string(const GLubyte ** inst, struct arb_program *Program, GLdouble *scale)
Brian Paul1ff8f502005-02-16 15:08:29 +0000750{
751 GLdouble value = 0.0;
752 GLdouble oscale = 1.0;
753
754 if (**inst == 0) { /* this string of digits is empty-- do nothing */
755 (*inst)++;
756 }
757 else { /* nonempty string-- parse out the digits */
Michal Krol98e35022005-04-14 10:19:19 +0000758 while (**inst >= '0' && **inst <= '9') {
Brian Paul1ff8f502005-02-16 15:08:29 +0000759 GLubyte digit = *((*inst)++);
760 value = value * 10.0 + (GLint) (digit - '0');
761 oscale *= 10.0;
762 }
763 assert(**inst == 0); /* integer string should end with 0 */
764 (*inst)++; /* skip over terminating 0 */
765 Program->Position = parse_position(inst); /* skip position (from integer) */
766 }
767 if (scale)
768 *scale = oscale;
769 return value;
770}
771
772/**
773 Parse an unsigned floating-point number from this stream of tokenized
774 characters. Example floating-point formats supported:
775 12.34
776 12
777 0.34
778 .34
779 12.34e-4
Jouk Jansen40322e12004-04-05 08:50:36 +0000780 */
781static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000782parse_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000783{
Brian Paul1ff8f502005-02-16 15:08:29 +0000784 GLint exponent;
785 GLdouble whole, fraction, fracScale = 1.0;
Jouk Jansen40322e12004-04-05 08:50:36 +0000786
Brian Paul1ff8f502005-02-16 15:08:29 +0000787 whole = parse_float_string(inst, Program, 0);
788 fraction = parse_float_string(inst, Program, &fracScale);
789
790 /* Parse signed exponent */
791 exponent = parse_integer(inst, Program); /* This is the exponent */
Jouk Jansen40322e12004-04-05 08:50:36 +0000792
Brian Paul1ff8f502005-02-16 15:08:29 +0000793 /* Assemble parts of floating-point number: */
794 return (GLfloat) ((whole + fraction / fracScale) *
795 _mesa_pow(10.0, (GLfloat) exponent));
Jouk Jansen40322e12004-04-05 08:50:36 +0000796}
797
798
799/**
800 */
801static GLfloat
Brian Paula088f162006-09-05 23:08:51 +0000802parse_signed_float (const GLubyte ** inst, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +0000803{
Brian Paul05908952004-06-08 15:20:23 +0000804 GLint sign = parse_sign (inst);
805 GLfloat value = parse_float (inst, Program);
806 return value * sign;
Jouk Jansen40322e12004-04-05 08:50:36 +0000807}
808
809/**
810 * This picks out a constant value from the parsed array. The constant vector is r
811 * returned in the *values array, which should be of length 4.
812 *
813 * \param values - The 4 component vector with the constant value in it
814 */
815static GLvoid
Brian Paula088f162006-09-05 23:08:51 +0000816parse_constant (const GLubyte ** inst, GLfloat *values, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000817 GLboolean use)
818{
819 GLuint components, i;
820
821
822 switch (*(*inst)++) {
823 case CONSTANT_SCALAR:
824 if (use == GL_TRUE) {
825 values[0] =
826 values[1] =
827 values[2] = values[3] = parse_float (inst, Program);
828 }
829 else {
830 values[0] =
831 values[1] =
832 values[2] = values[3] = parse_signed_float (inst, Program);
833 }
834
835 break;
836 case CONSTANT_VECTOR:
837 values[0] = values[1] = values[2] = 0;
838 values[3] = 1;
839 components = *(*inst)++;
840 for (i = 0; i < components; i++) {
841 values[i] = parse_signed_float (inst, Program);
842 }
843 break;
844 }
845}
846
847/**
848 * \param offset The offset from the address register that we should
849 * address
850 *
851 * \return 0 on sucess, 1 on error
852 */
853static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000854parse_relative_offset(GLcontext *ctx, const GLubyte **inst,
855 struct arb_program *Program, GLint *offset)
Jouk Jansen40322e12004-04-05 08:50:36 +0000856{
Brian Paul6a769d92006-04-28 15:42:15 +0000857 (void) ctx;
Jouk Jansen40322e12004-04-05 08:50:36 +0000858 *offset = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000859 return 0;
860}
861
862/**
863 * \param color 0 if color type is primary, 1 if color type is secondary
864 * \return 0 on sucess, 1 on error
865 */
866static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000867parse_color_type (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000868 GLint * color)
869{
Brian Paula6c423d2004-08-25 15:59:48 +0000870 (void) ctx; (void) Program;
Jouk Jansen40322e12004-04-05 08:50:36 +0000871 *color = *(*inst)++ != COLOR_PRIMARY;
872 return 0;
873}
874
875/**
876 * Get an integer corresponding to a generic vertex attribute.
877 *
878 * \return 0 on sucess, 1 on error
879 */
880static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000881parse_generic_attrib_num(GLcontext *ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000882 struct arb_program *Program, GLuint *attrib)
883{
Brian Paulbd997cd2004-07-20 21:12:56 +0000884 GLint i = parse_integer(inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000885
Michal Krolbb38cad2006-04-11 11:41:11 +0000886 if ((i < 0) || (i >= MAX_VERTEX_PROGRAM_ATTRIBS))
Jouk Jansen40322e12004-04-05 08:50:36 +0000887 {
Brian Paula088f162006-09-05 23:08:51 +0000888 program_error(ctx, Program->Position,
889 "Invalid generic vertex attribute index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000890 return 1;
891 }
892
Brian Paulbd997cd2004-07-20 21:12:56 +0000893 *attrib = (GLuint) i;
894
Jouk Jansen40322e12004-04-05 08:50:36 +0000895 return 0;
896}
897
898
899/**
Brian Paulbe76b7f2004-10-04 14:40:05 +0000900 * \param color The index of the color buffer to write into
901 * \return 0 on sucess, 1 on error
902 */
903static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000904parse_output_color_num (GLcontext * ctx, const GLubyte ** inst,
Brian Paulbe76b7f2004-10-04 14:40:05 +0000905 struct arb_program *Program, GLuint * color)
906{
907 GLint i = parse_integer (inst, Program);
908
909 if ((i < 0) || (i >= (int)ctx->Const.MaxDrawBuffers)) {
Brian Paula088f162006-09-05 23:08:51 +0000910 program_error(ctx, Program->Position, "Invalid draw buffer index");
Brian Paulbe76b7f2004-10-04 14:40:05 +0000911 return 1;
912 }
913
914 *color = (GLuint) i;
915 return 0;
916}
917
918
919/**
Jouk Jansen40322e12004-04-05 08:50:36 +0000920 * \param coord The texture unit index
921 * \return 0 on sucess, 1 on error
922 */
923static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000924parse_texcoord_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000925 struct arb_program *Program, GLuint * coord)
926{
Brian Paulbd997cd2004-07-20 21:12:56 +0000927 GLint i = parse_integer (inst, Program);
Jouk Jansen40322e12004-04-05 08:50:36 +0000928
Brian Paula6c423d2004-08-25 15:59:48 +0000929 if ((i < 0) || (i >= (int)ctx->Const.MaxTextureUnits)) {
Brian Paula088f162006-09-05 23:08:51 +0000930 program_error(ctx, Program->Position, "Invalid texture unit index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000931 return 1;
932 }
933
Brian Paulbd997cd2004-07-20 21:12:56 +0000934 *coord = (GLuint) i;
Jouk Jansen40322e12004-04-05 08:50:36 +0000935 return 0;
936}
937
938/**
939 * \param coord The weight index
940 * \return 0 on sucess, 1 on error
941 */
942static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000943parse_weight_num (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +0000944 GLint * coord)
945{
946 *coord = parse_integer (inst, Program);
947
948 if ((*coord < 0) || (*coord >= 1)) {
Brian Paula088f162006-09-05 23:08:51 +0000949 program_error(ctx, Program->Position, "Invalid weight index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000950 return 1;
951 }
952
953 return 0;
954}
955
956/**
957 * \param coord The clip plane index
958 * \return 0 on sucess, 1 on error
959 */
960static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000961parse_clipplane_num (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +0000962 struct arb_program *Program, GLint * coord)
963{
964 *coord = parse_integer (inst, Program);
965
966 if ((*coord < 0) || (*coord >= (GLint) ctx->Const.MaxClipPlanes)) {
Brian Paula088f162006-09-05 23:08:51 +0000967 program_error(ctx, Program->Position, "Invalid clip plane index");
Jouk Jansen40322e12004-04-05 08:50:36 +0000968 return 1;
969 }
970
971 return 0;
972}
973
974
975/**
976 * \return 0 on front face, 1 on back face
977 */
978static GLuint
Brian Paula088f162006-09-05 23:08:51 +0000979parse_face_type (const GLubyte ** inst)
Jouk Jansen40322e12004-04-05 08:50:36 +0000980{
981 switch (*(*inst)++) {
982 case FACE_FRONT:
983 return 0;
984
985 case FACE_BACK:
986 return 1;
987 }
988 return 0;
989}
990
991
992/**
993 * Given a matrix and a modifier token on the binary array, return tokens
994 * that _mesa_fetch_state() [program.c] can understand.
995 *
996 * \param matrix - the matrix we are talking about
997 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
998 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
999 * \return 0 on sucess, 1 on failure
1000 */
1001static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001002parse_matrix (GLcontext * ctx, const GLubyte ** inst, struct arb_program *Program,
Jouk Jansen40322e12004-04-05 08:50:36 +00001003 GLint * matrix, GLint * matrix_idx, GLint * matrix_modifier)
1004{
1005 GLubyte mat = *(*inst)++;
1006
1007 *matrix_idx = 0;
1008
1009 switch (mat) {
1010 case MATRIX_MODELVIEW:
1011 *matrix = STATE_MODELVIEW;
1012 *matrix_idx = parse_integer (inst, Program);
1013 if (*matrix_idx > 0) {
Brian Paula088f162006-09-05 23:08:51 +00001014 program_error(ctx, Program->Position,
1015 "ARB_vertex_blend not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001016 return 1;
1017 }
1018 break;
1019
1020 case MATRIX_PROJECTION:
1021 *matrix = STATE_PROJECTION;
1022 break;
1023
1024 case MATRIX_MVP:
1025 *matrix = STATE_MVP;
1026 break;
1027
1028 case MATRIX_TEXTURE:
1029 *matrix = STATE_TEXTURE;
1030 *matrix_idx = parse_integer (inst, Program);
1031 if (*matrix_idx >= (GLint) ctx->Const.MaxTextureUnits) {
Brian Paula088f162006-09-05 23:08:51 +00001032 program_error(ctx, Program->Position, "Invalid Texture Unit");
1033 /* bad *matrix_id */
Jouk Jansen40322e12004-04-05 08:50:36 +00001034 return 1;
1035 }
1036 break;
1037
1038 /* This is not currently supported (ARB_matrix_palette) */
1039 case MATRIX_PALETTE:
1040 *matrix_idx = parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001041 program_error(ctx, Program->Position,
1042 "ARB_matrix_palette not supported");
Jouk Jansen40322e12004-04-05 08:50:36 +00001043 return 1;
1044 break;
1045
1046 case MATRIX_PROGRAM:
1047 *matrix = STATE_PROGRAM;
1048 *matrix_idx = parse_integer (inst, Program);
1049 if (*matrix_idx >= (GLint) ctx->Const.MaxProgramMatrices) {
Brian Paula088f162006-09-05 23:08:51 +00001050 program_error(ctx, Program->Position, "Invalid Program Matrix");
1051 /* bad *matrix_idx */
Jouk Jansen40322e12004-04-05 08:50:36 +00001052 return 1;
1053 }
1054 break;
1055 }
1056
1057 switch (*(*inst)++) {
1058 case MATRIX_MODIFIER_IDENTITY:
1059 *matrix_modifier = 0;
1060 break;
1061 case MATRIX_MODIFIER_INVERSE:
1062 *matrix_modifier = STATE_MATRIX_INVERSE;
1063 break;
1064 case MATRIX_MODIFIER_TRANSPOSE:
1065 *matrix_modifier = STATE_MATRIX_TRANSPOSE;
1066 break;
1067 case MATRIX_MODIFIER_INVTRANS:
1068 *matrix_modifier = STATE_MATRIX_INVTRANS;
1069 break;
1070 }
1071
1072 return 0;
1073}
1074
1075
1076/**
1077 * This parses a state string (rather, the binary version of it) into
1078 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1079 *
1080 * \param inst - the start in the binary arry to start working from
1081 * \param state_tokens - the storage for the 6-token state description
1082 * \return - 0 on sucess, 1 on error
1083 */
1084static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001085parse_state_single_item (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001086 struct arb_program *Program, GLint * state_tokens)
1087{
1088 switch (*(*inst)++) {
1089 case STATE_MATERIAL_PARSER:
1090 state_tokens[0] = STATE_MATERIAL;
1091 state_tokens[1] = parse_face_type (inst);
1092 switch (*(*inst)++) {
1093 case MATERIAL_AMBIENT:
1094 state_tokens[2] = STATE_AMBIENT;
1095 break;
1096 case MATERIAL_DIFFUSE:
1097 state_tokens[2] = STATE_DIFFUSE;
1098 break;
1099 case MATERIAL_SPECULAR:
1100 state_tokens[2] = STATE_SPECULAR;
1101 break;
1102 case MATERIAL_EMISSION:
1103 state_tokens[2] = STATE_EMISSION;
1104 break;
1105 case MATERIAL_SHININESS:
1106 state_tokens[2] = STATE_SHININESS;
1107 break;
1108 }
1109 break;
1110
1111 case STATE_LIGHT_PARSER:
1112 state_tokens[0] = STATE_LIGHT;
1113 state_tokens[1] = parse_integer (inst, Program);
1114
1115 /* Check the value of state_tokens[1] against the # of lights */
1116 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001117 program_error(ctx, Program->Position, "Invalid Light Number");
1118 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001119 return 1;
1120 }
1121
1122 switch (*(*inst)++) {
1123 case LIGHT_AMBIENT:
1124 state_tokens[2] = STATE_AMBIENT;
1125 break;
1126 case LIGHT_DIFFUSE:
1127 state_tokens[2] = STATE_DIFFUSE;
1128 break;
1129 case LIGHT_SPECULAR:
1130 state_tokens[2] = STATE_SPECULAR;
1131 break;
1132 case LIGHT_POSITION:
1133 state_tokens[2] = STATE_POSITION;
1134 break;
1135 case LIGHT_ATTENUATION:
1136 state_tokens[2] = STATE_ATTENUATION;
1137 break;
1138 case LIGHT_HALF:
1139 state_tokens[2] = STATE_HALF;
1140 break;
1141 case LIGHT_SPOT_DIRECTION:
1142 state_tokens[2] = STATE_SPOT_DIRECTION;
1143 break;
1144 }
1145 break;
1146
1147 case STATE_LIGHT_MODEL:
1148 switch (*(*inst)++) {
1149 case LIGHT_MODEL_AMBIENT:
1150 state_tokens[0] = STATE_LIGHTMODEL_AMBIENT;
1151 break;
1152 case LIGHT_MODEL_SCENECOLOR:
1153 state_tokens[0] = STATE_LIGHTMODEL_SCENECOLOR;
1154 state_tokens[1] = parse_face_type (inst);
1155 break;
1156 }
1157 break;
1158
1159 case STATE_LIGHT_PROD:
1160 state_tokens[0] = STATE_LIGHTPROD;
1161 state_tokens[1] = parse_integer (inst, Program);
1162
1163 /* Check the value of state_tokens[1] against the # of lights */
1164 if (state_tokens[1] >= (GLint) ctx->Const.MaxLights) {
Brian Paula088f162006-09-05 23:08:51 +00001165 program_error(ctx, Program->Position, "Invalid Light Number");
1166 /* bad state_tokens[1] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001167 return 1;
1168 }
1169
1170 state_tokens[2] = parse_face_type (inst);
1171 switch (*(*inst)++) {
1172 case LIGHT_PROD_AMBIENT:
1173 state_tokens[3] = STATE_AMBIENT;
1174 break;
1175 case LIGHT_PROD_DIFFUSE:
1176 state_tokens[3] = STATE_DIFFUSE;
1177 break;
1178 case LIGHT_PROD_SPECULAR:
1179 state_tokens[3] = STATE_SPECULAR;
1180 break;
1181 }
1182 break;
1183
1184
1185 case STATE_FOG:
1186 switch (*(*inst)++) {
1187 case FOG_COLOR:
1188 state_tokens[0] = STATE_FOG_COLOR;
1189 break;
1190 case FOG_PARAMS:
1191 state_tokens[0] = STATE_FOG_PARAMS;
1192 break;
1193 }
1194 break;
1195
1196 case STATE_TEX_ENV:
1197 state_tokens[1] = parse_integer (inst, Program);
1198 switch (*(*inst)++) {
1199 case TEX_ENV_COLOR:
1200 state_tokens[0] = STATE_TEXENV_COLOR;
1201 break;
1202 }
1203 break;
1204
1205 case STATE_TEX_GEN:
1206 {
1207 GLuint type, coord;
1208
1209 state_tokens[0] = STATE_TEXGEN;
1210 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1211
1212 if (parse_texcoord_num (ctx, inst, Program, &coord))
1213 return 1;
1214 state_tokens[1] = coord;
1215
1216 /* EYE or OBJECT */
1217 type = *(*inst++);
1218
1219 /* 0 - s, 1 - t, 2 - r, 3 - q */
1220 coord = *(*inst++);
1221
1222 if (type == TEX_GEN_EYE) {
1223 switch (coord) {
1224 case COMPONENT_X:
1225 state_tokens[2] = STATE_TEXGEN_EYE_S;
1226 break;
1227 case COMPONENT_Y:
1228 state_tokens[2] = STATE_TEXGEN_EYE_T;
1229 break;
1230 case COMPONENT_Z:
1231 state_tokens[2] = STATE_TEXGEN_EYE_R;
1232 break;
1233 case COMPONENT_W:
1234 state_tokens[2] = STATE_TEXGEN_EYE_Q;
1235 break;
1236 }
1237 }
1238 else {
1239 switch (coord) {
1240 case COMPONENT_X:
1241 state_tokens[2] = STATE_TEXGEN_OBJECT_S;
1242 break;
1243 case COMPONENT_Y:
1244 state_tokens[2] = STATE_TEXGEN_OBJECT_T;
1245 break;
1246 case COMPONENT_Z:
1247 state_tokens[2] = STATE_TEXGEN_OBJECT_R;
1248 break;
1249 case COMPONENT_W:
1250 state_tokens[2] = STATE_TEXGEN_OBJECT_Q;
1251 break;
1252 }
1253 }
1254 }
1255 break;
1256
1257 case STATE_DEPTH:
1258 switch (*(*inst)++) {
1259 case DEPTH_RANGE:
1260 state_tokens[0] = STATE_DEPTH_RANGE;
1261 break;
1262 }
1263 break;
1264
1265 case STATE_CLIP_PLANE:
1266 state_tokens[0] = STATE_CLIPPLANE;
1267 state_tokens[1] = parse_integer (inst, Program);
1268 if (parse_clipplane_num (ctx, inst, Program, &state_tokens[1]))
1269 return 1;
1270 break;
1271
1272 case STATE_POINT:
1273 switch (*(*inst++)) {
1274 case POINT_SIZE:
1275 state_tokens[0] = STATE_POINT_SIZE;
1276 break;
1277
1278 case POINT_ATTENUATION:
1279 state_tokens[0] = STATE_POINT_ATTENUATION;
1280 break;
1281 }
1282 break;
1283
1284 /* XXX: I think this is the correct format for a matrix row */
1285 case STATE_MATRIX_ROWS:
1286 state_tokens[0] = STATE_MATRIX;
1287 if (parse_matrix
1288 (ctx, inst, Program, &state_tokens[1], &state_tokens[2],
1289 &state_tokens[5]))
1290 return 1;
1291
1292 state_tokens[3] = parse_integer (inst, Program); /* The first row to grab */
1293
1294 if ((**inst) != 0) { /* Either the last row, 0 */
1295 state_tokens[4] = parse_integer (inst, Program);
1296 if (state_tokens[4] < state_tokens[3]) {
Brian Paula088f162006-09-05 23:08:51 +00001297 program_error(ctx, Program->Position,
1298 "Second matrix index less than the first");
1299 /* state_tokens[4] vs. state_tokens[3] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001300 return 1;
1301 }
1302 }
1303 else {
1304 state_tokens[4] = state_tokens[3];
1305 (*inst)++;
1306 }
1307 break;
1308 }
1309
1310 return 0;
1311}
1312
1313/**
1314 * This parses a state string (rather, the binary version of it) into
1315 * a 6-token similar for the state fetching code in program.c
1316 *
1317 * One might ask, why fetch these parameters into just like you fetch
1318 * state when they are already stored in other places?
1319 *
1320 * Because of array offsets -> We can stick env/local parameters in the
1321 * middle of a parameter array and then index someplace into the array
1322 * when we execute.
1323 *
1324 * One optimization might be to only do this for the cases where the
1325 * env/local parameters end up inside of an array, and leave the
1326 * single parameters (or arrays of pure env/local pareameters) in their
1327 * respective register files.
1328 *
1329 * For ENV parameters, the format is:
1330 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1331 * state_tokens[1] = STATE_ENV
1332 * state_tokens[2] = the parameter index
1333 *
1334 * for LOCAL parameters, the format is:
1335 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1336 * state_tokens[1] = STATE_LOCAL
1337 * state_tokens[2] = the parameter index
1338 *
1339 * \param inst - the start in the binary arry to start working from
1340 * \param state_tokens - the storage for the 6-token state description
1341 * \return - 0 on sucess, 1 on failure
1342 */
1343static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001344parse_program_single_item (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001345 struct arb_program *Program, GLint * state_tokens)
1346{
1347 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1348 state_tokens[0] = STATE_FRAGMENT_PROGRAM;
1349 else
1350 state_tokens[0] = STATE_VERTEX_PROGRAM;
1351
1352
1353 switch (*(*inst)++) {
1354 case PROGRAM_PARAM_ENV:
1355 state_tokens[1] = STATE_ENV;
1356 state_tokens[2] = parse_integer (inst, Program);
1357
1358 /* Check state_tokens[2] against the number of ENV parameters available */
1359 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001360 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001361 ||
1362 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001363 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxEnvParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001364 program_error(ctx, Program->Position,
1365 "Invalid Program Env Parameter");
1366 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001367 return 1;
1368 }
1369
1370 break;
1371
1372 case PROGRAM_PARAM_LOCAL:
1373 state_tokens[1] = STATE_LOCAL;
1374 state_tokens[2] = parse_integer (inst, Program);
1375
1376 /* Check state_tokens[2] against the number of LOCAL parameters available */
1377 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001378 (state_tokens[2] >= (GLint) ctx->Const.FragmentProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001379 ||
1380 ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
Brian Paul05051032005-11-01 04:36:33 +00001381 (state_tokens[2] >= (GLint) ctx->Const.VertexProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001382 program_error(ctx, Program->Position,
1383 "Invalid Program Local Parameter");
1384 /* bad state_tokens[2] */
Jouk Jansen40322e12004-04-05 08:50:36 +00001385 return 1;
1386 }
1387 break;
1388 }
1389
1390 return 0;
1391}
1392
1393/**
1394 * For ARB_vertex_program, programs are not allowed to use both an explicit
1395 * vertex attribute and a generic vertex attribute corresponding to the same
1396 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1397 *
1398 * This will walk our var_cache and make sure that nobody does anything fishy.
1399 *
1400 * \return 0 on sucess, 1 on error
1401 */
1402static GLuint
1403generic_attrib_check(struct var_cache *vc_head)
1404{
1405 int a;
1406 struct var_cache *curr;
1407 GLboolean explicitAttrib[MAX_VERTEX_PROGRAM_ATTRIBS],
1408 genericAttrib[MAX_VERTEX_PROGRAM_ATTRIBS];
1409
1410 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1411 explicitAttrib[a] = GL_FALSE;
1412 genericAttrib[a] = GL_FALSE;
1413 }
1414
1415 curr = vc_head;
1416 while (curr) {
1417 if (curr->type == vt_attrib) {
1418 if (curr->attrib_is_generic)
Brian Paul18e7c5c2005-10-30 21:46:00 +00001419 genericAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001420 else
Brian Paul18e7c5c2005-10-30 21:46:00 +00001421 explicitAttrib[ curr->attrib_binding ] = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00001422 }
1423
1424 curr = curr->next;
1425 }
1426
1427 for (a=0; a<MAX_VERTEX_PROGRAM_ATTRIBS; a++) {
1428 if ((explicitAttrib[a]) && (genericAttrib[a]))
1429 return 1;
1430 }
1431
1432 return 0;
1433}
1434
1435/**
1436 * This will handle the binding side of an ATTRIB var declaration
1437 *
Brian Paul18e7c5c2005-10-30 21:46:00 +00001438 * \param inputReg returns the input register index, one of the
1439 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
Brian Paula088f162006-09-05 23:08:51 +00001440 * \return returns 0 on success, 1 on error
Jouk Jansen40322e12004-04-05 08:50:36 +00001441 */
1442static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001443parse_attrib_binding(GLcontext * ctx, const GLubyte ** inst,
Brian Paul18e7c5c2005-10-30 21:46:00 +00001444 struct arb_program *Program,
1445 GLuint *inputReg, GLuint *is_generic)
Jouk Jansen40322e12004-04-05 08:50:36 +00001446{
Jouk Jansen40322e12004-04-05 08:50:36 +00001447 GLint err = 0;
1448
1449 *is_generic = 0;
Brian Paul18e7c5c2005-10-30 21:46:00 +00001450
Jouk Jansen40322e12004-04-05 08:50:36 +00001451 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1452 switch (*(*inst)++) {
1453 case FRAGMENT_ATTRIB_COLOR:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001454 {
1455 GLint coord;
1456 err = parse_color_type (ctx, inst, Program, &coord);
1457 *inputReg = FRAG_ATTRIB_COL0 + coord;
1458 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001459 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001460 case FRAGMENT_ATTRIB_TEXCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001461 {
1462 GLuint texcoord;
1463 err = parse_texcoord_num (ctx, inst, Program, &texcoord);
1464 *inputReg = FRAG_ATTRIB_TEX0 + texcoord;
1465 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001466 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001467 case FRAGMENT_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001468 *inputReg = FRAG_ATTRIB_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001469 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001470 case FRAGMENT_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001471 *inputReg = FRAG_ATTRIB_WPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001472 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00001473 default:
1474 err = 1;
1475 break;
1476 }
1477 }
1478 else {
1479 switch (*(*inst)++) {
1480 case VERTEX_ATTRIB_POSITION:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001481 *inputReg = VERT_ATTRIB_POS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001482 break;
1483
1484 case VERTEX_ATTRIB_WEIGHT:
1485 {
1486 GLint weight;
Jouk Jansen40322e12004-04-05 08:50:36 +00001487 err = parse_weight_num (ctx, inst, Program, &weight);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001488 *inputReg = VERT_ATTRIB_WEIGHT;
Brian Paul3a557502006-09-05 23:15:29 +00001489#if 1
1490 /* hack for Warcraft (see bug 8060) */
1491 _mesa_warning(ctx, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
Brian Pauld9aebd82006-09-06 05:03:47 +00001492 break;
Brian Paul3a557502006-09-05 23:15:29 +00001493#else
Brian Paula088f162006-09-05 23:08:51 +00001494 program_error(ctx, Program->Position,
1495 "ARB_vertex_blend not supported");
Brian Pauld9aebd82006-09-06 05:03:47 +00001496 return 1;
Brian Paul3a557502006-09-05 23:15:29 +00001497#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00001498 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001499
1500 case VERTEX_ATTRIB_NORMAL:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001501 *inputReg = VERT_ATTRIB_NORMAL;
Jouk Jansen40322e12004-04-05 08:50:36 +00001502 break;
1503
1504 case VERTEX_ATTRIB_COLOR:
1505 {
1506 GLint color;
Jouk Jansen40322e12004-04-05 08:50:36 +00001507 err = parse_color_type (ctx, inst, Program, &color);
1508 if (color) {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001509 *inputReg = VERT_ATTRIB_COLOR1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001510 }
1511 else {
Brian Paul18e7c5c2005-10-30 21:46:00 +00001512 *inputReg = VERT_ATTRIB_COLOR0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001513 }
1514 }
1515 break;
1516
1517 case VERTEX_ATTRIB_FOGCOORD:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001518 *inputReg = VERT_ATTRIB_FOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00001519 break;
1520
1521 case VERTEX_ATTRIB_TEXCOORD:
1522 {
1523 GLuint unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001524 err = parse_texcoord_num (ctx, inst, Program, &unit);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001525 *inputReg = VERT_ATTRIB_TEX0 + unit;
Jouk Jansen40322e12004-04-05 08:50:36 +00001526 }
1527 break;
1528
Jouk Jansen40322e12004-04-05 08:50:36 +00001529 case VERTEX_ATTRIB_MATRIXINDEX:
Brian Paul18e7c5c2005-10-30 21:46:00 +00001530 /* Not supported at this time */
1531 {
1532 const char *msg = "ARB_palette_matrix not supported";
1533 parse_integer (inst, Program);
Brian Paula088f162006-09-05 23:08:51 +00001534 program_error(ctx, Program->Position, msg);
Brian Paul18e7c5c2005-10-30 21:46:00 +00001535 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001536 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001537
1538 case VERTEX_ATTRIB_GENERIC:
1539 {
1540 GLuint attrib;
Tilman Sauerbeck6c334752006-06-28 16:26:20 +00001541 err = parse_generic_attrib_num(ctx, inst, Program, &attrib);
Brian Paula088f162006-09-05 23:08:51 +00001542 if (!err) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001543 *is_generic = 1;
Brian Paul095c6692006-04-25 00:21:32 +00001544 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1545 * attributes do not alias the conventional vertex
1546 * attributes.
1547 */
1548 if (attrib > 0)
1549 *inputReg = attrib + VERT_ATTRIB_GENERIC0;
Brian Paul919f6a02006-05-29 14:37:56 +00001550 else
1551 *inputReg = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001552 }
1553 }
1554 break;
1555
1556 default:
1557 err = 1;
1558 break;
1559 }
1560 }
1561
Jouk Jansen40322e12004-04-05 08:50:36 +00001562 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00001563 program_error(ctx, Program->Position, "Bad attribute binding");
Jouk Jansen40322e12004-04-05 08:50:36 +00001564 }
1565
Brian Paulde997602005-11-12 17:53:14 +00001566 Program->Base.InputsRead |= (1 << *inputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001567
1568 return err;
1569}
1570
Brian Paul7aebaf32005-10-30 21:23:23 +00001571
Jouk Jansen40322e12004-04-05 08:50:36 +00001572/**
1573 * This translates between a binary token for an output variable type
1574 * and the mesa token for the same thing.
1575 *
Brian Paul7aebaf32005-10-30 21:23:23 +00001576 * \param inst The parsed tokens
1577 * \param outputReg Returned index/number of the output register,
Brian Paul90ebb582005-11-02 18:06:12 +00001578 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
Jouk Jansen40322e12004-04-05 08:50:36 +00001579 */
1580static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001581parse_result_binding(GLcontext *ctx, const GLubyte **inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00001582 GLuint *outputReg, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00001583{
Brian Paul7aebaf32005-10-30 21:23:23 +00001584 const GLubyte token = *(*inst)++;
Jouk Jansen40322e12004-04-05 08:50:36 +00001585
Brian Paul7aebaf32005-10-30 21:23:23 +00001586 switch (token) {
Jouk Jansen40322e12004-04-05 08:50:36 +00001587 case FRAGMENT_RESULT_COLOR:
Jouk Jansen40322e12004-04-05 08:50:36 +00001588 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001589 GLuint out_color;
1590
Brian Paulbe76b7f2004-10-04 14:40:05 +00001591 /* This gets result of the color buffer we're supposed to
Brian Paul7aebaf32005-10-30 21:23:23 +00001592 * draw into. This pertains to GL_ARB_draw_buffers.
Brian Paulbe76b7f2004-10-04 14:40:05 +00001593 */
1594 parse_output_color_num(ctx, inst, Program, &out_color);
Brian Paul7aebaf32005-10-30 21:23:23 +00001595 ASSERT(out_color < MAX_DRAW_BUFFERS);
Brian Paul90ebb582005-11-02 18:06:12 +00001596 *outputReg = FRAG_RESULT_COLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001597 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001598 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001599 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1600 *outputReg = VERT_RESULT_HPOS;
Jouk Jansen40322e12004-04-05 08:50:36 +00001601 }
1602 break;
1603
1604 case FRAGMENT_RESULT_DEPTH:
Jouk Jansen40322e12004-04-05 08:50:36 +00001605 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001606 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
Brian Paul90ebb582005-11-02 18:06:12 +00001607 *outputReg = FRAG_RESULT_DEPR;
Jouk Jansen40322e12004-04-05 08:50:36 +00001608 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001609 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001610 /* for vtx programs, this is VERTEX_RESULT_COLOR */
Jouk Jansen40322e12004-04-05 08:50:36 +00001611 GLint color_type;
1612 GLuint face_type = parse_face_type(inst);
Brian Paul7aebaf32005-10-30 21:23:23 +00001613 GLint err = parse_color_type(ctx, inst, Program, &color_type);
1614 if (err)
1615 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001616
Jouk Jansen40322e12004-04-05 08:50:36 +00001617 if (face_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001618 /* back face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001619 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001620 *outputReg = VERT_RESULT_BFC1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001621 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001622 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001623 *outputReg = VERT_RESULT_BFC0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001624 }
1625 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001626 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001627 /* front face */
Jouk Jansen40322e12004-04-05 08:50:36 +00001628 if (color_type) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001629 *outputReg = VERT_RESULT_COL1; /* secondary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001630 }
1631 /* primary color */
1632 else {
Brian Paul7aebaf32005-10-30 21:23:23 +00001633 *outputReg = VERT_RESULT_COL0; /* primary color */
Jouk Jansen40322e12004-04-05 08:50:36 +00001634 }
1635 }
1636 }
1637 break;
1638
1639 case VERTEX_RESULT_FOGCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001640 *outputReg = VERT_RESULT_FOGC;
Jouk Jansen40322e12004-04-05 08:50:36 +00001641 break;
1642
1643 case VERTEX_RESULT_POINTSIZE:
Brian Paul7aebaf32005-10-30 21:23:23 +00001644 *outputReg = VERT_RESULT_PSIZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00001645 break;
1646
1647 case VERTEX_RESULT_TEXCOORD:
Brian Paul7aebaf32005-10-30 21:23:23 +00001648 {
1649 GLuint unit;
1650 if (parse_texcoord_num (ctx, inst, Program, &unit))
1651 return 1;
1652 *outputReg = VERT_RESULT_TEX0 + unit;
1653 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001654 break;
1655 }
1656
Brian Paulde997602005-11-12 17:53:14 +00001657 Program->Base.OutputsWritten |= (1 << *outputReg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001658
1659 return 0;
1660}
1661
Brian Paul7aebaf32005-10-30 21:23:23 +00001662
Jouk Jansen40322e12004-04-05 08:50:36 +00001663/**
1664 * This handles the declaration of ATTRIB variables
1665 *
1666 * XXX: Still needs
1667 * parse_vert_attrib_binding(), or something like that
1668 *
1669 * \return 0 on sucess, 1 on error
1670 */
1671static GLint
Brian Paula088f162006-09-05 23:08:51 +00001672parse_attrib (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001673 struct arb_program *Program)
1674{
1675 GLuint found;
1676 char *error_msg;
1677 struct var_cache *attrib_var;
1678
1679 attrib_var = parse_string (inst, vc_head, Program, &found);
1680 Program->Position = parse_position (inst);
1681 if (found) {
1682 error_msg = (char *)
1683 _mesa_malloc (_mesa_strlen ((char *) attrib_var->name) + 40);
1684 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1685 attrib_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001686 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001687 _mesa_free (error_msg);
1688 return 1;
1689 }
1690
1691 attrib_var->type = vt_attrib;
1692
Brian Paul7aebaf32005-10-30 21:23:23 +00001693 if (parse_attrib_binding(ctx, inst, Program, &attrib_var->attrib_binding,
Brian Paul7aebaf32005-10-30 21:23:23 +00001694 &attrib_var->attrib_is_generic))
1695 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001696
Brian Paul7aebaf32005-10-30 21:23:23 +00001697 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00001698 program_error(ctx, Program->Position,
1699 "Cannot use both a generic vertex attribute "
1700 "and a specific attribute of the same type");
Brian Paul7aebaf32005-10-30 21:23:23 +00001701 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00001702 }
1703
1704 Program->Base.NumAttributes++;
1705 return 0;
1706}
1707
1708/**
1709 * \param use -- TRUE if we're called when declaring implicit parameters,
1710 * FALSE if we're declaraing variables. This has to do with
1711 * if we get a signed or unsigned float for scalar constants
1712 */
1713static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001714parse_param_elements (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00001715 struct var_cache *param_var,
1716 struct arb_program *Program, GLboolean use)
1717{
1718 GLint idx;
Brian Paul7aebaf32005-10-30 21:23:23 +00001719 GLuint err = 0;
Jouk Jansen40322e12004-04-05 08:50:36 +00001720 GLint state_tokens[6];
1721 GLfloat const_values[4];
1722
Jouk Jansen40322e12004-04-05 08:50:36 +00001723 switch (*(*inst)++) {
1724 case PARAM_STATE_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001725 if (parse_state_single_item (ctx, inst, Program, state_tokens))
1726 return 1;
1727
1728 /* If we adding STATE_MATRIX that has multiple rows, we need to
1729 * unroll it and call _mesa_add_state_reference() for each row
1730 */
1731 if ((state_tokens[0] == STATE_MATRIX)
1732 && (state_tokens[3] != state_tokens[4])) {
1733 GLint row;
1734 GLint first_row = state_tokens[3];
1735 GLint last_row = state_tokens[4];
1736
1737 for (row = first_row; row <= last_row; row++) {
1738 state_tokens[3] = state_tokens[4] = row;
1739
Brian Paulde997602005-11-12 17:53:14 +00001740 idx = _mesa_add_state_reference(Program->Base.Parameters,
1741 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001742 if (param_var->param_binding_begin == ~0U)
1743 param_var->param_binding_begin = idx;
1744 param_var->param_binding_length++;
1745 Program->Base.NumParameters++;
1746 }
1747 }
1748 else {
Brian Paulde997602005-11-12 17:53:14 +00001749 idx = _mesa_add_state_reference(Program->Base.Parameters,
1750 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001751 if (param_var->param_binding_begin == ~0U)
1752 param_var->param_binding_begin = idx;
1753 param_var->param_binding_length++;
1754 Program->Base.NumParameters++;
1755 }
1756 break;
1757
1758 case PARAM_PROGRAM_ELEMENT:
Jouk Jansen40322e12004-04-05 08:50:36 +00001759 if (parse_program_single_item (ctx, inst, Program, state_tokens))
1760 return 1;
Brian Paulde997602005-11-12 17:53:14 +00001761 idx = _mesa_add_state_reference (Program->Base.Parameters, state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001762 if (param_var->param_binding_begin == ~0U)
1763 param_var->param_binding_begin = idx;
1764 param_var->param_binding_length++;
1765 Program->Base.NumParameters++;
1766
1767 /* Check if there is more: 0 -> we're done, else its an integer */
1768 if (**inst) {
1769 GLuint out_of_range, new_idx;
1770 GLuint start_idx = state_tokens[2] + 1;
1771 GLuint end_idx = parse_integer (inst, Program);
1772
1773 out_of_range = 0;
1774 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
1775 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001776 && (end_idx >= ctx->Const.FragmentProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001777 || ((state_tokens[1] == STATE_LOCAL)
1778 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001779 ctx->Const.FragmentProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001780 out_of_range = 1;
1781 }
1782 else {
1783 if (((state_tokens[1] == STATE_ENV)
Brian Paul05051032005-11-01 04:36:33 +00001784 && (end_idx >= ctx->Const.VertexProgram.MaxEnvParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001785 || ((state_tokens[1] == STATE_LOCAL)
1786 && (end_idx >=
Brian Paul05051032005-11-01 04:36:33 +00001787 ctx->Const.VertexProgram.MaxLocalParams)))
Jouk Jansen40322e12004-04-05 08:50:36 +00001788 out_of_range = 1;
1789 }
1790 if (out_of_range) {
Brian Paula088f162006-09-05 23:08:51 +00001791 program_error(ctx, Program->Position,
1792 "Invalid Program Parameter"); /*end_idx*/
Jouk Jansen40322e12004-04-05 08:50:36 +00001793 return 1;
1794 }
1795
1796 for (new_idx = start_idx; new_idx <= end_idx; new_idx++) {
1797 state_tokens[2] = new_idx;
Brian Paulde997602005-11-12 17:53:14 +00001798 idx = _mesa_add_state_reference(Program->Base.Parameters,
1799 state_tokens);
Jouk Jansen40322e12004-04-05 08:50:36 +00001800 param_var->param_binding_length++;
1801 Program->Base.NumParameters++;
1802 }
1803 }
Brian Paul7aebaf32005-10-30 21:23:23 +00001804 else {
1805 (*inst)++;
1806 }
Jouk Jansen40322e12004-04-05 08:50:36 +00001807 break;
1808
1809 case PARAM_CONSTANT:
1810 parse_constant (inst, const_values, Program, use);
Brian Paulde997602005-11-12 17:53:14 +00001811 idx = _mesa_add_named_constant(Program->Base.Parameters,
1812 (char *) param_var->name,
Brian Paul0c6723a2006-11-15 23:38:02 +00001813 const_values, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00001814 if (param_var->param_binding_begin == ~0U)
1815 param_var->param_binding_begin = idx;
1816 param_var->param_binding_length++;
1817 Program->Base.NumParameters++;
1818 break;
1819
1820 default:
Brian Paula088f162006-09-05 23:08:51 +00001821 program_error(ctx, Program->Position,
1822 "Unexpected token (in parse_param_elements())");
Jouk Jansen40322e12004-04-05 08:50:36 +00001823 return 1;
1824 }
1825
1826 /* Make sure we haven't blown past our parameter limits */
1827 if (((Program->Base.Target == GL_VERTEX_PROGRAM_ARB) &&
1828 (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001829 ctx->Const.VertexProgram.MaxLocalParams))
Jouk Jansen40322e12004-04-05 08:50:36 +00001830 || ((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
1831 && (Program->Base.NumParameters >=
Brian Paul05051032005-11-01 04:36:33 +00001832 ctx->Const.FragmentProgram.MaxLocalParams))) {
Brian Paula088f162006-09-05 23:08:51 +00001833 program_error(ctx, Program->Position, "Too many parameter variables");
Jouk Jansen40322e12004-04-05 08:50:36 +00001834 return 1;
1835 }
1836
1837 return err;
1838}
1839
Brian Paul7aebaf32005-10-30 21:23:23 +00001840
Jouk Jansen40322e12004-04-05 08:50:36 +00001841/**
1842 * This picks out PARAM program parameter bindings.
1843 *
1844 * XXX: This needs to be stressed & tested
1845 *
1846 * \return 0 on sucess, 1 on error
1847 */
1848static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001849parse_param (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001850 struct arb_program *Program)
1851{
Brian Paulbd997cd2004-07-20 21:12:56 +00001852 GLuint found, err;
1853 GLint specified_length;
Jouk Jansen40322e12004-04-05 08:50:36 +00001854 struct var_cache *param_var;
1855
1856 err = 0;
1857 param_var = parse_string (inst, vc_head, Program, &found);
1858 Program->Position = parse_position (inst);
1859
1860 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001861 char *error_msg = (char *)
1862 _mesa_malloc (_mesa_strlen ((char *) param_var->name) + 40);
Jouk Jansen40322e12004-04-05 08:50:36 +00001863 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1864 param_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001865 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001866 _mesa_free (error_msg);
1867 return 1;
1868 }
1869
1870 specified_length = parse_integer (inst, Program);
1871
1872 if (specified_length < 0) {
Brian Paula088f162006-09-05 23:08:51 +00001873 program_error(ctx, Program->Position, "Negative parameter array length");
Jouk Jansen40322e12004-04-05 08:50:36 +00001874 return 1;
1875 }
1876
1877 param_var->type = vt_param;
1878 param_var->param_binding_length = 0;
1879
1880 /* Right now, everything is shoved into the main state register file.
1881 *
1882 * In the future, it would be nice to leave things ENV/LOCAL params
1883 * in their respective register files, if possible
1884 */
1885 param_var->param_binding_type = PROGRAM_STATE_VAR;
1886
1887 /* Remember to:
1888 * * - add each guy to the parameter list
1889 * * - increment the param_var->param_binding_len
1890 * * - store the param_var->param_binding_begin for the first one
1891 * * - compare the actual len to the specified len at the end
1892 */
1893 while (**inst != PARAM_NULL) {
1894 if (parse_param_elements (ctx, inst, param_var, Program, GL_FALSE))
1895 return 1;
1896 }
1897
1898 /* Test array length here! */
1899 if (specified_length) {
Brian Paula6c423d2004-08-25 15:59:48 +00001900 if (specified_length != (int)param_var->param_binding_length) {
Brian Paula088f162006-09-05 23:08:51 +00001901 program_error(ctx, Program->Position,
1902 "Declared parameter array length does not match parameter list");
Jouk Jansen40322e12004-04-05 08:50:36 +00001903 }
1904 }
1905
1906 (*inst)++;
1907
1908 return 0;
1909}
1910
1911/**
1912 *
1913 */
1914static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001915parse_param_use (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001916 struct arb_program *Program, struct var_cache **new_var)
1917{
1918 struct var_cache *param_var;
1919
1920 /* First, insert a dummy entry into the var_cache */
1921 var_cache_create (&param_var);
Brian Paul6a769d92006-04-28 15:42:15 +00001922 param_var->name = (const GLubyte *) " ";
Jouk Jansen40322e12004-04-05 08:50:36 +00001923 param_var->type = vt_param;
1924
1925 param_var->param_binding_length = 0;
1926 /* Don't fill in binding_begin; We use the default value of -1
1927 * to tell if its already initialized, elsewhere.
1928 *
1929 * param_var->param_binding_begin = 0;
1930 */
1931 param_var->param_binding_type = PROGRAM_STATE_VAR;
1932
1933 var_cache_append (vc_head, param_var);
1934
1935 /* Then fill it with juicy parameter goodness */
1936 if (parse_param_elements (ctx, inst, param_var, Program, GL_TRUE))
1937 return 1;
1938
1939 *new_var = param_var;
1940
1941 return 0;
1942}
1943
1944
1945/**
1946 * This handles the declaration of TEMP variables
1947 *
1948 * \return 0 on sucess, 1 on error
1949 */
1950static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001951parse_temp (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001952 struct arb_program *Program)
1953{
1954 GLuint found;
1955 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00001956
1957 while (**inst != 0) {
1958 temp_var = parse_string (inst, vc_head, Program, &found);
1959 Program->Position = parse_position (inst);
1960 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00001961 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00001962 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
1963 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
1964 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00001965 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00001966 _mesa_free (error_msg);
1967 return 1;
1968 }
1969
1970 temp_var->type = vt_temp;
1971
1972 if (((Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) &&
1973 (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001974 ctx->Const.FragmentProgram.MaxTemps))
Jouk Jansen40322e12004-04-05 08:50:36 +00001975 || ((Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
1976 && (Program->Base.NumTemporaries >=
Brian Paul05051032005-11-01 04:36:33 +00001977 ctx->Const.VertexProgram.MaxTemps))) {
Brian Paula088f162006-09-05 23:08:51 +00001978 program_error(ctx, Program->Position,
1979 "Too many TEMP variables declared");
Jouk Jansen40322e12004-04-05 08:50:36 +00001980 return 1;
1981 }
1982
1983 temp_var->temp_binding = Program->Base.NumTemporaries;
1984 Program->Base.NumTemporaries++;
1985 }
1986 (*inst)++;
1987
1988 return 0;
1989}
1990
1991/**
1992 * This handles variables of the OUTPUT variety
1993 *
1994 * \return 0 on sucess, 1 on error
1995 */
1996static GLuint
Brian Paula088f162006-09-05 23:08:51 +00001997parse_output (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00001998 struct arb_program *Program)
1999{
2000 GLuint found;
2001 struct var_cache *output_var;
Brian Paul7aebaf32005-10-30 21:23:23 +00002002 GLuint err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002003
2004 output_var = parse_string (inst, vc_head, Program, &found);
2005 Program->Position = parse_position (inst);
2006 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002007 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002008 _mesa_malloc (_mesa_strlen ((char *) output_var->name) + 40);
2009 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2010 output_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002011 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002012 _mesa_free (error_msg);
2013 return 1;
2014 }
2015
2016 output_var->type = vt_output;
Brian Paul7aebaf32005-10-30 21:23:23 +00002017
2018 err = parse_result_binding(ctx, inst, &output_var->output_binding, Program);
2019 return err;
Jouk Jansen40322e12004-04-05 08:50:36 +00002020}
2021
2022/**
2023 * This handles variables of the ALIAS kind
2024 *
2025 * \return 0 on sucess, 1 on error
2026 */
2027static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002028parse_alias (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002029 struct arb_program *Program)
2030{
2031 GLuint found;
2032 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002033
2034 temp_var = parse_string (inst, vc_head, Program, &found);
2035 Program->Position = parse_position (inst);
2036
2037 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002038 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002039 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2040 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2041 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002042 program_error(ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002043 _mesa_free (error_msg);
2044 return 1;
2045 }
2046
2047 temp_var->type = vt_alias;
2048 temp_var->alias_binding = parse_string (inst, vc_head, Program, &found);
2049 Program->Position = parse_position (inst);
2050
2051 if (!found)
2052 {
Brian Paul7aebaf32005-10-30 21:23:23 +00002053 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002054 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2055 _mesa_sprintf (error_msg, "Alias value %s is not defined",
2056 temp_var->alias_binding->name);
Brian Paula088f162006-09-05 23:08:51 +00002057 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002058 _mesa_free (error_msg);
2059 return 1;
2060 }
2061
2062 return 0;
2063}
2064
2065/**
2066 * This handles variables of the ADDRESS kind
2067 *
2068 * \return 0 on sucess, 1 on error
2069 */
2070static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002071parse_address (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002072 struct arb_program *Program)
2073{
2074 GLuint found;
2075 struct var_cache *temp_var;
Jouk Jansen40322e12004-04-05 08:50:36 +00002076
2077 while (**inst != 0) {
2078 temp_var = parse_string (inst, vc_head, Program, &found);
2079 Program->Position = parse_position (inst);
2080 if (found) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002081 char *error_msg = (char *)
Jouk Jansen40322e12004-04-05 08:50:36 +00002082 _mesa_malloc (_mesa_strlen ((char *) temp_var->name) + 40);
2083 _mesa_sprintf (error_msg, "Duplicate Varible Declaration: %s",
2084 temp_var->name);
Brian Paula088f162006-09-05 23:08:51 +00002085 program_error (ctx, Program->Position, error_msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002086 _mesa_free (error_msg);
2087 return 1;
2088 }
2089
2090 temp_var->type = vt_address;
2091
2092 if (Program->Base.NumAddressRegs >=
Brian Paul05051032005-11-01 04:36:33 +00002093 ctx->Const.VertexProgram.MaxAddressRegs) {
Brian Paul7aebaf32005-10-30 21:23:23 +00002094 const char *msg = "Too many ADDRESS variables declared";
Brian Paula088f162006-09-05 23:08:51 +00002095 program_error(ctx, Program->Position, msg);
Jouk Jansen40322e12004-04-05 08:50:36 +00002096 return 1;
2097 }
2098
2099 temp_var->address_binding = Program->Base.NumAddressRegs;
2100 Program->Base.NumAddressRegs++;
2101 }
2102 (*inst)++;
2103
2104 return 0;
2105}
2106
2107/**
2108 * Parse a program declaration
2109 *
2110 * \return 0 on sucess, 1 on error
2111 */
2112static GLint
Brian Paula088f162006-09-05 23:08:51 +00002113parse_declaration (GLcontext * ctx, const GLubyte ** inst, struct var_cache **vc_head,
Jouk Jansen40322e12004-04-05 08:50:36 +00002114 struct arb_program *Program)
2115{
2116 GLint err = 0;
2117
2118 switch (*(*inst)++) {
2119 case ADDRESS:
2120 err = parse_address (ctx, inst, vc_head, Program);
2121 break;
2122
2123 case ALIAS:
2124 err = parse_alias (ctx, inst, vc_head, Program);
2125 break;
2126
2127 case ATTRIB:
2128 err = parse_attrib (ctx, inst, vc_head, Program);
2129 break;
2130
2131 case OUTPUT:
2132 err = parse_output (ctx, inst, vc_head, Program);
2133 break;
2134
2135 case PARAM:
2136 err = parse_param (ctx, inst, vc_head, Program);
2137 break;
2138
2139 case TEMP:
2140 err = parse_temp (ctx, inst, vc_head, Program);
2141 break;
2142 }
2143
2144 return err;
2145}
2146
2147/**
Brian Paul7aebaf32005-10-30 21:23:23 +00002148 * Handle the parsing out of a masked destination register, either for a
2149 * vertex or fragment program.
Jouk Jansen40322e12004-04-05 08:50:36 +00002150 *
2151 * If we are a vertex program, make sure we don't write to
Brian Paul7aebaf32005-10-30 21:23:23 +00002152 * result.position if we have specified that the program is
Jouk Jansen40322e12004-04-05 08:50:36 +00002153 * position invariant
2154 *
2155 * \param File - The register file we write to
2156 * \param Index - The register index we write to
2157 * \param WriteMask - The mask controlling which components we write (1->write)
2158 *
2159 * \return 0 on sucess, 1 on error
2160 */
2161static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002162parse_masked_dst_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002163 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7aebaf32005-10-30 21:23:23 +00002164 enum register_file *File, GLuint *Index, GLint *WriteMask)
Jouk Jansen40322e12004-04-05 08:50:36 +00002165{
Brian Paul7aebaf32005-10-30 21:23:23 +00002166 GLuint tmp, result;
Jouk Jansen40322e12004-04-05 08:50:36 +00002167 struct var_cache *dst;
2168
2169 /* We either have a result register specified, or a
2170 * variable that may or may not be writable
2171 */
2172 switch (*(*inst)++) {
2173 case REGISTER_RESULT:
Brian Paul7aebaf32005-10-30 21:23:23 +00002174 if (parse_result_binding(ctx, inst, Index, Program))
Jouk Jansen40322e12004-04-05 08:50:36 +00002175 return 1;
2176 *File = PROGRAM_OUTPUT;
2177 break;
2178
2179 case REGISTER_ESTABLISHED_NAME:
2180 dst = parse_string (inst, vc_head, Program, &result);
2181 Program->Position = parse_position (inst);
2182
2183 /* If the name has never been added to our symbol table, we're hosed */
2184 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002185 program_error(ctx, Program->Position, "0: Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002186 return 1;
2187 }
2188
2189 switch (dst->type) {
2190 case vt_output:
2191 *File = PROGRAM_OUTPUT;
Brian Paul7aebaf32005-10-30 21:23:23 +00002192 *Index = dst->output_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002193 break;
2194
2195 case vt_temp:
2196 *File = PROGRAM_TEMPORARY;
2197 *Index = dst->temp_binding;
2198 break;
2199
2200 /* If the var type is not vt_output or vt_temp, no go */
2201 default:
Brian Paula088f162006-09-05 23:08:51 +00002202 program_error(ctx, Program->Position,
2203 "Destination register is read only");
Jouk Jansen40322e12004-04-05 08:50:36 +00002204 return 1;
2205 }
2206 break;
2207
2208 default:
Brian Paula088f162006-09-05 23:08:51 +00002209 program_error(ctx, Program->Position,
2210 "Unexpected opcode in parse_masked_dst_reg()");
Jouk Jansen40322e12004-04-05 08:50:36 +00002211 return 1;
2212 }
2213
2214
2215 /* Position invariance test */
2216 /* This test is done now in syntax portion - when position invariance OPTION
2217 is specified, "result.position" rule is disabled so there is no way
2218 to write the position
2219 */
2220 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2221 (*Index == 0)) {
Brian Paula088f162006-09-05 23:08:51 +00002222 program_error(ctx, Program->Position,
Jouk Jansen40322e12004-04-05 08:50:36 +00002223 "Vertex program specified position invariance and wrote vertex position");
2224 }*/
2225
2226 /* And then the mask.
2227 * w,a -> bit 0
2228 * z,b -> bit 1
2229 * y,g -> bit 2
2230 * x,r -> bit 3
Keith Whitwell7c26b612005-04-21 14:46:57 +00002231 *
2232 * ==> Need to reverse the order of bits for this!
Jouk Jansen40322e12004-04-05 08:50:36 +00002233 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002234 tmp = (GLint) *(*inst)++;
2235 *WriteMask = (((tmp>>3) & 0x1) |
2236 ((tmp>>1) & 0x2) |
2237 ((tmp<<1) & 0x4) |
2238 ((tmp<<3) & 0x8));
Jouk Jansen40322e12004-04-05 08:50:36 +00002239
2240 return 0;
2241}
2242
2243
2244/**
2245 * Handle the parsing of a address register
2246 *
2247 * \param Index - The register index we write to
2248 *
2249 * \return 0 on sucess, 1 on error
2250 */
2251static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002252parse_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002253 struct var_cache **vc_head,
2254 struct arb_program *Program, GLint * Index)
2255{
2256 struct var_cache *dst;
2257 GLuint result;
Aapo Tahkola6fc864b2006-03-22 21:29:15 +00002258
2259 *Index = 0; /* XXX */
Jouk Jansen40322e12004-04-05 08:50:36 +00002260
2261 dst = parse_string (inst, vc_head, Program, &result);
2262 Program->Position = parse_position (inst);
2263
2264 /* If the name has never been added to our symbol table, we're hosed */
2265 if (!result) {
Brian Paula088f162006-09-05 23:08:51 +00002266 program_error(ctx, Program->Position, "Undefined variable");
Jouk Jansen40322e12004-04-05 08:50:36 +00002267 return 1;
2268 }
2269
2270 if (dst->type != vt_address) {
Brian Paula088f162006-09-05 23:08:51 +00002271 program_error(ctx, Program->Position, "Variable is not of type ADDRESS");
Jouk Jansen40322e12004-04-05 08:50:36 +00002272 return 1;
2273 }
2274
2275 return 0;
2276}
2277
Brian Paul8e8fa632005-07-01 02:03:33 +00002278#if 0 /* unused */
Jouk Jansen40322e12004-04-05 08:50:36 +00002279/**
2280 * Handle the parsing out of a masked address register
2281 *
2282 * \param Index - The register index we write to
2283 * \param WriteMask - The mask controlling which components we write (1->write)
2284 *
2285 * \return 0 on sucess, 1 on error
2286 */
2287static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002288parse_masked_address_reg (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002289 struct var_cache **vc_head,
2290 struct arb_program *Program, GLint * Index,
2291 GLboolean * WriteMask)
2292{
2293 if (parse_address_reg (ctx, inst, vc_head, Program, Index))
2294 return 1;
2295
2296 /* This should be 0x8 */
2297 (*inst)++;
2298
2299 /* Writemask of .x is implied */
2300 WriteMask[0] = 1;
2301 WriteMask[1] = WriteMask[2] = WriteMask[3] = 0;
2302
2303 return 0;
2304}
Brian Paul8e8fa632005-07-01 02:03:33 +00002305#endif
Jouk Jansen40322e12004-04-05 08:50:36 +00002306
2307/**
2308 * Parse out a swizzle mask.
2309 *
Brian Paul32df89e2005-10-29 18:26:43 +00002310 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
Jouk Jansen40322e12004-04-05 08:50:36 +00002311 *
2312 * The len parameter allows us to grab 4 components for a vector
2313 * swizzle, or just 1 component for a scalar src register selection
2314 */
Brian Paul32df89e2005-10-29 18:26:43 +00002315static void
Brian Paula088f162006-09-05 23:08:51 +00002316parse_swizzle_mask(const GLubyte ** inst, GLubyte *swizzle, GLint len)
Jouk Jansen40322e12004-04-05 08:50:36 +00002317{
Brian Paul32df89e2005-10-29 18:26:43 +00002318 GLint i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002319
Brian Paul32df89e2005-10-29 18:26:43 +00002320 for (i = 0; i < 4; i++)
2321 swizzle[i] = i;
Jouk Jansen40322e12004-04-05 08:50:36 +00002322
Brian Paul32df89e2005-10-29 18:26:43 +00002323 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00002324 switch (*(*inst)++) {
2325 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002326 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002327 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002328 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002329 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002330 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002331 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002332 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002333 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00002334 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002335 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002336 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002337 default:
2338 _mesa_problem(NULL, "bad component in parse_swizzle_mask()");
2339 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002340 }
2341 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002342}
2343
Jouk Jansen40322e12004-04-05 08:50:36 +00002344
Brian Paul32df89e2005-10-29 18:26:43 +00002345/**
2346 * Parse an extended swizzle mask which is a sequence of
2347 * four x/y/z/w/0/1 tokens.
2348 * \return swizzle four swizzle values
2349 * \return negateMask four element bitfield
2350 */
2351static void
Brian Paula088f162006-09-05 23:08:51 +00002352parse_extended_swizzle_mask(const GLubyte **inst, GLubyte swizzle[4],
Brian Paul32df89e2005-10-29 18:26:43 +00002353 GLubyte *negateMask)
2354{
2355 GLint i;
2356
2357 *negateMask = 0x0;
2358 for (i = 0; i < 4; i++) {
2359 GLubyte swz;
2360 if (parse_sign(inst) == -1)
2361 *negateMask |= (1 << i);
Jouk Jansen40322e12004-04-05 08:50:36 +00002362
2363 swz = *(*inst)++;
2364
2365 switch (swz) {
2366 case COMPONENT_0:
Brian Paul32df89e2005-10-29 18:26:43 +00002367 swizzle[i] = SWIZZLE_ZERO;
Jouk Jansen40322e12004-04-05 08:50:36 +00002368 break;
2369 case COMPONENT_1:
Brian Paul32df89e2005-10-29 18:26:43 +00002370 swizzle[i] = SWIZZLE_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002371 break;
2372 case COMPONENT_X:
Brian Paul32df89e2005-10-29 18:26:43 +00002373 swizzle[i] = SWIZZLE_X;
Jouk Jansen40322e12004-04-05 08:50:36 +00002374 break;
2375 case COMPONENT_Y:
Brian Paul32df89e2005-10-29 18:26:43 +00002376 swizzle[i] = SWIZZLE_Y;
Jouk Jansen40322e12004-04-05 08:50:36 +00002377 break;
2378 case COMPONENT_Z:
Brian Paul32df89e2005-10-29 18:26:43 +00002379 swizzle[i] = SWIZZLE_Z;
Jouk Jansen40322e12004-04-05 08:50:36 +00002380 break;
2381 case COMPONENT_W:
Brian Paul32df89e2005-10-29 18:26:43 +00002382 swizzle[i] = SWIZZLE_W;
Jouk Jansen40322e12004-04-05 08:50:36 +00002383 break;
Brian Paul32df89e2005-10-29 18:26:43 +00002384 default:
2385 _mesa_problem(NULL, "bad case in parse_extended_swizzle_mask()");
2386 return;
Jouk Jansen40322e12004-04-05 08:50:36 +00002387 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002388 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002389}
2390
2391
2392static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002393parse_src_reg (GLcontext * ctx, const GLubyte ** inst,
2394 struct var_cache **vc_head,
Brian Paul7aebaf32005-10-30 21:23:23 +00002395 struct arb_program *Program,
2396 enum register_file * File, GLint * Index,
Jouk Jansen40322e12004-04-05 08:50:36 +00002397 GLboolean *IsRelOffset )
2398{
2399 struct var_cache *src;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002400 GLuint binding, is_generic, found;
Brian Paulbd997cd2004-07-20 21:12:56 +00002401 GLint offset;
Jouk Jansen40322e12004-04-05 08:50:36 +00002402
Keith Whitwell7c26b612005-04-21 14:46:57 +00002403 *IsRelOffset = 0;
2404
Jouk Jansen40322e12004-04-05 08:50:36 +00002405 /* And the binding for the src */
2406 switch (*(*inst)++) {
2407 case REGISTER_ATTRIB:
2408 if (parse_attrib_binding
Brian Paul18e7c5c2005-10-30 21:46:00 +00002409 (ctx, inst, Program, &binding, &is_generic))
Jouk Jansen40322e12004-04-05 08:50:36 +00002410 return 1;
2411 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002412 *Index = binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002413
2414 /* We need to insert a dummy variable into the var_cache so we can
2415 * catch generic vertex attrib aliasing errors
2416 */
2417 var_cache_create(&src);
2418 src->type = vt_attrib;
Brian Paul6a769d92006-04-28 15:42:15 +00002419 src->name = (const GLubyte *) "Dummy Attrib Variable";
Brian Paul18e7c5c2005-10-30 21:46:00 +00002420 src->attrib_binding = binding;
2421 src->attrib_is_generic = is_generic;
Jouk Jansen40322e12004-04-05 08:50:36 +00002422 var_cache_append(vc_head, src);
2423 if (generic_attrib_check(*vc_head)) {
Brian Paula088f162006-09-05 23:08:51 +00002424 program_error(ctx, Program->Position,
2425 "Cannot use both a generic vertex attribute "
2426 "and a specific attribute of the same type");
Jouk Jansen40322e12004-04-05 08:50:36 +00002427 return 1;
2428 }
2429 break;
2430
2431 case REGISTER_PARAM:
2432 switch (**inst) {
2433 case PARAM_ARRAY_ELEMENT:
2434 (*inst)++;
2435 src = parse_string (inst, vc_head, Program, &found);
2436 Program->Position = parse_position (inst);
2437
2438 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002439 program_error(ctx, Program->Position,
2440 "2: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002441 return 1;
2442 }
2443
Brian Paul95801792005-12-06 15:41:43 +00002444 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002445
2446 switch (*(*inst)++) {
2447 case ARRAY_INDEX_ABSOLUTE:
2448 offset = parse_integer (inst, Program);
2449
2450 if ((offset < 0)
Brian Paula6c423d2004-08-25 15:59:48 +00002451 || (offset >= (int)src->param_binding_length)) {
Brian Paula088f162006-09-05 23:08:51 +00002452 program_error(ctx, Program->Position,
2453 "Index out of range");
2454 /* offset, src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002455 return 1;
2456 }
2457
2458 *Index = src->param_binding_begin + offset;
2459 break;
2460
2461 case ARRAY_INDEX_RELATIVE:
2462 {
2463 GLint addr_reg_idx, rel_off;
2464
2465 /* First, grab the address regiseter */
2466 if (parse_address_reg (ctx, inst, vc_head, Program, &addr_reg_idx))
2467 return 1;
2468
2469 /* And the .x */
2470 ((*inst)++);
2471 ((*inst)++);
2472 ((*inst)++);
2473 ((*inst)++);
2474
2475 /* Then the relative offset */
2476 if (parse_relative_offset(ctx, inst, Program, &rel_off)) return 1;
2477
2478 /* And store it properly */
2479 *Index = src->param_binding_begin + rel_off;
2480 *IsRelOffset = 1;
2481 }
2482 break;
2483 }
2484 break;
2485
2486 default:
Jouk Jansen40322e12004-04-05 08:50:36 +00002487 if (parse_param_use (ctx, inst, vc_head, Program, &src))
2488 return 1;
2489
Brian Paul95801792005-12-06 15:41:43 +00002490 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002491 *Index = src->param_binding_begin;
2492 break;
2493 }
2494 break;
2495
2496 case REGISTER_ESTABLISHED_NAME:
Jouk Jansen40322e12004-04-05 08:50:36 +00002497 src = parse_string (inst, vc_head, Program, &found);
2498 Program->Position = parse_position (inst);
2499
2500 /* If the name has never been added to our symbol table, we're hosed */
2501 if (!found) {
Brian Paula088f162006-09-05 23:08:51 +00002502 program_error(ctx, Program->Position,
2503 "3: Undefined variable"); /* src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002504 return 1;
2505 }
2506
2507 switch (src->type) {
2508 case vt_attrib:
2509 *File = PROGRAM_INPUT;
Brian Paul18e7c5c2005-10-30 21:46:00 +00002510 *Index = src->attrib_binding;
Jouk Jansen40322e12004-04-05 08:50:36 +00002511 break;
2512
2513 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2514 case vt_param:
Brian Paul95801792005-12-06 15:41:43 +00002515 *File = (enum register_file) src->param_binding_type;
Jouk Jansen40322e12004-04-05 08:50:36 +00002516 *Index = src->param_binding_begin;
2517 break;
2518
2519 case vt_temp:
2520 *File = PROGRAM_TEMPORARY;
2521 *Index = src->temp_binding;
2522 break;
2523
2524 /* If the var type is vt_output no go */
2525 default:
Brian Paula088f162006-09-05 23:08:51 +00002526 program_error(ctx, Program->Position,
2527 "destination register is read only");
2528 /* bad src->name */
Jouk Jansen40322e12004-04-05 08:50:36 +00002529 return 1;
2530 }
2531 break;
2532
2533 default:
Brian Paula088f162006-09-05 23:08:51 +00002534 program_error(ctx, Program->Position,
2535 "Unknown token in parse_src_reg");
Jouk Jansen40322e12004-04-05 08:50:36 +00002536 return 1;
2537 }
2538
2539 return 0;
2540}
2541
2542/**
Brian Paul18e7c5c2005-10-30 21:46:00 +00002543 * Parse fragment program vector source register.
Jouk Jansen40322e12004-04-05 08:50:36 +00002544 */
2545static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002546parse_fp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00002547 struct var_cache **vc_head,
2548 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00002549 struct prog_src_register *reg)
Jouk Jansen40322e12004-04-05 08:50:36 +00002550{
Brian Paul7aebaf32005-10-30 21:23:23 +00002551 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00002552 GLint index;
2553 GLboolean negate;
2554 GLubyte swizzle[4];
2555 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002556
Jouk Jansen40322e12004-04-05 08:50:36 +00002557 /* Grab the sign */
Brian Paul32df89e2005-10-29 18:26:43 +00002558 negate = (parse_sign (inst) == -1) ? 0xf : 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00002559
2560 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00002561 if (parse_src_reg(ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Jouk Jansen40322e12004-04-05 08:50:36 +00002562 return 1;
2563
2564 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002565 parse_swizzle_mask(inst, swizzle, 4);
Jouk Jansen40322e12004-04-05 08:50:36 +00002566
Brian Paul32df89e2005-10-29 18:26:43 +00002567 reg->File = file;
2568 reg->Index = index;
Brian Paul32df89e2005-10-29 18:26:43 +00002569 reg->NegateBase = negate;
2570 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
Jouk Jansen40322e12004-04-05 08:50:36 +00002571 return 0;
2572}
2573
Jouk Jansen40322e12004-04-05 08:50:36 +00002574
Brian Paulb7fc1c32006-08-30 23:38:03 +00002575/**
2576 * Parse fragment program destination register.
2577 * \return 1 if error, 0 if no error.
2578 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002579static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002580parse_fp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00002581 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002582 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002583{
Brian Paul7aebaf32005-10-30 21:23:23 +00002584 GLint mask;
2585 GLuint idx;
2586 enum register_file file;
2587
Keith Whitwell7c26b612005-04-21 14:46:57 +00002588 if (parse_masked_dst_reg (ctx, inst, vc_head, Program, &file, &idx, &mask))
Jouk Jansen40322e12004-04-05 08:50:36 +00002589 return 1;
2590
Keith Whitwell7c26b612005-04-21 14:46:57 +00002591 reg->File = file;
2592 reg->Index = idx;
2593 reg->WriteMask = mask;
2594 return 0;
2595}
2596
2597
Brian Paul7aebaf32005-10-30 21:23:23 +00002598/**
2599 * Parse fragment program scalar src register.
Brian Paulb7fc1c32006-08-30 23:38:03 +00002600 * \return 1 if error, 0 if no error.
Brian Paul7aebaf32005-10-30 21:23:23 +00002601 */
Keith Whitwell7c26b612005-04-21 14:46:57 +00002602static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002603parse_fp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00002604 struct var_cache **vc_head,
2605 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002606 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00002607{
Brian Paul7aebaf32005-10-30 21:23:23 +00002608 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002609 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00002610 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002611 GLubyte Swizzle[4];
2612 GLboolean IsRelOffset;
2613
2614 /* Grab the sign */
2615 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
2616
2617 /* And the src reg */
2618 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
2619 return 1;
2620
2621 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00002622 parse_swizzle_mask(inst, Swizzle, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002623
Keith Whitwell7c26b612005-04-21 14:46:57 +00002624 reg->File = File;
2625 reg->Index = Index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002626 reg->NegateBase = Negate;
2627 reg->Swizzle = (Swizzle[0] << 0);
2628
Jouk Jansen40322e12004-04-05 08:50:36 +00002629 return 0;
2630}
2631
Keith Whitwell7c26b612005-04-21 14:46:57 +00002632
Jouk Jansen40322e12004-04-05 08:50:36 +00002633/**
2634 * This is a big mother that handles getting opcodes into the instruction
2635 * and handling the src & dst registers for fragment program instructions
Brian Paulb7fc1c32006-08-30 23:38:03 +00002636 * \return 1 if error, 0 if no error
Jouk Jansen40322e12004-04-05 08:50:36 +00002637 */
2638static GLuint
Brian Paula088f162006-09-05 23:08:51 +00002639parse_fp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00002640 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00002641 struct prog_instruction *fp)
Jouk Jansen40322e12004-04-05 08:50:36 +00002642{
Keith Whitwell7c26b612005-04-21 14:46:57 +00002643 GLint a;
Jouk Jansen40322e12004-04-05 08:50:36 +00002644 GLuint texcoord;
2645 GLubyte instClass, type, code;
2646 GLboolean rel;
2647
Brian Pauld6272e02006-10-29 18:03:16 +00002648 _mesa_init_instructions(fp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00002649
2650 /* Record the position in the program string for debugging */
2651 fp->StringPos = Program->Position;
2652
2653 /* OP_ALU_INST or OP_TEX_INST */
2654 instClass = *(*inst)++;
2655
2656 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2657 * OP_TEX_{SAMPLE, KIL}
2658 */
2659 type = *(*inst)++;
2660
2661 /* The actual opcode name */
2662 code = *(*inst)++;
2663
2664 /* Increment the correct count */
2665 switch (instClass) {
2666 case OP_ALU_INST:
2667 Program->NumAluInstructions++;
2668 break;
2669 case OP_TEX_INST:
2670 Program->NumTexInstructions++;
2671 break;
2672 }
2673
Jouk Jansen40322e12004-04-05 08:50:36 +00002674 switch (type) {
2675 case OP_ALU_VECTOR:
2676 switch (code) {
2677 case OP_ABS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002678 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002679 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00002680 fp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002681 break;
2682
2683 case OP_FLR_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002684 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002685 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00002686 fp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00002687 break;
2688
2689 case OP_FRC_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002690 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002691 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00002692 fp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00002693 break;
2694
2695 case OP_LIT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002696 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002697 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00002698 fp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002699 break;
2700
2701 case OP_MOV_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002702 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002703 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00002704 fp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00002705 break;
2706 }
2707
Keith Whitwell7c26b612005-04-21 14:46:57 +00002708 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002709 return 1;
2710
Keith Whitwell7c26b612005-04-21 14:46:57 +00002711 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002712 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002713 break;
2714
2715 case OP_ALU_SCALAR:
2716 switch (code) {
2717 case OP_COS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002718 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002719 case OP_COS:
Brian Paul7e807512005-11-05 17:10:45 +00002720 fp->Opcode = OPCODE_COS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002721 break;
2722
2723 case OP_EX2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002724 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002725 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00002726 fp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002727 break;
2728
2729 case OP_LG2_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002730 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002731 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00002732 fp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00002733 break;
2734
2735 case OP_RCP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002736 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002737 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00002738 fp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002739 break;
2740
2741 case OP_RSQ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002742 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002743 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00002744 fp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002745 break;
2746
2747 case OP_SIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002748 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002749 case OP_SIN:
Brian Paul7e807512005-11-05 17:10:45 +00002750 fp->Opcode = OPCODE_SIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002751 break;
2752
2753 case OP_SCS_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002754 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002755 case OP_SCS:
2756
Brian Paul7e807512005-11-05 17:10:45 +00002757 fp->Opcode = OPCODE_SCS;
Jouk Jansen40322e12004-04-05 08:50:36 +00002758 break;
2759 }
2760
Keith Whitwell7c26b612005-04-21 14:46:57 +00002761 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002762 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002763
2764 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002765 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002766 break;
2767
2768 case OP_ALU_BINSC:
2769 switch (code) {
2770 case OP_POW_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002771 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002772 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00002773 fp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00002774 break;
2775 }
2776
Keith Whitwell7c26b612005-04-21 14:46:57 +00002777 if (parse_fp_dst_reg(ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002778 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002779
Jouk Jansen40322e12004-04-05 08:50:36 +00002780 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002781 if (parse_fp_scalar_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002782 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002783 }
2784 break;
2785
2786
2787 case OP_ALU_BIN:
2788 switch (code) {
2789 case OP_ADD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002790 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002791 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00002792 fp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002793 break;
2794
2795 case OP_DP3_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002796 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002797 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00002798 fp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00002799 break;
2800
2801 case OP_DP4_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002802 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002803 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00002804 fp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00002805 break;
2806
2807 case OP_DPH_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002808 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002809 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00002810 fp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00002811 break;
2812
2813 case OP_DST_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002814 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002815 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00002816 fp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00002817 break;
2818
2819 case OP_MAX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002820 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002821 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00002822 fp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002823 break;
2824
2825 case OP_MIN_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002826 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002827 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00002828 fp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00002829 break;
2830
2831 case OP_MUL_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002832 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002833 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00002834 fp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002835 break;
2836
2837 case OP_SGE_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002838 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002839 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00002840 fp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002841 break;
2842
2843 case OP_SLT_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002844 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002845 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00002846 fp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00002847 break;
2848
2849 case OP_SUB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002850 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002851 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00002852 fp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002853 break;
2854
2855 case OP_XPD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002856 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002857 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00002858 fp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002859 break;
2860 }
2861
Keith Whitwell7c26b612005-04-21 14:46:57 +00002862 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002863 return 1;
2864 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002865 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2866 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002867 }
2868 break;
2869
2870 case OP_ALU_TRI:
2871 switch (code) {
2872 case OP_CMP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002873 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002874 case OP_CMP:
Brian Paul7e807512005-11-05 17:10:45 +00002875 fp->Opcode = OPCODE_CMP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002876 break;
2877
2878 case OP_LRP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002879 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002880 case OP_LRP:
Brian Paul7e807512005-11-05 17:10:45 +00002881 fp->Opcode = OPCODE_LRP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002882 break;
2883
2884 case OP_MAD_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002885 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002886 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00002887 fp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00002888 break;
2889 }
2890
Keith Whitwell7c26b612005-04-21 14:46:57 +00002891 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002892 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002893
Jouk Jansen40322e12004-04-05 08:50:36 +00002894 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00002895 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[a]))
2896 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002897 }
2898 break;
2899
2900 case OP_ALU_SWZ:
2901 switch (code) {
2902 case OP_SWZ_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002903 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002904 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00002905 fp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00002906 break;
2907 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00002908 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002909 return 1;
2910
Keith Whitwell7c26b612005-04-21 14:46:57 +00002911 {
Brian Paul32df89e2005-10-29 18:26:43 +00002912 GLubyte swizzle[4];
Brian Paul54cfe692005-10-21 15:22:36 +00002913 GLubyte negateMask;
Brian Paul7aebaf32005-10-30 21:23:23 +00002914 enum register_file file;
2915 GLint index;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002916
Brian Paul32df89e2005-10-29 18:26:43 +00002917 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &rel))
Keith Whitwell7c26b612005-04-21 14:46:57 +00002918 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00002919 parse_extended_swizzle_mask(inst, swizzle, &negateMask);
2920 fp->SrcReg[0].File = file;
2921 fp->SrcReg[0].Index = index;
Brian Paul54cfe692005-10-21 15:22:36 +00002922 fp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00002923 fp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
2924 swizzle[1],
2925 swizzle[2],
2926 swizzle[3]);
Keith Whitwell7c26b612005-04-21 14:46:57 +00002927 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002928 break;
2929
2930 case OP_TEX_SAMPLE:
2931 switch (code) {
2932 case OP_TEX_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002933 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002934 case OP_TEX:
Brian Paul7e807512005-11-05 17:10:45 +00002935 fp->Opcode = OPCODE_TEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002936 break;
2937
2938 case OP_TXP_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002939 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002940 case OP_TXP:
Brian Paul7e807512005-11-05 17:10:45 +00002941 fp->Opcode = OPCODE_TXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00002942 break;
2943
2944 case OP_TXB_SAT:
Brian Paule31ac052005-11-20 17:52:40 +00002945 fp->SaturateMode = SATURATE_ZERO_ONE;
Jouk Jansen40322e12004-04-05 08:50:36 +00002946 case OP_TXB:
Brian Paul7e807512005-11-05 17:10:45 +00002947 fp->Opcode = OPCODE_TXB;
Jouk Jansen40322e12004-04-05 08:50:36 +00002948 break;
2949 }
2950
Keith Whitwell7c26b612005-04-21 14:46:57 +00002951 if (parse_fp_dst_reg (ctx, inst, vc_head, Program, &fp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00002952 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002953
2954 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002955 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00002956
2957 /* texImageUnit */
2958 if (parse_texcoord_num (ctx, inst, Program, &texcoord))
2959 return 1;
2960 fp->TexSrcUnit = texcoord;
2961
2962 /* texTarget */
2963 switch (*(*inst)++) {
2964 case TEXTARGET_1D:
Brian Paul7e807512005-11-05 17:10:45 +00002965 fp->TexSrcTarget = TEXTURE_1D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002966 break;
2967 case TEXTARGET_2D:
Brian Paul7e807512005-11-05 17:10:45 +00002968 fp->TexSrcTarget = TEXTURE_2D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002969 break;
2970 case TEXTARGET_3D:
Brian Paul7e807512005-11-05 17:10:45 +00002971 fp->TexSrcTarget = TEXTURE_3D_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002972 break;
2973 case TEXTARGET_RECT:
Brian Paul7e807512005-11-05 17:10:45 +00002974 fp->TexSrcTarget = TEXTURE_RECT_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002975 break;
2976 case TEXTARGET_CUBE:
Brian Paul7e807512005-11-05 17:10:45 +00002977 fp->TexSrcTarget = TEXTURE_CUBE_INDEX;
Jouk Jansen40322e12004-04-05 08:50:36 +00002978 break;
2979 case TEXTARGET_SHADOW1D:
2980 case TEXTARGET_SHADOW2D:
2981 case TEXTARGET_SHADOWRECT:
2982 /* TODO ARB_fragment_program_shadow code */
2983 break;
2984 }
Brian Paulb7fc1c32006-08-30 23:38:03 +00002985 Program->TexturesUsed[texcoord] |= (1 << fp->TexSrcTarget);
2986 /* Check that both "2D" and "CUBE" (for example) aren't both used */
2987 if (_mesa_bitcount(Program->TexturesUsed[texcoord]) > 1) {
Brian Paula088f162006-09-05 23:08:51 +00002988 program_error(ctx, Program->Position,
2989 "multiple targets used on one texture image unit");
Brian Paulb7fc1c32006-08-30 23:38:03 +00002990 return 1;
2991 }
Jouk Jansen40322e12004-04-05 08:50:36 +00002992 break;
2993
2994 case OP_TEX_KIL:
Keith Whitwellc3626a92005-11-01 17:25:49 +00002995 Program->UsesKill = 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00002996 if (parse_fp_vector_src_reg(ctx, inst, vc_head, Program, &fp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00002997 return 1;
Brian Paul7e807512005-11-05 17:10:45 +00002998 fp->Opcode = OPCODE_KIL;
Jouk Jansen40322e12004-04-05 08:50:36 +00002999 break;
Brian Paulb7fc1c32006-08-30 23:38:03 +00003000 default:
3001 _mesa_problem(ctx, "bad type 0x%x in parse_fp_instruction()", type);
3002 return 1;
Jouk Jansen40322e12004-04-05 08:50:36 +00003003 }
3004
3005 return 0;
3006}
3007
Keith Whitwell7c26b612005-04-21 14:46:57 +00003008static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003009parse_vp_dst_reg(GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003010 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003011 struct prog_dst_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003012{
Brian Paul7aebaf32005-10-30 21:23:23 +00003013 GLint mask;
3014 GLuint idx;
3015 enum register_file file;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003016
3017 if (parse_masked_dst_reg(ctx, inst, vc_head, Program, &file, &idx, &mask))
3018 return 1;
3019
3020 reg->File = file;
3021 reg->Index = idx;
3022 reg->WriteMask = mask;
3023 return 0;
3024}
3025
3026/**
3027 * Handle the parsing out of a masked address register
3028 *
3029 * \param Index - The register index we write to
3030 * \param WriteMask - The mask controlling which components we write (1->write)
3031 *
3032 * \return 0 on sucess, 1 on error
3033 */
3034static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003035parse_vp_address_reg (GLcontext * ctx, const GLubyte ** inst,
Keith Whitwell7c26b612005-04-21 14:46:57 +00003036 struct var_cache **vc_head,
3037 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003038 struct prog_dst_register *reg)
Keith Whitwell7c26b612005-04-21 14:46:57 +00003039{
3040 GLint idx;
3041
3042 if (parse_address_reg (ctx, inst, vc_head, Program, &idx))
3043 return 1;
3044
3045 /* This should be 0x8 */
3046 (*inst)++;
3047
3048 reg->File = PROGRAM_ADDRESS;
3049 reg->Index = idx;
3050
3051 /* Writemask of .x is implied */
3052 reg->WriteMask = 0x1;
3053 return 0;
3054}
3055
3056/**
Brian Paul7aebaf32005-10-30 21:23:23 +00003057 * Parse vertex program vector source register.
Keith Whitwell7c26b612005-04-21 14:46:57 +00003058 */
3059static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003060parse_vp_vector_src_reg(GLcontext * ctx, const GLubyte ** inst,
Brian Paul32df89e2005-10-29 18:26:43 +00003061 struct var_cache **vc_head,
3062 struct arb_program *program,
Brian Paul7e807512005-11-05 17:10:45 +00003063 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003064{
Brian Paul7aebaf32005-10-30 21:23:23 +00003065 enum register_file file;
Brian Paul32df89e2005-10-29 18:26:43 +00003066 GLint index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003067 GLubyte negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003068 GLubyte swizzle[4];
3069 GLboolean isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003070
3071 /* Grab the sign */
Brian Paul7aebaf32005-10-30 21:23:23 +00003072 negateMask = (parse_sign (inst) == -1) ? 0xf : 0x0;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003073
3074 /* And the src reg */
Brian Paul32df89e2005-10-29 18:26:43 +00003075 if (parse_src_reg (ctx, inst, vc_head, program, &file, &index, &isRelOffset))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003076 return 1;
3077
3078 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003079 parse_swizzle_mask(inst, swizzle, 4);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003080
Brian Paul32df89e2005-10-29 18:26:43 +00003081 reg->File = file;
3082 reg->Index = index;
3083 reg->Swizzle = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
3084 swizzle[2], swizzle[3]);
Brian Paul7e807512005-11-05 17:10:45 +00003085 reg->NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003086 reg->RelAddr = isRelOffset;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003087 return 0;
3088}
3089
3090
3091static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003092parse_vp_scalar_src_reg (GLcontext * ctx, const GLubyte ** inst,
Brian Paul7aebaf32005-10-30 21:23:23 +00003093 struct var_cache **vc_head,
3094 struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003095 struct prog_src_register *reg )
Keith Whitwell7c26b612005-04-21 14:46:57 +00003096{
Brian Paul7aebaf32005-10-30 21:23:23 +00003097 enum register_file File;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003098 GLint Index;
Brian Paul7aebaf32005-10-30 21:23:23 +00003099 GLubyte Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003100 GLubyte Swizzle[4];
3101 GLboolean IsRelOffset;
3102
3103 /* Grab the sign */
3104 Negate = (parse_sign (inst) == -1) ? 0x1 : 0x0;
3105
3106 /* And the src reg */
3107 if (parse_src_reg (ctx, inst, vc_head, Program, &File, &Index, &IsRelOffset))
3108 return 1;
3109
3110 /* finally, the swizzle */
Brian Paul32df89e2005-10-29 18:26:43 +00003111 parse_swizzle_mask(inst, Swizzle, 1);
Keith Whitwell7c26b612005-04-21 14:46:57 +00003112
3113 reg->File = File;
3114 reg->Index = Index;
3115 reg->Swizzle = (Swizzle[0] << 0);
Brian Paul7e807512005-11-05 17:10:45 +00003116 reg->NegateBase = Negate;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003117 reg->RelAddr = IsRelOffset;
3118 return 0;
3119}
3120
3121
Jouk Jansen40322e12004-04-05 08:50:36 +00003122/**
3123 * This is a big mother that handles getting opcodes into the instruction
3124 * and handling the src & dst registers for vertex program instructions
3125 */
3126static GLuint
Brian Paula088f162006-09-05 23:08:51 +00003127parse_vp_instruction (GLcontext * ctx, const GLubyte ** inst,
Jouk Jansen40322e12004-04-05 08:50:36 +00003128 struct var_cache **vc_head, struct arb_program *Program,
Brian Paul7e807512005-11-05 17:10:45 +00003129 struct prog_instruction *vp)
Jouk Jansen40322e12004-04-05 08:50:36 +00003130{
3131 GLint a;
3132 GLubyte type, code;
3133
3134 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3135 type = *(*inst)++;
3136
3137 /* The actual opcode name */
3138 code = *(*inst)++;
3139
Brian Pauld6272e02006-10-29 18:03:16 +00003140 _mesa_init_instructions(vp, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003141 /* Record the position in the program string for debugging */
3142 vp->StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003143
3144 switch (type) {
3145 /* XXX: */
3146 case OP_ALU_ARL:
Brian Paul7e807512005-11-05 17:10:45 +00003147 vp->Opcode = OPCODE_ARL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003148
3149 /* Remember to set SrcReg.RelAddr; */
3150
3151 /* Get the masked address register [dst] */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003152 if (parse_vp_address_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003153 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003154
Jouk Jansen40322e12004-04-05 08:50:36 +00003155 vp->DstReg.File = PROGRAM_ADDRESS;
3156
3157 /* Get a scalar src register */
Keith Whitwell7c26b612005-04-21 14:46:57 +00003158 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003159 return 1;
3160
3161 break;
3162
3163 case OP_ALU_VECTOR:
3164 switch (code) {
3165 case OP_ABS:
Brian Paul7e807512005-11-05 17:10:45 +00003166 vp->Opcode = OPCODE_ABS;
Jouk Jansen40322e12004-04-05 08:50:36 +00003167 break;
3168 case OP_FLR:
Brian Paul7e807512005-11-05 17:10:45 +00003169 vp->Opcode = OPCODE_FLR;
Jouk Jansen40322e12004-04-05 08:50:36 +00003170 break;
3171 case OP_FRC:
Brian Paul7e807512005-11-05 17:10:45 +00003172 vp->Opcode = OPCODE_FRC;
Jouk Jansen40322e12004-04-05 08:50:36 +00003173 break;
3174 case OP_LIT:
Brian Paul7e807512005-11-05 17:10:45 +00003175 vp->Opcode = OPCODE_LIT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003176 break;
3177 case OP_MOV:
Brian Paul7e807512005-11-05 17:10:45 +00003178 vp->Opcode = OPCODE_MOV;
Jouk Jansen40322e12004-04-05 08:50:36 +00003179 break;
3180 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003181
3182 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003183 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003184
3185 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003186 return 1;
3187 break;
3188
3189 case OP_ALU_SCALAR:
3190 switch (code) {
3191 case OP_EX2:
Brian Paul7e807512005-11-05 17:10:45 +00003192 vp->Opcode = OPCODE_EX2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003193 break;
3194 case OP_EXP:
Brian Paul7e807512005-11-05 17:10:45 +00003195 vp->Opcode = OPCODE_EXP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003196 break;
3197 case OP_LG2:
Brian Paul7e807512005-11-05 17:10:45 +00003198 vp->Opcode = OPCODE_LG2;
Jouk Jansen40322e12004-04-05 08:50:36 +00003199 break;
3200 case OP_LOG:
Brian Paul7e807512005-11-05 17:10:45 +00003201 vp->Opcode = OPCODE_LOG;
Jouk Jansen40322e12004-04-05 08:50:36 +00003202 break;
3203 case OP_RCP:
Brian Paul7e807512005-11-05 17:10:45 +00003204 vp->Opcode = OPCODE_RCP;
Jouk Jansen40322e12004-04-05 08:50:36 +00003205 break;
3206 case OP_RSQ:
Brian Paul7e807512005-11-05 17:10:45 +00003207 vp->Opcode = OPCODE_RSQ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003208 break;
3209 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003210 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003211 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003212
3213 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[0]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003214 return 1;
3215 break;
3216
3217 case OP_ALU_BINSC:
3218 switch (code) {
3219 case OP_POW:
Brian Paul7e807512005-11-05 17:10:45 +00003220 vp->Opcode = OPCODE_POW;
Jouk Jansen40322e12004-04-05 08:50:36 +00003221 break;
3222 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003223 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003224 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003225
Jouk Jansen40322e12004-04-05 08:50:36 +00003226 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003227 if (parse_vp_scalar_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003228 return 1;
3229 }
3230 break;
3231
3232 case OP_ALU_BIN:
3233 switch (code) {
3234 case OP_ADD:
Brian Paul7e807512005-11-05 17:10:45 +00003235 vp->Opcode = OPCODE_ADD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003236 break;
3237 case OP_DP3:
Brian Paul7e807512005-11-05 17:10:45 +00003238 vp->Opcode = OPCODE_DP3;
Jouk Jansen40322e12004-04-05 08:50:36 +00003239 break;
3240 case OP_DP4:
Brian Paul7e807512005-11-05 17:10:45 +00003241 vp->Opcode = OPCODE_DP4;
Jouk Jansen40322e12004-04-05 08:50:36 +00003242 break;
3243 case OP_DPH:
Brian Paul7e807512005-11-05 17:10:45 +00003244 vp->Opcode = OPCODE_DPH;
Jouk Jansen40322e12004-04-05 08:50:36 +00003245 break;
3246 case OP_DST:
Brian Paul7e807512005-11-05 17:10:45 +00003247 vp->Opcode = OPCODE_DST;
Jouk Jansen40322e12004-04-05 08:50:36 +00003248 break;
3249 case OP_MAX:
Brian Paul7e807512005-11-05 17:10:45 +00003250 vp->Opcode = OPCODE_MAX;
Jouk Jansen40322e12004-04-05 08:50:36 +00003251 break;
3252 case OP_MIN:
Brian Paul7e807512005-11-05 17:10:45 +00003253 vp->Opcode = OPCODE_MIN;
Jouk Jansen40322e12004-04-05 08:50:36 +00003254 break;
3255 case OP_MUL:
Brian Paul7e807512005-11-05 17:10:45 +00003256 vp->Opcode = OPCODE_MUL;
Jouk Jansen40322e12004-04-05 08:50:36 +00003257 break;
3258 case OP_SGE:
Brian Paul7e807512005-11-05 17:10:45 +00003259 vp->Opcode = OPCODE_SGE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003260 break;
3261 case OP_SLT:
Brian Paul7e807512005-11-05 17:10:45 +00003262 vp->Opcode = OPCODE_SLT;
Jouk Jansen40322e12004-04-05 08:50:36 +00003263 break;
3264 case OP_SUB:
Brian Paul7e807512005-11-05 17:10:45 +00003265 vp->Opcode = OPCODE_SUB;
Jouk Jansen40322e12004-04-05 08:50:36 +00003266 break;
3267 case OP_XPD:
Brian Paul7e807512005-11-05 17:10:45 +00003268 vp->Opcode = OPCODE_XPD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003269 break;
3270 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003271 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003272 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003273
Jouk Jansen40322e12004-04-05 08:50:36 +00003274 for (a = 0; a < 2; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003275 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003276 return 1;
3277 }
3278 break;
3279
3280 case OP_ALU_TRI:
3281 switch (code) {
3282 case OP_MAD:
Brian Paul7e807512005-11-05 17:10:45 +00003283 vp->Opcode = OPCODE_MAD;
Jouk Jansen40322e12004-04-05 08:50:36 +00003284 break;
3285 }
3286
Keith Whitwell7c26b612005-04-21 14:46:57 +00003287 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
Jouk Jansen40322e12004-04-05 08:50:36 +00003288 return 1;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003289
Jouk Jansen40322e12004-04-05 08:50:36 +00003290 for (a = 0; a < 3; a++) {
Keith Whitwell7c26b612005-04-21 14:46:57 +00003291 if (parse_vp_vector_src_reg(ctx, inst, vc_head, Program, &vp->SrcReg[a]))
Jouk Jansen40322e12004-04-05 08:50:36 +00003292 return 1;
3293 }
3294 break;
3295
3296 case OP_ALU_SWZ:
3297 switch (code) {
3298 case OP_SWZ:
Brian Paul7e807512005-11-05 17:10:45 +00003299 vp->Opcode = OPCODE_SWZ;
Jouk Jansen40322e12004-04-05 08:50:36 +00003300 break;
3301 }
Keith Whitwell7c26b612005-04-21 14:46:57 +00003302 {
Brian Paul32df89e2005-10-29 18:26:43 +00003303 GLubyte swizzle[4];
3304 GLubyte negateMask;
3305 GLboolean relAddr;
Brian Paul7aebaf32005-10-30 21:23:23 +00003306 enum register_file file;
3307 GLint index;
Jouk Jansen40322e12004-04-05 08:50:36 +00003308
Keith Whitwell7c26b612005-04-21 14:46:57 +00003309 if (parse_vp_dst_reg(ctx, inst, vc_head, Program, &vp->DstReg))
3310 return 1;
3311
Brian Paul32df89e2005-10-29 18:26:43 +00003312 if (parse_src_reg(ctx, inst, vc_head, Program, &file, &index, &relAddr))
Keith Whitwell7c26b612005-04-21 14:46:57 +00003313 return 1;
Brian Paul32df89e2005-10-29 18:26:43 +00003314 parse_extended_swizzle_mask (inst, swizzle, &negateMask);
3315 vp->SrcReg[0].File = file;
3316 vp->SrcReg[0].Index = index;
Brian Paul7e807512005-11-05 17:10:45 +00003317 vp->SrcReg[0].NegateBase = negateMask;
Brian Paul32df89e2005-10-29 18:26:43 +00003318 vp->SrcReg[0].Swizzle = MAKE_SWIZZLE4(swizzle[0],
3319 swizzle[1],
3320 swizzle[2],
3321 swizzle[3]);
3322 vp->SrcReg[0].RelAddr = relAddr;
Keith Whitwell7c26b612005-04-21 14:46:57 +00003323 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003324 break;
3325 }
3326 return 0;
3327}
3328
3329#if DEBUG_PARSING
3330
3331static GLvoid
3332print_state_token (GLint token)
3333{
3334 switch (token) {
3335 case STATE_MATERIAL:
3336 fprintf (stderr, "STATE_MATERIAL ");
3337 break;
3338 case STATE_LIGHT:
3339 fprintf (stderr, "STATE_LIGHT ");
3340 break;
3341
3342 case STATE_LIGHTMODEL_AMBIENT:
3343 fprintf (stderr, "STATE_AMBIENT ");
3344 break;
3345
3346 case STATE_LIGHTMODEL_SCENECOLOR:
3347 fprintf (stderr, "STATE_SCENECOLOR ");
3348 break;
3349
3350 case STATE_LIGHTPROD:
3351 fprintf (stderr, "STATE_LIGHTPROD ");
3352 break;
3353
3354 case STATE_TEXGEN:
3355 fprintf (stderr, "STATE_TEXGEN ");
3356 break;
3357
3358 case STATE_FOG_COLOR:
3359 fprintf (stderr, "STATE_FOG_COLOR ");
3360 break;
3361
3362 case STATE_FOG_PARAMS:
3363 fprintf (stderr, "STATE_FOG_PARAMS ");
3364 break;
3365
3366 case STATE_CLIPPLANE:
3367 fprintf (stderr, "STATE_CLIPPLANE ");
3368 break;
3369
3370 case STATE_POINT_SIZE:
3371 fprintf (stderr, "STATE_POINT_SIZE ");
3372 break;
3373
3374 case STATE_POINT_ATTENUATION:
3375 fprintf (stderr, "STATE_ATTENUATION ");
3376 break;
3377
3378 case STATE_MATRIX:
3379 fprintf (stderr, "STATE_MATRIX ");
3380 break;
3381
3382 case STATE_MODELVIEW:
3383 fprintf (stderr, "STATE_MODELVIEW ");
3384 break;
3385
3386 case STATE_PROJECTION:
3387 fprintf (stderr, "STATE_PROJECTION ");
3388 break;
3389
3390 case STATE_MVP:
3391 fprintf (stderr, "STATE_MVP ");
3392 break;
3393
3394 case STATE_TEXTURE:
3395 fprintf (stderr, "STATE_TEXTURE ");
3396 break;
3397
3398 case STATE_PROGRAM:
3399 fprintf (stderr, "STATE_PROGRAM ");
3400 break;
3401
3402 case STATE_MATRIX_INVERSE:
3403 fprintf (stderr, "STATE_INVERSE ");
3404 break;
3405
3406 case STATE_MATRIX_TRANSPOSE:
3407 fprintf (stderr, "STATE_TRANSPOSE ");
3408 break;
3409
3410 case STATE_MATRIX_INVTRANS:
3411 fprintf (stderr, "STATE_INVTRANS ");
3412 break;
3413
3414 case STATE_AMBIENT:
3415 fprintf (stderr, "STATE_AMBIENT ");
3416 break;
3417
3418 case STATE_DIFFUSE:
3419 fprintf (stderr, "STATE_DIFFUSE ");
3420 break;
3421
3422 case STATE_SPECULAR:
3423 fprintf (stderr, "STATE_SPECULAR ");
3424 break;
3425
3426 case STATE_EMISSION:
3427 fprintf (stderr, "STATE_EMISSION ");
3428 break;
3429
3430 case STATE_SHININESS:
3431 fprintf (stderr, "STATE_SHININESS ");
3432 break;
3433
3434 case STATE_HALF:
3435 fprintf (stderr, "STATE_HALF ");
3436 break;
3437
3438 case STATE_POSITION:
3439 fprintf (stderr, "STATE_POSITION ");
3440 break;
3441
3442 case STATE_ATTENUATION:
3443 fprintf (stderr, "STATE_ATTENUATION ");
3444 break;
3445
3446 case STATE_SPOT_DIRECTION:
3447 fprintf (stderr, "STATE_DIRECTION ");
3448 break;
3449
3450 case STATE_TEXGEN_EYE_S:
3451 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3452 break;
3453
3454 case STATE_TEXGEN_EYE_T:
3455 fprintf (stderr, "STATE_TEXGEN_EYE_T ");
3456 break;
3457
3458 case STATE_TEXGEN_EYE_R:
3459 fprintf (stderr, "STATE_TEXGEN_EYE_R ");
3460 break;
3461
3462 case STATE_TEXGEN_EYE_Q:
3463 fprintf (stderr, "STATE_TEXGEN_EYE_Q ");
3464 break;
3465
3466 case STATE_TEXGEN_OBJECT_S:
3467 fprintf (stderr, "STATE_TEXGEN_EYE_S ");
3468 break;
3469
3470 case STATE_TEXGEN_OBJECT_T:
3471 fprintf (stderr, "STATE_TEXGEN_OBJECT_T ");
3472 break;
3473
3474 case STATE_TEXGEN_OBJECT_R:
3475 fprintf (stderr, "STATE_TEXGEN_OBJECT_R ");
3476 break;
3477
3478 case STATE_TEXGEN_OBJECT_Q:
3479 fprintf (stderr, "STATE_TEXGEN_OBJECT_Q ");
3480 break;
3481
3482 case STATE_TEXENV_COLOR:
3483 fprintf (stderr, "STATE_TEXENV_COLOR ");
3484 break;
3485
3486 case STATE_DEPTH_RANGE:
3487 fprintf (stderr, "STATE_DEPTH_RANGE ");
3488 break;
3489
3490 case STATE_VERTEX_PROGRAM:
3491 fprintf (stderr, "STATE_VERTEX_PROGRAM ");
3492 break;
3493
3494 case STATE_FRAGMENT_PROGRAM:
3495 fprintf (stderr, "STATE_FRAGMENT_PROGRAM ");
3496 break;
3497
3498 case STATE_ENV:
3499 fprintf (stderr, "STATE_ENV ");
3500 break;
3501
3502 case STATE_LOCAL:
3503 fprintf (stderr, "STATE_LOCAL ");
3504 break;
3505
3506 }
3507 fprintf (stderr, "[%d] ", token);
3508}
3509
3510
3511static GLvoid
3512debug_variables (GLcontext * ctx, struct var_cache *vc_head,
3513 struct arb_program *Program)
3514{
3515 struct var_cache *vc;
3516 GLint a, b;
3517
3518 fprintf (stderr, "debug_variables, vc_head: %x\n", vc_head);
3519
3520 /* First of all, print out the contents of the var_cache */
3521 vc = vc_head;
3522 while (vc) {
3523 fprintf (stderr, "[%x]\n", vc);
3524 switch (vc->type) {
3525 case vt_none:
3526 fprintf (stderr, "UNDEFINED %s\n", vc->name);
3527 break;
3528 case vt_attrib:
3529 fprintf (stderr, "ATTRIB %s\n", vc->name);
3530 fprintf (stderr, " binding: 0x%x\n", vc->attrib_binding);
3531 break;
3532 case vt_param:
3533 fprintf (stderr, "PARAM %s begin: %d len: %d\n", vc->name,
3534 vc->param_binding_begin, vc->param_binding_length);
3535 b = vc->param_binding_begin;
3536 for (a = 0; a < vc->param_binding_length; a++) {
3537 fprintf (stderr, "%s\n",
3538 Program->Parameters->Parameters[a + b].Name);
3539 if (Program->Parameters->Parameters[a + b].Type == STATE) {
3540 print_state_token (Program->Parameters->Parameters[a + b].
3541 StateIndexes[0]);
3542 print_state_token (Program->Parameters->Parameters[a + b].
3543 StateIndexes[1]);
3544 print_state_token (Program->Parameters->Parameters[a + b].
3545 StateIndexes[2]);
3546 print_state_token (Program->Parameters->Parameters[a + b].
3547 StateIndexes[3]);
3548 print_state_token (Program->Parameters->Parameters[a + b].
3549 StateIndexes[4]);
3550 print_state_token (Program->Parameters->Parameters[a + b].
3551 StateIndexes[5]);
3552 }
3553 else
3554 fprintf (stderr, "%f %f %f %f\n",
3555 Program->Parameters->Parameters[a + b].Values[0],
3556 Program->Parameters->Parameters[a + b].Values[1],
3557 Program->Parameters->Parameters[a + b].Values[2],
3558 Program->Parameters->Parameters[a + b].Values[3]);
3559 }
3560 break;
3561 case vt_temp:
3562 fprintf (stderr, "TEMP %s\n", vc->name);
3563 fprintf (stderr, " binding: 0x%x\n", vc->temp_binding);
3564 break;
3565 case vt_output:
3566 fprintf (stderr, "OUTPUT %s\n", vc->name);
3567 fprintf (stderr, " binding: 0x%x\n", vc->output_binding);
3568 break;
3569 case vt_alias:
3570 fprintf (stderr, "ALIAS %s\n", vc->name);
3571 fprintf (stderr, " binding: 0x%x (%s)\n",
3572 vc->alias_binding, vc->alias_binding->name);
3573 break;
3574 }
3575 vc = vc->next;
3576 }
3577}
3578
Brian Paul72030e02005-11-03 03:30:34 +00003579#endif /* DEBUG_PARSING */
Brian Paul7aebaf32005-10-30 21:23:23 +00003580
3581
3582/**
Jouk Jansen40322e12004-04-05 08:50:36 +00003583 * The main loop for parsing a fragment or vertex program
3584 *
Brian Paul72030e02005-11-03 03:30:34 +00003585 * \return 1 on error, 0 on success
Jouk Jansen40322e12004-04-05 08:50:36 +00003586 */
Brian Paul72030e02005-11-03 03:30:34 +00003587static GLint
Brian Paula088f162006-09-05 23:08:51 +00003588parse_instructions(GLcontext * ctx, const GLubyte * inst,
3589 struct var_cache **vc_head, struct arb_program *Program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003590{
Brian Paul72030e02005-11-03 03:30:34 +00003591 const GLuint maxInst = (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB)
3592 ? ctx->Const.FragmentProgram.MaxInstructions
3593 : ctx->Const.VertexProgram.MaxInstructions;
Jouk Jansen40322e12004-04-05 08:50:36 +00003594 GLint err = 0;
3595
Brian Paul72030e02005-11-03 03:30:34 +00003596 ASSERT(MAX_INSTRUCTIONS >= maxInst);
3597
Jouk Jansen40322e12004-04-05 08:50:36 +00003598 Program->MajorVersion = (GLuint) * inst++;
3599 Program->MinorVersion = (GLuint) * inst++;
3600
3601 while (*inst != END) {
3602 switch (*inst++) {
3603
3604 case OPTION:
3605 switch (*inst++) {
3606 case ARB_PRECISION_HINT_FASTEST:
3607 Program->PrecisionOption = GL_FASTEST;
3608 break;
3609
3610 case ARB_PRECISION_HINT_NICEST:
3611 Program->PrecisionOption = GL_NICEST;
3612 break;
3613
3614 case ARB_FOG_EXP:
3615 Program->FogOption = GL_EXP;
3616 break;
3617
3618 case ARB_FOG_EXP2:
3619 Program->FogOption = GL_EXP2;
3620 break;
3621
3622 case ARB_FOG_LINEAR:
3623 Program->FogOption = GL_LINEAR;
3624 break;
3625
3626 case ARB_POSITION_INVARIANT:
3627 if (Program->Base.Target == GL_VERTEX_PROGRAM_ARB)
Brian Paul45cd2f92005-11-03 02:25:10 +00003628 Program->HintPositionInvariant = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003629 break;
3630
3631 case ARB_FRAGMENT_PROGRAM_SHADOW:
3632 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3633 /* TODO ARB_fragment_program_shadow code */
3634 }
3635 break;
Michal Krolad22ce82004-10-11 08:13:25 +00003636
3637 case ARB_DRAW_BUFFERS:
3638 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
3639 /* do nothing for now */
3640 }
3641 break;
Jouk Jansen40322e12004-04-05 08:50:36 +00003642 }
3643 break;
3644
3645 case INSTRUCTION:
Brian Paul72030e02005-11-03 03:30:34 +00003646 /* check length */
3647 if (Program->Base.NumInstructions + 1 >= maxInst) {
Brian Paula088f162006-09-05 23:08:51 +00003648 program_error(ctx, Program->Position,
3649 "Max instruction count exceeded");
Brian Paul72030e02005-11-03 03:30:34 +00003650 return 1;
3651 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003652 Program->Position = parse_position (&inst);
Brian Paul72030e02005-11-03 03:30:34 +00003653 /* parse the current instruction */
Jouk Jansen40322e12004-04-05 08:50:36 +00003654 if (Program->Base.Target == GL_FRAGMENT_PROGRAM_ARB) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003655 err = parse_fp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003656 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003657 }
3658 else {
Jouk Jansen40322e12004-04-05 08:50:36 +00003659 err = parse_vp_instruction (ctx, &inst, vc_head, Program,
Brian Paul8c41a142005-11-19 15:36:28 +00003660 &Program->Base.Instructions[Program->Base.NumInstructions]);
Jouk Jansen40322e12004-04-05 08:50:36 +00003661 }
3662
Brian Paul72030e02005-11-03 03:30:34 +00003663 /* increment instuction count */
Jouk Jansen40322e12004-04-05 08:50:36 +00003664 Program->Base.NumInstructions++;
3665 break;
3666
3667 case DECLARATION:
3668 err = parse_declaration (ctx, &inst, vc_head, Program);
3669 break;
3670
3671 default:
3672 break;
3673 }
3674
3675 if (err)
3676 break;
3677 }
3678
3679 /* Finally, tag on an OPCODE_END instruction */
Brian Paul8c41a142005-11-19 15:36:28 +00003680 {
Brian Paul7aebaf32005-10-30 21:23:23 +00003681 const GLuint numInst = Program->Base.NumInstructions;
Brian Pauld6272e02006-10-29 18:03:16 +00003682 _mesa_init_instructions(Program->Base.Instructions + numInst, 1);
Brian Paul8c41a142005-11-19 15:36:28 +00003683 Program->Base.Instructions[numInst].Opcode = OPCODE_END;
Jouk Jansen40322e12004-04-05 08:50:36 +00003684 /* YYY Wrong Position in program, whatever, at least not random -> crash
3685 Program->Position = parse_position (&inst);
3686 */
Brian Paul8c41a142005-11-19 15:36:28 +00003687 Program->Base.Instructions[numInst].StringPos = Program->Position;
Jouk Jansen40322e12004-04-05 08:50:36 +00003688 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003689 Program->Base.NumInstructions++;
3690
Brian Paul05051032005-11-01 04:36:33 +00003691 /*
3692 * Initialize native counts to logical counts. The device driver may
3693 * change them if program is translated into a hardware program.
3694 */
3695 Program->Base.NumNativeInstructions = Program->Base.NumInstructions;
3696 Program->Base.NumNativeTemporaries = Program->Base.NumTemporaries;
3697 Program->Base.NumNativeParameters = Program->Base.NumParameters;
3698 Program->Base.NumNativeAttributes = Program->Base.NumAttributes;
3699 Program->Base.NumNativeAddressRegs = Program->Base.NumAddressRegs;
Brian Paul05051032005-11-01 04:36:33 +00003700
Jouk Jansen40322e12004-04-05 08:50:36 +00003701 return err;
3702}
3703
Brian Paul7aebaf32005-10-30 21:23:23 +00003704
Jouk Jansen40322e12004-04-05 08:50:36 +00003705/* XXX temporary */
Brian5cc12922006-12-14 14:27:05 -07003706LONGSTRING static char core_grammar_text[] =
Jouk Jansen40322e12004-04-05 08:50:36 +00003707#include "grammar_syn.h"
3708;
3709
Brian Paul7aebaf32005-10-30 21:23:23 +00003710
Brian Paul8c41a142005-11-19 15:36:28 +00003711/**
3712 * Set a grammar parameter.
3713 * \param name the grammar parameter
3714 * \param value the new parameter value
3715 * \return 0 if OK, 1 if error
3716 */
3717static int
3718set_reg8 (GLcontext *ctx, grammar id, const char *name, GLubyte value)
Jouk Jansen40322e12004-04-05 08:50:36 +00003719{
3720 char error_msg[300];
3721 GLint error_pos;
3722
Brian Paul8c41a142005-11-19 15:36:28 +00003723 if (grammar_set_reg8 (id, (const byte *) name, value))
Jouk Jansen40322e12004-04-05 08:50:36 +00003724 return 0;
3725
3726 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3727 _mesa_set_program_error (ctx, error_pos, error_msg);
3728 _mesa_error (ctx, GL_INVALID_OPERATION, "Grammar Register Error");
3729 return 1;
3730}
3731
Brian Paul8c41a142005-11-19 15:36:28 +00003732
3733/**
3734 * Enable support for the given language option in the parser.
3735 * \return 1 if OK, 0 if error
3736 */
3737static int
3738enable_ext(GLcontext *ctx, grammar id, const char *name)
Jouk Jansen40322e12004-04-05 08:50:36 +00003739{
Brian Paul8c41a142005-11-19 15:36:28 +00003740 return !set_reg8(ctx, id, name, 1);
Jouk Jansen40322e12004-04-05 08:50:36 +00003741}
3742
Brian Paul8c41a142005-11-19 15:36:28 +00003743
3744/**
3745 * Enable parser extensions based on which OpenGL extensions are supported
3746 * by this rendering context.
3747 *
3748 * \return GL_TRUE if OK, GL_FALSE if error.
3749 */
3750static GLboolean
3751enable_parser_extensions(GLcontext *ctx, grammar id)
Jouk Jansen40322e12004-04-05 08:50:36 +00003752{
Brian Paul8c41a142005-11-19 15:36:28 +00003753#if 0
3754 /* These are not supported at this time */
3755 if ((ctx->Extensions.ARB_vertex_blend ||
3756 ctx->Extensions.EXT_vertex_weighting)
Brian Paul43cc1dc2006-09-05 23:11:09 +00003757 && !enable_ext(ctx, id, "vertex_blend"))
Brian Paul8c41a142005-11-19 15:36:28 +00003758 return GL_FALSE;
3759 if (ctx->Extensions.ARB_matrix_palette
3760 && !enable_ext(ctx, id, "matrix_palette"))
3761 return GL_FALSE;
3762 if (ctx->Extensions.ARB_fragment_program_shadow
3763 && !enable_ext(ctx, id, "fragment_program_shadow"))
3764 return GL_FALSE;
3765#endif
3766 if (ctx->Extensions.EXT_point_parameters
3767 && !enable_ext(ctx, id, "point_parameters"))
3768 return GL_FALSE;
3769 if (ctx->Extensions.EXT_secondary_color
3770 && !enable_ext(ctx, id, "secondary_color"))
3771 return GL_FALSE;
3772 if (ctx->Extensions.EXT_fog_coord
3773 && !enable_ext(ctx, id, "fog_coord"))
3774 return GL_FALSE;
3775 if (ctx->Extensions.NV_texture_rectangle
3776 && !enable_ext(ctx, id, "texture_rectangle"))
3777 return GL_FALSE;
3778 if (ctx->Extensions.ARB_draw_buffers
3779 && !enable_ext(ctx, id, "draw_buffers"))
3780 return GL_FALSE;
3781
Brian Paul3a557502006-09-05 23:15:29 +00003782#if 1
3783 /* hack for Warcraft (see bug 8060) */
3784 enable_ext(ctx, id, "vertex_blend");
3785#endif
3786
Brian Paul8c41a142005-11-19 15:36:28 +00003787 return GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003788}
3789
Brian Paul8c41a142005-11-19 15:36:28 +00003790
Jouk Jansen40322e12004-04-05 08:50:36 +00003791/**
3792 * This kicks everything off.
3793 *
3794 * \param ctx - The GL Context
3795 * \param str - The program string
3796 * \param len - The program string length
Brian Paul45703642005-10-29 15:52:31 +00003797 * \param program - The arb_program struct to return all the parsed info in
3798 * \return GL_TRUE on sucess, GL_FALSE on error
Jouk Jansen40322e12004-04-05 08:50:36 +00003799 */
Brian Paul8c41a142005-11-19 15:36:28 +00003800static GLboolean
3801_mesa_parse_arb_program(GLcontext *ctx, GLenum target,
3802 const GLubyte *str, GLsizei len,
3803 struct arb_program *program)
Jouk Jansen40322e12004-04-05 08:50:36 +00003804{
3805 GLint a, err, error_pos;
3806 char error_msg[300];
3807 GLuint parsed_len;
3808 struct var_cache *vc_head;
3809 grammar arbprogram_syn_id;
3810 GLubyte *parsed, *inst;
3811 GLubyte *strz = NULL;
3812 static int arbprogram_syn_is_ok = 0; /* XXX temporary */
3813
Brian Paul8c41a142005-11-19 15:36:28 +00003814 /* set the program target before parsing */
3815 program->Base.Target = target;
3816
Brian Paul7f76b8f2004-09-10 01:05:39 +00003817 /* Reset error state */
3818 _mesa_set_program_error(ctx, -1, NULL);
3819
Brian Paul8c41a142005-11-19 15:36:28 +00003820 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
Jouk Jansen40322e12004-04-05 08:50:36 +00003821 if (!arbprogram_syn_is_ok) {
Brian Paul8c41a142005-11-19 15:36:28 +00003822 /* One-time initialization of parsing system */
Jouk Jansen40322e12004-04-05 08:50:36 +00003823 grammar grammar_syn_id;
Jouk Jansen40322e12004-04-05 08:50:36 +00003824 GLuint parsed_len;
Jouk Jansen40322e12004-04-05 08:50:36 +00003825
3826 grammar_syn_id = grammar_load_from_text ((byte *) core_grammar_text);
3827 if (grammar_syn_id == 0) {
3828 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
Brian Paul8c41a142005-11-19 15:36:28 +00003829 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003830 _mesa_set_program_error (ctx, error_pos, error_msg);
3831 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003832 "glProgramStringARB(Error loading grammar rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003833 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003834 }
3835
Brian Paul8c41a142005-11-19 15:36:28 +00003836 err = !grammar_check(grammar_syn_id, (byte *) arb_grammar_text,
3837 &parsed, &parsed_len);
Jouk Jansen40322e12004-04-05 08:50:36 +00003838
Brian Paul49a80ca2006-04-28 15:40:11 +00003839 /* 'parsed' is unused here */
3840 _mesa_free (parsed);
3841 parsed = NULL;
3842
Brian Paul45703642005-10-29 15:52:31 +00003843 /* NOTE: we can't destroy grammar_syn_id right here because
3844 * grammar_destroy() can reset the last error
3845 */
Brian Paul8c41a142005-11-19 15:36:28 +00003846 if (err) {
3847 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003848 grammar_get_last_error ((byte *) error_msg, 300, &error_pos);
3849 _mesa_set_program_error (ctx, error_pos, error_msg);
Brian Paul8c41a142005-11-19 15:36:28 +00003850 _mesa_error (ctx, GL_INVALID_OPERATION,
3851 "glProgramString(Error loading grammar rule set");
Jouk Jansen40322e12004-04-05 08:50:36 +00003852 grammar_destroy (grammar_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003853 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003854 }
3855
3856 grammar_destroy (grammar_syn_id);
3857
3858 arbprogram_syn_is_ok = 1;
3859 }
3860
3861 /* create the grammar object */
3862 arbprogram_syn_id = grammar_load_from_text ((byte *) arb_grammar_text);
3863 if (arbprogram_syn_id == 0) {
Brian Paul8c41a142005-11-19 15:36:28 +00003864 /* XXX this is not a GL error - it's an implementation bug! - FIX */
Jouk Jansen40322e12004-04-05 08:50:36 +00003865 grammar_get_last_error ((GLubyte *) error_msg, 300, &error_pos);
3866 _mesa_set_program_error (ctx, error_pos, error_msg);
3867 _mesa_error (ctx, GL_INVALID_OPERATION,
Brian Paul8c41a142005-11-19 15:36:28 +00003868 "glProgramString(Error loading grammer rule set)");
Brian Paul45703642005-10-29 15:52:31 +00003869 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003870 }
3871
3872 /* Set program_target register value */
Brian Paul55194df2005-11-19 23:29:18 +00003873 if (set_reg8 (ctx, arbprogram_syn_id, "program_target",
Jouk Jansen40322e12004-04-05 08:50:36 +00003874 program->Base.Target == GL_FRAGMENT_PROGRAM_ARB ? 0x10 : 0x20)) {
3875 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003876 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003877 }
3878
Brian Paul8c41a142005-11-19 15:36:28 +00003879 if (!enable_parser_extensions(ctx, arbprogram_syn_id)) {
3880 grammar_destroy(arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003881 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003882 }
3883
3884 /* check for NULL character occurences */
3885 {
Brian Paul8c41a142005-11-19 15:36:28 +00003886 GLint i;
3887 for (i = 0; i < len; i++) {
Jouk Jansen40322e12004-04-05 08:50:36 +00003888 if (str[i] == '\0') {
Brian Paula088f162006-09-05 23:08:51 +00003889 program_error(ctx, i, "illegal character");
Jouk Jansen40322e12004-04-05 08:50:36 +00003890 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003891 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003892 }
Brian Paul8c41a142005-11-19 15:36:28 +00003893 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003894 }
3895
3896 /* copy the program string to a null-terminated string */
Brian Paulbdd15b52004-05-04 15:11:06 +00003897 strz = (GLubyte *) _mesa_malloc (len + 1);
Brian Paul45703642005-10-29 15:52:31 +00003898 if (!strz) {
Brian Paul8c41a142005-11-19 15:36:28 +00003899 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
3900 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003901 return GL_FALSE;
3902 }
Jouk Jansen40322e12004-04-05 08:50:36 +00003903 _mesa_memcpy (strz, str, len);
3904 strz[len] = '\0';
3905
Michal Krolb80bc052004-10-21 14:09:54 +00003906 /* do a fast check on program string - initial production buffer is 4K */
Brian Paul8c41a142005-11-19 15:36:28 +00003907 err = !grammar_fast_check(arbprogram_syn_id, strz,
3908 &parsed, &parsed_len, 0x1000);
Jouk Jansen40322e12004-04-05 08:50:36 +00003909
3910 /* Syntax parse error */
Brian Paul8c41a142005-11-19 15:36:28 +00003911 if (err) {
Brian Paula088f162006-09-05 23:08:51 +00003912 grammar_get_last_error((GLubyte *) error_msg, 300, &error_pos);
3913 program_error(ctx, error_pos, error_msg);
Brian Paul5fe90292004-07-20 21:15:13 +00003914
Brian Paulb3aefd12005-09-19 20:12:32 +00003915#if DEBUG_PARSING
Brian Paula088f162006-09-05 23:08:51 +00003916 /* useful for debugging */
Brian Paulb3aefd12005-09-19 20:12:32 +00003917 do {
Brian Paul5fe90292004-07-20 21:15:13 +00003918 int line, col;
3919 char *s;
Brian Paul45703642005-10-29 15:52:31 +00003920 fprintf(stderr, "program: %s\n", (char *) strz);
3921 fprintf(stderr, "Error Pos: %d\n", ctx->program.ErrorPos);
Brian Paula088f162006-09-05 23:08:51 +00003922 s = (char *) _mesa_find_line_column(strz, strz+ctx->program.ErrorPos,
3923 &line, &col);
Brian Paulb3aefd12005-09-19 20:12:32 +00003924 fprintf(stderr, "line %d col %d: %s\n", line, col, s);
3925 } while (0)
3926#endif
Brian Paul5fe90292004-07-20 21:15:13 +00003927
Brian Paula088f162006-09-05 23:08:51 +00003928 _mesa_free(strz);
3929 _mesa_free(parsed);
3930
Jouk Jansen40322e12004-04-05 08:50:36 +00003931 grammar_destroy (arbprogram_syn_id);
Brian Paul45703642005-10-29 15:52:31 +00003932 return GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003933 }
3934
Jouk Jansen40322e12004-04-05 08:50:36 +00003935 grammar_destroy (arbprogram_syn_id);
3936
Brian Paul8c41a142005-11-19 15:36:28 +00003937 /*
3938 * Program string is syntactically correct at this point
3939 * Parse the tokenized version of the program now, generating
3940 * vertex/fragment program instructions.
3941 */
3942
Jouk Jansen40322e12004-04-05 08:50:36 +00003943 /* Initialize the arb_program struct */
3944 program->Base.String = strz;
Brian Paul383c39e2006-08-25 15:14:25 +00003945 program->Base.Instructions = _mesa_alloc_instructions(MAX_INSTRUCTIONS);
Jouk Jansen40322e12004-04-05 08:50:36 +00003946 program->Base.NumInstructions =
3947 program->Base.NumTemporaries =
3948 program->Base.NumParameters =
3949 program->Base.NumAttributes = program->Base.NumAddressRegs = 0;
Brian Paulde997602005-11-12 17:53:14 +00003950 program->Base.Parameters = _mesa_new_parameter_list ();
3951 program->Base.InputsRead = 0x0;
3952 program->Base.OutputsWritten = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003953 program->Position = 0;
3954 program->MajorVersion = program->MinorVersion = 0;
3955 program->PrecisionOption = GL_DONT_CARE;
3956 program->FogOption = GL_NONE;
3957 program->HintPositionInvariant = GL_FALSE;
3958 for (a = 0; a < MAX_TEXTURE_IMAGE_UNITS; a++)
Brian Paul45cd2f92005-11-03 02:25:10 +00003959 program->TexturesUsed[a] = 0x0;
Jouk Jansen40322e12004-04-05 08:50:36 +00003960 program->NumAluInstructions =
3961 program->NumTexInstructions =
3962 program->NumTexIndirections = 0;
Keith Whitwellc3626a92005-11-01 17:25:49 +00003963 program->UsesKill = 0;
3964
Jouk Jansen40322e12004-04-05 08:50:36 +00003965 vc_head = NULL;
Brian Paul45703642005-10-29 15:52:31 +00003966 err = GL_FALSE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003967
3968 /* Start examining the tokens in the array */
3969 inst = parsed;
3970
3971 /* Check the grammer rev */
3972 if (*inst++ != REVISION) {
Brian Paula088f162006-09-05 23:08:51 +00003973 program_error (ctx, 0, "Grammar version mismatch");
Brian Paul45703642005-10-29 15:52:31 +00003974 err = GL_TRUE;
Jouk Jansen40322e12004-04-05 08:50:36 +00003975 }
3976 else {
Michal Krolb80bc052004-10-21 14:09:54 +00003977 /* ignore program target */
3978 inst++;
Brian Paul8c41a142005-11-19 15:36:28 +00003979 err = parse_instructions(ctx, inst, &vc_head, program);
Jouk Jansen40322e12004-04-05 08:50:36 +00003980 }
3981
3982 /*debug_variables(ctx, vc_head, program); */
3983
3984 /* We're done with the parsed binary array */
3985 var_cache_destroy (&vc_head);
3986
3987 _mesa_free (parsed);
Brian Paul45703642005-10-29 15:52:31 +00003988
Brian Paul8c41a142005-11-19 15:36:28 +00003989 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
3990 * to size [ap.Base.NumInstructions].
3991 */
Brian Paula7543902006-08-24 21:58:32 +00003992 program->Base.Instructions
3993 = _mesa_realloc_instructions(program->Base.Instructions,
3994 MAX_INSTRUCTIONS,
3995 program->Base.NumInstructions);
3996
Brian Paul45703642005-10-29 15:52:31 +00003997 return !err;
Jouk Jansen40322e12004-04-05 08:50:36 +00003998}
Brian Paul8c41a142005-11-19 15:36:28 +00003999
4000
4001
4002void
4003_mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
4004 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00004005 struct gl_fragment_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00004006{
4007 struct arb_program ap;
4008 GLuint i;
4009
4010 ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
Brian Paul95801792005-12-06 15:41:43 +00004011 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004012 /* Error in the program. Just return. */
4013 return;
4014 }
4015
4016 /* Copy the relevant contents of the arb_program struct into the
4017 * fragment_program struct.
4018 */
4019 program->Base.String = ap.Base.String;
4020 program->Base.NumInstructions = ap.Base.NumInstructions;
4021 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4022 program->Base.NumParameters = ap.Base.NumParameters;
4023 program->Base.NumAttributes = ap.Base.NumAttributes;
4024 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00004025 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
4026 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
4027 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
4028 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
4029 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian Paul8c41a142005-11-19 15:36:28 +00004030 program->NumAluInstructions = ap.NumAluInstructions;
4031 program->NumTexInstructions = ap.NumTexInstructions;
4032 program->NumTexIndirections = ap.NumTexIndirections;
Brian Paul006e1832006-03-29 15:15:37 +00004033 program->NumNativeAluInstructions = ap.NumAluInstructions;
4034 program->NumNativeTexInstructions = ap.NumTexInstructions;
4035 program->NumNativeTexIndirections = ap.NumTexIndirections;
Brian Paul8c41a142005-11-19 15:36:28 +00004036 program->Base.InputsRead = ap.Base.InputsRead;
4037 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4038 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
4039 program->TexturesUsed[i] = ap.TexturesUsed[i];
4040 program->FogOption = ap.FogOption;
4041
4042 if (program->Base.Instructions)
4043 _mesa_free(program->Base.Instructions);
4044 program->Base.Instructions = ap.Base.Instructions;
4045
4046 if (program->Base.Parameters)
4047 _mesa_free_parameter_list(program->Base.Parameters);
4048 program->Base.Parameters = ap.Base.Parameters;
4049
4050#if DEBUG_FP
Brian Paul11a54c32006-11-15 19:54:25 +00004051 _mesa_printf("____________Fragment program %u ________\n", program->Base.ID);
4052 _mesa_print_program(&program->Base);
Brian Paul8c41a142005-11-19 15:36:28 +00004053#endif
4054}
4055
4056
4057
4058/**
4059 * Parse the vertex program string. If success, update the given
4060 * vertex_program object with the new program. Else, leave the vertex_program
4061 * object unchanged.
4062 */
4063void
4064_mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
4065 const GLvoid *str, GLsizei len,
Brian Paul122629f2006-07-20 16:49:57 +00004066 struct gl_vertex_program *program)
Brian Paul8c41a142005-11-19 15:36:28 +00004067{
4068 struct arb_program ap;
4069
4070 ASSERT(target == GL_VERTEX_PROGRAM_ARB);
4071
Brian Paul95801792005-12-06 15:41:43 +00004072 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len, &ap)) {
Brian Paul8c41a142005-11-19 15:36:28 +00004073 /* Error in the program. Just return. */
4074 return;
4075 }
4076
4077 /* Copy the relevant contents of the arb_program struct into the
4078 * vertex_program struct.
4079 */
4080 program->Base.String = ap.Base.String;
4081 program->Base.NumInstructions = ap.Base.NumInstructions;
4082 program->Base.NumTemporaries = ap.Base.NumTemporaries;
4083 program->Base.NumParameters = ap.Base.NumParameters;
4084 program->Base.NumAttributes = ap.Base.NumAttributes;
4085 program->Base.NumAddressRegs = ap.Base.NumAddressRegs;
Roland Scheideggere6de1ed2006-08-30 11:55:18 +00004086 program->Base.NumNativeInstructions = ap.Base.NumNativeInstructions;
4087 program->Base.NumNativeTemporaries = ap.Base.NumNativeTemporaries;
4088 program->Base.NumNativeParameters = ap.Base.NumNativeParameters;
4089 program->Base.NumNativeAttributes = ap.Base.NumNativeAttributes;
4090 program->Base.NumNativeAddressRegs = ap.Base.NumNativeAddressRegs;
Brian Paul8c41a142005-11-19 15:36:28 +00004091 program->Base.InputsRead = ap.Base.InputsRead;
4092 program->Base.OutputsWritten = ap.Base.OutputsWritten;
4093 program->IsPositionInvariant = ap.HintPositionInvariant;
4094
4095 if (program->Base.Instructions)
4096 _mesa_free(program->Base.Instructions);
4097 program->Base.Instructions = ap.Base.Instructions;
4098
4099 if (program->Base.Parameters)
4100 _mesa_free_parameter_list(program->Base.Parameters);
4101 program->Base.Parameters = ap.Base.Parameters;
4102
4103#if DEBUG_VP
Roland Scheidegger54dac2c2007-02-09 00:36:40 +01004104 _mesa_printf("____________Vertex program %u __________\n", program->Base.Id);
Brian Paul8c41a142005-11-19 15:36:28 +00004105 _mesa_print_program(&program->Base);
4106#endif
4107}